OSDN Git Service

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