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-2021 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"
34 #include "gimple-fold.h"
36 #include "fold-const-call.h"
37 #include "stor-layout.h"
40 static bool verify_constant (tree
, bool, bool *, bool *);
41 #define VERIFY_CONSTANT(X) \
43 if (verify_constant ((X), ctx->quiet, non_constant_p, overflow_p)) \
47 static HOST_WIDE_INT
find_array_ctor_elt (tree ary
, tree dindex
,
49 static int array_index_cmp (tree key
, tree index
);
51 /* Returns true iff FUN is an instantiation of a constexpr function
52 template or a defaulted constexpr function. */
55 is_instantiation_of_constexpr (tree fun
)
57 return ((DECL_TEMPLOID_INSTANTIATION (fun
)
58 && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun
)))
59 || (DECL_DEFAULTED_FN (fun
)
60 && DECL_DECLARED_CONSTEXPR_P (fun
)));
63 /* Return true if T is a literal type. */
66 literal_type_p (tree t
)
71 || (VOID_TYPE_P (t
) && cxx_dialect
>= cxx14
))
75 t
= complete_type (t
);
76 gcc_assert (COMPLETE_TYPE_P (t
) || errorcount
);
77 return CLASSTYPE_LITERAL_P (t
);
79 if (TREE_CODE (t
) == ARRAY_TYPE
)
80 return literal_type_p (strip_array_types (t
));
84 /* If DECL is a variable declared `constexpr', require its type
85 be literal. Return error_mark_node if we give an error, the
89 ensure_literal_type_for_constexpr_object (tree decl
)
91 tree type
= TREE_TYPE (decl
);
93 && (DECL_DECLARED_CONSTEXPR_P (decl
)
94 || var_in_constexpr_fn (decl
))
95 && !processing_template_decl
)
97 tree stype
= strip_array_types (type
);
98 if (CLASS_TYPE_P (stype
) && !COMPLETE_TYPE_P (complete_type (stype
)))
99 /* Don't complain here, we'll complain about incompleteness
100 when we try to initialize the variable. */;
101 else if (!literal_type_p (type
))
103 if (DECL_DECLARED_CONSTEXPR_P (decl
))
105 auto_diagnostic_group d
;
106 error_at (DECL_SOURCE_LOCATION (decl
),
107 "the type %qT of %<constexpr%> variable %qD "
108 "is not literal", type
, decl
);
109 explain_non_literal_class (type
);
110 decl
= error_mark_node
;
114 if (!is_instantiation_of_constexpr (current_function_decl
))
116 auto_diagnostic_group d
;
117 error_at (DECL_SOURCE_LOCATION (decl
),
118 "variable %qD of non-literal type %qT in "
119 "%<constexpr%> function", decl
, type
);
120 explain_non_literal_class (type
);
121 decl
= error_mark_node
;
123 cp_function_chain
->invalid_constexpr
= true;
126 else if (DECL_DECLARED_CONSTEXPR_P (decl
)
127 && variably_modified_type_p (type
, NULL_TREE
))
129 error_at (DECL_SOURCE_LOCATION (decl
),
130 "%<constexpr%> variable %qD has variably-modified "
131 "type %qT", decl
, type
);
132 decl
= error_mark_node
;
138 struct constexpr_fundef_hasher
: ggc_ptr_hash
<constexpr_fundef
>
140 static hashval_t
hash (const constexpr_fundef
*);
141 static bool equal (const constexpr_fundef
*, const constexpr_fundef
*);
144 /* This table holds all constexpr function definitions seen in
145 the current translation unit. */
147 static GTY (()) hash_table
<constexpr_fundef_hasher
> *constexpr_fundef_table
;
149 /* Utility function used for managing the constexpr function table.
150 Return true if the entries pointed to by P and Q are for the
151 same constexpr function. */
154 constexpr_fundef_hasher::equal (const constexpr_fundef
*lhs
,
155 const constexpr_fundef
*rhs
)
157 return lhs
->decl
== rhs
->decl
;
160 /* Utility function used for managing the constexpr function table.
161 Return a hash value for the entry pointed to by Q. */
164 constexpr_fundef_hasher::hash (const constexpr_fundef
*fundef
)
166 return DECL_UID (fundef
->decl
);
169 /* Return a previously saved definition of function FUN. */
172 retrieve_constexpr_fundef (tree fun
)
174 if (constexpr_fundef_table
== NULL
)
177 constexpr_fundef fundef
= { fun
, NULL_TREE
, NULL_TREE
, NULL_TREE
};
178 return constexpr_fundef_table
->find (&fundef
);
181 /* Check whether the parameter and return types of FUN are valid for a
182 constexpr function, and complain if COMPLAIN. */
185 is_valid_constexpr_fn (tree fun
, bool complain
)
189 if (DECL_INHERITED_CTOR (fun
)
190 && TREE_CODE (fun
) == TEMPLATE_DECL
)
194 error ("inherited constructor %qD is not %<constexpr%>",
195 DECL_INHERITED_CTOR (fun
));
199 for (tree parm
= FUNCTION_FIRST_USER_PARM (fun
);
200 parm
!= NULL_TREE
; parm
= TREE_CHAIN (parm
))
201 if (!literal_type_p (TREE_TYPE (parm
)))
206 auto_diagnostic_group d
;
207 error ("invalid type for parameter %d of %<constexpr%> "
208 "function %q+#D", DECL_PARM_INDEX (parm
), fun
);
209 explain_non_literal_class (TREE_TYPE (parm
));
214 if (LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun
)) && cxx_dialect
< cxx17
)
218 inform (DECL_SOURCE_LOCATION (fun
),
219 "lambdas are implicitly %<constexpr%> only in C++17 and later");
221 else if (!DECL_CONSTRUCTOR_P (fun
))
223 tree rettype
= TREE_TYPE (TREE_TYPE (fun
));
224 if (!literal_type_p (rettype
))
229 auto_diagnostic_group d
;
230 error ("invalid return type %qT of %<constexpr%> function %q+D",
232 explain_non_literal_class (rettype
);
236 /* C++14 DR 1684 removed this restriction. */
237 if (cxx_dialect
< cxx14
238 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fun
)
239 && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun
)))
244 auto_diagnostic_group d
;
245 if (pedwarn (DECL_SOURCE_LOCATION (fun
), OPT_Wpedantic
,
246 "enclosing class of %<constexpr%> non-static"
247 " member function %q+#D is not a literal type",
249 explain_non_literal_class (DECL_CONTEXT (fun
));
253 else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun
)))
257 error ("%q#T has virtual base classes", DECL_CONTEXT (fun
));
263 /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF
264 for a member of an anonymous aggregate, INIT is the initializer for that
265 member, and VEC_OUTER is the vector of constructor elements for the class
266 whose constructor we are processing. Add the initializer to the vector
267 and return true to indicate success. */
270 build_anon_member_initialization (tree member
, tree init
,
271 vec
<constructor_elt
, va_gc
> **vec_outer
)
273 /* MEMBER presents the relevant fields from the inside out, but we need
274 to build up the initializer from the outside in so that we can reuse
275 previously built CONSTRUCTORs if this is, say, the second field in an
276 anonymous struct. So we use a vec as a stack. */
277 auto_vec
<tree
, 2> fields
;
280 fields
.safe_push (TREE_OPERAND (member
, 1));
281 member
= TREE_OPERAND (member
, 0);
283 while (ANON_AGGR_TYPE_P (TREE_TYPE (member
))
284 && TREE_CODE (member
) == COMPONENT_REF
);
286 /* VEC has the constructor elements vector for the context of FIELD.
287 If FIELD is an anonymous aggregate, we will push inside it. */
288 vec
<constructor_elt
, va_gc
> **vec
= vec_outer
;
290 while (field
= fields
.pop(),
291 ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
294 /* If there is already an outer constructor entry for the anonymous
295 aggregate FIELD, use it; otherwise, insert one. */
296 if (vec_safe_is_empty (*vec
)
297 || (*vec
)->last().index
!= field
)
299 ctor
= build_constructor (TREE_TYPE (field
), NULL
);
300 CONSTRUCTOR_APPEND_ELT (*vec
, field
, ctor
);
303 ctor
= (*vec
)->last().value
;
304 vec
= &CONSTRUCTOR_ELTS (ctor
);
307 /* Now we're at the innermost field, the one that isn't an anonymous
308 aggregate. Add its initializer to the CONSTRUCTOR and we're done. */
309 gcc_assert (fields
.is_empty());
310 CONSTRUCTOR_APPEND_ELT (*vec
, field
, init
);
315 /* Subroutine of build_constexpr_constructor_member_initializers.
316 The expression tree T represents a data member initialization
317 in a (constexpr) constructor definition. Build a pairing of
318 the data member with its initializer, and prepend that pair
319 to the existing initialization pair INITS. */
322 build_data_member_initialization (tree t
, vec
<constructor_elt
, va_gc
> **vec
)
325 if (TREE_CODE (t
) == CLEANUP_POINT_EXPR
)
326 t
= TREE_OPERAND (t
, 0);
327 if (TREE_CODE (t
) == EXPR_STMT
)
328 t
= TREE_OPERAND (t
, 0);
329 if (t
== error_mark_node
)
331 if (TREE_CODE (t
) == STATEMENT_LIST
)
333 for (tree stmt
: tsi_range (t
))
334 if (! build_data_member_initialization (stmt
, vec
))
338 if (TREE_CODE (t
) == CLEANUP_STMT
)
340 /* We can't see a CLEANUP_STMT in a constructor for a literal class,
341 but we can in a constexpr constructor for a non-literal class. Just
342 ignore it; either all the initialization will be constant, in which
343 case the cleanup can't run, or it can't be constexpr.
344 Still recurse into CLEANUP_BODY. */
345 return build_data_member_initialization (CLEANUP_BODY (t
), vec
);
347 if (TREE_CODE (t
) == CONVERT_EXPR
)
348 t
= TREE_OPERAND (t
, 0);
349 if (TREE_CODE (t
) == INIT_EXPR
350 /* vptr initialization shows up as a MODIFY_EXPR. In C++14 we only
351 use what this function builds for cx_check_missing_mem_inits, and
352 assignment in the ctor body doesn't count. */
353 || (cxx_dialect
< cxx14
&& TREE_CODE (t
) == MODIFY_EXPR
))
355 member
= TREE_OPERAND (t
, 0);
356 init
= break_out_target_exprs (TREE_OPERAND (t
, 1));
358 else if (TREE_CODE (t
) == CALL_EXPR
)
360 tree fn
= get_callee_fndecl (t
);
361 if (!fn
|| !DECL_CONSTRUCTOR_P (fn
))
362 /* We're only interested in calls to subobject constructors. */
364 member
= CALL_EXPR_ARG (t
, 0);
365 /* We don't use build_cplus_new here because it complains about
366 abstract bases. Leaving the call unwrapped means that it has the
367 wrong type, but cxx_eval_constant_expression doesn't care. */
368 init
= break_out_target_exprs (t
);
370 else if (TREE_CODE (t
) == BIND_EXPR
)
371 return build_data_member_initialization (BIND_EXPR_BODY (t
), vec
);
373 /* Don't add anything else to the CONSTRUCTOR. */
375 if (INDIRECT_REF_P (member
))
376 member
= TREE_OPERAND (member
, 0);
377 if (TREE_CODE (member
) == NOP_EXPR
)
381 if (TREE_CODE (op
) == ADDR_EXPR
)
383 gcc_assert (same_type_ignoring_top_level_qualifiers_p
384 (TREE_TYPE (TREE_TYPE (op
)),
385 TREE_TYPE (TREE_TYPE (member
))));
386 /* Initializing a cv-qualified member; we need to look through
390 else if (op
== current_class_ptr
391 && (same_type_ignoring_top_level_qualifiers_p
392 (TREE_TYPE (TREE_TYPE (member
)),
393 current_class_type
)))
394 /* Delegating constructor. */
398 /* This is an initializer for an empty base; keep it for now so
399 we can check it in cxx_eval_bare_aggregate. */
400 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member
))));
403 if (TREE_CODE (member
) == ADDR_EXPR
)
404 member
= TREE_OPERAND (member
, 0);
405 if (TREE_CODE (member
) == COMPONENT_REF
)
407 tree aggr
= TREE_OPERAND (member
, 0);
408 if (TREE_CODE (aggr
) == VAR_DECL
)
409 /* Initializing a local variable, don't add anything. */
411 if (TREE_CODE (aggr
) != COMPONENT_REF
)
412 /* Normal member initialization. */
413 member
= TREE_OPERAND (member
, 1);
414 else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr
)))
415 /* Initializing a member of an anonymous union. */
416 return build_anon_member_initialization (member
, init
, vec
);
418 /* We're initializing a vtable pointer in a base. Leave it as
419 COMPONENT_REF so we remember the path to get to the vfield. */
420 gcc_assert (TREE_TYPE (member
) == vtbl_ptr_type_node
);
423 /* Value-initialization can produce multiple initializers for the
424 same field; use the last one. */
425 if (!vec_safe_is_empty (*vec
) && (*vec
)->last().index
== member
)
426 (*vec
)->last().value
= init
;
428 CONSTRUCTOR_APPEND_ELT (*vec
, member
, init
);
432 /* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval.
433 In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a
434 BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */
437 check_constexpr_bind_expr_vars (tree t
)
439 gcc_assert (TREE_CODE (t
) == BIND_EXPR
);
441 for (tree var
= BIND_EXPR_VARS (t
); var
; var
= DECL_CHAIN (var
))
442 if (TREE_CODE (var
) == TYPE_DECL
443 && DECL_IMPLICIT_TYPEDEF_P (var
)
444 && !LAMBDA_TYPE_P (TREE_TYPE (var
)))
449 /* Subroutine of check_constexpr_ctor_body. */
452 check_constexpr_ctor_body_1 (tree last
, tree list
)
454 switch (TREE_CODE (list
))
457 if (TREE_CODE (DECL_EXPR_DECL (list
)) == USING_DECL
458 || TREE_CODE (DECL_EXPR_DECL (list
)) == TYPE_DECL
)
462 case CLEANUP_POINT_EXPR
:
463 return check_constexpr_ctor_body (last
, TREE_OPERAND (list
, 0),
467 if (!check_constexpr_bind_expr_vars (list
)
468 || !check_constexpr_ctor_body (last
, BIND_EXPR_BODY (list
),
475 case DEBUG_BEGIN_STMT
:
483 /* Make sure that there are no statements after LAST in the constructor
484 body represented by LIST. */
487 check_constexpr_ctor_body (tree last
, tree list
, bool complain
)
489 /* C++14 doesn't require a constexpr ctor to have an empty body. */
490 if (cxx_dialect
>= cxx14
)
494 if (TREE_CODE (list
) == STATEMENT_LIST
)
496 tree_stmt_iterator i
= tsi_last (list
);
497 for (; !tsi_end_p (i
); tsi_prev (&i
))
499 tree t
= tsi_stmt (i
);
502 if (!check_constexpr_ctor_body_1 (last
, t
))
509 else if (list
!= last
510 && !check_constexpr_ctor_body_1 (last
, list
))
515 error ("%<constexpr%> constructor does not have empty body");
516 DECL_DECLARED_CONSTEXPR_P (current_function_decl
) = false;
521 /* V is a vector of constructor elements built up for the base and member
522 initializers of a constructor for TYPE. They need to be in increasing
523 offset order, which they might not be yet if TYPE has a primary base
524 which is not first in the base-clause or a vptr and at least one base
525 all of which are non-primary. */
527 static vec
<constructor_elt
, va_gc
> *
528 sort_constexpr_mem_initializers (tree type
, vec
<constructor_elt
, va_gc
> *v
)
530 tree pri
= CLASSTYPE_PRIMARY_BINFO (type
);
536 field_type
= BINFO_TYPE (pri
);
537 else if (TYPE_CONTAINS_VPTR_P (type
))
538 field_type
= vtbl_ptr_type_node
;
542 /* Find the element for the primary base or vptr and move it to the
543 beginning of the vec. */
544 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
545 if (TREE_TYPE (ce
->index
) == field_type
)
548 if (i
> 0 && i
< vec_safe_length (v
))
550 vec
<constructor_elt
, va_gc
> &vref
= *v
;
551 constructor_elt elt
= vref
[i
];
560 /* Build compile-time evalable representations of member-initializer list
561 for a constexpr constructor. */
564 build_constexpr_constructor_member_initializers (tree type
, tree body
)
566 vec
<constructor_elt
, va_gc
> *vec
= NULL
;
569 switch (TREE_CODE (body
))
571 case MUST_NOT_THROW_EXPR
:
573 body
= TREE_OPERAND (body
, 0);
577 for (tree stmt
: tsi_range (body
))
580 if (TREE_CODE (body
) == BIND_EXPR
)
586 body
= BIND_EXPR_BODY (body
);
593 if (TREE_CODE (body
) == TRY_BLOCK
)
595 body
= TREE_OPERAND (body
, 0);
596 if (TREE_CODE (body
) == BIND_EXPR
)
597 body
= BIND_EXPR_BODY (body
);
599 if (TREE_CODE (body
) == CLEANUP_POINT_EXPR
)
601 body
= TREE_OPERAND (body
, 0);
602 if (TREE_CODE (body
) == EXPR_STMT
)
603 body
= TREE_OPERAND (body
, 0);
604 if (TREE_CODE (body
) == INIT_EXPR
605 && (same_type_ignoring_top_level_qualifiers_p
606 (TREE_TYPE (TREE_OPERAND (body
, 0)),
607 current_class_type
)))
610 return TREE_OPERAND (body
, 1);
612 ok
= build_data_member_initialization (body
, &vec
);
614 else if (TREE_CODE (body
) == STATEMENT_LIST
)
616 for (tree stmt
: tsi_range (body
))
618 ok
= build_data_member_initialization (stmt
, &vec
);
623 else if (EXPR_P (body
))
624 ok
= build_data_member_initialization (body
, &vec
);
626 gcc_assert (errorcount
> 0);
629 if (vec_safe_length (vec
) > 0)
631 /* In a delegating constructor, return the target. */
632 constructor_elt
*ce
= &(*vec
)[0];
633 if (ce
->index
== current_class_ptr
)
640 vec
= sort_constexpr_mem_initializers (type
, vec
);
641 return build_constructor (type
, vec
);
644 return error_mark_node
;
647 /* We have an expression tree T that represents a call, either CALL_EXPR
648 or AGGR_INIT_EXPR. If the call is lexically to a named function,
649 retrun the _DECL for that function. */
652 get_function_named_in_call (tree t
)
654 tree fun
= cp_get_callee (t
);
655 if (fun
&& TREE_CODE (fun
) == ADDR_EXPR
656 && TREE_CODE (TREE_OPERAND (fun
, 0)) == FUNCTION_DECL
)
657 fun
= TREE_OPERAND (fun
, 0);
661 /* Subroutine of check_constexpr_fundef. BODY is the body of a function
662 declared to be constexpr, or a sub-statement thereof. Returns the
663 return value if suitable, error_mark_node for a statement not allowed in
664 a constexpr function, or NULL_TREE if no return value was found. */
667 constexpr_fn_retval (tree body
)
669 switch (TREE_CODE (body
))
673 tree expr
= NULL_TREE
;
674 for (tree stmt
: tsi_range (body
))
676 tree s
= constexpr_fn_retval (stmt
);
677 if (s
== error_mark_node
)
678 return error_mark_node
;
679 else if (s
== NULL_TREE
)
680 /* Keep iterating. */;
682 /* Multiple return statements. */
683 return error_mark_node
;
691 return break_out_target_exprs (TREE_OPERAND (body
, 0));
695 tree decl
= DECL_EXPR_DECL (body
);
696 if (TREE_CODE (decl
) == USING_DECL
697 /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */
698 || DECL_ARTIFICIAL (decl
))
700 return error_mark_node
;
703 case CLEANUP_POINT_EXPR
:
704 return constexpr_fn_retval (TREE_OPERAND (body
, 0));
707 if (!check_constexpr_bind_expr_vars (body
))
708 return error_mark_node
;
709 return constexpr_fn_retval (BIND_EXPR_BODY (body
));
712 case DEBUG_BEGIN_STMT
:
717 tree fun
= get_function_named_in_call (body
);
719 && fndecl_built_in_p (fun
, BUILT_IN_UNREACHABLE
))
725 return error_mark_node
;
729 /* Subroutine of check_constexpr_fundef. BODY is the DECL_SAVED_TREE of
730 FUN; do the necessary transformations to turn it into a single expression
731 that we can store in the hash table. */
734 massage_constexpr_body (tree fun
, tree body
)
736 if (DECL_CONSTRUCTOR_P (fun
))
737 body
= build_constexpr_constructor_member_initializers
738 (DECL_CONTEXT (fun
), body
);
739 else if (cxx_dialect
< cxx14
)
741 if (TREE_CODE (body
) == EH_SPEC_BLOCK
)
742 body
= EH_SPEC_STMTS (body
);
743 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
744 body
= TREE_OPERAND (body
, 0);
745 body
= constexpr_fn_retval (body
);
750 /* CTYPE is a type constructed from BODY. Return true if some
751 bases/fields are uninitialized, and complain if COMPLAIN. */
754 cx_check_missing_mem_inits (tree ctype
, tree body
, bool complain
)
756 /* We allow uninitialized bases/fields in C++20. */
757 if (cxx_dialect
>= cxx20
)
764 if (TREE_CODE (body
) != CONSTRUCTOR
)
766 nelts
= CONSTRUCTOR_NELTS (body
);
768 tree field
= TYPE_FIELDS (ctype
);
770 if (TREE_CODE (ctype
) == UNION_TYPE
)
772 if (nelts
== 0 && next_initializable_field (field
))
775 error ("%<constexpr%> constructor for union %qT must "
776 "initialize exactly one non-static data member", ctype
);
782 /* Iterate over the CONSTRUCTOR, checking any missing fields don't
783 need an explicit initialization. */
785 for (unsigned i
= 0; i
<= nelts
; ++i
)
787 tree index
= NULL_TREE
;
790 index
= CONSTRUCTOR_ELT (body
, i
)->index
;
791 /* Skip base and vtable inits. */
792 if (TREE_CODE (index
) != FIELD_DECL
793 || DECL_ARTIFICIAL (index
))
797 for (; field
!= index
; field
= DECL_CHAIN (field
))
800 if (TREE_CODE (field
) != FIELD_DECL
)
802 if (DECL_UNNAMED_BIT_FIELD (field
))
804 if (DECL_ARTIFICIAL (field
))
806 if (ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
808 /* Recurse to check the anonymous aggregate member. */
809 bad
|= cx_check_missing_mem_inits
810 (TREE_TYPE (field
), NULL_TREE
, complain
);
811 if (bad
&& !complain
)
815 ftype
= TREE_TYPE (field
);
816 if (!ftype
|| !TYPE_P (ftype
) || !COMPLETE_TYPE_P (ftype
))
817 /* A flexible array can't be intialized here, so don't complain
820 if (is_empty_field (field
))
821 /* An empty field doesn't need an initializer. */
823 ftype
= strip_array_types (ftype
);
824 if (type_has_constexpr_default_constructor (ftype
))
826 /* It's OK to skip a member with a trivial constexpr ctor.
827 A constexpr ctor that isn't trivial should have been
829 gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype
)
835 auto_diagnostic_group d
;
836 error ("member %qD must be initialized by mem-initializer "
837 "in %<constexpr%> constructor", field
);
838 inform (DECL_SOURCE_LOCATION (field
), "declared here");
841 if (field
== NULL_TREE
)
844 if (ANON_AGGR_TYPE_P (TREE_TYPE (index
)))
846 /* Check the anonymous aggregate initializer is valid. */
847 bad
|= cx_check_missing_mem_inits
848 (TREE_TYPE (index
), CONSTRUCTOR_ELT (body
, i
)->value
, complain
);
849 if (bad
&& !complain
)
852 field
= DECL_CHAIN (field
);
858 /* We are processing the definition of the constexpr function FUN.
859 Check that its body fulfills the apropriate requirements and
860 enter it in the constexpr function definition table. */
863 maybe_save_constexpr_fundef (tree fun
)
865 if (processing_template_decl
866 || !DECL_DECLARED_CONSTEXPR_P (fun
)
867 || cp_function_chain
->invalid_constexpr
868 || (DECL_CLONED_FUNCTION_P (fun
) && !DECL_DELETING_DESTRUCTOR_P (fun
)))
871 if (!is_valid_constexpr_fn (fun
, !DECL_GENERATED_P (fun
)))
874 tree massaged
= massage_constexpr_body (fun
, DECL_SAVED_TREE (fun
));
875 if (massaged
== NULL_TREE
|| massaged
== error_mark_node
)
877 if (!DECL_CONSTRUCTOR_P (fun
))
878 error ("body of %<constexpr%> function %qD not a return-statement",
883 bool potential
= potential_rvalue_constant_expression (massaged
);
884 if (!potential
&& !DECL_GENERATED_P (fun
))
885 require_potential_rvalue_constant_expression (massaged
);
887 if (DECL_CONSTRUCTOR_P (fun
)
888 && cx_check_missing_mem_inits (DECL_CONTEXT (fun
),
889 massaged
, !DECL_GENERATED_P (fun
)))
892 if (!potential
&& !DECL_GENERATED_P (fun
))
895 constexpr_fundef entry
= {fun
, NULL_TREE
, NULL_TREE
, NULL_TREE
};
896 bool clear_ctx
= false;
897 if (DECL_RESULT (fun
) && DECL_CONTEXT (DECL_RESULT (fun
)) == NULL_TREE
)
900 DECL_CONTEXT (DECL_RESULT (fun
)) = fun
;
902 tree saved_fn
= current_function_decl
;
903 current_function_decl
= fun
;
904 entry
.body
= copy_fn (entry
.decl
, entry
.parms
, entry
.result
);
905 current_function_decl
= saved_fn
;
907 DECL_CONTEXT (DECL_RESULT (entry
.decl
)) = NULL_TREE
;
909 /* For a template instantiation, we want to remember the pre-generic body
910 for explain_invalid_constexpr_fn, but do tell cxx_eval_call_expression
911 that it doesn't need to bother trying to expand the function. */
912 entry
.result
= error_mark_node
;
914 register_constexpr_fundef (entry
);
917 /* BODY is a validated and massaged definition of a constexpr
918 function. Register it in the hash table. */
921 register_constexpr_fundef (const constexpr_fundef
&value
)
923 /* Create the constexpr function table if necessary. */
924 if (constexpr_fundef_table
== NULL
)
925 constexpr_fundef_table
926 = hash_table
<constexpr_fundef_hasher
>::create_ggc (101);
928 constexpr_fundef
**slot
= constexpr_fundef_table
->find_slot
929 (const_cast<constexpr_fundef
*> (&value
), INSERT
);
931 gcc_assert (*slot
== NULL
);
932 *slot
= ggc_alloc
<constexpr_fundef
> ();
936 /* FUN is a non-constexpr function called in a context that requires a
937 constant expression. If it comes from a constexpr template, explain why
938 the instantiation isn't constexpr. */
941 explain_invalid_constexpr_fn (tree fun
)
943 static hash_set
<tree
> *diagnosed
;
945 /* Only diagnose defaulted functions, lambdas, or instantiations. */
946 if (!DECL_DEFAULTED_FN (fun
)
947 && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun
))
948 && !is_instantiation_of_constexpr (fun
))
950 inform (DECL_SOURCE_LOCATION (fun
), "%qD declared here", fun
);
953 if (diagnosed
== NULL
)
954 diagnosed
= new hash_set
<tree
>;
955 if (diagnosed
->add (fun
))
956 /* Already explained. */
959 iloc_sentinel ils
= input_location
;
960 if (!lambda_static_thunk_p (fun
))
962 /* Diagnostics should completely ignore the static thunk, so leave
963 input_location set to our caller's location. */
964 input_location
= DECL_SOURCE_LOCATION (fun
);
965 inform (input_location
,
966 "%qD is not usable as a %<constexpr%> function because:", fun
);
968 /* First check the declaration. */
969 if (is_valid_constexpr_fn (fun
, true))
971 /* Then if it's OK, the body. */
972 if (!DECL_DECLARED_CONSTEXPR_P (fun
)
973 && DECL_DEFAULTED_FN (fun
))
974 explain_implicit_non_constexpr (fun
);
977 if (constexpr_fundef
*fd
= retrieve_constexpr_fundef (fun
))
980 body
= DECL_SAVED_TREE (fun
);
981 body
= massage_constexpr_body (fun
, body
);
982 require_potential_rvalue_constant_expression (body
);
983 if (DECL_CONSTRUCTOR_P (fun
))
984 cx_check_missing_mem_inits (DECL_CONTEXT (fun
), body
, true);
989 /* Objects of this type represent calls to constexpr functions
990 along with the bindings of parameters to their arguments, for
991 the purpose of compile time evaluation. */
993 struct GTY((for_user
)) constexpr_call
{
994 /* Description of the constexpr function definition. */
995 constexpr_fundef
*fundef
;
996 /* Parameter bindings environment. A TREE_VEC of arguments. */
998 /* Result of the call.
999 NULL means the call is being evaluated.
1000 error_mark_node means that the evaluation was erroneous;
1001 otherwise, the actuall value of the call. */
1003 /* The hash of this call; we remember it here to avoid having to
1004 recalculate it when expanding the hash table. */
1006 /* Whether __builtin_is_constant_evaluated() should evaluate to true. */
1007 bool manifestly_const_eval
;
1010 struct constexpr_call_hasher
: ggc_ptr_hash
<constexpr_call
>
1012 static hashval_t
hash (constexpr_call
*);
1013 static bool equal (constexpr_call
*, constexpr_call
*);
1016 enum constexpr_switch_state
{
1017 /* Used when processing a switch for the first time by cxx_eval_switch_expr
1018 and default: label for that switch has not been seen yet. */
1019 css_default_not_seen
,
1020 /* Used when processing a switch for the first time by cxx_eval_switch_expr
1021 and default: label for that switch has been seen already. */
1023 /* Used when processing a switch for the second time by
1024 cxx_eval_switch_expr, where default: label should match. */
1025 css_default_processing
1028 /* The constexpr expansion context part which needs one instance per
1029 cxx_eval_outermost_constant_expr invocation. VALUES is a map of values of
1030 variables initialized within the expression. */
1032 struct constexpr_global_ctx
{
1033 /* Values for any temporaries or local variables within the
1034 constant-expression. */
1035 hash_map
<tree
,tree
> values
;
1036 /* Number of cxx_eval_constant_expression calls (except skipped ones,
1037 on simple constants or location wrappers) encountered during current
1038 cxx_eval_outermost_constant_expr call. */
1039 HOST_WIDE_INT constexpr_ops_count
;
1040 /* Heap VAR_DECLs created during the evaluation of the outermost constant
1042 auto_vec
<tree
, 16> heap_vars
;
1043 /* Cleanups that need to be evaluated at the end of CLEANUP_POINT_EXPR. */
1044 vec
<tree
> *cleanups
;
1045 /* Number of heap VAR_DECL deallocations. */
1046 unsigned heap_dealloc_count
;
1048 constexpr_global_ctx ()
1049 : constexpr_ops_count (0), cleanups (NULL
), heap_dealloc_count (0) {}
1052 /* The constexpr expansion context. CALL is the current function
1053 expansion, CTOR is the current aggregate initializer, OBJECT is the
1054 object being initialized by CTOR, either a VAR_DECL or a _REF. */
1056 struct constexpr_ctx
{
1057 /* The part of the context that needs to be unique to the whole
1058 cxx_eval_outermost_constant_expr invocation. */
1059 constexpr_global_ctx
*global
;
1060 /* The innermost call we're evaluating. */
1061 constexpr_call
*call
;
1062 /* SAVE_EXPRs and TARGET_EXPR_SLOT vars of TARGET_EXPRs that we've seen
1063 within the current LOOP_EXPR. NULL if we aren't inside a loop. */
1064 vec
<tree
> *save_exprs
;
1065 /* The CONSTRUCTOR we're currently building up for an aggregate
1068 /* The object we're building the CONSTRUCTOR for. */
1070 /* If inside SWITCH_EXPR. */
1071 constexpr_switch_state
*css_state
;
1072 /* The aggregate initialization context inside which this one is nested. This
1073 is used by lookup_placeholder to resolve PLACEHOLDER_EXPRs. */
1074 const constexpr_ctx
*parent
;
1076 /* Whether we should error on a non-constant expression or fail quietly.
1077 This flag needs to be here, but some of the others could move to global
1078 if they get larger than a word. */
1080 /* Whether we are strictly conforming to constant expression rules or
1081 trying harder to get a constant value. */
1083 /* Whether __builtin_is_constant_evaluated () should be true. */
1084 bool manifestly_const_eval
;
1087 /* This internal flag controls whether we should avoid doing anything during
1088 constexpr evaluation that would cause extra DECL_UID generation, such as
1089 template instantiation and function body copying. */
1091 static bool uid_sensitive_constexpr_evaluation_value
;
1093 /* An internal counter that keeps track of the number of times
1094 uid_sensitive_constexpr_evaluation_p returned true. */
1096 static unsigned uid_sensitive_constexpr_evaluation_true_counter
;
1098 /* The accessor for uid_sensitive_constexpr_evaluation_value which also
1099 increments the corresponding counter. */
1102 uid_sensitive_constexpr_evaluation_p ()
1104 if (uid_sensitive_constexpr_evaluation_value
)
1106 ++uid_sensitive_constexpr_evaluation_true_counter
;
1113 /* The default constructor for uid_sensitive_constexpr_evaluation_sentinel
1114 enables the internal flag for uid_sensitive_constexpr_evaluation_p
1115 during the lifetime of the sentinel object. Upon its destruction, the
1116 previous value of uid_sensitive_constexpr_evaluation_p is restored. */
1118 uid_sensitive_constexpr_evaluation_sentinel
1119 ::uid_sensitive_constexpr_evaluation_sentinel ()
1120 : ovr (uid_sensitive_constexpr_evaluation_value
, true)
1124 /* The default constructor for uid_sensitive_constexpr_evaluation_checker
1125 records the current number of times that uid_sensitive_constexpr_evaluation_p
1126 has been called and returned true. */
1128 uid_sensitive_constexpr_evaluation_checker
1129 ::uid_sensitive_constexpr_evaluation_checker ()
1130 : saved_counter (uid_sensitive_constexpr_evaluation_true_counter
)
1134 /* Returns true iff uid_sensitive_constexpr_evaluation_p is true, and
1135 some constexpr evaluation was restricted due to u_s_c_e_p being called
1136 and returning true during the lifetime of this checker object. */
1139 uid_sensitive_constexpr_evaluation_checker::evaluation_restricted_p () const
1141 return (uid_sensitive_constexpr_evaluation_value
1142 && saved_counter
!= uid_sensitive_constexpr_evaluation_true_counter
);
1146 /* A table of all constexpr calls that have been evaluated by the
1147 compiler in this translation unit. */
1149 static GTY (()) hash_table
<constexpr_call_hasher
> *constexpr_call_table
;
1151 static tree
cxx_eval_constant_expression (const constexpr_ctx
*, tree
,
1152 bool, bool *, bool *, tree
* = NULL
);
1154 /* Compute a hash value for a constexpr call representation. */
1157 constexpr_call_hasher::hash (constexpr_call
*info
)
1162 /* Return true if the objects pointed to by P and Q represent calls
1163 to the same constexpr function with the same arguments.
1164 Otherwise, return false. */
1167 constexpr_call_hasher::equal (constexpr_call
*lhs
, constexpr_call
*rhs
)
1171 if (lhs
->hash
!= rhs
->hash
)
1173 if (lhs
->manifestly_const_eval
!= rhs
->manifestly_const_eval
)
1175 if (!constexpr_fundef_hasher::equal (lhs
->fundef
, rhs
->fundef
))
1177 return cp_tree_equal (lhs
->bindings
, rhs
->bindings
);
1180 /* Initialize the constexpr call table, if needed. */
1183 maybe_initialize_constexpr_call_table (void)
1185 if (constexpr_call_table
== NULL
)
1186 constexpr_call_table
= hash_table
<constexpr_call_hasher
>::create_ggc (101);
1189 /* During constexpr CALL_EXPR evaluation, to avoid issues with sharing when
1190 a function happens to get called recursively, we unshare the callee
1191 function's body and evaluate this unshared copy instead of evaluating the
1194 FUNDEF_COPIES_TABLE is a per-function freelist of these unshared function
1195 copies. The underlying data structure of FUNDEF_COPIES_TABLE is a hash_map
1196 that's keyed off of the original FUNCTION_DECL and whose value is a
1197 TREE_LIST of this function's unused copies awaiting reuse.
1199 This is not GC-deletable to avoid GC affecting UID generation. */
1201 static GTY(()) decl_tree_map
*fundef_copies_table
;
1203 /* Reuse a copy or create a new unshared copy of the function FUN.
1204 Return this copy. We use a TREE_LIST whose PURPOSE is body, VALUE
1205 is parms, TYPE is result. */
1208 get_fundef_copy (constexpr_fundef
*fundef
)
1212 tree
*slot
= &(hash_map_safe_get_or_insert
<hm_ggc
>
1213 (fundef_copies_table
, fundef
->decl
, &existed
, 127));
1217 /* There is no cached function available, or in use. We can use
1218 the function directly. That the slot is now created records
1219 that this function is now in use. */
1220 copy
= build_tree_list (fundef
->body
, fundef
->parms
);
1221 TREE_TYPE (copy
) = fundef
->result
;
1223 else if (*slot
== NULL_TREE
)
1225 if (uid_sensitive_constexpr_evaluation_p ())
1228 /* We've already used the function itself, so make a copy. */
1229 copy
= build_tree_list (NULL
, NULL
);
1230 tree saved_body
= DECL_SAVED_TREE (fundef
->decl
);
1231 tree saved_parms
= DECL_ARGUMENTS (fundef
->decl
);
1232 tree saved_result
= DECL_RESULT (fundef
->decl
);
1233 tree saved_fn
= current_function_decl
;
1234 DECL_SAVED_TREE (fundef
->decl
) = fundef
->body
;
1235 DECL_ARGUMENTS (fundef
->decl
) = fundef
->parms
;
1236 DECL_RESULT (fundef
->decl
) = fundef
->result
;
1237 current_function_decl
= fundef
->decl
;
1238 TREE_PURPOSE (copy
) = copy_fn (fundef
->decl
, TREE_VALUE (copy
),
1240 current_function_decl
= saved_fn
;
1241 DECL_RESULT (fundef
->decl
) = saved_result
;
1242 DECL_ARGUMENTS (fundef
->decl
) = saved_parms
;
1243 DECL_SAVED_TREE (fundef
->decl
) = saved_body
;
1247 /* We have a cached function available. */
1249 *slot
= TREE_CHAIN (copy
);
1255 /* Save the copy COPY of function FUN for later reuse by
1256 get_fundef_copy(). By construction, there will always be an entry
1260 save_fundef_copy (tree fun
, tree copy
)
1262 tree
*slot
= fundef_copies_table
->get (fun
);
1263 TREE_CHAIN (copy
) = *slot
;
1267 /* We have an expression tree T that represents a call, either CALL_EXPR
1268 or AGGR_INIT_EXPR. Return the Nth argument. */
1271 get_nth_callarg (tree t
, int n
)
1273 switch (TREE_CODE (t
))
1276 return CALL_EXPR_ARG (t
, n
);
1278 case AGGR_INIT_EXPR
:
1279 return AGGR_INIT_EXPR_ARG (t
, n
);
1287 /* Attempt to evaluate T which represents a call to a builtin function.
1288 We assume here that all builtin functions evaluate to scalar types
1289 represented by _CST nodes. */
1292 cxx_eval_builtin_function_call (const constexpr_ctx
*ctx
, tree t
, tree fun
,
1294 bool *non_constant_p
, bool *overflow_p
)
1296 const int nargs
= call_expr_nargs (t
);
1297 tree
*args
= (tree
*) alloca (nargs
* sizeof (tree
));
1301 /* Don't fold __builtin_constant_p within a constexpr function. */
1302 bool bi_const_p
= DECL_IS_BUILTIN_CONSTANT_P (fun
);
1304 /* If we aren't requiring a constant expression, defer __builtin_constant_p
1305 in a constexpr function until we have values for the parameters. */
1307 && !ctx
->manifestly_const_eval
1308 && current_function_decl
1309 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
1311 *non_constant_p
= true;
1315 /* For __builtin_is_constant_evaluated, defer it if not
1316 ctx->manifestly_const_eval (as sometimes we try to constant evaluate
1317 without manifestly_const_eval even expressions or parts thereof which
1318 will later be manifestly const_eval evaluated), otherwise fold it to
1320 if (fndecl_built_in_p (fun
, CP_BUILT_IN_IS_CONSTANT_EVALUATED
,
1323 if (!ctx
->manifestly_const_eval
)
1325 *non_constant_p
= true;
1328 return boolean_true_node
;
1331 if (fndecl_built_in_p (fun
, CP_BUILT_IN_SOURCE_LOCATION
, BUILT_IN_FRONTEND
))
1333 temp_override
<tree
> ovr (current_function_decl
);
1334 if (ctx
->call
&& ctx
->call
->fundef
)
1335 current_function_decl
= ctx
->call
->fundef
->decl
;
1336 return fold_builtin_source_location (EXPR_LOCATION (t
));
1341 if (fndecl_built_in_p (fun
, BUILT_IN_NORMAL
))
1342 switch (DECL_FUNCTION_CODE (fun
))
1344 case BUILT_IN_STRLEN
:
1345 case BUILT_IN_STRNLEN
:
1348 case BUILT_IN_MEMCHR
:
1349 case BUILT_IN_STRCHR
:
1350 case BUILT_IN_STRRCHR
:
1354 case BUILT_IN_MEMCMP
:
1355 case BUILT_IN_STRCMP
:
1358 case BUILT_IN_STRSTR
:
1362 case BUILT_IN_ASAN_POINTER_COMPARE
:
1363 case BUILT_IN_ASAN_POINTER_SUBTRACT
:
1364 /* These builtins shall be ignored during constant expression
1371 /* Be permissive for arguments to built-ins; __builtin_constant_p should
1372 return constant false for a non-constant argument. */
1373 constexpr_ctx new_ctx
= *ctx
;
1374 new_ctx
.quiet
= true;
1375 for (i
= 0; i
< nargs
; ++i
)
1377 tree arg
= CALL_EXPR_ARG (t
, i
);
1380 /* To handle string built-ins we need to pass ADDR_EXPR<STRING_CST> since
1381 expand_builtin doesn't know how to look in the values table. */
1382 bool strop
= i
< strops
;
1386 if (TREE_CODE (arg
) == ADDR_EXPR
)
1387 arg
= TREE_OPERAND (arg
, 0);
1392 /* If builtin_valid_in_constant_expr_p is true,
1393 potential_constant_expression_1 has not recursed into the arguments
1394 of the builtin, verify it here. */
1395 if (!builtin_valid_in_constant_expr_p (fun
)
1396 || potential_constant_expression (arg
))
1398 bool dummy1
= false, dummy2
= false;
1399 arg
= cxx_eval_constant_expression (&new_ctx
, arg
, false,
1404 /* For __builtin_constant_p, fold all expressions with constant values
1405 even if they aren't C++ constant-expressions. */
1406 arg
= cp_fold_rvalue (arg
);
1409 if (TREE_CODE (arg
) == CONSTRUCTOR
)
1410 arg
= braced_lists_to_strings (TREE_TYPE (arg
), arg
);
1411 if (TREE_CODE (arg
) == STRING_CST
)
1412 arg
= build_address (arg
);
1420 bool save_ffbcp
= force_folding_builtin_constant_p
;
1421 force_folding_builtin_constant_p
|= ctx
->manifestly_const_eval
;
1422 tree save_cur_fn
= current_function_decl
;
1423 /* Return name of ctx->call->fundef->decl for __builtin_FUNCTION (). */
1424 if (fndecl_built_in_p (fun
, BUILT_IN_FUNCTION
)
1426 && ctx
->call
->fundef
)
1427 current_function_decl
= ctx
->call
->fundef
->decl
;
1428 if (fndecl_built_in_p (fun
,
1429 CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS
,
1432 location_t loc
= EXPR_LOCATION (t
);
1434 VERIFY_CONSTANT (args
[0]);
1436 = fold_builtin_is_pointer_inverconvertible_with_class (loc
, nargs
,
1439 else if (fndecl_built_in_p (fun
,
1440 CP_BUILT_IN_IS_CORRESPONDING_MEMBER
,
1443 location_t loc
= EXPR_LOCATION (t
);
1446 VERIFY_CONSTANT (args
[0]);
1447 VERIFY_CONSTANT (args
[1]);
1449 new_call
= fold_builtin_is_corresponding_member (loc
, nargs
, args
);
1452 new_call
= fold_builtin_call_array (EXPR_LOCATION (t
), TREE_TYPE (t
),
1453 CALL_EXPR_FN (t
), nargs
, args
);
1454 current_function_decl
= save_cur_fn
;
1455 force_folding_builtin_constant_p
= save_ffbcp
;
1456 if (new_call
== NULL
)
1458 if (!*non_constant_p
&& !ctx
->quiet
)
1460 /* Do not allow__builtin_unreachable in constexpr function.
1461 The __builtin_unreachable call with BUILTINS_LOCATION
1462 comes from cp_maybe_instrument_return. */
1463 if (fndecl_built_in_p (fun
, BUILT_IN_UNREACHABLE
)
1464 && EXPR_LOCATION (t
) == BUILTINS_LOCATION
)
1465 error ("%<constexpr%> call flows off the end of the function");
1468 new_call
= build_call_array_loc (EXPR_LOCATION (t
), TREE_TYPE (t
),
1469 CALL_EXPR_FN (t
), nargs
, args
);
1470 error ("%q+E is not a constant expression", new_call
);
1473 *non_constant_p
= true;
1477 if (!potential_constant_expression (new_call
))
1479 if (!*non_constant_p
&& !ctx
->quiet
)
1480 error ("%q+E is not a constant expression", new_call
);
1481 *non_constant_p
= true;
1487 /* memchr returns a pointer into the first argument, but we replaced the
1488 argument above with a STRING_CST; put it back it now. */
1489 tree op
= CALL_EXPR_ARG (t
, strret
-1);
1490 STRIP_NOPS (new_call
);
1491 if (TREE_CODE (new_call
) == POINTER_PLUS_EXPR
)
1492 TREE_OPERAND (new_call
, 0) = op
;
1493 else if (TREE_CODE (new_call
) == ADDR_EXPR
)
1497 return cxx_eval_constant_expression (&new_ctx
, new_call
, lval
,
1498 non_constant_p
, overflow_p
);
1501 /* TEMP is the constant value of a temporary object of type TYPE. Adjust
1502 the type of the value to match. */
1505 adjust_temp_type (tree type
, tree temp
)
1507 if (same_type_p (TREE_TYPE (temp
), type
))
1509 /* Avoid wrapping an aggregate value in a NOP_EXPR. */
1510 if (TREE_CODE (temp
) == CONSTRUCTOR
)
1512 /* build_constructor wouldn't retain various CONSTRUCTOR flags. */
1513 tree t
= copy_node (temp
);
1514 TREE_TYPE (t
) = type
;
1517 if (TREE_CODE (temp
) == EMPTY_CLASS_EXPR
)
1518 return build0 (EMPTY_CLASS_EXPR
, type
);
1519 gcc_assert (scalarish_type_p (type
));
1520 /* Now we know we're dealing with a scalar, and a prvalue of non-class
1521 type is cv-unqualified. */
1522 return cp_fold_convert (cv_unqualified (type
), temp
);
1525 /* If T is a CONSTRUCTOR, return an unshared copy of T and any
1526 sub-CONSTRUCTORs. Otherwise return T.
1528 We use this whenever we initialize an object as a whole, whether it's a
1529 parameter, a local variable, or a subobject, so that subsequent
1530 modifications don't affect other places where it was used. */
1533 unshare_constructor (tree t MEM_STAT_DECL
)
1535 if (!t
|| TREE_CODE (t
) != CONSTRUCTOR
)
1537 auto_vec
<tree
*, 4> ptrs
;
1538 ptrs
.safe_push (&t
);
1539 while (!ptrs
.is_empty ())
1541 tree
*p
= ptrs
.pop ();
1542 tree n
= copy_node (*p PASS_MEM_STAT
);
1543 CONSTRUCTOR_ELTS (n
) = vec_safe_copy (CONSTRUCTOR_ELTS (*p
) PASS_MEM_STAT
);
1545 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (n
);
1546 constructor_elt
*ce
;
1547 for (HOST_WIDE_INT i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
1548 if (ce
->value
&& TREE_CODE (ce
->value
) == CONSTRUCTOR
)
1549 ptrs
.safe_push (&ce
->value
);
1554 /* If T is a CONSTRUCTOR, ggc_free T and any sub-CONSTRUCTORs. */
1557 free_constructor (tree t
)
1559 if (!t
|| TREE_CODE (t
) != CONSTRUCTOR
)
1561 releasing_vec ctors
;
1562 vec_safe_push (ctors
, t
);
1563 while (!ctors
->is_empty ())
1565 tree c
= ctors
->pop ();
1566 if (vec
<constructor_elt
, va_gc
> *elts
= CONSTRUCTOR_ELTS (c
))
1568 constructor_elt
*ce
;
1569 for (HOST_WIDE_INT i
= 0; vec_safe_iterate (elts
, i
, &ce
); ++i
)
1570 if (TREE_CODE (ce
->value
) == CONSTRUCTOR
)
1571 vec_safe_push (ctors
, ce
->value
);
1578 /* Helper function of cxx_bind_parameters_in_call. Return non-NULL
1579 if *TP is address of a static variable (or part of it) currently being
1580 constructed or of a heap artificial variable. */
1583 addr_of_non_const_var (tree
*tp
, int *walk_subtrees
, void *data
)
1585 if (TREE_CODE (*tp
) == ADDR_EXPR
)
1586 if (tree var
= get_base_address (TREE_OPERAND (*tp
, 0)))
1587 if (VAR_P (var
) && TREE_STATIC (var
))
1589 if (DECL_NAME (var
) == heap_uninit_identifier
1590 || DECL_NAME (var
) == heap_identifier
1591 || DECL_NAME (var
) == heap_vec_uninit_identifier
1592 || DECL_NAME (var
) == heap_vec_identifier
)
1595 constexpr_global_ctx
*global
= (constexpr_global_ctx
*) data
;
1596 if (global
->values
.get (var
))
1600 *walk_subtrees
= false;
1604 /* Subroutine of cxx_eval_call_expression.
1605 We are processing a call expression (either CALL_EXPR or
1606 AGGR_INIT_EXPR) in the context of CTX. Evaluate
1607 all arguments and bind their values to correspondings
1608 parameters, making up the NEW_CALL context. */
1611 cxx_bind_parameters_in_call (const constexpr_ctx
*ctx
, tree t
,
1612 constexpr_call
*new_call
,
1613 bool *non_constant_p
, bool *overflow_p
,
1614 bool *non_constant_args
)
1616 const int nargs
= call_expr_nargs (t
);
1617 tree fun
= new_call
->fundef
->decl
;
1618 tree parms
= new_call
->fundef
->parms
;
1620 /* We don't record ellipsis args below. */
1621 int nparms
= list_length (parms
);
1622 int nbinds
= nargs
< nparms
? nargs
: nparms
;
1623 tree binds
= new_call
->bindings
= make_tree_vec (nbinds
);
1624 for (i
= 0; i
< nargs
; ++i
)
1627 tree type
= parms
? TREE_TYPE (parms
) : void_type_node
;
1628 x
= get_nth_callarg (t
, i
);
1629 /* For member function, the first argument is a pointer to the implied
1630 object. For a constructor, it might still be a dummy object, in
1631 which case we get the real argument from ctx. */
1632 if (i
== 0 && DECL_CONSTRUCTOR_P (fun
)
1633 && is_dummy_object (x
))
1636 x
= build_address (x
);
1638 if (TREE_ADDRESSABLE (type
))
1639 /* Undo convert_for_arg_passing work here. */
1640 x
= convert_from_reference (x
);
1641 /* Normally we would strip a TARGET_EXPR in an initialization context
1642 such as this, but here we do the elision differently: we keep the
1643 TARGET_EXPR, and use its CONSTRUCTOR as the value of the parm. */
1644 arg
= cxx_eval_constant_expression (ctx
, x
, /*lval=*/false,
1645 non_constant_p
, overflow_p
);
1646 /* Don't VERIFY_CONSTANT here. */
1647 if (*non_constant_p
&& ctx
->quiet
)
1649 /* Just discard ellipsis args after checking their constantitude. */
1653 if (!*non_constant_p
)
1655 /* Make sure the binding has the same type as the parm. But
1656 only for constant args. */
1657 if (!TYPE_REF_P (type
))
1658 arg
= adjust_temp_type (type
, arg
);
1659 if (!TREE_CONSTANT (arg
))
1660 *non_constant_args
= true;
1661 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
1662 /* The destructor needs to see any modifications the callee makes
1664 *non_constant_args
= true;
1665 /* If arg is or contains address of a heap artificial variable or
1666 of a static variable being constructed, avoid caching the
1667 function call, as those variables might be modified by the
1668 function, or might be modified by the callers in between
1669 the cached function and just read by the function. */
1670 else if (!*non_constant_args
1671 && cp_walk_tree (&arg
, addr_of_non_const_var
, ctx
->global
,
1673 *non_constant_args
= true;
1675 /* For virtual calls, adjust the this argument, so that it is
1676 the object on which the method is called, rather than
1677 one of its bases. */
1678 if (i
== 0 && DECL_VIRTUAL_P (fun
))
1682 if (TREE_CODE (addr
) == ADDR_EXPR
)
1684 tree obj
= TREE_OPERAND (addr
, 0);
1685 while (TREE_CODE (obj
) == COMPONENT_REF
1686 && DECL_FIELD_IS_BASE (TREE_OPERAND (obj
, 1))
1687 && !same_type_ignoring_top_level_qualifiers_p
1688 (TREE_TYPE (obj
), DECL_CONTEXT (fun
)))
1689 obj
= TREE_OPERAND (obj
, 0);
1690 if (obj
!= TREE_OPERAND (addr
, 0))
1691 arg
= build_fold_addr_expr_with_type (obj
,
1695 TREE_VEC_ELT (binds
, i
) = arg
;
1697 parms
= TREE_CHAIN (parms
);
1701 /* Variables and functions to manage constexpr call expansion context.
1702 These do not need to be marked for PCH or GC. */
1704 /* FIXME remember and print actual constant arguments. */
1705 static vec
<tree
> call_stack
;
1706 static int call_stack_tick
;
1707 static int last_cx_error_tick
;
1710 push_cx_call_context (tree call
)
1713 if (!EXPR_HAS_LOCATION (call
))
1714 SET_EXPR_LOCATION (call
, input_location
);
1715 call_stack
.safe_push (call
);
1716 int len
= call_stack
.length ();
1717 if (len
> max_constexpr_depth
)
1723 pop_cx_call_context (void)
1730 cx_error_context (void)
1732 vec
<tree
> r
= vNULL
;
1733 if (call_stack_tick
!= last_cx_error_tick
1734 && !call_stack
.is_empty ())
1736 last_cx_error_tick
= call_stack_tick
;
1740 /* Evaluate a call T to a GCC internal function when possible and return
1741 the evaluated result or, under the control of CTX, give an error, set
1742 NON_CONSTANT_P, and return the unevaluated call T otherwise. */
1745 cxx_eval_internal_function (const constexpr_ctx
*ctx
, tree t
,
1747 bool *non_constant_p
, bool *overflow_p
)
1749 enum tree_code opcode
= ERROR_MARK
;
1751 switch (CALL_EXPR_IFN (t
))
1753 case IFN_UBSAN_NULL
:
1754 case IFN_UBSAN_BOUNDS
:
1755 case IFN_UBSAN_VPTR
:
1756 case IFN_FALLTHROUGH
:
1759 case IFN_ADD_OVERFLOW
:
1762 case IFN_SUB_OVERFLOW
:
1763 opcode
= MINUS_EXPR
;
1765 case IFN_MUL_OVERFLOW
:
1770 return cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 0),
1771 false, non_constant_p
, overflow_p
);
1773 case IFN_VEC_CONVERT
:
1775 tree arg
= cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 0),
1776 false, non_constant_p
,
1778 if (TREE_CODE (arg
) == VECTOR_CST
)
1779 return fold_const_call (CFN_VEC_CONVERT
, TREE_TYPE (t
), arg
);
1782 *non_constant_p
= true;
1789 error_at (cp_expr_loc_or_input_loc (t
),
1790 "call to internal function %qE", t
);
1791 *non_constant_p
= true;
1795 /* Evaluate constant arguments using OPCODE and return a complex
1796 number containing the result and the overflow bit. */
1797 tree arg0
= cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 0), lval
,
1798 non_constant_p
, overflow_p
);
1799 tree arg1
= cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 1), lval
,
1800 non_constant_p
, overflow_p
);
1802 if (TREE_CODE (arg0
) == INTEGER_CST
&& TREE_CODE (arg1
) == INTEGER_CST
)
1804 location_t loc
= cp_expr_loc_or_input_loc (t
);
1805 tree type
= TREE_TYPE (TREE_TYPE (t
));
1806 tree result
= fold_binary_loc (loc
, opcode
, type
,
1807 fold_convert_loc (loc
, type
, arg0
),
1808 fold_convert_loc (loc
, type
, arg1
));
1810 = build_int_cst (type
, arith_overflowed_p (opcode
, type
, arg0
, arg1
));
1811 /* Reset TREE_OVERFLOW to avoid warnings for the overflow. */
1812 if (TREE_OVERFLOW (result
))
1813 TREE_OVERFLOW (result
) = 0;
1815 return build_complex (TREE_TYPE (t
), result
, ovf
);
1818 *non_constant_p
= true;
1822 /* Clean CONSTRUCTOR_NO_CLEARING from CTOR and its sub-aggregates. */
1825 clear_no_implicit_zero (tree ctor
)
1827 if (CONSTRUCTOR_NO_CLEARING (ctor
))
1829 CONSTRUCTOR_NO_CLEARING (ctor
) = false;
1830 tree elt
; unsigned HOST_WIDE_INT idx
;
1831 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor
), idx
, elt
)
1832 if (TREE_CODE (elt
) == CONSTRUCTOR
)
1833 clear_no_implicit_zero (elt
);
1837 /* Complain about a const object OBJ being modified in a constant expression.
1838 EXPR is the MODIFY_EXPR expression performing the modification. */
1841 modifying_const_object_error (tree expr
, tree obj
)
1843 location_t loc
= cp_expr_loc_or_input_loc (expr
);
1844 auto_diagnostic_group d
;
1845 error_at (loc
, "modifying a const object %qE is not allowed in "
1846 "a constant expression", TREE_OPERAND (expr
, 0));
1847 inform (location_of (obj
), "originally declared %<const%> here");
1850 /* Return true if FNDECL is a replaceable global allocation function that
1851 should be useable during constant expression evaluation. */
1854 cxx_replaceable_global_alloc_fn (tree fndecl
)
1856 return (cxx_dialect
>= cxx20
1857 && IDENTIFIER_NEWDEL_OP_P (DECL_NAME (fndecl
))
1858 && CP_DECL_CONTEXT (fndecl
) == global_namespace
1859 && (DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl
)
1860 || DECL_IS_OPERATOR_DELETE_P (fndecl
)));
1863 /* Return true if FNDECL is a placement new function that should be
1864 useable during constant expression evaluation of std::construct_at. */
1867 cxx_placement_new_fn (tree fndecl
)
1869 if (cxx_dialect
>= cxx20
1870 && IDENTIFIER_NEW_OP_P (DECL_NAME (fndecl
))
1871 && CP_DECL_CONTEXT (fndecl
) == global_namespace
1872 && !DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl
)
1873 && TREE_CODE (TREE_TYPE (fndecl
)) == FUNCTION_TYPE
)
1875 tree first_arg
= TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
1876 if (TREE_VALUE (first_arg
) == ptr_type_node
1877 && TREE_CHAIN (first_arg
) == void_list_node
)
1883 /* Return true if FNDECL is std::construct_at. */
1886 is_std_construct_at (tree fndecl
)
1888 if (!decl_in_std_namespace_p (fndecl
))
1891 tree name
= DECL_NAME (fndecl
);
1892 return name
&& id_equal (name
, "construct_at");
1895 /* Overload for the above taking constexpr_call*. */
1898 is_std_construct_at (const constexpr_call
*call
)
1902 && is_std_construct_at (call
->fundef
->decl
));
1905 /* Return true if FNDECL is std::allocator<T>::{,de}allocate. */
1908 is_std_allocator_allocate (tree fndecl
)
1910 tree name
= DECL_NAME (fndecl
);
1911 if (name
== NULL_TREE
1912 || !(id_equal (name
, "allocate") || id_equal (name
, "deallocate")))
1915 tree ctx
= DECL_CONTEXT (fndecl
);
1916 if (ctx
== NULL_TREE
|| !CLASS_TYPE_P (ctx
) || !TYPE_MAIN_DECL (ctx
))
1919 tree decl
= TYPE_MAIN_DECL (ctx
);
1920 name
= DECL_NAME (decl
);
1921 if (name
== NULL_TREE
|| !id_equal (name
, "allocator"))
1924 return decl_in_std_namespace_p (decl
);
1927 /* Overload for the above taking constexpr_call*. */
1930 is_std_allocator_allocate (const constexpr_call
*call
)
1934 && is_std_allocator_allocate (call
->fundef
->decl
));
1937 /* Return true if FNDECL is __dynamic_cast. */
1940 cxx_dynamic_cast_fn_p (tree fndecl
)
1942 return (cxx_dialect
>= cxx20
1943 && id_equal (DECL_NAME (fndecl
), "__dynamic_cast")
1944 && CP_DECL_CONTEXT (fndecl
) == global_namespace
);
1947 /* Often, we have an expression in the form of address + offset, e.g.
1948 "&_ZTV1A + 16". Extract the object from it, i.e. "_ZTV1A". */
1951 extract_obj_from_addr_offset (tree expr
)
1953 if (TREE_CODE (expr
) == POINTER_PLUS_EXPR
)
1954 expr
= TREE_OPERAND (expr
, 0);
1956 if (TREE_CODE (expr
) == ADDR_EXPR
)
1957 expr
= TREE_OPERAND (expr
, 0);
1961 /* Given a PATH like
1963 g.D.2181.D.2154.D.2102.D.2093
1965 find a component with type TYPE. Return NULL_TREE if not found, and
1966 error_mark_node if the component is not accessible. If STOP is non-null,
1967 this function will return NULL_TREE if STOP is found before TYPE. */
1970 get_component_with_type (tree path
, tree type
, tree stop
)
1974 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path
), type
))
1978 && (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path
),
1981 else if (TREE_CODE (path
) == COMPONENT_REF
1982 && DECL_FIELD_IS_BASE (TREE_OPERAND (path
, 1)))
1984 /* We need to check that the component we're accessing is in fact
1986 if (TREE_PRIVATE (TREE_OPERAND (path
, 1))
1987 || TREE_PROTECTED (TREE_OPERAND (path
, 1)))
1988 return error_mark_node
;
1989 path
= TREE_OPERAND (path
, 0);
1996 /* Evaluate a call to __dynamic_cast (permitted by P1327R1).
1998 The declaration of __dynamic_cast is:
2000 void* __dynamic_cast (const void* __src_ptr,
2001 const __class_type_info* __src_type,
2002 const __class_type_info* __dst_type,
2003 ptrdiff_t __src2dst);
2005 where src2dst has the following possible values
2007 >-1: src_type is a unique public non-virtual base of dst_type
2008 dst_ptr + src2dst == src_ptr
2009 -1: unspecified relationship
2010 -2: src_type is not a public base of dst_type
2011 -3: src_type is a multiple public non-virtual base of dst_type
2013 Since literal types can't have virtual bases, we only expect hint >=0,
2017 cxx_eval_dynamic_cast_fn (const constexpr_ctx
*ctx
, tree call
,
2018 bool *non_constant_p
, bool *overflow_p
)
2020 /* T will be something like
2021 __dynamic_cast ((B*) b, &_ZTI1B, &_ZTI1D, 8)
2023 gcc_assert (call_expr_nargs (call
) == 4);
2024 tsubst_flags_t complain
= ctx
->quiet
? tf_none
: tf_warning_or_error
;
2025 tree obj
= CALL_EXPR_ARG (call
, 0);
2026 tree type
= CALL_EXPR_ARG (call
, 2);
2027 HOST_WIDE_INT hint
= int_cst_value (CALL_EXPR_ARG (call
, 3));
2028 location_t loc
= cp_expr_loc_or_input_loc (call
);
2030 /* Get the target type of the dynamic_cast. */
2031 gcc_assert (TREE_CODE (type
) == ADDR_EXPR
);
2032 type
= TREE_OPERAND (type
, 0);
2033 type
= TREE_TYPE (DECL_NAME (type
));
2035 /* TYPE can only be either T* or T&. We can't know which of these it
2036 is by looking at TYPE, but OBJ will be "(T*) x" in the first case,
2037 and something like "(T*)(T&)(T*) x" in the second case. */
2038 bool reference_p
= false;
2039 while (CONVERT_EXPR_P (obj
) || TREE_CODE (obj
) == SAVE_EXPR
)
2041 reference_p
|= TYPE_REF_P (TREE_TYPE (obj
));
2042 obj
= TREE_OPERAND (obj
, 0);
2045 /* Evaluate the object so that we know its dynamic type. */
2046 obj
= cxx_eval_constant_expression (ctx
, obj
, /*lval*/false, non_constant_p
,
2048 if (*non_constant_p
)
2051 /* We expect OBJ to be in form of &d.D.2102 when HINT == 0,
2052 but when HINT is > 0, it can also be something like
2053 &d.D.2102 + 18446744073709551608, which includes the BINFO_OFFSET. */
2054 obj
= extract_obj_from_addr_offset (obj
);
2055 const tree objtype
= TREE_TYPE (obj
);
2056 /* If OBJ doesn't refer to a base field, we're done. */
2057 if (tree t
= (TREE_CODE (obj
) == COMPONENT_REF
2058 ? TREE_OPERAND (obj
, 1) : obj
))
2059 if (TREE_CODE (t
) != FIELD_DECL
|| !DECL_FIELD_IS_BASE (t
))
2065 error_at (loc
, "reference %<dynamic_cast%> failed");
2066 inform (loc
, "dynamic type %qT of its operand does "
2067 "not have a base class of type %qT",
2070 *non_constant_p
= true;
2072 return integer_zero_node
;
2075 /* [class.cdtor] When a dynamic_cast is used in a constructor ...
2076 or in a destructor ... if the operand of the dynamic_cast refers
2077 to the object under construction or destruction, this object is
2078 considered to be a most derived object that has the type of the
2079 constructor or destructor's class. */
2080 tree vtable
= build_vfield_ref (obj
, objtype
);
2081 vtable
= cxx_eval_constant_expression (ctx
, vtable
, /*lval*/false,
2082 non_constant_p
, overflow_p
);
2083 if (*non_constant_p
)
2085 /* With -fsanitize=vptr, we initialize all vtable pointers to null,
2086 so it's possible that we got a null pointer now. */
2087 if (integer_zerop (vtable
))
2090 error_at (loc
, "virtual table pointer is used uninitialized");
2091 *non_constant_p
= true;
2092 return integer_zero_node
;
2094 /* VTABLE will be &_ZTV1A + 16 or similar, get _ZTV1A. */
2095 vtable
= extract_obj_from_addr_offset (vtable
);
2096 const tree mdtype
= DECL_CONTEXT (vtable
);
2098 /* Given dynamic_cast<T>(v),
2100 [expr.dynamic.cast] If C is the class type to which T points or refers,
2101 the runtime check logically executes as follows:
2103 If, in the most derived object pointed (referred) to by v, v points
2104 (refers) to a public base class subobject of a C object, and if only
2105 one object of type C is derived from the subobject pointed (referred)
2106 to by v the result points (refers) to that C object.
2108 In this case, HINT >= 0 or -3. */
2109 if (hint
>= 0 || hint
== -3)
2111 /* Look for a component with type TYPE. */
2112 tree t
= get_component_with_type (obj
, type
, mdtype
);
2113 /* If not accessible, give an error. */
2114 if (t
== error_mark_node
)
2120 error_at (loc
, "reference %<dynamic_cast%> failed");
2121 inform (loc
, "static type %qT of its operand is a "
2122 "non-public base class of dynamic type %qT",
2126 *non_constant_p
= true;
2128 return integer_zero_node
;
2131 /* The result points to the TYPE object. */
2132 return cp_build_addr_expr (t
, complain
);
2133 /* Else, TYPE was not found, because the HINT turned out to be wrong.
2134 Fall through to the normal processing. */
2137 /* Otherwise, if v points (refers) to a public base class subobject of the
2138 most derived object, and the type of the most derived object has a base
2139 class, of type C, that is unambiguous and public, the result points
2140 (refers) to the C subobject of the most derived object.
2142 But it can also be an invalid case. */
2144 /* Get the most derived object. */
2145 obj
= get_component_with_type (obj
, mdtype
, NULL_TREE
);
2146 if (obj
== error_mark_node
)
2152 error_at (loc
, "reference %<dynamic_cast%> failed");
2153 inform (loc
, "static type %qT of its operand is a non-public"
2154 " base class of dynamic type %qT", objtype
, mdtype
);
2156 *non_constant_p
= true;
2158 return integer_zero_node
;
2163 /* Check that the type of the most derived object has a base class
2164 of type TYPE that is unambiguous and public. */
2166 tree binfo
= lookup_base (mdtype
, type
, ba_check
, &b_kind
, tf_none
);
2167 if (!binfo
|| binfo
== error_mark_node
)
2173 error_at (loc
, "reference %<dynamic_cast%> failed");
2174 if (b_kind
== bk_ambig
)
2175 inform (loc
, "%qT is an ambiguous base class of dynamic "
2176 "type %qT of its operand", type
, mdtype
);
2178 inform (loc
, "dynamic type %qT of its operand does not "
2179 "have an unambiguous public base class %qT",
2182 *non_constant_p
= true;
2184 return integer_zero_node
;
2186 /* If so, return the TYPE subobject of the most derived object. */
2187 obj
= convert_to_base_statically (obj
, binfo
);
2188 return cp_build_addr_expr (obj
, complain
);
2191 /* Data structure used by replace_result_decl and replace_result_decl_r. */
2193 struct replace_result_decl_data
2195 /* The RESULT_DECL we want to replace. */
2197 /* The replacement for DECL. */
2199 /* Whether we've performed any replacements. */
2203 /* Helper function for replace_result_decl, called through cp_walk_tree. */
2206 replace_result_decl_r (tree
*tp
, int *walk_subtrees
, void *data
)
2208 replace_result_decl_data
*d
= (replace_result_decl_data
*) data
;
2212 *tp
= unshare_expr (d
->replacement
);
2216 else if (TYPE_P (*tp
))
2222 /* Replace every occurrence of DECL, a RESULT_DECL, with (an unshared copy of)
2223 REPLACEMENT within the reduced constant expression *TP. Returns true iff a
2224 replacement was performed. */
2227 replace_result_decl (tree
*tp
, tree decl
, tree replacement
)
2229 gcc_checking_assert (TREE_CODE (decl
) == RESULT_DECL
2230 && (same_type_ignoring_top_level_qualifiers_p
2231 (TREE_TYPE (decl
), TREE_TYPE (replacement
))));
2232 replace_result_decl_data data
= { decl
, replacement
, false };
2233 cp_walk_tree_without_duplicates (tp
, replace_result_decl_r
, &data
);
2234 return data
.changed
;
2237 /* Evaluate the call T to virtual function thunk THUNK_FNDECL. */
2240 cxx_eval_thunk_call (const constexpr_ctx
*ctx
, tree t
, tree thunk_fndecl
,
2242 bool *non_constant_p
, bool *overflow_p
)
2244 tree function
= THUNK_TARGET (thunk_fndecl
);
2246 /* virtual_offset is only set in the presence of virtual bases, which make
2247 the class non-literal, so we don't need to handle it here. */
2248 if (THUNK_VIRTUAL_OFFSET (thunk_fndecl
))
2250 gcc_assert (!DECL_DECLARED_CONSTEXPR_P (function
));
2253 error ("call to non-%<constexpr%> function %qD", function
);
2254 explain_invalid_constexpr_fn (function
);
2256 *non_constant_p
= true;
2260 tree new_call
= copy_node (t
);
2261 CALL_EXPR_FN (new_call
) = function
;
2262 TREE_TYPE (new_call
) = TREE_TYPE (TREE_TYPE (function
));
2264 tree offset
= size_int (THUNK_FIXED_OFFSET (thunk_fndecl
));
2266 if (DECL_THIS_THUNK_P (thunk_fndecl
))
2268 /* 'this'-adjusting thunk. */
2269 tree this_arg
= CALL_EXPR_ARG (t
, 0);
2270 this_arg
= build2 (POINTER_PLUS_EXPR
, TREE_TYPE (this_arg
),
2272 CALL_EXPR_ARG (new_call
, 0) = this_arg
;
2275 /* Return-adjusting thunk. */
2276 new_call
= build2 (POINTER_PLUS_EXPR
, TREE_TYPE (new_call
),
2279 return cxx_eval_constant_expression (ctx
, new_call
, lval
,
2280 non_constant_p
, overflow_p
);
2283 /* If OBJECT is of const class type, evaluate it to a CONSTRUCTOR and set
2284 its TREE_READONLY flag according to READONLY_P. Used for constexpr
2285 'tors to detect modifying const objects in a constexpr context. */
2288 cxx_set_object_constness (const constexpr_ctx
*ctx
, tree object
,
2289 bool readonly_p
, bool *non_constant_p
,
2292 if (CLASS_TYPE_P (TREE_TYPE (object
))
2293 && CP_TYPE_CONST_P (TREE_TYPE (object
)))
2295 /* Subobjects might not be stored in ctx->global->values but we
2296 can get its CONSTRUCTOR by evaluating *this. */
2297 tree e
= cxx_eval_constant_expression (ctx
, object
, /*lval*/false,
2298 non_constant_p
, overflow_p
);
2299 if (TREE_CODE (e
) == CONSTRUCTOR
&& !*non_constant_p
)
2300 TREE_READONLY (e
) = readonly_p
;
2304 /* Subroutine of cxx_eval_constant_expression.
2305 Evaluate the call expression tree T in the context of OLD_CALL expression
2309 cxx_eval_call_expression (const constexpr_ctx
*ctx
, tree t
,
2311 bool *non_constant_p
, bool *overflow_p
)
2313 /* Handle concept checks separately. */
2314 if (concept_check_p (t
))
2315 return evaluate_concept_check (t
);
2317 location_t loc
= cp_expr_loc_or_input_loc (t
);
2318 tree fun
= get_function_named_in_call (t
);
2319 constexpr_call new_call
2320 = { NULL
, NULL
, NULL
, 0, ctx
->manifestly_const_eval
};
2323 if (fun
== NULL_TREE
)
2324 return cxx_eval_internal_function (ctx
, t
, lval
,
2325 non_constant_p
, overflow_p
);
2327 if (TREE_CODE (fun
) != FUNCTION_DECL
)
2329 /* Might be a constexpr function pointer. */
2330 fun
= cxx_eval_constant_expression (ctx
, fun
,
2331 /*lval*/false, non_constant_p
,
2334 if (TREE_CODE (fun
) == ADDR_EXPR
)
2335 fun
= TREE_OPERAND (fun
, 0);
2336 /* For TARGET_VTABLE_USES_DESCRIPTORS targets, there is no
2337 indirection, the called expression is a pointer into the
2338 virtual table which should contain FDESC_EXPR. Extract the
2339 FUNCTION_DECL from there. */
2340 else if (TARGET_VTABLE_USES_DESCRIPTORS
2341 && TREE_CODE (fun
) == POINTER_PLUS_EXPR
2342 && TREE_CODE (TREE_OPERAND (fun
, 0)) == ADDR_EXPR
2343 && TREE_CODE (TREE_OPERAND (fun
, 1)) == INTEGER_CST
)
2345 tree d
= TREE_OPERAND (TREE_OPERAND (fun
, 0), 0);
2347 && DECL_VTABLE_OR_VTT_P (d
)
2348 && TREE_CODE (TREE_TYPE (d
)) == ARRAY_TYPE
2349 && TREE_TYPE (TREE_TYPE (d
)) == vtable_entry_type
2351 && TREE_CODE (DECL_INITIAL (d
)) == CONSTRUCTOR
)
2353 tree i
= int_const_binop (TRUNC_DIV_EXPR
, TREE_OPERAND (fun
, 1),
2354 TYPE_SIZE_UNIT (vtable_entry_type
));
2355 HOST_WIDE_INT idx
= find_array_ctor_elt (DECL_INITIAL (d
), i
);
2359 = (*CONSTRUCTOR_ELTS (DECL_INITIAL (d
)))[idx
].value
;
2360 if (TREE_CODE (fdesc
) == FDESC_EXPR
2361 && integer_zerop (TREE_OPERAND (fdesc
, 1)))
2362 fun
= TREE_OPERAND (fdesc
, 0);
2367 if (TREE_CODE (fun
) != FUNCTION_DECL
)
2369 if (!ctx
->quiet
&& !*non_constant_p
)
2370 error_at (loc
, "expression %qE does not designate a %<constexpr%> "
2372 *non_constant_p
= true;
2375 if (DECL_CLONED_FUNCTION_P (fun
) && !DECL_DELETING_DESTRUCTOR_P (fun
))
2376 fun
= DECL_CLONED_FUNCTION (fun
);
2378 if (is_ubsan_builtin_p (fun
))
2381 if (fndecl_built_in_p (fun
))
2382 return cxx_eval_builtin_function_call (ctx
, t
, fun
,
2383 lval
, non_constant_p
, overflow_p
);
2384 if (DECL_THUNK_P (fun
))
2385 return cxx_eval_thunk_call (ctx
, t
, fun
, lval
, non_constant_p
, overflow_p
);
2386 if (!DECL_DECLARED_CONSTEXPR_P (fun
))
2388 if (TREE_CODE (t
) == CALL_EXPR
2389 && cxx_replaceable_global_alloc_fn (fun
)
2390 && (CALL_FROM_NEW_OR_DELETE_P (t
)
2391 || is_std_allocator_allocate (ctx
->call
)))
2393 const int nargs
= call_expr_nargs (t
);
2394 tree arg0
= NULL_TREE
;
2395 for (int i
= 0; i
< nargs
; ++i
)
2397 tree arg
= CALL_EXPR_ARG (t
, i
);
2398 arg
= cxx_eval_constant_expression (ctx
, arg
, false,
2399 non_constant_p
, overflow_p
);
2400 VERIFY_CONSTANT (arg
);
2405 if (IDENTIFIER_NEW_OP_P (DECL_NAME (fun
)))
2407 tree type
= build_array_type_nelts (char_type_node
,
2408 tree_to_uhwi (arg0
));
2409 tree var
= build_decl (loc
, VAR_DECL
,
2410 (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun
))
2412 ? heap_vec_uninit_identifier
2413 : heap_uninit_identifier
,
2415 DECL_ARTIFICIAL (var
) = 1;
2416 TREE_STATIC (var
) = 1;
2417 // Temporarily register the artificial var in varpool,
2418 // so that comparisons of its address against NULL are folded
2419 // through nonzero_address even with
2420 // -fno-delete-null-pointer-checks or that comparison of
2421 // addresses of different heap artificial vars is folded too.
2422 // See PR98988 and PR99031.
2423 varpool_node::finalize_decl (var
);
2424 ctx
->global
->heap_vars
.safe_push (var
);
2425 ctx
->global
->values
.put (var
, NULL_TREE
);
2426 return fold_convert (ptr_type_node
, build_address (var
));
2431 if (TREE_CODE (arg0
) == ADDR_EXPR
2432 && VAR_P (TREE_OPERAND (arg0
, 0)))
2434 tree var
= TREE_OPERAND (arg0
, 0);
2435 if (DECL_NAME (var
) == heap_uninit_identifier
2436 || DECL_NAME (var
) == heap_identifier
)
2438 if (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun
))
2443 error_at (loc
, "array deallocation of object "
2444 "allocated with non-array "
2446 inform (DECL_SOURCE_LOCATION (var
),
2447 "allocation performed here");
2449 *non_constant_p
= true;
2452 DECL_NAME (var
) = heap_deleted_identifier
;
2453 ctx
->global
->values
.remove (var
);
2454 ctx
->global
->heap_dealloc_count
++;
2457 else if (DECL_NAME (var
) == heap_vec_uninit_identifier
2458 || DECL_NAME (var
) == heap_vec_identifier
)
2460 if ((IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun
))
2461 & OVL_OP_FLAG_VEC
) == 0)
2465 error_at (loc
, "non-array deallocation of "
2466 "object allocated with array "
2468 inform (DECL_SOURCE_LOCATION (var
),
2469 "allocation performed here");
2471 *non_constant_p
= true;
2474 DECL_NAME (var
) = heap_deleted_identifier
;
2475 ctx
->global
->values
.remove (var
);
2476 ctx
->global
->heap_dealloc_count
++;
2479 else if (DECL_NAME (var
) == heap_deleted_identifier
)
2482 error_at (loc
, "deallocation of already deallocated "
2484 *non_constant_p
= true;
2489 error_at (loc
, "deallocation of storage that was "
2490 "not previously allocated");
2491 *non_constant_p
= true;
2495 /* Allow placement new in std::construct_at, just return the second
2497 if (TREE_CODE (t
) == CALL_EXPR
2498 && cxx_placement_new_fn (fun
)
2499 && is_std_construct_at (ctx
->call
))
2501 const int nargs
= call_expr_nargs (t
);
2502 tree arg1
= NULL_TREE
;
2503 for (int i
= 0; i
< nargs
; ++i
)
2505 tree arg
= CALL_EXPR_ARG (t
, i
);
2506 arg
= cxx_eval_constant_expression (ctx
, arg
, false,
2507 non_constant_p
, overflow_p
);
2511 VERIFY_CONSTANT (arg
);
2516 else if (cxx_dynamic_cast_fn_p (fun
))
2517 return cxx_eval_dynamic_cast_fn (ctx
, t
, non_constant_p
, overflow_p
);
2521 if (!lambda_static_thunk_p (fun
))
2522 error_at (loc
, "call to non-%<constexpr%> function %qD", fun
);
2523 explain_invalid_constexpr_fn (fun
);
2525 *non_constant_p
= true;
2529 constexpr_ctx new_ctx
= *ctx
;
2530 if (DECL_CONSTRUCTOR_P (fun
) && !ctx
->object
2531 && TREE_CODE (t
) == AGGR_INIT_EXPR
)
2533 /* We want to have an initialization target for an AGGR_INIT_EXPR.
2534 If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
2535 new_ctx
.object
= AGGR_INIT_EXPR_SLOT (t
);
2536 tree ctor
= new_ctx
.ctor
= build_constructor (DECL_CONTEXT (fun
), NULL
);
2537 CONSTRUCTOR_NO_CLEARING (ctor
) = true;
2538 ctx
->global
->values
.put (new_ctx
.object
, ctor
);
2542 /* Shortcut trivial constructor/op=. */
2543 if (trivial_fn_p (fun
))
2545 tree init
= NULL_TREE
;
2546 if (call_expr_nargs (t
) == 2)
2547 init
= convert_from_reference (get_nth_callarg (t
, 1));
2548 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
2549 && AGGR_INIT_ZERO_FIRST (t
))
2550 init
= build_zero_init (DECL_CONTEXT (fun
), NULL_TREE
, false);
2553 tree op
= get_nth_callarg (t
, 0);
2554 if (is_dummy_object (op
))
2557 op
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (op
)), op
);
2558 tree set
= build2 (MODIFY_EXPR
, TREE_TYPE (op
), op
, init
);
2559 new_ctx
.call
= &new_call
;
2560 return cxx_eval_constant_expression (&new_ctx
, set
, lval
,
2561 non_constant_p
, overflow_p
);
2565 /* We can't defer instantiating the function any longer. */
2566 if (!DECL_INITIAL (fun
)
2567 && DECL_TEMPLOID_INSTANTIATION (fun
)
2568 && !uid_sensitive_constexpr_evaluation_p ())
2570 location_t save_loc
= input_location
;
2571 input_location
= loc
;
2573 if (ctx
->manifestly_const_eval
)
2574 FNDECL_MANIFESTLY_CONST_EVALUATED (fun
) = true;
2575 instantiate_decl (fun
, /*defer_ok*/false, /*expl_inst*/false);
2577 input_location
= save_loc
;
2580 /* If in direct recursive call, optimize definition search. */
2581 if (ctx
&& ctx
->call
&& ctx
->call
->fundef
&& ctx
->call
->fundef
->decl
== fun
)
2582 new_call
.fundef
= ctx
->call
->fundef
;
2585 new_call
.fundef
= retrieve_constexpr_fundef (fun
);
2586 if (new_call
.fundef
== NULL
|| new_call
.fundef
->body
== NULL
2587 || new_call
.fundef
->result
== error_mark_node
2588 || fun
== current_function_decl
)
2592 /* We need to check for current_function_decl here in case we're
2593 being called during cp_fold_function, because at that point
2594 DECL_INITIAL is set properly and we have a fundef but we
2595 haven't lowered invisirefs yet (c++/70344). */
2596 if (DECL_INITIAL (fun
) == error_mark_node
2597 || fun
== current_function_decl
)
2598 error_at (loc
, "%qD called in a constant expression before its "
2599 "definition is complete", fun
);
2600 else if (DECL_INITIAL (fun
))
2602 /* The definition of fun was somehow unsuitable. But pretend
2603 that lambda static thunks don't exist. */
2604 if (!lambda_static_thunk_p (fun
))
2605 error_at (loc
, "%qD called in a constant expression", fun
);
2606 explain_invalid_constexpr_fn (fun
);
2609 error_at (loc
, "%qD used before its definition", fun
);
2611 *non_constant_p
= true;
2616 bool non_constant_args
= false;
2617 cxx_bind_parameters_in_call (ctx
, t
, &new_call
,
2618 non_constant_p
, overflow_p
, &non_constant_args
);
2620 /* We build up the bindings list before we know whether we already have this
2621 call cached. If we don't end up saving these bindings, ggc_free them when
2622 this function exits. */
2627 free_bindings (tree
&b
): bindings (&b
) { }
2628 ~free_bindings () { if (bindings
) ggc_free (*bindings
); }
2629 void preserve () { bindings
= NULL
; }
2630 } fb (new_call
.bindings
);
2632 if (*non_constant_p
)
2635 depth_ok
= push_cx_call_context (t
);
2637 /* Remember the object we are constructing or destructing. */
2638 tree new_obj
= NULL_TREE
;
2639 if (DECL_CONSTRUCTOR_P (fun
) || DECL_DESTRUCTOR_P (fun
))
2641 /* In a cdtor, it should be the first `this' argument.
2642 At this point it has already been evaluated in the call
2643 to cxx_bind_parameters_in_call. */
2644 new_obj
= TREE_VEC_ELT (new_call
.bindings
, 0);
2645 STRIP_NOPS (new_obj
);
2646 if (TREE_CODE (new_obj
) == ADDR_EXPR
)
2647 new_obj
= TREE_OPERAND (new_obj
, 0);
2649 if (ctx
->call
&& ctx
->call
->fundef
2650 && DECL_CONSTRUCTOR_P (ctx
->call
->fundef
->decl
))
2652 tree cur_obj
= TREE_VEC_ELT (ctx
->call
->bindings
, 0);
2653 STRIP_NOPS (cur_obj
);
2654 if (TREE_CODE (cur_obj
) == ADDR_EXPR
)
2655 cur_obj
= TREE_OPERAND (cur_obj
, 0);
2656 if (new_obj
== cur_obj
)
2657 /* We're calling the target constructor of a delegating
2658 constructor, or accessing a base subobject through a
2659 NOP_EXPR as part of a call to a base constructor, so
2660 there is no new (sub)object. */
2661 new_obj
= NULL_TREE
;
2665 tree result
= NULL_TREE
;
2667 constexpr_call
*entry
= NULL
;
2668 if (depth_ok
&& !non_constant_args
&& ctx
->strict
)
2670 new_call
.hash
= constexpr_fundef_hasher::hash (new_call
.fundef
);
2672 = iterative_hash_template_arg (new_call
.bindings
, new_call
.hash
);
2674 = iterative_hash_object (ctx
->manifestly_const_eval
, new_call
.hash
);
2676 /* If we have seen this call before, we are done. */
2677 maybe_initialize_constexpr_call_table ();
2678 constexpr_call
**slot
2679 = constexpr_call_table
->find_slot (&new_call
, INSERT
);
2683 /* Only cache up to constexpr_cache_depth to limit memory use. */
2684 if (depth_ok
< constexpr_cache_depth
)
2686 /* We need to keep a pointer to the entry, not just the slot, as
2687 the slot can move during evaluation of the body. */
2688 *slot
= entry
= ggc_alloc
<constexpr_call
> ();
2693 /* Calls that are in progress have their result set to NULL, so that we
2694 can detect circular dependencies. Now that we only cache up to
2695 constexpr_cache_depth this won't catch circular dependencies that
2696 start deeper, but they'll hit the recursion or ops limit. */
2697 else if (entry
->result
== NULL
)
2700 error ("call has circular dependency");
2701 *non_constant_p
= true;
2702 entry
->result
= result
= error_mark_node
;
2705 result
= entry
->result
;
2711 error ("%<constexpr%> evaluation depth exceeds maximum of %d (use "
2712 "%<-fconstexpr-depth=%> to increase the maximum)",
2713 max_constexpr_depth
);
2714 *non_constant_p
= true;
2715 result
= error_mark_node
;
2719 bool cacheable
= true;
2720 if (result
&& result
!= error_mark_node
)
2722 else if (!DECL_SAVED_TREE (fun
))
2724 /* When at_eof >= 2, cgraph has started throwing away
2725 DECL_SAVED_TREE, so fail quietly. FIXME we get here because of
2726 late code generation for VEC_INIT_EXPR, which needs to be
2727 completely reconsidered. */
2728 gcc_assert (at_eof
>= 2 && ctx
->quiet
);
2729 *non_constant_p
= true;
2731 else if (tree copy
= get_fundef_copy (new_call
.fundef
))
2733 tree body
, parms
, res
;
2734 releasing_vec ctors
;
2736 /* Reuse or create a new unshared copy of this function's body. */
2737 body
= TREE_PURPOSE (copy
);
2738 parms
= TREE_VALUE (copy
);
2739 res
= TREE_TYPE (copy
);
2741 /* Associate the bindings with the remapped parms. */
2742 tree bound
= new_call
.bindings
;
2743 tree remapped
= parms
;
2744 for (int i
= 0; i
< TREE_VEC_LENGTH (bound
); ++i
)
2746 tree arg
= TREE_VEC_ELT (bound
, i
);
2749 /* Unshare args going into the hash table to separate them
2750 from the caller's context, for better GC and to avoid
2751 problems with verify_gimple. */
2752 arg
= unshare_expr_without_location (arg
);
2753 TREE_VEC_ELT (bound
, i
) = arg
;
2755 /* And then unshare again so the callee doesn't change the
2756 argument values in the hash table. XXX Could we unshare
2757 lazily in cxx_eval_store_expression? */
2758 arg
= unshare_constructor (arg
);
2759 if (TREE_CODE (arg
) == CONSTRUCTOR
)
2760 vec_safe_push (ctors
, arg
);
2762 ctx
->global
->values
.put (remapped
, arg
);
2763 remapped
= DECL_CHAIN (remapped
);
2765 /* Add the RESULT_DECL to the values map, too. */
2766 gcc_assert (!DECL_BY_REFERENCE (res
));
2767 ctx
->global
->values
.put (res
, NULL_TREE
);
2769 /* Track the callee's evaluated SAVE_EXPRs and TARGET_EXPRs so that
2770 we can forget their values after the call. */
2771 constexpr_ctx ctx_with_save_exprs
= *ctx
;
2772 auto_vec
<tree
, 10> save_exprs
;
2773 ctx_with_save_exprs
.save_exprs
= &save_exprs
;
2774 ctx_with_save_exprs
.call
= &new_call
;
2775 unsigned save_heap_alloc_count
= ctx
->global
->heap_vars
.length ();
2776 unsigned save_heap_dealloc_count
= ctx
->global
->heap_dealloc_count
;
2778 /* If this is a constexpr destructor, the object's const and volatile
2779 semantics are no longer in effect; see [class.dtor]p5. */
2780 if (new_obj
&& DECL_DESTRUCTOR_P (fun
))
2781 cxx_set_object_constness (ctx
, new_obj
, /*readonly_p=*/false,
2782 non_constant_p
, overflow_p
);
2784 tree jump_target
= NULL_TREE
;
2785 cxx_eval_constant_expression (&ctx_with_save_exprs
, body
,
2786 lval
, non_constant_p
, overflow_p
,
2789 if (DECL_CONSTRUCTOR_P (fun
))
2791 /* This can be null for a subobject constructor call, in
2792 which case what we care about is the initialization
2793 side-effects rather than the value. We could get at the
2794 value by evaluating *this, but we don't bother; there's
2795 no need to put such a call in the hash table. */
2796 result
= lval
? ctx
->object
: ctx
->ctor
;
2798 /* If we've just evaluated a subobject constructor call for an
2799 empty union member, it might not have produced a side effect
2800 that actually activated the union member. So produce such a
2801 side effect now to ensure the union appears initialized. */
2802 if (!result
&& new_obj
2803 && TREE_CODE (new_obj
) == COMPONENT_REF
2804 && TREE_CODE (TREE_TYPE
2805 (TREE_OPERAND (new_obj
, 0))) == UNION_TYPE
2806 && is_really_empty_class (TREE_TYPE (new_obj
),
2807 /*ignore_vptr*/false))
2809 tree activate
= build2 (MODIFY_EXPR
, TREE_TYPE (new_obj
),
2811 build_constructor (TREE_TYPE (new_obj
),
2813 cxx_eval_constant_expression (ctx
, activate
, lval
,
2814 non_constant_p
, overflow_p
);
2815 ggc_free (activate
);
2818 else if (VOID_TYPE_P (TREE_TYPE (res
)))
2822 result
= *ctx
->global
->values
.get (res
);
2823 if (result
== NULL_TREE
&& !*non_constant_p
)
2826 error ("%<constexpr%> call flows off the end "
2828 *non_constant_p
= true;
2832 /* At this point, the object's constructor will have run, so
2833 the object is no longer under construction, and its possible
2834 'const' semantics now apply. Make a note of this fact by
2835 marking the CONSTRUCTOR TREE_READONLY. */
2836 if (new_obj
&& DECL_CONSTRUCTOR_P (fun
))
2837 cxx_set_object_constness (ctx
, new_obj
, /*readonly_p=*/true,
2838 non_constant_p
, overflow_p
);
2840 /* Forget the saved values of the callee's SAVE_EXPRs and
2842 for (tree save_expr
: save_exprs
)
2843 ctx
->global
->values
.remove (save_expr
);
2845 /* Remove the parms/result from the values map. Is it worth
2846 bothering to do this when the map itself is only live for
2847 one constexpr evaluation? If so, maybe also clear out
2848 other vars from call, maybe in BIND_EXPR handling? */
2849 ctx
->global
->values
.remove (res
);
2850 for (tree parm
= parms
; parm
; parm
= TREE_CHAIN (parm
))
2851 ctx
->global
->values
.remove (parm
);
2853 /* Free any parameter CONSTRUCTORs we aren't returning directly. */
2854 while (!ctors
->is_empty ())
2856 tree c
= ctors
->pop ();
2858 free_constructor (c
);
2861 /* Make the unshared function copy we used available for re-use. */
2862 save_fundef_copy (fun
, copy
);
2864 /* If the call allocated some heap object that hasn't been
2865 deallocated during the call, or if it deallocated some heap
2866 object it has not allocated, the call isn't really stateless
2867 for the constexpr evaluation and should not be cached.
2868 It is fine if the call allocates something and deallocates it
2871 && (save_heap_alloc_count
!= ctx
->global
->heap_vars
.length ()
2872 || (save_heap_dealloc_count
2873 != ctx
->global
->heap_dealloc_count
)))
2877 if ((ctx
->global
->heap_vars
.length ()
2878 - ctx
->global
->heap_dealloc_count
)
2879 != save_heap_alloc_count
- save_heap_dealloc_count
)
2882 FOR_EACH_VEC_ELT_FROM (ctx
->global
->heap_vars
, i
, heap_var
,
2883 save_heap_alloc_count
)
2884 if (DECL_NAME (heap_var
) != heap_deleted_identifier
)
2891 /* Rewrite all occurrences of the function's RESULT_DECL with the
2892 current object under construction. */
2893 if (!*non_constant_p
&& ctx
->object
2894 && CLASS_TYPE_P (TREE_TYPE (res
))
2895 && !is_empty_class (TREE_TYPE (res
)))
2896 if (replace_result_decl (&result
, res
, ctx
->object
))
2900 /* Couldn't get a function copy to evaluate. */
2901 *non_constant_p
= true;
2903 if (result
== error_mark_node
)
2904 *non_constant_p
= true;
2905 if (*non_constant_p
|| *overflow_p
)
2906 result
= error_mark_node
;
2910 entry
->result
= cacheable
? result
: error_mark_node
;
2913 /* The result of a constexpr function must be completely initialized.
2915 However, in C++20, a constexpr constructor doesn't necessarily have
2916 to initialize all the fields, so we don't clear CONSTRUCTOR_NO_CLEARING
2917 in order to detect reading an unitialized object in constexpr instead
2918 of value-initializing it. (reduced_constant_expression_p is expected to
2919 take care of clearing the flag.) */
2920 if (TREE_CODE (result
) == CONSTRUCTOR
2921 && (cxx_dialect
< cxx20
2922 || !DECL_CONSTRUCTOR_P (fun
)))
2923 clear_no_implicit_zero (result
);
2925 pop_cx_call_context ();
2929 /* Return true if T is a valid constant initializer. If a CONSTRUCTOR
2930 initializes all the members, the CONSTRUCTOR_NO_CLEARING flag will be
2932 FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
2935 reduced_constant_expression_p (tree t
)
2940 switch (TREE_CODE (t
))
2943 /* Even if we can't lower this yet, it's constant. */
2947 /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
2948 tree idx
, val
, field
; unsigned HOST_WIDE_INT i
;
2949 if (CONSTRUCTOR_NO_CLEARING (t
))
2951 if (TREE_CODE (TREE_TYPE (t
)) == VECTOR_TYPE
)
2952 /* An initialized vector would have a VECTOR_CST. */
2954 else if (cxx_dialect
>= cxx20
2955 && TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
2957 /* There must be a valid constant initializer at every array
2959 tree min
= TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (t
)));
2960 tree max
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (t
)));
2962 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t
), i
, idx
, val
)
2964 if (!reduced_constant_expression_p (val
))
2966 if (array_index_cmp (cursor
, idx
) != 0)
2968 if (TREE_CODE (idx
) == RANGE_EXPR
)
2969 cursor
= TREE_OPERAND (idx
, 1);
2970 cursor
= int_const_binop (PLUS_EXPR
, cursor
, size_one_node
);
2972 if (find_array_ctor_elt (t
, max
) == -1)
2976 else if (cxx_dialect
>= cxx20
2977 && TREE_CODE (TREE_TYPE (t
)) == UNION_TYPE
)
2979 if (CONSTRUCTOR_NELTS (t
) == 0)
2980 /* An initialized union has a constructor element. */
2982 /* And it only initializes one member. */
2986 field
= next_initializable_field (TYPE_FIELDS (TREE_TYPE (t
)));
2990 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t
), i
, idx
, val
)
2992 /* If VAL is null, we're in the middle of initializing this
2994 if (!reduced_constant_expression_p (val
))
2996 /* Empty class field may or may not have an initializer. */
2997 for (; field
&& idx
!= field
;
2998 field
= next_initializable_field (DECL_CHAIN (field
)))
2999 if (!is_really_empty_class (TREE_TYPE (field
),
3000 /*ignore_vptr*/false))
3003 field
= next_initializable_field (DECL_CHAIN (field
));
3005 /* There could be a non-empty field at the end. */
3006 for (; field
; field
= next_initializable_field (DECL_CHAIN (field
)))
3007 if (!is_really_empty_class (TREE_TYPE (field
), /*ignore_vptr*/false))
3010 if (CONSTRUCTOR_NO_CLEARING (t
))
3011 /* All the fields are initialized. */
3012 CONSTRUCTOR_NO_CLEARING (t
) = false;
3016 /* FIXME are we calling this too much? */
3017 return initializer_constant_valid_p (t
, TREE_TYPE (t
)) != NULL_TREE
;
3021 /* Some expressions may have constant operands but are not constant
3022 themselves, such as 1/0. Call this function to check for that
3025 We only call this in places that require an arithmetic constant, not in
3026 places where we might have a non-constant expression that can be a
3027 component of a constant expression, such as the address of a constexpr
3028 variable that might be dereferenced later. */
3031 verify_constant (tree t
, bool allow_non_constant
, bool *non_constant_p
,
3034 if (!*non_constant_p
&& !reduced_constant_expression_p (t
)
3037 if (!allow_non_constant
)
3038 error ("%q+E is not a constant expression", t
);
3039 *non_constant_p
= true;
3041 if (TREE_OVERFLOW_P (t
))
3043 if (!allow_non_constant
)
3045 permerror (input_location
, "overflow in constant expression");
3046 /* If we're being permissive (and are in an enforcing
3047 context), ignore the overflow. */
3048 if (flag_permissive
)
3049 return *non_constant_p
;
3053 return *non_constant_p
;
3056 /* Check whether the shift operation with code CODE and type TYPE on LHS
3057 and RHS is undefined. If it is, give an error with an explanation,
3058 and return true; return false otherwise. */
3061 cxx_eval_check_shift_p (location_t loc
, const constexpr_ctx
*ctx
,
3062 enum tree_code code
, tree type
, tree lhs
, tree rhs
)
3064 if ((code
!= LSHIFT_EXPR
&& code
!= RSHIFT_EXPR
)
3065 || TREE_CODE (lhs
) != INTEGER_CST
3066 || TREE_CODE (rhs
) != INTEGER_CST
)
3069 tree lhstype
= TREE_TYPE (lhs
);
3070 unsigned HOST_WIDE_INT uprec
= TYPE_PRECISION (TREE_TYPE (lhs
));
3072 /* [expr.shift] The behavior is undefined if the right operand
3073 is negative, or greater than or equal to the length in bits
3074 of the promoted left operand. */
3075 if (tree_int_cst_sgn (rhs
) == -1)
3078 permerror (loc
, "right operand of shift expression %q+E is negative",
3079 build2_loc (loc
, code
, type
, lhs
, rhs
));
3080 return (!flag_permissive
|| ctx
->quiet
);
3082 if (compare_tree_int (rhs
, uprec
) >= 0)
3085 permerror (loc
, "right operand of shift expression %q+E is greater "
3086 "than or equal to the precision %wu of the left operand",
3087 build2_loc (loc
, code
, type
, lhs
, rhs
), uprec
);
3088 return (!flag_permissive
|| ctx
->quiet
);
3091 /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
3092 if E1 has a signed type and non-negative value, and E1x2^E2 is
3093 representable in the corresponding unsigned type of the result type,
3094 then that value, converted to the result type, is the resulting value;
3095 otherwise, the behavior is undefined.
3097 The value of E1 << E2 is the unique value congruent to E1 x 2^E2 modulo
3098 2^N, where N is the range exponent of the type of the result. */
3099 if (code
== LSHIFT_EXPR
3100 && !TYPE_UNSIGNED (lhstype
)
3101 && cxx_dialect
>= cxx11
3102 && cxx_dialect
< cxx20
)
3104 if (tree_int_cst_sgn (lhs
) == -1)
3108 "left operand of shift expression %q+E is negative",
3109 build2_loc (loc
, code
, type
, lhs
, rhs
));
3110 return (!flag_permissive
|| ctx
->quiet
);
3112 /* For signed x << y the following:
3113 (unsigned) x >> ((prec (lhs) - 1) - y)
3114 if > 1, is undefined. The right-hand side of this formula
3115 is the highest bit of the LHS that can be set (starting from 0),
3116 so that the shift doesn't overflow. We then right-shift the LHS
3117 to see whether any other bit is set making the original shift
3118 undefined -- the result is not representable in the corresponding
3120 tree t
= build_int_cst (unsigned_type_node
, uprec
- 1);
3121 t
= fold_build2 (MINUS_EXPR
, unsigned_type_node
, t
, rhs
);
3122 tree ulhs
= fold_convert (unsigned_type_for (lhstype
), lhs
);
3123 t
= fold_build2 (RSHIFT_EXPR
, TREE_TYPE (ulhs
), ulhs
, t
);
3124 if (tree_int_cst_lt (integer_one_node
, t
))
3127 permerror (loc
, "shift expression %q+E overflows",
3128 build2_loc (loc
, code
, type
, lhs
, rhs
));
3129 return (!flag_permissive
|| ctx
->quiet
);
3135 /* Subroutine of cxx_eval_constant_expression.
3136 Attempt to reduce the unary expression tree T to a compile time value.
3137 If successful, return the value. Otherwise issue a diagnostic
3138 and return error_mark_node. */
3141 cxx_eval_unary_expression (const constexpr_ctx
*ctx
, tree t
,
3143 bool *non_constant_p
, bool *overflow_p
)
3146 tree orig_arg
= TREE_OPERAND (t
, 0);
3147 tree arg
= cxx_eval_constant_expression (ctx
, orig_arg
, /*lval*/false,
3148 non_constant_p
, overflow_p
);
3149 VERIFY_CONSTANT (arg
);
3150 location_t loc
= EXPR_LOCATION (t
);
3151 enum tree_code code
= TREE_CODE (t
);
3152 tree type
= TREE_TYPE (t
);
3153 r
= fold_unary_loc (loc
, code
, type
, arg
);
3156 if (arg
== orig_arg
)
3159 r
= build1_loc (loc
, code
, type
, arg
);
3161 VERIFY_CONSTANT (r
);
3165 /* Helper function for cxx_eval_binary_expression. Try to optimize
3166 original POINTER_PLUS_EXPR T, LHS p+ RHS, return NULL_TREE if the
3167 generic folding should be used. */
3170 cxx_fold_pointer_plus_expression (const constexpr_ctx
*ctx
, tree t
,
3171 tree lhs
, tree rhs
, bool *non_constant_p
,
3175 if (TREE_CODE (lhs
) != ADDR_EXPR
)
3178 lhs
= TREE_OPERAND (lhs
, 0);
3180 /* &A[i] p+ j => &A[i + j] */
3181 if (TREE_CODE (lhs
) == ARRAY_REF
3182 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == INTEGER_CST
3183 && TREE_CODE (rhs
) == INTEGER_CST
3184 && TYPE_SIZE_UNIT (TREE_TYPE (lhs
))
3185 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs
))) == INTEGER_CST
)
3187 tree orig_type
= TREE_TYPE (t
);
3188 location_t loc
= EXPR_LOCATION (t
);
3189 tree type
= TREE_TYPE (lhs
);
3191 t
= fold_convert_loc (loc
, ssizetype
, TREE_OPERAND (lhs
, 1));
3192 tree nelts
= array_type_nelts_top (TREE_TYPE (TREE_OPERAND (lhs
, 0)));
3193 nelts
= cxx_eval_constant_expression (ctx
, nelts
, false, non_constant_p
,
3195 if (*non_constant_p
)
3197 /* Don't fold an out-of-bound access. */
3198 if (!tree_int_cst_le (t
, nelts
))
3200 rhs
= cp_fold_convert (ssizetype
, rhs
);
3201 /* Don't fold if rhs can't be divided exactly by TYPE_SIZE_UNIT.
3202 constexpr int A[1]; ... (char *)&A[0] + 1 */
3203 if (!integer_zerop (fold_build2_loc (loc
, TRUNC_MOD_EXPR
, sizetype
,
3204 rhs
, TYPE_SIZE_UNIT (type
))))
3206 /* Make sure to treat the second operand of POINTER_PLUS_EXPR
3208 rhs
= fold_build2_loc (loc
, EXACT_DIV_EXPR
, ssizetype
, rhs
,
3209 TYPE_SIZE_UNIT (type
));
3210 t
= size_binop_loc (loc
, PLUS_EXPR
, rhs
, t
);
3211 t
= build4_loc (loc
, ARRAY_REF
, type
, TREE_OPERAND (lhs
, 0),
3212 t
, NULL_TREE
, NULL_TREE
);
3213 t
= cp_build_addr_expr (t
, tf_warning_or_error
);
3214 t
= cp_fold_convert (orig_type
, t
);
3215 return cxx_eval_constant_expression (ctx
, t
, /*lval*/false,
3216 non_constant_p
, overflow_p
);
3222 /* Subroutine of cxx_eval_constant_expression.
3223 Like cxx_eval_unary_expression, except for binary expressions. */
3226 cxx_eval_binary_expression (const constexpr_ctx
*ctx
, tree t
,
3228 bool *non_constant_p
, bool *overflow_p
)
3231 tree orig_lhs
= TREE_OPERAND (t
, 0);
3232 tree orig_rhs
= TREE_OPERAND (t
, 1);
3234 lhs
= cxx_eval_constant_expression (ctx
, orig_lhs
, /*lval*/false,
3235 non_constant_p
, overflow_p
);
3236 /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer
3238 if (*non_constant_p
)
3240 rhs
= cxx_eval_constant_expression (ctx
, orig_rhs
, /*lval*/false,
3241 non_constant_p
, overflow_p
);
3242 if (*non_constant_p
)
3245 location_t loc
= EXPR_LOCATION (t
);
3246 enum tree_code code
= TREE_CODE (t
);
3247 tree type
= TREE_TYPE (t
);
3249 if (code
== EQ_EXPR
|| code
== NE_EXPR
)
3251 bool is_code_eq
= (code
== EQ_EXPR
);
3253 if (TREE_CODE (lhs
) == PTRMEM_CST
3254 && TREE_CODE (rhs
) == PTRMEM_CST
)
3256 tree lmem
= PTRMEM_CST_MEMBER (lhs
);
3257 tree rmem
= PTRMEM_CST_MEMBER (rhs
);
3259 if (TREE_CODE (lmem
) == TREE_CODE (rmem
)
3260 && TREE_CODE (lmem
) == FIELD_DECL
3261 && TREE_CODE (DECL_CONTEXT (lmem
)) == UNION_TYPE
3262 && same_type_p (DECL_CONTEXT (lmem
),
3263 DECL_CONTEXT (rmem
)))
3264 /* If both refer to (possibly different) members of the same union
3265 (12.3), they compare equal. */
3268 eq
= cp_tree_equal (lhs
, rhs
);
3269 r
= constant_boolean_node (eq
== is_code_eq
, type
);
3271 else if ((TREE_CODE (lhs
) == PTRMEM_CST
3272 || TREE_CODE (rhs
) == PTRMEM_CST
)
3273 && (null_member_pointer_value_p (lhs
)
3274 || null_member_pointer_value_p (rhs
)))
3275 r
= constant_boolean_node (!is_code_eq
, type
);
3276 else if (TREE_CODE (lhs
) == PTRMEM_CST
)
3277 lhs
= cplus_expand_constant (lhs
);
3278 else if (TREE_CODE (rhs
) == PTRMEM_CST
)
3279 rhs
= cplus_expand_constant (rhs
);
3281 if (code
== POINTER_PLUS_EXPR
&& !*non_constant_p
3282 && integer_zerop (lhs
) && !integer_zerop (rhs
))
3285 error ("arithmetic involving a null pointer in %qE", lhs
);
3286 *non_constant_p
= true;
3289 else if (code
== POINTER_PLUS_EXPR
)
3290 r
= cxx_fold_pointer_plus_expression (ctx
, t
, lhs
, rhs
, non_constant_p
,
3292 else if (code
== SPACESHIP_EXPR
)
3294 r
= genericize_spaceship (loc
, type
, lhs
, rhs
);
3295 return cxx_eval_constant_expression (ctx
, r
, lval
, non_constant_p
,
3300 r
= fold_binary_loc (loc
, code
, type
, lhs
, rhs
);
3303 && (code
== LSHIFT_EXPR
|| code
== RSHIFT_EXPR
)
3304 && TREE_CODE (lhs
) == INTEGER_CST
3305 && TREE_CODE (rhs
) == INTEGER_CST
3306 && wi::neg_p (wi::to_wide (rhs
)))
3308 /* For diagnostics and -fpermissive emulate previous behavior of
3309 handling shifts by negative amount. */
3310 tree nrhs
= const_unop (NEGATE_EXPR
, TREE_TYPE (rhs
), rhs
);
3312 r
= fold_binary_loc (loc
,
3313 code
== LSHIFT_EXPR
? RSHIFT_EXPR
: LSHIFT_EXPR
,
3319 if (lhs
== orig_lhs
&& rhs
== orig_rhs
)
3322 r
= build2_loc (loc
, code
, type
, lhs
, rhs
);
3324 else if (cxx_eval_check_shift_p (loc
, ctx
, code
, type
, lhs
, rhs
))
3325 *non_constant_p
= true;
3326 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
3327 a local array in a constexpr function. */
3328 bool ptr
= INDIRECT_TYPE_P (TREE_TYPE (lhs
));
3330 VERIFY_CONSTANT (r
);
3334 /* Subroutine of cxx_eval_constant_expression.
3335 Attempt to evaluate condition expressions. Dead branches are not
3339 cxx_eval_conditional_expression (const constexpr_ctx
*ctx
, tree t
,
3341 bool *non_constant_p
, bool *overflow_p
,
3344 tree val
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
3346 non_constant_p
, overflow_p
);
3347 VERIFY_CONSTANT (val
);
3348 if (TREE_CODE (t
) == IF_STMT
&& IF_STMT_CONSTEVAL_P (t
))
3350 /* Evaluate the condition as if it was
3351 if (__builtin_is_constant_evaluated ()), i.e. defer it if not
3352 ctx->manifestly_const_eval (as sometimes we try to constant evaluate
3353 without manifestly_const_eval even expressions or parts thereof which
3354 will later be manifestly const_eval evaluated), otherwise fold it to
3356 if (ctx
->manifestly_const_eval
)
3357 val
= boolean_true_node
;
3360 *non_constant_p
= true;
3364 /* Don't VERIFY_CONSTANT the other operands. */
3365 if (integer_zerop (val
))
3366 val
= TREE_OPERAND (t
, 2);
3368 val
= TREE_OPERAND (t
, 1);
3369 if (TREE_CODE (t
) == IF_STMT
&& !val
)
3371 return cxx_eval_constant_expression (ctx
, val
, lval
, non_constant_p
,
3372 overflow_p
, jump_target
);
3375 /* Subroutine of cxx_eval_constant_expression.
3376 Attempt to evaluate vector condition expressions. Unlike
3377 cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal
3378 ternary arithmetics operation, where all 3 arguments have to be
3379 evaluated as constants and then folding computes the result from
3383 cxx_eval_vector_conditional_expression (const constexpr_ctx
*ctx
, tree t
,
3384 bool *non_constant_p
, bool *overflow_p
)
3386 tree arg1
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
3388 non_constant_p
, overflow_p
);
3389 VERIFY_CONSTANT (arg1
);
3390 tree arg2
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
3392 non_constant_p
, overflow_p
);
3393 VERIFY_CONSTANT (arg2
);
3394 tree arg3
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 2),
3396 non_constant_p
, overflow_p
);
3397 VERIFY_CONSTANT (arg3
);
3398 location_t loc
= EXPR_LOCATION (t
);
3399 tree type
= TREE_TYPE (t
);
3400 tree r
= fold_ternary_loc (loc
, VEC_COND_EXPR
, type
, arg1
, arg2
, arg3
);
3403 if (arg1
== TREE_OPERAND (t
, 0)
3404 && arg2
== TREE_OPERAND (t
, 1)
3405 && arg3
== TREE_OPERAND (t
, 2))
3408 r
= build3_loc (loc
, VEC_COND_EXPR
, type
, arg1
, arg2
, arg3
);
3410 VERIFY_CONSTANT (r
);
3414 /* Returns less than, equal to, or greater than zero if KEY is found to be
3415 less than, to match, or to be greater than the constructor_elt's INDEX. */
3418 array_index_cmp (tree key
, tree index
)
3420 gcc_assert (TREE_CODE (key
) == INTEGER_CST
);
3422 switch (TREE_CODE (index
))
3425 return tree_int_cst_compare (key
, index
);
3428 tree lo
= TREE_OPERAND (index
, 0);
3429 tree hi
= TREE_OPERAND (index
, 1);
3430 if (tree_int_cst_lt (key
, lo
))
3432 else if (tree_int_cst_lt (hi
, key
))
3442 /* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1
3443 if none. If INSERT is true, insert a matching element rather than fail. */
3445 static HOST_WIDE_INT
3446 find_array_ctor_elt (tree ary
, tree dindex
, bool insert
)
3448 if (tree_int_cst_sgn (dindex
) < 0)
3451 unsigned HOST_WIDE_INT i
= tree_to_uhwi (dindex
);
3452 vec
<constructor_elt
, va_gc
> *elts
= CONSTRUCTOR_ELTS (ary
);
3453 unsigned HOST_WIDE_INT len
= vec_safe_length (elts
);
3455 unsigned HOST_WIDE_INT end
= len
;
3456 unsigned HOST_WIDE_INT begin
= 0;
3458 /* If the last element of the CONSTRUCTOR has its own index, we can assume
3459 that the same is true of the other elements and index directly. */
3462 tree cindex
= (*elts
)[end
- 1].index
;
3463 if (cindex
== NULL_TREE
)
3465 /* Verify that if the last index is missing, all indexes
3468 for (unsigned int j
= 0; j
< len
- 1; ++j
)
3469 gcc_assert ((*elts
)[j
].index
== NULL_TREE
);
3476 /* If the element is to be added right at the end,
3477 make sure it is added with cleared index too. */
3480 /* Otherwise, in order not to break the assumption
3481 that CONSTRUCTOR either has all indexes or none,
3482 we need to add indexes to all elements. */
3483 for (unsigned int j
= 0; j
< len
; ++j
)
3484 (*elts
)[j
].index
= build_int_cst (TREE_TYPE (dindex
), j
);
3487 else if (TREE_CODE (cindex
) == INTEGER_CST
3488 && compare_tree_int (cindex
, end
- 1) == 0)
3497 /* Otherwise, find a matching index by means of a binary search. */
3498 while (begin
!= end
)
3500 unsigned HOST_WIDE_INT middle
= (begin
+ end
) / 2;
3501 constructor_elt
&elt
= (*elts
)[middle
];
3502 tree idx
= elt
.index
;
3504 int cmp
= array_index_cmp (dindex
, idx
);
3511 if (insert
&& TREE_CODE (idx
) == RANGE_EXPR
)
3513 /* We need to split the range. */
3515 tree lo
= TREE_OPERAND (idx
, 0);
3516 tree hi
= TREE_OPERAND (idx
, 1);
3517 tree value
= elt
.value
;
3518 dindex
= fold_convert (sizetype
, dindex
);
3519 if (tree_int_cst_lt (lo
, dindex
))
3521 /* There are still some lower elts; shorten the range. */
3522 tree new_hi
= int_const_binop (MINUS_EXPR
, dindex
,
3524 if (tree_int_cst_equal (lo
, new_hi
))
3525 /* Only one element left, no longer a range. */
3528 TREE_OPERAND (idx
, 1) = new_hi
;
3529 /* Append the element we want to insert. */
3532 e
.value
= unshare_constructor (value
);
3533 vec_safe_insert (CONSTRUCTOR_ELTS (ary
), middle
, e
);
3536 /* No lower elts, the range elt is now ours. */
3539 if (tree_int_cst_lt (dindex
, hi
))
3541 /* There are still some higher elts; append a range. */
3542 tree new_lo
= int_const_binop (PLUS_EXPR
, dindex
,
3544 if (tree_int_cst_equal (new_lo
, hi
))
3547 e
.index
= build2 (RANGE_EXPR
, sizetype
, new_lo
, hi
);
3548 e
.value
= unshare_constructor (value
);
3549 vec_safe_insert (CONSTRUCTOR_ELTS (ary
), middle
+ 1, e
);
3558 constructor_elt e
= { dindex
, NULL_TREE
};
3559 vec_safe_insert (CONSTRUCTOR_ELTS (ary
), end
, e
);
3566 /* Return a pointer to the constructor_elt of CTOR which matches INDEX. If no
3567 matching constructor_elt exists, then add one to CTOR.
3569 As an optimization, if POS_HINT is non-negative then it is used as a guess
3570 for the (integer) index of the matching constructor_elt within CTOR. */
3572 static constructor_elt
*
3573 get_or_insert_ctor_field (tree ctor
, tree index
, int pos_hint
= -1)
3575 /* Check the hint first. */
3576 if (pos_hint
>= 0 && (unsigned)pos_hint
< CONSTRUCTOR_NELTS (ctor
)
3577 && CONSTRUCTOR_ELT (ctor
, pos_hint
)->index
== index
)
3578 return CONSTRUCTOR_ELT (ctor
, pos_hint
);
3580 tree type
= TREE_TYPE (ctor
);
3581 if (TREE_CODE (type
) == VECTOR_TYPE
&& index
== NULL_TREE
)
3583 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (ctor
), index
, NULL_TREE
);
3584 return &CONSTRUCTOR_ELTS (ctor
)->last();
3586 else if (TREE_CODE (type
) == ARRAY_TYPE
|| TREE_CODE (type
) == VECTOR_TYPE
)
3588 if (TREE_CODE (index
) == RANGE_EXPR
)
3590 /* Support for RANGE_EXPR index lookups is currently limited to
3591 accessing an existing element via POS_HINT, or appending a new
3592 element to the end of CTOR. ??? Support for other access
3593 patterns may also be needed. */
3594 vec
<constructor_elt
, va_gc
> *elts
= CONSTRUCTOR_ELTS (ctor
);
3595 if (vec_safe_length (elts
))
3597 tree lo
= TREE_OPERAND (index
, 0);
3598 gcc_assert (array_index_cmp (elts
->last().index
, lo
) < 0);
3600 CONSTRUCTOR_APPEND_ELT (elts
, index
, NULL_TREE
);
3601 return &elts
->last();
3604 HOST_WIDE_INT i
= find_array_ctor_elt (ctor
, index
, /*insert*/true);
3605 gcc_assert (i
>= 0);
3606 constructor_elt
*cep
= CONSTRUCTOR_ELT (ctor
, i
);
3607 gcc_assert (cep
->index
== NULL_TREE
3608 || TREE_CODE (cep
->index
) != RANGE_EXPR
);
3613 gcc_assert (TREE_CODE (index
) == FIELD_DECL
3614 && (same_type_ignoring_top_level_qualifiers_p
3615 (DECL_CONTEXT (index
), TREE_TYPE (ctor
))));
3617 /* We must keep the CONSTRUCTOR's ELTS in FIELD order.
3618 Usually we meet initializers in that order, but it is
3619 possible for base types to be placed not in program
3621 tree fields
= TYPE_FIELDS (DECL_CONTEXT (index
));
3622 unsigned HOST_WIDE_INT idx
= 0;
3623 constructor_elt
*cep
= NULL
;
3625 /* Check if we're changing the active member of a union. */
3626 if (TREE_CODE (type
) == UNION_TYPE
&& CONSTRUCTOR_NELTS (ctor
)
3627 && CONSTRUCTOR_ELT (ctor
, 0)->index
!= index
)
3628 vec_safe_truncate (CONSTRUCTOR_ELTS (ctor
), 0);
3629 /* If the bit offset of INDEX is larger than that of the last
3630 constructor_elt, then we can just immediately append a new
3631 constructor_elt to the end of CTOR. */
3632 else if (CONSTRUCTOR_NELTS (ctor
)
3633 && tree_int_cst_compare (bit_position (index
),
3634 bit_position (CONSTRUCTOR_ELTS (ctor
)
3635 ->last().index
)) > 0)
3637 idx
= CONSTRUCTOR_NELTS (ctor
);
3641 /* Otherwise, we need to iterate over CTOR to find or insert INDEX
3644 for (; vec_safe_iterate (CONSTRUCTOR_ELTS (ctor
), idx
, &cep
);
3645 idx
++, fields
= DECL_CHAIN (fields
))
3647 if (index
== cep
->index
)
3650 /* The field we're initializing must be on the field
3651 list. Look to see if it is present before the
3652 field the current ELT initializes. */
3653 for (; fields
!= cep
->index
; fields
= DECL_CHAIN (fields
))
3654 if (index
== fields
)
3657 /* We fell off the end of the CONSTRUCTOR, so insert a new
3658 entry at the end. */
3662 constructor_elt ce
= { index
, NULL_TREE
};
3664 vec_safe_insert (CONSTRUCTOR_ELTS (ctor
), idx
, ce
);
3665 cep
= CONSTRUCTOR_ELT (ctor
, idx
);
3673 /* Under the control of CTX, issue a detailed diagnostic for
3674 an out-of-bounds subscript INDEX into the expression ARRAY. */
3677 diag_array_subscript (location_t loc
, const constexpr_ctx
*ctx
, tree array
, tree index
)
3681 tree arraytype
= TREE_TYPE (array
);
3683 /* Convert the unsigned array subscript to a signed integer to avoid
3684 printing huge numbers for small negative values. */
3685 tree sidx
= fold_convert (ssizetype
, index
);
3686 STRIP_ANY_LOCATION_WRAPPER (array
);
3689 if (TYPE_DOMAIN (arraytype
))
3690 error_at (loc
, "array subscript value %qE is outside the bounds "
3691 "of array %qD of type %qT", sidx
, array
, arraytype
);
3693 error_at (loc
, "nonzero array subscript %qE is used with array %qD of "
3694 "type %qT with unknown bounds", sidx
, array
, arraytype
);
3695 inform (DECL_SOURCE_LOCATION (array
), "declared here");
3697 else if (TYPE_DOMAIN (arraytype
))
3698 error_at (loc
, "array subscript value %qE is outside the bounds "
3699 "of array type %qT", sidx
, arraytype
);
3701 error_at (loc
, "nonzero array subscript %qE is used with array of type %qT "
3702 "with unknown bounds", sidx
, arraytype
);
3706 /* Return the number of elements for TYPE (which is an ARRAY_TYPE or
3710 get_array_or_vector_nelts (const constexpr_ctx
*ctx
, tree type
,
3711 bool *non_constant_p
, bool *overflow_p
)
3714 if (TREE_CODE (type
) == ARRAY_TYPE
)
3716 if (TYPE_DOMAIN (type
))
3717 nelts
= array_type_nelts_top (type
);
3719 nelts
= size_zero_node
;
3721 else if (VECTOR_TYPE_P (type
))
3722 nelts
= size_int (TYPE_VECTOR_SUBPARTS (type
));
3726 /* For VLAs, the number of elements won't be an integer constant. */
3727 nelts
= cxx_eval_constant_expression (ctx
, nelts
, false,
3728 non_constant_p
, overflow_p
);
3732 /* Extract element INDEX consisting of CHARS_PER_ELT chars from
3733 STRING_CST STRING. */
3736 extract_string_elt (tree string
, unsigned chars_per_elt
, unsigned index
)
3738 tree type
= cv_unqualified (TREE_TYPE (TREE_TYPE (string
)));
3741 if (chars_per_elt
== 1)
3742 r
= build_int_cst (type
, TREE_STRING_POINTER (string
)[index
]);
3745 const unsigned char *ptr
3746 = ((const unsigned char *)TREE_STRING_POINTER (string
)
3747 + index
* chars_per_elt
);
3748 r
= native_interpret_expr (type
, ptr
, chars_per_elt
);
3753 /* Subroutine of cxx_eval_array_reference. T is an ARRAY_REF; evaluate the
3754 subscript, diagnose any problems with it, and return the result. */
3757 eval_and_check_array_index (const constexpr_ctx
*ctx
,
3758 tree t
, bool allow_one_past
,
3759 bool *non_constant_p
, bool *overflow_p
)
3761 location_t loc
= cp_expr_loc_or_input_loc (t
);
3762 tree ary
= TREE_OPERAND (t
, 0);
3763 t
= TREE_OPERAND (t
, 1);
3764 tree index
= cxx_eval_constant_expression (ctx
, t
, false,
3765 non_constant_p
, overflow_p
);
3766 VERIFY_CONSTANT (index
);
3768 if (!tree_fits_shwi_p (index
)
3769 || tree_int_cst_sgn (index
) < 0)
3771 diag_array_subscript (loc
, ctx
, ary
, index
);
3772 *non_constant_p
= true;
3776 tree nelts
= get_array_or_vector_nelts (ctx
, TREE_TYPE (ary
), non_constant_p
,
3778 VERIFY_CONSTANT (nelts
);
3780 ? !tree_int_cst_le (index
, nelts
)
3781 : !tree_int_cst_lt (index
, nelts
))
3783 diag_array_subscript (loc
, ctx
, ary
, index
);
3784 *non_constant_p
= true;
3791 /* Subroutine of cxx_eval_constant_expression.
3792 Attempt to reduce a reference to an array slot. */
3795 cxx_eval_array_reference (const constexpr_ctx
*ctx
, tree t
,
3797 bool *non_constant_p
, bool *overflow_p
)
3799 tree oldary
= TREE_OPERAND (t
, 0);
3800 tree ary
= cxx_eval_constant_expression (ctx
, oldary
,
3802 non_constant_p
, overflow_p
);
3803 if (*non_constant_p
)
3806 && TREE_CODE (ary
) == VIEW_CONVERT_EXPR
3807 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary
, 0)))
3808 && TREE_TYPE (t
) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary
, 0))))
3809 ary
= TREE_OPERAND (ary
, 0);
3811 tree oldidx
= TREE_OPERAND (t
, 1);
3812 tree index
= eval_and_check_array_index (ctx
, t
, lval
,
3813 non_constant_p
, overflow_p
);
3814 if (*non_constant_p
)
3817 if (lval
&& ary
== oldary
&& index
== oldidx
)
3820 return build4 (ARRAY_REF
, TREE_TYPE (t
), ary
, index
, NULL
, NULL
);
3822 unsigned len
= 0, elem_nchars
= 1;
3823 tree elem_type
= TREE_TYPE (TREE_TYPE (ary
));
3824 if (TREE_CODE (ary
) == CONSTRUCTOR
)
3825 len
= CONSTRUCTOR_NELTS (ary
);
3826 else if (TREE_CODE (ary
) == STRING_CST
)
3828 elem_nchars
= (TYPE_PRECISION (elem_type
)
3829 / TYPE_PRECISION (char_type_node
));
3830 len
= (unsigned) TREE_STRING_LENGTH (ary
) / elem_nchars
;
3832 else if (TREE_CODE (ary
) == VECTOR_CST
)
3833 /* We don't create variable-length VECTOR_CSTs. */
3834 len
= VECTOR_CST_NELTS (ary
).to_constant ();
3837 /* We can't do anything with other tree codes, so use
3838 VERIFY_CONSTANT to complain and fail. */
3839 VERIFY_CONSTANT (ary
);
3844 HOST_WIDE_INT i
= 0;
3845 if (TREE_CODE (ary
) == CONSTRUCTOR
)
3847 HOST_WIDE_INT ix
= find_array_ctor_elt (ary
, index
);
3854 i
= tree_to_shwi (index
);
3861 if (TREE_CODE (ary
) == CONSTRUCTOR
)
3862 r
= (*CONSTRUCTOR_ELTS (ary
))[i
].value
;
3863 else if (TREE_CODE (ary
) == VECTOR_CST
)
3864 r
= VECTOR_CST_ELT (ary
, i
);
3866 r
= extract_string_elt (ary
, elem_nchars
, i
);
3869 /* Don't VERIFY_CONSTANT here. */
3872 /* Otherwise the element doesn't have a value yet. */
3877 if (TREE_CODE (ary
) == CONSTRUCTOR
3878 && CONSTRUCTOR_NO_CLEARING (ary
))
3880 /* 'ary' is part of the aggregate initializer we're currently
3881 building; if there's no initializer for this element yet,
3884 error ("accessing uninitialized array element");
3885 *non_constant_p
= true;
3889 /* If it's within the array bounds but doesn't have an explicit
3890 initializer, it's initialized from {}. But use build_value_init
3891 directly for non-aggregates to avoid creating a garbage CONSTRUCTOR. */
3893 constexpr_ctx new_ctx
;
3894 if (is_really_empty_class (elem_type
, /*ignore_vptr*/false))
3895 return build_constructor (elem_type
, NULL
);
3896 else if (CP_AGGREGATE_TYPE_P (elem_type
))
3898 tree empty_ctor
= build_constructor (init_list_type_node
, NULL
);
3899 val
= digest_init (elem_type
, empty_ctor
, tf_warning_or_error
);
3902 val
= build_value_init (elem_type
, tf_warning_or_error
);
3904 if (!SCALAR_TYPE_P (elem_type
))
3908 /* If there was no object, don't add one: it could confuse us
3909 into thinking we're modifying a const object. */
3911 new_ctx
.ctor
= build_constructor (elem_type
, NULL
);
3914 t
= cxx_eval_constant_expression (ctx
, val
, lval
, non_constant_p
,
3916 if (!SCALAR_TYPE_P (elem_type
) && t
!= ctx
->ctor
)
3917 free_constructor (ctx
->ctor
);
3921 /* Subroutine of cxx_eval_constant_expression.
3922 Attempt to reduce a field access of a value of class type. */
3925 cxx_eval_component_reference (const constexpr_ctx
*ctx
, tree t
,
3927 bool *non_constant_p
, bool *overflow_p
)
3929 unsigned HOST_WIDE_INT i
;
3932 tree part
= TREE_OPERAND (t
, 1);
3933 tree orig_whole
= TREE_OPERAND (t
, 0);
3934 tree whole
= cxx_eval_constant_expression (ctx
, orig_whole
,
3936 non_constant_p
, overflow_p
);
3937 if (INDIRECT_REF_P (whole
)
3938 && integer_zerop (TREE_OPERAND (whole
, 0)))
3941 error ("dereferencing a null pointer in %qE", orig_whole
);
3942 *non_constant_p
= true;
3946 if (TREE_CODE (whole
) == PTRMEM_CST
)
3947 whole
= cplus_expand_constant (whole
);
3948 if (whole
== orig_whole
)
3951 return fold_build3 (COMPONENT_REF
, TREE_TYPE (t
),
3952 whole
, part
, NULL_TREE
);
3953 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
3955 if (!*non_constant_p
&& TREE_CODE (whole
) != CONSTRUCTOR
)
3958 error ("%qE is not a constant expression", orig_whole
);
3959 *non_constant_p
= true;
3961 if (DECL_MUTABLE_P (part
))
3964 error ("mutable %qD is not usable in a constant expression", part
);
3965 *non_constant_p
= true;
3967 if (*non_constant_p
)
3969 bool pmf
= TYPE_PTRMEMFUNC_P (TREE_TYPE (whole
));
3970 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole
), i
, field
, value
)
3972 /* Use name match for PMF fields, as a variant will have a
3973 different FIELD_DECL with a different type. */
3974 if (pmf
? DECL_NAME (field
) == DECL_NAME (part
)
3979 STRIP_ANY_LOCATION_WRAPPER (value
);
3983 /* We're in the middle of initializing it. */
3987 if (TREE_CODE (TREE_TYPE (whole
)) == UNION_TYPE
3988 && CONSTRUCTOR_NELTS (whole
) > 0)
3990 /* DR 1188 says we don't have to deal with this. */
3993 constructor_elt
*cep
= CONSTRUCTOR_ELT (whole
, 0);
3994 if (cep
->value
== NULL_TREE
)
3995 error ("accessing uninitialized member %qD", part
);
3997 error ("accessing %qD member instead of initialized %qD member in "
3998 "constant expression", part
, cep
->index
);
4000 *non_constant_p
= true;
4004 /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty
4005 classes never get represented; throw together a value now. */
4006 if (is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
4007 return build_constructor (TREE_TYPE (t
), NULL
);
4009 gcc_assert (DECL_CONTEXT (part
) == TYPE_MAIN_VARIANT (TREE_TYPE (whole
)));
4011 if (CONSTRUCTOR_NO_CLEARING (whole
))
4013 /* 'whole' is part of the aggregate initializer we're currently
4014 building; if there's no initializer for this member yet, that's an
4017 error ("accessing uninitialized member %qD", part
);
4018 *non_constant_p
= true;
4022 /* If there's no explicit init for this field, it's value-initialized. */
4023 value
= build_value_init (TREE_TYPE (t
), tf_warning_or_error
);
4024 return cxx_eval_constant_expression (ctx
, value
,
4026 non_constant_p
, overflow_p
);
4029 /* Subroutine of cxx_eval_constant_expression.
4030 Attempt to reduce a field access of a value of class type that is
4031 expressed as a BIT_FIELD_REF. */
4034 cxx_eval_bit_field_ref (const constexpr_ctx
*ctx
, tree t
,
4036 bool *non_constant_p
, bool *overflow_p
)
4038 tree orig_whole
= TREE_OPERAND (t
, 0);
4039 tree retval
, fldval
, utype
, mask
;
4040 bool fld_seen
= false;
4041 HOST_WIDE_INT istart
, isize
;
4042 tree whole
= cxx_eval_constant_expression (ctx
, orig_whole
,
4044 non_constant_p
, overflow_p
);
4045 tree start
, field
, value
;
4046 unsigned HOST_WIDE_INT i
;
4048 if (whole
== orig_whole
)
4050 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
4052 if (!*non_constant_p
4053 && TREE_CODE (whole
) != VECTOR_CST
4054 && TREE_CODE (whole
) != CONSTRUCTOR
)
4057 error ("%qE is not a constant expression", orig_whole
);
4058 *non_constant_p
= true;
4060 if (*non_constant_p
)
4063 if (TREE_CODE (whole
) == VECTOR_CST
)
4064 return fold_ternary (BIT_FIELD_REF
, TREE_TYPE (t
), whole
,
4065 TREE_OPERAND (t
, 1), TREE_OPERAND (t
, 2));
4067 start
= TREE_OPERAND (t
, 2);
4068 istart
= tree_to_shwi (start
);
4069 isize
= tree_to_shwi (TREE_OPERAND (t
, 1));
4070 utype
= TREE_TYPE (t
);
4071 if (!TYPE_UNSIGNED (utype
))
4072 utype
= build_nonstandard_integer_type (TYPE_PRECISION (utype
), 1);
4073 retval
= build_int_cst (utype
, 0);
4074 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole
), i
, field
, value
)
4076 tree bitpos
= bit_position (field
);
4077 STRIP_ANY_LOCATION_WRAPPER (value
);
4078 if (bitpos
== start
&& DECL_SIZE (field
) == TREE_OPERAND (t
, 1))
4080 if (TREE_CODE (TREE_TYPE (field
)) == INTEGER_TYPE
4081 && TREE_CODE (value
) == INTEGER_CST
4082 && tree_fits_shwi_p (bitpos
)
4083 && tree_fits_shwi_p (DECL_SIZE (field
)))
4085 HOST_WIDE_INT bit
= tree_to_shwi (bitpos
);
4086 HOST_WIDE_INT sz
= tree_to_shwi (DECL_SIZE (field
));
4087 HOST_WIDE_INT shift
;
4088 if (bit
>= istart
&& bit
+ sz
<= istart
+ isize
)
4090 fldval
= fold_convert (utype
, value
);
4091 mask
= build_int_cst_type (utype
, -1);
4092 mask
= fold_build2 (LSHIFT_EXPR
, utype
, mask
,
4093 size_int (TYPE_PRECISION (utype
) - sz
));
4094 mask
= fold_build2 (RSHIFT_EXPR
, utype
, mask
,
4095 size_int (TYPE_PRECISION (utype
) - sz
));
4096 fldval
= fold_build2 (BIT_AND_EXPR
, utype
, fldval
, mask
);
4097 shift
= bit
- istart
;
4098 if (BYTES_BIG_ENDIAN
)
4099 shift
= TYPE_PRECISION (utype
) - shift
- sz
;
4100 fldval
= fold_build2 (LSHIFT_EXPR
, utype
, fldval
,
4102 retval
= fold_build2 (BIT_IOR_EXPR
, utype
, retval
, fldval
);
4108 return fold_convert (TREE_TYPE (t
), retval
);
4110 return error_mark_node
;
4113 /* Helper for cxx_eval_bit_cast.
4114 Check [bit.cast]/3 rules, bit_cast is constexpr only if the To and From
4115 types and types of all subobjects have is_union_v<T>, is_pointer_v<T>,
4116 is_member_pointer_v<T>, is_volatile_v<T> false and has no non-static
4117 data members of reference type. */
4120 check_bit_cast_type (const constexpr_ctx
*ctx
, location_t loc
, tree type
,
4123 if (TREE_CODE (type
) == UNION_TYPE
)
4127 if (type
== orig_type
)
4128 error_at (loc
, "%qs is not a constant expression because %qT is "
4129 "a union type", "__builtin_bit_cast", type
);
4131 error_at (loc
, "%qs is not a constant expression because %qT "
4132 "contains a union type", "__builtin_bit_cast",
4137 if (TREE_CODE (type
) == POINTER_TYPE
)
4141 if (type
== orig_type
)
4142 error_at (loc
, "%qs is not a constant expression because %qT is "
4143 "a pointer type", "__builtin_bit_cast", type
);
4145 error_at (loc
, "%qs is not a constant expression because %qT "
4146 "contains a pointer type", "__builtin_bit_cast",
4151 if (TREE_CODE (type
) == REFERENCE_TYPE
)
4155 if (type
== orig_type
)
4156 error_at (loc
, "%qs is not a constant expression because %qT is "
4157 "a reference type", "__builtin_bit_cast", type
);
4159 error_at (loc
, "%qs is not a constant expression because %qT "
4160 "contains a reference type", "__builtin_bit_cast",
4165 if (TYPE_PTRMEM_P (type
))
4169 if (type
== orig_type
)
4170 error_at (loc
, "%qs is not a constant expression because %qT is "
4171 "a pointer to member type", "__builtin_bit_cast",
4174 error_at (loc
, "%qs is not a constant expression because %qT "
4175 "contains a pointer to member type",
4176 "__builtin_bit_cast", orig_type
);
4180 if (TYPE_VOLATILE (type
))
4184 if (type
== orig_type
)
4185 error_at (loc
, "%qs is not a constant expression because %qT is "
4186 "volatile", "__builtin_bit_cast", type
);
4188 error_at (loc
, "%qs is not a constant expression because %qT "
4189 "contains a volatile subobject",
4190 "__builtin_bit_cast", orig_type
);
4194 if (TREE_CODE (type
) == RECORD_TYPE
)
4195 for (tree field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
4196 if (TREE_CODE (field
) == FIELD_DECL
4197 && check_bit_cast_type (ctx
, loc
, TREE_TYPE (field
), orig_type
))
4202 /* Subroutine of cxx_eval_constant_expression.
4203 Attempt to evaluate a BIT_CAST_EXPR. */
4206 cxx_eval_bit_cast (const constexpr_ctx
*ctx
, tree t
, bool *non_constant_p
,
4209 if (check_bit_cast_type (ctx
, EXPR_LOCATION (t
), TREE_TYPE (t
),
4211 || check_bit_cast_type (ctx
, cp_expr_loc_or_loc (TREE_OPERAND (t
, 0),
4213 TREE_TYPE (TREE_OPERAND (t
, 0)),
4214 TREE_TYPE (TREE_OPERAND (t
, 0))))
4216 *non_constant_p
= true;
4220 tree op
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), false,
4221 non_constant_p
, overflow_p
);
4222 if (*non_constant_p
)
4225 location_t loc
= EXPR_LOCATION (t
);
4226 if (BITS_PER_UNIT
!= 8 || CHAR_BIT
!= 8)
4229 sorry_at (loc
, "%qs cannot be constant evaluated on the target",
4230 "__builtin_bit_cast");
4231 *non_constant_p
= true;
4235 if (!tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (t
))))
4238 sorry_at (loc
, "%qs cannot be constant evaluated because the "
4239 "type is too large", "__builtin_bit_cast");
4240 *non_constant_p
= true;
4244 HOST_WIDE_INT len
= tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (t
)));
4245 if (len
< 0 || (int) len
!= len
)
4248 sorry_at (loc
, "%qs cannot be constant evaluated because the "
4249 "type is too large", "__builtin_bit_cast");
4250 *non_constant_p
= true;
4254 unsigned char buf
[64];
4255 unsigned char *ptr
, *mask
;
4256 size_t alen
= (size_t) len
* 2;
4257 if (alen
<= sizeof (buf
))
4260 ptr
= XNEWVEC (unsigned char, alen
);
4261 mask
= ptr
+ (size_t) len
;
4262 /* At the beginning consider everything indeterminate. */
4263 memset (mask
, ~0, (size_t) len
);
4265 if (native_encode_initializer (op
, ptr
, len
, 0, mask
) != len
)
4268 sorry_at (loc
, "%qs cannot be constant evaluated because the "
4269 "argument cannot be encoded", "__builtin_bit_cast");
4270 *non_constant_p
= true;
4277 if (can_native_interpret_type_p (TREE_TYPE (t
)))
4278 r
= native_interpret_expr (TREE_TYPE (t
), ptr
, len
);
4279 else if (TREE_CODE (TREE_TYPE (t
)) == RECORD_TYPE
)
4281 r
= native_interpret_aggregate (TREE_TYPE (t
), ptr
, 0, len
);
4283 clear_type_padding_in_mask (TREE_TYPE (t
), mask
);
4288 for (int i
= 0; i
< len
; i
++)
4292 error_at (loc
, "%qs accessing uninitialized byte at offset %d",
4293 "__builtin_bit_cast", i
);
4294 *non_constant_p
= true;
4304 sorry_at (loc
, "%qs cannot be constant evaluated because the "
4305 "argument cannot be interpreted", "__builtin_bit_cast");
4306 *non_constant_p
= true;
4312 /* Subroutine of cxx_eval_constant_expression.
4313 Evaluate a short-circuited logical expression T in the context
4314 of a given constexpr CALL. BAILOUT_VALUE is the value for
4315 early return. CONTINUE_VALUE is used here purely for
4316 sanity check purposes. */
4319 cxx_eval_logical_expression (const constexpr_ctx
*ctx
, tree t
,
4320 tree bailout_value
, tree continue_value
,
4322 bool *non_constant_p
, bool *overflow_p
)
4325 tree lhs
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
4327 non_constant_p
, overflow_p
);
4328 VERIFY_CONSTANT (lhs
);
4329 if (tree_int_cst_equal (lhs
, bailout_value
))
4331 gcc_assert (tree_int_cst_equal (lhs
, continue_value
));
4332 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
4333 lval
, non_constant_p
,
4335 VERIFY_CONSTANT (r
);
4339 /* REF is a COMPONENT_REF designating a particular field. V is a vector of
4340 CONSTRUCTOR elements to initialize (part of) an object containing that
4341 field. Return a pointer to the constructor_elt corresponding to the
4342 initialization of the field. */
4344 static constructor_elt
*
4345 base_field_constructor_elt (vec
<constructor_elt
, va_gc
> *v
, tree ref
)
4347 tree aggr
= TREE_OPERAND (ref
, 0);
4348 tree field
= TREE_OPERAND (ref
, 1);
4350 constructor_elt
*ce
;
4352 gcc_assert (TREE_CODE (ref
) == COMPONENT_REF
);
4354 if (TREE_CODE (aggr
) == COMPONENT_REF
)
4356 constructor_elt
*base_ce
4357 = base_field_constructor_elt (v
, aggr
);
4358 v
= CONSTRUCTOR_ELTS (base_ce
->value
);
4361 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
4362 if (ce
->index
== field
)
4369 /* Some of the expressions fed to the constexpr mechanism are calls to
4370 constructors, which have type void. In that case, return the type being
4371 initialized by the constructor. */
4374 initialized_type (tree t
)
4378 tree type
= TREE_TYPE (t
);
4379 if (TREE_CODE (t
) == CALL_EXPR
)
4381 /* A constructor call has void type, so we need to look deeper. */
4382 tree fn
= get_function_named_in_call (t
);
4383 if (fn
&& TREE_CODE (fn
) == FUNCTION_DECL
4384 && DECL_CXX_CONSTRUCTOR_P (fn
))
4385 type
= DECL_CONTEXT (fn
);
4387 else if (TREE_CODE (t
) == COMPOUND_EXPR
)
4388 return initialized_type (TREE_OPERAND (t
, 1));
4389 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
)
4390 type
= TREE_TYPE (AGGR_INIT_EXPR_SLOT (t
));
4391 return cv_unqualified (type
);
4394 /* We're about to initialize element INDEX of an array or class from VALUE.
4395 Set up NEW_CTX appropriately by adjusting .object to refer to the
4396 subobject and creating a new CONSTRUCTOR if the element is itself
4397 a class or array. */
4400 init_subob_ctx (const constexpr_ctx
*ctx
, constexpr_ctx
&new_ctx
,
4401 tree index
, tree
&value
)
4405 if (index
&& TREE_CODE (index
) != INTEGER_CST
4406 && TREE_CODE (index
) != FIELD_DECL
4407 && TREE_CODE (index
) != RANGE_EXPR
)
4408 /* This won't have an element in the new CONSTRUCTOR. */
4411 tree type
= initialized_type (value
);
4412 if (!AGGREGATE_TYPE_P (type
) && !VECTOR_TYPE_P (type
))
4413 /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
4416 /* The sub-aggregate initializer might contain a placeholder;
4417 update object to refer to the subobject and ctor to refer to
4418 the (newly created) sub-initializer. */
4421 if (index
== NULL_TREE
|| TREE_CODE (index
) == RANGE_EXPR
)
4422 /* There's no well-defined subobject for this index. */
4423 new_ctx
.object
= NULL_TREE
;
4425 new_ctx
.object
= build_ctor_subob_ref (index
, type
, ctx
->object
);
4427 tree elt
= build_constructor (type
, NULL
);
4428 CONSTRUCTOR_NO_CLEARING (elt
) = true;
4431 if (TREE_CODE (value
) == TARGET_EXPR
)
4432 /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
4433 value
= TARGET_EXPR_INITIAL (value
);
4436 /* We're about to process an initializer for a class or array TYPE. Make
4437 sure that CTX is set up appropriately. */
4440 verify_ctor_sanity (const constexpr_ctx
*ctx
, tree type
)
4442 /* We don't bother building a ctor for an empty base subobject. */
4443 if (is_empty_class (type
))
4446 /* We're in the middle of an initializer that might involve placeholders;
4447 our caller should have created a CONSTRUCTOR for us to put the
4448 initializer into. We will either return that constructor or T. */
4449 gcc_assert (ctx
->ctor
);
4450 gcc_assert (same_type_ignoring_top_level_qualifiers_p
4451 (type
, TREE_TYPE (ctx
->ctor
)));
4452 /* We used to check that ctx->ctor was empty, but that isn't the case when
4453 the object is zero-initialized before calling the constructor. */
4456 tree otype
= TREE_TYPE (ctx
->object
);
4457 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type
, otype
)
4458 /* Handle flexible array members. */
4459 || (TREE_CODE (otype
) == ARRAY_TYPE
4460 && TYPE_DOMAIN (otype
) == NULL_TREE
4461 && TREE_CODE (type
) == ARRAY_TYPE
4462 && (same_type_ignoring_top_level_qualifiers_p
4463 (TREE_TYPE (type
), TREE_TYPE (otype
)))));
4465 gcc_assert (!ctx
->object
|| !DECL_P (ctx
->object
)
4466 || *(ctx
->global
->values
.get (ctx
->object
)) == ctx
->ctor
);
4469 /* Subroutine of cxx_eval_constant_expression.
4470 The expression tree T denotes a C-style array or a C-style
4471 aggregate. Reduce it to a constant expression. */
4474 cxx_eval_bare_aggregate (const constexpr_ctx
*ctx
, tree t
,
4476 bool *non_constant_p
, bool *overflow_p
)
4478 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (t
);
4479 bool changed
= false;
4480 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t
));
4481 tree type
= TREE_TYPE (t
);
4483 constexpr_ctx new_ctx
;
4484 if (TYPE_PTRMEMFUNC_P (type
) || VECTOR_TYPE_P (type
))
4486 /* We don't really need the ctx->ctor business for a PMF or
4487 vector, but it's simpler to use the same code. */
4489 new_ctx
.ctor
= build_constructor (type
, NULL
);
4490 new_ctx
.object
= NULL_TREE
;
4493 verify_ctor_sanity (ctx
, type
);
4494 vec
<constructor_elt
, va_gc
> **p
= &CONSTRUCTOR_ELTS (ctx
->ctor
);
4495 vec_alloc (*p
, vec_safe_length (v
));
4497 if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (t
))
4498 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx
->ctor
) = 1;
4502 bool constant_p
= true;
4503 bool side_effects_p
= false;
4504 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, index
, value
)
4506 tree orig_value
= value
;
4507 /* Like in cxx_eval_store_expression, omit entries for empty fields. */
4508 bool no_slot
= TREE_CODE (type
) == RECORD_TYPE
&& is_empty_field (index
);
4512 init_subob_ctx (ctx
, new_ctx
, index
, value
);
4514 if (new_ctx
.ctor
!= ctx
->ctor
)
4516 /* If we built a new CONSTRUCTOR, attach it now so that other
4517 initializers can refer to it. */
4518 constructor_elt
*cep
= get_or_insert_ctor_field (ctx
->ctor
, index
);
4519 cep
->value
= new_ctx
.ctor
;
4520 pos_hint
= cep
- (*p
)->begin();
4522 else if (TREE_CODE (type
) == UNION_TYPE
)
4523 /* Otherwise if we're constructing a non-aggregate union member, set
4524 the active union member now so that we can later detect and diagnose
4525 if its initializer attempts to activate another member. */
4526 get_or_insert_ctor_field (ctx
->ctor
, index
);
4527 tree elt
= cxx_eval_constant_expression (&new_ctx
, value
,
4529 non_constant_p
, overflow_p
);
4530 /* Don't VERIFY_CONSTANT here. */
4531 if (ctx
->quiet
&& *non_constant_p
)
4533 if (elt
!= orig_value
)
4536 if (!TREE_CONSTANT (elt
))
4538 if (TREE_SIDE_EFFECTS (elt
))
4539 side_effects_p
= true;
4540 if (index
&& TREE_CODE (index
) == COMPONENT_REF
)
4542 /* This is an initialization of a vfield inside a base
4543 subaggregate that we already initialized; push this
4544 initialization into the previous initialization. */
4545 constructor_elt
*inner
= base_field_constructor_elt (*p
, index
);
4550 && (TREE_CODE (index
) == NOP_EXPR
4551 || TREE_CODE (index
) == POINTER_PLUS_EXPR
))
4553 /* This is an initializer for an empty base; now that we've
4554 checked that it's constant, we can ignore it. */
4555 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index
))));
4562 if (TREE_CODE (type
) == UNION_TYPE
4563 && (*p
)->last().index
!= index
)
4564 /* The initializer erroneously changed the active union member that
4565 we're initializing. */
4566 gcc_assert (*non_constant_p
);
4569 /* The initializer might have mutated the underlying CONSTRUCTOR,
4570 so recompute the location of the target constructer_elt. */
4571 constructor_elt
*cep
4572 = get_or_insert_ctor_field (ctx
->ctor
, index
, pos_hint
);
4576 /* Adding or replacing an element might change the ctor's flags. */
4577 TREE_CONSTANT (ctx
->ctor
) = constant_p
;
4578 TREE_SIDE_EFFECTS (ctx
->ctor
) = side_effects_p
;
4581 if (*non_constant_p
|| !changed
)
4584 /* We're done building this CONSTRUCTOR, so now we can interpret an
4585 element without an explicit initializer as value-initialized. */
4586 CONSTRUCTOR_NO_CLEARING (t
) = false;
4587 TREE_CONSTANT (t
) = constant_p
;
4588 TREE_SIDE_EFFECTS (t
) = side_effects_p
;
4589 if (VECTOR_TYPE_P (type
))
4594 /* Subroutine of cxx_eval_constant_expression.
4595 The expression tree T is a VEC_INIT_EXPR which denotes the desired
4596 initialization of a non-static data member of array type. Reduce it to a
4599 Note that apart from value-initialization (when VALUE_INIT is true),
4600 this is only intended to support value-initialization and the
4601 initializations done by defaulted constructors for classes with
4602 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
4603 will either be NULL_TREE for the default constructor, or a COMPONENT_REF
4604 for the copy/move constructor. */
4607 cxx_eval_vec_init_1 (const constexpr_ctx
*ctx
, tree atype
, tree init
,
4608 bool value_init
, bool lval
,
4609 bool *non_constant_p
, bool *overflow_p
)
4611 tree elttype
= TREE_TYPE (atype
);
4612 verify_ctor_sanity (ctx
, atype
);
4613 vec
<constructor_elt
, va_gc
> **p
= &CONSTRUCTOR_ELTS (ctx
->ctor
);
4614 bool pre_init
= false;
4615 unsigned HOST_WIDE_INT i
;
4616 tsubst_flags_t complain
= ctx
->quiet
? tf_none
: tf_warning_or_error
;
4618 if (init
&& TREE_CODE (init
) == CONSTRUCTOR
)
4619 return cxx_eval_bare_aggregate (ctx
, init
, lval
,
4620 non_constant_p
, overflow_p
);
4622 /* For the default constructor, build up a call to the default
4623 constructor of the element type. We only need to handle class types
4624 here, as for a constructor to be constexpr, all members must be
4625 initialized, which for a defaulted default constructor means they must
4626 be of a class type with a constexpr default constructor. */
4627 if (TREE_CODE (elttype
) == ARRAY_TYPE
)
4628 /* We only do this at the lowest level. */;
4629 else if (value_init
)
4631 init
= build_value_init (elttype
, complain
);
4636 releasing_vec argvec
;
4637 init
= build_special_member_call (NULL_TREE
, complete_ctor_identifier
,
4638 &argvec
, elttype
, LOOKUP_NORMAL
,
4640 init
= build_aggr_init_expr (elttype
, init
);
4644 bool zeroed_out
= false;
4645 if (!CONSTRUCTOR_NO_CLEARING (ctx
->ctor
))
4647 /* We're initializing an array object that had been zero-initialized
4648 earlier. Truncate ctx->ctor, and propagate its zeroed state by
4649 clearing CONSTRUCTOR_NO_CLEARING on each of the aggregate element
4650 initializers we append to it. */
4651 gcc_checking_assert (initializer_zerop (ctx
->ctor
));
4653 vec_safe_truncate (*p
, 0);
4656 tree nelts
= get_array_or_vector_nelts (ctx
, atype
, non_constant_p
,
4658 unsigned HOST_WIDE_INT max
= tree_to_uhwi (nelts
);
4659 for (i
= 0; i
< max
; ++i
)
4661 tree idx
= build_int_cst (size_type_node
, i
);
4664 constexpr_ctx new_ctx
;
4665 init_subob_ctx (ctx
, new_ctx
, idx
, pre_init
? init
: elttype
);
4666 if (new_ctx
.ctor
!= ctx
->ctor
)
4669 CONSTRUCTOR_NO_CLEARING (new_ctx
.ctor
) = false;
4670 CONSTRUCTOR_APPEND_ELT (*p
, idx
, new_ctx
.ctor
);
4672 if (TREE_CODE (elttype
) == ARRAY_TYPE
)
4674 /* A multidimensional array; recurse. */
4675 if (value_init
|| init
== NULL_TREE
)
4677 eltinit
= NULL_TREE
;
4681 eltinit
= cp_build_array_ref (input_location
, init
, idx
, complain
);
4682 eltinit
= cxx_eval_vec_init_1 (&new_ctx
, elttype
, eltinit
, value_init
,
4684 non_constant_p
, overflow_p
);
4688 /* Initializing an element using value or default initialization
4689 we just pre-built above. */
4690 if (init
== void_node
)
4691 /* Trivial default-init, don't do anything to the CONSTRUCTOR. */
4693 eltinit
= cxx_eval_constant_expression (&new_ctx
, init
, lval
,
4694 non_constant_p
, overflow_p
);
4699 /* Copying an element. */
4700 gcc_assert (same_type_ignoring_top_level_qualifiers_p
4701 (atype
, TREE_TYPE (init
)));
4702 eltinit
= cp_build_array_ref (input_location
, init
, idx
, complain
);
4703 if (!lvalue_p (init
))
4704 eltinit
= move (eltinit
);
4705 eltinit
= force_rvalue (eltinit
, complain
);
4706 eltinit
= cxx_eval_constant_expression (&new_ctx
, eltinit
, lval
,
4707 non_constant_p
, overflow_p
);
4709 if (*non_constant_p
)
4711 if (new_ctx
.ctor
!= ctx
->ctor
)
4713 /* We appended this element above; update the value. */
4714 gcc_assert ((*p
)->last().index
== idx
);
4715 (*p
)->last().value
= eltinit
;
4718 CONSTRUCTOR_APPEND_ELT (*p
, idx
, eltinit
);
4719 /* Reuse the result of cxx_eval_constant_expression call
4720 from the first iteration to all others if it is a constant
4721 initializer that doesn't require relocations. */
4724 && (eltinit
== NULL_TREE
4725 || (initializer_constant_valid_p (eltinit
, TREE_TYPE (eltinit
))
4726 == null_pointer_node
)))
4728 if (new_ctx
.ctor
!= ctx
->ctor
)
4729 eltinit
= new_ctx
.ctor
;
4730 tree range
= build2 (RANGE_EXPR
, size_type_node
,
4731 build_int_cst (size_type_node
, 1),
4732 build_int_cst (size_type_node
, max
- 1));
4733 CONSTRUCTOR_APPEND_ELT (*p
, range
, unshare_constructor (eltinit
));
4737 vec_safe_reserve (*p
, max
);
4740 if (!*non_constant_p
)
4743 CONSTRUCTOR_NO_CLEARING (init
) = false;
4749 cxx_eval_vec_init (const constexpr_ctx
*ctx
, tree t
,
4751 bool *non_constant_p
, bool *overflow_p
)
4753 tree atype
= TREE_TYPE (t
);
4754 tree init
= VEC_INIT_EXPR_INIT (t
);
4755 tree r
= cxx_eval_vec_init_1 (ctx
, atype
, init
,
4756 VEC_INIT_EXPR_VALUE_INIT (t
),
4757 lval
, non_constant_p
, overflow_p
);
4758 if (*non_constant_p
)
4764 /* Like same_type_ignoring_top_level_qualifiers_p, but also handle the case
4765 where the desired type is an array of unknown bounds because the variable
4766 has had its bounds deduced since the wrapping expression was created. */
4769 same_type_ignoring_tlq_and_bounds_p (tree type1
, tree type2
)
4771 while (TREE_CODE (type1
) == ARRAY_TYPE
4772 && TREE_CODE (type2
) == ARRAY_TYPE
4773 && (!TYPE_DOMAIN (type1
) || !TYPE_DOMAIN (type2
)))
4775 type1
= TREE_TYPE (type1
);
4776 type2
= TREE_TYPE (type2
);
4778 return same_type_ignoring_top_level_qualifiers_p (type1
, type2
);
4781 /* Try to determine the currently active union member for an expression
4782 with UNION_TYPE. If it can be determined, return the FIELD_DECL,
4783 otherwise return NULL_TREE. */
4786 cxx_union_active_member (const constexpr_ctx
*ctx
, tree t
)
4788 constexpr_ctx new_ctx
= *ctx
;
4789 new_ctx
.quiet
= true;
4790 bool non_constant_p
= false, overflow_p
= false;
4791 tree ctor
= cxx_eval_constant_expression (&new_ctx
, t
, false,
4794 if (TREE_CODE (ctor
) == CONSTRUCTOR
4795 && CONSTRUCTOR_NELTS (ctor
) == 1
4796 && CONSTRUCTOR_ELT (ctor
, 0)->index
4797 && TREE_CODE (CONSTRUCTOR_ELT (ctor
, 0)->index
) == FIELD_DECL
)
4798 return CONSTRUCTOR_ELT (ctor
, 0)->index
;
4802 /* Helper function for cxx_fold_indirect_ref_1, called recursively. */
4805 cxx_fold_indirect_ref_1 (const constexpr_ctx
*ctx
, location_t loc
, tree type
,
4806 tree op
, unsigned HOST_WIDE_INT off
, bool *empty_base
)
4808 tree optype
= TREE_TYPE (op
);
4809 unsigned HOST_WIDE_INT const_nunits
;
4810 if (off
== 0 && similar_type_p (optype
, type
))
4812 else if (TREE_CODE (optype
) == COMPLEX_TYPE
4813 && similar_type_p (type
, TREE_TYPE (optype
)))
4815 /* *(foo *)&complexfoo => __real__ complexfoo */
4817 return build1_loc (loc
, REALPART_EXPR
, type
, op
);
4818 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
4819 else if (tree_to_uhwi (TYPE_SIZE_UNIT (type
)) == off
)
4820 return build1_loc (loc
, IMAGPART_EXPR
, type
, op
);
4822 /* ((foo*)&vectorfoo)[x] => BIT_FIELD_REF<vectorfoo,...> */
4823 else if (VECTOR_TYPE_P (optype
)
4824 && similar_type_p (type
, TREE_TYPE (optype
))
4825 && TYPE_VECTOR_SUBPARTS (optype
).is_constant (&const_nunits
))
4827 unsigned HOST_WIDE_INT part_width
= tree_to_uhwi (TYPE_SIZE_UNIT (type
));
4828 unsigned HOST_WIDE_INT max_offset
= part_width
* const_nunits
;
4829 if (off
< max_offset
&& off
% part_width
== 0)
4831 tree index
= bitsize_int (off
* BITS_PER_UNIT
);
4832 return build3_loc (loc
, BIT_FIELD_REF
, type
, op
,
4833 TYPE_SIZE (type
), index
);
4836 /* ((foo *)&fooarray)[x] => fooarray[x] */
4837 else if (TREE_CODE (optype
) == ARRAY_TYPE
4838 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (optype
)))
4839 && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (optype
))))
4841 tree type_domain
= TYPE_DOMAIN (optype
);
4842 tree min_val
= size_zero_node
;
4843 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
4844 min_val
= TYPE_MIN_VALUE (type_domain
);
4845 unsigned HOST_WIDE_INT el_sz
4846 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (optype
)));
4847 unsigned HOST_WIDE_INT idx
= off
/ el_sz
;
4848 unsigned HOST_WIDE_INT rem
= off
% el_sz
;
4849 if (tree_fits_uhwi_p (min_val
))
4851 tree index
= size_int (idx
+ tree_to_uhwi (min_val
));
4852 op
= build4_loc (loc
, ARRAY_REF
, TREE_TYPE (optype
), op
, index
,
4853 NULL_TREE
, NULL_TREE
);
4854 return cxx_fold_indirect_ref_1 (ctx
, loc
, type
, op
, rem
,
4858 /* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */
4859 else if (TREE_CODE (optype
) == RECORD_TYPE
4860 || TREE_CODE (optype
) == UNION_TYPE
)
4862 if (TREE_CODE (optype
) == UNION_TYPE
)
4863 /* For unions prefer the currently active member. */
4864 if (tree field
= cxx_union_active_member (ctx
, op
))
4866 unsigned HOST_WIDE_INT el_sz
4867 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field
)));
4870 tree cop
= build3 (COMPONENT_REF
, TREE_TYPE (field
),
4871 op
, field
, NULL_TREE
);
4872 if (tree ret
= cxx_fold_indirect_ref_1 (ctx
, loc
, type
, cop
,
4877 for (tree field
= TYPE_FIELDS (optype
);
4878 field
; field
= DECL_CHAIN (field
))
4879 if (TREE_CODE (field
) == FIELD_DECL
4880 && TREE_TYPE (field
) != error_mark_node
4881 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (field
))))
4883 tree pos
= byte_position (field
);
4884 if (!tree_fits_uhwi_p (pos
))
4886 unsigned HOST_WIDE_INT upos
= tree_to_uhwi (pos
);
4887 unsigned HOST_WIDE_INT el_sz
4888 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field
)));
4889 if (upos
<= off
&& off
< upos
+ el_sz
)
4891 tree cop
= build3 (COMPONENT_REF
, TREE_TYPE (field
),
4892 op
, field
, NULL_TREE
);
4893 if (tree ret
= cxx_fold_indirect_ref_1 (ctx
, loc
, type
, cop
,
4899 /* Also handle conversion to an empty base class, which
4900 is represented with a NOP_EXPR. */
4901 if (is_empty_class (type
)
4902 && CLASS_TYPE_P (optype
)
4903 && DERIVED_FROM_P (type
, optype
))
4913 /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
4914 match. We want to be less strict for simple *& folding; if we have a
4915 non-const temporary that we access through a const pointer, that should
4916 work. We handle this here rather than change fold_indirect_ref_1
4917 because we're dealing with things like ADDR_EXPR of INTEGER_CST which
4918 don't really make sense outside of constant expression evaluation. Also
4919 we want to allow folding to COMPONENT_REF, which could cause trouble
4920 with TBAA in fold_indirect_ref_1. */
4923 cxx_fold_indirect_ref (const constexpr_ctx
*ctx
, location_t loc
, tree type
,
4924 tree op0
, bool *empty_base
)
4928 poly_uint64 const_op01
;
4930 /* STRIP_NOPS, but stop if REINTERPRET_CAST_P. */
4931 while (CONVERT_EXPR_P (sub
) || TREE_CODE (sub
) == NON_LVALUE_EXPR
4932 || TREE_CODE (sub
) == VIEW_CONVERT_EXPR
)
4934 if (TREE_CODE (sub
) == NOP_EXPR
4935 && REINTERPRET_CAST_P (sub
))
4937 sub
= TREE_OPERAND (sub
, 0);
4940 subtype
= TREE_TYPE (sub
);
4941 if (!INDIRECT_TYPE_P (subtype
))
4944 if (TREE_CODE (sub
) == ADDR_EXPR
)
4946 tree op
= TREE_OPERAND (sub
, 0);
4947 tree optype
= TREE_TYPE (op
);
4949 /* *&CONST_DECL -> to the value of the const decl. */
4950 if (TREE_CODE (op
) == CONST_DECL
)
4951 return DECL_INITIAL (op
);
4952 /* *&p => p; make sure to handle *&"str"[cst] here. */
4953 if (similar_type_p (optype
, type
))
4955 tree fop
= fold_read_from_constant_string (op
);
4962 return cxx_fold_indirect_ref_1 (ctx
, loc
, type
, op
, 0, empty_base
);
4964 else if (TREE_CODE (sub
) == POINTER_PLUS_EXPR
4965 && tree_fits_uhwi_p (TREE_OPERAND (sub
, 1)))
4967 tree op00
= TREE_OPERAND (sub
, 0);
4968 tree off
= TREE_OPERAND (sub
, 1);
4971 if (TREE_CODE (op00
) == ADDR_EXPR
)
4973 tree obj
= TREE_OPERAND (op00
, 0);
4974 while (TREE_CODE (obj
) == COMPONENT_REF
4975 && tree_int_cst_sign_bit (off
))
4977 /* Canonicalize this object/offset pair by iteratively absorbing
4978 the innermost component into the offset until the offset is
4979 nonnegative, so that cxx_fold_indirect_ref_1 can identify
4980 more folding opportunities. */
4981 tree field
= TREE_OPERAND (obj
, 1);
4982 off
= int_const_binop (PLUS_EXPR
, off
, byte_position (field
));
4983 obj
= TREE_OPERAND (obj
, 0);
4985 return cxx_fold_indirect_ref_1 (ctx
, loc
, type
, obj
,
4986 tree_to_uhwi (off
), empty_base
);
4989 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
4990 else if (TREE_CODE (TREE_TYPE (subtype
)) == ARRAY_TYPE
4991 && similar_type_p (type
, TREE_TYPE (TREE_TYPE (subtype
))))
4994 tree min_val
= size_zero_node
;
4996 = cxx_fold_indirect_ref (ctx
, loc
, TREE_TYPE (subtype
), sub
, NULL
);
5000 sub
= build1_loc (loc
, INDIRECT_REF
, TREE_TYPE (subtype
), sub
);
5001 type_domain
= TYPE_DOMAIN (TREE_TYPE (sub
));
5002 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
5003 min_val
= TYPE_MIN_VALUE (type_domain
);
5004 return build4_loc (loc
, ARRAY_REF
, type
, sub
, min_val
, NULL_TREE
,
5012 cxx_eval_indirect_ref (const constexpr_ctx
*ctx
, tree t
,
5014 bool *non_constant_p
, bool *overflow_p
)
5016 tree orig_op0
= TREE_OPERAND (t
, 0);
5017 bool empty_base
= false;
5019 /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second
5020 operand is an integer-zero. Otherwise reject the MEM_REF for now. */
5022 if (TREE_CODE (t
) == MEM_REF
5023 && (!TREE_OPERAND (t
, 1) || !integer_zerop (TREE_OPERAND (t
, 1))))
5025 gcc_assert (ctx
->quiet
);
5026 *non_constant_p
= true;
5030 /* First try to simplify it directly. */
5031 tree r
= cxx_fold_indirect_ref (ctx
, EXPR_LOCATION (t
), TREE_TYPE (t
),
5032 orig_op0
, &empty_base
);
5035 /* If that didn't work, evaluate the operand first. */
5036 tree op0
= cxx_eval_constant_expression (ctx
, orig_op0
,
5037 /*lval*/false, non_constant_p
,
5039 /* Don't VERIFY_CONSTANT here. */
5040 if (*non_constant_p
)
5043 if (!lval
&& integer_zerop (op0
))
5046 error ("dereferencing a null pointer");
5047 *non_constant_p
= true;
5051 r
= cxx_fold_indirect_ref (ctx
, EXPR_LOCATION (t
), TREE_TYPE (t
), op0
,
5055 /* We couldn't fold to a constant value. Make sure it's not
5056 something we should have been able to fold. */
5059 if (TREE_CODE (sub
) == ADDR_EXPR
)
5061 gcc_assert (!similar_type_p
5062 (TREE_TYPE (TREE_TYPE (sub
)), TREE_TYPE (t
)));
5063 /* DR 1188 says we don't have to deal with this. */
5065 error_at (cp_expr_loc_or_input_loc (t
),
5066 "accessing value of %qE through a %qT glvalue in a "
5067 "constant expression", build_fold_indirect_ref (sub
),
5069 *non_constant_p
= true;
5073 if (lval
&& op0
!= orig_op0
)
5074 return build1 (INDIRECT_REF
, TREE_TYPE (t
), op0
);
5076 VERIFY_CONSTANT (t
);
5081 r
= cxx_eval_constant_expression (ctx
, r
,
5082 lval
, non_constant_p
, overflow_p
);
5083 if (*non_constant_p
)
5086 /* If we're pulling out the value of an empty base, just return an empty
5088 if (empty_base
&& !lval
)
5090 r
= build_constructor (TREE_TYPE (t
), NULL
);
5091 TREE_CONSTANT (r
) = true;
5097 /* Complain about R, a VAR_DECL, not being usable in a constant expression.
5098 Shared between potential_constant_expression and
5099 cxx_eval_constant_expression. */
5102 non_const_var_error (location_t loc
, tree r
)
5104 auto_diagnostic_group d
;
5105 tree type
= TREE_TYPE (r
);
5106 if (DECL_NAME (r
) == heap_uninit_identifier
5107 || DECL_NAME (r
) == heap_identifier
5108 || DECL_NAME (r
) == heap_vec_uninit_identifier
5109 || DECL_NAME (r
) == heap_vec_identifier
)
5111 error_at (loc
, "the content of uninitialized storage is not usable "
5112 "in a constant expression");
5113 inform (DECL_SOURCE_LOCATION (r
), "allocated here");
5116 if (DECL_NAME (r
) == heap_deleted_identifier
)
5118 error_at (loc
, "use of allocated storage after deallocation in a "
5119 "constant expression");
5120 inform (DECL_SOURCE_LOCATION (r
), "allocated here");
5123 error_at (loc
, "the value of %qD is not usable in a constant "
5125 /* Avoid error cascade. */
5126 if (DECL_INITIAL (r
) == error_mark_node
)
5128 if (DECL_DECLARED_CONSTEXPR_P (r
))
5129 inform (DECL_SOURCE_LOCATION (r
),
5130 "%qD used in its own initializer", r
);
5131 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5133 if (!CP_TYPE_CONST_P (type
))
5134 inform (DECL_SOURCE_LOCATION (r
),
5135 "%q#D is not const", r
);
5136 else if (CP_TYPE_VOLATILE_P (type
))
5137 inform (DECL_SOURCE_LOCATION (r
),
5138 "%q#D is volatile", r
);
5139 else if (!DECL_INITIAL (r
)
5140 || !TREE_CONSTANT (DECL_INITIAL (r
))
5141 || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
))
5142 inform (DECL_SOURCE_LOCATION (r
),
5143 "%qD was not initialized with a constant "
5148 else if (TYPE_REF_P (type
))
5149 inform (DECL_SOURCE_LOCATION (r
),
5150 "%qD was not initialized with a constant "
5154 if (cxx_dialect
>= cxx11
&& !DECL_DECLARED_CONSTEXPR_P (r
))
5155 inform (DECL_SOURCE_LOCATION (r
),
5156 "%qD was not declared %<constexpr%>", r
);
5158 inform (DECL_SOURCE_LOCATION (r
),
5159 "%qD does not have integral or enumeration type",
5164 /* Subroutine of cxx_eval_constant_expression.
5165 Like cxx_eval_unary_expression, except for trinary expressions. */
5168 cxx_eval_trinary_expression (const constexpr_ctx
*ctx
, tree t
,
5170 bool *non_constant_p
, bool *overflow_p
)
5176 for (i
= 0; i
< 3; i
++)
5178 args
[i
] = cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, i
),
5180 non_constant_p
, overflow_p
);
5181 VERIFY_CONSTANT (args
[i
]);
5184 val
= fold_ternary_loc (EXPR_LOCATION (t
), TREE_CODE (t
), TREE_TYPE (t
),
5185 args
[0], args
[1], args
[2]);
5186 if (val
== NULL_TREE
)
5188 VERIFY_CONSTANT (val
);
5192 /* True if T was declared in a function declared to be constexpr, and
5193 therefore potentially constant in C++14. */
5196 var_in_constexpr_fn (tree t
)
5198 tree ctx
= DECL_CONTEXT (t
);
5199 return (ctx
&& TREE_CODE (ctx
) == FUNCTION_DECL
5200 && DECL_DECLARED_CONSTEXPR_P (ctx
));
5203 /* True if a function might be constexpr: either a function that was
5204 declared constexpr, or a C++17 lambda op(). */
5207 maybe_constexpr_fn (tree t
)
5209 return (DECL_DECLARED_CONSTEXPR_P (t
)
5210 || (cxx_dialect
>= cxx17
&& LAMBDA_FUNCTION_P (t
)));
5213 /* True if T was declared in a function that might be constexpr: either a
5214 function that was declared constexpr, or a C++17 lambda op(). */
5217 var_in_maybe_constexpr_fn (tree t
)
5219 if (cxx_dialect
>= cxx17
5220 && DECL_FUNCTION_SCOPE_P (t
)
5221 && LAMBDA_FUNCTION_P (DECL_CONTEXT (t
)))
5223 return var_in_constexpr_fn (t
);
5226 /* We're assigning INIT to TARGET. In do_build_copy_constructor and
5227 build_over_call we implement trivial copy of a class with tail padding using
5228 assignment of character arrays, which is valid in normal code, but not in
5229 constexpr evaluation. We don't need to worry about clobbering tail padding
5230 in constexpr evaluation, so strip the type punning. */
5233 maybe_simplify_trivial_copy (tree
&target
, tree
&init
)
5235 if (TREE_CODE (target
) == MEM_REF
5236 && TREE_CODE (init
) == MEM_REF
5237 && TREE_TYPE (target
) == TREE_TYPE (init
)
5238 && TREE_CODE (TREE_TYPE (target
)) == ARRAY_TYPE
5239 && TREE_TYPE (TREE_TYPE (target
)) == unsigned_char_type_node
)
5241 target
= build_fold_indirect_ref (TREE_OPERAND (target
, 0));
5242 init
= build_fold_indirect_ref (TREE_OPERAND (init
, 0));
5246 /* Returns true if REF, which is a COMPONENT_REF, has any fields
5247 of constant type. This does not check for 'mutable', so the
5248 caller is expected to be mindful of that. */
5251 cref_has_const_field (tree ref
)
5253 while (TREE_CODE (ref
) == COMPONENT_REF
)
5255 if (CP_TYPE_CONST_P (TREE_TYPE (TREE_OPERAND (ref
, 1))))
5257 ref
= TREE_OPERAND (ref
, 0);
5262 /* Return true if we are modifying something that is const during constant
5263 expression evaluation. CODE is the code of the statement, OBJ is the
5264 object in question, MUTABLE_P is true if one of the subobjects were
5265 declared mutable. */
5268 modifying_const_object_p (tree_code code
, tree obj
, bool mutable_p
)
5270 /* If this is initialization, there's no problem. */
5271 if (code
!= MODIFY_EXPR
)
5274 /* [basic.type.qualifier] "A const object is an object of type
5275 const T or a non-mutable subobject of a const object." */
5279 if (TREE_READONLY (obj
))
5282 if (CP_TYPE_CONST_P (TREE_TYPE (obj
)))
5284 /* Although a COMPONENT_REF may have a const type, we should
5285 only consider it modifying a const object when any of the
5286 field components is const. This can happen when using
5287 constructs such as const_cast<const T &>(m), making something
5288 const even though it wasn't declared const. */
5289 if (TREE_CODE (obj
) == COMPONENT_REF
)
5290 return cref_has_const_field (obj
);
5298 /* Evaluate an INIT_EXPR or MODIFY_EXPR. */
5301 cxx_eval_store_expression (const constexpr_ctx
*ctx
, tree t
,
5303 bool *non_constant_p
, bool *overflow_p
)
5305 constexpr_ctx new_ctx
= *ctx
;
5307 tree init
= TREE_OPERAND (t
, 1);
5308 if (TREE_CLOBBER_P (init
))
5309 /* Just ignore clobbers. */
5312 /* First we figure out where we're storing to. */
5313 tree target
= TREE_OPERAND (t
, 0);
5315 maybe_simplify_trivial_copy (target
, init
);
5317 tree type
= TREE_TYPE (target
);
5318 bool preeval
= SCALAR_TYPE_P (type
) || TREE_CODE (t
) == MODIFY_EXPR
;
5321 /* Evaluate the value to be stored without knowing what object it will be
5322 stored in, so that any side-effects happen first. */
5323 if (!SCALAR_TYPE_P (type
))
5324 new_ctx
.ctor
= new_ctx
.object
= NULL_TREE
;
5325 init
= cxx_eval_constant_expression (&new_ctx
, init
, false,
5326 non_constant_p
, overflow_p
);
5327 if (*non_constant_p
)
5331 bool evaluated
= false;
5334 /* If we want to return a reference to the target, we need to evaluate it
5335 as a whole; otherwise, only evaluate the innermost piece to avoid
5336 building up unnecessary *_REFs. */
5337 target
= cxx_eval_constant_expression (ctx
, target
, true,
5338 non_constant_p
, overflow_p
);
5340 if (*non_constant_p
)
5344 /* Find the underlying variable. */
5346 tree object
= NULL_TREE
;
5347 /* If we're modifying a const object, save it. */
5348 tree const_object_being_modified
= NULL_TREE
;
5349 bool mutable_p
= false;
5350 for (tree probe
= target
; object
== NULL_TREE
; )
5352 switch (TREE_CODE (probe
))
5358 tree ob
= TREE_OPERAND (probe
, 0);
5359 tree elt
= TREE_OPERAND (probe
, 1);
5360 if (TREE_CODE (elt
) == FIELD_DECL
&& DECL_MUTABLE_P (elt
))
5362 if (TREE_CODE (probe
) == ARRAY_REF
)
5364 elt
= eval_and_check_array_index (ctx
, probe
, false,
5365 non_constant_p
, overflow_p
);
5366 if (*non_constant_p
)
5369 /* We don't check modifying_const_object_p for ARRAY_REFs. Given
5370 "int a[10]", an ARRAY_REF "a[2]" can be "const int", even though
5371 the array isn't const. Instead, check "a" in the next iteration;
5372 that will detect modifying "const int a[10]". */
5374 && modifying_const_object_p (TREE_CODE (t
), probe
,
5376 && const_object_being_modified
== NULL_TREE
)
5377 const_object_being_modified
= probe
;
5378 vec_safe_push (refs
, elt
);
5379 vec_safe_push (refs
, TREE_TYPE (probe
));
5389 probe
= cxx_eval_constant_expression (ctx
, probe
, true,
5390 non_constant_p
, overflow_p
);
5392 if (*non_constant_p
)
5399 if (modifying_const_object_p (TREE_CODE (t
), object
, mutable_p
)
5400 && const_object_being_modified
== NULL_TREE
)
5401 const_object_being_modified
= object
;
5403 /* And then find/build up our initializer for the path to the subobject
5404 we're initializing. */
5406 if (DECL_P (object
))
5407 valp
= ctx
->global
->values
.get (object
);
5412 /* A constant-expression cannot modify objects from outside the
5413 constant-expression. */
5415 error ("modification of %qE is not a constant expression", object
);
5416 *non_constant_p
= true;
5419 type
= TREE_TYPE (object
);
5420 bool no_zero_init
= true;
5422 releasing_vec ctors
, indexes
;
5423 auto_vec
<int> index_pos_hints
;
5424 bool activated_union_member_p
= false;
5425 while (!refs
->is_empty ())
5427 if (*valp
== NULL_TREE
)
5429 *valp
= build_constructor (type
, NULL
);
5430 CONSTRUCTOR_NO_CLEARING (*valp
) = no_zero_init
;
5432 else if (TREE_CODE (*valp
) == STRING_CST
)
5434 /* An array was initialized with a string constant, and now
5435 we're writing into one of its elements. Explode the
5436 single initialization into a set of element
5438 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
5440 tree string
= *valp
;
5441 tree elt_type
= TREE_TYPE (type
);
5442 unsigned chars_per_elt
= (TYPE_PRECISION (elt_type
)
5443 / TYPE_PRECISION (char_type_node
));
5444 unsigned num_elts
= TREE_STRING_LENGTH (string
) / chars_per_elt
;
5445 tree ary_ctor
= build_constructor (type
, NULL
);
5447 vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor
), num_elts
);
5448 for (unsigned ix
= 0; ix
!= num_elts
; ix
++)
5450 constructor_elt elt
=
5452 build_int_cst (size_type_node
, ix
),
5453 extract_string_elt (string
, chars_per_elt
, ix
)
5455 CONSTRUCTOR_ELTS (ary_ctor
)->quick_push (elt
);
5461 /* If the value of object is already zero-initialized, any new ctors for
5462 subobjects will also be zero-initialized. */
5463 no_zero_init
= CONSTRUCTOR_NO_CLEARING (*valp
);
5465 enum tree_code code
= TREE_CODE (type
);
5467 tree index
= refs
->pop();
5469 if (code
== RECORD_TYPE
&& is_empty_field (index
))
5470 /* Don't build a sub-CONSTRUCTOR for an empty base or field, as they
5471 have no data and might have an offset lower than previously declared
5472 fields, which confuses the middle-end. The code below will notice
5473 that we don't have a CONSTRUCTOR for our inner target and just
5477 if (code
== UNION_TYPE
&& CONSTRUCTOR_NELTS (*valp
)
5478 && CONSTRUCTOR_ELT (*valp
, 0)->index
!= index
)
5480 if (cxx_dialect
< cxx20
)
5483 error_at (cp_expr_loc_or_input_loc (t
),
5484 "change of the active member of a union "
5486 CONSTRUCTOR_ELT (*valp
, 0)->index
,
5488 *non_constant_p
= true;
5490 else if (TREE_CODE (t
) == MODIFY_EXPR
5491 && CONSTRUCTOR_NO_CLEARING (*valp
))
5493 /* Diagnose changing the active union member while the union
5494 is in the process of being initialized. */
5496 error_at (cp_expr_loc_or_input_loc (t
),
5497 "change of the active member of a union "
5498 "from %qD to %qD during initialization",
5499 CONSTRUCTOR_ELT (*valp
, 0)->index
,
5501 *non_constant_p
= true;
5503 no_zero_init
= true;
5506 vec_safe_push (ctors
, *valp
);
5507 vec_safe_push (indexes
, index
);
5509 constructor_elt
*cep
5510 = get_or_insert_ctor_field (*valp
, index
);
5511 index_pos_hints
.safe_push (cep
- CONSTRUCTOR_ELTS (*valp
)->begin());
5513 if (code
== UNION_TYPE
)
5514 activated_union_member_p
= true;
5519 /* Detect modifying a constant object in constexpr evaluation.
5520 We have found a const object that is being modified. Figure out
5521 if we need to issue an error. Consider
5525 constexpr A() : n(1) { n = 2; } // #1
5529 constexpr B() { a.n = 3; } // #2
5533 #1 is OK, since we're modifying an object under construction, but
5534 #2 is wrong, since "a" is const and has been fully constructed.
5535 To track it, we use the TREE_READONLY bit in the object's CONSTRUCTOR
5536 which means that the object is read-only. For the example above, the
5537 *ctors stack at the point of #2 will look like:
5539 ctors[0] = {.a={.n=2}} TREE_READONLY = 0
5540 ctors[1] = {.n=2} TREE_READONLY = 1
5542 and we're modifying "b.a", so we search the stack and see if the
5543 constructor for "b.a" has already run. */
5544 if (const_object_being_modified
)
5548 = strip_array_types (TREE_TYPE (const_object_being_modified
));
5549 if (!CLASS_TYPE_P (const_objtype
))
5553 /* [class.ctor]p5 "A constructor can be invoked for a const,
5554 volatile, or const volatile object. const and volatile
5555 semantics are not applied on an object under construction.
5556 They come into effect when the constructor for the most
5557 derived object ends." */
5558 for (tree elt
: *ctors
)
5559 if (same_type_ignoring_top_level_qualifiers_p
5560 (TREE_TYPE (const_object_being_modified
), TREE_TYPE (elt
)))
5562 fail
= TREE_READONLY (elt
);
5569 modifying_const_object_error (t
, const_object_being_modified
);
5570 *non_constant_p
= true;
5577 /* We're handling an INIT_EXPR of class type, so the value of the
5578 initializer can depend on the object it's initializing. */
5580 /* Create a new CONSTRUCTOR in case evaluation of the initializer
5581 wants to modify it. */
5582 if (*valp
== NULL_TREE
)
5584 *valp
= build_constructor (type
, NULL
);
5585 CONSTRUCTOR_NO_CLEARING (*valp
) = no_zero_init
;
5587 new_ctx
.ctor
= *valp
;
5588 new_ctx
.object
= target
;
5589 /* Avoid temporary materialization when initializing from a TARGET_EXPR.
5590 We don't need to mess with AGGR_EXPR_SLOT/VEC_INIT_EXPR_SLOT because
5591 expansion of those trees uses ctx instead. */
5592 if (TREE_CODE (init
) == TARGET_EXPR
)
5593 if (tree tinit
= TARGET_EXPR_INITIAL (init
))
5595 init
= cxx_eval_constant_expression (&new_ctx
, init
, false,
5596 non_constant_p
, overflow_p
);
5597 /* The hash table might have moved since the get earlier, and the
5598 initializer might have mutated the underlying CONSTRUCTORs, so we must
5600 valp
= ctx
->global
->values
.get (object
);
5601 for (unsigned i
= 0; i
< vec_safe_length (indexes
); i
++)
5603 constructor_elt
*cep
5604 = get_or_insert_ctor_field (*valp
, indexes
[i
], index_pos_hints
[i
]);
5609 /* Don't share a CONSTRUCTOR that might be changed later. */
5610 init
= unshare_constructor (init
);
5612 if (*valp
&& TREE_CODE (*valp
) == CONSTRUCTOR
5613 && TREE_CODE (init
) == CONSTRUCTOR
)
5615 /* An outer ctx->ctor might be pointing to *valp, so replace
5617 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init
),
5620 /* For initialization of an empty base, the original target will be
5621 *(base*)this, evaluation of which resolves to the object
5622 argument, which has the derived type rather than the base type. In
5623 this situation, just evaluate the initializer and return, since
5624 there's no actual data to store. */
5625 gcc_assert (is_empty_class (TREE_TYPE (init
)));
5626 return lval
? target
: init
;
5628 CONSTRUCTOR_ELTS (*valp
) = CONSTRUCTOR_ELTS (init
);
5629 TREE_CONSTANT (*valp
) = TREE_CONSTANT (init
);
5630 TREE_SIDE_EFFECTS (*valp
) = TREE_SIDE_EFFECTS (init
);
5631 CONSTRUCTOR_NO_CLEARING (*valp
)
5632 = CONSTRUCTOR_NO_CLEARING (init
);
5634 else if (TREE_CODE (init
) == CONSTRUCTOR
5635 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init
),
5638 /* See above on initialization of empty bases. */
5639 gcc_assert (is_empty_class (TREE_TYPE (init
)) && !lval
);
5645 /* After initialization, 'const' semantics apply to the value of the
5646 object. Make a note of this fact by marking the CONSTRUCTOR
5648 if (TREE_CODE (t
) == INIT_EXPR
5649 && TREE_CODE (*valp
) == CONSTRUCTOR
5650 && TYPE_READONLY (type
))
5652 if (INDIRECT_REF_P (target
)
5653 && (is_this_parameter
5654 (tree_strip_nop_conversions (TREE_OPERAND (target
, 0)))))
5655 /* We've just initialized '*this' (perhaps via the target
5656 constructor of a delegating constructor). Leave it up to the
5657 caller that set 'this' to set TREE_READONLY appropriately. */
5658 gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
5659 (TREE_TYPE (target
), type
));
5661 TREE_READONLY (*valp
) = true;
5664 /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing
5665 CONSTRUCTORs, if any. */
5666 bool c
= TREE_CONSTANT (init
);
5667 bool s
= TREE_SIDE_EFFECTS (init
);
5668 if (!c
|| s
|| activated_union_member_p
)
5669 for (tree elt
: *ctors
)
5672 TREE_CONSTANT (elt
) = false;
5674 TREE_SIDE_EFFECTS (elt
) = true;
5675 /* Clear CONSTRUCTOR_NO_CLEARING since we've activated a member of
5677 if (TREE_CODE (TREE_TYPE (elt
)) == UNION_TYPE
)
5678 CONSTRUCTOR_NO_CLEARING (elt
) = false;
5681 if (*non_constant_p
)
5689 /* Evaluate a ++ or -- expression. */
5692 cxx_eval_increment_expression (const constexpr_ctx
*ctx
, tree t
,
5694 bool *non_constant_p
, bool *overflow_p
)
5696 enum tree_code code
= TREE_CODE (t
);
5697 tree type
= TREE_TYPE (t
);
5698 tree op
= TREE_OPERAND (t
, 0);
5699 tree offset
= TREE_OPERAND (t
, 1);
5700 gcc_assert (TREE_CONSTANT (offset
));
5702 /* OFFSET is constant, but perhaps not constant enough. We need to
5703 e.g. bash FLOAT_EXPRs to REAL_CSTs. */
5704 offset
= fold_simple (offset
);
5706 /* The operand as an lvalue. */
5707 op
= cxx_eval_constant_expression (ctx
, op
, true,
5708 non_constant_p
, overflow_p
);
5710 /* The operand as an rvalue. */
5712 = cxx_eval_constant_expression (ctx
, op
, false,
5713 non_constant_p
, overflow_p
);
5714 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
5715 a local array in a constexpr function. */
5716 bool ptr
= INDIRECT_TYPE_P (TREE_TYPE (val
));
5718 VERIFY_CONSTANT (val
);
5720 /* The modified value. */
5721 bool inc
= (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
);
5723 if (INDIRECT_TYPE_P (type
))
5725 /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
5726 offset
= convert_to_ptrofftype (offset
);
5728 offset
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (offset
), offset
);
5729 mod
= fold_build2 (POINTER_PLUS_EXPR
, type
, val
, offset
);
5732 mod
= fold_build2 (inc
? PLUS_EXPR
: MINUS_EXPR
, type
, val
, offset
);
5734 VERIFY_CONSTANT (mod
);
5736 /* Storing the modified value. */
5737 tree store
= build2_loc (cp_expr_loc_or_loc (t
, input_location
),
5738 MODIFY_EXPR
, type
, op
, mod
);
5739 mod
= cxx_eval_constant_expression (ctx
, store
, lval
,
5740 non_constant_p
, overflow_p
);
5742 if (*non_constant_p
)
5745 /* And the value of the expression. */
5746 if (code
== PREINCREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
5747 /* Prefix ops are lvalues, but the caller might want an rvalue;
5748 lval has already been taken into account in the store above. */
5751 /* Postfix ops are rvalues. */
5755 /* Predicates for the meaning of *jump_target. */
5758 returns (tree
*jump_target
)
5761 && (TREE_CODE (*jump_target
) == RETURN_EXPR
5762 || (TREE_CODE (*jump_target
) == LABEL_DECL
5763 && LABEL_DECL_CDTOR (*jump_target
)));
5767 breaks (tree
*jump_target
)
5770 && ((TREE_CODE (*jump_target
) == LABEL_DECL
5771 && LABEL_DECL_BREAK (*jump_target
))
5772 || TREE_CODE (*jump_target
) == BREAK_STMT
5773 || TREE_CODE (*jump_target
) == EXIT_EXPR
);
5777 continues (tree
*jump_target
)
5780 && ((TREE_CODE (*jump_target
) == LABEL_DECL
5781 && LABEL_DECL_CONTINUE (*jump_target
))
5782 || TREE_CODE (*jump_target
) == CONTINUE_STMT
);
5787 switches (tree
*jump_target
)
5790 && TREE_CODE (*jump_target
) == INTEGER_CST
;
5793 /* Subroutine of cxx_eval_statement_list. Determine whether the statement
5794 STMT matches *jump_target. If we're looking for a case label and we see
5795 the default label, note it in ctx->css_state. */
5798 label_matches (const constexpr_ctx
*ctx
, tree
*jump_target
, tree stmt
)
5800 switch (TREE_CODE (*jump_target
))
5803 if (TREE_CODE (stmt
) == LABEL_EXPR
5804 && LABEL_EXPR_LABEL (stmt
) == *jump_target
)
5809 if (TREE_CODE (stmt
) == CASE_LABEL_EXPR
)
5811 gcc_assert (ctx
->css_state
!= NULL
);
5812 if (!CASE_LOW (stmt
))
5814 /* default: should appear just once in a SWITCH_EXPR
5815 body (excluding nested SWITCH_EXPR). */
5816 gcc_assert (*ctx
->css_state
!= css_default_seen
);
5817 /* When evaluating SWITCH_EXPR body for the second time,
5818 return true for the default: label. */
5819 if (*ctx
->css_state
== css_default_processing
)
5821 *ctx
->css_state
= css_default_seen
;
5823 else if (CASE_HIGH (stmt
))
5825 if (tree_int_cst_le (CASE_LOW (stmt
), *jump_target
)
5826 && tree_int_cst_le (*jump_target
, CASE_HIGH (stmt
)))
5829 else if (tree_int_cst_equal (*jump_target
, CASE_LOW (stmt
)))
5836 /* These two are handled directly in cxx_eval_loop_expr by testing
5837 breaks (jump_target) or continues (jump_target). */
5846 /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
5847 semantics, for switch, break, continue, and return. */
5850 cxx_eval_statement_list (const constexpr_ctx
*ctx
, tree t
,
5851 bool *non_constant_p
, bool *overflow_p
,
5855 /* In a statement-expression we want to return the last value.
5856 For empty statement expression return void_node. */
5860 local_target
= NULL_TREE
;
5861 jump_target
= &local_target
;
5863 for (tree stmt
: tsi_range (t
))
5865 /* We've found a continue, so skip everything until we reach
5866 the label its jumping to. */
5867 if (continues (jump_target
))
5869 if (label_matches (ctx
, jump_target
, stmt
))
5871 *jump_target
= NULL_TREE
;
5875 if (TREE_CODE (stmt
) == DEBUG_BEGIN_STMT
)
5877 r
= cxx_eval_constant_expression (ctx
, stmt
, false,
5878 non_constant_p
, overflow_p
,
5880 if (*non_constant_p
)
5882 if (returns (jump_target
) || breaks (jump_target
))
5885 if (*jump_target
&& jump_target
== &local_target
)
5887 /* We aren't communicating the jump to our caller, so give up. We don't
5888 need to support evaluation of jumps out of statement-exprs. */
5890 error_at (cp_expr_loc_or_input_loc (r
),
5891 "statement is not a constant expression");
5892 *non_constant_p
= true;
5897 /* Evaluate a LOOP_EXPR for side-effects. Handles break and return
5898 semantics; continue semantics are covered by cxx_eval_statement_list. */
5901 cxx_eval_loop_expr (const constexpr_ctx
*ctx
, tree t
,
5902 bool *non_constant_p
, bool *overflow_p
,
5905 constexpr_ctx new_ctx
= *ctx
;
5909 local_target
= NULL_TREE
;
5910 jump_target
= &local_target
;
5913 tree body
, cond
= NULL_TREE
, expr
= NULL_TREE
;
5915 switch (TREE_CODE (t
))
5918 body
= LOOP_EXPR_BODY (t
);
5925 body
= WHILE_BODY (t
);
5926 cond
= WHILE_COND (t
);
5930 if (FOR_INIT_STMT (t
))
5931 cxx_eval_constant_expression (ctx
, FOR_INIT_STMT (t
), /*lval*/false,
5932 non_constant_p
, overflow_p
, jump_target
);
5933 if (*non_constant_p
)
5935 body
= FOR_BODY (t
);
5936 cond
= FOR_COND (t
);
5937 expr
= FOR_EXPR (t
);
5943 auto_vec
<tree
, 10> save_exprs
;
5944 new_ctx
.save_exprs
= &save_exprs
;
5950 cxx_eval_constant_expression (&new_ctx
, body
, /*lval*/false,
5951 non_constant_p
, overflow_p
,
5953 if (breaks (jump_target
))
5955 *jump_target
= NULL_TREE
;
5959 if (TREE_CODE (t
) != LOOP_EXPR
&& continues (jump_target
))
5960 *jump_target
= NULL_TREE
;
5963 cxx_eval_constant_expression (&new_ctx
, expr
, /*lval*/false,
5964 non_constant_p
, overflow_p
,
5971 = cxx_eval_constant_expression (&new_ctx
, cond
, /*lval*/false,
5972 non_constant_p
, overflow_p
,
5976 if (verify_constant (res
, ctx
->quiet
, non_constant_p
,
5979 if (integer_zerop (res
))
5983 gcc_assert (*jump_target
);
5986 /* Forget saved values of SAVE_EXPRs and TARGET_EXPRs. */
5987 for (tree save_expr
: save_exprs
)
5988 ctx
->global
->values
.remove (save_expr
);
5989 save_exprs
.truncate (0);
5991 if (++count
>= constexpr_loop_limit
)
5994 error_at (cp_expr_loc_or_input_loc (t
),
5995 "%<constexpr%> loop iteration count exceeds limit of %d "
5996 "(use %<-fconstexpr-loop-limit=%> to increase the limit)",
5997 constexpr_loop_limit
);
5998 *non_constant_p
= true;
6002 while (!returns (jump_target
)
6003 && !breaks (jump_target
)
6004 && !continues (jump_target
)
6005 && (!switches (jump_target
) || count
== 0)
6006 && !*non_constant_p
);
6008 /* Forget saved values of SAVE_EXPRs and TARGET_EXPRs. */
6009 for (tree save_expr
: save_exprs
)
6010 ctx
->global
->values
.remove (save_expr
);
6015 /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
6019 cxx_eval_switch_expr (const constexpr_ctx
*ctx
, tree t
,
6020 bool *non_constant_p
, bool *overflow_p
,
6024 = TREE_CODE (t
) == SWITCH_STMT
? SWITCH_STMT_COND (t
) : SWITCH_COND (t
);
6025 cond
= cxx_eval_constant_expression (ctx
, cond
, false,
6026 non_constant_p
, overflow_p
);
6027 VERIFY_CONSTANT (cond
);
6028 *jump_target
= cond
;
6031 = TREE_CODE (t
) == SWITCH_STMT
? SWITCH_STMT_BODY (t
) : SWITCH_BODY (t
);
6032 constexpr_ctx new_ctx
= *ctx
;
6033 constexpr_switch_state css
= css_default_not_seen
;
6034 new_ctx
.css_state
= &css
;
6035 cxx_eval_constant_expression (&new_ctx
, body
, false,
6036 non_constant_p
, overflow_p
, jump_target
);
6037 if (switches (jump_target
) && css
== css_default_seen
)
6039 /* If the SWITCH_EXPR body has default: label, process it once again,
6040 this time instructing label_matches to return true for default:
6041 label on switches (jump_target). */
6042 css
= css_default_processing
;
6043 cxx_eval_constant_expression (&new_ctx
, body
, false,
6044 non_constant_p
, overflow_p
, jump_target
);
6046 if (breaks (jump_target
) || switches (jump_target
))
6047 *jump_target
= NULL_TREE
;
6051 /* Find the object of TYPE under initialization in CTX. */
6054 lookup_placeholder (const constexpr_ctx
*ctx
, bool lval
, tree type
)
6059 /* Prefer the outermost matching object, but don't cross
6060 CONSTRUCTOR_PLACEHOLDER_BOUNDARY constructors. */
6061 if (ctx
->ctor
&& !CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx
->ctor
))
6062 if (tree outer_ob
= lookup_placeholder (ctx
->parent
, lval
, type
))
6065 /* We could use ctx->object unconditionally, but using ctx->ctor when we
6066 can is a minor optimization. */
6067 if (!lval
&& ctx
->ctor
&& same_type_p (TREE_TYPE (ctx
->ctor
), type
))
6073 /* Since an object cannot have a field of its own type, we can search outward
6074 from ctx->object to find the unique containing object of TYPE. */
6075 tree ob
= ctx
->object
;
6078 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob
), type
))
6080 if (handled_component_p (ob
))
6081 ob
= TREE_OPERAND (ob
, 0);
6089 /* Complain about an attempt to evaluate inline assembly. */
6092 inline_asm_in_constexpr_error (location_t loc
)
6094 auto_diagnostic_group d
;
6095 error_at (loc
, "inline assembly is not a constant expression");
6096 inform (loc
, "only unevaluated inline assembly is allowed in a "
6097 "%<constexpr%> function in C++20");
6100 /* We're getting the constant value of DECL in a manifestly constant-evaluated
6101 context; maybe complain about that. */
6104 maybe_warn_about_constant_value (location_t loc
, tree decl
)
6106 static bool explained
= false;
6107 if (cxx_dialect
>= cxx17
6108 && warn_interference_size
6109 && !global_options_set
.x_param_destruct_interfere_size
6110 && DECL_CONTEXT (decl
) == std_node
6111 && id_equal (DECL_NAME (decl
), "hardware_destructive_interference_size")
6112 && (LOCATION_FILE (input_location
) != main_input_filename
6113 || module_exporting_p ())
6114 && warning_at (loc
, OPT_Winterference_size
, "use of %qD", decl
)
6118 inform (loc
, "its value can vary between compiler versions or "
6119 "with different %<-mtune%> or %<-mcpu%> flags");
6120 inform (loc
, "if this use is part of a public ABI, change it to "
6121 "instead use a constant variable you define");
6122 inform (loc
, "the default value for the current CPU tuning "
6123 "is %d bytes", param_destruct_interfere_size
);
6124 inform (loc
, "you can stabilize this value with %<--param "
6125 "hardware_destructive_interference_size=%d%>, or disable "
6126 "this warning with %<-Wno-interference-size%>",
6127 param_destruct_interfere_size
);
6131 /* Attempt to reduce the expression T to a constant value.
6132 On failure, issue diagnostic and return error_mark_node. */
6133 /* FIXME unify with c_fully_fold */
6134 /* FIXME overflow_p is too global */
6137 cxx_eval_constant_expression (const constexpr_ctx
*ctx
, tree t
,
6139 bool *non_constant_p
, bool *overflow_p
,
6140 tree
*jump_target
/* = NULL */)
6142 if (jump_target
&& *jump_target
)
6144 /* If we are jumping, ignore all statements/expressions except those
6145 that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */
6146 switch (TREE_CODE (t
))
6149 case STATEMENT_LIST
:
6158 case CASE_LABEL_EXPR
:
6159 if (label_matches (ctx
, jump_target
, t
))
6161 *jump_target
= NULL_TREE
;
6167 if (error_operand_p (t
))
6169 *non_constant_p
= true;
6173 location_t loc
= cp_expr_loc_or_input_loc (t
);
6175 STRIP_ANY_LOCATION_WRAPPER (t
);
6177 if (CONSTANT_CLASS_P (t
))
6179 if (TREE_OVERFLOW (t
))
6182 permerror (input_location
, "overflow in constant expression");
6183 if (!flag_permissive
|| ctx
->quiet
)
6187 if (TREE_CODE (t
) == INTEGER_CST
6188 && TYPE_PTR_P (TREE_TYPE (t
))
6189 && !integer_zerop (t
))
6192 error ("value %qE of type %qT is not a constant expression",
6194 *non_constant_p
= true;
6200 /* Avoid excessively long constexpr evaluations. */
6201 if (++ctx
->global
->constexpr_ops_count
>= constexpr_ops_limit
)
6205 "%<constexpr%> evaluation operation count exceeds limit of "
6206 "%wd (use %<-fconstexpr-ops-limit=%> to increase the limit)",
6207 constexpr_ops_limit
);
6208 ctx
->global
->constexpr_ops_count
= INTTYPE_MINIMUM (HOST_WIDE_INT
);
6209 *non_constant_p
= true;
6213 constexpr_ctx new_ctx
;
6216 tree_code tcode
= TREE_CODE (t
);
6222 /* We ask for an rvalue for the RESULT_DECL when indirecting
6223 through an invisible reference, or in named return value
6225 if (tree
*p
= ctx
->global
->values
.get (t
))
6230 error ("%qE is not a constant expression", t
);
6231 *non_constant_p
= true;
6236 if (DECL_HAS_VALUE_EXPR_P (t
))
6238 if (is_normal_capture_proxy (t
)
6239 && current_function_decl
== DECL_CONTEXT (t
))
6241 /* Function parms aren't constexpr within the function
6242 definition, so don't try to look at the closure. But if the
6243 captured variable is constant, try to evaluate it directly. */
6244 r
= DECL_CAPTURED_VARIABLE (t
);
6245 tree type
= TREE_TYPE (t
);
6246 if (TYPE_REF_P (type
) != TYPE_REF_P (TREE_TYPE (r
)))
6248 /* Adjust r to match the reference-ness of t. */
6249 if (TYPE_REF_P (type
))
6250 r
= build_address (r
);
6252 r
= convert_from_reference (r
);
6256 r
= DECL_VALUE_EXPR (t
);
6257 return cxx_eval_constant_expression (ctx
, r
, lval
, non_constant_p
,
6262 /* We used to not check lval for CONST_DECL, but darwin.c uses
6263 CONST_DECL for aggregate constants. */
6266 else if (t
== ctx
->object
)
6269 if (tree
*p
= ctx
->global
->values
.get (t
))
6270 if (*p
!= NULL_TREE
)
6275 if (ctx
->manifestly_const_eval
)
6276 maybe_warn_about_constant_value (loc
, t
);
6277 if (COMPLETE_TYPE_P (TREE_TYPE (t
))
6278 && is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
6280 /* If the class is empty, we aren't actually loading anything. */
6281 r
= build_constructor (TREE_TYPE (t
), NULL
);
6282 TREE_CONSTANT (r
) = true;
6284 else if (ctx
->strict
)
6285 r
= decl_really_constant_value (t
, /*unshare_p=*/false);
6287 r
= decl_constant_value (t
, /*unshare_p=*/false);
6288 if (TREE_CODE (r
) == TARGET_EXPR
6289 && TREE_CODE (TARGET_EXPR_INITIAL (r
)) == CONSTRUCTOR
)
6290 r
= TARGET_EXPR_INITIAL (r
);
6294 non_const_var_error (loc
, r
);
6295 *non_constant_p
= true;
6299 case DEBUG_BEGIN_STMT
:
6300 /* ??? It might be nice to retain this information somehow, so
6301 as to be able to step into a constexpr function call. */
6308 case CASE_LABEL_EXPR
:
6313 if (lval
&& !TYPE_REF_P (TREE_TYPE (t
)))
6315 else if (tree
*p
= ctx
->global
->values
.get (r
))
6318 /* Defer in case this is only used for its type. */;
6319 else if (COMPLETE_TYPE_P (TREE_TYPE (t
))
6320 && is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
6322 /* If the class is empty, we aren't actually loading anything. */
6323 r
= build_constructor (TREE_TYPE (t
), NULL
);
6324 TREE_CONSTANT (r
) = true;
6329 error ("%qE is not a constant expression", t
);
6330 *non_constant_p
= true;
6335 case AGGR_INIT_EXPR
:
6336 r
= cxx_eval_call_expression (ctx
, t
, lval
,
6337 non_constant_p
, overflow_p
);
6342 r
= DECL_EXPR_DECL (t
);
6343 if (TREE_CODE (r
) == USING_DECL
)
6348 if (AGGREGATE_TYPE_P (TREE_TYPE (r
))
6349 || VECTOR_TYPE_P (TREE_TYPE (r
)))
6353 new_ctx
.ctor
= build_constructor (TREE_TYPE (r
), NULL
);
6354 CONSTRUCTOR_NO_CLEARING (new_ctx
.ctor
) = true;
6355 ctx
->global
->values
.put (r
, new_ctx
.ctor
);
6359 if (tree init
= DECL_INITIAL (r
))
6361 init
= cxx_eval_constant_expression (ctx
, init
,
6363 non_constant_p
, overflow_p
);
6364 /* Don't share a CONSTRUCTOR that might be changed. */
6365 init
= unshare_constructor (init
);
6366 /* Remember that a constant object's constructor has already
6368 if (CLASS_TYPE_P (TREE_TYPE (r
))
6369 && CP_TYPE_CONST_P (TREE_TYPE (r
)))
6370 TREE_READONLY (init
) = true;
6371 ctx
->global
->values
.put (r
, init
);
6373 else if (ctx
== &new_ctx
)
6374 /* We gave it a CONSTRUCTOR above. */;
6376 ctx
->global
->values
.put (r
, NULL_TREE
);
6382 tree type
= TREE_TYPE (t
);
6384 if (!literal_type_p (type
))
6388 auto_diagnostic_group d
;
6389 error ("temporary of non-literal type %qT in a "
6390 "constant expression", type
);
6391 explain_non_literal_class (type
);
6393 *non_constant_p
= true;
6396 gcc_checking_assert (!TARGET_EXPR_DIRECT_INIT_P (t
));
6397 /* Avoid evaluating a TARGET_EXPR more than once. */
6398 tree slot
= TARGET_EXPR_SLOT (t
);
6399 if (tree
*p
= ctx
->global
->values
.get (slot
))
6406 if ((AGGREGATE_TYPE_P (type
) || VECTOR_TYPE_P (type
)))
6408 /* We're being expanded without an explicit target, so start
6409 initializing a new object; expansion with an explicit target
6410 strips the TARGET_EXPR before we get here. */
6412 /* Link CTX to NEW_CTX so that lookup_placeholder can resolve
6413 any PLACEHOLDER_EXPR within the initializer that refers to the
6414 former object under construction. */
6415 new_ctx
.parent
= ctx
;
6416 new_ctx
.ctor
= build_constructor (type
, NULL
);
6417 CONSTRUCTOR_NO_CLEARING (new_ctx
.ctor
) = true;
6418 new_ctx
.object
= slot
;
6419 ctx
->global
->values
.put (new_ctx
.object
, new_ctx
.ctor
);
6422 /* Pass false for 'lval' because this indicates
6423 initialization of a temporary. */
6424 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
6426 non_constant_p
, overflow_p
);
6427 if (*non_constant_p
)
6429 /* Adjust the type of the result to the type of the temporary. */
6430 r
= adjust_temp_type (type
, r
);
6431 if (TARGET_EXPR_CLEANUP (t
) && !CLEANUP_EH_ONLY (t
))
6432 ctx
->global
->cleanups
->safe_push (TARGET_EXPR_CLEANUP (t
));
6433 r
= unshare_constructor (r
);
6434 ctx
->global
->values
.put (slot
, r
);
6435 if (ctx
->save_exprs
)
6436 ctx
->save_exprs
->safe_push (slot
);
6444 gcc_assert (jump_target
== NULL
|| *jump_target
== NULL_TREE
);
6445 r
= cxx_eval_store_expression (ctx
, t
, lval
,
6446 non_constant_p
, overflow_p
);
6450 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
6452 non_constant_p
, overflow_p
);
6456 if (TREE_OPERAND (t
, 0) != NULL_TREE
)
6457 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
6459 non_constant_p
, overflow_p
);
6467 /* Can happen with ({ return true; }) && false; passed to
6468 maybe_constant_value. There is nothing to jump over in this
6469 case, and the bug will be diagnosed later. */
6470 gcc_assert (ctx
->quiet
);
6471 *non_constant_p
= true;
6476 /* Avoid evaluating a SAVE_EXPR more than once. */
6477 if (tree
*p
= ctx
->global
->values
.get (t
))
6481 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), false,
6482 non_constant_p
, overflow_p
);
6483 if (*non_constant_p
)
6485 ctx
->global
->values
.put (t
, r
);
6486 if (ctx
->save_exprs
)
6487 ctx
->save_exprs
->safe_push (t
);
6491 case TRY_CATCH_EXPR
:
6492 if (TREE_OPERAND (t
, 0) == NULL_TREE
)
6498 case NON_LVALUE_EXPR
:
6500 case MUST_NOT_THROW_EXPR
:
6503 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
6505 non_constant_p
, overflow_p
,
6509 case CLEANUP_POINT_EXPR
:
6511 auto_vec
<tree
, 2> cleanups
;
6512 vec
<tree
> *prev_cleanups
= ctx
->global
->cleanups
;
6513 ctx
->global
->cleanups
= &cleanups
;
6514 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
6516 non_constant_p
, overflow_p
,
6518 ctx
->global
->cleanups
= prev_cleanups
;
6521 /* Evaluate the cleanups. */
6522 FOR_EACH_VEC_ELT_REVERSE (cleanups
, i
, cleanup
)
6523 cxx_eval_constant_expression (ctx
, cleanup
, false,
6524 non_constant_p
, overflow_p
);
6528 case TRY_FINALLY_EXPR
:
6529 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), lval
,
6530 non_constant_p
, overflow_p
,
6532 if (!*non_constant_p
)
6533 /* Also evaluate the cleanup. */
6534 cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1), true,
6535 non_constant_p
, overflow_p
);
6539 r
= cxx_eval_constant_expression (ctx
, CLEANUP_BODY (t
), lval
,
6540 non_constant_p
, overflow_p
,
6542 if (!CLEANUP_EH_ONLY (t
) && !*non_constant_p
)
6544 iloc_sentinel
ils (loc
);
6545 /* Also evaluate the cleanup. */
6546 cxx_eval_constant_expression (ctx
, CLEANUP_EXPR (t
), true,
6547 non_constant_p
, overflow_p
);
6551 /* These differ from cxx_eval_unary_expression in that this doesn't
6552 check for a constant operand or result; an address can be
6553 constant without its operand being, and vice versa. */
6556 r
= cxx_eval_indirect_ref (ctx
, t
, lval
,
6557 non_constant_p
, overflow_p
);
6562 tree oldop
= TREE_OPERAND (t
, 0);
6563 tree op
= cxx_eval_constant_expression (ctx
, oldop
,
6565 non_constant_p
, overflow_p
);
6566 /* Don't VERIFY_CONSTANT here. */
6567 if (*non_constant_p
)
6569 gcc_checking_assert (TREE_CODE (op
) != CONSTRUCTOR
);
6570 /* This function does more aggressive folding than fold itself. */
6571 r
= build_fold_addr_expr_with_type (op
, TREE_TYPE (t
));
6572 if (TREE_CODE (r
) == ADDR_EXPR
&& TREE_OPERAND (r
, 0) == oldop
)
6584 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), lval
,
6585 non_constant_p
, overflow_p
);
6586 if (r
== error_mark_node
)
6588 else if (r
== TREE_OPERAND (t
, 0))
6591 r
= fold_build1 (TREE_CODE (t
), TREE_TYPE (t
), r
);
6596 case FIX_TRUNC_EXPR
:
6602 case TRUTH_NOT_EXPR
:
6603 case FIXED_CONVERT_EXPR
:
6604 r
= cxx_eval_unary_expression (ctx
, t
, lval
,
6605 non_constant_p
, overflow_p
);
6609 r
= fold_sizeof_expr (t
);
6610 /* In a template, fold_sizeof_expr may merely create a new SIZEOF_EXPR,
6611 which could lead to an infinite recursion. */
6612 if (TREE_CODE (r
) != SIZEOF_EXPR
)
6613 r
= cxx_eval_constant_expression (ctx
, r
, lval
,
6614 non_constant_p
, overflow_p
,
6618 *non_constant_p
= true;
6619 gcc_assert (ctx
->quiet
);
6626 /* check_return_expr sometimes wraps a TARGET_EXPR in a
6627 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
6628 introduced by build_call_a. */
6629 tree op0
= TREE_OPERAND (t
, 0);
6630 tree op1
= TREE_OPERAND (t
, 1);
6632 if ((TREE_CODE (op0
) == TARGET_EXPR
&& op1
== TARGET_EXPR_SLOT (op0
))
6633 || TREE_CODE (op1
) == EMPTY_CLASS_EXPR
)
6634 r
= cxx_eval_constant_expression (ctx
, op0
,
6635 lval
, non_constant_p
, overflow_p
,
6639 /* Check that the LHS is constant and then discard it. */
6640 cxx_eval_constant_expression (ctx
, op0
,
6641 true, non_constant_p
, overflow_p
,
6643 if (*non_constant_p
)
6645 op1
= TREE_OPERAND (t
, 1);
6646 r
= cxx_eval_constant_expression (ctx
, op1
,
6647 lval
, non_constant_p
, overflow_p
,
6653 case POINTER_PLUS_EXPR
:
6654 case POINTER_DIFF_EXPR
:
6658 case TRUNC_DIV_EXPR
:
6660 case FLOOR_DIV_EXPR
:
6661 case ROUND_DIV_EXPR
:
6662 case TRUNC_MOD_EXPR
:
6664 case ROUND_MOD_EXPR
:
6666 case EXACT_DIV_EXPR
:
6676 case TRUTH_XOR_EXPR
:
6683 case SPACESHIP_EXPR
:
6684 case UNORDERED_EXPR
:
6694 r
= cxx_eval_binary_expression (ctx
, t
, lval
,
6695 non_constant_p
, overflow_p
);
6698 /* fold can introduce non-IF versions of these; still treat them as
6699 short-circuiting. */
6700 case TRUTH_AND_EXPR
:
6701 case TRUTH_ANDIF_EXPR
:
6702 r
= cxx_eval_logical_expression (ctx
, t
, boolean_false_node
,
6705 non_constant_p
, overflow_p
);
6709 case TRUTH_ORIF_EXPR
:
6710 r
= cxx_eval_logical_expression (ctx
, t
, boolean_true_node
,
6713 non_constant_p
, overflow_p
);
6717 r
= cxx_eval_array_reference (ctx
, t
, lval
,
6718 non_constant_p
, overflow_p
);
6722 if (is_overloaded_fn (t
))
6724 /* We can only get here in checking mode via
6725 build_non_dependent_expr, because any expression that
6726 calls or takes the address of the function will have
6727 pulled a FUNCTION_DECL out of the COMPONENT_REF. */
6728 gcc_checking_assert (ctx
->quiet
|| errorcount
);
6729 *non_constant_p
= true;
6732 r
= cxx_eval_component_reference (ctx
, t
, lval
,
6733 non_constant_p
, overflow_p
);
6737 r
= cxx_eval_bit_field_ref (ctx
, t
, lval
,
6738 non_constant_p
, overflow_p
);
6743 if (jump_target
&& *jump_target
)
6745 tree orig_jump
= *jump_target
;
6746 tree arg
= ((TREE_CODE (t
) != IF_STMT
|| TREE_OPERAND (t
, 1))
6747 ? TREE_OPERAND (t
, 1) : void_node
);
6748 /* When jumping to a label, the label might be either in the
6749 then or else blocks, so process then block first in skipping
6750 mode first, and if we are still in the skipping mode at its end,
6751 process the else block too. */
6752 r
= cxx_eval_constant_expression (ctx
, arg
, lval
, non_constant_p
,
6753 overflow_p
, jump_target
);
6754 /* It's possible that we found the label in the then block. But
6755 it could have been followed by another jumping statement, e.g.
6756 say we're looking for case 1:
6759 // skipped statements
6760 case 1:; // clears up *jump_target
6761 return 1; // and sets it to a RETURN_EXPR
6764 in which case we need not go looking to the else block.
6765 (goto is not allowed in a constexpr function.) */
6766 if (*jump_target
== orig_jump
)
6768 arg
= ((TREE_CODE (t
) != IF_STMT
|| TREE_OPERAND (t
, 2))
6769 ? TREE_OPERAND (t
, 2) : void_node
);
6770 r
= cxx_eval_constant_expression (ctx
, arg
, lval
, non_constant_p
,
6771 overflow_p
, jump_target
);
6775 r
= cxx_eval_conditional_expression (ctx
, t
, lval
,
6776 non_constant_p
, overflow_p
,
6780 r
= cxx_eval_vector_conditional_expression (ctx
, t
, non_constant_p
,
6785 if (TREE_CONSTANT (t
) && reduced_constant_expression_p (t
))
6787 /* Don't re-process a constant CONSTRUCTOR, but do fold it to
6788 VECTOR_CST if applicable. */
6789 verify_constructor_flags (t
);
6790 if (TREE_CONSTANT (t
))
6793 r
= cxx_eval_bare_aggregate (ctx
, t
, lval
,
6794 non_constant_p
, overflow_p
);
6798 /* We can get this in a defaulted constructor for a class with a
6799 non-static data member of array type. Either the initializer will
6800 be NULL, meaning default-initialization, or it will be an lvalue
6801 or xvalue of the same type, meaning direct-initialization from the
6802 corresponding member. */
6803 r
= cxx_eval_vec_init (ctx
, t
, lval
,
6804 non_constant_p
, overflow_p
);
6808 r
= cxx_eval_trinary_expression (ctx
, t
, lval
,
6809 non_constant_p
, overflow_p
);
6813 if (REINTERPRET_CAST_P (t
))
6817 "%<reinterpret_cast%> is not a constant expression");
6818 *non_constant_p
= true;
6823 case VIEW_CONVERT_EXPR
:
6824 case UNARY_PLUS_EXPR
:
6826 tree oldop
= TREE_OPERAND (t
, 0);
6828 tree op
= cxx_eval_constant_expression (ctx
, oldop
,
6830 non_constant_p
, overflow_p
);
6831 if (*non_constant_p
)
6833 tree type
= TREE_TYPE (t
);
6835 if (VOID_TYPE_P (type
))
6838 if (TREE_CODE (t
) == CONVERT_EXPR
6839 && ARITHMETIC_TYPE_P (type
)
6840 && INDIRECT_TYPE_P (TREE_TYPE (op
))
6841 && ctx
->manifestly_const_eval
)
6845 "conversion from pointer type %qT to arithmetic type "
6846 "%qT in a constant expression", TREE_TYPE (op
), type
);
6847 *non_constant_p
= true;
6851 /* [expr.const]: a conversion from type cv void* to a pointer-to-object
6852 type cannot be part of a core constant expression as a resolution to
6854 if (TYPE_PTROB_P (type
)
6855 && TYPE_PTR_P (TREE_TYPE (op
))
6856 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (op
)))
6857 /* Inside a call to std::construct_at or to
6858 std::allocator<T>::{,de}allocate, we permit casting from void*
6859 because that is compiler-generated code. */
6860 && !is_std_construct_at (ctx
->call
)
6861 && !is_std_allocator_allocate (ctx
->call
))
6863 /* Likewise, don't error when casting from void* when OP is
6864 &heap uninit and similar. */
6865 tree sop
= tree_strip_nop_conversions (op
);
6866 if (TREE_CODE (sop
) == ADDR_EXPR
6867 && VAR_P (TREE_OPERAND (sop
, 0))
6868 && DECL_ARTIFICIAL (TREE_OPERAND (sop
, 0)))
6873 error_at (loc
, "cast from %qT is not allowed",
6875 *non_constant_p
= true;
6880 if (TREE_CODE (op
) == PTRMEM_CST
&& !TYPE_PTRMEM_P (type
))
6881 op
= cplus_expand_constant (op
);
6883 if (TREE_CODE (op
) == PTRMEM_CST
&& tcode
== NOP_EXPR
)
6885 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (op
))
6886 && !can_convert_qual (type
, op
))
6887 op
= cplus_expand_constant (op
);
6888 return cp_fold_convert (type
, op
);
6891 if (INDIRECT_TYPE_P (type
) && TREE_CODE (op
) == INTEGER_CST
)
6893 if (integer_zerop (op
))
6895 if (TYPE_REF_P (type
))
6898 error_at (loc
, "dereferencing a null pointer");
6899 *non_constant_p
= true;
6905 /* This detects for example:
6906 reinterpret_cast<void*>(sizeof 0)
6909 error_at (loc
, "%<reinterpret_cast<%T>(%E)%> is not "
6910 "a constant expression",
6912 *non_constant_p
= true;
6917 if (INDIRECT_TYPE_P (type
)
6918 && TREE_CODE (op
) == NOP_EXPR
6919 && TREE_TYPE (op
) == ptr_type_node
6920 && TREE_CODE (TREE_OPERAND (op
, 0)) == ADDR_EXPR
6921 && VAR_P (TREE_OPERAND (TREE_OPERAND (op
, 0), 0))
6922 && (DECL_NAME (TREE_OPERAND (TREE_OPERAND (op
, 0),
6923 0)) == heap_uninit_identifier
6924 || DECL_NAME (TREE_OPERAND (TREE_OPERAND (op
, 0),
6925 0)) == heap_vec_uninit_identifier
))
6927 tree var
= TREE_OPERAND (TREE_OPERAND (op
, 0), 0);
6928 tree var_size
= TYPE_SIZE_UNIT (TREE_TYPE (var
));
6929 tree elt_type
= TREE_TYPE (type
);
6930 tree cookie_size
= NULL_TREE
;
6931 if (TREE_CODE (elt_type
) == RECORD_TYPE
6932 && TYPE_NAME (elt_type
) == heap_identifier
)
6934 tree fld1
= TYPE_FIELDS (elt_type
);
6935 tree fld2
= DECL_CHAIN (fld1
);
6936 elt_type
= TREE_TYPE (TREE_TYPE (fld2
));
6937 cookie_size
= TYPE_SIZE_UNIT (TREE_TYPE (fld1
));
6940 = (DECL_NAME (var
) == heap_uninit_identifier
6941 ? heap_identifier
: heap_vec_identifier
);
6943 = build_new_constexpr_heap_type (elt_type
, cookie_size
,
6945 TREE_TYPE (TREE_OPERAND (op
, 0))
6946 = build_pointer_type (TREE_TYPE (var
));
6949 if (op
== oldop
&& tcode
!= UNARY_PLUS_EXPR
)
6950 /* We didn't fold at the top so we could check for ptr-int
6956 /* Handle an array's bounds having been deduced after we built
6957 the wrapping expression. */
6958 if (same_type_ignoring_tlq_and_bounds_p (type
, TREE_TYPE (op
)))
6960 else if (sop
= tree_strip_nop_conversions (op
),
6961 sop
!= op
&& (same_type_ignoring_tlq_and_bounds_p
6962 (type
, TREE_TYPE (sop
))))
6964 else if (tcode
== UNARY_PLUS_EXPR
)
6965 r
= fold_convert (TREE_TYPE (t
), op
);
6967 r
= fold_build1 (tcode
, type
, op
);
6969 /* Conversion of an out-of-range value has implementation-defined
6970 behavior; the language considers it different from arithmetic
6971 overflow, which is undefined. */
6972 if (TREE_OVERFLOW_P (r
) && !TREE_OVERFLOW_P (op
))
6973 TREE_OVERFLOW (r
) = false;
6977 case EMPTY_CLASS_EXPR
:
6978 /* Handle EMPTY_CLASS_EXPR produced by build_call_a by lowering
6979 it to an appropriate CONSTRUCTOR. */
6980 return build_constructor (TREE_TYPE (t
), NULL
);
6982 case STATEMENT_LIST
:
6984 new_ctx
.ctor
= new_ctx
.object
= NULL_TREE
;
6985 return cxx_eval_statement_list (&new_ctx
, t
,
6986 non_constant_p
, overflow_p
, jump_target
);
6989 return cxx_eval_constant_expression (ctx
, BIND_EXPR_BODY (t
),
6991 non_constant_p
, overflow_p
,
6994 case PREINCREMENT_EXPR
:
6995 case POSTINCREMENT_EXPR
:
6996 case PREDECREMENT_EXPR
:
6997 case POSTDECREMENT_EXPR
:
6998 return cxx_eval_increment_expression (ctx
, t
,
6999 lval
, non_constant_p
, overflow_p
);
7005 case VEC_DELETE_EXPR
:
7008 /* GCC internal stuff. */
7010 case NON_DEPENDENT_EXPR
:
7014 error_at (loc
, "expression %qE is not a constant expression", t
);
7015 *non_constant_p
= true;
7019 /* Virtual function lookup. We don't need to do anything fancy. */
7020 return cxx_eval_constant_expression (ctx
, OBJ_TYPE_REF_EXPR (t
),
7021 lval
, non_constant_p
, overflow_p
);
7023 case PLACEHOLDER_EXPR
:
7024 /* Use of the value or address of the current object. */
7025 if (tree ctor
= lookup_placeholder (ctx
, lval
, TREE_TYPE (t
)))
7027 if (TREE_CODE (ctor
) == CONSTRUCTOR
)
7030 return cxx_eval_constant_expression (ctx
, ctor
, lval
,
7031 non_constant_p
, overflow_p
);
7033 /* A placeholder without a referent. We can get here when
7034 checking whether NSDMIs are noexcept, or in massage_init_elt;
7035 just say it's non-constant for now. */
7036 gcc_assert (ctx
->quiet
);
7037 *non_constant_p
= true;
7042 tree cond
= TREE_OPERAND (t
, 0);
7043 cond
= cxx_eval_constant_expression (ctx
, cond
, /*lval*/false,
7044 non_constant_p
, overflow_p
);
7045 VERIFY_CONSTANT (cond
);
7046 if (integer_nonzerop (cond
))
7052 *jump_target
= TREE_OPERAND (t
, 0);
7053 gcc_assert (breaks (jump_target
) || continues (jump_target
)
7054 /* Allow for jumping to a cdtor_label. */
7055 || returns (jump_target
));
7062 cxx_eval_loop_expr (ctx
, t
,
7063 non_constant_p
, overflow_p
, jump_target
);
7068 cxx_eval_switch_expr (ctx
, t
,
7069 non_constant_p
, overflow_p
, jump_target
);
7073 /* It's possible to get a requires-expression in a constant
7074 expression. For example:
7076 template<typename T> concept bool C() {
7077 return requires (T t) { t; };
7080 template<typename T> requires !C<T>() void f(T);
7082 Normalization leaves f with the associated constraint
7083 '!requires (T t) { ... }' which is not transformed into
7085 if (!processing_template_decl
)
7086 return evaluate_requires_expr (t
);
7088 *non_constant_p
= true;
7092 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
7094 non_constant_p
, overflow_p
,
7102 case TEMPLATE_ID_EXPR
:
7104 /* We can evaluate template-id that refers to a concept only if
7105 the template arguments are non-dependent. */
7106 tree id
= unpack_concept_check (t
);
7107 tree tmpl
= TREE_OPERAND (id
, 0);
7108 if (!concept_definition_p (tmpl
))
7109 internal_error ("unexpected template-id %qE", t
);
7111 if (function_concept_p (tmpl
))
7114 error_at (cp_expr_loc_or_input_loc (t
),
7115 "function concept must be called");
7116 r
= error_mark_node
;
7120 if (!processing_template_decl
7121 && !uid_sensitive_constexpr_evaluation_p ())
7122 r
= evaluate_concept_check (t
);
7124 *non_constant_p
= true;
7131 inline_asm_in_constexpr_error (loc
);
7132 *non_constant_p
= true;
7139 error_at (EXPR_LOCATION (t
),
7140 "address of a call to %qs is not a constant expression",
7141 "__builtin_bit_cast");
7142 *non_constant_p
= true;
7145 r
= cxx_eval_bit_cast (ctx
, t
, non_constant_p
, overflow_p
);
7149 if (STATEMENT_CODE_P (TREE_CODE (t
)))
7151 /* This function doesn't know how to deal with pre-genericize
7152 statements; this can only happen with statement-expressions,
7153 so for now just fail. */
7155 error_at (EXPR_LOCATION (t
),
7156 "statement is not a constant expression");
7159 internal_error ("unexpected expression %qE of kind %s", t
,
7160 get_tree_code_name (TREE_CODE (t
)));
7161 *non_constant_p
= true;
7165 if (r
== error_mark_node
)
7166 *non_constant_p
= true;
7168 if (*non_constant_p
)
7174 /* P0859: A function is needed for constant evaluation if it is a constexpr
7175 function that is named by an expression ([basic.def.odr]) that is
7176 potentially constant evaluated.
7178 So we need to instantiate any constexpr functions mentioned by the
7179 expression even if the definition isn't needed for evaluating the
7183 instantiate_cx_fn_r (tree
*tp
, int *walk_subtrees
, void */
*data*/
)
7185 if (TREE_CODE (*tp
) == FUNCTION_DECL
7186 && DECL_DECLARED_CONSTEXPR_P (*tp
)
7187 && !DECL_INITIAL (*tp
)
7188 && !trivial_fn_p (*tp
)
7189 && DECL_TEMPLOID_INSTANTIATION (*tp
)
7190 && !uid_sensitive_constexpr_evaluation_p ())
7193 instantiate_decl (*tp
, /*defer_ok*/false, /*expl_inst*/false);
7196 else if (TREE_CODE (*tp
) == CALL_EXPR
7197 || TREE_CODE (*tp
) == AGGR_INIT_EXPR
)
7199 if (EXPR_HAS_LOCATION (*tp
))
7200 input_location
= EXPR_LOCATION (*tp
);
7210 instantiate_constexpr_fns (tree t
)
7212 location_t loc
= input_location
;
7213 cp_walk_tree_without_duplicates (&t
, instantiate_cx_fn_r
, NULL
);
7214 input_location
= loc
;
7217 /* Look for heap variables in the expression *TP. */
7220 find_heap_var_refs (tree
*tp
, int *walk_subtrees
, void */
*data*/
)
7223 && (DECL_NAME (*tp
) == heap_uninit_identifier
7224 || DECL_NAME (*tp
) == heap_identifier
7225 || DECL_NAME (*tp
) == heap_vec_uninit_identifier
7226 || DECL_NAME (*tp
) == heap_vec_identifier
7227 || DECL_NAME (*tp
) == heap_deleted_identifier
))
7235 /* Find immediate function decls in *TP if any. */
7238 find_immediate_fndecl (tree
*tp
, int */
*walk_subtrees*/
, void */
*data*/
)
7240 if (TREE_CODE (*tp
) == FUNCTION_DECL
&& DECL_IMMEDIATE_FUNCTION_P (*tp
))
7245 /* ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
7246 STRICT has the same sense as for constant_value_1: true if we only allow
7247 conforming C++ constant expressions, or false if we want a constant value
7248 even if it doesn't conform.
7249 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
7250 per P0595 even when ALLOW_NON_CONSTANT is true.
7251 CONSTEXPR_DTOR is true when evaluating the dtor of a constexpr variable.
7252 OBJECT must be non-NULL in that case. */
7255 cxx_eval_outermost_constant_expr (tree t
, bool allow_non_constant
,
7257 bool manifestly_const_eval
= false,
7258 bool constexpr_dtor
= false,
7259 tree object
= NULL_TREE
)
7261 auto_timevar
time (TV_CONSTEXPR
);
7263 bool non_constant_p
= false;
7264 bool overflow_p
= false;
7266 if (BRACE_ENCLOSED_INITIALIZER_P (t
))
7268 gcc_checking_assert (allow_non_constant
);
7272 constexpr_global_ctx global_ctx
;
7273 constexpr_ctx ctx
= { &global_ctx
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
7274 allow_non_constant
, strict
,
7275 manifestly_const_eval
|| !allow_non_constant
};
7277 /* Turn off -frounding-math for manifestly constant evaluation. */
7278 warning_sentinel
rm (flag_rounding_math
, ctx
.manifestly_const_eval
);
7279 tree type
= initialized_type (t
);
7281 bool is_consteval
= false;
7282 if (VOID_TYPE_P (type
))
7285 /* Used for destructors of array elements. */
7286 type
= TREE_TYPE (object
);
7289 if (cxx_dialect
< cxx20
)
7291 if (TREE_CODE (t
) != CALL_EXPR
&& TREE_CODE (t
) != AGGR_INIT_EXPR
)
7293 /* Calls to immediate functions returning void need to be
7295 tree fndecl
= cp_get_callee_fndecl_nofold (t
);
7296 if (fndecl
== NULL_TREE
|| !DECL_IMMEDIATE_FUNCTION_P (fndecl
))
7299 is_consteval
= true;
7302 else if (cxx_dialect
>= cxx20
7303 && (TREE_CODE (t
) == CALL_EXPR
7304 || TREE_CODE (t
) == AGGR_INIT_EXPR
7305 || TREE_CODE (t
) == TARGET_EXPR
))
7307 /* For non-concept checks, determine if it is consteval. */
7308 if (!concept_check_p (t
))
7311 if (TREE_CODE (x
) == TARGET_EXPR
)
7312 x
= TARGET_EXPR_INITIAL (x
);
7313 tree fndecl
= cp_get_callee_fndecl_nofold (x
);
7314 if (fndecl
&& DECL_IMMEDIATE_FUNCTION_P (fndecl
))
7315 is_consteval
= true;
7318 if (AGGREGATE_TYPE_P (type
) || VECTOR_TYPE_P (type
))
7320 /* In C++14 an NSDMI can participate in aggregate initialization,
7321 and can refer to the address of the object being initialized, so
7322 we need to pass in the relevant VAR_DECL if we want to do the
7323 evaluation in a single pass. The evaluation will dynamically
7324 update ctx.values for the VAR_DECL. We use the same strategy
7325 for C++11 constexpr constructors that refer to the object being
7329 gcc_assert (object
&& VAR_P (object
));
7330 gcc_assert (DECL_DECLARED_CONSTEXPR_P (object
));
7331 gcc_assert (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object
));
7332 if (error_operand_p (DECL_INITIAL (object
)))
7334 ctx
.ctor
= unshare_expr (DECL_INITIAL (object
));
7335 TREE_READONLY (ctx
.ctor
) = false;
7336 /* Temporarily force decl_really_constant_value to return false
7337 for it, we want to use ctx.ctor for the current value instead. */
7338 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object
) = false;
7342 ctx
.ctor
= build_constructor (type
, NULL
);
7343 CONSTRUCTOR_NO_CLEARING (ctx
.ctor
) = true;
7347 if (TREE_CODE (t
) == TARGET_EXPR
)
7348 object
= TARGET_EXPR_SLOT (t
);
7349 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
)
7350 object
= AGGR_INIT_EXPR_SLOT (t
);
7352 ctx
.object
= object
;
7354 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7355 (type
, TREE_TYPE (object
)));
7356 if (object
&& DECL_P (object
))
7357 global_ctx
.values
.put (object
, ctx
.ctor
);
7358 if (TREE_CODE (r
) == TARGET_EXPR
)
7359 /* Avoid creating another CONSTRUCTOR when we expand the
7361 r
= TARGET_EXPR_INITIAL (r
);
7364 auto_vec
<tree
, 16> cleanups
;
7365 global_ctx
.cleanups
= &cleanups
;
7367 instantiate_constexpr_fns (r
);
7368 r
= cxx_eval_constant_expression (&ctx
, r
,
7369 false, &non_constant_p
, &overflow_p
);
7371 if (!constexpr_dtor
)
7372 verify_constant (r
, allow_non_constant
, &non_constant_p
, &overflow_p
);
7374 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object
) = true;
7378 /* Evaluate the cleanups. */
7379 FOR_EACH_VEC_ELT_REVERSE (cleanups
, i
, cleanup
)
7380 cxx_eval_constant_expression (&ctx
, cleanup
, false,
7381 &non_constant_p
, &overflow_p
);
7383 /* Mutable logic is a bit tricky: we want to allow initialization of
7384 constexpr variables with mutable members, but we can't copy those
7385 members to another constexpr variable. */
7386 if (TREE_CODE (r
) == CONSTRUCTOR
&& CONSTRUCTOR_MUTABLE_POISON (r
))
7388 if (!allow_non_constant
)
7389 error ("%qE is not a constant expression because it refers to "
7390 "mutable subobjects of %qT", t
, type
);
7391 non_constant_p
= true;
7394 if (TREE_CODE (r
) == CONSTRUCTOR
&& CONSTRUCTOR_NO_CLEARING (r
))
7396 if (!allow_non_constant
)
7397 error ("%qE is not a constant expression because it refers to "
7398 "an incompletely initialized variable", t
);
7399 TREE_CONSTANT (r
) = false;
7400 non_constant_p
= true;
7403 if (!global_ctx
.heap_vars
.is_empty ())
7405 tree heap_var
= cp_walk_tree_without_duplicates (&r
, find_heap_var_refs
,
7410 if (!allow_non_constant
&& !non_constant_p
)
7411 error_at (DECL_SOURCE_LOCATION (heap_var
),
7412 "%qE is not a constant expression because it refers to "
7413 "a result of %<operator new%>", t
);
7415 non_constant_p
= true;
7417 FOR_EACH_VEC_ELT (global_ctx
.heap_vars
, i
, heap_var
)
7419 if (DECL_NAME (heap_var
) != heap_deleted_identifier
)
7421 if (!allow_non_constant
&& !non_constant_p
)
7422 error_at (DECL_SOURCE_LOCATION (heap_var
),
7423 "%qE is not a constant expression because allocated "
7424 "storage has not been deallocated", t
);
7426 non_constant_p
= true;
7428 varpool_node::get (heap_var
)->remove ();
7432 /* Check that immediate invocation does not return an expression referencing
7433 any immediate function decls. They need to be allowed while parsing
7434 immediate functions, but can't leak outside of them. */
7437 && (current_function_decl
== NULL_TREE
7438 || !DECL_IMMEDIATE_FUNCTION_P (current_function_decl
)))
7439 if (tree immediate_fndecl
7440 = cp_walk_tree_without_duplicates (&r
, find_immediate_fndecl
,
7443 if (!allow_non_constant
&& !non_constant_p
)
7444 error_at (cp_expr_loc_or_input_loc (t
),
7445 "immediate evaluation returns address of immediate "
7446 "function %qD", immediate_fndecl
);
7448 non_constant_p
= true;
7452 /* If we saw something bad, go back to our argument. The wrapping below is
7453 only for the cases of TREE_CONSTANT argument or overflow. */
7456 if (!non_constant_p
&& overflow_p
)
7457 non_constant_p
= true;
7459 /* Unshare the result. */
7460 bool should_unshare
= true;
7461 if (r
== t
|| (TREE_CODE (t
) == TARGET_EXPR
7462 && TARGET_EXPR_INITIAL (t
) == r
))
7463 should_unshare
= false;
7465 if (non_constant_p
&& !allow_non_constant
)
7466 return error_mark_node
;
7467 else if (constexpr_dtor
)
7469 else if (non_constant_p
&& TREE_CONSTANT (r
))
7471 /* This isn't actually constant, so unset TREE_CONSTANT.
7472 Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires
7473 it to be set if it is invariant address, even when it is not
7474 a valid C++ constant expression. Wrap it with a NOP_EXPR
7476 if (EXPR_P (r
) && TREE_CODE (r
) != ADDR_EXPR
)
7478 else if (TREE_CODE (r
) == CONSTRUCTOR
)
7479 r
= build1 (VIEW_CONVERT_EXPR
, TREE_TYPE (r
), r
);
7481 r
= build_nop (TREE_TYPE (r
), r
);
7482 TREE_CONSTANT (r
) = false;
7484 else if (non_constant_p
)
7488 r
= unshare_expr (r
);
7490 if (TREE_CODE (r
) == CONSTRUCTOR
&& CLASS_TYPE_P (TREE_TYPE (r
)))
7492 r
= adjust_temp_type (type
, r
);
7493 if (TREE_CODE (t
) == TARGET_EXPR
7494 && TARGET_EXPR_INITIAL (t
) == r
)
7496 else if (TREE_CODE (t
) != CONSTRUCTOR
)
7498 r
= get_target_expr_sfinae (r
, tf_warning_or_error
| tf_no_cleanup
);
7499 TREE_CONSTANT (r
) = true;
7503 /* Remember the original location if that wouldn't need a wrapper. */
7504 if (location_t loc
= EXPR_LOCATION (t
))
7505 protected_set_expr_location (r
, loc
);
7510 /* If T represents a constant expression returns its reduced value.
7511 Otherwise return error_mark_node. If T is dependent, then
7515 cxx_constant_value (tree t
, tree decl
)
7517 return cxx_eval_outermost_constant_expr (t
, false, true, true, false, decl
);
7520 /* As above, but respect SFINAE. */
7523 cxx_constant_value_sfinae (tree t
, tsubst_flags_t complain
)
7525 bool sfinae
= !(complain
& tf_error
);
7526 tree r
= cxx_eval_outermost_constant_expr (t
, sfinae
, true, true);
7527 if (sfinae
&& !TREE_CONSTANT (r
))
7528 r
= error_mark_node
;
7532 /* Like cxx_constant_value, but used for evaluation of constexpr destructors
7533 of constexpr variables. The actual initializer of DECL is not modified. */
7536 cxx_constant_dtor (tree t
, tree decl
)
7538 cxx_eval_outermost_constant_expr (t
, false, true, true, true, decl
);
7541 /* Helper routine for fold_simple function. Either return simplified
7542 expression T, otherwise NULL_TREE.
7543 In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold
7544 even if we are within template-declaration. So be careful on call, as in
7545 such case types can be undefined. */
7548 fold_simple_1 (tree t
)
7551 enum tree_code code
= TREE_CODE (t
);
7563 return fold_sizeof_expr (t
);
7572 case TRUTH_NOT_EXPR
:
7574 case VIEW_CONVERT_EXPR
:
7577 case FIX_TRUNC_EXPR
:
7578 case FIXED_CONVERT_EXPR
:
7579 case ADDR_SPACE_CONVERT_EXPR
:
7581 op1
= TREE_OPERAND (t
, 0);
7583 t
= const_unop (code
, TREE_TYPE (t
), op1
);
7587 if (CONVERT_EXPR_CODE_P (code
)
7588 && TREE_OVERFLOW_P (t
) && !TREE_OVERFLOW_P (op1
))
7589 TREE_OVERFLOW (t
) = false;
7597 /* If T is a simple constant expression, returns its simplified value.
7598 Otherwise returns T. In contrast to maybe_constant_value we
7599 simplify only few operations on constant-expressions, and we don't
7600 try to simplify constexpressions. */
7603 fold_simple (tree t
)
7605 if (processing_template_decl
)
7608 tree r
= fold_simple_1 (t
);
7615 /* If T is a constant expression, returns its reduced value.
7616 Otherwise, if T does not have TREE_CONSTANT set, returns T.
7617 Otherwise, returns a version of T without TREE_CONSTANT.
7618 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated
7621 static GTY((deletable
)) hash_map
<tree
, tree
> *cv_cache
;
7624 maybe_constant_value (tree t
, tree decl
, bool manifestly_const_eval
)
7628 if (!is_nondependent_constant_expression (t
))
7630 if (TREE_OVERFLOW_P (t
))
7632 t
= build_nop (TREE_TYPE (t
), t
);
7633 TREE_CONSTANT (t
) = false;
7637 else if (CONSTANT_CLASS_P (t
))
7638 /* No caching or evaluation needed. */
7641 if (manifestly_const_eval
)
7642 return cxx_eval_outermost_constant_expr (t
, true, true, true, false, decl
);
7644 if (cv_cache
== NULL
)
7645 cv_cache
= hash_map
<tree
, tree
>::create_ggc (101);
7646 if (tree
*cached
= cv_cache
->get (t
))
7651 r
= break_out_target_exprs (r
, /*clear_loc*/true);
7652 protected_set_expr_location (r
, EXPR_LOCATION (t
));
7657 uid_sensitive_constexpr_evaluation_checker c
;
7658 r
= cxx_eval_outermost_constant_expr (t
, true, true, false, false, decl
);
7659 gcc_checking_assert (r
== t
7660 || CONVERT_EXPR_P (t
)
7661 || TREE_CODE (t
) == VIEW_CONVERT_EXPR
7662 || (TREE_CONSTANT (t
) && !TREE_CONSTANT (r
))
7663 || !cp_tree_equal (r
, t
));
7664 if (!c
.evaluation_restricted_p ())
7665 cv_cache
->put (t
, r
);
7669 /* Dispose of the whole CV_CACHE. */
7672 clear_cv_cache (void)
7674 if (cv_cache
!= NULL
)
7678 /* Dispose of the whole CV_CACHE and FOLD_CACHE. */
7681 clear_cv_and_fold_caches ()
7684 clear_fold_cache ();
7687 /* Internal function handling expressions in templates for
7688 fold_non_dependent_expr and fold_non_dependent_init.
7690 If we're in a template, but T isn't value dependent, simplify
7691 it. We're supposed to treat:
7693 template <typename T> void f(T[1 + 1]);
7694 template <typename T> void f(T[2]);
7696 as two declarations of the same function, for example. */
7699 fold_non_dependent_expr_template (tree t
, tsubst_flags_t complain
,
7700 bool manifestly_const_eval
,
7703 gcc_assert (processing_template_decl
);
7705 if (is_nondependent_constant_expression (t
))
7707 processing_template_decl_sentinel s
;
7708 t
= instantiate_non_dependent_expr_internal (t
, complain
);
7710 if (type_unknown_p (t
) || BRACE_ENCLOSED_INITIALIZER_P (t
))
7712 if (TREE_OVERFLOW_P (t
))
7714 t
= build_nop (TREE_TYPE (t
), t
);
7715 TREE_CONSTANT (t
) = false;
7720 tree r
= cxx_eval_outermost_constant_expr (t
, true, true,
7721 manifestly_const_eval
,
7723 /* cp_tree_equal looks through NOPs, so allow them. */
7724 gcc_checking_assert (r
== t
7725 || CONVERT_EXPR_P (t
)
7726 || TREE_CODE (t
) == VIEW_CONVERT_EXPR
7727 || (TREE_CONSTANT (t
) && !TREE_CONSTANT (r
))
7728 || !cp_tree_equal (r
, t
));
7731 else if (TREE_OVERFLOW_P (t
))
7733 t
= build_nop (TREE_TYPE (t
), t
);
7734 TREE_CONSTANT (t
) = false;
7740 /* Like maybe_constant_value but first fully instantiate the argument.
7742 Note: this is equivalent to instantiate_non_dependent_expr_sfinae
7743 (t, complain) followed by maybe_constant_value but is more efficient,
7744 because it calls instantiation_dependent_expression_p and
7745 potential_constant_expression at most once.
7746 The manifestly_const_eval argument is passed to maybe_constant_value.
7748 Callers should generally pass their active complain, or if they are in a
7749 non-template, diagnosing context, they can use the default of
7750 tf_warning_or_error. Callers that might be within a template context, don't
7751 have a complain parameter, and aren't going to remember the result for long
7752 (e.g. null_ptr_cst_p), can pass tf_none and deal with error_mark_node
7756 fold_non_dependent_expr (tree t
,
7757 tsubst_flags_t complain
/* = tf_warning_or_error */,
7758 bool manifestly_const_eval
/* = false */,
7759 tree object
/* = NULL_TREE */)
7764 if (processing_template_decl
)
7765 return fold_non_dependent_expr_template (t
, complain
,
7766 manifestly_const_eval
, object
);
7768 return maybe_constant_value (t
, object
, manifestly_const_eval
);
7771 /* Like fold_non_dependent_expr, but if EXPR couldn't be folded to a constant,
7772 return the original expression. */
7775 maybe_fold_non_dependent_expr (tree expr
,
7776 tsubst_flags_t complain
/*=tf_warning_or_error*/)
7778 tree t
= fold_non_dependent_expr (expr
, complain
);
7779 if (t
&& TREE_CONSTANT (t
))
7785 /* Like maybe_constant_init but first fully instantiate the argument. */
7788 fold_non_dependent_init (tree t
,
7789 tsubst_flags_t complain
/*=tf_warning_or_error*/,
7790 bool manifestly_const_eval
/*=false*/,
7791 tree object
/* = NULL_TREE */)
7796 if (processing_template_decl
)
7798 t
= fold_non_dependent_expr_template (t
, complain
,
7799 manifestly_const_eval
, object
);
7800 /* maybe_constant_init does this stripping, so do it here too. */
7801 if (TREE_CODE (t
) == TARGET_EXPR
)
7803 tree init
= TARGET_EXPR_INITIAL (t
);
7804 if (TREE_CODE (init
) == CONSTRUCTOR
)
7810 return maybe_constant_init (t
, object
, manifestly_const_eval
);
7813 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
7814 than wrapped in a TARGET_EXPR.
7815 ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
7816 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
7817 per P0595 even when ALLOW_NON_CONSTANT is true. */
7820 maybe_constant_init_1 (tree t
, tree decl
, bool allow_non_constant
,
7821 bool manifestly_const_eval
)
7825 if (TREE_CODE (t
) == EXPR_STMT
)
7826 t
= TREE_OPERAND (t
, 0);
7827 if (TREE_CODE (t
) == CONVERT_EXPR
7828 && VOID_TYPE_P (TREE_TYPE (t
)))
7829 t
= TREE_OPERAND (t
, 0);
7830 if (TREE_CODE (t
) == INIT_EXPR
)
7831 t
= TREE_OPERAND (t
, 1);
7832 if (TREE_CODE (t
) == TARGET_EXPR
)
7833 t
= TARGET_EXPR_INITIAL (t
);
7834 if (!is_nondependent_static_init_expression (t
))
7835 /* Don't try to evaluate it. */;
7836 else if (CONSTANT_CLASS_P (t
) && allow_non_constant
)
7837 /* No evaluation needed. */;
7839 t
= cxx_eval_outermost_constant_expr (t
, allow_non_constant
,
7841 manifestly_const_eval
, false, decl
);
7842 if (TREE_CODE (t
) == TARGET_EXPR
)
7844 tree init
= TARGET_EXPR_INITIAL (t
);
7845 if (TREE_CODE (init
) == CONSTRUCTOR
)
7851 /* Wrapper for maybe_constant_init_1 which permits non constants. */
7854 maybe_constant_init (tree t
, tree decl
, bool manifestly_const_eval
)
7856 return maybe_constant_init_1 (t
, decl
, true, manifestly_const_eval
);
7859 /* Wrapper for maybe_constant_init_1 which does not permit non constants. */
7862 cxx_constant_init (tree t
, tree decl
)
7864 return maybe_constant_init_1 (t
, decl
, false, true);
7868 /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
7869 /* Return true if the object referred to by REF has automatic or thread
7872 enum { ck_ok
, ck_bad
, ck_unknown
};
7874 check_automatic_or_tls (tree ref
)
7877 poly_int64 bitsize
, bitpos
;
7879 int volatilep
= 0, unsignedp
= 0;
7880 tree decl
= get_inner_reference (ref
, &bitsize
, &bitpos
, &offset
,
7881 &mode
, &unsignedp
, &volatilep
, false);
7884 /* If there isn't a decl in the middle, we don't know the linkage here,
7885 and this isn't a constant expression anyway. */
7888 dk
= decl_storage_duration (decl
);
7889 return (dk
== dk_auto
|| dk
== dk_thread
) ? ck_bad
: ck_ok
;
7893 /* Data structure for passing data from potential_constant_expression_1
7894 to check_for_return_continue via cp_walk_tree. */
7895 struct check_for_return_continue_data
{
7896 hash_set
<tree
> *pset
;
7901 /* Helper function for potential_constant_expression_1 SWITCH_STMT handling,
7902 called through cp_walk_tree. Return the first RETURN_EXPR found, or note
7903 the first CONTINUE_STMT and/or BREAK_STMT if RETURN_EXPR is not found. */
7905 check_for_return_continue (tree
*tp
, int *walk_subtrees
, void *data
)
7908 check_for_return_continue_data
*d
= (check_for_return_continue_data
*) data
;
7909 switch (TREE_CODE (t
))
7915 if (d
->continue_stmt
== NULL_TREE
)
7916 d
->continue_stmt
= t
;
7920 if (d
->break_stmt
== NULL_TREE
)
7925 if (tree r = cp_walk_tree (&x, check_for_return_continue, data, \
7929 /* For loops, walk subtrees manually, so that continue stmts found
7930 inside of the bodies of the loops are ignored. */
7933 RECUR (DO_COND (t
));
7934 s
= d
->continue_stmt
;
7936 RECUR (DO_BODY (t
));
7937 d
->continue_stmt
= s
;
7943 RECUR (WHILE_COND (t
));
7944 s
= d
->continue_stmt
;
7946 RECUR (WHILE_BODY (t
));
7947 d
->continue_stmt
= s
;
7953 RECUR (FOR_INIT_STMT (t
));
7954 RECUR (FOR_COND (t
));
7955 RECUR (FOR_EXPR (t
));
7956 s
= d
->continue_stmt
;
7958 RECUR (FOR_BODY (t
));
7959 d
->continue_stmt
= s
;
7963 case RANGE_FOR_STMT
:
7965 RECUR (RANGE_FOR_EXPR (t
));
7966 s
= d
->continue_stmt
;
7968 RECUR (RANGE_FOR_BODY (t
));
7969 d
->continue_stmt
= s
;
7975 RECUR (SWITCH_STMT_COND (t
));
7977 RECUR (SWITCH_STMT_BODY (t
));
7982 case STATEMENT_LIST
:
7995 /* Return true if T denotes a potentially constant expression. Issue
7996 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
7997 an lvalue-rvalue conversion is implied. If NOW is true, we want to
7998 consider the expression in the current context, independent of constexpr
8001 C++0x [expr.const] used to say
8003 6 An expression is a potential constant expression if it is
8004 a constant expression where all occurrences of function
8005 parameters are replaced by arbitrary constant expressions
8006 of the appropriate type.
8008 2 A conditional expression is a constant expression unless it
8009 involves one of the following as a potentially evaluated
8010 subexpression (3.2), but subexpressions of logical AND (5.14),
8011 logical OR (5.15), and conditional (5.16) operations that are
8012 not evaluated are not considered. */
8015 potential_constant_expression_1 (tree t
, bool want_rval
, bool strict
, bool now
,
8016 tsubst_flags_t flags
, tree
*jump_target
)
8018 #define RECUR(T,RV) \
8019 potential_constant_expression_1 ((T), (RV), strict, now, flags, jump_target)
8021 enum { any
= false, rval
= true };
8025 if (t
== error_mark_node
)
8029 location_t loc
= cp_expr_loc_or_input_loc (t
);
8032 /* If we are jumping, ignore everything. This is simpler than the
8033 cxx_eval_constant_expression handling because we only need to be
8034 conservatively correct, and we don't necessarily have a constant value
8035 available, so we don't bother with switch tracking. */
8038 if (TREE_THIS_VOLATILE (t
) && want_rval
)
8040 if (flags
& tf_error
)
8041 error_at (loc
, "lvalue-to-rvalue conversion of a volatile lvalue "
8042 "%qE with type %qT", t
, TREE_TYPE (t
));
8045 if (CONSTANT_CLASS_P (t
))
8047 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_TYPED
)
8048 && TREE_TYPE (t
) == error_mark_node
)
8051 switch (TREE_CODE (t
))
8057 case TEMPLATE_ID_EXPR
:
8059 case CASE_LABEL_EXPR
:
8066 case TEMPLATE_PARM_INDEX
:
8068 case IDENTIFIER_NODE
:
8069 case USERDEF_LITERAL
:
8070 /* We can see a FIELD_DECL in a pointer-to-member expression. */
8075 case PLACEHOLDER_EXPR
:
8078 case DEBUG_BEGIN_STMT
:
8082 if (!RECUR (TREE_OPERAND (t
, 0), any
))
8092 if (now
&& want_rval
)
8094 tree type
= TREE_TYPE (t
);
8095 if ((processing_template_decl
&& !COMPLETE_TYPE_P (type
))
8096 || dependent_type_p (type
)
8097 || is_really_empty_class (type
, /*ignore_vptr*/false))
8098 /* An empty class has no data to read. */
8100 if (flags
& tf_error
)
8101 error ("%qE is not a constant expression", t
);
8106 case AGGR_INIT_EXPR
:
8108 /* -- an invocation of a function other than a constexpr function
8109 or a constexpr constructor. */
8111 tree fun
= get_function_named_in_call (t
);
8112 const int nargs
= call_expr_nargs (t
);
8115 if (fun
== NULL_TREE
)
8117 /* Reset to allow the function to continue past the end
8118 of the block below. Otherwise return early. */
8121 if (TREE_CODE (t
) == CALL_EXPR
8122 && CALL_EXPR_FN (t
) == NULL_TREE
)
8123 switch (CALL_EXPR_IFN (t
))
8125 /* These should be ignored, they are optimized away from
8126 constexpr functions. */
8127 case IFN_UBSAN_NULL
:
8128 case IFN_UBSAN_BOUNDS
:
8129 case IFN_UBSAN_VPTR
:
8130 case IFN_FALLTHROUGH
:
8133 case IFN_ADD_OVERFLOW
:
8134 case IFN_SUB_OVERFLOW
:
8135 case IFN_MUL_OVERFLOW
:
8137 case IFN_VEC_CONVERT
:
8147 /* fold_call_expr can't do anything with IFN calls. */
8148 if (flags
& tf_error
)
8149 error_at (loc
, "call to internal function %qE", t
);
8154 if (fun
&& is_overloaded_fn (fun
))
8156 if (TREE_CODE (fun
) == FUNCTION_DECL
)
8158 if (builtin_valid_in_constant_expr_p (fun
))
8160 if (!DECL_DECLARED_CONSTEXPR_P (fun
)
8161 /* Allow any built-in function; if the expansion
8162 isn't constant, we'll deal with that then. */
8163 && !fndecl_built_in_p (fun
)
8164 /* In C++20, replaceable global allocation functions
8165 are constant expressions. */
8166 && (!cxx_replaceable_global_alloc_fn (fun
)
8167 || TREE_CODE (t
) != CALL_EXPR
8168 || (!CALL_FROM_NEW_OR_DELETE_P (t
)
8169 && (current_function_decl
== NULL_TREE
8170 || !is_std_allocator_allocate
8171 (current_function_decl
))))
8172 /* Allow placement new in std::construct_at. */
8173 && (!cxx_placement_new_fn (fun
)
8174 || TREE_CODE (t
) != CALL_EXPR
8175 || current_function_decl
== NULL_TREE
8176 || !is_std_construct_at (current_function_decl
))
8177 && !cxx_dynamic_cast_fn_p (fun
))
8179 if (flags
& tf_error
)
8181 error_at (loc
, "call to non-%<constexpr%> function %qD",
8183 explain_invalid_constexpr_fn (fun
);
8187 /* A call to a non-static member function takes the address
8188 of the object as the first argument. But in a constant
8189 expression the address will be folded away, so look
8191 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun
)
8192 && !DECL_CONSTRUCTOR_P (fun
))
8194 tree x
= get_nth_callarg (t
, 0);
8195 if (is_this_parameter (x
))
8197 /* Don't require an immediately constant value, as
8198 constexpr substitution might not use the value. */
8199 bool sub_now
= false;
8200 if (!potential_constant_expression_1 (x
, rval
, strict
,
8209 if (!RECUR (fun
, true))
8211 fun
= get_first_fn (fun
);
8213 /* Skip initial arguments to base constructors. */
8214 if (DECL_BASE_CONSTRUCTOR_P (fun
))
8215 i
= num_artificial_parms_for (fun
);
8216 fun
= DECL_ORIGIN (fun
);
8220 if (RECUR (fun
, rval
))
8221 /* Might end up being a constant function pointer. */;
8225 for (; i
< nargs
; ++i
)
8227 tree x
= get_nth_callarg (t
, i
);
8228 /* In a template, reference arguments haven't been converted to
8229 REFERENCE_TYPE and we might not even know if the parameter
8230 is a reference, so accept lvalue constants too. */
8231 bool rv
= processing_template_decl
? any
: rval
;
8232 /* Don't require an immediately constant value, as constexpr
8233 substitution might not use the value of the argument. */
8234 bool sub_now
= false;
8235 if (!potential_constant_expression_1 (x
, rv
, strict
,
8236 sub_now
, flags
, jump_target
))
8242 case NON_LVALUE_EXPR
:
8243 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
8244 -- an lvalue of integral type that refers to a non-volatile
8245 const variable or static data member initialized with
8246 constant expressions, or
8248 -- an lvalue of literal type that refers to non-volatile
8249 object defined with constexpr, or that refers to a
8250 sub-object of such an object; */
8251 return RECUR (TREE_OPERAND (t
, 0), rval
);
8254 if (DECL_HAS_VALUE_EXPR_P (t
))
8256 if (now
&& is_normal_capture_proxy (t
))
8258 /* -- in a lambda-expression, a reference to this or to a
8259 variable with automatic storage duration defined outside that
8260 lambda-expression, where the reference would be an
8264 /* Since we're doing an lvalue-rvalue conversion, this might
8265 not be an odr-use, so evaluate the variable directly. */
8266 return RECUR (DECL_CAPTURED_VARIABLE (t
), rval
);
8268 if (flags
& tf_error
)
8270 tree cap
= DECL_CAPTURED_VARIABLE (t
);
8271 error ("lambda capture of %qE is not a constant expression",
8273 if (decl_constant_var_p (cap
))
8274 inform (input_location
, "because it is used as a glvalue");
8278 /* Treat __PRETTY_FUNCTION__ inside a template function as
8279 potentially-constant. */
8280 else if (DECL_PRETTY_FUNCTION_P (t
)
8281 && DECL_VALUE_EXPR (t
) == error_mark_node
)
8283 return RECUR (DECL_VALUE_EXPR (t
), rval
);
8286 && !var_in_maybe_constexpr_fn (t
)
8287 && !type_dependent_expression_p (t
)
8288 && !decl_maybe_constant_var_p (t
)
8290 || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t
))
8291 || (DECL_INITIAL (t
)
8292 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t
)))
8293 && COMPLETE_TYPE_P (TREE_TYPE (t
))
8294 && !is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
8296 if (flags
& tf_error
)
8297 non_const_var_error (loc
, t
);
8303 if (REINTERPRET_CAST_P (t
))
8305 if (flags
& tf_error
)
8306 error_at (loc
, "%<reinterpret_cast%> is not a constant expression");
8311 case VIEW_CONVERT_EXPR
:
8312 /* -- a reinterpret_cast. FIXME not implemented, and this rule
8313 may change to something more specific to type-punning (DR 1312). */
8315 tree from
= TREE_OPERAND (t
, 0);
8316 if (location_wrapper_p (t
))
8317 return (RECUR (from
, want_rval
));
8318 if (INDIRECT_TYPE_P (TREE_TYPE (t
)))
8320 STRIP_ANY_LOCATION_WRAPPER (from
);
8321 if (TREE_CODE (from
) == INTEGER_CST
8322 && !integer_zerop (from
))
8324 if (flags
& tf_error
)
8326 "%<reinterpret_cast%> from integer to pointer");
8330 return (RECUR (from
, TREE_CODE (t
) != VIEW_CONVERT_EXPR
));
8333 case ADDRESSOF_EXPR
:
8334 /* This is like ADDR_EXPR, except it won't form pointer-to-member. */
8335 t
= TREE_OPERAND (t
, 0);
8336 goto handle_addr_expr
;
8339 /* -- a unary operator & that is applied to an lvalue that
8340 designates an object with thread or automatic storage
8342 t
= TREE_OPERAND (t
, 0);
8344 if (TREE_CODE (t
) == OFFSET_REF
&& PTRMEM_OK_P (t
))
8345 /* A pointer-to-member constant. */
8350 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
8351 any checking here, as we might dereference the pointer later. If
8352 we remove this code, also remove check_automatic_or_tls. */
8353 i
= check_automatic_or_tls (t
);
8358 if (flags
& tf_error
)
8359 error ("address-of an object %qE with thread local or "
8360 "automatic storage is not a constant expression", t
);
8364 return RECUR (t
, any
);
8369 /* -- a class member access unless its postfix-expression is
8370 of literal type or of pointer to literal type. */
8371 /* This test would be redundant, as it follows from the
8372 postfix-expression being a potential constant expression. */
8373 if (type_unknown_p (t
))
8375 if (is_overloaded_fn (t
))
8376 /* In a template, a COMPONENT_REF of a function expresses ob.fn(),
8377 which uses ob as an lvalue. */
8384 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
8386 case EXPR_PACK_EXPANSION
:
8387 return RECUR (PACK_EXPANSION_PATTERN (t
), want_rval
);
8391 tree x
= TREE_OPERAND (t
, 0);
8393 if (is_this_parameter (x
) && !is_capture_proxy (x
))
8395 if (!var_in_maybe_constexpr_fn (x
))
8397 if (flags
& tf_error
)
8398 error_at (loc
, "use of %<this%> in a constant expression");
8403 return RECUR (x
, rval
);
8406 case STATEMENT_LIST
:
8407 for (tree stmt
: tsi_range (t
))
8408 if (!RECUR (stmt
, any
))
8413 if (cxx_dialect
< cxx14
)
8415 if (!RECUR (TREE_OPERAND (t
, 0), any
))
8417 /* Just ignore clobbers. */
8418 if (TREE_CLOBBER_P (TREE_OPERAND (t
, 1)))
8420 if (!RECUR (TREE_OPERAND (t
, 1), rval
))
8425 if (cxx_dialect
< cxx14
)
8427 if (!RECUR (TREE_OPERAND (t
, 0), rval
))
8429 if (!RECUR (TREE_OPERAND (t
, 2), rval
))
8434 if (!RECUR (DO_COND (t
), rval
))
8436 if (!RECUR (DO_BODY (t
), any
))
8438 if (breaks (jump_target
) || continues (jump_target
))
8439 *jump_target
= NULL_TREE
;
8443 if (!RECUR (FOR_INIT_STMT (t
), any
))
8446 if (!RECUR (tmp
, rval
))
8450 if (!processing_template_decl
)
8451 tmp
= cxx_eval_outermost_constant_expr (tmp
, true);
8452 /* If we couldn't evaluate the condition, it might not ever be
8454 if (!integer_onep (tmp
))
8456 /* Before returning true, check if the for body can contain
8458 hash_set
<tree
> pset
;
8459 check_for_return_continue_data data
= { &pset
, NULL_TREE
,
8462 = cp_walk_tree (&FOR_BODY (t
), check_for_return_continue
,
8464 *jump_target
= ret_expr
;
8468 if (!RECUR (FOR_EXPR (t
), any
))
8470 if (!RECUR (FOR_BODY (t
), any
))
8472 if (breaks (jump_target
) || continues (jump_target
))
8473 *jump_target
= NULL_TREE
;
8476 case RANGE_FOR_STMT
:
8477 if (!RECUR (RANGE_FOR_INIT_STMT (t
), any
))
8479 if (!RECUR (RANGE_FOR_EXPR (t
), any
))
8481 if (!RECUR (RANGE_FOR_BODY (t
), any
))
8483 if (breaks (jump_target
) || continues (jump_target
))
8484 *jump_target
= NULL_TREE
;
8488 tmp
= WHILE_COND (t
);
8489 if (!RECUR (tmp
, rval
))
8491 if (!processing_template_decl
)
8492 tmp
= cxx_eval_outermost_constant_expr (tmp
, true);
8493 /* If we couldn't evaluate the condition, it might not ever be true. */
8494 if (!integer_onep (tmp
))
8496 /* Before returning true, check if the while body can contain
8498 hash_set
<tree
> pset
;
8499 check_for_return_continue_data data
= { &pset
, NULL_TREE
,
8502 = cp_walk_tree (&WHILE_BODY (t
), check_for_return_continue
,
8504 *jump_target
= ret_expr
;
8507 if (!RECUR (WHILE_BODY (t
), any
))
8509 if (breaks (jump_target
) || continues (jump_target
))
8510 *jump_target
= NULL_TREE
;
8514 if (!RECUR (SWITCH_STMT_COND (t
), rval
))
8516 /* FIXME we don't check SWITCH_STMT_BODY currently, because even
8517 unreachable labels would be checked and it is enough if there is
8518 a single switch cond value for which it is a valid constant
8519 expression. We need to check if there are any RETURN_EXPRs
8520 or CONTINUE_STMTs inside of the body though, as in that case
8521 we need to set *jump_target. */
8524 hash_set
<tree
> pset
;
8525 check_for_return_continue_data data
= { &pset
, NULL_TREE
,
8528 = cp_walk_tree (&SWITCH_STMT_BODY (t
), check_for_return_continue
,
8530 /* The switch might return. */
8531 *jump_target
= ret_expr
;
8532 else if (data
.continue_stmt
)
8533 /* The switch can't return, but might continue. */
8534 *jump_target
= data
.continue_stmt
;
8539 return RECUR (STMT_EXPR_STMT (t
), rval
);
8542 if (cxx_dialect
>= cxx17
)
8543 /* In C++17 lambdas can be constexpr, don't give up yet. */
8545 else if (flags
& tf_error
)
8546 error_at (loc
, "lambda-expression is not a constant expression "
8550 case DYNAMIC_CAST_EXPR
:
8551 case PSEUDO_DTOR_EXPR
:
8555 case VEC_DELETE_EXPR
:
8561 case OMP_DISTRIBUTE
:
8565 case OMP_TARGET_DATA
:
8574 case OMP_TARGET_UPDATE
:
8575 case OMP_TARGET_ENTER_DATA
:
8576 case OMP_TARGET_EXIT_DATA
:
8578 case OMP_ATOMIC_READ
:
8579 case OMP_ATOMIC_CAPTURE_OLD
:
8580 case OMP_ATOMIC_CAPTURE_NEW
:
8586 case OACC_HOST_DATA
:
8590 case OACC_ENTER_DATA
:
8591 case OACC_EXIT_DATA
:
8593 /* GCC internal stuff. */
8595 case TRANSACTION_EXPR
:
8596 case AT_ENCODE_EXPR
:
8598 if (flags
& tf_error
)
8599 error_at (loc
, "expression %qE is not a constant expression", t
);
8603 if (flags
& tf_error
)
8604 inline_asm_in_constexpr_error (loc
);
8608 if (cxx_dialect
>= cxx20
)
8609 /* In C++20 virtual calls can be constexpr, don't give up yet. */
8611 else if (flags
& tf_error
)
8613 "virtual functions cannot be %<constexpr%> before C++20");
8617 /* In C++20, a typeid expression whose operand is of polymorphic
8618 class type can be constexpr. */
8620 tree e
= TREE_OPERAND (t
, 0);
8621 if (cxx_dialect
< cxx20
8624 && !type_dependent_expression_p (e
)
8625 && TYPE_POLYMORPHIC_P (TREE_TYPE (e
)))
8627 if (flags
& tf_error
)
8628 error_at (loc
, "%<typeid%> is not a constant expression "
8629 "because %qE is of polymorphic type", e
);
8635 case POINTER_DIFF_EXPR
:
8646 case SPACESHIP_EXPR
:
8650 case PREINCREMENT_EXPR
:
8651 case POSTINCREMENT_EXPR
:
8652 case PREDECREMENT_EXPR
:
8653 case POSTDECREMENT_EXPR
:
8654 if (cxx_dialect
< cxx14
)
8660 if (TYPE_P (TREE_OPERAND (t
, 0)))
8666 case FIX_TRUNC_EXPR
:
8671 case TRUTH_NOT_EXPR
:
8672 case FIXED_CONVERT_EXPR
:
8673 case UNARY_PLUS_EXPR
:
8674 case UNARY_LEFT_FOLD_EXPR
:
8675 case UNARY_RIGHT_FOLD_EXPR
:
8677 return RECUR (TREE_OPERAND (t
, 0), rval
);
8680 case CONST_CAST_EXPR
:
8681 case STATIC_CAST_EXPR
:
8682 case REINTERPRET_CAST_EXPR
:
8683 case IMPLICIT_CONV_EXPR
:
8684 if (cxx_dialect
< cxx11
8685 && !dependent_type_p (TREE_TYPE (t
))
8686 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
)))
8687 /* In C++98, a conversion to non-integral type can't be part of a
8688 constant expression. */
8690 if (flags
& tf_error
)
8692 "cast to non-integral type %qT in a constant expression",
8696 /* This might be a conversion from a class to a (potentially) literal
8697 type. Let's consider it potentially constant since the conversion
8698 might be a constexpr user-defined conversion. */
8699 else if (cxx_dialect
>= cxx11
8700 && (dependent_type_p (TREE_TYPE (t
))
8701 || !COMPLETE_TYPE_P (TREE_TYPE (t
))
8702 || literal_type_p (TREE_TYPE (t
)))
8703 && TREE_OPERAND (t
, 0))
8705 tree type
= TREE_TYPE (TREE_OPERAND (t
, 0));
8706 /* If this is a dependent type, it could end up being a class
8707 with conversions. */
8708 if (type
== NULL_TREE
|| WILDCARD_TYPE_P (type
))
8710 /* Or a non-dependent class which has conversions. */
8711 else if (CLASS_TYPE_P (type
)
8712 && (TYPE_HAS_CONVERSION (type
) || dependent_scope_p (type
)))
8716 return (RECUR (TREE_OPERAND (t
, 0),
8717 !TYPE_REF_P (TREE_TYPE (t
))));
8720 return RECUR (BIND_EXPR_BODY (t
), want_rval
);
8722 case CLEANUP_POINT_EXPR
:
8723 case MUST_NOT_THROW_EXPR
:
8724 case TRY_CATCH_EXPR
:
8729 case NON_DEPENDENT_EXPR
:
8730 /* For convenience. */
8733 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
8736 tmp
= DECL_EXPR_DECL (t
);
8737 if (VAR_P (tmp
) && !DECL_ARTIFICIAL (tmp
))
8739 if (TREE_STATIC (tmp
))
8741 if (flags
& tf_error
)
8742 error_at (DECL_SOURCE_LOCATION (tmp
), "%qD declared "
8743 "%<static%> in %<constexpr%> context", tmp
);
8746 else if (CP_DECL_THREAD_LOCAL_P (tmp
))
8748 if (flags
& tf_error
)
8749 error_at (DECL_SOURCE_LOCATION (tmp
), "%qD declared "
8750 "%<thread_local%> in %<constexpr%> context", tmp
);
8753 else if (!check_for_uninitialized_const_var
8754 (tmp
, /*constexpr_context_p=*/true, flags
))
8757 return RECUR (tmp
, want_rval
);
8759 case TRY_FINALLY_EXPR
:
8760 return (RECUR (TREE_OPERAND (t
, 0), want_rval
)
8761 && RECUR (TREE_OPERAND (t
, 1), any
));
8764 return RECUR (TREE_OPERAND (t
, 1), want_rval
);
8767 if (!TARGET_EXPR_DIRECT_INIT_P (t
)
8768 && !literal_type_p (TREE_TYPE (t
)))
8770 if (flags
& tf_error
)
8772 auto_diagnostic_group d
;
8773 error_at (loc
, "temporary of non-literal type %qT in a "
8774 "constant expression", TREE_TYPE (t
));
8775 explain_non_literal_class (TREE_TYPE (t
));
8781 return RECUR (TREE_OPERAND (t
, 1), rval
);
8785 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (t
);
8786 constructor_elt
*ce
;
8787 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
8788 if (!RECUR (ce
->value
, want_rval
))
8795 gcc_assert (TREE_PURPOSE (t
) == NULL_TREE
8796 || DECL_P (TREE_PURPOSE (t
)));
8797 if (!RECUR (TREE_VALUE (t
), want_rval
))
8799 if (TREE_CHAIN (t
) == NULL_TREE
)
8801 return RECUR (TREE_CHAIN (t
), want_rval
);
8804 case TRUNC_DIV_EXPR
:
8806 case FLOOR_DIV_EXPR
:
8807 case ROUND_DIV_EXPR
:
8808 case TRUNC_MOD_EXPR
:
8810 case ROUND_MOD_EXPR
:
8812 tree denom
= TREE_OPERAND (t
, 1);
8813 if (!RECUR (denom
, rval
))
8815 /* We can't call cxx_eval_outermost_constant_expr on an expression
8816 that hasn't been through instantiate_non_dependent_expr yet. */
8817 if (!processing_template_decl
)
8818 denom
= cxx_eval_outermost_constant_expr (denom
, true);
8819 if (integer_zerop (denom
))
8821 if (flags
& tf_error
)
8822 error ("division by zero is not a constant expression");
8828 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
8834 /* check_return_expr sometimes wraps a TARGET_EXPR in a
8835 COMPOUND_EXPR; don't get confused. */
8836 tree op0
= TREE_OPERAND (t
, 0);
8837 tree op1
= TREE_OPERAND (t
, 1);
8839 if (TREE_CODE (op0
) == TARGET_EXPR
&& op1
== TARGET_EXPR_SLOT (op0
))
8840 return RECUR (op0
, want_rval
);
8845 /* If the first operand is the non-short-circuit constant, look at
8846 the second operand; otherwise we only care about the first one for
8848 case TRUTH_AND_EXPR
:
8849 case TRUTH_ANDIF_EXPR
:
8850 tmp
= boolean_true_node
;
8853 case TRUTH_ORIF_EXPR
:
8854 tmp
= boolean_false_node
;
8857 tree op
= TREE_OPERAND (t
, 0);
8858 if (!RECUR (op
, rval
))
8860 if (!processing_template_decl
)
8861 op
= cxx_eval_outermost_constant_expr (op
, true);
8862 if (tree_int_cst_equal (op
, tmp
))
8863 return RECUR (TREE_OPERAND (t
, 1), rval
);
8870 case POINTER_PLUS_EXPR
:
8872 case EXACT_DIV_EXPR
:
8882 case TRUTH_XOR_EXPR
:
8883 case UNORDERED_EXPR
:
8896 case ARRAY_RANGE_REF
:
8900 case BINARY_LEFT_FOLD_EXPR
:
8901 case BINARY_RIGHT_FOLD_EXPR
:
8903 for (i
= 0; i
< 2; ++i
)
8904 if (!RECUR (TREE_OPERAND (t
, i
), want_rval
))
8909 for (i
= 0; i
< 3; ++i
)
8910 if (!RECUR (TREE_OPERAND (t
, i
), true))
8915 if (COND_EXPR_IS_VEC_DELETE (t
) && cxx_dialect
< cxx20
)
8917 if (flags
& tf_error
)
8918 error_at (loc
, "%<delete[]%> is not a constant expression");
8924 /* If the condition is a known constant, we know which of the legs we
8925 care about; otherwise we only require that the condition and
8926 either of the legs be potentially constant. */
8927 tmp
= TREE_OPERAND (t
, 0);
8928 if (!RECUR (tmp
, rval
))
8930 if (!processing_template_decl
)
8931 tmp
= cxx_eval_outermost_constant_expr (tmp
, true);
8932 /* potential_constant_expression* isn't told if it is called for
8933 manifestly_const_eval or not, so for consteval if always
8934 process both branches as if the condition is not a known
8936 if (TREE_CODE (t
) != IF_STMT
|| !IF_STMT_CONSTEVAL_P (t
))
8938 if (integer_zerop (tmp
))
8939 return RECUR (TREE_OPERAND (t
, 2), want_rval
);
8940 else if (TREE_CODE (tmp
) == INTEGER_CST
)
8941 return RECUR (TREE_OPERAND (t
, 1), want_rval
);
8944 for (i
= 1; i
< 3; ++i
)
8946 tree this_jump_target
= tmp
;
8947 if (potential_constant_expression_1 (TREE_OPERAND (t
, i
),
8948 want_rval
, strict
, now
,
8949 tf_none
, &this_jump_target
))
8951 if (returns (&this_jump_target
))
8952 *jump_target
= this_jump_target
;
8953 else if (!returns (jump_target
))
8955 if (breaks (&this_jump_target
)
8956 || continues (&this_jump_target
))
8957 *jump_target
= this_jump_target
;
8960 /* If the then branch is potentially constant, but
8961 does not return, check if the else branch
8962 couldn't return, break or continue. */
8963 hash_set
<tree
> pset
;
8964 check_for_return_continue_data data
= { &pset
, NULL_TREE
,
8967 = cp_walk_tree (&TREE_OPERAND (t
, 2),
8968 check_for_return_continue
, &data
,
8970 *jump_target
= ret_expr
;
8971 else if (*jump_target
== NULL_TREE
)
8973 if (data
.continue_stmt
)
8974 *jump_target
= data
.continue_stmt
;
8975 else if (data
.break_stmt
)
8976 *jump_target
= data
.break_stmt
;
8983 if (flags
& tf_error
)
8984 error_at (loc
, "expression %qE is not a constant expression", t
);
8988 if (VEC_INIT_EXPR_IS_CONSTEXPR (t
))
8990 if (flags
& tf_error
)
8992 error_at (loc
, "non-constant array initialization");
8993 diagnose_non_constexpr_vec_init (t
);
8999 /* We can see these in statement-expressions. */
9003 if (!RECUR (CLEANUP_BODY (t
), any
))
9005 if (!CLEANUP_EH_ONLY (t
) && !RECUR (CLEANUP_EXPR (t
), any
))
9009 case EMPTY_CLASS_EXPR
:
9014 tree
*target
= &TREE_OPERAND (t
, 0);
9015 /* Gotos representing break and continue are OK. */
9016 if (breaks (target
) || continues (target
))
9018 *jump_target
= *target
;
9021 if (flags
& tf_error
)
9022 error_at (loc
, "%<goto%> is not a constant expression");
9027 t
= LABEL_EXPR_LABEL (t
);
9028 if (DECL_ARTIFICIAL (t
))
9030 else if (flags
& tf_error
)
9031 error_at (loc
, "label definition is not a constant expression");
9035 return RECUR (TREE_OPERAND (t
, 0), rval
);
9038 return RECUR (TREE_OPERAND (t
, 0), rval
);
9040 /* Coroutine await, yield and return expressions are not. */
9043 case CO_RETURN_EXPR
:
9047 if (objc_non_constant_expr_p (t
))
9050 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t
)));
9058 potential_constant_expression_1 (tree t
, bool want_rval
, bool strict
, bool now
,
9059 tsubst_flags_t flags
)
9061 tree target
= NULL_TREE
;
9062 return potential_constant_expression_1 (t
, want_rval
, strict
, now
,
9066 /* The main entry point to the above. */
9069 potential_constant_expression (tree t
)
9071 return potential_constant_expression_1 (t
, false, true, false, tf_none
);
9074 /* As above, but require a constant rvalue. */
9077 potential_rvalue_constant_expression (tree t
)
9079 return potential_constant_expression_1 (t
, true, true, false, tf_none
);
9082 /* Like above, but complain about non-constant expressions. */
9085 require_potential_constant_expression (tree t
)
9087 return potential_constant_expression_1 (t
, false, true, false,
9088 tf_warning_or_error
);
9091 /* Cross product of the above. */
9094 require_potential_rvalue_constant_expression (tree t
)
9096 return potential_constant_expression_1 (t
, true, true, false,
9097 tf_warning_or_error
);
9100 /* Like above, but don't consider PARM_DECL a potential_constant_expression. */
9103 require_rvalue_constant_expression (tree t
)
9105 return potential_constant_expression_1 (t
, true, true, true,
9106 tf_warning_or_error
);
9109 /* Like potential_constant_expression, but don't consider possible constexpr
9110 substitution of the current function. That is, PARM_DECL qualifies under
9111 potential_constant_expression, but not here.
9113 This is basically what you can check when any actual constant values might
9114 be value-dependent. */
9117 is_constant_expression (tree t
)
9119 return potential_constant_expression_1 (t
, false, true, true, tf_none
);
9122 /* As above, but expect an rvalue. */
9125 is_rvalue_constant_expression (tree t
)
9127 return potential_constant_expression_1 (t
, true, true, true, tf_none
);
9130 /* Like above, but complain about non-constant expressions. */
9133 require_constant_expression (tree t
)
9135 return potential_constant_expression_1 (t
, false, true, true,
9136 tf_warning_or_error
);
9139 /* Like is_constant_expression, but allow const variables that are not allowed
9140 under constexpr rules. */
9143 is_static_init_expression (tree t
)
9145 return potential_constant_expression_1 (t
, false, false, true, tf_none
);
9148 /* Returns true if T is a potential constant expression that is not
9149 instantiation-dependent, and therefore a candidate for constant folding even
9153 is_nondependent_constant_expression (tree t
)
9155 return (!type_unknown_p (t
)
9156 && is_constant_expression (t
)
9157 && !instantiation_dependent_expression_p (t
));
9160 /* Returns true if T is a potential static initializer expression that is not
9161 instantiation-dependent. */
9164 is_nondependent_static_init_expression (tree t
)
9166 return (!type_unknown_p (t
)
9167 && is_static_init_expression (t
)
9168 && !instantiation_dependent_expression_p (t
));
9171 /* Finalize constexpr processing after parsing. */
9174 fini_constexpr (void)
9176 /* The contexpr call and fundef copies tables are no longer needed. */
9177 constexpr_call_table
= NULL
;
9178 fundef_copies_table
= NULL
;
9181 #include "gt-cp-constexpr.h"