OSDN Git Service

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