OSDN Git Service

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