OSDN Git Service

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