OSDN Git Service

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