OSDN Git Service

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