OSDN Git Service

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