OSDN Git Service

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