OSDN Git Service

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