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-2022 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"
42 static bool verify_constant (tree
, bool, bool *, bool *);
43 #define VERIFY_CONSTANT(X) \
45 if (verify_constant ((X), ctx->quiet, non_constant_p, overflow_p)) \
49 static HOST_WIDE_INT
find_array_ctor_elt (tree ary
, tree dindex
,
51 static int array_index_cmp (tree key
, tree index
);
53 /* Returns true iff FUN is an instantiation of a constexpr function
54 template or a defaulted constexpr function. */
57 is_instantiation_of_constexpr (tree fun
)
59 return ((DECL_TEMPLOID_INSTANTIATION (fun
)
60 && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun
)))
61 || (DECL_DEFAULTED_FN (fun
)
62 && DECL_DECLARED_CONSTEXPR_P (fun
)));
65 /* Return true if T is a literal type. */
68 literal_type_p (tree t
)
73 || (VOID_TYPE_P (t
) && cxx_dialect
>= cxx14
))
77 t
= complete_type (t
);
78 gcc_assert (COMPLETE_TYPE_P (t
) || errorcount
);
79 return CLASSTYPE_LITERAL_P (t
);
81 if (TREE_CODE (t
) == ARRAY_TYPE
)
82 return literal_type_p (strip_array_types (t
));
86 /* If DECL is a variable declared `constexpr', require its type
87 be literal. Return error_mark_node if we give an error, the
91 ensure_literal_type_for_constexpr_object (tree decl
)
93 tree type
= TREE_TYPE (decl
);
95 && (DECL_DECLARED_CONSTEXPR_P (decl
)
96 || var_in_constexpr_fn (decl
))
97 && !processing_template_decl
)
99 tree stype
= strip_array_types (type
);
100 if (CLASS_TYPE_P (stype
) && !COMPLETE_TYPE_P (complete_type (stype
)))
101 /* Don't complain here, we'll complain about incompleteness
102 when we try to initialize the variable. */;
103 else if (!literal_type_p (type
))
105 if (DECL_DECLARED_CONSTEXPR_P (decl
))
107 auto_diagnostic_group d
;
108 error_at (DECL_SOURCE_LOCATION (decl
),
109 "the type %qT of %<constexpr%> variable %qD "
110 "is not literal", type
, decl
);
111 explain_non_literal_class (type
);
112 decl
= error_mark_node
;
114 else if (cxx_dialect
< cxx23
)
116 if (!is_instantiation_of_constexpr (current_function_decl
))
118 auto_diagnostic_group d
;
119 error_at (DECL_SOURCE_LOCATION (decl
),
120 "variable %qD of non-literal type %qT in "
121 "%<constexpr%> function only available with "
122 "%<-std=c++2b%> or %<-std=gnu++2b%>", decl
, type
);
123 explain_non_literal_class (type
);
124 decl
= error_mark_node
;
126 cp_function_chain
->invalid_constexpr
= true;
129 else if (DECL_DECLARED_CONSTEXPR_P (decl
)
130 && variably_modified_type_p (type
, NULL_TREE
))
132 error_at (DECL_SOURCE_LOCATION (decl
),
133 "%<constexpr%> variable %qD has variably-modified "
134 "type %qT", decl
, type
);
135 decl
= error_mark_node
;
141 struct constexpr_fundef_hasher
: ggc_ptr_hash
<constexpr_fundef
>
143 static hashval_t
hash (const constexpr_fundef
*);
144 static bool equal (const constexpr_fundef
*, const constexpr_fundef
*);
147 /* This table holds all constexpr function definitions seen in
148 the current translation unit. */
150 static GTY (()) hash_table
<constexpr_fundef_hasher
> *constexpr_fundef_table
;
152 /* Utility function used for managing the constexpr function table.
153 Return true if the entries pointed to by P and Q are for the
154 same constexpr function. */
157 constexpr_fundef_hasher::equal (const constexpr_fundef
*lhs
,
158 const constexpr_fundef
*rhs
)
160 return lhs
->decl
== rhs
->decl
;
163 /* Utility function used for managing the constexpr function table.
164 Return a hash value for the entry pointed to by Q. */
167 constexpr_fundef_hasher::hash (const constexpr_fundef
*fundef
)
169 return DECL_UID (fundef
->decl
);
172 /* Return a previously saved definition of function FUN. */
175 retrieve_constexpr_fundef (tree fun
)
177 if (constexpr_fundef_table
== NULL
)
180 constexpr_fundef fundef
= { fun
, NULL_TREE
, NULL_TREE
, NULL_TREE
};
181 return constexpr_fundef_table
->find (&fundef
);
184 /* Check whether the parameter and return types of FUN are valid for a
185 constexpr function, and complain if COMPLAIN. */
188 is_valid_constexpr_fn (tree fun
, bool complain
)
192 if (DECL_INHERITED_CTOR (fun
)
193 && TREE_CODE (fun
) == TEMPLATE_DECL
)
197 error ("inherited constructor %qD is not %<constexpr%>",
198 DECL_INHERITED_CTOR (fun
));
202 for (tree parm
= FUNCTION_FIRST_USER_PARM (fun
);
203 parm
!= NULL_TREE
; parm
= TREE_CHAIN (parm
))
204 if (!literal_type_p (TREE_TYPE (parm
)))
209 auto_diagnostic_group d
;
210 error ("invalid type for parameter %d of %<constexpr%> "
211 "function %q+#D", DECL_PARM_INDEX (parm
), fun
);
212 explain_non_literal_class (TREE_TYPE (parm
));
217 if (LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun
)) && cxx_dialect
< cxx17
)
221 inform (DECL_SOURCE_LOCATION (fun
),
222 "lambdas are implicitly %<constexpr%> only in C++17 and later");
224 else if (DECL_DESTRUCTOR_P (fun
))
226 if (cxx_dialect
< cxx20
)
230 error_at (DECL_SOURCE_LOCATION (fun
),
231 "%<constexpr%> destructors only available"
232 " with %<-std=c++20%> or %<-std=gnu++20%>");
235 else if (!DECL_CONSTRUCTOR_P (fun
))
237 tree rettype
= TREE_TYPE (TREE_TYPE (fun
));
238 if (!literal_type_p (rettype
))
243 auto_diagnostic_group d
;
244 error ("invalid return type %qT of %<constexpr%> function %q+D",
246 explain_non_literal_class (rettype
);
250 /* C++14 DR 1684 removed this restriction. */
251 if (cxx_dialect
< cxx14
252 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fun
)
253 && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun
)))
258 auto_diagnostic_group d
;
259 if (pedwarn (DECL_SOURCE_LOCATION (fun
), OPT_Wpedantic
,
260 "enclosing class of %<constexpr%> non-static"
261 " member function %q+#D is not a literal type",
263 explain_non_literal_class (DECL_CONTEXT (fun
));
267 else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun
)))
271 error ("%q#T has virtual base classes", DECL_CONTEXT (fun
));
277 /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF
278 for a member of an anonymous aggregate, INIT is the initializer for that
279 member, and VEC_OUTER is the vector of constructor elements for the class
280 whose constructor we are processing. Add the initializer to the vector
281 and return true to indicate success. */
284 build_anon_member_initialization (tree member
, tree init
,
285 vec
<constructor_elt
, va_gc
> **vec_outer
)
287 /* MEMBER presents the relevant fields from the inside out, but we need
288 to build up the initializer from the outside in so that we can reuse
289 previously built CONSTRUCTORs if this is, say, the second field in an
290 anonymous struct. So we use a vec as a stack. */
291 auto_vec
<tree
, 2> fields
;
294 fields
.safe_push (TREE_OPERAND (member
, 1));
295 member
= TREE_OPERAND (member
, 0);
297 while (ANON_AGGR_TYPE_P (TREE_TYPE (member
))
298 && TREE_CODE (member
) == COMPONENT_REF
);
300 /* VEC has the constructor elements vector for the context of FIELD.
301 If FIELD is an anonymous aggregate, we will push inside it. */
302 vec
<constructor_elt
, va_gc
> **vec
= vec_outer
;
304 while (field
= fields
.pop(),
305 ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
308 /* If there is already an outer constructor entry for the anonymous
309 aggregate FIELD, use it; otherwise, insert one. */
310 if (vec_safe_is_empty (*vec
)
311 || (*vec
)->last().index
!= field
)
313 ctor
= build_constructor (TREE_TYPE (field
), NULL
);
314 CONSTRUCTOR_APPEND_ELT (*vec
, field
, ctor
);
317 ctor
= (*vec
)->last().value
;
318 vec
= &CONSTRUCTOR_ELTS (ctor
);
321 /* Now we're at the innermost field, the one that isn't an anonymous
322 aggregate. Add its initializer to the CONSTRUCTOR and we're done. */
323 gcc_assert (fields
.is_empty());
324 CONSTRUCTOR_APPEND_ELT (*vec
, field
, init
);
329 /* Subroutine of build_constexpr_constructor_member_initializers.
330 The expression tree T represents a data member initialization
331 in a (constexpr) constructor definition. Build a pairing of
332 the data member with its initializer, and prepend that pair
333 to the existing initialization pair INITS. */
336 build_data_member_initialization (tree t
, vec
<constructor_elt
, va_gc
> **vec
)
339 if (TREE_CODE (t
) == CLEANUP_POINT_EXPR
)
340 t
= TREE_OPERAND (t
, 0);
341 if (TREE_CODE (t
) == EXPR_STMT
)
342 t
= TREE_OPERAND (t
, 0);
343 if (t
== error_mark_node
)
345 if (TREE_CODE (t
) == STATEMENT_LIST
)
347 for (tree stmt
: tsi_range (t
))
348 if (! build_data_member_initialization (stmt
, vec
))
352 if (TREE_CODE (t
) == CLEANUP_STMT
)
354 /* We can't see a CLEANUP_STMT in a constructor for a literal class,
355 but we can in a constexpr constructor for a non-literal class. Just
356 ignore it; either all the initialization will be constant, in which
357 case the cleanup can't run, or it can't be constexpr.
358 Still recurse into CLEANUP_BODY. */
359 return build_data_member_initialization (CLEANUP_BODY (t
), vec
);
361 if (TREE_CODE (t
) == CONVERT_EXPR
)
362 t
= TREE_OPERAND (t
, 0);
363 if (TREE_CODE (t
) == INIT_EXPR
364 /* vptr initialization shows up as a MODIFY_EXPR. In C++14 we only
365 use what this function builds for cx_check_missing_mem_inits, and
366 assignment in the ctor body doesn't count. */
367 || (cxx_dialect
< cxx14
&& TREE_CODE (t
) == MODIFY_EXPR
))
369 member
= TREE_OPERAND (t
, 0);
370 init
= break_out_target_exprs (TREE_OPERAND (t
, 1));
372 else if (TREE_CODE (t
) == CALL_EXPR
)
374 tree fn
= get_callee_fndecl (t
);
375 if (!fn
|| !DECL_CONSTRUCTOR_P (fn
))
376 /* We're only interested in calls to subobject constructors. */
378 member
= CALL_EXPR_ARG (t
, 0);
379 /* We don't use build_cplus_new here because it complains about
380 abstract bases. Leaving the call unwrapped means that it has the
381 wrong type, but cxx_eval_constant_expression doesn't care. */
382 init
= break_out_target_exprs (t
);
384 else if (TREE_CODE (t
) == BIND_EXPR
)
385 return build_data_member_initialization (BIND_EXPR_BODY (t
), vec
);
387 /* Don't add anything else to the CONSTRUCTOR. */
389 if (INDIRECT_REF_P (member
))
390 member
= TREE_OPERAND (member
, 0);
391 if (TREE_CODE (member
) == NOP_EXPR
)
395 if (TREE_CODE (op
) == ADDR_EXPR
)
397 gcc_assert (same_type_ignoring_top_level_qualifiers_p
398 (TREE_TYPE (TREE_TYPE (op
)),
399 TREE_TYPE (TREE_TYPE (member
))));
400 /* Initializing a cv-qualified member; we need to look through
404 else if (op
== current_class_ptr
405 && (same_type_ignoring_top_level_qualifiers_p
406 (TREE_TYPE (TREE_TYPE (member
)),
407 current_class_type
)))
408 /* Delegating constructor. */
412 /* This is an initializer for an empty base; keep it for now so
413 we can check it in cxx_eval_bare_aggregate. */
414 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member
))));
417 if (TREE_CODE (member
) == ADDR_EXPR
)
418 member
= TREE_OPERAND (member
, 0);
419 if (TREE_CODE (member
) == COMPONENT_REF
)
421 tree aggr
= TREE_OPERAND (member
, 0);
422 if (TREE_CODE (aggr
) == VAR_DECL
)
423 /* Initializing a local variable, don't add anything. */
425 if (TREE_CODE (aggr
) != COMPONENT_REF
)
426 /* Normal member initialization. */
427 member
= TREE_OPERAND (member
, 1);
428 else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr
)))
429 /* Initializing a member of an anonymous union. */
430 return build_anon_member_initialization (member
, init
, vec
);
432 /* We're initializing a vtable pointer in a base. Leave it as
433 COMPONENT_REF so we remember the path to get to the vfield. */
434 gcc_assert (TREE_TYPE (member
) == vtbl_ptr_type_node
);
437 /* Value-initialization can produce multiple initializers for the
438 same field; use the last one. */
439 if (!vec_safe_is_empty (*vec
) && (*vec
)->last().index
== member
)
440 (*vec
)->last().value
= init
;
442 CONSTRUCTOR_APPEND_ELT (*vec
, member
, init
);
446 /* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval.
447 In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a
448 BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */
451 check_constexpr_bind_expr_vars (tree t
)
453 gcc_assert (TREE_CODE (t
) == BIND_EXPR
);
455 for (tree var
= BIND_EXPR_VARS (t
); var
; var
= DECL_CHAIN (var
))
456 if (TREE_CODE (var
) == TYPE_DECL
457 && DECL_IMPLICIT_TYPEDEF_P (var
)
458 && !LAMBDA_TYPE_P (TREE_TYPE (var
)))
463 /* Subroutine of check_constexpr_ctor_body. */
466 check_constexpr_ctor_body_1 (tree last
, tree list
)
468 switch (TREE_CODE (list
))
471 if (TREE_CODE (DECL_EXPR_DECL (list
)) == USING_DECL
472 || TREE_CODE (DECL_EXPR_DECL (list
)) == TYPE_DECL
)
476 case CLEANUP_POINT_EXPR
:
477 return check_constexpr_ctor_body (last
, TREE_OPERAND (list
, 0),
481 if (!check_constexpr_bind_expr_vars (list
)
482 || !check_constexpr_ctor_body (last
, BIND_EXPR_BODY (list
),
489 case DEBUG_BEGIN_STMT
:
497 /* Make sure that there are no statements after LAST in the constructor
498 body represented by LIST. */
501 check_constexpr_ctor_body (tree last
, tree list
, bool complain
)
503 /* C++14 doesn't require a constexpr ctor to have an empty body. */
504 if (cxx_dialect
>= cxx14
)
508 if (TREE_CODE (list
) == STATEMENT_LIST
)
510 tree_stmt_iterator i
= tsi_last (list
);
511 for (; !tsi_end_p (i
); tsi_prev (&i
))
513 tree t
= tsi_stmt (i
);
516 if (!check_constexpr_ctor_body_1 (last
, t
))
523 else if (list
!= last
524 && !check_constexpr_ctor_body_1 (last
, list
))
529 error ("%<constexpr%> constructor does not have empty body");
530 DECL_DECLARED_CONSTEXPR_P (current_function_decl
) = false;
535 /* V is a vector of constructor elements built up for the base and member
536 initializers of a constructor for TYPE. They need to be in increasing
537 offset order, which they might not be yet if TYPE has a primary base
538 which is not first in the base-clause or a vptr and at least one base
539 all of which are non-primary. */
541 static vec
<constructor_elt
, va_gc
> *
542 sort_constexpr_mem_initializers (tree type
, vec
<constructor_elt
, va_gc
> *v
)
544 tree pri
= CLASSTYPE_PRIMARY_BINFO (type
);
550 field_type
= BINFO_TYPE (pri
);
551 else if (TYPE_CONTAINS_VPTR_P (type
))
552 field_type
= vtbl_ptr_type_node
;
556 /* Find the element for the primary base or vptr and move it to the
557 beginning of the vec. */
558 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
559 if (TREE_TYPE (ce
->index
) == field_type
)
562 if (i
> 0 && i
< vec_safe_length (v
))
564 vec
<constructor_elt
, va_gc
> &vref
= *v
;
565 constructor_elt elt
= vref
[i
];
574 /* Build compile-time evalable representations of member-initializer list
575 for a constexpr constructor. */
578 build_constexpr_constructor_member_initializers (tree type
, tree body
)
580 vec
<constructor_elt
, va_gc
> *vec
= NULL
;
583 switch (TREE_CODE (body
))
585 case MUST_NOT_THROW_EXPR
:
587 body
= TREE_OPERAND (body
, 0);
591 for (tree stmt
: tsi_range (body
))
594 if (TREE_CODE (body
) == BIND_EXPR
)
600 body
= BIND_EXPR_BODY (body
);
607 if (TREE_CODE (body
) == TRY_BLOCK
)
609 body
= TREE_OPERAND (body
, 0);
610 if (TREE_CODE (body
) == BIND_EXPR
)
611 body
= BIND_EXPR_BODY (body
);
613 if (TREE_CODE (body
) == CLEANUP_POINT_EXPR
)
615 body
= TREE_OPERAND (body
, 0);
616 if (TREE_CODE (body
) == EXPR_STMT
)
617 body
= TREE_OPERAND (body
, 0);
618 if (TREE_CODE (body
) == INIT_EXPR
619 && (same_type_ignoring_top_level_qualifiers_p
620 (TREE_TYPE (TREE_OPERAND (body
, 0)),
621 current_class_type
)))
624 return TREE_OPERAND (body
, 1);
626 ok
= build_data_member_initialization (body
, &vec
);
628 else if (TREE_CODE (body
) == STATEMENT_LIST
)
630 for (tree stmt
: tsi_range (body
))
632 ok
= build_data_member_initialization (stmt
, &vec
);
637 else if (EXPR_P (body
))
638 ok
= build_data_member_initialization (body
, &vec
);
640 gcc_assert (errorcount
> 0);
643 if (vec_safe_length (vec
) > 0)
645 /* In a delegating constructor, return the target. */
646 constructor_elt
*ce
= &(*vec
)[0];
647 if (ce
->index
== current_class_ptr
)
654 vec
= sort_constexpr_mem_initializers (type
, vec
);
655 return build_constructor (type
, vec
);
658 return error_mark_node
;
661 /* We have an expression tree T that represents a call, either CALL_EXPR
662 or AGGR_INIT_EXPR. If the call is lexically to a named function,
663 retrun the _DECL for that function. */
666 get_function_named_in_call (tree t
)
668 tree fun
= cp_get_callee (t
);
669 if (fun
&& TREE_CODE (fun
) == ADDR_EXPR
670 && TREE_CODE (TREE_OPERAND (fun
, 0)) == FUNCTION_DECL
)
671 fun
= TREE_OPERAND (fun
, 0);
675 /* Subroutine of check_constexpr_fundef. BODY is the body of a function
676 declared to be constexpr, or a sub-statement thereof. Returns the
677 return value if suitable, error_mark_node for a statement not allowed in
678 a constexpr function, or NULL_TREE if no return value was found. */
681 constexpr_fn_retval (tree body
)
683 switch (TREE_CODE (body
))
687 tree expr
= NULL_TREE
;
688 for (tree stmt
: tsi_range (body
))
690 tree s
= constexpr_fn_retval (stmt
);
691 if (s
== error_mark_node
)
692 return error_mark_node
;
693 else if (s
== NULL_TREE
)
694 /* Keep iterating. */;
696 /* Multiple return statements. */
697 return error_mark_node
;
705 return break_out_target_exprs (TREE_OPERAND (body
, 0));
709 tree decl
= DECL_EXPR_DECL (body
);
710 if (TREE_CODE (decl
) == USING_DECL
711 /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */
712 || DECL_ARTIFICIAL (decl
))
714 return error_mark_node
;
717 case CLEANUP_POINT_EXPR
:
718 return constexpr_fn_retval (TREE_OPERAND (body
, 0));
721 if (!check_constexpr_bind_expr_vars (body
))
722 return error_mark_node
;
723 return constexpr_fn_retval (BIND_EXPR_BODY (body
));
726 case DEBUG_BEGIN_STMT
:
731 tree fun
= get_function_named_in_call (body
);
733 && fndecl_built_in_p (fun
, BUILT_IN_UNREACHABLE
))
739 return error_mark_node
;
743 /* Subroutine of check_constexpr_fundef. BODY is the DECL_SAVED_TREE of
744 FUN; do the necessary transformations to turn it into a single expression
745 that we can store in the hash table. */
748 massage_constexpr_body (tree fun
, tree body
)
750 if (DECL_CONSTRUCTOR_P (fun
))
751 body
= build_constexpr_constructor_member_initializers
752 (DECL_CONTEXT (fun
), body
);
753 else if (cxx_dialect
< cxx14
)
755 if (TREE_CODE (body
) == EH_SPEC_BLOCK
)
756 body
= EH_SPEC_STMTS (body
);
757 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
758 body
= TREE_OPERAND (body
, 0);
759 body
= constexpr_fn_retval (body
);
764 /* CTYPE is a type constructed from BODY. Return true if some
765 bases/fields are uninitialized, and complain if COMPLAIN. */
768 cx_check_missing_mem_inits (tree ctype
, tree body
, bool complain
)
770 /* We allow uninitialized bases/fields in C++20. */
771 if (cxx_dialect
>= cxx20
)
778 if (TREE_CODE (body
) != CONSTRUCTOR
)
780 nelts
= CONSTRUCTOR_NELTS (body
);
782 tree field
= TYPE_FIELDS (ctype
);
784 if (TREE_CODE (ctype
) == UNION_TYPE
)
786 if (nelts
== 0 && next_aggregate_field (field
))
789 error ("%<constexpr%> constructor for union %qT must "
790 "initialize exactly one non-static data member", ctype
);
796 /* Iterate over the CONSTRUCTOR, checking any missing fields don't
797 need an explicit initialization. */
799 for (unsigned i
= 0; i
<= nelts
; ++i
)
801 tree index
= NULL_TREE
;
804 index
= CONSTRUCTOR_ELT (body
, i
)->index
;
805 /* Skip base and vtable inits. */
806 if (TREE_CODE (index
) != FIELD_DECL
807 || DECL_ARTIFICIAL (index
))
811 for (; field
!= index
; field
= DECL_CHAIN (field
))
814 if (TREE_CODE (field
) != FIELD_DECL
)
816 if (DECL_UNNAMED_BIT_FIELD (field
))
818 if (DECL_ARTIFICIAL (field
))
820 if (ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
822 /* Recurse to check the anonymous aggregate member. */
823 bad
|= cx_check_missing_mem_inits
824 (TREE_TYPE (field
), NULL_TREE
, complain
);
825 if (bad
&& !complain
)
829 ftype
= TREE_TYPE (field
);
830 if (!ftype
|| !TYPE_P (ftype
) || !COMPLETE_TYPE_P (ftype
))
831 /* A flexible array can't be intialized here, so don't complain
834 if (is_empty_field (field
))
835 /* An empty field doesn't need an initializer. */
837 ftype
= strip_array_types (ftype
);
838 if (type_has_constexpr_default_constructor (ftype
))
840 /* It's OK to skip a member with a trivial constexpr ctor.
841 A constexpr ctor that isn't trivial should have been
843 gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype
)
849 auto_diagnostic_group d
;
850 error ("member %qD must be initialized by mem-initializer "
851 "in %<constexpr%> constructor", field
);
852 inform (DECL_SOURCE_LOCATION (field
), "declared here");
855 if (field
== NULL_TREE
)
858 if (ANON_AGGR_TYPE_P (TREE_TYPE (index
)))
860 /* Check the anonymous aggregate initializer is valid. */
861 bad
|= cx_check_missing_mem_inits
862 (TREE_TYPE (index
), CONSTRUCTOR_ELT (body
, i
)->value
, complain
);
863 if (bad
&& !complain
)
866 field
= DECL_CHAIN (field
);
872 /* We are processing the definition of the constexpr function FUN.
873 Check that its body fulfills the apropriate requirements and
874 enter it in the constexpr function definition table. */
877 maybe_save_constexpr_fundef (tree fun
)
879 if (processing_template_decl
880 || cp_function_chain
->invalid_constexpr
881 || (DECL_CLONED_FUNCTION_P (fun
) && !DECL_DELETING_DESTRUCTOR_P (fun
)))
884 /* With -fimplicit-constexpr, try to make inlines constexpr. We'll
885 actually set DECL_DECLARED_CONSTEXPR_P below if the checks pass. */
886 bool implicit
= false;
887 if (flag_implicit_constexpr
)
889 if (DECL_DELETING_DESTRUCTOR_P (fun
)
890 && decl_implicit_constexpr_p (DECL_CLONED_FUNCTION (fun
)))
891 /* Don't inherit implicit constexpr from the non-deleting
893 DECL_DECLARED_CONSTEXPR_P (fun
) = false;
895 if (!DECL_DECLARED_CONSTEXPR_P (fun
)
896 && DECL_DECLARED_INLINE_P (fun
)
897 && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fun
)))
901 if (!DECL_DECLARED_CONSTEXPR_P (fun
) && !implicit
)
904 bool complain
= !DECL_GENERATED_P (fun
) && !implicit
;
906 if (!is_valid_constexpr_fn (fun
, complain
))
909 tree massaged
= massage_constexpr_body (fun
, DECL_SAVED_TREE (fun
));
910 if (massaged
== NULL_TREE
|| massaged
== error_mark_node
)
912 if (!DECL_CONSTRUCTOR_P (fun
) && complain
)
913 error ("body of %<constexpr%> function %qD not a return-statement",
918 bool potential
= potential_rvalue_constant_expression (massaged
);
919 if (!potential
&& complain
)
920 require_potential_rvalue_constant_expression (massaged
);
922 if (DECL_CONSTRUCTOR_P (fun
) && potential
923 && !DECL_DEFAULTED_FN (fun
))
925 if (cx_check_missing_mem_inits (DECL_CONTEXT (fun
),
928 else if (cxx_dialect
> cxx11
)
930 /* What we got from massage_constexpr_body is pretty much just the
931 ctor-initializer, also check the body. */
932 massaged
= DECL_SAVED_TREE (fun
);
933 potential
= potential_rvalue_constant_expression (massaged
);
934 if (!potential
&& complain
)
935 require_potential_rvalue_constant_expression (massaged
);
939 if (!potential
&& complain
)
946 DECL_DECLARED_CONSTEXPR_P (fun
) = true;
947 DECL_LANG_SPECIFIC (fun
)->u
.fn
.implicit_constexpr
= true;
948 if (DECL_CONSTRUCTOR_P (fun
))
949 TYPE_HAS_CONSTEXPR_CTOR (DECL_CONTEXT (fun
)) = true;
952 /* Don't bother keeping the pre-generic body of unsuitable functions
953 not explicitly declared constexpr. */
957 constexpr_fundef entry
= {fun
, NULL_TREE
, NULL_TREE
, NULL_TREE
};
958 bool clear_ctx
= false;
959 if (DECL_RESULT (fun
) && DECL_CONTEXT (DECL_RESULT (fun
)) == NULL_TREE
)
962 DECL_CONTEXT (DECL_RESULT (fun
)) = fun
;
964 tree saved_fn
= current_function_decl
;
965 current_function_decl
= fun
;
966 entry
.body
= copy_fn (entry
.decl
, entry
.parms
, entry
.result
);
967 current_function_decl
= saved_fn
;
969 DECL_CONTEXT (DECL_RESULT (entry
.decl
)) = NULL_TREE
;
971 /* For a template instantiation, we want to remember the pre-generic body
972 for explain_invalid_constexpr_fn, but do tell cxx_eval_call_expression
973 that it doesn't need to bother trying to expand the function. */
974 entry
.result
= error_mark_node
;
976 register_constexpr_fundef (entry
);
979 /* BODY is a validated and massaged definition of a constexpr
980 function. Register it in the hash table. */
983 register_constexpr_fundef (const constexpr_fundef
&value
)
985 /* Create the constexpr function table if necessary. */
986 if (constexpr_fundef_table
== NULL
)
987 constexpr_fundef_table
988 = hash_table
<constexpr_fundef_hasher
>::create_ggc (101);
990 constexpr_fundef
**slot
= constexpr_fundef_table
->find_slot
991 (const_cast<constexpr_fundef
*> (&value
), INSERT
);
993 gcc_assert (*slot
== NULL
);
994 *slot
= ggc_alloc
<constexpr_fundef
> ();
998 /* FUN is a non-constexpr function called in a context that requires a
999 constant expression. If it comes from a constexpr template, explain why
1000 the instantiation isn't constexpr. */
1003 explain_invalid_constexpr_fn (tree fun
)
1005 static hash_set
<tree
> *diagnosed
;
1007 /* Only diagnose defaulted functions, lambdas, or instantiations. */
1008 if (!DECL_DEFAULTED_FN (fun
)
1009 && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun
))
1010 && !is_instantiation_of_constexpr (fun
))
1012 inform (DECL_SOURCE_LOCATION (fun
), "%qD declared here", fun
);
1015 if (diagnosed
== NULL
)
1016 diagnosed
= new hash_set
<tree
>;
1017 if (diagnosed
->add (fun
))
1018 /* Already explained. */
1021 iloc_sentinel ils
= input_location
;
1022 if (!lambda_static_thunk_p (fun
))
1024 /* Diagnostics should completely ignore the static thunk, so leave
1025 input_location set to our caller's location. */
1026 input_location
= DECL_SOURCE_LOCATION (fun
);
1027 inform (input_location
,
1028 "%qD is not usable as a %<constexpr%> function because:", fun
);
1030 /* First check the declaration. */
1031 if (is_valid_constexpr_fn (fun
, true))
1033 /* Then if it's OK, the body. */
1034 if (!DECL_DECLARED_CONSTEXPR_P (fun
)
1035 && DECL_DEFAULTED_FN (fun
))
1036 explain_implicit_non_constexpr (fun
);
1039 if (constexpr_fundef
*fd
= retrieve_constexpr_fundef (fun
))
1042 body
= DECL_SAVED_TREE (fun
);
1043 body
= massage_constexpr_body (fun
, body
);
1044 require_potential_rvalue_constant_expression (body
);
1045 if (DECL_CONSTRUCTOR_P (fun
))
1046 cx_check_missing_mem_inits (DECL_CONTEXT (fun
), body
, true);
1051 /* Objects of this type represent calls to constexpr functions
1052 along with the bindings of parameters to their arguments, for
1053 the purpose of compile time evaluation. */
1055 struct GTY((for_user
)) constexpr_call
{
1056 /* Description of the constexpr function definition. */
1057 constexpr_fundef
*fundef
;
1058 /* Parameter bindings environment. A TREE_VEC of arguments. */
1060 /* Result of the call.
1061 NULL means the call is being evaluated.
1062 error_mark_node means that the evaluation was erroneous;
1063 otherwise, the actuall value of the call. */
1065 /* The hash of this call; we remember it here to avoid having to
1066 recalculate it when expanding the hash table. */
1068 /* Whether __builtin_is_constant_evaluated() should evaluate to true. */
1069 bool manifestly_const_eval
;
1072 struct constexpr_call_hasher
: ggc_ptr_hash
<constexpr_call
>
1074 static hashval_t
hash (constexpr_call
*);
1075 static bool equal (constexpr_call
*, constexpr_call
*);
1078 enum constexpr_switch_state
{
1079 /* Used when processing a switch for the first time by cxx_eval_switch_expr
1080 and default: label for that switch has not been seen yet. */
1081 css_default_not_seen
,
1082 /* Used when processing a switch for the first time by cxx_eval_switch_expr
1083 and default: label for that switch has been seen already. */
1085 /* Used when processing a switch for the second time by
1086 cxx_eval_switch_expr, where default: label should match. */
1087 css_default_processing
1090 /* The constexpr expansion context part which needs one instance per
1091 cxx_eval_outermost_constant_expr invocation. VALUES is a map of values of
1092 variables initialized within the expression. */
1094 struct constexpr_global_ctx
{
1095 /* Values for any temporaries or local variables within the
1096 constant-expression. */
1097 hash_map
<tree
,tree
> values
;
1098 /* Number of cxx_eval_constant_expression calls (except skipped ones,
1099 on simple constants or location wrappers) encountered during current
1100 cxx_eval_outermost_constant_expr call. */
1101 HOST_WIDE_INT constexpr_ops_count
;
1102 /* Heap VAR_DECLs created during the evaluation of the outermost constant
1104 auto_vec
<tree
, 16> heap_vars
;
1105 /* Cleanups that need to be evaluated at the end of CLEANUP_POINT_EXPR. */
1106 vec
<tree
> *cleanups
;
1107 /* Number of heap VAR_DECL deallocations. */
1108 unsigned heap_dealloc_count
;
1110 constexpr_global_ctx ()
1111 : constexpr_ops_count (0), cleanups (NULL
), heap_dealloc_count (0) {}
1114 /* The constexpr expansion context. CALL is the current function
1115 expansion, CTOR is the current aggregate initializer, OBJECT is the
1116 object being initialized by CTOR, either a VAR_DECL or a _REF. */
1118 struct constexpr_ctx
{
1119 /* The part of the context that needs to be unique to the whole
1120 cxx_eval_outermost_constant_expr invocation. */
1121 constexpr_global_ctx
*global
;
1122 /* The innermost call we're evaluating. */
1123 constexpr_call
*call
;
1124 /* SAVE_EXPRs and TARGET_EXPR_SLOT vars of TARGET_EXPRs that we've seen
1125 within the current LOOP_EXPR. NULL if we aren't inside a loop. */
1126 vec
<tree
> *save_exprs
;
1127 /* The CONSTRUCTOR we're currently building up for an aggregate
1130 /* The object we're building the CONSTRUCTOR for. */
1132 /* If inside SWITCH_EXPR. */
1133 constexpr_switch_state
*css_state
;
1134 /* The aggregate initialization context inside which this one is nested. This
1135 is used by lookup_placeholder to resolve PLACEHOLDER_EXPRs. */
1136 const constexpr_ctx
*parent
;
1138 /* Whether we should error on a non-constant expression or fail quietly.
1139 This flag needs to be here, but some of the others could move to global
1140 if they get larger than a word. */
1142 /* Whether we are strictly conforming to constant expression rules or
1143 trying harder to get a constant value. */
1145 /* Whether __builtin_is_constant_evaluated () should be true. */
1146 bool manifestly_const_eval
;
1149 /* This internal flag controls whether we should avoid doing anything during
1150 constexpr evaluation that would cause extra DECL_UID generation, such as
1151 template instantiation and function body copying. */
1153 static bool uid_sensitive_constexpr_evaluation_value
;
1155 /* An internal counter that keeps track of the number of times
1156 uid_sensitive_constexpr_evaluation_p returned true. */
1158 static unsigned uid_sensitive_constexpr_evaluation_true_counter
;
1160 /* The accessor for uid_sensitive_constexpr_evaluation_value which also
1161 increments the corresponding counter. */
1164 uid_sensitive_constexpr_evaluation_p ()
1166 if (uid_sensitive_constexpr_evaluation_value
)
1168 ++uid_sensitive_constexpr_evaluation_true_counter
;
1175 /* The default constructor for uid_sensitive_constexpr_evaluation_sentinel
1176 enables the internal flag for uid_sensitive_constexpr_evaluation_p
1177 during the lifetime of the sentinel object. Upon its destruction, the
1178 previous value of uid_sensitive_constexpr_evaluation_p is restored. */
1180 uid_sensitive_constexpr_evaluation_sentinel
1181 ::uid_sensitive_constexpr_evaluation_sentinel ()
1182 : ovr (uid_sensitive_constexpr_evaluation_value
, true)
1186 /* The default constructor for uid_sensitive_constexpr_evaluation_checker
1187 records the current number of times that uid_sensitive_constexpr_evaluation_p
1188 has been called and returned true. */
1190 uid_sensitive_constexpr_evaluation_checker
1191 ::uid_sensitive_constexpr_evaluation_checker ()
1192 : saved_counter (uid_sensitive_constexpr_evaluation_true_counter
)
1196 /* Returns true iff uid_sensitive_constexpr_evaluation_p is true, and
1197 some constexpr evaluation was restricted due to u_s_c_e_p being called
1198 and returning true during the lifetime of this checker object. */
1201 uid_sensitive_constexpr_evaluation_checker::evaluation_restricted_p () const
1203 return (uid_sensitive_constexpr_evaluation_value
1204 && saved_counter
!= uid_sensitive_constexpr_evaluation_true_counter
);
1208 /* A table of all constexpr calls that have been evaluated by the
1209 compiler in this translation unit. */
1211 static GTY (()) hash_table
<constexpr_call_hasher
> *constexpr_call_table
;
1213 /* Compute a hash value for a constexpr call representation. */
1216 constexpr_call_hasher::hash (constexpr_call
*info
)
1221 /* Return true if the objects pointed to by P and Q represent calls
1222 to the same constexpr function with the same arguments.
1223 Otherwise, return false. */
1226 constexpr_call_hasher::equal (constexpr_call
*lhs
, constexpr_call
*rhs
)
1230 if (lhs
->hash
!= rhs
->hash
)
1232 if (lhs
->manifestly_const_eval
!= rhs
->manifestly_const_eval
)
1234 if (!constexpr_fundef_hasher::equal (lhs
->fundef
, rhs
->fundef
))
1236 return cp_tree_equal (lhs
->bindings
, rhs
->bindings
);
1239 /* Initialize the constexpr call table, if needed. */
1242 maybe_initialize_constexpr_call_table (void)
1244 if (constexpr_call_table
== NULL
)
1245 constexpr_call_table
= hash_table
<constexpr_call_hasher
>::create_ggc (101);
1248 /* During constexpr CALL_EXPR evaluation, to avoid issues with sharing when
1249 a function happens to get called recursively, we unshare the callee
1250 function's body and evaluate this unshared copy instead of evaluating the
1253 FUNDEF_COPIES_TABLE is a per-function freelist of these unshared function
1254 copies. The underlying data structure of FUNDEF_COPIES_TABLE is a hash_map
1255 that's keyed off of the original FUNCTION_DECL and whose value is a
1256 TREE_LIST of this function's unused copies awaiting reuse.
1258 This is not GC-deletable to avoid GC affecting UID generation. */
1260 static GTY(()) decl_tree_map
*fundef_copies_table
;
1262 /* Reuse a copy or create a new unshared copy of the function FUN.
1263 Return this copy. We use a TREE_LIST whose PURPOSE is body, VALUE
1264 is parms, TYPE is result. */
1267 get_fundef_copy (constexpr_fundef
*fundef
)
1271 tree
*slot
= &(hash_map_safe_get_or_insert
<hm_ggc
>
1272 (fundef_copies_table
, fundef
->decl
, &existed
, 127));
1276 /* There is no cached function available, or in use. We can use
1277 the function directly. That the slot is now created records
1278 that this function is now in use. */
1279 copy
= build_tree_list (fundef
->body
, fundef
->parms
);
1280 TREE_TYPE (copy
) = fundef
->result
;
1282 else if (*slot
== NULL_TREE
)
1284 if (uid_sensitive_constexpr_evaluation_p ())
1287 /* We've already used the function itself, so make a copy. */
1288 copy
= build_tree_list (NULL
, NULL
);
1289 tree saved_body
= DECL_SAVED_TREE (fundef
->decl
);
1290 tree saved_parms
= DECL_ARGUMENTS (fundef
->decl
);
1291 tree saved_result
= DECL_RESULT (fundef
->decl
);
1292 tree saved_fn
= current_function_decl
;
1293 DECL_SAVED_TREE (fundef
->decl
) = fundef
->body
;
1294 DECL_ARGUMENTS (fundef
->decl
) = fundef
->parms
;
1295 DECL_RESULT (fundef
->decl
) = fundef
->result
;
1296 current_function_decl
= fundef
->decl
;
1297 TREE_PURPOSE (copy
) = copy_fn (fundef
->decl
, TREE_VALUE (copy
),
1299 current_function_decl
= saved_fn
;
1300 DECL_RESULT (fundef
->decl
) = saved_result
;
1301 DECL_ARGUMENTS (fundef
->decl
) = saved_parms
;
1302 DECL_SAVED_TREE (fundef
->decl
) = saved_body
;
1306 /* We have a cached function available. */
1308 *slot
= TREE_CHAIN (copy
);
1314 /* Save the copy COPY of function FUN for later reuse by
1315 get_fundef_copy(). By construction, there will always be an entry
1319 save_fundef_copy (tree fun
, tree copy
)
1321 tree
*slot
= fundef_copies_table
->get (fun
);
1322 TREE_CHAIN (copy
) = *slot
;
1326 /* We have an expression tree T that represents a call, either CALL_EXPR
1327 or AGGR_INIT_EXPR. Return the Nth argument. */
1330 get_nth_callarg (tree t
, int n
)
1332 switch (TREE_CODE (t
))
1335 return CALL_EXPR_ARG (t
, n
);
1337 case AGGR_INIT_EXPR
:
1338 return AGGR_INIT_EXPR_ARG (t
, n
);
1346 /* Whether our evaluation wants a prvalue (e.g. CONSTRUCTOR or _CST),
1347 a glvalue (e.g. VAR_DECL or _REF), or nothing. */
1355 static tree
cxx_eval_constant_expression (const constexpr_ctx
*, tree
,
1356 value_cat
, bool *, bool *, tree
* = NULL
);
1357 static tree
cxx_fold_indirect_ref (const constexpr_ctx
*, location_t
, tree
, tree
,
1359 static tree
find_heap_var_refs (tree
*, int *, void *);
1361 /* Attempt to evaluate T which represents a call to a builtin function.
1362 We assume here that all builtin functions evaluate to scalar types
1363 represented by _CST nodes. */
1366 cxx_eval_builtin_function_call (const constexpr_ctx
*ctx
, tree t
, tree fun
,
1368 bool *non_constant_p
, bool *overflow_p
)
1370 const int nargs
= call_expr_nargs (t
);
1371 tree
*args
= (tree
*) alloca (nargs
* sizeof (tree
));
1375 /* Don't fold __builtin_constant_p within a constexpr function. */
1376 bool bi_const_p
= DECL_IS_BUILTIN_CONSTANT_P (fun
);
1378 /* If we aren't requiring a constant expression, defer __builtin_constant_p
1379 in a constexpr function until we have values for the parameters. */
1381 && !ctx
->manifestly_const_eval
1382 && current_function_decl
1383 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
1385 *non_constant_p
= true;
1389 /* For __builtin_is_constant_evaluated, defer it if not
1390 ctx->manifestly_const_eval (as sometimes we try to constant evaluate
1391 without manifestly_const_eval even expressions or parts thereof which
1392 will later be manifestly const_eval evaluated), otherwise fold it to
1394 if (fndecl_built_in_p (fun
, CP_BUILT_IN_IS_CONSTANT_EVALUATED
,
1397 if (!ctx
->manifestly_const_eval
)
1399 *non_constant_p
= true;
1402 return boolean_true_node
;
1405 if (fndecl_built_in_p (fun
, CP_BUILT_IN_SOURCE_LOCATION
, BUILT_IN_FRONTEND
))
1407 temp_override
<tree
> ovr (current_function_decl
);
1408 if (ctx
->call
&& ctx
->call
->fundef
)
1409 current_function_decl
= ctx
->call
->fundef
->decl
;
1410 return fold_builtin_source_location (EXPR_LOCATION (t
));
1415 if (fndecl_built_in_p (fun
, BUILT_IN_NORMAL
))
1416 switch (DECL_FUNCTION_CODE (fun
))
1418 case BUILT_IN_STRLEN
:
1419 case BUILT_IN_STRNLEN
:
1422 case BUILT_IN_MEMCHR
:
1423 case BUILT_IN_STRCHR
:
1424 case BUILT_IN_STRRCHR
:
1428 case BUILT_IN_MEMCMP
:
1429 case BUILT_IN_STRCMP
:
1432 case BUILT_IN_STRSTR
:
1436 case BUILT_IN_ASAN_POINTER_COMPARE
:
1437 case BUILT_IN_ASAN_POINTER_SUBTRACT
:
1438 /* These builtins shall be ignored during constant expression
1441 case BUILT_IN_UNREACHABLE
:
1443 if (!*non_constant_p
&& !ctx
->quiet
)
1445 /* Do not allow__builtin_unreachable in constexpr function.
1446 The __builtin_unreachable call with BUILTINS_LOCATION
1447 comes from cp_maybe_instrument_return. */
1448 if (EXPR_LOCATION (t
) == BUILTINS_LOCATION
)
1449 error ("%<constexpr%> call flows off the end of the function");
1451 error ("%q+E is not a constant expression", t
);
1453 *non_constant_p
= true;
1459 /* Be permissive for arguments to built-ins; __builtin_constant_p should
1460 return constant false for a non-constant argument. */
1461 constexpr_ctx new_ctx
= *ctx
;
1462 new_ctx
.quiet
= true;
1463 for (i
= 0; i
< nargs
; ++i
)
1465 tree arg
= CALL_EXPR_ARG (t
, i
);
1468 /* To handle string built-ins we need to pass ADDR_EXPR<STRING_CST> since
1469 expand_builtin doesn't know how to look in the values table. */
1470 bool strop
= i
< strops
;
1474 if (TREE_CODE (arg
) == ADDR_EXPR
)
1475 arg
= TREE_OPERAND (arg
, 0);
1480 /* If builtin_valid_in_constant_expr_p is true,
1481 potential_constant_expression_1 has not recursed into the arguments
1482 of the builtin, verify it here. */
1483 if (!builtin_valid_in_constant_expr_p (fun
)
1484 || potential_constant_expression (arg
))
1486 bool dummy1
= false, dummy2
= false;
1487 arg
= cxx_eval_constant_expression (&new_ctx
, arg
, vc_prvalue
,
1492 /* For __builtin_constant_p, fold all expressions with constant values
1493 even if they aren't C++ constant-expressions. */
1494 arg
= cp_fold_rvalue (arg
);
1497 if (TREE_CODE (arg
) == CONSTRUCTOR
)
1498 arg
= braced_lists_to_strings (TREE_TYPE (arg
), arg
);
1499 if (TREE_CODE (arg
) == STRING_CST
)
1500 arg
= build_address (arg
);
1508 bool save_ffbcp
= force_folding_builtin_constant_p
;
1509 force_folding_builtin_constant_p
|= ctx
->manifestly_const_eval
;
1510 tree save_cur_fn
= current_function_decl
;
1511 /* Return name of ctx->call->fundef->decl for __builtin_FUNCTION (). */
1512 if (fndecl_built_in_p (fun
, BUILT_IN_FUNCTION
)
1514 && ctx
->call
->fundef
)
1515 current_function_decl
= ctx
->call
->fundef
->decl
;
1516 if (fndecl_built_in_p (fun
,
1517 CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS
,
1520 location_t loc
= EXPR_LOCATION (t
);
1522 VERIFY_CONSTANT (args
[0]);
1524 = fold_builtin_is_pointer_inverconvertible_with_class (loc
, nargs
,
1527 else if (fndecl_built_in_p (fun
,
1528 CP_BUILT_IN_IS_CORRESPONDING_MEMBER
,
1531 location_t loc
= EXPR_LOCATION (t
);
1534 VERIFY_CONSTANT (args
[0]);
1535 VERIFY_CONSTANT (args
[1]);
1537 new_call
= fold_builtin_is_corresponding_member (loc
, nargs
, args
);
1540 new_call
= fold_builtin_call_array (EXPR_LOCATION (t
), TREE_TYPE (t
),
1541 CALL_EXPR_FN (t
), nargs
, args
);
1542 current_function_decl
= save_cur_fn
;
1543 force_folding_builtin_constant_p
= save_ffbcp
;
1544 if (new_call
== NULL
)
1546 if (!*non_constant_p
&& !ctx
->quiet
)
1548 new_call
= build_call_array_loc (EXPR_LOCATION (t
), TREE_TYPE (t
),
1549 CALL_EXPR_FN (t
), nargs
, args
);
1550 error ("%q+E is not a constant expression", new_call
);
1552 *non_constant_p
= true;
1556 if (!potential_constant_expression (new_call
))
1558 if (!*non_constant_p
&& !ctx
->quiet
)
1559 error ("%q+E is not a constant expression", new_call
);
1560 *non_constant_p
= true;
1566 /* memchr returns a pointer into the first argument, but we replaced the
1567 argument above with a STRING_CST; put it back it now. */
1568 tree op
= CALL_EXPR_ARG (t
, strret
-1);
1569 STRIP_NOPS (new_call
);
1570 if (TREE_CODE (new_call
) == POINTER_PLUS_EXPR
)
1571 TREE_OPERAND (new_call
, 0) = op
;
1572 else if (TREE_CODE (new_call
) == ADDR_EXPR
)
1576 return cxx_eval_constant_expression (&new_ctx
, new_call
, lval
,
1577 non_constant_p
, overflow_p
);
1580 /* TEMP is the constant value of a temporary object of type TYPE. Adjust
1581 the type of the value to match. */
1584 adjust_temp_type (tree type
, tree temp
)
1586 if (same_type_p (TREE_TYPE (temp
), type
))
1588 /* Avoid wrapping an aggregate value in a NOP_EXPR. */
1589 if (TREE_CODE (temp
) == CONSTRUCTOR
)
1591 /* build_constructor wouldn't retain various CONSTRUCTOR flags. */
1592 tree t
= copy_node (temp
);
1593 TREE_TYPE (t
) = type
;
1596 if (TREE_CODE (temp
) == EMPTY_CLASS_EXPR
)
1597 return build0 (EMPTY_CLASS_EXPR
, type
);
1598 gcc_assert (scalarish_type_p (type
));
1599 /* Now we know we're dealing with a scalar, and a prvalue of non-class
1600 type is cv-unqualified. */
1601 return cp_fold_convert (cv_unqualified (type
), temp
);
1604 /* If T is a CONSTRUCTOR, return an unshared copy of T and any
1605 sub-CONSTRUCTORs. Otherwise return T.
1607 We use this whenever we initialize an object as a whole, whether it's a
1608 parameter, a local variable, or a subobject, so that subsequent
1609 modifications don't affect other places where it was used. */
1612 unshare_constructor (tree t MEM_STAT_DECL
)
1614 if (!t
|| TREE_CODE (t
) != CONSTRUCTOR
)
1616 auto_vec
<tree
*, 4> ptrs
;
1617 ptrs
.safe_push (&t
);
1618 while (!ptrs
.is_empty ())
1620 tree
*p
= ptrs
.pop ();
1621 tree n
= copy_node (*p PASS_MEM_STAT
);
1622 CONSTRUCTOR_ELTS (n
) = vec_safe_copy (CONSTRUCTOR_ELTS (*p
) PASS_MEM_STAT
);
1624 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (n
);
1625 constructor_elt
*ce
;
1626 for (HOST_WIDE_INT i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
1627 if (ce
->value
&& TREE_CODE (ce
->value
) == CONSTRUCTOR
)
1628 ptrs
.safe_push (&ce
->value
);
1633 /* If T is a CONSTRUCTOR, ggc_free T and any sub-CONSTRUCTORs. */
1636 free_constructor (tree t
)
1638 if (!t
|| TREE_CODE (t
) != CONSTRUCTOR
)
1640 releasing_vec ctors
;
1641 vec_safe_push (ctors
, t
);
1642 while (!ctors
->is_empty ())
1644 tree c
= ctors
->pop ();
1645 if (vec
<constructor_elt
, va_gc
> *elts
= CONSTRUCTOR_ELTS (c
))
1647 constructor_elt
*ce
;
1648 for (HOST_WIDE_INT i
= 0; vec_safe_iterate (elts
, i
, &ce
); ++i
)
1649 if (TREE_CODE (ce
->value
) == CONSTRUCTOR
)
1650 vec_safe_push (ctors
, ce
->value
);
1657 /* Helper function of cxx_bind_parameters_in_call. Return non-NULL
1658 if *TP is address of a static variable (or part of it) currently being
1659 constructed or of a heap artificial variable. */
1662 addr_of_non_const_var (tree
*tp
, int *walk_subtrees
, void *data
)
1664 if (TREE_CODE (*tp
) == ADDR_EXPR
)
1665 if (tree var
= get_base_address (TREE_OPERAND (*tp
, 0)))
1666 if (VAR_P (var
) && TREE_STATIC (var
))
1668 if (DECL_NAME (var
) == heap_uninit_identifier
1669 || DECL_NAME (var
) == heap_identifier
1670 || DECL_NAME (var
) == heap_vec_uninit_identifier
1671 || DECL_NAME (var
) == heap_vec_identifier
)
1674 constexpr_global_ctx
*global
= (constexpr_global_ctx
*) data
;
1675 if (global
->values
.get (var
))
1679 *walk_subtrees
= false;
1683 /* Subroutine of cxx_eval_call_expression.
1684 We are processing a call expression (either CALL_EXPR or
1685 AGGR_INIT_EXPR) in the context of CTX. Evaluate
1686 all arguments and bind their values to correspondings
1687 parameters, making up the NEW_CALL context. */
1690 cxx_bind_parameters_in_call (const constexpr_ctx
*ctx
, tree t
, tree fun
,
1691 bool *non_constant_p
, bool *overflow_p
,
1692 bool *non_constant_args
)
1694 const int nargs
= call_expr_nargs (t
);
1695 tree parms
= DECL_ARGUMENTS (fun
);
1697 /* We don't record ellipsis args below. */
1698 int nparms
= list_length (parms
);
1699 int nbinds
= nargs
< nparms
? nargs
: nparms
;
1700 tree binds
= make_tree_vec (nbinds
);
1701 for (i
= 0; i
< nargs
; ++i
)
1704 tree type
= parms
? TREE_TYPE (parms
) : void_type_node
;
1705 if (parms
&& DECL_BY_REFERENCE (parms
))
1706 type
= TREE_TYPE (type
);
1707 x
= get_nth_callarg (t
, i
);
1708 /* For member function, the first argument is a pointer to the implied
1709 object. For a constructor, it might still be a dummy object, in
1710 which case we get the real argument from ctx. */
1711 if (i
== 0 && DECL_CONSTRUCTOR_P (fun
)
1712 && is_dummy_object (x
))
1715 x
= build_address (x
);
1717 if (TREE_ADDRESSABLE (type
))
1718 /* Undo convert_for_arg_passing work here. */
1719 x
= convert_from_reference (x
);
1720 /* Normally we would strip a TARGET_EXPR in an initialization context
1721 such as this, but here we do the elision differently: we keep the
1722 TARGET_EXPR, and use its CONSTRUCTOR as the value of the parm. */
1723 arg
= cxx_eval_constant_expression (ctx
, x
, vc_prvalue
,
1724 non_constant_p
, overflow_p
);
1725 /* Don't VERIFY_CONSTANT here. */
1726 if (*non_constant_p
&& ctx
->quiet
)
1728 /* Just discard ellipsis args after checking their constantitude. */
1732 if (!*non_constant_p
)
1734 /* Make sure the binding has the same type as the parm. But
1735 only for constant args. */
1736 if (!TYPE_REF_P (type
))
1737 arg
= adjust_temp_type (type
, arg
);
1738 if (!TREE_CONSTANT (arg
))
1739 *non_constant_args
= true;
1740 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
1741 /* The destructor needs to see any modifications the callee makes
1743 *non_constant_args
= true;
1744 /* If arg is or contains address of a heap artificial variable or
1745 of a static variable being constructed, avoid caching the
1746 function call, as those variables might be modified by the
1747 function, or might be modified by the callers in between
1748 the cached function and just read by the function. */
1749 else if (!*non_constant_args
1750 && cp_walk_tree (&arg
, addr_of_non_const_var
, ctx
->global
,
1752 *non_constant_args
= true;
1754 /* For virtual calls, adjust the this argument, so that it is
1755 the object on which the method is called, rather than
1756 one of its bases. */
1757 if (i
== 0 && DECL_VIRTUAL_P (fun
))
1761 if (TREE_CODE (addr
) == ADDR_EXPR
)
1763 tree obj
= TREE_OPERAND (addr
, 0);
1764 while (TREE_CODE (obj
) == COMPONENT_REF
1765 && DECL_FIELD_IS_BASE (TREE_OPERAND (obj
, 1))
1766 && !same_type_ignoring_top_level_qualifiers_p
1767 (TREE_TYPE (obj
), DECL_CONTEXT (fun
)))
1768 obj
= TREE_OPERAND (obj
, 0);
1769 if (obj
!= TREE_OPERAND (addr
, 0))
1770 arg
= build_fold_addr_expr_with_type (obj
,
1774 TREE_VEC_ELT (binds
, i
) = arg
;
1776 parms
= TREE_CHAIN (parms
);
1782 /* Variables and functions to manage constexpr call expansion context.
1783 These do not need to be marked for PCH or GC. */
1785 /* FIXME remember and print actual constant arguments. */
1786 static vec
<tree
> call_stack
;
1787 static int call_stack_tick
;
1788 static int last_cx_error_tick
;
1791 push_cx_call_context (tree call
)
1794 if (!EXPR_HAS_LOCATION (call
))
1795 SET_EXPR_LOCATION (call
, input_location
);
1796 call_stack
.safe_push (call
);
1797 int len
= call_stack
.length ();
1798 if (len
> max_constexpr_depth
)
1804 pop_cx_call_context (void)
1811 cx_error_context (void)
1813 vec
<tree
> r
= vNULL
;
1814 if (call_stack_tick
!= last_cx_error_tick
1815 && !call_stack
.is_empty ())
1817 last_cx_error_tick
= call_stack_tick
;
1821 /* Evaluate a call T to a GCC internal function when possible and return
1822 the evaluated result or, under the control of CTX, give an error, set
1823 NON_CONSTANT_P, and return the unevaluated call T otherwise. */
1826 cxx_eval_internal_function (const constexpr_ctx
*ctx
, tree t
,
1828 bool *non_constant_p
, bool *overflow_p
)
1830 enum tree_code opcode
= ERROR_MARK
;
1832 switch (CALL_EXPR_IFN (t
))
1834 case IFN_UBSAN_NULL
:
1835 case IFN_UBSAN_BOUNDS
:
1836 case IFN_UBSAN_VPTR
:
1837 case IFN_FALLTHROUGH
:
1840 case IFN_ADD_OVERFLOW
:
1843 case IFN_SUB_OVERFLOW
:
1844 opcode
= MINUS_EXPR
;
1846 case IFN_MUL_OVERFLOW
:
1851 return cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 0),
1852 vc_prvalue
, non_constant_p
,
1855 case IFN_VEC_CONVERT
:
1857 tree arg
= cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 0),
1858 vc_prvalue
, non_constant_p
,
1860 if (TREE_CODE (arg
) == VECTOR_CST
)
1861 if (tree r
= fold_const_call (CFN_VEC_CONVERT
, TREE_TYPE (t
), arg
))
1868 error_at (cp_expr_loc_or_input_loc (t
),
1869 "call to internal function %qE", t
);
1870 *non_constant_p
= true;
1874 /* Evaluate constant arguments using OPCODE and return a complex
1875 number containing the result and the overflow bit. */
1876 tree arg0
= cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 0), lval
,
1877 non_constant_p
, overflow_p
);
1878 tree arg1
= cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 1), lval
,
1879 non_constant_p
, overflow_p
);
1881 if (TREE_CODE (arg0
) == INTEGER_CST
&& TREE_CODE (arg1
) == INTEGER_CST
)
1883 location_t loc
= cp_expr_loc_or_input_loc (t
);
1884 tree type
= TREE_TYPE (TREE_TYPE (t
));
1885 tree result
= fold_binary_loc (loc
, opcode
, type
,
1886 fold_convert_loc (loc
, type
, arg0
),
1887 fold_convert_loc (loc
, type
, arg1
));
1889 = build_int_cst (type
, arith_overflowed_p (opcode
, type
, arg0
, arg1
));
1890 /* Reset TREE_OVERFLOW to avoid warnings for the overflow. */
1891 if (TREE_OVERFLOW (result
))
1892 TREE_OVERFLOW (result
) = 0;
1894 return build_complex (TREE_TYPE (t
), result
, ovf
);
1897 *non_constant_p
= true;
1901 /* Clean CONSTRUCTOR_NO_CLEARING from CTOR and its sub-aggregates. */
1904 clear_no_implicit_zero (tree ctor
)
1906 if (CONSTRUCTOR_NO_CLEARING (ctor
))
1908 CONSTRUCTOR_NO_CLEARING (ctor
) = false;
1909 for (auto &e
: CONSTRUCTOR_ELTS (ctor
))
1910 if (TREE_CODE (e
.value
) == CONSTRUCTOR
)
1911 clear_no_implicit_zero (e
.value
);
1915 /* Complain about a const object OBJ being modified in a constant expression.
1916 EXPR is the MODIFY_EXPR expression performing the modification. */
1919 modifying_const_object_error (tree expr
, tree obj
)
1921 location_t loc
= cp_expr_loc_or_input_loc (expr
);
1922 auto_diagnostic_group d
;
1923 error_at (loc
, "modifying a const object %qE is not allowed in "
1924 "a constant expression", TREE_OPERAND (expr
, 0));
1925 inform (location_of (obj
), "originally declared %<const%> here");
1928 /* Return true if FNDECL is a replaceable global allocation function that
1929 should be useable during constant expression evaluation. */
1932 cxx_replaceable_global_alloc_fn (tree fndecl
)
1934 return (cxx_dialect
>= cxx20
1935 && IDENTIFIER_NEWDEL_OP_P (DECL_NAME (fndecl
))
1936 && CP_DECL_CONTEXT (fndecl
) == global_namespace
1937 && (DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl
)
1938 || DECL_IS_OPERATOR_DELETE_P (fndecl
)));
1941 /* Return true if FNDECL is a placement new function that should be
1942 useable during constant expression evaluation of std::construct_at. */
1945 cxx_placement_new_fn (tree fndecl
)
1947 if (cxx_dialect
>= cxx20
1948 && IDENTIFIER_NEW_OP_P (DECL_NAME (fndecl
))
1949 && CP_DECL_CONTEXT (fndecl
) == global_namespace
1950 && !DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl
)
1951 && TREE_CODE (TREE_TYPE (fndecl
)) == FUNCTION_TYPE
)
1953 tree first_arg
= TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
1954 if (TREE_VALUE (first_arg
) == ptr_type_node
1955 && TREE_CHAIN (first_arg
) == void_list_node
)
1961 /* Return true if FNDECL is std::construct_at. */
1964 is_std_construct_at (tree fndecl
)
1966 if (!decl_in_std_namespace_p (fndecl
))
1969 tree name
= DECL_NAME (fndecl
);
1970 return name
&& id_equal (name
, "construct_at");
1973 /* Overload for the above taking constexpr_call*. */
1976 is_std_construct_at (const constexpr_call
*call
)
1980 && is_std_construct_at (call
->fundef
->decl
));
1983 /* Return true if FNDECL is std::allocator<T>::{,de}allocate. */
1986 is_std_allocator_allocate (tree fndecl
)
1988 tree name
= DECL_NAME (fndecl
);
1989 if (name
== NULL_TREE
1990 || !(id_equal (name
, "allocate") || id_equal (name
, "deallocate")))
1993 tree ctx
= DECL_CONTEXT (fndecl
);
1994 if (ctx
== NULL_TREE
|| !CLASS_TYPE_P (ctx
) || !TYPE_MAIN_DECL (ctx
))
1997 tree decl
= TYPE_MAIN_DECL (ctx
);
1998 name
= DECL_NAME (decl
);
1999 if (name
== NULL_TREE
|| !id_equal (name
, "allocator"))
2002 return decl_in_std_namespace_p (decl
);
2005 /* Overload for the above taking constexpr_call*. */
2008 is_std_allocator_allocate (const constexpr_call
*call
)
2012 && is_std_allocator_allocate (call
->fundef
->decl
));
2015 /* Return true if FNDECL is __dynamic_cast. */
2018 cxx_dynamic_cast_fn_p (tree fndecl
)
2020 return (cxx_dialect
>= cxx20
2021 && id_equal (DECL_NAME (fndecl
), "__dynamic_cast")
2022 && CP_DECL_CONTEXT (fndecl
) == global_namespace
);
2025 /* Often, we have an expression in the form of address + offset, e.g.
2026 "&_ZTV1A + 16". Extract the object from it, i.e. "_ZTV1A". */
2029 extract_obj_from_addr_offset (tree expr
)
2031 if (TREE_CODE (expr
) == POINTER_PLUS_EXPR
)
2032 expr
= TREE_OPERAND (expr
, 0);
2034 if (TREE_CODE (expr
) == ADDR_EXPR
)
2035 expr
= TREE_OPERAND (expr
, 0);
2039 /* Given a PATH like
2041 g.D.2181.D.2154.D.2102.D.2093
2043 find a component with type TYPE. Return NULL_TREE if not found, and
2044 error_mark_node if the component is not accessible. If STOP is non-null,
2045 this function will return NULL_TREE if STOP is found before TYPE. */
2048 get_component_with_type (tree path
, tree type
, tree stop
)
2052 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path
), type
))
2056 && (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path
),
2059 else if (TREE_CODE (path
) == COMPONENT_REF
2060 && DECL_FIELD_IS_BASE (TREE_OPERAND (path
, 1)))
2062 /* We need to check that the component we're accessing is in fact
2064 if (TREE_PRIVATE (TREE_OPERAND (path
, 1))
2065 || TREE_PROTECTED (TREE_OPERAND (path
, 1)))
2066 return error_mark_node
;
2067 path
= TREE_OPERAND (path
, 0);
2074 /* Evaluate a call to __dynamic_cast (permitted by P1327R1).
2076 The declaration of __dynamic_cast is:
2078 void* __dynamic_cast (const void* __src_ptr,
2079 const __class_type_info* __src_type,
2080 const __class_type_info* __dst_type,
2081 ptrdiff_t __src2dst);
2083 where src2dst has the following possible values
2085 >-1: src_type is a unique public non-virtual base of dst_type
2086 dst_ptr + src2dst == src_ptr
2087 -1: unspecified relationship
2088 -2: src_type is not a public base of dst_type
2089 -3: src_type is a multiple public non-virtual base of dst_type
2091 Since literal types can't have virtual bases, we only expect hint >=0,
2095 cxx_eval_dynamic_cast_fn (const constexpr_ctx
*ctx
, tree call
,
2096 bool *non_constant_p
, bool *overflow_p
)
2098 /* T will be something like
2099 __dynamic_cast ((B*) b, &_ZTI1B, &_ZTI1D, 8)
2101 gcc_assert (call_expr_nargs (call
) == 4);
2102 tsubst_flags_t complain
= ctx
->quiet
? tf_none
: tf_warning_or_error
;
2103 tree obj
= CALL_EXPR_ARG (call
, 0);
2104 tree type
= CALL_EXPR_ARG (call
, 2);
2105 HOST_WIDE_INT hint
= int_cst_value (CALL_EXPR_ARG (call
, 3));
2106 location_t loc
= cp_expr_loc_or_input_loc (call
);
2108 /* Get the target type of the dynamic_cast. */
2109 gcc_assert (TREE_CODE (type
) == ADDR_EXPR
);
2110 type
= TREE_OPERAND (type
, 0);
2111 type
= TREE_TYPE (DECL_NAME (type
));
2113 /* TYPE can only be either T* or T&. We can't know which of these it
2114 is by looking at TYPE, but OBJ will be "(T*) x" in the first case,
2115 and something like "(T*)(T&)(T*) x" in the second case. */
2116 bool reference_p
= false;
2117 while (CONVERT_EXPR_P (obj
) || TREE_CODE (obj
) == SAVE_EXPR
)
2119 reference_p
|= TYPE_REF_P (TREE_TYPE (obj
));
2120 obj
= TREE_OPERAND (obj
, 0);
2123 /* Evaluate the object so that we know its dynamic type. */
2124 obj
= cxx_eval_constant_expression (ctx
, obj
, vc_prvalue
, non_constant_p
,
2126 if (*non_constant_p
)
2129 /* We expect OBJ to be in form of &d.D.2102 when HINT == 0,
2130 but when HINT is > 0, it can also be something like
2131 &d.D.2102 + 18446744073709551608, which includes the BINFO_OFFSET. */
2132 obj
= extract_obj_from_addr_offset (obj
);
2133 const tree objtype
= TREE_TYPE (obj
);
2134 /* If OBJ doesn't refer to a base field, we're done. */
2135 if (tree t
= (TREE_CODE (obj
) == COMPONENT_REF
2136 ? TREE_OPERAND (obj
, 1) : obj
))
2137 if (TREE_CODE (t
) != FIELD_DECL
|| !DECL_FIELD_IS_BASE (t
))
2143 error_at (loc
, "reference %<dynamic_cast%> failed");
2144 inform (loc
, "dynamic type %qT of its operand does "
2145 "not have a base class of type %qT",
2148 *non_constant_p
= true;
2150 return integer_zero_node
;
2153 /* [class.cdtor] When a dynamic_cast is used in a constructor ...
2154 or in a destructor ... if the operand of the dynamic_cast refers
2155 to the object under construction or destruction, this object is
2156 considered to be a most derived object that has the type of the
2157 constructor or destructor's class. */
2158 tree vtable
= build_vfield_ref (obj
, objtype
);
2159 vtable
= cxx_eval_constant_expression (ctx
, vtable
, vc_prvalue
,
2160 non_constant_p
, overflow_p
);
2161 if (*non_constant_p
)
2163 /* With -fsanitize=vptr, we initialize all vtable pointers to null,
2164 so it's possible that we got a null pointer now. */
2165 if (integer_zerop (vtable
))
2168 error_at (loc
, "virtual table pointer is used uninitialized");
2169 *non_constant_p
= true;
2170 return integer_zero_node
;
2172 /* VTABLE will be &_ZTV1A + 16 or similar, get _ZTV1A. */
2173 vtable
= extract_obj_from_addr_offset (vtable
);
2174 const tree mdtype
= DECL_CONTEXT (vtable
);
2176 /* Given dynamic_cast<T>(v),
2178 [expr.dynamic.cast] If C is the class type to which T points or refers,
2179 the runtime check logically executes as follows:
2181 If, in the most derived object pointed (referred) to by v, v points
2182 (refers) to a public base class subobject of a C object, and if only
2183 one object of type C is derived from the subobject pointed (referred)
2184 to by v the result points (refers) to that C object.
2186 In this case, HINT >= 0 or -3. */
2187 if (hint
>= 0 || hint
== -3)
2189 /* Look for a component with type TYPE. */
2190 tree t
= get_component_with_type (obj
, type
, mdtype
);
2191 /* If not accessible, give an error. */
2192 if (t
== error_mark_node
)
2198 error_at (loc
, "reference %<dynamic_cast%> failed");
2199 inform (loc
, "static type %qT of its operand is a "
2200 "non-public base class of dynamic type %qT",
2204 *non_constant_p
= true;
2206 return integer_zero_node
;
2209 /* The result points to the TYPE object. */
2210 return cp_build_addr_expr (t
, complain
);
2211 /* Else, TYPE was not found, because the HINT turned out to be wrong.
2212 Fall through to the normal processing. */
2215 /* Otherwise, if v points (refers) to a public base class subobject of the
2216 most derived object, and the type of the most derived object has a base
2217 class, of type C, that is unambiguous and public, the result points
2218 (refers) to the C subobject of the most derived object.
2220 But it can also be an invalid case. */
2222 /* Get the most derived object. */
2223 obj
= get_component_with_type (obj
, mdtype
, NULL_TREE
);
2224 if (obj
== error_mark_node
)
2230 error_at (loc
, "reference %<dynamic_cast%> failed");
2231 inform (loc
, "static type %qT of its operand is a non-public"
2232 " base class of dynamic type %qT", objtype
, mdtype
);
2234 *non_constant_p
= true;
2236 return integer_zero_node
;
2241 /* Check that the type of the most derived object has a base class
2242 of type TYPE that is unambiguous and public. */
2244 tree binfo
= lookup_base (mdtype
, type
, ba_check
, &b_kind
, tf_none
);
2245 if (!binfo
|| binfo
== error_mark_node
)
2251 error_at (loc
, "reference %<dynamic_cast%> failed");
2252 if (b_kind
== bk_ambig
)
2253 inform (loc
, "%qT is an ambiguous base class of dynamic "
2254 "type %qT of its operand", type
, mdtype
);
2256 inform (loc
, "dynamic type %qT of its operand does not "
2257 "have an unambiguous public base class %qT",
2260 *non_constant_p
= true;
2262 return integer_zero_node
;
2264 /* If so, return the TYPE subobject of the most derived object. */
2265 obj
= convert_to_base_statically (obj
, binfo
);
2266 return cp_build_addr_expr (obj
, complain
);
2269 /* Data structure used by replace_decl and replace_decl_r. */
2271 struct replace_decl_data
2273 /* The _DECL we want to replace. */
2275 /* The replacement for DECL. */
2277 /* Trees we've visited. */
2278 hash_set
<tree
> *pset
;
2279 /* Whether we've performed any replacements. */
2283 /* Helper function for replace_decl, called through cp_walk_tree. */
2286 replace_decl_r (tree
*tp
, int *walk_subtrees
, void *data
)
2288 replace_decl_data
*d
= (replace_decl_data
*) data
;
2292 *tp
= unshare_expr (d
->replacement
);
2296 else if (TYPE_P (*tp
)
2297 || d
->pset
->add (*tp
))
2303 /* Replace every occurrence of DECL with (an unshared copy of)
2304 REPLACEMENT within the expression *TP. Returns true iff a
2305 replacement was performed. */
2308 replace_decl (tree
*tp
, tree decl
, tree replacement
)
2310 gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
2311 (TREE_TYPE (decl
), TREE_TYPE (replacement
)));
2312 hash_set
<tree
> pset
;
2313 replace_decl_data data
= { decl
, replacement
, &pset
, false };
2314 cp_walk_tree (tp
, replace_decl_r
, &data
, NULL
);
2315 return data
.changed
;
2318 /* Evaluate the call T to virtual function thunk THUNK_FNDECL. */
2321 cxx_eval_thunk_call (const constexpr_ctx
*ctx
, tree t
, tree thunk_fndecl
,
2323 bool *non_constant_p
, bool *overflow_p
)
2325 tree function
= THUNK_TARGET (thunk_fndecl
);
2327 if (THUNK_VIRTUAL_OFFSET (thunk_fndecl
))
2331 if (!DECL_DECLARED_CONSTEXPR_P (function
))
2333 error ("call to non-%<constexpr%> function %qD", function
);
2334 explain_invalid_constexpr_fn (function
);
2337 /* virtual_offset is only set for virtual bases, which make the
2338 class non-literal, so we don't need to handle it here. */
2339 error ("calling constexpr member function %qD through virtual "
2340 "base subobject", function
);
2342 *non_constant_p
= true;
2346 tree new_call
= copy_node (t
);
2347 CALL_EXPR_FN (new_call
) = function
;
2348 TREE_TYPE (new_call
) = TREE_TYPE (TREE_TYPE (function
));
2350 tree offset
= size_int (THUNK_FIXED_OFFSET (thunk_fndecl
));
2352 if (DECL_THIS_THUNK_P (thunk_fndecl
))
2354 /* 'this'-adjusting thunk. */
2355 tree this_arg
= CALL_EXPR_ARG (t
, 0);
2356 this_arg
= build2 (POINTER_PLUS_EXPR
, TREE_TYPE (this_arg
),
2358 CALL_EXPR_ARG (new_call
, 0) = this_arg
;
2361 /* Return-adjusting thunk. */
2362 new_call
= build2 (POINTER_PLUS_EXPR
, TREE_TYPE (new_call
),
2365 return cxx_eval_constant_expression (ctx
, new_call
, lval
,
2366 non_constant_p
, overflow_p
);
2369 /* If OBJECT is of const class type, evaluate it to a CONSTRUCTOR and set
2370 its TREE_READONLY flag according to READONLY_P. Used for constexpr
2371 'tors to detect modifying const objects in a constexpr context. */
2374 cxx_set_object_constness (const constexpr_ctx
*ctx
, tree object
,
2375 bool readonly_p
, bool *non_constant_p
,
2378 if (CLASS_TYPE_P (TREE_TYPE (object
))
2379 && CP_TYPE_CONST_P (TREE_TYPE (object
)))
2381 /* Subobjects might not be stored in ctx->global->values but we
2382 can get its CONSTRUCTOR by evaluating *this. */
2383 tree e
= cxx_eval_constant_expression (ctx
, object
, vc_prvalue
,
2384 non_constant_p
, overflow_p
);
2385 if (TREE_CODE (e
) == CONSTRUCTOR
&& !*non_constant_p
)
2386 TREE_READONLY (e
) = readonly_p
;
2390 /* Subroutine of cxx_eval_constant_expression.
2391 Evaluate the call expression tree T in the context of OLD_CALL expression
2395 cxx_eval_call_expression (const constexpr_ctx
*ctx
, tree t
,
2397 bool *non_constant_p
, bool *overflow_p
)
2399 /* Handle concept checks separately. */
2400 if (concept_check_p (t
))
2401 return evaluate_concept_check (t
);
2403 location_t loc
= cp_expr_loc_or_input_loc (t
);
2404 tree fun
= get_function_named_in_call (t
);
2405 constexpr_call new_call
2406 = { NULL
, NULL
, NULL
, 0, ctx
->manifestly_const_eval
};
2409 if (fun
== NULL_TREE
)
2410 return cxx_eval_internal_function (ctx
, t
, lval
,
2411 non_constant_p
, overflow_p
);
2413 if (TREE_CODE (fun
) != FUNCTION_DECL
)
2415 /* Might be a constexpr function pointer. */
2416 fun
= cxx_eval_constant_expression (ctx
, fun
, vc_prvalue
,
2417 non_constant_p
, overflow_p
);
2419 if (TREE_CODE (fun
) == ADDR_EXPR
)
2420 fun
= TREE_OPERAND (fun
, 0);
2421 /* For TARGET_VTABLE_USES_DESCRIPTORS targets, there is no
2422 indirection, the called expression is a pointer into the
2423 virtual table which should contain FDESC_EXPR. Extract the
2424 FUNCTION_DECL from there. */
2425 else if (TARGET_VTABLE_USES_DESCRIPTORS
2426 && TREE_CODE (fun
) == POINTER_PLUS_EXPR
2427 && TREE_CODE (TREE_OPERAND (fun
, 0)) == ADDR_EXPR
2428 && TREE_CODE (TREE_OPERAND (fun
, 1)) == INTEGER_CST
)
2430 tree d
= TREE_OPERAND (TREE_OPERAND (fun
, 0), 0);
2432 && DECL_VTABLE_OR_VTT_P (d
)
2433 && TREE_CODE (TREE_TYPE (d
)) == ARRAY_TYPE
2434 && TREE_TYPE (TREE_TYPE (d
)) == vtable_entry_type
2436 && TREE_CODE (DECL_INITIAL (d
)) == CONSTRUCTOR
)
2438 tree i
= int_const_binop (TRUNC_DIV_EXPR
, TREE_OPERAND (fun
, 1),
2439 TYPE_SIZE_UNIT (vtable_entry_type
));
2440 HOST_WIDE_INT idx
= find_array_ctor_elt (DECL_INITIAL (d
), i
);
2444 = (*CONSTRUCTOR_ELTS (DECL_INITIAL (d
)))[idx
].value
;
2445 if (TREE_CODE (fdesc
) == FDESC_EXPR
2446 && integer_zerop (TREE_OPERAND (fdesc
, 1)))
2447 fun
= TREE_OPERAND (fdesc
, 0);
2452 if (TREE_CODE (fun
) != FUNCTION_DECL
)
2454 if (!ctx
->quiet
&& !*non_constant_p
)
2455 error_at (loc
, "expression %qE does not designate a %<constexpr%> "
2457 *non_constant_p
= true;
2460 if (DECL_CLONED_FUNCTION_P (fun
) && !DECL_DELETING_DESTRUCTOR_P (fun
))
2461 fun
= DECL_CLONED_FUNCTION (fun
);
2463 if (is_ubsan_builtin_p (fun
))
2466 if (fndecl_built_in_p (fun
))
2467 return cxx_eval_builtin_function_call (ctx
, t
, fun
,
2468 lval
, non_constant_p
, overflow_p
);
2469 if (DECL_THUNK_P (fun
))
2470 return cxx_eval_thunk_call (ctx
, t
, fun
, lval
, non_constant_p
, overflow_p
);
2471 if (!maybe_constexpr_fn (fun
))
2473 if (TREE_CODE (t
) == CALL_EXPR
2474 && cxx_replaceable_global_alloc_fn (fun
)
2475 && (CALL_FROM_NEW_OR_DELETE_P (t
)
2476 || is_std_allocator_allocate (ctx
->call
)))
2478 const int nargs
= call_expr_nargs (t
);
2479 tree arg0
= NULL_TREE
;
2480 for (int i
= 0; i
< nargs
; ++i
)
2482 tree arg
= CALL_EXPR_ARG (t
, i
);
2483 arg
= cxx_eval_constant_expression (ctx
, arg
, vc_prvalue
,
2484 non_constant_p
, overflow_p
);
2485 VERIFY_CONSTANT (arg
);
2490 if (IDENTIFIER_NEW_OP_P (DECL_NAME (fun
)))
2492 tree type
= build_array_type_nelts (char_type_node
,
2493 tree_to_uhwi (arg0
));
2494 tree var
= build_decl (loc
, VAR_DECL
,
2495 (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun
))
2497 ? heap_vec_uninit_identifier
2498 : heap_uninit_identifier
,
2500 DECL_ARTIFICIAL (var
) = 1;
2501 TREE_STATIC (var
) = 1;
2502 // Temporarily register the artificial var in varpool,
2503 // so that comparisons of its address against NULL are folded
2504 // through nonzero_address even with
2505 // -fno-delete-null-pointer-checks or that comparison of
2506 // addresses of different heap artificial vars is folded too.
2507 // See PR98988 and PR99031.
2508 varpool_node::finalize_decl (var
);
2509 ctx
->global
->heap_vars
.safe_push (var
);
2510 ctx
->global
->values
.put (var
, NULL_TREE
);
2511 return fold_convert (ptr_type_node
, build_address (var
));
2516 if (TREE_CODE (arg0
) == ADDR_EXPR
2517 && VAR_P (TREE_OPERAND (arg0
, 0)))
2519 tree var
= TREE_OPERAND (arg0
, 0);
2520 if (DECL_NAME (var
) == heap_uninit_identifier
2521 || DECL_NAME (var
) == heap_identifier
)
2523 if (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun
))
2528 error_at (loc
, "array deallocation of object "
2529 "allocated with non-array "
2531 inform (DECL_SOURCE_LOCATION (var
),
2532 "allocation performed here");
2534 *non_constant_p
= true;
2537 DECL_NAME (var
) = heap_deleted_identifier
;
2538 ctx
->global
->values
.remove (var
);
2539 ctx
->global
->heap_dealloc_count
++;
2542 else if (DECL_NAME (var
) == heap_vec_uninit_identifier
2543 || DECL_NAME (var
) == heap_vec_identifier
)
2545 if ((IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun
))
2546 & OVL_OP_FLAG_VEC
) == 0)
2550 error_at (loc
, "non-array deallocation of "
2551 "object allocated with array "
2553 inform (DECL_SOURCE_LOCATION (var
),
2554 "allocation performed here");
2556 *non_constant_p
= true;
2559 DECL_NAME (var
) = heap_deleted_identifier
;
2560 ctx
->global
->values
.remove (var
);
2561 ctx
->global
->heap_dealloc_count
++;
2564 else if (DECL_NAME (var
) == heap_deleted_identifier
)
2567 error_at (loc
, "deallocation of already deallocated "
2569 *non_constant_p
= true;
2574 error_at (loc
, "deallocation of storage that was "
2575 "not previously allocated");
2576 *non_constant_p
= true;
2580 /* Allow placement new in std::construct_at, just return the second
2582 if (TREE_CODE (t
) == CALL_EXPR
2583 && cxx_placement_new_fn (fun
)
2584 && is_std_construct_at (ctx
->call
))
2586 const int nargs
= call_expr_nargs (t
);
2587 tree arg1
= NULL_TREE
;
2588 for (int i
= 0; i
< nargs
; ++i
)
2590 tree arg
= CALL_EXPR_ARG (t
, i
);
2591 arg
= cxx_eval_constant_expression (ctx
, arg
, vc_prvalue
,
2592 non_constant_p
, overflow_p
);
2596 VERIFY_CONSTANT (arg
);
2601 else if (cxx_dynamic_cast_fn_p (fun
))
2602 return cxx_eval_dynamic_cast_fn (ctx
, t
, non_constant_p
, overflow_p
);
2606 if (!lambda_static_thunk_p (fun
))
2607 error_at (loc
, "call to non-%<constexpr%> function %qD", fun
);
2608 explain_invalid_constexpr_fn (fun
);
2610 *non_constant_p
= true;
2614 constexpr_ctx new_ctx
= *ctx
;
2615 if (DECL_CONSTRUCTOR_P (fun
) && !ctx
->object
2616 && TREE_CODE (t
) == AGGR_INIT_EXPR
)
2618 /* We want to have an initialization target for an AGGR_INIT_EXPR.
2619 If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
2620 new_ctx
.object
= AGGR_INIT_EXPR_SLOT (t
);
2621 tree ctor
= new_ctx
.ctor
= build_constructor (DECL_CONTEXT (fun
), NULL
);
2622 CONSTRUCTOR_NO_CLEARING (ctor
) = true;
2623 ctx
->global
->values
.put (new_ctx
.object
, ctor
);
2627 /* Shortcut trivial constructor/op=. */
2628 if (trivial_fn_p (fun
))
2630 tree init
= NULL_TREE
;
2631 if (call_expr_nargs (t
) == 2)
2632 init
= convert_from_reference (get_nth_callarg (t
, 1));
2633 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
2634 && AGGR_INIT_ZERO_FIRST (t
))
2635 init
= build_zero_init (DECL_CONTEXT (fun
), NULL_TREE
, false);
2638 tree op
= get_nth_callarg (t
, 0);
2639 if (is_dummy_object (op
))
2642 op
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (op
)), op
);
2643 tree set
= build2 (MODIFY_EXPR
, TREE_TYPE (op
), op
, init
);
2644 new_ctx
.call
= &new_call
;
2645 return cxx_eval_constant_expression (&new_ctx
, set
, lval
,
2646 non_constant_p
, overflow_p
);
2650 bool non_constant_args
= false;
2652 = cxx_bind_parameters_in_call (ctx
, t
, fun
, non_constant_p
,
2653 overflow_p
, &non_constant_args
);
2655 /* We build up the bindings list before we know whether we already have this
2656 call cached. If we don't end up saving these bindings, ggc_free them when
2657 this function exits. */
2662 free_bindings (tree
&b
): bindings (&b
) { }
2663 ~free_bindings () { if (bindings
) ggc_free (*bindings
); }
2664 void preserve () { bindings
= NULL
; }
2665 } fb (new_call
.bindings
);
2667 if (*non_constant_p
)
2670 /* We can't defer instantiating the function any longer. */
2671 if (!DECL_INITIAL (fun
)
2672 && DECL_TEMPLOID_INSTANTIATION (fun
)
2673 && !uid_sensitive_constexpr_evaluation_p ())
2675 location_t save_loc
= input_location
;
2676 input_location
= loc
;
2678 if (ctx
->manifestly_const_eval
)
2679 FNDECL_MANIFESTLY_CONST_EVALUATED (fun
) = true;
2680 instantiate_decl (fun
, /*defer_ok*/false, /*expl_inst*/false);
2682 input_location
= save_loc
;
2685 /* If in direct recursive call, optimize definition search. */
2686 if (ctx
&& ctx
->call
&& ctx
->call
->fundef
&& ctx
->call
->fundef
->decl
== fun
)
2687 new_call
.fundef
= ctx
->call
->fundef
;
2690 new_call
.fundef
= retrieve_constexpr_fundef (fun
);
2691 if (new_call
.fundef
== NULL
|| new_call
.fundef
->body
== NULL
2692 || new_call
.fundef
->result
== error_mark_node
2693 || fun
== current_function_decl
)
2697 /* We need to check for current_function_decl here in case we're
2698 being called during cp_fold_function, because at that point
2699 DECL_INITIAL is set properly and we have a fundef but we
2700 haven't lowered invisirefs yet (c++/70344). */
2701 if (DECL_INITIAL (fun
) == error_mark_node
2702 || fun
== current_function_decl
)
2703 error_at (loc
, "%qD called in a constant expression before its "
2704 "definition is complete", fun
);
2705 else if (DECL_INITIAL (fun
))
2707 /* The definition of fun was somehow unsuitable. But pretend
2708 that lambda static thunks don't exist. */
2709 if (!lambda_static_thunk_p (fun
))
2710 error_at (loc
, "%qD called in a constant expression", fun
);
2711 explain_invalid_constexpr_fn (fun
);
2714 error_at (loc
, "%qD used before its definition", fun
);
2716 *non_constant_p
= true;
2721 depth_ok
= push_cx_call_context (t
);
2723 /* Remember the object we are constructing or destructing. */
2724 tree new_obj
= NULL_TREE
;
2725 if (DECL_CONSTRUCTOR_P (fun
) || DECL_DESTRUCTOR_P (fun
))
2727 /* In a cdtor, it should be the first `this' argument.
2728 At this point it has already been evaluated in the call
2729 to cxx_bind_parameters_in_call. */
2730 new_obj
= TREE_VEC_ELT (new_call
.bindings
, 0);
2731 new_obj
= cxx_fold_indirect_ref (ctx
, loc
, DECL_CONTEXT (fun
), new_obj
);
2733 if (ctx
->call
&& ctx
->call
->fundef
2734 && DECL_CONSTRUCTOR_P (ctx
->call
->fundef
->decl
))
2736 tree cur_obj
= TREE_VEC_ELT (ctx
->call
->bindings
, 0);
2737 STRIP_NOPS (cur_obj
);
2738 if (TREE_CODE (cur_obj
) == ADDR_EXPR
)
2739 cur_obj
= TREE_OPERAND (cur_obj
, 0);
2740 if (new_obj
== cur_obj
)
2741 /* We're calling the target constructor of a delegating
2742 constructor, or accessing a base subobject through a
2743 NOP_EXPR as part of a call to a base constructor, so
2744 there is no new (sub)object. */
2745 new_obj
= NULL_TREE
;
2749 tree result
= NULL_TREE
;
2751 constexpr_call
*entry
= NULL
;
2752 if (depth_ok
&& !non_constant_args
&& ctx
->strict
)
2754 new_call
.hash
= constexpr_fundef_hasher::hash (new_call
.fundef
);
2756 = iterative_hash_template_arg (new_call
.bindings
, new_call
.hash
);
2758 = iterative_hash_object (ctx
->manifestly_const_eval
, new_call
.hash
);
2760 /* If we have seen this call before, we are done. */
2761 maybe_initialize_constexpr_call_table ();
2762 constexpr_call
**slot
2763 = constexpr_call_table
->find_slot (&new_call
, INSERT
);
2767 /* Only cache up to constexpr_cache_depth to limit memory use. */
2768 if (depth_ok
< constexpr_cache_depth
)
2770 /* We need to keep a pointer to the entry, not just the slot, as
2771 the slot can move during evaluation of the body. */
2772 *slot
= entry
= ggc_alloc
<constexpr_call
> ();
2777 /* Calls that are in progress have their result set to NULL, so that we
2778 can detect circular dependencies. Now that we only cache up to
2779 constexpr_cache_depth this won't catch circular dependencies that
2780 start deeper, but they'll hit the recursion or ops limit. */
2781 else if (entry
->result
== NULL
)
2784 error ("call has circular dependency");
2785 *non_constant_p
= true;
2786 entry
->result
= result
= error_mark_node
;
2789 result
= entry
->result
;
2795 error ("%<constexpr%> evaluation depth exceeds maximum of %d (use "
2796 "%<-fconstexpr-depth=%> to increase the maximum)",
2797 max_constexpr_depth
);
2798 *non_constant_p
= true;
2799 result
= error_mark_node
;
2803 bool cacheable
= true;
2804 if (result
&& result
!= error_mark_node
)
2806 else if (!DECL_SAVED_TREE (fun
))
2808 /* When at_eof >= 2, cgraph has started throwing away
2809 DECL_SAVED_TREE, so fail quietly. FIXME we get here because of
2810 late code generation for VEC_INIT_EXPR, which needs to be
2811 completely reconsidered. */
2812 gcc_assert (at_eof
>= 2 && ctx
->quiet
);
2813 *non_constant_p
= true;
2815 else if (tree copy
= get_fundef_copy (new_call
.fundef
))
2817 tree body
, parms
, res
;
2818 releasing_vec ctors
;
2820 /* Reuse or create a new unshared copy of this function's body. */
2821 body
= TREE_PURPOSE (copy
);
2822 parms
= TREE_VALUE (copy
);
2823 res
= TREE_TYPE (copy
);
2825 /* Associate the bindings with the remapped parms. */
2826 tree bound
= new_call
.bindings
;
2827 tree remapped
= parms
;
2828 for (int i
= 0; i
< TREE_VEC_LENGTH (bound
); ++i
)
2830 tree arg
= TREE_VEC_ELT (bound
, i
);
2833 /* Unshare args going into the hash table to separate them
2834 from the caller's context, for better GC and to avoid
2835 problems with verify_gimple. */
2836 arg
= unshare_expr_without_location (arg
);
2837 TREE_VEC_ELT (bound
, i
) = arg
;
2839 /* And then unshare again so the callee doesn't change the
2840 argument values in the hash table. XXX Could we unshare
2841 lazily in cxx_eval_store_expression? */
2842 arg
= unshare_constructor (arg
);
2843 if (TREE_CODE (arg
) == CONSTRUCTOR
)
2844 vec_safe_push (ctors
, arg
);
2846 ctx
->global
->values
.put (remapped
, arg
);
2847 remapped
= DECL_CHAIN (remapped
);
2849 /* Add the RESULT_DECL to the values map, too. */
2850 gcc_assert (!DECL_BY_REFERENCE (res
));
2851 ctx
->global
->values
.put (res
, NULL_TREE
);
2853 /* Track the callee's evaluated SAVE_EXPRs and TARGET_EXPRs so that
2854 we can forget their values after the call. */
2855 constexpr_ctx ctx_with_save_exprs
= *ctx
;
2856 auto_vec
<tree
, 10> save_exprs
;
2857 ctx_with_save_exprs
.save_exprs
= &save_exprs
;
2858 ctx_with_save_exprs
.call
= &new_call
;
2859 unsigned save_heap_alloc_count
= ctx
->global
->heap_vars
.length ();
2860 unsigned save_heap_dealloc_count
= ctx
->global
->heap_dealloc_count
;
2862 /* If this is a constexpr destructor, the object's const and volatile
2863 semantics are no longer in effect; see [class.dtor]p5. */
2864 if (new_obj
&& DECL_DESTRUCTOR_P (fun
))
2865 cxx_set_object_constness (ctx
, new_obj
, /*readonly_p=*/false,
2866 non_constant_p
, overflow_p
);
2868 tree jump_target
= NULL_TREE
;
2869 cxx_eval_constant_expression (&ctx_with_save_exprs
, body
,
2870 vc_discard
, non_constant_p
, overflow_p
,
2873 if (DECL_CONSTRUCTOR_P (fun
))
2875 /* This can be null for a subobject constructor call, in
2876 which case what we care about is the initialization
2877 side-effects rather than the value. We could get at the
2878 value by evaluating *this, but we don't bother; there's
2879 no need to put such a call in the hash table. */
2880 result
= lval
? ctx
->object
: ctx
->ctor
;
2882 /* If we've just evaluated a subobject constructor call for an
2883 empty union member, it might not have produced a side effect
2884 that actually activated the union member. So produce such a
2885 side effect now to ensure the union appears initialized. */
2886 if (!result
&& new_obj
2887 && TREE_CODE (new_obj
) == COMPONENT_REF
2888 && TREE_CODE (TREE_TYPE
2889 (TREE_OPERAND (new_obj
, 0))) == UNION_TYPE
2890 && is_really_empty_class (TREE_TYPE (new_obj
),
2891 /*ignore_vptr*/false))
2893 tree activate
= build2 (MODIFY_EXPR
, TREE_TYPE (new_obj
),
2895 build_constructor (TREE_TYPE (new_obj
),
2897 cxx_eval_constant_expression (ctx
, activate
, lval
,
2898 non_constant_p
, overflow_p
);
2899 ggc_free (activate
);
2902 else if (VOID_TYPE_P (TREE_TYPE (res
)))
2906 result
= *ctx
->global
->values
.get (res
);
2907 if (result
== NULL_TREE
&& !*non_constant_p
2908 && !DECL_DESTRUCTOR_P (fun
))
2911 error ("%<constexpr%> call flows off the end "
2913 *non_constant_p
= true;
2917 /* At this point, the object's constructor will have run, so
2918 the object is no longer under construction, and its possible
2919 'const' semantics now apply. Make a note of this fact by
2920 marking the CONSTRUCTOR TREE_READONLY. */
2921 if (new_obj
&& DECL_CONSTRUCTOR_P (fun
))
2922 cxx_set_object_constness (ctx
, new_obj
, /*readonly_p=*/true,
2923 non_constant_p
, overflow_p
);
2925 /* Forget the saved values of the callee's SAVE_EXPRs and
2927 for (tree save_expr
: save_exprs
)
2928 ctx
->global
->values
.remove (save_expr
);
2930 /* Remove the parms/result from the values map. Is it worth
2931 bothering to do this when the map itself is only live for
2932 one constexpr evaluation? If so, maybe also clear out
2933 other vars from call, maybe in BIND_EXPR handling? */
2934 ctx
->global
->values
.remove (res
);
2935 for (tree parm
= parms
; parm
; parm
= TREE_CHAIN (parm
))
2936 ctx
->global
->values
.remove (parm
);
2938 /* Free any parameter CONSTRUCTORs we aren't returning directly. */
2939 while (!ctors
->is_empty ())
2941 tree c
= ctors
->pop ();
2943 free_constructor (c
);
2946 /* Make the unshared function copy we used available for re-use. */
2947 save_fundef_copy (fun
, copy
);
2949 /* If the call allocated some heap object that hasn't been
2950 deallocated during the call, or if it deallocated some heap
2951 object it has not allocated, the call isn't really stateless
2952 for the constexpr evaluation and should not be cached.
2953 It is fine if the call allocates something and deallocates it
2956 && (save_heap_alloc_count
!= ctx
->global
->heap_vars
.length ()
2957 || (save_heap_dealloc_count
2958 != ctx
->global
->heap_dealloc_count
)))
2962 if ((ctx
->global
->heap_vars
.length ()
2963 - ctx
->global
->heap_dealloc_count
)
2964 != save_heap_alloc_count
- save_heap_dealloc_count
)
2967 FOR_EACH_VEC_ELT_FROM (ctx
->global
->heap_vars
, i
, heap_var
,
2968 save_heap_alloc_count
)
2969 if (DECL_NAME (heap_var
) != heap_deleted_identifier
)
2974 /* Also don't cache a call that returns a deallocated pointer. */
2975 if (cacheable
&& (cp_walk_tree_without_duplicates
2976 (&result
, find_heap_var_refs
, NULL
)))
2980 /* Rewrite all occurrences of the function's RESULT_DECL with the
2981 current object under construction. */
2982 if (!*non_constant_p
&& ctx
->object
2983 && CLASS_TYPE_P (TREE_TYPE (res
))
2984 && !is_empty_class (TREE_TYPE (res
)))
2985 if (replace_decl (&result
, res
, ctx
->object
))
2989 /* Couldn't get a function copy to evaluate. */
2990 *non_constant_p
= true;
2992 if (result
== error_mark_node
)
2993 *non_constant_p
= true;
2994 if (*non_constant_p
|| *overflow_p
)
2995 result
= error_mark_node
;
2999 entry
->result
= cacheable
? result
: error_mark_node
;
3002 /* The result of a constexpr function must be completely initialized.
3004 However, in C++20, a constexpr constructor doesn't necessarily have
3005 to initialize all the fields, so we don't clear CONSTRUCTOR_NO_CLEARING
3006 in order to detect reading an unitialized object in constexpr instead
3007 of value-initializing it. (reduced_constant_expression_p is expected to
3008 take care of clearing the flag.) */
3009 if (TREE_CODE (result
) == CONSTRUCTOR
3010 && (cxx_dialect
< cxx20
3011 || !DECL_CONSTRUCTOR_P (fun
)))
3012 clear_no_implicit_zero (result
);
3014 pop_cx_call_context ();
3018 /* Return true if T is a valid constant initializer. If a CONSTRUCTOR
3019 initializes all the members, the CONSTRUCTOR_NO_CLEARING flag will be
3021 FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
3024 reduced_constant_expression_p (tree t
)
3029 switch (TREE_CODE (t
))
3032 /* Even if we can't lower this yet, it's constant. */
3036 /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
3038 if (CONSTRUCTOR_NO_CLEARING (t
))
3040 if (TREE_CODE (TREE_TYPE (t
)) == VECTOR_TYPE
)
3041 /* An initialized vector would have a VECTOR_CST. */
3043 else if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
3045 /* There must be a valid constant initializer at every array
3047 tree min
= TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (t
)));
3048 tree max
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (t
)));
3050 for (auto &e
: CONSTRUCTOR_ELTS (t
))
3052 if (!reduced_constant_expression_p (e
.value
))
3054 if (array_index_cmp (cursor
, e
.index
) != 0)
3056 if (TREE_CODE (e
.index
) == RANGE_EXPR
)
3057 cursor
= TREE_OPERAND (e
.index
, 1);
3058 cursor
= int_const_binop (PLUS_EXPR
, cursor
, size_one_node
);
3060 if (find_array_ctor_elt (t
, max
) == -1)
3064 else if (cxx_dialect
>= cxx20
3065 && TREE_CODE (TREE_TYPE (t
)) == UNION_TYPE
)
3067 if (CONSTRUCTOR_NELTS (t
) == 0)
3068 /* An initialized union has a constructor element. */
3070 /* And it only initializes one member. */
3074 field
= next_subobject_field (TYPE_FIELDS (TREE_TYPE (t
)));
3078 for (auto &e
: CONSTRUCTOR_ELTS (t
))
3080 /* If VAL is null, we're in the middle of initializing this
3082 if (!reduced_constant_expression_p (e
.value
))
3084 /* We want to remove initializers for empty fields in a struct to
3085 avoid confusing output_constructor. */
3086 if (is_empty_field (e
.index
)
3087 && TREE_CODE (TREE_TYPE (t
)) == RECORD_TYPE
)
3089 /* Check for non-empty fields between initialized fields when
3090 CONSTRUCTOR_NO_CLEARING. */
3091 for (; field
&& e
.index
!= field
;
3092 field
= next_subobject_field (DECL_CHAIN (field
)))
3093 if (!is_really_empty_class (TREE_TYPE (field
),
3094 /*ignore_vptr*/false))
3097 field
= next_subobject_field (DECL_CHAIN (field
));
3099 /* There could be a non-empty field at the end. */
3100 for (; field
; field
= next_subobject_field (DECL_CHAIN (field
)))
3101 if (!is_really_empty_class (TREE_TYPE (field
), /*ignore_vptr*/false))
3104 if (CONSTRUCTOR_NO_CLEARING (t
))
3105 /* All the fields are initialized. */
3106 CONSTRUCTOR_NO_CLEARING (t
) = false;
3110 /* FIXME are we calling this too much? */
3111 return initializer_constant_valid_p (t
, TREE_TYPE (t
)) != NULL_TREE
;
3115 /* Some expressions may have constant operands but are not constant
3116 themselves, such as 1/0. Call this function to check for that
3119 We only call this in places that require an arithmetic constant, not in
3120 places where we might have a non-constant expression that can be a
3121 component of a constant expression, such as the address of a constexpr
3122 variable that might be dereferenced later. */
3125 verify_constant (tree t
, bool allow_non_constant
, bool *non_constant_p
,
3128 if (!*non_constant_p
&& !reduced_constant_expression_p (t
)
3131 if (!allow_non_constant
)
3132 error ("%q+E is not a constant expression", t
);
3133 *non_constant_p
= true;
3135 if (TREE_OVERFLOW_P (t
))
3137 if (!allow_non_constant
)
3139 permerror (input_location
, "overflow in constant expression");
3140 /* If we're being permissive (and are in an enforcing
3141 context), ignore the overflow. */
3142 if (flag_permissive
)
3143 return *non_constant_p
;
3147 return *non_constant_p
;
3150 /* Check whether the shift operation with code CODE and type TYPE on LHS
3151 and RHS is undefined. If it is, give an error with an explanation,
3152 and return true; return false otherwise. */
3155 cxx_eval_check_shift_p (location_t loc
, const constexpr_ctx
*ctx
,
3156 enum tree_code code
, tree type
, tree lhs
, tree rhs
)
3158 if ((code
!= LSHIFT_EXPR
&& code
!= RSHIFT_EXPR
)
3159 || TREE_CODE (lhs
) != INTEGER_CST
3160 || TREE_CODE (rhs
) != INTEGER_CST
)
3163 tree lhstype
= TREE_TYPE (lhs
);
3164 unsigned HOST_WIDE_INT uprec
= TYPE_PRECISION (TREE_TYPE (lhs
));
3166 /* [expr.shift] The behavior is undefined if the right operand
3167 is negative, or greater than or equal to the length in bits
3168 of the promoted left operand. */
3169 if (tree_int_cst_sgn (rhs
) == -1)
3172 permerror (loc
, "right operand of shift expression %q+E is negative",
3173 build2_loc (loc
, code
, type
, lhs
, rhs
));
3174 return (!flag_permissive
|| ctx
->quiet
);
3176 if (compare_tree_int (rhs
, uprec
) >= 0)
3179 permerror (loc
, "right operand of shift expression %q+E is greater "
3180 "than or equal to the precision %wu of the left operand",
3181 build2_loc (loc
, code
, type
, lhs
, rhs
), uprec
);
3182 return (!flag_permissive
|| ctx
->quiet
);
3185 /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
3186 if E1 has a signed type and non-negative value, and E1x2^E2 is
3187 representable in the corresponding unsigned type of the result type,
3188 then that value, converted to the result type, is the resulting value;
3189 otherwise, the behavior is undefined.
3191 The value of E1 << E2 is the unique value congruent to E1 x 2^E2 modulo
3192 2^N, where N is the range exponent of the type of the result. */
3193 if (code
== LSHIFT_EXPR
3194 && !TYPE_OVERFLOW_WRAPS (lhstype
)
3195 && cxx_dialect
>= cxx11
3196 && cxx_dialect
< cxx20
)
3198 if (tree_int_cst_sgn (lhs
) == -1)
3202 "left operand of shift expression %q+E is negative",
3203 build2_loc (loc
, code
, type
, lhs
, rhs
));
3204 return (!flag_permissive
|| ctx
->quiet
);
3206 /* For signed x << y the following:
3207 (unsigned) x >> ((prec (lhs) - 1) - y)
3208 if > 1, is undefined. The right-hand side of this formula
3209 is the highest bit of the LHS that can be set (starting from 0),
3210 so that the shift doesn't overflow. We then right-shift the LHS
3211 to see whether any other bit is set making the original shift
3212 undefined -- the result is not representable in the corresponding
3214 tree t
= build_int_cst (unsigned_type_node
, uprec
- 1);
3215 t
= fold_build2 (MINUS_EXPR
, unsigned_type_node
, t
, rhs
);
3216 tree ulhs
= fold_convert (unsigned_type_for (lhstype
), lhs
);
3217 t
= fold_build2 (RSHIFT_EXPR
, TREE_TYPE (ulhs
), ulhs
, t
);
3218 if (tree_int_cst_lt (integer_one_node
, t
))
3221 permerror (loc
, "shift expression %q+E overflows",
3222 build2_loc (loc
, code
, type
, lhs
, rhs
));
3223 return (!flag_permissive
|| ctx
->quiet
);
3229 /* Subroutine of cxx_eval_constant_expression.
3230 Attempt to reduce the unary expression tree T to a compile time value.
3231 If successful, return the value. Otherwise issue a diagnostic
3232 and return error_mark_node. */
3235 cxx_eval_unary_expression (const constexpr_ctx
*ctx
, tree t
,
3237 bool *non_constant_p
, bool *overflow_p
)
3240 tree orig_arg
= TREE_OPERAND (t
, 0);
3241 tree arg
= cxx_eval_constant_expression (ctx
, orig_arg
, vc_prvalue
,
3242 non_constant_p
, overflow_p
);
3243 VERIFY_CONSTANT (arg
);
3244 location_t loc
= EXPR_LOCATION (t
);
3245 enum tree_code code
= TREE_CODE (t
);
3246 tree type
= TREE_TYPE (t
);
3247 r
= fold_unary_loc (loc
, code
, type
, arg
);
3250 if (arg
== orig_arg
)
3253 r
= build1_loc (loc
, code
, type
, arg
);
3255 VERIFY_CONSTANT (r
);
3259 /* Helper function for cxx_eval_binary_expression. Try to optimize
3260 original POINTER_PLUS_EXPR T, LHS p+ RHS, return NULL_TREE if the
3261 generic folding should be used. */
3264 cxx_fold_pointer_plus_expression (const constexpr_ctx
*ctx
, tree t
,
3265 tree lhs
, tree rhs
, bool *non_constant_p
,
3269 if (TREE_CODE (lhs
) != ADDR_EXPR
)
3272 lhs
= TREE_OPERAND (lhs
, 0);
3274 /* &A[i] p+ j => &A[i + j] */
3275 if (TREE_CODE (lhs
) == ARRAY_REF
3276 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == INTEGER_CST
3277 && TREE_CODE (rhs
) == INTEGER_CST
3278 && TYPE_SIZE_UNIT (TREE_TYPE (lhs
))
3279 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs
))) == INTEGER_CST
)
3281 tree orig_type
= TREE_TYPE (t
);
3282 location_t loc
= EXPR_LOCATION (t
);
3283 tree type
= TREE_TYPE (lhs
);
3285 t
= fold_convert_loc (loc
, ssizetype
, TREE_OPERAND (lhs
, 1));
3286 tree nelts
= array_type_nelts_top (TREE_TYPE (TREE_OPERAND (lhs
, 0)));
3287 nelts
= cxx_eval_constant_expression (ctx
, nelts
, vc_prvalue
,
3288 non_constant_p
, overflow_p
);
3289 if (*non_constant_p
)
3291 /* Don't fold an out-of-bound access. */
3292 if (!tree_int_cst_le (t
, nelts
))
3294 rhs
= cp_fold_convert (ssizetype
, rhs
);
3295 /* Don't fold if rhs can't be divided exactly by TYPE_SIZE_UNIT.
3296 constexpr int A[1]; ... (char *)&A[0] + 1 */
3297 if (!integer_zerop (fold_build2_loc (loc
, TRUNC_MOD_EXPR
, sizetype
,
3298 rhs
, TYPE_SIZE_UNIT (type
))))
3300 /* Make sure to treat the second operand of POINTER_PLUS_EXPR
3302 rhs
= fold_build2_loc (loc
, EXACT_DIV_EXPR
, ssizetype
, rhs
,
3303 TYPE_SIZE_UNIT (type
));
3304 t
= size_binop_loc (loc
, PLUS_EXPR
, rhs
, t
);
3305 t
= build4_loc (loc
, ARRAY_REF
, type
, TREE_OPERAND (lhs
, 0),
3306 t
, NULL_TREE
, NULL_TREE
);
3307 t
= cp_build_addr_expr (t
, tf_warning_or_error
);
3308 t
= cp_fold_convert (orig_type
, t
);
3309 return cxx_eval_constant_expression (ctx
, t
, vc_prvalue
,
3310 non_constant_p
, overflow_p
);
3316 /* Try to fold expressions like
3317 (struct S *) (&a[0].D.2378 + 12)
3319 &MEM <struct T> [(void *)&a + 12B]
3320 This is something normally done by gimple_fold_stmt_to_constant_1
3321 on GIMPLE, but is undesirable on GENERIC if we are e.g. going to
3322 dereference the address because some details are lost.
3323 For pointer comparisons we want such folding though so that
3324 match.pd address_compare optimization works. */
3327 cxx_maybe_fold_addr_pointer_plus (tree t
)
3329 while (CONVERT_EXPR_P (t
)
3330 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t
, 0))))
3331 t
= TREE_OPERAND (t
, 0);
3332 if (TREE_CODE (t
) != POINTER_PLUS_EXPR
)
3334 tree op0
= TREE_OPERAND (t
, 0);
3335 tree op1
= TREE_OPERAND (t
, 1);
3336 if (TREE_CODE (op1
) != INTEGER_CST
)
3338 while (CONVERT_EXPR_P (op0
)
3339 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0
, 0))))
3340 op0
= TREE_OPERAND (op0
, 0);
3341 if (TREE_CODE (op0
) != ADDR_EXPR
)
3343 op1
= fold_convert (ptr_type_node
, op1
);
3344 tree r
= fold_build2 (MEM_REF
, TREE_TYPE (TREE_TYPE (op0
)), op0
, op1
);
3345 return build1_loc (EXPR_LOCATION (t
), ADDR_EXPR
, TREE_TYPE (op0
), r
);
3348 /* Subroutine of cxx_eval_constant_expression.
3349 Like cxx_eval_unary_expression, except for binary expressions. */
3352 cxx_eval_binary_expression (const constexpr_ctx
*ctx
, tree t
,
3354 bool *non_constant_p
, bool *overflow_p
)
3357 tree orig_lhs
= TREE_OPERAND (t
, 0);
3358 tree orig_rhs
= TREE_OPERAND (t
, 1);
3360 lhs
= cxx_eval_constant_expression (ctx
, orig_lhs
, vc_prvalue
,
3361 non_constant_p
, overflow_p
);
3362 /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer
3364 if (*non_constant_p
)
3366 rhs
= cxx_eval_constant_expression (ctx
, orig_rhs
, vc_prvalue
,
3367 non_constant_p
, overflow_p
);
3368 if (*non_constant_p
)
3371 location_t loc
= EXPR_LOCATION (t
);
3372 enum tree_code code
= TREE_CODE (t
);
3373 tree type
= TREE_TYPE (t
);
3375 if (code
== EQ_EXPR
|| code
== NE_EXPR
)
3377 bool is_code_eq
= (code
== EQ_EXPR
);
3379 if (TREE_CODE (lhs
) == PTRMEM_CST
3380 && TREE_CODE (rhs
) == PTRMEM_CST
)
3382 tree lmem
= PTRMEM_CST_MEMBER (lhs
);
3383 tree rmem
= PTRMEM_CST_MEMBER (rhs
);
3385 if (TREE_CODE (lmem
) == TREE_CODE (rmem
)
3386 && TREE_CODE (lmem
) == FIELD_DECL
3387 && TREE_CODE (DECL_CONTEXT (lmem
)) == UNION_TYPE
3388 && same_type_p (DECL_CONTEXT (lmem
),
3389 DECL_CONTEXT (rmem
)))
3390 /* If both refer to (possibly different) members of the same union
3391 (12.3), they compare equal. */
3394 eq
= cp_tree_equal (lhs
, rhs
);
3395 r
= constant_boolean_node (eq
== is_code_eq
, type
);
3397 else if ((TREE_CODE (lhs
) == PTRMEM_CST
3398 || TREE_CODE (rhs
) == PTRMEM_CST
)
3399 && (null_member_pointer_value_p (lhs
)
3400 || null_member_pointer_value_p (rhs
)))
3401 r
= constant_boolean_node (!is_code_eq
, type
);
3402 else if (TREE_CODE (lhs
) == PTRMEM_CST
)
3403 lhs
= cplus_expand_constant (lhs
);
3404 else if (TREE_CODE (rhs
) == PTRMEM_CST
)
3405 rhs
= cplus_expand_constant (rhs
);
3408 && TREE_CODE_CLASS (code
) == tcc_comparison
3409 && POINTER_TYPE_P (TREE_TYPE (lhs
)))
3411 if (tree lhso
= cxx_maybe_fold_addr_pointer_plus (lhs
))
3412 lhs
= fold_convert (TREE_TYPE (lhs
), lhso
);
3413 if (tree rhso
= cxx_maybe_fold_addr_pointer_plus (rhs
))
3414 rhs
= fold_convert (TREE_TYPE (rhs
), rhso
);
3416 if (code
== POINTER_PLUS_EXPR
&& !*non_constant_p
3417 && integer_zerop (lhs
) && !integer_zerop (rhs
))
3420 error ("arithmetic involving a null pointer in %qE", lhs
);
3421 *non_constant_p
= true;
3424 else if (code
== POINTER_PLUS_EXPR
)
3425 r
= cxx_fold_pointer_plus_expression (ctx
, t
, lhs
, rhs
, non_constant_p
,
3427 else if (code
== SPACESHIP_EXPR
)
3429 r
= genericize_spaceship (loc
, type
, lhs
, rhs
);
3430 return cxx_eval_constant_expression (ctx
, r
, lval
, non_constant_p
,
3436 if (ctx
->manifestly_const_eval
3437 && (flag_constexpr_fp_except
3438 || TREE_CODE (type
) != REAL_TYPE
))
3440 auto ofcc
= make_temp_override (folding_cxx_constexpr
, true);
3441 r
= fold_binary_initializer_loc (loc
, code
, type
, lhs
, rhs
);
3444 r
= fold_binary_loc (loc
, code
, type
, lhs
, rhs
);
3448 && (code
== LSHIFT_EXPR
|| code
== RSHIFT_EXPR
)
3449 && TREE_CODE (lhs
) == INTEGER_CST
3450 && TREE_CODE (rhs
) == INTEGER_CST
3451 && wi::neg_p (wi::to_wide (rhs
)))
3453 /* For diagnostics and -fpermissive emulate previous behavior of
3454 handling shifts by negative amount. */
3455 tree nrhs
= const_unop (NEGATE_EXPR
, TREE_TYPE (rhs
), rhs
);
3457 r
= fold_binary_loc (loc
,
3458 code
== LSHIFT_EXPR
? RSHIFT_EXPR
: LSHIFT_EXPR
,
3464 if (lhs
== orig_lhs
&& rhs
== orig_rhs
)
3467 r
= build2_loc (loc
, code
, type
, lhs
, rhs
);
3469 else if (cxx_eval_check_shift_p (loc
, ctx
, code
, type
, lhs
, rhs
))
3470 *non_constant_p
= true;
3471 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
3472 a local array in a constexpr function. */
3473 bool ptr
= INDIRECT_TYPE_P (TREE_TYPE (lhs
));
3475 VERIFY_CONSTANT (r
);
3479 /* Subroutine of cxx_eval_constant_expression.
3480 Attempt to evaluate condition expressions. Dead branches are not
3484 cxx_eval_conditional_expression (const constexpr_ctx
*ctx
, tree t
,
3486 bool *non_constant_p
, bool *overflow_p
,
3489 tree val
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
3491 non_constant_p
, overflow_p
);
3492 VERIFY_CONSTANT (val
);
3493 if (TREE_CODE (t
) == IF_STMT
&& IF_STMT_CONSTEVAL_P (t
))
3495 /* Evaluate the condition as if it was
3496 if (__builtin_is_constant_evaluated ()), i.e. defer it if not
3497 ctx->manifestly_const_eval (as sometimes we try to constant evaluate
3498 without manifestly_const_eval even expressions or parts thereof which
3499 will later be manifestly const_eval evaluated), otherwise fold it to
3501 if (ctx
->manifestly_const_eval
)
3502 val
= boolean_true_node
;
3505 *non_constant_p
= true;
3509 /* Don't VERIFY_CONSTANT the other operands. */
3510 if (integer_zerop (val
))
3511 val
= TREE_OPERAND (t
, 2);
3513 val
= TREE_OPERAND (t
, 1);
3514 if (TREE_CODE (t
) == IF_STMT
&& !val
)
3516 /* A TARGET_EXPR may be nested inside another TARGET_EXPR, but still
3517 serve as the initializer for the same object as the outer TARGET_EXPR,
3519 A a = true ? A{} : A{};
3520 so strip the inner TARGET_EXPR so we don't materialize a temporary. */
3521 if (TREE_CODE (val
) == TARGET_EXPR
)
3522 val
= TARGET_EXPR_INITIAL (val
);
3523 return cxx_eval_constant_expression (ctx
, val
, lval
, non_constant_p
,
3524 overflow_p
, jump_target
);
3527 /* Subroutine of cxx_eval_constant_expression.
3528 Attempt to evaluate vector condition expressions. Unlike
3529 cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal
3530 ternary arithmetics operation, where all 3 arguments have to be
3531 evaluated as constants and then folding computes the result from
3535 cxx_eval_vector_conditional_expression (const constexpr_ctx
*ctx
, tree t
,
3536 bool *non_constant_p
, bool *overflow_p
)
3538 tree arg1
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
3540 non_constant_p
, overflow_p
);
3541 VERIFY_CONSTANT (arg1
);
3542 tree arg2
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
3544 non_constant_p
, overflow_p
);
3545 VERIFY_CONSTANT (arg2
);
3546 tree arg3
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 2),
3548 non_constant_p
, overflow_p
);
3549 VERIFY_CONSTANT (arg3
);
3550 location_t loc
= EXPR_LOCATION (t
);
3551 tree type
= TREE_TYPE (t
);
3552 tree r
= fold_ternary_loc (loc
, VEC_COND_EXPR
, type
, arg1
, arg2
, arg3
);
3555 if (arg1
== TREE_OPERAND (t
, 0)
3556 && arg2
== TREE_OPERAND (t
, 1)
3557 && arg3
== TREE_OPERAND (t
, 2))
3560 r
= build3_loc (loc
, VEC_COND_EXPR
, type
, arg1
, arg2
, arg3
);
3562 VERIFY_CONSTANT (r
);
3566 /* Returns less than, equal to, or greater than zero if KEY is found to be
3567 less than, to match, or to be greater than the constructor_elt's INDEX. */
3570 array_index_cmp (tree key
, tree index
)
3572 gcc_assert (TREE_CODE (key
) == INTEGER_CST
);
3574 switch (TREE_CODE (index
))
3577 return tree_int_cst_compare (key
, index
);
3580 tree lo
= TREE_OPERAND (index
, 0);
3581 tree hi
= TREE_OPERAND (index
, 1);
3582 if (tree_int_cst_lt (key
, lo
))
3584 else if (tree_int_cst_lt (hi
, key
))
3594 /* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1
3595 if none. If INSERT is true, insert a matching element rather than fail. */
3597 static HOST_WIDE_INT
3598 find_array_ctor_elt (tree ary
, tree dindex
, bool insert
)
3600 if (tree_int_cst_sgn (dindex
) < 0)
3603 unsigned HOST_WIDE_INT i
= tree_to_uhwi (dindex
);
3604 vec
<constructor_elt
, va_gc
> *elts
= CONSTRUCTOR_ELTS (ary
);
3605 unsigned HOST_WIDE_INT len
= vec_safe_length (elts
);
3607 unsigned HOST_WIDE_INT end
= len
;
3608 unsigned HOST_WIDE_INT begin
= 0;
3610 /* If the last element of the CONSTRUCTOR has its own index, we can assume
3611 that the same is true of the other elements and index directly. */
3614 tree cindex
= (*elts
)[end
- 1].index
;
3615 if (cindex
== NULL_TREE
)
3617 /* Verify that if the last index is missing, all indexes
3620 for (unsigned int j
= 0; j
< len
- 1; ++j
)
3621 gcc_assert ((*elts
)[j
].index
== NULL_TREE
);
3628 /* If the element is to be added right at the end,
3629 make sure it is added with cleared index too. */
3632 /* Otherwise, in order not to break the assumption
3633 that CONSTRUCTOR either has all indexes or none,
3634 we need to add indexes to all elements. */
3635 for (unsigned int j
= 0; j
< len
; ++j
)
3636 (*elts
)[j
].index
= build_int_cst (TREE_TYPE (dindex
), j
);
3639 else if (TREE_CODE (cindex
) == INTEGER_CST
3640 && compare_tree_int (cindex
, end
- 1) == 0)
3649 /* Otherwise, find a matching index by means of a binary search. */
3650 while (begin
!= end
)
3652 unsigned HOST_WIDE_INT middle
= (begin
+ end
) / 2;
3653 constructor_elt
&elt
= (*elts
)[middle
];
3654 tree idx
= elt
.index
;
3656 int cmp
= array_index_cmp (dindex
, idx
);
3663 if (insert
&& TREE_CODE (idx
) == RANGE_EXPR
)
3665 /* We need to split the range. */
3667 tree lo
= TREE_OPERAND (idx
, 0);
3668 tree hi
= TREE_OPERAND (idx
, 1);
3669 tree value
= elt
.value
;
3670 dindex
= fold_convert (sizetype
, dindex
);
3671 if (tree_int_cst_lt (lo
, dindex
))
3673 /* There are still some lower elts; shorten the range. */
3674 tree new_hi
= int_const_binop (MINUS_EXPR
, dindex
,
3676 if (tree_int_cst_equal (lo
, new_hi
))
3677 /* Only one element left, no longer a range. */
3680 TREE_OPERAND (idx
, 1) = new_hi
;
3681 /* Append the element we want to insert. */
3684 e
.value
= unshare_constructor (value
);
3685 vec_safe_insert (CONSTRUCTOR_ELTS (ary
), middle
, e
);
3688 /* No lower elts, the range elt is now ours. */
3691 if (tree_int_cst_lt (dindex
, hi
))
3693 /* There are still some higher elts; append a range. */
3694 tree new_lo
= int_const_binop (PLUS_EXPR
, dindex
,
3696 if (tree_int_cst_equal (new_lo
, hi
))
3699 e
.index
= build2 (RANGE_EXPR
, sizetype
, new_lo
, hi
);
3700 e
.value
= unshare_constructor (value
);
3701 vec_safe_insert (CONSTRUCTOR_ELTS (ary
), middle
+ 1, e
);
3710 constructor_elt e
= { dindex
, NULL_TREE
};
3711 vec_safe_insert (CONSTRUCTOR_ELTS (ary
), end
, e
);
3718 /* Return a pointer to the constructor_elt of CTOR which matches INDEX. If no
3719 matching constructor_elt exists, then add one to CTOR.
3721 As an optimization, if POS_HINT is non-negative then it is used as a guess
3722 for the (integer) index of the matching constructor_elt within CTOR. */
3724 static constructor_elt
*
3725 get_or_insert_ctor_field (tree ctor
, tree index
, int pos_hint
= -1)
3727 /* Check the hint first. */
3728 if (pos_hint
>= 0 && (unsigned)pos_hint
< CONSTRUCTOR_NELTS (ctor
)
3729 && CONSTRUCTOR_ELT (ctor
, pos_hint
)->index
== index
)
3730 return CONSTRUCTOR_ELT (ctor
, pos_hint
);
3732 tree type
= TREE_TYPE (ctor
);
3733 if (TREE_CODE (type
) == VECTOR_TYPE
&& index
== NULL_TREE
)
3735 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (ctor
), index
, NULL_TREE
);
3736 return &CONSTRUCTOR_ELTS (ctor
)->last();
3738 else if (TREE_CODE (type
) == ARRAY_TYPE
|| TREE_CODE (type
) == VECTOR_TYPE
)
3740 if (TREE_CODE (index
) == RANGE_EXPR
)
3742 /* Support for RANGE_EXPR index lookups is currently limited to
3743 accessing an existing element via POS_HINT, or appending a new
3744 element to the end of CTOR. ??? Support for other access
3745 patterns may also be needed. */
3746 vec
<constructor_elt
, va_gc
> *elts
= CONSTRUCTOR_ELTS (ctor
);
3747 if (vec_safe_length (elts
))
3749 tree lo
= TREE_OPERAND (index
, 0);
3750 gcc_assert (array_index_cmp (elts
->last().index
, lo
) < 0);
3752 CONSTRUCTOR_APPEND_ELT (elts
, index
, NULL_TREE
);
3753 return &elts
->last();
3756 HOST_WIDE_INT i
= find_array_ctor_elt (ctor
, index
, /*insert*/true);
3757 gcc_assert (i
>= 0);
3758 constructor_elt
*cep
= CONSTRUCTOR_ELT (ctor
, i
);
3759 gcc_assert (cep
->index
== NULL_TREE
3760 || TREE_CODE (cep
->index
) != RANGE_EXPR
);
3765 gcc_assert (TREE_CODE (index
) == FIELD_DECL
3766 && (same_type_ignoring_top_level_qualifiers_p
3767 (DECL_CONTEXT (index
), TREE_TYPE (ctor
))));
3769 /* We must keep the CONSTRUCTOR's ELTS in FIELD order.
3770 Usually we meet initializers in that order, but it is
3771 possible for base types to be placed not in program
3773 tree fields
= TYPE_FIELDS (DECL_CONTEXT (index
));
3774 unsigned HOST_WIDE_INT idx
= 0;
3775 constructor_elt
*cep
= NULL
;
3777 /* Check if we're changing the active member of a union. */
3778 if (TREE_CODE (type
) == UNION_TYPE
&& CONSTRUCTOR_NELTS (ctor
)
3779 && CONSTRUCTOR_ELT (ctor
, 0)->index
!= index
)
3780 vec_safe_truncate (CONSTRUCTOR_ELTS (ctor
), 0);
3781 /* If the bit offset of INDEX is larger than that of the last
3782 constructor_elt, then we can just immediately append a new
3783 constructor_elt to the end of CTOR. */
3784 else if (CONSTRUCTOR_NELTS (ctor
)
3785 && tree_int_cst_compare (bit_position (index
),
3786 bit_position (CONSTRUCTOR_ELTS (ctor
)
3787 ->last().index
)) > 0)
3789 idx
= CONSTRUCTOR_NELTS (ctor
);
3793 /* Otherwise, we need to iterate over CTOR to find or insert INDEX
3796 for (; vec_safe_iterate (CONSTRUCTOR_ELTS (ctor
), idx
, &cep
);
3797 idx
++, fields
= DECL_CHAIN (fields
))
3799 if (index
== cep
->index
)
3802 /* The field we're initializing must be on the field
3803 list. Look to see if it is present before the
3804 field the current ELT initializes. */
3805 for (; fields
!= cep
->index
; fields
= DECL_CHAIN (fields
))
3806 if (index
== fields
)
3809 /* We fell off the end of the CONSTRUCTOR, so insert a new
3810 entry at the end. */
3814 constructor_elt ce
= { index
, NULL_TREE
};
3816 vec_safe_insert (CONSTRUCTOR_ELTS (ctor
), idx
, ce
);
3817 cep
= CONSTRUCTOR_ELT (ctor
, idx
);
3825 /* Under the control of CTX, issue a detailed diagnostic for
3826 an out-of-bounds subscript INDEX into the expression ARRAY. */
3829 diag_array_subscript (location_t loc
, const constexpr_ctx
*ctx
, tree array
, tree index
)
3833 tree arraytype
= TREE_TYPE (array
);
3835 /* Convert the unsigned array subscript to a signed integer to avoid
3836 printing huge numbers for small negative values. */
3837 tree sidx
= fold_convert (ssizetype
, index
);
3838 STRIP_ANY_LOCATION_WRAPPER (array
);
3841 if (TYPE_DOMAIN (arraytype
))
3842 error_at (loc
, "array subscript value %qE is outside the bounds "
3843 "of array %qD of type %qT", sidx
, array
, arraytype
);
3845 error_at (loc
, "nonzero array subscript %qE is used with array %qD of "
3846 "type %qT with unknown bounds", sidx
, array
, arraytype
);
3847 inform (DECL_SOURCE_LOCATION (array
), "declared here");
3849 else if (TYPE_DOMAIN (arraytype
))
3850 error_at (loc
, "array subscript value %qE is outside the bounds "
3851 "of array type %qT", sidx
, arraytype
);
3853 error_at (loc
, "nonzero array subscript %qE is used with array of type %qT "
3854 "with unknown bounds", sidx
, arraytype
);
3858 /* Return the number of elements for TYPE (which is an ARRAY_TYPE or
3862 get_array_or_vector_nelts (const constexpr_ctx
*ctx
, tree type
,
3863 bool *non_constant_p
, bool *overflow_p
)
3866 if (TREE_CODE (type
) == ARRAY_TYPE
)
3868 if (TYPE_DOMAIN (type
))
3869 nelts
= array_type_nelts_top (type
);
3871 nelts
= size_zero_node
;
3873 else if (VECTOR_TYPE_P (type
))
3874 nelts
= size_int (TYPE_VECTOR_SUBPARTS (type
));
3878 /* For VLAs, the number of elements won't be an integer constant. */
3879 nelts
= cxx_eval_constant_expression (ctx
, nelts
, vc_prvalue
,
3880 non_constant_p
, overflow_p
);
3884 /* Extract element INDEX consisting of CHARS_PER_ELT chars from
3885 STRING_CST STRING. */
3888 extract_string_elt (tree string
, unsigned chars_per_elt
, unsigned index
)
3890 tree type
= cv_unqualified (TREE_TYPE (TREE_TYPE (string
)));
3893 if (chars_per_elt
== 1)
3894 r
= build_int_cst (type
, TREE_STRING_POINTER (string
)[index
]);
3897 const unsigned char *ptr
3898 = ((const unsigned char *)TREE_STRING_POINTER (string
)
3899 + index
* chars_per_elt
);
3900 r
= native_interpret_expr (type
, ptr
, chars_per_elt
);
3905 /* Subroutine of cxx_eval_array_reference. T is an ARRAY_REF; evaluate the
3906 subscript, diagnose any problems with it, and return the result. */
3909 eval_and_check_array_index (const constexpr_ctx
*ctx
,
3910 tree t
, bool allow_one_past
,
3911 bool *non_constant_p
, bool *overflow_p
)
3913 location_t loc
= cp_expr_loc_or_input_loc (t
);
3914 tree ary
= TREE_OPERAND (t
, 0);
3915 t
= TREE_OPERAND (t
, 1);
3916 tree index
= cxx_eval_constant_expression (ctx
, t
, vc_prvalue
,
3917 non_constant_p
, overflow_p
);
3918 VERIFY_CONSTANT (index
);
3920 if (!tree_fits_shwi_p (index
)
3921 || tree_int_cst_sgn (index
) < 0)
3923 diag_array_subscript (loc
, ctx
, ary
, index
);
3924 *non_constant_p
= true;
3928 tree nelts
= get_array_or_vector_nelts (ctx
, TREE_TYPE (ary
), non_constant_p
,
3930 VERIFY_CONSTANT (nelts
);
3932 ? !tree_int_cst_le (index
, nelts
)
3933 : !tree_int_cst_lt (index
, nelts
))
3935 diag_array_subscript (loc
, ctx
, ary
, index
);
3936 *non_constant_p
= true;
3943 /* Subroutine of cxx_eval_constant_expression.
3944 Attempt to reduce a reference to an array slot. */
3947 cxx_eval_array_reference (const constexpr_ctx
*ctx
, tree t
,
3949 bool *non_constant_p
, bool *overflow_p
)
3951 tree oldary
= TREE_OPERAND (t
, 0);
3952 tree ary
= cxx_eval_constant_expression (ctx
, oldary
,
3954 non_constant_p
, overflow_p
);
3955 if (*non_constant_p
)
3958 && TREE_CODE (ary
) == VIEW_CONVERT_EXPR
3959 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary
, 0)))
3960 && TREE_TYPE (t
) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary
, 0))))
3961 ary
= TREE_OPERAND (ary
, 0);
3963 tree oldidx
= TREE_OPERAND (t
, 1);
3964 tree index
= eval_and_check_array_index (ctx
, t
, lval
,
3965 non_constant_p
, overflow_p
);
3966 if (*non_constant_p
)
3969 if (lval
&& ary
== oldary
&& index
== oldidx
)
3971 else if (lval
== vc_discard
)
3974 return build4 (ARRAY_REF
, TREE_TYPE (t
), ary
, index
, NULL
, NULL
);
3976 unsigned len
= 0, elem_nchars
= 1;
3977 tree elem_type
= TREE_TYPE (TREE_TYPE (ary
));
3978 if (TREE_CODE (ary
) == CONSTRUCTOR
)
3979 len
= CONSTRUCTOR_NELTS (ary
);
3980 else if (TREE_CODE (ary
) == STRING_CST
)
3982 elem_nchars
= (TYPE_PRECISION (elem_type
)
3983 / TYPE_PRECISION (char_type_node
));
3984 len
= (unsigned) TREE_STRING_LENGTH (ary
) / elem_nchars
;
3986 else if (TREE_CODE (ary
) == VECTOR_CST
)
3987 /* We don't create variable-length VECTOR_CSTs. */
3988 len
= VECTOR_CST_NELTS (ary
).to_constant ();
3991 /* We can't do anything with other tree codes, so use
3992 VERIFY_CONSTANT to complain and fail. */
3993 VERIFY_CONSTANT (ary
);
3998 HOST_WIDE_INT i
= 0;
3999 if (TREE_CODE (ary
) == CONSTRUCTOR
)
4001 HOST_WIDE_INT ix
= find_array_ctor_elt (ary
, index
);
4008 i
= tree_to_shwi (index
);
4015 if (TREE_CODE (ary
) == CONSTRUCTOR
)
4016 r
= (*CONSTRUCTOR_ELTS (ary
))[i
].value
;
4017 else if (TREE_CODE (ary
) == VECTOR_CST
)
4018 r
= VECTOR_CST_ELT (ary
, i
);
4020 r
= extract_string_elt (ary
, elem_nchars
, i
);
4023 /* Don't VERIFY_CONSTANT here. */
4026 /* Otherwise the element doesn't have a value yet. */
4031 if (TREE_CODE (ary
) == CONSTRUCTOR
4032 && CONSTRUCTOR_NO_CLEARING (ary
))
4034 /* 'ary' is part of the aggregate initializer we're currently
4035 building; if there's no initializer for this element yet,
4038 error ("accessing uninitialized array element");
4039 *non_constant_p
= true;
4043 /* If it's within the array bounds but doesn't have an explicit
4044 initializer, it's initialized from {}. But use build_value_init
4045 directly for non-aggregates to avoid creating a garbage CONSTRUCTOR. */
4047 constexpr_ctx new_ctx
;
4048 if (is_really_empty_class (elem_type
, /*ignore_vptr*/false))
4049 return build_constructor (elem_type
, NULL
);
4050 else if (CP_AGGREGATE_TYPE_P (elem_type
))
4052 tree empty_ctor
= build_constructor (init_list_type_node
, NULL
);
4053 val
= digest_init (elem_type
, empty_ctor
, tf_warning_or_error
);
4056 val
= build_value_init (elem_type
, tf_warning_or_error
);
4058 if (!SCALAR_TYPE_P (elem_type
))
4062 /* If there was no object, don't add one: it could confuse us
4063 into thinking we're modifying a const object. */
4065 new_ctx
.ctor
= build_constructor (elem_type
, NULL
);
4068 t
= cxx_eval_constant_expression (ctx
, val
, lval
, non_constant_p
,
4070 if (!SCALAR_TYPE_P (elem_type
) && t
!= ctx
->ctor
)
4071 free_constructor (ctx
->ctor
);
4075 /* Subroutine of cxx_eval_constant_expression.
4076 Attempt to reduce a field access of a value of class type. */
4079 cxx_eval_component_reference (const constexpr_ctx
*ctx
, tree t
,
4081 bool *non_constant_p
, bool *overflow_p
)
4083 unsigned HOST_WIDE_INT i
;
4086 tree part
= TREE_OPERAND (t
, 1);
4087 tree orig_whole
= TREE_OPERAND (t
, 0);
4088 tree whole
= cxx_eval_constant_expression (ctx
, orig_whole
,
4090 non_constant_p
, overflow_p
);
4091 if (*non_constant_p
)
4093 if (INDIRECT_REF_P (whole
)
4094 && integer_zerop (TREE_OPERAND (whole
, 0)))
4097 error ("dereferencing a null pointer in %qE", orig_whole
);
4098 *non_constant_p
= true;
4102 if (TREE_CODE (whole
) == PTRMEM_CST
)
4103 whole
= cplus_expand_constant (whole
);
4104 if (whole
== orig_whole
)
4106 if (lval
== vc_discard
)
4109 return fold_build3 (COMPONENT_REF
, TREE_TYPE (t
),
4110 whole
, part
, NULL_TREE
);
4111 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
4113 if (TREE_CODE (whole
) != CONSTRUCTOR
)
4116 error ("%qE is not a constant expression", orig_whole
);
4117 *non_constant_p
= true;
4120 if ((cxx_dialect
< cxx14
|| CONSTRUCTOR_MUTABLE_POISON (whole
))
4121 && DECL_MUTABLE_P (part
))
4124 error ("mutable %qD is not usable in a constant expression", part
);
4125 *non_constant_p
= true;
4128 bool pmf
= TYPE_PTRMEMFUNC_P (TREE_TYPE (whole
));
4129 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole
), i
, field
, value
)
4131 /* Use name match for PMF fields, as a variant will have a
4132 different FIELD_DECL with a different type. */
4133 if (pmf
? DECL_NAME (field
) == DECL_NAME (part
)
4138 STRIP_ANY_LOCATION_WRAPPER (value
);
4142 /* We're in the middle of initializing it. */
4146 if (TREE_CODE (TREE_TYPE (whole
)) == UNION_TYPE
4147 && CONSTRUCTOR_NELTS (whole
) > 0)
4149 /* DR 1188 says we don't have to deal with this. */
4152 constructor_elt
*cep
= CONSTRUCTOR_ELT (whole
, 0);
4153 if (cep
->value
== NULL_TREE
)
4154 error ("accessing uninitialized member %qD", part
);
4156 error ("accessing %qD member instead of initialized %qD member in "
4157 "constant expression", part
, cep
->index
);
4159 *non_constant_p
= true;
4163 /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty
4164 classes never get represented; throw together a value now. */
4165 if (is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
4166 return build_constructor (TREE_TYPE (t
), NULL
);
4168 gcc_assert (DECL_CONTEXT (part
) == TYPE_MAIN_VARIANT (TREE_TYPE (whole
)));
4170 if (CONSTRUCTOR_NO_CLEARING (whole
))
4172 /* 'whole' is part of the aggregate initializer we're currently
4173 building; if there's no initializer for this member yet, that's an
4176 error ("accessing uninitialized member %qD", part
);
4177 *non_constant_p
= true;
4181 /* If there's no explicit init for this field, it's value-initialized. */
4182 value
= build_value_init (TREE_TYPE (t
), tf_warning_or_error
);
4183 return cxx_eval_constant_expression (ctx
, value
,
4185 non_constant_p
, overflow_p
);
4188 /* Subroutine of cxx_eval_constant_expression.
4189 Attempt to reduce a field access of a value of class type that is
4190 expressed as a BIT_FIELD_REF. */
4193 cxx_eval_bit_field_ref (const constexpr_ctx
*ctx
, tree t
,
4195 bool *non_constant_p
, bool *overflow_p
)
4197 tree orig_whole
= TREE_OPERAND (t
, 0);
4198 tree retval
, fldval
, utype
, mask
;
4199 bool fld_seen
= false;
4200 HOST_WIDE_INT istart
, isize
;
4201 tree whole
= cxx_eval_constant_expression (ctx
, orig_whole
,
4203 non_constant_p
, overflow_p
);
4204 tree start
, field
, value
;
4205 unsigned HOST_WIDE_INT i
;
4207 if (whole
== orig_whole
)
4209 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
4211 if (!*non_constant_p
4212 && TREE_CODE (whole
) != VECTOR_CST
4213 && TREE_CODE (whole
) != CONSTRUCTOR
)
4216 error ("%qE is not a constant expression", orig_whole
);
4217 *non_constant_p
= true;
4219 if (*non_constant_p
)
4222 if (TREE_CODE (whole
) == VECTOR_CST
|| !INTEGRAL_TYPE_P (TREE_TYPE (t
)))
4224 if (tree r
= fold_ternary (BIT_FIELD_REF
, TREE_TYPE (t
), whole
,
4225 TREE_OPERAND (t
, 1), TREE_OPERAND (t
, 2)))
4228 error ("%qE is not a constant expression", orig_whole
);
4229 *non_constant_p
= true;
4233 start
= TREE_OPERAND (t
, 2);
4234 istart
= tree_to_shwi (start
);
4235 isize
= tree_to_shwi (TREE_OPERAND (t
, 1));
4236 utype
= TREE_TYPE (t
);
4237 if (!TYPE_UNSIGNED (utype
))
4238 utype
= build_nonstandard_integer_type (TYPE_PRECISION (utype
), 1);
4239 retval
= build_int_cst (utype
, 0);
4240 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole
), i
, field
, value
)
4242 tree bitpos
= bit_position (field
);
4243 STRIP_ANY_LOCATION_WRAPPER (value
);
4244 if (bitpos
== start
&& DECL_SIZE (field
) == TREE_OPERAND (t
, 1))
4246 if (TREE_CODE (TREE_TYPE (field
)) == INTEGER_TYPE
4247 && TREE_CODE (value
) == INTEGER_CST
4248 && tree_fits_shwi_p (bitpos
)
4249 && tree_fits_shwi_p (DECL_SIZE (field
)))
4251 HOST_WIDE_INT bit
= tree_to_shwi (bitpos
);
4252 HOST_WIDE_INT sz
= tree_to_shwi (DECL_SIZE (field
));
4253 HOST_WIDE_INT shift
;
4254 if (bit
>= istart
&& bit
+ sz
<= istart
+ isize
)
4256 fldval
= fold_convert (utype
, value
);
4257 mask
= build_int_cst_type (utype
, -1);
4258 mask
= fold_build2 (LSHIFT_EXPR
, utype
, mask
,
4259 size_int (TYPE_PRECISION (utype
) - sz
));
4260 mask
= fold_build2 (RSHIFT_EXPR
, utype
, mask
,
4261 size_int (TYPE_PRECISION (utype
) - sz
));
4262 fldval
= fold_build2 (BIT_AND_EXPR
, utype
, fldval
, mask
);
4263 shift
= bit
- istart
;
4264 if (BYTES_BIG_ENDIAN
)
4265 shift
= TYPE_PRECISION (utype
) - shift
- sz
;
4266 fldval
= fold_build2 (LSHIFT_EXPR
, utype
, fldval
,
4268 retval
= fold_build2 (BIT_IOR_EXPR
, utype
, retval
, fldval
);
4274 return fold_convert (TREE_TYPE (t
), retval
);
4276 return error_mark_node
;
4279 /* Helper for cxx_eval_bit_cast.
4280 Check [bit.cast]/3 rules, bit_cast is constexpr only if the To and From
4281 types and types of all subobjects have is_union_v<T>, is_pointer_v<T>,
4282 is_member_pointer_v<T>, is_volatile_v<T> false and has no non-static
4283 data members of reference type. */
4286 check_bit_cast_type (const constexpr_ctx
*ctx
, location_t loc
, tree type
,
4289 if (TREE_CODE (type
) == UNION_TYPE
)
4293 if (type
== orig_type
)
4294 error_at (loc
, "%qs is not a constant expression because %qT is "
4295 "a union type", "__builtin_bit_cast", type
);
4297 error_at (loc
, "%qs is not a constant expression because %qT "
4298 "contains a union type", "__builtin_bit_cast",
4303 if (TREE_CODE (type
) == POINTER_TYPE
)
4307 if (type
== orig_type
)
4308 error_at (loc
, "%qs is not a constant expression because %qT is "
4309 "a pointer type", "__builtin_bit_cast", type
);
4311 error_at (loc
, "%qs is not a constant expression because %qT "
4312 "contains a pointer type", "__builtin_bit_cast",
4317 if (TREE_CODE (type
) == REFERENCE_TYPE
)
4321 if (type
== orig_type
)
4322 error_at (loc
, "%qs is not a constant expression because %qT is "
4323 "a reference type", "__builtin_bit_cast", type
);
4325 error_at (loc
, "%qs is not a constant expression because %qT "
4326 "contains a reference type", "__builtin_bit_cast",
4331 if (TYPE_PTRMEM_P (type
))
4335 if (type
== orig_type
)
4336 error_at (loc
, "%qs is not a constant expression because %qT is "
4337 "a pointer to member type", "__builtin_bit_cast",
4340 error_at (loc
, "%qs is not a constant expression because %qT "
4341 "contains a pointer to member type",
4342 "__builtin_bit_cast", orig_type
);
4346 if (TYPE_VOLATILE (type
))
4350 if (type
== orig_type
)
4351 error_at (loc
, "%qs is not a constant expression because %qT is "
4352 "volatile", "__builtin_bit_cast", type
);
4354 error_at (loc
, "%qs is not a constant expression because %qT "
4355 "contains a volatile subobject",
4356 "__builtin_bit_cast", orig_type
);
4360 if (TREE_CODE (type
) == RECORD_TYPE
)
4361 for (tree field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
4362 if (TREE_CODE (field
) == FIELD_DECL
4363 && check_bit_cast_type (ctx
, loc
, TREE_TYPE (field
), orig_type
))
4368 /* Helper function for cxx_eval_bit_cast. For unsigned char or
4369 std::byte members of CONSTRUCTOR (recursively) if they contain
4370 some indeterminate bits (as set in MASK), remove the ctor elts,
4371 mark the CONSTRUCTOR as CONSTRUCTOR_NO_CLEARING and clear the
4375 clear_uchar_or_std_byte_in_mask (location_t loc
, tree t
, unsigned char *mask
)
4377 if (TREE_CODE (t
) != CONSTRUCTOR
)
4382 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t
), i
, index
, value
)
4384 tree type
= TREE_TYPE (value
);
4385 if (TREE_CODE (TREE_TYPE (t
)) != ARRAY_TYPE
4386 && DECL_BIT_FIELD_TYPE (index
) != NULL_TREE
)
4388 if (is_byte_access_type_not_plain_char (DECL_BIT_FIELD_TYPE (index
)))
4390 HOST_WIDE_INT fldsz
= TYPE_PRECISION (TREE_TYPE (index
));
4391 gcc_assert (fldsz
!= 0);
4392 HOST_WIDE_INT pos
= int_byte_position (index
);
4394 = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (index
));
4395 bpos
%= BITS_PER_UNIT
;
4397 = ROUND_UP (bpos
+ fldsz
, BITS_PER_UNIT
) / BITS_PER_UNIT
;
4398 gcc_assert (end
== 1 || end
== 2);
4399 unsigned char *p
= mask
+ pos
;
4400 unsigned char mask_save
[2];
4401 mask_save
[0] = mask
[pos
];
4402 mask_save
[1] = end
== 2 ? mask
[pos
+ 1] : 0;
4403 if (BYTES_BIG_ENDIAN
!= WORDS_BIG_ENDIAN
)
4404 sorry_at (loc
, "PDP11 bit-field handling unsupported"
4405 " in %qs", "__builtin_bit_cast");
4406 else if (BYTES_BIG_ENDIAN
)
4409 if (bpos
+ fldsz
<= BITS_PER_UNIT
)
4410 *p
&= ~(((1 << fldsz
) - 1)
4411 << (BITS_PER_UNIT
- bpos
- fldsz
));
4415 *p
&= ~(((1U << BITS_PER_UNIT
) - 1) >> bpos
);
4417 fldsz
-= BITS_PER_UNIT
- bpos
;
4418 gcc_assert (fldsz
&& fldsz
< BITS_PER_UNIT
);
4419 *p
&= ((1U << BITS_PER_UNIT
) - 1) >> fldsz
;
4424 /* Little endian. */
4425 if (bpos
+ fldsz
<= BITS_PER_UNIT
)
4426 *p
&= ~(((1 << fldsz
) - 1) << bpos
);
4430 *p
&= ~(((1 << BITS_PER_UNIT
) - 1) << bpos
);
4432 fldsz
-= BITS_PER_UNIT
- bpos
;
4433 gcc_assert (fldsz
&& fldsz
< BITS_PER_UNIT
);
4434 *p
&= ~((1 << fldsz
) - 1);
4437 if (mask_save
[0] != mask
[pos
]
4438 || (end
== 2 && mask_save
[1] != mask
[pos
+ 1]))
4440 CONSTRUCTOR_NO_CLEARING (t
) = 1;
4445 else if (is_byte_access_type_not_plain_char (type
))
4448 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
4449 pos
= tree_to_shwi (index
);
4451 pos
= int_byte_position (index
);
4454 CONSTRUCTOR_NO_CLEARING (t
) = 1;
4459 if (TREE_CODE (value
) == CONSTRUCTOR
)
4462 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
4463 pos
= tree_to_shwi (index
)
4464 * tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t
))));
4466 pos
= int_byte_position (index
);
4467 clear_uchar_or_std_byte_in_mask (loc
, value
, mask
+ pos
);
4471 CONSTRUCTOR_ELT (t
, j
)->index
= index
;
4472 CONSTRUCTOR_ELT (t
, j
)->value
= value
;
4476 if (CONSTRUCTOR_NELTS (t
) != j
)
4477 vec_safe_truncate (CONSTRUCTOR_ELTS (t
), j
);
4480 /* Subroutine of cxx_eval_constant_expression.
4481 Attempt to evaluate a BIT_CAST_EXPR. */
4484 cxx_eval_bit_cast (const constexpr_ctx
*ctx
, tree t
, bool *non_constant_p
,
4487 if (check_bit_cast_type (ctx
, EXPR_LOCATION (t
), TREE_TYPE (t
),
4489 || check_bit_cast_type (ctx
, cp_expr_loc_or_loc (TREE_OPERAND (t
, 0),
4491 TREE_TYPE (TREE_OPERAND (t
, 0)),
4492 TREE_TYPE (TREE_OPERAND (t
, 0))))
4494 *non_constant_p
= true;
4498 tree op
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), vc_prvalue
,
4499 non_constant_p
, overflow_p
);
4500 if (*non_constant_p
)
4503 location_t loc
= EXPR_LOCATION (t
);
4504 if (BITS_PER_UNIT
!= 8 || CHAR_BIT
!= 8)
4507 sorry_at (loc
, "%qs cannot be constant evaluated on the target",
4508 "__builtin_bit_cast");
4509 *non_constant_p
= true;
4513 if (!tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (t
))))
4516 sorry_at (loc
, "%qs cannot be constant evaluated because the "
4517 "type is too large", "__builtin_bit_cast");
4518 *non_constant_p
= true;
4522 HOST_WIDE_INT len
= tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (t
)));
4523 if (len
< 0 || (int) len
!= len
)
4526 sorry_at (loc
, "%qs cannot be constant evaluated because the "
4527 "type is too large", "__builtin_bit_cast");
4528 *non_constant_p
= true;
4532 unsigned char buf
[64];
4533 unsigned char *ptr
, *mask
;
4534 size_t alen
= (size_t) len
* 2;
4535 if (alen
<= sizeof (buf
))
4538 ptr
= XNEWVEC (unsigned char, alen
);
4539 mask
= ptr
+ (size_t) len
;
4540 /* At the beginning consider everything indeterminate. */
4541 memset (mask
, ~0, (size_t) len
);
4543 if (native_encode_initializer (op
, ptr
, len
, 0, mask
) != len
)
4546 sorry_at (loc
, "%qs cannot be constant evaluated because the "
4547 "argument cannot be encoded", "__builtin_bit_cast");
4548 *non_constant_p
= true;
4555 if (can_native_interpret_type_p (TREE_TYPE (t
)))
4557 r
= native_interpret_expr (TREE_TYPE (t
), ptr
, len
);
4558 if (is_byte_access_type_not_plain_char (TREE_TYPE (t
)))
4560 gcc_assert (len
== 1);
4563 memset (mask
, 0, len
);
4564 r
= build_constructor (TREE_TYPE (r
), NULL
);
4565 CONSTRUCTOR_NO_CLEARING (r
) = 1;
4569 else if (TREE_CODE (TREE_TYPE (t
)) == RECORD_TYPE
)
4571 r
= native_interpret_aggregate (TREE_TYPE (t
), ptr
, 0, len
);
4574 clear_type_padding_in_mask (TREE_TYPE (t
), mask
);
4575 clear_uchar_or_std_byte_in_mask (loc
, r
, mask
);
4581 for (int i
= 0; i
< len
; i
++)
4585 error_at (loc
, "%qs accessing uninitialized byte at offset %d",
4586 "__builtin_bit_cast", i
);
4587 *non_constant_p
= true;
4597 sorry_at (loc
, "%qs cannot be constant evaluated because the "
4598 "argument cannot be interpreted", "__builtin_bit_cast");
4599 *non_constant_p
= true;
4605 /* Subroutine of cxx_eval_constant_expression.
4606 Evaluate a short-circuited logical expression T in the context
4607 of a given constexpr CALL. BAILOUT_VALUE is the value for
4608 early return. CONTINUE_VALUE is used here purely for
4609 sanity check purposes. */
4612 cxx_eval_logical_expression (const constexpr_ctx
*ctx
, tree t
,
4613 tree bailout_value
, tree continue_value
,
4614 bool *non_constant_p
, bool *overflow_p
)
4617 tree lhs
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
4618 vc_prvalue
, non_constant_p
,
4620 VERIFY_CONSTANT (lhs
);
4621 if (tree_int_cst_equal (lhs
, bailout_value
))
4623 gcc_assert (tree_int_cst_equal (lhs
, continue_value
));
4624 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
4625 vc_prvalue
, non_constant_p
,
4627 VERIFY_CONSTANT (r
);
4631 /* REF is a COMPONENT_REF designating a particular field. V is a vector of
4632 CONSTRUCTOR elements to initialize (part of) an object containing that
4633 field. Return a pointer to the constructor_elt corresponding to the
4634 initialization of the field. */
4636 static constructor_elt
*
4637 base_field_constructor_elt (vec
<constructor_elt
, va_gc
> *v
, tree ref
)
4639 tree aggr
= TREE_OPERAND (ref
, 0);
4640 tree field
= TREE_OPERAND (ref
, 1);
4642 constructor_elt
*ce
;
4644 gcc_assert (TREE_CODE (ref
) == COMPONENT_REF
);
4646 if (TREE_CODE (aggr
) == COMPONENT_REF
)
4648 constructor_elt
*base_ce
4649 = base_field_constructor_elt (v
, aggr
);
4650 v
= CONSTRUCTOR_ELTS (base_ce
->value
);
4653 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
4654 if (ce
->index
== field
)
4661 /* Some of the expressions fed to the constexpr mechanism are calls to
4662 constructors, which have type void. In that case, return the type being
4663 initialized by the constructor. */
4666 initialized_type (tree t
)
4670 tree type
= TREE_TYPE (t
);
4671 if (TREE_CODE (t
) == CALL_EXPR
)
4673 /* A constructor call has void type, so we need to look deeper. */
4674 tree fn
= get_function_named_in_call (t
);
4675 if (fn
&& TREE_CODE (fn
) == FUNCTION_DECL
4676 && DECL_CXX_CONSTRUCTOR_P (fn
))
4677 type
= DECL_CONTEXT (fn
);
4679 else if (TREE_CODE (t
) == COMPOUND_EXPR
)
4680 return initialized_type (TREE_OPERAND (t
, 1));
4681 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
)
4682 type
= TREE_TYPE (AGGR_INIT_EXPR_SLOT (t
));
4683 return cv_unqualified (type
);
4686 /* We're about to initialize element INDEX of an array or class from VALUE.
4687 Set up NEW_CTX appropriately by adjusting .object to refer to the
4688 subobject and creating a new CONSTRUCTOR if the element is itself
4689 a class or array. */
4692 init_subob_ctx (const constexpr_ctx
*ctx
, constexpr_ctx
&new_ctx
,
4693 tree index
, tree
&value
)
4697 if (index
&& TREE_CODE (index
) != INTEGER_CST
4698 && TREE_CODE (index
) != FIELD_DECL
4699 && TREE_CODE (index
) != RANGE_EXPR
)
4700 /* This won't have an element in the new CONSTRUCTOR. */
4703 tree type
= initialized_type (value
);
4704 if (!AGGREGATE_TYPE_P (type
) && !VECTOR_TYPE_P (type
))
4705 /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
4707 if (VECTOR_TYPE_P (type
)
4708 && VECTOR_TYPE_P (TREE_TYPE (ctx
->ctor
))
4709 && index
== NULL_TREE
)
4710 /* A vector inside of a vector CONSTRUCTOR, e.g. when a larger
4711 vector is constructed from smaller vectors, doesn't get its own
4712 CONSTRUCTOR either. */
4715 /* The sub-aggregate initializer might contain a placeholder;
4716 update object to refer to the subobject and ctor to refer to
4717 the (newly created) sub-initializer. */
4720 if (index
== NULL_TREE
|| TREE_CODE (index
) == RANGE_EXPR
)
4721 /* There's no well-defined subobject for this index. */
4722 new_ctx
.object
= NULL_TREE
;
4724 new_ctx
.object
= build_ctor_subob_ref (index
, type
, ctx
->object
);
4727 if (is_empty_class (type
))
4728 /* Leave ctor null for an empty subobject, they aren't represented in the
4729 result of evaluation. */
4730 new_ctx
.ctor
= NULL_TREE
;
4733 tree elt
= build_constructor (type
, NULL
);
4734 CONSTRUCTOR_NO_CLEARING (elt
) = true;
4738 if (TREE_CODE (value
) == TARGET_EXPR
)
4739 /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
4740 value
= TARGET_EXPR_INITIAL (value
);
4743 /* We're about to process an initializer for a class or array TYPE. Make
4744 sure that CTX is set up appropriately. */
4747 verify_ctor_sanity (const constexpr_ctx
*ctx
, tree type
)
4749 /* We don't bother building a ctor for an empty base subobject. */
4750 if (is_empty_class (type
))
4753 /* We're in the middle of an initializer that might involve placeholders;
4754 our caller should have created a CONSTRUCTOR for us to put the
4755 initializer into. We will either return that constructor or T. */
4756 gcc_assert (ctx
->ctor
);
4757 gcc_assert (same_type_ignoring_top_level_qualifiers_p
4758 (type
, TREE_TYPE (ctx
->ctor
)));
4759 /* We used to check that ctx->ctor was empty, but that isn't the case when
4760 the object is zero-initialized before calling the constructor. */
4763 tree otype
= TREE_TYPE (ctx
->object
);
4764 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type
, otype
)
4765 /* Handle flexible array members. */
4766 || (TREE_CODE (otype
) == ARRAY_TYPE
4767 && TYPE_DOMAIN (otype
) == NULL_TREE
4768 && TREE_CODE (type
) == ARRAY_TYPE
4769 && (same_type_ignoring_top_level_qualifiers_p
4770 (TREE_TYPE (type
), TREE_TYPE (otype
)))));
4772 gcc_assert (!ctx
->object
|| !DECL_P (ctx
->object
)
4773 || *(ctx
->global
->values
.get (ctx
->object
)) == ctx
->ctor
);
4776 /* Subroutine of cxx_eval_constant_expression.
4777 The expression tree T denotes a C-style array or a C-style
4778 aggregate. Reduce it to a constant expression. */
4781 cxx_eval_bare_aggregate (const constexpr_ctx
*ctx
, tree t
,
4783 bool *non_constant_p
, bool *overflow_p
)
4785 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (t
);
4786 bool changed
= false;
4787 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t
));
4788 tree type
= TREE_TYPE (t
);
4790 constexpr_ctx new_ctx
;
4791 if (TYPE_PTRMEMFUNC_P (type
) || VECTOR_TYPE_P (type
))
4793 /* We don't really need the ctx->ctor business for a PMF or
4794 vector, but it's simpler to use the same code. */
4796 new_ctx
.ctor
= build_constructor (type
, NULL
);
4797 new_ctx
.object
= NULL_TREE
;
4800 verify_ctor_sanity (ctx
, type
);
4801 vec
<constructor_elt
, va_gc
> **p
= nullptr;
4804 p
= &CONSTRUCTOR_ELTS (ctx
->ctor
);
4805 vec_alloc (*p
, vec_safe_length (v
));
4806 if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (t
))
4807 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx
->ctor
) = 1;
4812 bool constant_p
= true;
4813 bool side_effects_p
= false;
4814 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, index
, value
)
4816 tree orig_value
= value
;
4817 /* Like in cxx_eval_store_expression, omit entries for empty fields. */
4818 bool no_slot
= TREE_CODE (type
) == RECORD_TYPE
&& is_empty_field (index
);
4819 init_subob_ctx (ctx
, new_ctx
, index
, value
);
4821 if (new_ctx
.ctor
!= ctx
->ctor
&& !no_slot
)
4823 /* If we built a new CONSTRUCTOR, attach it now so that other
4824 initializers can refer to it. */
4825 constructor_elt
*cep
= get_or_insert_ctor_field (ctx
->ctor
, index
);
4826 cep
->value
= new_ctx
.ctor
;
4827 pos_hint
= cep
- (*p
)->begin();
4829 else if (TREE_CODE (type
) == UNION_TYPE
)
4830 /* Otherwise if we're constructing a non-aggregate union member, set
4831 the active union member now so that we can later detect and diagnose
4832 if its initializer attempts to activate another member. */
4833 get_or_insert_ctor_field (ctx
->ctor
, index
);
4834 tree elt
= cxx_eval_constant_expression (&new_ctx
, value
,
4836 non_constant_p
, overflow_p
);
4837 /* Don't VERIFY_CONSTANT here. */
4838 if (ctx
->quiet
&& *non_constant_p
)
4840 if (elt
!= orig_value
)
4843 if (!TREE_CONSTANT (elt
))
4845 if (TREE_SIDE_EFFECTS (elt
))
4846 side_effects_p
= true;
4847 if (index
&& TREE_CODE (index
) == COMPONENT_REF
)
4849 /* This is an initialization of a vfield inside a base
4850 subaggregate that we already initialized; push this
4851 initialization into the previous initialization. */
4852 constructor_elt
*inner
= base_field_constructor_elt (*p
, index
);
4857 /* This is an initializer for an empty field; now that we've
4858 checked that it's constant, we can ignore it. */
4861 && (TREE_CODE (index
) == NOP_EXPR
4862 || TREE_CODE (index
) == POINTER_PLUS_EXPR
))
4864 /* Old representation of empty bases. FIXME remove. */
4865 gcc_checking_assert (false);
4866 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index
))));
4871 if (TREE_CODE (type
) == UNION_TYPE
4872 && (*p
)->last().index
!= index
)
4873 /* The initializer erroneously changed the active union member that
4874 we're initializing. */
4875 gcc_assert (*non_constant_p
);
4878 /* The initializer might have mutated the underlying CONSTRUCTOR,
4879 so recompute the location of the target constructer_elt. */
4880 constructor_elt
*cep
4881 = get_or_insert_ctor_field (ctx
->ctor
, index
, pos_hint
);
4885 /* Adding or replacing an element might change the ctor's flags. */
4886 TREE_CONSTANT (ctx
->ctor
) = constant_p
;
4887 TREE_SIDE_EFFECTS (ctx
->ctor
) = side_effects_p
;
4890 if (*non_constant_p
|| !changed
)
4894 t
= build_constructor (type
, NULL
);
4895 /* We're done building this CONSTRUCTOR, so now we can interpret an
4896 element without an explicit initializer as value-initialized. */
4897 CONSTRUCTOR_NO_CLEARING (t
) = false;
4898 TREE_CONSTANT (t
) = constant_p
;
4899 TREE_SIDE_EFFECTS (t
) = side_effects_p
;
4900 if (VECTOR_TYPE_P (type
))
4905 /* Subroutine of cxx_eval_constant_expression.
4906 The expression tree T is a VEC_INIT_EXPR which denotes the desired
4907 initialization of a non-static data member of array type. Reduce it to a
4910 Note that apart from value-initialization (when VALUE_INIT is true),
4911 this is only intended to support value-initialization and the
4912 initializations done by defaulted constructors for classes with
4913 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
4914 will either be NULL_TREE for the default constructor, or a COMPONENT_REF
4915 for the copy/move constructor. */
4918 cxx_eval_vec_init_1 (const constexpr_ctx
*ctx
, tree atype
, tree init
,
4919 bool value_init
, value_cat lval
,
4920 bool *non_constant_p
, bool *overflow_p
)
4922 tree elttype
= TREE_TYPE (atype
);
4923 verify_ctor_sanity (ctx
, atype
);
4924 vec
<constructor_elt
, va_gc
> **p
= &CONSTRUCTOR_ELTS (ctx
->ctor
);
4925 bool pre_init
= false;
4926 unsigned HOST_WIDE_INT i
;
4927 tsubst_flags_t complain
= ctx
->quiet
? tf_none
: tf_warning_or_error
;
4929 if (init
&& TREE_CODE (init
) == CONSTRUCTOR
)
4930 return cxx_eval_bare_aggregate (ctx
, init
, lval
,
4931 non_constant_p
, overflow_p
);
4933 /* For the default constructor, build up a call to the default
4934 constructor of the element type. We only need to handle class types
4935 here, as for a constructor to be constexpr, all members must be
4936 initialized, which for a defaulted default constructor means they must
4937 be of a class type with a constexpr default constructor. */
4938 if (TREE_CODE (elttype
) == ARRAY_TYPE
)
4939 /* We only do this at the lowest level. */;
4940 else if (value_init
)
4942 init
= build_value_init (elttype
, complain
);
4947 releasing_vec argvec
;
4948 init
= build_special_member_call (NULL_TREE
, complete_ctor_identifier
,
4949 &argvec
, elttype
, LOOKUP_NORMAL
,
4951 init
= build_aggr_init_expr (elttype
, init
);
4955 bool zeroed_out
= false;
4956 if (!CONSTRUCTOR_NO_CLEARING (ctx
->ctor
))
4958 /* We're initializing an array object that had been zero-initialized
4959 earlier. Truncate ctx->ctor, and propagate its zeroed state by
4960 clearing CONSTRUCTOR_NO_CLEARING on each of the aggregate element
4961 initializers we append to it. */
4962 gcc_checking_assert (initializer_zerop (ctx
->ctor
));
4964 vec_safe_truncate (*p
, 0);
4967 tree nelts
= get_array_or_vector_nelts (ctx
, atype
, non_constant_p
,
4969 unsigned HOST_WIDE_INT max
= tree_to_uhwi (nelts
);
4970 for (i
= 0; i
< max
; ++i
)
4972 tree idx
= build_int_cst (size_type_node
, i
);
4975 constexpr_ctx new_ctx
;
4976 init_subob_ctx (ctx
, new_ctx
, idx
, pre_init
? init
: elttype
);
4977 if (new_ctx
.ctor
!= ctx
->ctor
)
4980 CONSTRUCTOR_NO_CLEARING (new_ctx
.ctor
) = false;
4981 CONSTRUCTOR_APPEND_ELT (*p
, idx
, new_ctx
.ctor
);
4983 if (TREE_CODE (elttype
) == ARRAY_TYPE
)
4985 /* A multidimensional array; recurse. */
4986 if (value_init
|| init
== NULL_TREE
)
4988 eltinit
= NULL_TREE
;
4992 eltinit
= cp_build_array_ref (input_location
, init
, idx
, complain
);
4993 eltinit
= cxx_eval_vec_init_1 (&new_ctx
, elttype
, eltinit
, value_init
,
4995 non_constant_p
, overflow_p
);
4999 /* Initializing an element using value or default initialization
5000 we just pre-built above. */
5001 if (init
== void_node
)
5002 /* Trivial default-init, don't do anything to the CONSTRUCTOR. */
5004 eltinit
= cxx_eval_constant_expression (&new_ctx
, init
, lval
,
5005 non_constant_p
, overflow_p
);
5010 /* Copying an element. */
5011 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5012 (atype
, TREE_TYPE (init
)));
5013 eltinit
= cp_build_array_ref (input_location
, init
, idx
, complain
);
5014 if (!lvalue_p (init
))
5015 eltinit
= move (eltinit
);
5016 eltinit
= force_rvalue (eltinit
, complain
);
5017 eltinit
= cxx_eval_constant_expression (&new_ctx
, eltinit
, lval
,
5018 non_constant_p
, overflow_p
);
5020 if (*non_constant_p
)
5022 if (new_ctx
.ctor
!= ctx
->ctor
)
5024 /* We appended this element above; update the value. */
5025 gcc_assert ((*p
)->last().index
== idx
);
5026 (*p
)->last().value
= eltinit
;
5029 CONSTRUCTOR_APPEND_ELT (*p
, idx
, eltinit
);
5030 /* Reuse the result of cxx_eval_constant_expression call
5031 from the first iteration to all others if it is a constant
5032 initializer that doesn't require relocations. */
5035 && (eltinit
== NULL_TREE
5036 || (initializer_constant_valid_p (eltinit
, TREE_TYPE (eltinit
))
5037 == null_pointer_node
)))
5039 if (new_ctx
.ctor
!= ctx
->ctor
)
5040 eltinit
= new_ctx
.ctor
;
5041 tree range
= build2 (RANGE_EXPR
, size_type_node
,
5042 build_int_cst (size_type_node
, 1),
5043 build_int_cst (size_type_node
, max
- 1));
5044 CONSTRUCTOR_APPEND_ELT (*p
, range
, unshare_constructor (eltinit
));
5048 vec_safe_reserve (*p
, max
);
5051 if (!*non_constant_p
)
5054 CONSTRUCTOR_NO_CLEARING (init
) = false;
5060 cxx_eval_vec_init (const constexpr_ctx
*ctx
, tree t
,
5062 bool *non_constant_p
, bool *overflow_p
)
5064 tree atype
= TREE_TYPE (t
);
5065 tree init
= VEC_INIT_EXPR_INIT (t
);
5066 bool value_init
= VEC_INIT_EXPR_VALUE_INIT (t
);
5067 if (!init
|| !BRACE_ENCLOSED_INITIALIZER_P (init
))
5069 else if (CONSTRUCTOR_NELTS (init
) == 0
5070 && !CP_AGGREGATE_TYPE_P (strip_array_types (atype
)))
5072 /* Handle {} as value-init. */
5078 /* This is a more complicated case, like needing to loop over trailing
5079 elements; call build_vec_init and evaluate the result. */
5080 tsubst_flags_t complain
= ctx
->quiet
? tf_none
: tf_warning_or_error
;
5081 constexpr_ctx new_ctx
= *ctx
;
5084 /* We want to have an initialization target for an VEC_INIT_EXPR.
5085 If we don't already have one in CTX, use the VEC_INIT_EXPR_SLOT. */
5086 new_ctx
.object
= VEC_INIT_EXPR_SLOT (t
);
5087 tree ctor
= new_ctx
.ctor
= build_constructor (atype
, NULL
);
5088 CONSTRUCTOR_NO_CLEARING (ctor
) = true;
5089 ctx
->global
->values
.put (new_ctx
.object
, ctor
);
5092 init
= expand_vec_init_expr (ctx
->object
, t
, complain
);
5093 return cxx_eval_constant_expression (ctx
, init
, lval
, non_constant_p
,
5096 tree r
= cxx_eval_vec_init_1 (ctx
, atype
, init
, value_init
,
5097 lval
, non_constant_p
, overflow_p
);
5098 if (*non_constant_p
)
5104 /* Like same_type_ignoring_top_level_qualifiers_p, but also handle the case
5105 where the desired type is an array of unknown bounds because the variable
5106 has had its bounds deduced since the wrapping expression was created. */
5109 same_type_ignoring_tlq_and_bounds_p (tree type1
, tree type2
)
5111 while (TREE_CODE (type1
) == ARRAY_TYPE
5112 && TREE_CODE (type2
) == ARRAY_TYPE
5113 && (!TYPE_DOMAIN (type1
) || !TYPE_DOMAIN (type2
)))
5115 type1
= TREE_TYPE (type1
);
5116 type2
= TREE_TYPE (type2
);
5118 return same_type_ignoring_top_level_qualifiers_p (type1
, type2
);
5121 /* Try to determine the currently active union member for an expression
5122 with UNION_TYPE. If it can be determined, return the FIELD_DECL,
5123 otherwise return NULL_TREE. */
5126 cxx_union_active_member (const constexpr_ctx
*ctx
, tree t
)
5128 constexpr_ctx new_ctx
= *ctx
;
5129 new_ctx
.quiet
= true;
5130 bool non_constant_p
= false, overflow_p
= false;
5131 tree ctor
= cxx_eval_constant_expression (&new_ctx
, t
, vc_prvalue
,
5134 if (TREE_CODE (ctor
) == CONSTRUCTOR
5135 && CONSTRUCTOR_NELTS (ctor
) == 1
5136 && CONSTRUCTOR_ELT (ctor
, 0)->index
5137 && TREE_CODE (CONSTRUCTOR_ELT (ctor
, 0)->index
) == FIELD_DECL
)
5138 return CONSTRUCTOR_ELT (ctor
, 0)->index
;
5142 /* Helper function for cxx_fold_indirect_ref_1, called recursively. */
5145 cxx_fold_indirect_ref_1 (const constexpr_ctx
*ctx
, location_t loc
, tree type
,
5146 tree op
, unsigned HOST_WIDE_INT off
, bool *empty_base
)
5148 tree optype
= TREE_TYPE (op
);
5149 unsigned HOST_WIDE_INT const_nunits
;
5150 if (off
== 0 && similar_type_p (optype
, type
))
5152 else if (TREE_CODE (optype
) == COMPLEX_TYPE
5153 && similar_type_p (type
, TREE_TYPE (optype
)))
5155 /* *(foo *)&complexfoo => __real__ complexfoo */
5157 return build1_loc (loc
, REALPART_EXPR
, type
, op
);
5158 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
5159 else if (tree_to_uhwi (TYPE_SIZE_UNIT (type
)) == off
)
5160 return build1_loc (loc
, IMAGPART_EXPR
, type
, op
);
5162 /* ((foo*)&vectorfoo)[x] => BIT_FIELD_REF<vectorfoo,...> */
5163 else if (VECTOR_TYPE_P (optype
)
5164 && similar_type_p (type
, TREE_TYPE (optype
))
5165 && TYPE_VECTOR_SUBPARTS (optype
).is_constant (&const_nunits
))
5167 unsigned HOST_WIDE_INT part_width
= tree_to_uhwi (TYPE_SIZE_UNIT (type
));
5168 unsigned HOST_WIDE_INT max_offset
= part_width
* const_nunits
;
5169 if (off
< max_offset
&& off
% part_width
== 0)
5171 tree index
= bitsize_int (off
* BITS_PER_UNIT
);
5172 return build3_loc (loc
, BIT_FIELD_REF
, type
, op
,
5173 TYPE_SIZE (type
), index
);
5176 /* ((foo *)&fooarray)[x] => fooarray[x] */
5177 else if (TREE_CODE (optype
) == ARRAY_TYPE
5178 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (optype
)))
5179 && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (optype
))))
5181 tree type_domain
= TYPE_DOMAIN (optype
);
5182 tree min_val
= size_zero_node
;
5183 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
5184 min_val
= TYPE_MIN_VALUE (type_domain
);
5185 unsigned HOST_WIDE_INT el_sz
5186 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (optype
)));
5187 unsigned HOST_WIDE_INT idx
= off
/ el_sz
;
5188 unsigned HOST_WIDE_INT rem
= off
% el_sz
;
5189 if (tree_fits_uhwi_p (min_val
))
5191 tree index
= size_int (idx
+ tree_to_uhwi (min_val
));
5192 op
= build4_loc (loc
, ARRAY_REF
, TREE_TYPE (optype
), op
, index
,
5193 NULL_TREE
, NULL_TREE
);
5194 return cxx_fold_indirect_ref_1 (ctx
, loc
, type
, op
, rem
,
5198 /* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */
5199 else if (TREE_CODE (optype
) == RECORD_TYPE
5200 || TREE_CODE (optype
) == UNION_TYPE
)
5202 if (TREE_CODE (optype
) == UNION_TYPE
)
5203 /* For unions prefer the currently active member. */
5204 if (tree field
= cxx_union_active_member (ctx
, op
))
5206 unsigned HOST_WIDE_INT el_sz
5207 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field
)));
5210 tree cop
= build3 (COMPONENT_REF
, TREE_TYPE (field
),
5211 op
, field
, NULL_TREE
);
5212 if (tree ret
= cxx_fold_indirect_ref_1 (ctx
, loc
, type
, cop
,
5217 for (tree field
= TYPE_FIELDS (optype
);
5218 field
; field
= DECL_CHAIN (field
))
5219 if (TREE_CODE (field
) == FIELD_DECL
5220 && TREE_TYPE (field
) != error_mark_node
5221 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (field
))))
5223 tree pos
= byte_position (field
);
5224 if (!tree_fits_uhwi_p (pos
))
5226 unsigned HOST_WIDE_INT upos
= tree_to_uhwi (pos
);
5227 unsigned HOST_WIDE_INT el_sz
5228 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field
)));
5229 if (upos
<= off
&& off
< upos
+ el_sz
)
5231 tree cop
= build3 (COMPONENT_REF
, TREE_TYPE (field
),
5232 op
, field
, NULL_TREE
);
5233 if (tree ret
= cxx_fold_indirect_ref_1 (ctx
, loc
, type
, cop
,
5239 /* Also handle conversion to an empty base class, which
5240 is represented with a NOP_EXPR. */
5241 if (is_empty_class (type
)
5242 && CLASS_TYPE_P (optype
)
5243 && DERIVED_FROM_P (type
, optype
))
5254 /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
5255 match. We want to be less strict for simple *& folding; if we have a
5256 non-const temporary that we access through a const pointer, that should
5257 work. We handle this here rather than change fold_indirect_ref_1
5258 because we're dealing with things like ADDR_EXPR of INTEGER_CST which
5259 don't really make sense outside of constant expression evaluation. Also
5260 we want to allow folding to COMPONENT_REF, which could cause trouble
5261 with TBAA in fold_indirect_ref_1. */
5264 cxx_fold_indirect_ref (const constexpr_ctx
*ctx
, location_t loc
, tree type
,
5265 tree op0
, bool *empty_base
/* = NULL*/)
5269 poly_uint64 const_op01
;
5271 /* STRIP_NOPS, but stop if REINTERPRET_CAST_P. */
5272 while (CONVERT_EXPR_P (sub
) || TREE_CODE (sub
) == NON_LVALUE_EXPR
5273 || TREE_CODE (sub
) == VIEW_CONVERT_EXPR
)
5275 if (TREE_CODE (sub
) == NOP_EXPR
5276 && REINTERPRET_CAST_P (sub
))
5278 sub
= TREE_OPERAND (sub
, 0);
5281 subtype
= TREE_TYPE (sub
);
5282 if (!INDIRECT_TYPE_P (subtype
))
5285 /* Canonicalizes the given OBJ/OFF pair by iteratively absorbing
5286 the innermost component into the offset until it would make the
5287 offset positive, so that cxx_fold_indirect_ref_1 can identify
5288 more folding opportunities. */
5289 auto canonicalize_obj_off
= [] (tree
& obj
, tree
& off
) {
5290 while (TREE_CODE (obj
) == COMPONENT_REF
5291 && (tree_int_cst_sign_bit (off
) || integer_zerop (off
)))
5293 tree field
= TREE_OPERAND (obj
, 1);
5294 tree pos
= byte_position (field
);
5295 if (integer_zerop (off
) && integer_nonzerop (pos
))
5296 /* If the offset is already 0, keep going as long as the
5297 component is at position 0. */
5299 off
= int_const_binop (PLUS_EXPR
, off
, pos
);
5300 obj
= TREE_OPERAND (obj
, 0);
5304 if (TREE_CODE (sub
) == ADDR_EXPR
)
5306 tree op
= TREE_OPERAND (sub
, 0);
5307 tree optype
= TREE_TYPE (op
);
5309 /* *&CONST_DECL -> to the value of the const decl. */
5310 if (TREE_CODE (op
) == CONST_DECL
)
5311 return DECL_INITIAL (op
);
5312 /* *&p => p; make sure to handle *&"str"[cst] here. */
5313 if (similar_type_p (optype
, type
))
5315 tree fop
= fold_read_from_constant_string (op
);
5323 tree off
= integer_zero_node
;
5324 canonicalize_obj_off (op
, off
);
5325 gcc_assert (integer_zerop (off
));
5326 return cxx_fold_indirect_ref_1 (ctx
, loc
, type
, op
, 0, empty_base
);
5329 else if (TREE_CODE (sub
) == POINTER_PLUS_EXPR
5330 && tree_fits_uhwi_p (TREE_OPERAND (sub
, 1)))
5332 tree op00
= TREE_OPERAND (sub
, 0);
5333 tree off
= TREE_OPERAND (sub
, 1);
5336 if (TREE_CODE (op00
) == ADDR_EXPR
)
5338 tree obj
= TREE_OPERAND (op00
, 0);
5339 canonicalize_obj_off (obj
, off
);
5340 return cxx_fold_indirect_ref_1 (ctx
, loc
, type
, obj
,
5341 tree_to_uhwi (off
), empty_base
);
5344 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
5345 else if (TREE_CODE (TREE_TYPE (subtype
)) == ARRAY_TYPE
5346 && similar_type_p (type
, TREE_TYPE (TREE_TYPE (subtype
))))
5349 tree min_val
= size_zero_node
;
5351 = cxx_fold_indirect_ref (ctx
, loc
, TREE_TYPE (subtype
), sub
, NULL
);
5355 sub
= build1_loc (loc
, INDIRECT_REF
, TREE_TYPE (subtype
), sub
);
5356 type_domain
= TYPE_DOMAIN (TREE_TYPE (sub
));
5357 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
5358 min_val
= TYPE_MIN_VALUE (type_domain
);
5359 return build4_loc (loc
, ARRAY_REF
, type
, sub
, min_val
, NULL_TREE
,
5367 cxx_eval_indirect_ref (const constexpr_ctx
*ctx
, tree t
,
5369 bool *non_constant_p
, bool *overflow_p
)
5371 tree orig_op0
= TREE_OPERAND (t
, 0);
5372 bool empty_base
= false;
5374 /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second
5375 operand is an integer-zero. Otherwise reject the MEM_REF for now. */
5377 if (TREE_CODE (t
) == MEM_REF
5378 && (!TREE_OPERAND (t
, 1) || !integer_zerop (TREE_OPERAND (t
, 1))))
5380 gcc_assert (ctx
->quiet
);
5381 *non_constant_p
= true;
5385 /* First try to simplify it directly. */
5386 tree r
= cxx_fold_indirect_ref (ctx
, EXPR_LOCATION (t
), TREE_TYPE (t
),
5387 orig_op0
, &empty_base
);
5390 /* If that didn't work, evaluate the operand first. */
5391 tree op0
= cxx_eval_constant_expression (ctx
, orig_op0
,
5392 vc_prvalue
, non_constant_p
,
5394 /* Don't VERIFY_CONSTANT here. */
5395 if (*non_constant_p
)
5398 if (!lval
&& integer_zerop (op0
))
5401 error ("dereferencing a null pointer");
5402 *non_constant_p
= true;
5406 r
= cxx_fold_indirect_ref (ctx
, EXPR_LOCATION (t
), TREE_TYPE (t
), op0
,
5410 /* We couldn't fold to a constant value. Make sure it's not
5411 something we should have been able to fold. */
5414 if (TREE_CODE (sub
) == ADDR_EXPR
)
5416 gcc_assert (!similar_type_p
5417 (TREE_TYPE (TREE_TYPE (sub
)), TREE_TYPE (t
)));
5418 /* DR 1188 says we don't have to deal with this. */
5420 error_at (cp_expr_loc_or_input_loc (t
),
5421 "accessing value of %qE through a %qT glvalue in a "
5422 "constant expression", build_fold_indirect_ref (sub
),
5424 *non_constant_p
= true;
5428 if (lval
== vc_glvalue
&& op0
!= orig_op0
)
5429 return build1 (INDIRECT_REF
, TREE_TYPE (t
), op0
);
5431 VERIFY_CONSTANT (t
);
5436 r
= cxx_eval_constant_expression (ctx
, r
,
5437 lval
, non_constant_p
, overflow_p
);
5438 if (*non_constant_p
)
5441 /* If we're pulling out the value of an empty base, just return an empty
5443 if (empty_base
&& !lval
)
5445 r
= build_constructor (TREE_TYPE (t
), NULL
);
5446 TREE_CONSTANT (r
) = true;
5452 /* Complain about R, a VAR_DECL, not being usable in a constant expression.
5453 Shared between potential_constant_expression and
5454 cxx_eval_constant_expression. */
5457 non_const_var_error (location_t loc
, tree r
)
5459 auto_diagnostic_group d
;
5460 tree type
= TREE_TYPE (r
);
5461 if (DECL_NAME (r
) == heap_uninit_identifier
5462 || DECL_NAME (r
) == heap_identifier
5463 || DECL_NAME (r
) == heap_vec_uninit_identifier
5464 || DECL_NAME (r
) == heap_vec_identifier
)
5466 error_at (loc
, "the content of uninitialized storage is not usable "
5467 "in a constant expression");
5468 inform (DECL_SOURCE_LOCATION (r
), "allocated here");
5471 if (DECL_NAME (r
) == heap_deleted_identifier
)
5473 error_at (loc
, "use of allocated storage after deallocation in a "
5474 "constant expression");
5475 inform (DECL_SOURCE_LOCATION (r
), "allocated here");
5478 error_at (loc
, "the value of %qD is not usable in a constant "
5480 /* Avoid error cascade. */
5481 if (DECL_INITIAL (r
) == error_mark_node
)
5483 if (DECL_DECLARED_CONSTEXPR_P (r
))
5484 inform (DECL_SOURCE_LOCATION (r
),
5485 "%qD used in its own initializer", r
);
5486 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5488 if (!CP_TYPE_CONST_P (type
))
5489 inform (DECL_SOURCE_LOCATION (r
),
5490 "%q#D is not const", r
);
5491 else if (CP_TYPE_VOLATILE_P (type
))
5492 inform (DECL_SOURCE_LOCATION (r
),
5493 "%q#D is volatile", r
);
5494 else if (!DECL_INITIAL (r
)
5495 || !TREE_CONSTANT (DECL_INITIAL (r
))
5496 || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
))
5497 inform (DECL_SOURCE_LOCATION (r
),
5498 "%qD was not initialized with a constant "
5503 else if (TYPE_REF_P (type
))
5504 inform (DECL_SOURCE_LOCATION (r
),
5505 "%qD was not initialized with a constant "
5509 if (cxx_dialect
>= cxx11
&& !DECL_DECLARED_CONSTEXPR_P (r
))
5510 inform (DECL_SOURCE_LOCATION (r
),
5511 "%qD was not declared %<constexpr%>", r
);
5513 inform (DECL_SOURCE_LOCATION (r
),
5514 "%qD does not have integral or enumeration type",
5519 /* Subroutine of cxx_eval_constant_expression.
5520 Like cxx_eval_unary_expression, except for trinary expressions. */
5523 cxx_eval_trinary_expression (const constexpr_ctx
*ctx
, tree t
,
5525 bool *non_constant_p
, bool *overflow_p
)
5531 for (i
= 0; i
< 3; i
++)
5533 args
[i
] = cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, i
),
5535 non_constant_p
, overflow_p
);
5536 VERIFY_CONSTANT (args
[i
]);
5539 val
= fold_ternary_loc (EXPR_LOCATION (t
), TREE_CODE (t
), TREE_TYPE (t
),
5540 args
[0], args
[1], args
[2]);
5541 if (val
== NULL_TREE
)
5543 VERIFY_CONSTANT (val
);
5547 /* True if T was declared in a function declared to be constexpr, and
5548 therefore potentially constant in C++14. */
5551 var_in_constexpr_fn (tree t
)
5553 tree ctx
= DECL_CONTEXT (t
);
5554 return (ctx
&& TREE_CODE (ctx
) == FUNCTION_DECL
5555 && DECL_DECLARED_CONSTEXPR_P (ctx
));
5558 /* True if a function might be constexpr: either a function that was
5559 declared constexpr, or a C++17 lambda op(). */
5562 maybe_constexpr_fn (tree t
)
5564 return (DECL_DECLARED_CONSTEXPR_P (t
)
5565 || (cxx_dialect
>= cxx17
&& LAMBDA_FUNCTION_P (t
))
5566 || (flag_implicit_constexpr
5567 && DECL_DECLARED_INLINE_P (STRIP_TEMPLATE (t
))));
5570 /* True if T was declared in a function that might be constexpr: either a
5571 function that was declared constexpr, or a C++17 lambda op(). */
5574 var_in_maybe_constexpr_fn (tree t
)
5576 return (DECL_FUNCTION_SCOPE_P (t
)
5577 && maybe_constexpr_fn (DECL_CONTEXT (t
)));
5580 /* We're assigning INIT to TARGET. In do_build_copy_constructor and
5581 build_over_call we implement trivial copy of a class with tail padding using
5582 assignment of character arrays, which is valid in normal code, but not in
5583 constexpr evaluation. We don't need to worry about clobbering tail padding
5584 in constexpr evaluation, so strip the type punning. */
5587 maybe_simplify_trivial_copy (tree
&target
, tree
&init
)
5589 if (TREE_CODE (target
) == MEM_REF
5590 && TREE_CODE (init
) == MEM_REF
5591 && TREE_TYPE (target
) == TREE_TYPE (init
)
5592 && TREE_CODE (TREE_TYPE (target
)) == ARRAY_TYPE
5593 && TREE_TYPE (TREE_TYPE (target
)) == unsigned_char_type_node
)
5595 target
= build_fold_indirect_ref (TREE_OPERAND (target
, 0));
5596 init
= build_fold_indirect_ref (TREE_OPERAND (init
, 0));
5600 /* Returns true if REF, which is a COMPONENT_REF, has any fields
5601 of constant type. This does not check for 'mutable', so the
5602 caller is expected to be mindful of that. */
5605 cref_has_const_field (tree ref
)
5607 while (TREE_CODE (ref
) == COMPONENT_REF
)
5609 if (CP_TYPE_CONST_P (TREE_TYPE (TREE_OPERAND (ref
, 1))))
5611 ref
= TREE_OPERAND (ref
, 0);
5616 /* Return true if we are modifying something that is const during constant
5617 expression evaluation. CODE is the code of the statement, OBJ is the
5618 object in question, MUTABLE_P is true if one of the subobjects were
5619 declared mutable. */
5622 modifying_const_object_p (tree_code code
, tree obj
, bool mutable_p
)
5624 /* If this is initialization, there's no problem. */
5625 if (code
!= MODIFY_EXPR
)
5628 /* [basic.type.qualifier] "A const object is an object of type
5629 const T or a non-mutable subobject of a const object." */
5633 if (TREE_READONLY (obj
))
5636 if (CP_TYPE_CONST_P (TREE_TYPE (obj
)))
5638 /* Although a COMPONENT_REF may have a const type, we should
5639 only consider it modifying a const object when any of the
5640 field components is const. This can happen when using
5641 constructs such as const_cast<const T &>(m), making something
5642 const even though it wasn't declared const. */
5643 if (TREE_CODE (obj
) == COMPONENT_REF
)
5644 return cref_has_const_field (obj
);
5652 /* Evaluate an INIT_EXPR or MODIFY_EXPR. */
5655 cxx_eval_store_expression (const constexpr_ctx
*ctx
, tree t
,
5657 bool *non_constant_p
, bool *overflow_p
)
5659 constexpr_ctx new_ctx
= *ctx
;
5661 tree init
= TREE_OPERAND (t
, 1);
5662 if (TREE_CLOBBER_P (init
))
5663 /* Just ignore clobbers. */
5666 /* First we figure out where we're storing to. */
5667 tree target
= TREE_OPERAND (t
, 0);
5669 maybe_simplify_trivial_copy (target
, init
);
5671 tree type
= TREE_TYPE (target
);
5672 bool preeval
= SCALAR_TYPE_P (type
) || TREE_CODE (t
) == MODIFY_EXPR
;
5675 /* Evaluate the value to be stored without knowing what object it will be
5676 stored in, so that any side-effects happen first. */
5677 if (!SCALAR_TYPE_P (type
))
5678 new_ctx
.ctor
= new_ctx
.object
= NULL_TREE
;
5679 init
= cxx_eval_constant_expression (&new_ctx
, init
, vc_prvalue
,
5680 non_constant_p
, overflow_p
);
5681 if (*non_constant_p
)
5685 bool evaluated
= false;
5686 if (lval
== vc_glvalue
)
5688 /* If we want to return a reference to the target, we need to evaluate it
5689 as a whole; otherwise, only evaluate the innermost piece to avoid
5690 building up unnecessary *_REFs. */
5691 target
= cxx_eval_constant_expression (ctx
, target
, lval
,
5692 non_constant_p
, overflow_p
);
5694 if (*non_constant_p
)
5698 /* Find the underlying variable. */
5700 tree object
= NULL_TREE
;
5701 /* If we're modifying a const object, save it. */
5702 tree const_object_being_modified
= NULL_TREE
;
5703 bool mutable_p
= false;
5704 for (tree probe
= target
; object
== NULL_TREE
; )
5706 switch (TREE_CODE (probe
))
5712 tree ob
= TREE_OPERAND (probe
, 0);
5713 tree elt
= TREE_OPERAND (probe
, 1);
5714 if (TREE_CODE (elt
) == FIELD_DECL
&& DECL_MUTABLE_P (elt
))
5716 if (TREE_CODE (probe
) == ARRAY_REF
)
5718 elt
= eval_and_check_array_index (ctx
, probe
, false,
5719 non_constant_p
, overflow_p
);
5720 if (*non_constant_p
)
5723 /* We don't check modifying_const_object_p for ARRAY_REFs. Given
5724 "int a[10]", an ARRAY_REF "a[2]" can be "const int", even though
5725 the array isn't const. Instead, check "a" in the next iteration;
5726 that will detect modifying "const int a[10]". */
5728 && modifying_const_object_p (TREE_CODE (t
), probe
,
5730 && const_object_being_modified
== NULL_TREE
)
5731 const_object_being_modified
= probe
;
5732 vec_safe_push (refs
, elt
);
5733 vec_safe_push (refs
, TREE_TYPE (probe
));
5739 gcc_assert (probe
== target
);
5740 vec_safe_push (refs
, probe
);
5741 vec_safe_push (refs
, TREE_TYPE (probe
));
5742 probe
= TREE_OPERAND (probe
, 0);
5746 gcc_assert (probe
== target
);
5747 vec_safe_push (refs
, probe
);
5748 vec_safe_push (refs
, TREE_TYPE (probe
));
5749 probe
= TREE_OPERAND (probe
, 0);
5757 probe
= cxx_eval_constant_expression (ctx
, probe
, vc_glvalue
,
5758 non_constant_p
, overflow_p
);
5760 if (*non_constant_p
)
5767 if (modifying_const_object_p (TREE_CODE (t
), object
, mutable_p
)
5768 && const_object_being_modified
== NULL_TREE
)
5769 const_object_being_modified
= object
;
5771 /* And then find/build up our initializer for the path to the subobject
5772 we're initializing. */
5774 if (DECL_P (object
))
5775 valp
= ctx
->global
->values
.get (object
);
5780 /* A constant-expression cannot modify objects from outside the
5781 constant-expression. */
5783 error ("modification of %qE is not a constant expression", object
);
5784 *non_constant_p
= true;
5787 type
= TREE_TYPE (object
);
5788 bool no_zero_init
= true;
5790 auto_vec
<tree
*> ctors
;
5791 releasing_vec indexes
;
5792 auto_vec
<int> index_pos_hints
;
5793 bool activated_union_member_p
= false;
5794 bool empty_base
= false;
5795 while (!refs
->is_empty ())
5797 if (*valp
== NULL_TREE
)
5799 *valp
= build_constructor (type
, NULL
);
5800 CONSTRUCTOR_NO_CLEARING (*valp
) = no_zero_init
;
5802 else if (TREE_CODE (*valp
) == STRING_CST
)
5804 /* An array was initialized with a string constant, and now
5805 we're writing into one of its elements. Explode the
5806 single initialization into a set of element
5808 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
5810 tree string
= *valp
;
5811 tree elt_type
= TREE_TYPE (type
);
5812 unsigned chars_per_elt
= (TYPE_PRECISION (elt_type
)
5813 / TYPE_PRECISION (char_type_node
));
5814 unsigned num_elts
= TREE_STRING_LENGTH (string
) / chars_per_elt
;
5815 tree ary_ctor
= build_constructor (type
, NULL
);
5817 vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor
), num_elts
);
5818 for (unsigned ix
= 0; ix
!= num_elts
; ix
++)
5820 constructor_elt elt
=
5822 build_int_cst (size_type_node
, ix
),
5823 extract_string_elt (string
, chars_per_elt
, ix
)
5825 CONSTRUCTOR_ELTS (ary_ctor
)->quick_push (elt
);
5831 enum tree_code code
= TREE_CODE (type
);
5832 tree reftype
= refs
->pop();
5833 tree index
= refs
->pop();
5835 if (code
== COMPLEX_TYPE
)
5837 if (TREE_CODE (*valp
) == COMPLEX_CST
)
5838 *valp
= build2 (COMPLEX_EXPR
, type
, TREE_REALPART (*valp
),
5839 TREE_IMAGPART (*valp
));
5840 else if (TREE_CODE (*valp
) == CONSTRUCTOR
5841 && CONSTRUCTOR_NELTS (*valp
) == 0
5842 && CONSTRUCTOR_NO_CLEARING (*valp
))
5844 tree r
= build_constructor (reftype
, NULL
);
5845 CONSTRUCTOR_NO_CLEARING (r
) = 1;
5846 *valp
= build2 (COMPLEX_EXPR
, type
, r
, r
);
5848 gcc_assert (TREE_CODE (*valp
) == COMPLEX_EXPR
);
5849 ctors
.safe_push (valp
);
5850 vec_safe_push (indexes
, index
);
5851 valp
= &TREE_OPERAND (*valp
, TREE_CODE (index
) == IMAGPART_EXPR
);
5852 gcc_checking_assert (refs
->is_empty ());
5857 /* If the value of object is already zero-initialized, any new ctors for
5858 subobjects will also be zero-initialized. */
5859 no_zero_init
= CONSTRUCTOR_NO_CLEARING (*valp
);
5861 if (code
== RECORD_TYPE
&& is_empty_field (index
))
5862 /* Don't build a sub-CONSTRUCTOR for an empty base or field, as they
5863 have no data and might have an offset lower than previously declared
5864 fields, which confuses the middle-end. The code below will notice
5865 that we don't have a CONSTRUCTOR for our inner target and just
5874 if (code
== UNION_TYPE
&& CONSTRUCTOR_NELTS (*valp
)
5875 && CONSTRUCTOR_ELT (*valp
, 0)->index
!= index
)
5877 if (cxx_dialect
< cxx20
)
5880 error_at (cp_expr_loc_or_input_loc (t
),
5881 "change of the active member of a union "
5883 CONSTRUCTOR_ELT (*valp
, 0)->index
,
5885 *non_constant_p
= true;
5887 else if (TREE_CODE (t
) == MODIFY_EXPR
5888 && CONSTRUCTOR_NO_CLEARING (*valp
))
5890 /* Diagnose changing the active union member while the union
5891 is in the process of being initialized. */
5893 error_at (cp_expr_loc_or_input_loc (t
),
5894 "change of the active member of a union "
5895 "from %qD to %qD during initialization",
5896 CONSTRUCTOR_ELT (*valp
, 0)->index
,
5898 *non_constant_p
= true;
5900 no_zero_init
= true;
5903 ctors
.safe_push (valp
);
5904 vec_safe_push (indexes
, index
);
5906 constructor_elt
*cep
5907 = get_or_insert_ctor_field (*valp
, index
);
5908 index_pos_hints
.safe_push (cep
- CONSTRUCTOR_ELTS (*valp
)->begin());
5910 if (code
== UNION_TYPE
)
5911 activated_union_member_p
= true;
5916 /* For initialization of an empty base, the original target will be
5917 *(base*)this, evaluation of which resolves to the object
5918 argument, which has the derived type rather than the base type. */
5919 if (!empty_base
&& !(same_type_ignoring_top_level_qualifiers_p
5920 (initialized_type (init
), type
)))
5922 gcc_assert (is_empty_class (TREE_TYPE (target
)));
5926 /* Detect modifying a constant object in constexpr evaluation.
5927 We have found a const object that is being modified. Figure out
5928 if we need to issue an error. Consider
5932 constexpr A() : n(1) { n = 2; } // #1
5936 constexpr B() { a.n = 3; } // #2
5940 #1 is OK, since we're modifying an object under construction, but
5941 #2 is wrong, since "a" is const and has been fully constructed.
5942 To track it, we use the TREE_READONLY bit in the object's CONSTRUCTOR
5943 which means that the object is read-only. For the example above, the
5944 *ctors stack at the point of #2 will look like:
5946 ctors[0] = {.a={.n=2}} TREE_READONLY = 0
5947 ctors[1] = {.n=2} TREE_READONLY = 1
5949 and we're modifying "b.a", so we search the stack and see if the
5950 constructor for "b.a" has already run. */
5951 if (const_object_being_modified
)
5955 = strip_array_types (TREE_TYPE (const_object_being_modified
));
5956 if (!CLASS_TYPE_P (const_objtype
))
5960 /* [class.ctor]p5 "A constructor can be invoked for a const,
5961 volatile, or const volatile object. const and volatile
5962 semantics are not applied on an object under construction.
5963 They come into effect when the constructor for the most
5964 derived object ends." */
5965 for (tree
*elt
: ctors
)
5966 if (same_type_ignoring_top_level_qualifiers_p
5967 (TREE_TYPE (const_object_being_modified
), TREE_TYPE (*elt
)))
5969 fail
= TREE_READONLY (*elt
);
5976 modifying_const_object_error (t
, const_object_being_modified
);
5977 *non_constant_p
= true;
5984 /* We're handling an INIT_EXPR of class type, so the value of the
5985 initializer can depend on the object it's initializing. */
5987 /* Create a new CONSTRUCTOR in case evaluation of the initializer
5988 wants to modify it. */
5989 if (*valp
== NULL_TREE
)
5991 *valp
= build_constructor (type
, NULL
);
5992 CONSTRUCTOR_NO_CLEARING (*valp
) = no_zero_init
;
5994 new_ctx
.ctor
= empty_base
? NULL_TREE
: *valp
;
5995 new_ctx
.object
= target
;
5996 /* Avoid temporary materialization when initializing from a TARGET_EXPR.
5997 We don't need to mess with AGGR_EXPR_SLOT/VEC_INIT_EXPR_SLOT because
5998 expansion of those trees uses ctx instead. */
5999 if (TREE_CODE (init
) == TARGET_EXPR
)
6000 if (tree tinit
= TARGET_EXPR_INITIAL (init
))
6002 init
= cxx_eval_constant_expression (&new_ctx
, init
, vc_prvalue
,
6003 non_constant_p
, overflow_p
);
6004 /* The hash table might have moved since the get earlier, and the
6005 initializer might have mutated the underlying CONSTRUCTORs, so we must
6007 valp
= ctx
->global
->values
.get (object
);
6008 for (unsigned i
= 0; i
< vec_safe_length (indexes
); i
++)
6011 constructor_elt
*cep
6012 = get_or_insert_ctor_field (*valp
, indexes
[i
], index_pos_hints
[i
]);
6017 if (*non_constant_p
)
6020 /* Don't share a CONSTRUCTOR that might be changed later. */
6021 init
= unshare_constructor (init
);
6023 gcc_checking_assert (!*valp
|| (same_type_ignoring_top_level_qualifiers_p
6024 (TREE_TYPE (*valp
), type
)));
6027 /* Just evaluate the initializer and return, since there's no actual data
6028 to store, and we didn't build a CONSTRUCTOR. */
6031 /* But do make sure we have something in *valp. */
6032 *valp
= build_constructor (type
, nullptr);
6033 CONSTRUCTOR_NO_CLEARING (*valp
) = no_zero_init
;
6036 else if (*valp
&& TREE_CODE (*valp
) == CONSTRUCTOR
6037 && TREE_CODE (init
) == CONSTRUCTOR
)
6039 /* An outer ctx->ctor might be pointing to *valp, so replace
6041 CONSTRUCTOR_ELTS (*valp
) = CONSTRUCTOR_ELTS (init
);
6042 TREE_CONSTANT (*valp
) = TREE_CONSTANT (init
);
6043 TREE_SIDE_EFFECTS (*valp
) = TREE_SIDE_EFFECTS (init
);
6044 CONSTRUCTOR_NO_CLEARING (*valp
)
6045 = CONSTRUCTOR_NO_CLEARING (init
);
6050 /* After initialization, 'const' semantics apply to the value of the
6051 object. Make a note of this fact by marking the CONSTRUCTOR
6053 if (TREE_CODE (t
) == INIT_EXPR
6054 && TREE_CODE (*valp
) == CONSTRUCTOR
6055 && TYPE_READONLY (type
))
6057 if (INDIRECT_REF_P (target
)
6058 && (is_this_parameter
6059 (tree_strip_nop_conversions (TREE_OPERAND (target
, 0)))))
6060 /* We've just initialized '*this' (perhaps via the target
6061 constructor of a delegating constructor). Leave it up to the
6062 caller that set 'this' to set TREE_READONLY appropriately. */
6063 gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
6064 (TREE_TYPE (target
), type
) || empty_base
);
6066 TREE_READONLY (*valp
) = true;
6069 /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing
6070 CONSTRUCTORs, if any. */
6071 bool c
= TREE_CONSTANT (init
);
6072 bool s
= TREE_SIDE_EFFECTS (init
);
6073 if (!indexes
->is_empty ())
6075 tree last
= indexes
->last ();
6076 if (TREE_CODE (last
) == REALPART_EXPR
6077 || TREE_CODE (last
) == IMAGPART_EXPR
)
6079 /* And canonicalize COMPLEX_EXPR into COMPLEX_CST if
6081 tree
*cexpr
= ctors
.last ();
6082 if (tree c
= const_binop (COMPLEX_EXPR
, TREE_TYPE (*cexpr
),
6083 TREE_OPERAND (*cexpr
, 0),
6084 TREE_OPERAND (*cexpr
, 1)))
6088 TREE_CONSTANT (*cexpr
)
6089 = (TREE_CONSTANT (TREE_OPERAND (*cexpr
, 0))
6090 & TREE_CONSTANT (TREE_OPERAND (*cexpr
, 1)));
6091 TREE_SIDE_EFFECTS (*cexpr
)
6092 = (TREE_SIDE_EFFECTS (TREE_OPERAND (*cexpr
, 0))
6093 | TREE_SIDE_EFFECTS (TREE_OPERAND (*cexpr
, 1)));
6095 c
= TREE_CONSTANT (*cexpr
);
6096 s
= TREE_SIDE_EFFECTS (*cexpr
);
6099 if (!c
|| s
|| activated_union_member_p
)
6100 for (tree
*elt
: ctors
)
6102 if (TREE_CODE (*elt
) != CONSTRUCTOR
)
6105 TREE_CONSTANT (*elt
) = false;
6107 TREE_SIDE_EFFECTS (*elt
) = true;
6108 /* Clear CONSTRUCTOR_NO_CLEARING since we've activated a member of
6110 if (TREE_CODE (TREE_TYPE (*elt
)) == UNION_TYPE
)
6111 CONSTRUCTOR_NO_CLEARING (*elt
) = false;
6120 /* Evaluate a ++ or -- expression. */
6123 cxx_eval_increment_expression (const constexpr_ctx
*ctx
, tree t
,
6125 bool *non_constant_p
, bool *overflow_p
)
6127 enum tree_code code
= TREE_CODE (t
);
6128 tree type
= TREE_TYPE (t
);
6129 tree op
= TREE_OPERAND (t
, 0);
6130 tree offset
= TREE_OPERAND (t
, 1);
6131 gcc_assert (TREE_CONSTANT (offset
));
6133 /* OFFSET is constant, but perhaps not constant enough. We need to
6134 e.g. bash FLOAT_EXPRs to REAL_CSTs. */
6135 offset
= fold_simple (offset
);
6137 /* The operand as an lvalue. */
6138 op
= cxx_eval_constant_expression (ctx
, op
, vc_glvalue
,
6139 non_constant_p
, overflow_p
);
6141 /* The operand as an rvalue. */
6143 = cxx_eval_constant_expression (ctx
, op
, vc_prvalue
,
6144 non_constant_p
, overflow_p
);
6145 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
6146 a local array in a constexpr function. */
6147 bool ptr
= INDIRECT_TYPE_P (TREE_TYPE (val
));
6149 VERIFY_CONSTANT (val
);
6151 /* The modified value. */
6152 bool inc
= (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
);
6154 if (INDIRECT_TYPE_P (type
))
6156 /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
6157 offset
= convert_to_ptrofftype (offset
);
6159 offset
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (offset
), offset
);
6160 mod
= fold_build2 (POINTER_PLUS_EXPR
, type
, val
, offset
);
6163 mod
= fold_build2 (inc
? PLUS_EXPR
: MINUS_EXPR
, type
, val
, offset
);
6165 VERIFY_CONSTANT (mod
);
6167 /* Storing the modified value. */
6168 tree store
= build2_loc (cp_expr_loc_or_loc (t
, input_location
),
6169 MODIFY_EXPR
, type
, op
, mod
);
6170 mod
= cxx_eval_constant_expression (ctx
, store
, lval
,
6171 non_constant_p
, overflow_p
);
6173 if (*non_constant_p
)
6176 /* And the value of the expression. */
6177 if (code
== PREINCREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
6178 /* Prefix ops are lvalues, but the caller might want an rvalue;
6179 lval has already been taken into account in the store above. */
6182 /* Postfix ops are rvalues. */
6186 /* Predicates for the meaning of *jump_target. */
6189 returns (tree
*jump_target
)
6192 && TREE_CODE (*jump_target
) == RETURN_EXPR
;
6196 breaks (tree
*jump_target
)
6199 && ((TREE_CODE (*jump_target
) == LABEL_DECL
6200 && LABEL_DECL_BREAK (*jump_target
))
6201 || TREE_CODE (*jump_target
) == BREAK_STMT
6202 || TREE_CODE (*jump_target
) == EXIT_EXPR
);
6206 continues (tree
*jump_target
)
6209 && ((TREE_CODE (*jump_target
) == LABEL_DECL
6210 && LABEL_DECL_CONTINUE (*jump_target
))
6211 || TREE_CODE (*jump_target
) == CONTINUE_STMT
);
6216 switches (tree
*jump_target
)
6219 && TREE_CODE (*jump_target
) == INTEGER_CST
;
6222 /* Subroutine of cxx_eval_statement_list. Determine whether the statement
6223 STMT matches *jump_target. If we're looking for a case label and we see
6224 the default label, note it in ctx->css_state. */
6227 label_matches (const constexpr_ctx
*ctx
, tree
*jump_target
, tree stmt
)
6229 switch (TREE_CODE (*jump_target
))
6232 if (TREE_CODE (stmt
) == LABEL_EXPR
6233 && LABEL_EXPR_LABEL (stmt
) == *jump_target
)
6238 if (TREE_CODE (stmt
) == CASE_LABEL_EXPR
)
6240 gcc_assert (ctx
->css_state
!= NULL
);
6241 if (!CASE_LOW (stmt
))
6243 /* default: should appear just once in a SWITCH_EXPR
6244 body (excluding nested SWITCH_EXPR). */
6245 gcc_assert (*ctx
->css_state
!= css_default_seen
);
6246 /* When evaluating SWITCH_EXPR body for the second time,
6247 return true for the default: label. */
6248 if (*ctx
->css_state
== css_default_processing
)
6250 *ctx
->css_state
= css_default_seen
;
6252 else if (CASE_HIGH (stmt
))
6254 if (tree_int_cst_le (CASE_LOW (stmt
), *jump_target
)
6255 && tree_int_cst_le (*jump_target
, CASE_HIGH (stmt
)))
6258 else if (tree_int_cst_equal (*jump_target
, CASE_LOW (stmt
)))
6265 /* These two are handled directly in cxx_eval_loop_expr by testing
6266 breaks (jump_target) or continues (jump_target). */
6275 /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
6276 semantics, for switch, break, continue, and return. */
6279 cxx_eval_statement_list (const constexpr_ctx
*ctx
, tree t
,
6280 bool *non_constant_p
, bool *overflow_p
,
6284 /* In a statement-expression we want to return the last value.
6285 For empty statement expression return void_node. */
6289 local_target
= NULL_TREE
;
6290 jump_target
= &local_target
;
6292 for (tree_stmt_iterator i
= tsi_start (t
); !tsi_end_p (i
); ++i
)
6296 /* We've found a continue, so skip everything until we reach
6297 the label its jumping to. */
6298 if (continues (jump_target
))
6300 if (label_matches (ctx
, jump_target
, stmt
))
6302 *jump_target
= NULL_TREE
;
6306 if (TREE_CODE (stmt
) == DEBUG_BEGIN_STMT
)
6309 value_cat lval
= vc_discard
;
6310 /* The result of a statement-expression is not wrapped in EXPR_STMT. */
6311 if (tsi_one_before_end_p (i
) && TREE_CODE (stmt
) != EXPR_STMT
)
6314 r
= cxx_eval_constant_expression (ctx
, stmt
, lval
,
6315 non_constant_p
, overflow_p
,
6317 if (*non_constant_p
)
6319 if (returns (jump_target
) || breaks (jump_target
))
6322 if (*jump_target
&& jump_target
== &local_target
)
6324 /* We aren't communicating the jump to our caller, so give up. We don't
6325 need to support evaluation of jumps out of statement-exprs. */
6327 error_at (cp_expr_loc_or_input_loc (r
),
6328 "statement is not a constant expression");
6329 *non_constant_p
= true;
6334 /* Evaluate a LOOP_EXPR for side-effects. Handles break and return
6335 semantics; continue semantics are covered by cxx_eval_statement_list. */
6338 cxx_eval_loop_expr (const constexpr_ctx
*ctx
, tree t
,
6339 bool *non_constant_p
, bool *overflow_p
,
6342 constexpr_ctx new_ctx
= *ctx
;
6346 local_target
= NULL_TREE
;
6347 jump_target
= &local_target
;
6350 tree body
, cond
= NULL_TREE
, expr
= NULL_TREE
;
6352 switch (TREE_CODE (t
))
6355 body
= LOOP_EXPR_BODY (t
);
6362 body
= WHILE_BODY (t
);
6363 cond
= WHILE_COND (t
);
6367 if (FOR_INIT_STMT (t
))
6368 cxx_eval_constant_expression (ctx
, FOR_INIT_STMT (t
), vc_discard
,
6369 non_constant_p
, overflow_p
, jump_target
);
6370 if (*non_constant_p
)
6372 body
= FOR_BODY (t
);
6373 cond
= FOR_COND (t
);
6374 expr
= FOR_EXPR (t
);
6380 auto_vec
<tree
, 10> save_exprs
;
6381 new_ctx
.save_exprs
= &save_exprs
;
6387 cxx_eval_constant_expression (&new_ctx
, body
, vc_discard
,
6388 non_constant_p
, overflow_p
,
6390 if (breaks (jump_target
))
6392 *jump_target
= NULL_TREE
;
6396 if (TREE_CODE (t
) != LOOP_EXPR
&& continues (jump_target
))
6397 *jump_target
= NULL_TREE
;
6400 cxx_eval_constant_expression (&new_ctx
, expr
, vc_prvalue
,
6401 non_constant_p
, overflow_p
,
6408 = cxx_eval_constant_expression (&new_ctx
, cond
, vc_prvalue
,
6409 non_constant_p
, overflow_p
,
6413 if (verify_constant (res
, ctx
->quiet
, non_constant_p
,
6416 if (integer_zerop (res
))
6420 gcc_assert (*jump_target
);
6423 /* Forget saved values of SAVE_EXPRs and TARGET_EXPRs. */
6424 for (tree save_expr
: save_exprs
)
6425 ctx
->global
->values
.remove (save_expr
);
6426 save_exprs
.truncate (0);
6428 if (++count
>= constexpr_loop_limit
)
6431 error_at (cp_expr_loc_or_input_loc (t
),
6432 "%<constexpr%> loop iteration count exceeds limit of %d "
6433 "(use %<-fconstexpr-loop-limit=%> to increase the limit)",
6434 constexpr_loop_limit
);
6435 *non_constant_p
= true;
6439 while (!returns (jump_target
)
6440 && !breaks (jump_target
)
6441 && !continues (jump_target
)
6442 && (!switches (jump_target
) || count
== 0)
6443 && !*non_constant_p
);
6445 /* Forget saved values of SAVE_EXPRs and TARGET_EXPRs. */
6446 for (tree save_expr
: save_exprs
)
6447 ctx
->global
->values
.remove (save_expr
);
6452 /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
6456 cxx_eval_switch_expr (const constexpr_ctx
*ctx
, tree t
,
6457 bool *non_constant_p
, bool *overflow_p
,
6461 = TREE_CODE (t
) == SWITCH_STMT
? SWITCH_STMT_COND (t
) : SWITCH_COND (t
);
6462 cond
= cxx_eval_constant_expression (ctx
, cond
, vc_prvalue
,
6463 non_constant_p
, overflow_p
);
6464 VERIFY_CONSTANT (cond
);
6465 *jump_target
= cond
;
6468 = TREE_CODE (t
) == SWITCH_STMT
? SWITCH_STMT_BODY (t
) : SWITCH_BODY (t
);
6469 constexpr_ctx new_ctx
= *ctx
;
6470 constexpr_switch_state css
= css_default_not_seen
;
6471 new_ctx
.css_state
= &css
;
6472 cxx_eval_constant_expression (&new_ctx
, body
, vc_discard
,
6473 non_constant_p
, overflow_p
, jump_target
);
6474 if (switches (jump_target
) && css
== css_default_seen
)
6476 /* If the SWITCH_EXPR body has default: label, process it once again,
6477 this time instructing label_matches to return true for default:
6478 label on switches (jump_target). */
6479 css
= css_default_processing
;
6480 cxx_eval_constant_expression (&new_ctx
, body
, vc_discard
,
6481 non_constant_p
, overflow_p
, jump_target
);
6483 if (breaks (jump_target
) || switches (jump_target
))
6484 *jump_target
= NULL_TREE
;
6488 /* Find the object of TYPE under initialization in CTX. */
6491 lookup_placeholder (const constexpr_ctx
*ctx
, value_cat lval
, tree type
)
6496 /* Prefer the outermost matching object, but don't cross
6497 CONSTRUCTOR_PLACEHOLDER_BOUNDARY constructors. */
6498 if (ctx
->ctor
&& !CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx
->ctor
))
6499 if (tree outer_ob
= lookup_placeholder (ctx
->parent
, lval
, type
))
6502 /* We could use ctx->object unconditionally, but using ctx->ctor when we
6503 can is a minor optimization. */
6504 if (!lval
&& ctx
->ctor
&& same_type_p (TREE_TYPE (ctx
->ctor
), type
))
6510 /* Since an object cannot have a field of its own type, we can search outward
6511 from ctx->object to find the unique containing object of TYPE. */
6512 tree ob
= ctx
->object
;
6515 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob
), type
))
6517 if (handled_component_p (ob
))
6518 ob
= TREE_OPERAND (ob
, 0);
6526 /* Complain about an attempt to evaluate inline assembly. */
6529 inline_asm_in_constexpr_error (location_t loc
)
6531 auto_diagnostic_group d
;
6532 error_at (loc
, "inline assembly is not a constant expression");
6533 inform (loc
, "only unevaluated inline assembly is allowed in a "
6534 "%<constexpr%> function in C++20");
6537 /* We're getting the constant value of DECL in a manifestly constant-evaluated
6538 context; maybe complain about that. */
6541 maybe_warn_about_constant_value (location_t loc
, tree decl
)
6543 static bool explained
= false;
6544 if (cxx_dialect
>= cxx17
6545 && warn_interference_size
6546 && !OPTION_SET_P (param_destruct_interfere_size
)
6547 && DECL_CONTEXT (decl
) == std_node
6548 && id_equal (DECL_NAME (decl
), "hardware_destructive_interference_size")
6549 && (LOCATION_FILE (input_location
) != main_input_filename
6550 || module_exporting_p ())
6551 && warning_at (loc
, OPT_Winterference_size
, "use of %qD", decl
)
6555 inform (loc
, "its value can vary between compiler versions or "
6556 "with different %<-mtune%> or %<-mcpu%> flags");
6557 inform (loc
, "if this use is part of a public ABI, change it to "
6558 "instead use a constant variable you define");
6559 inform (loc
, "the default value for the current CPU tuning "
6560 "is %d bytes", param_destruct_interfere_size
);
6561 inform (loc
, "you can stabilize this value with %<--param "
6562 "hardware_destructive_interference_size=%d%>, or disable "
6563 "this warning with %<-Wno-interference-size%>",
6564 param_destruct_interfere_size
);
6568 /* For element type ELT_TYPE, return the appropriate type of the heap object
6569 containing such element(s). COOKIE_SIZE is NULL or the size of cookie
6570 in bytes. If COOKIE_SIZE is NULL, return array type
6571 ELT_TYPE[FULL_SIZE / sizeof(ELT_TYPE)], otherwise return
6572 struct { size_t[COOKIE_SIZE/sizeof(size_t)]; ELT_TYPE[N]; }
6573 where N is is computed such that the size of the struct fits into FULL_SIZE.
6574 If ARG_SIZE is non-NULL, it is the first argument to the new operator.
6575 It should be passed if ELT_TYPE is zero sized type in which case FULL_SIZE
6576 will be also 0 and so it is not possible to determine the actual array
6577 size. CTX, NON_CONSTANT_P and OVERFLOW_P are used during constant
6578 expression evaluation of subexpressions of ARG_SIZE. */
6581 build_new_constexpr_heap_type (const constexpr_ctx
*ctx
, tree elt_type
,
6582 tree cookie_size
, tree full_size
, tree arg_size
,
6583 bool *non_constant_p
, bool *overflow_p
)
6585 gcc_assert (cookie_size
== NULL_TREE
|| tree_fits_uhwi_p (cookie_size
));
6586 gcc_assert (tree_fits_uhwi_p (full_size
));
6587 unsigned HOST_WIDE_INT csz
= cookie_size
? tree_to_uhwi (cookie_size
) : 0;
6590 STRIP_NOPS (arg_size
);
6593 if (TREE_CODE (arg_size
) != PLUS_EXPR
)
6594 arg_size
= NULL_TREE
;
6595 else if (TREE_CODE (TREE_OPERAND (arg_size
, 0)) == INTEGER_CST
6596 && tree_int_cst_equal (cookie_size
,
6597 TREE_OPERAND (arg_size
, 0)))
6599 arg_size
= TREE_OPERAND (arg_size
, 1);
6600 STRIP_NOPS (arg_size
);
6602 else if (TREE_CODE (TREE_OPERAND (arg_size
, 1)) == INTEGER_CST
6603 && tree_int_cst_equal (cookie_size
,
6604 TREE_OPERAND (arg_size
, 1)))
6606 arg_size
= TREE_OPERAND (arg_size
, 0);
6607 STRIP_NOPS (arg_size
);
6610 arg_size
= NULL_TREE
;
6612 if (arg_size
&& TREE_CODE (arg_size
) == MULT_EXPR
)
6614 tree op0
= TREE_OPERAND (arg_size
, 0);
6615 tree op1
= TREE_OPERAND (arg_size
, 1);
6616 if (integer_zerop (op0
))
6618 = cxx_eval_constant_expression (ctx
, op1
, vc_prvalue
,
6619 non_constant_p
, overflow_p
);
6620 else if (integer_zerop (op1
))
6622 = cxx_eval_constant_expression (ctx
, op0
, vc_prvalue
,
6623 non_constant_p
, overflow_p
);
6625 arg_size
= NULL_TREE
;
6628 arg_size
= NULL_TREE
;
6631 unsigned HOST_WIDE_INT fsz
= tree_to_uhwi (arg_size
? arg_size
: full_size
);
6634 unsigned HOST_WIDE_INT esz
= int_size_in_bytes (elt_type
);
6635 gcc_assert (fsz
>= csz
);
6640 tree itype2
= build_index_type (size_int (fsz
- 1));
6642 return build_cplus_array_type (elt_type
, itype2
);
6643 return build_new_constexpr_heap_type (elt_type
, cookie_size
, itype2
);
6646 /* Attempt to reduce the expression T to a constant value.
6647 On failure, issue diagnostic and return error_mark_node. */
6648 /* FIXME unify with c_fully_fold */
6649 /* FIXME overflow_p is too global */
6652 cxx_eval_constant_expression (const constexpr_ctx
*ctx
, tree t
,
6654 bool *non_constant_p
, bool *overflow_p
,
6655 tree
*jump_target
/* = NULL */)
6657 if (jump_target
&& *jump_target
)
6659 /* If we are jumping, ignore all statements/expressions except those
6660 that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */
6661 switch (TREE_CODE (t
))
6664 case STATEMENT_LIST
:
6673 case CASE_LABEL_EXPR
:
6674 if (label_matches (ctx
, jump_target
, t
))
6676 *jump_target
= NULL_TREE
;
6682 if (error_operand_p (t
))
6684 *non_constant_p
= true;
6688 location_t loc
= cp_expr_loc_or_input_loc (t
);
6690 STRIP_ANY_LOCATION_WRAPPER (t
);
6692 if (CONSTANT_CLASS_P (t
))
6694 if (TREE_OVERFLOW (t
))
6697 permerror (input_location
, "overflow in constant expression");
6698 if (!flag_permissive
|| ctx
->quiet
)
6702 if (TREE_CODE (t
) == INTEGER_CST
6703 && TYPE_PTR_P (TREE_TYPE (t
))
6704 /* INTEGER_CST with pointer-to-method type is only used
6705 for a virtual method in a pointer to member function.
6706 Don't reject those. */
6707 && TREE_CODE (TREE_TYPE (TREE_TYPE (t
))) != METHOD_TYPE
6708 && !integer_zerop (t
))
6711 error ("value %qE of type %qT is not a constant expression",
6713 *non_constant_p
= true;
6719 /* Avoid excessively long constexpr evaluations. */
6720 if (++ctx
->global
->constexpr_ops_count
>= constexpr_ops_limit
)
6724 "%<constexpr%> evaluation operation count exceeds limit of "
6725 "%wd (use %<-fconstexpr-ops-limit=%> to increase the limit)",
6726 constexpr_ops_limit
);
6727 ctx
->global
->constexpr_ops_count
= INTTYPE_MINIMUM (HOST_WIDE_INT
);
6728 *non_constant_p
= true;
6732 constexpr_ctx new_ctx
;
6735 tree_code tcode
= TREE_CODE (t
);
6741 /* We ask for an rvalue for the RESULT_DECL when indirecting
6742 through an invisible reference, or in named return value
6744 if (tree
*p
= ctx
->global
->values
.get (t
))
6749 error ("%qE is not a constant expression", t
);
6750 *non_constant_p
= true;
6755 if (DECL_HAS_VALUE_EXPR_P (t
))
6757 if (is_normal_capture_proxy (t
)
6758 && current_function_decl
== DECL_CONTEXT (t
))
6760 /* Function parms aren't constexpr within the function
6761 definition, so don't try to look at the closure. But if the
6762 captured variable is constant, try to evaluate it directly. */
6763 r
= DECL_CAPTURED_VARIABLE (t
);
6764 tree type
= TREE_TYPE (t
);
6765 if (TYPE_REF_P (type
) != TYPE_REF_P (TREE_TYPE (r
)))
6767 /* Adjust r to match the reference-ness of t. */
6768 if (TYPE_REF_P (type
))
6769 r
= build_address (r
);
6771 r
= convert_from_reference (r
);
6775 r
= DECL_VALUE_EXPR (t
);
6776 return cxx_eval_constant_expression (ctx
, r
, lval
, non_constant_p
,
6781 /* We used to not check lval for CONST_DECL, but darwin.cc uses
6782 CONST_DECL for aggregate constants. */
6785 else if (t
== ctx
->object
)
6788 if (tree
*p
= ctx
->global
->values
.get (t
))
6789 if (*p
!= NULL_TREE
)
6794 if (ctx
->manifestly_const_eval
)
6795 maybe_warn_about_constant_value (loc
, t
);
6796 if (COMPLETE_TYPE_P (TREE_TYPE (t
))
6797 && is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
6799 /* If the class is empty, we aren't actually loading anything. */
6800 r
= build_constructor (TREE_TYPE (t
), NULL
);
6801 TREE_CONSTANT (r
) = true;
6803 else if (ctx
->strict
)
6804 r
= decl_really_constant_value (t
, /*unshare_p=*/false);
6806 r
= decl_constant_value (t
, /*unshare_p=*/false);
6807 if (TREE_CODE (r
) == TARGET_EXPR
6808 && TREE_CODE (TARGET_EXPR_INITIAL (r
)) == CONSTRUCTOR
)
6809 r
= TARGET_EXPR_INITIAL (r
);
6813 non_const_var_error (loc
, r
);
6814 *non_constant_p
= true;
6818 case DEBUG_BEGIN_STMT
:
6819 /* ??? It might be nice to retain this information somehow, so
6820 as to be able to step into a constexpr function call. */
6827 case CASE_LABEL_EXPR
:
6832 if (lval
&& !TYPE_REF_P (TREE_TYPE (t
)))
6834 else if (tree
*p
= ctx
->global
->values
.get (r
))
6837 /* Defer in case this is only used for its type. */;
6838 else if (COMPLETE_TYPE_P (TREE_TYPE (t
))
6839 && is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
6841 /* If the class is empty, we aren't actually loading anything. */
6842 r
= build_constructor (TREE_TYPE (t
), NULL
);
6843 TREE_CONSTANT (r
) = true;
6848 error ("%qE is not a constant expression", t
);
6849 *non_constant_p
= true;
6854 case AGGR_INIT_EXPR
:
6855 r
= cxx_eval_call_expression (ctx
, t
, lval
,
6856 non_constant_p
, overflow_p
);
6861 r
= DECL_EXPR_DECL (t
);
6862 if (TREE_CODE (r
) == USING_DECL
)
6870 || (CP_DECL_THREAD_LOCAL_P (r
) && !DECL_REALLY_EXTERN (r
)))
6871 /* Allow __FUNCTION__ etc. */
6872 && !DECL_ARTIFICIAL (r
))
6876 if (CP_DECL_THREAD_LOCAL_P (r
))
6877 error_at (loc
, "control passes through definition of %qD "
6878 "with thread storage duration", r
);
6880 error_at (loc
, "control passes through definition of %qD "
6881 "with static storage duration", r
);
6883 *non_constant_p
= true;
6887 if (AGGREGATE_TYPE_P (TREE_TYPE (r
))
6888 || VECTOR_TYPE_P (TREE_TYPE (r
)))
6892 new_ctx
.ctor
= build_constructor (TREE_TYPE (r
), NULL
);
6893 CONSTRUCTOR_NO_CLEARING (new_ctx
.ctor
) = true;
6894 ctx
->global
->values
.put (r
, new_ctx
.ctor
);
6898 if (tree init
= DECL_INITIAL (r
))
6900 init
= cxx_eval_constant_expression (ctx
, init
, vc_prvalue
,
6901 non_constant_p
, overflow_p
);
6902 /* Don't share a CONSTRUCTOR that might be changed. */
6903 init
= unshare_constructor (init
);
6904 /* Remember that a constant object's constructor has already
6906 if (CLASS_TYPE_P (TREE_TYPE (r
))
6907 && CP_TYPE_CONST_P (TREE_TYPE (r
)))
6908 TREE_READONLY (init
) = true;
6909 ctx
->global
->values
.put (r
, init
);
6911 else if (ctx
== &new_ctx
)
6912 /* We gave it a CONSTRUCTOR above. */;
6914 ctx
->global
->values
.put (r
, NULL_TREE
);
6920 tree type
= TREE_TYPE (t
);
6922 if (!literal_type_p (type
))
6926 auto_diagnostic_group d
;
6927 error ("temporary of non-literal type %qT in a "
6928 "constant expression", type
);
6929 explain_non_literal_class (type
);
6931 *non_constant_p
= true;
6934 gcc_checking_assert (!TARGET_EXPR_DIRECT_INIT_P (t
));
6935 /* Avoid evaluating a TARGET_EXPR more than once. */
6936 tree slot
= TARGET_EXPR_SLOT (t
);
6937 if (tree
*p
= ctx
->global
->values
.get (slot
))
6944 if ((AGGREGATE_TYPE_P (type
) || VECTOR_TYPE_P (type
)))
6946 /* We're being expanded without an explicit target, so start
6947 initializing a new object; expansion with an explicit target
6948 strips the TARGET_EXPR before we get here. */
6950 /* Link CTX to NEW_CTX so that lookup_placeholder can resolve
6951 any PLACEHOLDER_EXPR within the initializer that refers to the
6952 former object under construction. */
6953 new_ctx
.parent
= ctx
;
6954 new_ctx
.ctor
= build_constructor (type
, NULL
);
6955 CONSTRUCTOR_NO_CLEARING (new_ctx
.ctor
) = true;
6956 new_ctx
.object
= slot
;
6957 ctx
->global
->values
.put (new_ctx
.object
, new_ctx
.ctor
);
6960 /* Pass vc_prvalue because this indicates
6961 initialization of a temporary. */
6962 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1), vc_prvalue
,
6963 non_constant_p
, overflow_p
);
6964 if (*non_constant_p
)
6966 /* Adjust the type of the result to the type of the temporary. */
6967 r
= adjust_temp_type (type
, r
);
6968 if (TARGET_EXPR_CLEANUP (t
) && !CLEANUP_EH_ONLY (t
))
6969 ctx
->global
->cleanups
->safe_push (TARGET_EXPR_CLEANUP (t
));
6970 r
= unshare_constructor (r
);
6971 ctx
->global
->values
.put (slot
, r
);
6972 if (ctx
->save_exprs
)
6973 ctx
->save_exprs
->safe_push (slot
);
6981 gcc_assert (jump_target
== NULL
|| *jump_target
== NULL_TREE
);
6982 r
= cxx_eval_store_expression (ctx
, t
, lval
,
6983 non_constant_p
, overflow_p
);
6987 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
6989 non_constant_p
, overflow_p
);
6993 if (TREE_OPERAND (t
, 0) != NULL_TREE
)
6994 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
6996 non_constant_p
, overflow_p
);
7004 /* Can happen with ({ return true; }) && false; passed to
7005 maybe_constant_value. There is nothing to jump over in this
7006 case, and the bug will be diagnosed later. */
7007 gcc_assert (ctx
->quiet
);
7008 *non_constant_p
= true;
7013 /* Avoid evaluating a SAVE_EXPR more than once. */
7014 if (tree
*p
= ctx
->global
->values
.get (t
))
7018 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), vc_prvalue
,
7019 non_constant_p
, overflow_p
);
7020 if (*non_constant_p
)
7022 ctx
->global
->values
.put (t
, r
);
7023 if (ctx
->save_exprs
)
7024 ctx
->save_exprs
->safe_push (t
);
7028 case TRY_CATCH_EXPR
:
7029 if (TREE_OPERAND (t
, 0) == NULL_TREE
)
7035 case NON_LVALUE_EXPR
:
7037 case MUST_NOT_THROW_EXPR
:
7040 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
7042 non_constant_p
, overflow_p
,
7046 case CLEANUP_POINT_EXPR
:
7048 auto_vec
<tree
, 2> cleanups
;
7049 vec
<tree
> *prev_cleanups
= ctx
->global
->cleanups
;
7050 ctx
->global
->cleanups
= &cleanups
;
7051 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
7053 non_constant_p
, overflow_p
,
7055 ctx
->global
->cleanups
= prev_cleanups
;
7058 /* Evaluate the cleanups. */
7059 FOR_EACH_VEC_ELT_REVERSE (cleanups
, i
, cleanup
)
7060 cxx_eval_constant_expression (ctx
, cleanup
, vc_discard
,
7061 non_constant_p
, overflow_p
);
7065 case TRY_FINALLY_EXPR
:
7066 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), lval
,
7067 non_constant_p
, overflow_p
,
7069 if (!*non_constant_p
)
7070 /* Also evaluate the cleanup. */
7071 cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1), vc_discard
,
7072 non_constant_p
, overflow_p
);
7076 r
= cxx_eval_constant_expression (ctx
, CLEANUP_BODY (t
), lval
,
7077 non_constant_p
, overflow_p
,
7079 if (!CLEANUP_EH_ONLY (t
) && !*non_constant_p
)
7081 iloc_sentinel
ils (loc
);
7082 /* Also evaluate the cleanup. */
7083 cxx_eval_constant_expression (ctx
, CLEANUP_EXPR (t
), vc_discard
,
7084 non_constant_p
, overflow_p
);
7088 /* These differ from cxx_eval_unary_expression in that this doesn't
7089 check for a constant operand or result; an address can be
7090 constant without its operand being, and vice versa. */
7093 r
= cxx_eval_indirect_ref (ctx
, t
, lval
,
7094 non_constant_p
, overflow_p
);
7099 tree oldop
= TREE_OPERAND (t
, 0);
7100 tree op
= cxx_eval_constant_expression (ctx
, oldop
, vc_glvalue
,
7101 non_constant_p
, overflow_p
);
7102 /* Don't VERIFY_CONSTANT here. */
7103 if (*non_constant_p
)
7105 gcc_checking_assert (TREE_CODE (op
) != CONSTRUCTOR
);
7106 /* This function does more aggressive folding than fold itself. */
7107 r
= build_fold_addr_expr_with_type (op
, TREE_TYPE (t
));
7108 if (TREE_CODE (r
) == ADDR_EXPR
&& TREE_OPERAND (r
, 0) == oldop
)
7120 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), lval
,
7121 non_constant_p
, overflow_p
);
7122 if (r
== error_mark_node
)
7124 else if (r
== TREE_OPERAND (t
, 0) || lval
== vc_discard
)
7127 r
= fold_build1 (TREE_CODE (t
), TREE_TYPE (t
), r
);
7132 case FIX_TRUNC_EXPR
:
7138 case TRUTH_NOT_EXPR
:
7139 case FIXED_CONVERT_EXPR
:
7140 r
= cxx_eval_unary_expression (ctx
, t
, lval
,
7141 non_constant_p
, overflow_p
);
7145 r
= fold_sizeof_expr (t
);
7146 /* In a template, fold_sizeof_expr may merely create a new SIZEOF_EXPR,
7147 which could lead to an infinite recursion. */
7148 if (TREE_CODE (r
) != SIZEOF_EXPR
)
7149 r
= cxx_eval_constant_expression (ctx
, r
, lval
,
7150 non_constant_p
, overflow_p
,
7154 *non_constant_p
= true;
7155 gcc_assert (ctx
->quiet
);
7162 /* check_return_expr sometimes wraps a TARGET_EXPR in a
7163 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
7164 introduced by build_call_a. */
7165 tree op0
= TREE_OPERAND (t
, 0);
7166 tree op1
= TREE_OPERAND (t
, 1);
7168 if ((TREE_CODE (op0
) == TARGET_EXPR
&& op1
== TARGET_EXPR_SLOT (op0
))
7169 || TREE_CODE (op1
) == EMPTY_CLASS_EXPR
)
7170 r
= cxx_eval_constant_expression (ctx
, op0
,
7171 lval
, non_constant_p
, overflow_p
,
7175 /* Check that the LHS is constant and then discard it. */
7176 cxx_eval_constant_expression (ctx
, op0
, vc_discard
,
7177 non_constant_p
, overflow_p
,
7179 if (*non_constant_p
)
7181 op1
= TREE_OPERAND (t
, 1);
7182 r
= cxx_eval_constant_expression (ctx
, op1
,
7183 lval
, non_constant_p
, overflow_p
,
7189 case POINTER_PLUS_EXPR
:
7190 case POINTER_DIFF_EXPR
:
7194 case TRUNC_DIV_EXPR
:
7196 case FLOOR_DIV_EXPR
:
7197 case ROUND_DIV_EXPR
:
7198 case TRUNC_MOD_EXPR
:
7200 case ROUND_MOD_EXPR
:
7202 case EXACT_DIV_EXPR
:
7212 case TRUTH_XOR_EXPR
:
7219 case SPACESHIP_EXPR
:
7220 case UNORDERED_EXPR
:
7230 r
= cxx_eval_binary_expression (ctx
, t
, lval
,
7231 non_constant_p
, overflow_p
);
7234 /* fold can introduce non-IF versions of these; still treat them as
7235 short-circuiting. */
7236 case TRUTH_AND_EXPR
:
7237 case TRUTH_ANDIF_EXPR
:
7238 r
= cxx_eval_logical_expression (ctx
, t
, boolean_false_node
,
7240 non_constant_p
, overflow_p
);
7244 case TRUTH_ORIF_EXPR
:
7245 r
= cxx_eval_logical_expression (ctx
, t
, boolean_true_node
,
7247 non_constant_p
, overflow_p
);
7251 r
= cxx_eval_array_reference (ctx
, t
, lval
,
7252 non_constant_p
, overflow_p
);
7256 if (is_overloaded_fn (t
))
7258 /* We can only get here in checking mode via
7259 build_non_dependent_expr, because any expression that
7260 calls or takes the address of the function will have
7261 pulled a FUNCTION_DECL out of the COMPONENT_REF. */
7262 gcc_checking_assert (ctx
->quiet
|| errorcount
);
7263 *non_constant_p
= true;
7266 r
= cxx_eval_component_reference (ctx
, t
, lval
,
7267 non_constant_p
, overflow_p
);
7271 r
= cxx_eval_bit_field_ref (ctx
, t
, lval
,
7272 non_constant_p
, overflow_p
);
7277 if (jump_target
&& *jump_target
)
7279 tree orig_jump
= *jump_target
;
7280 tree arg
= ((TREE_CODE (t
) != IF_STMT
|| TREE_OPERAND (t
, 1))
7281 ? TREE_OPERAND (t
, 1) : void_node
);
7282 /* When jumping to a label, the label might be either in the
7283 then or else blocks, so process then block first in skipping
7284 mode first, and if we are still in the skipping mode at its end,
7285 process the else block too. */
7286 r
= cxx_eval_constant_expression (ctx
, arg
, lval
, non_constant_p
,
7287 overflow_p
, jump_target
);
7288 /* It's possible that we found the label in the then block. But
7289 it could have been followed by another jumping statement, e.g.
7290 say we're looking for case 1:
7293 // skipped statements
7294 case 1:; // clears up *jump_target
7295 return 1; // and sets it to a RETURN_EXPR
7298 in which case we need not go looking to the else block.
7299 (goto is not allowed in a constexpr function.) */
7300 if (*jump_target
== orig_jump
)
7302 arg
= ((TREE_CODE (t
) != IF_STMT
|| TREE_OPERAND (t
, 2))
7303 ? TREE_OPERAND (t
, 2) : void_node
);
7304 r
= cxx_eval_constant_expression (ctx
, arg
, lval
, non_constant_p
,
7305 overflow_p
, jump_target
);
7309 r
= cxx_eval_conditional_expression (ctx
, t
, lval
,
7310 non_constant_p
, overflow_p
,
7314 r
= cxx_eval_vector_conditional_expression (ctx
, t
, non_constant_p
,
7319 if (TREE_CONSTANT (t
) && reduced_constant_expression_p (t
))
7321 /* Don't re-process a constant CONSTRUCTOR, but do fold it to
7322 VECTOR_CST if applicable. */
7323 verify_constructor_flags (t
);
7324 if (TREE_CONSTANT (t
))
7327 r
= cxx_eval_bare_aggregate (ctx
, t
, lval
,
7328 non_constant_p
, overflow_p
);
7332 /* We can get this in a defaulted constructor for a class with a
7333 non-static data member of array type. Either the initializer will
7334 be NULL, meaning default-initialization, or it will be an lvalue
7335 or xvalue of the same type, meaning direct-initialization from the
7336 corresponding member. */
7337 r
= cxx_eval_vec_init (ctx
, t
, lval
,
7338 non_constant_p
, overflow_p
);
7342 r
= cxx_eval_trinary_expression (ctx
, t
, lval
,
7343 non_constant_p
, overflow_p
);
7347 gcc_assert (!REF_PARENTHESIZED_P (t
));
7348 /* A PAREN_EXPR resulting from __builtin_assoc_barrier has no effect in
7349 constant expressions since it's unaffected by -fassociative-math. */
7350 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), lval
,
7351 non_constant_p
, overflow_p
);
7355 if (REINTERPRET_CAST_P (t
))
7359 "%<reinterpret_cast%> is not a constant expression");
7360 *non_constant_p
= true;
7365 case VIEW_CONVERT_EXPR
:
7366 case UNARY_PLUS_EXPR
:
7368 tree oldop
= TREE_OPERAND (t
, 0);
7370 tree op
= cxx_eval_constant_expression (ctx
, oldop
,
7372 non_constant_p
, overflow_p
);
7373 if (*non_constant_p
)
7375 tree type
= TREE_TYPE (t
);
7377 if (VOID_TYPE_P (type
))
7380 if (TREE_CODE (t
) == CONVERT_EXPR
7381 && ARITHMETIC_TYPE_P (type
)
7382 && INDIRECT_TYPE_P (TREE_TYPE (op
))
7383 && ctx
->manifestly_const_eval
)
7387 "conversion from pointer type %qT to arithmetic type "
7388 "%qT in a constant expression", TREE_TYPE (op
), type
);
7389 *non_constant_p
= true;
7393 /* [expr.const]: a conversion from type cv void* to a pointer-to-object
7394 type cannot be part of a core constant expression as a resolution to
7396 if (TYPE_PTROB_P (type
)
7397 && TYPE_PTR_P (TREE_TYPE (op
))
7398 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (op
)))
7399 /* Inside a call to std::construct_at or to
7400 std::allocator<T>::{,de}allocate, we permit casting from void*
7401 because that is compiler-generated code. */
7402 && !is_std_construct_at (ctx
->call
)
7403 && !is_std_allocator_allocate (ctx
->call
))
7405 /* Likewise, don't error when casting from void* when OP is
7406 &heap uninit and similar. */
7407 tree sop
= tree_strip_nop_conversions (op
);
7408 if (TREE_CODE (sop
) == ADDR_EXPR
7409 && VAR_P (TREE_OPERAND (sop
, 0))
7410 && DECL_ARTIFICIAL (TREE_OPERAND (sop
, 0)))
7415 error_at (loc
, "cast from %qT is not allowed",
7417 *non_constant_p
= true;
7422 if (TREE_CODE (op
) == PTRMEM_CST
&& !TYPE_PTRMEM_P (type
))
7423 op
= cplus_expand_constant (op
);
7425 if (TREE_CODE (op
) == PTRMEM_CST
&& tcode
== NOP_EXPR
)
7427 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (op
))
7428 && !can_convert_qual (type
, op
))
7429 op
= cplus_expand_constant (op
);
7430 return cp_fold_convert (type
, op
);
7433 if (INDIRECT_TYPE_P (type
) && TREE_CODE (op
) == INTEGER_CST
)
7435 if (integer_zerop (op
))
7437 if (TYPE_REF_P (type
))
7440 error_at (loc
, "dereferencing a null pointer");
7441 *non_constant_p
= true;
7447 /* This detects for example:
7448 reinterpret_cast<void*>(sizeof 0)
7451 error_at (loc
, "%<reinterpret_cast<%T>(%E)%> is not "
7452 "a constant expression",
7454 *non_constant_p
= true;
7459 if (INDIRECT_TYPE_P (type
)
7460 && TREE_CODE (op
) == NOP_EXPR
7461 && TREE_TYPE (op
) == ptr_type_node
7462 && TREE_CODE (TREE_OPERAND (op
, 0)) == ADDR_EXPR
7463 && VAR_P (TREE_OPERAND (TREE_OPERAND (op
, 0), 0))
7464 && (DECL_NAME (TREE_OPERAND (TREE_OPERAND (op
, 0),
7465 0)) == heap_uninit_identifier
7466 || DECL_NAME (TREE_OPERAND (TREE_OPERAND (op
, 0),
7467 0)) == heap_vec_uninit_identifier
))
7469 tree var
= TREE_OPERAND (TREE_OPERAND (op
, 0), 0);
7470 tree var_size
= TYPE_SIZE_UNIT (TREE_TYPE (var
));
7471 tree elt_type
= TREE_TYPE (type
);
7472 tree cookie_size
= NULL_TREE
;
7473 tree arg_size
= NULL_TREE
;
7474 if (TREE_CODE (elt_type
) == RECORD_TYPE
7475 && TYPE_NAME (elt_type
) == heap_identifier
)
7477 tree fld1
= TYPE_FIELDS (elt_type
);
7478 tree fld2
= DECL_CHAIN (fld1
);
7479 elt_type
= TREE_TYPE (TREE_TYPE (fld2
));
7480 cookie_size
= TYPE_SIZE_UNIT (TREE_TYPE (fld1
));
7483 = (DECL_NAME (var
) == heap_uninit_identifier
7484 ? heap_identifier
: heap_vec_identifier
);
7485 /* For zero sized elt_type, try to recover how many outer_nelts
7487 if ((cookie_size
? tree_int_cst_equal (var_size
, cookie_size
)
7488 : integer_zerop (var_size
))
7489 && !int_size_in_bytes (elt_type
)
7490 && TREE_CODE (oldop
) == CALL_EXPR
7491 && call_expr_nargs (oldop
) >= 1)
7492 if (tree fun
= get_function_named_in_call (oldop
))
7493 if (cxx_replaceable_global_alloc_fn (fun
)
7494 && IDENTIFIER_NEW_OP_P (DECL_NAME (fun
)))
7495 arg_size
= CALL_EXPR_ARG (oldop
, 0);
7497 = build_new_constexpr_heap_type (ctx
, elt_type
, cookie_size
,
7499 non_constant_p
, overflow_p
);
7500 TREE_TYPE (TREE_OPERAND (op
, 0))
7501 = build_pointer_type (TREE_TYPE (var
));
7504 if (op
== oldop
&& tcode
!= UNARY_PLUS_EXPR
)
7505 /* We didn't fold at the top so we could check for ptr-int
7511 /* Handle an array's bounds having been deduced after we built
7512 the wrapping expression. */
7513 if (same_type_ignoring_tlq_and_bounds_p (type
, TREE_TYPE (op
)))
7515 else if (sop
= tree_strip_nop_conversions (op
),
7516 sop
!= op
&& (same_type_ignoring_tlq_and_bounds_p
7517 (type
, TREE_TYPE (sop
))))
7519 else if (tcode
== UNARY_PLUS_EXPR
)
7520 r
= fold_convert (TREE_TYPE (t
), op
);
7522 r
= fold_build1 (tcode
, type
, op
);
7524 /* Conversion of an out-of-range value has implementation-defined
7525 behavior; the language considers it different from arithmetic
7526 overflow, which is undefined. */
7527 if (TREE_OVERFLOW_P (r
) && !TREE_OVERFLOW_P (op
))
7528 TREE_OVERFLOW (r
) = false;
7532 case EMPTY_CLASS_EXPR
:
7533 /* Handle EMPTY_CLASS_EXPR produced by build_call_a by lowering
7534 it to an appropriate CONSTRUCTOR. */
7535 return build_constructor (TREE_TYPE (t
), NULL
);
7537 case STATEMENT_LIST
:
7539 new_ctx
.ctor
= new_ctx
.object
= NULL_TREE
;
7540 return cxx_eval_statement_list (&new_ctx
, t
,
7541 non_constant_p
, overflow_p
, jump_target
);
7544 return cxx_eval_constant_expression (ctx
, BIND_EXPR_BODY (t
),
7546 non_constant_p
, overflow_p
,
7549 case PREINCREMENT_EXPR
:
7550 case POSTINCREMENT_EXPR
:
7551 case PREDECREMENT_EXPR
:
7552 case POSTDECREMENT_EXPR
:
7553 return cxx_eval_increment_expression (ctx
, t
,
7554 lval
, non_constant_p
, overflow_p
);
7560 case VEC_DELETE_EXPR
:
7563 /* GCC internal stuff. */
7565 case NON_DEPENDENT_EXPR
:
7569 error_at (loc
, "expression %qE is not a constant expression", t
);
7570 *non_constant_p
= true;
7574 /* Virtual function lookup. We don't need to do anything fancy. */
7575 return cxx_eval_constant_expression (ctx
, OBJ_TYPE_REF_EXPR (t
),
7576 lval
, non_constant_p
, overflow_p
);
7578 case PLACEHOLDER_EXPR
:
7579 /* Use of the value or address of the current object. */
7580 if (tree ctor
= lookup_placeholder (ctx
, lval
, TREE_TYPE (t
)))
7582 if (TREE_CODE (ctor
) == CONSTRUCTOR
)
7585 return cxx_eval_constant_expression (ctx
, ctor
, lval
,
7586 non_constant_p
, overflow_p
);
7588 /* A placeholder without a referent. We can get here when
7589 checking whether NSDMIs are noexcept, or in massage_init_elt;
7590 just say it's non-constant for now. */
7591 gcc_assert (ctx
->quiet
);
7592 *non_constant_p
= true;
7597 tree cond
= TREE_OPERAND (t
, 0);
7598 cond
= cxx_eval_constant_expression (ctx
, cond
, vc_prvalue
,
7599 non_constant_p
, overflow_p
);
7600 VERIFY_CONSTANT (cond
);
7601 if (integer_nonzerop (cond
))
7607 if (breaks (&TREE_OPERAND (t
, 0))
7608 || continues (&TREE_OPERAND (t
, 0)))
7609 *jump_target
= TREE_OPERAND (t
, 0);
7612 gcc_assert (cxx_dialect
>= cxx23
);
7614 error_at (loc
, "%<goto%> is not a constant expression");
7615 *non_constant_p
= true;
7623 cxx_eval_loop_expr (ctx
, t
,
7624 non_constant_p
, overflow_p
, jump_target
);
7629 cxx_eval_switch_expr (ctx
, t
,
7630 non_constant_p
, overflow_p
, jump_target
);
7634 /* It's possible to get a requires-expression in a constant
7635 expression. For example:
7637 template<typename T> concept bool C() {
7638 return requires (T t) { t; };
7641 template<typename T> requires !C<T>() void f(T);
7643 Normalization leaves f with the associated constraint
7644 '!requires (T t) { ... }' which is not transformed into
7646 if (!processing_template_decl
)
7647 return evaluate_requires_expr (t
);
7649 *non_constant_p
= true;
7653 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
7655 non_constant_p
, overflow_p
,
7663 case TEMPLATE_ID_EXPR
:
7665 /* We can evaluate template-id that refers to a concept only if
7666 the template arguments are non-dependent. */
7667 tree id
= unpack_concept_check (t
);
7668 tree tmpl
= TREE_OPERAND (id
, 0);
7669 if (!concept_definition_p (tmpl
))
7670 internal_error ("unexpected template-id %qE", t
);
7672 if (function_concept_p (tmpl
))
7675 error_at (cp_expr_loc_or_input_loc (t
),
7676 "function concept must be called");
7677 r
= error_mark_node
;
7681 if (!value_dependent_expression_p (t
)
7682 && !uid_sensitive_constexpr_evaluation_p ())
7683 r
= evaluate_concept_check (t
);
7685 *non_constant_p
= true;
7692 inline_asm_in_constexpr_error (loc
);
7693 *non_constant_p
= true;
7700 error_at (EXPR_LOCATION (t
),
7701 "address of a call to %qs is not a constant expression",
7702 "__builtin_bit_cast");
7703 *non_constant_p
= true;
7706 r
= cxx_eval_bit_cast (ctx
, t
, non_constant_p
, overflow_p
);
7710 if (STATEMENT_CODE_P (TREE_CODE (t
)))
7712 /* This function doesn't know how to deal with pre-genericize
7713 statements; this can only happen with statement-expressions,
7714 so for now just fail. */
7716 error_at (EXPR_LOCATION (t
),
7717 "statement is not a constant expression");
7720 internal_error ("unexpected expression %qE of kind %s", t
,
7721 get_tree_code_name (TREE_CODE (t
)));
7722 *non_constant_p
= true;
7726 if (r
== error_mark_node
)
7727 *non_constant_p
= true;
7729 if (*non_constant_p
)
7735 /* P0859: A function is needed for constant evaluation if it is a constexpr
7736 function that is named by an expression ([basic.def.odr]) that is
7737 potentially constant evaluated.
7739 So we need to instantiate any constexpr functions mentioned by the
7740 expression even if the definition isn't needed for evaluating the
7744 instantiate_cx_fn_r (tree
*tp
, int *walk_subtrees
, void */
*data*/
)
7746 if (TREE_CODE (*tp
) == FUNCTION_DECL
7747 && DECL_DECLARED_CONSTEXPR_P (*tp
)
7748 && !DECL_INITIAL (*tp
)
7749 && !trivial_fn_p (*tp
)
7750 && DECL_TEMPLOID_INSTANTIATION (*tp
)
7751 && !uid_sensitive_constexpr_evaluation_p ())
7754 instantiate_decl (*tp
, /*defer_ok*/false, /*expl_inst*/false);
7757 else if (TREE_CODE (*tp
) == CALL_EXPR
7758 || TREE_CODE (*tp
) == AGGR_INIT_EXPR
)
7760 if (EXPR_HAS_LOCATION (*tp
))
7761 input_location
= EXPR_LOCATION (*tp
);
7771 instantiate_constexpr_fns (tree t
)
7773 location_t loc
= input_location
;
7774 cp_walk_tree_without_duplicates (&t
, instantiate_cx_fn_r
, NULL
);
7775 input_location
= loc
;
7778 /* Look for heap variables in the expression *TP. */
7781 find_heap_var_refs (tree
*tp
, int *walk_subtrees
, void */
*data*/
)
7784 && (DECL_NAME (*tp
) == heap_uninit_identifier
7785 || DECL_NAME (*tp
) == heap_identifier
7786 || DECL_NAME (*tp
) == heap_vec_uninit_identifier
7787 || DECL_NAME (*tp
) == heap_vec_identifier
7788 || DECL_NAME (*tp
) == heap_deleted_identifier
))
7796 /* Find immediate function decls in *TP if any. */
7799 find_immediate_fndecl (tree
*tp
, int */
*walk_subtrees*/
, void */
*data*/
)
7801 if (TREE_CODE (*tp
) == FUNCTION_DECL
&& DECL_IMMEDIATE_FUNCTION_P (*tp
))
7803 if (TREE_CODE (*tp
) == PTRMEM_CST
7804 && TREE_CODE (PTRMEM_CST_MEMBER (*tp
)) == FUNCTION_DECL
7805 && DECL_IMMEDIATE_FUNCTION_P (PTRMEM_CST_MEMBER (*tp
)))
7806 return PTRMEM_CST_MEMBER (*tp
);
7810 /* T has TREE_CONSTANT set but has been deemed not a valid C++ constant
7811 expression. Return a version of T that has TREE_CONSTANT cleared. */
7814 mark_non_constant (tree t
)
7816 gcc_checking_assert (TREE_CONSTANT (t
));
7818 /* This isn't actually constant, so unset TREE_CONSTANT.
7819 Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires
7820 it to be set if it is invariant address, even when it is not
7821 a valid C++ constant expression. Wrap it with a NOP_EXPR
7823 if (EXPR_P (t
) && TREE_CODE (t
) != ADDR_EXPR
)
7825 else if (TREE_CODE (t
) == CONSTRUCTOR
)
7826 t
= build1 (VIEW_CONVERT_EXPR
, TREE_TYPE (t
), t
);
7828 t
= build_nop (TREE_TYPE (t
), t
);
7829 TREE_CONSTANT (t
) = false;
7833 /* ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
7834 STRICT has the same sense as for constant_value_1: true if we only allow
7835 conforming C++ constant expressions, or false if we want a constant value
7836 even if it doesn't conform.
7837 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
7838 per P0595 even when ALLOW_NON_CONSTANT is true.
7839 CONSTEXPR_DTOR is true when evaluating the dtor of a constexpr variable.
7840 OBJECT must be non-NULL in that case. */
7843 cxx_eval_outermost_constant_expr (tree t
, bool allow_non_constant
,
7845 bool manifestly_const_eval
= false,
7846 bool constexpr_dtor
= false,
7847 tree object
= NULL_TREE
)
7849 auto_timevar
time (TV_CONSTEXPR
);
7851 bool non_constant_p
= false;
7852 bool overflow_p
= false;
7854 if (BRACE_ENCLOSED_INITIALIZER_P (t
))
7856 gcc_checking_assert (allow_non_constant
);
7860 constexpr_global_ctx global_ctx
;
7861 constexpr_ctx ctx
= { &global_ctx
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
7862 allow_non_constant
, strict
,
7863 manifestly_const_eval
|| !allow_non_constant
};
7865 /* Turn off -frounding-math for manifestly constant evaluation. */
7866 warning_sentinel
rm (flag_rounding_math
, ctx
.manifestly_const_eval
);
7867 tree type
= initialized_type (t
);
7869 bool is_consteval
= false;
7870 if (VOID_TYPE_P (type
))
7873 /* Used for destructors of array elements. */
7874 type
= TREE_TYPE (object
);
7877 if (cxx_dialect
< cxx20
)
7879 if (TREE_CODE (t
) != CALL_EXPR
&& TREE_CODE (t
) != AGGR_INIT_EXPR
)
7881 /* Calls to immediate functions returning void need to be
7883 tree fndecl
= cp_get_callee_fndecl_nofold (t
);
7884 if (fndecl
== NULL_TREE
|| !DECL_IMMEDIATE_FUNCTION_P (fndecl
))
7887 is_consteval
= true;
7890 else if (cxx_dialect
>= cxx20
7891 && (TREE_CODE (t
) == CALL_EXPR
7892 || TREE_CODE (t
) == AGGR_INIT_EXPR
7893 || TREE_CODE (t
) == TARGET_EXPR
))
7895 /* For non-concept checks, determine if it is consteval. */
7896 if (!concept_check_p (t
))
7899 if (TREE_CODE (x
) == TARGET_EXPR
)
7900 x
= TARGET_EXPR_INITIAL (x
);
7901 tree fndecl
= cp_get_callee_fndecl_nofold (x
);
7902 if (fndecl
&& DECL_IMMEDIATE_FUNCTION_P (fndecl
))
7903 is_consteval
= true;
7906 if (AGGREGATE_TYPE_P (type
) || VECTOR_TYPE_P (type
))
7908 /* In C++14 an NSDMI can participate in aggregate initialization,
7909 and can refer to the address of the object being initialized, so
7910 we need to pass in the relevant VAR_DECL if we want to do the
7911 evaluation in a single pass. The evaluation will dynamically
7912 update ctx.values for the VAR_DECL. We use the same strategy
7913 for C++11 constexpr constructors that refer to the object being
7917 gcc_assert (object
&& VAR_P (object
));
7918 gcc_assert (DECL_DECLARED_CONSTEXPR_P (object
));
7919 gcc_assert (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object
));
7920 if (error_operand_p (DECL_INITIAL (object
)))
7922 ctx
.ctor
= unshare_expr (DECL_INITIAL (object
));
7923 TREE_READONLY (ctx
.ctor
) = false;
7924 /* Temporarily force decl_really_constant_value to return false
7925 for it, we want to use ctx.ctor for the current value instead. */
7926 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object
) = false;
7930 ctx
.ctor
= build_constructor (type
, NULL
);
7931 CONSTRUCTOR_NO_CLEARING (ctx
.ctor
) = true;
7935 if (TREE_CODE (t
) == TARGET_EXPR
)
7936 object
= TARGET_EXPR_SLOT (t
);
7937 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
)
7938 object
= AGGR_INIT_EXPR_SLOT (t
);
7940 ctx
.object
= object
;
7942 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7943 (type
, TREE_TYPE (object
)));
7944 if (object
&& DECL_P (object
))
7945 global_ctx
.values
.put (object
, ctx
.ctor
);
7946 if (TREE_CODE (r
) == TARGET_EXPR
)
7947 /* Avoid creating another CONSTRUCTOR when we expand the
7949 r
= TARGET_EXPR_INITIAL (r
);
7952 auto_vec
<tree
, 16> cleanups
;
7953 global_ctx
.cleanups
= &cleanups
;
7955 if (manifestly_const_eval
)
7956 instantiate_constexpr_fns (r
);
7957 r
= cxx_eval_constant_expression (&ctx
, r
, vc_prvalue
,
7958 &non_constant_p
, &overflow_p
);
7960 if (!constexpr_dtor
)
7961 verify_constant (r
, allow_non_constant
, &non_constant_p
, &overflow_p
);
7963 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object
) = true;
7967 /* Evaluate the cleanups. */
7968 FOR_EACH_VEC_ELT_REVERSE (cleanups
, i
, cleanup
)
7969 cxx_eval_constant_expression (&ctx
, cleanup
, vc_discard
,
7970 &non_constant_p
, &overflow_p
);
7972 /* Mutable logic is a bit tricky: we want to allow initialization of
7973 constexpr variables with mutable members, but we can't copy those
7974 members to another constexpr variable. */
7975 if (TREE_CODE (r
) == CONSTRUCTOR
&& CONSTRUCTOR_MUTABLE_POISON (r
))
7977 if (!allow_non_constant
)
7978 error ("%qE is not a constant expression because it refers to "
7979 "mutable subobjects of %qT", t
, type
);
7980 non_constant_p
= true;
7983 if (TREE_CODE (r
) == CONSTRUCTOR
&& CONSTRUCTOR_NO_CLEARING (r
))
7985 if (!allow_non_constant
)
7986 error ("%qE is not a constant expression because it refers to "
7987 "an incompletely initialized variable", t
);
7988 TREE_CONSTANT (r
) = false;
7989 non_constant_p
= true;
7992 if (!global_ctx
.heap_vars
.is_empty ())
7994 tree heap_var
= cp_walk_tree_without_duplicates (&r
, find_heap_var_refs
,
7999 if (!allow_non_constant
&& !non_constant_p
)
8000 error_at (DECL_SOURCE_LOCATION (heap_var
),
8001 "%qE is not a constant expression because it refers to "
8002 "a result of %<operator new%>", t
);
8004 non_constant_p
= true;
8006 FOR_EACH_VEC_ELT (global_ctx
.heap_vars
, i
, heap_var
)
8008 if (DECL_NAME (heap_var
) != heap_deleted_identifier
)
8010 if (!allow_non_constant
&& !non_constant_p
)
8011 error_at (DECL_SOURCE_LOCATION (heap_var
),
8012 "%qE is not a constant expression because allocated "
8013 "storage has not been deallocated", t
);
8015 non_constant_p
= true;
8017 varpool_node::get (heap_var
)->remove ();
8021 /* Check that immediate invocation does not return an expression referencing
8022 any immediate function decls. */
8023 if (is_consteval
|| in_immediate_context ())
8024 if (tree immediate_fndecl
8025 = cp_walk_tree_without_duplicates (&r
, find_immediate_fndecl
,
8028 if (!allow_non_constant
&& !non_constant_p
)
8029 error_at (cp_expr_loc_or_input_loc (t
),
8030 "immediate evaluation returns address of immediate "
8031 "function %qD", immediate_fndecl
);
8033 non_constant_p
= true;
8037 /* If we saw something bad, go back to our argument. The wrapping below is
8038 only for the cases of TREE_CONSTANT argument or overflow. */
8041 if (!non_constant_p
&& overflow_p
)
8042 non_constant_p
= true;
8044 /* Unshare the result. */
8045 bool should_unshare
= true;
8046 if (r
== t
|| (TREE_CODE (t
) == TARGET_EXPR
8047 && TARGET_EXPR_INITIAL (t
) == r
))
8048 should_unshare
= false;
8050 if (non_constant_p
&& !allow_non_constant
)
8051 return error_mark_node
;
8052 else if (constexpr_dtor
)
8054 else if (non_constant_p
&& TREE_CONSTANT (r
))
8055 r
= mark_non_constant (r
);
8056 else if (non_constant_p
)
8060 r
= unshare_expr (r
);
8062 if (TREE_CODE (r
) == CONSTRUCTOR
&& CLASS_TYPE_P (TREE_TYPE (r
)))
8064 r
= adjust_temp_type (type
, r
);
8065 if (TREE_CODE (t
) == TARGET_EXPR
8066 && TARGET_EXPR_INITIAL (t
) == r
)
8068 else if (TREE_CODE (t
) == CONSTRUCTOR
|| TREE_CODE (t
) == CALL_EXPR
)
8069 /* Don't add a TARGET_EXPR if our argument didn't have one. */;
8070 else if (TREE_CODE (t
) == TARGET_EXPR
&& TARGET_EXPR_CLEANUP (t
))
8071 r
= get_target_expr (r
);
8074 r
= get_target_expr (r
, tf_warning_or_error
| tf_no_cleanup
);
8075 TREE_CONSTANT (r
) = true;
8079 /* Remember the original location if that wouldn't need a wrapper. */
8080 if (location_t loc
= EXPR_LOCATION (t
))
8081 protected_set_expr_location (r
, loc
);
8086 /* If T represents a constant expression returns its reduced value.
8087 Otherwise return error_mark_node. */
8090 cxx_constant_value (tree t
, tree decl
/* = NULL_TREE */,
8091 tsubst_flags_t complain
/* = tf_error */)
8093 bool sfinae
= !(complain
& tf_error
);
8094 tree r
= cxx_eval_outermost_constant_expr (t
, sfinae
, true, true, false, decl
);
8095 if (sfinae
&& !TREE_CONSTANT (r
))
8096 r
= error_mark_node
;
8100 /* Like cxx_constant_value, but used for evaluation of constexpr destructors
8101 of constexpr variables. The actual initializer of DECL is not modified. */
8104 cxx_constant_dtor (tree t
, tree decl
)
8106 cxx_eval_outermost_constant_expr (t
, false, true, true, true, decl
);
8109 /* Helper routine for fold_simple function. Either return simplified
8110 expression T, otherwise NULL_TREE.
8111 In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold
8112 even if we are within template-declaration. So be careful on call, as in
8113 such case types can be undefined. */
8116 fold_simple_1 (tree t
)
8119 enum tree_code code
= TREE_CODE (t
);
8131 return fold_sizeof_expr (t
);
8140 case TRUTH_NOT_EXPR
:
8141 case VIEW_CONVERT_EXPR
:
8144 case FIX_TRUNC_EXPR
:
8145 case FIXED_CONVERT_EXPR
:
8146 case ADDR_SPACE_CONVERT_EXPR
:
8148 op1
= TREE_OPERAND (t
, 0);
8150 t
= const_unop (code
, TREE_TYPE (t
), op1
);
8154 if (CONVERT_EXPR_CODE_P (code
)
8155 && TREE_OVERFLOW_P (t
) && !TREE_OVERFLOW_P (op1
))
8156 TREE_OVERFLOW (t
) = false;
8164 /* If T is a simple constant expression, returns its simplified value.
8165 Otherwise returns T. In contrast to maybe_constant_value we
8166 simplify only few operations on constant-expressions, and we don't
8167 try to simplify constexpressions. */
8170 fold_simple (tree t
)
8172 if (processing_template_decl
)
8175 tree r
= fold_simple_1 (t
);
8182 /* If T is a constant expression, returns its reduced value.
8183 Otherwise, if T does not have TREE_CONSTANT set, returns T.
8184 Otherwise, returns a version of T without TREE_CONSTANT.
8185 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated
8188 static GTY((deletable
)) hash_map
<tree
, tree
> *cv_cache
;
8191 maybe_constant_value (tree t
, tree decl
, bool manifestly_const_eval
)
8195 if (!is_nondependent_constant_expression (t
))
8197 if (TREE_OVERFLOW_P (t
)
8198 || (!processing_template_decl
&& TREE_CONSTANT (t
)))
8199 t
= mark_non_constant (t
);
8202 else if (CONSTANT_CLASS_P (t
))
8203 /* No caching or evaluation needed. */
8206 if (manifestly_const_eval
)
8207 return cxx_eval_outermost_constant_expr (t
, true, true, true, false, decl
);
8209 if (cv_cache
== NULL
)
8210 cv_cache
= hash_map
<tree
, tree
>::create_ggc (101);
8211 if (tree
*cached
= cv_cache
->get (t
))
8216 r
= break_out_target_exprs (r
, /*clear_loc*/true);
8217 protected_set_expr_location (r
, EXPR_LOCATION (t
));
8222 /* Don't evaluate an unevaluated operand. */
8223 if (cp_unevaluated_operand
)
8226 uid_sensitive_constexpr_evaluation_checker c
;
8227 r
= cxx_eval_outermost_constant_expr (t
, true, true, false, false, decl
);
8228 gcc_checking_assert (r
== t
8229 || CONVERT_EXPR_P (t
)
8230 || TREE_CODE (t
) == VIEW_CONVERT_EXPR
8231 || (TREE_CONSTANT (t
) && !TREE_CONSTANT (r
))
8232 || !cp_tree_equal (r
, t
));
8233 if (!c
.evaluation_restricted_p ())
8234 cv_cache
->put (t
, r
);
8238 /* Dispose of the whole CV_CACHE. */
8241 clear_cv_cache (void)
8243 if (cv_cache
!= NULL
)
8247 /* Dispose of the whole CV_CACHE and FOLD_CACHE. */
8250 clear_cv_and_fold_caches ()
8253 clear_fold_cache ();
8256 /* Internal function handling expressions in templates for
8257 fold_non_dependent_expr and fold_non_dependent_init.
8259 If we're in a template, but T isn't value dependent, simplify
8260 it. We're supposed to treat:
8262 template <typename T> void f(T[1 + 1]);
8263 template <typename T> void f(T[2]);
8265 as two declarations of the same function, for example. */
8268 fold_non_dependent_expr_template (tree t
, tsubst_flags_t complain
,
8269 bool manifestly_const_eval
,
8272 gcc_assert (processing_template_decl
);
8274 if (is_nondependent_constant_expression (t
))
8276 processing_template_decl_sentinel s
;
8277 t
= instantiate_non_dependent_expr_internal (t
, complain
);
8279 if (type_unknown_p (t
) || BRACE_ENCLOSED_INITIALIZER_P (t
))
8281 if (TREE_OVERFLOW_P (t
))
8283 t
= build_nop (TREE_TYPE (t
), t
);
8284 TREE_CONSTANT (t
) = false;
8289 if (cp_unevaluated_operand
&& !manifestly_const_eval
)
8292 tree r
= cxx_eval_outermost_constant_expr (t
, true, true,
8293 manifestly_const_eval
,
8295 /* cp_tree_equal looks through NOPs, so allow them. */
8296 gcc_checking_assert (r
== t
8297 || CONVERT_EXPR_P (t
)
8298 || TREE_CODE (t
) == VIEW_CONVERT_EXPR
8299 || (TREE_CONSTANT (t
) && !TREE_CONSTANT (r
))
8300 || !cp_tree_equal (r
, t
));
8303 else if (TREE_OVERFLOW_P (t
))
8305 t
= build_nop (TREE_TYPE (t
), t
);
8306 TREE_CONSTANT (t
) = false;
8312 /* Like maybe_constant_value but first fully instantiate the argument.
8314 Note: this is equivalent to instantiate_non_dependent_expr (t, complain)
8315 followed by maybe_constant_value but is more efficient,
8316 because it calls instantiation_dependent_expression_p and
8317 potential_constant_expression at most once.
8318 The manifestly_const_eval argument is passed to maybe_constant_value.
8320 Callers should generally pass their active complain, or if they are in a
8321 non-template, diagnosing context, they can use the default of
8322 tf_warning_or_error. Callers that might be within a template context, don't
8323 have a complain parameter, and aren't going to remember the result for long
8324 (e.g. null_ptr_cst_p), can pass tf_none and deal with error_mark_node
8328 fold_non_dependent_expr (tree t
,
8329 tsubst_flags_t complain
/* = tf_warning_or_error */,
8330 bool manifestly_const_eval
/* = false */,
8331 tree object
/* = NULL_TREE */)
8336 if (processing_template_decl
)
8337 return fold_non_dependent_expr_template (t
, complain
,
8338 manifestly_const_eval
, object
);
8340 return maybe_constant_value (t
, object
, manifestly_const_eval
);
8343 /* Like fold_non_dependent_expr, but if EXPR couldn't be folded to a constant,
8344 return the original expression. */
8347 maybe_fold_non_dependent_expr (tree expr
,
8348 tsubst_flags_t complain
/*=tf_warning_or_error*/)
8350 tree t
= fold_non_dependent_expr (expr
, complain
);
8351 if (t
&& TREE_CONSTANT (t
))
8357 /* Like maybe_constant_init but first fully instantiate the argument. */
8360 fold_non_dependent_init (tree t
,
8361 tsubst_flags_t complain
/*=tf_warning_or_error*/,
8362 bool manifestly_const_eval
/*=false*/,
8363 tree object
/* = NULL_TREE */)
8368 if (processing_template_decl
)
8370 t
= fold_non_dependent_expr_template (t
, complain
,
8371 manifestly_const_eval
, object
);
8372 /* maybe_constant_init does this stripping, so do it here too. */
8373 if (TREE_CODE (t
) == TARGET_EXPR
)
8375 tree init
= TARGET_EXPR_INITIAL (t
);
8376 if (TREE_CODE (init
) == CONSTRUCTOR
)
8382 return maybe_constant_init (t
, object
, manifestly_const_eval
);
8385 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
8386 than wrapped in a TARGET_EXPR.
8387 ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
8388 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
8389 per P0595 even when ALLOW_NON_CONSTANT is true. */
8392 maybe_constant_init_1 (tree t
, tree decl
, bool allow_non_constant
,
8393 bool manifestly_const_eval
)
8397 if (TREE_CODE (t
) == EXPR_STMT
)
8398 t
= TREE_OPERAND (t
, 0);
8399 if (TREE_CODE (t
) == CONVERT_EXPR
8400 && VOID_TYPE_P (TREE_TYPE (t
)))
8401 t
= TREE_OPERAND (t
, 0);
8402 if (TREE_CODE (t
) == INIT_EXPR
)
8403 t
= TREE_OPERAND (t
, 1);
8404 if (TREE_CODE (t
) == TARGET_EXPR
)
8405 t
= TARGET_EXPR_INITIAL (t
);
8406 if (!is_nondependent_static_init_expression (t
))
8407 /* Don't try to evaluate it. */;
8408 else if (CONSTANT_CLASS_P (t
) && allow_non_constant
)
8409 /* No evaluation needed. */;
8412 /* [basic.start.static] allows constant-initialization of variables with
8413 static or thread storage duration even if it isn't required, but we
8414 shouldn't bend the rules the same way for automatic variables. */
8415 bool is_static
= (decl
&& DECL_P (decl
)
8416 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)));
8417 t
= cxx_eval_outermost_constant_expr (t
, allow_non_constant
, !is_static
,
8418 manifestly_const_eval
, false, decl
);
8420 if (TREE_CODE (t
) == TARGET_EXPR
)
8422 tree init
= TARGET_EXPR_INITIAL (t
);
8423 if (TREE_CODE (init
) == CONSTRUCTOR
)
8429 /* Wrapper for maybe_constant_init_1 which permits non constants. */
8432 maybe_constant_init (tree t
, tree decl
, bool manifestly_const_eval
)
8434 return maybe_constant_init_1 (t
, decl
, true, manifestly_const_eval
);
8437 /* Wrapper for maybe_constant_init_1 which does not permit non constants. */
8440 cxx_constant_init (tree t
, tree decl
)
8442 return maybe_constant_init_1 (t
, decl
, false, true);
8446 /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
8447 /* Return true if the object referred to by REF has automatic or thread
8450 enum { ck_ok
, ck_bad
, ck_unknown
};
8452 check_automatic_or_tls (tree ref
)
8455 poly_int64 bitsize
, bitpos
;
8457 int volatilep
= 0, unsignedp
= 0;
8458 tree decl
= get_inner_reference (ref
, &bitsize
, &bitpos
, &offset
,
8459 &mode
, &unsignedp
, &volatilep
, false);
8462 /* If there isn't a decl in the middle, we don't know the linkage here,
8463 and this isn't a constant expression anyway. */
8466 dk
= decl_storage_duration (decl
);
8467 return (dk
== dk_auto
|| dk
== dk_thread
) ? ck_bad
: ck_ok
;
8471 /* Data structure for passing data from potential_constant_expression_1
8472 to check_for_return_continue via cp_walk_tree. */
8473 struct check_for_return_continue_data
{
8474 hash_set
<tree
> *pset
;
8479 /* Helper function for potential_constant_expression_1 SWITCH_STMT handling,
8480 called through cp_walk_tree. Return the first RETURN_EXPR found, or note
8481 the first CONTINUE_STMT and/or BREAK_STMT if RETURN_EXPR is not found. */
8483 check_for_return_continue (tree
*tp
, int *walk_subtrees
, void *data
)
8486 check_for_return_continue_data
*d
= (check_for_return_continue_data
*) data
;
8487 switch (TREE_CODE (t
))
8493 if (d
->continue_stmt
== NULL_TREE
)
8494 d
->continue_stmt
= t
;
8498 if (d
->break_stmt
== NULL_TREE
)
8503 if (tree r = cp_walk_tree (&x, check_for_return_continue, data, \
8507 /* For loops, walk subtrees manually, so that continue stmts found
8508 inside of the bodies of the loops are ignored. */
8511 RECUR (DO_COND (t
));
8512 s
= d
->continue_stmt
;
8514 RECUR (DO_BODY (t
));
8515 d
->continue_stmt
= s
;
8521 RECUR (WHILE_COND (t
));
8522 s
= d
->continue_stmt
;
8524 RECUR (WHILE_BODY (t
));
8525 d
->continue_stmt
= s
;
8531 RECUR (FOR_INIT_STMT (t
));
8532 RECUR (FOR_COND (t
));
8533 RECUR (FOR_EXPR (t
));
8534 s
= d
->continue_stmt
;
8536 RECUR (FOR_BODY (t
));
8537 d
->continue_stmt
= s
;
8541 case RANGE_FOR_STMT
:
8543 RECUR (RANGE_FOR_EXPR (t
));
8544 s
= d
->continue_stmt
;
8546 RECUR (RANGE_FOR_BODY (t
));
8547 d
->continue_stmt
= s
;
8553 RECUR (SWITCH_STMT_COND (t
));
8555 RECUR (SWITCH_STMT_BODY (t
));
8560 case STATEMENT_LIST
:
8573 /* Return true if T denotes a potentially constant expression. Issue
8574 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
8575 an lvalue-rvalue conversion is implied. If NOW is true, we want to
8576 consider the expression in the current context, independent of constexpr
8579 C++0x [expr.const] used to say
8581 6 An expression is a potential constant expression if it is
8582 a constant expression where all occurrences of function
8583 parameters are replaced by arbitrary constant expressions
8584 of the appropriate type.
8586 2 A conditional expression is a constant expression unless it
8587 involves one of the following as a potentially evaluated
8588 subexpression (3.2), but subexpressions of logical AND (5.14),
8589 logical OR (5.15), and conditional (5.16) operations that are
8590 not evaluated are not considered. */
8593 potential_constant_expression_1 (tree t
, bool want_rval
, bool strict
, bool now
,
8594 tsubst_flags_t flags
, tree
*jump_target
)
8596 #define RECUR(T,RV) \
8597 potential_constant_expression_1 ((T), (RV), strict, now, flags, jump_target)
8599 enum { any
= false, rval
= true };
8603 if (t
== error_mark_node
)
8607 location_t loc
= cp_expr_loc_or_input_loc (t
);
8610 /* If we are jumping, ignore everything. This is simpler than the
8611 cxx_eval_constant_expression handling because we only need to be
8612 conservatively correct, and we don't necessarily have a constant value
8613 available, so we don't bother with switch tracking. */
8616 if (TREE_THIS_VOLATILE (t
) && want_rval
)
8618 if (flags
& tf_error
)
8619 error_at (loc
, "lvalue-to-rvalue conversion of a volatile lvalue "
8620 "%qE with type %qT", t
, TREE_TYPE (t
));
8623 if (CONSTANT_CLASS_P (t
))
8625 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_TYPED
)
8626 && TREE_TYPE (t
) == error_mark_node
)
8629 switch (TREE_CODE (t
))
8635 case TEMPLATE_ID_EXPR
:
8637 case CASE_LABEL_EXPR
:
8644 case TEMPLATE_PARM_INDEX
:
8646 case IDENTIFIER_NODE
:
8647 case USERDEF_LITERAL
:
8648 /* We can see a FIELD_DECL in a pointer-to-member expression. */
8653 case PLACEHOLDER_EXPR
:
8656 case DEBUG_BEGIN_STMT
:
8660 if (!RECUR (TREE_OPERAND (t
, 0), any
))
8670 if (now
&& want_rval
)
8672 tree type
= TREE_TYPE (t
);
8673 if ((processing_template_decl
&& !COMPLETE_TYPE_P (type
))
8674 || dependent_type_p (type
)
8675 || is_really_empty_class (type
, /*ignore_vptr*/false))
8676 /* An empty class has no data to read. */
8678 if (flags
& tf_error
)
8679 error ("%qE is not a constant expression", t
);
8684 case AGGR_INIT_EXPR
:
8686 /* -- an invocation of a function other than a constexpr function
8687 or a constexpr constructor. */
8689 tree fun
= get_function_named_in_call (t
);
8690 const int nargs
= call_expr_nargs (t
);
8693 if (fun
== NULL_TREE
)
8695 /* Reset to allow the function to continue past the end
8696 of the block below. Otherwise return early. */
8699 if (TREE_CODE (t
) == CALL_EXPR
8700 && CALL_EXPR_FN (t
) == NULL_TREE
)
8701 switch (CALL_EXPR_IFN (t
))
8703 /* These should be ignored, they are optimized away from
8704 constexpr functions. */
8705 case IFN_UBSAN_NULL
:
8706 case IFN_UBSAN_BOUNDS
:
8707 case IFN_UBSAN_VPTR
:
8708 case IFN_FALLTHROUGH
:
8711 case IFN_ADD_OVERFLOW
:
8712 case IFN_SUB_OVERFLOW
:
8713 case IFN_MUL_OVERFLOW
:
8715 case IFN_VEC_CONVERT
:
8725 /* fold_call_expr can't do anything with IFN calls. */
8726 if (flags
& tf_error
)
8727 error_at (loc
, "call to internal function %qE", t
);
8732 if (fun
&& is_overloaded_fn (fun
))
8734 if (TREE_CODE (fun
) == FUNCTION_DECL
)
8736 if (builtin_valid_in_constant_expr_p (fun
))
8738 if (!maybe_constexpr_fn (fun
)
8739 /* Allow any built-in function; if the expansion
8740 isn't constant, we'll deal with that then. */
8741 && !fndecl_built_in_p (fun
)
8742 /* In C++20, replaceable global allocation functions
8743 are constant expressions. */
8744 && (!cxx_replaceable_global_alloc_fn (fun
)
8745 || TREE_CODE (t
) != CALL_EXPR
8746 || (!CALL_FROM_NEW_OR_DELETE_P (t
)
8747 && (current_function_decl
== NULL_TREE
8748 || !is_std_allocator_allocate
8749 (current_function_decl
))))
8750 /* Allow placement new in std::construct_at. */
8751 && (!cxx_placement_new_fn (fun
)
8752 || TREE_CODE (t
) != CALL_EXPR
8753 || current_function_decl
== NULL_TREE
8754 || !is_std_construct_at (current_function_decl
))
8755 && !cxx_dynamic_cast_fn_p (fun
))
8757 if (flags
& tf_error
)
8759 error_at (loc
, "call to non-%<constexpr%> function %qD",
8761 explain_invalid_constexpr_fn (fun
);
8765 /* A call to a non-static member function takes the address
8766 of the object as the first argument. But in a constant
8767 expression the address will be folded away, so look
8769 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun
)
8770 && !DECL_CONSTRUCTOR_P (fun
))
8772 tree x
= get_nth_callarg (t
, 0);
8773 if (is_this_parameter (x
))
8775 /* Don't require an immediately constant value, as
8776 constexpr substitution might not use the value. */
8777 bool sub_now
= false;
8778 if (!potential_constant_expression_1 (x
, rval
, strict
,
8787 if (!RECUR (fun
, true))
8789 fun
= get_first_fn (fun
);
8791 /* Skip initial arguments to base constructors. */
8792 if (DECL_BASE_CONSTRUCTOR_P (fun
))
8793 i
= num_artificial_parms_for (fun
);
8794 fun
= DECL_ORIGIN (fun
);
8798 if (RECUR (fun
, rval
))
8799 /* Might end up being a constant function pointer. */;
8803 for (; i
< nargs
; ++i
)
8805 tree x
= get_nth_callarg (t
, i
);
8806 /* In a template, reference arguments haven't been converted to
8807 REFERENCE_TYPE and we might not even know if the parameter
8808 is a reference, so accept lvalue constants too. */
8809 bool rv
= processing_template_decl
? any
: rval
;
8810 /* Don't require an immediately constant value, as constexpr
8811 substitution might not use the value of the argument. */
8812 bool sub_now
= false;
8813 if (!potential_constant_expression_1 (x
, rv
, strict
,
8814 sub_now
, flags
, jump_target
))
8820 case NON_LVALUE_EXPR
:
8821 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
8822 -- an lvalue of integral type that refers to a non-volatile
8823 const variable or static data member initialized with
8824 constant expressions, or
8826 -- an lvalue of literal type that refers to non-volatile
8827 object defined with constexpr, or that refers to a
8828 sub-object of such an object; */
8829 return RECUR (TREE_OPERAND (t
, 0), rval
);
8832 if (DECL_HAS_VALUE_EXPR_P (t
))
8834 if (now
&& is_normal_capture_proxy (t
))
8836 /* -- in a lambda-expression, a reference to this or to a
8837 variable with automatic storage duration defined outside that
8838 lambda-expression, where the reference would be an
8842 /* Since we're doing an lvalue-rvalue conversion, this might
8843 not be an odr-use, so evaluate the variable directly. */
8844 return RECUR (DECL_CAPTURED_VARIABLE (t
), rval
);
8846 if (flags
& tf_error
)
8848 tree cap
= DECL_CAPTURED_VARIABLE (t
);
8849 error ("lambda capture of %qE is not a constant expression",
8851 if (decl_constant_var_p (cap
))
8852 inform (input_location
, "because it is used as a glvalue");
8856 /* Treat __PRETTY_FUNCTION__ inside a template function as
8857 potentially-constant. */
8858 else if (DECL_PRETTY_FUNCTION_P (t
)
8859 && DECL_VALUE_EXPR (t
) == error_mark_node
)
8861 return RECUR (DECL_VALUE_EXPR (t
), rval
);
8864 && !var_in_maybe_constexpr_fn (t
)
8865 && !type_dependent_expression_p (t
)
8866 && !decl_maybe_constant_var_p (t
)
8868 || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t
))
8869 || (DECL_INITIAL (t
)
8870 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t
)))
8871 && COMPLETE_TYPE_P (TREE_TYPE (t
))
8872 && !is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
8874 if (flags
& tf_error
)
8875 non_const_var_error (loc
, t
);
8881 if (REINTERPRET_CAST_P (t
))
8883 if (flags
& tf_error
)
8884 error_at (loc
, "%<reinterpret_cast%> is not a constant expression");
8889 case VIEW_CONVERT_EXPR
:
8890 /* -- a reinterpret_cast. FIXME not implemented, and this rule
8891 may change to something more specific to type-punning (DR 1312). */
8893 tree from
= TREE_OPERAND (t
, 0);
8894 if (location_wrapper_p (t
))
8896 iloc_sentinel ils
= loc
;
8897 return (RECUR (from
, want_rval
));
8899 if (INDIRECT_TYPE_P (TREE_TYPE (t
)))
8901 STRIP_ANY_LOCATION_WRAPPER (from
);
8902 if (TREE_CODE (from
) == INTEGER_CST
8903 && !integer_zerop (from
))
8905 if (flags
& tf_error
)
8907 "%<reinterpret_cast%> from integer to pointer");
8911 return (RECUR (from
, TREE_CODE (t
) != VIEW_CONVERT_EXPR
));
8914 case ADDRESSOF_EXPR
:
8915 /* This is like ADDR_EXPR, except it won't form pointer-to-member. */
8916 t
= TREE_OPERAND (t
, 0);
8917 goto handle_addr_expr
;
8920 /* -- a unary operator & that is applied to an lvalue that
8921 designates an object with thread or automatic storage
8923 t
= TREE_OPERAND (t
, 0);
8925 if (TREE_CODE (t
) == OFFSET_REF
&& PTRMEM_OK_P (t
))
8926 /* A pointer-to-member constant. */
8931 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
8932 any checking here, as we might dereference the pointer later. If
8933 we remove this code, also remove check_automatic_or_tls. */
8934 i
= check_automatic_or_tls (t
);
8939 if (flags
& tf_error
)
8940 error ("address-of an object %qE with thread local or "
8941 "automatic storage is not a constant expression", t
);
8945 return RECUR (t
, any
);
8950 /* -- a class member access unless its postfix-expression is
8951 of literal type or of pointer to literal type. */
8952 /* This test would be redundant, as it follows from the
8953 postfix-expression being a potential constant expression. */
8954 if (type_unknown_p (t
))
8956 if (is_overloaded_fn (t
))
8957 /* In a template, a COMPONENT_REF of a function expresses ob.fn(),
8958 which uses ob as an lvalue. */
8965 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
8967 case EXPR_PACK_EXPANSION
:
8968 return RECUR (PACK_EXPANSION_PATTERN (t
), want_rval
);
8972 tree x
= TREE_OPERAND (t
, 0);
8974 if (is_this_parameter (x
) && !is_capture_proxy (x
))
8976 if (!var_in_maybe_constexpr_fn (x
))
8978 if (flags
& tf_error
)
8979 error_at (loc
, "use of %<this%> in a constant expression");
8984 return RECUR (x
, rval
);
8987 case STATEMENT_LIST
:
8988 for (tree stmt
: tsi_range (t
))
8989 if (!RECUR (stmt
, any
))
8994 if (cxx_dialect
< cxx14
)
8996 if (!RECUR (TREE_OPERAND (t
, 0), any
))
8998 /* Just ignore clobbers. */
8999 if (TREE_CLOBBER_P (TREE_OPERAND (t
, 1)))
9001 if (!RECUR (TREE_OPERAND (t
, 1), rval
))
9006 if (cxx_dialect
< cxx14
)
9008 if (!RECUR (TREE_OPERAND (t
, 0), rval
))
9010 if (!RECUR (TREE_OPERAND (t
, 2), rval
))
9015 if (!RECUR (DO_COND (t
), rval
))
9017 if (!RECUR (DO_BODY (t
), any
))
9019 if (breaks (jump_target
) || continues (jump_target
))
9020 *jump_target
= NULL_TREE
;
9024 if (!RECUR (FOR_INIT_STMT (t
), any
))
9027 if (!RECUR (tmp
, rval
))
9031 if (!processing_template_decl
)
9032 tmp
= cxx_eval_outermost_constant_expr (tmp
, true);
9033 /* If we couldn't evaluate the condition, it might not ever be
9035 if (!integer_onep (tmp
))
9037 /* Before returning true, check if the for body can contain
9039 hash_set
<tree
> pset
;
9040 check_for_return_continue_data data
= { &pset
, NULL_TREE
,
9043 = cp_walk_tree (&FOR_BODY (t
), check_for_return_continue
,
9045 *jump_target
= ret_expr
;
9049 if (!RECUR (FOR_EXPR (t
), any
))
9051 if (!RECUR (FOR_BODY (t
), any
))
9053 if (breaks (jump_target
) || continues (jump_target
))
9054 *jump_target
= NULL_TREE
;
9057 case RANGE_FOR_STMT
:
9058 if (!RECUR (RANGE_FOR_INIT_STMT (t
), any
))
9060 if (!RECUR (RANGE_FOR_EXPR (t
), any
))
9062 if (!RECUR (RANGE_FOR_BODY (t
), any
))
9064 if (breaks (jump_target
) || continues (jump_target
))
9065 *jump_target
= NULL_TREE
;
9069 tmp
= WHILE_COND (t
);
9070 if (!RECUR (tmp
, rval
))
9072 if (!processing_template_decl
)
9073 tmp
= cxx_eval_outermost_constant_expr (tmp
, true);
9074 /* If we couldn't evaluate the condition, it might not ever be true. */
9075 if (!integer_onep (tmp
))
9077 /* Before returning true, check if the while body can contain
9079 hash_set
<tree
> pset
;
9080 check_for_return_continue_data data
= { &pset
, NULL_TREE
,
9083 = cp_walk_tree (&WHILE_BODY (t
), check_for_return_continue
,
9085 *jump_target
= ret_expr
;
9088 if (!RECUR (WHILE_BODY (t
), any
))
9090 if (breaks (jump_target
) || continues (jump_target
))
9091 *jump_target
= NULL_TREE
;
9095 if (!RECUR (SWITCH_STMT_COND (t
), rval
))
9097 /* FIXME we don't check SWITCH_STMT_BODY currently, because even
9098 unreachable labels would be checked and it is enough if there is
9099 a single switch cond value for which it is a valid constant
9100 expression. We need to check if there are any RETURN_EXPRs
9101 or CONTINUE_STMTs inside of the body though, as in that case
9102 we need to set *jump_target. */
9105 hash_set
<tree
> pset
;
9106 check_for_return_continue_data data
= { &pset
, NULL_TREE
,
9109 = cp_walk_tree (&SWITCH_STMT_BODY (t
), check_for_return_continue
,
9111 /* The switch might return. */
9112 *jump_target
= ret_expr
;
9113 else if (data
.continue_stmt
)
9114 /* The switch can't return, but might continue. */
9115 *jump_target
= data
.continue_stmt
;
9120 return RECUR (STMT_EXPR_STMT (t
), rval
);
9123 if (cxx_dialect
>= cxx17
)
9124 /* In C++17 lambdas can be constexpr, don't give up yet. */
9126 else if (flags
& tf_error
)
9127 error_at (loc
, "lambda-expression is not a constant expression "
9134 case VEC_DELETE_EXPR
:
9135 if (cxx_dialect
>= cxx20
)
9136 /* In C++20, new-expressions are potentially constant. */
9138 else if (flags
& tf_error
)
9139 error_at (loc
, "new-expression is not a constant expression "
9143 case DYNAMIC_CAST_EXPR
:
9144 case PSEUDO_DTOR_EXPR
:
9150 case OMP_DISTRIBUTE
:
9154 case OMP_TARGET_DATA
:
9164 case OMP_TARGET_UPDATE
:
9165 case OMP_TARGET_ENTER_DATA
:
9166 case OMP_TARGET_EXIT_DATA
:
9168 case OMP_ATOMIC_READ
:
9169 case OMP_ATOMIC_CAPTURE_OLD
:
9170 case OMP_ATOMIC_CAPTURE_NEW
:
9176 case OACC_HOST_DATA
:
9180 case OACC_ENTER_DATA
:
9181 case OACC_EXIT_DATA
:
9183 /* GCC internal stuff. */
9185 case TRANSACTION_EXPR
:
9186 case AT_ENCODE_EXPR
:
9188 if (flags
& tf_error
)
9189 error_at (loc
, "expression %qE is not a constant expression", t
);
9193 if (flags
& tf_error
)
9194 inline_asm_in_constexpr_error (loc
);
9198 if (cxx_dialect
>= cxx20
)
9199 /* In C++20 virtual calls can be constexpr, don't give up yet. */
9201 else if (flags
& tf_error
)
9203 "virtual functions cannot be %<constexpr%> before C++20");
9207 /* In C++20, a typeid expression whose operand is of polymorphic
9208 class type can be constexpr. */
9210 tree e
= TREE_OPERAND (t
, 0);
9211 if (cxx_dialect
< cxx20
9214 && !type_dependent_expression_p (e
)
9215 && TYPE_POLYMORPHIC_P (TREE_TYPE (e
)))
9217 if (flags
& tf_error
)
9218 error_at (loc
, "%<typeid%> is not a constant expression "
9219 "because %qE is of polymorphic type", e
);
9225 case POINTER_DIFF_EXPR
:
9236 case SPACESHIP_EXPR
:
9240 case PREINCREMENT_EXPR
:
9241 case POSTINCREMENT_EXPR
:
9242 case PREDECREMENT_EXPR
:
9243 case POSTDECREMENT_EXPR
:
9244 if (cxx_dialect
< cxx14
)
9250 if (TYPE_P (TREE_OPERAND (t
, 0)))
9256 case FIX_TRUNC_EXPR
:
9261 case TRUTH_NOT_EXPR
:
9262 case FIXED_CONVERT_EXPR
:
9263 case UNARY_PLUS_EXPR
:
9264 case UNARY_LEFT_FOLD_EXPR
:
9265 case UNARY_RIGHT_FOLD_EXPR
:
9267 return RECUR (TREE_OPERAND (t
, 0), rval
);
9270 case CONST_CAST_EXPR
:
9271 case STATIC_CAST_EXPR
:
9272 case REINTERPRET_CAST_EXPR
:
9273 case IMPLICIT_CONV_EXPR
:
9274 if (cxx_dialect
< cxx11
9275 && !dependent_type_p (TREE_TYPE (t
))
9276 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
)))
9277 /* In C++98, a conversion to non-integral type can't be part of a
9278 constant expression. */
9280 if (flags
& tf_error
)
9282 "cast to non-integral type %qT in a constant expression",
9286 /* This might be a conversion from a class to a (potentially) literal
9287 type. Let's consider it potentially constant since the conversion
9288 might be a constexpr user-defined conversion. */
9289 else if (cxx_dialect
>= cxx11
9290 && (dependent_type_p (TREE_TYPE (t
))
9291 || !COMPLETE_TYPE_P (TREE_TYPE (t
))
9292 || literal_type_p (TREE_TYPE (t
)))
9293 && TREE_OPERAND (t
, 0))
9295 tree type
= TREE_TYPE (TREE_OPERAND (t
, 0));
9296 /* If this is a dependent type, it could end up being a class
9297 with conversions. */
9298 if (type
== NULL_TREE
|| WILDCARD_TYPE_P (type
))
9300 /* Or a non-dependent class which has conversions. */
9301 else if (CLASS_TYPE_P (type
)
9302 && (TYPE_HAS_CONVERSION (type
) || dependent_scope_p (type
)))
9306 return (RECUR (TREE_OPERAND (t
, 0),
9307 !TYPE_REF_P (TREE_TYPE (t
))));
9310 return RECUR (BIND_EXPR_BODY (t
), want_rval
);
9312 case NON_DEPENDENT_EXPR
:
9313 /* Treat NON_DEPENDENT_EXPR as non-constant: it's not handled by
9314 constexpr evaluation or tsubst, so fold_non_dependent_expr can't
9315 do anything useful with it. And we shouldn't see it in a context
9316 where a constant expression is strictly required, hence the assert. */
9317 gcc_checking_assert (!(flags
& tf_error
));
9320 case CLEANUP_POINT_EXPR
:
9321 case MUST_NOT_THROW_EXPR
:
9322 case TRY_CATCH_EXPR
:
9327 /* For convenience. */
9330 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
9333 tmp
= DECL_EXPR_DECL (t
);
9334 if (VAR_P (tmp
) && !DECL_ARTIFICIAL (tmp
))
9336 if (CP_DECL_THREAD_LOCAL_P (tmp
) && !DECL_REALLY_EXTERN (tmp
))
9338 if (flags
& tf_error
)
9339 error_at (DECL_SOURCE_LOCATION (tmp
), "%qD defined "
9340 "%<thread_local%> in %<constexpr%> context", tmp
);
9343 else if (TREE_STATIC (tmp
))
9345 if (flags
& tf_error
)
9346 error_at (DECL_SOURCE_LOCATION (tmp
), "%qD defined "
9347 "%<static%> in %<constexpr%> context", tmp
);
9350 else if (!check_for_uninitialized_const_var
9351 (tmp
, /*constexpr_context_p=*/true, flags
))
9354 return RECUR (DECL_INITIAL (tmp
), want_rval
);
9356 case TRY_FINALLY_EXPR
:
9357 return (RECUR (TREE_OPERAND (t
, 0), want_rval
)
9358 && RECUR (TREE_OPERAND (t
, 1), any
));
9361 return RECUR (TREE_OPERAND (t
, 1), want_rval
);
9364 if (!TARGET_EXPR_DIRECT_INIT_P (t
)
9365 && !literal_type_p (TREE_TYPE (t
)))
9367 if (flags
& tf_error
)
9369 auto_diagnostic_group d
;
9370 error_at (loc
, "temporary of non-literal type %qT in a "
9371 "constant expression", TREE_TYPE (t
));
9372 explain_non_literal_class (TREE_TYPE (t
));
9378 return RECUR (TREE_OPERAND (t
, 1), rval
);
9382 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (t
);
9383 constructor_elt
*ce
;
9384 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
9385 if (!RECUR (ce
->value
, want_rval
))
9392 gcc_assert (TREE_PURPOSE (t
) == NULL_TREE
9393 || DECL_P (TREE_PURPOSE (t
)));
9394 if (!RECUR (TREE_VALUE (t
), want_rval
))
9396 if (TREE_CHAIN (t
) == NULL_TREE
)
9398 return RECUR (TREE_CHAIN (t
), want_rval
);
9401 case TRUNC_DIV_EXPR
:
9403 case FLOOR_DIV_EXPR
:
9404 case ROUND_DIV_EXPR
:
9405 case TRUNC_MOD_EXPR
:
9407 case ROUND_MOD_EXPR
:
9409 tree denom
= TREE_OPERAND (t
, 1);
9410 if (!RECUR (denom
, rval
))
9412 /* We can't call cxx_eval_outermost_constant_expr on an expression
9413 that hasn't been through instantiate_non_dependent_expr yet. */
9414 if (!processing_template_decl
)
9415 denom
= cxx_eval_outermost_constant_expr (denom
, true);
9416 if (integer_zerop (denom
))
9418 if (flags
& tf_error
)
9419 error ("division by zero is not a constant expression");
9425 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
9431 /* check_return_expr sometimes wraps a TARGET_EXPR in a
9432 COMPOUND_EXPR; don't get confused. */
9433 tree op0
= TREE_OPERAND (t
, 0);
9434 tree op1
= TREE_OPERAND (t
, 1);
9436 if (TREE_CODE (op0
) == TARGET_EXPR
&& op1
== TARGET_EXPR_SLOT (op0
))
9437 return RECUR (op0
, want_rval
);
9442 /* If the first operand is the non-short-circuit constant, look at
9443 the second operand; otherwise we only care about the first one for
9445 case TRUTH_AND_EXPR
:
9446 case TRUTH_ANDIF_EXPR
:
9447 tmp
= boolean_true_node
;
9450 case TRUTH_ORIF_EXPR
:
9451 tmp
= boolean_false_node
;
9454 tree op0
= TREE_OPERAND (t
, 0);
9455 tree op1
= TREE_OPERAND (t
, 1);
9456 if (!RECUR (op0
, rval
))
9458 if (!(flags
& tf_error
) && RECUR (op1
, rval
))
9459 /* When quiet, try to avoid expensive trial evaluation by first
9460 checking potentiality of the second operand. */
9462 if (!processing_template_decl
)
9463 op0
= cxx_eval_outermost_constant_expr (op0
, true);
9464 if (tree_int_cst_equal (op0
, tmp
))
9465 return (flags
& tf_error
) ? RECUR (op1
, rval
) : false;
9472 case POINTER_PLUS_EXPR
:
9474 case EXACT_DIV_EXPR
:
9484 case TRUTH_XOR_EXPR
:
9485 case UNORDERED_EXPR
:
9498 case ARRAY_RANGE_REF
:
9502 case BINARY_LEFT_FOLD_EXPR
:
9503 case BINARY_RIGHT_FOLD_EXPR
:
9505 for (i
= 0; i
< 2; ++i
)
9506 if (!RECUR (TREE_OPERAND (t
, i
), want_rval
))
9511 for (i
= 0; i
< 3; ++i
)
9512 if (!RECUR (TREE_OPERAND (t
, i
), true))
9517 if (COND_EXPR_IS_VEC_DELETE (t
) && cxx_dialect
< cxx20
)
9519 if (flags
& tf_error
)
9520 error_at (loc
, "%<delete[]%> is not a constant expression");
9526 /* If the condition is a known constant, we know which of the legs we
9527 care about; otherwise we only require that the condition and
9528 either of the legs be potentially constant. */
9529 tmp
= TREE_OPERAND (t
, 0);
9530 if (!RECUR (tmp
, rval
))
9532 if (!processing_template_decl
)
9533 tmp
= cxx_eval_outermost_constant_expr (tmp
, true);
9534 /* potential_constant_expression* isn't told if it is called for
9535 manifestly_const_eval or not, so for consteval if always
9536 process both branches as if the condition is not a known
9538 if (TREE_CODE (t
) != IF_STMT
|| !IF_STMT_CONSTEVAL_P (t
))
9540 if (integer_zerop (tmp
))
9541 return RECUR (TREE_OPERAND (t
, 2), want_rval
);
9542 else if (TREE_CODE (tmp
) == INTEGER_CST
)
9543 return RECUR (TREE_OPERAND (t
, 1), want_rval
);
9546 for (i
= 1; i
< 3; ++i
)
9548 tree this_jump_target
= tmp
;
9549 if (potential_constant_expression_1 (TREE_OPERAND (t
, i
),
9550 want_rval
, strict
, now
,
9551 tf_none
, &this_jump_target
))
9553 if (returns (&this_jump_target
))
9554 *jump_target
= this_jump_target
;
9555 else if (!returns (jump_target
))
9557 if (breaks (&this_jump_target
)
9558 || continues (&this_jump_target
))
9559 *jump_target
= this_jump_target
;
9562 /* If the then branch is potentially constant, but
9563 does not return, check if the else branch
9564 couldn't return, break or continue. */
9565 hash_set
<tree
> pset
;
9566 check_for_return_continue_data data
= { &pset
, NULL_TREE
,
9569 = cp_walk_tree (&TREE_OPERAND (t
, 2),
9570 check_for_return_continue
, &data
,
9572 *jump_target
= ret_expr
;
9573 else if (*jump_target
== NULL_TREE
)
9575 if (data
.continue_stmt
)
9576 *jump_target
= data
.continue_stmt
;
9577 else if (data
.break_stmt
)
9578 *jump_target
= data
.break_stmt
;
9585 if (flags
& tf_error
)
9587 if (TREE_CODE (t
) == IF_STMT
)
9588 error_at (loc
, "neither branch of %<if%> is a constant expression");
9590 error_at (loc
, "expression %qE is not a constant expression", t
);
9595 if (VEC_INIT_EXPR_IS_CONSTEXPR (t
))
9597 if (flags
& tf_error
)
9599 error_at (loc
, "non-constant array initialization");
9600 diagnose_non_constexpr_vec_init (t
);
9606 /* We can see these in statement-expressions. */
9610 if (!RECUR (CLEANUP_BODY (t
), any
))
9612 if (!CLEANUP_EH_ONLY (t
) && !RECUR (CLEANUP_EXPR (t
), any
))
9616 case EMPTY_CLASS_EXPR
:
9621 tree
*target
= &TREE_OPERAND (t
, 0);
9622 /* Gotos representing break, continue and cdtor return are OK. */
9623 if (breaks (target
) || continues (target
) || returns (target
))
9625 *jump_target
= *target
;
9628 if (flags
& tf_error
)
9629 error_at (loc
, "%<goto%> is not a constant expression");
9634 t
= LABEL_EXPR_LABEL (t
);
9635 if (DECL_ARTIFICIAL (t
) || cxx_dialect
>= cxx23
)
9637 else if (flags
& tf_error
)
9638 error_at (loc
, "label definition in %<constexpr%> function only "
9639 "available with %<-std=c++2b%> or %<-std=gnu++2b%>");
9643 return RECUR (TREE_OPERAND (t
, 0), rval
);
9646 return RECUR (TREE_OPERAND (t
, 0), rval
);
9648 /* Coroutine await, yield and return expressions are not. */
9651 case CO_RETURN_EXPR
:
9654 case NONTYPE_ARGUMENT_PACK
:
9656 tree args
= ARGUMENT_PACK_ARGS (t
);
9657 int len
= TREE_VEC_LENGTH (args
);
9658 for (int i
= 0; i
< len
; ++i
)
9659 if (!RECUR (TREE_VEC_ELT (args
, i
), any
))
9665 if (objc_non_constant_expr_p (t
))
9668 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t
)));
9676 potential_constant_expression_1 (tree t
, bool want_rval
, bool strict
, bool now
,
9677 tsubst_flags_t flags
)
9679 if (flags
& tf_error
)
9681 /* Check potentiality quietly first, as that could be performed more
9682 efficiently in some cases (currently only for TRUTH_*_EXPR). If
9683 that fails, replay the check noisily to give errors. */
9685 if (potential_constant_expression_1 (t
, want_rval
, strict
, now
, flags
))
9690 tree target
= NULL_TREE
;
9691 return potential_constant_expression_1 (t
, want_rval
, strict
, now
,
9695 /* The main entry point to the above. */
9698 potential_constant_expression (tree t
)
9700 return potential_constant_expression_1 (t
, false, true, false, tf_none
);
9703 /* As above, but require a constant rvalue. */
9706 potential_rvalue_constant_expression (tree t
)
9708 return potential_constant_expression_1 (t
, true, true, false, tf_none
);
9711 /* Like above, but complain about non-constant expressions. */
9714 require_potential_constant_expression (tree t
)
9716 return potential_constant_expression_1 (t
, false, true, false,
9717 tf_warning_or_error
);
9720 /* Cross product of the above. */
9723 require_potential_rvalue_constant_expression (tree t
)
9725 return potential_constant_expression_1 (t
, true, true, false,
9726 tf_warning_or_error
);
9729 /* Like above, but don't consider PARM_DECL a potential_constant_expression. */
9732 require_rvalue_constant_expression (tree t
)
9734 return potential_constant_expression_1 (t
, true, true, true,
9735 tf_warning_or_error
);
9738 /* Like potential_constant_expression, but don't consider possible constexpr
9739 substitution of the current function. That is, PARM_DECL qualifies under
9740 potential_constant_expression, but not here.
9742 This is basically what you can check when any actual constant values might
9743 be value-dependent. */
9746 is_constant_expression (tree t
)
9748 return potential_constant_expression_1 (t
, false, true, true, tf_none
);
9751 /* As above, but expect an rvalue. */
9754 is_rvalue_constant_expression (tree t
)
9756 return potential_constant_expression_1 (t
, true, true, true, tf_none
);
9759 /* Like above, but complain about non-constant expressions. */
9762 require_constant_expression (tree t
)
9764 return potential_constant_expression_1 (t
, false, true, true,
9765 tf_warning_or_error
);
9768 /* Like is_constant_expression, but allow const variables that are not allowed
9769 under constexpr rules. */
9772 is_static_init_expression (tree t
)
9774 return potential_constant_expression_1 (t
, false, false, true, tf_none
);
9777 /* Returns true if T is a potential constant expression that is not
9778 instantiation-dependent, and therefore a candidate for constant folding even
9782 is_nondependent_constant_expression (tree t
)
9784 return (!type_unknown_p (t
)
9785 && is_constant_expression (t
)
9786 && !instantiation_dependent_expression_p (t
));
9789 /* Returns true if T is a potential static initializer expression that is not
9790 instantiation-dependent. */
9793 is_nondependent_static_init_expression (tree t
)
9795 return (!type_unknown_p (t
)
9796 && is_static_init_expression (t
)
9797 && !instantiation_dependent_expression_p (t
));
9800 /* True iff FN is an implicitly constexpr function. */
9803 decl_implicit_constexpr_p (tree fn
)
9805 if (!(flag_implicit_constexpr
9806 && TREE_CODE (fn
) == FUNCTION_DECL
9807 && DECL_DECLARED_CONSTEXPR_P (fn
)))
9810 if (DECL_CLONED_FUNCTION_P (fn
))
9811 fn
= DECL_CLONED_FUNCTION (fn
);
9813 return (DECL_LANG_SPECIFIC (fn
)
9814 && DECL_LANG_SPECIFIC (fn
)->u
.fn
.implicit_constexpr
);
9817 /* Finalize constexpr processing after parsing. */
9820 fini_constexpr (void)
9822 /* The contexpr call and fundef copies tables are no longer needed. */
9823 constexpr_call_table
= NULL
;
9824 fundef_copies_table
= NULL
;
9827 #include "gt-cp-constexpr.h"