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