OSDN Git Service

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