OSDN Git Service

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