OSDN Git Service

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