OSDN Git Service

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