OSDN Git Service

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