OSDN Git Service

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