OSDN Git Service

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