OSDN Git Service

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