OSDN Git Service

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