OSDN Git Service

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