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