1 /* Perform -*- C++ -*- constant expression evaluation, including calls to
2 constexpr functions. These routines are used both during actual parsing
3 and during the instantiation of template functions.
5 Copyright (C) 1998-2024 Free Software Foundation, Inc.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
14 GCC is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
25 #include "coretypes.h"
28 #include "c-family/c-objc.h"
29 #include "tree-iterator.h"
32 #include "tree-inline.h"
35 #include "fold-const-call.h"
36 #include "stor-layout.h"
39 #include "stringpool.h"
41 #include "fold-const.h"
45 static bool verify_constant (tree
, bool, bool *, bool *);
46 #define VERIFY_CONSTANT(X) \
48 if (verify_constant ((X), ctx->quiet, non_constant_p, overflow_p)) \
52 static HOST_WIDE_INT
find_array_ctor_elt (tree ary
, tree dindex
,
54 static int array_index_cmp (tree key
, tree index
);
56 /* Returns true iff FUN is an instantiation of a constexpr function
57 template or a defaulted constexpr function. */
60 is_instantiation_of_constexpr (tree fun
)
62 return ((DECL_TEMPLOID_INSTANTIATION (fun
)
63 && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun
)))
64 || (DECL_DEFAULTED_FN (fun
)
65 && DECL_DECLARED_CONSTEXPR_P (fun
)));
68 /* Return true if T is a literal type. */
71 literal_type_p (tree t
)
76 || (VOID_TYPE_P (t
) && cxx_dialect
>= cxx14
))
80 t
= complete_type (t
);
81 gcc_assert (COMPLETE_TYPE_P (t
) || errorcount
);
82 return CLASSTYPE_LITERAL_P (t
);
84 if (TREE_CODE (t
) == ARRAY_TYPE
)
85 return literal_type_p (strip_array_types (t
));
89 /* If DECL is a variable declared `constexpr', require its type
90 be literal. Return error_mark_node if we give an error, the
94 ensure_literal_type_for_constexpr_object (tree decl
)
96 tree type
= TREE_TYPE (decl
);
98 && (DECL_DECLARED_CONSTEXPR_P (decl
)
99 || var_in_constexpr_fn (decl
))
100 && !processing_template_decl
)
102 tree stype
= strip_array_types (type
);
103 if (CLASS_TYPE_P (stype
) && !COMPLETE_TYPE_P (complete_type (stype
)))
104 /* Don't complain here, we'll complain about incompleteness
105 when we try to initialize the variable. */;
106 else if (!literal_type_p (type
))
108 if (DECL_DECLARED_CONSTEXPR_P (decl
))
110 auto_diagnostic_group d
;
111 error_at (DECL_SOURCE_LOCATION (decl
),
112 "the type %qT of %<constexpr%> variable %qD "
113 "is not literal", type
, decl
);
114 explain_non_literal_class (type
);
115 decl
= error_mark_node
;
117 else if (cxx_dialect
< cxx23
)
119 if (!is_instantiation_of_constexpr (current_function_decl
))
121 auto_diagnostic_group d
;
122 error_at (DECL_SOURCE_LOCATION (decl
),
123 "variable %qD of non-literal type %qT in "
124 "%<constexpr%> function only available with "
125 "%<-std=c++2b%> or %<-std=gnu++2b%>", decl
, type
);
126 explain_non_literal_class (type
);
127 decl
= error_mark_node
;
129 cp_function_chain
->invalid_constexpr
= true;
132 else if (DECL_DECLARED_CONSTEXPR_P (decl
)
133 && variably_modified_type_p (type
, NULL_TREE
))
135 error_at (DECL_SOURCE_LOCATION (decl
),
136 "%<constexpr%> variable %qD has variably-modified "
137 "type %qT", decl
, type
);
138 decl
= error_mark_node
;
144 /* Issue a diagnostic with text GMSGID for constructs that are invalid in
145 constexpr functions. CONSTEXPR_FUNDEF_P is true if we're checking
146 a constexpr function body; if so, don't report hard errors and issue
147 a pedwarn pre-C++23, or a warning in C++23, if requested by
148 -Winvalid-constexpr. Otherwise, we're not in the context where we are
149 checking if a function can be marked 'constexpr', so give a hard error. */
151 ATTRIBUTE_GCC_DIAG(3,4)
153 constexpr_error (location_t location
, bool constexpr_fundef_p
,
154 const char *gmsgid
, ...)
156 diagnostic_info diagnostic
;
158 rich_location
richloc (line_table
, location
);
159 va_start (ap
, gmsgid
);
161 if (!constexpr_fundef_p
)
163 /* Report an error that cannot be suppressed. */
164 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, &richloc
, DK_ERROR
);
165 ret
= diagnostic_report_diagnostic (global_dc
, &diagnostic
);
167 else if (warn_invalid_constexpr
)
169 diagnostic_set_info (&diagnostic
, gmsgid
, &ap
, &richloc
,
170 cxx_dialect
< cxx23
? DK_PEDWARN
: DK_WARNING
);
171 diagnostic
.option_index
= OPT_Winvalid_constexpr
;
172 ret
= diagnostic_report_diagnostic (global_dc
, &diagnostic
);
180 struct constexpr_fundef_hasher
: ggc_ptr_hash
<constexpr_fundef
>
182 static hashval_t
hash (const constexpr_fundef
*);
183 static bool equal (const constexpr_fundef
*, const constexpr_fundef
*);
186 /* This table holds all constexpr function definitions seen in
187 the current translation unit. */
189 static GTY (()) hash_table
<constexpr_fundef_hasher
> *constexpr_fundef_table
;
191 /* Utility function used for managing the constexpr function table.
192 Return true if the entries pointed to by P and Q are for the
193 same constexpr function. */
196 constexpr_fundef_hasher::equal (const constexpr_fundef
*lhs
,
197 const constexpr_fundef
*rhs
)
199 return lhs
->decl
== rhs
->decl
;
202 /* Utility function used for managing the constexpr function table.
203 Return a hash value for the entry pointed to by Q. */
206 constexpr_fundef_hasher::hash (const constexpr_fundef
*fundef
)
208 return DECL_UID (fundef
->decl
);
211 /* Return a previously saved definition of function FUN. */
214 retrieve_constexpr_fundef (tree fun
)
216 if (constexpr_fundef_table
== NULL
)
219 constexpr_fundef fundef
= { fun
, NULL_TREE
, NULL_TREE
, NULL_TREE
};
220 return constexpr_fundef_table
->find (&fundef
);
223 /* Check whether the parameter and return types of FUN are valid for a
224 constexpr function, and complain if COMPLAIN. */
227 is_valid_constexpr_fn (tree fun
, bool complain
)
231 if (DECL_INHERITED_CTOR (fun
)
232 && TREE_CODE (fun
) == TEMPLATE_DECL
)
236 error ("inherited constructor %qD is not %<constexpr%>",
237 DECL_INHERITED_CTOR (fun
));
241 for (tree parm
= FUNCTION_FIRST_USER_PARM (fun
);
242 parm
!= NULL_TREE
; parm
= TREE_CHAIN (parm
))
243 if (!literal_type_p (TREE_TYPE (parm
)))
248 auto_diagnostic_group d
;
249 if (constexpr_error (input_location
, /*constexpr_fundef_p*/true,
250 "invalid type for parameter %d of "
251 "%<constexpr%> function %q+#D",
252 DECL_PARM_INDEX (parm
), fun
))
253 explain_non_literal_class (TREE_TYPE (parm
));
258 if (LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun
)) && cxx_dialect
< cxx17
)
262 inform (DECL_SOURCE_LOCATION (fun
),
263 "lambdas are implicitly %<constexpr%> only in C++17 and later");
265 else if (DECL_DESTRUCTOR_P (fun
) && cxx_dialect
< cxx20
)
269 error_at (DECL_SOURCE_LOCATION (fun
),
270 "%<constexpr%> destructors only available with "
271 "%<-std=c++20%> or %<-std=gnu++20%>");
273 else if (!DECL_CONSTRUCTOR_P (fun
) && !DECL_DESTRUCTOR_P (fun
))
275 tree rettype
= TREE_TYPE (TREE_TYPE (fun
));
276 if (!literal_type_p (rettype
))
281 auto_diagnostic_group d
;
282 if (constexpr_error (input_location
, /*constexpr_fundef_p*/true,
283 "invalid return type %qT of %<constexpr%> "
284 "function %q+D", rettype
, fun
))
285 explain_non_literal_class (rettype
);
289 /* C++14 DR 1684 removed this restriction. */
290 if (cxx_dialect
< cxx14
291 && DECL_IOBJ_MEMBER_FUNCTION_P (fun
)
292 && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun
)))
297 auto_diagnostic_group d
;
298 if (pedwarn (DECL_SOURCE_LOCATION (fun
), OPT_Wpedantic
,
299 "enclosing class of %<constexpr%> non-static"
300 " member function %q+#D is not a literal type",
302 explain_non_literal_class (DECL_CONTEXT (fun
));
306 else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun
)))
310 error ("%q#T has virtual base classes", DECL_CONTEXT (fun
));
316 /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF
317 for a member of an anonymous aggregate, INIT is the initializer for that
318 member, and VEC_OUTER is the vector of constructor elements for the class
319 whose constructor we are processing. Add the initializer to the vector
320 and return true to indicate success. */
323 build_anon_member_initialization (tree member
, tree init
,
324 vec
<constructor_elt
, va_gc
> **vec_outer
)
326 /* MEMBER presents the relevant fields from the inside out, but we need
327 to build up the initializer from the outside in so that we can reuse
328 previously built CONSTRUCTORs if this is, say, the second field in an
329 anonymous struct. So we use a vec as a stack. */
330 auto_vec
<tree
, 2> fields
;
333 fields
.safe_push (TREE_OPERAND (member
, 1));
334 member
= TREE_OPERAND (member
, 0);
336 while (ANON_AGGR_TYPE_P (TREE_TYPE (member
))
337 && TREE_CODE (member
) == COMPONENT_REF
);
339 /* VEC has the constructor elements vector for the context of FIELD.
340 If FIELD is an anonymous aggregate, we will push inside it. */
341 vec
<constructor_elt
, va_gc
> **vec
= vec_outer
;
343 while (field
= fields
.pop(),
344 ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
347 /* If there is already an outer constructor entry for the anonymous
348 aggregate FIELD, use it; otherwise, insert one. */
349 if (vec_safe_is_empty (*vec
)
350 || (*vec
)->last().index
!= field
)
352 ctor
= build_constructor (TREE_TYPE (field
), NULL
);
353 CONSTRUCTOR_APPEND_ELT (*vec
, field
, ctor
);
356 ctor
= (*vec
)->last().value
;
357 vec
= &CONSTRUCTOR_ELTS (ctor
);
360 /* Now we're at the innermost field, the one that isn't an anonymous
361 aggregate. Add its initializer to the CONSTRUCTOR and we're done. */
362 gcc_assert (fields
.is_empty());
363 CONSTRUCTOR_APPEND_ELT (*vec
, field
, init
);
368 /* Subroutine of build_constexpr_constructor_member_initializers.
369 The expression tree T represents a data member initialization
370 in a (constexpr) constructor definition. Build a pairing of
371 the data member with its initializer, and prepend that pair
372 to the existing initialization pair INITS. */
375 build_data_member_initialization (tree t
, vec
<constructor_elt
, va_gc
> **vec
)
378 if (TREE_CODE (t
) == CLEANUP_POINT_EXPR
)
379 t
= TREE_OPERAND (t
, 0);
380 if (TREE_CODE (t
) == EXPR_STMT
)
381 t
= TREE_OPERAND (t
, 0);
382 if (t
== error_mark_node
)
384 if (TREE_CODE (t
) == STATEMENT_LIST
)
386 for (tree stmt
: tsi_range (t
))
387 if (! build_data_member_initialization (stmt
, vec
))
391 if (TREE_CODE (t
) == CLEANUP_STMT
)
393 /* We can't see a CLEANUP_STMT in a constructor for a literal class,
394 but we can in a constexpr constructor for a non-literal class. Just
395 ignore it; either all the initialization will be constant, in which
396 case the cleanup can't run, or it can't be constexpr.
397 Still recurse into CLEANUP_BODY. */
398 return build_data_member_initialization (CLEANUP_BODY (t
), vec
);
400 if (TREE_CODE (t
) == CONVERT_EXPR
)
401 t
= TREE_OPERAND (t
, 0);
402 if (TREE_CODE (t
) == INIT_EXPR
403 /* vptr initialization shows up as a MODIFY_EXPR. In C++14 we only
404 use what this function builds for cx_check_missing_mem_inits, and
405 assignment in the ctor body doesn't count. */
406 || (cxx_dialect
< cxx14
&& TREE_CODE (t
) == MODIFY_EXPR
))
408 member
= TREE_OPERAND (t
, 0);
409 init
= break_out_target_exprs (TREE_OPERAND (t
, 1));
411 else if (TREE_CODE (t
) == CALL_EXPR
)
413 tree fn
= get_callee_fndecl (t
);
414 if (!fn
|| !DECL_CONSTRUCTOR_P (fn
))
415 /* We're only interested in calls to subobject constructors. */
417 member
= CALL_EXPR_ARG (t
, 0);
418 /* We don't use build_cplus_new here because it complains about
419 abstract bases. Leaving the call unwrapped means that it has the
420 wrong type, but cxx_eval_constant_expression doesn't care. */
421 init
= break_out_target_exprs (t
);
423 else if (TREE_CODE (t
) == BIND_EXPR
)
424 return build_data_member_initialization (BIND_EXPR_BODY (t
), vec
);
426 /* Don't add anything else to the CONSTRUCTOR. */
428 if (INDIRECT_REF_P (member
))
429 member
= TREE_OPERAND (member
, 0);
430 if (TREE_CODE (member
) == NOP_EXPR
)
434 if (TREE_CODE (op
) == ADDR_EXPR
)
436 gcc_assert (same_type_ignoring_top_level_qualifiers_p
437 (TREE_TYPE (TREE_TYPE (op
)),
438 TREE_TYPE (TREE_TYPE (member
))));
439 /* Initializing a cv-qualified member; we need to look through
443 else if (op
== current_class_ptr
444 && (same_type_ignoring_top_level_qualifiers_p
445 (TREE_TYPE (TREE_TYPE (member
)),
446 current_class_type
)))
447 /* Delegating constructor. */
451 /* This is an initializer for an empty base; keep it for now so
452 we can check it in cxx_eval_bare_aggregate. */
453 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member
))));
456 if (TREE_CODE (member
) == ADDR_EXPR
)
457 member
= TREE_OPERAND (member
, 0);
458 if (TREE_CODE (member
) == COMPONENT_REF
)
460 tree aggr
= TREE_OPERAND (member
, 0);
461 if (TREE_CODE (aggr
) == VAR_DECL
)
462 /* Initializing a local variable, don't add anything. */
464 if (TREE_CODE (aggr
) != COMPONENT_REF
)
465 /* Normal member initialization. */
466 member
= TREE_OPERAND (member
, 1);
467 else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr
)))
468 /* Initializing a member of an anonymous union. */
469 return build_anon_member_initialization (member
, init
, vec
);
471 /* We're initializing a vtable pointer in a base. Leave it as
472 COMPONENT_REF so we remember the path to get to the vfield. */
473 gcc_assert (TREE_TYPE (member
) == vtbl_ptr_type_node
);
476 /* Value-initialization can produce multiple initializers for the
477 same field; use the last one. */
478 if (!vec_safe_is_empty (*vec
) && (*vec
)->last().index
== member
)
479 (*vec
)->last().value
= init
;
481 CONSTRUCTOR_APPEND_ELT (*vec
, member
, init
);
485 /* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval.
486 In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a
487 BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */
490 check_constexpr_bind_expr_vars (tree t
)
492 gcc_assert (TREE_CODE (t
) == BIND_EXPR
);
494 for (tree var
= BIND_EXPR_VARS (t
); var
; var
= DECL_CHAIN (var
))
495 if (TREE_CODE (var
) == TYPE_DECL
496 && DECL_IMPLICIT_TYPEDEF_P (var
)
497 && !LAMBDA_TYPE_P (TREE_TYPE (var
)))
502 /* Subroutine of check_constexpr_ctor_body. */
505 check_constexpr_ctor_body_1 (tree last
, tree list
)
507 switch (TREE_CODE (list
))
510 if (TREE_CODE (DECL_EXPR_DECL (list
)) == USING_DECL
511 || TREE_CODE (DECL_EXPR_DECL (list
)) == TYPE_DECL
)
515 case CLEANUP_POINT_EXPR
:
516 return check_constexpr_ctor_body (last
, TREE_OPERAND (list
, 0),
520 if (!check_constexpr_bind_expr_vars (list
)
521 || !check_constexpr_ctor_body (last
, BIND_EXPR_BODY (list
),
528 case DEBUG_BEGIN_STMT
:
536 /* Make sure that there are no statements after LAST in the constructor
537 body represented by LIST. */
540 check_constexpr_ctor_body (tree last
, tree list
, bool complain
)
542 /* C++14 doesn't require a constexpr ctor to have an empty body. */
543 if (cxx_dialect
>= cxx14
)
547 if (TREE_CODE (list
) == STATEMENT_LIST
)
549 tree_stmt_iterator i
= tsi_last (list
);
550 for (; !tsi_end_p (i
); tsi_prev (&i
))
552 tree t
= tsi_stmt (i
);
555 if (!check_constexpr_ctor_body_1 (last
, t
))
562 else if (list
!= last
563 && !check_constexpr_ctor_body_1 (last
, list
))
568 error ("%<constexpr%> constructor does not have empty body");
569 DECL_DECLARED_CONSTEXPR_P (current_function_decl
) = false;
574 /* V is a vector of constructor elements built up for the base and member
575 initializers of a constructor for TYPE. They need to be in increasing
576 offset order, which they might not be yet if TYPE has a primary base
577 which is not first in the base-clause or a vptr and at least one base
578 all of which are non-primary. */
580 static vec
<constructor_elt
, va_gc
> *
581 sort_constexpr_mem_initializers (tree type
, vec
<constructor_elt
, va_gc
> *v
)
583 tree pri
= CLASSTYPE_PRIMARY_BINFO (type
);
589 field_type
= BINFO_TYPE (pri
);
590 else if (TYPE_CONTAINS_VPTR_P (type
))
591 field_type
= vtbl_ptr_type_node
;
595 /* Find the element for the primary base or vptr and move it to the
596 beginning of the vec. */
597 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
598 if (TREE_TYPE (ce
->index
) == field_type
)
601 if (i
> 0 && i
< vec_safe_length (v
))
603 vec
<constructor_elt
, va_gc
> &vref
= *v
;
604 constructor_elt elt
= vref
[i
];
613 /* Build compile-time evalable representations of member-initializer list
614 for a constexpr constructor. */
617 build_constexpr_constructor_member_initializers (tree type
, tree body
)
619 vec
<constructor_elt
, va_gc
> *vec
= NULL
;
622 switch (TREE_CODE (body
))
624 case MUST_NOT_THROW_EXPR
:
626 body
= TREE_OPERAND (body
, 0);
630 for (tree stmt
: tsi_range (body
))
633 if (TREE_CODE (body
) == BIND_EXPR
)
639 body
= BIND_EXPR_BODY (body
);
646 if (TREE_CODE (body
) == TRY_BLOCK
)
648 body
= TREE_OPERAND (body
, 0);
649 if (TREE_CODE (body
) == BIND_EXPR
)
650 body
= BIND_EXPR_BODY (body
);
652 if (TREE_CODE (body
) == CLEANUP_POINT_EXPR
)
654 body
= TREE_OPERAND (body
, 0);
655 if (TREE_CODE (body
) == EXPR_STMT
)
656 body
= TREE_OPERAND (body
, 0);
657 if (TREE_CODE (body
) == INIT_EXPR
658 && (same_type_ignoring_top_level_qualifiers_p
659 (TREE_TYPE (TREE_OPERAND (body
, 0)),
660 current_class_type
)))
663 return TREE_OPERAND (body
, 1);
665 ok
= build_data_member_initialization (body
, &vec
);
667 else if (TREE_CODE (body
) == STATEMENT_LIST
)
669 for (tree stmt
: tsi_range (body
))
671 ok
= build_data_member_initialization (stmt
, &vec
);
676 else if (EXPR_P (body
))
677 ok
= build_data_member_initialization (body
, &vec
);
679 gcc_assert (errorcount
> 0);
682 if (vec_safe_length (vec
) > 0)
684 /* In a delegating constructor, return the target. */
685 constructor_elt
*ce
= &(*vec
)[0];
686 if (ce
->index
== current_class_ptr
)
693 vec
= sort_constexpr_mem_initializers (type
, vec
);
694 return build_constructor (type
, vec
);
697 return error_mark_node
;
700 /* We have an expression tree T that represents a call, either CALL_EXPR
701 or AGGR_INIT_EXPR. If the call is lexically to a named function,
702 return the _DECL for that function. */
705 get_function_named_in_call (tree t
)
707 tree callee
= cp_get_callee (t
);
708 tree fun
= cp_get_fndecl_from_callee (callee
, /*fold*/false);
709 return fun
? fun
: callee
;
712 /* Subroutine of check_constexpr_fundef. BODY is the body of a function
713 declared to be constexpr, or a sub-statement thereof. Returns the
714 return value if suitable, error_mark_node for a statement not allowed in
715 a constexpr function, or NULL_TREE if no return value was found. */
718 constexpr_fn_retval (tree body
)
720 switch (TREE_CODE (body
))
724 tree expr
= NULL_TREE
;
725 for (tree stmt
: tsi_range (body
))
727 tree s
= constexpr_fn_retval (stmt
);
728 if (s
== error_mark_node
)
729 return error_mark_node
;
730 else if (s
== NULL_TREE
)
731 /* Keep iterating. */;
733 /* Multiple return statements. */
734 return error_mark_node
;
742 return break_out_target_exprs (TREE_OPERAND (body
, 0));
746 tree decl
= DECL_EXPR_DECL (body
);
747 if (TREE_CODE (decl
) == USING_DECL
748 /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */
749 || DECL_ARTIFICIAL (decl
))
751 return error_mark_node
;
754 case CLEANUP_POINT_EXPR
:
755 return constexpr_fn_retval (TREE_OPERAND (body
, 0));
758 if (!check_constexpr_bind_expr_vars (body
))
759 return error_mark_node
;
760 return constexpr_fn_retval (BIND_EXPR_BODY (body
));
763 case DEBUG_BEGIN_STMT
:
768 tree fun
= get_function_named_in_call (body
);
770 && fndecl_built_in_p (fun
, BUILT_IN_UNREACHABLE
))
776 return error_mark_node
;
780 /* Subroutine of check_constexpr_fundef. BODY is the DECL_SAVED_TREE of
781 FUN; do the necessary transformations to turn it into a single expression
782 that we can store in the hash table. */
785 massage_constexpr_body (tree fun
, tree body
)
787 if (DECL_CONSTRUCTOR_P (fun
))
788 body
= build_constexpr_constructor_member_initializers
789 (DECL_CONTEXT (fun
), body
);
790 else if (cxx_dialect
< cxx14
)
792 if (TREE_CODE (body
) == EH_SPEC_BLOCK
)
793 body
= EH_SPEC_STMTS (body
);
794 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
795 body
= TREE_OPERAND (body
, 0);
796 body
= constexpr_fn_retval (body
);
801 /* CTYPE is a type constructed from BODY. Return true if some
802 bases/fields are uninitialized, and complain if COMPLAIN. */
805 cx_check_missing_mem_inits (tree ctype
, tree body
, bool complain
)
807 /* We allow uninitialized bases/fields in C++20. */
808 if (cxx_dialect
>= cxx20
)
815 if (TREE_CODE (body
) != CONSTRUCTOR
)
817 nelts
= CONSTRUCTOR_NELTS (body
);
819 tree field
= TYPE_FIELDS (ctype
);
821 if (TREE_CODE (ctype
) == UNION_TYPE
)
823 if (nelts
== 0 && next_aggregate_field (field
))
826 error ("%<constexpr%> constructor for union %qT must "
827 "initialize exactly one non-static data member", ctype
);
833 /* Iterate over the CONSTRUCTOR, checking any missing fields don't
834 need an explicit initialization. */
836 for (unsigned i
= 0; i
<= nelts
; ++i
)
838 tree index
= NULL_TREE
;
841 index
= CONSTRUCTOR_ELT (body
, i
)->index
;
842 /* Skip base and vtable inits. */
843 if (TREE_CODE (index
) != FIELD_DECL
844 || DECL_ARTIFICIAL (index
))
848 for (; field
!= index
; field
= DECL_CHAIN (field
))
851 if (TREE_CODE (field
) != FIELD_DECL
)
853 if (DECL_UNNAMED_BIT_FIELD (field
))
855 if (DECL_ARTIFICIAL (field
))
857 if (ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
859 /* Recurse to check the anonymous aggregate member. */
860 bad
|= cx_check_missing_mem_inits
861 (TREE_TYPE (field
), NULL_TREE
, complain
);
862 if (bad
&& !complain
)
866 ftype
= TREE_TYPE (field
);
867 if (!ftype
|| !TYPE_P (ftype
) || !COMPLETE_TYPE_P (ftype
))
868 /* A flexible array can't be intialized here, so don't complain
871 if (is_empty_field (field
))
872 /* An empty field doesn't need an initializer. */
874 ftype
= strip_array_types (ftype
);
875 if (type_has_constexpr_default_constructor (ftype
))
877 /* It's OK to skip a member with a trivial constexpr ctor.
878 A constexpr ctor that isn't trivial should have been
880 gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype
)
886 auto_diagnostic_group d
;
887 error ("member %qD must be initialized by mem-initializer "
888 "in %<constexpr%> constructor", field
);
889 inform (DECL_SOURCE_LOCATION (field
), "declared here");
892 if (field
== NULL_TREE
)
895 if (ANON_AGGR_TYPE_P (TREE_TYPE (index
)))
897 /* Check the anonymous aggregate initializer is valid. */
898 bad
|= cx_check_missing_mem_inits
899 (TREE_TYPE (index
), CONSTRUCTOR_ELT (body
, i
)->value
, complain
);
900 if (bad
&& !complain
)
903 field
= DECL_CHAIN (field
);
909 /* We are processing the definition of the constexpr function FUN.
910 Check that its body fulfills the apropriate requirements and
911 enter it in the constexpr function definition table. */
914 maybe_save_constexpr_fundef (tree fun
)
916 if (processing_template_decl
917 || cp_function_chain
->invalid_constexpr
918 || (DECL_CLONED_FUNCTION_P (fun
) && !DECL_DELETING_DESTRUCTOR_P (fun
)))
921 /* With -fimplicit-constexpr, try to make inlines constexpr. We'll
922 actually set DECL_DECLARED_CONSTEXPR_P below if the checks pass. */
923 bool implicit
= false;
924 if (flag_implicit_constexpr
)
926 if (DECL_DELETING_DESTRUCTOR_P (fun
)
927 && decl_implicit_constexpr_p (DECL_CLONED_FUNCTION (fun
)))
928 /* Don't inherit implicit constexpr from the non-deleting
930 DECL_DECLARED_CONSTEXPR_P (fun
) = false;
932 if (!DECL_DECLARED_CONSTEXPR_P (fun
)
933 && DECL_DECLARED_INLINE_P (fun
)
934 && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fun
)))
938 if (!DECL_DECLARED_CONSTEXPR_P (fun
) && !implicit
)
941 bool complain
= !DECL_GENERATED_P (fun
) && !implicit
;
943 if (!is_valid_constexpr_fn (fun
, complain
))
946 tree massaged
= massage_constexpr_body (fun
, DECL_SAVED_TREE (fun
));
947 if (massaged
== NULL_TREE
|| massaged
== error_mark_node
)
949 if (!DECL_CONSTRUCTOR_P (fun
) && complain
)
950 error ("body of %<constexpr%> function %qD not a return-statement",
955 bool potential
= potential_rvalue_constant_expression (massaged
);
956 if (!potential
&& complain
)
957 require_potential_rvalue_constant_expression_fncheck (massaged
);
959 if (DECL_CONSTRUCTOR_P (fun
) && potential
960 && !DECL_DEFAULTED_FN (fun
))
962 if (cx_check_missing_mem_inits (DECL_CONTEXT (fun
),
965 else if (cxx_dialect
> cxx11
)
967 /* What we got from massage_constexpr_body is pretty much just the
968 ctor-initializer, also check the body. */
969 massaged
= DECL_SAVED_TREE (fun
);
970 potential
= potential_rvalue_constant_expression (massaged
);
971 if (!potential
&& complain
)
972 require_potential_rvalue_constant_expression_fncheck (massaged
);
976 if (!potential
&& complain
977 /* If -Wno-invalid-constexpr was specified, we haven't complained
978 about non-constant expressions yet. Register the function and
979 complain in explain_invalid_constexpr_fn if the function is
981 && warn_invalid_constexpr
!= 0)
988 DECL_DECLARED_CONSTEXPR_P (fun
) = true;
989 DECL_LANG_SPECIFIC (fun
)->u
.fn
.implicit_constexpr
= true;
990 if (DECL_CONSTRUCTOR_P (fun
))
991 TYPE_HAS_CONSTEXPR_CTOR (DECL_CONTEXT (fun
)) = true;
994 /* Don't bother keeping the pre-generic body of unsuitable functions
995 not explicitly declared constexpr. */
999 constexpr_fundef entry
= {fun
, NULL_TREE
, NULL_TREE
, NULL_TREE
};
1000 bool clear_ctx
= false;
1001 if (DECL_RESULT (fun
) && DECL_CONTEXT (DECL_RESULT (fun
)) == NULL_TREE
)
1004 DECL_CONTEXT (DECL_RESULT (fun
)) = fun
;
1006 tree saved_fn
= current_function_decl
;
1007 current_function_decl
= fun
;
1008 entry
.body
= copy_fn (entry
.decl
, entry
.parms
, entry
.result
);
1009 current_function_decl
= saved_fn
;
1011 DECL_CONTEXT (DECL_RESULT (entry
.decl
)) = NULL_TREE
;
1013 /* For a template instantiation, we want to remember the pre-generic body
1014 for explain_invalid_constexpr_fn, but do tell cxx_eval_call_expression
1015 that it doesn't need to bother trying to expand the function. */
1016 entry
.result
= error_mark_node
;
1018 register_constexpr_fundef (entry
);
1021 /* BODY is a validated and massaged definition of a constexpr
1022 function. Register it in the hash table. */
1025 register_constexpr_fundef (const constexpr_fundef
&value
)
1027 /* Create the constexpr function table if necessary. */
1028 if (constexpr_fundef_table
== NULL
)
1029 constexpr_fundef_table
1030 = hash_table
<constexpr_fundef_hasher
>::create_ggc (101);
1032 constexpr_fundef
**slot
= constexpr_fundef_table
->find_slot
1033 (const_cast<constexpr_fundef
*> (&value
), INSERT
);
1035 gcc_assert (*slot
== NULL
);
1036 *slot
= ggc_alloc
<constexpr_fundef
> ();
1040 /* FUN is a non-constexpr (or, with -Wno-invalid-constexpr, a constexpr
1041 function called in a context that requires a constant expression).
1042 If it comes from a constexpr template, explain why the instantiation
1043 isn't constexpr. Otherwise, explain why the function cannot be used
1044 in a constexpr context. */
1047 explain_invalid_constexpr_fn (tree fun
)
1049 static hash_set
<tree
> *diagnosed
;
1051 /* In C++23, a function marked 'constexpr' may not actually be a constant
1052 expression. We haven't diagnosed the problem yet: -Winvalid-constexpr
1053 wasn't enabled. The function was called, so diagnose why it cannot be
1054 used in a constant expression. */
1055 if (warn_invalid_constexpr
== 0 && DECL_DECLARED_CONSTEXPR_P (fun
))
1057 /* Only diagnose defaulted functions, lambdas, or instantiations. */
1058 else if (!DECL_DEFAULTED_FN (fun
)
1059 && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun
))
1060 && !is_instantiation_of_constexpr (fun
))
1062 inform (DECL_SOURCE_LOCATION (fun
), "%qD declared here", fun
);
1065 if (diagnosed
== NULL
)
1066 diagnosed
= new hash_set
<tree
>;
1067 if (diagnosed
->add (fun
))
1068 /* Already explained. */
1071 iloc_sentinel ils
= input_location
;
1072 if (!lambda_static_thunk_p (fun
))
1074 /* Diagnostics should completely ignore the static thunk, so leave
1075 input_location set to our caller's location. */
1076 input_location
= DECL_SOURCE_LOCATION (fun
);
1077 inform (input_location
,
1078 "%qD is not usable as a %<constexpr%> function because:", fun
);
1080 /* First check the declaration. */
1081 if (is_valid_constexpr_fn (fun
, true))
1083 /* Then if it's OK, the body. */
1084 if (!DECL_DECLARED_CONSTEXPR_P (fun
)
1085 && DECL_DEFAULTED_FN (fun
))
1086 explain_implicit_non_constexpr (fun
);
1089 if (constexpr_fundef
*fd
= retrieve_constexpr_fundef (fun
))
1092 body
= DECL_SAVED_TREE (fun
);
1093 body
= massage_constexpr_body (fun
, body
);
1094 require_potential_rvalue_constant_expression (body
);
1095 if (DECL_CONSTRUCTOR_P (fun
))
1097 cx_check_missing_mem_inits (DECL_CONTEXT (fun
), body
, true);
1098 if (cxx_dialect
> cxx11
)
1100 /* Also check the body, not just the ctor-initializer. */
1101 body
= DECL_SAVED_TREE (fun
);
1102 require_potential_rvalue_constant_expression (body
);
1109 /* Objects of this type represent calls to constexpr functions
1110 along with the bindings of parameters to their arguments, for
1111 the purpose of compile time evaluation. */
1113 struct GTY((for_user
)) constexpr_call
{
1114 /* Description of the constexpr function definition. */
1115 constexpr_fundef
*fundef
;
1116 /* Parameter bindings environment. A TREE_VEC of arguments. */
1118 /* Result of the call.
1119 NULL means the call is being evaluated.
1120 error_mark_node means that the evaluation was erroneous;
1121 otherwise, the actuall value of the call. */
1123 /* The hash of this call; we remember it here to avoid having to
1124 recalculate it when expanding the hash table. */
1126 /* The value of constexpr_ctx::manifestly_const_eval. */
1127 enum mce_value manifestly_const_eval
;
1130 struct constexpr_call_hasher
: ggc_ptr_hash
<constexpr_call
>
1132 static hashval_t
hash (constexpr_call
*);
1133 static bool equal (constexpr_call
*, constexpr_call
*);
1136 enum constexpr_switch_state
{
1137 /* Used when processing a switch for the first time by cxx_eval_switch_expr
1138 and default: label for that switch has not been seen yet. */
1139 css_default_not_seen
,
1140 /* Used when processing a switch for the first time by cxx_eval_switch_expr
1141 and default: label for that switch has been seen already. */
1143 /* Used when processing a switch for the second time by
1144 cxx_eval_switch_expr, where default: label should match. */
1145 css_default_processing
1148 /* The constexpr expansion context part which needs one instance per
1149 cxx_eval_outermost_constant_expr invocation. VALUES is a map of values of
1150 variables initialized within the expression. */
1152 class constexpr_global_ctx
{
1153 /* Values for any temporaries or local variables within the
1154 constant-expression. Objects outside their lifetime have
1155 value 'void_node'. */
1156 hash_map
<tree
,tree
> values
;
1158 /* Number of cxx_eval_constant_expression calls (except skipped ones,
1159 on simple constants or location wrappers) encountered during current
1160 cxx_eval_outermost_constant_expr call. */
1161 HOST_WIDE_INT constexpr_ops_count
;
1162 /* Heap VAR_DECLs created during the evaluation of the outermost constant
1164 auto_vec
<tree
, 16> heap_vars
;
1165 /* Cleanups that need to be evaluated at the end of CLEANUP_POINT_EXPR. */
1166 vec
<tree
> *cleanups
;
1167 /* If non-null, only allow modification of existing values of the variables
1168 in this set. Set by modifiable_tracker, below. */
1169 hash_set
<tree
> *modifiable
;
1170 /* Number of heap VAR_DECL deallocations. */
1171 unsigned heap_dealloc_count
;
1173 constexpr_global_ctx ()
1174 : constexpr_ops_count (0), cleanups (NULL
), modifiable (nullptr),
1175 heap_dealloc_count (0) {}
1177 bool is_outside_lifetime (tree t
)
1179 if (tree
*p
= values
.get (t
))
1180 if (*p
== void_node
)
1184 tree
get_value (tree t
)
1186 if (tree
*p
= values
.get (t
))
1187 if (*p
!= void_node
)
1191 tree
*get_value_ptr (tree t
, bool initializing
)
1193 if (modifiable
&& !modifiable
->contains (t
))
1195 if (tree
*p
= values
.get (t
))
1197 if (*p
!= void_node
)
1199 else if (initializing
)
1207 void put_value (tree t
, tree v
)
1209 bool already_in_map
= values
.put (t
, v
);
1210 if (!already_in_map
&& modifiable
)
1211 modifiable
->add (t
);
1213 void destroy_value (tree t
)
1215 if (TREE_CODE (t
) == VAR_DECL
1216 || TREE_CODE (t
) == PARM_DECL
1217 || TREE_CODE (t
) == RESULT_DECL
)
1218 values
.put (t
, void_node
);
1222 void clear_value (tree t
)
1228 /* Helper class for constexpr_global_ctx. In some cases we want to avoid
1229 side-effects from evaluation of a particular subexpression of a
1230 constant-expression. In such cases we use modifiable_tracker to prevent
1231 modification of variables created outside of that subexpression.
1233 ??? We could change the hash_set to a hash_map, allow and track external
1234 modifications, and roll them back in the destructor. It's not clear to me
1235 that this would be worthwhile. */
1237 class modifiable_tracker
1240 constexpr_global_ctx
*global
;
1242 modifiable_tracker (constexpr_global_ctx
*g
): global(g
)
1244 global
->modifiable
= &set
;
1246 ~modifiable_tracker ()
1249 global
->clear_value (t
);
1250 global
->modifiable
= nullptr;
1254 /* The constexpr expansion context. CALL is the current function
1255 expansion, CTOR is the current aggregate initializer, OBJECT is the
1256 object being initialized by CTOR, either a VAR_DECL or a _REF. */
1258 struct constexpr_ctx
{
1259 /* The part of the context that needs to be unique to the whole
1260 cxx_eval_outermost_constant_expr invocation. */
1261 constexpr_global_ctx
*global
;
1262 /* The innermost call we're evaluating. */
1263 constexpr_call
*call
;
1264 /* SAVE_EXPRs and TARGET_EXPR_SLOT vars of TARGET_EXPRs that we've seen
1265 within the current LOOP_EXPR. NULL if we aren't inside a loop. */
1266 vec
<tree
> *save_exprs
;
1267 /* The CONSTRUCTOR we're currently building up for an aggregate
1270 /* The object we're building the CONSTRUCTOR for. */
1272 /* If inside SWITCH_EXPR. */
1273 constexpr_switch_state
*css_state
;
1274 /* The aggregate initialization context inside which this one is nested. This
1275 is used by lookup_placeholder to resolve PLACEHOLDER_EXPRs. */
1276 const constexpr_ctx
*parent
;
1278 /* Whether we should error on a non-constant expression or fail quietly.
1279 This flag needs to be here, but some of the others could move to global
1280 if they get larger than a word. */
1282 /* Whether we are strictly conforming to constant expression rules or
1283 trying harder to get a constant value. */
1285 /* Whether __builtin_is_constant_evaluated () should be true. */
1286 mce_value manifestly_const_eval
;
1289 /* Remove T from the global values map, checking for attempts to destroy
1290 a value that has already finished its lifetime. */
1293 destroy_value_checked (const constexpr_ctx
* ctx
, tree t
, bool *non_constant_p
)
1295 if (t
== error_mark_node
|| TREE_TYPE (t
) == error_mark_node
)
1298 /* Don't error again here if we've already reported a problem. */
1299 if (!*non_constant_p
1301 /* Non-trivial destructors have their lifetimes ended explicitly
1302 with a clobber, so don't worry about it here. */
1303 && (!TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (t
))
1304 /* ...except parameters are remapped in cxx_eval_call_expression,
1305 and the destructor call during cleanup won't be able to tell that
1306 this value has already been destroyed, so complain now. This is
1307 not quite unobservable, but is extremely unlikely to crop up in
1308 practice; see g++.dg/cpp2a/constexpr-lifetime2.C. */
1309 || TREE_CODE (t
) == PARM_DECL
)
1310 && ctx
->global
->is_outside_lifetime (t
))
1314 auto_diagnostic_group d
;
1315 error ("destroying %qE outside its lifetime", t
);
1316 inform (DECL_SOURCE_LOCATION (t
), "declared here");
1318 *non_constant_p
= true;
1320 ctx
->global
->destroy_value (t
);
1323 /* This internal flag controls whether we should avoid doing anything during
1324 constexpr evaluation that would cause extra DECL_UID generation, such as
1325 template instantiation and function body copying. */
1327 static bool uid_sensitive_constexpr_evaluation_value
;
1329 /* An internal counter that keeps track of the number of times
1330 uid_sensitive_constexpr_evaluation_p returned true. */
1332 static unsigned uid_sensitive_constexpr_evaluation_true_counter
;
1334 /* The accessor for uid_sensitive_constexpr_evaluation_value which also
1335 increments the corresponding counter. */
1338 uid_sensitive_constexpr_evaluation_p ()
1340 if (uid_sensitive_constexpr_evaluation_value
)
1342 ++uid_sensitive_constexpr_evaluation_true_counter
;
1349 /* The default constructor for uid_sensitive_constexpr_evaluation_sentinel
1350 enables the internal flag for uid_sensitive_constexpr_evaluation_p
1351 during the lifetime of the sentinel object. Upon its destruction, the
1352 previous value of uid_sensitive_constexpr_evaluation_p is restored. */
1354 uid_sensitive_constexpr_evaluation_sentinel
1355 ::uid_sensitive_constexpr_evaluation_sentinel ()
1356 : ovr (uid_sensitive_constexpr_evaluation_value
, true)
1360 /* The default constructor for uid_sensitive_constexpr_evaluation_checker
1361 records the current number of times that uid_sensitive_constexpr_evaluation_p
1362 has been called and returned true. */
1364 uid_sensitive_constexpr_evaluation_checker
1365 ::uid_sensitive_constexpr_evaluation_checker ()
1366 : saved_counter (uid_sensitive_constexpr_evaluation_true_counter
)
1370 /* Returns true iff uid_sensitive_constexpr_evaluation_p is true, and
1371 some constexpr evaluation was restricted due to u_s_c_e_p being called
1372 and returning true during the lifetime of this checker object. */
1375 uid_sensitive_constexpr_evaluation_checker::evaluation_restricted_p () const
1377 return (uid_sensitive_constexpr_evaluation_value
1378 && saved_counter
!= uid_sensitive_constexpr_evaluation_true_counter
);
1382 /* A table of all constexpr calls that have been evaluated by the
1383 compiler in this translation unit. */
1385 static GTY (()) hash_table
<constexpr_call_hasher
> *constexpr_call_table
;
1387 /* Compute a hash value for a constexpr call representation. */
1390 constexpr_call_hasher::hash (constexpr_call
*info
)
1395 /* Return true if the objects pointed to by P and Q represent calls
1396 to the same constexpr function with the same arguments.
1397 Otherwise, return false. */
1400 constexpr_call_hasher::equal (constexpr_call
*lhs
, constexpr_call
*rhs
)
1404 if (lhs
->hash
!= rhs
->hash
)
1406 if (lhs
->manifestly_const_eval
!= rhs
->manifestly_const_eval
)
1408 if (!constexpr_fundef_hasher::equal (lhs
->fundef
, rhs
->fundef
))
1410 return cp_tree_equal (lhs
->bindings
, rhs
->bindings
);
1413 /* Initialize the constexpr call table, if needed. */
1416 maybe_initialize_constexpr_call_table (void)
1418 if (constexpr_call_table
== NULL
)
1419 constexpr_call_table
= hash_table
<constexpr_call_hasher
>::create_ggc (101);
1422 /* During constexpr CALL_EXPR evaluation, to avoid issues with sharing when
1423 a function happens to get called recursively, we unshare the callee
1424 function's body and evaluate this unshared copy instead of evaluating the
1427 FUNDEF_COPIES_TABLE is a per-function freelist of these unshared function
1428 copies. The underlying data structure of FUNDEF_COPIES_TABLE is a hash_map
1429 that's keyed off of the original FUNCTION_DECL and whose value is a
1430 TREE_LIST of this function's unused copies awaiting reuse.
1432 This is not GC-deletable to avoid GC affecting UID generation. */
1434 static GTY(()) decl_tree_map
*fundef_copies_table
;
1436 /* Reuse a copy or create a new unshared copy of the function FUN.
1437 Return this copy. We use a TREE_LIST whose PURPOSE is body, VALUE
1438 is parms, TYPE is result. */
1441 get_fundef_copy (constexpr_fundef
*fundef
)
1445 tree
*slot
= &(hash_map_safe_get_or_insert
<hm_ggc
>
1446 (fundef_copies_table
, fundef
->decl
, &existed
, 127));
1450 /* There is no cached function available, or in use. We can use
1451 the function directly. That the slot is now created records
1452 that this function is now in use. */
1453 copy
= build_tree_list (fundef
->body
, fundef
->parms
);
1454 TREE_TYPE (copy
) = fundef
->result
;
1456 else if (*slot
== NULL_TREE
)
1458 if (uid_sensitive_constexpr_evaluation_p ())
1461 /* We've already used the function itself, so make a copy. */
1462 copy
= build_tree_list (NULL
, NULL
);
1463 tree saved_body
= DECL_SAVED_TREE (fundef
->decl
);
1464 tree saved_parms
= DECL_ARGUMENTS (fundef
->decl
);
1465 tree saved_result
= DECL_RESULT (fundef
->decl
);
1466 tree saved_fn
= current_function_decl
;
1467 DECL_SAVED_TREE (fundef
->decl
) = fundef
->body
;
1468 DECL_ARGUMENTS (fundef
->decl
) = fundef
->parms
;
1469 DECL_RESULT (fundef
->decl
) = fundef
->result
;
1470 current_function_decl
= fundef
->decl
;
1471 TREE_PURPOSE (copy
) = copy_fn (fundef
->decl
, TREE_VALUE (copy
),
1473 current_function_decl
= saved_fn
;
1474 DECL_RESULT (fundef
->decl
) = saved_result
;
1475 DECL_ARGUMENTS (fundef
->decl
) = saved_parms
;
1476 DECL_SAVED_TREE (fundef
->decl
) = saved_body
;
1480 /* We have a cached function available. */
1482 *slot
= TREE_CHAIN (copy
);
1488 /* Save the copy COPY of function FUN for later reuse by
1489 get_fundef_copy(). By construction, there will always be an entry
1493 save_fundef_copy (tree fun
, tree copy
)
1495 tree
*slot
= fundef_copies_table
->get (fun
);
1496 TREE_CHAIN (copy
) = *slot
;
1500 /* Whether our evaluation wants a prvalue (e.g. CONSTRUCTOR or _CST),
1501 a glvalue (e.g. VAR_DECL or _REF), or nothing. */
1509 static tree
cxx_eval_constant_expression (const constexpr_ctx
*, tree
,
1510 value_cat
, bool *, bool *, tree
* = NULL
);
1511 static tree
cxx_eval_bare_aggregate (const constexpr_ctx
*, tree
,
1512 value_cat
, bool *, bool *);
1513 static tree
cxx_fold_indirect_ref (const constexpr_ctx
*, location_t
, tree
, tree
,
1515 static tree
find_heap_var_refs (tree
*, int *, void *);
1517 /* Attempt to evaluate T which represents a call to a builtin function.
1518 We assume here that all builtin functions evaluate to scalar types
1519 represented by _CST nodes. */
1522 cxx_eval_builtin_function_call (const constexpr_ctx
*ctx
, tree t
, tree fun
,
1524 bool *non_constant_p
, bool *overflow_p
)
1526 const int nargs
= call_expr_nargs (t
);
1527 tree
*args
= (tree
*) alloca (nargs
* sizeof (tree
));
1531 /* Don't fold __builtin_constant_p within a constexpr function. */
1532 bool bi_const_p
= DECL_IS_BUILTIN_CONSTANT_P (fun
);
1534 /* If we aren't requiring a constant expression, defer __builtin_constant_p
1535 in a constexpr function until we have values for the parameters. */
1537 && ctx
->manifestly_const_eval
!= mce_true
1538 && current_function_decl
1539 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
1541 *non_constant_p
= true;
1545 /* For __builtin_is_constant_evaluated, defer it if not
1546 ctx->manifestly_const_eval (as sometimes we try to constant evaluate
1547 without manifestly_const_eval even expressions or parts thereof which
1548 will later be manifestly const_eval evaluated), otherwise fold it to
1550 if (fndecl_built_in_p (fun
, CP_BUILT_IN_IS_CONSTANT_EVALUATED
,
1553 if (ctx
->manifestly_const_eval
== mce_unknown
)
1555 *non_constant_p
= true;
1558 return constant_boolean_node (ctx
->manifestly_const_eval
== mce_true
,
1562 if (fndecl_built_in_p (fun
, CP_BUILT_IN_SOURCE_LOCATION
, BUILT_IN_FRONTEND
))
1564 temp_override
<tree
> ovr (current_function_decl
);
1565 if (ctx
->call
&& ctx
->call
->fundef
)
1566 current_function_decl
= ctx
->call
->fundef
->decl
;
1567 return fold_builtin_source_location (t
);
1572 if (fndecl_built_in_p (fun
, BUILT_IN_NORMAL
))
1573 switch (DECL_FUNCTION_CODE (fun
))
1575 case BUILT_IN_STRLEN
:
1576 case BUILT_IN_STRNLEN
:
1579 case BUILT_IN_MEMCHR
:
1580 case BUILT_IN_STRCHR
:
1581 case BUILT_IN_STRRCHR
:
1585 case BUILT_IN_MEMCMP
:
1586 case BUILT_IN_STRCMP
:
1589 case BUILT_IN_STRSTR
:
1593 case BUILT_IN_ASAN_POINTER_COMPARE
:
1594 case BUILT_IN_ASAN_POINTER_SUBTRACT
:
1595 /* These builtins shall be ignored during constant expression
1598 case BUILT_IN_UNREACHABLE
:
1600 if (!*non_constant_p
&& !ctx
->quiet
)
1602 /* Do not allow__builtin_unreachable in constexpr function.
1603 The __builtin_unreachable call with BUILTINS_LOCATION
1604 comes from cp_maybe_instrument_return. */
1605 if (EXPR_LOCATION (t
) == BUILTINS_LOCATION
)
1606 error ("%<constexpr%> call flows off the end of the function");
1608 error ("%q+E is not a constant expression", t
);
1610 *non_constant_p
= true;
1616 /* Be permissive for arguments to built-ins; __builtin_constant_p should
1617 return constant false for a non-constant argument. */
1618 constexpr_ctx new_ctx
= *ctx
;
1619 new_ctx
.quiet
= true;
1620 for (i
= 0; i
< nargs
; ++i
)
1622 tree arg
= CALL_EXPR_ARG (t
, i
);
1625 /* To handle string built-ins we need to pass ADDR_EXPR<STRING_CST> since
1626 expand_builtin doesn't know how to look in the values table. */
1627 bool strop
= i
< strops
;
1631 if (TREE_CODE (arg
) == ADDR_EXPR
)
1632 arg
= TREE_OPERAND (arg
, 0);
1637 /* If builtin_valid_in_constant_expr_p is true,
1638 potential_constant_expression_1 has not recursed into the arguments
1639 of the builtin, verify it here. */
1640 if (!builtin_valid_in_constant_expr_p (fun
)
1641 || potential_constant_expression (arg
))
1643 bool dummy1
= false, dummy2
= false;
1644 arg
= cxx_eval_constant_expression (&new_ctx
, arg
, vc_prvalue
,
1649 /* For __builtin_constant_p, fold all expressions with constant values
1650 even if they aren't C++ constant-expressions. */
1651 arg
= cp_fold_rvalue (arg
);
1654 if (TREE_CODE (arg
) == CONSTRUCTOR
)
1655 arg
= braced_lists_to_strings (TREE_TYPE (arg
), arg
);
1656 if (TREE_CODE (arg
) == STRING_CST
)
1657 arg
= build_address (arg
);
1665 bool save_ffbcp
= force_folding_builtin_constant_p
;
1666 force_folding_builtin_constant_p
|= ctx
->manifestly_const_eval
== mce_true
;
1667 tree save_cur_fn
= current_function_decl
;
1668 /* Return name of ctx->call->fundef->decl for __builtin_FUNCTION (). */
1669 if (fndecl_built_in_p (fun
, BUILT_IN_FUNCTION
)
1671 && ctx
->call
->fundef
)
1672 current_function_decl
= ctx
->call
->fundef
->decl
;
1673 if (fndecl_built_in_p (fun
,
1674 CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS
,
1677 location_t loc
= EXPR_LOCATION (t
);
1679 VERIFY_CONSTANT (args
[0]);
1681 = fold_builtin_is_pointer_inverconvertible_with_class (loc
, nargs
,
1684 else if (fndecl_built_in_p (fun
,
1685 CP_BUILT_IN_IS_CORRESPONDING_MEMBER
,
1688 location_t loc
= EXPR_LOCATION (t
);
1691 VERIFY_CONSTANT (args
[0]);
1692 VERIFY_CONSTANT (args
[1]);
1694 new_call
= fold_builtin_is_corresponding_member (loc
, nargs
, args
);
1697 new_call
= fold_builtin_call_array (EXPR_LOCATION (t
), TREE_TYPE (t
),
1698 CALL_EXPR_FN (t
), nargs
, args
);
1699 current_function_decl
= save_cur_fn
;
1700 force_folding_builtin_constant_p
= save_ffbcp
;
1701 if (new_call
== NULL
)
1703 if (!*non_constant_p
&& !ctx
->quiet
)
1705 new_call
= build_call_array_loc (EXPR_LOCATION (t
), TREE_TYPE (t
),
1706 CALL_EXPR_FN (t
), nargs
, args
);
1707 error ("%q+E is not a constant expression", new_call
);
1709 *non_constant_p
= true;
1713 if (!potential_constant_expression (new_call
))
1715 if (!*non_constant_p
&& !ctx
->quiet
)
1716 error ("%q+E is not a constant expression", new_call
);
1717 *non_constant_p
= true;
1723 /* memchr returns a pointer into the first argument, but we replaced the
1724 argument above with a STRING_CST; put it back it now. */
1725 tree op
= CALL_EXPR_ARG (t
, strret
-1);
1726 STRIP_NOPS (new_call
);
1727 if (TREE_CODE (new_call
) == POINTER_PLUS_EXPR
)
1728 TREE_OPERAND (new_call
, 0) = op
;
1729 else if (TREE_CODE (new_call
) == ADDR_EXPR
)
1733 return cxx_eval_constant_expression (&new_ctx
, new_call
, lval
,
1734 non_constant_p
, overflow_p
);
1737 /* TEMP is the constant value of a temporary object of type TYPE. Adjust
1738 the type of the value to match. */
1741 adjust_temp_type (tree type
, tree temp
)
1743 if (same_type_p (TREE_TYPE (temp
), type
))
1745 /* Avoid wrapping an aggregate value in a NOP_EXPR. */
1746 if (TREE_CODE (temp
) == CONSTRUCTOR
)
1748 /* build_constructor wouldn't retain various CONSTRUCTOR flags. */
1749 tree t
= copy_node (temp
);
1750 TREE_TYPE (t
) = type
;
1753 if (TREE_CODE (temp
) == EMPTY_CLASS_EXPR
)
1754 return build0 (EMPTY_CLASS_EXPR
, type
);
1755 gcc_assert (scalarish_type_p (type
));
1756 /* Now we know we're dealing with a scalar, and a prvalue of non-class
1757 type is cv-unqualified. */
1758 return cp_fold_convert (cv_unqualified (type
), temp
);
1761 /* If T is a CONSTRUCTOR, return an unshared copy of T and any
1762 sub-CONSTRUCTORs. Otherwise return T.
1764 We use this whenever we initialize an object as a whole, whether it's a
1765 parameter, a local variable, or a subobject, so that subsequent
1766 modifications don't affect other places where it was used. */
1769 unshare_constructor (tree t MEM_STAT_DECL
)
1771 if (!t
|| TREE_CODE (t
) != CONSTRUCTOR
)
1773 auto_vec
<tree
*, 4> ptrs
;
1774 ptrs
.safe_push (&t
);
1775 while (!ptrs
.is_empty ())
1777 tree
*p
= ptrs
.pop ();
1778 tree n
= copy_node (*p PASS_MEM_STAT
);
1779 CONSTRUCTOR_ELTS (n
) = vec_safe_copy (CONSTRUCTOR_ELTS (*p
) PASS_MEM_STAT
);
1781 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (n
);
1782 constructor_elt
*ce
;
1783 for (HOST_WIDE_INT i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
1784 if (ce
->value
&& TREE_CODE (ce
->value
) == CONSTRUCTOR
)
1785 ptrs
.safe_push (&ce
->value
);
1790 /* If T is a CONSTRUCTOR, ggc_free T and any sub-CONSTRUCTORs. */
1793 free_constructor (tree t
)
1795 if (!t
|| TREE_CODE (t
) != CONSTRUCTOR
)
1797 releasing_vec ctors
;
1798 vec_safe_push (ctors
, t
);
1799 while (!ctors
->is_empty ())
1801 tree c
= ctors
->pop ();
1802 if (vec
<constructor_elt
, va_gc
> *elts
= CONSTRUCTOR_ELTS (c
))
1804 constructor_elt
*ce
;
1805 for (HOST_WIDE_INT i
= 0; vec_safe_iterate (elts
, i
, &ce
); ++i
)
1806 if (ce
->value
&& TREE_CODE (ce
->value
) == CONSTRUCTOR
)
1807 vec_safe_push (ctors
, ce
->value
);
1814 /* Helper function of cxx_bind_parameters_in_call. Return non-NULL
1815 if *TP is address of a static variable (or part of it) currently being
1816 constructed or of a heap artificial variable. */
1819 addr_of_non_const_var (tree
*tp
, int *walk_subtrees
, void *data
)
1821 if (TREE_CODE (*tp
) == ADDR_EXPR
)
1822 if (tree var
= get_base_address (TREE_OPERAND (*tp
, 0)))
1823 if (VAR_P (var
) && TREE_STATIC (var
))
1825 if (DECL_NAME (var
) == heap_uninit_identifier
1826 || DECL_NAME (var
) == heap_identifier
1827 || DECL_NAME (var
) == heap_vec_uninit_identifier
1828 || DECL_NAME (var
) == heap_vec_identifier
)
1831 constexpr_global_ctx
*global
= (constexpr_global_ctx
*) data
;
1832 if (global
->get_value (var
))
1836 *walk_subtrees
= false;
1840 /* Subroutine of cxx_eval_call_expression.
1841 We are processing a call expression (either CALL_EXPR or
1842 AGGR_INIT_EXPR) in the context of CTX. Evaluate
1843 all arguments and bind their values to correspondings
1844 parameters, making up the NEW_CALL context. */
1847 cxx_bind_parameters_in_call (const constexpr_ctx
*ctx
, tree t
, tree fun
,
1848 bool *non_constant_p
, bool *overflow_p
,
1849 bool *non_constant_args
)
1851 const int nargs
= call_expr_nargs (t
);
1852 tree parms
= DECL_ARGUMENTS (fun
);
1854 /* We don't record ellipsis args below. */
1855 int nparms
= list_length (parms
);
1856 int nbinds
= nargs
< nparms
? nargs
: nparms
;
1857 tree binds
= make_tree_vec (nbinds
);
1858 for (i
= 0; i
< nargs
; ++i
)
1861 tree type
= parms
? TREE_TYPE (parms
) : void_type_node
;
1862 if (parms
&& DECL_BY_REFERENCE (parms
))
1863 type
= TREE_TYPE (type
);
1864 x
= get_nth_callarg (t
, i
);
1865 /* For member function, the first argument is a pointer to the implied
1866 object. For a constructor, it might still be a dummy object, in
1867 which case we get the real argument from ctx. */
1868 if (i
== 0 && DECL_CONSTRUCTOR_P (fun
)
1869 && is_dummy_object (x
))
1872 x
= build_address (x
);
1874 if (TREE_ADDRESSABLE (type
))
1875 /* Undo convert_for_arg_passing work here. */
1876 x
= convert_from_reference (x
);
1877 /* Normally we would strip a TARGET_EXPR in an initialization context
1878 such as this, but here we do the elision differently: we keep the
1879 TARGET_EXPR, and use its CONSTRUCTOR as the value of the parm. */
1880 arg
= cxx_eval_constant_expression (ctx
, x
, vc_prvalue
,
1881 non_constant_p
, overflow_p
);
1882 /* Check we aren't dereferencing a null pointer when calling a non-static
1883 member function, which is undefined behaviour. */
1884 if (i
== 0 && DECL_OBJECT_MEMBER_FUNCTION_P (fun
)
1885 && integer_zerop (arg
)
1886 /* But ignore calls from within compiler-generated code, to handle
1887 cases like lambda function pointer conversion operator thunks
1888 which pass NULL as the 'this' pointer. */
1889 && !(TREE_CODE (t
) == CALL_EXPR
&& CALL_FROM_THUNK_P (t
)))
1892 error_at (cp_expr_loc_or_input_loc (x
),
1893 "dereferencing a null pointer");
1894 *non_constant_p
= true;
1896 /* Don't VERIFY_CONSTANT here. */
1897 if (*non_constant_p
&& ctx
->quiet
)
1899 /* Just discard ellipsis args after checking their constantitude. */
1903 if (!*non_constant_p
)
1905 /* Make sure the binding has the same type as the parm. But
1906 only for constant args. */
1907 if (!TYPE_REF_P (type
))
1908 arg
= adjust_temp_type (type
, arg
);
1909 if (!TREE_CONSTANT (arg
))
1910 *non_constant_args
= true;
1911 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
1912 /* The destructor needs to see any modifications the callee makes
1914 *non_constant_args
= true;
1915 /* If arg is or contains address of a heap artificial variable or
1916 of a static variable being constructed, avoid caching the
1917 function call, as those variables might be modified by the
1918 function, or might be modified by the callers in between
1919 the cached function and just read by the function. */
1920 else if (!*non_constant_args
1921 && cp_walk_tree (&arg
, addr_of_non_const_var
, ctx
->global
,
1923 *non_constant_args
= true;
1925 /* For virtual calls, adjust the this argument, so that it is
1926 the object on which the method is called, rather than
1927 one of its bases. */
1928 if (i
== 0 && DECL_VIRTUAL_P (fun
))
1932 if (TREE_CODE (addr
) == ADDR_EXPR
)
1934 tree obj
= TREE_OPERAND (addr
, 0);
1935 while (TREE_CODE (obj
) == COMPONENT_REF
1936 && DECL_FIELD_IS_BASE (TREE_OPERAND (obj
, 1))
1937 && !same_type_ignoring_top_level_qualifiers_p
1938 (TREE_TYPE (obj
), DECL_CONTEXT (fun
)))
1939 obj
= TREE_OPERAND (obj
, 0);
1940 if (obj
!= TREE_OPERAND (addr
, 0))
1941 arg
= build_fold_addr_expr_with_type (obj
,
1945 TREE_VEC_ELT (binds
, i
) = arg
;
1947 parms
= TREE_CHAIN (parms
);
1953 /* Variables and functions to manage constexpr call expansion context.
1954 These do not need to be marked for PCH or GC. */
1956 /* FIXME remember and print actual constant arguments. */
1957 static vec
<tree
> call_stack
;
1958 static int call_stack_tick
;
1959 static int last_cx_error_tick
;
1962 push_cx_call_context (tree call
)
1965 if (!EXPR_HAS_LOCATION (call
))
1966 SET_EXPR_LOCATION (call
, input_location
);
1967 call_stack
.safe_push (call
);
1968 int len
= call_stack
.length ();
1969 if (len
> max_constexpr_depth
)
1975 pop_cx_call_context (void)
1982 cx_error_context (void)
1984 vec
<tree
> r
= vNULL
;
1985 if (call_stack_tick
!= last_cx_error_tick
1986 && !call_stack
.is_empty ())
1988 last_cx_error_tick
= call_stack_tick
;
1992 /* E is an operand of a failed assertion, fold it either with or without
1993 constexpr context. */
1996 fold_operand (tree e
, const constexpr_ctx
*ctx
)
2000 bool new_non_constant_p
= false, new_overflow_p
= false;
2001 e
= cxx_eval_constant_expression (ctx
, e
, vc_prvalue
,
2002 &new_non_constant_p
,
2006 e
= fold_non_dependent_expr (e
, tf_none
, /*manifestly_const_eval=*/true);
2010 /* If we have a condition in conjunctive normal form (CNF), find the first
2011 failing clause. In other words, given an expression like
2013 true && true && false && true && false
2015 return the first 'false'. EXPR is the expression. */
2018 find_failing_clause_r (const constexpr_ctx
*ctx
, tree expr
)
2020 if (TREE_CODE (expr
) == TRUTH_ANDIF_EXPR
)
2022 /* First check the left side... */
2023 tree e
= find_failing_clause_r (ctx
, TREE_OPERAND (expr
, 0));
2025 /* ...if we didn't find a false clause, check the right side. */
2026 e
= find_failing_clause_r (ctx
, TREE_OPERAND (expr
, 1));
2029 tree e
= contextual_conv_bool (expr
, tf_none
);
2030 e
= fold_operand (e
, ctx
);
2031 if (integer_zerop (e
))
2032 /* This is the failing clause. */
2037 /* Wrapper for find_failing_clause_r. */
2040 find_failing_clause (const constexpr_ctx
*ctx
, tree expr
)
2042 if (TREE_CODE (expr
) == TRUTH_ANDIF_EXPR
)
2043 if (tree e
= find_failing_clause_r (ctx
, expr
))
2048 /* Emit additional diagnostics for failing condition BAD.
2049 Used by finish_static_assert and IFN_ASSUME constexpr diagnostics.
2050 If SHOW_EXPR_P is true, print the condition (because it was
2051 instantiation-dependent). */
2054 diagnose_failing_condition (tree bad
, location_t cloc
, bool show_expr_p
,
2055 const constexpr_ctx
*ctx
/* = nullptr */)
2057 /* Nobody wants to see the artificial (bool) cast. */
2058 bad
= tree_strip_nop_conversions (bad
);
2059 if (TREE_CODE (bad
) == CLEANUP_POINT_EXPR
)
2060 bad
= TREE_OPERAND (bad
, 0);
2062 /* Actually explain the failure if this is a concept check or a
2063 requires-expression. */
2064 if (concept_check_p (bad
) || TREE_CODE (bad
) == REQUIRES_EXPR
)
2065 diagnose_constraints (cloc
, bad
, NULL_TREE
);
2066 else if (COMPARISON_CLASS_P (bad
)
2067 && ARITHMETIC_TYPE_P (TREE_TYPE (TREE_OPERAND (bad
, 0))))
2069 tree op0
= fold_operand (TREE_OPERAND (bad
, 0), ctx
);
2070 tree op1
= fold_operand (TREE_OPERAND (bad
, 1), ctx
);
2071 tree cond
= build2 (TREE_CODE (bad
), boolean_type_node
, op0
, op1
);
2072 inform (cloc
, "the comparison reduces to %qE", cond
);
2074 else if (show_expr_p
)
2075 inform (cloc
, "%qE evaluates to false", bad
);
2078 /* Process an assert/assume of ORIG_ARG. If it's not supposed to be evaluated,
2079 do it without changing the current evaluation state. If it evaluates to
2080 false, complain and return false; otherwise, return true. */
2083 cxx_eval_assert (const constexpr_ctx
*ctx
, tree arg
, const char *msg
,
2084 location_t loc
, bool evaluated
,
2085 bool *non_constant_p
, bool *overflow_p
)
2087 if (*non_constant_p
)
2093 if (!potential_rvalue_constant_expression (arg
))
2096 constexpr_ctx new_ctx
= *ctx
;
2097 new_ctx
.quiet
= true;
2098 bool new_non_constant_p
= false, new_overflow_p
= false;
2099 /* Avoid modification of existing values. */
2100 modifiable_tracker
ms (new_ctx
.global
);
2101 eval
= cxx_eval_constant_expression (&new_ctx
, arg
, vc_prvalue
,
2102 &new_non_constant_p
,
2106 eval
= cxx_eval_constant_expression (ctx
, arg
, vc_prvalue
,
2109 if (!*non_constant_p
&& integer_zerop (eval
))
2113 /* See if we can find which clause was failing
2114 (for logical AND). */
2115 tree bad
= find_failing_clause (ctx
, arg
);
2116 /* If not, or its location is unusable, fall back to the
2117 previous location. */
2118 location_t cloc
= cp_expr_loc_or_loc (bad
, loc
);
2120 /* Report the error. */
2121 auto_diagnostic_group d
;
2122 error_at (cloc
, msg
);
2123 diagnose_failing_condition (bad
, cloc
, true, ctx
);
2126 *non_constant_p
= true;
2133 /* Evaluate a call T to a GCC internal function when possible and return
2134 the evaluated result or, under the control of CTX, give an error, set
2135 NON_CONSTANT_P, and return the unevaluated call T otherwise. */
2138 cxx_eval_internal_function (const constexpr_ctx
*ctx
, tree t
,
2140 bool *non_constant_p
, bool *overflow_p
)
2142 enum tree_code opcode
= ERROR_MARK
;
2144 switch (CALL_EXPR_IFN (t
))
2146 case IFN_UBSAN_NULL
:
2147 case IFN_UBSAN_BOUNDS
:
2148 case IFN_UBSAN_VPTR
:
2149 case IFN_FALLTHROUGH
:
2153 if (!cxx_eval_assert (ctx
, CALL_EXPR_ARG (t
, 0),
2154 G_("failed %<assume%> attribute assumption"),
2155 EXPR_LOCATION (t
), /*eval*/false,
2156 non_constant_p
, overflow_p
))
2160 case IFN_ADD_OVERFLOW
:
2163 case IFN_SUB_OVERFLOW
:
2164 opcode
= MINUS_EXPR
;
2166 case IFN_MUL_OVERFLOW
:
2171 return cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 0),
2172 vc_prvalue
, non_constant_p
,
2175 case IFN_VEC_CONVERT
:
2177 tree arg
= cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 0),
2178 vc_prvalue
, non_constant_p
,
2180 if (TREE_CODE (arg
) == VECTOR_CST
)
2181 if (tree r
= fold_const_call (CFN_VEC_CONVERT
, TREE_TYPE (t
), arg
))
2188 error_at (cp_expr_loc_or_input_loc (t
),
2189 "call to internal function %qE", t
);
2190 *non_constant_p
= true;
2194 /* Evaluate constant arguments using OPCODE and return a complex
2195 number containing the result and the overflow bit. */
2196 tree arg0
= cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 0), lval
,
2197 non_constant_p
, overflow_p
);
2198 tree arg1
= cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 1), lval
,
2199 non_constant_p
, overflow_p
);
2201 if (TREE_CODE (arg0
) == INTEGER_CST
&& TREE_CODE (arg1
) == INTEGER_CST
)
2203 location_t loc
= cp_expr_loc_or_input_loc (t
);
2204 tree type
= TREE_TYPE (TREE_TYPE (t
));
2205 tree result
= fold_binary_loc (loc
, opcode
, type
,
2206 fold_convert_loc (loc
, type
, arg0
),
2207 fold_convert_loc (loc
, type
, arg1
));
2209 = build_int_cst (type
, arith_overflowed_p (opcode
, type
, arg0
, arg1
));
2210 /* Reset TREE_OVERFLOW to avoid warnings for the overflow. */
2211 if (TREE_OVERFLOW (result
))
2212 TREE_OVERFLOW (result
) = 0;
2214 return build_complex (TREE_TYPE (t
), result
, ovf
);
2217 *non_constant_p
= true;
2221 /* Clean CONSTRUCTOR_NO_CLEARING from CTOR and its sub-aggregates. */
2224 clear_no_implicit_zero (tree ctor
)
2226 if (CONSTRUCTOR_NO_CLEARING (ctor
))
2228 CONSTRUCTOR_NO_CLEARING (ctor
) = false;
2229 for (auto &e
: CONSTRUCTOR_ELTS (ctor
))
2230 if (TREE_CODE (e
.value
) == CONSTRUCTOR
)
2231 clear_no_implicit_zero (e
.value
);
2235 /* Complain about a const object OBJ being modified in a constant expression.
2236 EXPR is the MODIFY_EXPR expression performing the modification. */
2239 modifying_const_object_error (tree expr
, tree obj
)
2241 location_t loc
= cp_expr_loc_or_input_loc (expr
);
2242 auto_diagnostic_group d
;
2243 error_at (loc
, "modifying a const object %qE is not allowed in "
2244 "a constant expression", TREE_OPERAND (expr
, 0));
2246 /* Find the underlying object that was declared as const. */
2247 location_t decl_loc
= UNKNOWN_LOCATION
;
2248 for (tree probe
= obj
; decl_loc
== UNKNOWN_LOCATION
; )
2249 switch (TREE_CODE (probe
))
2254 tree elt
= TREE_OPERAND (probe
, 1);
2255 if (CP_TYPE_CONST_P (TREE_TYPE (elt
)))
2256 decl_loc
= DECL_SOURCE_LOCATION (elt
);
2257 probe
= TREE_OPERAND (probe
, 0);
2264 probe
= TREE_OPERAND (probe
, 0);
2268 decl_loc
= location_of (probe
);
2271 inform (decl_loc
, "originally declared %<const%> here");
2274 /* Return true if FNDECL is a replaceable global allocation function that
2275 should be useable during constant expression evaluation. */
2278 cxx_replaceable_global_alloc_fn (tree fndecl
)
2280 return (cxx_dialect
>= cxx20
2281 && IDENTIFIER_NEWDEL_OP_P (DECL_NAME (fndecl
))
2282 && CP_DECL_CONTEXT (fndecl
) == global_namespace
2283 && (DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl
)
2284 || DECL_IS_OPERATOR_DELETE_P (fndecl
)));
2287 /* Return true if FNDECL is a placement new function that should be
2288 useable during constant expression evaluation of std::construct_at. */
2291 cxx_placement_new_fn (tree fndecl
)
2293 if (cxx_dialect
>= cxx20
2294 && IDENTIFIER_NEW_OP_P (DECL_NAME (fndecl
))
2295 && CP_DECL_CONTEXT (fndecl
) == global_namespace
2296 && !DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl
)
2297 && TREE_CODE (TREE_TYPE (fndecl
)) == FUNCTION_TYPE
)
2299 tree first_arg
= TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
2300 if (TREE_VALUE (first_arg
) == ptr_type_node
2301 && TREE_CHAIN (first_arg
) == void_list_node
)
2307 /* Return true if FNDECL is std::construct_at. */
2310 is_std_construct_at (tree fndecl
)
2312 if (!decl_in_std_namespace_p (fndecl
))
2315 tree name
= DECL_NAME (fndecl
);
2316 return name
&& id_equal (name
, "construct_at");
2319 /* Overload for the above taking constexpr_call*. */
2322 is_std_construct_at (const constexpr_call
*call
)
2326 && is_std_construct_at (call
->fundef
->decl
));
2329 /* True if CTX is an instance of std::allocator. */
2332 is_std_allocator (tree ctx
)
2334 if (ctx
== NULL_TREE
|| !CLASS_TYPE_P (ctx
) || !TYPE_MAIN_DECL (ctx
))
2337 tree decl
= TYPE_MAIN_DECL (ctx
);
2338 tree name
= DECL_NAME (decl
);
2339 if (name
== NULL_TREE
|| !id_equal (name
, "allocator"))
2342 return decl_in_std_namespace_p (decl
);
2345 /* Return true if FNDECL is std::allocator<T>::{,de}allocate. */
2348 is_std_allocator_allocate (tree fndecl
)
2350 tree name
= DECL_NAME (fndecl
);
2351 if (name
== NULL_TREE
2352 || !(id_equal (name
, "allocate") || id_equal (name
, "deallocate")))
2355 return is_std_allocator (DECL_CONTEXT (fndecl
));
2358 /* Overload for the above taking constexpr_call*. */
2361 is_std_allocator_allocate (const constexpr_call
*call
)
2365 && is_std_allocator_allocate (call
->fundef
->decl
));
2368 /* Return true if FNDECL is std::source_location::current. */
2371 is_std_source_location_current (tree fndecl
)
2373 if (!decl_in_std_namespace_p (fndecl
))
2376 tree name
= DECL_NAME (fndecl
);
2377 if (name
== NULL_TREE
|| !id_equal (name
, "current"))
2380 tree ctx
= DECL_CONTEXT (fndecl
);
2381 if (ctx
== NULL_TREE
|| !CLASS_TYPE_P (ctx
) || !TYPE_MAIN_DECL (ctx
))
2384 name
= DECL_NAME (TYPE_MAIN_DECL (ctx
));
2385 return name
&& id_equal (name
, "source_location");
2388 /* Overload for the above taking constexpr_call*. */
2391 is_std_source_location_current (const constexpr_call
*call
)
2395 && is_std_source_location_current (call
->fundef
->decl
));
2398 /* Return true if FNDECL is __dynamic_cast. */
2401 cxx_dynamic_cast_fn_p (tree fndecl
)
2403 return (cxx_dialect
>= cxx20
2404 && id_equal (DECL_NAME (fndecl
), "__dynamic_cast")
2405 && CP_DECL_CONTEXT (fndecl
) == abi_node
);
2408 /* Often, we have an expression in the form of address + offset, e.g.
2409 "&_ZTV1A + 16". Extract the object from it, i.e. "_ZTV1A". */
2412 extract_obj_from_addr_offset (tree expr
)
2414 if (TREE_CODE (expr
) == POINTER_PLUS_EXPR
)
2415 expr
= TREE_OPERAND (expr
, 0);
2417 if (TREE_CODE (expr
) == ADDR_EXPR
)
2418 expr
= TREE_OPERAND (expr
, 0);
2422 /* Given a PATH like
2424 g.D.2181.D.2154.D.2102.D.2093
2426 find a component with type TYPE. Return NULL_TREE if not found, and
2427 error_mark_node if the component is not accessible. If STOP is non-null,
2428 this function will return NULL_TREE if STOP is found before TYPE. */
2431 get_component_with_type (tree path
, tree type
, tree stop
)
2435 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path
), type
))
2439 && (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path
),
2442 else if (TREE_CODE (path
) == COMPONENT_REF
2443 && DECL_FIELD_IS_BASE (TREE_OPERAND (path
, 1)))
2445 /* We need to check that the component we're accessing is in fact
2447 if (TREE_PRIVATE (TREE_OPERAND (path
, 1))
2448 || TREE_PROTECTED (TREE_OPERAND (path
, 1)))
2449 return error_mark_node
;
2450 path
= TREE_OPERAND (path
, 0);
2457 /* Evaluate a call to __dynamic_cast (permitted by P1327R1).
2459 The declaration of __dynamic_cast is:
2461 void* __dynamic_cast (const void* __src_ptr,
2462 const __class_type_info* __src_type,
2463 const __class_type_info* __dst_type,
2464 ptrdiff_t __src2dst);
2466 where src2dst has the following possible values
2468 >-1: src_type is a unique public non-virtual base of dst_type
2469 dst_ptr + src2dst == src_ptr
2470 -1: unspecified relationship
2471 -2: src_type is not a public base of dst_type
2472 -3: src_type is a multiple public non-virtual base of dst_type
2474 Since literal types can't have virtual bases, we only expect hint >=0,
2478 cxx_eval_dynamic_cast_fn (const constexpr_ctx
*ctx
, tree call
,
2479 bool *non_constant_p
, bool *overflow_p
)
2481 /* T will be something like
2482 __dynamic_cast ((B*) b, &_ZTI1B, &_ZTI1D, 8)
2484 gcc_assert (call_expr_nargs (call
) == 4);
2485 tsubst_flags_t complain
= ctx
->quiet
? tf_none
: tf_warning_or_error
;
2486 tree obj
= CALL_EXPR_ARG (call
, 0);
2487 tree type
= CALL_EXPR_ARG (call
, 2);
2488 HOST_WIDE_INT hint
= int_cst_value (CALL_EXPR_ARG (call
, 3));
2489 location_t loc
= cp_expr_loc_or_input_loc (call
);
2491 /* Get the target type of the dynamic_cast. */
2492 gcc_assert (TREE_CODE (type
) == ADDR_EXPR
);
2493 type
= TREE_OPERAND (type
, 0);
2494 type
= TREE_TYPE (DECL_NAME (type
));
2496 /* TYPE can only be either T* or T&. We can't know which of these it
2497 is by looking at TYPE, but OBJ will be "(T*) x" in the first case,
2498 and something like "(T*)(T&)(T*) x" in the second case. */
2499 bool reference_p
= false;
2500 while (CONVERT_EXPR_P (obj
) || TREE_CODE (obj
) == SAVE_EXPR
)
2502 reference_p
|= TYPE_REF_P (TREE_TYPE (obj
));
2503 obj
= TREE_OPERAND (obj
, 0);
2506 /* Evaluate the object so that we know its dynamic type. */
2507 obj
= cxx_eval_constant_expression (ctx
, obj
, vc_prvalue
, non_constant_p
,
2509 if (*non_constant_p
)
2512 /* We expect OBJ to be in form of &d.D.2102 when HINT == 0,
2513 but when HINT is > 0, it can also be something like
2514 &d.D.2102 + 18446744073709551608, which includes the BINFO_OFFSET. */
2515 obj
= extract_obj_from_addr_offset (obj
);
2516 const tree objtype
= TREE_TYPE (obj
);
2517 /* If OBJ doesn't refer to a base field, we're done. */
2518 if (tree t
= (TREE_CODE (obj
) == COMPONENT_REF
2519 ? TREE_OPERAND (obj
, 1) : obj
))
2520 if (TREE_CODE (t
) != FIELD_DECL
|| !DECL_FIELD_IS_BASE (t
))
2526 auto_diagnostic_group d
;
2527 error_at (loc
, "reference %<dynamic_cast%> failed");
2528 inform (loc
, "dynamic type %qT of its operand does "
2529 "not have a base class of type %qT",
2532 *non_constant_p
= true;
2534 return integer_zero_node
;
2537 /* [class.cdtor] When a dynamic_cast is used in a constructor ...
2538 or in a destructor ... if the operand of the dynamic_cast refers
2539 to the object under construction or destruction, this object is
2540 considered to be a most derived object that has the type of the
2541 constructor or destructor's class. */
2542 tree vtable
= build_vfield_ref (obj
, objtype
);
2543 vtable
= cxx_eval_constant_expression (ctx
, vtable
, vc_prvalue
,
2544 non_constant_p
, overflow_p
);
2545 if (*non_constant_p
)
2547 /* With -fsanitize=vptr, we initialize all vtable pointers to null,
2548 so it's possible that we got a null pointer now. */
2549 if (integer_zerop (vtable
))
2552 error_at (loc
, "virtual table pointer is used uninitialized");
2553 *non_constant_p
= true;
2554 return integer_zero_node
;
2556 /* VTABLE will be &_ZTV1A + 16 or similar, get _ZTV1A. */
2557 vtable
= extract_obj_from_addr_offset (vtable
);
2558 const tree mdtype
= DECL_CONTEXT (vtable
);
2560 /* Given dynamic_cast<T>(v),
2562 [expr.dynamic.cast] If C is the class type to which T points or refers,
2563 the runtime check logically executes as follows:
2565 If, in the most derived object pointed (referred) to by v, v points
2566 (refers) to a public base class subobject of a C object, and if only
2567 one object of type C is derived from the subobject pointed (referred)
2568 to by v the result points (refers) to that C object.
2570 In this case, HINT >= 0 or -3. */
2571 if (hint
>= 0 || hint
== -3)
2573 /* Look for a component with type TYPE. */
2574 tree t
= get_component_with_type (obj
, type
, mdtype
);
2575 /* If not accessible, give an error. */
2576 if (t
== error_mark_node
)
2582 auto_diagnostic_group d
;
2583 error_at (loc
, "reference %<dynamic_cast%> failed");
2584 inform (loc
, "static type %qT of its operand is a "
2585 "non-public base class of dynamic type %qT",
2589 *non_constant_p
= true;
2591 return integer_zero_node
;
2594 /* The result points to the TYPE object. */
2595 return cp_build_addr_expr (t
, complain
);
2596 /* Else, TYPE was not found, because the HINT turned out to be wrong.
2597 Fall through to the normal processing. */
2600 /* Otherwise, if v points (refers) to a public base class subobject of the
2601 most derived object, and the type of the most derived object has a base
2602 class, of type C, that is unambiguous and public, the result points
2603 (refers) to the C subobject of the most derived object.
2605 But it can also be an invalid case. */
2607 /* Get the most derived object. */
2608 obj
= get_component_with_type (obj
, mdtype
, NULL_TREE
);
2609 if (obj
== error_mark_node
)
2615 auto_diagnostic_group d
;
2616 error_at (loc
, "reference %<dynamic_cast%> failed");
2617 inform (loc
, "static type %qT of its operand is a non-public"
2618 " base class of dynamic type %qT", objtype
, mdtype
);
2620 *non_constant_p
= true;
2622 return integer_zero_node
;
2627 /* Check that the type of the most derived object has a base class
2628 of type TYPE that is unambiguous and public. */
2630 tree binfo
= lookup_base (mdtype
, type
, ba_check
, &b_kind
, tf_none
);
2631 if (!binfo
|| binfo
== error_mark_node
)
2637 auto_diagnostic_group d
;
2638 error_at (loc
, "reference %<dynamic_cast%> failed");
2639 if (b_kind
== bk_ambig
)
2640 inform (loc
, "%qT is an ambiguous base class of dynamic "
2641 "type %qT of its operand", type
, mdtype
);
2643 inform (loc
, "dynamic type %qT of its operand does not "
2644 "have an unambiguous public base class %qT",
2647 *non_constant_p
= true;
2649 return integer_zero_node
;
2651 /* If so, return the TYPE subobject of the most derived object. */
2652 obj
= convert_to_base_statically (obj
, binfo
);
2653 return cp_build_addr_expr (obj
, complain
);
2656 /* Data structure used by replace_decl and replace_decl_r. */
2658 struct replace_decl_data
2660 /* The _DECL we want to replace. */
2662 /* The replacement for DECL. */
2664 /* Trees we've visited. */
2665 hash_set
<tree
> *pset
;
2666 /* Whether we've performed any replacements. */
2670 /* Helper function for replace_decl, called through cp_walk_tree. */
2673 replace_decl_r (tree
*tp
, int *walk_subtrees
, void *data
)
2675 replace_decl_data
*d
= (replace_decl_data
*) data
;
2679 *tp
= unshare_expr (d
->replacement
);
2683 else if (TYPE_P (*tp
)
2684 || d
->pset
->add (*tp
))
2690 /* Replace every occurrence of DECL with (an unshared copy of)
2691 REPLACEMENT within the expression *TP. Returns true iff a
2692 replacement was performed. */
2695 replace_decl (tree
*tp
, tree decl
, tree replacement
)
2697 gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
2698 (TREE_TYPE (decl
), TREE_TYPE (replacement
)));
2699 hash_set
<tree
> pset
;
2700 replace_decl_data data
= { decl
, replacement
, &pset
, false };
2701 cp_walk_tree (tp
, replace_decl_r
, &data
, NULL
);
2702 return data
.changed
;
2705 /* Evaluate the call T to virtual function thunk THUNK_FNDECL. */
2708 cxx_eval_thunk_call (const constexpr_ctx
*ctx
, tree t
, tree thunk_fndecl
,
2710 bool *non_constant_p
, bool *overflow_p
)
2712 tree function
= THUNK_TARGET (thunk_fndecl
);
2714 if (THUNK_VIRTUAL_OFFSET (thunk_fndecl
))
2718 if (!DECL_DECLARED_CONSTEXPR_P (function
))
2720 error ("call to non-%<constexpr%> function %qD", function
);
2721 explain_invalid_constexpr_fn (function
);
2724 /* virtual_offset is only set for virtual bases, which make the
2725 class non-literal, so we don't need to handle it here. */
2726 error ("calling constexpr member function %qD through virtual "
2727 "base subobject", function
);
2729 *non_constant_p
= true;
2733 tree new_call
= copy_node (t
);
2734 CALL_EXPR_FN (new_call
) = function
;
2735 TREE_TYPE (new_call
) = TREE_TYPE (TREE_TYPE (function
));
2737 tree offset
= size_int (THUNK_FIXED_OFFSET (thunk_fndecl
));
2739 if (DECL_THIS_THUNK_P (thunk_fndecl
))
2741 /* 'this'-adjusting thunk. */
2742 tree this_arg
= CALL_EXPR_ARG (t
, 0);
2743 this_arg
= build2 (POINTER_PLUS_EXPR
, TREE_TYPE (this_arg
),
2745 CALL_EXPR_ARG (new_call
, 0) = this_arg
;
2748 /* Return-adjusting thunk. */
2749 new_call
= build2 (POINTER_PLUS_EXPR
, TREE_TYPE (new_call
),
2752 return cxx_eval_constant_expression (ctx
, new_call
, lval
,
2753 non_constant_p
, overflow_p
);
2756 /* If OBJECT is of const class type, evaluate it to a CONSTRUCTOR and set
2757 its TREE_READONLY flag according to READONLY_P. Used for constexpr
2758 'tors to detect modifying const objects in a constexpr context. */
2761 cxx_set_object_constness (const constexpr_ctx
*ctx
, tree object
,
2762 bool readonly_p
, bool *non_constant_p
,
2765 if (CLASS_TYPE_P (TREE_TYPE (object
))
2766 && CP_TYPE_CONST_P (TREE_TYPE (object
)))
2768 /* Subobjects might not be stored in ctx->global->values but we
2769 can get its CONSTRUCTOR by evaluating *this. */
2770 tree e
= cxx_eval_constant_expression (ctx
, object
, vc_prvalue
,
2771 non_constant_p
, overflow_p
);
2772 if (TREE_CODE (e
) == CONSTRUCTOR
&& !*non_constant_p
)
2773 TREE_READONLY (e
) = readonly_p
;
2777 /* Subroutine of cxx_eval_constant_expression.
2778 Evaluate the call expression tree T in the context of OLD_CALL expression
2782 cxx_eval_call_expression (const constexpr_ctx
*ctx
, tree t
,
2784 bool *non_constant_p
, bool *overflow_p
)
2786 /* Handle concept checks separately. */
2787 if (concept_check_p (t
))
2788 return evaluate_concept_check (t
);
2790 location_t loc
= cp_expr_loc_or_input_loc (t
);
2791 tree fun
= get_function_named_in_call (t
);
2792 constexpr_call new_call
2793 = { NULL
, NULL
, NULL
, 0, ctx
->manifestly_const_eval
};
2796 if (fun
== NULL_TREE
)
2797 return cxx_eval_internal_function (ctx
, t
, lval
,
2798 non_constant_p
, overflow_p
);
2800 if (TREE_CODE (fun
) != FUNCTION_DECL
)
2802 /* Might be a constexpr function pointer. */
2803 fun
= cxx_eval_constant_expression (ctx
, fun
, vc_prvalue
,
2804 non_constant_p
, overflow_p
);
2806 if (TREE_CODE (fun
) == ADDR_EXPR
)
2807 fun
= TREE_OPERAND (fun
, 0);
2808 /* For TARGET_VTABLE_USES_DESCRIPTORS targets, there is no
2809 indirection, the called expression is a pointer into the
2810 virtual table which should contain FDESC_EXPR. Extract the
2811 FUNCTION_DECL from there. */
2812 else if (TARGET_VTABLE_USES_DESCRIPTORS
2813 && TREE_CODE (fun
) == POINTER_PLUS_EXPR
2814 && TREE_CODE (TREE_OPERAND (fun
, 0)) == ADDR_EXPR
2815 && TREE_CODE (TREE_OPERAND (fun
, 1)) == INTEGER_CST
)
2817 tree d
= TREE_OPERAND (TREE_OPERAND (fun
, 0), 0);
2819 && DECL_VTABLE_OR_VTT_P (d
)
2820 && TREE_CODE (TREE_TYPE (d
)) == ARRAY_TYPE
2821 && TREE_TYPE (TREE_TYPE (d
)) == vtable_entry_type
2823 && TREE_CODE (DECL_INITIAL (d
)) == CONSTRUCTOR
)
2825 tree i
= int_const_binop (TRUNC_DIV_EXPR
, TREE_OPERAND (fun
, 1),
2826 TYPE_SIZE_UNIT (vtable_entry_type
));
2827 HOST_WIDE_INT idx
= find_array_ctor_elt (DECL_INITIAL (d
), i
);
2831 = (*CONSTRUCTOR_ELTS (DECL_INITIAL (d
)))[idx
].value
;
2832 if (TREE_CODE (fdesc
) == FDESC_EXPR
2833 && integer_zerop (TREE_OPERAND (fdesc
, 1)))
2834 fun
= TREE_OPERAND (fdesc
, 0);
2839 if (TREE_CODE (fun
) != FUNCTION_DECL
)
2841 if (!ctx
->quiet
&& !*non_constant_p
)
2842 error_at (loc
, "expression %qE does not designate a %<constexpr%> "
2844 *non_constant_p
= true;
2847 if (DECL_CLONED_FUNCTION_P (fun
) && !DECL_DELETING_DESTRUCTOR_P (fun
))
2848 fun
= DECL_CLONED_FUNCTION (fun
);
2850 if (is_ubsan_builtin_p (fun
))
2853 if (fndecl_built_in_p (fun
))
2854 return cxx_eval_builtin_function_call (ctx
, t
, fun
,
2855 lval
, non_constant_p
, overflow_p
);
2856 if (DECL_THUNK_P (fun
))
2857 return cxx_eval_thunk_call (ctx
, t
, fun
, lval
, non_constant_p
, overflow_p
);
2858 if (!maybe_constexpr_fn (fun
))
2860 if (TREE_CODE (t
) == CALL_EXPR
2861 && cxx_replaceable_global_alloc_fn (fun
)
2862 && (CALL_FROM_NEW_OR_DELETE_P (t
)
2863 || is_std_allocator_allocate (ctx
->call
)))
2865 const bool new_op_p
= IDENTIFIER_NEW_OP_P (DECL_NAME (fun
));
2866 const int nargs
= call_expr_nargs (t
);
2867 tree arg0
= NULL_TREE
;
2868 for (int i
= 0; i
< nargs
; ++i
)
2870 tree arg
= CALL_EXPR_ARG (t
, i
);
2871 arg
= cxx_eval_constant_expression (ctx
, arg
, vc_prvalue
,
2872 non_constant_p
, overflow_p
);
2873 /* Deleting a non-constant pointer has a better error message
2875 if (new_op_p
|| i
!= 0)
2876 VERIFY_CONSTANT (arg
);
2883 tree type
= build_array_type_nelts (char_type_node
,
2884 tree_to_uhwi (arg0
));
2885 tree var
= build_decl (loc
, VAR_DECL
,
2886 (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun
))
2888 ? heap_vec_uninit_identifier
2889 : heap_uninit_identifier
,
2891 DECL_ARTIFICIAL (var
) = 1;
2892 TREE_STATIC (var
) = 1;
2893 // Temporarily register the artificial var in varpool,
2894 // so that comparisons of its address against NULL are folded
2895 // through nonzero_address even with
2896 // -fno-delete-null-pointer-checks or that comparison of
2897 // addresses of different heap artificial vars is folded too.
2898 // See PR98988 and PR99031.
2899 varpool_node::finalize_decl (var
);
2900 ctx
->global
->heap_vars
.safe_push (var
);
2901 ctx
->global
->put_value (var
, NULL_TREE
);
2902 return fold_convert (ptr_type_node
, build_address (var
));
2907 if (TREE_CODE (arg0
) == ADDR_EXPR
2908 && VAR_P (TREE_OPERAND (arg0
, 0)))
2910 tree var
= TREE_OPERAND (arg0
, 0);
2911 if (DECL_NAME (var
) == heap_uninit_identifier
2912 || DECL_NAME (var
) == heap_identifier
)
2914 if (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun
))
2919 auto_diagnostic_group d
;
2920 error_at (loc
, "array deallocation of object "
2921 "allocated with non-array "
2923 inform (DECL_SOURCE_LOCATION (var
),
2924 "allocation performed here");
2926 *non_constant_p
= true;
2929 DECL_NAME (var
) = heap_deleted_identifier
;
2930 ctx
->global
->destroy_value (var
);
2931 ctx
->global
->heap_dealloc_count
++;
2934 else if (DECL_NAME (var
) == heap_vec_uninit_identifier
2935 || DECL_NAME (var
) == heap_vec_identifier
)
2937 if ((IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun
))
2938 & OVL_OP_FLAG_VEC
) == 0)
2942 auto_diagnostic_group d
;
2943 error_at (loc
, "non-array deallocation of "
2944 "object allocated with array "
2946 inform (DECL_SOURCE_LOCATION (var
),
2947 "allocation performed here");
2949 *non_constant_p
= true;
2952 DECL_NAME (var
) = heap_deleted_identifier
;
2953 ctx
->global
->destroy_value (var
);
2954 ctx
->global
->heap_dealloc_count
++;
2957 else if (DECL_NAME (var
) == heap_deleted_identifier
)
2960 error_at (loc
, "deallocation of already deallocated "
2962 *non_constant_p
= true;
2967 error_at (loc
, "deallocation of storage that was "
2968 "not previously allocated");
2969 *non_constant_p
= true;
2973 /* Allow placement new in std::construct_at, just return the second
2975 if (TREE_CODE (t
) == CALL_EXPR
2976 && cxx_placement_new_fn (fun
)
2977 && is_std_construct_at (ctx
->call
))
2979 const int nargs
= call_expr_nargs (t
);
2980 tree arg1
= NULL_TREE
;
2981 for (int i
= 0; i
< nargs
; ++i
)
2983 tree arg
= CALL_EXPR_ARG (t
, i
);
2984 arg
= cxx_eval_constant_expression (ctx
, arg
, vc_prvalue
,
2985 non_constant_p
, overflow_p
);
2989 VERIFY_CONSTANT (arg
);
2994 else if (cxx_dynamic_cast_fn_p (fun
))
2995 return cxx_eval_dynamic_cast_fn (ctx
, t
, non_constant_p
, overflow_p
);
2999 if (!lambda_static_thunk_p (fun
))
3000 error_at (loc
, "call to non-%<constexpr%> function %qD", fun
);
3001 explain_invalid_constexpr_fn (fun
);
3003 *non_constant_p
= true;
3007 constexpr_ctx new_ctx
= *ctx
;
3008 if (DECL_CONSTRUCTOR_P (fun
) && !ctx
->object
3009 && TREE_CODE (t
) == AGGR_INIT_EXPR
)
3011 /* We want to have an initialization target for an AGGR_INIT_EXPR.
3012 If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
3013 new_ctx
.object
= AGGR_INIT_EXPR_SLOT (t
);
3014 tree ctor
= new_ctx
.ctor
= build_constructor (DECL_CONTEXT (fun
), NULL
);
3015 CONSTRUCTOR_NO_CLEARING (ctor
) = true;
3016 ctx
->global
->put_value (new_ctx
.object
, ctor
);
3020 /* We used to shortcut trivial constructor/op= here, but nowadays
3021 we can only get a trivial function here with -fno-elide-constructors. */
3022 gcc_checking_assert (!trivial_fn_p (fun
)
3023 || !flag_elide_constructors
3024 /* We don't elide constructors when processing
3025 a noexcept-expression. */
3026 || cp_noexcept_operand
);
3028 bool non_constant_args
= false;
3030 = cxx_bind_parameters_in_call (ctx
, t
, fun
, non_constant_p
,
3031 overflow_p
, &non_constant_args
);
3033 /* We build up the bindings list before we know whether we already have this
3034 call cached. If we don't end up saving these bindings, ggc_free them when
3035 this function exits. */
3040 free_bindings (tree
&b
): bindings (&b
) { }
3041 ~free_bindings () { if (bindings
) ggc_free (*bindings
); }
3042 void preserve () { bindings
= NULL
; }
3043 } fb (new_call
.bindings
);
3045 if (*non_constant_p
)
3048 /* We can't defer instantiating the function any longer. */
3049 if (!DECL_INITIAL (fun
)
3050 && (DECL_TEMPLOID_INSTANTIATION (fun
) || DECL_DEFAULTED_FN (fun
))
3051 && !uid_sensitive_constexpr_evaluation_p ())
3053 location_t save_loc
= input_location
;
3054 input_location
= loc
;
3056 if (ctx
->manifestly_const_eval
== mce_true
)
3057 FNDECL_MANIFESTLY_CONST_EVALUATED (fun
) = true;
3058 if (DECL_TEMPLOID_INSTANTIATION (fun
))
3059 instantiate_decl (fun
, /*defer_ok*/false, /*expl_inst*/false);
3061 synthesize_method (fun
);
3063 input_location
= save_loc
;
3066 /* If in direct recursive call, optimize definition search. */
3067 if (ctx
&& ctx
->call
&& ctx
->call
->fundef
&& ctx
->call
->fundef
->decl
== fun
)
3068 new_call
.fundef
= ctx
->call
->fundef
;
3071 new_call
.fundef
= retrieve_constexpr_fundef (fun
);
3072 if (new_call
.fundef
== NULL
|| new_call
.fundef
->body
== NULL
3073 || new_call
.fundef
->result
== error_mark_node
3074 || fun
== current_function_decl
)
3078 /* We need to check for current_function_decl here in case we're
3079 being called during cp_fold_function, because at that point
3080 DECL_INITIAL is set properly and we have a fundef but we
3081 haven't lowered invisirefs yet (c++/70344). */
3082 if (DECL_INITIAL (fun
) == error_mark_node
3083 || fun
== current_function_decl
)
3084 error_at (loc
, "%qD called in a constant expression before its "
3085 "definition is complete", fun
);
3086 else if (DECL_INITIAL (fun
))
3088 /* The definition of fun was somehow unsuitable. But pretend
3089 that lambda static thunks don't exist. */
3090 if (!lambda_static_thunk_p (fun
))
3091 error_at (loc
, "%qD called in a constant expression", fun
);
3092 explain_invalid_constexpr_fn (fun
);
3095 error_at (loc
, "%qD used before its definition", fun
);
3097 *non_constant_p
= true;
3102 depth_ok
= push_cx_call_context (t
);
3104 /* Remember the object we are constructing or destructing. */
3105 tree new_obj
= NULL_TREE
;
3106 if (DECL_CONSTRUCTOR_P (fun
) || DECL_DESTRUCTOR_P (fun
))
3108 /* In a cdtor, it should be the first `this' argument.
3109 At this point it has already been evaluated in the call
3110 to cxx_bind_parameters_in_call. */
3111 new_obj
= TREE_VEC_ELT (new_call
.bindings
, 0);
3112 new_obj
= cxx_fold_indirect_ref (ctx
, loc
, DECL_CONTEXT (fun
), new_obj
);
3114 if (ctx
->call
&& ctx
->call
->fundef
3115 && DECL_CONSTRUCTOR_P (ctx
->call
->fundef
->decl
))
3117 tree cur_obj
= TREE_VEC_ELT (ctx
->call
->bindings
, 0);
3118 STRIP_NOPS (cur_obj
);
3119 if (TREE_CODE (cur_obj
) == ADDR_EXPR
)
3120 cur_obj
= TREE_OPERAND (cur_obj
, 0);
3121 if (new_obj
== cur_obj
)
3122 /* We're calling the target constructor of a delegating
3123 constructor, or accessing a base subobject through a
3124 NOP_EXPR as part of a call to a base constructor, so
3125 there is no new (sub)object. */
3126 new_obj
= NULL_TREE
;
3130 tree result
= NULL_TREE
;
3132 constexpr_call
*entry
= NULL
;
3133 if (depth_ok
&& !non_constant_args
&& ctx
->strict
)
3135 new_call
.hash
= constexpr_fundef_hasher::hash (new_call
.fundef
);
3137 = iterative_hash_template_arg (new_call
.bindings
, new_call
.hash
);
3139 = iterative_hash_object (ctx
->manifestly_const_eval
, new_call
.hash
);
3141 /* If we have seen this call before, we are done. */
3142 maybe_initialize_constexpr_call_table ();
3143 bool insert
= depth_ok
< constexpr_cache_depth
;
3144 constexpr_call
**slot
3145 = constexpr_call_table
->find_slot (&new_call
,
3146 insert
? INSERT
: NO_INSERT
);
3147 entry
= slot
? *slot
: NULL
;
3150 /* Only cache up to constexpr_cache_depth to limit memory use. */
3153 /* We need to keep a pointer to the entry, not just the slot, as
3154 the slot can move during evaluation of the body. */
3155 *slot
= entry
= ggc_alloc
<constexpr_call
> ();
3160 /* Calls that are in progress have their result set to NULL, so that we
3161 can detect circular dependencies. Now that we only cache up to
3162 constexpr_cache_depth this won't catch circular dependencies that
3163 start deeper, but they'll hit the recursion or ops limit. */
3164 else if (entry
->result
== NULL
)
3167 error ("call has circular dependency");
3168 *non_constant_p
= true;
3169 entry
->result
= result
= error_mark_node
;
3172 result
= entry
->result
;
3178 error ("%<constexpr%> evaluation depth exceeds maximum of %d (use "
3179 "%<-fconstexpr-depth=%> to increase the maximum)",
3180 max_constexpr_depth
);
3181 *non_constant_p
= true;
3182 result
= error_mark_node
;
3186 bool cacheable
= !!entry
;
3187 if (result
&& result
!= error_mark_node
)
3189 else if (!DECL_SAVED_TREE (fun
))
3191 /* When at_eof >= 3, cgraph has started throwing away
3192 DECL_SAVED_TREE, so fail quietly. FIXME we get here because of
3193 late code generation for VEC_INIT_EXPR, which needs to be
3194 completely reconsidered. */
3195 gcc_assert (at_eof
>= 3 && ctx
->quiet
);
3196 *non_constant_p
= true;
3198 else if (tree copy
= get_fundef_copy (new_call
.fundef
))
3200 tree body
, parms
, res
;
3201 releasing_vec ctors
;
3203 /* Reuse or create a new unshared copy of this function's body. */
3204 body
= TREE_PURPOSE (copy
);
3205 parms
= TREE_VALUE (copy
);
3206 res
= TREE_TYPE (copy
);
3208 /* Associate the bindings with the remapped parms. */
3209 tree bound
= new_call
.bindings
;
3210 tree remapped
= parms
;
3211 for (int i
= 0; i
< TREE_VEC_LENGTH (bound
); ++i
)
3213 tree arg
= TREE_VEC_ELT (bound
, i
);
3216 /* Unshare args going into the hash table to separate them
3217 from the caller's context, for better GC and to avoid
3218 problems with verify_gimple. */
3219 arg
= unshare_expr_without_location (arg
);
3220 TREE_VEC_ELT (bound
, i
) = arg
;
3222 /* And then unshare again so the callee doesn't change the
3223 argument values in the hash table. XXX Could we unshare
3224 lazily in cxx_eval_store_expression? */
3225 arg
= unshare_constructor (arg
);
3226 if (TREE_CODE (arg
) == CONSTRUCTOR
)
3227 vec_safe_push (ctors
, arg
);
3229 ctx
->global
->put_value (remapped
, arg
);
3230 remapped
= DECL_CHAIN (remapped
);
3232 for (; remapped
; remapped
= TREE_CHAIN (remapped
))
3233 if (DECL_NAME (remapped
) == in_charge_identifier
)
3235 /* FIXME destructors unnecessarily have in-charge parameters
3236 even in classes without vbases, map it to 0 for now. */
3237 gcc_assert (!CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun
)));
3238 ctx
->global
->put_value (remapped
, integer_zero_node
);
3242 gcc_assert (seen_error ());
3243 *non_constant_p
= true;
3245 /* Add the RESULT_DECL to the values map, too. */
3246 gcc_assert (!DECL_BY_REFERENCE (res
));
3247 ctx
->global
->put_value (res
, NULL_TREE
);
3249 /* Remember the current call we're evaluating. */
3250 constexpr_ctx call_ctx
= *ctx
;
3251 call_ctx
.call
= &new_call
;
3252 unsigned save_heap_alloc_count
= ctx
->global
->heap_vars
.length ();
3253 unsigned save_heap_dealloc_count
= ctx
->global
->heap_dealloc_count
;
3255 /* Make sure we fold std::is_constant_evaluated to true in an
3256 immediate function. */
3257 if (DECL_IMMEDIATE_FUNCTION_P (fun
))
3258 call_ctx
.manifestly_const_eval
= mce_true
;
3260 /* If this is a constexpr destructor, the object's const and volatile
3261 semantics are no longer in effect; see [class.dtor]p5. */
3262 if (new_obj
&& DECL_DESTRUCTOR_P (fun
))
3263 cxx_set_object_constness (ctx
, new_obj
, /*readonly_p=*/false,
3264 non_constant_p
, overflow_p
);
3266 /* If this is a constructor, we are beginning the lifetime of the
3267 object we are initializing. */
3269 && DECL_CONSTRUCTOR_P (fun
)
3270 && TREE_CODE (new_obj
) == COMPONENT_REF
3271 && TREE_CODE (TREE_TYPE (TREE_OPERAND (new_obj
, 0))) == UNION_TYPE
)
3273 tree activate
= build2 (INIT_EXPR
, TREE_TYPE (new_obj
),
3275 build_constructor (TREE_TYPE (new_obj
),
3277 cxx_eval_constant_expression (ctx
, activate
,
3278 lval
, non_constant_p
, overflow_p
);
3279 ggc_free (activate
);
3282 tree jump_target
= NULL_TREE
;
3283 cxx_eval_constant_expression (&call_ctx
, body
,
3284 vc_discard
, non_constant_p
, overflow_p
,
3287 if (DECL_CONSTRUCTOR_P (fun
))
3288 /* This can be null for a subobject constructor call, in
3289 which case what we care about is the initialization
3290 side-effects rather than the value. We could get at the
3291 value by evaluating *this, but we don't bother; there's
3292 no need to put such a call in the hash table. */
3293 result
= lval
? ctx
->object
: ctx
->ctor
;
3294 else if (VOID_TYPE_P (TREE_TYPE (res
)))
3298 result
= ctx
->global
->get_value (res
);
3299 if (result
== NULL_TREE
&& !*non_constant_p
3300 && !DECL_DESTRUCTOR_P (fun
))
3303 error ("%<constexpr%> call flows off the end "
3305 *non_constant_p
= true;
3309 /* At this point, the object's constructor will have run, so
3310 the object is no longer under construction, and its possible
3311 'const' semantics now apply. Make a note of this fact by
3312 marking the CONSTRUCTOR TREE_READONLY. */
3313 if (new_obj
&& DECL_CONSTRUCTOR_P (fun
))
3314 cxx_set_object_constness (ctx
, new_obj
, /*readonly_p=*/true,
3315 non_constant_p
, overflow_p
);
3317 /* Remove the parms/result from the values map. */
3318 destroy_value_checked (ctx
, res
, non_constant_p
);
3319 for (tree parm
= parms
; parm
; parm
= TREE_CHAIN (parm
))
3320 destroy_value_checked (ctx
, parm
, non_constant_p
);
3322 /* Free any parameter CONSTRUCTORs we aren't returning directly. */
3323 while (!ctors
->is_empty ())
3325 tree c
= ctors
->pop ();
3327 free_constructor (c
);
3330 /* Make the unshared function copy we used available for re-use. */
3331 save_fundef_copy (fun
, copy
);
3333 /* If the call allocated some heap object that hasn't been
3334 deallocated during the call, or if it deallocated some heap
3335 object it has not allocated, the call isn't really stateless
3336 for the constexpr evaluation and should not be cached.
3337 It is fine if the call allocates something and deallocates it
3340 && (save_heap_alloc_count
!= ctx
->global
->heap_vars
.length ()
3341 || (save_heap_dealloc_count
3342 != ctx
->global
->heap_dealloc_count
)))
3346 if ((ctx
->global
->heap_vars
.length ()
3347 - ctx
->global
->heap_dealloc_count
)
3348 != save_heap_alloc_count
- save_heap_dealloc_count
)
3351 FOR_EACH_VEC_ELT_FROM (ctx
->global
->heap_vars
, i
, heap_var
,
3352 save_heap_alloc_count
)
3353 if (DECL_NAME (heap_var
) != heap_deleted_identifier
)
3360 /* Rewrite all occurrences of the function's RESULT_DECL with the
3361 current object under construction. */
3362 if (!*non_constant_p
&& ctx
->object
3363 && CLASS_TYPE_P (TREE_TYPE (res
))
3364 && !is_empty_class (TREE_TYPE (res
)))
3365 if (replace_decl (&result
, res
, ctx
->object
))
3368 /* Only cache a permitted result of a constant expression. */
3369 if (cacheable
&& !reduced_constant_expression_p (result
))
3373 /* Couldn't get a function copy to evaluate. */
3374 *non_constant_p
= true;
3376 if (result
== error_mark_node
)
3377 *non_constant_p
= true;
3378 if (*non_constant_p
|| *overflow_p
)
3379 result
= error_mark_node
;
3383 entry
->result
= cacheable
? result
: error_mark_node
;
3386 /* The result of a constexpr function must be completely initialized.
3388 However, in C++20, a constexpr constructor doesn't necessarily have
3389 to initialize all the fields, so we don't clear CONSTRUCTOR_NO_CLEARING
3390 in order to detect reading an unitialized object in constexpr instead
3391 of value-initializing it. (reduced_constant_expression_p is expected to
3392 take care of clearing the flag.) */
3393 if (TREE_CODE (result
) == CONSTRUCTOR
3394 && (cxx_dialect
< cxx20
3395 || !DECL_CONSTRUCTOR_P (fun
)))
3396 clear_no_implicit_zero (result
);
3398 pop_cx_call_context ();
3402 /* Return true if T is a valid constant initializer. If a CONSTRUCTOR
3403 initializes all the members, the CONSTRUCTOR_NO_CLEARING flag will be
3405 FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
3408 reduced_constant_expression_p (tree t
)
3413 switch (TREE_CODE (t
))
3416 /* Even if we can't lower this yet, it's constant. */
3420 /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
3422 if (!AGGREGATE_TYPE_P (TREE_TYPE (t
)))
3423 /* A constant vector would be folded to VECTOR_CST.
3424 A CONSTRUCTOR of scalar type means uninitialized. */
3426 if (CONSTRUCTOR_NO_CLEARING (t
))
3428 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
3430 /* There must be a valid constant initializer at every array
3432 tree min
= TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (t
)));
3433 tree max
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (t
)));
3435 for (auto &e
: CONSTRUCTOR_ELTS (t
))
3437 if (!reduced_constant_expression_p (e
.value
))
3439 if (array_index_cmp (cursor
, e
.index
) != 0)
3441 if (TREE_CODE (e
.index
) == RANGE_EXPR
)
3442 cursor
= TREE_OPERAND (e
.index
, 1);
3443 cursor
= int_const_binop (PLUS_EXPR
, cursor
, size_one_node
);
3445 if (find_array_ctor_elt (t
, max
) == -1)
3449 else if (cxx_dialect
>= cxx20
3450 && TREE_CODE (TREE_TYPE (t
)) == UNION_TYPE
)
3452 if (CONSTRUCTOR_NELTS (t
) == 0)
3453 /* An initialized union has a constructor element. */
3455 /* And it only initializes one member. */
3459 field
= next_subobject_field (TYPE_FIELDS (TREE_TYPE (t
)));
3463 for (auto &e
: CONSTRUCTOR_ELTS (t
))
3465 /* If VAL is null, we're in the middle of initializing this
3467 if (!reduced_constant_expression_p (e
.value
))
3469 /* We want to remove initializers for empty fields in a struct to
3470 avoid confusing output_constructor. */
3471 if (is_empty_field (e
.index
)
3472 && TREE_CODE (TREE_TYPE (t
)) == RECORD_TYPE
)
3474 /* Check for non-empty fields between initialized fields when
3475 CONSTRUCTOR_NO_CLEARING. */
3476 for (; field
&& e
.index
!= field
;
3477 field
= next_subobject_field (DECL_CHAIN (field
)))
3478 if (!is_really_empty_class (TREE_TYPE (field
),
3479 /*ignore_vptr*/false))
3482 field
= next_subobject_field (DECL_CHAIN (field
));
3484 /* There could be a non-empty field at the end. */
3485 for (; field
; field
= next_subobject_field (DECL_CHAIN (field
)))
3486 if (!is_really_empty_class (TREE_TYPE (field
), /*ignore_vptr*/false))
3489 if (CONSTRUCTOR_NO_CLEARING (t
))
3490 /* All the fields are initialized. */
3491 CONSTRUCTOR_NO_CLEARING (t
) = false;
3495 /* FIXME are we calling this too much? */
3496 return initializer_constant_valid_p (t
, TREE_TYPE (t
)) != NULL_TREE
;
3500 /* *TP was not deemed constant by reduced_constant_expression_p. Explain
3501 why and suggest what could be done about it. */
3504 verify_constant_explain_r (tree
*tp
, int *walk_subtrees
, void *)
3508 /* No need to look into types or unevaluated operands. */
3509 if (TYPE_P (*tp
) || unevaluated_p (TREE_CODE (*tp
)))
3511 *walk_subtrees
= false;
3515 switch (TREE_CODE (*tp
))
3518 if (TREE_CODE (TREE_OPERAND (*tp
, 0)) != ADDR_EXPR
)
3520 ref_p
= TYPE_REF_P (TREE_TYPE (*tp
));
3521 *tp
= TREE_OPERAND (*tp
, 0);
3525 tree op
= TREE_OPERAND (*tp
, 0);
3527 && DECL_DECLARED_CONSTEXPR_P (op
)
3528 && !TREE_STATIC (op
)
3529 /* ??? We should also say something about temporaries. */
3530 && !DECL_ARTIFICIAL (op
))
3533 inform (location_of (*tp
), "reference to %qD is not a constant "
3536 inform (location_of (*tp
), "pointer to %qD is not a constant "
3538 const location_t op_loc
= DECL_SOURCE_LOCATION (op
);
3539 rich_location
richloc (line_table
, op_loc
);
3540 richloc
.add_fixit_insert_before (op_loc
, "static ");
3542 "address of non-static constexpr variable %qD may differ on "
3543 "each invocation of the enclosing function; add %<static%> "
3544 "to give it a constant address", op
);
3555 /* Some expressions may have constant operands but are not constant
3556 themselves, such as 1/0. Call this function to check for that
3559 We only call this in places that require an arithmetic constant, not in
3560 places where we might have a non-constant expression that can be a
3561 component of a constant expression, such as the address of a constexpr
3562 variable that might be dereferenced later. */
3565 verify_constant (tree t
, bool allow_non_constant
, bool *non_constant_p
,
3568 if (!*non_constant_p
&& !reduced_constant_expression_p (t
)
3571 if (!allow_non_constant
)
3573 auto_diagnostic_group d
;
3574 error_at (cp_expr_loc_or_input_loc (t
),
3575 "%q+E is not a constant expression", t
);
3576 cp_walk_tree_without_duplicates (&t
, verify_constant_explain_r
,
3579 *non_constant_p
= true;
3581 if (TREE_OVERFLOW_P (t
))
3583 if (!allow_non_constant
)
3585 permerror (input_location
, "overflow in constant expression");
3586 /* If we're being permissive (and are in an enforcing
3587 context), ignore the overflow. */
3588 if (flag_permissive
)
3589 return *non_constant_p
;
3593 return *non_constant_p
;
3596 /* Check whether the shift operation with code CODE and type TYPE on LHS
3597 and RHS is undefined. If it is, give an error with an explanation,
3598 and return true; return false otherwise. */
3601 cxx_eval_check_shift_p (location_t loc
, const constexpr_ctx
*ctx
,
3602 enum tree_code code
, tree type
, tree lhs
, tree rhs
)
3604 if ((code
!= LSHIFT_EXPR
&& code
!= RSHIFT_EXPR
)
3605 || TREE_CODE (lhs
) != INTEGER_CST
3606 || TREE_CODE (rhs
) != INTEGER_CST
)
3609 tree lhstype
= TREE_TYPE (lhs
);
3610 unsigned HOST_WIDE_INT uprec
= TYPE_PRECISION (TREE_TYPE (lhs
));
3612 /* [expr.shift] The behavior is undefined if the right operand
3613 is negative, or greater than or equal to the length in bits
3614 of the promoted left operand. */
3615 if (tree_int_cst_sgn (rhs
) == -1)
3618 permerror (loc
, "right operand of shift expression %q+E is negative",
3619 build2_loc (loc
, code
, type
, lhs
, rhs
));
3620 return (!flag_permissive
|| ctx
->quiet
);
3622 if (compare_tree_int (rhs
, uprec
) >= 0)
3625 permerror (loc
, "right operand of shift expression %q+E is greater "
3626 "than or equal to the precision %wu of the left operand",
3627 build2_loc (loc
, code
, type
, lhs
, rhs
), uprec
);
3628 return (!flag_permissive
|| ctx
->quiet
);
3631 /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
3632 if E1 has a signed type and non-negative value, and E1x2^E2 is
3633 representable in the corresponding unsigned type of the result type,
3634 then that value, converted to the result type, is the resulting value;
3635 otherwise, the behavior is undefined.
3637 The value of E1 << E2 is the unique value congruent to E1 x 2^E2 modulo
3638 2^N, where N is the range exponent of the type of the result. */
3639 if (code
== LSHIFT_EXPR
3640 && !TYPE_OVERFLOW_WRAPS (lhstype
)
3641 && cxx_dialect
>= cxx11
3642 && cxx_dialect
< cxx20
)
3644 if (tree_int_cst_sgn (lhs
) == -1)
3648 "left operand of shift expression %q+E is negative",
3649 build2_loc (loc
, code
, type
, lhs
, rhs
));
3650 return (!flag_permissive
|| ctx
->quiet
);
3652 /* For signed x << y the following:
3653 (unsigned) x >> ((prec (lhs) - 1) - y)
3654 if > 1, is undefined. The right-hand side of this formula
3655 is the highest bit of the LHS that can be set (starting from 0),
3656 so that the shift doesn't overflow. We then right-shift the LHS
3657 to see whether any other bit is set making the original shift
3658 undefined -- the result is not representable in the corresponding
3660 tree t
= build_int_cst (unsigned_type_node
, uprec
- 1);
3661 t
= fold_build2 (MINUS_EXPR
, unsigned_type_node
, t
, rhs
);
3662 tree ulhs
= fold_convert (unsigned_type_for (lhstype
), lhs
);
3663 t
= fold_build2 (RSHIFT_EXPR
, TREE_TYPE (ulhs
), ulhs
, t
);
3664 if (tree_int_cst_lt (integer_one_node
, t
))
3667 permerror (loc
, "shift expression %q+E overflows",
3668 build2_loc (loc
, code
, type
, lhs
, rhs
));
3669 return (!flag_permissive
|| ctx
->quiet
);
3675 /* Subroutine of cxx_eval_constant_expression.
3676 Attempt to reduce the unary expression tree T to a compile time value.
3677 If successful, return the value. Otherwise issue a diagnostic
3678 and return error_mark_node. */
3681 cxx_eval_unary_expression (const constexpr_ctx
*ctx
, tree t
,
3683 bool *non_constant_p
, bool *overflow_p
)
3686 tree orig_arg
= TREE_OPERAND (t
, 0);
3687 tree arg
= cxx_eval_constant_expression (ctx
, orig_arg
, vc_prvalue
,
3688 non_constant_p
, overflow_p
);
3689 VERIFY_CONSTANT (arg
);
3690 location_t loc
= EXPR_LOCATION (t
);
3691 enum tree_code code
= TREE_CODE (t
);
3692 tree type
= TREE_TYPE (t
);
3693 r
= fold_unary_loc (loc
, code
, type
, arg
);
3696 if (arg
== orig_arg
)
3699 r
= build1_loc (loc
, code
, type
, arg
);
3701 VERIFY_CONSTANT (r
);
3705 /* Helper function for cxx_eval_binary_expression. Try to optimize
3706 original POINTER_PLUS_EXPR T, LHS p+ RHS, return NULL_TREE if the
3707 generic folding should be used. */
3710 cxx_fold_pointer_plus_expression (const constexpr_ctx
*ctx
, tree t
,
3711 tree lhs
, tree rhs
, bool *non_constant_p
,
3715 if (TREE_CODE (lhs
) != ADDR_EXPR
)
3718 lhs
= TREE_OPERAND (lhs
, 0);
3720 /* &A[i] p+ j => &A[i + j] */
3721 if (TREE_CODE (lhs
) == ARRAY_REF
3722 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == INTEGER_CST
3723 && TREE_CODE (rhs
) == INTEGER_CST
3724 && TYPE_SIZE_UNIT (TREE_TYPE (lhs
))
3725 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs
))) == INTEGER_CST
)
3727 tree orig_type
= TREE_TYPE (t
);
3728 location_t loc
= EXPR_LOCATION (t
);
3729 tree type
= TREE_TYPE (lhs
);
3731 t
= fold_convert_loc (loc
, ssizetype
, TREE_OPERAND (lhs
, 1));
3732 tree nelts
= array_type_nelts_top (TREE_TYPE (TREE_OPERAND (lhs
, 0)));
3733 nelts
= cxx_eval_constant_expression (ctx
, nelts
, vc_prvalue
,
3734 non_constant_p
, overflow_p
);
3735 if (*non_constant_p
)
3737 /* Don't fold an out-of-bound access. */
3738 if (!tree_int_cst_le (t
, nelts
))
3740 rhs
= cp_fold_convert (ssizetype
, rhs
);
3741 /* Don't fold if rhs can't be divided exactly by TYPE_SIZE_UNIT.
3742 constexpr int A[1]; ... (char *)&A[0] + 1 */
3743 if (!integer_zerop (fold_build2_loc (loc
, TRUNC_MOD_EXPR
, sizetype
,
3744 rhs
, TYPE_SIZE_UNIT (type
))))
3746 /* Make sure to treat the second operand of POINTER_PLUS_EXPR
3748 rhs
= fold_build2_loc (loc
, EXACT_DIV_EXPR
, ssizetype
, rhs
,
3749 TYPE_SIZE_UNIT (type
));
3750 t
= size_binop_loc (loc
, PLUS_EXPR
, rhs
, t
);
3751 t
= build4_loc (loc
, ARRAY_REF
, type
, TREE_OPERAND (lhs
, 0),
3752 t
, NULL_TREE
, NULL_TREE
);
3753 t
= cp_build_addr_expr (t
, tf_warning_or_error
);
3754 t
= cp_fold_convert (orig_type
, t
);
3755 return cxx_eval_constant_expression (ctx
, t
, vc_prvalue
,
3756 non_constant_p
, overflow_p
);
3762 /* Try to fold expressions like
3763 (struct S *) (&a[0].D.2378 + 12)
3765 &MEM <struct T> [(void *)&a + 12B]
3766 This is something normally done by gimple_fold_stmt_to_constant_1
3767 on GIMPLE, but is undesirable on GENERIC if we are e.g. going to
3768 dereference the address because some details are lost.
3769 For pointer comparisons we want such folding though so that
3770 match.pd address_compare optimization works. */
3773 cxx_maybe_fold_addr_pointer_plus (tree t
)
3775 while (CONVERT_EXPR_P (t
)
3776 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t
, 0))))
3777 t
= TREE_OPERAND (t
, 0);
3778 if (TREE_CODE (t
) != POINTER_PLUS_EXPR
)
3780 tree op0
= TREE_OPERAND (t
, 0);
3781 tree op1
= TREE_OPERAND (t
, 1);
3782 if (TREE_CODE (op1
) != INTEGER_CST
)
3784 while (CONVERT_EXPR_P (op0
)
3785 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0
, 0))))
3786 op0
= TREE_OPERAND (op0
, 0);
3787 if (TREE_CODE (op0
) != ADDR_EXPR
)
3789 op1
= fold_convert (ptr_type_node
, op1
);
3790 tree r
= fold_build2 (MEM_REF
, TREE_TYPE (TREE_TYPE (op0
)), op0
, op1
);
3791 return build1_loc (EXPR_LOCATION (t
), ADDR_EXPR
, TREE_TYPE (op0
), r
);
3794 /* Subroutine of cxx_eval_constant_expression.
3795 Like cxx_eval_unary_expression, except for binary expressions. */
3798 cxx_eval_binary_expression (const constexpr_ctx
*ctx
, tree t
,
3800 bool *non_constant_p
, bool *overflow_p
)
3803 tree orig_lhs
= TREE_OPERAND (t
, 0);
3804 tree orig_rhs
= TREE_OPERAND (t
, 1);
3806 lhs
= cxx_eval_constant_expression (ctx
, orig_lhs
, vc_prvalue
,
3807 non_constant_p
, overflow_p
);
3808 /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer
3810 if (*non_constant_p
)
3812 rhs
= cxx_eval_constant_expression (ctx
, orig_rhs
, vc_prvalue
,
3813 non_constant_p
, overflow_p
);
3814 if (*non_constant_p
)
3817 location_t loc
= EXPR_LOCATION (t
);
3818 enum tree_code code
= TREE_CODE (t
);
3819 tree type
= TREE_TYPE (t
);
3821 if (code
== EQ_EXPR
|| code
== NE_EXPR
)
3823 bool is_code_eq
= (code
== EQ_EXPR
);
3825 if (TREE_CODE (lhs
) == PTRMEM_CST
3826 && TREE_CODE (rhs
) == PTRMEM_CST
)
3828 tree lmem
= PTRMEM_CST_MEMBER (lhs
);
3829 tree rmem
= PTRMEM_CST_MEMBER (rhs
);
3831 if (TREE_CODE (lmem
) == TREE_CODE (rmem
)
3832 && TREE_CODE (lmem
) == FIELD_DECL
3833 && TREE_CODE (DECL_CONTEXT (lmem
)) == UNION_TYPE
3834 && same_type_p (DECL_CONTEXT (lmem
),
3835 DECL_CONTEXT (rmem
)))
3836 /* If both refer to (possibly different) members of the same union
3837 (12.3), they compare equal. */
3840 eq
= cp_tree_equal (lhs
, rhs
);
3841 r
= constant_boolean_node (eq
== is_code_eq
, type
);
3843 else if ((TREE_CODE (lhs
) == PTRMEM_CST
3844 || TREE_CODE (rhs
) == PTRMEM_CST
)
3845 && (null_member_pointer_value_p (lhs
)
3846 || null_member_pointer_value_p (rhs
)))
3847 r
= constant_boolean_node (!is_code_eq
, type
);
3848 else if (TREE_CODE (lhs
) == PTRMEM_CST
)
3849 lhs
= cplus_expand_constant (lhs
);
3850 else if (TREE_CODE (rhs
) == PTRMEM_CST
)
3851 rhs
= cplus_expand_constant (rhs
);
3854 && TREE_CODE_CLASS (code
) == tcc_comparison
3855 && POINTER_TYPE_P (TREE_TYPE (lhs
)))
3857 if (tree lhso
= cxx_maybe_fold_addr_pointer_plus (lhs
))
3858 lhs
= fold_convert (TREE_TYPE (lhs
), lhso
);
3859 if (tree rhso
= cxx_maybe_fold_addr_pointer_plus (rhs
))
3860 rhs
= fold_convert (TREE_TYPE (rhs
), rhso
);
3862 if (code
== POINTER_PLUS_EXPR
&& !*non_constant_p
3863 && integer_zerop (lhs
) && !integer_zerop (rhs
))
3866 error ("arithmetic involving a null pointer in %qE", lhs
);
3867 *non_constant_p
= true;
3870 else if (code
== POINTER_PLUS_EXPR
)
3871 r
= cxx_fold_pointer_plus_expression (ctx
, t
, lhs
, rhs
, non_constant_p
,
3873 else if (code
== SPACESHIP_EXPR
)
3875 r
= genericize_spaceship (loc
, type
, lhs
, rhs
);
3876 return cxx_eval_constant_expression (ctx
, r
, lval
, non_constant_p
,
3882 if (ctx
->manifestly_const_eval
== mce_true
3883 && (flag_constexpr_fp_except
3884 || TREE_CODE (type
) != REAL_TYPE
))
3886 auto ofcc
= make_temp_override (folding_cxx_constexpr
, true);
3887 r
= fold_binary_initializer_loc (loc
, code
, type
, lhs
, rhs
);
3890 r
= fold_binary_loc (loc
, code
, type
, lhs
, rhs
);
3894 && (code
== LSHIFT_EXPR
|| code
== RSHIFT_EXPR
)
3895 && TREE_CODE (lhs
) == INTEGER_CST
3896 && TREE_CODE (rhs
) == INTEGER_CST
3897 && wi::neg_p (wi::to_wide (rhs
)))
3899 /* For diagnostics and -fpermissive emulate previous behavior of
3900 handling shifts by negative amount. */
3901 tree nrhs
= const_unop (NEGATE_EXPR
, TREE_TYPE (rhs
), rhs
);
3903 r
= fold_binary_loc (loc
,
3904 code
== LSHIFT_EXPR
? RSHIFT_EXPR
: LSHIFT_EXPR
,
3910 if (lhs
== orig_lhs
&& rhs
== orig_rhs
)
3913 r
= build2_loc (loc
, code
, type
, lhs
, rhs
);
3915 else if (cxx_eval_check_shift_p (loc
, ctx
, code
, type
, lhs
, rhs
))
3916 *non_constant_p
= true;
3917 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
3918 a local array in a constexpr function. */
3919 bool ptr
= INDIRECT_TYPE_P (TREE_TYPE (lhs
));
3921 VERIFY_CONSTANT (r
);
3925 /* Subroutine of cxx_eval_constant_expression.
3926 Attempt to evaluate condition expressions. */
3929 cxx_eval_conditional_expression (const constexpr_ctx
*ctx
, tree t
,
3931 bool *non_constant_p
, bool *overflow_p
,
3934 tree val
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
3936 non_constant_p
, overflow_p
);
3937 VERIFY_CONSTANT (val
);
3938 if (TREE_CODE (t
) == IF_STMT
&& IF_STMT_CONSTEVAL_P (t
))
3940 /* Evaluate the condition as if it was
3941 if (__builtin_is_constant_evaluated ()), i.e. defer it if not
3942 ctx->manifestly_const_eval (as sometimes we try to constant evaluate
3943 without manifestly_const_eval even expressions or parts thereof which
3944 will later be manifestly const_eval evaluated), otherwise fold it to
3946 if (ctx
->manifestly_const_eval
== mce_unknown
)
3948 *non_constant_p
= true;
3951 val
= constant_boolean_node (ctx
->manifestly_const_eval
== mce_true
,
3954 /* Don't VERIFY_CONSTANT the other operands. */
3955 const bool zero_p
= integer_zerop (val
);
3957 val
= TREE_OPERAND (t
, 2);
3959 val
= TREE_OPERAND (t
, 1);
3960 if (TREE_CODE (t
) == IF_STMT
&& !val
)
3963 /* P2564: a subexpression of a manifestly constant-evaluated expression
3964 or conversion is an immediate function context. */
3965 if (ctx
->manifestly_const_eval
!= mce_true
3966 && !in_immediate_context ()
3967 && cp_fold_immediate (&TREE_OPERAND (t
, zero_p
? 1 : 2),
3968 ctx
->manifestly_const_eval
))
3970 *non_constant_p
= true;
3974 /* A TARGET_EXPR may be nested inside another TARGET_EXPR, but still
3975 serve as the initializer for the same object as the outer TARGET_EXPR,
3977 A a = true ? A{} : A{};
3978 so strip the inner TARGET_EXPR so we don't materialize a temporary. */
3979 if (TREE_CODE (val
) == TARGET_EXPR
)
3980 val
= TARGET_EXPR_INITIAL (val
);
3981 return cxx_eval_constant_expression (ctx
, val
, lval
, non_constant_p
,
3982 overflow_p
, jump_target
);
3985 /* Subroutine of cxx_eval_constant_expression.
3986 Attempt to evaluate vector condition expressions. Unlike
3987 cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal
3988 ternary arithmetics operation, where all 3 arguments have to be
3989 evaluated as constants and then folding computes the result from
3993 cxx_eval_vector_conditional_expression (const constexpr_ctx
*ctx
, tree t
,
3994 bool *non_constant_p
, bool *overflow_p
)
3996 tree arg1
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
3998 non_constant_p
, overflow_p
);
3999 VERIFY_CONSTANT (arg1
);
4000 tree arg2
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
4002 non_constant_p
, overflow_p
);
4003 VERIFY_CONSTANT (arg2
);
4004 tree arg3
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 2),
4006 non_constant_p
, overflow_p
);
4007 VERIFY_CONSTANT (arg3
);
4008 location_t loc
= EXPR_LOCATION (t
);
4009 tree type
= TREE_TYPE (t
);
4010 tree r
= fold_ternary_loc (loc
, VEC_COND_EXPR
, type
, arg1
, arg2
, arg3
);
4013 if (arg1
== TREE_OPERAND (t
, 0)
4014 && arg2
== TREE_OPERAND (t
, 1)
4015 && arg3
== TREE_OPERAND (t
, 2))
4018 r
= build3_loc (loc
, VEC_COND_EXPR
, type
, arg1
, arg2
, arg3
);
4020 VERIFY_CONSTANT (r
);
4024 /* Returns less than, equal to, or greater than zero if KEY is found to be
4025 less than, to match, or to be greater than the constructor_elt's INDEX. */
4028 array_index_cmp (tree key
, tree index
)
4030 gcc_assert (TREE_CODE (key
) == INTEGER_CST
);
4032 switch (TREE_CODE (index
))
4035 return tree_int_cst_compare (key
, index
);
4038 tree lo
= TREE_OPERAND (index
, 0);
4039 tree hi
= TREE_OPERAND (index
, 1);
4040 if (tree_int_cst_lt (key
, lo
))
4042 else if (tree_int_cst_lt (hi
, key
))
4052 /* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1
4053 if none. If INSERT is true, insert a matching element rather than fail. */
4055 static HOST_WIDE_INT
4056 find_array_ctor_elt (tree ary
, tree dindex
, bool insert
)
4058 if (tree_int_cst_sgn (dindex
) < 0)
4061 unsigned HOST_WIDE_INT i
= tree_to_uhwi (dindex
);
4062 vec
<constructor_elt
, va_gc
> *elts
= CONSTRUCTOR_ELTS (ary
);
4063 unsigned HOST_WIDE_INT len
= vec_safe_length (elts
);
4065 unsigned HOST_WIDE_INT end
= len
;
4066 unsigned HOST_WIDE_INT begin
= 0;
4068 /* If the last element of the CONSTRUCTOR has its own index, we can assume
4069 that the same is true of the other elements and index directly. */
4072 tree cindex
= (*elts
)[end
- 1].index
;
4073 if (cindex
== NULL_TREE
)
4075 /* Verify that if the last index is missing, all indexes
4078 for (unsigned int j
= 0; j
< len
- 1; ++j
)
4079 gcc_assert ((*elts
)[j
].index
== NULL_TREE
);
4086 /* If the element is to be added right at the end,
4087 make sure it is added with cleared index too. */
4090 /* Otherwise, in order not to break the assumption
4091 that CONSTRUCTOR either has all indexes or none,
4092 we need to add indexes to all elements. */
4093 for (unsigned int j
= 0; j
< len
; ++j
)
4094 (*elts
)[j
].index
= build_int_cst (TREE_TYPE (dindex
), j
);
4097 else if (TREE_CODE (cindex
) == INTEGER_CST
4098 && compare_tree_int (cindex
, end
- 1) == 0)
4107 /* Otherwise, find a matching index by means of a binary search. */
4108 while (begin
!= end
)
4110 unsigned HOST_WIDE_INT middle
= (begin
+ end
) / 2;
4111 constructor_elt
&elt
= (*elts
)[middle
];
4112 tree idx
= elt
.index
;
4114 int cmp
= array_index_cmp (dindex
, idx
);
4121 if (insert
&& TREE_CODE (idx
) == RANGE_EXPR
)
4123 /* We need to split the range. */
4125 tree lo
= TREE_OPERAND (idx
, 0);
4126 tree hi
= TREE_OPERAND (idx
, 1);
4127 tree value
= elt
.value
;
4128 dindex
= fold_convert (sizetype
, dindex
);
4129 if (tree_int_cst_lt (lo
, dindex
))
4131 /* There are still some lower elts; shorten the range. */
4132 tree new_hi
= int_const_binop (MINUS_EXPR
, dindex
,
4134 if (tree_int_cst_equal (lo
, new_hi
))
4135 /* Only one element left, no longer a range. */
4138 TREE_OPERAND (idx
, 1) = new_hi
;
4139 /* Append the element we want to insert. */
4142 e
.value
= unshare_constructor (value
);
4143 vec_safe_insert (CONSTRUCTOR_ELTS (ary
), middle
, e
);
4146 /* No lower elts, the range elt is now ours. */
4149 if (tree_int_cst_lt (dindex
, hi
))
4151 /* There are still some higher elts; append a range. */
4152 tree new_lo
= int_const_binop (PLUS_EXPR
, dindex
,
4154 if (tree_int_cst_equal (new_lo
, hi
))
4157 e
.index
= build2 (RANGE_EXPR
, sizetype
, new_lo
, hi
);
4158 e
.value
= unshare_constructor (value
);
4159 vec_safe_insert (CONSTRUCTOR_ELTS (ary
), middle
+ 1, e
);
4168 constructor_elt e
= { dindex
, NULL_TREE
};
4169 vec_safe_insert (CONSTRUCTOR_ELTS (ary
), end
, e
);
4176 /* Return a pointer to the constructor_elt of CTOR which matches INDEX. If no
4177 matching constructor_elt exists, then add one to CTOR.
4179 As an optimization, if POS_HINT is non-negative then it is used as a guess
4180 for the (integer) index of the matching constructor_elt within CTOR. */
4182 static constructor_elt
*
4183 get_or_insert_ctor_field (tree ctor
, tree index
, int pos_hint
= -1)
4185 /* Check the hint first. */
4186 if (pos_hint
>= 0 && (unsigned)pos_hint
< CONSTRUCTOR_NELTS (ctor
)
4187 && CONSTRUCTOR_ELT (ctor
, pos_hint
)->index
== index
)
4188 return CONSTRUCTOR_ELT (ctor
, pos_hint
);
4190 tree type
= TREE_TYPE (ctor
);
4191 if (TREE_CODE (type
) == VECTOR_TYPE
&& index
== NULL_TREE
)
4193 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (ctor
), index
, NULL_TREE
);
4194 return &CONSTRUCTOR_ELTS (ctor
)->last();
4196 else if (TREE_CODE (type
) == ARRAY_TYPE
|| TREE_CODE (type
) == VECTOR_TYPE
)
4198 if (TREE_CODE (index
) == RANGE_EXPR
)
4200 /* Support for RANGE_EXPR index lookups is currently limited to
4201 accessing an existing element via POS_HINT, or appending a new
4202 element to the end of CTOR. ??? Support for other access
4203 patterns may also be needed. */
4204 vec
<constructor_elt
, va_gc
> *elts
= CONSTRUCTOR_ELTS (ctor
);
4205 if (vec_safe_length (elts
))
4207 tree lo
= TREE_OPERAND (index
, 0);
4208 gcc_assert (array_index_cmp (elts
->last().index
, lo
) < 0);
4210 CONSTRUCTOR_APPEND_ELT (elts
, index
, NULL_TREE
);
4211 return &elts
->last();
4214 HOST_WIDE_INT i
= find_array_ctor_elt (ctor
, index
, /*insert*/true);
4215 gcc_assert (i
>= 0);
4216 constructor_elt
*cep
= CONSTRUCTOR_ELT (ctor
, i
);
4217 gcc_assert (cep
->index
== NULL_TREE
4218 || TREE_CODE (cep
->index
) != RANGE_EXPR
);
4223 gcc_assert (TREE_CODE (index
) == FIELD_DECL
4224 && (same_type_ignoring_top_level_qualifiers_p
4225 (DECL_CONTEXT (index
), TREE_TYPE (ctor
))));
4227 /* We must keep the CONSTRUCTOR's ELTS in FIELD order.
4228 Usually we meet initializers in that order, but it is
4229 possible for base types to be placed not in program
4231 tree fields
= TYPE_FIELDS (DECL_CONTEXT (index
));
4232 unsigned HOST_WIDE_INT idx
= 0;
4233 constructor_elt
*cep
= NULL
;
4235 /* Check if we're changing the active member of a union. */
4236 if (TREE_CODE (type
) == UNION_TYPE
&& CONSTRUCTOR_NELTS (ctor
)
4237 && CONSTRUCTOR_ELT (ctor
, 0)->index
!= index
)
4238 vec_safe_truncate (CONSTRUCTOR_ELTS (ctor
), 0);
4239 /* If the bit offset of INDEX is larger than that of the last
4240 constructor_elt, then we can just immediately append a new
4241 constructor_elt to the end of CTOR. */
4242 else if (CONSTRUCTOR_NELTS (ctor
)
4243 && tree_int_cst_compare (bit_position (index
),
4244 bit_position (CONSTRUCTOR_ELTS (ctor
)
4245 ->last().index
)) > 0)
4247 idx
= CONSTRUCTOR_NELTS (ctor
);
4251 /* Otherwise, we need to iterate over CTOR to find or insert INDEX
4254 for (; vec_safe_iterate (CONSTRUCTOR_ELTS (ctor
), idx
, &cep
);
4255 idx
++, fields
= DECL_CHAIN (fields
))
4257 if (index
== cep
->index
)
4260 /* The field we're initializing must be on the field
4261 list. Look to see if it is present before the
4262 field the current ELT initializes. */
4263 for (; fields
!= cep
->index
; fields
= DECL_CHAIN (fields
))
4264 if (index
== fields
)
4267 /* We fell off the end of the CONSTRUCTOR, so insert a new
4268 entry at the end. */
4272 constructor_elt ce
= { index
, NULL_TREE
};
4274 vec_safe_insert (CONSTRUCTOR_ELTS (ctor
), idx
, ce
);
4275 cep
= CONSTRUCTOR_ELT (ctor
, idx
);
4283 /* Under the control of CTX, issue a detailed diagnostic for
4284 an out-of-bounds subscript INDEX into the expression ARRAY. */
4287 diag_array_subscript (location_t loc
, const constexpr_ctx
*ctx
, tree array
, tree index
)
4291 tree arraytype
= TREE_TYPE (array
);
4293 /* Convert the unsigned array subscript to a signed integer to avoid
4294 printing huge numbers for small negative values. */
4295 tree sidx
= fold_convert (ssizetype
, index
);
4296 STRIP_ANY_LOCATION_WRAPPER (array
);
4299 auto_diagnostic_group d
;
4300 if (TYPE_DOMAIN (arraytype
))
4301 error_at (loc
, "array subscript value %qE is outside the bounds "
4302 "of array %qD of type %qT", sidx
, array
, arraytype
);
4304 error_at (loc
, "nonzero array subscript %qE is used with array %qD of "
4305 "type %qT with unknown bounds", sidx
, array
, arraytype
);
4306 inform (DECL_SOURCE_LOCATION (array
), "declared here");
4308 else if (TYPE_DOMAIN (arraytype
))
4309 error_at (loc
, "array subscript value %qE is outside the bounds "
4310 "of array type %qT", sidx
, arraytype
);
4312 error_at (loc
, "nonzero array subscript %qE is used with array of type %qT "
4313 "with unknown bounds", sidx
, arraytype
);
4317 /* Return the number of elements for TYPE (which is an ARRAY_TYPE or
4321 get_array_or_vector_nelts (const constexpr_ctx
*ctx
, tree type
,
4322 bool *non_constant_p
, bool *overflow_p
)
4325 if (TREE_CODE (type
) == ARRAY_TYPE
)
4327 if (TYPE_DOMAIN (type
))
4328 nelts
= array_type_nelts_top (type
);
4330 nelts
= size_zero_node
;
4332 else if (VECTOR_TYPE_P (type
))
4333 nelts
= size_int (TYPE_VECTOR_SUBPARTS (type
));
4337 /* For VLAs, the number of elements won't be an integer constant. */
4338 nelts
= cxx_eval_constant_expression (ctx
, nelts
, vc_prvalue
,
4339 non_constant_p
, overflow_p
);
4343 /* Extract element INDEX consisting of CHARS_PER_ELT chars from
4344 STRING_CST STRING. */
4347 extract_string_elt (tree string
, unsigned chars_per_elt
, unsigned index
)
4349 tree type
= cv_unqualified (TREE_TYPE (TREE_TYPE (string
)));
4352 if (chars_per_elt
== 1)
4353 r
= build_int_cst (type
, TREE_STRING_POINTER (string
)[index
]);
4356 const unsigned char *ptr
4357 = ((const unsigned char *)TREE_STRING_POINTER (string
)
4358 + index
* chars_per_elt
);
4359 r
= native_interpret_expr (type
, ptr
, chars_per_elt
);
4364 /* Subroutine of cxx_eval_array_reference. T is an ARRAY_REF; evaluate the
4365 subscript, diagnose any problems with it, and return the result. */
4368 eval_and_check_array_index (const constexpr_ctx
*ctx
,
4369 tree t
, bool allow_one_past
,
4370 bool *non_constant_p
, bool *overflow_p
)
4372 location_t loc
= cp_expr_loc_or_input_loc (t
);
4373 tree ary
= TREE_OPERAND (t
, 0);
4374 t
= TREE_OPERAND (t
, 1);
4375 tree index
= cxx_eval_constant_expression (ctx
, t
, vc_prvalue
,
4376 non_constant_p
, overflow_p
);
4377 VERIFY_CONSTANT (index
);
4379 if (!tree_fits_shwi_p (index
)
4380 || tree_int_cst_sgn (index
) < 0)
4382 diag_array_subscript (loc
, ctx
, ary
, index
);
4383 *non_constant_p
= true;
4387 tree nelts
= get_array_or_vector_nelts (ctx
, TREE_TYPE (ary
), non_constant_p
,
4389 VERIFY_CONSTANT (nelts
);
4391 ? !tree_int_cst_le (index
, nelts
)
4392 : !tree_int_cst_lt (index
, nelts
))
4394 diag_array_subscript (loc
, ctx
, ary
, index
);
4395 *non_constant_p
= true;
4402 /* Subroutine of cxx_eval_constant_expression.
4403 Attempt to reduce a reference to an array slot. */
4406 cxx_eval_array_reference (const constexpr_ctx
*ctx
, tree t
,
4408 bool *non_constant_p
, bool *overflow_p
)
4410 tree oldary
= TREE_OPERAND (t
, 0);
4411 tree ary
= cxx_eval_constant_expression (ctx
, oldary
,
4413 non_constant_p
, overflow_p
);
4414 if (*non_constant_p
)
4417 && TREE_CODE (ary
) == VIEW_CONVERT_EXPR
4418 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary
, 0)))
4419 && TREE_TYPE (t
) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary
, 0))))
4420 ary
= TREE_OPERAND (ary
, 0);
4422 tree oldidx
= TREE_OPERAND (t
, 1);
4423 tree index
= eval_and_check_array_index (ctx
, t
, lval
,
4424 non_constant_p
, overflow_p
);
4425 if (*non_constant_p
)
4428 if (lval
&& ary
== oldary
&& index
== oldidx
)
4430 else if (lval
== vc_discard
)
4433 return build4 (ARRAY_REF
, TREE_TYPE (t
), ary
, index
, NULL
, NULL
);
4435 unsigned len
= 0, elem_nchars
= 1;
4436 tree elem_type
= TREE_TYPE (TREE_TYPE (ary
));
4437 if (TREE_CODE (ary
) == CONSTRUCTOR
)
4438 len
= CONSTRUCTOR_NELTS (ary
);
4439 else if (TREE_CODE (ary
) == STRING_CST
)
4441 elem_nchars
= (TYPE_PRECISION (elem_type
)
4442 / TYPE_PRECISION (char_type_node
));
4443 len
= (unsigned) TREE_STRING_LENGTH (ary
) / elem_nchars
;
4445 else if (TREE_CODE (ary
) == VECTOR_CST
)
4446 /* We don't create variable-length VECTOR_CSTs. */
4447 len
= VECTOR_CST_NELTS (ary
).to_constant ();
4450 /* We can't do anything with other tree codes, so use
4451 VERIFY_CONSTANT to complain and fail. */
4452 VERIFY_CONSTANT (ary
);
4457 HOST_WIDE_INT i
= 0;
4458 if (TREE_CODE (ary
) == CONSTRUCTOR
)
4460 HOST_WIDE_INT ix
= find_array_ctor_elt (ary
, index
);
4467 i
= tree_to_shwi (index
);
4474 if (TREE_CODE (ary
) == CONSTRUCTOR
)
4475 r
= (*CONSTRUCTOR_ELTS (ary
))[i
].value
;
4476 else if (TREE_CODE (ary
) == VECTOR_CST
)
4477 r
= VECTOR_CST_ELT (ary
, i
);
4479 r
= extract_string_elt (ary
, elem_nchars
, i
);
4482 /* Don't VERIFY_CONSTANT here. */
4485 /* Otherwise the element doesn't have a value yet. */
4490 if (is_really_empty_class (elem_type
, /*ignore_vptr*/false))
4491 return build_constructor (elem_type
, NULL
);
4493 if (TREE_CODE (ary
) == CONSTRUCTOR
4494 && CONSTRUCTOR_NO_CLEARING (ary
))
4496 /* 'ary' is part of the aggregate initializer we're currently
4497 building; if there's no initializer for this element yet,
4500 error ("accessing uninitialized array element");
4501 *non_constant_p
= true;
4505 /* If it's within the array bounds but doesn't have an explicit
4506 initializer, it's initialized from {}. But use build_value_init
4507 directly for non-aggregates to avoid creating a garbage CONSTRUCTOR. */
4509 constexpr_ctx new_ctx
;
4510 if (CP_AGGREGATE_TYPE_P (elem_type
))
4512 tree empty_ctor
= build_constructor (init_list_type_node
, NULL
);
4513 val
= digest_init (elem_type
, empty_ctor
, tf_warning_or_error
);
4516 val
= build_value_init (elem_type
, tf_warning_or_error
);
4518 /* Create a new constructor only if we don't already have a suitable one. */
4519 const bool new_ctor
= (!SCALAR_TYPE_P (elem_type
)
4521 || !same_type_ignoring_top_level_qualifiers_p
4522 (elem_type
, TREE_TYPE (ctx
->ctor
))));
4526 /* We clear the object here. We used to replace it with T, but that
4527 caused problems (101371, 108158); and anyway, T is the initializer,
4528 not the target object. */
4529 new_ctx
.object
= NULL_TREE
;
4530 new_ctx
.ctor
= build_constructor (elem_type
, NULL
);
4533 t
= cxx_eval_constant_expression (ctx
, val
, lval
, non_constant_p
,
4535 if (new_ctor
&& t
!= ctx
->ctor
)
4536 free_constructor (ctx
->ctor
);
4540 /* Subroutine of cxx_eval_constant_expression.
4541 Attempt to reduce a field access of a value of class type. */
4544 cxx_eval_component_reference (const constexpr_ctx
*ctx
, tree t
,
4546 bool *non_constant_p
, bool *overflow_p
)
4548 unsigned HOST_WIDE_INT i
;
4551 tree part
= TREE_OPERAND (t
, 1);
4552 tree orig_whole
= TREE_OPERAND (t
, 0);
4553 tree whole
= cxx_eval_constant_expression (ctx
, orig_whole
,
4555 non_constant_p
, overflow_p
);
4556 if (*non_constant_p
)
4558 if (INDIRECT_REF_P (whole
)
4559 && integer_zerop (TREE_OPERAND (whole
, 0)))
4562 error ("dereferencing a null pointer in %qE", orig_whole
);
4563 *non_constant_p
= true;
4567 if (TREE_CODE (whole
) == PTRMEM_CST
)
4568 whole
= cplus_expand_constant (whole
);
4569 if (whole
== orig_whole
)
4571 if (lval
== vc_discard
)
4574 return fold_build3 (COMPONENT_REF
, TREE_TYPE (t
),
4575 whole
, part
, NULL_TREE
);
4576 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
4578 if (TREE_CODE (whole
) != CONSTRUCTOR
)
4581 error ("%qE is not a constant expression", orig_whole
);
4582 *non_constant_p
= true;
4585 if ((cxx_dialect
< cxx14
|| CONSTRUCTOR_MUTABLE_POISON (whole
))
4586 && DECL_MUTABLE_P (part
))
4589 error ("mutable %qD is not usable in a constant expression", part
);
4590 *non_constant_p
= true;
4594 bool pmf
= TYPE_PTRMEMFUNC_P (TREE_TYPE (whole
));
4595 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole
), i
, field
, value
)
4597 /* Use name match for PMF fields, as a variant will have a
4598 different FIELD_DECL with a different type. */
4599 if (pmf
? DECL_NAME (field
) == DECL_NAME (part
)
4604 STRIP_ANY_LOCATION_WRAPPER (value
);
4608 /* We're in the middle of initializing it. */
4612 if (TREE_CODE (TREE_TYPE (whole
)) == UNION_TYPE
)
4614 if (CONSTRUCTOR_NELTS (whole
) > 0)
4616 /* DR 1188 says we don't have to deal with this. */
4619 constructor_elt
*cep
= CONSTRUCTOR_ELT (whole
, 0);
4620 if (cep
->value
== NULL_TREE
)
4621 error ("accessing uninitialized member %qD", part
);
4623 error ("accessing %qD member instead of initialized %qD member "
4624 "in constant expression", part
, cep
->index
);
4626 *non_constant_p
= true;
4629 else if (!CONSTRUCTOR_NO_CLEARING (whole
))
4631 /* Value-initialized union, check if looking at the first member. */
4632 tree first
= next_aggregate_field (TYPE_FIELDS (TREE_TYPE (whole
)));
4636 error ("accessing %qD member instead of initialized %qD "
4637 "member in constant expression", part
, first
);
4638 *non_constant_p
= true;
4644 /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty
4645 classes never get represented; throw together a value now. */
4646 if (is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
4647 return build_constructor (TREE_TYPE (t
), NULL
);
4649 gcc_assert (DECL_CONTEXT (part
) == TYPE_MAIN_VARIANT (TREE_TYPE (whole
)));
4651 if (CONSTRUCTOR_NO_CLEARING (whole
))
4653 /* 'whole' is part of the aggregate initializer we're currently
4654 building; if there's no initializer for this member yet, that's an
4657 error ("accessing uninitialized member %qD", part
);
4658 *non_constant_p
= true;
4662 /* If there's no explicit init for this field, it's value-initialized. */
4663 value
= build_value_init (TREE_TYPE (t
), tf_warning_or_error
);
4664 return cxx_eval_constant_expression (ctx
, value
,
4666 non_constant_p
, overflow_p
);
4669 /* Subroutine of cxx_eval_constant_expression.
4670 Attempt to reduce a field access of a value of class type that is
4671 expressed as a BIT_FIELD_REF. */
4674 cxx_eval_bit_field_ref (const constexpr_ctx
*ctx
, tree t
,
4676 bool *non_constant_p
, bool *overflow_p
)
4678 tree orig_whole
= TREE_OPERAND (t
, 0);
4679 tree retval
, fldval
, utype
, mask
;
4680 bool fld_seen
= false;
4681 HOST_WIDE_INT istart
, isize
;
4682 tree whole
= cxx_eval_constant_expression (ctx
, orig_whole
,
4684 non_constant_p
, overflow_p
);
4685 tree start
, field
, value
;
4686 unsigned HOST_WIDE_INT i
;
4688 if (whole
== orig_whole
)
4690 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
4692 if (!*non_constant_p
4693 && TREE_CODE (whole
) != VECTOR_CST
4694 && TREE_CODE (whole
) != CONSTRUCTOR
)
4697 error ("%qE is not a constant expression", orig_whole
);
4698 *non_constant_p
= true;
4700 if (*non_constant_p
)
4703 if (TREE_CODE (whole
) == VECTOR_CST
|| !INTEGRAL_TYPE_P (TREE_TYPE (t
)))
4705 if (tree r
= fold_ternary (BIT_FIELD_REF
, TREE_TYPE (t
), whole
,
4706 TREE_OPERAND (t
, 1), TREE_OPERAND (t
, 2)))
4709 error ("%qE is not a constant expression", orig_whole
);
4710 *non_constant_p
= true;
4714 start
= TREE_OPERAND (t
, 2);
4715 istart
= tree_to_shwi (start
);
4716 isize
= tree_to_shwi (TREE_OPERAND (t
, 1));
4717 utype
= TREE_TYPE (t
);
4718 if (!TYPE_UNSIGNED (utype
))
4719 utype
= build_nonstandard_integer_type (TYPE_PRECISION (utype
), 1);
4720 retval
= build_int_cst (utype
, 0);
4721 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole
), i
, field
, value
)
4723 tree bitpos
= bit_position (field
);
4724 STRIP_ANY_LOCATION_WRAPPER (value
);
4725 if (bitpos
== start
&& DECL_SIZE (field
) == TREE_OPERAND (t
, 1))
4727 if (TREE_CODE (TREE_TYPE (field
)) == INTEGER_TYPE
4728 && TREE_CODE (value
) == INTEGER_CST
4729 && tree_fits_shwi_p (bitpos
)
4730 && tree_fits_shwi_p (DECL_SIZE (field
)))
4732 HOST_WIDE_INT bit
= tree_to_shwi (bitpos
);
4733 HOST_WIDE_INT sz
= tree_to_shwi (DECL_SIZE (field
));
4734 HOST_WIDE_INT shift
;
4735 if (bit
>= istart
&& bit
+ sz
<= istart
+ isize
)
4737 fldval
= fold_convert (utype
, value
);
4738 mask
= build_int_cst_type (utype
, -1);
4739 mask
= fold_build2 (LSHIFT_EXPR
, utype
, mask
,
4740 size_int (TYPE_PRECISION (utype
) - sz
));
4741 mask
= fold_build2 (RSHIFT_EXPR
, utype
, mask
,
4742 size_int (TYPE_PRECISION (utype
) - sz
));
4743 fldval
= fold_build2 (BIT_AND_EXPR
, utype
, fldval
, mask
);
4744 shift
= bit
- istart
;
4745 if (BYTES_BIG_ENDIAN
)
4746 shift
= TYPE_PRECISION (utype
) - shift
- sz
;
4747 fldval
= fold_build2 (LSHIFT_EXPR
, utype
, fldval
,
4749 retval
= fold_build2 (BIT_IOR_EXPR
, utype
, retval
, fldval
);
4755 return fold_convert (TREE_TYPE (t
), retval
);
4757 return error_mark_node
;
4760 /* Helper for cxx_eval_bit_cast.
4761 Check [bit.cast]/3 rules, bit_cast is constexpr only if the To and From
4762 types and types of all subobjects have is_union_v<T>, is_pointer_v<T>,
4763 is_member_pointer_v<T>, is_volatile_v<T> false and has no non-static
4764 data members of reference type. */
4767 check_bit_cast_type (const constexpr_ctx
*ctx
, location_t loc
, tree type
,
4770 if (TREE_CODE (type
) == UNION_TYPE
)
4774 if (type
== orig_type
)
4775 error_at (loc
, "%qs is not a constant expression because %qT is "
4776 "a union type", "__builtin_bit_cast", type
);
4778 error_at (loc
, "%qs is not a constant expression because %qT "
4779 "contains a union type", "__builtin_bit_cast",
4784 if (TREE_CODE (type
) == POINTER_TYPE
)
4788 if (type
== orig_type
)
4789 error_at (loc
, "%qs is not a constant expression because %qT is "
4790 "a pointer type", "__builtin_bit_cast", type
);
4792 error_at (loc
, "%qs is not a constant expression because %qT "
4793 "contains a pointer type", "__builtin_bit_cast",
4798 if (TREE_CODE (type
) == REFERENCE_TYPE
)
4802 if (type
== orig_type
)
4803 error_at (loc
, "%qs is not a constant expression because %qT is "
4804 "a reference type", "__builtin_bit_cast", type
);
4806 error_at (loc
, "%qs is not a constant expression because %qT "
4807 "contains a reference type", "__builtin_bit_cast",
4812 if (TYPE_PTRMEM_P (type
))
4816 if (type
== orig_type
)
4817 error_at (loc
, "%qs is not a constant expression because %qT is "
4818 "a pointer to member type", "__builtin_bit_cast",
4821 error_at (loc
, "%qs is not a constant expression because %qT "
4822 "contains a pointer to member type",
4823 "__builtin_bit_cast", orig_type
);
4827 if (TYPE_VOLATILE (type
))
4831 if (type
== orig_type
)
4832 error_at (loc
, "%qs is not a constant expression because %qT is "
4833 "volatile", "__builtin_bit_cast", type
);
4835 error_at (loc
, "%qs is not a constant expression because %qT "
4836 "contains a volatile subobject",
4837 "__builtin_bit_cast", orig_type
);
4841 if (TREE_CODE (type
) == RECORD_TYPE
)
4842 for (tree field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
4843 if (TREE_CODE (field
) == FIELD_DECL
4844 && check_bit_cast_type (ctx
, loc
, TREE_TYPE (field
), orig_type
))
4849 /* Helper function for cxx_eval_bit_cast. For unsigned char or
4850 std::byte members of CONSTRUCTOR (recursively) if they contain
4851 some indeterminate bits (as set in MASK), remove the ctor elts,
4852 mark the CONSTRUCTOR as CONSTRUCTOR_NO_CLEARING and clear the
4856 clear_uchar_or_std_byte_in_mask (location_t loc
, tree t
, unsigned char *mask
)
4858 if (TREE_CODE (t
) != CONSTRUCTOR
)
4863 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t
), i
, index
, value
)
4865 tree type
= TREE_TYPE (value
);
4866 if (TREE_CODE (TREE_TYPE (t
)) != ARRAY_TYPE
4867 && DECL_BIT_FIELD_TYPE (index
) != NULL_TREE
)
4869 if (is_byte_access_type_not_plain_char (DECL_BIT_FIELD_TYPE (index
)))
4871 HOST_WIDE_INT fldsz
= TYPE_PRECISION (TREE_TYPE (index
));
4872 gcc_assert (fldsz
!= 0);
4873 HOST_WIDE_INT pos
= int_byte_position (index
);
4875 = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (index
));
4876 bpos
%= BITS_PER_UNIT
;
4878 = ROUND_UP (bpos
+ fldsz
, BITS_PER_UNIT
) / BITS_PER_UNIT
;
4879 gcc_assert (end
== 1 || end
== 2);
4880 unsigned char *p
= mask
+ pos
;
4881 unsigned char mask_save
[2];
4882 mask_save
[0] = mask
[pos
];
4883 mask_save
[1] = end
== 2 ? mask
[pos
+ 1] : 0;
4884 if (BYTES_BIG_ENDIAN
!= WORDS_BIG_ENDIAN
)
4885 sorry_at (loc
, "PDP11 bit-field handling unsupported"
4886 " in %qs", "__builtin_bit_cast");
4887 else if (BYTES_BIG_ENDIAN
)
4890 if (bpos
+ fldsz
<= BITS_PER_UNIT
)
4891 *p
&= ~(((1 << fldsz
) - 1)
4892 << (BITS_PER_UNIT
- bpos
- fldsz
));
4896 *p
&= ~(((1U << BITS_PER_UNIT
) - 1) >> bpos
);
4898 fldsz
-= BITS_PER_UNIT
- bpos
;
4899 gcc_assert (fldsz
&& fldsz
< BITS_PER_UNIT
);
4900 *p
&= ((1U << BITS_PER_UNIT
) - 1) >> fldsz
;
4905 /* Little endian. */
4906 if (bpos
+ fldsz
<= BITS_PER_UNIT
)
4907 *p
&= ~(((1 << fldsz
) - 1) << bpos
);
4911 *p
&= ~(((1 << BITS_PER_UNIT
) - 1) << bpos
);
4913 fldsz
-= BITS_PER_UNIT
- bpos
;
4914 gcc_assert (fldsz
&& fldsz
< BITS_PER_UNIT
);
4915 *p
&= ~((1 << fldsz
) - 1);
4918 if (mask_save
[0] != mask
[pos
]
4919 || (end
== 2 && mask_save
[1] != mask
[pos
+ 1]))
4921 CONSTRUCTOR_NO_CLEARING (t
) = 1;
4926 else if (is_byte_access_type_not_plain_char (type
))
4929 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
4930 pos
= tree_to_shwi (index
);
4932 pos
= int_byte_position (index
);
4935 CONSTRUCTOR_NO_CLEARING (t
) = 1;
4940 if (TREE_CODE (value
) == CONSTRUCTOR
)
4943 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
4944 pos
= tree_to_shwi (index
)
4945 * tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t
))));
4947 pos
= int_byte_position (index
);
4948 clear_uchar_or_std_byte_in_mask (loc
, value
, mask
+ pos
);
4952 CONSTRUCTOR_ELT (t
, j
)->index
= index
;
4953 CONSTRUCTOR_ELT (t
, j
)->value
= value
;
4957 if (CONSTRUCTOR_NELTS (t
) != j
)
4958 vec_safe_truncate (CONSTRUCTOR_ELTS (t
), j
);
4961 /* Subroutine of cxx_eval_constant_expression.
4962 Attempt to evaluate a BIT_CAST_EXPR. */
4965 cxx_eval_bit_cast (const constexpr_ctx
*ctx
, tree t
, bool *non_constant_p
,
4968 if (check_bit_cast_type (ctx
, EXPR_LOCATION (t
), TREE_TYPE (t
),
4970 || check_bit_cast_type (ctx
, cp_expr_loc_or_loc (TREE_OPERAND (t
, 0),
4972 TREE_TYPE (TREE_OPERAND (t
, 0)),
4973 TREE_TYPE (TREE_OPERAND (t
, 0))))
4975 *non_constant_p
= true;
4979 tree op
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), vc_prvalue
,
4980 non_constant_p
, overflow_p
);
4981 if (*non_constant_p
)
4984 location_t loc
= EXPR_LOCATION (t
);
4985 if (BITS_PER_UNIT
!= 8 || CHAR_BIT
!= 8)
4988 sorry_at (loc
, "%qs cannot be constant evaluated on the target",
4989 "__builtin_bit_cast");
4990 *non_constant_p
= true;
4994 if (!tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (t
))))
4997 sorry_at (loc
, "%qs cannot be constant evaluated because the "
4998 "type is too large", "__builtin_bit_cast");
4999 *non_constant_p
= true;
5003 HOST_WIDE_INT len
= tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (t
)));
5004 if (len
< 0 || (int) len
!= len
)
5007 sorry_at (loc
, "%qs cannot be constant evaluated because the "
5008 "type is too large", "__builtin_bit_cast");
5009 *non_constant_p
= true;
5013 unsigned char buf
[64];
5014 unsigned char *ptr
, *mask
;
5015 size_t alen
= (size_t) len
* 2;
5016 if (alen
<= sizeof (buf
))
5019 ptr
= XNEWVEC (unsigned char, alen
);
5020 mask
= ptr
+ (size_t) len
;
5021 /* At the beginning consider everything indeterminate. */
5022 memset (mask
, ~0, (size_t) len
);
5024 if (native_encode_initializer (op
, ptr
, len
, 0, mask
) != len
)
5027 sorry_at (loc
, "%qs cannot be constant evaluated because the "
5028 "argument cannot be encoded", "__builtin_bit_cast");
5029 *non_constant_p
= true;
5036 if (can_native_interpret_type_p (TREE_TYPE (t
)))
5038 r
= native_interpret_expr (TREE_TYPE (t
), ptr
, len
);
5039 if (is_byte_access_type_not_plain_char (TREE_TYPE (t
)))
5041 gcc_assert (len
== 1);
5044 memset (mask
, 0, len
);
5045 r
= build_constructor (TREE_TYPE (r
), NULL
);
5046 CONSTRUCTOR_NO_CLEARING (r
) = 1;
5050 else if (TREE_CODE (TREE_TYPE (t
)) == RECORD_TYPE
)
5052 r
= native_interpret_aggregate (TREE_TYPE (t
), ptr
, 0, len
);
5055 clear_type_padding_in_mask (TREE_TYPE (t
), mask
);
5056 clear_uchar_or_std_byte_in_mask (loc
, r
, mask
);
5059 tree e
= cxx_eval_bare_aggregate (ctx
, r
, vc_prvalue
,
5060 non_constant_p
, overflow_p
);
5061 gcc_checking_assert (e
== r
);
5069 for (int i
= 0; i
< len
; i
++)
5073 error_at (loc
, "%qs accessing uninitialized byte at offset %d",
5074 "__builtin_bit_cast", i
);
5075 *non_constant_p
= true;
5085 sorry_at (loc
, "%qs cannot be constant evaluated because the "
5086 "argument cannot be interpreted", "__builtin_bit_cast");
5087 *non_constant_p
= true;
5093 /* Subroutine of cxx_eval_constant_expression.
5094 Evaluate a short-circuited logical expression T in the context
5095 of a given constexpr CALL. BAILOUT_VALUE is the value for
5096 early return. CONTINUE_VALUE is used here purely for
5097 sanity check purposes. */
5100 cxx_eval_logical_expression (const constexpr_ctx
*ctx
, tree t
,
5101 tree bailout_value
, tree continue_value
,
5102 bool *non_constant_p
, bool *overflow_p
)
5105 tree lhs
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
5106 vc_prvalue
, non_constant_p
,
5108 VERIFY_CONSTANT (lhs
);
5109 if (tree_int_cst_equal (lhs
, bailout_value
))
5111 gcc_assert (tree_int_cst_equal (lhs
, continue_value
));
5112 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
5113 vc_prvalue
, non_constant_p
,
5115 VERIFY_CONSTANT (r
);
5119 /* REF is a COMPONENT_REF designating a particular field. V is a vector of
5120 CONSTRUCTOR elements to initialize (part of) an object containing that
5121 field. Return a pointer to the constructor_elt corresponding to the
5122 initialization of the field. */
5124 static constructor_elt
*
5125 base_field_constructor_elt (vec
<constructor_elt
, va_gc
> *v
, tree ref
)
5127 tree aggr
= TREE_OPERAND (ref
, 0);
5128 tree field
= TREE_OPERAND (ref
, 1);
5130 constructor_elt
*ce
;
5132 gcc_assert (TREE_CODE (ref
) == COMPONENT_REF
);
5134 if (TREE_CODE (aggr
) == COMPONENT_REF
)
5136 constructor_elt
*base_ce
5137 = base_field_constructor_elt (v
, aggr
);
5138 v
= CONSTRUCTOR_ELTS (base_ce
->value
);
5141 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
5142 if (ce
->index
== field
)
5149 /* Some of the expressions fed to the constexpr mechanism are calls to
5150 constructors, which have type void. In that case, return the type being
5151 initialized by the constructor. */
5154 initialized_type (tree t
)
5158 tree type
= TREE_TYPE (t
);
5159 if (TREE_CODE (t
) == CALL_EXPR
)
5161 /* A constructor call has void type, so we need to look deeper. */
5162 tree fn
= get_function_named_in_call (t
);
5163 if (fn
&& TREE_CODE (fn
) == FUNCTION_DECL
5164 && DECL_CXX_CONSTRUCTOR_P (fn
))
5165 type
= DECL_CONTEXT (fn
);
5167 else if (TREE_CODE (t
) == COMPOUND_EXPR
)
5168 return initialized_type (TREE_OPERAND (t
, 1));
5169 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
)
5170 type
= TREE_TYPE (AGGR_INIT_EXPR_SLOT (t
));
5171 return cv_unqualified (type
);
5174 /* We're about to initialize element INDEX of an array or class from VALUE.
5175 Set up NEW_CTX appropriately by adjusting .object to refer to the
5176 subobject and creating a new CONSTRUCTOR if the element is itself
5177 a class or array. */
5180 init_subob_ctx (const constexpr_ctx
*ctx
, constexpr_ctx
&new_ctx
,
5181 tree index
, tree
&value
)
5185 if (index
&& TREE_CODE (index
) != INTEGER_CST
5186 && TREE_CODE (index
) != FIELD_DECL
5187 && TREE_CODE (index
) != RANGE_EXPR
)
5188 /* This won't have an element in the new CONSTRUCTOR. */
5191 tree type
= initialized_type (value
);
5192 if (!AGGREGATE_TYPE_P (type
) && !VECTOR_TYPE_P (type
))
5193 /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
5195 if (VECTOR_TYPE_P (type
)
5196 && VECTOR_TYPE_P (TREE_TYPE (ctx
->ctor
))
5197 && index
== NULL_TREE
)
5198 /* A vector inside of a vector CONSTRUCTOR, e.g. when a larger
5199 vector is constructed from smaller vectors, doesn't get its own
5200 CONSTRUCTOR either. */
5203 /* The sub-aggregate initializer might contain a placeholder;
5204 update object to refer to the subobject and ctor to refer to
5205 the (newly created) sub-initializer. */
5208 if (index
== NULL_TREE
|| TREE_CODE (index
) == RANGE_EXPR
)
5209 /* There's no well-defined subobject for this index. */
5210 new_ctx
.object
= NULL_TREE
;
5212 new_ctx
.object
= build_ctor_subob_ref (index
, type
, ctx
->object
);
5215 if (is_empty_class (type
))
5216 /* Leave ctor null for an empty subobject, they aren't represented in the
5217 result of evaluation. */
5218 new_ctx
.ctor
= NULL_TREE
;
5221 tree elt
= build_constructor (type
, NULL
);
5222 CONSTRUCTOR_NO_CLEARING (elt
) = true;
5226 if (TREE_CODE (value
) == TARGET_EXPR
)
5227 /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
5228 value
= TARGET_EXPR_INITIAL (value
);
5231 /* We're about to process an initializer for a class or array TYPE. Make
5232 sure that CTX is set up appropriately. */
5235 verify_ctor_sanity (const constexpr_ctx
*ctx
, tree type
)
5237 /* We don't bother building a ctor for an empty base subobject. */
5238 if (is_empty_class (type
))
5241 /* We're in the middle of an initializer that might involve placeholders;
5242 our caller should have created a CONSTRUCTOR for us to put the
5243 initializer into. We will either return that constructor or T. */
5244 gcc_assert (ctx
->ctor
);
5245 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5246 (type
, TREE_TYPE (ctx
->ctor
)));
5247 /* We used to check that ctx->ctor was empty, but that isn't the case when
5248 the object is zero-initialized before calling the constructor. */
5251 tree otype
= TREE_TYPE (ctx
->object
);
5252 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type
, otype
)
5253 /* Handle flexible array members. */
5254 || (TREE_CODE (otype
) == ARRAY_TYPE
5255 && TYPE_DOMAIN (otype
) == NULL_TREE
5256 && TREE_CODE (type
) == ARRAY_TYPE
5257 && (same_type_ignoring_top_level_qualifiers_p
5258 (TREE_TYPE (type
), TREE_TYPE (otype
)))));
5260 gcc_assert (!ctx
->object
|| !DECL_P (ctx
->object
)
5261 || ctx
->global
->get_value (ctx
->object
) == ctx
->ctor
);
5264 /* Subroutine of cxx_eval_constant_expression.
5265 The expression tree T denotes a C-style array or a C-style
5266 aggregate. Reduce it to a constant expression. */
5269 cxx_eval_bare_aggregate (const constexpr_ctx
*ctx
, tree t
,
5271 bool *non_constant_p
, bool *overflow_p
)
5273 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (t
);
5274 bool changed
= false;
5275 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t
));
5276 tree type
= TREE_TYPE (t
);
5278 constexpr_ctx new_ctx
;
5279 if (TYPE_PTRMEMFUNC_P (type
) || VECTOR_TYPE_P (type
))
5281 /* We don't really need the ctx->ctor business for a PMF or
5282 vector, but it's simpler to use the same code. */
5284 new_ctx
.ctor
= build_constructor (type
, NULL
);
5285 new_ctx
.object
= NULL_TREE
;
5288 verify_ctor_sanity (ctx
, type
);
5289 vec
<constructor_elt
, va_gc
> **p
= nullptr;
5292 p
= &CONSTRUCTOR_ELTS (ctx
->ctor
);
5293 vec_alloc (*p
, vec_safe_length (v
));
5294 if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (t
))
5295 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx
->ctor
) = 1;
5300 bool constant_p
= true;
5301 bool side_effects_p
= false;
5302 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, index
, value
)
5304 tree orig_value
= value
;
5305 init_subob_ctx (ctx
, new_ctx
, index
, value
);
5306 /* Like in cxx_eval_store_expression, omit entries for empty fields. */
5307 bool no_slot
= new_ctx
.ctor
== NULL_TREE
;
5309 if (new_ctx
.ctor
!= ctx
->ctor
&& !no_slot
)
5311 /* If we built a new CONSTRUCTOR, attach it now so that other
5312 initializers can refer to it. */
5313 constructor_elt
*cep
= get_or_insert_ctor_field (ctx
->ctor
, index
);
5314 cep
->value
= new_ctx
.ctor
;
5315 pos_hint
= cep
- (*p
)->begin();
5317 else if (TREE_CODE (type
) == UNION_TYPE
)
5318 /* Otherwise if we're constructing a non-aggregate union member, set
5319 the active union member now so that we can later detect and diagnose
5320 if its initializer attempts to activate another member. */
5321 get_or_insert_ctor_field (ctx
->ctor
, index
);
5322 tree elt
= cxx_eval_constant_expression (&new_ctx
, value
,
5324 non_constant_p
, overflow_p
);
5325 /* Don't VERIFY_CONSTANT here. */
5326 if (ctx
->quiet
&& *non_constant_p
)
5328 if (elt
!= orig_value
)
5331 if (!TREE_CONSTANT (elt
))
5333 if (TREE_SIDE_EFFECTS (elt
))
5334 side_effects_p
= true;
5335 if (index
&& TREE_CODE (index
) == COMPONENT_REF
)
5337 /* This is an initialization of a vfield inside a base
5338 subaggregate that we already initialized; push this
5339 initialization into the previous initialization. */
5340 constructor_elt
*inner
= base_field_constructor_elt (*p
, index
);
5345 /* This is an initializer for an empty field; now that we've
5346 checked that it's constant, we can ignore it. */
5349 && (TREE_CODE (index
) == NOP_EXPR
5350 || TREE_CODE (index
) == POINTER_PLUS_EXPR
))
5352 /* Old representation of empty bases. FIXME remove. */
5353 gcc_checking_assert (false);
5354 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index
))));
5359 if (TREE_CODE (type
) == UNION_TYPE
5360 && (*p
)->last().index
!= index
)
5361 /* The initializer erroneously changed the active union member that
5362 we're initializing. */
5363 gcc_assert (*non_constant_p
);
5366 /* The initializer might have mutated the underlying CONSTRUCTOR,
5367 so recompute the location of the target constructer_elt. */
5368 constructor_elt
*cep
5369 = get_or_insert_ctor_field (ctx
->ctor
, index
, pos_hint
);
5373 /* Adding or replacing an element might change the ctor's flags. */
5374 TREE_CONSTANT (ctx
->ctor
) = constant_p
;
5375 TREE_SIDE_EFFECTS (ctx
->ctor
) = side_effects_p
;
5378 if (*non_constant_p
)
5382 if (VECTOR_TYPE_P (type
))
5388 t
= build_constructor (type
, NULL
);
5389 /* We're done building this CONSTRUCTOR, so now we can interpret an
5390 element without an explicit initializer as value-initialized. */
5391 CONSTRUCTOR_NO_CLEARING (t
) = false;
5392 TREE_CONSTANT (t
) = constant_p
;
5393 TREE_SIDE_EFFECTS (t
) = side_effects_p
;
5394 if (VECTOR_TYPE_P (type
))
5399 /* Subroutine of cxx_eval_constant_expression.
5400 The expression tree T is a VEC_INIT_EXPR which denotes the desired
5401 initialization of a non-static data member of array type. Reduce it to a
5404 Note that apart from value-initialization (when VALUE_INIT is true),
5405 this is only intended to support value-initialization and the
5406 initializations done by defaulted constructors for classes with
5407 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
5408 will either be NULL_TREE for the default constructor, or a COMPONENT_REF
5409 for the copy/move constructor. */
5412 cxx_eval_vec_init_1 (const constexpr_ctx
*ctx
, tree atype
, tree init
,
5413 bool value_init
, value_cat lval
,
5414 bool *non_constant_p
, bool *overflow_p
)
5416 tree elttype
= TREE_TYPE (atype
);
5417 verify_ctor_sanity (ctx
, atype
);
5418 vec
<constructor_elt
, va_gc
> **p
= &CONSTRUCTOR_ELTS (ctx
->ctor
);
5419 bool pre_init
= false;
5420 unsigned HOST_WIDE_INT i
;
5421 tsubst_flags_t complain
= ctx
->quiet
? tf_none
: tf_warning_or_error
;
5423 if (init
&& TREE_CODE (init
) == CONSTRUCTOR
)
5424 return cxx_eval_bare_aggregate (ctx
, init
, lval
,
5425 non_constant_p
, overflow_p
);
5427 /* For the default constructor, build up a call to the default
5428 constructor of the element type. We only need to handle class types
5429 here, as for a constructor to be constexpr, all members must be
5430 initialized, which for a defaulted default constructor means they must
5431 be of a class type with a constexpr default constructor. */
5432 if (TREE_CODE (elttype
) == ARRAY_TYPE
)
5433 /* We only do this at the lowest level. */;
5434 else if (value_init
)
5436 init
= build_value_init (elttype
, complain
);
5441 releasing_vec argvec
;
5442 init
= build_special_member_call (NULL_TREE
, complete_ctor_identifier
,
5443 &argvec
, elttype
, LOOKUP_NORMAL
,
5445 init
= build_aggr_init_expr (elttype
, init
);
5449 bool zeroed_out
= false;
5450 if (!CONSTRUCTOR_NO_CLEARING (ctx
->ctor
))
5452 /* We're initializing an array object that had been zero-initialized
5453 earlier. Truncate ctx->ctor, and propagate its zeroed state by
5454 clearing CONSTRUCTOR_NO_CLEARING on each of the aggregate element
5455 initializers we append to it. */
5456 gcc_checking_assert (initializer_zerop (ctx
->ctor
));
5458 vec_safe_truncate (*p
, 0);
5461 tree nelts
= get_array_or_vector_nelts (ctx
, atype
, non_constant_p
,
5463 unsigned HOST_WIDE_INT max
= tree_to_uhwi (nelts
);
5464 for (i
= 0; i
< max
; ++i
)
5466 tree idx
= build_int_cst (size_type_node
, i
);
5469 constexpr_ctx new_ctx
;
5470 init_subob_ctx (ctx
, new_ctx
, idx
, pre_init
? init
: elttype
);
5471 bool no_slot
= new_ctx
.ctor
== NULL_TREE
;
5472 if (new_ctx
.ctor
!= ctx
->ctor
&& !no_slot
)
5475 CONSTRUCTOR_NO_CLEARING (new_ctx
.ctor
) = false;
5476 CONSTRUCTOR_APPEND_ELT (*p
, idx
, new_ctx
.ctor
);
5478 if (TREE_CODE (elttype
) == ARRAY_TYPE
)
5480 /* A multidimensional array; recurse. */
5481 if (value_init
|| init
== NULL_TREE
)
5483 eltinit
= NULL_TREE
;
5487 eltinit
= cp_build_array_ref (input_location
, init
, idx
, complain
);
5488 eltinit
= cxx_eval_vec_init_1 (&new_ctx
, elttype
, eltinit
, value_init
,
5490 non_constant_p
, overflow_p
);
5494 /* Initializing an element using value or default initialization
5495 we just pre-built above. */
5496 if (init
== void_node
)
5497 /* Trivial default-init, don't do anything to the CONSTRUCTOR. */
5499 eltinit
= cxx_eval_constant_expression (&new_ctx
, init
, lval
,
5500 non_constant_p
, overflow_p
);
5505 /* Copying an element. */
5506 eltinit
= cp_build_array_ref (input_location
, init
, idx
, complain
);
5507 if (!lvalue_p (init
))
5508 eltinit
= move (eltinit
);
5509 eltinit
= (perform_implicit_conversion_flags
5510 (elttype
, eltinit
, complain
,
5511 LOOKUP_IMPLICIT
|LOOKUP_NO_NARROWING
));
5512 eltinit
= cxx_eval_constant_expression (&new_ctx
, eltinit
, lval
,
5513 non_constant_p
, overflow_p
);
5515 if (*non_constant_p
)
5519 /* This is an initializer for an empty subobject; now that we've
5520 checked that it's constant, we can ignore it. */
5521 gcc_checking_assert (i
== 0);
5524 else if (new_ctx
.ctor
!= ctx
->ctor
)
5526 /* We appended this element above; update the value. */
5527 gcc_assert ((*p
)->last().index
== idx
);
5528 (*p
)->last().value
= eltinit
;
5531 CONSTRUCTOR_APPEND_ELT (*p
, idx
, eltinit
);
5532 /* Reuse the result of cxx_eval_constant_expression call
5533 from the first iteration to all others if it is a constant
5534 initializer that doesn't require relocations. */
5537 && (eltinit
== NULL_TREE
5538 || (initializer_constant_valid_p (eltinit
, TREE_TYPE (eltinit
))
5539 == null_pointer_node
)))
5541 if (new_ctx
.ctor
!= ctx
->ctor
)
5542 eltinit
= new_ctx
.ctor
;
5543 tree range
= build2 (RANGE_EXPR
, size_type_node
,
5544 build_int_cst (size_type_node
, 1),
5545 build_int_cst (size_type_node
, max
- 1));
5546 CONSTRUCTOR_APPEND_ELT (*p
, range
, unshare_constructor (eltinit
));
5550 vec_safe_reserve (*p
, max
);
5553 if (!*non_constant_p
)
5556 CONSTRUCTOR_NO_CLEARING (init
) = false;
5562 cxx_eval_vec_init (const constexpr_ctx
*ctx
, tree t
,
5564 bool *non_constant_p
, bool *overflow_p
)
5566 tree atype
= TREE_TYPE (t
);
5567 tree init
= VEC_INIT_EXPR_INIT (t
);
5568 bool value_init
= VEC_INIT_EXPR_VALUE_INIT (t
);
5569 if (!init
|| !BRACE_ENCLOSED_INITIALIZER_P (init
))
5571 else if (CONSTRUCTOR_NELTS (init
) == 0
5572 && !CP_AGGREGATE_TYPE_P (strip_array_types (atype
)))
5574 /* Handle {} as value-init. */
5580 /* This is a more complicated case, like needing to loop over trailing
5581 elements; call build_vec_init and evaluate the result. */
5582 tsubst_flags_t complain
= ctx
->quiet
? tf_none
: tf_warning_or_error
;
5583 constexpr_ctx new_ctx
= *ctx
;
5586 /* We want to have an initialization target for an VEC_INIT_EXPR.
5587 If we don't already have one in CTX, use the VEC_INIT_EXPR_SLOT. */
5588 new_ctx
.object
= VEC_INIT_EXPR_SLOT (t
);
5589 tree ctor
= new_ctx
.ctor
= build_constructor (atype
, NULL
);
5590 CONSTRUCTOR_NO_CLEARING (ctor
) = true;
5591 ctx
->global
->put_value (new_ctx
.object
, ctor
);
5594 init
= expand_vec_init_expr (ctx
->object
, t
, complain
);
5595 return cxx_eval_constant_expression (ctx
, init
, lval
, non_constant_p
,
5598 tree r
= cxx_eval_vec_init_1 (ctx
, atype
, init
, value_init
,
5599 lval
, non_constant_p
, overflow_p
);
5600 if (*non_constant_p
)
5606 /* Like same_type_ignoring_top_level_qualifiers_p, but also handle the case
5607 where the desired type is an array of unknown bounds because the variable
5608 has had its bounds deduced since the wrapping expression was created. */
5611 same_type_ignoring_tlq_and_bounds_p (tree type1
, tree type2
)
5613 while (TREE_CODE (type1
) == ARRAY_TYPE
5614 && TREE_CODE (type2
) == ARRAY_TYPE
5615 && (!TYPE_DOMAIN (type1
) || !TYPE_DOMAIN (type2
)))
5617 type1
= TREE_TYPE (type1
);
5618 type2
= TREE_TYPE (type2
);
5620 return same_type_ignoring_top_level_qualifiers_p (type1
, type2
);
5623 /* Try to determine the currently active union member for an expression
5624 with UNION_TYPE. If it can be determined, return the FIELD_DECL,
5625 otherwise return NULL_TREE. */
5628 cxx_union_active_member (const constexpr_ctx
*ctx
, tree t
)
5630 constexpr_ctx new_ctx
= *ctx
;
5631 new_ctx
.quiet
= true;
5632 bool non_constant_p
= false, overflow_p
= false;
5633 tree ctor
= cxx_eval_constant_expression (&new_ctx
, t
, vc_prvalue
,
5636 if (TREE_CODE (ctor
) == CONSTRUCTOR
5637 && CONSTRUCTOR_NELTS (ctor
) == 1
5638 && CONSTRUCTOR_ELT (ctor
, 0)->index
5639 && TREE_CODE (CONSTRUCTOR_ELT (ctor
, 0)->index
) == FIELD_DECL
)
5640 return CONSTRUCTOR_ELT (ctor
, 0)->index
;
5644 /* Helper function for cxx_fold_indirect_ref_1, called recursively. */
5647 cxx_fold_indirect_ref_1 (const constexpr_ctx
*ctx
, location_t loc
, tree type
,
5648 tree op
, unsigned HOST_WIDE_INT off
, bool *empty_base
)
5650 tree optype
= TREE_TYPE (op
);
5651 unsigned HOST_WIDE_INT const_nunits
;
5652 if (off
== 0 && similar_type_p (optype
, type
))
5654 else if (TREE_CODE (optype
) == COMPLEX_TYPE
5655 && similar_type_p (type
, TREE_TYPE (optype
)))
5657 /* *(foo *)&complexfoo => __real__ complexfoo */
5659 return build1_loc (loc
, REALPART_EXPR
, type
, op
);
5660 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
5661 else if (tree_to_uhwi (TYPE_SIZE_UNIT (type
)) == off
)
5662 return build1_loc (loc
, IMAGPART_EXPR
, type
, op
);
5664 /* ((foo*)&vectorfoo)[x] => BIT_FIELD_REF<vectorfoo,...> */
5665 else if (VECTOR_TYPE_P (optype
)
5666 && similar_type_p (type
, TREE_TYPE (optype
))
5667 && TYPE_VECTOR_SUBPARTS (optype
).is_constant (&const_nunits
))
5669 unsigned HOST_WIDE_INT part_width
= tree_to_uhwi (TYPE_SIZE_UNIT (type
));
5670 unsigned HOST_WIDE_INT max_offset
= part_width
* const_nunits
;
5671 if (off
< max_offset
&& off
% part_width
== 0)
5673 tree index
= bitsize_int (off
* BITS_PER_UNIT
);
5674 return build3_loc (loc
, BIT_FIELD_REF
, type
, op
,
5675 TYPE_SIZE (type
), index
);
5678 /* ((foo *)&fooarray)[x] => fooarray[x] */
5679 else if (TREE_CODE (optype
) == ARRAY_TYPE
5680 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (optype
)))
5681 && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (optype
))))
5683 tree type_domain
= TYPE_DOMAIN (optype
);
5684 tree min_val
= size_zero_node
;
5685 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
5686 min_val
= TYPE_MIN_VALUE (type_domain
);
5687 unsigned HOST_WIDE_INT el_sz
5688 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (optype
)));
5689 unsigned HOST_WIDE_INT idx
= off
/ el_sz
;
5690 unsigned HOST_WIDE_INT rem
= off
% el_sz
;
5691 if (tree_fits_uhwi_p (min_val
))
5693 tree index
= size_int (idx
+ tree_to_uhwi (min_val
));
5694 op
= build4_loc (loc
, ARRAY_REF
, TREE_TYPE (optype
), op
, index
,
5695 NULL_TREE
, NULL_TREE
);
5696 return cxx_fold_indirect_ref_1 (ctx
, loc
, type
, op
, rem
,
5700 /* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */
5701 else if (TREE_CODE (optype
) == RECORD_TYPE
5702 || TREE_CODE (optype
) == UNION_TYPE
)
5704 if (TREE_CODE (optype
) == UNION_TYPE
)
5705 /* For unions prefer the currently active member. */
5706 if (tree field
= cxx_union_active_member (ctx
, op
))
5708 unsigned HOST_WIDE_INT el_sz
5709 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field
)));
5712 tree cop
= build3 (COMPONENT_REF
, TREE_TYPE (field
),
5713 op
, field
, NULL_TREE
);
5714 if (tree ret
= cxx_fold_indirect_ref_1 (ctx
, loc
, type
, cop
,
5720 /* Handle conversion to "as base" type. */
5721 if (CLASS_TYPE_P (optype
)
5722 && CLASSTYPE_AS_BASE (optype
) == type
)
5725 /* Handle conversion to an empty base class, which is represented with a
5726 NOP_EXPR. Do this before spelunking into the non-empty subobjects,
5727 which is likely to be a waste of time (109678). */
5728 if (is_empty_class (type
)
5729 && CLASS_TYPE_P (optype
)
5730 && lookup_base (optype
, type
, ba_any
, NULL
, tf_none
, off
))
5737 for (tree field
= TYPE_FIELDS (optype
);
5738 field
; field
= DECL_CHAIN (field
))
5739 if (TREE_CODE (field
) == FIELD_DECL
5740 && TREE_TYPE (field
) != error_mark_node
5741 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (field
))))
5743 tree pos
= byte_position (field
);
5744 if (!tree_fits_uhwi_p (pos
))
5746 unsigned HOST_WIDE_INT upos
= tree_to_uhwi (pos
);
5747 unsigned HOST_WIDE_INT el_sz
5748 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field
)));
5749 if (upos
<= off
&& off
< upos
+ el_sz
)
5751 tree cop
= build3 (COMPONENT_REF
, TREE_TYPE (field
),
5752 op
, field
, NULL_TREE
);
5753 if (tree ret
= cxx_fold_indirect_ref_1 (ctx
, loc
, type
, cop
,
5764 /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
5765 match. We want to be less strict for simple *& folding; if we have a
5766 non-const temporary that we access through a const pointer, that should
5767 work. We handle this here rather than change fold_indirect_ref_1
5768 because we're dealing with things like ADDR_EXPR of INTEGER_CST which
5769 don't really make sense outside of constant expression evaluation. Also
5770 we want to allow folding to COMPONENT_REF, which could cause trouble
5771 with TBAA in fold_indirect_ref_1. */
5774 cxx_fold_indirect_ref (const constexpr_ctx
*ctx
, location_t loc
, tree type
,
5775 tree op0
, bool *empty_base
/* = NULL*/)
5780 /* STRIP_NOPS, but stop if REINTERPRET_CAST_P. */
5781 while (CONVERT_EXPR_P (sub
) || TREE_CODE (sub
) == NON_LVALUE_EXPR
5782 || TREE_CODE (sub
) == VIEW_CONVERT_EXPR
)
5784 if (TREE_CODE (sub
) == NOP_EXPR
5785 && REINTERPRET_CAST_P (sub
))
5787 sub
= TREE_OPERAND (sub
, 0);
5790 subtype
= TREE_TYPE (sub
);
5791 if (!INDIRECT_TYPE_P (subtype
))
5794 /* Canonicalizes the given OBJ/OFF pair by iteratively absorbing
5795 the innermost component into the offset until it would make the
5796 offset positive, so that cxx_fold_indirect_ref_1 can identify
5797 more folding opportunities. */
5798 auto canonicalize_obj_off
= [] (tree
& obj
, tree
& off
) {
5799 while (TREE_CODE (obj
) == COMPONENT_REF
5800 && (tree_int_cst_sign_bit (off
) || integer_zerop (off
)))
5802 tree field
= TREE_OPERAND (obj
, 1);
5803 tree pos
= byte_position (field
);
5804 if (integer_zerop (off
) && integer_nonzerop (pos
))
5805 /* If the offset is already 0, keep going as long as the
5806 component is at position 0. */
5808 off
= int_const_binop (PLUS_EXPR
, off
, pos
);
5809 obj
= TREE_OPERAND (obj
, 0);
5813 if (TREE_CODE (sub
) == ADDR_EXPR
)
5815 tree op
= TREE_OPERAND (sub
, 0);
5816 tree optype
= TREE_TYPE (op
);
5818 /* *&CONST_DECL -> to the value of the const decl. */
5819 if (TREE_CODE (op
) == CONST_DECL
)
5820 return DECL_INITIAL (op
);
5821 /* *&p => p; make sure to handle *&"str"[cst] here. */
5822 if (similar_type_p (optype
, type
))
5824 tree fop
= fold_read_from_constant_string (op
);
5832 tree off
= integer_zero_node
;
5833 canonicalize_obj_off (op
, off
);
5834 gcc_assert (integer_zerop (off
));
5835 return cxx_fold_indirect_ref_1 (ctx
, loc
, type
, op
, 0, empty_base
);
5838 else if (TREE_CODE (sub
) == POINTER_PLUS_EXPR
5839 && tree_fits_uhwi_p (TREE_OPERAND (sub
, 1)))
5841 tree op00
= TREE_OPERAND (sub
, 0);
5842 tree off
= TREE_OPERAND (sub
, 1);
5845 if (TREE_CODE (op00
) == ADDR_EXPR
)
5847 tree obj
= TREE_OPERAND (op00
, 0);
5848 canonicalize_obj_off (obj
, off
);
5849 return cxx_fold_indirect_ref_1 (ctx
, loc
, type
, obj
,
5850 tree_to_uhwi (off
), empty_base
);
5853 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
5854 else if (TREE_CODE (TREE_TYPE (subtype
)) == ARRAY_TYPE
5855 && similar_type_p (type
, TREE_TYPE (TREE_TYPE (subtype
))))
5858 tree min_val
= size_zero_node
;
5860 = cxx_fold_indirect_ref (ctx
, loc
, TREE_TYPE (subtype
), sub
, NULL
);
5864 sub
= build1_loc (loc
, INDIRECT_REF
, TREE_TYPE (subtype
), sub
);
5865 type_domain
= TYPE_DOMAIN (TREE_TYPE (sub
));
5866 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
5867 min_val
= TYPE_MIN_VALUE (type_domain
);
5868 return build4_loc (loc
, ARRAY_REF
, type
, sub
, min_val
, NULL_TREE
,
5876 cxx_eval_indirect_ref (const constexpr_ctx
*ctx
, tree t
,
5878 bool *non_constant_p
, bool *overflow_p
)
5880 tree orig_op0
= TREE_OPERAND (t
, 0);
5881 bool empty_base
= false;
5883 /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second
5884 operand is an integer-zero. Otherwise reject the MEM_REF for now. */
5886 if (TREE_CODE (t
) == MEM_REF
5887 && (!TREE_OPERAND (t
, 1) || !integer_zerop (TREE_OPERAND (t
, 1))))
5889 gcc_assert (ctx
->quiet
);
5890 *non_constant_p
= true;
5894 /* First try to simplify it directly. */
5895 tree r
= cxx_fold_indirect_ref (ctx
, EXPR_LOCATION (t
), TREE_TYPE (t
),
5896 orig_op0
, &empty_base
);
5899 /* If that didn't work, evaluate the operand first. */
5900 tree op0
= cxx_eval_constant_expression (ctx
, orig_op0
,
5901 vc_prvalue
, non_constant_p
,
5903 /* Don't VERIFY_CONSTANT here. */
5904 if (*non_constant_p
)
5907 if (!lval
&& integer_zerop (op0
))
5910 error ("dereferencing a null pointer");
5911 *non_constant_p
= true;
5915 r
= cxx_fold_indirect_ref (ctx
, EXPR_LOCATION (t
), TREE_TYPE (t
), op0
,
5919 /* We couldn't fold to a constant value. Make sure it's not
5920 something we should have been able to fold. */
5923 if (TREE_CODE (sub
) == ADDR_EXPR
)
5925 gcc_assert (!similar_type_p
5926 (TREE_TYPE (TREE_TYPE (sub
)), TREE_TYPE (t
)));
5927 /* DR 1188 says we don't have to deal with this. */
5929 error_at (cp_expr_loc_or_input_loc (t
),
5930 "accessing value of %qE through a %qT glvalue in a "
5931 "constant expression", build_fold_indirect_ref (sub
),
5933 *non_constant_p
= true;
5937 if (lval
== vc_glvalue
&& op0
!= orig_op0
)
5938 return build1 (INDIRECT_REF
, TREE_TYPE (t
), op0
);
5940 VERIFY_CONSTANT (t
);
5945 r
= cxx_eval_constant_expression (ctx
, r
,
5946 lval
, non_constant_p
, overflow_p
);
5947 if (*non_constant_p
)
5950 /* If we're pulling out the value of an empty base, just return an empty
5952 if (empty_base
&& !lval
)
5954 r
= build_constructor (TREE_TYPE (t
), NULL
);
5955 TREE_CONSTANT (r
) = true;
5961 /* Complain about R, a DECL that is accessed outside its lifetime. */
5964 outside_lifetime_error (location_t loc
, tree r
)
5966 auto_diagnostic_group d
;
5967 if (DECL_NAME (r
) == heap_deleted_identifier
)
5969 /* Provide a more accurate message for deleted variables. */
5970 error_at (loc
, "use of allocated storage after deallocation "
5971 "in a constant expression");
5972 inform (DECL_SOURCE_LOCATION (r
), "allocated here");
5976 error_at (loc
, "accessing %qE outside its lifetime", r
);
5977 inform (DECL_SOURCE_LOCATION (r
), "declared here");
5981 /* Complain about R, a VAR_DECL, not being usable in a constant expression.
5982 FUNDEF_P is true if we're checking a constexpr function body.
5983 Shared between potential_constant_expression and
5984 cxx_eval_constant_expression. */
5987 non_const_var_error (location_t loc
, tree r
, bool fundef_p
)
5989 auto_diagnostic_group d
;
5990 tree type
= TREE_TYPE (r
);
5991 if (DECL_NAME (r
) == heap_uninit_identifier
5992 || DECL_NAME (r
) == heap_identifier
5993 || DECL_NAME (r
) == heap_vec_uninit_identifier
5994 || DECL_NAME (r
) == heap_vec_identifier
)
5996 if (constexpr_error (loc
, fundef_p
, "the content of uninitialized "
5997 "storage is not usable in a constant expression"))
5998 inform (DECL_SOURCE_LOCATION (r
), "allocated here");
6001 if (DECL_NAME (r
) == heap_deleted_identifier
)
6003 if (constexpr_error (loc
, fundef_p
, "use of allocated storage after "
6004 "deallocation in a constant expression"))
6005 inform (DECL_SOURCE_LOCATION (r
), "allocated here");
6008 if (!constexpr_error (loc
, fundef_p
, "the value of %qD is not usable in "
6009 "a constant expression", r
))
6011 /* Avoid error cascade. */
6012 if (DECL_INITIAL (r
) == error_mark_node
)
6014 if (DECL_DECLARED_CONSTEXPR_P (r
))
6015 inform (DECL_SOURCE_LOCATION (r
),
6016 "%qD used in its own initializer", r
);
6017 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
6019 if (!CP_TYPE_CONST_P (type
))
6020 inform (DECL_SOURCE_LOCATION (r
),
6021 "%q#D is not const", r
);
6022 else if (CP_TYPE_VOLATILE_P (type
))
6023 inform (DECL_SOURCE_LOCATION (r
),
6024 "%q#D is volatile", r
);
6025 else if (!DECL_INITIAL (r
)
6026 || !TREE_CONSTANT (DECL_INITIAL (r
))
6027 || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
))
6028 inform (DECL_SOURCE_LOCATION (r
),
6029 "%qD was not initialized with a constant "
6034 else if (TYPE_REF_P (type
))
6035 inform (DECL_SOURCE_LOCATION (r
),
6036 "%qD was not initialized with a constant "
6040 if (cxx_dialect
>= cxx11
&& !DECL_DECLARED_CONSTEXPR_P (r
))
6041 inform (DECL_SOURCE_LOCATION (r
),
6042 "%qD was not declared %<constexpr%>", r
);
6044 inform (DECL_SOURCE_LOCATION (r
),
6045 "%qD does not have integral or enumeration type",
6050 /* Subroutine of cxx_eval_constant_expression.
6051 Like cxx_eval_unary_expression, except for trinary expressions. */
6054 cxx_eval_trinary_expression (const constexpr_ctx
*ctx
, tree t
,
6056 bool *non_constant_p
, bool *overflow_p
)
6062 for (i
= 0; i
< 3; i
++)
6064 args
[i
] = cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, i
),
6066 non_constant_p
, overflow_p
);
6067 VERIFY_CONSTANT (args
[i
]);
6070 val
= fold_ternary_loc (EXPR_LOCATION (t
), TREE_CODE (t
), TREE_TYPE (t
),
6071 args
[0], args
[1], args
[2]);
6072 if (val
== NULL_TREE
)
6074 VERIFY_CONSTANT (val
);
6078 /* True if T was declared in a function declared to be constexpr, and
6079 therefore potentially constant in C++14. */
6082 var_in_constexpr_fn (tree t
)
6084 tree ctx
= DECL_CONTEXT (t
);
6085 return (ctx
&& TREE_CODE (ctx
) == FUNCTION_DECL
6086 && DECL_DECLARED_CONSTEXPR_P (ctx
));
6089 /* True if a function might be constexpr: either a function that was
6090 declared constexpr, or a C++17 lambda op(). */
6093 maybe_constexpr_fn (tree t
)
6095 return (DECL_DECLARED_CONSTEXPR_P (t
)
6096 || (cxx_dialect
>= cxx17
&& LAMBDA_FUNCTION_P (t
))
6097 || (flag_implicit_constexpr
6098 && DECL_DECLARED_INLINE_P (STRIP_TEMPLATE (t
))));
6101 /* True if T was declared in a function that might be constexpr: either a
6102 function that was declared constexpr, or a C++17 lambda op(). */
6105 var_in_maybe_constexpr_fn (tree t
)
6107 return (DECL_FUNCTION_SCOPE_P (t
)
6108 && maybe_constexpr_fn (DECL_CONTEXT (t
)));
6111 /* We're assigning INIT to TARGET. In do_build_copy_constructor and
6112 build_over_call we implement trivial copy of a class with tail padding using
6113 assignment of character arrays, which is valid in normal code, but not in
6114 constexpr evaluation. We don't need to worry about clobbering tail padding
6115 in constexpr evaluation, so strip the type punning. */
6118 maybe_simplify_trivial_copy (tree
&target
, tree
&init
)
6120 if (TREE_CODE (target
) == MEM_REF
6121 && TREE_CODE (init
) == MEM_REF
6122 && TREE_TYPE (target
) == TREE_TYPE (init
)
6123 && TREE_CODE (TREE_TYPE (target
)) == ARRAY_TYPE
6124 && TREE_TYPE (TREE_TYPE (target
)) == unsigned_char_type_node
)
6126 target
= build_fold_indirect_ref (TREE_OPERAND (target
, 0));
6127 init
= build_fold_indirect_ref (TREE_OPERAND (init
, 0));
6131 /* Returns true if REF, which is a COMPONENT_REF, has any fields
6132 of constant type. This does not check for 'mutable', so the
6133 caller is expected to be mindful of that. */
6136 cref_has_const_field (tree ref
)
6138 while (TREE_CODE (ref
) == COMPONENT_REF
)
6140 if (CP_TYPE_CONST_P (TREE_TYPE (TREE_OPERAND (ref
, 1))))
6142 ref
= TREE_OPERAND (ref
, 0);
6147 /* Return true if we are modifying something that is const during constant
6148 expression evaluation. CODE is the code of the statement, OBJ is the
6149 object in question, MUTABLE_P is true if one of the subobjects were
6150 declared mutable. */
6153 modifying_const_object_p (tree_code code
, tree obj
, bool mutable_p
)
6155 /* If this is initialization, there's no problem. */
6156 if (code
!= MODIFY_EXPR
)
6159 /* [basic.type.qualifier] "A const object is an object of type
6160 const T or a non-mutable subobject of a const object." */
6164 if (TREE_READONLY (obj
))
6167 if (CP_TYPE_CONST_P (TREE_TYPE (obj
)))
6169 /* Although a COMPONENT_REF may have a const type, we should
6170 only consider it modifying a const object when any of the
6171 field components is const. This can happen when using
6172 constructs such as const_cast<const T &>(m), making something
6173 const even though it wasn't declared const. */
6174 if (TREE_CODE (obj
) == COMPONENT_REF
)
6175 return cref_has_const_field (obj
);
6183 /* Evaluate an INIT_EXPR or MODIFY_EXPR. */
6186 cxx_eval_store_expression (const constexpr_ctx
*ctx
, tree t
,
6188 bool *non_constant_p
, bool *overflow_p
)
6190 constexpr_ctx new_ctx
= *ctx
;
6192 tree init
= TREE_OPERAND (t
, 1);
6194 if (TREE_CLOBBER_P (init
)
6195 && CLOBBER_KIND (init
) < CLOBBER_OBJECT_END
)
6196 /* Only handle clobbers ending the lifetime of objects. */
6199 /* First we figure out where we're storing to. */
6200 tree target
= TREE_OPERAND (t
, 0);
6202 maybe_simplify_trivial_copy (target
, init
);
6204 tree type
= TREE_TYPE (target
);
6205 bool preeval
= SCALAR_TYPE_P (type
) || TREE_CODE (t
) == MODIFY_EXPR
;
6206 if (preeval
&& !TREE_CLOBBER_P (init
))
6208 /* Evaluate the value to be stored without knowing what object it will be
6209 stored in, so that any side-effects happen first. */
6210 if (!SCALAR_TYPE_P (type
))
6211 new_ctx
.ctor
= new_ctx
.object
= NULL_TREE
;
6212 init
= cxx_eval_constant_expression (&new_ctx
, init
, vc_prvalue
,
6213 non_constant_p
, overflow_p
);
6214 if (*non_constant_p
)
6218 bool evaluated
= false;
6219 if (lval
== vc_glvalue
)
6221 /* If we want to return a reference to the target, we need to evaluate it
6222 as a whole; otherwise, only evaluate the innermost piece to avoid
6223 building up unnecessary *_REFs. */
6224 target
= cxx_eval_constant_expression (ctx
, target
, lval
,
6225 non_constant_p
, overflow_p
);
6227 if (*non_constant_p
)
6231 /* Find the underlying variable. */
6233 tree object
= NULL_TREE
;
6234 /* If we're modifying a const object, save it. */
6235 tree const_object_being_modified
= NULL_TREE
;
6236 bool mutable_p
= false;
6237 for (tree probe
= target
; object
== NULL_TREE
; )
6239 switch (TREE_CODE (probe
))
6245 tree ob
= TREE_OPERAND (probe
, 0);
6246 tree elt
= TREE_OPERAND (probe
, 1);
6247 if (TREE_CODE (elt
) == FIELD_DECL
&& DECL_MUTABLE_P (elt
))
6249 if (TREE_CODE (probe
) == ARRAY_REF
)
6251 elt
= eval_and_check_array_index (ctx
, probe
, false,
6252 non_constant_p
, overflow_p
);
6253 if (*non_constant_p
)
6256 /* We don't check modifying_const_object_p for ARRAY_REFs. Given
6257 "int a[10]", an ARRAY_REF "a[2]" can be "const int", even though
6258 the array isn't const. Instead, check "a" in the next iteration;
6259 that will detect modifying "const int a[10]". */
6261 && modifying_const_object_p (TREE_CODE (t
), probe
,
6263 && const_object_being_modified
== NULL_TREE
)
6264 const_object_being_modified
= probe
;
6266 /* Track named member accesses for unions to validate modifications
6267 that change active member. */
6268 if (!evaluated
&& TREE_CODE (probe
) == COMPONENT_REF
)
6269 vec_safe_push (refs
, probe
);
6271 vec_safe_push (refs
, NULL_TREE
);
6273 vec_safe_push (refs
, elt
);
6274 vec_safe_push (refs
, TREE_TYPE (probe
));
6280 gcc_assert (probe
== target
);
6281 vec_safe_push (refs
, NULL_TREE
);
6282 vec_safe_push (refs
, probe
);
6283 vec_safe_push (refs
, TREE_TYPE (probe
));
6284 probe
= TREE_OPERAND (probe
, 0);
6288 gcc_assert (probe
== target
);
6289 vec_safe_push (refs
, NULL_TREE
);
6290 vec_safe_push (refs
, probe
);
6291 vec_safe_push (refs
, TREE_TYPE (probe
));
6292 probe
= TREE_OPERAND (probe
, 0);
6300 probe
= cxx_eval_constant_expression (ctx
, probe
, vc_glvalue
,
6301 non_constant_p
, overflow_p
);
6303 if (*non_constant_p
)
6310 if (modifying_const_object_p (TREE_CODE (t
), object
, mutable_p
)
6311 && const_object_being_modified
== NULL_TREE
)
6312 const_object_being_modified
= object
;
6315 && TREE_CLOBBER_P (init
)
6316 && DECL_NAME (object
) == heap_deleted_identifier
)
6317 /* Ignore clobbers of deleted allocations for now; we'll get a better error
6318 message later when operator delete is called. */
6321 /* And then find/build up our initializer for the path to the subobject
6322 we're initializing. */
6324 if (DECL_P (object
))
6325 valp
= ctx
->global
->get_value_ptr (object
, TREE_CODE (t
) == INIT_EXPR
);
6330 /* A constant-expression cannot modify objects from outside the
6331 constant-expression. */
6334 auto_diagnostic_group d
;
6335 if (DECL_P (object
) && DECL_NAME (object
) == heap_deleted_identifier
)
6337 error ("modification of allocated storage after deallocation "
6338 "is not a constant expression");
6339 inform (DECL_SOURCE_LOCATION (object
), "allocated here");
6341 else if (DECL_P (object
) && ctx
->global
->is_outside_lifetime (object
))
6343 if (TREE_CLOBBER_P (init
))
6344 error ("destroying %qE outside its lifetime", object
);
6346 error ("modification of %qE outside its lifetime "
6347 "is not a constant expression", object
);
6348 inform (DECL_SOURCE_LOCATION (object
), "declared here");
6352 if (TREE_CLOBBER_P (init
))
6353 error ("destroying %qE from outside current evaluation "
6354 "is not a constant expression", object
);
6356 error ("modification of %qE from outside current evaluation "
6357 "is not a constant expression", object
);
6360 *non_constant_p
= true;
6364 /* Handle explicit end-of-lifetime. */
6365 if (TREE_CLOBBER_P (init
))
6367 if (refs
->is_empty ())
6368 ctx
->global
->destroy_value (object
);
6372 type
= TREE_TYPE (object
);
6373 bool no_zero_init
= true;
6375 auto_vec
<tree
*> ctors
;
6376 releasing_vec indexes
;
6377 auto_vec
<int> index_pos_hints
;
6378 bool activated_union_member_p
= false;
6379 bool empty_base
= false;
6380 while (!refs
->is_empty ())
6382 if (*valp
== NULL_TREE
)
6384 *valp
= build_constructor (type
, NULL
);
6385 CONSTRUCTOR_NO_CLEARING (*valp
) = no_zero_init
;
6387 else if (STRIP_ANY_LOCATION_WRAPPER (*valp
),
6388 TREE_CODE (*valp
) == STRING_CST
)
6390 /* An array was initialized with a string constant, and now
6391 we're writing into one of its elements. Explode the
6392 single initialization into a set of element
6394 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
6396 tree string
= *valp
;
6397 tree elt_type
= TREE_TYPE (type
);
6398 unsigned chars_per_elt
= (TYPE_PRECISION (elt_type
)
6399 / TYPE_PRECISION (char_type_node
));
6400 unsigned num_elts
= TREE_STRING_LENGTH (string
) / chars_per_elt
;
6401 tree ary_ctor
= build_constructor (type
, NULL
);
6403 vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor
), num_elts
);
6404 for (unsigned ix
= 0; ix
!= num_elts
; ix
++)
6406 constructor_elt elt
=
6408 build_int_cst (size_type_node
, ix
),
6409 extract_string_elt (string
, chars_per_elt
, ix
)
6411 CONSTRUCTOR_ELTS (ary_ctor
)->quick_push (elt
);
6417 enum tree_code code
= TREE_CODE (type
);
6418 tree reftype
= refs
->pop();
6419 tree index
= refs
->pop();
6420 bool is_access_expr
= refs
->pop() != NULL_TREE
;
6422 if (code
== COMPLEX_TYPE
)
6424 if (TREE_CODE (*valp
) == COMPLEX_CST
)
6425 *valp
= build2 (COMPLEX_EXPR
, type
, TREE_REALPART (*valp
),
6426 TREE_IMAGPART (*valp
));
6427 else if (TREE_CODE (*valp
) == CONSTRUCTOR
6428 && CONSTRUCTOR_NELTS (*valp
) == 0
6429 && CONSTRUCTOR_NO_CLEARING (*valp
))
6431 tree r
= build_constructor (reftype
, NULL
);
6432 CONSTRUCTOR_NO_CLEARING (r
) = 1;
6433 *valp
= build2 (COMPLEX_EXPR
, type
, r
, r
);
6435 gcc_assert (TREE_CODE (*valp
) == COMPLEX_EXPR
);
6436 ctors
.safe_push (valp
);
6437 vec_safe_push (indexes
, index
);
6438 valp
= &TREE_OPERAND (*valp
, TREE_CODE (index
) == IMAGPART_EXPR
);
6439 gcc_checking_assert (refs
->is_empty ());
6444 /* If the value of object is already zero-initialized, any new ctors for
6445 subobjects will also be zero-initialized. */
6446 no_zero_init
= CONSTRUCTOR_NO_CLEARING (*valp
);
6448 if (code
== RECORD_TYPE
&& is_empty_field (index
))
6449 /* Don't build a sub-CONSTRUCTOR for an empty base or field, as they
6450 have no data and might have an offset lower than previously declared
6451 fields, which confuses the middle-end. The code below will notice
6452 that we don't have a CONSTRUCTOR for our inner target and just
6459 /* If a union is zero-initialized, its first non-static named data member
6460 is zero-initialized (and therefore active). */
6461 if (code
== UNION_TYPE
6463 && CONSTRUCTOR_NELTS (*valp
) == 0)
6464 if (tree first
= next_aggregate_field (TYPE_FIELDS (type
)))
6465 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (*valp
), first
, NULL_TREE
);
6467 /* Check for implicit change of active member for a union. */
6468 if (code
== UNION_TYPE
6469 && (CONSTRUCTOR_NELTS (*valp
) == 0
6470 || CONSTRUCTOR_ELT (*valp
, 0)->index
!= index
)
6471 /* An INIT_EXPR of the last member in an access chain is always OK,
6472 but still check implicit change of members earlier on; see
6473 cpp2a/constexpr-union6.C. */
6474 && !(TREE_CODE (t
) == INIT_EXPR
&& refs
->is_empty ()))
6476 bool has_active_member
= CONSTRUCTOR_NELTS (*valp
) != 0;
6477 tree inner
= strip_array_types (reftype
);
6479 if (has_active_member
&& cxx_dialect
< cxx20
)
6482 error_at (cp_expr_loc_or_input_loc (t
),
6483 "change of the active member of a union "
6484 "from %qD to %qD is not a constant expression "
6486 CONSTRUCTOR_ELT (*valp
, 0)->index
,
6488 *non_constant_p
= true;
6490 else if (!is_access_expr
6491 || (TREE_CODE (t
) == MODIFY_EXPR
6492 && CLASS_TYPE_P (inner
)
6493 && !type_has_non_deleted_trivial_default_ctor (inner
)))
6495 /* Diagnose changing active union member after initialization
6496 without a valid member access expression, as described in
6497 [class.union.general] p5. */
6500 auto_diagnostic_group d
;
6501 if (has_active_member
)
6502 error_at (cp_expr_loc_or_input_loc (t
),
6503 "accessing %qD member instead of initialized "
6504 "%qD member in constant expression",
6505 index
, CONSTRUCTOR_ELT (*valp
, 0)->index
);
6507 error_at (cp_expr_loc_or_input_loc (t
),
6508 "accessing uninitialized member %qD",
6511 inform (DECL_SOURCE_LOCATION (index
),
6512 "%qD does not implicitly begin its lifetime "
6513 "because %qT does not have a non-deleted "
6514 "trivial default constructor, use "
6515 "%<std::construct_at%> instead",
6518 inform (DECL_SOURCE_LOCATION (index
),
6519 "initializing %qD requires a member access "
6520 "expression as the left operand of the assignment",
6523 *non_constant_p
= true;
6525 else if (has_active_member
&& CONSTRUCTOR_NO_CLEARING (*valp
))
6527 /* Diagnose changing the active union member while the union
6528 is in the process of being initialized. */
6530 error_at (cp_expr_loc_or_input_loc (t
),
6531 "change of the active member of a union "
6532 "from %qD to %qD during initialization",
6533 CONSTRUCTOR_ELT (*valp
, 0)->index
,
6535 *non_constant_p
= true;
6537 no_zero_init
= true;
6540 ctors
.safe_push (valp
);
6541 vec_safe_push (indexes
, index
);
6543 constructor_elt
*cep
6544 = get_or_insert_ctor_field (*valp
, index
);
6545 index_pos_hints
.safe_push (cep
- CONSTRUCTOR_ELTS (*valp
)->begin());
6547 if (code
== UNION_TYPE
)
6548 activated_union_member_p
= true;
6554 /* For initialization of an empty base, the original target will be
6555 *(base*)this, evaluation of which resolves to the object
6556 argument, which has the derived type rather than the base type. */
6557 if (!empty_base
&& !(same_type_ignoring_top_level_qualifiers_p
6558 (initialized_type (init
), type
)))
6560 gcc_assert (is_empty_class (TREE_TYPE (target
)));
6564 /* Detect modifying a constant object in constexpr evaluation.
6565 We have found a const object that is being modified. Figure out
6566 if we need to issue an error. Consider
6570 constexpr A() : n(1) { n = 2; } // #1
6574 constexpr B() { a.n = 3; } // #2
6578 #1 is OK, since we're modifying an object under construction, but
6579 #2 is wrong, since "a" is const and has been fully constructed.
6580 To track it, we use the TREE_READONLY bit in the object's CONSTRUCTOR
6581 which means that the object is read-only. For the example above, the
6582 *ctors stack at the point of #2 will look like:
6584 ctors[0] = {.a={.n=2}} TREE_READONLY = 0
6585 ctors[1] = {.n=2} TREE_READONLY = 1
6587 and we're modifying "b.a", so we search the stack and see if the
6588 constructor for "b.a" has already run. */
6589 if (const_object_being_modified
)
6593 = strip_array_types (TREE_TYPE (const_object_being_modified
));
6594 if (!CLASS_TYPE_P (const_objtype
))
6598 /* [class.ctor]p5 "A constructor can be invoked for a const,
6599 volatile, or const volatile object. const and volatile
6600 semantics are not applied on an object under construction.
6601 They come into effect when the constructor for the most
6602 derived object ends." */
6603 for (tree
*elt
: ctors
)
6604 if (same_type_ignoring_top_level_qualifiers_p
6605 (TREE_TYPE (const_object_being_modified
), TREE_TYPE (*elt
)))
6607 fail
= TREE_READONLY (*elt
);
6614 modifying_const_object_error (t
, const_object_being_modified
);
6615 *non_constant_p
= true;
6622 /* We're handling an INIT_EXPR of class type, so the value of the
6623 initializer can depend on the object it's initializing. */
6625 /* Create a new CONSTRUCTOR in case evaluation of the initializer
6626 wants to modify it. */
6627 if (*valp
== NULL_TREE
)
6629 *valp
= build_constructor (type
, NULL
);
6630 CONSTRUCTOR_NO_CLEARING (*valp
) = no_zero_init
;
6632 new_ctx
.ctor
= empty_base
? NULL_TREE
: *valp
;
6633 new_ctx
.object
= target
;
6634 /* Avoid temporary materialization when initializing from a TARGET_EXPR.
6635 We don't need to mess with AGGR_EXPR_SLOT/VEC_INIT_EXPR_SLOT because
6636 expansion of those trees uses ctx instead. */
6637 if (TREE_CODE (init
) == TARGET_EXPR
)
6638 if (tree tinit
= TARGET_EXPR_INITIAL (init
))
6640 init
= cxx_eval_constant_expression (&new_ctx
, init
, vc_prvalue
,
6641 non_constant_p
, overflow_p
);
6642 /* The hash table might have moved since the get earlier, and the
6643 initializer might have mutated the underlying CONSTRUCTORs, so we must
6645 valp
= ctx
->global
->get_value_ptr (object
, TREE_CODE (t
) == INIT_EXPR
);
6646 for (unsigned i
= 0; i
< vec_safe_length (indexes
); i
++)
6649 constructor_elt
*cep
6650 = get_or_insert_ctor_field (*valp
, indexes
[i
], index_pos_hints
[i
]);
6655 if (*non_constant_p
)
6658 /* Don't share a CONSTRUCTOR that might be changed later. */
6659 init
= unshare_constructor (init
);
6661 gcc_checking_assert (!*valp
|| (same_type_ignoring_top_level_qualifiers_p
6662 (TREE_TYPE (*valp
), type
)));
6665 /* Just evaluate the initializer and return, since there's no actual data
6666 to store, and we didn't build a CONSTRUCTOR. */
6669 /* But do make sure we have something in *valp. */
6670 *valp
= build_constructor (type
, nullptr);
6671 CONSTRUCTOR_NO_CLEARING (*valp
) = no_zero_init
;
6674 else if (*valp
&& TREE_CODE (*valp
) == CONSTRUCTOR
6675 && TREE_CODE (init
) == CONSTRUCTOR
)
6677 /* An outer ctx->ctor might be pointing to *valp, so replace
6679 CONSTRUCTOR_ELTS (*valp
) = CONSTRUCTOR_ELTS (init
);
6680 TREE_CONSTANT (*valp
) = TREE_CONSTANT (init
);
6681 TREE_SIDE_EFFECTS (*valp
) = TREE_SIDE_EFFECTS (init
);
6682 CONSTRUCTOR_NO_CLEARING (*valp
)
6683 = CONSTRUCTOR_NO_CLEARING (init
);
6688 /* After initialization, 'const' semantics apply to the value of the
6689 object. Make a note of this fact by marking the CONSTRUCTOR
6691 if (TREE_CODE (t
) == INIT_EXPR
6693 && TREE_CODE (*valp
) == CONSTRUCTOR
6694 && TYPE_READONLY (type
))
6696 if (INDIRECT_REF_P (target
)
6697 && (is_this_parameter
6698 (tree_strip_nop_conversions (TREE_OPERAND (target
, 0)))))
6699 /* We've just initialized '*this' (perhaps via the target
6700 constructor of a delegating constructor). Leave it up to the
6701 caller that set 'this' to set TREE_READONLY appropriately. */
6702 gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
6703 (TREE_TYPE (target
), type
) || empty_base
);
6705 TREE_READONLY (*valp
) = true;
6708 /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing
6709 CONSTRUCTORs, if any. */
6710 bool c
= TREE_CONSTANT (init
);
6711 bool s
= TREE_SIDE_EFFECTS (init
);
6712 if (!indexes
->is_empty ())
6714 tree last
= indexes
->last ();
6715 if (TREE_CODE (last
) == REALPART_EXPR
6716 || TREE_CODE (last
) == IMAGPART_EXPR
)
6718 /* And canonicalize COMPLEX_EXPR into COMPLEX_CST if
6720 tree
*cexpr
= ctors
.last ();
6721 if (tree c
= const_binop (COMPLEX_EXPR
, TREE_TYPE (*cexpr
),
6722 TREE_OPERAND (*cexpr
, 0),
6723 TREE_OPERAND (*cexpr
, 1)))
6727 TREE_CONSTANT (*cexpr
)
6728 = (TREE_CONSTANT (TREE_OPERAND (*cexpr
, 0))
6729 & TREE_CONSTANT (TREE_OPERAND (*cexpr
, 1)));
6730 TREE_SIDE_EFFECTS (*cexpr
)
6731 = (TREE_SIDE_EFFECTS (TREE_OPERAND (*cexpr
, 0))
6732 | TREE_SIDE_EFFECTS (TREE_OPERAND (*cexpr
, 1)));
6734 c
= TREE_CONSTANT (*cexpr
);
6735 s
= TREE_SIDE_EFFECTS (*cexpr
);
6738 if (!c
|| s
|| activated_union_member_p
)
6739 for (tree
*elt
: ctors
)
6741 if (TREE_CODE (*elt
) != CONSTRUCTOR
)
6744 TREE_CONSTANT (*elt
) = false;
6746 TREE_SIDE_EFFECTS (*elt
) = true;
6747 /* Clear CONSTRUCTOR_NO_CLEARING since we've activated a member of
6749 if (TREE_CODE (TREE_TYPE (*elt
)) == UNION_TYPE
)
6750 CONSTRUCTOR_NO_CLEARING (*elt
) = false;
6759 /* Evaluate a ++ or -- expression. */
6762 cxx_eval_increment_expression (const constexpr_ctx
*ctx
, tree t
,
6764 bool *non_constant_p
, bool *overflow_p
)
6766 enum tree_code code
= TREE_CODE (t
);
6767 tree type
= TREE_TYPE (t
);
6768 tree op
= TREE_OPERAND (t
, 0);
6769 tree offset
= TREE_OPERAND (t
, 1);
6770 gcc_assert (TREE_CONSTANT (offset
));
6772 /* OFFSET is constant, but perhaps not constant enough. We need to
6773 e.g. bash FLOAT_EXPRs to REAL_CSTs. */
6774 offset
= fold_simple (offset
);
6776 /* The operand as an lvalue. */
6777 op
= cxx_eval_constant_expression (ctx
, op
, vc_glvalue
,
6778 non_constant_p
, overflow_p
);
6780 /* The operand as an rvalue. */
6782 = cxx_eval_constant_expression (ctx
, op
, vc_prvalue
,
6783 non_constant_p
, overflow_p
);
6784 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
6785 a local array in a constexpr function. */
6786 bool ptr
= INDIRECT_TYPE_P (TREE_TYPE (val
));
6788 VERIFY_CONSTANT (val
);
6790 /* The modified value. */
6791 bool inc
= (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
);
6793 if (INDIRECT_TYPE_P (type
))
6795 /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
6796 offset
= convert_to_ptrofftype (offset
);
6798 offset
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (offset
), offset
);
6799 mod
= fold_build2 (POINTER_PLUS_EXPR
, type
, val
, offset
);
6801 else if (c_promoting_integer_type_p (type
)
6802 && !TYPE_UNSIGNED (type
)
6803 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
6805 offset
= fold_convert (integer_type_node
, offset
);
6806 mod
= fold_convert (integer_type_node
, val
);
6807 tree t
= fold_build2 (inc
? PLUS_EXPR
: MINUS_EXPR
, integer_type_node
,
6809 mod
= fold_convert (type
, t
);
6810 if (TREE_OVERFLOW_P (mod
) && !TREE_OVERFLOW_P (t
))
6811 TREE_OVERFLOW (mod
) = false;
6814 mod
= fold_build2 (inc
? PLUS_EXPR
: MINUS_EXPR
, type
, val
, offset
);
6816 VERIFY_CONSTANT (mod
);
6818 /* Storing the modified value. */
6819 tree store
= build2_loc (cp_expr_loc_or_loc (t
, input_location
),
6820 MODIFY_EXPR
, type
, op
, mod
);
6821 mod
= cxx_eval_constant_expression (ctx
, store
, lval
,
6822 non_constant_p
, overflow_p
);
6824 if (*non_constant_p
)
6827 /* And the value of the expression. */
6828 if (code
== PREINCREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
6829 /* Prefix ops are lvalues, but the caller might want an rvalue;
6830 lval has already been taken into account in the store above. */
6833 /* Postfix ops are rvalues. */
6837 /* Predicates for the meaning of *jump_target. */
6840 returns (tree
*jump_target
)
6843 && TREE_CODE (*jump_target
) == RETURN_EXPR
;
6847 breaks (tree
*jump_target
)
6850 && ((TREE_CODE (*jump_target
) == LABEL_DECL
6851 && LABEL_DECL_BREAK (*jump_target
))
6852 || TREE_CODE (*jump_target
) == BREAK_STMT
6853 || TREE_CODE (*jump_target
) == EXIT_EXPR
);
6857 continues (tree
*jump_target
)
6860 && ((TREE_CODE (*jump_target
) == LABEL_DECL
6861 && LABEL_DECL_CONTINUE (*jump_target
))
6862 || TREE_CODE (*jump_target
) == CONTINUE_STMT
);
6867 switches (tree
*jump_target
)
6870 && TREE_CODE (*jump_target
) == INTEGER_CST
;
6873 /* Subroutine of cxx_eval_statement_list. Determine whether the statement
6874 STMT matches *jump_target. If we're looking for a case label and we see
6875 the default label, note it in ctx->css_state. */
6878 label_matches (const constexpr_ctx
*ctx
, tree
*jump_target
, tree stmt
)
6880 switch (TREE_CODE (*jump_target
))
6883 if (TREE_CODE (stmt
) == LABEL_EXPR
6884 && LABEL_EXPR_LABEL (stmt
) == *jump_target
)
6889 if (TREE_CODE (stmt
) == CASE_LABEL_EXPR
)
6891 gcc_assert (ctx
->css_state
!= NULL
);
6892 if (!CASE_LOW (stmt
))
6894 /* default: should appear just once in a SWITCH_EXPR
6895 body (excluding nested SWITCH_EXPR). */
6896 gcc_assert (*ctx
->css_state
!= css_default_seen
);
6897 /* When evaluating SWITCH_EXPR body for the second time,
6898 return true for the default: label. */
6899 if (*ctx
->css_state
== css_default_processing
)
6901 *ctx
->css_state
= css_default_seen
;
6903 else if (CASE_HIGH (stmt
))
6905 if (tree_int_cst_le (CASE_LOW (stmt
), *jump_target
)
6906 && tree_int_cst_le (*jump_target
, CASE_HIGH (stmt
)))
6909 else if (tree_int_cst_equal (*jump_target
, CASE_LOW (stmt
)))
6916 /* These two are handled directly in cxx_eval_loop_expr by testing
6917 breaks (jump_target) or continues (jump_target). */
6926 /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
6927 semantics, for switch, break, continue, and return. */
6930 cxx_eval_statement_list (const constexpr_ctx
*ctx
, tree t
,
6931 bool *non_constant_p
, bool *overflow_p
,
6935 /* In a statement-expression we want to return the last value.
6936 For empty statement expression return void_node. */
6940 local_target
= NULL_TREE
;
6941 jump_target
= &local_target
;
6943 for (tree_stmt_iterator i
= tsi_start (t
); !tsi_end_p (i
); ++i
)
6947 /* We've found a continue, so skip everything until we reach
6948 the label its jumping to. */
6949 if (continues (jump_target
))
6951 if (label_matches (ctx
, jump_target
, stmt
))
6953 *jump_target
= NULL_TREE
;
6957 if (TREE_CODE (stmt
) == DEBUG_BEGIN_STMT
)
6960 value_cat lval
= vc_discard
;
6961 /* The result of a statement-expression is not wrapped in EXPR_STMT. */
6962 if (tsi_one_before_end_p (i
) && TREE_CODE (stmt
) != EXPR_STMT
)
6965 r
= cxx_eval_constant_expression (ctx
, stmt
, lval
,
6966 non_constant_p
, overflow_p
,
6968 if (*non_constant_p
)
6970 if (returns (jump_target
) || breaks (jump_target
))
6973 if (*jump_target
&& jump_target
== &local_target
)
6975 /* We aren't communicating the jump to our caller, so give up. We don't
6976 need to support evaluation of jumps out of statement-exprs. */
6978 error_at (cp_expr_loc_or_input_loc (r
),
6979 "statement is not a constant expression");
6980 *non_constant_p
= true;
6985 /* Evaluate a LOOP_EXPR for side-effects. Handles break and return
6986 semantics; continue semantics are covered by cxx_eval_statement_list. */
6989 cxx_eval_loop_expr (const constexpr_ctx
*ctx
, tree t
,
6990 bool *non_constant_p
, bool *overflow_p
,
6996 local_target
= NULL_TREE
;
6997 jump_target
= &local_target
;
7000 tree body
, cond
= NULL_TREE
, expr
= NULL_TREE
;
7002 switch (TREE_CODE (t
))
7005 body
= LOOP_EXPR_BODY (t
);
7012 body
= WHILE_BODY (t
);
7013 cond
= WHILE_COND (t
);
7017 if (FOR_INIT_STMT (t
))
7018 cxx_eval_constant_expression (ctx
, FOR_INIT_STMT (t
), vc_discard
,
7019 non_constant_p
, overflow_p
, jump_target
);
7020 if (*non_constant_p
)
7022 body
= FOR_BODY (t
);
7023 cond
= FOR_COND (t
);
7024 expr
= FOR_EXPR (t
);
7035 cxx_eval_constant_expression (ctx
, body
, vc_discard
,
7036 non_constant_p
, overflow_p
,
7038 if (breaks (jump_target
))
7040 *jump_target
= NULL_TREE
;
7044 if (TREE_CODE (t
) != LOOP_EXPR
&& continues (jump_target
))
7045 *jump_target
= NULL_TREE
;
7048 cxx_eval_constant_expression (ctx
, expr
, vc_prvalue
,
7049 non_constant_p
, overflow_p
,
7056 = cxx_eval_constant_expression (ctx
, cond
, vc_prvalue
,
7057 non_constant_p
, overflow_p
,
7061 if (verify_constant (res
, ctx
->quiet
, non_constant_p
,
7064 if (integer_zerop (res
))
7068 gcc_assert (*jump_target
);
7071 if (++count
>= constexpr_loop_limit
)
7074 error_at (cp_expr_loc_or_input_loc (t
),
7075 "%<constexpr%> loop iteration count exceeds limit of %d "
7076 "(use %<-fconstexpr-loop-limit=%> to increase the limit)",
7077 constexpr_loop_limit
);
7078 *non_constant_p
= true;
7082 while (!returns (jump_target
)
7083 && !breaks (jump_target
)
7084 && !continues (jump_target
)
7085 && (!switches (jump_target
) || count
== 0)
7086 && !*non_constant_p
);
7091 /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
7095 cxx_eval_switch_expr (const constexpr_ctx
*ctx
, tree t
,
7096 bool *non_constant_p
, bool *overflow_p
,
7100 = TREE_CODE (t
) == SWITCH_STMT
? SWITCH_STMT_COND (t
) : SWITCH_COND (t
);
7101 cond
= cxx_eval_constant_expression (ctx
, cond
, vc_prvalue
,
7102 non_constant_p
, overflow_p
);
7103 VERIFY_CONSTANT (cond
);
7104 if (TREE_CODE (cond
) != INTEGER_CST
)
7106 /* If the condition doesn't reduce to an INTEGER_CST it isn't a usable
7107 switch condition even if it's constant enough for other things
7109 gcc_checking_assert (ctx
->quiet
);
7110 *non_constant_p
= true;
7114 *jump_target
= cond
;
7117 = TREE_CODE (t
) == SWITCH_STMT
? SWITCH_STMT_BODY (t
) : SWITCH_BODY (t
);
7118 constexpr_ctx new_ctx
= *ctx
;
7119 constexpr_switch_state css
= css_default_not_seen
;
7120 new_ctx
.css_state
= &css
;
7121 cxx_eval_constant_expression (&new_ctx
, body
, vc_discard
,
7122 non_constant_p
, overflow_p
, jump_target
);
7123 if (switches (jump_target
) && css
== css_default_seen
)
7125 /* If the SWITCH_EXPR body has default: label, process it once again,
7126 this time instructing label_matches to return true for default:
7127 label on switches (jump_target). */
7128 css
= css_default_processing
;
7129 cxx_eval_constant_expression (&new_ctx
, body
, vc_discard
,
7130 non_constant_p
, overflow_p
, jump_target
);
7132 if (breaks (jump_target
) || switches (jump_target
))
7133 *jump_target
= NULL_TREE
;
7137 /* Find the object of TYPE under initialization in CTX. */
7140 lookup_placeholder (const constexpr_ctx
*ctx
, value_cat lval
, tree type
)
7145 /* Prefer the outermost matching object, but don't cross
7146 CONSTRUCTOR_PLACEHOLDER_BOUNDARY constructors. */
7147 if (ctx
->ctor
&& !CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx
->ctor
))
7148 if (tree outer_ob
= lookup_placeholder (ctx
->parent
, lval
, type
))
7151 /* We could use ctx->object unconditionally, but using ctx->ctor when we
7152 can is a minor optimization. */
7153 if (!lval
&& ctx
->ctor
&& same_type_p (TREE_TYPE (ctx
->ctor
), type
))
7159 /* Since an object cannot have a field of its own type, we can search outward
7160 from ctx->object to find the unique containing object of TYPE. */
7161 tree ob
= ctx
->object
;
7164 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob
), type
))
7166 if (handled_component_p (ob
))
7167 ob
= TREE_OPERAND (ob
, 0);
7175 /* Complain about an attempt to evaluate inline assembly. If FUNDEF_P is
7176 true, we're checking a constexpr function body. */
7179 inline_asm_in_constexpr_error (location_t loc
, bool fundef_p
)
7181 auto_diagnostic_group d
;
7182 if (constexpr_error (loc
, fundef_p
, "inline assembly is not a "
7183 "constant expression"))
7184 inform (loc
, "only unevaluated inline assembly is allowed in a "
7185 "%<constexpr%> function in C++20");
7188 /* We're getting the constant value of DECL in a manifestly constant-evaluated
7189 context; maybe complain about that. */
7192 maybe_warn_about_constant_value (location_t loc
, tree decl
)
7194 static bool explained
= false;
7195 if (cxx_dialect
>= cxx17
7196 && warn_interference_size
7197 && !OPTION_SET_P (param_destruct_interfere_size
)
7198 && DECL_CONTEXT (decl
) == std_node
7199 && id_equal (DECL_NAME (decl
), "hardware_destructive_interference_size")
7200 && (LOCATION_FILE (input_location
) != main_input_filename
7201 || module_exporting_p ())
7202 && warning_at (loc
, OPT_Winterference_size
, "use of %qD", decl
)
7206 inform (loc
, "its value can vary between compiler versions or "
7207 "with different %<-mtune%> or %<-mcpu%> flags");
7208 inform (loc
, "if this use is part of a public ABI, change it to "
7209 "instead use a constant variable you define");
7210 inform (loc
, "the default value for the current CPU tuning "
7211 "is %d bytes", param_destruct_interfere_size
);
7212 inform (loc
, "you can stabilize this value with %<--param "
7213 "hardware_destructive_interference_size=%d%>, or disable "
7214 "this warning with %<-Wno-interference-size%>",
7215 param_destruct_interfere_size
);
7219 /* For element type ELT_TYPE, return the appropriate type of the heap object
7220 containing such element(s). COOKIE_SIZE is NULL or the size of cookie
7221 in bytes. If COOKIE_SIZE is NULL, return array type
7222 ELT_TYPE[FULL_SIZE / sizeof(ELT_TYPE)], otherwise return
7223 struct { size_t[COOKIE_SIZE/sizeof(size_t)]; ELT_TYPE[N]; }
7224 where N is computed such that the size of the struct fits into FULL_SIZE.
7225 If ARG_SIZE is non-NULL, it is the first argument to the new operator.
7226 It should be passed if ELT_TYPE is zero sized type in which case FULL_SIZE
7227 will be also 0 and so it is not possible to determine the actual array
7228 size. CTX, NON_CONSTANT_P and OVERFLOW_P are used during constant
7229 expression evaluation of subexpressions of ARG_SIZE. */
7232 build_new_constexpr_heap_type (const constexpr_ctx
*ctx
, tree elt_type
,
7233 tree cookie_size
, tree full_size
, tree arg_size
,
7234 bool *non_constant_p
, bool *overflow_p
)
7236 gcc_assert (cookie_size
== NULL_TREE
|| tree_fits_uhwi_p (cookie_size
));
7237 gcc_assert (tree_fits_uhwi_p (full_size
));
7238 unsigned HOST_WIDE_INT csz
= cookie_size
? tree_to_uhwi (cookie_size
) : 0;
7241 STRIP_NOPS (arg_size
);
7244 if (TREE_CODE (arg_size
) != PLUS_EXPR
)
7245 arg_size
= NULL_TREE
;
7246 else if (TREE_CODE (TREE_OPERAND (arg_size
, 0)) == INTEGER_CST
7247 && tree_int_cst_equal (cookie_size
,
7248 TREE_OPERAND (arg_size
, 0)))
7250 arg_size
= TREE_OPERAND (arg_size
, 1);
7251 STRIP_NOPS (arg_size
);
7253 else if (TREE_CODE (TREE_OPERAND (arg_size
, 1)) == INTEGER_CST
7254 && tree_int_cst_equal (cookie_size
,
7255 TREE_OPERAND (arg_size
, 1)))
7257 arg_size
= TREE_OPERAND (arg_size
, 0);
7258 STRIP_NOPS (arg_size
);
7261 arg_size
= NULL_TREE
;
7263 if (arg_size
&& TREE_CODE (arg_size
) == MULT_EXPR
)
7265 tree op0
= TREE_OPERAND (arg_size
, 0);
7266 tree op1
= TREE_OPERAND (arg_size
, 1);
7267 if (integer_zerop (op0
))
7269 = cxx_eval_constant_expression (ctx
, op1
, vc_prvalue
,
7270 non_constant_p
, overflow_p
);
7271 else if (integer_zerop (op1
))
7273 = cxx_eval_constant_expression (ctx
, op0
, vc_prvalue
,
7274 non_constant_p
, overflow_p
);
7276 arg_size
= NULL_TREE
;
7279 arg_size
= NULL_TREE
;
7282 unsigned HOST_WIDE_INT fsz
= tree_to_uhwi (arg_size
? arg_size
: full_size
);
7285 unsigned HOST_WIDE_INT esz
= int_size_in_bytes (elt_type
);
7286 gcc_assert (fsz
>= csz
);
7291 tree itype2
= build_index_type (size_int (fsz
- 1));
7293 return build_cplus_array_type (elt_type
, itype2
);
7294 return build_new_constexpr_heap_type (elt_type
, cookie_size
, itype2
);
7297 /* Attempt to reduce the expression T to a constant value.
7298 On failure, issue diagnostic and return error_mark_node. */
7299 /* FIXME unify with c_fully_fold */
7300 /* FIXME overflow_p is too global */
7303 cxx_eval_constant_expression (const constexpr_ctx
*ctx
, tree t
,
7305 bool *non_constant_p
, bool *overflow_p
,
7306 tree
*jump_target
/* = NULL */)
7308 if (jump_target
&& *jump_target
)
7310 /* If we are jumping, ignore all statements/expressions except those
7311 that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */
7312 switch (TREE_CODE (t
))
7315 case STATEMENT_LIST
:
7324 case CASE_LABEL_EXPR
:
7325 if (label_matches (ctx
, jump_target
, t
))
7327 *jump_target
= NULL_TREE
;
7333 if (error_operand_p (t
))
7335 *non_constant_p
= true;
7339 /* Change the input location to the currently processed expression for
7340 better error messages when a subexpression has no location. */
7341 location_t loc
= cp_expr_loc_or_input_loc (t
);
7342 iloc_sentinel
sentinel (loc
);
7344 STRIP_ANY_LOCATION_WRAPPER (t
);
7346 if (CONSTANT_CLASS_P (t
))
7348 if (TREE_OVERFLOW (t
))
7351 permerror (input_location
, "overflow in constant expression");
7352 if (!flag_permissive
|| ctx
->quiet
)
7356 if (TREE_CODE (t
) == INTEGER_CST
7357 && TYPE_PTR_P (TREE_TYPE (t
))
7358 /* INTEGER_CST with pointer-to-method type is only used
7359 for a virtual method in a pointer to member function.
7360 Don't reject those. */
7361 && TREE_CODE (TREE_TYPE (TREE_TYPE (t
))) != METHOD_TYPE
7362 && !integer_zerop (t
))
7365 error ("value %qE of type %qT is not a constant expression",
7367 *non_constant_p
= true;
7373 /* Avoid excessively long constexpr evaluations. */
7374 if (++ctx
->global
->constexpr_ops_count
>= constexpr_ops_limit
)
7378 "%<constexpr%> evaluation operation count exceeds limit of "
7379 "%wd (use %<-fconstexpr-ops-limit=%> to increase the limit)",
7380 constexpr_ops_limit
);
7381 ctx
->global
->constexpr_ops_count
= INTTYPE_MINIMUM (HOST_WIDE_INT
);
7382 *non_constant_p
= true;
7386 constexpr_ctx new_ctx
;
7389 tree_code tcode
= TREE_CODE (t
);
7395 /* We ask for an rvalue for the RESULT_DECL when indirecting
7396 through an invisible reference, or in named return value
7398 if (tree v
= ctx
->global
->get_value (t
))
7403 error ("%qE is not a constant expression", t
);
7404 *non_constant_p
= true;
7409 if (DECL_HAS_VALUE_EXPR_P (t
))
7411 if (is_normal_capture_proxy (t
)
7412 && current_function_decl
== DECL_CONTEXT (t
))
7414 /* Function parms aren't constexpr within the function
7415 definition, so don't try to look at the closure. But if the
7416 captured variable is constant, try to evaluate it directly. */
7417 r
= DECL_CAPTURED_VARIABLE (t
);
7418 tree type
= TREE_TYPE (t
);
7419 if (TYPE_REF_P (type
) != TYPE_REF_P (TREE_TYPE (r
)))
7421 /* Adjust r to match the reference-ness of t. */
7422 if (TYPE_REF_P (type
))
7423 r
= build_address (r
);
7425 r
= convert_from_reference (r
);
7429 r
= DECL_VALUE_EXPR (t
);
7430 return cxx_eval_constant_expression (ctx
, r
, lval
, non_constant_p
,
7435 /* We used to not check lval for CONST_DECL, but darwin.cc uses
7436 CONST_DECL for aggregate constants. */
7439 else if (t
== ctx
->object
)
7443 if (tree v
= ctx
->global
->get_value (t
))
7448 if (ctx
->global
->is_outside_lifetime (t
))
7451 outside_lifetime_error (loc
, t
);
7452 *non_constant_p
= true;
7456 if (ctx
->manifestly_const_eval
== mce_true
)
7457 maybe_warn_about_constant_value (loc
, t
);
7458 if (COMPLETE_TYPE_P (TREE_TYPE (t
))
7459 && is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
7461 /* If the class is empty, we aren't actually loading anything. */
7462 r
= build_constructor (TREE_TYPE (t
), NULL
);
7463 TREE_CONSTANT (r
) = true;
7465 else if (ctx
->strict
)
7466 r
= decl_really_constant_value (t
, /*unshare_p=*/false);
7468 r
= decl_constant_value (t
, /*unshare_p=*/false);
7469 if (TREE_CODE (r
) == TARGET_EXPR
7470 && TREE_CODE (TARGET_EXPR_INITIAL (r
)) == CONSTRUCTOR
)
7471 r
= TARGET_EXPR_INITIAL (r
);
7473 /* P2280 allows references to unknown. */
7474 && !(VAR_P (t
) && TYPE_REF_P (TREE_TYPE (t
))))
7477 non_const_var_error (loc
, r
, /*fundef_p*/false);
7478 *non_constant_p
= true;
7482 case DEBUG_BEGIN_STMT
:
7483 /* ??? It might be nice to retain this information somehow, so
7484 as to be able to step into a constexpr function call. */
7491 case CASE_LABEL_EXPR
:
7496 if (lval
&& !TYPE_REF_P (TREE_TYPE (t
)))
7498 else if (tree v
= ctx
->global
->get_value (t
))
7501 /* Defer in case this is only used for its type. */;
7502 else if (ctx
->global
->is_outside_lifetime (t
))
7505 outside_lifetime_error (loc
, t
);
7506 *non_constant_p
= true;
7509 else if (COMPLETE_TYPE_P (TREE_TYPE (t
))
7510 && is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
7512 /* If the class is empty, we aren't actually loading anything. */
7513 r
= build_constructor (TREE_TYPE (t
), NULL
);
7514 TREE_CONSTANT (r
) = true;
7516 else if (TYPE_REF_P (TREE_TYPE (t
)))
7517 /* P2280 allows references to unknown... */;
7518 else if (is_this_parameter (t
))
7519 /* ...as well as the this pointer. */;
7523 error ("%qE is not a constant expression", t
);
7524 *non_constant_p
= true;
7529 case AGGR_INIT_EXPR
:
7530 r
= cxx_eval_call_expression (ctx
, t
, lval
,
7531 non_constant_p
, overflow_p
);
7536 r
= DECL_EXPR_DECL (t
);
7537 if (TREE_CODE (r
) == USING_DECL
)
7545 || (CP_DECL_THREAD_LOCAL_P (r
) && !DECL_REALLY_EXTERN (r
)))
7546 /* Allow __FUNCTION__ etc. */
7547 && !DECL_ARTIFICIAL (r
)
7548 && !decl_constant_var_p (r
))
7552 if (CP_DECL_THREAD_LOCAL_P (r
))
7553 error_at (loc
, "control passes through definition of %qD "
7554 "with thread storage duration", r
);
7556 error_at (loc
, "control passes through definition of %qD "
7557 "with static storage duration", r
);
7559 *non_constant_p
= true;
7563 /* make_rtl_for_nonlocal_decl could have deferred emission of
7564 a local static var, but if it appears in a statement expression
7565 which is constant expression evaluated to e.g. just the address
7566 of the variable, its DECL_EXPR will never be seen during
7567 gimple lowering's record_vars_into as the statement expression
7568 will not be in the IL at all. */
7571 && !DECL_REALLY_EXTERN (r
)
7572 && DECL_FUNCTION_SCOPE_P (r
)
7573 && !var_in_maybe_constexpr_fn (r
)
7574 && decl_constant_var_p (r
))
7576 varpool_node
*node
= varpool_node::get (r
);
7577 if (node
== NULL
|| !node
->definition
)
7578 rest_of_decl_compilation (r
, 0, at_eof
);
7581 if (AGGREGATE_TYPE_P (TREE_TYPE (r
))
7582 || VECTOR_TYPE_P (TREE_TYPE (r
)))
7586 new_ctx
.ctor
= build_constructor (TREE_TYPE (r
), NULL
);
7587 CONSTRUCTOR_NO_CLEARING (new_ctx
.ctor
) = true;
7588 ctx
->global
->put_value (r
, new_ctx
.ctor
);
7592 if (tree init
= DECL_INITIAL (r
))
7594 init
= cxx_eval_constant_expression (ctx
, init
, vc_prvalue
,
7595 non_constant_p
, overflow_p
);
7596 /* Don't share a CONSTRUCTOR that might be changed. */
7597 init
= unshare_constructor (init
);
7598 /* Remember that a constant object's constructor has already
7600 if (CLASS_TYPE_P (TREE_TYPE (r
))
7601 && CP_TYPE_CONST_P (TREE_TYPE (r
)))
7602 TREE_READONLY (init
) = true;
7603 ctx
->global
->put_value (r
, init
);
7605 else if (ctx
== &new_ctx
)
7606 /* We gave it a CONSTRUCTOR above. */;
7608 ctx
->global
->put_value (r
, NULL_TREE
);
7614 tree type
= TREE_TYPE (t
);
7616 if (!literal_type_p (type
))
7620 auto_diagnostic_group d
;
7621 error ("temporary of non-literal type %qT in a "
7622 "constant expression", type
);
7623 explain_non_literal_class (type
);
7625 *non_constant_p
= true;
7628 gcc_checking_assert (!TARGET_EXPR_DIRECT_INIT_P (t
));
7629 /* Avoid evaluating a TARGET_EXPR more than once. */
7630 tree slot
= TARGET_EXPR_SLOT (t
);
7631 if (tree v
= ctx
->global
->get_value (slot
))
7638 if ((AGGREGATE_TYPE_P (type
) || VECTOR_TYPE_P (type
)))
7640 /* We're being expanded without an explicit target, so start
7641 initializing a new object; expansion with an explicit target
7642 strips the TARGET_EXPR before we get here. */
7644 /* Link CTX to NEW_CTX so that lookup_placeholder can resolve
7645 any PLACEHOLDER_EXPR within the initializer that refers to the
7646 former object under construction. */
7647 new_ctx
.parent
= ctx
;
7648 new_ctx
.ctor
= build_constructor (type
, NULL
);
7649 CONSTRUCTOR_NO_CLEARING (new_ctx
.ctor
) = true;
7650 new_ctx
.object
= slot
;
7651 ctx
->global
->put_value (new_ctx
.object
, new_ctx
.ctor
);
7654 /* Pass vc_prvalue because this indicates
7655 initialization of a temporary. */
7656 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1), vc_prvalue
,
7657 non_constant_p
, overflow_p
);
7658 if (*non_constant_p
)
7660 /* If the initializer is complex, evaluate it to initialize slot. */
7661 bool is_complex
= target_expr_needs_replace (t
);
7664 r
= unshare_constructor (r
);
7665 /* Adjust the type of the result to the type of the temporary. */
7666 r
= adjust_temp_type (type
, r
);
7667 ctx
->global
->put_value (slot
, r
);
7669 if (TARGET_EXPR_CLEANUP (t
) && !CLEANUP_EH_ONLY (t
))
7670 ctx
->global
->cleanups
->safe_push (TARGET_EXPR_CLEANUP (t
));
7671 if (ctx
->save_exprs
)
7672 ctx
->save_exprs
->safe_push (slot
);
7676 r
= ctx
->global
->get_value (slot
);
7682 gcc_assert (jump_target
== NULL
|| *jump_target
== NULL_TREE
);
7683 r
= cxx_eval_store_expression (ctx
, t
, lval
,
7684 non_constant_p
, overflow_p
);
7688 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
7690 non_constant_p
, overflow_p
);
7694 if (TREE_OPERAND (t
, 0) != NULL_TREE
)
7695 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
7697 non_constant_p
, overflow_p
);
7705 /* Can happen with ({ return true; }) && false; passed to
7706 maybe_constant_value. There is nothing to jump over in this
7707 case, and the bug will be diagnosed later. */
7708 gcc_assert (ctx
->quiet
);
7709 *non_constant_p
= true;
7714 /* Avoid evaluating a SAVE_EXPR more than once. */
7715 if (tree v
= ctx
->global
->get_value (t
))
7719 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), vc_prvalue
,
7720 non_constant_p
, overflow_p
);
7721 if (*non_constant_p
)
7723 ctx
->global
->put_value (t
, r
);
7724 if (ctx
->save_exprs
)
7725 ctx
->save_exprs
->safe_push (t
);
7729 case TRY_CATCH_EXPR
:
7730 if (TREE_OPERAND (t
, 0) == NULL_TREE
)
7736 case NON_LVALUE_EXPR
:
7738 case MUST_NOT_THROW_EXPR
:
7741 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
7743 non_constant_p
, overflow_p
,
7747 case CLEANUP_POINT_EXPR
:
7749 auto_vec
<tree
, 2> cleanups
;
7750 vec
<tree
> *prev_cleanups
= ctx
->global
->cleanups
;
7751 ctx
->global
->cleanups
= &cleanups
;
7753 auto_vec
<tree
, 10> save_exprs
;
7754 constexpr_ctx new_ctx
= *ctx
;
7755 new_ctx
.save_exprs
= &save_exprs
;
7757 r
= cxx_eval_constant_expression (&new_ctx
, TREE_OPERAND (t
, 0),
7759 non_constant_p
, overflow_p
,
7762 ctx
->global
->cleanups
= prev_cleanups
;
7765 /* Evaluate the cleanups. */
7766 FOR_EACH_VEC_ELT_REVERSE (cleanups
, i
, cleanup
)
7767 cxx_eval_constant_expression (&new_ctx
, cleanup
, vc_discard
,
7768 non_constant_p
, overflow_p
);
7770 /* Forget SAVE_EXPRs and TARGET_EXPRs created by this
7772 for (tree save_expr
: save_exprs
)
7773 destroy_value_checked (ctx
, save_expr
, non_constant_p
);
7777 case TRY_FINALLY_EXPR
:
7778 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), lval
,
7779 non_constant_p
, overflow_p
,
7781 if (!*non_constant_p
)
7782 /* Also evaluate the cleanup. */
7783 cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1), vc_discard
,
7784 non_constant_p
, overflow_p
);
7788 r
= cxx_eval_constant_expression (ctx
, CLEANUP_BODY (t
), lval
,
7789 non_constant_p
, overflow_p
,
7791 if (!CLEANUP_EH_ONLY (t
) && !*non_constant_p
)
7793 iloc_sentinel
ils (loc
);
7794 /* Also evaluate the cleanup. */
7795 cxx_eval_constant_expression (ctx
, CLEANUP_EXPR (t
), vc_discard
,
7796 non_constant_p
, overflow_p
);
7800 /* These differ from cxx_eval_unary_expression in that this doesn't
7801 check for a constant operand or result; an address can be
7802 constant without its operand being, and vice versa. */
7805 r
= cxx_eval_indirect_ref (ctx
, t
, lval
,
7806 non_constant_p
, overflow_p
);
7811 tree oldop
= TREE_OPERAND (t
, 0);
7812 tree op
= cxx_eval_constant_expression (ctx
, oldop
, vc_glvalue
,
7813 non_constant_p
, overflow_p
);
7814 /* Don't VERIFY_CONSTANT here. */
7815 if (*non_constant_p
)
7817 gcc_checking_assert (TREE_CODE (op
) != CONSTRUCTOR
);
7818 /* This function does more aggressive folding than fold itself. */
7819 r
= build_fold_addr_expr_with_type (op
, TREE_TYPE (t
));
7820 if (TREE_CODE (r
) == ADDR_EXPR
&& TREE_OPERAND (r
, 0) == oldop
)
7832 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), lval
,
7833 non_constant_p
, overflow_p
);
7834 if (r
== error_mark_node
)
7836 else if (r
== TREE_OPERAND (t
, 0) || lval
== vc_discard
)
7839 r
= fold_build1 (TREE_CODE (t
), TREE_TYPE (t
), r
);
7844 case FIX_TRUNC_EXPR
:
7850 case TRUTH_NOT_EXPR
:
7851 case FIXED_CONVERT_EXPR
:
7852 r
= cxx_eval_unary_expression (ctx
, t
, lval
,
7853 non_constant_p
, overflow_p
);
7857 r
= fold_sizeof_expr (t
);
7858 /* In a template, fold_sizeof_expr may merely create a new SIZEOF_EXPR,
7859 which could lead to an infinite recursion. */
7860 if (TREE_CODE (r
) != SIZEOF_EXPR
)
7861 r
= cxx_eval_constant_expression (ctx
, r
, lval
,
7862 non_constant_p
, overflow_p
,
7866 *non_constant_p
= true;
7867 gcc_assert (ctx
->quiet
);
7874 /* check_return_expr sometimes wraps a TARGET_EXPR in a
7875 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
7876 introduced by build_call_a. */
7877 tree op0
= TREE_OPERAND (t
, 0);
7878 tree op1
= TREE_OPERAND (t
, 1);
7880 if ((TREE_CODE (op0
) == TARGET_EXPR
&& op1
== TARGET_EXPR_SLOT (op0
))
7881 || TREE_CODE (op1
) == EMPTY_CLASS_EXPR
)
7882 r
= cxx_eval_constant_expression (ctx
, op0
,
7883 lval
, non_constant_p
, overflow_p
,
7887 /* Check that the LHS is constant and then discard it. */
7888 cxx_eval_constant_expression (ctx
, op0
, vc_discard
,
7889 non_constant_p
, overflow_p
,
7891 if (*non_constant_p
)
7893 op1
= TREE_OPERAND (t
, 1);
7894 r
= cxx_eval_constant_expression (ctx
, op1
,
7895 lval
, non_constant_p
, overflow_p
,
7901 case POINTER_PLUS_EXPR
:
7902 case POINTER_DIFF_EXPR
:
7906 case TRUNC_DIV_EXPR
:
7908 case FLOOR_DIV_EXPR
:
7909 case ROUND_DIV_EXPR
:
7910 case TRUNC_MOD_EXPR
:
7912 case ROUND_MOD_EXPR
:
7914 case EXACT_DIV_EXPR
:
7924 case TRUTH_XOR_EXPR
:
7931 case SPACESHIP_EXPR
:
7932 case UNORDERED_EXPR
:
7942 r
= cxx_eval_binary_expression (ctx
, t
, lval
,
7943 non_constant_p
, overflow_p
);
7946 /* fold can introduce non-IF versions of these; still treat them as
7947 short-circuiting. */
7948 case TRUTH_AND_EXPR
:
7949 case TRUTH_ANDIF_EXPR
:
7950 r
= cxx_eval_logical_expression (ctx
, t
, boolean_false_node
,
7952 non_constant_p
, overflow_p
);
7956 case TRUTH_ORIF_EXPR
:
7957 r
= cxx_eval_logical_expression (ctx
, t
, boolean_true_node
,
7959 non_constant_p
, overflow_p
);
7963 r
= cxx_eval_array_reference (ctx
, t
, lval
,
7964 non_constant_p
, overflow_p
);
7968 if (is_overloaded_fn (t
))
7970 /* We can only get here in checking mode via
7971 build_non_dependent_expr, because any expression that
7972 calls or takes the address of the function will have
7973 pulled a FUNCTION_DECL out of the COMPONENT_REF. */
7974 gcc_checking_assert (ctx
->quiet
|| errorcount
);
7975 *non_constant_p
= true;
7978 r
= cxx_eval_component_reference (ctx
, t
, lval
,
7979 non_constant_p
, overflow_p
);
7983 r
= cxx_eval_bit_field_ref (ctx
, t
, lval
,
7984 non_constant_p
, overflow_p
);
7989 if (jump_target
&& *jump_target
)
7991 tree orig_jump
= *jump_target
;
7992 tree arg
= ((TREE_CODE (t
) != IF_STMT
|| TREE_OPERAND (t
, 1))
7993 ? TREE_OPERAND (t
, 1) : void_node
);
7994 /* When jumping to a label, the label might be either in the
7995 then or else blocks, so process then block first in skipping
7996 mode first, and if we are still in the skipping mode at its end,
7997 process the else block too. */
7998 r
= cxx_eval_constant_expression (ctx
, arg
, lval
, non_constant_p
,
7999 overflow_p
, jump_target
);
8000 /* It's possible that we found the label in the then block. But
8001 it could have been followed by another jumping statement, e.g.
8002 say we're looking for case 1:
8005 // skipped statements
8006 case 1:; // clears up *jump_target
8007 return 1; // and sets it to a RETURN_EXPR
8010 in which case we need not go looking to the else block.
8011 (goto is not allowed in a constexpr function.) */
8012 if (*jump_target
== orig_jump
)
8014 arg
= ((TREE_CODE (t
) != IF_STMT
|| TREE_OPERAND (t
, 2))
8015 ? TREE_OPERAND (t
, 2) : void_node
);
8016 r
= cxx_eval_constant_expression (ctx
, arg
, lval
, non_constant_p
,
8017 overflow_p
, jump_target
);
8021 r
= cxx_eval_conditional_expression (ctx
, t
, lval
,
8022 non_constant_p
, overflow_p
,
8026 r
= cxx_eval_vector_conditional_expression (ctx
, t
, non_constant_p
,
8031 if (TREE_CONSTANT (t
) && reduced_constant_expression_p (t
))
8033 /* Don't re-process a constant CONSTRUCTOR. */
8034 verify_constructor_flags (t
);
8035 if (TREE_CONSTANT (t
))
8038 r
= cxx_eval_bare_aggregate (ctx
, t
, lval
,
8039 non_constant_p
, overflow_p
);
8043 /* We can get this in a defaulted constructor for a class with a
8044 non-static data member of array type. Either the initializer will
8045 be NULL, meaning default-initialization, or it will be an lvalue
8046 or xvalue of the same type, meaning direct-initialization from the
8047 corresponding member. */
8048 r
= cxx_eval_vec_init (ctx
, t
, lval
,
8049 non_constant_p
, overflow_p
);
8053 r
= cxx_eval_trinary_expression (ctx
, t
, lval
,
8054 non_constant_p
, overflow_p
);
8058 gcc_assert (!REF_PARENTHESIZED_P (t
));
8059 /* A PAREN_EXPR resulting from __builtin_assoc_barrier has no effect in
8060 constant expressions since it's unaffected by -fassociative-math. */
8061 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), lval
,
8062 non_constant_p
, overflow_p
);
8066 if (REINTERPRET_CAST_P (t
))
8070 "%<reinterpret_cast%> is not a constant expression");
8071 *non_constant_p
= true;
8076 case VIEW_CONVERT_EXPR
:
8077 case UNARY_PLUS_EXPR
:
8079 tree oldop
= TREE_OPERAND (t
, 0);
8081 tree op
= cxx_eval_constant_expression (ctx
, oldop
,
8083 non_constant_p
, overflow_p
);
8084 if (*non_constant_p
)
8086 tree type
= TREE_TYPE (t
);
8088 if (VOID_TYPE_P (type
))
8091 if (TREE_CODE (t
) == CONVERT_EXPR
8092 && ARITHMETIC_TYPE_P (type
)
8093 && INDIRECT_TYPE_P (TREE_TYPE (op
))
8094 && ctx
->manifestly_const_eval
== mce_true
)
8098 "conversion from pointer type %qT to arithmetic type "
8099 "%qT in a constant expression", TREE_TYPE (op
), type
);
8100 *non_constant_p
= true;
8104 /* [expr.const]: a conversion from type cv void* to a pointer-to-object
8105 type cannot be part of a core constant expression as a resolution to
8107 if (TYPE_PTROB_P (type
)
8108 && TYPE_PTR_P (TREE_TYPE (op
))
8109 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (op
)))
8110 /* Inside a call to std::construct_at,
8111 std::allocator<T>::{,de}allocate, or
8112 std::source_location::current, we permit casting from void*
8113 because that is compiler-generated code. */
8114 && !is_std_construct_at (ctx
->call
)
8115 && !is_std_allocator_allocate (ctx
->call
)
8116 && !is_std_source_location_current (ctx
->call
))
8118 /* Likewise, don't error when casting from void* when OP is
8119 &heap uninit and similar. */
8120 tree sop
= tree_strip_nop_conversions (op
);
8121 tree decl
= NULL_TREE
;
8122 if (TREE_CODE (sop
) == ADDR_EXPR
)
8123 decl
= TREE_OPERAND (sop
, 0);
8126 && DECL_ARTIFICIAL (decl
)
8127 && (DECL_NAME (decl
) == heap_identifier
8128 || DECL_NAME (decl
) == heap_uninit_identifier
8129 || DECL_NAME (decl
) == heap_vec_identifier
8130 || DECL_NAME (decl
) == heap_vec_uninit_identifier
))
8132 /* P2738 (C++26): a conversion from a prvalue P of type "pointer to
8133 cv void" to a pointer-to-object type T unless P points to an
8134 object whose type is similar to T. */
8135 else if (cxx_dialect
> cxx23
)
8137 r
= cxx_fold_indirect_ref (ctx
, loc
, TREE_TYPE (type
), sop
);
8140 r
= build1 (ADDR_EXPR
, type
, r
);
8145 if (TREE_CODE (sop
) == ADDR_EXPR
)
8147 auto_diagnostic_group d
;
8148 error_at (loc
, "cast from %qT is not allowed in a "
8149 "constant expression because "
8150 "pointed-to type %qT is not similar to %qT",
8151 TREE_TYPE (op
), TREE_TYPE (TREE_TYPE (sop
)),
8153 tree obj
= build_fold_indirect_ref (sop
);
8154 inform (DECL_SOURCE_LOCATION (obj
),
8155 "pointed-to object declared here");
8159 gcc_assert (integer_zerop (sop
));
8160 error_at (loc
, "cast from %qT is not allowed in a "
8161 "constant expression because "
8162 "%qE does not point to an object",
8163 TREE_TYPE (op
), oldop
);
8166 *non_constant_p
= true;
8172 error_at (loc
, "cast from %qT is not allowed in a "
8173 "constant expression before C++26",
8175 *non_constant_p
= true;
8180 if (TREE_CODE (op
) == PTRMEM_CST
&& !TYPE_PTRMEM_P (type
))
8182 op
= cplus_expand_constant (op
);
8183 if (TREE_CODE (op
) == PTRMEM_CST
)
8186 error_at (loc
, "%qE is not a constant expression when the "
8187 "class %qT is still incomplete", op
,
8188 PTRMEM_CST_CLASS (op
));
8189 *non_constant_p
= true;
8194 if (TREE_CODE (op
) == PTRMEM_CST
&& tcode
== NOP_EXPR
)
8196 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (op
))
8197 && !can_convert_qual (type
, op
))
8198 op
= cplus_expand_constant (op
);
8199 return cp_fold_convert (type
, op
);
8202 if (INDIRECT_TYPE_P (type
) && TREE_CODE (op
) == INTEGER_CST
)
8204 if (integer_zerop (op
))
8206 if (TYPE_REF_P (type
))
8209 error_at (loc
, "dereferencing a null pointer");
8210 *non_constant_p
= true;
8216 /* This detects for example:
8217 reinterpret_cast<void*>(sizeof 0)
8220 error_at (loc
, "%<reinterpret_cast<%T>(%E)%> is not "
8221 "a constant expression",
8223 *non_constant_p
= true;
8228 if (INDIRECT_TYPE_P (type
)
8229 && TREE_CODE (op
) == NOP_EXPR
8230 && TREE_TYPE (op
) == ptr_type_node
8231 && TREE_CODE (TREE_OPERAND (op
, 0)) == ADDR_EXPR
8232 && VAR_P (TREE_OPERAND (TREE_OPERAND (op
, 0), 0))
8233 && (DECL_NAME (TREE_OPERAND (TREE_OPERAND (op
, 0),
8234 0)) == heap_uninit_identifier
8235 || DECL_NAME (TREE_OPERAND (TREE_OPERAND (op
, 0),
8236 0)) == heap_vec_uninit_identifier
))
8238 tree var
= TREE_OPERAND (TREE_OPERAND (op
, 0), 0);
8239 tree var_size
= TYPE_SIZE_UNIT (TREE_TYPE (var
));
8240 tree elt_type
= TREE_TYPE (type
);
8241 tree cookie_size
= NULL_TREE
;
8242 tree arg_size
= NULL_TREE
;
8243 if (TREE_CODE (elt_type
) == RECORD_TYPE
8244 && TYPE_NAME (elt_type
) == heap_identifier
)
8246 tree fld1
= TYPE_FIELDS (elt_type
);
8247 tree fld2
= DECL_CHAIN (fld1
);
8248 elt_type
= TREE_TYPE (TREE_TYPE (fld2
));
8249 cookie_size
= TYPE_SIZE_UNIT (TREE_TYPE (fld1
));
8252 = (DECL_NAME (var
) == heap_uninit_identifier
8253 ? heap_identifier
: heap_vec_identifier
);
8254 /* For zero sized elt_type, try to recover how many outer_nelts
8256 if ((cookie_size
? tree_int_cst_equal (var_size
, cookie_size
)
8257 : integer_zerop (var_size
))
8258 && !int_size_in_bytes (elt_type
)
8259 && TREE_CODE (oldop
) == CALL_EXPR
8260 && call_expr_nargs (oldop
) >= 1)
8261 if (tree fun
= get_function_named_in_call (oldop
))
8262 if (cxx_replaceable_global_alloc_fn (fun
)
8263 && IDENTIFIER_NEW_OP_P (DECL_NAME (fun
)))
8264 arg_size
= CALL_EXPR_ARG (oldop
, 0);
8266 = build_new_constexpr_heap_type (ctx
, elt_type
, cookie_size
,
8268 non_constant_p
, overflow_p
);
8269 TREE_TYPE (TREE_OPERAND (op
, 0))
8270 = build_pointer_type (TREE_TYPE (var
));
8273 if (op
== oldop
&& tcode
!= UNARY_PLUS_EXPR
)
8274 /* We didn't fold at the top so we could check for ptr-int
8280 /* Handle an array's bounds having been deduced after we built
8281 the wrapping expression. */
8282 if (same_type_ignoring_tlq_and_bounds_p (type
, TREE_TYPE (op
)))
8284 else if (sop
= tree_strip_nop_conversions (op
),
8285 sop
!= op
&& (same_type_ignoring_tlq_and_bounds_p
8286 (type
, TREE_TYPE (sop
))))
8288 else if (tcode
== UNARY_PLUS_EXPR
)
8289 r
= fold_convert (TREE_TYPE (t
), op
);
8291 r
= fold_build1 (tcode
, type
, op
);
8293 /* Conversion of an out-of-range value has implementation-defined
8294 behavior; the language considers it different from arithmetic
8295 overflow, which is undefined. */
8296 if (TREE_OVERFLOW_P (r
) && !TREE_OVERFLOW_P (op
))
8297 TREE_OVERFLOW (r
) = false;
8301 case EXCESS_PRECISION_EXPR
:
8303 tree oldop
= TREE_OPERAND (t
, 0);
8305 tree op
= cxx_eval_constant_expression (ctx
, oldop
,
8307 non_constant_p
, overflow_p
);
8308 if (*non_constant_p
)
8310 r
= fold_convert (TREE_TYPE (t
), op
);
8314 case EMPTY_CLASS_EXPR
:
8315 /* Handle EMPTY_CLASS_EXPR produced by build_call_a by lowering
8316 it to an appropriate CONSTRUCTOR. */
8317 return build_constructor (TREE_TYPE (t
), NULL
);
8319 case STATEMENT_LIST
:
8321 new_ctx
.ctor
= new_ctx
.object
= NULL_TREE
;
8322 return cxx_eval_statement_list (&new_ctx
, t
,
8323 non_constant_p
, overflow_p
, jump_target
);
8326 /* Pre-emptively clear the vars declared by this BIND_EXPR from the value
8327 map, so that when checking whether they're already destroyed later we
8328 don't get confused by remnants of previous calls. */
8329 for (tree decl
= BIND_EXPR_VARS (t
); decl
; decl
= DECL_CHAIN (decl
))
8330 ctx
->global
->clear_value (decl
);
8331 r
= cxx_eval_constant_expression (ctx
, BIND_EXPR_BODY (t
),
8333 non_constant_p
, overflow_p
,
8335 for (tree decl
= BIND_EXPR_VARS (t
); decl
; decl
= DECL_CHAIN (decl
))
8336 destroy_value_checked (ctx
, decl
, non_constant_p
);
8339 case PREINCREMENT_EXPR
:
8340 case POSTINCREMENT_EXPR
:
8341 case PREDECREMENT_EXPR
:
8342 case POSTDECREMENT_EXPR
:
8343 return cxx_eval_increment_expression (ctx
, t
,
8344 lval
, non_constant_p
, overflow_p
);
8350 case VEC_DELETE_EXPR
:
8353 /* GCC internal stuff. */
8358 error_at (loc
, "expression %qE is not a constant expression", t
);
8359 *non_constant_p
= true;
8363 /* Virtual function lookup. We don't need to do anything fancy. */
8364 return cxx_eval_constant_expression (ctx
, OBJ_TYPE_REF_EXPR (t
),
8365 lval
, non_constant_p
, overflow_p
);
8367 case PLACEHOLDER_EXPR
:
8368 /* Use of the value or address of the current object. */
8369 if (tree ctor
= lookup_placeholder (ctx
, lval
, TREE_TYPE (t
)))
8371 if (TREE_CODE (ctor
) == CONSTRUCTOR
)
8374 return cxx_eval_constant_expression (ctx
, ctor
, lval
,
8375 non_constant_p
, overflow_p
);
8377 /* A placeholder without a referent. We can get here when
8378 checking whether NSDMIs are noexcept, or in massage_init_elt;
8379 just say it's non-constant for now. */
8380 gcc_assert (ctx
->quiet
);
8381 *non_constant_p
= true;
8386 tree cond
= TREE_OPERAND (t
, 0);
8387 cond
= cxx_eval_constant_expression (ctx
, cond
, vc_prvalue
,
8388 non_constant_p
, overflow_p
);
8389 VERIFY_CONSTANT (cond
);
8390 if (integer_nonzerop (cond
))
8396 if (breaks (&TREE_OPERAND (t
, 0))
8397 || continues (&TREE_OPERAND (t
, 0)))
8398 *jump_target
= TREE_OPERAND (t
, 0);
8401 gcc_assert (cxx_dialect
>= cxx23
);
8403 error_at (loc
, "%<goto%> is not a constant expression");
8404 *non_constant_p
= true;
8412 cxx_eval_loop_expr (ctx
, t
,
8413 non_constant_p
, overflow_p
, jump_target
);
8418 cxx_eval_switch_expr (ctx
, t
,
8419 non_constant_p
, overflow_p
, jump_target
);
8423 /* It's possible to get a requires-expression in a constant
8424 expression. For example:
8426 template<typename T> concept bool C() {
8427 return requires (T t) { t; };
8430 template<typename T> requires !C<T>() void f(T);
8432 Normalization leaves f with the associated constraint
8433 '!requires (T t) { ... }' which is not transformed into
8435 if (!processing_template_decl
)
8436 return evaluate_requires_expr (t
);
8438 *non_constant_p
= true;
8442 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
8444 non_constant_p
, overflow_p
,
8452 case ASSERTION_STMT
:
8453 case PRECONDITION_STMT
:
8454 case POSTCONDITION_STMT
:
8456 contract_semantic semantic
= get_contract_semantic (t
);
8457 if (semantic
== CCS_IGNORE
)
8460 if (!cxx_eval_assert (ctx
, CONTRACT_CONDITION (t
),
8461 G_("contract predicate is false in "
8462 "constant expression"),
8463 EXPR_LOCATION (t
), checked_contract_p (semantic
),
8464 non_constant_p
, overflow_p
))
8465 *non_constant_p
= true;
8470 case TEMPLATE_ID_EXPR
:
8472 /* We can evaluate template-id that refers to a concept only if
8473 the template arguments are non-dependent. */
8474 tree id
= unpack_concept_check (t
);
8475 tree tmpl
= TREE_OPERAND (id
, 0);
8476 if (!concept_definition_p (tmpl
))
8477 internal_error ("unexpected template-id %qE", t
);
8479 if (function_concept_p (tmpl
))
8482 error_at (cp_expr_loc_or_input_loc (t
),
8483 "function concept must be called");
8484 r
= error_mark_node
;
8488 if (!value_dependent_expression_p (t
)
8489 && !uid_sensitive_constexpr_evaluation_p ())
8490 r
= evaluate_concept_check (t
);
8492 *non_constant_p
= true;
8499 inline_asm_in_constexpr_error (loc
, /*constexpr_fundef_p*/false);
8500 *non_constant_p
= true;
8507 error_at (EXPR_LOCATION (t
),
8508 "address of a call to %qs is not a constant expression",
8509 "__builtin_bit_cast");
8510 *non_constant_p
= true;
8513 r
= cxx_eval_bit_cast (ctx
, t
, non_constant_p
, overflow_p
);
8520 case OMP_DISTRIBUTE
:
8524 case OMP_TARGET_DATA
:
8533 case OMP_STRUCTURED_BLOCK
:
8537 case OMP_TARGET_UPDATE
:
8538 case OMP_TARGET_ENTER_DATA
:
8539 case OMP_TARGET_EXIT_DATA
:
8541 case OMP_ATOMIC_READ
:
8542 case OMP_ATOMIC_CAPTURE_OLD
:
8543 case OMP_ATOMIC_CAPTURE_NEW
:
8549 case OACC_HOST_DATA
:
8553 case OACC_ENTER_DATA
:
8554 case OACC_EXIT_DATA
:
8557 error_at (EXPR_LOCATION (t
),
8558 "statement is not a constant expression");
8559 *non_constant_p
= true;
8563 if (STATEMENT_CODE_P (TREE_CODE (t
)))
8565 /* This function doesn't know how to deal with pre-genericize
8566 statements; this can only happen with statement-expressions,
8567 so for now just fail. */
8569 error_at (EXPR_LOCATION (t
),
8570 "statement is not a constant expression");
8573 internal_error ("unexpected expression %qE of kind %s", t
,
8574 get_tree_code_name (TREE_CODE (t
)));
8575 *non_constant_p
= true;
8579 if (r
== error_mark_node
)
8580 *non_constant_p
= true;
8582 if (*non_constant_p
)
8588 /* P0859: A function is needed for constant evaluation if it is a constexpr
8589 function that is named by an expression ([basic.def.odr]) that is
8590 potentially constant evaluated.
8592 So we need to instantiate any constexpr functions mentioned by the
8593 expression even if the definition isn't needed for evaluating the
8597 instantiate_cx_fn_r (tree
*tp
, int *walk_subtrees
, void */
*data*/
)
8599 if (TREE_CODE (*tp
) == FUNCTION_DECL
8600 && DECL_DECLARED_CONSTEXPR_P (*tp
)
8601 && !DECL_INITIAL (*tp
)
8602 && !trivial_fn_p (*tp
)
8603 && (DECL_TEMPLOID_INSTANTIATION (*tp
) || DECL_DEFAULTED_FN (*tp
))
8604 && !uid_sensitive_constexpr_evaluation_p ())
8607 if (DECL_TEMPLOID_INSTANTIATION (*tp
))
8608 instantiate_decl (*tp
, /*defer_ok*/false, /*expl_inst*/false);
8610 synthesize_method (*tp
);
8613 else if (TREE_CODE (*tp
) == CALL_EXPR
8614 || TREE_CODE (*tp
) == AGGR_INIT_EXPR
)
8616 if (EXPR_HAS_LOCATION (*tp
))
8617 input_location
= EXPR_LOCATION (*tp
);
8627 instantiate_constexpr_fns (tree t
)
8629 location_t loc
= input_location
;
8630 cp_walk_tree_without_duplicates (&t
, instantiate_cx_fn_r
, NULL
);
8631 input_location
= loc
;
8634 /* Look for heap variables in the expression *TP. */
8637 find_heap_var_refs (tree
*tp
, int *walk_subtrees
, void */
*data*/
)
8640 && (DECL_NAME (*tp
) == heap_uninit_identifier
8641 || DECL_NAME (*tp
) == heap_identifier
8642 || DECL_NAME (*tp
) == heap_vec_uninit_identifier
8643 || DECL_NAME (*tp
) == heap_vec_identifier
8644 || DECL_NAME (*tp
) == heap_deleted_identifier
))
8652 /* Find immediate function decls in *TP if any. */
8655 find_immediate_fndecl (tree
*tp
, int */
*walk_subtrees*/
, void */
*data*/
)
8657 if (TREE_CODE (*tp
) == FUNCTION_DECL
&& DECL_IMMEDIATE_FUNCTION_P (*tp
))
8659 if (TREE_CODE (*tp
) == PTRMEM_CST
8660 && TREE_CODE (PTRMEM_CST_MEMBER (*tp
)) == FUNCTION_DECL
8661 && DECL_IMMEDIATE_FUNCTION_P (PTRMEM_CST_MEMBER (*tp
)))
8662 return PTRMEM_CST_MEMBER (*tp
);
8666 /* T has TREE_CONSTANT set but has been deemed not a valid C++ constant
8667 expression. Return a version of T that has TREE_CONSTANT cleared. */
8670 mark_non_constant (tree t
)
8672 gcc_checking_assert (TREE_CONSTANT (t
));
8674 /* This isn't actually constant, so unset TREE_CONSTANT.
8675 Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires
8676 it to be set if it is invariant address, even when it is not
8677 a valid C++ constant expression. Wrap it with a NOP_EXPR
8679 if (EXPR_P (t
) && TREE_CODE (t
) != ADDR_EXPR
)
8681 else if (TREE_CODE (t
) == CONSTRUCTOR
)
8682 t
= build1 (VIEW_CONVERT_EXPR
, TREE_TYPE (t
), t
);
8684 t
= build_nop (TREE_TYPE (t
), t
);
8685 TREE_CONSTANT (t
) = false;
8689 /* ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
8690 STRICT has the same sense as for constant_value_1: true if we only allow
8691 conforming C++ constant expressions, or false if we want a constant value
8692 even if it doesn't conform.
8693 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
8694 per P0595 even when ALLOW_NON_CONSTANT is true.
8695 CONSTEXPR_DTOR is true when evaluating the dtor of a constexpr variable.
8696 OBJECT must be non-NULL in that case. */
8699 cxx_eval_outermost_constant_expr (tree t
, bool allow_non_constant
,
8701 mce_value manifestly_const_eval
= mce_unknown
,
8702 bool constexpr_dtor
= false,
8703 tree object
= NULL_TREE
)
8705 auto_timevar
time (TV_CONSTEXPR
);
8707 bool non_constant_p
= false;
8708 bool overflow_p
= false;
8710 if (BRACE_ENCLOSED_INITIALIZER_P (t
))
8712 gcc_checking_assert (allow_non_constant
);
8716 constexpr_global_ctx global_ctx
;
8717 constexpr_ctx ctx
= { &global_ctx
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
8718 allow_non_constant
, strict
,
8719 !allow_non_constant
? mce_true
: manifestly_const_eval
};
8721 /* Turn off -frounding-math for manifestly constant evaluation. */
8722 warning_sentinel
rm (flag_rounding_math
,
8723 ctx
.manifestly_const_eval
== mce_true
);
8724 tree type
= initialized_type (t
);
8726 bool is_consteval
= false;
8727 if (VOID_TYPE_P (type
))
8730 /* Used for destructors of array elements. */
8731 type
= TREE_TYPE (object
);
8734 if (cxx_dialect
< cxx20
)
8736 if (TREE_CODE (t
) != CALL_EXPR
&& TREE_CODE (t
) != AGGR_INIT_EXPR
)
8738 /* Calls to immediate functions returning void need to be
8740 tree fndecl
= cp_get_callee_fndecl_nofold (t
);
8741 if (fndecl
== NULL_TREE
|| !DECL_IMMEDIATE_FUNCTION_P (fndecl
))
8744 is_consteval
= true;
8747 else if (cxx_dialect
>= cxx20
8748 && (TREE_CODE (t
) == CALL_EXPR
8749 || TREE_CODE (t
) == AGGR_INIT_EXPR
8750 || TREE_CODE (t
) == TARGET_EXPR
))
8752 /* For non-concept checks, determine if it is consteval. */
8753 if (!concept_check_p (t
))
8756 if (TREE_CODE (x
) == TARGET_EXPR
)
8757 x
= TARGET_EXPR_INITIAL (x
);
8758 tree fndecl
= cp_get_callee_fndecl_nofold (x
);
8759 if (fndecl
&& DECL_IMMEDIATE_FUNCTION_P (fndecl
))
8760 is_consteval
= true;
8763 if (AGGREGATE_TYPE_P (type
) || VECTOR_TYPE_P (type
))
8765 /* In C++14 an NSDMI can participate in aggregate initialization,
8766 and can refer to the address of the object being initialized, so
8767 we need to pass in the relevant VAR_DECL if we want to do the
8768 evaluation in a single pass. The evaluation will dynamically
8769 update ctx.values for the VAR_DECL. We use the same strategy
8770 for C++11 constexpr constructors that refer to the object being
8774 gcc_assert (object
&& VAR_P (object
));
8775 gcc_assert (DECL_DECLARED_CONSTEXPR_P (object
));
8776 gcc_assert (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object
));
8777 if (error_operand_p (DECL_INITIAL (object
)))
8779 ctx
.ctor
= unshare_expr (DECL_INITIAL (object
));
8780 TREE_READONLY (ctx
.ctor
) = false;
8781 /* Temporarily force decl_really_constant_value to return false
8782 for it, we want to use ctx.ctor for the current value instead. */
8783 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object
) = false;
8787 ctx
.ctor
= build_constructor (type
, NULL
);
8788 CONSTRUCTOR_NO_CLEARING (ctx
.ctor
) = true;
8792 if (TREE_CODE (t
) == CALL_EXPR
)
8794 /* If T is calling a constructor to initialize an object, reframe
8795 it as an AGGR_INIT_EXPR to avoid trying to modify an object
8796 from outside the constant evaluation, which will fail even if
8797 the value is actually constant (is_constant_evaluated3.C). */
8798 tree fn
= cp_get_callee_fndecl_nofold (t
);
8799 if (fn
&& DECL_CONSTRUCTOR_P (fn
))
8801 object
= CALL_EXPR_ARG (t
, 0);
8802 object
= build_fold_indirect_ref (object
);
8803 r
= build_aggr_init_expr (type
, r
);
8806 else if (TREE_CODE (t
) == TARGET_EXPR
)
8807 object
= TARGET_EXPR_SLOT (t
);
8808 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
)
8809 object
= AGGR_INIT_EXPR_SLOT (t
);
8811 ctx
.object
= object
;
8813 gcc_assert (same_type_ignoring_top_level_qualifiers_p
8814 (type
, TREE_TYPE (object
)));
8815 if (object
&& DECL_P (object
))
8816 global_ctx
.put_value (object
, ctx
.ctor
);
8817 if (TREE_CODE (r
) == TARGET_EXPR
)
8818 /* Avoid creating another CONSTRUCTOR when we expand the
8820 r
= TARGET_EXPR_INITIAL (r
);
8823 auto_vec
<tree
, 16> cleanups
;
8824 global_ctx
.cleanups
= &cleanups
;
8826 if (manifestly_const_eval
== mce_true
)
8827 instantiate_constexpr_fns (r
);
8828 r
= cxx_eval_constant_expression (&ctx
, r
, vc_prvalue
,
8829 &non_constant_p
, &overflow_p
);
8831 if (!constexpr_dtor
)
8832 verify_constant (r
, allow_non_constant
, &non_constant_p
, &overflow_p
);
8834 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object
) = true;
8838 /* Evaluate the cleanups. */
8839 FOR_EACH_VEC_ELT_REVERSE (cleanups
, i
, cleanup
)
8840 cxx_eval_constant_expression (&ctx
, cleanup
, vc_discard
,
8841 &non_constant_p
, &overflow_p
);
8843 /* Mutable logic is a bit tricky: we want to allow initialization of
8844 constexpr variables with mutable members, but we can't copy those
8845 members to another constexpr variable. */
8846 if (TREE_CODE (r
) == CONSTRUCTOR
&& CONSTRUCTOR_MUTABLE_POISON (r
))
8848 if (!allow_non_constant
)
8849 error ("%qE is not a constant expression because it refers to "
8850 "mutable subobjects of %qT", t
, type
);
8851 non_constant_p
= true;
8854 if (TREE_CODE (r
) == CONSTRUCTOR
&& CONSTRUCTOR_NO_CLEARING (r
))
8856 if (!allow_non_constant
)
8857 error ("%qE is not a constant expression because it refers to "
8858 "an incompletely initialized variable", t
);
8859 TREE_CONSTANT (r
) = false;
8860 non_constant_p
= true;
8863 if (!non_constant_p
&& cxx_dialect
>= cxx20
8864 && !global_ctx
.heap_vars
.is_empty ())
8866 tree heap_var
= cp_walk_tree_without_duplicates (&r
, find_heap_var_refs
,
8871 if (!allow_non_constant
&& !non_constant_p
)
8872 error_at (DECL_SOURCE_LOCATION (heap_var
),
8873 "%qE is not a constant expression because it refers to "
8874 "a result of %<operator new%>", t
);
8876 non_constant_p
= true;
8878 FOR_EACH_VEC_ELT (global_ctx
.heap_vars
, i
, heap_var
)
8880 if (DECL_NAME (heap_var
) != heap_deleted_identifier
)
8882 if (!allow_non_constant
&& !non_constant_p
)
8883 error_at (DECL_SOURCE_LOCATION (heap_var
),
8884 "%qE is not a constant expression because allocated "
8885 "storage has not been deallocated", t
);
8887 non_constant_p
= true;
8889 varpool_node::get (heap_var
)->remove ();
8893 /* Check that immediate invocation does not return an expression referencing
8894 any immediate function decls. */
8895 if (!non_constant_p
&& cxx_dialect
>= cxx20
)
8896 if (tree immediate_fndecl
8897 = cp_walk_tree_without_duplicates (&r
, find_immediate_fndecl
,
8900 if (!allow_non_constant
&& !non_constant_p
)
8903 error_at (cp_expr_loc_or_input_loc (t
),
8904 "immediate evaluation returns address of immediate "
8905 "function %qD", immediate_fndecl
);
8907 error_at (cp_expr_loc_or_input_loc (t
),
8908 "constant evaluation returns address of immediate "
8909 "function %qD", immediate_fndecl
);
8912 non_constant_p
= true;
8916 /* If we saw something bad, go back to our argument. The wrapping below is
8917 only for the cases of TREE_CONSTANT argument or overflow. */
8920 if (!non_constant_p
&& overflow_p
)
8921 non_constant_p
= true;
8923 /* Unshare the result. */
8924 bool should_unshare
= true;
8925 if (r
== t
|| (TREE_CODE (t
) == TARGET_EXPR
8926 && TARGET_EXPR_INITIAL (t
) == r
))
8927 should_unshare
= false;
8929 if (non_constant_p
&& !allow_non_constant
)
8930 return error_mark_node
;
8931 else if (constexpr_dtor
)
8933 else if (non_constant_p
&& TREE_CONSTANT (r
))
8934 r
= mark_non_constant (r
);
8935 else if (non_constant_p
)
8939 r
= unshare_expr (r
);
8941 if (TREE_CODE (r
) == CONSTRUCTOR
&& CLASS_TYPE_P (TREE_TYPE (r
)))
8943 r
= adjust_temp_type (type
, r
);
8944 if (TREE_CODE (t
) == TARGET_EXPR
8945 && TARGET_EXPR_INITIAL (t
) == r
)
8947 else if (TREE_CODE (t
) == CONSTRUCTOR
|| TREE_CODE (t
) == CALL_EXPR
)
8948 /* Don't add a TARGET_EXPR if our argument didn't have one. */;
8949 else if (TREE_CODE (t
) == TARGET_EXPR
&& TARGET_EXPR_CLEANUP (t
))
8950 r
= get_target_expr (r
);
8953 r
= get_target_expr (r
, tf_warning_or_error
| tf_no_cleanup
);
8954 TREE_CONSTANT (r
) = true;
8958 if (TREE_CODE (t
) == TARGET_EXPR
8959 && TREE_CODE (r
) == TARGET_EXPR
)
8961 /* Preserve this flag for potential_constant_expression, and the others
8962 for good measure. */
8963 TARGET_EXPR_ELIDING_P (r
) = TARGET_EXPR_ELIDING_P (t
);
8964 TARGET_EXPR_IMPLICIT_P (r
) = TARGET_EXPR_IMPLICIT_P (t
);
8965 TARGET_EXPR_LIST_INIT_P (r
) = TARGET_EXPR_LIST_INIT_P (t
);
8966 TARGET_EXPR_DIRECT_INIT_P (r
) = TARGET_EXPR_DIRECT_INIT_P (t
);
8969 /* Remember the original location if that wouldn't need a wrapper. */
8970 if (location_t loc
= EXPR_LOCATION (t
))
8971 protected_set_expr_location (r
, loc
);
8976 /* If T represents a constant expression returns its reduced value.
8977 Otherwise return error_mark_node. */
8980 cxx_constant_value (tree t
, tree decl
/* = NULL_TREE */,
8981 tsubst_flags_t complain
/* = tf_error */)
8983 bool sfinae
= !(complain
& tf_error
);
8984 tree r
= cxx_eval_outermost_constant_expr (t
, sfinae
, true, mce_true
, false, decl
);
8985 if (sfinae
&& !TREE_CONSTANT (r
))
8986 r
= error_mark_node
;
8990 /* Like cxx_constant_value, but used for evaluation of constexpr destructors
8991 of constexpr variables. The actual initializer of DECL is not modified. */
8994 cxx_constant_dtor (tree t
, tree decl
)
8996 cxx_eval_outermost_constant_expr (t
, false, true, mce_true
, true, decl
);
8999 /* Helper routine for fold_simple function. Either return simplified
9000 expression T, otherwise NULL_TREE.
9001 In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold
9002 even if we are within template-declaration. So be careful on call, as in
9003 such case types can be undefined. */
9006 fold_simple_1 (tree t
)
9009 enum tree_code code
= TREE_CODE (t
);
9021 return fold_sizeof_expr (t
);
9030 case TRUTH_NOT_EXPR
:
9031 case VIEW_CONVERT_EXPR
:
9034 case FIX_TRUNC_EXPR
:
9035 case FIXED_CONVERT_EXPR
:
9036 case ADDR_SPACE_CONVERT_EXPR
:
9038 op1
= TREE_OPERAND (t
, 0);
9040 t
= const_unop (code
, TREE_TYPE (t
), op1
);
9044 if (CONVERT_EXPR_CODE_P (code
)
9045 && TREE_OVERFLOW_P (t
) && !TREE_OVERFLOW_P (op1
))
9046 TREE_OVERFLOW (t
) = false;
9054 /* If T is a simple constant expression, returns its simplified value.
9055 Otherwise returns T. In contrast to maybe_constant_value we
9056 simplify only few operations on constant-expressions, and we don't
9057 try to simplify constexpressions. */
9060 fold_simple (tree t
)
9062 if (processing_template_decl
)
9065 tree r
= fold_simple_1 (t
);
9072 /* Try folding the expression T to a simple constant.
9073 Returns that constant, otherwise returns T. */
9076 fold_to_constant (tree t
)
9079 if (CONSTANT_CLASS_P (r
) && !TREE_OVERFLOW (r
))
9085 /* If T is a constant expression, returns its reduced value.
9086 Otherwise, if T does not have TREE_CONSTANT set, returns T.
9087 Otherwise, returns a version of T without TREE_CONSTANT.
9088 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated
9091 static GTY((deletable
)) hash_map
<tree
, tree
> *cv_cache
;
9094 maybe_constant_value (tree t
, tree decl
/* = NULL_TREE */,
9095 mce_value manifestly_const_eval
/* = mce_unknown */)
9099 if (!is_nondependent_constant_expression (t
))
9101 if (TREE_OVERFLOW_P (t
)
9102 || (!processing_template_decl
&& TREE_CONSTANT (t
)))
9103 t
= mark_non_constant (t
);
9106 else if (CONSTANT_CLASS_P (t
))
9107 /* No caching or evaluation needed. */
9110 /* Don't constant evaluate an unevaluated non-manifestly-constant operand,
9111 but at least try folding it to a simple constant. */
9112 if (cp_unevaluated_operand
&& manifestly_const_eval
!= mce_true
)
9113 return fold_to_constant (t
);
9115 if (manifestly_const_eval
!= mce_unknown
)
9116 return cxx_eval_outermost_constant_expr (t
, true, true,
9117 manifestly_const_eval
, false, decl
);
9119 if (cv_cache
== NULL
)
9120 cv_cache
= hash_map
<tree
, tree
>::create_ggc (101);
9121 if (tree
*cached
= cv_cache
->get (t
))
9126 /* Clear processing_template_decl for sake of break_out_target_exprs;
9127 entries in the cv_cache are non-templated. */
9128 processing_template_decl_sentinel ptds
;
9130 r
= break_out_target_exprs (r
, /*clear_loc*/true);
9131 protected_set_expr_location (r
, EXPR_LOCATION (t
));
9136 uid_sensitive_constexpr_evaluation_checker c
;
9137 r
= cxx_eval_outermost_constant_expr (t
, true, true,
9138 manifestly_const_eval
, false, decl
);
9139 gcc_checking_assert (r
== t
9140 || CONVERT_EXPR_P (t
)
9141 || TREE_CODE (t
) == VIEW_CONVERT_EXPR
9142 || (TREE_CONSTANT (t
) && !TREE_CONSTANT (r
))
9143 || !cp_tree_equal (r
, t
));
9144 if (!c
.evaluation_restricted_p ())
9145 cv_cache
->put (t
, r
);
9149 /* Dispose of the whole CV_CACHE. */
9152 clear_cv_cache (void)
9154 if (cv_cache
!= NULL
)
9158 /* Dispose of the whole CV_CACHE and FOLD_CACHE. */
9161 clear_cv_and_fold_caches ()
9164 clear_fold_cache ();
9167 /* Internal function handling expressions in templates for
9168 fold_non_dependent_expr and fold_non_dependent_init.
9170 If we're in a template, but T isn't value dependent, simplify
9171 it. We're supposed to treat:
9173 template <typename T> void f(T[1 + 1]);
9174 template <typename T> void f(T[2]);
9176 as two declarations of the same function, for example. */
9179 fold_non_dependent_expr_template (tree t
, tsubst_flags_t complain
,
9180 bool manifestly_const_eval
,
9183 gcc_assert (processing_template_decl
);
9185 if (is_nondependent_constant_expression (t
))
9187 processing_template_decl_sentinel s
;
9188 t
= instantiate_non_dependent_expr_internal (t
, complain
);
9190 if (type_unknown_p (t
) || BRACE_ENCLOSED_INITIALIZER_P (t
))
9192 if (TREE_OVERFLOW_P (t
))
9194 t
= build_nop (TREE_TYPE (t
), t
);
9195 TREE_CONSTANT (t
) = false;
9199 else if (CONSTANT_CLASS_P (t
))
9200 /* No evaluation needed. */
9203 /* Don't constant evaluate an unevaluated non-manifestly-constant operand,
9204 but at least try folding it to a simple constant. */
9205 if (cp_unevaluated_operand
&& !manifestly_const_eval
)
9206 return fold_to_constant (t
);
9208 tree r
= cxx_eval_outermost_constant_expr (t
, true, true,
9209 mce_value (manifestly_const_eval
),
9211 /* cp_tree_equal looks through NOPs, so allow them. */
9212 gcc_checking_assert (r
== t
9213 || CONVERT_EXPR_P (t
)
9214 || TREE_CODE (t
) == VIEW_CONVERT_EXPR
9215 || (TREE_CONSTANT (t
) && !TREE_CONSTANT (r
))
9216 || !cp_tree_equal (r
, t
));
9219 else if (TREE_OVERFLOW_P (t
))
9221 t
= build_nop (TREE_TYPE (t
), t
);
9222 TREE_CONSTANT (t
) = false;
9228 /* Like maybe_constant_value but first fully instantiate the argument.
9230 Note: this is equivalent to instantiate_non_dependent_expr (t, complain)
9231 followed by maybe_constant_value but is more efficient,
9232 because it calls instantiation_dependent_expression_p and
9233 potential_constant_expression at most once.
9234 The manifestly_const_eval argument is passed to maybe_constant_value.
9236 Callers should generally pass their active complain, or if they are in a
9237 non-template, diagnosing context, they can use the default of
9238 tf_warning_or_error. Callers that might be within a template context, don't
9239 have a complain parameter, and aren't going to remember the result for long
9240 (e.g. null_ptr_cst_p), can pass tf_none and deal with error_mark_node
9244 fold_non_dependent_expr (tree t
,
9245 tsubst_flags_t complain
/* = tf_warning_or_error */,
9246 bool manifestly_const_eval
/* = false */,
9247 tree object
/* = NULL_TREE */)
9252 if (processing_template_decl
)
9253 return fold_non_dependent_expr_template (t
, complain
,
9254 manifestly_const_eval
, object
);
9256 return maybe_constant_value (t
, object
, mce_value (manifestly_const_eval
));
9259 /* Like fold_non_dependent_expr, but if EXPR couldn't be folded to a constant,
9260 return the original expression. */
9263 maybe_fold_non_dependent_expr (tree expr
,
9264 tsubst_flags_t complain
/*=tf_warning_or_error*/)
9266 tree t
= fold_non_dependent_expr (expr
, complain
);
9267 if (t
&& TREE_CONSTANT (t
))
9273 /* Like maybe_constant_init but first fully instantiate the argument. */
9276 fold_non_dependent_init (tree t
,
9277 tsubst_flags_t complain
/*=tf_warning_or_error*/,
9278 bool manifestly_const_eval
/*=false*/,
9279 tree object
/* = NULL_TREE */)
9284 if (processing_template_decl
)
9286 t
= fold_non_dependent_expr_template (t
, complain
,
9287 manifestly_const_eval
, object
);
9288 /* maybe_constant_init does this stripping, so do it here too. */
9289 if (TREE_CODE (t
) == TARGET_EXPR
)
9291 tree init
= TARGET_EXPR_INITIAL (t
);
9292 if (TREE_CODE (init
) == CONSTRUCTOR
)
9298 return maybe_constant_init (t
, object
, manifestly_const_eval
);
9301 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
9302 than wrapped in a TARGET_EXPR.
9303 ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
9304 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
9305 per P0595 even when ALLOW_NON_CONSTANT is true. */
9308 maybe_constant_init_1 (tree t
, tree decl
, bool allow_non_constant
,
9309 bool manifestly_const_eval
)
9313 if (TREE_CODE (t
) == EXPR_STMT
)
9314 t
= TREE_OPERAND (t
, 0);
9315 if (TREE_CODE (t
) == CONVERT_EXPR
9316 && VOID_TYPE_P (TREE_TYPE (t
)))
9317 t
= TREE_OPERAND (t
, 0);
9318 if (TREE_CODE (t
) == INIT_EXPR
)
9319 t
= TREE_OPERAND (t
, 1);
9320 if (TREE_CODE (t
) == TARGET_EXPR
)
9321 t
= TARGET_EXPR_INITIAL (t
);
9322 if (!is_nondependent_static_init_expression (t
))
9323 /* Don't try to evaluate it. */;
9324 else if (CONSTANT_CLASS_P (t
) && TREE_CODE (t
) != PTRMEM_CST
)
9325 /* No evaluation needed. PTRMEM_CST needs the immediate fn check. */;
9328 /* [basic.start.static] allows constant-initialization of variables with
9329 static or thread storage duration even if it isn't required, but we
9330 shouldn't bend the rules the same way for automatic variables. */
9331 bool is_static
= (decl
&& DECL_P (decl
)
9332 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)));
9334 manifestly_const_eval
= true;
9336 if (cp_unevaluated_operand
&& !manifestly_const_eval
)
9337 return fold_to_constant (t
);
9339 t
= cxx_eval_outermost_constant_expr (t
, allow_non_constant
, !is_static
,
9340 mce_value (manifestly_const_eval
),
9343 if (TREE_CODE (t
) == TARGET_EXPR
)
9345 tree init
= TARGET_EXPR_INITIAL (t
);
9346 if (TREE_CODE (init
) == CONSTRUCTOR
)
9352 /* Wrapper for maybe_constant_init_1 which permits non constants. */
9355 maybe_constant_init (tree t
, tree decl
, bool manifestly_const_eval
)
9357 return maybe_constant_init_1 (t
, decl
, true, manifestly_const_eval
);
9360 /* Wrapper for maybe_constant_init_1 which does not permit non constants. */
9363 cxx_constant_init (tree t
, tree decl
)
9365 return maybe_constant_init_1 (t
, decl
, false, true);
9369 /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
9370 /* Return true if the object referred to by REF has automatic or thread
9373 enum { ck_ok
, ck_bad
, ck_unknown
};
9375 check_automatic_or_tls (tree ref
)
9378 poly_int64 bitsize
, bitpos
;
9380 int volatilep
= 0, unsignedp
= 0;
9381 tree decl
= get_inner_reference (ref
, &bitsize
, &bitpos
, &offset
,
9382 &mode
, &unsignedp
, &volatilep
, false);
9385 /* If there isn't a decl in the middle, we don't know the linkage here,
9386 and this isn't a constant expression anyway. */
9389 dk
= decl_storage_duration (decl
);
9390 return (dk
== dk_auto
|| dk
== dk_thread
) ? ck_bad
: ck_ok
;
9394 /* Data structure for passing data from potential_constant_expression_1
9395 to check_for_return_continue via cp_walk_tree. */
9396 struct check_for_return_continue_data
{
9397 hash_set
<tree
> *pset
;
9402 /* Helper function for potential_constant_expression_1 SWITCH_STMT handling,
9403 called through cp_walk_tree. Return the first RETURN_EXPR found, or note
9404 the first CONTINUE_STMT and/or BREAK_STMT if RETURN_EXPR is not found. */
9406 check_for_return_continue (tree
*tp
, int *walk_subtrees
, void *data
)
9409 check_for_return_continue_data
*d
= (check_for_return_continue_data
*) data
;
9410 switch (TREE_CODE (t
))
9416 if (d
->continue_stmt
== NULL_TREE
)
9417 d
->continue_stmt
= t
;
9421 if (d
->break_stmt
== NULL_TREE
)
9426 if (tree r = cp_walk_tree (&x, check_for_return_continue, data, \
9430 /* For loops, walk subtrees manually, so that continue stmts found
9431 inside of the bodies of the loops are ignored. */
9434 RECUR (DO_COND (t
));
9435 s
= d
->continue_stmt
;
9437 RECUR (DO_BODY (t
));
9438 d
->continue_stmt
= s
;
9444 RECUR (WHILE_COND (t
));
9445 s
= d
->continue_stmt
;
9447 RECUR (WHILE_BODY (t
));
9448 d
->continue_stmt
= s
;
9454 RECUR (FOR_INIT_STMT (t
));
9455 RECUR (FOR_COND (t
));
9456 RECUR (FOR_EXPR (t
));
9457 s
= d
->continue_stmt
;
9459 RECUR (FOR_BODY (t
));
9460 d
->continue_stmt
= s
;
9464 case RANGE_FOR_STMT
:
9466 RECUR (RANGE_FOR_EXPR (t
));
9467 s
= d
->continue_stmt
;
9469 RECUR (RANGE_FOR_BODY (t
));
9470 d
->continue_stmt
= s
;
9476 RECUR (SWITCH_STMT_COND (t
));
9478 RECUR (SWITCH_STMT_BODY (t
));
9483 case STATEMENT_LIST
:
9496 /* Return true if T denotes a potentially constant expression. Issue
9497 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
9498 an lvalue-rvalue conversion is implied. If NOW is true, we want to
9499 consider the expression in the current context, independent of constexpr
9500 substitution. If FUNDEF_P is true, we're checking a constexpr function body
9501 and hard errors should not be reported by constexpr_error.
9503 C++0x [expr.const] used to say
9505 6 An expression is a potential constant expression if it is
9506 a constant expression where all occurrences of function
9507 parameters are replaced by arbitrary constant expressions
9508 of the appropriate type.
9510 2 A conditional expression is a constant expression unless it
9511 involves one of the following as a potentially evaluated
9512 subexpression (3.2), but subexpressions of logical AND (5.14),
9513 logical OR (5.15), and conditional (5.16) operations that are
9514 not evaluated are not considered. */
9517 potential_constant_expression_1 (tree t
, bool want_rval
, bool strict
, bool now
,
9518 bool fundef_p
, tsubst_flags_t flags
,
9521 #define RECUR(T,RV) \
9522 potential_constant_expression_1 ((T), (RV), strict, now, fundef_p, flags, \
9525 enum { any
= false, rval
= true };
9529 if (t
== error_mark_node
)
9533 location_t loc
= cp_expr_loc_or_input_loc (t
);
9536 /* If we are jumping, ignore everything. This is simpler than the
9537 cxx_eval_constant_expression handling because we only need to be
9538 conservatively correct, and we don't necessarily have a constant value
9539 available, so we don't bother with switch tracking. */
9542 if (TREE_THIS_VOLATILE (t
) && want_rval
9543 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (t
)))
9545 if (flags
& tf_error
)
9546 constexpr_error (loc
, fundef_p
, "lvalue-to-rvalue conversion of "
9547 "a volatile lvalue %qE with type %qT", t
,
9551 if (CONSTANT_CLASS_P (t
))
9553 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_TYPED
)
9554 && TREE_TYPE (t
) == error_mark_node
)
9557 switch (TREE_CODE (t
))
9563 case TEMPLATE_ID_EXPR
:
9565 case CASE_LABEL_EXPR
:
9572 case TEMPLATE_PARM_INDEX
:
9574 case IDENTIFIER_NODE
:
9575 case USERDEF_LITERAL
:
9576 /* We can see a FIELD_DECL in a pointer-to-member expression. */
9581 case PLACEHOLDER_EXPR
:
9584 case DEBUG_BEGIN_STMT
:
9588 if (!RECUR (TREE_OPERAND (t
, 0), any
))
9598 if (now
&& want_rval
)
9600 tree type
= TREE_TYPE (t
);
9601 if (dependent_type_p (type
)
9602 || !COMPLETE_TYPE_P (processing_template_decl
9603 ? type
: complete_type (type
))
9604 || is_really_empty_class (type
, /*ignore_vptr*/false))
9605 /* An empty class has no data to read. */
9607 if (flags
& tf_error
)
9608 constexpr_error (input_location
, fundef_p
,
9609 "%qE is not a constant expression", t
);
9614 case AGGR_INIT_EXPR
:
9616 /* -- an invocation of a function other than a constexpr function
9617 or a constexpr constructor. */
9619 tree fun
= get_function_named_in_call (t
);
9620 const int nargs
= call_expr_nargs (t
);
9623 if (fun
== NULL_TREE
)
9625 /* Reset to allow the function to continue past the end
9626 of the block below. Otherwise return early. */
9629 if (TREE_CODE (t
) == CALL_EXPR
9630 && CALL_EXPR_FN (t
) == NULL_TREE
)
9631 switch (CALL_EXPR_IFN (t
))
9633 /* These should be ignored, they are optimized away from
9634 constexpr functions. */
9635 case IFN_UBSAN_NULL
:
9636 case IFN_UBSAN_BOUNDS
:
9637 case IFN_UBSAN_VPTR
:
9638 case IFN_FALLTHROUGH
:
9642 case IFN_ADD_OVERFLOW
:
9643 case IFN_SUB_OVERFLOW
:
9644 case IFN_MUL_OVERFLOW
:
9646 case IFN_VEC_CONVERT
:
9656 /* fold_call_expr can't do anything with IFN calls. */
9657 if (flags
& tf_error
)
9658 constexpr_error (loc
, fundef_p
,
9659 "call to internal function %qE", t
);
9664 if (fun
&& is_overloaded_fn (fun
))
9666 if (!RECUR (fun
, true))
9668 fun
= get_fns (fun
);
9670 if (TREE_CODE (fun
) == FUNCTION_DECL
)
9672 if (builtin_valid_in_constant_expr_p (fun
))
9674 if (!maybe_constexpr_fn (fun
)
9675 /* Allow any built-in function; if the expansion
9676 isn't constant, we'll deal with that then. */
9677 && !fndecl_built_in_p (fun
)
9678 /* In C++20, replaceable global allocation functions
9679 are constant expressions. */
9680 && (!cxx_replaceable_global_alloc_fn (fun
)
9681 || TREE_CODE (t
) != CALL_EXPR
9682 || (!CALL_FROM_NEW_OR_DELETE_P (t
)
9683 && (current_function_decl
== NULL_TREE
9684 || !is_std_allocator_allocate
9685 (current_function_decl
))))
9686 /* Allow placement new in std::construct_at. */
9687 && (!cxx_placement_new_fn (fun
)
9688 || TREE_CODE (t
) != CALL_EXPR
9689 || current_function_decl
== NULL_TREE
9690 || !is_std_construct_at (current_function_decl
))
9691 && !cxx_dynamic_cast_fn_p (fun
))
9693 if ((flags
& tf_error
)
9694 && constexpr_error (loc
, fundef_p
,
9695 "call to non-%<constexpr%> "
9696 "function %qD", fun
))
9697 explain_invalid_constexpr_fn (fun
);
9702 fun
= OVL_FIRST (fun
);
9703 /* Skip initial arguments to base constructors. */
9704 if (DECL_BASE_CONSTRUCTOR_P (fun
))
9705 i
= num_artificial_parms_for (fun
);
9710 && FUNCTION_POINTER_TYPE_P (TREE_TYPE (fun
)))
9714 if (RECUR (fun
, want_rval
))
9715 /* Might end up being a constant function pointer. But it
9716 could also be a function object with constexpr op(), so
9717 we pass 'any' so that the underlying VAR_DECL is deemed
9718 as potentially-constant even though it wasn't declared
9723 for (; i
< nargs
; ++i
)
9725 tree x
= get_nth_callarg (t
, i
);
9726 /* In a template, reference arguments haven't been converted to
9727 REFERENCE_TYPE and we might not even know if the parameter
9728 is a reference, so accept lvalue constants too. */
9729 bool rv
= processing_template_decl
? any
: rval
;
9730 /* Don't require an immediately constant value, as constexpr
9731 substitution might not use the value of the argument. */
9732 bool sub_now
= false;
9733 if (!potential_constant_expression_1 (x
, rv
, strict
,
9734 sub_now
, fundef_p
, flags
,
9741 case NON_LVALUE_EXPR
:
9742 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
9743 -- an lvalue of integral type that refers to a non-volatile
9744 const variable or static data member initialized with
9745 constant expressions, or
9747 -- an lvalue of literal type that refers to non-volatile
9748 object defined with constexpr, or that refers to a
9749 sub-object of such an object; */
9750 return RECUR (TREE_OPERAND (t
, 0), rval
);
9752 case EXCESS_PRECISION_EXPR
:
9753 return RECUR (TREE_OPERAND (t
, 0), rval
);
9756 if (DECL_HAS_VALUE_EXPR_P (t
))
9758 if (now
&& is_normal_capture_proxy (t
))
9760 /* -- in a lambda-expression, a reference to this or to a
9761 variable with automatic storage duration defined outside that
9762 lambda-expression, where the reference would be an
9766 /* Since we're doing an lvalue-rvalue conversion, this might
9767 not be an odr-use, so evaluate the variable directly. */
9768 return RECUR (DECL_CAPTURED_VARIABLE (t
), rval
);
9770 if (flags
& tf_error
)
9772 tree cap
= DECL_CAPTURED_VARIABLE (t
);
9773 auto_diagnostic_group d
;
9774 if (constexpr_error (input_location
, fundef_p
,
9775 "lambda capture of %qE is not a "
9776 "constant expression", cap
)
9777 && decl_constant_var_p (cap
))
9778 inform (input_location
, "because it is used as a glvalue");
9782 /* Treat __PRETTY_FUNCTION__ inside a template function as
9783 potentially-constant. */
9784 else if (DECL_PRETTY_FUNCTION_P (t
)
9785 && DECL_VALUE_EXPR (t
) == error_mark_node
)
9787 return RECUR (DECL_VALUE_EXPR (t
), rval
);
9790 && (now
|| !var_in_maybe_constexpr_fn (t
))
9791 && !type_dependent_expression_p (t
)
9792 && !decl_maybe_constant_var_p (t
)
9794 || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t
))
9795 || (DECL_INITIAL (t
)
9796 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t
)))
9797 && COMPLETE_TYPE_P (TREE_TYPE (t
))
9798 && !is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
9800 if (flags
& tf_error
)
9801 non_const_var_error (loc
, t
, fundef_p
);
9807 if (REINTERPRET_CAST_P (t
))
9809 if (flags
& tf_error
)
9810 constexpr_error (loc
, fundef_p
, "%<reinterpret_cast%> is not a "
9811 "constant expression");
9816 case VIEW_CONVERT_EXPR
:
9817 /* -- a reinterpret_cast. FIXME not implemented, and this rule
9818 may change to something more specific to type-punning (DR 1312). */
9820 tree from
= TREE_OPERAND (t
, 0);
9821 if (location_wrapper_p (t
))
9823 iloc_sentinel ils
= loc
;
9824 return (RECUR (from
, want_rval
));
9826 if (INDIRECT_TYPE_P (TREE_TYPE (t
)))
9828 STRIP_ANY_LOCATION_WRAPPER (from
);
9829 if (TREE_CODE (from
) == INTEGER_CST
9830 && !integer_zerop (from
))
9832 if (flags
& tf_error
)
9833 constexpr_error (loc
, fundef_p
,
9834 "%<reinterpret_cast%> from integer to "
9839 return (RECUR (from
, TREE_CODE (t
) != VIEW_CONVERT_EXPR
));
9842 case ADDRESSOF_EXPR
:
9843 /* This is like ADDR_EXPR, except it won't form pointer-to-member. */
9844 t
= TREE_OPERAND (t
, 0);
9845 goto handle_addr_expr
;
9848 /* -- a unary operator & that is applied to an lvalue that
9849 designates an object with thread or automatic storage
9851 t
= TREE_OPERAND (t
, 0);
9853 if (TREE_CODE (t
) == OFFSET_REF
&& PTRMEM_OK_P (t
))
9854 /* A pointer-to-member constant. */
9859 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
9860 any checking here, as we might dereference the pointer later. If
9861 we remove this code, also remove check_automatic_or_tls. */
9862 i
= check_automatic_or_tls (t
);
9867 if (flags
& tf_error
)
9868 error ("address-of an object %qE with thread local or "
9869 "automatic storage is not a constant expression", t
);
9873 return RECUR (t
, any
);
9878 /* -- a class member access unless its postfix-expression is
9879 of literal type or of pointer to literal type. */
9880 /* This test would be redundant, as it follows from the
9881 postfix-expression being a potential constant expression. */
9882 if (type_unknown_p (t
))
9884 if (is_overloaded_fn (t
))
9885 /* In a template, a COMPONENT_REF of a function expresses ob.fn(),
9886 which uses ob as an lvalue. */
9893 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
9895 case EXPR_PACK_EXPANSION
:
9896 return RECUR (PACK_EXPANSION_PATTERN (t
), want_rval
);
9900 tree x
= TREE_OPERAND (t
, 0);
9902 if (is_this_parameter (x
) && !is_capture_proxy (x
))
9904 if (now
|| !var_in_maybe_constexpr_fn (x
))
9906 if (flags
& tf_error
)
9907 constexpr_error (loc
, fundef_p
, "use of %<this%> in a "
9908 "constant expression");
9913 return RECUR (x
, rval
);
9916 case STATEMENT_LIST
:
9917 for (tree stmt
: tsi_range (t
))
9918 if (!RECUR (stmt
, any
))
9923 if (cxx_dialect
< cxx14
)
9925 if (!RECUR (TREE_OPERAND (t
, 0), any
))
9927 /* Just ignore clobbers. */
9928 if (TREE_CLOBBER_P (TREE_OPERAND (t
, 1)))
9930 if (!RECUR (TREE_OPERAND (t
, 1), rval
))
9935 if (cxx_dialect
< cxx14
)
9937 if (!RECUR (TREE_OPERAND (t
, 0), rval
))
9939 if (!RECUR (TREE_OPERAND (t
, 2), rval
))
9944 if (!RECUR (DO_COND (t
), rval
))
9946 if (!RECUR (DO_BODY (t
), any
))
9948 if (breaks (jump_target
) || continues (jump_target
))
9949 *jump_target
= NULL_TREE
;
9953 if (!RECUR (FOR_INIT_STMT (t
), any
))
9956 if (!RECUR (tmp
, rval
))
9960 if (!processing_template_decl
)
9961 tmp
= cxx_eval_outermost_constant_expr (tmp
, true);
9962 /* If we couldn't evaluate the condition, it might not ever be
9964 if (!integer_onep (tmp
))
9966 /* Before returning true, check if the for body can contain
9968 hash_set
<tree
> pset
;
9969 check_for_return_continue_data data
= { &pset
, NULL_TREE
,
9972 = cp_walk_tree (&FOR_BODY (t
), check_for_return_continue
,
9974 *jump_target
= ret_expr
;
9978 if (!RECUR (FOR_EXPR (t
), any
))
9980 if (!RECUR (FOR_BODY (t
), any
))
9982 if (breaks (jump_target
) || continues (jump_target
))
9983 *jump_target
= NULL_TREE
;
9986 case RANGE_FOR_STMT
:
9987 if (!RECUR (RANGE_FOR_INIT_STMT (t
), any
))
9989 if (!RECUR (RANGE_FOR_EXPR (t
), any
))
9991 if (!RECUR (RANGE_FOR_BODY (t
), any
))
9993 if (breaks (jump_target
) || continues (jump_target
))
9994 *jump_target
= NULL_TREE
;
9998 tmp
= WHILE_COND (t
);
9999 if (!RECUR (tmp
, rval
))
10001 if (!processing_template_decl
)
10002 tmp
= cxx_eval_outermost_constant_expr (tmp
, true);
10003 /* If we couldn't evaluate the condition, it might not ever be true. */
10004 if (!integer_onep (tmp
))
10006 /* Before returning true, check if the while body can contain
10008 hash_set
<tree
> pset
;
10009 check_for_return_continue_data data
= { &pset
, NULL_TREE
,
10012 = cp_walk_tree (&WHILE_BODY (t
), check_for_return_continue
,
10014 *jump_target
= ret_expr
;
10017 if (!RECUR (WHILE_BODY (t
), any
))
10019 if (breaks (jump_target
) || continues (jump_target
))
10020 *jump_target
= NULL_TREE
;
10024 if (!RECUR (SWITCH_STMT_COND (t
), rval
))
10026 /* FIXME we don't check SWITCH_STMT_BODY currently, because even
10027 unreachable labels would be checked and it is enough if there is
10028 a single switch cond value for which it is a valid constant
10029 expression. We need to check if there are any RETURN_EXPRs
10030 or CONTINUE_STMTs inside of the body though, as in that case
10031 we need to set *jump_target. */
10034 hash_set
<tree
> pset
;
10035 check_for_return_continue_data data
= { &pset
, NULL_TREE
,
10038 = cp_walk_tree (&SWITCH_STMT_BODY (t
), check_for_return_continue
,
10040 /* The switch might return. */
10041 *jump_target
= ret_expr
;
10042 else if (data
.continue_stmt
)
10043 /* The switch can't return, but might continue. */
10044 *jump_target
= data
.continue_stmt
;
10049 return RECUR (STMT_EXPR_STMT (t
), rval
);
10052 if (cxx_dialect
>= cxx17
)
10053 /* In C++17 lambdas can be constexpr, don't give up yet. */
10055 else if (flags
& tf_error
)
10056 constexpr_error (loc
, fundef_p
, "lambda-expression is not a "
10057 "constant expression before C++17");
10063 case VEC_DELETE_EXPR
:
10064 if (cxx_dialect
>= cxx20
)
10065 /* In C++20, new-expressions are potentially constant. */
10067 else if (flags
& tf_error
)
10068 constexpr_error (loc
, fundef_p
, "new-expression is not a "
10069 "constant expression before C++20");
10072 case DYNAMIC_CAST_EXPR
:
10073 case PSEUDO_DTOR_EXPR
:
10079 case OMP_DISTRIBUTE
:
10083 case OMP_TARGET_DATA
:
10094 case OMP_TASKGROUP
:
10095 case OMP_TARGET_UPDATE
:
10096 case OMP_TARGET_ENTER_DATA
:
10097 case OMP_TARGET_EXIT_DATA
:
10099 case OMP_ATOMIC_READ
:
10100 case OMP_ATOMIC_CAPTURE_OLD
:
10101 case OMP_ATOMIC_CAPTURE_NEW
:
10103 case OACC_PARALLEL
:
10107 case OACC_HOST_DATA
:
10111 case OACC_ENTER_DATA
:
10112 case OACC_EXIT_DATA
:
10114 case OMP_ARRAY_SECTION
:
10115 /* GCC internal stuff. */
10117 case TRANSACTION_EXPR
:
10118 case AT_ENCODE_EXPR
:
10120 if (flags
& tf_error
)
10121 constexpr_error (loc
, fundef_p
, "expression %qE is not a constant "
10126 if (flags
& tf_error
)
10127 inline_asm_in_constexpr_error (loc
, fundef_p
);
10131 if (cxx_dialect
>= cxx20
)
10132 /* In C++20 virtual calls can be constexpr, don't give up yet. */
10134 else if (flags
& tf_error
)
10135 constexpr_error (loc
, fundef_p
, "virtual functions cannot be "
10136 "%<constexpr%> before C++20");
10140 /* In C++20, a typeid expression whose operand is of polymorphic
10141 class type can be constexpr. */
10143 tree e
= TREE_OPERAND (t
, 0);
10144 if (cxx_dialect
< cxx20
10147 && !type_dependent_expression_p (e
)
10148 && TYPE_POLYMORPHIC_P (TREE_TYPE (e
)))
10150 if (flags
& tf_error
)
10151 constexpr_error (loc
, fundef_p
, "%<typeid%> is not a "
10152 "constant expression because %qE is "
10153 "of polymorphic type", e
);
10159 case POINTER_DIFF_EXPR
:
10170 case SPACESHIP_EXPR
:
10174 case PREINCREMENT_EXPR
:
10175 case POSTINCREMENT_EXPR
:
10176 case PREDECREMENT_EXPR
:
10177 case POSTDECREMENT_EXPR
:
10178 if (cxx_dialect
< cxx14
)
10183 /* A destructor. */
10184 if (TYPE_P (TREE_OPERAND (t
, 0)))
10186 /* fall through. */
10190 case FIX_TRUNC_EXPR
:
10195 case TRUTH_NOT_EXPR
:
10196 case FIXED_CONVERT_EXPR
:
10197 case UNARY_PLUS_EXPR
:
10198 case UNARY_LEFT_FOLD_EXPR
:
10199 case UNARY_RIGHT_FOLD_EXPR
:
10201 return RECUR (TREE_OPERAND (t
, 0), rval
);
10204 case CONST_CAST_EXPR
:
10205 case STATIC_CAST_EXPR
:
10206 case REINTERPRET_CAST_EXPR
:
10207 case IMPLICIT_CONV_EXPR
:
10208 if (!cast_valid_in_integral_constant_expression_p (TREE_TYPE (t
)))
10209 /* In C++98, a conversion to non-integral type can't be part of a
10210 constant expression. */
10212 if (flags
& tf_error
)
10213 constexpr_error (loc
, fundef_p
,
10214 "cast to non-integral type %qT in a constant "
10215 "expression", TREE_TYPE (t
));
10218 /* This might be a conversion from a class to a (potentially) literal
10219 type. Let's consider it potentially constant since the conversion
10220 might be a constexpr user-defined conversion. */
10221 else if (cxx_dialect
>= cxx11
10222 && (dependent_type_p (TREE_TYPE (t
))
10223 || !COMPLETE_TYPE_P (TREE_TYPE (t
))
10224 || literal_type_p (TREE_TYPE (t
)))
10225 && TREE_OPERAND (t
, 0))
10227 tree type
= TREE_TYPE (TREE_OPERAND (t
, 0));
10228 /* If this is a dependent type, it could end up being a class
10229 with conversions. */
10230 if (type
== NULL_TREE
|| WILDCARD_TYPE_P (type
))
10232 /* Or a non-dependent class which has conversions. */
10233 else if (CLASS_TYPE_P (type
)
10234 && (TYPE_HAS_CONVERSION (type
) || dependent_scope_p (type
)))
10238 return (RECUR (TREE_OPERAND (t
, 0),
10239 !TYPE_REF_P (TREE_TYPE (t
))));
10242 return RECUR (BIND_EXPR_BODY (t
), want_rval
);
10244 case CLEANUP_POINT_EXPR
:
10245 case MUST_NOT_THROW_EXPR
:
10246 case TRY_CATCH_EXPR
:
10248 case EH_SPEC_BLOCK
:
10251 /* For convenience. */
10254 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
10257 tmp
= DECL_EXPR_DECL (t
);
10258 if (VAR_P (tmp
) && !DECL_ARTIFICIAL (tmp
)
10259 && (processing_template_decl
10260 ? !decl_maybe_constant_var_p (tmp
)
10261 : !decl_constant_var_p (tmp
)))
10263 if (CP_DECL_THREAD_LOCAL_P (tmp
) && !DECL_REALLY_EXTERN (tmp
))
10265 if (flags
& tf_error
)
10266 constexpr_error (DECL_SOURCE_LOCATION (tmp
), fundef_p
,
10267 "%qD defined %<thread_local%> in "
10268 "%<constexpr%> context", tmp
);
10271 else if (TREE_STATIC (tmp
))
10273 if (flags
& tf_error
)
10274 constexpr_error (DECL_SOURCE_LOCATION (tmp
), fundef_p
,
10275 "%qD defined %<static%> in %<constexpr%> "
10279 else if (!check_for_uninitialized_const_var
10280 (tmp
, /*constexpr_context_p=*/true, flags
))
10284 return RECUR (DECL_INITIAL (tmp
), want_rval
);
10287 case TRY_FINALLY_EXPR
:
10288 return (RECUR (TREE_OPERAND (t
, 0), want_rval
)
10289 && RECUR (TREE_OPERAND (t
, 1), any
));
10292 return RECUR (TREE_OPERAND (t
, 1), want_rval
);
10295 if (!TARGET_EXPR_DIRECT_INIT_P (t
)
10296 && !TARGET_EXPR_ELIDING_P (t
)
10297 && !literal_type_p (TREE_TYPE (t
)))
10299 if (flags
& tf_error
)
10301 auto_diagnostic_group d
;
10302 if (constexpr_error (loc
, fundef_p
,
10303 "temporary of non-literal type %qT in a "
10304 "constant expression", TREE_TYPE (t
)))
10305 explain_non_literal_class (TREE_TYPE (t
));
10311 return RECUR (TREE_OPERAND (t
, 1), rval
);
10315 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (t
);
10316 constructor_elt
*ce
;
10317 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
10318 if (!RECUR (ce
->value
, want_rval
))
10325 gcc_assert (TREE_PURPOSE (t
) == NULL_TREE
10326 || DECL_P (TREE_PURPOSE (t
)));
10327 if (!RECUR (TREE_VALUE (t
), want_rval
))
10329 if (TREE_CHAIN (t
) == NULL_TREE
)
10331 return RECUR (TREE_CHAIN (t
), want_rval
);
10334 case TRUNC_DIV_EXPR
:
10335 case CEIL_DIV_EXPR
:
10336 case FLOOR_DIV_EXPR
:
10337 case ROUND_DIV_EXPR
:
10338 case TRUNC_MOD_EXPR
:
10339 case CEIL_MOD_EXPR
:
10340 case ROUND_MOD_EXPR
:
10342 tree denom
= TREE_OPERAND (t
, 1);
10343 if (!RECUR (denom
, rval
))
10345 /* We can't call cxx_eval_outermost_constant_expr on an expression
10346 that hasn't been through instantiate_non_dependent_expr yet. */
10347 if (!processing_template_decl
)
10348 denom
= cxx_eval_outermost_constant_expr (denom
, true);
10349 if (integer_zerop (denom
))
10351 if (flags
& tf_error
)
10352 constexpr_error (input_location
, fundef_p
,
10353 "division by zero is not a constant expression");
10359 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
10363 case COMPOUND_EXPR
:
10365 /* check_return_expr sometimes wraps a TARGET_EXPR in a
10366 COMPOUND_EXPR; don't get confused. */
10367 tree op0
= TREE_OPERAND (t
, 0);
10368 tree op1
= TREE_OPERAND (t
, 1);
10370 if (TREE_CODE (op0
) == TARGET_EXPR
&& op1
== TARGET_EXPR_SLOT (op0
))
10371 return RECUR (op0
, want_rval
);
10376 /* If the first operand is the non-short-circuit constant, look at
10377 the second operand; otherwise we only care about the first one for
10379 case TRUTH_AND_EXPR
:
10380 case TRUTH_ANDIF_EXPR
:
10381 tmp
= boolean_true_node
;
10383 case TRUTH_OR_EXPR
:
10384 case TRUTH_ORIF_EXPR
:
10385 tmp
= boolean_false_node
;
10388 tree op0
= TREE_OPERAND (t
, 0);
10389 tree op1
= TREE_OPERAND (t
, 1);
10390 if (!RECUR (op0
, rval
))
10392 if (!(flags
& tf_error
) && RECUR (op1
, rval
))
10393 /* When quiet, try to avoid expensive trial evaluation by first
10394 checking potentiality of the second operand. */
10396 if (!processing_template_decl
)
10397 op0
= cxx_eval_outermost_constant_expr (op0
, true);
10398 if (tree_int_cst_equal (op0
, tmp
))
10399 return (flags
& tf_error
) ? RECUR (op1
, rval
) : false;
10406 case POINTER_PLUS_EXPR
:
10408 case EXACT_DIV_EXPR
:
10418 case TRUTH_XOR_EXPR
:
10419 case UNORDERED_EXPR
:
10430 /* Fall through. */
10432 case ARRAY_RANGE_REF
:
10436 case BINARY_LEFT_FOLD_EXPR
:
10437 case BINARY_RIGHT_FOLD_EXPR
:
10439 for (i
= 0; i
< 2; ++i
)
10440 if (!RECUR (TREE_OPERAND (t
, i
), want_rval
))
10444 case VEC_PERM_EXPR
:
10445 for (i
= 0; i
< 3; ++i
)
10446 if (!RECUR (TREE_OPERAND (t
, i
), true))
10451 if (COND_EXPR_IS_VEC_DELETE (t
) && cxx_dialect
< cxx20
)
10453 if (flags
& tf_error
)
10454 constexpr_error (loc
, fundef_p
, "%<delete[]%> is not a "
10455 "constant expression");
10458 /* Fall through. */
10460 case VEC_COND_EXPR
:
10461 /* If the condition is a known constant, we know which of the legs we
10462 care about; otherwise we only require that the condition and
10463 either of the legs be potentially constant. */
10464 tmp
= TREE_OPERAND (t
, 0);
10465 if (!RECUR (tmp
, rval
))
10467 if (!processing_template_decl
)
10468 tmp
= cxx_eval_outermost_constant_expr (tmp
, true);
10469 /* potential_constant_expression* isn't told if it is called for
10470 manifestly_const_eval or not, so for consteval if always
10471 process both branches as if the condition is not a known
10473 if (TREE_CODE (t
) != IF_STMT
|| !IF_STMT_CONSTEVAL_P (t
))
10475 if (integer_zerop (tmp
))
10476 return RECUR (TREE_OPERAND (t
, 2), want_rval
);
10477 else if (TREE_CODE (tmp
) == INTEGER_CST
)
10478 return RECUR (TREE_OPERAND (t
, 1), want_rval
);
10480 tmp
= *jump_target
;
10481 for (i
= 1; i
< 3; ++i
)
10483 tree this_jump_target
= tmp
;
10484 if (potential_constant_expression_1 (TREE_OPERAND (t
, i
),
10485 want_rval
, strict
, now
, fundef_p
,
10486 tf_none
, &this_jump_target
))
10488 if (returns (&this_jump_target
))
10489 *jump_target
= this_jump_target
;
10490 else if (!returns (jump_target
))
10492 if (breaks (&this_jump_target
)
10493 || continues (&this_jump_target
))
10494 *jump_target
= this_jump_target
;
10497 /* If the then branch is potentially constant, but
10498 does not return, check if the else branch
10499 couldn't return, break or continue. */
10500 hash_set
<tree
> pset
;
10501 check_for_return_continue_data data
= { &pset
, NULL_TREE
,
10504 = cp_walk_tree (&TREE_OPERAND (t
, 2),
10505 check_for_return_continue
, &data
,
10507 *jump_target
= ret_expr
;
10508 else if (*jump_target
== NULL_TREE
)
10510 if (data
.continue_stmt
)
10511 *jump_target
= data
.continue_stmt
;
10512 else if (data
.break_stmt
)
10513 *jump_target
= data
.break_stmt
;
10520 if (flags
& tf_error
)
10522 if (TREE_CODE (t
) == IF_STMT
)
10523 constexpr_error (loc
, fundef_p
, "neither branch of %<if%> is a "
10524 "constant expression");
10526 constexpr_error (loc
, fundef_p
, "expression %qE is not a "
10527 "constant expression", t
);
10531 case VEC_INIT_EXPR
:
10532 if (VEC_INIT_EXPR_IS_CONSTEXPR (t
))
10534 if (flags
& tf_error
)
10536 if (constexpr_error (loc
, fundef_p
, "non-constant array "
10538 diagnose_non_constexpr_vec_init (t
);
10544 /* We can see these in statement-expressions. */
10548 if (!RECUR (CLEANUP_BODY (t
), any
))
10550 if (!CLEANUP_EH_ONLY (t
) && !RECUR (CLEANUP_EXPR (t
), any
))
10554 case EMPTY_CLASS_EXPR
:
10559 tree
*target
= &TREE_OPERAND (t
, 0);
10560 /* Gotos representing break, continue and cdtor return are OK. */
10561 if (breaks (target
) || continues (target
) || returns (target
))
10563 *jump_target
= *target
;
10566 if (flags
& tf_error
)
10567 constexpr_error (loc
, fundef_p
, "%<goto%> is not a constant "
10572 case ASSERTION_STMT
:
10573 case PRECONDITION_STMT
:
10574 case POSTCONDITION_STMT
:
10575 if (!checked_contract_p (get_contract_semantic (t
)))
10577 return RECUR (CONTRACT_CONDITION (t
), rval
);
10580 t
= LABEL_EXPR_LABEL (t
);
10581 if (DECL_ARTIFICIAL (t
) || cxx_dialect
>= cxx23
)
10583 else if (flags
& tf_error
)
10584 constexpr_error (loc
, fundef_p
, "label definition in %<constexpr%> "
10585 "function only available with %<-std=c++2b%> or "
10586 "%<-std=gnu++2b%>");
10589 case ANNOTATE_EXPR
:
10590 return RECUR (TREE_OPERAND (t
, 0), rval
);
10592 case BIT_CAST_EXPR
:
10593 return RECUR (TREE_OPERAND (t
, 0), rval
);
10595 /* Coroutine await, yield and return expressions are not. */
10596 case CO_AWAIT_EXPR
:
10597 case CO_YIELD_EXPR
:
10598 case CO_RETURN_EXPR
:
10601 case NONTYPE_ARGUMENT_PACK
:
10603 tree args
= ARGUMENT_PACK_ARGS (t
);
10604 int len
= TREE_VEC_LENGTH (args
);
10605 for (int i
= 0; i
< len
; ++i
)
10606 if (!RECUR (TREE_VEC_ELT (args
, i
), any
))
10612 if (objc_non_constant_expr_p (t
))
10615 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t
)));
10616 gcc_unreachable ();
10623 potential_constant_expression_1 (tree t
, bool want_rval
, bool strict
, bool now
,
10624 bool fundef_p
, tsubst_flags_t flags
)
10626 if (flags
& tf_error
)
10628 /* Check potentiality quietly first, as that could be performed more
10629 efficiently in some cases (currently only for TRUTH_*_EXPR). If
10630 that fails, replay the check noisily to give errors. */
10631 flags
&= ~tf_error
;
10632 if (potential_constant_expression_1 (t
, want_rval
, strict
, now
, fundef_p
,
10638 tree target
= NULL_TREE
;
10639 return potential_constant_expression_1 (t
, want_rval
, strict
, now
, fundef_p
,
10643 /* The main entry point to the above. */
10646 potential_constant_expression (tree t
)
10648 return potential_constant_expression_1 (t
, /*want_rval*/false, /*strict*/true,
10649 /*now*/false, /*fundef_p*/false,
10653 /* As above, but require a constant rvalue. */
10656 potential_rvalue_constant_expression (tree t
)
10658 return potential_constant_expression_1 (t
, /*want_rval*/true, /*strict*/true,
10659 /*now*/false, /*fundef_p*/false,
10663 /* Like above, but complain about non-constant expressions. */
10666 require_potential_constant_expression (tree t
)
10668 return potential_constant_expression_1 (t
, /*want_rval*/false, /*strict*/true,
10669 /*now*/false, /*fundef_p*/false,
10670 tf_warning_or_error
);
10673 /* Cross product of the above. */
10676 require_potential_rvalue_constant_expression (tree t
)
10678 return potential_constant_expression_1 (t
, /*want_rval*/true, /*strict*/true,
10679 /*now*/false, /*fundef_p*/false,
10680 tf_warning_or_error
);
10683 /* Like require_potential_rvalue_constant_expression, but fundef_p is true. */
10686 require_potential_rvalue_constant_expression_fncheck (tree t
)
10688 return potential_constant_expression_1 (t
, /*want_rval*/true, /*strict*/true,
10689 /*now*/false, /*fundef_p*/true,
10690 tf_warning_or_error
);
10693 /* Like above, but don't consider PARM_DECL a potential_constant_expression. */
10696 require_rvalue_constant_expression (tree t
)
10698 return potential_constant_expression_1 (t
, /*want_rval*/true, /*strict*/true,
10699 /*now*/true, /*fundef_p*/false,
10700 tf_warning_or_error
);
10703 /* Like potential_constant_expression, but don't consider possible constexpr
10704 substitution of the current function. That is, PARM_DECL qualifies under
10705 potential_constant_expression, but not here.
10707 This is basically what you can check when any actual constant values might
10708 be value-dependent. */
10711 is_constant_expression (tree t
)
10713 return potential_constant_expression_1 (t
, /*want_rval*/false, /*strict*/true,
10714 /*now*/true, /*fundef_p*/false,
10718 /* As above, but expect an rvalue. */
10721 is_rvalue_constant_expression (tree t
)
10723 return potential_constant_expression_1 (t
, /*want_rval*/true, /*strict*/true,
10724 /*now*/true, /*fundef_p*/false,
10728 /* Like above, but complain about non-constant expressions. */
10731 require_constant_expression (tree t
)
10733 return potential_constant_expression_1 (t
, /*want_rval*/false, /*strict*/true,
10734 /*now*/true, /*fundef_p*/false,
10735 tf_warning_or_error
);
10738 /* Like is_constant_expression, but allow const variables that are not allowed
10739 under constexpr rules. */
10742 is_static_init_expression (tree t
)
10744 return potential_constant_expression_1 (t
, /*want_rval*/false,
10745 /*strict*/false, /*now*/true,
10746 /*fundef_p*/false, tf_none
);
10749 /* Returns true if T is a potential constant expression that is not
10750 instantiation-dependent, and therefore a candidate for constant folding even
10754 is_nondependent_constant_expression (tree t
)
10756 return (!type_unknown_p (t
)
10757 && is_constant_expression (t
)
10758 && !instantiation_dependent_expression_p (t
));
10761 /* Returns true if T is a potential static initializer expression that is not
10762 instantiation-dependent. */
10765 is_nondependent_static_init_expression (tree t
)
10767 return (!type_unknown_p (t
)
10768 && is_static_init_expression (t
)
10769 && !instantiation_dependent_expression_p (t
));
10772 /* True iff FN is an implicitly constexpr function. */
10775 decl_implicit_constexpr_p (tree fn
)
10777 if (!(flag_implicit_constexpr
10778 && TREE_CODE (fn
) == FUNCTION_DECL
10779 && DECL_DECLARED_CONSTEXPR_P (fn
)))
10782 if (DECL_CLONED_FUNCTION_P (fn
))
10783 fn
= DECL_CLONED_FUNCTION (fn
);
10785 return (DECL_LANG_SPECIFIC (fn
)
10786 && DECL_LANG_SPECIFIC (fn
)->u
.fn
.implicit_constexpr
);
10789 /* Finalize constexpr processing after parsing. */
10792 fini_constexpr (void)
10794 /* The contexpr call and fundef copies tables are no longer needed. */
10795 constexpr_call_table
= NULL
;
10796 fundef_copies_table
= NULL
;
10799 #include "gt-cp-constexpr.h"