1 /* Build expressions with type checking for C++ compiler.
2 Copyright (C) 1987-2024 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"
38 #include "c-family/c-type-mismatch.h"
39 #include "stringpool.h"
44 static tree
cp_build_addr_expr_strict (tree
, tsubst_flags_t
);
45 static tree
cp_build_function_call (tree
, tree
, tsubst_flags_t
);
46 static tree
pfn_from_ptrmemfunc (tree
);
47 static tree
delta_from_ptrmemfunc (tree
);
48 static tree
convert_for_assignment (tree
, tree
, impl_conv_rhs
, tree
, int,
50 static tree
cp_pointer_int_sum (location_t
, enum tree_code
, tree
, tree
,
52 static tree
rationalize_conditional_expr (enum tree_code
, tree
,
54 static bool comp_ptr_ttypes_real (tree
, tree
, int);
55 static bool comp_except_types (tree
, tree
, bool);
56 static bool comp_array_types (const_tree
, const_tree
, compare_bounds_t
, bool);
57 static tree
pointer_diff (location_t
, tree
, tree
, tree
, tsubst_flags_t
, tree
*);
58 static tree
get_delta_difference (tree
, tree
, bool, bool, tsubst_flags_t
);
59 static void casts_away_constness_r (tree
*, tree
*, tsubst_flags_t
);
60 static bool casts_away_constness (tree
, tree
, tsubst_flags_t
);
61 static bool maybe_warn_about_returning_address_of_local (tree
, location_t
= UNKNOWN_LOCATION
);
62 static void error_args_num (location_t
, tree
, bool);
63 static int convert_arguments (tree
, vec
<tree
, va_gc
> **, tree
, int,
65 static bool is_std_move_p (tree
);
66 static bool is_std_forward_p (tree
);
68 /* Do `exp = require_complete_type (exp);' to make sure exp
69 does not have an incomplete type. (That includes void types.)
70 Returns error_mark_node if the VALUE does not have
71 complete type when this function returns. */
74 require_complete_type (tree value
,
75 tsubst_flags_t complain
/* = tf_warning_or_error */)
79 if (processing_template_decl
|| value
== error_mark_node
)
82 if (TREE_CODE (value
) == OVERLOAD
)
83 type
= unknown_type_node
;
85 type
= TREE_TYPE (value
);
87 if (type
== error_mark_node
)
88 return error_mark_node
;
90 /* First, detect a valid value with a complete type. */
91 if (COMPLETE_TYPE_P (type
))
94 if (complete_type_or_maybe_complain (type
, value
, complain
))
97 return error_mark_node
;
100 /* Try to complete TYPE, if it is incomplete. For example, if TYPE is
101 a template instantiation, do the instantiation. Returns TYPE,
102 whether or not it could be completed, unless something goes
103 horribly wrong, in which case the error_mark_node is returned. */
106 complete_type (tree type
)
108 if (type
== NULL_TREE
)
109 /* Rather than crash, we return something sure to cause an error
111 return error_mark_node
;
113 if (type
== error_mark_node
|| COMPLETE_TYPE_P (type
))
115 else if (TREE_CODE (type
) == ARRAY_TYPE
)
117 tree t
= complete_type (TREE_TYPE (type
));
118 unsigned int needs_constructing
, has_nontrivial_dtor
;
119 if (COMPLETE_TYPE_P (t
) && !dependent_type_p (type
))
122 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t
));
124 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t
));
125 for (t
= TYPE_MAIN_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
127 TYPE_NEEDS_CONSTRUCTING (t
) = needs_constructing
;
128 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
) = has_nontrivial_dtor
;
131 else if (CLASS_TYPE_P (type
))
134 /* TYPE could be a class member we've not loaded the definition of. */
135 lazy_load_pendings (TYPE_NAME (TYPE_MAIN_VARIANT (type
)));
137 if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
138 instantiate_class_template (TYPE_MAIN_VARIANT (type
));
144 /* Like complete_type, but issue an error if the TYPE cannot be completed.
145 VALUE is used for informative diagnostics.
146 Returns NULL_TREE if the type cannot be made complete. */
149 complete_type_or_maybe_complain (tree type
, tree value
, tsubst_flags_t complain
)
151 type
= complete_type (type
);
152 if (type
== error_mark_node
)
153 /* We already issued an error. */
155 else if (!COMPLETE_TYPE_P (type
))
157 if (complain
& tf_error
)
158 cxx_incomplete_type_diagnostic (value
, type
, DK_ERROR
);
159 note_failed_type_completion_for_satisfaction (type
);
167 complete_type_or_else (tree type
, tree value
)
169 return complete_type_or_maybe_complain (type
, value
, tf_warning_or_error
);
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 (simple_cst_equal (TREE_PURPOSE (p1
), TREE_PURPOSE (p2
)) != 1)
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 /* Merge the attributes of type OTHER_TYPE into the attributes of type TYPE
260 and return a variant of TYPE with the merged attributes. */
263 merge_type_attributes_from (tree type
, tree other_type
)
265 tree attrs
= targetm
.merge_type_attributes (type
, other_type
);
266 attrs
= restrict_type_identity_attributes_to (attrs
, TYPE_ATTRIBUTES (type
));
267 return cp_build_type_attribute_variant (type
, attrs
);
270 /* Compare floating point conversion ranks and subranks of T1 and T2
271 types. If T1 and T2 have unordered conversion ranks, return 3.
272 If T1 has greater conversion rank than T2, return 2.
273 If T2 has greater conversion rank than T1, return -2.
274 If T1 has equal conversion rank as T2, return -1, 0 or 1 depending
275 on if T1 has smaller, equal or greater conversion subrank than
279 cp_compare_floating_point_conversion_ranks (tree t1
, tree t2
)
281 tree mv1
= TYPE_MAIN_VARIANT (t1
);
282 tree mv2
= TYPE_MAIN_VARIANT (t2
);
289 for (int i
= 0; i
< NUM_FLOATN_NX_TYPES
; ++i
)
291 if (mv1
== FLOATN_NX_TYPE_NODE (i
))
293 if (mv2
== FLOATN_NX_TYPE_NODE (i
))
296 if (mv1
== bfloat16_type_node
)
298 if (mv2
== bfloat16_type_node
)
300 if (extended2
&& !extended1
)
302 int ret
= cp_compare_floating_point_conversion_ranks (t2
, t1
);
303 return ret
== 3 ? 3 : -ret
;
306 const struct real_format
*fmt1
= REAL_MODE_FORMAT (TYPE_MODE (t1
));
307 const struct real_format
*fmt2
= REAL_MODE_FORMAT (TYPE_MODE (t2
));
308 gcc_assert (fmt1
->b
== 2 && fmt2
->b
== 2);
309 /* For {ibm,mips}_extended_format formats, the type has variable
310 precision up to ~2150 bits when the first double is around maximum
311 representable double and second double is subnormal minimum.
312 So, e.g. for __ibm128 vs. std::float128_t, they have unordered
314 int p1
= (MODE_COMPOSITE_P (TYPE_MODE (t1
))
315 ? fmt1
->emax
- fmt1
->emin
+ fmt1
->p
- 1 : fmt1
->p
);
316 int p2
= (MODE_COMPOSITE_P (TYPE_MODE (t2
))
317 ? fmt2
->emax
- fmt2
->emin
+ fmt2
->p
- 1 : fmt2
->p
);
318 /* The rank of a floating point type T is greater than the rank of
319 any floating-point type whose set of values is a proper subset
320 of the set of values of T. */
321 if ((p1
> p2
&& fmt1
->emax
>= fmt2
->emax
)
322 || (p1
== p2
&& fmt1
->emax
> fmt2
->emax
))
324 if ((p1
< p2
&& fmt1
->emax
<= fmt2
->emax
)
325 || (p1
== p2
&& fmt1
->emax
< fmt2
->emax
))
327 if ((p1
> p2
&& fmt1
->emax
< fmt2
->emax
)
328 || (p1
< p2
&& fmt1
->emax
> fmt2
->emax
))
330 if (!extended1
&& !extended2
)
332 /* The rank of long double is greater than the rank of double, which
333 is greater than the rank of float. */
334 if (t1
== long_double_type_node
)
336 else if (t2
== long_double_type_node
)
338 if (t1
== double_type_node
)
340 else if (t2
== double_type_node
)
342 if (t1
== float_type_node
)
344 else if (t2
== float_type_node
)
348 /* Two extended floating-point types with the same set of values have equal
350 if (extended1
&& extended2
)
352 if ((extended1
<= NUM_FLOATN_TYPES
) == (extended2
<= NUM_FLOATN_TYPES
))
354 /* Prefer higher extendedN value. */
355 if (extended1
> extended2
)
357 else if (extended1
< extended2
)
362 else if (extended1
<= NUM_FLOATN_TYPES
)
363 /* Prefer _FloatN type over _FloatMx type. */
365 else if (extended2
<= NUM_FLOATN_TYPES
)
371 /* gcc_assert (extended1 && !extended2); */
374 for (p
= &float_type_node
; p
<= &long_double_type_node
; ++p
)
376 const struct real_format
*fmt3
= REAL_MODE_FORMAT (TYPE_MODE (*p
));
377 gcc_assert (fmt3
->b
== 2);
378 int p3
= (MODE_COMPOSITE_P (TYPE_MODE (*p
))
379 ? fmt3
->emax
- fmt3
->emin
+ fmt3
->p
- 1 : fmt3
->p
);
380 if (p1
== p3
&& fmt1
->emax
== fmt3
->emax
)
383 /* An extended floating-point type with the same set of values
384 as exactly one cv-unqualified standard floating-point type
385 has a rank equal to the rank of that standard floating-point
388 An extended floating-point type with the same set of values
389 as more than one cv-unqualified standard floating-point type
390 has a rank equal to the rank of double.
392 Thus, if the latter is true and t2 is long double, t2
394 if (cnt
> 1 && mv2
== long_double_type_node
)
396 /* And similarly if t2 is float, t2 has lower rank. */
397 if (cnt
> 1 && mv2
== float_type_node
)
399 /* Otherwise, they have equal rank, but extended types
400 (other than std::bfloat16_t) have higher subrank.
401 std::bfloat16_t shouldn't have equal rank to any standard
402 floating point type. */
406 /* Return the common type for two arithmetic types T1 and T2 under the
407 usual arithmetic conversions. The default conversions have already
408 been applied, and enumerated types converted to their compatible
412 cp_common_type (tree t1
, tree t2
)
414 enum tree_code code1
= TREE_CODE (t1
);
415 enum tree_code code2
= TREE_CODE (t2
);
420 /* In what follows, we slightly generalize the rules given in [expr] so
421 as to deal with `long long' and `complex'. First, merge the
423 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
425 if (SCOPED_ENUM_P (t1
) || SCOPED_ENUM_P (t2
))
427 if (TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
428 return build_type_attribute_variant (t1
, attributes
);
433 /* FIXME: Attributes. */
434 gcc_assert (ARITHMETIC_TYPE_P (t1
)
435 || VECTOR_TYPE_P (t1
)
436 || UNSCOPED_ENUM_P (t1
));
437 gcc_assert (ARITHMETIC_TYPE_P (t2
)
438 || VECTOR_TYPE_P (t2
)
439 || UNSCOPED_ENUM_P (t2
));
441 /* If one type is complex, form the common type of the non-complex
442 components, then make that complex. Use T1 or T2 if it is the
444 if (code1
== COMPLEX_TYPE
|| code2
== COMPLEX_TYPE
)
446 tree subtype1
= code1
== COMPLEX_TYPE
? TREE_TYPE (t1
) : t1
;
447 tree subtype2
= code2
== COMPLEX_TYPE
? TREE_TYPE (t2
) : t2
;
449 = type_after_usual_arithmetic_conversions (subtype1
, subtype2
);
451 if (subtype
== error_mark_node
)
453 if (code1
== COMPLEX_TYPE
&& TREE_TYPE (t1
) == subtype
)
454 return build_type_attribute_variant (t1
, attributes
);
455 else if (code2
== COMPLEX_TYPE
&& TREE_TYPE (t2
) == subtype
)
456 return build_type_attribute_variant (t2
, attributes
);
458 return build_type_attribute_variant (build_complex_type (subtype
),
462 if (code1
== VECTOR_TYPE
)
464 /* When we get here we should have two vectors of the same size.
465 Just prefer the unsigned one if present. */
466 if (TYPE_UNSIGNED (t1
))
467 return merge_type_attributes_from (t1
, t2
);
469 return merge_type_attributes_from (t2
, t1
);
472 /* If only one is real, use it as the result. */
473 if (code1
== REAL_TYPE
&& code2
!= REAL_TYPE
)
474 return build_type_attribute_variant (t1
, attributes
);
475 if (code2
== REAL_TYPE
&& code1
!= REAL_TYPE
)
476 return build_type_attribute_variant (t2
, attributes
);
478 if (code1
== REAL_TYPE
479 && (extended_float_type_p (t1
) || extended_float_type_p (t2
)))
481 tree mv1
= TYPE_MAIN_VARIANT (t1
);
482 tree mv2
= TYPE_MAIN_VARIANT (t2
);
484 return build_type_attribute_variant (t1
, attributes
);
486 int cmpret
= cp_compare_floating_point_conversion_ranks (mv1
, mv2
);
488 return error_mark_node
;
489 else if (cmpret
>= 0)
490 return build_type_attribute_variant (t1
, attributes
);
492 return build_type_attribute_variant (t2
, attributes
);
495 /* Both real or both integers; use the one with greater precision. */
496 if (TYPE_PRECISION (t1
) > TYPE_PRECISION (t2
))
497 return build_type_attribute_variant (t1
, attributes
);
498 else if (TYPE_PRECISION (t2
) > TYPE_PRECISION (t1
))
499 return build_type_attribute_variant (t2
, attributes
);
501 /* The types are the same; no need to do anything fancy. */
502 if (TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
503 return build_type_attribute_variant (t1
, attributes
);
505 if (code1
!= REAL_TYPE
)
507 /* If one is unsigned long long, then convert the other to unsigned
509 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_long_unsigned_type_node
)
510 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_long_unsigned_type_node
))
511 return build_type_attribute_variant (long_long_unsigned_type_node
,
513 /* If one is a long long, and the other is an unsigned long, and
514 long long can represent all the values of an unsigned long, then
515 convert to a long long. Otherwise, convert to an unsigned long
516 long. Otherwise, if either operand is long long, convert the
519 Since we're here, we know the TYPE_PRECISION is the same;
520 therefore converting to long long cannot represent all the values
521 of an unsigned long, so we choose unsigned long long in that
523 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_long_integer_type_node
)
524 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_long_integer_type_node
))
526 tree t
= ((TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
527 ? long_long_unsigned_type_node
528 : long_long_integer_type_node
);
529 return build_type_attribute_variant (t
, attributes
);
532 /* Go through the same procedure, but for longs. */
533 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_unsigned_type_node
)
534 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_unsigned_type_node
))
535 return build_type_attribute_variant (long_unsigned_type_node
,
537 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_integer_type_node
)
538 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_integer_type_node
))
540 tree t
= ((TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
541 ? long_unsigned_type_node
: long_integer_type_node
);
542 return build_type_attribute_variant (t
, attributes
);
545 /* For __intN types, either the type is __int128 (and is lower
546 priority than the types checked above, but higher than other
547 128-bit types) or it's known to not be the same size as other
548 types (enforced in toplev.cc). Prefer the unsigned type. */
549 for (i
= 0; i
< NUM_INT_N_ENTS
; i
++)
551 if (int_n_enabled_p
[i
]
552 && (same_type_p (TYPE_MAIN_VARIANT (t1
), int_n_trees
[i
].signed_type
)
553 || same_type_p (TYPE_MAIN_VARIANT (t2
), int_n_trees
[i
].signed_type
)
554 || same_type_p (TYPE_MAIN_VARIANT (t1
), int_n_trees
[i
].unsigned_type
)
555 || same_type_p (TYPE_MAIN_VARIANT (t2
), int_n_trees
[i
].unsigned_type
)))
557 tree t
= ((TYPE_UNSIGNED (t1
) || TYPE_UNSIGNED (t2
))
558 ? int_n_trees
[i
].unsigned_type
559 : int_n_trees
[i
].signed_type
);
560 return build_type_attribute_variant (t
, attributes
);
564 /* Otherwise prefer the unsigned one. */
565 if (TYPE_UNSIGNED (t1
))
566 return build_type_attribute_variant (t1
, attributes
);
568 return build_type_attribute_variant (t2
, attributes
);
572 if (same_type_p (TYPE_MAIN_VARIANT (t1
), long_double_type_node
)
573 || same_type_p (TYPE_MAIN_VARIANT (t2
), long_double_type_node
))
574 return build_type_attribute_variant (long_double_type_node
,
576 if (same_type_p (TYPE_MAIN_VARIANT (t1
), double_type_node
)
577 || same_type_p (TYPE_MAIN_VARIANT (t2
), double_type_node
))
578 return build_type_attribute_variant (double_type_node
,
580 if (same_type_p (TYPE_MAIN_VARIANT (t1
), float_type_node
)
581 || same_type_p (TYPE_MAIN_VARIANT (t2
), float_type_node
))
582 return build_type_attribute_variant (float_type_node
,
585 /* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
586 the standard C++ floating-point types. Logic earlier in this
587 function has already eliminated the possibility that
588 TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
589 compelling reason to choose one or the other. */
590 return build_type_attribute_variant (t1
, attributes
);
594 /* T1 and T2 are arithmetic or enumeration types. Return the type
595 that will result from the "usual arithmetic conversions" on T1 and
596 T2 as described in [expr]. */
599 type_after_usual_arithmetic_conversions (tree t1
, tree t2
)
601 gcc_assert (ARITHMETIC_TYPE_P (t1
)
602 || VECTOR_TYPE_P (t1
)
603 || UNSCOPED_ENUM_P (t1
));
604 gcc_assert (ARITHMETIC_TYPE_P (t2
)
605 || VECTOR_TYPE_P (t2
)
606 || UNSCOPED_ENUM_P (t2
));
608 /* Perform the integral promotions. We do not promote real types here. */
609 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1
)
610 && INTEGRAL_OR_ENUMERATION_TYPE_P (t2
))
612 t1
= type_promotes_to (t1
);
613 t2
= type_promotes_to (t2
);
616 return cp_common_type (t1
, t2
);
620 composite_pointer_error (const op_location_t
&location
,
621 diagnostic_t kind
, tree t1
, tree t2
,
622 composite_pointer_operation operation
)
627 emit_diagnostic (kind
, location
, 0,
628 "comparison between "
629 "distinct pointer types %qT and %qT lacks a cast",
633 emit_diagnostic (kind
, location
, 0,
634 "conversion between "
635 "distinct pointer types %qT and %qT lacks a cast",
638 case CPO_CONDITIONAL_EXPR
:
639 emit_diagnostic (kind
, location
, 0,
640 "conditional expression between "
641 "distinct pointer types %qT and %qT lacks a cast",
649 /* Subroutine of composite_pointer_type to implement the recursive
650 case. See that function for documentation of the parameters. And ADD_CONST
651 is used to track adding "const" where needed. */
654 composite_pointer_type_r (const op_location_t
&location
,
655 tree t1
, tree t2
, bool *add_const
,
656 composite_pointer_operation operation
,
657 tsubst_flags_t complain
)
664 /* Determine the types pointed to by T1 and T2. */
667 pointee1
= TREE_TYPE (t1
);
668 pointee2
= TREE_TYPE (t2
);
672 pointee1
= TYPE_PTRMEM_POINTED_TO_TYPE (t1
);
673 pointee2
= TYPE_PTRMEM_POINTED_TO_TYPE (t2
);
678 If T1 and T2 are similar types, the result is the cv-combined type of
680 if (same_type_ignoring_top_level_qualifiers_p (pointee1
, pointee2
))
681 result_type
= pointee1
;
682 else if ((TYPE_PTR_P (pointee1
) && TYPE_PTR_P (pointee2
))
683 || (TYPE_PTRMEM_P (pointee1
) && TYPE_PTRMEM_P (pointee2
)))
685 result_type
= composite_pointer_type_r (location
, pointee1
, pointee2
,
686 add_const
, operation
, complain
);
687 if (result_type
== error_mark_node
)
688 return error_mark_node
;
692 if (complain
& tf_error
)
693 composite_pointer_error (location
, DK_PERMERROR
,
696 return error_mark_node
;
697 result_type
= void_type_node
;
699 const int q1
= cp_type_quals (pointee1
);
700 const int q2
= cp_type_quals (pointee2
);
701 const int quals
= q1
| q2
;
702 result_type
= cp_build_qualified_type (result_type
,
705 : TYPE_UNQUALIFIED
)));
706 /* The cv-combined type can add "const" as per [conv.qual]/3.3 (except for
707 the TLQ). The reason is that both T1 and T2 can then be converted to the
708 cv-combined type of T1 and T2. */
709 if (quals
!= q1
|| quals
!= q2
)
711 /* If the original types were pointers to members, so is the
713 if (TYPE_PTRMEM_P (t1
))
715 if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1
),
716 TYPE_PTRMEM_CLASS_TYPE (t2
)))
718 if (complain
& tf_error
)
719 composite_pointer_error (location
, DK_PERMERROR
,
722 return error_mark_node
;
724 result_type
= build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1
),
728 result_type
= build_pointer_type (result_type
);
730 /* Merge the attributes. */
731 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
732 return build_type_attribute_variant (result_type
, attributes
);
735 /* Return the composite pointer type (see [expr.type]) for T1 and T2.
736 ARG1 and ARG2 are the values with those types. The OPERATION is to
737 describe the operation between the pointer types,
738 in case an error occurs.
740 This routine also implements the computation of a common type for
741 pointers-to-members as per [expr.eq]. */
744 composite_pointer_type (const op_location_t
&location
,
745 tree t1
, tree t2
, tree arg1
, tree arg2
,
746 composite_pointer_operation operation
,
747 tsubst_flags_t complain
)
754 If one operand is a null pointer constant, the composite pointer
755 type is the type of the other operand. */
756 if (null_ptr_cst_p (arg1
))
758 if (null_ptr_cst_p (arg2
))
765 If one of the operands has type "pointer to cv1 void", then
766 the other has type "pointer to cv2 T", and the composite pointer
767 type is "pointer to cv12 void", where cv12 is the union of cv1
770 If either type is a pointer to void, make sure it is T1. */
771 if (TYPE_PTR_P (t2
) && VOID_TYPE_P (TREE_TYPE (t2
)))
774 /* Now, if T1 is a pointer to void, merge the qualifiers. */
775 if (TYPE_PTR_P (t1
) && VOID_TYPE_P (TREE_TYPE (t1
)))
780 if (TYPE_PTRFN_P (t2
))
782 if (complain
& tf_error
)
787 pedwarn (location
, OPT_Wpedantic
,
788 "ISO C++ forbids comparison between pointer "
789 "of type %<void *%> and pointer-to-function");
792 pedwarn (location
, OPT_Wpedantic
,
793 "ISO C++ forbids conversion between pointer "
794 "of type %<void *%> and pointer-to-function");
796 case CPO_CONDITIONAL_EXPR
:
797 pedwarn (location
, OPT_Wpedantic
,
798 "ISO C++ forbids conditional expression between "
799 "pointer of type %<void *%> and "
800 "pointer-to-function");
807 return error_mark_node
;
810 = cp_build_qualified_type (void_type_node
,
811 (cp_type_quals (TREE_TYPE (t1
))
812 | cp_type_quals (TREE_TYPE (t2
))));
813 result_type
= build_pointer_type (result_type
);
814 /* Merge the attributes. */
815 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
816 return build_type_attribute_variant (result_type
, attributes
);
819 if (c_dialect_objc () && TYPE_PTR_P (t1
)
822 if (objc_have_common_type (t1
, t2
, -3, NULL_TREE
))
823 return objc_common_type (t1
, t2
);
826 /* if T1 or T2 is "pointer to noexcept function" and the other type is
827 "pointer to function", where the function types are otherwise the same,
828 "pointer to function" */
829 if (fnptr_conv_p (t1
, t2
))
831 if (fnptr_conv_p (t2
, t1
))
834 /* [expr.eq] permits the application of a pointer conversion to
835 bring the pointers to a common type. */
836 if (TYPE_PTR_P (t1
) && TYPE_PTR_P (t2
)
837 && CLASS_TYPE_P (TREE_TYPE (t1
))
838 && CLASS_TYPE_P (TREE_TYPE (t2
))
839 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1
),
842 class1
= TREE_TYPE (t1
);
843 class2
= TREE_TYPE (t2
);
845 if (DERIVED_FROM_P (class1
, class2
))
846 t2
= (build_pointer_type
847 (cp_build_qualified_type (class1
, cp_type_quals (class2
))));
848 else if (DERIVED_FROM_P (class2
, class1
))
849 t1
= (build_pointer_type
850 (cp_build_qualified_type (class2
, cp_type_quals (class1
))));
853 if (complain
& tf_error
)
854 composite_pointer_error (location
, DK_ERROR
, t1
, t2
, operation
);
855 return error_mark_node
;
858 /* [expr.eq] permits the application of a pointer-to-member
859 conversion to change the class type of one of the types. */
860 else if (TYPE_PTRMEM_P (t1
)
861 && !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1
),
862 TYPE_PTRMEM_CLASS_TYPE (t2
)))
864 class1
= TYPE_PTRMEM_CLASS_TYPE (t1
);
865 class2
= TYPE_PTRMEM_CLASS_TYPE (t2
);
867 if (DERIVED_FROM_P (class1
, class2
))
868 t1
= build_ptrmem_type (class2
, TYPE_PTRMEM_POINTED_TO_TYPE (t1
));
869 else if (DERIVED_FROM_P (class2
, class1
))
870 t2
= build_ptrmem_type (class1
, TYPE_PTRMEM_POINTED_TO_TYPE (t2
));
873 if (complain
& tf_error
)
877 error_at (location
, "comparison between distinct "
878 "pointer-to-member types %qT and %qT lacks a cast",
882 error_at (location
, "conversion between distinct "
883 "pointer-to-member types %qT and %qT lacks a cast",
886 case CPO_CONDITIONAL_EXPR
:
887 error_at (location
, "conditional expression between distinct "
888 "pointer-to-member types %qT and %qT lacks a cast",
894 return error_mark_node
;
898 bool add_const
= false;
899 return composite_pointer_type_r (location
, t1
, t2
, &add_const
, operation
,
903 /* Return the merged type of two types.
904 We assume that comptypes has already been done and returned 1;
905 if that isn't so, this may crash.
907 This just combines attributes and default arguments; any other
908 differences would cause the two types to compare unalike. */
911 merge_types (tree t1
, tree t2
)
913 enum tree_code code1
;
914 enum tree_code code2
;
917 /* Save time if the two types are the same. */
920 if (original_type (t1
) == original_type (t2
))
923 /* If one type is nonsense, use the other. */
924 if (t1
== error_mark_node
)
926 if (t2
== error_mark_node
)
929 /* Handle merging an auto redeclaration with a previous deduced
934 /* Merge the attributes. */
935 attributes
= (*targetm
.merge_type_attributes
) (t1
, t2
);
937 if (TYPE_PTRMEMFUNC_P (t1
))
938 t1
= TYPE_PTRMEMFUNC_FN_TYPE (t1
);
939 if (TYPE_PTRMEMFUNC_P (t2
))
940 t2
= TYPE_PTRMEMFUNC_FN_TYPE (t2
);
942 code1
= TREE_CODE (t1
);
943 code2
= TREE_CODE (t2
);
946 gcc_assert (code1
== TYPENAME_TYPE
|| code2
== TYPENAME_TYPE
);
947 if (code1
== TYPENAME_TYPE
)
949 t1
= resolve_typename_type (t1
, /*only_current_p=*/true);
950 code1
= TREE_CODE (t1
);
954 t2
= resolve_typename_type (t2
, /*only_current_p=*/true);
955 code2
= TREE_CODE (t2
);
963 /* For two pointers, do this recursively on the target type. */
965 tree target
= merge_types (TREE_TYPE (t1
), TREE_TYPE (t2
));
966 int quals
= cp_type_quals (t1
);
968 if (code1
== POINTER_TYPE
)
970 t1
= build_pointer_type (target
);
971 if (TREE_CODE (target
) == METHOD_TYPE
)
972 t1
= build_ptrmemfunc_type (t1
);
975 t1
= cp_build_reference_type (target
, TYPE_REF_IS_RVALUE (t1
));
976 t1
= build_type_attribute_variant (t1
, attributes
);
977 t1
= cp_build_qualified_type (t1
, quals
);
986 quals
= cp_type_quals (t1
);
987 pointee
= merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1
),
988 TYPE_PTRMEM_POINTED_TO_TYPE (t2
));
989 t1
= build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1
),
991 t1
= cp_build_qualified_type (t1
, quals
);
997 tree elt
= merge_types (TREE_TYPE (t1
), TREE_TYPE (t2
));
998 /* Save space: see if the result is identical to one of the args. */
999 if (elt
== TREE_TYPE (t1
) && TYPE_DOMAIN (t1
))
1000 return build_type_attribute_variant (t1
, attributes
);
1001 if (elt
== TREE_TYPE (t2
) && TYPE_DOMAIN (t2
))
1002 return build_type_attribute_variant (t2
, attributes
);
1003 /* Merge the element types, and have a size if either arg has one. */
1004 t1
= build_cplus_array_type
1005 (elt
, TYPE_DOMAIN (TYPE_DOMAIN (t1
) ? t1
: t2
));
1010 /* Function types: prefer the one that specified arg types.
1011 If both do, merge the arg types. Also merge the return types. */
1013 tree valtype
= merge_types (TREE_TYPE (t1
), TREE_TYPE (t2
));
1014 tree p1
= TYPE_ARG_TYPES (t1
);
1015 tree p2
= TYPE_ARG_TYPES (t2
);
1018 /* Save space: see if the result is identical to one of the args. */
1019 if (valtype
== TREE_TYPE (t1
) && ! p2
)
1020 return cp_build_type_attribute_variant (t1
, attributes
);
1021 if (valtype
== TREE_TYPE (t2
) && ! p1
)
1022 return cp_build_type_attribute_variant (t2
, attributes
);
1024 /* Simple way if one arg fails to specify argument types. */
1025 if (p1
== NULL_TREE
|| TREE_VALUE (p1
) == void_type_node
)
1027 else if (p2
== NULL_TREE
|| TREE_VALUE (p2
) == void_type_node
)
1030 parms
= commonparms (p1
, p2
);
1032 cp_cv_quals quals
= type_memfn_quals (t1
);
1033 cp_ref_qualifier rqual
= type_memfn_rqual (t1
);
1034 gcc_assert (quals
== type_memfn_quals (t2
));
1035 gcc_assert (rqual
== type_memfn_rqual (t2
));
1037 tree rval
= build_function_type (valtype
, parms
);
1038 rval
= apply_memfn_quals (rval
, quals
);
1039 tree raises
= merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1
),
1040 TYPE_RAISES_EXCEPTIONS (t2
));
1041 bool late_return_type_p
= TYPE_HAS_LATE_RETURN_TYPE (t1
);
1042 t1
= build_cp_fntype_variant (rval
, rqual
, raises
, late_return_type_p
);
1048 /* Get this value the long way, since TYPE_METHOD_BASETYPE
1049 is just the main variant of this. */
1050 tree basetype
= class_of_this_parm (t2
);
1051 tree raises
= merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1
),
1052 TYPE_RAISES_EXCEPTIONS (t2
));
1053 cp_ref_qualifier rqual
= type_memfn_rqual (t1
);
1055 bool late_return_type_1_p
= TYPE_HAS_LATE_RETURN_TYPE (t1
);
1057 /* If this was a member function type, get back to the
1058 original type of type member function (i.e., without
1059 the class instance variable up front. */
1060 t1
= build_function_type (TREE_TYPE (t1
),
1061 TREE_CHAIN (TYPE_ARG_TYPES (t1
)));
1062 t2
= build_function_type (TREE_TYPE (t2
),
1063 TREE_CHAIN (TYPE_ARG_TYPES (t2
)));
1064 t3
= merge_types (t1
, t2
);
1065 t3
= build_method_type_directly (basetype
, TREE_TYPE (t3
),
1066 TYPE_ARG_TYPES (t3
));
1067 t1
= build_cp_fntype_variant (t3
, rqual
, raises
, late_return_type_1_p
);
1072 /* There is no need to merge attributes into a TYPENAME_TYPE.
1073 When the type is instantiated it will have whatever
1074 attributes result from the instantiation. */
1078 if (attribute_list_equal (TYPE_ATTRIBUTES (t1
), attributes
))
1080 else if (attribute_list_equal (TYPE_ATTRIBUTES (t2
), attributes
))
1085 return cp_build_type_attribute_variant (t1
, attributes
);
1088 /* Return the ARRAY_TYPE type without its domain. */
1091 strip_array_domain (tree type
)
1094 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
1095 if (TYPE_DOMAIN (type
) == NULL_TREE
)
1097 t2
= build_cplus_array_type (TREE_TYPE (type
), NULL_TREE
);
1098 return cp_build_type_attribute_variant (t2
, TYPE_ATTRIBUTES (type
));
1101 /* Wrapper around cp_common_type that is used by c-common.cc and other
1102 front end optimizations that remove promotions.
1104 Return the common type for two arithmetic types T1 and T2 under the
1105 usual arithmetic conversions. The default conversions have already
1106 been applied, and enumerated types converted to their compatible
1110 common_type (tree t1
, tree t2
)
1112 /* If one type is nonsense, use the other */
1113 if (t1
== error_mark_node
)
1115 if (t2
== error_mark_node
)
1118 return cp_common_type (t1
, t2
);
1121 /* Return the common type of two pointer types T1 and T2. This is the
1122 type for the result of most arithmetic operations if the operands
1123 have the given two types.
1125 We assume that comp_target_types has already been done and returned
1126 nonzero; if that isn't so, this may crash. */
1129 common_pointer_type (tree t1
, tree t2
)
1131 gcc_assert ((TYPE_PTR_P (t1
) && TYPE_PTR_P (t2
))
1132 || (TYPE_PTRDATAMEM_P (t1
) && TYPE_PTRDATAMEM_P (t2
))
1133 || (TYPE_PTRMEMFUNC_P (t1
) && TYPE_PTRMEMFUNC_P (t2
)));
1135 return composite_pointer_type (input_location
, t1
, t2
,
1136 error_mark_node
, error_mark_node
,
1137 CPO_CONVERSION
, tf_warning_or_error
);
1140 /* Compare two exception specifier types for exactness or subsetness, if
1141 allowed. Returns false for mismatch, true for match (same, or
1142 derived and !exact).
1144 [except.spec] "If a class X ... objects of class X or any class publicly
1145 and unambiguously derived from X. Similarly, if a pointer type Y * ...
1146 exceptions of type Y * or that are pointers to any type publicly and
1147 unambiguously derived from Y. Otherwise a function only allows exceptions
1148 that have the same type ..."
1149 This does not mention cv qualifiers and is different to what throw
1150 [except.throw] and catch [except.catch] will do. They will ignore the
1151 top level cv qualifiers, and allow qualifiers in the pointer to class
1154 We implement the letter of the standard. */
1157 comp_except_types (tree a
, tree b
, bool exact
)
1159 if (same_type_p (a
, b
))
1163 if (cp_type_quals (a
) || cp_type_quals (b
))
1166 if (TYPE_PTR_P (a
) && TYPE_PTR_P (b
))
1170 if (cp_type_quals (a
) || cp_type_quals (b
))
1174 if (TREE_CODE (a
) != RECORD_TYPE
1175 || TREE_CODE (b
) != RECORD_TYPE
)
1178 if (publicly_uniquely_derived_p (a
, b
))
1184 /* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
1185 If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
1186 If EXACT is ce_type, the C++17 type compatibility rules apply.
1187 If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
1188 If EXACT is ce_exact, the specs must be exactly the same. Exception lists
1189 are unordered, but we've already filtered out duplicates. Most lists will
1190 be in order, we should try to make use of that. */
1193 comp_except_specs (const_tree t1
, const_tree t2
, int exact
)
1202 /* First handle noexcept. */
1203 if (exact
< ce_exact
)
1205 if (exact
== ce_type
1206 && (canonical_eh_spec (CONST_CAST_TREE (t1
))
1207 == canonical_eh_spec (CONST_CAST_TREE (t2
))))
1210 /* noexcept(false) is compatible with no exception-specification,
1211 and less strict than any spec. */
1212 if (t1
== noexcept_false_spec
)
1213 return t2
== NULL_TREE
|| exact
== ce_derived
;
1214 /* Even a derived noexcept(false) is compatible with no
1215 exception-specification. */
1216 if (t2
== noexcept_false_spec
)
1217 return t1
== NULL_TREE
;
1219 /* Otherwise, if we aren't looking for an exact match, noexcept is
1220 equivalent to throw(). */
1221 if (t1
== noexcept_true_spec
)
1222 t1
= empty_except_spec
;
1223 if (t2
== noexcept_true_spec
)
1224 t2
= empty_except_spec
;
1227 /* If any noexcept is left, it is only comparable to itself;
1228 either we're looking for an exact match or we're redeclaring a
1229 template with dependent noexcept. */
1230 if ((t1
&& TREE_PURPOSE (t1
))
1231 || (t2
&& TREE_PURPOSE (t2
)))
1233 && (exact
== ce_exact
1234 ? TREE_PURPOSE (t1
) == TREE_PURPOSE (t2
)
1235 : cp_tree_equal (TREE_PURPOSE (t1
), TREE_PURPOSE (t2
))));
1237 if (t1
== NULL_TREE
) /* T1 is ... */
1238 return t2
== NULL_TREE
|| exact
== ce_derived
;
1239 if (!TREE_VALUE (t1
)) /* t1 is EMPTY */
1240 return t2
!= NULL_TREE
&& !TREE_VALUE (t2
);
1241 if (t2
== NULL_TREE
) /* T2 is ... */
1243 if (TREE_VALUE (t1
) && !TREE_VALUE (t2
)) /* T2 is EMPTY, T1 is not */
1244 return exact
== ce_derived
;
1246 /* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
1247 Count how many we find, to determine exactness. For exact matching and
1248 ordered T1, T2, this is an O(n) operation, otherwise its worst case is
1250 for (base
= t1
; t2
!= NULL_TREE
; t2
= TREE_CHAIN (t2
))
1252 for (probe
= base
; probe
!= NULL_TREE
; probe
= TREE_CHAIN (probe
))
1254 tree a
= TREE_VALUE (probe
);
1255 tree b
= TREE_VALUE (t2
);
1257 if (comp_except_types (a
, b
, exact
))
1259 if (probe
== base
&& exact
> ce_derived
)
1260 base
= TREE_CHAIN (probe
);
1265 if (probe
== NULL_TREE
)
1268 return exact
== ce_derived
|| base
== NULL_TREE
|| length
== list_length (t1
);
1271 /* Compare the array types T1 and T2. CB says how we should behave when
1272 comparing array bounds: bounds_none doesn't allow dimensionless arrays,
1273 bounds_either says than any array can be [], bounds_first means that
1274 onlt T1 can be an array with unknown bounds. STRICT is true if
1275 qualifiers must match when comparing the types of the array elements. */
1278 comp_array_types (const_tree t1
, const_tree t2
, compare_bounds_t cb
,
1288 /* The type of the array elements must be the same. */
1290 ? !same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
))
1291 : !similar_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1294 d1
= TYPE_DOMAIN (t1
);
1295 d2
= TYPE_DOMAIN (t2
);
1300 /* If one of the arrays is dimensionless, and the other has a
1301 dimension, they are of different types. However, it is valid to
1309 declarations for an array object can specify
1310 array types that differ by the presence or absence of a major
1311 array bound (_dcl.array_). */
1313 return cb
>= bounds_either
;
1315 return cb
== bounds_either
;
1317 /* Check that the dimensions are the same. */
1319 if (!cp_tree_equal (TYPE_MIN_VALUE (d1
), TYPE_MIN_VALUE (d2
)))
1321 max1
= TYPE_MAX_VALUE (d1
);
1322 max2
= TYPE_MAX_VALUE (d2
);
1324 if (!cp_tree_equal (max1
, max2
))
1330 /* Compare the relative position of T1 and T2 into their respective
1331 template parameter list.
1332 T1 and T2 must be template parameter types.
1333 Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
1336 comp_template_parms_position (tree t1
, tree t2
)
1338 tree index1
, index2
;
1339 gcc_assert (t1
&& t2
1340 && TREE_CODE (t1
) == TREE_CODE (t2
)
1341 && (TREE_CODE (t1
) == BOUND_TEMPLATE_TEMPLATE_PARM
1342 || TREE_CODE (t1
) == TEMPLATE_TEMPLATE_PARM
1343 || TREE_CODE (t1
) == TEMPLATE_TYPE_PARM
));
1345 index1
= TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1
));
1346 index2
= TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2
));
1348 /* Then compare their relative position. */
1349 if (TEMPLATE_PARM_IDX (index1
) != TEMPLATE_PARM_IDX (index2
)
1350 || TEMPLATE_PARM_LEVEL (index1
) != TEMPLATE_PARM_LEVEL (index2
)
1351 || (TEMPLATE_PARM_PARAMETER_PACK (index1
)
1352 != TEMPLATE_PARM_PARAMETER_PACK (index2
)))
1355 /* In C++14 we can end up comparing 'auto' to a normal template
1356 parameter. Don't confuse them. */
1357 if (cxx_dialect
>= cxx14
&& (is_auto (t1
) || is_auto (t2
)))
1358 return TYPE_IDENTIFIER (t1
) == TYPE_IDENTIFIER (t2
);
1363 /* Heuristic check if two parameter types can be considered ABI-equivalent. */
1366 cxx_safe_arg_type_equiv_p (tree t1
, tree t2
)
1368 t1
= TYPE_MAIN_VARIANT (t1
);
1369 t2
= TYPE_MAIN_VARIANT (t2
);
1375 /* The signedness of the parameter matters only when an integral
1376 type smaller than int is promoted to int, otherwise only the
1377 precision of the parameter matters.
1378 This check should make sure that the callee does not see
1379 undefined values in argument registers. */
1380 if (INTEGRAL_TYPE_P (t1
)
1381 && INTEGRAL_TYPE_P (t2
)
1382 && TYPE_PRECISION (t1
) == TYPE_PRECISION (t2
)
1383 && (TYPE_UNSIGNED (t1
) == TYPE_UNSIGNED (t2
)
1384 || !targetm
.calls
.promote_prototypes (NULL_TREE
)
1385 || TYPE_PRECISION (t1
) >= TYPE_PRECISION (integer_type_node
)))
1388 return same_type_p (t1
, t2
);
1391 /* Check if a type cast between two function types can be considered safe. */
1394 cxx_safe_function_type_cast_p (tree t1
, tree t2
)
1396 if (TREE_TYPE (t1
) == void_type_node
&&
1397 TYPE_ARG_TYPES (t1
) == void_list_node
)
1400 if (TREE_TYPE (t2
) == void_type_node
&&
1401 TYPE_ARG_TYPES (t2
) == void_list_node
)
1404 if (!cxx_safe_arg_type_equiv_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1407 for (t1
= TYPE_ARG_TYPES (t1
), t2
= TYPE_ARG_TYPES (t2
);
1409 t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
))
1410 if (!cxx_safe_arg_type_equiv_p (TREE_VALUE (t1
), TREE_VALUE (t2
)))
1416 /* Subroutine in comptypes. */
1419 structural_comptypes (tree t1
, tree t2
, int strict
)
1421 /* Both should be types that are not obviously the same. */
1422 gcc_checking_assert (t1
!= t2
&& TYPE_P (t1
) && TYPE_P (t2
));
1424 /* Suppress typename resolution under spec_hasher::equal in place of calling
1425 push_to_top_level there. */
1426 if (!comparing_specializations
)
1428 /* TYPENAME_TYPEs should be resolved if the qualifying scope is the
1429 current instantiation. */
1430 if (TREE_CODE (t1
) == TYPENAME_TYPE
)
1431 t1
= resolve_typename_type (t1
, /*only_current_p=*/true);
1433 if (TREE_CODE (t2
) == TYPENAME_TYPE
)
1434 t2
= resolve_typename_type (t2
, /*only_current_p=*/true);
1437 if (TYPE_PTRMEMFUNC_P (t1
))
1438 t1
= TYPE_PTRMEMFUNC_FN_TYPE (t1
);
1439 if (TYPE_PTRMEMFUNC_P (t2
))
1440 t2
= TYPE_PTRMEMFUNC_FN_TYPE (t2
);
1442 /* Different classes of types can't be compatible. */
1443 if (TREE_CODE (t1
) != TREE_CODE (t2
))
1446 /* Qualifiers must match. For array types, we will check when we
1447 recur on the array element types. */
1448 if (TREE_CODE (t1
) != ARRAY_TYPE
1449 && cp_type_quals (t1
) != cp_type_quals (t2
))
1451 if (TREE_CODE (t1
) == FUNCTION_TYPE
1452 && type_memfn_quals (t1
) != type_memfn_quals (t2
))
1454 /* Need to check this before TYPE_MAIN_VARIANT.
1455 FIXME function qualifiers should really change the main variant. */
1456 if (FUNC_OR_METHOD_TYPE_P (t1
))
1458 if (type_memfn_rqual (t1
) != type_memfn_rqual (t2
))
1460 if (flag_noexcept_type
1461 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1
),
1462 TYPE_RAISES_EXCEPTIONS (t2
),
1467 /* Allow for two different type nodes which have essentially the same
1468 definition. Note that we already checked for equality of the type
1469 qualifiers (just above). */
1470 if (TREE_CODE (t1
) != ARRAY_TYPE
1471 && TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
1474 /* Compare the types. Return false on known not-same. Break on not
1475 known. Never return true from this switch -- you'll break
1476 specialization comparison. */
1477 switch (TREE_CODE (t1
))
1481 /* All void and bool types are the same. */
1486 case FIXED_POINT_TYPE
:
1488 /* With these nodes, we can't determine type equivalence by
1489 looking at what is stored in the nodes themselves, because
1490 two nodes might have different TYPE_MAIN_VARIANTs but still
1491 represent the same type. For example, wchar_t and int could
1492 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1493 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1494 and are distinct types. On the other hand, int and the
1497 typedef int INT __attribute((may_alias));
1499 have identical properties, different TYPE_MAIN_VARIANTs, but
1500 represent the same type. The canonical type system keeps
1501 track of equivalence in this case, so we fall back on it. */
1502 if (TYPE_CANONICAL (t1
) != TYPE_CANONICAL (t2
))
1505 /* We don't need or want the attribute comparison. */
1508 case TEMPLATE_TEMPLATE_PARM
:
1509 case BOUND_TEMPLATE_TEMPLATE_PARM
:
1510 if (!comp_template_parms_position (t1
, t2
))
1512 if (!comp_template_parms
1513 (DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1
)),
1514 DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2
))))
1516 if (TREE_CODE (t1
) == TEMPLATE_TEMPLATE_PARM
)
1518 /* Don't check inheritance. */
1519 strict
= COMPARE_STRICT
;
1524 if (TYPE_TEMPLATE_INFO (t1
) && TYPE_TEMPLATE_INFO (t2
)
1525 && (TYPE_TI_TEMPLATE (t1
) == TYPE_TI_TEMPLATE (t2
)
1526 || TREE_CODE (t1
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
1527 && comp_template_args (TYPE_TI_ARGS (t1
), TYPE_TI_ARGS (t2
)))
1530 if ((strict
& COMPARE_BASE
) && DERIVED_FROM_P (t1
, t2
))
1532 else if ((strict
& COMPARE_DERIVED
) && DERIVED_FROM_P (t2
, t1
))
1538 if (!comptypes (TYPE_OFFSET_BASETYPE (t1
), TYPE_OFFSET_BASETYPE (t2
),
1539 strict
& ~COMPARE_REDECLARATION
))
1541 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1545 case REFERENCE_TYPE
:
1546 if (TYPE_REF_IS_RVALUE (t1
) != TYPE_REF_IS_RVALUE (t2
))
1548 /* fall through to checks for pointer types */
1552 if (TYPE_MODE (t1
) != TYPE_MODE (t2
)
1553 || !same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1559 /* Exception specs and memfn_rquals were checked above. */
1560 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1562 if (!compparms (TYPE_ARG_TYPES (t1
), TYPE_ARG_TYPES (t2
)))
1567 /* Target types must match incl. qualifiers. */
1568 if (!comp_array_types (t1
, t2
, ((strict
& COMPARE_REDECLARATION
)
1569 ? bounds_either
: bounds_none
),
1574 case TEMPLATE_TYPE_PARM
:
1575 /* If T1 and T2 don't have the same relative position in their
1576 template parameters set, they can't be equal. */
1577 if (!comp_template_parms_position (t1
, t2
))
1579 /* If T1 and T2 don't represent the same class template deduction,
1580 they aren't equal. */
1581 if (!cp_tree_equal (CLASS_PLACEHOLDER_TEMPLATE (t1
),
1582 CLASS_PLACEHOLDER_TEMPLATE (t2
)))
1584 /* Constrained 'auto's are distinct from parms that don't have the same
1586 if (!equivalent_placeholder_constraints (t1
, t2
))
1591 if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1
),
1592 TYPENAME_TYPE_FULLNAME (t2
)))
1594 /* Qualifiers don't matter on scopes. */
1595 if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1
),
1600 case UNBOUND_CLASS_TEMPLATE
:
1601 if (!cp_tree_equal (TYPE_IDENTIFIER (t1
), TYPE_IDENTIFIER (t2
)))
1603 if (!same_type_p (TYPE_CONTEXT (t1
), TYPE_CONTEXT (t2
)))
1608 if (!same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1613 if (gnu_vector_type_p (t1
) != gnu_vector_type_p (t2
)
1614 || maybe_ne (TYPE_VECTOR_SUBPARTS (t1
), TYPE_VECTOR_SUBPARTS (t2
))
1615 || !same_type_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
1619 case TYPE_PACK_EXPANSION
:
1620 return (same_type_p (PACK_EXPANSION_PATTERN (t1
),
1621 PACK_EXPANSION_PATTERN (t2
))
1622 && comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1
),
1623 PACK_EXPANSION_EXTRA_ARGS (t2
)));
1626 if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1
)
1627 != DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2
))
1629 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t1
) != DECLTYPE_FOR_LAMBDA_CAPTURE (t2
))
1631 if (DECLTYPE_FOR_LAMBDA_PROXY (t1
) != DECLTYPE_FOR_LAMBDA_PROXY (t2
))
1633 if (!cp_tree_equal (DECLTYPE_TYPE_EXPR (t1
), DECLTYPE_TYPE_EXPR (t2
)))
1638 if (TRAIT_TYPE_KIND (t1
) != TRAIT_TYPE_KIND (t2
))
1640 if (!cp_tree_equal (TRAIT_TYPE_TYPE1 (t1
), TRAIT_TYPE_TYPE1 (t2
))
1641 || !cp_tree_equal (TRAIT_TYPE_TYPE2 (t1
), TRAIT_TYPE_TYPE2 (t2
)))
1646 if (!cp_tree_equal (TYPEOF_TYPE_EXPR (t1
), TYPEOF_TYPE_EXPR (t2
)))
1654 /* If we get here, we know that from a target independent POV the
1655 types are the same. Make sure the target attributes are also
1657 if (!comp_type_attributes (t1
, t2
))
1661 if (comparing_dependent_aliases
1662 && (typedef_variant_p (t1
) || typedef_variant_p (t2
)))
1664 tree dep1
= dependent_opaque_alias_p (t1
) ? t1
: NULL_TREE
;
1665 tree dep2
= dependent_opaque_alias_p (t2
) ? t2
: NULL_TREE
;
1668 || !comp_type_attributes (DECL_ATTRIBUTES (TYPE_NAME (dep1
)),
1669 DECL_ATTRIBUTES (TYPE_NAME (dep2
)))))
1672 /* Don't treat an alias template specialization with dependent
1673 arguments as equivalent to its underlying type when used as a
1674 template argument; we need them to be distinct so that we
1675 substitute into the specialization arguments at instantiation
1676 time. And aliases can't be equivalent without being ==, so
1677 we don't need to look any deeper. */
1678 ++processing_template_decl
;
1679 dep1
= dependent_alias_template_spec_p (t1
, nt_transparent
);
1680 dep2
= dependent_alias_template_spec_p (t2
, nt_transparent
);
1681 --processing_template_decl
;
1682 if ((dep1
|| dep2
) && dep1
!= dep2
)
1689 /* Return true if T1 and T2 are related as allowed by STRICT. STRICT
1690 is a bitwise-or of the COMPARE_* flags. */
1693 comptypes (tree t1
, tree t2
, int strict
)
1695 gcc_checking_assert (t1
&& t2
);
1697 /* TYPE_ARGUMENT_PACKS are not really types. */
1698 gcc_checking_assert (TREE_CODE (t1
) != TYPE_ARGUMENT_PACK
1699 && TREE_CODE (t2
) != TYPE_ARGUMENT_PACK
);
1704 /* Suppress errors caused by previously reported errors. */
1705 if (t1
== error_mark_node
|| t2
== error_mark_node
)
1708 if (strict
== COMPARE_STRICT
)
1710 if (TYPE_STRUCTURAL_EQUALITY_P (t1
) || TYPE_STRUCTURAL_EQUALITY_P (t2
))
1711 /* At least one of the types requires structural equality, so
1712 perform a deep check. */
1713 return structural_comptypes (t1
, t2
, strict
);
1715 if (flag_checking
&& param_use_canonical_types
)
1717 bool result
= structural_comptypes (t1
, t2
, strict
);
1719 if (result
&& TYPE_CANONICAL (t1
) != TYPE_CANONICAL (t2
))
1720 /* The two types are structurally equivalent, but their
1721 canonical types were different. This is a failure of the
1722 canonical type propagation code.*/
1724 ("canonical types differ for identical types %qT and %qT",
1726 else if (!result
&& TYPE_CANONICAL (t1
) == TYPE_CANONICAL (t2
))
1727 /* Two types are structurally different, but the canonical
1728 types are the same. This means we were over-eager in
1729 assigning canonical types. */
1731 ("same canonical type node for different types %qT and %qT",
1736 if (!flag_checking
&& param_use_canonical_types
)
1737 return TYPE_CANONICAL (t1
) == TYPE_CANONICAL (t2
);
1739 return structural_comptypes (t1
, t2
, strict
);
1741 else if (strict
== COMPARE_STRUCTURAL
)
1742 return structural_comptypes (t1
, t2
, COMPARE_STRICT
);
1744 return structural_comptypes (t1
, t2
, strict
);
1747 /* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
1748 top-level qualifiers. */
1751 same_type_ignoring_top_level_qualifiers_p (tree type1
, tree type2
)
1753 if (type1
== error_mark_node
|| type2
== error_mark_node
)
1758 type1
= cp_build_qualified_type (type1
, TYPE_UNQUALIFIED
);
1759 type2
= cp_build_qualified_type (type2
, TYPE_UNQUALIFIED
);
1760 return same_type_p (type1
, type2
);
1763 /* Returns nonzero iff TYPE1 and TYPE2 are similar, as per [conv.qual]. */
1766 similar_type_p (tree type1
, tree type2
)
1768 if (type1
== error_mark_node
|| type2
== error_mark_node
)
1771 /* Informally, two types are similar if, ignoring top-level cv-qualification:
1772 * they are the same type; or
1773 * they are both pointers, and the pointed-to types are similar; or
1774 * they are both pointers to member of the same class, and the types of
1775 the pointed-to members are similar; or
1776 * they are both arrays of the same size or both arrays of unknown bound,
1777 and the array element types are similar. */
1779 if (same_type_ignoring_top_level_qualifiers_p (type1
, type2
))
1782 if ((TYPE_PTR_P (type1
) && TYPE_PTR_P (type2
))
1783 || (TYPE_PTRDATAMEM_P (type1
) && TYPE_PTRDATAMEM_P (type2
))
1784 || (TREE_CODE (type1
) == ARRAY_TYPE
&& TREE_CODE (type2
) == ARRAY_TYPE
))
1785 return comp_ptr_ttypes_const (type1
, type2
, bounds_either
);
1790 /* Helper function for layout_compatible_type_p and
1791 is_corresponding_member_aggr. Advance to next members (NULL if
1792 no further ones) and return true if those members are still part of
1793 the common initial sequence. */
1796 next_common_initial_sequence (tree
&memb1
, tree
&memb2
)
1800 if (TREE_CODE (memb1
) != FIELD_DECL
1801 || (DECL_FIELD_IS_BASE (memb1
) && is_empty_field (memb1
)))
1803 memb1
= DECL_CHAIN (memb1
);
1806 if (DECL_FIELD_IS_BASE (memb1
))
1808 memb1
= TYPE_FIELDS (TREE_TYPE (memb1
));
1815 if (TREE_CODE (memb2
) != FIELD_DECL
1816 || (DECL_FIELD_IS_BASE (memb2
) && is_empty_field (memb2
)))
1818 memb2
= DECL_CHAIN (memb2
);
1821 if (DECL_FIELD_IS_BASE (memb2
))
1823 memb2
= TYPE_FIELDS (TREE_TYPE (memb2
));
1828 if (memb1
== NULL_TREE
&& memb2
== NULL_TREE
)
1830 if (memb1
== NULL_TREE
|| memb2
== NULL_TREE
)
1832 if (DECL_BIT_FIELD_TYPE (memb1
))
1834 if (!DECL_BIT_FIELD_TYPE (memb2
))
1836 if (!layout_compatible_type_p (DECL_BIT_FIELD_TYPE (memb1
),
1837 DECL_BIT_FIELD_TYPE (memb2
)))
1839 if (TYPE_PRECISION (TREE_TYPE (memb1
))
1840 != TYPE_PRECISION (TREE_TYPE (memb2
)))
1843 else if (DECL_BIT_FIELD_TYPE (memb2
))
1845 else if (!layout_compatible_type_p (TREE_TYPE (memb1
), TREE_TYPE (memb2
)))
1847 if ((!lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (memb1
)))
1848 != !lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (memb2
)))
1850 if (DECL_ALIGN (memb1
) != DECL_ALIGN (memb2
))
1852 if (!tree_int_cst_equal (bit_position (memb1
), bit_position (memb2
)))
1857 /* Return true if TYPE1 and TYPE2 are layout-compatible types. */
1860 layout_compatible_type_p (tree type1
, tree type2
)
1862 if (type1
== error_mark_node
|| type2
== error_mark_node
)
1866 if (TREE_CODE (type1
) != TREE_CODE (type2
))
1869 type1
= cp_build_qualified_type (type1
, TYPE_UNQUALIFIED
);
1870 type2
= cp_build_qualified_type (type2
, TYPE_UNQUALIFIED
);
1872 if (TREE_CODE (type1
) == ENUMERAL_TYPE
)
1873 return (tree_int_cst_equal (TYPE_SIZE (type1
), TYPE_SIZE (type2
))
1874 && same_type_p (finish_underlying_type (type1
),
1875 finish_underlying_type (type2
)));
1877 if (CLASS_TYPE_P (type1
)
1878 && std_layout_type_p (type1
)
1879 && std_layout_type_p (type2
)
1880 && tree_int_cst_equal (TYPE_SIZE (type1
), TYPE_SIZE (type2
)))
1882 tree field1
= TYPE_FIELDS (type1
);
1883 tree field2
= TYPE_FIELDS (type2
);
1884 if (TREE_CODE (type1
) == RECORD_TYPE
)
1888 if (!next_common_initial_sequence (field1
, field2
))
1890 if (field1
== NULL_TREE
)
1892 field1
= DECL_CHAIN (field1
);
1893 field2
= DECL_CHAIN (field2
);
1896 /* Otherwise both types must be union types.
1898 "Two standard-layout unions are layout-compatible if they have
1899 the same number of non-static data members and corresponding
1900 non-static data members (in any order) have layout-compatible
1902 but the code anticipates that bitfield vs. non-bitfield,
1903 different bitfield widths or presence/absence of
1904 [[no_unique_address]] should be checked as well. */
1905 auto_vec
<tree
, 16> vec
;
1906 unsigned int count
= 0;
1907 for (; field1
; field1
= DECL_CHAIN (field1
))
1908 if (TREE_CODE (field1
) == FIELD_DECL
)
1910 for (; field2
; field2
= DECL_CHAIN (field2
))
1911 if (TREE_CODE (field2
) == FIELD_DECL
)
1912 vec
.safe_push (field2
);
1913 /* Discussions on core lean towards treating multiple union fields
1914 of the same type as the same field, so this might need changing
1916 if (count
!= vec
.length ())
1918 for (field1
= TYPE_FIELDS (type1
); field1
; field1
= DECL_CHAIN (field1
))
1920 if (TREE_CODE (field1
) != FIELD_DECL
)
1923 tree t1
= DECL_BIT_FIELD_TYPE (field1
);
1924 if (t1
== NULL_TREE
)
1925 t1
= TREE_TYPE (field1
);
1926 FOR_EACH_VEC_ELT (vec
, j
, field2
)
1928 tree t2
= DECL_BIT_FIELD_TYPE (field2
);
1929 if (t2
== NULL_TREE
)
1930 t2
= TREE_TYPE (field2
);
1931 if (DECL_BIT_FIELD_TYPE (field1
))
1933 if (!DECL_BIT_FIELD_TYPE (field2
))
1935 if (TYPE_PRECISION (TREE_TYPE (field1
))
1936 != TYPE_PRECISION (TREE_TYPE (field2
)))
1939 else if (DECL_BIT_FIELD_TYPE (field2
))
1941 if (!layout_compatible_type_p (t1
, t2
))
1943 if ((!lookup_attribute ("no_unique_address",
1944 DECL_ATTRIBUTES (field1
)))
1945 != !lookup_attribute ("no_unique_address",
1946 DECL_ATTRIBUTES (field2
)))
1950 if (j
== vec
.length ())
1952 vec
.unordered_remove (j
);
1957 return same_type_p (type1
, type2
);
1960 /* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
1963 at_least_as_qualified_p (const_tree type1
, const_tree type2
)
1965 int q1
= cp_type_quals (type1
);
1966 int q2
= cp_type_quals (type2
);
1968 /* All qualifiers for TYPE2 must also appear in TYPE1. */
1969 return (q1
& q2
) == q2
;
1972 /* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
1973 more cv-qualified that TYPE1, and 0 otherwise. */
1976 comp_cv_qualification (int q1
, int q2
)
1981 if ((q1
& q2
) == q2
)
1983 else if ((q1
& q2
) == q1
)
1990 comp_cv_qualification (const_tree type1
, const_tree type2
)
1992 int q1
= cp_type_quals (type1
);
1993 int q2
= cp_type_quals (type2
);
1994 return comp_cv_qualification (q1
, q2
);
1997 /* Returns 1 if the cv-qualification signature of TYPE1 is a proper
1998 subset of the cv-qualification signature of TYPE2, and the types
1999 are similar. Returns -1 if the other way 'round, and 0 otherwise. */
2002 comp_cv_qual_signature (tree type1
, tree type2
)
2004 if (comp_ptr_ttypes_real (type2
, type1
, -1))
2006 else if (comp_ptr_ttypes_real (type1
, type2
, -1))
2012 /* Subroutines of `comptypes'. */
2014 /* Return true if two parameter type lists PARMS1 and PARMS2 are
2015 equivalent in the sense that functions with those parameter types
2016 can have equivalent types. The two lists must be equivalent,
2017 element by element. */
2020 compparms (const_tree parms1
, const_tree parms2
)
2024 /* An unspecified parmlist matches any specified parmlist
2025 whose argument types don't need default promotions. */
2027 for (t1
= parms1
, t2
= parms2
;
2029 t1
= TREE_CHAIN (t1
), t2
= TREE_CHAIN (t2
))
2031 /* If one parmlist is shorter than the other,
2032 they fail to match. */
2035 if (!same_type_p (TREE_VALUE (t1
), TREE_VALUE (t2
)))
2042 /* Process a sizeof or alignof expression where the operand is a type.
2043 STD_ALIGNOF indicates whether an alignof has C++11 (minimum alignment)
2044 or GNU (preferred alignment) semantics; it is ignored if OP is
2048 cxx_sizeof_or_alignof_type (location_t loc
, tree type
, enum tree_code op
,
2049 bool std_alignof
, bool complain
)
2051 gcc_assert (op
== SIZEOF_EXPR
|| op
== ALIGNOF_EXPR
);
2052 if (type
== error_mark_node
)
2053 return error_mark_node
;
2055 type
= non_reference (type
);
2056 if (TREE_CODE (type
) == METHOD_TYPE
)
2060 pedwarn (loc
, OPT_Wpointer_arith
,
2061 "invalid application of %qs to a member function",
2062 OVL_OP_INFO (false, op
)->name
);
2063 return size_one_node
;
2066 return error_mark_node
;
2068 else if (VOID_TYPE_P (type
) && std_alignof
)
2071 error_at (loc
, "invalid application of %qs to a void type",
2072 OVL_OP_INFO (false, op
)->name
);
2073 return error_mark_node
;
2076 bool dependent_p
= dependent_type_p (type
);
2078 complete_type (type
);
2080 /* VLA types will have a non-constant size. In the body of an
2081 uninstantiated template, we don't need to try to compute the
2082 value, because the sizeof expression is not an integral
2083 constant expression in that case. And, if we do try to
2084 compute the value, we'll likely end up with SAVE_EXPRs, which
2085 the template substitution machinery does not expect to see. */
2086 || (processing_template_decl
2087 && COMPLETE_TYPE_P (type
)
2088 && TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
))
2090 tree value
= build_min (op
, size_type_node
, type
);
2091 TREE_READONLY (value
) = 1;
2092 if (op
== ALIGNOF_EXPR
&& std_alignof
)
2093 ALIGNOF_EXPR_STD_P (value
) = true;
2094 SET_EXPR_LOCATION (value
, loc
);
2098 return c_sizeof_or_alignof_type (loc
, complete_type (type
),
2099 op
== SIZEOF_EXPR
, std_alignof
,
2103 /* Return the size of the type, without producing any warnings for
2104 types whose size cannot be taken. This routine should be used only
2105 in some other routine that has already produced a diagnostic about
2106 using the size of such a type. */
2108 cxx_sizeof_nowarn (tree type
)
2110 if (TREE_CODE (type
) == FUNCTION_TYPE
2111 || VOID_TYPE_P (type
)
2112 || TREE_CODE (type
) == ERROR_MARK
)
2113 return size_one_node
;
2114 else if (!COMPLETE_TYPE_P (type
))
2115 return size_zero_node
;
2117 return cxx_sizeof_or_alignof_type (input_location
, type
,
2118 SIZEOF_EXPR
, false, false);
2121 /* Process a sizeof expression where the operand is an expression. */
2124 cxx_sizeof_expr (location_t loc
, tree e
, tsubst_flags_t complain
)
2126 if (e
== error_mark_node
)
2127 return error_mark_node
;
2129 if (instantiation_dependent_uneval_expression_p (e
))
2131 e
= build_min (SIZEOF_EXPR
, size_type_node
, e
);
2132 TREE_SIDE_EFFECTS (e
) = 0;
2133 TREE_READONLY (e
) = 1;
2134 SET_EXPR_LOCATION (e
, loc
);
2139 location_t e_loc
= cp_expr_loc_or_loc (e
, loc
);
2140 STRIP_ANY_LOCATION_WRAPPER (e
);
2142 if (TREE_CODE (e
) == PARM_DECL
2143 && DECL_ARRAY_PARAMETER_P (e
)
2144 && (complain
& tf_warning
))
2146 auto_diagnostic_group d
;
2147 if (warning_at (e_loc
, OPT_Wsizeof_array_argument
,
2148 "%<sizeof%> on array function parameter %qE "
2149 "will return size of %qT", e
, TREE_TYPE (e
)))
2150 inform (DECL_SOURCE_LOCATION (e
), "declared here");
2153 e
= mark_type_use (e
);
2157 if (complain
& tf_error
)
2159 "invalid application of %<sizeof%> to a bit-field");
2161 return error_mark_node
;
2164 else if (is_overloaded_fn (e
))
2166 if (complain
& tf_error
)
2167 permerror (e_loc
, "ISO C++ forbids applying %<sizeof%> to "
2168 "an expression of function type");
2170 return error_mark_node
;
2173 else if (type_unknown_p (e
))
2175 if (complain
& tf_error
)
2176 cxx_incomplete_type_error (e_loc
, e
, TREE_TYPE (e
));
2178 return error_mark_node
;
2184 return cxx_sizeof_or_alignof_type (loc
, e
, SIZEOF_EXPR
, false,
2185 complain
& tf_error
);
2188 /* Implement the __alignof keyword: Return the minimum required
2189 alignment of E, measured in bytes. For VAR_DECL's and
2190 FIELD_DECL's return DECL_ALIGN (which can be set from an
2191 "aligned" __attribute__ specification). STD_ALIGNOF acts
2192 like in cxx_sizeof_or_alignof_type. */
2195 cxx_alignof_expr (location_t loc
, tree e
, bool std_alignof
,
2196 tsubst_flags_t complain
)
2200 if (e
== error_mark_node
)
2201 return error_mark_node
;
2203 if (processing_template_decl
)
2205 e
= build_min (ALIGNOF_EXPR
, size_type_node
, e
);
2206 TREE_SIDE_EFFECTS (e
) = 0;
2207 TREE_READONLY (e
) = 1;
2208 SET_EXPR_LOCATION (e
, loc
);
2209 ALIGNOF_EXPR_STD_P (e
) = std_alignof
;
2214 location_t e_loc
= cp_expr_loc_or_loc (e
, loc
);
2215 STRIP_ANY_LOCATION_WRAPPER (e
);
2217 e
= mark_type_use (e
);
2219 if (!verify_type_context (loc
, TCTX_ALIGNOF
, TREE_TYPE (e
),
2220 !(complain
& tf_error
)))
2222 if (!(complain
& tf_error
))
2223 return error_mark_node
;
2227 t
= size_int (DECL_ALIGN_UNIT (e
));
2228 else if (bitfield_p (e
))
2230 if (complain
& tf_error
)
2232 "invalid application of %<__alignof%> to a bit-field");
2234 return error_mark_node
;
2237 else if (TREE_CODE (e
) == COMPONENT_REF
2238 && TREE_CODE (TREE_OPERAND (e
, 1)) == FIELD_DECL
)
2239 t
= size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e
, 1)));
2240 else if (is_overloaded_fn (e
))
2242 if (complain
& tf_error
)
2243 permerror (e_loc
, "ISO C++ forbids applying %<__alignof%> to "
2244 "an expression of function type");
2246 return error_mark_node
;
2247 if (TREE_CODE (e
) == FUNCTION_DECL
)
2248 t
= size_int (DECL_ALIGN_UNIT (e
));
2252 else if (type_unknown_p (e
))
2254 if (complain
& tf_error
)
2255 cxx_incomplete_type_error (e_loc
, e
, TREE_TYPE (e
));
2257 return error_mark_node
;
2261 return cxx_sizeof_or_alignof_type (loc
, TREE_TYPE (e
),
2262 ALIGNOF_EXPR
, std_alignof
,
2263 complain
& tf_error
);
2265 return fold_convert_loc (loc
, size_type_node
, t
);
2268 /* Process a sizeof or alignof expression E with code OP where the operand
2269 is an expression. STD_ALIGNOF acts like in cxx_sizeof_or_alignof_type. */
2272 cxx_sizeof_or_alignof_expr (location_t loc
, tree e
, enum tree_code op
,
2273 bool std_alignof
, bool complain
)
2275 gcc_assert (op
== SIZEOF_EXPR
|| op
== ALIGNOF_EXPR
);
2276 if (op
== SIZEOF_EXPR
)
2277 return cxx_sizeof_expr (loc
, e
, complain
? tf_warning_or_error
: tf_none
);
2279 return cxx_alignof_expr (loc
, e
, std_alignof
,
2280 complain
? tf_warning_or_error
: tf_none
);
2283 /* Build a representation of an expression 'alignas(E).' Return the
2284 folded integer value of E if it is an integral constant expression
2285 that resolves to a valid alignment. If E depends on a template
2286 parameter, return a syntactic representation tree of kind
2287 ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
2288 expression is ill formed, or NULL_TREE if E is NULL_TREE. */
2291 cxx_alignas_expr (tree e
)
2293 if (e
== NULL_TREE
|| e
== error_mark_node
2294 || (!TYPE_P (e
) && !require_potential_rvalue_constant_expression (e
)))
2300 When the alignment-specifier is of the form
2301 alignas(type-id), it shall have the same effect as
2302 alignas(alignof(type-id)). */
2304 return cxx_sizeof_or_alignof_type (input_location
,
2306 /*std_alignof=*/true,
2309 /* If we reach this point, it means the alignas expression if of
2310 the form "alignas(assignment-expression)", so we should follow
2311 what is stated by [dcl.align]/2. */
2313 if (value_dependent_expression_p (e
))
2314 /* Leave value-dependent expression alone for now. */
2317 e
= instantiate_non_dependent_expr (e
);
2318 e
= mark_rvalue_use (e
);
2320 /* [dcl.align]/2 says:
2322 the assignment-expression shall be an integral constant
2325 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (e
)))
2327 error ("%<alignas%> argument has non-integral type %qT", TREE_TYPE (e
));
2328 return error_mark_node
;
2331 return cxx_constant_value (e
);
2335 /* EXPR is being used in a context that is not a function call.
2340 The expression can be used only as the left-hand operand of a
2341 member function call.
2343 [expr.mptr.operator]
2345 If the result of .* or ->* is a function, then that result can be
2346 used only as the operand for the function call operator ().
2348 by issuing an error message if appropriate. Returns true iff EXPR
2349 violates these rules. */
2352 invalid_nonstatic_memfn_p (location_t loc
, tree expr
, tsubst_flags_t complain
)
2354 if (expr
== NULL_TREE
)
2356 /* Don't enforce this in MS mode. */
2357 if (flag_ms_extensions
)
2359 if (is_overloaded_fn (expr
) && !really_overloaded_fn (expr
))
2360 expr
= get_first_fn (expr
);
2361 if (TREE_TYPE (expr
)
2362 && DECL_IOBJ_MEMBER_FUNCTION_P (expr
))
2364 if (complain
& tf_error
)
2368 auto_diagnostic_group d
;
2369 error_at (loc
, "invalid use of non-static member function %qD",
2371 inform (DECL_SOURCE_LOCATION (expr
), "declared here");
2374 error_at (loc
, "invalid use of non-static member function of "
2375 "type %qT", TREE_TYPE (expr
));
2382 /* If EXP is a reference to a bit-field, and the type of EXP does not
2383 match the declared type of the bit-field, return the declared type
2384 of the bit-field. Otherwise, return NULL_TREE. */
2387 is_bitfield_expr_with_lowered_type (const_tree exp
)
2389 switch (TREE_CODE (exp
))
2392 if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 1)
2393 ? TREE_OPERAND (exp
, 1)
2394 : TREE_OPERAND (exp
, 0)))
2396 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 2));
2399 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 1));
2403 case UNARY_PLUS_EXPR
:
2404 case PREDECREMENT_EXPR
:
2405 case PREINCREMENT_EXPR
:
2406 case POSTDECREMENT_EXPR
:
2407 case POSTINCREMENT_EXPR
:
2409 case NON_LVALUE_EXPR
:
2411 case CLEANUP_POINT_EXPR
:
2412 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 0));
2418 field
= TREE_OPERAND (exp
, 1);
2419 if (TREE_CODE (field
) != FIELD_DECL
|| !DECL_BIT_FIELD_TYPE (field
))
2421 if (same_type_ignoring_top_level_qualifiers_p
2422 (TREE_TYPE (exp
), DECL_BIT_FIELD_TYPE (field
)))
2424 return DECL_BIT_FIELD_TYPE (field
);
2428 if (DECL_HAS_VALUE_EXPR_P (exp
))
2429 return is_bitfield_expr_with_lowered_type (DECL_VALUE_EXPR
2430 (CONST_CAST_TREE (exp
)));
2433 case VIEW_CONVERT_EXPR
:
2434 if (location_wrapper_p (exp
))
2435 return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp
, 0));
2444 /* Like is_bitfield_with_lowered_type, except that if EXP is not a
2445 bitfield with a lowered type, the type of EXP is returned, rather
2449 unlowered_expr_type (const_tree exp
)
2452 tree etype
= TREE_TYPE (exp
);
2454 type
= is_bitfield_expr_with_lowered_type (exp
);
2456 type
= cp_build_qualified_type (type
, cp_type_quals (etype
));
2463 /* Perform the conversions in [expr] that apply when an lvalue appears
2464 in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
2465 function-to-pointer conversions. In addition, bitfield references are
2466 converted to their declared types. Note that this function does not perform
2467 the lvalue-to-rvalue conversion for class types. If you need that conversion
2468 for class types, then you probably need to use force_rvalue.
2470 Although the returned value is being used as an rvalue, this
2471 function does not wrap the returned expression in a
2472 NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
2473 that the return value is no longer an lvalue. */
2476 decay_conversion (tree exp
,
2477 tsubst_flags_t complain
,
2478 bool reject_builtin
/* = true */)
2481 enum tree_code code
;
2482 location_t loc
= cp_expr_loc_or_input_loc (exp
);
2484 type
= TREE_TYPE (exp
);
2485 if (type
== error_mark_node
)
2486 return error_mark_node
;
2488 exp
= resolve_nondeduced_context_or_error (exp
, complain
);
2490 code
= TREE_CODE (type
);
2492 if (error_operand_p (exp
))
2493 return error_mark_node
;
2495 if (NULLPTR_TYPE_P (type
) && !TREE_SIDE_EFFECTS (exp
))
2497 mark_rvalue_use (exp
, loc
, reject_builtin
);
2498 return nullptr_node
;
2501 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
2502 Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
2503 if (code
== VOID_TYPE
)
2505 if (complain
& tf_error
)
2506 error_at (loc
, "void value not ignored as it ought to be");
2507 return error_mark_node
;
2509 if (invalid_nonstatic_memfn_p (loc
, exp
, complain
))
2510 return error_mark_node
;
2511 if (code
== FUNCTION_TYPE
|| is_overloaded_fn (exp
))
2513 exp
= mark_lvalue_use (exp
);
2514 if (reject_builtin
&& reject_gcc_builtin (exp
, loc
))
2515 return error_mark_node
;
2516 return cp_build_addr_expr (exp
, complain
);
2518 if (code
== ARRAY_TYPE
)
2523 exp
= mark_lvalue_use (exp
);
2525 if (INDIRECT_REF_P (exp
))
2526 return build_nop (build_pointer_type (TREE_TYPE (type
)),
2527 TREE_OPERAND (exp
, 0));
2529 if (TREE_CODE (exp
) == COMPOUND_EXPR
)
2531 tree op1
= decay_conversion (TREE_OPERAND (exp
, 1), complain
);
2532 if (op1
== error_mark_node
)
2533 return error_mark_node
;
2534 return build2 (COMPOUND_EXPR
, TREE_TYPE (op1
),
2535 TREE_OPERAND (exp
, 0), op1
);
2538 if (!obvalue_p (exp
)
2539 && ! (TREE_CODE (exp
) == CONSTRUCTOR
&& TREE_STATIC (exp
)))
2541 if (complain
& tf_error
)
2542 error_at (loc
, "invalid use of non-lvalue array");
2543 return error_mark_node
;
2546 ptrtype
= build_pointer_type (TREE_TYPE (type
));
2550 if (!cxx_mark_addressable (exp
))
2551 return error_mark_node
;
2552 adr
= build_nop (ptrtype
, build_address (exp
));
2555 /* This way is better for a COMPONENT_REF since it can
2556 simplify the offset for a component. */
2557 adr
= cp_build_addr_expr (exp
, complain
);
2558 return cp_convert (ptrtype
, adr
, complain
);
2561 /* Otherwise, it's the lvalue-to-rvalue conversion. */
2562 exp
= mark_rvalue_use (exp
, loc
, reject_builtin
);
2564 /* If a bitfield is used in a context where integral promotion
2565 applies, then the caller is expected to have used
2566 default_conversion. That function promotes bitfields correctly
2567 before calling this function. At this point, if we have a
2568 bitfield referenced, we may assume that is not subject to
2569 promotion, and that, therefore, the type of the resulting rvalue
2570 is the declared type of the bitfield. */
2571 exp
= convert_bitfield_to_declared_type (exp
);
2573 /* We do not call rvalue() here because we do not want to wrap EXP
2574 in a NON_LVALUE_EXPR. */
2578 Non-class rvalues always have cv-unqualified types. */
2579 type
= TREE_TYPE (exp
);
2580 if (!CLASS_TYPE_P (type
) && cv_qualified_p (type
))
2581 exp
= build_nop (cv_unqualified (type
), exp
);
2583 if (!complete_type_or_maybe_complain (type
, exp
, complain
))
2584 return error_mark_node
;
2589 /* Perform preparatory conversions, as part of the "usual arithmetic
2590 conversions". In particular, as per [expr]:
2592 Whenever an lvalue expression appears as an operand of an
2593 operator that expects the rvalue for that operand, the
2594 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
2595 standard conversions are applied to convert the expression to an
2598 In addition, we perform integral promotions here, as those are
2599 applied to both operands to a binary operator before determining
2600 what additional conversions should apply. */
2603 cp_default_conversion (tree exp
, tsubst_flags_t complain
)
2605 /* Check for target-specific promotions. */
2606 tree promoted_type
= targetm
.promoted_type (TREE_TYPE (exp
));
2608 exp
= cp_convert (promoted_type
, exp
, complain
);
2609 /* Perform the integral promotions first so that bitfield
2610 expressions (which may promote to "int", even if the bitfield is
2611 declared "unsigned") are promoted correctly. */
2612 else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp
)))
2613 exp
= cp_perform_integral_promotions (exp
, complain
);
2614 /* Perform the other conversions. */
2615 exp
= decay_conversion (exp
, complain
);
2623 default_conversion (tree exp
)
2625 return cp_default_conversion (exp
, tf_warning_or_error
);
2628 /* EXPR is an expression with an integral or enumeration type.
2629 Perform the integral promotions in [conv.prom], and return the
2633 cp_perform_integral_promotions (tree expr
, tsubst_flags_t complain
)
2638 expr
= mark_rvalue_use (expr
);
2639 if (error_operand_p (expr
))
2640 return error_mark_node
;
2642 type
= TREE_TYPE (expr
);
2646 A prvalue for an integral bit-field (11.3.9) can be converted to a prvalue
2647 of type int if int can represent all the values of the bit-field;
2648 otherwise, it can be converted to unsigned int if unsigned int can
2649 represent all the values of the bit-field. If the bit-field is larger yet,
2650 no integral promotion applies to it. If the bit-field has an enumerated
2651 type, it is treated as any other value of that type for promotion
2653 tree bitfield_type
= is_bitfield_expr_with_lowered_type (expr
);
2655 && (TREE_CODE (bitfield_type
) == ENUMERAL_TYPE
2656 || TYPE_PRECISION (type
) > TYPE_PRECISION (integer_type_node
)))
2657 type
= bitfield_type
;
2659 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
2660 /* Scoped enums don't promote. */
2661 if (SCOPED_ENUM_P (type
))
2663 promoted_type
= type_promotes_to (type
);
2664 if (type
!= promoted_type
)
2665 expr
= cp_convert (promoted_type
, expr
, complain
);
2666 else if (bitfield_type
&& bitfield_type
!= type
)
2667 /* Prevent decay_conversion from converting to bitfield_type. */
2668 expr
= build_nop (type
, expr
);
2675 perform_integral_promotions (tree expr
)
2677 return cp_perform_integral_promotions (expr
, tf_warning_or_error
);
2680 /* Returns nonzero iff exp is a STRING_CST or the result of applying
2681 decay_conversion to one. */
2684 string_conv_p (const_tree totype
, const_tree exp
, int warn
)
2688 if (!TYPE_PTR_P (totype
))
2691 t
= TREE_TYPE (totype
);
2692 if (!same_type_p (t
, char_type_node
)
2693 && !same_type_p (t
, char8_type_node
)
2694 && !same_type_p (t
, char16_type_node
)
2695 && !same_type_p (t
, char32_type_node
)
2696 && !same_type_p (t
, wchar_type_node
))
2699 location_t loc
= EXPR_LOC_OR_LOC (exp
, input_location
);
2701 STRIP_ANY_LOCATION_WRAPPER (exp
);
2703 if (TREE_CODE (exp
) == STRING_CST
)
2705 /* Make sure that we don't try to convert between char and wide chars. */
2706 if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp
))), t
))
2711 /* Is this a string constant which has decayed to 'const char *'? */
2712 t
= build_pointer_type (cp_build_qualified_type (t
, TYPE_QUAL_CONST
));
2713 if (!same_type_p (TREE_TYPE (exp
), t
))
2716 if (TREE_CODE (exp
) != ADDR_EXPR
2717 || TREE_CODE (TREE_OPERAND (exp
, 0)) != STRING_CST
)
2722 if (cxx_dialect
>= cxx11
)
2723 pedwarn (loc
, OPT_Wwrite_strings
,
2724 "ISO C++ forbids converting a string constant to %qT",
2727 warning_at (loc
, OPT_Wwrite_strings
,
2728 "deprecated conversion from string constant to %qT",
2735 /* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
2736 can, for example, use as an lvalue. This code used to be in
2737 unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
2738 expressions, where we're dealing with aggregates. But now it's again only
2739 called from unary_complex_lvalue. The case (in particular) that led to
2740 this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
2744 rationalize_conditional_expr (enum tree_code code
, tree t
,
2745 tsubst_flags_t complain
)
2747 location_t loc
= cp_expr_loc_or_input_loc (t
);
2749 /* For MIN_EXPR or MAX_EXPR, fold-const.cc has arranged things so that
2750 the first operand is always the one to be used if both operands
2751 are equal, so we know what conditional expression this used to be. */
2752 if (TREE_CODE (t
) == MIN_EXPR
|| TREE_CODE (t
) == MAX_EXPR
)
2754 tree op0
= TREE_OPERAND (t
, 0);
2755 tree op1
= TREE_OPERAND (t
, 1);
2757 /* The following code is incorrect if either operand side-effects. */
2758 gcc_assert (!TREE_SIDE_EFFECTS (op0
)
2759 && !TREE_SIDE_EFFECTS (op1
));
2761 build_conditional_expr (loc
,
2762 build_x_binary_op (loc
,
2763 (TREE_CODE (t
) == MIN_EXPR
2764 ? LE_EXPR
: GE_EXPR
),
2765 op0
, TREE_CODE (op0
),
2766 op1
, TREE_CODE (op1
),
2770 cp_build_unary_op (code
, op0
, false, complain
),
2771 cp_build_unary_op (code
, op1
, false, complain
),
2775 tree op1
= TREE_OPERAND (t
, 1);
2776 if (TREE_CODE (op1
) != THROW_EXPR
)
2777 op1
= cp_build_unary_op (code
, op1
, false, complain
);
2778 tree op2
= TREE_OPERAND (t
, 2);
2779 if (TREE_CODE (op2
) != THROW_EXPR
)
2780 op2
= cp_build_unary_op (code
, op2
, false, complain
);
2783 build_conditional_expr (loc
, TREE_OPERAND (t
, 0), op1
, op2
, complain
);
2786 /* Given the TYPE of an anonymous union field inside T, return the
2787 FIELD_DECL for the field. If not found return NULL_TREE. Because
2788 anonymous unions can nest, we must also search all anonymous unions
2789 that are directly reachable. */
2792 lookup_anon_field (tree
, tree type
)
2796 type
= TYPE_MAIN_VARIANT (type
);
2797 field
= ANON_AGGR_TYPE_FIELD (type
);
2802 /* Build an expression representing OBJECT.MEMBER. OBJECT is an
2803 expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
2804 non-NULL, it indicates the path to the base used to name MEMBER.
2805 If PRESERVE_REFERENCE is true, the expression returned will have
2806 REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
2807 returned will have the type referred to by the reference.
2809 This function does not perform access control; that is either done
2810 earlier by the parser when the name of MEMBER is resolved to MEMBER
2811 itself, or later when overload resolution selects one of the
2812 functions indicated by MEMBER. */
2815 build_class_member_access_expr (cp_expr object
, tree member
,
2816 tree access_path
, bool preserve_reference
,
2817 tsubst_flags_t complain
)
2821 tree result
= NULL_TREE
;
2822 tree using_decl
= NULL_TREE
;
2824 if (error_operand_p (object
) || error_operand_p (member
))
2825 return error_mark_node
;
2827 gcc_assert (DECL_P (member
) || BASELINK_P (member
));
2831 The type of the first expression shall be "class object" (of a
2833 object_type
= TREE_TYPE (object
);
2834 if (!currently_open_class (object_type
)
2835 && !complete_type_or_maybe_complain (object_type
, object
, complain
))
2836 return error_mark_node
;
2837 if (!CLASS_TYPE_P (object_type
))
2839 if (complain
& tf_error
)
2841 if (INDIRECT_TYPE_P (object_type
)
2842 && CLASS_TYPE_P (TREE_TYPE (object_type
)))
2843 error ("request for member %qD in %qE, which is of pointer "
2844 "type %qT (maybe you meant to use %<->%> ?)",
2845 member
, object
.get_value (), object_type
);
2847 error ("request for member %qD in %qE, which is of non-class "
2848 "type %qT", member
, object
.get_value (), object_type
);
2850 return error_mark_node
;
2853 /* The standard does not seem to actually say that MEMBER must be a
2854 member of OBJECT_TYPE. However, that is clearly what is
2856 if (DECL_P (member
))
2858 member_scope
= DECL_CLASS_CONTEXT (member
);
2859 if (!mark_used (member
, complain
) && !(complain
& tf_error
))
2860 return error_mark_node
;
2862 if (TREE_UNAVAILABLE (member
))
2863 error_unavailable_use (member
, NULL_TREE
);
2864 else if (TREE_DEPRECATED (member
))
2865 warn_deprecated_use (member
, NULL_TREE
);
2868 member_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (member
));
2869 /* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
2870 presently be the anonymous union. Go outwards until we find a
2871 type related to OBJECT_TYPE. */
2872 while ((ANON_AGGR_TYPE_P (member_scope
) || UNSCOPED_ENUM_P (member_scope
))
2873 && !same_type_ignoring_top_level_qualifiers_p (member_scope
,
2875 member_scope
= TYPE_CONTEXT (member_scope
);
2876 if (!member_scope
|| !DERIVED_FROM_P (member_scope
, object_type
))
2878 if (complain
& tf_error
)
2880 if (TREE_CODE (member
) == FIELD_DECL
)
2881 error ("invalid use of non-static data member %qE", member
);
2883 error ("%qD is not a member of %qT", member
, object_type
);
2885 return error_mark_node
;
2888 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
2889 `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
2890 in the front end; only _DECLs and _REFs are lvalues in the back end. */
2891 if (tree temp
= unary_complex_lvalue (ADDR_EXPR
, object
))
2893 temp
= cp_build_fold_indirect_ref (temp
);
2894 if (!lvalue_p (object
) && lvalue_p (temp
))
2895 /* Preserve rvalueness. */
2900 /* In [expr.ref], there is an explicit list of the valid choices for
2901 MEMBER. We check for each of those cases here. */
2904 /* A static data member. */
2906 mark_exp_read (object
);
2908 if (tree wrap
= maybe_get_tls_wrapper_call (result
))
2909 /* Replace an evaluated use of the thread_local variable with
2910 a call to its wrapper. */
2913 /* If OBJECT has side-effects, they are supposed to occur. */
2914 if (TREE_SIDE_EFFECTS (object
))
2915 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
), object
, result
);
2917 else if (TREE_CODE (member
) == FIELD_DECL
)
2919 /* A non-static data member. */
2924 if (INDIRECT_REF_P (object
))
2926 integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object
, 0)));
2928 null_object_p
= false;
2930 /* Convert OBJECT to the type of MEMBER. */
2931 if (!same_type_p (TYPE_MAIN_VARIANT (object_type
),
2932 TYPE_MAIN_VARIANT (member_scope
)))
2937 /* We didn't complain above about a currently open class, but now we
2938 must: we don't know how to refer to a base member before layout is
2939 complete. But still don't complain in a template. */
2940 if (!cp_unevaluated_operand
2941 && !dependent_type_p (object_type
)
2942 && !complete_type_or_maybe_complain (object_type
, object
,
2944 return error_mark_node
;
2946 binfo
= lookup_base (access_path
? access_path
: object_type
,
2947 member_scope
, ba_unique
, &kind
, complain
);
2948 if (binfo
== error_mark_node
)
2949 return error_mark_node
;
2951 /* It is invalid to try to get to a virtual base of a
2952 NULL object. The most common cause is invalid use of
2954 if (null_object_p
&& kind
== bk_via_virtual
)
2956 if (complain
& tf_error
)
2958 error ("invalid access to non-static data member %qD in "
2959 "virtual base of NULL object", member
);
2961 return error_mark_node
;
2964 /* Convert to the base. */
2965 object
= build_base_path (PLUS_EXPR
, object
, binfo
,
2966 /*nonnull=*/1, complain
);
2967 /* If we found the base successfully then we should be able
2968 to convert to it successfully. */
2969 gcc_assert (object
!= error_mark_node
);
2972 /* If MEMBER is from an anonymous aggregate, we have converted
2973 OBJECT so that it refers to the class containing the
2974 anonymous union. Generate a reference to the anonymous union
2975 itself, and recur to find MEMBER. */
2976 if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member
))
2977 /* When this code is called from build_field_call, the
2978 object already has the type of the anonymous union.
2979 That is because the COMPONENT_REF was already
2980 constructed, and was then disassembled before calling
2981 build_field_call. After the function-call code is
2982 cleaned up, this waste can be eliminated. */
2983 && (!same_type_ignoring_top_level_qualifiers_p
2984 (TREE_TYPE (object
), DECL_CONTEXT (member
))))
2986 tree anonymous_union
;
2988 anonymous_union
= lookup_anon_field (TREE_TYPE (object
),
2989 DECL_CONTEXT (member
));
2990 object
= build_class_member_access_expr (object
,
2992 /*access_path=*/NULL_TREE
,
2997 /* Compute the type of the field, as described in [expr.ref]. */
2998 type_quals
= TYPE_UNQUALIFIED
;
2999 member_type
= TREE_TYPE (member
);
3000 if (!TYPE_REF_P (member_type
))
3002 type_quals
= (cp_type_quals (member_type
)
3003 | cp_type_quals (object_type
));
3005 /* A field is const (volatile) if the enclosing object, or the
3006 field itself, is const (volatile). But, a mutable field is
3007 not const, even within a const object. */
3008 if (DECL_MUTABLE_P (member
))
3009 type_quals
&= ~TYPE_QUAL_CONST
;
3010 member_type
= cp_build_qualified_type (member_type
, type_quals
);
3013 result
= build3_loc (input_location
, COMPONENT_REF
, member_type
,
3014 object
, member
, NULL_TREE
);
3016 /* Mark the expression const or volatile, as appropriate. Even
3017 though we've dealt with the type above, we still have to mark the
3018 expression itself. */
3019 if (type_quals
& TYPE_QUAL_CONST
)
3020 TREE_READONLY (result
) = 1;
3021 if (type_quals
& TYPE_QUAL_VOLATILE
)
3022 TREE_THIS_VOLATILE (result
) = 1;
3024 else if (BASELINK_P (member
))
3026 /* The member is a (possibly overloaded) member function. */
3030 /* If the MEMBER is exactly one static member function, then we
3031 know the type of the expression. Otherwise, we must wait
3032 until overload resolution has been performed. */
3033 functions
= BASELINK_FUNCTIONS (member
);
3034 if (TREE_CODE (functions
) == OVERLOAD
&& OVL_SINGLE_P (functions
))
3035 functions
= OVL_FIRST (functions
);
3036 if (TREE_CODE (functions
) == FUNCTION_DECL
3037 && DECL_STATIC_FUNCTION_P (functions
))
3038 type
= TREE_TYPE (functions
);
3040 type
= unknown_type_node
;
3041 /* Note that we do not convert OBJECT to the BASELINK_BINFO
3042 base. That will happen when the function is called. */
3043 result
= build3_loc (input_location
, COMPONENT_REF
, type
, object
, member
,
3046 else if (TREE_CODE (member
) == CONST_DECL
)
3048 /* The member is an enumerator. */
3050 /* If OBJECT has side-effects, they are supposed to occur. */
3051 if (TREE_SIDE_EFFECTS (object
))
3052 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
),
3055 else if ((using_decl
= strip_using_decl (member
)) != member
)
3056 result
= build_class_member_access_expr (object
,
3058 access_path
, preserve_reference
,
3062 if (complain
& tf_error
)
3063 error ("invalid use of %qD", member
);
3064 return error_mark_node
;
3067 if (!preserve_reference
)
3070 If E2 is declared to have type "reference to T", then ... the
3071 type of E1.E2 is T. */
3072 result
= convert_from_reference (result
);
3077 /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
3078 SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
3081 lookup_destructor (tree object
, tree scope
, tree dtor_name
,
3082 tsubst_flags_t complain
)
3084 tree object_type
= TREE_TYPE (object
);
3085 tree dtor_type
= TREE_OPERAND (dtor_name
, 0);
3088 /* We've already complained about this destructor. */
3089 if (dtor_type
== error_mark_node
)
3090 return error_mark_node
;
3092 if (scope
&& !check_dtor_name (scope
, dtor_type
))
3094 if (complain
& tf_error
)
3095 error ("qualified type %qT does not match destructor name ~%qT",
3097 return error_mark_node
;
3099 if (is_auto (dtor_type
))
3100 dtor_type
= object_type
;
3101 else if (identifier_p (dtor_type
))
3103 /* In a template, names we can't find a match for are still accepted
3104 destructor names, and we check them here. */
3105 if (check_dtor_name (object_type
, dtor_type
))
3106 dtor_type
= object_type
;
3109 if (complain
& tf_error
)
3110 error ("object type %qT does not match destructor name ~%qT",
3111 object_type
, dtor_type
);
3112 return error_mark_node
;
3116 else if (!DERIVED_FROM_P (dtor_type
, TYPE_MAIN_VARIANT (object_type
)))
3118 if (complain
& tf_error
)
3119 error ("the type being destroyed is %qT, but the destructor "
3120 "refers to %qT", TYPE_MAIN_VARIANT (object_type
), dtor_type
);
3121 return error_mark_node
;
3123 expr
= lookup_member (dtor_type
, complete_dtor_identifier
,
3124 /*protect=*/1, /*want_type=*/false,
3125 tf_warning_or_error
);
3128 if (complain
& tf_error
)
3129 cxx_incomplete_type_error (dtor_name
, dtor_type
);
3130 return error_mark_node
;
3132 expr
= (adjust_result_of_qualified_name_lookup
3133 (expr
, dtor_type
, object_type
));
3134 if (scope
== NULL_TREE
)
3135 /* We need to call adjust_result_of_qualified_name_lookup in case the
3136 destructor names a base class, but we unset BASELINK_QUALIFIED_P so
3137 that we still get virtual function binding. */
3138 BASELINK_QUALIFIED_P (expr
) = false;
3142 /* An expression of the form "A::template B" has been resolved to
3143 DECL. Issue a diagnostic if B is not a template or template
3147 check_template_keyword (tree decl
)
3149 /* The standard says:
3153 If a name prefixed by the keyword template is not a member
3154 template, the program is ill-formed.
3156 DR 228 removed the restriction that the template be a member
3159 DR 96, if accepted would add the further restriction that explicit
3160 template arguments must be provided if the template keyword is
3161 used, but, as of 2005-10-16, that DR is still in "drafting". If
3162 this DR is accepted, then the semantic checks here can be
3163 simplified, as the entity named must in fact be a template
3164 specialization, rather than, as at present, a set of overloaded
3165 functions containing at least one template function. */
3166 if (TREE_CODE (decl
) != TEMPLATE_DECL
3167 && TREE_CODE (decl
) != TEMPLATE_ID_EXPR
)
3171 if (DECL_USE_TEMPLATE (decl
)
3172 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
3175 permerror (input_location
, "%qD is not a template", decl
);
3177 else if (!is_overloaded_fn (decl
))
3178 permerror (input_location
, "%qD is not a template", decl
);
3183 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (decl
));
3184 !found
&& iter
; ++iter
)
3187 if (TREE_CODE (fn
) == TEMPLATE_DECL
3188 || TREE_CODE (fn
) == TEMPLATE_ID_EXPR
3189 || (TREE_CODE (fn
) == FUNCTION_DECL
3190 && DECL_USE_TEMPLATE (fn
)
3191 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn
))))
3195 permerror (input_location
, "%qD is not a template", decl
);
3200 /* Record that an access failure occurred on BASETYPE_PATH attempting
3201 to access DECL, where DIAG_DECL should be used for diagnostics. */
3204 access_failure_info::record_access_failure (tree basetype_path
,
3205 tree decl
, tree diag_decl
)
3207 m_was_inaccessible
= true;
3208 m_basetype_path
= basetype_path
;
3210 m_diag_decl
= diag_decl
;
3213 /* If an access failure was recorded, then attempt to locate an
3214 accessor function for the pertinent field.
3215 Otherwise, return NULL_TREE. */
3218 access_failure_info::get_any_accessor (bool const_p
) const
3220 if (!was_inaccessible_p ())
3224 = locate_field_accessor (m_basetype_path
, m_diag_decl
, const_p
);
3228 /* The accessor must itself be accessible for it to be a reasonable
3230 if (!accessible_p (m_basetype_path
, accessor
, true))
3236 /* Add a fix-it hint to RICHLOC suggesting the use of ACCESSOR_DECL, by
3237 replacing the primary location in RICHLOC with "accessor()". */
3240 access_failure_info::add_fixit_hint (rich_location
*richloc
,
3244 pp_string (&pp
, IDENTIFIER_POINTER (DECL_NAME (accessor_decl
)));
3245 pp_string (&pp
, "()");
3246 richloc
->add_fixit_replace (pp_formatted_text (&pp
));
3249 /* If an access failure was recorded, then attempt to locate an
3250 accessor function for the pertinent field, and if one is
3251 available, add a note and fix-it hint suggesting using it. */
3254 access_failure_info::maybe_suggest_accessor (bool const_p
) const
3256 tree accessor
= get_any_accessor (const_p
);
3257 if (accessor
== NULL_TREE
)
3259 rich_location
richloc (line_table
, input_location
);
3260 add_fixit_hint (&richloc
, accessor
);
3261 inform (&richloc
, "field %q#D can be accessed via %q#D",
3262 m_diag_decl
, accessor
);
3265 /* Subroutine of finish_class_member_access_expr.
3266 Issue an error about NAME not being a member of ACCESS_PATH (or
3267 OBJECT_TYPE), potentially providing a fix-it hint for misspelled
3271 complain_about_unrecognized_member (tree access_path
, tree name
,
3274 /* Attempt to provide a hint about misspelled names. */
3275 tree guessed_id
= lookup_member_fuzzy (access_path
, name
,
3276 /*want_type=*/false);
3277 if (guessed_id
== NULL_TREE
)
3280 error ("%q#T has no member named %qE",
3281 TREE_CODE (access_path
) == TREE_BINFO
3282 ? TREE_TYPE (access_path
) : object_type
, name
);
3286 location_t bogus_component_loc
= input_location
;
3287 gcc_rich_location
rich_loc (bogus_component_loc
);
3289 /* Check that the guessed name is accessible along access_path. */
3290 access_failure_info afi
;
3291 lookup_member (access_path
, guessed_id
, /*protect=*/1,
3292 /*want_type=*/false, /*complain=*/false,
3294 if (afi
.was_inaccessible_p ())
3296 tree accessor
= afi
.get_any_accessor (TYPE_READONLY (object_type
));
3299 /* The guessed name isn't directly accessible, but can be accessed
3300 via an accessor member function. */
3301 afi
.add_fixit_hint (&rich_loc
, accessor
);
3302 error_at (&rich_loc
,
3303 "%q#T has no member named %qE;"
3304 " did you mean %q#D? (accessible via %q#D)",
3305 TREE_CODE (access_path
) == TREE_BINFO
3306 ? TREE_TYPE (access_path
) : object_type
,
3307 name
, afi
.get_diag_decl (), accessor
);
3311 /* The guessed name isn't directly accessible, and no accessor
3312 member function could be found. */
3313 auto_diagnostic_group d
;
3314 error_at (&rich_loc
,
3315 "%q#T has no member named %qE;"
3316 " did you mean %q#D? (not accessible from this context)",
3317 TREE_CODE (access_path
) == TREE_BINFO
3318 ? TREE_TYPE (access_path
) : object_type
,
3319 name
, afi
.get_diag_decl ());
3320 complain_about_access (afi
.get_decl (), afi
.get_diag_decl (),
3321 afi
.get_diag_decl (), false, ak_none
);
3326 /* The guessed name is directly accessible; suggest it. */
3327 rich_loc
.add_fixit_misspelled_id (bogus_component_loc
,
3329 error_at (&rich_loc
,
3330 "%q#T has no member named %qE;"
3331 " did you mean %qE?",
3332 TREE_CODE (access_path
) == TREE_BINFO
3333 ? TREE_TYPE (access_path
) : object_type
,
3338 /* This function is called by the parser to process a class member
3339 access expression of the form OBJECT.NAME. NAME is a node used by
3340 the parser to represent a name; it is not yet a DECL. It may,
3341 however, be a BASELINK where the BASELINK_FUNCTIONS is a
3342 TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
3343 there is no reason to do the lookup twice, so the parser keeps the
3344 BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
3345 be a template via the use of the "A::template B" syntax. */
3348 finish_class_member_access_expr (cp_expr object
, tree name
, bool template_p
,
3349 tsubst_flags_t complain
)
3354 tree access_path
= NULL_TREE
;
3355 tree orig_object
= object
;
3356 tree orig_name
= name
;
3358 if (object
== error_mark_node
|| name
== error_mark_node
)
3359 return error_mark_node
;
3361 /* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
3362 if (!objc_is_public (object
, name
))
3363 return error_mark_node
;
3365 object_type
= TREE_TYPE (object
);
3367 if (processing_template_decl
)
3369 if (/* If OBJECT is dependent, so is OBJECT.NAME. */
3370 type_dependent_object_expression_p (object
)
3371 /* If NAME is "f<args>", where either 'f' or 'args' is
3372 dependent, then the expression is dependent. */
3373 || (TREE_CODE (name
) == TEMPLATE_ID_EXPR
3374 && dependent_template_id_p (TREE_OPERAND (name
, 0),
3375 TREE_OPERAND (name
, 1)))
3376 /* If NAME is "T::X" where "T" is dependent, then the
3377 expression is dependent. */
3378 || (TREE_CODE (name
) == SCOPE_REF
3379 && TYPE_P (TREE_OPERAND (name
, 0))
3380 && dependent_scope_p (TREE_OPERAND (name
, 0)))
3381 /* If NAME is operator T where "T" is dependent, we can't
3382 lookup until we instantiate the T. */
3383 || (TREE_CODE (name
) == IDENTIFIER_NODE
3384 && IDENTIFIER_CONV_OP_P (name
)
3385 && dependent_type_p (TREE_TYPE (name
))))
3388 return build_min_nt_loc (UNKNOWN_LOCATION
, COMPONENT_REF
,
3389 orig_object
, orig_name
, NULL_TREE
);
3392 else if (c_dialect_objc ()
3393 && identifier_p (name
)
3394 && (expr
= objc_maybe_build_component_ref (object
, name
)))
3399 The type of the first expression shall be "class object" (of a
3401 if (!currently_open_class (object_type
)
3402 && !complete_type_or_maybe_complain (object_type
, object
, complain
))
3403 return error_mark_node
;
3404 if (!CLASS_TYPE_P (object_type
))
3406 if (complain
& tf_error
)
3408 if (INDIRECT_TYPE_P (object_type
)
3409 && CLASS_TYPE_P (TREE_TYPE (object_type
)))
3410 error ("request for member %qD in %qE, which is of pointer "
3411 "type %qT (maybe you meant to use %<->%> ?)",
3412 name
, object
.get_value (), object_type
);
3414 error ("request for member %qD in %qE, which is of non-class "
3415 "type %qT", name
, object
.get_value (), object_type
);
3417 return error_mark_node
;
3420 if (BASELINK_P (name
))
3421 /* A member function that has already been looked up. */
3425 bool is_template_id
= false;
3426 tree template_args
= NULL_TREE
;
3427 tree scope
= NULL_TREE
;
3429 access_path
= object_type
;
3431 if (TREE_CODE (name
) == SCOPE_REF
)
3433 /* A qualified name. The qualifying class or namespace `S'
3434 has already been looked up; it is either a TYPE or a
3436 scope
= TREE_OPERAND (name
, 0);
3437 name
= TREE_OPERAND (name
, 1);
3439 /* If SCOPE is a namespace, then the qualified name does not
3440 name a member of OBJECT_TYPE. */
3441 if (TREE_CODE (scope
) == NAMESPACE_DECL
)
3443 if (complain
& tf_error
)
3444 error ("%<%D::%D%> is not a member of %qT",
3445 scope
, name
, object_type
);
3446 return error_mark_node
;
3450 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
3452 is_template_id
= true;
3453 template_args
= TREE_OPERAND (name
, 1);
3454 name
= TREE_OPERAND (name
, 0);
3456 if (!identifier_p (name
))
3457 name
= OVL_NAME (name
);
3462 if (TREE_CODE (scope
) == ENUMERAL_TYPE
)
3464 gcc_assert (!is_template_id
);
3465 /* Looking up a member enumerator (c++/56793). */
3466 if (!TYPE_CLASS_SCOPE_P (scope
)
3467 || !DERIVED_FROM_P (TYPE_CONTEXT (scope
), object_type
))
3469 if (complain
& tf_error
)
3470 error ("%<%D::%D%> is not a member of %qT",
3471 scope
, name
, object_type
);
3472 return error_mark_node
;
3474 tree val
= lookup_enumerator (scope
, name
);
3477 if (complain
& tf_error
)
3478 error ("%qD is not a member of %qD",
3480 return error_mark_node
;
3483 if (TREE_SIDE_EFFECTS (object
))
3484 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (val
), object
, val
);
3488 gcc_assert (CLASS_TYPE_P (scope
));
3489 gcc_assert (identifier_p (name
) || TREE_CODE (name
) == BIT_NOT_EXPR
);
3491 if (constructor_name_p (name
, scope
))
3493 if (complain
& tf_error
)
3494 error ("cannot call constructor %<%T::%D%> directly",
3496 return error_mark_node
;
3499 /* NAME may refer to a static data member, in which case there is
3500 one copy of the data member that is shared by all the objects of
3501 the class. So NAME can be unambiguously referred to even if
3502 there are multiple indirect base classes containing NAME. */
3503 const base_access ba
= [scope
, name
] ()
3505 if (identifier_p (name
))
3507 tree m
= lookup_member (scope
, name
, /*protect=*/0,
3508 /*want_type=*/false, tf_none
);
3509 if (!m
|| shared_member_p (m
))
3515 /* Find the base of OBJECT_TYPE corresponding to SCOPE. */
3516 access_path
= lookup_base (object_type
, scope
, ba
, NULL
, complain
);
3517 if (access_path
== error_mark_node
)
3518 return error_mark_node
;
3521 if (any_dependent_bases_p (object_type
))
3523 if (complain
& tf_error
)
3524 error ("%qT is not a base of %qT", scope
, object_type
);
3525 return error_mark_node
;
3529 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
3531 if (dependent_type_p (object_type
))
3532 /* The destructor isn't declared yet. */
3534 member
= lookup_destructor (object
, scope
, name
, complain
);
3538 /* Look up the member. */
3540 auto_diagnostic_group d
;
3541 access_failure_info afi
;
3542 if (processing_template_decl
)
3543 /* Even though this class member access expression is at this
3544 point not dependent, the member itself may be dependent, and
3545 we must not potentially push a access check for a dependent
3546 member onto TI_DEFERRED_ACCESS_CHECKS. So don't check access
3547 ahead of time here; we're going to redo this member lookup at
3548 instantiation time anyway. */
3549 push_deferring_access_checks (dk_no_check
);
3550 member
= lookup_member (access_path
, name
, /*protect=*/1,
3551 /*want_type=*/false, complain
,
3553 if (processing_template_decl
)
3554 pop_deferring_access_checks ();
3555 afi
.maybe_suggest_accessor (TYPE_READONLY (object_type
));
3557 if (member
== NULL_TREE
)
3559 if (dependentish_scope_p (object_type
))
3560 /* Try again at instantiation time. */
3562 if (complain
& tf_error
)
3563 complain_about_unrecognized_member (access_path
, name
,
3565 return error_mark_node
;
3567 if (member
== error_mark_node
)
3568 return error_mark_node
;
3570 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (member
)))
3571 /* Dependent type attributes on the decl mean that the TREE_TYPE is
3572 wrong, so don't use it. */
3574 if (TREE_CODE (member
) == USING_DECL
&& DECL_DEPENDENT_P (member
))
3580 tree templ
= member
;
3582 if (BASELINK_P (templ
))
3583 member
= lookup_template_function (templ
, template_args
);
3584 else if (variable_template_p (templ
))
3585 member
= (lookup_and_finish_template_variable
3586 (templ
, template_args
, complain
));
3589 if (complain
& tf_error
)
3590 error ("%qD is not a member template function", name
);
3591 return error_mark_node
;
3596 if (TREE_UNAVAILABLE (member
))
3597 error_unavailable_use (member
, NULL_TREE
);
3598 else if (TREE_DEPRECATED (member
))
3599 warn_deprecated_use (member
, NULL_TREE
);
3602 check_template_keyword (member
);
3604 expr
= build_class_member_access_expr (object
, member
, access_path
,
3605 /*preserve_reference=*/false,
3607 if (processing_template_decl
&& expr
!= error_mark_node
)
3609 if (BASELINK_P (member
))
3611 if (TREE_CODE (orig_name
) == SCOPE_REF
)
3612 BASELINK_QUALIFIED_P (member
) = 1;
3615 return build_min_non_dep (COMPONENT_REF
, expr
,
3616 orig_object
, orig_name
,
3623 /* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
3627 build_simple_component_ref (tree object
, tree member
)
3629 tree type
= cp_build_qualified_type (TREE_TYPE (member
),
3630 cp_type_quals (TREE_TYPE (object
)));
3631 return build3_loc (input_location
,
3632 COMPONENT_REF
, type
,
3633 object
, member
, NULL_TREE
);
3636 /* Return an expression for the MEMBER_NAME field in the internal
3637 representation of PTRMEM, a pointer-to-member function. (Each
3638 pointer-to-member function type gets its own RECORD_TYPE so it is
3639 more convenient to access the fields by name than by FIELD_DECL.)
3640 This routine converts the NAME to a FIELD_DECL and then creates the
3641 node for the complete expression. */
3644 build_ptrmemfunc_access_expr (tree ptrmem
, tree member_name
)
3649 if (TREE_CODE (ptrmem
) == CONSTRUCTOR
)
3651 for (auto &e
: CONSTRUCTOR_ELTS (ptrmem
))
3652 if (e
.index
&& DECL_P (e
.index
) && DECL_NAME (e
.index
) == member_name
)
3657 /* This code is a stripped down version of
3658 build_class_member_access_expr. It does not work to use that
3659 routine directly because it expects the object to be of class
3661 ptrmem_type
= TREE_TYPE (ptrmem
);
3662 gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type
));
3663 for (member
= TYPE_FIELDS (ptrmem_type
); member
;
3664 member
= DECL_CHAIN (member
))
3665 if (DECL_NAME (member
) == member_name
)
3667 return build_simple_component_ref (ptrmem
, member
);
3670 /* Return a TREE_LIST of namespace-scope overloads for the given operator,
3671 and for any other relevant operator. */
3674 op_unqualified_lookup (tree_code code
, bool is_assign
)
3676 tree lookups
= NULL_TREE
;
3678 if (cxx_dialect
>= cxx20
&& !is_assign
)
3680 if (code
== NE_EXPR
)
3682 /* != can get rewritten in terms of ==. */
3683 tree fnname
= ovl_op_identifier (false, EQ_EXPR
);
3684 if (tree fns
= lookup_name (fnname
, LOOK_where::BLOCK_NAMESPACE
))
3685 lookups
= tree_cons (fnname
, fns
, lookups
);
3687 else if (code
== GT_EXPR
|| code
== LE_EXPR
3688 || code
== LT_EXPR
|| code
== GE_EXPR
)
3690 /* These can get rewritten in terms of <=>. */
3691 tree fnname
= ovl_op_identifier (false, SPACESHIP_EXPR
);
3692 if (tree fns
= lookup_name (fnname
, LOOK_where::BLOCK_NAMESPACE
))
3693 lookups
= tree_cons (fnname
, fns
, lookups
);
3697 tree fnname
= ovl_op_identifier (is_assign
, code
);
3698 if (tree fns
= lookup_name (fnname
, LOOK_where::BLOCK_NAMESPACE
))
3699 lookups
= tree_cons (fnname
, fns
, lookups
);
3704 return build_tree_list (NULL_TREE
, NULL_TREE
);
3707 /* Create a DEPENDENT_OPERATOR_TYPE for a dependent operator expression of
3708 the given operator. LOOKUPS, if non-NULL, is the result of phase 1
3709 name lookup for the given operator. */
3712 build_dependent_operator_type (tree lookups
, tree_code code
, bool is_assign
)
3715 /* We're partially instantiating a dependent operator expression, and
3716 LOOKUPS is the result of phase 1 name lookup that we performed
3717 earlier at template definition time, so just reuse the corresponding
3718 DEPENDENT_OPERATOR_TYPE. */
3719 return TREE_TYPE (lookups
);
3721 /* Otherwise we're processing a dependent operator expression at template
3722 definition time, so perform phase 1 name lookup now. */
3723 lookups
= op_unqualified_lookup (code
, is_assign
);
3725 tree type
= cxx_make_type (DEPENDENT_OPERATOR_TYPE
);
3726 DEPENDENT_OPERATOR_TYPE_SAVED_LOOKUPS (type
) = lookups
;
3727 TREE_TYPE (lookups
) = type
;
3731 /* Given an expression PTR for a pointer, return an expression
3732 for the value pointed to.
3733 ERRORSTRING is the name of the operator to appear in error messages.
3735 This function may need to overload OPERATOR_FNNAME.
3736 Must also handle REFERENCE_TYPEs for C++. */
3739 build_x_indirect_ref (location_t loc
, tree expr
, ref_operator errorstring
,
3740 tree lookups
, tsubst_flags_t complain
)
3742 tree orig_expr
= expr
;
3744 tree overload
= NULL_TREE
;
3746 if (processing_template_decl
)
3748 /* Retain the type if we know the operand is a pointer. */
3749 if (TREE_TYPE (expr
) && INDIRECT_TYPE_P (TREE_TYPE (expr
)))
3751 if (expr
== current_class_ptr
3752 || (TREE_CODE (expr
) == NOP_EXPR
3753 && TREE_OPERAND (expr
, 0) == current_class_ptr
3754 && (same_type_ignoring_top_level_qualifiers_p
3755 (TREE_TYPE (expr
), TREE_TYPE (current_class_ptr
)))))
3756 return current_class_ref
;
3757 return build_min (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (expr
)), expr
);
3759 if (type_dependent_expression_p (expr
))
3761 expr
= build_min_nt_loc (loc
, INDIRECT_REF
, expr
);
3763 = build_dependent_operator_type (lookups
, INDIRECT_REF
, false);
3768 rval
= build_new_op (loc
, INDIRECT_REF
, LOOKUP_NORMAL
, expr
,
3769 NULL_TREE
, NULL_TREE
, lookups
,
3770 &overload
, complain
);
3772 rval
= cp_build_indirect_ref (loc
, expr
, errorstring
, complain
);
3774 if (processing_template_decl
&& rval
!= error_mark_node
)
3776 if (overload
!= NULL_TREE
)
3777 return (build_min_non_dep_op_overload
3778 (INDIRECT_REF
, rval
, overload
, orig_expr
));
3780 return build_min_non_dep (INDIRECT_REF
, rval
, orig_expr
);
3786 /* Like c-family strict_aliasing_warning, but don't warn for dependent
3787 types or expressions. */
3790 cp_strict_aliasing_warning (location_t loc
, tree type
, tree expr
)
3792 if (processing_template_decl
)
3796 if (dependent_type_p (type
) || type_dependent_expression_p (e
))
3799 return strict_aliasing_warning (loc
, type
, expr
);
3802 /* The implementation of the above, and of indirection implied by other
3803 constructs. If DO_FOLD is true, fold away INDIRECT_REF of ADDR_EXPR. */
3806 cp_build_indirect_ref_1 (location_t loc
, tree ptr
, ref_operator errorstring
,
3807 tsubst_flags_t complain
, bool do_fold
)
3811 /* RO_NULL should only be used with the folding entry points below, not
3812 cp_build_indirect_ref. */
3813 gcc_checking_assert (errorstring
!= RO_NULL
|| do_fold
);
3815 if (ptr
== current_class_ptr
3816 || (TREE_CODE (ptr
) == NOP_EXPR
3817 && TREE_OPERAND (ptr
, 0) == current_class_ptr
3818 && (same_type_ignoring_top_level_qualifiers_p
3819 (TREE_TYPE (ptr
), TREE_TYPE (current_class_ptr
)))))
3820 return current_class_ref
;
3822 pointer
= (TYPE_REF_P (TREE_TYPE (ptr
))
3823 ? ptr
: decay_conversion (ptr
, complain
));
3824 if (pointer
== error_mark_node
)
3825 return error_mark_node
;
3827 type
= TREE_TYPE (pointer
);
3829 if (INDIRECT_TYPE_P (type
))
3833 If the type of the expression is "pointer to T," the type
3834 of the result is "T." */
3835 tree t
= TREE_TYPE (type
);
3837 if ((CONVERT_EXPR_P (ptr
)
3838 || TREE_CODE (ptr
) == VIEW_CONVERT_EXPR
)
3839 && (!CLASS_TYPE_P (t
) || !CLASSTYPE_EMPTY_P (t
)))
3841 /* If a warning is issued, mark it to avoid duplicates from
3842 the backend. This only needs to be done at
3843 warn_strict_aliasing > 2. */
3844 if (warn_strict_aliasing
> 2
3845 && cp_strict_aliasing_warning (EXPR_LOCATION (ptr
),
3846 type
, TREE_OPERAND (ptr
, 0)))
3847 suppress_warning (ptr
, OPT_Wstrict_aliasing
);
3850 if (VOID_TYPE_P (t
))
3852 /* A pointer to incomplete type (other than cv void) can be
3853 dereferenced [expr.unary.op]/1 */
3854 if (complain
& tf_error
)
3855 error_at (loc
, "%qT is not a pointer-to-object type", type
);
3856 return error_mark_node
;
3858 else if (do_fold
&& TREE_CODE (pointer
) == ADDR_EXPR
3859 && same_type_p (t
, TREE_TYPE (TREE_OPERAND (pointer
, 0))))
3860 /* The POINTER was something like `&x'. We simplify `*&x' to
3862 return TREE_OPERAND (pointer
, 0);
3865 tree ref
= build1 (INDIRECT_REF
, t
, pointer
);
3867 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
3868 so that we get the proper error message if the result is used
3869 to assign to. Also, &* is supposed to be a no-op. */
3870 TREE_READONLY (ref
) = CP_TYPE_CONST_P (t
);
3871 TREE_THIS_VOLATILE (ref
) = CP_TYPE_VOLATILE_P (t
);
3872 TREE_SIDE_EFFECTS (ref
)
3873 = (TREE_THIS_VOLATILE (ref
) || TREE_SIDE_EFFECTS (pointer
));
3877 else if (!(complain
& tf_error
))
3878 /* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
3880 /* `pointer' won't be an error_mark_node if we were given a
3881 pointer to member, so it's cool to check for this here. */
3882 else if (TYPE_PTRMEM_P (type
))
3883 switch (errorstring
)
3885 case RO_ARRAY_INDEXING
:
3887 "invalid use of array indexing on pointer to member");
3890 error_at (loc
, "invalid use of unary %<*%> on pointer to member");
3892 case RO_IMPLICIT_CONVERSION
:
3893 error_at (loc
, "invalid use of implicit conversion on pointer "
3897 error_at (loc
, "left hand operand of %<->*%> must be a pointer to "
3898 "class, but is a pointer to member of type %qT", type
);
3903 else if (pointer
!= error_mark_node
)
3904 invalid_indirection_error (loc
, type
, errorstring
);
3906 return error_mark_node
;
3909 /* Entry point used by c-common, which expects folding. */
3912 build_indirect_ref (location_t loc
, tree ptr
, ref_operator errorstring
)
3914 return cp_build_indirect_ref_1 (loc
, ptr
, errorstring
,
3915 tf_warning_or_error
, true);
3918 /* Entry point used by internal indirection needs that don't correspond to any
3919 syntactic construct. */
3922 cp_build_fold_indirect_ref (tree pointer
)
3924 return cp_build_indirect_ref_1 (input_location
, pointer
, RO_NULL
,
3925 tf_warning_or_error
, true);
3928 /* Entry point used by indirection needs that correspond to some syntactic
3932 cp_build_indirect_ref (location_t loc
, tree ptr
, ref_operator errorstring
,
3933 tsubst_flags_t complain
)
3935 return cp_build_indirect_ref_1 (loc
, ptr
, errorstring
, complain
, false);
3938 /* This handles expressions of the form "a[i]", which denotes
3941 This is logically equivalent in C to *(a+i), but we may do it differently.
3942 If A is a variable or a member, we generate a primitive ARRAY_REF.
3943 This avoids forcing the array out of registers, and can work on
3944 arrays that are not lvalues (for example, members of structures returned
3947 If INDEX is of some user-defined type, it must be converted to
3948 integer type. Otherwise, to make a compatible PLUS_EXPR, it
3949 will inherit the type of the array, which will be some pointer type.
3951 LOC is the location to use in building the array reference. */
3954 cp_build_array_ref (location_t loc
, tree array
, tree idx
,
3955 tsubst_flags_t complain
)
3957 tree first
= NULL_TREE
;
3962 if (complain
& tf_error
)
3963 error_at (loc
, "subscript missing in array reference");
3964 return error_mark_node
;
3967 if (TREE_TYPE (array
) == error_mark_node
3968 || TREE_TYPE (idx
) == error_mark_node
)
3969 return error_mark_node
;
3971 /* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
3973 switch (TREE_CODE (array
))
3977 tree value
= cp_build_array_ref (loc
, TREE_OPERAND (array
, 1), idx
,
3979 ret
= build2 (COMPOUND_EXPR
, TREE_TYPE (value
),
3980 TREE_OPERAND (array
, 0), value
);
3981 SET_EXPR_LOCATION (ret
, loc
);
3986 ret
= build_conditional_expr
3987 (loc
, TREE_OPERAND (array
, 0),
3988 cp_build_array_ref (loc
, TREE_OPERAND (array
, 1), idx
,
3990 cp_build_array_ref (loc
, TREE_OPERAND (array
, 2), idx
,
3993 protected_set_expr_location (ret
, loc
);
4000 bool non_lvalue
= convert_vector_to_array_for_subscript (loc
, &array
, idx
);
4003 if (TREE_CODE (TREE_TYPE (idx
)) == ARRAY_TYPE
)
4005 std::swap (array
, idx
);
4006 if (flag_strong_eval_order
== 2 && TREE_SIDE_EFFECTS (array
))
4007 idx
= first
= save_expr (idx
);
4010 if (TREE_CODE (TREE_TYPE (array
)) == ARRAY_TYPE
)
4014 warn_array_subscript_with_type_char (loc
, idx
);
4016 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx
)))
4018 if (complain
& tf_error
)
4019 error_at (loc
, "array subscript is not an integer");
4020 return error_mark_node
;
4023 /* Apply integral promotions *after* noticing character types.
4024 (It is unclear why we do these promotions -- the standard
4025 does not say that we should. In fact, the natural thing would
4026 seem to be to convert IDX to ptrdiff_t; we're performing
4027 pointer arithmetic.) */
4028 idx
= cp_perform_integral_promotions (idx
, complain
);
4030 idx
= maybe_fold_non_dependent_expr (idx
, complain
);
4032 /* An array that is indexed by a non-constant
4033 cannot be stored in a register; we must be able to do
4034 address arithmetic on its address.
4035 Likewise an array of elements of variable size. */
4036 if (TREE_CODE (idx
) != INTEGER_CST
4037 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array
)))
4038 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array
))))
4041 if (!cxx_mark_addressable (array
, true))
4042 return error_mark_node
;
4045 /* An array that is indexed by a constant value which is not within
4046 the array bounds cannot be stored in a register either; because we
4047 would get a crash in store_bit_field/extract_bit_field when trying
4048 to access a non-existent part of the register. */
4049 if (TREE_CODE (idx
) == INTEGER_CST
4050 && TYPE_DOMAIN (TREE_TYPE (array
))
4051 && ! int_fits_type_p (idx
, TYPE_DOMAIN (TREE_TYPE (array
))))
4053 if (!cxx_mark_addressable (array
))
4054 return error_mark_node
;
4057 /* Note in C++ it is valid to subscript a `register' array, since
4058 it is valid to take the address of something with that
4059 storage specification. */
4063 while (TREE_CODE (foo
) == COMPONENT_REF
)
4064 foo
= TREE_OPERAND (foo
, 0);
4065 if (VAR_P (foo
) && DECL_REGISTER (foo
)
4066 && (complain
& tf_warning
))
4067 warning_at (loc
, OPT_Wextra
,
4068 "subscripting array declared %<register%>");
4071 type
= TREE_TYPE (TREE_TYPE (array
));
4072 rval
= build4 (ARRAY_REF
, type
, array
, idx
, NULL_TREE
, NULL_TREE
);
4073 /* Array ref is const/volatile if the array elements are
4074 or if the array is.. */
4075 TREE_READONLY (rval
)
4076 |= (CP_TYPE_CONST_P (type
) | TREE_READONLY (array
));
4077 TREE_SIDE_EFFECTS (rval
)
4078 |= (CP_TYPE_VOLATILE_P (type
) | TREE_SIDE_EFFECTS (array
));
4079 TREE_THIS_VOLATILE (rval
)
4080 |= (CP_TYPE_VOLATILE_P (type
) | TREE_THIS_VOLATILE (array
));
4081 ret
= require_complete_type (rval
, complain
);
4082 protected_set_expr_location (ret
, loc
);
4084 ret
= non_lvalue_loc (loc
, ret
);
4086 ret
= build2_loc (loc
, COMPOUND_EXPR
, TREE_TYPE (ret
), first
, ret
);
4091 tree ar
= cp_default_conversion (array
, complain
);
4092 tree ind
= cp_default_conversion (idx
, complain
);
4094 if (!first
&& flag_strong_eval_order
== 2 && TREE_SIDE_EFFECTS (ind
))
4095 ar
= first
= save_expr (ar
);
4097 /* Put the integer in IND to simplify error checking. */
4098 if (TREE_CODE (TREE_TYPE (ar
)) == INTEGER_TYPE
)
4099 std::swap (ar
, ind
);
4101 if (ar
== error_mark_node
|| ind
== error_mark_node
)
4102 return error_mark_node
;
4104 if (!TYPE_PTR_P (TREE_TYPE (ar
)))
4106 if (complain
& tf_error
)
4107 error_at (loc
, "subscripted value is neither array nor pointer");
4108 return error_mark_node
;
4110 if (TREE_CODE (TREE_TYPE (ind
)) != INTEGER_TYPE
)
4112 if (complain
& tf_error
)
4113 error_at (loc
, "array subscript is not an integer");
4114 return error_mark_node
;
4117 warn_array_subscript_with_type_char (loc
, idx
);
4119 ret
= cp_build_binary_op (input_location
, PLUS_EXPR
, ar
, ind
, complain
);
4121 ret
= build2_loc (loc
, COMPOUND_EXPR
, TREE_TYPE (ret
), first
, ret
);
4122 ret
= cp_build_indirect_ref (loc
, ret
, RO_ARRAY_INDEXING
, complain
);
4123 protected_set_expr_location (ret
, loc
);
4125 ret
= non_lvalue_loc (loc
, ret
);
4130 /* Entry point for Obj-C++. */
4133 build_array_ref (location_t loc
, tree array
, tree idx
)
4135 return cp_build_array_ref (loc
, array
, idx
, tf_warning_or_error
);
4138 /* Resolve a pointer to member function. INSTANCE is the object
4139 instance to use, if the member points to a virtual member.
4141 This used to avoid checking for virtual functions if basetype
4142 has no virtual functions, according to an earlier ANSI draft.
4143 With the final ISO C++ rules, such an optimization is
4144 incorrect: A pointer to a derived member can be static_cast
4145 to pointer-to-base-member, as long as the dynamic object
4146 later has the right member. So now we only do this optimization
4147 when we know the dynamic type of the object. */
4150 get_member_function_from_ptrfunc (tree
*instance_ptrptr
, tree function
,
4151 tsubst_flags_t complain
)
4153 if (TREE_CODE (function
) == OFFSET_REF
)
4154 function
= TREE_OPERAND (function
, 1);
4156 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function
)))
4158 tree idx
, delta
, e1
, e2
, e3
, vtbl
;
4160 tree fntype
= TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function
));
4161 tree basetype
= TYPE_METHOD_BASETYPE (TREE_TYPE (fntype
));
4163 tree instance_ptr
= *instance_ptrptr
;
4164 tree instance_save_expr
= 0;
4165 if (instance_ptr
== error_mark_node
)
4167 if (TREE_CODE (function
) == PTRMEM_CST
)
4169 /* Extracting the function address from a pmf is only
4170 allowed with -Wno-pmf-conversions. It only works for
4172 e1
= build_addr_func (PTRMEM_CST_MEMBER (function
), complain
);
4173 e1
= convert (fntype
, e1
);
4178 if (complain
& tf_error
)
4179 error ("object missing in use of %qE", function
);
4180 return error_mark_node
;
4184 /* True if we know that the dynamic type of the object doesn't have
4185 virtual functions, so we can assume the PFN field is a pointer. */
4186 nonvirtual
= (COMPLETE_TYPE_P (basetype
)
4187 && !TYPE_POLYMORPHIC_P (basetype
)
4188 && resolves_to_fixed_type_p (instance_ptr
, 0));
4190 /* If we don't really have an object (i.e. in an ill-formed
4191 conversion from PMF to pointer), we can't resolve virtual
4192 functions anyway. */
4193 if (!nonvirtual
&& is_dummy_object (instance_ptr
))
4196 /* Use save_expr even when instance_ptr doesn't have side-effects,
4197 unless it is a simple decl (save_expr won't do anything on
4198 constants), so that we don't ubsan instrument the expression
4199 multiple times. See PR116449. */
4200 if (TREE_SIDE_EFFECTS (instance_ptr
)
4201 || (!nonvirtual
&& !DECL_P (instance_ptr
)))
4203 instance_save_expr
= save_expr (instance_ptr
);
4204 if (instance_save_expr
== instance_ptr
)
4205 instance_save_expr
= NULL_TREE
;
4207 instance_ptr
= instance_save_expr
;
4210 /* See above comment. */
4211 if (TREE_SIDE_EFFECTS (function
)
4212 || (!nonvirtual
&& !DECL_P (function
)))
4213 function
= save_expr (function
);
4215 /* Start by extracting all the information from the PMF itself. */
4216 e3
= pfn_from_ptrmemfunc (function
);
4217 delta
= delta_from_ptrmemfunc (function
);
4218 idx
= build1 (NOP_EXPR
, vtable_index_type
, e3
);
4219 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION
)
4221 int flag_sanitize_save
;
4222 case ptrmemfunc_vbit_in_pfn
:
4223 e1
= cp_build_binary_op (input_location
,
4224 BIT_AND_EXPR
, idx
, integer_one_node
,
4226 idx
= cp_build_binary_op (input_location
,
4227 MINUS_EXPR
, idx
, integer_one_node
,
4229 if (idx
== error_mark_node
)
4230 return error_mark_node
;
4233 case ptrmemfunc_vbit_in_delta
:
4234 e1
= cp_build_binary_op (input_location
,
4235 BIT_AND_EXPR
, delta
, integer_one_node
,
4237 /* Don't instrument the RSHIFT_EXPR we're about to create because
4238 we're going to use DELTA number of times, and that wouldn't play
4239 well with SAVE_EXPRs therein. */
4240 flag_sanitize_save
= flag_sanitize
;
4242 delta
= cp_build_binary_op (input_location
,
4243 RSHIFT_EXPR
, delta
, integer_one_node
,
4245 flag_sanitize
= flag_sanitize_save
;
4246 if (delta
== error_mark_node
)
4247 return error_mark_node
;
4254 if (e1
== error_mark_node
)
4255 return error_mark_node
;
4257 /* Convert down to the right base before using the instance. A
4258 special case is that in a pointer to member of class C, C may
4259 be incomplete. In that case, the function will of course be
4260 a member of C, and no conversion is required. In fact,
4261 lookup_base will fail in that case, because incomplete
4262 classes do not have BINFOs. */
4263 if (!same_type_ignoring_top_level_qualifiers_p
4264 (basetype
, TREE_TYPE (TREE_TYPE (instance_ptr
))))
4266 basetype
= lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr
)),
4267 basetype
, ba_check
, NULL
, complain
);
4268 instance_ptr
= build_base_path (PLUS_EXPR
, instance_ptr
, basetype
,
4270 if (instance_ptr
== error_mark_node
)
4271 return error_mark_node
;
4273 /* ...and then the delta in the PMF. */
4274 instance_ptr
= fold_build_pointer_plus (instance_ptr
, delta
);
4276 /* Hand back the adjusted 'this' argument to our caller. */
4277 *instance_ptrptr
= instance_ptr
;
4280 /* Now just return the pointer. */
4283 /* Next extract the vtable pointer from the object. */
4284 vtbl
= build1 (NOP_EXPR
, build_pointer_type (vtbl_ptr_type_node
),
4286 vtbl
= cp_build_fold_indirect_ref (vtbl
);
4287 if (vtbl
== error_mark_node
)
4288 return error_mark_node
;
4290 /* Finally, extract the function pointer from the vtable. */
4291 e2
= fold_build_pointer_plus_loc (input_location
, vtbl
, idx
);
4292 e2
= cp_build_fold_indirect_ref (e2
);
4293 if (e2
== error_mark_node
)
4294 return error_mark_node
;
4295 TREE_CONSTANT (e2
) = 1;
4297 /* When using function descriptors, the address of the
4298 vtable entry is treated as a function pointer. */
4299 if (TARGET_VTABLE_USES_DESCRIPTORS
)
4300 e2
= build1 (NOP_EXPR
, TREE_TYPE (e2
),
4301 cp_build_addr_expr (e2
, complain
));
4303 e2
= fold_convert (TREE_TYPE (e3
), e2
);
4304 e1
= build_conditional_expr (input_location
, e1
, e2
, e3
, complain
);
4305 if (e1
== error_mark_node
)
4306 return error_mark_node
;
4308 /* Make sure this doesn't get evaluated first inside one of the
4309 branches of the COND_EXPR. */
4310 if (instance_save_expr
)
4311 e1
= build2 (COMPOUND_EXPR
, TREE_TYPE (e1
),
4312 instance_save_expr
, e1
);
4319 /* Used by the C-common bits. */
4321 build_function_call (location_t
/*loc*/,
4322 tree function
, tree params
)
4324 return cp_build_function_call (function
, params
, tf_warning_or_error
);
4327 /* Used by the C-common bits. */
4329 build_function_call_vec (location_t
/*loc*/, vec
<location_t
> /*arg_loc*/,
4330 tree function
, vec
<tree
, va_gc
> *params
,
4331 vec
<tree
, va_gc
> * /*origtypes*/, tree orig_function
)
4333 vec
<tree
, va_gc
> *orig_params
= params
;
4334 tree ret
= cp_build_function_call_vec (function
, ¶ms
,
4335 tf_warning_or_error
, orig_function
);
4337 /* cp_build_function_call_vec can reallocate PARAMS by adding
4338 default arguments. That should never happen here. Verify
4340 gcc_assert (params
== orig_params
);
4345 /* Build a function call using a tree list of arguments. */
4348 cp_build_function_call (tree function
, tree params
, tsubst_flags_t complain
)
4353 for (; params
!= NULL_TREE
; params
= TREE_CHAIN (params
))
4354 vec_safe_push (vec
, TREE_VALUE (params
));
4355 ret
= cp_build_function_call_vec (function
, &vec
, complain
);
4359 /* Build a function call using varargs. */
4362 cp_build_function_call_nary (tree function
, tsubst_flags_t complain
, ...)
4368 va_start (args
, complain
);
4369 for (t
= va_arg (args
, tree
); t
!= NULL_TREE
; t
= va_arg (args
, tree
))
4370 vec_safe_push (vec
, t
);
4372 ret
= cp_build_function_call_vec (function
, &vec
, complain
);
4376 /* C++ implementation of callback for use when checking param types. */
4379 cp_comp_parm_types (tree wanted_type
, tree actual_type
)
4381 if (TREE_CODE (wanted_type
) == POINTER_TYPE
4382 && TREE_CODE (actual_type
) == POINTER_TYPE
)
4383 return same_or_base_type_p (TREE_TYPE (wanted_type
),
4384 TREE_TYPE (actual_type
));
4388 /* Build a function call using a vector of arguments.
4389 If FUNCTION is the result of resolving an overloaded target built-in,
4390 ORIG_FNDECL is the original function decl, otherwise it is null.
4391 PARAMS may be NULL if there are no parameters. This changes the
4392 contents of PARAMS. */
4395 cp_build_function_call_vec (tree function
, vec
<tree
, va_gc
> **params
,
4396 tsubst_flags_t complain
, tree orig_fndecl
)
4398 tree fntype
, fndecl
;
4400 tree original
= function
;
4404 vec
<tree
, va_gc
> *allocated
= NULL
;
4407 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
4408 expressions, like those used for ObjC messenger dispatches. */
4409 if (params
!= NULL
&& !vec_safe_is_empty (*params
))
4410 function
= objc_rewrite_function_call (function
, (**params
)[0]);
4412 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4413 Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
4414 if (TREE_CODE (function
) == NOP_EXPR
4415 && TREE_TYPE (function
) == TREE_TYPE (TREE_OPERAND (function
, 0)))
4416 function
= TREE_OPERAND (function
, 0);
4418 if (TREE_CODE (function
) == FUNCTION_DECL
)
4420 if (!mark_used (function
, complain
))
4421 return error_mark_node
;
4424 /* Convert anything with function type to a pointer-to-function. */
4425 if (DECL_MAIN_P (function
))
4427 if (complain
& tf_error
)
4428 pedwarn (input_location
, OPT_Wpedantic
,
4429 "ISO C++ forbids calling %<::main%> from within program");
4431 return error_mark_node
;
4433 function
= build_addr_func (function
, complain
);
4439 function
= build_addr_func (function
, complain
);
4442 if (function
== error_mark_node
)
4443 return error_mark_node
;
4445 fntype
= TREE_TYPE (function
);
4447 if (TYPE_PTRMEMFUNC_P (fntype
))
4449 if (complain
& tf_error
)
4450 error ("must use %<.*%> or %<->*%> to call pointer-to-member "
4451 "function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
4452 original
, original
);
4453 return error_mark_node
;
4456 is_method
= (TYPE_PTR_P (fntype
)
4457 && TREE_CODE (TREE_TYPE (fntype
)) == METHOD_TYPE
);
4459 if (!(TYPE_PTRFN_P (fntype
)
4461 || TREE_CODE (function
) == TEMPLATE_ID_EXPR
))
4463 if (complain
& tf_error
)
4465 if (!flag_diagnostics_show_caret
)
4466 error_at (input_location
,
4467 "%qE cannot be used as a function", original
);
4468 else if (DECL_P (original
))
4469 error_at (input_location
,
4470 "%qD cannot be used as a function", original
);
4472 error_at (input_location
,
4473 "expression cannot be used as a function");
4476 return error_mark_node
;
4479 /* fntype now gets the type of function pointed to. */
4480 fntype
= TREE_TYPE (fntype
);
4481 parm_types
= TYPE_ARG_TYPES (fntype
);
4485 allocated
= make_tree_vector ();
4486 params
= &allocated
;
4489 nargs
= convert_arguments (parm_types
, params
, fndecl
, LOOKUP_NORMAL
,
4492 return error_mark_node
;
4494 argarray
= (*params
)->address ();
4496 /* Check for errors in format strings and inappropriately
4498 bool warned_p
= check_function_arguments (input_location
, fndecl
, fntype
,
4499 nargs
, argarray
, NULL
,
4500 cp_comp_parm_types
);
4502 ret
= build_cxx_call (function
, nargs
, argarray
, complain
, orig_fndecl
);
4506 tree c
= extract_call_expr (ret
);
4507 if (TREE_CODE (c
) == CALL_EXPR
)
4508 suppress_warning (c
, OPT_Wnonnull
);
4511 if (allocated
!= NULL
)
4512 release_tree_vector (allocated
);
4517 /* Subroutine of convert_arguments.
4518 Print an error message about a wrong number of arguments. */
4521 error_args_num (location_t loc
, tree fndecl
, bool too_many_p
)
4525 auto_diagnostic_group d
;
4526 if (TREE_CODE (TREE_TYPE (fndecl
)) == METHOD_TYPE
)
4528 if (DECL_NAME (fndecl
) == NULL_TREE
4529 || (DECL_NAME (fndecl
)
4530 == DECL_NAME (TYPE_NAME (DECL_CONTEXT (fndecl
)))))
4533 ? G_("too many arguments to constructor %q#D")
4534 : G_("too few arguments to constructor %q#D"),
4539 ? G_("too many arguments to member function %q#D")
4540 : G_("too few arguments to member function %q#D"),
4546 ? G_("too many arguments to function %q#D")
4547 : G_("too few arguments to function %q#D"),
4549 if (!DECL_IS_UNDECLARED_BUILTIN (fndecl
))
4550 inform (DECL_SOURCE_LOCATION (fndecl
), "declared here");
4554 if (c_dialect_objc () && objc_message_selector ())
4557 ? G_("too many arguments to method %q#D")
4558 : G_("too few arguments to method %q#D"),
4559 objc_message_selector ());
4561 error_at (loc
, too_many_p
? G_("too many arguments to function")
4562 : G_("too few arguments to function"));
4566 /* Convert the actual parameter expressions in the list VALUES to the
4567 types in the list TYPELIST. The converted expressions are stored
4568 back in the VALUES vector.
4569 If parmdecls is exhausted, or when an element has NULL as its type,
4570 perform the default conversions.
4572 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
4574 This is also where warnings about wrong number of args are generated.
4576 Returns the actual number of arguments processed (which might be less
4577 than the length of the vector), or -1 on error.
4579 In C++, unspecified trailing parameters can be filled in with their
4580 default arguments, if such were specified. Do so here. */
4583 convert_arguments (tree typelist
, vec
<tree
, va_gc
> **values
, tree fndecl
,
4584 int flags
, tsubst_flags_t complain
)
4589 /* Argument passing is always copy-initialization. */
4590 flags
|= LOOKUP_ONLYCONVERTING
;
4592 for (i
= 0, typetail
= typelist
;
4593 i
< vec_safe_length (*values
);
4596 tree type
= typetail
? TREE_VALUE (typetail
) : 0;
4597 tree val
= (**values
)[i
];
4599 if (val
== error_mark_node
|| type
== error_mark_node
)
4602 if (type
== void_type_node
)
4604 if (complain
& tf_error
)
4606 error_args_num (input_location
, fndecl
, /*too_many_p=*/true);
4613 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
4614 Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
4615 if (TREE_CODE (val
) == NOP_EXPR
4616 && TREE_TYPE (val
) == TREE_TYPE (TREE_OPERAND (val
, 0))
4617 && (type
== 0 || !TYPE_REF_P (type
)))
4618 val
= TREE_OPERAND (val
, 0);
4620 if (type
== 0 || !TYPE_REF_P (type
))
4622 if (TREE_CODE (TREE_TYPE (val
)) == ARRAY_TYPE
4623 || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (val
)))
4624 val
= decay_conversion (val
, complain
);
4627 if (val
== error_mark_node
)
4632 /* Formal parm type is specified by a function prototype. */
4635 if (!COMPLETE_TYPE_P (complete_type (type
)))
4637 if (complain
& tf_error
)
4639 location_t loc
= EXPR_LOC_OR_LOC (val
, input_location
);
4642 auto_diagnostic_group d
;
4644 "parameter %P of %qD has incomplete type %qT",
4646 inform (get_fndecl_argument_location (fndecl
, i
),
4650 error_at (loc
, "parameter %P has incomplete type %qT", i
,
4653 parmval
= error_mark_node
;
4657 parmval
= convert_for_initialization
4658 (NULL_TREE
, type
, val
, flags
,
4659 ICR_ARGPASS
, fndecl
, i
, complain
);
4660 parmval
= convert_for_arg_passing (type
, parmval
, complain
);
4663 if (parmval
== error_mark_node
)
4666 (**values
)[i
] = parmval
;
4670 int magic
= fndecl
? magic_varargs_p (fndecl
) : 0;
4673 /* Don't truncate excess precision to the semantic type. */
4674 if (magic
== 1 && TREE_CODE (val
) == EXCESS_PRECISION_EXPR
)
4675 val
= TREE_OPERAND (val
, 0);
4676 /* Don't do ellipsis conversion for __built_in_constant_p
4677 as this will result in spurious errors for non-trivial
4679 val
= require_complete_type (val
, complain
);
4682 val
= convert_arg_to_ellipsis (val
, complain
);
4684 (**values
)[i
] = val
;
4688 typetail
= TREE_CHAIN (typetail
);
4691 if (typetail
!= 0 && typetail
!= void_list_node
)
4693 /* See if there are default arguments that can be used. Because
4694 we hold default arguments in the FUNCTION_TYPE (which is so
4695 wrong), we can see default parameters here from deduced
4696 contexts (and via typeof) for indirect function calls.
4697 Fortunately we know whether we have a function decl to
4698 provide default arguments in a language conformant
4700 if (fndecl
&& TREE_PURPOSE (typetail
)
4701 && TREE_CODE (TREE_PURPOSE (typetail
)) != DEFERRED_PARSE
)
4703 for (; typetail
!= void_list_node
; ++i
)
4705 /* After DR777, with explicit template args we can end up with a
4706 default argument followed by no default argument. */
4707 if (!TREE_PURPOSE (typetail
))
4710 = convert_default_arg (TREE_VALUE (typetail
),
4711 TREE_PURPOSE (typetail
),
4712 fndecl
, i
, complain
);
4714 if (parmval
== error_mark_node
)
4717 vec_safe_push (*values
, parmval
);
4718 typetail
= TREE_CHAIN (typetail
);
4719 /* ends with `...'. */
4720 if (typetail
== NULL_TREE
)
4725 if (typetail
&& typetail
!= void_list_node
)
4727 if (complain
& tf_error
)
4728 error_args_num (input_location
, fndecl
, /*too_many_p=*/false);
4736 /* Build a binary-operation expression, after performing default
4737 conversions on the operands. CODE is the kind of expression to
4738 build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
4739 are the tree codes which correspond to ARG1 and ARG2 when issuing
4740 warnings about possibly misplaced parentheses. They may differ
4741 from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
4742 folding (e.g., if the parser sees "a | 1 + 1", it may call this
4743 routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
4744 To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
4745 ARG2_CODE as ERROR_MARK. */
4748 build_x_binary_op (const op_location_t
&loc
, enum tree_code code
, tree arg1
,
4749 enum tree_code arg1_code
, tree arg2
,
4750 enum tree_code arg2_code
, tree lookups
,
4751 tree
*overload_p
, tsubst_flags_t complain
)
4756 tree overload
= NULL_TREE
;
4761 if (processing_template_decl
)
4763 if (type_dependent_expression_p (arg1
)
4764 || type_dependent_expression_p (arg2
))
4766 expr
= build_min_nt_loc (loc
, code
, arg1
, arg2
);
4768 = build_dependent_operator_type (lookups
, code
, false);
4773 if (code
== DOTSTAR_EXPR
)
4774 expr
= build_m_component_ref (arg1
, arg2
, complain
);
4776 expr
= build_new_op (loc
, code
, LOOKUP_NORMAL
, arg1
, arg2
, NULL_TREE
,
4777 lookups
, &overload
, complain
);
4779 if (overload_p
!= NULL
)
4780 *overload_p
= overload
;
4782 /* Check for cases such as x+y<<z which users are likely to
4783 misinterpret. But don't warn about obj << x + y, since that is a
4784 common idiom for I/O. */
4785 if (warn_parentheses
4786 && (complain
& tf_warning
)
4787 && !processing_template_decl
4788 && !error_operand_p (arg1
)
4789 && !error_operand_p (arg2
)
4790 && (code
!= LSHIFT_EXPR
4791 || !CLASS_TYPE_P (TREE_TYPE (arg1
))))
4792 warn_about_parentheses (loc
, code
, arg1_code
, orig_arg1
,
4793 arg2_code
, orig_arg2
);
4795 if (processing_template_decl
&& expr
!= error_mark_node
)
4797 if (overload
!= NULL_TREE
)
4798 return (build_min_non_dep_op_overload
4799 (code
, expr
, overload
, orig_arg1
, orig_arg2
));
4801 return build_min_non_dep (code
, expr
, orig_arg1
, orig_arg2
);
4807 /* Build and return an ARRAY_REF expression. */
4810 build_x_array_ref (location_t loc
, tree arg1
, tree arg2
,
4811 tsubst_flags_t complain
)
4813 tree orig_arg1
= arg1
;
4814 tree orig_arg2
= arg2
;
4816 tree overload
= NULL_TREE
;
4818 if (processing_template_decl
)
4820 if (type_dependent_expression_p (arg1
)
4821 || type_dependent_expression_p (arg2
))
4822 return build_min_nt_loc (loc
, ARRAY_REF
, arg1
, arg2
,
4823 NULL_TREE
, NULL_TREE
);
4826 expr
= build_new_op (loc
, ARRAY_REF
, LOOKUP_NORMAL
, arg1
, arg2
,
4827 NULL_TREE
, NULL_TREE
, &overload
, complain
);
4829 if (processing_template_decl
&& expr
!= error_mark_node
)
4831 if (overload
!= NULL_TREE
)
4832 return (build_min_non_dep_op_overload
4833 (ARRAY_REF
, expr
, overload
, orig_arg1
, orig_arg2
));
4835 return build_min_non_dep (ARRAY_REF
, expr
, orig_arg1
, orig_arg2
,
4836 NULL_TREE
, NULL_TREE
);
4841 /* Build an OpenMP array section reference, creating an exact type for the
4842 resulting expression based on the element type and bounds if possible. If
4843 we have variable bounds, create an incomplete array type for the result
4847 build_omp_array_section (location_t loc
, tree array_expr
, tree index
,
4850 tree type
= TREE_TYPE (array_expr
);
4852 type
= non_reference (type
);
4854 tree sectype
, eltype
= TREE_TYPE (type
);
4856 /* It's not an array or pointer type. Just reuse the type of the
4857 original expression as the type of the array section (an error will be
4858 raised anyway, later). */
4859 if (eltype
== NULL_TREE
)
4860 sectype
= TREE_TYPE (array_expr
);
4863 tree idxtype
= NULL_TREE
;
4865 /* If we know the integer bounds, create an index type with exact
4866 low/high (or zero/length) bounds. Otherwise, create an incomplete
4867 array type. (This mostly only affects diagnostics.) */
4868 if (index
!= NULL_TREE
4869 && length
!= NULL_TREE
4870 && TREE_CODE (index
) == INTEGER_CST
4871 && TREE_CODE (length
) == INTEGER_CST
)
4873 tree low
= fold_convert (sizetype
, index
);
4874 tree high
= fold_convert (sizetype
, length
);
4875 high
= size_binop (PLUS_EXPR
, low
, high
);
4876 high
= size_binop (MINUS_EXPR
, high
, size_one_node
);
4877 idxtype
= build_range_type (sizetype
, low
, high
);
4879 else if ((index
== NULL_TREE
|| integer_zerop (index
))
4880 && length
!= NULL_TREE
4881 && TREE_CODE (length
) == INTEGER_CST
)
4882 idxtype
= build_index_type (length
);
4884 sectype
= build_array_type (eltype
, idxtype
);
4887 return build3_loc (loc
, OMP_ARRAY_SECTION
, sectype
, array_expr
, index
,
4891 /* Return whether OP is an expression of enum type cast to integer
4892 type. In C++ even unsigned enum types are cast to signed integer
4893 types. We do not want to issue warnings about comparisons between
4894 signed and unsigned types when one of the types is an enum type.
4895 Those warnings are always false positives in practice. */
4898 enum_cast_to_int (tree op
)
4900 if (CONVERT_EXPR_P (op
)
4901 && TREE_TYPE (op
) == integer_type_node
4902 && TREE_CODE (TREE_TYPE (TREE_OPERAND (op
, 0))) == ENUMERAL_TYPE
4903 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op
, 0))))
4906 /* The cast may have been pushed into a COND_EXPR. */
4907 if (TREE_CODE (op
) == COND_EXPR
)
4908 return (enum_cast_to_int (TREE_OPERAND (op
, 1))
4909 || enum_cast_to_int (TREE_OPERAND (op
, 2)));
4914 /* For the c-common bits. */
4916 build_binary_op (location_t location
, enum tree_code code
, tree op0
, tree op1
,
4919 return cp_build_binary_op (location
, code
, op0
, op1
, tf_warning_or_error
);
4922 /* Build a vector comparison of ARG0 and ARG1 using CODE opcode
4923 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
4926 build_vec_cmp (tree_code code
, tree type
,
4927 tree arg0
, tree arg1
)
4929 tree zero_vec
= build_zero_cst (type
);
4930 tree minus_one_vec
= build_minus_one_cst (type
);
4931 tree cmp_type
= truth_type_for (TREE_TYPE (arg0
));
4932 tree cmp
= build2 (code
, cmp_type
, arg0
, arg1
);
4933 return build3 (VEC_COND_EXPR
, type
, cmp
, minus_one_vec
, zero_vec
);
4936 /* Possibly warn about an address never being NULL. */
4939 warn_for_null_address (location_t location
, tree op
, tsubst_flags_t complain
)
4941 /* Prevent warnings issued for macro expansion. */
4943 || (complain
& tf_warning
) == 0
4944 || c_inhibit_evaluation_warnings
!= 0
4945 || from_macro_expansion_at (location
)
4946 || warning_suppressed_p (op
, OPT_Waddress
))
4949 tree cop
= fold_for_warn (op
);
4951 if (TREE_CODE (cop
) == NON_LVALUE_EXPR
)
4952 /* Unwrap the expression for C++ 98. */
4953 cop
= TREE_OPERAND (cop
, 0);
4955 if (TREE_CODE (cop
) == PTRMEM_CST
)
4957 /* The address of a nonstatic data member is never null. */
4958 warning_at (location
, OPT_Waddress
,
4959 "the address %qE will never be NULL",
4964 if (TREE_CODE (cop
) == NOP_EXPR
)
4966 /* Allow casts to intptr_t to suppress the warning. */
4967 tree type
= TREE_TYPE (cop
);
4968 if (TREE_CODE (type
) == INTEGER_TYPE
)
4974 auto_diagnostic_group d
;
4975 bool warned
= false;
4976 if (TREE_CODE (cop
) == ADDR_EXPR
)
4978 cop
= TREE_OPERAND (cop
, 0);
4980 /* Set to true in the loop below if OP dereferences its operand.
4981 In such a case the ultimate target need not be a decl for
4982 the null [in]equality test to be necessarily constant. */
4985 /* Get the outermost array or object, or member. */
4986 while (handled_component_p (cop
))
4988 if (TREE_CODE (cop
) == COMPONENT_REF
)
4990 /* Get the member (its address is never null). */
4991 cop
= TREE_OPERAND (cop
, 1);
4995 /* Get the outer array/object to refer to in the warning. */
4996 cop
= TREE_OPERAND (cop
, 0);
5000 if ((!deref
&& !decl_with_nonnull_addr_p (cop
))
5001 || from_macro_expansion_at (location
)
5002 || warning_suppressed_p (cop
, OPT_Waddress
))
5005 warned
= warning_at (location
, OPT_Waddress
,
5006 "the address of %qD will never be NULL", cop
);
5009 else if (TREE_CODE (cop
) == POINTER_PLUS_EXPR
)
5011 /* Adding zero to the null pointer is well-defined in C++. When
5012 the offset is unknown (i.e., not a constant) warn anyway since
5013 it's less likely that the pointer operand is null than not. */
5014 tree off
= TREE_OPERAND (cop
, 1);
5015 if (!integer_zerop (off
)
5016 && !warning_suppressed_p (cop
, OPT_Waddress
))
5018 tree base
= TREE_OPERAND (cop
, 0);
5020 if (TYPE_REF_P (TREE_TYPE (base
)))
5021 warning_at (location
, OPT_Waddress
, "the compiler can assume that "
5022 "the address of %qE will never be NULL", base
);
5024 warning_at (location
, OPT_Waddress
, "comparing the result of "
5025 "pointer addition %qE and NULL", cop
);
5029 else if (CONVERT_EXPR_P (op
)
5030 && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (op
, 0))))
5034 if (TREE_CODE (op
) == COMPONENT_REF
)
5035 op
= TREE_OPERAND (op
, 1);
5038 warned
= warning_at (location
, OPT_Waddress
,
5039 "the compiler can assume that the address of "
5040 "%qD will never be NULL", op
);
5043 if (warned
&& DECL_P (op
))
5044 inform (DECL_SOURCE_LOCATION (op
), "%qD declared here", op
);
5047 /* Warn about [expr.arith.conv]/2: If one operand is of enumeration type and
5048 the other operand is of a different enumeration type or a floating-point
5049 type, this behavior is deprecated ([depr.arith.conv.enum]). CODE is the
5050 code of the binary operation, TYPE0 and TYPE1 are the types of the operands,
5051 and LOC is the location for the whole binary expression.
5052 For C++26 this is ill-formed rather than deprecated.
5053 Return true for SFINAE errors.
5054 TODO: Consider combining this with -Wenum-compare in build_new_op_1. */
5057 do_warn_enum_conversions (location_t loc
, enum tree_code code
, tree type0
,
5058 tree type1
, tsubst_flags_t complain
)
5060 if (TREE_CODE (type0
) == ENUMERAL_TYPE
5061 && TREE_CODE (type1
) == ENUMERAL_TYPE
5062 && TYPE_MAIN_VARIANT (type0
) != TYPE_MAIN_VARIANT (type1
))
5064 if (cxx_dialect
>= cxx26
)
5066 if ((complain
& tf_warning_or_error
) == 0)
5069 else if ((complain
& tf_warning
) == 0)
5071 /* In C++20, -Wdeprecated-enum-enum-conversion is on by default.
5072 Otherwise, warn if -Wenum-conversion is on. */
5074 if (warn_deprecated_enum_enum_conv
)
5075 opt
= OPT_Wdeprecated_enum_enum_conversion
;
5076 else if (warn_enum_conversion
)
5077 opt
= OPT_Wenum_conversion
;
5089 /* Comparisons are handled by -Wenum-compare. */
5091 case SPACESHIP_EXPR
:
5092 /* This is invalid, don't warn. */
5097 if (cxx_dialect
>= cxx26
)
5098 pedwarn (loc
, opt
, "bitwise operation between different "
5099 "enumeration types %qT and %qT", type0
, type1
);
5101 warning_at (loc
, opt
, "bitwise operation between different "
5102 "enumeration types %qT and %qT is deprecated",
5106 if (cxx_dialect
>= cxx26
)
5107 pedwarn (loc
, opt
, "arithmetic between different enumeration "
5108 "types %qT and %qT", type0
, type1
);
5110 warning_at (loc
, opt
, "arithmetic between different enumeration "
5111 "types %qT and %qT is deprecated", type0
, type1
);
5115 else if ((TREE_CODE (type0
) == ENUMERAL_TYPE
5116 && SCALAR_FLOAT_TYPE_P (type1
))
5117 || (SCALAR_FLOAT_TYPE_P (type0
)
5118 && TREE_CODE (type1
) == ENUMERAL_TYPE
))
5120 if (cxx_dialect
>= cxx26
)
5122 if ((complain
& tf_warning_or_error
) == 0)
5125 else if ((complain
& tf_warning
) == 0)
5127 const bool enum_first_p
= TREE_CODE (type0
) == ENUMERAL_TYPE
;
5128 /* In C++20, -Wdeprecated-enum-float-conversion is on by default.
5129 Otherwise, warn if -Wenum-conversion is on. */
5131 if (warn_deprecated_enum_float_conv
)
5132 opt
= OPT_Wdeprecated_enum_float_conversion
;
5133 else if (warn_enum_conversion
)
5134 opt
= OPT_Wenum_conversion
;
5146 if (enum_first_p
&& cxx_dialect
>= cxx26
)
5147 pedwarn (loc
, opt
, "comparison of enumeration type %qT with "
5148 "floating-point type %qT", type0
, type1
);
5149 else if (cxx_dialect
>= cxx26
)
5150 pedwarn (loc
, opt
, "comparison of floating-point type %qT "
5151 "with enumeration type %qT", type0
, type1
);
5152 else if (enum_first_p
)
5153 warning_at (loc
, opt
, "comparison of enumeration type %qT with "
5154 "floating-point type %qT is deprecated",
5157 warning_at (loc
, opt
, "comparison of floating-point type %qT "
5158 "with enumeration type %qT is deprecated",
5161 case SPACESHIP_EXPR
:
5162 /* This is invalid, don't warn. */
5165 if (enum_first_p
&& cxx_dialect
>= cxx26
)
5166 pedwarn (loc
, opt
, "arithmetic between enumeration type %qT "
5167 "and floating-point type %qT", type0
, type1
);
5168 else if (cxx_dialect
>= cxx26
)
5169 pedwarn (loc
, opt
, "arithmetic between floating-point type %qT "
5170 "and enumeration type %qT", type0
, type1
);
5171 else if (enum_first_p
)
5172 warning_at (loc
, opt
, "arithmetic between enumeration type %qT "
5173 "and floating-point type %qT is deprecated",
5176 warning_at (loc
, opt
, "arithmetic between floating-point type %qT "
5177 "and enumeration type %qT is deprecated",
5185 /* Build a binary-operation expression without default conversions.
5186 CODE is the kind of expression to build.
5187 LOCATION is the location_t of the operator in the source code.
5188 This function differs from `build' in several ways:
5189 the data type of the result is computed and recorded in it,
5190 warnings are generated if arg data types are invalid,
5191 special handling for addition and subtraction of pointers is known,
5192 and some optimization is done (operations on narrow ints
5193 are done in the narrower type when that gives the same result).
5194 Constant folding is also done before the result is returned.
5196 Note that the operands will never have enumeral types
5197 because either they have just had the default conversions performed
5198 or they have both just been converted to some other type in which
5199 the arithmetic is to be done.
5201 C++: must do special pointer arithmetic when implementing
5202 multiple inheritance, and deal with pointer to member functions. */
5205 cp_build_binary_op (const op_location_t
&location
,
5206 enum tree_code code
, tree orig_op0
, tree orig_op1
,
5207 tsubst_flags_t complain
)
5210 enum tree_code code0
, code1
;
5211 tree type0
, type1
, orig_type0
, orig_type1
;
5212 const char *invalid_op_diag
;
5214 /* Expression code to give to the expression when it is built.
5215 Normally this is CODE, which is what the caller asked for,
5216 but in some special cases we change it. */
5217 enum tree_code resultcode
= code
;
5219 /* Data type in which the computation is to be performed.
5220 In the simplest cases this is the common type of the arguments. */
5221 tree result_type
= NULL_TREE
;
5223 /* When the computation is in excess precision, the type of the
5224 final EXCESS_PRECISION_EXPR. */
5225 tree semantic_result_type
= NULL
;
5227 /* Nonzero means operands have already been type-converted
5228 in whatever way is necessary.
5229 Zero means they need to be converted to RESULT_TYPE. */
5232 /* Nonzero means create the expression with this type, rather than
5234 tree build_type
= 0;
5236 /* Nonzero means after finally constructing the expression
5237 convert it to this type. */
5238 tree final_type
= 0;
5242 /* Nonzero if this is an operation like MIN or MAX which can
5243 safely be computed in short if both args are promoted shorts.
5244 Also implies COMMON.
5245 -1 indicates a bitwise operation; this makes a difference
5246 in the exact conditions for when it is safe to do the operation
5247 in a narrower mode. */
5250 /* Nonzero if this is a comparison operation;
5251 if both args are promoted shorts, compare the original shorts.
5252 Also implies COMMON. */
5253 int short_compare
= 0;
5255 /* Nonzero if this is a right-shift operation, which can be computed on the
5256 original short and then promoted if the operand is a promoted short. */
5257 int short_shift
= 0;
5259 /* Nonzero means set RESULT_TYPE to the common type of the args. */
5262 /* True if both operands have arithmetic type. */
5263 bool arithmetic_types_p
;
5265 /* Remember whether we're doing / or %. */
5266 bool doing_div_or_mod
= false;
5268 /* Remember whether we're doing << or >>. */
5269 bool doing_shift
= false;
5271 /* Tree holding instrumentation expression. */
5272 tree instrument_expr
= NULL_TREE
;
5274 /* True means this is an arithmetic operation that may need excess
5276 bool may_need_excess_precision
;
5278 /* Apply default conversions. */
5279 op0
= resolve_nondeduced_context (orig_op0
, complain
);
5280 op1
= resolve_nondeduced_context (orig_op1
, complain
);
5282 if (code
== TRUTH_AND_EXPR
|| code
== TRUTH_ANDIF_EXPR
5283 || code
== TRUTH_OR_EXPR
|| code
== TRUTH_ORIF_EXPR
5284 || code
== TRUTH_XOR_EXPR
)
5286 if (!really_overloaded_fn (op0
) && !VOID_TYPE_P (TREE_TYPE (op0
)))
5287 op0
= decay_conversion (op0
, complain
);
5288 if (!really_overloaded_fn (op1
) && !VOID_TYPE_P (TREE_TYPE (op1
)))
5289 op1
= decay_conversion (op1
, complain
);
5293 if (!really_overloaded_fn (op0
) && !VOID_TYPE_P (TREE_TYPE (op0
)))
5294 op0
= cp_default_conversion (op0
, complain
);
5295 if (!really_overloaded_fn (op1
) && !VOID_TYPE_P (TREE_TYPE (op1
)))
5296 op1
= cp_default_conversion (op1
, complain
);
5299 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
5300 STRIP_TYPE_NOPS (op0
);
5301 STRIP_TYPE_NOPS (op1
);
5303 /* DTRT if one side is an overloaded function, but complain about it. */
5304 if (type_unknown_p (op0
))
5306 tree t
= instantiate_type (TREE_TYPE (op1
), op0
, tf_none
);
5307 if (t
!= error_mark_node
)
5309 if (complain
& tf_error
)
5310 permerror (location
,
5311 "assuming cast to type %qT from overloaded function",
5316 if (type_unknown_p (op1
))
5318 tree t
= instantiate_type (TREE_TYPE (op0
), op1
, tf_none
);
5319 if (t
!= error_mark_node
)
5321 if (complain
& tf_error
)
5322 permerror (location
,
5323 "assuming cast to type %qT from overloaded function",
5329 orig_type0
= type0
= TREE_TYPE (op0
);
5330 orig_type1
= type1
= TREE_TYPE (op1
);
5331 tree non_ep_op0
= op0
;
5332 tree non_ep_op1
= op1
;
5334 /* The expression codes of the data types of the arguments tell us
5335 whether the arguments are integers, floating, pointers, etc. */
5336 code0
= TREE_CODE (type0
);
5337 code1
= TREE_CODE (type1
);
5339 /* If an error was already reported for one of the arguments,
5340 avoid reporting another error. */
5341 if (code0
== ERROR_MARK
|| code1
== ERROR_MARK
)
5342 return error_mark_node
;
5344 if ((invalid_op_diag
5345 = targetm
.invalid_binary_op (code
, type0
, type1
)))
5347 if (complain
& tf_error
)
5349 if (code0
== REAL_TYPE
5350 && code1
== REAL_TYPE
5351 && (extended_float_type_p (type0
)
5352 || extended_float_type_p (type1
))
5353 && cp_compare_floating_point_conversion_ranks (type0
,
5356 rich_location
richloc (line_table
, location
);
5357 binary_op_error (&richloc
, code
, type0
, type1
);
5360 error (invalid_op_diag
);
5362 return error_mark_node
;
5370 case TRUNC_DIV_EXPR
:
5372 case FLOOR_DIV_EXPR
:
5373 case ROUND_DIV_EXPR
:
5374 case EXACT_DIV_EXPR
:
5375 may_need_excess_precision
= true;
5383 case SPACESHIP_EXPR
:
5384 /* Excess precision for implicit conversions of integers to
5386 may_need_excess_precision
= (ANY_INTEGRAL_TYPE_P (type0
)
5387 || ANY_INTEGRAL_TYPE_P (type1
));
5390 may_need_excess_precision
= false;
5393 if (TREE_CODE (op0
) == EXCESS_PRECISION_EXPR
)
5395 op0
= TREE_OPERAND (op0
, 0);
5396 type0
= TREE_TYPE (op0
);
5398 else if (may_need_excess_precision
5399 && (code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
))
5400 if (tree eptype
= excess_precision_type (type0
))
5403 op0
= convert (eptype
, op0
);
5405 if (TREE_CODE (op1
) == EXCESS_PRECISION_EXPR
)
5407 op1
= TREE_OPERAND (op1
, 0);
5408 type1
= TREE_TYPE (op1
);
5410 else if (may_need_excess_precision
5411 && (code1
== REAL_TYPE
|| code1
== COMPLEX_TYPE
))
5412 if (tree eptype
= excess_precision_type (type1
))
5415 op1
= convert (eptype
, op1
);
5418 /* Issue warnings about peculiar, but valid, uses of NULL. */
5419 if ((null_node_p (orig_op0
) || null_node_p (orig_op1
))
5420 /* It's reasonable to use pointer values as operands of &&
5421 and ||, so NULL is no exception. */
5422 && code
!= TRUTH_ANDIF_EXPR
&& code
!= TRUTH_ORIF_EXPR
5423 && ( /* Both are NULL (or 0) and the operation was not a
5424 comparison or a pointer subtraction. */
5425 (null_ptr_cst_p (orig_op0
) && null_ptr_cst_p (orig_op1
)
5426 && code
!= EQ_EXPR
&& code
!= NE_EXPR
&& code
!= MINUS_EXPR
)
5427 /* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
5428 || (!null_ptr_cst_p (orig_op0
)
5429 && !TYPE_PTR_OR_PTRMEM_P (type0
))
5430 || (!null_ptr_cst_p (orig_op1
)
5431 && !TYPE_PTR_OR_PTRMEM_P (type1
)))
5432 && (complain
& tf_warning
))
5435 expansion_point_location_if_in_system_header (input_location
);
5437 warning_at (loc
, OPT_Wpointer_arith
, "NULL used in arithmetic");
5440 /* In case when one of the operands of the binary operation is
5441 a vector and another is a scalar -- convert scalar to vector. */
5442 if ((gnu_vector_type_p (type0
) && code1
!= VECTOR_TYPE
)
5443 || (gnu_vector_type_p (type1
) && code0
!= VECTOR_TYPE
))
5445 enum stv_conv convert_flag
5446 = scalar_to_vector (location
, code
, non_ep_op0
, non_ep_op1
,
5447 complain
& tf_error
);
5449 switch (convert_flag
)
5452 return error_mark_node
;
5455 op0
= convert (TREE_TYPE (type1
), op0
);
5456 op0
= save_expr (op0
);
5457 op0
= build_vector_from_val (type1
, op0
);
5458 orig_type0
= type0
= TREE_TYPE (op0
);
5459 code0
= TREE_CODE (type0
);
5465 op1
= convert (TREE_TYPE (type0
), op1
);
5466 op1
= save_expr (op1
);
5467 op1
= build_vector_from_val (type0
, op1
);
5468 orig_type1
= type1
= TREE_TYPE (op1
);
5469 code1
= TREE_CODE (type1
);
5481 /* Subtraction of two similar pointers.
5482 We must subtract them as integers, then divide by object size. */
5483 if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
5484 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0
),
5487 result
= pointer_diff (location
, op0
, op1
,
5488 common_pointer_type (type0
, type1
), complain
,
5490 if (instrument_expr
!= NULL
)
5491 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
),
5492 instrument_expr
, result
);
5496 /* In all other cases except pointer - int, the usual arithmetic
5498 else if (!(code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
))
5503 /* The pointer - int case is just like pointer + int; fall
5507 if ((code0
== POINTER_TYPE
|| code1
== POINTER_TYPE
)
5508 && (code0
== INTEGER_TYPE
|| code1
== INTEGER_TYPE
))
5512 ptr_operand
= ((code0
== POINTER_TYPE
) ? op0
: op1
);
5513 int_operand
= ((code0
== INTEGER_TYPE
) ? op0
: op1
);
5514 if (processing_template_decl
)
5516 result_type
= TREE_TYPE (ptr_operand
);
5519 return cp_pointer_int_sum (location
, code
,
5531 case TRUNC_DIV_EXPR
:
5533 case FLOOR_DIV_EXPR
:
5534 case ROUND_DIV_EXPR
:
5535 case EXACT_DIV_EXPR
:
5536 if (TREE_CODE (op0
) == SIZEOF_EXPR
&& TREE_CODE (op1
) == SIZEOF_EXPR
)
5538 tree type0
= TREE_OPERAND (op0
, 0);
5539 tree type1
= TREE_OPERAND (op1
, 0);
5540 tree first_arg
= tree_strip_any_location_wrapper (type0
);
5541 if (!TYPE_P (type0
))
5542 type0
= TREE_TYPE (type0
);
5543 if (!TYPE_P (type1
))
5544 type1
= TREE_TYPE (type1
);
5547 && INDIRECT_TYPE_P (type0
)
5548 && same_type_p (TREE_TYPE (type0
), type1
))
5550 if (!(TREE_CODE (first_arg
) == PARM_DECL
5551 && DECL_ARRAY_PARAMETER_P (first_arg
)
5552 && warn_sizeof_array_argument
)
5553 && (complain
& tf_warning
))
5555 auto_diagnostic_group d
;
5556 if (warning_at (location
, OPT_Wsizeof_pointer_div
,
5557 "division %<sizeof (%T) / sizeof (%T)%> does "
5558 "not compute the number of array elements",
5560 if (DECL_P (first_arg
))
5561 inform (DECL_SOURCE_LOCATION (first_arg
),
5562 "first %<sizeof%> operand was declared here");
5565 else if (!dependent_type_p (type0
)
5566 && !dependent_type_p (type1
)
5567 && TREE_CODE (type0
) == ARRAY_TYPE
5568 && !char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type0
)))
5569 /* Set by finish_parenthesized_expr. */
5570 && !warning_suppressed_p (op1
, OPT_Wsizeof_array_div
)
5571 && (complain
& tf_warning
))
5572 maybe_warn_sizeof_array_div (location
, first_arg
, type0
,
5573 op1
, non_reference (type1
));
5576 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
5577 || code0
== COMPLEX_TYPE
|| code0
== VECTOR_TYPE
)
5578 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
5579 || code1
== COMPLEX_TYPE
|| code1
== VECTOR_TYPE
))
5581 enum tree_code tcode0
= code0
, tcode1
= code1
;
5582 doing_div_or_mod
= true;
5583 warn_for_div_by_zero (location
, fold_for_warn (op1
));
5585 if (tcode0
== COMPLEX_TYPE
|| tcode0
== VECTOR_TYPE
)
5586 tcode0
= TREE_CODE (TREE_TYPE (TREE_TYPE (op0
)));
5587 if (tcode1
== COMPLEX_TYPE
|| tcode1
== VECTOR_TYPE
)
5588 tcode1
= TREE_CODE (TREE_TYPE (TREE_TYPE (op1
)));
5590 if (!(tcode0
== INTEGER_TYPE
&& tcode1
== INTEGER_TYPE
))
5591 resultcode
= RDIV_EXPR
;
5594 /* When dividing two signed integers, we have to promote to int.
5595 unless we divide by a constant != -1. Note that default
5596 conversion will have been performed on the operands at this
5597 point, so we have to dig out the original type to find out if
5599 tree stripped_op1
= tree_strip_any_location_wrapper (op1
);
5600 shorten
= may_shorten_divmod (op0
, stripped_op1
);
5610 if ((code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
5611 || (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
5612 && !VECTOR_FLOAT_TYPE_P (type0
)
5613 && !VECTOR_FLOAT_TYPE_P (type1
)))
5617 case TRUNC_MOD_EXPR
:
5618 case FLOOR_MOD_EXPR
:
5619 doing_div_or_mod
= true;
5620 warn_for_div_by_zero (location
, fold_for_warn (op1
));
5622 if (code0
== VECTOR_TYPE
&& code1
== VECTOR_TYPE
5623 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
5624 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
)
5626 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
5628 /* Although it would be tempting to shorten always here, that loses
5629 on some targets, since the modulo instruction is undefined if the
5630 quotient can't be represented in the computation mode. We shorten
5631 only if unsigned or if dividing by something we know != -1. */
5632 tree stripped_op1
= tree_strip_any_location_wrapper (op1
);
5633 shorten
= may_shorten_divmod (op0
, stripped_op1
);
5638 case TRUTH_ANDIF_EXPR
:
5639 case TRUTH_ORIF_EXPR
:
5640 case TRUTH_AND_EXPR
:
5642 if (!VECTOR_TYPE_P (type0
) && gnu_vector_type_p (type1
))
5644 if (!COMPARISON_CLASS_P (op1
))
5645 op1
= cp_build_binary_op (EXPR_LOCATION (op1
), NE_EXPR
, op1
,
5646 build_zero_cst (type1
), complain
);
5647 if (code
== TRUTH_ANDIF_EXPR
)
5649 tree z
= build_zero_cst (TREE_TYPE (op1
));
5650 return build_conditional_expr (location
, op0
, op1
, z
, complain
);
5652 else if (code
== TRUTH_ORIF_EXPR
)
5654 tree m1
= build_all_ones_cst (TREE_TYPE (op1
));
5655 return build_conditional_expr (location
, op0
, m1
, op1
, complain
);
5660 if (gnu_vector_type_p (type0
)
5661 && (!VECTOR_TYPE_P (type1
) || gnu_vector_type_p (type1
)))
5663 if (!COMPARISON_CLASS_P (op0
))
5664 op0
= cp_build_binary_op (EXPR_LOCATION (op0
), NE_EXPR
, op0
,
5665 build_zero_cst (type0
), complain
);
5666 if (!VECTOR_TYPE_P (type1
))
5668 tree m1
= build_all_ones_cst (TREE_TYPE (op0
));
5669 tree z
= build_zero_cst (TREE_TYPE (op0
));
5670 op1
= build_conditional_expr (location
, op1
, m1
, z
, complain
);
5672 else if (!COMPARISON_CLASS_P (op1
))
5673 op1
= cp_build_binary_op (EXPR_LOCATION (op1
), NE_EXPR
, op1
,
5674 build_zero_cst (type1
), complain
);
5676 if (code
== TRUTH_ANDIF_EXPR
)
5677 code
= BIT_AND_EXPR
;
5678 else if (code
== TRUTH_ORIF_EXPR
)
5679 code
= BIT_IOR_EXPR
;
5683 return cp_build_binary_op (location
, code
, op0
, op1
, complain
);
5686 result_type
= boolean_type_node
;
5689 /* Shift operations: result has same type as first operand;
5690 always convert second operand to int.
5691 Also set SHORT_SHIFT if shifting rightward. */
5694 if (gnu_vector_type_p (type0
)
5695 && code1
== INTEGER_TYPE
5696 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
5698 result_type
= type0
;
5701 else if (gnu_vector_type_p (type0
)
5702 && gnu_vector_type_p (type1
)
5703 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
5704 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
5705 && known_eq (TYPE_VECTOR_SUBPARTS (type0
),
5706 TYPE_VECTOR_SUBPARTS (type1
)))
5708 result_type
= type0
;
5711 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
5713 tree const_op1
= fold_for_warn (op1
);
5714 if (TREE_CODE (const_op1
) != INTEGER_CST
)
5716 result_type
= type0
;
5718 if (TREE_CODE (const_op1
) == INTEGER_CST
)
5720 if (tree_int_cst_lt (const_op1
, integer_zero_node
))
5722 if ((complain
& tf_warning
)
5723 && c_inhibit_evaluation_warnings
== 0)
5724 warning_at (location
, OPT_Wshift_count_negative
,
5725 "right shift count is negative");
5729 if (!integer_zerop (const_op1
))
5732 if (compare_tree_int (const_op1
, TYPE_PRECISION (type0
)) >= 0
5733 && (complain
& tf_warning
)
5734 && c_inhibit_evaluation_warnings
== 0)
5735 warning_at (location
, OPT_Wshift_count_overflow
,
5736 "right shift count >= width of type");
5739 /* Avoid converting op1 to result_type later. */
5745 if (gnu_vector_type_p (type0
)
5746 && code1
== INTEGER_TYPE
5747 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
)
5749 result_type
= type0
;
5752 else if (gnu_vector_type_p (type0
)
5753 && gnu_vector_type_p (type1
)
5754 && TREE_CODE (TREE_TYPE (type0
)) == INTEGER_TYPE
5755 && TREE_CODE (TREE_TYPE (type1
)) == INTEGER_TYPE
5756 && known_eq (TYPE_VECTOR_SUBPARTS (type0
),
5757 TYPE_VECTOR_SUBPARTS (type1
)))
5759 result_type
= type0
;
5762 else if (code0
== INTEGER_TYPE
&& code1
== INTEGER_TYPE
)
5764 tree const_op0
= fold_for_warn (op0
);
5765 if (TREE_CODE (const_op0
) != INTEGER_CST
)
5767 tree const_op1
= fold_for_warn (op1
);
5768 if (TREE_CODE (const_op1
) != INTEGER_CST
)
5770 result_type
= type0
;
5772 if (TREE_CODE (const_op0
) == INTEGER_CST
5773 && tree_int_cst_sgn (const_op0
) < 0
5774 && !TYPE_OVERFLOW_WRAPS (type0
)
5775 && (complain
& tf_warning
)
5776 && c_inhibit_evaluation_warnings
== 0)
5777 warning_at (location
, OPT_Wshift_negative_value
,
5778 "left shift of negative value");
5779 if (TREE_CODE (const_op1
) == INTEGER_CST
)
5781 if (tree_int_cst_lt (const_op1
, integer_zero_node
))
5783 if ((complain
& tf_warning
)
5784 && c_inhibit_evaluation_warnings
== 0)
5785 warning_at (location
, OPT_Wshift_count_negative
,
5786 "left shift count is negative");
5788 else if (compare_tree_int (const_op1
,
5789 TYPE_PRECISION (type0
)) >= 0)
5791 if ((complain
& tf_warning
)
5792 && c_inhibit_evaluation_warnings
== 0)
5793 warning_at (location
, OPT_Wshift_count_overflow
,
5794 "left shift count >= width of type");
5796 else if (TREE_CODE (const_op0
) == INTEGER_CST
5797 && (complain
& tf_warning
))
5798 maybe_warn_shift_overflow (location
, const_op0
, const_op1
);
5800 /* Avoid converting op1 to result_type later. */
5807 if (gnu_vector_type_p (type0
) && gnu_vector_type_p (type1
))
5808 goto vector_compare
;
5809 if ((complain
& tf_warning
)
5810 && c_inhibit_evaluation_warnings
== 0
5811 && (FLOAT_TYPE_P (type0
) || FLOAT_TYPE_P (type1
)))
5812 warning_at (location
, OPT_Wfloat_equal
,
5813 "comparing floating-point with %<==%> "
5814 "or %<!=%> is unsafe");
5815 if (complain
& tf_warning
)
5817 tree stripped_orig_op0
= tree_strip_any_location_wrapper (orig_op0
);
5818 tree stripped_orig_op1
= tree_strip_any_location_wrapper (orig_op1
);
5819 if ((TREE_CODE (stripped_orig_op0
) == STRING_CST
5820 && !integer_zerop (cp_fully_fold (op1
)))
5821 || (TREE_CODE (stripped_orig_op1
) == STRING_CST
5822 && !integer_zerop (cp_fully_fold (op0
))))
5823 warning_at (location
, OPT_Waddress
,
5824 "comparison with string literal results in "
5825 "unspecified behavior");
5826 else if (warn_array_compare
5827 && TREE_CODE (TREE_TYPE (orig_op0
)) == ARRAY_TYPE
5828 && TREE_CODE (TREE_TYPE (orig_op1
)) == ARRAY_TYPE
)
5829 do_warn_array_compare (location
, code
, stripped_orig_op0
,
5833 build_type
= boolean_type_node
;
5834 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
5835 || code0
== COMPLEX_TYPE
|| code0
== ENUMERAL_TYPE
)
5836 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
5837 || code1
== COMPLEX_TYPE
|| code1
== ENUMERAL_TYPE
))
5839 else if (((code0
== POINTER_TYPE
|| TYPE_PTRDATAMEM_P (type0
))
5840 && null_ptr_cst_p (orig_op1
))
5841 /* Handle, eg, (void*)0 (c++/43906), and more. */
5842 || (code0
== POINTER_TYPE
5843 && TYPE_PTR_P (type1
) && integer_zerop (op1
)))
5845 if (TYPE_PTR_P (type1
))
5846 result_type
= composite_pointer_type (location
,
5847 type0
, type1
, op0
, op1
,
5848 CPO_COMPARISON
, complain
);
5850 result_type
= type0
;
5852 if (char_type_p (TREE_TYPE (orig_op1
)))
5854 auto_diagnostic_group d
;
5855 if (warning_at (location
, OPT_Wpointer_compare
,
5856 "comparison between pointer and zero character "
5859 "did you mean to dereference the pointer?");
5861 warn_for_null_address (location
, op0
, complain
);
5863 else if (((code1
== POINTER_TYPE
|| TYPE_PTRDATAMEM_P (type1
))
5864 && null_ptr_cst_p (orig_op0
))
5865 /* Handle, eg, (void*)0 (c++/43906), and more. */
5866 || (code1
== POINTER_TYPE
5867 && TYPE_PTR_P (type0
) && integer_zerop (op0
)))
5869 if (TYPE_PTR_P (type0
))
5870 result_type
= composite_pointer_type (location
,
5871 type0
, type1
, op0
, op1
,
5872 CPO_COMPARISON
, complain
);
5874 result_type
= type1
;
5876 if (char_type_p (TREE_TYPE (orig_op0
)))
5878 auto_diagnostic_group d
;
5879 if (warning_at (location
, OPT_Wpointer_compare
,
5880 "comparison between pointer and zero character "
5883 "did you mean to dereference the pointer?");
5885 warn_for_null_address (location
, op1
, complain
);
5887 else if ((code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
5888 || (TYPE_PTRDATAMEM_P (type0
) && TYPE_PTRDATAMEM_P (type1
)))
5889 result_type
= composite_pointer_type (location
,
5890 type0
, type1
, op0
, op1
,
5891 CPO_COMPARISON
, complain
);
5892 else if (null_ptr_cst_p (orig_op0
) && null_ptr_cst_p (orig_op1
))
5893 /* One of the operands must be of nullptr_t type. */
5894 result_type
= TREE_TYPE (nullptr_node
);
5895 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
5897 result_type
= type0
;
5898 if (complain
& tf_error
)
5899 permerror (location
, "ISO C++ forbids comparison between "
5900 "pointer and integer");
5902 return error_mark_node
;
5904 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
5906 result_type
= type1
;
5907 if (complain
& tf_error
)
5908 permerror (location
, "ISO C++ forbids comparison between "
5909 "pointer and integer");
5911 return error_mark_node
;
5913 else if (TYPE_PTRMEMFUNC_P (type0
) && null_ptr_cst_p (orig_op1
))
5915 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
5916 == ptrmemfunc_vbit_in_delta
)
5918 tree pfn0
, delta0
, e1
, e2
;
5920 if (TREE_SIDE_EFFECTS (op0
))
5921 op0
= cp_save_expr (op0
);
5923 pfn0
= pfn_from_ptrmemfunc (op0
);
5924 delta0
= delta_from_ptrmemfunc (op0
);
5926 /* If we will warn below about a null-address compare
5927 involving the orig_op0 ptrmemfunc, we'd likely also
5928 warn about the pfn0's null-address compare, and
5929 that would be redundant, so suppress it. */
5930 warning_sentinel
ws (warn_address
);
5931 e1
= cp_build_binary_op (location
,
5934 build_zero_cst (TREE_TYPE (pfn0
)),
5937 e2
= cp_build_binary_op (location
,
5943 if (complain
& tf_warning
)
5944 maybe_warn_zero_as_null_pointer_constant (op1
, input_location
);
5946 e2
= cp_build_binary_op (location
,
5947 EQ_EXPR
, e2
, integer_zero_node
,
5949 op0
= cp_build_binary_op (location
,
5950 TRUTH_ANDIF_EXPR
, e1
, e2
,
5952 op1
= cp_convert (TREE_TYPE (op0
), integer_one_node
, complain
);
5956 op0
= build_ptrmemfunc_access_expr (op0
, pfn_identifier
);
5957 op1
= cp_convert (TREE_TYPE (op0
), op1
, complain
);
5959 result_type
= TREE_TYPE (op0
);
5961 warn_for_null_address (location
, orig_op0
, complain
);
5963 else if (TYPE_PTRMEMFUNC_P (type1
) && null_ptr_cst_p (orig_op0
))
5964 return cp_build_binary_op (location
, code
, op1
, op0
, complain
);
5965 else if (TYPE_PTRMEMFUNC_P (type0
) && TYPE_PTRMEMFUNC_P (type1
))
5968 /* E will be the final comparison. */
5970 /* E1 and E2 are for scratch. */
5978 type
= composite_pointer_type (location
, type0
, type1
, op0
, op1
,
5979 CPO_COMPARISON
, complain
);
5981 if (!same_type_p (TREE_TYPE (op0
), type
))
5982 op0
= cp_convert_and_check (type
, op0
, complain
);
5983 if (!same_type_p (TREE_TYPE (op1
), type
))
5984 op1
= cp_convert_and_check (type
, op1
, complain
);
5986 if (op0
== error_mark_node
|| op1
== error_mark_node
)
5987 return error_mark_node
;
5989 if (TREE_SIDE_EFFECTS (op0
))
5990 op0
= save_expr (op0
);
5991 if (TREE_SIDE_EFFECTS (op1
))
5992 op1
= save_expr (op1
);
5994 pfn0
= pfn_from_ptrmemfunc (op0
);
5995 pfn0
= cp_fully_fold (pfn0
);
5996 /* Avoid -Waddress warnings (c++/64877). */
5997 if (TREE_CODE (pfn0
) == ADDR_EXPR
)
5998 suppress_warning (pfn0
, OPT_Waddress
);
5999 pfn1
= pfn_from_ptrmemfunc (op1
);
6000 pfn1
= cp_fully_fold (pfn1
);
6001 delta0
= delta_from_ptrmemfunc (op0
);
6002 delta1
= delta_from_ptrmemfunc (op1
);
6003 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
6004 == ptrmemfunc_vbit_in_delta
)
6009 && ((op0.delta == op1.delta)
6010 || (!op0.pfn && op0.delta & 1 == 0
6011 && op1.delta & 1 == 0))
6013 The reason for the `!op0.pfn' bit is that a NULL
6014 pointer-to-member is any member with a zero PFN and
6015 LSB of the DELTA field is 0. */
6017 e1
= cp_build_binary_op (location
, BIT_AND_EXPR
,
6021 e1
= cp_build_binary_op (location
,
6022 EQ_EXPR
, e1
, integer_zero_node
,
6024 e2
= cp_build_binary_op (location
, BIT_AND_EXPR
,
6028 e2
= cp_build_binary_op (location
,
6029 EQ_EXPR
, e2
, integer_zero_node
,
6031 e1
= cp_build_binary_op (location
,
6032 TRUTH_ANDIF_EXPR
, e2
, e1
,
6034 e2
= cp_build_binary_op (location
, EQ_EXPR
,
6036 build_zero_cst (TREE_TYPE (pfn0
)),
6038 e2
= cp_build_binary_op (location
,
6039 TRUTH_ANDIF_EXPR
, e2
, e1
, complain
);
6040 e1
= cp_build_binary_op (location
,
6041 EQ_EXPR
, delta0
, delta1
, complain
);
6042 e1
= cp_build_binary_op (location
,
6043 TRUTH_ORIF_EXPR
, e1
, e2
, complain
);
6050 && (!op0.pfn || op0.delta == op1.delta))
6052 The reason for the `!op0.pfn' bit is that a NULL
6053 pointer-to-member is any member with a zero PFN; the
6054 DELTA field is unspecified. */
6056 e1
= cp_build_binary_op (location
,
6057 EQ_EXPR
, delta0
, delta1
, complain
);
6058 e2
= cp_build_binary_op (location
,
6061 build_zero_cst (TREE_TYPE (pfn0
)),
6063 e1
= cp_build_binary_op (location
,
6064 TRUTH_ORIF_EXPR
, e1
, e2
, complain
);
6066 e2
= build2 (EQ_EXPR
, boolean_type_node
, pfn0
, pfn1
);
6067 e
= cp_build_binary_op (location
,
6068 TRUTH_ANDIF_EXPR
, e2
, e1
, complain
);
6069 if (code
== EQ_EXPR
)
6071 return cp_build_binary_op (location
,
6072 EQ_EXPR
, e
, integer_zero_node
, complain
);
6076 gcc_assert (!TYPE_PTRMEMFUNC_P (type0
)
6077 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0
),
6079 gcc_assert (!TYPE_PTRMEMFUNC_P (type1
)
6080 || !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1
),
6088 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
)
6089 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
))
6091 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
6092 result_type
= composite_pointer_type (location
,
6093 type0
, type1
, op0
, op1
,
6094 CPO_COMPARISON
, complain
);
6101 case SPACESHIP_EXPR
:
6102 if (TREE_CODE (orig_op0
) == STRING_CST
6103 || TREE_CODE (orig_op1
) == STRING_CST
)
6105 if (complain
& tf_warning
)
6106 warning_at (location
, OPT_Waddress
,
6107 "comparison with string literal results "
6108 "in unspecified behavior");
6110 else if (warn_array_compare
6111 && TREE_CODE (TREE_TYPE (orig_op0
)) == ARRAY_TYPE
6112 && TREE_CODE (TREE_TYPE (orig_op1
)) == ARRAY_TYPE
6113 && code
!= SPACESHIP_EXPR
6114 && (complain
& tf_warning
))
6115 do_warn_array_compare (location
, code
,
6116 tree_strip_any_location_wrapper (orig_op0
),
6117 tree_strip_any_location_wrapper (orig_op1
));
6119 if (gnu_vector_type_p (type0
) && gnu_vector_type_p (type1
))
6123 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0
),
6125 && !vector_types_compatible_elements_p (type0
, type1
))
6127 if (complain
& tf_error
)
6129 auto_diagnostic_group d
;
6130 error_at (location
, "comparing vectors with different "
6132 inform (location
, "operand types are %qT and %qT",
6135 return error_mark_node
;
6138 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0
),
6139 TYPE_VECTOR_SUBPARTS (type1
)))
6141 if (complain
& tf_error
)
6143 auto_diagnostic_group d
;
6144 error_at (location
, "comparing vectors with different "
6145 "number of elements");
6146 inform (location
, "operand types are %qT and %qT",
6149 return error_mark_node
;
6152 /* It's not precisely specified how the usual arithmetic
6153 conversions apply to the vector types. Here, we use
6154 the unsigned type if one of the operands is signed and
6155 the other one is unsigned. */
6156 if (TYPE_UNSIGNED (type0
) != TYPE_UNSIGNED (type1
))
6158 if (!TYPE_UNSIGNED (type0
))
6159 op0
= build1 (VIEW_CONVERT_EXPR
, type1
, op0
);
6161 op1
= build1 (VIEW_CONVERT_EXPR
, type0
, op1
);
6162 warning_at (location
, OPT_Wsign_compare
, "comparison between "
6163 "types %qT and %qT", type0
, type1
);
6166 if (resultcode
== SPACESHIP_EXPR
)
6168 if (complain
& tf_error
)
6169 sorry_at (location
, "three-way comparison of vectors");
6170 return error_mark_node
;
6173 /* Always construct signed integer vector type. */
6174 intt
= c_common_type_for_size
6175 (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (type0
))), 0);
6178 if (complain
& tf_error
)
6179 error_at (location
, "could not find an integer type "
6180 "of the same size as %qT", TREE_TYPE (type0
));
6181 return error_mark_node
;
6183 result_type
= build_opaque_vector_type (intt
,
6184 TYPE_VECTOR_SUBPARTS (type0
));
6185 return build_vec_cmp (resultcode
, result_type
, op0
, op1
);
6187 build_type
= boolean_type_node
;
6188 if ((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
6189 || code0
== ENUMERAL_TYPE
)
6190 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
6191 || code1
== ENUMERAL_TYPE
))
6193 else if (code0
== POINTER_TYPE
&& code1
== POINTER_TYPE
)
6194 result_type
= composite_pointer_type (location
,
6195 type0
, type1
, op0
, op1
,
6196 CPO_COMPARISON
, complain
);
6197 else if ((code0
== POINTER_TYPE
&& null_ptr_cst_p (orig_op1
))
6198 || (code1
== POINTER_TYPE
&& null_ptr_cst_p (orig_op0
))
6199 || (null_ptr_cst_p (orig_op0
) && null_ptr_cst_p (orig_op1
)))
6201 /* Core Issue 1512 made this ill-formed. */
6202 if (complain
& tf_error
)
6203 error_at (location
, "ordered comparison of pointer with "
6204 "integer zero (%qT and %qT)", type0
, type1
);
6205 return error_mark_node
;
6207 else if (code0
== POINTER_TYPE
&& code1
== INTEGER_TYPE
)
6209 result_type
= type0
;
6210 if (complain
& tf_error
)
6211 permerror (location
, "ISO C++ forbids comparison between "
6212 "pointer and integer");
6214 return error_mark_node
;
6216 else if (code0
== INTEGER_TYPE
&& code1
== POINTER_TYPE
)
6218 result_type
= type1
;
6219 if (complain
& tf_error
)
6220 permerror (location
, "ISO C++ forbids comparison between "
6221 "pointer and integer");
6223 return error_mark_node
;
6226 if ((code0
== POINTER_TYPE
|| code1
== POINTER_TYPE
)
6227 && !processing_template_decl
6228 && sanitize_flags_p (SANITIZE_POINTER_COMPARE
))
6230 op0
= save_expr (op0
);
6231 op1
= save_expr (op1
);
6233 tree tt
= builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE
);
6234 instrument_expr
= build_call_expr_loc (location
, tt
, 2, op0
, op1
);
6239 case UNORDERED_EXPR
:
6246 build_type
= integer_type_node
;
6247 if (code0
!= REAL_TYPE
|| code1
!= REAL_TYPE
)
6249 if (complain
& tf_error
)
6250 error ("unordered comparison on non-floating-point argument");
6251 return error_mark_node
;
6260 if (((code0
== INTEGER_TYPE
|| code0
== REAL_TYPE
|| code0
== COMPLEX_TYPE
6261 || code0
== ENUMERAL_TYPE
)
6262 && (code1
== INTEGER_TYPE
|| code1
== REAL_TYPE
6263 || code1
== COMPLEX_TYPE
|| code1
== ENUMERAL_TYPE
)))
6264 arithmetic_types_p
= 1;
6267 arithmetic_types_p
= 0;
6268 /* Vector arithmetic is only allowed when both sides are vectors. */
6269 if (gnu_vector_type_p (type0
) && gnu_vector_type_p (type1
))
6271 if (!tree_int_cst_equal (TYPE_SIZE (type0
), TYPE_SIZE (type1
))
6272 || !vector_types_compatible_elements_p (type0
, type1
))
6274 if (complain
& tf_error
)
6276 /* "location" already embeds the locations of the
6277 operands, so we don't need to add them separately
6279 rich_location
richloc (line_table
, location
);
6280 binary_op_error (&richloc
, code
, type0
, type1
);
6282 return error_mark_node
;
6284 arithmetic_types_p
= 1;
6287 /* Determine the RESULT_TYPE, if it is not already known. */
6289 && arithmetic_types_p
6290 && (shorten
|| common
|| short_compare
))
6292 result_type
= cp_common_type (type0
, type1
);
6293 if (result_type
== error_mark_node
)
6297 if (TREE_CODE (t1
) == COMPLEX_TYPE
)
6298 t1
= TREE_TYPE (t1
);
6299 if (TREE_CODE (t2
) == COMPLEX_TYPE
)
6300 t2
= TREE_TYPE (t2
);
6301 gcc_checking_assert (TREE_CODE (t1
) == REAL_TYPE
6302 && TREE_CODE (t2
) == REAL_TYPE
6303 && (extended_float_type_p (t1
)
6304 || extended_float_type_p (t2
))
6305 && cp_compare_floating_point_conversion_ranks
6307 if (complain
& tf_error
)
6309 rich_location
richloc (line_table
, location
);
6310 binary_op_error (&richloc
, code
, type0
, type1
);
6312 return error_mark_node
;
6314 if (complain
& tf_warning
)
6315 do_warn_double_promotion (result_type
, type0
, type1
,
6316 "implicit conversion from %qH to %qI "
6317 "to match other operand of binary "
6318 "expression", location
);
6319 if (do_warn_enum_conversions (location
, code
, TREE_TYPE (orig_op0
),
6320 TREE_TYPE (orig_op1
), complain
))
6321 return error_mark_node
;
6323 if (may_need_excess_precision
6324 && (orig_type0
!= type0
|| orig_type1
!= type1
)
6325 && build_type
== NULL_TREE
6328 gcc_assert (common
);
6329 semantic_result_type
= cp_common_type (orig_type0
, orig_type1
);
6330 if (semantic_result_type
== error_mark_node
)
6332 tree t1
= orig_type0
;
6333 tree t2
= orig_type1
;
6334 if (TREE_CODE (t1
) == COMPLEX_TYPE
)
6335 t1
= TREE_TYPE (t1
);
6336 if (TREE_CODE (t2
) == COMPLEX_TYPE
)
6337 t2
= TREE_TYPE (t2
);
6338 gcc_checking_assert (TREE_CODE (t1
) == REAL_TYPE
6339 && TREE_CODE (t2
) == REAL_TYPE
6340 && (extended_float_type_p (t1
)
6341 || extended_float_type_p (t2
))
6342 && cp_compare_floating_point_conversion_ranks
6344 if (complain
& tf_error
)
6346 rich_location
richloc (line_table
, location
);
6347 binary_op_error (&richloc
, code
, type0
, type1
);
6349 return error_mark_node
;
6353 if (code
== SPACESHIP_EXPR
)
6355 iloc_sentinel
s (location
);
6357 tree orig_type0
= TREE_TYPE (orig_op0
);
6358 tree_code orig_code0
= TREE_CODE (orig_type0
);
6359 tree orig_type1
= TREE_TYPE (orig_op1
);
6360 tree_code orig_code1
= TREE_CODE (orig_type1
);
6361 if (!result_type
|| result_type
== error_mark_node
)
6363 result_type
= NULL_TREE
;
6364 else if ((orig_code0
== BOOLEAN_TYPE
) != (orig_code1
== BOOLEAN_TYPE
))
6365 /* "If one of the operands is of type bool and the other is not, the
6366 program is ill-formed." */
6367 result_type
= NULL_TREE
;
6368 else if (code0
== POINTER_TYPE
&& orig_code0
!= POINTER_TYPE
6369 && code1
== POINTER_TYPE
&& orig_code1
!= POINTER_TYPE
)
6370 /* We only do array/function-to-pointer conversion if "at least one of
6371 the operands is of pointer type". */
6372 result_type
= NULL_TREE
;
6373 else if (TYPE_PTRFN_P (result_type
) || NULLPTR_TYPE_P (result_type
))
6374 /* <=> no longer supports equality relations. */
6375 result_type
= NULL_TREE
;
6376 else if (orig_code0
== ENUMERAL_TYPE
&& orig_code1
== ENUMERAL_TYPE
6377 && !(same_type_ignoring_top_level_qualifiers_p
6378 (orig_type0
, orig_type1
)))
6379 /* "If both operands have arithmetic types, or one operand has integral
6380 type and the other operand has unscoped enumeration type, the usual
6381 arithmetic conversions are applied to the operands." So we don't do
6382 arithmetic conversions if the operands both have enumeral type. */
6383 result_type
= NULL_TREE
;
6384 else if ((orig_code0
== ENUMERAL_TYPE
&& orig_code1
== REAL_TYPE
)
6385 || (orig_code0
== REAL_TYPE
&& orig_code1
== ENUMERAL_TYPE
))
6386 /* [depr.arith.conv.enum]: Three-way comparisons between such operands
6387 [where one is of enumeration type and the other is of a different
6388 enumeration type or a floating-point type] are ill-formed. */
6389 result_type
= NULL_TREE
;
6393 build_type
= spaceship_type (result_type
, complain
);
6394 if (build_type
== error_mark_node
)
6395 return error_mark_node
;
6398 if (result_type
&& arithmetic_types_p
)
6400 /* If a narrowing conversion is required, other than from an integral
6401 type to a floating point type, the program is ill-formed. */
6403 if (TREE_CODE (result_type
) == REAL_TYPE
6404 && CP_INTEGRAL_TYPE_P (orig_type0
))
6406 else if (!check_narrowing (result_type
, orig_op0
, complain
))
6408 if (TREE_CODE (result_type
) == REAL_TYPE
6409 && CP_INTEGRAL_TYPE_P (orig_type1
))
6411 else if (!check_narrowing (result_type
, orig_op1
, complain
))
6413 if (!ok
&& !(complain
& tf_error
))
6414 return error_mark_node
;
6420 if (complain
& tf_error
)
6422 binary_op_rich_location
richloc (location
,
6423 orig_op0
, orig_op1
, true);
6425 "invalid operands of types %qT and %qT to binary %qO",
6426 TREE_TYPE (orig_op0
), TREE_TYPE (orig_op1
), code
);
6428 return error_mark_node
;
6431 /* If we're in a template, the only thing we need to know is the
6433 if (processing_template_decl
)
6435 /* Since the middle-end checks the type when doing a build2, we
6436 need to build the tree in pieces. This built tree will never
6437 get out of the front-end as we replace it when instantiating
6439 tree tmp
= build2 (resultcode
,
6440 build_type
? build_type
: result_type
,
6442 TREE_OPERAND (tmp
, 0) = op0
;
6443 if (semantic_result_type
)
6444 tmp
= build1 (EXCESS_PRECISION_EXPR
, semantic_result_type
, tmp
);
6448 /* Remember the original type; RESULT_TYPE might be changed later on
6449 by shorten_binary_op. */
6450 tree orig_type
= result_type
;
6452 if (arithmetic_types_p
)
6454 bool first_complex
= (code0
== COMPLEX_TYPE
);
6455 bool second_complex
= (code1
== COMPLEX_TYPE
);
6456 int none_complex
= (!first_complex
&& !second_complex
);
6458 /* Adapted from patch for c/24581. */
6459 if (first_complex
!= second_complex
6460 && (code
== PLUS_EXPR
6461 || code
== MINUS_EXPR
6462 || code
== MULT_EXPR
6463 || (code
== TRUNC_DIV_EXPR
&& first_complex
))
6464 && TREE_CODE (TREE_TYPE (result_type
)) == REAL_TYPE
6465 && flag_signed_zeros
)
6467 /* An operation on mixed real/complex operands must be
6468 handled specially, but the language-independent code can
6469 more easily optimize the plain complex arithmetic if
6470 -fno-signed-zeros. */
6471 tree real_type
= TREE_TYPE (result_type
);
6475 if (TREE_TYPE (op0
) != result_type
)
6476 op0
= cp_convert_and_check (result_type
, op0
, complain
);
6477 if (TREE_TYPE (op1
) != real_type
)
6478 op1
= cp_convert_and_check (real_type
, op1
, complain
);
6482 if (TREE_TYPE (op0
) != real_type
)
6483 op0
= cp_convert_and_check (real_type
, op0
, complain
);
6484 if (TREE_TYPE (op1
) != result_type
)
6485 op1
= cp_convert_and_check (result_type
, op1
, complain
);
6487 if (TREE_CODE (op0
) == ERROR_MARK
|| TREE_CODE (op1
) == ERROR_MARK
)
6488 return error_mark_node
;
6491 op0
= save_expr (op0
);
6492 real
= cp_build_unary_op (REALPART_EXPR
, op0
, true, complain
);
6493 imag
= cp_build_unary_op (IMAGPART_EXPR
, op0
, true, complain
);
6497 case TRUNC_DIV_EXPR
:
6498 op1
= save_expr (op1
);
6499 imag
= build2 (resultcode
, real_type
, imag
, op1
);
6503 real
= build2 (resultcode
, real_type
, real
, op1
);
6511 op1
= save_expr (op1
);
6512 real
= cp_build_unary_op (REALPART_EXPR
, op1
, true, complain
);
6513 imag
= cp_build_unary_op (IMAGPART_EXPR
, op1
, true, complain
);
6517 op0
= save_expr (op0
);
6518 imag
= build2 (resultcode
, real_type
, op0
, imag
);
6521 real
= build2 (resultcode
, real_type
, op0
, real
);
6524 real
= build2 (resultcode
, real_type
, op0
, real
);
6525 imag
= build1 (NEGATE_EXPR
, real_type
, imag
);
6531 result
= build2 (COMPLEX_EXPR
, result_type
, real
, imag
);
6532 if (semantic_result_type
)
6533 result
= build1 (EXCESS_PRECISION_EXPR
, semantic_result_type
,
6538 /* For certain operations (which identify themselves by shorten != 0)
6539 if both args were extended from the same smaller type,
6540 do the arithmetic in that type and then extend.
6542 shorten !=0 and !=1 indicates a bitwise operation.
6543 For them, this optimization is safe only if
6544 both args are zero-extended or both are sign-extended.
6545 Otherwise, we might change the result.
6546 E.g., (short)-1 | (unsigned short)-1 is (int)-1
6547 but calculated in (unsigned short) it would be (unsigned short)-1. */
6549 if (shorten
&& none_complex
)
6551 final_type
= result_type
;
6552 result_type
= shorten_binary_op (result_type
, op0
, op1
,
6556 /* Shifts can be shortened if shifting right. */
6561 tree arg0
= get_narrower (op0
, &unsigned_arg
);
6562 /* We're not really warning here but when we set short_shift we
6563 used fold_for_warn to fold the operand. */
6564 tree const_op1
= fold_for_warn (op1
);
6566 final_type
= result_type
;
6568 if (arg0
== op0
&& final_type
== TREE_TYPE (op0
))
6569 unsigned_arg
= TYPE_UNSIGNED (TREE_TYPE (op0
));
6571 if (TYPE_PRECISION (TREE_TYPE (arg0
)) < TYPE_PRECISION (result_type
)
6572 && tree_int_cst_sgn (const_op1
) > 0
6573 /* We can shorten only if the shift count is less than the
6574 number of bits in the smaller type size. */
6575 && compare_tree_int (const_op1
,
6576 TYPE_PRECISION (TREE_TYPE (arg0
))) < 0
6577 /* We cannot drop an unsigned shift after sign-extension. */
6578 && (!TYPE_UNSIGNED (final_type
) || unsigned_arg
))
6580 /* Do an unsigned shift if the operand was zero-extended. */
6582 = c_common_signed_or_unsigned_type (unsigned_arg
,
6584 /* Convert value-to-be-shifted to that type. */
6585 if (TREE_TYPE (op0
) != result_type
)
6586 op0
= convert (result_type
, op0
);
6591 /* Comparison operations are shortened too but differently.
6592 They identify themselves by setting short_compare = 1. */
6596 /* We call shorten_compare only for diagnostics. */
6597 tree xop0
= fold_simple (op0
);
6598 tree xop1
= fold_simple (op1
);
6599 tree xresult_type
= result_type
;
6600 enum tree_code xresultcode
= resultcode
;
6601 shorten_compare (location
, &xop0
, &xop1
, &xresult_type
,
6605 if ((short_compare
|| code
== MIN_EXPR
|| code
== MAX_EXPR
)
6606 && warn_sign_compare
6607 /* Do not warn until the template is instantiated; we cannot
6608 bound the ranges of the arguments until that point. */
6609 && !processing_template_decl
6610 && (complain
& tf_warning
)
6611 && c_inhibit_evaluation_warnings
== 0
6612 /* Even unsigned enum types promote to signed int. We don't
6613 want to issue -Wsign-compare warnings for this case. */
6614 && !enum_cast_to_int (orig_op0
)
6615 && !enum_cast_to_int (orig_op1
))
6617 warn_for_sign_compare (location
, orig_op0
, orig_op1
, op0
, op1
,
6618 result_type
, resultcode
);
6622 /* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
6623 Then the expression will be built.
6624 It will be given type FINAL_TYPE if that is nonzero;
6625 otherwise, it will be given type RESULT_TYPE. */
6628 warning_sentinel
w (warn_sign_conversion
, short_compare
);
6629 if (!same_type_p (TREE_TYPE (op0
), result_type
))
6630 op0
= cp_convert_and_check (result_type
, op0
, complain
);
6631 if (!same_type_p (TREE_TYPE (op1
), result_type
))
6632 op1
= cp_convert_and_check (result_type
, op1
, complain
);
6634 if (op0
== error_mark_node
|| op1
== error_mark_node
)
6635 return error_mark_node
;
6638 if (build_type
== NULL_TREE
)
6639 build_type
= result_type
;
6642 && flag_strong_eval_order
== 2
6643 && TREE_SIDE_EFFECTS (op1
)
6644 && !processing_template_decl
)
6646 /* In C++17, in both op0 << op1 and op0 >> op1 op0 is sequenced before
6647 op1, so if op1 has side-effects, use SAVE_EXPR around op0. */
6648 op0
= cp_save_expr (op0
);
6649 instrument_expr
= op0
;
6652 if (sanitize_flags_p ((SANITIZE_SHIFT
6654 | SANITIZE_FLOAT_DIVIDE
6655 | SANITIZE_SI_OVERFLOW
))
6656 && current_function_decl
!= NULL_TREE
6657 && !processing_template_decl
6658 && (doing_div_or_mod
|| doing_shift
))
6660 /* OP0 and/or OP1 might have side-effects. */
6661 op0
= cp_save_expr (op0
);
6662 op1
= cp_save_expr (op1
);
6663 op0
= fold_non_dependent_expr (op0
, complain
);
6664 op1
= fold_non_dependent_expr (op1
, complain
);
6665 tree instrument_expr1
= NULL_TREE
;
6666 if (doing_div_or_mod
6667 && sanitize_flags_p (SANITIZE_DIVIDE
6668 | SANITIZE_FLOAT_DIVIDE
6669 | SANITIZE_SI_OVERFLOW
))
6671 /* For diagnostics we want to use the promoted types without
6672 shorten_binary_op. So convert the arguments to the
6673 original result_type. */
6676 if (TREE_TYPE (cop0
) != orig_type
)
6677 cop0
= cp_convert (orig_type
, op0
, complain
);
6678 if (TREE_TYPE (cop1
) != orig_type
)
6679 cop1
= cp_convert (orig_type
, op1
, complain
);
6680 instrument_expr1
= ubsan_instrument_division (location
, cop0
, cop1
);
6682 else if (doing_shift
&& sanitize_flags_p (SANITIZE_SHIFT
))
6683 instrument_expr1
= ubsan_instrument_shift (location
, code
, op0
, op1
);
6684 if (instrument_expr
!= NULL
)
6685 instrument_expr
= add_stmt_to_compound (instrument_expr
,
6688 instrument_expr
= instrument_expr1
;
6691 result
= build2_loc (location
, resultcode
, build_type
, op0
, op1
);
6692 if (final_type
!= 0)
6693 result
= cp_convert (final_type
, result
, complain
);
6695 if (instrument_expr
!= NULL
)
6696 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
),
6697 instrument_expr
, result
);
6699 if (resultcode
== SPACESHIP_EXPR
&& !processing_template_decl
)
6700 result
= get_target_expr (result
, complain
);
6702 if (semantic_result_type
)
6703 result
= build1 (EXCESS_PRECISION_EXPR
, semantic_result_type
, result
);
6705 if (!c_inhibit_evaluation_warnings
)
6707 if (!processing_template_decl
)
6709 op0
= cp_fully_fold (op0
);
6710 /* Only consider the second argument if the first isn't overflowed. */
6711 if (!CONSTANT_CLASS_P (op0
) || TREE_OVERFLOW_P (op0
))
6713 op1
= cp_fully_fold (op1
);
6714 if (!CONSTANT_CLASS_P (op1
) || TREE_OVERFLOW_P (op1
))
6717 else if (!CONSTANT_CLASS_P (op0
) || !CONSTANT_CLASS_P (op1
)
6718 || TREE_OVERFLOW_P (op0
) || TREE_OVERFLOW_P (op1
))
6721 tree result_ovl
= fold_build2 (resultcode
, build_type
, op0
, op1
);
6722 if (TREE_OVERFLOW_P (result_ovl
))
6723 overflow_warning (location
, result_ovl
);
6729 /* Build a VEC_PERM_EXPR.
6730 This is a simple wrapper for c_build_vec_perm_expr. */
6732 build_x_vec_perm_expr (location_t loc
,
6733 tree arg0
, tree arg1
, tree arg2
,
6734 tsubst_flags_t complain
)
6736 tree orig_arg0
= arg0
;
6737 tree orig_arg1
= arg1
;
6738 tree orig_arg2
= arg2
;
6739 if (processing_template_decl
)
6741 if (type_dependent_expression_p (arg0
)
6742 || type_dependent_expression_p (arg1
)
6743 || type_dependent_expression_p (arg2
))
6744 return build_min_nt_loc (loc
, VEC_PERM_EXPR
, arg0
, arg1
, arg2
);
6746 tree exp
= c_build_vec_perm_expr (loc
, arg0
, arg1
, arg2
, complain
& tf_error
);
6747 if (processing_template_decl
&& exp
!= error_mark_node
)
6748 return build_min_non_dep (VEC_PERM_EXPR
, exp
, orig_arg0
,
6749 orig_arg1
, orig_arg2
);
6753 /* Build a VEC_PERM_EXPR.
6754 This is a simple wrapper for c_build_shufflevector. */
6756 build_x_shufflevector (location_t loc
, vec
<tree
, va_gc
> *args
,
6757 tsubst_flags_t complain
)
6759 tree arg0
= (*args
)[0];
6760 tree arg1
= (*args
)[1];
6761 if (processing_template_decl
)
6763 for (unsigned i
= 0; i
< args
->length (); ++i
)
6765 ? type_dependent_expression_p ((*args
)[i
])
6766 : instantiation_dependent_expression_p ((*args
)[i
]))
6768 tree exp
= build_min_nt_call_vec (NULL
, args
);
6769 CALL_EXPR_IFN (exp
) = IFN_SHUFFLEVECTOR
;
6773 auto_vec
<tree
, 16> mask
;
6774 for (unsigned i
= 2; i
< args
->length (); ++i
)
6776 tree idx
= fold_non_dependent_expr ((*args
)[i
], complain
);
6777 mask
.safe_push (idx
);
6779 tree exp
= c_build_shufflevector (loc
, arg0
, arg1
, mask
, complain
& tf_error
);
6780 if (processing_template_decl
&& exp
!= error_mark_node
)
6782 exp
= build_min_non_dep_call_vec (exp
, NULL
, args
);
6783 CALL_EXPR_IFN (exp
) = IFN_SHUFFLEVECTOR
;
6788 /* Return a tree for the sum or difference (RESULTCODE says which)
6789 of pointer PTROP and integer INTOP. */
6792 cp_pointer_int_sum (location_t loc
, enum tree_code resultcode
, tree ptrop
,
6793 tree intop
, tsubst_flags_t complain
)
6795 tree res_type
= TREE_TYPE (ptrop
);
6797 /* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
6798 in certain circumstance (when it's valid to do so). So we need
6799 to make sure it's complete. We don't need to check here, if we
6800 can actually complete it at all, as those checks will be done in
6801 pointer_int_sum() anyway. */
6802 complete_type (TREE_TYPE (res_type
));
6804 return pointer_int_sum (loc
, resultcode
, ptrop
,
6805 intop
, complain
& tf_warning_or_error
);
6808 /* Return a tree for the difference of pointers OP0 and OP1.
6809 The resulting tree has type int. If POINTER_SUBTRACT sanitization is
6810 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
6813 pointer_diff (location_t loc
, tree op0
, tree op1
, tree ptrtype
,
6814 tsubst_flags_t complain
, tree
*instrument_expr
)
6816 tree result
, inttype
;
6817 tree restype
= ptrdiff_type_node
;
6818 tree target_type
= TREE_TYPE (ptrtype
);
6820 if (!complete_type_or_maybe_complain (target_type
, NULL_TREE
, complain
))
6821 return error_mark_node
;
6823 if (VOID_TYPE_P (target_type
))
6825 if (complain
& tf_error
)
6826 permerror (loc
, "ISO C++ forbids using pointer of "
6827 "type %<void *%> in subtraction");
6829 return error_mark_node
;
6831 if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
6833 if (complain
& tf_error
)
6834 permerror (loc
, "ISO C++ forbids using pointer to "
6835 "a function in subtraction");
6837 return error_mark_node
;
6839 if (TREE_CODE (target_type
) == METHOD_TYPE
)
6841 if (complain
& tf_error
)
6842 permerror (loc
, "ISO C++ forbids using pointer to "
6843 "a method in subtraction");
6845 return error_mark_node
;
6847 else if (!verify_type_context (loc
, TCTX_POINTER_ARITH
,
6848 TREE_TYPE (TREE_TYPE (op0
)),
6849 !(complain
& tf_error
))
6850 || !verify_type_context (loc
, TCTX_POINTER_ARITH
,
6851 TREE_TYPE (TREE_TYPE (op1
)),
6852 !(complain
& tf_error
)))
6853 return error_mark_node
;
6855 /* Determine integer type result of the subtraction. This will usually
6856 be the same as the result type (ptrdiff_t), but may need to be a wider
6857 type if pointers for the address space are wider than ptrdiff_t. */
6858 if (TYPE_PRECISION (restype
) < TYPE_PRECISION (TREE_TYPE (op0
)))
6859 inttype
= c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0
)), 0);
6863 if (!processing_template_decl
6864 && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT
))
6866 op0
= save_expr (op0
);
6867 op1
= save_expr (op1
);
6869 tree tt
= builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT
);
6870 *instrument_expr
= build_call_expr_loc (loc
, tt
, 2, op0
, op1
);
6873 /* First do the subtraction, then build the divide operator
6874 and only convert at the very end.
6875 Do not do default conversions in case restype is a short type. */
6877 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
6878 pointers. If some platform cannot provide that, or has a larger
6879 ptrdiff_type to support differences larger than half the address
6880 space, cast the pointers to some larger integer type and do the
6881 computations in that type. */
6882 if (TYPE_PRECISION (inttype
) > TYPE_PRECISION (TREE_TYPE (op0
)))
6883 op0
= cp_build_binary_op (loc
,
6885 cp_convert (inttype
, op0
, complain
),
6886 cp_convert (inttype
, op1
, complain
),
6889 op0
= build2_loc (loc
, POINTER_DIFF_EXPR
, inttype
, op0
, op1
);
6891 /* This generates an error if op1 is a pointer to an incomplete type. */
6892 if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1
))))
6894 if (complain
& tf_error
)
6895 error_at (loc
, "invalid use of a pointer to an incomplete type in "
6896 "pointer arithmetic");
6898 return error_mark_node
;
6901 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1
)))
6903 if (complain
& tf_error
)
6904 error_at (loc
, "arithmetic on pointer to an empty aggregate");
6906 return error_mark_node
;
6909 op1
= (TYPE_PTROB_P (ptrtype
)
6910 ? size_in_bytes_loc (loc
, target_type
)
6911 : integer_one_node
);
6913 /* Do the division. */
6915 result
= build2_loc (loc
, EXACT_DIV_EXPR
, inttype
, op0
,
6916 cp_convert (inttype
, op1
, complain
));
6917 return cp_convert (restype
, result
, complain
);
6920 /* Construct and perhaps optimize a tree representation
6921 for a unary operation. CODE, a tree_code, specifies the operation
6922 and XARG is the operand. */
6925 build_x_unary_op (location_t loc
, enum tree_code code
, cp_expr xarg
,
6926 tree lookups
, tsubst_flags_t complain
)
6928 tree orig_expr
= xarg
;
6931 tree overload
= NULL_TREE
;
6933 if (processing_template_decl
)
6935 if (type_dependent_expression_p (xarg
))
6937 tree e
= build_min_nt_loc (loc
, code
, xarg
.get_value (), NULL_TREE
);
6938 TREE_TYPE (e
) = build_dependent_operator_type (lookups
, code
, false);
6945 /* [expr.unary.op] says:
6947 The address of an object of incomplete type can be taken.
6949 (And is just the ordinary address operator, not an overloaded
6950 "operator &".) However, if the type is a template
6951 specialization, we must complete the type at this point so that
6952 an overloaded "operator &" will be available if required. */
6953 if (code
== ADDR_EXPR
6954 && TREE_CODE (xarg
) != TEMPLATE_ID_EXPR
6955 && ((CLASS_TYPE_P (TREE_TYPE (xarg
))
6956 && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg
))))
6957 || (TREE_CODE (xarg
) == OFFSET_REF
)))
6958 /* Don't look for a function. */;
6960 exp
= build_new_op (loc
, code
, LOOKUP_NORMAL
, xarg
, NULL_TREE
,
6961 NULL_TREE
, lookups
, &overload
, complain
);
6963 if (!exp
&& code
== ADDR_EXPR
)
6965 if (is_overloaded_fn (xarg
))
6967 tree fn
= get_first_fn (xarg
);
6968 if (DECL_CONSTRUCTOR_P (fn
) || DECL_DESTRUCTOR_P (fn
))
6970 if (complain
& tf_error
)
6971 error_at (loc
, DECL_CONSTRUCTOR_P (fn
)
6972 ? G_("taking address of constructor %qD")
6973 : G_("taking address of destructor %qD"),
6975 return error_mark_node
;
6979 /* A pointer to member-function can be formed only by saying
6981 if (!flag_ms_extensions
&& TREE_CODE (TREE_TYPE (xarg
)) == METHOD_TYPE
6982 && (TREE_CODE (xarg
) != OFFSET_REF
|| !PTRMEM_OK_P (xarg
)))
6984 if (TREE_CODE (xarg
) != OFFSET_REF
6985 || !TYPE_P (TREE_OPERAND (xarg
, 0)))
6987 if (complain
& tf_error
)
6989 auto_diagnostic_group d
;
6990 error_at (loc
, "invalid use of %qE to form a "
6991 "pointer-to-member-function", xarg
.get_value ());
6992 if (TREE_CODE (xarg
) != OFFSET_REF
)
6993 inform (loc
, " a qualified-id is required");
6995 return error_mark_node
;
6999 if (complain
& tf_error
)
7000 error_at (loc
, "parentheses around %qE cannot be used to "
7001 "form a pointer-to-member-function",
7004 return error_mark_node
;
7005 PTRMEM_OK_P (xarg
) = 1;
7009 if (TREE_CODE (xarg
) == OFFSET_REF
)
7011 ptrmem
= PTRMEM_OK_P (xarg
);
7013 if (!ptrmem
&& !flag_ms_extensions
7014 && TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg
, 1))) == METHOD_TYPE
)
7016 /* A single non-static member, make sure we don't allow a
7017 pointer-to-member. */
7018 xarg
= build2 (OFFSET_REF
, TREE_TYPE (xarg
),
7019 TREE_OPERAND (xarg
, 0),
7020 ovl_make (TREE_OPERAND (xarg
, 1)));
7021 PTRMEM_OK_P (xarg
) = ptrmem
;
7025 exp
= cp_build_addr_expr_strict (xarg
, complain
);
7027 if (TREE_CODE (exp
) == PTRMEM_CST
)
7028 PTRMEM_CST_LOCATION (exp
) = loc
;
7030 protected_set_expr_location (exp
, loc
);
7033 if (processing_template_decl
&& exp
!= error_mark_node
)
7035 if (overload
!= NULL_TREE
)
7036 return (build_min_non_dep_op_overload
7037 (code
, exp
, overload
, orig_expr
, integer_zero_node
));
7039 exp
= build_min_non_dep (code
, exp
, orig_expr
,
7040 /*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE
);
7042 if (TREE_CODE (exp
) == ADDR_EXPR
)
7043 PTRMEM_OK_P (exp
) = ptrmem
;
7047 /* Construct and perhaps optimize a tree representation
7048 for __builtin_addressof operation. ARG specifies the operand. */
7051 cp_build_addressof (location_t loc
, tree arg
, tsubst_flags_t complain
)
7053 tree orig_expr
= arg
;
7055 if (processing_template_decl
)
7057 if (type_dependent_expression_p (arg
))
7058 return build_min_nt_loc (loc
, ADDRESSOF_EXPR
, arg
, NULL_TREE
);
7061 tree exp
= cp_build_addr_expr_strict (arg
, complain
);
7063 if (processing_template_decl
&& exp
!= error_mark_node
)
7064 exp
= build_min_non_dep (ADDRESSOF_EXPR
, exp
, orig_expr
, NULL_TREE
);
7068 /* Like c_common_truthvalue_conversion, but handle pointer-to-member
7069 constants, where a null value is represented by an INTEGER_CST of
7073 cp_truthvalue_conversion (tree expr
, tsubst_flags_t complain
)
7075 tree type
= TREE_TYPE (expr
);
7076 location_t loc
= cp_expr_loc_or_input_loc (expr
);
7077 if (TYPE_PTR_OR_PTRMEM_P (type
)
7078 /* Avoid ICE on invalid use of non-static member function. */
7079 || TREE_CODE (expr
) == FUNCTION_DECL
)
7080 return cp_build_binary_op (loc
, NE_EXPR
, expr
, nullptr_node
, complain
);
7082 return c_common_truthvalue_conversion (loc
, expr
);
7085 /* Returns EXPR contextually converted to bool. */
7088 contextual_conv_bool (tree expr
, tsubst_flags_t complain
)
7090 return perform_implicit_conversion_flags (boolean_type_node
, expr
,
7091 complain
, LOOKUP_NORMAL
);
7094 /* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. This
7095 is a low-level function; most callers should use maybe_convert_cond. */
7098 condition_conversion (tree expr
)
7100 tree t
= contextual_conv_bool (expr
, tf_warning_or_error
);
7101 if (!processing_template_decl
)
7102 t
= fold_build_cleanup_point_expr (boolean_type_node
, t
);
7106 /* Returns the address of T. This function will fold away
7107 ADDR_EXPR of INDIRECT_REF. This is only for low-level usage;
7108 most places should use cp_build_addr_expr instead. */
7111 build_address (tree t
)
7113 if (error_operand_p (t
) || !cxx_mark_addressable (t
))
7114 return error_mark_node
;
7115 gcc_checking_assert (TREE_CODE (t
) != CONSTRUCTOR
7116 || processing_template_decl
);
7117 t
= build_fold_addr_expr_loc (EXPR_LOCATION (t
), t
);
7118 if (TREE_CODE (t
) != ADDR_EXPR
)
7123 /* Return a NOP_EXPR converting EXPR to TYPE. */
7126 build_nop (tree type
, tree expr
)
7128 if (type
== error_mark_node
|| error_operand_p (expr
))
7130 return build1_loc (EXPR_LOCATION (expr
), NOP_EXPR
, type
, expr
);
7133 /* Take the address of ARG, whatever that means under C++ semantics.
7134 If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
7135 and class rvalues as well.
7137 Nothing should call this function directly; instead, callers should use
7138 cp_build_addr_expr or cp_build_addr_expr_strict. */
7141 cp_build_addr_expr_1 (tree arg
, bool strict_lvalue
, tsubst_flags_t complain
)
7146 if (!arg
|| error_operand_p (arg
))
7147 return error_mark_node
;
7149 arg
= mark_lvalue_use (arg
);
7150 if (error_operand_p (arg
))
7151 return error_mark_node
;
7153 argtype
= lvalue_type (arg
);
7154 location_t loc
= cp_expr_loc_or_input_loc (arg
);
7156 gcc_assert (!(identifier_p (arg
) && IDENTIFIER_ANY_OP_P (arg
)));
7158 if (TREE_CODE (arg
) == COMPONENT_REF
&& type_unknown_p (arg
)
7159 && !really_overloaded_fn (arg
))
7161 /* They're trying to take the address of a unique non-static
7162 member function. This is ill-formed (except in MS-land),
7163 but let's try to DTRT.
7164 Note: We only handle unique functions here because we don't
7165 want to complain if there's a static overload; non-unique
7166 cases will be handled by instantiate_type. But we need to
7167 handle this case here to allow casts on the resulting PMF.
7168 We could defer this in non-MS mode, but it's easier to give
7169 a useful error here. */
7171 /* Inside constant member functions, the `this' pointer
7172 contains an extra const qualifier. TYPE_MAIN_VARIANT
7173 is used here to remove this const from the diagnostics
7174 and the created OFFSET_REF. */
7175 tree base
= TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg
, 0)));
7176 tree fn
= get_first_fn (TREE_OPERAND (arg
, 1));
7177 if (!mark_used (fn
, complain
) && !(complain
& tf_error
))
7178 return error_mark_node
;
7179 /* Until microsoft headers are known to incorrectly take the address of
7180 unqualified xobj member functions we should not support this
7182 See comment in class.cc:resolve_address_of_overloaded_function for
7183 the extended reasoning. */
7184 if (!flag_ms_extensions
|| DECL_XOBJ_MEMBER_FUNCTION_P (fn
))
7186 auto_diagnostic_group d
;
7187 tree name
= DECL_NAME (fn
);
7188 if (!(complain
& tf_error
))
7189 return error_mark_node
;
7190 else if (current_class_type
7191 && TREE_OPERAND (arg
, 0) == current_class_ref
)
7192 /* An expression like &memfn. */
7193 if (!DECL_XOBJ_MEMBER_FUNCTION_P (fn
))
7195 "ISO C++ forbids taking the address of an unqualified"
7196 " or parenthesized non-static member function to form"
7197 " a pointer to member function. Say %<&%T::%D%>",
7201 "ISO C++ forbids taking the address of an unqualified"
7202 " or parenthesized non-static member function to form"
7203 " a pointer to explicit object member function");
7205 if (!DECL_XOBJ_MEMBER_FUNCTION_P (fn
))
7207 "ISO C++ forbids taking the address of a bound member"
7208 " function to form a pointer to member function."
7213 "ISO C++ forbids taking the address of a bound member"
7214 " function to form a pointer to explicit object member"
7216 if (DECL_XOBJ_MEMBER_FUNCTION_P (fn
))
7218 "a pointer to explicit object member function can only be "
7219 "formed with %<&%T::%D%>", base
, name
);
7221 arg
= build_offset_ref (base
, fn
, /*address_p=*/true, complain
);
7224 /* Uninstantiated types are all functions. Taking the
7225 address of a function is a no-op, so just return the
7227 if (type_unknown_p (arg
))
7228 return build1 (ADDR_EXPR
, unknown_type_node
, arg
);
7230 if (TREE_CODE (arg
) == OFFSET_REF
)
7231 /* We want a pointer to member; bypass all the code for actually taking
7232 the address of something. */
7235 /* Anything not already handled and not a true memory reference
7237 if (!FUNC_OR_METHOD_TYPE_P (argtype
))
7239 cp_lvalue_kind kind
= lvalue_kind (arg
);
7240 if (kind
== clk_none
)
7242 if (complain
& tf_error
)
7243 lvalue_error (loc
, lv_addressof
);
7244 return error_mark_node
;
7246 if (strict_lvalue
&& (kind
& (clk_rvalueref
|clk_class
)))
7248 if (!(complain
& tf_error
))
7249 return error_mark_node
;
7250 /* Make this a permerror because we used to accept it. */
7251 permerror (loc
, "taking address of rvalue");
7255 if (TYPE_REF_P (argtype
))
7257 tree type
= build_pointer_type (TREE_TYPE (argtype
));
7258 arg
= build1 (CONVERT_EXPR
, type
, arg
);
7261 else if (pedantic
&& DECL_MAIN_P (tree_strip_any_location_wrapper (arg
)))
7264 /* Apparently a lot of autoconf scripts for C++ packages do this,
7265 so only complain if -Wpedantic. */
7266 if (complain
& (flag_pedantic_errors
? tf_error
: tf_warning
))
7267 pedwarn (loc
, OPT_Wpedantic
,
7268 "ISO C++ forbids taking address of function %<::main%>");
7269 else if (flag_pedantic_errors
)
7270 return error_mark_node
;
7273 /* Let &* cancel out to simplify resulting code. */
7274 if (INDIRECT_REF_P (arg
))
7276 arg
= TREE_OPERAND (arg
, 0);
7277 if (TYPE_REF_P (TREE_TYPE (arg
)))
7279 tree type
= build_pointer_type (TREE_TYPE (TREE_TYPE (arg
)));
7280 arg
= build1 (CONVERT_EXPR
, type
, arg
);
7283 /* Don't let this be an lvalue. */
7288 /* Handle complex lvalues (when permitted)
7289 by reduction to simpler cases. */
7290 val
= unary_complex_lvalue (ADDR_EXPR
, arg
);
7294 switch (TREE_CODE (arg
))
7298 case FIX_TRUNC_EXPR
:
7299 /* We should have handled this above in the lvalue_kind check. */
7304 arg
= BASELINK_FUNCTIONS (arg
);
7308 arg
= OVL_FIRST (arg
);
7313 /* Turn a reference to a non-static data member into a
7314 pointer-to-member. */
7319 gcc_assert (PTRMEM_OK_P (arg
));
7321 t
= TREE_OPERAND (arg
, 1);
7322 if (TYPE_REF_P (TREE_TYPE (t
)))
7324 if (complain
& tf_error
)
7326 "cannot create pointer to reference member %qD", t
);
7327 return error_mark_node
;
7330 /* Forming a pointer-to-member is a use of non-pure-virtual fns. */
7331 if (TREE_CODE (t
) == FUNCTION_DECL
7332 && !DECL_PURE_VIRTUAL_P (t
)
7333 && !mark_used (t
, complain
) && !(complain
& tf_error
))
7334 return error_mark_node
;
7336 /* Pull out the function_decl for a single xobj member function, and
7337 let the rest of this function handle it. This is similar to how
7338 static member functions are handled in the BASELINK case above. */
7339 if (DECL_XOBJ_MEMBER_FUNCTION_P (t
))
7345 type
= build_ptrmem_type (context_for_name_lookup (t
),
7347 t
= make_ptrmem_cst (type
, t
);
7355 if (argtype
!= error_mark_node
)
7356 argtype
= build_pointer_type (argtype
);
7358 if (bitfield_p (arg
))
7360 if (complain
& tf_error
)
7361 error_at (loc
, "attempt to take address of bit-field");
7362 return error_mark_node
;
7365 /* In a template, we are processing a non-dependent expression
7366 so we can just form an ADDR_EXPR with the correct type. */
7367 if (processing_template_decl
|| TREE_CODE (arg
) != COMPONENT_REF
)
7369 if (!mark_single_function (arg
, complain
))
7370 return error_mark_node
;
7371 val
= build_address (arg
);
7372 if (TREE_CODE (arg
) == OFFSET_REF
)
7373 PTRMEM_OK_P (val
) = PTRMEM_OK_P (arg
);
7375 else if (BASELINK_P (TREE_OPERAND (arg
, 1)))
7377 tree fn
= BASELINK_FUNCTIONS (TREE_OPERAND (arg
, 1));
7379 if (TREE_CODE (fn
) == OVERLOAD
&& OVL_SINGLE_P (fn
))
7380 fn
= OVL_FIRST (fn
);
7382 /* We can only get here with a single static member
7384 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
7385 && DECL_STATIC_FUNCTION_P (fn
));
7386 if (!mark_used (fn
, complain
) && !(complain
& tf_error
))
7387 return error_mark_node
;
7388 val
= build_address (fn
);
7389 if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg
, 0)))
7390 /* Do not lose object's side effects. */
7391 val
= build2 (COMPOUND_EXPR
, TREE_TYPE (val
),
7392 TREE_OPERAND (arg
, 0), val
);
7396 tree object
= TREE_OPERAND (arg
, 0);
7397 tree field
= TREE_OPERAND (arg
, 1);
7398 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7399 (TREE_TYPE (object
), decl_type_context (field
)));
7400 val
= build_address (arg
);
7403 if (TYPE_PTR_P (argtype
)
7404 && TREE_CODE (TREE_TYPE (argtype
)) == METHOD_TYPE
)
7406 build_ptrmemfunc_type (argtype
);
7407 val
= build_ptrmemfunc (argtype
, val
, 0,
7412 /* Ensure we have EXPR_LOCATION set for possible later diagnostics. */
7413 if (TREE_CODE (val
) == ADDR_EXPR
7414 && TREE_CODE (TREE_OPERAND (val
, 0)) == FUNCTION_DECL
)
7415 SET_EXPR_LOCATION (val
, input_location
);
7420 /* Take the address of ARG if it has one, even if it's an rvalue. */
7423 cp_build_addr_expr (tree arg
, tsubst_flags_t complain
)
7425 return cp_build_addr_expr_1 (arg
, 0, complain
);
7428 /* Take the address of ARG, but only if it's an lvalue. */
7431 cp_build_addr_expr_strict (tree arg
, tsubst_flags_t complain
)
7433 return cp_build_addr_expr_1 (arg
, 1, complain
);
7436 /* C++: Must handle pointers to members.
7438 Perhaps type instantiation should be extended to handle conversion
7439 from aggregates to types we don't yet know we want? (Or are those
7440 cases typically errors which should be reported?)
7442 NOCONVERT suppresses the default promotions (such as from short to int). */
7445 cp_build_unary_op (enum tree_code code
, tree xarg
, bool noconvert
,
7446 tsubst_flags_t complain
)
7448 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
7450 location_t location
= cp_expr_loc_or_input_loc (arg
);
7452 tree eptype
= NULL_TREE
;
7453 const char *errstring
= NULL
;
7455 const char *invalid_op_diag
;
7457 if (!arg
|| error_operand_p (arg
))
7458 return error_mark_node
;
7460 arg
= resolve_nondeduced_context (arg
, complain
);
7462 if ((invalid_op_diag
7463 = targetm
.invalid_unary_op ((code
== UNARY_PLUS_EXPR
7468 if (complain
& tf_error
)
7469 error (invalid_op_diag
);
7470 return error_mark_node
;
7473 if (TREE_CODE (arg
) == EXCESS_PRECISION_EXPR
)
7475 eptype
= TREE_TYPE (arg
);
7476 arg
= TREE_OPERAND (arg
, 0);
7481 case UNARY_PLUS_EXPR
:
7484 int flags
= WANT_ARITH
| WANT_ENUM
;
7485 /* Unary plus (but not unary minus) is allowed on pointers. */
7486 if (code
== UNARY_PLUS_EXPR
)
7487 flags
|= WANT_POINTER
;
7488 arg
= build_expr_type_conversion (flags
, arg
, true);
7490 errstring
= (code
== NEGATE_EXPR
7491 ? _("wrong type argument to unary minus")
7492 : _("wrong type argument to unary plus"));
7495 if (!noconvert
&& INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg
)))
7496 arg
= cp_perform_integral_promotions (arg
, complain
);
7498 /* Make sure the result is not an lvalue: a unary plus or minus
7499 expression is always a rvalue. */
7506 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
7511 arg
= cp_default_conversion (arg
, complain
);
7512 if (arg
== error_mark_node
)
7513 return error_mark_node
;
7516 else if (!(arg
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
7517 | WANT_VECTOR_OR_COMPLEX
,
7519 errstring
= _("wrong type argument to bit-complement");
7520 else if (!noconvert
&& INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg
)))
7522 /* Warn if the expression has boolean value. */
7523 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
7524 && (complain
& tf_warning
))
7526 auto_diagnostic_group d
;
7527 if (warning_at (location
, OPT_Wbool_operation
,
7528 "%<~%> on an expression of type %<bool%>"))
7529 inform (location
, "did you mean to use logical not (%<!%>)?");
7531 arg
= cp_perform_integral_promotions (arg
, complain
);
7533 else if (!noconvert
&& VECTOR_TYPE_P (TREE_TYPE (arg
)))
7534 arg
= mark_rvalue_use (arg
);
7538 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, true)))
7539 errstring
= _("wrong type argument to abs");
7540 else if (!noconvert
)
7542 arg
= cp_default_conversion (arg
, complain
);
7543 if (arg
== error_mark_node
)
7544 return error_mark_node
;
7549 /* Conjugating a real value is a no-op, but allow it anyway. */
7550 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_ENUM
, arg
, true)))
7551 errstring
= _("wrong type argument to conjugation");
7552 else if (!noconvert
)
7554 arg
= cp_default_conversion (arg
, complain
);
7555 if (arg
== error_mark_node
)
7556 return error_mark_node
;
7560 case TRUTH_NOT_EXPR
:
7561 if (gnu_vector_type_p (TREE_TYPE (arg
)))
7562 return cp_build_binary_op (input_location
, EQ_EXPR
, arg
,
7563 build_zero_cst (TREE_TYPE (arg
)), complain
);
7564 arg
= perform_implicit_conversion (boolean_type_node
, arg
,
7566 if (arg
!= error_mark_node
)
7568 if (processing_template_decl
)
7569 return build1_loc (location
, TRUTH_NOT_EXPR
, boolean_type_node
, arg
);
7570 val
= invert_truthvalue_loc (location
, arg
);
7571 if (obvalue_p (val
))
7572 val
= non_lvalue_loc (location
, val
);
7575 errstring
= _("in argument to unary !");
7583 val
= build_real_imag_expr (input_location
, code
, arg
);
7584 if (eptype
&& TREE_CODE (eptype
) == COMPLEX_EXPR
)
7585 val
= build1_loc (input_location
, EXCESS_PRECISION_EXPR
,
7586 TREE_TYPE (eptype
), val
);
7589 case PREINCREMENT_EXPR
:
7590 case POSTINCREMENT_EXPR
:
7591 case PREDECREMENT_EXPR
:
7592 case POSTDECREMENT_EXPR
:
7593 /* Handle complex lvalues (when permitted)
7594 by reduction to simpler cases. */
7596 val
= unary_complex_lvalue (code
, arg
);
7598 goto return_build_unary_op
;
7600 arg
= mark_lvalue_use (arg
);
7602 /* Increment or decrement the real part of the value,
7603 and don't change the imaginary part. */
7604 if (TREE_CODE (TREE_TYPE (arg
)) == COMPLEX_TYPE
)
7608 arg
= cp_stabilize_reference (arg
);
7609 real
= cp_build_unary_op (REALPART_EXPR
, arg
, true, complain
);
7610 imag
= cp_build_unary_op (IMAGPART_EXPR
, arg
, true, complain
);
7611 real
= cp_build_unary_op (code
, real
, true, complain
);
7612 if (real
== error_mark_node
|| imag
== error_mark_node
)
7613 return error_mark_node
;
7614 val
= build2 (COMPLEX_EXPR
, TREE_TYPE (arg
), real
, imag
);
7615 goto return_build_unary_op
;
7618 /* Report invalid types. */
7620 if (!(arg
= build_expr_type_conversion (WANT_ARITH
| WANT_POINTER
,
7623 if (code
== PREINCREMENT_EXPR
)
7624 errstring
= _("no pre-increment operator for type");
7625 else if (code
== POSTINCREMENT_EXPR
)
7626 errstring
= _("no post-increment operator for type");
7627 else if (code
== PREDECREMENT_EXPR
)
7628 errstring
= _("no pre-decrement operator for type");
7630 errstring
= _("no post-decrement operator for type");
7633 else if (arg
== error_mark_node
)
7634 return error_mark_node
;
7636 /* Report something read-only. */
7638 if (CP_TYPE_CONST_P (TREE_TYPE (arg
))
7639 || TREE_READONLY (arg
))
7641 if (complain
& tf_error
)
7642 cxx_readonly_error (location
, arg
,
7643 ((code
== PREINCREMENT_EXPR
7644 || code
== POSTINCREMENT_EXPR
)
7645 ? lv_increment
: lv_decrement
));
7647 return error_mark_node
;
7652 tree declared_type
= unlowered_expr_type (arg
);
7654 argtype
= TREE_TYPE (arg
);
7656 /* ARM $5.2.5 last annotation says this should be forbidden. */
7657 if (TREE_CODE (argtype
) == ENUMERAL_TYPE
)
7659 if (complain
& tf_error
)
7660 permerror (location
, (code
== PREINCREMENT_EXPR
7661 || code
== POSTINCREMENT_EXPR
)
7662 ? G_("ISO C++ forbids incrementing an enum")
7663 : G_("ISO C++ forbids decrementing an enum"));
7665 return error_mark_node
;
7668 /* Compute the increment. */
7670 if (TYPE_PTR_P (argtype
))
7672 tree type
= complete_type (TREE_TYPE (argtype
));
7674 if (!COMPLETE_OR_VOID_TYPE_P (type
))
7676 if (complain
& tf_error
)
7677 error_at (location
, ((code
== PREINCREMENT_EXPR
7678 || code
== POSTINCREMENT_EXPR
))
7679 ? G_("cannot increment a pointer to incomplete "
7681 : G_("cannot decrement a pointer to incomplete "
7683 TREE_TYPE (argtype
));
7685 return error_mark_node
;
7687 else if (!TYPE_PTROB_P (argtype
))
7689 if (complain
& tf_error
)
7690 pedwarn (location
, OPT_Wpointer_arith
,
7691 (code
== PREINCREMENT_EXPR
7692 || code
== POSTINCREMENT_EXPR
)
7693 ? G_("ISO C++ forbids incrementing a pointer "
7695 : G_("ISO C++ forbids decrementing a pointer "
7699 return error_mark_node
;
7701 else if (!verify_type_context (location
, TCTX_POINTER_ARITH
,
7702 TREE_TYPE (argtype
),
7703 !(complain
& tf_error
)))
7704 return error_mark_node
;
7706 inc
= cxx_sizeof_nowarn (TREE_TYPE (argtype
));
7709 inc
= VECTOR_TYPE_P (argtype
)
7710 ? build_one_cst (argtype
)
7713 inc
= cp_convert (argtype
, inc
, complain
);
7715 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
7716 need to ask Objective-C to build the increment or decrement
7717 expression for it. */
7718 if (objc_is_property_ref (arg
))
7719 return objc_build_incr_expr_for_property_ref (input_location
, code
,
7722 /* Complain about anything else that is not a true lvalue. */
7723 if (!lvalue_or_else (arg
, ((code
== PREINCREMENT_EXPR
7724 || code
== POSTINCREMENT_EXPR
)
7725 ? lv_increment
: lv_decrement
),
7727 return error_mark_node
;
7729 /* [depr.volatile.type] "Postfix ++ and -- expressions and
7730 prefix ++ and -- expressions of volatile-qualified arithmetic
7731 and pointer types are deprecated." */
7732 if ((TREE_THIS_VOLATILE (arg
) || CP_TYPE_VOLATILE_P (TREE_TYPE (arg
)))
7733 && (complain
& tf_warning
))
7734 warning_at (location
, OPT_Wvolatile
,
7735 "%qs expression of %<volatile%>-qualified type is "
7737 ((code
== PREINCREMENT_EXPR
7738 || code
== POSTINCREMENT_EXPR
)
7741 /* Forbid using -- or ++ in C++17 on `bool'. */
7742 if (TREE_CODE (declared_type
) == BOOLEAN_TYPE
)
7744 if (code
== POSTDECREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
7746 if (complain
& tf_error
)
7748 "use of an operand of type %qT in %<operator--%> "
7749 "is forbidden", boolean_type_node
);
7750 return error_mark_node
;
7754 if (cxx_dialect
>= cxx17
)
7756 if (complain
& tf_error
)
7758 "use of an operand of type %qT in "
7759 "%<operator++%> is forbidden in C++17",
7761 return error_mark_node
;
7763 /* Otherwise, [depr.incr.bool] says this is deprecated. */
7764 else if (complain
& tf_warning
)
7765 warning_at (location
, OPT_Wdeprecated
,
7766 "use of an operand of type %qT "
7767 "in %<operator++%> is deprecated",
7770 val
= boolean_increment (code
, arg
);
7772 else if (code
== POSTINCREMENT_EXPR
|| code
== POSTDECREMENT_EXPR
)
7773 /* An rvalue has no cv-qualifiers. */
7774 val
= build2 (code
, cv_unqualified (TREE_TYPE (arg
)), arg
, inc
);
7776 val
= build2 (code
, TREE_TYPE (arg
), arg
, inc
);
7778 TREE_SIDE_EFFECTS (val
) = 1;
7779 goto return_build_unary_op
;
7783 /* Note that this operation never does default_conversion
7784 regardless of NOCONVERT. */
7785 return cp_build_addr_expr (arg
, complain
);
7794 argtype
= TREE_TYPE (arg
);
7795 val
= build1 (code
, argtype
, arg
);
7796 return_build_unary_op
:
7798 val
= build1 (EXCESS_PRECISION_EXPR
, eptype
, val
);
7802 if (complain
& tf_error
)
7803 error_at (location
, "%s", errstring
);
7804 return error_mark_node
;
7807 /* Hook for the c-common bits that build a unary op. */
7809 build_unary_op (location_t
/*location*/,
7810 enum tree_code code
, tree xarg
, bool noconvert
)
7812 return cp_build_unary_op (code
, xarg
, noconvert
, tf_warning_or_error
);
7815 /* Adjust LVALUE, an MODIFY_EXPR, PREINCREMENT_EXPR or PREDECREMENT_EXPR,
7816 so that it is a valid lvalue even for GENERIC by replacing
7817 (lhs = rhs) with ((lhs = rhs), lhs)
7818 (--lhs) with ((--lhs), lhs)
7819 (++lhs) with ((++lhs), lhs)
7820 and if lhs has side-effects, calling cp_stabilize_reference on it, so
7821 that it can be evaluated multiple times. */
7824 genericize_compound_lvalue (tree lvalue
)
7826 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lvalue
, 0)))
7827 lvalue
= build2 (TREE_CODE (lvalue
), TREE_TYPE (lvalue
),
7828 cp_stabilize_reference (TREE_OPERAND (lvalue
, 0)),
7829 TREE_OPERAND (lvalue
, 1));
7830 return build2 (COMPOUND_EXPR
, TREE_TYPE (TREE_OPERAND (lvalue
, 0)),
7831 lvalue
, TREE_OPERAND (lvalue
, 0));
7834 /* Apply unary lvalue-demanding operator CODE to the expression ARG
7835 for certain kinds of expressions which are not really lvalues
7836 but which we can accept as lvalues.
7838 If ARG is not a kind of expression we can handle, return
7842 unary_complex_lvalue (enum tree_code code
, tree arg
)
7844 /* Inside a template, making these kinds of adjustments is
7845 pointless; we are only concerned with the type of the
7847 if (processing_template_decl
)
7850 /* Handle (a, b) used as an "lvalue". */
7851 if (TREE_CODE (arg
) == COMPOUND_EXPR
)
7853 tree real_result
= cp_build_unary_op (code
, TREE_OPERAND (arg
, 1), false,
7854 tf_warning_or_error
);
7855 return build2 (COMPOUND_EXPR
, TREE_TYPE (real_result
),
7856 TREE_OPERAND (arg
, 0), real_result
);
7859 /* Handle (a ? b : c) used as an "lvalue". */
7860 if (TREE_CODE (arg
) == COND_EXPR
7861 || TREE_CODE (arg
) == MIN_EXPR
|| TREE_CODE (arg
) == MAX_EXPR
)
7862 return rationalize_conditional_expr (code
, arg
, tf_warning_or_error
);
7864 /* Handle (a = b), (++a), and (--a) used as an "lvalue". */
7865 if (TREE_CODE (arg
) == MODIFY_EXPR
7866 || TREE_CODE (arg
) == PREINCREMENT_EXPR
7867 || TREE_CODE (arg
) == PREDECREMENT_EXPR
)
7868 return unary_complex_lvalue (code
, genericize_compound_lvalue (arg
));
7870 if (code
!= ADDR_EXPR
)
7873 /* Handle (a = b) used as an "lvalue" for `&'. */
7874 if (TREE_CODE (arg
) == MODIFY_EXPR
7875 || TREE_CODE (arg
) == INIT_EXPR
)
7877 tree real_result
= cp_build_unary_op (code
, TREE_OPERAND (arg
, 0), false,
7878 tf_warning_or_error
);
7879 arg
= build2 (COMPOUND_EXPR
, TREE_TYPE (real_result
),
7881 suppress_warning (arg
/* What warning? */);
7885 if (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (arg
))
7886 || TREE_CODE (arg
) == OFFSET_REF
)
7889 /* We permit compiler to make function calls returning
7890 objects of aggregate type look like lvalues. */
7894 if (TREE_CODE (targ
) == SAVE_EXPR
)
7895 targ
= TREE_OPERAND (targ
, 0);
7897 if (TREE_CODE (targ
) == CALL_EXPR
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (targ
)))
7899 if (TREE_CODE (arg
) == SAVE_EXPR
)
7902 targ
= build_cplus_new (TREE_TYPE (arg
), arg
, tf_warning_or_error
);
7903 return build1 (ADDR_EXPR
, build_pointer_type (TREE_TYPE (arg
)), targ
);
7906 if (TREE_CODE (arg
) == SAVE_EXPR
&& INDIRECT_REF_P (targ
))
7907 return build3 (SAVE_EXPR
, build_pointer_type (TREE_TYPE (arg
)),
7908 TREE_OPERAND (targ
, 0), current_function_decl
, NULL
);
7911 /* Don't let anything else be handled specially. */
7915 /* Mark EXP saying that we need to be able to take the
7916 address of it; it should not be allocated in a register.
7917 Value is true if successful. ARRAY_REF_P is true if this
7918 is for ARRAY_REF construction - in that case we don't want
7919 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
7920 it is fine to use ARRAY_REFs for vector subscripts on vector
7923 C++: we do not allow `current_class_ptr' to be addressable. */
7926 cxx_mark_addressable (tree exp
, bool array_ref_p
)
7931 switch (TREE_CODE (x
))
7933 case VIEW_CONVERT_EXPR
:
7935 && TREE_CODE (TREE_TYPE (x
)) == ARRAY_TYPE
7936 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x
, 0))))
7938 x
= TREE_OPERAND (x
, 0);
7943 error ("attempt to take address of bit-field");
7949 x
= TREE_OPERAND (x
, 0);
7953 if (x
== current_class_ptr
)
7955 error ("cannot take the address of %<this%>, which is an rvalue expression");
7956 TREE_ADDRESSABLE (x
) = 1; /* so compiler doesn't die later. */
7962 /* Caller should not be trying to mark initialized
7963 constant fields addressable. */
7964 gcc_assert (DECL_LANG_SPECIFIC (x
) == 0
7965 || DECL_IN_AGGR_P (x
) == 0
7967 || DECL_EXTERNAL (x
));
7971 if (DECL_REGISTER (x
) && !TREE_ADDRESSABLE (x
)
7972 && !DECL_ARTIFICIAL (x
))
7974 if (VAR_P (x
) && DECL_HARD_REGISTER (x
))
7977 ("address of explicit register variable %qD requested", x
);
7980 else if (extra_warnings
)
7982 (OPT_Wextra
, "address requested for %qD, which is declared %<register%>", x
);
7984 TREE_ADDRESSABLE (x
) = 1;
7989 TREE_ADDRESSABLE (x
) = 1;
7993 TREE_ADDRESSABLE (x
) = 1;
7997 TREE_ADDRESSABLE (x
) = 1;
7998 cxx_mark_addressable (TARGET_EXPR_SLOT (x
));
8006 /* Build and return a conditional expression IFEXP ? OP1 : OP2. */
8009 build_x_conditional_expr (location_t loc
, tree ifexp
, tree op1
, tree op2
,
8010 tsubst_flags_t complain
)
8012 tree orig_ifexp
= ifexp
;
8013 tree orig_op1
= op1
;
8014 tree orig_op2
= op2
;
8017 if (processing_template_decl
)
8019 /* The standard says that the expression is type-dependent if
8020 IFEXP is type-dependent, even though the eventual type of the
8021 expression doesn't dependent on IFEXP. */
8022 if (type_dependent_expression_p (ifexp
)
8023 /* As a GNU extension, the middle operand may be omitted. */
8024 || (op1
&& type_dependent_expression_p (op1
))
8025 || type_dependent_expression_p (op2
))
8026 return build_min_nt_loc (loc
, COND_EXPR
, ifexp
, op1
, op2
);
8029 expr
= build_conditional_expr (loc
, ifexp
, op1
, op2
, complain
);
8030 if (processing_template_decl
&& expr
!= error_mark_node
)
8032 tree min
= build_min_non_dep (COND_EXPR
, expr
,
8033 orig_ifexp
, orig_op1
, orig_op2
);
8034 expr
= convert_from_reference (min
);
8039 /* Given a list of expressions, return a compound expression
8040 that performs them all and returns the value of the last of them. */
8043 build_x_compound_expr_from_list (tree list
, expr_list_kind exp
,
8044 tsubst_flags_t complain
)
8046 tree expr
= TREE_VALUE (list
);
8048 if (BRACE_ENCLOSED_INITIALIZER_P (expr
)
8049 && !CONSTRUCTOR_IS_DIRECT_INIT (expr
))
8051 if (complain
& tf_error
)
8052 pedwarn (cp_expr_loc_or_input_loc (expr
), 0,
8053 "list-initializer for non-class type must not "
8054 "be parenthesized");
8056 return error_mark_node
;
8059 if (TREE_CHAIN (list
))
8061 if (complain
& tf_error
)
8065 permerror (input_location
, "expression list treated as compound "
8066 "expression in initializer");
8069 permerror (input_location
, "expression list treated as compound "
8070 "expression in mem-initializer");
8073 permerror (input_location
, "expression list treated as compound "
8074 "expression in functional cast");
8080 return error_mark_node
;
8082 for (list
= TREE_CHAIN (list
); list
; list
= TREE_CHAIN (list
))
8083 expr
= build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list
)),
8084 expr
, TREE_VALUE (list
), NULL_TREE
,
8091 /* Like build_x_compound_expr_from_list, but using a VEC. */
8094 build_x_compound_expr_from_vec (vec
<tree
, va_gc
> *vec
, const char *msg
,
8095 tsubst_flags_t complain
)
8097 if (vec_safe_is_empty (vec
))
8099 else if (vec
->length () == 1)
8109 if (complain
& tf_error
)
8110 permerror (input_location
,
8111 "%s expression list treated as compound expression",
8114 return error_mark_node
;
8118 for (ix
= 1; vec
->iterate (ix
, &t
); ++ix
)
8119 expr
= build_x_compound_expr (EXPR_LOCATION (t
), expr
,
8120 t
, NULL_TREE
, complain
);
8126 /* Handle overloading of the ',' operator when needed. */
8129 build_x_compound_expr (location_t loc
, tree op1
, tree op2
,
8130 tree lookups
, tsubst_flags_t complain
)
8133 tree orig_op1
= op1
;
8134 tree orig_op2
= op2
;
8135 tree overload
= NULL_TREE
;
8137 if (processing_template_decl
)
8139 if (type_dependent_expression_p (op1
)
8140 || type_dependent_expression_p (op2
))
8142 result
= build_min_nt_loc (loc
, COMPOUND_EXPR
, op1
, op2
);
8144 = build_dependent_operator_type (lookups
, COMPOUND_EXPR
, false);
8149 result
= build_new_op (loc
, COMPOUND_EXPR
, LOOKUP_NORMAL
, op1
, op2
,
8150 NULL_TREE
, lookups
, &overload
, complain
);
8152 result
= cp_build_compound_expr (op1
, op2
, complain
);
8154 if (processing_template_decl
&& result
!= error_mark_node
)
8156 if (overload
!= NULL_TREE
)
8157 return (build_min_non_dep_op_overload
8158 (COMPOUND_EXPR
, result
, overload
, orig_op1
, orig_op2
));
8160 return build_min_non_dep (COMPOUND_EXPR
, result
, orig_op1
, orig_op2
);
8166 /* Like cp_build_compound_expr, but for the c-common bits. */
8169 build_compound_expr (location_t
/*loc*/, tree lhs
, tree rhs
)
8171 return cp_build_compound_expr (lhs
, rhs
, tf_warning_or_error
);
8174 /* Build a compound expression. */
8177 cp_build_compound_expr (tree lhs
, tree rhs
, tsubst_flags_t complain
)
8179 lhs
= convert_to_void (lhs
, ICV_LEFT_OF_COMMA
, complain
);
8181 if (lhs
== error_mark_node
|| rhs
== error_mark_node
)
8182 return error_mark_node
;
8184 if (TREE_CODE (lhs
) == EXCESS_PRECISION_EXPR
)
8185 lhs
= TREE_OPERAND (lhs
, 0);
8186 tree eptype
= NULL_TREE
;
8187 if (TREE_CODE (rhs
) == EXCESS_PRECISION_EXPR
)
8189 eptype
= TREE_TYPE (rhs
);
8190 rhs
= TREE_OPERAND (rhs
, 0);
8193 if (TREE_CODE (rhs
) == TARGET_EXPR
)
8195 /* If the rhs is a TARGET_EXPR, then build the compound
8196 expression inside the target_expr's initializer. This
8197 helps the compiler to eliminate unnecessary temporaries. */
8198 tree init
= TARGET_EXPR_INITIAL (rhs
);
8200 init
= build2 (COMPOUND_EXPR
, TREE_TYPE (init
), lhs
, init
);
8201 TARGET_EXPR_INITIAL (rhs
) = init
;
8204 rhs
= build1 (EXCESS_PRECISION_EXPR
, eptype
, rhs
);
8208 rhs
= resolve_nondeduced_context (rhs
, complain
);
8210 if (type_unknown_p (rhs
))
8212 if (complain
& tf_error
)
8213 error_at (cp_expr_loc_or_input_loc (rhs
),
8214 "no context to resolve type of %qE", rhs
);
8215 return error_mark_node
;
8218 tree ret
= build2 (COMPOUND_EXPR
, TREE_TYPE (rhs
), lhs
, rhs
);
8220 ret
= build1 (EXCESS_PRECISION_EXPR
, eptype
, ret
);
8224 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
8225 casts away constness. CAST gives the type of cast. Returns true
8226 if the cast is ill-formed, false if it is well-formed.
8228 ??? This function warns for casting away any qualifier not just
8229 const. We would like to specify exactly what qualifiers are casted
8234 check_for_casting_away_constness (location_t loc
, tree src_type
,
8235 tree dest_type
, enum tree_code cast
,
8236 tsubst_flags_t complain
)
8238 /* C-style casts are allowed to cast away constness. With
8239 WARN_CAST_QUAL, we still want to issue a warning. */
8240 if (cast
== CAST_EXPR
&& !warn_cast_qual
)
8243 if (!casts_away_constness (src_type
, dest_type
, complain
))
8249 if (complain
& tf_warning
)
8250 warning_at (loc
, OPT_Wcast_qual
,
8251 "cast from type %qT to type %qT casts away qualifiers",
8252 src_type
, dest_type
);
8255 case STATIC_CAST_EXPR
:
8256 if (complain
& tf_error
)
8257 error_at (loc
, "%<static_cast%> from type %qT to type %qT casts "
8259 src_type
, dest_type
);
8262 case REINTERPRET_CAST_EXPR
:
8263 if (complain
& tf_error
)
8264 error_at (loc
, "%<reinterpret_cast%> from type %qT to type %qT "
8265 "casts away qualifiers",
8266 src_type
, dest_type
);
8274 /* Warns if the cast from expression EXPR to type TYPE is useless. */
8276 maybe_warn_about_useless_cast (location_t loc
, tree type
, tree expr
,
8277 tsubst_flags_t complain
)
8279 if (warn_useless_cast
8280 && complain
& tf_warning
)
8282 if (TYPE_REF_P (type
)
8283 ? ((TYPE_REF_IS_RVALUE (type
)
8284 ? xvalue_p (expr
) : lvalue_p (expr
))
8285 && same_type_p (TREE_TYPE (expr
), TREE_TYPE (type
)))
8286 /* Don't warn when converting a class object to a non-reference type,
8287 because that's a common way to create a temporary. */
8288 : (!glvalue_p (expr
) && same_type_p (TREE_TYPE (expr
), type
)))
8289 warning_at (loc
, OPT_Wuseless_cast
,
8290 "useless cast to type %q#T", type
);
8294 /* Warns if the cast ignores cv-qualifiers on TYPE. */
8296 maybe_warn_about_cast_ignoring_quals (location_t loc
, tree type
,
8297 tsubst_flags_t complain
)
8299 if (warn_ignored_qualifiers
8300 && complain
& tf_warning
8301 && !CLASS_TYPE_P (type
)
8302 && (cp_type_quals (type
) & (TYPE_QUAL_CONST
|TYPE_QUAL_VOLATILE
)))
8303 warning_at (loc
, OPT_Wignored_qualifiers
,
8304 "type qualifiers ignored on cast result type");
8307 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
8308 (another pointer-to-member type in the same hierarchy) and return
8309 the converted expression. If ALLOW_INVERSE_P is permitted, a
8310 pointer-to-derived may be converted to pointer-to-base; otherwise,
8311 only the other direction is permitted. If C_CAST_P is true, this
8312 conversion is taking place as part of a C-style cast. */
8315 convert_ptrmem (tree type
, tree expr
, bool allow_inverse_p
,
8316 bool c_cast_p
, tsubst_flags_t complain
)
8318 if (same_type_p (type
, TREE_TYPE (expr
)))
8321 if (TYPE_PTRDATAMEM_P (type
))
8323 tree obase
= TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr
));
8324 tree nbase
= TYPE_PTRMEM_CLASS_TYPE (type
);
8325 tree delta
= (get_delta_difference
8327 allow_inverse_p
, c_cast_p
, complain
));
8329 if (delta
== error_mark_node
)
8330 return error_mark_node
;
8332 if (!same_type_p (obase
, nbase
))
8334 if (TREE_CODE (expr
) == PTRMEM_CST
)
8335 expr
= cplus_expand_constant (expr
);
8337 tree cond
= cp_build_binary_op (input_location
, EQ_EXPR
, expr
,
8338 build_int_cst (TREE_TYPE (expr
), -1),
8340 tree op1
= build_nop (ptrdiff_type_node
, expr
);
8341 tree op2
= cp_build_binary_op (input_location
, PLUS_EXPR
, op1
, delta
,
8344 expr
= fold_build3_loc (input_location
,
8345 COND_EXPR
, ptrdiff_type_node
, cond
, op1
, op2
);
8348 return build_nop (type
, expr
);
8351 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type
), expr
,
8352 allow_inverse_p
, c_cast_p
, complain
);
8355 /* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
8356 this static_cast is being attempted as one of the possible casts
8357 allowed by a C-style cast. (In that case, accessibility of base
8358 classes is not considered, and it is OK to cast away
8359 constness.) Return the result of the cast. *VALID_P is set to
8360 indicate whether or not the cast was valid. */
8363 build_static_cast_1 (location_t loc
, tree type
, tree expr
, bool c_cast_p
,
8364 bool *valid_p
, tsubst_flags_t complain
)
8370 /* Assume the cast is valid. */
8373 intype
= unlowered_expr_type (expr
);
8375 /* Save casted types in the function's used types hash table. */
8376 used_types_insert (type
);
8378 /* A prvalue of non-class type is cv-unqualified. */
8379 if (!CLASS_TYPE_P (type
))
8380 type
= cv_unqualified (type
);
8382 /* [expr.static.cast]
8384 An lvalue of type "cv1 B", where B is a class type, can be cast
8385 to type "reference to cv2 D", where D is a class derived (clause
8386 _class.derived_) from B, if a valid standard conversion from
8387 "pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
8388 same cv-qualification as, or greater cv-qualification than, cv1,
8389 and B is not a virtual base class of D. */
8390 /* We check this case before checking the validity of "TYPE t =
8391 EXPR;" below because for this case:
8394 struct D : public B { D(const B&); };
8396 void f() { static_cast<const D&>(b); }
8398 we want to avoid constructing a new D. The standard is not
8399 completely clear about this issue, but our interpretation is
8400 consistent with other compilers. */
8401 if (TYPE_REF_P (type
)
8402 && CLASS_TYPE_P (TREE_TYPE (type
))
8403 && CLASS_TYPE_P (intype
)
8404 && (TYPE_REF_IS_RVALUE (type
) || lvalue_p (expr
))
8405 && DERIVED_FROM_P (intype
, TREE_TYPE (type
))
8406 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype
)),
8407 build_pointer_type (TYPE_MAIN_VARIANT
8408 (TREE_TYPE (type
))),
8411 || at_least_as_qualified_p (TREE_TYPE (type
), intype
)))
8415 if (processing_template_decl
)
8418 /* There is a standard conversion from "D*" to "B*" even if "B"
8419 is ambiguous or inaccessible. If this is really a
8420 static_cast, then we check both for inaccessibility and
8421 ambiguity. However, if this is a static_cast being performed
8422 because the user wrote a C-style cast, then accessibility is
8424 base
= lookup_base (TREE_TYPE (type
), intype
,
8425 c_cast_p
? ba_unique
: ba_check
,
8427 expr
= cp_build_addr_expr (expr
, complain
);
8429 if (sanitize_flags_p (SANITIZE_VPTR
))
8432 = cp_ubsan_maybe_instrument_downcast (loc
, type
,
8438 /* Convert from "B*" to "D*". This function will check that "B"
8439 is not a virtual base of "D". Even if we don't have a guarantee
8440 that expr is NULL, if the static_cast is to a reference type,
8441 it is UB if it would be NULL, so omit the non-NULL check. */
8442 expr
= build_base_path (MINUS_EXPR
, expr
, base
,
8443 /*nonnull=*/flag_delete_null_pointer_checks
,
8446 /* Convert the pointer to a reference -- but then remember that
8447 there are no expressions with reference type in C++.
8449 We call rvalue so that there's an actual tree code
8450 (NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
8451 is a variable with the same type, the conversion would get folded
8452 away, leaving just the variable and causing lvalue_kind to give
8453 the wrong answer. */
8454 expr
= cp_fold_convert (type
, expr
);
8456 /* When -fsanitize=null, make sure to diagnose reference binding to
8457 NULL even when the reference is converted to pointer later on. */
8458 if (sanitize_flags_p (SANITIZE_NULL
)
8459 && TREE_CODE (expr
) == COND_EXPR
8460 && TREE_OPERAND (expr
, 2)
8461 && TREE_CODE (TREE_OPERAND (expr
, 2)) == INTEGER_CST
8462 && TREE_TYPE (TREE_OPERAND (expr
, 2)) == type
)
8463 ubsan_maybe_instrument_reference (&TREE_OPERAND (expr
, 2));
8465 return convert_from_reference (rvalue (expr
));
8468 /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
8469 cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
8470 if (TYPE_REF_P (type
)
8471 && TYPE_REF_IS_RVALUE (type
)
8472 && (clk
= real_lvalue_p (expr
))
8473 && reference_compatible_p (TREE_TYPE (type
), intype
)
8474 && (c_cast_p
|| at_least_as_qualified_p (TREE_TYPE (type
), intype
)))
8476 if (processing_template_decl
)
8478 if (clk
== clk_ordinary
)
8480 /* Handle the (non-bit-field) lvalue case here by casting to
8481 lvalue reference and then changing it to an rvalue reference.
8482 Casting an xvalue to rvalue reference will be handled by the
8484 tree lref
= cp_build_reference_type (TREE_TYPE (type
), false);
8485 result
= (perform_direct_initialization_if_possible
8486 (lref
, expr
, c_cast_p
, complain
));
8487 result
= build1 (NON_LVALUE_EXPR
, type
, result
);
8488 return convert_from_reference (result
);
8491 /* For a bit-field or packed field, bind to a temporary. */
8492 expr
= rvalue (expr
);
8495 /* Resolve overloaded address here rather than once in
8496 implicit_conversion and again in the inverse code below. */
8497 if (TYPE_PTRMEMFUNC_P (type
) && type_unknown_p (expr
))
8499 expr
= instantiate_type (type
, expr
, complain
);
8500 intype
= TREE_TYPE (expr
);
8503 /* [expr.static.cast]
8505 Any expression can be explicitly converted to type cv void. */
8506 if (VOID_TYPE_P (type
))
8508 if (TREE_CODE (expr
) == EXCESS_PRECISION_EXPR
)
8509 expr
= TREE_OPERAND (expr
, 0);
8510 return convert_to_void (expr
, ICV_CAST
, complain
);
8514 An abstract class shall not be used ... as the type of an explicit
8516 if (abstract_virtuals_error (ACU_CAST
, type
, complain
))
8517 return error_mark_node
;
8519 /* [expr.static.cast]
8521 An expression e can be explicitly converted to a type T using a
8522 static_cast of the form static_cast<T>(e) if the declaration T
8523 t(e);" is well-formed, for some invented temporary variable
8525 result
= perform_direct_initialization_if_possible (type
, expr
,
8526 c_cast_p
, complain
);
8527 /* P1975 allows static_cast<Aggr>(42), as well as static_cast<T[5]>(42),
8528 which initialize the first element of the aggregate. We need to handle
8529 the array case specifically. */
8530 if (result
== NULL_TREE
8531 && cxx_dialect
>= cxx20
8532 && TREE_CODE (type
) == ARRAY_TYPE
)
8534 /* Create { EXPR } and perform direct-initialization from it. */
8535 tree e
= build_constructor_single (init_list_type_node
, NULL_TREE
, expr
);
8536 CONSTRUCTOR_IS_DIRECT_INIT (e
) = true;
8537 CONSTRUCTOR_IS_PAREN_INIT (e
) = true;
8538 result
= perform_direct_initialization_if_possible (type
, e
, c_cast_p
,
8543 if (processing_template_decl
)
8546 result
= convert_from_reference (result
);
8548 /* [expr.static.cast]
8550 If T is a reference type, the result is an lvalue; otherwise,
8551 the result is an rvalue. */
8552 if (!TYPE_REF_P (type
))
8554 result
= rvalue (result
);
8556 if (result
== expr
&& SCALAR_TYPE_P (type
))
8557 /* Leave some record of the cast. */
8558 result
= build_nop (type
, expr
);
8563 /* [expr.static.cast]
8565 The inverse of any standard conversion sequence (clause _conv_),
8566 other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
8567 (_conv.array_), function-to-pointer (_conv.func_), and boolean
8568 (_conv.bool_) conversions, can be performed explicitly using
8569 static_cast subject to the restriction that the explicit
8570 conversion does not cast away constness (_expr.const.cast_), and
8571 the following additional rules for specific cases: */
8572 /* For reference, the conversions not excluded are: integral
8573 promotions, floating-point promotion, integral conversions,
8574 floating-point conversions, floating-integral conversions,
8575 pointer conversions, and pointer to member conversions. */
8578 A value of integral _or enumeration_ type can be explicitly
8579 converted to an enumeration type. */
8580 /* The effect of all that is that any conversion between any two
8581 types which are integral, floating, or enumeration types can be
8583 if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
8584 || SCALAR_FLOAT_TYPE_P (type
))
8585 && (INTEGRAL_OR_ENUMERATION_TYPE_P (intype
)
8586 || SCALAR_FLOAT_TYPE_P (intype
)))
8588 if (processing_template_decl
)
8590 if (TREE_CODE (expr
) == EXCESS_PRECISION_EXPR
)
8591 expr
= TREE_OPERAND (expr
, 0);
8592 /* [expr.static.cast]: "If the value is not a bit-field, the result
8593 refers to the object or the specified base class subobject thereof;
8594 otherwise, the lvalue-to-rvalue conversion is applied to the
8595 bit-field and the resulting prvalue is used as the operand of the
8596 static_cast." There are no prvalue bit-fields; the l-to-r conversion
8597 will give us an object of the underlying type of the bit-field. */
8598 expr
= decay_conversion (expr
, complain
);
8599 return ocp_convert (type
, expr
, CONV_C_CAST
, LOOKUP_NORMAL
, complain
);
8602 if (TYPE_PTR_P (type
) && TYPE_PTR_P (intype
)
8603 && CLASS_TYPE_P (TREE_TYPE (type
))
8604 && CLASS_TYPE_P (TREE_TYPE (intype
))
8605 && can_convert (build_pointer_type (TYPE_MAIN_VARIANT
8606 (TREE_TYPE (intype
))),
8607 build_pointer_type (TYPE_MAIN_VARIANT
8608 (TREE_TYPE (type
))),
8613 if (processing_template_decl
)
8617 && check_for_casting_away_constness (loc
, intype
, type
,
8620 return error_mark_node
;
8621 base
= lookup_base (TREE_TYPE (type
), TREE_TYPE (intype
),
8622 c_cast_p
? ba_unique
: ba_check
,
8624 expr
= build_base_path (MINUS_EXPR
, expr
, base
, /*nonnull=*/false,
8627 if (sanitize_flags_p (SANITIZE_VPTR
))
8630 = cp_ubsan_maybe_instrument_downcast (loc
, type
,
8636 return cp_fold_convert (type
, expr
);
8639 if ((TYPE_PTRDATAMEM_P (type
) && TYPE_PTRDATAMEM_P (intype
))
8640 || (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
)))
8647 c1
= TYPE_PTRMEM_CLASS_TYPE (intype
);
8648 c2
= TYPE_PTRMEM_CLASS_TYPE (type
);
8650 if (TYPE_PTRDATAMEM_P (type
))
8652 t1
= (build_ptrmem_type
8654 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype
))));
8655 t2
= (build_ptrmem_type
8657 TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type
))));
8664 if (can_convert (t1
, t2
, complain
) || can_convert (t2
, t1
, complain
))
8667 && check_for_casting_away_constness (loc
, intype
, type
,
8670 return error_mark_node
;
8671 if (processing_template_decl
)
8673 return convert_ptrmem (type
, expr
, /*allow_inverse_p=*/1,
8674 c_cast_p
, complain
);
8678 /* [expr.static.cast]
8680 An rvalue of type "pointer to cv void" can be explicitly
8681 converted to a pointer to object type. A value of type pointer
8682 to object converted to "pointer to cv void" and back to the
8683 original pointer type will have its original value. */
8684 if (TYPE_PTR_P (intype
)
8685 && VOID_TYPE_P (TREE_TYPE (intype
))
8686 && TYPE_PTROB_P (type
))
8689 && check_for_casting_away_constness (loc
, intype
, type
,
8692 return error_mark_node
;
8693 if (processing_template_decl
)
8695 return build_nop (type
, expr
);
8699 return error_mark_node
;
8702 /* Return an expression representing static_cast<TYPE>(EXPR). */
8705 build_static_cast (location_t loc
, tree type
, tree oexpr
,
8706 tsubst_flags_t complain
)
8712 if (type
== error_mark_node
|| expr
== error_mark_node
)
8713 return error_mark_node
;
8715 bool dependent
= (dependent_type_p (type
)
8716 || type_dependent_expression_p (expr
));
8720 expr
= build_min (STATIC_CAST_EXPR
, type
, oexpr
);
8721 /* We don't know if it will or will not have side effects. */
8722 TREE_SIDE_EFFECTS (expr
) = 1;
8723 result
= convert_from_reference (expr
);
8724 protected_set_expr_location (result
, loc
);
8728 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8729 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
8730 if (!TYPE_REF_P (type
)
8731 && TREE_CODE (expr
) == NOP_EXPR
8732 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
8733 expr
= TREE_OPERAND (expr
, 0);
8735 result
= build_static_cast_1 (loc
, type
, expr
, /*c_cast_p=*/false,
8736 &valid_p
, complain
);
8739 if (result
!= error_mark_node
)
8741 maybe_warn_about_useless_cast (loc
, type
, expr
, complain
);
8742 maybe_warn_about_cast_ignoring_quals (loc
, type
, complain
);
8744 if (processing_template_decl
)
8746 protected_set_expr_location (result
, loc
);
8750 if (complain
& tf_error
)
8752 auto_diagnostic_group d
;
8753 error_at (loc
, "invalid %<static_cast%> from type %qT to type %qT",
8754 TREE_TYPE (expr
), type
);
8755 if ((TYPE_PTR_P (type
) || TYPE_REF_P (type
))
8756 && CLASS_TYPE_P (TREE_TYPE (type
))
8757 && !COMPLETE_TYPE_P (TREE_TYPE (type
)))
8758 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (TREE_TYPE (type
))),
8759 "class type %qT is incomplete", TREE_TYPE (type
));
8760 tree expr_type
= TREE_TYPE (expr
);
8761 if (TYPE_PTR_P (expr_type
))
8762 expr_type
= TREE_TYPE (expr_type
);
8763 if (CLASS_TYPE_P (expr_type
) && !COMPLETE_TYPE_P (expr_type
))
8764 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (expr_type
)),
8765 "class type %qT is incomplete", expr_type
);
8767 return error_mark_node
;
8770 /* EXPR is an expression with member function or pointer-to-member
8771 function type. TYPE is a pointer type. Converting EXPR to TYPE is
8772 not permitted by ISO C++, but we accept it in some modes. If we
8773 are not in one of those modes, issue a diagnostic. Return the
8774 converted expression. */
8777 convert_member_func_to_ptr (tree type
, tree expr
, tsubst_flags_t complain
)
8782 intype
= TREE_TYPE (expr
);
8783 gcc_assert (TYPE_PTRMEMFUNC_P (intype
)
8784 || TREE_CODE (intype
) == METHOD_TYPE
);
8786 if (!(complain
& tf_warning_or_error
))
8787 return error_mark_node
;
8789 location_t loc
= cp_expr_loc_or_input_loc (expr
);
8791 if (pedantic
|| warn_pmf2ptr
)
8792 pedwarn (loc
, pedantic
? OPT_Wpedantic
: OPT_Wpmf_conversions
,
8793 "converting from %qH to %qI", intype
, type
);
8795 STRIP_ANY_LOCATION_WRAPPER (expr
);
8797 if (TREE_CODE (intype
) == METHOD_TYPE
)
8798 expr
= build_addr_func (expr
, complain
);
8799 else if (TREE_CODE (expr
) == PTRMEM_CST
)
8800 expr
= build_address (PTRMEM_CST_MEMBER (expr
));
8803 decl
= maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype
), 0);
8804 decl
= build_address (decl
);
8805 expr
= get_member_function_from_ptrfunc (&decl
, expr
, complain
);
8808 if (expr
== error_mark_node
)
8809 return error_mark_node
;
8811 expr
= build_nop (type
, expr
);
8812 SET_EXPR_LOCATION (expr
, loc
);
8816 /* Build a NOP_EXPR to TYPE, but mark it as a reinterpret_cast so that
8817 constexpr evaluation knows to reject it. */
8820 build_nop_reinterpret (tree type
, tree expr
)
8822 tree ret
= build_nop (type
, expr
);
8824 REINTERPRET_CAST_P (ret
) = true;
8828 /* Return a representation for a reinterpret_cast from EXPR to TYPE.
8829 If C_CAST_P is true, this reinterpret cast is being done as part of
8830 a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
8831 indicate whether or not reinterpret_cast was valid. */
8834 build_reinterpret_cast_1 (location_t loc
, tree type
, tree expr
,
8835 bool c_cast_p
, bool *valid_p
,
8836 tsubst_flags_t complain
)
8840 /* Assume the cast is invalid. */
8844 if (type
== error_mark_node
|| error_operand_p (expr
))
8845 return error_mark_node
;
8847 intype
= TREE_TYPE (expr
);
8849 /* Save casted types in the function's used types hash table. */
8850 used_types_insert (type
);
8852 /* A prvalue of non-class type is cv-unqualified. */
8853 if (!CLASS_TYPE_P (type
))
8854 type
= cv_unqualified (type
);
8856 /* [expr.reinterpret.cast]
8857 A glvalue of type T1, designating an object x, can be cast to the type
8858 "reference to T2" if an expression of type "pointer to T1" can be
8859 explicitly converted to the type "pointer to T2" using a reinterpret_cast.
8860 The result is that of *reinterpret_cast<T2 *>(p) where p is a pointer to x
8861 of type "pointer to T1". No temporary is created, no copy is made, and no
8862 constructors (11.4.4) or conversion functions (11.4.7) are called. */
8863 if (TYPE_REF_P (type
))
8865 if (!glvalue_p (expr
))
8867 if (complain
& tf_error
)
8868 error_at (loc
, "invalid cast of a prvalue expression of type "
8871 return error_mark_node
;
8874 /* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
8875 "B" are related class types; the reinterpret_cast does not
8876 adjust the pointer. */
8877 if (TYPE_PTR_P (intype
)
8878 && (complain
& tf_warning
)
8879 && (comptypes (TREE_TYPE (intype
), TREE_TYPE (type
),
8880 COMPARE_BASE
| COMPARE_DERIVED
)))
8881 warning_at (loc
, 0, "casting %qT to %qT does not dereference pointer",
8884 expr
= cp_build_addr_expr (expr
, complain
);
8886 if (warn_strict_aliasing
> 2)
8887 cp_strict_aliasing_warning (EXPR_LOCATION (expr
), type
, expr
);
8889 if (expr
!= error_mark_node
)
8890 expr
= build_reinterpret_cast_1
8891 (loc
, build_pointer_type (TREE_TYPE (type
)), expr
, c_cast_p
,
8893 if (expr
!= error_mark_node
)
8894 /* cp_build_indirect_ref isn't right for rvalue refs. */
8895 expr
= convert_from_reference (fold_convert (type
, expr
));
8899 /* As a G++ extension, we consider conversions from member
8900 functions, and pointers to member functions to
8901 pointer-to-function and pointer-to-void types. If
8902 -Wno-pmf-conversions has not been specified,
8903 convert_member_func_to_ptr will issue an error message. */
8904 if ((TYPE_PTRMEMFUNC_P (intype
)
8905 || TREE_CODE (intype
) == METHOD_TYPE
)
8906 && TYPE_PTR_P (type
)
8907 && (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
8908 || VOID_TYPE_P (TREE_TYPE (type
))))
8909 return convert_member_func_to_ptr (type
, expr
, complain
);
8911 /* If the cast is not to a reference type, the lvalue-to-rvalue,
8912 array-to-pointer, and function-to-pointer conversions are
8914 expr
= decay_conversion (expr
, complain
);
8916 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
8917 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
8918 if (TREE_CODE (expr
) == NOP_EXPR
8919 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
8920 expr
= TREE_OPERAND (expr
, 0);
8922 if (error_operand_p (expr
))
8923 return error_mark_node
;
8925 intype
= TREE_TYPE (expr
);
8927 /* [expr.reinterpret.cast]
8928 A pointer can be converted to any integral type large enough to
8929 hold it. ... A value of type std::nullptr_t can be converted to
8930 an integral type; the conversion has the same meaning and
8931 validity as a conversion of (void*)0 to the integral type. */
8932 if (CP_INTEGRAL_TYPE_P (type
)
8933 && (TYPE_PTR_P (intype
) || NULLPTR_TYPE_P (intype
)))
8935 if (TYPE_PRECISION (type
) < TYPE_PRECISION (intype
))
8937 if (complain
& tf_error
)
8938 permerror (loc
, "cast from %qH to %qI loses precision",
8941 return error_mark_node
;
8943 if (NULLPTR_TYPE_P (intype
))
8944 return build_int_cst (type
, 0);
8946 /* [expr.reinterpret.cast]
8947 A value of integral or enumeration type can be explicitly
8948 converted to a pointer. */
8949 else if (TYPE_PTR_P (type
) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype
))
8952 else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type
)
8953 || TYPE_PTR_OR_PTRMEM_P (type
))
8954 && same_type_p (type
, intype
))
8956 return rvalue (expr
);
8957 else if (TYPE_PTRFN_P (type
) && TYPE_PTRFN_P (intype
))
8959 if ((complain
& tf_warning
)
8960 && !cxx_safe_function_type_cast_p (TREE_TYPE (type
),
8961 TREE_TYPE (intype
)))
8962 warning_at (loc
, OPT_Wcast_function_type
,
8963 "cast between incompatible function types"
8964 " from %qH to %qI", intype
, type
);
8965 return build_nop_reinterpret (type
, expr
);
8967 else if (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
))
8969 if ((complain
& tf_warning
)
8970 && !cxx_safe_function_type_cast_p
8971 (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (type
)),
8972 TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (intype
))))
8973 warning_at (loc
, OPT_Wcast_function_type
,
8974 "cast between incompatible pointer to member types"
8975 " from %qH to %qI", intype
, type
);
8976 return build_nop_reinterpret (type
, expr
);
8978 else if ((TYPE_PTRDATAMEM_P (type
) && TYPE_PTRDATAMEM_P (intype
))
8979 || (TYPE_PTROBV_P (type
) && TYPE_PTROBV_P (intype
)))
8982 && check_for_casting_away_constness (loc
, intype
, type
,
8983 REINTERPRET_CAST_EXPR
,
8985 return error_mark_node
;
8986 /* Warn about possible alignment problems. */
8987 if ((STRICT_ALIGNMENT
|| warn_cast_align
== 2)
8988 && (complain
& tf_warning
)
8989 && !VOID_TYPE_P (type
)
8990 && TREE_CODE (TREE_TYPE (intype
)) != FUNCTION_TYPE
8991 && COMPLETE_TYPE_P (TREE_TYPE (type
))
8992 && COMPLETE_TYPE_P (TREE_TYPE (intype
))
8993 && min_align_of_type (TREE_TYPE (type
))
8994 > min_align_of_type (TREE_TYPE (intype
)))
8995 warning_at (loc
, OPT_Wcast_align
, "cast from %qH to %qI "
8996 "increases required alignment of target type",
8999 if (warn_strict_aliasing
<= 2)
9000 /* strict_aliasing_warning STRIP_NOPs its expr. */
9001 cp_strict_aliasing_warning (EXPR_LOCATION (expr
), type
, expr
);
9003 return build_nop_reinterpret (type
, expr
);
9005 else if ((TYPE_PTRFN_P (type
) && TYPE_PTROBV_P (intype
))
9006 || (TYPE_PTRFN_P (intype
) && TYPE_PTROBV_P (type
)))
9008 if (complain
& tf_warning
)
9009 /* C++11 5.2.10 p8 says that "Converting a function pointer to an
9010 object pointer type or vice versa is conditionally-supported." */
9011 warning_at (loc
, OPT_Wconditionally_supported
,
9012 "casting between pointer-to-function and "
9013 "pointer-to-object is conditionally-supported");
9014 return build_nop_reinterpret (type
, expr
);
9016 else if (gnu_vector_type_p (type
) && scalarish_type_p (intype
))
9017 return convert_to_vector (type
, rvalue (expr
));
9018 else if (gnu_vector_type_p (intype
)
9019 && INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
9020 return convert_to_integer_nofold (type
, expr
);
9025 if (complain
& tf_error
)
9026 error_at (loc
, "invalid cast from type %qT to type %qT",
9028 return error_mark_node
;
9031 expr
= cp_convert (type
, expr
, complain
);
9032 if (TREE_CODE (expr
) == NOP_EXPR
)
9033 /* Mark any nop_expr that created as a reintepret_cast. */
9034 REINTERPRET_CAST_P (expr
) = true;
9039 build_reinterpret_cast (location_t loc
, tree type
, tree expr
,
9040 tsubst_flags_t complain
)
9044 if (type
== error_mark_node
|| expr
== error_mark_node
)
9045 return error_mark_node
;
9047 if (processing_template_decl
)
9049 tree t
= build_min (REINTERPRET_CAST_EXPR
, type
, expr
);
9051 if (!TREE_SIDE_EFFECTS (t
)
9052 && type_dependent_expression_p (expr
))
9053 /* There might turn out to be side effects inside expr. */
9054 TREE_SIDE_EFFECTS (t
) = 1;
9055 r
= convert_from_reference (t
);
9056 protected_set_expr_location (r
, loc
);
9060 r
= build_reinterpret_cast_1 (loc
, type
, expr
, /*c_cast_p=*/false,
9061 /*valid_p=*/NULL
, complain
);
9062 if (r
!= error_mark_node
)
9064 maybe_warn_about_useless_cast (loc
, type
, expr
, complain
);
9065 maybe_warn_about_cast_ignoring_quals (loc
, type
, complain
);
9067 protected_set_expr_location (r
, loc
);
9071 /* Perform a const_cast from EXPR to TYPE. If the cast is valid,
9072 return an appropriate expression. Otherwise, return
9073 error_mark_node. If the cast is not valid, and COMPLAIN is true,
9074 then a diagnostic will be issued. If VALID_P is non-NULL, we are
9075 performing a C-style cast, its value upon return will indicate
9076 whether or not the conversion succeeded. */
9079 build_const_cast_1 (location_t loc
, tree dst_type
, tree expr
,
9080 tsubst_flags_t complain
, bool *valid_p
)
9083 tree reference_type
;
9085 /* Callers are responsible for handling error_mark_node as a
9086 destination type. */
9087 gcc_assert (dst_type
!= error_mark_node
);
9088 /* In a template, callers should be building syntactic
9089 representations of casts, not using this machinery. */
9090 gcc_assert (!processing_template_decl
);
9092 /* Assume the conversion is invalid. */
9096 if (!INDIRECT_TYPE_P (dst_type
) && !TYPE_PTRDATAMEM_P (dst_type
))
9098 if (complain
& tf_error
)
9099 error_at (loc
, "invalid use of %<const_cast%> with type %qT, "
9100 "which is not a pointer, reference, "
9101 "nor a pointer-to-data-member type", dst_type
);
9102 return error_mark_node
;
9105 if (TREE_CODE (TREE_TYPE (dst_type
)) == FUNCTION_TYPE
)
9107 if (complain
& tf_error
)
9108 error_at (loc
, "invalid use of %<const_cast%> with type %qT, "
9109 "which is a pointer or reference to a function type",
9111 return error_mark_node
;
9114 /* A prvalue of non-class type is cv-unqualified. */
9115 dst_type
= cv_unqualified (dst_type
);
9117 /* Save casted types in the function's used types hash table. */
9118 used_types_insert (dst_type
);
9120 src_type
= TREE_TYPE (expr
);
9121 /* Expressions do not really have reference types. */
9122 if (TYPE_REF_P (src_type
))
9123 src_type
= TREE_TYPE (src_type
);
9125 /* [expr.const.cast]
9127 For two object types T1 and T2, if a pointer to T1 can be explicitly
9128 converted to the type "pointer to T2" using a const_cast, then the
9129 following conversions can also be made:
9131 -- an lvalue of type T1 can be explicitly converted to an lvalue of
9132 type T2 using the cast const_cast<T2&>;
9134 -- a glvalue of type T1 can be explicitly converted to an xvalue of
9135 type T2 using the cast const_cast<T2&&>; and
9137 -- if T1 is a class type, a prvalue of type T1 can be explicitly
9138 converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
9140 if (TYPE_REF_P (dst_type
))
9142 reference_type
= dst_type
;
9143 if (!TYPE_REF_IS_RVALUE (dst_type
)
9149 if (complain
& tf_error
)
9150 error_at (loc
, "invalid %<const_cast%> of an rvalue of type %qT "
9152 src_type
, dst_type
);
9153 return error_mark_node
;
9155 dst_type
= build_pointer_type (TREE_TYPE (dst_type
));
9156 src_type
= build_pointer_type (src_type
);
9160 reference_type
= NULL_TREE
;
9161 /* If the destination type is not a reference type, the
9162 lvalue-to-rvalue, array-to-pointer, and function-to-pointer
9163 conversions are performed. */
9164 src_type
= type_decays_to (src_type
);
9165 if (src_type
== error_mark_node
)
9166 return error_mark_node
;
9169 if (TYPE_PTR_P (src_type
) || TYPE_PTRDATAMEM_P (src_type
))
9171 if (comp_ptr_ttypes_const (dst_type
, src_type
, bounds_none
))
9176 /* This cast is actually a C-style cast. Issue a warning if
9177 the user is making a potentially unsafe cast. */
9178 check_for_casting_away_constness (loc
, src_type
, dst_type
,
9179 CAST_EXPR
, complain
);
9180 /* ??? comp_ptr_ttypes_const ignores TYPE_ALIGN. */
9181 if ((STRICT_ALIGNMENT
|| warn_cast_align
== 2)
9182 && (complain
& tf_warning
)
9183 && min_align_of_type (TREE_TYPE (dst_type
))
9184 > min_align_of_type (TREE_TYPE (src_type
)))
9185 warning_at (loc
, OPT_Wcast_align
, "cast from %qH to %qI "
9186 "increases required alignment of target type",
9187 src_type
, dst_type
);
9191 expr
= cp_build_addr_expr (expr
, complain
);
9192 if (expr
== error_mark_node
)
9193 return error_mark_node
;
9194 expr
= build_nop (reference_type
, expr
);
9195 return convert_from_reference (expr
);
9199 expr
= decay_conversion (expr
, complain
);
9200 if (expr
== error_mark_node
)
9201 return error_mark_node
;
9203 /* build_c_cast puts on a NOP_EXPR to make the result not an
9204 lvalue. Strip such NOP_EXPRs if VALUE is being used in
9205 non-lvalue context. */
9206 if (TREE_CODE (expr
) == NOP_EXPR
9207 && TREE_TYPE (expr
) == TREE_TYPE (TREE_OPERAND (expr
, 0)))
9208 expr
= TREE_OPERAND (expr
, 0);
9209 return build_nop (dst_type
, expr
);
9213 && !at_least_as_qualified_p (TREE_TYPE (dst_type
),
9214 TREE_TYPE (src_type
)))
9215 check_for_casting_away_constness (loc
, src_type
, dst_type
,
9216 CAST_EXPR
, complain
);
9219 if (complain
& tf_error
)
9220 error_at (loc
, "invalid %<const_cast%> from type %qT to type %qT",
9221 src_type
, dst_type
);
9222 return error_mark_node
;
9226 build_const_cast (location_t loc
, tree type
, tree expr
,
9227 tsubst_flags_t complain
)
9231 if (type
== error_mark_node
|| error_operand_p (expr
))
9232 return error_mark_node
;
9234 if (processing_template_decl
)
9236 tree t
= build_min (CONST_CAST_EXPR
, type
, expr
);
9238 if (!TREE_SIDE_EFFECTS (t
)
9239 && type_dependent_expression_p (expr
))
9240 /* There might turn out to be side effects inside expr. */
9241 TREE_SIDE_EFFECTS (t
) = 1;
9242 r
= convert_from_reference (t
);
9243 protected_set_expr_location (r
, loc
);
9247 r
= build_const_cast_1 (loc
, type
, expr
, complain
, /*valid_p=*/NULL
);
9248 if (r
!= error_mark_node
)
9250 maybe_warn_about_useless_cast (loc
, type
, expr
, complain
);
9251 maybe_warn_about_cast_ignoring_quals (loc
, type
, complain
);
9253 protected_set_expr_location (r
, loc
);
9257 /* Like cp_build_c_cast, but for the c-common bits. */
9260 build_c_cast (location_t loc
, tree type
, tree expr
)
9262 return cp_build_c_cast (loc
, type
, expr
, tf_warning_or_error
);
9265 /* Like the "build_c_cast" used for c-common, but using cp_expr to
9266 preserve location information even for tree nodes that don't
9270 build_c_cast (location_t loc
, tree type
, cp_expr expr
)
9272 cp_expr result
= cp_build_c_cast (loc
, type
, expr
, tf_warning_or_error
);
9273 result
.set_location (loc
);
9277 /* Build an expression representing an explicit C-style cast to type
9278 TYPE of expression EXPR. */
9281 cp_build_c_cast (location_t loc
, tree type
, tree expr
,
9282 tsubst_flags_t complain
)
9288 if (type
== error_mark_node
|| error_operand_p (expr
))
9289 return error_mark_node
;
9291 if (processing_template_decl
)
9293 tree t
= build_min (CAST_EXPR
, type
,
9294 tree_cons (NULL_TREE
, value
, NULL_TREE
));
9295 /* We don't know if it will or will not have side effects. */
9296 TREE_SIDE_EFFECTS (t
) = 1;
9297 return convert_from_reference (t
);
9300 /* Casts to a (pointer to a) specific ObjC class (or 'id' or
9301 'Class') should always be retained, because this information aids
9302 in method lookup. */
9303 if (objc_is_object_ptr (type
)
9304 && objc_is_object_ptr (TREE_TYPE (expr
)))
9305 return build_nop (type
, expr
);
9307 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
9308 Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
9309 if (!TYPE_REF_P (type
)
9310 && TREE_CODE (value
) == NOP_EXPR
9311 && TREE_TYPE (value
) == TREE_TYPE (TREE_OPERAND (value
, 0)))
9312 value
= TREE_OPERAND (value
, 0);
9314 if (TREE_CODE (type
) == ARRAY_TYPE
)
9316 /* Allow casting from T1* to T2[] because Cfront allows it.
9317 NIHCL uses it. It is not valid ISO C++ however. */
9318 if (TYPE_PTR_P (TREE_TYPE (expr
)))
9320 if (complain
& tf_error
)
9321 permerror (loc
, "ISO C++ forbids casting to an array type %qT",
9324 return error_mark_node
;
9325 type
= build_pointer_type (TREE_TYPE (type
));
9329 if (complain
& tf_error
)
9330 error_at (loc
, "ISO C++ forbids casting to an array type %qT",
9332 return error_mark_node
;
9336 if (FUNC_OR_METHOD_TYPE_P (type
))
9338 if (complain
& tf_error
)
9339 error_at (loc
, "invalid cast to function type %qT", type
);
9340 return error_mark_node
;
9343 if (TYPE_PTR_P (type
)
9344 && TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
9345 /* Casting to an integer of smaller size is an error detected elsewhere. */
9346 && TYPE_PRECISION (type
) > TYPE_PRECISION (TREE_TYPE (value
))
9347 /* Don't warn about converting any constant. */
9348 && !TREE_CONSTANT (value
))
9349 warning_at (loc
, OPT_Wint_to_pointer_cast
,
9350 "cast to pointer from integer of different size");
9352 /* A C-style cast can be a const_cast. */
9353 result
= build_const_cast_1 (loc
, type
, value
, complain
& tf_warning
,
9357 if (result
!= error_mark_node
)
9359 maybe_warn_about_useless_cast (loc
, type
, value
, complain
);
9360 maybe_warn_about_cast_ignoring_quals (loc
, type
, complain
);
9362 else if (complain
& tf_error
)
9363 build_const_cast_1 (loc
, type
, value
, tf_error
, &valid_p
);
9367 /* Or a static cast. */
9368 result
= build_static_cast_1 (loc
, type
, value
, /*c_cast_p=*/true,
9369 &valid_p
, complain
);
9370 /* Or a reinterpret_cast. */
9372 result
= build_reinterpret_cast_1 (loc
, type
, value
, /*c_cast_p=*/true,
9373 &valid_p
, complain
);
9374 /* The static_cast or reinterpret_cast may be followed by a
9377 /* A valid cast may result in errors if, for example, a
9378 conversion to an ambiguous base class is required. */
9379 && !error_operand_p (result
))
9383 maybe_warn_about_useless_cast (loc
, type
, value
, complain
);
9384 maybe_warn_about_cast_ignoring_quals (loc
, type
, complain
);
9386 /* Non-class rvalues always have cv-unqualified type. */
9387 if (!CLASS_TYPE_P (type
))
9388 type
= TYPE_MAIN_VARIANT (type
);
9389 result_type
= TREE_TYPE (result
);
9390 if (!CLASS_TYPE_P (result_type
) && !TYPE_REF_P (type
))
9391 result_type
= TYPE_MAIN_VARIANT (result_type
);
9392 /* If the type of RESULT does not match TYPE, perform a
9393 const_cast to make it match. If the static_cast or
9394 reinterpret_cast succeeded, we will differ by at most
9395 cv-qualification, so the follow-on const_cast is guaranteed
9397 if (!same_type_p (non_reference (type
), non_reference (result_type
)))
9399 result
= build_const_cast_1 (loc
, type
, result
, tf_none
, &valid_p
);
9400 gcc_assert (valid_p
);
9405 return error_mark_node
;
9408 /* Warn when a value is moved to itself with std::move. LHS is the target,
9409 RHS may be the std::move call, and LOC is the location of the whole
9410 assignment. Return true if we warned. */
9413 maybe_warn_self_move (location_t loc
, tree lhs
, tree rhs
)
9415 if (!warn_self_move
)
9418 /* C++98 doesn't know move. */
9419 if (cxx_dialect
< cxx11
)
9422 if (processing_template_decl
)
9425 if (!REFERENCE_REF_P (rhs
)
9426 || TREE_CODE (TREE_OPERAND (rhs
, 0)) != CALL_EXPR
)
9428 tree fn
= TREE_OPERAND (rhs
, 0);
9429 if (!is_std_move_p (fn
))
9432 /* Just a little helper to strip * and various NOPs. */
9433 auto extract_op
= [] (tree
&op
) {
9435 while (INDIRECT_REF_P (op
))
9436 op
= TREE_OPERAND (op
, 0);
9437 op
= maybe_undo_parenthesized_ref (op
);
9438 STRIP_ANY_LOCATION_WRAPPER (op
);
9441 tree arg
= CALL_EXPR_ARG (fn
, 0);
9443 if (TREE_CODE (arg
) == ADDR_EXPR
)
9444 arg
= TREE_OPERAND (arg
, 0);
9445 tree type
= TREE_TYPE (lhs
);
9446 tree orig_lhs
= lhs
;
9448 if (cp_tree_equal (lhs
, arg
)
9449 /* Also warn in a member-initializer-list, as in : i(std::move(i)). */
9450 || (TREE_CODE (lhs
) == FIELD_DECL
9451 && TREE_CODE (arg
) == COMPONENT_REF
9452 && cp_tree_equal (TREE_OPERAND (arg
, 0), current_class_ref
)
9453 && TREE_OPERAND (arg
, 1) == lhs
))
9454 if (warning_at (loc
, OPT_Wself_move
,
9455 "moving %qE of type %qT to itself", orig_lhs
, type
))
9461 /* For use from the C common bits. */
9463 build_modify_expr (location_t location
,
9464 tree lhs
, tree
/*lhs_origtype*/,
9465 enum tree_code modifycode
,
9466 location_t
/*rhs_location*/, tree rhs
,
9467 tree
/*rhs_origtype*/)
9469 return cp_build_modify_expr (location
, lhs
, modifycode
, rhs
,
9470 tf_warning_or_error
);
9473 /* Build an assignment expression of lvalue LHS from value RHS.
9474 MODIFYCODE is the code for a binary operator that we use
9475 to combine the old value of LHS with RHS to get the new value.
9476 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
9478 C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
9481 cp_build_modify_expr (location_t loc
, tree lhs
, enum tree_code modifycode
,
9482 tree rhs
, tsubst_flags_t complain
)
9484 lhs
= mark_lvalue_use_nonread (lhs
);
9486 tree result
= NULL_TREE
;
9488 tree lhstype
= TREE_TYPE (lhs
);
9490 tree olhstype
= lhstype
;
9491 bool plain_assign
= (modifycode
== NOP_EXPR
);
9492 bool compound_side_effects_p
= false;
9493 tree preeval
= NULL_TREE
;
9495 /* Avoid duplicate error messages from operands that had errors. */
9496 if (error_operand_p (lhs
) || error_operand_p (rhs
))
9497 return error_mark_node
;
9499 while (TREE_CODE (lhs
) == COMPOUND_EXPR
)
9501 if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0)))
9502 compound_side_effects_p
= true;
9503 lhs
= TREE_OPERAND (lhs
, 1);
9506 /* Handle control structure constructs used as "lvalues". Note that we
9507 leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS. */
9508 switch (TREE_CODE (lhs
))
9510 /* Handle --foo = 5; as these are valid constructs in C++. */
9511 case PREDECREMENT_EXPR
:
9512 case PREINCREMENT_EXPR
:
9513 if (compound_side_effects_p
)
9514 newrhs
= rhs
= stabilize_expr (rhs
, &preeval
);
9515 lhs
= genericize_compound_lvalue (lhs
);
9517 /* If we had (bar, --foo) = 5; or (bar, (baz, --foo)) = 5;
9518 and looked through the COMPOUND_EXPRs, readd them now around
9519 the resulting lhs. */
9520 if (TREE_CODE (olhs
) == COMPOUND_EXPR
)
9522 lhs
= build2 (COMPOUND_EXPR
, lhstype
, TREE_OPERAND (olhs
, 0), lhs
);
9523 tree
*ptr
= &TREE_OPERAND (lhs
, 1);
9524 for (olhs
= TREE_OPERAND (olhs
, 1);
9525 TREE_CODE (olhs
) == COMPOUND_EXPR
;
9526 olhs
= TREE_OPERAND (olhs
, 1))
9528 *ptr
= build2 (COMPOUND_EXPR
, lhstype
,
9529 TREE_OPERAND (olhs
, 0), *ptr
);
9530 ptr
= &TREE_OPERAND (*ptr
, 1);
9536 if (compound_side_effects_p
)
9537 newrhs
= rhs
= stabilize_expr (rhs
, &preeval
);
9538 lhs
= genericize_compound_lvalue (lhs
);
9539 goto maybe_add_compound
;
9543 /* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
9544 when neither operand has side-effects. */
9545 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
9546 return error_mark_node
;
9548 gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 0))
9549 && !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs
, 1)));
9551 lhs
= build3 (COND_EXPR
, TREE_TYPE (lhs
),
9552 build2 (TREE_CODE (lhs
) == MIN_EXPR
? LE_EXPR
: GE_EXPR
,
9554 TREE_OPERAND (lhs
, 0),
9555 TREE_OPERAND (lhs
, 1)),
9556 TREE_OPERAND (lhs
, 0),
9557 TREE_OPERAND (lhs
, 1));
9560 /* Handle (a ? b : c) used as an "lvalue". */
9563 /* Produce (a ? (b = rhs) : (c = rhs))
9564 except that the RHS goes through a save-expr
9565 so the code to compute it is only emitted once. */
9566 if (VOID_TYPE_P (TREE_TYPE (rhs
)))
9568 if (complain
& tf_error
)
9569 error_at (cp_expr_loc_or_loc (rhs
, loc
),
9570 "void value not ignored as it ought to be");
9571 return error_mark_node
;
9574 rhs
= stabilize_expr (rhs
, &preeval
);
9576 /* Check this here to avoid odd errors when trying to convert
9577 a throw to the type of the COND_EXPR. */
9578 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
9579 return error_mark_node
;
9581 tree op1
= TREE_OPERAND (lhs
, 1);
9582 if (TREE_CODE (op1
) != THROW_EXPR
)
9583 op1
= cp_build_modify_expr (loc
, op1
, modifycode
, rhs
, complain
);
9584 /* When sanitizing undefined behavior, even when rhs doesn't need
9585 stabilization at this point, the sanitization might add extra
9586 SAVE_EXPRs in there and so make sure there is no tree sharing
9587 in the rhs, otherwise those SAVE_EXPRs will have initialization
9588 only in one of the two branches. */
9589 if (sanitize_flags_p (SANITIZE_UNDEFINED
9590 | SANITIZE_UNDEFINED_NONDEFAULT
))
9591 rhs
= unshare_expr (rhs
);
9592 tree op2
= TREE_OPERAND (lhs
, 2);
9593 if (TREE_CODE (op2
) != THROW_EXPR
)
9594 op2
= cp_build_modify_expr (loc
, op2
, modifycode
, rhs
, complain
);
9595 tree cond
= build_conditional_expr (input_location
,
9596 TREE_OPERAND (lhs
, 0), op1
, op2
,
9599 if (cond
== error_mark_node
)
9601 /* If we had (e, (a ? b : c)) = d; or (e, (f, (a ? b : c))) = d;
9602 and looked through the COMPOUND_EXPRs, readd them now around
9603 the resulting cond before adding the preevaluated rhs. */
9604 if (TREE_CODE (olhs
) == COMPOUND_EXPR
)
9606 cond
= build2 (COMPOUND_EXPR
, TREE_TYPE (cond
),
9607 TREE_OPERAND (olhs
, 0), cond
);
9608 tree
*ptr
= &TREE_OPERAND (cond
, 1);
9609 for (olhs
= TREE_OPERAND (olhs
, 1);
9610 TREE_CODE (olhs
) == COMPOUND_EXPR
;
9611 olhs
= TREE_OPERAND (olhs
, 1))
9613 *ptr
= build2 (COMPOUND_EXPR
, TREE_TYPE (cond
),
9614 TREE_OPERAND (olhs
, 0), *ptr
);
9615 ptr
= &TREE_OPERAND (*ptr
, 1);
9618 /* Make sure the code to compute the rhs comes out
9619 before the split. */
9629 if (modifycode
== INIT_EXPR
)
9631 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
9632 /* Do the default thing. */;
9633 else if (TREE_CODE (rhs
) == CONSTRUCTOR
)
9635 /* Compound literal. */
9636 if (! same_type_p (TREE_TYPE (rhs
), lhstype
))
9637 /* Call convert to generate an error; see PR 11063. */
9638 rhs
= convert (lhstype
, rhs
);
9639 result
= cp_build_init_expr (lhs
, rhs
);
9640 TREE_SIDE_EFFECTS (result
) = 1;
9643 else if (! MAYBE_CLASS_TYPE_P (lhstype
))
9644 /* Do the default thing. */;
9647 releasing_vec rhs_vec
= make_tree_vector_single (rhs
);
9648 result
= build_special_member_call (lhs
, complete_ctor_identifier
,
9649 &rhs_vec
, lhstype
, LOOKUP_NORMAL
,
9651 if (result
== NULL_TREE
)
9652 return error_mark_node
;
9658 lhs
= require_complete_type (lhs
, complain
);
9659 if (lhs
== error_mark_node
)
9660 return error_mark_node
;
9662 if (modifycode
== NOP_EXPR
)
9664 maybe_warn_self_move (loc
, lhs
, rhs
);
9666 if (c_dialect_objc ())
9668 result
= objc_maybe_build_modify_expr (lhs
, rhs
);
9673 /* `operator=' is not an inheritable operator. */
9674 if (! MAYBE_CLASS_TYPE_P (lhstype
))
9675 /* Do the default thing. */;
9678 result
= build_new_op (input_location
, MODIFY_EXPR
,
9679 LOOKUP_NORMAL
, lhs
, rhs
,
9680 make_node (NOP_EXPR
), NULL_TREE
,
9681 /*overload=*/NULL
, complain
);
9682 if (result
== NULL_TREE
)
9683 return error_mark_node
;
9690 tree init
= NULL_TREE
;
9692 /* A binary op has been requested. Combine the old LHS
9693 value with the RHS producing the value we should actually
9694 store into the LHS. */
9695 gcc_assert (!((TYPE_REF_P (lhstype
)
9696 && MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype
)))
9697 || MAYBE_CLASS_TYPE_P (lhstype
)));
9699 /* Preevaluate the RHS to make sure its evaluation is complete
9700 before the lvalue-to-rvalue conversion of the LHS:
9702 [expr.ass] With respect to an indeterminately-sequenced
9703 function call, the operation of a compound assignment is a
9704 single evaluation. [ Note: Therefore, a function call shall
9705 not intervene between the lvalue-to-rvalue conversion and the
9706 side effect associated with any single compound assignment
9707 operator. -- end note ] */
9708 lhs
= cp_stabilize_reference (lhs
);
9709 rhs
= decay_conversion (rhs
, complain
);
9710 if (rhs
== error_mark_node
)
9711 return error_mark_node
;
9714 auto_diagnostic_group d
;
9715 rhs
= stabilize_expr (rhs
, &init
);
9716 newrhs
= cp_build_binary_op (loc
, modifycode
, lhs
, rhs
, complain
);
9717 if (newrhs
== error_mark_node
)
9719 if (complain
& tf_error
)
9720 inform (loc
, " in evaluation of %<%Q(%#T, %#T)%>",
9721 modifycode
, TREE_TYPE (lhs
), TREE_TYPE (rhs
));
9722 return error_mark_node
;
9727 newrhs
= build2 (COMPOUND_EXPR
, TREE_TYPE (newrhs
), init
, newrhs
);
9729 /* Now it looks like a plain assignment. */
9730 modifycode
= NOP_EXPR
;
9731 if (c_dialect_objc ())
9733 result
= objc_maybe_build_modify_expr (lhs
, newrhs
);
9738 gcc_assert (!TYPE_REF_P (lhstype
));
9739 gcc_assert (!TYPE_REF_P (TREE_TYPE (newrhs
)));
9742 /* The left-hand side must be an lvalue. */
9743 if (!lvalue_or_else (lhs
, lv_assign
, complain
))
9744 return error_mark_node
;
9746 /* Warn about modifying something that is `const'. Don't warn if
9747 this is initialization. */
9748 if (modifycode
!= INIT_EXPR
9749 && (TREE_READONLY (lhs
) || CP_TYPE_CONST_P (lhstype
)
9750 /* Functions are not modifiable, even though they are
9752 || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (lhs
))
9753 /* If it's an aggregate and any field is const, then it is
9754 effectively const. */
9755 || (CLASS_TYPE_P (lhstype
)
9756 && C_TYPE_FIELDS_READONLY (lhstype
))))
9758 if (complain
& tf_error
)
9759 cxx_readonly_error (loc
, lhs
, lv_assign
);
9760 return error_mark_node
;
9763 /* If storing into a structure or union member, it may have been given a
9764 lowered bitfield type. We need to convert to the declared type first,
9765 so retrieve it now. */
9767 olhstype
= unlowered_expr_type (lhs
);
9769 /* Convert new value to destination type. */
9771 if (TREE_CODE (lhstype
) == ARRAY_TYPE
)
9775 if (BRACE_ENCLOSED_INITIALIZER_P (newrhs
))
9777 if (modifycode
!= INIT_EXPR
)
9779 if (complain
& tf_error
)
9781 "assigning to an array from an initializer list");
9782 return error_mark_node
;
9784 if (check_array_initializer (lhs
, lhstype
, newrhs
))
9785 return error_mark_node
;
9786 newrhs
= digest_init (lhstype
, newrhs
, complain
);
9787 if (newrhs
== error_mark_node
)
9788 return error_mark_node
;
9791 /* C++11 8.5/17: "If the destination type is an array of characters,
9792 an array of char16_t, an array of char32_t, or an array of wchar_t,
9793 and the initializer is a string literal...". */
9794 else if ((TREE_CODE (tree_strip_any_location_wrapper (newrhs
))
9796 && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype
)))
9797 && modifycode
== INIT_EXPR
)
9799 newrhs
= digest_init (lhstype
, newrhs
, complain
);
9800 if (newrhs
== error_mark_node
)
9801 return error_mark_node
;
9804 else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype
),
9805 TYPE_MAIN_VARIANT (TREE_TYPE (newrhs
))))
9807 if (complain
& tf_error
)
9808 error_at (loc
, "incompatible types in assignment of %qT to %qT",
9809 TREE_TYPE (rhs
), lhstype
);
9810 return error_mark_node
;
9813 /* Allow array assignment in compiler-generated code. */
9814 else if (DECL_P (lhs
) && DECL_ARTIFICIAL (lhs
))
9815 /* OK, used by coroutines (co-await-initlist1.C). */;
9816 else if (!current_function_decl
9817 || !DECL_DEFAULTED_FN (current_function_decl
))
9819 /* This routine is used for both initialization and assignment.
9820 Make sure the diagnostic message differentiates the context. */
9821 if (complain
& tf_error
)
9823 if (modifycode
== INIT_EXPR
)
9824 error_at (loc
, "array used as initializer");
9826 error_at (loc
, "invalid array assignment");
9828 return error_mark_node
;
9831 from_array
= TREE_CODE (TREE_TYPE (newrhs
)) == ARRAY_TYPE
9832 ? 1 + (modifycode
!= INIT_EXPR
): 0;
9833 result
= build_vec_init (lhs
, NULL_TREE
, newrhs
,
9834 /*explicit_value_init_p=*/false,
9835 from_array
, complain
);
9839 if (modifycode
== INIT_EXPR
)
9840 /* Calls with INIT_EXPR are all direct-initialization, so don't set
9841 LOOKUP_ONLYCONVERTING. */
9842 newrhs
= convert_for_initialization (lhs
, olhstype
, newrhs
, LOOKUP_NORMAL
,
9843 ICR_INIT
, NULL_TREE
, 0,
9844 complain
| tf_no_cleanup
);
9846 newrhs
= convert_for_assignment (olhstype
, newrhs
, ICR_ASSIGN
,
9847 NULL_TREE
, 0, complain
, LOOKUP_IMPLICIT
);
9849 if (!same_type_p (lhstype
, olhstype
))
9850 newrhs
= cp_convert_and_check (lhstype
, newrhs
, complain
);
9852 if (modifycode
!= INIT_EXPR
)
9854 if (TREE_CODE (newrhs
) == CALL_EXPR
9855 && TYPE_NEEDS_CONSTRUCTING (lhstype
))
9856 newrhs
= build_cplus_new (lhstype
, newrhs
, complain
);
9858 /* Can't initialize directly from a TARGET_EXPR, since that would
9859 cause the lhs to be constructed twice, and possibly result in
9860 accidental self-initialization. So we force the TARGET_EXPR to be
9861 expanded without a target. */
9862 if (TREE_CODE (newrhs
) == TARGET_EXPR
)
9863 newrhs
= build2 (COMPOUND_EXPR
, TREE_TYPE (newrhs
), newrhs
,
9864 TARGET_EXPR_SLOT (newrhs
));
9867 if (newrhs
== error_mark_node
)
9868 return error_mark_node
;
9870 if (c_dialect_objc () && flag_objc_gc
)
9872 result
= objc_generate_write_barrier (lhs
, modifycode
, newrhs
);
9878 result
= build2_loc (loc
, modifycode
== NOP_EXPR
? MODIFY_EXPR
: INIT_EXPR
,
9879 lhstype
, lhs
, newrhs
);
9880 if (modifycode
== INIT_EXPR
)
9881 set_target_expr_eliding (newrhs
);
9883 TREE_SIDE_EFFECTS (result
) = 1;
9885 suppress_warning (result
, OPT_Wparentheses
);
9889 result
= build2 (COMPOUND_EXPR
, TREE_TYPE (result
), preeval
, result
);
9894 build_x_modify_expr (location_t loc
, tree lhs
, enum tree_code modifycode
,
9895 tree rhs
, tree lookups
, tsubst_flags_t complain
)
9897 tree orig_lhs
= lhs
;
9898 tree orig_rhs
= rhs
;
9899 tree overload
= NULL_TREE
;
9901 if (lhs
== error_mark_node
|| rhs
== error_mark_node
)
9902 return cp_expr (error_mark_node
, loc
);
9904 tree op
= build_min (modifycode
, void_type_node
, NULL_TREE
, NULL_TREE
);
9906 if (processing_template_decl
)
9908 if (type_dependent_expression_p (lhs
)
9909 || type_dependent_expression_p (rhs
))
9911 tree rval
= build_min_nt_loc (loc
, MODOP_EXPR
, lhs
, op
, rhs
);
9912 if (modifycode
!= NOP_EXPR
)
9914 = build_dependent_operator_type (lookups
, modifycode
, true);
9920 if (modifycode
== NOP_EXPR
)
9921 rval
= cp_build_modify_expr (loc
, lhs
, modifycode
, rhs
, complain
);
9923 rval
= build_new_op (loc
, MODIFY_EXPR
, LOOKUP_NORMAL
,
9924 lhs
, rhs
, op
, lookups
, &overload
, complain
);
9925 if (rval
== error_mark_node
)
9926 return error_mark_node
;
9927 if (processing_template_decl
)
9929 if (overload
!= NULL_TREE
)
9930 return (build_min_non_dep_op_overload
9931 (MODIFY_EXPR
, rval
, overload
, orig_lhs
, orig_rhs
));
9933 return (build_min_non_dep
9934 (MODOP_EXPR
, rval
, orig_lhs
, op
, orig_rhs
));
9939 /* Helper function for get_delta_difference which assumes FROM is a base
9940 class of TO. Returns a delta for the conversion of pointer-to-member
9941 of FROM to pointer-to-member of TO. If the conversion is invalid and
9942 tf_error is not set in COMPLAIN returns error_mark_node, otherwise
9943 returns zero. If FROM is not a base class of TO, returns NULL_TREE.
9944 If C_CAST_P is true, this conversion is taking place as part of a
9948 get_delta_difference_1 (tree from
, tree to
, bool c_cast_p
,
9949 tsubst_flags_t complain
)
9954 binfo
= lookup_base (to
, from
, c_cast_p
? ba_unique
: ba_check
,
9957 if (binfo
== error_mark_node
)
9959 if (!(complain
& tf_error
))
9960 return error_mark_node
;
9962 inform (input_location
, " in pointer to member function conversion");
9963 return size_zero_node
;
9967 if (kind
!= bk_via_virtual
)
9968 return BINFO_OFFSET (binfo
);
9970 /* FROM is a virtual base class of TO. Issue an error or warning
9971 depending on whether or not this is a reinterpret cast. */
9973 if (!(complain
& tf_error
))
9974 return error_mark_node
;
9976 error ("pointer to member conversion via virtual base %qT",
9977 BINFO_TYPE (binfo_from_vbase (binfo
)));
9979 return size_zero_node
;
9986 /* Get difference in deltas for different pointer to member function
9987 types. If the conversion is invalid and tf_error is not set in
9988 COMPLAIN, returns error_mark_node, otherwise returns an integer
9989 constant of type PTRDIFF_TYPE_NODE and its value is zero if the
9990 conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
9991 conversions as well. If C_CAST_P is true this conversion is taking
9992 place as part of a C-style cast.
9994 Note that the naming of FROM and TO is kind of backwards; the return
9995 value is what we add to a TO in order to get a FROM. They are named
9996 this way because we call this function to find out how to convert from
9997 a pointer to member of FROM to a pointer to member of TO. */
10000 get_delta_difference (tree from
, tree to
,
10001 bool allow_inverse_p
,
10002 bool c_cast_p
, tsubst_flags_t complain
)
10004 auto_diagnostic_group d
;
10007 if (same_type_ignoring_top_level_qualifiers_p (from
, to
))
10008 /* Pointer to member of incomplete class is permitted*/
10009 result
= size_zero_node
;
10011 result
= get_delta_difference_1 (from
, to
, c_cast_p
, complain
);
10013 if (result
== error_mark_node
)
10014 return error_mark_node
;
10018 if (!allow_inverse_p
)
10020 if (!(complain
& tf_error
))
10021 return error_mark_node
;
10023 error_not_base_type (from
, to
);
10024 inform (input_location
, " in pointer to member conversion");
10025 result
= size_zero_node
;
10029 result
= get_delta_difference_1 (to
, from
, c_cast_p
, complain
);
10031 if (result
== error_mark_node
)
10032 return error_mark_node
;
10035 result
= size_diffop_loc (input_location
,
10036 size_zero_node
, result
);
10039 if (!(complain
& tf_error
))
10040 return error_mark_node
;
10042 error_not_base_type (from
, to
);
10043 inform (input_location
, " in pointer to member conversion");
10044 result
= size_zero_node
;
10049 return convert_to_integer (ptrdiff_type_node
, result
);
10052 /* Return a constructor for the pointer-to-member-function TYPE using
10053 the other components as specified. */
10056 build_ptrmemfunc1 (tree type
, tree delta
, tree pfn
)
10058 tree u
= NULL_TREE
;
10061 vec
<constructor_elt
, va_gc
> *v
;
10063 /* Pull the FIELD_DECLs out of the type. */
10064 pfn_field
= TYPE_FIELDS (type
);
10065 delta_field
= DECL_CHAIN (pfn_field
);
10067 /* Make sure DELTA has the type we want. */
10068 delta
= convert_and_check (input_location
, delta_type_node
, delta
);
10070 /* Convert to the correct target type if necessary. */
10071 pfn
= fold_convert (TREE_TYPE (pfn_field
), pfn
);
10073 /* Finish creating the initializer. */
10075 CONSTRUCTOR_APPEND_ELT(v
, pfn_field
, pfn
);
10076 CONSTRUCTOR_APPEND_ELT(v
, delta_field
, delta
);
10077 u
= build_constructor (type
, v
);
10078 TREE_CONSTANT (u
) = TREE_CONSTANT (pfn
) & TREE_CONSTANT (delta
);
10079 TREE_STATIC (u
) = (TREE_CONSTANT (u
)
10080 && (initializer_constant_valid_p (pfn
, TREE_TYPE (pfn
))
10082 && (initializer_constant_valid_p (delta
, TREE_TYPE (delta
))
10087 /* Build a constructor for a pointer to member function. It can be
10088 used to initialize global variables, local variable, or used
10089 as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
10092 If FORCE is nonzero, then force this conversion, even if
10093 we would rather not do it. Usually set when using an explicit
10094 cast. A C-style cast is being processed iff C_CAST_P is true.
10096 Return error_mark_node, if something goes wrong. */
10099 build_ptrmemfunc (tree type
, tree pfn
, int force
, bool c_cast_p
,
10100 tsubst_flags_t complain
)
10106 if (error_operand_p (pfn
))
10107 return error_mark_node
;
10109 pfn_type
= TREE_TYPE (pfn
);
10110 to_type
= build_ptrmemfunc_type (type
);
10112 /* Handle multiple conversions of pointer to member functions. */
10113 if (TYPE_PTRMEMFUNC_P (pfn_type
))
10115 tree delta
= NULL_TREE
;
10116 tree npfn
= NULL_TREE
;
10120 && !can_convert_arg (to_type
, TREE_TYPE (pfn
), pfn
,
10121 LOOKUP_NORMAL
, complain
))
10123 if (complain
& tf_error
)
10124 error ("invalid conversion to type %qT from type %qT",
10125 to_type
, pfn_type
);
10127 return error_mark_node
;
10130 n
= get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type
),
10131 TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type
),
10133 c_cast_p
, complain
);
10134 if (n
== error_mark_node
)
10135 return error_mark_node
;
10137 STRIP_ANY_LOCATION_WRAPPER (pfn
);
10139 /* We don't have to do any conversion to convert a
10140 pointer-to-member to its own type. But, we don't want to
10141 just return a PTRMEM_CST if there's an explicit cast; that
10142 cast should make the expression an invalid template argument. */
10143 if (TREE_CODE (pfn
) != PTRMEM_CST
10144 && same_type_p (to_type
, pfn_type
))
10147 if (TREE_SIDE_EFFECTS (pfn
))
10148 pfn
= save_expr (pfn
);
10150 /* Obtain the function pointer and the current DELTA. */
10151 if (TREE_CODE (pfn
) == PTRMEM_CST
)
10152 expand_ptrmemfunc_cst (pfn
, &delta
, &npfn
);
10155 npfn
= build_ptrmemfunc_access_expr (pfn
, pfn_identifier
);
10156 delta
= build_ptrmemfunc_access_expr (pfn
, delta_identifier
);
10159 /* Just adjust the DELTA field. */
10160 gcc_assert (same_type_ignoring_top_level_qualifiers_p
10161 (TREE_TYPE (delta
), ptrdiff_type_node
));
10162 if (!integer_zerop (n
))
10164 if (TARGET_PTRMEMFUNC_VBIT_LOCATION
== ptrmemfunc_vbit_in_delta
)
10165 n
= cp_build_binary_op (input_location
,
10166 LSHIFT_EXPR
, n
, integer_one_node
,
10168 delta
= cp_build_binary_op (input_location
,
10169 PLUS_EXPR
, delta
, n
, complain
);
10171 return build_ptrmemfunc1 (to_type
, delta
, npfn
);
10174 /* Handle null pointer to member function conversions. */
10175 if (null_ptr_cst_p (pfn
))
10177 pfn
= cp_build_c_cast (input_location
,
10178 TYPE_PTRMEMFUNC_FN_TYPE_RAW (to_type
),
10180 return build_ptrmemfunc1 (to_type
,
10185 if (type_unknown_p (pfn
))
10186 return instantiate_type (type
, pfn
, complain
);
10188 fn
= TREE_OPERAND (pfn
, 0);
10189 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
10190 /* In a template, we will have preserved the
10192 || (processing_template_decl
&& TREE_CODE (fn
) == OFFSET_REF
));
10193 return make_ptrmem_cst (to_type
, fn
);
10196 /* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
10199 ??? There is no consistency as to the types returned for the above
10200 values. Some code acts as if it were a sizetype and some as if it were
10201 integer_type_node. */
10204 expand_ptrmemfunc_cst (tree cst
, tree
*delta
, tree
*pfn
)
10206 tree type
= TREE_TYPE (cst
);
10207 tree fn
= PTRMEM_CST_MEMBER (cst
);
10208 tree ptr_class
, fn_class
;
10210 gcc_assert (TREE_CODE (fn
) == FUNCTION_DECL
);
10212 /* The class that the function belongs to. */
10213 fn_class
= DECL_CONTEXT (fn
);
10215 /* The class that we're creating a pointer to member of. */
10216 ptr_class
= TYPE_PTRMEMFUNC_OBJECT_TYPE (type
);
10218 /* First, calculate the adjustment to the function's class. */
10219 *delta
= get_delta_difference (fn_class
, ptr_class
, /*force=*/0,
10220 /*c_cast_p=*/0, tf_warning_or_error
);
10222 if (!DECL_VIRTUAL_P (fn
))
10224 tree t
= build_addr_func (fn
, tf_warning_or_error
);
10225 if (TREE_CODE (t
) == ADDR_EXPR
)
10226 SET_EXPR_LOCATION (t
, PTRMEM_CST_LOCATION (cst
));
10227 *pfn
= convert (TYPE_PTRMEMFUNC_FN_TYPE (type
), t
);
10231 /* If we're dealing with a virtual function, we have to adjust 'this'
10232 again, to point to the base which provides the vtable entry for
10233 fn; the call will do the opposite adjustment. */
10234 tree orig_class
= DECL_CONTEXT (fn
);
10235 tree binfo
= binfo_or_else (orig_class
, fn_class
);
10236 *delta
= fold_build2 (PLUS_EXPR
, TREE_TYPE (*delta
),
10237 *delta
, BINFO_OFFSET (binfo
));
10239 /* We set PFN to the vtable offset at which the function can be
10240 found, plus one (unless ptrmemfunc_vbit_in_delta, in which
10241 case delta is shifted left, and then incremented). */
10242 *pfn
= DECL_VINDEX (fn
);
10243 *pfn
= fold_build2 (MULT_EXPR
, integer_type_node
, *pfn
,
10244 TYPE_SIZE_UNIT (vtable_entry_type
));
10246 switch (TARGET_PTRMEMFUNC_VBIT_LOCATION
)
10248 case ptrmemfunc_vbit_in_pfn
:
10249 *pfn
= fold_build2 (PLUS_EXPR
, integer_type_node
, *pfn
,
10253 case ptrmemfunc_vbit_in_delta
:
10254 *delta
= fold_build2 (LSHIFT_EXPR
, TREE_TYPE (*delta
),
10255 *delta
, integer_one_node
);
10256 *delta
= fold_build2 (PLUS_EXPR
, TREE_TYPE (*delta
),
10257 *delta
, integer_one_node
);
10261 gcc_unreachable ();
10264 *pfn
= fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type
), *pfn
);
10268 /* Return an expression for PFN from the pointer-to-member function
10272 pfn_from_ptrmemfunc (tree t
)
10274 if (TREE_CODE (t
) == PTRMEM_CST
)
10279 expand_ptrmemfunc_cst (t
, &delta
, &pfn
);
10284 return build_ptrmemfunc_access_expr (t
, pfn_identifier
);
10287 /* Return an expression for DELTA from the pointer-to-member function
10291 delta_from_ptrmemfunc (tree t
)
10293 if (TREE_CODE (t
) == PTRMEM_CST
)
10298 expand_ptrmemfunc_cst (t
, &delta
, &pfn
);
10303 return build_ptrmemfunc_access_expr (t
, delta_identifier
);
10306 /* Convert value RHS to type TYPE as preparation for an assignment to
10307 an lvalue of type TYPE. ERRTYPE indicates what kind of error the
10308 implicit conversion is. If FNDECL is non-NULL, we are doing the
10309 conversion in order to pass the PARMNUMth argument of FNDECL.
10310 If FNDECL is NULL, we are doing the conversion in function pointer
10311 argument passing, conversion in initialization, etc. */
10314 convert_for_assignment (tree type
, tree rhs
,
10315 impl_conv_rhs errtype
, tree fndecl
, int parmnum
,
10316 tsubst_flags_t complain
, int flags
)
10319 enum tree_code coder
;
10321 location_t rhs_loc
= cp_expr_loc_or_input_loc (rhs
);
10322 bool has_loc
= EXPR_LOCATION (rhs
) != UNKNOWN_LOCATION
;
10323 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue,
10324 but preserve location wrappers. */
10325 if (TREE_CODE (rhs
) == NON_LVALUE_EXPR
10326 && !location_wrapper_p (rhs
))
10327 rhs
= TREE_OPERAND (rhs
, 0);
10329 /* Handle [dcl.init.list] direct-list-initialization from
10330 single element of enumeration with a fixed underlying type. */
10331 if (is_direct_enum_init (type
, rhs
))
10333 tree elt
= CONSTRUCTOR_ELT (rhs
, 0)->value
;
10334 if (check_narrowing (ENUM_UNDERLYING_TYPE (type
), elt
, complain
))
10336 warning_sentinel
w (warn_useless_cast
);
10337 warning_sentinel
w2 (warn_ignored_qualifiers
);
10338 rhs
= cp_build_c_cast (rhs_loc
, type
, elt
, complain
);
10341 rhs
= error_mark_node
;
10344 rhstype
= TREE_TYPE (rhs
);
10345 coder
= TREE_CODE (rhstype
);
10347 if (VECTOR_TYPE_P (type
) && coder
== VECTOR_TYPE
10348 && vector_types_convertible_p (type
, rhstype
, true))
10350 rhs
= mark_rvalue_use (rhs
);
10351 return convert (type
, rhs
);
10354 if (rhs
== error_mark_node
|| rhstype
== error_mark_node
)
10355 return error_mark_node
;
10356 if (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
)
10357 return error_mark_node
;
10359 /* The RHS of an assignment cannot have void type. */
10360 if (coder
== VOID_TYPE
)
10362 if (complain
& tf_error
)
10363 error_at (rhs_loc
, "void value not ignored as it ought to be");
10364 return error_mark_node
;
10367 if (c_dialect_objc ())
10371 tree rname
= fndecl
;
10382 selector
= objc_message_selector ();
10384 if (selector
&& parmno
> 1)
10391 if (objc_compare_types (type
, rhstype
, parmno
, rname
))
10393 rhs
= mark_rvalue_use (rhs
);
10394 return convert (type
, rhs
);
10400 The expression is implicitly converted (clause _conv_) to the
10401 cv-unqualified type of the left operand.
10403 We allow bad conversions here because by the time we get to this point
10404 we are committed to doing the conversion. If we end up doing a bad
10405 conversion, convert_like will complain. */
10406 if (!can_convert_arg_bad (type
, rhstype
, rhs
, flags
, complain
))
10408 /* When -Wno-pmf-conversions is use, we just silently allow
10409 conversions from pointers-to-members to plain pointers. If
10410 the conversion doesn't work, cp_convert will complain. */
10412 && TYPE_PTR_P (type
)
10413 && TYPE_PTRMEMFUNC_P (rhstype
))
10414 rhs
= cp_convert (strip_top_quals (type
), rhs
, complain
);
10417 if (complain
& tf_error
)
10419 auto_diagnostic_group d
;
10421 /* If the right-hand side has unknown type, then it is an
10422 overloaded function. Call instantiate_type to get error
10424 if (rhstype
== unknown_type_node
)
10426 tree r
= instantiate_type (type
, rhs
, tf_warning_or_error
);
10427 /* -fpermissive might allow this; recurse. */
10428 if (!seen_error ())
10429 return convert_for_assignment (type
, r
, errtype
, fndecl
,
10430 parmnum
, complain
, flags
);
10433 complain_about_bad_argument (rhs_loc
,
10438 range_label_for_type_mismatch
label (rhstype
, type
);
10439 gcc_rich_location richloc
10441 has_loc
? &label
: NULL
,
10442 has_loc
? highlight_colors::percent_h
: NULL
);
10446 case ICR_DEFAULT_ARGUMENT
:
10447 error_at (&richloc
,
10448 "cannot convert %qH to %qI in default argument",
10452 error_at (&richloc
,
10453 "cannot convert %qH to %qI in argument passing",
10456 case ICR_CONVERTING
:
10457 error_at (&richloc
, "cannot convert %qH to %qI",
10461 error_at (&richloc
,
10462 "cannot convert %qH to %qI in initialization",
10466 error_at (&richloc
, "cannot convert %qH to %qI in return",
10470 error_at (&richloc
,
10471 "cannot convert %qH to %qI in assignment",
10479 /* See if we can be more helpful. */
10480 maybe_show_nonconverting_candidate (type
, rhstype
, rhs
, flags
);
10482 if (TYPE_PTR_P (rhstype
)
10483 && TYPE_PTR_P (type
)
10484 && CLASS_TYPE_P (TREE_TYPE (rhstype
))
10485 && CLASS_TYPE_P (TREE_TYPE (type
))
10486 && !COMPLETE_TYPE_P (TREE_TYPE (rhstype
)))
10487 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
10488 (TREE_TYPE (rhstype
))),
10489 "class type %qT is incomplete", TREE_TYPE (rhstype
));
10491 return error_mark_node
;
10494 if (warn_suggest_attribute_format
)
10496 const enum tree_code codel
= TREE_CODE (type
);
10497 if ((codel
== POINTER_TYPE
|| codel
== REFERENCE_TYPE
)
10499 && check_missing_format_attribute (type
, rhstype
)
10500 && (complain
& tf_warning
))
10504 case ICR_DEFAULT_ARGUMENT
:
10506 warning (OPT_Wsuggest_attribute_format
,
10507 "parameter %qP of %qD might be a candidate "
10508 "for a format attribute", parmnum
, fndecl
);
10510 warning (OPT_Wsuggest_attribute_format
,
10511 "parameter might be a candidate "
10512 "for a format attribute");
10514 case ICR_CONVERTING
:
10515 warning (OPT_Wsuggest_attribute_format
,
10516 "target of conversion might be a candidate "
10517 "for a format attribute");
10520 warning (OPT_Wsuggest_attribute_format
,
10521 "target of initialization might be a candidate "
10522 "for a format attribute");
10525 warning (OPT_Wsuggest_attribute_format
,
10526 "return type might be a candidate "
10527 "for a format attribute");
10530 warning (OPT_Wsuggest_attribute_format
,
10531 "left-hand side of assignment might be a candidate "
10532 "for a format attribute");
10539 if (TREE_CODE (type
) == BOOLEAN_TYPE
)
10540 maybe_warn_unparenthesized_assignment (rhs
, /*nested_p=*/true, complain
);
10542 if (complain
& tf_warning
)
10543 warn_for_address_of_packed_member (type
, rhs
);
10545 return perform_implicit_conversion_flags (strip_top_quals (type
), rhs
,
10549 /* Convert RHS to be of type TYPE.
10550 If EXP is nonzero, it is the target of the initialization.
10551 ERRTYPE indicates what kind of error the implicit conversion is.
10553 Two major differences between the behavior of
10554 `convert_for_assignment' and `convert_for_initialization'
10555 are that references are bashed in the former, while
10556 copied in the latter, and aggregates are assigned in
10557 the former (operator=) while initialized in the
10560 If using constructor make sure no conversion operator exists, if one does
10561 exist, an ambiguity exists. */
10564 convert_for_initialization (tree exp
, tree type
, tree rhs
, int flags
,
10565 impl_conv_rhs errtype
, tree fndecl
, int parmnum
,
10566 tsubst_flags_t complain
)
10568 enum tree_code codel
= TREE_CODE (type
);
10570 enum tree_code coder
;
10572 /* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
10573 Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
10574 if (TREE_CODE (rhs
) == NOP_EXPR
10575 && TREE_TYPE (rhs
) == TREE_TYPE (TREE_OPERAND (rhs
, 0))
10576 && codel
!= REFERENCE_TYPE
)
10577 rhs
= TREE_OPERAND (rhs
, 0);
10579 if (type
== error_mark_node
10580 || rhs
== error_mark_node
10581 || (TREE_CODE (rhs
) == TREE_LIST
&& TREE_VALUE (rhs
) == error_mark_node
))
10582 return error_mark_node
;
10584 if (MAYBE_CLASS_TYPE_P (non_reference (type
)))
10586 else if ((TREE_CODE (TREE_TYPE (rhs
)) == ARRAY_TYPE
10587 && TREE_CODE (type
) != ARRAY_TYPE
10588 && (!TYPE_REF_P (type
)
10589 || TREE_CODE (TREE_TYPE (type
)) != ARRAY_TYPE
))
10590 || (TREE_CODE (TREE_TYPE (rhs
)) == FUNCTION_TYPE
10591 && !TYPE_REFFN_P (type
))
10592 || TREE_CODE (TREE_TYPE (rhs
)) == METHOD_TYPE
)
10593 rhs
= decay_conversion (rhs
, complain
);
10595 rhstype
= TREE_TYPE (rhs
);
10596 coder
= TREE_CODE (rhstype
);
10598 if (coder
== ERROR_MARK
)
10599 return error_mark_node
;
10601 /* We accept references to incomplete types, so we can
10602 return here before checking if RHS is of complete type. */
10604 if (codel
== REFERENCE_TYPE
)
10606 auto_diagnostic_group d
;
10607 /* This should eventually happen in convert_arguments. */
10608 int savew
= 0, savee
= 0;
10611 savew
= warningcount
+ werrorcount
, savee
= errorcount
;
10612 rhs
= initialize_reference (type
, rhs
, flags
, complain
);
10615 && (warningcount
+ werrorcount
> savew
|| errorcount
> savee
))
10616 inform (get_fndecl_argument_location (fndecl
, parmnum
),
10617 "in passing argument %P of %qD", parmnum
, fndecl
);
10622 exp
= require_complete_type (exp
, complain
);
10623 if (exp
== error_mark_node
)
10624 return error_mark_node
;
10626 type
= complete_type (type
);
10628 if (DIRECT_INIT_EXPR_P (type
, rhs
))
10629 /* Don't try to do copy-initialization if we already have
10630 direct-initialization. */
10633 if (MAYBE_CLASS_TYPE_P (type
))
10634 return perform_implicit_conversion_flags (type
, rhs
, complain
, flags
);
10636 return convert_for_assignment (type
, rhs
, errtype
, fndecl
, parmnum
,
10640 /* If RETVAL is the address of, or a reference to, a local variable or
10641 temporary give an appropriate warning and return true. */
10644 maybe_warn_about_returning_address_of_local (tree retval
, location_t loc
)
10646 tree valtype
= TREE_TYPE (DECL_RESULT (current_function_decl
));
10647 tree whats_returned
= fold_for_warn (retval
);
10649 loc
= cp_expr_loc_or_input_loc (retval
);
10653 if (TREE_CODE (whats_returned
) == COMPOUND_EXPR
)
10654 whats_returned
= TREE_OPERAND (whats_returned
, 1);
10655 else if (CONVERT_EXPR_P (whats_returned
)
10656 || TREE_CODE (whats_returned
) == NON_LVALUE_EXPR
)
10657 whats_returned
= TREE_OPERAND (whats_returned
, 0);
10662 if (TREE_CODE (whats_returned
) == TARGET_EXPR
10663 && is_std_init_list (TREE_TYPE (whats_returned
)))
10665 tree init
= TARGET_EXPR_INITIAL (whats_returned
);
10666 if (TREE_CODE (init
) == CONSTRUCTOR
)
10667 /* Pull out the array address. */
10668 whats_returned
= CONSTRUCTOR_ELT (init
, 0)->value
;
10669 else if (TREE_CODE (init
) == INDIRECT_REF
)
10670 /* The source of a trivial copy looks like *(T*)&var. */
10671 whats_returned
= TREE_OPERAND (init
, 0);
10674 STRIP_NOPS (whats_returned
);
10677 /* As a special case, we handle a call to std::move or std::forward. */
10678 if (TREE_CODE (whats_returned
) == CALL_EXPR
10679 && (is_std_move_p (whats_returned
)
10680 || is_std_forward_p (whats_returned
)))
10682 tree arg
= CALL_EXPR_ARG (whats_returned
, 0);
10683 return maybe_warn_about_returning_address_of_local (arg
, loc
);
10686 if (TREE_CODE (whats_returned
) != ADDR_EXPR
)
10688 whats_returned
= TREE_OPERAND (whats_returned
, 0);
10690 while (TREE_CODE (whats_returned
) == COMPONENT_REF
10691 || TREE_CODE (whats_returned
) == ARRAY_REF
)
10692 whats_returned
= TREE_OPERAND (whats_returned
, 0);
10694 if (TREE_CODE (whats_returned
) == AGGR_INIT_EXPR
10695 || TREE_CODE (whats_returned
) == TARGET_EXPR
)
10697 if (TYPE_REF_P (valtype
))
10698 /* P2748 made this an error in C++26. */
10699 emit_diagnostic (cxx_dialect
>= cxx26
? DK_PERMERROR
: DK_WARNING
,
10700 loc
, OPT_Wreturn_local_addr
,
10701 "returning reference to temporary");
10702 else if (TYPE_PTR_P (valtype
))
10703 warning_at (loc
, OPT_Wreturn_local_addr
,
10704 "returning pointer to temporary");
10705 else if (is_std_init_list (valtype
))
10706 warning_at (loc
, OPT_Winit_list_lifetime
,
10707 "returning temporary %<initializer_list%> does not extend "
10708 "the lifetime of the underlying array");
10712 STRIP_ANY_LOCATION_WRAPPER (whats_returned
);
10714 if (DECL_P (whats_returned
)
10715 && DECL_NAME (whats_returned
)
10716 && DECL_FUNCTION_SCOPE_P (whats_returned
)
10717 && !is_capture_proxy (whats_returned
)
10718 && !(TREE_STATIC (whats_returned
)
10719 || TREE_PUBLIC (whats_returned
)))
10721 if (DECL_DECOMPOSITION_P (whats_returned
)
10722 && !DECL_DECOMP_IS_BASE (whats_returned
)
10723 && DECL_HAS_VALUE_EXPR_P (whats_returned
))
10725 /* When returning address of a structured binding, if the structured
10726 binding is not a reference, continue normally, if it is a
10727 reference, recurse on the initializer of the structured
10729 tree base
= DECL_DECOMP_BASE (whats_returned
);
10730 if (TYPE_REF_P (TREE_TYPE (base
)))
10732 if (tree init
= DECL_INITIAL (base
))
10733 return maybe_warn_about_returning_address_of_local (init
, loc
);
10739 auto_diagnostic_group d
;
10740 if (TYPE_REF_P (valtype
))
10741 w
= warning_at (loc
, OPT_Wreturn_local_addr
,
10742 "reference to local variable %qD returned",
10744 else if (is_std_init_list (valtype
))
10745 w
= warning_at (loc
, OPT_Winit_list_lifetime
,
10746 "returning local %<initializer_list%> variable %qD "
10747 "does not extend the lifetime of the underlying array",
10749 else if (POINTER_TYPE_P (valtype
)
10750 && TREE_CODE (whats_returned
) == LABEL_DECL
)
10751 w
= warning_at (loc
, OPT_Wreturn_local_addr
,
10752 "address of label %qD returned",
10754 else if (POINTER_TYPE_P (valtype
))
10755 w
= warning_at (loc
, OPT_Wreturn_local_addr
,
10756 "address of local variable %qD returned",
10759 inform (DECL_SOURCE_LOCATION (whats_returned
),
10767 /* Returns true if DECL is in the std namespace. */
10770 decl_in_std_namespace_p (tree decl
)
10774 decl
= decl_namespace_context (decl
);
10775 if (DECL_NAMESPACE_STD_P (decl
))
10777 /* Allow inline namespaces inside of std namespace, e.g. with
10778 --enable-symvers=gnu-versioned-namespace std::forward would be
10779 actually std::_8::forward. */
10780 if (!DECL_NAMESPACE_INLINE_P (decl
))
10782 decl
= CP_DECL_CONTEXT (decl
);
10787 /* Returns true if FN, a CALL_EXPR, is a call to std::forward. */
10790 is_std_forward_p (tree fn
)
10792 /* std::forward only takes one argument. */
10793 if (call_expr_nargs (fn
) != 1)
10796 tree fndecl
= cp_get_callee_fndecl_nofold (fn
);
10797 if (!decl_in_std_namespace_p (fndecl
))
10800 tree name
= DECL_NAME (fndecl
);
10801 return name
&& id_equal (name
, "forward");
10804 /* Returns true if FN, a CALL_EXPR, is a call to std::move. */
10807 is_std_move_p (tree fn
)
10809 /* std::move only takes one argument. */
10810 if (call_expr_nargs (fn
) != 1)
10813 tree fndecl
= cp_get_callee_fndecl_nofold (fn
);
10814 if (!decl_in_std_namespace_p (fndecl
))
10817 tree name
= DECL_NAME (fndecl
);
10818 return name
&& id_equal (name
, "move");
10821 /* Returns true if RETVAL is a good candidate for the NRVO as per
10822 [class.copy.elision]. FUNCTYPE is the type the function is declared
10826 can_do_nrvo_p (tree retval
, tree functype
)
10828 if (functype
== error_mark_node
)
10831 STRIP_ANY_LOCATION_WRAPPER (retval
);
10832 tree result
= DECL_RESULT (current_function_decl
);
10833 return (retval
!= NULL_TREE
10834 && !processing_template_decl
10835 /* Must be a local, automatic variable. */
10837 && DECL_CONTEXT (retval
) == current_function_decl
10838 && !TREE_STATIC (retval
)
10839 /* And not a lambda or anonymous union proxy. */
10840 && !DECL_HAS_VALUE_EXPR_P (retval
)
10841 && (DECL_ALIGN (retval
) <= DECL_ALIGN (result
))
10842 /* The cv-unqualified type of the returned value must be the
10843 same as the cv-unqualified return type of the
10845 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (retval
)),
10846 TYPE_MAIN_VARIANT (functype
))
10847 /* And the returned value must be non-volatile. */
10848 && !TYPE_VOLATILE (TREE_TYPE (retval
)));
10851 /* True if we would like to perform NRVO, i.e. can_do_nrvo_p is true and we
10852 would otherwise return in memory. */
10855 want_nrvo_p (tree retval
, tree functype
)
10857 return (can_do_nrvo_p (retval
, functype
)
10858 && aggregate_value_p (functype
, current_function_decl
));
10861 /* Like can_do_nrvo_p, but we check if we're trying to move a class
10865 can_elide_copy_prvalue_p (tree retval
, tree functype
)
10867 if (functype
== error_mark_node
)
10870 STRIP_ANY_LOCATION_WRAPPER (retval
);
10871 return (retval
!= NULL_TREE
10872 && !glvalue_p (retval
)
10873 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (retval
)),
10874 TYPE_MAIN_VARIANT (functype
))
10875 && !TYPE_VOLATILE (TREE_TYPE (retval
)));
10878 /* If we should treat RETVAL, an expression being returned, as if it were
10879 designated by an rvalue, returns it adjusted accordingly; otherwise, returns
10880 NULL_TREE. See [class.copy.elision]. RETURN_P is true if this is a return
10881 context (rather than throw). */
10884 treat_lvalue_as_rvalue_p (tree expr
, bool return_p
)
10886 if (cxx_dialect
== cxx98
)
10889 tree retval
= expr
;
10890 STRIP_ANY_LOCATION_WRAPPER (retval
);
10891 if (REFERENCE_REF_P (retval
))
10892 retval
= TREE_OPERAND (retval
, 0);
10894 /* An implicitly movable entity is a variable of automatic storage duration
10895 that is either a non-volatile object or (C++20) an rvalue reference to a
10896 non-volatile object type. */
10897 if (!(((VAR_P (retval
) && !DECL_HAS_VALUE_EXPR_P (retval
))
10898 || TREE_CODE (retval
) == PARM_DECL
)
10899 && !TREE_STATIC (retval
)
10900 && !CP_TYPE_VOLATILE_P (non_reference (TREE_TYPE (retval
)))
10901 && (TREE_CODE (TREE_TYPE (retval
)) != REFERENCE_TYPE
10902 || (cxx_dialect
>= cxx20
10903 && TYPE_REF_IS_RVALUE (TREE_TYPE (retval
))))))
10906 /* If the expression in a return or co_return statement is a (possibly
10907 parenthesized) id-expression that names an implicitly movable entity
10908 declared in the body or parameter-declaration-clause of the innermost
10909 enclosing function or lambda-expression, */
10912 if (DECL_CONTEXT (retval
) != current_function_decl
)
10914 expr
= move (expr
);
10915 if (expr
== error_mark_node
)
10917 return set_implicit_rvalue_p (expr
);
10920 /* if the id-expression (possibly parenthesized) is the operand of
10921 a throw-expression, and names an implicitly movable entity that belongs
10922 to a scope that does not contain the compound-statement of the innermost
10923 lambda-expression, try-block, or function-try-block (if any) whose
10924 compound-statement or ctor-initializer contains the throw-expression. */
10926 /* C++20 added move on throw of parms. */
10927 if (TREE_CODE (retval
) == PARM_DECL
&& cxx_dialect
< cxx20
)
10930 /* We don't check for lambda-expression here, because we should not get past
10931 the DECL_HAS_VALUE_EXPR_P check above. */
10932 for (cp_binding_level
*b
= current_binding_level
;
10933 b
->kind
!= sk_namespace
; b
= b
->level_chain
)
10935 for (tree decl
= b
->names
; decl
; decl
= TREE_CHAIN (decl
))
10936 if (decl
== retval
)
10937 return set_implicit_rvalue_p (move (expr
));
10938 if (b
->kind
== sk_try
)
10942 return set_implicit_rvalue_p (move (expr
));
10945 /* Warn about dubious usage of std::move (in a return statement, if RETURN_P
10946 is true). EXPR is the std::move expression; TYPE is the type of the object
10947 being initialized. */
10950 maybe_warn_pessimizing_move (tree expr
, tree type
, bool return_p
)
10952 if (!(warn_pessimizing_move
|| warn_redundant_move
))
10955 const location_t loc
= cp_expr_loc_or_input_loc (expr
);
10957 /* C++98 doesn't know move. */
10958 if (cxx_dialect
< cxx11
)
10961 /* Wait until instantiation time, since we can't gauge if we should do
10962 the NRVO until then. */
10963 if (processing_template_decl
)
10966 /* This is only interesting for class types. */
10967 if (!CLASS_TYPE_P (type
))
10970 bool wrapped_p
= false;
10971 /* A a = std::move (A()); */
10972 if (TREE_CODE (expr
) == TREE_LIST
)
10974 if (list_length (expr
) == 1)
10976 expr
= TREE_VALUE (expr
);
10982 /* A a = {std::move (A())};
10983 A a{std::move (A())}; */
10984 else if (TREE_CODE (expr
) == CONSTRUCTOR
)
10986 if (CONSTRUCTOR_NELTS (expr
) == 1)
10988 expr
= CONSTRUCTOR_ELT (expr
, 0)->value
;
10995 /* First, check if this is a call to std::move. */
10996 if (!REFERENCE_REF_P (expr
)
10997 || TREE_CODE (TREE_OPERAND (expr
, 0)) != CALL_EXPR
)
10999 tree fn
= TREE_OPERAND (expr
, 0);
11000 if (!is_std_move_p (fn
))
11002 tree arg
= CALL_EXPR_ARG (fn
, 0);
11003 if (TREE_CODE (arg
) != NOP_EXPR
)
11005 /* If we're looking at *std::move<T&> ((T &) &arg), do the pessimizing N/RVO
11006 and implicitly-movable warnings. */
11007 if (TREE_CODE (TREE_OPERAND (arg
, 0)) == ADDR_EXPR
)
11009 arg
= TREE_OPERAND (arg
, 0);
11010 arg
= TREE_OPERAND (arg
, 0);
11011 arg
= convert_from_reference (arg
);
11012 if (can_elide_copy_prvalue_p (arg
, type
))
11014 auto_diagnostic_group d
;
11015 if (warning_at (loc
, OPT_Wpessimizing_move
,
11016 "moving a temporary object prevents copy elision"))
11017 inform (loc
, "remove %<std::move%> call");
11019 /* The rest of the warnings is only relevant for when we are returning
11020 from a function. */
11025 /* Warn if we could do copy elision were it not for the move. */
11026 if (can_do_nrvo_p (arg
, type
))
11028 auto_diagnostic_group d
;
11029 if (!warning_suppressed_p (expr
, OPT_Wpessimizing_move
)
11030 && warning_at (loc
, OPT_Wpessimizing_move
,
11031 "moving a local object in a return statement "
11032 "prevents copy elision"))
11033 inform (loc
, "remove %<std::move%> call");
11035 /* Warn if the move is redundant. It is redundant when we would
11036 do maybe-rvalue overload resolution even without std::move. */
11037 else if (warn_redundant_move
11038 /* This doesn't apply for return {std::move (t)};. */
11040 && !warning_suppressed_p (expr
, OPT_Wredundant_move
)
11041 && (moved
= treat_lvalue_as_rvalue_p (arg
, /*return*/true)))
11043 /* Make sure that overload resolution would actually succeed
11044 if we removed the std::move call. */
11045 tree t
= convert_for_initialization (NULL_TREE
, type
,
11048 | LOOKUP_ONLYCONVERTING
),
11049 ICR_RETURN
, NULL_TREE
, 0,
11051 /* If this worked, implicit rvalue would work, so the call to
11052 std::move is redundant. */
11053 if (t
!= error_mark_node
)
11055 auto_diagnostic_group d
;
11056 if (warning_at (loc
, OPT_Wredundant_move
,
11057 "redundant move in return statement"))
11058 inform (loc
, "remove %<std::move%> call");
11062 /* Also try to warn about redundant std::move in code such as
11065 return std::move(t);
11067 for which EXPR will be something like
11068 *std::move<const T&> ((const struct T &) (const struct T *) t)
11069 and where the std::move does nothing if T does not have a T(const T&&)
11070 constructor, because the argument is const. It will not use T(T&&)
11071 because that would mean losing the const. */
11072 else if (warn_redundant_move
11073 && !warning_suppressed_p (expr
, OPT_Wredundant_move
)
11074 && TYPE_REF_P (TREE_TYPE (arg
))
11075 && CP_TYPE_CONST_P (TREE_TYPE (TREE_TYPE (arg
))))
11077 tree rtype
= TREE_TYPE (TREE_TYPE (arg
));
11078 if (!same_type_ignoring_top_level_qualifiers_p (rtype
, type
))
11080 /* Check for the unlikely case there's T(const T&&) (we don't care if
11082 for (tree fn
: ovl_range (CLASSTYPE_CONSTRUCTORS (rtype
)))
11083 if (move_fn_p (fn
))
11085 tree t
= TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (fn
));
11086 if (UNLIKELY (CP_TYPE_CONST_P (TREE_TYPE (t
))))
11089 auto_diagnostic_group d
;
11091 ? warning_at (loc
, OPT_Wredundant_move
,
11092 "redundant move in return statement")
11093 : warning_at (loc
, OPT_Wredundant_move
,
11094 "redundant move in initialization"))
11095 inform (loc
, "remove %<std::move%> call");
11099 /* Check that returning RETVAL from the current function is valid.
11100 Return an expression explicitly showing all conversions required to
11101 change RETVAL into the function return type, and to assign it to
11102 the DECL_RESULT for the function. Set *NO_WARNING to true if
11103 code reaches end of non-void function warning shouldn't be issued
11104 on this RETURN_EXPR. Set *DANGLING to true if code returns the
11105 address of a local variable. */
11108 check_return_expr (tree retval
, bool *no_warning
, bool *dangling
)
11111 /* The type actually returned by the function. */
11113 /* The type the function is declared to return, or void if
11114 the declared type is incomplete. */
11116 int fn_returns_value_p
;
11117 location_t loc
= cp_expr_loc_or_input_loc (retval
);
11119 *no_warning
= false;
11122 /* A `volatile' function is one that isn't supposed to return, ever.
11123 (This is a G++ extension, used to get better code for functions
11124 that call the `volatile' function.) */
11125 if (TREE_THIS_VOLATILE (current_function_decl
))
11126 warning (0, "function declared %<noreturn%> has a %<return%> statement");
11128 /* Check for various simple errors. */
11129 if (DECL_DESTRUCTOR_P (current_function_decl
))
11132 error_at (loc
, "returning a value from a destructor");
11134 if (targetm
.cxx
.cdtor_returns_this () && !processing_template_decl
)
11135 retval
= current_class_ptr
;
11139 else if (DECL_CONSTRUCTOR_P (current_function_decl
))
11141 if (in_function_try_handler
)
11142 /* If a return statement appears in a handler of the
11143 function-try-block of a constructor, the program is ill-formed. */
11144 error ("cannot return from a handler of a function-try-block of a constructor");
11146 /* You can't return a value from a constructor. */
11147 error_at (loc
, "returning a value from a constructor");
11149 if (targetm
.cxx
.cdtor_returns_this () && !processing_template_decl
)
11150 retval
= current_class_ptr
;
11155 const tree saved_retval
= retval
;
11157 if (processing_template_decl
)
11159 current_function_returns_value
= 1;
11161 if (check_for_bare_parameter_packs (retval
))
11162 return error_mark_node
;
11164 /* If one of the types might be void, we can't tell whether we're
11165 returning a value. */
11166 if ((WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl
)))
11167 && !FNDECL_USED_AUTO (current_function_decl
))
11168 || (retval
!= NULL_TREE
11169 && (TREE_TYPE (retval
) == NULL_TREE
11170 || WILDCARD_TYPE_P (TREE_TYPE (retval
)))))
11174 functype
= TREE_TYPE (TREE_TYPE (current_function_decl
));
11176 /* Deduce auto return type from a return statement. */
11177 if (FNDECL_USED_AUTO (current_function_decl
))
11179 tree pattern
= DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl
);
11183 if (!retval
&& !is_auto (pattern
))
11185 /* Give a helpful error message. */
11186 auto_diagnostic_group d
;
11187 error ("return-statement with no value, in function returning %qT",
11189 inform (input_location
, "only plain %<auto%> return type can be "
11190 "deduced to %<void%>");
11191 type
= error_mark_node
;
11193 else if (retval
&& BRACE_ENCLOSED_INITIALIZER_P (retval
))
11195 error ("returning initializer list");
11196 type
= error_mark_node
;
11201 retval
= void_node
;
11202 auto_node
= type_uses_auto (pattern
);
11203 type
= do_auto_deduction (pattern
, retval
, auto_node
,
11204 tf_warning_or_error
, adc_return_type
);
11207 if (type
== error_mark_node
)
11209 else if (functype
== pattern
)
11210 apply_deduced_return_type (current_function_decl
, type
);
11211 else if (!same_type_p (type
, functype
))
11213 if (LAMBDA_FUNCTION_P (current_function_decl
))
11214 error_at (loc
, "inconsistent types %qT and %qT deduced for "
11215 "lambda return type", functype
, type
);
11217 error_at (loc
, "inconsistent deduction for auto return type: "
11218 "%qT and then %qT", functype
, type
);
11223 result
= DECL_RESULT (current_function_decl
);
11224 valtype
= TREE_TYPE (result
);
11225 gcc_assert (valtype
!= NULL_TREE
);
11226 fn_returns_value_p
= !VOID_TYPE_P (valtype
);
11228 /* Check for a return statement with no return value in a function
11229 that's supposed to return a value. */
11230 if (!retval
&& fn_returns_value_p
)
11232 if (functype
!= error_mark_node
)
11233 permerror (input_location
, "return-statement with no value, in "
11234 "function returning %qT", valtype
);
11235 /* Remember that this function did return. */
11236 current_function_returns_value
= 1;
11237 /* And signal caller that TREE_NO_WARNING should be set on the
11238 RETURN_EXPR to avoid control reaches end of non-void function
11239 warnings in tree-cfg.cc. */
11240 *no_warning
= true;
11242 /* Check for a return statement with a value in a function that
11243 isn't supposed to return a value. */
11244 else if (retval
&& !fn_returns_value_p
)
11246 if (VOID_TYPE_P (TREE_TYPE (retval
)))
11247 /* You can return a `void' value from a function of `void'
11248 type. In that case, we have to evaluate the expression for
11249 its side-effects. */
11250 finish_expr_stmt (retval
);
11251 else if (retval
!= error_mark_node
)
11252 permerror (loc
, "return-statement with a value, in function "
11253 "returning %qT", valtype
);
11254 current_function_returns_null
= 1;
11256 /* There's really no value to return, after all. */
11260 /* Remember that this function can sometimes return without a
11262 current_function_returns_null
= 1;
11264 /* Remember that this function did return a value. */
11265 current_function_returns_value
= 1;
11267 /* Check for erroneous operands -- but after giving ourselves a
11268 chance to provide an error about returning a value from a void
11270 if (error_operand_p (retval
))
11272 current_function_return_value
= error_mark_node
;
11273 return error_mark_node
;
11276 /* Only operator new(...) throw(), can return NULL [expr.new/13]. */
11277 if (IDENTIFIER_NEW_OP_P (DECL_NAME (current_function_decl
))
11278 && !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl
))
11279 && ! flag_check_new
11280 && retval
&& null_ptr_cst_p (retval
))
11281 warning (0, "%<operator new%> must not return NULL unless it is "
11282 "declared %<throw()%> (or %<-fcheck-new%> is in effect)");
11284 /* Effective C++ rule 15. See also start_function. */
11286 && DECL_NAME (current_function_decl
) == assign_op_identifier
11287 && !type_dependent_expression_p (retval
))
11291 /* The function return type must be a reference to the current
11293 if (TYPE_REF_P (valtype
)
11294 && same_type_ignoring_top_level_qualifiers_p
11295 (TREE_TYPE (valtype
), TREE_TYPE (current_class_ref
)))
11297 /* Returning '*this' is obviously OK. */
11298 if (retval
== current_class_ref
)
11300 /* If we are calling a function whose return type is the same of
11301 the current class reference, it is ok. */
11302 else if (INDIRECT_REF_P (retval
)
11303 && TREE_CODE (TREE_OPERAND (retval
, 0)) == CALL_EXPR
)
11308 warning_at (loc
, OPT_Weffc__
,
11309 "%<operator=%> should return a reference to %<*this%>");
11312 if (dependent_type_p (functype
)
11313 || type_dependent_expression_p (retval
))
11316 /* We should not have changed the return value. */
11317 gcc_assert (retval
== saved_retval
);
11318 /* We don't know if this is an lvalue or rvalue use, but
11319 either way we can mark it as read. */
11320 mark_exp_read (retval
);
11324 /* The fabled Named Return Value optimization, as per [class.copy]/15:
11326 [...] For a function with a class return type, if the expression
11327 in the return statement is the name of a local object, and the cv-
11328 unqualified type of the local object is the same as the function
11329 return type, an implementation is permitted to omit creating the tem-
11330 porary object to hold the function return value [...]
11332 So, if this is a value-returning function that always returns the same
11333 local variable, remember it.
11335 We choose the first suitable variable even if the function sometimes
11336 returns something else, but only if the variable is out of scope at the
11337 other return sites, or else we run the risk of clobbering the variable we
11338 chose if the other returned expression uses the chosen variable somehow.
11340 We don't currently do this if the first return is a non-variable, as it
11341 would be complicated to determine whether an NRV selected later was in
11342 scope at the point of the earlier return. We also don't currently support
11343 multiple variables with non-overlapping scopes (53637).
11345 See finish_function and finalize_nrv for the rest of this optimization. */
11346 tree bare_retval
= NULL_TREE
;
11349 retval
= maybe_undo_parenthesized_ref (retval
);
11350 bare_retval
= tree_strip_any_location_wrapper (retval
);
11353 bool named_return_value_okay_p
= want_nrvo_p (bare_retval
, functype
);
11354 if (fn_returns_value_p
&& flag_elide_constructors
11355 && current_function_return_value
!= bare_retval
)
11357 if (named_return_value_okay_p
11358 && current_function_return_value
== NULL_TREE
)
11359 current_function_return_value
= bare_retval
;
11360 else if (current_function_return_value
11361 && VAR_P (current_function_return_value
)
11362 && DECL_NAME (current_function_return_value
)
11363 && !decl_in_scope_p (current_function_return_value
))
11365 /* The earlier NRV is out of scope at this point, so it's safe to
11366 leave it alone; the current return can't refer to it. */;
11367 if (named_return_value_okay_p
11368 && !warning_suppressed_p (current_function_decl
, OPT_Wnrvo
))
11370 warning (OPT_Wnrvo
, "not eliding copy on return from %qD",
11372 suppress_warning (current_function_decl
, OPT_Wnrvo
);
11377 if ((named_return_value_okay_p
11378 || (current_function_return_value
11379 && current_function_return_value
!= error_mark_node
))
11380 && !warning_suppressed_p (current_function_decl
, OPT_Wnrvo
))
11382 warning (OPT_Wnrvo
, "not eliding copy on return in %qD",
11383 current_function_decl
);
11384 suppress_warning (current_function_decl
, OPT_Wnrvo
);
11386 current_function_return_value
= error_mark_node
;
11390 /* We don't need to do any conversions when there's nothing being
11395 if (!named_return_value_okay_p
)
11396 maybe_warn_pessimizing_move (retval
, functype
, /*return_p*/true);
11398 /* Do any required conversions. */
11399 if (bare_retval
== result
|| DECL_CONSTRUCTOR_P (current_function_decl
))
11400 /* No conversions are required. */
11404 int flags
= LOOKUP_NORMAL
| LOOKUP_ONLYCONVERTING
;
11406 /* The functype's return type will have been set to void, if it
11407 was an incomplete type. Just treat this as 'return;' */
11408 if (VOID_TYPE_P (functype
))
11409 return error_mark_node
;
11411 /* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
11412 treated as an rvalue for the purposes of overload resolution to
11413 favor move constructors over copy constructors.
11415 Note that these conditions are similar to, but not as strict as,
11416 the conditions for the named return value optimization. */
11417 bool converted
= false;
11419 /* Until C++23, this was only interesting for class type, but in C++23,
11420 we should do the below when we're converting rom/to a class/reference
11421 (a non-scalar type). */
11422 if ((cxx_dialect
< cxx23
11423 ? CLASS_TYPE_P (functype
)
11424 : !SCALAR_TYPE_P (functype
) || !SCALAR_TYPE_P (TREE_TYPE (retval
)))
11425 && (moved
= treat_lvalue_as_rvalue_p (retval
, /*return*/true)))
11426 /* In C++20 and earlier we treat the return value as an rvalue
11427 that can bind to lvalue refs. In C++23, such an expression is just
11428 an xvalue (see reference_binding). */
11431 /* The call in a (lambda) thunk needs no conversions. */
11432 if (TREE_CODE (retval
) == CALL_EXPR
11433 && call_from_lambda_thunk_p (retval
))
11436 /* First convert the value to the function's return type, then
11437 to the type of return value's location to handle the
11438 case that functype is smaller than the valtype. */
11440 retval
= convert_for_initialization
11441 (NULL_TREE
, functype
, retval
, flags
, ICR_RETURN
, NULL_TREE
, 0,
11442 tf_warning_or_error
);
11443 retval
= convert (valtype
, retval
);
11445 /* If the conversion failed, treat this just like `return;'. */
11446 if (retval
== error_mark_node
)
11448 /* We can't initialize a register from a AGGR_INIT_EXPR. */
11449 else if (! cfun
->returns_struct
11450 && TREE_CODE (retval
) == TARGET_EXPR
11451 && TREE_CODE (TARGET_EXPR_INITIAL (retval
)) == AGGR_INIT_EXPR
)
11452 retval
= build2 (COMPOUND_EXPR
, TREE_TYPE (retval
), retval
,
11453 TARGET_EXPR_SLOT (retval
));
11454 else if (!processing_template_decl
11455 && maybe_warn_about_returning_address_of_local (retval
, loc
)
11456 && INDIRECT_TYPE_P (valtype
))
11460 /* A naive attempt to reduce the number of -Wdangling-reference false
11461 positives: if we know that this function can return a variable with
11462 static storage duration rather than one of its parameters, suppress
11464 if (warn_dangling_reference
11465 && TYPE_REF_P (functype
)
11467 && VAR_P (bare_retval
)
11468 && TREE_STATIC (bare_retval
))
11469 suppress_warning (current_function_decl
, OPT_Wdangling_reference
);
11471 if (processing_template_decl
)
11472 return saved_retval
;
11474 /* Actually copy the value returned into the appropriate location. */
11475 if (retval
&& retval
!= result
)
11476 retval
= cp_build_init_expr (result
, retval
);
11478 if (current_function_return_value
== bare_retval
)
11479 INIT_EXPR_NRV_P (retval
) = true;
11481 if (tree set
= maybe_set_retval_sentinel ())
11482 retval
= build2 (COMPOUND_EXPR
, void_type_node
, retval
, set
);
11488 /* Returns nonzero if the pointer-type FROM can be converted to the
11489 pointer-type TO via a qualification conversion. If CONSTP is -1,
11490 then we return nonzero if the pointers are similar, and the
11491 cv-qualification signature of FROM is a proper subset of that of TO.
11493 If CONSTP is positive, then all outer pointers have been
11494 const-qualified. */
11497 comp_ptr_ttypes_real (tree to
, tree from
, int constp
)
11499 bool to_more_cv_qualified
= false;
11500 bool is_opaque_pointer
= false;
11502 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
11504 if (TREE_CODE (to
) != TREE_CODE (from
))
11507 if (TREE_CODE (from
) == OFFSET_TYPE
11508 && !same_type_p (TYPE_OFFSET_BASETYPE (from
),
11509 TYPE_OFFSET_BASETYPE (to
)))
11512 /* Const and volatile mean something different for function and
11513 array types, so the usual checks are not appropriate. We'll
11514 check the array type elements in further iterations. */
11515 if (!FUNC_OR_METHOD_TYPE_P (to
) && TREE_CODE (to
) != ARRAY_TYPE
)
11517 if (!at_least_as_qualified_p (to
, from
))
11520 if (!at_least_as_qualified_p (from
, to
))
11524 to_more_cv_qualified
= true;
11528 constp
&= TYPE_READONLY (to
);
11531 if (VECTOR_TYPE_P (to
))
11532 is_opaque_pointer
= vector_targets_convertible_p (to
, from
);
11534 /* P0388R4 allows a conversion from int[N] to int[] but not the
11535 other way round. When both arrays have bounds but they do
11536 not match, then no conversion is possible. */
11537 if (TREE_CODE (to
) == ARRAY_TYPE
11538 && !comp_array_types (to
, from
, bounds_first
, /*strict=*/false))
11541 if (!TYPE_PTR_P (to
)
11542 && !TYPE_PTRDATAMEM_P (to
)
11543 /* CWG 330 says we need to look through arrays. */
11544 && TREE_CODE (to
) != ARRAY_TYPE
)
11545 return ((constp
>= 0 || to_more_cv_qualified
)
11546 && (is_opaque_pointer
11547 || same_type_ignoring_top_level_qualifiers_p (to
, from
)));
11551 /* When comparing, say, char ** to char const **, this function takes
11552 the 'char *' and 'char const *'. Do not pass non-pointer/reference
11553 types to this function. */
11556 comp_ptr_ttypes (tree to
, tree from
)
11558 return comp_ptr_ttypes_real (to
, from
, 1);
11561 /* Returns true iff FNTYPE is a non-class type that involves
11562 error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
11563 if a parameter type is ill-formed. */
11566 error_type_p (const_tree type
)
11570 switch (TREE_CODE (type
))
11576 case REFERENCE_TYPE
:
11578 return error_type_p (TREE_TYPE (type
));
11580 case FUNCTION_TYPE
:
11582 if (error_type_p (TREE_TYPE (type
)))
11584 for (t
= TYPE_ARG_TYPES (type
); t
; t
= TREE_CHAIN (t
))
11585 if (error_type_p (TREE_VALUE (t
)))
11590 if (TYPE_PTRMEMFUNC_P (type
))
11591 return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type
));
11599 /* Returns true if to and from are (possibly multi-level) pointers to the same
11600 type or inheritance-related types, regardless of cv-quals. */
11603 ptr_reasonably_similar (const_tree to
, const_tree from
)
11605 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
11607 /* Any target type is similar enough to void. */
11608 if (VOID_TYPE_P (to
))
11609 return !error_type_p (from
);
11610 if (VOID_TYPE_P (from
))
11611 return !error_type_p (to
);
11613 if (TREE_CODE (to
) != TREE_CODE (from
))
11616 if (TREE_CODE (from
) == OFFSET_TYPE
11617 && comptypes (TYPE_OFFSET_BASETYPE (to
),
11618 TYPE_OFFSET_BASETYPE (from
),
11619 COMPARE_BASE
| COMPARE_DERIVED
))
11622 if (VECTOR_TYPE_P (to
)
11623 && vector_types_convertible_p (to
, from
, false))
11626 if (TREE_CODE (to
) == INTEGER_TYPE
11627 && TYPE_PRECISION (to
) == TYPE_PRECISION (from
))
11630 if (TREE_CODE (to
) == FUNCTION_TYPE
)
11631 return !error_type_p (to
) && !error_type_p (from
);
11633 if (!TYPE_PTR_P (to
))
11635 /* When either type is incomplete avoid DERIVED_FROM_P,
11636 which may call complete_type (c++/57942). */
11637 bool b
= !COMPLETE_TYPE_P (to
) || !COMPLETE_TYPE_P (from
);
11639 (TYPE_MAIN_VARIANT (to
), TYPE_MAIN_VARIANT (from
),
11640 b
? COMPARE_STRICT
: COMPARE_BASE
| COMPARE_DERIVED
);
11645 /* Return true if TO and FROM (both of which are POINTER_TYPEs or
11646 pointer-to-member types) are the same, ignoring cv-qualification at
11647 all levels. CB says how we should behave when comparing array bounds. */
11650 comp_ptr_ttypes_const (tree to
, tree from
, compare_bounds_t cb
)
11652 bool is_opaque_pointer
= false;
11654 for (; ; to
= TREE_TYPE (to
), from
= TREE_TYPE (from
))
11656 if (TREE_CODE (to
) != TREE_CODE (from
))
11659 if (TREE_CODE (from
) == OFFSET_TYPE
11660 && same_type_p (TYPE_OFFSET_BASETYPE (from
),
11661 TYPE_OFFSET_BASETYPE (to
)))
11664 if (VECTOR_TYPE_P (to
))
11665 is_opaque_pointer
= vector_targets_convertible_p (to
, from
);
11667 if (TREE_CODE (to
) == ARRAY_TYPE
11668 /* Ignore cv-qualification, but if we see e.g. int[3] and int[4],
11670 && !comp_array_types (to
, from
, cb
, /*strict=*/false))
11673 /* CWG 330 says we need to look through arrays. */
11674 if (!TYPE_PTR_P (to
) && TREE_CODE (to
) != ARRAY_TYPE
)
11675 return (is_opaque_pointer
11676 || same_type_ignoring_top_level_qualifiers_p (to
, from
));
11680 /* Returns the type qualifiers for this type, including the qualifiers on the
11681 elements for an array type. */
11684 cp_type_quals (const_tree type
)
11687 /* This CONST_CAST is okay because strip_array_types returns its
11688 argument unmodified and we assign it to a const_tree. */
11689 type
= strip_array_types (CONST_CAST_TREE (type
));
11690 if (type
== error_mark_node
11691 /* Quals on a FUNCTION_TYPE are memfn quals. */
11692 || TREE_CODE (type
) == FUNCTION_TYPE
)
11693 return TYPE_UNQUALIFIED
;
11694 quals
= TYPE_QUALS (type
);
11695 /* METHOD and REFERENCE_TYPEs should never have quals. */
11696 gcc_assert ((TREE_CODE (type
) != METHOD_TYPE
11697 && !TYPE_REF_P (type
))
11698 || ((quals
& (TYPE_QUAL_CONST
|TYPE_QUAL_VOLATILE
))
11699 == TYPE_UNQUALIFIED
));
11703 /* Returns the function-ref-qualifier for TYPE */
11706 type_memfn_rqual (const_tree type
)
11708 gcc_assert (FUNC_OR_METHOD_TYPE_P (type
));
11710 if (!FUNCTION_REF_QUALIFIED (type
))
11711 return REF_QUAL_NONE
;
11712 else if (FUNCTION_RVALUE_QUALIFIED (type
))
11713 return REF_QUAL_RVALUE
;
11715 return REF_QUAL_LVALUE
;
11718 /* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
11722 type_memfn_quals (const_tree type
)
11724 if (TREE_CODE (type
) == FUNCTION_TYPE
)
11725 return TYPE_QUALS (type
);
11726 else if (TREE_CODE (type
) == METHOD_TYPE
)
11727 return cp_type_quals (class_of_this_parm (type
));
11729 gcc_unreachable ();
11732 /* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
11733 MEMFN_QUALS and its ref-qualifier to RQUAL. */
11736 apply_memfn_quals (tree type
, cp_cv_quals memfn_quals
, cp_ref_qualifier rqual
)
11738 /* Could handle METHOD_TYPE here if necessary. */
11739 gcc_assert (TREE_CODE (type
) == FUNCTION_TYPE
);
11740 if (TYPE_QUALS (type
) == memfn_quals
11741 && type_memfn_rqual (type
) == rqual
)
11744 /* This should really have a different TYPE_MAIN_VARIANT, but that gets
11746 tree result
= build_qualified_type (type
, memfn_quals
);
11747 return build_ref_qualified_type (result
, rqual
);
11750 /* Returns nonzero if TYPE is const or volatile. */
11753 cv_qualified_p (const_tree type
)
11755 int quals
= cp_type_quals (type
);
11756 return (quals
& (TYPE_QUAL_CONST
|TYPE_QUAL_VOLATILE
)) != 0;
11759 /* Returns nonzero if the TYPE contains a mutable member. */
11762 cp_has_mutable_p (const_tree type
)
11764 /* This CONST_CAST is okay because strip_array_types returns its
11765 argument unmodified and we assign it to a const_tree. */
11766 type
= strip_array_types (CONST_CAST_TREE(type
));
11768 return CLASS_TYPE_P (type
) && CLASSTYPE_HAS_MUTABLE (type
);
11771 /* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
11772 TYPE_QUALS. For a VAR_DECL, this may be an optimistic
11773 approximation. In particular, consider:
11776 struct S { int i; };
11777 const S s = { f(); }
11779 Here, we will make "s" as TREE_READONLY (because it is declared
11780 "const") -- only to reverse ourselves upon seeing that the
11781 initializer is non-constant. */
11784 cp_apply_type_quals_to_decl (int type_quals
, tree decl
)
11786 tree type
= TREE_TYPE (decl
);
11788 if (type
== error_mark_node
)
11791 if (TREE_CODE (decl
) == TYPE_DECL
)
11794 gcc_assert (!(TREE_CODE (type
) == FUNCTION_TYPE
11795 && type_quals
!= TYPE_UNQUALIFIED
));
11797 /* Avoid setting TREE_READONLY incorrectly. */
11798 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
11799 constructor can produce constant init, so rely on cp_finish_decl to
11800 clear TREE_READONLY if the variable has non-constant init. */
11802 /* If the type has (or might have) a mutable component, that component
11803 might be modified. */
11804 if (TYPE_HAS_MUTABLE_P (type
) || !COMPLETE_TYPE_P (type
))
11805 type_quals
&= ~TYPE_QUAL_CONST
;
11807 c_apply_type_quals_to_decl (type_quals
, decl
);
11810 /* Subroutine of casts_away_constness. Make T1 and T2 point at
11811 exemplar types such that casting T1 to T2 is casting away constness
11812 if and only if there is no implicit conversion from T1 to T2. */
11815 casts_away_constness_r (tree
*t1
, tree
*t2
, tsubst_flags_t complain
)
11820 /* [expr.const.cast]
11822 For multi-level pointer to members and multi-level mixed pointers
11823 and pointers to members (conv.qual), the "member" aspect of a
11824 pointer to member level is ignored when determining if a const
11825 cv-qualifier has been cast away. */
11826 /* [expr.const.cast]
11828 For two pointer types:
11830 X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
11831 X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
11834 casting from X1 to X2 casts away constness if, for a non-pointer
11835 type T there does not exist an implicit conversion (clause
11838 Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
11842 Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
11843 if ((!TYPE_PTR_P (*t1
) && !TYPE_PTRDATAMEM_P (*t1
))
11844 || (!TYPE_PTR_P (*t2
) && !TYPE_PTRDATAMEM_P (*t2
)))
11846 *t1
= cp_build_qualified_type (void_type_node
,
11847 cp_type_quals (*t1
));
11848 *t2
= cp_build_qualified_type (void_type_node
,
11849 cp_type_quals (*t2
));
11853 quals1
= cp_type_quals (*t1
);
11854 quals2
= cp_type_quals (*t2
);
11856 if (TYPE_PTRDATAMEM_P (*t1
))
11857 *t1
= TYPE_PTRMEM_POINTED_TO_TYPE (*t1
);
11859 *t1
= TREE_TYPE (*t1
);
11860 if (TYPE_PTRDATAMEM_P (*t2
))
11861 *t2
= TYPE_PTRMEM_POINTED_TO_TYPE (*t2
);
11863 *t2
= TREE_TYPE (*t2
);
11865 casts_away_constness_r (t1
, t2
, complain
);
11866 *t1
= build_pointer_type (*t1
);
11867 *t2
= build_pointer_type (*t2
);
11868 *t1
= cp_build_qualified_type (*t1
, quals1
);
11869 *t2
= cp_build_qualified_type (*t2
, quals2
);
11872 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
11875 ??? This function returns non-zero if casting away qualifiers not
11876 just const. We would like to return to the caller exactly which
11877 qualifiers are casted away to give more accurate diagnostics.
11881 casts_away_constness (tree t1
, tree t2
, tsubst_flags_t complain
)
11883 if (TYPE_REF_P (t2
))
11885 /* [expr.const.cast]
11887 Casting from an lvalue of type T1 to an lvalue of type T2
11888 using a reference cast casts away constness if a cast from an
11889 rvalue of type "pointer to T1" to the type "pointer to T2"
11890 casts away constness. */
11891 t1
= (TYPE_REF_P (t1
) ? TREE_TYPE (t1
) : t1
);
11892 return casts_away_constness (build_pointer_type (t1
),
11893 build_pointer_type (TREE_TYPE (t2
)),
11897 if (TYPE_PTRDATAMEM_P (t1
) && TYPE_PTRDATAMEM_P (t2
))
11898 /* [expr.const.cast]
11900 Casting from an rvalue of type "pointer to data member of X
11901 of type T1" to the type "pointer to data member of Y of type
11902 T2" casts away constness if a cast from an rvalue of type
11903 "pointer to T1" to the type "pointer to T2" casts away
11905 return casts_away_constness
11906 (build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1
)),
11907 build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2
)),
11910 /* Casting away constness is only something that makes sense for
11911 pointer or reference types. */
11912 if (!TYPE_PTR_P (t1
) || !TYPE_PTR_P (t2
))
11915 /* Top-level qualifiers don't matter. */
11916 t1
= TYPE_MAIN_VARIANT (t1
);
11917 t2
= TYPE_MAIN_VARIANT (t2
);
11918 casts_away_constness_r (&t1
, &t2
, complain
);
11919 if (!can_convert (t2
, t1
, complain
))
11925 /* If T is a REFERENCE_TYPE return the type to which T refers.
11926 Otherwise, return T itself. */
11929 non_reference (tree t
)
11931 if (t
&& TYPE_REF_P (t
))
11937 /* Return nonzero if REF is an lvalue valid for this language;
11938 otherwise, print an error message and return zero. USE says
11939 how the lvalue is being used and so selects the error message. */
11942 lvalue_or_else (tree ref
, enum lvalue_use use
, tsubst_flags_t complain
)
11944 cp_lvalue_kind kind
= lvalue_kind (ref
);
11946 if (kind
== clk_none
)
11948 if (complain
& tf_error
)
11949 lvalue_error (cp_expr_loc_or_input_loc (ref
), use
);
11952 else if (kind
& (clk_rvalueref
|clk_class
))
11954 if (!(complain
& tf_error
))
11956 /* Make this a permerror because we used to accept it. */
11957 permerror (cp_expr_loc_or_input_loc (ref
),
11958 "using rvalue as lvalue");
11963 /* Return true if a user-defined literal operator is a raw operator. */
11966 check_raw_literal_operator (const_tree decl
)
11968 tree argtypes
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
11971 bool maybe_raw_p
= false;
11973 /* Count the number and type of arguments and check for ellipsis. */
11974 for (argtype
= argtypes
, arity
= 0;
11975 argtype
&& argtype
!= void_list_node
;
11976 ++arity
, argtype
= TREE_CHAIN (argtype
))
11978 tree t
= TREE_VALUE (argtype
);
11980 if (same_type_p (t
, const_string_type_node
))
11981 maybe_raw_p
= true;
11984 return false; /* Found ellipsis. */
11986 if (!maybe_raw_p
|| arity
!= 1)
11993 /* Return true if a user-defined literal operator has one of the allowed
11997 check_literal_operator_args (const_tree decl
,
11998 bool *long_long_unsigned_p
, bool *long_double_p
)
12000 tree argtypes
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
12002 *long_long_unsigned_p
= false;
12003 *long_double_p
= false;
12004 if (processing_template_decl
|| processing_specialization
)
12005 return argtypes
== void_list_node
;
12012 /* Count the number and type of arguments and check for ellipsis. */
12013 for (argtype
= argtypes
, arity
= 0;
12014 argtype
&& argtype
!= void_list_node
;
12015 argtype
= TREE_CHAIN (argtype
))
12017 tree t
= TREE_VALUE (argtype
);
12020 if (TYPE_PTR_P (t
))
12022 bool maybe_raw_p
= false;
12024 if (cp_type_quals (t
) != TYPE_QUAL_CONST
)
12026 t
= TYPE_MAIN_VARIANT (t
);
12027 if ((maybe_raw_p
= same_type_p (t
, char_type_node
))
12028 || same_type_p (t
, wchar_type_node
)
12029 || same_type_p (t
, char8_type_node
)
12030 || same_type_p (t
, char16_type_node
)
12031 || same_type_p (t
, char32_type_node
))
12033 argtype
= TREE_CHAIN (argtype
);
12036 t
= TREE_VALUE (argtype
);
12037 if (maybe_raw_p
&& argtype
== void_list_node
)
12039 else if (same_type_p (t
, size_type_node
))
12048 else if (same_type_p (t
, long_long_unsigned_type_node
))
12051 *long_long_unsigned_p
= true;
12053 else if (same_type_p (t
, long_double_type_node
))
12056 *long_double_p
= true;
12058 else if (same_type_p (t
, char_type_node
))
12060 else if (same_type_p (t
, wchar_type_node
))
12062 else if (same_type_p (t
, char8_type_node
))
12064 else if (same_type_p (t
, char16_type_node
))
12066 else if (same_type_p (t
, char32_type_node
))
12072 return false; /* Found ellipsis. */
12074 if (arity
!= max_arity
)
12081 /* Always returns false since unlike C90, C++ has no concept of implicit
12082 function declarations. */
12085 c_decl_implicit (const_tree
)