OSDN Git Service

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