OSDN Git Service

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