OSDN Git Service

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