OSDN Git Service

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