OSDN Git Service

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