OSDN Git Service

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