OSDN Git Service

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