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