OSDN Git Service

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