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-2023 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
))
267 if (cxx_dialect
< cxx20
)
271 error_at (DECL_SOURCE_LOCATION (fun
),
272 "%<constexpr%> destructors only available"
273 " with %<-std=c++20%> or %<-std=gnu++20%>");
276 else if (!DECL_CONSTRUCTOR_P (fun
))
278 tree rettype
= TREE_TYPE (TREE_TYPE (fun
));
279 if (!literal_type_p (rettype
))
284 auto_diagnostic_group d
;
285 if (constexpr_error (input_location
, /*constexpr_fundef_p*/true,
286 "invalid return type %qT of %<constexpr%> "
287 "function %q+D", rettype
, fun
))
288 explain_non_literal_class (rettype
);
292 /* C++14 DR 1684 removed this restriction. */
293 if (cxx_dialect
< cxx14
294 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fun
)
295 && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun
)))
300 auto_diagnostic_group d
;
301 if (pedwarn (DECL_SOURCE_LOCATION (fun
), OPT_Wpedantic
,
302 "enclosing class of %<constexpr%> non-static"
303 " member function %q+#D is not a literal type",
305 explain_non_literal_class (DECL_CONTEXT (fun
));
309 else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun
)))
313 error ("%q#T has virtual base classes", DECL_CONTEXT (fun
));
319 /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF
320 for a member of an anonymous aggregate, INIT is the initializer for that
321 member, and VEC_OUTER is the vector of constructor elements for the class
322 whose constructor we are processing. Add the initializer to the vector
323 and return true to indicate success. */
326 build_anon_member_initialization (tree member
, tree init
,
327 vec
<constructor_elt
, va_gc
> **vec_outer
)
329 /* MEMBER presents the relevant fields from the inside out, but we need
330 to build up the initializer from the outside in so that we can reuse
331 previously built CONSTRUCTORs if this is, say, the second field in an
332 anonymous struct. So we use a vec as a stack. */
333 auto_vec
<tree
, 2> fields
;
336 fields
.safe_push (TREE_OPERAND (member
, 1));
337 member
= TREE_OPERAND (member
, 0);
339 while (ANON_AGGR_TYPE_P (TREE_TYPE (member
))
340 && TREE_CODE (member
) == COMPONENT_REF
);
342 /* VEC has the constructor elements vector for the context of FIELD.
343 If FIELD is an anonymous aggregate, we will push inside it. */
344 vec
<constructor_elt
, va_gc
> **vec
= vec_outer
;
346 while (field
= fields
.pop(),
347 ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
350 /* If there is already an outer constructor entry for the anonymous
351 aggregate FIELD, use it; otherwise, insert one. */
352 if (vec_safe_is_empty (*vec
)
353 || (*vec
)->last().index
!= field
)
355 ctor
= build_constructor (TREE_TYPE (field
), NULL
);
356 CONSTRUCTOR_APPEND_ELT (*vec
, field
, ctor
);
359 ctor
= (*vec
)->last().value
;
360 vec
= &CONSTRUCTOR_ELTS (ctor
);
363 /* Now we're at the innermost field, the one that isn't an anonymous
364 aggregate. Add its initializer to the CONSTRUCTOR and we're done. */
365 gcc_assert (fields
.is_empty());
366 CONSTRUCTOR_APPEND_ELT (*vec
, field
, init
);
371 /* Subroutine of build_constexpr_constructor_member_initializers.
372 The expression tree T represents a data member initialization
373 in a (constexpr) constructor definition. Build a pairing of
374 the data member with its initializer, and prepend that pair
375 to the existing initialization pair INITS. */
378 build_data_member_initialization (tree t
, vec
<constructor_elt
, va_gc
> **vec
)
381 if (TREE_CODE (t
) == CLEANUP_POINT_EXPR
)
382 t
= TREE_OPERAND (t
, 0);
383 if (TREE_CODE (t
) == EXPR_STMT
)
384 t
= TREE_OPERAND (t
, 0);
385 if (t
== error_mark_node
)
387 if (TREE_CODE (t
) == STATEMENT_LIST
)
389 for (tree stmt
: tsi_range (t
))
390 if (! build_data_member_initialization (stmt
, vec
))
394 if (TREE_CODE (t
) == CLEANUP_STMT
)
396 /* We can't see a CLEANUP_STMT in a constructor for a literal class,
397 but we can in a constexpr constructor for a non-literal class. Just
398 ignore it; either all the initialization will be constant, in which
399 case the cleanup can't run, or it can't be constexpr.
400 Still recurse into CLEANUP_BODY. */
401 return build_data_member_initialization (CLEANUP_BODY (t
), vec
);
403 if (TREE_CODE (t
) == CONVERT_EXPR
)
404 t
= TREE_OPERAND (t
, 0);
405 if (TREE_CODE (t
) == INIT_EXPR
406 /* vptr initialization shows up as a MODIFY_EXPR. In C++14 we only
407 use what this function builds for cx_check_missing_mem_inits, and
408 assignment in the ctor body doesn't count. */
409 || (cxx_dialect
< cxx14
&& TREE_CODE (t
) == MODIFY_EXPR
))
411 member
= TREE_OPERAND (t
, 0);
412 init
= break_out_target_exprs (TREE_OPERAND (t
, 1));
414 else if (TREE_CODE (t
) == CALL_EXPR
)
416 tree fn
= get_callee_fndecl (t
);
417 if (!fn
|| !DECL_CONSTRUCTOR_P (fn
))
418 /* We're only interested in calls to subobject constructors. */
420 member
= CALL_EXPR_ARG (t
, 0);
421 /* We don't use build_cplus_new here because it complains about
422 abstract bases. Leaving the call unwrapped means that it has the
423 wrong type, but cxx_eval_constant_expression doesn't care. */
424 init
= break_out_target_exprs (t
);
426 else if (TREE_CODE (t
) == BIND_EXPR
)
427 return build_data_member_initialization (BIND_EXPR_BODY (t
), vec
);
429 /* Don't add anything else to the CONSTRUCTOR. */
431 if (INDIRECT_REF_P (member
))
432 member
= TREE_OPERAND (member
, 0);
433 if (TREE_CODE (member
) == NOP_EXPR
)
437 if (TREE_CODE (op
) == ADDR_EXPR
)
439 gcc_assert (same_type_ignoring_top_level_qualifiers_p
440 (TREE_TYPE (TREE_TYPE (op
)),
441 TREE_TYPE (TREE_TYPE (member
))));
442 /* Initializing a cv-qualified member; we need to look through
446 else if (op
== current_class_ptr
447 && (same_type_ignoring_top_level_qualifiers_p
448 (TREE_TYPE (TREE_TYPE (member
)),
449 current_class_type
)))
450 /* Delegating constructor. */
454 /* This is an initializer for an empty base; keep it for now so
455 we can check it in cxx_eval_bare_aggregate. */
456 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member
))));
459 if (TREE_CODE (member
) == ADDR_EXPR
)
460 member
= TREE_OPERAND (member
, 0);
461 if (TREE_CODE (member
) == COMPONENT_REF
)
463 tree aggr
= TREE_OPERAND (member
, 0);
464 if (TREE_CODE (aggr
) == VAR_DECL
)
465 /* Initializing a local variable, don't add anything. */
467 if (TREE_CODE (aggr
) != COMPONENT_REF
)
468 /* Normal member initialization. */
469 member
= TREE_OPERAND (member
, 1);
470 else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr
)))
471 /* Initializing a member of an anonymous union. */
472 return build_anon_member_initialization (member
, init
, vec
);
474 /* We're initializing a vtable pointer in a base. Leave it as
475 COMPONENT_REF so we remember the path to get to the vfield. */
476 gcc_assert (TREE_TYPE (member
) == vtbl_ptr_type_node
);
479 /* Value-initialization can produce multiple initializers for the
480 same field; use the last one. */
481 if (!vec_safe_is_empty (*vec
) && (*vec
)->last().index
== member
)
482 (*vec
)->last().value
= init
;
484 CONSTRUCTOR_APPEND_ELT (*vec
, member
, init
);
488 /* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval.
489 In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a
490 BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */
493 check_constexpr_bind_expr_vars (tree t
)
495 gcc_assert (TREE_CODE (t
) == BIND_EXPR
);
497 for (tree var
= BIND_EXPR_VARS (t
); var
; var
= DECL_CHAIN (var
))
498 if (TREE_CODE (var
) == TYPE_DECL
499 && DECL_IMPLICIT_TYPEDEF_P (var
)
500 && !LAMBDA_TYPE_P (TREE_TYPE (var
)))
505 /* Subroutine of check_constexpr_ctor_body. */
508 check_constexpr_ctor_body_1 (tree last
, tree list
)
510 switch (TREE_CODE (list
))
513 if (TREE_CODE (DECL_EXPR_DECL (list
)) == USING_DECL
514 || TREE_CODE (DECL_EXPR_DECL (list
)) == TYPE_DECL
)
518 case CLEANUP_POINT_EXPR
:
519 return check_constexpr_ctor_body (last
, TREE_OPERAND (list
, 0),
523 if (!check_constexpr_bind_expr_vars (list
)
524 || !check_constexpr_ctor_body (last
, BIND_EXPR_BODY (list
),
531 case DEBUG_BEGIN_STMT
:
539 /* Make sure that there are no statements after LAST in the constructor
540 body represented by LIST. */
543 check_constexpr_ctor_body (tree last
, tree list
, bool complain
)
545 /* C++14 doesn't require a constexpr ctor to have an empty body. */
546 if (cxx_dialect
>= cxx14
)
550 if (TREE_CODE (list
) == STATEMENT_LIST
)
552 tree_stmt_iterator i
= tsi_last (list
);
553 for (; !tsi_end_p (i
); tsi_prev (&i
))
555 tree t
= tsi_stmt (i
);
558 if (!check_constexpr_ctor_body_1 (last
, t
))
565 else if (list
!= last
566 && !check_constexpr_ctor_body_1 (last
, list
))
571 error ("%<constexpr%> constructor does not have empty body");
572 DECL_DECLARED_CONSTEXPR_P (current_function_decl
) = false;
577 /* V is a vector of constructor elements built up for the base and member
578 initializers of a constructor for TYPE. They need to be in increasing
579 offset order, which they might not be yet if TYPE has a primary base
580 which is not first in the base-clause or a vptr and at least one base
581 all of which are non-primary. */
583 static vec
<constructor_elt
, va_gc
> *
584 sort_constexpr_mem_initializers (tree type
, vec
<constructor_elt
, va_gc
> *v
)
586 tree pri
= CLASSTYPE_PRIMARY_BINFO (type
);
592 field_type
= BINFO_TYPE (pri
);
593 else if (TYPE_CONTAINS_VPTR_P (type
))
594 field_type
= vtbl_ptr_type_node
;
598 /* Find the element for the primary base or vptr and move it to the
599 beginning of the vec. */
600 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
601 if (TREE_TYPE (ce
->index
) == field_type
)
604 if (i
> 0 && i
< vec_safe_length (v
))
606 vec
<constructor_elt
, va_gc
> &vref
= *v
;
607 constructor_elt elt
= vref
[i
];
616 /* Build compile-time evalable representations of member-initializer list
617 for a constexpr constructor. */
620 build_constexpr_constructor_member_initializers (tree type
, tree body
)
622 vec
<constructor_elt
, va_gc
> *vec
= NULL
;
625 switch (TREE_CODE (body
))
627 case MUST_NOT_THROW_EXPR
:
629 body
= TREE_OPERAND (body
, 0);
633 for (tree stmt
: tsi_range (body
))
636 if (TREE_CODE (body
) == BIND_EXPR
)
642 body
= BIND_EXPR_BODY (body
);
649 if (TREE_CODE (body
) == TRY_BLOCK
)
651 body
= TREE_OPERAND (body
, 0);
652 if (TREE_CODE (body
) == BIND_EXPR
)
653 body
= BIND_EXPR_BODY (body
);
655 if (TREE_CODE (body
) == CLEANUP_POINT_EXPR
)
657 body
= TREE_OPERAND (body
, 0);
658 if (TREE_CODE (body
) == EXPR_STMT
)
659 body
= TREE_OPERAND (body
, 0);
660 if (TREE_CODE (body
) == INIT_EXPR
661 && (same_type_ignoring_top_level_qualifiers_p
662 (TREE_TYPE (TREE_OPERAND (body
, 0)),
663 current_class_type
)))
666 return TREE_OPERAND (body
, 1);
668 ok
= build_data_member_initialization (body
, &vec
);
670 else if (TREE_CODE (body
) == STATEMENT_LIST
)
672 for (tree stmt
: tsi_range (body
))
674 ok
= build_data_member_initialization (stmt
, &vec
);
679 else if (EXPR_P (body
))
680 ok
= build_data_member_initialization (body
, &vec
);
682 gcc_assert (errorcount
> 0);
685 if (vec_safe_length (vec
) > 0)
687 /* In a delegating constructor, return the target. */
688 constructor_elt
*ce
= &(*vec
)[0];
689 if (ce
->index
== current_class_ptr
)
696 vec
= sort_constexpr_mem_initializers (type
, vec
);
697 return build_constructor (type
, vec
);
700 return error_mark_node
;
703 /* We have an expression tree T that represents a call, either CALL_EXPR
704 or AGGR_INIT_EXPR. If the call is lexically to a named function,
705 retrun the _DECL for that function. */
708 get_function_named_in_call (tree t
)
710 tree fun
= cp_get_callee (t
);
711 if (fun
&& TREE_CODE (fun
) == ADDR_EXPR
712 && TREE_CODE (TREE_OPERAND (fun
, 0)) == FUNCTION_DECL
)
713 fun
= TREE_OPERAND (fun
, 0);
717 /* Subroutine of check_constexpr_fundef. BODY is the body of a function
718 declared to be constexpr, or a sub-statement thereof. Returns the
719 return value if suitable, error_mark_node for a statement not allowed in
720 a constexpr function, or NULL_TREE if no return value was found. */
723 constexpr_fn_retval (tree body
)
725 switch (TREE_CODE (body
))
729 tree expr
= NULL_TREE
;
730 for (tree stmt
: tsi_range (body
))
732 tree s
= constexpr_fn_retval (stmt
);
733 if (s
== error_mark_node
)
734 return error_mark_node
;
735 else if (s
== NULL_TREE
)
736 /* Keep iterating. */;
738 /* Multiple return statements. */
739 return error_mark_node
;
747 return break_out_target_exprs (TREE_OPERAND (body
, 0));
751 tree decl
= DECL_EXPR_DECL (body
);
752 if (TREE_CODE (decl
) == USING_DECL
753 /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */
754 || DECL_ARTIFICIAL (decl
))
756 return error_mark_node
;
759 case CLEANUP_POINT_EXPR
:
760 return constexpr_fn_retval (TREE_OPERAND (body
, 0));
763 if (!check_constexpr_bind_expr_vars (body
))
764 return error_mark_node
;
765 return constexpr_fn_retval (BIND_EXPR_BODY (body
));
768 case DEBUG_BEGIN_STMT
:
773 tree fun
= get_function_named_in_call (body
);
775 && fndecl_built_in_p (fun
, BUILT_IN_UNREACHABLE
))
781 return error_mark_node
;
785 /* Subroutine of check_constexpr_fundef. BODY is the DECL_SAVED_TREE of
786 FUN; do the necessary transformations to turn it into a single expression
787 that we can store in the hash table. */
790 massage_constexpr_body (tree fun
, tree body
)
792 if (DECL_CONSTRUCTOR_P (fun
))
793 body
= build_constexpr_constructor_member_initializers
794 (DECL_CONTEXT (fun
), body
);
795 else if (cxx_dialect
< cxx14
)
797 if (TREE_CODE (body
) == EH_SPEC_BLOCK
)
798 body
= EH_SPEC_STMTS (body
);
799 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
800 body
= TREE_OPERAND (body
, 0);
801 body
= constexpr_fn_retval (body
);
806 /* CTYPE is a type constructed from BODY. Return true if some
807 bases/fields are uninitialized, and complain if COMPLAIN. */
810 cx_check_missing_mem_inits (tree ctype
, tree body
, bool complain
)
812 /* We allow uninitialized bases/fields in C++20. */
813 if (cxx_dialect
>= cxx20
)
820 if (TREE_CODE (body
) != CONSTRUCTOR
)
822 nelts
= CONSTRUCTOR_NELTS (body
);
824 tree field
= TYPE_FIELDS (ctype
);
826 if (TREE_CODE (ctype
) == UNION_TYPE
)
828 if (nelts
== 0 && next_aggregate_field (field
))
831 error ("%<constexpr%> constructor for union %qT must "
832 "initialize exactly one non-static data member", ctype
);
838 /* Iterate over the CONSTRUCTOR, checking any missing fields don't
839 need an explicit initialization. */
841 for (unsigned i
= 0; i
<= nelts
; ++i
)
843 tree index
= NULL_TREE
;
846 index
= CONSTRUCTOR_ELT (body
, i
)->index
;
847 /* Skip base and vtable inits. */
848 if (TREE_CODE (index
) != FIELD_DECL
849 || DECL_ARTIFICIAL (index
))
853 for (; field
!= index
; field
= DECL_CHAIN (field
))
856 if (TREE_CODE (field
) != FIELD_DECL
)
858 if (DECL_UNNAMED_BIT_FIELD (field
))
860 if (DECL_ARTIFICIAL (field
))
862 if (ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
864 /* Recurse to check the anonymous aggregate member. */
865 bad
|= cx_check_missing_mem_inits
866 (TREE_TYPE (field
), NULL_TREE
, complain
);
867 if (bad
&& !complain
)
871 ftype
= TREE_TYPE (field
);
872 if (!ftype
|| !TYPE_P (ftype
) || !COMPLETE_TYPE_P (ftype
))
873 /* A flexible array can't be intialized here, so don't complain
876 if (is_empty_field (field
))
877 /* An empty field doesn't need an initializer. */
879 ftype
= strip_array_types (ftype
);
880 if (type_has_constexpr_default_constructor (ftype
))
882 /* It's OK to skip a member with a trivial constexpr ctor.
883 A constexpr ctor that isn't trivial should have been
885 gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype
)
891 auto_diagnostic_group d
;
892 error ("member %qD must be initialized by mem-initializer "
893 "in %<constexpr%> constructor", field
);
894 inform (DECL_SOURCE_LOCATION (field
), "declared here");
897 if (field
== NULL_TREE
)
900 if (ANON_AGGR_TYPE_P (TREE_TYPE (index
)))
902 /* Check the anonymous aggregate initializer is valid. */
903 bad
|= cx_check_missing_mem_inits
904 (TREE_TYPE (index
), CONSTRUCTOR_ELT (body
, i
)->value
, complain
);
905 if (bad
&& !complain
)
908 field
= DECL_CHAIN (field
);
914 /* We are processing the definition of the constexpr function FUN.
915 Check that its body fulfills the apropriate requirements and
916 enter it in the constexpr function definition table. */
919 maybe_save_constexpr_fundef (tree fun
)
921 if (processing_template_decl
922 || cp_function_chain
->invalid_constexpr
923 || (DECL_CLONED_FUNCTION_P (fun
) && !DECL_DELETING_DESTRUCTOR_P (fun
)))
926 /* With -fimplicit-constexpr, try to make inlines constexpr. We'll
927 actually set DECL_DECLARED_CONSTEXPR_P below if the checks pass. */
928 bool implicit
= false;
929 if (flag_implicit_constexpr
)
931 if (DECL_DELETING_DESTRUCTOR_P (fun
)
932 && decl_implicit_constexpr_p (DECL_CLONED_FUNCTION (fun
)))
933 /* Don't inherit implicit constexpr from the non-deleting
935 DECL_DECLARED_CONSTEXPR_P (fun
) = false;
937 if (!DECL_DECLARED_CONSTEXPR_P (fun
)
938 && DECL_DECLARED_INLINE_P (fun
)
939 && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fun
)))
943 if (!DECL_DECLARED_CONSTEXPR_P (fun
) && !implicit
)
946 bool complain
= !DECL_GENERATED_P (fun
) && !implicit
;
948 if (!is_valid_constexpr_fn (fun
, complain
))
951 tree massaged
= massage_constexpr_body (fun
, DECL_SAVED_TREE (fun
));
952 if (massaged
== NULL_TREE
|| massaged
== error_mark_node
)
954 if (!DECL_CONSTRUCTOR_P (fun
) && complain
)
955 error ("body of %<constexpr%> function %qD not a return-statement",
960 bool potential
= potential_rvalue_constant_expression (massaged
);
961 if (!potential
&& complain
)
962 require_potential_rvalue_constant_expression_fncheck (massaged
);
964 if (DECL_CONSTRUCTOR_P (fun
) && potential
965 && !DECL_DEFAULTED_FN (fun
))
967 if (cx_check_missing_mem_inits (DECL_CONTEXT (fun
),
970 else if (cxx_dialect
> cxx11
)
972 /* What we got from massage_constexpr_body is pretty much just the
973 ctor-initializer, also check the body. */
974 massaged
= DECL_SAVED_TREE (fun
);
975 potential
= potential_rvalue_constant_expression (massaged
);
976 if (!potential
&& complain
)
977 require_potential_rvalue_constant_expression_fncheck (massaged
);
981 if (!potential
&& complain
982 /* If -Wno-invalid-constexpr was specified, we haven't complained
983 about non-constant expressions yet. Register the function and
984 complain in explain_invalid_constexpr_fn if the function is
986 && warn_invalid_constexpr
!= 0)
993 DECL_DECLARED_CONSTEXPR_P (fun
) = true;
994 DECL_LANG_SPECIFIC (fun
)->u
.fn
.implicit_constexpr
= true;
995 if (DECL_CONSTRUCTOR_P (fun
))
996 TYPE_HAS_CONSTEXPR_CTOR (DECL_CONTEXT (fun
)) = true;
999 /* Don't bother keeping the pre-generic body of unsuitable functions
1000 not explicitly declared constexpr. */
1004 constexpr_fundef entry
= {fun
, NULL_TREE
, NULL_TREE
, NULL_TREE
};
1005 bool clear_ctx
= false;
1006 if (DECL_RESULT (fun
) && DECL_CONTEXT (DECL_RESULT (fun
)) == NULL_TREE
)
1009 DECL_CONTEXT (DECL_RESULT (fun
)) = fun
;
1011 tree saved_fn
= current_function_decl
;
1012 current_function_decl
= fun
;
1013 entry
.body
= copy_fn (entry
.decl
, entry
.parms
, entry
.result
);
1014 current_function_decl
= saved_fn
;
1016 DECL_CONTEXT (DECL_RESULT (entry
.decl
)) = NULL_TREE
;
1018 /* For a template instantiation, we want to remember the pre-generic body
1019 for explain_invalid_constexpr_fn, but do tell cxx_eval_call_expression
1020 that it doesn't need to bother trying to expand the function. */
1021 entry
.result
= error_mark_node
;
1023 register_constexpr_fundef (entry
);
1026 /* BODY is a validated and massaged definition of a constexpr
1027 function. Register it in the hash table. */
1030 register_constexpr_fundef (const constexpr_fundef
&value
)
1032 /* Create the constexpr function table if necessary. */
1033 if (constexpr_fundef_table
== NULL
)
1034 constexpr_fundef_table
1035 = hash_table
<constexpr_fundef_hasher
>::create_ggc (101);
1037 constexpr_fundef
**slot
= constexpr_fundef_table
->find_slot
1038 (const_cast<constexpr_fundef
*> (&value
), INSERT
);
1040 gcc_assert (*slot
== NULL
);
1041 *slot
= ggc_alloc
<constexpr_fundef
> ();
1045 /* FUN is a non-constexpr (or, with -Wno-invalid-constexpr, a constexpr
1046 function called in a context that requires a constant expression).
1047 If it comes from a constexpr template, explain why the instantiation
1048 isn't constexpr. Otherwise, explain why the function cannot be used
1049 in a constexpr context. */
1052 explain_invalid_constexpr_fn (tree fun
)
1054 static hash_set
<tree
> *diagnosed
;
1056 /* In C++23, a function marked 'constexpr' may not actually be a constant
1057 expression. We haven't diagnosed the problem yet: -Winvalid-constexpr
1058 wasn't enabled. The function was called, so diagnose why it cannot be
1059 used in a constant expression. */
1060 if (warn_invalid_constexpr
== 0 && DECL_DECLARED_CONSTEXPR_P (fun
))
1062 /* Only diagnose defaulted functions, lambdas, or instantiations. */
1063 else if (!DECL_DEFAULTED_FN (fun
)
1064 && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun
))
1065 && !is_instantiation_of_constexpr (fun
))
1067 inform (DECL_SOURCE_LOCATION (fun
), "%qD declared here", fun
);
1070 if (diagnosed
== NULL
)
1071 diagnosed
= new hash_set
<tree
>;
1072 if (diagnosed
->add (fun
))
1073 /* Already explained. */
1076 iloc_sentinel ils
= input_location
;
1077 if (!lambda_static_thunk_p (fun
))
1079 /* Diagnostics should completely ignore the static thunk, so leave
1080 input_location set to our caller's location. */
1081 input_location
= DECL_SOURCE_LOCATION (fun
);
1082 inform (input_location
,
1083 "%qD is not usable as a %<constexpr%> function because:", fun
);
1085 /* First check the declaration. */
1086 if (is_valid_constexpr_fn (fun
, true))
1088 /* Then if it's OK, the body. */
1089 if (!DECL_DECLARED_CONSTEXPR_P (fun
)
1090 && DECL_DEFAULTED_FN (fun
))
1091 explain_implicit_non_constexpr (fun
);
1094 if (constexpr_fundef
*fd
= retrieve_constexpr_fundef (fun
))
1097 body
= DECL_SAVED_TREE (fun
);
1098 body
= massage_constexpr_body (fun
, body
);
1099 require_potential_rvalue_constant_expression (body
);
1100 if (DECL_CONSTRUCTOR_P (fun
))
1101 cx_check_missing_mem_inits (DECL_CONTEXT (fun
), body
, true);
1106 /* Objects of this type represent calls to constexpr functions
1107 along with the bindings of parameters to their arguments, for
1108 the purpose of compile time evaluation. */
1110 struct GTY((for_user
)) constexpr_call
{
1111 /* Description of the constexpr function definition. */
1112 constexpr_fundef
*fundef
;
1113 /* Parameter bindings environment. A TREE_VEC of arguments. */
1115 /* Result of the call.
1116 NULL means the call is being evaluated.
1117 error_mark_node means that the evaluation was erroneous;
1118 otherwise, the actuall value of the call. */
1120 /* The hash of this call; we remember it here to avoid having to
1121 recalculate it when expanding the hash table. */
1123 /* The value of constexpr_ctx::manifestly_const_eval. */
1124 enum mce_value manifestly_const_eval
;
1127 struct constexpr_call_hasher
: ggc_ptr_hash
<constexpr_call
>
1129 static hashval_t
hash (constexpr_call
*);
1130 static bool equal (constexpr_call
*, constexpr_call
*);
1133 enum constexpr_switch_state
{
1134 /* Used when processing a switch for the first time by cxx_eval_switch_expr
1135 and default: label for that switch has not been seen yet. */
1136 css_default_not_seen
,
1137 /* Used when processing a switch for the first time by cxx_eval_switch_expr
1138 and default: label for that switch has been seen already. */
1140 /* Used when processing a switch for the second time by
1141 cxx_eval_switch_expr, where default: label should match. */
1142 css_default_processing
1145 /* The constexpr expansion context part which needs one instance per
1146 cxx_eval_outermost_constant_expr invocation. VALUES is a map of values of
1147 variables initialized within the expression. */
1149 class constexpr_global_ctx
{
1150 /* Values for any temporaries or local variables within the
1151 constant-expression. */
1152 hash_map
<tree
,tree
> values
;
1154 /* Number of cxx_eval_constant_expression calls (except skipped ones,
1155 on simple constants or location wrappers) encountered during current
1156 cxx_eval_outermost_constant_expr call. */
1157 HOST_WIDE_INT constexpr_ops_count
;
1158 /* Heap VAR_DECLs created during the evaluation of the outermost constant
1160 auto_vec
<tree
, 16> heap_vars
;
1161 /* Cleanups that need to be evaluated at the end of CLEANUP_POINT_EXPR. */
1162 vec
<tree
> *cleanups
;
1163 /* If non-null, only allow modification of existing values of the variables
1164 in this set. Set by modifiable_tracker, below. */
1165 hash_set
<tree
> *modifiable
;
1166 /* Number of heap VAR_DECL deallocations. */
1167 unsigned heap_dealloc_count
;
1169 constexpr_global_ctx ()
1170 : constexpr_ops_count (0), cleanups (NULL
), modifiable (nullptr),
1171 heap_dealloc_count (0) {}
1173 tree
get_value (tree t
)
1175 if (tree
*p
= values
.get (t
))
1179 tree
*get_value_ptr (tree t
)
1181 if (modifiable
&& !modifiable
->contains (t
))
1183 return values
.get (t
);
1185 void put_value (tree t
, tree v
)
1187 bool already_in_map
= values
.put (t
, v
);
1188 if (!already_in_map
&& modifiable
)
1189 modifiable
->add (t
);
1191 void remove_value (tree t
) { values
.remove (t
); }
1194 /* Helper class for constexpr_global_ctx. In some cases we want to avoid
1195 side-effects from evaluation of a particular subexpression of a
1196 constant-expression. In such cases we use modifiable_tracker to prevent
1197 modification of variables created outside of that subexpression.
1199 ??? We could change the hash_set to a hash_map, allow and track external
1200 modifications, and roll them back in the destructor. It's not clear to me
1201 that this would be worthwhile. */
1203 class modifiable_tracker
1206 constexpr_global_ctx
*global
;
1208 modifiable_tracker (constexpr_global_ctx
*g
): global(g
)
1210 global
->modifiable
= &set
;
1212 ~modifiable_tracker ()
1215 global
->remove_value (t
);
1216 global
->modifiable
= nullptr;
1220 /* The constexpr expansion context. CALL is the current function
1221 expansion, CTOR is the current aggregate initializer, OBJECT is the
1222 object being initialized by CTOR, either a VAR_DECL or a _REF. */
1224 struct constexpr_ctx
{
1225 /* The part of the context that needs to be unique to the whole
1226 cxx_eval_outermost_constant_expr invocation. */
1227 constexpr_global_ctx
*global
;
1228 /* The innermost call we're evaluating. */
1229 constexpr_call
*call
;
1230 /* SAVE_EXPRs and TARGET_EXPR_SLOT vars of TARGET_EXPRs that we've seen
1231 within the current LOOP_EXPR. NULL if we aren't inside a loop. */
1232 vec
<tree
> *save_exprs
;
1233 /* The CONSTRUCTOR we're currently building up for an aggregate
1236 /* The object we're building the CONSTRUCTOR for. */
1238 /* If inside SWITCH_EXPR. */
1239 constexpr_switch_state
*css_state
;
1240 /* The aggregate initialization context inside which this one is nested. This
1241 is used by lookup_placeholder to resolve PLACEHOLDER_EXPRs. */
1242 const constexpr_ctx
*parent
;
1244 /* Whether we should error on a non-constant expression or fail quietly.
1245 This flag needs to be here, but some of the others could move to global
1246 if they get larger than a word. */
1248 /* Whether we are strictly conforming to constant expression rules or
1249 trying harder to get a constant value. */
1251 /* Whether __builtin_is_constant_evaluated () should be true. */
1252 mce_value manifestly_const_eval
;
1255 /* This internal flag controls whether we should avoid doing anything during
1256 constexpr evaluation that would cause extra DECL_UID generation, such as
1257 template instantiation and function body copying. */
1259 static bool uid_sensitive_constexpr_evaluation_value
;
1261 /* An internal counter that keeps track of the number of times
1262 uid_sensitive_constexpr_evaluation_p returned true. */
1264 static unsigned uid_sensitive_constexpr_evaluation_true_counter
;
1266 /* The accessor for uid_sensitive_constexpr_evaluation_value which also
1267 increments the corresponding counter. */
1270 uid_sensitive_constexpr_evaluation_p ()
1272 if (uid_sensitive_constexpr_evaluation_value
)
1274 ++uid_sensitive_constexpr_evaluation_true_counter
;
1281 /* The default constructor for uid_sensitive_constexpr_evaluation_sentinel
1282 enables the internal flag for uid_sensitive_constexpr_evaluation_p
1283 during the lifetime of the sentinel object. Upon its destruction, the
1284 previous value of uid_sensitive_constexpr_evaluation_p is restored. */
1286 uid_sensitive_constexpr_evaluation_sentinel
1287 ::uid_sensitive_constexpr_evaluation_sentinel ()
1288 : ovr (uid_sensitive_constexpr_evaluation_value
, true)
1292 /* The default constructor for uid_sensitive_constexpr_evaluation_checker
1293 records the current number of times that uid_sensitive_constexpr_evaluation_p
1294 has been called and returned true. */
1296 uid_sensitive_constexpr_evaluation_checker
1297 ::uid_sensitive_constexpr_evaluation_checker ()
1298 : saved_counter (uid_sensitive_constexpr_evaluation_true_counter
)
1302 /* Returns true iff uid_sensitive_constexpr_evaluation_p is true, and
1303 some constexpr evaluation was restricted due to u_s_c_e_p being called
1304 and returning true during the lifetime of this checker object. */
1307 uid_sensitive_constexpr_evaluation_checker::evaluation_restricted_p () const
1309 return (uid_sensitive_constexpr_evaluation_value
1310 && saved_counter
!= uid_sensitive_constexpr_evaluation_true_counter
);
1314 /* A table of all constexpr calls that have been evaluated by the
1315 compiler in this translation unit. */
1317 static GTY (()) hash_table
<constexpr_call_hasher
> *constexpr_call_table
;
1319 /* Compute a hash value for a constexpr call representation. */
1322 constexpr_call_hasher::hash (constexpr_call
*info
)
1327 /* Return true if the objects pointed to by P and Q represent calls
1328 to the same constexpr function with the same arguments.
1329 Otherwise, return false. */
1332 constexpr_call_hasher::equal (constexpr_call
*lhs
, constexpr_call
*rhs
)
1336 if (lhs
->hash
!= rhs
->hash
)
1338 if (lhs
->manifestly_const_eval
!= rhs
->manifestly_const_eval
)
1340 if (!constexpr_fundef_hasher::equal (lhs
->fundef
, rhs
->fundef
))
1342 return cp_tree_equal (lhs
->bindings
, rhs
->bindings
);
1345 /* Initialize the constexpr call table, if needed. */
1348 maybe_initialize_constexpr_call_table (void)
1350 if (constexpr_call_table
== NULL
)
1351 constexpr_call_table
= hash_table
<constexpr_call_hasher
>::create_ggc (101);
1354 /* During constexpr CALL_EXPR evaluation, to avoid issues with sharing when
1355 a function happens to get called recursively, we unshare the callee
1356 function's body and evaluate this unshared copy instead of evaluating the
1359 FUNDEF_COPIES_TABLE is a per-function freelist of these unshared function
1360 copies. The underlying data structure of FUNDEF_COPIES_TABLE is a hash_map
1361 that's keyed off of the original FUNCTION_DECL and whose value is a
1362 TREE_LIST of this function's unused copies awaiting reuse.
1364 This is not GC-deletable to avoid GC affecting UID generation. */
1366 static GTY(()) decl_tree_map
*fundef_copies_table
;
1368 /* Reuse a copy or create a new unshared copy of the function FUN.
1369 Return this copy. We use a TREE_LIST whose PURPOSE is body, VALUE
1370 is parms, TYPE is result. */
1373 get_fundef_copy (constexpr_fundef
*fundef
)
1377 tree
*slot
= &(hash_map_safe_get_or_insert
<hm_ggc
>
1378 (fundef_copies_table
, fundef
->decl
, &existed
, 127));
1382 /* There is no cached function available, or in use. We can use
1383 the function directly. That the slot is now created records
1384 that this function is now in use. */
1385 copy
= build_tree_list (fundef
->body
, fundef
->parms
);
1386 TREE_TYPE (copy
) = fundef
->result
;
1388 else if (*slot
== NULL_TREE
)
1390 if (uid_sensitive_constexpr_evaluation_p ())
1393 /* We've already used the function itself, so make a copy. */
1394 copy
= build_tree_list (NULL
, NULL
);
1395 tree saved_body
= DECL_SAVED_TREE (fundef
->decl
);
1396 tree saved_parms
= DECL_ARGUMENTS (fundef
->decl
);
1397 tree saved_result
= DECL_RESULT (fundef
->decl
);
1398 tree saved_fn
= current_function_decl
;
1399 DECL_SAVED_TREE (fundef
->decl
) = fundef
->body
;
1400 DECL_ARGUMENTS (fundef
->decl
) = fundef
->parms
;
1401 DECL_RESULT (fundef
->decl
) = fundef
->result
;
1402 current_function_decl
= fundef
->decl
;
1403 TREE_PURPOSE (copy
) = copy_fn (fundef
->decl
, TREE_VALUE (copy
),
1405 current_function_decl
= saved_fn
;
1406 DECL_RESULT (fundef
->decl
) = saved_result
;
1407 DECL_ARGUMENTS (fundef
->decl
) = saved_parms
;
1408 DECL_SAVED_TREE (fundef
->decl
) = saved_body
;
1412 /* We have a cached function available. */
1414 *slot
= TREE_CHAIN (copy
);
1420 /* Save the copy COPY of function FUN for later reuse by
1421 get_fundef_copy(). By construction, there will always be an entry
1425 save_fundef_copy (tree fun
, tree copy
)
1427 tree
*slot
= fundef_copies_table
->get (fun
);
1428 TREE_CHAIN (copy
) = *slot
;
1432 /* Whether our evaluation wants a prvalue (e.g. CONSTRUCTOR or _CST),
1433 a glvalue (e.g. VAR_DECL or _REF), or nothing. */
1441 static tree
cxx_eval_constant_expression (const constexpr_ctx
*, tree
,
1442 value_cat
, bool *, bool *, tree
* = NULL
);
1443 static tree
cxx_eval_bare_aggregate (const constexpr_ctx
*, tree
,
1444 value_cat
, bool *, bool *);
1445 static tree
cxx_fold_indirect_ref (const constexpr_ctx
*, location_t
, tree
, tree
,
1447 static tree
find_heap_var_refs (tree
*, int *, void *);
1449 /* Attempt to evaluate T which represents a call to a builtin function.
1450 We assume here that all builtin functions evaluate to scalar types
1451 represented by _CST nodes. */
1454 cxx_eval_builtin_function_call (const constexpr_ctx
*ctx
, tree t
, tree fun
,
1456 bool *non_constant_p
, bool *overflow_p
)
1458 const int nargs
= call_expr_nargs (t
);
1459 tree
*args
= (tree
*) alloca (nargs
* sizeof (tree
));
1463 /* Don't fold __builtin_constant_p within a constexpr function. */
1464 bool bi_const_p
= DECL_IS_BUILTIN_CONSTANT_P (fun
);
1466 /* If we aren't requiring a constant expression, defer __builtin_constant_p
1467 in a constexpr function until we have values for the parameters. */
1469 && ctx
->manifestly_const_eval
!= mce_true
1470 && current_function_decl
1471 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
1473 *non_constant_p
= true;
1477 /* For __builtin_is_constant_evaluated, defer it if not
1478 ctx->manifestly_const_eval (as sometimes we try to constant evaluate
1479 without manifestly_const_eval even expressions or parts thereof which
1480 will later be manifestly const_eval evaluated), otherwise fold it to
1482 if (fndecl_built_in_p (fun
, CP_BUILT_IN_IS_CONSTANT_EVALUATED
,
1485 if (ctx
->manifestly_const_eval
== mce_unknown
)
1487 *non_constant_p
= true;
1490 return constant_boolean_node (ctx
->manifestly_const_eval
== mce_true
,
1494 if (fndecl_built_in_p (fun
, CP_BUILT_IN_SOURCE_LOCATION
, BUILT_IN_FRONTEND
))
1496 temp_override
<tree
> ovr (current_function_decl
);
1497 if (ctx
->call
&& ctx
->call
->fundef
)
1498 current_function_decl
= ctx
->call
->fundef
->decl
;
1499 return fold_builtin_source_location (t
);
1504 if (fndecl_built_in_p (fun
, BUILT_IN_NORMAL
))
1505 switch (DECL_FUNCTION_CODE (fun
))
1507 case BUILT_IN_STRLEN
:
1508 case BUILT_IN_STRNLEN
:
1511 case BUILT_IN_MEMCHR
:
1512 case BUILT_IN_STRCHR
:
1513 case BUILT_IN_STRRCHR
:
1517 case BUILT_IN_MEMCMP
:
1518 case BUILT_IN_STRCMP
:
1521 case BUILT_IN_STRSTR
:
1525 case BUILT_IN_ASAN_POINTER_COMPARE
:
1526 case BUILT_IN_ASAN_POINTER_SUBTRACT
:
1527 /* These builtins shall be ignored during constant expression
1530 case BUILT_IN_UNREACHABLE
:
1532 if (!*non_constant_p
&& !ctx
->quiet
)
1534 /* Do not allow__builtin_unreachable in constexpr function.
1535 The __builtin_unreachable call with BUILTINS_LOCATION
1536 comes from cp_maybe_instrument_return. */
1537 if (EXPR_LOCATION (t
) == BUILTINS_LOCATION
)
1538 error ("%<constexpr%> call flows off the end of the function");
1540 error ("%q+E is not a constant expression", t
);
1542 *non_constant_p
= true;
1548 /* Be permissive for arguments to built-ins; __builtin_constant_p should
1549 return constant false for a non-constant argument. */
1550 constexpr_ctx new_ctx
= *ctx
;
1551 new_ctx
.quiet
= true;
1552 for (i
= 0; i
< nargs
; ++i
)
1554 tree arg
= CALL_EXPR_ARG (t
, i
);
1557 /* To handle string built-ins we need to pass ADDR_EXPR<STRING_CST> since
1558 expand_builtin doesn't know how to look in the values table. */
1559 bool strop
= i
< strops
;
1563 if (TREE_CODE (arg
) == ADDR_EXPR
)
1564 arg
= TREE_OPERAND (arg
, 0);
1569 /* If builtin_valid_in_constant_expr_p is true,
1570 potential_constant_expression_1 has not recursed into the arguments
1571 of the builtin, verify it here. */
1572 if (!builtin_valid_in_constant_expr_p (fun
)
1573 || potential_constant_expression (arg
))
1575 bool dummy1
= false, dummy2
= false;
1576 arg
= cxx_eval_constant_expression (&new_ctx
, arg
, vc_prvalue
,
1581 /* For __builtin_constant_p, fold all expressions with constant values
1582 even if they aren't C++ constant-expressions. */
1583 arg
= cp_fold_rvalue (arg
);
1586 if (TREE_CODE (arg
) == CONSTRUCTOR
)
1587 arg
= braced_lists_to_strings (TREE_TYPE (arg
), arg
);
1588 if (TREE_CODE (arg
) == STRING_CST
)
1589 arg
= build_address (arg
);
1597 bool save_ffbcp
= force_folding_builtin_constant_p
;
1598 force_folding_builtin_constant_p
|= ctx
->manifestly_const_eval
== mce_true
;
1599 tree save_cur_fn
= current_function_decl
;
1600 /* Return name of ctx->call->fundef->decl for __builtin_FUNCTION (). */
1601 if (fndecl_built_in_p (fun
, BUILT_IN_FUNCTION
)
1603 && ctx
->call
->fundef
)
1604 current_function_decl
= ctx
->call
->fundef
->decl
;
1605 if (fndecl_built_in_p (fun
,
1606 CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS
,
1609 location_t loc
= EXPR_LOCATION (t
);
1611 VERIFY_CONSTANT (args
[0]);
1613 = fold_builtin_is_pointer_inverconvertible_with_class (loc
, nargs
,
1616 else if (fndecl_built_in_p (fun
,
1617 CP_BUILT_IN_IS_CORRESPONDING_MEMBER
,
1620 location_t loc
= EXPR_LOCATION (t
);
1623 VERIFY_CONSTANT (args
[0]);
1624 VERIFY_CONSTANT (args
[1]);
1626 new_call
= fold_builtin_is_corresponding_member (loc
, nargs
, args
);
1629 new_call
= fold_builtin_call_array (EXPR_LOCATION (t
), TREE_TYPE (t
),
1630 CALL_EXPR_FN (t
), nargs
, args
);
1631 current_function_decl
= save_cur_fn
;
1632 force_folding_builtin_constant_p
= save_ffbcp
;
1633 if (new_call
== NULL
)
1635 if (!*non_constant_p
&& !ctx
->quiet
)
1637 new_call
= build_call_array_loc (EXPR_LOCATION (t
), TREE_TYPE (t
),
1638 CALL_EXPR_FN (t
), nargs
, args
);
1639 error ("%q+E is not a constant expression", new_call
);
1641 *non_constant_p
= true;
1645 if (!potential_constant_expression (new_call
))
1647 if (!*non_constant_p
&& !ctx
->quiet
)
1648 error ("%q+E is not a constant expression", new_call
);
1649 *non_constant_p
= true;
1655 /* memchr returns a pointer into the first argument, but we replaced the
1656 argument above with a STRING_CST; put it back it now. */
1657 tree op
= CALL_EXPR_ARG (t
, strret
-1);
1658 STRIP_NOPS (new_call
);
1659 if (TREE_CODE (new_call
) == POINTER_PLUS_EXPR
)
1660 TREE_OPERAND (new_call
, 0) = op
;
1661 else if (TREE_CODE (new_call
) == ADDR_EXPR
)
1665 return cxx_eval_constant_expression (&new_ctx
, new_call
, lval
,
1666 non_constant_p
, overflow_p
);
1669 /* TEMP is the constant value of a temporary object of type TYPE. Adjust
1670 the type of the value to match. */
1673 adjust_temp_type (tree type
, tree temp
)
1675 if (same_type_p (TREE_TYPE (temp
), type
))
1677 /* Avoid wrapping an aggregate value in a NOP_EXPR. */
1678 if (TREE_CODE (temp
) == CONSTRUCTOR
)
1680 /* build_constructor wouldn't retain various CONSTRUCTOR flags. */
1681 tree t
= copy_node (temp
);
1682 TREE_TYPE (t
) = type
;
1685 if (TREE_CODE (temp
) == EMPTY_CLASS_EXPR
)
1686 return build0 (EMPTY_CLASS_EXPR
, type
);
1687 gcc_assert (scalarish_type_p (type
));
1688 /* Now we know we're dealing with a scalar, and a prvalue of non-class
1689 type is cv-unqualified. */
1690 return cp_fold_convert (cv_unqualified (type
), temp
);
1693 /* If T is a CONSTRUCTOR, return an unshared copy of T and any
1694 sub-CONSTRUCTORs. Otherwise return T.
1696 We use this whenever we initialize an object as a whole, whether it's a
1697 parameter, a local variable, or a subobject, so that subsequent
1698 modifications don't affect other places where it was used. */
1701 unshare_constructor (tree t MEM_STAT_DECL
)
1703 if (!t
|| TREE_CODE (t
) != CONSTRUCTOR
)
1705 auto_vec
<tree
*, 4> ptrs
;
1706 ptrs
.safe_push (&t
);
1707 while (!ptrs
.is_empty ())
1709 tree
*p
= ptrs
.pop ();
1710 tree n
= copy_node (*p PASS_MEM_STAT
);
1711 CONSTRUCTOR_ELTS (n
) = vec_safe_copy (CONSTRUCTOR_ELTS (*p
) PASS_MEM_STAT
);
1713 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (n
);
1714 constructor_elt
*ce
;
1715 for (HOST_WIDE_INT i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
1716 if (ce
->value
&& TREE_CODE (ce
->value
) == CONSTRUCTOR
)
1717 ptrs
.safe_push (&ce
->value
);
1722 /* If T is a CONSTRUCTOR, ggc_free T and any sub-CONSTRUCTORs. */
1725 free_constructor (tree t
)
1727 if (!t
|| TREE_CODE (t
) != CONSTRUCTOR
)
1729 releasing_vec ctors
;
1730 vec_safe_push (ctors
, t
);
1731 while (!ctors
->is_empty ())
1733 tree c
= ctors
->pop ();
1734 if (vec
<constructor_elt
, va_gc
> *elts
= CONSTRUCTOR_ELTS (c
))
1736 constructor_elt
*ce
;
1737 for (HOST_WIDE_INT i
= 0; vec_safe_iterate (elts
, i
, &ce
); ++i
)
1738 if (TREE_CODE (ce
->value
) == CONSTRUCTOR
)
1739 vec_safe_push (ctors
, ce
->value
);
1746 /* Helper function of cxx_bind_parameters_in_call. Return non-NULL
1747 if *TP is address of a static variable (or part of it) currently being
1748 constructed or of a heap artificial variable. */
1751 addr_of_non_const_var (tree
*tp
, int *walk_subtrees
, void *data
)
1753 if (TREE_CODE (*tp
) == ADDR_EXPR
)
1754 if (tree var
= get_base_address (TREE_OPERAND (*tp
, 0)))
1755 if (VAR_P (var
) && TREE_STATIC (var
))
1757 if (DECL_NAME (var
) == heap_uninit_identifier
1758 || DECL_NAME (var
) == heap_identifier
1759 || DECL_NAME (var
) == heap_vec_uninit_identifier
1760 || DECL_NAME (var
) == heap_vec_identifier
)
1763 constexpr_global_ctx
*global
= (constexpr_global_ctx
*) data
;
1764 if (global
->get_value (var
))
1768 *walk_subtrees
= false;
1772 /* Subroutine of cxx_eval_call_expression.
1773 We are processing a call expression (either CALL_EXPR or
1774 AGGR_INIT_EXPR) in the context of CTX. Evaluate
1775 all arguments and bind their values to correspondings
1776 parameters, making up the NEW_CALL context. */
1779 cxx_bind_parameters_in_call (const constexpr_ctx
*ctx
, tree t
, tree fun
,
1780 bool *non_constant_p
, bool *overflow_p
,
1781 bool *non_constant_args
)
1783 const int nargs
= call_expr_nargs (t
);
1784 tree parms
= DECL_ARGUMENTS (fun
);
1786 /* We don't record ellipsis args below. */
1787 int nparms
= list_length (parms
);
1788 int nbinds
= nargs
< nparms
? nargs
: nparms
;
1789 tree binds
= make_tree_vec (nbinds
);
1790 for (i
= 0; i
< nargs
; ++i
)
1793 tree type
= parms
? TREE_TYPE (parms
) : void_type_node
;
1794 if (parms
&& DECL_BY_REFERENCE (parms
))
1795 type
= TREE_TYPE (type
);
1796 x
= get_nth_callarg (t
, i
);
1797 /* For member function, the first argument is a pointer to the implied
1798 object. For a constructor, it might still be a dummy object, in
1799 which case we get the real argument from ctx. */
1800 if (i
== 0 && DECL_CONSTRUCTOR_P (fun
)
1801 && is_dummy_object (x
))
1804 x
= build_address (x
);
1806 if (TREE_ADDRESSABLE (type
))
1807 /* Undo convert_for_arg_passing work here. */
1808 x
= convert_from_reference (x
);
1809 /* Normally we would strip a TARGET_EXPR in an initialization context
1810 such as this, but here we do the elision differently: we keep the
1811 TARGET_EXPR, and use its CONSTRUCTOR as the value of the parm. */
1812 arg
= cxx_eval_constant_expression (ctx
, x
, vc_prvalue
,
1813 non_constant_p
, overflow_p
);
1814 /* Don't VERIFY_CONSTANT here. */
1815 if (*non_constant_p
&& ctx
->quiet
)
1817 /* Just discard ellipsis args after checking their constantitude. */
1821 if (!*non_constant_p
)
1823 /* Make sure the binding has the same type as the parm. But
1824 only for constant args. */
1825 if (!TYPE_REF_P (type
))
1826 arg
= adjust_temp_type (type
, arg
);
1827 if (!TREE_CONSTANT (arg
))
1828 *non_constant_args
= true;
1829 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
1830 /* The destructor needs to see any modifications the callee makes
1832 *non_constant_args
= true;
1833 /* If arg is or contains address of a heap artificial variable or
1834 of a static variable being constructed, avoid caching the
1835 function call, as those variables might be modified by the
1836 function, or might be modified by the callers in between
1837 the cached function and just read by the function. */
1838 else if (!*non_constant_args
1839 && cp_walk_tree (&arg
, addr_of_non_const_var
, ctx
->global
,
1841 *non_constant_args
= true;
1843 /* For virtual calls, adjust the this argument, so that it is
1844 the object on which the method is called, rather than
1845 one of its bases. */
1846 if (i
== 0 && DECL_VIRTUAL_P (fun
))
1850 if (TREE_CODE (addr
) == ADDR_EXPR
)
1852 tree obj
= TREE_OPERAND (addr
, 0);
1853 while (TREE_CODE (obj
) == COMPONENT_REF
1854 && DECL_FIELD_IS_BASE (TREE_OPERAND (obj
, 1))
1855 && !same_type_ignoring_top_level_qualifiers_p
1856 (TREE_TYPE (obj
), DECL_CONTEXT (fun
)))
1857 obj
= TREE_OPERAND (obj
, 0);
1858 if (obj
!= TREE_OPERAND (addr
, 0))
1859 arg
= build_fold_addr_expr_with_type (obj
,
1863 TREE_VEC_ELT (binds
, i
) = arg
;
1865 parms
= TREE_CHAIN (parms
);
1871 /* Variables and functions to manage constexpr call expansion context.
1872 These do not need to be marked for PCH or GC. */
1874 /* FIXME remember and print actual constant arguments. */
1875 static vec
<tree
> call_stack
;
1876 static int call_stack_tick
;
1877 static int last_cx_error_tick
;
1880 push_cx_call_context (tree call
)
1883 if (!EXPR_HAS_LOCATION (call
))
1884 SET_EXPR_LOCATION (call
, input_location
);
1885 call_stack
.safe_push (call
);
1886 int len
= call_stack
.length ();
1887 if (len
> max_constexpr_depth
)
1893 pop_cx_call_context (void)
1900 cx_error_context (void)
1902 vec
<tree
> r
= vNULL
;
1903 if (call_stack_tick
!= last_cx_error_tick
1904 && !call_stack
.is_empty ())
1906 last_cx_error_tick
= call_stack_tick
;
1910 /* E is an operand of a failed assertion, fold it either with or without
1911 constexpr context. */
1914 fold_operand (tree e
, const constexpr_ctx
*ctx
)
1918 bool new_non_constant_p
= false, new_overflow_p
= false;
1919 e
= cxx_eval_constant_expression (ctx
, e
, vc_prvalue
,
1920 &new_non_constant_p
,
1924 e
= fold_non_dependent_expr (e
, tf_none
, /*manifestly_const_eval=*/true);
1928 /* If we have a condition in conjunctive normal form (CNF), find the first
1929 failing clause. In other words, given an expression like
1931 true && true && false && true && false
1933 return the first 'false'. EXPR is the expression. */
1936 find_failing_clause_r (const constexpr_ctx
*ctx
, tree expr
)
1938 if (TREE_CODE (expr
) == TRUTH_ANDIF_EXPR
)
1940 /* First check the left side... */
1941 tree e
= find_failing_clause_r (ctx
, TREE_OPERAND (expr
, 0));
1943 /* ...if we didn't find a false clause, check the right side. */
1944 e
= find_failing_clause_r (ctx
, TREE_OPERAND (expr
, 1));
1947 tree e
= contextual_conv_bool (expr
, tf_none
);
1948 e
= fold_operand (e
, ctx
);
1949 if (integer_zerop (e
))
1950 /* This is the failing clause. */
1955 /* Wrapper for find_failing_clause_r. */
1958 find_failing_clause (const constexpr_ctx
*ctx
, tree expr
)
1960 if (TREE_CODE (expr
) == TRUTH_ANDIF_EXPR
)
1961 if (tree e
= find_failing_clause_r (ctx
, expr
))
1966 /* Emit additional diagnostics for failing condition BAD.
1967 Used by finish_static_assert and IFN_ASSUME constexpr diagnostics.
1968 If SHOW_EXPR_P is true, print the condition (because it was
1969 instantiation-dependent). */
1972 diagnose_failing_condition (tree bad
, location_t cloc
, bool show_expr_p
,
1973 const constexpr_ctx
*ctx
/* = nullptr */)
1975 /* Nobody wants to see the artificial (bool) cast. */
1976 bad
= tree_strip_nop_conversions (bad
);
1977 if (TREE_CODE (bad
) == CLEANUP_POINT_EXPR
)
1978 bad
= TREE_OPERAND (bad
, 0);
1980 /* Actually explain the failure if this is a concept check or a
1981 requires-expression. */
1982 if (concept_check_p (bad
) || TREE_CODE (bad
) == REQUIRES_EXPR
)
1983 diagnose_constraints (cloc
, bad
, NULL_TREE
);
1984 else if (COMPARISON_CLASS_P (bad
)
1985 && ARITHMETIC_TYPE_P (TREE_TYPE (TREE_OPERAND (bad
, 0))))
1987 tree op0
= fold_operand (TREE_OPERAND (bad
, 0), ctx
);
1988 tree op1
= fold_operand (TREE_OPERAND (bad
, 1), ctx
);
1989 tree cond
= build2 (TREE_CODE (bad
), boolean_type_node
, op0
, op1
);
1990 inform (cloc
, "the comparison reduces to %qE", cond
);
1992 else if (show_expr_p
)
1993 inform (cloc
, "%qE evaluates to false", bad
);
1996 /* Process an assert/assume of ORIG_ARG. If it's not supposed to be evaluated,
1997 do it without changing the current evaluation state. If it evaluates to
1998 false, complain and return false; otherwise, return true. */
2001 cxx_eval_assert (const constexpr_ctx
*ctx
, tree arg
, const char *msg
,
2002 location_t loc
, bool evaluated
,
2003 bool *non_constant_p
, bool *overflow_p
)
2005 if (*non_constant_p
)
2011 if (!potential_rvalue_constant_expression (arg
))
2014 constexpr_ctx new_ctx
= *ctx
;
2015 new_ctx
.quiet
= true;
2016 bool new_non_constant_p
= false, new_overflow_p
= false;
2017 /* Avoid modification of existing values. */
2018 modifiable_tracker
ms (new_ctx
.global
);
2019 eval
= cxx_eval_constant_expression (&new_ctx
, arg
, vc_prvalue
,
2020 &new_non_constant_p
,
2024 eval
= cxx_eval_constant_expression (ctx
, arg
, vc_prvalue
,
2027 if (!*non_constant_p
&& integer_zerop (eval
))
2031 /* See if we can find which clause was failing
2032 (for logical AND). */
2033 tree bad
= find_failing_clause (ctx
, arg
);
2034 /* If not, or its location is unusable, fall back to the
2035 previous location. */
2036 location_t cloc
= cp_expr_loc_or_loc (bad
, loc
);
2038 /* Report the error. */
2039 auto_diagnostic_group d
;
2040 error_at (cloc
, msg
);
2041 diagnose_failing_condition (bad
, cloc
, true, ctx
);
2044 *non_constant_p
= true;
2051 /* Evaluate a call T to a GCC internal function when possible and return
2052 the evaluated result or, under the control of CTX, give an error, set
2053 NON_CONSTANT_P, and return the unevaluated call T otherwise. */
2056 cxx_eval_internal_function (const constexpr_ctx
*ctx
, tree t
,
2058 bool *non_constant_p
, bool *overflow_p
)
2060 enum tree_code opcode
= ERROR_MARK
;
2062 switch (CALL_EXPR_IFN (t
))
2064 case IFN_UBSAN_NULL
:
2065 case IFN_UBSAN_BOUNDS
:
2066 case IFN_UBSAN_VPTR
:
2067 case IFN_FALLTHROUGH
:
2071 if (!cxx_eval_assert (ctx
, CALL_EXPR_ARG (t
, 0),
2072 G_("failed %<assume%> attribute assumption"),
2073 EXPR_LOCATION (t
), /*eval*/false,
2074 non_constant_p
, overflow_p
))
2078 case IFN_ADD_OVERFLOW
:
2081 case IFN_SUB_OVERFLOW
:
2082 opcode
= MINUS_EXPR
;
2084 case IFN_MUL_OVERFLOW
:
2089 return cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 0),
2090 vc_prvalue
, non_constant_p
,
2093 case IFN_VEC_CONVERT
:
2095 tree arg
= cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 0),
2096 vc_prvalue
, non_constant_p
,
2098 if (TREE_CODE (arg
) == VECTOR_CST
)
2099 if (tree r
= fold_const_call (CFN_VEC_CONVERT
, TREE_TYPE (t
), arg
))
2106 error_at (cp_expr_loc_or_input_loc (t
),
2107 "call to internal function %qE", t
);
2108 *non_constant_p
= true;
2112 /* Evaluate constant arguments using OPCODE and return a complex
2113 number containing the result and the overflow bit. */
2114 tree arg0
= cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 0), lval
,
2115 non_constant_p
, overflow_p
);
2116 tree arg1
= cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 1), lval
,
2117 non_constant_p
, overflow_p
);
2119 if (TREE_CODE (arg0
) == INTEGER_CST
&& TREE_CODE (arg1
) == INTEGER_CST
)
2121 location_t loc
= cp_expr_loc_or_input_loc (t
);
2122 tree type
= TREE_TYPE (TREE_TYPE (t
));
2123 tree result
= fold_binary_loc (loc
, opcode
, type
,
2124 fold_convert_loc (loc
, type
, arg0
),
2125 fold_convert_loc (loc
, type
, arg1
));
2127 = build_int_cst (type
, arith_overflowed_p (opcode
, type
, arg0
, arg1
));
2128 /* Reset TREE_OVERFLOW to avoid warnings for the overflow. */
2129 if (TREE_OVERFLOW (result
))
2130 TREE_OVERFLOW (result
) = 0;
2132 return build_complex (TREE_TYPE (t
), result
, ovf
);
2135 *non_constant_p
= true;
2139 /* Clean CONSTRUCTOR_NO_CLEARING from CTOR and its sub-aggregates. */
2142 clear_no_implicit_zero (tree ctor
)
2144 if (CONSTRUCTOR_NO_CLEARING (ctor
))
2146 CONSTRUCTOR_NO_CLEARING (ctor
) = false;
2147 for (auto &e
: CONSTRUCTOR_ELTS (ctor
))
2148 if (TREE_CODE (e
.value
) == CONSTRUCTOR
)
2149 clear_no_implicit_zero (e
.value
);
2153 /* Complain about a const object OBJ being modified in a constant expression.
2154 EXPR is the MODIFY_EXPR expression performing the modification. */
2157 modifying_const_object_error (tree expr
, tree obj
)
2159 location_t loc
= cp_expr_loc_or_input_loc (expr
);
2160 auto_diagnostic_group d
;
2161 error_at (loc
, "modifying a const object %qE is not allowed in "
2162 "a constant expression", TREE_OPERAND (expr
, 0));
2164 /* Find the underlying object that was declared as const. */
2165 location_t decl_loc
= UNKNOWN_LOCATION
;
2166 for (tree probe
= obj
; decl_loc
== UNKNOWN_LOCATION
; )
2167 switch (TREE_CODE (probe
))
2172 tree elt
= TREE_OPERAND (probe
, 1);
2173 if (CP_TYPE_CONST_P (TREE_TYPE (elt
)))
2174 decl_loc
= DECL_SOURCE_LOCATION (elt
);
2175 probe
= TREE_OPERAND (probe
, 0);
2182 probe
= TREE_OPERAND (probe
, 0);
2186 decl_loc
= location_of (probe
);
2189 inform (decl_loc
, "originally declared %<const%> here");
2192 /* Return true if FNDECL is a replaceable global allocation function that
2193 should be useable during constant expression evaluation. */
2196 cxx_replaceable_global_alloc_fn (tree fndecl
)
2198 return (cxx_dialect
>= cxx20
2199 && IDENTIFIER_NEWDEL_OP_P (DECL_NAME (fndecl
))
2200 && CP_DECL_CONTEXT (fndecl
) == global_namespace
2201 && (DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl
)
2202 || DECL_IS_OPERATOR_DELETE_P (fndecl
)));
2205 /* Return true if FNDECL is a placement new function that should be
2206 useable during constant expression evaluation of std::construct_at. */
2209 cxx_placement_new_fn (tree fndecl
)
2211 if (cxx_dialect
>= cxx20
2212 && IDENTIFIER_NEW_OP_P (DECL_NAME (fndecl
))
2213 && CP_DECL_CONTEXT (fndecl
) == global_namespace
2214 && !DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl
)
2215 && TREE_CODE (TREE_TYPE (fndecl
)) == FUNCTION_TYPE
)
2217 tree first_arg
= TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
2218 if (TREE_VALUE (first_arg
) == ptr_type_node
2219 && TREE_CHAIN (first_arg
) == void_list_node
)
2225 /* Return true if FNDECL is std::construct_at. */
2228 is_std_construct_at (tree fndecl
)
2230 if (!decl_in_std_namespace_p (fndecl
))
2233 tree name
= DECL_NAME (fndecl
);
2234 return name
&& id_equal (name
, "construct_at");
2237 /* Overload for the above taking constexpr_call*. */
2240 is_std_construct_at (const constexpr_call
*call
)
2244 && is_std_construct_at (call
->fundef
->decl
));
2247 /* True if CTX is an instance of std::allocator. */
2250 is_std_allocator (tree ctx
)
2252 if (ctx
== NULL_TREE
|| !CLASS_TYPE_P (ctx
) || !TYPE_MAIN_DECL (ctx
))
2255 tree decl
= TYPE_MAIN_DECL (ctx
);
2256 tree name
= DECL_NAME (decl
);
2257 if (name
== NULL_TREE
|| !id_equal (name
, "allocator"))
2260 return decl_in_std_namespace_p (decl
);
2263 /* Return true if FNDECL is std::allocator<T>::{,de}allocate. */
2266 is_std_allocator_allocate (tree fndecl
)
2268 tree name
= DECL_NAME (fndecl
);
2269 if (name
== NULL_TREE
2270 || !(id_equal (name
, "allocate") || id_equal (name
, "deallocate")))
2273 return is_std_allocator (DECL_CONTEXT (fndecl
));
2276 /* Overload for the above taking constexpr_call*. */
2279 is_std_allocator_allocate (const constexpr_call
*call
)
2283 && is_std_allocator_allocate (call
->fundef
->decl
));
2286 /* Return true if FNDECL is __dynamic_cast. */
2289 cxx_dynamic_cast_fn_p (tree fndecl
)
2291 return (cxx_dialect
>= cxx20
2292 && id_equal (DECL_NAME (fndecl
), "__dynamic_cast")
2293 && CP_DECL_CONTEXT (fndecl
) == abi_node
);
2296 /* Often, we have an expression in the form of address + offset, e.g.
2297 "&_ZTV1A + 16". Extract the object from it, i.e. "_ZTV1A". */
2300 extract_obj_from_addr_offset (tree expr
)
2302 if (TREE_CODE (expr
) == POINTER_PLUS_EXPR
)
2303 expr
= TREE_OPERAND (expr
, 0);
2305 if (TREE_CODE (expr
) == ADDR_EXPR
)
2306 expr
= TREE_OPERAND (expr
, 0);
2310 /* Given a PATH like
2312 g.D.2181.D.2154.D.2102.D.2093
2314 find a component with type TYPE. Return NULL_TREE if not found, and
2315 error_mark_node if the component is not accessible. If STOP is non-null,
2316 this function will return NULL_TREE if STOP is found before TYPE. */
2319 get_component_with_type (tree path
, tree type
, tree stop
)
2323 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path
), type
))
2327 && (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path
),
2330 else if (TREE_CODE (path
) == COMPONENT_REF
2331 && DECL_FIELD_IS_BASE (TREE_OPERAND (path
, 1)))
2333 /* We need to check that the component we're accessing is in fact
2335 if (TREE_PRIVATE (TREE_OPERAND (path
, 1))
2336 || TREE_PROTECTED (TREE_OPERAND (path
, 1)))
2337 return error_mark_node
;
2338 path
= TREE_OPERAND (path
, 0);
2345 /* Evaluate a call to __dynamic_cast (permitted by P1327R1).
2347 The declaration of __dynamic_cast is:
2349 void* __dynamic_cast (const void* __src_ptr,
2350 const __class_type_info* __src_type,
2351 const __class_type_info* __dst_type,
2352 ptrdiff_t __src2dst);
2354 where src2dst has the following possible values
2356 >-1: src_type is a unique public non-virtual base of dst_type
2357 dst_ptr + src2dst == src_ptr
2358 -1: unspecified relationship
2359 -2: src_type is not a public base of dst_type
2360 -3: src_type is a multiple public non-virtual base of dst_type
2362 Since literal types can't have virtual bases, we only expect hint >=0,
2366 cxx_eval_dynamic_cast_fn (const constexpr_ctx
*ctx
, tree call
,
2367 bool *non_constant_p
, bool *overflow_p
)
2369 /* T will be something like
2370 __dynamic_cast ((B*) b, &_ZTI1B, &_ZTI1D, 8)
2372 gcc_assert (call_expr_nargs (call
) == 4);
2373 tsubst_flags_t complain
= ctx
->quiet
? tf_none
: tf_warning_or_error
;
2374 tree obj
= CALL_EXPR_ARG (call
, 0);
2375 tree type
= CALL_EXPR_ARG (call
, 2);
2376 HOST_WIDE_INT hint
= int_cst_value (CALL_EXPR_ARG (call
, 3));
2377 location_t loc
= cp_expr_loc_or_input_loc (call
);
2379 /* Get the target type of the dynamic_cast. */
2380 gcc_assert (TREE_CODE (type
) == ADDR_EXPR
);
2381 type
= TREE_OPERAND (type
, 0);
2382 type
= TREE_TYPE (DECL_NAME (type
));
2384 /* TYPE can only be either T* or T&. We can't know which of these it
2385 is by looking at TYPE, but OBJ will be "(T*) x" in the first case,
2386 and something like "(T*)(T&)(T*) x" in the second case. */
2387 bool reference_p
= false;
2388 while (CONVERT_EXPR_P (obj
) || TREE_CODE (obj
) == SAVE_EXPR
)
2390 reference_p
|= TYPE_REF_P (TREE_TYPE (obj
));
2391 obj
= TREE_OPERAND (obj
, 0);
2394 /* Evaluate the object so that we know its dynamic type. */
2395 obj
= cxx_eval_constant_expression (ctx
, obj
, vc_prvalue
, non_constant_p
,
2397 if (*non_constant_p
)
2400 /* We expect OBJ to be in form of &d.D.2102 when HINT == 0,
2401 but when HINT is > 0, it can also be something like
2402 &d.D.2102 + 18446744073709551608, which includes the BINFO_OFFSET. */
2403 obj
= extract_obj_from_addr_offset (obj
);
2404 const tree objtype
= TREE_TYPE (obj
);
2405 /* If OBJ doesn't refer to a base field, we're done. */
2406 if (tree t
= (TREE_CODE (obj
) == COMPONENT_REF
2407 ? TREE_OPERAND (obj
, 1) : obj
))
2408 if (TREE_CODE (t
) != FIELD_DECL
|| !DECL_FIELD_IS_BASE (t
))
2414 error_at (loc
, "reference %<dynamic_cast%> failed");
2415 inform (loc
, "dynamic type %qT of its operand does "
2416 "not have a base class of type %qT",
2419 *non_constant_p
= true;
2421 return integer_zero_node
;
2424 /* [class.cdtor] When a dynamic_cast is used in a constructor ...
2425 or in a destructor ... if the operand of the dynamic_cast refers
2426 to the object under construction or destruction, this object is
2427 considered to be a most derived object that has the type of the
2428 constructor or destructor's class. */
2429 tree vtable
= build_vfield_ref (obj
, objtype
);
2430 vtable
= cxx_eval_constant_expression (ctx
, vtable
, vc_prvalue
,
2431 non_constant_p
, overflow_p
);
2432 if (*non_constant_p
)
2434 /* With -fsanitize=vptr, we initialize all vtable pointers to null,
2435 so it's possible that we got a null pointer now. */
2436 if (integer_zerop (vtable
))
2439 error_at (loc
, "virtual table pointer is used uninitialized");
2440 *non_constant_p
= true;
2441 return integer_zero_node
;
2443 /* VTABLE will be &_ZTV1A + 16 or similar, get _ZTV1A. */
2444 vtable
= extract_obj_from_addr_offset (vtable
);
2445 const tree mdtype
= DECL_CONTEXT (vtable
);
2447 /* Given dynamic_cast<T>(v),
2449 [expr.dynamic.cast] If C is the class type to which T points or refers,
2450 the runtime check logically executes as follows:
2452 If, in the most derived object pointed (referred) to by v, v points
2453 (refers) to a public base class subobject of a C object, and if only
2454 one object of type C is derived from the subobject pointed (referred)
2455 to by v the result points (refers) to that C object.
2457 In this case, HINT >= 0 or -3. */
2458 if (hint
>= 0 || hint
== -3)
2460 /* Look for a component with type TYPE. */
2461 tree t
= get_component_with_type (obj
, type
, mdtype
);
2462 /* If not accessible, give an error. */
2463 if (t
== error_mark_node
)
2469 error_at (loc
, "reference %<dynamic_cast%> failed");
2470 inform (loc
, "static type %qT of its operand is a "
2471 "non-public base class of dynamic type %qT",
2475 *non_constant_p
= true;
2477 return integer_zero_node
;
2480 /* The result points to the TYPE object. */
2481 return cp_build_addr_expr (t
, complain
);
2482 /* Else, TYPE was not found, because the HINT turned out to be wrong.
2483 Fall through to the normal processing. */
2486 /* Otherwise, if v points (refers) to a public base class subobject of the
2487 most derived object, and the type of the most derived object has a base
2488 class, of type C, that is unambiguous and public, the result points
2489 (refers) to the C subobject of the most derived object.
2491 But it can also be an invalid case. */
2493 /* Get the most derived object. */
2494 obj
= get_component_with_type (obj
, mdtype
, NULL_TREE
);
2495 if (obj
== error_mark_node
)
2501 error_at (loc
, "reference %<dynamic_cast%> failed");
2502 inform (loc
, "static type %qT of its operand is a non-public"
2503 " base class of dynamic type %qT", objtype
, mdtype
);
2505 *non_constant_p
= true;
2507 return integer_zero_node
;
2512 /* Check that the type of the most derived object has a base class
2513 of type TYPE that is unambiguous and public. */
2515 tree binfo
= lookup_base (mdtype
, type
, ba_check
, &b_kind
, tf_none
);
2516 if (!binfo
|| binfo
== error_mark_node
)
2522 error_at (loc
, "reference %<dynamic_cast%> failed");
2523 if (b_kind
== bk_ambig
)
2524 inform (loc
, "%qT is an ambiguous base class of dynamic "
2525 "type %qT of its operand", type
, mdtype
);
2527 inform (loc
, "dynamic type %qT of its operand does not "
2528 "have an unambiguous public base class %qT",
2531 *non_constant_p
= true;
2533 return integer_zero_node
;
2535 /* If so, return the TYPE subobject of the most derived object. */
2536 obj
= convert_to_base_statically (obj
, binfo
);
2537 return cp_build_addr_expr (obj
, complain
);
2540 /* Data structure used by replace_decl and replace_decl_r. */
2542 struct replace_decl_data
2544 /* The _DECL we want to replace. */
2546 /* The replacement for DECL. */
2548 /* Trees we've visited. */
2549 hash_set
<tree
> *pset
;
2550 /* Whether we've performed any replacements. */
2554 /* Helper function for replace_decl, called through cp_walk_tree. */
2557 replace_decl_r (tree
*tp
, int *walk_subtrees
, void *data
)
2559 replace_decl_data
*d
= (replace_decl_data
*) data
;
2563 *tp
= unshare_expr (d
->replacement
);
2567 else if (TYPE_P (*tp
)
2568 || d
->pset
->add (*tp
))
2574 /* Replace every occurrence of DECL with (an unshared copy of)
2575 REPLACEMENT within the expression *TP. Returns true iff a
2576 replacement was performed. */
2579 replace_decl (tree
*tp
, tree decl
, tree replacement
)
2581 gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
2582 (TREE_TYPE (decl
), TREE_TYPE (replacement
)));
2583 hash_set
<tree
> pset
;
2584 replace_decl_data data
= { decl
, replacement
, &pset
, false };
2585 cp_walk_tree (tp
, replace_decl_r
, &data
, NULL
);
2586 return data
.changed
;
2589 /* Evaluate the call T to virtual function thunk THUNK_FNDECL. */
2592 cxx_eval_thunk_call (const constexpr_ctx
*ctx
, tree t
, tree thunk_fndecl
,
2594 bool *non_constant_p
, bool *overflow_p
)
2596 tree function
= THUNK_TARGET (thunk_fndecl
);
2598 if (THUNK_VIRTUAL_OFFSET (thunk_fndecl
))
2602 if (!DECL_DECLARED_CONSTEXPR_P (function
))
2604 error ("call to non-%<constexpr%> function %qD", function
);
2605 explain_invalid_constexpr_fn (function
);
2608 /* virtual_offset is only set for virtual bases, which make the
2609 class non-literal, so we don't need to handle it here. */
2610 error ("calling constexpr member function %qD through virtual "
2611 "base subobject", function
);
2613 *non_constant_p
= true;
2617 tree new_call
= copy_node (t
);
2618 CALL_EXPR_FN (new_call
) = function
;
2619 TREE_TYPE (new_call
) = TREE_TYPE (TREE_TYPE (function
));
2621 tree offset
= size_int (THUNK_FIXED_OFFSET (thunk_fndecl
));
2623 if (DECL_THIS_THUNK_P (thunk_fndecl
))
2625 /* 'this'-adjusting thunk. */
2626 tree this_arg
= CALL_EXPR_ARG (t
, 0);
2627 this_arg
= build2 (POINTER_PLUS_EXPR
, TREE_TYPE (this_arg
),
2629 CALL_EXPR_ARG (new_call
, 0) = this_arg
;
2632 /* Return-adjusting thunk. */
2633 new_call
= build2 (POINTER_PLUS_EXPR
, TREE_TYPE (new_call
),
2636 return cxx_eval_constant_expression (ctx
, new_call
, lval
,
2637 non_constant_p
, overflow_p
);
2640 /* If OBJECT is of const class type, evaluate it to a CONSTRUCTOR and set
2641 its TREE_READONLY flag according to READONLY_P. Used for constexpr
2642 'tors to detect modifying const objects in a constexpr context. */
2645 cxx_set_object_constness (const constexpr_ctx
*ctx
, tree object
,
2646 bool readonly_p
, bool *non_constant_p
,
2649 if (CLASS_TYPE_P (TREE_TYPE (object
))
2650 && CP_TYPE_CONST_P (TREE_TYPE (object
)))
2652 /* Subobjects might not be stored in ctx->global->values but we
2653 can get its CONSTRUCTOR by evaluating *this. */
2654 tree e
= cxx_eval_constant_expression (ctx
, object
, vc_prvalue
,
2655 non_constant_p
, overflow_p
);
2656 if (TREE_CODE (e
) == CONSTRUCTOR
&& !*non_constant_p
)
2657 TREE_READONLY (e
) = readonly_p
;
2661 /* Subroutine of cxx_eval_constant_expression.
2662 Evaluate the call expression tree T in the context of OLD_CALL expression
2666 cxx_eval_call_expression (const constexpr_ctx
*ctx
, tree t
,
2668 bool *non_constant_p
, bool *overflow_p
)
2670 /* Handle concept checks separately. */
2671 if (concept_check_p (t
))
2672 return evaluate_concept_check (t
);
2674 location_t loc
= cp_expr_loc_or_input_loc (t
);
2675 tree fun
= get_function_named_in_call (t
);
2676 constexpr_call new_call
2677 = { NULL
, NULL
, NULL
, 0, ctx
->manifestly_const_eval
};
2680 if (fun
== NULL_TREE
)
2681 return cxx_eval_internal_function (ctx
, t
, lval
,
2682 non_constant_p
, overflow_p
);
2684 if (TREE_CODE (fun
) != FUNCTION_DECL
)
2686 /* Might be a constexpr function pointer. */
2687 fun
= cxx_eval_constant_expression (ctx
, fun
, vc_prvalue
,
2688 non_constant_p
, overflow_p
);
2690 if (TREE_CODE (fun
) == ADDR_EXPR
)
2691 fun
= TREE_OPERAND (fun
, 0);
2692 /* For TARGET_VTABLE_USES_DESCRIPTORS targets, there is no
2693 indirection, the called expression is a pointer into the
2694 virtual table which should contain FDESC_EXPR. Extract the
2695 FUNCTION_DECL from there. */
2696 else if (TARGET_VTABLE_USES_DESCRIPTORS
2697 && TREE_CODE (fun
) == POINTER_PLUS_EXPR
2698 && TREE_CODE (TREE_OPERAND (fun
, 0)) == ADDR_EXPR
2699 && TREE_CODE (TREE_OPERAND (fun
, 1)) == INTEGER_CST
)
2701 tree d
= TREE_OPERAND (TREE_OPERAND (fun
, 0), 0);
2703 && DECL_VTABLE_OR_VTT_P (d
)
2704 && TREE_CODE (TREE_TYPE (d
)) == ARRAY_TYPE
2705 && TREE_TYPE (TREE_TYPE (d
)) == vtable_entry_type
2707 && TREE_CODE (DECL_INITIAL (d
)) == CONSTRUCTOR
)
2709 tree i
= int_const_binop (TRUNC_DIV_EXPR
, TREE_OPERAND (fun
, 1),
2710 TYPE_SIZE_UNIT (vtable_entry_type
));
2711 HOST_WIDE_INT idx
= find_array_ctor_elt (DECL_INITIAL (d
), i
);
2715 = (*CONSTRUCTOR_ELTS (DECL_INITIAL (d
)))[idx
].value
;
2716 if (TREE_CODE (fdesc
) == FDESC_EXPR
2717 && integer_zerop (TREE_OPERAND (fdesc
, 1)))
2718 fun
= TREE_OPERAND (fdesc
, 0);
2723 if (TREE_CODE (fun
) != FUNCTION_DECL
)
2725 if (!ctx
->quiet
&& !*non_constant_p
)
2726 error_at (loc
, "expression %qE does not designate a %<constexpr%> "
2728 *non_constant_p
= true;
2731 if (DECL_CLONED_FUNCTION_P (fun
) && !DECL_DELETING_DESTRUCTOR_P (fun
))
2732 fun
= DECL_CLONED_FUNCTION (fun
);
2734 if (is_ubsan_builtin_p (fun
))
2737 if (fndecl_built_in_p (fun
))
2738 return cxx_eval_builtin_function_call (ctx
, t
, fun
,
2739 lval
, non_constant_p
, overflow_p
);
2740 if (DECL_THUNK_P (fun
))
2741 return cxx_eval_thunk_call (ctx
, t
, fun
, lval
, non_constant_p
, overflow_p
);
2742 if (!maybe_constexpr_fn (fun
))
2744 if (TREE_CODE (t
) == CALL_EXPR
2745 && cxx_replaceable_global_alloc_fn (fun
)
2746 && (CALL_FROM_NEW_OR_DELETE_P (t
)
2747 || is_std_allocator_allocate (ctx
->call
)))
2749 const int nargs
= call_expr_nargs (t
);
2750 tree arg0
= NULL_TREE
;
2751 for (int i
= 0; i
< nargs
; ++i
)
2753 tree arg
= CALL_EXPR_ARG (t
, i
);
2754 arg
= cxx_eval_constant_expression (ctx
, arg
, vc_prvalue
,
2755 non_constant_p
, overflow_p
);
2756 VERIFY_CONSTANT (arg
);
2761 if (IDENTIFIER_NEW_OP_P (DECL_NAME (fun
)))
2763 tree type
= build_array_type_nelts (char_type_node
,
2764 tree_to_uhwi (arg0
));
2765 tree var
= build_decl (loc
, VAR_DECL
,
2766 (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun
))
2768 ? heap_vec_uninit_identifier
2769 : heap_uninit_identifier
,
2771 DECL_ARTIFICIAL (var
) = 1;
2772 TREE_STATIC (var
) = 1;
2773 // Temporarily register the artificial var in varpool,
2774 // so that comparisons of its address against NULL are folded
2775 // through nonzero_address even with
2776 // -fno-delete-null-pointer-checks or that comparison of
2777 // addresses of different heap artificial vars is folded too.
2778 // See PR98988 and PR99031.
2779 varpool_node::finalize_decl (var
);
2780 ctx
->global
->heap_vars
.safe_push (var
);
2781 ctx
->global
->put_value (var
, NULL_TREE
);
2782 return fold_convert (ptr_type_node
, build_address (var
));
2787 if (TREE_CODE (arg0
) == ADDR_EXPR
2788 && VAR_P (TREE_OPERAND (arg0
, 0)))
2790 tree var
= TREE_OPERAND (arg0
, 0);
2791 if (DECL_NAME (var
) == heap_uninit_identifier
2792 || DECL_NAME (var
) == heap_identifier
)
2794 if (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun
))
2799 error_at (loc
, "array deallocation of object "
2800 "allocated with non-array "
2802 inform (DECL_SOURCE_LOCATION (var
),
2803 "allocation performed here");
2805 *non_constant_p
= true;
2808 DECL_NAME (var
) = heap_deleted_identifier
;
2809 ctx
->global
->remove_value (var
);
2810 ctx
->global
->heap_dealloc_count
++;
2813 else if (DECL_NAME (var
) == heap_vec_uninit_identifier
2814 || DECL_NAME (var
) == heap_vec_identifier
)
2816 if ((IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun
))
2817 & OVL_OP_FLAG_VEC
) == 0)
2821 error_at (loc
, "non-array deallocation of "
2822 "object allocated with array "
2824 inform (DECL_SOURCE_LOCATION (var
),
2825 "allocation performed here");
2827 *non_constant_p
= true;
2830 DECL_NAME (var
) = heap_deleted_identifier
;
2831 ctx
->global
->remove_value (var
);
2832 ctx
->global
->heap_dealloc_count
++;
2835 else if (DECL_NAME (var
) == heap_deleted_identifier
)
2838 error_at (loc
, "deallocation of already deallocated "
2840 *non_constant_p
= true;
2845 error_at (loc
, "deallocation of storage that was "
2846 "not previously allocated");
2847 *non_constant_p
= true;
2851 /* Allow placement new in std::construct_at, just return the second
2853 if (TREE_CODE (t
) == CALL_EXPR
2854 && cxx_placement_new_fn (fun
)
2855 && is_std_construct_at (ctx
->call
))
2857 const int nargs
= call_expr_nargs (t
);
2858 tree arg1
= NULL_TREE
;
2859 for (int i
= 0; i
< nargs
; ++i
)
2861 tree arg
= CALL_EXPR_ARG (t
, i
);
2862 arg
= cxx_eval_constant_expression (ctx
, arg
, vc_prvalue
,
2863 non_constant_p
, overflow_p
);
2867 VERIFY_CONSTANT (arg
);
2872 else if (cxx_dynamic_cast_fn_p (fun
))
2873 return cxx_eval_dynamic_cast_fn (ctx
, t
, non_constant_p
, overflow_p
);
2877 if (!lambda_static_thunk_p (fun
))
2878 error_at (loc
, "call to non-%<constexpr%> function %qD", fun
);
2879 explain_invalid_constexpr_fn (fun
);
2881 *non_constant_p
= true;
2885 constexpr_ctx new_ctx
= *ctx
;
2886 if (DECL_CONSTRUCTOR_P (fun
) && !ctx
->object
2887 && TREE_CODE (t
) == AGGR_INIT_EXPR
)
2889 /* We want to have an initialization target for an AGGR_INIT_EXPR.
2890 If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
2891 new_ctx
.object
= AGGR_INIT_EXPR_SLOT (t
);
2892 tree ctor
= new_ctx
.ctor
= build_constructor (DECL_CONTEXT (fun
), NULL
);
2893 CONSTRUCTOR_NO_CLEARING (ctor
) = true;
2894 ctx
->global
->put_value (new_ctx
.object
, ctor
);
2898 /* We used to shortcut trivial constructor/op= here, but nowadays
2899 we can only get a trivial function here with -fno-elide-constructors. */
2900 gcc_checking_assert (!trivial_fn_p (fun
)
2901 || !flag_elide_constructors
2902 /* We don't elide constructors when processing
2903 a noexcept-expression. */
2904 || cp_noexcept_operand
);
2906 bool non_constant_args
= false;
2908 = cxx_bind_parameters_in_call (ctx
, t
, fun
, non_constant_p
,
2909 overflow_p
, &non_constant_args
);
2911 /* We build up the bindings list before we know whether we already have this
2912 call cached. If we don't end up saving these bindings, ggc_free them when
2913 this function exits. */
2918 free_bindings (tree
&b
): bindings (&b
) { }
2919 ~free_bindings () { if (bindings
) ggc_free (*bindings
); }
2920 void preserve () { bindings
= NULL
; }
2921 } fb (new_call
.bindings
);
2923 if (*non_constant_p
)
2926 /* We can't defer instantiating the function any longer. */
2927 if (!DECL_INITIAL (fun
)
2928 && (DECL_TEMPLOID_INSTANTIATION (fun
) || DECL_DEFAULTED_FN (fun
))
2929 && !uid_sensitive_constexpr_evaluation_p ())
2931 location_t save_loc
= input_location
;
2932 input_location
= loc
;
2934 if (ctx
->manifestly_const_eval
== mce_true
)
2935 FNDECL_MANIFESTLY_CONST_EVALUATED (fun
) = true;
2936 if (DECL_TEMPLOID_INSTANTIATION (fun
))
2937 instantiate_decl (fun
, /*defer_ok*/false, /*expl_inst*/false);
2939 synthesize_method (fun
);
2941 input_location
= save_loc
;
2944 /* If in direct recursive call, optimize definition search. */
2945 if (ctx
&& ctx
->call
&& ctx
->call
->fundef
&& ctx
->call
->fundef
->decl
== fun
)
2946 new_call
.fundef
= ctx
->call
->fundef
;
2949 new_call
.fundef
= retrieve_constexpr_fundef (fun
);
2950 if (new_call
.fundef
== NULL
|| new_call
.fundef
->body
== NULL
2951 || new_call
.fundef
->result
== error_mark_node
2952 || fun
== current_function_decl
)
2956 /* We need to check for current_function_decl here in case we're
2957 being called during cp_fold_function, because at that point
2958 DECL_INITIAL is set properly and we have a fundef but we
2959 haven't lowered invisirefs yet (c++/70344). */
2960 if (DECL_INITIAL (fun
) == error_mark_node
2961 || fun
== current_function_decl
)
2962 error_at (loc
, "%qD called in a constant expression before its "
2963 "definition is complete", fun
);
2964 else if (DECL_INITIAL (fun
))
2966 /* The definition of fun was somehow unsuitable. But pretend
2967 that lambda static thunks don't exist. */
2968 if (!lambda_static_thunk_p (fun
))
2969 error_at (loc
, "%qD called in a constant expression", fun
);
2970 explain_invalid_constexpr_fn (fun
);
2973 error_at (loc
, "%qD used before its definition", fun
);
2975 *non_constant_p
= true;
2980 depth_ok
= push_cx_call_context (t
);
2982 /* Remember the object we are constructing or destructing. */
2983 tree new_obj
= NULL_TREE
;
2984 if (DECL_CONSTRUCTOR_P (fun
) || DECL_DESTRUCTOR_P (fun
))
2986 /* In a cdtor, it should be the first `this' argument.
2987 At this point it has already been evaluated in the call
2988 to cxx_bind_parameters_in_call. */
2989 new_obj
= TREE_VEC_ELT (new_call
.bindings
, 0);
2990 new_obj
= cxx_fold_indirect_ref (ctx
, loc
, DECL_CONTEXT (fun
), new_obj
);
2992 if (ctx
->call
&& ctx
->call
->fundef
2993 && DECL_CONSTRUCTOR_P (ctx
->call
->fundef
->decl
))
2995 tree cur_obj
= TREE_VEC_ELT (ctx
->call
->bindings
, 0);
2996 STRIP_NOPS (cur_obj
);
2997 if (TREE_CODE (cur_obj
) == ADDR_EXPR
)
2998 cur_obj
= TREE_OPERAND (cur_obj
, 0);
2999 if (new_obj
== cur_obj
)
3000 /* We're calling the target constructor of a delegating
3001 constructor, or accessing a base subobject through a
3002 NOP_EXPR as part of a call to a base constructor, so
3003 there is no new (sub)object. */
3004 new_obj
= NULL_TREE
;
3008 tree result
= NULL_TREE
;
3010 constexpr_call
*entry
= NULL
;
3011 if (depth_ok
&& !non_constant_args
&& ctx
->strict
)
3013 new_call
.hash
= constexpr_fundef_hasher::hash (new_call
.fundef
);
3015 = iterative_hash_template_arg (new_call
.bindings
, new_call
.hash
);
3017 = iterative_hash_object (ctx
->manifestly_const_eval
, new_call
.hash
);
3019 /* If we have seen this call before, we are done. */
3020 maybe_initialize_constexpr_call_table ();
3021 bool insert
= depth_ok
< constexpr_cache_depth
;
3022 constexpr_call
**slot
3023 = constexpr_call_table
->find_slot (&new_call
,
3024 insert
? INSERT
: NO_INSERT
);
3025 entry
= slot
? *slot
: NULL
;
3028 /* Only cache up to constexpr_cache_depth to limit memory use. */
3031 /* We need to keep a pointer to the entry, not just the slot, as
3032 the slot can move during evaluation of the body. */
3033 *slot
= entry
= ggc_alloc
<constexpr_call
> ();
3038 /* Calls that are in progress have their result set to NULL, so that we
3039 can detect circular dependencies. Now that we only cache up to
3040 constexpr_cache_depth this won't catch circular dependencies that
3041 start deeper, but they'll hit the recursion or ops limit. */
3042 else if (entry
->result
== NULL
)
3045 error ("call has circular dependency");
3046 *non_constant_p
= true;
3047 entry
->result
= result
= error_mark_node
;
3050 result
= entry
->result
;
3056 error ("%<constexpr%> evaluation depth exceeds maximum of %d (use "
3057 "%<-fconstexpr-depth=%> to increase the maximum)",
3058 max_constexpr_depth
);
3059 *non_constant_p
= true;
3060 result
= error_mark_node
;
3064 bool cacheable
= !!entry
;
3065 if (result
&& result
!= error_mark_node
)
3067 else if (!DECL_SAVED_TREE (fun
))
3069 /* When at_eof >= 2, cgraph has started throwing away
3070 DECL_SAVED_TREE, so fail quietly. FIXME we get here because of
3071 late code generation for VEC_INIT_EXPR, which needs to be
3072 completely reconsidered. */
3073 gcc_assert (at_eof
>= 2 && ctx
->quiet
);
3074 *non_constant_p
= true;
3076 else if (tree copy
= get_fundef_copy (new_call
.fundef
))
3078 tree body
, parms
, res
;
3079 releasing_vec ctors
;
3081 /* Reuse or create a new unshared copy of this function's body. */
3082 body
= TREE_PURPOSE (copy
);
3083 parms
= TREE_VALUE (copy
);
3084 res
= TREE_TYPE (copy
);
3086 /* Associate the bindings with the remapped parms. */
3087 tree bound
= new_call
.bindings
;
3088 tree remapped
= parms
;
3089 for (int i
= 0; i
< TREE_VEC_LENGTH (bound
); ++i
)
3091 tree arg
= TREE_VEC_ELT (bound
, i
);
3094 /* Unshare args going into the hash table to separate them
3095 from the caller's context, for better GC and to avoid
3096 problems with verify_gimple. */
3097 arg
= unshare_expr_without_location (arg
);
3098 TREE_VEC_ELT (bound
, i
) = arg
;
3100 /* And then unshare again so the callee doesn't change the
3101 argument values in the hash table. XXX Could we unshare
3102 lazily in cxx_eval_store_expression? */
3103 arg
= unshare_constructor (arg
);
3104 if (TREE_CODE (arg
) == CONSTRUCTOR
)
3105 vec_safe_push (ctors
, arg
);
3107 ctx
->global
->put_value (remapped
, arg
);
3108 remapped
= DECL_CHAIN (remapped
);
3110 /* Add the RESULT_DECL to the values map, too. */
3111 gcc_assert (!DECL_BY_REFERENCE (res
));
3112 ctx
->global
->put_value (res
, NULL_TREE
);
3114 /* Track the callee's evaluated SAVE_EXPRs and TARGET_EXPRs so that
3115 we can forget their values after the call. */
3116 constexpr_ctx ctx_with_save_exprs
= *ctx
;
3117 auto_vec
<tree
, 10> save_exprs
;
3118 ctx_with_save_exprs
.save_exprs
= &save_exprs
;
3119 ctx_with_save_exprs
.call
= &new_call
;
3120 unsigned save_heap_alloc_count
= ctx
->global
->heap_vars
.length ();
3121 unsigned save_heap_dealloc_count
= ctx
->global
->heap_dealloc_count
;
3123 /* If this is a constexpr destructor, the object's const and volatile
3124 semantics are no longer in effect; see [class.dtor]p5. */
3125 if (new_obj
&& DECL_DESTRUCTOR_P (fun
))
3126 cxx_set_object_constness (ctx
, new_obj
, /*readonly_p=*/false,
3127 non_constant_p
, overflow_p
);
3129 tree jump_target
= NULL_TREE
;
3130 cxx_eval_constant_expression (&ctx_with_save_exprs
, body
,
3131 vc_discard
, non_constant_p
, overflow_p
,
3134 if (DECL_CONSTRUCTOR_P (fun
))
3136 /* This can be null for a subobject constructor call, in
3137 which case what we care about is the initialization
3138 side-effects rather than the value. We could get at the
3139 value by evaluating *this, but we don't bother; there's
3140 no need to put such a call in the hash table. */
3141 result
= lval
? ctx
->object
: ctx
->ctor
;
3143 /* If we've just evaluated a subobject constructor call for an
3144 empty union member, it might not have produced a side effect
3145 that actually activated the union member. So produce such a
3146 side effect now to ensure the union appears initialized. */
3147 if (!result
&& new_obj
3148 && TREE_CODE (new_obj
) == COMPONENT_REF
3149 && TREE_CODE (TREE_TYPE
3150 (TREE_OPERAND (new_obj
, 0))) == UNION_TYPE
3151 && is_really_empty_class (TREE_TYPE (new_obj
),
3152 /*ignore_vptr*/false))
3154 tree activate
= build2 (MODIFY_EXPR
, TREE_TYPE (new_obj
),
3156 build_constructor (TREE_TYPE (new_obj
),
3158 cxx_eval_constant_expression (ctx
, activate
, lval
,
3159 non_constant_p
, overflow_p
);
3160 ggc_free (activate
);
3163 else if (VOID_TYPE_P (TREE_TYPE (res
)))
3167 result
= ctx
->global
->get_value (res
);
3168 if (result
== NULL_TREE
&& !*non_constant_p
3169 && !DECL_DESTRUCTOR_P (fun
))
3172 error ("%<constexpr%> call flows off the end "
3174 *non_constant_p
= true;
3178 /* At this point, the object's constructor will have run, so
3179 the object is no longer under construction, and its possible
3180 'const' semantics now apply. Make a note of this fact by
3181 marking the CONSTRUCTOR TREE_READONLY. */
3182 if (new_obj
&& DECL_CONSTRUCTOR_P (fun
))
3183 cxx_set_object_constness (ctx
, new_obj
, /*readonly_p=*/true,
3184 non_constant_p
, overflow_p
);
3186 /* Forget the saved values of the callee's SAVE_EXPRs and
3188 for (tree save_expr
: save_exprs
)
3189 ctx
->global
->remove_value (save_expr
);
3191 /* Remove the parms/result from the values map. Is it worth
3192 bothering to do this when the map itself is only live for
3193 one constexpr evaluation? If so, maybe also clear out
3194 other vars from call, maybe in BIND_EXPR handling? */
3195 ctx
->global
->remove_value (res
);
3196 for (tree parm
= parms
; parm
; parm
= TREE_CHAIN (parm
))
3197 ctx
->global
->remove_value (parm
);
3199 /* Free any parameter CONSTRUCTORs we aren't returning directly. */
3200 while (!ctors
->is_empty ())
3202 tree c
= ctors
->pop ();
3204 free_constructor (c
);
3207 /* Make the unshared function copy we used available for re-use. */
3208 save_fundef_copy (fun
, copy
);
3210 /* If the call allocated some heap object that hasn't been
3211 deallocated during the call, or if it deallocated some heap
3212 object it has not allocated, the call isn't really stateless
3213 for the constexpr evaluation and should not be cached.
3214 It is fine if the call allocates something and deallocates it
3217 && (save_heap_alloc_count
!= ctx
->global
->heap_vars
.length ()
3218 || (save_heap_dealloc_count
3219 != ctx
->global
->heap_dealloc_count
)))
3223 if ((ctx
->global
->heap_vars
.length ()
3224 - ctx
->global
->heap_dealloc_count
)
3225 != save_heap_alloc_count
- save_heap_dealloc_count
)
3228 FOR_EACH_VEC_ELT_FROM (ctx
->global
->heap_vars
, i
, heap_var
,
3229 save_heap_alloc_count
)
3230 if (DECL_NAME (heap_var
) != heap_deleted_identifier
)
3237 /* Rewrite all occurrences of the function's RESULT_DECL with the
3238 current object under construction. */
3239 if (!*non_constant_p
&& ctx
->object
3240 && CLASS_TYPE_P (TREE_TYPE (res
))
3241 && !is_empty_class (TREE_TYPE (res
)))
3242 if (replace_decl (&result
, res
, ctx
->object
))
3245 /* Only cache a permitted result of a constant expression. */
3246 if (cacheable
&& !reduced_constant_expression_p (result
))
3250 /* Couldn't get a function copy to evaluate. */
3251 *non_constant_p
= true;
3253 if (result
== error_mark_node
)
3254 *non_constant_p
= true;
3255 if (*non_constant_p
|| *overflow_p
)
3256 result
= error_mark_node
;
3260 entry
->result
= cacheable
? result
: error_mark_node
;
3263 /* The result of a constexpr function must be completely initialized.
3265 However, in C++20, a constexpr constructor doesn't necessarily have
3266 to initialize all the fields, so we don't clear CONSTRUCTOR_NO_CLEARING
3267 in order to detect reading an unitialized object in constexpr instead
3268 of value-initializing it. (reduced_constant_expression_p is expected to
3269 take care of clearing the flag.) */
3270 if (TREE_CODE (result
) == CONSTRUCTOR
3271 && (cxx_dialect
< cxx20
3272 || !DECL_CONSTRUCTOR_P (fun
)))
3273 clear_no_implicit_zero (result
);
3275 pop_cx_call_context ();
3279 /* Return true if T is a valid constant initializer. If a CONSTRUCTOR
3280 initializes all the members, the CONSTRUCTOR_NO_CLEARING flag will be
3282 FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
3285 reduced_constant_expression_p (tree t
)
3290 switch (TREE_CODE (t
))
3293 /* Even if we can't lower this yet, it's constant. */
3297 /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
3299 if (!AGGREGATE_TYPE_P (TREE_TYPE (t
)))
3300 /* A constant vector would be folded to VECTOR_CST.
3301 A CONSTRUCTOR of scalar type means uninitialized. */
3303 if (CONSTRUCTOR_NO_CLEARING (t
))
3305 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
3307 /* There must be a valid constant initializer at every array
3309 tree min
= TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (t
)));
3310 tree max
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (t
)));
3312 for (auto &e
: CONSTRUCTOR_ELTS (t
))
3314 if (!reduced_constant_expression_p (e
.value
))
3316 if (array_index_cmp (cursor
, e
.index
) != 0)
3318 if (TREE_CODE (e
.index
) == RANGE_EXPR
)
3319 cursor
= TREE_OPERAND (e
.index
, 1);
3320 cursor
= int_const_binop (PLUS_EXPR
, cursor
, size_one_node
);
3322 if (find_array_ctor_elt (t
, max
) == -1)
3326 else if (cxx_dialect
>= cxx20
3327 && TREE_CODE (TREE_TYPE (t
)) == UNION_TYPE
)
3329 if (CONSTRUCTOR_NELTS (t
) == 0)
3330 /* An initialized union has a constructor element. */
3332 /* And it only initializes one member. */
3336 field
= next_subobject_field (TYPE_FIELDS (TREE_TYPE (t
)));
3340 for (auto &e
: CONSTRUCTOR_ELTS (t
))
3342 /* If VAL is null, we're in the middle of initializing this
3344 if (!reduced_constant_expression_p (e
.value
))
3346 /* We want to remove initializers for empty fields in a struct to
3347 avoid confusing output_constructor. */
3348 if (is_empty_field (e
.index
)
3349 && TREE_CODE (TREE_TYPE (t
)) == RECORD_TYPE
)
3351 /* Check for non-empty fields between initialized fields when
3352 CONSTRUCTOR_NO_CLEARING. */
3353 for (; field
&& e
.index
!= field
;
3354 field
= next_subobject_field (DECL_CHAIN (field
)))
3355 if (!is_really_empty_class (TREE_TYPE (field
),
3356 /*ignore_vptr*/false))
3359 field
= next_subobject_field (DECL_CHAIN (field
));
3361 /* There could be a non-empty field at the end. */
3362 for (; field
; field
= next_subobject_field (DECL_CHAIN (field
)))
3363 if (!is_really_empty_class (TREE_TYPE (field
), /*ignore_vptr*/false))
3366 if (CONSTRUCTOR_NO_CLEARING (t
))
3367 /* All the fields are initialized. */
3368 CONSTRUCTOR_NO_CLEARING (t
) = false;
3372 /* FIXME are we calling this too much? */
3373 return initializer_constant_valid_p (t
, TREE_TYPE (t
)) != NULL_TREE
;
3377 /* Some expressions may have constant operands but are not constant
3378 themselves, such as 1/0. Call this function to check for that
3381 We only call this in places that require an arithmetic constant, not in
3382 places where we might have a non-constant expression that can be a
3383 component of a constant expression, such as the address of a constexpr
3384 variable that might be dereferenced later. */
3387 verify_constant (tree t
, bool allow_non_constant
, bool *non_constant_p
,
3390 if (!*non_constant_p
&& !reduced_constant_expression_p (t
)
3393 if (!allow_non_constant
)
3394 error ("%q+E is not a constant expression", t
);
3395 *non_constant_p
= true;
3397 if (TREE_OVERFLOW_P (t
))
3399 if (!allow_non_constant
)
3401 permerror (input_location
, "overflow in constant expression");
3402 /* If we're being permissive (and are in an enforcing
3403 context), ignore the overflow. */
3404 if (flag_permissive
)
3405 return *non_constant_p
;
3409 return *non_constant_p
;
3412 /* Check whether the shift operation with code CODE and type TYPE on LHS
3413 and RHS is undefined. If it is, give an error with an explanation,
3414 and return true; return false otherwise. */
3417 cxx_eval_check_shift_p (location_t loc
, const constexpr_ctx
*ctx
,
3418 enum tree_code code
, tree type
, tree lhs
, tree rhs
)
3420 if ((code
!= LSHIFT_EXPR
&& code
!= RSHIFT_EXPR
)
3421 || TREE_CODE (lhs
) != INTEGER_CST
3422 || TREE_CODE (rhs
) != INTEGER_CST
)
3425 tree lhstype
= TREE_TYPE (lhs
);
3426 unsigned HOST_WIDE_INT uprec
= TYPE_PRECISION (TREE_TYPE (lhs
));
3428 /* [expr.shift] The behavior is undefined if the right operand
3429 is negative, or greater than or equal to the length in bits
3430 of the promoted left operand. */
3431 if (tree_int_cst_sgn (rhs
) == -1)
3434 permerror (loc
, "right operand of shift expression %q+E is negative",
3435 build2_loc (loc
, code
, type
, lhs
, rhs
));
3436 return (!flag_permissive
|| ctx
->quiet
);
3438 if (compare_tree_int (rhs
, uprec
) >= 0)
3441 permerror (loc
, "right operand of shift expression %q+E is greater "
3442 "than or equal to the precision %wu of the left operand",
3443 build2_loc (loc
, code
, type
, lhs
, rhs
), uprec
);
3444 return (!flag_permissive
|| ctx
->quiet
);
3447 /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
3448 if E1 has a signed type and non-negative value, and E1x2^E2 is
3449 representable in the corresponding unsigned type of the result type,
3450 then that value, converted to the result type, is the resulting value;
3451 otherwise, the behavior is undefined.
3453 The value of E1 << E2 is the unique value congruent to E1 x 2^E2 modulo
3454 2^N, where N is the range exponent of the type of the result. */
3455 if (code
== LSHIFT_EXPR
3456 && !TYPE_OVERFLOW_WRAPS (lhstype
)
3457 && cxx_dialect
>= cxx11
3458 && cxx_dialect
< cxx20
)
3460 if (tree_int_cst_sgn (lhs
) == -1)
3464 "left operand of shift expression %q+E is negative",
3465 build2_loc (loc
, code
, type
, lhs
, rhs
));
3466 return (!flag_permissive
|| ctx
->quiet
);
3468 /* For signed x << y the following:
3469 (unsigned) x >> ((prec (lhs) - 1) - y)
3470 if > 1, is undefined. The right-hand side of this formula
3471 is the highest bit of the LHS that can be set (starting from 0),
3472 so that the shift doesn't overflow. We then right-shift the LHS
3473 to see whether any other bit is set making the original shift
3474 undefined -- the result is not representable in the corresponding
3476 tree t
= build_int_cst (unsigned_type_node
, uprec
- 1);
3477 t
= fold_build2 (MINUS_EXPR
, unsigned_type_node
, t
, rhs
);
3478 tree ulhs
= fold_convert (unsigned_type_for (lhstype
), lhs
);
3479 t
= fold_build2 (RSHIFT_EXPR
, TREE_TYPE (ulhs
), ulhs
, t
);
3480 if (tree_int_cst_lt (integer_one_node
, t
))
3483 permerror (loc
, "shift expression %q+E overflows",
3484 build2_loc (loc
, code
, type
, lhs
, rhs
));
3485 return (!flag_permissive
|| ctx
->quiet
);
3491 /* Subroutine of cxx_eval_constant_expression.
3492 Attempt to reduce the unary expression tree T to a compile time value.
3493 If successful, return the value. Otherwise issue a diagnostic
3494 and return error_mark_node. */
3497 cxx_eval_unary_expression (const constexpr_ctx
*ctx
, tree t
,
3499 bool *non_constant_p
, bool *overflow_p
)
3502 tree orig_arg
= TREE_OPERAND (t
, 0);
3503 tree arg
= cxx_eval_constant_expression (ctx
, orig_arg
, vc_prvalue
,
3504 non_constant_p
, overflow_p
);
3505 VERIFY_CONSTANT (arg
);
3506 location_t loc
= EXPR_LOCATION (t
);
3507 enum tree_code code
= TREE_CODE (t
);
3508 tree type
= TREE_TYPE (t
);
3509 r
= fold_unary_loc (loc
, code
, type
, arg
);
3512 if (arg
== orig_arg
)
3515 r
= build1_loc (loc
, code
, type
, arg
);
3517 VERIFY_CONSTANT (r
);
3521 /* Helper function for cxx_eval_binary_expression. Try to optimize
3522 original POINTER_PLUS_EXPR T, LHS p+ RHS, return NULL_TREE if the
3523 generic folding should be used. */
3526 cxx_fold_pointer_plus_expression (const constexpr_ctx
*ctx
, tree t
,
3527 tree lhs
, tree rhs
, bool *non_constant_p
,
3531 if (TREE_CODE (lhs
) != ADDR_EXPR
)
3534 lhs
= TREE_OPERAND (lhs
, 0);
3536 /* &A[i] p+ j => &A[i + j] */
3537 if (TREE_CODE (lhs
) == ARRAY_REF
3538 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == INTEGER_CST
3539 && TREE_CODE (rhs
) == INTEGER_CST
3540 && TYPE_SIZE_UNIT (TREE_TYPE (lhs
))
3541 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs
))) == INTEGER_CST
)
3543 tree orig_type
= TREE_TYPE (t
);
3544 location_t loc
= EXPR_LOCATION (t
);
3545 tree type
= TREE_TYPE (lhs
);
3547 t
= fold_convert_loc (loc
, ssizetype
, TREE_OPERAND (lhs
, 1));
3548 tree nelts
= array_type_nelts_top (TREE_TYPE (TREE_OPERAND (lhs
, 0)));
3549 nelts
= cxx_eval_constant_expression (ctx
, nelts
, vc_prvalue
,
3550 non_constant_p
, overflow_p
);
3551 if (*non_constant_p
)
3553 /* Don't fold an out-of-bound access. */
3554 if (!tree_int_cst_le (t
, nelts
))
3556 rhs
= cp_fold_convert (ssizetype
, rhs
);
3557 /* Don't fold if rhs can't be divided exactly by TYPE_SIZE_UNIT.
3558 constexpr int A[1]; ... (char *)&A[0] + 1 */
3559 if (!integer_zerop (fold_build2_loc (loc
, TRUNC_MOD_EXPR
, sizetype
,
3560 rhs
, TYPE_SIZE_UNIT (type
))))
3562 /* Make sure to treat the second operand of POINTER_PLUS_EXPR
3564 rhs
= fold_build2_loc (loc
, EXACT_DIV_EXPR
, ssizetype
, rhs
,
3565 TYPE_SIZE_UNIT (type
));
3566 t
= size_binop_loc (loc
, PLUS_EXPR
, rhs
, t
);
3567 t
= build4_loc (loc
, ARRAY_REF
, type
, TREE_OPERAND (lhs
, 0),
3568 t
, NULL_TREE
, NULL_TREE
);
3569 t
= cp_build_addr_expr (t
, tf_warning_or_error
);
3570 t
= cp_fold_convert (orig_type
, t
);
3571 return cxx_eval_constant_expression (ctx
, t
, vc_prvalue
,
3572 non_constant_p
, overflow_p
);
3578 /* Try to fold expressions like
3579 (struct S *) (&a[0].D.2378 + 12)
3581 &MEM <struct T> [(void *)&a + 12B]
3582 This is something normally done by gimple_fold_stmt_to_constant_1
3583 on GIMPLE, but is undesirable on GENERIC if we are e.g. going to
3584 dereference the address because some details are lost.
3585 For pointer comparisons we want such folding though so that
3586 match.pd address_compare optimization works. */
3589 cxx_maybe_fold_addr_pointer_plus (tree t
)
3591 while (CONVERT_EXPR_P (t
)
3592 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t
, 0))))
3593 t
= TREE_OPERAND (t
, 0);
3594 if (TREE_CODE (t
) != POINTER_PLUS_EXPR
)
3596 tree op0
= TREE_OPERAND (t
, 0);
3597 tree op1
= TREE_OPERAND (t
, 1);
3598 if (TREE_CODE (op1
) != INTEGER_CST
)
3600 while (CONVERT_EXPR_P (op0
)
3601 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0
, 0))))
3602 op0
= TREE_OPERAND (op0
, 0);
3603 if (TREE_CODE (op0
) != ADDR_EXPR
)
3605 op1
= fold_convert (ptr_type_node
, op1
);
3606 tree r
= fold_build2 (MEM_REF
, TREE_TYPE (TREE_TYPE (op0
)), op0
, op1
);
3607 return build1_loc (EXPR_LOCATION (t
), ADDR_EXPR
, TREE_TYPE (op0
), r
);
3610 /* Subroutine of cxx_eval_constant_expression.
3611 Like cxx_eval_unary_expression, except for binary expressions. */
3614 cxx_eval_binary_expression (const constexpr_ctx
*ctx
, tree t
,
3616 bool *non_constant_p
, bool *overflow_p
)
3619 tree orig_lhs
= TREE_OPERAND (t
, 0);
3620 tree orig_rhs
= TREE_OPERAND (t
, 1);
3622 lhs
= cxx_eval_constant_expression (ctx
, orig_lhs
, vc_prvalue
,
3623 non_constant_p
, overflow_p
);
3624 /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer
3626 if (*non_constant_p
)
3628 rhs
= cxx_eval_constant_expression (ctx
, orig_rhs
, vc_prvalue
,
3629 non_constant_p
, overflow_p
);
3630 if (*non_constant_p
)
3633 location_t loc
= EXPR_LOCATION (t
);
3634 enum tree_code code
= TREE_CODE (t
);
3635 tree type
= TREE_TYPE (t
);
3637 if (code
== EQ_EXPR
|| code
== NE_EXPR
)
3639 bool is_code_eq
= (code
== EQ_EXPR
);
3641 if (TREE_CODE (lhs
) == PTRMEM_CST
3642 && TREE_CODE (rhs
) == PTRMEM_CST
)
3644 tree lmem
= PTRMEM_CST_MEMBER (lhs
);
3645 tree rmem
= PTRMEM_CST_MEMBER (rhs
);
3647 if (TREE_CODE (lmem
) == TREE_CODE (rmem
)
3648 && TREE_CODE (lmem
) == FIELD_DECL
3649 && TREE_CODE (DECL_CONTEXT (lmem
)) == UNION_TYPE
3650 && same_type_p (DECL_CONTEXT (lmem
),
3651 DECL_CONTEXT (rmem
)))
3652 /* If both refer to (possibly different) members of the same union
3653 (12.3), they compare equal. */
3656 eq
= cp_tree_equal (lhs
, rhs
);
3657 r
= constant_boolean_node (eq
== is_code_eq
, type
);
3659 else if ((TREE_CODE (lhs
) == PTRMEM_CST
3660 || TREE_CODE (rhs
) == PTRMEM_CST
)
3661 && (null_member_pointer_value_p (lhs
)
3662 || null_member_pointer_value_p (rhs
)))
3663 r
= constant_boolean_node (!is_code_eq
, type
);
3664 else if (TREE_CODE (lhs
) == PTRMEM_CST
)
3665 lhs
= cplus_expand_constant (lhs
);
3666 else if (TREE_CODE (rhs
) == PTRMEM_CST
)
3667 rhs
= cplus_expand_constant (rhs
);
3670 && TREE_CODE_CLASS (code
) == tcc_comparison
3671 && POINTER_TYPE_P (TREE_TYPE (lhs
)))
3673 if (tree lhso
= cxx_maybe_fold_addr_pointer_plus (lhs
))
3674 lhs
= fold_convert (TREE_TYPE (lhs
), lhso
);
3675 if (tree rhso
= cxx_maybe_fold_addr_pointer_plus (rhs
))
3676 rhs
= fold_convert (TREE_TYPE (rhs
), rhso
);
3678 if (code
== POINTER_PLUS_EXPR
&& !*non_constant_p
3679 && integer_zerop (lhs
) && !integer_zerop (rhs
))
3682 error ("arithmetic involving a null pointer in %qE", lhs
);
3683 *non_constant_p
= true;
3686 else if (code
== POINTER_PLUS_EXPR
)
3687 r
= cxx_fold_pointer_plus_expression (ctx
, t
, lhs
, rhs
, non_constant_p
,
3689 else if (code
== SPACESHIP_EXPR
)
3691 r
= genericize_spaceship (loc
, type
, lhs
, rhs
);
3692 return cxx_eval_constant_expression (ctx
, r
, lval
, non_constant_p
,
3698 if (ctx
->manifestly_const_eval
== mce_true
3699 && (flag_constexpr_fp_except
3700 || TREE_CODE (type
) != REAL_TYPE
))
3702 auto ofcc
= make_temp_override (folding_cxx_constexpr
, true);
3703 r
= fold_binary_initializer_loc (loc
, code
, type
, lhs
, rhs
);
3706 r
= fold_binary_loc (loc
, code
, type
, lhs
, rhs
);
3710 && (code
== LSHIFT_EXPR
|| code
== RSHIFT_EXPR
)
3711 && TREE_CODE (lhs
) == INTEGER_CST
3712 && TREE_CODE (rhs
) == INTEGER_CST
3713 && wi::neg_p (wi::to_wide (rhs
)))
3715 /* For diagnostics and -fpermissive emulate previous behavior of
3716 handling shifts by negative amount. */
3717 tree nrhs
= const_unop (NEGATE_EXPR
, TREE_TYPE (rhs
), rhs
);
3719 r
= fold_binary_loc (loc
,
3720 code
== LSHIFT_EXPR
? RSHIFT_EXPR
: LSHIFT_EXPR
,
3726 if (lhs
== orig_lhs
&& rhs
== orig_rhs
)
3729 r
= build2_loc (loc
, code
, type
, lhs
, rhs
);
3731 else if (cxx_eval_check_shift_p (loc
, ctx
, code
, type
, lhs
, rhs
))
3732 *non_constant_p
= true;
3733 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
3734 a local array in a constexpr function. */
3735 bool ptr
= INDIRECT_TYPE_P (TREE_TYPE (lhs
));
3737 VERIFY_CONSTANT (r
);
3741 /* Subroutine of cxx_eval_constant_expression.
3742 Attempt to evaluate condition expressions. Dead branches are not
3746 cxx_eval_conditional_expression (const constexpr_ctx
*ctx
, tree t
,
3748 bool *non_constant_p
, bool *overflow_p
,
3751 tree val
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
3753 non_constant_p
, overflow_p
);
3754 VERIFY_CONSTANT (val
);
3755 if (TREE_CODE (t
) == IF_STMT
&& IF_STMT_CONSTEVAL_P (t
))
3757 /* Evaluate the condition as if it was
3758 if (__builtin_is_constant_evaluated ()), i.e. defer it if not
3759 ctx->manifestly_const_eval (as sometimes we try to constant evaluate
3760 without manifestly_const_eval even expressions or parts thereof which
3761 will later be manifestly const_eval evaluated), otherwise fold it to
3763 if (ctx
->manifestly_const_eval
== mce_unknown
)
3765 *non_constant_p
= true;
3768 val
= constant_boolean_node (ctx
->manifestly_const_eval
== mce_true
,
3771 /* Don't VERIFY_CONSTANT the other operands. */
3772 if (integer_zerop (val
))
3773 val
= TREE_OPERAND (t
, 2);
3775 val
= TREE_OPERAND (t
, 1);
3776 if (TREE_CODE (t
) == IF_STMT
&& !val
)
3778 /* A TARGET_EXPR may be nested inside another TARGET_EXPR, but still
3779 serve as the initializer for the same object as the outer TARGET_EXPR,
3781 A a = true ? A{} : A{};
3782 so strip the inner TARGET_EXPR so we don't materialize a temporary. */
3783 if (TREE_CODE (val
) == TARGET_EXPR
)
3784 val
= TARGET_EXPR_INITIAL (val
);
3785 return cxx_eval_constant_expression (ctx
, val
, lval
, non_constant_p
,
3786 overflow_p
, jump_target
);
3789 /* Subroutine of cxx_eval_constant_expression.
3790 Attempt to evaluate vector condition expressions. Unlike
3791 cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal
3792 ternary arithmetics operation, where all 3 arguments have to be
3793 evaluated as constants and then folding computes the result from
3797 cxx_eval_vector_conditional_expression (const constexpr_ctx
*ctx
, tree t
,
3798 bool *non_constant_p
, bool *overflow_p
)
3800 tree arg1
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
3802 non_constant_p
, overflow_p
);
3803 VERIFY_CONSTANT (arg1
);
3804 tree arg2
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
3806 non_constant_p
, overflow_p
);
3807 VERIFY_CONSTANT (arg2
);
3808 tree arg3
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 2),
3810 non_constant_p
, overflow_p
);
3811 VERIFY_CONSTANT (arg3
);
3812 location_t loc
= EXPR_LOCATION (t
);
3813 tree type
= TREE_TYPE (t
);
3814 tree r
= fold_ternary_loc (loc
, VEC_COND_EXPR
, type
, arg1
, arg2
, arg3
);
3817 if (arg1
== TREE_OPERAND (t
, 0)
3818 && arg2
== TREE_OPERAND (t
, 1)
3819 && arg3
== TREE_OPERAND (t
, 2))
3822 r
= build3_loc (loc
, VEC_COND_EXPR
, type
, arg1
, arg2
, arg3
);
3824 VERIFY_CONSTANT (r
);
3828 /* Returns less than, equal to, or greater than zero if KEY is found to be
3829 less than, to match, or to be greater than the constructor_elt's INDEX. */
3832 array_index_cmp (tree key
, tree index
)
3834 gcc_assert (TREE_CODE (key
) == INTEGER_CST
);
3836 switch (TREE_CODE (index
))
3839 return tree_int_cst_compare (key
, index
);
3842 tree lo
= TREE_OPERAND (index
, 0);
3843 tree hi
= TREE_OPERAND (index
, 1);
3844 if (tree_int_cst_lt (key
, lo
))
3846 else if (tree_int_cst_lt (hi
, key
))
3856 /* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1
3857 if none. If INSERT is true, insert a matching element rather than fail. */
3859 static HOST_WIDE_INT
3860 find_array_ctor_elt (tree ary
, tree dindex
, bool insert
)
3862 if (tree_int_cst_sgn (dindex
) < 0)
3865 unsigned HOST_WIDE_INT i
= tree_to_uhwi (dindex
);
3866 vec
<constructor_elt
, va_gc
> *elts
= CONSTRUCTOR_ELTS (ary
);
3867 unsigned HOST_WIDE_INT len
= vec_safe_length (elts
);
3869 unsigned HOST_WIDE_INT end
= len
;
3870 unsigned HOST_WIDE_INT begin
= 0;
3872 /* If the last element of the CONSTRUCTOR has its own index, we can assume
3873 that the same is true of the other elements and index directly. */
3876 tree cindex
= (*elts
)[end
- 1].index
;
3877 if (cindex
== NULL_TREE
)
3879 /* Verify that if the last index is missing, all indexes
3882 for (unsigned int j
= 0; j
< len
- 1; ++j
)
3883 gcc_assert ((*elts
)[j
].index
== NULL_TREE
);
3890 /* If the element is to be added right at the end,
3891 make sure it is added with cleared index too. */
3894 /* Otherwise, in order not to break the assumption
3895 that CONSTRUCTOR either has all indexes or none,
3896 we need to add indexes to all elements. */
3897 for (unsigned int j
= 0; j
< len
; ++j
)
3898 (*elts
)[j
].index
= build_int_cst (TREE_TYPE (dindex
), j
);
3901 else if (TREE_CODE (cindex
) == INTEGER_CST
3902 && compare_tree_int (cindex
, end
- 1) == 0)
3911 /* Otherwise, find a matching index by means of a binary search. */
3912 while (begin
!= end
)
3914 unsigned HOST_WIDE_INT middle
= (begin
+ end
) / 2;
3915 constructor_elt
&elt
= (*elts
)[middle
];
3916 tree idx
= elt
.index
;
3918 int cmp
= array_index_cmp (dindex
, idx
);
3925 if (insert
&& TREE_CODE (idx
) == RANGE_EXPR
)
3927 /* We need to split the range. */
3929 tree lo
= TREE_OPERAND (idx
, 0);
3930 tree hi
= TREE_OPERAND (idx
, 1);
3931 tree value
= elt
.value
;
3932 dindex
= fold_convert (sizetype
, dindex
);
3933 if (tree_int_cst_lt (lo
, dindex
))
3935 /* There are still some lower elts; shorten the range. */
3936 tree new_hi
= int_const_binop (MINUS_EXPR
, dindex
,
3938 if (tree_int_cst_equal (lo
, new_hi
))
3939 /* Only one element left, no longer a range. */
3942 TREE_OPERAND (idx
, 1) = new_hi
;
3943 /* Append the element we want to insert. */
3946 e
.value
= unshare_constructor (value
);
3947 vec_safe_insert (CONSTRUCTOR_ELTS (ary
), middle
, e
);
3950 /* No lower elts, the range elt is now ours. */
3953 if (tree_int_cst_lt (dindex
, hi
))
3955 /* There are still some higher elts; append a range. */
3956 tree new_lo
= int_const_binop (PLUS_EXPR
, dindex
,
3958 if (tree_int_cst_equal (new_lo
, hi
))
3961 e
.index
= build2 (RANGE_EXPR
, sizetype
, new_lo
, hi
);
3962 e
.value
= unshare_constructor (value
);
3963 vec_safe_insert (CONSTRUCTOR_ELTS (ary
), middle
+ 1, e
);
3972 constructor_elt e
= { dindex
, NULL_TREE
};
3973 vec_safe_insert (CONSTRUCTOR_ELTS (ary
), end
, e
);
3980 /* Return a pointer to the constructor_elt of CTOR which matches INDEX. If no
3981 matching constructor_elt exists, then add one to CTOR.
3983 As an optimization, if POS_HINT is non-negative then it is used as a guess
3984 for the (integer) index of the matching constructor_elt within CTOR. */
3986 static constructor_elt
*
3987 get_or_insert_ctor_field (tree ctor
, tree index
, int pos_hint
= -1)
3989 /* Check the hint first. */
3990 if (pos_hint
>= 0 && (unsigned)pos_hint
< CONSTRUCTOR_NELTS (ctor
)
3991 && CONSTRUCTOR_ELT (ctor
, pos_hint
)->index
== index
)
3992 return CONSTRUCTOR_ELT (ctor
, pos_hint
);
3994 tree type
= TREE_TYPE (ctor
);
3995 if (TREE_CODE (type
) == VECTOR_TYPE
&& index
== NULL_TREE
)
3997 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (ctor
), index
, NULL_TREE
);
3998 return &CONSTRUCTOR_ELTS (ctor
)->last();
4000 else if (TREE_CODE (type
) == ARRAY_TYPE
|| TREE_CODE (type
) == VECTOR_TYPE
)
4002 if (TREE_CODE (index
) == RANGE_EXPR
)
4004 /* Support for RANGE_EXPR index lookups is currently limited to
4005 accessing an existing element via POS_HINT, or appending a new
4006 element to the end of CTOR. ??? Support for other access
4007 patterns may also be needed. */
4008 vec
<constructor_elt
, va_gc
> *elts
= CONSTRUCTOR_ELTS (ctor
);
4009 if (vec_safe_length (elts
))
4011 tree lo
= TREE_OPERAND (index
, 0);
4012 gcc_assert (array_index_cmp (elts
->last().index
, lo
) < 0);
4014 CONSTRUCTOR_APPEND_ELT (elts
, index
, NULL_TREE
);
4015 return &elts
->last();
4018 HOST_WIDE_INT i
= find_array_ctor_elt (ctor
, index
, /*insert*/true);
4019 gcc_assert (i
>= 0);
4020 constructor_elt
*cep
= CONSTRUCTOR_ELT (ctor
, i
);
4021 gcc_assert (cep
->index
== NULL_TREE
4022 || TREE_CODE (cep
->index
) != RANGE_EXPR
);
4027 gcc_assert (TREE_CODE (index
) == FIELD_DECL
4028 && (same_type_ignoring_top_level_qualifiers_p
4029 (DECL_CONTEXT (index
), TREE_TYPE (ctor
))));
4031 /* We must keep the CONSTRUCTOR's ELTS in FIELD order.
4032 Usually we meet initializers in that order, but it is
4033 possible for base types to be placed not in program
4035 tree fields
= TYPE_FIELDS (DECL_CONTEXT (index
));
4036 unsigned HOST_WIDE_INT idx
= 0;
4037 constructor_elt
*cep
= NULL
;
4039 /* Check if we're changing the active member of a union. */
4040 if (TREE_CODE (type
) == UNION_TYPE
&& CONSTRUCTOR_NELTS (ctor
)
4041 && CONSTRUCTOR_ELT (ctor
, 0)->index
!= index
)
4042 vec_safe_truncate (CONSTRUCTOR_ELTS (ctor
), 0);
4043 /* If the bit offset of INDEX is larger than that of the last
4044 constructor_elt, then we can just immediately append a new
4045 constructor_elt to the end of CTOR. */
4046 else if (CONSTRUCTOR_NELTS (ctor
)
4047 && tree_int_cst_compare (bit_position (index
),
4048 bit_position (CONSTRUCTOR_ELTS (ctor
)
4049 ->last().index
)) > 0)
4051 idx
= CONSTRUCTOR_NELTS (ctor
);
4055 /* Otherwise, we need to iterate over CTOR to find or insert INDEX
4058 for (; vec_safe_iterate (CONSTRUCTOR_ELTS (ctor
), idx
, &cep
);
4059 idx
++, fields
= DECL_CHAIN (fields
))
4061 if (index
== cep
->index
)
4064 /* The field we're initializing must be on the field
4065 list. Look to see if it is present before the
4066 field the current ELT initializes. */
4067 for (; fields
!= cep
->index
; fields
= DECL_CHAIN (fields
))
4068 if (index
== fields
)
4071 /* We fell off the end of the CONSTRUCTOR, so insert a new
4072 entry at the end. */
4076 constructor_elt ce
= { index
, NULL_TREE
};
4078 vec_safe_insert (CONSTRUCTOR_ELTS (ctor
), idx
, ce
);
4079 cep
= CONSTRUCTOR_ELT (ctor
, idx
);
4087 /* Under the control of CTX, issue a detailed diagnostic for
4088 an out-of-bounds subscript INDEX into the expression ARRAY. */
4091 diag_array_subscript (location_t loc
, const constexpr_ctx
*ctx
, tree array
, tree index
)
4095 tree arraytype
= TREE_TYPE (array
);
4097 /* Convert the unsigned array subscript to a signed integer to avoid
4098 printing huge numbers for small negative values. */
4099 tree sidx
= fold_convert (ssizetype
, index
);
4100 STRIP_ANY_LOCATION_WRAPPER (array
);
4103 if (TYPE_DOMAIN (arraytype
))
4104 error_at (loc
, "array subscript value %qE is outside the bounds "
4105 "of array %qD of type %qT", sidx
, array
, arraytype
);
4107 error_at (loc
, "nonzero array subscript %qE is used with array %qD of "
4108 "type %qT with unknown bounds", sidx
, array
, arraytype
);
4109 inform (DECL_SOURCE_LOCATION (array
), "declared here");
4111 else if (TYPE_DOMAIN (arraytype
))
4112 error_at (loc
, "array subscript value %qE is outside the bounds "
4113 "of array type %qT", sidx
, arraytype
);
4115 error_at (loc
, "nonzero array subscript %qE is used with array of type %qT "
4116 "with unknown bounds", sidx
, arraytype
);
4120 /* Return the number of elements for TYPE (which is an ARRAY_TYPE or
4124 get_array_or_vector_nelts (const constexpr_ctx
*ctx
, tree type
,
4125 bool *non_constant_p
, bool *overflow_p
)
4128 if (TREE_CODE (type
) == ARRAY_TYPE
)
4130 if (TYPE_DOMAIN (type
))
4131 nelts
= array_type_nelts_top (type
);
4133 nelts
= size_zero_node
;
4135 else if (VECTOR_TYPE_P (type
))
4136 nelts
= size_int (TYPE_VECTOR_SUBPARTS (type
));
4140 /* For VLAs, the number of elements won't be an integer constant. */
4141 nelts
= cxx_eval_constant_expression (ctx
, nelts
, vc_prvalue
,
4142 non_constant_p
, overflow_p
);
4146 /* Extract element INDEX consisting of CHARS_PER_ELT chars from
4147 STRING_CST STRING. */
4150 extract_string_elt (tree string
, unsigned chars_per_elt
, unsigned index
)
4152 tree type
= cv_unqualified (TREE_TYPE (TREE_TYPE (string
)));
4155 if (chars_per_elt
== 1)
4156 r
= build_int_cst (type
, TREE_STRING_POINTER (string
)[index
]);
4159 const unsigned char *ptr
4160 = ((const unsigned char *)TREE_STRING_POINTER (string
)
4161 + index
* chars_per_elt
);
4162 r
= native_interpret_expr (type
, ptr
, chars_per_elt
);
4167 /* Subroutine of cxx_eval_array_reference. T is an ARRAY_REF; evaluate the
4168 subscript, diagnose any problems with it, and return the result. */
4171 eval_and_check_array_index (const constexpr_ctx
*ctx
,
4172 tree t
, bool allow_one_past
,
4173 bool *non_constant_p
, bool *overflow_p
)
4175 location_t loc
= cp_expr_loc_or_input_loc (t
);
4176 tree ary
= TREE_OPERAND (t
, 0);
4177 t
= TREE_OPERAND (t
, 1);
4178 tree index
= cxx_eval_constant_expression (ctx
, t
, vc_prvalue
,
4179 non_constant_p
, overflow_p
);
4180 VERIFY_CONSTANT (index
);
4182 if (!tree_fits_shwi_p (index
)
4183 || tree_int_cst_sgn (index
) < 0)
4185 diag_array_subscript (loc
, ctx
, ary
, index
);
4186 *non_constant_p
= true;
4190 tree nelts
= get_array_or_vector_nelts (ctx
, TREE_TYPE (ary
), non_constant_p
,
4192 VERIFY_CONSTANT (nelts
);
4194 ? !tree_int_cst_le (index
, nelts
)
4195 : !tree_int_cst_lt (index
, nelts
))
4197 diag_array_subscript (loc
, ctx
, ary
, index
);
4198 *non_constant_p
= true;
4205 /* Subroutine of cxx_eval_constant_expression.
4206 Attempt to reduce a reference to an array slot. */
4209 cxx_eval_array_reference (const constexpr_ctx
*ctx
, tree t
,
4211 bool *non_constant_p
, bool *overflow_p
)
4213 tree oldary
= TREE_OPERAND (t
, 0);
4214 tree ary
= cxx_eval_constant_expression (ctx
, oldary
,
4216 non_constant_p
, overflow_p
);
4217 if (*non_constant_p
)
4220 && TREE_CODE (ary
) == VIEW_CONVERT_EXPR
4221 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary
, 0)))
4222 && TREE_TYPE (t
) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary
, 0))))
4223 ary
= TREE_OPERAND (ary
, 0);
4225 tree oldidx
= TREE_OPERAND (t
, 1);
4226 tree index
= eval_and_check_array_index (ctx
, t
, lval
,
4227 non_constant_p
, overflow_p
);
4228 if (*non_constant_p
)
4231 if (lval
&& ary
== oldary
&& index
== oldidx
)
4233 else if (lval
== vc_discard
)
4236 return build4 (ARRAY_REF
, TREE_TYPE (t
), ary
, index
, NULL
, NULL
);
4238 unsigned len
= 0, elem_nchars
= 1;
4239 tree elem_type
= TREE_TYPE (TREE_TYPE (ary
));
4240 if (TREE_CODE (ary
) == CONSTRUCTOR
)
4241 len
= CONSTRUCTOR_NELTS (ary
);
4242 else if (TREE_CODE (ary
) == STRING_CST
)
4244 elem_nchars
= (TYPE_PRECISION (elem_type
)
4245 / TYPE_PRECISION (char_type_node
));
4246 len
= (unsigned) TREE_STRING_LENGTH (ary
) / elem_nchars
;
4248 else if (TREE_CODE (ary
) == VECTOR_CST
)
4249 /* We don't create variable-length VECTOR_CSTs. */
4250 len
= VECTOR_CST_NELTS (ary
).to_constant ();
4253 /* We can't do anything with other tree codes, so use
4254 VERIFY_CONSTANT to complain and fail. */
4255 VERIFY_CONSTANT (ary
);
4260 HOST_WIDE_INT i
= 0;
4261 if (TREE_CODE (ary
) == CONSTRUCTOR
)
4263 HOST_WIDE_INT ix
= find_array_ctor_elt (ary
, index
);
4270 i
= tree_to_shwi (index
);
4277 if (TREE_CODE (ary
) == CONSTRUCTOR
)
4278 r
= (*CONSTRUCTOR_ELTS (ary
))[i
].value
;
4279 else if (TREE_CODE (ary
) == VECTOR_CST
)
4280 r
= VECTOR_CST_ELT (ary
, i
);
4282 r
= extract_string_elt (ary
, elem_nchars
, i
);
4285 /* Don't VERIFY_CONSTANT here. */
4288 /* Otherwise the element doesn't have a value yet. */
4293 if (TREE_CODE (ary
) == CONSTRUCTOR
4294 && CONSTRUCTOR_NO_CLEARING (ary
))
4296 /* 'ary' is part of the aggregate initializer we're currently
4297 building; if there's no initializer for this element yet,
4300 error ("accessing uninitialized array element");
4301 *non_constant_p
= true;
4305 /* If it's within the array bounds but doesn't have an explicit
4306 initializer, it's initialized from {}. But use build_value_init
4307 directly for non-aggregates to avoid creating a garbage CONSTRUCTOR. */
4309 constexpr_ctx new_ctx
;
4310 if (is_really_empty_class (elem_type
, /*ignore_vptr*/false))
4311 return build_constructor (elem_type
, NULL
);
4312 else if (CP_AGGREGATE_TYPE_P (elem_type
))
4314 tree empty_ctor
= build_constructor (init_list_type_node
, NULL
);
4315 val
= digest_init (elem_type
, empty_ctor
, tf_warning_or_error
);
4318 val
= build_value_init (elem_type
, tf_warning_or_error
);
4320 /* Create a new constructor only if we don't already have a suitable one. */
4321 const bool new_ctor
= (!SCALAR_TYPE_P (elem_type
)
4323 || !same_type_ignoring_top_level_qualifiers_p
4324 (elem_type
, TREE_TYPE (ctx
->ctor
))));
4328 /* We clear the object here. We used to replace it with T, but that
4329 caused problems (101371, 108158); and anyway, T is the initializer,
4330 not the target object. */
4331 new_ctx
.object
= NULL_TREE
;
4332 new_ctx
.ctor
= build_constructor (elem_type
, NULL
);
4335 t
= cxx_eval_constant_expression (ctx
, val
, lval
, non_constant_p
,
4337 if (new_ctor
&& t
!= ctx
->ctor
)
4338 free_constructor (ctx
->ctor
);
4342 /* Subroutine of cxx_eval_constant_expression.
4343 Attempt to reduce a field access of a value of class type. */
4346 cxx_eval_component_reference (const constexpr_ctx
*ctx
, tree t
,
4348 bool *non_constant_p
, bool *overflow_p
)
4350 unsigned HOST_WIDE_INT i
;
4353 tree part
= TREE_OPERAND (t
, 1);
4354 tree orig_whole
= TREE_OPERAND (t
, 0);
4355 tree whole
= cxx_eval_constant_expression (ctx
, orig_whole
,
4357 non_constant_p
, overflow_p
);
4358 if (*non_constant_p
)
4360 if (INDIRECT_REF_P (whole
)
4361 && integer_zerop (TREE_OPERAND (whole
, 0)))
4364 error ("dereferencing a null pointer in %qE", orig_whole
);
4365 *non_constant_p
= true;
4369 if (TREE_CODE (whole
) == PTRMEM_CST
)
4370 whole
= cplus_expand_constant (whole
);
4371 if (whole
== orig_whole
)
4373 if (lval
== vc_discard
)
4376 return fold_build3 (COMPONENT_REF
, TREE_TYPE (t
),
4377 whole
, part
, NULL_TREE
);
4378 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
4380 if (TREE_CODE (whole
) != CONSTRUCTOR
)
4383 error ("%qE is not a constant expression", orig_whole
);
4384 *non_constant_p
= true;
4387 if ((cxx_dialect
< cxx14
|| CONSTRUCTOR_MUTABLE_POISON (whole
))
4388 && DECL_MUTABLE_P (part
))
4391 error ("mutable %qD is not usable in a constant expression", part
);
4392 *non_constant_p
= true;
4395 bool pmf
= TYPE_PTRMEMFUNC_P (TREE_TYPE (whole
));
4396 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole
), i
, field
, value
)
4398 /* Use name match for PMF fields, as a variant will have a
4399 different FIELD_DECL with a different type. */
4400 if (pmf
? DECL_NAME (field
) == DECL_NAME (part
)
4405 STRIP_ANY_LOCATION_WRAPPER (value
);
4409 /* We're in the middle of initializing it. */
4413 if (TREE_CODE (TREE_TYPE (whole
)) == UNION_TYPE
4414 && CONSTRUCTOR_NELTS (whole
) > 0)
4416 /* DR 1188 says we don't have to deal with this. */
4419 constructor_elt
*cep
= CONSTRUCTOR_ELT (whole
, 0);
4420 if (cep
->value
== NULL_TREE
)
4421 error ("accessing uninitialized member %qD", part
);
4423 error ("accessing %qD member instead of initialized %qD member in "
4424 "constant expression", part
, cep
->index
);
4426 *non_constant_p
= true;
4430 /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty
4431 classes never get represented; throw together a value now. */
4432 if (is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
4433 return build_constructor (TREE_TYPE (t
), NULL
);
4435 gcc_assert (DECL_CONTEXT (part
) == TYPE_MAIN_VARIANT (TREE_TYPE (whole
)));
4437 if (CONSTRUCTOR_NO_CLEARING (whole
))
4439 /* 'whole' is part of the aggregate initializer we're currently
4440 building; if there's no initializer for this member yet, that's an
4443 error ("accessing uninitialized member %qD", part
);
4444 *non_constant_p
= true;
4448 /* If there's no explicit init for this field, it's value-initialized. */
4449 value
= build_value_init (TREE_TYPE (t
), tf_warning_or_error
);
4450 return cxx_eval_constant_expression (ctx
, value
,
4452 non_constant_p
, overflow_p
);
4455 /* Subroutine of cxx_eval_constant_expression.
4456 Attempt to reduce a field access of a value of class type that is
4457 expressed as a BIT_FIELD_REF. */
4460 cxx_eval_bit_field_ref (const constexpr_ctx
*ctx
, tree t
,
4462 bool *non_constant_p
, bool *overflow_p
)
4464 tree orig_whole
= TREE_OPERAND (t
, 0);
4465 tree retval
, fldval
, utype
, mask
;
4466 bool fld_seen
= false;
4467 HOST_WIDE_INT istart
, isize
;
4468 tree whole
= cxx_eval_constant_expression (ctx
, orig_whole
,
4470 non_constant_p
, overflow_p
);
4471 tree start
, field
, value
;
4472 unsigned HOST_WIDE_INT i
;
4474 if (whole
== orig_whole
)
4476 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
4478 if (!*non_constant_p
4479 && TREE_CODE (whole
) != VECTOR_CST
4480 && TREE_CODE (whole
) != CONSTRUCTOR
)
4483 error ("%qE is not a constant expression", orig_whole
);
4484 *non_constant_p
= true;
4486 if (*non_constant_p
)
4489 if (TREE_CODE (whole
) == VECTOR_CST
|| !INTEGRAL_TYPE_P (TREE_TYPE (t
)))
4491 if (tree r
= fold_ternary (BIT_FIELD_REF
, TREE_TYPE (t
), whole
,
4492 TREE_OPERAND (t
, 1), TREE_OPERAND (t
, 2)))
4495 error ("%qE is not a constant expression", orig_whole
);
4496 *non_constant_p
= true;
4500 start
= TREE_OPERAND (t
, 2);
4501 istart
= tree_to_shwi (start
);
4502 isize
= tree_to_shwi (TREE_OPERAND (t
, 1));
4503 utype
= TREE_TYPE (t
);
4504 if (!TYPE_UNSIGNED (utype
))
4505 utype
= build_nonstandard_integer_type (TYPE_PRECISION (utype
), 1);
4506 retval
= build_int_cst (utype
, 0);
4507 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole
), i
, field
, value
)
4509 tree bitpos
= bit_position (field
);
4510 STRIP_ANY_LOCATION_WRAPPER (value
);
4511 if (bitpos
== start
&& DECL_SIZE (field
) == TREE_OPERAND (t
, 1))
4513 if (TREE_CODE (TREE_TYPE (field
)) == INTEGER_TYPE
4514 && TREE_CODE (value
) == INTEGER_CST
4515 && tree_fits_shwi_p (bitpos
)
4516 && tree_fits_shwi_p (DECL_SIZE (field
)))
4518 HOST_WIDE_INT bit
= tree_to_shwi (bitpos
);
4519 HOST_WIDE_INT sz
= tree_to_shwi (DECL_SIZE (field
));
4520 HOST_WIDE_INT shift
;
4521 if (bit
>= istart
&& bit
+ sz
<= istart
+ isize
)
4523 fldval
= fold_convert (utype
, value
);
4524 mask
= build_int_cst_type (utype
, -1);
4525 mask
= fold_build2 (LSHIFT_EXPR
, utype
, mask
,
4526 size_int (TYPE_PRECISION (utype
) - sz
));
4527 mask
= fold_build2 (RSHIFT_EXPR
, utype
, mask
,
4528 size_int (TYPE_PRECISION (utype
) - sz
));
4529 fldval
= fold_build2 (BIT_AND_EXPR
, utype
, fldval
, mask
);
4530 shift
= bit
- istart
;
4531 if (BYTES_BIG_ENDIAN
)
4532 shift
= TYPE_PRECISION (utype
) - shift
- sz
;
4533 fldval
= fold_build2 (LSHIFT_EXPR
, utype
, fldval
,
4535 retval
= fold_build2 (BIT_IOR_EXPR
, utype
, retval
, fldval
);
4541 return fold_convert (TREE_TYPE (t
), retval
);
4543 return error_mark_node
;
4546 /* Helper for cxx_eval_bit_cast.
4547 Check [bit.cast]/3 rules, bit_cast is constexpr only if the To and From
4548 types and types of all subobjects have is_union_v<T>, is_pointer_v<T>,
4549 is_member_pointer_v<T>, is_volatile_v<T> false and has no non-static
4550 data members of reference type. */
4553 check_bit_cast_type (const constexpr_ctx
*ctx
, location_t loc
, tree type
,
4556 if (TREE_CODE (type
) == UNION_TYPE
)
4560 if (type
== orig_type
)
4561 error_at (loc
, "%qs is not a constant expression because %qT is "
4562 "a union type", "__builtin_bit_cast", type
);
4564 error_at (loc
, "%qs is not a constant expression because %qT "
4565 "contains a union type", "__builtin_bit_cast",
4570 if (TREE_CODE (type
) == POINTER_TYPE
)
4574 if (type
== orig_type
)
4575 error_at (loc
, "%qs is not a constant expression because %qT is "
4576 "a pointer type", "__builtin_bit_cast", type
);
4578 error_at (loc
, "%qs is not a constant expression because %qT "
4579 "contains a pointer type", "__builtin_bit_cast",
4584 if (TREE_CODE (type
) == REFERENCE_TYPE
)
4588 if (type
== orig_type
)
4589 error_at (loc
, "%qs is not a constant expression because %qT is "
4590 "a reference type", "__builtin_bit_cast", type
);
4592 error_at (loc
, "%qs is not a constant expression because %qT "
4593 "contains a reference type", "__builtin_bit_cast",
4598 if (TYPE_PTRMEM_P (type
))
4602 if (type
== orig_type
)
4603 error_at (loc
, "%qs is not a constant expression because %qT is "
4604 "a pointer to member type", "__builtin_bit_cast",
4607 error_at (loc
, "%qs is not a constant expression because %qT "
4608 "contains a pointer to member type",
4609 "__builtin_bit_cast", orig_type
);
4613 if (TYPE_VOLATILE (type
))
4617 if (type
== orig_type
)
4618 error_at (loc
, "%qs is not a constant expression because %qT is "
4619 "volatile", "__builtin_bit_cast", type
);
4621 error_at (loc
, "%qs is not a constant expression because %qT "
4622 "contains a volatile subobject",
4623 "__builtin_bit_cast", orig_type
);
4627 if (TREE_CODE (type
) == RECORD_TYPE
)
4628 for (tree field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
4629 if (TREE_CODE (field
) == FIELD_DECL
4630 && check_bit_cast_type (ctx
, loc
, TREE_TYPE (field
), orig_type
))
4635 /* Helper function for cxx_eval_bit_cast. For unsigned char or
4636 std::byte members of CONSTRUCTOR (recursively) if they contain
4637 some indeterminate bits (as set in MASK), remove the ctor elts,
4638 mark the CONSTRUCTOR as CONSTRUCTOR_NO_CLEARING and clear the
4642 clear_uchar_or_std_byte_in_mask (location_t loc
, tree t
, unsigned char *mask
)
4644 if (TREE_CODE (t
) != CONSTRUCTOR
)
4649 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t
), i
, index
, value
)
4651 tree type
= TREE_TYPE (value
);
4652 if (TREE_CODE (TREE_TYPE (t
)) != ARRAY_TYPE
4653 && DECL_BIT_FIELD_TYPE (index
) != NULL_TREE
)
4655 if (is_byte_access_type_not_plain_char (DECL_BIT_FIELD_TYPE (index
)))
4657 HOST_WIDE_INT fldsz
= TYPE_PRECISION (TREE_TYPE (index
));
4658 gcc_assert (fldsz
!= 0);
4659 HOST_WIDE_INT pos
= int_byte_position (index
);
4661 = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (index
));
4662 bpos
%= BITS_PER_UNIT
;
4664 = ROUND_UP (bpos
+ fldsz
, BITS_PER_UNIT
) / BITS_PER_UNIT
;
4665 gcc_assert (end
== 1 || end
== 2);
4666 unsigned char *p
= mask
+ pos
;
4667 unsigned char mask_save
[2];
4668 mask_save
[0] = mask
[pos
];
4669 mask_save
[1] = end
== 2 ? mask
[pos
+ 1] : 0;
4670 if (BYTES_BIG_ENDIAN
!= WORDS_BIG_ENDIAN
)
4671 sorry_at (loc
, "PDP11 bit-field handling unsupported"
4672 " in %qs", "__builtin_bit_cast");
4673 else if (BYTES_BIG_ENDIAN
)
4676 if (bpos
+ fldsz
<= BITS_PER_UNIT
)
4677 *p
&= ~(((1 << fldsz
) - 1)
4678 << (BITS_PER_UNIT
- bpos
- fldsz
));
4682 *p
&= ~(((1U << BITS_PER_UNIT
) - 1) >> bpos
);
4684 fldsz
-= BITS_PER_UNIT
- bpos
;
4685 gcc_assert (fldsz
&& fldsz
< BITS_PER_UNIT
);
4686 *p
&= ((1U << BITS_PER_UNIT
) - 1) >> fldsz
;
4691 /* Little endian. */
4692 if (bpos
+ fldsz
<= BITS_PER_UNIT
)
4693 *p
&= ~(((1 << fldsz
) - 1) << bpos
);
4697 *p
&= ~(((1 << BITS_PER_UNIT
) - 1) << bpos
);
4699 fldsz
-= BITS_PER_UNIT
- bpos
;
4700 gcc_assert (fldsz
&& fldsz
< BITS_PER_UNIT
);
4701 *p
&= ~((1 << fldsz
) - 1);
4704 if (mask_save
[0] != mask
[pos
]
4705 || (end
== 2 && mask_save
[1] != mask
[pos
+ 1]))
4707 CONSTRUCTOR_NO_CLEARING (t
) = 1;
4712 else if (is_byte_access_type_not_plain_char (type
))
4715 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
4716 pos
= tree_to_shwi (index
);
4718 pos
= int_byte_position (index
);
4721 CONSTRUCTOR_NO_CLEARING (t
) = 1;
4726 if (TREE_CODE (value
) == CONSTRUCTOR
)
4729 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
4730 pos
= tree_to_shwi (index
)
4731 * tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t
))));
4733 pos
= int_byte_position (index
);
4734 clear_uchar_or_std_byte_in_mask (loc
, value
, mask
+ pos
);
4738 CONSTRUCTOR_ELT (t
, j
)->index
= index
;
4739 CONSTRUCTOR_ELT (t
, j
)->value
= value
;
4743 if (CONSTRUCTOR_NELTS (t
) != j
)
4744 vec_safe_truncate (CONSTRUCTOR_ELTS (t
), j
);
4747 /* Subroutine of cxx_eval_constant_expression.
4748 Attempt to evaluate a BIT_CAST_EXPR. */
4751 cxx_eval_bit_cast (const constexpr_ctx
*ctx
, tree t
, bool *non_constant_p
,
4754 if (check_bit_cast_type (ctx
, EXPR_LOCATION (t
), TREE_TYPE (t
),
4756 || check_bit_cast_type (ctx
, cp_expr_loc_or_loc (TREE_OPERAND (t
, 0),
4758 TREE_TYPE (TREE_OPERAND (t
, 0)),
4759 TREE_TYPE (TREE_OPERAND (t
, 0))))
4761 *non_constant_p
= true;
4765 tree op
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), vc_prvalue
,
4766 non_constant_p
, overflow_p
);
4767 if (*non_constant_p
)
4770 location_t loc
= EXPR_LOCATION (t
);
4771 if (BITS_PER_UNIT
!= 8 || CHAR_BIT
!= 8)
4774 sorry_at (loc
, "%qs cannot be constant evaluated on the target",
4775 "__builtin_bit_cast");
4776 *non_constant_p
= true;
4780 if (!tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (t
))))
4783 sorry_at (loc
, "%qs cannot be constant evaluated because the "
4784 "type is too large", "__builtin_bit_cast");
4785 *non_constant_p
= true;
4789 HOST_WIDE_INT len
= tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (t
)));
4790 if (len
< 0 || (int) len
!= len
)
4793 sorry_at (loc
, "%qs cannot be constant evaluated because the "
4794 "type is too large", "__builtin_bit_cast");
4795 *non_constant_p
= true;
4799 unsigned char buf
[64];
4800 unsigned char *ptr
, *mask
;
4801 size_t alen
= (size_t) len
* 2;
4802 if (alen
<= sizeof (buf
))
4805 ptr
= XNEWVEC (unsigned char, alen
);
4806 mask
= ptr
+ (size_t) len
;
4807 /* At the beginning consider everything indeterminate. */
4808 memset (mask
, ~0, (size_t) len
);
4810 if (native_encode_initializer (op
, ptr
, len
, 0, mask
) != len
)
4813 sorry_at (loc
, "%qs cannot be constant evaluated because the "
4814 "argument cannot be encoded", "__builtin_bit_cast");
4815 *non_constant_p
= true;
4822 if (can_native_interpret_type_p (TREE_TYPE (t
)))
4824 r
= native_interpret_expr (TREE_TYPE (t
), ptr
, len
);
4825 if (is_byte_access_type_not_plain_char (TREE_TYPE (t
)))
4827 gcc_assert (len
== 1);
4830 memset (mask
, 0, len
);
4831 r
= build_constructor (TREE_TYPE (r
), NULL
);
4832 CONSTRUCTOR_NO_CLEARING (r
) = 1;
4836 else if (TREE_CODE (TREE_TYPE (t
)) == RECORD_TYPE
)
4838 r
= native_interpret_aggregate (TREE_TYPE (t
), ptr
, 0, len
);
4841 clear_type_padding_in_mask (TREE_TYPE (t
), mask
);
4842 clear_uchar_or_std_byte_in_mask (loc
, r
, mask
);
4845 tree e
= cxx_eval_bare_aggregate (ctx
, r
, vc_prvalue
,
4846 non_constant_p
, overflow_p
);
4847 gcc_checking_assert (e
== r
);
4855 for (int i
= 0; i
< len
; i
++)
4859 error_at (loc
, "%qs accessing uninitialized byte at offset %d",
4860 "__builtin_bit_cast", i
);
4861 *non_constant_p
= true;
4871 sorry_at (loc
, "%qs cannot be constant evaluated because the "
4872 "argument cannot be interpreted", "__builtin_bit_cast");
4873 *non_constant_p
= true;
4879 /* Subroutine of cxx_eval_constant_expression.
4880 Evaluate a short-circuited logical expression T in the context
4881 of a given constexpr CALL. BAILOUT_VALUE is the value for
4882 early return. CONTINUE_VALUE is used here purely for
4883 sanity check purposes. */
4886 cxx_eval_logical_expression (const constexpr_ctx
*ctx
, tree t
,
4887 tree bailout_value
, tree continue_value
,
4888 bool *non_constant_p
, bool *overflow_p
)
4891 tree lhs
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
4892 vc_prvalue
, non_constant_p
,
4894 VERIFY_CONSTANT (lhs
);
4895 if (tree_int_cst_equal (lhs
, bailout_value
))
4897 gcc_assert (tree_int_cst_equal (lhs
, continue_value
));
4898 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
4899 vc_prvalue
, non_constant_p
,
4901 VERIFY_CONSTANT (r
);
4905 /* REF is a COMPONENT_REF designating a particular field. V is a vector of
4906 CONSTRUCTOR elements to initialize (part of) an object containing that
4907 field. Return a pointer to the constructor_elt corresponding to the
4908 initialization of the field. */
4910 static constructor_elt
*
4911 base_field_constructor_elt (vec
<constructor_elt
, va_gc
> *v
, tree ref
)
4913 tree aggr
= TREE_OPERAND (ref
, 0);
4914 tree field
= TREE_OPERAND (ref
, 1);
4916 constructor_elt
*ce
;
4918 gcc_assert (TREE_CODE (ref
) == COMPONENT_REF
);
4920 if (TREE_CODE (aggr
) == COMPONENT_REF
)
4922 constructor_elt
*base_ce
4923 = base_field_constructor_elt (v
, aggr
);
4924 v
= CONSTRUCTOR_ELTS (base_ce
->value
);
4927 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
4928 if (ce
->index
== field
)
4935 /* Some of the expressions fed to the constexpr mechanism are calls to
4936 constructors, which have type void. In that case, return the type being
4937 initialized by the constructor. */
4940 initialized_type (tree t
)
4944 tree type
= TREE_TYPE (t
);
4945 if (TREE_CODE (t
) == CALL_EXPR
)
4947 /* A constructor call has void type, so we need to look deeper. */
4948 tree fn
= get_function_named_in_call (t
);
4949 if (fn
&& TREE_CODE (fn
) == FUNCTION_DECL
4950 && DECL_CXX_CONSTRUCTOR_P (fn
))
4951 type
= DECL_CONTEXT (fn
);
4953 else if (TREE_CODE (t
) == COMPOUND_EXPR
)
4954 return initialized_type (TREE_OPERAND (t
, 1));
4955 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
)
4956 type
= TREE_TYPE (AGGR_INIT_EXPR_SLOT (t
));
4957 return cv_unqualified (type
);
4960 /* We're about to initialize element INDEX of an array or class from VALUE.
4961 Set up NEW_CTX appropriately by adjusting .object to refer to the
4962 subobject and creating a new CONSTRUCTOR if the element is itself
4963 a class or array. */
4966 init_subob_ctx (const constexpr_ctx
*ctx
, constexpr_ctx
&new_ctx
,
4967 tree index
, tree
&value
)
4971 if (index
&& TREE_CODE (index
) != INTEGER_CST
4972 && TREE_CODE (index
) != FIELD_DECL
4973 && TREE_CODE (index
) != RANGE_EXPR
)
4974 /* This won't have an element in the new CONSTRUCTOR. */
4977 tree type
= initialized_type (value
);
4978 if (!AGGREGATE_TYPE_P (type
) && !VECTOR_TYPE_P (type
))
4979 /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
4981 if (VECTOR_TYPE_P (type
)
4982 && VECTOR_TYPE_P (TREE_TYPE (ctx
->ctor
))
4983 && index
== NULL_TREE
)
4984 /* A vector inside of a vector CONSTRUCTOR, e.g. when a larger
4985 vector is constructed from smaller vectors, doesn't get its own
4986 CONSTRUCTOR either. */
4989 /* The sub-aggregate initializer might contain a placeholder;
4990 update object to refer to the subobject and ctor to refer to
4991 the (newly created) sub-initializer. */
4994 if (index
== NULL_TREE
|| TREE_CODE (index
) == RANGE_EXPR
)
4995 /* There's no well-defined subobject for this index. */
4996 new_ctx
.object
= NULL_TREE
;
4998 new_ctx
.object
= build_ctor_subob_ref (index
, type
, ctx
->object
);
5001 if (is_empty_class (type
))
5002 /* Leave ctor null for an empty subobject, they aren't represented in the
5003 result of evaluation. */
5004 new_ctx
.ctor
= NULL_TREE
;
5007 tree elt
= build_constructor (type
, NULL
);
5008 CONSTRUCTOR_NO_CLEARING (elt
) = true;
5012 if (TREE_CODE (value
) == TARGET_EXPR
)
5013 /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
5014 value
= TARGET_EXPR_INITIAL (value
);
5017 /* We're about to process an initializer for a class or array TYPE. Make
5018 sure that CTX is set up appropriately. */
5021 verify_ctor_sanity (const constexpr_ctx
*ctx
, tree type
)
5023 /* We don't bother building a ctor for an empty base subobject. */
5024 if (is_empty_class (type
))
5027 /* We're in the middle of an initializer that might involve placeholders;
5028 our caller should have created a CONSTRUCTOR for us to put the
5029 initializer into. We will either return that constructor or T. */
5030 gcc_assert (ctx
->ctor
);
5031 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5032 (type
, TREE_TYPE (ctx
->ctor
)));
5033 /* We used to check that ctx->ctor was empty, but that isn't the case when
5034 the object is zero-initialized before calling the constructor. */
5037 tree otype
= TREE_TYPE (ctx
->object
);
5038 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type
, otype
)
5039 /* Handle flexible array members. */
5040 || (TREE_CODE (otype
) == ARRAY_TYPE
5041 && TYPE_DOMAIN (otype
) == NULL_TREE
5042 && TREE_CODE (type
) == ARRAY_TYPE
5043 && (same_type_ignoring_top_level_qualifiers_p
5044 (TREE_TYPE (type
), TREE_TYPE (otype
)))));
5046 gcc_assert (!ctx
->object
|| !DECL_P (ctx
->object
)
5047 || ctx
->global
->get_value (ctx
->object
) == ctx
->ctor
);
5050 /* Subroutine of cxx_eval_constant_expression.
5051 The expression tree T denotes a C-style array or a C-style
5052 aggregate. Reduce it to a constant expression. */
5055 cxx_eval_bare_aggregate (const constexpr_ctx
*ctx
, tree t
,
5057 bool *non_constant_p
, bool *overflow_p
)
5059 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (t
);
5060 bool changed
= false;
5061 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t
));
5062 tree type
= TREE_TYPE (t
);
5064 constexpr_ctx new_ctx
;
5065 if (TYPE_PTRMEMFUNC_P (type
) || VECTOR_TYPE_P (type
))
5067 /* We don't really need the ctx->ctor business for a PMF or
5068 vector, but it's simpler to use the same code. */
5070 new_ctx
.ctor
= build_constructor (type
, NULL
);
5071 new_ctx
.object
= NULL_TREE
;
5074 verify_ctor_sanity (ctx
, type
);
5075 vec
<constructor_elt
, va_gc
> **p
= nullptr;
5078 p
= &CONSTRUCTOR_ELTS (ctx
->ctor
);
5079 vec_alloc (*p
, vec_safe_length (v
));
5080 if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (t
))
5081 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx
->ctor
) = 1;
5086 bool constant_p
= true;
5087 bool side_effects_p
= false;
5088 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, index
, value
)
5090 tree orig_value
= value
;
5091 /* Like in cxx_eval_store_expression, omit entries for empty fields. */
5092 bool no_slot
= TREE_CODE (type
) == RECORD_TYPE
&& is_empty_field (index
);
5093 init_subob_ctx (ctx
, new_ctx
, index
, value
);
5095 if (new_ctx
.ctor
!= ctx
->ctor
&& !no_slot
)
5097 /* If we built a new CONSTRUCTOR, attach it now so that other
5098 initializers can refer to it. */
5099 constructor_elt
*cep
= get_or_insert_ctor_field (ctx
->ctor
, index
);
5100 cep
->value
= new_ctx
.ctor
;
5101 pos_hint
= cep
- (*p
)->begin();
5103 else if (TREE_CODE (type
) == UNION_TYPE
)
5104 /* Otherwise if we're constructing a non-aggregate union member, set
5105 the active union member now so that we can later detect and diagnose
5106 if its initializer attempts to activate another member. */
5107 get_or_insert_ctor_field (ctx
->ctor
, index
);
5108 tree elt
= cxx_eval_constant_expression (&new_ctx
, value
,
5110 non_constant_p
, overflow_p
);
5111 /* Don't VERIFY_CONSTANT here. */
5112 if (ctx
->quiet
&& *non_constant_p
)
5114 if (elt
!= orig_value
)
5117 if (!TREE_CONSTANT (elt
))
5119 if (TREE_SIDE_EFFECTS (elt
))
5120 side_effects_p
= true;
5121 if (index
&& TREE_CODE (index
) == COMPONENT_REF
)
5123 /* This is an initialization of a vfield inside a base
5124 subaggregate that we already initialized; push this
5125 initialization into the previous initialization. */
5126 constructor_elt
*inner
= base_field_constructor_elt (*p
, index
);
5131 /* This is an initializer for an empty field; now that we've
5132 checked that it's constant, we can ignore it. */
5135 && (TREE_CODE (index
) == NOP_EXPR
5136 || TREE_CODE (index
) == POINTER_PLUS_EXPR
))
5138 /* Old representation of empty bases. FIXME remove. */
5139 gcc_checking_assert (false);
5140 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index
))));
5145 if (TREE_CODE (type
) == UNION_TYPE
5146 && (*p
)->last().index
!= index
)
5147 /* The initializer erroneously changed the active union member that
5148 we're initializing. */
5149 gcc_assert (*non_constant_p
);
5152 /* The initializer might have mutated the underlying CONSTRUCTOR,
5153 so recompute the location of the target constructer_elt. */
5154 constructor_elt
*cep
5155 = get_or_insert_ctor_field (ctx
->ctor
, index
, pos_hint
);
5159 /* Adding or replacing an element might change the ctor's flags. */
5160 TREE_CONSTANT (ctx
->ctor
) = constant_p
;
5161 TREE_SIDE_EFFECTS (ctx
->ctor
) = side_effects_p
;
5164 if (*non_constant_p
)
5168 if (VECTOR_TYPE_P (type
))
5174 t
= build_constructor (type
, NULL
);
5175 /* We're done building this CONSTRUCTOR, so now we can interpret an
5176 element without an explicit initializer as value-initialized. */
5177 CONSTRUCTOR_NO_CLEARING (t
) = false;
5178 TREE_CONSTANT (t
) = constant_p
;
5179 TREE_SIDE_EFFECTS (t
) = side_effects_p
;
5180 if (VECTOR_TYPE_P (type
))
5185 /* Subroutine of cxx_eval_constant_expression.
5186 The expression tree T is a VEC_INIT_EXPR which denotes the desired
5187 initialization of a non-static data member of array type. Reduce it to a
5190 Note that apart from value-initialization (when VALUE_INIT is true),
5191 this is only intended to support value-initialization and the
5192 initializations done by defaulted constructors for classes with
5193 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
5194 will either be NULL_TREE for the default constructor, or a COMPONENT_REF
5195 for the copy/move constructor. */
5198 cxx_eval_vec_init_1 (const constexpr_ctx
*ctx
, tree atype
, tree init
,
5199 bool value_init
, value_cat lval
,
5200 bool *non_constant_p
, bool *overflow_p
)
5202 tree elttype
= TREE_TYPE (atype
);
5203 verify_ctor_sanity (ctx
, atype
);
5204 vec
<constructor_elt
, va_gc
> **p
= &CONSTRUCTOR_ELTS (ctx
->ctor
);
5205 bool pre_init
= false;
5206 unsigned HOST_WIDE_INT i
;
5207 tsubst_flags_t complain
= ctx
->quiet
? tf_none
: tf_warning_or_error
;
5209 if (init
&& TREE_CODE (init
) == CONSTRUCTOR
)
5210 return cxx_eval_bare_aggregate (ctx
, init
, lval
,
5211 non_constant_p
, overflow_p
);
5213 /* For the default constructor, build up a call to the default
5214 constructor of the element type. We only need to handle class types
5215 here, as for a constructor to be constexpr, all members must be
5216 initialized, which for a defaulted default constructor means they must
5217 be of a class type with a constexpr default constructor. */
5218 if (TREE_CODE (elttype
) == ARRAY_TYPE
)
5219 /* We only do this at the lowest level. */;
5220 else if (value_init
)
5222 init
= build_value_init (elttype
, complain
);
5227 releasing_vec argvec
;
5228 init
= build_special_member_call (NULL_TREE
, complete_ctor_identifier
,
5229 &argvec
, elttype
, LOOKUP_NORMAL
,
5231 init
= build_aggr_init_expr (elttype
, init
);
5235 bool zeroed_out
= false;
5236 if (!CONSTRUCTOR_NO_CLEARING (ctx
->ctor
))
5238 /* We're initializing an array object that had been zero-initialized
5239 earlier. Truncate ctx->ctor, and propagate its zeroed state by
5240 clearing CONSTRUCTOR_NO_CLEARING on each of the aggregate element
5241 initializers we append to it. */
5242 gcc_checking_assert (initializer_zerop (ctx
->ctor
));
5244 vec_safe_truncate (*p
, 0);
5247 tree nelts
= get_array_or_vector_nelts (ctx
, atype
, non_constant_p
,
5249 unsigned HOST_WIDE_INT max
= tree_to_uhwi (nelts
);
5250 for (i
= 0; i
< max
; ++i
)
5252 tree idx
= build_int_cst (size_type_node
, i
);
5255 constexpr_ctx new_ctx
;
5256 init_subob_ctx (ctx
, new_ctx
, idx
, pre_init
? init
: elttype
);
5257 if (new_ctx
.ctor
!= ctx
->ctor
)
5260 CONSTRUCTOR_NO_CLEARING (new_ctx
.ctor
) = false;
5261 CONSTRUCTOR_APPEND_ELT (*p
, idx
, new_ctx
.ctor
);
5263 if (TREE_CODE (elttype
) == ARRAY_TYPE
)
5265 /* A multidimensional array; recurse. */
5266 if (value_init
|| init
== NULL_TREE
)
5268 eltinit
= NULL_TREE
;
5272 eltinit
= cp_build_array_ref (input_location
, init
, idx
, complain
);
5273 eltinit
= cxx_eval_vec_init_1 (&new_ctx
, elttype
, eltinit
, value_init
,
5275 non_constant_p
, overflow_p
);
5279 /* Initializing an element using value or default initialization
5280 we just pre-built above. */
5281 if (init
== void_node
)
5282 /* Trivial default-init, don't do anything to the CONSTRUCTOR. */
5284 eltinit
= cxx_eval_constant_expression (&new_ctx
, init
, lval
,
5285 non_constant_p
, overflow_p
);
5290 /* Copying an element. */
5291 eltinit
= cp_build_array_ref (input_location
, init
, idx
, complain
);
5292 if (!lvalue_p (init
))
5293 eltinit
= move (eltinit
);
5294 eltinit
= (perform_implicit_conversion_flags
5295 (elttype
, eltinit
, complain
,
5296 LOOKUP_IMPLICIT
|LOOKUP_NO_NARROWING
));
5297 eltinit
= cxx_eval_constant_expression (&new_ctx
, eltinit
, lval
,
5298 non_constant_p
, overflow_p
);
5300 if (*non_constant_p
)
5302 if (new_ctx
.ctor
!= ctx
->ctor
)
5304 /* We appended this element above; update the value. */
5305 gcc_assert ((*p
)->last().index
== idx
);
5306 (*p
)->last().value
= eltinit
;
5309 CONSTRUCTOR_APPEND_ELT (*p
, idx
, eltinit
);
5310 /* Reuse the result of cxx_eval_constant_expression call
5311 from the first iteration to all others if it is a constant
5312 initializer that doesn't require relocations. */
5315 && (eltinit
== NULL_TREE
5316 || (initializer_constant_valid_p (eltinit
, TREE_TYPE (eltinit
))
5317 == null_pointer_node
)))
5319 if (new_ctx
.ctor
!= ctx
->ctor
)
5320 eltinit
= new_ctx
.ctor
;
5321 tree range
= build2 (RANGE_EXPR
, size_type_node
,
5322 build_int_cst (size_type_node
, 1),
5323 build_int_cst (size_type_node
, max
- 1));
5324 CONSTRUCTOR_APPEND_ELT (*p
, range
, unshare_constructor (eltinit
));
5328 vec_safe_reserve (*p
, max
);
5331 if (!*non_constant_p
)
5334 CONSTRUCTOR_NO_CLEARING (init
) = false;
5340 cxx_eval_vec_init (const constexpr_ctx
*ctx
, tree t
,
5342 bool *non_constant_p
, bool *overflow_p
)
5344 tree atype
= TREE_TYPE (t
);
5345 tree init
= VEC_INIT_EXPR_INIT (t
);
5346 bool value_init
= VEC_INIT_EXPR_VALUE_INIT (t
);
5347 if (!init
|| !BRACE_ENCLOSED_INITIALIZER_P (init
))
5349 else if (CONSTRUCTOR_NELTS (init
) == 0
5350 && !CP_AGGREGATE_TYPE_P (strip_array_types (atype
)))
5352 /* Handle {} as value-init. */
5358 /* This is a more complicated case, like needing to loop over trailing
5359 elements; call build_vec_init and evaluate the result. */
5360 tsubst_flags_t complain
= ctx
->quiet
? tf_none
: tf_warning_or_error
;
5361 constexpr_ctx new_ctx
= *ctx
;
5364 /* We want to have an initialization target for an VEC_INIT_EXPR.
5365 If we don't already have one in CTX, use the VEC_INIT_EXPR_SLOT. */
5366 new_ctx
.object
= VEC_INIT_EXPR_SLOT (t
);
5367 tree ctor
= new_ctx
.ctor
= build_constructor (atype
, NULL
);
5368 CONSTRUCTOR_NO_CLEARING (ctor
) = true;
5369 ctx
->global
->put_value (new_ctx
.object
, ctor
);
5372 init
= expand_vec_init_expr (ctx
->object
, t
, complain
);
5373 return cxx_eval_constant_expression (ctx
, init
, lval
, non_constant_p
,
5376 tree r
= cxx_eval_vec_init_1 (ctx
, atype
, init
, value_init
,
5377 lval
, non_constant_p
, overflow_p
);
5378 if (*non_constant_p
)
5384 /* Like same_type_ignoring_top_level_qualifiers_p, but also handle the case
5385 where the desired type is an array of unknown bounds because the variable
5386 has had its bounds deduced since the wrapping expression was created. */
5389 same_type_ignoring_tlq_and_bounds_p (tree type1
, tree type2
)
5391 while (TREE_CODE (type1
) == ARRAY_TYPE
5392 && TREE_CODE (type2
) == ARRAY_TYPE
5393 && (!TYPE_DOMAIN (type1
) || !TYPE_DOMAIN (type2
)))
5395 type1
= TREE_TYPE (type1
);
5396 type2
= TREE_TYPE (type2
);
5398 return same_type_ignoring_top_level_qualifiers_p (type1
, type2
);
5401 /* Try to determine the currently active union member for an expression
5402 with UNION_TYPE. If it can be determined, return the FIELD_DECL,
5403 otherwise return NULL_TREE. */
5406 cxx_union_active_member (const constexpr_ctx
*ctx
, tree t
)
5408 constexpr_ctx new_ctx
= *ctx
;
5409 new_ctx
.quiet
= true;
5410 bool non_constant_p
= false, overflow_p
= false;
5411 tree ctor
= cxx_eval_constant_expression (&new_ctx
, t
, vc_prvalue
,
5414 if (TREE_CODE (ctor
) == CONSTRUCTOR
5415 && CONSTRUCTOR_NELTS (ctor
) == 1
5416 && CONSTRUCTOR_ELT (ctor
, 0)->index
5417 && TREE_CODE (CONSTRUCTOR_ELT (ctor
, 0)->index
) == FIELD_DECL
)
5418 return CONSTRUCTOR_ELT (ctor
, 0)->index
;
5422 /* Helper function for cxx_fold_indirect_ref_1, called recursively. */
5425 cxx_fold_indirect_ref_1 (const constexpr_ctx
*ctx
, location_t loc
, tree type
,
5426 tree op
, unsigned HOST_WIDE_INT off
, bool *empty_base
)
5428 tree optype
= TREE_TYPE (op
);
5429 unsigned HOST_WIDE_INT const_nunits
;
5430 if (off
== 0 && similar_type_p (optype
, type
))
5432 else if (TREE_CODE (optype
) == COMPLEX_TYPE
5433 && similar_type_p (type
, TREE_TYPE (optype
)))
5435 /* *(foo *)&complexfoo => __real__ complexfoo */
5437 return build1_loc (loc
, REALPART_EXPR
, type
, op
);
5438 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
5439 else if (tree_to_uhwi (TYPE_SIZE_UNIT (type
)) == off
)
5440 return build1_loc (loc
, IMAGPART_EXPR
, type
, op
);
5442 /* ((foo*)&vectorfoo)[x] => BIT_FIELD_REF<vectorfoo,...> */
5443 else if (VECTOR_TYPE_P (optype
)
5444 && similar_type_p (type
, TREE_TYPE (optype
))
5445 && TYPE_VECTOR_SUBPARTS (optype
).is_constant (&const_nunits
))
5447 unsigned HOST_WIDE_INT part_width
= tree_to_uhwi (TYPE_SIZE_UNIT (type
));
5448 unsigned HOST_WIDE_INT max_offset
= part_width
* const_nunits
;
5449 if (off
< max_offset
&& off
% part_width
== 0)
5451 tree index
= bitsize_int (off
* BITS_PER_UNIT
);
5452 return build3_loc (loc
, BIT_FIELD_REF
, type
, op
,
5453 TYPE_SIZE (type
), index
);
5456 /* ((foo *)&fooarray)[x] => fooarray[x] */
5457 else if (TREE_CODE (optype
) == ARRAY_TYPE
5458 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (optype
)))
5459 && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (optype
))))
5461 tree type_domain
= TYPE_DOMAIN (optype
);
5462 tree min_val
= size_zero_node
;
5463 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
5464 min_val
= TYPE_MIN_VALUE (type_domain
);
5465 unsigned HOST_WIDE_INT el_sz
5466 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (optype
)));
5467 unsigned HOST_WIDE_INT idx
= off
/ el_sz
;
5468 unsigned HOST_WIDE_INT rem
= off
% el_sz
;
5469 if (tree_fits_uhwi_p (min_val
))
5471 tree index
= size_int (idx
+ tree_to_uhwi (min_val
));
5472 op
= build4_loc (loc
, ARRAY_REF
, TREE_TYPE (optype
), op
, index
,
5473 NULL_TREE
, NULL_TREE
);
5474 return cxx_fold_indirect_ref_1 (ctx
, loc
, type
, op
, rem
,
5478 /* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */
5479 else if (TREE_CODE (optype
) == RECORD_TYPE
5480 || TREE_CODE (optype
) == UNION_TYPE
)
5482 if (TREE_CODE (optype
) == UNION_TYPE
)
5483 /* For unions prefer the currently active member. */
5484 if (tree field
= cxx_union_active_member (ctx
, op
))
5486 unsigned HOST_WIDE_INT el_sz
5487 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field
)));
5490 tree cop
= build3 (COMPONENT_REF
, TREE_TYPE (field
),
5491 op
, field
, NULL_TREE
);
5492 if (tree ret
= cxx_fold_indirect_ref_1 (ctx
, loc
, type
, cop
,
5498 /* Handle conversion to an empty base class, which is represented with a
5499 NOP_EXPR. Do this before spelunking into the non-empty subobjects,
5500 which is likely to be a waste of time (109678). */
5501 if (is_empty_class (type
)
5502 && CLASS_TYPE_P (optype
)
5503 && lookup_base (optype
, type
, ba_any
, NULL
, tf_none
, off
))
5510 for (tree field
= TYPE_FIELDS (optype
);
5511 field
; field
= DECL_CHAIN (field
))
5512 if (TREE_CODE (field
) == FIELD_DECL
5513 && TREE_TYPE (field
) != error_mark_node
5514 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (field
))))
5516 tree pos
= byte_position (field
);
5517 if (!tree_fits_uhwi_p (pos
))
5519 unsigned HOST_WIDE_INT upos
= tree_to_uhwi (pos
);
5520 unsigned HOST_WIDE_INT el_sz
5521 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field
)));
5522 if (upos
<= off
&& off
< upos
+ el_sz
)
5524 tree cop
= build3 (COMPONENT_REF
, TREE_TYPE (field
),
5525 op
, field
, NULL_TREE
);
5526 if (tree ret
= cxx_fold_indirect_ref_1 (ctx
, loc
, type
, cop
,
5537 /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
5538 match. We want to be less strict for simple *& folding; if we have a
5539 non-const temporary that we access through a const pointer, that should
5540 work. We handle this here rather than change fold_indirect_ref_1
5541 because we're dealing with things like ADDR_EXPR of INTEGER_CST which
5542 don't really make sense outside of constant expression evaluation. Also
5543 we want to allow folding to COMPONENT_REF, which could cause trouble
5544 with TBAA in fold_indirect_ref_1. */
5547 cxx_fold_indirect_ref (const constexpr_ctx
*ctx
, location_t loc
, tree type
,
5548 tree op0
, bool *empty_base
/* = NULL*/)
5552 poly_uint64 const_op01
;
5554 /* STRIP_NOPS, but stop if REINTERPRET_CAST_P. */
5555 while (CONVERT_EXPR_P (sub
) || TREE_CODE (sub
) == NON_LVALUE_EXPR
5556 || TREE_CODE (sub
) == VIEW_CONVERT_EXPR
)
5558 if (TREE_CODE (sub
) == NOP_EXPR
5559 && REINTERPRET_CAST_P (sub
))
5561 sub
= TREE_OPERAND (sub
, 0);
5564 subtype
= TREE_TYPE (sub
);
5565 if (!INDIRECT_TYPE_P (subtype
))
5568 /* Canonicalizes the given OBJ/OFF pair by iteratively absorbing
5569 the innermost component into the offset until it would make the
5570 offset positive, so that cxx_fold_indirect_ref_1 can identify
5571 more folding opportunities. */
5572 auto canonicalize_obj_off
= [] (tree
& obj
, tree
& off
) {
5573 while (TREE_CODE (obj
) == COMPONENT_REF
5574 && (tree_int_cst_sign_bit (off
) || integer_zerop (off
)))
5576 tree field
= TREE_OPERAND (obj
, 1);
5577 tree pos
= byte_position (field
);
5578 if (integer_zerop (off
) && integer_nonzerop (pos
))
5579 /* If the offset is already 0, keep going as long as the
5580 component is at position 0. */
5582 off
= int_const_binop (PLUS_EXPR
, off
, pos
);
5583 obj
= TREE_OPERAND (obj
, 0);
5587 if (TREE_CODE (sub
) == ADDR_EXPR
)
5589 tree op
= TREE_OPERAND (sub
, 0);
5590 tree optype
= TREE_TYPE (op
);
5592 /* *&CONST_DECL -> to the value of the const decl. */
5593 if (TREE_CODE (op
) == CONST_DECL
)
5594 return DECL_INITIAL (op
);
5595 /* *&p => p; make sure to handle *&"str"[cst] here. */
5596 if (similar_type_p (optype
, type
))
5598 tree fop
= fold_read_from_constant_string (op
);
5606 tree off
= integer_zero_node
;
5607 canonicalize_obj_off (op
, off
);
5608 gcc_assert (integer_zerop (off
));
5609 return cxx_fold_indirect_ref_1 (ctx
, loc
, type
, op
, 0, empty_base
);
5612 else if (TREE_CODE (sub
) == POINTER_PLUS_EXPR
5613 && tree_fits_uhwi_p (TREE_OPERAND (sub
, 1)))
5615 tree op00
= TREE_OPERAND (sub
, 0);
5616 tree off
= TREE_OPERAND (sub
, 1);
5619 if (TREE_CODE (op00
) == ADDR_EXPR
)
5621 tree obj
= TREE_OPERAND (op00
, 0);
5622 canonicalize_obj_off (obj
, off
);
5623 return cxx_fold_indirect_ref_1 (ctx
, loc
, type
, obj
,
5624 tree_to_uhwi (off
), empty_base
);
5627 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
5628 else if (TREE_CODE (TREE_TYPE (subtype
)) == ARRAY_TYPE
5629 && similar_type_p (type
, TREE_TYPE (TREE_TYPE (subtype
))))
5632 tree min_val
= size_zero_node
;
5634 = cxx_fold_indirect_ref (ctx
, loc
, TREE_TYPE (subtype
), sub
, NULL
);
5638 sub
= build1_loc (loc
, INDIRECT_REF
, TREE_TYPE (subtype
), sub
);
5639 type_domain
= TYPE_DOMAIN (TREE_TYPE (sub
));
5640 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
5641 min_val
= TYPE_MIN_VALUE (type_domain
);
5642 return build4_loc (loc
, ARRAY_REF
, type
, sub
, min_val
, NULL_TREE
,
5650 cxx_eval_indirect_ref (const constexpr_ctx
*ctx
, tree t
,
5652 bool *non_constant_p
, bool *overflow_p
)
5654 tree orig_op0
= TREE_OPERAND (t
, 0);
5655 bool empty_base
= false;
5657 /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second
5658 operand is an integer-zero. Otherwise reject the MEM_REF for now. */
5660 if (TREE_CODE (t
) == MEM_REF
5661 && (!TREE_OPERAND (t
, 1) || !integer_zerop (TREE_OPERAND (t
, 1))))
5663 gcc_assert (ctx
->quiet
);
5664 *non_constant_p
= true;
5668 /* First try to simplify it directly. */
5669 tree r
= cxx_fold_indirect_ref (ctx
, EXPR_LOCATION (t
), TREE_TYPE (t
),
5670 orig_op0
, &empty_base
);
5673 /* If that didn't work, evaluate the operand first. */
5674 tree op0
= cxx_eval_constant_expression (ctx
, orig_op0
,
5675 vc_prvalue
, non_constant_p
,
5677 /* Don't VERIFY_CONSTANT here. */
5678 if (*non_constant_p
)
5681 if (!lval
&& integer_zerop (op0
))
5684 error ("dereferencing a null pointer");
5685 *non_constant_p
= true;
5689 r
= cxx_fold_indirect_ref (ctx
, EXPR_LOCATION (t
), TREE_TYPE (t
), op0
,
5693 /* We couldn't fold to a constant value. Make sure it's not
5694 something we should have been able to fold. */
5697 if (TREE_CODE (sub
) == ADDR_EXPR
)
5699 gcc_assert (!similar_type_p
5700 (TREE_TYPE (TREE_TYPE (sub
)), TREE_TYPE (t
)));
5701 /* DR 1188 says we don't have to deal with this. */
5703 error_at (cp_expr_loc_or_input_loc (t
),
5704 "accessing value of %qE through a %qT glvalue in a "
5705 "constant expression", build_fold_indirect_ref (sub
),
5707 *non_constant_p
= true;
5711 if (lval
== vc_glvalue
&& op0
!= orig_op0
)
5712 return build1 (INDIRECT_REF
, TREE_TYPE (t
), op0
);
5714 VERIFY_CONSTANT (t
);
5719 r
= cxx_eval_constant_expression (ctx
, r
,
5720 lval
, non_constant_p
, overflow_p
);
5721 if (*non_constant_p
)
5724 /* If we're pulling out the value of an empty base, just return an empty
5726 if (empty_base
&& !lval
)
5728 r
= build_constructor (TREE_TYPE (t
), NULL
);
5729 TREE_CONSTANT (r
) = true;
5735 /* Complain about R, a VAR_DECL, not being usable in a constant expression.
5736 FUNDEF_P is true if we're checking a constexpr function body.
5737 Shared between potential_constant_expression and
5738 cxx_eval_constant_expression. */
5741 non_const_var_error (location_t loc
, tree r
, bool fundef_p
)
5743 auto_diagnostic_group d
;
5744 tree type
= TREE_TYPE (r
);
5745 if (DECL_NAME (r
) == heap_uninit_identifier
5746 || DECL_NAME (r
) == heap_identifier
5747 || DECL_NAME (r
) == heap_vec_uninit_identifier
5748 || DECL_NAME (r
) == heap_vec_identifier
)
5750 if (constexpr_error (loc
, fundef_p
, "the content of uninitialized "
5751 "storage is not usable in a constant expression"))
5752 inform (DECL_SOURCE_LOCATION (r
), "allocated here");
5755 if (DECL_NAME (r
) == heap_deleted_identifier
)
5757 if (constexpr_error (loc
, fundef_p
, "use of allocated storage after "
5758 "deallocation in a constant expression"))
5759 inform (DECL_SOURCE_LOCATION (r
), "allocated here");
5762 if (!constexpr_error (loc
, fundef_p
, "the value of %qD is not usable in "
5763 "a constant expression", r
))
5765 /* Avoid error cascade. */
5766 if (DECL_INITIAL (r
) == error_mark_node
)
5768 if (DECL_DECLARED_CONSTEXPR_P (r
))
5769 inform (DECL_SOURCE_LOCATION (r
),
5770 "%qD used in its own initializer", r
);
5771 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5773 if (!CP_TYPE_CONST_P (type
))
5774 inform (DECL_SOURCE_LOCATION (r
),
5775 "%q#D is not const", r
);
5776 else if (CP_TYPE_VOLATILE_P (type
))
5777 inform (DECL_SOURCE_LOCATION (r
),
5778 "%q#D is volatile", r
);
5779 else if (!DECL_INITIAL (r
)
5780 || !TREE_CONSTANT (DECL_INITIAL (r
))
5781 || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
))
5782 inform (DECL_SOURCE_LOCATION (r
),
5783 "%qD was not initialized with a constant "
5788 else if (TYPE_REF_P (type
))
5789 inform (DECL_SOURCE_LOCATION (r
),
5790 "%qD was not initialized with a constant "
5794 if (cxx_dialect
>= cxx11
&& !DECL_DECLARED_CONSTEXPR_P (r
))
5795 inform (DECL_SOURCE_LOCATION (r
),
5796 "%qD was not declared %<constexpr%>", r
);
5798 inform (DECL_SOURCE_LOCATION (r
),
5799 "%qD does not have integral or enumeration type",
5804 /* Subroutine of cxx_eval_constant_expression.
5805 Like cxx_eval_unary_expression, except for trinary expressions. */
5808 cxx_eval_trinary_expression (const constexpr_ctx
*ctx
, tree t
,
5810 bool *non_constant_p
, bool *overflow_p
)
5816 for (i
= 0; i
< 3; i
++)
5818 args
[i
] = cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, i
),
5820 non_constant_p
, overflow_p
);
5821 VERIFY_CONSTANT (args
[i
]);
5824 val
= fold_ternary_loc (EXPR_LOCATION (t
), TREE_CODE (t
), TREE_TYPE (t
),
5825 args
[0], args
[1], args
[2]);
5826 if (val
== NULL_TREE
)
5828 VERIFY_CONSTANT (val
);
5832 /* True if T was declared in a function declared to be constexpr, and
5833 therefore potentially constant in C++14. */
5836 var_in_constexpr_fn (tree t
)
5838 tree ctx
= DECL_CONTEXT (t
);
5839 return (ctx
&& TREE_CODE (ctx
) == FUNCTION_DECL
5840 && DECL_DECLARED_CONSTEXPR_P (ctx
));
5843 /* True if a function might be constexpr: either a function that was
5844 declared constexpr, or a C++17 lambda op(). */
5847 maybe_constexpr_fn (tree t
)
5849 return (DECL_DECLARED_CONSTEXPR_P (t
)
5850 || (cxx_dialect
>= cxx17
&& LAMBDA_FUNCTION_P (t
))
5851 || (flag_implicit_constexpr
5852 && DECL_DECLARED_INLINE_P (STRIP_TEMPLATE (t
))));
5855 /* True if T was declared in a function that might be constexpr: either a
5856 function that was declared constexpr, or a C++17 lambda op(). */
5859 var_in_maybe_constexpr_fn (tree t
)
5861 return (DECL_FUNCTION_SCOPE_P (t
)
5862 && maybe_constexpr_fn (DECL_CONTEXT (t
)));
5865 /* We're assigning INIT to TARGET. In do_build_copy_constructor and
5866 build_over_call we implement trivial copy of a class with tail padding using
5867 assignment of character arrays, which is valid in normal code, but not in
5868 constexpr evaluation. We don't need to worry about clobbering tail padding
5869 in constexpr evaluation, so strip the type punning. */
5872 maybe_simplify_trivial_copy (tree
&target
, tree
&init
)
5874 if (TREE_CODE (target
) == MEM_REF
5875 && TREE_CODE (init
) == MEM_REF
5876 && TREE_TYPE (target
) == TREE_TYPE (init
)
5877 && TREE_CODE (TREE_TYPE (target
)) == ARRAY_TYPE
5878 && TREE_TYPE (TREE_TYPE (target
)) == unsigned_char_type_node
)
5880 target
= build_fold_indirect_ref (TREE_OPERAND (target
, 0));
5881 init
= build_fold_indirect_ref (TREE_OPERAND (init
, 0));
5885 /* Returns true if REF, which is a COMPONENT_REF, has any fields
5886 of constant type. This does not check for 'mutable', so the
5887 caller is expected to be mindful of that. */
5890 cref_has_const_field (tree ref
)
5892 while (TREE_CODE (ref
) == COMPONENT_REF
)
5894 if (CP_TYPE_CONST_P (TREE_TYPE (TREE_OPERAND (ref
, 1))))
5896 ref
= TREE_OPERAND (ref
, 0);
5901 /* Return true if we are modifying something that is const during constant
5902 expression evaluation. CODE is the code of the statement, OBJ is the
5903 object in question, MUTABLE_P is true if one of the subobjects were
5904 declared mutable. */
5907 modifying_const_object_p (tree_code code
, tree obj
, bool mutable_p
)
5909 /* If this is initialization, there's no problem. */
5910 if (code
!= MODIFY_EXPR
)
5913 /* [basic.type.qualifier] "A const object is an object of type
5914 const T or a non-mutable subobject of a const object." */
5918 if (TREE_READONLY (obj
))
5921 if (CP_TYPE_CONST_P (TREE_TYPE (obj
)))
5923 /* Although a COMPONENT_REF may have a const type, we should
5924 only consider it modifying a const object when any of the
5925 field components is const. This can happen when using
5926 constructs such as const_cast<const T &>(m), making something
5927 const even though it wasn't declared const. */
5928 if (TREE_CODE (obj
) == COMPONENT_REF
)
5929 return cref_has_const_field (obj
);
5937 /* Evaluate an INIT_EXPR or MODIFY_EXPR. */
5940 cxx_eval_store_expression (const constexpr_ctx
*ctx
, tree t
,
5942 bool *non_constant_p
, bool *overflow_p
)
5944 constexpr_ctx new_ctx
= *ctx
;
5946 tree init
= TREE_OPERAND (t
, 1);
5947 if (TREE_CLOBBER_P (init
))
5948 /* Just ignore clobbers. */
5951 /* First we figure out where we're storing to. */
5952 tree target
= TREE_OPERAND (t
, 0);
5954 maybe_simplify_trivial_copy (target
, init
);
5956 tree type
= TREE_TYPE (target
);
5957 bool preeval
= SCALAR_TYPE_P (type
) || TREE_CODE (t
) == MODIFY_EXPR
;
5960 /* Evaluate the value to be stored without knowing what object it will be
5961 stored in, so that any side-effects happen first. */
5962 if (!SCALAR_TYPE_P (type
))
5963 new_ctx
.ctor
= new_ctx
.object
= NULL_TREE
;
5964 init
= cxx_eval_constant_expression (&new_ctx
, init
, vc_prvalue
,
5965 non_constant_p
, overflow_p
);
5966 if (*non_constant_p
)
5970 bool evaluated
= false;
5971 if (lval
== vc_glvalue
)
5973 /* If we want to return a reference to the target, we need to evaluate it
5974 as a whole; otherwise, only evaluate the innermost piece to avoid
5975 building up unnecessary *_REFs. */
5976 target
= cxx_eval_constant_expression (ctx
, target
, lval
,
5977 non_constant_p
, overflow_p
);
5979 if (*non_constant_p
)
5983 /* Find the underlying variable. */
5985 tree object
= NULL_TREE
;
5986 /* If we're modifying a const object, save it. */
5987 tree const_object_being_modified
= NULL_TREE
;
5988 bool mutable_p
= false;
5989 for (tree probe
= target
; object
== NULL_TREE
; )
5991 switch (TREE_CODE (probe
))
5997 tree ob
= TREE_OPERAND (probe
, 0);
5998 tree elt
= TREE_OPERAND (probe
, 1);
5999 if (TREE_CODE (elt
) == FIELD_DECL
&& DECL_MUTABLE_P (elt
))
6001 if (TREE_CODE (probe
) == ARRAY_REF
)
6003 elt
= eval_and_check_array_index (ctx
, probe
, false,
6004 non_constant_p
, overflow_p
);
6005 if (*non_constant_p
)
6008 /* We don't check modifying_const_object_p for ARRAY_REFs. Given
6009 "int a[10]", an ARRAY_REF "a[2]" can be "const int", even though
6010 the array isn't const. Instead, check "a" in the next iteration;
6011 that will detect modifying "const int a[10]". */
6013 && modifying_const_object_p (TREE_CODE (t
), probe
,
6015 && const_object_being_modified
== NULL_TREE
)
6016 const_object_being_modified
= probe
;
6017 vec_safe_push (refs
, elt
);
6018 vec_safe_push (refs
, TREE_TYPE (probe
));
6024 gcc_assert (probe
== target
);
6025 vec_safe_push (refs
, probe
);
6026 vec_safe_push (refs
, TREE_TYPE (probe
));
6027 probe
= TREE_OPERAND (probe
, 0);
6031 gcc_assert (probe
== target
);
6032 vec_safe_push (refs
, probe
);
6033 vec_safe_push (refs
, TREE_TYPE (probe
));
6034 probe
= TREE_OPERAND (probe
, 0);
6042 probe
= cxx_eval_constant_expression (ctx
, probe
, vc_glvalue
,
6043 non_constant_p
, overflow_p
);
6045 if (*non_constant_p
)
6052 if (modifying_const_object_p (TREE_CODE (t
), object
, mutable_p
)
6053 && const_object_being_modified
== NULL_TREE
)
6054 const_object_being_modified
= object
;
6056 /* And then find/build up our initializer for the path to the subobject
6057 we're initializing. */
6059 if (DECL_P (object
))
6060 valp
= ctx
->global
->get_value_ptr (object
);
6065 /* A constant-expression cannot modify objects from outside the
6066 constant-expression. */
6068 error ("modification of %qE is not a constant expression", object
);
6069 *non_constant_p
= true;
6072 type
= TREE_TYPE (object
);
6073 bool no_zero_init
= true;
6075 auto_vec
<tree
*> ctors
;
6076 releasing_vec indexes
;
6077 auto_vec
<int> index_pos_hints
;
6078 bool activated_union_member_p
= false;
6079 bool empty_base
= false;
6080 while (!refs
->is_empty ())
6082 if (*valp
== NULL_TREE
)
6084 *valp
= build_constructor (type
, NULL
);
6085 CONSTRUCTOR_NO_CLEARING (*valp
) = no_zero_init
;
6087 else if (STRIP_ANY_LOCATION_WRAPPER (*valp
),
6088 TREE_CODE (*valp
) == STRING_CST
)
6090 /* An array was initialized with a string constant, and now
6091 we're writing into one of its elements. Explode the
6092 single initialization into a set of element
6094 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
6096 tree string
= *valp
;
6097 tree elt_type
= TREE_TYPE (type
);
6098 unsigned chars_per_elt
= (TYPE_PRECISION (elt_type
)
6099 / TYPE_PRECISION (char_type_node
));
6100 unsigned num_elts
= TREE_STRING_LENGTH (string
) / chars_per_elt
;
6101 tree ary_ctor
= build_constructor (type
, NULL
);
6103 vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor
), num_elts
);
6104 for (unsigned ix
= 0; ix
!= num_elts
; ix
++)
6106 constructor_elt elt
=
6108 build_int_cst (size_type_node
, ix
),
6109 extract_string_elt (string
, chars_per_elt
, ix
)
6111 CONSTRUCTOR_ELTS (ary_ctor
)->quick_push (elt
);
6117 enum tree_code code
= TREE_CODE (type
);
6118 tree reftype
= refs
->pop();
6119 tree index
= refs
->pop();
6121 if (code
== COMPLEX_TYPE
)
6123 if (TREE_CODE (*valp
) == COMPLEX_CST
)
6124 *valp
= build2 (COMPLEX_EXPR
, type
, TREE_REALPART (*valp
),
6125 TREE_IMAGPART (*valp
));
6126 else if (TREE_CODE (*valp
) == CONSTRUCTOR
6127 && CONSTRUCTOR_NELTS (*valp
) == 0
6128 && CONSTRUCTOR_NO_CLEARING (*valp
))
6130 tree r
= build_constructor (reftype
, NULL
);
6131 CONSTRUCTOR_NO_CLEARING (r
) = 1;
6132 *valp
= build2 (COMPLEX_EXPR
, type
, r
, r
);
6134 gcc_assert (TREE_CODE (*valp
) == COMPLEX_EXPR
);
6135 ctors
.safe_push (valp
);
6136 vec_safe_push (indexes
, index
);
6137 valp
= &TREE_OPERAND (*valp
, TREE_CODE (index
) == IMAGPART_EXPR
);
6138 gcc_checking_assert (refs
->is_empty ());
6143 /* If the value of object is already zero-initialized, any new ctors for
6144 subobjects will also be zero-initialized. */
6145 no_zero_init
= CONSTRUCTOR_NO_CLEARING (*valp
);
6147 if (code
== RECORD_TYPE
&& is_empty_field (index
))
6148 /* Don't build a sub-CONSTRUCTOR for an empty base or field, as they
6149 have no data and might have an offset lower than previously declared
6150 fields, which confuses the middle-end. The code below will notice
6151 that we don't have a CONSTRUCTOR for our inner target and just
6160 if (code
== UNION_TYPE
&& CONSTRUCTOR_NELTS (*valp
)
6161 && CONSTRUCTOR_ELT (*valp
, 0)->index
!= index
)
6163 if (cxx_dialect
< cxx20
)
6166 error_at (cp_expr_loc_or_input_loc (t
),
6167 "change of the active member of a union "
6169 CONSTRUCTOR_ELT (*valp
, 0)->index
,
6171 *non_constant_p
= true;
6173 else if (TREE_CODE (t
) == MODIFY_EXPR
6174 && CONSTRUCTOR_NO_CLEARING (*valp
))
6176 /* Diagnose changing the active union member while the union
6177 is in the process of being initialized. */
6179 error_at (cp_expr_loc_or_input_loc (t
),
6180 "change of the active member of a union "
6181 "from %qD to %qD during initialization",
6182 CONSTRUCTOR_ELT (*valp
, 0)->index
,
6184 *non_constant_p
= true;
6186 no_zero_init
= true;
6189 ctors
.safe_push (valp
);
6190 vec_safe_push (indexes
, index
);
6192 constructor_elt
*cep
6193 = get_or_insert_ctor_field (*valp
, index
);
6194 index_pos_hints
.safe_push (cep
- CONSTRUCTOR_ELTS (*valp
)->begin());
6196 if (code
== UNION_TYPE
)
6197 activated_union_member_p
= true;
6202 /* For initialization of an empty base, the original target will be
6203 *(base*)this, evaluation of which resolves to the object
6204 argument, which has the derived type rather than the base type. */
6205 if (!empty_base
&& !(same_type_ignoring_top_level_qualifiers_p
6206 (initialized_type (init
), type
)))
6208 gcc_assert (is_empty_class (TREE_TYPE (target
)));
6212 /* Detect modifying a constant object in constexpr evaluation.
6213 We have found a const object that is being modified. Figure out
6214 if we need to issue an error. Consider
6218 constexpr A() : n(1) { n = 2; } // #1
6222 constexpr B() { a.n = 3; } // #2
6226 #1 is OK, since we're modifying an object under construction, but
6227 #2 is wrong, since "a" is const and has been fully constructed.
6228 To track it, we use the TREE_READONLY bit in the object's CONSTRUCTOR
6229 which means that the object is read-only. For the example above, the
6230 *ctors stack at the point of #2 will look like:
6232 ctors[0] = {.a={.n=2}} TREE_READONLY = 0
6233 ctors[1] = {.n=2} TREE_READONLY = 1
6235 and we're modifying "b.a", so we search the stack and see if the
6236 constructor for "b.a" has already run. */
6237 if (const_object_being_modified
)
6241 = strip_array_types (TREE_TYPE (const_object_being_modified
));
6242 if (!CLASS_TYPE_P (const_objtype
))
6246 /* [class.ctor]p5 "A constructor can be invoked for a const,
6247 volatile, or const volatile object. const and volatile
6248 semantics are not applied on an object under construction.
6249 They come into effect when the constructor for the most
6250 derived object ends." */
6251 for (tree
*elt
: ctors
)
6252 if (same_type_ignoring_top_level_qualifiers_p
6253 (TREE_TYPE (const_object_being_modified
), TREE_TYPE (*elt
)))
6255 fail
= TREE_READONLY (*elt
);
6262 modifying_const_object_error (t
, const_object_being_modified
);
6263 *non_constant_p
= true;
6270 /* We're handling an INIT_EXPR of class type, so the value of the
6271 initializer can depend on the object it's initializing. */
6273 /* Create a new CONSTRUCTOR in case evaluation of the initializer
6274 wants to modify it. */
6275 if (*valp
== NULL_TREE
)
6277 *valp
= build_constructor (type
, NULL
);
6278 CONSTRUCTOR_NO_CLEARING (*valp
) = no_zero_init
;
6280 new_ctx
.ctor
= empty_base
? NULL_TREE
: *valp
;
6281 new_ctx
.object
= target
;
6282 /* Avoid temporary materialization when initializing from a TARGET_EXPR.
6283 We don't need to mess with AGGR_EXPR_SLOT/VEC_INIT_EXPR_SLOT because
6284 expansion of those trees uses ctx instead. */
6285 if (TREE_CODE (init
) == TARGET_EXPR
)
6286 if (tree tinit
= TARGET_EXPR_INITIAL (init
))
6288 init
= cxx_eval_constant_expression (&new_ctx
, init
, vc_prvalue
,
6289 non_constant_p
, overflow_p
);
6290 /* The hash table might have moved since the get earlier, and the
6291 initializer might have mutated the underlying CONSTRUCTORs, so we must
6293 valp
= ctx
->global
->get_value_ptr (object
);
6294 for (unsigned i
= 0; i
< vec_safe_length (indexes
); i
++)
6297 constructor_elt
*cep
6298 = get_or_insert_ctor_field (*valp
, indexes
[i
], index_pos_hints
[i
]);
6303 if (*non_constant_p
)
6306 /* Don't share a CONSTRUCTOR that might be changed later. */
6307 init
= unshare_constructor (init
);
6309 gcc_checking_assert (!*valp
|| (same_type_ignoring_top_level_qualifiers_p
6310 (TREE_TYPE (*valp
), type
)));
6313 /* Just evaluate the initializer and return, since there's no actual data
6314 to store, and we didn't build a CONSTRUCTOR. */
6317 /* But do make sure we have something in *valp. */
6318 *valp
= build_constructor (type
, nullptr);
6319 CONSTRUCTOR_NO_CLEARING (*valp
) = no_zero_init
;
6322 else if (*valp
&& TREE_CODE (*valp
) == CONSTRUCTOR
6323 && TREE_CODE (init
) == CONSTRUCTOR
)
6325 /* An outer ctx->ctor might be pointing to *valp, so replace
6327 CONSTRUCTOR_ELTS (*valp
) = CONSTRUCTOR_ELTS (init
);
6328 TREE_CONSTANT (*valp
) = TREE_CONSTANT (init
);
6329 TREE_SIDE_EFFECTS (*valp
) = TREE_SIDE_EFFECTS (init
);
6330 CONSTRUCTOR_NO_CLEARING (*valp
)
6331 = CONSTRUCTOR_NO_CLEARING (init
);
6336 /* After initialization, 'const' semantics apply to the value of the
6337 object. Make a note of this fact by marking the CONSTRUCTOR
6339 if (TREE_CODE (t
) == INIT_EXPR
6340 && TREE_CODE (*valp
) == CONSTRUCTOR
6341 && TYPE_READONLY (type
))
6343 if (INDIRECT_REF_P (target
)
6344 && (is_this_parameter
6345 (tree_strip_nop_conversions (TREE_OPERAND (target
, 0)))))
6346 /* We've just initialized '*this' (perhaps via the target
6347 constructor of a delegating constructor). Leave it up to the
6348 caller that set 'this' to set TREE_READONLY appropriately. */
6349 gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
6350 (TREE_TYPE (target
), type
) || empty_base
);
6352 TREE_READONLY (*valp
) = true;
6355 /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing
6356 CONSTRUCTORs, if any. */
6357 bool c
= TREE_CONSTANT (init
);
6358 bool s
= TREE_SIDE_EFFECTS (init
);
6359 if (!indexes
->is_empty ())
6361 tree last
= indexes
->last ();
6362 if (TREE_CODE (last
) == REALPART_EXPR
6363 || TREE_CODE (last
) == IMAGPART_EXPR
)
6365 /* And canonicalize COMPLEX_EXPR into COMPLEX_CST if
6367 tree
*cexpr
= ctors
.last ();
6368 if (tree c
= const_binop (COMPLEX_EXPR
, TREE_TYPE (*cexpr
),
6369 TREE_OPERAND (*cexpr
, 0),
6370 TREE_OPERAND (*cexpr
, 1)))
6374 TREE_CONSTANT (*cexpr
)
6375 = (TREE_CONSTANT (TREE_OPERAND (*cexpr
, 0))
6376 & TREE_CONSTANT (TREE_OPERAND (*cexpr
, 1)));
6377 TREE_SIDE_EFFECTS (*cexpr
)
6378 = (TREE_SIDE_EFFECTS (TREE_OPERAND (*cexpr
, 0))
6379 | TREE_SIDE_EFFECTS (TREE_OPERAND (*cexpr
, 1)));
6381 c
= TREE_CONSTANT (*cexpr
);
6382 s
= TREE_SIDE_EFFECTS (*cexpr
);
6385 if (!c
|| s
|| activated_union_member_p
)
6386 for (tree
*elt
: ctors
)
6388 if (TREE_CODE (*elt
) != CONSTRUCTOR
)
6391 TREE_CONSTANT (*elt
) = false;
6393 TREE_SIDE_EFFECTS (*elt
) = true;
6394 /* Clear CONSTRUCTOR_NO_CLEARING since we've activated a member of
6396 if (TREE_CODE (TREE_TYPE (*elt
)) == UNION_TYPE
)
6397 CONSTRUCTOR_NO_CLEARING (*elt
) = false;
6406 /* Evaluate a ++ or -- expression. */
6409 cxx_eval_increment_expression (const constexpr_ctx
*ctx
, tree t
,
6411 bool *non_constant_p
, bool *overflow_p
)
6413 enum tree_code code
= TREE_CODE (t
);
6414 tree type
= TREE_TYPE (t
);
6415 tree op
= TREE_OPERAND (t
, 0);
6416 tree offset
= TREE_OPERAND (t
, 1);
6417 gcc_assert (TREE_CONSTANT (offset
));
6419 /* OFFSET is constant, but perhaps not constant enough. We need to
6420 e.g. bash FLOAT_EXPRs to REAL_CSTs. */
6421 offset
= fold_simple (offset
);
6423 /* The operand as an lvalue. */
6424 op
= cxx_eval_constant_expression (ctx
, op
, vc_glvalue
,
6425 non_constant_p
, overflow_p
);
6427 /* The operand as an rvalue. */
6429 = cxx_eval_constant_expression (ctx
, op
, vc_prvalue
,
6430 non_constant_p
, overflow_p
);
6431 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
6432 a local array in a constexpr function. */
6433 bool ptr
= INDIRECT_TYPE_P (TREE_TYPE (val
));
6435 VERIFY_CONSTANT (val
);
6437 /* The modified value. */
6438 bool inc
= (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
);
6440 if (INDIRECT_TYPE_P (type
))
6442 /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
6443 offset
= convert_to_ptrofftype (offset
);
6445 offset
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (offset
), offset
);
6446 mod
= fold_build2 (POINTER_PLUS_EXPR
, type
, val
, offset
);
6448 else if (c_promoting_integer_type_p (type
)
6449 && !TYPE_UNSIGNED (type
)
6450 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
6452 offset
= fold_convert (integer_type_node
, offset
);
6453 mod
= fold_convert (integer_type_node
, val
);
6454 tree t
= fold_build2 (inc
? PLUS_EXPR
: MINUS_EXPR
, integer_type_node
,
6456 mod
= fold_convert (type
, t
);
6457 if (TREE_OVERFLOW_P (mod
) && !TREE_OVERFLOW_P (t
))
6458 TREE_OVERFLOW (mod
) = false;
6461 mod
= fold_build2 (inc
? PLUS_EXPR
: MINUS_EXPR
, type
, val
, offset
);
6463 VERIFY_CONSTANT (mod
);
6465 /* Storing the modified value. */
6466 tree store
= build2_loc (cp_expr_loc_or_loc (t
, input_location
),
6467 MODIFY_EXPR
, type
, op
, mod
);
6468 mod
= cxx_eval_constant_expression (ctx
, store
, lval
,
6469 non_constant_p
, overflow_p
);
6471 if (*non_constant_p
)
6474 /* And the value of the expression. */
6475 if (code
== PREINCREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
6476 /* Prefix ops are lvalues, but the caller might want an rvalue;
6477 lval has already been taken into account in the store above. */
6480 /* Postfix ops are rvalues. */
6484 /* Predicates for the meaning of *jump_target. */
6487 returns (tree
*jump_target
)
6490 && TREE_CODE (*jump_target
) == RETURN_EXPR
;
6494 breaks (tree
*jump_target
)
6497 && ((TREE_CODE (*jump_target
) == LABEL_DECL
6498 && LABEL_DECL_BREAK (*jump_target
))
6499 || TREE_CODE (*jump_target
) == BREAK_STMT
6500 || TREE_CODE (*jump_target
) == EXIT_EXPR
);
6504 continues (tree
*jump_target
)
6507 && ((TREE_CODE (*jump_target
) == LABEL_DECL
6508 && LABEL_DECL_CONTINUE (*jump_target
))
6509 || TREE_CODE (*jump_target
) == CONTINUE_STMT
);
6514 switches (tree
*jump_target
)
6517 && TREE_CODE (*jump_target
) == INTEGER_CST
;
6520 /* Subroutine of cxx_eval_statement_list. Determine whether the statement
6521 STMT matches *jump_target. If we're looking for a case label and we see
6522 the default label, note it in ctx->css_state. */
6525 label_matches (const constexpr_ctx
*ctx
, tree
*jump_target
, tree stmt
)
6527 switch (TREE_CODE (*jump_target
))
6530 if (TREE_CODE (stmt
) == LABEL_EXPR
6531 && LABEL_EXPR_LABEL (stmt
) == *jump_target
)
6536 if (TREE_CODE (stmt
) == CASE_LABEL_EXPR
)
6538 gcc_assert (ctx
->css_state
!= NULL
);
6539 if (!CASE_LOW (stmt
))
6541 /* default: should appear just once in a SWITCH_EXPR
6542 body (excluding nested SWITCH_EXPR). */
6543 gcc_assert (*ctx
->css_state
!= css_default_seen
);
6544 /* When evaluating SWITCH_EXPR body for the second time,
6545 return true for the default: label. */
6546 if (*ctx
->css_state
== css_default_processing
)
6548 *ctx
->css_state
= css_default_seen
;
6550 else if (CASE_HIGH (stmt
))
6552 if (tree_int_cst_le (CASE_LOW (stmt
), *jump_target
)
6553 && tree_int_cst_le (*jump_target
, CASE_HIGH (stmt
)))
6556 else if (tree_int_cst_equal (*jump_target
, CASE_LOW (stmt
)))
6563 /* These two are handled directly in cxx_eval_loop_expr by testing
6564 breaks (jump_target) or continues (jump_target). */
6573 /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
6574 semantics, for switch, break, continue, and return. */
6577 cxx_eval_statement_list (const constexpr_ctx
*ctx
, tree t
,
6578 bool *non_constant_p
, bool *overflow_p
,
6582 /* In a statement-expression we want to return the last value.
6583 For empty statement expression return void_node. */
6587 local_target
= NULL_TREE
;
6588 jump_target
= &local_target
;
6590 for (tree_stmt_iterator i
= tsi_start (t
); !tsi_end_p (i
); ++i
)
6594 /* We've found a continue, so skip everything until we reach
6595 the label its jumping to. */
6596 if (continues (jump_target
))
6598 if (label_matches (ctx
, jump_target
, stmt
))
6600 *jump_target
= NULL_TREE
;
6604 if (TREE_CODE (stmt
) == DEBUG_BEGIN_STMT
)
6607 value_cat lval
= vc_discard
;
6608 /* The result of a statement-expression is not wrapped in EXPR_STMT. */
6609 if (tsi_one_before_end_p (i
) && TREE_CODE (stmt
) != EXPR_STMT
)
6612 r
= cxx_eval_constant_expression (ctx
, stmt
, lval
,
6613 non_constant_p
, overflow_p
,
6615 if (*non_constant_p
)
6617 if (returns (jump_target
) || breaks (jump_target
))
6620 if (*jump_target
&& jump_target
== &local_target
)
6622 /* We aren't communicating the jump to our caller, so give up. We don't
6623 need to support evaluation of jumps out of statement-exprs. */
6625 error_at (cp_expr_loc_or_input_loc (r
),
6626 "statement is not a constant expression");
6627 *non_constant_p
= true;
6632 /* Evaluate a LOOP_EXPR for side-effects. Handles break and return
6633 semantics; continue semantics are covered by cxx_eval_statement_list. */
6636 cxx_eval_loop_expr (const constexpr_ctx
*ctx
, tree t
,
6637 bool *non_constant_p
, bool *overflow_p
,
6640 constexpr_ctx new_ctx
= *ctx
;
6644 local_target
= NULL_TREE
;
6645 jump_target
= &local_target
;
6648 tree body
, cond
= NULL_TREE
, expr
= NULL_TREE
;
6650 switch (TREE_CODE (t
))
6653 body
= LOOP_EXPR_BODY (t
);
6660 body
= WHILE_BODY (t
);
6661 cond
= WHILE_COND (t
);
6665 if (FOR_INIT_STMT (t
))
6666 cxx_eval_constant_expression (ctx
, FOR_INIT_STMT (t
), vc_discard
,
6667 non_constant_p
, overflow_p
, jump_target
);
6668 if (*non_constant_p
)
6670 body
= FOR_BODY (t
);
6671 cond
= FOR_COND (t
);
6672 expr
= FOR_EXPR (t
);
6678 auto_vec
<tree
, 10> save_exprs
;
6679 new_ctx
.save_exprs
= &save_exprs
;
6685 cxx_eval_constant_expression (&new_ctx
, body
, vc_discard
,
6686 non_constant_p
, overflow_p
,
6688 if (breaks (jump_target
))
6690 *jump_target
= NULL_TREE
;
6694 if (TREE_CODE (t
) != LOOP_EXPR
&& continues (jump_target
))
6695 *jump_target
= NULL_TREE
;
6698 cxx_eval_constant_expression (&new_ctx
, expr
, vc_prvalue
,
6699 non_constant_p
, overflow_p
,
6706 = cxx_eval_constant_expression (&new_ctx
, cond
, vc_prvalue
,
6707 non_constant_p
, overflow_p
,
6711 if (verify_constant (res
, ctx
->quiet
, non_constant_p
,
6714 if (integer_zerop (res
))
6718 gcc_assert (*jump_target
);
6721 /* Forget saved values of SAVE_EXPRs and TARGET_EXPRs. */
6722 for (tree save_expr
: save_exprs
)
6723 ctx
->global
->remove_value (save_expr
);
6724 save_exprs
.truncate (0);
6726 if (++count
>= constexpr_loop_limit
)
6729 error_at (cp_expr_loc_or_input_loc (t
),
6730 "%<constexpr%> loop iteration count exceeds limit of %d "
6731 "(use %<-fconstexpr-loop-limit=%> to increase the limit)",
6732 constexpr_loop_limit
);
6733 *non_constant_p
= true;
6737 while (!returns (jump_target
)
6738 && !breaks (jump_target
)
6739 && !continues (jump_target
)
6740 && (!switches (jump_target
) || count
== 0)
6741 && !*non_constant_p
);
6743 /* Forget saved values of SAVE_EXPRs and TARGET_EXPRs. */
6744 for (tree save_expr
: save_exprs
)
6745 ctx
->global
->remove_value (save_expr
);
6750 /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
6754 cxx_eval_switch_expr (const constexpr_ctx
*ctx
, tree t
,
6755 bool *non_constant_p
, bool *overflow_p
,
6759 = TREE_CODE (t
) == SWITCH_STMT
? SWITCH_STMT_COND (t
) : SWITCH_COND (t
);
6760 cond
= cxx_eval_constant_expression (ctx
, cond
, vc_prvalue
,
6761 non_constant_p
, overflow_p
);
6762 VERIFY_CONSTANT (cond
);
6763 *jump_target
= cond
;
6766 = TREE_CODE (t
) == SWITCH_STMT
? SWITCH_STMT_BODY (t
) : SWITCH_BODY (t
);
6767 constexpr_ctx new_ctx
= *ctx
;
6768 constexpr_switch_state css
= css_default_not_seen
;
6769 new_ctx
.css_state
= &css
;
6770 cxx_eval_constant_expression (&new_ctx
, body
, vc_discard
,
6771 non_constant_p
, overflow_p
, jump_target
);
6772 if (switches (jump_target
) && css
== css_default_seen
)
6774 /* If the SWITCH_EXPR body has default: label, process it once again,
6775 this time instructing label_matches to return true for default:
6776 label on switches (jump_target). */
6777 css
= css_default_processing
;
6778 cxx_eval_constant_expression (&new_ctx
, body
, vc_discard
,
6779 non_constant_p
, overflow_p
, jump_target
);
6781 if (breaks (jump_target
) || switches (jump_target
))
6782 *jump_target
= NULL_TREE
;
6786 /* Find the object of TYPE under initialization in CTX. */
6789 lookup_placeholder (const constexpr_ctx
*ctx
, value_cat lval
, tree type
)
6794 /* Prefer the outermost matching object, but don't cross
6795 CONSTRUCTOR_PLACEHOLDER_BOUNDARY constructors. */
6796 if (ctx
->ctor
&& !CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx
->ctor
))
6797 if (tree outer_ob
= lookup_placeholder (ctx
->parent
, lval
, type
))
6800 /* We could use ctx->object unconditionally, but using ctx->ctor when we
6801 can is a minor optimization. */
6802 if (!lval
&& ctx
->ctor
&& same_type_p (TREE_TYPE (ctx
->ctor
), type
))
6808 /* Since an object cannot have a field of its own type, we can search outward
6809 from ctx->object to find the unique containing object of TYPE. */
6810 tree ob
= ctx
->object
;
6813 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob
), type
))
6815 if (handled_component_p (ob
))
6816 ob
= TREE_OPERAND (ob
, 0);
6824 /* Complain about an attempt to evaluate inline assembly. If FUNDEF_P is
6825 true, we're checking a constexpr function body. */
6828 inline_asm_in_constexpr_error (location_t loc
, bool fundef_p
)
6830 auto_diagnostic_group d
;
6831 if (constexpr_error (loc
, fundef_p
, "inline assembly is not a "
6832 "constant expression"))
6833 inform (loc
, "only unevaluated inline assembly is allowed in a "
6834 "%<constexpr%> function in C++20");
6837 /* We're getting the constant value of DECL in a manifestly constant-evaluated
6838 context; maybe complain about that. */
6841 maybe_warn_about_constant_value (location_t loc
, tree decl
)
6843 static bool explained
= false;
6844 if (cxx_dialect
>= cxx17
6845 && warn_interference_size
6846 && !OPTION_SET_P (param_destruct_interfere_size
)
6847 && DECL_CONTEXT (decl
) == std_node
6848 && id_equal (DECL_NAME (decl
), "hardware_destructive_interference_size")
6849 && (LOCATION_FILE (input_location
) != main_input_filename
6850 || module_exporting_p ())
6851 && warning_at (loc
, OPT_Winterference_size
, "use of %qD", decl
)
6855 inform (loc
, "its value can vary between compiler versions or "
6856 "with different %<-mtune%> or %<-mcpu%> flags");
6857 inform (loc
, "if this use is part of a public ABI, change it to "
6858 "instead use a constant variable you define");
6859 inform (loc
, "the default value for the current CPU tuning "
6860 "is %d bytes", param_destruct_interfere_size
);
6861 inform (loc
, "you can stabilize this value with %<--param "
6862 "hardware_destructive_interference_size=%d%>, or disable "
6863 "this warning with %<-Wno-interference-size%>",
6864 param_destruct_interfere_size
);
6868 /* For element type ELT_TYPE, return the appropriate type of the heap object
6869 containing such element(s). COOKIE_SIZE is NULL or the size of cookie
6870 in bytes. If COOKIE_SIZE is NULL, return array type
6871 ELT_TYPE[FULL_SIZE / sizeof(ELT_TYPE)], otherwise return
6872 struct { size_t[COOKIE_SIZE/sizeof(size_t)]; ELT_TYPE[N]; }
6873 where N is is computed such that the size of the struct fits into FULL_SIZE.
6874 If ARG_SIZE is non-NULL, it is the first argument to the new operator.
6875 It should be passed if ELT_TYPE is zero sized type in which case FULL_SIZE
6876 will be also 0 and so it is not possible to determine the actual array
6877 size. CTX, NON_CONSTANT_P and OVERFLOW_P are used during constant
6878 expression evaluation of subexpressions of ARG_SIZE. */
6881 build_new_constexpr_heap_type (const constexpr_ctx
*ctx
, tree elt_type
,
6882 tree cookie_size
, tree full_size
, tree arg_size
,
6883 bool *non_constant_p
, bool *overflow_p
)
6885 gcc_assert (cookie_size
== NULL_TREE
|| tree_fits_uhwi_p (cookie_size
));
6886 gcc_assert (tree_fits_uhwi_p (full_size
));
6887 unsigned HOST_WIDE_INT csz
= cookie_size
? tree_to_uhwi (cookie_size
) : 0;
6890 STRIP_NOPS (arg_size
);
6893 if (TREE_CODE (arg_size
) != PLUS_EXPR
)
6894 arg_size
= NULL_TREE
;
6895 else if (TREE_CODE (TREE_OPERAND (arg_size
, 0)) == INTEGER_CST
6896 && tree_int_cst_equal (cookie_size
,
6897 TREE_OPERAND (arg_size
, 0)))
6899 arg_size
= TREE_OPERAND (arg_size
, 1);
6900 STRIP_NOPS (arg_size
);
6902 else if (TREE_CODE (TREE_OPERAND (arg_size
, 1)) == INTEGER_CST
6903 && tree_int_cst_equal (cookie_size
,
6904 TREE_OPERAND (arg_size
, 1)))
6906 arg_size
= TREE_OPERAND (arg_size
, 0);
6907 STRIP_NOPS (arg_size
);
6910 arg_size
= NULL_TREE
;
6912 if (arg_size
&& TREE_CODE (arg_size
) == MULT_EXPR
)
6914 tree op0
= TREE_OPERAND (arg_size
, 0);
6915 tree op1
= TREE_OPERAND (arg_size
, 1);
6916 if (integer_zerop (op0
))
6918 = cxx_eval_constant_expression (ctx
, op1
, vc_prvalue
,
6919 non_constant_p
, overflow_p
);
6920 else if (integer_zerop (op1
))
6922 = cxx_eval_constant_expression (ctx
, op0
, vc_prvalue
,
6923 non_constant_p
, overflow_p
);
6925 arg_size
= NULL_TREE
;
6928 arg_size
= NULL_TREE
;
6931 unsigned HOST_WIDE_INT fsz
= tree_to_uhwi (arg_size
? arg_size
: full_size
);
6934 unsigned HOST_WIDE_INT esz
= int_size_in_bytes (elt_type
);
6935 gcc_assert (fsz
>= csz
);
6940 tree itype2
= build_index_type (size_int (fsz
- 1));
6942 return build_cplus_array_type (elt_type
, itype2
);
6943 return build_new_constexpr_heap_type (elt_type
, cookie_size
, itype2
);
6946 /* Attempt to reduce the expression T to a constant value.
6947 On failure, issue diagnostic and return error_mark_node. */
6948 /* FIXME unify with c_fully_fold */
6949 /* FIXME overflow_p is too global */
6952 cxx_eval_constant_expression (const constexpr_ctx
*ctx
, tree t
,
6954 bool *non_constant_p
, bool *overflow_p
,
6955 tree
*jump_target
/* = NULL */)
6957 if (jump_target
&& *jump_target
)
6959 /* If we are jumping, ignore all statements/expressions except those
6960 that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */
6961 switch (TREE_CODE (t
))
6964 case STATEMENT_LIST
:
6973 case CASE_LABEL_EXPR
:
6974 if (label_matches (ctx
, jump_target
, t
))
6976 *jump_target
= NULL_TREE
;
6982 if (error_operand_p (t
))
6984 *non_constant_p
= true;
6988 /* Change the input location to the currently processed expression for
6989 better error messages when a subexpression has no location. */
6990 location_t loc
= cp_expr_loc_or_input_loc (t
);
6991 iloc_sentinel
sentinel (loc
);
6993 STRIP_ANY_LOCATION_WRAPPER (t
);
6995 if (CONSTANT_CLASS_P (t
))
6997 if (TREE_OVERFLOW (t
))
7000 permerror (input_location
, "overflow in constant expression");
7001 if (!flag_permissive
|| ctx
->quiet
)
7005 if (TREE_CODE (t
) == INTEGER_CST
7006 && TYPE_PTR_P (TREE_TYPE (t
))
7007 /* INTEGER_CST with pointer-to-method type is only used
7008 for a virtual method in a pointer to member function.
7009 Don't reject those. */
7010 && TREE_CODE (TREE_TYPE (TREE_TYPE (t
))) != METHOD_TYPE
7011 && !integer_zerop (t
))
7014 error ("value %qE of type %qT is not a constant expression",
7016 *non_constant_p
= true;
7022 /* Avoid excessively long constexpr evaluations. */
7023 if (++ctx
->global
->constexpr_ops_count
>= constexpr_ops_limit
)
7027 "%<constexpr%> evaluation operation count exceeds limit of "
7028 "%wd (use %<-fconstexpr-ops-limit=%> to increase the limit)",
7029 constexpr_ops_limit
);
7030 ctx
->global
->constexpr_ops_count
= INTTYPE_MINIMUM (HOST_WIDE_INT
);
7031 *non_constant_p
= true;
7035 constexpr_ctx new_ctx
;
7038 tree_code tcode
= TREE_CODE (t
);
7044 /* We ask for an rvalue for the RESULT_DECL when indirecting
7045 through an invisible reference, or in named return value
7047 if (tree v
= ctx
->global
->get_value (t
))
7052 error ("%qE is not a constant expression", t
);
7053 *non_constant_p
= true;
7058 if (DECL_HAS_VALUE_EXPR_P (t
))
7060 if (is_normal_capture_proxy (t
)
7061 && current_function_decl
== DECL_CONTEXT (t
))
7063 /* Function parms aren't constexpr within the function
7064 definition, so don't try to look at the closure. But if the
7065 captured variable is constant, try to evaluate it directly. */
7066 r
= DECL_CAPTURED_VARIABLE (t
);
7067 tree type
= TREE_TYPE (t
);
7068 if (TYPE_REF_P (type
) != TYPE_REF_P (TREE_TYPE (r
)))
7070 /* Adjust r to match the reference-ness of t. */
7071 if (TYPE_REF_P (type
))
7072 r
= build_address (r
);
7074 r
= convert_from_reference (r
);
7078 r
= DECL_VALUE_EXPR (t
);
7079 return cxx_eval_constant_expression (ctx
, r
, lval
, non_constant_p
,
7084 /* We used to not check lval for CONST_DECL, but darwin.cc uses
7085 CONST_DECL for aggregate constants. */
7088 else if (t
== ctx
->object
)
7091 if (tree v
= ctx
->global
->get_value (t
))
7096 if (ctx
->manifestly_const_eval
== mce_true
)
7097 maybe_warn_about_constant_value (loc
, t
);
7098 if (COMPLETE_TYPE_P (TREE_TYPE (t
))
7099 && is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
7101 /* If the class is empty, we aren't actually loading anything. */
7102 r
= build_constructor (TREE_TYPE (t
), NULL
);
7103 TREE_CONSTANT (r
) = true;
7105 else if (ctx
->strict
)
7106 r
= decl_really_constant_value (t
, /*unshare_p=*/false);
7108 r
= decl_constant_value (t
, /*unshare_p=*/false);
7109 if (TREE_CODE (r
) == TARGET_EXPR
7110 && TREE_CODE (TARGET_EXPR_INITIAL (r
)) == CONSTRUCTOR
)
7111 r
= TARGET_EXPR_INITIAL (r
);
7115 non_const_var_error (loc
, r
, /*fundef_p*/false);
7116 *non_constant_p
= true;
7120 case DEBUG_BEGIN_STMT
:
7121 /* ??? It might be nice to retain this information somehow, so
7122 as to be able to step into a constexpr function call. */
7129 case CASE_LABEL_EXPR
:
7134 if (lval
&& !TYPE_REF_P (TREE_TYPE (t
)))
7136 else if (tree v
= ctx
->global
->get_value (t
))
7139 /* Defer in case this is only used for its type. */;
7140 else if (COMPLETE_TYPE_P (TREE_TYPE (t
))
7141 && is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
7143 /* If the class is empty, we aren't actually loading anything. */
7144 r
= build_constructor (TREE_TYPE (t
), NULL
);
7145 TREE_CONSTANT (r
) = true;
7150 error ("%qE is not a constant expression", t
);
7151 *non_constant_p
= true;
7156 case AGGR_INIT_EXPR
:
7157 r
= cxx_eval_call_expression (ctx
, t
, lval
,
7158 non_constant_p
, overflow_p
);
7163 r
= DECL_EXPR_DECL (t
);
7164 if (TREE_CODE (r
) == USING_DECL
)
7172 || (CP_DECL_THREAD_LOCAL_P (r
) && !DECL_REALLY_EXTERN (r
)))
7173 /* Allow __FUNCTION__ etc. */
7174 && !DECL_ARTIFICIAL (r
)
7175 && !decl_constant_var_p (r
))
7179 if (CP_DECL_THREAD_LOCAL_P (r
))
7180 error_at (loc
, "control passes through definition of %qD "
7181 "with thread storage duration", r
);
7183 error_at (loc
, "control passes through definition of %qD "
7184 "with static storage duration", r
);
7186 *non_constant_p
= true;
7190 /* make_rtl_for_nonlocal_decl could have deferred emission of
7191 a local static var, but if it appears in a statement expression
7192 which is constant expression evaluated to e.g. just the address
7193 of the variable, its DECL_EXPR will never be seen during
7194 gimple lowering's record_vars_into as the statement expression
7195 will not be in the IL at all. */
7198 && !DECL_REALLY_EXTERN (r
)
7199 && DECL_FUNCTION_SCOPE_P (r
)
7200 && !var_in_maybe_constexpr_fn (r
)
7201 && decl_constant_var_p (r
))
7203 varpool_node
*node
= varpool_node::get (r
);
7204 if (node
== NULL
|| !node
->definition
)
7205 rest_of_decl_compilation (r
, 0, at_eof
);
7208 if (AGGREGATE_TYPE_P (TREE_TYPE (r
))
7209 || VECTOR_TYPE_P (TREE_TYPE (r
)))
7213 new_ctx
.ctor
= build_constructor (TREE_TYPE (r
), NULL
);
7214 CONSTRUCTOR_NO_CLEARING (new_ctx
.ctor
) = true;
7215 ctx
->global
->put_value (r
, new_ctx
.ctor
);
7219 if (tree init
= DECL_INITIAL (r
))
7221 init
= cxx_eval_constant_expression (ctx
, init
, vc_prvalue
,
7222 non_constant_p
, overflow_p
);
7223 /* Don't share a CONSTRUCTOR that might be changed. */
7224 init
= unshare_constructor (init
);
7225 /* Remember that a constant object's constructor has already
7227 if (CLASS_TYPE_P (TREE_TYPE (r
))
7228 && CP_TYPE_CONST_P (TREE_TYPE (r
)))
7229 TREE_READONLY (init
) = true;
7230 ctx
->global
->put_value (r
, init
);
7232 else if (ctx
== &new_ctx
)
7233 /* We gave it a CONSTRUCTOR above. */;
7235 ctx
->global
->put_value (r
, NULL_TREE
);
7241 tree type
= TREE_TYPE (t
);
7243 if (!literal_type_p (type
))
7247 auto_diagnostic_group d
;
7248 error ("temporary of non-literal type %qT in a "
7249 "constant expression", type
);
7250 explain_non_literal_class (type
);
7252 *non_constant_p
= true;
7255 gcc_checking_assert (!TARGET_EXPR_DIRECT_INIT_P (t
));
7256 /* Avoid evaluating a TARGET_EXPR more than once. */
7257 tree slot
= TARGET_EXPR_SLOT (t
);
7258 if (tree v
= ctx
->global
->get_value (slot
))
7265 if ((AGGREGATE_TYPE_P (type
) || VECTOR_TYPE_P (type
)))
7267 /* We're being expanded without an explicit target, so start
7268 initializing a new object; expansion with an explicit target
7269 strips the TARGET_EXPR before we get here. */
7271 /* Link CTX to NEW_CTX so that lookup_placeholder can resolve
7272 any PLACEHOLDER_EXPR within the initializer that refers to the
7273 former object under construction. */
7274 new_ctx
.parent
= ctx
;
7275 new_ctx
.ctor
= build_constructor (type
, NULL
);
7276 CONSTRUCTOR_NO_CLEARING (new_ctx
.ctor
) = true;
7277 new_ctx
.object
= slot
;
7278 ctx
->global
->put_value (new_ctx
.object
, new_ctx
.ctor
);
7281 /* Pass vc_prvalue because this indicates
7282 initialization of a temporary. */
7283 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1), vc_prvalue
,
7284 non_constant_p
, overflow_p
);
7285 if (*non_constant_p
)
7287 /* If the initializer is complex, evaluate it to initialize slot. */
7288 bool is_complex
= target_expr_needs_replace (t
);
7291 r
= unshare_constructor (r
);
7292 /* Adjust the type of the result to the type of the temporary. */
7293 r
= adjust_temp_type (type
, r
);
7294 ctx
->global
->put_value (slot
, r
);
7296 if (TARGET_EXPR_CLEANUP (t
) && !CLEANUP_EH_ONLY (t
))
7297 ctx
->global
->cleanups
->safe_push (TARGET_EXPR_CLEANUP (t
));
7298 if (ctx
->save_exprs
)
7299 ctx
->save_exprs
->safe_push (slot
);
7303 r
= ctx
->global
->get_value (slot
);
7309 gcc_assert (jump_target
== NULL
|| *jump_target
== NULL_TREE
);
7310 r
= cxx_eval_store_expression (ctx
, t
, lval
,
7311 non_constant_p
, overflow_p
);
7315 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
7317 non_constant_p
, overflow_p
);
7321 if (TREE_OPERAND (t
, 0) != NULL_TREE
)
7322 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
7324 non_constant_p
, overflow_p
);
7332 /* Can happen with ({ return true; }) && false; passed to
7333 maybe_constant_value. There is nothing to jump over in this
7334 case, and the bug will be diagnosed later. */
7335 gcc_assert (ctx
->quiet
);
7336 *non_constant_p
= true;
7341 /* Avoid evaluating a SAVE_EXPR more than once. */
7342 if (tree v
= ctx
->global
->get_value (t
))
7346 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), vc_prvalue
,
7347 non_constant_p
, overflow_p
);
7348 if (*non_constant_p
)
7350 ctx
->global
->put_value (t
, r
);
7351 if (ctx
->save_exprs
)
7352 ctx
->save_exprs
->safe_push (t
);
7356 case TRY_CATCH_EXPR
:
7357 if (TREE_OPERAND (t
, 0) == NULL_TREE
)
7363 case NON_LVALUE_EXPR
:
7365 case MUST_NOT_THROW_EXPR
:
7368 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
7370 non_constant_p
, overflow_p
,
7374 case CLEANUP_POINT_EXPR
:
7376 auto_vec
<tree
, 2> cleanups
;
7377 vec
<tree
> *prev_cleanups
= ctx
->global
->cleanups
;
7378 ctx
->global
->cleanups
= &cleanups
;
7379 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
7381 non_constant_p
, overflow_p
,
7383 ctx
->global
->cleanups
= prev_cleanups
;
7386 /* Evaluate the cleanups. */
7387 FOR_EACH_VEC_ELT_REVERSE (cleanups
, i
, cleanup
)
7388 cxx_eval_constant_expression (ctx
, cleanup
, vc_discard
,
7389 non_constant_p
, overflow_p
);
7393 case TRY_FINALLY_EXPR
:
7394 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), lval
,
7395 non_constant_p
, overflow_p
,
7397 if (!*non_constant_p
)
7398 /* Also evaluate the cleanup. */
7399 cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1), vc_discard
,
7400 non_constant_p
, overflow_p
);
7404 r
= cxx_eval_constant_expression (ctx
, CLEANUP_BODY (t
), lval
,
7405 non_constant_p
, overflow_p
,
7407 if (!CLEANUP_EH_ONLY (t
) && !*non_constant_p
)
7409 iloc_sentinel
ils (loc
);
7410 /* Also evaluate the cleanup. */
7411 cxx_eval_constant_expression (ctx
, CLEANUP_EXPR (t
), vc_discard
,
7412 non_constant_p
, overflow_p
);
7416 /* These differ from cxx_eval_unary_expression in that this doesn't
7417 check for a constant operand or result; an address can be
7418 constant without its operand being, and vice versa. */
7421 r
= cxx_eval_indirect_ref (ctx
, t
, lval
,
7422 non_constant_p
, overflow_p
);
7427 tree oldop
= TREE_OPERAND (t
, 0);
7428 tree op
= cxx_eval_constant_expression (ctx
, oldop
, vc_glvalue
,
7429 non_constant_p
, overflow_p
);
7430 /* Don't VERIFY_CONSTANT here. */
7431 if (*non_constant_p
)
7433 gcc_checking_assert (TREE_CODE (op
) != CONSTRUCTOR
);
7434 /* This function does more aggressive folding than fold itself. */
7435 r
= build_fold_addr_expr_with_type (op
, TREE_TYPE (t
));
7436 if (TREE_CODE (r
) == ADDR_EXPR
&& TREE_OPERAND (r
, 0) == oldop
)
7448 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), lval
,
7449 non_constant_p
, overflow_p
);
7450 if (r
== error_mark_node
)
7452 else if (r
== TREE_OPERAND (t
, 0) || lval
== vc_discard
)
7455 r
= fold_build1 (TREE_CODE (t
), TREE_TYPE (t
), r
);
7460 case FIX_TRUNC_EXPR
:
7466 case TRUTH_NOT_EXPR
:
7467 case FIXED_CONVERT_EXPR
:
7468 r
= cxx_eval_unary_expression (ctx
, t
, lval
,
7469 non_constant_p
, overflow_p
);
7473 r
= fold_sizeof_expr (t
);
7474 /* In a template, fold_sizeof_expr may merely create a new SIZEOF_EXPR,
7475 which could lead to an infinite recursion. */
7476 if (TREE_CODE (r
) != SIZEOF_EXPR
)
7477 r
= cxx_eval_constant_expression (ctx
, r
, lval
,
7478 non_constant_p
, overflow_p
,
7482 *non_constant_p
= true;
7483 gcc_assert (ctx
->quiet
);
7490 /* check_return_expr sometimes wraps a TARGET_EXPR in a
7491 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
7492 introduced by build_call_a. */
7493 tree op0
= TREE_OPERAND (t
, 0);
7494 tree op1
= TREE_OPERAND (t
, 1);
7496 if ((TREE_CODE (op0
) == TARGET_EXPR
&& op1
== TARGET_EXPR_SLOT (op0
))
7497 || TREE_CODE (op1
) == EMPTY_CLASS_EXPR
)
7498 r
= cxx_eval_constant_expression (ctx
, op0
,
7499 lval
, non_constant_p
, overflow_p
,
7503 /* Check that the LHS is constant and then discard it. */
7504 cxx_eval_constant_expression (ctx
, op0
, vc_discard
,
7505 non_constant_p
, overflow_p
,
7507 if (*non_constant_p
)
7509 op1
= TREE_OPERAND (t
, 1);
7510 r
= cxx_eval_constant_expression (ctx
, op1
,
7511 lval
, non_constant_p
, overflow_p
,
7517 case POINTER_PLUS_EXPR
:
7518 case POINTER_DIFF_EXPR
:
7522 case TRUNC_DIV_EXPR
:
7524 case FLOOR_DIV_EXPR
:
7525 case ROUND_DIV_EXPR
:
7526 case TRUNC_MOD_EXPR
:
7528 case ROUND_MOD_EXPR
:
7530 case EXACT_DIV_EXPR
:
7540 case TRUTH_XOR_EXPR
:
7547 case SPACESHIP_EXPR
:
7548 case UNORDERED_EXPR
:
7558 r
= cxx_eval_binary_expression (ctx
, t
, lval
,
7559 non_constant_p
, overflow_p
);
7562 /* fold can introduce non-IF versions of these; still treat them as
7563 short-circuiting. */
7564 case TRUTH_AND_EXPR
:
7565 case TRUTH_ANDIF_EXPR
:
7566 r
= cxx_eval_logical_expression (ctx
, t
, boolean_false_node
,
7568 non_constant_p
, overflow_p
);
7572 case TRUTH_ORIF_EXPR
:
7573 r
= cxx_eval_logical_expression (ctx
, t
, boolean_true_node
,
7575 non_constant_p
, overflow_p
);
7579 r
= cxx_eval_array_reference (ctx
, t
, lval
,
7580 non_constant_p
, overflow_p
);
7584 if (is_overloaded_fn (t
))
7586 /* We can only get here in checking mode via
7587 build_non_dependent_expr, because any expression that
7588 calls or takes the address of the function will have
7589 pulled a FUNCTION_DECL out of the COMPONENT_REF. */
7590 gcc_checking_assert (ctx
->quiet
|| errorcount
);
7591 *non_constant_p
= true;
7594 r
= cxx_eval_component_reference (ctx
, t
, lval
,
7595 non_constant_p
, overflow_p
);
7599 r
= cxx_eval_bit_field_ref (ctx
, t
, lval
,
7600 non_constant_p
, overflow_p
);
7605 if (jump_target
&& *jump_target
)
7607 tree orig_jump
= *jump_target
;
7608 tree arg
= ((TREE_CODE (t
) != IF_STMT
|| TREE_OPERAND (t
, 1))
7609 ? TREE_OPERAND (t
, 1) : void_node
);
7610 /* When jumping to a label, the label might be either in the
7611 then or else blocks, so process then block first in skipping
7612 mode first, and if we are still in the skipping mode at its end,
7613 process the else block too. */
7614 r
= cxx_eval_constant_expression (ctx
, arg
, lval
, non_constant_p
,
7615 overflow_p
, jump_target
);
7616 /* It's possible that we found the label in the then block. But
7617 it could have been followed by another jumping statement, e.g.
7618 say we're looking for case 1:
7621 // skipped statements
7622 case 1:; // clears up *jump_target
7623 return 1; // and sets it to a RETURN_EXPR
7626 in which case we need not go looking to the else block.
7627 (goto is not allowed in a constexpr function.) */
7628 if (*jump_target
== orig_jump
)
7630 arg
= ((TREE_CODE (t
) != IF_STMT
|| TREE_OPERAND (t
, 2))
7631 ? TREE_OPERAND (t
, 2) : void_node
);
7632 r
= cxx_eval_constant_expression (ctx
, arg
, lval
, non_constant_p
,
7633 overflow_p
, jump_target
);
7637 r
= cxx_eval_conditional_expression (ctx
, t
, lval
,
7638 non_constant_p
, overflow_p
,
7642 r
= cxx_eval_vector_conditional_expression (ctx
, t
, non_constant_p
,
7647 if (TREE_CONSTANT (t
) && reduced_constant_expression_p (t
))
7649 /* Don't re-process a constant CONSTRUCTOR. */
7650 verify_constructor_flags (t
);
7651 if (TREE_CONSTANT (t
))
7654 r
= cxx_eval_bare_aggregate (ctx
, t
, lval
,
7655 non_constant_p
, overflow_p
);
7659 /* We can get this in a defaulted constructor for a class with a
7660 non-static data member of array type. Either the initializer will
7661 be NULL, meaning default-initialization, or it will be an lvalue
7662 or xvalue of the same type, meaning direct-initialization from the
7663 corresponding member. */
7664 r
= cxx_eval_vec_init (ctx
, t
, lval
,
7665 non_constant_p
, overflow_p
);
7669 r
= cxx_eval_trinary_expression (ctx
, t
, lval
,
7670 non_constant_p
, overflow_p
);
7674 gcc_assert (!REF_PARENTHESIZED_P (t
));
7675 /* A PAREN_EXPR resulting from __builtin_assoc_barrier has no effect in
7676 constant expressions since it's unaffected by -fassociative-math. */
7677 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), lval
,
7678 non_constant_p
, overflow_p
);
7682 if (REINTERPRET_CAST_P (t
))
7686 "%<reinterpret_cast%> is not a constant expression");
7687 *non_constant_p
= true;
7692 case VIEW_CONVERT_EXPR
:
7693 case UNARY_PLUS_EXPR
:
7695 tree oldop
= TREE_OPERAND (t
, 0);
7697 tree op
= cxx_eval_constant_expression (ctx
, oldop
,
7699 non_constant_p
, overflow_p
);
7700 if (*non_constant_p
)
7702 tree type
= TREE_TYPE (t
);
7704 if (VOID_TYPE_P (type
))
7707 if (TREE_CODE (t
) == CONVERT_EXPR
7708 && ARITHMETIC_TYPE_P (type
)
7709 && INDIRECT_TYPE_P (TREE_TYPE (op
))
7710 && ctx
->manifestly_const_eval
== mce_true
)
7714 "conversion from pointer type %qT to arithmetic type "
7715 "%qT in a constant expression", TREE_TYPE (op
), type
);
7716 *non_constant_p
= true;
7720 /* [expr.const]: a conversion from type cv void* to a pointer-to-object
7721 type cannot be part of a core constant expression as a resolution to
7723 if (TYPE_PTROB_P (type
)
7724 && TYPE_PTR_P (TREE_TYPE (op
))
7725 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (op
)))
7726 /* Inside a call to std::construct_at or to
7727 std::allocator<T>::{,de}allocate, we permit casting from void*
7728 because that is compiler-generated code. */
7729 && !is_std_construct_at (ctx
->call
)
7730 && !is_std_allocator_allocate (ctx
->call
))
7732 /* Likewise, don't error when casting from void* when OP is
7733 &heap uninit and similar. */
7734 tree sop
= tree_strip_nop_conversions (op
);
7735 if (TREE_CODE (sop
) == ADDR_EXPR
7736 && VAR_P (TREE_OPERAND (sop
, 0))
7737 && DECL_ARTIFICIAL (TREE_OPERAND (sop
, 0)))
7739 /* P2738 (C++26): a conversion from a prvalue P of type "pointer to
7740 cv void" to a pointer-to-object type T unless P points to an
7741 object whose type is similar to T. */
7742 else if (cxx_dialect
> cxx23
7743 && (sop
= cxx_fold_indirect_ref (ctx
, loc
,
7744 TREE_TYPE (type
), sop
)))
7746 r
= build1 (ADDR_EXPR
, type
, sop
);
7752 error_at (loc
, "cast from %qT is not allowed",
7754 *non_constant_p
= true;
7759 if (TREE_CODE (op
) == PTRMEM_CST
&& !TYPE_PTRMEM_P (type
))
7761 op
= cplus_expand_constant (op
);
7762 if (TREE_CODE (op
) == PTRMEM_CST
)
7765 error_at (loc
, "%qE is not a constant expression when the "
7766 "class %qT is still incomplete", op
,
7767 PTRMEM_CST_CLASS (op
));
7768 *non_constant_p
= true;
7773 if (TREE_CODE (op
) == PTRMEM_CST
&& tcode
== NOP_EXPR
)
7775 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (op
))
7776 && !can_convert_qual (type
, op
))
7777 op
= cplus_expand_constant (op
);
7778 return cp_fold_convert (type
, op
);
7781 if (INDIRECT_TYPE_P (type
) && TREE_CODE (op
) == INTEGER_CST
)
7783 if (integer_zerop (op
))
7785 if (TYPE_REF_P (type
))
7788 error_at (loc
, "dereferencing a null pointer");
7789 *non_constant_p
= true;
7795 /* This detects for example:
7796 reinterpret_cast<void*>(sizeof 0)
7799 error_at (loc
, "%<reinterpret_cast<%T>(%E)%> is not "
7800 "a constant expression",
7802 *non_constant_p
= true;
7807 if (INDIRECT_TYPE_P (type
)
7808 && TREE_CODE (op
) == NOP_EXPR
7809 && TREE_TYPE (op
) == ptr_type_node
7810 && TREE_CODE (TREE_OPERAND (op
, 0)) == ADDR_EXPR
7811 && VAR_P (TREE_OPERAND (TREE_OPERAND (op
, 0), 0))
7812 && (DECL_NAME (TREE_OPERAND (TREE_OPERAND (op
, 0),
7813 0)) == heap_uninit_identifier
7814 || DECL_NAME (TREE_OPERAND (TREE_OPERAND (op
, 0),
7815 0)) == heap_vec_uninit_identifier
))
7817 tree var
= TREE_OPERAND (TREE_OPERAND (op
, 0), 0);
7818 tree var_size
= TYPE_SIZE_UNIT (TREE_TYPE (var
));
7819 tree elt_type
= TREE_TYPE (type
);
7820 tree cookie_size
= NULL_TREE
;
7821 tree arg_size
= NULL_TREE
;
7822 if (TREE_CODE (elt_type
) == RECORD_TYPE
7823 && TYPE_NAME (elt_type
) == heap_identifier
)
7825 tree fld1
= TYPE_FIELDS (elt_type
);
7826 tree fld2
= DECL_CHAIN (fld1
);
7827 elt_type
= TREE_TYPE (TREE_TYPE (fld2
));
7828 cookie_size
= TYPE_SIZE_UNIT (TREE_TYPE (fld1
));
7831 = (DECL_NAME (var
) == heap_uninit_identifier
7832 ? heap_identifier
: heap_vec_identifier
);
7833 /* For zero sized elt_type, try to recover how many outer_nelts
7835 if ((cookie_size
? tree_int_cst_equal (var_size
, cookie_size
)
7836 : integer_zerop (var_size
))
7837 && !int_size_in_bytes (elt_type
)
7838 && TREE_CODE (oldop
) == CALL_EXPR
7839 && call_expr_nargs (oldop
) >= 1)
7840 if (tree fun
= get_function_named_in_call (oldop
))
7841 if (cxx_replaceable_global_alloc_fn (fun
)
7842 && IDENTIFIER_NEW_OP_P (DECL_NAME (fun
)))
7843 arg_size
= CALL_EXPR_ARG (oldop
, 0);
7845 = build_new_constexpr_heap_type (ctx
, elt_type
, cookie_size
,
7847 non_constant_p
, overflow_p
);
7848 TREE_TYPE (TREE_OPERAND (op
, 0))
7849 = build_pointer_type (TREE_TYPE (var
));
7852 if (op
== oldop
&& tcode
!= UNARY_PLUS_EXPR
)
7853 /* We didn't fold at the top so we could check for ptr-int
7859 /* Handle an array's bounds having been deduced after we built
7860 the wrapping expression. */
7861 if (same_type_ignoring_tlq_and_bounds_p (type
, TREE_TYPE (op
)))
7863 else if (sop
= tree_strip_nop_conversions (op
),
7864 sop
!= op
&& (same_type_ignoring_tlq_and_bounds_p
7865 (type
, TREE_TYPE (sop
))))
7867 else if (tcode
== UNARY_PLUS_EXPR
)
7868 r
= fold_convert (TREE_TYPE (t
), op
);
7870 r
= fold_build1 (tcode
, type
, op
);
7872 /* Conversion of an out-of-range value has implementation-defined
7873 behavior; the language considers it different from arithmetic
7874 overflow, which is undefined. */
7875 if (TREE_OVERFLOW_P (r
) && !TREE_OVERFLOW_P (op
))
7876 TREE_OVERFLOW (r
) = false;
7880 case EXCESS_PRECISION_EXPR
:
7882 tree oldop
= TREE_OPERAND (t
, 0);
7884 tree op
= cxx_eval_constant_expression (ctx
, oldop
,
7886 non_constant_p
, overflow_p
);
7887 if (*non_constant_p
)
7889 r
= fold_convert (TREE_TYPE (t
), op
);
7893 case EMPTY_CLASS_EXPR
:
7894 /* Handle EMPTY_CLASS_EXPR produced by build_call_a by lowering
7895 it to an appropriate CONSTRUCTOR. */
7896 return build_constructor (TREE_TYPE (t
), NULL
);
7898 case STATEMENT_LIST
:
7900 new_ctx
.ctor
= new_ctx
.object
= NULL_TREE
;
7901 return cxx_eval_statement_list (&new_ctx
, t
,
7902 non_constant_p
, overflow_p
, jump_target
);
7905 return cxx_eval_constant_expression (ctx
, BIND_EXPR_BODY (t
),
7907 non_constant_p
, overflow_p
,
7910 case PREINCREMENT_EXPR
:
7911 case POSTINCREMENT_EXPR
:
7912 case PREDECREMENT_EXPR
:
7913 case POSTDECREMENT_EXPR
:
7914 return cxx_eval_increment_expression (ctx
, t
,
7915 lval
, non_constant_p
, overflow_p
);
7921 case VEC_DELETE_EXPR
:
7924 /* GCC internal stuff. */
7926 case NON_DEPENDENT_EXPR
:
7930 error_at (loc
, "expression %qE is not a constant expression", t
);
7931 *non_constant_p
= true;
7935 /* Virtual function lookup. We don't need to do anything fancy. */
7936 return cxx_eval_constant_expression (ctx
, OBJ_TYPE_REF_EXPR (t
),
7937 lval
, non_constant_p
, overflow_p
);
7939 case PLACEHOLDER_EXPR
:
7940 /* Use of the value or address of the current object. */
7941 if (tree ctor
= lookup_placeholder (ctx
, lval
, TREE_TYPE (t
)))
7943 if (TREE_CODE (ctor
) == CONSTRUCTOR
)
7946 return cxx_eval_constant_expression (ctx
, ctor
, lval
,
7947 non_constant_p
, overflow_p
);
7949 /* A placeholder without a referent. We can get here when
7950 checking whether NSDMIs are noexcept, or in massage_init_elt;
7951 just say it's non-constant for now. */
7952 gcc_assert (ctx
->quiet
);
7953 *non_constant_p
= true;
7958 tree cond
= TREE_OPERAND (t
, 0);
7959 cond
= cxx_eval_constant_expression (ctx
, cond
, vc_prvalue
,
7960 non_constant_p
, overflow_p
);
7961 VERIFY_CONSTANT (cond
);
7962 if (integer_nonzerop (cond
))
7968 if (breaks (&TREE_OPERAND (t
, 0))
7969 || continues (&TREE_OPERAND (t
, 0)))
7970 *jump_target
= TREE_OPERAND (t
, 0);
7973 gcc_assert (cxx_dialect
>= cxx23
);
7975 error_at (loc
, "%<goto%> is not a constant expression");
7976 *non_constant_p
= true;
7984 cxx_eval_loop_expr (ctx
, t
,
7985 non_constant_p
, overflow_p
, jump_target
);
7990 cxx_eval_switch_expr (ctx
, t
,
7991 non_constant_p
, overflow_p
, jump_target
);
7995 /* It's possible to get a requires-expression in a constant
7996 expression. For example:
7998 template<typename T> concept bool C() {
7999 return requires (T t) { t; };
8002 template<typename T> requires !C<T>() void f(T);
8004 Normalization leaves f with the associated constraint
8005 '!requires (T t) { ... }' which is not transformed into
8007 if (!processing_template_decl
)
8008 return evaluate_requires_expr (t
);
8010 *non_constant_p
= true;
8014 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
8016 non_constant_p
, overflow_p
,
8024 case ASSERTION_STMT
:
8025 case PRECONDITION_STMT
:
8026 case POSTCONDITION_STMT
:
8028 contract_semantic semantic
= get_contract_semantic (t
);
8029 if (semantic
== CCS_IGNORE
)
8032 if (!cxx_eval_assert (ctx
, CONTRACT_CONDITION (t
),
8033 G_("contract predicate is false in "
8034 "constant expression"),
8035 EXPR_LOCATION (t
), checked_contract_p (semantic
),
8036 non_constant_p
, overflow_p
))
8037 *non_constant_p
= true;
8042 case TEMPLATE_ID_EXPR
:
8044 /* We can evaluate template-id that refers to a concept only if
8045 the template arguments are non-dependent. */
8046 tree id
= unpack_concept_check (t
);
8047 tree tmpl
= TREE_OPERAND (id
, 0);
8048 if (!concept_definition_p (tmpl
))
8049 internal_error ("unexpected template-id %qE", t
);
8051 if (function_concept_p (tmpl
))
8054 error_at (cp_expr_loc_or_input_loc (t
),
8055 "function concept must be called");
8056 r
= error_mark_node
;
8060 if (!value_dependent_expression_p (t
)
8061 && !uid_sensitive_constexpr_evaluation_p ())
8062 r
= evaluate_concept_check (t
);
8064 *non_constant_p
= true;
8071 inline_asm_in_constexpr_error (loc
, /*constexpr_fundef_p*/false);
8072 *non_constant_p
= true;
8079 error_at (EXPR_LOCATION (t
),
8080 "address of a call to %qs is not a constant expression",
8081 "__builtin_bit_cast");
8082 *non_constant_p
= true;
8085 r
= cxx_eval_bit_cast (ctx
, t
, non_constant_p
, overflow_p
);
8092 case OMP_DISTRIBUTE
:
8096 case OMP_TARGET_DATA
:
8108 case OMP_TARGET_UPDATE
:
8109 case OMP_TARGET_ENTER_DATA
:
8110 case OMP_TARGET_EXIT_DATA
:
8112 case OMP_ATOMIC_READ
:
8113 case OMP_ATOMIC_CAPTURE_OLD
:
8114 case OMP_ATOMIC_CAPTURE_NEW
:
8120 case OACC_HOST_DATA
:
8124 case OACC_ENTER_DATA
:
8125 case OACC_EXIT_DATA
:
8128 error_at (EXPR_LOCATION (t
),
8129 "statement is not a constant expression");
8130 *non_constant_p
= true;
8134 if (STATEMENT_CODE_P (TREE_CODE (t
)))
8136 /* This function doesn't know how to deal with pre-genericize
8137 statements; this can only happen with statement-expressions,
8138 so for now just fail. */
8140 error_at (EXPR_LOCATION (t
),
8141 "statement is not a constant expression");
8144 internal_error ("unexpected expression %qE of kind %s", t
,
8145 get_tree_code_name (TREE_CODE (t
)));
8146 *non_constant_p
= true;
8150 if (r
== error_mark_node
)
8151 *non_constant_p
= true;
8153 if (*non_constant_p
)
8159 /* P0859: A function is needed for constant evaluation if it is a constexpr
8160 function that is named by an expression ([basic.def.odr]) that is
8161 potentially constant evaluated.
8163 So we need to instantiate any constexpr functions mentioned by the
8164 expression even if the definition isn't needed for evaluating the
8168 instantiate_cx_fn_r (tree
*tp
, int *walk_subtrees
, void */
*data*/
)
8170 if (TREE_CODE (*tp
) == FUNCTION_DECL
8171 && DECL_DECLARED_CONSTEXPR_P (*tp
)
8172 && !DECL_INITIAL (*tp
)
8173 && !trivial_fn_p (*tp
)
8174 && (DECL_TEMPLOID_INSTANTIATION (*tp
) || DECL_DEFAULTED_FN (*tp
))
8175 && !uid_sensitive_constexpr_evaluation_p ())
8178 if (DECL_TEMPLOID_INSTANTIATION (*tp
))
8179 instantiate_decl (*tp
, /*defer_ok*/false, /*expl_inst*/false);
8181 synthesize_method (*tp
);
8184 else if (TREE_CODE (*tp
) == CALL_EXPR
8185 || TREE_CODE (*tp
) == AGGR_INIT_EXPR
)
8187 if (EXPR_HAS_LOCATION (*tp
))
8188 input_location
= EXPR_LOCATION (*tp
);
8198 instantiate_constexpr_fns (tree t
)
8200 location_t loc
= input_location
;
8201 cp_walk_tree_without_duplicates (&t
, instantiate_cx_fn_r
, NULL
);
8202 input_location
= loc
;
8205 /* Look for heap variables in the expression *TP. */
8208 find_heap_var_refs (tree
*tp
, int *walk_subtrees
, void */
*data*/
)
8211 && (DECL_NAME (*tp
) == heap_uninit_identifier
8212 || DECL_NAME (*tp
) == heap_identifier
8213 || DECL_NAME (*tp
) == heap_vec_uninit_identifier
8214 || DECL_NAME (*tp
) == heap_vec_identifier
8215 || DECL_NAME (*tp
) == heap_deleted_identifier
))
8223 /* Find immediate function decls in *TP if any. */
8226 find_immediate_fndecl (tree
*tp
, int */
*walk_subtrees*/
, void */
*data*/
)
8228 if (TREE_CODE (*tp
) == FUNCTION_DECL
&& DECL_IMMEDIATE_FUNCTION_P (*tp
))
8230 if (TREE_CODE (*tp
) == PTRMEM_CST
8231 && TREE_CODE (PTRMEM_CST_MEMBER (*tp
)) == FUNCTION_DECL
8232 && DECL_IMMEDIATE_FUNCTION_P (PTRMEM_CST_MEMBER (*tp
)))
8233 return PTRMEM_CST_MEMBER (*tp
);
8237 /* T has TREE_CONSTANT set but has been deemed not a valid C++ constant
8238 expression. Return a version of T that has TREE_CONSTANT cleared. */
8241 mark_non_constant (tree t
)
8243 gcc_checking_assert (TREE_CONSTANT (t
));
8245 /* This isn't actually constant, so unset TREE_CONSTANT.
8246 Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires
8247 it to be set if it is invariant address, even when it is not
8248 a valid C++ constant expression. Wrap it with a NOP_EXPR
8250 if (EXPR_P (t
) && TREE_CODE (t
) != ADDR_EXPR
)
8252 else if (TREE_CODE (t
) == CONSTRUCTOR
)
8253 t
= build1 (VIEW_CONVERT_EXPR
, TREE_TYPE (t
), t
);
8255 t
= build_nop (TREE_TYPE (t
), t
);
8256 TREE_CONSTANT (t
) = false;
8260 /* ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
8261 STRICT has the same sense as for constant_value_1: true if we only allow
8262 conforming C++ constant expressions, or false if we want a constant value
8263 even if it doesn't conform.
8264 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
8265 per P0595 even when ALLOW_NON_CONSTANT is true.
8266 CONSTEXPR_DTOR is true when evaluating the dtor of a constexpr variable.
8267 OBJECT must be non-NULL in that case. */
8270 cxx_eval_outermost_constant_expr (tree t
, bool allow_non_constant
,
8272 mce_value manifestly_const_eval
= mce_unknown
,
8273 bool constexpr_dtor
= false,
8274 tree object
= NULL_TREE
)
8276 auto_timevar
time (TV_CONSTEXPR
);
8278 bool non_constant_p
= false;
8279 bool overflow_p
= false;
8281 if (BRACE_ENCLOSED_INITIALIZER_P (t
))
8283 gcc_checking_assert (allow_non_constant
);
8287 constexpr_global_ctx global_ctx
;
8288 constexpr_ctx ctx
= { &global_ctx
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
8289 allow_non_constant
, strict
,
8290 !allow_non_constant
? mce_true
: manifestly_const_eval
};
8292 /* Turn off -frounding-math for manifestly constant evaluation. */
8293 warning_sentinel
rm (flag_rounding_math
,
8294 ctx
.manifestly_const_eval
== mce_true
);
8295 tree type
= initialized_type (t
);
8297 bool is_consteval
= false;
8298 if (VOID_TYPE_P (type
))
8301 /* Used for destructors of array elements. */
8302 type
= TREE_TYPE (object
);
8305 if (cxx_dialect
< cxx20
)
8307 if (TREE_CODE (t
) != CALL_EXPR
&& TREE_CODE (t
) != AGGR_INIT_EXPR
)
8309 /* Calls to immediate functions returning void need to be
8311 tree fndecl
= cp_get_callee_fndecl_nofold (t
);
8312 if (fndecl
== NULL_TREE
|| !DECL_IMMEDIATE_FUNCTION_P (fndecl
))
8315 is_consteval
= true;
8318 else if (cxx_dialect
>= cxx20
8319 && (TREE_CODE (t
) == CALL_EXPR
8320 || TREE_CODE (t
) == AGGR_INIT_EXPR
8321 || TREE_CODE (t
) == TARGET_EXPR
))
8323 /* For non-concept checks, determine if it is consteval. */
8324 if (!concept_check_p (t
))
8327 if (TREE_CODE (x
) == TARGET_EXPR
)
8328 x
= TARGET_EXPR_INITIAL (x
);
8329 tree fndecl
= cp_get_callee_fndecl_nofold (x
);
8330 if (fndecl
&& DECL_IMMEDIATE_FUNCTION_P (fndecl
))
8331 is_consteval
= true;
8334 if (AGGREGATE_TYPE_P (type
) || VECTOR_TYPE_P (type
))
8336 /* In C++14 an NSDMI can participate in aggregate initialization,
8337 and can refer to the address of the object being initialized, so
8338 we need to pass in the relevant VAR_DECL if we want to do the
8339 evaluation in a single pass. The evaluation will dynamically
8340 update ctx.values for the VAR_DECL. We use the same strategy
8341 for C++11 constexpr constructors that refer to the object being
8345 gcc_assert (object
&& VAR_P (object
));
8346 gcc_assert (DECL_DECLARED_CONSTEXPR_P (object
));
8347 gcc_assert (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object
));
8348 if (error_operand_p (DECL_INITIAL (object
)))
8350 ctx
.ctor
= unshare_expr (DECL_INITIAL (object
));
8351 TREE_READONLY (ctx
.ctor
) = false;
8352 /* Temporarily force decl_really_constant_value to return false
8353 for it, we want to use ctx.ctor for the current value instead. */
8354 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object
) = false;
8358 ctx
.ctor
= build_constructor (type
, NULL
);
8359 CONSTRUCTOR_NO_CLEARING (ctx
.ctor
) = true;
8363 if (TREE_CODE (t
) == TARGET_EXPR
)
8364 object
= TARGET_EXPR_SLOT (t
);
8365 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
)
8366 object
= AGGR_INIT_EXPR_SLOT (t
);
8368 ctx
.object
= object
;
8370 gcc_assert (same_type_ignoring_top_level_qualifiers_p
8371 (type
, TREE_TYPE (object
)));
8372 if (object
&& DECL_P (object
))
8373 global_ctx
.put_value (object
, ctx
.ctor
);
8374 if (TREE_CODE (r
) == TARGET_EXPR
)
8375 /* Avoid creating another CONSTRUCTOR when we expand the
8377 r
= TARGET_EXPR_INITIAL (r
);
8380 auto_vec
<tree
, 16> cleanups
;
8381 global_ctx
.cleanups
= &cleanups
;
8383 if (manifestly_const_eval
== mce_true
)
8384 instantiate_constexpr_fns (r
);
8385 r
= cxx_eval_constant_expression (&ctx
, r
, vc_prvalue
,
8386 &non_constant_p
, &overflow_p
);
8388 if (!constexpr_dtor
)
8389 verify_constant (r
, allow_non_constant
, &non_constant_p
, &overflow_p
);
8391 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object
) = true;
8395 /* Evaluate the cleanups. */
8396 FOR_EACH_VEC_ELT_REVERSE (cleanups
, i
, cleanup
)
8397 cxx_eval_constant_expression (&ctx
, cleanup
, vc_discard
,
8398 &non_constant_p
, &overflow_p
);
8400 /* Mutable logic is a bit tricky: we want to allow initialization of
8401 constexpr variables with mutable members, but we can't copy those
8402 members to another constexpr variable. */
8403 if (TREE_CODE (r
) == CONSTRUCTOR
&& CONSTRUCTOR_MUTABLE_POISON (r
))
8405 if (!allow_non_constant
)
8406 error ("%qE is not a constant expression because it refers to "
8407 "mutable subobjects of %qT", t
, type
);
8408 non_constant_p
= true;
8411 if (TREE_CODE (r
) == CONSTRUCTOR
&& CONSTRUCTOR_NO_CLEARING (r
))
8413 if (!allow_non_constant
)
8414 error ("%qE is not a constant expression because it refers to "
8415 "an incompletely initialized variable", t
);
8416 TREE_CONSTANT (r
) = false;
8417 non_constant_p
= true;
8420 if (!non_constant_p
&& cxx_dialect
>= cxx20
8421 && !global_ctx
.heap_vars
.is_empty ())
8423 tree heap_var
= cp_walk_tree_without_duplicates (&r
, find_heap_var_refs
,
8428 if (!allow_non_constant
&& !non_constant_p
)
8429 error_at (DECL_SOURCE_LOCATION (heap_var
),
8430 "%qE is not a constant expression because it refers to "
8431 "a result of %<operator new%>", t
);
8433 non_constant_p
= true;
8435 FOR_EACH_VEC_ELT (global_ctx
.heap_vars
, i
, heap_var
)
8437 if (DECL_NAME (heap_var
) != heap_deleted_identifier
)
8439 if (!allow_non_constant
&& !non_constant_p
)
8440 error_at (DECL_SOURCE_LOCATION (heap_var
),
8441 "%qE is not a constant expression because allocated "
8442 "storage has not been deallocated", t
);
8444 non_constant_p
= true;
8446 varpool_node::get (heap_var
)->remove ();
8450 /* Check that immediate invocation does not return an expression referencing
8451 any immediate function decls. */
8452 if (!non_constant_p
&& cxx_dialect
>= cxx20
)
8453 if (tree immediate_fndecl
8454 = cp_walk_tree_without_duplicates (&r
, find_immediate_fndecl
,
8457 if (!allow_non_constant
&& !non_constant_p
)
8460 error_at (cp_expr_loc_or_input_loc (t
),
8461 "immediate evaluation returns address of immediate "
8462 "function %qD", immediate_fndecl
);
8464 error_at (cp_expr_loc_or_input_loc (t
),
8465 "constant evaluation returns address of immediate "
8466 "function %qD", immediate_fndecl
);
8469 non_constant_p
= true;
8473 /* If we saw something bad, go back to our argument. The wrapping below is
8474 only for the cases of TREE_CONSTANT argument or overflow. */
8477 if (!non_constant_p
&& overflow_p
)
8478 non_constant_p
= true;
8480 /* Unshare the result. */
8481 bool should_unshare
= true;
8482 if (r
== t
|| (TREE_CODE (t
) == TARGET_EXPR
8483 && TARGET_EXPR_INITIAL (t
) == r
))
8484 should_unshare
= false;
8486 if (non_constant_p
&& !allow_non_constant
)
8487 return error_mark_node
;
8488 else if (constexpr_dtor
)
8490 else if (non_constant_p
&& TREE_CONSTANT (r
))
8491 r
= mark_non_constant (r
);
8492 else if (non_constant_p
)
8496 r
= unshare_expr (r
);
8498 if (TREE_CODE (r
) == CONSTRUCTOR
&& CLASS_TYPE_P (TREE_TYPE (r
)))
8500 r
= adjust_temp_type (type
, r
);
8501 if (TREE_CODE (t
) == TARGET_EXPR
8502 && TARGET_EXPR_INITIAL (t
) == r
)
8504 else if (TREE_CODE (t
) == CONSTRUCTOR
|| TREE_CODE (t
) == CALL_EXPR
)
8505 /* Don't add a TARGET_EXPR if our argument didn't have one. */;
8506 else if (TREE_CODE (t
) == TARGET_EXPR
&& TARGET_EXPR_CLEANUP (t
))
8507 r
= get_target_expr (r
);
8510 r
= get_target_expr (r
, tf_warning_or_error
| tf_no_cleanup
);
8511 TREE_CONSTANT (r
) = true;
8515 if (TREE_CODE (t
) == TARGET_EXPR
8516 && TREE_CODE (r
) == TARGET_EXPR
)
8518 /* Preserve this flag for potential_constant_expression, and the others
8519 for good measure. */
8520 TARGET_EXPR_ELIDING_P (r
) = TARGET_EXPR_ELIDING_P (t
);
8521 TARGET_EXPR_IMPLICIT_P (r
) = TARGET_EXPR_IMPLICIT_P (t
);
8522 TARGET_EXPR_LIST_INIT_P (r
) = TARGET_EXPR_LIST_INIT_P (t
);
8523 TARGET_EXPR_DIRECT_INIT_P (r
) = TARGET_EXPR_DIRECT_INIT_P (t
);
8526 /* Remember the original location if that wouldn't need a wrapper. */
8527 if (location_t loc
= EXPR_LOCATION (t
))
8528 protected_set_expr_location (r
, loc
);
8533 /* If T represents a constant expression returns its reduced value.
8534 Otherwise return error_mark_node. */
8537 cxx_constant_value (tree t
, tree decl
/* = NULL_TREE */,
8538 tsubst_flags_t complain
/* = tf_error */)
8540 bool sfinae
= !(complain
& tf_error
);
8541 tree r
= cxx_eval_outermost_constant_expr (t
, sfinae
, true, mce_true
, false, decl
);
8542 if (sfinae
&& !TREE_CONSTANT (r
))
8543 r
= error_mark_node
;
8547 /* Like cxx_constant_value, but used for evaluation of constexpr destructors
8548 of constexpr variables. The actual initializer of DECL is not modified. */
8551 cxx_constant_dtor (tree t
, tree decl
)
8553 cxx_eval_outermost_constant_expr (t
, false, true, mce_true
, true, decl
);
8556 /* Helper routine for fold_simple function. Either return simplified
8557 expression T, otherwise NULL_TREE.
8558 In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold
8559 even if we are within template-declaration. So be careful on call, as in
8560 such case types can be undefined. */
8563 fold_simple_1 (tree t
)
8566 enum tree_code code
= TREE_CODE (t
);
8578 return fold_sizeof_expr (t
);
8587 case TRUTH_NOT_EXPR
:
8588 case VIEW_CONVERT_EXPR
:
8591 case FIX_TRUNC_EXPR
:
8592 case FIXED_CONVERT_EXPR
:
8593 case ADDR_SPACE_CONVERT_EXPR
:
8595 op1
= TREE_OPERAND (t
, 0);
8597 t
= const_unop (code
, TREE_TYPE (t
), op1
);
8601 if (CONVERT_EXPR_CODE_P (code
)
8602 && TREE_OVERFLOW_P (t
) && !TREE_OVERFLOW_P (op1
))
8603 TREE_OVERFLOW (t
) = false;
8611 /* If T is a simple constant expression, returns its simplified value.
8612 Otherwise returns T. In contrast to maybe_constant_value we
8613 simplify only few operations on constant-expressions, and we don't
8614 try to simplify constexpressions. */
8617 fold_simple (tree t
)
8619 if (processing_template_decl
)
8622 tree r
= fold_simple_1 (t
);
8629 /* Try folding the expression T to a simple constant.
8630 Returns that constant, otherwise returns T. */
8633 fold_to_constant (tree t
)
8636 if (CONSTANT_CLASS_P (r
) && !TREE_OVERFLOW (r
))
8642 /* If T is a constant expression, returns its reduced value.
8643 Otherwise, if T does not have TREE_CONSTANT set, returns T.
8644 Otherwise, returns a version of T without TREE_CONSTANT.
8645 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated
8648 static GTY((deletable
)) hash_map
<tree
, tree
> *cv_cache
;
8651 maybe_constant_value (tree t
, tree decl
/* = NULL_TREE */,
8652 mce_value manifestly_const_eval
/* = mce_unknown */)
8656 if (!is_nondependent_constant_expression (t
))
8658 if (TREE_OVERFLOW_P (t
)
8659 || (!processing_template_decl
&& TREE_CONSTANT (t
)))
8660 t
= mark_non_constant (t
);
8663 else if (CONSTANT_CLASS_P (t
))
8664 /* No caching or evaluation needed. */
8667 /* Don't constant evaluate an unevaluated non-manifestly-constant operand,
8668 but at least try folding it to a simple constant. */
8669 if (cp_unevaluated_operand
&& manifestly_const_eval
!= mce_true
)
8670 return fold_to_constant (t
);
8672 if (manifestly_const_eval
!= mce_unknown
)
8673 return cxx_eval_outermost_constant_expr (t
, true, true,
8674 manifestly_const_eval
, false, decl
);
8676 if (cv_cache
== NULL
)
8677 cv_cache
= hash_map
<tree
, tree
>::create_ggc (101);
8678 if (tree
*cached
= cv_cache
->get (t
))
8683 /* Clear processing_template_decl for sake of break_out_target_exprs;
8684 entries in the cv_cache are non-templated. */
8685 processing_template_decl_sentinel ptds
;
8687 r
= break_out_target_exprs (r
, /*clear_loc*/true);
8688 protected_set_expr_location (r
, EXPR_LOCATION (t
));
8693 uid_sensitive_constexpr_evaluation_checker c
;
8694 r
= cxx_eval_outermost_constant_expr (t
, true, true,
8695 manifestly_const_eval
, false, decl
);
8696 gcc_checking_assert (r
== t
8697 || CONVERT_EXPR_P (t
)
8698 || TREE_CODE (t
) == VIEW_CONVERT_EXPR
8699 || (TREE_CONSTANT (t
) && !TREE_CONSTANT (r
))
8700 || !cp_tree_equal (r
, t
));
8701 if (!c
.evaluation_restricted_p ())
8702 cv_cache
->put (t
, r
);
8706 /* Dispose of the whole CV_CACHE. */
8709 clear_cv_cache (void)
8711 if (cv_cache
!= NULL
)
8715 /* Dispose of the whole CV_CACHE and FOLD_CACHE. */
8718 clear_cv_and_fold_caches ()
8721 clear_fold_cache ();
8724 /* Internal function handling expressions in templates for
8725 fold_non_dependent_expr and fold_non_dependent_init.
8727 If we're in a template, but T isn't value dependent, simplify
8728 it. We're supposed to treat:
8730 template <typename T> void f(T[1 + 1]);
8731 template <typename T> void f(T[2]);
8733 as two declarations of the same function, for example. */
8736 fold_non_dependent_expr_template (tree t
, tsubst_flags_t complain
,
8737 bool manifestly_const_eval
,
8740 gcc_assert (processing_template_decl
);
8742 if (is_nondependent_constant_expression (t
))
8744 processing_template_decl_sentinel s
;
8745 t
= instantiate_non_dependent_expr_internal (t
, complain
);
8747 if (type_unknown_p (t
) || BRACE_ENCLOSED_INITIALIZER_P (t
))
8749 if (TREE_OVERFLOW_P (t
))
8751 t
= build_nop (TREE_TYPE (t
), t
);
8752 TREE_CONSTANT (t
) = false;
8756 else if (CONSTANT_CLASS_P (t
))
8757 /* No evaluation needed. */
8760 /* Don't constant evaluate an unevaluated non-manifestly-constant operand,
8761 but at least try folding it to a simple constant. */
8762 if (cp_unevaluated_operand
&& !manifestly_const_eval
)
8763 return fold_to_constant (t
);
8765 tree r
= cxx_eval_outermost_constant_expr (t
, true, true,
8766 mce_value (manifestly_const_eval
),
8768 /* cp_tree_equal looks through NOPs, so allow them. */
8769 gcc_checking_assert (r
== t
8770 || CONVERT_EXPR_P (t
)
8771 || TREE_CODE (t
) == VIEW_CONVERT_EXPR
8772 || (TREE_CONSTANT (t
) && !TREE_CONSTANT (r
))
8773 || !cp_tree_equal (r
, t
));
8776 else if (TREE_OVERFLOW_P (t
))
8778 t
= build_nop (TREE_TYPE (t
), t
);
8779 TREE_CONSTANT (t
) = false;
8785 /* Like maybe_constant_value but first fully instantiate the argument.
8787 Note: this is equivalent to instantiate_non_dependent_expr (t, complain)
8788 followed by maybe_constant_value but is more efficient,
8789 because it calls instantiation_dependent_expression_p and
8790 potential_constant_expression at most once.
8791 The manifestly_const_eval argument is passed to maybe_constant_value.
8793 Callers should generally pass their active complain, or if they are in a
8794 non-template, diagnosing context, they can use the default of
8795 tf_warning_or_error. Callers that might be within a template context, don't
8796 have a complain parameter, and aren't going to remember the result for long
8797 (e.g. null_ptr_cst_p), can pass tf_none and deal with error_mark_node
8801 fold_non_dependent_expr (tree t
,
8802 tsubst_flags_t complain
/* = tf_warning_or_error */,
8803 bool manifestly_const_eval
/* = false */,
8804 tree object
/* = NULL_TREE */)
8809 if (processing_template_decl
)
8810 return fold_non_dependent_expr_template (t
, complain
,
8811 manifestly_const_eval
, object
);
8813 return maybe_constant_value (t
, object
, mce_value (manifestly_const_eval
));
8816 /* Like fold_non_dependent_expr, but if EXPR couldn't be folded to a constant,
8817 return the original expression. */
8820 maybe_fold_non_dependent_expr (tree expr
,
8821 tsubst_flags_t complain
/*=tf_warning_or_error*/)
8823 tree t
= fold_non_dependent_expr (expr
, complain
);
8824 if (t
&& TREE_CONSTANT (t
))
8830 /* Like maybe_constant_init but first fully instantiate the argument. */
8833 fold_non_dependent_init (tree t
,
8834 tsubst_flags_t complain
/*=tf_warning_or_error*/,
8835 bool manifestly_const_eval
/*=false*/,
8836 tree object
/* = NULL_TREE */)
8841 if (processing_template_decl
)
8843 t
= fold_non_dependent_expr_template (t
, complain
,
8844 manifestly_const_eval
, object
);
8845 /* maybe_constant_init does this stripping, so do it here too. */
8846 if (TREE_CODE (t
) == TARGET_EXPR
)
8848 tree init
= TARGET_EXPR_INITIAL (t
);
8849 if (TREE_CODE (init
) == CONSTRUCTOR
)
8855 return maybe_constant_init (t
, object
, manifestly_const_eval
);
8858 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
8859 than wrapped in a TARGET_EXPR.
8860 ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
8861 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
8862 per P0595 even when ALLOW_NON_CONSTANT is true. */
8865 maybe_constant_init_1 (tree t
, tree decl
, bool allow_non_constant
,
8866 bool manifestly_const_eval
)
8870 if (TREE_CODE (t
) == EXPR_STMT
)
8871 t
= TREE_OPERAND (t
, 0);
8872 if (TREE_CODE (t
) == CONVERT_EXPR
8873 && VOID_TYPE_P (TREE_TYPE (t
)))
8874 t
= TREE_OPERAND (t
, 0);
8875 if (TREE_CODE (t
) == INIT_EXPR
)
8876 t
= TREE_OPERAND (t
, 1);
8877 if (TREE_CODE (t
) == TARGET_EXPR
)
8878 t
= TARGET_EXPR_INITIAL (t
);
8879 if (!is_nondependent_static_init_expression (t
))
8880 /* Don't try to evaluate it. */;
8881 else if (CONSTANT_CLASS_P (t
) && TREE_CODE (t
) != PTRMEM_CST
)
8882 /* No evaluation needed. PTRMEM_CST needs the immediate fn check. */;
8885 /* [basic.start.static] allows constant-initialization of variables with
8886 static or thread storage duration even if it isn't required, but we
8887 shouldn't bend the rules the same way for automatic variables. */
8888 bool is_static
= (decl
&& DECL_P (decl
)
8889 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)));
8891 manifestly_const_eval
= true;
8893 if (cp_unevaluated_operand
&& !manifestly_const_eval
)
8894 return fold_to_constant (t
);
8896 t
= cxx_eval_outermost_constant_expr (t
, allow_non_constant
, !is_static
,
8897 mce_value (manifestly_const_eval
),
8900 if (TREE_CODE (t
) == TARGET_EXPR
)
8902 tree init
= TARGET_EXPR_INITIAL (t
);
8903 if (TREE_CODE (init
) == CONSTRUCTOR
)
8909 /* Wrapper for maybe_constant_init_1 which permits non constants. */
8912 maybe_constant_init (tree t
, tree decl
, bool manifestly_const_eval
)
8914 return maybe_constant_init_1 (t
, decl
, true, manifestly_const_eval
);
8917 /* Wrapper for maybe_constant_init_1 which does not permit non constants. */
8920 cxx_constant_init (tree t
, tree decl
)
8922 return maybe_constant_init_1 (t
, decl
, false, true);
8926 /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
8927 /* Return true if the object referred to by REF has automatic or thread
8930 enum { ck_ok
, ck_bad
, ck_unknown
};
8932 check_automatic_or_tls (tree ref
)
8935 poly_int64 bitsize
, bitpos
;
8937 int volatilep
= 0, unsignedp
= 0;
8938 tree decl
= get_inner_reference (ref
, &bitsize
, &bitpos
, &offset
,
8939 &mode
, &unsignedp
, &volatilep
, false);
8942 /* If there isn't a decl in the middle, we don't know the linkage here,
8943 and this isn't a constant expression anyway. */
8946 dk
= decl_storage_duration (decl
);
8947 return (dk
== dk_auto
|| dk
== dk_thread
) ? ck_bad
: ck_ok
;
8951 /* Data structure for passing data from potential_constant_expression_1
8952 to check_for_return_continue via cp_walk_tree. */
8953 struct check_for_return_continue_data
{
8954 hash_set
<tree
> *pset
;
8959 /* Helper function for potential_constant_expression_1 SWITCH_STMT handling,
8960 called through cp_walk_tree. Return the first RETURN_EXPR found, or note
8961 the first CONTINUE_STMT and/or BREAK_STMT if RETURN_EXPR is not found. */
8963 check_for_return_continue (tree
*tp
, int *walk_subtrees
, void *data
)
8966 check_for_return_continue_data
*d
= (check_for_return_continue_data
*) data
;
8967 switch (TREE_CODE (t
))
8973 if (d
->continue_stmt
== NULL_TREE
)
8974 d
->continue_stmt
= t
;
8978 if (d
->break_stmt
== NULL_TREE
)
8983 if (tree r = cp_walk_tree (&x, check_for_return_continue, data, \
8987 /* For loops, walk subtrees manually, so that continue stmts found
8988 inside of the bodies of the loops are ignored. */
8991 RECUR (DO_COND (t
));
8992 s
= d
->continue_stmt
;
8994 RECUR (DO_BODY (t
));
8995 d
->continue_stmt
= s
;
9001 RECUR (WHILE_COND (t
));
9002 s
= d
->continue_stmt
;
9004 RECUR (WHILE_BODY (t
));
9005 d
->continue_stmt
= s
;
9011 RECUR (FOR_INIT_STMT (t
));
9012 RECUR (FOR_COND (t
));
9013 RECUR (FOR_EXPR (t
));
9014 s
= d
->continue_stmt
;
9016 RECUR (FOR_BODY (t
));
9017 d
->continue_stmt
= s
;
9021 case RANGE_FOR_STMT
:
9023 RECUR (RANGE_FOR_EXPR (t
));
9024 s
= d
->continue_stmt
;
9026 RECUR (RANGE_FOR_BODY (t
));
9027 d
->continue_stmt
= s
;
9033 RECUR (SWITCH_STMT_COND (t
));
9035 RECUR (SWITCH_STMT_BODY (t
));
9040 case STATEMENT_LIST
:
9053 /* Return true if T denotes a potentially constant expression. Issue
9054 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
9055 an lvalue-rvalue conversion is implied. If NOW is true, we want to
9056 consider the expression in the current context, independent of constexpr
9057 substitution. If FUNDEF_P is true, we're checking a constexpr function body
9058 and hard errors should not be reported by constexpr_error.
9060 C++0x [expr.const] used to say
9062 6 An expression is a potential constant expression if it is
9063 a constant expression where all occurrences of function
9064 parameters are replaced by arbitrary constant expressions
9065 of the appropriate type.
9067 2 A conditional expression is a constant expression unless it
9068 involves one of the following as a potentially evaluated
9069 subexpression (3.2), but subexpressions of logical AND (5.14),
9070 logical OR (5.15), and conditional (5.16) operations that are
9071 not evaluated are not considered. */
9074 potential_constant_expression_1 (tree t
, bool want_rval
, bool strict
, bool now
,
9075 bool fundef_p
, tsubst_flags_t flags
,
9078 #define RECUR(T,RV) \
9079 potential_constant_expression_1 ((T), (RV), strict, now, fundef_p, flags, \
9082 enum { any
= false, rval
= true };
9086 if (t
== error_mark_node
)
9090 location_t loc
= cp_expr_loc_or_input_loc (t
);
9093 /* If we are jumping, ignore everything. This is simpler than the
9094 cxx_eval_constant_expression handling because we only need to be
9095 conservatively correct, and we don't necessarily have a constant value
9096 available, so we don't bother with switch tracking. */
9099 if (TREE_THIS_VOLATILE (t
) && want_rval
)
9101 if (flags
& tf_error
)
9102 constexpr_error (loc
, fundef_p
, "lvalue-to-rvalue conversion of "
9103 "a volatile lvalue %qE with type %qT", t
,
9107 if (CONSTANT_CLASS_P (t
))
9109 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_TYPED
)
9110 && TREE_TYPE (t
) == error_mark_node
)
9113 switch (TREE_CODE (t
))
9119 case TEMPLATE_ID_EXPR
:
9121 case CASE_LABEL_EXPR
:
9128 case TEMPLATE_PARM_INDEX
:
9130 case IDENTIFIER_NODE
:
9131 case USERDEF_LITERAL
:
9132 /* We can see a FIELD_DECL in a pointer-to-member expression. */
9137 case PLACEHOLDER_EXPR
:
9140 case DEBUG_BEGIN_STMT
:
9144 if (!RECUR (TREE_OPERAND (t
, 0), any
))
9154 if (now
&& want_rval
)
9156 tree type
= TREE_TYPE (t
);
9157 if (dependent_type_p (type
)
9158 || !COMPLETE_TYPE_P (processing_template_decl
9159 ? type
: complete_type (type
))
9160 || is_really_empty_class (type
, /*ignore_vptr*/false))
9161 /* An empty class has no data to read. */
9163 if (flags
& tf_error
)
9164 constexpr_error (input_location
, fundef_p
,
9165 "%qE is not a constant expression", t
);
9170 case AGGR_INIT_EXPR
:
9172 /* -- an invocation of a function other than a constexpr function
9173 or a constexpr constructor. */
9175 tree fun
= get_function_named_in_call (t
);
9176 const int nargs
= call_expr_nargs (t
);
9179 if (fun
== NULL_TREE
)
9181 /* Reset to allow the function to continue past the end
9182 of the block below. Otherwise return early. */
9185 if (TREE_CODE (t
) == CALL_EXPR
9186 && CALL_EXPR_FN (t
) == NULL_TREE
)
9187 switch (CALL_EXPR_IFN (t
))
9189 /* These should be ignored, they are optimized away from
9190 constexpr functions. */
9191 case IFN_UBSAN_NULL
:
9192 case IFN_UBSAN_BOUNDS
:
9193 case IFN_UBSAN_VPTR
:
9194 case IFN_FALLTHROUGH
:
9198 case IFN_ADD_OVERFLOW
:
9199 case IFN_SUB_OVERFLOW
:
9200 case IFN_MUL_OVERFLOW
:
9202 case IFN_VEC_CONVERT
:
9212 /* fold_call_expr can't do anything with IFN calls. */
9213 if (flags
& tf_error
)
9214 constexpr_error (loc
, fundef_p
,
9215 "call to internal function %qE", t
);
9220 if (fun
&& is_overloaded_fn (fun
))
9222 if (!RECUR (fun
, true))
9224 fun
= get_fns (fun
);
9226 if (TREE_CODE (fun
) == FUNCTION_DECL
)
9228 if (builtin_valid_in_constant_expr_p (fun
))
9230 if (!maybe_constexpr_fn (fun
)
9231 /* Allow any built-in function; if the expansion
9232 isn't constant, we'll deal with that then. */
9233 && !fndecl_built_in_p (fun
)
9234 /* In C++20, replaceable global allocation functions
9235 are constant expressions. */
9236 && (!cxx_replaceable_global_alloc_fn (fun
)
9237 || TREE_CODE (t
) != CALL_EXPR
9238 || (!CALL_FROM_NEW_OR_DELETE_P (t
)
9239 && (current_function_decl
== NULL_TREE
9240 || !is_std_allocator_allocate
9241 (current_function_decl
))))
9242 /* Allow placement new in std::construct_at. */
9243 && (!cxx_placement_new_fn (fun
)
9244 || TREE_CODE (t
) != CALL_EXPR
9245 || current_function_decl
== NULL_TREE
9246 || !is_std_construct_at (current_function_decl
))
9247 && !cxx_dynamic_cast_fn_p (fun
))
9249 if ((flags
& tf_error
)
9250 && constexpr_error (loc
, fundef_p
,
9251 "call to non-%<constexpr%> "
9252 "function %qD", fun
))
9253 explain_invalid_constexpr_fn (fun
);
9258 fun
= OVL_FIRST (fun
);
9259 /* Skip initial arguments to base constructors. */
9260 if (DECL_BASE_CONSTRUCTOR_P (fun
))
9261 i
= num_artificial_parms_for (fun
);
9265 if (RECUR (fun
, FUNCTION_POINTER_TYPE_P (fun
) ? rval
: any
))
9266 /* Might end up being a constant function pointer. But it
9267 could also be a function object with constexpr op(), so
9268 we pass 'any' so that the underlying VAR_DECL is deemed
9269 as potentially-constant even though it wasn't declared
9274 for (; i
< nargs
; ++i
)
9276 tree x
= get_nth_callarg (t
, i
);
9277 /* In a template, reference arguments haven't been converted to
9278 REFERENCE_TYPE and we might not even know if the parameter
9279 is a reference, so accept lvalue constants too. */
9280 bool rv
= processing_template_decl
? any
: rval
;
9281 /* Don't require an immediately constant value, as constexpr
9282 substitution might not use the value of the argument. */
9283 bool sub_now
= false;
9284 if (!potential_constant_expression_1 (x
, rv
, strict
,
9285 sub_now
, fundef_p
, flags
,
9292 case NON_LVALUE_EXPR
:
9293 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
9294 -- an lvalue of integral type that refers to a non-volatile
9295 const variable or static data member initialized with
9296 constant expressions, or
9298 -- an lvalue of literal type that refers to non-volatile
9299 object defined with constexpr, or that refers to a
9300 sub-object of such an object; */
9301 return RECUR (TREE_OPERAND (t
, 0), rval
);
9303 case EXCESS_PRECISION_EXPR
:
9304 return RECUR (TREE_OPERAND (t
, 0), rval
);
9307 if (DECL_HAS_VALUE_EXPR_P (t
))
9309 if (now
&& is_normal_capture_proxy (t
))
9311 /* -- in a lambda-expression, a reference to this or to a
9312 variable with automatic storage duration defined outside that
9313 lambda-expression, where the reference would be an
9317 /* Since we're doing an lvalue-rvalue conversion, this might
9318 not be an odr-use, so evaluate the variable directly. */
9319 return RECUR (DECL_CAPTURED_VARIABLE (t
), rval
);
9321 if (flags
& tf_error
)
9323 tree cap
= DECL_CAPTURED_VARIABLE (t
);
9324 if (constexpr_error (input_location
, fundef_p
,
9325 "lambda capture of %qE is not a "
9326 "constant expression", cap
)
9327 && decl_constant_var_p (cap
))
9328 inform (input_location
, "because it is used as a glvalue");
9332 /* Treat __PRETTY_FUNCTION__ inside a template function as
9333 potentially-constant. */
9334 else if (DECL_PRETTY_FUNCTION_P (t
)
9335 && DECL_VALUE_EXPR (t
) == error_mark_node
)
9337 return RECUR (DECL_VALUE_EXPR (t
), rval
);
9340 && !var_in_maybe_constexpr_fn (t
)
9341 && !type_dependent_expression_p (t
)
9342 && !decl_maybe_constant_var_p (t
)
9344 || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t
))
9345 || (DECL_INITIAL (t
)
9346 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t
)))
9347 && COMPLETE_TYPE_P (TREE_TYPE (t
))
9348 && !is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
9350 if (flags
& tf_error
)
9351 non_const_var_error (loc
, t
, fundef_p
);
9357 if (REINTERPRET_CAST_P (t
))
9359 if (flags
& tf_error
)
9360 constexpr_error (loc
, fundef_p
, "%<reinterpret_cast%> is not a "
9361 "constant expression");
9366 case VIEW_CONVERT_EXPR
:
9367 /* -- a reinterpret_cast. FIXME not implemented, and this rule
9368 may change to something more specific to type-punning (DR 1312). */
9370 tree from
= TREE_OPERAND (t
, 0);
9371 if (location_wrapper_p (t
))
9373 iloc_sentinel ils
= loc
;
9374 return (RECUR (from
, want_rval
));
9376 if (INDIRECT_TYPE_P (TREE_TYPE (t
)))
9378 STRIP_ANY_LOCATION_WRAPPER (from
);
9379 if (TREE_CODE (from
) == INTEGER_CST
9380 && !integer_zerop (from
))
9382 if (flags
& tf_error
)
9383 constexpr_error (loc
, fundef_p
,
9384 "%<reinterpret_cast%> from integer to "
9389 return (RECUR (from
, TREE_CODE (t
) != VIEW_CONVERT_EXPR
));
9392 case ADDRESSOF_EXPR
:
9393 /* This is like ADDR_EXPR, except it won't form pointer-to-member. */
9394 t
= TREE_OPERAND (t
, 0);
9395 goto handle_addr_expr
;
9398 /* -- a unary operator & that is applied to an lvalue that
9399 designates an object with thread or automatic storage
9401 t
= TREE_OPERAND (t
, 0);
9403 if (TREE_CODE (t
) == OFFSET_REF
&& PTRMEM_OK_P (t
))
9404 /* A pointer-to-member constant. */
9409 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
9410 any checking here, as we might dereference the pointer later. If
9411 we remove this code, also remove check_automatic_or_tls. */
9412 i
= check_automatic_or_tls (t
);
9417 if (flags
& tf_error
)
9418 error ("address-of an object %qE with thread local or "
9419 "automatic storage is not a constant expression", t
);
9423 return RECUR (t
, any
);
9428 /* -- a class member access unless its postfix-expression is
9429 of literal type or of pointer to literal type. */
9430 /* This test would be redundant, as it follows from the
9431 postfix-expression being a potential constant expression. */
9432 if (type_unknown_p (t
))
9434 if (is_overloaded_fn (t
))
9435 /* In a template, a COMPONENT_REF of a function expresses ob.fn(),
9436 which uses ob as an lvalue. */
9443 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
9445 case EXPR_PACK_EXPANSION
:
9446 return RECUR (PACK_EXPANSION_PATTERN (t
), want_rval
);
9450 tree x
= TREE_OPERAND (t
, 0);
9452 if (is_this_parameter (x
) && !is_capture_proxy (x
))
9454 if (!var_in_maybe_constexpr_fn (x
))
9456 if (flags
& tf_error
)
9457 constexpr_error (loc
, fundef_p
, "use of %<this%> in a "
9458 "constant expression");
9463 return RECUR (x
, rval
);
9466 case STATEMENT_LIST
:
9467 for (tree stmt
: tsi_range (t
))
9468 if (!RECUR (stmt
, any
))
9473 if (cxx_dialect
< cxx14
)
9475 if (!RECUR (TREE_OPERAND (t
, 0), any
))
9477 /* Just ignore clobbers. */
9478 if (TREE_CLOBBER_P (TREE_OPERAND (t
, 1)))
9480 if (!RECUR (TREE_OPERAND (t
, 1), rval
))
9485 if (cxx_dialect
< cxx14
)
9487 if (!RECUR (TREE_OPERAND (t
, 0), rval
))
9489 if (!RECUR (TREE_OPERAND (t
, 2), rval
))
9494 if (!RECUR (DO_COND (t
), rval
))
9496 if (!RECUR (DO_BODY (t
), any
))
9498 if (breaks (jump_target
) || continues (jump_target
))
9499 *jump_target
= NULL_TREE
;
9503 if (!RECUR (FOR_INIT_STMT (t
), any
))
9506 if (!RECUR (tmp
, rval
))
9510 if (!processing_template_decl
)
9511 tmp
= cxx_eval_outermost_constant_expr (tmp
, true);
9512 /* If we couldn't evaluate the condition, it might not ever be
9514 if (!integer_onep (tmp
))
9516 /* Before returning true, check if the for body can contain
9518 hash_set
<tree
> pset
;
9519 check_for_return_continue_data data
= { &pset
, NULL_TREE
,
9522 = cp_walk_tree (&FOR_BODY (t
), check_for_return_continue
,
9524 *jump_target
= ret_expr
;
9528 if (!RECUR (FOR_EXPR (t
), any
))
9530 if (!RECUR (FOR_BODY (t
), any
))
9532 if (breaks (jump_target
) || continues (jump_target
))
9533 *jump_target
= NULL_TREE
;
9536 case RANGE_FOR_STMT
:
9537 if (!RECUR (RANGE_FOR_INIT_STMT (t
), any
))
9539 if (!RECUR (RANGE_FOR_EXPR (t
), any
))
9541 if (!RECUR (RANGE_FOR_BODY (t
), any
))
9543 if (breaks (jump_target
) || continues (jump_target
))
9544 *jump_target
= NULL_TREE
;
9548 tmp
= WHILE_COND (t
);
9549 if (!RECUR (tmp
, rval
))
9551 if (!processing_template_decl
)
9552 tmp
= cxx_eval_outermost_constant_expr (tmp
, true);
9553 /* If we couldn't evaluate the condition, it might not ever be true. */
9554 if (!integer_onep (tmp
))
9556 /* Before returning true, check if the while body can contain
9558 hash_set
<tree
> pset
;
9559 check_for_return_continue_data data
= { &pset
, NULL_TREE
,
9562 = cp_walk_tree (&WHILE_BODY (t
), check_for_return_continue
,
9564 *jump_target
= ret_expr
;
9567 if (!RECUR (WHILE_BODY (t
), any
))
9569 if (breaks (jump_target
) || continues (jump_target
))
9570 *jump_target
= NULL_TREE
;
9574 if (!RECUR (SWITCH_STMT_COND (t
), rval
))
9576 /* FIXME we don't check SWITCH_STMT_BODY currently, because even
9577 unreachable labels would be checked and it is enough if there is
9578 a single switch cond value for which it is a valid constant
9579 expression. We need to check if there are any RETURN_EXPRs
9580 or CONTINUE_STMTs inside of the body though, as in that case
9581 we need to set *jump_target. */
9584 hash_set
<tree
> pset
;
9585 check_for_return_continue_data data
= { &pset
, NULL_TREE
,
9588 = cp_walk_tree (&SWITCH_STMT_BODY (t
), check_for_return_continue
,
9590 /* The switch might return. */
9591 *jump_target
= ret_expr
;
9592 else if (data
.continue_stmt
)
9593 /* The switch can't return, but might continue. */
9594 *jump_target
= data
.continue_stmt
;
9599 return RECUR (STMT_EXPR_STMT (t
), rval
);
9602 if (cxx_dialect
>= cxx17
)
9603 /* In C++17 lambdas can be constexpr, don't give up yet. */
9605 else if (flags
& tf_error
)
9606 constexpr_error (loc
, fundef_p
, "lambda-expression is not a "
9607 "constant expression before C++17");
9613 case VEC_DELETE_EXPR
:
9614 if (cxx_dialect
>= cxx20
)
9615 /* In C++20, new-expressions are potentially constant. */
9617 else if (flags
& tf_error
)
9618 constexpr_error (loc
, fundef_p
, "new-expression is not a "
9619 "constant expression before C++20");
9622 case DYNAMIC_CAST_EXPR
:
9623 case PSEUDO_DTOR_EXPR
:
9629 case OMP_DISTRIBUTE
:
9633 case OMP_TARGET_DATA
:
9645 case OMP_TARGET_UPDATE
:
9646 case OMP_TARGET_ENTER_DATA
:
9647 case OMP_TARGET_EXIT_DATA
:
9649 case OMP_ATOMIC_READ
:
9650 case OMP_ATOMIC_CAPTURE_OLD
:
9651 case OMP_ATOMIC_CAPTURE_NEW
:
9657 case OACC_HOST_DATA
:
9661 case OACC_ENTER_DATA
:
9662 case OACC_EXIT_DATA
:
9664 /* GCC internal stuff. */
9666 case TRANSACTION_EXPR
:
9667 case AT_ENCODE_EXPR
:
9669 if (flags
& tf_error
)
9670 constexpr_error (loc
, fundef_p
, "expression %qE is not a constant "
9675 if (flags
& tf_error
)
9676 inline_asm_in_constexpr_error (loc
, fundef_p
);
9680 if (cxx_dialect
>= cxx20
)
9681 /* In C++20 virtual calls can be constexpr, don't give up yet. */
9683 else if (flags
& tf_error
)
9684 constexpr_error (loc
, fundef_p
, "virtual functions cannot be "
9685 "%<constexpr%> before C++20");
9689 /* In C++20, a typeid expression whose operand is of polymorphic
9690 class type can be constexpr. */
9692 tree e
= TREE_OPERAND (t
, 0);
9693 if (cxx_dialect
< cxx20
9696 && !type_dependent_expression_p (e
)
9697 && TYPE_POLYMORPHIC_P (TREE_TYPE (e
)))
9699 if (flags
& tf_error
)
9700 constexpr_error (loc
, fundef_p
, "%<typeid%> is not a "
9701 "constant expression because %qE is "
9702 "of polymorphic type", e
);
9708 case POINTER_DIFF_EXPR
:
9719 case SPACESHIP_EXPR
:
9723 case PREINCREMENT_EXPR
:
9724 case POSTINCREMENT_EXPR
:
9725 case PREDECREMENT_EXPR
:
9726 case POSTDECREMENT_EXPR
:
9727 if (cxx_dialect
< cxx14
)
9733 if (TYPE_P (TREE_OPERAND (t
, 0)))
9739 case FIX_TRUNC_EXPR
:
9744 case TRUTH_NOT_EXPR
:
9745 case FIXED_CONVERT_EXPR
:
9746 case UNARY_PLUS_EXPR
:
9747 case UNARY_LEFT_FOLD_EXPR
:
9748 case UNARY_RIGHT_FOLD_EXPR
:
9750 return RECUR (TREE_OPERAND (t
, 0), rval
);
9753 case CONST_CAST_EXPR
:
9754 case STATIC_CAST_EXPR
:
9755 case REINTERPRET_CAST_EXPR
:
9756 case IMPLICIT_CONV_EXPR
:
9757 if (!cast_valid_in_integral_constant_expression_p (TREE_TYPE (t
)))
9758 /* In C++98, a conversion to non-integral type can't be part of a
9759 constant expression. */
9761 if (flags
& tf_error
)
9762 constexpr_error (loc
, fundef_p
,
9763 "cast to non-integral type %qT in a constant "
9764 "expression", TREE_TYPE (t
));
9767 /* This might be a conversion from a class to a (potentially) literal
9768 type. Let's consider it potentially constant since the conversion
9769 might be a constexpr user-defined conversion. */
9770 else if (cxx_dialect
>= cxx11
9771 && (dependent_type_p (TREE_TYPE (t
))
9772 || !COMPLETE_TYPE_P (TREE_TYPE (t
))
9773 || literal_type_p (TREE_TYPE (t
)))
9774 && TREE_OPERAND (t
, 0))
9776 tree type
= TREE_TYPE (TREE_OPERAND (t
, 0));
9777 /* If this is a dependent type, it could end up being a class
9778 with conversions. */
9779 if (type
== NULL_TREE
|| WILDCARD_TYPE_P (type
))
9781 /* Or a non-dependent class which has conversions. */
9782 else if (CLASS_TYPE_P (type
)
9783 && (TYPE_HAS_CONVERSION (type
) || dependent_scope_p (type
)))
9787 return (RECUR (TREE_OPERAND (t
, 0),
9788 !TYPE_REF_P (TREE_TYPE (t
))));
9791 return RECUR (BIND_EXPR_BODY (t
), want_rval
);
9793 case NON_DEPENDENT_EXPR
:
9794 /* Treat NON_DEPENDENT_EXPR as non-constant: it's not handled by
9795 constexpr evaluation or tsubst, so fold_non_dependent_expr can't
9796 do anything useful with it. And we shouldn't see it in a context
9797 where a constant expression is strictly required, hence the assert. */
9798 gcc_checking_assert (!(flags
& tf_error
));
9801 case CLEANUP_POINT_EXPR
:
9802 case MUST_NOT_THROW_EXPR
:
9803 case TRY_CATCH_EXPR
:
9808 /* For convenience. */
9811 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
9814 tmp
= DECL_EXPR_DECL (t
);
9815 if (VAR_P (tmp
) && !DECL_ARTIFICIAL (tmp
)
9816 && (processing_template_decl
9817 ? !decl_maybe_constant_var_p (tmp
)
9818 : !decl_constant_var_p (tmp
)))
9820 if (CP_DECL_THREAD_LOCAL_P (tmp
) && !DECL_REALLY_EXTERN (tmp
))
9822 if (flags
& tf_error
)
9823 constexpr_error (DECL_SOURCE_LOCATION (tmp
), fundef_p
,
9824 "%qD defined %<thread_local%> in "
9825 "%<constexpr%> context", tmp
);
9828 else if (TREE_STATIC (tmp
))
9830 if (flags
& tf_error
)
9831 constexpr_error (DECL_SOURCE_LOCATION (tmp
), fundef_p
,
9832 "%qD defined %<static%> in %<constexpr%> "
9836 else if (!check_for_uninitialized_const_var
9837 (tmp
, /*constexpr_context_p=*/true, flags
))
9841 return RECUR (DECL_INITIAL (tmp
), want_rval
);
9844 case TRY_FINALLY_EXPR
:
9845 return (RECUR (TREE_OPERAND (t
, 0), want_rval
)
9846 && RECUR (TREE_OPERAND (t
, 1), any
));
9849 return RECUR (TREE_OPERAND (t
, 1), want_rval
);
9852 if (!TARGET_EXPR_DIRECT_INIT_P (t
)
9853 && !TARGET_EXPR_ELIDING_P (t
)
9854 && !literal_type_p (TREE_TYPE (t
)))
9856 if (flags
& tf_error
)
9858 auto_diagnostic_group d
;
9859 if (constexpr_error (loc
, fundef_p
,
9860 "temporary of non-literal type %qT in a "
9861 "constant expression", TREE_TYPE (t
)))
9862 explain_non_literal_class (TREE_TYPE (t
));
9868 return RECUR (TREE_OPERAND (t
, 1), rval
);
9872 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (t
);
9873 constructor_elt
*ce
;
9874 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
9875 if (!RECUR (ce
->value
, want_rval
))
9882 gcc_assert (TREE_PURPOSE (t
) == NULL_TREE
9883 || DECL_P (TREE_PURPOSE (t
)));
9884 if (!RECUR (TREE_VALUE (t
), want_rval
))
9886 if (TREE_CHAIN (t
) == NULL_TREE
)
9888 return RECUR (TREE_CHAIN (t
), want_rval
);
9891 case TRUNC_DIV_EXPR
:
9893 case FLOOR_DIV_EXPR
:
9894 case ROUND_DIV_EXPR
:
9895 case TRUNC_MOD_EXPR
:
9897 case ROUND_MOD_EXPR
:
9899 tree denom
= TREE_OPERAND (t
, 1);
9900 if (!RECUR (denom
, rval
))
9902 /* We can't call cxx_eval_outermost_constant_expr on an expression
9903 that hasn't been through instantiate_non_dependent_expr yet. */
9904 if (!processing_template_decl
)
9905 denom
= cxx_eval_outermost_constant_expr (denom
, true);
9906 if (integer_zerop (denom
))
9908 if (flags
& tf_error
)
9909 constexpr_error (input_location
, fundef_p
,
9910 "division by zero is not a constant expression");
9916 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
9922 /* check_return_expr sometimes wraps a TARGET_EXPR in a
9923 COMPOUND_EXPR; don't get confused. */
9924 tree op0
= TREE_OPERAND (t
, 0);
9925 tree op1
= TREE_OPERAND (t
, 1);
9927 if (TREE_CODE (op0
) == TARGET_EXPR
&& op1
== TARGET_EXPR_SLOT (op0
))
9928 return RECUR (op0
, want_rval
);
9933 /* If the first operand is the non-short-circuit constant, look at
9934 the second operand; otherwise we only care about the first one for
9936 case TRUTH_AND_EXPR
:
9937 case TRUTH_ANDIF_EXPR
:
9938 tmp
= boolean_true_node
;
9941 case TRUTH_ORIF_EXPR
:
9942 tmp
= boolean_false_node
;
9945 tree op0
= TREE_OPERAND (t
, 0);
9946 tree op1
= TREE_OPERAND (t
, 1);
9947 if (!RECUR (op0
, rval
))
9949 if (!(flags
& tf_error
) && RECUR (op1
, rval
))
9950 /* When quiet, try to avoid expensive trial evaluation by first
9951 checking potentiality of the second operand. */
9953 if (!processing_template_decl
)
9954 op0
= cxx_eval_outermost_constant_expr (op0
, true);
9955 if (tree_int_cst_equal (op0
, tmp
))
9956 return (flags
& tf_error
) ? RECUR (op1
, rval
) : false;
9963 case POINTER_PLUS_EXPR
:
9965 case EXACT_DIV_EXPR
:
9975 case TRUTH_XOR_EXPR
:
9976 case UNORDERED_EXPR
:
9989 case ARRAY_RANGE_REF
:
9993 case BINARY_LEFT_FOLD_EXPR
:
9994 case BINARY_RIGHT_FOLD_EXPR
:
9996 for (i
= 0; i
< 2; ++i
)
9997 if (!RECUR (TREE_OPERAND (t
, i
), want_rval
))
10001 case VEC_PERM_EXPR
:
10002 for (i
= 0; i
< 3; ++i
)
10003 if (!RECUR (TREE_OPERAND (t
, i
), true))
10008 if (COND_EXPR_IS_VEC_DELETE (t
) && cxx_dialect
< cxx20
)
10010 if (flags
& tf_error
)
10011 constexpr_error (loc
, fundef_p
, "%<delete[]%> is not a "
10012 "constant expression");
10015 /* Fall through. */
10017 case VEC_COND_EXPR
:
10018 /* If the condition is a known constant, we know which of the legs we
10019 care about; otherwise we only require that the condition and
10020 either of the legs be potentially constant. */
10021 tmp
= TREE_OPERAND (t
, 0);
10022 if (!RECUR (tmp
, rval
))
10024 if (!processing_template_decl
)
10025 tmp
= cxx_eval_outermost_constant_expr (tmp
, true);
10026 /* potential_constant_expression* isn't told if it is called for
10027 manifestly_const_eval or not, so for consteval if always
10028 process both branches as if the condition is not a known
10030 if (TREE_CODE (t
) != IF_STMT
|| !IF_STMT_CONSTEVAL_P (t
))
10032 if (integer_zerop (tmp
))
10033 return RECUR (TREE_OPERAND (t
, 2), want_rval
);
10034 else if (TREE_CODE (tmp
) == INTEGER_CST
)
10035 return RECUR (TREE_OPERAND (t
, 1), want_rval
);
10037 tmp
= *jump_target
;
10038 for (i
= 1; i
< 3; ++i
)
10040 tree this_jump_target
= tmp
;
10041 if (potential_constant_expression_1 (TREE_OPERAND (t
, i
),
10042 want_rval
, strict
, now
, fundef_p
,
10043 tf_none
, &this_jump_target
))
10045 if (returns (&this_jump_target
))
10046 *jump_target
= this_jump_target
;
10047 else if (!returns (jump_target
))
10049 if (breaks (&this_jump_target
)
10050 || continues (&this_jump_target
))
10051 *jump_target
= this_jump_target
;
10054 /* If the then branch is potentially constant, but
10055 does not return, check if the else branch
10056 couldn't return, break or continue. */
10057 hash_set
<tree
> pset
;
10058 check_for_return_continue_data data
= { &pset
, NULL_TREE
,
10061 = cp_walk_tree (&TREE_OPERAND (t
, 2),
10062 check_for_return_continue
, &data
,
10064 *jump_target
= ret_expr
;
10065 else if (*jump_target
== NULL_TREE
)
10067 if (data
.continue_stmt
)
10068 *jump_target
= data
.continue_stmt
;
10069 else if (data
.break_stmt
)
10070 *jump_target
= data
.break_stmt
;
10077 if (flags
& tf_error
)
10079 if (TREE_CODE (t
) == IF_STMT
)
10080 constexpr_error (loc
, fundef_p
, "neither branch of %<if%> is a "
10081 "constant expression");
10083 constexpr_error (loc
, fundef_p
, "expression %qE is not a "
10084 "constant expression", t
);
10088 case VEC_INIT_EXPR
:
10089 if (VEC_INIT_EXPR_IS_CONSTEXPR (t
))
10091 if (flags
& tf_error
)
10093 if (constexpr_error (loc
, fundef_p
, "non-constant array "
10095 diagnose_non_constexpr_vec_init (t
);
10101 /* We can see these in statement-expressions. */
10105 if (!RECUR (CLEANUP_BODY (t
), any
))
10107 if (!CLEANUP_EH_ONLY (t
) && !RECUR (CLEANUP_EXPR (t
), any
))
10111 case EMPTY_CLASS_EXPR
:
10116 tree
*target
= &TREE_OPERAND (t
, 0);
10117 /* Gotos representing break, continue and cdtor return are OK. */
10118 if (breaks (target
) || continues (target
) || returns (target
))
10120 *jump_target
= *target
;
10123 if (flags
& tf_error
)
10124 constexpr_error (loc
, fundef_p
, "%<goto%> is not a constant "
10129 case ASSERTION_STMT
:
10130 case PRECONDITION_STMT
:
10131 case POSTCONDITION_STMT
:
10132 if (!checked_contract_p (get_contract_semantic (t
)))
10134 return RECUR (CONTRACT_CONDITION (t
), rval
);
10137 t
= LABEL_EXPR_LABEL (t
);
10138 if (DECL_ARTIFICIAL (t
) || cxx_dialect
>= cxx23
)
10140 else if (flags
& tf_error
)
10141 constexpr_error (loc
, fundef_p
, "label definition in %<constexpr%> "
10142 "function only available with %<-std=c++2b%> or "
10143 "%<-std=gnu++2b%>");
10146 case ANNOTATE_EXPR
:
10147 return RECUR (TREE_OPERAND (t
, 0), rval
);
10149 case BIT_CAST_EXPR
:
10150 return RECUR (TREE_OPERAND (t
, 0), rval
);
10152 /* Coroutine await, yield and return expressions are not. */
10153 case CO_AWAIT_EXPR
:
10154 case CO_YIELD_EXPR
:
10155 case CO_RETURN_EXPR
:
10158 case NONTYPE_ARGUMENT_PACK
:
10160 tree args
= ARGUMENT_PACK_ARGS (t
);
10161 int len
= TREE_VEC_LENGTH (args
);
10162 for (int i
= 0; i
< len
; ++i
)
10163 if (!RECUR (TREE_VEC_ELT (args
, i
), any
))
10169 if (objc_non_constant_expr_p (t
))
10172 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t
)));
10173 gcc_unreachable ();
10180 potential_constant_expression_1 (tree t
, bool want_rval
, bool strict
, bool now
,
10181 bool fundef_p
, tsubst_flags_t flags
)
10183 if (flags
& tf_error
)
10185 /* Check potentiality quietly first, as that could be performed more
10186 efficiently in some cases (currently only for TRUTH_*_EXPR). If
10187 that fails, replay the check noisily to give errors. */
10188 flags
&= ~tf_error
;
10189 if (potential_constant_expression_1 (t
, want_rval
, strict
, now
, fundef_p
,
10195 tree target
= NULL_TREE
;
10196 return potential_constant_expression_1 (t
, want_rval
, strict
, now
, fundef_p
,
10200 /* The main entry point to the above. */
10203 potential_constant_expression (tree t
)
10205 return potential_constant_expression_1 (t
, /*want_rval*/false, /*strict*/true,
10206 /*now*/false, /*fundef_p*/false,
10210 /* As above, but require a constant rvalue. */
10213 potential_rvalue_constant_expression (tree t
)
10215 return potential_constant_expression_1 (t
, /*want_rval*/true, /*strict*/true,
10216 /*now*/false, /*fundef_p*/false,
10220 /* Like above, but complain about non-constant expressions. */
10223 require_potential_constant_expression (tree t
)
10225 return potential_constant_expression_1 (t
, /*want_rval*/false, /*strict*/true,
10226 /*now*/false, /*fundef_p*/false,
10227 tf_warning_or_error
);
10230 /* Cross product of the above. */
10233 require_potential_rvalue_constant_expression (tree t
)
10235 return potential_constant_expression_1 (t
, /*want_rval*/true, /*strict*/true,
10236 /*now*/false, /*fundef_p*/false,
10237 tf_warning_or_error
);
10240 /* Like require_potential_rvalue_constant_expression, but fundef_p is true. */
10243 require_potential_rvalue_constant_expression_fncheck (tree t
)
10245 return potential_constant_expression_1 (t
, /*want_rval*/true, /*strict*/true,
10246 /*now*/false, /*fundef_p*/true,
10247 tf_warning_or_error
);
10250 /* Like above, but don't consider PARM_DECL a potential_constant_expression. */
10253 require_rvalue_constant_expression (tree t
)
10255 return potential_constant_expression_1 (t
, /*want_rval*/true, /*strict*/true,
10256 /*now*/true, /*fundef_p*/false,
10257 tf_warning_or_error
);
10260 /* Like potential_constant_expression, but don't consider possible constexpr
10261 substitution of the current function. That is, PARM_DECL qualifies under
10262 potential_constant_expression, but not here.
10264 This is basically what you can check when any actual constant values might
10265 be value-dependent. */
10268 is_constant_expression (tree t
)
10270 return potential_constant_expression_1 (t
, /*want_rval*/false, /*strict*/true,
10271 /*now*/true, /*fundef_p*/false,
10275 /* As above, but expect an rvalue. */
10278 is_rvalue_constant_expression (tree t
)
10280 return potential_constant_expression_1 (t
, /*want_rval*/true, /*strict*/true,
10281 /*now*/true, /*fundef_p*/false,
10285 /* Like above, but complain about non-constant expressions. */
10288 require_constant_expression (tree t
)
10290 return potential_constant_expression_1 (t
, /*want_rval*/false, /*strict*/true,
10291 /*now*/true, /*fundef_p*/false,
10292 tf_warning_or_error
);
10295 /* Like is_constant_expression, but allow const variables that are not allowed
10296 under constexpr rules. */
10299 is_static_init_expression (tree t
)
10301 return potential_constant_expression_1 (t
, /*want_rval*/false,
10302 /*strict*/false, /*now*/true,
10303 /*fundef_p*/false, tf_none
);
10306 /* Returns true if T is a potential constant expression that is not
10307 instantiation-dependent, and therefore a candidate for constant folding even
10311 is_nondependent_constant_expression (tree t
)
10313 return (!type_unknown_p (t
)
10314 && is_constant_expression (t
)
10315 && !instantiation_dependent_expression_p (t
));
10318 /* Returns true if T is a potential static initializer expression that is not
10319 instantiation-dependent. */
10322 is_nondependent_static_init_expression (tree t
)
10324 return (!type_unknown_p (t
)
10325 && is_static_init_expression (t
)
10326 && !instantiation_dependent_expression_p (t
));
10329 /* True iff FN is an implicitly constexpr function. */
10332 decl_implicit_constexpr_p (tree fn
)
10334 if (!(flag_implicit_constexpr
10335 && TREE_CODE (fn
) == FUNCTION_DECL
10336 && DECL_DECLARED_CONSTEXPR_P (fn
)))
10339 if (DECL_CLONED_FUNCTION_P (fn
))
10340 fn
= DECL_CLONED_FUNCTION (fn
);
10342 return (DECL_LANG_SPECIFIC (fn
)
10343 && DECL_LANG_SPECIFIC (fn
)->u
.fn
.implicit_constexpr
);
10346 /* Finalize constexpr processing after parsing. */
10349 fini_constexpr (void)
10351 /* The contexpr call and fundef copies tables are no longer needed. */
10352 constexpr_call_table
= NULL
;
10353 fundef_copies_table
= NULL
;
10356 #include "gt-cp-constexpr.h"