OSDN Git Service

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