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-2020 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"
39 static bool verify_constant (tree
, bool, bool *, bool *);
40 #define VERIFY_CONSTANT(X) \
42 if (verify_constant ((X), ctx->quiet, non_constant_p, overflow_p)) \
46 static HOST_WIDE_INT
find_array_ctor_elt (tree ary
, tree dindex
,
49 /* Returns true iff FUN is an instantiation of a constexpr function
50 template or a defaulted constexpr function. */
53 is_instantiation_of_constexpr (tree fun
)
55 return ((DECL_TEMPLOID_INSTANTIATION (fun
)
56 && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun
)))
57 || (DECL_DEFAULTED_FN (fun
)
58 && DECL_DECLARED_CONSTEXPR_P (fun
)));
61 /* Return true if T is a literal type. */
64 literal_type_p (tree t
)
69 || (VOID_TYPE_P (t
) && cxx_dialect
>= cxx14
))
73 t
= complete_type (t
);
74 gcc_assert (COMPLETE_TYPE_P (t
) || errorcount
);
75 return CLASSTYPE_LITERAL_P (t
);
77 if (TREE_CODE (t
) == ARRAY_TYPE
)
78 return literal_type_p (strip_array_types (t
));
82 /* If DECL is a variable declared `constexpr', require its type
83 be literal. Return error_mark_node if we give an error, the
87 ensure_literal_type_for_constexpr_object (tree decl
)
89 tree type
= TREE_TYPE (decl
);
91 && (DECL_DECLARED_CONSTEXPR_P (decl
)
92 || var_in_constexpr_fn (decl
))
93 && !processing_template_decl
)
95 tree stype
= strip_array_types (type
);
96 if (CLASS_TYPE_P (stype
) && !COMPLETE_TYPE_P (complete_type (stype
)))
97 /* Don't complain here, we'll complain about incompleteness
98 when we try to initialize the variable. */;
99 else if (!literal_type_p (type
))
101 if (DECL_DECLARED_CONSTEXPR_P (decl
))
103 auto_diagnostic_group d
;
104 error_at (DECL_SOURCE_LOCATION (decl
),
105 "the type %qT of %<constexpr%> variable %qD "
106 "is not literal", type
, decl
);
107 explain_non_literal_class (type
);
108 decl
= error_mark_node
;
112 if (!is_instantiation_of_constexpr (current_function_decl
))
114 auto_diagnostic_group d
;
115 error_at (DECL_SOURCE_LOCATION (decl
),
116 "variable %qD of non-literal type %qT in "
117 "%<constexpr%> function", decl
, type
);
118 explain_non_literal_class (type
);
119 decl
= error_mark_node
;
121 cp_function_chain
->invalid_constexpr
= true;
124 else if (DECL_DECLARED_CONSTEXPR_P (decl
)
125 && variably_modified_type_p (type
, NULL_TREE
))
127 error_at (DECL_SOURCE_LOCATION (decl
),
128 "%<constexpr%> variable %qD has variably-modified "
129 "type %qT", decl
, type
);
130 decl
= error_mark_node
;
136 /* Representation of entries in the constexpr function definition table. */
138 struct GTY((for_user
)) constexpr_fundef
{
145 struct constexpr_fundef_hasher
: ggc_ptr_hash
<constexpr_fundef
>
147 static hashval_t
hash (constexpr_fundef
*);
148 static bool equal (constexpr_fundef
*, constexpr_fundef
*);
151 /* This table holds all constexpr function definitions seen in
152 the current translation unit. */
154 static GTY (()) hash_table
<constexpr_fundef_hasher
> *constexpr_fundef_table
;
156 /* Utility function used for managing the constexpr function table.
157 Return true if the entries pointed to by P and Q are for the
158 same constexpr function. */
161 constexpr_fundef_hasher::equal (constexpr_fundef
*lhs
, constexpr_fundef
*rhs
)
163 return lhs
->decl
== rhs
->decl
;
166 /* Utility function used for managing the constexpr function table.
167 Return a hash value for the entry pointed to by Q. */
170 constexpr_fundef_hasher::hash (constexpr_fundef
*fundef
)
172 return DECL_UID (fundef
->decl
);
175 /* Return a previously saved definition of function FUN. */
177 static constexpr_fundef
*
178 retrieve_constexpr_fundef (tree fun
)
180 if (constexpr_fundef_table
== NULL
)
183 constexpr_fundef fundef
= { fun
, NULL
, NULL
, NULL
};
184 return constexpr_fundef_table
->find (&fundef
);
187 /* Check whether the parameter and return types of FUN are valid for a
188 constexpr function, and complain if COMPLAIN. */
191 is_valid_constexpr_fn (tree fun
, bool complain
)
195 if (DECL_INHERITED_CTOR (fun
)
196 && TREE_CODE (fun
) == TEMPLATE_DECL
)
200 error ("inherited constructor %qD is not %<constexpr%>",
201 DECL_INHERITED_CTOR (fun
));
205 for (tree parm
= FUNCTION_FIRST_USER_PARM (fun
);
206 parm
!= NULL_TREE
; parm
= TREE_CHAIN (parm
))
207 if (!literal_type_p (TREE_TYPE (parm
)))
212 auto_diagnostic_group d
;
213 error ("invalid type for parameter %d of %<constexpr%> "
214 "function %q+#D", DECL_PARM_INDEX (parm
), fun
);
215 explain_non_literal_class (TREE_TYPE (parm
));
220 if (LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun
)) && cxx_dialect
< cxx17
)
224 inform (DECL_SOURCE_LOCATION (fun
),
225 "lambdas are implicitly %<constexpr%> only in C++17 and later");
227 else if (!DECL_CONSTRUCTOR_P (fun
))
229 tree rettype
= TREE_TYPE (TREE_TYPE (fun
));
230 if (!literal_type_p (rettype
))
235 auto_diagnostic_group d
;
236 error ("invalid return type %qT of %<constexpr%> function %q+D",
238 explain_non_literal_class (rettype
);
242 /* C++14 DR 1684 removed this restriction. */
243 if (cxx_dialect
< cxx14
244 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fun
)
245 && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun
)))
250 auto_diagnostic_group d
;
251 if (pedwarn (DECL_SOURCE_LOCATION (fun
), OPT_Wpedantic
,
252 "enclosing class of %<constexpr%> non-static"
253 " member function %q+#D is not a literal type",
255 explain_non_literal_class (DECL_CONTEXT (fun
));
259 else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun
)))
263 error ("%q#T has virtual base classes", DECL_CONTEXT (fun
));
269 /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF
270 for a member of an anonymous aggregate, INIT is the initializer for that
271 member, and VEC_OUTER is the vector of constructor elements for the class
272 whose constructor we are processing. Add the initializer to the vector
273 and return true to indicate success. */
276 build_anon_member_initialization (tree member
, tree init
,
277 vec
<constructor_elt
, va_gc
> **vec_outer
)
279 /* MEMBER presents the relevant fields from the inside out, but we need
280 to build up the initializer from the outside in so that we can reuse
281 previously built CONSTRUCTORs if this is, say, the second field in an
282 anonymous struct. So we use a vec as a stack. */
283 auto_vec
<tree
, 2> fields
;
286 fields
.safe_push (TREE_OPERAND (member
, 1));
287 member
= TREE_OPERAND (member
, 0);
289 while (ANON_AGGR_TYPE_P (TREE_TYPE (member
))
290 && TREE_CODE (member
) == COMPONENT_REF
);
292 /* VEC has the constructor elements vector for the context of FIELD.
293 If FIELD is an anonymous aggregate, we will push inside it. */
294 vec
<constructor_elt
, va_gc
> **vec
= vec_outer
;
296 while (field
= fields
.pop(),
297 ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
300 /* If there is already an outer constructor entry for the anonymous
301 aggregate FIELD, use it; otherwise, insert one. */
302 if (vec_safe_is_empty (*vec
)
303 || (*vec
)->last().index
!= field
)
305 ctor
= build_constructor (TREE_TYPE (field
), NULL
);
306 CONSTRUCTOR_APPEND_ELT (*vec
, field
, ctor
);
309 ctor
= (*vec
)->last().value
;
310 vec
= &CONSTRUCTOR_ELTS (ctor
);
313 /* Now we're at the innermost field, the one that isn't an anonymous
314 aggregate. Add its initializer to the CONSTRUCTOR and we're done. */
315 gcc_assert (fields
.is_empty());
316 CONSTRUCTOR_APPEND_ELT (*vec
, field
, init
);
321 /* Subroutine of build_constexpr_constructor_member_initializers.
322 The expression tree T represents a data member initialization
323 in a (constexpr) constructor definition. Build a pairing of
324 the data member with its initializer, and prepend that pair
325 to the existing initialization pair INITS. */
328 build_data_member_initialization (tree t
, vec
<constructor_elt
, va_gc
> **vec
)
331 if (TREE_CODE (t
) == CLEANUP_POINT_EXPR
)
332 t
= TREE_OPERAND (t
, 0);
333 if (TREE_CODE (t
) == EXPR_STMT
)
334 t
= TREE_OPERAND (t
, 0);
335 if (t
== error_mark_node
)
337 if (TREE_CODE (t
) == STATEMENT_LIST
)
339 tree_stmt_iterator i
;
340 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
342 if (! build_data_member_initialization (tsi_stmt (i
), vec
))
347 if (TREE_CODE (t
) == CLEANUP_STMT
)
349 /* We can't see a CLEANUP_STMT in a constructor for a literal class,
350 but we can in a constexpr constructor for a non-literal class. Just
351 ignore it; either all the initialization will be constant, in which
352 case the cleanup can't run, or it can't be constexpr.
353 Still recurse into CLEANUP_BODY. */
354 return build_data_member_initialization (CLEANUP_BODY (t
), vec
);
356 if (TREE_CODE (t
) == CONVERT_EXPR
)
357 t
= TREE_OPERAND (t
, 0);
358 if (TREE_CODE (t
) == INIT_EXPR
359 /* vptr initialization shows up as a MODIFY_EXPR. In C++14 we only
360 use what this function builds for cx_check_missing_mem_inits, and
361 assignment in the ctor body doesn't count. */
362 || (cxx_dialect
< cxx14
&& TREE_CODE (t
) == MODIFY_EXPR
))
364 member
= TREE_OPERAND (t
, 0);
365 init
= break_out_target_exprs (TREE_OPERAND (t
, 1));
367 else if (TREE_CODE (t
) == CALL_EXPR
)
369 tree fn
= get_callee_fndecl (t
);
370 if (!fn
|| !DECL_CONSTRUCTOR_P (fn
))
371 /* We're only interested in calls to subobject constructors. */
373 member
= CALL_EXPR_ARG (t
, 0);
374 /* We don't use build_cplus_new here because it complains about
375 abstract bases. Leaving the call unwrapped means that it has the
376 wrong type, but cxx_eval_constant_expression doesn't care. */
377 init
= break_out_target_exprs (t
);
379 else if (TREE_CODE (t
) == BIND_EXPR
)
380 return build_data_member_initialization (BIND_EXPR_BODY (t
), vec
);
382 /* Don't add anything else to the CONSTRUCTOR. */
384 if (INDIRECT_REF_P (member
))
385 member
= TREE_OPERAND (member
, 0);
386 if (TREE_CODE (member
) == NOP_EXPR
)
390 if (TREE_CODE (op
) == ADDR_EXPR
)
392 gcc_assert (same_type_ignoring_top_level_qualifiers_p
393 (TREE_TYPE (TREE_TYPE (op
)),
394 TREE_TYPE (TREE_TYPE (member
))));
395 /* Initializing a cv-qualified member; we need to look through
399 else if (op
== current_class_ptr
400 && (same_type_ignoring_top_level_qualifiers_p
401 (TREE_TYPE (TREE_TYPE (member
)),
402 current_class_type
)))
403 /* Delegating constructor. */
407 /* This is an initializer for an empty base; keep it for now so
408 we can check it in cxx_eval_bare_aggregate. */
409 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member
))));
412 if (TREE_CODE (member
) == ADDR_EXPR
)
413 member
= TREE_OPERAND (member
, 0);
414 if (TREE_CODE (member
) == COMPONENT_REF
)
416 tree aggr
= TREE_OPERAND (member
, 0);
417 if (TREE_CODE (aggr
) == VAR_DECL
)
418 /* Initializing a local variable, don't add anything. */
420 if (TREE_CODE (aggr
) != COMPONENT_REF
)
421 /* Normal member initialization. */
422 member
= TREE_OPERAND (member
, 1);
423 else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr
)))
424 /* Initializing a member of an anonymous union. */
425 return build_anon_member_initialization (member
, init
, vec
);
427 /* We're initializing a vtable pointer in a base. Leave it as
428 COMPONENT_REF so we remember the path to get to the vfield. */
429 gcc_assert (TREE_TYPE (member
) == vtbl_ptr_type_node
);
432 /* Value-initialization can produce multiple initializers for the
433 same field; use the last one. */
434 if (!vec_safe_is_empty (*vec
) && (*vec
)->last().index
== member
)
435 (*vec
)->last().value
= init
;
437 CONSTRUCTOR_APPEND_ELT (*vec
, member
, init
);
441 /* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval.
442 In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a
443 BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */
446 check_constexpr_bind_expr_vars (tree t
)
448 gcc_assert (TREE_CODE (t
) == BIND_EXPR
);
450 for (tree var
= BIND_EXPR_VARS (t
); var
; var
= DECL_CHAIN (var
))
451 if (TREE_CODE (var
) == TYPE_DECL
452 && DECL_IMPLICIT_TYPEDEF_P (var
)
453 && !LAMBDA_TYPE_P (TREE_TYPE (var
)))
458 /* Subroutine of check_constexpr_ctor_body. */
461 check_constexpr_ctor_body_1 (tree last
, tree list
)
463 switch (TREE_CODE (list
))
466 if (TREE_CODE (DECL_EXPR_DECL (list
)) == USING_DECL
467 || TREE_CODE (DECL_EXPR_DECL (list
)) == TYPE_DECL
)
471 case CLEANUP_POINT_EXPR
:
472 return check_constexpr_ctor_body (last
, TREE_OPERAND (list
, 0),
476 if (!check_constexpr_bind_expr_vars (list
)
477 || !check_constexpr_ctor_body (last
, BIND_EXPR_BODY (list
),
484 case DEBUG_BEGIN_STMT
:
492 /* Make sure that there are no statements after LAST in the constructor
493 body represented by LIST. */
496 check_constexpr_ctor_body (tree last
, tree list
, bool complain
)
498 /* C++14 doesn't require a constexpr ctor to have an empty body. */
499 if (cxx_dialect
>= cxx14
)
503 if (TREE_CODE (list
) == STATEMENT_LIST
)
505 tree_stmt_iterator i
= tsi_last (list
);
506 for (; !tsi_end_p (i
); tsi_prev (&i
))
508 tree t
= tsi_stmt (i
);
511 if (!check_constexpr_ctor_body_1 (last
, t
))
518 else if (list
!= last
519 && !check_constexpr_ctor_body_1 (last
, list
))
524 error ("%<constexpr%> constructor does not have empty body");
525 DECL_DECLARED_CONSTEXPR_P (current_function_decl
) = false;
530 /* V is a vector of constructor elements built up for the base and member
531 initializers of a constructor for TYPE. They need to be in increasing
532 offset order, which they might not be yet if TYPE has a primary base
533 which is not first in the base-clause or a vptr and at least one base
534 all of which are non-primary. */
536 static vec
<constructor_elt
, va_gc
> *
537 sort_constexpr_mem_initializers (tree type
, vec
<constructor_elt
, va_gc
> *v
)
539 tree pri
= CLASSTYPE_PRIMARY_BINFO (type
);
545 field_type
= BINFO_TYPE (pri
);
546 else if (TYPE_CONTAINS_VPTR_P (type
))
547 field_type
= vtbl_ptr_type_node
;
551 /* Find the element for the primary base or vptr and move it to the
552 beginning of the vec. */
553 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
554 if (TREE_TYPE (ce
->index
) == field_type
)
557 if (i
> 0 && i
< vec_safe_length (v
))
559 vec
<constructor_elt
, va_gc
> &vref
= *v
;
560 constructor_elt elt
= vref
[i
];
569 /* Build compile-time evalable representations of member-initializer list
570 for a constexpr constructor. */
573 build_constexpr_constructor_member_initializers (tree type
, tree body
)
575 vec
<constructor_elt
, va_gc
> *vec
= NULL
;
578 switch (TREE_CODE (body
))
580 case MUST_NOT_THROW_EXPR
:
582 body
= TREE_OPERAND (body
, 0);
586 for (tree_stmt_iterator i
= tsi_start (body
);
587 !tsi_end_p (i
); tsi_next (&i
))
590 if (TREE_CODE (body
) == BIND_EXPR
)
596 body
= BIND_EXPR_BODY (body
);
603 if (TREE_CODE (body
) == TRY_BLOCK
)
605 body
= TREE_OPERAND (body
, 0);
606 if (TREE_CODE (body
) == BIND_EXPR
)
607 body
= BIND_EXPR_BODY (body
);
609 if (TREE_CODE (body
) == CLEANUP_POINT_EXPR
)
611 body
= TREE_OPERAND (body
, 0);
612 if (TREE_CODE (body
) == EXPR_STMT
)
613 body
= TREE_OPERAND (body
, 0);
614 if (TREE_CODE (body
) == INIT_EXPR
615 && (same_type_ignoring_top_level_qualifiers_p
616 (TREE_TYPE (TREE_OPERAND (body
, 0)),
617 current_class_type
)))
620 return TREE_OPERAND (body
, 1);
622 ok
= build_data_member_initialization (body
, &vec
);
624 else if (TREE_CODE (body
) == STATEMENT_LIST
)
626 tree_stmt_iterator i
;
627 for (i
= tsi_start (body
); !tsi_end_p (i
); tsi_next (&i
))
629 ok
= build_data_member_initialization (tsi_stmt (i
), &vec
);
634 else if (EXPR_P (body
))
635 ok
= build_data_member_initialization (body
, &vec
);
637 gcc_assert (errorcount
> 0);
640 if (vec_safe_length (vec
) > 0)
642 /* In a delegating constructor, return the target. */
643 constructor_elt
*ce
= &(*vec
)[0];
644 if (ce
->index
== current_class_ptr
)
651 vec
= sort_constexpr_mem_initializers (type
, vec
);
652 return build_constructor (type
, vec
);
655 return error_mark_node
;
658 /* We have an expression tree T that represents a call, either CALL_EXPR
659 or AGGR_INIT_EXPR. If the call is lexically to a named function,
660 retrun the _DECL for that function. */
663 get_function_named_in_call (tree t
)
665 tree fun
= cp_get_callee (t
);
666 if (fun
&& TREE_CODE (fun
) == ADDR_EXPR
667 && TREE_CODE (TREE_OPERAND (fun
, 0)) == FUNCTION_DECL
)
668 fun
= TREE_OPERAND (fun
, 0);
672 /* Subroutine of register_constexpr_fundef. BODY is the body of a function
673 declared to be constexpr, or a sub-statement thereof. Returns the
674 return value if suitable, error_mark_node for a statement not allowed in
675 a constexpr function, or NULL_TREE if no return value was found. */
678 constexpr_fn_retval (tree body
)
680 switch (TREE_CODE (body
))
684 tree_stmt_iterator i
;
685 tree expr
= NULL_TREE
;
686 for (i
= tsi_start (body
); !tsi_end_p (i
); tsi_next (&i
))
688 tree s
= constexpr_fn_retval (tsi_stmt (i
));
689 if (s
== error_mark_node
)
690 return error_mark_node
;
691 else if (s
== NULL_TREE
)
692 /* Keep iterating. */;
694 /* Multiple return statements. */
695 return error_mark_node
;
703 return break_out_target_exprs (TREE_OPERAND (body
, 0));
707 tree decl
= DECL_EXPR_DECL (body
);
708 if (TREE_CODE (decl
) == USING_DECL
709 /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */
710 || DECL_ARTIFICIAL (decl
))
712 return error_mark_node
;
715 case CLEANUP_POINT_EXPR
:
716 return constexpr_fn_retval (TREE_OPERAND (body
, 0));
719 if (!check_constexpr_bind_expr_vars (body
))
720 return error_mark_node
;
721 return constexpr_fn_retval (BIND_EXPR_BODY (body
));
724 case DEBUG_BEGIN_STMT
:
729 tree fun
= get_function_named_in_call (body
);
731 && fndecl_built_in_p (fun
, BUILT_IN_UNREACHABLE
))
737 return error_mark_node
;
741 /* Subroutine of register_constexpr_fundef. BODY is the DECL_SAVED_TREE of
742 FUN; do the necessary transformations to turn it into a single expression
743 that we can store in the hash table. */
746 massage_constexpr_body (tree fun
, tree body
)
748 if (DECL_CONSTRUCTOR_P (fun
))
749 body
= build_constexpr_constructor_member_initializers
750 (DECL_CONTEXT (fun
), body
);
751 else if (cxx_dialect
< cxx14
)
753 if (TREE_CODE (body
) == EH_SPEC_BLOCK
)
754 body
= EH_SPEC_STMTS (body
);
755 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
756 body
= TREE_OPERAND (body
, 0);
757 body
= constexpr_fn_retval (body
);
762 /* CTYPE is a type constructed from BODY. Return true if some
763 bases/fields are uninitialized, and complain if COMPLAIN. */
766 cx_check_missing_mem_inits (tree ctype
, tree body
, bool complain
)
768 /* We allow uninitialized bases/fields in C++20. */
769 if (cxx_dialect
>= cxx20
)
776 if (TREE_CODE (body
) != CONSTRUCTOR
)
778 nelts
= CONSTRUCTOR_NELTS (body
);
780 tree field
= TYPE_FIELDS (ctype
);
782 if (TREE_CODE (ctype
) == UNION_TYPE
)
784 if (nelts
== 0 && next_initializable_field (field
))
787 error ("%<constexpr%> constructor for union %qT must "
788 "initialize exactly one non-static data member", ctype
);
794 /* Iterate over the CONSTRUCTOR, checking any missing fields don't
795 need an explicit initialization. */
797 for (unsigned i
= 0; i
<= nelts
; ++i
)
799 tree index
= NULL_TREE
;
802 index
= CONSTRUCTOR_ELT (body
, i
)->index
;
803 /* Skip base and vtable inits. */
804 if (TREE_CODE (index
) != FIELD_DECL
805 || DECL_ARTIFICIAL (index
))
809 for (; field
!= index
; field
= DECL_CHAIN (field
))
812 if (TREE_CODE (field
) != FIELD_DECL
)
814 if (DECL_UNNAMED_BIT_FIELD (field
))
816 if (DECL_ARTIFICIAL (field
))
818 if (ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
820 /* Recurse to check the anonymous aggregate member. */
821 bad
|= cx_check_missing_mem_inits
822 (TREE_TYPE (field
), NULL_TREE
, complain
);
823 if (bad
&& !complain
)
827 ftype
= TREE_TYPE (field
);
828 if (!ftype
|| !TYPE_P (ftype
) || !COMPLETE_TYPE_P (ftype
))
829 /* A flexible array can't be intialized here, so don't complain
832 if (DECL_SIZE (field
) && integer_zerop (DECL_SIZE (field
)))
833 /* An empty field doesn't need an initializer. */
835 ftype
= strip_array_types (ftype
);
836 if (type_has_constexpr_default_constructor (ftype
))
838 /* It's OK to skip a member with a trivial constexpr ctor.
839 A constexpr ctor that isn't trivial should have been
841 gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype
)
847 auto_diagnostic_group d
;
848 error ("member %qD must be initialized by mem-initializer "
849 "in %<constexpr%> constructor", field
);
850 inform (DECL_SOURCE_LOCATION (field
), "declared here");
853 if (field
== NULL_TREE
)
856 if (ANON_AGGR_TYPE_P (TREE_TYPE (index
)))
858 /* Check the anonymous aggregate initializer is valid. */
859 bad
|= cx_check_missing_mem_inits
860 (TREE_TYPE (index
), CONSTRUCTOR_ELT (body
, i
)->value
, complain
);
861 if (bad
&& !complain
)
864 field
= DECL_CHAIN (field
);
870 /* We are processing the definition of the constexpr function FUN.
871 Check that its BODY fulfills the propriate requirements and
872 enter it in the constexpr function definition table.
873 For constructor BODY is actually the TREE_LIST of the
874 member-initializer list. */
877 register_constexpr_fundef (tree fun
, tree body
)
879 constexpr_fundef entry
;
880 constexpr_fundef
**slot
;
882 if (!is_valid_constexpr_fn (fun
, !DECL_GENERATED_P (fun
)))
885 tree massaged
= massage_constexpr_body (fun
, body
);
886 if (massaged
== NULL_TREE
|| massaged
== error_mark_node
)
888 if (!DECL_CONSTRUCTOR_P (fun
))
889 error ("body of %<constexpr%> function %qD not a return-statement",
894 bool potential
= potential_rvalue_constant_expression (massaged
);
895 if (!potential
&& !DECL_GENERATED_P (fun
))
896 require_potential_rvalue_constant_expression (massaged
);
898 if (DECL_CONSTRUCTOR_P (fun
)
899 && cx_check_missing_mem_inits (DECL_CONTEXT (fun
),
900 massaged
, !DECL_GENERATED_P (fun
)))
903 if (!potential
&& !DECL_GENERATED_P (fun
))
906 /* Create the constexpr function table if necessary. */
907 if (constexpr_fundef_table
== NULL
)
908 constexpr_fundef_table
909 = hash_table
<constexpr_fundef_hasher
>::create_ggc (101);
912 tree saved_fn
= current_function_decl
;
913 bool clear_ctx
= false;
914 current_function_decl
= fun
;
915 if (DECL_RESULT (fun
) && DECL_CONTEXT (DECL_RESULT (fun
)) == NULL_TREE
)
918 DECL_CONTEXT (DECL_RESULT (fun
)) = fun
;
920 entry
.body
= copy_fn (fun
, entry
.parms
, entry
.result
);
921 current_function_decl
= saved_fn
;
922 slot
= constexpr_fundef_table
->find_slot (&entry
, INSERT
);
924 DECL_CONTEXT (DECL_RESULT (fun
)) = NULL_TREE
;
927 /* For a template instantiation, we want to remember the pre-generic body
928 for explain_invalid_constexpr_fn, but do tell cxx_eval_call_expression
929 that it doesn't need to bother trying to expand the function. */
930 entry
.result
= error_mark_node
;
932 gcc_assert (*slot
== NULL
);
933 *slot
= ggc_alloc
<constexpr_fundef
> ();
939 /* FUN is a non-constexpr function called in a context that requires a
940 constant expression. If it comes from a constexpr template, explain why
941 the instantiation isn't constexpr. */
944 explain_invalid_constexpr_fn (tree fun
)
946 static hash_set
<tree
> *diagnosed
;
949 /* Only diagnose defaulted functions, lambdas, or instantiations. */
950 if (!DECL_DEFAULTED_FN (fun
)
951 && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun
))
952 && !is_instantiation_of_constexpr (fun
))
954 inform (DECL_SOURCE_LOCATION (fun
), "%qD declared here", fun
);
957 if (diagnosed
== NULL
)
958 diagnosed
= new hash_set
<tree
>;
959 if (diagnosed
->add (fun
))
960 /* Already explained. */
963 save_loc
= input_location
;
964 if (!lambda_static_thunk_p (fun
))
966 /* Diagnostics should completely ignore the static thunk, so leave
967 input_location set to our caller's location. */
968 input_location
= DECL_SOURCE_LOCATION (fun
);
969 inform (input_location
,
970 "%qD is not usable as a %<constexpr%> function because:", fun
);
972 /* First check the declaration. */
973 if (is_valid_constexpr_fn (fun
, true))
975 /* Then if it's OK, the body. */
976 if (!DECL_DECLARED_CONSTEXPR_P (fun
)
977 && DECL_DEFAULTED_FN (fun
))
978 explain_implicit_non_constexpr (fun
);
981 if (constexpr_fundef
*fd
= retrieve_constexpr_fundef (fun
))
984 body
= DECL_SAVED_TREE (fun
);
985 body
= massage_constexpr_body (fun
, body
);
986 require_potential_rvalue_constant_expression (body
);
987 if (DECL_CONSTRUCTOR_P (fun
))
988 cx_check_missing_mem_inits (DECL_CONTEXT (fun
), body
, true);
991 input_location
= save_loc
;
994 /* Objects of this type represent calls to constexpr functions
995 along with the bindings of parameters to their arguments, for
996 the purpose of compile time evaluation. */
998 struct GTY((for_user
)) constexpr_call
{
999 /* Description of the constexpr function definition. */
1000 constexpr_fundef
*fundef
;
1001 /* Parameter bindings environment. A TREE_VEC of arguments. */
1003 /* Result of the call.
1004 NULL means the call is being evaluated.
1005 error_mark_node means that the evaluation was erroneous;
1006 otherwise, the actuall value of the call. */
1008 /* The hash of this call; we remember it here to avoid having to
1009 recalculate it when expanding the hash table. */
1011 /* Whether __builtin_is_constant_evaluated() should evaluate to true. */
1012 bool manifestly_const_eval
;
1015 struct constexpr_call_hasher
: ggc_ptr_hash
<constexpr_call
>
1017 static hashval_t
hash (constexpr_call
*);
1018 static bool equal (constexpr_call
*, constexpr_call
*);
1021 enum constexpr_switch_state
{
1022 /* Used when processing a switch for the first time by cxx_eval_switch_expr
1023 and default: label for that switch has not been seen yet. */
1024 css_default_not_seen
,
1025 /* Used when processing a switch for the first time by cxx_eval_switch_expr
1026 and default: label for that switch has been seen already. */
1028 /* Used when processing a switch for the second time by
1029 cxx_eval_switch_expr, where default: label should match. */
1030 css_default_processing
1033 /* The constexpr expansion context part which needs one instance per
1034 cxx_eval_outermost_constant_expr invocation. VALUES is a map of values of
1035 variables initialized within the expression. */
1037 struct constexpr_global_ctx
{
1038 /* Values for any temporaries or local variables within the
1039 constant-expression. */
1040 hash_map
<tree
,tree
> values
;
1041 /* Number of cxx_eval_constant_expression calls (except skipped ones,
1042 on simple constants or location wrappers) encountered during current
1043 cxx_eval_outermost_constant_expr call. */
1044 HOST_WIDE_INT constexpr_ops_count
;
1045 /* Heap VAR_DECLs created during the evaluation of the outermost constant
1047 auto_vec
<tree
, 16> heap_vars
;
1048 /* Cleanups that need to be evaluated at the end of CLEANUP_POINT_EXPR. */
1049 vec
<tree
> *cleanups
;
1050 /* Number of heap VAR_DECL deallocations. */
1051 unsigned heap_dealloc_count
;
1053 constexpr_global_ctx ()
1054 : constexpr_ops_count (0), cleanups (NULL
), heap_dealloc_count (0) {}
1057 /* The constexpr expansion context. CALL is the current function
1058 expansion, CTOR is the current aggregate initializer, OBJECT is the
1059 object being initialized by CTOR, either a VAR_DECL or a _REF. */
1061 struct constexpr_ctx
{
1062 /* The part of the context that needs to be unique to the whole
1063 cxx_eval_outermost_constant_expr invocation. */
1064 constexpr_global_ctx
*global
;
1065 /* The innermost call we're evaluating. */
1066 constexpr_call
*call
;
1067 /* SAVE_EXPRs and TARGET_EXPR_SLOT vars of TARGET_EXPRs that we've seen
1068 within the current LOOP_EXPR. NULL if we aren't inside a loop. */
1069 vec
<tree
> *save_exprs
;
1070 /* The CONSTRUCTOR we're currently building up for an aggregate
1073 /* The object we're building the CONSTRUCTOR for. */
1075 /* If inside SWITCH_EXPR. */
1076 constexpr_switch_state
*css_state
;
1077 /* The aggregate initialization context inside which this one is nested. This
1078 is used by lookup_placeholder to resolve PLACEHOLDER_EXPRs. */
1079 const constexpr_ctx
*parent
;
1081 /* Whether we should error on a non-constant expression or fail quietly.
1082 This flag needs to be here, but some of the others could move to global
1083 if they get larger than a word. */
1085 /* Whether we are strictly conforming to constant expression rules or
1086 trying harder to get a constant value. */
1088 /* Whether __builtin_is_constant_evaluated () should be true. */
1089 bool manifestly_const_eval
;
1092 /* This internal flag controls whether we should avoid doing anything during
1093 constexpr evaluation that would cause extra DECL_UID generation, such as
1094 template instantiation and function body copying. */
1096 static bool uid_sensitive_constexpr_evaluation_value
;
1098 /* An internal counter that keeps track of the number of times
1099 uid_sensitive_constexpr_evaluation_p returned true. */
1101 static unsigned uid_sensitive_constexpr_evaluation_true_counter
;
1103 /* The accessor for uid_sensitive_constexpr_evaluation_value which also
1104 increments the corresponding counter. */
1107 uid_sensitive_constexpr_evaluation_p ()
1109 if (uid_sensitive_constexpr_evaluation_value
)
1111 ++uid_sensitive_constexpr_evaluation_true_counter
;
1118 /* The default constructor for uid_sensitive_constexpr_evaluation_sentinel
1119 enables the internal flag for uid_sensitive_constexpr_evaluation_p
1120 during the lifetime of the sentinel object. Upon its destruction, the
1121 previous value of uid_sensitive_constexpr_evaluation_p is restored. */
1123 uid_sensitive_constexpr_evaluation_sentinel
1124 ::uid_sensitive_constexpr_evaluation_sentinel ()
1125 : ovr (uid_sensitive_constexpr_evaluation_value
, true)
1129 /* The default constructor for uid_sensitive_constexpr_evaluation_checker
1130 records the current number of times that uid_sensitive_constexpr_evaluation_p
1131 has been called and returned true. */
1133 uid_sensitive_constexpr_evaluation_checker
1134 ::uid_sensitive_constexpr_evaluation_checker ()
1135 : saved_counter (uid_sensitive_constexpr_evaluation_true_counter
)
1139 /* Returns true iff uid_sensitive_constexpr_evaluation_p is true, and
1140 some constexpr evaluation was restricted due to u_s_c_e_p being called
1141 and returning true during the lifetime of this checker object. */
1144 uid_sensitive_constexpr_evaluation_checker::evaluation_restricted_p () const
1146 return (uid_sensitive_constexpr_evaluation_value
1147 && saved_counter
!= uid_sensitive_constexpr_evaluation_true_counter
);
1151 /* A table of all constexpr calls that have been evaluated by the
1152 compiler in this translation unit. */
1154 static GTY (()) hash_table
<constexpr_call_hasher
> *constexpr_call_table
;
1156 static tree
cxx_eval_constant_expression (const constexpr_ctx
*, tree
,
1157 bool, bool *, bool *, tree
* = NULL
);
1159 /* Compute a hash value for a constexpr call representation. */
1162 constexpr_call_hasher::hash (constexpr_call
*info
)
1167 /* Return true if the objects pointed to by P and Q represent calls
1168 to the same constexpr function with the same arguments.
1169 Otherwise, return false. */
1172 constexpr_call_hasher::equal (constexpr_call
*lhs
, constexpr_call
*rhs
)
1176 if (lhs
->hash
!= rhs
->hash
)
1178 if (lhs
->manifestly_const_eval
!= rhs
->manifestly_const_eval
)
1180 if (!constexpr_fundef_hasher::equal (lhs
->fundef
, rhs
->fundef
))
1182 return cp_tree_equal (lhs
->bindings
, rhs
->bindings
);
1185 /* Initialize the constexpr call table, if needed. */
1188 maybe_initialize_constexpr_call_table (void)
1190 if (constexpr_call_table
== NULL
)
1191 constexpr_call_table
= hash_table
<constexpr_call_hasher
>::create_ggc (101);
1194 /* During constexpr CALL_EXPR evaluation, to avoid issues with sharing when
1195 a function happens to get called recursively, we unshare the callee
1196 function's body and evaluate this unshared copy instead of evaluating the
1199 FUNDEF_COPIES_TABLE is a per-function freelist of these unshared function
1200 copies. The underlying data structure of FUNDEF_COPIES_TABLE is a hash_map
1201 that's keyed off of the original FUNCTION_DECL and whose value is a
1202 TREE_LIST of this function's unused copies awaiting reuse.
1204 This is not GC-deletable to avoid GC affecting UID generation. */
1206 static GTY(()) decl_tree_map
*fundef_copies_table
;
1208 /* Reuse a copy or create a new unshared copy of the function FUN.
1209 Return this copy. We use a TREE_LIST whose PURPOSE is body, VALUE
1210 is parms, TYPE is result. */
1213 get_fundef_copy (constexpr_fundef
*fundef
)
1217 tree
*slot
= &(hash_map_safe_get_or_insert
<hm_ggc
>
1218 (fundef_copies_table
, fundef
->decl
, &existed
, 127));
1222 /* There is no cached function available, or in use. We can use
1223 the function directly. That the slot is now created records
1224 that this function is now in use. */
1225 copy
= build_tree_list (fundef
->body
, fundef
->parms
);
1226 TREE_TYPE (copy
) = fundef
->result
;
1228 else if (*slot
== NULL_TREE
)
1230 if (uid_sensitive_constexpr_evaluation_p ())
1233 /* We've already used the function itself, so make a copy. */
1234 copy
= build_tree_list (NULL
, NULL
);
1235 tree saved_body
= DECL_SAVED_TREE (fundef
->decl
);
1236 tree saved_parms
= DECL_ARGUMENTS (fundef
->decl
);
1237 tree saved_result
= DECL_RESULT (fundef
->decl
);
1238 tree saved_fn
= current_function_decl
;
1239 DECL_SAVED_TREE (fundef
->decl
) = fundef
->body
;
1240 DECL_ARGUMENTS (fundef
->decl
) = fundef
->parms
;
1241 DECL_RESULT (fundef
->decl
) = fundef
->result
;
1242 current_function_decl
= fundef
->decl
;
1243 TREE_PURPOSE (copy
) = copy_fn (fundef
->decl
, TREE_VALUE (copy
),
1245 current_function_decl
= saved_fn
;
1246 DECL_RESULT (fundef
->decl
) = saved_result
;
1247 DECL_ARGUMENTS (fundef
->decl
) = saved_parms
;
1248 DECL_SAVED_TREE (fundef
->decl
) = saved_body
;
1252 /* We have a cached function available. */
1254 *slot
= TREE_CHAIN (copy
);
1260 /* Save the copy COPY of function FUN for later reuse by
1261 get_fundef_copy(). By construction, there will always be an entry
1265 save_fundef_copy (tree fun
, tree copy
)
1267 tree
*slot
= fundef_copies_table
->get (fun
);
1268 TREE_CHAIN (copy
) = *slot
;
1272 /* We have an expression tree T that represents a call, either CALL_EXPR
1273 or AGGR_INIT_EXPR. Return the Nth argument. */
1276 get_nth_callarg (tree t
, int n
)
1278 switch (TREE_CODE (t
))
1281 return CALL_EXPR_ARG (t
, n
);
1283 case AGGR_INIT_EXPR
:
1284 return AGGR_INIT_EXPR_ARG (t
, n
);
1292 /* Attempt to evaluate T which represents a call to a builtin function.
1293 We assume here that all builtin functions evaluate to scalar types
1294 represented by _CST nodes. */
1297 cxx_eval_builtin_function_call (const constexpr_ctx
*ctx
, tree t
, tree fun
,
1299 bool *non_constant_p
, bool *overflow_p
)
1301 const int nargs
= call_expr_nargs (t
);
1302 tree
*args
= (tree
*) alloca (nargs
* sizeof (tree
));
1306 /* Don't fold __builtin_constant_p within a constexpr function. */
1307 bool bi_const_p
= DECL_IS_BUILTIN_CONSTANT_P (fun
);
1309 /* If we aren't requiring a constant expression, defer __builtin_constant_p
1310 in a constexpr function until we have values for the parameters. */
1312 && !ctx
->manifestly_const_eval
1313 && current_function_decl
1314 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
1316 *non_constant_p
= true;
1320 /* For __builtin_is_constant_evaluated, defer it if not
1321 ctx->manifestly_const_eval, otherwise fold it to true. */
1322 if (fndecl_built_in_p (fun
, CP_BUILT_IN_IS_CONSTANT_EVALUATED
,
1325 if (!ctx
->manifestly_const_eval
)
1327 *non_constant_p
= true;
1330 return boolean_true_node
;
1333 if (fndecl_built_in_p (fun
, CP_BUILT_IN_SOURCE_LOCATION
, BUILT_IN_FRONTEND
))
1334 return fold_builtin_source_location (EXPR_LOCATION (t
));
1338 if (fndecl_built_in_p (fun
, BUILT_IN_NORMAL
))
1339 switch (DECL_FUNCTION_CODE (fun
))
1341 case BUILT_IN_STRLEN
:
1342 case BUILT_IN_STRNLEN
:
1345 case BUILT_IN_MEMCHR
:
1346 case BUILT_IN_STRCHR
:
1347 case BUILT_IN_STRRCHR
:
1351 case BUILT_IN_MEMCMP
:
1352 case BUILT_IN_STRCMP
:
1355 case BUILT_IN_STRSTR
:
1362 /* Be permissive for arguments to built-ins; __builtin_constant_p should
1363 return constant false for a non-constant argument. */
1364 constexpr_ctx new_ctx
= *ctx
;
1365 new_ctx
.quiet
= true;
1366 for (i
= 0; i
< nargs
; ++i
)
1368 tree arg
= CALL_EXPR_ARG (t
, i
);
1371 /* To handle string built-ins we need to pass ADDR_EXPR<STRING_CST> since
1372 expand_builtin doesn't know how to look in the values table. */
1373 bool strop
= i
< strops
;
1377 if (TREE_CODE (arg
) == ADDR_EXPR
)
1378 arg
= TREE_OPERAND (arg
, 0);
1383 /* If builtin_valid_in_constant_expr_p is true,
1384 potential_constant_expression_1 has not recursed into the arguments
1385 of the builtin, verify it here. */
1386 if (!builtin_valid_in_constant_expr_p (fun
)
1387 || potential_constant_expression (arg
))
1389 bool dummy1
= false, dummy2
= false;
1390 arg
= cxx_eval_constant_expression (&new_ctx
, arg
, false,
1395 /* For __builtin_constant_p, fold all expressions with constant values
1396 even if they aren't C++ constant-expressions. */
1397 arg
= cp_fold_rvalue (arg
);
1400 if (TREE_CODE (arg
) == CONSTRUCTOR
)
1401 arg
= braced_lists_to_strings (TREE_TYPE (arg
), arg
);
1402 if (TREE_CODE (arg
) == STRING_CST
)
1403 arg
= build_address (arg
);
1411 bool save_ffbcp
= force_folding_builtin_constant_p
;
1412 force_folding_builtin_constant_p
|= ctx
->manifestly_const_eval
;
1413 tree save_cur_fn
= current_function_decl
;
1414 /* Return name of ctx->call->fundef->decl for __builtin_FUNCTION (). */
1415 if (fndecl_built_in_p (fun
, BUILT_IN_FUNCTION
)
1417 && ctx
->call
->fundef
)
1418 current_function_decl
= ctx
->call
->fundef
->decl
;
1419 new_call
= fold_builtin_call_array (EXPR_LOCATION (t
), TREE_TYPE (t
),
1420 CALL_EXPR_FN (t
), nargs
, args
);
1421 current_function_decl
= save_cur_fn
;
1422 force_folding_builtin_constant_p
= save_ffbcp
;
1423 if (new_call
== NULL
)
1425 if (!*non_constant_p
&& !ctx
->quiet
)
1427 /* Do not allow__builtin_unreachable in constexpr function.
1428 The __builtin_unreachable call with BUILTINS_LOCATION
1429 comes from cp_maybe_instrument_return. */
1430 if (fndecl_built_in_p (fun
, BUILT_IN_UNREACHABLE
)
1431 && EXPR_LOCATION (t
) == BUILTINS_LOCATION
)
1432 error ("%<constexpr%> call flows off the end of the function");
1435 new_call
= build_call_array_loc (EXPR_LOCATION (t
), TREE_TYPE (t
),
1436 CALL_EXPR_FN (t
), nargs
, args
);
1437 error ("%q+E is not a constant expression", new_call
);
1440 *non_constant_p
= true;
1444 if (!potential_constant_expression (new_call
))
1446 if (!*non_constant_p
&& !ctx
->quiet
)
1447 error ("%q+E is not a constant expression", new_call
);
1448 *non_constant_p
= true;
1454 /* memchr returns a pointer into the first argument, but we replaced the
1455 argument above with a STRING_CST; put it back it now. */
1456 tree op
= CALL_EXPR_ARG (t
, strret
-1);
1457 STRIP_NOPS (new_call
);
1458 if (TREE_CODE (new_call
) == POINTER_PLUS_EXPR
)
1459 TREE_OPERAND (new_call
, 0) = op
;
1460 else if (TREE_CODE (new_call
) == ADDR_EXPR
)
1464 return cxx_eval_constant_expression (&new_ctx
, new_call
, lval
,
1465 non_constant_p
, overflow_p
);
1468 /* TEMP is the constant value of a temporary object of type TYPE. Adjust
1469 the type of the value to match. */
1472 adjust_temp_type (tree type
, tree temp
)
1474 if (same_type_p (TREE_TYPE (temp
), type
))
1476 /* Avoid wrapping an aggregate value in a NOP_EXPR. */
1477 if (TREE_CODE (temp
) == CONSTRUCTOR
)
1479 /* build_constructor wouldn't retain various CONSTRUCTOR flags. */
1480 tree t
= copy_node (temp
);
1481 TREE_TYPE (t
) = type
;
1484 if (TREE_CODE (temp
) == EMPTY_CLASS_EXPR
)
1485 return build0 (EMPTY_CLASS_EXPR
, type
);
1486 gcc_assert (scalarish_type_p (type
));
1487 /* Now we know we're dealing with a scalar, and a prvalue of non-class
1488 type is cv-unqualified. */
1489 return cp_fold_convert (cv_unqualified (type
), temp
);
1492 /* If T is a CONSTRUCTOR, return an unshared copy of T and any
1493 sub-CONSTRUCTORs. Otherwise return T.
1495 We use this whenever we initialize an object as a whole, whether it's a
1496 parameter, a local variable, or a subobject, so that subsequent
1497 modifications don't affect other places where it was used. */
1500 unshare_constructor (tree t MEM_STAT_DECL
)
1502 if (!t
|| TREE_CODE (t
) != CONSTRUCTOR
)
1504 auto_vec
<tree
*, 4> ptrs
;
1505 ptrs
.safe_push (&t
);
1506 while (!ptrs
.is_empty ())
1508 tree
*p
= ptrs
.pop ();
1509 tree n
= copy_node (*p PASS_MEM_STAT
);
1510 CONSTRUCTOR_ELTS (n
) = vec_safe_copy (CONSTRUCTOR_ELTS (*p
) PASS_MEM_STAT
);
1512 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (n
);
1513 constructor_elt
*ce
;
1514 for (HOST_WIDE_INT i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
1515 if (TREE_CODE (ce
->value
) == CONSTRUCTOR
)
1516 ptrs
.safe_push (&ce
->value
);
1521 /* If T is a CONSTRUCTOR, ggc_free T and any sub-CONSTRUCTORs. */
1524 free_constructor (tree t
)
1526 if (!t
|| TREE_CODE (t
) != CONSTRUCTOR
)
1528 releasing_vec ctors
;
1529 vec_safe_push (ctors
, t
);
1530 while (!ctors
->is_empty ())
1532 tree c
= ctors
->pop ();
1533 if (vec
<constructor_elt
, va_gc
> *elts
= CONSTRUCTOR_ELTS (c
))
1535 constructor_elt
*ce
;
1536 for (HOST_WIDE_INT i
= 0; vec_safe_iterate (elts
, i
, &ce
); ++i
)
1537 if (TREE_CODE (ce
->value
) == CONSTRUCTOR
)
1538 vec_safe_push (ctors
, ce
->value
);
1545 /* Subroutine of cxx_eval_call_expression.
1546 We are processing a call expression (either CALL_EXPR or
1547 AGGR_INIT_EXPR) in the context of CTX. Evaluate
1548 all arguments and bind their values to correspondings
1549 parameters, making up the NEW_CALL context. */
1552 cxx_bind_parameters_in_call (const constexpr_ctx
*ctx
, tree t
,
1553 constexpr_call
*new_call
,
1554 bool *non_constant_p
, bool *overflow_p
,
1555 bool *non_constant_args
)
1557 const int nargs
= call_expr_nargs (t
);
1558 tree fun
= new_call
->fundef
->decl
;
1559 tree parms
= new_call
->fundef
->parms
;
1561 /* We don't record ellipsis args below. */
1562 int nparms
= list_length (parms
);
1563 int nbinds
= nargs
< nparms
? nargs
: nparms
;
1564 tree binds
= new_call
->bindings
= make_tree_vec (nbinds
);
1565 for (i
= 0; i
< nargs
; ++i
)
1568 tree type
= parms
? TREE_TYPE (parms
) : void_type_node
;
1569 x
= get_nth_callarg (t
, i
);
1570 /* For member function, the first argument is a pointer to the implied
1571 object. For a constructor, it might still be a dummy object, in
1572 which case we get the real argument from ctx. */
1573 if (i
== 0 && DECL_CONSTRUCTOR_P (fun
)
1574 && is_dummy_object (x
))
1577 x
= build_address (x
);
1579 if (TREE_ADDRESSABLE (type
))
1580 /* Undo convert_for_arg_passing work here. */
1581 x
= convert_from_reference (x
);
1582 arg
= cxx_eval_constant_expression (ctx
, x
, /*lval=*/false,
1583 non_constant_p
, overflow_p
);
1584 /* Don't VERIFY_CONSTANT here. */
1585 if (*non_constant_p
&& ctx
->quiet
)
1587 /* Just discard ellipsis args after checking their constantitude. */
1591 if (!*non_constant_p
)
1593 /* Make sure the binding has the same type as the parm. But
1594 only for constant args. */
1595 if (!TYPE_REF_P (type
))
1596 arg
= adjust_temp_type (type
, arg
);
1597 if (!TREE_CONSTANT (arg
))
1598 *non_constant_args
= true;
1599 /* For virtual calls, adjust the this argument, so that it is
1600 the object on which the method is called, rather than
1601 one of its bases. */
1602 if (i
== 0 && DECL_VIRTUAL_P (fun
))
1606 if (TREE_CODE (addr
) == ADDR_EXPR
)
1608 tree obj
= TREE_OPERAND (addr
, 0);
1609 while (TREE_CODE (obj
) == COMPONENT_REF
1610 && DECL_FIELD_IS_BASE (TREE_OPERAND (obj
, 1))
1611 && !same_type_ignoring_top_level_qualifiers_p
1612 (TREE_TYPE (obj
), DECL_CONTEXT (fun
)))
1613 obj
= TREE_OPERAND (obj
, 0);
1614 if (obj
!= TREE_OPERAND (addr
, 0))
1615 arg
= build_fold_addr_expr_with_type (obj
,
1619 TREE_VEC_ELT (binds
, i
) = arg
;
1621 parms
= TREE_CHAIN (parms
);
1625 /* Variables and functions to manage constexpr call expansion context.
1626 These do not need to be marked for PCH or GC. */
1628 /* FIXME remember and print actual constant arguments. */
1629 static vec
<tree
> call_stack
;
1630 static int call_stack_tick
;
1631 static int last_cx_error_tick
;
1634 push_cx_call_context (tree call
)
1637 if (!EXPR_HAS_LOCATION (call
))
1638 SET_EXPR_LOCATION (call
, input_location
);
1639 call_stack
.safe_push (call
);
1640 int len
= call_stack
.length ();
1641 if (len
> max_constexpr_depth
)
1647 pop_cx_call_context (void)
1654 cx_error_context (void)
1656 vec
<tree
> r
= vNULL
;
1657 if (call_stack_tick
!= last_cx_error_tick
1658 && !call_stack
.is_empty ())
1660 last_cx_error_tick
= call_stack_tick
;
1664 /* Evaluate a call T to a GCC internal function when possible and return
1665 the evaluated result or, under the control of CTX, give an error, set
1666 NON_CONSTANT_P, and return the unevaluated call T otherwise. */
1669 cxx_eval_internal_function (const constexpr_ctx
*ctx
, tree t
,
1671 bool *non_constant_p
, bool *overflow_p
)
1673 enum tree_code opcode
= ERROR_MARK
;
1675 switch (CALL_EXPR_IFN (t
))
1677 case IFN_UBSAN_NULL
:
1678 case IFN_UBSAN_BOUNDS
:
1679 case IFN_UBSAN_VPTR
:
1680 case IFN_FALLTHROUGH
:
1683 case IFN_ADD_OVERFLOW
:
1686 case IFN_SUB_OVERFLOW
:
1687 opcode
= MINUS_EXPR
;
1689 case IFN_MUL_OVERFLOW
:
1694 return cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 0),
1695 false, non_constant_p
, overflow_p
);
1697 case IFN_VEC_CONVERT
:
1699 tree arg
= cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 0),
1700 false, non_constant_p
,
1702 if (TREE_CODE (arg
) == VECTOR_CST
)
1703 return fold_const_call (CFN_VEC_CONVERT
, TREE_TYPE (t
), arg
);
1706 *non_constant_p
= true;
1713 error_at (cp_expr_loc_or_input_loc (t
),
1714 "call to internal function %qE", t
);
1715 *non_constant_p
= true;
1719 /* Evaluate constant arguments using OPCODE and return a complex
1720 number containing the result and the overflow bit. */
1721 tree arg0
= cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 0), lval
,
1722 non_constant_p
, overflow_p
);
1723 tree arg1
= cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 1), lval
,
1724 non_constant_p
, overflow_p
);
1726 if (TREE_CODE (arg0
) == INTEGER_CST
&& TREE_CODE (arg1
) == INTEGER_CST
)
1728 location_t loc
= cp_expr_loc_or_input_loc (t
);
1729 tree type
= TREE_TYPE (TREE_TYPE (t
));
1730 tree result
= fold_binary_loc (loc
, opcode
, type
,
1731 fold_convert_loc (loc
, type
, arg0
),
1732 fold_convert_loc (loc
, type
, arg1
));
1734 = build_int_cst (type
, arith_overflowed_p (opcode
, type
, arg0
, arg1
));
1735 /* Reset TREE_OVERFLOW to avoid warnings for the overflow. */
1736 if (TREE_OVERFLOW (result
))
1737 TREE_OVERFLOW (result
) = 0;
1739 return build_complex (TREE_TYPE (t
), result
, ovf
);
1742 *non_constant_p
= true;
1746 /* Clean CONSTRUCTOR_NO_CLEARING from CTOR and its sub-aggregates. */
1749 clear_no_implicit_zero (tree ctor
)
1751 if (CONSTRUCTOR_NO_CLEARING (ctor
))
1753 CONSTRUCTOR_NO_CLEARING (ctor
) = false;
1754 tree elt
; unsigned HOST_WIDE_INT idx
;
1755 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor
), idx
, elt
)
1756 if (TREE_CODE (elt
) == CONSTRUCTOR
)
1757 clear_no_implicit_zero (elt
);
1761 /* Complain about a const object OBJ being modified in a constant expression.
1762 EXPR is the MODIFY_EXPR expression performing the modification. */
1765 modifying_const_object_error (tree expr
, tree obj
)
1767 location_t loc
= cp_expr_loc_or_input_loc (expr
);
1768 auto_diagnostic_group d
;
1769 error_at (loc
, "modifying a const object %qE is not allowed in "
1770 "a constant expression", TREE_OPERAND (expr
, 0));
1771 inform (location_of (obj
), "originally declared %<const%> here");
1774 /* Return true if FNDECL is a replaceable global allocation function that
1775 should be useable during constant expression evaluation. */
1778 cxx_replaceable_global_alloc_fn (tree fndecl
)
1780 return (cxx_dialect
>= cxx20
1781 && IDENTIFIER_NEWDEL_OP_P (DECL_NAME (fndecl
))
1782 && CP_DECL_CONTEXT (fndecl
) == global_namespace
1783 && (DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl
)
1784 || DECL_IS_OPERATOR_DELETE_P (fndecl
)));
1787 /* Return true if FNDECL is a placement new function that should be
1788 useable during constant expression evaluation of std::construct_at. */
1791 cxx_placement_new_fn (tree fndecl
)
1793 if (cxx_dialect
>= cxx20
1794 && IDENTIFIER_NEW_OP_P (DECL_NAME (fndecl
))
1795 && CP_DECL_CONTEXT (fndecl
) == global_namespace
1796 && !DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl
)
1797 && TREE_CODE (TREE_TYPE (fndecl
)) == FUNCTION_TYPE
)
1799 tree first_arg
= TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
1800 if (TREE_VALUE (first_arg
) == ptr_type_node
1801 && TREE_CHAIN (first_arg
) == void_list_node
)
1807 /* Return true if FNDECL is std::construct_at. */
1810 is_std_construct_at (tree fndecl
)
1812 if (!decl_in_std_namespace_p (fndecl
))
1815 tree name
= DECL_NAME (fndecl
);
1816 return name
&& id_equal (name
, "construct_at");
1819 /* Return true if FNDECL is std::allocator<T>::{,de}allocate. */
1822 is_std_allocator_allocate (tree fndecl
)
1824 tree name
= DECL_NAME (fndecl
);
1825 if (name
== NULL_TREE
1826 || !(id_equal (name
, "allocate") || id_equal (name
, "deallocate")))
1829 tree ctx
= DECL_CONTEXT (fndecl
);
1830 if (ctx
== NULL_TREE
|| !CLASS_TYPE_P (ctx
) || !TYPE_MAIN_DECL (ctx
))
1833 tree decl
= TYPE_MAIN_DECL (ctx
);
1834 name
= DECL_NAME (decl
);
1835 if (name
== NULL_TREE
|| !id_equal (name
, "allocator"))
1838 return decl_in_std_namespace_p (decl
);
1841 /* Return true if FNDECL is __dynamic_cast. */
1844 cxx_dynamic_cast_fn_p (tree fndecl
)
1846 return (cxx_dialect
>= cxx20
1847 && id_equal (DECL_NAME (fndecl
), "__dynamic_cast")
1848 && CP_DECL_CONTEXT (fndecl
) == global_namespace
);
1851 /* Often, we have an expression in the form of address + offset, e.g.
1852 "&_ZTV1A + 16". Extract the object from it, i.e. "_ZTV1A". */
1855 extract_obj_from_addr_offset (tree expr
)
1857 if (TREE_CODE (expr
) == POINTER_PLUS_EXPR
)
1858 expr
= TREE_OPERAND (expr
, 0);
1860 if (TREE_CODE (expr
) == ADDR_EXPR
)
1861 expr
= TREE_OPERAND (expr
, 0);
1865 /* Given a PATH like
1867 g.D.2181.D.2154.D.2102.D.2093
1869 find a component with type TYPE. Return NULL_TREE if not found, and
1870 error_mark_node if the component is not accessible. If STOP is non-null,
1871 this function will return NULL_TREE if STOP is found before TYPE. */
1874 get_component_with_type (tree path
, tree type
, tree stop
)
1878 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path
), type
))
1882 && (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path
),
1885 else if (TREE_CODE (path
) == COMPONENT_REF
1886 && DECL_FIELD_IS_BASE (TREE_OPERAND (path
, 1)))
1888 /* We need to check that the component we're accessing is in fact
1890 if (TREE_PRIVATE (TREE_OPERAND (path
, 1))
1891 || TREE_PROTECTED (TREE_OPERAND (path
, 1)))
1892 return error_mark_node
;
1893 path
= TREE_OPERAND (path
, 0);
1900 /* Evaluate a call to __dynamic_cast (permitted by P1327R1).
1902 The declaration of __dynamic_cast is:
1904 void* __dynamic_cast (const void* __src_ptr,
1905 const __class_type_info* __src_type,
1906 const __class_type_info* __dst_type,
1907 ptrdiff_t __src2dst);
1909 where src2dst has the following possible values
1911 >-1: src_type is a unique public non-virtual base of dst_type
1912 dst_ptr + src2dst == src_ptr
1913 -1: unspecified relationship
1914 -2: src_type is not a public base of dst_type
1915 -3: src_type is a multiple public non-virtual base of dst_type
1917 Since literal types can't have virtual bases, we only expect hint >=0,
1921 cxx_eval_dynamic_cast_fn (const constexpr_ctx
*ctx
, tree call
,
1922 bool *non_constant_p
, bool *overflow_p
)
1924 /* T will be something like
1925 __dynamic_cast ((B*) b, &_ZTI1B, &_ZTI1D, 8)
1927 gcc_assert (call_expr_nargs (call
) == 4);
1928 tsubst_flags_t complain
= ctx
->quiet
? tf_none
: tf_warning_or_error
;
1929 tree obj
= CALL_EXPR_ARG (call
, 0);
1930 tree type
= CALL_EXPR_ARG (call
, 2);
1931 HOST_WIDE_INT hint
= int_cst_value (CALL_EXPR_ARG (call
, 3));
1932 location_t loc
= cp_expr_loc_or_input_loc (call
);
1934 /* Get the target type of the dynamic_cast. */
1935 gcc_assert (TREE_CODE (type
) == ADDR_EXPR
);
1936 type
= TREE_OPERAND (type
, 0);
1937 type
= TREE_TYPE (DECL_NAME (type
));
1939 /* TYPE can only be either T* or T&. We can't know which of these it
1940 is by looking at TYPE, but OBJ will be "(T*) x" in the first case,
1941 and something like "(T*)(T&)(T*) x" in the second case. */
1942 bool reference_p
= false;
1943 while (CONVERT_EXPR_P (obj
) || TREE_CODE (obj
) == SAVE_EXPR
)
1945 reference_p
|= TYPE_REF_P (TREE_TYPE (obj
));
1946 obj
= TREE_OPERAND (obj
, 0);
1949 /* Evaluate the object so that we know its dynamic type. */
1950 obj
= cxx_eval_constant_expression (ctx
, obj
, /*lval*/false, non_constant_p
,
1952 if (*non_constant_p
)
1955 /* We expect OBJ to be in form of &d.D.2102 when HINT == 0,
1956 but when HINT is > 0, it can also be something like
1957 &d.D.2102 + 18446744073709551608, which includes the BINFO_OFFSET. */
1958 obj
= extract_obj_from_addr_offset (obj
);
1959 const tree objtype
= TREE_TYPE (obj
);
1960 /* If OBJ doesn't refer to a base field, we're done. */
1961 if (tree t
= (TREE_CODE (obj
) == COMPONENT_REF
1962 ? TREE_OPERAND (obj
, 1) : obj
))
1963 if (TREE_CODE (t
) != FIELD_DECL
|| !DECL_FIELD_IS_BASE (t
))
1969 error_at (loc
, "reference %<dynamic_cast%> failed");
1970 inform (loc
, "dynamic type %qT of its operand does "
1971 "not have a base class of type %qT",
1974 *non_constant_p
= true;
1976 return integer_zero_node
;
1979 /* [class.cdtor] When a dynamic_cast is used in a constructor ...
1980 or in a destructor ... if the operand of the dynamic_cast refers
1981 to the object under construction or destruction, this object is
1982 considered to be a most derived object that has the type of the
1983 constructor or destructor's class. */
1984 tree vtable
= build_vfield_ref (obj
, TREE_TYPE (obj
));
1985 vtable
= cxx_eval_constant_expression (ctx
, vtable
, /*lval*/false,
1986 non_constant_p
, overflow_p
);
1987 if (*non_constant_p
)
1989 /* VTABLE will be &_ZTV1A + 16 or similar, get _ZTV1A. */
1990 vtable
= extract_obj_from_addr_offset (vtable
);
1991 const tree mdtype
= DECL_CONTEXT (vtable
);
1993 /* Given dynamic_cast<T>(v),
1995 [expr.dynamic.cast] If C is the class type to which T points or refers,
1996 the runtime check logically executes as follows:
1998 If, in the most derived object pointed (referred) to by v, v points
1999 (refers) to a public base class subobject of a C object, and if only
2000 one object of type C is derived from the subobject pointed (referred)
2001 to by v the result points (refers) to that C object.
2003 In this case, HINT >= 0 or -3. */
2004 if (hint
>= 0 || hint
== -3)
2006 /* Look for a component with type TYPE. */
2007 tree t
= get_component_with_type (obj
, type
, mdtype
);
2008 /* If not accessible, give an error. */
2009 if (t
== error_mark_node
)
2015 error_at (loc
, "reference %<dynamic_cast%> failed");
2016 inform (loc
, "static type %qT of its operand is a "
2017 "non-public base class of dynamic type %qT",
2021 *non_constant_p
= true;
2023 return integer_zero_node
;
2026 /* The result points to the TYPE object. */
2027 return cp_build_addr_expr (t
, complain
);
2028 /* Else, TYPE was not found, because the HINT turned out to be wrong.
2029 Fall through to the normal processing. */
2032 /* Otherwise, if v points (refers) to a public base class subobject of the
2033 most derived object, and the type of the most derived object has a base
2034 class, of type C, that is unambiguous and public, the result points
2035 (refers) to the C subobject of the most derived object.
2037 But it can also be an invalid case. */
2039 /* Get the most derived object. */
2040 obj
= get_component_with_type (obj
, mdtype
, NULL_TREE
);
2041 if (obj
== error_mark_node
)
2047 error_at (loc
, "reference %<dynamic_cast%> failed");
2048 inform (loc
, "static type %qT of its operand is a non-public"
2049 " base class of dynamic type %qT", objtype
, mdtype
);
2051 *non_constant_p
= true;
2053 return integer_zero_node
;
2058 /* Check that the type of the most derived object has a base class
2059 of type TYPE that is unambiguous and public. */
2061 tree binfo
= lookup_base (mdtype
, type
, ba_check
, &b_kind
, tf_none
);
2062 if (!binfo
|| binfo
== error_mark_node
)
2068 error_at (loc
, "reference %<dynamic_cast%> failed");
2069 if (b_kind
== bk_ambig
)
2070 inform (loc
, "%qT is an ambiguous base class of dynamic "
2071 "type %qT of its operand", type
, mdtype
);
2073 inform (loc
, "dynamic type %qT of its operand does not "
2074 "have an unambiguous public base class %qT",
2077 *non_constant_p
= true;
2079 return integer_zero_node
;
2081 /* If so, return the TYPE subobject of the most derived object. */
2082 obj
= convert_to_base_statically (obj
, binfo
);
2083 return cp_build_addr_expr (obj
, complain
);
2086 /* Data structure used by replace_result_decl and replace_result_decl_r. */
2088 struct replace_result_decl_data
2090 /* The RESULT_DECL we want to replace. */
2092 /* The replacement for DECL. */
2094 /* Whether we've performed any replacements. */
2098 /* Helper function for replace_result_decl, called through cp_walk_tree. */
2101 replace_result_decl_r (tree
*tp
, int *walk_subtrees
, void *data
)
2103 replace_result_decl_data
*d
= (replace_result_decl_data
*) data
;
2107 *tp
= unshare_expr (d
->replacement
);
2111 else if (TYPE_P (*tp
))
2117 /* Replace every occurrence of DECL, a RESULT_DECL, with (an unshared copy of)
2118 REPLACEMENT within the reduced constant expression *TP. Returns true iff a
2119 replacement was performed. */
2122 replace_result_decl (tree
*tp
, tree decl
, tree replacement
)
2124 gcc_checking_assert (TREE_CODE (decl
) == RESULT_DECL
2125 && (same_type_ignoring_top_level_qualifiers_p
2126 (TREE_TYPE (decl
), TREE_TYPE (replacement
))));
2127 replace_result_decl_data data
= { decl
, replacement
, false };
2128 cp_walk_tree_without_duplicates (tp
, replace_result_decl_r
, &data
);
2129 return data
.changed
;
2132 /* Evaluate the call T to virtual function thunk THUNK_FNDECL. */
2135 cxx_eval_thunk_call (const constexpr_ctx
*ctx
, tree t
, tree thunk_fndecl
,
2137 bool *non_constant_p
, bool *overflow_p
)
2139 tree function
= THUNK_TARGET (thunk_fndecl
);
2141 /* virtual_offset is only set in the presence of virtual bases, which make
2142 the class non-literal, so we don't need to handle it here. */
2143 if (THUNK_VIRTUAL_OFFSET (thunk_fndecl
))
2145 gcc_assert (!DECL_DECLARED_CONSTEXPR_P (function
));
2148 error ("call to non-%<constexpr%> function %qD", function
);
2149 explain_invalid_constexpr_fn (function
);
2151 *non_constant_p
= true;
2155 tree new_call
= copy_node (t
);
2156 CALL_EXPR_FN (new_call
) = function
;
2157 TREE_TYPE (new_call
) = TREE_TYPE (TREE_TYPE (function
));
2159 tree offset
= size_int (THUNK_FIXED_OFFSET (thunk_fndecl
));
2161 if (DECL_THIS_THUNK_P (thunk_fndecl
))
2163 /* 'this'-adjusting thunk. */
2164 tree this_arg
= CALL_EXPR_ARG (t
, 0);
2165 this_arg
= build2 (POINTER_PLUS_EXPR
, TREE_TYPE (this_arg
),
2167 CALL_EXPR_ARG (new_call
, 0) = this_arg
;
2170 /* Return-adjusting thunk. */
2171 new_call
= build2 (POINTER_PLUS_EXPR
, TREE_TYPE (new_call
),
2174 return cxx_eval_constant_expression (ctx
, new_call
, lval
,
2175 non_constant_p
, overflow_p
);
2178 /* Subroutine of cxx_eval_constant_expression.
2179 Evaluate the call expression tree T in the context of OLD_CALL expression
2183 cxx_eval_call_expression (const constexpr_ctx
*ctx
, tree t
,
2185 bool *non_constant_p
, bool *overflow_p
)
2187 /* Handle concept checks separately. */
2188 if (concept_check_p (t
))
2189 return evaluate_concept_check (t
, tf_warning_or_error
);
2191 location_t loc
= cp_expr_loc_or_input_loc (t
);
2192 tree fun
= get_function_named_in_call (t
);
2193 constexpr_call new_call
2194 = { NULL
, NULL
, NULL
, 0, ctx
->manifestly_const_eval
};
2197 if (fun
== NULL_TREE
)
2198 return cxx_eval_internal_function (ctx
, t
, lval
,
2199 non_constant_p
, overflow_p
);
2201 if (TREE_CODE (fun
) != FUNCTION_DECL
)
2203 /* Might be a constexpr function pointer. */
2204 fun
= cxx_eval_constant_expression (ctx
, fun
,
2205 /*lval*/false, non_constant_p
,
2208 if (TREE_CODE (fun
) == ADDR_EXPR
)
2209 fun
= TREE_OPERAND (fun
, 0);
2210 /* For TARGET_VTABLE_USES_DESCRIPTORS targets, there is no
2211 indirection, the called expression is a pointer into the
2212 virtual table which should contain FDESC_EXPR. Extract the
2213 FUNCTION_DECL from there. */
2214 else if (TARGET_VTABLE_USES_DESCRIPTORS
2215 && TREE_CODE (fun
) == POINTER_PLUS_EXPR
2216 && TREE_CODE (TREE_OPERAND (fun
, 0)) == ADDR_EXPR
2217 && TREE_CODE (TREE_OPERAND (fun
, 1)) == INTEGER_CST
)
2219 tree d
= TREE_OPERAND (TREE_OPERAND (fun
, 0), 0);
2221 && DECL_VTABLE_OR_VTT_P (d
)
2222 && TREE_CODE (TREE_TYPE (d
)) == ARRAY_TYPE
2223 && TREE_TYPE (TREE_TYPE (d
)) == vtable_entry_type
2225 && TREE_CODE (DECL_INITIAL (d
)) == CONSTRUCTOR
)
2227 tree i
= int_const_binop (TRUNC_DIV_EXPR
, TREE_OPERAND (fun
, 1),
2228 TYPE_SIZE_UNIT (vtable_entry_type
));
2229 HOST_WIDE_INT idx
= find_array_ctor_elt (DECL_INITIAL (d
), i
);
2233 = (*CONSTRUCTOR_ELTS (DECL_INITIAL (d
)))[idx
].value
;
2234 if (TREE_CODE (fdesc
) == FDESC_EXPR
2235 && integer_zerop (TREE_OPERAND (fdesc
, 1)))
2236 fun
= TREE_OPERAND (fdesc
, 0);
2241 if (TREE_CODE (fun
) != FUNCTION_DECL
)
2243 if (!ctx
->quiet
&& !*non_constant_p
)
2244 error_at (loc
, "expression %qE does not designate a %<constexpr%> "
2246 *non_constant_p
= true;
2249 if (DECL_CLONED_FUNCTION_P (fun
))
2250 fun
= DECL_CLONED_FUNCTION (fun
);
2252 if (is_ubsan_builtin_p (fun
))
2255 if (fndecl_built_in_p (fun
))
2256 return cxx_eval_builtin_function_call (ctx
, t
, fun
,
2257 lval
, non_constant_p
, overflow_p
);
2258 if (DECL_THUNK_P (fun
))
2259 return cxx_eval_thunk_call (ctx
, t
, fun
, lval
, non_constant_p
, overflow_p
);
2260 if (!DECL_DECLARED_CONSTEXPR_P (fun
))
2262 if (TREE_CODE (t
) == CALL_EXPR
2263 && cxx_replaceable_global_alloc_fn (fun
)
2264 && (CALL_FROM_NEW_OR_DELETE_P (t
)
2266 && ctx
->call
->fundef
2267 && is_std_allocator_allocate (ctx
->call
->fundef
->decl
))))
2269 const int nargs
= call_expr_nargs (t
);
2270 tree arg0
= NULL_TREE
;
2271 for (int i
= 0; i
< nargs
; ++i
)
2273 tree arg
= CALL_EXPR_ARG (t
, i
);
2274 arg
= cxx_eval_constant_expression (ctx
, arg
, false,
2275 non_constant_p
, overflow_p
);
2276 VERIFY_CONSTANT (arg
);
2281 if (IDENTIFIER_NEW_OP_P (DECL_NAME (fun
)))
2283 tree type
= build_array_type_nelts (char_type_node
,
2284 tree_to_uhwi (arg0
));
2285 tree var
= build_decl (loc
, VAR_DECL
, heap_uninit_identifier
,
2287 DECL_ARTIFICIAL (var
) = 1;
2288 TREE_STATIC (var
) = 1;
2289 ctx
->global
->heap_vars
.safe_push (var
);
2290 ctx
->global
->values
.put (var
, NULL_TREE
);
2291 return fold_convert (ptr_type_node
, build_address (var
));
2296 if (TREE_CODE (arg0
) == ADDR_EXPR
2297 && VAR_P (TREE_OPERAND (arg0
, 0)))
2299 tree var
= TREE_OPERAND (arg0
, 0);
2300 if (DECL_NAME (var
) == heap_uninit_identifier
2301 || DECL_NAME (var
) == heap_identifier
)
2303 DECL_NAME (var
) = heap_deleted_identifier
;
2304 ctx
->global
->values
.remove (var
);
2305 ctx
->global
->heap_dealloc_count
++;
2308 else if (DECL_NAME (var
) == heap_deleted_identifier
)
2311 error_at (loc
, "deallocation of already deallocated "
2313 *non_constant_p
= true;
2318 error_at (loc
, "deallocation of storage that was "
2319 "not previously allocated");
2320 *non_constant_p
= true;
2324 /* Allow placement new in std::construct_at, just return the second
2326 if (TREE_CODE (t
) == CALL_EXPR
2327 && cxx_placement_new_fn (fun
)
2329 && ctx
->call
->fundef
2330 && is_std_construct_at (ctx
->call
->fundef
->decl
))
2332 const int nargs
= call_expr_nargs (t
);
2333 tree arg1
= NULL_TREE
;
2334 for (int i
= 0; i
< nargs
; ++i
)
2336 tree arg
= CALL_EXPR_ARG (t
, i
);
2337 arg
= cxx_eval_constant_expression (ctx
, arg
, false,
2338 non_constant_p
, overflow_p
);
2339 VERIFY_CONSTANT (arg
);
2346 else if (cxx_dynamic_cast_fn_p (fun
))
2347 return cxx_eval_dynamic_cast_fn (ctx
, t
, non_constant_p
, overflow_p
);
2351 if (!lambda_static_thunk_p (fun
))
2352 error_at (loc
, "call to non-%<constexpr%> function %qD", fun
);
2353 explain_invalid_constexpr_fn (fun
);
2355 *non_constant_p
= true;
2359 constexpr_ctx new_ctx
= *ctx
;
2360 if (DECL_CONSTRUCTOR_P (fun
) && !ctx
->object
2361 && TREE_CODE (t
) == AGGR_INIT_EXPR
)
2363 /* We want to have an initialization target for an AGGR_INIT_EXPR.
2364 If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
2365 new_ctx
.object
= AGGR_INIT_EXPR_SLOT (t
);
2366 tree ctor
= new_ctx
.ctor
= build_constructor (DECL_CONTEXT (fun
), NULL
);
2367 CONSTRUCTOR_NO_CLEARING (ctor
) = true;
2368 ctx
->global
->values
.put (new_ctx
.object
, ctor
);
2372 /* Shortcut trivial constructor/op=. */
2373 if (trivial_fn_p (fun
))
2375 tree init
= NULL_TREE
;
2376 if (call_expr_nargs (t
) == 2)
2377 init
= convert_from_reference (get_nth_callarg (t
, 1));
2378 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
2379 && AGGR_INIT_ZERO_FIRST (t
))
2380 init
= build_zero_init (DECL_CONTEXT (fun
), NULL_TREE
, false);
2383 tree op
= get_nth_callarg (t
, 0);
2384 if (is_dummy_object (op
))
2387 op
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (op
)), op
);
2388 tree set
= build2 (MODIFY_EXPR
, TREE_TYPE (op
), op
, init
);
2389 new_ctx
.call
= &new_call
;
2390 return cxx_eval_constant_expression (&new_ctx
, set
, lval
,
2391 non_constant_p
, overflow_p
);
2395 /* We can't defer instantiating the function any longer. */
2396 if (!DECL_INITIAL (fun
)
2397 && DECL_TEMPLOID_INSTANTIATION (fun
)
2398 && !uid_sensitive_constexpr_evaluation_p ())
2400 location_t save_loc
= input_location
;
2401 input_location
= loc
;
2403 instantiate_decl (fun
, /*defer_ok*/false, /*expl_inst*/false);
2405 input_location
= save_loc
;
2408 /* If in direct recursive call, optimize definition search. */
2409 if (ctx
&& ctx
->call
&& ctx
->call
->fundef
&& ctx
->call
->fundef
->decl
== fun
)
2410 new_call
.fundef
= ctx
->call
->fundef
;
2413 new_call
.fundef
= retrieve_constexpr_fundef (fun
);
2414 if (new_call
.fundef
== NULL
|| new_call
.fundef
->body
== NULL
2415 || new_call
.fundef
->result
== error_mark_node
2416 || fun
== current_function_decl
)
2420 /* We need to check for current_function_decl here in case we're
2421 being called during cp_fold_function, because at that point
2422 DECL_INITIAL is set properly and we have a fundef but we
2423 haven't lowered invisirefs yet (c++/70344). */
2424 if (DECL_INITIAL (fun
) == error_mark_node
2425 || fun
== current_function_decl
)
2426 error_at (loc
, "%qD called in a constant expression before its "
2427 "definition is complete", fun
);
2428 else if (DECL_INITIAL (fun
))
2430 /* The definition of fun was somehow unsuitable. But pretend
2431 that lambda static thunks don't exist. */
2432 if (!lambda_static_thunk_p (fun
))
2433 error_at (loc
, "%qD called in a constant expression", fun
);
2434 explain_invalid_constexpr_fn (fun
);
2437 error_at (loc
, "%qD used before its definition", fun
);
2439 *non_constant_p
= true;
2444 bool non_constant_args
= false;
2445 cxx_bind_parameters_in_call (ctx
, t
, &new_call
,
2446 non_constant_p
, overflow_p
, &non_constant_args
);
2448 /* We build up the bindings list before we know whether we already have this
2449 call cached. If we don't end up saving these bindings, ggc_free them when
2450 this function exits. */
2455 free_bindings (tree
&b
): bindings (&b
) { }
2456 ~free_bindings () { if (bindings
) ggc_free (*bindings
); }
2457 void preserve () { bindings
= NULL
; }
2458 } fb (new_call
.bindings
);
2460 if (*non_constant_p
)
2463 depth_ok
= push_cx_call_context (t
);
2465 /* Remember the object we are constructing. */
2466 tree new_obj
= NULL_TREE
;
2467 if (DECL_CONSTRUCTOR_P (fun
))
2469 /* In a constructor, it should be the first `this' argument.
2470 At this point it has already been evaluated in the call
2471 to cxx_bind_parameters_in_call. */
2472 new_obj
= TREE_VEC_ELT (new_call
.bindings
, 0);
2473 STRIP_NOPS (new_obj
);
2474 if (TREE_CODE (new_obj
) == ADDR_EXPR
)
2475 new_obj
= TREE_OPERAND (new_obj
, 0);
2477 if (ctx
->call
&& ctx
->call
->fundef
2478 && DECL_CONSTRUCTOR_P (ctx
->call
->fundef
->decl
))
2480 tree cur_obj
= TREE_VEC_ELT (ctx
->call
->bindings
, 0);
2481 STRIP_NOPS (cur_obj
);
2482 if (TREE_CODE (cur_obj
) == ADDR_EXPR
)
2483 cur_obj
= TREE_OPERAND (cur_obj
, 0);
2484 if (new_obj
== cur_obj
)
2485 /* We're calling the target constructor of a delegating
2486 constructor, or accessing a base subobject through a
2487 NOP_EXPR as part of a call to a base constructor, so
2488 there is no new (sub)object. */
2489 new_obj
= NULL_TREE
;
2493 tree result
= NULL_TREE
;
2495 constexpr_call
*entry
= NULL
;
2496 if (depth_ok
&& !non_constant_args
&& ctx
->strict
)
2498 new_call
.hash
= constexpr_fundef_hasher::hash (new_call
.fundef
);
2500 = iterative_hash_template_arg (new_call
.bindings
, new_call
.hash
);
2502 = iterative_hash_object (ctx
->manifestly_const_eval
, new_call
.hash
);
2504 /* If we have seen this call before, we are done. */
2505 maybe_initialize_constexpr_call_table ();
2506 constexpr_call
**slot
2507 = constexpr_call_table
->find_slot (&new_call
, INSERT
);
2511 /* Only cache up to constexpr_cache_depth to limit memory use. */
2512 if (depth_ok
< constexpr_cache_depth
)
2514 /* We need to keep a pointer to the entry, not just the slot, as
2515 the slot can move during evaluation of the body. */
2516 *slot
= entry
= ggc_alloc
<constexpr_call
> ();
2521 /* Calls that are in progress have their result set to NULL, so that we
2522 can detect circular dependencies. Now that we only cache up to
2523 constexpr_cache_depth this won't catch circular dependencies that
2524 start deeper, but they'll hit the recursion or ops limit. */
2525 else if (entry
->result
== NULL
)
2528 error ("call has circular dependency");
2529 *non_constant_p
= true;
2530 entry
->result
= result
= error_mark_node
;
2533 result
= entry
->result
;
2539 error ("%<constexpr%> evaluation depth exceeds maximum of %d (use "
2540 "%<-fconstexpr-depth=%> to increase the maximum)",
2541 max_constexpr_depth
);
2542 *non_constant_p
= true;
2543 result
= error_mark_node
;
2547 bool cacheable
= true;
2548 if (result
&& result
!= error_mark_node
)
2550 else if (!DECL_SAVED_TREE (fun
))
2552 /* When at_eof >= 2, cgraph has started throwing away
2553 DECL_SAVED_TREE, so fail quietly. FIXME we get here because of
2554 late code generation for VEC_INIT_EXPR, which needs to be
2555 completely reconsidered. */
2556 gcc_assert (at_eof
>= 2 && ctx
->quiet
);
2557 *non_constant_p
= true;
2559 else if (tree copy
= get_fundef_copy (new_call
.fundef
))
2561 tree body
, parms
, res
;
2562 releasing_vec ctors
;
2564 /* Reuse or create a new unshared copy of this function's body. */
2565 body
= TREE_PURPOSE (copy
);
2566 parms
= TREE_VALUE (copy
);
2567 res
= TREE_TYPE (copy
);
2569 /* Associate the bindings with the remapped parms. */
2570 tree bound
= new_call
.bindings
;
2571 tree remapped
= parms
;
2572 for (int i
= 0; i
< TREE_VEC_LENGTH (bound
); ++i
)
2574 tree arg
= TREE_VEC_ELT (bound
, i
);
2577 /* Unshare args going into the hash table to separate them
2578 from the caller's context, for better GC and to avoid
2579 problems with verify_gimple. */
2580 arg
= unshare_expr_without_location (arg
);
2581 TREE_VEC_ELT (bound
, i
) = arg
;
2583 /* Don't share a CONSTRUCTOR that might be changed. This is not
2584 redundant with the unshare just above; we also don't want to
2585 change the argument values in the hash table. XXX Could we
2586 unshare lazily in cxx_eval_store_expression? */
2587 arg
= unshare_constructor (arg
);
2588 if (TREE_CODE (arg
) == CONSTRUCTOR
)
2589 vec_safe_push (ctors
, arg
);
2590 ctx
->global
->values
.put (remapped
, arg
);
2591 remapped
= DECL_CHAIN (remapped
);
2593 /* Add the RESULT_DECL to the values map, too. */
2594 gcc_assert (!DECL_BY_REFERENCE (res
));
2595 ctx
->global
->values
.put (res
, NULL_TREE
);
2597 /* Track the callee's evaluated SAVE_EXPRs and TARGET_EXPRs so that
2598 we can forget their values after the call. */
2599 constexpr_ctx ctx_with_save_exprs
= *ctx
;
2600 auto_vec
<tree
, 10> save_exprs
;
2601 ctx_with_save_exprs
.save_exprs
= &save_exprs
;
2602 ctx_with_save_exprs
.call
= &new_call
;
2603 unsigned save_heap_alloc_count
= ctx
->global
->heap_vars
.length ();
2604 unsigned save_heap_dealloc_count
= ctx
->global
->heap_dealloc_count
;
2606 tree jump_target
= NULL_TREE
;
2607 cxx_eval_constant_expression (&ctx_with_save_exprs
, body
,
2608 lval
, non_constant_p
, overflow_p
,
2611 if (DECL_CONSTRUCTOR_P (fun
))
2612 /* This can be null for a subobject constructor call, in
2613 which case what we care about is the initialization
2614 side-effects rather than the value. We could get at the
2615 value by evaluating *this, but we don't bother; there's
2616 no need to put such a call in the hash table. */
2617 result
= lval
? ctx
->object
: ctx
->ctor
;
2618 else if (VOID_TYPE_P (TREE_TYPE (res
)))
2622 result
= *ctx
->global
->values
.get (res
);
2623 if (result
== NULL_TREE
&& !*non_constant_p
)
2626 error ("%<constexpr%> call flows off the end "
2628 *non_constant_p
= true;
2632 /* At this point, the object's constructor will have run, so
2633 the object is no longer under construction, and its possible
2634 'const' semantics now apply. Make a note of this fact by
2635 marking the CONSTRUCTOR TREE_READONLY. */
2637 && CLASS_TYPE_P (TREE_TYPE (new_obj
))
2638 && CP_TYPE_CONST_P (TREE_TYPE (new_obj
)))
2640 /* Subobjects might not be stored in ctx->global->values but we
2641 can get its CONSTRUCTOR by evaluating *this. */
2642 tree e
= cxx_eval_constant_expression (ctx
, new_obj
,
2646 if (TREE_CODE (e
) == CONSTRUCTOR
&& !*non_constant_p
)
2647 TREE_READONLY (e
) = true;
2650 /* Forget the saved values of the callee's SAVE_EXPRs and
2654 FOR_EACH_VEC_ELT (save_exprs
, i
, save_expr
)
2655 ctx
->global
->values
.remove (save_expr
);
2657 /* Remove the parms/result from the values map. Is it worth
2658 bothering to do this when the map itself is only live for
2659 one constexpr evaluation? If so, maybe also clear out
2660 other vars from call, maybe in BIND_EXPR handling? */
2661 ctx
->global
->values
.remove (res
);
2662 for (tree parm
= parms
; parm
; parm
= TREE_CHAIN (parm
))
2663 ctx
->global
->values
.remove (parm
);
2665 /* Free any parameter CONSTRUCTORs we aren't returning directly. */
2666 while (!ctors
->is_empty ())
2668 tree c
= ctors
->pop ();
2670 free_constructor (c
);
2673 /* Make the unshared function copy we used available for re-use. */
2674 save_fundef_copy (fun
, copy
);
2676 /* If the call allocated some heap object that hasn't been
2677 deallocated during the call, or if it deallocated some heap
2678 object it has not allocated, the call isn't really stateless
2679 for the constexpr evaluation and should not be cached.
2680 It is fine if the call allocates something and deallocates it
2683 && (save_heap_alloc_count
!= ctx
->global
->heap_vars
.length ()
2684 || (save_heap_dealloc_count
2685 != ctx
->global
->heap_dealloc_count
)))
2689 if ((ctx
->global
->heap_vars
.length ()
2690 - ctx
->global
->heap_dealloc_count
)
2691 != save_heap_alloc_count
- save_heap_dealloc_count
)
2694 FOR_EACH_VEC_ELT_FROM (ctx
->global
->heap_vars
, i
, heap_var
,
2695 save_heap_alloc_count
)
2696 if (DECL_NAME (heap_var
) != heap_deleted_identifier
)
2703 /* Rewrite all occurrences of the function's RESULT_DECL with the
2704 current object under construction. */
2705 if (!*non_constant_p
&& ctx
->object
2706 && AGGREGATE_TYPE_P (TREE_TYPE (res
))
2707 && !is_empty_class (TREE_TYPE (res
)))
2708 if (replace_result_decl (&result
, res
, ctx
->object
))
2712 /* Couldn't get a function copy to evaluate. */
2713 *non_constant_p
= true;
2715 if (result
== error_mark_node
)
2716 *non_constant_p
= true;
2717 if (*non_constant_p
|| *overflow_p
)
2718 result
= error_mark_node
;
2722 entry
->result
= cacheable
? result
: error_mark_node
;
2725 /* The result of a constexpr function must be completely initialized.
2727 However, in C++20, a constexpr constructor doesn't necessarily have
2728 to initialize all the fields, so we don't clear CONSTRUCTOR_NO_CLEARING
2729 in order to detect reading an unitialized object in constexpr instead
2730 of value-initializing it. (reduced_constant_expression_p is expected to
2731 take care of clearing the flag.) */
2732 if (TREE_CODE (result
) == CONSTRUCTOR
2733 && (cxx_dialect
< cxx20
2734 || !DECL_CONSTRUCTOR_P (fun
)))
2735 clear_no_implicit_zero (result
);
2737 pop_cx_call_context ();
2741 /* Return true if T is a valid constant initializer. If a CONSTRUCTOR
2742 initializes all the members, the CONSTRUCTOR_NO_CLEARING flag will be
2744 FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
2747 reduced_constant_expression_p (tree t
)
2752 switch (TREE_CODE (t
))
2755 /* Even if we can't lower this yet, it's constant. */
2759 /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
2760 tree idx
, val
, field
; unsigned HOST_WIDE_INT i
;
2761 if (CONSTRUCTOR_NO_CLEARING (t
))
2763 if (TREE_CODE (TREE_TYPE (t
)) == VECTOR_TYPE
)
2764 /* An initialized vector would have a VECTOR_CST. */
2766 else if (cxx_dialect
>= cxx20
2767 /* An ARRAY_TYPE doesn't have any TYPE_FIELDS. */
2768 && TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
2770 else if (cxx_dialect
>= cxx20
2771 && TREE_CODE (TREE_TYPE (t
)) == UNION_TYPE
)
2773 if (CONSTRUCTOR_NELTS (t
) == 0)
2774 /* An initialized union has a constructor element. */
2776 /* And it only initializes one member. */
2780 field
= next_initializable_field (TYPE_FIELDS (TREE_TYPE (t
)));
2784 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t
), i
, idx
, val
)
2786 /* If VAL is null, we're in the middle of initializing this
2788 if (!reduced_constant_expression_p (val
))
2790 /* Empty class field may or may not have an initializer. */
2791 for (; field
&& idx
!= field
;
2792 field
= next_initializable_field (DECL_CHAIN (field
)))
2793 if (!is_really_empty_class (TREE_TYPE (field
),
2794 /*ignore_vptr*/false))
2797 field
= next_initializable_field (DECL_CHAIN (field
));
2799 /* There could be a non-empty field at the end. */
2800 for (; field
; field
= next_initializable_field (DECL_CHAIN (field
)))
2801 if (!is_really_empty_class (TREE_TYPE (field
), /*ignore_vptr*/false))
2803 if (CONSTRUCTOR_NO_CLEARING (t
))
2804 /* All the fields are initialized. */
2805 CONSTRUCTOR_NO_CLEARING (t
) = false;
2809 /* FIXME are we calling this too much? */
2810 return initializer_constant_valid_p (t
, TREE_TYPE (t
)) != NULL_TREE
;
2814 /* Some expressions may have constant operands but are not constant
2815 themselves, such as 1/0. Call this function to check for that
2818 We only call this in places that require an arithmetic constant, not in
2819 places where we might have a non-constant expression that can be a
2820 component of a constant expression, such as the address of a constexpr
2821 variable that might be dereferenced later. */
2824 verify_constant (tree t
, bool allow_non_constant
, bool *non_constant_p
,
2827 if (!*non_constant_p
&& !reduced_constant_expression_p (t
)
2830 if (!allow_non_constant
)
2831 error ("%q+E is not a constant expression", t
);
2832 *non_constant_p
= true;
2834 if (TREE_OVERFLOW_P (t
))
2836 if (!allow_non_constant
)
2838 permerror (input_location
, "overflow in constant expression");
2839 /* If we're being permissive (and are in an enforcing
2840 context), ignore the overflow. */
2841 if (flag_permissive
)
2842 return *non_constant_p
;
2846 return *non_constant_p
;
2849 /* Check whether the shift operation with code CODE and type TYPE on LHS
2850 and RHS is undefined. If it is, give an error with an explanation,
2851 and return true; return false otherwise. */
2854 cxx_eval_check_shift_p (location_t loc
, const constexpr_ctx
*ctx
,
2855 enum tree_code code
, tree type
, tree lhs
, tree rhs
)
2857 if ((code
!= LSHIFT_EXPR
&& code
!= RSHIFT_EXPR
)
2858 || TREE_CODE (lhs
) != INTEGER_CST
2859 || TREE_CODE (rhs
) != INTEGER_CST
)
2862 tree lhstype
= TREE_TYPE (lhs
);
2863 unsigned HOST_WIDE_INT uprec
= TYPE_PRECISION (TREE_TYPE (lhs
));
2865 /* [expr.shift] The behavior is undefined if the right operand
2866 is negative, or greater than or equal to the length in bits
2867 of the promoted left operand. */
2868 if (tree_int_cst_sgn (rhs
) == -1)
2871 permerror (loc
, "right operand of shift expression %q+E is negative",
2872 build2_loc (loc
, code
, type
, lhs
, rhs
));
2873 return (!flag_permissive
|| ctx
->quiet
);
2875 if (compare_tree_int (rhs
, uprec
) >= 0)
2878 permerror (loc
, "right operand of shift expression %q+E is greater "
2879 "than or equal to the precision %wu of the left operand",
2880 build2_loc (loc
, code
, type
, lhs
, rhs
), uprec
);
2881 return (!flag_permissive
|| ctx
->quiet
);
2884 /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
2885 if E1 has a signed type and non-negative value, and E1x2^E2 is
2886 representable in the corresponding unsigned type of the result type,
2887 then that value, converted to the result type, is the resulting value;
2888 otherwise, the behavior is undefined.
2890 The value of E1 << E2 is the unique value congruent to E1 x 2^E2 modulo
2891 2^N, where N is the range exponent of the type of the result. */
2892 if (code
== LSHIFT_EXPR
2893 && !TYPE_UNSIGNED (lhstype
)
2894 && cxx_dialect
>= cxx11
2895 && cxx_dialect
< cxx20
)
2897 if (tree_int_cst_sgn (lhs
) == -1)
2901 "left operand of shift expression %q+E is negative",
2902 build2_loc (loc
, code
, type
, lhs
, rhs
));
2903 return (!flag_permissive
|| ctx
->quiet
);
2905 /* For signed x << y the following:
2906 (unsigned) x >> ((prec (lhs) - 1) - y)
2907 if > 1, is undefined. The right-hand side of this formula
2908 is the highest bit of the LHS that can be set (starting from 0),
2909 so that the shift doesn't overflow. We then right-shift the LHS
2910 to see whether any other bit is set making the original shift
2911 undefined -- the result is not representable in the corresponding
2913 tree t
= build_int_cst (unsigned_type_node
, uprec
- 1);
2914 t
= fold_build2 (MINUS_EXPR
, unsigned_type_node
, t
, rhs
);
2915 tree ulhs
= fold_convert (unsigned_type_for (lhstype
), lhs
);
2916 t
= fold_build2 (RSHIFT_EXPR
, TREE_TYPE (ulhs
), ulhs
, t
);
2917 if (tree_int_cst_lt (integer_one_node
, t
))
2920 permerror (loc
, "shift expression %q+E overflows",
2921 build2_loc (loc
, code
, type
, lhs
, rhs
));
2922 return (!flag_permissive
|| ctx
->quiet
);
2928 /* Subroutine of cxx_eval_constant_expression.
2929 Attempt to reduce the unary expression tree T to a compile time value.
2930 If successful, return the value. Otherwise issue a diagnostic
2931 and return error_mark_node. */
2934 cxx_eval_unary_expression (const constexpr_ctx
*ctx
, tree t
,
2936 bool *non_constant_p
, bool *overflow_p
)
2939 tree orig_arg
= TREE_OPERAND (t
, 0);
2940 tree arg
= cxx_eval_constant_expression (ctx
, orig_arg
, /*lval*/false,
2941 non_constant_p
, overflow_p
);
2942 VERIFY_CONSTANT (arg
);
2943 location_t loc
= EXPR_LOCATION (t
);
2944 enum tree_code code
= TREE_CODE (t
);
2945 tree type
= TREE_TYPE (t
);
2946 r
= fold_unary_loc (loc
, code
, type
, arg
);
2949 if (arg
== orig_arg
)
2952 r
= build1_loc (loc
, code
, type
, arg
);
2954 VERIFY_CONSTANT (r
);
2958 /* Helper function for cxx_eval_binary_expression. Try to optimize
2959 original POINTER_PLUS_EXPR T, LHS p+ RHS, return NULL_TREE if the
2960 generic folding should be used. */
2963 cxx_fold_pointer_plus_expression (const constexpr_ctx
*ctx
, tree t
,
2964 tree lhs
, tree rhs
, bool *non_constant_p
,
2968 if (TREE_CODE (lhs
) != ADDR_EXPR
)
2971 lhs
= TREE_OPERAND (lhs
, 0);
2973 /* &A[i] p+ j => &A[i + j] */
2974 if (TREE_CODE (lhs
) == ARRAY_REF
2975 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == INTEGER_CST
2976 && TREE_CODE (rhs
) == INTEGER_CST
2977 && TYPE_SIZE_UNIT (TREE_TYPE (lhs
))
2978 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs
))) == INTEGER_CST
)
2980 tree orig_type
= TREE_TYPE (t
);
2981 location_t loc
= EXPR_LOCATION (t
);
2982 tree type
= TREE_TYPE (lhs
);
2984 t
= fold_convert_loc (loc
, ssizetype
, TREE_OPERAND (lhs
, 1));
2985 tree nelts
= array_type_nelts_top (TREE_TYPE (TREE_OPERAND (lhs
, 0)));
2986 nelts
= cxx_eval_constant_expression (ctx
, nelts
, false, non_constant_p
,
2988 if (*non_constant_p
)
2990 /* Don't fold an out-of-bound access. */
2991 if (!tree_int_cst_le (t
, nelts
))
2993 rhs
= cp_fold_convert (ssizetype
, rhs
);
2994 /* Don't fold if rhs can't be divided exactly by TYPE_SIZE_UNIT.
2995 constexpr int A[1]; ... (char *)&A[0] + 1 */
2996 if (!integer_zerop (fold_build2_loc (loc
, TRUNC_MOD_EXPR
, sizetype
,
2997 rhs
, TYPE_SIZE_UNIT (type
))))
2999 /* Make sure to treat the second operand of POINTER_PLUS_EXPR
3001 rhs
= fold_build2_loc (loc
, EXACT_DIV_EXPR
, ssizetype
, rhs
,
3002 TYPE_SIZE_UNIT (type
));
3003 t
= size_binop_loc (loc
, PLUS_EXPR
, rhs
, t
);
3004 t
= build4_loc (loc
, ARRAY_REF
, type
, TREE_OPERAND (lhs
, 0),
3005 t
, NULL_TREE
, NULL_TREE
);
3006 t
= cp_build_addr_expr (t
, tf_warning_or_error
);
3007 t
= cp_fold_convert (orig_type
, t
);
3008 return cxx_eval_constant_expression (ctx
, t
, /*lval*/false,
3009 non_constant_p
, overflow_p
);
3015 /* Subroutine of cxx_eval_constant_expression.
3016 Like cxx_eval_unary_expression, except for binary expressions. */
3019 cxx_eval_binary_expression (const constexpr_ctx
*ctx
, tree t
,
3021 bool *non_constant_p
, bool *overflow_p
)
3024 tree orig_lhs
= TREE_OPERAND (t
, 0);
3025 tree orig_rhs
= TREE_OPERAND (t
, 1);
3027 lhs
= cxx_eval_constant_expression (ctx
, orig_lhs
, /*lval*/false,
3028 non_constant_p
, overflow_p
);
3029 /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer
3031 if (*non_constant_p
)
3033 rhs
= cxx_eval_constant_expression (ctx
, orig_rhs
, /*lval*/false,
3034 non_constant_p
, overflow_p
);
3035 if (*non_constant_p
)
3038 location_t loc
= EXPR_LOCATION (t
);
3039 enum tree_code code
= TREE_CODE (t
);
3040 tree type
= TREE_TYPE (t
);
3042 if (code
== EQ_EXPR
|| code
== NE_EXPR
)
3044 bool is_code_eq
= (code
== EQ_EXPR
);
3046 if (TREE_CODE (lhs
) == PTRMEM_CST
3047 && TREE_CODE (rhs
) == PTRMEM_CST
)
3049 tree lmem
= PTRMEM_CST_MEMBER (lhs
);
3050 tree rmem
= PTRMEM_CST_MEMBER (rhs
);
3052 if (TREE_CODE (lmem
) == TREE_CODE (rmem
)
3053 && TREE_CODE (lmem
) == FIELD_DECL
3054 && TREE_CODE (DECL_CONTEXT (lmem
)) == UNION_TYPE
3055 && same_type_p (DECL_CONTEXT (lmem
),
3056 DECL_CONTEXT (rmem
)))
3057 /* If both refer to (possibly different) members of the same union
3058 (12.3), they compare equal. */
3061 eq
= cp_tree_equal (lhs
, rhs
);
3062 r
= constant_boolean_node (eq
== is_code_eq
, type
);
3064 else if ((TREE_CODE (lhs
) == PTRMEM_CST
3065 || TREE_CODE (rhs
) == PTRMEM_CST
)
3066 && (null_member_pointer_value_p (lhs
)
3067 || null_member_pointer_value_p (rhs
)))
3068 r
= constant_boolean_node (!is_code_eq
, type
);
3069 else if (TREE_CODE (lhs
) == PTRMEM_CST
)
3070 lhs
= cplus_expand_constant (lhs
);
3071 else if (TREE_CODE (rhs
) == PTRMEM_CST
)
3072 rhs
= cplus_expand_constant (rhs
);
3074 if (code
== POINTER_PLUS_EXPR
&& !*non_constant_p
3075 && integer_zerop (lhs
) && !integer_zerop (rhs
))
3078 error ("arithmetic involving a null pointer in %qE", lhs
);
3079 *non_constant_p
= true;
3082 else if (code
== POINTER_PLUS_EXPR
)
3083 r
= cxx_fold_pointer_plus_expression (ctx
, t
, lhs
, rhs
, non_constant_p
,
3085 else if (code
== SPACESHIP_EXPR
)
3087 r
= genericize_spaceship (type
, lhs
, rhs
);
3088 return cxx_eval_constant_expression (ctx
, r
, lval
, non_constant_p
,
3093 r
= fold_binary_loc (loc
, code
, type
, lhs
, rhs
);
3097 if (lhs
== orig_lhs
&& rhs
== orig_rhs
)
3100 r
= build2_loc (loc
, code
, type
, lhs
, rhs
);
3102 else if (cxx_eval_check_shift_p (loc
, ctx
, code
, type
, lhs
, rhs
))
3103 *non_constant_p
= true;
3104 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
3105 a local array in a constexpr function. */
3106 bool ptr
= INDIRECT_TYPE_P (TREE_TYPE (lhs
));
3108 VERIFY_CONSTANT (r
);
3112 /* Subroutine of cxx_eval_constant_expression.
3113 Attempt to evaluate condition expressions. Dead branches are not
3117 cxx_eval_conditional_expression (const constexpr_ctx
*ctx
, tree t
,
3119 bool *non_constant_p
, bool *overflow_p
,
3122 tree val
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
3124 non_constant_p
, overflow_p
);
3125 VERIFY_CONSTANT (val
);
3126 /* Don't VERIFY_CONSTANT the other operands. */
3127 if (integer_zerop (val
))
3128 val
= TREE_OPERAND (t
, 2);
3130 val
= TREE_OPERAND (t
, 1);
3131 if (TREE_CODE (t
) == IF_STMT
&& !val
)
3133 return cxx_eval_constant_expression (ctx
, val
, lval
, non_constant_p
,
3134 overflow_p
, jump_target
);
3137 /* Subroutine of cxx_eval_constant_expression.
3138 Attempt to evaluate vector condition expressions. Unlike
3139 cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal
3140 ternary arithmetics operation, where all 3 arguments have to be
3141 evaluated as constants and then folding computes the result from
3145 cxx_eval_vector_conditional_expression (const constexpr_ctx
*ctx
, tree t
,
3146 bool *non_constant_p
, bool *overflow_p
)
3148 tree arg1
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
3150 non_constant_p
, overflow_p
);
3151 VERIFY_CONSTANT (arg1
);
3152 tree arg2
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
3154 non_constant_p
, overflow_p
);
3155 VERIFY_CONSTANT (arg2
);
3156 tree arg3
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 2),
3158 non_constant_p
, overflow_p
);
3159 VERIFY_CONSTANT (arg3
);
3160 location_t loc
= EXPR_LOCATION (t
);
3161 tree type
= TREE_TYPE (t
);
3162 tree r
= fold_ternary_loc (loc
, VEC_COND_EXPR
, type
, arg1
, arg2
, arg3
);
3165 if (arg1
== TREE_OPERAND (t
, 0)
3166 && arg2
== TREE_OPERAND (t
, 1)
3167 && arg3
== TREE_OPERAND (t
, 2))
3170 r
= build3_loc (loc
, VEC_COND_EXPR
, type
, arg1
, arg2
, arg3
);
3172 VERIFY_CONSTANT (r
);
3176 /* Returns less than, equal to, or greater than zero if KEY is found to be
3177 less than, to match, or to be greater than the constructor_elt's INDEX. */
3180 array_index_cmp (tree key
, tree index
)
3182 gcc_assert (TREE_CODE (key
) == INTEGER_CST
);
3184 switch (TREE_CODE (index
))
3187 return tree_int_cst_compare (key
, index
);
3190 tree lo
= TREE_OPERAND (index
, 0);
3191 tree hi
= TREE_OPERAND (index
, 1);
3192 if (tree_int_cst_lt (key
, lo
))
3194 else if (tree_int_cst_lt (hi
, key
))
3204 /* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1
3205 if none. If INSERT is true, insert a matching element rather than fail. */
3207 static HOST_WIDE_INT
3208 find_array_ctor_elt (tree ary
, tree dindex
, bool insert
)
3210 if (tree_int_cst_sgn (dindex
) < 0)
3213 unsigned HOST_WIDE_INT i
= tree_to_uhwi (dindex
);
3214 vec
<constructor_elt
, va_gc
> *elts
= CONSTRUCTOR_ELTS (ary
);
3215 unsigned HOST_WIDE_INT len
= vec_safe_length (elts
);
3217 unsigned HOST_WIDE_INT end
= len
;
3218 unsigned HOST_WIDE_INT begin
= 0;
3220 /* If the last element of the CONSTRUCTOR has its own index, we can assume
3221 that the same is true of the other elements and index directly. */
3224 tree cindex
= (*elts
)[end
- 1].index
;
3225 if (cindex
== NULL_TREE
)
3227 /* Verify that if the last index is missing, all indexes
3230 for (unsigned int j
= 0; j
< len
- 1; ++j
)
3231 gcc_assert ((*elts
)[j
].index
== NULL_TREE
);
3238 /* If the element is to be added right at the end,
3239 make sure it is added with cleared index too. */
3242 /* Otherwise, in order not to break the assumption
3243 that CONSTRUCTOR either has all indexes or none,
3244 we need to add indexes to all elements. */
3245 for (unsigned int j
= 0; j
< len
; ++j
)
3246 (*elts
)[j
].index
= build_int_cst (TREE_TYPE (dindex
), j
);
3249 else if (TREE_CODE (cindex
) == INTEGER_CST
3250 && compare_tree_int (cindex
, end
- 1) == 0)
3259 /* Otherwise, find a matching index by means of a binary search. */
3260 while (begin
!= end
)
3262 unsigned HOST_WIDE_INT middle
= (begin
+ end
) / 2;
3263 constructor_elt
&elt
= (*elts
)[middle
];
3264 tree idx
= elt
.index
;
3266 int cmp
= array_index_cmp (dindex
, idx
);
3273 if (insert
&& TREE_CODE (idx
) == RANGE_EXPR
)
3275 /* We need to split the range. */
3277 tree lo
= TREE_OPERAND (idx
, 0);
3278 tree hi
= TREE_OPERAND (idx
, 1);
3279 tree value
= elt
.value
;
3280 dindex
= fold_convert (sizetype
, dindex
);
3281 if (tree_int_cst_lt (lo
, dindex
))
3283 /* There are still some lower elts; shorten the range. */
3284 tree new_hi
= int_const_binop (MINUS_EXPR
, dindex
,
3286 if (tree_int_cst_equal (lo
, new_hi
))
3287 /* Only one element left, no longer a range. */
3290 TREE_OPERAND (idx
, 1) = new_hi
;
3291 /* Append the element we want to insert. */
3294 e
.value
= unshare_constructor (value
);
3295 vec_safe_insert (CONSTRUCTOR_ELTS (ary
), middle
, e
);
3298 /* No lower elts, the range elt is now ours. */
3301 if (tree_int_cst_lt (dindex
, hi
))
3303 /* There are still some higher elts; append a range. */
3304 tree new_lo
= int_const_binop (PLUS_EXPR
, dindex
,
3306 if (tree_int_cst_equal (new_lo
, hi
))
3309 e
.index
= build2 (RANGE_EXPR
, sizetype
, new_lo
, hi
);
3310 e
.value
= unshare_constructor (value
);
3311 vec_safe_insert (CONSTRUCTOR_ELTS (ary
), middle
+ 1, e
);
3320 constructor_elt e
= { dindex
, NULL_TREE
};
3321 vec_safe_insert (CONSTRUCTOR_ELTS (ary
), end
, e
);
3328 /* Return a pointer to the constructor_elt of CTOR which matches INDEX. If no
3329 matching constructor_elt exists, then add one to CTOR.
3331 As an optimization, if POS_HINT is non-negative then it is used as a guess
3332 for the (integer) index of the matching constructor_elt within CTOR. */
3334 static constructor_elt
*
3335 get_or_insert_ctor_field (tree ctor
, tree index
, int pos_hint
= -1)
3337 /* Check the hint first. */
3338 if (pos_hint
>= 0 && (unsigned)pos_hint
< CONSTRUCTOR_NELTS (ctor
)
3339 && CONSTRUCTOR_ELT (ctor
, pos_hint
)->index
== index
)
3340 return CONSTRUCTOR_ELT (ctor
, pos_hint
);
3342 tree type
= TREE_TYPE (ctor
);
3343 if (TREE_CODE (type
) == VECTOR_TYPE
&& index
== NULL_TREE
)
3345 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (ctor
), index
, NULL_TREE
);
3346 return &CONSTRUCTOR_ELTS (ctor
)->last();
3348 else if (TREE_CODE (type
) == ARRAY_TYPE
|| TREE_CODE (type
) == VECTOR_TYPE
)
3350 if (TREE_CODE (index
) == RANGE_EXPR
)
3352 /* Support for RANGE_EXPR index lookups is currently limited to
3353 accessing an existing element via POS_HINT, or appending a new
3354 element to the end of CTOR. ??? Support for other access
3355 patterns may also be needed. */
3356 vec
<constructor_elt
, va_gc
> *elts
= CONSTRUCTOR_ELTS (ctor
);
3357 if (vec_safe_length (elts
))
3359 tree lo
= TREE_OPERAND (index
, 0);
3360 gcc_assert (array_index_cmp (elts
->last().index
, lo
) < 0);
3362 CONSTRUCTOR_APPEND_ELT (elts
, index
, NULL_TREE
);
3363 return &elts
->last();
3366 HOST_WIDE_INT i
= find_array_ctor_elt (ctor
, index
, /*insert*/true);
3367 gcc_assert (i
>= 0);
3368 constructor_elt
*cep
= CONSTRUCTOR_ELT (ctor
, i
);
3369 gcc_assert (cep
->index
== NULL_TREE
3370 || TREE_CODE (cep
->index
) != RANGE_EXPR
);
3375 gcc_assert (TREE_CODE (index
) == FIELD_DECL
);
3377 /* We must keep the CONSTRUCTOR's ELTS in FIELD order.
3378 Usually we meet initializers in that order, but it is
3379 possible for base types to be placed not in program
3381 tree fields
= TYPE_FIELDS (DECL_CONTEXT (index
));
3382 unsigned HOST_WIDE_INT idx
= 0;
3383 constructor_elt
*cep
= NULL
;
3385 /* Check if we're changing the active member of a union. */
3386 if (TREE_CODE (type
) == UNION_TYPE
&& CONSTRUCTOR_NELTS (ctor
)
3387 && CONSTRUCTOR_ELT (ctor
, 0)->index
!= index
)
3388 vec_safe_truncate (CONSTRUCTOR_ELTS (ctor
), 0);
3389 /* If the bit offset of INDEX is larger than that of the last
3390 constructor_elt, then we can just immediately append a new
3391 constructor_elt to the end of CTOR. */
3392 else if (CONSTRUCTOR_NELTS (ctor
)
3393 && tree_int_cst_compare (bit_position (index
),
3394 bit_position (CONSTRUCTOR_ELTS (ctor
)
3395 ->last().index
)) > 0)
3397 idx
= CONSTRUCTOR_NELTS (ctor
);
3401 /* Otherwise, we need to iterate over CTOR to find or insert INDEX
3404 for (; vec_safe_iterate (CONSTRUCTOR_ELTS (ctor
), idx
, &cep
);
3405 idx
++, fields
= DECL_CHAIN (fields
))
3407 if (index
== cep
->index
)
3410 /* The field we're initializing must be on the field
3411 list. Look to see if it is present before the
3412 field the current ELT initializes. */
3413 for (; fields
!= cep
->index
; fields
= DECL_CHAIN (fields
))
3414 if (index
== fields
)
3417 /* We fell off the end of the CONSTRUCTOR, so insert a new
3418 entry at the end. */
3422 constructor_elt ce
= { index
, NULL_TREE
};
3424 vec_safe_insert (CONSTRUCTOR_ELTS (ctor
), idx
, ce
);
3425 cep
= CONSTRUCTOR_ELT (ctor
, idx
);
3433 /* Under the control of CTX, issue a detailed diagnostic for
3434 an out-of-bounds subscript INDEX into the expression ARRAY. */
3437 diag_array_subscript (location_t loc
, const constexpr_ctx
*ctx
, tree array
, tree index
)
3441 tree arraytype
= TREE_TYPE (array
);
3443 /* Convert the unsigned array subscript to a signed integer to avoid
3444 printing huge numbers for small negative values. */
3445 tree sidx
= fold_convert (ssizetype
, index
);
3446 STRIP_ANY_LOCATION_WRAPPER (array
);
3449 if (TYPE_DOMAIN (arraytype
))
3450 error_at (loc
, "array subscript value %qE is outside the bounds "
3451 "of array %qD of type %qT", sidx
, array
, arraytype
);
3453 error_at (loc
, "nonzero array subscript %qE is used with array %qD of "
3454 "type %qT with unknown bounds", sidx
, array
, arraytype
);
3455 inform (DECL_SOURCE_LOCATION (array
), "declared here");
3457 else if (TYPE_DOMAIN (arraytype
))
3458 error_at (loc
, "array subscript value %qE is outside the bounds "
3459 "of array type %qT", sidx
, arraytype
);
3461 error_at (loc
, "nonzero array subscript %qE is used with array of type %qT "
3462 "with unknown bounds", sidx
, arraytype
);
3466 /* Return the number of elements for TYPE (which is an ARRAY_TYPE or
3470 get_array_or_vector_nelts (const constexpr_ctx
*ctx
, tree type
,
3471 bool *non_constant_p
, bool *overflow_p
)
3474 if (TREE_CODE (type
) == ARRAY_TYPE
)
3476 if (TYPE_DOMAIN (type
))
3477 nelts
= array_type_nelts_top (type
);
3479 nelts
= size_zero_node
;
3481 else if (VECTOR_TYPE_P (type
))
3482 nelts
= size_int (TYPE_VECTOR_SUBPARTS (type
));
3486 /* For VLAs, the number of elements won't be an integer constant. */
3487 nelts
= cxx_eval_constant_expression (ctx
, nelts
, false,
3488 non_constant_p
, overflow_p
);
3492 /* Extract element INDEX consisting of CHARS_PER_ELT chars from
3493 STRING_CST STRING. */
3496 extract_string_elt (tree string
, unsigned chars_per_elt
, unsigned index
)
3498 tree type
= cv_unqualified (TREE_TYPE (TREE_TYPE (string
)));
3501 if (chars_per_elt
== 1)
3502 r
= build_int_cst (type
, TREE_STRING_POINTER (string
)[index
]);
3505 const unsigned char *ptr
3506 = ((const unsigned char *)TREE_STRING_POINTER (string
)
3507 + index
* chars_per_elt
);
3508 r
= native_interpret_expr (type
, ptr
, chars_per_elt
);
3513 /* Subroutine of cxx_eval_array_reference. T is an ARRAY_REF; evaluate the
3514 subscript, diagnose any problems with it, and return the result. */
3517 eval_and_check_array_index (const constexpr_ctx
*ctx
,
3518 tree t
, bool allow_one_past
,
3519 bool *non_constant_p
, bool *overflow_p
)
3521 location_t loc
= cp_expr_loc_or_input_loc (t
);
3522 tree ary
= TREE_OPERAND (t
, 0);
3523 t
= TREE_OPERAND (t
, 1);
3524 tree index
= cxx_eval_constant_expression (ctx
, t
, false,
3525 non_constant_p
, overflow_p
);
3526 VERIFY_CONSTANT (index
);
3528 if (!tree_fits_shwi_p (index
)
3529 || tree_int_cst_sgn (index
) < 0)
3531 diag_array_subscript (loc
, ctx
, ary
, index
);
3532 *non_constant_p
= true;
3536 tree nelts
= get_array_or_vector_nelts (ctx
, TREE_TYPE (ary
), non_constant_p
,
3538 VERIFY_CONSTANT (nelts
);
3540 ? !tree_int_cst_le (index
, nelts
)
3541 : !tree_int_cst_lt (index
, nelts
))
3543 diag_array_subscript (loc
, ctx
, ary
, index
);
3544 *non_constant_p
= true;
3551 /* Subroutine of cxx_eval_constant_expression.
3552 Attempt to reduce a reference to an array slot. */
3555 cxx_eval_array_reference (const constexpr_ctx
*ctx
, tree t
,
3557 bool *non_constant_p
, bool *overflow_p
)
3559 tree oldary
= TREE_OPERAND (t
, 0);
3560 tree ary
= cxx_eval_constant_expression (ctx
, oldary
,
3562 non_constant_p
, overflow_p
);
3563 if (*non_constant_p
)
3566 && TREE_CODE (ary
) == VIEW_CONVERT_EXPR
3567 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary
, 0)))
3568 && TREE_TYPE (t
) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary
, 0))))
3569 ary
= TREE_OPERAND (ary
, 0);
3571 tree oldidx
= TREE_OPERAND (t
, 1);
3572 tree index
= eval_and_check_array_index (ctx
, t
, lval
,
3573 non_constant_p
, overflow_p
);
3574 if (*non_constant_p
)
3577 if (lval
&& ary
== oldary
&& index
== oldidx
)
3580 return build4 (ARRAY_REF
, TREE_TYPE (t
), ary
, index
, NULL
, NULL
);
3582 unsigned len
= 0, elem_nchars
= 1;
3583 tree elem_type
= TREE_TYPE (TREE_TYPE (ary
));
3584 if (TREE_CODE (ary
) == CONSTRUCTOR
)
3585 len
= CONSTRUCTOR_NELTS (ary
);
3586 else if (TREE_CODE (ary
) == STRING_CST
)
3588 elem_nchars
= (TYPE_PRECISION (elem_type
)
3589 / TYPE_PRECISION (char_type_node
));
3590 len
= (unsigned) TREE_STRING_LENGTH (ary
) / elem_nchars
;
3592 else if (TREE_CODE (ary
) == VECTOR_CST
)
3593 /* We don't create variable-length VECTOR_CSTs. */
3594 len
= VECTOR_CST_NELTS (ary
).to_constant ();
3597 /* We can't do anything with other tree codes, so use
3598 VERIFY_CONSTANT to complain and fail. */
3599 VERIFY_CONSTANT (ary
);
3604 HOST_WIDE_INT i
= 0;
3605 if (TREE_CODE (ary
) == CONSTRUCTOR
)
3607 HOST_WIDE_INT ix
= find_array_ctor_elt (ary
, index
);
3614 i
= tree_to_shwi (index
);
3621 if (TREE_CODE (ary
) == CONSTRUCTOR
)
3622 r
= (*CONSTRUCTOR_ELTS (ary
))[i
].value
;
3623 else if (TREE_CODE (ary
) == VECTOR_CST
)
3624 r
= VECTOR_CST_ELT (ary
, i
);
3626 r
= extract_string_elt (ary
, elem_nchars
, i
);
3629 /* Don't VERIFY_CONSTANT here. */
3632 /* Otherwise the element doesn't have a value yet. */
3637 if (TREE_CODE (ary
) == CONSTRUCTOR
3638 && CONSTRUCTOR_NO_CLEARING (ary
))
3640 /* 'ary' is part of the aggregate initializer we're currently
3641 building; if there's no initializer for this element yet,
3644 error ("accessing uninitialized array element");
3645 *non_constant_p
= true;
3649 /* If it's within the array bounds but doesn't have an explicit
3650 initializer, it's initialized from {}. But use build_value_init
3651 directly for non-aggregates to avoid creating a garbage CONSTRUCTOR. */
3653 if (CP_AGGREGATE_TYPE_P (elem_type
))
3655 tree empty_ctor
= build_constructor (init_list_type_node
, NULL
);
3656 val
= digest_init (elem_type
, empty_ctor
, tf_warning_or_error
);
3659 val
= build_value_init (elem_type
, tf_warning_or_error
);
3660 return cxx_eval_constant_expression (ctx
, val
, lval
, non_constant_p
,
3664 /* Subroutine of cxx_eval_constant_expression.
3665 Attempt to reduce a field access of a value of class type. */
3668 cxx_eval_component_reference (const constexpr_ctx
*ctx
, tree t
,
3670 bool *non_constant_p
, bool *overflow_p
)
3672 unsigned HOST_WIDE_INT i
;
3675 tree part
= TREE_OPERAND (t
, 1);
3676 tree orig_whole
= TREE_OPERAND (t
, 0);
3677 tree whole
= cxx_eval_constant_expression (ctx
, orig_whole
,
3679 non_constant_p
, overflow_p
);
3680 if (INDIRECT_REF_P (whole
)
3681 && integer_zerop (TREE_OPERAND (whole
, 0)))
3684 error ("dereferencing a null pointer in %qE", orig_whole
);
3685 *non_constant_p
= true;
3689 if (TREE_CODE (whole
) == PTRMEM_CST
)
3690 whole
= cplus_expand_constant (whole
);
3691 if (whole
== orig_whole
)
3694 return fold_build3 (COMPONENT_REF
, TREE_TYPE (t
),
3695 whole
, part
, NULL_TREE
);
3696 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
3698 if (!*non_constant_p
&& TREE_CODE (whole
) != CONSTRUCTOR
)
3701 error ("%qE is not a constant expression", orig_whole
);
3702 *non_constant_p
= true;
3704 if (DECL_MUTABLE_P (part
))
3707 error ("mutable %qD is not usable in a constant expression", part
);
3708 *non_constant_p
= true;
3710 if (*non_constant_p
)
3712 bool pmf
= TYPE_PTRMEMFUNC_P (TREE_TYPE (whole
));
3713 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole
), i
, field
, value
)
3715 /* Use name match for PMF fields, as a variant will have a
3716 different FIELD_DECL with a different type. */
3717 if (pmf
? DECL_NAME (field
) == DECL_NAME (part
)
3722 STRIP_ANY_LOCATION_WRAPPER (value
);
3726 /* We're in the middle of initializing it. */
3730 if (TREE_CODE (TREE_TYPE (whole
)) == UNION_TYPE
3731 && CONSTRUCTOR_NELTS (whole
) > 0)
3733 /* DR 1188 says we don't have to deal with this. */
3736 constructor_elt
*cep
= CONSTRUCTOR_ELT (whole
, 0);
3737 if (cep
->value
== NULL_TREE
)
3738 error ("accessing uninitialized member %qD", part
);
3740 error ("accessing %qD member instead of initialized %qD member in "
3741 "constant expression", part
, cep
->index
);
3743 *non_constant_p
= true;
3747 /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty
3748 classes never get represented; throw together a value now. */
3749 if (is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
3750 return build_constructor (TREE_TYPE (t
), NULL
);
3752 gcc_assert (DECL_CONTEXT (part
) == TYPE_MAIN_VARIANT (TREE_TYPE (whole
)));
3754 if (CONSTRUCTOR_NO_CLEARING (whole
))
3756 /* 'whole' is part of the aggregate initializer we're currently
3757 building; if there's no initializer for this member yet, that's an
3760 error ("accessing uninitialized member %qD", part
);
3761 *non_constant_p
= true;
3765 /* If there's no explicit init for this field, it's value-initialized. */
3766 value
= build_value_init (TREE_TYPE (t
), tf_warning_or_error
);
3767 return cxx_eval_constant_expression (ctx
, value
,
3769 non_constant_p
, overflow_p
);
3772 /* Subroutine of cxx_eval_constant_expression.
3773 Attempt to reduce a field access of a value of class type that is
3774 expressed as a BIT_FIELD_REF. */
3777 cxx_eval_bit_field_ref (const constexpr_ctx
*ctx
, tree t
,
3779 bool *non_constant_p
, bool *overflow_p
)
3781 tree orig_whole
= TREE_OPERAND (t
, 0);
3782 tree retval
, fldval
, utype
, mask
;
3783 bool fld_seen
= false;
3784 HOST_WIDE_INT istart
, isize
;
3785 tree whole
= cxx_eval_constant_expression (ctx
, orig_whole
,
3787 non_constant_p
, overflow_p
);
3788 tree start
, field
, value
;
3789 unsigned HOST_WIDE_INT i
;
3791 if (whole
== orig_whole
)
3793 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
3795 if (!*non_constant_p
3796 && TREE_CODE (whole
) != VECTOR_CST
3797 && TREE_CODE (whole
) != CONSTRUCTOR
)
3800 error ("%qE is not a constant expression", orig_whole
);
3801 *non_constant_p
= true;
3803 if (*non_constant_p
)
3806 if (TREE_CODE (whole
) == VECTOR_CST
)
3807 return fold_ternary (BIT_FIELD_REF
, TREE_TYPE (t
), whole
,
3808 TREE_OPERAND (t
, 1), TREE_OPERAND (t
, 2));
3810 start
= TREE_OPERAND (t
, 2);
3811 istart
= tree_to_shwi (start
);
3812 isize
= tree_to_shwi (TREE_OPERAND (t
, 1));
3813 utype
= TREE_TYPE (t
);
3814 if (!TYPE_UNSIGNED (utype
))
3815 utype
= build_nonstandard_integer_type (TYPE_PRECISION (utype
), 1);
3816 retval
= build_int_cst (utype
, 0);
3817 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole
), i
, field
, value
)
3819 tree bitpos
= bit_position (field
);
3820 STRIP_ANY_LOCATION_WRAPPER (value
);
3821 if (bitpos
== start
&& DECL_SIZE (field
) == TREE_OPERAND (t
, 1))
3823 if (TREE_CODE (TREE_TYPE (field
)) == INTEGER_TYPE
3824 && TREE_CODE (value
) == INTEGER_CST
3825 && tree_fits_shwi_p (bitpos
)
3826 && tree_fits_shwi_p (DECL_SIZE (field
)))
3828 HOST_WIDE_INT bit
= tree_to_shwi (bitpos
);
3829 HOST_WIDE_INT sz
= tree_to_shwi (DECL_SIZE (field
));
3830 HOST_WIDE_INT shift
;
3831 if (bit
>= istart
&& bit
+ sz
<= istart
+ isize
)
3833 fldval
= fold_convert (utype
, value
);
3834 mask
= build_int_cst_type (utype
, -1);
3835 mask
= fold_build2 (LSHIFT_EXPR
, utype
, mask
,
3836 size_int (TYPE_PRECISION (utype
) - sz
));
3837 mask
= fold_build2 (RSHIFT_EXPR
, utype
, mask
,
3838 size_int (TYPE_PRECISION (utype
) - sz
));
3839 fldval
= fold_build2 (BIT_AND_EXPR
, utype
, fldval
, mask
);
3840 shift
= bit
- istart
;
3841 if (BYTES_BIG_ENDIAN
)
3842 shift
= TYPE_PRECISION (utype
) - shift
- sz
;
3843 fldval
= fold_build2 (LSHIFT_EXPR
, utype
, fldval
,
3845 retval
= fold_build2 (BIT_IOR_EXPR
, utype
, retval
, fldval
);
3851 return fold_convert (TREE_TYPE (t
), retval
);
3853 return error_mark_node
;
3856 /* Subroutine of cxx_eval_constant_expression.
3857 Evaluate a short-circuited logical expression T in the context
3858 of a given constexpr CALL. BAILOUT_VALUE is the value for
3859 early return. CONTINUE_VALUE is used here purely for
3860 sanity check purposes. */
3863 cxx_eval_logical_expression (const constexpr_ctx
*ctx
, tree t
,
3864 tree bailout_value
, tree continue_value
,
3866 bool *non_constant_p
, bool *overflow_p
)
3869 tree lhs
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
3871 non_constant_p
, overflow_p
);
3872 VERIFY_CONSTANT (lhs
);
3873 if (tree_int_cst_equal (lhs
, bailout_value
))
3875 gcc_assert (tree_int_cst_equal (lhs
, continue_value
));
3876 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
3877 lval
, non_constant_p
,
3879 VERIFY_CONSTANT (r
);
3883 /* REF is a COMPONENT_REF designating a particular field. V is a vector of
3884 CONSTRUCTOR elements to initialize (part of) an object containing that
3885 field. Return a pointer to the constructor_elt corresponding to the
3886 initialization of the field. */
3888 static constructor_elt
*
3889 base_field_constructor_elt (vec
<constructor_elt
, va_gc
> *v
, tree ref
)
3891 tree aggr
= TREE_OPERAND (ref
, 0);
3892 tree field
= TREE_OPERAND (ref
, 1);
3894 constructor_elt
*ce
;
3896 gcc_assert (TREE_CODE (ref
) == COMPONENT_REF
);
3898 if (TREE_CODE (aggr
) == COMPONENT_REF
)
3900 constructor_elt
*base_ce
3901 = base_field_constructor_elt (v
, aggr
);
3902 v
= CONSTRUCTOR_ELTS (base_ce
->value
);
3905 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
3906 if (ce
->index
== field
)
3913 /* Some of the expressions fed to the constexpr mechanism are calls to
3914 constructors, which have type void. In that case, return the type being
3915 initialized by the constructor. */
3918 initialized_type (tree t
)
3922 tree type
= TREE_TYPE (t
);
3923 if (TREE_CODE (t
) == CALL_EXPR
)
3925 /* A constructor call has void type, so we need to look deeper. */
3926 tree fn
= get_function_named_in_call (t
);
3927 if (fn
&& TREE_CODE (fn
) == FUNCTION_DECL
3928 && DECL_CXX_CONSTRUCTOR_P (fn
))
3929 type
= DECL_CONTEXT (fn
);
3931 else if (TREE_CODE (t
) == COMPOUND_EXPR
)
3932 return initialized_type (TREE_OPERAND (t
, 1));
3933 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
)
3934 type
= TREE_TYPE (AGGR_INIT_EXPR_SLOT (t
));
3935 return cv_unqualified (type
);
3938 /* We're about to initialize element INDEX of an array or class from VALUE.
3939 Set up NEW_CTX appropriately by adjusting .object to refer to the
3940 subobject and creating a new CONSTRUCTOR if the element is itself
3941 a class or array. */
3944 init_subob_ctx (const constexpr_ctx
*ctx
, constexpr_ctx
&new_ctx
,
3945 tree index
, tree
&value
)
3949 if (index
&& TREE_CODE (index
) != INTEGER_CST
3950 && TREE_CODE (index
) != FIELD_DECL
)
3951 /* This won't have an element in the new CONSTRUCTOR. */
3954 tree type
= initialized_type (value
);
3955 if (!AGGREGATE_TYPE_P (type
) && !VECTOR_TYPE_P (type
))
3956 /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
3959 /* The sub-aggregate initializer might contain a placeholder;
3960 update object to refer to the subobject and ctor to refer to
3961 the (newly created) sub-initializer. */
3963 new_ctx
.object
= build_ctor_subob_ref (index
, type
, ctx
->object
);
3964 tree elt
= build_constructor (type
, NULL
);
3965 CONSTRUCTOR_NO_CLEARING (elt
) = true;
3968 if (TREE_CODE (value
) == TARGET_EXPR
)
3969 /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
3970 value
= TARGET_EXPR_INITIAL (value
);
3973 /* We're about to process an initializer for a class or array TYPE. Make
3974 sure that CTX is set up appropriately. */
3977 verify_ctor_sanity (const constexpr_ctx
*ctx
, tree type
)
3979 /* We don't bother building a ctor for an empty base subobject. */
3980 if (is_empty_class (type
))
3983 /* We're in the middle of an initializer that might involve placeholders;
3984 our caller should have created a CONSTRUCTOR for us to put the
3985 initializer into. We will either return that constructor or T. */
3986 gcc_assert (ctx
->ctor
);
3987 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3988 (type
, TREE_TYPE (ctx
->ctor
)));
3989 /* We used to check that ctx->ctor was empty, but that isn't the case when
3990 the object is zero-initialized before calling the constructor. */
3993 tree otype
= TREE_TYPE (ctx
->object
);
3994 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type
, otype
)
3995 /* Handle flexible array members. */
3996 || (TREE_CODE (otype
) == ARRAY_TYPE
3997 && TYPE_DOMAIN (otype
) == NULL_TREE
3998 && TREE_CODE (type
) == ARRAY_TYPE
3999 && (same_type_ignoring_top_level_qualifiers_p
4000 (TREE_TYPE (type
), TREE_TYPE (otype
)))));
4002 gcc_assert (!ctx
->object
|| !DECL_P (ctx
->object
)
4003 || *(ctx
->global
->values
.get (ctx
->object
)) == ctx
->ctor
);
4006 /* Subroutine of cxx_eval_constant_expression.
4007 The expression tree T denotes a C-style array or a C-style
4008 aggregate. Reduce it to a constant expression. */
4011 cxx_eval_bare_aggregate (const constexpr_ctx
*ctx
, tree t
,
4013 bool *non_constant_p
, bool *overflow_p
)
4015 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (t
);
4016 bool changed
= false;
4017 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t
));
4018 tree type
= TREE_TYPE (t
);
4020 constexpr_ctx new_ctx
;
4021 if (TYPE_PTRMEMFUNC_P (type
) || VECTOR_TYPE_P (type
))
4023 /* We don't really need the ctx->ctor business for a PMF or
4024 vector, but it's simpler to use the same code. */
4026 new_ctx
.ctor
= build_constructor (type
, NULL
);
4027 new_ctx
.object
= NULL_TREE
;
4030 verify_ctor_sanity (ctx
, type
);
4031 vec
<constructor_elt
, va_gc
> **p
= &CONSTRUCTOR_ELTS (ctx
->ctor
);
4032 vec_alloc (*p
, vec_safe_length (v
));
4034 if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (t
))
4035 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx
->ctor
) = 1;
4039 bool constant_p
= true;
4040 bool side_effects_p
= false;
4041 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, index
, value
)
4043 tree orig_value
= value
;
4044 init_subob_ctx (ctx
, new_ctx
, index
, value
);
4046 if (new_ctx
.ctor
!= ctx
->ctor
)
4048 /* If we built a new CONSTRUCTOR, attach it now so that other
4049 initializers can refer to it. */
4050 constructor_elt
*cep
= get_or_insert_ctor_field (ctx
->ctor
, index
);
4051 cep
->value
= new_ctx
.ctor
;
4052 pos_hint
= cep
- (*p
)->begin();
4054 else if (TREE_CODE (type
) == UNION_TYPE
)
4055 /* Otherwise if we're constructing a non-aggregate union member, set
4056 the active union member now so that we can later detect and diagnose
4057 if its initializer attempts to activate another member. */
4058 get_or_insert_ctor_field (ctx
->ctor
, index
);
4059 tree elt
= cxx_eval_constant_expression (&new_ctx
, value
,
4061 non_constant_p
, overflow_p
);
4062 /* Don't VERIFY_CONSTANT here. */
4063 if (ctx
->quiet
&& *non_constant_p
)
4065 if (elt
!= orig_value
)
4068 if (!TREE_CONSTANT (elt
))
4070 if (TREE_SIDE_EFFECTS (elt
))
4071 side_effects_p
= true;
4072 if (index
&& TREE_CODE (index
) == COMPONENT_REF
)
4074 /* This is an initialization of a vfield inside a base
4075 subaggregate that we already initialized; push this
4076 initialization into the previous initialization. */
4077 constructor_elt
*inner
= base_field_constructor_elt (*p
, index
);
4082 && (TREE_CODE (index
) == NOP_EXPR
4083 || TREE_CODE (index
) == POINTER_PLUS_EXPR
))
4085 /* This is an initializer for an empty base; now that we've
4086 checked that it's constant, we can ignore it. */
4087 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index
))));
4092 if (TREE_CODE (type
) == UNION_TYPE
4093 && (*p
)->last().index
!= index
)
4094 /* The initializer erroneously changed the active union member that
4095 we're initializing. */
4096 gcc_assert (*non_constant_p
);
4099 /* The initializer might have mutated the underlying CONSTRUCTOR,
4100 so recompute the location of the target constructer_elt. */
4101 constructor_elt
*cep
4102 = get_or_insert_ctor_field (ctx
->ctor
, index
, pos_hint
);
4106 /* Adding or replacing an element might change the ctor's flags. */
4107 TREE_CONSTANT (ctx
->ctor
) = constant_p
;
4108 TREE_SIDE_EFFECTS (ctx
->ctor
) = side_effects_p
;
4111 if (*non_constant_p
|| !changed
)
4114 /* We're done building this CONSTRUCTOR, so now we can interpret an
4115 element without an explicit initializer as value-initialized. */
4116 CONSTRUCTOR_NO_CLEARING (t
) = false;
4117 TREE_CONSTANT (t
) = constant_p
;
4118 TREE_SIDE_EFFECTS (t
) = side_effects_p
;
4119 if (VECTOR_TYPE_P (type
))
4124 /* Subroutine of cxx_eval_constant_expression.
4125 The expression tree T is a VEC_INIT_EXPR which denotes the desired
4126 initialization of a non-static data member of array type. Reduce it to a
4129 Note that apart from value-initialization (when VALUE_INIT is true),
4130 this is only intended to support value-initialization and the
4131 initializations done by defaulted constructors for classes with
4132 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
4133 will either be NULL_TREE for the default constructor, or a COMPONENT_REF
4134 for the copy/move constructor. */
4137 cxx_eval_vec_init_1 (const constexpr_ctx
*ctx
, tree atype
, tree init
,
4138 bool value_init
, bool lval
,
4139 bool *non_constant_p
, bool *overflow_p
)
4141 tree elttype
= TREE_TYPE (atype
);
4142 verify_ctor_sanity (ctx
, atype
);
4143 vec
<constructor_elt
, va_gc
> **p
= &CONSTRUCTOR_ELTS (ctx
->ctor
);
4144 bool pre_init
= false;
4145 unsigned HOST_WIDE_INT i
;
4146 tsubst_flags_t complain
= ctx
->quiet
? tf_none
: tf_warning_or_error
;
4148 if (init
&& TREE_CODE (init
) == CONSTRUCTOR
)
4149 return cxx_eval_bare_aggregate (ctx
, init
, lval
,
4150 non_constant_p
, overflow_p
);
4152 /* For the default constructor, build up a call to the default
4153 constructor of the element type. We only need to handle class types
4154 here, as for a constructor to be constexpr, all members must be
4155 initialized, which for a defaulted default constructor means they must
4156 be of a class type with a constexpr default constructor. */
4157 if (TREE_CODE (elttype
) == ARRAY_TYPE
)
4158 /* We only do this at the lowest level. */;
4159 else if (value_init
)
4161 init
= build_value_init (elttype
, complain
);
4166 releasing_vec argvec
;
4167 init
= build_special_member_call (NULL_TREE
, complete_ctor_identifier
,
4168 &argvec
, elttype
, LOOKUP_NORMAL
,
4170 init
= build_aggr_init_expr (elttype
, init
);
4174 bool zeroed_out
= false;
4175 if (!CONSTRUCTOR_NO_CLEARING (ctx
->ctor
))
4177 /* We're initializing an array object that had been zero-initialized
4178 earlier. Truncate ctx->ctor, and propagate its zeroed state by
4179 clearing CONSTRUCTOR_NO_CLEARING on each of the aggregate element
4180 initializers we append to it. */
4181 gcc_checking_assert (initializer_zerop (ctx
->ctor
));
4183 vec_safe_truncate (*p
, 0);
4186 tree nelts
= get_array_or_vector_nelts (ctx
, atype
, non_constant_p
,
4188 unsigned HOST_WIDE_INT max
= tree_to_uhwi (nelts
);
4189 for (i
= 0; i
< max
; ++i
)
4191 tree idx
= build_int_cst (size_type_node
, i
);
4194 constexpr_ctx new_ctx
;
4195 init_subob_ctx (ctx
, new_ctx
, idx
, pre_init
? init
: elttype
);
4196 if (new_ctx
.ctor
!= ctx
->ctor
)
4199 CONSTRUCTOR_NO_CLEARING (new_ctx
.ctor
) = false;
4200 CONSTRUCTOR_APPEND_ELT (*p
, idx
, new_ctx
.ctor
);
4202 if (TREE_CODE (elttype
) == ARRAY_TYPE
)
4204 /* A multidimensional array; recurse. */
4205 if (value_init
|| init
== NULL_TREE
)
4207 eltinit
= NULL_TREE
;
4211 eltinit
= cp_build_array_ref (input_location
, init
, idx
, complain
);
4212 eltinit
= cxx_eval_vec_init_1 (&new_ctx
, elttype
, eltinit
, value_init
,
4214 non_constant_p
, overflow_p
);
4218 /* Initializing an element using value or default initialization
4219 we just pre-built above. */
4220 if (init
== void_node
)
4221 /* Trivial default-init, don't do anything to the CONSTRUCTOR. */
4223 eltinit
= cxx_eval_constant_expression (&new_ctx
, init
, lval
,
4224 non_constant_p
, overflow_p
);
4229 /* Copying an element. */
4230 gcc_assert (same_type_ignoring_top_level_qualifiers_p
4231 (atype
, TREE_TYPE (init
)));
4232 eltinit
= cp_build_array_ref (input_location
, init
, idx
, complain
);
4233 if (!lvalue_p (init
))
4234 eltinit
= move (eltinit
);
4235 eltinit
= force_rvalue (eltinit
, complain
);
4236 eltinit
= cxx_eval_constant_expression (&new_ctx
, eltinit
, lval
,
4237 non_constant_p
, overflow_p
);
4239 if (*non_constant_p
&& !ctx
->quiet
)
4241 if (new_ctx
.ctor
!= ctx
->ctor
)
4243 /* We appended this element above; update the value. */
4244 gcc_assert ((*p
)->last().index
== idx
);
4245 (*p
)->last().value
= eltinit
;
4248 CONSTRUCTOR_APPEND_ELT (*p
, idx
, eltinit
);
4249 /* Reuse the result of cxx_eval_constant_expression call
4250 from the first iteration to all others if it is a constant
4251 initializer that doesn't require relocations. */
4254 && (eltinit
== NULL_TREE
4255 || (initializer_constant_valid_p (eltinit
, TREE_TYPE (eltinit
))
4256 == null_pointer_node
)))
4258 if (new_ctx
.ctor
!= ctx
->ctor
)
4259 eltinit
= new_ctx
.ctor
;
4260 tree range
= build2 (RANGE_EXPR
, size_type_node
,
4261 build_int_cst (size_type_node
, 1),
4262 build_int_cst (size_type_node
, max
- 1));
4263 CONSTRUCTOR_APPEND_ELT (*p
, range
, unshare_constructor (eltinit
));
4267 vec_safe_reserve (*p
, max
);
4270 if (!*non_constant_p
)
4273 CONSTRUCTOR_NO_CLEARING (init
) = false;
4279 cxx_eval_vec_init (const constexpr_ctx
*ctx
, tree t
,
4281 bool *non_constant_p
, bool *overflow_p
)
4283 tree atype
= TREE_TYPE (t
);
4284 tree init
= VEC_INIT_EXPR_INIT (t
);
4285 tree r
= cxx_eval_vec_init_1 (ctx
, atype
, init
,
4286 VEC_INIT_EXPR_VALUE_INIT (t
),
4287 lval
, non_constant_p
, overflow_p
);
4288 if (*non_constant_p
)
4294 /* Like same_type_ignoring_top_level_qualifiers_p, but also handle the case
4295 where the desired type is an array of unknown bounds because the variable
4296 has had its bounds deduced since the wrapping expression was created. */
4299 same_type_ignoring_tlq_and_bounds_p (tree type1
, tree type2
)
4301 while (TREE_CODE (type1
) == ARRAY_TYPE
4302 && TREE_CODE (type2
) == ARRAY_TYPE
4303 && (!TYPE_DOMAIN (type1
) || !TYPE_DOMAIN (type2
)))
4305 type1
= TREE_TYPE (type1
);
4306 type2
= TREE_TYPE (type2
);
4308 return same_type_ignoring_top_level_qualifiers_p (type1
, type2
);
4311 /* Helper function for cxx_fold_indirect_ref_1, called recursively. */
4314 cxx_fold_indirect_ref_1 (location_t loc
, tree type
, tree op
,
4315 unsigned HOST_WIDE_INT off
, bool *empty_base
)
4317 tree optype
= TREE_TYPE (op
);
4318 unsigned HOST_WIDE_INT const_nunits
;
4321 if (similar_type_p (optype
, type
))
4323 /* Also handle conversion to an empty base class, which
4324 is represented with a NOP_EXPR. */
4325 /* *(foo *)&complexfoo => __real__ complexfoo */
4326 else if (TREE_CODE (optype
) == COMPLEX_TYPE
4327 && similar_type_p (type
, TREE_TYPE (optype
)))
4328 return build1_loc (loc
, REALPART_EXPR
, type
, op
);
4330 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
4331 else if (TREE_CODE (optype
) == COMPLEX_TYPE
4332 && similar_type_p (type
, TREE_TYPE (optype
))
4333 && tree_to_uhwi (TYPE_SIZE_UNIT (type
)) == off
)
4334 return build1_loc (loc
, IMAGPART_EXPR
, type
, op
);
4335 if (is_empty_class (type
)
4336 && CLASS_TYPE_P (optype
)
4337 && DERIVED_FROM_P (type
, optype
))
4342 /* ((foo*)&vectorfoo)[x] => BIT_FIELD_REF<vectorfoo,...> */
4343 else if (VECTOR_TYPE_P (optype
)
4344 && similar_type_p (type
, TREE_TYPE (optype
))
4345 && TYPE_VECTOR_SUBPARTS (optype
).is_constant (&const_nunits
))
4347 unsigned HOST_WIDE_INT part_width
= tree_to_uhwi (TYPE_SIZE_UNIT (type
));
4348 unsigned HOST_WIDE_INT max_offset
= part_width
* const_nunits
;
4349 if (off
< max_offset
&& off
% part_width
== 0)
4351 tree index
= bitsize_int (off
* BITS_PER_UNIT
);
4352 return build3_loc (loc
, BIT_FIELD_REF
, type
, op
,
4353 TYPE_SIZE (type
), index
);
4356 /* ((foo *)&fooarray)[x] => fooarray[x] */
4357 else if (TREE_CODE (optype
) == ARRAY_TYPE
4358 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (optype
)))
4359 && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (optype
))))
4361 tree type_domain
= TYPE_DOMAIN (optype
);
4362 tree min_val
= size_zero_node
;
4363 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
4364 min_val
= TYPE_MIN_VALUE (type_domain
);
4365 unsigned HOST_WIDE_INT el_sz
4366 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (optype
)));
4367 unsigned HOST_WIDE_INT idx
= off
/ el_sz
;
4368 unsigned HOST_WIDE_INT rem
= off
% el_sz
;
4369 if (tree_fits_uhwi_p (min_val
))
4371 tree index
= size_int (idx
+ tree_to_uhwi (min_val
));
4372 op
= build4_loc (loc
, ARRAY_REF
, TREE_TYPE (optype
), op
, index
,
4373 NULL_TREE
, NULL_TREE
);
4374 return cxx_fold_indirect_ref_1 (loc
, type
, op
, rem
,
4378 /* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */
4379 else if (TREE_CODE (optype
) == RECORD_TYPE
)
4381 for (tree field
= TYPE_FIELDS (optype
);
4382 field
; field
= DECL_CHAIN (field
))
4383 if (TREE_CODE (field
) == FIELD_DECL
4384 && TREE_TYPE (field
) != error_mark_node
4385 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (field
))))
4387 tree pos
= byte_position (field
);
4388 if (!tree_fits_uhwi_p (pos
))
4390 unsigned HOST_WIDE_INT upos
= tree_to_uhwi (pos
);
4392 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field
)));
4393 if (upos
<= off
&& off
< upos
+ el_sz
)
4395 tree cop
= build3 (COMPONENT_REF
, TREE_TYPE (field
),
4396 op
, field
, NULL_TREE
);
4397 if (tree ret
= cxx_fold_indirect_ref_1 (loc
, type
, cop
,
4408 /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
4409 match. We want to be less strict for simple *& folding; if we have a
4410 non-const temporary that we access through a const pointer, that should
4411 work. We handle this here rather than change fold_indirect_ref_1
4412 because we're dealing with things like ADDR_EXPR of INTEGER_CST which
4413 don't really make sense outside of constant expression evaluation. Also
4414 we want to allow folding to COMPONENT_REF, which could cause trouble
4415 with TBAA in fold_indirect_ref_1. */
4418 cxx_fold_indirect_ref (location_t loc
, tree type
, tree op0
, bool *empty_base
)
4422 poly_uint64 const_op01
;
4424 /* STRIP_NOPS, but stop if REINTERPRET_CAST_P. */
4425 while (CONVERT_EXPR_P (sub
) || TREE_CODE (sub
) == NON_LVALUE_EXPR
4426 || TREE_CODE (sub
) == VIEW_CONVERT_EXPR
)
4428 if (TREE_CODE (sub
) == NOP_EXPR
4429 && REINTERPRET_CAST_P (sub
))
4431 sub
= TREE_OPERAND (sub
, 0);
4434 subtype
= TREE_TYPE (sub
);
4435 if (!INDIRECT_TYPE_P (subtype
))
4438 if (TREE_CODE (sub
) == ADDR_EXPR
)
4440 tree op
= TREE_OPERAND (sub
, 0);
4441 tree optype
= TREE_TYPE (op
);
4443 /* *&CONST_DECL -> to the value of the const decl. */
4444 if (TREE_CODE (op
) == CONST_DECL
)
4445 return DECL_INITIAL (op
);
4446 /* *&p => p; make sure to handle *&"str"[cst] here. */
4447 if (similar_type_p (optype
, type
))
4449 tree fop
= fold_read_from_constant_string (op
);
4456 return cxx_fold_indirect_ref_1 (loc
, type
, op
, 0, empty_base
);
4458 else if (TREE_CODE (sub
) == POINTER_PLUS_EXPR
4459 && tree_fits_uhwi_p (TREE_OPERAND (sub
, 1)))
4461 tree op00
= TREE_OPERAND (sub
, 0);
4462 tree op01
= TREE_OPERAND (sub
, 1);
4465 if (TREE_CODE (op00
) == ADDR_EXPR
)
4466 return cxx_fold_indirect_ref_1 (loc
, type
, TREE_OPERAND (op00
, 0),
4467 tree_to_uhwi (op01
), empty_base
);
4469 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
4470 else if (TREE_CODE (TREE_TYPE (subtype
)) == ARRAY_TYPE
4471 && similar_type_p (type
, TREE_TYPE (TREE_TYPE (subtype
))))
4474 tree min_val
= size_zero_node
;
4476 = cxx_fold_indirect_ref (loc
, TREE_TYPE (subtype
), sub
, NULL
);
4480 sub
= build1_loc (loc
, INDIRECT_REF
, TREE_TYPE (subtype
), sub
);
4481 type_domain
= TYPE_DOMAIN (TREE_TYPE (sub
));
4482 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
4483 min_val
= TYPE_MIN_VALUE (type_domain
);
4484 return build4_loc (loc
, ARRAY_REF
, type
, sub
, min_val
, NULL_TREE
,
4492 cxx_eval_indirect_ref (const constexpr_ctx
*ctx
, tree t
,
4494 bool *non_constant_p
, bool *overflow_p
)
4496 tree orig_op0
= TREE_OPERAND (t
, 0);
4497 bool empty_base
= false;
4499 /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second
4500 operand is an integer-zero. Otherwise reject the MEM_REF for now. */
4502 if (TREE_CODE (t
) == MEM_REF
4503 && (!TREE_OPERAND (t
, 1) || !integer_zerop (TREE_OPERAND (t
, 1))))
4505 gcc_assert (ctx
->quiet
);
4506 *non_constant_p
= true;
4510 /* First try to simplify it directly. */
4511 tree r
= cxx_fold_indirect_ref (EXPR_LOCATION (t
), TREE_TYPE (t
), orig_op0
,
4515 /* If that didn't work, evaluate the operand first. */
4516 tree op0
= cxx_eval_constant_expression (ctx
, orig_op0
,
4517 /*lval*/false, non_constant_p
,
4519 /* Don't VERIFY_CONSTANT here. */
4520 if (*non_constant_p
)
4523 if (!lval
&& integer_zerop (op0
))
4526 error ("dereferencing a null pointer");
4527 *non_constant_p
= true;
4531 r
= cxx_fold_indirect_ref (EXPR_LOCATION (t
), TREE_TYPE (t
), op0
,
4535 /* We couldn't fold to a constant value. Make sure it's not
4536 something we should have been able to fold. */
4539 if (TREE_CODE (sub
) == ADDR_EXPR
)
4541 gcc_assert (!similar_type_p
4542 (TREE_TYPE (TREE_TYPE (sub
)), TREE_TYPE (t
)));
4543 /* DR 1188 says we don't have to deal with this. */
4545 error_at (cp_expr_loc_or_input_loc (t
),
4546 "accessing value of %qE through a %qT glvalue in a "
4547 "constant expression", build_fold_indirect_ref (sub
),
4549 *non_constant_p
= true;
4553 if (lval
&& op0
!= orig_op0
)
4554 return build1 (INDIRECT_REF
, TREE_TYPE (t
), op0
);
4556 VERIFY_CONSTANT (t
);
4561 r
= cxx_eval_constant_expression (ctx
, r
,
4562 lval
, non_constant_p
, overflow_p
);
4563 if (*non_constant_p
)
4566 /* If we're pulling out the value of an empty base, just return an empty
4568 if (empty_base
&& !lval
)
4570 r
= build_constructor (TREE_TYPE (t
), NULL
);
4571 TREE_CONSTANT (r
) = true;
4577 /* Complain about R, a VAR_DECL, not being usable in a constant expression.
4578 Shared between potential_constant_expression and
4579 cxx_eval_constant_expression. */
4582 non_const_var_error (location_t loc
, tree r
)
4584 auto_diagnostic_group d
;
4585 tree type
= TREE_TYPE (r
);
4586 if (DECL_NAME (r
) == heap_uninit_identifier
4587 || DECL_NAME (r
) == heap_identifier
)
4589 error_at (loc
, "the content of uninitialized storage is not usable "
4590 "in a constant expression");
4591 inform (DECL_SOURCE_LOCATION (r
), "allocated here");
4594 if (DECL_NAME (r
) == heap_deleted_identifier
)
4596 error_at (loc
, "use of allocated storage after deallocation in a "
4597 "constant expression");
4598 inform (DECL_SOURCE_LOCATION (r
), "allocated here");
4601 error_at (loc
, "the value of %qD is not usable in a constant "
4603 /* Avoid error cascade. */
4604 if (DECL_INITIAL (r
) == error_mark_node
)
4606 if (DECL_DECLARED_CONSTEXPR_P (r
))
4607 inform (DECL_SOURCE_LOCATION (r
),
4608 "%qD used in its own initializer", r
);
4609 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
4611 if (!CP_TYPE_CONST_P (type
))
4612 inform (DECL_SOURCE_LOCATION (r
),
4613 "%q#D is not const", r
);
4614 else if (CP_TYPE_VOLATILE_P (type
))
4615 inform (DECL_SOURCE_LOCATION (r
),
4616 "%q#D is volatile", r
);
4617 else if (!DECL_INITIAL (r
)
4618 || !TREE_CONSTANT (DECL_INITIAL (r
))
4619 || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
))
4620 inform (DECL_SOURCE_LOCATION (r
),
4621 "%qD was not initialized with a constant "
4626 else if (TYPE_REF_P (type
))
4627 inform (DECL_SOURCE_LOCATION (r
),
4628 "%qD was not initialized with a constant "
4632 if (cxx_dialect
>= cxx11
&& !DECL_DECLARED_CONSTEXPR_P (r
))
4633 inform (DECL_SOURCE_LOCATION (r
),
4634 "%qD was not declared %<constexpr%>", r
);
4636 inform (DECL_SOURCE_LOCATION (r
),
4637 "%qD does not have integral or enumeration type",
4642 /* Subroutine of cxx_eval_constant_expression.
4643 Like cxx_eval_unary_expression, except for trinary expressions. */
4646 cxx_eval_trinary_expression (const constexpr_ctx
*ctx
, tree t
,
4648 bool *non_constant_p
, bool *overflow_p
)
4654 for (i
= 0; i
< 3; i
++)
4656 args
[i
] = cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, i
),
4658 non_constant_p
, overflow_p
);
4659 VERIFY_CONSTANT (args
[i
]);
4662 val
= fold_ternary_loc (EXPR_LOCATION (t
), TREE_CODE (t
), TREE_TYPE (t
),
4663 args
[0], args
[1], args
[2]);
4664 if (val
== NULL_TREE
)
4666 VERIFY_CONSTANT (val
);
4670 /* True if T was declared in a function declared to be constexpr, and
4671 therefore potentially constant in C++14. */
4674 var_in_constexpr_fn (tree t
)
4676 tree ctx
= DECL_CONTEXT (t
);
4677 return (ctx
&& TREE_CODE (ctx
) == FUNCTION_DECL
4678 && DECL_DECLARED_CONSTEXPR_P (ctx
));
4681 /* True if T was declared in a function that might be constexpr: either a
4682 function that was declared constexpr, or a C++17 lambda op(). */
4685 var_in_maybe_constexpr_fn (tree t
)
4687 if (cxx_dialect
>= cxx17
4688 && DECL_FUNCTION_SCOPE_P (t
)
4689 && LAMBDA_FUNCTION_P (DECL_CONTEXT (t
)))
4691 return var_in_constexpr_fn (t
);
4694 /* We're assigning INIT to TARGET. In do_build_copy_constructor and
4695 build_over_call we implement trivial copy of a class with tail padding using
4696 assignment of character arrays, which is valid in normal code, but not in
4697 constexpr evaluation. We don't need to worry about clobbering tail padding
4698 in constexpr evaluation, so strip the type punning. */
4701 maybe_simplify_trivial_copy (tree
&target
, tree
&init
)
4703 if (TREE_CODE (target
) == MEM_REF
4704 && TREE_CODE (init
) == MEM_REF
4705 && TREE_TYPE (target
) == TREE_TYPE (init
)
4706 && TREE_CODE (TREE_TYPE (target
)) == ARRAY_TYPE
4707 && TREE_TYPE (TREE_TYPE (target
)) == unsigned_char_type_node
)
4709 target
= build_fold_indirect_ref (TREE_OPERAND (target
, 0));
4710 init
= build_fold_indirect_ref (TREE_OPERAND (init
, 0));
4714 /* Returns true if REF, which is a COMPONENT_REF, has any fields
4715 of constant type. This does not check for 'mutable', so the
4716 caller is expected to be mindful of that. */
4719 cref_has_const_field (tree ref
)
4721 while (TREE_CODE (ref
) == COMPONENT_REF
)
4723 if (CP_TYPE_CONST_P (TREE_TYPE (TREE_OPERAND (ref
, 1))))
4725 ref
= TREE_OPERAND (ref
, 0);
4730 /* Return true if we are modifying something that is const during constant
4731 expression evaluation. CODE is the code of the statement, OBJ is the
4732 object in question, MUTABLE_P is true if one of the subobjects were
4733 declared mutable. */
4736 modifying_const_object_p (tree_code code
, tree obj
, bool mutable_p
)
4738 /* If this is initialization, there's no problem. */
4739 if (code
!= MODIFY_EXPR
)
4742 /* [basic.type.qualifier] "A const object is an object of type
4743 const T or a non-mutable subobject of a const object." */
4747 if (TREE_READONLY (obj
))
4750 if (CP_TYPE_CONST_P (TREE_TYPE (obj
)))
4752 /* Although a COMPONENT_REF may have a const type, we should
4753 only consider it modifying a const object when any of the
4754 field components is const. This can happen when using
4755 constructs such as const_cast<const T &>(m), making something
4756 const even though it wasn't declared const. */
4757 if (TREE_CODE (obj
) == COMPONENT_REF
)
4758 return cref_has_const_field (obj
);
4766 /* Evaluate an INIT_EXPR or MODIFY_EXPR. */
4769 cxx_eval_store_expression (const constexpr_ctx
*ctx
, tree t
,
4771 bool *non_constant_p
, bool *overflow_p
)
4773 constexpr_ctx new_ctx
= *ctx
;
4775 tree init
= TREE_OPERAND (t
, 1);
4776 if (TREE_CLOBBER_P (init
))
4777 /* Just ignore clobbers. */
4780 /* First we figure out where we're storing to. */
4781 tree target
= TREE_OPERAND (t
, 0);
4783 maybe_simplify_trivial_copy (target
, init
);
4785 tree type
= TREE_TYPE (target
);
4786 bool preeval
= SCALAR_TYPE_P (type
) || TREE_CODE (t
) == MODIFY_EXPR
;
4789 /* Evaluate the value to be stored without knowing what object it will be
4790 stored in, so that any side-effects happen first. */
4791 if (!SCALAR_TYPE_P (type
))
4792 new_ctx
.ctor
= new_ctx
.object
= NULL_TREE
;
4793 init
= cxx_eval_constant_expression (&new_ctx
, init
, false,
4794 non_constant_p
, overflow_p
);
4795 if (*non_constant_p
)
4799 bool evaluated
= false;
4802 /* If we want to return a reference to the target, we need to evaluate it
4803 as a whole; otherwise, only evaluate the innermost piece to avoid
4804 building up unnecessary *_REFs. */
4805 target
= cxx_eval_constant_expression (ctx
, target
, true,
4806 non_constant_p
, overflow_p
);
4808 if (*non_constant_p
)
4812 /* Find the underlying variable. */
4814 tree object
= NULL_TREE
;
4815 /* If we're modifying a const object, save it. */
4816 tree const_object_being_modified
= NULL_TREE
;
4817 bool mutable_p
= false;
4818 for (tree probe
= target
; object
== NULL_TREE
; )
4820 switch (TREE_CODE (probe
))
4826 tree ob
= TREE_OPERAND (probe
, 0);
4827 tree elt
= TREE_OPERAND (probe
, 1);
4828 if (TREE_CODE (elt
) == FIELD_DECL
&& DECL_MUTABLE_P (elt
))
4830 if (TREE_CODE (probe
) == ARRAY_REF
)
4832 elt
= eval_and_check_array_index (ctx
, probe
, false,
4833 non_constant_p
, overflow_p
);
4834 if (*non_constant_p
)
4837 /* We don't check modifying_const_object_p for ARRAY_REFs. Given
4838 "int a[10]", an ARRAY_REF "a[2]" can be "const int", even though
4839 the array isn't const. Instead, check "a" in the next iteration;
4840 that will detect modifying "const int a[10]". */
4842 && modifying_const_object_p (TREE_CODE (t
), probe
,
4844 && const_object_being_modified
== NULL_TREE
)
4845 const_object_being_modified
= probe
;
4846 vec_safe_push (refs
, elt
);
4847 vec_safe_push (refs
, TREE_TYPE (probe
));
4857 probe
= cxx_eval_constant_expression (ctx
, probe
, true,
4858 non_constant_p
, overflow_p
);
4860 if (*non_constant_p
)
4867 if (modifying_const_object_p (TREE_CODE (t
), object
, mutable_p
)
4868 && const_object_being_modified
== NULL_TREE
)
4869 const_object_being_modified
= object
;
4871 /* And then find/build up our initializer for the path to the subobject
4872 we're initializing. */
4874 if (DECL_P (object
))
4875 valp
= ctx
->global
->values
.get (object
);
4880 /* A constant-expression cannot modify objects from outside the
4881 constant-expression. */
4883 error ("modification of %qE is not a constant expression", object
);
4884 *non_constant_p
= true;
4887 type
= TREE_TYPE (object
);
4888 bool no_zero_init
= true;
4890 releasing_vec ctors
, indexes
;
4891 auto_vec
<int> index_pos_hints
;
4892 bool activated_union_member_p
= false;
4893 while (!refs
->is_empty ())
4895 if (*valp
== NULL_TREE
)
4897 *valp
= build_constructor (type
, NULL
);
4898 CONSTRUCTOR_NO_CLEARING (*valp
) = no_zero_init
;
4900 else if (TREE_CODE (*valp
) == STRING_CST
)
4902 /* An array was initialized with a string constant, and now
4903 we're writing into one of its elements. Explode the
4904 single initialization into a set of element
4906 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
4908 tree string
= *valp
;
4909 tree elt_type
= TREE_TYPE (type
);
4910 unsigned chars_per_elt
= (TYPE_PRECISION (elt_type
)
4911 / TYPE_PRECISION (char_type_node
));
4912 unsigned num_elts
= TREE_STRING_LENGTH (string
) / chars_per_elt
;
4913 tree ary_ctor
= build_constructor (type
, NULL
);
4915 vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor
), num_elts
);
4916 for (unsigned ix
= 0; ix
!= num_elts
; ix
++)
4918 constructor_elt elt
=
4920 build_int_cst (size_type_node
, ix
),
4921 extract_string_elt (string
, chars_per_elt
, ix
)
4923 CONSTRUCTOR_ELTS (ary_ctor
)->quick_push (elt
);
4929 /* If the value of object is already zero-initialized, any new ctors for
4930 subobjects will also be zero-initialized. */
4931 no_zero_init
= CONSTRUCTOR_NO_CLEARING (*valp
);
4933 enum tree_code code
= TREE_CODE (type
);
4935 tree index
= refs
->pop();
4937 if (code
== UNION_TYPE
&& CONSTRUCTOR_NELTS (*valp
)
4938 && CONSTRUCTOR_ELT (*valp
, 0)->index
!= index
)
4940 if (cxx_dialect
< cxx20
)
4943 error_at (cp_expr_loc_or_input_loc (t
),
4944 "change of the active member of a union "
4946 CONSTRUCTOR_ELT (*valp
, 0)->index
,
4948 *non_constant_p
= true;
4950 else if (TREE_CODE (t
) == MODIFY_EXPR
4951 && CONSTRUCTOR_NO_CLEARING (*valp
))
4953 /* Diagnose changing the active union member while the union
4954 is in the process of being initialized. */
4956 error_at (cp_expr_loc_or_input_loc (t
),
4957 "change of the active member of a union "
4958 "from %qD to %qD during initialization",
4959 CONSTRUCTOR_ELT (*valp
, 0)->index
,
4961 *non_constant_p
= true;
4963 no_zero_init
= true;
4966 vec_safe_push (ctors
, *valp
);
4967 vec_safe_push (indexes
, index
);
4969 constructor_elt
*cep
4970 = get_or_insert_ctor_field (*valp
, index
);
4971 index_pos_hints
.safe_push (cep
- CONSTRUCTOR_ELTS (*valp
)->begin());
4973 if (code
== UNION_TYPE
)
4974 activated_union_member_p
= true;
4979 /* Detect modifying a constant object in constexpr evaluation.
4980 We have found a const object that is being modified. Figure out
4981 if we need to issue an error. Consider
4985 constexpr A() : n(1) { n = 2; } // #1
4989 constexpr B() { a.n = 3; } // #2
4993 #1 is OK, since we're modifying an object under construction, but
4994 #2 is wrong, since "a" is const and has been fully constructed.
4995 To track it, we use the TREE_READONLY bit in the object's CONSTRUCTOR
4996 which means that the object is read-only. For the example above, the
4997 *ctors stack at the point of #2 will look like:
4999 ctors[0] = {.a={.n=2}} TREE_READONLY = 0
5000 ctors[1] = {.n=2} TREE_READONLY = 1
5002 and we're modifying "b.a", so we search the stack and see if the
5003 constructor for "b.a" has already run. */
5004 if (const_object_being_modified
)
5008 = strip_array_types (TREE_TYPE (const_object_being_modified
));
5009 if (!CLASS_TYPE_P (const_objtype
))
5013 /* [class.ctor]p5 "A constructor can be invoked for a const,
5014 volatile, or const volatile object. const and volatile
5015 semantics are not applied on an object under construction.
5016 They come into effect when the constructor for the most
5017 derived object ends." */
5020 FOR_EACH_VEC_ELT (*ctors
, i
, elt
)
5021 if (same_type_ignoring_top_level_qualifiers_p
5022 (TREE_TYPE (const_object_being_modified
), TREE_TYPE (elt
)))
5024 fail
= TREE_READONLY (elt
);
5031 modifying_const_object_error (t
, const_object_being_modified
);
5032 *non_constant_p
= true;
5039 /* Create a new CONSTRUCTOR in case evaluation of the initializer
5040 wants to modify it. */
5041 if (*valp
== NULL_TREE
)
5043 *valp
= build_constructor (type
, NULL
);
5044 CONSTRUCTOR_NO_CLEARING (*valp
) = no_zero_init
;
5046 new_ctx
.ctor
= *valp
;
5047 new_ctx
.object
= target
;
5048 /* Avoid temporary materialization when initializing from a TARGET_EXPR.
5049 We don't need to mess with AGGR_EXPR_SLOT/VEC_INIT_EXPR_SLOT because
5050 expansion of those trees uses ctx instead. */
5051 if (TREE_CODE (init
) == TARGET_EXPR
)
5052 if (tree tinit
= TARGET_EXPR_INITIAL (init
))
5054 init
= cxx_eval_constant_expression (&new_ctx
, init
, false,
5055 non_constant_p
, overflow_p
);
5056 /* The hash table might have moved since the get earlier, and the
5057 initializer might have mutated the underlying CONSTRUCTORs, so we must
5059 valp
= ctx
->global
->values
.get (object
);
5060 for (unsigned i
= 0; i
< vec_safe_length (indexes
); i
++)
5062 constructor_elt
*cep
5063 = get_or_insert_ctor_field (*valp
, indexes
[i
], index_pos_hints
[i
]);
5068 /* Don't share a CONSTRUCTOR that might be changed later. */
5069 init
= unshare_constructor (init
);
5071 if (*valp
&& TREE_CODE (*valp
) == CONSTRUCTOR
5072 && TREE_CODE (init
) == CONSTRUCTOR
)
5074 /* An outer ctx->ctor might be pointing to *valp, so replace
5076 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init
),
5079 /* For initialization of an empty base, the original target will be
5080 *(base*)this, evaluation of which resolves to the object
5081 argument, which has the derived type rather than the base type. In
5082 this situation, just evaluate the initializer and return, since
5083 there's no actual data to store. */
5084 gcc_assert (is_empty_class (TREE_TYPE (init
)) && !lval
);
5087 CONSTRUCTOR_ELTS (*valp
) = CONSTRUCTOR_ELTS (init
);
5088 TREE_CONSTANT (*valp
) = TREE_CONSTANT (init
);
5089 TREE_SIDE_EFFECTS (*valp
) = TREE_SIDE_EFFECTS (init
);
5090 CONSTRUCTOR_NO_CLEARING (*valp
)
5091 = CONSTRUCTOR_NO_CLEARING (init
);
5096 /* After initialization, 'const' semantics apply to the value of the
5097 object. Make a note of this fact by marking the CONSTRUCTOR
5099 if (TREE_CODE (t
) == INIT_EXPR
5100 && TREE_CODE (*valp
) == CONSTRUCTOR
5101 && TYPE_READONLY (type
))
5103 if (INDIRECT_REF_P (target
)
5104 && (is_this_parameter
5105 (tree_strip_nop_conversions (TREE_OPERAND (target
, 0)))))
5106 /* We've just initialized '*this' (perhaps via the target
5107 constructor of a delegating constructor). Leave it up to the
5108 caller that set 'this' to set TREE_READONLY appropriately. */
5109 gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
5110 (TREE_TYPE (target
), type
));
5112 TREE_READONLY (*valp
) = true;
5115 /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing
5116 CONSTRUCTORs, if any. */
5119 bool c
= TREE_CONSTANT (init
);
5120 bool s
= TREE_SIDE_EFFECTS (init
);
5121 if (!c
|| s
|| activated_union_member_p
)
5122 FOR_EACH_VEC_ELT (*ctors
, i
, elt
)
5125 TREE_CONSTANT (elt
) = false;
5127 TREE_SIDE_EFFECTS (elt
) = true;
5128 /* Clear CONSTRUCTOR_NO_CLEARING since we've activated a member of
5130 if (TREE_CODE (TREE_TYPE (elt
)) == UNION_TYPE
)
5131 CONSTRUCTOR_NO_CLEARING (elt
) = false;
5134 if (*non_constant_p
)
5142 /* Evaluate a ++ or -- expression. */
5145 cxx_eval_increment_expression (const constexpr_ctx
*ctx
, tree t
,
5147 bool *non_constant_p
, bool *overflow_p
)
5149 enum tree_code code
= TREE_CODE (t
);
5150 tree type
= TREE_TYPE (t
);
5151 tree op
= TREE_OPERAND (t
, 0);
5152 tree offset
= TREE_OPERAND (t
, 1);
5153 gcc_assert (TREE_CONSTANT (offset
));
5155 /* OFFSET is constant, but perhaps not constant enough. We need to
5156 e.g. bash FLOAT_EXPRs to REAL_CSTs. */
5157 offset
= fold_simple (offset
);
5159 /* The operand as an lvalue. */
5160 op
= cxx_eval_constant_expression (ctx
, op
, true,
5161 non_constant_p
, overflow_p
);
5163 /* The operand as an rvalue. */
5165 = cxx_eval_constant_expression (ctx
, op
, false,
5166 non_constant_p
, overflow_p
);
5167 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
5168 a local array in a constexpr function. */
5169 bool ptr
= INDIRECT_TYPE_P (TREE_TYPE (val
));
5171 VERIFY_CONSTANT (val
);
5173 /* The modified value. */
5174 bool inc
= (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
);
5176 if (INDIRECT_TYPE_P (type
))
5178 /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
5179 offset
= convert_to_ptrofftype (offset
);
5181 offset
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (offset
), offset
);
5182 mod
= fold_build2 (POINTER_PLUS_EXPR
, type
, val
, offset
);
5185 mod
= fold_build2 (inc
? PLUS_EXPR
: MINUS_EXPR
, type
, val
, offset
);
5187 VERIFY_CONSTANT (mod
);
5189 /* Storing the modified value. */
5190 tree store
= build2_loc (cp_expr_loc_or_loc (t
, input_location
),
5191 MODIFY_EXPR
, type
, op
, mod
);
5192 cxx_eval_constant_expression (ctx
, store
,
5193 true, non_constant_p
, overflow_p
);
5196 /* And the value of the expression. */
5197 if (code
== PREINCREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
5199 /* Prefix ops are lvalues. */
5203 /* But we optimize when the caller wants an rvalue. */
5207 /* Postfix ops are rvalues. */
5211 /* Predicates for the meaning of *jump_target. */
5214 returns (tree
*jump_target
)
5217 && (TREE_CODE (*jump_target
) == RETURN_EXPR
5218 || (TREE_CODE (*jump_target
) == LABEL_DECL
5219 && LABEL_DECL_CDTOR (*jump_target
)));
5223 breaks (tree
*jump_target
)
5226 && ((TREE_CODE (*jump_target
) == LABEL_DECL
5227 && LABEL_DECL_BREAK (*jump_target
))
5228 || TREE_CODE (*jump_target
) == BREAK_STMT
5229 || TREE_CODE (*jump_target
) == EXIT_EXPR
);
5233 continues (tree
*jump_target
)
5236 && ((TREE_CODE (*jump_target
) == LABEL_DECL
5237 && LABEL_DECL_CONTINUE (*jump_target
))
5238 || TREE_CODE (*jump_target
) == CONTINUE_STMT
);
5243 switches (tree
*jump_target
)
5246 && TREE_CODE (*jump_target
) == INTEGER_CST
;
5249 /* Subroutine of cxx_eval_statement_list. Determine whether the statement
5250 STMT matches *jump_target. If we're looking for a case label and we see
5251 the default label, note it in ctx->css_state. */
5254 label_matches (const constexpr_ctx
*ctx
, tree
*jump_target
, tree stmt
)
5256 switch (TREE_CODE (*jump_target
))
5259 if (TREE_CODE (stmt
) == LABEL_EXPR
5260 && LABEL_EXPR_LABEL (stmt
) == *jump_target
)
5265 if (TREE_CODE (stmt
) == CASE_LABEL_EXPR
)
5267 gcc_assert (ctx
->css_state
!= NULL
);
5268 if (!CASE_LOW (stmt
))
5270 /* default: should appear just once in a SWITCH_EXPR
5271 body (excluding nested SWITCH_EXPR). */
5272 gcc_assert (*ctx
->css_state
!= css_default_seen
);
5273 /* When evaluating SWITCH_EXPR body for the second time,
5274 return true for the default: label. */
5275 if (*ctx
->css_state
== css_default_processing
)
5277 *ctx
->css_state
= css_default_seen
;
5279 else if (CASE_HIGH (stmt
))
5281 if (tree_int_cst_le (CASE_LOW (stmt
), *jump_target
)
5282 && tree_int_cst_le (*jump_target
, CASE_HIGH (stmt
)))
5285 else if (tree_int_cst_equal (*jump_target
, CASE_LOW (stmt
)))
5292 /* These two are handled directly in cxx_eval_loop_expr by testing
5293 breaks (jump_target) or continues (jump_target). */
5302 /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
5303 semantics, for switch, break, continue, and return. */
5306 cxx_eval_statement_list (const constexpr_ctx
*ctx
, tree t
,
5307 bool *non_constant_p
, bool *overflow_p
,
5310 tree_stmt_iterator i
;
5312 /* In a statement-expression we want to return the last value.
5313 For empty statement expression return void_node. */
5317 local_target
= NULL_TREE
;
5318 jump_target
= &local_target
;
5320 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
5322 tree stmt
= tsi_stmt (i
);
5323 /* We've found a continue, so skip everything until we reach
5324 the label its jumping to. */
5325 if (continues (jump_target
))
5327 if (label_matches (ctx
, jump_target
, stmt
))
5329 *jump_target
= NULL_TREE
;
5333 if (TREE_CODE (stmt
) == DEBUG_BEGIN_STMT
)
5335 r
= cxx_eval_constant_expression (ctx
, stmt
, false,
5336 non_constant_p
, overflow_p
,
5338 if (*non_constant_p
)
5340 if (returns (jump_target
) || breaks (jump_target
))
5343 if (*jump_target
&& jump_target
== &local_target
)
5345 /* We aren't communicating the jump to our caller, so give up. We don't
5346 need to support evaluation of jumps out of statement-exprs. */
5348 error_at (cp_expr_loc_or_input_loc (r
),
5349 "statement is not a constant expression");
5350 *non_constant_p
= true;
5355 /* Evaluate a LOOP_EXPR for side-effects. Handles break and return
5356 semantics; continue semantics are covered by cxx_eval_statement_list. */
5359 cxx_eval_loop_expr (const constexpr_ctx
*ctx
, tree t
,
5360 bool *non_constant_p
, bool *overflow_p
,
5363 constexpr_ctx new_ctx
= *ctx
;
5367 local_target
= NULL_TREE
;
5368 jump_target
= &local_target
;
5371 tree body
, cond
= NULL_TREE
, expr
= NULL_TREE
;
5373 switch (TREE_CODE (t
))
5376 body
= LOOP_EXPR_BODY (t
);
5383 body
= WHILE_BODY (t
);
5384 cond
= WHILE_COND (t
);
5388 if (FOR_INIT_STMT (t
))
5389 cxx_eval_constant_expression (ctx
, FOR_INIT_STMT (t
), /*lval*/false,
5390 non_constant_p
, overflow_p
, jump_target
);
5391 if (*non_constant_p
)
5393 body
= FOR_BODY (t
);
5394 cond
= FOR_COND (t
);
5395 expr
= FOR_EXPR (t
);
5401 auto_vec
<tree
, 10> save_exprs
;
5402 new_ctx
.save_exprs
= &save_exprs
;
5408 cxx_eval_constant_expression (&new_ctx
, body
, /*lval*/false,
5409 non_constant_p
, overflow_p
,
5411 if (breaks (jump_target
))
5413 *jump_target
= NULL_TREE
;
5417 if (TREE_CODE (t
) != LOOP_EXPR
&& continues (jump_target
))
5418 *jump_target
= NULL_TREE
;
5421 cxx_eval_constant_expression (&new_ctx
, expr
, /*lval*/false,
5422 non_constant_p
, overflow_p
,
5429 = cxx_eval_constant_expression (&new_ctx
, cond
, /*lval*/false,
5430 non_constant_p
, overflow_p
,
5434 if (verify_constant (res
, ctx
->quiet
, non_constant_p
,
5437 if (integer_zerop (res
))
5441 gcc_assert (*jump_target
);
5444 /* Forget saved values of SAVE_EXPRs and TARGET_EXPRs. */
5447 FOR_EACH_VEC_ELT (save_exprs
, i
, save_expr
)
5448 ctx
->global
->values
.remove (save_expr
);
5449 save_exprs
.truncate (0);
5451 if (++count
>= constexpr_loop_limit
)
5454 error_at (cp_expr_loc_or_input_loc (t
),
5455 "%<constexpr%> loop iteration count exceeds limit of %d "
5456 "(use %<-fconstexpr-loop-limit=%> to increase the limit)",
5457 constexpr_loop_limit
);
5458 *non_constant_p
= true;
5462 while (!returns (jump_target
)
5463 && !breaks (jump_target
)
5464 && !continues (jump_target
)
5465 && (!switches (jump_target
) || count
== 0)
5466 && !*non_constant_p
);
5468 /* Forget saved values of SAVE_EXPRs and TARGET_EXPRs. */
5471 FOR_EACH_VEC_ELT (save_exprs
, i
, save_expr
)
5472 ctx
->global
->values
.remove (save_expr
);
5477 /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
5481 cxx_eval_switch_expr (const constexpr_ctx
*ctx
, tree t
,
5482 bool *non_constant_p
, bool *overflow_p
,
5486 = TREE_CODE (t
) == SWITCH_STMT
? SWITCH_STMT_COND (t
) : SWITCH_COND (t
);
5487 cond
= cxx_eval_constant_expression (ctx
, cond
, false,
5488 non_constant_p
, overflow_p
);
5489 VERIFY_CONSTANT (cond
);
5490 *jump_target
= cond
;
5493 = TREE_CODE (t
) == SWITCH_STMT
? SWITCH_STMT_BODY (t
) : SWITCH_BODY (t
);
5494 constexpr_ctx new_ctx
= *ctx
;
5495 constexpr_switch_state css
= css_default_not_seen
;
5496 new_ctx
.css_state
= &css
;
5497 cxx_eval_constant_expression (&new_ctx
, body
, false,
5498 non_constant_p
, overflow_p
, jump_target
);
5499 if (switches (jump_target
) && css
== css_default_seen
)
5501 /* If the SWITCH_EXPR body has default: label, process it once again,
5502 this time instructing label_matches to return true for default:
5503 label on switches (jump_target). */
5504 css
= css_default_processing
;
5505 cxx_eval_constant_expression (&new_ctx
, body
, false,
5506 non_constant_p
, overflow_p
, jump_target
);
5508 if (breaks (jump_target
) || switches (jump_target
))
5509 *jump_target
= NULL_TREE
;
5513 /* Find the object of TYPE under initialization in CTX. */
5516 lookup_placeholder (const constexpr_ctx
*ctx
, bool lval
, tree type
)
5521 /* Prefer the outermost matching object, but don't cross
5522 CONSTRUCTOR_PLACEHOLDER_BOUNDARY constructors. */
5523 if (ctx
->ctor
&& !CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx
->ctor
))
5524 if (tree outer_ob
= lookup_placeholder (ctx
->parent
, lval
, type
))
5527 /* We could use ctx->object unconditionally, but using ctx->ctor when we
5528 can is a minor optimization. */
5529 if (!lval
&& ctx
->ctor
&& same_type_p (TREE_TYPE (ctx
->ctor
), type
))
5535 /* Since an object cannot have a field of its own type, we can search outward
5536 from ctx->object to find the unique containing object of TYPE. */
5537 tree ob
= ctx
->object
;
5540 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob
), type
))
5542 if (handled_component_p (ob
))
5543 ob
= TREE_OPERAND (ob
, 0);
5551 /* Complain about an attempt to evaluate inline assembly. */
5554 inline_asm_in_constexpr_error (location_t loc
)
5556 auto_diagnostic_group d
;
5557 error_at (loc
, "inline assembly is not a constant expression");
5558 inform (loc
, "only unevaluated inline assembly is allowed in a "
5559 "%<constexpr%> function in C++20");
5562 /* Attempt to reduce the expression T to a constant value.
5563 On failure, issue diagnostic and return error_mark_node. */
5564 /* FIXME unify with c_fully_fold */
5565 /* FIXME overflow_p is too global */
5568 cxx_eval_constant_expression (const constexpr_ctx
*ctx
, tree t
,
5570 bool *non_constant_p
, bool *overflow_p
,
5571 tree
*jump_target
/* = NULL */)
5573 if (jump_target
&& *jump_target
)
5575 /* If we are jumping, ignore all statements/expressions except those
5576 that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */
5577 switch (TREE_CODE (t
))
5580 case STATEMENT_LIST
:
5589 case CASE_LABEL_EXPR
:
5590 if (label_matches (ctx
, jump_target
, t
))
5592 *jump_target
= NULL_TREE
;
5598 if (error_operand_p (t
))
5600 *non_constant_p
= true;
5604 location_t loc
= cp_expr_loc_or_input_loc (t
);
5606 STRIP_ANY_LOCATION_WRAPPER (t
);
5608 if (CONSTANT_CLASS_P (t
))
5610 if (TREE_OVERFLOW (t
))
5613 permerror (input_location
, "overflow in constant expression");
5614 if (!flag_permissive
|| ctx
->quiet
)
5618 if (TREE_CODE (t
) == INTEGER_CST
5619 && TYPE_PTR_P (TREE_TYPE (t
))
5620 && !integer_zerop (t
))
5623 error ("value %qE of type %qT is not a constant expression",
5625 *non_constant_p
= true;
5631 /* Avoid excessively long constexpr evaluations. */
5632 if (++ctx
->global
->constexpr_ops_count
>= constexpr_ops_limit
)
5636 "%<constexpr%> evaluation operation count exceeds limit of "
5637 "%wd (use %<-fconstexpr-ops-limit=%> to increase the limit)",
5638 constexpr_ops_limit
);
5639 ctx
->global
->constexpr_ops_count
= INTTYPE_MINIMUM (HOST_WIDE_INT
);
5640 *non_constant_p
= true;
5644 constexpr_ctx new_ctx
;
5647 tree_code tcode
= TREE_CODE (t
);
5653 /* We ask for an rvalue for the RESULT_DECL when indirecting
5654 through an invisible reference, or in named return value
5656 if (tree
*p
= ctx
->global
->values
.get (t
))
5661 error ("%qE is not a constant expression", t
);
5662 *non_constant_p
= true;
5667 if (DECL_HAS_VALUE_EXPR_P (t
))
5669 if (is_normal_capture_proxy (t
)
5670 && current_function_decl
== DECL_CONTEXT (t
))
5672 /* Function parms aren't constexpr within the function
5673 definition, so don't try to look at the closure. But if the
5674 captured variable is constant, try to evaluate it directly. */
5675 r
= DECL_CAPTURED_VARIABLE (t
);
5676 tree type
= TREE_TYPE (t
);
5677 if (TYPE_REF_P (type
) != TYPE_REF_P (TREE_TYPE (r
)))
5679 /* Adjust r to match the reference-ness of t. */
5680 if (TYPE_REF_P (type
))
5681 r
= build_address (r
);
5683 r
= convert_from_reference (r
);
5687 r
= DECL_VALUE_EXPR (t
);
5688 return cxx_eval_constant_expression (ctx
, r
, lval
, non_constant_p
,
5693 /* We used to not check lval for CONST_DECL, but darwin.c uses
5694 CONST_DECL for aggregate constants. */
5697 else if (t
== ctx
->object
)
5700 if (tree
*p
= ctx
->global
->values
.get (t
))
5701 if (*p
!= NULL_TREE
)
5706 if (COMPLETE_TYPE_P (TREE_TYPE (t
))
5707 && is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
5709 /* If the class is empty, we aren't actually loading anything. */
5710 r
= build_constructor (TREE_TYPE (t
), NULL
);
5711 TREE_CONSTANT (r
) = true;
5713 else if (ctx
->strict
)
5714 r
= decl_really_constant_value (t
, /*unshare_p=*/false);
5716 r
= decl_constant_value (t
, /*unshare_p=*/false);
5717 if (TREE_CODE (r
) == TARGET_EXPR
5718 && TREE_CODE (TARGET_EXPR_INITIAL (r
)) == CONSTRUCTOR
)
5719 r
= TARGET_EXPR_INITIAL (r
);
5723 non_const_var_error (loc
, r
);
5724 *non_constant_p
= true;
5728 case DEBUG_BEGIN_STMT
:
5729 /* ??? It might be nice to retain this information somehow, so
5730 as to be able to step into a constexpr function call. */
5737 case CASE_LABEL_EXPR
:
5742 if (lval
&& !TYPE_REF_P (TREE_TYPE (t
)))
5744 else if (tree
*p
= ctx
->global
->values
.get (r
))
5747 /* Defer in case this is only used for its type. */;
5748 else if (COMPLETE_TYPE_P (TREE_TYPE (t
))
5749 && is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
5751 /* If the class is empty, we aren't actually loading anything. */
5752 r
= build_constructor (TREE_TYPE (t
), NULL
);
5753 TREE_CONSTANT (r
) = true;
5758 error ("%qE is not a constant expression", t
);
5759 *non_constant_p
= true;
5764 case AGGR_INIT_EXPR
:
5765 r
= cxx_eval_call_expression (ctx
, t
, lval
,
5766 non_constant_p
, overflow_p
);
5771 r
= DECL_EXPR_DECL (t
);
5772 if (TREE_CODE (r
) == USING_DECL
)
5777 if (AGGREGATE_TYPE_P (TREE_TYPE (r
))
5778 || VECTOR_TYPE_P (TREE_TYPE (r
)))
5782 new_ctx
.ctor
= build_constructor (TREE_TYPE (r
), NULL
);
5783 CONSTRUCTOR_NO_CLEARING (new_ctx
.ctor
) = true;
5784 ctx
->global
->values
.put (r
, new_ctx
.ctor
);
5788 if (tree init
= DECL_INITIAL (r
))
5790 init
= cxx_eval_constant_expression (ctx
, init
,
5792 non_constant_p
, overflow_p
);
5793 /* Don't share a CONSTRUCTOR that might be changed. */
5794 init
= unshare_constructor (init
);
5795 /* Remember that a constant object's constructor has already
5797 if (CLASS_TYPE_P (TREE_TYPE (r
))
5798 && CP_TYPE_CONST_P (TREE_TYPE (r
)))
5799 TREE_READONLY (init
) = true;
5800 ctx
->global
->values
.put (r
, init
);
5802 else if (ctx
== &new_ctx
)
5803 /* We gave it a CONSTRUCTOR above. */;
5805 ctx
->global
->values
.put (r
, NULL_TREE
);
5811 tree type
= TREE_TYPE (t
);
5813 if (!literal_type_p (type
))
5817 auto_diagnostic_group d
;
5818 error ("temporary of non-literal type %qT in a "
5819 "constant expression", type
);
5820 explain_non_literal_class (type
);
5822 *non_constant_p
= true;
5825 gcc_checking_assert (!TARGET_EXPR_DIRECT_INIT_P (t
));
5826 /* Avoid evaluating a TARGET_EXPR more than once. */
5827 tree slot
= TARGET_EXPR_SLOT (t
);
5828 if (tree
*p
= ctx
->global
->values
.get (slot
))
5835 if ((AGGREGATE_TYPE_P (type
) || VECTOR_TYPE_P (type
)))
5837 /* We're being expanded without an explicit target, so start
5838 initializing a new object; expansion with an explicit target
5839 strips the TARGET_EXPR before we get here. */
5841 /* Link CTX to NEW_CTX so that lookup_placeholder can resolve
5842 any PLACEHOLDER_EXPR within the initializer that refers to the
5843 former object under construction. */
5844 new_ctx
.parent
= ctx
;
5845 new_ctx
.ctor
= build_constructor (type
, NULL
);
5846 CONSTRUCTOR_NO_CLEARING (new_ctx
.ctor
) = true;
5847 new_ctx
.object
= slot
;
5848 ctx
->global
->values
.put (new_ctx
.object
, new_ctx
.ctor
);
5851 /* Pass false for 'lval' because this indicates
5852 initialization of a temporary. */
5853 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
5855 non_constant_p
, overflow_p
);
5856 if (*non_constant_p
)
5858 /* Adjust the type of the result to the type of the temporary. */
5859 r
= adjust_temp_type (type
, r
);
5860 if (TARGET_EXPR_CLEANUP (t
) && !CLEANUP_EH_ONLY (t
))
5861 ctx
->global
->cleanups
->safe_push (TARGET_EXPR_CLEANUP (t
));
5862 r
= unshare_constructor (r
);
5863 ctx
->global
->values
.put (slot
, r
);
5864 if (ctx
->save_exprs
)
5865 ctx
->save_exprs
->safe_push (slot
);
5873 gcc_assert (jump_target
== NULL
|| *jump_target
== NULL_TREE
);
5874 r
= cxx_eval_store_expression (ctx
, t
, lval
,
5875 non_constant_p
, overflow_p
);
5879 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
5881 non_constant_p
, overflow_p
);
5885 if (TREE_OPERAND (t
, 0) != NULL_TREE
)
5886 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
5888 non_constant_p
, overflow_p
);
5896 /* Can happen with ({ return true; }) && false; passed to
5897 maybe_constant_value. There is nothing to jump over in this
5898 case, and the bug will be diagnosed later. */
5899 gcc_assert (ctx
->quiet
);
5900 *non_constant_p
= true;
5905 /* Avoid evaluating a SAVE_EXPR more than once. */
5906 if (tree
*p
= ctx
->global
->values
.get (t
))
5910 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), false,
5911 non_constant_p
, overflow_p
);
5912 if (*non_constant_p
)
5914 ctx
->global
->values
.put (t
, r
);
5915 if (ctx
->save_exprs
)
5916 ctx
->save_exprs
->safe_push (t
);
5920 case TRY_CATCH_EXPR
:
5921 if (TREE_OPERAND (t
, 0) == NULL_TREE
)
5927 case NON_LVALUE_EXPR
:
5929 case MUST_NOT_THROW_EXPR
:
5932 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
5934 non_constant_p
, overflow_p
,
5938 case CLEANUP_POINT_EXPR
:
5940 auto_vec
<tree
, 2> cleanups
;
5941 vec
<tree
> *prev_cleanups
= ctx
->global
->cleanups
;
5942 ctx
->global
->cleanups
= &cleanups
;
5943 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
5945 non_constant_p
, overflow_p
,
5947 ctx
->global
->cleanups
= prev_cleanups
;
5950 /* Evaluate the cleanups. */
5951 FOR_EACH_VEC_ELT_REVERSE (cleanups
, i
, cleanup
)
5952 cxx_eval_constant_expression (ctx
, cleanup
, false,
5953 non_constant_p
, overflow_p
,
5958 case TRY_FINALLY_EXPR
:
5959 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), lval
,
5960 non_constant_p
, overflow_p
,
5962 if (!*non_constant_p
)
5963 /* Also evaluate the cleanup. */
5964 cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1), true,
5965 non_constant_p
, overflow_p
,
5971 tree initial_jump_target
= jump_target
? *jump_target
: NULL_TREE
;
5972 r
= cxx_eval_constant_expression (ctx
, CLEANUP_BODY (t
), lval
,
5973 non_constant_p
, overflow_p
,
5975 if (!CLEANUP_EH_ONLY (t
) && !*non_constant_p
)
5977 iloc_sentinel
ils (loc
);
5978 /* Also evaluate the cleanup. If we weren't skipping at the
5979 start of the CLEANUP_BODY, change jump_target temporarily
5980 to &initial_jump_target, so that even a return or break or
5981 continue in the body doesn't skip the cleanup. */
5982 cxx_eval_constant_expression (ctx
, CLEANUP_EXPR (t
), true,
5983 non_constant_p
, overflow_p
,
5984 jump_target
? &initial_jump_target
5990 /* These differ from cxx_eval_unary_expression in that this doesn't
5991 check for a constant operand or result; an address can be
5992 constant without its operand being, and vice versa. */
5995 r
= cxx_eval_indirect_ref (ctx
, t
, lval
,
5996 non_constant_p
, overflow_p
);
6001 tree oldop
= TREE_OPERAND (t
, 0);
6002 tree op
= cxx_eval_constant_expression (ctx
, oldop
,
6004 non_constant_p
, overflow_p
);
6005 /* Don't VERIFY_CONSTANT here. */
6006 if (*non_constant_p
)
6008 gcc_checking_assert (TREE_CODE (op
) != CONSTRUCTOR
);
6009 /* This function does more aggressive folding than fold itself. */
6010 r
= build_fold_addr_expr_with_type (op
, TREE_TYPE (t
));
6011 if (TREE_CODE (r
) == ADDR_EXPR
&& TREE_OPERAND (r
, 0) == oldop
)
6023 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), lval
,
6024 non_constant_p
, overflow_p
);
6025 if (r
== error_mark_node
)
6027 else if (r
== TREE_OPERAND (t
, 0))
6030 r
= fold_build1 (TREE_CODE (t
), TREE_TYPE (t
), r
);
6035 case FIX_TRUNC_EXPR
:
6041 case TRUTH_NOT_EXPR
:
6042 case FIXED_CONVERT_EXPR
:
6043 r
= cxx_eval_unary_expression (ctx
, t
, lval
,
6044 non_constant_p
, overflow_p
);
6048 r
= fold_sizeof_expr (t
);
6049 /* In a template, fold_sizeof_expr may merely create a new SIZEOF_EXPR,
6050 which could lead to an infinite recursion. */
6051 if (TREE_CODE (r
) != SIZEOF_EXPR
)
6052 r
= cxx_eval_constant_expression (ctx
, r
, lval
,
6053 non_constant_p
, overflow_p
,
6057 *non_constant_p
= true;
6058 gcc_assert (ctx
->quiet
);
6065 /* check_return_expr sometimes wraps a TARGET_EXPR in a
6066 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
6067 introduced by build_call_a. */
6068 tree op0
= TREE_OPERAND (t
, 0);
6069 tree op1
= TREE_OPERAND (t
, 1);
6071 if ((TREE_CODE (op0
) == TARGET_EXPR
&& op1
== TARGET_EXPR_SLOT (op0
))
6072 || TREE_CODE (op1
) == EMPTY_CLASS_EXPR
)
6073 r
= cxx_eval_constant_expression (ctx
, op0
,
6074 lval
, non_constant_p
, overflow_p
,
6078 /* Check that the LHS is constant and then discard it. */
6079 cxx_eval_constant_expression (ctx
, op0
,
6080 true, non_constant_p
, overflow_p
,
6082 if (*non_constant_p
)
6084 op1
= TREE_OPERAND (t
, 1);
6085 r
= cxx_eval_constant_expression (ctx
, op1
,
6086 lval
, non_constant_p
, overflow_p
,
6092 case POINTER_PLUS_EXPR
:
6093 case POINTER_DIFF_EXPR
:
6097 case TRUNC_DIV_EXPR
:
6099 case FLOOR_DIV_EXPR
:
6100 case ROUND_DIV_EXPR
:
6101 case TRUNC_MOD_EXPR
:
6103 case ROUND_MOD_EXPR
:
6105 case EXACT_DIV_EXPR
:
6115 case TRUTH_XOR_EXPR
:
6122 case SPACESHIP_EXPR
:
6123 case UNORDERED_EXPR
:
6133 r
= cxx_eval_binary_expression (ctx
, t
, lval
,
6134 non_constant_p
, overflow_p
);
6137 /* fold can introduce non-IF versions of these; still treat them as
6138 short-circuiting. */
6139 case TRUTH_AND_EXPR
:
6140 case TRUTH_ANDIF_EXPR
:
6141 r
= cxx_eval_logical_expression (ctx
, t
, boolean_false_node
,
6144 non_constant_p
, overflow_p
);
6148 case TRUTH_ORIF_EXPR
:
6149 r
= cxx_eval_logical_expression (ctx
, t
, boolean_true_node
,
6152 non_constant_p
, overflow_p
);
6156 r
= cxx_eval_array_reference (ctx
, t
, lval
,
6157 non_constant_p
, overflow_p
);
6161 if (is_overloaded_fn (t
))
6163 /* We can only get here in checking mode via
6164 build_non_dependent_expr, because any expression that
6165 calls or takes the address of the function will have
6166 pulled a FUNCTION_DECL out of the COMPONENT_REF. */
6167 gcc_checking_assert (ctx
->quiet
|| errorcount
);
6168 *non_constant_p
= true;
6171 r
= cxx_eval_component_reference (ctx
, t
, lval
,
6172 non_constant_p
, overflow_p
);
6176 r
= cxx_eval_bit_field_ref (ctx
, t
, lval
,
6177 non_constant_p
, overflow_p
);
6182 if (jump_target
&& *jump_target
)
6184 tree orig_jump
= *jump_target
;
6185 tree arg
= ((TREE_CODE (t
) != IF_STMT
|| TREE_OPERAND (t
, 1))
6186 ? TREE_OPERAND (t
, 1) : void_node
);
6187 /* When jumping to a label, the label might be either in the
6188 then or else blocks, so process then block first in skipping
6189 mode first, and if we are still in the skipping mode at its end,
6190 process the else block too. */
6191 r
= cxx_eval_constant_expression (ctx
, arg
, lval
, non_constant_p
,
6192 overflow_p
, jump_target
);
6193 /* It's possible that we found the label in the then block. But
6194 it could have been followed by another jumping statement, e.g.
6195 say we're looking for case 1:
6198 // skipped statements
6199 case 1:; // clears up *jump_target
6200 return 1; // and sets it to a RETURN_EXPR
6203 in which case we need not go looking to the else block.
6204 (goto is not allowed in a constexpr function.) */
6205 if (*jump_target
== orig_jump
)
6207 arg
= ((TREE_CODE (t
) != IF_STMT
|| TREE_OPERAND (t
, 2))
6208 ? TREE_OPERAND (t
, 2) : void_node
);
6209 r
= cxx_eval_constant_expression (ctx
, arg
, lval
, non_constant_p
,
6210 overflow_p
, jump_target
);
6214 r
= cxx_eval_conditional_expression (ctx
, t
, lval
,
6215 non_constant_p
, overflow_p
,
6219 r
= cxx_eval_vector_conditional_expression (ctx
, t
, non_constant_p
,
6224 if (TREE_CONSTANT (t
) && reduced_constant_expression_p (t
))
6226 /* Don't re-process a constant CONSTRUCTOR, but do fold it to
6227 VECTOR_CST if applicable. */
6228 verify_constructor_flags (t
);
6229 if (TREE_CONSTANT (t
))
6232 r
= cxx_eval_bare_aggregate (ctx
, t
, lval
,
6233 non_constant_p
, overflow_p
);
6237 /* We can get this in a defaulted constructor for a class with a
6238 non-static data member of array type. Either the initializer will
6239 be NULL, meaning default-initialization, or it will be an lvalue
6240 or xvalue of the same type, meaning direct-initialization from the
6241 corresponding member. */
6242 r
= cxx_eval_vec_init (ctx
, t
, lval
,
6243 non_constant_p
, overflow_p
);
6247 r
= cxx_eval_trinary_expression (ctx
, t
, lval
,
6248 non_constant_p
, overflow_p
);
6252 if (REINTERPRET_CAST_P (t
))
6256 "%<reinterpret_cast%> is not a constant expression");
6257 *non_constant_p
= true;
6262 case VIEW_CONVERT_EXPR
:
6263 case UNARY_PLUS_EXPR
:
6265 tree oldop
= TREE_OPERAND (t
, 0);
6267 tree op
= cxx_eval_constant_expression (ctx
, oldop
,
6269 non_constant_p
, overflow_p
);
6270 if (*non_constant_p
)
6272 tree type
= TREE_TYPE (t
);
6274 if (VOID_TYPE_P (type
))
6277 if (TREE_CODE (t
) == CONVERT_EXPR
6278 && ARITHMETIC_TYPE_P (type
)
6279 && INDIRECT_TYPE_P (TREE_TYPE (op
)))
6283 "conversion from pointer type %qT to arithmetic type "
6284 "%qT in a constant expression", TREE_TYPE (op
), type
);
6285 *non_constant_p
= true;
6289 if (TREE_CODE (op
) == PTRMEM_CST
&& !TYPE_PTRMEM_P (type
))
6290 op
= cplus_expand_constant (op
);
6292 if (TREE_CODE (op
) == PTRMEM_CST
&& tcode
== NOP_EXPR
)
6294 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (op
))
6295 && !can_convert_qual (type
, op
))
6296 op
= cplus_expand_constant (op
);
6297 return cp_fold_convert (type
, op
);
6300 if (INDIRECT_TYPE_P (type
) && TREE_CODE (op
) == INTEGER_CST
)
6302 if (integer_zerop (op
))
6304 if (TYPE_REF_P (type
))
6308 "dereferencing a null pointer");
6309 *non_constant_p
= true;
6312 else if (TYPE_PTR_P (TREE_TYPE (op
)))
6314 tree from
= TREE_TYPE (op
);
6316 if (!can_convert (type
, from
, tf_none
))
6320 "conversion of %qT null pointer to %qT "
6321 "is not a constant expression",
6323 *non_constant_p
= true;
6330 /* This detects for example:
6331 reinterpret_cast<void*>(sizeof 0)
6334 error_at (loc
, "%<reinterpret_cast<%T>(%E)%> is not "
6335 "a constant expression",
6337 *non_constant_p
= true;
6342 if (INDIRECT_TYPE_P (type
)
6343 && TREE_CODE (op
) == NOP_EXPR
6344 && TREE_TYPE (op
) == ptr_type_node
6345 && TREE_CODE (TREE_OPERAND (op
, 0)) == ADDR_EXPR
6346 && VAR_P (TREE_OPERAND (TREE_OPERAND (op
, 0), 0))
6347 && DECL_NAME (TREE_OPERAND (TREE_OPERAND (op
, 0),
6348 0)) == heap_uninit_identifier
)
6350 tree var
= TREE_OPERAND (TREE_OPERAND (op
, 0), 0);
6351 tree var_size
= TYPE_SIZE_UNIT (TREE_TYPE (var
));
6352 tree elt_type
= TREE_TYPE (type
);
6353 tree cookie_size
= NULL_TREE
;
6354 if (TREE_CODE (elt_type
) == RECORD_TYPE
6355 && TYPE_NAME (elt_type
) == heap_identifier
)
6357 tree fld1
= TYPE_FIELDS (elt_type
);
6358 tree fld2
= DECL_CHAIN (fld1
);
6359 elt_type
= TREE_TYPE (TREE_TYPE (fld2
));
6360 cookie_size
= TYPE_SIZE_UNIT (TREE_TYPE (fld1
));
6362 DECL_NAME (var
) = heap_identifier
;
6364 = build_new_constexpr_heap_type (elt_type
, cookie_size
,
6366 TREE_TYPE (TREE_OPERAND (op
, 0))
6367 = build_pointer_type (TREE_TYPE (var
));
6370 if (op
== oldop
&& tcode
!= UNARY_PLUS_EXPR
)
6371 /* We didn't fold at the top so we could check for ptr-int
6377 /* Handle an array's bounds having been deduced after we built
6378 the wrapping expression. */
6379 if (same_type_ignoring_tlq_and_bounds_p (type
, TREE_TYPE (op
)))
6381 else if (sop
= tree_strip_nop_conversions (op
),
6382 sop
!= op
&& (same_type_ignoring_tlq_and_bounds_p
6383 (type
, TREE_TYPE (sop
))))
6385 else if (tcode
== UNARY_PLUS_EXPR
)
6386 r
= fold_convert (TREE_TYPE (t
), op
);
6388 r
= fold_build1 (tcode
, type
, op
);
6390 /* Conversion of an out-of-range value has implementation-defined
6391 behavior; the language considers it different from arithmetic
6392 overflow, which is undefined. */
6393 if (TREE_OVERFLOW_P (r
) && !TREE_OVERFLOW_P (op
))
6394 TREE_OVERFLOW (r
) = false;
6398 case EMPTY_CLASS_EXPR
:
6399 /* This is good enough for a function argument that might not get
6400 used, and they can't do anything with it, so just return it. */
6403 case STATEMENT_LIST
:
6405 new_ctx
.ctor
= new_ctx
.object
= NULL_TREE
;
6406 return cxx_eval_statement_list (&new_ctx
, t
,
6407 non_constant_p
, overflow_p
, jump_target
);
6410 return cxx_eval_constant_expression (ctx
, BIND_EXPR_BODY (t
),
6412 non_constant_p
, overflow_p
,
6415 case PREINCREMENT_EXPR
:
6416 case POSTINCREMENT_EXPR
:
6417 case PREDECREMENT_EXPR
:
6418 case POSTDECREMENT_EXPR
:
6419 return cxx_eval_increment_expression (ctx
, t
,
6420 lval
, non_constant_p
, overflow_p
);
6426 case VEC_DELETE_EXPR
:
6429 /* GCC internal stuff. */
6431 case NON_DEPENDENT_EXPR
:
6435 error_at (loc
, "expression %qE is not a constant expression", t
);
6436 *non_constant_p
= true;
6440 /* Virtual function lookup. We don't need to do anything fancy. */
6441 return cxx_eval_constant_expression (ctx
, OBJ_TYPE_REF_EXPR (t
),
6442 lval
, non_constant_p
, overflow_p
);
6444 case PLACEHOLDER_EXPR
:
6445 /* Use of the value or address of the current object. */
6446 if (tree ctor
= lookup_placeholder (ctx
, lval
, TREE_TYPE (t
)))
6448 if (TREE_CODE (ctor
) == CONSTRUCTOR
)
6451 return cxx_eval_constant_expression (ctx
, ctor
, lval
,
6452 non_constant_p
, overflow_p
);
6454 /* A placeholder without a referent. We can get here when
6455 checking whether NSDMIs are noexcept, or in massage_init_elt;
6456 just say it's non-constant for now. */
6457 gcc_assert (ctx
->quiet
);
6458 *non_constant_p
= true;
6463 tree cond
= TREE_OPERAND (t
, 0);
6464 cond
= cxx_eval_constant_expression (ctx
, cond
, /*lval*/false,
6465 non_constant_p
, overflow_p
);
6466 VERIFY_CONSTANT (cond
);
6467 if (integer_nonzerop (cond
))
6473 *jump_target
= TREE_OPERAND (t
, 0);
6474 gcc_assert (breaks (jump_target
) || continues (jump_target
)
6475 /* Allow for jumping to a cdtor_label. */
6476 || returns (jump_target
));
6483 cxx_eval_loop_expr (ctx
, t
,
6484 non_constant_p
, overflow_p
, jump_target
);
6489 cxx_eval_switch_expr (ctx
, t
,
6490 non_constant_p
, overflow_p
, jump_target
);
6494 /* It's possible to get a requires-expression in a constant
6495 expression. For example:
6497 template<typename T> concept bool C() {
6498 return requires (T t) { t; };
6501 template<typename T> requires !C<T>() void f(T);
6503 Normalization leaves f with the associated constraint
6504 '!requires (T t) { ... }' which is not transformed into
6506 if (!processing_template_decl
)
6507 return satisfy_constraint_expression (t
);
6509 *non_constant_p
= true;
6513 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
6515 non_constant_p
, overflow_p
,
6523 case TEMPLATE_ID_EXPR
:
6525 /* We can evaluate template-id that refers to a concept only if
6526 the template arguments are non-dependent. */
6527 tree id
= unpack_concept_check (t
);
6528 tree tmpl
= TREE_OPERAND (id
, 0);
6529 if (!concept_definition_p (tmpl
))
6530 internal_error ("unexpected template-id %qE", t
);
6532 if (function_concept_p (tmpl
))
6535 error_at (cp_expr_loc_or_input_loc (t
),
6536 "function concept must be called");
6537 r
= error_mark_node
;
6541 if (!processing_template_decl
6542 && !uid_sensitive_constexpr_evaluation_p ())
6543 r
= evaluate_concept_check (t
, tf_warning_or_error
);
6545 *non_constant_p
= true;
6552 inline_asm_in_constexpr_error (loc
);
6553 *non_constant_p
= true;
6557 if (STATEMENT_CODE_P (TREE_CODE (t
)))
6559 /* This function doesn't know how to deal with pre-genericize
6560 statements; this can only happen with statement-expressions,
6561 so for now just fail. */
6563 error_at (EXPR_LOCATION (t
),
6564 "statement is not a constant expression");
6567 internal_error ("unexpected expression %qE of kind %s", t
,
6568 get_tree_code_name (TREE_CODE (t
)));
6569 *non_constant_p
= true;
6573 if (r
== error_mark_node
)
6574 *non_constant_p
= true;
6576 if (*non_constant_p
)
6582 /* P0859: A function is needed for constant evaluation if it is a constexpr
6583 function that is named by an expression ([basic.def.odr]) that is
6584 potentially constant evaluated.
6586 So we need to instantiate any constexpr functions mentioned by the
6587 expression even if the definition isn't needed for evaluating the
6591 instantiate_cx_fn_r (tree
*tp
, int *walk_subtrees
, void */
*data*/
)
6593 if (TREE_CODE (*tp
) == FUNCTION_DECL
6594 && DECL_DECLARED_CONSTEXPR_P (*tp
)
6595 && !DECL_INITIAL (*tp
)
6596 && !trivial_fn_p (*tp
)
6597 && DECL_TEMPLOID_INSTANTIATION (*tp
)
6598 && !uid_sensitive_constexpr_evaluation_p ())
6601 instantiate_decl (*tp
, /*defer_ok*/false, /*expl_inst*/false);
6604 else if (TREE_CODE (*tp
) == CALL_EXPR
6605 || TREE_CODE (*tp
) == AGGR_INIT_EXPR
)
6607 if (EXPR_HAS_LOCATION (*tp
))
6608 input_location
= EXPR_LOCATION (*tp
);
6618 instantiate_constexpr_fns (tree t
)
6620 location_t loc
= input_location
;
6621 cp_walk_tree_without_duplicates (&t
, instantiate_cx_fn_r
, NULL
);
6622 input_location
= loc
;
6625 /* Look for heap variables in the expression *TP. */
6628 find_heap_var_refs (tree
*tp
, int *walk_subtrees
, void */
*data*/
)
6631 && (DECL_NAME (*tp
) == heap_uninit_identifier
6632 || DECL_NAME (*tp
) == heap_identifier
6633 || DECL_NAME (*tp
) == heap_deleted_identifier
))
6641 /* Find immediate function decls in *TP if any. */
6644 find_immediate_fndecl (tree
*tp
, int */
*walk_subtrees*/
, void */
*data*/
)
6646 if (TREE_CODE (*tp
) == FUNCTION_DECL
&& DECL_IMMEDIATE_FUNCTION_P (*tp
))
6651 /* ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
6652 STRICT has the same sense as for constant_value_1: true if we only allow
6653 conforming C++ constant expressions, or false if we want a constant value
6654 even if it doesn't conform.
6655 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
6656 per P0595 even when ALLOW_NON_CONSTANT is true.
6657 CONSTEXPR_DTOR is true when evaluating the dtor of a constexpr variable.
6658 OBJECT must be non-NULL in that case. */
6661 cxx_eval_outermost_constant_expr (tree t
, bool allow_non_constant
,
6663 bool manifestly_const_eval
= false,
6664 bool constexpr_dtor
= false,
6665 tree object
= NULL_TREE
)
6667 auto_timevar
time (TV_CONSTEXPR
);
6669 bool non_constant_p
= false;
6670 bool overflow_p
= false;
6672 if (BRACE_ENCLOSED_INITIALIZER_P (t
))
6674 gcc_checking_assert (allow_non_constant
);
6678 constexpr_global_ctx global_ctx
;
6679 constexpr_ctx ctx
= { &global_ctx
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
6680 allow_non_constant
, strict
,
6681 manifestly_const_eval
|| !allow_non_constant
};
6683 /* Turn off -frounding-math for manifestly constant evaluation. */
6684 warning_sentinel
rm (flag_rounding_math
, ctx
.manifestly_const_eval
);
6685 tree type
= initialized_type (t
);
6687 bool is_consteval
= false;
6688 if (VOID_TYPE_P (type
))
6691 /* Used for destructors of array elements. */
6692 type
= TREE_TYPE (object
);
6695 if (cxx_dialect
< cxx20
)
6697 if (TREE_CODE (t
) != CALL_EXPR
&& TREE_CODE (t
) != AGGR_INIT_EXPR
)
6699 /* Calls to immediate functions returning void need to be
6701 tree fndecl
= cp_get_callee_fndecl_nofold (t
);
6702 if (fndecl
== NULL_TREE
|| !DECL_IMMEDIATE_FUNCTION_P (fndecl
))
6705 is_consteval
= true;
6708 else if (cxx_dialect
>= cxx20
6709 && (TREE_CODE (t
) == CALL_EXPR
6710 || TREE_CODE (t
) == AGGR_INIT_EXPR
6711 || TREE_CODE (t
) == TARGET_EXPR
))
6713 /* For non-concept checks, determine if it is consteval. */
6714 if (!concept_check_p (t
))
6717 if (TREE_CODE (x
) == TARGET_EXPR
)
6718 x
= TARGET_EXPR_INITIAL (x
);
6719 tree fndecl
= cp_get_callee_fndecl_nofold (x
);
6720 if (fndecl
&& DECL_IMMEDIATE_FUNCTION_P (fndecl
))
6721 is_consteval
= true;
6724 if (AGGREGATE_TYPE_P (type
) || VECTOR_TYPE_P (type
))
6726 /* In C++14 an NSDMI can participate in aggregate initialization,
6727 and can refer to the address of the object being initialized, so
6728 we need to pass in the relevant VAR_DECL if we want to do the
6729 evaluation in a single pass. The evaluation will dynamically
6730 update ctx.values for the VAR_DECL. We use the same strategy
6731 for C++11 constexpr constructors that refer to the object being
6735 gcc_assert (object
&& VAR_P (object
));
6736 gcc_assert (DECL_DECLARED_CONSTEXPR_P (object
));
6737 gcc_assert (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object
));
6738 if (error_operand_p (DECL_INITIAL (object
)))
6740 ctx
.ctor
= unshare_expr (DECL_INITIAL (object
));
6741 TREE_READONLY (ctx
.ctor
) = false;
6742 /* Temporarily force decl_really_constant_value to return false
6743 for it, we want to use ctx.ctor for the current value instead. */
6744 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object
) = false;
6748 ctx
.ctor
= build_constructor (type
, NULL
);
6749 CONSTRUCTOR_NO_CLEARING (ctx
.ctor
) = true;
6753 if (TREE_CODE (t
) == TARGET_EXPR
)
6754 object
= TARGET_EXPR_SLOT (t
);
6755 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
)
6756 object
= AGGR_INIT_EXPR_SLOT (t
);
6758 ctx
.object
= object
;
6760 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6761 (type
, TREE_TYPE (object
)));
6762 if (object
&& DECL_P (object
))
6763 global_ctx
.values
.put (object
, ctx
.ctor
);
6764 if (TREE_CODE (r
) == TARGET_EXPR
)
6765 /* Avoid creating another CONSTRUCTOR when we expand the
6767 r
= TARGET_EXPR_INITIAL (r
);
6770 auto_vec
<tree
, 16> cleanups
;
6771 global_ctx
.cleanups
= &cleanups
;
6773 instantiate_constexpr_fns (r
);
6774 r
= cxx_eval_constant_expression (&ctx
, r
,
6775 false, &non_constant_p
, &overflow_p
);
6777 if (!constexpr_dtor
)
6778 verify_constant (r
, allow_non_constant
, &non_constant_p
, &overflow_p
);
6780 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object
) = true;
6784 /* Evaluate the cleanups. */
6785 FOR_EACH_VEC_ELT_REVERSE (cleanups
, i
, cleanup
)
6786 cxx_eval_constant_expression (&ctx
, cleanup
, false,
6787 &non_constant_p
, &overflow_p
);
6789 /* Mutable logic is a bit tricky: we want to allow initialization of
6790 constexpr variables with mutable members, but we can't copy those
6791 members to another constexpr variable. */
6792 if (TREE_CODE (r
) == CONSTRUCTOR
&& CONSTRUCTOR_MUTABLE_POISON (r
))
6794 if (!allow_non_constant
)
6795 error ("%qE is not a constant expression because it refers to "
6796 "mutable subobjects of %qT", t
, type
);
6797 non_constant_p
= true;
6800 if (TREE_CODE (r
) == CONSTRUCTOR
&& CONSTRUCTOR_NO_CLEARING (r
))
6802 if (!allow_non_constant
)
6803 error ("%qE is not a constant expression because it refers to "
6804 "an incompletely initialized variable", t
);
6805 TREE_CONSTANT (r
) = false;
6806 non_constant_p
= true;
6809 if (!global_ctx
.heap_vars
.is_empty ())
6811 tree heap_var
= cp_walk_tree_without_duplicates (&r
, find_heap_var_refs
,
6816 if (!allow_non_constant
&& !non_constant_p
)
6817 error_at (DECL_SOURCE_LOCATION (heap_var
),
6818 "%qE is not a constant expression because it refers to "
6819 "a result of %<operator new%>", t
);
6821 non_constant_p
= true;
6823 FOR_EACH_VEC_ELT (global_ctx
.heap_vars
, i
, heap_var
)
6824 if (DECL_NAME (heap_var
) != heap_deleted_identifier
)
6826 if (!allow_non_constant
&& !non_constant_p
)
6827 error_at (DECL_SOURCE_LOCATION (heap_var
),
6828 "%qE is not a constant expression because allocated "
6829 "storage has not been deallocated", t
);
6831 non_constant_p
= true;
6835 /* Check that immediate invocation does not return an expression referencing
6836 any immediate function decls. They need to be allowed while parsing
6837 immediate functions, but can't leak outside of them. */
6840 && (current_function_decl
== NULL_TREE
6841 || !DECL_IMMEDIATE_FUNCTION_P (current_function_decl
)))
6842 if (tree immediate_fndecl
6843 = cp_walk_tree_without_duplicates (&r
, find_immediate_fndecl
,
6846 if (!allow_non_constant
&& !non_constant_p
)
6847 error_at (cp_expr_loc_or_input_loc (t
),
6848 "immediate evaluation returns address of immediate "
6849 "function %qD", immediate_fndecl
);
6851 non_constant_p
= true;
6855 if (!non_constant_p
&& overflow_p
)
6856 non_constant_p
= true;
6858 /* Unshare the result. */
6859 bool should_unshare
= true;
6860 if (r
== t
|| (TREE_CODE (t
) == TARGET_EXPR
6861 && TARGET_EXPR_INITIAL (t
) == r
))
6862 should_unshare
= false;
6864 if (non_constant_p
&& !allow_non_constant
)
6865 return error_mark_node
;
6866 else if (constexpr_dtor
)
6868 else if (non_constant_p
&& TREE_CONSTANT (r
))
6870 /* If __builtin_is_constant_evaluated () was evaluated to true
6871 and the result is not a valid constant expression, we need to
6873 if (manifestly_const_eval
)
6874 return cxx_eval_outermost_constant_expr (t
, true, strict
,
6875 false, false, object
);
6876 /* This isn't actually constant, so unset TREE_CONSTANT.
6877 Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires
6878 it to be set if it is invariant address, even when it is not
6879 a valid C++ constant expression. Wrap it with a NOP_EXPR
6881 if (EXPR_P (r
) && TREE_CODE (r
) != ADDR_EXPR
)
6883 else if (TREE_CODE (r
) == CONSTRUCTOR
)
6884 r
= build1 (VIEW_CONVERT_EXPR
, TREE_TYPE (r
), r
);
6886 r
= build_nop (TREE_TYPE (r
), r
);
6887 TREE_CONSTANT (r
) = false;
6889 else if (non_constant_p
)
6893 r
= unshare_expr (r
);
6895 if (TREE_CODE (r
) == CONSTRUCTOR
&& CLASS_TYPE_P (TREE_TYPE (r
)))
6897 r
= adjust_temp_type (type
, r
);
6898 if (TREE_CODE (t
) == TARGET_EXPR
6899 && TARGET_EXPR_INITIAL (t
) == r
)
6901 else if (TREE_CODE (t
) != CONSTRUCTOR
)
6903 r
= get_target_expr_sfinae (r
, tf_warning_or_error
| tf_no_cleanup
);
6904 TREE_CONSTANT (r
) = true;
6911 /* If T represents a constant expression returns its reduced value.
6912 Otherwise return error_mark_node. If T is dependent, then
6916 cxx_constant_value (tree t
, tree decl
)
6918 return cxx_eval_outermost_constant_expr (t
, false, true, true, false, decl
);
6921 /* Like cxx_constant_value, but used for evaluation of constexpr destructors
6922 of constexpr variables. The actual initializer of DECL is not modified. */
6925 cxx_constant_dtor (tree t
, tree decl
)
6927 cxx_eval_outermost_constant_expr (t
, false, true, true, true, decl
);
6930 /* Helper routine for fold_simple function. Either return simplified
6931 expression T, otherwise NULL_TREE.
6932 In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold
6933 even if we are within template-declaration. So be careful on call, as in
6934 such case types can be undefined. */
6937 fold_simple_1 (tree t
)
6940 enum tree_code code
= TREE_CODE (t
);
6952 return fold_sizeof_expr (t
);
6961 case TRUTH_NOT_EXPR
:
6963 case VIEW_CONVERT_EXPR
:
6966 case FIX_TRUNC_EXPR
:
6967 case FIXED_CONVERT_EXPR
:
6968 case ADDR_SPACE_CONVERT_EXPR
:
6970 op1
= TREE_OPERAND (t
, 0);
6972 t
= const_unop (code
, TREE_TYPE (t
), op1
);
6976 if (CONVERT_EXPR_CODE_P (code
)
6977 && TREE_OVERFLOW_P (t
) && !TREE_OVERFLOW_P (op1
))
6978 TREE_OVERFLOW (t
) = false;
6986 /* If T is a simple constant expression, returns its simplified value.
6987 Otherwise returns T. In contrast to maybe_constant_value we
6988 simplify only few operations on constant-expressions, and we don't
6989 try to simplify constexpressions. */
6992 fold_simple (tree t
)
6994 if (processing_template_decl
)
6997 tree r
= fold_simple_1 (t
);
7004 /* If T is a constant expression, returns its reduced value.
7005 Otherwise, if T does not have TREE_CONSTANT set, returns T.
7006 Otherwise, returns a version of T without TREE_CONSTANT.
7007 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated
7010 static GTY((deletable
)) hash_map
<tree
, tree
> *cv_cache
;
7013 maybe_constant_value (tree t
, tree decl
, bool manifestly_const_eval
)
7017 if (!is_nondependent_constant_expression (t
))
7019 if (TREE_OVERFLOW_P (t
))
7021 t
= build_nop (TREE_TYPE (t
), t
);
7022 TREE_CONSTANT (t
) = false;
7026 else if (CONSTANT_CLASS_P (t
))
7027 /* No caching or evaluation needed. */
7030 if (manifestly_const_eval
)
7031 return cxx_eval_outermost_constant_expr (t
, true, true, true, false, decl
);
7033 if (cv_cache
== NULL
)
7034 cv_cache
= hash_map
<tree
, tree
>::create_ggc (101);
7035 if (tree
*cached
= cv_cache
->get (t
))
7040 r
= break_out_target_exprs (r
, /*clear_loc*/true);
7041 protected_set_expr_location (r
, EXPR_LOCATION (t
));
7046 uid_sensitive_constexpr_evaluation_checker c
;
7047 r
= cxx_eval_outermost_constant_expr (t
, true, true, false, false, decl
);
7048 gcc_checking_assert (r
== t
7049 || CONVERT_EXPR_P (t
)
7050 || TREE_CODE (t
) == VIEW_CONVERT_EXPR
7051 || (TREE_CONSTANT (t
) && !TREE_CONSTANT (r
))
7052 || !cp_tree_equal (r
, t
));
7053 if (!c
.evaluation_restricted_p ())
7054 cv_cache
->put (t
, r
);
7058 /* Dispose of the whole CV_CACHE. */
7061 clear_cv_cache (void)
7063 if (cv_cache
!= NULL
)
7067 /* Dispose of the whole CV_CACHE, FOLD_CACHE, and satisfaction caches. */
7070 clear_cv_and_fold_caches (bool sat
/*= true*/)
7073 clear_fold_cache ();
7075 clear_satisfaction_cache ();
7078 /* Internal function handling expressions in templates for
7079 fold_non_dependent_expr and fold_non_dependent_init.
7081 If we're in a template, but T isn't value dependent, simplify
7082 it. We're supposed to treat:
7084 template <typename T> void f(T[1 + 1]);
7085 template <typename T> void f(T[2]);
7087 as two declarations of the same function, for example. */
7090 fold_non_dependent_expr_template (tree t
, tsubst_flags_t complain
,
7091 bool manifestly_const_eval
,
7094 gcc_assert (processing_template_decl
);
7096 if (is_nondependent_constant_expression (t
))
7098 processing_template_decl_sentinel s
;
7099 t
= instantiate_non_dependent_expr_internal (t
, complain
);
7101 if (type_unknown_p (t
) || BRACE_ENCLOSED_INITIALIZER_P (t
))
7103 if (TREE_OVERFLOW_P (t
))
7105 t
= build_nop (TREE_TYPE (t
), t
);
7106 TREE_CONSTANT (t
) = false;
7111 tree r
= cxx_eval_outermost_constant_expr (t
, true, true,
7112 manifestly_const_eval
,
7114 /* cp_tree_equal looks through NOPs, so allow them. */
7115 gcc_checking_assert (r
== t
7116 || CONVERT_EXPR_P (t
)
7117 || TREE_CODE (t
) == VIEW_CONVERT_EXPR
7118 || (TREE_CONSTANT (t
) && !TREE_CONSTANT (r
))
7119 || !cp_tree_equal (r
, t
));
7122 else if (TREE_OVERFLOW_P (t
))
7124 t
= build_nop (TREE_TYPE (t
), t
);
7125 TREE_CONSTANT (t
) = false;
7131 /* Like maybe_constant_value but first fully instantiate the argument.
7133 Note: this is equivalent to instantiate_non_dependent_expr_sfinae
7134 (t, complain) followed by maybe_constant_value but is more efficient,
7135 because it calls instantiation_dependent_expression_p and
7136 potential_constant_expression at most once.
7137 The manifestly_const_eval argument is passed to maybe_constant_value.
7139 Callers should generally pass their active complain, or if they are in a
7140 non-template, diagnosing context, they can use the default of
7141 tf_warning_or_error. Callers that might be within a template context, don't
7142 have a complain parameter, and aren't going to remember the result for long
7143 (e.g. null_ptr_cst_p), can pass tf_none and deal with error_mark_node
7147 fold_non_dependent_expr (tree t
,
7148 tsubst_flags_t complain
/* = tf_warning_or_error */,
7149 bool manifestly_const_eval
/* = false */,
7150 tree object
/* = NULL_TREE */)
7155 if (processing_template_decl
)
7156 return fold_non_dependent_expr_template (t
, complain
,
7157 manifestly_const_eval
, object
);
7159 return maybe_constant_value (t
, object
, manifestly_const_eval
);
7162 /* Like fold_non_dependent_expr, but if EXPR couldn't be folded to a constant,
7163 return the original expression. */
7166 maybe_fold_non_dependent_expr (tree expr
,
7167 tsubst_flags_t complain
/*=tf_warning_or_error*/)
7169 tree t
= fold_non_dependent_expr (expr
, complain
);
7170 if (t
&& TREE_CONSTANT (t
))
7176 /* Like maybe_constant_init but first fully instantiate the argument. */
7179 fold_non_dependent_init (tree t
,
7180 tsubst_flags_t complain
/*=tf_warning_or_error*/,
7181 bool manifestly_const_eval
/*=false*/)
7186 if (processing_template_decl
)
7188 t
= fold_non_dependent_expr_template (t
, complain
,
7189 manifestly_const_eval
, NULL_TREE
);
7190 /* maybe_constant_init does this stripping, so do it here too. */
7191 if (TREE_CODE (t
) == TARGET_EXPR
)
7193 tree init
= TARGET_EXPR_INITIAL (t
);
7194 if (TREE_CODE (init
) == CONSTRUCTOR
)
7200 return maybe_constant_init (t
, NULL_TREE
, manifestly_const_eval
);
7203 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
7204 than wrapped in a TARGET_EXPR.
7205 ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
7206 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
7207 per P0595 even when ALLOW_NON_CONSTANT is true. */
7210 maybe_constant_init_1 (tree t
, tree decl
, bool allow_non_constant
,
7211 bool manifestly_const_eval
)
7215 if (TREE_CODE (t
) == EXPR_STMT
)
7216 t
= TREE_OPERAND (t
, 0);
7217 if (TREE_CODE (t
) == CONVERT_EXPR
7218 && VOID_TYPE_P (TREE_TYPE (t
)))
7219 t
= TREE_OPERAND (t
, 0);
7220 if (TREE_CODE (t
) == INIT_EXPR
)
7221 t
= TREE_OPERAND (t
, 1);
7222 if (TREE_CODE (t
) == TARGET_EXPR
)
7223 t
= TARGET_EXPR_INITIAL (t
);
7224 if (!is_nondependent_static_init_expression (t
))
7225 /* Don't try to evaluate it. */;
7226 else if (CONSTANT_CLASS_P (t
) && allow_non_constant
)
7227 /* No evaluation needed. */;
7229 t
= cxx_eval_outermost_constant_expr (t
, allow_non_constant
,
7231 manifestly_const_eval
, false, decl
);
7232 if (TREE_CODE (t
) == TARGET_EXPR
)
7234 tree init
= TARGET_EXPR_INITIAL (t
);
7235 if (TREE_CODE (init
) == CONSTRUCTOR
)
7241 /* Wrapper for maybe_constant_init_1 which permits non constants. */
7244 maybe_constant_init (tree t
, tree decl
, bool manifestly_const_eval
)
7246 return maybe_constant_init_1 (t
, decl
, true, manifestly_const_eval
);
7249 /* Wrapper for maybe_constant_init_1 which does not permit non constants. */
7252 cxx_constant_init (tree t
, tree decl
)
7254 return maybe_constant_init_1 (t
, decl
, false, true);
7258 /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
7259 /* Return true if the object referred to by REF has automatic or thread
7262 enum { ck_ok
, ck_bad
, ck_unknown
};
7264 check_automatic_or_tls (tree ref
)
7267 poly_int64 bitsize
, bitpos
;
7269 int volatilep
= 0, unsignedp
= 0;
7270 tree decl
= get_inner_reference (ref
, &bitsize
, &bitpos
, &offset
,
7271 &mode
, &unsignedp
, &volatilep
, false);
7274 /* If there isn't a decl in the middle, we don't know the linkage here,
7275 and this isn't a constant expression anyway. */
7278 dk
= decl_storage_duration (decl
);
7279 return (dk
== dk_auto
|| dk
== dk_thread
) ? ck_bad
: ck_ok
;
7283 /* Data structure for passing data from potential_constant_expression_1
7284 to check_for_return_continue via cp_walk_tree. */
7285 struct check_for_return_continue_data
{
7286 hash_set
<tree
> *pset
;
7290 /* Helper function for potential_constant_expression_1 SWITCH_STMT handling,
7291 called through cp_walk_tree. Return the first RETURN_EXPR found, or note
7292 the first CONTINUE_STMT if RETURN_EXPR is not found. */
7294 check_for_return_continue (tree
*tp
, int *walk_subtrees
, void *data
)
7297 check_for_return_continue_data
*d
= (check_for_return_continue_data
*) data
;
7298 switch (TREE_CODE (t
))
7304 if (d
->continue_stmt
== NULL_TREE
)
7305 d
->continue_stmt
= t
;
7309 if (tree r = cp_walk_tree (&x, check_for_return_continue, data, \
7313 /* For loops, walk subtrees manually, so that continue stmts found
7314 inside of the bodies of the loops are ignored. */
7317 RECUR (DO_COND (t
));
7318 s
= d
->continue_stmt
;
7319 RECUR (DO_BODY (t
));
7320 d
->continue_stmt
= s
;
7325 RECUR (WHILE_COND (t
));
7326 s
= d
->continue_stmt
;
7327 RECUR (WHILE_BODY (t
));
7328 d
->continue_stmt
= s
;
7333 RECUR (FOR_INIT_STMT (t
));
7334 RECUR (FOR_COND (t
));
7335 RECUR (FOR_EXPR (t
));
7336 s
= d
->continue_stmt
;
7337 RECUR (FOR_BODY (t
));
7338 d
->continue_stmt
= s
;
7341 case RANGE_FOR_STMT
:
7343 RECUR (RANGE_FOR_EXPR (t
));
7344 s
= d
->continue_stmt
;
7345 RECUR (RANGE_FOR_BODY (t
));
7346 d
->continue_stmt
= s
;
7350 case STATEMENT_LIST
:
7363 /* Return true if T denotes a potentially constant expression. Issue
7364 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
7365 an lvalue-rvalue conversion is implied. If NOW is true, we want to
7366 consider the expression in the current context, independent of constexpr
7369 C++0x [expr.const] used to say
7371 6 An expression is a potential constant expression if it is
7372 a constant expression where all occurrences of function
7373 parameters are replaced by arbitrary constant expressions
7374 of the appropriate type.
7376 2 A conditional expression is a constant expression unless it
7377 involves one of the following as a potentially evaluated
7378 subexpression (3.2), but subexpressions of logical AND (5.14),
7379 logical OR (5.15), and conditional (5.16) operations that are
7380 not evaluated are not considered. */
7383 potential_constant_expression_1 (tree t
, bool want_rval
, bool strict
, bool now
,
7384 tsubst_flags_t flags
, tree
*jump_target
)
7386 #define RECUR(T,RV) \
7387 potential_constant_expression_1 ((T), (RV), strict, now, flags, jump_target)
7389 enum { any
= false, rval
= true };
7393 if (t
== error_mark_node
)
7397 location_t loc
= cp_expr_loc_or_input_loc (t
);
7400 /* If we are jumping, ignore everything. This is simpler than the
7401 cxx_eval_constant_expression handling because we only need to be
7402 conservatively correct, and we don't necessarily have a constant value
7403 available, so we don't bother with switch tracking. */
7406 if (TREE_THIS_VOLATILE (t
) && want_rval
)
7408 if (flags
& tf_error
)
7409 error_at (loc
, "lvalue-to-rvalue conversion of a volatile lvalue "
7410 "%qE with type %qT", t
, TREE_TYPE (t
));
7413 if (CONSTANT_CLASS_P (t
))
7415 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_TYPED
)
7416 && TREE_TYPE (t
) == error_mark_node
)
7419 switch (TREE_CODE (t
))
7425 case TEMPLATE_ID_EXPR
:
7428 case CASE_LABEL_EXPR
:
7435 case TEMPLATE_PARM_INDEX
:
7437 case IDENTIFIER_NODE
:
7438 case USERDEF_LITERAL
:
7439 /* We can see a FIELD_DECL in a pointer-to-member expression. */
7444 case PLACEHOLDER_EXPR
:
7447 case DEBUG_BEGIN_STMT
:
7451 if (!RECUR (TREE_OPERAND (t
, 0), any
))
7461 if (now
&& want_rval
)
7463 tree type
= TREE_TYPE (t
);
7464 if ((processing_template_decl
&& !COMPLETE_TYPE_P (type
))
7465 || dependent_type_p (type
)
7466 || is_really_empty_class (type
, /*ignore_vptr*/false))
7467 /* An empty class has no data to read. */
7469 if (flags
& tf_error
)
7470 error ("%qE is not a constant expression", t
);
7475 case AGGR_INIT_EXPR
:
7477 /* -- an invocation of a function other than a constexpr function
7478 or a constexpr constructor. */
7480 tree fun
= get_function_named_in_call (t
);
7481 const int nargs
= call_expr_nargs (t
);
7484 if (fun
== NULL_TREE
)
7486 /* Reset to allow the function to continue past the end
7487 of the block below. Otherwise return early. */
7490 if (TREE_CODE (t
) == CALL_EXPR
7491 && CALL_EXPR_FN (t
) == NULL_TREE
)
7492 switch (CALL_EXPR_IFN (t
))
7494 /* These should be ignored, they are optimized away from
7495 constexpr functions. */
7496 case IFN_UBSAN_NULL
:
7497 case IFN_UBSAN_BOUNDS
:
7498 case IFN_UBSAN_VPTR
:
7499 case IFN_FALLTHROUGH
:
7502 case IFN_ADD_OVERFLOW
:
7503 case IFN_SUB_OVERFLOW
:
7504 case IFN_MUL_OVERFLOW
:
7506 case IFN_VEC_CONVERT
:
7516 /* fold_call_expr can't do anything with IFN calls. */
7517 if (flags
& tf_error
)
7518 error_at (loc
, "call to internal function %qE", t
);
7523 if (fun
&& is_overloaded_fn (fun
))
7525 if (TREE_CODE (fun
) == FUNCTION_DECL
)
7527 if (builtin_valid_in_constant_expr_p (fun
))
7529 if (!DECL_DECLARED_CONSTEXPR_P (fun
)
7530 /* Allow any built-in function; if the expansion
7531 isn't constant, we'll deal with that then. */
7532 && !fndecl_built_in_p (fun
)
7533 /* In C++20, replaceable global allocation functions
7534 are constant expressions. */
7535 && (!cxx_replaceable_global_alloc_fn (fun
)
7536 || TREE_CODE (t
) != CALL_EXPR
7537 || (!CALL_FROM_NEW_OR_DELETE_P (t
)
7538 && (current_function_decl
== NULL_TREE
7539 || !is_std_allocator_allocate
7540 (current_function_decl
))))
7541 /* Allow placement new in std::construct_at. */
7542 && (!cxx_placement_new_fn (fun
)
7543 || TREE_CODE (t
) != CALL_EXPR
7544 || current_function_decl
== NULL_TREE
7545 || !is_std_construct_at (current_function_decl
))
7546 && !cxx_dynamic_cast_fn_p (fun
))
7548 if (flags
& tf_error
)
7550 error_at (loc
, "call to non-%<constexpr%> function %qD",
7552 explain_invalid_constexpr_fn (fun
);
7556 /* A call to a non-static member function takes the address
7557 of the object as the first argument. But in a constant
7558 expression the address will be folded away, so look
7560 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun
)
7561 && !DECL_CONSTRUCTOR_P (fun
))
7563 tree x
= get_nth_callarg (t
, 0);
7564 if (is_this_parameter (x
))
7566 /* Don't require an immediately constant value, as
7567 constexpr substitution might not use the value. */
7568 bool sub_now
= false;
7569 if (!potential_constant_expression_1 (x
, rval
, strict
,
7578 if (!RECUR (fun
, true))
7580 fun
= get_first_fn (fun
);
7582 /* Skip initial arguments to base constructors. */
7583 if (DECL_BASE_CONSTRUCTOR_P (fun
))
7584 i
= num_artificial_parms_for (fun
);
7585 fun
= DECL_ORIGIN (fun
);
7589 if (RECUR (fun
, rval
))
7590 /* Might end up being a constant function pointer. */;
7594 for (; i
< nargs
; ++i
)
7596 tree x
= get_nth_callarg (t
, i
);
7597 /* In a template, reference arguments haven't been converted to
7598 REFERENCE_TYPE and we might not even know if the parameter
7599 is a reference, so accept lvalue constants too. */
7600 bool rv
= processing_template_decl
? any
: rval
;
7601 /* Don't require an immediately constant value, as constexpr
7602 substitution might not use the value of the argument. */
7603 bool sub_now
= false;
7604 if (!potential_constant_expression_1 (x
, rv
, strict
,
7605 sub_now
, flags
, jump_target
))
7611 case NON_LVALUE_EXPR
:
7612 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
7613 -- an lvalue of integral type that refers to a non-volatile
7614 const variable or static data member initialized with
7615 constant expressions, or
7617 -- an lvalue of literal type that refers to non-volatile
7618 object defined with constexpr, or that refers to a
7619 sub-object of such an object; */
7620 return RECUR (TREE_OPERAND (t
, 0), rval
);
7623 if (DECL_HAS_VALUE_EXPR_P (t
))
7625 if (now
&& is_normal_capture_proxy (t
))
7627 /* -- in a lambda-expression, a reference to this or to a
7628 variable with automatic storage duration defined outside that
7629 lambda-expression, where the reference would be an
7633 /* Since we're doing an lvalue-rvalue conversion, this might
7634 not be an odr-use, so evaluate the variable directly. */
7635 return RECUR (DECL_CAPTURED_VARIABLE (t
), rval
);
7637 if (flags
& tf_error
)
7639 tree cap
= DECL_CAPTURED_VARIABLE (t
);
7640 error ("lambda capture of %qE is not a constant expression",
7642 if (decl_constant_var_p (cap
))
7643 inform (input_location
, "because it is used as a glvalue");
7647 return RECUR (DECL_VALUE_EXPR (t
), rval
);
7650 && !var_in_maybe_constexpr_fn (t
)
7651 && !type_dependent_expression_p (t
)
7652 && !decl_maybe_constant_var_p (t
)
7654 || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t
))
7655 || (DECL_INITIAL (t
)
7656 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t
)))
7657 && COMPLETE_TYPE_P (TREE_TYPE (t
))
7658 && !is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
7660 if (flags
& tf_error
)
7661 non_const_var_error (loc
, t
);
7667 if (REINTERPRET_CAST_P (t
))
7669 if (flags
& tf_error
)
7670 error_at (loc
, "%<reinterpret_cast%> is not a constant expression");
7675 case VIEW_CONVERT_EXPR
:
7676 /* -- a reinterpret_cast. FIXME not implemented, and this rule
7677 may change to something more specific to type-punning (DR 1312). */
7679 tree from
= TREE_OPERAND (t
, 0);
7680 if (location_wrapper_p (t
))
7681 return (RECUR (from
, want_rval
));
7682 if (INDIRECT_TYPE_P (TREE_TYPE (t
)))
7684 STRIP_ANY_LOCATION_WRAPPER (from
);
7685 if (TREE_CODE (from
) == INTEGER_CST
7686 && !integer_zerop (from
))
7688 if (flags
& tf_error
)
7690 "%<reinterpret_cast%> from integer to pointer");
7694 return (RECUR (from
, TREE_CODE (t
) != VIEW_CONVERT_EXPR
));
7697 case ADDRESSOF_EXPR
:
7698 /* This is like ADDR_EXPR, except it won't form pointer-to-member. */
7699 t
= TREE_OPERAND (t
, 0);
7700 goto handle_addr_expr
;
7703 /* -- a unary operator & that is applied to an lvalue that
7704 designates an object with thread or automatic storage
7706 t
= TREE_OPERAND (t
, 0);
7708 if (TREE_CODE (t
) == OFFSET_REF
&& PTRMEM_OK_P (t
))
7709 /* A pointer-to-member constant. */
7714 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
7715 any checking here, as we might dereference the pointer later. If
7716 we remove this code, also remove check_automatic_or_tls. */
7717 i
= check_automatic_or_tls (t
);
7722 if (flags
& tf_error
)
7723 error ("address-of an object %qE with thread local or "
7724 "automatic storage is not a constant expression", t
);
7728 return RECUR (t
, any
);
7733 /* -- a class member access unless its postfix-expression is
7734 of literal type or of pointer to literal type. */
7735 /* This test would be redundant, as it follows from the
7736 postfix-expression being a potential constant expression. */
7737 if (type_unknown_p (t
))
7739 if (is_overloaded_fn (t
))
7740 /* In a template, a COMPONENT_REF of a function expresses ob.fn(),
7741 which uses ob as an lvalue. */
7748 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
7750 case EXPR_PACK_EXPANSION
:
7751 return RECUR (PACK_EXPANSION_PATTERN (t
), want_rval
);
7755 tree x
= TREE_OPERAND (t
, 0);
7757 if (is_this_parameter (x
) && !is_capture_proxy (x
))
7759 if (!var_in_maybe_constexpr_fn (x
))
7761 if (flags
& tf_error
)
7762 error_at (loc
, "use of %<this%> in a constant expression");
7767 return RECUR (x
, rval
);
7770 case STATEMENT_LIST
:
7772 tree_stmt_iterator i
;
7773 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
7775 if (!RECUR (tsi_stmt (i
), any
))
7783 if (cxx_dialect
< cxx14
)
7785 if (!RECUR (TREE_OPERAND (t
, 0), any
))
7787 /* Just ignore clobbers. */
7788 if (TREE_CLOBBER_P (TREE_OPERAND (t
, 1)))
7790 if (!RECUR (TREE_OPERAND (t
, 1), rval
))
7795 if (cxx_dialect
< cxx14
)
7797 if (!RECUR (TREE_OPERAND (t
, 0), rval
))
7799 if (!RECUR (TREE_OPERAND (t
, 2), rval
))
7804 if (!RECUR (DO_COND (t
), rval
))
7806 if (!RECUR (DO_BODY (t
), any
))
7808 if (breaks (jump_target
) || continues (jump_target
))
7809 *jump_target
= NULL_TREE
;
7813 if (!RECUR (FOR_INIT_STMT (t
), any
))
7816 if (!RECUR (tmp
, rval
))
7820 if (!processing_template_decl
)
7821 tmp
= cxx_eval_outermost_constant_expr (tmp
, true);
7822 /* If we couldn't evaluate the condition, it might not ever be
7824 if (!integer_onep (tmp
))
7827 if (!RECUR (FOR_EXPR (t
), any
))
7829 if (!RECUR (FOR_BODY (t
), any
))
7831 if (breaks (jump_target
) || continues (jump_target
))
7832 *jump_target
= NULL_TREE
;
7835 case RANGE_FOR_STMT
:
7836 if (!RECUR (RANGE_FOR_INIT_STMT (t
), any
))
7838 if (!RECUR (RANGE_FOR_EXPR (t
), any
))
7840 if (!RECUR (RANGE_FOR_BODY (t
), any
))
7842 if (breaks (jump_target
) || continues (jump_target
))
7843 *jump_target
= NULL_TREE
;
7847 tmp
= WHILE_COND (t
);
7848 if (!RECUR (tmp
, rval
))
7850 if (!processing_template_decl
)
7851 tmp
= cxx_eval_outermost_constant_expr (tmp
, true);
7852 /* If we couldn't evaluate the condition, it might not ever be true. */
7853 if (!integer_onep (tmp
))
7855 if (!RECUR (WHILE_BODY (t
), any
))
7857 if (breaks (jump_target
) || continues (jump_target
))
7858 *jump_target
= NULL_TREE
;
7862 if (!RECUR (SWITCH_STMT_COND (t
), rval
))
7864 /* FIXME we don't check SWITCH_STMT_BODY currently, because even
7865 unreachable labels would be checked and it is enough if there is
7866 a single switch cond value for which it is a valid constant
7867 expression. We need to check if there are any RETURN_EXPRs
7868 or CONTINUE_STMTs inside of the body though, as in that case
7869 we need to set *jump_target. */
7872 hash_set
<tree
> pset
;
7873 check_for_return_continue_data data
= { &pset
, NULL_TREE
};
7875 = cp_walk_tree (&SWITCH_STMT_BODY (t
), check_for_return_continue
,
7877 /* The switch might return. */
7878 *jump_target
= ret_expr
;
7879 else if (data
.continue_stmt
)
7880 /* The switch can't return, but might continue. */
7881 *jump_target
= data
.continue_stmt
;
7886 return RECUR (STMT_EXPR_STMT (t
), rval
);
7889 if (cxx_dialect
>= cxx17
)
7890 /* In C++17 lambdas can be constexpr, don't give up yet. */
7892 else if (flags
& tf_error
)
7893 error_at (loc
, "lambda-expression is not a constant expression "
7897 case DYNAMIC_CAST_EXPR
:
7898 case PSEUDO_DTOR_EXPR
:
7902 case VEC_DELETE_EXPR
:
7908 case OMP_DISTRIBUTE
:
7912 case OMP_TARGET_DATA
:
7921 case OMP_TARGET_UPDATE
:
7922 case OMP_TARGET_ENTER_DATA
:
7923 case OMP_TARGET_EXIT_DATA
:
7925 case OMP_ATOMIC_READ
:
7926 case OMP_ATOMIC_CAPTURE_OLD
:
7927 case OMP_ATOMIC_CAPTURE_NEW
:
7933 case OACC_HOST_DATA
:
7937 case OACC_ENTER_DATA
:
7938 case OACC_EXIT_DATA
:
7940 /* GCC internal stuff. */
7942 case TRANSACTION_EXPR
:
7943 case AT_ENCODE_EXPR
:
7945 if (flags
& tf_error
)
7946 error_at (loc
, "expression %qE is not a constant expression", t
);
7950 if (flags
& tf_error
)
7951 inline_asm_in_constexpr_error (loc
);
7955 if (cxx_dialect
>= cxx20
)
7956 /* In C++20 virtual calls can be constexpr, don't give up yet. */
7958 else if (flags
& tf_error
)
7960 "virtual functions cannot be %<constexpr%> before C++20");
7964 /* In C++20, a typeid expression whose operand is of polymorphic
7965 class type can be constexpr. */
7967 tree e
= TREE_OPERAND (t
, 0);
7968 if (cxx_dialect
< cxx20
7971 && !type_dependent_expression_p (e
)
7972 && TYPE_POLYMORPHIC_P (TREE_TYPE (e
)))
7974 if (flags
& tf_error
)
7975 error_at (loc
, "%<typeid%> is not a constant expression "
7976 "because %qE is of polymorphic type", e
);
7982 case POINTER_DIFF_EXPR
:
7993 case SPACESHIP_EXPR
:
7997 case PREINCREMENT_EXPR
:
7998 case POSTINCREMENT_EXPR
:
7999 case PREDECREMENT_EXPR
:
8000 case POSTDECREMENT_EXPR
:
8001 if (cxx_dialect
< cxx14
)
8007 if (TYPE_P (TREE_OPERAND (t
, 0)))
8013 case FIX_TRUNC_EXPR
:
8018 case TRUTH_NOT_EXPR
:
8019 case FIXED_CONVERT_EXPR
:
8020 case UNARY_PLUS_EXPR
:
8021 case UNARY_LEFT_FOLD_EXPR
:
8022 case UNARY_RIGHT_FOLD_EXPR
:
8024 return RECUR (TREE_OPERAND (t
, 0), rval
);
8027 case CONST_CAST_EXPR
:
8028 case STATIC_CAST_EXPR
:
8029 case REINTERPRET_CAST_EXPR
:
8030 case IMPLICIT_CONV_EXPR
:
8031 if (cxx_dialect
< cxx11
8032 && !dependent_type_p (TREE_TYPE (t
))
8033 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
)))
8034 /* In C++98, a conversion to non-integral type can't be part of a
8035 constant expression. */
8037 if (flags
& tf_error
)
8039 "cast to non-integral type %qT in a constant expression",
8043 /* This might be a conversion from a class to a (potentially) literal
8044 type. Let's consider it potentially constant since the conversion
8045 might be a constexpr user-defined conversion. */
8046 else if (cxx_dialect
>= cxx11
8047 && (dependent_type_p (TREE_TYPE (t
))
8048 || !COMPLETE_TYPE_P (TREE_TYPE (t
))
8049 || literal_type_p (TREE_TYPE (t
)))
8050 && TREE_OPERAND (t
, 0))
8052 tree type
= TREE_TYPE (TREE_OPERAND (t
, 0));
8053 /* If this is a dependent type, it could end up being a class
8054 with conversions. */
8055 if (type
== NULL_TREE
|| WILDCARD_TYPE_P (type
))
8057 /* Or a non-dependent class which has conversions. */
8058 else if (CLASS_TYPE_P (type
)
8059 && (TYPE_HAS_CONVERSION (type
) || dependent_scope_p (type
)))
8063 return (RECUR (TREE_OPERAND (t
, 0),
8064 !TYPE_REF_P (TREE_TYPE (t
))));
8067 return RECUR (BIND_EXPR_BODY (t
), want_rval
);
8069 case CLEANUP_POINT_EXPR
:
8070 case MUST_NOT_THROW_EXPR
:
8071 case TRY_CATCH_EXPR
:
8076 case NON_DEPENDENT_EXPR
:
8077 /* For convenience. */
8080 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
8083 tmp
= DECL_EXPR_DECL (t
);
8084 if (VAR_P (tmp
) && !DECL_ARTIFICIAL (tmp
))
8086 if (TREE_STATIC (tmp
))
8088 if (flags
& tf_error
)
8089 error_at (DECL_SOURCE_LOCATION (tmp
), "%qD declared "
8090 "%<static%> in %<constexpr%> context", tmp
);
8093 else if (CP_DECL_THREAD_LOCAL_P (tmp
))
8095 if (flags
& tf_error
)
8096 error_at (DECL_SOURCE_LOCATION (tmp
), "%qD declared "
8097 "%<thread_local%> in %<constexpr%> context", tmp
);
8100 else if (!check_for_uninitialized_const_var
8101 (tmp
, /*constexpr_context_p=*/true, flags
))
8104 return RECUR (tmp
, want_rval
);
8106 case TRY_FINALLY_EXPR
:
8107 return (RECUR (TREE_OPERAND (t
, 0), want_rval
)
8108 && RECUR (TREE_OPERAND (t
, 1), any
));
8111 return RECUR (TREE_OPERAND (t
, 1), want_rval
);
8114 if (!TARGET_EXPR_DIRECT_INIT_P (t
)
8115 && !literal_type_p (TREE_TYPE (t
)))
8117 if (flags
& tf_error
)
8119 auto_diagnostic_group d
;
8120 error_at (loc
, "temporary of non-literal type %qT in a "
8121 "constant expression", TREE_TYPE (t
));
8122 explain_non_literal_class (TREE_TYPE (t
));
8128 return RECUR (TREE_OPERAND (t
, 1), rval
);
8132 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (t
);
8133 constructor_elt
*ce
;
8134 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
8135 if (!RECUR (ce
->value
, want_rval
))
8142 gcc_assert (TREE_PURPOSE (t
) == NULL_TREE
8143 || DECL_P (TREE_PURPOSE (t
)));
8144 if (!RECUR (TREE_VALUE (t
), want_rval
))
8146 if (TREE_CHAIN (t
) == NULL_TREE
)
8148 return RECUR (TREE_CHAIN (t
), want_rval
);
8151 case TRUNC_DIV_EXPR
:
8153 case FLOOR_DIV_EXPR
:
8154 case ROUND_DIV_EXPR
:
8155 case TRUNC_MOD_EXPR
:
8157 case ROUND_MOD_EXPR
:
8159 tree denom
= TREE_OPERAND (t
, 1);
8160 if (!RECUR (denom
, rval
))
8162 /* We can't call cxx_eval_outermost_constant_expr on an expression
8163 that hasn't been through instantiate_non_dependent_expr yet. */
8164 if (!processing_template_decl
)
8165 denom
= cxx_eval_outermost_constant_expr (denom
, true);
8166 if (integer_zerop (denom
))
8168 if (flags
& tf_error
)
8169 error ("division by zero is not a constant expression");
8175 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
8181 /* check_return_expr sometimes wraps a TARGET_EXPR in a
8182 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
8183 introduced by build_call_a. */
8184 tree op0
= TREE_OPERAND (t
, 0);
8185 tree op1
= TREE_OPERAND (t
, 1);
8187 if ((TREE_CODE (op0
) == TARGET_EXPR
&& op1
== TARGET_EXPR_SLOT (op0
))
8188 || TREE_CODE (op1
) == EMPTY_CLASS_EXPR
)
8189 return RECUR (op0
, want_rval
);
8194 /* If the first operand is the non-short-circuit constant, look at
8195 the second operand; otherwise we only care about the first one for
8197 case TRUTH_AND_EXPR
:
8198 case TRUTH_ANDIF_EXPR
:
8199 tmp
= boolean_true_node
;
8202 case TRUTH_ORIF_EXPR
:
8203 tmp
= boolean_false_node
;
8206 tree op
= TREE_OPERAND (t
, 0);
8207 if (!RECUR (op
, rval
))
8209 if (!processing_template_decl
)
8210 op
= cxx_eval_outermost_constant_expr (op
, true);
8211 if (tree_int_cst_equal (op
, tmp
))
8212 return RECUR (TREE_OPERAND (t
, 1), rval
);
8219 case POINTER_PLUS_EXPR
:
8221 case EXACT_DIV_EXPR
:
8231 case TRUTH_XOR_EXPR
:
8232 case UNORDERED_EXPR
:
8245 case ARRAY_RANGE_REF
:
8249 case BINARY_LEFT_FOLD_EXPR
:
8250 case BINARY_RIGHT_FOLD_EXPR
:
8252 for (i
= 0; i
< 2; ++i
)
8253 if (!RECUR (TREE_OPERAND (t
, i
), want_rval
))
8258 for (i
= 0; i
< 3; ++i
)
8259 if (!RECUR (TREE_OPERAND (t
, i
), true))
8264 if (COND_EXPR_IS_VEC_DELETE (t
) && cxx_dialect
< cxx20
)
8266 if (flags
& tf_error
)
8267 error_at (loc
, "%<delete[]%> is not a constant expression");
8273 /* If the condition is a known constant, we know which of the legs we
8274 care about; otherwise we only require that the condition and
8275 either of the legs be potentially constant. */
8276 tmp
= TREE_OPERAND (t
, 0);
8277 if (!RECUR (tmp
, rval
))
8279 if (!processing_template_decl
)
8280 tmp
= cxx_eval_outermost_constant_expr (tmp
, true);
8281 if (integer_zerop (tmp
))
8282 return RECUR (TREE_OPERAND (t
, 2), want_rval
);
8283 else if (TREE_CODE (tmp
) == INTEGER_CST
)
8284 return RECUR (TREE_OPERAND (t
, 1), want_rval
);
8285 for (i
= 1; i
< 3; ++i
)
8286 if (potential_constant_expression_1 (TREE_OPERAND (t
, i
),
8287 want_rval
, strict
, now
,
8288 tf_none
, jump_target
))
8290 if (flags
& tf_error
)
8291 error_at (loc
, "expression %qE is not a constant expression", t
);
8295 if (VEC_INIT_EXPR_IS_CONSTEXPR (t
))
8297 if (flags
& tf_error
)
8299 error_at (loc
, "non-constant array initialization");
8300 diagnose_non_constexpr_vec_init (t
);
8306 /* We can see these in statement-expressions. */
8310 if (!RECUR (CLEANUP_BODY (t
), any
))
8312 if (!CLEANUP_EH_ONLY (t
) && !RECUR (CLEANUP_EXPR (t
), any
))
8316 case EMPTY_CLASS_EXPR
:
8321 tree
*target
= &TREE_OPERAND (t
, 0);
8322 /* Gotos representing break and continue are OK. */
8323 if (breaks (target
) || continues (target
))
8325 *jump_target
= *target
;
8328 if (flags
& tf_error
)
8329 error_at (loc
, "%<goto%> is not a constant expression");
8334 return RECUR (TREE_OPERAND (t
, 0), rval
);
8336 /* Coroutine await, yield and return expressions are not. */
8339 case CO_RETURN_EXPR
:
8343 if (objc_is_property_ref (t
))
8346 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t
)));
8354 potential_constant_expression_1 (tree t
, bool want_rval
, bool strict
, bool now
,
8355 tsubst_flags_t flags
)
8357 tree target
= NULL_TREE
;
8358 return potential_constant_expression_1 (t
, want_rval
, strict
, now
,
8362 /* The main entry point to the above. */
8365 potential_constant_expression (tree t
)
8367 return potential_constant_expression_1 (t
, false, true, false, tf_none
);
8370 /* As above, but require a constant rvalue. */
8373 potential_rvalue_constant_expression (tree t
)
8375 return potential_constant_expression_1 (t
, true, true, false, tf_none
);
8378 /* Like above, but complain about non-constant expressions. */
8381 require_potential_constant_expression (tree t
)
8383 return potential_constant_expression_1 (t
, false, true, false,
8384 tf_warning_or_error
);
8387 /* Cross product of the above. */
8390 require_potential_rvalue_constant_expression (tree t
)
8392 return potential_constant_expression_1 (t
, true, true, false,
8393 tf_warning_or_error
);
8396 /* Like above, but don't consider PARM_DECL a potential_constant_expression. */
8399 require_rvalue_constant_expression (tree t
)
8401 return potential_constant_expression_1 (t
, true, true, true,
8402 tf_warning_or_error
);
8405 /* Like potential_constant_expression, but don't consider possible constexpr
8406 substitution of the current function. That is, PARM_DECL qualifies under
8407 potential_constant_expression, but not here.
8409 This is basically what you can check when any actual constant values might
8410 be value-dependent. */
8413 is_constant_expression (tree t
)
8415 return potential_constant_expression_1 (t
, false, true, true, tf_none
);
8418 /* As above, but expect an rvalue. */
8421 is_rvalue_constant_expression (tree t
)
8423 return potential_constant_expression_1 (t
, true, true, true, tf_none
);
8426 /* Like above, but complain about non-constant expressions. */
8429 require_constant_expression (tree t
)
8431 return potential_constant_expression_1 (t
, false, true, true,
8432 tf_warning_or_error
);
8435 /* Like is_constant_expression, but allow const variables that are not allowed
8436 under constexpr rules. */
8439 is_static_init_expression (tree t
)
8441 return potential_constant_expression_1 (t
, false, false, true, tf_none
);
8444 /* Returns true if T is a potential constant expression that is not
8445 instantiation-dependent, and therefore a candidate for constant folding even
8449 is_nondependent_constant_expression (tree t
)
8451 return (!type_unknown_p (t
)
8452 && is_constant_expression (t
)
8453 && !instantiation_dependent_expression_p (t
));
8456 /* Returns true if T is a potential static initializer expression that is not
8457 instantiation-dependent. */
8460 is_nondependent_static_init_expression (tree t
)
8462 return (!type_unknown_p (t
)
8463 && is_static_init_expression (t
)
8464 && !instantiation_dependent_expression_p (t
));
8467 /* Finalize constexpr processing after parsing. */
8470 fini_constexpr (void)
8472 /* The contexpr call and fundef copies tables are no longer needed. */
8473 constexpr_call_table
= NULL
;
8474 fundef_copies_table
= NULL
;
8477 #include "gt-cp-constexpr.h"