OSDN Git Service

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