OSDN Git Service

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