OSDN Git Service

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