OSDN Git Service

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