OSDN Git Service

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