OSDN Git Service

Jakub Jelinek <jj@ultra.linux.cz>
[pf3gnuchains/gcc-fork.git] / gcc / cp / typeck.c
1 /* Build expressions with type checking for C++ compiler.
2    Copyright (C) 1987, 88, 89, 92-98, 1999 Free Software Foundation, Inc.
3    Hacked by Michael Tiemann (tiemann@cygnus.com)
4
5 This file is part of GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22
23 /* This file is part of the C++ front end.
24    It contains routines to build C++ expressions given their operands,
25    including computing the types of the result, C and C++ specific error
26    checks, and some optimization.
27
28    There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
29    and to process initializations in declarations (since they work
30    like a strange sort of assignment).  */
31
32 #include "config.h"
33 #include "system.h"
34 #include "tree.h"
35 #include "rtl.h"
36 #include "cp-tree.h"
37 #include "flags.h"
38 #include "output.h"
39 #include "expr.h"
40 #include "toplev.h"
41
42 static tree convert_for_assignment PROTO((tree, tree, const char *, tree,
43                                           int));
44 static tree pointer_int_sum PROTO((enum tree_code, tree, tree));
45 static tree rationalize_conditional_expr PROTO((enum tree_code, tree));
46 static int comp_target_parms PROTO((tree, tree, int));
47 static int comp_ptr_ttypes_real PROTO((tree, tree, int));
48 static int comp_ptr_ttypes_const PROTO((tree, tree));
49 static int comp_ptr_ttypes_reinterpret PROTO((tree, tree));
50 static int comp_array_types PROTO((int (*) (tree, tree, int), tree,
51                                    tree, int));
52 static tree common_base_type PROTO((tree, tree));
53 #if 0
54 static tree convert_sequence PROTO((tree, tree));
55 #endif
56 static tree lookup_anon_field PROTO((tree, tree));
57 static tree pointer_diff PROTO((tree, tree, tree));
58 static tree build_component_addr PROTO((tree, tree));
59 static tree qualify_type PROTO((tree, tree));
60 static tree get_delta_difference PROTO((tree, tree, int));
61 static int comp_cv_target_types PROTO((tree, tree, int));
62 static void casts_away_constness_r PROTO((tree *, tree *));
63 static int casts_away_constness PROTO ((tree, tree));
64
65 /* Return the target type of TYPE, which meas return T for:
66    T*, T&, T[], T (...), and otherwise, just T.  */
67
68 tree
69 target_type (type)
70      tree type;
71 {
72   if (TREE_CODE (type) == REFERENCE_TYPE)
73     type = TREE_TYPE (type);
74   while (TREE_CODE (type) == POINTER_TYPE
75          || TREE_CODE (type) == ARRAY_TYPE
76          || TREE_CODE (type) == FUNCTION_TYPE
77          || TREE_CODE (type) == METHOD_TYPE
78          || TREE_CODE (type) == OFFSET_TYPE)
79     type = TREE_TYPE (type);
80   return type;
81 }
82
83 /* Do `exp = require_complete_type (exp);' to make sure exp
84    does not have an incomplete type.  (That includes void types.)
85    Returns the error_mark_node if the VALUE does not have
86    complete type when this function returns.  */
87
88 tree
89 require_complete_type (value)
90      tree value;
91 {
92   tree type;
93
94   if (processing_template_decl || value == error_mark_node)
95     return value;
96
97   if (TREE_CODE (value) == OVERLOAD)
98     type = unknown_type_node;
99   else
100     type = TREE_TYPE (value);
101
102   /* First, detect a valid value with a complete type.  */
103   if (TYPE_SIZE (type) != 0
104       && TYPE_SIZE (type) != size_zero_node
105       && ! (TYPE_LANG_SPECIFIC (type)
106             && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type))
107             && TYPE_SIZE (SIGNATURE_TYPE (type)) == 0))
108     return value;
109
110   /* If we see X::Y, we build an OFFSET_TYPE which has
111      not been laid out.  Try to avoid an error by interpreting
112      it as this->X::Y, if reasonable.  */
113   if (TREE_CODE (value) == OFFSET_REF
114       && current_class_ref != 0
115       && TREE_OPERAND (value, 0) == current_class_ref)
116     {
117       tree base, member = TREE_OPERAND (value, 1);
118       tree basetype = TYPE_OFFSET_BASETYPE (type);
119       my_friendly_assert (TREE_CODE (member) == FIELD_DECL, 305);
120       base = convert_pointer_to (basetype, current_class_ptr);
121       value = build (COMPONENT_REF, TREE_TYPE (member),
122                      build_indirect_ref (base, NULL_PTR), member);
123       return require_complete_type (value);
124     }
125
126   if (complete_type_or_else (type, value))
127     return value;
128   else
129     return error_mark_node;
130 }
131
132 /* Makes sure EXPR is a complete type when used in a void context, like a
133    whole expression, or lhs of a comma operator. Issue a diagnostic and
134    return error_mark_node on failure. This is a little tricky, because some
135    valid void types look stunningly similar to invalid void types. We err on
136    the side of caution */
137
138 tree
139 require_complete_type_in_void (expr)
140      tree expr;
141 {
142   switch (TREE_CODE (expr))
143     {
144     case COND_EXPR:
145       {
146         tree op;
147         
148         op = TREE_OPERAND (expr,2);
149         op = require_complete_type_in_void (op);
150         TREE_OPERAND (expr,2) = op;
151         if (op == error_mark_node)
152           {
153             expr = op;
154             break;
155           }
156         
157         /* fallthrough */
158       }
159     
160     case COMPOUND_EXPR:
161       {
162         tree op;
163         
164         op = TREE_OPERAND (expr,1);
165         op = require_complete_type_in_void (op);
166         TREE_OPERAND (expr,1) = op;
167         if (op == error_mark_node)
168           {
169             expr = op;
170             break;
171           }
172         
173         break;
174       }
175     
176     case NON_LVALUE_EXPR:
177     case NOP_EXPR:
178       {
179         tree op;
180         
181         op = TREE_OPERAND (expr,0);
182         op = require_complete_type_in_void (op);
183         TREE_OPERAND (expr,0) = op;
184         if (op == error_mark_node)
185           {
186             expr = op;
187             break;
188           }
189         break;
190       }
191     
192     case CALL_EXPR:   /* function call return can be ignored */
193     case RTL_EXPR:    /* RTL nodes have no value */
194     case DELETE_EXPR: /* delete expressions have no type */
195     case VEC_DELETE_EXPR:
196     case INTEGER_CST: /* used for null pointer */
197     case EXIT_EXPR:   /* have no return */
198     case LOOP_EXPR:   /* have no return */
199     case BIND_EXPR:   /* have no return */
200     case THROW_EXPR:  /* have no return */
201     case MODIFY_EXPR: /* sometimes this has a void type, but that's ok */
202     case CONVERT_EXPR:  /* sometimes has a void type */
203       break;
204     
205     case INDIRECT_REF:
206       {
207         tree op = TREE_OPERAND (expr,0);
208         
209         /* Calling a function returning a reference has an implicit
210            dereference applied. We don't want to make that an error. */
211         if (TREE_CODE (op) == CALL_EXPR
212             && TREE_CODE (TREE_TYPE (op)) == REFERENCE_TYPE)
213           break;
214         /* else fallthrough */
215       }
216     
217     default:
218       expr = require_complete_type (expr);
219       break;
220     }
221
222   return expr;
223 }
224
225 /* Try to complete TYPE, if it is incomplete.  For example, if TYPE is
226    a template instantiation, do the instantiation.  Returns TYPE,
227    whether or not it could be completed, unless something goes
228    horribly wrong, in which case the error_mark_node is returned.  */
229
230 tree
231 complete_type (type)
232      tree type;
233 {
234   if (type == NULL_TREE)
235     /* Rather than crash, we return something sure to cause an error
236        at some point.  */
237     return error_mark_node;
238
239   if (type == error_mark_node || TYPE_SIZE (type) != NULL_TREE)
240     ;
241   else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
242     {
243       tree t = complete_type (TREE_TYPE (type));
244       if (TYPE_SIZE (t) != NULL_TREE && ! processing_template_decl)
245         layout_type (type);
246       TYPE_NEEDS_CONSTRUCTING (type)
247         = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
248       TYPE_NEEDS_DESTRUCTOR (type)
249         = TYPE_NEEDS_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
250     }
251   else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
252     instantiate_class_template (TYPE_MAIN_VARIANT (type));
253
254   return type;
255 }
256
257 /* Like complete_type, but issue an error if the TYPE cannot be
258    completed.  VALUE is used for informative diagnostics.
259    Returns NULL_TREE if the type cannot be made complete.  */
260
261 tree
262 complete_type_or_else (type, value)
263      tree type;
264      tree value;
265 {
266   type = complete_type (type);
267   if (type == error_mark_node)
268     /* We already issued an error.  */
269     return NULL_TREE;
270   else if (!TYPE_SIZE (type) || TYPE_SIZE (type) == size_zero_node)
271     {
272       incomplete_type_error (value, type);
273       return NULL_TREE;
274     }
275   else
276     return type;
277 }
278
279 /* Return truthvalue of whether type of EXP is instantiated.  */
280
281 int
282 type_unknown_p (exp)
283      tree exp;
284 {
285   return (TREE_CODE (exp) == OVERLOAD
286           || TREE_CODE (exp) == TREE_LIST
287           || TREE_TYPE (exp) == unknown_type_node
288           || (TREE_CODE (TREE_TYPE (exp)) == OFFSET_TYPE
289               && TREE_TYPE (TREE_TYPE (exp)) == unknown_type_node));
290 }
291
292 /* Return truthvalue of whether T is function (or pfn) type.  */
293
294 int
295 fntype_p (t)
296      tree t;
297 {
298   return (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE
299           || (TREE_CODE (t) == POINTER_TYPE
300               && (TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE
301                   || TREE_CODE (TREE_TYPE (t)) == METHOD_TYPE)));
302 }
303
304 /* Return a variant of TYPE which has all the type qualifiers of LIKE
305    as well as those of TYPE.  */
306
307 static tree
308 qualify_type (type, like)
309      tree type, like;
310 {
311   /* @@ Must do member pointers here.  */
312   return cp_build_qualified_type (type, (CP_TYPE_QUALS (type) 
313                                          | CP_TYPE_QUALS (like)));
314 }
315 \f
316 /* Return the common type of two parameter lists.
317    We assume that comptypes has already been done and returned 1;
318    if that isn't so, this may crash.
319
320    As an optimization, free the space we allocate if the parameter
321    lists are already common.  */
322
323 tree
324 commonparms (p1, p2)
325      tree p1, p2;
326 {
327   tree oldargs = p1, newargs, n;
328   int i, len;
329   int any_change = 0;
330   char *first_obj = (char *) oballoc (0);
331
332   len = list_length (p1);
333   newargs = tree_last (p1);
334
335   if (newargs == void_list_node)
336     i = 1;
337   else
338     {
339       i = 0;
340       newargs = 0;
341     }
342
343   for (; i < len; i++)
344     newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
345
346   n = newargs;
347
348   for (i = 0; p1;
349        p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
350     {
351       if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
352         {
353           TREE_PURPOSE (n) = TREE_PURPOSE (p1);
354           any_change = 1;
355         }
356       else if (! TREE_PURPOSE (p1))
357         {
358           if (TREE_PURPOSE (p2))
359             {
360               TREE_PURPOSE (n) = TREE_PURPOSE (p2);
361               any_change = 1;
362             }
363         }
364       else
365         {
366           if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
367             any_change = 1;
368           TREE_PURPOSE (n) = TREE_PURPOSE (p2);
369         }
370       if (TREE_VALUE (p1) != TREE_VALUE (p2))
371         {
372           any_change = 1;
373           TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
374         }
375       else
376         TREE_VALUE (n) = TREE_VALUE (p1);
377     }
378   if (! any_change)
379     {
380       obfree (first_obj);
381       return oldargs;
382     }
383
384   return newargs;
385 }
386
387 /* Given a type, perhaps copied for a typedef,
388    find the "original" version of it.  */
389 tree
390 original_type (t)
391      tree t;
392 {
393   while (TYPE_NAME (t) != NULL_TREE)
394     {
395       tree x = TYPE_NAME (t);
396       if (TREE_CODE (x) != TYPE_DECL)
397         break;
398       x = DECL_ORIGINAL_TYPE (x);
399       if (x == NULL_TREE)
400         break;
401       t = x;
402     }
403   return t;
404 }
405
406 /* T1 and T2 are arithmetic or enumeration types.  Return the type
407    that will result from the "usual arithmetic converions" on T1 and
408    T2 as described in [expr].  */
409
410 tree
411 type_after_usual_arithmetic_conversions (t1, t2)
412      tree t1;
413      tree t2;
414 {
415   enum tree_code code1 = TREE_CODE (t1);
416   enum tree_code code2 = TREE_CODE (t2);
417   tree attributes;
418
419   /* FIXME: Attributes.  */
420   my_friendly_assert (ARITHMETIC_TYPE_P (t1) 
421                       || TREE_CODE (t1) == ENUMERAL_TYPE,
422                       19990725);
423   my_friendly_assert (ARITHMETIC_TYPE_P (t2) 
424                       || TREE_CODE (t2) == ENUMERAL_TYPE,
425                       19990725);
426
427   /* In what follows, we slightly generalize the rules given in [expr]
428      so as to deal with `long long'.  First, merge the attributes.  */
429   attributes = merge_machine_type_attributes (t1, t2);
430
431   /* If only one is real, use it as the result.  */
432   if (code1 == REAL_TYPE && code2 != REAL_TYPE)
433     return build_type_attribute_variant (t1, attributes);
434   if (code2 == REAL_TYPE && code1 != REAL_TYPE)
435     return build_type_attribute_variant (t2, attributes);
436
437   /* Perform the integral promotions.  */
438   if (code1 != REAL_TYPE)
439     {
440       t1 = type_promotes_to (t1);
441       t2 = type_promotes_to (t2);
442     }
443
444   /* Both real or both integers; use the one with greater precision.  */
445   if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
446     return build_type_attribute_variant (t1, attributes);
447   else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
448     return build_type_attribute_variant (t2, attributes);
449
450   if (code1 != REAL_TYPE)
451     {
452       /* If one is unsigned long long, then convert the other to unsigned
453          long long.  */
454       if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
455           || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
456         return build_type_attribute_variant (long_long_unsigned_type_node,
457                                              attributes);
458       /* If one is a long long, and the other is an unsigned long, and
459          long long can represent all the values of an unsigned long, then
460          convert to a long long.  Otherwise, convert to an unsigned long
461          long.  Otherwise, if either operand is long long, convert the
462          other to long long.
463          
464          Since we're here, we know the TYPE_PRECISION is the same;
465          therefore converting to long long cannot represent all the values
466          of an unsigned long, so we choose unsigned long long in that
467          case.  */
468       if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
469           || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
470         {
471           tree t = ((TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
472                     ? long_long_unsigned_type_node 
473                     : long_long_integer_type_node);
474           return build_type_attribute_variant (t, attributes);
475         }
476       
477       /* Go through the same procedure, but for longs.  */
478       if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
479           || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
480         return build_type_attribute_variant (long_unsigned_type_node,
481                                              attributes);
482       if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
483           || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
484         {
485           tree t = ((TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
486                     ? long_unsigned_type_node : long_integer_type_node);
487           return build_type_attribute_variant (t, attributes);
488         }
489       /* Otherwise prefer the unsigned one.  */
490       if (TREE_UNSIGNED (t1))
491         return build_type_attribute_variant (t1, attributes);
492       else
493         return build_type_attribute_variant (t2, attributes);
494     }
495   else
496     {
497       if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
498           || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
499         return build_type_attribute_variant (long_double_type_node,
500                                              attributes);
501       if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
502           || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
503         return build_type_attribute_variant (double_type_node,
504                                              attributes);
505       else 
506         return build_type_attribute_variant (float_type_node,
507                                              attributes);
508     }
509 }
510
511 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
512    ARG1 and ARG2 are the values with those types.  The LOCATION is a
513    string describing the current location, in case an error occurs.  */
514
515 tree 
516 composite_pointer_type (t1, t2, arg1, arg2, location)
517      tree t1;
518      tree t2;
519      tree arg1;
520      tree arg2;
521      char* location;
522 {
523   tree result_type;
524
525   /* [expr.rel]
526
527      If one operand is a null pointer constant, the composite pointer
528      type is the type of the other operand.  */
529   if (null_ptr_cst_p (arg1))
530     return t2;
531   if (null_ptr_cst_p (arg2))
532     return t1;
533  
534   /* Deal with pointer-to-member functions in the same way as we deal
535      with pointers to functions. */
536   if (TYPE_PTRMEMFUNC_P (t1))
537     t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
538   if (TYPE_PTRMEMFUNC_P (t2))
539     t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
540   
541   if (comp_target_types (t1, t2, 1))
542     result_type = common_type (t1, t2);
543   else if (TYPE_MAIN_VARIANT (TREE_TYPE (t1)) == void_type_node)
544     {
545       if (pedantic && TREE_CODE (t2) == FUNCTION_TYPE)
546         pedwarn ("ANSI C++ forbids %s between `void *' and function pointer",
547                  location);
548       result_type = qualify_type (t1, t2);
549     }
550   else if (TYPE_MAIN_VARIANT (TREE_TYPE (t2)) == void_type_node)
551     {
552       if (pedantic && TREE_CODE (t1) == FUNCTION_TYPE)
553         pedwarn ("ANSI C++ forbids %s between `void *' and function pointer",
554                  location);
555       result_type = qualify_type (t2, t1);
556     }
557   /* C++ */
558   else if (same_or_base_type_p (t2, t1))
559     result_type = t2;
560   else if (IS_AGGR_TYPE (TREE_TYPE (t1))
561            && IS_AGGR_TYPE (TREE_TYPE (t2))
562            && (result_type = common_base_type (TREE_TYPE (t1),
563                                                TREE_TYPE (t2))))
564     {
565       if (result_type == error_mark_node)
566         {
567           cp_error ("common base type of types `%T' and `%T' is ambiguous",
568                     TREE_TYPE (t1), TREE_TYPE (t2));
569           result_type = ptr_type_node;
570         }
571       else
572         {
573           if (pedantic
574               && result_type != TREE_TYPE (t1)
575               && result_type != TREE_TYPE (t2))
576             cp_pedwarn ("`%T' and `%T' converted to `%T *' in %s",
577                         t1, t2, result_type, location);
578           
579           result_type = build_pointer_type (result_type);
580         }
581     }
582   else
583     {
584       cp_pedwarn ("pointer type mismatch in %s", location);
585       result_type = ptr_type_node;
586     }
587
588   return result_type;
589 }
590
591 /* Return the common type of two types.
592    We assume that comptypes has already been done and returned 1;
593    if that isn't so, this may crash.
594
595    This is the type for the result of most arithmetic operations
596    if the operands have the given two types.
597
598    We do not deal with enumeral types here because they have already been
599    converted to integer types.  */
600
601 tree
602 common_type (t1, t2)
603      tree t1, t2;
604 {
605   register enum tree_code code1;
606   register enum tree_code code2;
607   tree attributes;
608
609   /* Save time if the two types are the same.  */
610   if (t1 == t2)
611     return t1;
612   t1 = original_type (t1);
613   t2 = original_type (t2);
614   if (t1 == t2)
615     return t1;
616
617   /* If one type is nonsense, use the other.  */
618   if (t1 == error_mark_node)
619     return t2;
620   if (t2 == error_mark_node)
621     return t1;
622
623   if ((ARITHMETIC_TYPE_P (t1) || TREE_CODE (t1) == ENUMERAL_TYPE)
624       && (ARITHMETIC_TYPE_P (t2) || TREE_CODE (t2) == ENUMERAL_TYPE))
625     return type_after_usual_arithmetic_conversions (t1, t2);
626
627   /* Merge the attributes.  */
628   attributes = merge_machine_type_attributes (t1, t2);
629
630   /* Treat an enum type as the unsigned integer type of the same width.  */
631
632   if (TREE_CODE (t1) == ENUMERAL_TYPE)
633     t1 = type_for_size (TYPE_PRECISION (t1), 1);
634   if (TREE_CODE (t2) == ENUMERAL_TYPE)
635     t2 = type_for_size (TYPE_PRECISION (t2), 1);
636
637   if (TYPE_PTRMEMFUNC_P (t1))
638     t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
639   if (TYPE_PTRMEMFUNC_P (t2))
640     t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
641
642   code1 = TREE_CODE (t1);
643   code2 = TREE_CODE (t2);
644
645   /* If one type is complex, form the common type of the non-complex
646      components, then make that complex.  Use T1 or T2 if it is the
647      required type.  */
648   if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
649     {
650       tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
651       tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
652       tree subtype = common_type (subtype1, subtype2);
653
654       if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
655         return build_type_attribute_variant (t1, attributes);
656       else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
657         return build_type_attribute_variant (t2, attributes);
658       else
659         return build_type_attribute_variant (build_complex_type (subtype),
660                                              attributes);
661     }
662
663   switch (code1)
664     {
665     case INTEGER_TYPE:
666     case REAL_TYPE:
667       /* We should have called type_after_usual_arithmetic_conversions
668          above.  */
669       my_friendly_abort (19990725);
670       break;
671
672     case POINTER_TYPE:
673     case REFERENCE_TYPE:
674       /* For two pointers, do this recursively on the target type,
675          and combine the qualifiers of the two types' targets.  */
676       /* This code was turned off; I don't know why.
677          But ANSI C++ specifies doing this with the qualifiers.
678          So I turned it on again.  */
679       {
680         tree tt1 = TREE_TYPE (t1);
681         tree tt2 = TREE_TYPE (t2);
682         tree b1, b2;
683         int type_quals;
684         tree target;
685
686         if (TREE_CODE (tt1) == OFFSET_TYPE)
687           {
688             b1 = TYPE_OFFSET_BASETYPE (tt1);
689             b2 = TYPE_OFFSET_BASETYPE (tt2);
690             tt1 = TREE_TYPE (tt1);
691             tt2 = TREE_TYPE (tt2);
692           }
693         else
694           b1 = b2 = NULL_TREE;
695
696         type_quals = (CP_TYPE_QUALS (tt1) | CP_TYPE_QUALS (tt2));
697         tt1 = TYPE_MAIN_VARIANT (tt1);
698         tt2 = TYPE_MAIN_VARIANT (tt2);
699
700         if (tt1 == tt2)
701           target = tt1;
702         else if (b1)
703           {
704             compiler_error ("common_type called with uncommon member types");
705             target = tt1;
706           }
707         else if (tt1 == void_type_node || tt2 == void_type_node)
708           target = void_type_node;
709         else if (tt1 == unknown_type_node)
710           target = tt2;
711         else if (tt2 == unknown_type_node)
712           target = tt1;
713         else
714           target = common_type (tt1, tt2);
715
716         target = cp_build_qualified_type (target, type_quals);
717
718         if (b1)
719           {
720             if (same_type_p (b1, b2)
721                 || (DERIVED_FROM_P (b1, b2) && binfo_or_else (b1, b2)))
722               target = build_offset_type (b2, target);
723             else if (binfo_or_else (b2, b1))
724               target = build_offset_type (b1, target);
725           }
726
727         if (code1 == POINTER_TYPE)
728           t1 = build_pointer_type (target);
729         else
730           t1 = build_reference_type (target);
731         t1 = build_type_attribute_variant (t1, attributes);
732
733         if (TREE_CODE (target) == METHOD_TYPE)
734           t1 = build_ptrmemfunc_type (t1);
735
736         return t1;
737       }
738
739     case ARRAY_TYPE:
740       {
741         tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
742         /* Save space: see if the result is identical to one of the args.  */
743         if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
744           return build_type_attribute_variant (t1, attributes);
745         if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
746           return build_type_attribute_variant (t2, attributes);
747         /* Merge the element types, and have a size if either arg has one.  */
748         t1 = build_cplus_array_type
749           (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
750         return build_type_attribute_variant (t1, attributes);
751       }
752
753     case FUNCTION_TYPE:
754       /* Function types: prefer the one that specified arg types.
755          If both do, merge the arg types.  Also merge the return types.  */
756       {
757         tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
758         tree p1 = TYPE_ARG_TYPES (t1);
759         tree p2 = TYPE_ARG_TYPES (t2);
760         tree rval, raises;
761
762         /* Save space: see if the result is identical to one of the args.  */
763         if (valtype == TREE_TYPE (t1) && ! p2)
764           return build_type_attribute_variant (t1, attributes);
765         if (valtype == TREE_TYPE (t2) && ! p1)
766           return build_type_attribute_variant (t2, attributes);
767
768         /* Simple way if one arg fails to specify argument types.  */
769         if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
770           {
771             rval = build_function_type (valtype, p2);
772             if ((raises = TYPE_RAISES_EXCEPTIONS (t2)))
773               rval = build_exception_variant (rval, raises);
774             return build_type_attribute_variant (rval, attributes);
775           }
776         raises = TYPE_RAISES_EXCEPTIONS (t1);
777         if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
778           {
779             rval = build_function_type (valtype, p1);
780             if (raises)
781               rval = build_exception_variant (rval, raises);
782             return build_type_attribute_variant (rval, attributes);
783           }
784
785         rval = build_function_type (valtype, commonparms (p1, p2));
786         rval = build_exception_variant (rval, raises);
787         return build_type_attribute_variant (rval, attributes);
788       }
789
790     case RECORD_TYPE:
791     case UNION_TYPE:
792       t1 = TYPE_MAIN_VARIANT (t1);
793       t2 = TYPE_MAIN_VARIANT (t2);
794
795       if (DERIVED_FROM_P (t1, t2) && binfo_or_else (t1, t2))
796         return build_type_attribute_variant (t1, attributes);
797       else if (binfo_or_else (t2, t1))
798         return build_type_attribute_variant (t2, attributes);
799       else
800         {
801           compiler_error ("common_type called with uncommon aggregate types");
802           return error_mark_node;
803         }
804
805     case METHOD_TYPE:
806       if (TREE_CODE (TREE_TYPE (t1)) == TREE_CODE (TREE_TYPE (t2)))
807         {
808           /* Get this value the long way, since TYPE_METHOD_BASETYPE
809              is just the main variant of this.  */
810           tree basetype;
811           tree raises, t3;
812
813           tree b1 = TYPE_OFFSET_BASETYPE (t1);
814           tree b2 = TYPE_OFFSET_BASETYPE (t2);
815
816           if (same_type_p (b1, b2)
817               || (DERIVED_FROM_P (b1, b2) && binfo_or_else (b1, b2)))
818             basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t2)));
819           else
820             {
821               if (binfo_or_else (b2, b1) == NULL_TREE)
822                 compiler_error ("common_type called with uncommon method types");
823               basetype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t1)));
824             }
825
826           raises = TYPE_RAISES_EXCEPTIONS (t1);
827
828           /* If this was a member function type, get back to the
829              original type of type member function (i.e., without
830              the class instance variable up front.  */
831           t1 = build_function_type (TREE_TYPE (t1),
832                                     TREE_CHAIN (TYPE_ARG_TYPES (t1)));
833           t2 = build_function_type (TREE_TYPE (t2),
834                                     TREE_CHAIN (TYPE_ARG_TYPES (t2)));
835           t3 = common_type (t1, t2);
836           t3 = build_cplus_method_type (basetype, TREE_TYPE (t3),
837                                         TYPE_ARG_TYPES (t3));
838           t1 = build_exception_variant (t3, raises);
839         }
840       else
841         compiler_error ("common_type called with uncommon method types");
842
843       return build_type_attribute_variant (t1, attributes);
844
845     case OFFSET_TYPE:
846       /* Pointers to members should now be handled by the POINTER_TYPE
847          case above.  */
848       my_friendly_abort (990325);
849
850     default:
851       return build_type_attribute_variant (t1, attributes);
852     }
853 }
854 \f
855 /* Return 1 if TYPE1 and TYPE2 raise the same exceptions.  */
856
857 int
858 compexcepttypes (t1, t2)
859      tree t1, t2;
860 {
861   return TYPE_RAISES_EXCEPTIONS (t1) == TYPE_RAISES_EXCEPTIONS (t2);
862 }
863
864 /* Compare the array types T1 and T2, using CMP as the type comparison
865    function for the element types.  STRICT is as for comptypes.  */
866
867 static int
868 comp_array_types (cmp, t1, t2, strict)
869      register int (*cmp) PROTO((tree, tree, int));
870      tree t1, t2;
871      int strict;
872 {
873   tree d1;
874   tree d2;
875
876   if (t1 == t2)
877     return 1;
878
879   /* The type of the array elements must be the same.  */
880   if (!(TREE_TYPE (t1) == TREE_TYPE (t2)
881         || (*cmp) (TREE_TYPE (t1), TREE_TYPE (t2), 
882                    strict & ~COMPARE_REDECLARATION)))
883     return 0;
884
885   d1 = TYPE_DOMAIN (t1);
886   d2 = TYPE_DOMAIN (t2);
887
888   if (d1 == d2)
889     return 1;
890
891   /* If one of the arrays is dimensionless, and the other has a
892      dimension, they are of different types.  However, it is legal to
893      write:
894
895        extern int a[];
896        int a[3];
897
898      by [basic.link]: 
899
900        declarations for an array object can specify
901        array types that differ by the presence or absence of a major
902        array bound (_dcl.array_).  */
903   if (!d1 || !d2)
904     return strict & COMPARE_REDECLARATION;
905
906   /* Check that the dimensions are the same.  */
907   return (cp_tree_equal (TYPE_MIN_VALUE (d1),
908                          TYPE_MIN_VALUE (d2))
909           && cp_tree_equal (TYPE_MAX_VALUE (d1),
910                             TYPE_MAX_VALUE (d2)));
911 }
912
913 /* Return 1 if T1 and T2 are compatible types for assignment or
914    various other operations.  STRICT is a bitwise-or of the COMPARE_*
915    flags.  */
916
917 int
918 comptypes (t1, t2, strict)
919      tree t1;
920      tree t2;
921      int strict;
922 {
923   int attrval, val;
924   int orig_strict = strict;
925
926   /* The special exemption for redeclaring array types without an
927      array bound only applies at the top level:
928
929        extern int (*i)[];
930        int (*i)[8];
931
932      is not legal, for example.  */
933   strict &= ~COMPARE_REDECLARATION;
934
935   /* Suppress errors caused by previously reported errors */
936   if (t1 == t2)
937     return 1;
938
939   /* This should never happen.  */
940   my_friendly_assert (t1 != error_mark_node, 307);
941
942   if (t2 == error_mark_node)
943     return 0;
944
945   if (strict & COMPARE_RELAXED)
946     {
947       /* Treat an enum type as the unsigned integer type of the same width.  */
948
949       if (TREE_CODE (t1) == ENUMERAL_TYPE)
950         t1 = type_for_size (TYPE_PRECISION (t1), 1);
951       if (TREE_CODE (t2) == ENUMERAL_TYPE)
952         t2 = type_for_size (TYPE_PRECISION (t2), 1);
953
954       if (t1 == t2)
955         return 1;
956     }
957
958   if (TYPE_PTRMEMFUNC_P (t1))
959     t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
960   if (TYPE_PTRMEMFUNC_P (t2))
961     t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
962
963   /* Different classes of types can't be compatible.  */
964   if (TREE_CODE (t1) != TREE_CODE (t2))
965     return 0;
966
967   /* Qualifiers must match.  */
968   if (CP_TYPE_QUALS (t1) != CP_TYPE_QUALS (t2))
969     return 0;
970   if (strict == COMPARE_STRICT 
971       && TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
972     return 0;
973
974   /* Allow for two different type nodes which have essentially the same
975      definition.  Note that we already checked for equality of the type
976      qualifiers (just above).  */
977
978   if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
979     return 1;
980
981   /* ??? COMP_TYPE_ATTRIBUTES is currently useless for variables as each
982      attribute is its own main variant (`val' will remain 0).  */
983 #ifndef COMP_TYPE_ATTRIBUTES
984 #define COMP_TYPE_ATTRIBUTES(t1,t2)     1
985 #endif
986
987   if (strict & COMPARE_NO_ATTRIBUTES)
988     attrval = 1;
989   /* 1 if no need for warning yet, 2 if warning cause has been seen.  */
990   else if (! (attrval = COMP_TYPE_ATTRIBUTES (t1, t2)))
991      return 0;
992
993   /* 1 if no need for warning yet, 2 if warning cause has been seen.  */
994   val = 0;
995
996   switch (TREE_CODE (t1))
997     {
998     case TEMPLATE_TEMPLATE_PARM:
999       if (TEMPLATE_TYPE_IDX (t1) != TEMPLATE_TYPE_IDX (t2)
1000           || TEMPLATE_TYPE_LEVEL (t1) != TEMPLATE_TYPE_LEVEL (t2))
1001         return 0;
1002       if (! comp_template_parms (DECL_TEMPLATE_PARMS (TYPE_NAME (t1)),
1003                                  DECL_TEMPLATE_PARMS (TYPE_NAME (t2))))
1004         return 0;
1005       if (!TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t1) 
1006           && ! TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t2))
1007         return 1;
1008       /* Don't check inheritance.  */
1009       strict = COMPARE_STRICT;
1010       /* fall through */
1011
1012     case RECORD_TYPE:
1013     case UNION_TYPE:
1014       if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1015           && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1016               || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM))
1017         val = comp_template_args (TYPE_TI_ARGS (t1),
1018                                   TYPE_TI_ARGS (t2));
1019     look_hard:
1020       if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1021         val = 1;
1022       else if ((strict & COMPARE_RELAXED) && DERIVED_FROM_P (t2, t1))
1023         val = 1;
1024       break;
1025
1026     case OFFSET_TYPE:
1027       val = (comptypes (build_pointer_type (TYPE_OFFSET_BASETYPE (t1)),
1028                         build_pointer_type (TYPE_OFFSET_BASETYPE (t2)), strict)
1029              && comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict));
1030       break;
1031
1032     case METHOD_TYPE:
1033       if (! compexcepttypes (t1, t2))
1034         return 0;
1035
1036       /* This case is anti-symmetrical!
1037          One can pass a base member (or member function)
1038          to something expecting a derived member (or member function),
1039          but not vice-versa!  */
1040
1041       val = (comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict)
1042              && compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)));
1043       break;
1044
1045     case POINTER_TYPE:
1046     case REFERENCE_TYPE:
1047       t1 = TREE_TYPE (t1);
1048       t2 = TREE_TYPE (t2);
1049       /* first, check whether the referred types match with the
1050          required level of strictness */
1051       val = comptypes (t1, t2, strict);
1052       if (val)
1053         break;
1054       if (TREE_CODE (t1) == RECORD_TYPE 
1055           && TREE_CODE (t2) == RECORD_TYPE)
1056         goto look_hard;
1057       break;
1058
1059     case FUNCTION_TYPE:
1060       if (! compexcepttypes (t1, t2))
1061         return 0;
1062
1063       val = ((TREE_TYPE (t1) == TREE_TYPE (t2)
1064               || comptypes (TREE_TYPE (t1), TREE_TYPE (t2), strict))
1065              && compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)));
1066       break;
1067
1068     case ARRAY_TYPE:
1069       /* Target types must match incl. qualifiers.  We use ORIG_STRICT
1070          here since this is the one place where
1071          COMPARE_REDECLARATION should be used.  */
1072       val = comp_array_types (comptypes, t1, t2, orig_strict);
1073       break;
1074
1075     case TEMPLATE_TYPE_PARM:
1076       return TEMPLATE_TYPE_IDX (t1) == TEMPLATE_TYPE_IDX (t2)
1077         && TEMPLATE_TYPE_LEVEL (t1) == TEMPLATE_TYPE_LEVEL (t2);
1078
1079     case TYPENAME_TYPE:
1080       if (TYPE_IDENTIFIER (t1) != TYPE_IDENTIFIER (t2))
1081         return 0;
1082       return same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2));
1083
1084     case COMPLEX_TYPE:
1085       return same_type_p (TREE_TYPE (t1), TREE_TYPE (t2));
1086
1087     default:
1088       break;
1089     }
1090   return attrval == 2 && val == 1 ? 2 : val;
1091 }
1092
1093 /* Subroutine of comp_target-types.  Make sure that the cv-quals change
1094    only in the same direction as the target type.  */
1095
1096 static int
1097 comp_cv_target_types (ttl, ttr, nptrs)
1098      tree ttl, ttr;
1099      int nptrs;
1100 {
1101   int t;
1102
1103   if (!at_least_as_qualified_p (ttl, ttr)
1104       && !at_least_as_qualified_p (ttr, ttl))
1105     /* The qualifications are incomparable.  */
1106     return 0;
1107
1108   if (TYPE_MAIN_VARIANT (ttl) == TYPE_MAIN_VARIANT (ttr))
1109     return more_qualified_p (ttr, ttl) ? -1 : 1;
1110
1111   t = comp_target_types (ttl, ttr, nptrs);
1112   if ((t == 1 && at_least_as_qualified_p (ttl, ttr)) 
1113       || (t == -1 && at_least_as_qualified_p (ttr, ttl)))
1114     return t;
1115
1116   return 0;
1117 }
1118
1119 /* Return 1 or -1 if TTL and TTR are pointers to types that are equivalent,
1120    ignoring their qualifiers, 0 if not. Return 1 means that TTR can be
1121    converted to TTL. Return -1 means that TTL can be converted to TTR but
1122    not vice versa.
1123
1124    NPTRS is the number of pointers we can strip off and keep cool.
1125    This is used to permit (for aggr A, aggr B) A, B* to convert to A*,
1126    but to not permit B** to convert to A**.
1127
1128    This should go away.  Callers should use can_convert or something
1129    similar instead.  (jason 17 Apr 1997)  */
1130
1131 int
1132 comp_target_types (ttl, ttr, nptrs)
1133      tree ttl, ttr;
1134      int nptrs;
1135 {
1136   ttl = TYPE_MAIN_VARIANT (ttl);
1137   ttr = TYPE_MAIN_VARIANT (ttr);
1138   if (same_type_p (ttl, ttr))
1139     return 1;
1140
1141   if (TREE_CODE (ttr) != TREE_CODE (ttl))
1142     return 0;
1143
1144   if ((TREE_CODE (ttr) == POINTER_TYPE
1145        || TREE_CODE (ttr) == REFERENCE_TYPE)
1146       /* If we get a pointer with nptrs == 0, we don't allow any tweaking
1147          of the type pointed to.  This is necessary for reference init
1148          semantics.  We won't get here from a previous call with nptrs == 1;
1149          for multi-level pointers we end up in comp_ptr_ttypes.  */
1150       && nptrs > 0)
1151     {
1152       int is_ptr = TREE_CODE (ttr) == POINTER_TYPE;
1153
1154       ttl = TREE_TYPE (ttl);
1155       ttr = TREE_TYPE (ttr);
1156
1157       if (is_ptr)
1158         {
1159           if (TREE_CODE (ttl) == UNKNOWN_TYPE
1160               || TREE_CODE (ttr) == UNKNOWN_TYPE)
1161             return 1;
1162           else if (TREE_CODE (ttl) == VOID_TYPE
1163                    && TREE_CODE (ttr) != FUNCTION_TYPE
1164                    && TREE_CODE (ttr) != METHOD_TYPE
1165                    && TREE_CODE (ttr) != OFFSET_TYPE)
1166             return 1;
1167           else if (TREE_CODE (ttr) == VOID_TYPE
1168                    && TREE_CODE (ttl) != FUNCTION_TYPE
1169                    && TREE_CODE (ttl) != METHOD_TYPE
1170                    && TREE_CODE (ttl) != OFFSET_TYPE)
1171             return -1;
1172           else if (TREE_CODE (ttl) == POINTER_TYPE
1173                    || TREE_CODE (ttl) == ARRAY_TYPE)
1174             {
1175               if (comp_ptr_ttypes (ttl, ttr))
1176                 return 1;
1177               else if (comp_ptr_ttypes (ttr, ttl))
1178                 return -1;
1179               return 0;
1180             }
1181         }
1182
1183       /* Const and volatile mean something different for function types,
1184          so the usual checks are not appropriate.  */
1185       if (TREE_CODE (ttl) == FUNCTION_TYPE || TREE_CODE (ttl) == METHOD_TYPE)
1186         return comp_target_types (ttl, ttr, nptrs - 1);
1187
1188       return comp_cv_target_types (ttl, ttr, nptrs - 1);
1189     }
1190
1191   if (TREE_CODE (ttr) == ARRAY_TYPE)
1192     return comp_array_types (comp_target_types, ttl, ttr, COMPARE_STRICT);
1193   else if (TREE_CODE (ttr) == FUNCTION_TYPE || TREE_CODE (ttr) == METHOD_TYPE)
1194     {
1195       tree argsl, argsr;
1196       int saw_contra = 0;
1197
1198       if (pedantic)
1199         {
1200           if (!same_type_p (TREE_TYPE (ttl), TREE_TYPE (ttr)))
1201             return 0;
1202         }
1203       else
1204         {
1205           switch (comp_target_types (TREE_TYPE (ttl), TREE_TYPE (ttr), -1))
1206             {
1207             case 0:
1208               return 0;
1209             case -1:
1210               saw_contra = 1;
1211             }
1212         }
1213
1214       argsl = TYPE_ARG_TYPES (ttl);
1215       argsr = TYPE_ARG_TYPES (ttr);
1216
1217       /* Compare 'this' here, not in comp_target_parms.  */
1218       if (TREE_CODE (ttr) == METHOD_TYPE)
1219         {
1220           tree tl = TYPE_METHOD_BASETYPE (ttl);
1221           tree tr = TYPE_METHOD_BASETYPE (ttr);
1222
1223           if (!same_or_base_type_p (tr, tl))
1224             {
1225               if (same_or_base_type_p (tl, tr))
1226                 saw_contra = 1;
1227               else
1228                 return 0;
1229             }
1230
1231           argsl = TREE_CHAIN (argsl);
1232           argsr = TREE_CHAIN (argsr);
1233         }
1234
1235         switch (comp_target_parms (argsl, argsr, 1))
1236           {
1237           case 0:
1238             return 0;
1239           case -1:
1240             saw_contra = 1;
1241           }
1242
1243         return saw_contra ? -1 : 1;
1244     }
1245   /* for C++ */
1246   else if (TREE_CODE (ttr) == OFFSET_TYPE)
1247     {
1248       int base;
1249
1250       /* Contravariance: we can assign a pointer to base member to a pointer
1251          to derived member.  Note difference from simple pointer case, where
1252          we can pass a pointer to derived to a pointer to base.  */
1253       if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttr),
1254                                TYPE_OFFSET_BASETYPE (ttl)))
1255         base = 1;
1256       else if (same_or_base_type_p (TYPE_OFFSET_BASETYPE (ttl),
1257                                     TYPE_OFFSET_BASETYPE (ttr)))
1258         {
1259           tree tmp = ttl;
1260           ttl = ttr;
1261           ttr = tmp;
1262           base = -1;
1263         }
1264       else
1265         return 0;
1266
1267       ttl = TREE_TYPE (ttl);
1268       ttr = TREE_TYPE (ttr);
1269
1270       if (TREE_CODE (ttl) == POINTER_TYPE
1271           || TREE_CODE (ttl) == ARRAY_TYPE)
1272         {
1273           if (comp_ptr_ttypes (ttl, ttr))
1274             return base;
1275           return 0;
1276         }
1277       else
1278         {
1279           if (comp_cv_target_types (ttl, ttr, nptrs) == 1)
1280             return base;
1281           return 0;
1282         }
1283     }
1284   else if (IS_AGGR_TYPE (ttl))
1285     {
1286       if (nptrs < 0)
1287         return 0;
1288       if (same_or_base_type_p (build_pointer_type (ttl), 
1289                                build_pointer_type (ttr)))
1290         return 1;
1291       if (same_or_base_type_p (build_pointer_type (ttr), 
1292                                build_pointer_type (ttl)))
1293         return -1;
1294       return 0;
1295     }
1296
1297   return 0;
1298 }
1299
1300 /* Returns 1 if TYPE1 is at least as qualified as TYPE2.  */
1301
1302 int
1303 at_least_as_qualified_p (type1, type2)
1304      tree type1;
1305      tree type2;
1306 {
1307   /* All qualifiers for TYPE2 must also appear in TYPE1.  */
1308   return ((CP_TYPE_QUALS (type1) & CP_TYPE_QUALS (type2))
1309           == CP_TYPE_QUALS (type2));
1310 }
1311
1312 /* Returns 1 if TYPE1 is more qualified than TYPE2.  */
1313
1314 int
1315 more_qualified_p (type1, type2)
1316      tree type1;
1317      tree type2;
1318 {
1319   return (CP_TYPE_QUALS (type1) != CP_TYPE_QUALS (type2)
1320           && at_least_as_qualified_p (type1, type2));
1321 }
1322
1323 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1324    more cv-qualified that TYPE1, and 0 otherwise.  */
1325
1326 int
1327 comp_cv_qualification (type1, type2)
1328      tree type1;
1329      tree type2;
1330 {
1331   if (CP_TYPE_QUALS (type1) == CP_TYPE_QUALS (type2))
1332     return 0;
1333
1334   if (at_least_as_qualified_p (type1, type2))
1335     return 1;
1336
1337   else if (at_least_as_qualified_p (type2, type1))
1338     return -1;
1339
1340   return 0;
1341 }
1342
1343 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1344    subset of the cv-qualification signature of TYPE2, and the types
1345    are similar.  Returns -1 if the other way 'round, and 0 otherwise.  */
1346
1347 int
1348 comp_cv_qual_signature (type1, type2)
1349      tree type1;
1350      tree type2;
1351 {
1352   if (comp_ptr_ttypes_real (type2, type1, -1))
1353     return 1;
1354   else if (comp_ptr_ttypes_real (type1, type2, -1))
1355     return -1;
1356   else
1357     return 0;
1358 }
1359
1360 /* If two types share a common base type, return that basetype.
1361    If there is not a unique most-derived base type, this function
1362    returns ERROR_MARK_NODE.  */
1363
1364 static tree
1365 common_base_type (tt1, tt2)
1366      tree tt1, tt2;
1367 {
1368   tree best = NULL_TREE;
1369   int i;
1370
1371   /* If one is a baseclass of another, that's good enough.  */
1372   if (UNIQUELY_DERIVED_FROM_P (tt1, tt2))
1373     return tt1;
1374   if (UNIQUELY_DERIVED_FROM_P (tt2, tt1))
1375     return tt2;
1376
1377   /* Otherwise, try to find a unique baseclass of TT1
1378      that is shared by TT2, and follow that down.  */
1379   for (i = CLASSTYPE_N_BASECLASSES (tt1)-1; i >= 0; i--)
1380     {
1381       tree basetype = TYPE_BINFO_BASETYPE (tt1, i);
1382       tree trial = common_base_type (basetype, tt2);
1383       if (trial)
1384         {
1385           if (trial == error_mark_node)
1386             return trial;
1387           if (best == NULL_TREE)
1388             best = trial;
1389           else if (best != trial)
1390             return error_mark_node;
1391         }
1392     }
1393
1394   /* Same for TT2.  */
1395   for (i = CLASSTYPE_N_BASECLASSES (tt2)-1; i >= 0; i--)
1396     {
1397       tree basetype = TYPE_BINFO_BASETYPE (tt2, i);
1398       tree trial = common_base_type (tt1, basetype);
1399       if (trial)
1400         {
1401           if (trial == error_mark_node)
1402             return trial;
1403           if (best == NULL_TREE)
1404             best = trial;
1405           else if (best != trial)
1406             return error_mark_node;
1407         }
1408     }
1409   return best;
1410 }
1411 \f
1412 /* Subroutines of `comptypes'.  */
1413
1414 /* Return 1 if two parameter type lists PARMS1 and PARMS2 are
1415    equivalent in the sense that functions with those parameter types
1416    can have equivalent types.  The two lists must be equivalent,
1417    element by element.
1418
1419    C++: See comment above about TYPE1, TYPE2.  */
1420
1421 int
1422 compparms (parms1, parms2)
1423      tree parms1, parms2;
1424 {
1425   register tree t1 = parms1, t2 = parms2;
1426
1427   /* An unspecified parmlist matches any specified parmlist
1428      whose argument types don't need default promotions.  */
1429
1430   while (1)
1431     {
1432       if (t1 == 0 && t2 == 0)
1433         return 1;
1434       /* If one parmlist is shorter than the other,
1435          they fail to match.  */
1436       if (t1 == 0 || t2 == 0)
1437         return 0;
1438       if (!same_type_p (TREE_VALUE (t2), TREE_VALUE (t1)))
1439         return 0;
1440
1441       t1 = TREE_CHAIN (t1);
1442       t2 = TREE_CHAIN (t2);
1443     }
1444 }
1445
1446 /* This really wants return whether or not parameter type lists
1447    would make their owning functions assignment compatible or not.
1448
1449    The return value is like for comp_target_types.
1450
1451    This should go away, possibly with the exception of the empty parmlist
1452    conversion; there are no conversions between function types in C++.
1453    (jason 17 Apr 1997)  */
1454
1455 static int
1456 comp_target_parms (parms1, parms2, strict)
1457      tree parms1, parms2;
1458      int strict;
1459 {
1460   register tree t1 = parms1, t2 = parms2;
1461   int warn_contravariance = 0;
1462
1463   /* In C, an unspecified parmlist matches any specified parmlist
1464      whose argument types don't need default promotions.  This is not
1465      true for C++, but let's do it anyway for unfixed headers.  */
1466
1467   if (t1 == 0 && t2 != 0)
1468     {
1469       if (! flag_strict_prototype && t2 == void_list_node)
1470         /* t1 might be the arglist of a function pointer in extern "C"
1471            declared to take (), which we fudged to (...).  Don't make the
1472            user pay for our mistake.  */;
1473       else
1474         cp_pedwarn ("ANSI C++ prohibits conversion from `%#T' to `(...)'",
1475                     parms2);
1476       return self_promoting_args_p (t2);
1477     }
1478   if (t2 == 0)
1479     return self_promoting_args_p (t1);
1480
1481   for (; t1 || t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1482     {
1483       tree p1, p2;
1484
1485       /* If one parmlist is shorter than the other,
1486          they fail to match, unless STRICT is <= 0.  */
1487       if (t1 == 0 || t2 == 0)
1488         {
1489           if (strict > 0)
1490             return 0;
1491           if (strict < 0)
1492             return 1 + warn_contravariance;
1493           return ((t1 && TREE_PURPOSE (t1)) + warn_contravariance);
1494         }
1495       p1 = TREE_VALUE (t1);
1496       p2 = TREE_VALUE (t2);
1497       if (same_type_p (p1, p2))
1498         continue;
1499
1500       if (pedantic)
1501         return 0;
1502
1503       if ((TREE_CODE (p1) == POINTER_TYPE && TREE_CODE (p2) == POINTER_TYPE)
1504           || (TREE_CODE (p1) == REFERENCE_TYPE
1505               && TREE_CODE (p2) == REFERENCE_TYPE))
1506         {
1507           if (strict <= 0
1508               && (TYPE_MAIN_VARIANT (TREE_TYPE (p1))
1509                   == TYPE_MAIN_VARIANT (TREE_TYPE (p2))))
1510             continue;
1511
1512           /* The following is wrong for contravariance,
1513              but many programs depend on it.  */
1514           if (TREE_TYPE (p1) == void_type_node)
1515             continue;
1516           if (TREE_TYPE (p2) == void_type_node)
1517             {
1518               warn_contravariance = 1;
1519               continue;
1520             }
1521           if (IS_AGGR_TYPE (TREE_TYPE (p1))
1522               && !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (p1)),
1523                                TYPE_MAIN_VARIANT (TREE_TYPE (p2))))
1524             return 0;
1525         }
1526       /* Note backwards order due to contravariance.  */
1527       if (comp_target_types (p2, p1, 1) <= 0)
1528         {
1529           if (comp_target_types (p1, p2, 1) > 0)
1530             {
1531               warn_contravariance = 1;
1532               continue;
1533             }
1534           if (strict != 0)
1535             return 0;
1536         }
1537     }
1538   return warn_contravariance ? -1 : 1;
1539 }
1540
1541 /* Return 1 if PARMS specifies a fixed number of parameters
1542    and none of their types is affected by default promotions.  */
1543
1544 int
1545 self_promoting_args_p (parms)
1546      tree parms;
1547 {
1548   register tree t;
1549   for (t = parms; t; t = TREE_CHAIN (t))
1550     {
1551       register tree type = TREE_VALUE (t);
1552
1553       if (TREE_CHAIN (t) == 0 && type != void_type_node)
1554         return 0;
1555
1556       if (type == 0)
1557         return 0;
1558
1559       if (TYPE_MAIN_VARIANT (type) == float_type_node)
1560         return 0;
1561
1562       if (C_PROMOTING_INTEGER_TYPE_P (type))
1563         return 0;
1564     }
1565   return 1;
1566 }
1567 \f
1568 /* Compute the value of the `sizeof' operator.  */
1569
1570 tree
1571 c_sizeof (type)
1572      tree type;
1573 {
1574   enum tree_code code = TREE_CODE (type);
1575   tree t;
1576
1577   if (processing_template_decl)
1578     return build_min (SIZEOF_EXPR, sizetype, type);
1579
1580   if (code == FUNCTION_TYPE)
1581     {
1582       if (pedantic || warn_pointer_arith)
1583         pedwarn ("ANSI C++ forbids taking the sizeof a function type");
1584       return size_int (1);
1585     }
1586   if (code == METHOD_TYPE)
1587     {
1588       if (pedantic || warn_pointer_arith)
1589         pedwarn ("ANSI C++ forbids taking the sizeof a method type");
1590       return size_int (1);
1591     }
1592   if (code == VOID_TYPE)
1593     {
1594       if (pedantic || warn_pointer_arith)
1595         pedwarn ("ANSI C++ forbids taking the sizeof a void type");
1596       return size_int (1);
1597     }
1598   if (code == ERROR_MARK)
1599     return size_int (1);
1600
1601   /* ARM $5.3.2: ``When applied to a reference, the result is the size of the
1602      referenced object.'' */
1603   if (code == REFERENCE_TYPE)
1604     type = TREE_TYPE (type);
1605
1606   /* @@ This also produces an error for a signature ref.
1607         In that case we should be able to do better.  */
1608   if (IS_SIGNATURE (type))
1609     {
1610       error ("`sizeof' applied to a signature type");
1611       return size_int (0);
1612     }
1613   else if (code == OFFSET_TYPE)
1614     {
1615       cp_error ("`sizeof' applied to non-static member");
1616       return size_int (0);
1617     }
1618
1619   if (TYPE_SIZE (complete_type (type)) == 0)
1620     {
1621       cp_error ("`sizeof' applied to incomplete type `%T'", type);
1622       return size_int (0);
1623     }
1624
1625   /* Convert in case a char is more than one unit.  */
1626   t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type), 
1627                   size_int (TYPE_PRECISION (char_type_node)));
1628   t = convert (sizetype, t);
1629   /* size_binop does not put the constant in range, so do it now.  */
1630   if (TREE_CODE (t) == INTEGER_CST && force_fit_type (t, 0))
1631     TREE_CONSTANT_OVERFLOW (t) = TREE_OVERFLOW (t) = 1;
1632   return t;
1633 }
1634
1635 tree
1636 expr_sizeof (e)
1637      tree e;
1638 {
1639   if (processing_template_decl)
1640     return build_min (SIZEOF_EXPR, sizetype, e);
1641
1642   if (TREE_CODE (e) == COMPONENT_REF
1643       && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1644     error ("sizeof applied to a bit-field");
1645   /* ANSI says arrays and functions are converted inside comma.
1646      But we can't really convert them in build_compound_expr
1647      because that would break commas in lvalues.
1648      So do the conversion here if operand was a comma.  */
1649   if (TREE_CODE (e) == COMPOUND_EXPR
1650       && (TREE_CODE (TREE_TYPE (e)) == ARRAY_TYPE
1651           || TREE_CODE (TREE_TYPE (e)) == FUNCTION_TYPE))
1652     e = default_conversion (e);
1653   else if (is_overloaded_fn (e))
1654     {
1655       pedwarn ("ANSI C++ forbids taking the sizeof a function type");
1656       return size_int (1);
1657     }
1658   else if (type_unknown_p (e))
1659     {
1660       incomplete_type_error (e, TREE_TYPE (e));
1661       return size_int (1);
1662     }
1663   /* It's illegal to say `sizeof (X::i)' for `i' a non-static data
1664      member unless you're in a non-static member of X.  But, we used
1665      to support this usage, so we still permit it unless we're being
1666      pedantic.  */
1667   else if (TREE_CODE (e) == OFFSET_REF)
1668     e = resolve_offset_ref (e);
1669
1670   if (e == error_mark_node)
1671     return e;
1672
1673   return c_sizeof (TREE_TYPE (e));
1674 }
1675   
1676 tree
1677 c_sizeof_nowarn (type)
1678      tree type;
1679 {
1680   enum tree_code code = TREE_CODE (type);
1681   tree t;
1682
1683   if (code == FUNCTION_TYPE
1684       || code == METHOD_TYPE
1685       || code == VOID_TYPE
1686       || code == ERROR_MARK)
1687     return size_int (1);
1688   if (code == REFERENCE_TYPE)
1689     type = TREE_TYPE (type);
1690
1691   if (TYPE_SIZE (type) == 0)
1692     return size_int (0);
1693
1694   /* Convert in case a char is more than one unit.  */
1695   t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type), 
1696                   size_int (TYPE_PRECISION (char_type_node)));
1697   t = convert (sizetype, t);
1698   force_fit_type (t, 0);
1699   return t;
1700 }
1701
1702 /* Implement the __alignof keyword: Return the minimum required
1703    alignment of TYPE, measured in bytes.  */
1704
1705 tree
1706 c_alignof (type)
1707      tree type;
1708 {
1709   enum tree_code code = TREE_CODE (type);
1710   tree t;
1711
1712   if (processing_template_decl)
1713     return build_min (ALIGNOF_EXPR, sizetype, type);
1714
1715   if (code == FUNCTION_TYPE || code == METHOD_TYPE)
1716     return size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
1717
1718   if (code == VOID_TYPE || code == ERROR_MARK)
1719     return size_int (1);
1720
1721   /* C++: this is really correct!  */
1722   if (code == REFERENCE_TYPE)
1723     type = TREE_TYPE (type);
1724
1725   /* @@ This also produces an error for a signature ref.
1726         In that case we should be able to do better.  */
1727   if (IS_SIGNATURE (type))
1728     {
1729       error ("`__alignof' applied to a signature type");
1730       return size_int (1);
1731     }
1732
1733   t = size_int (TYPE_ALIGN (type) / BITS_PER_UNIT);
1734   force_fit_type (t, 0);
1735   return t;
1736 }
1737 \f
1738 /* Perform the array-to-pointer and function-to-pointer conversions
1739    for EXP.  
1740
1741    In addition, references are converted to rvalues and manifest
1742    constants are replaced by their values.  */
1743
1744 tree
1745 decay_conversion (exp)
1746      tree exp;
1747 {
1748   register tree type;
1749   register enum tree_code code;
1750
1751   if (TREE_CODE (exp) == OFFSET_REF)
1752     exp = resolve_offset_ref (exp);
1753
1754   type = TREE_TYPE (exp);
1755   code = TREE_CODE (type);
1756
1757   if (code == REFERENCE_TYPE)
1758     {
1759       exp = convert_from_reference (exp);
1760       type = TREE_TYPE (exp);
1761       code = TREE_CODE (type);
1762     }
1763
1764   /* Constants can be used directly unless they're not loadable.  */
1765   if (TREE_CODE (exp) == CONST_DECL)
1766     exp = DECL_INITIAL (exp);
1767   /* Replace a nonvolatile const static variable with its value.  We
1768      don't do this for arrays, though; we want the address of the
1769      first element of the array, not the address of the first element
1770      of its initializing constant.  We *do* replace variables that the
1771      user isn't really supposed to know about; this is a hack to deal
1772      with __PRETTY_FUNCTION__ and the like.  */
1773   else if (TREE_READONLY_DECL_P (exp)
1774            && (code != ARRAY_TYPE 
1775                || (TREE_CODE (exp) == VAR_DECL && DECL_IGNORED_P (exp))))
1776     {
1777       exp = decl_constant_value (exp);
1778       type = TREE_TYPE (exp);
1779     }
1780
1781   /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1782      Leave such NOP_EXPRs, since RHS is being used in non-lvalue context.  */
1783
1784   if (code == VOID_TYPE)
1785     {
1786       error ("void value not ignored as it ought to be");
1787       return error_mark_node;
1788     }
1789   if (code == METHOD_TYPE)
1790     my_friendly_abort (990506);
1791   if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1792     return build_unary_op (ADDR_EXPR, exp, 0);
1793   if (code == ARRAY_TYPE)
1794     {
1795       register tree adr;
1796       tree ptrtype;
1797
1798       if (TREE_CODE (exp) == INDIRECT_REF)
1799         {
1800           /* Stripping away the INDIRECT_REF is not the right
1801              thing to do for references...  */
1802           tree inner = TREE_OPERAND (exp, 0);
1803           if (TREE_CODE (TREE_TYPE (inner)) == REFERENCE_TYPE)
1804             {
1805               inner = build1 (CONVERT_EXPR,
1806                               build_pointer_type (TREE_TYPE
1807                                                   (TREE_TYPE (inner))),
1808                               inner);
1809               TREE_CONSTANT (inner) = TREE_CONSTANT (TREE_OPERAND (inner, 0));
1810             }
1811           return cp_convert (build_pointer_type (TREE_TYPE (type)), inner);
1812         }
1813
1814       if (TREE_CODE (exp) == COMPOUND_EXPR)
1815         {
1816           tree op1 = decay_conversion (TREE_OPERAND (exp, 1));
1817           return build (COMPOUND_EXPR, TREE_TYPE (op1),
1818                         TREE_OPERAND (exp, 0), op1);
1819         }
1820
1821       if (!lvalue_p (exp)
1822           && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1823         {
1824           error ("invalid use of non-lvalue array");
1825           return error_mark_node;
1826         }
1827
1828       ptrtype = build_pointer_type (TREE_TYPE (type));
1829
1830       if (TREE_CODE (exp) == VAR_DECL)
1831         {
1832           /* ??? This is not really quite correct
1833              in that the type of the operand of ADDR_EXPR
1834              is not the target type of the type of the ADDR_EXPR itself.
1835              Question is, can this lossage be avoided?  */
1836           adr = build1 (ADDR_EXPR, ptrtype, exp);
1837           if (mark_addressable (exp) == 0)
1838             return error_mark_node;
1839           TREE_CONSTANT (adr) = staticp (exp);
1840           TREE_SIDE_EFFECTS (adr) = 0;   /* Default would be, same as EXP.  */
1841           return adr;
1842         }
1843       /* This way is better for a COMPONENT_REF since it can
1844          simplify the offset for a component.  */
1845       adr = build_unary_op (ADDR_EXPR, exp, 1);
1846       return cp_convert (ptrtype, adr);
1847     }
1848
1849   return exp;
1850 }
1851
1852 tree
1853 default_conversion (exp)
1854      tree exp;
1855 {
1856   tree type;
1857   enum tree_code code;
1858
1859   exp = decay_conversion (exp);
1860
1861   type = TREE_TYPE (exp);
1862   code = TREE_CODE (type);
1863
1864   if (INTEGRAL_CODE_P (code))
1865     {
1866       tree t = type_promotes_to (type);
1867       if (t != type)
1868         return cp_convert (t, exp);
1869     }
1870
1871   return exp;
1872 }
1873
1874 /* Take the address of an inline function without setting TREE_ADDRESSABLE
1875    or TREE_USED.  */
1876
1877 tree
1878 inline_conversion (exp)
1879      tree exp;
1880 {
1881   if (TREE_CODE (exp) == FUNCTION_DECL)
1882     exp = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (exp)), exp);
1883
1884   return exp;
1885 }
1886
1887 /* Returns nonzero iff exp is a STRING_CST or the result of applying
1888    decay_conversion to one.  */
1889
1890 int
1891 string_conv_p (totype, exp, warn)
1892      tree totype, exp;
1893      int warn;
1894 {
1895   tree t;
1896
1897   if (! flag_const_strings || TREE_CODE (totype) != POINTER_TYPE)
1898     return 0;
1899
1900   t = TREE_TYPE (totype);
1901   if (!same_type_p (t, char_type_node)
1902       && !same_type_p (t, wchar_type_node))
1903     return 0;
1904
1905   if (TREE_CODE (exp) == STRING_CST)
1906     {
1907       /* Make sure that we don't try to convert between char and wchar_t.  */
1908       if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
1909         return 0;
1910     }
1911   else
1912     {
1913       /* Is this a string constant which has decayed to 'const char *'?  */
1914       t = build_pointer_type (build_qualified_type (t, TYPE_QUAL_CONST));
1915       if (!same_type_p (TREE_TYPE (exp), t))
1916         return 0;
1917       STRIP_NOPS (exp);
1918       if (TREE_CODE (exp) != ADDR_EXPR
1919           || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
1920         return 0;
1921     }
1922
1923   /* This warning is not very useful, as it complains about printf.  */
1924   if (warn && warn_write_strings)
1925     cp_warning ("deprecated conversion from string constant to `%T'", totype);
1926
1927   return 1;
1928 }
1929 \f
1930 tree
1931 build_object_ref (datum, basetype, field)
1932      tree datum, basetype, field;
1933 {
1934   tree dtype;
1935   if (datum == error_mark_node)
1936     return error_mark_node;
1937
1938   dtype = TREE_TYPE (datum);
1939   if (TREE_CODE (dtype) == REFERENCE_TYPE)
1940     dtype = TREE_TYPE (dtype);
1941   if (! IS_AGGR_TYPE_CODE (TREE_CODE (dtype)))
1942     {
1943       cp_error ("request for member `%T::%D' in expression of non-aggregate type `%T'",
1944                 basetype, field, dtype);
1945       return error_mark_node;
1946     }
1947   else if (IS_SIGNATURE (basetype))
1948     {
1949       warning ("signature name in scope resolution ignored");
1950       return build_component_ref (datum, field, NULL_TREE, 1);
1951     }
1952   else if (is_aggr_type (basetype, 1))
1953     {
1954       tree binfo = binfo_or_else (basetype, dtype);
1955       if (binfo)
1956         return build_x_component_ref (build_scoped_ref (datum, basetype),
1957                                       field, binfo, 1);
1958     }
1959   return error_mark_node;
1960 }
1961
1962 /* Like `build_component_ref, but uses an already found field, and converts
1963    from a reference.  Must compute access for current_class_ref.
1964    Otherwise, ok.  */
1965
1966 tree
1967 build_component_ref_1 (datum, field, protect)
1968      tree datum, field;
1969      int protect;
1970 {
1971   return convert_from_reference
1972     (build_component_ref (datum, field, NULL_TREE, protect));
1973 }
1974
1975 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
1976    can, for example, use as an lvalue.  This code used to be in
1977    unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
1978    expressions, where we're dealing with aggregates.  But now it's again only
1979    called from unary_complex_lvalue.  The case (in particular) that led to
1980    this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
1981    get it there.  */
1982
1983 static tree
1984 rationalize_conditional_expr (code, t)
1985      enum tree_code code;
1986      tree t;
1987 {
1988   /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
1989      the first operand is always the one to be used if both operands
1990      are equal, so we know what conditional expression this used to be.  */
1991   if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
1992     {
1993       return
1994         build_conditional_expr (build_x_binary_op ((TREE_CODE (t) == MIN_EXPR
1995                                                     ? LE_EXPR : GE_EXPR),
1996                                                    TREE_OPERAND (t, 0),
1997                                                    TREE_OPERAND (t, 1)),
1998                             build_unary_op (code, TREE_OPERAND (t, 0), 0),
1999                             build_unary_op (code, TREE_OPERAND (t, 1), 0));
2000     }
2001
2002   return
2003     build_conditional_expr (TREE_OPERAND (t, 0),
2004                             build_unary_op (code, TREE_OPERAND (t, 1), 0),
2005                             build_unary_op (code, TREE_OPERAND (t, 2), 0));
2006 }
2007
2008 /* Given the TYPE of an anonymous union field inside T, return the
2009    FIELD_DECL for the field.  If not found return NULL_TREE.  Because
2010    anonymous unions can nest, we must also search all anonymous unions
2011    that are directly reachable.  */
2012
2013 static tree
2014 lookup_anon_field (t, type)
2015      tree t, type;
2016 {
2017   tree field;
2018
2019   for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
2020     {
2021       if (TREE_STATIC (field))
2022         continue;
2023       if (TREE_CODE (field) != FIELD_DECL)
2024         continue;
2025
2026       /* If we find it directly, return the field.  */
2027       if (DECL_NAME (field) == NULL_TREE
2028           && type == TREE_TYPE (field))
2029         {
2030           return field;
2031         }
2032
2033       /* Otherwise, it could be nested, search harder.  */
2034       if (DECL_NAME (field) == NULL_TREE
2035           && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2036         {
2037           tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2038           if (subfield)
2039             return subfield;
2040         }
2041     }
2042   return NULL_TREE;
2043 }
2044
2045 /* Build a COMPONENT_REF for a given DATUM, and it's member COMPONENT.
2046    COMPONENT can be an IDENTIFIER_NODE that is the name of the member
2047    that we are interested in, or it can be a FIELD_DECL.  */
2048
2049 tree
2050 build_component_ref (datum, component, basetype_path, protect)
2051      tree datum, component, basetype_path;
2052      int protect;
2053 {
2054   register tree basetype;
2055   register enum tree_code code;
2056   register tree field = NULL;
2057   register tree ref;
2058   tree field_type;
2059   int type_quals;
2060
2061   if (processing_template_decl)
2062     return build_min_nt (COMPONENT_REF, datum, component);
2063   
2064   if (datum == error_mark_node 
2065       || TREE_TYPE (datum) == error_mark_node)
2066     return error_mark_node;
2067
2068   /* BASETYPE holds the type of the class containing the COMPONENT.  */
2069   basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2070     
2071   /* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference
2072      inside it.  */
2073   switch (TREE_CODE (datum))
2074     {
2075     case COMPOUND_EXPR:
2076       {
2077         tree value = build_component_ref (TREE_OPERAND (datum, 1), component,
2078                                           basetype_path, protect);
2079         return build (COMPOUND_EXPR, TREE_TYPE (value),
2080                       TREE_OPERAND (datum, 0), value);
2081       }
2082     case COND_EXPR:
2083       return build_conditional_expr
2084         (TREE_OPERAND (datum, 0),
2085          build_component_ref (TREE_OPERAND (datum, 1), component,
2086                               basetype_path, protect),
2087          build_component_ref (TREE_OPERAND (datum, 2), component,
2088                               basetype_path, protect));
2089
2090     case TEMPLATE_DECL:
2091       cp_error ("invalid use of %D", datum);
2092       datum = error_mark_node;
2093       break;
2094
2095     default:
2096       break;
2097     }
2098
2099   code = TREE_CODE (basetype);
2100
2101   if (code == REFERENCE_TYPE)
2102     {
2103       datum = convert_from_reference (datum);
2104       basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2105       code = TREE_CODE (basetype);
2106     }
2107   if (TREE_CODE (datum) == OFFSET_REF)
2108     {
2109       datum = resolve_offset_ref (datum);
2110       basetype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
2111       code = TREE_CODE (basetype);
2112     }
2113
2114   /* First, see if there is a field or component with name COMPONENT.  */
2115   if (TREE_CODE (component) == TREE_LIST)
2116     {
2117       /* I could not trigger this code. MvL */
2118       my_friendly_abort (980326);
2119 #ifdef DEAD
2120       my_friendly_assert (!(TREE_CHAIN (component) == NULL_TREE
2121                 && DECL_CHAIN (TREE_VALUE (component)) == NULL_TREE), 309);
2122 #endif
2123       return build (COMPONENT_REF, TREE_TYPE (component), datum, component);
2124     }
2125
2126   if (! IS_AGGR_TYPE_CODE (code))
2127     {
2128       if (code != ERROR_MARK)
2129         cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
2130                   component, datum, basetype);
2131       return error_mark_node;
2132     }
2133
2134   if (!complete_type_or_else (basetype, datum))
2135     return error_mark_node;
2136
2137   if (TREE_CODE (component) == BIT_NOT_EXPR)
2138     {
2139       if (TYPE_IDENTIFIER (basetype) != TREE_OPERAND (component, 0))
2140         {
2141           cp_error ("destructor specifier `%T::~%T' must have matching names",
2142                     basetype, TREE_OPERAND (component, 0));
2143           return error_mark_node;
2144         }
2145       if (! TYPE_HAS_DESTRUCTOR (basetype))
2146         {
2147           cp_error ("type `%T' has no destructor", basetype);
2148           return error_mark_node;
2149         }
2150       return TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 1);
2151     }
2152
2153   /* Look up component name in the structure type definition.  */
2154   if (CLASSTYPE_VFIELD (basetype)
2155       && DECL_NAME (CLASSTYPE_VFIELD (basetype)) == component)
2156     /* Special-case this because if we use normal lookups in an ambiguous
2157        hierarchy, the compiler will abort (because vptr lookups are
2158        not supposed to be ambiguous.  */
2159     field = CLASSTYPE_VFIELD (basetype);
2160   else if (TREE_CODE (component) == FIELD_DECL)
2161     field = component;
2162   else if (TREE_CODE (component) == TYPE_DECL)
2163     {
2164       cp_error ("invalid use of type decl `%#D' as expression", component);
2165       return error_mark_node;
2166     }
2167   else if (TREE_CODE (component) == TEMPLATE_DECL)
2168     {
2169       cp_error ("invalid use of template `%#D' as expression", component);
2170       return error_mark_node;
2171     }
2172   else
2173     {
2174       tree name = component;
2175       if (TREE_CODE (component) == VAR_DECL)
2176         name = DECL_NAME (component);
2177       if (basetype_path == NULL_TREE)
2178         basetype_path = TYPE_BINFO (basetype);
2179       field = lookup_field (basetype_path, name,
2180                             protect && !VFIELD_NAME_P (name), 0);
2181       if (field == error_mark_node)
2182         return error_mark_node;
2183
2184       if (field == NULL_TREE)
2185         {
2186           /* Not found as a data field, look for it as a method.  If found,
2187              then if this is the only possible one, return it, else
2188              report ambiguity error.  */
2189           tree fndecls = lookup_fnfields (basetype_path, name, 1);
2190           if (fndecls == error_mark_node)
2191             return error_mark_node;
2192           if (fndecls)
2193             {
2194               /* If the function is unique and static, we can resolve it
2195                  now.  Otherwise, we have to wait and see what context it is
2196                  used in; a component_ref involving a non-static member
2197                  function can only be used in a call (expr.ref).  */
2198
2199               if (TREE_CHAIN (fndecls) == NULL_TREE
2200                   && TREE_CODE (TREE_VALUE (fndecls)) == FUNCTION_DECL)
2201                 {
2202                   if (DECL_STATIC_FUNCTION_P (TREE_VALUE (fndecls)))
2203                     {
2204                       tree fndecl = TREE_VALUE (fndecls);
2205                       enforce_access (TREE_PURPOSE (fndecls), fndecl);
2206                       mark_used (fndecl);
2207                       return fndecl;
2208                     }
2209                   else
2210                     {
2211                       /* A unique non-static member function.  Other parts
2212                          of the compiler expect something with
2213                          unknown_type_node to be really overloaded, so
2214                          let's oblige.  */
2215                       TREE_VALUE (fndecls)
2216                         = scratch_ovl_cons (TREE_VALUE (fndecls), NULL_TREE);
2217                     }
2218                 }
2219
2220               ref = build (COMPONENT_REF, unknown_type_node,
2221                            datum, TREE_VALUE (fndecls));
2222               return ref;
2223             }
2224
2225           cp_error ("`%#T' has no member named `%D'", basetype, name);
2226           return error_mark_node;
2227         }
2228       else if (TREE_TYPE (field) == error_mark_node)
2229         return error_mark_node;
2230
2231       if (TREE_CODE (field) != FIELD_DECL)
2232         {
2233           if (TREE_CODE (field) == TYPE_DECL)
2234             cp_pedwarn ("invalid use of type decl `%#D' as expression", field);
2235           else if (DECL_RTL (field) != 0)
2236             mark_used (field);
2237           else
2238             TREE_USED (field) = 1;
2239           return field;
2240         }
2241     }
2242
2243   /* See if we have to do any conversions so that we pick up the field from the
2244      right context.  */
2245   if (DECL_FIELD_CONTEXT (field) != basetype)
2246     {
2247       tree context = DECL_FIELD_CONTEXT (field);
2248       tree base = context;
2249       while (!same_type_p (base, basetype) && TYPE_NAME (base)
2250              && ANON_AGGR_TYPE_P (base))
2251         {
2252           base = TYPE_CONTEXT (base);
2253         }
2254
2255       /* Handle base classes here...  */
2256       if (base != basetype && TYPE_USES_COMPLEX_INHERITANCE (basetype))
2257         {
2258           tree addr = build_unary_op (ADDR_EXPR, datum, 0);
2259           if (integer_zerop (addr))
2260             {
2261               error ("invalid reference to NULL ptr, use ptr-to-member instead");
2262               return error_mark_node;
2263             }
2264           if (VBASE_NAME_P (DECL_NAME (field)))
2265             {
2266               /* It doesn't matter which vbase pointer we grab, just
2267                  find one of them.  */
2268               tree binfo = get_binfo (base,
2269                                       TREE_TYPE (TREE_TYPE (addr)), 0);
2270               addr = convert_pointer_to_real (binfo, addr);
2271             }
2272           else
2273             addr = convert_pointer_to (base, addr);
2274           datum = build_indirect_ref (addr, NULL_PTR);
2275           my_friendly_assert (datum != error_mark_node, 311);
2276         }
2277       basetype = base;
2278  
2279       /* Handle things from anon unions here...  */
2280       if (TYPE_NAME (context) && ANON_AGGR_TYPE_P (context))
2281         {
2282           tree subfield = lookup_anon_field (basetype, context);
2283           tree subdatum = build_component_ref (datum, subfield,
2284                                                basetype_path, protect);
2285           return build_component_ref (subdatum, field, basetype_path, protect);
2286         }
2287     }
2288
2289   /* Compute the type of the field, as described in [expr.ref].  */
2290   type_quals = TYPE_UNQUALIFIED;
2291   field_type = TREE_TYPE (field);
2292   if (TREE_CODE (field_type) == REFERENCE_TYPE)
2293     /* The standard says that the type of the result should be the
2294        type referred to by the reference.  But for now, at least, we
2295        do the conversion from reference type later.  */
2296     ;
2297   else
2298     {
2299       type_quals = (CP_TYPE_QUALS (field_type)  
2300                     | CP_TYPE_QUALS (TREE_TYPE (datum)));
2301
2302       /* A field is const (volatile) if the enclosing object, or the
2303          field itself, is const (volatile).  But, a mutable field is
2304          not const, even within a const object.  */
2305       if (DECL_LANG_SPECIFIC (field) && DECL_MUTABLE_P (field))
2306         type_quals &= ~TYPE_QUAL_CONST;
2307       if (!IS_SIGNATURE (field_type))
2308         field_type = cp_build_qualified_type (field_type, type_quals);
2309     }
2310
2311   ref = fold (build (COMPONENT_REF, field_type,
2312                      break_out_cleanups (datum), field));
2313
2314   /* Mark the expression const or volatile, as appropriate.  Even
2315      though we've dealt with the type above, we still have to mark the
2316      expression itself.  */
2317   if (type_quals & TYPE_QUAL_CONST)
2318     TREE_READONLY (ref) = 1;
2319   else if (type_quals & TYPE_QUAL_VOLATILE)
2320     TREE_THIS_VOLATILE (ref) = 1;
2321
2322   return ref;
2323 }
2324
2325 /* Variant of build_component_ref for use in expressions, which should
2326    never have REFERENCE_TYPE.  */
2327
2328 tree
2329 build_x_component_ref (datum, component, basetype_path, protect)
2330      tree datum, component, basetype_path;
2331      int protect;
2332 {
2333   tree t = build_component_ref (datum, component, basetype_path, protect);
2334
2335   if (! processing_template_decl)
2336     t = convert_from_reference (t);
2337
2338   return t;
2339 }
2340 \f
2341 /* Given an expression PTR for a pointer, return an expression
2342    for the value pointed to.
2343    ERRORSTRING is the name of the operator to appear in error messages.
2344
2345    This function may need to overload OPERATOR_FNNAME.
2346    Must also handle REFERENCE_TYPEs for C++.  */
2347
2348 tree
2349 build_x_indirect_ref (ptr, errorstring)
2350      tree ptr;
2351      const char *errorstring;
2352 {
2353   tree rval;
2354
2355   if (processing_template_decl)
2356     return build_min_nt (INDIRECT_REF, ptr);
2357
2358   rval = build_opfncall (INDIRECT_REF, LOOKUP_NORMAL, ptr, NULL_TREE,
2359                          NULL_TREE);
2360   if (rval)
2361     return rval;
2362   return build_indirect_ref (ptr, errorstring);
2363 }
2364
2365 tree
2366 build_indirect_ref (ptr, errorstring)
2367      tree ptr;
2368      const char *errorstring;
2369 {
2370   register tree pointer, type;
2371
2372   if (ptr == error_mark_node)
2373     return error_mark_node;
2374
2375   if (ptr == current_class_ptr)
2376     return current_class_ref;
2377
2378   pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2379              ? ptr : default_conversion (ptr));
2380   type = TREE_TYPE (pointer);
2381
2382   if (TYPE_PTR_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
2383     {
2384       /* [expr.unary.op]
2385          
2386          If the type of the expression is "pointer to T," the type
2387          of  the  result  is  "T."   
2388
2389          We must use the canonical variant because certain parts of
2390          the back end, like fold, do pointer comparisons between
2391          types.  */
2392       tree t = canonical_type_variant (TREE_TYPE (type));
2393
2394       if (TREE_CODE (pointer) == ADDR_EXPR
2395           && !flag_volatile
2396           && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2397         /* The POINTER was something like `&x'.  We simplify `*&x' to
2398            `x'.  */
2399         return TREE_OPERAND (pointer, 0);
2400       else
2401         {
2402           tree ref = build1 (INDIRECT_REF, t, pointer);
2403
2404           /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2405              so that we get the proper error message if the result is used
2406              to assign to.  Also, &* is supposed to be a no-op.  */
2407           TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2408           TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2409           TREE_SIDE_EFFECTS (ref)
2410             = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer)
2411                || flag_volatile);
2412           return ref;
2413         }
2414     }
2415   /* `pointer' won't be an error_mark_node if we were given a
2416      pointer to member, so it's cool to check for this here.  */
2417   else if (TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
2418     error ("invalid use of `%s' on pointer to member", errorstring);
2419   else if (TREE_CODE (type) == RECORD_TYPE
2420            && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))
2421     error ("cannot dereference signature pointer/reference");
2422   else if (pointer != error_mark_node)
2423     {
2424       if (errorstring)
2425         error ("invalid type argument of `%s'", errorstring);
2426       else
2427         error ("invalid type argument");
2428     }
2429   return error_mark_node;
2430 }
2431
2432 /* This handles expressions of the form "a[i]", which denotes
2433    an array reference.
2434
2435    This is logically equivalent in C to *(a+i), but we may do it differently.
2436    If A is a variable or a member, we generate a primitive ARRAY_REF.
2437    This avoids forcing the array out of registers, and can work on
2438    arrays that are not lvalues (for example, members of structures returned
2439    by functions).
2440
2441    If INDEX is of some user-defined type, it must be converted to
2442    integer type.  Otherwise, to make a compatible PLUS_EXPR, it
2443    will inherit the type of the array, which will be some pointer type.  */
2444
2445 tree
2446 build_array_ref (array, idx)
2447      tree array, idx;
2448 {
2449   if (idx == 0)
2450     {
2451       error ("subscript missing in array reference");
2452       return error_mark_node;
2453     }
2454
2455   if (TREE_TYPE (array) == error_mark_node
2456       || TREE_TYPE (idx) == error_mark_node)
2457     return error_mark_node;
2458
2459   if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
2460       && TREE_CODE (array) != INDIRECT_REF)
2461     {
2462       tree rval, type;
2463
2464       /* Subscripting with type char is likely to lose
2465          on a machine where chars are signed.
2466          So warn on any machine, but optionally.
2467          Don't warn for unsigned char since that type is safe.
2468          Don't warn for signed char because anyone who uses that
2469          must have done so deliberately.  */
2470       if (warn_char_subscripts
2471           && TYPE_MAIN_VARIANT (TREE_TYPE (idx)) == char_type_node)
2472         warning ("array subscript has type `char'");
2473
2474       /* Apply default promotions *after* noticing character types.  */
2475       idx = default_conversion (idx);
2476
2477       if (TREE_CODE (TREE_TYPE (idx)) != INTEGER_TYPE)
2478         {
2479           error ("array subscript is not an integer");
2480           return error_mark_node;
2481         }
2482
2483       /* An array that is indexed by a non-constant
2484          cannot be stored in a register; we must be able to do
2485          address arithmetic on its address.
2486          Likewise an array of elements of variable size.  */
2487       if (TREE_CODE (idx) != INTEGER_CST
2488           || (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))) != 0
2489               && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
2490                   != INTEGER_CST)))
2491         {
2492           if (mark_addressable (array) == 0)
2493             return error_mark_node;
2494         }
2495       /* An array that is indexed by a constant value which is not within
2496          the array bounds cannot be stored in a register either; because we
2497          would get a crash in store_bit_field/extract_bit_field when trying
2498          to access a non-existent part of the register.  */
2499       if (TREE_CODE (idx) == INTEGER_CST
2500           && TYPE_VALUES (TREE_TYPE (array))
2501           && ! int_fits_type_p (idx, TYPE_VALUES (TREE_TYPE (array))))
2502         {
2503           if (mark_addressable (array) == 0)
2504             return error_mark_node;
2505         }
2506
2507       if (pedantic && !lvalue_p (array))
2508         pedwarn ("ANSI C++ forbids subscripting non-lvalue array");
2509
2510       /* Note in C++ it is valid to subscript a `register' array, since
2511          it is valid to take the address of something with that
2512          storage specification.  */
2513       if (extra_warnings)
2514         {
2515           tree foo = array;
2516           while (TREE_CODE (foo) == COMPONENT_REF)
2517             foo = TREE_OPERAND (foo, 0);
2518           if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
2519             warning ("subscripting array declared `register'");
2520         }
2521
2522       type = TREE_TYPE (TREE_TYPE (array));
2523       rval = build (ARRAY_REF, type, array, idx);
2524       /* Array ref is const/volatile if the array elements are
2525          or if the array is..  */
2526       TREE_READONLY (rval)
2527         |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
2528       TREE_SIDE_EFFECTS (rval)
2529         |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
2530       TREE_THIS_VOLATILE (rval)
2531         |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
2532       return require_complete_type (fold (rval));
2533     }
2534
2535   {
2536     tree ar = default_conversion (array);
2537     tree ind = default_conversion (idx);
2538
2539     /* Put the integer in IND to simplify error checking.  */
2540     if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
2541       {
2542         tree temp = ar;
2543         ar = ind;
2544         ind = temp;
2545       }
2546
2547     if (ar == error_mark_node)
2548       return ar;
2549
2550     if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE)
2551       {
2552         error ("subscripted value is neither array nor pointer");
2553         return error_mark_node;
2554       }
2555     if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
2556       {
2557         error ("array subscript is not an integer");
2558         return error_mark_node;
2559       }
2560
2561     return build_indirect_ref (build_binary_op_nodefault (PLUS_EXPR, ar,
2562                                                           ind, PLUS_EXPR),
2563                                "array indexing");
2564   }
2565 }
2566 \f
2567 /* Build a function call to function FUNCTION with parameters PARAMS.
2568    PARAMS is a list--a chain of TREE_LIST nodes--in which the
2569    TREE_VALUE of each node is a parameter-expression.  The PARAMS do
2570    not include any object pointer that may be required.  FUNCTION's
2571    data type may be a function type or a pointer-to-function.
2572
2573    For C++: If FUNCTION's data type is a TREE_LIST, then the tree list
2574    is the list of possible methods that FUNCTION could conceivably
2575    be.  If the list of methods comes from a class, then it will be
2576    a list of lists (where each element is associated with the class
2577    that produced it), otherwise it will be a simple list (for
2578    functions overloaded in global scope).
2579
2580    In the first case, TREE_VALUE (function) is the head of one of those
2581    lists, and TREE_PURPOSE is the name of the function.
2582
2583    In the second case, TREE_PURPOSE (function) is the function's
2584    name directly.
2585
2586    DECL is the class instance variable, usually CURRENT_CLASS_REF.
2587
2588    When calling a TEMPLATE_DECL, we don't require a complete return
2589    type.  */
2590
2591 tree
2592 build_x_function_call (function, params, decl)
2593      tree function, params, decl;
2594 {
2595   tree type;
2596   tree template_id = NULL_TREE;
2597   int is_method;
2598
2599   if (function == error_mark_node)
2600     return error_mark_node;
2601
2602   if (processing_template_decl)
2603     return build_min_nt (CALL_EXPR, function, params, NULL_TREE);
2604
2605   /* Save explicit template arguments if found */
2606   if (TREE_CODE (function) == TEMPLATE_ID_EXPR)
2607     {
2608       template_id = function;
2609       function = TREE_OPERAND (function, 0);
2610     }
2611
2612   type = TREE_TYPE (function);
2613
2614   if (TREE_CODE (type) == OFFSET_TYPE
2615       && TREE_TYPE (type) == unknown_type_node
2616       && TREE_CODE (function) == TREE_LIST
2617       && TREE_CHAIN (function) == NULL_TREE)
2618     {
2619       /* Undo (Foo:bar)()...  */
2620       type = TYPE_OFFSET_BASETYPE (type);
2621       function = TREE_VALUE (function);
2622       my_friendly_assert (TREE_CODE (function) == TREE_LIST, 999);
2623       my_friendly_assert (TREE_CHAIN (function) == NULL_TREE, 999);
2624       function = TREE_VALUE (function);
2625       if (TREE_CODE (function) == OVERLOAD)
2626         function = OVL_FUNCTION (function);
2627       my_friendly_assert (TREE_CODE (function) == FUNCTION_DECL, 999);
2628       function = DECL_NAME (function);
2629       return build_method_call (decl, function, params,
2630                                 TYPE_BINFO (type), LOOKUP_NORMAL);
2631     }
2632     
2633   if ((TREE_CODE (function) == FUNCTION_DECL
2634        && DECL_STATIC_FUNCTION_P (function))
2635       || (TREE_CODE (function) == TEMPLATE_DECL
2636           && DECL_STATIC_FUNCTION_P (DECL_RESULT (function))))
2637       return build_member_call(DECL_CONTEXT (function), 
2638                                template_id 
2639                                ? template_id : DECL_NAME (function), 
2640                                params);
2641
2642   is_method = ((TREE_CODE (function) == TREE_LIST
2643                 && current_class_type != NULL_TREE
2644                 && (IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (function))
2645                     == function))
2646                || (TREE_CODE (function) == OVERLOAD
2647                    && DECL_FUNCTION_MEMBER_P (OVL_CURRENT (function)))
2648                || TREE_CODE (function) == IDENTIFIER_NODE
2649                || TREE_CODE (type) == METHOD_TYPE
2650                || TYPE_PTRMEMFUNC_P (type));
2651
2652   /* A friend template.  Make it look like a toplevel declaration.  */
2653   if (! is_method && TREE_CODE (function) == TEMPLATE_DECL)
2654     function = scratch_ovl_cons (function, NULL_TREE);
2655
2656   /* Handle methods, friends, and overloaded functions, respectively.  */
2657   if (is_method)
2658     {
2659       tree basetype = NULL_TREE;
2660
2661       if (TREE_CODE (function) == OVERLOAD)
2662         function = OVL_CURRENT (function);
2663
2664       if (TREE_CODE (function) == FUNCTION_DECL
2665           || DECL_FUNCTION_TEMPLATE_P (function))
2666         {
2667           basetype = DECL_CLASS_CONTEXT (function);
2668
2669           if (DECL_NAME (function))
2670             function = DECL_NAME (function);
2671           else
2672             function = TYPE_IDENTIFIER (DECL_CLASS_CONTEXT (function));
2673         }
2674       else if (TREE_CODE (function) == TREE_LIST)
2675         {
2676           my_friendly_assert (TREE_CODE (TREE_VALUE (function))
2677                               == FUNCTION_DECL, 312);
2678           basetype = DECL_CLASS_CONTEXT (TREE_VALUE (function));
2679           function = TREE_PURPOSE (function);
2680         }
2681       else if (TREE_CODE (function) != IDENTIFIER_NODE)
2682         {
2683           if (TREE_CODE (function) == OFFSET_REF)
2684             {
2685               if (TREE_OPERAND (function, 0))
2686                 decl = TREE_OPERAND (function, 0);
2687             }
2688           /* Call via a pointer to member function.  */
2689           if (decl == NULL_TREE)
2690             {
2691               error ("pointer to member function called, but not in class scope");
2692               return error_mark_node;
2693             }
2694           /* What other type of POINTER_TYPE could this be? */
2695           if (TREE_CODE (TREE_TYPE (function)) != POINTER_TYPE
2696               && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (function))
2697               && TREE_CODE (function) != OFFSET_REF)
2698             function = build (OFFSET_REF, TREE_TYPE (type), NULL_TREE,
2699                               function);
2700           goto do_x_function;
2701         }
2702
2703       /* this is an abbreviated method call.
2704          must go through here in case it is a virtual function.
2705          @@ Perhaps this could be optimized.  */
2706
2707       if (basetype && (! current_class_type
2708                        || ! DERIVED_FROM_P (basetype, current_class_type)))
2709         return build_member_call (basetype, function, params);
2710
2711       if (decl == NULL_TREE)
2712         {
2713           if (current_class_type == NULL_TREE)
2714             {
2715               cp_error ("object missing in call to method `%D'", function);
2716               return error_mark_node;
2717             }
2718           /* Yow: call from a static member function.  */
2719           decl = build_dummy_object (current_class_type);
2720         }
2721
2722       /* Put back explicit template arguments, if any.  */
2723       if (template_id)
2724         function = template_id;
2725       return build_method_call (decl, function, params,
2726                                 NULL_TREE, LOOKUP_NORMAL);
2727     }
2728   else if (TREE_CODE (function) == COMPONENT_REF
2729            && type == unknown_type_node)
2730     {
2731       /* Undo what we did in build_component_ref.  */
2732       decl = TREE_OPERAND (function, 0);
2733       function = TREE_OPERAND (function, 1);
2734       function = DECL_NAME (OVL_CURRENT (function));
2735
2736       if (template_id)
2737         {
2738           TREE_OPERAND (template_id, 0) = function;
2739           function = template_id;
2740         }
2741
2742       return build_method_call (decl, function, params,
2743                                 NULL_TREE, LOOKUP_NORMAL);
2744     }
2745   else if (really_overloaded_fn (function))
2746     {
2747       if (OVL_FUNCTION (function) == NULL_TREE)
2748         {
2749           cp_error ("function `%D' declared overloaded, but no definitions appear with which to resolve it?!?",
2750                     TREE_PURPOSE (function));
2751           return error_mark_node;
2752         }
2753       else
2754         {
2755           /* Put back explicit template arguments, if any.  */
2756           if (template_id)
2757             function = template_id;
2758           return build_new_function_call (function, params);
2759         }
2760     }
2761   else
2762     /* Remove a potential OVERLOAD around it */
2763     function = OVL_CURRENT (function);
2764
2765  do_x_function:
2766   if (TREE_CODE (function) == OFFSET_REF)
2767     {
2768       /* If the component is a data element (or a virtual function), we play
2769          games here to make things work.  */
2770       tree decl_addr;
2771
2772       if (TREE_OPERAND (function, 0))
2773         decl = TREE_OPERAND (function, 0);
2774       else
2775         decl = current_class_ref;
2776
2777       decl_addr = build_unary_op (ADDR_EXPR, decl, 0);
2778
2779       /* Sigh.  OFFSET_REFs are being used for too many things.
2780          They're being used both for -> and ->*, and we want to resolve
2781          the -> cases here, but leave the ->*.  We could use
2782          resolve_offset_ref for those, too, but it would call
2783          get_member_function_from_ptrfunc and decl_addr wouldn't get
2784          updated properly.  Nasty.  */
2785       if (TREE_CODE (TREE_OPERAND (function, 1)) == FIELD_DECL)
2786         function = resolve_offset_ref (function);
2787       else
2788         function = TREE_OPERAND (function, 1);
2789
2790       function = get_member_function_from_ptrfunc (&decl_addr, function);
2791       params = expr_tree_cons (NULL_TREE, decl_addr, params);
2792       return build_function_call (function, params);
2793     }
2794
2795   type = TREE_TYPE (function);
2796   if (type != error_mark_node)
2797     {
2798       if (TREE_CODE (type) == REFERENCE_TYPE)
2799         type = TREE_TYPE (type);
2800
2801       if (IS_AGGR_TYPE (type))
2802         return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, function, params, NULL_TREE);
2803     }
2804
2805   if (is_method)
2806     {
2807       tree fntype = TREE_TYPE (function);
2808       tree ctypeptr = NULL_TREE;
2809
2810       /* Explicitly named method?  */
2811       if (TREE_CODE (function) == FUNCTION_DECL)
2812         ctypeptr = build_pointer_type (DECL_CLASS_CONTEXT (function));
2813       /* Expression with ptr-to-method type?  It could either be a plain
2814          usage, or it might be a case where the ptr-to-method is being
2815          passed in as an argument.  */
2816       else if (TYPE_PTRMEMFUNC_P (fntype))
2817         {
2818           tree rec = TYPE_METHOD_BASETYPE (TREE_TYPE
2819                                            (TYPE_PTRMEMFUNC_FN_TYPE (fntype)));
2820           ctypeptr = build_pointer_type (rec);
2821         }
2822       /* Unexpected node type?  */
2823       else
2824         my_friendly_abort (116);
2825       if (decl == NULL_TREE)
2826         {
2827           if (current_function_decl
2828               && DECL_STATIC_FUNCTION_P (current_function_decl))
2829             error ("invalid call to member function needing `this' in static member function scope");
2830           else
2831             error ("pointer to member function called, but not in class scope");
2832           return error_mark_node;
2833         }
2834       if (TREE_CODE (TREE_TYPE (decl)) != POINTER_TYPE
2835           && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))
2836         {
2837           decl = build_unary_op (ADDR_EXPR, decl, 0);
2838           decl = convert_pointer_to (TREE_TYPE (ctypeptr), decl);
2839         }
2840       else
2841         decl = build_c_cast (ctypeptr, decl);
2842       params = expr_tree_cons (NULL_TREE, decl, params);
2843     }
2844
2845   return build_function_call (function, params);
2846 }
2847
2848 /* Resolve a pointer to member function.  INSTANCE is the object
2849    instance to use, if the member points to a virtual member.  */
2850
2851 tree
2852 get_member_function_from_ptrfunc (instance_ptrptr, function)
2853      tree *instance_ptrptr;
2854      tree function;
2855 {
2856   if (TREE_CODE (function) == OFFSET_REF)
2857     {
2858       function = TREE_OPERAND (function, 1);
2859     }
2860
2861   if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
2862     {
2863       tree fntype, idx, e1, delta, delta2, e2, e3, aref, vtbl;
2864       tree instance, basetype;
2865
2866       tree instance_ptr = *instance_ptrptr;
2867
2868       if (TREE_SIDE_EFFECTS (instance_ptr))
2869         instance_ptr = save_expr (instance_ptr);
2870
2871       if (TREE_SIDE_EFFECTS (function))
2872         function = save_expr (function);
2873
2874       fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
2875       basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
2876
2877       delta = cp_convert (ptrdiff_type_node,
2878                           build_component_ref (function, delta_identifier,
2879                                                NULL_TREE, 0));
2880       e3 = PFN_FROM_PTRMEMFUNC (function);
2881
2882       if (TYPE_SIZE (basetype) != NULL_TREE
2883           && ! TYPE_VIRTUAL_P (basetype))
2884         /* If basetype doesn't have virtual functions, don't emit code to
2885            handle that case.  */
2886         e1 = e3;
2887       else
2888         {
2889           /* Promoting idx before saving it improves performance on RISC
2890              targets.  Without promoting, the first compare used
2891              load-with-sign-extend, while the second used normal load then
2892              shift to sign-extend.  An optimizer flaw, perhaps, but it's
2893              easier to make this change.  */
2894           idx = save_expr (default_conversion
2895                            (build_component_ref (function,
2896                                                  index_identifier,
2897                                                  NULL_TREE, 0)));
2898           e1 = build_binary_op (GE_EXPR, idx, integer_zero_node);
2899
2900           /* Convert down to the right base, before using the instance.  */
2901           instance = convert_pointer_to_real (basetype, instance_ptr);
2902           if (instance == error_mark_node && instance_ptr != error_mark_node)
2903             return instance;
2904
2905           vtbl = convert_pointer_to (ptr_type_node, instance);
2906           delta2 = DELTA2_FROM_PTRMEMFUNC (function);
2907           vtbl = build
2908             (PLUS_EXPR,
2909              build_pointer_type (build_pointer_type (vtable_entry_type)),
2910              vtbl, cp_convert (ptrdiff_type_node, delta2));
2911           vtbl = build_indirect_ref (vtbl, NULL_PTR);
2912           aref = build_array_ref (vtbl, build_binary_op (MINUS_EXPR,
2913                                                          idx,
2914                                                          integer_one_node));
2915           if (! flag_vtable_thunks)
2916             {
2917               aref = save_expr (aref);
2918
2919               delta = build_binary_op
2920                 (PLUS_EXPR,
2921                  build_conditional_expr (e1,
2922                                          build_component_ref (aref,
2923                                                               delta_identifier,
2924                                                               NULL_TREE, 0),
2925                                          integer_zero_node),
2926                  delta);
2927             }
2928
2929           if (flag_vtable_thunks)
2930             e2 = aref;
2931           else
2932             e2 = build_component_ref (aref, pfn_identifier, NULL_TREE, 0);
2933           TREE_TYPE (e2) = TREE_TYPE (e3);
2934           e1 = build_conditional_expr (e1, e2, e3);
2935
2936           /* Make sure this doesn't get evaluated first inside one of the
2937              branches of the COND_EXPR.  */
2938           if (TREE_CODE (instance_ptr) == SAVE_EXPR)
2939             e1 = build (COMPOUND_EXPR, TREE_TYPE (e1),
2940                         instance_ptr, e1);
2941         }
2942
2943       *instance_ptrptr = build (PLUS_EXPR, TREE_TYPE (instance_ptr),
2944                                 instance_ptr, delta);
2945
2946       if (instance_ptr == error_mark_node
2947           && TREE_CODE (e1) != ADDR_EXPR
2948           && TREE_CODE (TREE_OPERAND (e1, 0)) != FUNCTION_DECL)
2949         cp_error ("object missing in `%E'", function);
2950
2951       function = e1;
2952     }
2953   return function;
2954 }
2955
2956 tree
2957 build_function_call_real (function, params, require_complete, flags)
2958      tree function, params;
2959      int require_complete, flags;
2960 {
2961   register tree fntype, fndecl;
2962   register tree value_type;
2963   register tree coerced_params;
2964   tree name = NULL_TREE, assembler_name = NULL_TREE;
2965   int is_method;
2966
2967   /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2968      Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context.  */
2969   if (TREE_CODE (function) == NOP_EXPR
2970       && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
2971     function = TREE_OPERAND (function, 0);
2972
2973   if (TREE_CODE (function) == FUNCTION_DECL)
2974     {
2975       name = DECL_NAME (function);
2976       assembler_name = DECL_ASSEMBLER_NAME (function);
2977
2978       GNU_xref_call (current_function_decl,
2979                      IDENTIFIER_POINTER (name ? name
2980                                          : TYPE_IDENTIFIER (DECL_CLASS_CONTEXT
2981                                                             (function))));
2982       mark_used (function);
2983       fndecl = function;
2984
2985       /* Convert anything with function type to a pointer-to-function.  */
2986       if (pedantic && DECL_MAIN_P (function))
2987         pedwarn ("ANSI C++ forbids calling `main' from within program");
2988
2989       /* Differs from default_conversion by not setting TREE_ADDRESSABLE
2990          (because calling an inline function does not mean the function
2991          needs to be separately compiled).  */
2992
2993       if (DECL_INLINE (function))
2994         function = inline_conversion (function);
2995       else
2996         function = build_addr_func (function);
2997     }
2998   else
2999     {
3000       fndecl = NULL_TREE;
3001
3002       function = build_addr_func (function);
3003     }
3004
3005   if (function == error_mark_node)
3006     return error_mark_node;
3007
3008   fntype = TREE_TYPE (function);
3009
3010   if (TYPE_PTRMEMFUNC_P (fntype))
3011     {
3012       cp_error ("must use .* or ->* to call pointer-to-member function in `%E (...)'",
3013                 function);
3014       return error_mark_node;
3015     }
3016
3017   is_method = (TREE_CODE (fntype) == POINTER_TYPE
3018                && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3019
3020   if (!((TREE_CODE (fntype) == POINTER_TYPE
3021          && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE)
3022         || is_method
3023         || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3024     {
3025       cp_error ("`%E' cannot be used as a function", function);
3026       return error_mark_node;
3027     }
3028
3029   /* fntype now gets the type of function pointed to.  */
3030   fntype = TREE_TYPE (fntype);
3031
3032   /* Convert the parameters to the types declared in the
3033      function prototype, or apply default promotions.  */
3034
3035   if (flags & LOOKUP_COMPLAIN)
3036     coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype),
3037                                         params, fndecl, LOOKUP_NORMAL);
3038   else
3039     coerced_params = convert_arguments (TYPE_ARG_TYPES (fntype),
3040                                         params, fndecl, 0);
3041
3042   if (coerced_params == error_mark_node)
3043     {
3044       if (flags & LOOKUP_SPECULATIVELY)
3045         return NULL_TREE;
3046       else
3047         return error_mark_node;
3048     }
3049
3050   /* Check for errors in format strings.  */
3051
3052   if (warn_format && (name || assembler_name))
3053     check_function_format (name, assembler_name, coerced_params);
3054
3055   /* Recognize certain built-in functions so we can make tree-codes
3056      other than CALL_EXPR.  We do this when it enables fold-const.c
3057      to do something useful.  */
3058
3059   if (TREE_CODE (function) == ADDR_EXPR
3060       && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
3061       && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
3062     switch (DECL_FUNCTION_CODE (TREE_OPERAND (function, 0)))
3063       {
3064       case BUILT_IN_ABS:
3065       case BUILT_IN_LABS:
3066       case BUILT_IN_FABS:
3067         if (coerced_params == 0)
3068           return integer_zero_node;
3069         return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
3070
3071       default:
3072         break;
3073       }
3074
3075   /* C++ */
3076   value_type = TREE_TYPE (fntype) ? TREE_TYPE (fntype) : void_type_node;
3077   {
3078     register tree result
3079       = build_call (function, value_type, coerced_params);
3080
3081     if (require_complete)
3082       {
3083         if (TREE_CODE (value_type) == VOID_TYPE)
3084           return result;
3085         result = require_complete_type (result);
3086       }
3087     if (IS_AGGR_TYPE (value_type))
3088       result = build_cplus_new (value_type, result);
3089     return convert_from_reference (result);
3090   }
3091 }
3092
3093 tree
3094 build_function_call (function, params)
3095      tree function, params;
3096 {
3097   return build_function_call_real (function, params, 1, LOOKUP_NORMAL);
3098 }
3099 \f
3100 /* Convert the actual parameter expressions in the list VALUES
3101    to the types in the list TYPELIST.
3102    If parmdecls is exhausted, or when an element has NULL as its type,
3103    perform the default conversions.
3104
3105    NAME is an IDENTIFIER_NODE or 0.  It is used only for error messages.
3106
3107    This is also where warnings about wrong number of args are generated.
3108    
3109    Return a list of expressions for the parameters as converted.
3110
3111    Both VALUES and the returned value are chains of TREE_LIST nodes
3112    with the elements of the list in the TREE_VALUE slots of those nodes.
3113
3114    In C++, unspecified trailing parameters can be filled in with their
3115    default arguments, if such were specified.  Do so here.  */
3116
3117 tree
3118 convert_arguments (typelist, values, fndecl, flags)
3119      tree typelist, values, fndecl;
3120      int flags;
3121 {
3122   register tree typetail, valtail;
3123   register tree result = NULL_TREE;
3124   const char *called_thing = 0;
3125   int i = 0;
3126
3127   /* Argument passing is always copy-initialization.  */
3128   flags |= LOOKUP_ONLYCONVERTING;
3129
3130   if (fndecl)
3131     {
3132       if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3133         {
3134           if (DECL_NAME (fndecl) == NULL_TREE
3135               || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3136             called_thing = "constructor";
3137           else
3138             called_thing = "member function";
3139         }
3140       else
3141         called_thing = "function";
3142     }
3143
3144   for (valtail = values, typetail = typelist;
3145        valtail;
3146        valtail = TREE_CHAIN (valtail), i++)
3147     {
3148       register tree type = typetail ? TREE_VALUE (typetail) : 0;
3149       register tree val = TREE_VALUE (valtail);
3150
3151       if (val == error_mark_node)
3152         return error_mark_node;
3153
3154       if (type == void_type_node)
3155         {
3156           if (fndecl)
3157             {
3158               cp_error_at ("too many arguments to %s `%+#D'", called_thing,
3159                            fndecl);
3160               error ("at this point in file");
3161             }
3162           else
3163             error ("too many arguments to function");
3164           /* In case anybody wants to know if this argument
3165              list is valid.  */
3166           if (result)
3167             TREE_TYPE (tree_last (result)) = error_mark_node;
3168           break;
3169         }
3170
3171       if (TREE_CODE (val) == OFFSET_REF)
3172         val = resolve_offset_ref (val);
3173
3174       /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3175          Strip such NOP_EXPRs, since VAL is used in non-lvalue context.  */
3176       if (TREE_CODE (val) == NOP_EXPR
3177           && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3178           && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3179         val = TREE_OPERAND (val, 0);
3180
3181       if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3182         {
3183           if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3184               || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3185               || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3186             val = default_conversion (val);
3187         }
3188
3189       if (val == error_mark_node)
3190         return error_mark_node;
3191
3192       if (type != 0)
3193         {
3194           /* Formal parm type is specified by a function prototype.  */
3195           tree parmval;
3196
3197           if (TYPE_SIZE (complete_type (type)) == 0)
3198             {
3199               error ("parameter type of called function is incomplete");
3200               parmval = val;
3201             }
3202           else
3203             {
3204               parmval = convert_for_initialization
3205                 (NULL_TREE, type, val, flags,
3206                  "argument passing", fndecl, i);
3207               if (PROMOTE_PROTOTYPES
3208                   && (TREE_CODE (type) == INTEGER_TYPE
3209                       || TREE_CODE (type) == ENUMERAL_TYPE)
3210                   && (TYPE_PRECISION (type)
3211                       < TYPE_PRECISION (integer_type_node)))
3212                 parmval = default_conversion (parmval);
3213             }
3214
3215           if (parmval == error_mark_node)
3216             return error_mark_node;
3217
3218           result = expr_tree_cons (NULL_TREE, parmval, result);
3219         }
3220       else
3221         {
3222           if (TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
3223             val = convert_from_reference (val);
3224
3225           result = expr_tree_cons (NULL_TREE,
3226                                    convert_arg_to_ellipsis (val),
3227                                    result);
3228         }
3229
3230       if (typetail)
3231         typetail = TREE_CHAIN (typetail);
3232     }
3233
3234   if (typetail != 0 && typetail != void_list_node)
3235     {
3236       /* See if there are default arguments that can be used */
3237       if (TREE_PURPOSE (typetail))
3238         {
3239           for (; typetail != void_list_node; ++i)
3240             {
3241               tree parmval 
3242                 = convert_default_arg (TREE_VALUE (typetail), 
3243                                        TREE_PURPOSE (typetail), 
3244                                        fndecl);
3245
3246               if (parmval == error_mark_node)
3247                 return error_mark_node;
3248
3249               result = expr_tree_cons (0, parmval, result);
3250               typetail = TREE_CHAIN (typetail);
3251               /* ends with `...'.  */
3252               if (typetail == NULL_TREE)
3253                 break;
3254             }
3255         }
3256       else
3257         {
3258           if (fndecl)
3259             {
3260               cp_error_at ("too few arguments to %s `%+#D'",
3261                            called_thing, fndecl);
3262               error ("at this point in file");
3263             }
3264           else
3265             error ("too few arguments to function");
3266           return error_mark_list;
3267         }
3268     }
3269
3270   return nreverse (result);
3271 }
3272 \f
3273 /* Build a binary-operation expression, after performing default
3274    conversions on the operands.  CODE is the kind of expression to build.  */
3275
3276 tree
3277 build_x_binary_op (code, arg1, arg2)
3278      enum tree_code code;
3279      tree arg1, arg2;
3280 {
3281   if (processing_template_decl)
3282     return build_min_nt (code, arg1, arg2);
3283
3284   return build_new_op (code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE);
3285 }
3286
3287 tree
3288 build_binary_op (code, arg1, arg2)
3289      enum tree_code code;
3290      tree arg1, arg2;
3291 {
3292   return build_binary_op_nodefault (code, arg1, arg2, code);
3293 }
3294
3295 /* Build a binary-operation expression without default conversions.
3296    CODE is the kind of expression to build.
3297    This function differs from `build' in several ways:
3298    the data type of the result is computed and recorded in it,
3299    warnings are generated if arg data types are invalid,
3300    special handling for addition and subtraction of pointers is known,
3301    and some optimization is done (operations on narrow ints
3302    are done in the narrower type when that gives the same result).
3303    Constant folding is also done before the result is returned.
3304
3305    ERROR_CODE is the code that determines what to say in error messages.
3306    It is usually, but not always, the same as CODE.
3307
3308    Note that the operands will never have enumeral types
3309    because either they have just had the default conversions performed
3310    or they have both just been converted to some other type in which
3311    the arithmetic is to be done.
3312
3313    C++: must do special pointer arithmetic when implementing
3314    multiple inheritance, and deal with pointer to member functions.  */
3315
3316 tree
3317 build_binary_op_nodefault (code, orig_op0, orig_op1, error_code)
3318      enum tree_code code;
3319      tree orig_op0, orig_op1;
3320      enum tree_code error_code;
3321 {
3322   tree op0, op1;
3323   register enum tree_code code0, code1;
3324   tree type0, type1;
3325
3326   /* Expression code to give to the expression when it is built.
3327      Normally this is CODE, which is what the caller asked for,
3328      but in some special cases we change it.  */
3329   register enum tree_code resultcode = code;
3330
3331   /* Data type in which the computation is to be performed.
3332      In the simplest cases this is the common type of the arguments.  */
3333   register tree result_type = NULL;
3334
3335   /* Nonzero means operands have already been type-converted
3336      in whatever way is necessary.
3337      Zero means they need to be converted to RESULT_TYPE.  */
3338   int converted = 0;
3339
3340   /* Nonzero means create the expression with this type, rather than
3341      RESULT_TYPE.  */
3342   tree build_type = 0;
3343
3344   /* Nonzero means after finally constructing the expression
3345      convert it to this type.  */
3346   tree final_type = 0;
3347
3348   /* Nonzero if this is an operation like MIN or MAX which can
3349      safely be computed in short if both args are promoted shorts.
3350      Also implies COMMON.
3351      -1 indicates a bitwise operation; this makes a difference
3352      in the exact conditions for when it is safe to do the operation
3353      in a narrower mode.  */
3354   int shorten = 0;
3355
3356   /* Nonzero if this is a comparison operation;
3357      if both args are promoted shorts, compare the original shorts.
3358      Also implies COMMON.  */
3359   int short_compare = 0;
3360
3361   /* Nonzero if this is a right-shift operation, which can be computed on the
3362      original short and then promoted if the operand is a promoted short.  */
3363   int short_shift = 0;
3364
3365   /* Nonzero means set RESULT_TYPE to the common type of the args.  */
3366   int common = 0;
3367
3368   /* Apply default conversions.  */
3369   if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3370       || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3371       || code == TRUTH_XOR_EXPR)
3372     {
3373       op0 = decay_conversion (orig_op0);
3374       op1 = decay_conversion (orig_op1);
3375     }
3376   else
3377     {
3378       op0 = default_conversion (orig_op0);
3379       op1 = default_conversion (orig_op1);
3380     }
3381
3382   /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue.  */
3383   STRIP_TYPE_NOPS (op0);
3384   STRIP_TYPE_NOPS (op1);
3385
3386   /* DTRT if one side is an overloaded function, but complain about it.  */
3387   if (type_unknown_p (op0))
3388     {
3389       tree t = instantiate_type (TREE_TYPE (op1), op0, 0);
3390       if (t != error_mark_node)
3391         {
3392           cp_pedwarn ("assuming cast to `%T' from overloaded function",
3393                       TREE_TYPE (t));
3394           op0 = t;
3395         }
3396     }
3397   if (type_unknown_p (op1))
3398     {
3399       tree t = instantiate_type (TREE_TYPE (op0), op1, 0);
3400       if (t != error_mark_node)
3401         {
3402           cp_pedwarn ("assuming cast to `%T' from overloaded function",
3403                       TREE_TYPE (t));
3404           op1 = t;
3405         }
3406     }
3407
3408   type0 = TREE_TYPE (op0);
3409   type1 = TREE_TYPE (op1);
3410
3411   /* The expression codes of the data types of the arguments tell us
3412      whether the arguments are integers, floating, pointers, etc.  */
3413   code0 = TREE_CODE (type0);
3414   code1 = TREE_CODE (type1);
3415
3416   /* If an error was already reported for one of the arguments,
3417      avoid reporting another error.  */
3418
3419   if (code0 == ERROR_MARK || code1 == ERROR_MARK)
3420     return error_mark_node;
3421
3422   switch (code)
3423     {
3424     case PLUS_EXPR:
3425       /* Handle the pointer + int case.  */
3426       if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3427         return pointer_int_sum (PLUS_EXPR, op0, op1);
3428       else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
3429         return pointer_int_sum (PLUS_EXPR, op1, op0);
3430       else
3431         common = 1;
3432       break;
3433
3434     case MINUS_EXPR:
3435       /* Subtraction of two similar pointers.
3436          We must subtract them as integers, then divide by object size.  */
3437       if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
3438           && comp_target_types (type0, type1, 1))
3439         return pointer_diff (op0, op1, common_type (type0, type1));
3440       /* Handle pointer minus int.  Just like pointer plus int.  */
3441       else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3442         return pointer_int_sum (MINUS_EXPR, op0, op1);
3443       else
3444         common = 1;
3445       break;
3446
3447     case MULT_EXPR:
3448       common = 1;
3449       break;
3450
3451     case TRUNC_DIV_EXPR:
3452     case CEIL_DIV_EXPR:
3453     case FLOOR_DIV_EXPR:
3454     case ROUND_DIV_EXPR:
3455     case EXACT_DIV_EXPR:
3456       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3457            || code0 == COMPLEX_TYPE)
3458           && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3459               || code1 == COMPLEX_TYPE))
3460         {
3461           if (TREE_CODE (op1) == INTEGER_CST && integer_zerop (op1))
3462             cp_warning ("division by zero in `%E / 0'", op0);
3463           else if (TREE_CODE (op1) == REAL_CST && real_zerop (op1))
3464             cp_warning ("division by zero in `%E / 0.'", op0);
3465               
3466           if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
3467             resultcode = RDIV_EXPR;
3468           else
3469             /* When dividing two signed integers, we have to promote to int.
3470                unless we divide by a constant != -1.  Note that default
3471                conversion will have been performed on the operands at this
3472                point, so we have to dig out the original type to find out if
3473                it was unsigned.  */
3474             shorten = ((TREE_CODE (op0) == NOP_EXPR
3475                         && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3476                        || (TREE_CODE (op1) == INTEGER_CST
3477                            && (TREE_INT_CST_LOW (op1) != -1
3478                                || TREE_INT_CST_HIGH (op1) != -1)));
3479           common = 1;
3480         }
3481       break;
3482
3483     case BIT_AND_EXPR:
3484     case BIT_ANDTC_EXPR:
3485     case BIT_IOR_EXPR:
3486     case BIT_XOR_EXPR:
3487       if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3488         shorten = -1;
3489       /* If one operand is a constant, and the other is a short type
3490          that has been converted to an int,
3491          really do the work in the short type and then convert the
3492          result to int.  If we are lucky, the constant will be 0 or 1
3493          in the short type, making the entire operation go away.  */
3494       if (TREE_CODE (op0) == INTEGER_CST
3495           && TREE_CODE (op1) == NOP_EXPR
3496           && (TYPE_PRECISION (type1)
3497               > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0))))
3498           && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op1, 0))))
3499         {
3500           final_type = result_type;
3501           op1 = TREE_OPERAND (op1, 0);
3502           result_type = TREE_TYPE (op1);
3503         }
3504       if (TREE_CODE (op1) == INTEGER_CST
3505           && TREE_CODE (op0) == NOP_EXPR
3506           && (TYPE_PRECISION (type0)
3507               > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0))))
3508           && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3509         {
3510           final_type = result_type;
3511           op0 = TREE_OPERAND (op0, 0);
3512           result_type = TREE_TYPE (op0);
3513         }
3514       break;
3515
3516     case TRUNC_MOD_EXPR:
3517     case FLOOR_MOD_EXPR:
3518       if (code1 == INTEGER_TYPE && integer_zerop (op1))
3519         cp_warning ("division by zero in `%E %% 0'", op0);
3520       else if (code1 == REAL_TYPE && real_zerop (op1))
3521         cp_warning ("division by zero in `%E %% 0.'", op0);
3522       
3523       if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3524         {
3525           /* Although it would be tempting to shorten always here, that loses
3526              on some targets, since the modulo instruction is undefined if the
3527              quotient can't be represented in the computation mode.  We shorten
3528              only if unsigned or if dividing by something we know != -1.  */
3529           shorten = ((TREE_CODE (op0) == NOP_EXPR
3530                       && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
3531                      || (TREE_CODE (op1) == INTEGER_CST
3532                          && (TREE_INT_CST_LOW (op1) != -1
3533                              || TREE_INT_CST_HIGH (op1) != -1)));
3534           common = 1;
3535         }
3536       break;
3537
3538     case TRUTH_ANDIF_EXPR:
3539     case TRUTH_ORIF_EXPR:
3540     case TRUTH_AND_EXPR:
3541     case TRUTH_OR_EXPR:
3542       result_type = boolean_type_node;
3543       break;
3544
3545       /* Shift operations: result has same type as first operand;
3546          always convert second operand to int.
3547          Also set SHORT_SHIFT if shifting rightward.  */
3548
3549     case RSHIFT_EXPR:
3550       if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3551         {
3552           result_type = type0;
3553           if (TREE_CODE (op1) == INTEGER_CST)
3554             {
3555               if (tree_int_cst_lt (op1, integer_zero_node))
3556                 warning ("right shift count is negative");
3557               else
3558                 {
3559                   if (TREE_INT_CST_LOW (op1) | TREE_INT_CST_HIGH (op1))
3560                     short_shift = 1;
3561                   if (TREE_INT_CST_HIGH (op1) != 0
3562                       || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3563                           >= TYPE_PRECISION (type0)))
3564                     warning ("right shift count >= width of type");
3565                 }
3566             }
3567           /* Convert the shift-count to an integer, regardless of
3568              size of value being shifted.  */
3569           if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3570             op1 = cp_convert (integer_type_node, op1);
3571           /* Avoid converting op1 to result_type later.  */
3572           converted = 1;
3573         }
3574       break;
3575
3576     case LSHIFT_EXPR:
3577       if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3578         {
3579           result_type = type0;
3580           if (TREE_CODE (op1) == INTEGER_CST)
3581             {
3582               if (tree_int_cst_lt (op1, integer_zero_node))
3583                 warning ("left shift count is negative");
3584               else if (TREE_INT_CST_HIGH (op1) != 0
3585                        || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3586                            >= TYPE_PRECISION (type0)))
3587                 warning ("left shift count >= width of type");
3588             }
3589           /* Convert the shift-count to an integer, regardless of
3590              size of value being shifted.  */
3591           if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3592             op1 = cp_convert (integer_type_node, op1);
3593           /* Avoid converting op1 to result_type later.  */
3594           converted = 1;
3595         }
3596       break;
3597
3598     case RROTATE_EXPR:
3599     case LROTATE_EXPR:
3600       if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3601         {
3602           result_type = type0;
3603           if (TREE_CODE (op1) == INTEGER_CST)
3604             {
3605               if (tree_int_cst_lt (op1, integer_zero_node))
3606                 warning ("%s rotate count is negative",
3607                          (code == LROTATE_EXPR) ? "left" : "right");
3608               else if (TREE_INT_CST_HIGH (op1) != 0
3609                        || ((unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (op1)
3610                            >= TYPE_PRECISION (type0)))
3611                 warning ("%s rotate count >= width of type",
3612                          (code == LROTATE_EXPR) ? "left" : "right");
3613             }
3614           /* Convert the shift-count to an integer, regardless of
3615              size of value being shifted.  */
3616           if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3617             op1 = cp_convert (integer_type_node, op1);
3618         }
3619       break;
3620
3621     case EQ_EXPR:
3622     case NE_EXPR:
3623       build_type = boolean_type_node; 
3624       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
3625            || code0 == COMPLEX_TYPE)
3626           && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
3627               || code1 == COMPLEX_TYPE))
3628         short_compare = 1;
3629       else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3630         {
3631           register tree tt0 = TYPE_MAIN_VARIANT (TREE_TYPE (type0));
3632           register tree tt1 = TYPE_MAIN_VARIANT (TREE_TYPE (type1));
3633
3634           if (comp_target_types (type0, type1, 1))
3635             result_type = common_type (type0, type1);
3636           else if (tt0 == void_type_node)
3637             {
3638               if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE
3639                   && tree_int_cst_lt (TYPE_SIZE (type0), TYPE_SIZE (type1)))
3640                 pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3641               else if (TREE_CODE (tt1) == OFFSET_TYPE)
3642                 pedwarn ("ANSI C++ forbids conversion of a pointer to member to `void *'");
3643             }
3644           else if (tt1 == void_type_node)
3645             {
3646               if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE
3647                   && tree_int_cst_lt (TYPE_SIZE (type1), TYPE_SIZE (type0)))
3648                 pedwarn ("ANSI C++ forbids comparison of `void *' with function pointer");
3649             }
3650           else
3651             cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3652                         type0, type1);
3653
3654           if (result_type == NULL_TREE)
3655             result_type = ptr_type_node;
3656         }
3657       else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
3658         result_type = type0;
3659       else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
3660         result_type = type1;
3661       else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3662         {
3663           result_type = type0;
3664           error ("ANSI C++ forbids comparison between pointer and integer");
3665         }
3666       else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3667         {
3668           result_type = type1;
3669           error ("ANSI C++ forbids comparison between pointer and integer");
3670         }
3671       else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
3672         {
3673           op0 = build_component_ref (op0, index_identifier, NULL_TREE, 0);
3674           op1 = integer_zero_node;
3675           result_type = TREE_TYPE (op0);
3676         }
3677       else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
3678         {
3679           op0 = build_component_ref (op1, index_identifier, NULL_TREE, 0);
3680           op1 = integer_zero_node;
3681           result_type = TREE_TYPE (op0);
3682         }
3683       else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1)
3684                && same_type_p (type0, type1))
3685         {
3686           /* The code we generate for the test is:
3687
3688           (op0.index == op1.index
3689            && ((op1.index != -1 && op0.delta2 == op1.delta2)
3690                || op0.pfn == op1.pfn)) */
3691
3692           tree index0 = build_component_ref (op0, index_identifier,
3693                                              NULL_TREE, 0);
3694           tree index1 = save_expr (build_component_ref (op1, index_identifier,
3695                                                         NULL_TREE, 0));
3696           tree pfn0 = PFN_FROM_PTRMEMFUNC (op0);
3697           tree pfn1 = PFN_FROM_PTRMEMFUNC (op1);
3698           tree delta20 = DELTA2_FROM_PTRMEMFUNC (op0);
3699           tree delta21 = DELTA2_FROM_PTRMEMFUNC (op1);
3700           tree e1, e2, e3;
3701           tree integer_neg_one_node
3702             = build_binary_op (MINUS_EXPR, integer_zero_node,
3703                                integer_one_node);
3704           e1 = build_binary_op (EQ_EXPR, index0, index1);
3705           e2 = build_binary_op (NE_EXPR, index1, integer_neg_one_node);
3706           e2 = build_binary_op (TRUTH_ANDIF_EXPR, e2,
3707                                 build_binary_op (EQ_EXPR, delta20, delta21));
3708           /* We can't use build_binary_op for this cmp because it would get
3709              confused by the ptr to method types and think we want pmfs.  */
3710           e3 = build (EQ_EXPR, boolean_type_node, pfn0, pfn1);
3711           e2 = build_binary_op (TRUTH_ORIF_EXPR, e2, e3);
3712           e2 = build_binary_op (TRUTH_ANDIF_EXPR, e1, e2);
3713           if (code == EQ_EXPR)
3714             return e2;
3715           return build_binary_op (EQ_EXPR, e2, integer_zero_node);
3716         }
3717       else if (TYPE_PTRMEMFUNC_P (type0)
3718                && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0), type1))
3719         {
3720           tree index0 = build_component_ref (op0, index_identifier,
3721                                              NULL_TREE, 0);
3722           tree index1;
3723           tree pfn0 = PFN_FROM_PTRMEMFUNC (op0);
3724           tree delta20 = DELTA2_FROM_PTRMEMFUNC (op0);
3725           tree delta21 = integer_zero_node;
3726           tree e1, e2, e3;
3727           tree integer_neg_one_node
3728             = build_binary_op (MINUS_EXPR, integer_zero_node, integer_one_node);
3729           if (TREE_CODE (TREE_OPERAND (op1, 0)) == FUNCTION_DECL
3730               && DECL_VINDEX (TREE_OPERAND (op1, 0)))
3731             {
3732               /* Map everything down one to make room for
3733                  the null pointer to member.  */
3734               index1 = size_binop (PLUS_EXPR,
3735                                    DECL_VINDEX (TREE_OPERAND (op1, 0)),
3736                                    integer_one_node);
3737               op1 = integer_zero_node;
3738               delta21 = CLASSTYPE_VFIELD (TYPE_METHOD_BASETYPE
3739                                           (TREE_TYPE (type1)));
3740               delta21 = DECL_FIELD_BITPOS (delta21);
3741               delta21 = size_binop (FLOOR_DIV_EXPR, delta21,
3742                                     size_int (BITS_PER_UNIT));
3743               delta21 = convert (sizetype, delta21);
3744             }
3745           else
3746             index1 = integer_neg_one_node;
3747           {
3748             tree nop1 = build1 (NOP_EXPR, TYPE_PTRMEMFUNC_FN_TYPE (type0),
3749                                 op1);
3750             TREE_CONSTANT (nop1) = TREE_CONSTANT (op1);
3751             op1 = nop1;
3752           }
3753           e1 = build_binary_op (EQ_EXPR, index0, index1);
3754           e2 = build_binary_op (NE_EXPR, index1, integer_neg_one_node);
3755           e2 = build_binary_op (TRUTH_ANDIF_EXPR, e2,
3756                                 build_binary_op (EQ_EXPR, delta20, delta21));
3757           /* We can't use build_binary_op for this cmp because it would get
3758              confused by the ptr to method types and think we want pmfs.  */
3759           e3 = build (EQ_EXPR, boolean_type_node, pfn0, op1);
3760           e2 = build_binary_op (TRUTH_ORIF_EXPR, e2, e3);
3761           e2 = build_binary_op (TRUTH_ANDIF_EXPR, e1, e2);
3762           if (code == EQ_EXPR)
3763             return e2;
3764           return build_binary_op (EQ_EXPR, e2, integer_zero_node);
3765         }
3766       else if (TYPE_PTRMEMFUNC_P (type1)
3767                && same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1), type0))
3768         return build_binary_op (code, op1, op0);
3769       break;
3770
3771     case MAX_EXPR:
3772     case MIN_EXPR:
3773       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3774            && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3775         shorten = 1;
3776       else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3777         {
3778           if (comp_target_types (type0, type1, 1))
3779             result_type = common_type (type0, type1);
3780           else
3781             {
3782               cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3783                           type0, type1);
3784               result_type = ptr_type_node;
3785             }
3786         }
3787       break;
3788
3789     case LE_EXPR:
3790     case GE_EXPR:
3791     case LT_EXPR:
3792     case GT_EXPR:
3793       build_type = boolean_type_node;
3794       if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3795            && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3796         short_compare = 1;
3797       else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
3798         {
3799           if (comp_target_types (type0, type1, 1))
3800             result_type = common_type (type0, type1);
3801           else
3802             {
3803               cp_pedwarn ("comparison of distinct pointer types `%T' and `%T' lacks a cast",
3804                           type0, type1);
3805               result_type = ptr_type_node;
3806             }
3807         }
3808       else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
3809                && integer_zerop (op1))
3810         result_type = type0;
3811       else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
3812                && integer_zerop (op0))
3813         result_type = type1;
3814       else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
3815         {
3816           result_type = type0;
3817           pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3818         }
3819       else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
3820         {
3821           result_type = type1;
3822           pedwarn ("ANSI C++ forbids comparison between pointer and integer");
3823         }
3824       break;
3825
3826     default:
3827       break;
3828     }
3829
3830   if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
3831       &&
3832       (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
3833     {
3834       int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
3835
3836       if (shorten || common || short_compare)
3837         result_type = common_type (type0, type1);
3838
3839       /* For certain operations (which identify themselves by shorten != 0)
3840          if both args were extended from the same smaller type,
3841          do the arithmetic in that type and then extend.
3842
3843          shorten !=0 and !=1 indicates a bitwise operation.
3844          For them, this optimization is safe only if
3845          both args are zero-extended or both are sign-extended.
3846          Otherwise, we might change the result.
3847          Eg, (short)-1 | (unsigned short)-1 is (int)-1
3848          but calculated in (unsigned short) it would be (unsigned short)-1.  */
3849
3850       if (shorten && none_complex)
3851         {
3852           int unsigned0, unsigned1;
3853           tree arg0 = get_narrower (op0, &unsigned0);
3854           tree arg1 = get_narrower (op1, &unsigned1);
3855           /* UNS is 1 if the operation to be done is an unsigned one.  */
3856           int uns = TREE_UNSIGNED (result_type);
3857           tree type;
3858
3859           final_type = result_type;
3860
3861           /* Handle the case that OP0 does not *contain* a conversion
3862              but it *requires* conversion to FINAL_TYPE.  */
3863
3864           if (op0 == arg0 && TREE_TYPE (op0) != final_type)
3865             unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
3866           if (op1 == arg1 && TREE_TYPE (op1) != final_type)
3867             unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
3868
3869           /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE.  */
3870
3871           /* For bitwise operations, signedness of nominal type
3872              does not matter.  Consider only how operands were extended.  */
3873           if (shorten == -1)
3874             uns = unsigned0;
3875
3876           /* Note that in all three cases below we refrain from optimizing
3877              an unsigned operation on sign-extended args.
3878              That would not be valid.  */
3879
3880           /* Both args variable: if both extended in same way
3881              from same width, do it in that width.
3882              Do it unsigned if args were zero-extended.  */
3883           if ((TYPE_PRECISION (TREE_TYPE (arg0))
3884                < TYPE_PRECISION (result_type))
3885               && (TYPE_PRECISION (TREE_TYPE (arg1))
3886                   == TYPE_PRECISION (TREE_TYPE (arg0)))
3887               && unsigned0 == unsigned1
3888               && (unsigned0 || !uns))
3889             result_type
3890               = signed_or_unsigned_type (unsigned0,
3891                                          common_type (TREE_TYPE (arg0),
3892                                                       TREE_TYPE (arg1)));
3893           else if (TREE_CODE (arg0) == INTEGER_CST
3894                    && (unsigned1 || !uns)
3895                    && (TYPE_PRECISION (TREE_TYPE (arg1))
3896                        < TYPE_PRECISION (result_type))
3897                    && (type = signed_or_unsigned_type (unsigned1,
3898                                                        TREE_TYPE (arg1)),
3899                        int_fits_type_p (arg0, type)))
3900             result_type = type;
3901           else if (TREE_CODE (arg1) == INTEGER_CST
3902                    && (unsigned0 || !uns)
3903                    && (TYPE_PRECISION (TREE_TYPE (arg0))
3904                        < TYPE_PRECISION (result_type))
3905                    && (type = signed_or_unsigned_type (unsigned0,
3906                                                        TREE_TYPE (arg0)),
3907                        int_fits_type_p (arg1, type)))
3908             result_type = type;
3909         }
3910
3911       /* Shifts can be shortened if shifting right.  */
3912
3913       if (short_shift)
3914         {
3915           int unsigned_arg;
3916           tree arg0 = get_narrower (op0, &unsigned_arg);
3917
3918           final_type = result_type;
3919
3920           if (arg0 == op0 && final_type == TREE_TYPE (op0))
3921             unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
3922
3923           if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
3924               /* We can shorten only if the shift count is less than the
3925                  number of bits in the smaller type size.  */
3926               && TREE_INT_CST_HIGH (op1) == 0
3927               && TYPE_PRECISION (TREE_TYPE (arg0)) > TREE_INT_CST_LOW (op1)
3928               /* If arg is sign-extended and then unsigned-shifted,
3929                  we can simulate this with a signed shift in arg's type
3930                  only if the extended result is at least twice as wide
3931                  as the arg.  Otherwise, the shift could use up all the
3932                  ones made by sign-extension and bring in zeros.
3933                  We can't optimize that case at all, but in most machines
3934                  it never happens because available widths are 2**N.  */
3935               && (!TREE_UNSIGNED (final_type)
3936                   || unsigned_arg
3937                   || (((unsigned) 2 * TYPE_PRECISION (TREE_TYPE (arg0)))
3938                       <= TYPE_PRECISION (result_type))))
3939             {
3940               /* Do an unsigned shift if the operand was zero-extended.  */
3941               result_type
3942                 = signed_or_unsigned_type (unsigned_arg,
3943                                            TREE_TYPE (arg0));
3944               /* Convert value-to-be-shifted to that type.  */
3945               if (TREE_TYPE (op0) != result_type)
3946                 op0 = cp_convert (result_type, op0);
3947               converted = 1;
3948             }
3949         }
3950
3951       /* Comparison operations are shortened too but differently.
3952          They identify themselves by setting short_compare = 1.  */
3953
3954       if (short_compare)
3955         {
3956           /* Don't write &op0, etc., because that would prevent op0
3957              from being kept in a register.
3958              Instead, make copies of the our local variables and
3959              pass the copies by reference, then copy them back afterward.  */
3960           tree xop0 = op0, xop1 = op1, xresult_type = result_type;
3961           enum tree_code xresultcode = resultcode;
3962           tree val 
3963             = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
3964           if (val != 0)
3965             return cp_convert (boolean_type_node, val);
3966           op0 = xop0, op1 = xop1;
3967           converted = 1;
3968           resultcode = xresultcode;
3969         }
3970
3971       if (short_compare && warn_sign_compare)
3972         {
3973           int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
3974           int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
3975
3976           int unsignedp0, unsignedp1;
3977           tree primop0 = get_narrower (op0, &unsignedp0);
3978           tree primop1 = get_narrower (op1, &unsignedp1);
3979
3980           /* Check for comparison of different enum types.  */
3981           if (TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE 
3982               && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE 
3983               && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
3984                  != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
3985             {
3986               cp_warning ("comparison between `%#T' and `%#T'", 
3987                           TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
3988             }
3989
3990           /* Give warnings for comparisons between signed and unsigned
3991              quantities that may fail.  */
3992           /* Do the checking based on the original operand trees, so that
3993              casts will be considered, but default promotions won't be.  */
3994
3995           /* Do not warn if the comparison is being done in a signed type,
3996              since the signed type will only be chosen if it can represent
3997              all the values of the unsigned type.  */
3998           if (! TREE_UNSIGNED (result_type))
3999             /* OK */;
4000           /* Do not warn if both operands are unsigned.  */
4001           else if (op0_signed == op1_signed)
4002             /* OK */;
4003           /* Do not warn if the signed quantity is an unsuffixed
4004              integer literal (or some static constant expression
4005              involving such literals) and it is non-negative.  */
4006           else if ((op0_signed && TREE_CODE (orig_op0) == INTEGER_CST
4007                     && tree_int_cst_sgn (orig_op0) >= 0)
4008                    || (op1_signed && TREE_CODE (orig_op1) == INTEGER_CST
4009                        && tree_int_cst_sgn (orig_op1) >= 0))
4010             /* OK */;
4011           /* Do not warn if the comparison is an equality operation,
4012              the unsigned quantity is an integral constant and it does
4013              not use the most significant bit of result_type.  */
4014           else if ((resultcode == EQ_EXPR || resultcode == NE_EXPR)
4015                    && ((op0_signed && TREE_CODE (orig_op1) == INTEGER_CST
4016                         && int_fits_type_p (orig_op1,
4017                                             signed_type (result_type)))
4018                         || (op1_signed && TREE_CODE (orig_op0) == INTEGER_CST
4019                             && int_fits_type_p (orig_op0,
4020                                                 signed_type (result_type)))))
4021             /* OK */;
4022           else
4023             warning ("comparison between signed and unsigned");
4024
4025           /* Warn if two unsigned values are being compared in a size
4026              larger than their original size, and one (and only one) is the
4027              result of a `~' operator.  This comparison will always fail.
4028
4029              Also warn if one operand is a constant, and the constant does not
4030              have all bits set that are set in the ~ operand when it is
4031              extended.  */
4032
4033           if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
4034               ^ (TREE_CODE (primop1) == BIT_NOT_EXPR))
4035             {
4036               if (TREE_CODE (primop0) == BIT_NOT_EXPR)
4037                 primop0 = get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
4038               if (TREE_CODE (primop1) == BIT_NOT_EXPR)
4039                 primop1 = get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
4040               
4041               if (TREE_CODE (primop0) == INTEGER_CST
4042                   || TREE_CODE (primop1) == INTEGER_CST)
4043                 {
4044                   tree primop;
4045                   HOST_WIDE_INT constant, mask;
4046                   int unsignedp;
4047                   unsigned bits;
4048
4049                   if (TREE_CODE (primop0) == INTEGER_CST)
4050                     {
4051                       primop = primop1;
4052                       unsignedp = unsignedp1;
4053                       constant = TREE_INT_CST_LOW (primop0);
4054                     }
4055                   else
4056                     {
4057                       primop = primop0;
4058                       unsignedp = unsignedp0;
4059                       constant = TREE_INT_CST_LOW (primop1);
4060                     }
4061
4062                   bits = TYPE_PRECISION (TREE_TYPE (primop));
4063                   if (bits < TYPE_PRECISION (result_type)
4064                       && bits < HOST_BITS_PER_LONG && unsignedp)
4065                     {
4066                       mask = (~ (HOST_WIDE_INT) 0) << bits;
4067                       if ((mask & constant) != mask)
4068                         warning ("comparison of promoted ~unsigned with constant");
4069                     }
4070                 }
4071               else if (unsignedp0 && unsignedp1
4072                        && (TYPE_PRECISION (TREE_TYPE (primop0))
4073                            < TYPE_PRECISION (result_type))
4074                        && (TYPE_PRECISION (TREE_TYPE (primop1))
4075                            < TYPE_PRECISION (result_type)))
4076                 warning ("comparison of promoted ~unsigned with unsigned");
4077             }
4078         }
4079     }
4080
4081   /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
4082      If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
4083      Then the expression will be built.
4084      It will be given type FINAL_TYPE if that is nonzero;
4085      otherwise, it will be given type RESULT_TYPE.  */
4086
4087   if (!result_type)
4088     {
4089       cp_error ("invalid operands `%T' and `%T' to binary `%O'",
4090                 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), error_code);
4091       return error_mark_node;
4092     }
4093
4094   /* Issue warnings about peculiar, but legal, uses of NULL.  */
4095   if (/* It's reasonable to use pointer values as operands of &&
4096          and ||, so NULL is no exception.  */
4097       !(code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
4098       && (/* If OP0 is NULL and OP1 is not a pointer, or vice versa.  */
4099           (orig_op0 == null_node
4100            && TREE_CODE (TREE_TYPE (op1)) != POINTER_TYPE)
4101           /* Or vice versa.  */
4102           || (orig_op1 == null_node
4103               && TREE_CODE (TREE_TYPE (op0)) != POINTER_TYPE)
4104           /* Or, both are NULL and the operation was not a comparison.  */
4105           || (orig_op0 == null_node && orig_op1 == null_node 
4106               && code != EQ_EXPR && code != NE_EXPR)))
4107     /* Some sort of arithmetic operation involving NULL was
4108        performed.  Note that pointer-difference and pointer-addition
4109        have already been handled above, and so we don't end up here in
4110        that case.  */
4111     cp_warning ("NULL used in arithmetic");
4112
4113   if (! converted)
4114     {
4115       if (TREE_TYPE (op0) != result_type)
4116         op0 = cp_convert (result_type, op0); 
4117       if (TREE_TYPE (op1) != result_type)
4118         op1 = cp_convert (result_type, op1); 
4119
4120       if (op0 == error_mark_node || op1 == error_mark_node)
4121         return error_mark_node;
4122     }
4123
4124   if (build_type == NULL_TREE)
4125     build_type = result_type;
4126
4127   {
4128     register tree result = build (resultcode, build_type, op0, op1);
4129     register tree folded;
4130
4131     folded = fold (result);
4132     if (folded == result)
4133       TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
4134     if (final_type != 0)
4135       return cp_convert (final_type, folded);
4136     return folded;
4137   }
4138 }
4139 \f
4140 /* Return a tree for the sum or difference (RESULTCODE says which)
4141    of pointer PTROP and integer INTOP.  */
4142
4143 static tree
4144 pointer_int_sum (resultcode, ptrop, intop)
4145      enum tree_code resultcode;
4146      register tree ptrop, intop;
4147 {
4148   tree size_exp;
4149
4150   register tree result;
4151   register tree folded = fold (intop);
4152
4153   /* The result is a pointer of the same type that is being added.  */
4154
4155   register tree result_type = TREE_TYPE (ptrop);
4156
4157   if (!complete_type_or_else (result_type, ptrop))
4158     return error_mark_node;
4159
4160   if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4161     {
4162       if (pedantic || warn_pointer_arith)
4163         pedwarn ("ANSI C++ forbids using pointer of type `void *' in arithmetic");
4164       size_exp = integer_one_node;
4165     }
4166   else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4167     {
4168       if (pedantic || warn_pointer_arith)
4169         pedwarn ("ANSI C++ forbids using pointer to a function in arithmetic");
4170       size_exp = integer_one_node;
4171     }
4172   else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
4173     {
4174       if (pedantic || warn_pointer_arith)
4175         pedwarn ("ANSI C++ forbids using pointer to a method in arithmetic");
4176       size_exp = integer_one_node;
4177     }
4178   else if (TREE_CODE (TREE_TYPE (result_type)) == OFFSET_TYPE)
4179     {
4180       if (pedantic || warn_pointer_arith)
4181         pedwarn ("ANSI C++ forbids using pointer to a member in arithmetic");
4182       size_exp = integer_one_node;
4183     }
4184   else
4185     size_exp = size_in_bytes (complete_type (TREE_TYPE (result_type)));
4186
4187   /* Needed to make OOPS V2R3 work.  */
4188   intop = folded;
4189   if (TREE_CODE (intop) == INTEGER_CST
4190       && TREE_INT_CST_LOW (intop) == 0
4191       && TREE_INT_CST_HIGH (intop) == 0)
4192     return ptrop;
4193
4194   /* If what we are about to multiply by the size of the elements
4195      contains a constant term, apply distributive law
4196      and multiply that constant term separately.
4197      This helps produce common subexpressions.  */
4198
4199   if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4200       && ! TREE_CONSTANT (intop)
4201       && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4202       && TREE_CONSTANT (size_exp))
4203     {
4204       enum tree_code subcode = resultcode;
4205       if (TREE_CODE (intop) == MINUS_EXPR)
4206         subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4207       ptrop = build_binary_op (subcode, ptrop, TREE_OPERAND (intop, 1));
4208       intop = TREE_OPERAND (intop, 0);
4209     }
4210
4211   /* Convert the integer argument to a type the same size as sizetype
4212      so the multiply won't overflow spuriously.  */
4213
4214   if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype))
4215     intop = cp_convert (type_for_size (TYPE_PRECISION (sizetype), 0), intop);
4216
4217   /* Replace the integer argument with a suitable product by the object size.
4218      Do this multiplication as signed, then convert to the appropriate
4219      pointer type (actually unsigned integral).  */
4220
4221   intop = cp_convert (result_type,
4222                       build_binary_op (MULT_EXPR, intop,
4223                                        cp_convert (TREE_TYPE (intop),
4224                                                    size_exp)));
4225
4226   /* Create the sum or difference.  */
4227
4228   result = build (resultcode, result_type, ptrop, intop);
4229
4230   folded = fold (result);
4231   if (folded == result)
4232     TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
4233   return folded;
4234 }
4235
4236 /* Return a tree for the difference of pointers OP0 and OP1.
4237    The resulting tree has type int.  */
4238
4239 static tree
4240 pointer_diff (op0, op1, ptrtype)
4241      register tree op0, op1;
4242      register tree ptrtype;
4243 {
4244   register tree result, folded;
4245   tree restype = ptrdiff_type_node;
4246   tree target_type = TREE_TYPE (ptrtype);
4247
4248   if (!complete_type_or_else (target_type, NULL_TREE))
4249     return error_mark_node;
4250
4251   if (pedantic || warn_pointer_arith)
4252     {
4253       if (TREE_CODE (target_type) == VOID_TYPE)
4254         pedwarn ("ANSI C++ forbids using pointer of type `void *' in subtraction");
4255       if (TREE_CODE (target_type) == FUNCTION_TYPE)
4256         pedwarn ("ANSI C++ forbids using pointer to a function in subtraction");
4257       if (TREE_CODE (target_type) == METHOD_TYPE)
4258         pedwarn ("ANSI C++ forbids using pointer to a method in subtraction");
4259       if (TREE_CODE (target_type) == OFFSET_TYPE)
4260         pedwarn ("ANSI C++ forbids using pointer to a member in subtraction");
4261     }
4262
4263   /* First do the subtraction as integers;
4264      then drop through to build the divide operator.  */
4265
4266   op0 = build_binary_op (MINUS_EXPR, cp_convert (restype, op0),
4267                          cp_convert (restype, op1));
4268
4269   /* This generates an error if op1 is a pointer to an incomplete type.  */
4270   if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (op1))) == 0)
4271     error ("arithmetic on pointer to an incomplete type");
4272
4273   op1 = ((TREE_CODE (target_type) == VOID_TYPE
4274           || TREE_CODE (target_type) == FUNCTION_TYPE
4275           || TREE_CODE (target_type) == METHOD_TYPE
4276           || TREE_CODE (target_type) == OFFSET_TYPE)
4277          ? integer_one_node
4278          : size_in_bytes (target_type));
4279
4280   /* Do the division.  */
4281
4282   result = build (EXACT_DIV_EXPR, restype, op0, cp_convert (restype, op1));
4283
4284   folded = fold (result);
4285   if (folded == result)
4286     TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
4287   return folded;
4288 }
4289 \f
4290 /* Handle the case of taking the address of a COMPONENT_REF.
4291    Called by `build_unary_op'.
4292
4293    ARG is the COMPONENT_REF whose address we want.
4294    ARGTYPE is the pointer type that this address should have. */
4295
4296 static tree
4297 build_component_addr (arg, argtype)
4298      tree arg, argtype;
4299 {
4300   tree field = TREE_OPERAND (arg, 1);
4301   tree basetype = decl_type_context (field);
4302   tree rval = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
4303
4304   my_friendly_assert (TREE_CODE (field) == FIELD_DECL, 981018);
4305
4306   if (DECL_C_BIT_FIELD (field))
4307     {
4308       cp_error ("attempt to take address of bit-field structure member `%D'",
4309                 field);
4310       return error_mark_node;
4311     }
4312
4313   if (TREE_CODE (field) == FIELD_DECL
4314       && TYPE_USES_COMPLEX_INHERITANCE (basetype))
4315     {
4316       /* Can't convert directly to ARGTYPE, since that
4317          may have the same pointer type as one of our
4318          baseclasses.  */
4319       rval = build1 (NOP_EXPR, argtype,
4320                      convert_pointer_to (basetype, rval));
4321       TREE_CONSTANT (rval) = TREE_CONSTANT (TREE_OPERAND (rval, 0));
4322     }
4323   else
4324     /* This conversion is harmless.  */
4325     rval = convert_force (argtype, rval, 0);
4326
4327   if (! integer_zerop (DECL_FIELD_BITPOS (field)))
4328     {
4329       tree offset = size_binop (EASY_DIV_EXPR, DECL_FIELD_BITPOS (field),
4330                                 size_int (BITS_PER_UNIT));
4331       int flag = TREE_CONSTANT (rval);
4332       offset = convert (sizetype, offset);
4333       rval = fold (build (PLUS_EXPR, argtype,
4334                           rval, cp_convert (argtype, offset)));
4335       TREE_CONSTANT (rval) = flag;
4336     }
4337   return rval;
4338 }
4339    
4340 /* Construct and perhaps optimize a tree representation
4341    for a unary operation.  CODE, a tree_code, specifies the operation
4342    and XARG is the operand.  */
4343
4344 tree
4345 build_x_unary_op (code, xarg)
4346      enum tree_code code;
4347      tree xarg;
4348 {
4349   if (processing_template_decl)
4350     return build_min_nt (code, xarg, NULL_TREE);
4351
4352   /* & rec, on incomplete RECORD_TYPEs is the simple opr &, not an
4353      error message.  */
4354   if (code == ADDR_EXPR
4355       && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
4356       && ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (xarg)))
4357            && TYPE_SIZE (TREE_TYPE (xarg)) == NULL_TREE)
4358           || (TREE_CODE (xarg) == OFFSET_REF)))
4359     /* don't look for a function */;
4360   else
4361     {
4362       tree rval;
4363
4364       rval = build_new_op (code, LOOKUP_NORMAL, xarg,
4365                            NULL_TREE, NULL_TREE);
4366       if (rval || code != ADDR_EXPR)
4367         return rval;
4368     }
4369
4370   if (code == ADDR_EXPR)
4371     {
4372       if (TREE_CODE (xarg) == TARGET_EXPR)
4373         warning ("taking address of temporary");
4374     }
4375
4376   return build_unary_op (code, xarg, 0);
4377 }
4378
4379 /* Just like truthvalue_conversion, but we want a CLEANUP_POINT_EXPR.  */
4380    
4381 tree
4382 condition_conversion (expr)
4383      tree expr;
4384 {
4385   tree t;
4386   if (processing_template_decl)
4387     return expr;
4388   t = cp_convert (boolean_type_node, expr);
4389   t = fold (build1 (CLEANUP_POINT_EXPR, boolean_type_node, t));
4390   return t;
4391 }
4392                                
4393 /* C++: Must handle pointers to members.
4394
4395    Perhaps type instantiation should be extended to handle conversion
4396    from aggregates to types we don't yet know we want?  (Or are those
4397    cases typically errors which should be reported?)
4398
4399    NOCONVERT nonzero suppresses the default promotions
4400    (such as from short to int).  */
4401
4402 tree
4403 build_unary_op (code, xarg, noconvert)
4404      enum tree_code code;
4405      tree xarg;
4406      int noconvert;
4407 {
4408   /* No default_conversion here.  It causes trouble for ADDR_EXPR.  */
4409   register tree arg = xarg;
4410   register tree argtype = 0;
4411   const char *errstring = NULL;
4412   tree val;
4413
4414   if (arg == error_mark_node)
4415     return error_mark_node;
4416
4417   switch (code)
4418     {
4419     case CONVERT_EXPR:
4420       /* This is used for unary plus, because a CONVERT_EXPR
4421          is enough to prevent anybody from looking inside for
4422          associativity, but won't generate any code.  */
4423       if (!(arg = build_expr_type_conversion
4424             (WANT_ARITH | WANT_ENUM | WANT_POINTER, arg, 1)))
4425         errstring = "wrong type argument to unary plus";
4426       else
4427         {
4428           if (!noconvert)
4429            arg = default_conversion (arg);
4430           arg = build1 (NON_LVALUE_EXPR, TREE_TYPE (arg), arg);
4431           TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4432         }
4433       break;
4434
4435     case NEGATE_EXPR:
4436       if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4437         errstring = "wrong type argument to unary minus";
4438       else if (!noconvert)
4439         arg = default_conversion (arg);
4440       break;
4441
4442     case BIT_NOT_EXPR:
4443       if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4444         {
4445           code = CONJ_EXPR;
4446           if (!noconvert)
4447             arg = default_conversion (arg);
4448         }
4449       else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4450                                                    arg, 1)))
4451         errstring = "wrong type argument to bit-complement";
4452       else if (!noconvert)
4453         arg = default_conversion (arg);
4454       break;
4455
4456     case ABS_EXPR:
4457       if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4458         errstring = "wrong type argument to abs";
4459       else if (!noconvert)
4460         arg = default_conversion (arg);
4461       break;
4462
4463     case CONJ_EXPR:
4464       /* Conjugating a real value is a no-op, but allow it anyway.  */
4465       if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, 1)))
4466         errstring = "wrong type argument to conjugation";
4467       else if (!noconvert)
4468         arg = default_conversion (arg);
4469       break;
4470
4471     case TRUTH_NOT_EXPR:
4472       arg = cp_convert (boolean_type_node, arg);
4473       val = invert_truthvalue (arg);
4474       if (arg != error_mark_node)
4475         return val;
4476       errstring = "in argument to unary !";
4477       break;
4478
4479     case NOP_EXPR:
4480       break;
4481       
4482     case REALPART_EXPR:
4483       if (TREE_CODE (arg) == COMPLEX_CST)
4484         return TREE_REALPART (arg);
4485       else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4486         return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4487       else
4488         return arg;
4489
4490     case IMAGPART_EXPR:
4491       if (TREE_CODE (arg) == COMPLEX_CST)
4492         return TREE_IMAGPART (arg);
4493       else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4494         return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
4495       else
4496         return cp_convert (TREE_TYPE (arg), integer_zero_node);
4497       
4498     case PREINCREMENT_EXPR:
4499     case POSTINCREMENT_EXPR:
4500     case PREDECREMENT_EXPR:
4501     case POSTDECREMENT_EXPR:
4502       /* Handle complex lvalues (when permitted)
4503          by reduction to simpler cases.  */
4504
4505       val = unary_complex_lvalue (code, arg);
4506       if (val != 0)
4507         return val;
4508
4509       /* Increment or decrement the real part of the value,
4510          and don't change the imaginary part.  */
4511       if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
4512         {
4513           tree real, imag;
4514
4515           arg = stabilize_reference (arg);
4516           real = build_unary_op (REALPART_EXPR, arg, 1);
4517           imag = build_unary_op (IMAGPART_EXPR, arg, 1);
4518           return build (COMPLEX_EXPR, TREE_TYPE (arg),
4519                         build_unary_op (code, real, 1), imag);
4520         }
4521
4522       /* Report invalid types.  */
4523
4524       if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
4525                                               arg, 1)))
4526         {
4527           if (code == PREINCREMENT_EXPR)
4528             errstring ="no pre-increment operator for type";
4529           else if (code == POSTINCREMENT_EXPR)
4530             errstring ="no post-increment operator for type";
4531           else if (code == PREDECREMENT_EXPR)
4532             errstring ="no pre-decrement operator for type";
4533           else
4534             errstring ="no post-decrement operator for type";
4535           break;
4536         }
4537
4538       /* Report something read-only.  */
4539
4540       if (CP_TYPE_CONST_P (TREE_TYPE (arg))
4541           || TREE_READONLY (arg))
4542         readonly_error (arg, ((code == PREINCREMENT_EXPR
4543                                || code == POSTINCREMENT_EXPR)
4544                               ? "increment" : "decrement"),
4545                         0);
4546
4547       {
4548         register tree inc;
4549         tree result_type = TREE_TYPE (arg);
4550
4551         arg = get_unwidened (arg, 0);
4552         argtype = TREE_TYPE (arg);
4553
4554         /* ARM $5.2.5 last annotation says this should be forbidden.  */
4555         if (TREE_CODE (argtype) == ENUMERAL_TYPE)
4556           pedwarn ("ANSI C++ forbids %sing an enum",
4557                    (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4558                    ? "increment" : "decrement");
4559             
4560         /* Compute the increment.  */
4561
4562         if (TREE_CODE (argtype) == POINTER_TYPE)
4563           {
4564             enum tree_code tmp = TREE_CODE (TREE_TYPE (argtype));
4565             if (TYPE_SIZE (complete_type (TREE_TYPE (argtype))) == 0)
4566               cp_error ("cannot %s a pointer to incomplete type `%T'",
4567                         ((code == PREINCREMENT_EXPR
4568                           || code == POSTINCREMENT_EXPR)
4569                          ? "increment" : "decrement"), TREE_TYPE (argtype));
4570             else if ((pedantic || warn_pointer_arith)
4571                      && (tmp == FUNCTION_TYPE || tmp == METHOD_TYPE
4572                          || tmp == VOID_TYPE || tmp == OFFSET_TYPE))
4573               cp_pedwarn ("ANSI C++ forbids %sing a pointer of type `%T'",
4574                           ((code == PREINCREMENT_EXPR
4575                             || code == POSTINCREMENT_EXPR)
4576                            ? "increment" : "decrement"), argtype);
4577             inc = c_sizeof_nowarn (TREE_TYPE (argtype));
4578           }
4579         else
4580           inc = integer_one_node;
4581
4582         inc = cp_convert (argtype, inc);
4583
4584         /* Handle incrementing a cast-expression.  */
4585
4586         switch (TREE_CODE (arg))
4587           {
4588           case NOP_EXPR:
4589           case CONVERT_EXPR:
4590           case FLOAT_EXPR:
4591           case FIX_TRUNC_EXPR:
4592           case FIX_FLOOR_EXPR:
4593           case FIX_ROUND_EXPR:
4594           case FIX_CEIL_EXPR:
4595             {
4596               tree incremented, modify, value, compound;
4597               if (! lvalue_p (arg) && pedantic)
4598                 pedwarn ("cast to non-reference type used as lvalue");
4599               arg = stabilize_reference (arg);
4600               if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
4601                 value = arg;
4602               else
4603                 value = save_expr (arg);
4604               incremented = build (((code == PREINCREMENT_EXPR
4605                                      || code == POSTINCREMENT_EXPR)
4606                                     ? PLUS_EXPR : MINUS_EXPR),
4607                                    argtype, value, inc);
4608               TREE_SIDE_EFFECTS (incremented) = 1;
4609
4610               modify = build_modify_expr (arg, NOP_EXPR, incremented);
4611               compound = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
4612
4613               /* Eliminate warning about unused result of + or -.  */
4614               TREE_NO_UNUSED_WARNING (compound) = 1;
4615               return compound;
4616             }
4617
4618           default:
4619             break;
4620           }
4621
4622         /* Complain about anything else that is not a true lvalue.  */
4623         if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
4624                                     || code == POSTINCREMENT_EXPR)
4625                                    ? "increment" : "decrement")))
4626           return error_mark_node;
4627
4628         /* Forbid using -- on `bool'.  */
4629         if (TREE_TYPE (arg) == boolean_type_node)
4630           {
4631             if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
4632               {
4633                 cp_error ("invalid use of `--' on bool variable `%D'", arg);
4634                 return error_mark_node;
4635               }
4636 #if 0
4637             /* This will only work if someone can convince Kenner to accept
4638                my patch to expand_increment. (jason)  */
4639             val = build (code, TREE_TYPE (arg), arg, inc);
4640 #else
4641             if (code == POSTINCREMENT_EXPR)
4642               {
4643                 arg = stabilize_reference (arg);
4644                 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg,
4645                              boolean_true_node);
4646                 TREE_SIDE_EFFECTS (val) = 1;
4647                 arg = save_expr (arg);
4648                 val = build (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
4649                 val = build (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4650               }
4651             else
4652               val = build (MODIFY_EXPR, TREE_TYPE (arg), arg,
4653                            boolean_true_node);
4654 #endif
4655           }
4656         else
4657           val = build (code, TREE_TYPE (arg), arg, inc);
4658
4659         TREE_SIDE_EFFECTS (val) = 1;
4660         return cp_convert (result_type, val);
4661       }
4662
4663     case ADDR_EXPR:
4664       /* Note that this operation never does default_conversion
4665          regardless of NOCONVERT.  */
4666
4667       argtype = lvalue_type (arg);
4668       if (TREE_CODE (argtype) == REFERENCE_TYPE)
4669         {
4670           arg = build1
4671             (CONVERT_EXPR,
4672              build_pointer_type (TREE_TYPE (argtype)), arg);
4673           TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4674           return arg;
4675         }
4676       else if (pedantic && DECL_MAIN_P (arg))
4677         /* ARM $3.4 */
4678         pedwarn ("taking address of function `main'");
4679
4680       /* Let &* cancel out to simplify resulting code.  */
4681       if (TREE_CODE (arg) == INDIRECT_REF)
4682         {
4683           /* We don't need to have `current_class_ptr' wrapped in a
4684              NON_LVALUE_EXPR node.  */
4685           if (arg == current_class_ref)
4686             return current_class_ptr;
4687
4688           arg = TREE_OPERAND (arg, 0);
4689           if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
4690             {
4691               arg = build1
4692                 (CONVERT_EXPR,
4693                  build_pointer_type (TREE_TYPE (TREE_TYPE (arg))), arg);
4694               TREE_CONSTANT (arg) = TREE_CONSTANT (TREE_OPERAND (arg, 0));
4695             }
4696           else if (lvalue_p (arg))
4697             /* Don't let this be an lvalue.  */
4698             return non_lvalue (arg);
4699           return arg;
4700         }
4701
4702       /* For &x[y], return x+y */
4703       if (TREE_CODE (arg) == ARRAY_REF)
4704         {
4705           if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
4706             return error_mark_node;
4707           return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
4708                                   TREE_OPERAND (arg, 1));
4709         }
4710
4711       /* Uninstantiated types are all functions.  Taking the
4712          address of a function is a no-op, so just return the
4713          argument.  */
4714
4715       if (TREE_CODE (arg) == IDENTIFIER_NODE
4716           && IDENTIFIER_OPNAME_P (arg))
4717         {
4718           my_friendly_abort (117);
4719           /* We don't know the type yet, so just work around the problem.
4720              We know that this will resolve to an lvalue.  */
4721           return build1 (ADDR_EXPR, unknown_type_node, arg);
4722         }
4723
4724       if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
4725           && OVL_NEXT (TREE_OPERAND (arg, 1)) == NULL_TREE)
4726         {
4727           /* They're trying to take the address of a unique non-static
4728              member function.  This is ill-formed, but let's try to DTRT.  */
4729           tree base, name;
4730
4731           if (current_class_type
4732               && TREE_OPERAND (arg, 0) == current_class_ref)
4733             /* An expression like &memfn.  */
4734             pedwarn ("taking the address of a non-static member function");
4735           else
4736             pedwarn ("taking the address of a bound member function");
4737
4738           base = TREE_TYPE (TREE_OPERAND (arg, 0));
4739           name = DECL_NAME (OVL_CURRENT (TREE_OPERAND (arg, 1)));
4740
4741           cp_pedwarn ("  to form a pointer to member function, say `&%T::%D'",
4742                       base, name);
4743           arg = build_offset_ref (base, name);
4744         }
4745
4746       if (type_unknown_p (arg))
4747         return build1 (ADDR_EXPR, unknown_type_node, arg);
4748
4749       /* Handle complex lvalues (when permitted)
4750          by reduction to simpler cases.  */
4751       val = unary_complex_lvalue (code, arg);
4752       if (val != 0)
4753         return val;
4754
4755       switch (TREE_CODE (arg))
4756         {
4757         case NOP_EXPR:
4758         case CONVERT_EXPR:
4759         case FLOAT_EXPR:
4760         case FIX_TRUNC_EXPR:
4761         case FIX_FLOOR_EXPR:
4762         case FIX_ROUND_EXPR:
4763         case FIX_CEIL_EXPR:
4764           if (! lvalue_p (arg) && pedantic)
4765             pedwarn ("taking the address of a cast to non-reference type");
4766           break;
4767           
4768         default:
4769           break;
4770         }
4771
4772       /* Allow the address of a constructor if all the elements
4773          are constant.  */
4774       if (TREE_CODE (arg) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (arg)
4775           && TREE_CONSTANT (arg))
4776         ;
4777       /* Anything not already handled and not a true memory reference
4778          is an error.  */
4779       else if (TREE_CODE (argtype) != FUNCTION_TYPE
4780                && TREE_CODE (argtype) != METHOD_TYPE
4781                && !lvalue_or_else (arg, "unary `&'"))
4782         return error_mark_node;
4783
4784       if (argtype != error_mark_node)
4785         argtype = build_pointer_type (argtype);
4786
4787       if (mark_addressable (arg) == 0)
4788         return error_mark_node;
4789
4790       {
4791         tree addr;
4792
4793         if (TREE_CODE (arg) == COMPONENT_REF)
4794           addr = build_component_addr (arg, argtype);
4795         else
4796           addr = build1 (ADDR_EXPR, argtype, arg);
4797
4798         /* Address of a static or external variable or
4799            function counts as a constant */
4800         if (staticp (arg))
4801           TREE_CONSTANT (addr) = 1;
4802
4803         if (TREE_CODE (argtype) == POINTER_TYPE
4804             && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
4805           {
4806             build_ptrmemfunc_type (argtype);
4807             addr = build_ptrmemfunc (argtype, addr, 0);
4808           }
4809
4810         return addr;
4811       }
4812
4813     default:
4814       break;
4815     }
4816
4817   if (!errstring)
4818     {
4819       if (argtype == 0)
4820         argtype = TREE_TYPE (arg);
4821       return fold (build1 (code, argtype, arg));
4822     }
4823
4824   error (errstring);
4825   return error_mark_node;
4826 }
4827
4828 #if 0
4829 /* If CONVERSIONS is a conversion expression or a nested sequence of such,
4830    convert ARG with the same conversions in the same order
4831    and return the result.  */
4832
4833 static tree
4834 convert_sequence (conversions, arg)
4835      tree conversions;
4836      tree arg;
4837 {
4838   switch (TREE_CODE (conversions))
4839     {
4840     case NOP_EXPR:
4841     case CONVERT_EXPR:
4842     case FLOAT_EXPR:
4843     case FIX_TRUNC_EXPR:
4844     case FIX_FLOOR_EXPR:
4845     case FIX_ROUND_EXPR:
4846     case FIX_CEIL_EXPR:
4847       return cp_convert (TREE_TYPE (conversions),
4848                          convert_sequence (TREE_OPERAND (conversions, 0),
4849                                            arg));
4850
4851     default:
4852       return arg;
4853     }
4854 }
4855 #endif
4856
4857 /* Apply unary lvalue-demanding operator CODE to the expression ARG
4858    for certain kinds of expressions which are not really lvalues
4859    but which we can accept as lvalues.
4860
4861    If ARG is not a kind of expression we can handle, return zero.  */
4862    
4863 tree
4864 unary_complex_lvalue (code, arg)
4865      enum tree_code code;
4866      tree arg;
4867 {
4868   /* Handle (a, b) used as an "lvalue".  */
4869   if (TREE_CODE (arg) == COMPOUND_EXPR)
4870     {
4871       tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
4872       return build (COMPOUND_EXPR, TREE_TYPE (real_result),
4873                     TREE_OPERAND (arg, 0), real_result);
4874     }
4875
4876   /* Handle (a ? b : c) used as an "lvalue".  */
4877   if (TREE_CODE (arg) == COND_EXPR
4878       || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
4879     return rationalize_conditional_expr (code, arg);
4880
4881   if (TREE_CODE (arg) == MODIFY_EXPR
4882       || TREE_CODE (arg) == PREINCREMENT_EXPR
4883       || TREE_CODE (arg) == PREDECREMENT_EXPR)
4884     return unary_complex_lvalue
4885       (code, build (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (arg, 0)),
4886                     arg, TREE_OPERAND (arg, 0)));
4887
4888   if (code != ADDR_EXPR)
4889     return 0;
4890
4891   /* Handle (a = b) used as an "lvalue" for `&'.  */
4892   if (TREE_CODE (arg) == MODIFY_EXPR
4893       || TREE_CODE (arg) == INIT_EXPR)
4894     {
4895       tree real_result = build_unary_op (code, TREE_OPERAND (arg, 0), 0);
4896       arg = build (COMPOUND_EXPR, TREE_TYPE (real_result), arg, real_result);
4897       TREE_NO_UNUSED_WARNING (arg) = 1;
4898       return arg;
4899     }
4900
4901   if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
4902       || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
4903       || TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
4904     {
4905       /* The representation of something of type OFFSET_TYPE
4906          is really the representation of a pointer to it.
4907          Here give the representation its true type.  */
4908       tree t;
4909
4910       my_friendly_assert (TREE_CODE (arg) != SCOPE_REF, 313);
4911
4912       if (TREE_CODE (arg) != OFFSET_REF)
4913         return 0;
4914
4915       t = TREE_OPERAND (arg, 1);
4916
4917       /* Check all this code for right semantics.  */   
4918       if (TREE_CODE (t) == FUNCTION_DECL)
4919         {
4920           if (DECL_DESTRUCTOR_P (t))
4921             cp_error ("taking address of destructor");
4922           return build_unary_op (ADDR_EXPR, t, 0);
4923         }
4924       if (TREE_CODE (t) == VAR_DECL)
4925         return build_unary_op (ADDR_EXPR, t, 0);
4926       else
4927         {
4928           tree type;
4929
4930           if (TREE_OPERAND (arg, 0)
4931               && ! is_dummy_object (TREE_OPERAND (arg, 0))
4932               && TREE_CODE (t) != FIELD_DECL)
4933             {
4934               cp_error ("taking address of bound pointer-to-member expression");
4935               return error_mark_node;
4936             }
4937
4938           type = build_offset_type (DECL_FIELD_CONTEXT (t), TREE_TYPE (t));
4939           type = build_pointer_type (type);
4940
4941           t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
4942           return t;
4943         }
4944     }
4945
4946   
4947   /* We permit compiler to make function calls returning
4948      objects of aggregate type look like lvalues.  */
4949   {
4950     tree targ = arg;
4951
4952     if (TREE_CODE (targ) == SAVE_EXPR)
4953       targ = TREE_OPERAND (targ, 0);
4954
4955     if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (TREE_TYPE (targ)))
4956       {
4957         if (TREE_CODE (arg) == SAVE_EXPR)
4958           targ = arg;
4959         else
4960           targ = build_cplus_new (TREE_TYPE (arg), arg);
4961         return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
4962       }
4963
4964     if (TREE_CODE (arg) == SAVE_EXPR && TREE_CODE (targ) == INDIRECT_REF)
4965       return build (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
4966                      TREE_OPERAND (targ, 0), current_function_decl, NULL);
4967   }
4968
4969   /* Don't let anything else be handled specially.  */
4970   return 0;
4971 }
4972 \f
4973 /* Mark EXP saying that we need to be able to take the
4974    address of it; it should not be allocated in a register.
4975    Value is 1 if successful.
4976
4977    C++: we do not allow `current_class_ptr' to be addressable.  */
4978
4979 int
4980 mark_addressable (exp)
4981      tree exp;
4982 {
4983   register tree x = exp;
4984
4985   if (TREE_ADDRESSABLE (x) == 1)
4986     return 1;
4987
4988   while (1)
4989     switch (TREE_CODE (x))
4990       {
4991       case ADDR_EXPR:
4992       case COMPONENT_REF:
4993       case ARRAY_REF:
4994       case REALPART_EXPR:
4995       case IMAGPART_EXPR:
4996         x = TREE_OPERAND (x, 0);
4997         break;
4998
4999       case PARM_DECL:
5000         if (x == current_class_ptr)
5001           {
5002             if (! flag_this_is_variable)
5003               error ("address of `this' not available");
5004             TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later */
5005             put_var_into_stack (x);
5006             return 1;
5007           }
5008       case VAR_DECL:
5009         if (TREE_STATIC (x) && TREE_READONLY (x)
5010             && DECL_RTL (x) != 0
5011             && ! DECL_IN_MEMORY_P (x))
5012           {
5013             /* We thought this would make a good constant variable,
5014                but we were wrong.  */
5015             push_obstacks_nochange ();
5016             end_temporary_allocation ();
5017
5018             TREE_ASM_WRITTEN (x) = 0;
5019             DECL_RTL (x) = 0;
5020             rest_of_decl_compilation (x, 0, 
5021                                       !DECL_FUNCTION_SCOPE_P (x),
5022                                       0);
5023             TREE_ADDRESSABLE (x) = 1;
5024
5025             pop_obstacks ();
5026
5027             return 1;
5028           }
5029         /* Caller should not be trying to mark initialized
5030            constant fields addressable.  */
5031         my_friendly_assert (DECL_LANG_SPECIFIC (x) == 0
5032                             || DECL_IN_AGGR_P (x) == 0
5033                             || TREE_STATIC (x)
5034                             || DECL_EXTERNAL (x), 314);
5035
5036       case CONST_DECL:
5037       case RESULT_DECL:
5038         if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
5039             && !DECL_ARTIFICIAL (x) && extra_warnings)
5040           cp_warning ("address requested for `%D', which is declared `register'",
5041                       x);
5042         put_var_into_stack (x);
5043         TREE_ADDRESSABLE (x) = 1;
5044         return 1;
5045
5046       case FUNCTION_DECL:
5047         if (DECL_LANG_SPECIFIC (x) != 0)
5048           {
5049             x = DECL_MAIN_VARIANT (x);
5050             /* We have to test both conditions here.  The first may be
5051                non-zero in the case of processing a default function.  The
5052                second may be non-zero in the case of a template function.  */
5053             if (DECL_TEMPLATE_INFO (x) && !DECL_TEMPLATE_SPECIALIZATION (x))
5054               mark_used (x);
5055           }
5056         TREE_ADDRESSABLE (x) = 1;
5057         TREE_USED (x) = 1;
5058         TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
5059         return 1;
5060
5061       case CONSTRUCTOR:
5062         TREE_ADDRESSABLE (x) = 1;
5063         return 1;
5064
5065       case TARGET_EXPR:
5066         TREE_ADDRESSABLE (x) = 1;
5067         mark_addressable (TREE_OPERAND (x, 0));
5068         return 1;
5069
5070       default:
5071         return 1;
5072     }
5073 }
5074 \f
5075 /* Build and return a conditional expression IFEXP ? OP1 : OP2.  */
5076
5077 tree
5078 build_x_conditional_expr (ifexp, op1, op2)
5079      tree ifexp, op1, op2;
5080 {
5081   if (processing_template_decl)
5082     return build_min_nt (COND_EXPR, ifexp, op1, op2);
5083
5084   return build_conditional_expr (ifexp, op1, op2);
5085 }
5086 \f
5087 /* Handle overloading of the ',' operator when needed.  Otherwise,
5088    this function just builds an expression list.  */
5089
5090 tree
5091 build_x_compound_expr (list)
5092      tree list;
5093 {
5094   tree rest = TREE_CHAIN (list);
5095   tree result;
5096
5097   if (processing_template_decl)
5098     return build_min_nt (COMPOUND_EXPR, list, NULL_TREE);
5099
5100   if (rest == NULL_TREE)
5101     return build_compound_expr (list);
5102
5103   result = build_opfncall (COMPOUND_EXPR, LOOKUP_NORMAL,
5104                            TREE_VALUE (list), TREE_VALUE (rest), NULL_TREE);
5105   if (result)
5106     return build_x_compound_expr (expr_tree_cons (NULL_TREE, result,
5107                                                   TREE_CHAIN (rest)));
5108
5109   if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
5110     {
5111       /* the left-hand operand of a comma expression is like an expression
5112          statement: we should warn if it doesn't have any side-effects,
5113          unless it was explicitly cast to (void).  */
5114       if ((extra_warnings || warn_unused)
5115            && !(TREE_CODE (TREE_VALUE(list)) == CONVERT_EXPR
5116                 && TREE_TYPE (TREE_VALUE(list)) == void_type_node))
5117         warning("left-hand operand of comma expression has no effect");
5118     }
5119 #if 0 /* this requires a gcc backend patch to export warn_if_unused_value */
5120   else if (warn_unused)
5121     warn_if_unused_value (TREE_VALUE(list));
5122 #endif
5123
5124   return build_compound_expr
5125     (expr_tree_cons (NULL_TREE, TREE_VALUE (list),
5126                      build_expr_list (NULL_TREE,
5127                                       build_x_compound_expr (rest))));
5128 }
5129
5130 /* Given a list of expressions, return a compound expression
5131    that performs them all and returns the value of the last of them.  */
5132
5133 tree
5134 build_compound_expr (list)
5135      tree list;
5136 {
5137   register tree rest;
5138   tree first;
5139
5140   if (TREE_READONLY_DECL_P (TREE_VALUE (list)))
5141     TREE_VALUE (list) = decl_constant_value (TREE_VALUE (list));
5142
5143   if (TREE_CHAIN (list) == 0)
5144     {
5145       /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5146          Strip such NOP_EXPRs, since LIST is used in non-lvalue context.  */
5147       if (TREE_CODE (list) == NOP_EXPR
5148           && TREE_TYPE (list) == TREE_TYPE (TREE_OPERAND (list, 0)))
5149         list = TREE_OPERAND (list, 0);
5150
5151       /* Convert arrays to pointers.  */
5152       if (TREE_CODE (TREE_TYPE (TREE_VALUE (list))) == ARRAY_TYPE)
5153         return default_conversion (TREE_VALUE (list));
5154       else
5155         return TREE_VALUE (list);
5156     }
5157
5158   first = TREE_VALUE (list);
5159   first = require_complete_type_in_void (first);
5160   if (first == error_mark_node)
5161     return error_mark_node;
5162   
5163   rest = build_compound_expr (TREE_CHAIN (list));
5164   if (rest == error_mark_node)
5165     return error_mark_node;
5166
5167   /* When pedantic, a compound expression cannot be a constant expression.  */
5168   if (! TREE_SIDE_EFFECTS (first) && ! pedantic)
5169     return rest;
5170
5171   return build (COMPOUND_EXPR, TREE_TYPE (rest),
5172                 break_out_cleanups (first), rest);
5173 }
5174
5175 tree
5176 build_static_cast (type, expr)
5177    tree type, expr;
5178 {
5179   tree intype, binfo;
5180   int ok;
5181
5182   if (type == error_mark_node || expr == error_mark_node)
5183     return error_mark_node;
5184
5185   if (TREE_CODE (expr) == OFFSET_REF)
5186     expr = resolve_offset_ref (expr);
5187
5188   if (processing_template_decl)
5189     {
5190       tree t = build_min (STATIC_CAST_EXPR, copy_to_permanent (type),
5191                           expr); 
5192       return t;
5193     }
5194
5195   /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5196      Strip such NOP_EXPRs if VALUE is being used in non-lvalue context.  */
5197   if (TREE_CODE (type) != REFERENCE_TYPE
5198       && TREE_CODE (expr) == NOP_EXPR
5199       && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5200     expr = TREE_OPERAND (expr, 0);
5201
5202   if (TREE_CODE (type) == VOID_TYPE)
5203     return build1 (CONVERT_EXPR, type, expr);
5204
5205   if (TREE_CODE (type) == REFERENCE_TYPE)
5206     return (convert_from_reference
5207             (convert_to_reference (type, expr, CONV_STATIC|CONV_IMPLICIT,
5208                                    LOOKUP_COMPLAIN, NULL_TREE)));
5209
5210   if (IS_AGGR_TYPE (type))
5211     return build_cplus_new
5212       (type, (build_method_call
5213               (NULL_TREE, ctor_identifier, build_expr_list (NULL_TREE, expr),
5214                TYPE_BINFO (type), LOOKUP_NORMAL)));
5215
5216   expr = decay_conversion (expr);
5217   intype = TREE_TYPE (expr);
5218
5219   /* FIXME handle casting to array type.  */
5220
5221   ok = 0;
5222   if (can_convert_arg (type, intype, expr))
5223     ok = 1;
5224   else if (TYPE_PTROB_P (type) && TYPE_PTROB_P (intype))
5225     {
5226       tree binfo;
5227       if (IS_AGGR_TYPE (TREE_TYPE (type)) && IS_AGGR_TYPE (TREE_TYPE (intype))
5228           && at_least_as_qualified_p (TREE_TYPE (type),
5229                                       TREE_TYPE (intype))
5230           && (binfo = get_binfo (TREE_TYPE (intype), TREE_TYPE (type), 0))
5231           && ! TREE_VIA_VIRTUAL (binfo))
5232         ok = 1;
5233     }
5234   else if (TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
5235     {
5236       if (same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))),
5237                        TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (intype))))
5238           && at_least_as_qualified_p (TREE_TYPE (TREE_TYPE (type)),
5239                                       TREE_TYPE (TREE_TYPE (intype)))
5240           && (binfo = get_binfo (TYPE_OFFSET_BASETYPE (TREE_TYPE (type)),
5241                                  TYPE_OFFSET_BASETYPE (TREE_TYPE (intype)), 0))
5242           && ! TREE_VIA_VIRTUAL (binfo))
5243         ok = 1;
5244     }
5245   else if (TREE_CODE (intype) != BOOLEAN_TYPE
5246            && TREE_CODE (type) != ARRAY_TYPE
5247            && TREE_CODE (type) != FUNCTION_TYPE
5248            && can_convert (intype, type))
5249     ok = 1;
5250
5251   /* [expr.static.cast]
5252
5253      The static_cast operator shall not be used to cast away
5254      constnes.  */
5255   if (ok && casts_away_constness (intype, type))
5256     {
5257       cp_error ("static_cast from `%T' to `%T' casts away constness",
5258                 intype, type);
5259       return error_mark_node;
5260     }
5261
5262   if (ok)
5263     return build_c_cast (type, expr);
5264
5265   cp_error ("static_cast from `%T' to `%T'", intype, type);
5266   return error_mark_node;
5267 }
5268
5269 tree
5270 build_reinterpret_cast (type, expr)
5271    tree type, expr;
5272 {
5273   tree intype;
5274
5275   if (type == error_mark_node || expr == error_mark_node)
5276     return error_mark_node;
5277
5278   if (TREE_CODE (expr) == OFFSET_REF)
5279     expr = resolve_offset_ref (expr);
5280
5281   if (processing_template_decl)
5282     {
5283       tree t = build_min (REINTERPRET_CAST_EXPR, 
5284                           copy_to_permanent (type), expr);
5285       return t;
5286     }
5287
5288   if (TREE_CODE (type) != REFERENCE_TYPE)
5289     {
5290       expr = decay_conversion (expr);
5291
5292       /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5293          Strip such NOP_EXPRs if VALUE is being used in non-lvalue context.  */
5294       if (TREE_CODE (expr) == NOP_EXPR
5295           && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5296         expr = TREE_OPERAND (expr, 0);
5297     }
5298
5299   intype = TREE_TYPE (expr);
5300
5301   if (TREE_CODE (type) == REFERENCE_TYPE)
5302     {
5303       if (! real_lvalue_p (expr))
5304         {
5305           cp_error ("reinterpret_cast from `%T' rvalue to `%T'", intype, type);
5306           return error_mark_node;
5307         }
5308       expr = build_unary_op (ADDR_EXPR, expr, 0);
5309       if (expr != error_mark_node)
5310         expr = build_reinterpret_cast
5311           (build_pointer_type (TREE_TYPE (type)), expr);
5312       if (expr != error_mark_node)
5313         expr = build_indirect_ref (expr, 0);
5314       return expr;
5315     }
5316   else if (same_type_p (TYPE_MAIN_VARIANT (intype), 
5317                         TYPE_MAIN_VARIANT (type)))
5318     return build_static_cast (type, expr);
5319
5320   if (TYPE_PTR_P (type) && (TREE_CODE (intype) == INTEGER_TYPE
5321                             || TREE_CODE (intype) == ENUMERAL_TYPE))
5322     /* OK */;
5323   else if (TREE_CODE (type) == INTEGER_TYPE && TYPE_PTR_P (intype))
5324     {
5325       if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
5326         cp_pedwarn ("reinterpret_cast from `%T' to `%T' loses precision",
5327                     intype, type);
5328     }
5329   else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
5330            || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
5331     {
5332       if (TREE_READONLY_DECL_P (expr))
5333         expr = decl_constant_value (expr);
5334       return fold (build1 (NOP_EXPR, type, expr));
5335     }
5336   else if ((TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
5337            || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
5338     {
5339       if (! comp_ptr_ttypes_reinterpret (TREE_TYPE (type), TREE_TYPE (intype)))
5340         cp_pedwarn ("reinterpret_cast from `%T' to `%T' casts away const (or volatile)",
5341                     intype, type);
5342
5343       if (TREE_READONLY_DECL_P (expr))
5344         expr = decl_constant_value (expr);
5345       return fold (build1 (NOP_EXPR, type, expr));
5346     }
5347   else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
5348            || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
5349     {
5350       pedwarn ("ANSI C++ forbids casting between pointers to functions and objects");
5351       if (TREE_READONLY_DECL_P (expr))
5352         expr = decl_constant_value (expr);
5353       return fold (build1 (NOP_EXPR, type, expr));
5354     }
5355   else
5356     {
5357       cp_error ("reinterpret_cast from `%T' to `%T'", intype, type);
5358       return error_mark_node;
5359     }
5360       
5361   return cp_convert (type, expr);
5362 }
5363
5364 tree
5365 build_const_cast (type, expr)
5366    tree type, expr;
5367 {
5368   tree intype;
5369
5370   if (type == error_mark_node || expr == error_mark_node)
5371     return error_mark_node;
5372
5373   if (TREE_CODE (expr) == OFFSET_REF)
5374     expr = resolve_offset_ref (expr);
5375
5376   if (processing_template_decl)
5377     {
5378       tree t = build_min (CONST_CAST_EXPR, copy_to_permanent (type),
5379                           expr);
5380       return t;
5381     }
5382
5383   if (!POINTER_TYPE_P (type))
5384     {
5385       cp_error ("`%T' is not a pointer, reference, or pointer-to-data-member type",
5386                 type);
5387       cp_error ("as required by const_cast");
5388     }
5389   else if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
5390     {
5391       cp_error ("`%T' is a pointer or reference to a function type",
5392                 type);
5393       cp_error ("which is forbidden by const_cast");
5394       return error_mark_node;
5395     }
5396
5397   if (TREE_CODE (type) != REFERENCE_TYPE)
5398     {
5399       expr = decay_conversion (expr);
5400
5401       /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5402          Strip such NOP_EXPRs if VALUE is being used in non-lvalue context.  */
5403       if (TREE_CODE (expr) == NOP_EXPR
5404           && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
5405         expr = TREE_OPERAND (expr, 0);
5406     }
5407
5408   intype = TREE_TYPE (expr);
5409
5410   if (same_type_p (TYPE_MAIN_VARIANT (intype), TYPE_MAIN_VARIANT (type)))
5411     return build_static_cast (type, expr);
5412   else if (TREE_CODE (type) == REFERENCE_TYPE)
5413     {
5414       if (! real_lvalue_p (expr))
5415         {
5416           cp_error ("const_cast from `%T' rvalue to `%T'", intype, type);
5417           return error_mark_node;
5418         }
5419
5420       if (comp_ptr_ttypes_const (TREE_TYPE (type), intype))
5421         {
5422           expr = build_unary_op (ADDR_EXPR, expr, 0);
5423           expr = build1 (NOP_EXPR, type, expr);
5424           return convert_from_reference (expr);
5425         }
5426     }
5427   else if (TREE_CODE (type) == POINTER_TYPE
5428            && TREE_CODE (intype) == POINTER_TYPE
5429            && comp_ptr_ttypes_const (TREE_TYPE (type), TREE_TYPE (intype)))
5430     return cp_convert (type, expr);
5431
5432   cp_error ("const_cast from `%T' to `%T'", intype, type);
5433   return error_mark_node;
5434 }
5435
5436 /* Build an expression representing a cast to type TYPE of expression EXPR.
5437
5438    ALLOW_NONCONVERTING is true if we should allow non-converting constructors
5439    when doing the cast.  */
5440
5441 tree
5442 build_c_cast (type, expr)
5443      tree type, expr;
5444 {
5445   register tree value = expr;
5446   tree otype;
5447
5448   if (type == error_mark_node || expr == error_mark_node)
5449     return error_mark_node;
5450
5451   /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
5452      Strip such NOP_EXPRs if VALUE is being used in non-lvalue context.  */
5453   if (TREE_CODE (type) != REFERENCE_TYPE
5454       && TREE_CODE (value) == NOP_EXPR
5455       && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
5456     value = TREE_OPERAND (value, 0);
5457
5458   if (TREE_CODE (value) == OFFSET_REF)
5459     value = resolve_offset_ref (value);
5460
5461   if (TREE_CODE (type) == ARRAY_TYPE)
5462     {
5463       /* Allow casting from T1* to T2[] because Cfront allows it.
5464          NIHCL uses it. It is not valid ANSI C however, and hence, not
5465          valid ANSI C++.  */
5466       if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
5467         {
5468           if (pedantic)
5469             pedwarn ("ANSI C++ forbids casting to an array type");
5470           type = build_pointer_type (TREE_TYPE (type));
5471         }
5472       else
5473         {
5474           error ("ANSI C++ forbids casting to an array type");
5475           return error_mark_node;
5476         }
5477     }
5478
5479   if (TREE_CODE (type) == FUNCTION_TYPE
5480       || TREE_CODE (type) == METHOD_TYPE)
5481     {
5482       cp_error ("casting to function type `%T'", type);
5483       return error_mark_node;
5484     }
5485
5486   if (IS_SIGNATURE (type))
5487     {
5488       error ("cast specifies signature type");
5489       return error_mark_node;
5490     }
5491
5492   if (processing_template_decl)
5493     {
5494       tree t = build_min (CAST_EXPR, type,
5495                           min_tree_cons (NULL_TREE, value, NULL_TREE));
5496       return t;
5497     }
5498
5499   /* Convert functions and arrays to pointers and
5500      convert references to their expanded types,
5501      but don't convert any other types.  If, however, we are
5502      casting to a class type, there's no reason to do this: the
5503      cast will only succeed if there is a converting constructor,
5504      and the default conversions will be done at that point.  In
5505      fact, doing the default conversion here is actually harmful
5506      in cases like this:
5507
5508      typedef int A[2];
5509      struct S { S(const A&); };
5510
5511      since we don't want the array-to-pointer conversion done.  */
5512   if (!IS_AGGR_TYPE (type))
5513     {
5514       if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
5515           || (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE
5516               /* Don't do the default conversion on a ->* expression.  */
5517               && ! (TREE_CODE (type) == POINTER_TYPE
5518                     && bound_pmf_p (value)))
5519           || TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
5520           || TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5521         value = default_conversion (value);
5522     }
5523   else if (TREE_CODE (TREE_TYPE (value)) == REFERENCE_TYPE)
5524     /* However, even for class types, we still need to strip away
5525        the reference type, since the call to convert_force below
5526        does not expect the input expression to be of reference
5527        type.  */
5528     value = convert_from_reference (value);
5529         
5530   otype = TREE_TYPE (value);
5531
5532   /* Optionally warn about potentially worrisome casts.  */
5533
5534   if (warn_cast_qual
5535       && TREE_CODE (type) == POINTER_TYPE
5536       && TREE_CODE (otype) == POINTER_TYPE
5537       && !at_least_as_qualified_p (TREE_TYPE (type),
5538                                    TREE_TYPE (otype)))
5539     cp_warning ("cast discards qualifiers from pointer target type");
5540
5541   /* Warn about possible alignment problems.  */
5542   if (STRICT_ALIGNMENT && warn_cast_align
5543       && TREE_CODE (type) == POINTER_TYPE
5544       && TREE_CODE (otype) == POINTER_TYPE
5545       && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5546       && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5547       && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
5548     warning ("cast increases required alignment of target type");
5549
5550 #if 0
5551   /* We should see about re-enabling these, they seem useful to
5552      me.  */
5553   if (TREE_CODE (type) == INTEGER_TYPE
5554       && TREE_CODE (otype) == POINTER_TYPE
5555       && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
5556     warning ("cast from pointer to integer of different size");
5557
5558   if (TREE_CODE (type) == POINTER_TYPE
5559       && TREE_CODE (otype) == INTEGER_TYPE
5560       && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5561       /* Don't warn about converting 0 to pointer,
5562          provided the 0 was explicit--not cast or made by folding.  */
5563       && !(TREE_CODE (value) == INTEGER_CST && integer_zerop (value)))
5564     warning ("cast to pointer from integer of different size");
5565 #endif
5566
5567   if (TREE_CODE (type) == VOID_TYPE)
5568     {
5569       value = require_complete_type_in_void (value);
5570       if (value != error_mark_node)
5571         value = build1 (CONVERT_EXPR, void_type_node, value);
5572     }
5573   else if (TREE_CODE (type) == REFERENCE_TYPE)
5574     value = (convert_from_reference
5575              (convert_to_reference (type, value, CONV_C_CAST,
5576                                     LOOKUP_COMPLAIN, NULL_TREE)));
5577   else
5578     {
5579       tree ovalue;
5580
5581       if (TREE_READONLY_DECL_P (value))
5582         value = decl_constant_value (value);
5583
5584       ovalue = value;
5585       value = convert_force (type, value, CONV_C_CAST);
5586
5587       /* Ignore any integer overflow caused by the cast.  */
5588       if (TREE_CODE (value) == INTEGER_CST)
5589         {
5590           TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5591           TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
5592         }
5593     }
5594
5595     /* Always produce some operator for an explicit cast,
5596        so we can tell (for -pedantic) that the cast is no lvalue.  */
5597   if (TREE_CODE (type) != REFERENCE_TYPE && value == expr
5598       && real_lvalue_p (value))
5599     value = non_lvalue (value);
5600
5601   return value;
5602 }
5603 \f
5604 /* Build an assignment expression of lvalue LHS from value RHS.
5605    MODIFYCODE is the code for a binary operator that we use
5606    to combine the old value of LHS with RHS to get the new value.
5607    Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
5608
5609    C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed.  */
5610
5611 tree
5612 build_modify_expr (lhs, modifycode, rhs)
5613      tree lhs;
5614      enum tree_code modifycode;
5615      tree rhs;
5616 {
5617   register tree result;
5618   tree newrhs = rhs;
5619   tree lhstype = TREE_TYPE (lhs);
5620   tree olhstype = lhstype;
5621   tree olhs = lhs;
5622
5623   /* Avoid duplicate error messages from operands that had errors.  */
5624   if (lhs == error_mark_node || rhs == error_mark_node)
5625     return error_mark_node;
5626
5627   /* Types that aren't fully specified cannot be used in assignments.  */
5628   lhs = require_complete_type (lhs);
5629
5630   newrhs = rhs;
5631
5632   /* Handle assignment to signature pointers/refs.  */
5633
5634   if (TYPE_LANG_SPECIFIC (lhstype)
5635       && (IS_SIGNATURE_POINTER (lhstype) || IS_SIGNATURE_REFERENCE (lhstype)))
5636     {
5637       return build_signature_pointer_constructor (lhs, rhs);
5638     }
5639
5640   /* Handle control structure constructs used as "lvalues".  */
5641
5642   switch (TREE_CODE (lhs))
5643     {
5644       /* Handle --foo = 5; as these are valid constructs in C++ */
5645     case PREDECREMENT_EXPR:
5646     case PREINCREMENT_EXPR:
5647       if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
5648         lhs = build (TREE_CODE (lhs), TREE_TYPE (lhs),
5649                      stabilize_reference (TREE_OPERAND (lhs, 0)),
5650                      TREE_OPERAND (lhs, 1));
5651       return build (COMPOUND_EXPR, lhstype,
5652                     lhs,
5653                     build_modify_expr (TREE_OPERAND (lhs, 0),
5654                                        modifycode, rhs));
5655
5656       /* Handle (a, b) used as an "lvalue".  */
5657     case COMPOUND_EXPR:
5658       newrhs = build_modify_expr (TREE_OPERAND (lhs, 1),
5659                                   modifycode, rhs);
5660       if (newrhs == error_mark_node)
5661         return error_mark_node;
5662       return build (COMPOUND_EXPR, lhstype,
5663                     TREE_OPERAND (lhs, 0), newrhs);
5664
5665     case MODIFY_EXPR:
5666       newrhs = build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs);
5667       if (newrhs == error_mark_node)
5668         return error_mark_node;
5669       return build (COMPOUND_EXPR, lhstype, lhs, newrhs);
5670
5671       /* Handle (a ? b : c) used as an "lvalue".  */
5672     case COND_EXPR:
5673       rhs = save_expr (rhs);
5674       {
5675         /* Produce (a ? (b = rhs) : (c = rhs))
5676            except that the RHS goes through a save-expr
5677            so the code to compute it is only emitted once.  */
5678         tree cond
5679           = build_conditional_expr (TREE_OPERAND (lhs, 0),
5680                                     build_modify_expr (cp_convert (TREE_TYPE (lhs), TREE_OPERAND (lhs, 1)),
5681                                                        modifycode, rhs),
5682                                     build_modify_expr (cp_convert (TREE_TYPE (lhs), TREE_OPERAND (lhs, 2)),
5683                                                        modifycode, rhs));
5684         if (cond == error_mark_node)
5685           return cond;
5686         /* Make sure the code to compute the rhs comes out
5687            before the split.  */
5688         return build (COMPOUND_EXPR, TREE_TYPE (lhs),
5689                       /* Case to void to suppress warning
5690                          from warn_if_unused_value.  */
5691                       cp_convert (void_type_node, rhs), cond);
5692       }
5693
5694     default:
5695       break;
5696     }
5697
5698   if (TREE_CODE (lhs) == OFFSET_REF)
5699     {
5700       if (TREE_OPERAND (lhs, 0) == NULL_TREE)
5701         {
5702           /* Static class member?  */
5703           tree member = TREE_OPERAND (lhs, 1);
5704           if (TREE_CODE (member) == VAR_DECL)
5705             lhs = member;
5706           else
5707             {
5708               compiler_error ("invalid static class member");
5709               return error_mark_node;
5710             }
5711         }
5712       else
5713         lhs = resolve_offset_ref (lhs);
5714
5715       olhstype = lhstype = TREE_TYPE (lhs);
5716     }
5717
5718   if (lhs == error_mark_node)
5719     return lhs;
5720
5721   if (TREE_CODE (lhstype) == REFERENCE_TYPE
5722       && modifycode != INIT_EXPR)
5723     {
5724       lhs = convert_from_reference (lhs);
5725       olhstype = lhstype = TREE_TYPE (lhs);
5726     }
5727
5728   /* If a binary op has been requested, combine the old LHS value with the RHS
5729      producing the value we should actually store into the LHS.  */
5730
5731   if (modifycode == INIT_EXPR)
5732     {
5733       if (! IS_AGGR_TYPE (lhstype))
5734         /* Do the default thing */;
5735       else
5736         {
5737           result = build_method_call (lhs, ctor_identifier,
5738                                       build_expr_list (NULL_TREE, rhs),
5739                                       TYPE_BINFO (lhstype), LOOKUP_NORMAL);
5740           if (result == NULL_TREE)
5741             return error_mark_node;
5742           return result;
5743         }
5744     }
5745   else if (modifycode == NOP_EXPR)
5746     {
5747       /* `operator=' is not an inheritable operator.  */
5748       if (! IS_AGGR_TYPE (lhstype))
5749         /* Do the default thing */;
5750       else
5751         {
5752           result = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL,
5753                                    lhs, rhs, make_node (NOP_EXPR));
5754           if (result == NULL_TREE)
5755             return error_mark_node;
5756           return result;
5757         }
5758       lhstype = olhstype;
5759     }
5760   else if (PROMOTES_TO_AGGR_TYPE (lhstype, REFERENCE_TYPE))
5761     {
5762       my_friendly_abort (978652);
5763     }
5764   else
5765     {
5766       lhs = stabilize_reference (lhs);
5767       newrhs = build_binary_op (modifycode, lhs, rhs);
5768       if (newrhs == error_mark_node)
5769         {
5770           cp_error ("  in evaluation of `%Q(%#T, %#T)'", modifycode,
5771                     TREE_TYPE (lhs), TREE_TYPE (rhs));
5772           return error_mark_node;
5773         }
5774     }
5775
5776   /* Handle a cast used as an "lvalue".
5777      We have already performed any binary operator using the value as cast.
5778      Now convert the result to the cast type of the lhs,
5779      and then true type of the lhs and store it there;
5780      then convert result back to the cast type to be the value
5781      of the assignment.  */
5782
5783   switch (TREE_CODE (lhs))
5784     {
5785     case NOP_EXPR:
5786     case CONVERT_EXPR:
5787     case FLOAT_EXPR:
5788     case FIX_TRUNC_EXPR:
5789     case FIX_FLOOR_EXPR:
5790     case FIX_ROUND_EXPR:
5791     case FIX_CEIL_EXPR:
5792       if (TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5793           || TREE_CODE (TREE_TYPE (newrhs)) == FUNCTION_TYPE
5794           || TREE_CODE (TREE_TYPE (newrhs)) == METHOD_TYPE
5795           || TREE_CODE (TREE_TYPE (newrhs)) == OFFSET_TYPE)
5796         newrhs = default_conversion (newrhs);
5797       {
5798         tree inner_lhs = TREE_OPERAND (lhs, 0);
5799         tree result;
5800
5801         /* WP 5.4.1:  The result is an lvalue if T is a reference type,
5802            otherwise the result is an rvalue.   */
5803         if (! lvalue_p (lhs))
5804           pedwarn ("ANSI C++ forbids cast to non-reference type used as lvalue");
5805
5806         result = build_modify_expr (inner_lhs, NOP_EXPR,
5807                                     cp_convert (TREE_TYPE (inner_lhs),
5808                                                 cp_convert (lhstype, newrhs)));
5809         if (result == error_mark_node)
5810           return result;
5811         return cp_convert (TREE_TYPE (lhs), result);
5812       }
5813
5814     default:
5815       break;
5816     }
5817
5818   /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
5819      Reject anything strange now.  */
5820
5821   if (!lvalue_or_else (lhs, "assignment"))
5822     return error_mark_node;
5823
5824   GNU_xref_assign (lhs);
5825
5826   /* Warn about storing in something that is `const'.  */
5827   /* For C++, don't warn if this is initialization.  */
5828   if (modifycode != INIT_EXPR
5829       /* For assignment to `const' signature pointer/reference fields,
5830          don't warn either, we already printed a better message before.  */
5831       && ! (TREE_CODE (lhs) == COMPONENT_REF
5832             && (IS_SIGNATURE_POINTER (TREE_TYPE (TREE_OPERAND (lhs, 0)))
5833                 || IS_SIGNATURE_REFERENCE (TREE_TYPE (TREE_OPERAND (lhs, 0)))))
5834       && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
5835           /* Functions are not modifiable, even though they are
5836              lvalues.  */
5837           || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
5838           || (IS_AGGR_TYPE_CODE (TREE_CODE (lhstype))
5839               && C_TYPE_FIELDS_READONLY (lhstype))
5840           || (TREE_CODE (lhstype) == REFERENCE_TYPE
5841               && CP_TYPE_CONST_P (TREE_TYPE (lhstype)))))
5842     readonly_error (lhs, "assignment", 0);
5843
5844   /* If storing into a structure or union member,
5845      it has probably been given type `int'.
5846      Compute the type that would go with
5847      the actual amount of storage the member occupies.  */
5848
5849   if (TREE_CODE (lhs) == COMPONENT_REF
5850       && (TREE_CODE (lhstype) == INTEGER_TYPE
5851           || TREE_CODE (lhstype) == REAL_TYPE
5852           || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5853     {
5854       lhstype = TREE_TYPE (get_unwidened (lhs, 0));
5855
5856       /* If storing in a field that is in actuality a short or narrower
5857          than one, we must store in the field in its actual type.  */
5858
5859       if (lhstype != TREE_TYPE (lhs))
5860         {
5861           lhs = copy_node (lhs);
5862           TREE_TYPE (lhs) = lhstype;
5863         }
5864     }
5865
5866   /* check to see if there is an assignment to `this' */
5867   if (lhs == current_class_ptr)
5868     {
5869       if (flag_this_is_variable > 0
5870           && DECL_NAME (current_function_decl) != NULL_TREE
5871           && (DECL_NAME (current_function_decl)
5872               != constructor_name (current_class_type)))
5873         warning ("assignment to `this' not in constructor or destructor");
5874       current_function_just_assigned_this = 1;
5875     }
5876
5877   if (modifycode != INIT_EXPR)
5878     {
5879       /* Make modifycode now either a NOP_EXPR or an INIT_EXPR.  */
5880       modifycode = NOP_EXPR;
5881       /* Reference-bashing */
5882       if (TREE_CODE (lhstype) == REFERENCE_TYPE)
5883         {
5884           tree tmp = convert_from_reference (lhs);
5885           lhstype = TREE_TYPE (tmp);
5886           if (TYPE_SIZE (lhstype) == 0)
5887             {
5888               incomplete_type_error (lhs, lhstype);
5889               return error_mark_node;
5890             }
5891           lhs = tmp;
5892           olhstype = lhstype;
5893         }
5894       if (TREE_CODE (TREE_TYPE (newrhs)) == REFERENCE_TYPE)
5895         {
5896           tree tmp = convert_from_reference (newrhs);
5897           if (TYPE_SIZE (TREE_TYPE (tmp)) == 0)
5898             {
5899               incomplete_type_error (newrhs, TREE_TYPE (tmp));
5900               return error_mark_node;
5901             }
5902           newrhs = tmp;
5903         }
5904     }
5905
5906   if (TREE_SIDE_EFFECTS (lhs))
5907     lhs = stabilize_reference (lhs);
5908   if (TREE_SIDE_EFFECTS (newrhs))
5909     newrhs = stabilize_reference (newrhs);
5910
5911   /* Convert new value to destination type.  */
5912
5913   if (TREE_CODE (lhstype) == ARRAY_TYPE)
5914     {
5915       int from_array;
5916       
5917       if (!same_or_base_type_p (lhstype, TREE_TYPE (rhs)))
5918         {
5919           cp_error ("incompatible types in assignment of `%T' to `%T'",
5920                     TREE_TYPE (rhs), lhstype);
5921           return error_mark_node;
5922         }
5923
5924       /* Allow array assignment in compiler-generated code.  */
5925       if (pedantic && ! DECL_ARTIFICIAL (current_function_decl))
5926         pedwarn ("ANSI C++ forbids assignment of arrays");
5927
5928       /* Have to wrap this in RTL_EXPR for two cases:
5929          in base or member initialization and if we
5930          are a branch of a ?: operator.  Since we
5931          can't easily know the latter, just do it always.  */
5932
5933       result = make_node (RTL_EXPR);
5934
5935       TREE_TYPE (result) = void_type_node;
5936       do_pending_stack_adjust ();
5937       start_sequence_for_rtl_expr (result);
5938
5939       /* As a matter of principle, `start_sequence' should do this.  */
5940       emit_note (0, -1);
5941
5942       from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
5943                    ? 1 + (modifycode != INIT_EXPR): 0;
5944       expand_vec_init (lhs, lhs, array_type_nelts (lhstype), newrhs,
5945                        from_array);
5946
5947       do_pending_stack_adjust ();
5948
5949       TREE_SIDE_EFFECTS (result) = 1;
5950       RTL_EXPR_SEQUENCE (result) = get_insns ();
5951       RTL_EXPR_RTL (result) = const0_rtx;
5952       end_sequence ();
5953       return result;
5954     }
5955
5956   if (modifycode == INIT_EXPR)
5957     {
5958       newrhs = convert_for_initialization (lhs, lhstype, newrhs, LOOKUP_NORMAL,
5959                                            "assignment", NULL_TREE, 0);
5960       if (lhs == DECL_RESULT (current_function_decl))
5961         {
5962           if (DECL_INITIAL (lhs))
5963             warning ("return value from function receives multiple initializations");
5964           DECL_INITIAL (lhs) = newrhs;
5965         }
5966     }
5967   else
5968     {
5969       /* Avoid warnings on enum bit fields.  */
5970       if (TREE_CODE (olhstype) == ENUMERAL_TYPE
5971           && TREE_CODE (lhstype) == INTEGER_TYPE)
5972         {
5973           newrhs = convert_for_assignment (olhstype, newrhs, "assignment",
5974                                            NULL_TREE, 0);
5975           newrhs = convert_force (lhstype, newrhs, 0);
5976         }
5977       else
5978         newrhs = convert_for_assignment (lhstype, newrhs, "assignment",
5979                                          NULL_TREE, 0);
5980       if (TREE_CODE (newrhs) == CALL_EXPR
5981           && TYPE_NEEDS_CONSTRUCTING (lhstype))
5982         newrhs = build_cplus_new (lhstype, newrhs);
5983
5984       /* Can't initialize directly from a TARGET_EXPR, since that would
5985          cause the lhs to be constructed twice, and possibly result in
5986          accidental self-initialization.  So we force the TARGET_EXPR to be
5987          expanded without a target.  */
5988       if (TREE_CODE (newrhs) == TARGET_EXPR)
5989         newrhs = build (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
5990                         TREE_OPERAND (newrhs, 0));
5991     }
5992
5993   if (newrhs == error_mark_node)
5994     return error_mark_node;
5995
5996   if (TREE_CODE (newrhs) == COND_EXPR)
5997     {
5998       tree lhs1;
5999       tree cond = TREE_OPERAND (newrhs, 0);
6000
6001       if (TREE_SIDE_EFFECTS (lhs))
6002         cond = build_compound_expr (tree_cons
6003                                     (NULL_TREE, lhs,
6004                                      build_expr_list (NULL_TREE, cond)));
6005
6006       /* Cannot have two identical lhs on this one tree (result) as preexpand
6007          calls will rip them out and fill in RTL for them, but when the
6008          rtl is generated, the calls will only be in the first side of the
6009          condition, not on both, or before the conditional jump! (mrs) */
6010       lhs1 = break_out_calls (lhs);
6011
6012       if (lhs == lhs1)
6013         /* If there's no change, the COND_EXPR behaves like any other rhs.  */
6014         result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
6015                         lhstype, lhs, newrhs);
6016       else
6017         {
6018           tree result_type = TREE_TYPE (newrhs);
6019           /* We have to convert each arm to the proper type because the
6020              types may have been munged by constant folding.  */
6021           result
6022             = build (COND_EXPR, result_type, cond,
6023                      build_modify_expr (lhs, modifycode,
6024                                         cp_convert (result_type,
6025                                                     TREE_OPERAND (newrhs, 1))),
6026                      build_modify_expr (lhs1, modifycode,
6027                                         cp_convert (result_type,
6028                                                     TREE_OPERAND (newrhs, 2))));
6029         }
6030     }
6031   else
6032     result = build (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
6033                     lhstype, lhs, newrhs);
6034
6035   TREE_SIDE_EFFECTS (result) = 1;
6036
6037   /* If we got the LHS in a different type for storing in,
6038      convert the result back to the nominal type of LHS
6039      so that the value we return always has the same type
6040      as the LHS argument.  */
6041
6042   if (olhstype == TREE_TYPE (result))
6043     return result;
6044   /* Avoid warnings converting integral types back into enums
6045      for enum bit fields.  */
6046   if (TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
6047       && TREE_CODE (olhstype) == ENUMERAL_TYPE)
6048     {
6049       result = build (COMPOUND_EXPR, olhstype, result, olhs);
6050       TREE_NO_UNUSED_WARNING (result) = 1;
6051       return result;
6052     }
6053   return convert_for_assignment (olhstype, result, "assignment",
6054                                  NULL_TREE, 0);
6055 }
6056
6057 tree
6058 build_x_modify_expr (lhs, modifycode, rhs)
6059      tree lhs;
6060      enum tree_code modifycode;
6061      tree rhs;
6062 {
6063   if (processing_template_decl)
6064     return build_min_nt (MODOP_EXPR, lhs,
6065                          build_min_nt (modifycode, NULL_TREE, NULL_TREE), rhs);
6066
6067   if (modifycode != NOP_EXPR)
6068     {
6069       tree rval = build_opfncall (MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
6070                                   make_node (modifycode));
6071       if (rval)
6072         return rval;
6073     }
6074   return build_modify_expr (lhs, modifycode, rhs);
6075 }
6076
6077 \f
6078 /* Get difference in deltas for different pointer to member function
6079    types.  Return integer_zero_node, if FROM cannot be converted to a
6080    TO type.  If FORCE is true, then allow reverse conversions as well.
6081
6082    Note that the naming of FROM and TO is kind of backwards; the return
6083    value is what we add to a TO in order to get a FROM.  They are named
6084    this way because we call this function to find out how to convert from
6085    a pointer to member of FROM to a pointer to member of TO.  */
6086
6087 static tree
6088 get_delta_difference (from, to, force)
6089      tree from, to;
6090      int force;
6091 {
6092   tree delta = integer_zero_node;
6093   tree binfo;
6094   
6095   if (to == from)
6096     return delta;
6097
6098   /* Should get_base_distance here, so we can check if any thing along the
6099      path is virtual, and we need to make sure we stay
6100      inside the real binfos when going through virtual bases.
6101      Maybe we should replace virtual bases with
6102      binfo_member (...CLASSTYPE_VBASECLASSES...)...  (mrs) */
6103   binfo = get_binfo (from, to, 1);
6104   if (binfo == error_mark_node)
6105     {
6106       error ("   in pointer to member function conversion");
6107       return delta;
6108     }
6109   if (binfo == 0)
6110     {
6111       if (!force)
6112         {
6113           error_not_base_type (from, to);
6114           error ("   in pointer to member conversion");
6115           return delta;
6116         }
6117       binfo = get_binfo (to, from, 1);
6118       if (binfo == 0 || binfo == error_mark_node)
6119         return delta;
6120       if (TREE_VIA_VIRTUAL (binfo))
6121         {
6122           binfo = binfo_member (BINFO_TYPE (binfo),
6123                                 CLASSTYPE_VBASECLASSES (from));
6124           cp_warning ("pointer to member cast to virtual base `%T'",
6125                       BINFO_TYPE (binfo));
6126           warning ("  will only work if you are very careful");
6127         }
6128       delta = BINFO_OFFSET (binfo);
6129       delta = cp_convert (ptrdiff_type_node, delta);
6130       
6131       return build_binary_op (MINUS_EXPR,
6132                               integer_zero_node,
6133                               delta);
6134     }
6135
6136   if (TREE_VIA_VIRTUAL (binfo))
6137     {
6138       if (force)
6139         {
6140           cp_warning ("pointer to member cast from virtual base `%T'",
6141                       BINFO_TYPE (binfo));
6142           warning ("  will only work if you are very careful");
6143         }
6144       else
6145         cp_error ("pointer to member conversion from virtual base `%T'",
6146                   BINFO_TYPE (binfo));
6147     }
6148
6149   return BINFO_OFFSET (binfo);
6150 }
6151
6152 tree
6153 build_ptrmemfunc1 (type, delta, idx, pfn, delta2)
6154      tree type, delta, idx, pfn, delta2;
6155 {
6156   tree u;
6157
6158 #if 0
6159   /* This is the old way we did it.  We want to avoid calling
6160      digest_init, so that it can give an error if we use { } when
6161      initializing a pointer to member function.  */
6162
6163   if (pfn)
6164     {
6165       u = build_nt (CONSTRUCTOR, NULL_TREE,
6166                     expr_tree_cons (pfn_identifier, pfn, NULL_TREE));
6167     }
6168   else
6169     {
6170       u = build_nt (CONSTRUCTOR, NULL_TREE,
6171                     expr_tree_cons (delta2_identifier, delta2, NULL_TREE));
6172     }
6173
6174   u = build_nt (CONSTRUCTOR, NULL_TREE,
6175                 expr_tree_cons (NULL_TREE, delta,
6176                            expr_tree_cons (NULL_TREE, idx,
6177                                       expr_tree_cons (NULL_TREE, u, NULL_TREE))));
6178
6179   return digest_init (type, u, (tree*)0);
6180 #else
6181   tree delta_field, idx_field, pfn_or_delta2_field, pfn_field, delta2_field;
6182   tree subtype;
6183   int allconstant, allsimple;
6184
6185   delta_field = TYPE_FIELDS (type);
6186   idx_field = TREE_CHAIN (delta_field);
6187   pfn_or_delta2_field = TREE_CHAIN (idx_field);
6188   subtype = TREE_TYPE (pfn_or_delta2_field);
6189   pfn_field = TYPE_FIELDS (subtype);
6190   delta2_field = TREE_CHAIN (pfn_field);
6191
6192   if (pfn)
6193     {
6194       allconstant = TREE_CONSTANT (pfn);
6195       allsimple = !! initializer_constant_valid_p (pfn, TREE_TYPE (pfn));
6196       u = expr_tree_cons (pfn_field, pfn, NULL_TREE);
6197     }
6198   else
6199     {
6200       delta2 = convert_and_check (delta_type_node, delta2);
6201       allconstant = TREE_CONSTANT (delta2);
6202       allsimple = !! initializer_constant_valid_p (delta2, TREE_TYPE (delta2));
6203       u = expr_tree_cons (delta2_field, delta2, NULL_TREE);
6204     }
6205
6206   delta = convert_and_check (delta_type_node, delta);
6207   idx = convert_and_check (delta_type_node, idx);
6208
6209   allconstant = allconstant && TREE_CONSTANT (delta) && TREE_CONSTANT (idx);
6210   allsimple = allsimple
6211     && initializer_constant_valid_p (delta, TREE_TYPE (delta))
6212       && initializer_constant_valid_p (idx, TREE_TYPE (idx));
6213
6214   u = build (CONSTRUCTOR, subtype, NULL_TREE, u);
6215   u = expr_tree_cons (delta_field, delta,
6216                  expr_tree_cons (idx_field, idx,
6217                             expr_tree_cons (pfn_or_delta2_field, u, NULL_TREE)));
6218   u = build (CONSTRUCTOR, type, NULL_TREE, u);
6219   TREE_CONSTANT (u) = allconstant;
6220   TREE_STATIC (u) = allconstant && allsimple;
6221   return u;
6222 #endif
6223 }
6224
6225 /* Build a constructor for a pointer to member function.  It can be
6226    used to initialize global variables, local variable, or used
6227    as a value in expressions.  TYPE is the POINTER to METHOD_TYPE we
6228    want to be.
6229
6230    If FORCE is non-zero, then force this conversion, even if
6231    we would rather not do it.  Usually set when using an explicit
6232    cast.
6233
6234    Return error_mark_node, if something goes wrong.  */
6235
6236 tree
6237 build_ptrmemfunc (type, pfn, force)
6238      tree type, pfn;
6239      int force;
6240 {
6241   tree fn;
6242   tree pfn_type = TREE_TYPE (pfn);
6243   tree to_type = build_ptrmemfunc_type (type);
6244
6245   /* Handle multiple conversions of pointer to member functions.  */
6246   if (TYPE_PTRMEMFUNC_P (TREE_TYPE (pfn)))
6247     {
6248       tree idx = integer_zero_node;
6249       tree delta = integer_zero_node;
6250       tree delta2 = integer_zero_node;
6251       tree npfn = NULL_TREE;
6252       tree ndelta, ndelta2;
6253       tree e1, e2, e3, n;
6254
6255       if (!force 
6256           && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn))
6257         cp_error ("conversion to `%T' from `%T'", 
6258                   to_type, pfn_type);
6259
6260       if (TREE_CODE (pfn) == PTRMEM_CST)
6261         {
6262           /* We could just build the resulting CONSTRUCTOR now, but we
6263              don't, relying on the general machinery below, together
6264              with constant-folding, to do the right thing.  We don't
6265              want to return a PTRMEM_CST here, since a
6266              pointer-to-member constant is no longer a valid template
6267              argument once it is cast to any type, including its
6268              original type.  */
6269           expand_ptrmemfunc_cst (pfn, &ndelta, &idx, &npfn, &ndelta2);
6270           if (npfn)
6271             /* This constant points to a non-virtual function.
6272                NDELTA2 will be NULL, but it's value doesn't really
6273                matter since we won't use it anyhow.  */
6274             ndelta2 = integer_zero_node;
6275         }
6276       else if (same_type_p (to_type, pfn_type))
6277         /* We don't have to do any conversion.  Note that we do this
6278            after checking for a PTRMEM_CST so that a PTRMEM_CST, cast
6279            to its own type, will not be considered a legal non-type
6280            template argument.  */
6281         return pfn;
6282       else
6283         {
6284           ndelta = cp_convert (ptrdiff_type_node, 
6285                                build_component_ref (pfn, 
6286                                                     delta_identifier, 
6287                                                     NULL_TREE, 0));
6288           ndelta2 = cp_convert (ptrdiff_type_node, 
6289                                 DELTA2_FROM_PTRMEMFUNC (pfn));
6290           idx = build_component_ref (pfn, index_identifier, NULL_TREE, 0);
6291         }
6292
6293       n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
6294                                 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
6295                                 force);
6296       delta = build_binary_op (PLUS_EXPR, ndelta, n);
6297       delta2 = build_binary_op (PLUS_EXPR, ndelta2, n);
6298       e1 = fold (build (GT_EXPR, boolean_type_node, idx, integer_zero_node));
6299           
6300       /* If it's a virtual function, this is what we want.  */
6301       e2 = build_ptrmemfunc1 (to_type, delta, idx,
6302                               NULL_TREE, delta2);
6303
6304       pfn = PFN_FROM_PTRMEMFUNC (pfn);
6305       npfn = build1 (NOP_EXPR, type, pfn);
6306       TREE_CONSTANT (npfn) = TREE_CONSTANT (pfn);
6307
6308       /* But if it's a non-virtual function, or NULL, we use this
6309          instead.  */
6310       e3 = build_ptrmemfunc1 (to_type, delta,
6311                               idx, npfn, NULL_TREE);
6312       return build_conditional_expr (e1, e2, e3);
6313     }
6314
6315   /* Handle null pointer to member function conversions.  */
6316   if (integer_zerop (pfn))
6317     {
6318       pfn = build_c_cast (type, integer_zero_node);
6319       return build_ptrmemfunc1 (to_type,
6320                                 integer_zero_node, integer_zero_node,
6321                                 pfn, NULL_TREE);
6322     }
6323
6324   if (type_unknown_p (pfn))
6325     return instantiate_type (type, pfn, 1);
6326
6327   fn = TREE_OPERAND (pfn, 0);
6328   my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
6329   return make_ptrmem_cst (to_type, fn);
6330 }
6331
6332 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
6333    given by CST.  */
6334
6335 void
6336 expand_ptrmemfunc_cst (cst, delta, idx, pfn, delta2)
6337      tree cst;
6338      tree *delta;
6339      tree *idx;
6340      tree *pfn;
6341      tree *delta2;
6342 {
6343   tree type = TREE_TYPE (cst);
6344   tree fn = PTRMEM_CST_MEMBER (cst);
6345   tree ptr_class, fn_class;
6346
6347   my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 0);
6348
6349   /* The class that the function belongs to.  */
6350   fn_class = DECL_CLASS_CONTEXT (fn);
6351
6352   /* The class that we're creating a pointer to member of.  */
6353   ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
6354
6355   /* First, calculate the adjustment to the function's class.  */
6356   *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0);
6357
6358   if (!DECL_VIRTUAL_P (fn))
6359     {
6360       *idx = size_binop (MINUS_EXPR, integer_zero_node, integer_one_node);
6361       *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), build_addr_func (fn));
6362       *delta2 = NULL_TREE;
6363     }
6364   else
6365     {
6366       /* If we're dealing with a virtual function, we have to adjust 'this'
6367          again, to point to the base which provides the vtable entry for
6368          fn; the call will do the opposite adjustment.  */
6369       tree orig_class = DECL_VIRTUAL_CONTEXT (fn);
6370       tree binfo = binfo_or_else (orig_class, fn_class);
6371       *delta = size_binop (PLUS_EXPR, *delta, BINFO_OFFSET (binfo));
6372
6373       /* Map everything down one to make room for the null PMF.  */
6374       *idx = size_binop (PLUS_EXPR, DECL_VINDEX (fn), integer_one_node);
6375       *pfn = NULL_TREE;
6376
6377       /* Offset from an object of PTR_CLASS to the vptr for ORIG_CLASS.  */
6378       *delta2 = size_binop (PLUS_EXPR, *delta,
6379                             get_vfield_offset (TYPE_BINFO (orig_class)));
6380     }
6381 }
6382
6383 /* Return an expression for DELTA2 from the pointer-to-member function
6384    given by T.  */
6385
6386 tree
6387 delta2_from_ptrmemfunc (t)
6388      tree t;
6389 {
6390   if (TREE_CODE (t) == PTRMEM_CST)
6391     {
6392       tree delta;
6393       tree idx;
6394       tree pfn;
6395       tree delta2;
6396       
6397       expand_ptrmemfunc_cst (t, &delta, &idx, &pfn, &delta2);
6398       if (delta2)
6399         return delta2;
6400     }
6401
6402   return (build_component_ref 
6403           (build_component_ref (t,
6404                                 pfn_or_delta2_identifier, NULL_TREE,
6405                                 0), 
6406            delta2_identifier, NULL_TREE, 0)); 
6407 }
6408
6409 /* Return an expression for PFN from the pointer-to-member function
6410    given by T.  */
6411
6412 tree
6413 pfn_from_ptrmemfunc (t)
6414      tree t;
6415 {
6416   if (TREE_CODE (t) == PTRMEM_CST)
6417     {
6418       tree delta;
6419       tree idx;
6420       tree pfn;
6421       tree delta2;
6422       
6423       expand_ptrmemfunc_cst (t, &delta, &idx, &pfn, &delta2);
6424       if (pfn)
6425         return pfn;
6426     }
6427
6428   return (build_component_ref 
6429           (build_component_ref (t,
6430                                 pfn_or_delta2_identifier, NULL_TREE,
6431                                 0), 
6432            pfn_identifier, NULL_TREE, 0)); 
6433 }
6434
6435 /* Convert value RHS to type TYPE as preparation for an assignment to
6436    an lvalue of type TYPE.  ERRTYPE is a string to use in error
6437    messages: "assignment", "return", etc.  If FNDECL is non-NULL, we
6438    are doing the conversion in order to pass the PARMNUMth argument of
6439    FNDECL.  */
6440
6441 static tree
6442 convert_for_assignment (type, rhs, errtype, fndecl, parmnum)
6443      tree type, rhs;
6444      const char *errtype;
6445      tree fndecl;
6446      int parmnum;
6447 {
6448   register enum tree_code codel = TREE_CODE (type);
6449   register tree rhstype;
6450   register enum tree_code coder;
6451
6452   if (codel == OFFSET_TYPE)
6453     my_friendly_abort (990505);
6454
6455   if (TREE_CODE (rhs) == OFFSET_REF)
6456     rhs = resolve_offset_ref (rhs);
6457
6458   /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue.  */
6459   if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
6460     rhs = TREE_OPERAND (rhs, 0);
6461
6462   rhstype = TREE_TYPE (rhs);
6463   coder = TREE_CODE (rhstype);
6464
6465   if (rhs == error_mark_node || rhstype == error_mark_node)
6466     return error_mark_node;
6467   if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
6468     return error_mark_node;
6469
6470   /* Issue warnings about peculiar, but legal, uses of NULL.  */
6471   if (ARITHMETIC_TYPE_P (type) && rhs == null_node)
6472     cp_warning ("converting NULL to non-pointer type");
6473
6474   /* The RHS of an assignment cannot have void type.  */
6475   if (coder == VOID_TYPE)
6476     {
6477       error ("void value not ignored as it ought to be");
6478       return error_mark_node;
6479     }
6480
6481   /* Simplify the RHS if possible.  */
6482   if (TREE_CODE (rhs) == CONST_DECL)
6483     rhs = DECL_INITIAL (rhs);
6484   else if (TREE_READONLY_DECL_P (rhs))
6485     rhs = decl_constant_value (rhs);
6486
6487   /* Warn about assigning a floating-point type to an integer type.  */
6488   if (coder == REAL_TYPE && codel == INTEGER_TYPE)
6489     {
6490       if (fndecl)
6491         cp_warning ("`%T' used for argument %P of `%D'",
6492                     rhstype, parmnum, fndecl);
6493       else
6494         cp_warning ("%s to `%T' from `%T'", errtype, type, rhstype);
6495     }
6496   /* And warn about assigning a negative value to an unsigned
6497      variable.  */
6498   else if (TREE_UNSIGNED (type) && codel != BOOLEAN_TYPE)
6499     {
6500       if (TREE_CODE (rhs) == INTEGER_CST
6501           && TREE_NEGATED_INT (rhs))
6502         {
6503           if (fndecl)
6504             cp_warning ("negative value `%E' passed as argument %P of `%D'",
6505                         rhs, parmnum, fndecl);
6506           else
6507             cp_warning ("%s of negative value `%E' to `%T'",
6508                         errtype, rhs, type);
6509         }
6510       overflow_warning (rhs);
6511       if (TREE_CONSTANT (rhs))
6512         rhs = fold (rhs);
6513     }
6514
6515   /* [expr.ass]
6516
6517      The expression is implicitly converted (clause _conv_) to the
6518      cv-unqualified type of the left operand.  */
6519   if (!can_convert_arg (type, rhstype, rhs))
6520     {
6521       /* When -Wno-pmf-converions is use, we just silently allow
6522          conversions from pointers-to-members to plain pointers.  If
6523          the conversion doesn't work, cp_convert will complain.  */
6524       if (!warn_pmf2ptr 
6525           && TYPE_PTR_P (type) 
6526           && TYPE_PTRMEMFUNC_P (rhstype))
6527         rhs = cp_convert (strip_top_quals (type), rhs);
6528       /* If the right-hand side has unknown type, then it is an
6529          overloaded function.  Call instantiate_type to get error
6530          messages.  */
6531       else if (rhstype == unknown_type_node)
6532         instantiate_type (type, rhs, 1);
6533       else if (fndecl)
6534         cp_error ("cannot convert `%T' to `%T' for argument `%P' to `%D'",
6535                   rhstype, type, parmnum, fndecl);
6536       else
6537         cp_error ("cannot convert `%T' to `%T' in %s", rhstype, type, 
6538                   errtype);
6539       return error_mark_node;
6540     }
6541   return perform_implicit_conversion (strip_top_quals (type), rhs);
6542 }
6543
6544 /* Convert RHS to be of type TYPE.
6545    If EXP is non-zero, it is the target of the initialization.
6546    ERRTYPE is a string to use in error messages.
6547
6548    Two major differences between the behavior of
6549    `convert_for_assignment' and `convert_for_initialization'
6550    are that references are bashed in the former, while
6551    copied in the latter, and aggregates are assigned in
6552    the former (operator=) while initialized in the
6553    latter (X(X&)).
6554
6555    If using constructor make sure no conversion operator exists, if one does
6556    exist, an ambiguity exists.
6557
6558    If flags doesn't include LOOKUP_COMPLAIN, don't complain about anything.  */
6559
6560 tree
6561 convert_for_initialization (exp, type, rhs, flags, errtype, fndecl, parmnum)
6562      tree exp, type, rhs;
6563      int flags;
6564      const char *errtype;
6565      tree fndecl;
6566      int parmnum;
6567 {
6568   register enum tree_code codel = TREE_CODE (type);
6569   register tree rhstype;
6570   register enum tree_code coder;
6571
6572   /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6573      Strip such NOP_EXPRs, since RHS is used in non-lvalue context.  */
6574   if (TREE_CODE (rhs) == NOP_EXPR
6575       && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
6576       && codel != REFERENCE_TYPE)
6577     rhs = TREE_OPERAND (rhs, 0);
6578
6579   if (rhs == error_mark_node
6580       || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
6581     return error_mark_node;
6582
6583   if (TREE_CODE (rhs) == OFFSET_REF)
6584     {
6585       rhs = resolve_offset_ref (rhs);
6586       if (rhs == error_mark_node)
6587         return error_mark_node;
6588     }
6589
6590   if (TREE_CODE (TREE_TYPE (rhs)) == REFERENCE_TYPE)
6591     rhs = convert_from_reference (rhs);
6592
6593   if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
6594        && TREE_CODE (type) != ARRAY_TYPE
6595        && (TREE_CODE (type) != REFERENCE_TYPE
6596            || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
6597       || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
6598           && (TREE_CODE (type) != REFERENCE_TYPE
6599               || TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE))
6600       || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
6601     rhs = default_conversion (rhs);
6602
6603   rhstype = TREE_TYPE (rhs);
6604   coder = TREE_CODE (rhstype);
6605
6606   if (coder == ERROR_MARK)
6607     return error_mark_node;
6608
6609   /* We accept references to incomplete types, so we can
6610      return here before checking if RHS is of complete type.  */
6611      
6612   if (codel == REFERENCE_TYPE)
6613     {
6614       /* This should eventually happen in convert_arguments.  */
6615       extern int warningcount, errorcount;
6616       int savew = 0, savee = 0;
6617
6618       if (fndecl)
6619         savew = warningcount, savee = errorcount;
6620       rhs = initialize_reference (type, rhs);
6621       if (fndecl)
6622         {
6623           if (warningcount > savew)
6624             cp_warning_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6625           else if (errorcount > savee)
6626             cp_error_at ("in passing argument %P of `%+D'", parmnum, fndecl);
6627         }
6628       return rhs;
6629     }      
6630
6631   if (exp != 0)
6632     exp = require_complete_type (exp);
6633   if (exp == error_mark_node)
6634     return error_mark_node;
6635
6636   if (TREE_CODE (rhstype) == REFERENCE_TYPE)
6637     rhstype = TREE_TYPE (rhstype);
6638
6639   type = complete_type (type);
6640
6641   if (TYPE_LANG_SPECIFIC (type)
6642       && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))
6643     return build_signature_pointer_constructor (type, rhs);
6644
6645   if (IS_AGGR_TYPE (type))
6646     return ocp_convert (type, rhs, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
6647
6648   if (type == TREE_TYPE (rhs))
6649     {
6650       /* Issue warnings about peculiar, but legal, uses of NULL.  We
6651          do this *before* the call to decl_constant_value so as to
6652          avoid duplicate warnings on code like `const int I = NULL;
6653          f(I);'.  */
6654       if (ARITHMETIC_TYPE_P (type) && rhs == null_node)
6655         cp_warning ("converting NULL to non-pointer type");
6656
6657       if (TREE_READONLY_DECL_P (rhs))
6658         rhs = decl_constant_value (rhs);
6659
6660       return rhs;
6661     }
6662
6663   return convert_for_assignment (type, rhs, errtype, fndecl, parmnum);
6664 }
6665 \f
6666 /* Expand an ASM statement with operands, handling output operands
6667    that are not variables or INDIRECT_REFS by transforming such
6668    cases into cases that expand_asm_operands can handle.
6669
6670    Arguments are same as for expand_asm_operands.
6671
6672    We don't do default conversions on all inputs, because it can screw
6673    up operands that are expected to be in memory.  */
6674
6675 void
6676 c_expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
6677      tree string, outputs, inputs, clobbers;
6678      int vol;
6679      char *filename;
6680      int line;
6681 {
6682   int noutputs = list_length (outputs);
6683   register int i;
6684   /* o[I] is the place that output number I should be written.  */
6685   register tree *o = (tree *) alloca (noutputs * sizeof (tree));
6686   register tree tail;
6687
6688   /* Record the contents of OUTPUTS before it is modified.  */
6689   for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6690     o[i] = TREE_VALUE (tail);
6691
6692   /* Generate the ASM_OPERANDS insn;
6693      store into the TREE_VALUEs of OUTPUTS some trees for
6694      where the values were actually stored.  */
6695   expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6696
6697   /* Copy all the intermediate outputs into the specified outputs.  */
6698   for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6699     {
6700       if (o[i] != TREE_VALUE (tail))
6701         {
6702           expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
6703                        const0_rtx, VOIDmode, EXPAND_NORMAL);
6704           free_temp_slots ();
6705         }
6706       /* Detect modification of read-only values.
6707          (Otherwise done by build_modify_expr.)  */
6708       else
6709         {
6710           tree type = TREE_TYPE (o[i]);
6711           if (CP_TYPE_CONST_P (type)
6712               || (IS_AGGR_TYPE_CODE (TREE_CODE (type))
6713                   && C_TYPE_FIELDS_READONLY (type)))
6714             readonly_error (o[i], "modification by `asm'", 1);
6715         }
6716     }
6717
6718   /* Those MODIFY_EXPRs could do autoincrements.  */
6719   emit_queue ();
6720 }
6721 \f
6722 /* Expand a C `return' statement.
6723    RETVAL is the expression for what to return,
6724    or a null pointer for `return;' with no value.
6725
6726    C++: upon seeing a `return', we must call destructors on all
6727    variables in scope which had constructors called on them.
6728    This means that if in a destructor, the base class destructors
6729    must be called before returning.
6730
6731    The RETURN statement in C++ has initialization semantics.  */
6732
6733 void
6734 c_expand_return (retval)
6735      tree retval;
6736 {
6737   extern tree dtor_label, ctor_label;
6738   tree result = DECL_RESULT (current_function_decl);
6739   tree valtype = TREE_TYPE (result);
6740
6741   if (TREE_THIS_VOLATILE (current_function_decl))
6742     warning ("function declared `noreturn' has a `return' statement");
6743
6744   if (retval == error_mark_node)
6745     {
6746       current_function_returns_null = 1;
6747       return;
6748     }
6749
6750   if (processing_template_decl)
6751     {
6752       add_tree (build_min_nt (RETURN_STMT, retval));
6753       return;
6754     }
6755
6756   if (dtor_label)
6757     {
6758       if (retval)
6759         error ("returning a value from a destructor");
6760
6761       /* Can't just return from a destructor.  */
6762       expand_goto (dtor_label);
6763       return;
6764     }
6765
6766   /* Only operator new(...) throw(), can return NULL [expr.new/13].  */
6767   if ((DECL_NAME (current_function_decl) == ansi_opname[(int) NEW_EXPR]
6768        || DECL_NAME (current_function_decl) == ansi_opname[(int) VEC_NEW_EXPR])
6769       && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
6770       && null_ptr_cst_p (retval))
6771     cp_warning ("operator new should throw an exception, not return NULL");
6772   
6773   if (retval == NULL_TREE)
6774     {
6775       /* A non-named return value does not count.  */
6776
6777       if (DECL_CONSTRUCTOR_P (current_function_decl))
6778         retval = current_class_ptr;
6779       else if (DECL_NAME (result) != NULL_TREE
6780                && TREE_CODE (valtype) != VOID_TYPE)
6781         retval = result;
6782       else
6783         {
6784           current_function_returns_null = 1;
6785
6786           if (valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
6787             {
6788               if (DECL_NAME (DECL_RESULT (current_function_decl)) == NULL_TREE)
6789                 {
6790                   pedwarn ("`return' with no value, in function returning non-void");
6791                   /* Clear this, so finish_function won't say that we
6792                      reach the end of a non-void function (which we don't,
6793                      we gave a return!).  */
6794                   current_function_returns_null = 0;
6795                 }
6796             }
6797
6798           expand_null_return ();
6799           return;
6800         }
6801     }
6802   else if (DECL_CONSTRUCTOR_P (current_function_decl))
6803     {
6804       if (flag_this_is_variable)
6805         error ("return from a constructor: use `this = ...' instead");
6806       else
6807         error ("returning a value from a constructor");
6808       retval = current_class_ptr;
6809     }
6810
6811   /* Effective C++ rule 15.  See also start_function.  */
6812   if (warn_ecpp
6813       && DECL_NAME (current_function_decl) == ansi_opname[(int) MODIFY_EXPR]
6814       && retval != current_class_ref)
6815     cp_warning ("`operator=' should return a reference to `*this'");
6816
6817   if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
6818     {
6819       current_function_returns_null = 1;
6820       if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
6821         pedwarn ("`return' with a value, in function returning void");
6822       expand_return (retval);
6823       return;
6824     }
6825   
6826   /* Now deal with possible C++ hair:
6827      (1) Compute the return value.
6828      (2) If there are aggregate values with destructors which
6829      must be cleaned up, clean them (taking care
6830      not to clobber the return value).
6831      (3) If an X(X&) constructor is defined, the return
6832      value must be returned via that.  */
6833
6834   if (retval == result
6835       || DECL_CONSTRUCTOR_P (current_function_decl))
6836     /* It's already done for us.  */;
6837   else if (TREE_CODE (TREE_TYPE (retval)) == VOID_TYPE)
6838     {
6839       pedwarn ("return of void value in function returning non-void");
6840       expand_expr_stmt (retval);
6841       retval = 0;
6842     }
6843   else
6844     {
6845       tree functype = TREE_TYPE (TREE_TYPE (current_function_decl));
6846
6847       /* First convert the value to the function's return type, then
6848          to the type of return value's location to handle the
6849          case that functype is thiner than the valtype. */
6850
6851       retval = convert_for_initialization
6852         (NULL_TREE, functype, retval, LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING,
6853          "return", NULL_TREE, 0);
6854
6855       retval = convert (valtype, retval);
6856
6857       if (retval == error_mark_node)
6858         {
6859           /* Avoid warning about control reaching end of function.  */
6860           expand_null_return ();
6861           return;
6862         }
6863
6864       /* We can't initialize a register from a AGGR_INIT_EXPR.  */
6865       else if (! current_function_returns_struct
6866                && TREE_CODE (retval) == TARGET_EXPR
6867                && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
6868         retval = build (COMPOUND_EXPR, TREE_TYPE (retval), retval,
6869                         TREE_OPERAND (retval, 0));
6870
6871       /* Add some useful error checking for C++.  */
6872       else if (TREE_CODE (valtype) == REFERENCE_TYPE)
6873         {
6874           tree whats_returned;
6875
6876           /* Sort through common things to see what it is
6877              we are returning.  */
6878           whats_returned = retval;
6879           if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
6880             {
6881               whats_returned = TREE_OPERAND (whats_returned, 1);
6882               if (TREE_CODE (whats_returned) == ADDR_EXPR)
6883                 whats_returned = TREE_OPERAND (whats_returned, 0);
6884             }
6885           while (TREE_CODE (whats_returned) == CONVERT_EXPR
6886                  || TREE_CODE (whats_returned) == NOP_EXPR)
6887             whats_returned = TREE_OPERAND (whats_returned, 0);
6888           if (TREE_CODE (whats_returned) == ADDR_EXPR)
6889             {
6890               whats_returned = TREE_OPERAND (whats_returned, 0);
6891               while (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
6892                      || TREE_CODE (whats_returned) == TARGET_EXPR)
6893                 {
6894                   /* Get the target.  */
6895                   whats_returned = TREE_OPERAND (whats_returned, 0);
6896                   warning ("returning reference to temporary");
6897                 }
6898             }
6899
6900           if (TREE_CODE (whats_returned) == VAR_DECL && DECL_NAME (whats_returned))
6901             {
6902               if (TEMP_NAME_P (DECL_NAME (whats_returned)))
6903                 warning ("reference to non-lvalue returned");
6904               else if (TREE_CODE (TREE_TYPE (whats_returned)) != REFERENCE_TYPE
6905                        && DECL_FUNCTION_SCOPE_P (whats_returned)
6906                        && !(TREE_STATIC (whats_returned)
6907                             || TREE_PUBLIC (whats_returned)))
6908                 cp_warning_at ("reference to local variable `%D' returned", whats_returned);
6909             }
6910         }
6911       else if (TREE_CODE (retval) == ADDR_EXPR)
6912         {
6913           tree whats_returned = TREE_OPERAND (retval, 0);
6914
6915           if (TREE_CODE (whats_returned) == VAR_DECL
6916               && DECL_NAME (whats_returned)
6917               && DECL_FUNCTION_SCOPE_P (whats_returned)
6918               && !(TREE_STATIC (whats_returned)
6919                    || TREE_PUBLIC (whats_returned)))
6920             cp_warning_at ("address of local variable `%D' returned", whats_returned);
6921         }
6922     }
6923
6924   if (retval != NULL_TREE
6925       && TREE_CODE_CLASS (TREE_CODE (retval)) == 'd'
6926       && ! in_control_zone_p ())
6927     current_function_return_value = retval;
6928
6929   if (ctor_label && TREE_CODE (ctor_label) != ERROR_MARK)
6930     {
6931       /* Here RETVAL is CURRENT_CLASS_PTR, so there's nothing to do.  */
6932       expand_goto (ctor_label);
6933     }
6934
6935   if (retval && retval != result)
6936     {
6937       result = build (INIT_EXPR, TREE_TYPE (result), result, retval);
6938       TREE_SIDE_EFFECTS (result) = 1;
6939     }
6940
6941   expand_start_target_temps ();
6942
6943   expand_return (result);
6944
6945   expand_end_target_temps ();
6946
6947   current_function_returns_value = 1;
6948 }
6949 \f
6950 /* Start a C switch statement, testing expression EXP.
6951    Return EXP if it is valid, an error node otherwise.  */
6952
6953 tree
6954 c_expand_start_case (exp)
6955      tree exp;
6956 {
6957   tree type, idx;
6958
6959   exp = build_expr_type_conversion (WANT_INT | WANT_ENUM, exp, 1);
6960   if (exp == NULL_TREE)
6961     {
6962       error ("switch quantity not an integer");
6963       exp = error_mark_node;
6964     }
6965   if (exp == error_mark_node)
6966     return error_mark_node;
6967
6968   exp = default_conversion (exp);
6969   type = TREE_TYPE (exp);
6970   idx = get_unwidened (exp, 0);
6971   /* We can't strip a conversion from a signed type to an unsigned,
6972      because if we did, int_fits_type_p would do the wrong thing
6973      when checking case values for being in range,
6974      and it's too hard to do the right thing.  */
6975   if (TREE_UNSIGNED (TREE_TYPE (exp)) == TREE_UNSIGNED (TREE_TYPE (idx)))
6976     exp = idx;
6977
6978   expand_start_case
6979     (1, fold (build1 (CLEANUP_POINT_EXPR, TREE_TYPE (exp), exp)),
6980      type, "switch statement");
6981
6982   return exp;
6983 }
6984
6985 /* Returns non-zero if the pointer-type FROM can be converted to the
6986    pointer-type TO via a qualification conversion.  If CONSTP is -1,
6987    then we return non-zero if the pointers are similar, and the
6988    cv-qualification signature of FROM is a proper subset of that of TO.
6989
6990    If CONSTP is positive, then all outer pointers have been
6991    const-qualified.  */
6992
6993 static int
6994 comp_ptr_ttypes_real (to, from, constp)
6995      tree to, from;
6996      int constp;
6997 {
6998   int to_more_cv_qualified = 0;
6999
7000   for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7001     {
7002       if (TREE_CODE (to) != TREE_CODE (from))
7003         return 0;
7004
7005       if (TREE_CODE (from) == OFFSET_TYPE
7006           && same_type_p (TYPE_OFFSET_BASETYPE (from),
7007                           TYPE_OFFSET_BASETYPE (to)))
7008           continue;
7009
7010       /* Const and volatile mean something different for function types,
7011          so the usual checks are not appropriate.  */
7012       if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
7013         {
7014           if (!at_least_as_qualified_p (to, from))
7015             return 0;
7016
7017           if (!at_least_as_qualified_p (from, to))
7018             {
7019               if (constp == 0)
7020                 return 0;
7021               else
7022                 ++to_more_cv_qualified;
7023             }
7024
7025           if (constp > 0)
7026             constp &= TYPE_READONLY (to);
7027         }
7028
7029       if (TREE_CODE (to) != POINTER_TYPE)
7030         return 
7031           same_type_p (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from))
7032           && (constp >= 0 || to_more_cv_qualified);
7033     }
7034 }
7035
7036 /* When comparing, say, char ** to char const **, this function takes the
7037    'char *' and 'char const *'.  Do not pass non-pointer types to this
7038    function.  */
7039
7040 int
7041 comp_ptr_ttypes (to, from)
7042      tree to, from;
7043 {
7044   return comp_ptr_ttypes_real (to, from, 1);
7045 }
7046
7047 /* Returns 1 if to and from are (possibly multi-level) pointers to the same
7048    type or inheritance-related types, regardless of cv-quals.  */
7049
7050 int
7051 ptr_reasonably_similar (to, from)
7052      tree to, from;
7053 {
7054   for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7055     {
7056       if (TREE_CODE (to) != TREE_CODE (from))
7057         return 0;
7058
7059       if (TREE_CODE (from) == OFFSET_TYPE
7060           && comptypes (TYPE_OFFSET_BASETYPE (to),
7061                         TYPE_OFFSET_BASETYPE (from), 
7062                         COMPARE_BASE | COMPARE_RELAXED))
7063         continue;
7064
7065       if (TREE_CODE (to) != POINTER_TYPE)
7066         return comptypes
7067           (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from), 
7068            COMPARE_BASE | COMPARE_RELAXED);
7069     }
7070 }
7071
7072 /* Like comp_ptr_ttypes, for const_cast.  */
7073
7074 static int
7075 comp_ptr_ttypes_const (to, from)
7076      tree to, from;
7077 {
7078   for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7079     {
7080       if (TREE_CODE (to) != TREE_CODE (from))
7081         return 0;
7082
7083       if (TREE_CODE (from) == OFFSET_TYPE
7084           && same_type_p (TYPE_OFFSET_BASETYPE (from),
7085                           TYPE_OFFSET_BASETYPE (to)))
7086           continue;
7087
7088       if (TREE_CODE (to) != POINTER_TYPE)
7089         return same_type_p (TYPE_MAIN_VARIANT (to), 
7090                             TYPE_MAIN_VARIANT (from));
7091     }
7092 }
7093
7094 /* Like comp_ptr_ttypes, for reinterpret_cast.  */
7095
7096 static int
7097 comp_ptr_ttypes_reinterpret (to, from)
7098      tree to, from;
7099 {
7100   int constp = 1;
7101
7102   for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
7103     {
7104       if (TREE_CODE (from) == OFFSET_TYPE)
7105         from = TREE_TYPE (from);
7106       if (TREE_CODE (to) == OFFSET_TYPE)
7107         to = TREE_TYPE (to);
7108
7109       /* Const and volatile mean something different for function types,
7110          so the usual checks are not appropriate.  */
7111       if (TREE_CODE (from) != FUNCTION_TYPE && TREE_CODE (from) != METHOD_TYPE
7112           && TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
7113         {
7114           if (!at_least_as_qualified_p (to, from))
7115             return 0;
7116
7117           if (! constp
7118               && !at_least_as_qualified_p (from, to))
7119             return 0;
7120           constp &= TYPE_READONLY (to);
7121         }
7122
7123       if (TREE_CODE (from) != POINTER_TYPE
7124           || TREE_CODE (to) != POINTER_TYPE)
7125         return 1;
7126     }
7127 }
7128
7129 /* Returns the type-qualifier set corresponding to TYPE.  */
7130
7131 int
7132 cp_type_quals (type)
7133      tree type;
7134 {
7135   while (TREE_CODE (type) == ARRAY_TYPE)
7136     type = TREE_TYPE (type);
7137
7138   return TYPE_QUALS (type);
7139 }
7140
7141 /* Returns non-zero if the TYPE contains a mutable member */
7142
7143 int
7144 cp_has_mutable_p (type)
7145      tree type;
7146 {
7147   while (TREE_CODE (type) == ARRAY_TYPE)
7148     type = TREE_TYPE (type);
7149
7150   return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
7151 }
7152
7153 /* Subroutine of casts_away_constness.  Make T1 and T2 point at
7154    exemplar types such that casting T1 to T2 is casting away castness
7155    if and only if there is no implicit conversion from T1 to T2.  */
7156
7157 static void
7158 casts_away_constness_r (t1, t2)
7159      tree *t1;
7160      tree *t2;
7161 {
7162   int quals1;
7163   int quals2;
7164
7165   /* [expr.const.cast]
7166
7167      For multi-level pointer to members and multi-level mixed pointers
7168      and pointers to members (conv.qual), the "member" aspect of a
7169      pointer to member level is ignored when determining if a const
7170      cv-qualifier has been cast away.  */
7171   if (TYPE_PTRMEM_P (*t1))
7172     *t1 = build_pointer_type (TREE_TYPE (TREE_TYPE (*t1)));
7173   if (TYPE_PTRMEM_P (*t2))
7174     *t2 = build_pointer_type (TREE_TYPE (TREE_TYPE (*t2)));
7175
7176   /* [expr.const.cast]
7177
7178      For  two  pointer types:
7179
7180             X1 is T1cv1,1 * ... cv1,N *   where T1 is not a pointer type
7181             X2 is T2cv2,1 * ... cv2,M *   where T2 is not a pointer type
7182             K is min(N,M)
7183
7184      casting from X1 to X2 casts away constness if, for a non-pointer
7185      type T there does not exist an implicit conversion (clause
7186      _conv_) from:
7187
7188             Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
7189       
7190      to
7191
7192             Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *.  */
7193
7194   if (TREE_CODE (*t1) != POINTER_TYPE
7195       || TREE_CODE (*t2) != POINTER_TYPE)
7196     {
7197       *t1 = cp_build_qualified_type (void_type_node,
7198                                      CP_TYPE_QUALS (*t1));
7199       *t2 = cp_build_qualified_type (void_type_node,
7200                                      CP_TYPE_QUALS (*t2));
7201       return;
7202     }
7203   
7204   quals1 = CP_TYPE_QUALS (*t1);
7205   quals2 = CP_TYPE_QUALS (*t2);
7206   *t1 = TREE_TYPE (*t1);
7207   *t2 = TREE_TYPE (*t2);
7208   casts_away_constness_r (t1, t2);
7209   *t1 = build_pointer_type (*t1);
7210   *t2 = build_pointer_type (*t2);
7211   *t1 = cp_build_qualified_type (*t1, quals1);
7212   *t2 = cp_build_qualified_type (*t2, quals2);
7213 }
7214
7215 /* Returns non-zero if casting from TYPE1 to TYPE2 casts away
7216    constness.  */
7217
7218 static int
7219 casts_away_constness (t1, t2)
7220      tree t1;
7221      tree t2;
7222 {
7223   if (TREE_CODE (t2) == REFERENCE_TYPE)
7224     {
7225       /* [expr.const.cast]
7226          
7227          Casting from an lvalue of type T1 to an lvalue of type T2
7228          using a reference cast casts away constness if a cast from an
7229          rvalue of type "pointer to T1" to the type "pointer to T2"
7230          casts away constness.  */
7231       t1 = (TREE_CODE (t1) == REFERENCE_TYPE
7232             ? TREE_TYPE (t1) : t1);
7233       return casts_away_constness (build_pointer_type (t1),
7234                                    build_pointer_type (TREE_TYPE (t2)));
7235     }
7236
7237   if (TYPE_PTRMEM_P (t1) && TYPE_PTRMEM_P (t2))
7238     /* [expr.const.cast]
7239        
7240        Casting from an rvalue of type "pointer to data member of X
7241        of type T1" to the type "pointer to data member of Y of type
7242        T2" casts away constness if a cast from an rvalue of type
7243        "poitner to T1" to the type "pointer to T2" casts away
7244        constness.  */
7245     return casts_away_constness
7246       (build_pointer_type (TREE_TYPE (TREE_TYPE (t1))),
7247        build_pointer_type (TREE_TYPE (TREE_TYPE (t2))));
7248
7249   /* Casting away constness is only something that makes sense for
7250      pointer or reference types.  */
7251   if (TREE_CODE (t1) != POINTER_TYPE 
7252       || TREE_CODE (t2) != POINTER_TYPE)
7253     return 0;
7254
7255   /* Top-level qualifiers don't matter.  */
7256   t1 = TYPE_MAIN_VARIANT (t1);
7257   t2 = TYPE_MAIN_VARIANT (t2);
7258   casts_away_constness_r (&t1, &t2);
7259   if (!can_convert (t2, t1))
7260     return 1;
7261
7262   return 0;
7263 }