OSDN Git Service

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