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)
5 This file is part of GCC.
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)
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.
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/>. */
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. */
29 #include "coretypes.h"
34 #include "double-int.h"
41 #include "fold-const.h"
42 #include "stor-layout.h"
46 #include "diagnostic.h"
50 #include "c-family/c-common.h"
51 #include "c-family/c-objc.h"
52 #include "c-family/c-ubsan.h"
55 static tree cp_build_addr_expr_strict (tree, tsubst_flags_t);
56 static tree cp_build_function_call (tree, tree, tsubst_flags_t);
57 static tree pfn_from_ptrmemfunc (tree);
58 static tree delta_from_ptrmemfunc (tree);
59 static tree convert_for_assignment (tree, tree, impl_conv_rhs, tree, int,
61 static tree cp_pointer_int_sum (enum tree_code, tree, tree, tsubst_flags_t);
62 static tree rationalize_conditional_expr (enum tree_code, tree,
64 static int comp_ptr_ttypes_real (tree, tree, int);
65 static bool comp_except_types (tree, tree, bool);
66 static bool comp_array_types (const_tree, const_tree, bool);
67 static tree pointer_diff (tree, tree, tree, tsubst_flags_t);
68 static tree get_delta_difference (tree, tree, bool, bool, tsubst_flags_t);
69 static void casts_away_constness_r (tree *, tree *, tsubst_flags_t);
70 static bool casts_away_constness (tree, tree, tsubst_flags_t);
71 static bool maybe_warn_about_returning_address_of_local (tree);
72 static tree lookup_destructor (tree, tree, tree, tsubst_flags_t);
73 static void warn_args_num (location_t, tree, bool);
74 static int convert_arguments (tree, vec<tree, va_gc> **, tree, int,
77 /* Do `exp = require_complete_type (exp);' to make sure exp
78 does not have an incomplete type. (That includes void types.)
79 Returns error_mark_node if the VALUE does not have
80 complete type when this function returns. */
83 require_complete_type_sfinae (tree value, tsubst_flags_t complain)
87 if (processing_template_decl || value == error_mark_node)
90 if (TREE_CODE (value) == OVERLOAD)
91 type = unknown_type_node;
93 type = TREE_TYPE (value);
95 if (type == error_mark_node)
96 return error_mark_node;
98 /* First, detect a valid value with a complete type. */
99 if (COMPLETE_TYPE_P (type))
102 if (complete_type_or_maybe_complain (type, value, complain))
105 return error_mark_node;
109 require_complete_type (tree value)
111 return require_complete_type_sfinae (value, tf_warning_or_error);
114 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
115 a template instantiation, do the instantiation. Returns TYPE,
116 whether or not it could be completed, unless something goes
117 horribly wrong, in which case the error_mark_node is returned. */
120 complete_type (tree type)
122 if (type == NULL_TREE)
123 /* Rather than crash, we return something sure to cause an error
125 return error_mark_node;
127 if (type == error_mark_node || COMPLETE_TYPE_P (type))
129 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
131 tree t = complete_type (TREE_TYPE (type));
132 unsigned int needs_constructing, has_nontrivial_dtor;
133 if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
136 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
138 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
139 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
141 TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
142 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
145 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
146 instantiate_class_template (TYPE_MAIN_VARIANT (type));
151 /* Like complete_type, but issue an error if the TYPE cannot be completed.
152 VALUE is used for informative diagnostics.
153 Returns NULL_TREE if the type cannot be made complete. */
156 complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
158 type = complete_type (type);
159 if (type == error_mark_node)
160 /* We already issued an error. */
162 else if (!COMPLETE_TYPE_P (type))
164 if (complain & tf_error)
165 cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
173 complete_type_or_else (tree type, tree value)
175 return complete_type_or_maybe_complain (type, value, tf_warning_or_error);
178 /* Return truthvalue of whether type of EXP is instantiated. */
181 type_unknown_p (const_tree exp)
183 return (TREE_CODE (exp) == TREE_LIST
184 || TREE_TYPE (exp) == unknown_type_node);
188 /* Return the common type of two parameter lists.
189 We assume that comptypes has already been done and returned 1;
190 if that isn't so, this may crash.
192 As an optimization, free the space we allocate if the parameter
193 lists are already common. */
196 commonparms (tree p1, tree p2)
198 tree oldargs = p1, newargs, n;
202 len = list_length (p1);
203 newargs = tree_last (p1);
205 if (newargs == void_list_node)
214 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
219 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
221 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
223 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
226 else if (! TREE_PURPOSE (p1))
228 if (TREE_PURPOSE (p2))
230 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
236 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
238 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
240 if (TREE_VALUE (p1) != TREE_VALUE (p2))
243 TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
246 TREE_VALUE (n) = TREE_VALUE (p1);
254 /* Given a type, perhaps copied for a typedef,
255 find the "original" version of it. */
257 original_type (tree t)
259 int quals = cp_type_quals (t);
260 while (t != error_mark_node
261 && TYPE_NAME (t) != NULL_TREE)
263 tree x = TYPE_NAME (t);
264 if (TREE_CODE (x) != TYPE_DECL)
266 x = DECL_ORIGINAL_TYPE (x);
271 return cp_build_qualified_type (t, quals);
274 /* Return the common type for two arithmetic types T1 and T2 under the
275 usual arithmetic conversions. The default conversions have already
276 been applied, and enumerated types converted to their compatible
280 cp_common_type (tree t1, tree t2)
282 enum tree_code code1 = TREE_CODE (t1);
283 enum tree_code code2 = TREE_CODE (t2);
288 /* In what follows, we slightly generalize the rules given in [expr] so
289 as to deal with `long long' and `complex'. First, merge the
291 attributes = (*targetm.merge_type_attributes) (t1, t2);
293 if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
295 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
296 return build_type_attribute_variant (t1, attributes);
301 /* FIXME: Attributes. */
302 gcc_assert (ARITHMETIC_TYPE_P (t1)
303 || TREE_CODE (t1) == VECTOR_TYPE
304 || UNSCOPED_ENUM_P (t1));
305 gcc_assert (ARITHMETIC_TYPE_P (t2)
306 || TREE_CODE (t2) == VECTOR_TYPE
307 || UNSCOPED_ENUM_P (t2));
309 /* If one type is complex, form the common type of the non-complex
310 components, then make that complex. Use T1 or T2 if it is the
312 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
314 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
315 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
317 = type_after_usual_arithmetic_conversions (subtype1, subtype2);
319 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
320 return build_type_attribute_variant (t1, attributes);
321 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
322 return build_type_attribute_variant (t2, attributes);
324 return build_type_attribute_variant (build_complex_type (subtype),
328 if (code1 == VECTOR_TYPE)
330 /* When we get here we should have two vectors of the same size.
331 Just prefer the unsigned one if present. */
332 if (TYPE_UNSIGNED (t1))
333 return build_type_attribute_variant (t1, attributes);
335 return build_type_attribute_variant (t2, attributes);
338 /* If only one is real, use it as the result. */
339 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
340 return build_type_attribute_variant (t1, attributes);
341 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
342 return build_type_attribute_variant (t2, attributes);
344 /* Both real or both integers; use the one with greater precision. */
345 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
346 return build_type_attribute_variant (t1, attributes);
347 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
348 return build_type_attribute_variant (t2, attributes);
350 /* The types are the same; no need to do anything fancy. */
351 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
352 return build_type_attribute_variant (t1, attributes);
354 if (code1 != REAL_TYPE)
356 /* If one is unsigned long long, then convert the other to unsigned
358 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
359 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
360 return build_type_attribute_variant (long_long_unsigned_type_node,
362 /* If one is a long long, and the other is an unsigned long, and
363 long long can represent all the values of an unsigned long, then
364 convert to a long long. Otherwise, convert to an unsigned long
365 long. Otherwise, if either operand is long long, convert the
368 Since we're here, we know the TYPE_PRECISION is the same;
369 therefore converting to long long cannot represent all the values
370 of an unsigned long, so we choose unsigned long long in that
372 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
373 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
375 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
376 ? long_long_unsigned_type_node
377 : long_long_integer_type_node);
378 return build_type_attribute_variant (t, attributes);
381 /* Go through the same procedure, but for longs. */
382 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
383 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
384 return build_type_attribute_variant (long_unsigned_type_node,
386 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
387 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
389 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
390 ? long_unsigned_type_node : long_integer_type_node);
391 return build_type_attribute_variant (t, attributes);
394 /* For __intN types, either the type is __int128 (and is lower
395 priority than the types checked above, but higher than other
396 128-bit types) or it's known to not be the same size as other
397 types (enforced in toplev.c). Prefer the unsigned type. */
398 for (i = 0; i < NUM_INT_N_ENTS; i ++)
400 if (int_n_enabled_p [i]
401 && (same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].signed_type)
402 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].signed_type)
403 || same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].unsigned_type)
404 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].unsigned_type)))
406 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
407 ? int_n_trees[i].unsigned_type
408 : int_n_trees[i].signed_type);
409 return build_type_attribute_variant (t, attributes);
413 /* Otherwise prefer the unsigned one. */
414 if (TYPE_UNSIGNED (t1))
415 return build_type_attribute_variant (t1, attributes);
417 return build_type_attribute_variant (t2, attributes);
421 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
422 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
423 return build_type_attribute_variant (long_double_type_node,
425 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
426 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
427 return build_type_attribute_variant (double_type_node,
429 if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
430 || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
431 return build_type_attribute_variant (float_type_node,
434 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
435 the standard C++ floating-point types. Logic earlier in this
436 function has already eliminated the possibility that
437 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
438 compelling reason to choose one or the other. */
439 return build_type_attribute_variant (t1, attributes);
443 /* T1 and T2 are arithmetic or enumeration types. Return the type
444 that will result from the "usual arithmetic conversions" on T1 and
445 T2 as described in [expr]. */
448 type_after_usual_arithmetic_conversions (tree t1, tree t2)
450 gcc_assert (ARITHMETIC_TYPE_P (t1)
451 || TREE_CODE (t1) == VECTOR_TYPE
452 || UNSCOPED_ENUM_P (t1));
453 gcc_assert (ARITHMETIC_TYPE_P (t2)
454 || TREE_CODE (t2) == VECTOR_TYPE
455 || UNSCOPED_ENUM_P (t2));
457 /* Perform the integral promotions. We do not promote real types here. */
458 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
459 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
461 t1 = type_promotes_to (t1);
462 t2 = type_promotes_to (t2);
465 return cp_common_type (t1, t2);
469 composite_pointer_error (diagnostic_t kind, tree t1, tree t2,
470 composite_pointer_operation operation)
475 emit_diagnostic (kind, input_location, 0,
476 "comparison between "
477 "distinct pointer types %qT and %qT lacks a cast",
481 emit_diagnostic (kind, input_location, 0,
482 "conversion between "
483 "distinct pointer types %qT and %qT lacks a cast",
486 case CPO_CONDITIONAL_EXPR:
487 emit_diagnostic (kind, input_location, 0,
488 "conditional expression between "
489 "distinct pointer types %qT and %qT lacks a cast",
497 /* Subroutine of composite_pointer_type to implement the recursive
498 case. See that function for documentation of the parameters. */
501 composite_pointer_type_r (tree t1, tree t2,
502 composite_pointer_operation operation,
503 tsubst_flags_t complain)
510 /* Determine the types pointed to by T1 and T2. */
513 pointee1 = TREE_TYPE (t1);
514 pointee2 = TREE_TYPE (t2);
518 pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
519 pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
524 Otherwise, the composite pointer type is a pointer type
525 similar (_conv.qual_) to the type of one of the operands,
526 with a cv-qualification signature (_conv.qual_) that is the
527 union of the cv-qualification signatures of the operand
529 if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
530 result_type = pointee1;
531 else if ((TYPE_PTR_P (pointee1) && TYPE_PTR_P (pointee2))
532 || (TYPE_PTRMEM_P (pointee1) && TYPE_PTRMEM_P (pointee2)))
534 result_type = composite_pointer_type_r (pointee1, pointee2, operation,
536 if (result_type == error_mark_node)
537 return error_mark_node;
541 if (complain & tf_error)
542 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
544 return error_mark_node;
545 result_type = void_type_node;
547 result_type = cp_build_qualified_type (result_type,
548 (cp_type_quals (pointee1)
549 | cp_type_quals (pointee2)));
550 /* If the original types were pointers to members, so is the
552 if (TYPE_PTRMEM_P (t1))
554 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
555 TYPE_PTRMEM_CLASS_TYPE (t2)))
557 if (complain & tf_error)
558 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
560 return error_mark_node;
562 result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
566 result_type = build_pointer_type (result_type);
568 /* Merge the attributes. */
569 attributes = (*targetm.merge_type_attributes) (t1, t2);
570 return build_type_attribute_variant (result_type, attributes);
573 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
574 ARG1 and ARG2 are the values with those types. The OPERATION is to
575 describe the operation between the pointer types,
576 in case an error occurs.
578 This routine also implements the computation of a common type for
579 pointers-to-members as per [expr.eq]. */
582 composite_pointer_type (tree t1, tree t2, tree arg1, tree arg2,
583 composite_pointer_operation operation,
584 tsubst_flags_t complain)
591 If one operand is a null pointer constant, the composite pointer
592 type is the type of the other operand. */
593 if (null_ptr_cst_p (arg1))
595 if (null_ptr_cst_p (arg2))
602 If one of the operands has type "pointer to cv1 void*", then
603 the other has type "pointer to cv2T", and the composite pointer
604 type is "pointer to cv12 void", where cv12 is the union of cv1
607 If either type is a pointer to void, make sure it is T1. */
608 if (TYPE_PTR_P (t2) && VOID_TYPE_P (TREE_TYPE (t2)))
616 /* Now, if T1 is a pointer to void, merge the qualifiers. */
617 if (TYPE_PTR_P (t1) && VOID_TYPE_P (TREE_TYPE (t1)))
622 if (TYPE_PTRFN_P (t2))
624 if (complain & tf_error)
629 pedwarn (input_location, OPT_Wpedantic,
630 "ISO C++ forbids comparison between pointer "
631 "of type %<void *%> and pointer-to-function");
634 pedwarn (input_location, OPT_Wpedantic,
635 "ISO C++ forbids conversion between pointer "
636 "of type %<void *%> and pointer-to-function");
638 case CPO_CONDITIONAL_EXPR:
639 pedwarn (input_location, OPT_Wpedantic,
640 "ISO C++ forbids conditional expression between "
641 "pointer of type %<void *%> and "
642 "pointer-to-function");
649 return error_mark_node;
652 = cp_build_qualified_type (void_type_node,
653 (cp_type_quals (TREE_TYPE (t1))
654 | cp_type_quals (TREE_TYPE (t2))));
655 result_type = build_pointer_type (result_type);
656 /* Merge the attributes. */
657 attributes = (*targetm.merge_type_attributes) (t1, t2);
658 return build_type_attribute_variant (result_type, attributes);
661 if (c_dialect_objc () && TYPE_PTR_P (t1)
664 if (objc_have_common_type (t1, t2, -3, NULL_TREE))
665 return objc_common_type (t1, t2);
668 /* [expr.eq] permits the application of a pointer conversion to
669 bring the pointers to a common type. */
670 if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2)
671 && CLASS_TYPE_P (TREE_TYPE (t1))
672 && CLASS_TYPE_P (TREE_TYPE (t2))
673 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
676 class1 = TREE_TYPE (t1);
677 class2 = TREE_TYPE (t2);
679 if (DERIVED_FROM_P (class1, class2))
680 t2 = (build_pointer_type
681 (cp_build_qualified_type (class1, cp_type_quals (class2))));
682 else if (DERIVED_FROM_P (class2, class1))
683 t1 = (build_pointer_type
684 (cp_build_qualified_type (class2, cp_type_quals (class1))));
687 if (complain & tf_error)
688 composite_pointer_error (DK_ERROR, t1, t2, operation);
689 return error_mark_node;
692 /* [expr.eq] permits the application of a pointer-to-member
693 conversion to change the class type of one of the types. */
694 else if (TYPE_PTRMEM_P (t1)
695 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
696 TYPE_PTRMEM_CLASS_TYPE (t2)))
698 class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
699 class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
701 if (DERIVED_FROM_P (class1, class2))
702 t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
703 else if (DERIVED_FROM_P (class2, class1))
704 t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
707 if (complain & tf_error)
711 error ("comparison between distinct "
712 "pointer-to-member types %qT and %qT lacks a cast",
716 error ("conversion between distinct "
717 "pointer-to-member types %qT and %qT lacks a cast",
720 case CPO_CONDITIONAL_EXPR:
721 error ("conditional expression between distinct "
722 "pointer-to-member types %qT and %qT lacks a cast",
728 return error_mark_node;
732 return composite_pointer_type_r (t1, t2, operation, complain);
735 /* Return the merged type of two types.
736 We assume that comptypes has already been done and returned 1;
737 if that isn't so, this may crash.
739 This just combines attributes and default arguments; any other
740 differences would cause the two types to compare unalike. */
743 merge_types (tree t1, tree t2)
745 enum tree_code code1;
746 enum tree_code code2;
749 /* Save time if the two types are the same. */
752 if (original_type (t1) == original_type (t2))
755 /* If one type is nonsense, use the other. */
756 if (t1 == error_mark_node)
758 if (t2 == error_mark_node)
761 /* Handle merging an auto redeclaration with a previous deduced
766 /* Merge the attributes. */
767 attributes = (*targetm.merge_type_attributes) (t1, t2);
769 if (TYPE_PTRMEMFUNC_P (t1))
770 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
771 if (TYPE_PTRMEMFUNC_P (t2))
772 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
774 code1 = TREE_CODE (t1);
775 code2 = TREE_CODE (t2);
778 gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
779 if (code1 == TYPENAME_TYPE)
781 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
782 code1 = TREE_CODE (t1);
786 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
787 code2 = TREE_CODE (t2);
795 /* For two pointers, do this recursively on the target type. */
797 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
798 int quals = cp_type_quals (t1);
800 if (code1 == POINTER_TYPE)
801 t1 = build_pointer_type (target);
803 t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
804 t1 = build_type_attribute_variant (t1, attributes);
805 t1 = cp_build_qualified_type (t1, quals);
807 if (TREE_CODE (target) == METHOD_TYPE)
808 t1 = build_ptrmemfunc_type (t1);
817 quals = cp_type_quals (t1);
818 pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
819 TYPE_PTRMEM_POINTED_TO_TYPE (t2));
820 t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
822 t1 = cp_build_qualified_type (t1, quals);
828 tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
829 /* Save space: see if the result is identical to one of the args. */
830 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
831 return build_type_attribute_variant (t1, attributes);
832 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
833 return build_type_attribute_variant (t2, attributes);
834 /* Merge the element types, and have a size if either arg has one. */
835 t1 = build_cplus_array_type
836 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
841 /* Function types: prefer the one that specified arg types.
842 If both do, merge the arg types. Also merge the return types. */
844 tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
845 tree p1 = TYPE_ARG_TYPES (t1);
846 tree p2 = TYPE_ARG_TYPES (t2);
849 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
851 /* Save space: see if the result is identical to one of the args. */
852 if (valtype == TREE_TYPE (t1) && ! p2)
853 return cp_build_type_attribute_variant (t1, attributes);
854 if (valtype == TREE_TYPE (t2) && ! p1)
855 return cp_build_type_attribute_variant (t2, attributes);
857 /* Simple way if one arg fails to specify argument types. */
858 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
860 else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
863 parms = commonparms (p1, p2);
865 rval = build_function_type (valtype, parms);
866 gcc_assert (type_memfn_quals (t1) == type_memfn_quals (t2));
867 gcc_assert (type_memfn_rqual (t1) == type_memfn_rqual (t2));
868 rval = apply_memfn_quals (rval,
869 type_memfn_quals (t1),
870 type_memfn_rqual (t1));
871 raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
872 TYPE_RAISES_EXCEPTIONS (t2));
873 t1 = build_exception_variant (rval, raises);
874 if (late_return_type_p)
875 TYPE_HAS_LATE_RETURN_TYPE (t1) = 1;
881 /* Get this value the long way, since TYPE_METHOD_BASETYPE
882 is just the main variant of this. */
883 tree basetype = class_of_this_parm (t2);
884 tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
885 TYPE_RAISES_EXCEPTIONS (t2));
886 cp_ref_qualifier rqual = type_memfn_rqual (t1);
888 bool late_return_type_1_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
889 bool late_return_type_2_p = TYPE_HAS_LATE_RETURN_TYPE (t2);
891 /* If this was a member function type, get back to the
892 original type of type member function (i.e., without
893 the class instance variable up front. */
894 t1 = build_function_type (TREE_TYPE (t1),
895 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
896 t2 = build_function_type (TREE_TYPE (t2),
897 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
898 t3 = merge_types (t1, t2);
899 t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
900 TYPE_ARG_TYPES (t3));
901 t1 = build_exception_variant (t3, raises);
902 t1 = build_ref_qualified_type (t1, rqual);
903 if (late_return_type_1_p)
904 TYPE_HAS_LATE_RETURN_TYPE (t1) = 1;
905 if (late_return_type_2_p)
906 TYPE_HAS_LATE_RETURN_TYPE (t2) = 1;
911 /* There is no need to merge attributes into a TYPENAME_TYPE.
912 When the type is instantiated it will have whatever
913 attributes result from the instantiation. */
919 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
921 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
924 return cp_build_type_attribute_variant (t1, attributes);
927 /* Return the ARRAY_TYPE type without its domain. */
930 strip_array_domain (tree type)
933 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
934 if (TYPE_DOMAIN (type) == NULL_TREE)
936 t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
937 return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
940 /* Wrapper around cp_common_type that is used by c-common.c and other
941 front end optimizations that remove promotions.
943 Return the common type for two arithmetic types T1 and T2 under the
944 usual arithmetic conversions. The default conversions have already
945 been applied, and enumerated types converted to their compatible
949 common_type (tree t1, tree t2)
951 /* If one type is nonsense, use the other */
952 if (t1 == error_mark_node)
954 if (t2 == error_mark_node)
957 return cp_common_type (t1, t2);
960 /* Return the common type of two pointer types T1 and T2. This is the
961 type for the result of most arithmetic operations if the operands
962 have the given two types.
964 We assume that comp_target_types has already been done and returned
965 nonzero; if that isn't so, this may crash. */
968 common_pointer_type (tree t1, tree t2)
970 gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
971 || (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
972 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
974 return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
975 CPO_CONVERSION, tf_warning_or_error);
978 /* Compare two exception specifier types for exactness or subsetness, if
979 allowed. Returns false for mismatch, true for match (same, or
982 [except.spec] "If a class X ... objects of class X or any class publicly
983 and unambiguously derived from X. Similarly, if a pointer type Y * ...
984 exceptions of type Y * or that are pointers to any type publicly and
985 unambiguously derived from Y. Otherwise a function only allows exceptions
986 that have the same type ..."
987 This does not mention cv qualifiers and is different to what throw
988 [except.throw] and catch [except.catch] will do. They will ignore the
989 top level cv qualifiers, and allow qualifiers in the pointer to class
992 We implement the letter of the standard. */
995 comp_except_types (tree a, tree b, bool exact)
997 if (same_type_p (a, b))
1001 if (cp_type_quals (a) || cp_type_quals (b))
1004 if (TYPE_PTR_P (a) && TYPE_PTR_P (b))
1008 if (cp_type_quals (a) || cp_type_quals (b))
1012 if (TREE_CODE (a) != RECORD_TYPE
1013 || TREE_CODE (b) != RECORD_TYPE)
1016 if (publicly_uniquely_derived_p (a, b))
1022 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
1023 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
1024 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
1025 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
1026 are unordered, but we've already filtered out duplicates. Most lists will
1027 be in order, we should try to make use of that. */
1030 comp_except_specs (const_tree t1, const_tree t2, int exact)
1039 /* First handle noexcept. */
1040 if (exact < ce_exact)
1042 /* noexcept(false) is compatible with no exception-specification,
1043 and stricter than any spec. */
1044 if (t1 == noexcept_false_spec)
1045 return t2 == NULL_TREE || exact == ce_derived;
1046 /* Even a derived noexcept(false) is compatible with no
1047 exception-specification. */
1048 if (t2 == noexcept_false_spec)
1049 return t1 == NULL_TREE;
1051 /* Otherwise, if we aren't looking for an exact match, noexcept is
1052 equivalent to throw(). */
1053 if (t1 == noexcept_true_spec)
1054 t1 = empty_except_spec;
1055 if (t2 == noexcept_true_spec)
1056 t2 = empty_except_spec;
1059 /* If any noexcept is left, it is only comparable to itself;
1060 either we're looking for an exact match or we're redeclaring a
1061 template with dependent noexcept. */
1062 if ((t1 && TREE_PURPOSE (t1))
1063 || (t2 && TREE_PURPOSE (t2)))
1065 && cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)));
1067 if (t1 == NULL_TREE) /* T1 is ... */
1068 return t2 == NULL_TREE || exact == ce_derived;
1069 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
1070 return t2 != NULL_TREE && !TREE_VALUE (t2);
1071 if (t2 == NULL_TREE) /* T2 is ... */
1073 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
1074 return exact == ce_derived;
1076 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1077 Count how many we find, to determine exactness. For exact matching and
1078 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1080 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1082 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1084 tree a = TREE_VALUE (probe);
1085 tree b = TREE_VALUE (t2);
1087 if (comp_except_types (a, b, exact))
1089 if (probe == base && exact > ce_derived)
1090 base = TREE_CHAIN (probe);
1095 if (probe == NULL_TREE)
1098 return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
1101 /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if
1102 [] can match [size]. */
1105 comp_array_types (const_tree t1, const_tree t2, bool allow_redeclaration)
1114 /* The type of the array elements must be the same. */
1115 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1118 d1 = TYPE_DOMAIN (t1);
1119 d2 = TYPE_DOMAIN (t2);
1124 /* If one of the arrays is dimensionless, and the other has a
1125 dimension, they are of different types. However, it is valid to
1133 declarations for an array object can specify
1134 array types that differ by the presence or absence of a major
1135 array bound (_dcl.array_). */
1137 return allow_redeclaration;
1139 /* Check that the dimensions are the same. */
1141 if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1143 max1 = TYPE_MAX_VALUE (d1);
1144 max2 = TYPE_MAX_VALUE (d2);
1146 if (!cp_tree_equal (max1, max2))
1152 /* Compare the relative position of T1 and T2 into their respective
1153 template parameter list.
1154 T1 and T2 must be template parameter types.
1155 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1158 comp_template_parms_position (tree t1, tree t2)
1160 tree index1, index2;
1161 gcc_assert (t1 && t2
1162 && TREE_CODE (t1) == TREE_CODE (t2)
1163 && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
1164 || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
1165 || TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
1167 index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1168 index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1170 /* Then compare their relative position. */
1171 if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1172 || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1173 || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1174 != TEMPLATE_PARM_PARAMETER_PACK (index2)))
1177 /* In C++14 we can end up comparing 'auto' to a normal template
1178 parameter. Don't confuse them. */
1179 if (cxx_dialect >= cxx14 && (is_auto (t1) || is_auto (t2)))
1180 return TYPE_IDENTIFIER (t1) == TYPE_IDENTIFIER (t2);
1185 /* Subroutine in comptypes. */
1188 structural_comptypes (tree t1, tree t2, int strict)
1193 /* Suppress errors caused by previously reported errors. */
1194 if (t1 == error_mark_node || t2 == error_mark_node)
1197 gcc_assert (TYPE_P (t1) && TYPE_P (t2));
1199 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1200 current instantiation. */
1201 if (TREE_CODE (t1) == TYPENAME_TYPE)
1202 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1204 if (TREE_CODE (t2) == TYPENAME_TYPE)
1205 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1207 if (TYPE_PTRMEMFUNC_P (t1))
1208 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1209 if (TYPE_PTRMEMFUNC_P (t2))
1210 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1212 /* Different classes of types can't be compatible. */
1213 if (TREE_CODE (t1) != TREE_CODE (t2))
1216 /* Qualifiers must match. For array types, we will check when we
1217 recur on the array element types. */
1218 if (TREE_CODE (t1) != ARRAY_TYPE
1219 && cp_type_quals (t1) != cp_type_quals (t2))
1221 if (TREE_CODE (t1) == FUNCTION_TYPE
1222 && type_memfn_quals (t1) != type_memfn_quals (t2))
1224 /* Need to check this before TYPE_MAIN_VARIANT.
1225 FIXME function qualifiers should really change the main variant. */
1226 if ((TREE_CODE (t1) == FUNCTION_TYPE
1227 || TREE_CODE (t1) == METHOD_TYPE)
1228 && type_memfn_rqual (t1) != type_memfn_rqual (t2))
1230 if (TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
1233 /* Allow for two different type nodes which have essentially the same
1234 definition. Note that we already checked for equality of the type
1235 qualifiers (just above). */
1237 if (TREE_CODE (t1) != ARRAY_TYPE
1238 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1242 /* Compare the types. Break out if they could be the same. */
1243 switch (TREE_CODE (t1))
1247 /* All void and bool types are the same. */
1251 case FIXED_POINT_TYPE:
1253 /* With these nodes, we can't determine type equivalence by
1254 looking at what is stored in the nodes themselves, because
1255 two nodes might have different TYPE_MAIN_VARIANTs but still
1256 represent the same type. For example, wchar_t and int could
1257 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1258 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1259 and are distinct types. On the other hand, int and the
1262 typedef int INT __attribute((may_alias));
1264 have identical properties, different TYPE_MAIN_VARIANTs, but
1265 represent the same type. The canonical type system keeps
1266 track of equivalence in this case, so we fall back on it. */
1267 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1269 case TEMPLATE_TEMPLATE_PARM:
1270 case BOUND_TEMPLATE_TEMPLATE_PARM:
1271 if (!comp_template_parms_position (t1, t2))
1273 if (!comp_template_parms
1274 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1275 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1277 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1279 /* Don't check inheritance. */
1280 strict = COMPARE_STRICT;
1285 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1286 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1287 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1288 && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1291 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1293 else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1299 if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1300 strict & ~COMPARE_REDECLARATION))
1302 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1306 case REFERENCE_TYPE:
1307 if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1309 /* fall through to checks for pointer types */
1312 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1313 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2)
1314 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1320 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1322 if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1327 /* Target types must match incl. qualifiers. */
1328 if (!comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION)))
1332 case TEMPLATE_TYPE_PARM:
1333 /* If T1 and T2 don't have the same relative position in their
1334 template parameters set, they can't be equal. */
1335 if (!comp_template_parms_position (t1, t2))
1340 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
1341 TYPENAME_TYPE_FULLNAME (t2)))
1343 /* Qualifiers don't matter on scopes. */
1344 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
1349 case UNBOUND_CLASS_TEMPLATE:
1350 if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
1352 if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
1357 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1362 if (TYPE_VECTOR_SUBPARTS (t1) != TYPE_VECTOR_SUBPARTS (t2)
1363 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1367 case TYPE_PACK_EXPANSION:
1368 return (same_type_p (PACK_EXPANSION_PATTERN (t1),
1369 PACK_EXPANSION_PATTERN (t2))
1370 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
1371 PACK_EXPANSION_EXTRA_ARGS (t2)));
1374 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
1375 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2)
1376 || (DECLTYPE_FOR_LAMBDA_CAPTURE (t1)
1377 != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
1378 || (DECLTYPE_FOR_LAMBDA_PROXY (t1)
1379 != DECLTYPE_FOR_LAMBDA_PROXY (t2))
1380 || !cp_tree_equal (DECLTYPE_TYPE_EXPR (t1),
1381 DECLTYPE_TYPE_EXPR (t2)))
1385 case UNDERLYING_TYPE:
1386 return same_type_p (UNDERLYING_TYPE_TYPE (t1),
1387 UNDERLYING_TYPE_TYPE (t2));
1393 /* If we get here, we know that from a target independent POV the
1394 types are the same. Make sure the target attributes are also
1396 return comp_type_attributes (t1, t2);
1399 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1400 is a bitwise-or of the COMPARE_* flags. */
1403 comptypes (tree t1, tree t2, int strict)
1405 if (strict == COMPARE_STRICT)
1410 if (t1 == error_mark_node || t2 == error_mark_node)
1413 if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
1414 /* At least one of the types requires structural equality, so
1415 perform a deep check. */
1416 return structural_comptypes (t1, t2, strict);
1418 #ifdef ENABLE_CHECKING
1419 if (USE_CANONICAL_TYPES)
1421 bool result = structural_comptypes (t1, t2, strict);
1423 if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1424 /* The two types are structurally equivalent, but their
1425 canonical types were different. This is a failure of the
1426 canonical type propagation code.*/
1428 ("canonical types differ for identical types %T and %T",
1430 else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1431 /* Two types are structurally different, but the canonical
1432 types are the same. This means we were over-eager in
1433 assigning canonical types. */
1435 ("same canonical type node for different types %T and %T",
1441 if (USE_CANONICAL_TYPES)
1442 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1445 return structural_comptypes (t1, t2, strict);
1447 else if (strict == COMPARE_STRUCTURAL)
1448 return structural_comptypes (t1, t2, COMPARE_STRICT);
1450 return structural_comptypes (t1, t2, strict);
1453 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1454 top-level qualifiers. */
1457 same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1459 if (type1 == error_mark_node || type2 == error_mark_node)
1462 return same_type_p (TYPE_MAIN_VARIANT (type1), TYPE_MAIN_VARIANT (type2));
1465 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1468 at_least_as_qualified_p (const_tree type1, const_tree type2)
1470 int q1 = cp_type_quals (type1);
1471 int q2 = cp_type_quals (type2);
1473 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1474 return (q1 & q2) == q2;
1477 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1478 more cv-qualified that TYPE1, and 0 otherwise. */
1481 comp_cv_qualification (int q1, int q2)
1486 if ((q1 & q2) == q2)
1488 else if ((q1 & q2) == q1)
1495 comp_cv_qualification (const_tree type1, const_tree type2)
1497 int q1 = cp_type_quals (type1);
1498 int q2 = cp_type_quals (type2);
1499 return comp_cv_qualification (q1, q2);
1502 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1503 subset of the cv-qualification signature of TYPE2, and the types
1504 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1507 comp_cv_qual_signature (tree type1, tree type2)
1509 if (comp_ptr_ttypes_real (type2, type1, -1))
1511 else if (comp_ptr_ttypes_real (type1, type2, -1))
1517 /* Subroutines of `comptypes'. */
1519 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1520 equivalent in the sense that functions with those parameter types
1521 can have equivalent types. The two lists must be equivalent,
1522 element by element. */
1525 compparms (const_tree parms1, const_tree parms2)
1529 /* An unspecified parmlist matches any specified parmlist
1530 whose argument types don't need default promotions. */
1532 for (t1 = parms1, t2 = parms2;
1534 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1536 /* If one parmlist is shorter than the other,
1537 they fail to match. */
1540 if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1547 /* Process a sizeof or alignof expression where the operand is a
1551 cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool complain)
1556 gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
1557 if (type == error_mark_node)
1558 return error_mark_node;
1560 type = non_reference (type);
1561 if (TREE_CODE (type) == METHOD_TYPE)
1564 pedwarn (input_location, OPT_Wpointer_arith,
1565 "invalid application of %qs to a member function",
1566 operator_name_info[(int) op].name);
1568 return error_mark_node;
1569 value = size_one_node;
1572 dependent_p = dependent_type_p (type);
1574 complete_type (type);
1576 /* VLA types will have a non-constant size. In the body of an
1577 uninstantiated template, we don't need to try to compute the
1578 value, because the sizeof expression is not an integral
1579 constant expression in that case. And, if we do try to
1580 compute the value, we'll likely end up with SAVE_EXPRs, which
1581 the template substitution machinery does not expect to see. */
1582 || (processing_template_decl
1583 && COMPLETE_TYPE_P (type)
1584 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
1586 value = build_min (op, size_type_node, type);
1587 TREE_READONLY (value) = 1;
1591 return c_sizeof_or_alignof_type (input_location, complete_type (type),
1592 op == SIZEOF_EXPR, false,
1596 /* Return the size of the type, without producing any warnings for
1597 types whose size cannot be taken. This routine should be used only
1598 in some other routine that has already produced a diagnostic about
1599 using the size of such a type. */
1601 cxx_sizeof_nowarn (tree type)
1603 if (TREE_CODE (type) == FUNCTION_TYPE
1604 || VOID_TYPE_P (type)
1605 || TREE_CODE (type) == ERROR_MARK)
1606 return size_one_node;
1607 else if (!COMPLETE_TYPE_P (type))
1608 return size_zero_node;
1610 return cxx_sizeof_or_alignof_type (type, SIZEOF_EXPR, false);
1613 /* Process a sizeof expression where the operand is an expression. */
1616 cxx_sizeof_expr (tree e, tsubst_flags_t complain)
1618 if (e == error_mark_node)
1619 return error_mark_node;
1621 if (processing_template_decl)
1623 e = build_min (SIZEOF_EXPR, size_type_node, e);
1624 TREE_SIDE_EFFECTS (e) = 0;
1625 TREE_READONLY (e) = 1;
1630 /* To get the size of a static data member declared as an array of
1631 unknown bound, we need to instantiate it. */
1633 && VAR_HAD_UNKNOWN_BOUND (e)
1634 && DECL_TEMPLATE_INSTANTIATION (e))
1635 instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false);
1637 if (TREE_CODE (e) == PARM_DECL
1638 && DECL_ARRAY_PARAMETER_P (e)
1639 && (complain & tf_warning))
1641 if (warning (OPT_Wsizeof_array_argument, "%<sizeof%> on array function "
1642 "parameter %qE will return size of %qT", e, TREE_TYPE (e)))
1643 inform (DECL_SOURCE_LOCATION (e), "declared here");
1646 e = mark_type_use (e);
1648 if (TREE_CODE (e) == COMPONENT_REF
1649 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1650 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1652 if (complain & tf_error)
1653 error ("invalid application of %<sizeof%> to a bit-field");
1655 return error_mark_node;
1658 else if (is_overloaded_fn (e))
1660 if (complain & tf_error)
1661 permerror (input_location, "ISO C++ forbids applying %<sizeof%> to an expression of "
1664 return error_mark_node;
1667 else if (type_unknown_p (e))
1669 if (complain & tf_error)
1670 cxx_incomplete_type_error (e, TREE_TYPE (e));
1672 return error_mark_node;
1678 return cxx_sizeof_or_alignof_type (e, SIZEOF_EXPR, complain & tf_error);
1681 /* Implement the __alignof keyword: Return the minimum required
1682 alignment of E, measured in bytes. For VAR_DECL's and
1683 FIELD_DECL's return DECL_ALIGN (which can be set from an
1684 "aligned" __attribute__ specification). */
1687 cxx_alignof_expr (tree e, tsubst_flags_t complain)
1691 if (e == error_mark_node)
1692 return error_mark_node;
1694 if (processing_template_decl)
1696 e = build_min (ALIGNOF_EXPR, size_type_node, e);
1697 TREE_SIDE_EFFECTS (e) = 0;
1698 TREE_READONLY (e) = 1;
1703 e = mark_type_use (e);
1706 t = size_int (DECL_ALIGN_UNIT (e));
1707 else if (TREE_CODE (e) == COMPONENT_REF
1708 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1709 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1711 if (complain & tf_error)
1712 error ("invalid application of %<__alignof%> to a bit-field");
1714 return error_mark_node;
1717 else if (TREE_CODE (e) == COMPONENT_REF
1718 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
1719 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
1720 else if (is_overloaded_fn (e))
1722 if (complain & tf_error)
1723 permerror (input_location, "ISO C++ forbids applying %<__alignof%> to an expression of "
1726 return error_mark_node;
1727 if (TREE_CODE (e) == FUNCTION_DECL)
1728 t = size_int (DECL_ALIGN_UNIT (e));
1732 else if (type_unknown_p (e))
1734 if (complain & tf_error)
1735 cxx_incomplete_type_error (e, TREE_TYPE (e));
1737 return error_mark_node;
1741 return cxx_sizeof_or_alignof_type (TREE_TYPE (e), ALIGNOF_EXPR,
1742 complain & tf_error);
1744 return fold_convert (size_type_node, t);
1747 /* Process a sizeof or alignof expression E with code OP where the operand
1748 is an expression. */
1751 cxx_sizeof_or_alignof_expr (tree e, enum tree_code op, bool complain)
1753 if (op == SIZEOF_EXPR)
1754 return cxx_sizeof_expr (e, complain? tf_warning_or_error : tf_none);
1756 return cxx_alignof_expr (e, complain? tf_warning_or_error : tf_none);
1759 /* Build a representation of an expression 'alignas(E).' Return the
1760 folded integer value of E if it is an integral constant expression
1761 that resolves to a valid alignment. If E depends on a template
1762 parameter, return a syntactic representation tree of kind
1763 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
1764 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
1767 cxx_alignas_expr (tree e)
1769 if (e == NULL_TREE || e == error_mark_node
1770 || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e)))
1776 When the alignment-specifier is of the form
1777 alignas(type-id ), it shall have the same effect as
1778 alignas(alignof(type-id )). */
1780 return cxx_sizeof_or_alignof_type (e, ALIGNOF_EXPR, false);
1782 /* If we reach this point, it means the alignas expression if of
1783 the form "alignas(assignment-expression)", so we should follow
1784 what is stated by [dcl.align]/2. */
1786 if (value_dependent_expression_p (e))
1787 /* Leave value-dependent expression alone for now. */
1790 e = instantiate_non_dependent_expr (e);
1791 e = mark_rvalue_use (e);
1793 /* [dcl.align]/2 says:
1795 the assignment-expression shall be an integral constant
1798 return cxx_constant_value (e);
1802 /* EXPR is being used in a context that is not a function call.
1807 The expression can be used only as the left-hand operand of a
1808 member function call.
1810 [expr.mptr.operator]
1812 If the result of .* or ->* is a function, then that result can be
1813 used only as the operand for the function call operator ().
1815 by issuing an error message if appropriate. Returns true iff EXPR
1816 violates these rules. */
1819 invalid_nonstatic_memfn_p (tree expr, tsubst_flags_t complain)
1821 if (expr == NULL_TREE)
1823 /* Don't enforce this in MS mode. */
1824 if (flag_ms_extensions)
1826 if (is_overloaded_fn (expr) && !really_overloaded_fn (expr))
1827 expr = get_first_fn (expr);
1828 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
1830 if (complain & tf_error)
1831 error ("invalid use of non-static member function");
1837 /* If EXP is a reference to a bitfield, and the type of EXP does not
1838 match the declared type of the bitfield, return the declared type
1839 of the bitfield. Otherwise, return NULL_TREE. */
1842 is_bitfield_expr_with_lowered_type (const_tree exp)
1844 switch (TREE_CODE (exp))
1847 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
1848 ? TREE_OPERAND (exp, 1)
1849 : TREE_OPERAND (exp, 0)))
1851 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
1854 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
1858 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1864 field = TREE_OPERAND (exp, 1);
1865 if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
1867 if (same_type_ignoring_top_level_qualifiers_p
1868 (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
1870 return DECL_BIT_FIELD_TYPE (field);
1874 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp, 0)))
1875 == TYPE_MAIN_VARIANT (TREE_TYPE (exp)))
1876 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1884 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1885 bitfield with a lowered type, the type of EXP is returned, rather
1889 unlowered_expr_type (const_tree exp)
1892 tree etype = TREE_TYPE (exp);
1894 type = is_bitfield_expr_with_lowered_type (exp);
1896 type = cp_build_qualified_type (type, cp_type_quals (etype));
1903 /* Perform the conversions in [expr] that apply when an lvalue appears
1904 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1905 function-to-pointer conversions. In addition, manifest constants
1906 are replaced by their values, and bitfield references are converted
1907 to their declared types. Note that this function does not perform the
1908 lvalue-to-rvalue conversion for class types. If you need that conversion
1909 to for class types, then you probably need to use force_rvalue.
1911 Although the returned value is being used as an rvalue, this
1912 function does not wrap the returned expression in a
1913 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1914 that the return value is no longer an lvalue. */
1917 decay_conversion (tree exp, tsubst_flags_t complain)
1920 enum tree_code code;
1921 location_t loc = EXPR_LOC_OR_LOC (exp, input_location);
1923 type = TREE_TYPE (exp);
1924 if (type == error_mark_node)
1925 return error_mark_node;
1927 exp = mark_rvalue_use (exp);
1929 exp = resolve_nondeduced_context (exp);
1930 if (type_unknown_p (exp))
1932 if (complain & tf_error)
1933 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
1934 return error_mark_node;
1937 code = TREE_CODE (type);
1939 /* FIXME remove for delayed folding. */
1940 exp = scalar_constant_value (exp);
1941 if (error_operand_p (exp))
1942 return error_mark_node;
1944 if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
1945 return nullptr_node;
1947 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1948 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1949 if (code == VOID_TYPE)
1951 if (complain & tf_error)
1952 error_at (loc, "void value not ignored as it ought to be");
1953 return error_mark_node;
1955 if (invalid_nonstatic_memfn_p (exp, complain))
1956 return error_mark_node;
1957 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1958 return cp_build_addr_expr (exp, complain);
1959 if (code == ARRAY_TYPE)
1964 if (INDIRECT_REF_P (exp))
1965 return build_nop (build_pointer_type (TREE_TYPE (type)),
1966 TREE_OPERAND (exp, 0));
1968 if (TREE_CODE (exp) == COMPOUND_EXPR)
1970 tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
1971 if (op1 == error_mark_node)
1972 return error_mark_node;
1973 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
1974 TREE_OPERAND (exp, 0), op1);
1978 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1980 if (complain & tf_error)
1981 error_at (loc, "invalid use of non-lvalue array");
1982 return error_mark_node;
1985 /* Don't let an array compound literal decay to a pointer. It can
1986 still be used to initialize an array or bind to a reference. */
1987 if (TREE_CODE (exp) == TARGET_EXPR)
1989 if (complain & tf_error)
1990 error_at (loc, "taking address of temporary array");
1991 return error_mark_node;
1994 ptrtype = build_pointer_type (TREE_TYPE (type));
1998 if (!cxx_mark_addressable (exp))
1999 return error_mark_node;
2000 adr = build_nop (ptrtype, build_address (exp));
2003 /* This way is better for a COMPONENT_REF since it can
2004 simplify the offset for a component. */
2005 adr = cp_build_addr_expr (exp, complain);
2006 return cp_convert (ptrtype, adr, complain);
2009 /* If a bitfield is used in a context where integral promotion
2010 applies, then the caller is expected to have used
2011 default_conversion. That function promotes bitfields correctly
2012 before calling this function. At this point, if we have a
2013 bitfield referenced, we may assume that is not subject to
2014 promotion, and that, therefore, the type of the resulting rvalue
2015 is the declared type of the bitfield. */
2016 exp = convert_bitfield_to_declared_type (exp);
2018 /* We do not call rvalue() here because we do not want to wrap EXP
2019 in a NON_LVALUE_EXPR. */
2023 Non-class rvalues always have cv-unqualified types. */
2024 type = TREE_TYPE (exp);
2025 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
2026 exp = build_nop (cv_unqualified (type), exp);
2031 /* Perform preparatory conversions, as part of the "usual arithmetic
2032 conversions". In particular, as per [expr]:
2034 Whenever an lvalue expression appears as an operand of an
2035 operator that expects the rvalue for that operand, the
2036 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2037 standard conversions are applied to convert the expression to an
2040 In addition, we perform integral promotions here, as those are
2041 applied to both operands to a binary operator before determining
2042 what additional conversions should apply. */
2045 cp_default_conversion (tree exp, tsubst_flags_t complain)
2047 /* Check for target-specific promotions. */
2048 tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
2050 exp = cp_convert (promoted_type, exp, complain);
2051 /* Perform the integral promotions first so that bitfield
2052 expressions (which may promote to "int", even if the bitfield is
2053 declared "unsigned") are promoted correctly. */
2054 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
2055 exp = cp_perform_integral_promotions (exp, complain);
2056 /* Perform the other conversions. */
2057 exp = decay_conversion (exp, complain);
2065 default_conversion (tree exp)
2067 return cp_default_conversion (exp, tf_warning_or_error);
2070 /* EXPR is an expression with an integral or enumeration type.
2071 Perform the integral promotions in [conv.prom], and return the
2075 cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
2080 expr = mark_rvalue_use (expr);
2084 If the bitfield has an enumerated type, it is treated as any
2085 other value of that type for promotion purposes. */
2086 type = is_bitfield_expr_with_lowered_type (expr);
2087 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
2088 type = TREE_TYPE (expr);
2089 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
2090 /* Scoped enums don't promote. */
2091 if (SCOPED_ENUM_P (type))
2093 promoted_type = type_promotes_to (type);
2094 if (type != promoted_type)
2095 expr = cp_convert (promoted_type, expr, complain);
2102 perform_integral_promotions (tree expr)
2104 return cp_perform_integral_promotions (expr, tf_warning_or_error);
2107 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2108 decay_conversion to one. */
2111 string_conv_p (const_tree totype, const_tree exp, int warn)
2115 if (!TYPE_PTR_P (totype))
2118 t = TREE_TYPE (totype);
2119 if (!same_type_p (t, char_type_node)
2120 && !same_type_p (t, char16_type_node)
2121 && !same_type_p (t, char32_type_node)
2122 && !same_type_p (t, wchar_type_node))
2125 if (TREE_CODE (exp) == STRING_CST)
2127 /* Make sure that we don't try to convert between char and wide chars. */
2128 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2133 /* Is this a string constant which has decayed to 'const char *'? */
2134 t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2135 if (!same_type_p (TREE_TYPE (exp), t))
2138 if (TREE_CODE (exp) != ADDR_EXPR
2139 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2144 if (cxx_dialect >= cxx11)
2145 pedwarn (input_location,
2146 pedantic ? OPT_Wpedantic : OPT_Wwrite_strings,
2147 "ISO C++ forbids converting a string constant to %qT",
2150 warning (OPT_Wwrite_strings,
2151 "deprecated conversion from string constant to %qT",
2158 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2159 can, for example, use as an lvalue. This code used to be in
2160 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2161 expressions, where we're dealing with aggregates. But now it's again only
2162 called from unary_complex_lvalue. The case (in particular) that led to
2163 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2167 rationalize_conditional_expr (enum tree_code code, tree t,
2168 tsubst_flags_t complain)
2170 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
2172 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2173 the first operand is always the one to be used if both operands
2174 are equal, so we know what conditional expression this used to be. */
2175 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2177 tree op0 = TREE_OPERAND (t, 0);
2178 tree op1 = TREE_OPERAND (t, 1);
2180 /* The following code is incorrect if either operand side-effects. */
2181 gcc_assert (!TREE_SIDE_EFFECTS (op0)
2182 && !TREE_SIDE_EFFECTS (op1));
2184 build_conditional_expr (loc,
2185 build_x_binary_op (loc,
2186 (TREE_CODE (t) == MIN_EXPR
2187 ? LE_EXPR : GE_EXPR),
2188 op0, TREE_CODE (op0),
2189 op1, TREE_CODE (op1),
2192 cp_build_unary_op (code, op0, 0, complain),
2193 cp_build_unary_op (code, op1, 0, complain),
2198 build_conditional_expr (loc, TREE_OPERAND (t, 0),
2199 cp_build_unary_op (code, TREE_OPERAND (t, 1), 0,
2201 cp_build_unary_op (code, TREE_OPERAND (t, 2), 0,
2206 /* Given the TYPE of an anonymous union field inside T, return the
2207 FIELD_DECL for the field. If not found return NULL_TREE. Because
2208 anonymous unions can nest, we must also search all anonymous unions
2209 that are directly reachable. */
2212 lookup_anon_field (tree t, tree type)
2216 t = TYPE_MAIN_VARIANT (t);
2218 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2220 if (TREE_STATIC (field))
2222 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
2225 /* If we find it directly, return the field. */
2226 if (DECL_NAME (field) == NULL_TREE
2227 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
2232 /* Otherwise, it could be nested, search harder. */
2233 if (DECL_NAME (field) == NULL_TREE
2234 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2236 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2244 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2245 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2246 non-NULL, it indicates the path to the base used to name MEMBER.
2247 If PRESERVE_REFERENCE is true, the expression returned will have
2248 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2249 returned will have the type referred to by the reference.
2251 This function does not perform access control; that is either done
2252 earlier by the parser when the name of MEMBER is resolved to MEMBER
2253 itself, or later when overload resolution selects one of the
2254 functions indicated by MEMBER. */
2257 build_class_member_access_expr (tree object, tree member,
2258 tree access_path, bool preserve_reference,
2259 tsubst_flags_t complain)
2263 tree result = NULL_TREE;
2264 tree using_decl = NULL_TREE;
2266 if (error_operand_p (object) || error_operand_p (member))
2267 return error_mark_node;
2269 gcc_assert (DECL_P (member) || BASELINK_P (member));
2273 The type of the first expression shall be "class object" (of a
2275 object_type = TREE_TYPE (object);
2276 if (!currently_open_class (object_type)
2277 && !complete_type_or_maybe_complain (object_type, object, complain))
2278 return error_mark_node;
2279 if (!CLASS_TYPE_P (object_type))
2281 if (complain & tf_error)
2283 if (POINTER_TYPE_P (object_type)
2284 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2285 error ("request for member %qD in %qE, which is of pointer "
2286 "type %qT (maybe you meant to use %<->%> ?)",
2287 member, object, object_type);
2289 error ("request for member %qD in %qE, which is of non-class "
2290 "type %qT", member, object, object_type);
2292 return error_mark_node;
2295 /* The standard does not seem to actually say that MEMBER must be a
2296 member of OBJECT_TYPE. However, that is clearly what is
2298 if (DECL_P (member))
2300 member_scope = DECL_CLASS_CONTEXT (member);
2302 if (TREE_DEPRECATED (member))
2303 warn_deprecated_use (member, NULL_TREE);
2306 member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2307 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2308 presently be the anonymous union. Go outwards until we find a
2309 type related to OBJECT_TYPE. */
2310 while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope))
2311 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2313 member_scope = TYPE_CONTEXT (member_scope);
2314 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2316 if (complain & tf_error)
2318 if (TREE_CODE (member) == FIELD_DECL)
2319 error ("invalid use of nonstatic data member %qE", member);
2321 error ("%qD is not a member of %qT", member, object_type);
2323 return error_mark_node;
2326 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2327 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2328 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2330 tree temp = unary_complex_lvalue (ADDR_EXPR, object);
2332 object = cp_build_indirect_ref (temp, RO_NULL, complain);
2335 /* In [expr.ref], there is an explicit list of the valid choices for
2336 MEMBER. We check for each of those cases here. */
2339 /* A static data member. */
2341 mark_exp_read (object);
2342 /* If OBJECT has side-effects, they are supposed to occur. */
2343 if (TREE_SIDE_EFFECTS (object))
2344 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2346 else if (TREE_CODE (member) == FIELD_DECL)
2348 /* A non-static data member. */
2353 null_object_p = (INDIRECT_REF_P (object)
2354 && integer_zerop (TREE_OPERAND (object, 0)));
2356 /* Convert OBJECT to the type of MEMBER. */
2357 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2358 TYPE_MAIN_VARIANT (member_scope)))
2363 binfo = lookup_base (access_path ? access_path : object_type,
2364 member_scope, ba_unique, &kind, complain);
2365 if (binfo == error_mark_node)
2366 return error_mark_node;
2368 /* It is invalid to try to get to a virtual base of a
2369 NULL object. The most common cause is invalid use of
2371 if (null_object_p && kind == bk_via_virtual)
2373 if (complain & tf_error)
2375 error ("invalid access to non-static data member %qD in "
2376 "virtual base of NULL object", member);
2378 return error_mark_node;
2381 /* Convert to the base. */
2382 object = build_base_path (PLUS_EXPR, object, binfo,
2383 /*nonnull=*/1, complain);
2384 /* If we found the base successfully then we should be able
2385 to convert to it successfully. */
2386 gcc_assert (object != error_mark_node);
2389 /* If MEMBER is from an anonymous aggregate, we have converted
2390 OBJECT so that it refers to the class containing the
2391 anonymous union. Generate a reference to the anonymous union
2392 itself, and recur to find MEMBER. */
2393 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2394 /* When this code is called from build_field_call, the
2395 object already has the type of the anonymous union.
2396 That is because the COMPONENT_REF was already
2397 constructed, and was then disassembled before calling
2398 build_field_call. After the function-call code is
2399 cleaned up, this waste can be eliminated. */
2400 && (!same_type_ignoring_top_level_qualifiers_p
2401 (TREE_TYPE (object), DECL_CONTEXT (member))))
2403 tree anonymous_union;
2405 anonymous_union = lookup_anon_field (TREE_TYPE (object),
2406 DECL_CONTEXT (member));
2407 object = build_class_member_access_expr (object,
2409 /*access_path=*/NULL_TREE,
2414 /* Compute the type of the field, as described in [expr.ref]. */
2415 type_quals = TYPE_UNQUALIFIED;
2416 member_type = TREE_TYPE (member);
2417 if (TREE_CODE (member_type) != REFERENCE_TYPE)
2419 type_quals = (cp_type_quals (member_type)
2420 | cp_type_quals (object_type));
2422 /* A field is const (volatile) if the enclosing object, or the
2423 field itself, is const (volatile). But, a mutable field is
2424 not const, even within a const object. */
2425 if (DECL_MUTABLE_P (member))
2426 type_quals &= ~TYPE_QUAL_CONST;
2427 member_type = cp_build_qualified_type (member_type, type_quals);
2430 result = build3_loc (input_location, COMPONENT_REF, member_type,
2431 object, member, NULL_TREE);
2432 result = fold_if_not_in_template (result);
2434 /* Mark the expression const or volatile, as appropriate. Even
2435 though we've dealt with the type above, we still have to mark the
2436 expression itself. */
2437 if (type_quals & TYPE_QUAL_CONST)
2438 TREE_READONLY (result) = 1;
2439 if (type_quals & TYPE_QUAL_VOLATILE)
2440 TREE_THIS_VOLATILE (result) = 1;
2442 else if (BASELINK_P (member))
2444 /* The member is a (possibly overloaded) member function. */
2448 /* If the MEMBER is exactly one static member function, then we
2449 know the type of the expression. Otherwise, we must wait
2450 until overload resolution has been performed. */
2451 functions = BASELINK_FUNCTIONS (member);
2452 if (TREE_CODE (functions) == FUNCTION_DECL
2453 && DECL_STATIC_FUNCTION_P (functions))
2454 type = TREE_TYPE (functions);
2456 type = unknown_type_node;
2457 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2458 base. That will happen when the function is called. */
2459 result = build3 (COMPONENT_REF, type, object, member, NULL_TREE);
2461 else if (TREE_CODE (member) == CONST_DECL)
2463 /* The member is an enumerator. */
2465 /* If OBJECT has side-effects, they are supposed to occur. */
2466 if (TREE_SIDE_EFFECTS (object))
2467 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
2470 else if ((using_decl = strip_using_decl (member)) != member)
2471 result = build_class_member_access_expr (object,
2473 access_path, preserve_reference,
2477 if (complain & tf_error)
2478 error ("invalid use of %qD", member);
2479 return error_mark_node;
2482 if (!preserve_reference)
2485 If E2 is declared to have type "reference to T", then ... the
2486 type of E1.E2 is T. */
2487 result = convert_from_reference (result);
2492 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2493 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2496 lookup_destructor (tree object, tree scope, tree dtor_name,
2497 tsubst_flags_t complain)
2499 tree object_type = TREE_TYPE (object);
2500 tree dtor_type = TREE_OPERAND (dtor_name, 0);
2503 /* We've already complained about this destructor. */
2504 if (dtor_type == error_mark_node)
2505 return error_mark_node;
2507 if (scope && !check_dtor_name (scope, dtor_type))
2509 if (complain & tf_error)
2510 error ("qualified type %qT does not match destructor name ~%qT",
2512 return error_mark_node;
2514 if (is_auto (dtor_type))
2515 dtor_type = object_type;
2516 else if (identifier_p (dtor_type))
2518 /* In a template, names we can't find a match for are still accepted
2519 destructor names, and we check them here. */
2520 if (check_dtor_name (object_type, dtor_type))
2521 dtor_type = object_type;
2524 if (complain & tf_error)
2525 error ("object type %qT does not match destructor name ~%qT",
2526 object_type, dtor_type);
2527 return error_mark_node;
2531 else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2533 if (complain & tf_error)
2534 error ("the type being destroyed is %qT, but the destructor "
2535 "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type);
2536 return error_mark_node;
2538 expr = lookup_member (dtor_type, complete_dtor_identifier,
2539 /*protect=*/1, /*want_type=*/false,
2540 tf_warning_or_error);
2543 if (complain & tf_error)
2544 cxx_incomplete_type_error (dtor_name, dtor_type);
2545 return error_mark_node;
2547 expr = (adjust_result_of_qualified_name_lookup
2548 (expr, dtor_type, object_type));
2549 if (scope == NULL_TREE)
2550 /* We need to call adjust_result_of_qualified_name_lookup in case the
2551 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2552 that we still get virtual function binding. */
2553 BASELINK_QUALIFIED_P (expr) = false;
2557 /* An expression of the form "A::template B" has been resolved to
2558 DECL. Issue a diagnostic if B is not a template or template
2562 check_template_keyword (tree decl)
2564 /* The standard says:
2568 If a name prefixed by the keyword template is not a member
2569 template, the program is ill-formed.
2571 DR 228 removed the restriction that the template be a member
2574 DR 96, if accepted would add the further restriction that explicit
2575 template arguments must be provided if the template keyword is
2576 used, but, as of 2005-10-16, that DR is still in "drafting". If
2577 this DR is accepted, then the semantic checks here can be
2578 simplified, as the entity named must in fact be a template
2579 specialization, rather than, as at present, a set of overloaded
2580 functions containing at least one template function. */
2581 if (TREE_CODE (decl) != TEMPLATE_DECL
2582 && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
2584 if (!is_overloaded_fn (decl))
2585 permerror (input_location, "%qD is not a template", decl);
2590 if (BASELINK_P (fns))
2591 fns = BASELINK_FUNCTIONS (fns);
2594 tree fn = OVL_CURRENT (fns);
2595 if (TREE_CODE (fn) == TEMPLATE_DECL
2596 || TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2598 if (TREE_CODE (fn) == FUNCTION_DECL
2599 && DECL_USE_TEMPLATE (fn)
2600 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
2602 fns = OVL_NEXT (fns);
2605 permerror (input_location, "%qD is not a template", decl);
2610 /* This function is called by the parser to process a class member
2611 access expression of the form OBJECT.NAME. NAME is a node used by
2612 the parser to represent a name; it is not yet a DECL. It may,
2613 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2614 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2615 there is no reason to do the lookup twice, so the parser keeps the
2616 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2617 be a template via the use of the "A::template B" syntax. */
2620 finish_class_member_access_expr (tree object, tree name, bool template_p,
2621 tsubst_flags_t complain)
2626 tree access_path = NULL_TREE;
2627 tree orig_object = object;
2628 tree orig_name = name;
2630 if (object == error_mark_node || name == error_mark_node)
2631 return error_mark_node;
2633 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2634 if (!objc_is_public (object, name))
2635 return error_mark_node;
2637 object_type = TREE_TYPE (object);
2639 if (processing_template_decl)
2641 if (/* If OBJECT_TYPE is dependent, so is OBJECT.NAME. */
2642 dependent_type_p (object_type)
2643 /* If NAME is just an IDENTIFIER_NODE, then the expression
2645 || identifier_p (object)
2646 /* If NAME is "f<args>", where either 'f' or 'args' is
2647 dependent, then the expression is dependent. */
2648 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
2649 && dependent_template_id_p (TREE_OPERAND (name, 0),
2650 TREE_OPERAND (name, 1)))
2651 /* If NAME is "T::X" where "T" is dependent, then the
2652 expression is dependent. */
2653 || (TREE_CODE (name) == SCOPE_REF
2654 && TYPE_P (TREE_OPERAND (name, 0))
2655 && dependent_type_p (TREE_OPERAND (name, 0))))
2656 return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
2657 object, name, NULL_TREE);
2658 object = build_non_dependent_expr (object);
2660 else if (c_dialect_objc ()
2661 && identifier_p (name)
2662 && (expr = objc_maybe_build_component_ref (object, name)))
2667 The type of the first expression shall be "class object" (of a
2669 if (!currently_open_class (object_type)
2670 && !complete_type_or_maybe_complain (object_type, object, complain))
2671 return error_mark_node;
2672 if (!CLASS_TYPE_P (object_type))
2674 if (complain & tf_error)
2676 if (POINTER_TYPE_P (object_type)
2677 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2678 error ("request for member %qD in %qE, which is of pointer "
2679 "type %qT (maybe you meant to use %<->%> ?)",
2680 name, object, object_type);
2682 error ("request for member %qD in %qE, which is of non-class "
2683 "type %qT", name, object, object_type);
2685 return error_mark_node;
2688 if (BASELINK_P (name))
2689 /* A member function that has already been looked up. */
2693 bool is_template_id = false;
2694 tree template_args = NULL_TREE;
2697 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2699 is_template_id = true;
2700 template_args = TREE_OPERAND (name, 1);
2701 name = TREE_OPERAND (name, 0);
2703 if (TREE_CODE (name) == OVERLOAD)
2704 name = DECL_NAME (get_first_fn (name));
2705 else if (DECL_P (name))
2706 name = DECL_NAME (name);
2709 if (TREE_CODE (name) == SCOPE_REF)
2711 /* A qualified name. The qualifying class or namespace `S'
2712 has already been looked up; it is either a TYPE or a
2714 scope = TREE_OPERAND (name, 0);
2715 name = TREE_OPERAND (name, 1);
2717 /* If SCOPE is a namespace, then the qualified name does not
2718 name a member of OBJECT_TYPE. */
2719 if (TREE_CODE (scope) == NAMESPACE_DECL)
2721 if (complain & tf_error)
2722 error ("%<%D::%D%> is not a member of %qT",
2723 scope, name, object_type);
2724 return error_mark_node;
2727 if (TREE_CODE (scope) == ENUMERAL_TYPE)
2729 /* Looking up a member enumerator (c++/56793). */
2730 if (!TYPE_CLASS_SCOPE_P (scope)
2731 || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
2733 if (complain & tf_error)
2734 error ("%<%D::%D%> is not a member of %qT",
2735 scope, name, object_type);
2736 return error_mark_node;
2738 tree val = lookup_enumerator (scope, name);
2739 if (TREE_SIDE_EFFECTS (object))
2740 val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
2744 gcc_assert (CLASS_TYPE_P (scope));
2745 gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR);
2747 if (constructor_name_p (name, scope))
2749 if (complain & tf_error)
2750 error ("cannot call constructor %<%T::%D%> directly",
2752 return error_mark_node;
2755 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2756 access_path = lookup_base (object_type, scope, ba_check,
2758 if (access_path == error_mark_node)
2759 return error_mark_node;
2762 if (complain & tf_error)
2763 error ("%qT is not a base of %qT", scope, object_type);
2764 return error_mark_node;
2770 access_path = object_type;
2773 if (TREE_CODE (name) == BIT_NOT_EXPR)
2774 member = lookup_destructor (object, scope, name, complain);
2777 /* Look up the member. */
2778 member = lookup_member (access_path, name, /*protect=*/1,
2779 /*want_type=*/false, complain);
2780 if (member == NULL_TREE)
2782 if (complain & tf_error)
2783 error ("%q#T has no member named %qE",
2784 TREE_CODE (access_path) == TREE_BINFO
2785 ? TREE_TYPE (access_path) : object_type, name);
2786 return error_mark_node;
2788 if (member == error_mark_node)
2789 return error_mark_node;
2794 tree templ = member;
2796 if (BASELINK_P (templ))
2797 templ = lookup_template_function (templ, template_args);
2800 if (complain & tf_error)
2801 error ("%qD is not a member template function", name);
2802 return error_mark_node;
2807 if (TREE_DEPRECATED (member))
2808 warn_deprecated_use (member, NULL_TREE);
2811 check_template_keyword (member);
2813 expr = build_class_member_access_expr (object, member, access_path,
2814 /*preserve_reference=*/false,
2816 if (processing_template_decl && expr != error_mark_node)
2818 if (BASELINK_P (member))
2820 if (TREE_CODE (orig_name) == SCOPE_REF)
2821 BASELINK_QUALIFIED_P (member) = 1;
2824 return build_min_non_dep (COMPONENT_REF, expr,
2825 orig_object, orig_name,
2832 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
2836 build_simple_component_ref (tree object, tree member)
2838 tree type = cp_build_qualified_type (TREE_TYPE (member),
2839 cp_type_quals (TREE_TYPE (object)));
2840 return fold_build3_loc (input_location,
2841 COMPONENT_REF, type,
2842 object, member, NULL_TREE);
2845 /* Return an expression for the MEMBER_NAME field in the internal
2846 representation of PTRMEM, a pointer-to-member function. (Each
2847 pointer-to-member function type gets its own RECORD_TYPE so it is
2848 more convenient to access the fields by name than by FIELD_DECL.)
2849 This routine converts the NAME to a FIELD_DECL and then creates the
2850 node for the complete expression. */
2853 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
2858 /* This code is a stripped down version of
2859 build_class_member_access_expr. It does not work to use that
2860 routine directly because it expects the object to be of class
2862 ptrmem_type = TREE_TYPE (ptrmem);
2863 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
2864 for (member = TYPE_FIELDS (ptrmem_type); member;
2865 member = DECL_CHAIN (member))
2866 if (DECL_NAME (member) == member_name)
2868 return build_simple_component_ref (ptrmem, member);
2871 /* Given an expression PTR for a pointer, return an expression
2872 for the value pointed to.
2873 ERRORSTRING is the name of the operator to appear in error messages.
2875 This function may need to overload OPERATOR_FNNAME.
2876 Must also handle REFERENCE_TYPEs for C++. */
2879 build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
2880 tsubst_flags_t complain)
2882 tree orig_expr = expr;
2885 if (processing_template_decl)
2887 /* Retain the type if we know the operand is a pointer. */
2888 if (TREE_TYPE (expr) && POINTER_TYPE_P (TREE_TYPE (expr)))
2889 return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
2890 if (type_dependent_expression_p (expr))
2891 return build_min_nt_loc (loc, INDIRECT_REF, expr);
2892 expr = build_non_dependent_expr (expr);
2895 rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
2896 NULL_TREE, NULL_TREE, /*overload=*/NULL, complain);
2898 rval = cp_build_indirect_ref (expr, errorstring, complain);
2900 if (processing_template_decl && rval != error_mark_node)
2901 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
2906 /* Helper function called from c-common. */
2908 build_indirect_ref (location_t /*loc*/,
2909 tree ptr, ref_operator errorstring)
2911 return cp_build_indirect_ref (ptr, errorstring, tf_warning_or_error);
2915 cp_build_indirect_ref (tree ptr, ref_operator errorstring,
2916 tsubst_flags_t complain)
2920 if (ptr == current_class_ptr
2921 || (TREE_CODE (ptr) == NOP_EXPR
2922 && TREE_OPERAND (ptr, 0) == current_class_ptr
2923 && (same_type_ignoring_top_level_qualifiers_p
2924 (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
2925 return current_class_ref;
2927 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2928 ? ptr : decay_conversion (ptr, complain));
2929 if (pointer == error_mark_node)
2930 return error_mark_node;
2932 type = TREE_TYPE (pointer);
2934 if (POINTER_TYPE_P (type))
2938 If the type of the expression is "pointer to T," the type
2939 of the result is "T." */
2940 tree t = TREE_TYPE (type);
2942 if ((CONVERT_EXPR_P (ptr)
2943 || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
2944 && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t)))
2946 /* If a warning is issued, mark it to avoid duplicates from
2947 the backend. This only needs to be done at
2948 warn_strict_aliasing > 2. */
2949 if (warn_strict_aliasing > 2)
2950 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr, 0)),
2951 type, TREE_OPERAND (ptr, 0)))
2952 TREE_NO_WARNING (ptr) = 1;
2955 if (VOID_TYPE_P (t))
2957 /* A pointer to incomplete type (other than cv void) can be
2958 dereferenced [expr.unary.op]/1 */
2959 if (complain & tf_error)
2960 error ("%qT is not a pointer-to-object type", type);
2961 return error_mark_node;
2963 else if (TREE_CODE (pointer) == ADDR_EXPR
2964 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
2965 /* The POINTER was something like `&x'. We simplify `*&x' to
2967 return TREE_OPERAND (pointer, 0);
2970 tree ref = build1 (INDIRECT_REF, t, pointer);
2972 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2973 so that we get the proper error message if the result is used
2974 to assign to. Also, &* is supposed to be a no-op. */
2975 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
2976 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
2977 TREE_SIDE_EFFECTS (ref)
2978 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
2982 else if (!(complain & tf_error))
2983 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
2985 /* `pointer' won't be an error_mark_node if we were given a
2986 pointer to member, so it's cool to check for this here. */
2987 else if (TYPE_PTRMEM_P (type))
2988 switch (errorstring)
2990 case RO_ARRAY_INDEXING:
2991 error ("invalid use of array indexing on pointer to member");
2994 error ("invalid use of unary %<*%> on pointer to member");
2996 case RO_IMPLICIT_CONVERSION:
2997 error ("invalid use of implicit conversion on pointer to member");
3000 error ("left hand operand of %<->*%> must be a pointer to class, "
3001 "but is a pointer to member of type %qT", type);
3006 else if (pointer != error_mark_node)
3007 invalid_indirection_error (input_location, type, errorstring);
3009 return error_mark_node;
3012 /* This handles expressions of the form "a[i]", which denotes
3015 This is logically equivalent in C to *(a+i), but we may do it differently.
3016 If A is a variable or a member, we generate a primitive ARRAY_REF.
3017 This avoids forcing the array out of registers, and can work on
3018 arrays that are not lvalues (for example, members of structures returned
3021 If INDEX is of some user-defined type, it must be converted to
3022 integer type. Otherwise, to make a compatible PLUS_EXPR, it
3023 will inherit the type of the array, which will be some pointer type.
3025 LOC is the location to use in building the array reference. */
3028 cp_build_array_ref (location_t loc, tree array, tree idx,
3029 tsubst_flags_t complain)
3035 if (complain & tf_error)
3036 error_at (loc, "subscript missing in array reference");
3037 return error_mark_node;
3040 /* If an array's index is an array notation, then its rank cannot be
3041 greater than one. */
3042 if (flag_cilkplus && contains_array_notation_expr (idx))
3046 /* If find_rank returns false, then it should have reported an error,
3047 thus it is unnecessary for repetition. */
3048 if (!find_rank (loc, idx, idx, true, &rank))
3049 return error_mark_node;
3052 error_at (loc, "rank of the array%'s index is greater than 1");
3053 return error_mark_node;
3056 if (TREE_TYPE (array) == error_mark_node
3057 || TREE_TYPE (idx) == error_mark_node)
3058 return error_mark_node;
3060 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3062 switch (TREE_CODE (array))
3066 tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3068 ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
3069 TREE_OPERAND (array, 0), value);
3070 SET_EXPR_LOCATION (ret, loc);
3075 ret = build_conditional_expr
3076 (loc, TREE_OPERAND (array, 0),
3077 cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3079 cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
3082 protected_set_expr_location (ret, loc);
3090 = convert_vector_to_pointer_for_subscript (loc, &array, idx);
3092 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
3096 warn_array_subscript_with_type_char (loc, idx);
3098 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
3100 if (complain & tf_error)
3101 error_at (loc, "array subscript is not an integer");
3102 return error_mark_node;
3105 /* Apply integral promotions *after* noticing character types.
3106 (It is unclear why we do these promotions -- the standard
3107 does not say that we should. In fact, the natural thing would
3108 seem to be to convert IDX to ptrdiff_t; we're performing
3109 pointer arithmetic.) */
3110 idx = cp_perform_integral_promotions (idx, complain);
3112 /* An array that is indexed by a non-constant
3113 cannot be stored in a register; we must be able to do
3114 address arithmetic on its address.
3115 Likewise an array of elements of variable size. */
3116 if (TREE_CODE (idx) != INTEGER_CST
3117 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
3118 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
3121 if (!cxx_mark_addressable (array))
3122 return error_mark_node;
3125 /* An array that is indexed by a constant value which is not within
3126 the array bounds cannot be stored in a register either; because we
3127 would get a crash in store_bit_field/extract_bit_field when trying
3128 to access a non-existent part of the register. */
3129 if (TREE_CODE (idx) == INTEGER_CST
3130 && TYPE_DOMAIN (TREE_TYPE (array))
3131 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
3133 if (!cxx_mark_addressable (array))
3134 return error_mark_node;
3137 /* Note in C++ it is valid to subscript a `register' array, since
3138 it is valid to take the address of something with that
3139 storage specification. */
3143 while (TREE_CODE (foo) == COMPONENT_REF)
3144 foo = TREE_OPERAND (foo, 0);
3145 if (VAR_P (foo) && DECL_REGISTER (foo)
3146 && (complain & tf_warning))
3147 warning_at (loc, OPT_Wextra,
3148 "subscripting array declared %<register%>");
3151 type = TREE_TYPE (TREE_TYPE (array));
3152 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
3153 /* Array ref is const/volatile if the array elements are
3154 or if the array is.. */
3155 TREE_READONLY (rval)
3156 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
3157 TREE_SIDE_EFFECTS (rval)
3158 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
3159 TREE_THIS_VOLATILE (rval)
3160 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
3161 ret = require_complete_type_sfinae (fold_if_not_in_template (rval),
3163 protected_set_expr_location (ret, loc);
3165 ret = non_lvalue_loc (loc, ret);
3170 tree ar = cp_default_conversion (array, complain);
3171 tree ind = cp_default_conversion (idx, complain);
3173 /* Put the integer in IND to simplify error checking. */
3174 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
3181 if (ar == error_mark_node || ind == error_mark_node)
3182 return error_mark_node;
3184 if (!TYPE_PTR_P (TREE_TYPE (ar)))
3186 if (complain & tf_error)
3187 error_at (loc, "subscripted value is neither array nor pointer");
3188 return error_mark_node;
3190 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
3192 if (complain & tf_error)
3193 error_at (loc, "array subscript is not an integer");
3194 return error_mark_node;
3197 warn_array_subscript_with_type_char (loc, idx);
3199 ret = cp_build_indirect_ref (cp_build_binary_op (input_location,
3204 protected_set_expr_location (ret, loc);
3206 ret = non_lvalue_loc (loc, ret);
3211 /* Entry point for Obj-C++. */
3214 build_array_ref (location_t loc, tree array, tree idx)
3216 return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
3219 /* Resolve a pointer to member function. INSTANCE is the object
3220 instance to use, if the member points to a virtual member.
3222 This used to avoid checking for virtual functions if basetype
3223 has no virtual functions, according to an earlier ANSI draft.
3224 With the final ISO C++ rules, such an optimization is
3225 incorrect: A pointer to a derived member can be static_cast
3226 to pointer-to-base-member, as long as the dynamic object
3227 later has the right member. So now we only do this optimization
3228 when we know the dynamic type of the object. */
3231 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
3232 tsubst_flags_t complain)
3234 if (TREE_CODE (function) == OFFSET_REF)
3235 function = TREE_OPERAND (function, 1);
3237 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
3239 tree idx, delta, e1, e2, e3, vtbl;
3241 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
3242 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
3244 tree instance_ptr = *instance_ptrptr;
3245 tree instance_save_expr = 0;
3246 if (instance_ptr == error_mark_node)
3248 if (TREE_CODE (function) == PTRMEM_CST)
3250 /* Extracting the function address from a pmf is only
3251 allowed with -Wno-pmf-conversions. It only works for
3253 e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
3254 e1 = convert (fntype, e1);
3259 if (complain & tf_error)
3260 error ("object missing in use of %qE", function);
3261 return error_mark_node;
3265 /* True if we know that the dynamic type of the object doesn't have
3266 virtual functions, so we can assume the PFN field is a pointer. */
3267 nonvirtual = (COMPLETE_TYPE_P (basetype)
3268 && !TYPE_POLYMORPHIC_P (basetype)
3269 && resolves_to_fixed_type_p (instance_ptr, 0));
3271 /* If we don't really have an object (i.e. in an ill-formed
3272 conversion from PMF to pointer), we can't resolve virtual
3273 functions anyway. */
3274 if (!nonvirtual && is_dummy_object (instance_ptr))
3277 if (TREE_SIDE_EFFECTS (instance_ptr))
3278 instance_ptr = instance_save_expr = save_expr (instance_ptr);
3280 if (TREE_SIDE_EFFECTS (function))
3281 function = save_expr (function);
3283 /* Start by extracting all the information from the PMF itself. */
3284 e3 = pfn_from_ptrmemfunc (function);
3285 delta = delta_from_ptrmemfunc (function);
3286 idx = build1 (NOP_EXPR, vtable_index_type, e3);
3287 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
3289 case ptrmemfunc_vbit_in_pfn:
3290 e1 = cp_build_binary_op (input_location,
3291 BIT_AND_EXPR, idx, integer_one_node,
3293 idx = cp_build_binary_op (input_location,
3294 MINUS_EXPR, idx, integer_one_node,
3296 if (idx == error_mark_node)
3297 return error_mark_node;
3300 case ptrmemfunc_vbit_in_delta:
3301 e1 = cp_build_binary_op (input_location,
3302 BIT_AND_EXPR, delta, integer_one_node,
3304 delta = cp_build_binary_op (input_location,
3305 RSHIFT_EXPR, delta, integer_one_node,
3307 if (delta == error_mark_node)
3308 return error_mark_node;
3315 if (e1 == error_mark_node)
3316 return error_mark_node;
3318 /* Convert down to the right base before using the instance. A
3319 special case is that in a pointer to member of class C, C may
3320 be incomplete. In that case, the function will of course be
3321 a member of C, and no conversion is required. In fact,
3322 lookup_base will fail in that case, because incomplete
3323 classes do not have BINFOs. */
3324 if (!same_type_ignoring_top_level_qualifiers_p
3325 (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
3327 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
3328 basetype, ba_check, NULL, complain);
3329 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
3331 if (instance_ptr == error_mark_node)
3332 return error_mark_node;
3334 /* ...and then the delta in the PMF. */
3335 instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
3337 /* Hand back the adjusted 'this' argument to our caller. */
3338 *instance_ptrptr = instance_ptr;
3341 /* Now just return the pointer. */
3344 /* Next extract the vtable pointer from the object. */
3345 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
3347 vtbl = cp_build_indirect_ref (vtbl, RO_NULL, complain);
3348 if (vtbl == error_mark_node)
3349 return error_mark_node;
3351 /* Finally, extract the function pointer from the vtable. */
3352 e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
3353 e2 = cp_build_indirect_ref (e2, RO_NULL, complain);
3354 if (e2 == error_mark_node)
3355 return error_mark_node;
3356 TREE_CONSTANT (e2) = 1;
3358 /* When using function descriptors, the address of the
3359 vtable entry is treated as a function pointer. */
3360 if (TARGET_VTABLE_USES_DESCRIPTORS)
3361 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
3362 cp_build_addr_expr (e2, complain));
3364 e2 = fold_convert (TREE_TYPE (e3), e2);
3365 e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
3366 if (e1 == error_mark_node)
3367 return error_mark_node;
3369 /* Make sure this doesn't get evaluated first inside one of the
3370 branches of the COND_EXPR. */
3371 if (instance_save_expr)
3372 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
3373 instance_save_expr, e1);
3380 /* Used by the C-common bits. */
3382 build_function_call (location_t /*loc*/,
3383 tree function, tree params)
3385 return cp_build_function_call (function, params, tf_warning_or_error);
3388 /* Used by the C-common bits. */
3390 build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/,
3391 tree function, vec<tree, va_gc> *params,
3392 vec<tree, va_gc> * /*origtypes*/)
3394 vec<tree, va_gc> *orig_params = params;
3395 tree ret = cp_build_function_call_vec (function, ¶ms,
3396 tf_warning_or_error);
3398 /* cp_build_function_call_vec can reallocate PARAMS by adding
3399 default arguments. That should never happen here. Verify
3401 gcc_assert (params == orig_params);
3406 /* Build a function call using a tree list of arguments. */
3409 cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
3411 vec<tree, va_gc> *vec;
3414 vec = make_tree_vector ();
3415 for (; params != NULL_TREE; params = TREE_CHAIN (params))
3416 vec_safe_push (vec, TREE_VALUE (params));
3417 ret = cp_build_function_call_vec (function, &vec, complain);
3418 release_tree_vector (vec);
3422 /* Build a function call using varargs. */
3425 cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
3427 vec<tree, va_gc> *vec;
3431 vec = make_tree_vector ();
3432 va_start (args, complain);
3433 for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
3434 vec_safe_push (vec, t);
3436 ret = cp_build_function_call_vec (function, &vec, complain);
3437 release_tree_vector (vec);
3441 /* Build a function call using a vector of arguments. PARAMS may be
3442 NULL if there are no parameters. This changes the contents of
3446 cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
3447 tsubst_flags_t complain)
3449 tree fntype, fndecl;
3451 tree original = function;
3455 vec<tree, va_gc> *allocated = NULL;
3458 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3459 expressions, like those used for ObjC messenger dispatches. */
3460 if (params != NULL && !vec_safe_is_empty (*params))
3461 function = objc_rewrite_function_call (function, (**params)[0]);
3463 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3464 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3465 if (TREE_CODE (function) == NOP_EXPR
3466 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3467 function = TREE_OPERAND (function, 0);
3469 if (TREE_CODE (function) == FUNCTION_DECL)
3471 mark_used (function);
3474 /* Convert anything with function type to a pointer-to-function. */
3475 if (DECL_MAIN_P (function))
3477 if (complain & tf_error)
3478 pedwarn (input_location, OPT_Wpedantic,
3479 "ISO C++ forbids calling %<::main%> from within program");
3481 return error_mark_node;
3483 function = build_addr_func (function, complain);
3489 function = build_addr_func (function, complain);
3492 if (function == error_mark_node)
3493 return error_mark_node;
3495 fntype = TREE_TYPE (function);
3497 if (TYPE_PTRMEMFUNC_P (fntype))
3499 if (complain & tf_error)
3500 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3501 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3502 original, original);
3503 return error_mark_node;
3506 is_method = (TYPE_PTR_P (fntype)
3507 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3509 if (!(TYPE_PTRFN_P (fntype)
3511 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3513 if (complain & tf_error)
3515 if (!flag_diagnostics_show_caret)
3516 error_at (input_location,
3517 "%qE cannot be used as a function", original);
3518 else if (DECL_P (original))
3519 error_at (input_location,
3520 "%qD cannot be used as a function", original);
3522 error_at (input_location,
3523 "expression cannot be used as a function");
3526 return error_mark_node;
3529 /* fntype now gets the type of function pointed to. */
3530 fntype = TREE_TYPE (fntype);
3531 parm_types = TYPE_ARG_TYPES (fntype);
3535 allocated = make_tree_vector ();
3536 params = &allocated;
3539 nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
3542 return error_mark_node;
3544 argarray = (*params)->address ();
3546 /* Check for errors in format strings and inappropriately
3548 check_function_arguments (fntype, nargs, argarray);
3550 ret = build_cxx_call (function, nargs, argarray, complain);
3552 if (allocated != NULL)
3553 release_tree_vector (allocated);
3558 /* Subroutine of convert_arguments.
3559 Warn about wrong number of args are genereted. */
3562 warn_args_num (location_t loc, tree fndecl, bool too_many_p)
3566 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3568 if (DECL_NAME (fndecl) == NULL_TREE
3569 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3572 ? G_("too many arguments to constructor %q#D")
3573 : G_("too few arguments to constructor %q#D"),
3578 ? G_("too many arguments to member function %q#D")
3579 : G_("too few arguments to member function %q#D"),
3585 ? G_("too many arguments to function %q#D")
3586 : G_("too few arguments to function %q#D"),
3588 inform (DECL_SOURCE_LOCATION (fndecl),
3593 if (c_dialect_objc () && objc_message_selector ())
3596 ? G_("too many arguments to method %q#D")
3597 : G_("too few arguments to method %q#D"),
3598 objc_message_selector ());
3600 error_at (loc, too_many_p ? G_("too many arguments to function")
3601 : G_("too few arguments to function"));
3605 /* Convert the actual parameter expressions in the list VALUES to the
3606 types in the list TYPELIST. The converted expressions are stored
3607 back in the VALUES vector.
3608 If parmdecls is exhausted, or when an element has NULL as its type,
3609 perform the default conversions.
3611 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3613 This is also where warnings about wrong number of args are generated.
3615 Returns the actual number of arguments processed (which might be less
3616 than the length of the vector), or -1 on error.
3618 In C++, unspecified trailing parameters can be filled in with their
3619 default arguments, if such were specified. Do so here. */
3622 convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
3623 int flags, tsubst_flags_t complain)
3628 /* Argument passing is always copy-initialization. */
3629 flags |= LOOKUP_ONLYCONVERTING;
3631 for (i = 0, typetail = typelist;
3632 i < vec_safe_length (*values);
3635 tree type = typetail ? TREE_VALUE (typetail) : 0;
3636 tree val = (**values)[i];
3638 if (val == error_mark_node || type == error_mark_node)
3641 if (type == void_type_node)
3643 if (complain & tf_error)
3645 warn_args_num (input_location, fndecl, /*too_many_p=*/true);
3652 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3653 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3654 if (TREE_CODE (val) == NOP_EXPR
3655 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3656 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3657 val = TREE_OPERAND (val, 0);
3659 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3661 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3662 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3663 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3664 val = decay_conversion (val, complain);
3667 if (val == error_mark_node)
3672 /* Formal parm type is specified by a function prototype. */
3675 if (!COMPLETE_TYPE_P (complete_type (type)))
3677 if (complain & tf_error)
3680 error ("parameter %P of %qD has incomplete type %qT",
3683 error ("parameter %P has incomplete type %qT", i, type);
3685 parmval = error_mark_node;
3689 parmval = convert_for_initialization
3690 (NULL_TREE, type, val, flags,
3691 ICR_ARGPASS, fndecl, i, complain);
3692 parmval = convert_for_arg_passing (type, parmval, complain);
3695 if (parmval == error_mark_node)
3698 (**values)[i] = parmval;
3702 if (fndecl && magic_varargs_p (fndecl))
3703 /* Don't do ellipsis conversion for __built_in_constant_p
3704 as this will result in spurious errors for non-trivial
3706 val = require_complete_type_sfinae (val, complain);
3708 val = convert_arg_to_ellipsis (val, complain);
3710 (**values)[i] = val;
3714 typetail = TREE_CHAIN (typetail);
3717 if (typetail != 0 && typetail != void_list_node)
3719 /* See if there are default arguments that can be used. Because
3720 we hold default arguments in the FUNCTION_TYPE (which is so
3721 wrong), we can see default parameters here from deduced
3722 contexts (and via typeof) for indirect function calls.
3723 Fortunately we know whether we have a function decl to
3724 provide default arguments in a language conformant
3726 if (fndecl && TREE_PURPOSE (typetail)
3727 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
3729 for (; typetail != void_list_node; ++i)
3732 = convert_default_arg (TREE_VALUE (typetail),
3733 TREE_PURPOSE (typetail),
3734 fndecl, i, complain);
3736 if (parmval == error_mark_node)
3739 vec_safe_push (*values, parmval);
3740 typetail = TREE_CHAIN (typetail);
3741 /* ends with `...'. */
3742 if (typetail == NULL_TREE)
3748 if (complain & tf_error)
3749 warn_args_num (input_location, fndecl, /*too_many_p=*/false);
3757 /* Build a binary-operation expression, after performing default
3758 conversions on the operands. CODE is the kind of expression to
3759 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
3760 are the tree codes which correspond to ARG1 and ARG2 when issuing
3761 warnings about possibly misplaced parentheses. They may differ
3762 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
3763 folding (e.g., if the parser sees "a | 1 + 1", it may call this
3764 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
3765 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
3766 ARG2_CODE as ERROR_MARK. */
3769 build_x_binary_op (location_t loc, enum tree_code code, tree arg1,
3770 enum tree_code arg1_code, tree arg2,
3771 enum tree_code arg2_code, tree *overload,
3772 tsubst_flags_t complain)
3781 if (processing_template_decl)
3783 if (type_dependent_expression_p (arg1)
3784 || type_dependent_expression_p (arg2))
3785 return build_min_nt_loc (loc, code, arg1, arg2);
3786 arg1 = build_non_dependent_expr (arg1);
3787 arg2 = build_non_dependent_expr (arg2);
3790 if (code == DOTSTAR_EXPR)
3791 expr = build_m_component_ref (arg1, arg2, complain);
3793 expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
3794 overload, complain);
3796 /* Check for cases such as x+y<<z which users are likely to
3797 misinterpret. But don't warn about obj << x + y, since that is a
3798 common idiom for I/O. */
3799 if (warn_parentheses
3800 && (complain & tf_warning)
3801 && !processing_template_decl
3802 && !error_operand_p (arg1)
3803 && !error_operand_p (arg2)
3804 && (code != LSHIFT_EXPR
3805 || !CLASS_TYPE_P (TREE_TYPE (arg1))))
3806 warn_about_parentheses (loc, code, arg1_code, orig_arg1,
3807 arg2_code, orig_arg2);
3809 if (processing_template_decl && expr != error_mark_node)
3810 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
3815 /* Build and return an ARRAY_REF expression. */
3818 build_x_array_ref (location_t loc, tree arg1, tree arg2,
3819 tsubst_flags_t complain)
3821 tree orig_arg1 = arg1;
3822 tree orig_arg2 = arg2;
3825 if (processing_template_decl)
3827 if (type_dependent_expression_p (arg1)
3828 || type_dependent_expression_p (arg2))
3829 return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
3830 NULL_TREE, NULL_TREE);
3831 arg1 = build_non_dependent_expr (arg1);
3832 arg2 = build_non_dependent_expr (arg2);
3835 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
3836 NULL_TREE, /*overload=*/NULL, complain);
3838 if (processing_template_decl && expr != error_mark_node)
3839 return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
3840 NULL_TREE, NULL_TREE);
3844 /* Return whether OP is an expression of enum type cast to integer
3845 type. In C++ even unsigned enum types are cast to signed integer
3846 types. We do not want to issue warnings about comparisons between
3847 signed and unsigned types when one of the types is an enum type.
3848 Those warnings are always false positives in practice. */
3851 enum_cast_to_int (tree op)
3853 if (CONVERT_EXPR_P (op)
3854 && TREE_TYPE (op) == integer_type_node
3855 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
3856 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
3859 /* The cast may have been pushed into a COND_EXPR. */
3860 if (TREE_CODE (op) == COND_EXPR)
3861 return (enum_cast_to_int (TREE_OPERAND (op, 1))
3862 || enum_cast_to_int (TREE_OPERAND (op, 2)));
3867 /* For the c-common bits. */
3869 build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
3872 return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
3876 /* Build a binary-operation expression without default conversions.
3877 CODE is the kind of expression to build.
3878 LOCATION is the location_t of the operator in the source code.
3879 This function differs from `build' in several ways:
3880 the data type of the result is computed and recorded in it,
3881 warnings are generated if arg data types are invalid,
3882 special handling for addition and subtraction of pointers is known,
3883 and some optimization is done (operations on narrow ints
3884 are done in the narrower type when that gives the same result).
3885 Constant folding is also done before the result is returned.
3887 Note that the operands will never have enumeral types
3888 because either they have just had the default conversions performed
3889 or they have both just been converted to some other type in which
3890 the arithmetic is to be done.
3892 C++: must do special pointer arithmetic when implementing
3893 multiple inheritance, and deal with pointer to member functions. */
3896 cp_build_binary_op (location_t location,
3897 enum tree_code code, tree orig_op0, tree orig_op1,
3898 tsubst_flags_t complain)
3901 enum tree_code code0, code1;
3903 const char *invalid_op_diag;
3905 /* Expression code to give to the expression when it is built.
3906 Normally this is CODE, which is what the caller asked for,
3907 but in some special cases we change it. */
3908 enum tree_code resultcode = code;
3910 /* Data type in which the computation is to be performed.
3911 In the simplest cases this is the common type of the arguments. */
3912 tree result_type = NULL;
3914 /* Nonzero means operands have already been type-converted
3915 in whatever way is necessary.
3916 Zero means they need to be converted to RESULT_TYPE. */
3919 /* Nonzero means create the expression with this type, rather than
3921 tree build_type = 0;
3923 /* Nonzero means after finally constructing the expression
3924 convert it to this type. */
3925 tree final_type = 0;
3928 tree orig_type = NULL;
3930 /* Nonzero if this is an operation like MIN or MAX which can
3931 safely be computed in short if both args are promoted shorts.
3932 Also implies COMMON.
3933 -1 indicates a bitwise operation; this makes a difference
3934 in the exact conditions for when it is safe to do the operation
3935 in a narrower mode. */
3938 /* Nonzero if this is a comparison operation;
3939 if both args are promoted shorts, compare the original shorts.
3940 Also implies COMMON. */
3941 int short_compare = 0;
3943 /* Nonzero means set RESULT_TYPE to the common type of the args. */
3946 /* True if both operands have arithmetic type. */
3947 bool arithmetic_types_p;
3949 /* Apply default conversions. */
3953 /* Remember whether we're doing / or %. */
3954 bool doing_div_or_mod = false;
3956 /* Remember whether we're doing << or >>. */
3957 bool doing_shift = false;
3959 /* Tree holding instrumentation expression. */
3960 tree instrument_expr = NULL;
3962 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
3963 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
3964 || code == TRUTH_XOR_EXPR)
3966 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
3967 op0 = decay_conversion (op0, complain);
3968 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
3969 op1 = decay_conversion (op1, complain);
3973 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
3974 op0 = cp_default_conversion (op0, complain);
3975 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
3976 op1 = cp_default_conversion (op1, complain);
3979 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3980 STRIP_TYPE_NOPS (op0);
3981 STRIP_TYPE_NOPS (op1);
3983 /* DTRT if one side is an overloaded function, but complain about it. */
3984 if (type_unknown_p (op0))
3986 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
3987 if (t != error_mark_node)
3989 if (complain & tf_error)
3990 permerror (input_location, "assuming cast to type %qT from overloaded function",
3995 if (type_unknown_p (op1))
3997 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
3998 if (t != error_mark_node)
4000 if (complain & tf_error)
4001 permerror (input_location, "assuming cast to type %qT from overloaded function",
4007 type0 = TREE_TYPE (op0);
4008 type1 = TREE_TYPE (op1);
4010 /* The expression codes of the data types of the arguments tell us
4011 whether the arguments are integers, floating, pointers, etc. */
4012 code0 = TREE_CODE (type0);
4013 code1 = TREE_CODE (type1);
4015 /* If an error was already reported for one of the arguments,
4016 avoid reporting another error. */
4017 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
4018 return error_mark_node;
4020 if ((invalid_op_diag
4021 = targetm.invalid_binary_op (code, type0, type1)))
4023 if (complain & tf_error)
4024 error (invalid_op_diag);
4025 return error_mark_node;
4028 /* Issue warnings about peculiar, but valid, uses of NULL. */
4029 if ((orig_op0 == null_node || orig_op1 == null_node)
4030 /* It's reasonable to use pointer values as operands of &&
4031 and ||, so NULL is no exception. */
4032 && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
4033 && ( /* Both are NULL (or 0) and the operation was not a
4034 comparison or a pointer subtraction. */
4035 (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
4036 && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
4037 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
4038 || (!null_ptr_cst_p (orig_op0)
4039 && !TYPE_PTR_OR_PTRMEM_P (type0))
4040 || (!null_ptr_cst_p (orig_op1)
4041 && !TYPE_PTR_OR_PTRMEM_P (type1)))
4042 && (complain & tf_warning))
4044 source_location loc =
4045 expansion_point_location_if_in_system_header (input_location);
4047 warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
4050 /* In case when one of the operands of the binary operation is
4051 a vector and another is a scalar -- convert scalar to vector. */
4052 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
4054 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
4055 complain & tf_error);
4057 switch (convert_flag)
4060 return error_mark_node;
4063 op0 = convert (TREE_TYPE (type1), op0);
4064 op0 = save_expr (op0);
4065 op0 = build_vector_from_val (type1, op0);
4066 type0 = TREE_TYPE (op0);
4067 code0 = TREE_CODE (type0);
4073 op1 = convert (TREE_TYPE (type0), op1);
4074 op1 = save_expr (op1);
4075 op1 = build_vector_from_val (type0, op1);
4076 type1 = TREE_TYPE (op1);
4077 code1 = TREE_CODE (type1);
4089 /* Subtraction of two similar pointers.
4090 We must subtract them as integers, then divide by object size. */
4091 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
4092 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4094 return pointer_diff (op0, op1, common_pointer_type (type0, type1),
4096 /* In all other cases except pointer - int, the usual arithmetic
4098 else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
4103 /* The pointer - int case is just like pointer + int; fall
4106 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
4107 && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
4111 ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
4112 int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
4113 if (processing_template_decl)
4115 result_type = TREE_TYPE (ptr_operand);
4118 return cp_pointer_int_sum (code,
4130 case TRUNC_DIV_EXPR:
4132 case FLOOR_DIV_EXPR:
4133 case ROUND_DIV_EXPR:
4134 case EXACT_DIV_EXPR:
4135 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4136 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
4137 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4138 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
4140 enum tree_code tcode0 = code0, tcode1 = code1;
4141 tree cop1 = fold_non_dependent_expr (op1);
4142 doing_div_or_mod = true;
4143 warn_for_div_by_zero (location, cop1);
4145 if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
4146 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
4147 if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
4148 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
4150 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
4151 resultcode = RDIV_EXPR;
4153 /* When dividing two signed integers, we have to promote to int.
4154 unless we divide by a constant != -1. Note that default
4155 conversion will have been performed on the operands at this
4156 point, so we have to dig out the original type to find out if
4158 shorten = ((TREE_CODE (op0) == NOP_EXPR
4159 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4160 || (TREE_CODE (op1) == INTEGER_CST
4161 && ! integer_all_onesp (op1)));
4170 if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4171 || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4172 && !VECTOR_FLOAT_TYPE_P (type0)
4173 && !VECTOR_FLOAT_TYPE_P (type1)))
4177 case TRUNC_MOD_EXPR:
4178 case FLOOR_MOD_EXPR:
4180 tree cop1 = fold_non_dependent_expr (op1);
4181 doing_div_or_mod = true;
4182 warn_for_div_by_zero (location, cop1);
4185 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4186 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4187 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
4189 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4191 /* Although it would be tempting to shorten always here, that loses
4192 on some targets, since the modulo instruction is undefined if the
4193 quotient can't be represented in the computation mode. We shorten
4194 only if unsigned or if dividing by something we know != -1. */
4195 shorten = ((TREE_CODE (op0) == NOP_EXPR
4196 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4197 || (TREE_CODE (op1) == INTEGER_CST
4198 && ! integer_all_onesp (op1)));
4203 case TRUTH_ANDIF_EXPR:
4204 case TRUTH_ORIF_EXPR:
4205 case TRUTH_AND_EXPR:
4207 if (!VECTOR_TYPE_P (type0) && VECTOR_TYPE_P (type1))
4209 if (!COMPARISON_CLASS_P (op1))
4210 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4211 build_zero_cst (type1), complain);
4212 if (code == TRUTH_ANDIF_EXPR)
4214 tree z = build_zero_cst (TREE_TYPE (op1));
4215 return build_conditional_expr (location, op0, op1, z, complain);
4217 else if (code == TRUTH_ORIF_EXPR)
4219 tree m1 = build_all_ones_cst (TREE_TYPE (op1));
4220 return build_conditional_expr (location, op0, m1, op1, complain);
4225 if (VECTOR_TYPE_P (type0))
4227 if (!COMPARISON_CLASS_P (op0))
4228 op0 = cp_build_binary_op (EXPR_LOCATION (op0), NE_EXPR, op0,
4229 build_zero_cst (type0), complain);
4230 if (!VECTOR_TYPE_P (type1))
4232 tree m1 = build_all_ones_cst (TREE_TYPE (op0));
4233 tree z = build_zero_cst (TREE_TYPE (op0));
4234 op1 = build_conditional_expr (location, op1, z, m1, complain);
4236 else if (!COMPARISON_CLASS_P (op1))
4237 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4238 build_zero_cst (type1), complain);
4240 if (code == TRUTH_ANDIF_EXPR)
4241 code = BIT_AND_EXPR;
4242 else if (code == TRUTH_ORIF_EXPR)
4243 code = BIT_IOR_EXPR;
4247 return cp_build_binary_op (location, code, op0, op1, complain);
4250 result_type = boolean_type_node;
4253 /* Shift operations: result has same type as first operand;
4254 always convert second operand to int.
4255 Also set SHORT_SHIFT if shifting rightward. */
4258 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4259 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4261 result_type = type0;
4264 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4265 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4266 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4267 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4269 result_type = type0;
4272 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4274 tree const_op1 = fold_non_dependent_expr (op1);
4275 if (TREE_CODE (const_op1) != INTEGER_CST)
4277 result_type = type0;
4279 if (TREE_CODE (const_op1) == INTEGER_CST)
4281 if (tree_int_cst_lt (const_op1, integer_zero_node))
4283 if ((complain & tf_warning)
4284 && c_inhibit_evaluation_warnings == 0)
4285 warning (OPT_Wshift_count_negative,
4286 "right shift count is negative");
4290 if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
4291 && (complain & tf_warning)
4292 && c_inhibit_evaluation_warnings == 0)
4293 warning (OPT_Wshift_count_overflow,
4294 "right shift count >= width of type");
4297 /* Avoid converting op1 to result_type later. */
4303 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4304 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4306 result_type = type0;
4309 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4310 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4311 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4312 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4314 result_type = type0;
4317 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4319 tree const_op1 = fold_non_dependent_expr (op1);
4320 if (TREE_CODE (const_op1) != INTEGER_CST)
4322 result_type = type0;
4324 if (TREE_CODE (const_op1) == INTEGER_CST)
4326 if (tree_int_cst_lt (const_op1, integer_zero_node))
4328 if ((complain & tf_warning)
4329 && c_inhibit_evaluation_warnings == 0)
4330 warning (OPT_Wshift_count_negative,
4331 "left shift count is negative");
4333 else if (compare_tree_int (const_op1,
4334 TYPE_PRECISION (type0)) >= 0)
4336 if ((complain & tf_warning)
4337 && c_inhibit_evaluation_warnings == 0)
4338 warning (OPT_Wshift_count_overflow,
4339 "left shift count >= width of type");
4342 /* Avoid converting op1 to result_type later. */
4349 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4351 result_type = type0;
4352 if (TREE_CODE (op1) == INTEGER_CST)
4354 if (tree_int_cst_lt (op1, integer_zero_node))
4356 if (complain & tf_warning)
4357 warning (0, (code == LROTATE_EXPR)
4358 ? G_("left rotate count is negative")
4359 : G_("right rotate count is negative"));
4361 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
4363 if (complain & tf_warning)
4364 warning (0, (code == LROTATE_EXPR)
4365 ? G_("left rotate count >= width of type")
4366 : G_("right rotate count >= width of type"));
4369 /* Convert the shift-count to an integer, regardless of
4370 size of value being shifted. */
4371 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4372 op1 = cp_convert (integer_type_node, op1, complain);
4378 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4379 goto vector_compare;
4380 if ((complain & tf_warning)
4381 && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
4382 warning (OPT_Wfloat_equal,
4383 "comparing floating point with == or != is unsafe");
4384 if ((complain & tf_warning)
4385 && ((TREE_CODE (orig_op0) == STRING_CST && !integer_zerop (op1))
4386 || (TREE_CODE (orig_op1) == STRING_CST && !integer_zerop (op0))))
4387 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
4389 build_type = boolean_type_node;
4390 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4391 || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
4392 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4393 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
4395 else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
4396 && null_ptr_cst_p (op1))
4397 /* Handle, eg, (void*)0 (c++/43906), and more. */
4398 || (code0 == POINTER_TYPE
4399 && TYPE_PTR_P (type1) && integer_zerop (op1)))
4401 if (TYPE_PTR_P (type1))
4402 result_type = composite_pointer_type (type0, type1, op0, op1,
4403 CPO_COMPARISON, complain);
4405 result_type = type0;
4407 if (TREE_CODE (op0) == ADDR_EXPR
4408 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
4410 if ((complain & tf_warning)
4411 && c_inhibit_evaluation_warnings == 0
4412 && !TREE_NO_WARNING (op0))
4413 warning (OPT_Waddress, "the address of %qD will never be NULL",
4414 TREE_OPERAND (op0, 0));
4417 else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
4418 && null_ptr_cst_p (op0))
4419 /* Handle, eg, (void*)0 (c++/43906), and more. */
4420 || (code1 == POINTER_TYPE
4421 && TYPE_PTR_P (type0) && integer_zerop (op0)))
4423 if (TYPE_PTR_P (type0))
4424 result_type = composite_pointer_type (type0, type1, op0, op1,
4425 CPO_COMPARISON, complain);
4427 result_type = type1;
4429 if (TREE_CODE (op1) == ADDR_EXPR
4430 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
4432 if ((complain & tf_warning)
4433 && c_inhibit_evaluation_warnings == 0
4434 && !TREE_NO_WARNING (op1))
4435 warning (OPT_Waddress, "the address of %qD will never be NULL",
4436 TREE_OPERAND (op1, 0));
4439 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4440 || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
4441 result_type = composite_pointer_type (type0, type1, op0, op1,
4442 CPO_COMPARISON, complain);
4443 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4444 /* One of the operands must be of nullptr_t type. */
4445 result_type = TREE_TYPE (nullptr_node);
4446 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4448 result_type = type0;
4449 if (complain & tf_error)
4450 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4452 return error_mark_node;
4454 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4456 result_type = type1;
4457 if (complain & tf_error)
4458 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4460 return error_mark_node;
4462 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
4464 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4465 == ptrmemfunc_vbit_in_delta)
4467 tree pfn0, delta0, e1, e2;
4469 if (TREE_SIDE_EFFECTS (op0))
4470 op0 = save_expr (op0);
4472 pfn0 = pfn_from_ptrmemfunc (op0);
4473 delta0 = delta_from_ptrmemfunc (op0);
4474 e1 = cp_build_binary_op (location,
4477 build_zero_cst (TREE_TYPE (pfn0)),
4479 e2 = cp_build_binary_op (location,
4485 if (complain & tf_warning)
4486 maybe_warn_zero_as_null_pointer_constant (op1, input_location);
4488 e2 = cp_build_binary_op (location,
4489 EQ_EXPR, e2, integer_zero_node,
4491 op0 = cp_build_binary_op (location,
4492 TRUTH_ANDIF_EXPR, e1, e2,
4494 op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
4498 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
4499 op1 = cp_convert (TREE_TYPE (op0), op1, complain);
4501 result_type = TREE_TYPE (op0);
4503 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
4504 return cp_build_binary_op (location, code, op1, op0, complain);
4505 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
4508 /* E will be the final comparison. */
4510 /* E1 and E2 are for scratch. */
4518 type = composite_pointer_type (type0, type1, op0, op1,
4519 CPO_COMPARISON, complain);
4521 if (!same_type_p (TREE_TYPE (op0), type))
4522 op0 = cp_convert_and_check (type, op0, complain);
4523 if (!same_type_p (TREE_TYPE (op1), type))
4524 op1 = cp_convert_and_check (type, op1, complain);
4526 if (op0 == error_mark_node || op1 == error_mark_node)
4527 return error_mark_node;
4529 if (TREE_SIDE_EFFECTS (op0))
4530 op0 = save_expr (op0);
4531 if (TREE_SIDE_EFFECTS (op1))
4532 op1 = save_expr (op1);
4534 pfn0 = pfn_from_ptrmemfunc (op0);
4535 /* Avoid -Waddress warnings (c++/64877). */
4536 if (TREE_CODE (pfn0) == ADDR_EXPR)
4537 TREE_NO_WARNING (pfn0) = 1;
4538 pfn1 = pfn_from_ptrmemfunc (op1);
4539 delta0 = delta_from_ptrmemfunc (op0);
4540 delta1 = delta_from_ptrmemfunc (op1);
4541 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4542 == ptrmemfunc_vbit_in_delta)
4547 && ((op0.delta == op1.delta)
4548 || (!op0.pfn && op0.delta & 1 == 0
4549 && op1.delta & 1 == 0))
4551 The reason for the `!op0.pfn' bit is that a NULL
4552 pointer-to-member is any member with a zero PFN and
4553 LSB of the DELTA field is 0. */
4555 e1 = cp_build_binary_op (location, BIT_AND_EXPR,
4559 e1 = cp_build_binary_op (location,
4560 EQ_EXPR, e1, integer_zero_node,
4562 e2 = cp_build_binary_op (location, BIT_AND_EXPR,
4566 e2 = cp_build_binary_op (location,
4567 EQ_EXPR, e2, integer_zero_node,
4569 e1 = cp_build_binary_op (location,
4570 TRUTH_ANDIF_EXPR, e2, e1,
4572 e2 = cp_build_binary_op (location, EQ_EXPR,
4574 build_zero_cst (TREE_TYPE (pfn0)),
4576 e2 = cp_build_binary_op (location,
4577 TRUTH_ANDIF_EXPR, e2, e1, complain);
4578 e1 = cp_build_binary_op (location,
4579 EQ_EXPR, delta0, delta1, complain);
4580 e1 = cp_build_binary_op (location,
4581 TRUTH_ORIF_EXPR, e1, e2, complain);
4588 && (!op0.pfn || op0.delta == op1.delta))
4590 The reason for the `!op0.pfn' bit is that a NULL
4591 pointer-to-member is any member with a zero PFN; the
4592 DELTA field is unspecified. */
4594 e1 = cp_build_binary_op (location,
4595 EQ_EXPR, delta0, delta1, complain);
4596 e2 = cp_build_binary_op (location,
4599 build_zero_cst (TREE_TYPE (pfn0)),
4601 e1 = cp_build_binary_op (location,
4602 TRUTH_ORIF_EXPR, e1, e2, complain);
4604 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
4605 e = cp_build_binary_op (location,
4606 TRUTH_ANDIF_EXPR, e2, e1, complain);
4607 if (code == EQ_EXPR)
4609 return cp_build_binary_op (location,
4610 EQ_EXPR, e, integer_zero_node, complain);
4614 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
4615 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
4617 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
4618 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
4626 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
4627 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
4629 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4630 result_type = composite_pointer_type (type0, type1, op0, op1,
4631 CPO_COMPARISON, complain);
4638 if (TREE_CODE (orig_op0) == STRING_CST
4639 || TREE_CODE (orig_op1) == STRING_CST)
4641 if (complain & tf_warning)
4642 warning (OPT_Waddress, "comparison with string literal results in unspecified behaviour");
4645 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4649 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4651 && !vector_types_compatible_elements_p (type0, type1))
4653 if (complain & tf_error)
4655 error_at (location, "comparing vectors with different "
4657 inform (location, "operand types are %qT and %qT",
4660 return error_mark_node;
4663 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
4665 if (complain & tf_error)
4667 error_at (location, "comparing vectors with different "
4668 "number of elements");
4669 inform (location, "operand types are %qT and %qT",
4672 return error_mark_node;
4675 /* Always construct signed integer vector type. */
4676 intt = c_common_type_for_size (GET_MODE_BITSIZE
4677 (TYPE_MODE (TREE_TYPE (type0))), 0);
4680 if (complain & tf_error)
4681 error_at (location, "could not find an integer type "
4682 "of the same size as %qT", TREE_TYPE (type0));
4683 return error_mark_node;
4685 result_type = build_opaque_vector_type (intt,
4686 TYPE_VECTOR_SUBPARTS (type0));
4690 build_type = boolean_type_node;
4691 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4692 || code0 == ENUMERAL_TYPE)
4693 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4694 || code1 == ENUMERAL_TYPE))
4696 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4697 result_type = composite_pointer_type (type0, type1, op0, op1,
4698 CPO_COMPARISON, complain);
4699 else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
4701 result_type = type0;
4702 if (extra_warnings && (complain & tf_warning))
4703 warning (OPT_Wextra,
4704 "ordered comparison of pointer with integer zero");
4706 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
4708 result_type = type1;
4709 if (extra_warnings && (complain & tf_warning))
4710 warning (OPT_Wextra,
4711 "ordered comparison of pointer with integer zero");
4713 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4714 /* One of the operands must be of nullptr_t type. */
4715 result_type = TREE_TYPE (nullptr_node);
4716 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4718 result_type = type0;
4719 if (complain & tf_error)
4720 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4722 return error_mark_node;
4724 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4726 result_type = type1;
4727 if (complain & tf_error)
4728 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4730 return error_mark_node;
4734 case UNORDERED_EXPR:
4741 build_type = integer_type_node;
4742 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
4744 if (complain & tf_error)
4745 error ("unordered comparison on non-floating point argument");
4746 return error_mark_node;
4755 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
4756 || code0 == ENUMERAL_TYPE)
4757 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4758 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
4759 arithmetic_types_p = 1;
4762 arithmetic_types_p = 0;
4763 /* Vector arithmetic is only allowed when both sides are vectors. */
4764 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4766 if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
4767 || !vector_types_compatible_elements_p (type0, type1))
4769 if (complain & tf_error)
4770 binary_op_error (location, code, type0, type1);
4771 return error_mark_node;
4773 arithmetic_types_p = 1;
4776 /* Determine the RESULT_TYPE, if it is not already known. */
4778 && arithmetic_types_p
4779 && (shorten || common || short_compare))
4781 result_type = cp_common_type (type0, type1);
4782 if (complain & tf_warning)
4783 do_warn_double_promotion (result_type, type0, type1,
4784 "implicit conversion from %qT to %qT "
4785 "to match other operand of binary "
4792 if (complain & tf_error)
4793 error ("invalid operands of types %qT and %qT to binary %qO",
4794 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
4795 return error_mark_node;
4798 /* If we're in a template, the only thing we need to know is the
4800 if (processing_template_decl)
4802 /* Since the middle-end checks the type when doing a build2, we
4803 need to build the tree in pieces. This built tree will never
4804 get out of the front-end as we replace it when instantiating
4806 tree tmp = build2 (resultcode,
4807 build_type ? build_type : result_type,
4809 TREE_OPERAND (tmp, 0) = op0;
4813 if (arithmetic_types_p)
4815 bool first_complex = (code0 == COMPLEX_TYPE);
4816 bool second_complex = (code1 == COMPLEX_TYPE);
4817 int none_complex = (!first_complex && !second_complex);
4819 /* Adapted from patch for c/24581. */
4820 if (first_complex != second_complex
4821 && (code == PLUS_EXPR
4822 || code == MINUS_EXPR
4823 || code == MULT_EXPR
4824 || (code == TRUNC_DIV_EXPR && first_complex))
4825 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
4826 && flag_signed_zeros)
4828 /* An operation on mixed real/complex operands must be
4829 handled specially, but the language-independent code can
4830 more easily optimize the plain complex arithmetic if
4831 -fno-signed-zeros. */
4832 tree real_type = TREE_TYPE (result_type);
4836 if (TREE_TYPE (op0) != result_type)
4837 op0 = cp_convert_and_check (result_type, op0, complain);
4838 if (TREE_TYPE (op1) != real_type)
4839 op1 = cp_convert_and_check (real_type, op1, complain);
4843 if (TREE_TYPE (op0) != real_type)
4844 op0 = cp_convert_and_check (real_type, op0, complain);
4845 if (TREE_TYPE (op1) != result_type)
4846 op1 = cp_convert_and_check (result_type, op1, complain);
4848 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
4849 return error_mark_node;
4852 op0 = save_expr (op0);
4853 real = cp_build_unary_op (REALPART_EXPR, op0, 1, complain);
4854 imag = cp_build_unary_op (IMAGPART_EXPR, op0, 1, complain);
4858 case TRUNC_DIV_EXPR:
4859 op1 = save_expr (op1);
4860 imag = build2 (resultcode, real_type, imag, op1);
4864 real = build2 (resultcode, real_type, real, op1);
4872 op1 = save_expr (op1);
4873 real = cp_build_unary_op (REALPART_EXPR, op1, 1, complain);
4874 imag = cp_build_unary_op (IMAGPART_EXPR, op1, 1, complain);
4878 op0 = save_expr (op0);
4879 imag = build2 (resultcode, real_type, op0, imag);
4882 real = build2 (resultcode, real_type, op0, real);
4885 real = build2 (resultcode, real_type, op0, real);
4886 imag = build1 (NEGATE_EXPR, real_type, imag);
4892 real = fold_if_not_in_template (real);
4893 imag = fold_if_not_in_template (imag);
4894 result = build2 (COMPLEX_EXPR, result_type, real, imag);
4895 result = fold_if_not_in_template (result);
4899 /* For certain operations (which identify themselves by shorten != 0)
4900 if both args were extended from the same smaller type,
4901 do the arithmetic in that type and then extend.
4903 shorten !=0 and !=1 indicates a bitwise operation.
4904 For them, this optimization is safe only if
4905 both args are zero-extended or both are sign-extended.
4906 Otherwise, we might change the result.
4907 E.g., (short)-1 | (unsigned short)-1 is (int)-1
4908 but calculated in (unsigned short) it would be (unsigned short)-1. */
4910 if (shorten && none_complex)
4912 orig_type = result_type;
4913 final_type = result_type;
4914 result_type = shorten_binary_op (result_type, op0, op1,
4918 /* Comparison operations are shortened too but differently.
4919 They identify themselves by setting short_compare = 1. */
4923 /* Don't write &op0, etc., because that would prevent op0
4924 from being kept in a register.
4925 Instead, make copies of the our local variables and
4926 pass the copies by reference, then copy them back afterward. */
4927 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
4928 enum tree_code xresultcode = resultcode;
4930 = shorten_compare (location, &xop0, &xop1, &xresult_type,
4933 return cp_convert (boolean_type_node, val, complain);
4934 op0 = xop0, op1 = xop1;
4936 resultcode = xresultcode;
4939 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
4940 && warn_sign_compare
4941 /* Do not warn until the template is instantiated; we cannot
4942 bound the ranges of the arguments until that point. */
4943 && !processing_template_decl
4944 && (complain & tf_warning)
4945 && c_inhibit_evaluation_warnings == 0
4946 /* Even unsigned enum types promote to signed int. We don't
4947 want to issue -Wsign-compare warnings for this case. */
4948 && !enum_cast_to_int (orig_op0)
4949 && !enum_cast_to_int (orig_op1))
4951 tree oop0 = maybe_constant_value (orig_op0);
4952 tree oop1 = maybe_constant_value (orig_op1);
4954 if (TREE_CODE (oop0) != INTEGER_CST)
4956 if (TREE_CODE (oop1) != INTEGER_CST)
4958 warn_for_sign_compare (location, oop0, oop1, op0, op1,
4959 result_type, resultcode);
4963 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
4964 Then the expression will be built.
4965 It will be given type FINAL_TYPE if that is nonzero;
4966 otherwise, it will be given type RESULT_TYPE. */
4969 if (TREE_TYPE (op0) != result_type)
4970 op0 = cp_convert_and_check (result_type, op0, complain);
4971 if (TREE_TYPE (op1) != result_type)
4972 op1 = cp_convert_and_check (result_type, op1, complain);
4974 if (op0 == error_mark_node || op1 == error_mark_node)
4975 return error_mark_node;
4978 if (build_type == NULL_TREE)
4979 build_type = result_type;
4981 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
4982 | SANITIZE_FLOAT_DIVIDE))
4983 && !processing_template_decl
4984 && do_ubsan_in_current_function ()
4985 && (doing_div_or_mod || doing_shift))
4987 /* OP0 and/or OP1 might have side-effects. */
4988 op0 = cp_save_expr (op0);
4989 op1 = cp_save_expr (op1);
4990 op0 = fold_non_dependent_expr (op0);
4991 op1 = fold_non_dependent_expr (op1);
4992 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
4993 | SANITIZE_FLOAT_DIVIDE)))
4995 /* For diagnostics we want to use the promoted types without
4996 shorten_binary_op. So convert the arguments to the
4997 original result_type. */
5000 if (orig_type != NULL && result_type != orig_type)
5002 cop0 = cp_convert (orig_type, op0, complain);
5003 cop1 = cp_convert (orig_type, op1, complain);
5005 instrument_expr = ubsan_instrument_division (location, cop0, cop1);
5007 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
5008 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
5011 result = build2 (resultcode, build_type, op0, op1);
5012 result = fold_if_not_in_template (result);
5013 if (final_type != 0)
5014 result = cp_convert (final_type, result, complain);
5016 if (TREE_OVERFLOW_P (result)
5017 && !TREE_OVERFLOW_P (op0)
5018 && !TREE_OVERFLOW_P (op1))
5019 overflow_warning (location, result);
5021 if (instrument_expr != NULL)
5022 result = fold_build2 (COMPOUND_EXPR, TREE_TYPE (result),
5023 instrument_expr, result);
5028 /* Build a VEC_PERM_EXPR.
5029 This is a simple wrapper for c_build_vec_perm_expr. */
5031 build_x_vec_perm_expr (location_t loc,
5032 tree arg0, tree arg1, tree arg2,
5033 tsubst_flags_t complain)
5035 tree orig_arg0 = arg0;
5036 tree orig_arg1 = arg1;
5037 tree orig_arg2 = arg2;
5038 if (processing_template_decl)
5040 if (type_dependent_expression_p (arg0)
5041 || type_dependent_expression_p (arg1)
5042 || type_dependent_expression_p (arg2))
5043 return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2);
5044 arg0 = build_non_dependent_expr (arg0);
5046 arg1 = build_non_dependent_expr (arg1);
5047 arg2 = build_non_dependent_expr (arg2);
5049 tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
5050 if (processing_template_decl && exp != error_mark_node)
5051 return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0,
5052 orig_arg1, orig_arg2);
5056 /* Return a tree for the sum or difference (RESULTCODE says which)
5057 of pointer PTROP and integer INTOP. */
5060 cp_pointer_int_sum (enum tree_code resultcode, tree ptrop, tree intop,
5061 tsubst_flags_t complain)
5063 tree res_type = TREE_TYPE (ptrop);
5065 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
5066 in certain circumstance (when it's valid to do so). So we need
5067 to make sure it's complete. We don't need to check here, if we
5068 can actually complete it at all, as those checks will be done in
5069 pointer_int_sum() anyway. */
5070 complete_type (TREE_TYPE (res_type));
5072 return pointer_int_sum (input_location, resultcode, ptrop,
5073 fold_if_not_in_template (intop),
5074 complain & tf_warning_or_error);
5077 /* Return a tree for the difference of pointers OP0 and OP1.
5078 The resulting tree has type int. */
5081 pointer_diff (tree op0, tree op1, tree ptrtype, tsubst_flags_t complain)
5084 tree restype = ptrdiff_type_node;
5085 tree target_type = TREE_TYPE (ptrtype);
5087 if (!complete_type_or_else (target_type, NULL_TREE))
5088 return error_mark_node;
5090 if (VOID_TYPE_P (target_type))
5092 if (complain & tf_error)
5093 permerror (input_location, "ISO C++ forbids using pointer of "
5094 "type %<void *%> in subtraction");
5096 return error_mark_node;
5098 if (TREE_CODE (target_type) == FUNCTION_TYPE)
5100 if (complain & tf_error)
5101 permerror (input_location, "ISO C++ forbids using pointer to "
5102 "a function in subtraction");
5104 return error_mark_node;
5106 if (TREE_CODE (target_type) == METHOD_TYPE)
5108 if (complain & tf_error)
5109 permerror (input_location, "ISO C++ forbids using pointer to "
5110 "a method in subtraction");
5112 return error_mark_node;
5115 /* First do the subtraction as integers;
5116 then drop through to build the divide operator. */
5118 op0 = cp_build_binary_op (input_location,
5120 cp_convert (restype, op0, complain),
5121 cp_convert (restype, op1, complain),
5124 /* This generates an error if op1 is a pointer to an incomplete type. */
5125 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
5127 if (complain & tf_error)
5128 error ("invalid use of a pointer to an incomplete type in "
5129 "pointer arithmetic");
5131 return error_mark_node;
5134 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1)))
5136 if (complain & tf_error)
5137 error ("arithmetic on pointer to an empty aggregate");
5139 return error_mark_node;
5142 op1 = (TYPE_PTROB_P (ptrtype)
5143 ? size_in_bytes (target_type)
5144 : integer_one_node);
5146 /* Do the division. */
5148 result = build2 (EXACT_DIV_EXPR, restype, op0,
5149 cp_convert (restype, op1, complain));
5150 return fold_if_not_in_template (result);
5153 /* Construct and perhaps optimize a tree representation
5154 for a unary operation. CODE, a tree_code, specifies the operation
5155 and XARG is the operand. */
5158 build_x_unary_op (location_t loc, enum tree_code code, tree xarg,
5159 tsubst_flags_t complain)
5161 tree orig_expr = xarg;
5165 if (processing_template_decl)
5167 if (type_dependent_expression_p (xarg))
5168 return build_min_nt_loc (loc, code, xarg, NULL_TREE);
5170 xarg = build_non_dependent_expr (xarg);
5175 /* [expr.unary.op] says:
5177 The address of an object of incomplete type can be taken.
5179 (And is just the ordinary address operator, not an overloaded
5180 "operator &".) However, if the type is a template
5181 specialization, we must complete the type at this point so that
5182 an overloaded "operator &" will be available if required. */
5183 if (code == ADDR_EXPR
5184 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
5185 && ((CLASS_TYPE_P (TREE_TYPE (xarg))
5186 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
5187 || (TREE_CODE (xarg) == OFFSET_REF)))
5188 /* Don't look for a function. */;
5190 exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
5191 NULL_TREE, /*overload=*/NULL, complain);
5192 if (!exp && code == ADDR_EXPR)
5194 if (is_overloaded_fn (xarg))
5196 tree fn = get_first_fn (xarg);
5197 if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
5199 if (complain & tf_error)
5200 error (DECL_CONSTRUCTOR_P (fn)
5201 ? G_("taking address of constructor %qE")
5202 : G_("taking address of destructor %qE"),
5204 return error_mark_node;
5208 /* A pointer to member-function can be formed only by saying
5210 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
5211 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
5213 if (TREE_CODE (xarg) != OFFSET_REF
5214 || !TYPE_P (TREE_OPERAND (xarg, 0)))
5216 if (complain & tf_error)
5218 error ("invalid use of %qE to form a "
5219 "pointer-to-member-function", xarg);
5220 if (TREE_CODE (xarg) != OFFSET_REF)
5221 inform (input_location, " a qualified-id is required");
5223 return error_mark_node;
5227 if (complain & tf_error)
5228 error ("parentheses around %qE cannot be used to form a"
5229 " pointer-to-member-function",
5232 return error_mark_node;
5233 PTRMEM_OK_P (xarg) = 1;
5237 if (TREE_CODE (xarg) == OFFSET_REF)
5239 ptrmem = PTRMEM_OK_P (xarg);
5241 if (!ptrmem && !flag_ms_extensions
5242 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
5244 /* A single non-static member, make sure we don't allow a
5245 pointer-to-member. */
5246 xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
5247 TREE_OPERAND (xarg, 0),
5248 ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE));
5249 PTRMEM_OK_P (xarg) = ptrmem;
5253 exp = cp_build_addr_expr_strict (xarg, complain);
5256 if (processing_template_decl && exp != error_mark_node)
5257 exp = build_min_non_dep (code, exp, orig_expr,
5258 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
5259 if (TREE_CODE (exp) == ADDR_EXPR)
5260 PTRMEM_OK_P (exp) = ptrmem;
5264 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
5265 constants, where a null value is represented by an INTEGER_CST of
5269 cp_truthvalue_conversion (tree expr)
5271 tree type = TREE_TYPE (expr);
5272 if (TYPE_PTRDATAMEM_P (type)
5273 /* Avoid ICE on invalid use of non-static member function. */
5274 || TREE_CODE (expr) == FUNCTION_DECL)
5275 return build_binary_op (EXPR_LOCATION (expr),
5276 NE_EXPR, expr, nullptr_node, 1);
5277 else if (TYPE_PTR_P (type) || TYPE_PTRMEMFUNC_P (type))
5279 /* With -Wzero-as-null-pointer-constant do not warn for an
5280 'if (p)' or a 'while (!p)', where p is a pointer. */
5282 ++c_inhibit_evaluation_warnings;
5283 ret = c_common_truthvalue_conversion (input_location, expr);
5284 --c_inhibit_evaluation_warnings;
5288 return c_common_truthvalue_conversion (input_location, expr);
5291 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
5294 condition_conversion (tree expr)
5297 if (processing_template_decl)
5299 t = perform_implicit_conversion_flags (boolean_type_node, expr,
5300 tf_warning_or_error, LOOKUP_NORMAL);
5301 t = fold_build_cleanup_point_expr (boolean_type_node, t);
5305 /* Returns the address of T. This function will fold away
5306 ADDR_EXPR of INDIRECT_REF. */
5309 build_address (tree t)
5311 if (error_operand_p (t) || !cxx_mark_addressable (t))
5312 return error_mark_node;
5313 gcc_checking_assert (TREE_CODE (t) != CONSTRUCTOR);
5314 t = build_fold_addr_expr (t);
5315 if (TREE_CODE (t) != ADDR_EXPR)
5320 /* Return a NOP_EXPR converting EXPR to TYPE. */
5323 build_nop (tree type, tree expr)
5325 if (type == error_mark_node || error_operand_p (expr))
5327 return build1 (NOP_EXPR, type, expr);
5330 /* Take the address of ARG, whatever that means under C++ semantics.
5331 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
5332 and class rvalues as well.
5334 Nothing should call this function directly; instead, callers should use
5335 cp_build_addr_expr or cp_build_addr_expr_strict. */
5338 cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
5343 if (!arg || error_operand_p (arg))
5344 return error_mark_node;
5346 arg = mark_lvalue_use (arg);
5347 argtype = lvalue_type (arg);
5349 gcc_assert (!identifier_p (arg) || !IDENTIFIER_OPNAME_P (arg));
5351 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
5352 && !really_overloaded_fn (TREE_OPERAND (arg, 1)))
5354 /* They're trying to take the address of a unique non-static
5355 member function. This is ill-formed (except in MS-land),
5356 but let's try to DTRT.
5357 Note: We only handle unique functions here because we don't
5358 want to complain if there's a static overload; non-unique
5359 cases will be handled by instantiate_type. But we need to
5360 handle this case here to allow casts on the resulting PMF.
5361 We could defer this in non-MS mode, but it's easier to give
5362 a useful error here. */
5364 /* Inside constant member functions, the `this' pointer
5365 contains an extra const qualifier. TYPE_MAIN_VARIANT
5366 is used here to remove this const from the diagnostics
5367 and the created OFFSET_REF. */
5368 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
5369 tree fn = get_first_fn (TREE_OPERAND (arg, 1));
5372 if (! flag_ms_extensions)
5374 tree name = DECL_NAME (fn);
5375 if (!(complain & tf_error))
5376 return error_mark_node;
5377 else if (current_class_type
5378 && TREE_OPERAND (arg, 0) == current_class_ref)
5379 /* An expression like &memfn. */
5380 permerror (input_location, "ISO C++ forbids taking the address of an unqualified"
5381 " or parenthesized non-static member function to form"
5382 " a pointer to member function. Say %<&%T::%D%>",
5385 permerror (input_location, "ISO C++ forbids taking the address of a bound member"
5386 " function to form a pointer to member function."
5390 arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
5393 /* Uninstantiated types are all functions. Taking the
5394 address of a function is a no-op, so just return the
5396 if (type_unknown_p (arg))
5397 return build1 (ADDR_EXPR, unknown_type_node, arg);
5399 if (TREE_CODE (arg) == OFFSET_REF)
5400 /* We want a pointer to member; bypass all the code for actually taking
5401 the address of something. */
5404 /* Anything not already handled and not a true memory reference
5406 if (TREE_CODE (argtype) != FUNCTION_TYPE
5407 && TREE_CODE (argtype) != METHOD_TYPE)
5409 cp_lvalue_kind kind = lvalue_kind (arg);
5410 if (kind == clk_none)
5412 if (complain & tf_error)
5413 lvalue_error (input_location, lv_addressof);
5414 return error_mark_node;
5416 if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
5418 if (!(complain & tf_error))
5419 return error_mark_node;
5420 if (kind & clk_class)
5421 /* Make this a permerror because we used to accept it. */
5422 permerror (input_location, "taking address of temporary");
5424 error ("taking address of xvalue (rvalue reference)");
5428 if (TREE_CODE (argtype) == REFERENCE_TYPE)
5430 tree type = build_pointer_type (TREE_TYPE (argtype));
5431 arg = build1 (CONVERT_EXPR, type, arg);
5434 else if (pedantic && DECL_MAIN_P (arg))
5437 /* Apparently a lot of autoconf scripts for C++ packages do this,
5438 so only complain if -Wpedantic. */
5439 if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
5440 pedwarn (input_location, OPT_Wpedantic,
5441 "ISO C++ forbids taking address of function %<::main%>");
5442 else if (flag_pedantic_errors)
5443 return error_mark_node;
5446 /* Let &* cancel out to simplify resulting code. */
5447 if (INDIRECT_REF_P (arg))
5449 /* We don't need to have `current_class_ptr' wrapped in a
5450 NON_LVALUE_EXPR node. */
5451 if (arg == current_class_ref)
5452 return current_class_ptr;
5454 arg = TREE_OPERAND (arg, 0);
5455 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
5457 tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
5458 arg = build1 (CONVERT_EXPR, type, arg);
5461 /* Don't let this be an lvalue. */
5466 /* ??? Cope with user tricks that amount to offsetof. */
5467 if (TREE_CODE (argtype) != FUNCTION_TYPE
5468 && TREE_CODE (argtype) != METHOD_TYPE
5469 && argtype != unknown_type_node
5470 && (val = get_base_address (arg))
5471 && COMPLETE_TYPE_P (TREE_TYPE (val))
5472 && INDIRECT_REF_P (val)
5473 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
5475 tree type = build_pointer_type (argtype);
5476 return fold_convert (type, fold_offsetof_1 (arg));
5479 /* Handle complex lvalues (when permitted)
5480 by reduction to simpler cases. */
5481 val = unary_complex_lvalue (ADDR_EXPR, arg);
5485 switch (TREE_CODE (arg))
5489 case FIX_TRUNC_EXPR:
5490 /* Even if we're not being pedantic, we cannot allow this
5491 extension when we're instantiating in a SFINAE
5493 if (! lvalue_p (arg) && complain == tf_none)
5495 if (complain & tf_error)
5496 permerror (input_location, "ISO C++ forbids taking the address of a cast to a non-lvalue expression");
5498 return error_mark_node;
5503 arg = BASELINK_FUNCTIONS (arg);
5507 arg = OVL_CURRENT (arg);
5512 /* Turn a reference to a non-static data member into a
5513 pointer-to-member. */
5518 gcc_assert (PTRMEM_OK_P (arg));
5520 t = TREE_OPERAND (arg, 1);
5521 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
5523 if (complain & tf_error)
5524 error ("cannot create pointer to reference member %qD", t);
5525 return error_mark_node;
5528 type = build_ptrmem_type (context_for_name_lookup (t),
5530 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
5538 if (argtype != error_mark_node)
5539 argtype = build_pointer_type (argtype);
5541 /* In a template, we are processing a non-dependent expression
5542 so we can just form an ADDR_EXPR with the correct type. */
5543 if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
5545 val = build_address (arg);
5546 if (TREE_CODE (arg) == OFFSET_REF)
5547 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
5549 else if (BASELINK_P (TREE_OPERAND (arg, 1)))
5551 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
5553 /* We can only get here with a single static member
5555 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
5556 && DECL_STATIC_FUNCTION_P (fn));
5558 val = build_address (fn);
5559 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
5560 /* Do not lose object's side effects. */
5561 val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
5562 TREE_OPERAND (arg, 0), val);
5564 else if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
5566 if (complain & tf_error)
5567 error ("attempt to take address of bit-field structure member %qD",
5568 TREE_OPERAND (arg, 1));
5569 return error_mark_node;
5573 tree object = TREE_OPERAND (arg, 0);
5574 tree field = TREE_OPERAND (arg, 1);
5575 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5576 (TREE_TYPE (object), decl_type_context (field)));
5577 val = build_address (arg);
5580 if (TYPE_PTR_P (argtype)
5581 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
5583 build_ptrmemfunc_type (argtype);
5584 val = build_ptrmemfunc (argtype, val, 0,
5592 /* Take the address of ARG if it has one, even if it's an rvalue. */
5595 cp_build_addr_expr (tree arg, tsubst_flags_t complain)
5597 return cp_build_addr_expr_1 (arg, 0, complain);
5600 /* Take the address of ARG, but only if it's an lvalue. */
5603 cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
5605 return cp_build_addr_expr_1 (arg, 1, complain);
5608 /* C++: Must handle pointers to members.
5610 Perhaps type instantiation should be extended to handle conversion
5611 from aggregates to types we don't yet know we want? (Or are those
5612 cases typically errors which should be reported?)
5614 NOCONVERT nonzero suppresses the default promotions
5615 (such as from short to int). */
5618 cp_build_unary_op (enum tree_code code, tree xarg, int noconvert,
5619 tsubst_flags_t complain)
5621 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
5624 const char *errstring = NULL;
5626 const char *invalid_op_diag;
5628 if (!arg || error_operand_p (arg))
5629 return error_mark_node;
5631 if ((invalid_op_diag
5632 = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
5637 if (complain & tf_error)
5638 error (invalid_op_diag);
5639 return error_mark_node;
5644 case UNARY_PLUS_EXPR:
5647 int flags = WANT_ARITH | WANT_ENUM;
5648 /* Unary plus (but not unary minus) is allowed on pointers. */
5649 if (code == UNARY_PLUS_EXPR)
5650 flags |= WANT_POINTER;
5651 arg = build_expr_type_conversion (flags, arg, true);
5653 errstring = (code == NEGATE_EXPR
5654 ? _("wrong type argument to unary minus")
5655 : _("wrong type argument to unary plus"));
5658 if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5659 arg = cp_perform_integral_promotions (arg, complain);
5661 /* Make sure the result is not an lvalue: a unary plus or minus
5662 expression is always a rvalue. */
5669 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5674 arg = cp_default_conversion (arg, complain);
5675 if (arg == error_mark_node)
5676 return error_mark_node;
5679 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
5680 | WANT_VECTOR_OR_COMPLEX,
5682 errstring = _("wrong type argument to bit-complement");
5683 else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5684 arg = cp_perform_integral_promotions (arg, complain);
5688 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5689 errstring = _("wrong type argument to abs");
5690 else if (!noconvert)
5692 arg = cp_default_conversion (arg, complain);
5693 if (arg == error_mark_node)
5694 return error_mark_node;
5699 /* Conjugating a real value is a no-op, but allow it anyway. */
5700 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5701 errstring = _("wrong type argument to conjugation");
5702 else if (!noconvert)
5704 arg = cp_default_conversion (arg, complain);
5705 if (arg == error_mark_node)
5706 return error_mark_node;
5710 case TRUTH_NOT_EXPR:
5711 if (VECTOR_TYPE_P (TREE_TYPE (arg)))
5712 return cp_build_binary_op (input_location, EQ_EXPR, arg,
5713 build_zero_cst (TREE_TYPE (arg)), complain);
5714 arg = perform_implicit_conversion (boolean_type_node, arg,
5716 val = invert_truthvalue_loc (input_location, arg);
5717 if (arg != error_mark_node)
5719 errstring = _("in argument to unary !");
5727 arg = build_real_imag_expr (input_location, code, arg);
5728 if (arg == error_mark_node)
5731 return fold_if_not_in_template (arg);
5733 case PREINCREMENT_EXPR:
5734 case POSTINCREMENT_EXPR:
5735 case PREDECREMENT_EXPR:
5736 case POSTDECREMENT_EXPR:
5737 /* Handle complex lvalues (when permitted)
5738 by reduction to simpler cases. */
5740 val = unary_complex_lvalue (code, arg);
5744 arg = mark_lvalue_use (arg);
5746 /* Increment or decrement the real part of the value,
5747 and don't change the imaginary part. */
5748 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5752 arg = stabilize_reference (arg);
5753 real = cp_build_unary_op (REALPART_EXPR, arg, 1, complain);
5754 imag = cp_build_unary_op (IMAGPART_EXPR, arg, 1, complain);
5755 real = cp_build_unary_op (code, real, 1, complain);
5756 if (real == error_mark_node || imag == error_mark_node)
5757 return error_mark_node;
5758 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
5762 /* Report invalid types. */
5764 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
5767 if (code == PREINCREMENT_EXPR)
5768 errstring = _("no pre-increment operator for type");
5769 else if (code == POSTINCREMENT_EXPR)
5770 errstring = _("no post-increment operator for type");
5771 else if (code == PREDECREMENT_EXPR)
5772 errstring = _("no pre-decrement operator for type");
5774 errstring = _("no post-decrement operator for type");
5777 else if (arg == error_mark_node)
5778 return error_mark_node;
5780 /* Report something read-only. */
5782 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
5783 || TREE_READONLY (arg))
5785 if (complain & tf_error)
5786 cxx_readonly_error (arg, ((code == PREINCREMENT_EXPR
5787 || code == POSTINCREMENT_EXPR)
5788 ? lv_increment : lv_decrement));
5790 return error_mark_node;
5795 tree declared_type = unlowered_expr_type (arg);
5797 argtype = TREE_TYPE (arg);
5799 /* ARM $5.2.5 last annotation says this should be forbidden. */
5800 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
5802 if (complain & tf_error)
5803 permerror (input_location, (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
5804 ? G_("ISO C++ forbids incrementing an enum")
5805 : G_("ISO C++ forbids decrementing an enum"));
5807 return error_mark_node;
5810 /* Compute the increment. */
5812 if (TYPE_PTR_P (argtype))
5814 tree type = complete_type (TREE_TYPE (argtype));
5816 if (!COMPLETE_OR_VOID_TYPE_P (type))
5818 if (complain & tf_error)
5819 error (((code == PREINCREMENT_EXPR
5820 || code == POSTINCREMENT_EXPR))
5821 ? G_("cannot increment a pointer to incomplete type %qT")
5822 : G_("cannot decrement a pointer to incomplete type %qT"),
5823 TREE_TYPE (argtype));
5825 return error_mark_node;
5827 else if (!TYPE_PTROB_P (argtype))
5829 if (complain & tf_error)
5830 pedwarn (input_location, OPT_Wpointer_arith,
5831 (code == PREINCREMENT_EXPR
5832 || code == POSTINCREMENT_EXPR)
5833 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
5834 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
5837 return error_mark_node;
5840 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
5843 inc = VECTOR_TYPE_P (argtype)
5844 ? build_one_cst (argtype)
5847 inc = cp_convert (argtype, inc, complain);
5849 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
5850 need to ask Objective-C to build the increment or decrement
5851 expression for it. */
5852 if (objc_is_property_ref (arg))
5853 return objc_build_incr_expr_for_property_ref (input_location, code,
5856 /* Complain about anything else that is not a true lvalue. */
5857 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
5858 || code == POSTINCREMENT_EXPR)
5859 ? lv_increment : lv_decrement),
5861 return error_mark_node;
5863 /* Forbid using -- on `bool'. */
5864 if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
5866 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
5868 if (complain & tf_error)
5869 error ("invalid use of Boolean expression as operand "
5870 "to %<operator--%>");
5871 return error_mark_node;
5873 val = boolean_increment (code, arg);
5875 else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
5876 /* An rvalue has no cv-qualifiers. */
5877 val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
5879 val = build2 (code, TREE_TYPE (arg), arg, inc);
5881 TREE_SIDE_EFFECTS (val) = 1;
5886 /* Note that this operation never does default_conversion
5887 regardless of NOCONVERT. */
5888 return cp_build_addr_expr (arg, complain);
5897 argtype = TREE_TYPE (arg);
5898 return fold_if_not_in_template (build1 (code, argtype, arg));
5901 if (complain & tf_error)
5902 error ("%s", errstring);
5903 return error_mark_node;
5906 /* Hook for the c-common bits that build a unary op. */
5908 build_unary_op (location_t /*location*/,
5909 enum tree_code code, tree xarg, int noconvert)
5911 return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
5914 /* Apply unary lvalue-demanding operator CODE to the expression ARG
5915 for certain kinds of expressions which are not really lvalues
5916 but which we can accept as lvalues.
5918 If ARG is not a kind of expression we can handle, return
5922 unary_complex_lvalue (enum tree_code code, tree arg)
5924 /* Inside a template, making these kinds of adjustments is
5925 pointless; we are only concerned with the type of the
5927 if (processing_template_decl)
5930 /* Handle (a, b) used as an "lvalue". */
5931 if (TREE_CODE (arg) == COMPOUND_EXPR)
5933 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), 0,
5934 tf_warning_or_error);
5935 return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
5936 TREE_OPERAND (arg, 0), real_result);
5939 /* Handle (a ? b : c) used as an "lvalue". */
5940 if (TREE_CODE (arg) == COND_EXPR
5941 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
5942 return rationalize_conditional_expr (code, arg, tf_warning_or_error);
5944 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
5945 if (TREE_CODE (arg) == MODIFY_EXPR
5946 || TREE_CODE (arg) == PREINCREMENT_EXPR
5947 || TREE_CODE (arg) == PREDECREMENT_EXPR)
5949 tree lvalue = TREE_OPERAND (arg, 0);
5950 if (TREE_SIDE_EFFECTS (lvalue))
5952 lvalue = stabilize_reference (lvalue);
5953 arg = build2 (TREE_CODE (arg), TREE_TYPE (arg),
5954 lvalue, TREE_OPERAND (arg, 1));
5956 return unary_complex_lvalue
5957 (code, build2 (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
5960 if (code != ADDR_EXPR)
5963 /* Handle (a = b) used as an "lvalue" for `&'. */
5964 if (TREE_CODE (arg) == MODIFY_EXPR
5965 || TREE_CODE (arg) == INIT_EXPR)
5967 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), 0,
5968 tf_warning_or_error);
5969 arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
5971 TREE_NO_WARNING (arg) = 1;
5975 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
5976 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
5977 || TREE_CODE (arg) == OFFSET_REF)
5980 /* We permit compiler to make function calls returning
5981 objects of aggregate type look like lvalues. */
5985 if (TREE_CODE (targ) == SAVE_EXPR)
5986 targ = TREE_OPERAND (targ, 0);
5988 if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
5990 if (TREE_CODE (arg) == SAVE_EXPR)
5993 targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
5994 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
5997 if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
5998 return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
5999 TREE_OPERAND (targ, 0), current_function_decl, NULL);
6002 /* Don't let anything else be handled specially. */
6006 /* Mark EXP saying that we need to be able to take the
6007 address of it; it should not be allocated in a register.
6008 Value is true if successful.
6010 C++: we do not allow `current_class_ptr' to be addressable. */
6013 cxx_mark_addressable (tree exp)
6018 switch (TREE_CODE (x))
6025 x = TREE_OPERAND (x, 0);
6029 if (x == current_class_ptr)
6031 error ("cannot take the address of %<this%>, which is an rvalue expression");
6032 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
6038 /* Caller should not be trying to mark initialized
6039 constant fields addressable. */
6040 gcc_assert (DECL_LANG_SPECIFIC (x) == 0
6041 || DECL_IN_AGGR_P (x) == 0
6043 || DECL_EXTERNAL (x));
6047 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
6048 && !DECL_ARTIFICIAL (x))
6050 if (VAR_P (x) && DECL_HARD_REGISTER (x))
6053 ("address of explicit register variable %qD requested", x);
6056 else if (extra_warnings)
6058 (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
6060 TREE_ADDRESSABLE (x) = 1;
6065 TREE_ADDRESSABLE (x) = 1;
6069 TREE_ADDRESSABLE (x) = 1;
6073 TREE_ADDRESSABLE (x) = 1;
6074 cxx_mark_addressable (TREE_OPERAND (x, 0));
6082 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
6085 build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
6086 tsubst_flags_t complain)
6088 tree orig_ifexp = ifexp;
6089 tree orig_op1 = op1;
6090 tree orig_op2 = op2;
6093 if (processing_template_decl)
6095 /* The standard says that the expression is type-dependent if
6096 IFEXP is type-dependent, even though the eventual type of the
6097 expression doesn't dependent on IFEXP. */
6098 if (type_dependent_expression_p (ifexp)
6099 /* As a GNU extension, the middle operand may be omitted. */
6100 || (op1 && type_dependent_expression_p (op1))
6101 || type_dependent_expression_p (op2))
6102 return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
6103 ifexp = build_non_dependent_expr (ifexp);
6105 op1 = build_non_dependent_expr (op1);
6106 op2 = build_non_dependent_expr (op2);
6109 expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
6110 if (processing_template_decl && expr != error_mark_node
6111 && TREE_CODE (expr) != VEC_COND_EXPR)
6113 tree min = build_min_non_dep (COND_EXPR, expr,
6114 orig_ifexp, orig_op1, orig_op2);
6115 /* In C++11, remember that the result is an lvalue or xvalue.
6116 In C++98, lvalue_kind can just assume lvalue in a template. */
6117 if (cxx_dialect >= cxx11
6118 && lvalue_or_rvalue_with_address_p (expr)
6119 && !lvalue_or_rvalue_with_address_p (min))
6120 TREE_TYPE (min) = cp_build_reference_type (TREE_TYPE (min),
6121 !real_lvalue_p (expr));
6122 expr = convert_from_reference (min);
6127 /* Given a list of expressions, return a compound expression
6128 that performs them all and returns the value of the last of them. */
6131 build_x_compound_expr_from_list (tree list, expr_list_kind exp,
6132 tsubst_flags_t complain)
6134 tree expr = TREE_VALUE (list);
6136 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6137 && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
6139 if (complain & tf_error)
6140 pedwarn (EXPR_LOC_OR_LOC (expr, input_location), 0,
6141 "list-initializer for non-class type must not "
6142 "be parenthesized");
6144 return error_mark_node;
6147 if (TREE_CHAIN (list))
6149 if (complain & tf_error)
6153 permerror (input_location, "expression list treated as compound "
6154 "expression in initializer");
6157 permerror (input_location, "expression list treated as compound "
6158 "expression in mem-initializer");
6161 permerror (input_location, "expression list treated as compound "
6162 "expression in functional cast");
6168 return error_mark_node;
6170 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
6171 expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
6172 expr, TREE_VALUE (list), complain);
6178 /* Like build_x_compound_expr_from_list, but using a VEC. */
6181 build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
6182 tsubst_flags_t complain)
6184 if (vec_safe_is_empty (vec))
6186 else if (vec->length () == 1)
6196 if (complain & tf_error)
6197 permerror (input_location,
6198 "%s expression list treated as compound expression",
6201 return error_mark_node;
6205 for (ix = 1; vec->iterate (ix, &t); ++ix)
6206 expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
6213 /* Handle overloading of the ',' operator when needed. */
6216 build_x_compound_expr (location_t loc, tree op1, tree op2,
6217 tsubst_flags_t complain)
6220 tree orig_op1 = op1;
6221 tree orig_op2 = op2;
6223 if (processing_template_decl)
6225 if (type_dependent_expression_p (op1)
6226 || type_dependent_expression_p (op2))
6227 return build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
6228 op1 = build_non_dependent_expr (op1);
6229 op2 = build_non_dependent_expr (op2);
6232 result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
6233 NULL_TREE, /*overload=*/NULL, complain);
6235 result = cp_build_compound_expr (op1, op2, complain);
6237 if (processing_template_decl && result != error_mark_node)
6238 return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
6243 /* Like cp_build_compound_expr, but for the c-common bits. */
6246 build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
6248 return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
6251 /* Build a compound expression. */
6254 cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
6256 lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
6258 if (lhs == error_mark_node || rhs == error_mark_node)
6259 return error_mark_node;
6262 && (TREE_CODE (lhs) == CILK_SPAWN_STMT
6263 || TREE_CODE (rhs) == CILK_SPAWN_STMT))
6265 location_t loc = (EXPR_HAS_LOCATION (lhs) ? EXPR_LOCATION (lhs)
6266 : EXPR_LOCATION (rhs));
6268 "spawned function call cannot be part of a comma expression");
6269 return error_mark_node;
6272 if (TREE_CODE (rhs) == TARGET_EXPR)
6274 /* If the rhs is a TARGET_EXPR, then build the compound
6275 expression inside the target_expr's initializer. This
6276 helps the compiler to eliminate unnecessary temporaries. */
6277 tree init = TREE_OPERAND (rhs, 1);
6279 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
6280 TREE_OPERAND (rhs, 1) = init;
6285 if (type_unknown_p (rhs))
6287 if (complain & tf_error)
6288 error ("no context to resolve type of %qE", rhs);
6289 return error_mark_node;
6292 return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
6295 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
6296 casts away constness. CAST gives the type of cast. Returns true
6297 if the cast is ill-formed, false if it is well-formed.
6299 ??? This function warns for casting away any qualifier not just
6300 const. We would like to specify exactly what qualifiers are casted
6305 check_for_casting_away_constness (tree src_type, tree dest_type,
6306 enum tree_code cast, tsubst_flags_t complain)
6308 /* C-style casts are allowed to cast away constness. With
6309 WARN_CAST_QUAL, we still want to issue a warning. */
6310 if (cast == CAST_EXPR && !warn_cast_qual)
6313 if (!casts_away_constness (src_type, dest_type, complain))
6319 if (complain & tf_warning)
6320 warning (OPT_Wcast_qual,
6321 "cast from type %qT to type %qT casts away qualifiers",
6322 src_type, dest_type);
6325 case STATIC_CAST_EXPR:
6326 if (complain & tf_error)
6327 error ("static_cast from type %qT to type %qT casts away qualifiers",
6328 src_type, dest_type);
6331 case REINTERPRET_CAST_EXPR:
6332 if (complain & tf_error)
6333 error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
6334 src_type, dest_type);
6343 Warns if the cast from expression EXPR to type TYPE is useless.
6346 maybe_warn_about_useless_cast (tree type, tree expr, tsubst_flags_t complain)
6348 if (warn_useless_cast
6349 && complain & tf_warning)
6351 if ((TREE_CODE (type) == REFERENCE_TYPE
6352 && (TYPE_REF_IS_RVALUE (type)
6353 ? xvalue_p (expr) : real_lvalue_p (expr))
6354 && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
6355 || same_type_p (TREE_TYPE (expr), type))
6356 warning (OPT_Wuseless_cast, "useless cast to type %qT", type);
6360 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
6361 (another pointer-to-member type in the same hierarchy) and return
6362 the converted expression. If ALLOW_INVERSE_P is permitted, a
6363 pointer-to-derived may be converted to pointer-to-base; otherwise,
6364 only the other direction is permitted. If C_CAST_P is true, this
6365 conversion is taking place as part of a C-style cast. */
6368 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
6369 bool c_cast_p, tsubst_flags_t complain)
6371 if (TYPE_PTRDATAMEM_P (type))
6375 if (TREE_CODE (expr) == PTRMEM_CST)
6376 expr = cplus_expand_constant (expr);
6377 delta = get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr)),
6378 TYPE_PTRMEM_CLASS_TYPE (type),
6380 c_cast_p, complain);
6381 if (delta == error_mark_node)
6382 return error_mark_node;
6384 if (!integer_zerop (delta))
6386 tree cond, op1, op2;
6388 cond = cp_build_binary_op (input_location,
6391 build_int_cst (TREE_TYPE (expr), -1),
6393 op1 = build_nop (ptrdiff_type_node, expr);
6394 op2 = cp_build_binary_op (input_location,
6395 PLUS_EXPR, op1, delta,
6398 expr = fold_build3_loc (input_location,
6399 COND_EXPR, ptrdiff_type_node, cond, op1, op2);
6403 return build_nop (type, expr);
6406 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
6407 allow_inverse_p, c_cast_p, complain);
6410 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
6411 this static_cast is being attempted as one of the possible casts
6412 allowed by a C-style cast. (In that case, accessibility of base
6413 classes is not considered, and it is OK to cast away
6414 constness.) Return the result of the cast. *VALID_P is set to
6415 indicate whether or not the cast was valid. */
6418 build_static_cast_1 (tree type, tree expr, bool c_cast_p,
6419 bool *valid_p, tsubst_flags_t complain)
6425 /* Assume the cast is valid. */
6428 intype = unlowered_expr_type (expr);
6430 /* Save casted types in the function's used types hash table. */
6431 used_types_insert (type);
6433 /* [expr.static.cast]
6435 An lvalue of type "cv1 B", where B is a class type, can be cast
6436 to type "reference to cv2 D", where D is a class derived (clause
6437 _class.derived_) from B, if a valid standard conversion from
6438 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
6439 same cv-qualification as, or greater cv-qualification than, cv1,
6440 and B is not a virtual base class of D. */
6441 /* We check this case before checking the validity of "TYPE t =
6442 EXPR;" below because for this case:
6445 struct D : public B { D(const B&); };
6447 void f() { static_cast<const D&>(b); }
6449 we want to avoid constructing a new D. The standard is not
6450 completely clear about this issue, but our interpretation is
6451 consistent with other compilers. */
6452 if (TREE_CODE (type) == REFERENCE_TYPE
6453 && CLASS_TYPE_P (TREE_TYPE (type))
6454 && CLASS_TYPE_P (intype)
6455 && (TYPE_REF_IS_RVALUE (type) || real_lvalue_p (expr))
6456 && DERIVED_FROM_P (intype, TREE_TYPE (type))
6457 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
6458 build_pointer_type (TYPE_MAIN_VARIANT
6459 (TREE_TYPE (type))),
6462 || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6466 /* There is a standard conversion from "D*" to "B*" even if "B"
6467 is ambiguous or inaccessible. If this is really a
6468 static_cast, then we check both for inaccessibility and
6469 ambiguity. However, if this is a static_cast being performed
6470 because the user wrote a C-style cast, then accessibility is
6472 base = lookup_base (TREE_TYPE (type), intype,
6473 c_cast_p ? ba_unique : ba_check,
6475 expr = build_address (expr);
6477 if (flag_sanitize & SANITIZE_VPTR)
6480 = cp_ubsan_maybe_instrument_downcast (input_location, type, expr);
6485 /* Convert from "B*" to "D*". This function will check that "B"
6486 is not a virtual base of "D". */
6487 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
6490 /* Convert the pointer to a reference -- but then remember that
6491 there are no expressions with reference type in C++.
6493 We call rvalue so that there's an actual tree code
6494 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
6495 is a variable with the same type, the conversion would get folded
6496 away, leaving just the variable and causing lvalue_kind to give
6497 the wrong answer. */
6498 return convert_from_reference (rvalue (cp_fold_convert (type, expr)));
6501 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
6502 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
6503 if (TREE_CODE (type) == REFERENCE_TYPE
6504 && TYPE_REF_IS_RVALUE (type)
6505 && (clk = real_lvalue_p (expr))
6506 && reference_related_p (TREE_TYPE (type), intype)
6507 && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6509 if (clk == clk_ordinary)
6511 /* Handle the (non-bit-field) lvalue case here by casting to
6512 lvalue reference and then changing it to an rvalue reference.
6513 Casting an xvalue to rvalue reference will be handled by the
6515 tree lref = cp_build_reference_type (TREE_TYPE (type), false);
6516 result = (perform_direct_initialization_if_possible
6517 (lref, expr, c_cast_p, complain));
6518 result = cp_fold_convert (type, result);
6519 /* Make sure we don't fold back down to a named rvalue reference,
6520 because that would be an lvalue. */
6521 if (DECL_P (result))
6522 result = build1 (NON_LVALUE_EXPR, type, result);
6523 return convert_from_reference (result);
6526 /* For a bit-field or packed field, bind to a temporary. */
6527 expr = rvalue (expr);
6530 /* Resolve overloaded address here rather than once in
6531 implicit_conversion and again in the inverse code below. */
6532 if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
6534 expr = instantiate_type (type, expr, complain);
6535 intype = TREE_TYPE (expr);
6538 /* [expr.static.cast]
6540 Any expression can be explicitly converted to type cv void. */
6541 if (VOID_TYPE_P (type))
6542 return convert_to_void (expr, ICV_CAST, complain);
6545 An abstract class shall not be used ... as the type of an explicit
6547 if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
6548 return error_mark_node;
6550 /* [expr.static.cast]
6552 An expression e can be explicitly converted to a type T using a
6553 static_cast of the form static_cast<T>(e) if the declaration T
6554 t(e);" is well-formed, for some invented temporary variable
6556 result = perform_direct_initialization_if_possible (type, expr,
6557 c_cast_p, complain);
6560 result = convert_from_reference (result);
6562 /* [expr.static.cast]
6564 If T is a reference type, the result is an lvalue; otherwise,
6565 the result is an rvalue. */
6566 if (TREE_CODE (type) != REFERENCE_TYPE)
6567 result = rvalue (result);
6571 /* [expr.static.cast]
6573 The inverse of any standard conversion sequence (clause _conv_),
6574 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
6575 (_conv.array_), function-to-pointer (_conv.func_), and boolean
6576 (_conv.bool_) conversions, can be performed explicitly using
6577 static_cast subject to the restriction that the explicit
6578 conversion does not cast away constness (_expr.const.cast_), and
6579 the following additional rules for specific cases: */
6580 /* For reference, the conversions not excluded are: integral
6581 promotions, floating point promotion, integral conversions,
6582 floating point conversions, floating-integral conversions,
6583 pointer conversions, and pointer to member conversions. */
6586 A value of integral _or enumeration_ type can be explicitly
6587 converted to an enumeration type. */
6588 /* The effect of all that is that any conversion between any two
6589 types which are integral, floating, or enumeration types can be
6591 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6592 || SCALAR_FLOAT_TYPE_P (type))
6593 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
6594 || SCALAR_FLOAT_TYPE_P (intype)))
6595 return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
6597 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
6598 && CLASS_TYPE_P (TREE_TYPE (type))
6599 && CLASS_TYPE_P (TREE_TYPE (intype))
6600 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
6601 (TREE_TYPE (intype))),
6602 build_pointer_type (TYPE_MAIN_VARIANT
6603 (TREE_TYPE (type))),
6609 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6611 return error_mark_node;
6612 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
6613 c_cast_p ? ba_unique : ba_check,
6615 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
6618 if (flag_sanitize & SANITIZE_VPTR)
6621 = cp_ubsan_maybe_instrument_downcast (input_location, type, expr);
6626 return cp_fold_convert (type, expr);
6629 if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
6630 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6637 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
6638 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
6640 if (TYPE_PTRDATAMEM_P (type))
6642 t1 = (build_ptrmem_type
6644 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
6645 t2 = (build_ptrmem_type
6647 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
6654 if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
6657 && check_for_casting_away_constness (intype, type,
6660 return error_mark_node;
6661 return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
6662 c_cast_p, complain);
6666 /* [expr.static.cast]
6668 An rvalue of type "pointer to cv void" can be explicitly
6669 converted to a pointer to object type. A value of type pointer
6670 to object converted to "pointer to cv void" and back to the
6671 original pointer type will have its original value. */
6672 if (TYPE_PTR_P (intype)
6673 && VOID_TYPE_P (TREE_TYPE (intype))
6674 && TYPE_PTROB_P (type))
6677 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6679 return error_mark_node;
6680 return build_nop (type, expr);
6684 return error_mark_node;
6687 /* Return an expression representing static_cast<TYPE>(EXPR). */
6690 build_static_cast (tree type, tree expr, tsubst_flags_t complain)
6695 if (type == error_mark_node || expr == error_mark_node)
6696 return error_mark_node;
6698 if (processing_template_decl)
6700 expr = build_min (STATIC_CAST_EXPR, type, expr);
6701 /* We don't know if it will or will not have side effects. */
6702 TREE_SIDE_EFFECTS (expr) = 1;
6703 return convert_from_reference (expr);
6706 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6707 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6708 if (TREE_CODE (type) != REFERENCE_TYPE
6709 && TREE_CODE (expr) == NOP_EXPR
6710 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6711 expr = TREE_OPERAND (expr, 0);
6713 result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p,
6717 if (result != error_mark_node)
6718 maybe_warn_about_useless_cast (type, expr, complain);
6722 if (complain & tf_error)
6723 error ("invalid static_cast from type %qT to type %qT",
6724 TREE_TYPE (expr), type);
6725 return error_mark_node;
6728 /* EXPR is an expression with member function or pointer-to-member
6729 function type. TYPE is a pointer type. Converting EXPR to TYPE is
6730 not permitted by ISO C++, but we accept it in some modes. If we
6731 are not in one of those modes, issue a diagnostic. Return the
6732 converted expression. */
6735 convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
6740 intype = TREE_TYPE (expr);
6741 gcc_assert (TYPE_PTRMEMFUNC_P (intype)
6742 || TREE_CODE (intype) == METHOD_TYPE);
6744 if (!(complain & tf_warning_or_error))
6745 return error_mark_node;
6747 if (pedantic || warn_pmf2ptr)
6748 pedwarn (input_location, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
6749 "converting from %qT to %qT", intype, type);
6751 if (TREE_CODE (intype) == METHOD_TYPE)
6752 expr = build_addr_func (expr, complain);
6753 else if (TREE_CODE (expr) == PTRMEM_CST)
6754 expr = build_address (PTRMEM_CST_MEMBER (expr));
6757 decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
6758 decl = build_address (decl);
6759 expr = get_member_function_from_ptrfunc (&decl, expr, complain);
6762 if (expr == error_mark_node)
6763 return error_mark_node;
6765 return build_nop (type, expr);
6768 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
6769 If C_CAST_P is true, this reinterpret cast is being done as part of
6770 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
6771 indicate whether or not reinterpret_cast was valid. */
6774 build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
6775 bool *valid_p, tsubst_flags_t complain)
6779 /* Assume the cast is invalid. */
6783 if (type == error_mark_node || error_operand_p (expr))
6784 return error_mark_node;
6786 intype = TREE_TYPE (expr);
6788 /* Save casted types in the function's used types hash table. */
6789 used_types_insert (type);
6791 /* [expr.reinterpret.cast]
6792 An lvalue expression of type T1 can be cast to the type
6793 "reference to T2" if an expression of type "pointer to T1" can be
6794 explicitly converted to the type "pointer to T2" using a
6795 reinterpret_cast. */
6796 if (TREE_CODE (type) == REFERENCE_TYPE)
6798 if (! real_lvalue_p (expr))
6800 if (complain & tf_error)
6801 error ("invalid cast of an rvalue expression of type "
6804 return error_mark_node;
6807 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
6808 "B" are related class types; the reinterpret_cast does not
6809 adjust the pointer. */
6810 if (TYPE_PTR_P (intype)
6811 && (complain & tf_warning)
6812 && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
6813 COMPARE_BASE | COMPARE_DERIVED)))
6814 warning (0, "casting %qT to %qT does not dereference pointer",
6817 expr = cp_build_addr_expr (expr, complain);
6819 if (warn_strict_aliasing > 2)
6820 strict_aliasing_warning (TREE_TYPE (expr), type, expr);
6822 if (expr != error_mark_node)
6823 expr = build_reinterpret_cast_1
6824 (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
6826 if (expr != error_mark_node)
6827 /* cp_build_indirect_ref isn't right for rvalue refs. */
6828 expr = convert_from_reference (fold_convert (type, expr));
6832 /* As a G++ extension, we consider conversions from member
6833 functions, and pointers to member functions to
6834 pointer-to-function and pointer-to-void types. If
6835 -Wno-pmf-conversions has not been specified,
6836 convert_member_func_to_ptr will issue an error message. */
6837 if ((TYPE_PTRMEMFUNC_P (intype)
6838 || TREE_CODE (intype) == METHOD_TYPE)
6839 && TYPE_PTR_P (type)
6840 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
6841 || VOID_TYPE_P (TREE_TYPE (type))))
6842 return convert_member_func_to_ptr (type, expr, complain);
6844 /* If the cast is not to a reference type, the lvalue-to-rvalue,
6845 array-to-pointer, and function-to-pointer conversions are
6847 expr = decay_conversion (expr, complain);
6849 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6850 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6851 if (TREE_CODE (expr) == NOP_EXPR
6852 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6853 expr = TREE_OPERAND (expr, 0);
6855 if (error_operand_p (expr))
6856 return error_mark_node;
6858 intype = TREE_TYPE (expr);
6860 /* [expr.reinterpret.cast]
6861 A pointer can be converted to any integral type large enough to
6862 hold it. ... A value of type std::nullptr_t can be converted to
6863 an integral type; the conversion has the same meaning and
6864 validity as a conversion of (void*)0 to the integral type. */
6865 if (CP_INTEGRAL_TYPE_P (type)
6866 && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
6868 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
6870 if (complain & tf_error)
6871 permerror (input_location, "cast from %qT to %qT loses precision",
6874 return error_mark_node;
6876 if (NULLPTR_TYPE_P (intype))
6877 return build_int_cst (type, 0);
6879 /* [expr.reinterpret.cast]
6880 A value of integral or enumeration type can be explicitly
6881 converted to a pointer. */
6882 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
6885 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6886 || TYPE_PTR_OR_PTRMEM_P (type))
6887 && same_type_p (type, intype))
6889 return rvalue (expr);
6890 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
6891 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6892 return fold_if_not_in_template (build_nop (type, expr));
6893 else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
6894 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
6899 && check_for_casting_away_constness (intype, type,
6900 REINTERPRET_CAST_EXPR,
6902 return error_mark_node;
6903 /* Warn about possible alignment problems. */
6904 if (STRICT_ALIGNMENT && warn_cast_align
6905 && (complain & tf_warning)
6906 && !VOID_TYPE_P (type)
6907 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
6908 && COMPLETE_TYPE_P (TREE_TYPE (type))
6909 && COMPLETE_TYPE_P (TREE_TYPE (intype))
6910 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (intype)))
6911 warning (OPT_Wcast_align, "cast from %qT to %qT "
6912 "increases required alignment of target type", intype, type);
6914 /* We need to strip nops here, because the front end likes to
6915 create (int *)&a for array-to-pointer decay, instead of &a[0]. */
6917 if (warn_strict_aliasing <= 2)
6918 strict_aliasing_warning (intype, type, sexpr);
6920 return fold_if_not_in_template (build_nop (type, expr));
6922 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
6923 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
6925 if (complain & tf_warning)
6926 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
6927 object pointer type or vice versa is conditionally-supported." */
6928 warning (OPT_Wconditionally_supported,
6929 "casting between pointer-to-function and pointer-to-object "
6930 "is conditionally-supported");
6931 return fold_if_not_in_template (build_nop (type, expr));
6933 else if (TREE_CODE (type) == VECTOR_TYPE)
6934 return fold_if_not_in_template (convert_to_vector (type, expr));
6935 else if (TREE_CODE (intype) == VECTOR_TYPE
6936 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6937 return fold_if_not_in_template (convert_to_integer (type, expr));
6942 if (complain & tf_error)
6943 error ("invalid cast from type %qT to type %qT", intype, type);
6944 return error_mark_node;
6947 return cp_convert (type, expr, complain);
6951 build_reinterpret_cast (tree type, tree expr, tsubst_flags_t complain)
6955 if (type == error_mark_node || expr == error_mark_node)
6956 return error_mark_node;
6958 if (processing_template_decl)
6960 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
6962 if (!TREE_SIDE_EFFECTS (t)
6963 && type_dependent_expression_p (expr))
6964 /* There might turn out to be side effects inside expr. */
6965 TREE_SIDE_EFFECTS (t) = 1;
6966 return convert_from_reference (t);
6969 r = build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
6970 /*valid_p=*/NULL, complain);
6971 if (r != error_mark_node)
6972 maybe_warn_about_useless_cast (type, expr, complain);
6976 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
6977 return an appropriate expression. Otherwise, return
6978 error_mark_node. If the cast is not valid, and COMPLAIN is true,
6979 then a diagnostic will be issued. If VALID_P is non-NULL, we are
6980 performing a C-style cast, its value upon return will indicate
6981 whether or not the conversion succeeded. */
6984 build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain,
6988 tree reference_type;
6990 /* Callers are responsible for handling error_mark_node as a
6991 destination type. */
6992 gcc_assert (dst_type != error_mark_node);
6993 /* In a template, callers should be building syntactic
6994 representations of casts, not using this machinery. */
6995 gcc_assert (!processing_template_decl);
6997 /* Assume the conversion is invalid. */
7001 if (!POINTER_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
7003 if (complain & tf_error)
7004 error ("invalid use of const_cast with type %qT, "
7005 "which is not a pointer, "
7006 "reference, nor a pointer-to-data-member type", dst_type);
7007 return error_mark_node;
7010 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
7012 if (complain & tf_error)
7013 error ("invalid use of const_cast with type %qT, which is a pointer "
7014 "or reference to a function type", dst_type);
7015 return error_mark_node;
7018 /* Save casted types in the function's used types hash table. */
7019 used_types_insert (dst_type);
7021 src_type = TREE_TYPE (expr);
7022 /* Expressions do not really have reference types. */
7023 if (TREE_CODE (src_type) == REFERENCE_TYPE)
7024 src_type = TREE_TYPE (src_type);
7026 /* [expr.const.cast]
7028 For two object types T1 and T2, if a pointer to T1 can be explicitly
7029 converted to the type "pointer to T2" using a const_cast, then the
7030 following conversions can also be made:
7032 -- an lvalue of type T1 can be explicitly converted to an lvalue of
7033 type T2 using the cast const_cast<T2&>;
7035 -- a glvalue of type T1 can be explicitly converted to an xvalue of
7036 type T2 using the cast const_cast<T2&&>; and
7038 -- if T1 is a class type, a prvalue of type T1 can be explicitly
7039 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
7041 if (TREE_CODE (dst_type) == REFERENCE_TYPE)
7043 reference_type = dst_type;
7044 if (!TYPE_REF_IS_RVALUE (dst_type)
7045 ? real_lvalue_p (expr)
7046 : (CLASS_TYPE_P (TREE_TYPE (dst_type))
7048 : lvalue_or_rvalue_with_address_p (expr)))
7052 if (complain & tf_error)
7053 error ("invalid const_cast of an rvalue of type %qT to type %qT",
7054 src_type, dst_type);
7055 return error_mark_node;
7057 dst_type = build_pointer_type (TREE_TYPE (dst_type));
7058 src_type = build_pointer_type (src_type);
7062 reference_type = NULL_TREE;
7063 /* If the destination type is not a reference type, the
7064 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
7065 conversions are performed. */
7066 src_type = type_decays_to (src_type);
7067 if (src_type == error_mark_node)
7068 return error_mark_node;
7071 if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
7073 if (comp_ptr_ttypes_const (dst_type, src_type))
7078 /* This cast is actually a C-style cast. Issue a warning if
7079 the user is making a potentially unsafe cast. */
7080 check_for_casting_away_constness (src_type, dst_type,
7081 CAST_EXPR, complain);
7085 expr = cp_build_addr_expr (expr, complain);
7086 if (expr == error_mark_node)
7087 return error_mark_node;
7088 expr = build_nop (reference_type, expr);
7089 return convert_from_reference (expr);
7093 expr = decay_conversion (expr, complain);
7094 if (expr == error_mark_node)
7095 return error_mark_node;
7097 /* build_c_cast puts on a NOP_EXPR to make the result not an
7098 lvalue. Strip such NOP_EXPRs if VALUE is being used in
7099 non-lvalue context. */
7100 if (TREE_CODE (expr) == NOP_EXPR
7101 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7102 expr = TREE_OPERAND (expr, 0);
7103 return build_nop (dst_type, expr);
7107 && !at_least_as_qualified_p (TREE_TYPE (dst_type),
7108 TREE_TYPE (src_type)))
7109 check_for_casting_away_constness (src_type, dst_type, CAST_EXPR,
7113 if (complain & tf_error)
7114 error ("invalid const_cast from type %qT to type %qT",
7115 src_type, dst_type);
7116 return error_mark_node;
7120 build_const_cast (tree type, tree expr, tsubst_flags_t complain)
7124 if (type == error_mark_node || error_operand_p (expr))
7125 return error_mark_node;
7127 if (processing_template_decl)
7129 tree t = build_min (CONST_CAST_EXPR, type, expr);
7131 if (!TREE_SIDE_EFFECTS (t)
7132 && type_dependent_expression_p (expr))
7133 /* There might turn out to be side effects inside expr. */
7134 TREE_SIDE_EFFECTS (t) = 1;
7135 return convert_from_reference (t);
7138 r = build_const_cast_1 (type, expr, complain, /*valid_p=*/NULL);
7139 if (r != error_mark_node)
7140 maybe_warn_about_useless_cast (type, expr, complain);
7144 /* Like cp_build_c_cast, but for the c-common bits. */
7147 build_c_cast (location_t /*loc*/, tree type, tree expr)
7149 return cp_build_c_cast (type, expr, tf_warning_or_error);
7152 /* Build an expression representing an explicit C-style cast to type
7153 TYPE of expression EXPR. */
7156 cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain)
7162 if (type == error_mark_node || error_operand_p (expr))
7163 return error_mark_node;
7165 if (processing_template_decl)
7167 tree t = build_min (CAST_EXPR, type,
7168 tree_cons (NULL_TREE, value, NULL_TREE));
7169 /* We don't know if it will or will not have side effects. */
7170 TREE_SIDE_EFFECTS (t) = 1;
7171 return convert_from_reference (t);
7174 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
7175 'Class') should always be retained, because this information aids
7176 in method lookup. */
7177 if (objc_is_object_ptr (type)
7178 && objc_is_object_ptr (TREE_TYPE (expr)))
7179 return build_nop (type, expr);
7181 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7182 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7183 if (TREE_CODE (type) != REFERENCE_TYPE
7184 && TREE_CODE (value) == NOP_EXPR
7185 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
7186 value = TREE_OPERAND (value, 0);
7188 if (TREE_CODE (type) == ARRAY_TYPE)
7190 /* Allow casting from T1* to T2[] because Cfront allows it.
7191 NIHCL uses it. It is not valid ISO C++ however. */
7192 if (TYPE_PTR_P (TREE_TYPE (expr)))
7194 if (complain & tf_error)
7195 permerror (input_location, "ISO C++ forbids casting to an array type %qT", type);
7197 return error_mark_node;
7198 type = build_pointer_type (TREE_TYPE (type));
7202 if (complain & tf_error)
7203 error ("ISO C++ forbids casting to an array type %qT", type);
7204 return error_mark_node;
7208 if (TREE_CODE (type) == FUNCTION_TYPE
7209 || TREE_CODE (type) == METHOD_TYPE)
7211 if (complain & tf_error)
7212 error ("invalid cast to function type %qT", type);
7213 return error_mark_node;
7216 if (TYPE_PTR_P (type)
7217 && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
7218 /* Casting to an integer of smaller size is an error detected elsewhere. */
7219 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
7220 /* Don't warn about converting any constant. */
7221 && !TREE_CONSTANT (value))
7222 warning_at (input_location, OPT_Wint_to_pointer_cast,
7223 "cast to pointer from integer of different size");
7225 /* A C-style cast can be a const_cast. */
7226 result = build_const_cast_1 (type, value, complain & tf_warning,
7230 if (result != error_mark_node)
7231 maybe_warn_about_useless_cast (type, value, complain);
7235 /* Or a static cast. */
7236 result = build_static_cast_1 (type, value, /*c_cast_p=*/true,
7237 &valid_p, complain);
7238 /* Or a reinterpret_cast. */
7240 result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
7241 &valid_p, complain);
7242 /* The static_cast or reinterpret_cast may be followed by a
7245 /* A valid cast may result in errors if, for example, a
7246 conversion to an ambiguous base class is required. */
7247 && !error_operand_p (result))
7251 maybe_warn_about_useless_cast (type, value, complain);
7253 /* Non-class rvalues always have cv-unqualified type. */
7254 if (!CLASS_TYPE_P (type))
7255 type = TYPE_MAIN_VARIANT (type);
7256 result_type = TREE_TYPE (result);
7257 if (!CLASS_TYPE_P (result_type) && TREE_CODE (type) != REFERENCE_TYPE)
7258 result_type = TYPE_MAIN_VARIANT (result_type);
7259 /* If the type of RESULT does not match TYPE, perform a
7260 const_cast to make it match. If the static_cast or
7261 reinterpret_cast succeeded, we will differ by at most
7262 cv-qualification, so the follow-on const_cast is guaranteed
7264 if (!same_type_p (non_reference (type), non_reference (result_type)))
7266 result = build_const_cast_1 (type, result, false, &valid_p);
7267 gcc_assert (valid_p);
7272 return error_mark_node;
7275 /* For use from the C common bits. */
7277 build_modify_expr (location_t /*location*/,
7278 tree lhs, tree /*lhs_origtype*/,
7279 enum tree_code modifycode,
7280 location_t /*rhs_location*/, tree rhs,
7281 tree /*rhs_origtype*/)
7283 return cp_build_modify_expr (lhs, modifycode, rhs, tf_warning_or_error);
7286 /* Build an assignment expression of lvalue LHS from value RHS.
7287 MODIFYCODE is the code for a binary operator that we use
7288 to combine the old value of LHS with RHS to get the new value.
7289 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7291 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
7294 cp_build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs,
7295 tsubst_flags_t complain)
7299 tree lhstype = TREE_TYPE (lhs);
7300 tree olhstype = lhstype;
7301 bool plain_assign = (modifycode == NOP_EXPR);
7303 /* Avoid duplicate error messages from operands that had errors. */
7304 if (error_operand_p (lhs) || error_operand_p (rhs))
7305 return error_mark_node;
7307 /* Handle control structure constructs used as "lvalues". */
7308 switch (TREE_CODE (lhs))
7310 /* Handle --foo = 5; as these are valid constructs in C++. */
7311 case PREDECREMENT_EXPR:
7312 case PREINCREMENT_EXPR:
7313 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7314 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7315 stabilize_reference (TREE_OPERAND (lhs, 0)),
7316 TREE_OPERAND (lhs, 1));
7317 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0),
7318 modifycode, rhs, complain);
7319 if (newrhs == error_mark_node)
7320 return error_mark_node;
7321 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
7323 /* Handle (a, b) used as an "lvalue". */
7325 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 1),
7326 modifycode, rhs, complain);
7327 if (newrhs == error_mark_node)
7328 return error_mark_node;
7329 return build2 (COMPOUND_EXPR, lhstype,
7330 TREE_OPERAND (lhs, 0), newrhs);
7333 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7334 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7335 stabilize_reference (TREE_OPERAND (lhs, 0)),
7336 TREE_OPERAND (lhs, 1));
7337 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs,
7339 if (newrhs == error_mark_node)
7340 return error_mark_node;
7341 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
7345 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
7346 when neither operand has side-effects. */
7347 if (!lvalue_or_else (lhs, lv_assign, complain))
7348 return error_mark_node;
7350 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
7351 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
7353 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
7354 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
7356 TREE_OPERAND (lhs, 0),
7357 TREE_OPERAND (lhs, 1)),
7358 TREE_OPERAND (lhs, 0),
7359 TREE_OPERAND (lhs, 1));
7362 /* Handle (a ? b : c) used as an "lvalue". */
7365 /* Produce (a ? (b = rhs) : (c = rhs))
7366 except that the RHS goes through a save-expr
7367 so the code to compute it is only emitted once. */
7369 tree preeval = NULL_TREE;
7371 if (VOID_TYPE_P (TREE_TYPE (rhs)))
7373 if (complain & tf_error)
7374 error ("void value not ignored as it ought to be");
7375 return error_mark_node;
7378 rhs = stabilize_expr (rhs, &preeval);
7380 /* Check this here to avoid odd errors when trying to convert
7381 a throw to the type of the COND_EXPR. */
7382 if (!lvalue_or_else (lhs, lv_assign, complain))
7383 return error_mark_node;
7385 cond = build_conditional_expr
7386 (input_location, TREE_OPERAND (lhs, 0),
7387 cp_build_modify_expr (TREE_OPERAND (lhs, 1),
7388 modifycode, rhs, complain),
7389 cp_build_modify_expr (TREE_OPERAND (lhs, 2),
7390 modifycode, rhs, complain),
7393 if (cond == error_mark_node)
7395 /* Make sure the code to compute the rhs comes out
7396 before the split. */
7398 cond = build2 (COMPOUND_EXPR, TREE_TYPE (lhs), preeval, cond);
7406 if (modifycode == INIT_EXPR)
7408 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7409 /* Do the default thing. */;
7410 else if (TREE_CODE (rhs) == CONSTRUCTOR)
7412 /* Compound literal. */
7413 if (! same_type_p (TREE_TYPE (rhs), lhstype))
7414 /* Call convert to generate an error; see PR 11063. */
7415 rhs = convert (lhstype, rhs);
7416 result = build2 (INIT_EXPR, lhstype, lhs, rhs);
7417 TREE_SIDE_EFFECTS (result) = 1;
7420 else if (! MAYBE_CLASS_TYPE_P (lhstype))
7421 /* Do the default thing. */;
7424 vec<tree, va_gc> *rhs_vec = make_tree_vector_single (rhs);
7425 result = build_special_member_call (lhs, complete_ctor_identifier,
7426 &rhs_vec, lhstype, LOOKUP_NORMAL,
7428 release_tree_vector (rhs_vec);
7429 if (result == NULL_TREE)
7430 return error_mark_node;
7436 lhs = require_complete_type_sfinae (lhs, complain);
7437 if (lhs == error_mark_node)
7438 return error_mark_node;
7440 if (modifycode == NOP_EXPR)
7442 if (c_dialect_objc ())
7444 result = objc_maybe_build_modify_expr (lhs, rhs);
7449 /* `operator=' is not an inheritable operator. */
7450 if (! MAYBE_CLASS_TYPE_P (lhstype))
7451 /* Do the default thing. */;
7454 result = build_new_op (input_location, MODIFY_EXPR,
7455 LOOKUP_NORMAL, lhs, rhs,
7456 make_node (NOP_EXPR), /*overload=*/NULL,
7458 if (result == NULL_TREE)
7459 return error_mark_node;
7466 tree init = NULL_TREE;
7468 /* A binary op has been requested. Combine the old LHS
7469 value with the RHS producing the value we should actually
7470 store into the LHS. */
7471 gcc_assert (!((TREE_CODE (lhstype) == REFERENCE_TYPE
7472 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
7473 || MAYBE_CLASS_TYPE_P (lhstype)));
7475 /* Preevaluate the RHS to make sure its evaluation is complete
7476 before the lvalue-to-rvalue conversion of the LHS:
7478 [expr.ass] With respect to an indeterminately-sequenced
7479 function call, the operation of a compound assignment is a
7480 single evaluation. [ Note: Therefore, a function call shall
7481 not intervene between the lvalue-to-rvalue conversion and the
7482 side effect associated with any single compound assignment
7483 operator. -- end note ] */
7484 lhs = stabilize_reference (lhs);
7486 rhs = stabilize_expr (rhs, &init);
7487 newrhs = cp_build_binary_op (input_location,
7488 modifycode, lhs, rhs,
7490 if (newrhs == error_mark_node)
7492 if (complain & tf_error)
7493 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode,
7494 TREE_TYPE (lhs), TREE_TYPE (rhs));
7495 return error_mark_node;
7499 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
7501 /* Now it looks like a plain assignment. */
7502 modifycode = NOP_EXPR;
7503 if (c_dialect_objc ())
7505 result = objc_maybe_build_modify_expr (lhs, newrhs);
7510 gcc_assert (TREE_CODE (lhstype) != REFERENCE_TYPE);
7511 gcc_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE);
7514 /* The left-hand side must be an lvalue. */
7515 if (!lvalue_or_else (lhs, lv_assign, complain))
7516 return error_mark_node;
7518 /* Warn about modifying something that is `const'. Don't warn if
7519 this is initialization. */
7520 if (modifycode != INIT_EXPR
7521 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
7522 /* Functions are not modifiable, even though they are
7524 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
7525 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
7526 /* If it's an aggregate and any field is const, then it is
7527 effectively const. */
7528 || (CLASS_TYPE_P (lhstype)
7529 && C_TYPE_FIELDS_READONLY (lhstype))))
7531 if (complain & tf_error)
7532 cxx_readonly_error (lhs, lv_assign);
7534 return error_mark_node;
7537 /* If storing into a structure or union member, it may have been given a
7538 lowered bitfield type. We need to convert to the declared type first,
7539 so retrieve it now. */
7541 olhstype = unlowered_expr_type (lhs);
7543 /* Convert new value to destination type. */
7545 if (TREE_CODE (lhstype) == ARRAY_TYPE)
7549 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
7551 if (modifycode != INIT_EXPR)
7553 if (complain & tf_error)
7554 error ("assigning to an array from an initializer list");
7555 return error_mark_node;
7557 if (check_array_initializer (lhs, lhstype, newrhs))
7558 return error_mark_node;
7559 newrhs = digest_init (lhstype, newrhs, complain);
7560 if (newrhs == error_mark_node)
7561 return error_mark_node;
7564 /* C++11 8.5/17: "If the destination type is an array of characters,
7565 an array of char16_t, an array of char32_t, or an array of wchar_t,
7566 and the initializer is a string literal...". */
7567 else if (TREE_CODE (newrhs) == STRING_CST
7568 && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype)))
7569 && modifycode == INIT_EXPR)
7571 newrhs = digest_init (lhstype, newrhs, complain);
7572 if (newrhs == error_mark_node)
7573 return error_mark_node;
7576 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
7577 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
7579 if (complain & tf_error)
7580 error ("incompatible types in assignment of %qT to %qT",
7581 TREE_TYPE (rhs), lhstype);
7582 return error_mark_node;
7585 /* Allow array assignment in compiler-generated code. */
7586 else if (!current_function_decl
7587 || !DECL_DEFAULTED_FN (current_function_decl))
7589 /* This routine is used for both initialization and assignment.
7590 Make sure the diagnostic message differentiates the context. */
7591 if (complain & tf_error)
7593 if (modifycode == INIT_EXPR)
7594 error ("array used as initializer");
7596 error ("invalid array assignment");
7598 return error_mark_node;
7601 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
7602 ? 1 + (modifycode != INIT_EXPR): 0;
7603 return build_vec_init (lhs, NULL_TREE, newrhs,
7604 /*explicit_value_init_p=*/false,
7605 from_array, complain);
7608 if (modifycode == INIT_EXPR)
7609 /* Calls with INIT_EXPR are all direct-initialization, so don't set
7610 LOOKUP_ONLYCONVERTING. */
7611 newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
7612 ICR_INIT, NULL_TREE, 0,
7615 newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
7616 NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
7618 if (!same_type_p (lhstype, olhstype))
7619 newrhs = cp_convert_and_check (lhstype, newrhs, complain);
7621 if (modifycode != INIT_EXPR)
7623 if (TREE_CODE (newrhs) == CALL_EXPR
7624 && TYPE_NEEDS_CONSTRUCTING (lhstype))
7625 newrhs = build_cplus_new (lhstype, newrhs, complain);
7627 /* Can't initialize directly from a TARGET_EXPR, since that would
7628 cause the lhs to be constructed twice, and possibly result in
7629 accidental self-initialization. So we force the TARGET_EXPR to be
7630 expanded without a target. */
7631 if (TREE_CODE (newrhs) == TARGET_EXPR)
7632 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
7633 TREE_OPERAND (newrhs, 0));
7636 if (newrhs == error_mark_node)
7637 return error_mark_node;
7639 if (c_dialect_objc () && flag_objc_gc)
7641 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
7647 result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
7648 lhstype, lhs, newrhs);
7650 TREE_SIDE_EFFECTS (result) = 1;
7652 TREE_NO_WARNING (result) = 1;
7658 build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
7659 tree rhs, tsubst_flags_t complain)
7661 if (processing_template_decl)
7662 return build_min_nt_loc (loc, MODOP_EXPR, lhs,
7663 build_min_nt_loc (loc, modifycode, NULL_TREE,
7666 if (modifycode != NOP_EXPR)
7668 tree rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL, lhs, rhs,
7669 make_node (modifycode), /*overload=*/NULL,
7673 TREE_NO_WARNING (rval) = 1;
7677 return cp_build_modify_expr (lhs, modifycode, rhs, complain);
7680 /* Helper function for get_delta_difference which assumes FROM is a base
7681 class of TO. Returns a delta for the conversion of pointer-to-member
7682 of FROM to pointer-to-member of TO. If the conversion is invalid and
7683 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
7684 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
7685 If C_CAST_P is true, this conversion is taking place as part of a
7689 get_delta_difference_1 (tree from, tree to, bool c_cast_p,
7690 tsubst_flags_t complain)
7695 binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
7698 if (binfo == error_mark_node)
7700 if (!(complain & tf_error))
7701 return error_mark_node;
7703 error (" in pointer to member function conversion");
7704 return size_zero_node;
7708 if (kind != bk_via_virtual)
7709 return BINFO_OFFSET (binfo);
7711 /* FROM is a virtual base class of TO. Issue an error or warning
7712 depending on whether or not this is a reinterpret cast. */
7714 if (!(complain & tf_error))
7715 return error_mark_node;
7717 error ("pointer to member conversion via virtual base %qT",
7718 BINFO_TYPE (binfo_from_vbase (binfo)));
7720 return size_zero_node;
7727 /* Get difference in deltas for different pointer to member function
7728 types. If the conversion is invalid and tf_error is not set in
7729 COMPLAIN, returns error_mark_node, otherwise returns an integer
7730 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
7731 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
7732 conversions as well. If C_CAST_P is true this conversion is taking
7733 place as part of a C-style cast.
7735 Note that the naming of FROM and TO is kind of backwards; the return
7736 value is what we add to a TO in order to get a FROM. They are named
7737 this way because we call this function to find out how to convert from
7738 a pointer to member of FROM to a pointer to member of TO. */
7741 get_delta_difference (tree from, tree to,
7742 bool allow_inverse_p,
7743 bool c_cast_p, tsubst_flags_t complain)
7747 if (same_type_ignoring_top_level_qualifiers_p (from, to))
7748 /* Pointer to member of incomplete class is permitted*/
7749 result = size_zero_node;
7751 result = get_delta_difference_1 (from, to, c_cast_p, complain);
7753 if (result == error_mark_node)
7754 return error_mark_node;
7758 if (!allow_inverse_p)
7760 if (!(complain & tf_error))
7761 return error_mark_node;
7763 error_not_base_type (from, to);
7764 error (" in pointer to member conversion");
7765 result = size_zero_node;
7769 result = get_delta_difference_1 (to, from, c_cast_p, complain);
7771 if (result == error_mark_node)
7772 return error_mark_node;
7775 result = size_diffop_loc (input_location,
7776 size_zero_node, result);
7779 if (!(complain & tf_error))
7780 return error_mark_node;
7782 error_not_base_type (from, to);
7783 error (" in pointer to member conversion");
7784 result = size_zero_node;
7789 return fold_if_not_in_template (convert_to_integer (ptrdiff_type_node,
7793 /* Return a constructor for the pointer-to-member-function TYPE using
7794 the other components as specified. */
7797 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
7802 vec<constructor_elt, va_gc> *v;
7804 /* Pull the FIELD_DECLs out of the type. */
7805 pfn_field = TYPE_FIELDS (type);
7806 delta_field = DECL_CHAIN (pfn_field);
7808 /* Make sure DELTA has the type we want. */
7809 delta = convert_and_check (input_location, delta_type_node, delta);
7811 /* Convert to the correct target type if necessary. */
7812 pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
7814 /* Finish creating the initializer. */
7816 CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
7817 CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
7818 u = build_constructor (type, v);
7819 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
7820 TREE_STATIC (u) = (TREE_CONSTANT (u)
7821 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
7823 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
7828 /* Build a constructor for a pointer to member function. It can be
7829 used to initialize global variables, local variable, or used
7830 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
7833 If FORCE is nonzero, then force this conversion, even if
7834 we would rather not do it. Usually set when using an explicit
7835 cast. A C-style cast is being processed iff C_CAST_P is true.
7837 Return error_mark_node, if something goes wrong. */
7840 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
7841 tsubst_flags_t complain)
7847 if (error_operand_p (pfn))
7848 return error_mark_node;
7850 pfn_type = TREE_TYPE (pfn);
7851 to_type = build_ptrmemfunc_type (type);
7853 /* Handle multiple conversions of pointer to member functions. */
7854 if (TYPE_PTRMEMFUNC_P (pfn_type))
7856 tree delta = NULL_TREE;
7857 tree npfn = NULL_TREE;
7861 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
7862 LOOKUP_NORMAL, complain))
7864 if (complain & tf_error)
7865 error ("invalid conversion to type %qT from type %qT",
7868 return error_mark_node;
7871 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
7872 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
7874 c_cast_p, complain);
7875 if (n == error_mark_node)
7876 return error_mark_node;
7878 /* We don't have to do any conversion to convert a
7879 pointer-to-member to its own type. But, we don't want to
7880 just return a PTRMEM_CST if there's an explicit cast; that
7881 cast should make the expression an invalid template argument. */
7882 if (TREE_CODE (pfn) != PTRMEM_CST)
7884 if (same_type_p (to_type, pfn_type))
7886 else if (integer_zerop (n))
7887 return build_reinterpret_cast (to_type, pfn,
7891 if (TREE_SIDE_EFFECTS (pfn))
7892 pfn = save_expr (pfn);
7894 /* Obtain the function pointer and the current DELTA. */
7895 if (TREE_CODE (pfn) == PTRMEM_CST)
7896 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
7899 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
7900 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
7903 /* Just adjust the DELTA field. */
7904 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7905 (TREE_TYPE (delta), ptrdiff_type_node));
7906 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
7907 n = cp_build_binary_op (input_location,
7908 LSHIFT_EXPR, n, integer_one_node,
7910 delta = cp_build_binary_op (input_location,
7911 PLUS_EXPR, delta, n, complain);
7912 return build_ptrmemfunc1 (to_type, delta, npfn);
7915 /* Handle null pointer to member function conversions. */
7916 if (null_ptr_cst_p (pfn))
7918 pfn = cp_build_c_cast (type, pfn, complain);
7919 return build_ptrmemfunc1 (to_type,
7924 if (type_unknown_p (pfn))
7925 return instantiate_type (type, pfn, complain);
7927 fn = TREE_OPERAND (pfn, 0);
7928 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
7929 /* In a template, we will have preserved the
7931 || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
7932 return make_ptrmem_cst (to_type, fn);
7935 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
7938 ??? There is no consistency as to the types returned for the above
7939 values. Some code acts as if it were a sizetype and some as if it were
7940 integer_type_node. */
7943 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
7945 tree type = TREE_TYPE (cst);
7946 tree fn = PTRMEM_CST_MEMBER (cst);
7947 tree ptr_class, fn_class;
7949 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
7951 /* The class that the function belongs to. */
7952 fn_class = DECL_CONTEXT (fn);
7954 /* The class that we're creating a pointer to member of. */
7955 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
7957 /* First, calculate the adjustment to the function's class. */
7958 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
7959 /*c_cast_p=*/0, tf_warning_or_error);
7961 if (!DECL_VIRTUAL_P (fn))
7962 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type),
7963 build_addr_func (fn, tf_warning_or_error));
7966 /* If we're dealing with a virtual function, we have to adjust 'this'
7967 again, to point to the base which provides the vtable entry for
7968 fn; the call will do the opposite adjustment. */
7969 tree orig_class = DECL_CONTEXT (fn);
7970 tree binfo = binfo_or_else (orig_class, fn_class);
7971 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
7972 *delta, BINFO_OFFSET (binfo));
7973 *delta = fold_if_not_in_template (*delta);
7975 /* We set PFN to the vtable offset at which the function can be
7976 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
7977 case delta is shifted left, and then incremented). */
7978 *pfn = DECL_VINDEX (fn);
7979 *pfn = build2 (MULT_EXPR, integer_type_node, *pfn,
7980 TYPE_SIZE_UNIT (vtable_entry_type));
7981 *pfn = fold_if_not_in_template (*pfn);
7983 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
7985 case ptrmemfunc_vbit_in_pfn:
7986 *pfn = build2 (PLUS_EXPR, integer_type_node, *pfn,
7988 *pfn = fold_if_not_in_template (*pfn);
7991 case ptrmemfunc_vbit_in_delta:
7992 *delta = build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
7993 *delta, integer_one_node);
7994 *delta = fold_if_not_in_template (*delta);
7995 *delta = build2 (PLUS_EXPR, TREE_TYPE (*delta),
7996 *delta, integer_one_node);
7997 *delta = fold_if_not_in_template (*delta);
8004 *pfn = build_nop (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
8005 *pfn = fold_if_not_in_template (*pfn);
8009 /* Return an expression for PFN from the pointer-to-member function
8013 pfn_from_ptrmemfunc (tree t)
8015 if (TREE_CODE (t) == PTRMEM_CST)
8020 expand_ptrmemfunc_cst (t, &delta, &pfn);
8025 return build_ptrmemfunc_access_expr (t, pfn_identifier);
8028 /* Return an expression for DELTA from the pointer-to-member function
8032 delta_from_ptrmemfunc (tree t)
8034 if (TREE_CODE (t) == PTRMEM_CST)
8039 expand_ptrmemfunc_cst (t, &delta, &pfn);
8044 return build_ptrmemfunc_access_expr (t, delta_identifier);
8047 /* Convert value RHS to type TYPE as preparation for an assignment to
8048 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
8049 implicit conversion is. If FNDECL is non-NULL, we are doing the
8050 conversion in order to pass the PARMNUMth argument of FNDECL.
8051 If FNDECL is NULL, we are doing the conversion in function pointer
8052 argument passing, conversion in initialization, etc. */
8055 convert_for_assignment (tree type, tree rhs,
8056 impl_conv_rhs errtype, tree fndecl, int parmnum,
8057 tsubst_flags_t complain, int flags)
8060 enum tree_code coder;
8062 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
8063 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
8064 rhs = TREE_OPERAND (rhs, 0);
8066 rhstype = TREE_TYPE (rhs);
8067 coder = TREE_CODE (rhstype);
8069 if (TREE_CODE (type) == VECTOR_TYPE && coder == VECTOR_TYPE
8070 && vector_types_convertible_p (type, rhstype, true))
8072 rhs = mark_rvalue_use (rhs);
8073 return convert (type, rhs);
8076 if (rhs == error_mark_node || rhstype == error_mark_node)
8077 return error_mark_node;
8078 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
8079 return error_mark_node;
8081 /* The RHS of an assignment cannot have void type. */
8082 if (coder == VOID_TYPE)
8084 if (complain & tf_error)
8085 error ("void value not ignored as it ought to be");
8086 return error_mark_node;
8089 if (c_dialect_objc ())
8093 tree rname = fndecl;
8104 selector = objc_message_selector ();
8106 if (selector && parmno > 1)
8113 if (objc_compare_types (type, rhstype, parmno, rname))
8115 rhs = mark_rvalue_use (rhs);
8116 return convert (type, rhs);
8122 The expression is implicitly converted (clause _conv_) to the
8123 cv-unqualified type of the left operand.
8125 We allow bad conversions here because by the time we get to this point
8126 we are committed to doing the conversion. If we end up doing a bad
8127 conversion, convert_like will complain. */
8128 if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
8130 /* When -Wno-pmf-conversions is use, we just silently allow
8131 conversions from pointers-to-members to plain pointers. If
8132 the conversion doesn't work, cp_convert will complain. */
8134 && TYPE_PTR_P (type)
8135 && TYPE_PTRMEMFUNC_P (rhstype))
8136 rhs = cp_convert (strip_top_quals (type), rhs, complain);
8139 if (complain & tf_error)
8141 /* If the right-hand side has unknown type, then it is an
8142 overloaded function. Call instantiate_type to get error
8144 if (rhstype == unknown_type_node)
8145 instantiate_type (type, rhs, tf_warning_or_error);
8147 error ("cannot convert %qT to %qT for argument %qP to %qD",
8148 rhstype, type, parmnum, fndecl);
8152 case ICR_DEFAULT_ARGUMENT:
8153 error ("cannot convert %qT to %qT in default argument",
8157 error ("cannot convert %qT to %qT in argument passing",
8160 case ICR_CONVERTING:
8161 error ("cannot convert %qT to %qT",
8165 error ("cannot convert %qT to %qT in initialization",
8169 error ("cannot convert %qT to %qT in return",
8173 error ("cannot convert %qT to %qT in assignment",
8179 if (TYPE_PTR_P (rhstype)
8180 && TYPE_PTR_P (type)
8181 && CLASS_TYPE_P (TREE_TYPE (rhstype))
8182 && CLASS_TYPE_P (TREE_TYPE (type))
8183 && !COMPLETE_TYPE_P (TREE_TYPE (rhstype)))
8184 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
8185 (TREE_TYPE (rhstype))),
8186 "class type %qT is incomplete", TREE_TYPE (rhstype));
8188 return error_mark_node;
8191 if (warn_suggest_attribute_format)
8193 const enum tree_code codel = TREE_CODE (type);
8194 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
8196 && check_missing_format_attribute (type, rhstype)
8197 && (complain & tf_warning))
8201 case ICR_DEFAULT_ARGUMENT:
8203 warning (OPT_Wsuggest_attribute_format,
8204 "parameter %qP of %qD might be a candidate "
8205 "for a format attribute", parmnum, fndecl);
8207 warning (OPT_Wsuggest_attribute_format,
8208 "parameter might be a candidate "
8209 "for a format attribute");
8211 case ICR_CONVERTING:
8212 warning (OPT_Wsuggest_attribute_format,
8213 "target of conversion might be a candidate "
8214 "for a format attribute");
8217 warning (OPT_Wsuggest_attribute_format,
8218 "target of initialization might be a candidate "
8219 "for a format attribute");
8222 warning (OPT_Wsuggest_attribute_format,
8223 "return type might be a candidate "
8224 "for a format attribute");
8227 warning (OPT_Wsuggest_attribute_format,
8228 "left-hand side of assignment might be a candidate "
8229 "for a format attribute");
8236 /* If -Wparentheses, warn about a = b = c when a has type bool and b
8238 if (warn_parentheses
8239 && TREE_CODE (type) == BOOLEAN_TYPE
8240 && TREE_CODE (rhs) == MODIFY_EXPR
8241 && !TREE_NO_WARNING (rhs)
8242 && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
8243 && (complain & tf_warning))
8245 location_t loc = EXPR_LOC_OR_LOC (rhs, input_location);
8247 warning_at (loc, OPT_Wparentheses,
8248 "suggest parentheses around assignment used as truth value");
8249 TREE_NO_WARNING (rhs) = 1;
8252 return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
8256 /* Convert RHS to be of type TYPE.
8257 If EXP is nonzero, it is the target of the initialization.
8258 ERRTYPE indicates what kind of error the implicit conversion is.
8260 Two major differences between the behavior of
8261 `convert_for_assignment' and `convert_for_initialization'
8262 are that references are bashed in the former, while
8263 copied in the latter, and aggregates are assigned in
8264 the former (operator=) while initialized in the
8267 If using constructor make sure no conversion operator exists, if one does
8268 exist, an ambiguity exists. */
8271 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
8272 impl_conv_rhs errtype, tree fndecl, int parmnum,
8273 tsubst_flags_t complain)
8275 enum tree_code codel = TREE_CODE (type);
8277 enum tree_code coder;
8279 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8280 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
8281 if (TREE_CODE (rhs) == NOP_EXPR
8282 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
8283 && codel != REFERENCE_TYPE)
8284 rhs = TREE_OPERAND (rhs, 0);
8286 if (type == error_mark_node
8287 || rhs == error_mark_node
8288 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
8289 return error_mark_node;
8291 if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
8292 && TREE_CODE (type) != ARRAY_TYPE
8293 && (TREE_CODE (type) != REFERENCE_TYPE
8294 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
8295 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
8296 && !TYPE_REFFN_P (type))
8297 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
8298 rhs = decay_conversion (rhs, complain);
8300 rhstype = TREE_TYPE (rhs);
8301 coder = TREE_CODE (rhstype);
8303 if (coder == ERROR_MARK)
8304 return error_mark_node;
8306 /* We accept references to incomplete types, so we can
8307 return here before checking if RHS is of complete type. */
8309 if (codel == REFERENCE_TYPE)
8311 /* This should eventually happen in convert_arguments. */
8312 int savew = 0, savee = 0;
8315 savew = warningcount + werrorcount, savee = errorcount;
8316 rhs = initialize_reference (type, rhs, flags, complain);
8319 && (warningcount + werrorcount > savew || errorcount > savee))
8320 inform (input_location,
8321 "in passing argument %P of %q+D", parmnum, fndecl);
8327 exp = require_complete_type_sfinae (exp, complain);
8328 if (exp == error_mark_node)
8329 return error_mark_node;
8331 rhstype = non_reference (rhstype);
8333 type = complete_type (type);
8335 if (DIRECT_INIT_EXPR_P (type, rhs))
8336 /* Don't try to do copy-initialization if we already have
8337 direct-initialization. */
8340 if (MAYBE_CLASS_TYPE_P (type))
8341 return perform_implicit_conversion_flags (type, rhs, complain, flags);
8343 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
8347 /* If RETVAL is the address of, or a reference to, a local variable or
8348 temporary give an appropriate warning and return true. */
8351 maybe_warn_about_returning_address_of_local (tree retval)
8353 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
8354 tree whats_returned = retval;
8358 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
8359 whats_returned = TREE_OPERAND (whats_returned, 1);
8360 else if (CONVERT_EXPR_P (whats_returned)
8361 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
8362 whats_returned = TREE_OPERAND (whats_returned, 0);
8367 if (TREE_CODE (whats_returned) != ADDR_EXPR)
8369 whats_returned = TREE_OPERAND (whats_returned, 0);
8371 while (TREE_CODE (whats_returned) == COMPONENT_REF
8372 || TREE_CODE (whats_returned) == ARRAY_REF)
8373 whats_returned = TREE_OPERAND (whats_returned, 0);
8375 if (TREE_CODE (valtype) == REFERENCE_TYPE)
8377 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
8378 || TREE_CODE (whats_returned) == TARGET_EXPR)
8380 warning (OPT_Wreturn_local_addr, "returning reference to temporary");
8383 if (VAR_P (whats_returned)
8384 && DECL_NAME (whats_returned)
8385 && TEMP_NAME_P (DECL_NAME (whats_returned)))
8387 warning (OPT_Wreturn_local_addr, "reference to non-lvalue returned");
8392 if (DECL_P (whats_returned)
8393 && DECL_NAME (whats_returned)
8394 && DECL_FUNCTION_SCOPE_P (whats_returned)
8395 && !is_capture_proxy (whats_returned)
8396 && !(TREE_STATIC (whats_returned)
8397 || TREE_PUBLIC (whats_returned)))
8399 if (TREE_CODE (valtype) == REFERENCE_TYPE)
8400 warning (OPT_Wreturn_local_addr, "reference to local variable %q+D returned",
8402 else if (TREE_CODE (whats_returned) == LABEL_DECL)
8403 warning (OPT_Wreturn_local_addr, "address of label %q+D returned",
8406 warning (OPT_Wreturn_local_addr, "address of local variable %q+D "
8407 "returned", whats_returned);
8414 /* Check that returning RETVAL from the current function is valid.
8415 Return an expression explicitly showing all conversions required to
8416 change RETVAL into the function return type, and to assign it to
8417 the DECL_RESULT for the function. Set *NO_WARNING to true if
8418 code reaches end of non-void function warning shouldn't be issued
8419 on this RETURN_EXPR. */
8422 check_return_expr (tree retval, bool *no_warning)
8425 /* The type actually returned by the function. */
8427 /* The type the function is declared to return, or void if
8428 the declared type is incomplete. */
8430 int fn_returns_value_p;
8431 bool named_return_value_okay_p;
8433 *no_warning = false;
8435 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
8437 error_at (EXPR_LOCATION (retval), "use of %<_Cilk_spawn%> in a return "
8438 "statement is not allowed");
8442 /* A `volatile' function is one that isn't supposed to return, ever.
8443 (This is a G++ extension, used to get better code for functions
8444 that call the `volatile' function.) */
8445 if (TREE_THIS_VOLATILE (current_function_decl))
8446 warning (0, "function declared %<noreturn%> has a %<return%> statement");
8448 /* Check for various simple errors. */
8449 if (DECL_DESTRUCTOR_P (current_function_decl))
8452 error ("returning a value from a destructor");
8455 else if (DECL_CONSTRUCTOR_P (current_function_decl))
8457 if (in_function_try_handler)
8458 /* If a return statement appears in a handler of the
8459 function-try-block of a constructor, the program is ill-formed. */
8460 error ("cannot return from a handler of a function-try-block of a constructor");
8462 /* You can't return a value from a constructor. */
8463 error ("returning a value from a constructor");
8467 if (processing_template_decl)
8469 current_function_returns_value = 1;
8470 if (check_for_bare_parameter_packs (retval))
8471 retval = error_mark_node;
8475 functype = TREE_TYPE (TREE_TYPE (current_function_decl));
8477 /* Deduce auto return type from a return statement. */
8478 if (current_function_auto_return_pattern)
8483 if (!retval && !is_auto (current_function_auto_return_pattern))
8485 /* Give a helpful error message. */
8486 error ("return-statement with no value, in function returning %qT",
8487 current_function_auto_return_pattern);
8488 inform (input_location, "only plain %<auto%> return type can be "
8489 "deduced to %<void%>");
8490 type = error_mark_node;
8492 else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
8494 error ("returning initializer list");
8495 type = error_mark_node;
8501 auto_node = type_uses_auto (current_function_auto_return_pattern);
8502 type = do_auto_deduction (current_function_auto_return_pattern,
8506 if (type == error_mark_node)
8508 else if (functype == current_function_auto_return_pattern)
8509 apply_deduced_return_type (current_function_decl, type);
8511 /* A mismatch should have been diagnosed in do_auto_deduction. */
8512 gcc_assert (same_type_p (type, functype));
8516 /* When no explicit return-value is given in a function with a named
8517 return value, the named return value is used. */
8518 result = DECL_RESULT (current_function_decl);
8519 valtype = TREE_TYPE (result);
8520 gcc_assert (valtype != NULL_TREE);
8521 fn_returns_value_p = !VOID_TYPE_P (valtype);
8522 if (!retval && DECL_NAME (result) && fn_returns_value_p)
8525 /* Check for a return statement with no return value in a function
8526 that's supposed to return a value. */
8527 if (!retval && fn_returns_value_p)
8529 if (functype != error_mark_node)
8530 permerror (input_location, "return-statement with no value, in "
8531 "function returning %qT", valtype);
8532 /* Remember that this function did return. */
8533 current_function_returns_value = 1;
8534 /* And signal caller that TREE_NO_WARNING should be set on the
8535 RETURN_EXPR to avoid control reaches end of non-void function
8536 warnings in tree-cfg.c. */
8539 /* Check for a return statement with a value in a function that
8540 isn't supposed to return a value. */
8541 else if (retval && !fn_returns_value_p)
8543 if (VOID_TYPE_P (TREE_TYPE (retval)))
8544 /* You can return a `void' value from a function of `void'
8545 type. In that case, we have to evaluate the expression for
8546 its side-effects. */
8547 finish_expr_stmt (retval);
8549 permerror (input_location, "return-statement with a value, in function "
8550 "returning 'void'");
8551 current_function_returns_null = 1;
8553 /* There's really no value to return, after all. */
8557 /* Remember that this function can sometimes return without a
8559 current_function_returns_null = 1;
8561 /* Remember that this function did return a value. */
8562 current_function_returns_value = 1;
8564 /* Check for erroneous operands -- but after giving ourselves a
8565 chance to provide an error about returning a value from a void
8567 if (error_operand_p (retval))
8569 current_function_return_value = error_mark_node;
8570 return error_mark_node;
8573 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
8574 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
8575 || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
8576 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
8578 && retval && null_ptr_cst_p (retval))
8579 warning (0, "%<operator new%> must not return NULL unless it is "
8580 "declared %<throw()%> (or -fcheck-new is in effect)");
8582 /* Effective C++ rule 15. See also start_function. */
8584 && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR))
8588 /* The function return type must be a reference to the current
8590 if (TREE_CODE (valtype) == REFERENCE_TYPE
8591 && same_type_ignoring_top_level_qualifiers_p
8592 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
8594 /* Returning '*this' is obviously OK. */
8595 if (retval == current_class_ref)
8597 /* If we are calling a function whose return type is the same of
8598 the current class reference, it is ok. */
8599 else if (INDIRECT_REF_P (retval)
8600 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
8605 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
8608 /* The fabled Named Return Value optimization, as per [class.copy]/15:
8610 [...] For a function with a class return type, if the expression
8611 in the return statement is the name of a local object, and the cv-
8612 unqualified type of the local object is the same as the function
8613 return type, an implementation is permitted to omit creating the tem-
8614 porary object to hold the function return value [...]
8616 So, if this is a value-returning function that always returns the same
8617 local variable, remember it.
8619 It might be nice to be more flexible, and choose the first suitable
8620 variable even if the function sometimes returns something else, but
8621 then we run the risk of clobbering the variable we chose if the other
8622 returned expression uses the chosen variable somehow. And people expect
8623 this restriction, anyway. (jason 2000-11-19)
8625 See finish_function and finalize_nrv for the rest of this optimization. */
8627 named_return_value_okay_p =
8628 (retval != NULL_TREE
8629 /* Must be a local, automatic variable. */
8631 && DECL_CONTEXT (retval) == current_function_decl
8632 && ! TREE_STATIC (retval)
8633 /* And not a lambda or anonymous union proxy. */
8634 && !DECL_HAS_VALUE_EXPR_P (retval)
8635 && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
8636 /* The cv-unqualified type of the returned value must be the
8637 same as the cv-unqualified return type of the
8639 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
8640 (TYPE_MAIN_VARIANT (functype)))
8641 /* And the returned value must be non-volatile. */
8642 && ! TYPE_VOLATILE (TREE_TYPE (retval)));
8644 if (fn_returns_value_p && flag_elide_constructors)
8646 if (named_return_value_okay_p
8647 && (current_function_return_value == NULL_TREE
8648 || current_function_return_value == retval))
8649 current_function_return_value = retval;
8651 current_function_return_value = error_mark_node;
8654 /* We don't need to do any conversions when there's nothing being
8659 /* Do any required conversions. */
8660 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
8661 /* No conversions are required. */
8665 int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
8667 /* The functype's return type will have been set to void, if it
8668 was an incomplete type. Just treat this as 'return;' */
8669 if (VOID_TYPE_P (functype))
8670 return error_mark_node;
8672 /* If we had an id-expression obfuscated by force_paren_expr, we need
8673 to undo it so we can try to treat it as an rvalue below. */
8674 if (cxx_dialect >= cxx14
8675 && INDIRECT_REF_P (retval)
8676 && REF_PARENTHESIZED_P (retval))
8678 retval = TREE_OPERAND (retval, 0);
8679 while (TREE_CODE (retval) == NON_LVALUE_EXPR
8680 || TREE_CODE (retval) == NOP_EXPR)
8681 retval = TREE_OPERAND (retval, 0);
8682 gcc_assert (TREE_CODE (retval) == ADDR_EXPR);
8683 retval = TREE_OPERAND (retval, 0);
8686 /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
8687 treated as an rvalue for the purposes of overload resolution to
8688 favor move constructors over copy constructors.
8690 Note that these conditions are similar to, but not as strict as,
8691 the conditions for the named return value optimization. */
8692 if ((cxx_dialect != cxx98)
8693 && ((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
8694 || TREE_CODE (retval) == PARM_DECL)
8695 && DECL_CONTEXT (retval) == current_function_decl
8696 && !TREE_STATIC (retval)
8697 /* This is only interesting for class type. */
8698 && CLASS_TYPE_P (functype))
8699 flags = flags | LOOKUP_PREFER_RVALUE;
8701 /* First convert the value to the function's return type, then
8702 to the type of return value's location to handle the
8703 case that functype is smaller than the valtype. */
8704 retval = convert_for_initialization
8705 (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
8706 tf_warning_or_error);
8707 retval = convert (valtype, retval);
8709 /* If the conversion failed, treat this just like `return;'. */
8710 if (retval == error_mark_node)
8712 /* We can't initialize a register from a AGGR_INIT_EXPR. */
8713 else if (! cfun->returns_struct
8714 && TREE_CODE (retval) == TARGET_EXPR
8715 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
8716 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
8717 TREE_OPERAND (retval, 0));
8718 else if (maybe_warn_about_returning_address_of_local (retval))
8719 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
8720 build_zero_cst (TREE_TYPE (retval)));
8723 /* Actually copy the value returned into the appropriate location. */
8724 if (retval && retval != result)
8725 retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
8731 /* Returns nonzero if the pointer-type FROM can be converted to the
8732 pointer-type TO via a qualification conversion. If CONSTP is -1,
8733 then we return nonzero if the pointers are similar, and the
8734 cv-qualification signature of FROM is a proper subset of that of TO.
8736 If CONSTP is positive, then all outer pointers have been
8740 comp_ptr_ttypes_real (tree to, tree from, int constp)
8742 bool to_more_cv_qualified = false;
8743 bool is_opaque_pointer = false;
8745 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8747 if (TREE_CODE (to) != TREE_CODE (from))
8750 if (TREE_CODE (from) == OFFSET_TYPE
8751 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
8752 TYPE_OFFSET_BASETYPE (to)))
8755 /* Const and volatile mean something different for function types,
8756 so the usual checks are not appropriate. */
8757 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
8759 if (!at_least_as_qualified_p (to, from))
8762 if (!at_least_as_qualified_p (from, to))
8766 to_more_cv_qualified = true;
8770 constp &= TYPE_READONLY (to);
8773 if (TREE_CODE (to) == VECTOR_TYPE)
8774 is_opaque_pointer = vector_targets_convertible_p (to, from);
8776 if (!TYPE_PTR_P (to) && !TYPE_PTRDATAMEM_P (to))
8777 return ((constp >= 0 || to_more_cv_qualified)
8778 && (is_opaque_pointer
8779 || same_type_ignoring_top_level_qualifiers_p (to, from)));
8783 /* When comparing, say, char ** to char const **, this function takes
8784 the 'char *' and 'char const *'. Do not pass non-pointer/reference
8785 types to this function. */
8788 comp_ptr_ttypes (tree to, tree from)
8790 return comp_ptr_ttypes_real (to, from, 1);
8793 /* Returns true iff FNTYPE is a non-class type that involves
8794 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
8795 if a parameter type is ill-formed. */
8798 error_type_p (const_tree type)
8802 switch (TREE_CODE (type))
8808 case REFERENCE_TYPE:
8810 return error_type_p (TREE_TYPE (type));
8814 if (error_type_p (TREE_TYPE (type)))
8816 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
8817 if (error_type_p (TREE_VALUE (t)))
8822 if (TYPE_PTRMEMFUNC_P (type))
8823 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
8831 /* Returns true if to and from are (possibly multi-level) pointers to the same
8832 type or inheritance-related types, regardless of cv-quals. */
8835 ptr_reasonably_similar (const_tree to, const_tree from)
8837 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8839 /* Any target type is similar enough to void. */
8840 if (VOID_TYPE_P (to))
8841 return !error_type_p (from);
8842 if (VOID_TYPE_P (from))
8843 return !error_type_p (to);
8845 if (TREE_CODE (to) != TREE_CODE (from))
8848 if (TREE_CODE (from) == OFFSET_TYPE
8849 && comptypes (TYPE_OFFSET_BASETYPE (to),
8850 TYPE_OFFSET_BASETYPE (from),
8851 COMPARE_BASE | COMPARE_DERIVED))
8854 if (TREE_CODE (to) == VECTOR_TYPE
8855 && vector_types_convertible_p (to, from, false))
8858 if (TREE_CODE (to) == INTEGER_TYPE
8859 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
8862 if (TREE_CODE (to) == FUNCTION_TYPE)
8863 return !error_type_p (to) && !error_type_p (from);
8865 if (!TYPE_PTR_P (to))
8867 /* When either type is incomplete avoid DERIVED_FROM_P,
8868 which may call complete_type (c++/57942). */
8869 bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
8871 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
8872 b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
8877 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
8878 pointer-to-member types) are the same, ignoring cv-qualification at
8882 comp_ptr_ttypes_const (tree to, tree from)
8884 bool is_opaque_pointer = false;
8886 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8888 if (TREE_CODE (to) != TREE_CODE (from))
8891 if (TREE_CODE (from) == OFFSET_TYPE
8892 && same_type_p (TYPE_OFFSET_BASETYPE (from),
8893 TYPE_OFFSET_BASETYPE (to)))
8896 if (TREE_CODE (to) == VECTOR_TYPE)
8897 is_opaque_pointer = vector_targets_convertible_p (to, from);
8899 if (!TYPE_PTR_P (to))
8900 return (is_opaque_pointer
8901 || same_type_ignoring_top_level_qualifiers_p (to, from));
8905 /* Returns the type qualifiers for this type, including the qualifiers on the
8906 elements for an array type. */
8909 cp_type_quals (const_tree type)
8912 /* This CONST_CAST is okay because strip_array_types returns its
8913 argument unmodified and we assign it to a const_tree. */
8914 type = strip_array_types (CONST_CAST_TREE (type));
8915 if (type == error_mark_node
8916 /* Quals on a FUNCTION_TYPE are memfn quals. */
8917 || TREE_CODE (type) == FUNCTION_TYPE)
8918 return TYPE_UNQUALIFIED;
8919 quals = TYPE_QUALS (type);
8920 /* METHOD and REFERENCE_TYPEs should never have quals. */
8921 gcc_assert ((TREE_CODE (type) != METHOD_TYPE
8922 && TREE_CODE (type) != REFERENCE_TYPE)
8923 || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
8924 == TYPE_UNQUALIFIED));
8928 /* Returns the function-ref-qualifier for TYPE */
8931 type_memfn_rqual (const_tree type)
8933 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE
8934 || TREE_CODE (type) == METHOD_TYPE);
8936 if (!FUNCTION_REF_QUALIFIED (type))
8937 return REF_QUAL_NONE;
8938 else if (FUNCTION_RVALUE_QUALIFIED (type))
8939 return REF_QUAL_RVALUE;
8941 return REF_QUAL_LVALUE;
8944 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
8948 type_memfn_quals (const_tree type)
8950 if (TREE_CODE (type) == FUNCTION_TYPE)
8951 return TYPE_QUALS (type);
8952 else if (TREE_CODE (type) == METHOD_TYPE)
8953 return cp_type_quals (class_of_this_parm (type));
8958 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
8959 MEMFN_QUALS and its ref-qualifier to RQUAL. */
8962 apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
8964 /* Could handle METHOD_TYPE here if necessary. */
8965 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
8966 if (TYPE_QUALS (type) == memfn_quals
8967 && type_memfn_rqual (type) == rqual)
8970 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
8972 tree result = build_qualified_type (type, memfn_quals);
8973 if (tree canon = TYPE_CANONICAL (result))
8974 if (canon != result)
8975 /* check_qualified_type doesn't check the ref-qualifier, so make sure
8976 TYPE_CANONICAL is correct. */
8977 TYPE_CANONICAL (result)
8978 = build_ref_qualified_type (canon, type_memfn_rqual (result));
8979 result = build_exception_variant (result, TYPE_RAISES_EXCEPTIONS (type));
8980 return build_ref_qualified_type (result, rqual);
8983 /* Returns nonzero if TYPE is const or volatile. */
8986 cv_qualified_p (const_tree type)
8988 int quals = cp_type_quals (type);
8989 return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
8992 /* Returns nonzero if the TYPE contains a mutable member. */
8995 cp_has_mutable_p (const_tree type)
8997 /* This CONST_CAST is okay because strip_array_types returns its
8998 argument unmodified and we assign it to a const_tree. */
8999 type = strip_array_types (CONST_CAST_TREE(type));
9001 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
9004 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
9005 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
9006 approximation. In particular, consider:
9009 struct S { int i; };
9010 const S s = { f(); }
9012 Here, we will make "s" as TREE_READONLY (because it is declared
9013 "const") -- only to reverse ourselves upon seeing that the
9014 initializer is non-constant. */
9017 cp_apply_type_quals_to_decl (int type_quals, tree decl)
9019 tree type = TREE_TYPE (decl);
9021 if (type == error_mark_node)
9024 if (TREE_CODE (decl) == TYPE_DECL)
9027 gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
9028 && type_quals != TYPE_UNQUALIFIED));
9030 /* Avoid setting TREE_READONLY incorrectly. */
9031 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
9032 constructor can produce constant init, so rely on cp_finish_decl to
9033 clear TREE_READONLY if the variable has non-constant init. */
9035 /* If the type has (or might have) a mutable component, that component
9036 might be modified. */
9037 if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
9038 type_quals &= ~TYPE_QUAL_CONST;
9040 c_apply_type_quals_to_decl (type_quals, decl);
9043 /* Subroutine of casts_away_constness. Make T1 and T2 point at
9044 exemplar types such that casting T1 to T2 is casting away constness
9045 if and only if there is no implicit conversion from T1 to T2. */
9048 casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
9053 /* [expr.const.cast]
9055 For multi-level pointer to members and multi-level mixed pointers
9056 and pointers to members (conv.qual), the "member" aspect of a
9057 pointer to member level is ignored when determining if a const
9058 cv-qualifier has been cast away. */
9059 /* [expr.const.cast]
9061 For two pointer types:
9063 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
9064 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
9067 casting from X1 to X2 casts away constness if, for a non-pointer
9068 type T there does not exist an implicit conversion (clause
9071 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
9075 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
9076 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
9077 || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
9079 *t1 = cp_build_qualified_type (void_type_node,
9080 cp_type_quals (*t1));
9081 *t2 = cp_build_qualified_type (void_type_node,
9082 cp_type_quals (*t2));
9086 quals1 = cp_type_quals (*t1);
9087 quals2 = cp_type_quals (*t2);
9089 if (TYPE_PTRDATAMEM_P (*t1))
9090 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
9092 *t1 = TREE_TYPE (*t1);
9093 if (TYPE_PTRDATAMEM_P (*t2))
9094 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
9096 *t2 = TREE_TYPE (*t2);
9098 casts_away_constness_r (t1, t2, complain);
9099 *t1 = build_pointer_type (*t1);
9100 *t2 = build_pointer_type (*t2);
9101 *t1 = cp_build_qualified_type (*t1, quals1);
9102 *t2 = cp_build_qualified_type (*t2, quals2);
9105 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
9108 ??? This function returns non-zero if casting away qualifiers not
9109 just const. We would like to return to the caller exactly which
9110 qualifiers are casted away to give more accurate diagnostics.
9114 casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
9116 if (TREE_CODE (t2) == REFERENCE_TYPE)
9118 /* [expr.const.cast]
9120 Casting from an lvalue of type T1 to an lvalue of type T2
9121 using a reference cast casts away constness if a cast from an
9122 rvalue of type "pointer to T1" to the type "pointer to T2"
9123 casts away constness. */
9124 t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1);
9125 return casts_away_constness (build_pointer_type (t1),
9126 build_pointer_type (TREE_TYPE (t2)),
9130 if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
9131 /* [expr.const.cast]
9133 Casting from an rvalue of type "pointer to data member of X
9134 of type T1" to the type "pointer to data member of Y of type
9135 T2" casts away constness if a cast from an rvalue of type
9136 "pointer to T1" to the type "pointer to T2" casts away
9138 return casts_away_constness
9139 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
9140 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
9143 /* Casting away constness is only something that makes sense for
9144 pointer or reference types. */
9145 if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
9148 /* Top-level qualifiers don't matter. */
9149 t1 = TYPE_MAIN_VARIANT (t1);
9150 t2 = TYPE_MAIN_VARIANT (t2);
9151 casts_away_constness_r (&t1, &t2, complain);
9152 if (!can_convert (t2, t1, complain))
9158 /* If T is a REFERENCE_TYPE return the type to which T refers.
9159 Otherwise, return T itself. */
9162 non_reference (tree t)
9164 if (t && TREE_CODE (t) == REFERENCE_TYPE)
9170 /* Return nonzero if REF is an lvalue valid for this language;
9171 otherwise, print an error message and return zero. USE says
9172 how the lvalue is being used and so selects the error message. */
9175 lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
9177 cp_lvalue_kind kind = lvalue_kind (ref);
9179 if (kind == clk_none)
9181 if (complain & tf_error)
9182 lvalue_error (input_location, use);
9185 else if (kind & (clk_rvalueref|clk_class))
9187 if (!(complain & tf_error))
9189 if (kind & clk_class)
9190 /* Make this a permerror because we used to accept it. */
9191 permerror (input_location, "using temporary as lvalue");
9193 error ("using xvalue (rvalue reference) as lvalue");
9198 /* Return true if a user-defined literal operator is a raw operator. */
9201 check_raw_literal_operator (const_tree decl)
9203 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9206 bool maybe_raw_p = false;
9208 /* Count the number and type of arguments and check for ellipsis. */
9209 for (argtype = argtypes, arity = 0;
9210 argtype && argtype != void_list_node;
9211 ++arity, argtype = TREE_CHAIN (argtype))
9213 tree t = TREE_VALUE (argtype);
9215 if (same_type_p (t, const_string_type_node))
9219 return false; /* Found ellipsis. */
9221 if (!maybe_raw_p || arity != 1)
9228 /* Return true if a user-defined literal operator has one of the allowed
9232 check_literal_operator_args (const_tree decl,
9233 bool *long_long_unsigned_p, bool *long_double_p)
9235 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9237 *long_long_unsigned_p = false;
9238 *long_double_p = false;
9239 if (processing_template_decl || processing_specialization)
9240 return argtypes == void_list_node;
9247 /* Count the number and type of arguments and check for ellipsis. */
9248 for (argtype = argtypes, arity = 0;
9249 argtype && argtype != void_list_node;
9250 argtype = TREE_CHAIN (argtype))
9252 tree t = TREE_VALUE (argtype);
9257 bool maybe_raw_p = false;
9259 if (cp_type_quals (t) != TYPE_QUAL_CONST)
9261 t = TYPE_MAIN_VARIANT (t);
9262 if ((maybe_raw_p = same_type_p (t, char_type_node))
9263 || same_type_p (t, wchar_type_node)
9264 || same_type_p (t, char16_type_node)
9265 || same_type_p (t, char32_type_node))
9267 argtype = TREE_CHAIN (argtype);
9270 t = TREE_VALUE (argtype);
9271 if (maybe_raw_p && argtype == void_list_node)
9273 else if (same_type_p (t, size_type_node))
9282 else if (same_type_p (t, long_long_unsigned_type_node))
9285 *long_long_unsigned_p = true;
9287 else if (same_type_p (t, long_double_type_node))
9290 *long_double_p = true;
9292 else if (same_type_p (t, char_type_node))
9294 else if (same_type_p (t, wchar_type_node))
9296 else if (same_type_p (t, char16_type_node))
9298 else if (same_type_p (t, char32_type_node))
9304 return false; /* Found ellipsis. */
9306 if (arity != max_arity)