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