OSDN Git Service

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