OSDN Git Service

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