OSDN Git Service

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