1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987-2016 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"
32 #include "stor-layout.h"
36 #include "c-family/c-objc.h"
37 #include "c-family/c-ubsan.h"
40 static tree cp_build_addr_expr_strict (tree, tsubst_flags_t);
41 static tree cp_build_function_call (tree, tree, tsubst_flags_t);
42 static tree pfn_from_ptrmemfunc (tree);
43 static tree delta_from_ptrmemfunc (tree);
44 static tree convert_for_assignment (tree, tree, impl_conv_rhs, tree, int,
46 static tree cp_pointer_int_sum (enum tree_code, tree, tree, tsubst_flags_t);
47 static tree rationalize_conditional_expr (enum tree_code, tree,
49 static int comp_ptr_ttypes_real (tree, tree, int);
50 static bool comp_except_types (tree, tree, bool);
51 static bool comp_array_types (const_tree, const_tree, bool);
52 static tree pointer_diff (tree, tree, tree, tsubst_flags_t);
53 static tree get_delta_difference (tree, tree, bool, bool, tsubst_flags_t);
54 static void casts_away_constness_r (tree *, tree *, tsubst_flags_t);
55 static bool casts_away_constness (tree, tree, tsubst_flags_t);
56 static bool maybe_warn_about_returning_address_of_local (tree);
57 static tree lookup_destructor (tree, tree, tree, tsubst_flags_t);
58 static void error_args_num (location_t, tree, bool);
59 static int convert_arguments (tree, vec<tree, va_gc> **, tree, int,
62 /* Do `exp = require_complete_type (exp);' to make sure exp
63 does not have an incomplete type. (That includes void types.)
64 Returns error_mark_node if the VALUE does not have
65 complete type when this function returns. */
68 require_complete_type_sfinae (tree value, tsubst_flags_t complain)
72 if (processing_template_decl || value == error_mark_node)
75 if (TREE_CODE (value) == OVERLOAD)
76 type = unknown_type_node;
78 type = TREE_TYPE (value);
80 if (type == error_mark_node)
81 return error_mark_node;
83 /* First, detect a valid value with a complete type. */
84 if (COMPLETE_TYPE_P (type))
87 if (complete_type_or_maybe_complain (type, value, complain))
90 return error_mark_node;
94 require_complete_type (tree value)
96 return require_complete_type_sfinae (value, tf_warning_or_error);
99 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
100 a template instantiation, do the instantiation. Returns TYPE,
101 whether or not it could be completed, unless something goes
102 horribly wrong, in which case the error_mark_node is returned. */
105 complete_type (tree type)
107 if (type == NULL_TREE)
108 /* Rather than crash, we return something sure to cause an error
110 return error_mark_node;
112 if (type == error_mark_node || COMPLETE_TYPE_P (type))
114 else if (TREE_CODE (type) == ARRAY_TYPE)
116 tree t = complete_type (TREE_TYPE (type));
117 unsigned int needs_constructing, has_nontrivial_dtor;
118 if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
121 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
123 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
124 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
126 TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
127 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
130 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
131 instantiate_class_template (TYPE_MAIN_VARIANT (type));
136 /* Like complete_type, but issue an error if the TYPE cannot be completed.
137 VALUE is used for informative diagnostics.
138 Returns NULL_TREE if the type cannot be made complete. */
141 complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
143 type = complete_type (type);
144 if (type == error_mark_node)
145 /* We already issued an error. */
147 else if (!COMPLETE_TYPE_P (type))
149 if (complain & tf_error)
150 cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
158 complete_type_or_else (tree type, tree value)
160 return complete_type_or_maybe_complain (type, value, tf_warning_or_error);
163 /* Return truthvalue of whether type of EXP is instantiated. */
166 type_unknown_p (const_tree exp)
168 return (TREE_CODE (exp) == TREE_LIST
169 || TREE_TYPE (exp) == unknown_type_node);
173 /* Return the common type of two parameter lists.
174 We assume that comptypes has already been done and returned 1;
175 if that isn't so, this may crash.
177 As an optimization, free the space we allocate if the parameter
178 lists are already common. */
181 commonparms (tree p1, tree p2)
183 tree oldargs = p1, newargs, n;
187 len = list_length (p1);
188 newargs = tree_last (p1);
190 if (newargs == void_list_node)
199 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
204 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
206 if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
208 TREE_PURPOSE (n) = TREE_PURPOSE (p1);
211 else if (! TREE_PURPOSE (p1))
213 if (TREE_PURPOSE (p2))
215 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
221 if (1 != simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)))
223 TREE_PURPOSE (n) = TREE_PURPOSE (p2);
225 if (TREE_VALUE (p1) != TREE_VALUE (p2))
228 TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
231 TREE_VALUE (n) = TREE_VALUE (p1);
239 /* Given a type, perhaps copied for a typedef,
240 find the "original" version of it. */
242 original_type (tree t)
244 int quals = cp_type_quals (t);
245 while (t != error_mark_node
246 && TYPE_NAME (t) != NULL_TREE)
248 tree x = TYPE_NAME (t);
249 if (TREE_CODE (x) != TYPE_DECL)
251 x = DECL_ORIGINAL_TYPE (x);
256 return cp_build_qualified_type (t, quals);
259 /* Return the common type for two arithmetic types T1 and T2 under the
260 usual arithmetic conversions. The default conversions have already
261 been applied, and enumerated types converted to their compatible
265 cp_common_type (tree t1, tree t2)
267 enum tree_code code1 = TREE_CODE (t1);
268 enum tree_code code2 = TREE_CODE (t2);
273 /* In what follows, we slightly generalize the rules given in [expr] so
274 as to deal with `long long' and `complex'. First, merge the
276 attributes = (*targetm.merge_type_attributes) (t1, t2);
278 if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
280 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
281 return build_type_attribute_variant (t1, attributes);
286 /* FIXME: Attributes. */
287 gcc_assert (ARITHMETIC_TYPE_P (t1)
288 || VECTOR_TYPE_P (t1)
289 || UNSCOPED_ENUM_P (t1));
290 gcc_assert (ARITHMETIC_TYPE_P (t2)
291 || VECTOR_TYPE_P (t2)
292 || UNSCOPED_ENUM_P (t2));
294 /* If one type is complex, form the common type of the non-complex
295 components, then make that complex. Use T1 or T2 if it is the
297 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
299 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
300 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
302 = type_after_usual_arithmetic_conversions (subtype1, subtype2);
304 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
305 return build_type_attribute_variant (t1, attributes);
306 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
307 return build_type_attribute_variant (t2, attributes);
309 return build_type_attribute_variant (build_complex_type (subtype),
313 if (code1 == VECTOR_TYPE)
315 /* When we get here we should have two vectors of the same size.
316 Just prefer the unsigned one if present. */
317 if (TYPE_UNSIGNED (t1))
318 return build_type_attribute_variant (t1, attributes);
320 return build_type_attribute_variant (t2, attributes);
323 /* If only one is real, use it as the result. */
324 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
325 return build_type_attribute_variant (t1, attributes);
326 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
327 return build_type_attribute_variant (t2, attributes);
329 /* Both real or both integers; use the one with greater precision. */
330 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
331 return build_type_attribute_variant (t1, attributes);
332 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
333 return build_type_attribute_variant (t2, attributes);
335 /* The types are the same; no need to do anything fancy. */
336 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
337 return build_type_attribute_variant (t1, attributes);
339 if (code1 != REAL_TYPE)
341 /* If one is unsigned long long, then convert the other to unsigned
343 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
344 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
345 return build_type_attribute_variant (long_long_unsigned_type_node,
347 /* If one is a long long, and the other is an unsigned long, and
348 long long can represent all the values of an unsigned long, then
349 convert to a long long. Otherwise, convert to an unsigned long
350 long. Otherwise, if either operand is long long, convert the
353 Since we're here, we know the TYPE_PRECISION is the same;
354 therefore converting to long long cannot represent all the values
355 of an unsigned long, so we choose unsigned long long in that
357 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
358 || same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
360 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
361 ? long_long_unsigned_type_node
362 : long_long_integer_type_node);
363 return build_type_attribute_variant (t, attributes);
366 /* Go through the same procedure, but for longs. */
367 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
368 || same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
369 return build_type_attribute_variant (long_unsigned_type_node,
371 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
372 || same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
374 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
375 ? long_unsigned_type_node : long_integer_type_node);
376 return build_type_attribute_variant (t, attributes);
379 /* For __intN types, either the type is __int128 (and is lower
380 priority than the types checked above, but higher than other
381 128-bit types) or it's known to not be the same size as other
382 types (enforced in toplev.c). Prefer the unsigned type. */
383 for (i = 0; i < NUM_INT_N_ENTS; i ++)
385 if (int_n_enabled_p [i]
386 && (same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].signed_type)
387 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].signed_type)
388 || same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].unsigned_type)
389 || same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].unsigned_type)))
391 tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
392 ? int_n_trees[i].unsigned_type
393 : int_n_trees[i].signed_type);
394 return build_type_attribute_variant (t, attributes);
398 /* Otherwise prefer the unsigned one. */
399 if (TYPE_UNSIGNED (t1))
400 return build_type_attribute_variant (t1, attributes);
402 return build_type_attribute_variant (t2, attributes);
406 if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
407 || same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
408 return build_type_attribute_variant (long_double_type_node,
410 if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
411 || same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
412 return build_type_attribute_variant (double_type_node,
414 if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
415 || same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
416 return build_type_attribute_variant (float_type_node,
419 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
420 the standard C++ floating-point types. Logic earlier in this
421 function has already eliminated the possibility that
422 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
423 compelling reason to choose one or the other. */
424 return build_type_attribute_variant (t1, attributes);
428 /* T1 and T2 are arithmetic or enumeration types. Return the type
429 that will result from the "usual arithmetic conversions" on T1 and
430 T2 as described in [expr]. */
433 type_after_usual_arithmetic_conversions (tree t1, tree t2)
435 gcc_assert (ARITHMETIC_TYPE_P (t1)
436 || VECTOR_TYPE_P (t1)
437 || UNSCOPED_ENUM_P (t1));
438 gcc_assert (ARITHMETIC_TYPE_P (t2)
439 || VECTOR_TYPE_P (t2)
440 || UNSCOPED_ENUM_P (t2));
442 /* Perform the integral promotions. We do not promote real types here. */
443 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
444 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
446 t1 = type_promotes_to (t1);
447 t2 = type_promotes_to (t2);
450 return cp_common_type (t1, t2);
454 composite_pointer_error (diagnostic_t kind, tree t1, tree t2,
455 composite_pointer_operation operation)
460 emit_diagnostic (kind, input_location, 0,
461 "comparison between "
462 "distinct pointer types %qT and %qT lacks a cast",
466 emit_diagnostic (kind, input_location, 0,
467 "conversion between "
468 "distinct pointer types %qT and %qT lacks a cast",
471 case CPO_CONDITIONAL_EXPR:
472 emit_diagnostic (kind, input_location, 0,
473 "conditional expression between "
474 "distinct pointer types %qT and %qT lacks a cast",
482 /* Subroutine of composite_pointer_type to implement the recursive
483 case. See that function for documentation of the parameters. */
486 composite_pointer_type_r (tree t1, tree t2,
487 composite_pointer_operation operation,
488 tsubst_flags_t complain)
495 /* Determine the types pointed to by T1 and T2. */
498 pointee1 = TREE_TYPE (t1);
499 pointee2 = TREE_TYPE (t2);
503 pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
504 pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
509 Otherwise, the composite pointer type is a pointer type
510 similar (_conv.qual_) to the type of one of the operands,
511 with a cv-qualification signature (_conv.qual_) that is the
512 union of the cv-qualification signatures of the operand
514 if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
515 result_type = pointee1;
516 else if ((TYPE_PTR_P (pointee1) && TYPE_PTR_P (pointee2))
517 || (TYPE_PTRMEM_P (pointee1) && TYPE_PTRMEM_P (pointee2)))
519 result_type = composite_pointer_type_r (pointee1, pointee2, operation,
521 if (result_type == error_mark_node)
522 return error_mark_node;
526 if (complain & tf_error)
527 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
529 return error_mark_node;
530 result_type = void_type_node;
532 result_type = cp_build_qualified_type (result_type,
533 (cp_type_quals (pointee1)
534 | cp_type_quals (pointee2)));
535 /* If the original types were pointers to members, so is the
537 if (TYPE_PTRMEM_P (t1))
539 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
540 TYPE_PTRMEM_CLASS_TYPE (t2)))
542 if (complain & tf_error)
543 composite_pointer_error (DK_PERMERROR, t1, t2, operation);
545 return error_mark_node;
547 result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
551 result_type = build_pointer_type (result_type);
553 /* Merge the attributes. */
554 attributes = (*targetm.merge_type_attributes) (t1, t2);
555 return build_type_attribute_variant (result_type, attributes);
558 /* Return the composite pointer type (see [expr.rel]) for T1 and T2.
559 ARG1 and ARG2 are the values with those types. The OPERATION is to
560 describe the operation between the pointer types,
561 in case an error occurs.
563 This routine also implements the computation of a common type for
564 pointers-to-members as per [expr.eq]. */
567 composite_pointer_type (tree t1, tree t2, tree arg1, tree arg2,
568 composite_pointer_operation operation,
569 tsubst_flags_t complain)
576 If one operand is a null pointer constant, the composite pointer
577 type is the type of the other operand. */
578 if (null_ptr_cst_p (arg1))
580 if (null_ptr_cst_p (arg2))
587 If one of the operands has type "pointer to cv1 void*", then
588 the other has type "pointer to cv2T", and the composite pointer
589 type is "pointer to cv12 void", where cv12 is the union of cv1
592 If either type is a pointer to void, make sure it is T1. */
593 if (TYPE_PTR_P (t2) && VOID_TYPE_P (TREE_TYPE (t2)))
596 /* Now, if T1 is a pointer to void, merge the qualifiers. */
597 if (TYPE_PTR_P (t1) && VOID_TYPE_P (TREE_TYPE (t1)))
602 if (TYPE_PTRFN_P (t2))
604 if (complain & tf_error)
609 pedwarn (input_location, OPT_Wpedantic,
610 "ISO C++ forbids comparison between pointer "
611 "of type %<void *%> and pointer-to-function");
614 pedwarn (input_location, OPT_Wpedantic,
615 "ISO C++ forbids conversion between pointer "
616 "of type %<void *%> and pointer-to-function");
618 case CPO_CONDITIONAL_EXPR:
619 pedwarn (input_location, OPT_Wpedantic,
620 "ISO C++ forbids conditional expression between "
621 "pointer of type %<void *%> and "
622 "pointer-to-function");
629 return error_mark_node;
632 = cp_build_qualified_type (void_type_node,
633 (cp_type_quals (TREE_TYPE (t1))
634 | cp_type_quals (TREE_TYPE (t2))));
635 result_type = build_pointer_type (result_type);
636 /* Merge the attributes. */
637 attributes = (*targetm.merge_type_attributes) (t1, t2);
638 return build_type_attribute_variant (result_type, attributes);
641 if (c_dialect_objc () && TYPE_PTR_P (t1)
644 if (objc_have_common_type (t1, t2, -3, NULL_TREE))
645 return objc_common_type (t1, t2);
648 /* [expr.eq] permits the application of a pointer conversion to
649 bring the pointers to a common type. */
650 if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2)
651 && CLASS_TYPE_P (TREE_TYPE (t1))
652 && CLASS_TYPE_P (TREE_TYPE (t2))
653 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
656 class1 = TREE_TYPE (t1);
657 class2 = TREE_TYPE (t2);
659 if (DERIVED_FROM_P (class1, class2))
660 t2 = (build_pointer_type
661 (cp_build_qualified_type (class1, cp_type_quals (class2))));
662 else if (DERIVED_FROM_P (class2, class1))
663 t1 = (build_pointer_type
664 (cp_build_qualified_type (class2, cp_type_quals (class1))));
667 if (complain & tf_error)
668 composite_pointer_error (DK_ERROR, t1, t2, operation);
669 return error_mark_node;
672 /* [expr.eq] permits the application of a pointer-to-member
673 conversion to change the class type of one of the types. */
674 else if (TYPE_PTRMEM_P (t1)
675 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
676 TYPE_PTRMEM_CLASS_TYPE (t2)))
678 class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
679 class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
681 if (DERIVED_FROM_P (class1, class2))
682 t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
683 else if (DERIVED_FROM_P (class2, class1))
684 t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
687 if (complain & tf_error)
691 error ("comparison between distinct "
692 "pointer-to-member types %qT and %qT lacks a cast",
696 error ("conversion between distinct "
697 "pointer-to-member types %qT and %qT lacks a cast",
700 case CPO_CONDITIONAL_EXPR:
701 error ("conditional expression between distinct "
702 "pointer-to-member types %qT and %qT lacks a cast",
708 return error_mark_node;
711 else if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2)
712 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (t1))
713 && TREE_CODE (TREE_TYPE (t2)) == TREE_CODE (TREE_TYPE (t1)))
715 /* ...if T1 is "pointer to transaction_safe function" and T2 is "pointer
716 to function", where the function types are otherwise the same, T2, and
718 tree f1 = TREE_TYPE (t1);
719 tree f2 = TREE_TYPE (t2);
720 bool safe1 = tx_safe_fn_type_p (f1);
721 bool safe2 = tx_safe_fn_type_p (f2);
723 t1 = build_pointer_type (tx_unsafe_fn_variant (f1));
724 else if (safe2 && !safe1)
725 t2 = build_pointer_type (tx_unsafe_fn_variant (f2));
728 return composite_pointer_type_r (t1, t2, operation, complain);
731 /* Return the merged type of two types.
732 We assume that comptypes has already been done and returned 1;
733 if that isn't so, this may crash.
735 This just combines attributes and default arguments; any other
736 differences would cause the two types to compare unalike. */
739 merge_types (tree t1, tree t2)
741 enum tree_code code1;
742 enum tree_code code2;
745 /* Save time if the two types are the same. */
748 if (original_type (t1) == original_type (t2))
751 /* If one type is nonsense, use the other. */
752 if (t1 == error_mark_node)
754 if (t2 == error_mark_node)
757 /* Handle merging an auto redeclaration with a previous deduced
762 /* Merge the attributes. */
763 attributes = (*targetm.merge_type_attributes) (t1, t2);
765 if (TYPE_PTRMEMFUNC_P (t1))
766 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
767 if (TYPE_PTRMEMFUNC_P (t2))
768 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
770 code1 = TREE_CODE (t1);
771 code2 = TREE_CODE (t2);
774 gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
775 if (code1 == TYPENAME_TYPE)
777 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
778 code1 = TREE_CODE (t1);
782 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
783 code2 = TREE_CODE (t2);
791 /* For two pointers, do this recursively on the target type. */
793 tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
794 int quals = cp_type_quals (t1);
796 if (code1 == POINTER_TYPE)
798 t1 = build_pointer_type (target);
799 if (TREE_CODE (target) == METHOD_TYPE)
800 t1 = build_ptrmemfunc_type (t1);
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);
814 quals = cp_type_quals (t1);
815 pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
816 TYPE_PTRMEM_POINTED_TO_TYPE (t2));
817 t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
819 t1 = cp_build_qualified_type (t1, quals);
825 tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
826 /* Save space: see if the result is identical to one of the args. */
827 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
828 return build_type_attribute_variant (t1, attributes);
829 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
830 return build_type_attribute_variant (t2, attributes);
831 /* Merge the element types, and have a size if either arg has one. */
832 t1 = build_cplus_array_type
833 (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
838 /* Function types: prefer the one that specified arg types.
839 If both do, merge the arg types. Also merge the return types. */
841 tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
842 tree p1 = TYPE_ARG_TYPES (t1);
843 tree p2 = TYPE_ARG_TYPES (t2);
846 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
848 /* Save space: see if the result is identical to one of the args. */
849 if (valtype == TREE_TYPE (t1) && ! p2)
850 return cp_build_type_attribute_variant (t1, attributes);
851 if (valtype == TREE_TYPE (t2) && ! p1)
852 return cp_build_type_attribute_variant (t2, attributes);
854 /* Simple way if one arg fails to specify argument types. */
855 if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
857 else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
860 parms = commonparms (p1, p2);
862 rval = build_function_type (valtype, parms);
863 gcc_assert (type_memfn_quals (t1) == type_memfn_quals (t2));
864 gcc_assert (type_memfn_rqual (t1) == type_memfn_rqual (t2));
865 rval = apply_memfn_quals (rval,
866 type_memfn_quals (t1),
867 type_memfn_rqual (t1));
868 raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
869 TYPE_RAISES_EXCEPTIONS (t2));
870 t1 = build_exception_variant (rval, raises);
871 if (late_return_type_p)
872 TYPE_HAS_LATE_RETURN_TYPE (t1) = 1;
878 /* Get this value the long way, since TYPE_METHOD_BASETYPE
879 is just the main variant of this. */
880 tree basetype = class_of_this_parm (t2);
881 tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
882 TYPE_RAISES_EXCEPTIONS (t2));
883 cp_ref_qualifier rqual = type_memfn_rqual (t1);
885 bool late_return_type_1_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
886 bool late_return_type_2_p = TYPE_HAS_LATE_RETURN_TYPE (t2);
888 /* If this was a member function type, get back to the
889 original type of type member function (i.e., without
890 the class instance variable up front. */
891 t1 = build_function_type (TREE_TYPE (t1),
892 TREE_CHAIN (TYPE_ARG_TYPES (t1)));
893 t2 = build_function_type (TREE_TYPE (t2),
894 TREE_CHAIN (TYPE_ARG_TYPES (t2)));
895 t3 = merge_types (t1, t2);
896 t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
897 TYPE_ARG_TYPES (t3));
898 t1 = build_exception_variant (t3, raises);
899 t1 = build_ref_qualified_type (t1, rqual);
900 if (late_return_type_1_p)
901 TYPE_HAS_LATE_RETURN_TYPE (t1) = 1;
902 if (late_return_type_2_p)
903 TYPE_HAS_LATE_RETURN_TYPE (t2) = 1;
908 /* There is no need to merge attributes into a TYPENAME_TYPE.
909 When the type is instantiated it will have whatever
910 attributes result from the instantiation. */
916 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
918 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
921 return cp_build_type_attribute_variant (t1, attributes);
924 /* Return the ARRAY_TYPE type without its domain. */
927 strip_array_domain (tree type)
930 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
931 if (TYPE_DOMAIN (type) == NULL_TREE)
933 t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
934 return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
937 /* Wrapper around cp_common_type that is used by c-common.c and other
938 front end optimizations that remove promotions.
940 Return the common type for two arithmetic types T1 and T2 under the
941 usual arithmetic conversions. The default conversions have already
942 been applied, and enumerated types converted to their compatible
946 common_type (tree t1, tree t2)
948 /* If one type is nonsense, use the other */
949 if (t1 == error_mark_node)
951 if (t2 == error_mark_node)
954 return cp_common_type (t1, t2);
957 /* Return the common type of two pointer types T1 and T2. This is the
958 type for the result of most arithmetic operations if the operands
959 have the given two types.
961 We assume that comp_target_types has already been done and returned
962 nonzero; if that isn't so, this may crash. */
965 common_pointer_type (tree t1, tree t2)
967 gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
968 || (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
969 || (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
971 return composite_pointer_type (t1, t2, error_mark_node, error_mark_node,
972 CPO_CONVERSION, tf_warning_or_error);
975 /* Compare two exception specifier types for exactness or subsetness, if
976 allowed. Returns false for mismatch, true for match (same, or
979 [except.spec] "If a class X ... objects of class X or any class publicly
980 and unambiguously derived from X. Similarly, if a pointer type Y * ...
981 exceptions of type Y * or that are pointers to any type publicly and
982 unambiguously derived from Y. Otherwise a function only allows exceptions
983 that have the same type ..."
984 This does not mention cv qualifiers and is different to what throw
985 [except.throw] and catch [except.catch] will do. They will ignore the
986 top level cv qualifiers, and allow qualifiers in the pointer to class
989 We implement the letter of the standard. */
992 comp_except_types (tree a, tree b, bool exact)
994 if (same_type_p (a, b))
998 if (cp_type_quals (a) || cp_type_quals (b))
1001 if (TYPE_PTR_P (a) && TYPE_PTR_P (b))
1005 if (cp_type_quals (a) || cp_type_quals (b))
1009 if (TREE_CODE (a) != RECORD_TYPE
1010 || TREE_CODE (b) != RECORD_TYPE)
1013 if (publicly_uniquely_derived_p (a, b))
1019 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
1020 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
1021 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
1022 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
1023 are unordered, but we've already filtered out duplicates. Most lists will
1024 be in order, we should try to make use of that. */
1027 comp_except_specs (const_tree t1, const_tree t2, int exact)
1036 /* First handle noexcept. */
1037 if (exact < ce_exact)
1039 /* noexcept(false) is compatible with no exception-specification,
1040 and stricter than any spec. */
1041 if (t1 == noexcept_false_spec)
1042 return t2 == NULL_TREE || exact == ce_derived;
1043 /* Even a derived noexcept(false) is compatible with no
1044 exception-specification. */
1045 if (t2 == noexcept_false_spec)
1046 return t1 == NULL_TREE;
1048 /* Otherwise, if we aren't looking for an exact match, noexcept is
1049 equivalent to throw(). */
1050 if (t1 == noexcept_true_spec)
1051 t1 = empty_except_spec;
1052 if (t2 == noexcept_true_spec)
1053 t2 = empty_except_spec;
1056 /* If any noexcept is left, it is only comparable to itself;
1057 either we're looking for an exact match or we're redeclaring a
1058 template with dependent noexcept. */
1059 if ((t1 && TREE_PURPOSE (t1))
1060 || (t2 && TREE_PURPOSE (t2)))
1062 && cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)));
1064 if (t1 == NULL_TREE) /* T1 is ... */
1065 return t2 == NULL_TREE || exact == ce_derived;
1066 if (!TREE_VALUE (t1)) /* t1 is EMPTY */
1067 return t2 != NULL_TREE && !TREE_VALUE (t2);
1068 if (t2 == NULL_TREE) /* T2 is ... */
1070 if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
1071 return exact == ce_derived;
1073 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1074 Count how many we find, to determine exactness. For exact matching and
1075 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1077 for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
1079 for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
1081 tree a = TREE_VALUE (probe);
1082 tree b = TREE_VALUE (t2);
1084 if (comp_except_types (a, b, exact))
1086 if (probe == base && exact > ce_derived)
1087 base = TREE_CHAIN (probe);
1092 if (probe == NULL_TREE)
1095 return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
1098 /* Compare the array types T1 and T2. ALLOW_REDECLARATION is true if
1099 [] can match [size]. */
1102 comp_array_types (const_tree t1, const_tree t2, bool allow_redeclaration)
1111 /* The type of the array elements must be the same. */
1112 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1115 d1 = TYPE_DOMAIN (t1);
1116 d2 = TYPE_DOMAIN (t2);
1121 /* If one of the arrays is dimensionless, and the other has a
1122 dimension, they are of different types. However, it is valid to
1130 declarations for an array object can specify
1131 array types that differ by the presence or absence of a major
1132 array bound (_dcl.array_). */
1134 return allow_redeclaration;
1136 /* Check that the dimensions are the same. */
1138 if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
1140 max1 = TYPE_MAX_VALUE (d1);
1141 max2 = TYPE_MAX_VALUE (d2);
1143 if (!cp_tree_equal (max1, max2))
1149 /* Compare the relative position of T1 and T2 into their respective
1150 template parameter list.
1151 T1 and T2 must be template parameter types.
1152 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1155 comp_template_parms_position (tree t1, tree t2)
1157 tree index1, index2;
1158 gcc_assert (t1 && t2
1159 && TREE_CODE (t1) == TREE_CODE (t2)
1160 && (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
1161 || TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
1162 || TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
1164 index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
1165 index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
1167 /* Then compare their relative position. */
1168 if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
1169 || TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
1170 || (TEMPLATE_PARM_PARAMETER_PACK (index1)
1171 != TEMPLATE_PARM_PARAMETER_PACK (index2)))
1174 /* In C++14 we can end up comparing 'auto' to a normal template
1175 parameter. Don't confuse them. */
1176 if (cxx_dialect >= cxx14 && (is_auto (t1) || is_auto (t2)))
1177 return TYPE_IDENTIFIER (t1) == TYPE_IDENTIFIER (t2);
1182 /* Subroutine in comptypes. */
1185 structural_comptypes (tree t1, tree t2, int strict)
1190 /* Suppress errors caused by previously reported errors. */
1191 if (t1 == error_mark_node || t2 == error_mark_node)
1194 gcc_assert (TYPE_P (t1) && TYPE_P (t2));
1196 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1197 current instantiation. */
1198 if (TREE_CODE (t1) == TYPENAME_TYPE)
1199 t1 = resolve_typename_type (t1, /*only_current_p=*/true);
1201 if (TREE_CODE (t2) == TYPENAME_TYPE)
1202 t2 = resolve_typename_type (t2, /*only_current_p=*/true);
1204 if (TYPE_PTRMEMFUNC_P (t1))
1205 t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
1206 if (TYPE_PTRMEMFUNC_P (t2))
1207 t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
1209 /* Different classes of types can't be compatible. */
1210 if (TREE_CODE (t1) != TREE_CODE (t2))
1213 /* Qualifiers must match. For array types, we will check when we
1214 recur on the array element types. */
1215 if (TREE_CODE (t1) != ARRAY_TYPE
1216 && cp_type_quals (t1) != cp_type_quals (t2))
1218 if (TREE_CODE (t1) == FUNCTION_TYPE
1219 && type_memfn_quals (t1) != type_memfn_quals (t2))
1221 /* Need to check this before TYPE_MAIN_VARIANT.
1222 FIXME function qualifiers should really change the main variant. */
1223 if ((TREE_CODE (t1) == FUNCTION_TYPE
1224 || TREE_CODE (t1) == METHOD_TYPE)
1225 && type_memfn_rqual (t1) != type_memfn_rqual (t2))
1227 if (TYPE_FOR_JAVA (t1) != TYPE_FOR_JAVA (t2))
1230 /* Allow for two different type nodes which have essentially the same
1231 definition. Note that we already checked for equality of the type
1232 qualifiers (just above). */
1234 if (TREE_CODE (t1) != ARRAY_TYPE
1235 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
1239 /* Compare the types. Break out if they could be the same. */
1240 switch (TREE_CODE (t1))
1244 /* All void and bool types are the same. */
1248 case FIXED_POINT_TYPE:
1250 /* With these nodes, we can't determine type equivalence by
1251 looking at what is stored in the nodes themselves, because
1252 two nodes might have different TYPE_MAIN_VARIANTs but still
1253 represent the same type. For example, wchar_t and int could
1254 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1255 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1256 and are distinct types. On the other hand, int and the
1259 typedef int INT __attribute((may_alias));
1261 have identical properties, different TYPE_MAIN_VARIANTs, but
1262 represent the same type. The canonical type system keeps
1263 track of equivalence in this case, so we fall back on it. */
1264 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1266 case TEMPLATE_TEMPLATE_PARM:
1267 case BOUND_TEMPLATE_TEMPLATE_PARM:
1268 if (!comp_template_parms_position (t1, t2))
1270 if (!comp_template_parms
1271 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
1272 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
1274 if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
1276 /* Don't check inheritance. */
1277 strict = COMPARE_STRICT;
1282 if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
1283 && (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
1284 || TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
1285 && comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
1288 if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
1290 else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
1296 if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
1297 strict & ~COMPARE_REDECLARATION))
1299 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1303 case REFERENCE_TYPE:
1304 if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
1306 /* fall through to checks for pointer types */
1309 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1310 || !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1316 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1318 if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
1323 /* Target types must match incl. qualifiers. */
1324 if (!comp_array_types (t1, t2, !!(strict & COMPARE_REDECLARATION)))
1328 case TEMPLATE_TYPE_PARM:
1329 /* If T1 and T2 don't have the same relative position in their
1330 template parameters set, they can't be equal. */
1331 if (!comp_template_parms_position (t1, t2))
1333 /* Constrained 'auto's are distinct from parms that don't have the same
1335 if (!equivalent_placeholder_constraints (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 if (flag_checking && USE_CANONICAL_TYPES)
1420 bool result = structural_comptypes (t1, t2, strict);
1422 if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
1423 /* The two types are structurally equivalent, but their
1424 canonical types were different. This is a failure of the
1425 canonical type propagation code.*/
1427 ("canonical types differ for identical types %T and %T",
1429 else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
1430 /* Two types are structurally different, but the canonical
1431 types are the same. This means we were over-eager in
1432 assigning canonical types. */
1434 ("same canonical type node for different types %T and %T",
1439 if (!flag_checking && USE_CANONICAL_TYPES)
1440 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1442 return structural_comptypes (t1, t2, strict);
1444 else if (strict == COMPARE_STRUCTURAL)
1445 return structural_comptypes (t1, t2, COMPARE_STRICT);
1447 return structural_comptypes (t1, t2, strict);
1450 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1451 top-level qualifiers. */
1454 same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
1456 if (type1 == error_mark_node || type2 == error_mark_node)
1459 return same_type_p (TYPE_MAIN_VARIANT (type1), TYPE_MAIN_VARIANT (type2));
1462 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1465 at_least_as_qualified_p (const_tree type1, const_tree type2)
1467 int q1 = cp_type_quals (type1);
1468 int q2 = cp_type_quals (type2);
1470 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1471 return (q1 & q2) == q2;
1474 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1475 more cv-qualified that TYPE1, and 0 otherwise. */
1478 comp_cv_qualification (int q1, int q2)
1483 if ((q1 & q2) == q2)
1485 else if ((q1 & q2) == q1)
1492 comp_cv_qualification (const_tree type1, const_tree type2)
1494 int q1 = cp_type_quals (type1);
1495 int q2 = cp_type_quals (type2);
1496 return comp_cv_qualification (q1, q2);
1499 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1500 subset of the cv-qualification signature of TYPE2, and the types
1501 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
1504 comp_cv_qual_signature (tree type1, tree type2)
1506 if (comp_ptr_ttypes_real (type2, type1, -1))
1508 else if (comp_ptr_ttypes_real (type1, type2, -1))
1514 /* Subroutines of `comptypes'. */
1516 /* Return true if two parameter type lists PARMS1 and PARMS2 are
1517 equivalent in the sense that functions with those parameter types
1518 can have equivalent types. The two lists must be equivalent,
1519 element by element. */
1522 compparms (const_tree parms1, const_tree parms2)
1526 /* An unspecified parmlist matches any specified parmlist
1527 whose argument types don't need default promotions. */
1529 for (t1 = parms1, t2 = parms2;
1531 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1533 /* If one parmlist is shorter than the other,
1534 they fail to match. */
1537 if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1544 /* Process a sizeof or alignof expression where the operand is a
1548 cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool complain)
1553 gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
1554 if (type == error_mark_node)
1555 return error_mark_node;
1557 type = non_reference (type);
1558 if (TREE_CODE (type) == METHOD_TYPE)
1561 pedwarn (input_location, OPT_Wpointer_arith,
1562 "invalid application of %qs to a member function",
1563 operator_name_info[(int) op].name);
1565 return error_mark_node;
1566 value = size_one_node;
1569 dependent_p = dependent_type_p (type);
1571 complete_type (type);
1573 /* VLA types will have a non-constant size. In the body of an
1574 uninstantiated template, we don't need to try to compute the
1575 value, because the sizeof expression is not an integral
1576 constant expression in that case. And, if we do try to
1577 compute the value, we'll likely end up with SAVE_EXPRs, which
1578 the template substitution machinery does not expect to see. */
1579 || (processing_template_decl
1580 && COMPLETE_TYPE_P (type)
1581 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
1583 value = build_min (op, size_type_node, type);
1584 TREE_READONLY (value) = 1;
1588 return c_sizeof_or_alignof_type (input_location, complete_type (type),
1589 op == SIZEOF_EXPR, false,
1593 /* Return the size of the type, without producing any warnings for
1594 types whose size cannot be taken. This routine should be used only
1595 in some other routine that has already produced a diagnostic about
1596 using the size of such a type. */
1598 cxx_sizeof_nowarn (tree type)
1600 if (TREE_CODE (type) == FUNCTION_TYPE
1601 || VOID_TYPE_P (type)
1602 || TREE_CODE (type) == ERROR_MARK)
1603 return size_one_node;
1604 else if (!COMPLETE_TYPE_P (type))
1605 return size_zero_node;
1607 return cxx_sizeof_or_alignof_type (type, SIZEOF_EXPR, false);
1610 /* Process a sizeof expression where the operand is an expression. */
1613 cxx_sizeof_expr (tree e, tsubst_flags_t complain)
1615 if (e == error_mark_node)
1616 return error_mark_node;
1618 if (processing_template_decl)
1620 e = build_min (SIZEOF_EXPR, size_type_node, e);
1621 TREE_SIDE_EFFECTS (e) = 0;
1622 TREE_READONLY (e) = 1;
1627 /* To get the size of a static data member declared as an array of
1628 unknown bound, we need to instantiate it. */
1630 && VAR_HAD_UNKNOWN_BOUND (e)
1631 && DECL_TEMPLATE_INSTANTIATION (e))
1632 instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false);
1634 if (TREE_CODE (e) == PARM_DECL
1635 && DECL_ARRAY_PARAMETER_P (e)
1636 && (complain & tf_warning))
1638 if (warning (OPT_Wsizeof_array_argument, "%<sizeof%> on array function "
1639 "parameter %qE will return size of %qT", e, TREE_TYPE (e)))
1640 inform (DECL_SOURCE_LOCATION (e), "declared here");
1643 e = mark_type_use (e);
1645 if (TREE_CODE (e) == COMPONENT_REF
1646 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1647 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1649 if (complain & tf_error)
1650 error ("invalid application of %<sizeof%> to a bit-field");
1652 return error_mark_node;
1655 else if (is_overloaded_fn (e))
1657 if (complain & tf_error)
1658 permerror (input_location, "ISO C++ forbids applying %<sizeof%> to an expression of "
1661 return error_mark_node;
1664 else if (type_unknown_p (e))
1666 if (complain & tf_error)
1667 cxx_incomplete_type_error (e, TREE_TYPE (e));
1669 return error_mark_node;
1675 return cxx_sizeof_or_alignof_type (e, SIZEOF_EXPR, complain & tf_error);
1678 /* Implement the __alignof keyword: Return the minimum required
1679 alignment of E, measured in bytes. For VAR_DECL's and
1680 FIELD_DECL's return DECL_ALIGN (which can be set from an
1681 "aligned" __attribute__ specification). */
1684 cxx_alignof_expr (tree e, tsubst_flags_t complain)
1688 if (e == error_mark_node)
1689 return error_mark_node;
1691 if (processing_template_decl)
1693 e = build_min (ALIGNOF_EXPR, size_type_node, e);
1694 TREE_SIDE_EFFECTS (e) = 0;
1695 TREE_READONLY (e) = 1;
1700 e = mark_type_use (e);
1703 t = size_int (DECL_ALIGN_UNIT (e));
1704 else if (TREE_CODE (e) == COMPONENT_REF
1705 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL
1706 && DECL_C_BIT_FIELD (TREE_OPERAND (e, 1)))
1708 if (complain & tf_error)
1709 error ("invalid application of %<__alignof%> to a bit-field");
1711 return error_mark_node;
1714 else if (TREE_CODE (e) == COMPONENT_REF
1715 && TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
1716 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
1717 else if (is_overloaded_fn (e))
1719 if (complain & tf_error)
1720 permerror (input_location, "ISO C++ forbids applying %<__alignof%> to an expression of "
1723 return error_mark_node;
1724 if (TREE_CODE (e) == FUNCTION_DECL)
1725 t = size_int (DECL_ALIGN_UNIT (e));
1729 else if (type_unknown_p (e))
1731 if (complain & tf_error)
1732 cxx_incomplete_type_error (e, TREE_TYPE (e));
1734 return error_mark_node;
1738 return cxx_sizeof_or_alignof_type (TREE_TYPE (e), ALIGNOF_EXPR,
1739 complain & tf_error);
1741 return fold_convert (size_type_node, t);
1744 /* Process a sizeof or alignof expression E with code OP where the operand
1745 is an expression. */
1748 cxx_sizeof_or_alignof_expr (tree e, enum tree_code op, bool complain)
1750 if (op == SIZEOF_EXPR)
1751 return cxx_sizeof_expr (e, complain? tf_warning_or_error : tf_none);
1753 return cxx_alignof_expr (e, complain? tf_warning_or_error : tf_none);
1756 /* Build a representation of an expression 'alignas(E).' Return the
1757 folded integer value of E if it is an integral constant expression
1758 that resolves to a valid alignment. If E depends on a template
1759 parameter, return a syntactic representation tree of kind
1760 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
1761 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
1764 cxx_alignas_expr (tree e)
1766 if (e == NULL_TREE || e == error_mark_node
1767 || (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e)))
1773 When the alignment-specifier is of the form
1774 alignas(type-id ), it shall have the same effect as
1775 alignas(alignof(type-id )). */
1777 return cxx_sizeof_or_alignof_type (e, ALIGNOF_EXPR, false);
1779 /* If we reach this point, it means the alignas expression if of
1780 the form "alignas(assignment-expression)", so we should follow
1781 what is stated by [dcl.align]/2. */
1783 if (value_dependent_expression_p (e))
1784 /* Leave value-dependent expression alone for now. */
1787 e = instantiate_non_dependent_expr (e);
1788 e = mark_rvalue_use (e);
1790 /* [dcl.align]/2 says:
1792 the assignment-expression shall be an integral constant
1795 return cxx_constant_value (e);
1799 /* EXPR is being used in a context that is not a function call.
1804 The expression can be used only as the left-hand operand of a
1805 member function call.
1807 [expr.mptr.operator]
1809 If the result of .* or ->* is a function, then that result can be
1810 used only as the operand for the function call operator ().
1812 by issuing an error message if appropriate. Returns true iff EXPR
1813 violates these rules. */
1816 invalid_nonstatic_memfn_p (location_t loc, tree expr, tsubst_flags_t complain)
1818 if (expr == NULL_TREE)
1820 /* Don't enforce this in MS mode. */
1821 if (flag_ms_extensions)
1823 if (is_overloaded_fn (expr) && !really_overloaded_fn (expr))
1824 expr = get_first_fn (expr);
1825 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
1827 if (complain & tf_error)
1831 error_at (loc, "invalid use of non-static member function %qD",
1833 inform (DECL_SOURCE_LOCATION (expr), "declared here");
1836 error_at (loc, "invalid use of non-static member function of "
1837 "type %qT", TREE_TYPE (expr));
1844 /* If EXP is a reference to a bitfield, and the type of EXP does not
1845 match the declared type of the bitfield, return the declared type
1846 of the bitfield. Otherwise, return NULL_TREE. */
1849 is_bitfield_expr_with_lowered_type (const_tree exp)
1851 switch (TREE_CODE (exp))
1854 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
1855 ? TREE_OPERAND (exp, 1)
1856 : TREE_OPERAND (exp, 0)))
1858 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
1861 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
1865 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1871 field = TREE_OPERAND (exp, 1);
1872 if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
1874 if (same_type_ignoring_top_level_qualifiers_p
1875 (TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
1877 return DECL_BIT_FIELD_TYPE (field);
1881 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (exp, 0)))
1882 == TYPE_MAIN_VARIANT (TREE_TYPE (exp)))
1883 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
1891 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
1892 bitfield with a lowered type, the type of EXP is returned, rather
1896 unlowered_expr_type (const_tree exp)
1899 tree etype = TREE_TYPE (exp);
1901 type = is_bitfield_expr_with_lowered_type (exp);
1903 type = cp_build_qualified_type (type, cp_type_quals (etype));
1910 /* Perform the conversions in [expr] that apply when an lvalue appears
1911 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
1912 function-to-pointer conversions. In addition, bitfield references are
1913 converted to their declared types. Note that this function does not perform
1914 the lvalue-to-rvalue conversion for class types. If you need that conversion
1915 for class types, then you probably need to use force_rvalue.
1917 Although the returned value is being used as an rvalue, this
1918 function does not wrap the returned expression in a
1919 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
1920 that the return value is no longer an lvalue. */
1923 decay_conversion (tree exp,
1924 tsubst_flags_t complain,
1925 bool reject_builtin /* = true */)
1928 enum tree_code code;
1929 location_t loc = EXPR_LOC_OR_LOC (exp, input_location);
1931 type = TREE_TYPE (exp);
1932 if (type == error_mark_node)
1933 return error_mark_node;
1935 exp = resolve_nondeduced_context (exp, complain);
1936 if (type_unknown_p (exp))
1938 if (complain & tf_error)
1939 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
1940 return error_mark_node;
1943 code = TREE_CODE (type);
1945 if (error_operand_p (exp))
1946 return error_mark_node;
1948 if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
1949 return nullptr_node;
1951 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
1952 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
1953 if (code == VOID_TYPE)
1955 if (complain & tf_error)
1956 error_at (loc, "void value not ignored as it ought to be");
1957 return error_mark_node;
1959 if (invalid_nonstatic_memfn_p (loc, exp, complain))
1960 return error_mark_node;
1961 if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
1963 exp = mark_lvalue_use (exp);
1964 if (reject_builtin && reject_gcc_builtin (exp, loc))
1965 return error_mark_node;
1966 return cp_build_addr_expr (exp, complain);
1968 if (code == ARRAY_TYPE)
1973 exp = mark_lvalue_use (exp);
1975 if (INDIRECT_REF_P (exp))
1976 return build_nop (build_pointer_type (TREE_TYPE (type)),
1977 TREE_OPERAND (exp, 0));
1979 if (TREE_CODE (exp) == COMPOUND_EXPR)
1981 tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
1982 if (op1 == error_mark_node)
1983 return error_mark_node;
1984 return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
1985 TREE_OPERAND (exp, 0), op1);
1989 && ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
1991 if (complain & tf_error)
1992 error_at (loc, "invalid use of non-lvalue array");
1993 return error_mark_node;
1996 /* Don't let an array compound literal decay to a pointer. It can
1997 still be used to initialize an array or bind to a reference. */
1998 if (TREE_CODE (exp) == TARGET_EXPR)
2000 if (complain & tf_error)
2001 error_at (loc, "taking address of temporary array");
2002 return error_mark_node;
2005 ptrtype = build_pointer_type (TREE_TYPE (type));
2009 if (!cxx_mark_addressable (exp))
2010 return error_mark_node;
2011 adr = build_nop (ptrtype, build_address (exp));
2014 /* This way is better for a COMPONENT_REF since it can
2015 simplify the offset for a component. */
2016 adr = cp_build_addr_expr (exp, complain);
2017 return cp_convert (ptrtype, adr, complain);
2020 /* Otherwise, it's the lvalue-to-rvalue conversion. */
2021 exp = mark_rvalue_use (exp, loc, reject_builtin);
2023 /* If a bitfield is used in a context where integral promotion
2024 applies, then the caller is expected to have used
2025 default_conversion. That function promotes bitfields correctly
2026 before calling this function. At this point, if we have a
2027 bitfield referenced, we may assume that is not subject to
2028 promotion, and that, therefore, the type of the resulting rvalue
2029 is the declared type of the bitfield. */
2030 exp = convert_bitfield_to_declared_type (exp);
2032 /* We do not call rvalue() here because we do not want to wrap EXP
2033 in a NON_LVALUE_EXPR. */
2037 Non-class rvalues always have cv-unqualified types. */
2038 type = TREE_TYPE (exp);
2039 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
2040 exp = build_nop (cv_unqualified (type), exp);
2042 if (!complete_type_or_maybe_complain (type, exp, complain))
2043 return error_mark_node;
2048 /* Perform preparatory conversions, as part of the "usual arithmetic
2049 conversions". In particular, as per [expr]:
2051 Whenever an lvalue expression appears as an operand of an
2052 operator that expects the rvalue for that operand, the
2053 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2054 standard conversions are applied to convert the expression to an
2057 In addition, we perform integral promotions here, as those are
2058 applied to both operands to a binary operator before determining
2059 what additional conversions should apply. */
2062 cp_default_conversion (tree exp, tsubst_flags_t complain)
2064 /* Check for target-specific promotions. */
2065 tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
2067 exp = cp_convert (promoted_type, exp, complain);
2068 /* Perform the integral promotions first so that bitfield
2069 expressions (which may promote to "int", even if the bitfield is
2070 declared "unsigned") are promoted correctly. */
2071 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
2072 exp = cp_perform_integral_promotions (exp, complain);
2073 /* Perform the other conversions. */
2074 exp = decay_conversion (exp, complain);
2082 default_conversion (tree exp)
2084 return cp_default_conversion (exp, tf_warning_or_error);
2087 /* EXPR is an expression with an integral or enumeration type.
2088 Perform the integral promotions in [conv.prom], and return the
2092 cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
2097 expr = mark_rvalue_use (expr);
2101 If the bitfield has an enumerated type, it is treated as any
2102 other value of that type for promotion purposes. */
2103 type = is_bitfield_expr_with_lowered_type (expr);
2104 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
2105 type = TREE_TYPE (expr);
2106 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
2107 /* Scoped enums don't promote. */
2108 if (SCOPED_ENUM_P (type))
2110 promoted_type = type_promotes_to (type);
2111 if (type != promoted_type)
2112 expr = cp_convert (promoted_type, expr, complain);
2119 perform_integral_promotions (tree expr)
2121 return cp_perform_integral_promotions (expr, tf_warning_or_error);
2124 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2125 decay_conversion to one. */
2128 string_conv_p (const_tree totype, const_tree exp, int warn)
2132 if (!TYPE_PTR_P (totype))
2135 t = TREE_TYPE (totype);
2136 if (!same_type_p (t, char_type_node)
2137 && !same_type_p (t, char16_type_node)
2138 && !same_type_p (t, char32_type_node)
2139 && !same_type_p (t, wchar_type_node))
2142 if (TREE_CODE (exp) == STRING_CST)
2144 /* Make sure that we don't try to convert between char and wide chars. */
2145 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
2150 /* Is this a string constant which has decayed to 'const char *'? */
2151 t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
2152 if (!same_type_p (TREE_TYPE (exp), t))
2155 if (TREE_CODE (exp) != ADDR_EXPR
2156 || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
2161 if (cxx_dialect >= cxx11)
2162 pedwarn (input_location,
2163 pedantic ? OPT_Wpedantic : OPT_Wwrite_strings,
2164 "ISO C++ forbids converting a string constant to %qT",
2167 warning (OPT_Wwrite_strings,
2168 "deprecated conversion from string constant to %qT",
2175 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2176 can, for example, use as an lvalue. This code used to be in
2177 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2178 expressions, where we're dealing with aggregates. But now it's again only
2179 called from unary_complex_lvalue. The case (in particular) that led to
2180 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2184 rationalize_conditional_expr (enum tree_code code, tree t,
2185 tsubst_flags_t complain)
2187 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
2189 /* For MIN_EXPR or MAX_EXPR, fold-const.c has arranged things so that
2190 the first operand is always the one to be used if both operands
2191 are equal, so we know what conditional expression this used to be. */
2192 if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
2194 tree op0 = TREE_OPERAND (t, 0);
2195 tree op1 = TREE_OPERAND (t, 1);
2197 /* The following code is incorrect if either operand side-effects. */
2198 gcc_assert (!TREE_SIDE_EFFECTS (op0)
2199 && !TREE_SIDE_EFFECTS (op1));
2201 build_conditional_expr (loc,
2202 build_x_binary_op (loc,
2203 (TREE_CODE (t) == MIN_EXPR
2204 ? LE_EXPR : GE_EXPR),
2205 op0, TREE_CODE (op0),
2206 op1, TREE_CODE (op1),
2209 cp_build_unary_op (code, op0, 0, complain),
2210 cp_build_unary_op (code, op1, 0, complain),
2215 build_conditional_expr (loc, TREE_OPERAND (t, 0),
2216 cp_build_unary_op (code, TREE_OPERAND (t, 1), 0,
2218 cp_build_unary_op (code, TREE_OPERAND (t, 2), 0,
2223 /* Given the TYPE of an anonymous union field inside T, return the
2224 FIELD_DECL for the field. If not found return NULL_TREE. Because
2225 anonymous unions can nest, we must also search all anonymous unions
2226 that are directly reachable. */
2229 lookup_anon_field (tree t, tree type)
2233 t = TYPE_MAIN_VARIANT (t);
2235 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2237 if (TREE_STATIC (field))
2239 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
2242 /* If we find it directly, return the field. */
2243 if (DECL_NAME (field) == NULL_TREE
2244 && type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
2249 /* Otherwise, it could be nested, search harder. */
2250 if (DECL_NAME (field) == NULL_TREE
2251 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2253 tree subfield = lookup_anon_field (TREE_TYPE (field), type);
2261 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2262 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2263 non-NULL, it indicates the path to the base used to name MEMBER.
2264 If PRESERVE_REFERENCE is true, the expression returned will have
2265 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2266 returned will have the type referred to by the reference.
2268 This function does not perform access control; that is either done
2269 earlier by the parser when the name of MEMBER is resolved to MEMBER
2270 itself, or later when overload resolution selects one of the
2271 functions indicated by MEMBER. */
2274 build_class_member_access_expr (cp_expr object, tree member,
2275 tree access_path, bool preserve_reference,
2276 tsubst_flags_t complain)
2280 tree result = NULL_TREE;
2281 tree using_decl = NULL_TREE;
2283 if (error_operand_p (object) || error_operand_p (member))
2284 return error_mark_node;
2286 gcc_assert (DECL_P (member) || BASELINK_P (member));
2290 The type of the first expression shall be "class object" (of a
2292 object_type = TREE_TYPE (object);
2293 if (!currently_open_class (object_type)
2294 && !complete_type_or_maybe_complain (object_type, object, complain))
2295 return error_mark_node;
2296 if (!CLASS_TYPE_P (object_type))
2298 if (complain & tf_error)
2300 if (POINTER_TYPE_P (object_type)
2301 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2302 error ("request for member %qD in %qE, which is of pointer "
2303 "type %qT (maybe you meant to use %<->%> ?)",
2304 member, object.get_value (), object_type);
2306 error ("request for member %qD in %qE, which is of non-class "
2307 "type %qT", member, object.get_value (), object_type);
2309 return error_mark_node;
2312 /* The standard does not seem to actually say that MEMBER must be a
2313 member of OBJECT_TYPE. However, that is clearly what is
2315 if (DECL_P (member))
2317 member_scope = DECL_CLASS_CONTEXT (member);
2318 if (!mark_used (member, complain) && !(complain & tf_error))
2319 return error_mark_node;
2320 if (TREE_DEPRECATED (member))
2321 warn_deprecated_use (member, NULL_TREE);
2324 member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
2325 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2326 presently be the anonymous union. Go outwards until we find a
2327 type related to OBJECT_TYPE. */
2328 while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope))
2329 && !same_type_ignoring_top_level_qualifiers_p (member_scope,
2331 member_scope = TYPE_CONTEXT (member_scope);
2332 if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
2334 if (complain & tf_error)
2336 if (TREE_CODE (member) == FIELD_DECL)
2337 error ("invalid use of nonstatic data member %qE", member);
2339 error ("%qD is not a member of %qT", member, object_type);
2341 return error_mark_node;
2344 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2345 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2346 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2348 tree temp = unary_complex_lvalue (ADDR_EXPR, object);
2350 object = cp_build_indirect_ref (temp, RO_NULL, complain);
2353 /* In [expr.ref], there is an explicit list of the valid choices for
2354 MEMBER. We check for each of those cases here. */
2357 /* A static data member. */
2359 mark_exp_read (object);
2360 /* If OBJECT has side-effects, they are supposed to occur. */
2361 if (TREE_SIDE_EFFECTS (object))
2362 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
2364 else if (TREE_CODE (member) == FIELD_DECL)
2366 /* A non-static data member. */
2371 if (INDIRECT_REF_P (object))
2373 integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object, 0)));
2375 null_object_p = false;
2377 /* Convert OBJECT to the type of MEMBER. */
2378 if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
2379 TYPE_MAIN_VARIANT (member_scope)))
2384 binfo = lookup_base (access_path ? access_path : object_type,
2385 member_scope, ba_unique, &kind, complain);
2386 if (binfo == error_mark_node)
2387 return error_mark_node;
2389 /* It is invalid to try to get to a virtual base of a
2390 NULL object. The most common cause is invalid use of
2392 if (null_object_p && kind == bk_via_virtual)
2394 if (complain & tf_error)
2396 error ("invalid access to non-static data member %qD in "
2397 "virtual base of NULL object", member);
2399 return error_mark_node;
2402 /* Convert to the base. */
2403 object = build_base_path (PLUS_EXPR, object, binfo,
2404 /*nonnull=*/1, complain);
2405 /* If we found the base successfully then we should be able
2406 to convert to it successfully. */
2407 gcc_assert (object != error_mark_node);
2410 /* If MEMBER is from an anonymous aggregate, we have converted
2411 OBJECT so that it refers to the class containing the
2412 anonymous union. Generate a reference to the anonymous union
2413 itself, and recur to find MEMBER. */
2414 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
2415 /* When this code is called from build_field_call, the
2416 object already has the type of the anonymous union.
2417 That is because the COMPONENT_REF was already
2418 constructed, and was then disassembled before calling
2419 build_field_call. After the function-call code is
2420 cleaned up, this waste can be eliminated. */
2421 && (!same_type_ignoring_top_level_qualifiers_p
2422 (TREE_TYPE (object), DECL_CONTEXT (member))))
2424 tree anonymous_union;
2426 anonymous_union = lookup_anon_field (TREE_TYPE (object),
2427 DECL_CONTEXT (member));
2428 object = build_class_member_access_expr (object,
2430 /*access_path=*/NULL_TREE,
2435 /* Compute the type of the field, as described in [expr.ref]. */
2436 type_quals = TYPE_UNQUALIFIED;
2437 member_type = TREE_TYPE (member);
2438 if (TREE_CODE (member_type) != REFERENCE_TYPE)
2440 type_quals = (cp_type_quals (member_type)
2441 | cp_type_quals (object_type));
2443 /* A field is const (volatile) if the enclosing object, or the
2444 field itself, is const (volatile). But, a mutable field is
2445 not const, even within a const object. */
2446 if (DECL_MUTABLE_P (member))
2447 type_quals &= ~TYPE_QUAL_CONST;
2448 member_type = cp_build_qualified_type (member_type, type_quals);
2451 result = build3_loc (input_location, COMPONENT_REF, member_type,
2452 object, member, NULL_TREE);
2454 /* Mark the expression const or volatile, as appropriate. Even
2455 though we've dealt with the type above, we still have to mark the
2456 expression itself. */
2457 if (type_quals & TYPE_QUAL_CONST)
2458 TREE_READONLY (result) = 1;
2459 if (type_quals & TYPE_QUAL_VOLATILE)
2460 TREE_THIS_VOLATILE (result) = 1;
2462 else if (BASELINK_P (member))
2464 /* The member is a (possibly overloaded) member function. */
2468 /* If the MEMBER is exactly one static member function, then we
2469 know the type of the expression. Otherwise, we must wait
2470 until overload resolution has been performed. */
2471 functions = BASELINK_FUNCTIONS (member);
2472 if (TREE_CODE (functions) == FUNCTION_DECL
2473 && DECL_STATIC_FUNCTION_P (functions))
2474 type = TREE_TYPE (functions);
2476 type = unknown_type_node;
2477 /* Note that we do not convert OBJECT to the BASELINK_BINFO
2478 base. That will happen when the function is called. */
2479 result = build3 (COMPONENT_REF, type, object, member, NULL_TREE);
2481 else if (TREE_CODE (member) == CONST_DECL)
2483 /* The member is an enumerator. */
2485 /* If OBJECT has side-effects, they are supposed to occur. */
2486 if (TREE_SIDE_EFFECTS (object))
2487 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
2490 else if ((using_decl = strip_using_decl (member)) != member)
2491 result = build_class_member_access_expr (object,
2493 access_path, preserve_reference,
2497 if (complain & tf_error)
2498 error ("invalid use of %qD", member);
2499 return error_mark_node;
2502 if (!preserve_reference)
2505 If E2 is declared to have type "reference to T", then ... the
2506 type of E1.E2 is T. */
2507 result = convert_from_reference (result);
2512 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
2513 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
2516 lookup_destructor (tree object, tree scope, tree dtor_name,
2517 tsubst_flags_t complain)
2519 tree object_type = TREE_TYPE (object);
2520 tree dtor_type = TREE_OPERAND (dtor_name, 0);
2523 /* We've already complained about this destructor. */
2524 if (dtor_type == error_mark_node)
2525 return error_mark_node;
2527 if (scope && !check_dtor_name (scope, dtor_type))
2529 if (complain & tf_error)
2530 error ("qualified type %qT does not match destructor name ~%qT",
2532 return error_mark_node;
2534 if (is_auto (dtor_type))
2535 dtor_type = object_type;
2536 else if (identifier_p (dtor_type))
2538 /* In a template, names we can't find a match for are still accepted
2539 destructor names, and we check them here. */
2540 if (check_dtor_name (object_type, dtor_type))
2541 dtor_type = object_type;
2544 if (complain & tf_error)
2545 error ("object type %qT does not match destructor name ~%qT",
2546 object_type, dtor_type);
2547 return error_mark_node;
2551 else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
2553 if (complain & tf_error)
2554 error ("the type being destroyed is %qT, but the destructor "
2555 "refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type);
2556 return error_mark_node;
2558 expr = lookup_member (dtor_type, complete_dtor_identifier,
2559 /*protect=*/1, /*want_type=*/false,
2560 tf_warning_or_error);
2563 if (complain & tf_error)
2564 cxx_incomplete_type_error (dtor_name, dtor_type);
2565 return error_mark_node;
2567 expr = (adjust_result_of_qualified_name_lookup
2568 (expr, dtor_type, object_type));
2569 if (scope == NULL_TREE)
2570 /* We need to call adjust_result_of_qualified_name_lookup in case the
2571 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
2572 that we still get virtual function binding. */
2573 BASELINK_QUALIFIED_P (expr) = false;
2577 /* An expression of the form "A::template B" has been resolved to
2578 DECL. Issue a diagnostic if B is not a template or template
2582 check_template_keyword (tree decl)
2584 /* The standard says:
2588 If a name prefixed by the keyword template is not a member
2589 template, the program is ill-formed.
2591 DR 228 removed the restriction that the template be a member
2594 DR 96, if accepted would add the further restriction that explicit
2595 template arguments must be provided if the template keyword is
2596 used, but, as of 2005-10-16, that DR is still in "drafting". If
2597 this DR is accepted, then the semantic checks here can be
2598 simplified, as the entity named must in fact be a template
2599 specialization, rather than, as at present, a set of overloaded
2600 functions containing at least one template function. */
2601 if (TREE_CODE (decl) != TEMPLATE_DECL
2602 && TREE_CODE (decl) != TEMPLATE_ID_EXPR)
2606 if (DECL_USE_TEMPLATE (decl)
2607 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2610 permerror (input_location, "%qD is not a template", decl);
2612 else if (!is_overloaded_fn (decl))
2613 permerror (input_location, "%qD is not a template", decl);
2618 if (BASELINK_P (fns))
2619 fns = BASELINK_FUNCTIONS (fns);
2622 tree fn = OVL_CURRENT (fns);
2623 if (TREE_CODE (fn) == TEMPLATE_DECL
2624 || TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2626 if (TREE_CODE (fn) == FUNCTION_DECL
2627 && DECL_USE_TEMPLATE (fn)
2628 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
2630 fns = OVL_NEXT (fns);
2633 permerror (input_location, "%qD is not a template", decl);
2638 /* This function is called by the parser to process a class member
2639 access expression of the form OBJECT.NAME. NAME is a node used by
2640 the parser to represent a name; it is not yet a DECL. It may,
2641 however, be a BASELINK where the BASELINK_FUNCTIONS is a
2642 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
2643 there is no reason to do the lookup twice, so the parser keeps the
2644 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
2645 be a template via the use of the "A::template B" syntax. */
2648 finish_class_member_access_expr (cp_expr object, tree name, bool template_p,
2649 tsubst_flags_t complain)
2654 tree access_path = NULL_TREE;
2655 tree orig_object = object;
2656 tree orig_name = name;
2658 if (object == error_mark_node || name == error_mark_node)
2659 return error_mark_node;
2661 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
2662 if (!objc_is_public (object, name))
2663 return error_mark_node;
2665 object_type = TREE_TYPE (object);
2667 if (processing_template_decl)
2669 if (/* If OBJECT is dependent, so is OBJECT.NAME. */
2670 type_dependent_expression_p (object)
2671 /* If NAME is "f<args>", where either 'f' or 'args' is
2672 dependent, then the expression is dependent. */
2673 || (TREE_CODE (name) == TEMPLATE_ID_EXPR
2674 && dependent_template_id_p (TREE_OPERAND (name, 0),
2675 TREE_OPERAND (name, 1)))
2676 /* If NAME is "T::X" where "T" is dependent, then the
2677 expression is dependent. */
2678 || (TREE_CODE (name) == SCOPE_REF
2679 && TYPE_P (TREE_OPERAND (name, 0))
2680 && dependent_type_p (TREE_OPERAND (name, 0))))
2681 return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
2682 object.get_value (), name, NULL_TREE);
2683 object = build_non_dependent_expr (object);
2685 else if (c_dialect_objc ()
2686 && identifier_p (name)
2687 && (expr = objc_maybe_build_component_ref (object, name)))
2692 The type of the first expression shall be "class object" (of a
2694 if (!currently_open_class (object_type)
2695 && !complete_type_or_maybe_complain (object_type, object, complain))
2696 return error_mark_node;
2697 if (!CLASS_TYPE_P (object_type))
2699 if (complain & tf_error)
2701 if (POINTER_TYPE_P (object_type)
2702 && CLASS_TYPE_P (TREE_TYPE (object_type)))
2703 error ("request for member %qD in %qE, which is of pointer "
2704 "type %qT (maybe you meant to use %<->%> ?)",
2705 name, object.get_value (), object_type);
2707 error ("request for member %qD in %qE, which is of non-class "
2708 "type %qT", name, object.get_value (), object_type);
2710 return error_mark_node;
2713 if (BASELINK_P (name))
2714 /* A member function that has already been looked up. */
2718 bool is_template_id = false;
2719 tree template_args = NULL_TREE;
2722 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2724 is_template_id = true;
2725 template_args = TREE_OPERAND (name, 1);
2726 name = TREE_OPERAND (name, 0);
2728 if (TREE_CODE (name) == OVERLOAD)
2729 name = DECL_NAME (get_first_fn (name));
2730 else if (DECL_P (name))
2731 name = DECL_NAME (name);
2734 if (TREE_CODE (name) == SCOPE_REF)
2736 /* A qualified name. The qualifying class or namespace `S'
2737 has already been looked up; it is either a TYPE or a
2739 scope = TREE_OPERAND (name, 0);
2740 name = TREE_OPERAND (name, 1);
2742 /* If SCOPE is a namespace, then the qualified name does not
2743 name a member of OBJECT_TYPE. */
2744 if (TREE_CODE (scope) == NAMESPACE_DECL)
2746 if (complain & tf_error)
2747 error ("%<%D::%D%> is not a member of %qT",
2748 scope, name, object_type);
2749 return error_mark_node;
2752 if (TREE_CODE (scope) == ENUMERAL_TYPE)
2754 /* Looking up a member enumerator (c++/56793). */
2755 if (!TYPE_CLASS_SCOPE_P (scope)
2756 || !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
2758 if (complain & tf_error)
2759 error ("%<%D::%D%> is not a member of %qT",
2760 scope, name, object_type);
2761 return error_mark_node;
2763 tree val = lookup_enumerator (scope, name);
2766 if (complain & tf_error)
2767 error ("%qD is not a member of %qD",
2769 return error_mark_node;
2772 if (TREE_SIDE_EFFECTS (object))
2773 val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
2777 gcc_assert (CLASS_TYPE_P (scope));
2778 gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR);
2780 if (constructor_name_p (name, scope))
2782 if (complain & tf_error)
2783 error ("cannot call constructor %<%T::%D%> directly",
2785 return error_mark_node;
2788 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
2789 access_path = lookup_base (object_type, scope, ba_check,
2791 if (access_path == error_mark_node)
2792 return error_mark_node;
2795 if (complain & tf_error)
2796 error ("%qT is not a base of %qT", scope, object_type);
2797 return error_mark_node;
2803 access_path = object_type;
2806 if (TREE_CODE (name) == BIT_NOT_EXPR)
2807 member = lookup_destructor (object, scope, name, complain);
2810 /* Look up the member. */
2811 member = lookup_member (access_path, name, /*protect=*/1,
2812 /*want_type=*/false, complain);
2813 if (member == NULL_TREE)
2815 if (complain & tf_error)
2817 tree guessed_id = lookup_member_fuzzy (access_path, name,
2818 /*want_type=*/false);
2820 error ("%q#T has no member named %qE; did you mean %qE?",
2821 TREE_CODE (access_path) == TREE_BINFO
2822 ? TREE_TYPE (access_path) : object_type, name, guessed_id);
2824 error ("%q#T has no member named %qE",
2825 TREE_CODE (access_path) == TREE_BINFO
2826 ? TREE_TYPE (access_path) : object_type, name);
2828 return error_mark_node;
2830 if (member == error_mark_node)
2831 return error_mark_node;
2836 tree templ = member;
2838 if (BASELINK_P (templ))
2839 templ = lookup_template_function (templ, template_args);
2842 if (complain & tf_error)
2843 error ("%qD is not a member template function", name);
2844 return error_mark_node;
2849 if (TREE_DEPRECATED (member))
2850 warn_deprecated_use (member, NULL_TREE);
2853 check_template_keyword (member);
2855 expr = build_class_member_access_expr (object, member, access_path,
2856 /*preserve_reference=*/false,
2858 if (processing_template_decl && expr != error_mark_node)
2860 if (BASELINK_P (member))
2862 if (TREE_CODE (orig_name) == SCOPE_REF)
2863 BASELINK_QUALIFIED_P (member) = 1;
2866 return build_min_non_dep (COMPONENT_REF, expr,
2867 orig_object, orig_name,
2874 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
2878 build_simple_component_ref (tree object, tree member)
2880 tree type = cp_build_qualified_type (TREE_TYPE (member),
2881 cp_type_quals (TREE_TYPE (object)));
2882 return build3_loc (input_location,
2883 COMPONENT_REF, type,
2884 object, member, NULL_TREE);
2887 /* Return an expression for the MEMBER_NAME field in the internal
2888 representation of PTRMEM, a pointer-to-member function. (Each
2889 pointer-to-member function type gets its own RECORD_TYPE so it is
2890 more convenient to access the fields by name than by FIELD_DECL.)
2891 This routine converts the NAME to a FIELD_DECL and then creates the
2892 node for the complete expression. */
2895 build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
2900 /* This code is a stripped down version of
2901 build_class_member_access_expr. It does not work to use that
2902 routine directly because it expects the object to be of class
2904 ptrmem_type = TREE_TYPE (ptrmem);
2905 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
2906 for (member = TYPE_FIELDS (ptrmem_type); member;
2907 member = DECL_CHAIN (member))
2908 if (DECL_NAME (member) == member_name)
2910 return build_simple_component_ref (ptrmem, member);
2913 /* Given an expression PTR for a pointer, return an expression
2914 for the value pointed to.
2915 ERRORSTRING is the name of the operator to appear in error messages.
2917 This function may need to overload OPERATOR_FNNAME.
2918 Must also handle REFERENCE_TYPEs for C++. */
2921 build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
2922 tsubst_flags_t complain)
2924 tree orig_expr = expr;
2926 tree overload = NULL_TREE;
2928 if (processing_template_decl)
2930 /* Retain the type if we know the operand is a pointer. */
2931 if (TREE_TYPE (expr) && POINTER_TYPE_P (TREE_TYPE (expr)))
2932 return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
2933 if (type_dependent_expression_p (expr))
2934 return build_min_nt_loc (loc, INDIRECT_REF, expr);
2935 expr = build_non_dependent_expr (expr);
2938 rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
2939 NULL_TREE, NULL_TREE, &overload, complain);
2941 rval = cp_build_indirect_ref (expr, errorstring, complain);
2943 if (processing_template_decl && rval != error_mark_node)
2945 if (overload != NULL_TREE)
2946 return (build_min_non_dep_op_overload
2947 (INDIRECT_REF, rval, overload, orig_expr));
2949 return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
2955 /* Helper function called from c-common. */
2957 build_indirect_ref (location_t /*loc*/,
2958 tree ptr, ref_operator errorstring)
2960 return cp_build_indirect_ref (ptr, errorstring, tf_warning_or_error);
2964 cp_build_indirect_ref (tree ptr, ref_operator errorstring,
2965 tsubst_flags_t complain)
2969 if (ptr == current_class_ptr
2970 || (TREE_CODE (ptr) == NOP_EXPR
2971 && TREE_OPERAND (ptr, 0) == current_class_ptr
2972 && (same_type_ignoring_top_level_qualifiers_p
2973 (TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
2974 return current_class_ref;
2976 pointer = (TREE_CODE (TREE_TYPE (ptr)) == REFERENCE_TYPE
2977 ? ptr : decay_conversion (ptr, complain));
2978 if (pointer == error_mark_node)
2979 return error_mark_node;
2981 type = TREE_TYPE (pointer);
2983 if (POINTER_TYPE_P (type))
2987 If the type of the expression is "pointer to T," the type
2988 of the result is "T." */
2989 tree t = TREE_TYPE (type);
2991 if ((CONVERT_EXPR_P (ptr)
2992 || TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
2993 && (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t)))
2995 /* If a warning is issued, mark it to avoid duplicates from
2996 the backend. This only needs to be done at
2997 warn_strict_aliasing > 2. */
2998 if (warn_strict_aliasing > 2)
2999 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr, 0)),
3000 type, TREE_OPERAND (ptr, 0)))
3001 TREE_NO_WARNING (ptr) = 1;
3004 if (VOID_TYPE_P (t))
3006 /* A pointer to incomplete type (other than cv void) can be
3007 dereferenced [expr.unary.op]/1 */
3008 if (complain & tf_error)
3009 error ("%qT is not a pointer-to-object type", type);
3010 return error_mark_node;
3012 else if (TREE_CODE (pointer) == ADDR_EXPR
3013 && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
3014 /* The POINTER was something like `&x'. We simplify `*&x' to
3016 return TREE_OPERAND (pointer, 0);
3019 tree ref = build1 (INDIRECT_REF, t, pointer);
3021 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3022 so that we get the proper error message if the result is used
3023 to assign to. Also, &* is supposed to be a no-op. */
3024 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
3025 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
3026 TREE_SIDE_EFFECTS (ref)
3027 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
3031 else if (!(complain & tf_error))
3032 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
3034 /* `pointer' won't be an error_mark_node if we were given a
3035 pointer to member, so it's cool to check for this here. */
3036 else if (TYPE_PTRMEM_P (type))
3037 switch (errorstring)
3039 case RO_ARRAY_INDEXING:
3040 error ("invalid use of array indexing on pointer to member");
3043 error ("invalid use of unary %<*%> on pointer to member");
3045 case RO_IMPLICIT_CONVERSION:
3046 error ("invalid use of implicit conversion on pointer to member");
3049 error ("left hand operand of %<->*%> must be a pointer to class, "
3050 "but is a pointer to member of type %qT", type);
3055 else if (pointer != error_mark_node)
3056 invalid_indirection_error (input_location, type, errorstring);
3058 return error_mark_node;
3061 /* This handles expressions of the form "a[i]", which denotes
3064 This is logically equivalent in C to *(a+i), but we may do it differently.
3065 If A is a variable or a member, we generate a primitive ARRAY_REF.
3066 This avoids forcing the array out of registers, and can work on
3067 arrays that are not lvalues (for example, members of structures returned
3070 If INDEX is of some user-defined type, it must be converted to
3071 integer type. Otherwise, to make a compatible PLUS_EXPR, it
3072 will inherit the type of the array, which will be some pointer type.
3074 LOC is the location to use in building the array reference. */
3077 cp_build_array_ref (location_t loc, tree array, tree idx,
3078 tsubst_flags_t complain)
3084 if (complain & tf_error)
3085 error_at (loc, "subscript missing in array reference");
3086 return error_mark_node;
3089 /* If an array's index is an array notation, then its rank cannot be
3090 greater than one. */
3091 if (flag_cilkplus && contains_array_notation_expr (idx))
3095 /* If find_rank returns false, then it should have reported an error,
3096 thus it is unnecessary for repetition. */
3097 if (!find_rank (loc, idx, idx, true, &rank))
3098 return error_mark_node;
3101 error_at (loc, "rank of the array%'s index is greater than 1");
3102 return error_mark_node;
3105 if (TREE_TYPE (array) == error_mark_node
3106 || TREE_TYPE (idx) == error_mark_node)
3107 return error_mark_node;
3109 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3111 switch (TREE_CODE (array))
3115 tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3117 ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
3118 TREE_OPERAND (array, 0), value);
3119 SET_EXPR_LOCATION (ret, loc);
3124 ret = build_conditional_expr
3125 (loc, TREE_OPERAND (array, 0),
3126 cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
3128 cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
3131 protected_set_expr_location (ret, loc);
3139 = convert_vector_to_pointer_for_subscript (loc, &array, idx);
3141 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
3145 warn_array_subscript_with_type_char (loc, idx);
3147 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
3149 if (complain & tf_error)
3150 error_at (loc, "array subscript is not an integer");
3151 return error_mark_node;
3154 /* Apply integral promotions *after* noticing character types.
3155 (It is unclear why we do these promotions -- the standard
3156 does not say that we should. In fact, the natural thing would
3157 seem to be to convert IDX to ptrdiff_t; we're performing
3158 pointer arithmetic.) */
3159 idx = cp_perform_integral_promotions (idx, complain);
3161 /* An array that is indexed by a non-constant
3162 cannot be stored in a register; we must be able to do
3163 address arithmetic on its address.
3164 Likewise an array of elements of variable size. */
3165 if (TREE_CODE (idx) != INTEGER_CST
3166 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
3167 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
3170 if (!cxx_mark_addressable (array))
3171 return error_mark_node;
3174 /* An array that is indexed by a constant value which is not within
3175 the array bounds cannot be stored in a register either; because we
3176 would get a crash in store_bit_field/extract_bit_field when trying
3177 to access a non-existent part of the register. */
3178 if (TREE_CODE (idx) == INTEGER_CST
3179 && TYPE_DOMAIN (TREE_TYPE (array))
3180 && ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
3182 if (!cxx_mark_addressable (array))
3183 return error_mark_node;
3186 /* Note in C++ it is valid to subscript a `register' array, since
3187 it is valid to take the address of something with that
3188 storage specification. */
3192 while (TREE_CODE (foo) == COMPONENT_REF)
3193 foo = TREE_OPERAND (foo, 0);
3194 if (VAR_P (foo) && DECL_REGISTER (foo)
3195 && (complain & tf_warning))
3196 warning_at (loc, OPT_Wextra,
3197 "subscripting array declared %<register%>");
3200 type = TREE_TYPE (TREE_TYPE (array));
3201 rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
3202 /* Array ref is const/volatile if the array elements are
3203 or if the array is.. */
3204 TREE_READONLY (rval)
3205 |= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
3206 TREE_SIDE_EFFECTS (rval)
3207 |= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
3208 TREE_THIS_VOLATILE (rval)
3209 |= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
3210 ret = require_complete_type_sfinae (rval, complain);
3211 protected_set_expr_location (ret, loc);
3213 ret = non_lvalue_loc (loc, ret);
3218 tree ar = cp_default_conversion (array, complain);
3219 tree ind = cp_default_conversion (idx, complain);
3221 /* Put the integer in IND to simplify error checking. */
3222 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
3223 std::swap (ar, ind);
3225 if (ar == error_mark_node || ind == error_mark_node)
3226 return error_mark_node;
3228 if (!TYPE_PTR_P (TREE_TYPE (ar)))
3230 if (complain & tf_error)
3231 error_at (loc, "subscripted value is neither array nor pointer");
3232 return error_mark_node;
3234 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
3236 if (complain & tf_error)
3237 error_at (loc, "array subscript is not an integer");
3238 return error_mark_node;
3241 warn_array_subscript_with_type_char (loc, idx);
3243 ret = cp_build_indirect_ref (cp_build_binary_op (input_location,
3248 protected_set_expr_location (ret, loc);
3250 ret = non_lvalue_loc (loc, ret);
3255 /* Entry point for Obj-C++. */
3258 build_array_ref (location_t loc, tree array, tree idx)
3260 return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
3263 /* Resolve a pointer to member function. INSTANCE is the object
3264 instance to use, if the member points to a virtual member.
3266 This used to avoid checking for virtual functions if basetype
3267 has no virtual functions, according to an earlier ANSI draft.
3268 With the final ISO C++ rules, such an optimization is
3269 incorrect: A pointer to a derived member can be static_cast
3270 to pointer-to-base-member, as long as the dynamic object
3271 later has the right member. So now we only do this optimization
3272 when we know the dynamic type of the object. */
3275 get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
3276 tsubst_flags_t complain)
3278 if (TREE_CODE (function) == OFFSET_REF)
3279 function = TREE_OPERAND (function, 1);
3281 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
3283 tree idx, delta, e1, e2, e3, vtbl;
3285 tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
3286 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
3288 tree instance_ptr = *instance_ptrptr;
3289 tree instance_save_expr = 0;
3290 if (instance_ptr == error_mark_node)
3292 if (TREE_CODE (function) == PTRMEM_CST)
3294 /* Extracting the function address from a pmf is only
3295 allowed with -Wno-pmf-conversions. It only works for
3297 e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
3298 e1 = convert (fntype, e1);
3303 if (complain & tf_error)
3304 error ("object missing in use of %qE", function);
3305 return error_mark_node;
3309 /* True if we know that the dynamic type of the object doesn't have
3310 virtual functions, so we can assume the PFN field is a pointer. */
3311 nonvirtual = (COMPLETE_TYPE_P (basetype)
3312 && !TYPE_POLYMORPHIC_P (basetype)
3313 && resolves_to_fixed_type_p (instance_ptr, 0));
3315 /* If we don't really have an object (i.e. in an ill-formed
3316 conversion from PMF to pointer), we can't resolve virtual
3317 functions anyway. */
3318 if (!nonvirtual && is_dummy_object (instance_ptr))
3321 if (TREE_SIDE_EFFECTS (instance_ptr))
3322 instance_ptr = instance_save_expr = save_expr (instance_ptr);
3324 if (TREE_SIDE_EFFECTS (function))
3325 function = save_expr (function);
3327 /* Start by extracting all the information from the PMF itself. */
3328 e3 = pfn_from_ptrmemfunc (function);
3329 delta = delta_from_ptrmemfunc (function);
3330 idx = build1 (NOP_EXPR, vtable_index_type, e3);
3331 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
3333 int flag_sanitize_save;
3334 case ptrmemfunc_vbit_in_pfn:
3335 e1 = cp_build_binary_op (input_location,
3336 BIT_AND_EXPR, idx, integer_one_node,
3338 idx = cp_build_binary_op (input_location,
3339 MINUS_EXPR, idx, integer_one_node,
3341 if (idx == error_mark_node)
3342 return error_mark_node;
3345 case ptrmemfunc_vbit_in_delta:
3346 e1 = cp_build_binary_op (input_location,
3347 BIT_AND_EXPR, delta, integer_one_node,
3349 /* Don't instrument the RSHIFT_EXPR we're about to create because
3350 we're going to use DELTA number of times, and that wouldn't play
3351 well with SAVE_EXPRs therein. */
3352 flag_sanitize_save = flag_sanitize;
3354 delta = cp_build_binary_op (input_location,
3355 RSHIFT_EXPR, delta, integer_one_node,
3357 flag_sanitize = flag_sanitize_save;
3358 if (delta == error_mark_node)
3359 return error_mark_node;
3366 if (e1 == error_mark_node)
3367 return error_mark_node;
3369 /* Convert down to the right base before using the instance. A
3370 special case is that in a pointer to member of class C, C may
3371 be incomplete. In that case, the function will of course be
3372 a member of C, and no conversion is required. In fact,
3373 lookup_base will fail in that case, because incomplete
3374 classes do not have BINFOs. */
3375 if (!same_type_ignoring_top_level_qualifiers_p
3376 (basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
3378 basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
3379 basetype, ba_check, NULL, complain);
3380 instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
3382 if (instance_ptr == error_mark_node)
3383 return error_mark_node;
3385 /* ...and then the delta in the PMF. */
3386 instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
3388 /* Hand back the adjusted 'this' argument to our caller. */
3389 *instance_ptrptr = instance_ptr;
3392 /* Now just return the pointer. */
3395 /* Next extract the vtable pointer from the object. */
3396 vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
3398 vtbl = cp_build_indirect_ref (vtbl, RO_NULL, complain);
3399 if (vtbl == error_mark_node)
3400 return error_mark_node;
3402 /* Finally, extract the function pointer from the vtable. */
3403 e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
3404 e2 = cp_build_indirect_ref (e2, RO_NULL, complain);
3405 if (e2 == error_mark_node)
3406 return error_mark_node;
3407 TREE_CONSTANT (e2) = 1;
3409 /* When using function descriptors, the address of the
3410 vtable entry is treated as a function pointer. */
3411 if (TARGET_VTABLE_USES_DESCRIPTORS)
3412 e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
3413 cp_build_addr_expr (e2, complain));
3415 e2 = fold_convert (TREE_TYPE (e3), e2);
3416 e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
3417 if (e1 == error_mark_node)
3418 return error_mark_node;
3420 /* Make sure this doesn't get evaluated first inside one of the
3421 branches of the COND_EXPR. */
3422 if (instance_save_expr)
3423 e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
3424 instance_save_expr, e1);
3431 /* Used by the C-common bits. */
3433 build_function_call (location_t /*loc*/,
3434 tree function, tree params)
3436 return cp_build_function_call (function, params, tf_warning_or_error);
3439 /* Used by the C-common bits. */
3441 build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/,
3442 tree function, vec<tree, va_gc> *params,
3443 vec<tree, va_gc> * /*origtypes*/)
3445 vec<tree, va_gc> *orig_params = params;
3446 tree ret = cp_build_function_call_vec (function, ¶ms,
3447 tf_warning_or_error);
3449 /* cp_build_function_call_vec can reallocate PARAMS by adding
3450 default arguments. That should never happen here. Verify
3452 gcc_assert (params == orig_params);
3457 /* Build a function call using a tree list of arguments. */
3460 cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
3462 vec<tree, va_gc> *vec;
3465 vec = make_tree_vector ();
3466 for (; params != NULL_TREE; params = TREE_CHAIN (params))
3467 vec_safe_push (vec, TREE_VALUE (params));
3468 ret = cp_build_function_call_vec (function, &vec, complain);
3469 release_tree_vector (vec);
3473 /* Build a function call using varargs. */
3476 cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
3478 vec<tree, va_gc> *vec;
3482 vec = make_tree_vector ();
3483 va_start (args, complain);
3484 for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
3485 vec_safe_push (vec, t);
3487 ret = cp_build_function_call_vec (function, &vec, complain);
3488 release_tree_vector (vec);
3492 /* Build a function call using a vector of arguments. PARAMS may be
3493 NULL if there are no parameters. This changes the contents of
3497 cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
3498 tsubst_flags_t complain)
3500 tree fntype, fndecl;
3502 tree original = function;
3506 vec<tree, va_gc> *allocated = NULL;
3509 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3510 expressions, like those used for ObjC messenger dispatches. */
3511 if (params != NULL && !vec_safe_is_empty (*params))
3512 function = objc_rewrite_function_call (function, (**params)[0]);
3514 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3515 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
3516 if (TREE_CODE (function) == NOP_EXPR
3517 && TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
3518 function = TREE_OPERAND (function, 0);
3520 if (TREE_CODE (function) == FUNCTION_DECL)
3522 /* If the function is a non-template member function
3523 or a non-template friend, then we need to check the
3526 Note that if overload resolution failed with a single
3527 candidate this function will be used to explicitly diagnose
3528 the failure for the single call expression. The check is
3529 technically redundant since we also would have failed in
3530 add_function_candidate. */
3532 && (complain & tf_error)
3533 && !constraints_satisfied_p (function))
3535 error ("cannot call function %qD", function);
3536 location_t loc = DECL_SOURCE_LOCATION (function);
3537 diagnose_constraints (loc, function, NULL_TREE);
3538 return error_mark_node;
3541 if (!mark_used (function, complain) && !(complain & tf_error))
3542 return error_mark_node;
3545 /* Convert anything with function type to a pointer-to-function. */
3546 if (DECL_MAIN_P (function))
3548 if (complain & tf_error)
3549 pedwarn (input_location, OPT_Wpedantic,
3550 "ISO C++ forbids calling %<::main%> from within program");
3552 return error_mark_node;
3554 function = build_addr_func (function, complain);
3560 function = build_addr_func (function, complain);
3563 if (function == error_mark_node)
3564 return error_mark_node;
3566 fntype = TREE_TYPE (function);
3568 if (TYPE_PTRMEMFUNC_P (fntype))
3570 if (complain & tf_error)
3571 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
3572 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
3573 original, original);
3574 return error_mark_node;
3577 is_method = (TYPE_PTR_P (fntype)
3578 && TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
3580 if (!(TYPE_PTRFN_P (fntype)
3582 || TREE_CODE (function) == TEMPLATE_ID_EXPR))
3584 if (complain & tf_error)
3586 if (!flag_diagnostics_show_caret)
3587 error_at (input_location,
3588 "%qE cannot be used as a function", original);
3589 else if (DECL_P (original))
3590 error_at (input_location,
3591 "%qD cannot be used as a function", original);
3593 error_at (input_location,
3594 "expression cannot be used as a function");
3597 return error_mark_node;
3600 /* fntype now gets the type of function pointed to. */
3601 fntype = TREE_TYPE (fntype);
3602 parm_types = TYPE_ARG_TYPES (fntype);
3606 allocated = make_tree_vector ();
3607 params = &allocated;
3610 nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
3613 return error_mark_node;
3615 argarray = (*params)->address ();
3617 /* Check for errors in format strings and inappropriately
3619 check_function_arguments (input_location, fntype, nargs, argarray);
3621 ret = build_cxx_call (function, nargs, argarray, complain);
3623 if (allocated != NULL)
3624 release_tree_vector (allocated);
3629 /* Subroutine of convert_arguments.
3630 Print an error message about a wrong number of arguments. */
3633 error_args_num (location_t loc, tree fndecl, bool too_many_p)
3637 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
3639 if (DECL_NAME (fndecl) == NULL_TREE
3640 || IDENTIFIER_HAS_TYPE_VALUE (DECL_NAME (fndecl)))
3643 ? G_("too many arguments to constructor %q#D")
3644 : G_("too few arguments to constructor %q#D"),
3649 ? G_("too many arguments to member function %q#D")
3650 : G_("too few arguments to member function %q#D"),
3656 ? G_("too many arguments to function %q#D")
3657 : G_("too few arguments to function %q#D"),
3659 if (!DECL_IS_BUILTIN (fndecl))
3660 inform (DECL_SOURCE_LOCATION (fndecl), "declared here");
3664 if (c_dialect_objc () && objc_message_selector ())
3667 ? G_("too many arguments to method %q#D")
3668 : G_("too few arguments to method %q#D"),
3669 objc_message_selector ());
3671 error_at (loc, too_many_p ? G_("too many arguments to function")
3672 : G_("too few arguments to function"));
3676 /* Convert the actual parameter expressions in the list VALUES to the
3677 types in the list TYPELIST. The converted expressions are stored
3678 back in the VALUES vector.
3679 If parmdecls is exhausted, or when an element has NULL as its type,
3680 perform the default conversions.
3682 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
3684 This is also where warnings about wrong number of args are generated.
3686 Returns the actual number of arguments processed (which might be less
3687 than the length of the vector), or -1 on error.
3689 In C++, unspecified trailing parameters can be filled in with their
3690 default arguments, if such were specified. Do so here. */
3693 convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
3694 int flags, tsubst_flags_t complain)
3699 /* Argument passing is always copy-initialization. */
3700 flags |= LOOKUP_ONLYCONVERTING;
3702 for (i = 0, typetail = typelist;
3703 i < vec_safe_length (*values);
3706 tree type = typetail ? TREE_VALUE (typetail) : 0;
3707 tree val = (**values)[i];
3709 if (val == error_mark_node || type == error_mark_node)
3712 if (type == void_type_node)
3714 if (complain & tf_error)
3716 error_args_num (input_location, fndecl, /*too_many_p=*/true);
3723 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
3724 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
3725 if (TREE_CODE (val) == NOP_EXPR
3726 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
3727 && (type == 0 || TREE_CODE (type) != REFERENCE_TYPE))
3728 val = TREE_OPERAND (val, 0);
3730 if (type == 0 || TREE_CODE (type) != REFERENCE_TYPE)
3732 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
3733 || TREE_CODE (TREE_TYPE (val)) == FUNCTION_TYPE
3734 || TREE_CODE (TREE_TYPE (val)) == METHOD_TYPE)
3735 val = decay_conversion (val, complain);
3738 if (val == error_mark_node)
3743 /* Formal parm type is specified by a function prototype. */
3746 if (!COMPLETE_TYPE_P (complete_type (type)))
3748 if (complain & tf_error)
3751 error ("parameter %P of %qD has incomplete type %qT",
3754 error ("parameter %P has incomplete type %qT", i, type);
3756 parmval = error_mark_node;
3760 parmval = convert_for_initialization
3761 (NULL_TREE, type, val, flags,
3762 ICR_ARGPASS, fndecl, i, complain);
3763 parmval = convert_for_arg_passing (type, parmval, complain);
3766 if (parmval == error_mark_node)
3769 (**values)[i] = parmval;
3773 if (fndecl && magic_varargs_p (fndecl))
3774 /* Don't do ellipsis conversion for __built_in_constant_p
3775 as this will result in spurious errors for non-trivial
3777 val = require_complete_type_sfinae (val, complain);
3779 val = convert_arg_to_ellipsis (val, complain);
3781 (**values)[i] = val;
3785 typetail = TREE_CHAIN (typetail);
3788 if (typetail != 0 && typetail != void_list_node)
3790 /* See if there are default arguments that can be used. Because
3791 we hold default arguments in the FUNCTION_TYPE (which is so
3792 wrong), we can see default parameters here from deduced
3793 contexts (and via typeof) for indirect function calls.
3794 Fortunately we know whether we have a function decl to
3795 provide default arguments in a language conformant
3797 if (fndecl && TREE_PURPOSE (typetail)
3798 && TREE_CODE (TREE_PURPOSE (typetail)) != DEFAULT_ARG)
3800 for (; typetail != void_list_node; ++i)
3803 = convert_default_arg (TREE_VALUE (typetail),
3804 TREE_PURPOSE (typetail),
3805 fndecl, i, complain);
3807 if (parmval == error_mark_node)
3810 vec_safe_push (*values, parmval);
3811 typetail = TREE_CHAIN (typetail);
3812 /* ends with `...'. */
3813 if (typetail == NULL_TREE)
3819 if (complain & tf_error)
3820 error_args_num (input_location, fndecl, /*too_many_p=*/false);
3828 /* Build a binary-operation expression, after performing default
3829 conversions on the operands. CODE is the kind of expression to
3830 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
3831 are the tree codes which correspond to ARG1 and ARG2 when issuing
3832 warnings about possibly misplaced parentheses. They may differ
3833 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
3834 folding (e.g., if the parser sees "a | 1 + 1", it may call this
3835 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
3836 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
3837 ARG2_CODE as ERROR_MARK. */
3840 build_x_binary_op (location_t loc, enum tree_code code, tree arg1,
3841 enum tree_code arg1_code, tree arg2,
3842 enum tree_code arg2_code, tree *overload_p,
3843 tsubst_flags_t complain)
3848 tree overload = NULL_TREE;
3853 if (processing_template_decl)
3855 if (type_dependent_expression_p (arg1)
3856 || type_dependent_expression_p (arg2))
3857 return build_min_nt_loc (loc, code, arg1, arg2);
3858 arg1 = build_non_dependent_expr (arg1);
3859 arg2 = build_non_dependent_expr (arg2);
3862 if (code == DOTSTAR_EXPR)
3863 expr = build_m_component_ref (arg1, arg2, complain);
3865 expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
3866 &overload, complain);
3868 if (overload_p != NULL)
3869 *overload_p = overload;
3871 /* Check for cases such as x+y<<z which users are likely to
3872 misinterpret. But don't warn about obj << x + y, since that is a
3873 common idiom for I/O. */
3874 if (warn_parentheses
3875 && (complain & tf_warning)
3876 && !processing_template_decl
3877 && !error_operand_p (arg1)
3878 && !error_operand_p (arg2)
3879 && (code != LSHIFT_EXPR
3880 || !CLASS_TYPE_P (TREE_TYPE (arg1))))
3881 warn_about_parentheses (loc, code, arg1_code, orig_arg1,
3882 arg2_code, orig_arg2);
3884 if (processing_template_decl && expr != error_mark_node)
3886 if (overload != NULL_TREE)
3887 return (build_min_non_dep_op_overload
3888 (code, expr, overload, orig_arg1, orig_arg2));
3890 return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
3896 /* Build and return an ARRAY_REF expression. */
3899 build_x_array_ref (location_t loc, tree arg1, tree arg2,
3900 tsubst_flags_t complain)
3902 tree orig_arg1 = arg1;
3903 tree orig_arg2 = arg2;
3905 tree overload = NULL_TREE;
3907 if (processing_template_decl)
3909 if (type_dependent_expression_p (arg1)
3910 || type_dependent_expression_p (arg2))
3911 return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
3912 NULL_TREE, NULL_TREE);
3913 arg1 = build_non_dependent_expr (arg1);
3914 arg2 = build_non_dependent_expr (arg2);
3917 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
3918 NULL_TREE, &overload, complain);
3920 if (processing_template_decl && expr != error_mark_node)
3922 if (overload != NULL_TREE)
3923 return (build_min_non_dep_op_overload
3924 (ARRAY_REF, expr, overload, orig_arg1, orig_arg2));
3926 return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
3927 NULL_TREE, NULL_TREE);
3932 /* Return whether OP is an expression of enum type cast to integer
3933 type. In C++ even unsigned enum types are cast to signed integer
3934 types. We do not want to issue warnings about comparisons between
3935 signed and unsigned types when one of the types is an enum type.
3936 Those warnings are always false positives in practice. */
3939 enum_cast_to_int (tree op)
3941 if (CONVERT_EXPR_P (op)
3942 && TREE_TYPE (op) == integer_type_node
3943 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
3944 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
3947 /* The cast may have been pushed into a COND_EXPR. */
3948 if (TREE_CODE (op) == COND_EXPR)
3949 return (enum_cast_to_int (TREE_OPERAND (op, 1))
3950 || enum_cast_to_int (TREE_OPERAND (op, 2)));
3955 /* For the c-common bits. */
3957 build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
3960 return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
3963 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
3964 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
3967 build_vec_cmp (tree_code code, tree type,
3968 tree arg0, tree arg1)
3970 tree zero_vec = build_zero_cst (type);
3971 tree minus_one_vec = build_minus_one_cst (type);
3972 tree cmp_type = build_same_sized_truth_vector_type(type);
3973 tree cmp = build2 (code, cmp_type, arg0, arg1);
3974 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
3977 /* Possibly warn about an address never being NULL. */
3980 warn_for_null_address (location_t location, tree op, tsubst_flags_t complain)
3983 || (complain & tf_warning) == 0
3984 || c_inhibit_evaluation_warnings != 0
3985 || TREE_NO_WARNING (op))
3988 tree cop = fold_non_dependent_expr (op);
3990 if (TREE_CODE (cop) == ADDR_EXPR
3991 && decl_with_nonnull_addr_p (TREE_OPERAND (cop, 0))
3992 && !TREE_NO_WARNING (cop))
3993 warning_at (location, OPT_Waddress, "the address of %qD will never "
3994 "be NULL", TREE_OPERAND (cop, 0));
3996 if (CONVERT_EXPR_P (op)
3997 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == REFERENCE_TYPE)
4000 STRIP_NOPS (inner_op);
4002 if (DECL_P (inner_op))
4003 warning_at (location, OPT_Waddress,
4004 "the compiler can assume that the address of "
4005 "%qD will never be NULL", inner_op);
4009 /* Build a binary-operation expression without default conversions.
4010 CODE is the kind of expression to build.
4011 LOCATION is the location_t of the operator in the source code.
4012 This function differs from `build' in several ways:
4013 the data type of the result is computed and recorded in it,
4014 warnings are generated if arg data types are invalid,
4015 special handling for addition and subtraction of pointers is known,
4016 and some optimization is done (operations on narrow ints
4017 are done in the narrower type when that gives the same result).
4018 Constant folding is also done before the result is returned.
4020 Note that the operands will never have enumeral types
4021 because either they have just had the default conversions performed
4022 or they have both just been converted to some other type in which
4023 the arithmetic is to be done.
4025 C++: must do special pointer arithmetic when implementing
4026 multiple inheritance, and deal with pointer to member functions. */
4029 cp_build_binary_op (location_t location,
4030 enum tree_code code, tree orig_op0, tree orig_op1,
4031 tsubst_flags_t complain)
4034 enum tree_code code0, code1;
4036 const char *invalid_op_diag;
4038 /* Expression code to give to the expression when it is built.
4039 Normally this is CODE, which is what the caller asked for,
4040 but in some special cases we change it. */
4041 enum tree_code resultcode = code;
4043 /* Data type in which the computation is to be performed.
4044 In the simplest cases this is the common type of the arguments. */
4045 tree result_type = NULL;
4047 /* Nonzero means operands have already been type-converted
4048 in whatever way is necessary.
4049 Zero means they need to be converted to RESULT_TYPE. */
4052 /* Nonzero means create the expression with this type, rather than
4054 tree build_type = 0;
4056 /* Nonzero means after finally constructing the expression
4057 convert it to this type. */
4058 tree final_type = 0;
4060 tree result, result_ovl;
4061 tree orig_type = NULL;
4063 /* Nonzero if this is an operation like MIN or MAX which can
4064 safely be computed in short if both args are promoted shorts.
4065 Also implies COMMON.
4066 -1 indicates a bitwise operation; this makes a difference
4067 in the exact conditions for when it is safe to do the operation
4068 in a narrower mode. */
4071 /* Nonzero if this is a comparison operation;
4072 if both args are promoted shorts, compare the original shorts.
4073 Also implies COMMON. */
4074 int short_compare = 0;
4076 /* Nonzero means set RESULT_TYPE to the common type of the args. */
4079 /* True if both operands have arithmetic type. */
4080 bool arithmetic_types_p;
4082 /* Apply default conversions. */
4086 /* Remember whether we're doing / or %. */
4087 bool doing_div_or_mod = false;
4089 /* Remember whether we're doing << or >>. */
4090 bool doing_shift = false;
4092 /* Tree holding instrumentation expression. */
4093 tree instrument_expr = NULL;
4095 if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
4096 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
4097 || code == TRUTH_XOR_EXPR)
4099 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
4100 op0 = decay_conversion (op0, complain);
4101 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
4102 op1 = decay_conversion (op1, complain);
4106 if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
4107 op0 = cp_default_conversion (op0, complain);
4108 if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
4109 op1 = cp_default_conversion (op1, complain);
4112 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
4113 STRIP_TYPE_NOPS (op0);
4114 STRIP_TYPE_NOPS (op1);
4116 /* DTRT if one side is an overloaded function, but complain about it. */
4117 if (type_unknown_p (op0))
4119 tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
4120 if (t != error_mark_node)
4122 if (complain & tf_error)
4123 permerror (input_location, "assuming cast to type %qT from overloaded function",
4128 if (type_unknown_p (op1))
4130 tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
4131 if (t != error_mark_node)
4133 if (complain & tf_error)
4134 permerror (input_location, "assuming cast to type %qT from overloaded function",
4140 type0 = TREE_TYPE (op0);
4141 type1 = TREE_TYPE (op1);
4143 /* The expression codes of the data types of the arguments tell us
4144 whether the arguments are integers, floating, pointers, etc. */
4145 code0 = TREE_CODE (type0);
4146 code1 = TREE_CODE (type1);
4148 /* If an error was already reported for one of the arguments,
4149 avoid reporting another error. */
4150 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
4151 return error_mark_node;
4153 if ((invalid_op_diag
4154 = targetm.invalid_binary_op (code, type0, type1)))
4156 if (complain & tf_error)
4157 error (invalid_op_diag);
4158 return error_mark_node;
4161 /* Issue warnings about peculiar, but valid, uses of NULL. */
4162 if ((orig_op0 == null_node || orig_op1 == null_node)
4163 /* It's reasonable to use pointer values as operands of &&
4164 and ||, so NULL is no exception. */
4165 && code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
4166 && ( /* Both are NULL (or 0) and the operation was not a
4167 comparison or a pointer subtraction. */
4168 (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
4169 && code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
4170 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
4171 || (!null_ptr_cst_p (orig_op0)
4172 && !TYPE_PTR_OR_PTRMEM_P (type0))
4173 || (!null_ptr_cst_p (orig_op1)
4174 && !TYPE_PTR_OR_PTRMEM_P (type1)))
4175 && (complain & tf_warning))
4177 source_location loc =
4178 expansion_point_location_if_in_system_header (input_location);
4180 warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
4183 /* In case when one of the operands of the binary operation is
4184 a vector and another is a scalar -- convert scalar to vector. */
4185 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
4187 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
4188 complain & tf_error);
4190 switch (convert_flag)
4193 return error_mark_node;
4196 op0 = convert (TREE_TYPE (type1), op0);
4197 op0 = save_expr (op0);
4198 op0 = build_vector_from_val (type1, op0);
4199 type0 = TREE_TYPE (op0);
4200 code0 = TREE_CODE (type0);
4206 op1 = convert (TREE_TYPE (type0), op1);
4207 op1 = save_expr (op1);
4208 op1 = build_vector_from_val (type0, op1);
4209 type1 = TREE_TYPE (op1);
4210 code1 = TREE_CODE (type1);
4222 /* Subtraction of two similar pointers.
4223 We must subtract them as integers, then divide by object size. */
4224 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
4225 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4227 return pointer_diff (op0, op1, common_pointer_type (type0, type1),
4229 /* In all other cases except pointer - int, the usual arithmetic
4231 else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
4236 /* The pointer - int case is just like pointer + int; fall
4239 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
4240 && (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
4244 ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
4245 int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
4246 if (processing_template_decl)
4248 result_type = TREE_TYPE (ptr_operand);
4251 return cp_pointer_int_sum (code,
4263 case TRUNC_DIV_EXPR:
4265 case FLOOR_DIV_EXPR:
4266 case ROUND_DIV_EXPR:
4267 case EXACT_DIV_EXPR:
4268 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4269 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
4270 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4271 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
4273 enum tree_code tcode0 = code0, tcode1 = code1;
4274 tree cop1 = fold_non_dependent_expr (op1);
4275 doing_div_or_mod = true;
4276 warn_for_div_by_zero (location, cop1);
4278 if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
4279 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
4280 if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
4281 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
4283 if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
4284 resultcode = RDIV_EXPR;
4286 /* When dividing two signed integers, we have to promote to int.
4287 unless we divide by a constant != -1. Note that default
4288 conversion will have been performed on the operands at this
4289 point, so we have to dig out the original type to find out if
4291 shorten = ((TREE_CODE (op0) == NOP_EXPR
4292 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4293 || (TREE_CODE (op1) == INTEGER_CST
4294 && ! integer_all_onesp (op1)));
4303 if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4304 || (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4305 && !VECTOR_FLOAT_TYPE_P (type0)
4306 && !VECTOR_FLOAT_TYPE_P (type1)))
4310 case TRUNC_MOD_EXPR:
4311 case FLOOR_MOD_EXPR:
4313 tree cop1 = fold_non_dependent_expr (op1);
4314 doing_div_or_mod = true;
4315 warn_for_div_by_zero (location, cop1);
4318 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4319 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4320 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
4322 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4324 /* Although it would be tempting to shorten always here, that loses
4325 on some targets, since the modulo instruction is undefined if the
4326 quotient can't be represented in the computation mode. We shorten
4327 only if unsigned or if dividing by something we know != -1. */
4328 shorten = ((TREE_CODE (op0) == NOP_EXPR
4329 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0))))
4330 || (TREE_CODE (op1) == INTEGER_CST
4331 && ! integer_all_onesp (op1)));
4336 case TRUTH_ANDIF_EXPR:
4337 case TRUTH_ORIF_EXPR:
4338 case TRUTH_AND_EXPR:
4340 if (!VECTOR_TYPE_P (type0) && VECTOR_TYPE_P (type1))
4342 if (!COMPARISON_CLASS_P (op1))
4343 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4344 build_zero_cst (type1), complain);
4345 if (code == TRUTH_ANDIF_EXPR)
4347 tree z = build_zero_cst (TREE_TYPE (op1));
4348 return build_conditional_expr (location, op0, op1, z, complain);
4350 else if (code == TRUTH_ORIF_EXPR)
4352 tree m1 = build_all_ones_cst (TREE_TYPE (op1));
4353 return build_conditional_expr (location, op0, m1, op1, complain);
4358 if (VECTOR_TYPE_P (type0))
4360 if (!COMPARISON_CLASS_P (op0))
4361 op0 = cp_build_binary_op (EXPR_LOCATION (op0), NE_EXPR, op0,
4362 build_zero_cst (type0), complain);
4363 if (!VECTOR_TYPE_P (type1))
4365 tree m1 = build_all_ones_cst (TREE_TYPE (op0));
4366 tree z = build_zero_cst (TREE_TYPE (op0));
4367 op1 = build_conditional_expr (location, op1, m1, z, complain);
4369 else if (!COMPARISON_CLASS_P (op1))
4370 op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
4371 build_zero_cst (type1), complain);
4373 if (code == TRUTH_ANDIF_EXPR)
4374 code = BIT_AND_EXPR;
4375 else if (code == TRUTH_ORIF_EXPR)
4376 code = BIT_IOR_EXPR;
4380 return cp_build_binary_op (location, code, op0, op1, complain);
4383 result_type = boolean_type_node;
4386 /* Shift operations: result has same type as first operand;
4387 always convert second operand to int.
4388 Also set SHORT_SHIFT if shifting rightward. */
4391 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4392 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4394 result_type = type0;
4397 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4398 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4399 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4400 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4402 result_type = type0;
4405 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4407 tree const_op1 = fold_non_dependent_expr (op1);
4408 if (TREE_CODE (const_op1) != INTEGER_CST)
4410 result_type = type0;
4412 if (TREE_CODE (const_op1) == INTEGER_CST)
4414 if (tree_int_cst_lt (const_op1, integer_zero_node))
4416 if ((complain & tf_warning)
4417 && c_inhibit_evaluation_warnings == 0)
4418 warning (OPT_Wshift_count_negative,
4419 "right shift count is negative");
4423 if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
4424 && (complain & tf_warning)
4425 && c_inhibit_evaluation_warnings == 0)
4426 warning (OPT_Wshift_count_overflow,
4427 "right shift count >= width of type");
4430 /* Avoid converting op1 to result_type later. */
4436 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
4437 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
4439 result_type = type0;
4442 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
4443 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
4444 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
4445 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
4447 result_type = type0;
4450 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4452 tree const_op0 = fold_non_dependent_expr (op0);
4453 if (TREE_CODE (const_op0) != INTEGER_CST)
4455 tree const_op1 = fold_non_dependent_expr (op1);
4456 if (TREE_CODE (const_op1) != INTEGER_CST)
4458 result_type = type0;
4460 if (TREE_CODE (const_op0) == INTEGER_CST
4461 && tree_int_cst_sgn (const_op0) < 0
4462 && (complain & tf_warning)
4463 && c_inhibit_evaluation_warnings == 0)
4464 warning (OPT_Wshift_negative_value,
4465 "left shift of negative value");
4466 if (TREE_CODE (const_op1) == INTEGER_CST)
4468 if (tree_int_cst_lt (const_op1, integer_zero_node))
4470 if ((complain & tf_warning)
4471 && c_inhibit_evaluation_warnings == 0)
4472 warning (OPT_Wshift_count_negative,
4473 "left shift count is negative");
4475 else if (compare_tree_int (const_op1,
4476 TYPE_PRECISION (type0)) >= 0)
4478 if ((complain & tf_warning)
4479 && c_inhibit_evaluation_warnings == 0)
4480 warning (OPT_Wshift_count_overflow,
4481 "left shift count >= width of type");
4483 else if (TREE_CODE (const_op0) == INTEGER_CST
4484 && (complain & tf_warning))
4485 maybe_warn_shift_overflow (location, const_op0, const_op1);
4487 /* Avoid converting op1 to result_type later. */
4494 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
4496 result_type = type0;
4497 if (TREE_CODE (op1) == INTEGER_CST)
4499 if (tree_int_cst_lt (op1, integer_zero_node))
4501 if (complain & tf_warning)
4502 warning (0, (code == LROTATE_EXPR)
4503 ? G_("left rotate count is negative")
4504 : G_("right rotate count is negative"));
4506 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
4508 if (complain & tf_warning)
4509 warning (0, (code == LROTATE_EXPR)
4510 ? G_("left rotate count >= width of type")
4511 : G_("right rotate count >= width of type"));
4514 /* Convert the shift-count to an integer, regardless of
4515 size of value being shifted. */
4516 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
4517 op1 = cp_convert (integer_type_node, op1, complain);
4523 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4524 goto vector_compare;
4525 if ((complain & tf_warning)
4526 && (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
4527 warning (OPT_Wfloat_equal,
4528 "comparing floating point with == or != is unsafe");
4529 if ((complain & tf_warning)
4530 && ((TREE_CODE (orig_op0) == STRING_CST
4531 && !integer_zerop (cp_fully_fold (op1)))
4532 || (TREE_CODE (orig_op1) == STRING_CST
4533 && !integer_zerop (cp_fully_fold (op0)))))
4534 warning (OPT_Waddress, "comparison with string literal results "
4535 "in unspecified behavior");
4537 build_type = boolean_type_node;
4538 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4539 || code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
4540 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4541 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
4543 else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
4544 && null_ptr_cst_p (op1))
4545 /* Handle, eg, (void*)0 (c++/43906), and more. */
4546 || (code0 == POINTER_TYPE
4547 && TYPE_PTR_P (type1) && integer_zerop (op1)))
4549 if (TYPE_PTR_P (type1))
4550 result_type = composite_pointer_type (type0, type1, op0, op1,
4551 CPO_COMPARISON, complain);
4553 result_type = type0;
4555 warn_for_null_address (location, op0, complain);
4557 else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
4558 && null_ptr_cst_p (op0))
4559 /* Handle, eg, (void*)0 (c++/43906), and more. */
4560 || (code1 == POINTER_TYPE
4561 && TYPE_PTR_P (type0) && integer_zerop (op0)))
4563 if (TYPE_PTR_P (type0))
4564 result_type = composite_pointer_type (type0, type1, op0, op1,
4565 CPO_COMPARISON, complain);
4567 result_type = type1;
4569 warn_for_null_address (location, op1, complain);
4571 else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4572 || (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
4573 result_type = composite_pointer_type (type0, type1, op0, op1,
4574 CPO_COMPARISON, complain);
4575 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4576 /* One of the operands must be of nullptr_t type. */
4577 result_type = TREE_TYPE (nullptr_node);
4578 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4580 result_type = type0;
4581 if (complain & tf_error)
4582 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4584 return error_mark_node;
4586 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4588 result_type = type1;
4589 if (complain & tf_error)
4590 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4592 return error_mark_node;
4594 else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (op1))
4596 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4597 == ptrmemfunc_vbit_in_delta)
4599 tree pfn0, delta0, e1, e2;
4601 if (TREE_SIDE_EFFECTS (op0))
4602 op0 = save_expr (op0);
4604 pfn0 = pfn_from_ptrmemfunc (op0);
4605 delta0 = delta_from_ptrmemfunc (op0);
4606 e1 = cp_build_binary_op (location,
4609 build_zero_cst (TREE_TYPE (pfn0)),
4611 e2 = cp_build_binary_op (location,
4617 if (complain & tf_warning)
4618 maybe_warn_zero_as_null_pointer_constant (op1, input_location);
4620 e2 = cp_build_binary_op (location,
4621 EQ_EXPR, e2, integer_zero_node,
4623 op0 = cp_build_binary_op (location,
4624 TRUTH_ANDIF_EXPR, e1, e2,
4626 op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
4630 op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
4631 op1 = cp_convert (TREE_TYPE (op0), op1, complain);
4633 result_type = TREE_TYPE (op0);
4635 else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
4636 return cp_build_binary_op (location, code, op1, op0, complain);
4637 else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
4640 /* E will be the final comparison. */
4642 /* E1 and E2 are for scratch. */
4650 type = composite_pointer_type (type0, type1, op0, op1,
4651 CPO_COMPARISON, complain);
4653 if (!same_type_p (TREE_TYPE (op0), type))
4654 op0 = cp_convert_and_check (type, op0, complain);
4655 if (!same_type_p (TREE_TYPE (op1), type))
4656 op1 = cp_convert_and_check (type, op1, complain);
4658 if (op0 == error_mark_node || op1 == error_mark_node)
4659 return error_mark_node;
4661 if (TREE_SIDE_EFFECTS (op0))
4662 op0 = save_expr (op0);
4663 if (TREE_SIDE_EFFECTS (op1))
4664 op1 = save_expr (op1);
4666 pfn0 = pfn_from_ptrmemfunc (op0);
4667 pfn0 = cp_fully_fold (pfn0);
4668 /* Avoid -Waddress warnings (c++/64877). */
4669 if (TREE_CODE (pfn0) == ADDR_EXPR)
4670 TREE_NO_WARNING (pfn0) = 1;
4671 pfn1 = pfn_from_ptrmemfunc (op1);
4672 pfn1 = cp_fully_fold (pfn1);
4673 delta0 = delta_from_ptrmemfunc (op0);
4674 delta1 = delta_from_ptrmemfunc (op1);
4675 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
4676 == ptrmemfunc_vbit_in_delta)
4681 && ((op0.delta == op1.delta)
4682 || (!op0.pfn && op0.delta & 1 == 0
4683 && op1.delta & 1 == 0))
4685 The reason for the `!op0.pfn' bit is that a NULL
4686 pointer-to-member is any member with a zero PFN and
4687 LSB of the DELTA field is 0. */
4689 e1 = cp_build_binary_op (location, BIT_AND_EXPR,
4693 e1 = cp_build_binary_op (location,
4694 EQ_EXPR, e1, integer_zero_node,
4696 e2 = cp_build_binary_op (location, BIT_AND_EXPR,
4700 e2 = cp_build_binary_op (location,
4701 EQ_EXPR, e2, integer_zero_node,
4703 e1 = cp_build_binary_op (location,
4704 TRUTH_ANDIF_EXPR, e2, e1,
4706 e2 = cp_build_binary_op (location, EQ_EXPR,
4708 build_zero_cst (TREE_TYPE (pfn0)),
4710 e2 = cp_build_binary_op (location,
4711 TRUTH_ANDIF_EXPR, e2, e1, complain);
4712 e1 = cp_build_binary_op (location,
4713 EQ_EXPR, delta0, delta1, complain);
4714 e1 = cp_build_binary_op (location,
4715 TRUTH_ORIF_EXPR, e1, e2, complain);
4722 && (!op0.pfn || op0.delta == op1.delta))
4724 The reason for the `!op0.pfn' bit is that a NULL
4725 pointer-to-member is any member with a zero PFN; the
4726 DELTA field is unspecified. */
4728 e1 = cp_build_binary_op (location,
4729 EQ_EXPR, delta0, delta1, complain);
4730 e2 = cp_build_binary_op (location,
4733 build_zero_cst (TREE_TYPE (pfn0)),
4735 e1 = cp_build_binary_op (location,
4736 TRUTH_ORIF_EXPR, e1, e2, complain);
4738 e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
4739 e = cp_build_binary_op (location,
4740 TRUTH_ANDIF_EXPR, e2, e1, complain);
4741 if (code == EQ_EXPR)
4743 return cp_build_binary_op (location,
4744 EQ_EXPR, e, integer_zero_node, complain);
4748 gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
4749 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
4751 gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
4752 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
4760 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
4761 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
4763 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4764 result_type = composite_pointer_type (type0, type1, op0, op1,
4765 CPO_COMPARISON, complain);
4772 if (TREE_CODE (orig_op0) == STRING_CST
4773 || TREE_CODE (orig_op1) == STRING_CST)
4775 if (complain & tf_warning)
4776 warning (OPT_Waddress, "comparison with string literal results "
4777 "in unspecified behavior");
4780 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4784 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
4786 && !vector_types_compatible_elements_p (type0, type1))
4788 if (complain & tf_error)
4790 error_at (location, "comparing vectors with different "
4792 inform (location, "operand types are %qT and %qT",
4795 return error_mark_node;
4798 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
4800 if (complain & tf_error)
4802 error_at (location, "comparing vectors with different "
4803 "number of elements");
4804 inform (location, "operand types are %qT and %qT",
4807 return error_mark_node;
4810 /* It's not precisely specified how the usual arithmetic
4811 conversions apply to the vector types. Here, we use
4812 the unsigned type if one of the operands is signed and
4813 the other one is unsigned. */
4814 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
4816 if (!TYPE_UNSIGNED (type0))
4817 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
4819 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
4820 warning_at (location, OPT_Wsign_compare, "comparison between "
4821 "types %qT and %qT", type0, type1);
4824 /* Always construct signed integer vector type. */
4825 intt = c_common_type_for_size (GET_MODE_BITSIZE
4826 (TYPE_MODE (TREE_TYPE (type0))), 0);
4829 if (complain & tf_error)
4830 error_at (location, "could not find an integer type "
4831 "of the same size as %qT", TREE_TYPE (type0));
4832 return error_mark_node;
4834 result_type = build_opaque_vector_type (intt,
4835 TYPE_VECTOR_SUBPARTS (type0));
4837 return build_vec_cmp (resultcode, result_type, op0, op1);
4839 build_type = boolean_type_node;
4840 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
4841 || code0 == ENUMERAL_TYPE)
4842 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4843 || code1 == ENUMERAL_TYPE))
4845 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
4846 result_type = composite_pointer_type (type0, type1, op0, op1,
4847 CPO_COMPARISON, complain);
4848 else if (code0 == POINTER_TYPE && null_ptr_cst_p (op1))
4850 result_type = type0;
4851 if (extra_warnings && (complain & tf_warning))
4852 warning (OPT_Wextra,
4853 "ordered comparison of pointer with integer zero");
4855 else if (code1 == POINTER_TYPE && null_ptr_cst_p (op0))
4857 result_type = type1;
4858 if (extra_warnings && (complain & tf_warning))
4859 warning (OPT_Wextra,
4860 "ordered comparison of pointer with integer zero");
4862 else if (null_ptr_cst_p (op0) && null_ptr_cst_p (op1))
4863 /* One of the operands must be of nullptr_t type. */
4864 result_type = TREE_TYPE (nullptr_node);
4865 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
4867 result_type = type0;
4868 if (complain & tf_error)
4869 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4871 return error_mark_node;
4873 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
4875 result_type = type1;
4876 if (complain & tf_error)
4877 permerror (input_location, "ISO C++ forbids comparison between pointer and integer");
4879 return error_mark_node;
4883 case UNORDERED_EXPR:
4890 build_type = integer_type_node;
4891 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
4893 if (complain & tf_error)
4894 error ("unordered comparison on non-floating point argument");
4895 return error_mark_node;
4904 if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
4905 || code0 == ENUMERAL_TYPE)
4906 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4907 || code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
4908 arithmetic_types_p = 1;
4911 arithmetic_types_p = 0;
4912 /* Vector arithmetic is only allowed when both sides are vectors. */
4913 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
4915 if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
4916 || !vector_types_compatible_elements_p (type0, type1))
4918 if (complain & tf_error)
4920 /* "location" already embeds the locations of the
4921 operands, so we don't need to add them separately
4923 rich_location richloc (line_table, location);
4924 binary_op_error (&richloc, code, type0, type1);
4926 return error_mark_node;
4928 arithmetic_types_p = 1;
4931 /* Determine the RESULT_TYPE, if it is not already known. */
4933 && arithmetic_types_p
4934 && (shorten || common || short_compare))
4936 result_type = cp_common_type (type0, type1);
4937 if (complain & tf_warning)
4938 do_warn_double_promotion (result_type, type0, type1,
4939 "implicit conversion from %qT to %qT "
4940 "to match other operand of binary "
4947 if (complain & tf_error)
4949 "invalid operands of types %qT and %qT to binary %qO",
4950 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
4951 return error_mark_node;
4954 /* If we're in a template, the only thing we need to know is the
4956 if (processing_template_decl)
4958 /* Since the middle-end checks the type when doing a build2, we
4959 need to build the tree in pieces. This built tree will never
4960 get out of the front-end as we replace it when instantiating
4962 tree tmp = build2 (resultcode,
4963 build_type ? build_type : result_type,
4965 TREE_OPERAND (tmp, 0) = op0;
4969 if (arithmetic_types_p)
4971 bool first_complex = (code0 == COMPLEX_TYPE);
4972 bool second_complex = (code1 == COMPLEX_TYPE);
4973 int none_complex = (!first_complex && !second_complex);
4975 /* Adapted from patch for c/24581. */
4976 if (first_complex != second_complex
4977 && (code == PLUS_EXPR
4978 || code == MINUS_EXPR
4979 || code == MULT_EXPR
4980 || (code == TRUNC_DIV_EXPR && first_complex))
4981 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
4982 && flag_signed_zeros)
4984 /* An operation on mixed real/complex operands must be
4985 handled specially, but the language-independent code can
4986 more easily optimize the plain complex arithmetic if
4987 -fno-signed-zeros. */
4988 tree real_type = TREE_TYPE (result_type);
4992 if (TREE_TYPE (op0) != result_type)
4993 op0 = cp_convert_and_check (result_type, op0, complain);
4994 if (TREE_TYPE (op1) != real_type)
4995 op1 = cp_convert_and_check (real_type, op1, complain);
4999 if (TREE_TYPE (op0) != real_type)
5000 op0 = cp_convert_and_check (real_type, op0, complain);
5001 if (TREE_TYPE (op1) != result_type)
5002 op1 = cp_convert_and_check (result_type, op1, complain);
5004 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
5005 return error_mark_node;
5008 op0 = save_expr (op0);
5009 real = cp_build_unary_op (REALPART_EXPR, op0, 1, complain);
5010 imag = cp_build_unary_op (IMAGPART_EXPR, op0, 1, complain);
5014 case TRUNC_DIV_EXPR:
5015 op1 = save_expr (op1);
5016 imag = build2 (resultcode, real_type, imag, op1);
5020 real = build2 (resultcode, real_type, real, op1);
5028 op1 = save_expr (op1);
5029 real = cp_build_unary_op (REALPART_EXPR, op1, 1, complain);
5030 imag = cp_build_unary_op (IMAGPART_EXPR, op1, 1, complain);
5034 op0 = save_expr (op0);
5035 imag = build2 (resultcode, real_type, op0, imag);
5038 real = build2 (resultcode, real_type, op0, real);
5041 real = build2 (resultcode, real_type, op0, real);
5042 imag = build1 (NEGATE_EXPR, real_type, imag);
5048 result = build2 (COMPLEX_EXPR, result_type, real, imag);
5052 /* For certain operations (which identify themselves by shorten != 0)
5053 if both args were extended from the same smaller type,
5054 do the arithmetic in that type and then extend.
5056 shorten !=0 and !=1 indicates a bitwise operation.
5057 For them, this optimization is safe only if
5058 both args are zero-extended or both are sign-extended.
5059 Otherwise, we might change the result.
5060 E.g., (short)-1 | (unsigned short)-1 is (int)-1
5061 but calculated in (unsigned short) it would be (unsigned short)-1. */
5063 if (shorten && none_complex)
5065 orig_type = result_type;
5066 final_type = result_type;
5067 result_type = shorten_binary_op (result_type, op0, op1,
5071 /* Comparison operations are shortened too but differently.
5072 They identify themselves by setting short_compare = 1. */
5076 /* We call shorten_compare only for diagnostic-reason. */
5077 tree xop0 = fold_simple (op0), xop1 = fold_simple (op1),
5078 xresult_type = result_type;
5079 enum tree_code xresultcode = resultcode;
5080 shorten_compare (location, &xop0, &xop1, &xresult_type,
5084 if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
5085 && warn_sign_compare
5086 /* Do not warn until the template is instantiated; we cannot
5087 bound the ranges of the arguments until that point. */
5088 && !processing_template_decl
5089 && (complain & tf_warning)
5090 && c_inhibit_evaluation_warnings == 0
5091 /* Even unsigned enum types promote to signed int. We don't
5092 want to issue -Wsign-compare warnings for this case. */
5093 && !enum_cast_to_int (orig_op0)
5094 && !enum_cast_to_int (orig_op1))
5096 tree oop0 = maybe_constant_value (orig_op0);
5097 tree oop1 = maybe_constant_value (orig_op1);
5099 if (TREE_CODE (oop0) != INTEGER_CST)
5100 oop0 = cp_fully_fold (orig_op0);
5101 if (TREE_CODE (oop1) != INTEGER_CST)
5102 oop1 = cp_fully_fold (orig_op1);
5103 warn_for_sign_compare (location, oop0, oop1, op0, op1,
5104 result_type, resultcode);
5108 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
5109 Then the expression will be built.
5110 It will be given type FINAL_TYPE if that is nonzero;
5111 otherwise, it will be given type RESULT_TYPE. */
5114 if (TREE_TYPE (op0) != result_type)
5115 op0 = cp_convert_and_check (result_type, op0, complain);
5116 if (TREE_TYPE (op1) != result_type)
5117 op1 = cp_convert_and_check (result_type, op1, complain);
5119 if (op0 == error_mark_node || op1 == error_mark_node)
5120 return error_mark_node;
5123 if (build_type == NULL_TREE)
5124 build_type = result_type;
5126 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
5127 | SANITIZE_FLOAT_DIVIDE))
5128 && !processing_template_decl
5129 && do_ubsan_in_current_function ()
5130 && (doing_div_or_mod || doing_shift))
5132 /* OP0 and/or OP1 might have side-effects. */
5133 op0 = cp_save_expr (op0);
5134 op1 = cp_save_expr (op1);
5135 op0 = fold_non_dependent_expr (op0);
5136 op1 = fold_non_dependent_expr (op1);
5137 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
5138 | SANITIZE_FLOAT_DIVIDE)))
5140 /* For diagnostics we want to use the promoted types without
5141 shorten_binary_op. So convert the arguments to the
5142 original result_type. */
5145 if (orig_type != NULL && result_type != orig_type)
5147 cop0 = cp_convert (orig_type, op0, complain);
5148 cop1 = cp_convert (orig_type, op1, complain);
5150 instrument_expr = ubsan_instrument_division (location, cop0, cop1);
5152 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
5153 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
5156 result = build2_loc (location, resultcode, build_type, op0, op1);
5157 if (final_type != 0)
5158 result = cp_convert (final_type, result, complain);
5160 if (instrument_expr != NULL)
5161 result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
5162 instrument_expr, result);
5164 if (!processing_template_decl)
5166 op0 = cp_fully_fold (op0);
5167 /* Only consider the second argument if the first isn't overflowed. */
5168 if (!CONSTANT_CLASS_P (op0) || TREE_OVERFLOW_P (op0))
5170 op1 = cp_fully_fold (op1);
5171 if (!CONSTANT_CLASS_P (op1) || TREE_OVERFLOW_P (op1))
5174 else if (!CONSTANT_CLASS_P (op0) || !CONSTANT_CLASS_P (op1)
5175 || TREE_OVERFLOW_P (op0) || TREE_OVERFLOW_P (op1))
5178 result_ovl = fold_build2 (resultcode, build_type, op0, op1);
5179 if (TREE_OVERFLOW_P (result_ovl))
5180 overflow_warning (location, result_ovl);
5185 /* Build a VEC_PERM_EXPR.
5186 This is a simple wrapper for c_build_vec_perm_expr. */
5188 build_x_vec_perm_expr (location_t loc,
5189 tree arg0, tree arg1, tree arg2,
5190 tsubst_flags_t complain)
5192 tree orig_arg0 = arg0;
5193 tree orig_arg1 = arg1;
5194 tree orig_arg2 = arg2;
5195 if (processing_template_decl)
5197 if (type_dependent_expression_p (arg0)
5198 || type_dependent_expression_p (arg1)
5199 || type_dependent_expression_p (arg2))
5200 return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2);
5201 arg0 = build_non_dependent_expr (arg0);
5203 arg1 = build_non_dependent_expr (arg1);
5204 arg2 = build_non_dependent_expr (arg2);
5206 tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
5207 if (processing_template_decl && exp != error_mark_node)
5208 return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0,
5209 orig_arg1, orig_arg2);
5213 /* Return a tree for the sum or difference (RESULTCODE says which)
5214 of pointer PTROP and integer INTOP. */
5217 cp_pointer_int_sum (enum tree_code resultcode, tree ptrop, tree intop,
5218 tsubst_flags_t complain)
5220 tree res_type = TREE_TYPE (ptrop);
5222 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
5223 in certain circumstance (when it's valid to do so). So we need
5224 to make sure it's complete. We don't need to check here, if we
5225 can actually complete it at all, as those checks will be done in
5226 pointer_int_sum() anyway. */
5227 complete_type (TREE_TYPE (res_type));
5229 return pointer_int_sum (input_location, resultcode, ptrop,
5230 intop, complain & tf_warning_or_error);
5233 /* Return a tree for the difference of pointers OP0 and OP1.
5234 The resulting tree has type int. */
5237 pointer_diff (tree op0, tree op1, tree ptrtype, tsubst_flags_t complain)
5240 tree restype = ptrdiff_type_node;
5241 tree target_type = TREE_TYPE (ptrtype);
5243 if (!complete_type_or_else (target_type, NULL_TREE))
5244 return error_mark_node;
5246 if (VOID_TYPE_P (target_type))
5248 if (complain & tf_error)
5249 permerror (input_location, "ISO C++ forbids using pointer of "
5250 "type %<void *%> in subtraction");
5252 return error_mark_node;
5254 if (TREE_CODE (target_type) == FUNCTION_TYPE)
5256 if (complain & tf_error)
5257 permerror (input_location, "ISO C++ forbids using pointer to "
5258 "a function in subtraction");
5260 return error_mark_node;
5262 if (TREE_CODE (target_type) == METHOD_TYPE)
5264 if (complain & tf_error)
5265 permerror (input_location, "ISO C++ forbids using pointer to "
5266 "a method in subtraction");
5268 return error_mark_node;
5271 /* First do the subtraction as integers;
5272 then drop through to build the divide operator. */
5274 op0 = cp_build_binary_op (input_location,
5276 cp_convert (restype, op0, complain),
5277 cp_convert (restype, op1, complain),
5280 /* This generates an error if op1 is a pointer to an incomplete type. */
5281 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
5283 if (complain & tf_error)
5284 error ("invalid use of a pointer to an incomplete type in "
5285 "pointer arithmetic");
5287 return error_mark_node;
5290 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1)))
5292 if (complain & tf_error)
5293 error ("arithmetic on pointer to an empty aggregate");
5295 return error_mark_node;
5298 op1 = (TYPE_PTROB_P (ptrtype)
5299 ? size_in_bytes (target_type)
5300 : integer_one_node);
5302 /* Do the division. */
5304 result = build2 (EXACT_DIV_EXPR, restype, op0,
5305 cp_convert (restype, op1, complain));
5309 /* Construct and perhaps optimize a tree representation
5310 for a unary operation. CODE, a tree_code, specifies the operation
5311 and XARG is the operand. */
5314 build_x_unary_op (location_t loc, enum tree_code code, cp_expr xarg,
5315 tsubst_flags_t complain)
5317 tree orig_expr = xarg;
5320 tree overload = NULL_TREE;
5322 if (processing_template_decl)
5324 if (type_dependent_expression_p (xarg))
5325 return build_min_nt_loc (loc, code, xarg.get_value (), NULL_TREE);
5327 xarg = build_non_dependent_expr (xarg);
5332 /* [expr.unary.op] says:
5334 The address of an object of incomplete type can be taken.
5336 (And is just the ordinary address operator, not an overloaded
5337 "operator &".) However, if the type is a template
5338 specialization, we must complete the type at this point so that
5339 an overloaded "operator &" will be available if required. */
5340 if (code == ADDR_EXPR
5341 && TREE_CODE (xarg) != TEMPLATE_ID_EXPR
5342 && ((CLASS_TYPE_P (TREE_TYPE (xarg))
5343 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
5344 || (TREE_CODE (xarg) == OFFSET_REF)))
5345 /* Don't look for a function. */;
5347 exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
5348 NULL_TREE, &overload, complain);
5350 if (!exp && code == ADDR_EXPR)
5352 if (is_overloaded_fn (xarg))
5354 tree fn = get_first_fn (xarg);
5355 if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
5357 if (complain & tf_error)
5358 error (DECL_CONSTRUCTOR_P (fn)
5359 ? G_("taking address of constructor %qE")
5360 : G_("taking address of destructor %qE"),
5362 return error_mark_node;
5366 /* A pointer to member-function can be formed only by saying
5368 if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
5369 && (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
5371 if (TREE_CODE (xarg) != OFFSET_REF
5372 || !TYPE_P (TREE_OPERAND (xarg, 0)))
5374 if (complain & tf_error)
5376 error ("invalid use of %qE to form a "
5377 "pointer-to-member-function", xarg.get_value ());
5378 if (TREE_CODE (xarg) != OFFSET_REF)
5379 inform (input_location, " a qualified-id is required");
5381 return error_mark_node;
5385 if (complain & tf_error)
5386 error ("parentheses around %qE cannot be used to form a"
5387 " pointer-to-member-function",
5390 return error_mark_node;
5391 PTRMEM_OK_P (xarg) = 1;
5395 if (TREE_CODE (xarg) == OFFSET_REF)
5397 ptrmem = PTRMEM_OK_P (xarg);
5399 if (!ptrmem && !flag_ms_extensions
5400 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
5402 /* A single non-static member, make sure we don't allow a
5403 pointer-to-member. */
5404 xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
5405 TREE_OPERAND (xarg, 0),
5406 ovl_cons (TREE_OPERAND (xarg, 1), NULL_TREE));
5407 PTRMEM_OK_P (xarg) = ptrmem;
5411 exp = cp_build_addr_expr_strict (xarg, complain);
5414 if (processing_template_decl && exp != error_mark_node)
5416 if (overload != NULL_TREE)
5417 return (build_min_non_dep_op_overload
5418 (code, exp, overload, orig_expr, integer_zero_node));
5420 exp = build_min_non_dep (code, exp, orig_expr,
5421 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
5423 if (TREE_CODE (exp) == ADDR_EXPR)
5424 PTRMEM_OK_P (exp) = ptrmem;
5428 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
5429 constants, where a null value is represented by an INTEGER_CST of
5433 cp_truthvalue_conversion (tree expr)
5435 tree type = TREE_TYPE (expr);
5436 if (TYPE_PTRDATAMEM_P (type)
5437 /* Avoid ICE on invalid use of non-static member function. */
5438 || TREE_CODE (expr) == FUNCTION_DECL)
5439 return build_binary_op (EXPR_LOCATION (expr),
5440 NE_EXPR, expr, nullptr_node, 1);
5441 else if (TYPE_PTR_P (type) || TYPE_PTRMEMFUNC_P (type))
5443 /* With -Wzero-as-null-pointer-constant do not warn for an
5444 'if (p)' or a 'while (!p)', where p is a pointer. */
5446 ++c_inhibit_evaluation_warnings;
5447 ret = c_common_truthvalue_conversion (input_location, expr);
5448 --c_inhibit_evaluation_warnings;
5452 return c_common_truthvalue_conversion (input_location, expr);
5455 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. */
5458 condition_conversion (tree expr)
5461 if (processing_template_decl)
5463 t = perform_implicit_conversion_flags (boolean_type_node, expr,
5464 tf_warning_or_error, LOOKUP_NORMAL);
5465 t = fold_build_cleanup_point_expr (boolean_type_node, t);
5469 /* Returns the address of T. This function will fold away
5470 ADDR_EXPR of INDIRECT_REF. */
5473 build_address (tree t)
5475 if (error_operand_p (t) || !cxx_mark_addressable (t))
5476 return error_mark_node;
5477 gcc_checking_assert (TREE_CODE (t) != CONSTRUCTOR);
5478 t = build_fold_addr_expr (t);
5479 if (TREE_CODE (t) != ADDR_EXPR)
5484 /* Return a NOP_EXPR converting EXPR to TYPE. */
5487 build_nop (tree type, tree expr)
5489 if (type == error_mark_node || error_operand_p (expr))
5491 return build1_loc (EXPR_LOCATION (expr), NOP_EXPR, type, expr);
5494 /* Take the address of ARG, whatever that means under C++ semantics.
5495 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
5496 and class rvalues as well.
5498 Nothing should call this function directly; instead, callers should use
5499 cp_build_addr_expr or cp_build_addr_expr_strict. */
5502 cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
5507 if (!arg || error_operand_p (arg))
5508 return error_mark_node;
5510 arg = mark_lvalue_use (arg);
5511 argtype = lvalue_type (arg);
5513 gcc_assert (!identifier_p (arg) || !IDENTIFIER_OPNAME_P (arg));
5515 if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
5516 && !really_overloaded_fn (TREE_OPERAND (arg, 1)))
5518 /* They're trying to take the address of a unique non-static
5519 member function. This is ill-formed (except in MS-land),
5520 but let's try to DTRT.
5521 Note: We only handle unique functions here because we don't
5522 want to complain if there's a static overload; non-unique
5523 cases will be handled by instantiate_type. But we need to
5524 handle this case here to allow casts on the resulting PMF.
5525 We could defer this in non-MS mode, but it's easier to give
5526 a useful error here. */
5528 /* Inside constant member functions, the `this' pointer
5529 contains an extra const qualifier. TYPE_MAIN_VARIANT
5530 is used here to remove this const from the diagnostics
5531 and the created OFFSET_REF. */
5532 tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
5533 tree fn = get_first_fn (TREE_OPERAND (arg, 1));
5534 if (!mark_used (fn, complain) && !(complain & tf_error))
5535 return error_mark_node;
5537 if (! flag_ms_extensions)
5539 tree name = DECL_NAME (fn);
5540 if (!(complain & tf_error))
5541 return error_mark_node;
5542 else if (current_class_type
5543 && TREE_OPERAND (arg, 0) == current_class_ref)
5544 /* An expression like &memfn. */
5545 permerror (input_location, "ISO C++ forbids taking the address of an unqualified"
5546 " or parenthesized non-static member function to form"
5547 " a pointer to member function. Say %<&%T::%D%>",
5550 permerror (input_location, "ISO C++ forbids taking the address of a bound member"
5551 " function to form a pointer to member function."
5555 arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
5558 /* Uninstantiated types are all functions. Taking the
5559 address of a function is a no-op, so just return the
5561 if (type_unknown_p (arg))
5562 return build1 (ADDR_EXPR, unknown_type_node, arg);
5564 if (TREE_CODE (arg) == OFFSET_REF)
5565 /* We want a pointer to member; bypass all the code for actually taking
5566 the address of something. */
5569 /* Anything not already handled and not a true memory reference
5571 if (TREE_CODE (argtype) != FUNCTION_TYPE
5572 && TREE_CODE (argtype) != METHOD_TYPE)
5574 cp_lvalue_kind kind = lvalue_kind (arg);
5575 if (kind == clk_none)
5577 if (complain & tf_error)
5578 lvalue_error (input_location, lv_addressof);
5579 return error_mark_node;
5581 if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
5583 if (!(complain & tf_error))
5584 return error_mark_node;
5585 if (kind & clk_class)
5586 /* Make this a permerror because we used to accept it. */
5587 permerror (input_location, "taking address of temporary");
5589 error ("taking address of xvalue (rvalue reference)");
5593 if (TREE_CODE (argtype) == REFERENCE_TYPE)
5595 tree type = build_pointer_type (TREE_TYPE (argtype));
5596 arg = build1 (CONVERT_EXPR, type, arg);
5599 else if (pedantic && DECL_MAIN_P (arg))
5602 /* Apparently a lot of autoconf scripts for C++ packages do this,
5603 so only complain if -Wpedantic. */
5604 if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
5605 pedwarn (input_location, OPT_Wpedantic,
5606 "ISO C++ forbids taking address of function %<::main%>");
5607 else if (flag_pedantic_errors)
5608 return error_mark_node;
5611 /* Let &* cancel out to simplify resulting code. */
5612 if (INDIRECT_REF_P (arg))
5614 /* We don't need to have `current_class_ptr' wrapped in a
5615 NON_LVALUE_EXPR node. */
5616 if (arg == current_class_ref)
5617 return current_class_ptr;
5619 arg = TREE_OPERAND (arg, 0);
5620 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE)
5622 tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
5623 arg = build1 (CONVERT_EXPR, type, arg);
5626 /* Don't let this be an lvalue. */
5631 /* ??? Cope with user tricks that amount to offsetof. */
5632 if (TREE_CODE (argtype) != FUNCTION_TYPE
5633 && TREE_CODE (argtype) != METHOD_TYPE
5634 && argtype != unknown_type_node
5635 && (val = get_base_address (arg))
5636 && COMPLETE_TYPE_P (TREE_TYPE (val))
5637 && INDIRECT_REF_P (val)
5638 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
5640 tree type = build_pointer_type (argtype);
5641 return fold_convert (type, fold_offsetof_1 (arg));
5644 /* Handle complex lvalues (when permitted)
5645 by reduction to simpler cases. */
5646 val = unary_complex_lvalue (ADDR_EXPR, arg);
5650 switch (TREE_CODE (arg))
5654 case FIX_TRUNC_EXPR:
5655 /* Even if we're not being pedantic, we cannot allow this
5656 extension when we're instantiating in a SFINAE
5658 if (! lvalue_p (arg) && complain == tf_none)
5660 if (complain & tf_error)
5661 permerror (input_location, "ISO C++ forbids taking the address of a cast to a non-lvalue expression");
5663 return error_mark_node;
5668 arg = BASELINK_FUNCTIONS (arg);
5672 arg = OVL_CURRENT (arg);
5677 /* Turn a reference to a non-static data member into a
5678 pointer-to-member. */
5683 gcc_assert (PTRMEM_OK_P (arg));
5685 t = TREE_OPERAND (arg, 1);
5686 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
5688 if (complain & tf_error)
5689 error ("cannot create pointer to reference member %qD", t);
5690 return error_mark_node;
5693 type = build_ptrmem_type (context_for_name_lookup (t),
5695 t = make_ptrmem_cst (type, TREE_OPERAND (arg, 1));
5703 if (argtype != error_mark_node)
5704 argtype = build_pointer_type (argtype);
5706 /* In a template, we are processing a non-dependent expression
5707 so we can just form an ADDR_EXPR with the correct type. */
5708 if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
5710 val = build_address (arg);
5711 if (TREE_CODE (arg) == OFFSET_REF)
5712 PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
5714 else if (BASELINK_P (TREE_OPERAND (arg, 1)))
5716 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
5718 /* We can only get here with a single static member
5720 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
5721 && DECL_STATIC_FUNCTION_P (fn));
5722 if (!mark_used (fn, complain) && !(complain & tf_error))
5723 return error_mark_node;
5724 val = build_address (fn);
5725 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
5726 /* Do not lose object's side effects. */
5727 val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
5728 TREE_OPERAND (arg, 0), val);
5730 else if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
5732 if (complain & tf_error)
5733 error ("attempt to take address of bit-field structure member %qD",
5734 TREE_OPERAND (arg, 1));
5735 return error_mark_node;
5739 tree object = TREE_OPERAND (arg, 0);
5740 tree field = TREE_OPERAND (arg, 1);
5741 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5742 (TREE_TYPE (object), decl_type_context (field)));
5743 val = build_address (arg);
5746 if (TYPE_PTR_P (argtype)
5747 && TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
5749 build_ptrmemfunc_type (argtype);
5750 val = build_ptrmemfunc (argtype, val, 0,
5758 /* Take the address of ARG if it has one, even if it's an rvalue. */
5761 cp_build_addr_expr (tree arg, tsubst_flags_t complain)
5763 return cp_build_addr_expr_1 (arg, 0, complain);
5766 /* Take the address of ARG, but only if it's an lvalue. */
5769 cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
5771 return cp_build_addr_expr_1 (arg, 1, complain);
5774 /* C++: Must handle pointers to members.
5776 Perhaps type instantiation should be extended to handle conversion
5777 from aggregates to types we don't yet know we want? (Or are those
5778 cases typically errors which should be reported?)
5780 NOCONVERT nonzero suppresses the default promotions
5781 (such as from short to int). */
5784 cp_build_unary_op (enum tree_code code, tree xarg, int noconvert,
5785 tsubst_flags_t complain)
5787 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
5790 const char *errstring = NULL;
5792 const char *invalid_op_diag;
5794 if (!arg || error_operand_p (arg))
5795 return error_mark_node;
5797 if ((invalid_op_diag
5798 = targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
5803 if (complain & tf_error)
5804 error (invalid_op_diag);
5805 return error_mark_node;
5810 case UNARY_PLUS_EXPR:
5813 int flags = WANT_ARITH | WANT_ENUM;
5814 /* Unary plus (but not unary minus) is allowed on pointers. */
5815 if (code == UNARY_PLUS_EXPR)
5816 flags |= WANT_POINTER;
5817 arg = build_expr_type_conversion (flags, arg, true);
5819 errstring = (code == NEGATE_EXPR
5820 ? _("wrong type argument to unary minus")
5821 : _("wrong type argument to unary plus"));
5824 if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5825 arg = cp_perform_integral_promotions (arg, complain);
5827 /* Make sure the result is not an lvalue: a unary plus or minus
5828 expression is always a rvalue. */
5835 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5840 arg = cp_default_conversion (arg, complain);
5841 if (arg == error_mark_node)
5842 return error_mark_node;
5845 else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
5846 | WANT_VECTOR_OR_COMPLEX,
5848 errstring = _("wrong type argument to bit-complement");
5849 else if (!noconvert && CP_INTEGRAL_TYPE_P (TREE_TYPE (arg)))
5850 arg = cp_perform_integral_promotions (arg, complain);
5851 else if (!noconvert && VECTOR_TYPE_P (TREE_TYPE (arg)))
5852 arg = mark_rvalue_use (arg);
5856 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5857 errstring = _("wrong type argument to abs");
5858 else if (!noconvert)
5860 arg = cp_default_conversion (arg, complain);
5861 if (arg == error_mark_node)
5862 return error_mark_node;
5867 /* Conjugating a real value is a no-op, but allow it anyway. */
5868 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
5869 errstring = _("wrong type argument to conjugation");
5870 else if (!noconvert)
5872 arg = cp_default_conversion (arg, complain);
5873 if (arg == error_mark_node)
5874 return error_mark_node;
5878 case TRUTH_NOT_EXPR:
5879 if (VECTOR_TYPE_P (TREE_TYPE (arg)))
5880 return cp_build_binary_op (input_location, EQ_EXPR, arg,
5881 build_zero_cst (TREE_TYPE (arg)), complain);
5882 arg = perform_implicit_conversion (boolean_type_node, arg,
5884 val = invert_truthvalue_loc (input_location, arg);
5885 if (arg != error_mark_node)
5887 errstring = _("in argument to unary !");
5895 arg = build_real_imag_expr (input_location, code, arg);
5898 case PREINCREMENT_EXPR:
5899 case POSTINCREMENT_EXPR:
5900 case PREDECREMENT_EXPR:
5901 case POSTDECREMENT_EXPR:
5902 /* Handle complex lvalues (when permitted)
5903 by reduction to simpler cases. */
5905 val = unary_complex_lvalue (code, arg);
5909 arg = mark_lvalue_use (arg);
5911 /* Increment or decrement the real part of the value,
5912 and don't change the imaginary part. */
5913 if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
5917 arg = stabilize_reference (arg);
5918 real = cp_build_unary_op (REALPART_EXPR, arg, 1, complain);
5919 imag = cp_build_unary_op (IMAGPART_EXPR, arg, 1, complain);
5920 real = cp_build_unary_op (code, real, 1, complain);
5921 if (real == error_mark_node || imag == error_mark_node)
5922 return error_mark_node;
5923 return build2 (COMPLEX_EXPR, TREE_TYPE (arg),
5927 /* Report invalid types. */
5929 if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
5932 if (code == PREINCREMENT_EXPR)
5933 errstring = _("no pre-increment operator for type");
5934 else if (code == POSTINCREMENT_EXPR)
5935 errstring = _("no post-increment operator for type");
5936 else if (code == PREDECREMENT_EXPR)
5937 errstring = _("no pre-decrement operator for type");
5939 errstring = _("no post-decrement operator for type");
5942 else if (arg == error_mark_node)
5943 return error_mark_node;
5945 /* Report something read-only. */
5947 if (CP_TYPE_CONST_P (TREE_TYPE (arg))
5948 || TREE_READONLY (arg))
5950 if (complain & tf_error)
5951 cxx_readonly_error (arg, ((code == PREINCREMENT_EXPR
5952 || code == POSTINCREMENT_EXPR)
5953 ? lv_increment : lv_decrement));
5955 return error_mark_node;
5960 tree declared_type = unlowered_expr_type (arg);
5962 argtype = TREE_TYPE (arg);
5964 /* ARM $5.2.5 last annotation says this should be forbidden. */
5965 if (TREE_CODE (argtype) == ENUMERAL_TYPE)
5967 if (complain & tf_error)
5968 permerror (input_location, (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
5969 ? G_("ISO C++ forbids incrementing an enum")
5970 : G_("ISO C++ forbids decrementing an enum"));
5972 return error_mark_node;
5975 /* Compute the increment. */
5977 if (TYPE_PTR_P (argtype))
5979 tree type = complete_type (TREE_TYPE (argtype));
5981 if (!COMPLETE_OR_VOID_TYPE_P (type))
5983 if (complain & tf_error)
5984 error (((code == PREINCREMENT_EXPR
5985 || code == POSTINCREMENT_EXPR))
5986 ? G_("cannot increment a pointer to incomplete type %qT")
5987 : G_("cannot decrement a pointer to incomplete type %qT"),
5988 TREE_TYPE (argtype));
5990 return error_mark_node;
5992 else if (!TYPE_PTROB_P (argtype))
5994 if (complain & tf_error)
5995 pedwarn (input_location, OPT_Wpointer_arith,
5996 (code == PREINCREMENT_EXPR
5997 || code == POSTINCREMENT_EXPR)
5998 ? G_("ISO C++ forbids incrementing a pointer of type %qT")
5999 : G_("ISO C++ forbids decrementing a pointer of type %qT"),
6002 return error_mark_node;
6005 inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
6008 inc = VECTOR_TYPE_P (argtype)
6009 ? build_one_cst (argtype)
6012 inc = cp_convert (argtype, inc, complain);
6014 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
6015 need to ask Objective-C to build the increment or decrement
6016 expression for it. */
6017 if (objc_is_property_ref (arg))
6018 return objc_build_incr_expr_for_property_ref (input_location, code,
6021 /* Complain about anything else that is not a true lvalue. */
6022 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
6023 || code == POSTINCREMENT_EXPR)
6024 ? lv_increment : lv_decrement),
6026 return error_mark_node;
6028 /* Forbid using -- on `bool'. */
6029 if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
6031 if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
6033 if (complain & tf_error)
6034 error ("invalid use of Boolean expression as operand "
6035 "to %<operator--%>");
6036 return error_mark_node;
6038 val = boolean_increment (code, arg);
6040 else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
6041 /* An rvalue has no cv-qualifiers. */
6042 val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
6044 val = build2 (code, TREE_TYPE (arg), arg, inc);
6046 TREE_SIDE_EFFECTS (val) = 1;
6051 /* Note that this operation never does default_conversion
6052 regardless of NOCONVERT. */
6053 return cp_build_addr_expr (arg, complain);
6062 argtype = TREE_TYPE (arg);
6063 return build1 (code, argtype, arg);
6066 if (complain & tf_error)
6067 error ("%s", errstring);
6068 return error_mark_node;
6071 /* Hook for the c-common bits that build a unary op. */
6073 build_unary_op (location_t /*location*/,
6074 enum tree_code code, tree xarg, int noconvert)
6076 return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
6079 /* Apply unary lvalue-demanding operator CODE to the expression ARG
6080 for certain kinds of expressions which are not really lvalues
6081 but which we can accept as lvalues.
6083 If ARG is not a kind of expression we can handle, return
6087 unary_complex_lvalue (enum tree_code code, tree arg)
6089 /* Inside a template, making these kinds of adjustments is
6090 pointless; we are only concerned with the type of the
6092 if (processing_template_decl)
6095 /* Handle (a, b) used as an "lvalue". */
6096 if (TREE_CODE (arg) == COMPOUND_EXPR)
6098 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), 0,
6099 tf_warning_or_error);
6100 return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
6101 TREE_OPERAND (arg, 0), real_result);
6104 /* Handle (a ? b : c) used as an "lvalue". */
6105 if (TREE_CODE (arg) == COND_EXPR
6106 || TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
6107 return rationalize_conditional_expr (code, arg, tf_warning_or_error);
6109 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
6110 if (TREE_CODE (arg) == MODIFY_EXPR
6111 || TREE_CODE (arg) == PREINCREMENT_EXPR
6112 || TREE_CODE (arg) == PREDECREMENT_EXPR)
6114 tree lvalue = TREE_OPERAND (arg, 0);
6115 if (TREE_SIDE_EFFECTS (lvalue))
6117 lvalue = stabilize_reference (lvalue);
6118 arg = build2 (TREE_CODE (arg), TREE_TYPE (arg),
6119 lvalue, TREE_OPERAND (arg, 1));
6121 return unary_complex_lvalue
6122 (code, build2 (COMPOUND_EXPR, TREE_TYPE (lvalue), arg, lvalue));
6125 if (code != ADDR_EXPR)
6128 /* Handle (a = b) used as an "lvalue" for `&'. */
6129 if (TREE_CODE (arg) == MODIFY_EXPR
6130 || TREE_CODE (arg) == INIT_EXPR)
6132 tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), 0,
6133 tf_warning_or_error);
6134 arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
6136 TREE_NO_WARNING (arg) = 1;
6140 if (TREE_CODE (TREE_TYPE (arg)) == FUNCTION_TYPE
6141 || TREE_CODE (TREE_TYPE (arg)) == METHOD_TYPE
6142 || TREE_CODE (arg) == OFFSET_REF)
6145 /* We permit compiler to make function calls returning
6146 objects of aggregate type look like lvalues. */
6150 if (TREE_CODE (targ) == SAVE_EXPR)
6151 targ = TREE_OPERAND (targ, 0);
6153 if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
6155 if (TREE_CODE (arg) == SAVE_EXPR)
6158 targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
6159 return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
6162 if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
6163 return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
6164 TREE_OPERAND (targ, 0), current_function_decl, NULL);
6167 /* Don't let anything else be handled specially. */
6171 /* Mark EXP saying that we need to be able to take the
6172 address of it; it should not be allocated in a register.
6173 Value is true if successful.
6175 C++: we do not allow `current_class_ptr' to be addressable. */
6178 cxx_mark_addressable (tree exp)
6183 switch (TREE_CODE (x))
6190 x = TREE_OPERAND (x, 0);
6194 if (x == current_class_ptr)
6196 error ("cannot take the address of %<this%>, which is an rvalue expression");
6197 TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
6203 /* Caller should not be trying to mark initialized
6204 constant fields addressable. */
6205 gcc_assert (DECL_LANG_SPECIFIC (x) == 0
6206 || DECL_IN_AGGR_P (x) == 0
6208 || DECL_EXTERNAL (x));
6212 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
6213 && !DECL_ARTIFICIAL (x))
6215 if (VAR_P (x) && DECL_HARD_REGISTER (x))
6218 ("address of explicit register variable %qD requested", x);
6221 else if (extra_warnings)
6223 (OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
6225 TREE_ADDRESSABLE (x) = 1;
6230 TREE_ADDRESSABLE (x) = 1;
6234 TREE_ADDRESSABLE (x) = 1;
6238 TREE_ADDRESSABLE (x) = 1;
6239 cxx_mark_addressable (TREE_OPERAND (x, 0));
6247 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
6250 build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
6251 tsubst_flags_t complain)
6253 tree orig_ifexp = ifexp;
6254 tree orig_op1 = op1;
6255 tree orig_op2 = op2;
6258 if (processing_template_decl)
6260 /* The standard says that the expression is type-dependent if
6261 IFEXP is type-dependent, even though the eventual type of the
6262 expression doesn't dependent on IFEXP. */
6263 if (type_dependent_expression_p (ifexp)
6264 /* As a GNU extension, the middle operand may be omitted. */
6265 || (op1 && type_dependent_expression_p (op1))
6266 || type_dependent_expression_p (op2))
6267 return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
6268 ifexp = build_non_dependent_expr (ifexp);
6270 op1 = build_non_dependent_expr (op1);
6271 op2 = build_non_dependent_expr (op2);
6274 expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
6275 if (processing_template_decl && expr != error_mark_node)
6277 tree min = build_min_non_dep (COND_EXPR, expr,
6278 orig_ifexp, orig_op1, orig_op2);
6279 /* Remember that the result is an lvalue or xvalue. */
6280 if (lvalue_or_rvalue_with_address_p (expr)
6281 && !lvalue_or_rvalue_with_address_p (min))
6282 TREE_TYPE (min) = cp_build_reference_type (TREE_TYPE (min),
6283 !real_lvalue_p (expr));
6284 expr = convert_from_reference (min);
6289 /* Given a list of expressions, return a compound expression
6290 that performs them all and returns the value of the last of them. */
6293 build_x_compound_expr_from_list (tree list, expr_list_kind exp,
6294 tsubst_flags_t complain)
6296 tree expr = TREE_VALUE (list);
6298 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
6299 && !CONSTRUCTOR_IS_DIRECT_INIT (expr))
6301 if (complain & tf_error)
6302 pedwarn (EXPR_LOC_OR_LOC (expr, input_location), 0,
6303 "list-initializer for non-class type must not "
6304 "be parenthesized");
6306 return error_mark_node;
6309 if (TREE_CHAIN (list))
6311 if (complain & tf_error)
6315 permerror (input_location, "expression list treated as compound "
6316 "expression in initializer");
6319 permerror (input_location, "expression list treated as compound "
6320 "expression in mem-initializer");
6323 permerror (input_location, "expression list treated as compound "
6324 "expression in functional cast");
6330 return error_mark_node;
6332 for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
6333 expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
6334 expr, TREE_VALUE (list), complain);
6340 /* Like build_x_compound_expr_from_list, but using a VEC. */
6343 build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
6344 tsubst_flags_t complain)
6346 if (vec_safe_is_empty (vec))
6348 else if (vec->length () == 1)
6358 if (complain & tf_error)
6359 permerror (input_location,
6360 "%s expression list treated as compound expression",
6363 return error_mark_node;
6367 for (ix = 1; vec->iterate (ix, &t); ++ix)
6368 expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
6375 /* Handle overloading of the ',' operator when needed. */
6378 build_x_compound_expr (location_t loc, tree op1, tree op2,
6379 tsubst_flags_t complain)
6382 tree orig_op1 = op1;
6383 tree orig_op2 = op2;
6384 tree overload = NULL_TREE;
6386 if (processing_template_decl)
6388 if (type_dependent_expression_p (op1)
6389 || type_dependent_expression_p (op2))
6390 return build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
6391 op1 = build_non_dependent_expr (op1);
6392 op2 = build_non_dependent_expr (op2);
6395 result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
6396 NULL_TREE, &overload, complain);
6398 result = cp_build_compound_expr (op1, op2, complain);
6400 if (processing_template_decl && result != error_mark_node)
6402 if (overload != NULL_TREE)
6403 return (build_min_non_dep_op_overload
6404 (COMPOUND_EXPR, result, overload, orig_op1, orig_op2));
6406 return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
6412 /* Like cp_build_compound_expr, but for the c-common bits. */
6415 build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
6417 return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
6420 /* Build a compound expression. */
6423 cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
6425 lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
6427 if (lhs == error_mark_node || rhs == error_mark_node)
6428 return error_mark_node;
6431 && (TREE_CODE (lhs) == CILK_SPAWN_STMT
6432 || TREE_CODE (rhs) == CILK_SPAWN_STMT))
6434 location_t loc = (EXPR_HAS_LOCATION (lhs) ? EXPR_LOCATION (lhs)
6435 : EXPR_LOCATION (rhs));
6437 "spawned function call cannot be part of a comma expression");
6438 return error_mark_node;
6441 if (TREE_CODE (rhs) == TARGET_EXPR)
6443 /* If the rhs is a TARGET_EXPR, then build the compound
6444 expression inside the target_expr's initializer. This
6445 helps the compiler to eliminate unnecessary temporaries. */
6446 tree init = TREE_OPERAND (rhs, 1);
6448 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
6449 TREE_OPERAND (rhs, 1) = init;
6454 if (type_unknown_p (rhs))
6456 if (complain & tf_error)
6457 error ("no context to resolve type of %qE", rhs);
6458 return error_mark_node;
6461 return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
6464 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
6465 casts away constness. CAST gives the type of cast. Returns true
6466 if the cast is ill-formed, false if it is well-formed.
6468 ??? This function warns for casting away any qualifier not just
6469 const. We would like to specify exactly what qualifiers are casted
6474 check_for_casting_away_constness (tree src_type, tree dest_type,
6475 enum tree_code cast, tsubst_flags_t complain)
6477 /* C-style casts are allowed to cast away constness. With
6478 WARN_CAST_QUAL, we still want to issue a warning. */
6479 if (cast == CAST_EXPR && !warn_cast_qual)
6482 if (!casts_away_constness (src_type, dest_type, complain))
6488 if (complain & tf_warning)
6489 warning (OPT_Wcast_qual,
6490 "cast from type %qT to type %qT casts away qualifiers",
6491 src_type, dest_type);
6494 case STATIC_CAST_EXPR:
6495 if (complain & tf_error)
6496 error ("static_cast from type %qT to type %qT casts away qualifiers",
6497 src_type, dest_type);
6500 case REINTERPRET_CAST_EXPR:
6501 if (complain & tf_error)
6502 error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
6503 src_type, dest_type);
6512 Warns if the cast from expression EXPR to type TYPE is useless.
6515 maybe_warn_about_useless_cast (tree type, tree expr, tsubst_flags_t complain)
6517 if (warn_useless_cast
6518 && complain & tf_warning)
6520 if ((TREE_CODE (type) == REFERENCE_TYPE
6521 && (TYPE_REF_IS_RVALUE (type)
6522 ? xvalue_p (expr) : real_lvalue_p (expr))
6523 && same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
6524 || same_type_p (TREE_TYPE (expr), type))
6525 warning (OPT_Wuseless_cast, "useless cast to type %qT", type);
6529 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
6530 (another pointer-to-member type in the same hierarchy) and return
6531 the converted expression. If ALLOW_INVERSE_P is permitted, a
6532 pointer-to-derived may be converted to pointer-to-base; otherwise,
6533 only the other direction is permitted. If C_CAST_P is true, this
6534 conversion is taking place as part of a C-style cast. */
6537 convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
6538 bool c_cast_p, tsubst_flags_t complain)
6540 if (TYPE_PTRDATAMEM_P (type))
6544 if (TREE_CODE (expr) == PTRMEM_CST)
6545 expr = cplus_expand_constant (expr);
6546 delta = get_delta_difference (TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr)),
6547 TYPE_PTRMEM_CLASS_TYPE (type),
6549 c_cast_p, complain);
6550 if (delta == error_mark_node)
6551 return error_mark_node;
6553 if (!integer_zerop (delta))
6555 tree cond, op1, op2;
6557 cond = cp_build_binary_op (input_location,
6560 build_int_cst (TREE_TYPE (expr), -1),
6562 op1 = build_nop (ptrdiff_type_node, expr);
6563 op2 = cp_build_binary_op (input_location,
6564 PLUS_EXPR, op1, delta,
6567 expr = fold_build3_loc (input_location,
6568 COND_EXPR, ptrdiff_type_node, cond, op1, op2);
6572 return build_nop (type, expr);
6575 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
6576 allow_inverse_p, c_cast_p, complain);
6579 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
6580 this static_cast is being attempted as one of the possible casts
6581 allowed by a C-style cast. (In that case, accessibility of base
6582 classes is not considered, and it is OK to cast away
6583 constness.) Return the result of the cast. *VALID_P is set to
6584 indicate whether or not the cast was valid. */
6587 build_static_cast_1 (tree type, tree expr, bool c_cast_p,
6588 bool *valid_p, tsubst_flags_t complain)
6594 /* Assume the cast is valid. */
6597 intype = unlowered_expr_type (expr);
6599 /* Save casted types in the function's used types hash table. */
6600 used_types_insert (type);
6602 /* [expr.static.cast]
6604 An lvalue of type "cv1 B", where B is a class type, can be cast
6605 to type "reference to cv2 D", where D is a class derived (clause
6606 _class.derived_) from B, if a valid standard conversion from
6607 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
6608 same cv-qualification as, or greater cv-qualification than, cv1,
6609 and B is not a virtual base class of D. */
6610 /* We check this case before checking the validity of "TYPE t =
6611 EXPR;" below because for this case:
6614 struct D : public B { D(const B&); };
6616 void f() { static_cast<const D&>(b); }
6618 we want to avoid constructing a new D. The standard is not
6619 completely clear about this issue, but our interpretation is
6620 consistent with other compilers. */
6621 if (TREE_CODE (type) == REFERENCE_TYPE
6622 && CLASS_TYPE_P (TREE_TYPE (type))
6623 && CLASS_TYPE_P (intype)
6624 && (TYPE_REF_IS_RVALUE (type) || real_lvalue_p (expr))
6625 && DERIVED_FROM_P (intype, TREE_TYPE (type))
6626 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
6627 build_pointer_type (TYPE_MAIN_VARIANT
6628 (TREE_TYPE (type))),
6631 || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6635 /* There is a standard conversion from "D*" to "B*" even if "B"
6636 is ambiguous or inaccessible. If this is really a
6637 static_cast, then we check both for inaccessibility and
6638 ambiguity. However, if this is a static_cast being performed
6639 because the user wrote a C-style cast, then accessibility is
6641 base = lookup_base (TREE_TYPE (type), intype,
6642 c_cast_p ? ba_unique : ba_check,
6644 expr = build_address (expr);
6646 if (flag_sanitize & SANITIZE_VPTR)
6649 = cp_ubsan_maybe_instrument_downcast (input_location, type,
6655 /* Convert from "B*" to "D*". This function will check that "B"
6656 is not a virtual base of "D". */
6657 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
6660 /* Convert the pointer to a reference -- but then remember that
6661 there are no expressions with reference type in C++.
6663 We call rvalue so that there's an actual tree code
6664 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
6665 is a variable with the same type, the conversion would get folded
6666 away, leaving just the variable and causing lvalue_kind to give
6667 the wrong answer. */
6668 return convert_from_reference (rvalue (cp_fold_convert (type, expr)));
6671 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
6672 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
6673 if (TREE_CODE (type) == REFERENCE_TYPE
6674 && TYPE_REF_IS_RVALUE (type)
6675 && (clk = real_lvalue_p (expr))
6676 && reference_related_p (TREE_TYPE (type), intype)
6677 && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
6679 if (clk == clk_ordinary)
6681 /* Handle the (non-bit-field) lvalue case here by casting to
6682 lvalue reference and then changing it to an rvalue reference.
6683 Casting an xvalue to rvalue reference will be handled by the
6685 tree lref = cp_build_reference_type (TREE_TYPE (type), false);
6686 result = (perform_direct_initialization_if_possible
6687 (lref, expr, c_cast_p, complain));
6688 result = build1 (NON_LVALUE_EXPR, type, result);
6689 return convert_from_reference (result);
6692 /* For a bit-field or packed field, bind to a temporary. */
6693 expr = rvalue (expr);
6696 /* Resolve overloaded address here rather than once in
6697 implicit_conversion and again in the inverse code below. */
6698 if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
6700 expr = instantiate_type (type, expr, complain);
6701 intype = TREE_TYPE (expr);
6704 /* [expr.static.cast]
6706 Any expression can be explicitly converted to type cv void. */
6707 if (VOID_TYPE_P (type))
6708 return convert_to_void (expr, ICV_CAST, complain);
6711 An abstract class shall not be used ... as the type of an explicit
6713 if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
6714 return error_mark_node;
6716 /* [expr.static.cast]
6718 An expression e can be explicitly converted to a type T using a
6719 static_cast of the form static_cast<T>(e) if the declaration T
6720 t(e);" is well-formed, for some invented temporary variable
6722 result = perform_direct_initialization_if_possible (type, expr,
6723 c_cast_p, complain);
6726 result = convert_from_reference (result);
6728 /* [expr.static.cast]
6730 If T is a reference type, the result is an lvalue; otherwise,
6731 the result is an rvalue. */
6732 if (TREE_CODE (type) != REFERENCE_TYPE)
6734 result = rvalue (result);
6736 if (result == expr && SCALAR_TYPE_P (type))
6737 /* Leave some record of the cast. */
6738 result = build_nop (type, expr);
6743 /* [expr.static.cast]
6745 The inverse of any standard conversion sequence (clause _conv_),
6746 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
6747 (_conv.array_), function-to-pointer (_conv.func_), and boolean
6748 (_conv.bool_) conversions, can be performed explicitly using
6749 static_cast subject to the restriction that the explicit
6750 conversion does not cast away constness (_expr.const.cast_), and
6751 the following additional rules for specific cases: */
6752 /* For reference, the conversions not excluded are: integral
6753 promotions, floating point promotion, integral conversions,
6754 floating point conversions, floating-integral conversions,
6755 pointer conversions, and pointer to member conversions. */
6758 A value of integral _or enumeration_ type can be explicitly
6759 converted to an enumeration type. */
6760 /* The effect of all that is that any conversion between any two
6761 types which are integral, floating, or enumeration types can be
6763 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6764 || SCALAR_FLOAT_TYPE_P (type))
6765 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
6766 || SCALAR_FLOAT_TYPE_P (intype)))
6767 return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
6769 if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
6770 && CLASS_TYPE_P (TREE_TYPE (type))
6771 && CLASS_TYPE_P (TREE_TYPE (intype))
6772 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
6773 (TREE_TYPE (intype))),
6774 build_pointer_type (TYPE_MAIN_VARIANT
6775 (TREE_TYPE (type))),
6781 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6783 return error_mark_node;
6784 base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
6785 c_cast_p ? ba_unique : ba_check,
6787 expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
6790 if (flag_sanitize & SANITIZE_VPTR)
6793 = cp_ubsan_maybe_instrument_downcast (input_location, type,
6799 return cp_fold_convert (type, expr);
6802 if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
6803 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
6810 c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
6811 c2 = TYPE_PTRMEM_CLASS_TYPE (type);
6813 if (TYPE_PTRDATAMEM_P (type))
6815 t1 = (build_ptrmem_type
6817 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
6818 t2 = (build_ptrmem_type
6820 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
6827 if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
6830 && check_for_casting_away_constness (intype, type,
6833 return error_mark_node;
6834 return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
6835 c_cast_p, complain);
6839 /* [expr.static.cast]
6841 An rvalue of type "pointer to cv void" can be explicitly
6842 converted to a pointer to object type. A value of type pointer
6843 to object converted to "pointer to cv void" and back to the
6844 original pointer type will have its original value. */
6845 if (TYPE_PTR_P (intype)
6846 && VOID_TYPE_P (TREE_TYPE (intype))
6847 && TYPE_PTROB_P (type))
6850 && check_for_casting_away_constness (intype, type, STATIC_CAST_EXPR,
6852 return error_mark_node;
6853 return build_nop (type, expr);
6857 return error_mark_node;
6860 /* Return an expression representing static_cast<TYPE>(EXPR). */
6863 build_static_cast (tree type, tree expr, tsubst_flags_t complain)
6868 if (type == error_mark_node || expr == error_mark_node)
6869 return error_mark_node;
6871 if (processing_template_decl)
6873 expr = build_min (STATIC_CAST_EXPR, type, expr);
6874 /* We don't know if it will or will not have side effects. */
6875 TREE_SIDE_EFFECTS (expr) = 1;
6876 return convert_from_reference (expr);
6879 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
6880 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
6881 if (TREE_CODE (type) != REFERENCE_TYPE
6882 && TREE_CODE (expr) == NOP_EXPR
6883 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
6884 expr = TREE_OPERAND (expr, 0);
6886 result = build_static_cast_1 (type, expr, /*c_cast_p=*/false, &valid_p,
6890 if (result != error_mark_node)
6891 maybe_warn_about_useless_cast (type, expr, complain);
6895 if (complain & tf_error)
6896 error ("invalid static_cast from type %qT to type %qT",
6897 TREE_TYPE (expr), type);
6898 return error_mark_node;
6901 /* EXPR is an expression with member function or pointer-to-member
6902 function type. TYPE is a pointer type. Converting EXPR to TYPE is
6903 not permitted by ISO C++, but we accept it in some modes. If we
6904 are not in one of those modes, issue a diagnostic. Return the
6905 converted expression. */
6908 convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
6913 intype = TREE_TYPE (expr);
6914 gcc_assert (TYPE_PTRMEMFUNC_P (intype)
6915 || TREE_CODE (intype) == METHOD_TYPE);
6917 if (!(complain & tf_warning_or_error))
6918 return error_mark_node;
6920 if (pedantic || warn_pmf2ptr)
6921 pedwarn (input_location, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
6922 "converting from %qT to %qT", intype, type);
6924 if (TREE_CODE (intype) == METHOD_TYPE)
6925 expr = build_addr_func (expr, complain);
6926 else if (TREE_CODE (expr) == PTRMEM_CST)
6927 expr = build_address (PTRMEM_CST_MEMBER (expr));
6930 decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
6931 decl = build_address (decl);
6932 expr = get_member_function_from_ptrfunc (&decl, expr, complain);
6935 if (expr == error_mark_node)
6936 return error_mark_node;
6938 return build_nop (type, expr);
6941 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
6942 If C_CAST_P is true, this reinterpret cast is being done as part of
6943 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
6944 indicate whether or not reinterpret_cast was valid. */
6947 build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
6948 bool *valid_p, tsubst_flags_t complain)
6952 /* Assume the cast is invalid. */
6956 if (type == error_mark_node || error_operand_p (expr))
6957 return error_mark_node;
6959 intype = TREE_TYPE (expr);
6961 /* Save casted types in the function's used types hash table. */
6962 used_types_insert (type);
6964 /* [expr.reinterpret.cast]
6965 An lvalue expression of type T1 can be cast to the type
6966 "reference to T2" if an expression of type "pointer to T1" can be
6967 explicitly converted to the type "pointer to T2" using a
6968 reinterpret_cast. */
6969 if (TREE_CODE (type) == REFERENCE_TYPE)
6971 if (! real_lvalue_p (expr))
6973 if (complain & tf_error)
6974 error ("invalid cast of an rvalue expression of type "
6977 return error_mark_node;
6980 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
6981 "B" are related class types; the reinterpret_cast does not
6982 adjust the pointer. */
6983 if (TYPE_PTR_P (intype)
6984 && (complain & tf_warning)
6985 && (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
6986 COMPARE_BASE | COMPARE_DERIVED)))
6987 warning (0, "casting %qT to %qT does not dereference pointer",
6990 expr = cp_build_addr_expr (expr, complain);
6992 if (warn_strict_aliasing > 2)
6993 strict_aliasing_warning (TREE_TYPE (expr), type, expr);
6995 if (expr != error_mark_node)
6996 expr = build_reinterpret_cast_1
6997 (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
6999 if (expr != error_mark_node)
7000 /* cp_build_indirect_ref isn't right for rvalue refs. */
7001 expr = convert_from_reference (fold_convert (type, expr));
7005 /* As a G++ extension, we consider conversions from member
7006 functions, and pointers to member functions to
7007 pointer-to-function and pointer-to-void types. If
7008 -Wno-pmf-conversions has not been specified,
7009 convert_member_func_to_ptr will issue an error message. */
7010 if ((TYPE_PTRMEMFUNC_P (intype)
7011 || TREE_CODE (intype) == METHOD_TYPE)
7012 && TYPE_PTR_P (type)
7013 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
7014 || VOID_TYPE_P (TREE_TYPE (type))))
7015 return convert_member_func_to_ptr (type, expr, complain);
7017 /* If the cast is not to a reference type, the lvalue-to-rvalue,
7018 array-to-pointer, and function-to-pointer conversions are
7020 expr = decay_conversion (expr, complain);
7022 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7023 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7024 if (TREE_CODE (expr) == NOP_EXPR
7025 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7026 expr = TREE_OPERAND (expr, 0);
7028 if (error_operand_p (expr))
7029 return error_mark_node;
7031 intype = TREE_TYPE (expr);
7033 /* [expr.reinterpret.cast]
7034 A pointer can be converted to any integral type large enough to
7035 hold it. ... A value of type std::nullptr_t can be converted to
7036 an integral type; the conversion has the same meaning and
7037 validity as a conversion of (void*)0 to the integral type. */
7038 if (CP_INTEGRAL_TYPE_P (type)
7039 && (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
7041 if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
7043 if (complain & tf_error)
7044 permerror (input_location, "cast from %qT to %qT loses precision",
7047 return error_mark_node;
7049 if (NULLPTR_TYPE_P (intype))
7050 return build_int_cst (type, 0);
7052 /* [expr.reinterpret.cast]
7053 A value of integral or enumeration type can be explicitly
7054 converted to a pointer. */
7055 else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
7058 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7059 || TYPE_PTR_OR_PTRMEM_P (type))
7060 && same_type_p (type, intype))
7062 return rvalue (expr);
7063 else if ((TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
7064 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
7065 return build_nop (type, expr);
7066 else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
7067 || (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
7072 && check_for_casting_away_constness (intype, type,
7073 REINTERPRET_CAST_EXPR,
7075 return error_mark_node;
7076 /* Warn about possible alignment problems. */
7077 if (STRICT_ALIGNMENT && warn_cast_align
7078 && (complain & tf_warning)
7079 && !VOID_TYPE_P (type)
7080 && TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
7081 && COMPLETE_TYPE_P (TREE_TYPE (type))
7082 && COMPLETE_TYPE_P (TREE_TYPE (intype))
7083 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (intype)))
7084 warning (OPT_Wcast_align, "cast from %qT to %qT "
7085 "increases required alignment of target type", intype, type);
7087 /* We need to strip nops here, because the front end likes to
7088 create (int *)&a for array-to-pointer decay, instead of &a[0]. */
7090 if (warn_strict_aliasing <= 2)
7091 strict_aliasing_warning (intype, type, sexpr);
7093 return build_nop (type, expr);
7095 else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
7096 || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
7098 if (complain & tf_warning)
7099 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
7100 object pointer type or vice versa is conditionally-supported." */
7101 warning (OPT_Wconditionally_supported,
7102 "casting between pointer-to-function and pointer-to-object "
7103 "is conditionally-supported");
7104 return build_nop (type, expr);
7106 else if (VECTOR_TYPE_P (type))
7107 return convert_to_vector (type, expr);
7108 else if (VECTOR_TYPE_P (intype)
7109 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
7110 return convert_to_integer_nofold (type, expr);
7115 if (complain & tf_error)
7116 error ("invalid cast from type %qT to type %qT", intype, type);
7117 return error_mark_node;
7120 return cp_convert (type, expr, complain);
7124 build_reinterpret_cast (tree type, tree expr, tsubst_flags_t complain)
7128 if (type == error_mark_node || expr == error_mark_node)
7129 return error_mark_node;
7131 if (processing_template_decl)
7133 tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
7135 if (!TREE_SIDE_EFFECTS (t)
7136 && type_dependent_expression_p (expr))
7137 /* There might turn out to be side effects inside expr. */
7138 TREE_SIDE_EFFECTS (t) = 1;
7139 return convert_from_reference (t);
7142 r = build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
7143 /*valid_p=*/NULL, complain);
7144 if (r != error_mark_node)
7145 maybe_warn_about_useless_cast (type, expr, complain);
7149 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
7150 return an appropriate expression. Otherwise, return
7151 error_mark_node. If the cast is not valid, and COMPLAIN is true,
7152 then a diagnostic will be issued. If VALID_P is non-NULL, we are
7153 performing a C-style cast, its value upon return will indicate
7154 whether or not the conversion succeeded. */
7157 build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain,
7161 tree reference_type;
7163 /* Callers are responsible for handling error_mark_node as a
7164 destination type. */
7165 gcc_assert (dst_type != error_mark_node);
7166 /* In a template, callers should be building syntactic
7167 representations of casts, not using this machinery. */
7168 gcc_assert (!processing_template_decl);
7170 /* Assume the conversion is invalid. */
7174 if (!POINTER_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
7176 if (complain & tf_error)
7177 error ("invalid use of const_cast with type %qT, "
7178 "which is not a pointer, "
7179 "reference, nor a pointer-to-data-member type", dst_type);
7180 return error_mark_node;
7183 if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
7185 if (complain & tf_error)
7186 error ("invalid use of const_cast with type %qT, which is a pointer "
7187 "or reference to a function type", dst_type);
7188 return error_mark_node;
7191 /* Save casted types in the function's used types hash table. */
7192 used_types_insert (dst_type);
7194 src_type = TREE_TYPE (expr);
7195 /* Expressions do not really have reference types. */
7196 if (TREE_CODE (src_type) == REFERENCE_TYPE)
7197 src_type = TREE_TYPE (src_type);
7199 /* [expr.const.cast]
7201 For two object types T1 and T2, if a pointer to T1 can be explicitly
7202 converted to the type "pointer to T2" using a const_cast, then the
7203 following conversions can also be made:
7205 -- an lvalue of type T1 can be explicitly converted to an lvalue of
7206 type T2 using the cast const_cast<T2&>;
7208 -- a glvalue of type T1 can be explicitly converted to an xvalue of
7209 type T2 using the cast const_cast<T2&&>; and
7211 -- if T1 is a class type, a prvalue of type T1 can be explicitly
7212 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
7214 if (TREE_CODE (dst_type) == REFERENCE_TYPE)
7216 reference_type = dst_type;
7217 if (!TYPE_REF_IS_RVALUE (dst_type)
7218 ? real_lvalue_p (expr)
7219 : (CLASS_TYPE_P (TREE_TYPE (dst_type))
7221 : lvalue_or_rvalue_with_address_p (expr)))
7225 if (complain & tf_error)
7226 error ("invalid const_cast of an rvalue of type %qT to type %qT",
7227 src_type, dst_type);
7228 return error_mark_node;
7230 dst_type = build_pointer_type (TREE_TYPE (dst_type));
7231 src_type = build_pointer_type (src_type);
7235 reference_type = NULL_TREE;
7236 /* If the destination type is not a reference type, the
7237 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
7238 conversions are performed. */
7239 src_type = type_decays_to (src_type);
7240 if (src_type == error_mark_node)
7241 return error_mark_node;
7244 if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
7246 if (comp_ptr_ttypes_const (dst_type, src_type))
7251 /* This cast is actually a C-style cast. Issue a warning if
7252 the user is making a potentially unsafe cast. */
7253 check_for_casting_away_constness (src_type, dst_type,
7254 CAST_EXPR, complain);
7258 expr = cp_build_addr_expr (expr, complain);
7259 if (expr == error_mark_node)
7260 return error_mark_node;
7261 expr = build_nop (reference_type, expr);
7262 return convert_from_reference (expr);
7266 expr = decay_conversion (expr, complain);
7267 if (expr == error_mark_node)
7268 return error_mark_node;
7270 /* build_c_cast puts on a NOP_EXPR to make the result not an
7271 lvalue. Strip such NOP_EXPRs if VALUE is being used in
7272 non-lvalue context. */
7273 if (TREE_CODE (expr) == NOP_EXPR
7274 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
7275 expr = TREE_OPERAND (expr, 0);
7276 return build_nop (dst_type, expr);
7280 && !at_least_as_qualified_p (TREE_TYPE (dst_type),
7281 TREE_TYPE (src_type)))
7282 check_for_casting_away_constness (src_type, dst_type, CAST_EXPR,
7286 if (complain & tf_error)
7287 error ("invalid const_cast from type %qT to type %qT",
7288 src_type, dst_type);
7289 return error_mark_node;
7293 build_const_cast (tree type, tree expr, tsubst_flags_t complain)
7297 if (type == error_mark_node || error_operand_p (expr))
7298 return error_mark_node;
7300 if (processing_template_decl)
7302 tree t = build_min (CONST_CAST_EXPR, type, expr);
7304 if (!TREE_SIDE_EFFECTS (t)
7305 && type_dependent_expression_p (expr))
7306 /* There might turn out to be side effects inside expr. */
7307 TREE_SIDE_EFFECTS (t) = 1;
7308 return convert_from_reference (t);
7311 r = build_const_cast_1 (type, expr, complain, /*valid_p=*/NULL);
7312 if (r != error_mark_node)
7313 maybe_warn_about_useless_cast (type, expr, complain);
7317 /* Like cp_build_c_cast, but for the c-common bits. */
7320 build_c_cast (location_t /*loc*/, tree type, tree expr)
7322 return cp_build_c_cast (type, expr, tf_warning_or_error);
7325 /* Like the "build_c_cast" used for c-common, but using cp_expr to
7326 preserve location information even for tree nodes that don't
7330 build_c_cast (location_t loc, tree type, cp_expr expr)
7332 cp_expr result = cp_build_c_cast (type, expr, tf_warning_or_error);
7333 result.set_location (loc);
7337 /* Build an expression representing an explicit C-style cast to type
7338 TYPE of expression EXPR. */
7341 cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain)
7347 if (type == error_mark_node || error_operand_p (expr))
7348 return error_mark_node;
7350 if (processing_template_decl)
7352 tree t = build_min (CAST_EXPR, type,
7353 tree_cons (NULL_TREE, value, NULL_TREE));
7354 /* We don't know if it will or will not have side effects. */
7355 TREE_SIDE_EFFECTS (t) = 1;
7356 return convert_from_reference (t);
7359 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
7360 'Class') should always be retained, because this information aids
7361 in method lookup. */
7362 if (objc_is_object_ptr (type)
7363 && objc_is_object_ptr (TREE_TYPE (expr)))
7364 return build_nop (type, expr);
7366 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
7367 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
7368 if (TREE_CODE (type) != REFERENCE_TYPE
7369 && TREE_CODE (value) == NOP_EXPR
7370 && TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
7371 value = TREE_OPERAND (value, 0);
7373 if (TREE_CODE (type) == ARRAY_TYPE)
7375 /* Allow casting from T1* to T2[] because Cfront allows it.
7376 NIHCL uses it. It is not valid ISO C++ however. */
7377 if (TYPE_PTR_P (TREE_TYPE (expr)))
7379 if (complain & tf_error)
7380 permerror (input_location, "ISO C++ forbids casting to an array type %qT", type);
7382 return error_mark_node;
7383 type = build_pointer_type (TREE_TYPE (type));
7387 if (complain & tf_error)
7388 error ("ISO C++ forbids casting to an array type %qT", type);
7389 return error_mark_node;
7393 if (TREE_CODE (type) == FUNCTION_TYPE
7394 || TREE_CODE (type) == METHOD_TYPE)
7396 if (complain & tf_error)
7397 error ("invalid cast to function type %qT", type);
7398 return error_mark_node;
7401 if (TYPE_PTR_P (type)
7402 && TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
7403 /* Casting to an integer of smaller size is an error detected elsewhere. */
7404 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
7405 /* Don't warn about converting any constant. */
7406 && !TREE_CONSTANT (value))
7407 warning_at (input_location, OPT_Wint_to_pointer_cast,
7408 "cast to pointer from integer of different size");
7410 /* A C-style cast can be a const_cast. */
7411 result = build_const_cast_1 (type, value, complain & tf_warning,
7415 if (result != error_mark_node)
7416 maybe_warn_about_useless_cast (type, value, complain);
7420 /* Or a static cast. */
7421 result = build_static_cast_1 (type, value, /*c_cast_p=*/true,
7422 &valid_p, complain);
7423 /* Or a reinterpret_cast. */
7425 result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
7426 &valid_p, complain);
7427 /* The static_cast or reinterpret_cast may be followed by a
7430 /* A valid cast may result in errors if, for example, a
7431 conversion to an ambiguous base class is required. */
7432 && !error_operand_p (result))
7436 maybe_warn_about_useless_cast (type, value, complain);
7438 /* Non-class rvalues always have cv-unqualified type. */
7439 if (!CLASS_TYPE_P (type))
7440 type = TYPE_MAIN_VARIANT (type);
7441 result_type = TREE_TYPE (result);
7442 if (!CLASS_TYPE_P (result_type) && TREE_CODE (type) != REFERENCE_TYPE)
7443 result_type = TYPE_MAIN_VARIANT (result_type);
7444 /* If the type of RESULT does not match TYPE, perform a
7445 const_cast to make it match. If the static_cast or
7446 reinterpret_cast succeeded, we will differ by at most
7447 cv-qualification, so the follow-on const_cast is guaranteed
7449 if (!same_type_p (non_reference (type), non_reference (result_type)))
7451 result = build_const_cast_1 (type, result, false, &valid_p);
7452 gcc_assert (valid_p);
7457 return error_mark_node;
7460 /* For use from the C common bits. */
7462 build_modify_expr (location_t /*location*/,
7463 tree lhs, tree /*lhs_origtype*/,
7464 enum tree_code modifycode,
7465 location_t /*rhs_location*/, tree rhs,
7466 tree /*rhs_origtype*/)
7468 return cp_build_modify_expr (lhs, modifycode, rhs, tf_warning_or_error);
7471 /* Build an assignment expression of lvalue LHS from value RHS.
7472 MODIFYCODE is the code for a binary operator that we use
7473 to combine the old value of LHS with RHS to get the new value.
7474 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
7476 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
7479 cp_build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs,
7480 tsubst_flags_t complain)
7484 tree lhstype = TREE_TYPE (lhs);
7485 tree olhstype = lhstype;
7486 bool plain_assign = (modifycode == NOP_EXPR);
7488 /* Avoid duplicate error messages from operands that had errors. */
7489 if (error_operand_p (lhs) || error_operand_p (rhs))
7490 return error_mark_node;
7492 /* Handle control structure constructs used as "lvalues". */
7493 switch (TREE_CODE (lhs))
7495 /* Handle --foo = 5; as these are valid constructs in C++. */
7496 case PREDECREMENT_EXPR:
7497 case PREINCREMENT_EXPR:
7498 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7499 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7500 stabilize_reference (TREE_OPERAND (lhs, 0)),
7501 TREE_OPERAND (lhs, 1));
7502 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0),
7503 modifycode, rhs, complain);
7504 if (newrhs == error_mark_node)
7505 return error_mark_node;
7506 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
7508 /* Handle (a, b) used as an "lvalue". */
7510 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 1),
7511 modifycode, rhs, complain);
7512 if (newrhs == error_mark_node)
7513 return error_mark_node;
7514 return build2 (COMPOUND_EXPR, lhstype,
7515 TREE_OPERAND (lhs, 0), newrhs);
7518 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
7519 lhs = build2 (TREE_CODE (lhs), TREE_TYPE (lhs),
7520 stabilize_reference (TREE_OPERAND (lhs, 0)),
7521 TREE_OPERAND (lhs, 1));
7522 newrhs = cp_build_modify_expr (TREE_OPERAND (lhs, 0), modifycode, rhs,
7524 if (newrhs == error_mark_node)
7525 return error_mark_node;
7526 return build2 (COMPOUND_EXPR, lhstype, lhs, newrhs);
7530 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
7531 when neither operand has side-effects. */
7532 if (!lvalue_or_else (lhs, lv_assign, complain))
7533 return error_mark_node;
7535 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
7536 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
7538 lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
7539 build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
7541 TREE_OPERAND (lhs, 0),
7542 TREE_OPERAND (lhs, 1)),
7543 TREE_OPERAND (lhs, 0),
7544 TREE_OPERAND (lhs, 1));
7547 /* Handle (a ? b : c) used as an "lvalue". */
7550 /* Produce (a ? (b = rhs) : (c = rhs))
7551 except that the RHS goes through a save-expr
7552 so the code to compute it is only emitted once. */
7554 tree preeval = NULL_TREE;
7556 if (VOID_TYPE_P (TREE_TYPE (rhs)))
7558 if (complain & tf_error)
7559 error ("void value not ignored as it ought to be");
7560 return error_mark_node;
7563 rhs = stabilize_expr (rhs, &preeval);
7565 /* Check this here to avoid odd errors when trying to convert
7566 a throw to the type of the COND_EXPR. */
7567 if (!lvalue_or_else (lhs, lv_assign, complain))
7568 return error_mark_node;
7570 cond = build_conditional_expr
7571 (input_location, TREE_OPERAND (lhs, 0),
7572 cp_build_modify_expr (TREE_OPERAND (lhs, 1),
7573 modifycode, rhs, complain),
7574 cp_build_modify_expr (TREE_OPERAND (lhs, 2),
7575 modifycode, rhs, complain),
7578 if (cond == error_mark_node)
7580 /* Make sure the code to compute the rhs comes out
7581 before the split. */
7583 cond = build2 (COMPOUND_EXPR, TREE_TYPE (lhs), preeval, cond);
7591 if (modifycode == INIT_EXPR)
7593 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7594 /* Do the default thing. */;
7595 else if (TREE_CODE (rhs) == CONSTRUCTOR)
7597 /* Compound literal. */
7598 if (! same_type_p (TREE_TYPE (rhs), lhstype))
7599 /* Call convert to generate an error; see PR 11063. */
7600 rhs = convert (lhstype, rhs);
7601 result = build2 (INIT_EXPR, lhstype, lhs, rhs);
7602 TREE_SIDE_EFFECTS (result) = 1;
7605 else if (! MAYBE_CLASS_TYPE_P (lhstype))
7606 /* Do the default thing. */;
7609 vec<tree, va_gc> *rhs_vec = make_tree_vector_single (rhs);
7610 result = build_special_member_call (lhs, complete_ctor_identifier,
7611 &rhs_vec, lhstype, LOOKUP_NORMAL,
7613 release_tree_vector (rhs_vec);
7614 if (result == NULL_TREE)
7615 return error_mark_node;
7621 lhs = require_complete_type_sfinae (lhs, complain);
7622 if (lhs == error_mark_node)
7623 return error_mark_node;
7625 if (modifycode == NOP_EXPR)
7627 if (c_dialect_objc ())
7629 result = objc_maybe_build_modify_expr (lhs, rhs);
7634 /* `operator=' is not an inheritable operator. */
7635 if (! MAYBE_CLASS_TYPE_P (lhstype))
7636 /* Do the default thing. */;
7639 result = build_new_op (input_location, MODIFY_EXPR,
7640 LOOKUP_NORMAL, lhs, rhs,
7641 make_node (NOP_EXPR), /*overload=*/NULL,
7643 if (result == NULL_TREE)
7644 return error_mark_node;
7651 tree init = NULL_TREE;
7653 /* A binary op has been requested. Combine the old LHS
7654 value with the RHS producing the value we should actually
7655 store into the LHS. */
7656 gcc_assert (!((TREE_CODE (lhstype) == REFERENCE_TYPE
7657 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
7658 || MAYBE_CLASS_TYPE_P (lhstype)));
7660 /* Preevaluate the RHS to make sure its evaluation is complete
7661 before the lvalue-to-rvalue conversion of the LHS:
7663 [expr.ass] With respect to an indeterminately-sequenced
7664 function call, the operation of a compound assignment is a
7665 single evaluation. [ Note: Therefore, a function call shall
7666 not intervene between the lvalue-to-rvalue conversion and the
7667 side effect associated with any single compound assignment
7668 operator. -- end note ] */
7669 lhs = stabilize_reference (lhs);
7671 rhs = stabilize_expr (rhs, &init);
7672 newrhs = cp_build_binary_op (input_location,
7673 modifycode, lhs, rhs,
7675 if (newrhs == error_mark_node)
7677 if (complain & tf_error)
7678 error (" in evaluation of %<%Q(%#T, %#T)%>", modifycode,
7679 TREE_TYPE (lhs), TREE_TYPE (rhs));
7680 return error_mark_node;
7684 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
7686 /* Now it looks like a plain assignment. */
7687 modifycode = NOP_EXPR;
7688 if (c_dialect_objc ())
7690 result = objc_maybe_build_modify_expr (lhs, newrhs);
7695 gcc_assert (TREE_CODE (lhstype) != REFERENCE_TYPE);
7696 gcc_assert (TREE_CODE (TREE_TYPE (newrhs)) != REFERENCE_TYPE);
7699 /* The left-hand side must be an lvalue. */
7700 if (!lvalue_or_else (lhs, lv_assign, complain))
7701 return error_mark_node;
7703 /* Warn about modifying something that is `const'. Don't warn if
7704 this is initialization. */
7705 if (modifycode != INIT_EXPR
7706 && (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
7707 /* Functions are not modifiable, even though they are
7709 || TREE_CODE (TREE_TYPE (lhs)) == FUNCTION_TYPE
7710 || TREE_CODE (TREE_TYPE (lhs)) == METHOD_TYPE
7711 /* If it's an aggregate and any field is const, then it is
7712 effectively const. */
7713 || (CLASS_TYPE_P (lhstype)
7714 && C_TYPE_FIELDS_READONLY (lhstype))))
7716 if (complain & tf_error)
7717 cxx_readonly_error (lhs, lv_assign);
7719 return error_mark_node;
7722 /* If storing into a structure or union member, it may have been given a
7723 lowered bitfield type. We need to convert to the declared type first,
7724 so retrieve it now. */
7726 olhstype = unlowered_expr_type (lhs);
7728 /* Convert new value to destination type. */
7730 if (TREE_CODE (lhstype) == ARRAY_TYPE)
7734 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
7736 if (modifycode != INIT_EXPR)
7738 if (complain & tf_error)
7739 error ("assigning to an array from an initializer list");
7740 return error_mark_node;
7742 if (check_array_initializer (lhs, lhstype, newrhs))
7743 return error_mark_node;
7744 newrhs = digest_init (lhstype, newrhs, complain);
7745 if (newrhs == error_mark_node)
7746 return error_mark_node;
7749 /* C++11 8.5/17: "If the destination type is an array of characters,
7750 an array of char16_t, an array of char32_t, or an array of wchar_t,
7751 and the initializer is a string literal...". */
7752 else if (TREE_CODE (newrhs) == STRING_CST
7753 && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype)))
7754 && modifycode == INIT_EXPR)
7756 newrhs = digest_init (lhstype, newrhs, complain);
7757 if (newrhs == error_mark_node)
7758 return error_mark_node;
7761 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
7762 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
7764 if (complain & tf_error)
7765 error ("incompatible types in assignment of %qT to %qT",
7766 TREE_TYPE (rhs), lhstype);
7767 return error_mark_node;
7770 /* Allow array assignment in compiler-generated code. */
7771 else if (!current_function_decl
7772 || !DECL_DEFAULTED_FN (current_function_decl))
7774 /* This routine is used for both initialization and assignment.
7775 Make sure the diagnostic message differentiates the context. */
7776 if (complain & tf_error)
7778 if (modifycode == INIT_EXPR)
7779 error ("array used as initializer");
7781 error ("invalid array assignment");
7783 return error_mark_node;
7786 from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
7787 ? 1 + (modifycode != INIT_EXPR): 0;
7788 return build_vec_init (lhs, NULL_TREE, newrhs,
7789 /*explicit_value_init_p=*/false,
7790 from_array, complain);
7793 if (modifycode == INIT_EXPR)
7794 /* Calls with INIT_EXPR are all direct-initialization, so don't set
7795 LOOKUP_ONLYCONVERTING. */
7796 newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
7797 ICR_INIT, NULL_TREE, 0,
7800 newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
7801 NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
7803 if (!same_type_p (lhstype, olhstype))
7804 newrhs = cp_convert_and_check (lhstype, newrhs, complain);
7806 if (modifycode != INIT_EXPR)
7808 if (TREE_CODE (newrhs) == CALL_EXPR
7809 && TYPE_NEEDS_CONSTRUCTING (lhstype))
7810 newrhs = build_cplus_new (lhstype, newrhs, complain);
7812 /* Can't initialize directly from a TARGET_EXPR, since that would
7813 cause the lhs to be constructed twice, and possibly result in
7814 accidental self-initialization. So we force the TARGET_EXPR to be
7815 expanded without a target. */
7816 if (TREE_CODE (newrhs) == TARGET_EXPR)
7817 newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
7818 TREE_OPERAND (newrhs, 0));
7821 if (newrhs == error_mark_node)
7822 return error_mark_node;
7824 if (c_dialect_objc () && flag_objc_gc)
7826 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
7832 result = build2 (modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
7833 lhstype, lhs, newrhs);
7835 TREE_SIDE_EFFECTS (result) = 1;
7837 TREE_NO_WARNING (result) = 1;
7843 build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
7844 tree rhs, tsubst_flags_t complain)
7846 tree orig_lhs = lhs;
7847 tree orig_rhs = rhs;
7848 tree overload = NULL_TREE;
7849 tree op = build_nt (modifycode, NULL_TREE, NULL_TREE);
7851 if (processing_template_decl)
7853 if (modifycode == NOP_EXPR
7854 || type_dependent_expression_p (lhs)
7855 || type_dependent_expression_p (rhs))
7856 return build_min_nt_loc (loc, MODOP_EXPR, lhs,
7857 build_min_nt_loc (loc, modifycode, NULL_TREE,
7860 lhs = build_non_dependent_expr (lhs);
7861 rhs = build_non_dependent_expr (rhs);
7864 if (modifycode != NOP_EXPR)
7866 tree rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL,
7867 lhs, rhs, op, &overload, complain);
7870 if (rval == error_mark_node)
7872 TREE_NO_WARNING (rval) = 1;
7873 if (processing_template_decl)
7875 if (overload != NULL_TREE)
7876 return (build_min_non_dep_op_overload
7877 (MODIFY_EXPR, rval, overload, orig_lhs, orig_rhs));
7879 return (build_min_non_dep
7880 (MODOP_EXPR, rval, orig_lhs, op, orig_rhs));
7885 return cp_build_modify_expr (lhs, modifycode, rhs, complain);
7888 /* Helper function for get_delta_difference which assumes FROM is a base
7889 class of TO. Returns a delta for the conversion of pointer-to-member
7890 of FROM to pointer-to-member of TO. If the conversion is invalid and
7891 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
7892 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
7893 If C_CAST_P is true, this conversion is taking place as part of a
7897 get_delta_difference_1 (tree from, tree to, bool c_cast_p,
7898 tsubst_flags_t complain)
7903 binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
7906 if (binfo == error_mark_node)
7908 if (!(complain & tf_error))
7909 return error_mark_node;
7911 error (" in pointer to member function conversion");
7912 return size_zero_node;
7916 if (kind != bk_via_virtual)
7917 return BINFO_OFFSET (binfo);
7919 /* FROM is a virtual base class of TO. Issue an error or warning
7920 depending on whether or not this is a reinterpret cast. */
7922 if (!(complain & tf_error))
7923 return error_mark_node;
7925 error ("pointer to member conversion via virtual base %qT",
7926 BINFO_TYPE (binfo_from_vbase (binfo)));
7928 return size_zero_node;
7935 /* Get difference in deltas for different pointer to member function
7936 types. If the conversion is invalid and tf_error is not set in
7937 COMPLAIN, returns error_mark_node, otherwise returns an integer
7938 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
7939 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
7940 conversions as well. If C_CAST_P is true this conversion is taking
7941 place as part of a C-style cast.
7943 Note that the naming of FROM and TO is kind of backwards; the return
7944 value is what we add to a TO in order to get a FROM. They are named
7945 this way because we call this function to find out how to convert from
7946 a pointer to member of FROM to a pointer to member of TO. */
7949 get_delta_difference (tree from, tree to,
7950 bool allow_inverse_p,
7951 bool c_cast_p, tsubst_flags_t complain)
7955 if (same_type_ignoring_top_level_qualifiers_p (from, to))
7956 /* Pointer to member of incomplete class is permitted*/
7957 result = size_zero_node;
7959 result = get_delta_difference_1 (from, to, c_cast_p, complain);
7961 if (result == error_mark_node)
7962 return error_mark_node;
7966 if (!allow_inverse_p)
7968 if (!(complain & tf_error))
7969 return error_mark_node;
7971 error_not_base_type (from, to);
7972 error (" in pointer to member conversion");
7973 result = size_zero_node;
7977 result = get_delta_difference_1 (to, from, c_cast_p, complain);
7979 if (result == error_mark_node)
7980 return error_mark_node;
7983 result = size_diffop_loc (input_location,
7984 size_zero_node, result);
7987 if (!(complain & tf_error))
7988 return error_mark_node;
7990 error_not_base_type (from, to);
7991 error (" in pointer to member conversion");
7992 result = size_zero_node;
7997 return convert_to_integer (ptrdiff_type_node, result);
8000 /* Return a constructor for the pointer-to-member-function TYPE using
8001 the other components as specified. */
8004 build_ptrmemfunc1 (tree type, tree delta, tree pfn)
8009 vec<constructor_elt, va_gc> *v;
8011 /* Pull the FIELD_DECLs out of the type. */
8012 pfn_field = TYPE_FIELDS (type);
8013 delta_field = DECL_CHAIN (pfn_field);
8015 /* Make sure DELTA has the type we want. */
8016 delta = convert_and_check (input_location, delta_type_node, delta);
8018 /* Convert to the correct target type if necessary. */
8019 pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
8021 /* Finish creating the initializer. */
8023 CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
8024 CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
8025 u = build_constructor (type, v);
8026 TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
8027 TREE_STATIC (u) = (TREE_CONSTANT (u)
8028 && (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
8030 && (initializer_constant_valid_p (delta, TREE_TYPE (delta))
8035 /* Build a constructor for a pointer to member function. It can be
8036 used to initialize global variables, local variable, or used
8037 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
8040 If FORCE is nonzero, then force this conversion, even if
8041 we would rather not do it. Usually set when using an explicit
8042 cast. A C-style cast is being processed iff C_CAST_P is true.
8044 Return error_mark_node, if something goes wrong. */
8047 build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
8048 tsubst_flags_t complain)
8054 if (error_operand_p (pfn))
8055 return error_mark_node;
8057 pfn_type = TREE_TYPE (pfn);
8058 to_type = build_ptrmemfunc_type (type);
8060 /* Handle multiple conversions of pointer to member functions. */
8061 if (TYPE_PTRMEMFUNC_P (pfn_type))
8063 tree delta = NULL_TREE;
8064 tree npfn = NULL_TREE;
8068 && !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
8069 LOOKUP_NORMAL, complain))
8071 if (complain & tf_error)
8072 error ("invalid conversion to type %qT from type %qT",
8075 return error_mark_node;
8078 n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
8079 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
8081 c_cast_p, complain);
8082 if (n == error_mark_node)
8083 return error_mark_node;
8085 /* We don't have to do any conversion to convert a
8086 pointer-to-member to its own type. But, we don't want to
8087 just return a PTRMEM_CST if there's an explicit cast; that
8088 cast should make the expression an invalid template argument. */
8089 if (TREE_CODE (pfn) != PTRMEM_CST)
8091 if (same_type_p (to_type, pfn_type))
8093 else if (integer_zerop (n))
8094 return build_reinterpret_cast (to_type, pfn,
8098 if (TREE_SIDE_EFFECTS (pfn))
8099 pfn = save_expr (pfn);
8101 /* Obtain the function pointer and the current DELTA. */
8102 if (TREE_CODE (pfn) == PTRMEM_CST)
8103 expand_ptrmemfunc_cst (pfn, &delta, &npfn);
8106 npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
8107 delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
8110 /* Just adjust the DELTA field. */
8111 gcc_assert (same_type_ignoring_top_level_qualifiers_p
8112 (TREE_TYPE (delta), ptrdiff_type_node));
8113 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
8114 n = cp_build_binary_op (input_location,
8115 LSHIFT_EXPR, n, integer_one_node,
8117 delta = cp_build_binary_op (input_location,
8118 PLUS_EXPR, delta, n, complain);
8119 return build_ptrmemfunc1 (to_type, delta, npfn);
8122 /* Handle null pointer to member function conversions. */
8123 if (null_ptr_cst_p (pfn))
8125 pfn = cp_build_c_cast (type, pfn, complain);
8126 return build_ptrmemfunc1 (to_type,
8131 if (type_unknown_p (pfn))
8132 return instantiate_type (type, pfn, complain);
8134 fn = TREE_OPERAND (pfn, 0);
8135 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
8136 /* In a template, we will have preserved the
8138 || (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
8139 return make_ptrmem_cst (to_type, fn);
8142 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
8145 ??? There is no consistency as to the types returned for the above
8146 values. Some code acts as if it were a sizetype and some as if it were
8147 integer_type_node. */
8150 expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
8152 tree type = TREE_TYPE (cst);
8153 tree fn = PTRMEM_CST_MEMBER (cst);
8154 tree ptr_class, fn_class;
8156 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
8158 /* The class that the function belongs to. */
8159 fn_class = DECL_CONTEXT (fn);
8161 /* The class that we're creating a pointer to member of. */
8162 ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
8164 /* First, calculate the adjustment to the function's class. */
8165 *delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
8166 /*c_cast_p=*/0, tf_warning_or_error);
8168 if (!DECL_VIRTUAL_P (fn))
8169 *pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type),
8170 build_addr_func (fn, tf_warning_or_error));
8173 /* If we're dealing with a virtual function, we have to adjust 'this'
8174 again, to point to the base which provides the vtable entry for
8175 fn; the call will do the opposite adjustment. */
8176 tree orig_class = DECL_CONTEXT (fn);
8177 tree binfo = binfo_or_else (orig_class, fn_class);
8178 *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
8179 *delta, BINFO_OFFSET (binfo));
8181 /* We set PFN to the vtable offset at which the function can be
8182 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
8183 case delta is shifted left, and then incremented). */
8184 *pfn = DECL_VINDEX (fn);
8185 *pfn = fold_build2 (MULT_EXPR, integer_type_node, *pfn,
8186 TYPE_SIZE_UNIT (vtable_entry_type));
8188 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
8190 case ptrmemfunc_vbit_in_pfn:
8191 *pfn = fold_build2 (PLUS_EXPR, integer_type_node, *pfn,
8195 case ptrmemfunc_vbit_in_delta:
8196 *delta = fold_build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
8197 *delta, integer_one_node);
8198 *delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
8199 *delta, integer_one_node);
8206 *pfn = fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
8210 /* Return an expression for PFN from the pointer-to-member function
8214 pfn_from_ptrmemfunc (tree t)
8216 if (TREE_CODE (t) == PTRMEM_CST)
8221 expand_ptrmemfunc_cst (t, &delta, &pfn);
8226 return build_ptrmemfunc_access_expr (t, pfn_identifier);
8229 /* Return an expression for DELTA from the pointer-to-member function
8233 delta_from_ptrmemfunc (tree t)
8235 if (TREE_CODE (t) == PTRMEM_CST)
8240 expand_ptrmemfunc_cst (t, &delta, &pfn);
8245 return build_ptrmemfunc_access_expr (t, delta_identifier);
8248 /* Convert value RHS to type TYPE as preparation for an assignment to
8249 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
8250 implicit conversion is. If FNDECL is non-NULL, we are doing the
8251 conversion in order to pass the PARMNUMth argument of FNDECL.
8252 If FNDECL is NULL, we are doing the conversion in function pointer
8253 argument passing, conversion in initialization, etc. */
8256 convert_for_assignment (tree type, tree rhs,
8257 impl_conv_rhs errtype, tree fndecl, int parmnum,
8258 tsubst_flags_t complain, int flags)
8261 enum tree_code coder;
8263 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
8264 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
8265 rhs = TREE_OPERAND (rhs, 0);
8267 rhstype = TREE_TYPE (rhs);
8268 coder = TREE_CODE (rhstype);
8270 if (VECTOR_TYPE_P (type) && coder == VECTOR_TYPE
8271 && vector_types_convertible_p (type, rhstype, true))
8273 rhs = mark_rvalue_use (rhs);
8274 return convert (type, rhs);
8277 if (rhs == error_mark_node || rhstype == error_mark_node)
8278 return error_mark_node;
8279 if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
8280 return error_mark_node;
8282 /* The RHS of an assignment cannot have void type. */
8283 if (coder == VOID_TYPE)
8285 if (complain & tf_error)
8286 error ("void value not ignored as it ought to be");
8287 return error_mark_node;
8290 if (c_dialect_objc ())
8294 tree rname = fndecl;
8305 selector = objc_message_selector ();
8307 if (selector && parmno > 1)
8314 if (objc_compare_types (type, rhstype, parmno, rname))
8316 rhs = mark_rvalue_use (rhs);
8317 return convert (type, rhs);
8323 The expression is implicitly converted (clause _conv_) to the
8324 cv-unqualified type of the left operand.
8326 We allow bad conversions here because by the time we get to this point
8327 we are committed to doing the conversion. If we end up doing a bad
8328 conversion, convert_like will complain. */
8329 if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
8331 /* When -Wno-pmf-conversions is use, we just silently allow
8332 conversions from pointers-to-members to plain pointers. If
8333 the conversion doesn't work, cp_convert will complain. */
8335 && TYPE_PTR_P (type)
8336 && TYPE_PTRMEMFUNC_P (rhstype))
8337 rhs = cp_convert (strip_top_quals (type), rhs, complain);
8340 if (complain & tf_error)
8342 /* If the right-hand side has unknown type, then it is an
8343 overloaded function. Call instantiate_type to get error
8345 if (rhstype == unknown_type_node)
8346 instantiate_type (type, rhs, tf_warning_or_error);
8348 error ("cannot convert %qT to %qT for argument %qP to %qD",
8349 rhstype, type, parmnum, fndecl);
8353 case ICR_DEFAULT_ARGUMENT:
8354 error ("cannot convert %qT to %qT in default argument",
8358 error ("cannot convert %qT to %qT in argument passing",
8361 case ICR_CONVERTING:
8362 error ("cannot convert %qT to %qT",
8366 error ("cannot convert %qT to %qT in initialization",
8370 error ("cannot convert %qT to %qT in return",
8374 error ("cannot convert %qT to %qT in assignment",
8380 if (TYPE_PTR_P (rhstype)
8381 && TYPE_PTR_P (type)
8382 && CLASS_TYPE_P (TREE_TYPE (rhstype))
8383 && CLASS_TYPE_P (TREE_TYPE (type))
8384 && !COMPLETE_TYPE_P (TREE_TYPE (rhstype)))
8385 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
8386 (TREE_TYPE (rhstype))),
8387 "class type %qT is incomplete", TREE_TYPE (rhstype));
8389 return error_mark_node;
8392 if (warn_suggest_attribute_format)
8394 const enum tree_code codel = TREE_CODE (type);
8395 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
8397 && check_missing_format_attribute (type, rhstype)
8398 && (complain & tf_warning))
8402 case ICR_DEFAULT_ARGUMENT:
8404 warning (OPT_Wsuggest_attribute_format,
8405 "parameter %qP of %qD might be a candidate "
8406 "for a format attribute", parmnum, fndecl);
8408 warning (OPT_Wsuggest_attribute_format,
8409 "parameter might be a candidate "
8410 "for a format attribute");
8412 case ICR_CONVERTING:
8413 warning (OPT_Wsuggest_attribute_format,
8414 "target of conversion might be a candidate "
8415 "for a format attribute");
8418 warning (OPT_Wsuggest_attribute_format,
8419 "target of initialization might be a candidate "
8420 "for a format attribute");
8423 warning (OPT_Wsuggest_attribute_format,
8424 "return type might be a candidate "
8425 "for a format attribute");
8428 warning (OPT_Wsuggest_attribute_format,
8429 "left-hand side of assignment might be a candidate "
8430 "for a format attribute");
8437 /* If -Wparentheses, warn about a = b = c when a has type bool and b
8439 if (warn_parentheses
8440 && TREE_CODE (type) == BOOLEAN_TYPE
8441 && TREE_CODE (rhs) == MODIFY_EXPR
8442 && !TREE_NO_WARNING (rhs)
8443 && TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE
8444 && (complain & tf_warning))
8446 location_t loc = EXPR_LOC_OR_LOC (rhs, input_location);
8448 warning_at (loc, OPT_Wparentheses,
8449 "suggest parentheses around assignment used as truth value");
8450 TREE_NO_WARNING (rhs) = 1;
8453 return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
8457 /* Convert RHS to be of type TYPE.
8458 If EXP is nonzero, it is the target of the initialization.
8459 ERRTYPE indicates what kind of error the implicit conversion is.
8461 Two major differences between the behavior of
8462 `convert_for_assignment' and `convert_for_initialization'
8463 are that references are bashed in the former, while
8464 copied in the latter, and aggregates are assigned in
8465 the former (operator=) while initialized in the
8468 If using constructor make sure no conversion operator exists, if one does
8469 exist, an ambiguity exists. */
8472 convert_for_initialization (tree exp, tree type, tree rhs, int flags,
8473 impl_conv_rhs errtype, tree fndecl, int parmnum,
8474 tsubst_flags_t complain)
8476 enum tree_code codel = TREE_CODE (type);
8478 enum tree_code coder;
8480 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8481 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
8482 if (TREE_CODE (rhs) == NOP_EXPR
8483 && TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
8484 && codel != REFERENCE_TYPE)
8485 rhs = TREE_OPERAND (rhs, 0);
8487 if (type == error_mark_node
8488 || rhs == error_mark_node
8489 || (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
8490 return error_mark_node;
8492 if (MAYBE_CLASS_TYPE_P (non_reference (type)))
8494 else if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
8495 && TREE_CODE (type) != ARRAY_TYPE
8496 && (TREE_CODE (type) != REFERENCE_TYPE
8497 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
8498 || (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
8499 && !TYPE_REFFN_P (type))
8500 || TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
8501 rhs = decay_conversion (rhs, complain);
8503 rhstype = TREE_TYPE (rhs);
8504 coder = TREE_CODE (rhstype);
8506 if (coder == ERROR_MARK)
8507 return error_mark_node;
8509 /* We accept references to incomplete types, so we can
8510 return here before checking if RHS is of complete type. */
8512 if (codel == REFERENCE_TYPE)
8514 /* This should eventually happen in convert_arguments. */
8515 int savew = 0, savee = 0;
8518 savew = warningcount + werrorcount, savee = errorcount;
8519 rhs = initialize_reference (type, rhs, flags, complain);
8522 && (warningcount + werrorcount > savew || errorcount > savee))
8523 inform (DECL_SOURCE_LOCATION (fndecl),
8524 "in passing argument %P of %qD", parmnum, fndecl);
8530 exp = require_complete_type_sfinae (exp, complain);
8531 if (exp == error_mark_node)
8532 return error_mark_node;
8534 rhstype = non_reference (rhstype);
8536 type = complete_type (type);
8538 if (DIRECT_INIT_EXPR_P (type, rhs))
8539 /* Don't try to do copy-initialization if we already have
8540 direct-initialization. */
8543 if (MAYBE_CLASS_TYPE_P (type))
8544 return perform_implicit_conversion_flags (type, rhs, complain, flags);
8546 return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
8550 /* If RETVAL is the address of, or a reference to, a local variable or
8551 temporary give an appropriate warning and return true. */
8554 maybe_warn_about_returning_address_of_local (tree retval)
8556 tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
8557 tree whats_returned = c_fully_fold (retval, /*for_init*/false,
8558 /*maybe_constp*/NULL);
8562 if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
8563 whats_returned = TREE_OPERAND (whats_returned, 1);
8564 else if (CONVERT_EXPR_P (whats_returned)
8565 || TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
8566 whats_returned = TREE_OPERAND (whats_returned, 0);
8571 if (TREE_CODE (whats_returned) != ADDR_EXPR)
8573 whats_returned = TREE_OPERAND (whats_returned, 0);
8575 while (TREE_CODE (whats_returned) == COMPONENT_REF
8576 || TREE_CODE (whats_returned) == ARRAY_REF)
8577 whats_returned = TREE_OPERAND (whats_returned, 0);
8579 if (TREE_CODE (valtype) == REFERENCE_TYPE)
8581 if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
8582 || TREE_CODE (whats_returned) == TARGET_EXPR)
8584 warning (OPT_Wreturn_local_addr, "returning reference to temporary");
8587 if (VAR_P (whats_returned)
8588 && DECL_NAME (whats_returned)
8589 && TEMP_NAME_P (DECL_NAME (whats_returned)))
8591 warning (OPT_Wreturn_local_addr, "reference to non-lvalue returned");
8596 if (DECL_P (whats_returned)
8597 && DECL_NAME (whats_returned)
8598 && DECL_FUNCTION_SCOPE_P (whats_returned)
8599 && !is_capture_proxy (whats_returned)
8600 && !(TREE_STATIC (whats_returned)
8601 || TREE_PUBLIC (whats_returned)))
8603 if (TREE_CODE (valtype) == REFERENCE_TYPE)
8604 warning_at (DECL_SOURCE_LOCATION (whats_returned),
8605 OPT_Wreturn_local_addr,
8606 "reference to local variable %qD returned",
8608 else if (TREE_CODE (whats_returned) == LABEL_DECL)
8609 warning_at (DECL_SOURCE_LOCATION (whats_returned),
8610 OPT_Wreturn_local_addr, "address of label %qD returned",
8613 warning_at (DECL_SOURCE_LOCATION (whats_returned),
8614 OPT_Wreturn_local_addr, "address of local variable %qD "
8615 "returned", whats_returned);
8622 /* Check that returning RETVAL from the current function is valid.
8623 Return an expression explicitly showing all conversions required to
8624 change RETVAL into the function return type, and to assign it to
8625 the DECL_RESULT for the function. Set *NO_WARNING to true if
8626 code reaches end of non-void function warning shouldn't be issued
8627 on this RETURN_EXPR. */
8630 check_return_expr (tree retval, bool *no_warning)
8633 /* The type actually returned by the function. */
8635 /* The type the function is declared to return, or void if
8636 the declared type is incomplete. */
8638 int fn_returns_value_p;
8639 bool named_return_value_okay_p;
8641 *no_warning = false;
8643 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
8645 error_at (EXPR_LOCATION (retval), "use of %<_Cilk_spawn%> in a return "
8646 "statement is not allowed");
8650 /* A `volatile' function is one that isn't supposed to return, ever.
8651 (This is a G++ extension, used to get better code for functions
8652 that call the `volatile' function.) */
8653 if (TREE_THIS_VOLATILE (current_function_decl))
8654 warning (0, "function declared %<noreturn%> has a %<return%> statement");
8656 /* Check for various simple errors. */
8657 if (DECL_DESTRUCTOR_P (current_function_decl))
8660 error ("returning a value from a destructor");
8663 else if (DECL_CONSTRUCTOR_P (current_function_decl))
8665 if (in_function_try_handler)
8666 /* If a return statement appears in a handler of the
8667 function-try-block of a constructor, the program is ill-formed. */
8668 error ("cannot return from a handler of a function-try-block of a constructor");
8670 /* You can't return a value from a constructor. */
8671 error ("returning a value from a constructor");
8675 const tree saved_retval = retval;
8677 if (processing_template_decl)
8679 current_function_returns_value = 1;
8681 if (check_for_bare_parameter_packs (retval))
8682 return error_mark_node;
8684 if (WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl)))
8685 || (retval != NULL_TREE
8686 && type_dependent_expression_p (retval)))
8690 functype = TREE_TYPE (TREE_TYPE (current_function_decl));
8692 /* Deduce auto return type from a return statement. */
8693 if (current_function_auto_return_pattern)
8698 if (!retval && !is_auto (current_function_auto_return_pattern))
8700 /* Give a helpful error message. */
8701 error ("return-statement with no value, in function returning %qT",
8702 current_function_auto_return_pattern);
8703 inform (input_location, "only plain %<auto%> return type can be "
8704 "deduced to %<void%>");
8705 type = error_mark_node;
8707 else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
8709 error ("returning initializer list");
8710 type = error_mark_node;
8716 auto_node = type_uses_auto (current_function_auto_return_pattern);
8717 type = do_auto_deduction (current_function_auto_return_pattern,
8721 if (type == error_mark_node)
8723 else if (functype == current_function_auto_return_pattern)
8724 apply_deduced_return_type (current_function_decl, type);
8725 else if (!same_type_p (type, functype))
8727 if (LAMBDA_FUNCTION_P (current_function_decl))
8728 error ("inconsistent types %qT and %qT deduced for "
8729 "lambda return type", functype, type);
8731 error ("inconsistent deduction for auto return type: "
8732 "%qT and then %qT", functype, type);
8737 result = DECL_RESULT (current_function_decl);
8738 valtype = TREE_TYPE (result);
8739 gcc_assert (valtype != NULL_TREE);
8740 fn_returns_value_p = !VOID_TYPE_P (valtype);
8742 /* Check for a return statement with no return value in a function
8743 that's supposed to return a value. */
8744 if (!retval && fn_returns_value_p)
8746 if (functype != error_mark_node)
8747 permerror (input_location, "return-statement with no value, in "
8748 "function returning %qT", valtype);
8749 /* Remember that this function did return. */
8750 current_function_returns_value = 1;
8751 /* And signal caller that TREE_NO_WARNING should be set on the
8752 RETURN_EXPR to avoid control reaches end of non-void function
8753 warnings in tree-cfg.c. */
8756 /* Check for a return statement with a value in a function that
8757 isn't supposed to return a value. */
8758 else if (retval && !fn_returns_value_p)
8760 if (VOID_TYPE_P (TREE_TYPE (retval)))
8761 /* You can return a `void' value from a function of `void'
8762 type. In that case, we have to evaluate the expression for
8763 its side-effects. */
8764 finish_expr_stmt (retval);
8766 permerror (input_location, "return-statement with a value, in function "
8767 "returning 'void'");
8768 current_function_returns_null = 1;
8770 /* There's really no value to return, after all. */
8774 /* Remember that this function can sometimes return without a
8776 current_function_returns_null = 1;
8778 /* Remember that this function did return a value. */
8779 current_function_returns_value = 1;
8781 /* Check for erroneous operands -- but after giving ourselves a
8782 chance to provide an error about returning a value from a void
8784 if (error_operand_p (retval))
8786 current_function_return_value = error_mark_node;
8787 return error_mark_node;
8790 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
8791 if ((DECL_OVERLOADED_OPERATOR_P (current_function_decl) == NEW_EXPR
8792 || DECL_OVERLOADED_OPERATOR_P (current_function_decl) == VEC_NEW_EXPR)
8793 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
8795 && retval && null_ptr_cst_p (retval))
8796 warning (0, "%<operator new%> must not return NULL unless it is "
8797 "declared %<throw()%> (or -fcheck-new is in effect)");
8799 /* Effective C++ rule 15. See also start_function. */
8801 && DECL_NAME (current_function_decl) == ansi_assopname(NOP_EXPR))
8805 /* The function return type must be a reference to the current
8807 if (TREE_CODE (valtype) == REFERENCE_TYPE
8808 && same_type_ignoring_top_level_qualifiers_p
8809 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
8811 /* Returning '*this' is obviously OK. */
8812 if (retval == current_class_ref)
8814 /* If we are calling a function whose return type is the same of
8815 the current class reference, it is ok. */
8816 else if (INDIRECT_REF_P (retval)
8817 && TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
8822 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
8825 if (processing_template_decl)
8827 /* We should not have changed the return value. */
8828 gcc_assert (retval == saved_retval);
8832 /* The fabled Named Return Value optimization, as per [class.copy]/15:
8834 [...] For a function with a class return type, if the expression
8835 in the return statement is the name of a local object, and the cv-
8836 unqualified type of the local object is the same as the function
8837 return type, an implementation is permitted to omit creating the tem-
8838 porary object to hold the function return value [...]
8840 So, if this is a value-returning function that always returns the same
8841 local variable, remember it.
8843 It might be nice to be more flexible, and choose the first suitable
8844 variable even if the function sometimes returns something else, but
8845 then we run the risk of clobbering the variable we chose if the other
8846 returned expression uses the chosen variable somehow. And people expect
8847 this restriction, anyway. (jason 2000-11-19)
8849 See finish_function and finalize_nrv for the rest of this optimization. */
8851 named_return_value_okay_p =
8852 (retval != NULL_TREE
8853 /* Must be a local, automatic variable. */
8855 && DECL_CONTEXT (retval) == current_function_decl
8856 && ! TREE_STATIC (retval)
8857 /* And not a lambda or anonymous union proxy. */
8858 && !DECL_HAS_VALUE_EXPR_P (retval)
8859 && (DECL_ALIGN (retval) <= DECL_ALIGN (result))
8860 /* The cv-unqualified type of the returned value must be the
8861 same as the cv-unqualified return type of the
8863 && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))),
8864 (TYPE_MAIN_VARIANT (functype)))
8865 /* And the returned value must be non-volatile. */
8866 && ! TYPE_VOLATILE (TREE_TYPE (retval)));
8868 if (fn_returns_value_p && flag_elide_constructors)
8870 if (named_return_value_okay_p
8871 && (current_function_return_value == NULL_TREE
8872 || current_function_return_value == retval))
8873 current_function_return_value = retval;
8875 current_function_return_value = error_mark_node;
8878 /* We don't need to do any conversions when there's nothing being
8883 /* Do any required conversions. */
8884 if (retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
8885 /* No conversions are required. */
8889 int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
8891 /* The functype's return type will have been set to void, if it
8892 was an incomplete type. Just treat this as 'return;' */
8893 if (VOID_TYPE_P (functype))
8894 return error_mark_node;
8896 /* If we had an id-expression obfuscated by force_paren_expr, we need
8897 to undo it so we can try to treat it as an rvalue below. */
8898 retval = maybe_undo_parenthesized_ref (retval);
8900 /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
8901 treated as an rvalue for the purposes of overload resolution to
8902 favor move constructors over copy constructors.
8904 Note that these conditions are similar to, but not as strict as,
8905 the conditions for the named return value optimization. */
8906 if ((cxx_dialect != cxx98)
8907 && ((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
8908 || TREE_CODE (retval) == PARM_DECL)
8909 && DECL_CONTEXT (retval) == current_function_decl
8910 && !TREE_STATIC (retval)
8911 /* This is only interesting for class type. */
8912 && CLASS_TYPE_P (functype))
8913 flags = flags | LOOKUP_PREFER_RVALUE;
8915 /* First convert the value to the function's return type, then
8916 to the type of return value's location to handle the
8917 case that functype is smaller than the valtype. */
8918 retval = convert_for_initialization
8919 (NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
8920 tf_warning_or_error);
8921 retval = convert (valtype, retval);
8923 /* If the conversion failed, treat this just like `return;'. */
8924 if (retval == error_mark_node)
8926 /* We can't initialize a register from a AGGR_INIT_EXPR. */
8927 else if (! cfun->returns_struct
8928 && TREE_CODE (retval) == TARGET_EXPR
8929 && TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
8930 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
8931 TREE_OPERAND (retval, 0));
8932 else if (maybe_warn_about_returning_address_of_local (retval))
8933 retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
8934 build_zero_cst (TREE_TYPE (retval)));
8937 /* Actually copy the value returned into the appropriate location. */
8938 if (retval && retval != result)
8939 retval = build2 (INIT_EXPR, TREE_TYPE (result), result, retval);
8945 /* Returns nonzero if the pointer-type FROM can be converted to the
8946 pointer-type TO via a qualification conversion. If CONSTP is -1,
8947 then we return nonzero if the pointers are similar, and the
8948 cv-qualification signature of FROM is a proper subset of that of TO.
8950 If CONSTP is positive, then all outer pointers have been
8954 comp_ptr_ttypes_real (tree to, tree from, int constp)
8956 bool to_more_cv_qualified = false;
8957 bool is_opaque_pointer = false;
8959 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
8961 if (TREE_CODE (to) != TREE_CODE (from))
8964 if (TREE_CODE (from) == OFFSET_TYPE
8965 && !same_type_p (TYPE_OFFSET_BASETYPE (from),
8966 TYPE_OFFSET_BASETYPE (to)))
8969 /* Const and volatile mean something different for function types,
8970 so the usual checks are not appropriate. */
8971 if (TREE_CODE (to) != FUNCTION_TYPE && TREE_CODE (to) != METHOD_TYPE)
8973 if (!at_least_as_qualified_p (to, from))
8976 if (!at_least_as_qualified_p (from, to))
8980 to_more_cv_qualified = true;
8984 constp &= TYPE_READONLY (to);
8987 if (VECTOR_TYPE_P (to))
8988 is_opaque_pointer = vector_targets_convertible_p (to, from);
8990 if (!TYPE_PTR_P (to) && !TYPE_PTRDATAMEM_P (to))
8991 return ((constp >= 0 || to_more_cv_qualified)
8992 && (is_opaque_pointer
8993 || same_type_ignoring_top_level_qualifiers_p (to, from)));
8997 /* When comparing, say, char ** to char const **, this function takes
8998 the 'char *' and 'char const *'. Do not pass non-pointer/reference
8999 types to this function. */
9002 comp_ptr_ttypes (tree to, tree from)
9004 return comp_ptr_ttypes_real (to, from, 1);
9007 /* Returns true iff FNTYPE is a non-class type that involves
9008 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
9009 if a parameter type is ill-formed. */
9012 error_type_p (const_tree type)
9016 switch (TREE_CODE (type))
9022 case REFERENCE_TYPE:
9024 return error_type_p (TREE_TYPE (type));
9028 if (error_type_p (TREE_TYPE (type)))
9030 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
9031 if (error_type_p (TREE_VALUE (t)))
9036 if (TYPE_PTRMEMFUNC_P (type))
9037 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
9045 /* Returns true if to and from are (possibly multi-level) pointers to the same
9046 type or inheritance-related types, regardless of cv-quals. */
9049 ptr_reasonably_similar (const_tree to, const_tree from)
9051 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
9053 /* Any target type is similar enough to void. */
9054 if (VOID_TYPE_P (to))
9055 return !error_type_p (from);
9056 if (VOID_TYPE_P (from))
9057 return !error_type_p (to);
9059 if (TREE_CODE (to) != TREE_CODE (from))
9062 if (TREE_CODE (from) == OFFSET_TYPE
9063 && comptypes (TYPE_OFFSET_BASETYPE (to),
9064 TYPE_OFFSET_BASETYPE (from),
9065 COMPARE_BASE | COMPARE_DERIVED))
9068 if (VECTOR_TYPE_P (to)
9069 && vector_types_convertible_p (to, from, false))
9072 if (TREE_CODE (to) == INTEGER_TYPE
9073 && TYPE_PRECISION (to) == TYPE_PRECISION (from))
9076 if (TREE_CODE (to) == FUNCTION_TYPE)
9077 return !error_type_p (to) && !error_type_p (from);
9079 if (!TYPE_PTR_P (to))
9081 /* When either type is incomplete avoid DERIVED_FROM_P,
9082 which may call complete_type (c++/57942). */
9083 bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
9085 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
9086 b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
9091 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
9092 pointer-to-member types) are the same, ignoring cv-qualification at
9096 comp_ptr_ttypes_const (tree to, tree from)
9098 bool is_opaque_pointer = false;
9100 for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
9102 if (TREE_CODE (to) != TREE_CODE (from))
9105 if (TREE_CODE (from) == OFFSET_TYPE
9106 && same_type_p (TYPE_OFFSET_BASETYPE (from),
9107 TYPE_OFFSET_BASETYPE (to)))
9110 if (VECTOR_TYPE_P (to))
9111 is_opaque_pointer = vector_targets_convertible_p (to, from);
9113 if (!TYPE_PTR_P (to))
9114 return (is_opaque_pointer
9115 || same_type_ignoring_top_level_qualifiers_p (to, from));
9119 /* Returns the type qualifiers for this type, including the qualifiers on the
9120 elements for an array type. */
9123 cp_type_quals (const_tree type)
9126 /* This CONST_CAST is okay because strip_array_types returns its
9127 argument unmodified and we assign it to a const_tree. */
9128 type = strip_array_types (CONST_CAST_TREE (type));
9129 if (type == error_mark_node
9130 /* Quals on a FUNCTION_TYPE are memfn quals. */
9131 || TREE_CODE (type) == FUNCTION_TYPE)
9132 return TYPE_UNQUALIFIED;
9133 quals = TYPE_QUALS (type);
9134 /* METHOD and REFERENCE_TYPEs should never have quals. */
9135 gcc_assert ((TREE_CODE (type) != METHOD_TYPE
9136 && TREE_CODE (type) != REFERENCE_TYPE)
9137 || ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
9138 == TYPE_UNQUALIFIED));
9142 /* Returns the function-ref-qualifier for TYPE */
9145 type_memfn_rqual (const_tree type)
9147 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE
9148 || TREE_CODE (type) == METHOD_TYPE);
9150 if (!FUNCTION_REF_QUALIFIED (type))
9151 return REF_QUAL_NONE;
9152 else if (FUNCTION_RVALUE_QUALIFIED (type))
9153 return REF_QUAL_RVALUE;
9155 return REF_QUAL_LVALUE;
9158 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
9162 type_memfn_quals (const_tree type)
9164 if (TREE_CODE (type) == FUNCTION_TYPE)
9165 return TYPE_QUALS (type);
9166 else if (TREE_CODE (type) == METHOD_TYPE)
9167 return cp_type_quals (class_of_this_parm (type));
9172 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
9173 MEMFN_QUALS and its ref-qualifier to RQUAL. */
9176 apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
9178 /* Could handle METHOD_TYPE here if necessary. */
9179 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
9180 if (TYPE_QUALS (type) == memfn_quals
9181 && type_memfn_rqual (type) == rqual)
9184 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
9186 tree result = build_qualified_type (type, memfn_quals);
9187 if (tree canon = TYPE_CANONICAL (result))
9188 if (canon != result)
9189 /* check_qualified_type doesn't check the ref-qualifier, so make sure
9190 TYPE_CANONICAL is correct. */
9191 TYPE_CANONICAL (result)
9192 = build_ref_qualified_type (canon, type_memfn_rqual (result));
9193 result = build_exception_variant (result, TYPE_RAISES_EXCEPTIONS (type));
9194 return build_ref_qualified_type (result, rqual);
9197 /* Returns nonzero if TYPE is const or volatile. */
9200 cv_qualified_p (const_tree type)
9202 int quals = cp_type_quals (type);
9203 return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
9206 /* Returns nonzero if the TYPE contains a mutable member. */
9209 cp_has_mutable_p (const_tree type)
9211 /* This CONST_CAST is okay because strip_array_types returns its
9212 argument unmodified and we assign it to a const_tree. */
9213 type = strip_array_types (CONST_CAST_TREE(type));
9215 return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
9218 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
9219 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
9220 approximation. In particular, consider:
9223 struct S { int i; };
9224 const S s = { f(); }
9226 Here, we will make "s" as TREE_READONLY (because it is declared
9227 "const") -- only to reverse ourselves upon seeing that the
9228 initializer is non-constant. */
9231 cp_apply_type_quals_to_decl (int type_quals, tree decl)
9233 tree type = TREE_TYPE (decl);
9235 if (type == error_mark_node)
9238 if (TREE_CODE (decl) == TYPE_DECL)
9241 gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
9242 && type_quals != TYPE_UNQUALIFIED));
9244 /* Avoid setting TREE_READONLY incorrectly. */
9245 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
9246 constructor can produce constant init, so rely on cp_finish_decl to
9247 clear TREE_READONLY if the variable has non-constant init. */
9249 /* If the type has (or might have) a mutable component, that component
9250 might be modified. */
9251 if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
9252 type_quals &= ~TYPE_QUAL_CONST;
9254 c_apply_type_quals_to_decl (type_quals, decl);
9257 /* Subroutine of casts_away_constness. Make T1 and T2 point at
9258 exemplar types such that casting T1 to T2 is casting away constness
9259 if and only if there is no implicit conversion from T1 to T2. */
9262 casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
9267 /* [expr.const.cast]
9269 For multi-level pointer to members and multi-level mixed pointers
9270 and pointers to members (conv.qual), the "member" aspect of a
9271 pointer to member level is ignored when determining if a const
9272 cv-qualifier has been cast away. */
9273 /* [expr.const.cast]
9275 For two pointer types:
9277 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
9278 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
9281 casting from X1 to X2 casts away constness if, for a non-pointer
9282 type T there does not exist an implicit conversion (clause
9285 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
9289 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
9290 if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
9291 || (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
9293 *t1 = cp_build_qualified_type (void_type_node,
9294 cp_type_quals (*t1));
9295 *t2 = cp_build_qualified_type (void_type_node,
9296 cp_type_quals (*t2));
9300 quals1 = cp_type_quals (*t1);
9301 quals2 = cp_type_quals (*t2);
9303 if (TYPE_PTRDATAMEM_P (*t1))
9304 *t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
9306 *t1 = TREE_TYPE (*t1);
9307 if (TYPE_PTRDATAMEM_P (*t2))
9308 *t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
9310 *t2 = TREE_TYPE (*t2);
9312 casts_away_constness_r (t1, t2, complain);
9313 *t1 = build_pointer_type (*t1);
9314 *t2 = build_pointer_type (*t2);
9315 *t1 = cp_build_qualified_type (*t1, quals1);
9316 *t2 = cp_build_qualified_type (*t2, quals2);
9319 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
9322 ??? This function returns non-zero if casting away qualifiers not
9323 just const. We would like to return to the caller exactly which
9324 qualifiers are casted away to give more accurate diagnostics.
9328 casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
9330 if (TREE_CODE (t2) == REFERENCE_TYPE)
9332 /* [expr.const.cast]
9334 Casting from an lvalue of type T1 to an lvalue of type T2
9335 using a reference cast casts away constness if a cast from an
9336 rvalue of type "pointer to T1" to the type "pointer to T2"
9337 casts away constness. */
9338 t1 = (TREE_CODE (t1) == REFERENCE_TYPE ? TREE_TYPE (t1) : t1);
9339 return casts_away_constness (build_pointer_type (t1),
9340 build_pointer_type (TREE_TYPE (t2)),
9344 if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
9345 /* [expr.const.cast]
9347 Casting from an rvalue of type "pointer to data member of X
9348 of type T1" to the type "pointer to data member of Y of type
9349 T2" casts away constness if a cast from an rvalue of type
9350 "pointer to T1" to the type "pointer to T2" casts away
9352 return casts_away_constness
9353 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
9354 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
9357 /* Casting away constness is only something that makes sense for
9358 pointer or reference types. */
9359 if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
9362 /* Top-level qualifiers don't matter. */
9363 t1 = TYPE_MAIN_VARIANT (t1);
9364 t2 = TYPE_MAIN_VARIANT (t2);
9365 casts_away_constness_r (&t1, &t2, complain);
9366 if (!can_convert (t2, t1, complain))
9372 /* If T is a REFERENCE_TYPE return the type to which T refers.
9373 Otherwise, return T itself. */
9376 non_reference (tree t)
9378 if (t && TREE_CODE (t) == REFERENCE_TYPE)
9384 /* Return nonzero if REF is an lvalue valid for this language;
9385 otherwise, print an error message and return zero. USE says
9386 how the lvalue is being used and so selects the error message. */
9389 lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
9391 cp_lvalue_kind kind = lvalue_kind (ref);
9393 if (kind == clk_none)
9395 if (complain & tf_error)
9396 lvalue_error (input_location, use);
9399 else if (kind & (clk_rvalueref|clk_class))
9401 if (!(complain & tf_error))
9403 if (kind & clk_class)
9404 /* Make this a permerror because we used to accept it. */
9405 permerror (input_location, "using temporary as lvalue");
9407 error ("using xvalue (rvalue reference) as lvalue");
9412 /* Return true if a user-defined literal operator is a raw operator. */
9415 check_raw_literal_operator (const_tree decl)
9417 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9420 bool maybe_raw_p = false;
9422 /* Count the number and type of arguments and check for ellipsis. */
9423 for (argtype = argtypes, arity = 0;
9424 argtype && argtype != void_list_node;
9425 ++arity, argtype = TREE_CHAIN (argtype))
9427 tree t = TREE_VALUE (argtype);
9429 if (same_type_p (t, const_string_type_node))
9433 return false; /* Found ellipsis. */
9435 if (!maybe_raw_p || arity != 1)
9442 /* Return true if a user-defined literal operator has one of the allowed
9446 check_literal_operator_args (const_tree decl,
9447 bool *long_long_unsigned_p, bool *long_double_p)
9449 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9451 *long_long_unsigned_p = false;
9452 *long_double_p = false;
9453 if (processing_template_decl || processing_specialization)
9454 return argtypes == void_list_node;
9461 /* Count the number and type of arguments and check for ellipsis. */
9462 for (argtype = argtypes, arity = 0;
9463 argtype && argtype != void_list_node;
9464 argtype = TREE_CHAIN (argtype))
9466 tree t = TREE_VALUE (argtype);
9471 bool maybe_raw_p = false;
9473 if (cp_type_quals (t) != TYPE_QUAL_CONST)
9475 t = TYPE_MAIN_VARIANT (t);
9476 if ((maybe_raw_p = same_type_p (t, char_type_node))
9477 || same_type_p (t, wchar_type_node)
9478 || same_type_p (t, char16_type_node)
9479 || same_type_p (t, char32_type_node))
9481 argtype = TREE_CHAIN (argtype);
9484 t = TREE_VALUE (argtype);
9485 if (maybe_raw_p && argtype == void_list_node)
9487 else if (same_type_p (t, size_type_node))
9496 else if (same_type_p (t, long_long_unsigned_type_node))
9499 *long_long_unsigned_p = true;
9501 else if (same_type_p (t, long_double_type_node))
9504 *long_double_p = true;
9506 else if (same_type_p (t, char_type_node))
9508 else if (same_type_p (t, wchar_type_node))
9510 else if (same_type_p (t, char16_type_node))
9512 else if (same_type_p (t, char32_type_node))
9518 return false; /* Found ellipsis. */
9520 if (arity != max_arity)
9527 /* Always returns false since unlike C90, C++ has no concept of implicit
9528 function declarations. */
9531 c_decl_implicit (const_tree)