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