OSDN Git Service

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