OSDN Git Service

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