OSDN Git Service

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