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
:
1359 case BUILT_IN_ASAN_POINTER_COMPARE
:
1360 case BUILT_IN_ASAN_POINTER_SUBTRACT
:
1361 /* These builtins shall be ignored during constant expression
1368 /* Be permissive for arguments to built-ins; __builtin_constant_p should
1369 return constant false for a non-constant argument. */
1370 constexpr_ctx new_ctx
= *ctx
;
1371 new_ctx
.quiet
= true;
1372 for (i
= 0; i
< nargs
; ++i
)
1374 tree arg
= CALL_EXPR_ARG (t
, i
);
1377 /* To handle string built-ins we need to pass ADDR_EXPR<STRING_CST> since
1378 expand_builtin doesn't know how to look in the values table. */
1379 bool strop
= i
< strops
;
1383 if (TREE_CODE (arg
) == ADDR_EXPR
)
1384 arg
= TREE_OPERAND (arg
, 0);
1389 /* If builtin_valid_in_constant_expr_p is true,
1390 potential_constant_expression_1 has not recursed into the arguments
1391 of the builtin, verify it here. */
1392 if (!builtin_valid_in_constant_expr_p (fun
)
1393 || potential_constant_expression (arg
))
1395 bool dummy1
= false, dummy2
= false;
1396 arg
= cxx_eval_constant_expression (&new_ctx
, arg
, false,
1401 /* For __builtin_constant_p, fold all expressions with constant values
1402 even if they aren't C++ constant-expressions. */
1403 arg
= cp_fold_rvalue (arg
);
1406 if (TREE_CODE (arg
) == CONSTRUCTOR
)
1407 arg
= braced_lists_to_strings (TREE_TYPE (arg
), arg
);
1408 if (TREE_CODE (arg
) == STRING_CST
)
1409 arg
= build_address (arg
);
1417 bool save_ffbcp
= force_folding_builtin_constant_p
;
1418 force_folding_builtin_constant_p
|= ctx
->manifestly_const_eval
;
1419 tree save_cur_fn
= current_function_decl
;
1420 /* Return name of ctx->call->fundef->decl for __builtin_FUNCTION (). */
1421 if (fndecl_built_in_p (fun
, BUILT_IN_FUNCTION
)
1423 && ctx
->call
->fundef
)
1424 current_function_decl
= ctx
->call
->fundef
->decl
;
1425 new_call
= fold_builtin_call_array (EXPR_LOCATION (t
), TREE_TYPE (t
),
1426 CALL_EXPR_FN (t
), nargs
, args
);
1427 current_function_decl
= save_cur_fn
;
1428 force_folding_builtin_constant_p
= save_ffbcp
;
1429 if (new_call
== NULL
)
1431 if (!*non_constant_p
&& !ctx
->quiet
)
1433 /* Do not allow__builtin_unreachable in constexpr function.
1434 The __builtin_unreachable call with BUILTINS_LOCATION
1435 comes from cp_maybe_instrument_return. */
1436 if (fndecl_built_in_p (fun
, BUILT_IN_UNREACHABLE
)
1437 && EXPR_LOCATION (t
) == BUILTINS_LOCATION
)
1438 error ("%<constexpr%> call flows off the end of the function");
1441 new_call
= build_call_array_loc (EXPR_LOCATION (t
), TREE_TYPE (t
),
1442 CALL_EXPR_FN (t
), nargs
, args
);
1443 error ("%q+E is not a constant expression", new_call
);
1446 *non_constant_p
= true;
1450 if (!potential_constant_expression (new_call
))
1452 if (!*non_constant_p
&& !ctx
->quiet
)
1453 error ("%q+E is not a constant expression", new_call
);
1454 *non_constant_p
= true;
1460 /* memchr returns a pointer into the first argument, but we replaced the
1461 argument above with a STRING_CST; put it back it now. */
1462 tree op
= CALL_EXPR_ARG (t
, strret
-1);
1463 STRIP_NOPS (new_call
);
1464 if (TREE_CODE (new_call
) == POINTER_PLUS_EXPR
)
1465 TREE_OPERAND (new_call
, 0) = op
;
1466 else if (TREE_CODE (new_call
) == ADDR_EXPR
)
1470 return cxx_eval_constant_expression (&new_ctx
, new_call
, lval
,
1471 non_constant_p
, overflow_p
);
1474 /* TEMP is the constant value of a temporary object of type TYPE. Adjust
1475 the type of the value to match. */
1478 adjust_temp_type (tree type
, tree temp
)
1480 if (same_type_p (TREE_TYPE (temp
), type
))
1482 /* Avoid wrapping an aggregate value in a NOP_EXPR. */
1483 if (TREE_CODE (temp
) == CONSTRUCTOR
)
1485 /* build_constructor wouldn't retain various CONSTRUCTOR flags. */
1486 tree t
= copy_node (temp
);
1487 TREE_TYPE (t
) = type
;
1490 if (TREE_CODE (temp
) == EMPTY_CLASS_EXPR
)
1491 return build0 (EMPTY_CLASS_EXPR
, type
);
1492 gcc_assert (scalarish_type_p (type
));
1493 /* Now we know we're dealing with a scalar, and a prvalue of non-class
1494 type is cv-unqualified. */
1495 return cp_fold_convert (cv_unqualified (type
), temp
);
1498 /* If T is a CONSTRUCTOR, return an unshared copy of T and any
1499 sub-CONSTRUCTORs. Otherwise return T.
1501 We use this whenever we initialize an object as a whole, whether it's a
1502 parameter, a local variable, or a subobject, so that subsequent
1503 modifications don't affect other places where it was used. */
1506 unshare_constructor (tree t MEM_STAT_DECL
)
1508 if (!t
|| TREE_CODE (t
) != CONSTRUCTOR
)
1510 auto_vec
<tree
*, 4> ptrs
;
1511 ptrs
.safe_push (&t
);
1512 while (!ptrs
.is_empty ())
1514 tree
*p
= ptrs
.pop ();
1515 tree n
= copy_node (*p PASS_MEM_STAT
);
1516 CONSTRUCTOR_ELTS (n
) = vec_safe_copy (CONSTRUCTOR_ELTS (*p
) PASS_MEM_STAT
);
1518 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (n
);
1519 constructor_elt
*ce
;
1520 for (HOST_WIDE_INT i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
1521 if (TREE_CODE (ce
->value
) == CONSTRUCTOR
)
1522 ptrs
.safe_push (&ce
->value
);
1527 /* If T is a CONSTRUCTOR, ggc_free T and any sub-CONSTRUCTORs. */
1530 free_constructor (tree t
)
1532 if (!t
|| TREE_CODE (t
) != CONSTRUCTOR
)
1534 releasing_vec ctors
;
1535 vec_safe_push (ctors
, t
);
1536 while (!ctors
->is_empty ())
1538 tree c
= ctors
->pop ();
1539 if (vec
<constructor_elt
, va_gc
> *elts
= CONSTRUCTOR_ELTS (c
))
1541 constructor_elt
*ce
;
1542 for (HOST_WIDE_INT i
= 0; vec_safe_iterate (elts
, i
, &ce
); ++i
)
1543 if (TREE_CODE (ce
->value
) == CONSTRUCTOR
)
1544 vec_safe_push (ctors
, ce
->value
);
1551 /* Subroutine of cxx_eval_call_expression.
1552 We are processing a call expression (either CALL_EXPR or
1553 AGGR_INIT_EXPR) in the context of CTX. Evaluate
1554 all arguments and bind their values to correspondings
1555 parameters, making up the NEW_CALL context. */
1558 cxx_bind_parameters_in_call (const constexpr_ctx
*ctx
, tree t
,
1559 constexpr_call
*new_call
,
1560 bool *non_constant_p
, bool *overflow_p
,
1561 bool *non_constant_args
)
1563 const int nargs
= call_expr_nargs (t
);
1564 tree fun
= new_call
->fundef
->decl
;
1565 tree parms
= new_call
->fundef
->parms
;
1567 /* We don't record ellipsis args below. */
1568 int nparms
= list_length (parms
);
1569 int nbinds
= nargs
< nparms
? nargs
: nparms
;
1570 tree binds
= new_call
->bindings
= make_tree_vec (nbinds
);
1571 for (i
= 0; i
< nargs
; ++i
)
1574 tree type
= parms
? TREE_TYPE (parms
) : void_type_node
;
1575 x
= get_nth_callarg (t
, i
);
1576 /* For member function, the first argument is a pointer to the implied
1577 object. For a constructor, it might still be a dummy object, in
1578 which case we get the real argument from ctx. */
1579 if (i
== 0 && DECL_CONSTRUCTOR_P (fun
)
1580 && is_dummy_object (x
))
1583 x
= build_address (x
);
1585 if (TREE_ADDRESSABLE (type
))
1586 /* Undo convert_for_arg_passing work here. */
1587 x
= convert_from_reference (x
);
1588 arg
= cxx_eval_constant_expression (ctx
, x
, /*lval=*/false,
1589 non_constant_p
, overflow_p
);
1590 /* Don't VERIFY_CONSTANT here. */
1591 if (*non_constant_p
&& ctx
->quiet
)
1593 /* Just discard ellipsis args after checking their constantitude. */
1597 if (!*non_constant_p
)
1599 /* Make sure the binding has the same type as the parm. But
1600 only for constant args. */
1601 if (!TYPE_REF_P (type
))
1602 arg
= adjust_temp_type (type
, arg
);
1603 if (!TREE_CONSTANT (arg
))
1604 *non_constant_args
= true;
1605 /* For virtual calls, adjust the this argument, so that it is
1606 the object on which the method is called, rather than
1607 one of its bases. */
1608 if (i
== 0 && DECL_VIRTUAL_P (fun
))
1612 if (TREE_CODE (addr
) == ADDR_EXPR
)
1614 tree obj
= TREE_OPERAND (addr
, 0);
1615 while (TREE_CODE (obj
) == COMPONENT_REF
1616 && DECL_FIELD_IS_BASE (TREE_OPERAND (obj
, 1))
1617 && !same_type_ignoring_top_level_qualifiers_p
1618 (TREE_TYPE (obj
), DECL_CONTEXT (fun
)))
1619 obj
= TREE_OPERAND (obj
, 0);
1620 if (obj
!= TREE_OPERAND (addr
, 0))
1621 arg
= build_fold_addr_expr_with_type (obj
,
1625 TREE_VEC_ELT (binds
, i
) = arg
;
1627 parms
= TREE_CHAIN (parms
);
1631 /* Variables and functions to manage constexpr call expansion context.
1632 These do not need to be marked for PCH or GC. */
1634 /* FIXME remember and print actual constant arguments. */
1635 static vec
<tree
> call_stack
;
1636 static int call_stack_tick
;
1637 static int last_cx_error_tick
;
1640 push_cx_call_context (tree call
)
1643 if (!EXPR_HAS_LOCATION (call
))
1644 SET_EXPR_LOCATION (call
, input_location
);
1645 call_stack
.safe_push (call
);
1646 int len
= call_stack
.length ();
1647 if (len
> max_constexpr_depth
)
1653 pop_cx_call_context (void)
1660 cx_error_context (void)
1662 vec
<tree
> r
= vNULL
;
1663 if (call_stack_tick
!= last_cx_error_tick
1664 && !call_stack
.is_empty ())
1666 last_cx_error_tick
= call_stack_tick
;
1670 /* Evaluate a call T to a GCC internal function when possible and return
1671 the evaluated result or, under the control of CTX, give an error, set
1672 NON_CONSTANT_P, and return the unevaluated call T otherwise. */
1675 cxx_eval_internal_function (const constexpr_ctx
*ctx
, tree t
,
1677 bool *non_constant_p
, bool *overflow_p
)
1679 enum tree_code opcode
= ERROR_MARK
;
1681 switch (CALL_EXPR_IFN (t
))
1683 case IFN_UBSAN_NULL
:
1684 case IFN_UBSAN_BOUNDS
:
1685 case IFN_UBSAN_VPTR
:
1686 case IFN_FALLTHROUGH
:
1689 case IFN_ADD_OVERFLOW
:
1692 case IFN_SUB_OVERFLOW
:
1693 opcode
= MINUS_EXPR
;
1695 case IFN_MUL_OVERFLOW
:
1700 return cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 0),
1701 false, non_constant_p
, overflow_p
);
1703 case IFN_VEC_CONVERT
:
1705 tree arg
= cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 0),
1706 false, non_constant_p
,
1708 if (TREE_CODE (arg
) == VECTOR_CST
)
1709 return fold_const_call (CFN_VEC_CONVERT
, TREE_TYPE (t
), arg
);
1712 *non_constant_p
= true;
1719 error_at (cp_expr_loc_or_input_loc (t
),
1720 "call to internal function %qE", t
);
1721 *non_constant_p
= true;
1725 /* Evaluate constant arguments using OPCODE and return a complex
1726 number containing the result and the overflow bit. */
1727 tree arg0
= cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 0), lval
,
1728 non_constant_p
, overflow_p
);
1729 tree arg1
= cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 1), lval
,
1730 non_constant_p
, overflow_p
);
1732 if (TREE_CODE (arg0
) == INTEGER_CST
&& TREE_CODE (arg1
) == INTEGER_CST
)
1734 location_t loc
= cp_expr_loc_or_input_loc (t
);
1735 tree type
= TREE_TYPE (TREE_TYPE (t
));
1736 tree result
= fold_binary_loc (loc
, opcode
, type
,
1737 fold_convert_loc (loc
, type
, arg0
),
1738 fold_convert_loc (loc
, type
, arg1
));
1740 = build_int_cst (type
, arith_overflowed_p (opcode
, type
, arg0
, arg1
));
1741 /* Reset TREE_OVERFLOW to avoid warnings for the overflow. */
1742 if (TREE_OVERFLOW (result
))
1743 TREE_OVERFLOW (result
) = 0;
1745 return build_complex (TREE_TYPE (t
), result
, ovf
);
1748 *non_constant_p
= true;
1752 /* Clean CONSTRUCTOR_NO_CLEARING from CTOR and its sub-aggregates. */
1755 clear_no_implicit_zero (tree ctor
)
1757 if (CONSTRUCTOR_NO_CLEARING (ctor
))
1759 CONSTRUCTOR_NO_CLEARING (ctor
) = false;
1760 tree elt
; unsigned HOST_WIDE_INT idx
;
1761 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor
), idx
, elt
)
1762 if (TREE_CODE (elt
) == CONSTRUCTOR
)
1763 clear_no_implicit_zero (elt
);
1767 /* Complain about a const object OBJ being modified in a constant expression.
1768 EXPR is the MODIFY_EXPR expression performing the modification. */
1771 modifying_const_object_error (tree expr
, tree obj
)
1773 location_t loc
= cp_expr_loc_or_input_loc (expr
);
1774 auto_diagnostic_group d
;
1775 error_at (loc
, "modifying a const object %qE is not allowed in "
1776 "a constant expression", TREE_OPERAND (expr
, 0));
1777 inform (location_of (obj
), "originally declared %<const%> here");
1780 /* Return true if FNDECL is a replaceable global allocation function that
1781 should be useable during constant expression evaluation. */
1784 cxx_replaceable_global_alloc_fn (tree fndecl
)
1786 return (cxx_dialect
>= cxx20
1787 && IDENTIFIER_NEWDEL_OP_P (DECL_NAME (fndecl
))
1788 && CP_DECL_CONTEXT (fndecl
) == global_namespace
1789 && (DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl
)
1790 || DECL_IS_OPERATOR_DELETE_P (fndecl
)));
1793 /* Return true if FNDECL is a placement new function that should be
1794 useable during constant expression evaluation of std::construct_at. */
1797 cxx_placement_new_fn (tree fndecl
)
1799 if (cxx_dialect
>= cxx20
1800 && IDENTIFIER_NEW_OP_P (DECL_NAME (fndecl
))
1801 && CP_DECL_CONTEXT (fndecl
) == global_namespace
1802 && !DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl
)
1803 && TREE_CODE (TREE_TYPE (fndecl
)) == FUNCTION_TYPE
)
1805 tree first_arg
= TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
1806 if (TREE_VALUE (first_arg
) == ptr_type_node
1807 && TREE_CHAIN (first_arg
) == void_list_node
)
1813 /* Return true if FNDECL is std::construct_at. */
1816 is_std_construct_at (tree fndecl
)
1818 if (!decl_in_std_namespace_p (fndecl
))
1821 tree name
= DECL_NAME (fndecl
);
1822 return name
&& id_equal (name
, "construct_at");
1825 /* Return true if FNDECL is std::allocator<T>::{,de}allocate. */
1828 is_std_allocator_allocate (tree fndecl
)
1830 tree name
= DECL_NAME (fndecl
);
1831 if (name
== NULL_TREE
1832 || !(id_equal (name
, "allocate") || id_equal (name
, "deallocate")))
1835 tree ctx
= DECL_CONTEXT (fndecl
);
1836 if (ctx
== NULL_TREE
|| !CLASS_TYPE_P (ctx
) || !TYPE_MAIN_DECL (ctx
))
1839 tree decl
= TYPE_MAIN_DECL (ctx
);
1840 name
= DECL_NAME (decl
);
1841 if (name
== NULL_TREE
|| !id_equal (name
, "allocator"))
1844 return decl_in_std_namespace_p (decl
);
1847 /* Return true if FNDECL is __dynamic_cast. */
1850 cxx_dynamic_cast_fn_p (tree fndecl
)
1852 return (cxx_dialect
>= cxx20
1853 && id_equal (DECL_NAME (fndecl
), "__dynamic_cast")
1854 && CP_DECL_CONTEXT (fndecl
) == global_namespace
);
1857 /* Often, we have an expression in the form of address + offset, e.g.
1858 "&_ZTV1A + 16". Extract the object from it, i.e. "_ZTV1A". */
1861 extract_obj_from_addr_offset (tree expr
)
1863 if (TREE_CODE (expr
) == POINTER_PLUS_EXPR
)
1864 expr
= TREE_OPERAND (expr
, 0);
1866 if (TREE_CODE (expr
) == ADDR_EXPR
)
1867 expr
= TREE_OPERAND (expr
, 0);
1871 /* Given a PATH like
1873 g.D.2181.D.2154.D.2102.D.2093
1875 find a component with type TYPE. Return NULL_TREE if not found, and
1876 error_mark_node if the component is not accessible. If STOP is non-null,
1877 this function will return NULL_TREE if STOP is found before TYPE. */
1880 get_component_with_type (tree path
, tree type
, tree stop
)
1884 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path
), type
))
1888 && (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path
),
1891 else if (TREE_CODE (path
) == COMPONENT_REF
1892 && DECL_FIELD_IS_BASE (TREE_OPERAND (path
, 1)))
1894 /* We need to check that the component we're accessing is in fact
1896 if (TREE_PRIVATE (TREE_OPERAND (path
, 1))
1897 || TREE_PROTECTED (TREE_OPERAND (path
, 1)))
1898 return error_mark_node
;
1899 path
= TREE_OPERAND (path
, 0);
1906 /* Evaluate a call to __dynamic_cast (permitted by P1327R1).
1908 The declaration of __dynamic_cast is:
1910 void* __dynamic_cast (const void* __src_ptr,
1911 const __class_type_info* __src_type,
1912 const __class_type_info* __dst_type,
1913 ptrdiff_t __src2dst);
1915 where src2dst has the following possible values
1917 >-1: src_type is a unique public non-virtual base of dst_type
1918 dst_ptr + src2dst == src_ptr
1919 -1: unspecified relationship
1920 -2: src_type is not a public base of dst_type
1921 -3: src_type is a multiple public non-virtual base of dst_type
1923 Since literal types can't have virtual bases, we only expect hint >=0,
1927 cxx_eval_dynamic_cast_fn (const constexpr_ctx
*ctx
, tree call
,
1928 bool *non_constant_p
, bool *overflow_p
)
1930 /* T will be something like
1931 __dynamic_cast ((B*) b, &_ZTI1B, &_ZTI1D, 8)
1933 gcc_assert (call_expr_nargs (call
) == 4);
1934 tsubst_flags_t complain
= ctx
->quiet
? tf_none
: tf_warning_or_error
;
1935 tree obj
= CALL_EXPR_ARG (call
, 0);
1936 tree type
= CALL_EXPR_ARG (call
, 2);
1937 HOST_WIDE_INT hint
= int_cst_value (CALL_EXPR_ARG (call
, 3));
1938 location_t loc
= cp_expr_loc_or_input_loc (call
);
1940 /* Get the target type of the dynamic_cast. */
1941 gcc_assert (TREE_CODE (type
) == ADDR_EXPR
);
1942 type
= TREE_OPERAND (type
, 0);
1943 type
= TREE_TYPE (DECL_NAME (type
));
1945 /* TYPE can only be either T* or T&. We can't know which of these it
1946 is by looking at TYPE, but OBJ will be "(T*) x" in the first case,
1947 and something like "(T*)(T&)(T*) x" in the second case. */
1948 bool reference_p
= false;
1949 while (CONVERT_EXPR_P (obj
) || TREE_CODE (obj
) == SAVE_EXPR
)
1951 reference_p
|= TYPE_REF_P (TREE_TYPE (obj
));
1952 obj
= TREE_OPERAND (obj
, 0);
1955 /* Evaluate the object so that we know its dynamic type. */
1956 obj
= cxx_eval_constant_expression (ctx
, obj
, /*lval*/false, non_constant_p
,
1958 if (*non_constant_p
)
1961 /* We expect OBJ to be in form of &d.D.2102 when HINT == 0,
1962 but when HINT is > 0, it can also be something like
1963 &d.D.2102 + 18446744073709551608, which includes the BINFO_OFFSET. */
1964 obj
= extract_obj_from_addr_offset (obj
);
1965 const tree objtype
= TREE_TYPE (obj
);
1966 /* If OBJ doesn't refer to a base field, we're done. */
1967 if (tree t
= (TREE_CODE (obj
) == COMPONENT_REF
1968 ? TREE_OPERAND (obj
, 1) : obj
))
1969 if (TREE_CODE (t
) != FIELD_DECL
|| !DECL_FIELD_IS_BASE (t
))
1975 error_at (loc
, "reference %<dynamic_cast%> failed");
1976 inform (loc
, "dynamic type %qT of its operand does "
1977 "not have a base class of type %qT",
1980 *non_constant_p
= true;
1982 return integer_zero_node
;
1985 /* [class.cdtor] When a dynamic_cast is used in a constructor ...
1986 or in a destructor ... if the operand of the dynamic_cast refers
1987 to the object under construction or destruction, this object is
1988 considered to be a most derived object that has the type of the
1989 constructor or destructor's class. */
1990 tree vtable
= build_vfield_ref (obj
, TREE_TYPE (obj
));
1991 vtable
= cxx_eval_constant_expression (ctx
, vtable
, /*lval*/false,
1992 non_constant_p
, overflow_p
);
1993 if (*non_constant_p
)
1995 /* VTABLE will be &_ZTV1A + 16 or similar, get _ZTV1A. */
1996 vtable
= extract_obj_from_addr_offset (vtable
);
1997 const tree mdtype
= DECL_CONTEXT (vtable
);
1999 /* Given dynamic_cast<T>(v),
2001 [expr.dynamic.cast] If C is the class type to which T points or refers,
2002 the runtime check logically executes as follows:
2004 If, in the most derived object pointed (referred) to by v, v points
2005 (refers) to a public base class subobject of a C object, and if only
2006 one object of type C is derived from the subobject pointed (referred)
2007 to by v the result points (refers) to that C object.
2009 In this case, HINT >= 0 or -3. */
2010 if (hint
>= 0 || hint
== -3)
2012 /* Look for a component with type TYPE. */
2013 tree t
= get_component_with_type (obj
, type
, mdtype
);
2014 /* If not accessible, give an error. */
2015 if (t
== error_mark_node
)
2021 error_at (loc
, "reference %<dynamic_cast%> failed");
2022 inform (loc
, "static type %qT of its operand is a "
2023 "non-public base class of dynamic type %qT",
2027 *non_constant_p
= true;
2029 return integer_zero_node
;
2032 /* The result points to the TYPE object. */
2033 return cp_build_addr_expr (t
, complain
);
2034 /* Else, TYPE was not found, because the HINT turned out to be wrong.
2035 Fall through to the normal processing. */
2038 /* Otherwise, if v points (refers) to a public base class subobject of the
2039 most derived object, and the type of the most derived object has a base
2040 class, of type C, that is unambiguous and public, the result points
2041 (refers) to the C subobject of the most derived object.
2043 But it can also be an invalid case. */
2045 /* Get the most derived object. */
2046 obj
= get_component_with_type (obj
, mdtype
, NULL_TREE
);
2047 if (obj
== error_mark_node
)
2053 error_at (loc
, "reference %<dynamic_cast%> failed");
2054 inform (loc
, "static type %qT of its operand is a non-public"
2055 " base class of dynamic type %qT", objtype
, mdtype
);
2057 *non_constant_p
= true;
2059 return integer_zero_node
;
2064 /* Check that the type of the most derived object has a base class
2065 of type TYPE that is unambiguous and public. */
2067 tree binfo
= lookup_base (mdtype
, type
, ba_check
, &b_kind
, tf_none
);
2068 if (!binfo
|| binfo
== error_mark_node
)
2074 error_at (loc
, "reference %<dynamic_cast%> failed");
2075 if (b_kind
== bk_ambig
)
2076 inform (loc
, "%qT is an ambiguous base class of dynamic "
2077 "type %qT of its operand", type
, mdtype
);
2079 inform (loc
, "dynamic type %qT of its operand does not "
2080 "have an unambiguous public base class %qT",
2083 *non_constant_p
= true;
2085 return integer_zero_node
;
2087 /* If so, return the TYPE subobject of the most derived object. */
2088 obj
= convert_to_base_statically (obj
, binfo
);
2089 return cp_build_addr_expr (obj
, complain
);
2092 /* Data structure used by replace_result_decl and replace_result_decl_r. */
2094 struct replace_result_decl_data
2096 /* The RESULT_DECL we want to replace. */
2098 /* The replacement for DECL. */
2100 /* Whether we've performed any replacements. */
2104 /* Helper function for replace_result_decl, called through cp_walk_tree. */
2107 replace_result_decl_r (tree
*tp
, int *walk_subtrees
, void *data
)
2109 replace_result_decl_data
*d
= (replace_result_decl_data
*) data
;
2113 *tp
= unshare_expr (d
->replacement
);
2117 else if (TYPE_P (*tp
))
2123 /* Replace every occurrence of DECL, a RESULT_DECL, with (an unshared copy of)
2124 REPLACEMENT within the reduced constant expression *TP. Returns true iff a
2125 replacement was performed. */
2128 replace_result_decl (tree
*tp
, tree decl
, tree replacement
)
2130 gcc_checking_assert (TREE_CODE (decl
) == RESULT_DECL
2131 && (same_type_ignoring_top_level_qualifiers_p
2132 (TREE_TYPE (decl
), TREE_TYPE (replacement
))));
2133 replace_result_decl_data data
= { decl
, replacement
, false };
2134 cp_walk_tree_without_duplicates (tp
, replace_result_decl_r
, &data
);
2135 return data
.changed
;
2138 /* Evaluate the call T to virtual function thunk THUNK_FNDECL. */
2141 cxx_eval_thunk_call (const constexpr_ctx
*ctx
, tree t
, tree thunk_fndecl
,
2143 bool *non_constant_p
, bool *overflow_p
)
2145 tree function
= THUNK_TARGET (thunk_fndecl
);
2147 /* virtual_offset is only set in the presence of virtual bases, which make
2148 the class non-literal, so we don't need to handle it here. */
2149 if (THUNK_VIRTUAL_OFFSET (thunk_fndecl
))
2151 gcc_assert (!DECL_DECLARED_CONSTEXPR_P (function
));
2154 error ("call to non-%<constexpr%> function %qD", function
);
2155 explain_invalid_constexpr_fn (function
);
2157 *non_constant_p
= true;
2161 tree new_call
= copy_node (t
);
2162 CALL_EXPR_FN (new_call
) = function
;
2163 TREE_TYPE (new_call
) = TREE_TYPE (TREE_TYPE (function
));
2165 tree offset
= size_int (THUNK_FIXED_OFFSET (thunk_fndecl
));
2167 if (DECL_THIS_THUNK_P (thunk_fndecl
))
2169 /* 'this'-adjusting thunk. */
2170 tree this_arg
= CALL_EXPR_ARG (t
, 0);
2171 this_arg
= build2 (POINTER_PLUS_EXPR
, TREE_TYPE (this_arg
),
2173 CALL_EXPR_ARG (new_call
, 0) = this_arg
;
2176 /* Return-adjusting thunk. */
2177 new_call
= build2 (POINTER_PLUS_EXPR
, TREE_TYPE (new_call
),
2180 return cxx_eval_constant_expression (ctx
, new_call
, lval
,
2181 non_constant_p
, overflow_p
);
2184 /* Subroutine of cxx_eval_constant_expression.
2185 Evaluate the call expression tree T in the context of OLD_CALL expression
2189 cxx_eval_call_expression (const constexpr_ctx
*ctx
, tree t
,
2191 bool *non_constant_p
, bool *overflow_p
)
2193 /* Handle concept checks separately. */
2194 if (concept_check_p (t
))
2195 return evaluate_concept_check (t
, tf_warning_or_error
);
2197 location_t loc
= cp_expr_loc_or_input_loc (t
);
2198 tree fun
= get_function_named_in_call (t
);
2199 constexpr_call new_call
2200 = { NULL
, NULL
, NULL
, 0, ctx
->manifestly_const_eval
};
2203 if (fun
== NULL_TREE
)
2204 return cxx_eval_internal_function (ctx
, t
, lval
,
2205 non_constant_p
, overflow_p
);
2207 if (TREE_CODE (fun
) != FUNCTION_DECL
)
2209 /* Might be a constexpr function pointer. */
2210 fun
= cxx_eval_constant_expression (ctx
, fun
,
2211 /*lval*/false, non_constant_p
,
2214 if (TREE_CODE (fun
) == ADDR_EXPR
)
2215 fun
= TREE_OPERAND (fun
, 0);
2216 /* For TARGET_VTABLE_USES_DESCRIPTORS targets, there is no
2217 indirection, the called expression is a pointer into the
2218 virtual table which should contain FDESC_EXPR. Extract the
2219 FUNCTION_DECL from there. */
2220 else if (TARGET_VTABLE_USES_DESCRIPTORS
2221 && TREE_CODE (fun
) == POINTER_PLUS_EXPR
2222 && TREE_CODE (TREE_OPERAND (fun
, 0)) == ADDR_EXPR
2223 && TREE_CODE (TREE_OPERAND (fun
, 1)) == INTEGER_CST
)
2225 tree d
= TREE_OPERAND (TREE_OPERAND (fun
, 0), 0);
2227 && DECL_VTABLE_OR_VTT_P (d
)
2228 && TREE_CODE (TREE_TYPE (d
)) == ARRAY_TYPE
2229 && TREE_TYPE (TREE_TYPE (d
)) == vtable_entry_type
2231 && TREE_CODE (DECL_INITIAL (d
)) == CONSTRUCTOR
)
2233 tree i
= int_const_binop (TRUNC_DIV_EXPR
, TREE_OPERAND (fun
, 1),
2234 TYPE_SIZE_UNIT (vtable_entry_type
));
2235 HOST_WIDE_INT idx
= find_array_ctor_elt (DECL_INITIAL (d
), i
);
2239 = (*CONSTRUCTOR_ELTS (DECL_INITIAL (d
)))[idx
].value
;
2240 if (TREE_CODE (fdesc
) == FDESC_EXPR
2241 && integer_zerop (TREE_OPERAND (fdesc
, 1)))
2242 fun
= TREE_OPERAND (fdesc
, 0);
2247 if (TREE_CODE (fun
) != FUNCTION_DECL
)
2249 if (!ctx
->quiet
&& !*non_constant_p
)
2250 error_at (loc
, "expression %qE does not designate a %<constexpr%> "
2252 *non_constant_p
= true;
2255 if (DECL_CLONED_FUNCTION_P (fun
))
2256 fun
= DECL_CLONED_FUNCTION (fun
);
2258 if (is_ubsan_builtin_p (fun
))
2261 if (fndecl_built_in_p (fun
))
2262 return cxx_eval_builtin_function_call (ctx
, t
, fun
,
2263 lval
, non_constant_p
, overflow_p
);
2264 if (DECL_THUNK_P (fun
))
2265 return cxx_eval_thunk_call (ctx
, t
, fun
, lval
, non_constant_p
, overflow_p
);
2266 if (!DECL_DECLARED_CONSTEXPR_P (fun
))
2268 if (TREE_CODE (t
) == CALL_EXPR
2269 && cxx_replaceable_global_alloc_fn (fun
)
2270 && (CALL_FROM_NEW_OR_DELETE_P (t
)
2272 && ctx
->call
->fundef
2273 && is_std_allocator_allocate (ctx
->call
->fundef
->decl
))))
2275 const int nargs
= call_expr_nargs (t
);
2276 tree arg0
= NULL_TREE
;
2277 for (int i
= 0; i
< nargs
; ++i
)
2279 tree arg
= CALL_EXPR_ARG (t
, i
);
2280 arg
= cxx_eval_constant_expression (ctx
, arg
, false,
2281 non_constant_p
, overflow_p
);
2282 VERIFY_CONSTANT (arg
);
2287 if (IDENTIFIER_NEW_OP_P (DECL_NAME (fun
)))
2289 tree type
= build_array_type_nelts (char_type_node
,
2290 tree_to_uhwi (arg0
));
2291 tree var
= build_decl (loc
, VAR_DECL
, heap_uninit_identifier
,
2293 DECL_ARTIFICIAL (var
) = 1;
2294 TREE_STATIC (var
) = 1;
2295 ctx
->global
->heap_vars
.safe_push (var
);
2296 ctx
->global
->values
.put (var
, NULL_TREE
);
2297 return fold_convert (ptr_type_node
, build_address (var
));
2302 if (TREE_CODE (arg0
) == ADDR_EXPR
2303 && VAR_P (TREE_OPERAND (arg0
, 0)))
2305 tree var
= TREE_OPERAND (arg0
, 0);
2306 if (DECL_NAME (var
) == heap_uninit_identifier
2307 || DECL_NAME (var
) == heap_identifier
)
2309 DECL_NAME (var
) = heap_deleted_identifier
;
2310 ctx
->global
->values
.remove (var
);
2311 ctx
->global
->heap_dealloc_count
++;
2314 else if (DECL_NAME (var
) == heap_deleted_identifier
)
2317 error_at (loc
, "deallocation of already deallocated "
2319 *non_constant_p
= true;
2324 error_at (loc
, "deallocation of storage that was "
2325 "not previously allocated");
2326 *non_constant_p
= true;
2330 /* Allow placement new in std::construct_at, just return the second
2332 if (TREE_CODE (t
) == CALL_EXPR
2333 && cxx_placement_new_fn (fun
)
2335 && ctx
->call
->fundef
2336 && is_std_construct_at (ctx
->call
->fundef
->decl
))
2338 const int nargs
= call_expr_nargs (t
);
2339 tree arg1
= NULL_TREE
;
2340 for (int i
= 0; i
< nargs
; ++i
)
2342 tree arg
= CALL_EXPR_ARG (t
, i
);
2343 arg
= cxx_eval_constant_expression (ctx
, arg
, false,
2344 non_constant_p
, overflow_p
);
2345 VERIFY_CONSTANT (arg
);
2352 else if (cxx_dynamic_cast_fn_p (fun
))
2353 return cxx_eval_dynamic_cast_fn (ctx
, t
, non_constant_p
, overflow_p
);
2357 if (!lambda_static_thunk_p (fun
))
2358 error_at (loc
, "call to non-%<constexpr%> function %qD", fun
);
2359 explain_invalid_constexpr_fn (fun
);
2361 *non_constant_p
= true;
2365 constexpr_ctx new_ctx
= *ctx
;
2366 if (DECL_CONSTRUCTOR_P (fun
) && !ctx
->object
2367 && TREE_CODE (t
) == AGGR_INIT_EXPR
)
2369 /* We want to have an initialization target for an AGGR_INIT_EXPR.
2370 If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
2371 new_ctx
.object
= AGGR_INIT_EXPR_SLOT (t
);
2372 tree ctor
= new_ctx
.ctor
= build_constructor (DECL_CONTEXT (fun
), NULL
);
2373 CONSTRUCTOR_NO_CLEARING (ctor
) = true;
2374 ctx
->global
->values
.put (new_ctx
.object
, ctor
);
2378 /* Shortcut trivial constructor/op=. */
2379 if (trivial_fn_p (fun
))
2381 tree init
= NULL_TREE
;
2382 if (call_expr_nargs (t
) == 2)
2383 init
= convert_from_reference (get_nth_callarg (t
, 1));
2384 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
2385 && AGGR_INIT_ZERO_FIRST (t
))
2386 init
= build_zero_init (DECL_CONTEXT (fun
), NULL_TREE
, false);
2389 tree op
= get_nth_callarg (t
, 0);
2390 if (is_dummy_object (op
))
2393 op
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (op
)), op
);
2394 tree set
= build2 (MODIFY_EXPR
, TREE_TYPE (op
), op
, init
);
2395 new_ctx
.call
= &new_call
;
2396 return cxx_eval_constant_expression (&new_ctx
, set
, lval
,
2397 non_constant_p
, overflow_p
);
2401 /* We can't defer instantiating the function any longer. */
2402 if (!DECL_INITIAL (fun
)
2403 && DECL_TEMPLOID_INSTANTIATION (fun
)
2404 && !uid_sensitive_constexpr_evaluation_p ())
2406 location_t save_loc
= input_location
;
2407 input_location
= loc
;
2409 instantiate_decl (fun
, /*defer_ok*/false, /*expl_inst*/false);
2411 input_location
= save_loc
;
2414 /* If in direct recursive call, optimize definition search. */
2415 if (ctx
&& ctx
->call
&& ctx
->call
->fundef
&& ctx
->call
->fundef
->decl
== fun
)
2416 new_call
.fundef
= ctx
->call
->fundef
;
2419 new_call
.fundef
= retrieve_constexpr_fundef (fun
);
2420 if (new_call
.fundef
== NULL
|| new_call
.fundef
->body
== NULL
2421 || new_call
.fundef
->result
== error_mark_node
2422 || fun
== current_function_decl
)
2426 /* We need to check for current_function_decl here in case we're
2427 being called during cp_fold_function, because at that point
2428 DECL_INITIAL is set properly and we have a fundef but we
2429 haven't lowered invisirefs yet (c++/70344). */
2430 if (DECL_INITIAL (fun
) == error_mark_node
2431 || fun
== current_function_decl
)
2432 error_at (loc
, "%qD called in a constant expression before its "
2433 "definition is complete", fun
);
2434 else if (DECL_INITIAL (fun
))
2436 /* The definition of fun was somehow unsuitable. But pretend
2437 that lambda static thunks don't exist. */
2438 if (!lambda_static_thunk_p (fun
))
2439 error_at (loc
, "%qD called in a constant expression", fun
);
2440 explain_invalid_constexpr_fn (fun
);
2443 error_at (loc
, "%qD used before its definition", fun
);
2445 *non_constant_p
= true;
2450 bool non_constant_args
= false;
2451 cxx_bind_parameters_in_call (ctx
, t
, &new_call
,
2452 non_constant_p
, overflow_p
, &non_constant_args
);
2454 /* We build up the bindings list before we know whether we already have this
2455 call cached. If we don't end up saving these bindings, ggc_free them when
2456 this function exits. */
2461 free_bindings (tree
&b
): bindings (&b
) { }
2462 ~free_bindings () { if (bindings
) ggc_free (*bindings
); }
2463 void preserve () { bindings
= NULL
; }
2464 } fb (new_call
.bindings
);
2466 if (*non_constant_p
)
2469 depth_ok
= push_cx_call_context (t
);
2471 /* Remember the object we are constructing. */
2472 tree new_obj
= NULL_TREE
;
2473 if (DECL_CONSTRUCTOR_P (fun
))
2475 /* In a constructor, it should be the first `this' argument.
2476 At this point it has already been evaluated in the call
2477 to cxx_bind_parameters_in_call. */
2478 new_obj
= TREE_VEC_ELT (new_call
.bindings
, 0);
2479 STRIP_NOPS (new_obj
);
2480 if (TREE_CODE (new_obj
) == ADDR_EXPR
)
2481 new_obj
= TREE_OPERAND (new_obj
, 0);
2483 if (ctx
->call
&& ctx
->call
->fundef
2484 && DECL_CONSTRUCTOR_P (ctx
->call
->fundef
->decl
))
2486 tree cur_obj
= TREE_VEC_ELT (ctx
->call
->bindings
, 0);
2487 STRIP_NOPS (cur_obj
);
2488 if (TREE_CODE (cur_obj
) == ADDR_EXPR
)
2489 cur_obj
= TREE_OPERAND (cur_obj
, 0);
2490 if (new_obj
== cur_obj
)
2491 /* We're calling the target constructor of a delegating
2492 constructor, or accessing a base subobject through a
2493 NOP_EXPR as part of a call to a base constructor, so
2494 there is no new (sub)object. */
2495 new_obj
= NULL_TREE
;
2499 tree result
= NULL_TREE
;
2501 constexpr_call
*entry
= NULL
;
2502 if (depth_ok
&& !non_constant_args
&& ctx
->strict
)
2504 new_call
.hash
= constexpr_fundef_hasher::hash (new_call
.fundef
);
2506 = iterative_hash_template_arg (new_call
.bindings
, new_call
.hash
);
2508 = iterative_hash_object (ctx
->manifestly_const_eval
, new_call
.hash
);
2510 /* If we have seen this call before, we are done. */
2511 maybe_initialize_constexpr_call_table ();
2512 constexpr_call
**slot
2513 = constexpr_call_table
->find_slot (&new_call
, INSERT
);
2517 /* Only cache up to constexpr_cache_depth to limit memory use. */
2518 if (depth_ok
< constexpr_cache_depth
)
2520 /* We need to keep a pointer to the entry, not just the slot, as
2521 the slot can move during evaluation of the body. */
2522 *slot
= entry
= ggc_alloc
<constexpr_call
> ();
2527 /* Calls that are in progress have their result set to NULL, so that we
2528 can detect circular dependencies. Now that we only cache up to
2529 constexpr_cache_depth this won't catch circular dependencies that
2530 start deeper, but they'll hit the recursion or ops limit. */
2531 else if (entry
->result
== NULL
)
2534 error ("call has circular dependency");
2535 *non_constant_p
= true;
2536 entry
->result
= result
= error_mark_node
;
2539 result
= entry
->result
;
2545 error ("%<constexpr%> evaluation depth exceeds maximum of %d (use "
2546 "%<-fconstexpr-depth=%> to increase the maximum)",
2547 max_constexpr_depth
);
2548 *non_constant_p
= true;
2549 result
= error_mark_node
;
2553 bool cacheable
= true;
2554 if (result
&& result
!= error_mark_node
)
2556 else if (!DECL_SAVED_TREE (fun
))
2558 /* When at_eof >= 2, cgraph has started throwing away
2559 DECL_SAVED_TREE, so fail quietly. FIXME we get here because of
2560 late code generation for VEC_INIT_EXPR, which needs to be
2561 completely reconsidered. */
2562 gcc_assert (at_eof
>= 2 && ctx
->quiet
);
2563 *non_constant_p
= true;
2565 else if (tree copy
= get_fundef_copy (new_call
.fundef
))
2567 tree body
, parms
, res
;
2568 releasing_vec ctors
;
2570 /* Reuse or create a new unshared copy of this function's body. */
2571 body
= TREE_PURPOSE (copy
);
2572 parms
= TREE_VALUE (copy
);
2573 res
= TREE_TYPE (copy
);
2575 /* Associate the bindings with the remapped parms. */
2576 tree bound
= new_call
.bindings
;
2577 tree remapped
= parms
;
2578 for (int i
= 0; i
< TREE_VEC_LENGTH (bound
); ++i
)
2580 tree arg
= TREE_VEC_ELT (bound
, i
);
2583 /* Unshare args going into the hash table to separate them
2584 from the caller's context, for better GC and to avoid
2585 problems with verify_gimple. */
2586 arg
= unshare_expr_without_location (arg
);
2587 TREE_VEC_ELT (bound
, i
) = arg
;
2589 /* Don't share a CONSTRUCTOR that might be changed. This is not
2590 redundant with the unshare just above; we also don't want to
2591 change the argument values in the hash table. XXX Could we
2592 unshare lazily in cxx_eval_store_expression? */
2593 arg
= unshare_constructor (arg
);
2594 if (TREE_CODE (arg
) == CONSTRUCTOR
)
2595 vec_safe_push (ctors
, arg
);
2596 ctx
->global
->values
.put (remapped
, arg
);
2597 remapped
= DECL_CHAIN (remapped
);
2599 /* Add the RESULT_DECL to the values map, too. */
2600 gcc_assert (!DECL_BY_REFERENCE (res
));
2601 ctx
->global
->values
.put (res
, NULL_TREE
);
2603 /* Track the callee's evaluated SAVE_EXPRs and TARGET_EXPRs so that
2604 we can forget their values after the call. */
2605 constexpr_ctx ctx_with_save_exprs
= *ctx
;
2606 auto_vec
<tree
, 10> save_exprs
;
2607 ctx_with_save_exprs
.save_exprs
= &save_exprs
;
2608 ctx_with_save_exprs
.call
= &new_call
;
2609 unsigned save_heap_alloc_count
= ctx
->global
->heap_vars
.length ();
2610 unsigned save_heap_dealloc_count
= ctx
->global
->heap_dealloc_count
;
2612 tree jump_target
= NULL_TREE
;
2613 cxx_eval_constant_expression (&ctx_with_save_exprs
, body
,
2614 lval
, non_constant_p
, overflow_p
,
2617 if (DECL_CONSTRUCTOR_P (fun
))
2618 /* This can be null for a subobject constructor call, in
2619 which case what we care about is the initialization
2620 side-effects rather than the value. We could get at the
2621 value by evaluating *this, but we don't bother; there's
2622 no need to put such a call in the hash table. */
2623 result
= lval
? ctx
->object
: ctx
->ctor
;
2624 else if (VOID_TYPE_P (TREE_TYPE (res
)))
2628 result
= *ctx
->global
->values
.get (res
);
2629 if (result
== NULL_TREE
&& !*non_constant_p
)
2632 error ("%<constexpr%> call flows off the end "
2634 *non_constant_p
= true;
2638 /* At this point, the object's constructor will have run, so
2639 the object is no longer under construction, and its possible
2640 'const' semantics now apply. Make a note of this fact by
2641 marking the CONSTRUCTOR TREE_READONLY. */
2643 && CLASS_TYPE_P (TREE_TYPE (new_obj
))
2644 && CP_TYPE_CONST_P (TREE_TYPE (new_obj
)))
2646 /* Subobjects might not be stored in ctx->global->values but we
2647 can get its CONSTRUCTOR by evaluating *this. */
2648 tree e
= cxx_eval_constant_expression (ctx
, new_obj
,
2652 if (TREE_CODE (e
) == CONSTRUCTOR
&& !*non_constant_p
)
2653 TREE_READONLY (e
) = true;
2656 /* Forget the saved values of the callee's SAVE_EXPRs and
2660 FOR_EACH_VEC_ELT (save_exprs
, i
, save_expr
)
2661 ctx
->global
->values
.remove (save_expr
);
2663 /* Remove the parms/result from the values map. Is it worth
2664 bothering to do this when the map itself is only live for
2665 one constexpr evaluation? If so, maybe also clear out
2666 other vars from call, maybe in BIND_EXPR handling? */
2667 ctx
->global
->values
.remove (res
);
2668 for (tree parm
= parms
; parm
; parm
= TREE_CHAIN (parm
))
2669 ctx
->global
->values
.remove (parm
);
2671 /* Free any parameter CONSTRUCTORs we aren't returning directly. */
2672 while (!ctors
->is_empty ())
2674 tree c
= ctors
->pop ();
2676 free_constructor (c
);
2679 /* Make the unshared function copy we used available for re-use. */
2680 save_fundef_copy (fun
, copy
);
2682 /* If the call allocated some heap object that hasn't been
2683 deallocated during the call, or if it deallocated some heap
2684 object it has not allocated, the call isn't really stateless
2685 for the constexpr evaluation and should not be cached.
2686 It is fine if the call allocates something and deallocates it
2689 && (save_heap_alloc_count
!= ctx
->global
->heap_vars
.length ()
2690 || (save_heap_dealloc_count
2691 != ctx
->global
->heap_dealloc_count
)))
2695 if ((ctx
->global
->heap_vars
.length ()
2696 - ctx
->global
->heap_dealloc_count
)
2697 != save_heap_alloc_count
- save_heap_dealloc_count
)
2700 FOR_EACH_VEC_ELT_FROM (ctx
->global
->heap_vars
, i
, heap_var
,
2701 save_heap_alloc_count
)
2702 if (DECL_NAME (heap_var
) != heap_deleted_identifier
)
2709 /* Rewrite all occurrences of the function's RESULT_DECL with the
2710 current object under construction. */
2711 if (!*non_constant_p
&& ctx
->object
2712 && AGGREGATE_TYPE_P (TREE_TYPE (res
))
2713 && !is_empty_class (TREE_TYPE (res
)))
2714 if (replace_result_decl (&result
, res
, ctx
->object
))
2718 /* Couldn't get a function copy to evaluate. */
2719 *non_constant_p
= true;
2721 if (result
== error_mark_node
)
2722 *non_constant_p
= true;
2723 if (*non_constant_p
|| *overflow_p
)
2724 result
= error_mark_node
;
2728 entry
->result
= cacheable
? result
: error_mark_node
;
2731 /* The result of a constexpr function must be completely initialized.
2733 However, in C++20, a constexpr constructor doesn't necessarily have
2734 to initialize all the fields, so we don't clear CONSTRUCTOR_NO_CLEARING
2735 in order to detect reading an unitialized object in constexpr instead
2736 of value-initializing it. (reduced_constant_expression_p is expected to
2737 take care of clearing the flag.) */
2738 if (TREE_CODE (result
) == CONSTRUCTOR
2739 && (cxx_dialect
< cxx20
2740 || !DECL_CONSTRUCTOR_P (fun
)))
2741 clear_no_implicit_zero (result
);
2743 pop_cx_call_context ();
2747 /* Return true if T is a valid constant initializer. If a CONSTRUCTOR
2748 initializes all the members, the CONSTRUCTOR_NO_CLEARING flag will be
2750 FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
2753 reduced_constant_expression_p (tree t
)
2758 switch (TREE_CODE (t
))
2761 /* Even if we can't lower this yet, it's constant. */
2765 /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
2766 tree idx
, val
, field
; unsigned HOST_WIDE_INT i
;
2767 if (CONSTRUCTOR_NO_CLEARING (t
))
2769 if (TREE_CODE (TREE_TYPE (t
)) == VECTOR_TYPE
)
2770 /* An initialized vector would have a VECTOR_CST. */
2772 else if (cxx_dialect
>= cxx20
2773 /* An ARRAY_TYPE doesn't have any TYPE_FIELDS. */
2774 && TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
2776 else if (cxx_dialect
>= cxx20
2777 && TREE_CODE (TREE_TYPE (t
)) == UNION_TYPE
)
2779 if (CONSTRUCTOR_NELTS (t
) == 0)
2780 /* An initialized union has a constructor element. */
2782 /* And it only initializes one member. */
2786 field
= next_initializable_field (TYPE_FIELDS (TREE_TYPE (t
)));
2790 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t
), i
, idx
, val
)
2792 /* If VAL is null, we're in the middle of initializing this
2794 if (!reduced_constant_expression_p (val
))
2796 /* Empty class field may or may not have an initializer. */
2797 for (; field
&& idx
!= field
;
2798 field
= next_initializable_field (DECL_CHAIN (field
)))
2799 if (!is_really_empty_class (TREE_TYPE (field
),
2800 /*ignore_vptr*/false))
2803 field
= next_initializable_field (DECL_CHAIN (field
));
2805 /* There could be a non-empty field at the end. */
2806 for (; field
; field
= next_initializable_field (DECL_CHAIN (field
)))
2807 if (!is_really_empty_class (TREE_TYPE (field
), /*ignore_vptr*/false))
2809 if (CONSTRUCTOR_NO_CLEARING (t
))
2810 /* All the fields are initialized. */
2811 CONSTRUCTOR_NO_CLEARING (t
) = false;
2815 /* FIXME are we calling this too much? */
2816 return initializer_constant_valid_p (t
, TREE_TYPE (t
)) != NULL_TREE
;
2820 /* Some expressions may have constant operands but are not constant
2821 themselves, such as 1/0. Call this function to check for that
2824 We only call this in places that require an arithmetic constant, not in
2825 places where we might have a non-constant expression that can be a
2826 component of a constant expression, such as the address of a constexpr
2827 variable that might be dereferenced later. */
2830 verify_constant (tree t
, bool allow_non_constant
, bool *non_constant_p
,
2833 if (!*non_constant_p
&& !reduced_constant_expression_p (t
)
2836 if (!allow_non_constant
)
2837 error ("%q+E is not a constant expression", t
);
2838 *non_constant_p
= true;
2840 if (TREE_OVERFLOW_P (t
))
2842 if (!allow_non_constant
)
2844 permerror (input_location
, "overflow in constant expression");
2845 /* If we're being permissive (and are in an enforcing
2846 context), ignore the overflow. */
2847 if (flag_permissive
)
2848 return *non_constant_p
;
2852 return *non_constant_p
;
2855 /* Check whether the shift operation with code CODE and type TYPE on LHS
2856 and RHS is undefined. If it is, give an error with an explanation,
2857 and return true; return false otherwise. */
2860 cxx_eval_check_shift_p (location_t loc
, const constexpr_ctx
*ctx
,
2861 enum tree_code code
, tree type
, tree lhs
, tree rhs
)
2863 if ((code
!= LSHIFT_EXPR
&& code
!= RSHIFT_EXPR
)
2864 || TREE_CODE (lhs
) != INTEGER_CST
2865 || TREE_CODE (rhs
) != INTEGER_CST
)
2868 tree lhstype
= TREE_TYPE (lhs
);
2869 unsigned HOST_WIDE_INT uprec
= TYPE_PRECISION (TREE_TYPE (lhs
));
2871 /* [expr.shift] The behavior is undefined if the right operand
2872 is negative, or greater than or equal to the length in bits
2873 of the promoted left operand. */
2874 if (tree_int_cst_sgn (rhs
) == -1)
2877 permerror (loc
, "right operand of shift expression %q+E is negative",
2878 build2_loc (loc
, code
, type
, lhs
, rhs
));
2879 return (!flag_permissive
|| ctx
->quiet
);
2881 if (compare_tree_int (rhs
, uprec
) >= 0)
2884 permerror (loc
, "right operand of shift expression %q+E is greater "
2885 "than or equal to the precision %wu of the left operand",
2886 build2_loc (loc
, code
, type
, lhs
, rhs
), uprec
);
2887 return (!flag_permissive
|| ctx
->quiet
);
2890 /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
2891 if E1 has a signed type and non-negative value, and E1x2^E2 is
2892 representable in the corresponding unsigned type of the result type,
2893 then that value, converted to the result type, is the resulting value;
2894 otherwise, the behavior is undefined.
2896 The value of E1 << E2 is the unique value congruent to E1 x 2^E2 modulo
2897 2^N, where N is the range exponent of the type of the result. */
2898 if (code
== LSHIFT_EXPR
2899 && !TYPE_UNSIGNED (lhstype
)
2900 && cxx_dialect
>= cxx11
2901 && cxx_dialect
< cxx20
)
2903 if (tree_int_cst_sgn (lhs
) == -1)
2907 "left operand of shift expression %q+E is negative",
2908 build2_loc (loc
, code
, type
, lhs
, rhs
));
2909 return (!flag_permissive
|| ctx
->quiet
);
2911 /* For signed x << y the following:
2912 (unsigned) x >> ((prec (lhs) - 1) - y)
2913 if > 1, is undefined. The right-hand side of this formula
2914 is the highest bit of the LHS that can be set (starting from 0),
2915 so that the shift doesn't overflow. We then right-shift the LHS
2916 to see whether any other bit is set making the original shift
2917 undefined -- the result is not representable in the corresponding
2919 tree t
= build_int_cst (unsigned_type_node
, uprec
- 1);
2920 t
= fold_build2 (MINUS_EXPR
, unsigned_type_node
, t
, rhs
);
2921 tree ulhs
= fold_convert (unsigned_type_for (lhstype
), lhs
);
2922 t
= fold_build2 (RSHIFT_EXPR
, TREE_TYPE (ulhs
), ulhs
, t
);
2923 if (tree_int_cst_lt (integer_one_node
, t
))
2926 permerror (loc
, "shift expression %q+E overflows",
2927 build2_loc (loc
, code
, type
, lhs
, rhs
));
2928 return (!flag_permissive
|| ctx
->quiet
);
2934 /* Subroutine of cxx_eval_constant_expression.
2935 Attempt to reduce the unary expression tree T to a compile time value.
2936 If successful, return the value. Otherwise issue a diagnostic
2937 and return error_mark_node. */
2940 cxx_eval_unary_expression (const constexpr_ctx
*ctx
, tree t
,
2942 bool *non_constant_p
, bool *overflow_p
)
2945 tree orig_arg
= TREE_OPERAND (t
, 0);
2946 tree arg
= cxx_eval_constant_expression (ctx
, orig_arg
, /*lval*/false,
2947 non_constant_p
, overflow_p
);
2948 VERIFY_CONSTANT (arg
);
2949 location_t loc
= EXPR_LOCATION (t
);
2950 enum tree_code code
= TREE_CODE (t
);
2951 tree type
= TREE_TYPE (t
);
2952 r
= fold_unary_loc (loc
, code
, type
, arg
);
2955 if (arg
== orig_arg
)
2958 r
= build1_loc (loc
, code
, type
, arg
);
2960 VERIFY_CONSTANT (r
);
2964 /* Helper function for cxx_eval_binary_expression. Try to optimize
2965 original POINTER_PLUS_EXPR T, LHS p+ RHS, return NULL_TREE if the
2966 generic folding should be used. */
2969 cxx_fold_pointer_plus_expression (const constexpr_ctx
*ctx
, tree t
,
2970 tree lhs
, tree rhs
, bool *non_constant_p
,
2974 if (TREE_CODE (lhs
) != ADDR_EXPR
)
2977 lhs
= TREE_OPERAND (lhs
, 0);
2979 /* &A[i] p+ j => &A[i + j] */
2980 if (TREE_CODE (lhs
) == ARRAY_REF
2981 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == INTEGER_CST
2982 && TREE_CODE (rhs
) == INTEGER_CST
2983 && TYPE_SIZE_UNIT (TREE_TYPE (lhs
))
2984 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs
))) == INTEGER_CST
)
2986 tree orig_type
= TREE_TYPE (t
);
2987 location_t loc
= EXPR_LOCATION (t
);
2988 tree type
= TREE_TYPE (lhs
);
2990 t
= fold_convert_loc (loc
, ssizetype
, TREE_OPERAND (lhs
, 1));
2991 tree nelts
= array_type_nelts_top (TREE_TYPE (TREE_OPERAND (lhs
, 0)));
2992 nelts
= cxx_eval_constant_expression (ctx
, nelts
, false, non_constant_p
,
2994 if (*non_constant_p
)
2996 /* Don't fold an out-of-bound access. */
2997 if (!tree_int_cst_le (t
, nelts
))
2999 rhs
= cp_fold_convert (ssizetype
, rhs
);
3000 /* Don't fold if rhs can't be divided exactly by TYPE_SIZE_UNIT.
3001 constexpr int A[1]; ... (char *)&A[0] + 1 */
3002 if (!integer_zerop (fold_build2_loc (loc
, TRUNC_MOD_EXPR
, sizetype
,
3003 rhs
, TYPE_SIZE_UNIT (type
))))
3005 /* Make sure to treat the second operand of POINTER_PLUS_EXPR
3007 rhs
= fold_build2_loc (loc
, EXACT_DIV_EXPR
, ssizetype
, rhs
,
3008 TYPE_SIZE_UNIT (type
));
3009 t
= size_binop_loc (loc
, PLUS_EXPR
, rhs
, t
);
3010 t
= build4_loc (loc
, ARRAY_REF
, type
, TREE_OPERAND (lhs
, 0),
3011 t
, NULL_TREE
, NULL_TREE
);
3012 t
= cp_build_addr_expr (t
, tf_warning_or_error
);
3013 t
= cp_fold_convert (orig_type
, t
);
3014 return cxx_eval_constant_expression (ctx
, t
, /*lval*/false,
3015 non_constant_p
, overflow_p
);
3021 /* Subroutine of cxx_eval_constant_expression.
3022 Like cxx_eval_unary_expression, except for binary expressions. */
3025 cxx_eval_binary_expression (const constexpr_ctx
*ctx
, tree t
,
3027 bool *non_constant_p
, bool *overflow_p
)
3030 tree orig_lhs
= TREE_OPERAND (t
, 0);
3031 tree orig_rhs
= TREE_OPERAND (t
, 1);
3033 lhs
= cxx_eval_constant_expression (ctx
, orig_lhs
, /*lval*/false,
3034 non_constant_p
, overflow_p
);
3035 /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer
3037 if (*non_constant_p
)
3039 rhs
= cxx_eval_constant_expression (ctx
, orig_rhs
, /*lval*/false,
3040 non_constant_p
, overflow_p
);
3041 if (*non_constant_p
)
3044 location_t loc
= EXPR_LOCATION (t
);
3045 enum tree_code code
= TREE_CODE (t
);
3046 tree type
= TREE_TYPE (t
);
3048 if (code
== EQ_EXPR
|| code
== NE_EXPR
)
3050 bool is_code_eq
= (code
== EQ_EXPR
);
3052 if (TREE_CODE (lhs
) == PTRMEM_CST
3053 && TREE_CODE (rhs
) == PTRMEM_CST
)
3055 tree lmem
= PTRMEM_CST_MEMBER (lhs
);
3056 tree rmem
= PTRMEM_CST_MEMBER (rhs
);
3058 if (TREE_CODE (lmem
) == TREE_CODE (rmem
)
3059 && TREE_CODE (lmem
) == FIELD_DECL
3060 && TREE_CODE (DECL_CONTEXT (lmem
)) == UNION_TYPE
3061 && same_type_p (DECL_CONTEXT (lmem
),
3062 DECL_CONTEXT (rmem
)))
3063 /* If both refer to (possibly different) members of the same union
3064 (12.3), they compare equal. */
3067 eq
= cp_tree_equal (lhs
, rhs
);
3068 r
= constant_boolean_node (eq
== is_code_eq
, type
);
3070 else if ((TREE_CODE (lhs
) == PTRMEM_CST
3071 || TREE_CODE (rhs
) == PTRMEM_CST
)
3072 && (null_member_pointer_value_p (lhs
)
3073 || null_member_pointer_value_p (rhs
)))
3074 r
= constant_boolean_node (!is_code_eq
, type
);
3075 else if (TREE_CODE (lhs
) == PTRMEM_CST
)
3076 lhs
= cplus_expand_constant (lhs
);
3077 else if (TREE_CODE (rhs
) == PTRMEM_CST
)
3078 rhs
= cplus_expand_constant (rhs
);
3080 if (code
== POINTER_PLUS_EXPR
&& !*non_constant_p
3081 && integer_zerop (lhs
) && !integer_zerop (rhs
))
3084 error ("arithmetic involving a null pointer in %qE", lhs
);
3085 *non_constant_p
= true;
3088 else if (code
== POINTER_PLUS_EXPR
)
3089 r
= cxx_fold_pointer_plus_expression (ctx
, t
, lhs
, rhs
, non_constant_p
,
3091 else if (code
== SPACESHIP_EXPR
)
3093 r
= genericize_spaceship (type
, lhs
, rhs
);
3094 return cxx_eval_constant_expression (ctx
, r
, lval
, non_constant_p
,
3099 r
= fold_binary_loc (loc
, code
, type
, lhs
, rhs
);
3103 if (lhs
== orig_lhs
&& rhs
== orig_rhs
)
3106 r
= build2_loc (loc
, code
, type
, lhs
, rhs
);
3108 else if (cxx_eval_check_shift_p (loc
, ctx
, code
, type
, lhs
, rhs
))
3109 *non_constant_p
= true;
3110 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
3111 a local array in a constexpr function. */
3112 bool ptr
= INDIRECT_TYPE_P (TREE_TYPE (lhs
));
3114 VERIFY_CONSTANT (r
);
3118 /* Subroutine of cxx_eval_constant_expression.
3119 Attempt to evaluate condition expressions. Dead branches are not
3123 cxx_eval_conditional_expression (const constexpr_ctx
*ctx
, tree t
,
3125 bool *non_constant_p
, bool *overflow_p
,
3128 tree val
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
3130 non_constant_p
, overflow_p
);
3131 VERIFY_CONSTANT (val
);
3132 /* Don't VERIFY_CONSTANT the other operands. */
3133 if (integer_zerop (val
))
3134 val
= TREE_OPERAND (t
, 2);
3136 val
= TREE_OPERAND (t
, 1);
3137 if (TREE_CODE (t
) == IF_STMT
&& !val
)
3139 return cxx_eval_constant_expression (ctx
, val
, lval
, non_constant_p
,
3140 overflow_p
, jump_target
);
3143 /* Subroutine of cxx_eval_constant_expression.
3144 Attempt to evaluate vector condition expressions. Unlike
3145 cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal
3146 ternary arithmetics operation, where all 3 arguments have to be
3147 evaluated as constants and then folding computes the result from
3151 cxx_eval_vector_conditional_expression (const constexpr_ctx
*ctx
, tree t
,
3152 bool *non_constant_p
, bool *overflow_p
)
3154 tree arg1
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
3156 non_constant_p
, overflow_p
);
3157 VERIFY_CONSTANT (arg1
);
3158 tree arg2
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
3160 non_constant_p
, overflow_p
);
3161 VERIFY_CONSTANT (arg2
);
3162 tree arg3
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 2),
3164 non_constant_p
, overflow_p
);
3165 VERIFY_CONSTANT (arg3
);
3166 location_t loc
= EXPR_LOCATION (t
);
3167 tree type
= TREE_TYPE (t
);
3168 tree r
= fold_ternary_loc (loc
, VEC_COND_EXPR
, type
, arg1
, arg2
, arg3
);
3171 if (arg1
== TREE_OPERAND (t
, 0)
3172 && arg2
== TREE_OPERAND (t
, 1)
3173 && arg3
== TREE_OPERAND (t
, 2))
3176 r
= build3_loc (loc
, VEC_COND_EXPR
, type
, arg1
, arg2
, arg3
);
3178 VERIFY_CONSTANT (r
);
3182 /* Returns less than, equal to, or greater than zero if KEY is found to be
3183 less than, to match, or to be greater than the constructor_elt's INDEX. */
3186 array_index_cmp (tree key
, tree index
)
3188 gcc_assert (TREE_CODE (key
) == INTEGER_CST
);
3190 switch (TREE_CODE (index
))
3193 return tree_int_cst_compare (key
, index
);
3196 tree lo
= TREE_OPERAND (index
, 0);
3197 tree hi
= TREE_OPERAND (index
, 1);
3198 if (tree_int_cst_lt (key
, lo
))
3200 else if (tree_int_cst_lt (hi
, key
))
3210 /* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1
3211 if none. If INSERT is true, insert a matching element rather than fail. */
3213 static HOST_WIDE_INT
3214 find_array_ctor_elt (tree ary
, tree dindex
, bool insert
)
3216 if (tree_int_cst_sgn (dindex
) < 0)
3219 unsigned HOST_WIDE_INT i
= tree_to_uhwi (dindex
);
3220 vec
<constructor_elt
, va_gc
> *elts
= CONSTRUCTOR_ELTS (ary
);
3221 unsigned HOST_WIDE_INT len
= vec_safe_length (elts
);
3223 unsigned HOST_WIDE_INT end
= len
;
3224 unsigned HOST_WIDE_INT begin
= 0;
3226 /* If the last element of the CONSTRUCTOR has its own index, we can assume
3227 that the same is true of the other elements and index directly. */
3230 tree cindex
= (*elts
)[end
- 1].index
;
3231 if (cindex
== NULL_TREE
)
3233 /* Verify that if the last index is missing, all indexes
3236 for (unsigned int j
= 0; j
< len
- 1; ++j
)
3237 gcc_assert ((*elts
)[j
].index
== NULL_TREE
);
3244 /* If the element is to be added right at the end,
3245 make sure it is added with cleared index too. */
3248 /* Otherwise, in order not to break the assumption
3249 that CONSTRUCTOR either has all indexes or none,
3250 we need to add indexes to all elements. */
3251 for (unsigned int j
= 0; j
< len
; ++j
)
3252 (*elts
)[j
].index
= build_int_cst (TREE_TYPE (dindex
), j
);
3255 else if (TREE_CODE (cindex
) == INTEGER_CST
3256 && compare_tree_int (cindex
, end
- 1) == 0)
3265 /* Otherwise, find a matching index by means of a binary search. */
3266 while (begin
!= end
)
3268 unsigned HOST_WIDE_INT middle
= (begin
+ end
) / 2;
3269 constructor_elt
&elt
= (*elts
)[middle
];
3270 tree idx
= elt
.index
;
3272 int cmp
= array_index_cmp (dindex
, idx
);
3279 if (insert
&& TREE_CODE (idx
) == RANGE_EXPR
)
3281 /* We need to split the range. */
3283 tree lo
= TREE_OPERAND (idx
, 0);
3284 tree hi
= TREE_OPERAND (idx
, 1);
3285 tree value
= elt
.value
;
3286 dindex
= fold_convert (sizetype
, dindex
);
3287 if (tree_int_cst_lt (lo
, dindex
))
3289 /* There are still some lower elts; shorten the range. */
3290 tree new_hi
= int_const_binop (MINUS_EXPR
, dindex
,
3292 if (tree_int_cst_equal (lo
, new_hi
))
3293 /* Only one element left, no longer a range. */
3296 TREE_OPERAND (idx
, 1) = new_hi
;
3297 /* Append the element we want to insert. */
3300 e
.value
= unshare_constructor (value
);
3301 vec_safe_insert (CONSTRUCTOR_ELTS (ary
), middle
, e
);
3304 /* No lower elts, the range elt is now ours. */
3307 if (tree_int_cst_lt (dindex
, hi
))
3309 /* There are still some higher elts; append a range. */
3310 tree new_lo
= int_const_binop (PLUS_EXPR
, dindex
,
3312 if (tree_int_cst_equal (new_lo
, hi
))
3315 e
.index
= build2 (RANGE_EXPR
, sizetype
, new_lo
, hi
);
3316 e
.value
= unshare_constructor (value
);
3317 vec_safe_insert (CONSTRUCTOR_ELTS (ary
), middle
+ 1, e
);
3326 constructor_elt e
= { dindex
, NULL_TREE
};
3327 vec_safe_insert (CONSTRUCTOR_ELTS (ary
), end
, e
);
3334 /* Return a pointer to the constructor_elt of CTOR which matches INDEX. If no
3335 matching constructor_elt exists, then add one to CTOR.
3337 As an optimization, if POS_HINT is non-negative then it is used as a guess
3338 for the (integer) index of the matching constructor_elt within CTOR. */
3340 static constructor_elt
*
3341 get_or_insert_ctor_field (tree ctor
, tree index
, int pos_hint
= -1)
3343 /* Check the hint first. */
3344 if (pos_hint
>= 0 && (unsigned)pos_hint
< CONSTRUCTOR_NELTS (ctor
)
3345 && CONSTRUCTOR_ELT (ctor
, pos_hint
)->index
== index
)
3346 return CONSTRUCTOR_ELT (ctor
, pos_hint
);
3348 tree type
= TREE_TYPE (ctor
);
3349 if (TREE_CODE (type
) == VECTOR_TYPE
&& index
== NULL_TREE
)
3351 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (ctor
), index
, NULL_TREE
);
3352 return &CONSTRUCTOR_ELTS (ctor
)->last();
3354 else if (TREE_CODE (type
) == ARRAY_TYPE
|| TREE_CODE (type
) == VECTOR_TYPE
)
3356 if (TREE_CODE (index
) == RANGE_EXPR
)
3358 /* Support for RANGE_EXPR index lookups is currently limited to
3359 accessing an existing element via POS_HINT, or appending a new
3360 element to the end of CTOR. ??? Support for other access
3361 patterns may also be needed. */
3362 vec
<constructor_elt
, va_gc
> *elts
= CONSTRUCTOR_ELTS (ctor
);
3363 if (vec_safe_length (elts
))
3365 tree lo
= TREE_OPERAND (index
, 0);
3366 gcc_assert (array_index_cmp (elts
->last().index
, lo
) < 0);
3368 CONSTRUCTOR_APPEND_ELT (elts
, index
, NULL_TREE
);
3369 return &elts
->last();
3372 HOST_WIDE_INT i
= find_array_ctor_elt (ctor
, index
, /*insert*/true);
3373 gcc_assert (i
>= 0);
3374 constructor_elt
*cep
= CONSTRUCTOR_ELT (ctor
, i
);
3375 gcc_assert (cep
->index
== NULL_TREE
3376 || TREE_CODE (cep
->index
) != RANGE_EXPR
);
3381 gcc_assert (TREE_CODE (index
) == FIELD_DECL
);
3383 /* We must keep the CONSTRUCTOR's ELTS in FIELD order.
3384 Usually we meet initializers in that order, but it is
3385 possible for base types to be placed not in program
3387 tree fields
= TYPE_FIELDS (DECL_CONTEXT (index
));
3388 unsigned HOST_WIDE_INT idx
= 0;
3389 constructor_elt
*cep
= NULL
;
3391 /* Check if we're changing the active member of a union. */
3392 if (TREE_CODE (type
) == UNION_TYPE
&& CONSTRUCTOR_NELTS (ctor
)
3393 && CONSTRUCTOR_ELT (ctor
, 0)->index
!= index
)
3394 vec_safe_truncate (CONSTRUCTOR_ELTS (ctor
), 0);
3395 /* If the bit offset of INDEX is larger than that of the last
3396 constructor_elt, then we can just immediately append a new
3397 constructor_elt to the end of CTOR. */
3398 else if (CONSTRUCTOR_NELTS (ctor
)
3399 && tree_int_cst_compare (bit_position (index
),
3400 bit_position (CONSTRUCTOR_ELTS (ctor
)
3401 ->last().index
)) > 0)
3403 idx
= CONSTRUCTOR_NELTS (ctor
);
3407 /* Otherwise, we need to iterate over CTOR to find or insert INDEX
3410 for (; vec_safe_iterate (CONSTRUCTOR_ELTS (ctor
), idx
, &cep
);
3411 idx
++, fields
= DECL_CHAIN (fields
))
3413 if (index
== cep
->index
)
3416 /* The field we're initializing must be on the field
3417 list. Look to see if it is present before the
3418 field the current ELT initializes. */
3419 for (; fields
!= cep
->index
; fields
= DECL_CHAIN (fields
))
3420 if (index
== fields
)
3423 /* We fell off the end of the CONSTRUCTOR, so insert a new
3424 entry at the end. */
3428 constructor_elt ce
= { index
, NULL_TREE
};
3430 vec_safe_insert (CONSTRUCTOR_ELTS (ctor
), idx
, ce
);
3431 cep
= CONSTRUCTOR_ELT (ctor
, idx
);
3439 /* Under the control of CTX, issue a detailed diagnostic for
3440 an out-of-bounds subscript INDEX into the expression ARRAY. */
3443 diag_array_subscript (location_t loc
, const constexpr_ctx
*ctx
, tree array
, tree index
)
3447 tree arraytype
= TREE_TYPE (array
);
3449 /* Convert the unsigned array subscript to a signed integer to avoid
3450 printing huge numbers for small negative values. */
3451 tree sidx
= fold_convert (ssizetype
, index
);
3452 STRIP_ANY_LOCATION_WRAPPER (array
);
3455 if (TYPE_DOMAIN (arraytype
))
3456 error_at (loc
, "array subscript value %qE is outside the bounds "
3457 "of array %qD of type %qT", sidx
, array
, arraytype
);
3459 error_at (loc
, "nonzero array subscript %qE is used with array %qD of "
3460 "type %qT with unknown bounds", sidx
, array
, arraytype
);
3461 inform (DECL_SOURCE_LOCATION (array
), "declared here");
3463 else if (TYPE_DOMAIN (arraytype
))
3464 error_at (loc
, "array subscript value %qE is outside the bounds "
3465 "of array type %qT", sidx
, arraytype
);
3467 error_at (loc
, "nonzero array subscript %qE is used with array of type %qT "
3468 "with unknown bounds", sidx
, arraytype
);
3472 /* Return the number of elements for TYPE (which is an ARRAY_TYPE or
3476 get_array_or_vector_nelts (const constexpr_ctx
*ctx
, tree type
,
3477 bool *non_constant_p
, bool *overflow_p
)
3480 if (TREE_CODE (type
) == ARRAY_TYPE
)
3482 if (TYPE_DOMAIN (type
))
3483 nelts
= array_type_nelts_top (type
);
3485 nelts
= size_zero_node
;
3487 else if (VECTOR_TYPE_P (type
))
3488 nelts
= size_int (TYPE_VECTOR_SUBPARTS (type
));
3492 /* For VLAs, the number of elements won't be an integer constant. */
3493 nelts
= cxx_eval_constant_expression (ctx
, nelts
, false,
3494 non_constant_p
, overflow_p
);
3498 /* Extract element INDEX consisting of CHARS_PER_ELT chars from
3499 STRING_CST STRING. */
3502 extract_string_elt (tree string
, unsigned chars_per_elt
, unsigned index
)
3504 tree type
= cv_unqualified (TREE_TYPE (TREE_TYPE (string
)));
3507 if (chars_per_elt
== 1)
3508 r
= build_int_cst (type
, TREE_STRING_POINTER (string
)[index
]);
3511 const unsigned char *ptr
3512 = ((const unsigned char *)TREE_STRING_POINTER (string
)
3513 + index
* chars_per_elt
);
3514 r
= native_interpret_expr (type
, ptr
, chars_per_elt
);
3519 /* Subroutine of cxx_eval_array_reference. T is an ARRAY_REF; evaluate the
3520 subscript, diagnose any problems with it, and return the result. */
3523 eval_and_check_array_index (const constexpr_ctx
*ctx
,
3524 tree t
, bool allow_one_past
,
3525 bool *non_constant_p
, bool *overflow_p
)
3527 location_t loc
= cp_expr_loc_or_input_loc (t
);
3528 tree ary
= TREE_OPERAND (t
, 0);
3529 t
= TREE_OPERAND (t
, 1);
3530 tree index
= cxx_eval_constant_expression (ctx
, t
, false,
3531 non_constant_p
, overflow_p
);
3532 VERIFY_CONSTANT (index
);
3534 if (!tree_fits_shwi_p (index
)
3535 || tree_int_cst_sgn (index
) < 0)
3537 diag_array_subscript (loc
, ctx
, ary
, index
);
3538 *non_constant_p
= true;
3542 tree nelts
= get_array_or_vector_nelts (ctx
, TREE_TYPE (ary
), non_constant_p
,
3544 VERIFY_CONSTANT (nelts
);
3546 ? !tree_int_cst_le (index
, nelts
)
3547 : !tree_int_cst_lt (index
, nelts
))
3549 diag_array_subscript (loc
, ctx
, ary
, index
);
3550 *non_constant_p
= true;
3557 /* Subroutine of cxx_eval_constant_expression.
3558 Attempt to reduce a reference to an array slot. */
3561 cxx_eval_array_reference (const constexpr_ctx
*ctx
, tree t
,
3563 bool *non_constant_p
, bool *overflow_p
)
3565 tree oldary
= TREE_OPERAND (t
, 0);
3566 tree ary
= cxx_eval_constant_expression (ctx
, oldary
,
3568 non_constant_p
, overflow_p
);
3569 if (*non_constant_p
)
3572 && TREE_CODE (ary
) == VIEW_CONVERT_EXPR
3573 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary
, 0)))
3574 && TREE_TYPE (t
) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary
, 0))))
3575 ary
= TREE_OPERAND (ary
, 0);
3577 tree oldidx
= TREE_OPERAND (t
, 1);
3578 tree index
= eval_and_check_array_index (ctx
, t
, lval
,
3579 non_constant_p
, overflow_p
);
3580 if (*non_constant_p
)
3583 if (lval
&& ary
== oldary
&& index
== oldidx
)
3586 return build4 (ARRAY_REF
, TREE_TYPE (t
), ary
, index
, NULL
, NULL
);
3588 unsigned len
= 0, elem_nchars
= 1;
3589 tree elem_type
= TREE_TYPE (TREE_TYPE (ary
));
3590 if (TREE_CODE (ary
) == CONSTRUCTOR
)
3591 len
= CONSTRUCTOR_NELTS (ary
);
3592 else if (TREE_CODE (ary
) == STRING_CST
)
3594 elem_nchars
= (TYPE_PRECISION (elem_type
)
3595 / TYPE_PRECISION (char_type_node
));
3596 len
= (unsigned) TREE_STRING_LENGTH (ary
) / elem_nchars
;
3598 else if (TREE_CODE (ary
) == VECTOR_CST
)
3599 /* We don't create variable-length VECTOR_CSTs. */
3600 len
= VECTOR_CST_NELTS (ary
).to_constant ();
3603 /* We can't do anything with other tree codes, so use
3604 VERIFY_CONSTANT to complain and fail. */
3605 VERIFY_CONSTANT (ary
);
3610 HOST_WIDE_INT i
= 0;
3611 if (TREE_CODE (ary
) == CONSTRUCTOR
)
3613 HOST_WIDE_INT ix
= find_array_ctor_elt (ary
, index
);
3620 i
= tree_to_shwi (index
);
3627 if (TREE_CODE (ary
) == CONSTRUCTOR
)
3628 r
= (*CONSTRUCTOR_ELTS (ary
))[i
].value
;
3629 else if (TREE_CODE (ary
) == VECTOR_CST
)
3630 r
= VECTOR_CST_ELT (ary
, i
);
3632 r
= extract_string_elt (ary
, elem_nchars
, i
);
3635 /* Don't VERIFY_CONSTANT here. */
3638 /* Otherwise the element doesn't have a value yet. */
3643 if (TREE_CODE (ary
) == CONSTRUCTOR
3644 && CONSTRUCTOR_NO_CLEARING (ary
))
3646 /* 'ary' is part of the aggregate initializer we're currently
3647 building; if there's no initializer for this element yet,
3650 error ("accessing uninitialized array element");
3651 *non_constant_p
= true;
3655 /* If it's within the array bounds but doesn't have an explicit
3656 initializer, it's initialized from {}. But use build_value_init
3657 directly for non-aggregates to avoid creating a garbage CONSTRUCTOR. */
3659 if (CP_AGGREGATE_TYPE_P (elem_type
))
3661 tree empty_ctor
= build_constructor (init_list_type_node
, NULL
);
3662 val
= digest_init (elem_type
, empty_ctor
, tf_warning_or_error
);
3665 val
= build_value_init (elem_type
, tf_warning_or_error
);
3666 return cxx_eval_constant_expression (ctx
, val
, lval
, non_constant_p
,
3670 /* Subroutine of cxx_eval_constant_expression.
3671 Attempt to reduce a field access of a value of class type. */
3674 cxx_eval_component_reference (const constexpr_ctx
*ctx
, tree t
,
3676 bool *non_constant_p
, bool *overflow_p
)
3678 unsigned HOST_WIDE_INT i
;
3681 tree part
= TREE_OPERAND (t
, 1);
3682 tree orig_whole
= TREE_OPERAND (t
, 0);
3683 tree whole
= cxx_eval_constant_expression (ctx
, orig_whole
,
3685 non_constant_p
, overflow_p
);
3686 if (INDIRECT_REF_P (whole
)
3687 && integer_zerop (TREE_OPERAND (whole
, 0)))
3690 error ("dereferencing a null pointer in %qE", orig_whole
);
3691 *non_constant_p
= true;
3695 if (TREE_CODE (whole
) == PTRMEM_CST
)
3696 whole
= cplus_expand_constant (whole
);
3697 if (whole
== orig_whole
)
3700 return fold_build3 (COMPONENT_REF
, TREE_TYPE (t
),
3701 whole
, part
, NULL_TREE
);
3702 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
3704 if (!*non_constant_p
&& TREE_CODE (whole
) != CONSTRUCTOR
)
3707 error ("%qE is not a constant expression", orig_whole
);
3708 *non_constant_p
= true;
3710 if (DECL_MUTABLE_P (part
))
3713 error ("mutable %qD is not usable in a constant expression", part
);
3714 *non_constant_p
= true;
3716 if (*non_constant_p
)
3718 bool pmf
= TYPE_PTRMEMFUNC_P (TREE_TYPE (whole
));
3719 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole
), i
, field
, value
)
3721 /* Use name match for PMF fields, as a variant will have a
3722 different FIELD_DECL with a different type. */
3723 if (pmf
? DECL_NAME (field
) == DECL_NAME (part
)
3728 STRIP_ANY_LOCATION_WRAPPER (value
);
3732 /* We're in the middle of initializing it. */
3736 if (TREE_CODE (TREE_TYPE (whole
)) == UNION_TYPE
3737 && CONSTRUCTOR_NELTS (whole
) > 0)
3739 /* DR 1188 says we don't have to deal with this. */
3742 constructor_elt
*cep
= CONSTRUCTOR_ELT (whole
, 0);
3743 if (cep
->value
== NULL_TREE
)
3744 error ("accessing uninitialized member %qD", part
);
3746 error ("accessing %qD member instead of initialized %qD member in "
3747 "constant expression", part
, cep
->index
);
3749 *non_constant_p
= true;
3753 /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty
3754 classes never get represented; throw together a value now. */
3755 if (is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
3756 return build_constructor (TREE_TYPE (t
), NULL
);
3758 gcc_assert (DECL_CONTEXT (part
) == TYPE_MAIN_VARIANT (TREE_TYPE (whole
)));
3760 if (CONSTRUCTOR_NO_CLEARING (whole
))
3762 /* 'whole' is part of the aggregate initializer we're currently
3763 building; if there's no initializer for this member yet, that's an
3766 error ("accessing uninitialized member %qD", part
);
3767 *non_constant_p
= true;
3771 /* If there's no explicit init for this field, it's value-initialized. */
3772 value
= build_value_init (TREE_TYPE (t
), tf_warning_or_error
);
3773 return cxx_eval_constant_expression (ctx
, value
,
3775 non_constant_p
, overflow_p
);
3778 /* Subroutine of cxx_eval_constant_expression.
3779 Attempt to reduce a field access of a value of class type that is
3780 expressed as a BIT_FIELD_REF. */
3783 cxx_eval_bit_field_ref (const constexpr_ctx
*ctx
, tree t
,
3785 bool *non_constant_p
, bool *overflow_p
)
3787 tree orig_whole
= TREE_OPERAND (t
, 0);
3788 tree retval
, fldval
, utype
, mask
;
3789 bool fld_seen
= false;
3790 HOST_WIDE_INT istart
, isize
;
3791 tree whole
= cxx_eval_constant_expression (ctx
, orig_whole
,
3793 non_constant_p
, overflow_p
);
3794 tree start
, field
, value
;
3795 unsigned HOST_WIDE_INT i
;
3797 if (whole
== orig_whole
)
3799 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
3801 if (!*non_constant_p
3802 && TREE_CODE (whole
) != VECTOR_CST
3803 && TREE_CODE (whole
) != CONSTRUCTOR
)
3806 error ("%qE is not a constant expression", orig_whole
);
3807 *non_constant_p
= true;
3809 if (*non_constant_p
)
3812 if (TREE_CODE (whole
) == VECTOR_CST
)
3813 return fold_ternary (BIT_FIELD_REF
, TREE_TYPE (t
), whole
,
3814 TREE_OPERAND (t
, 1), TREE_OPERAND (t
, 2));
3816 start
= TREE_OPERAND (t
, 2);
3817 istart
= tree_to_shwi (start
);
3818 isize
= tree_to_shwi (TREE_OPERAND (t
, 1));
3819 utype
= TREE_TYPE (t
);
3820 if (!TYPE_UNSIGNED (utype
))
3821 utype
= build_nonstandard_integer_type (TYPE_PRECISION (utype
), 1);
3822 retval
= build_int_cst (utype
, 0);
3823 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole
), i
, field
, value
)
3825 tree bitpos
= bit_position (field
);
3826 STRIP_ANY_LOCATION_WRAPPER (value
);
3827 if (bitpos
== start
&& DECL_SIZE (field
) == TREE_OPERAND (t
, 1))
3829 if (TREE_CODE (TREE_TYPE (field
)) == INTEGER_TYPE
3830 && TREE_CODE (value
) == INTEGER_CST
3831 && tree_fits_shwi_p (bitpos
)
3832 && tree_fits_shwi_p (DECL_SIZE (field
)))
3834 HOST_WIDE_INT bit
= tree_to_shwi (bitpos
);
3835 HOST_WIDE_INT sz
= tree_to_shwi (DECL_SIZE (field
));
3836 HOST_WIDE_INT shift
;
3837 if (bit
>= istart
&& bit
+ sz
<= istart
+ isize
)
3839 fldval
= fold_convert (utype
, value
);
3840 mask
= build_int_cst_type (utype
, -1);
3841 mask
= fold_build2 (LSHIFT_EXPR
, utype
, mask
,
3842 size_int (TYPE_PRECISION (utype
) - sz
));
3843 mask
= fold_build2 (RSHIFT_EXPR
, utype
, mask
,
3844 size_int (TYPE_PRECISION (utype
) - sz
));
3845 fldval
= fold_build2 (BIT_AND_EXPR
, utype
, fldval
, mask
);
3846 shift
= bit
- istart
;
3847 if (BYTES_BIG_ENDIAN
)
3848 shift
= TYPE_PRECISION (utype
) - shift
- sz
;
3849 fldval
= fold_build2 (LSHIFT_EXPR
, utype
, fldval
,
3851 retval
= fold_build2 (BIT_IOR_EXPR
, utype
, retval
, fldval
);
3857 return fold_convert (TREE_TYPE (t
), retval
);
3859 return error_mark_node
;
3862 /* Subroutine of cxx_eval_constant_expression.
3863 Evaluate a short-circuited logical expression T in the context
3864 of a given constexpr CALL. BAILOUT_VALUE is the value for
3865 early return. CONTINUE_VALUE is used here purely for
3866 sanity check purposes. */
3869 cxx_eval_logical_expression (const constexpr_ctx
*ctx
, tree t
,
3870 tree bailout_value
, tree continue_value
,
3872 bool *non_constant_p
, bool *overflow_p
)
3875 tree lhs
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
3877 non_constant_p
, overflow_p
);
3878 VERIFY_CONSTANT (lhs
);
3879 if (tree_int_cst_equal (lhs
, bailout_value
))
3881 gcc_assert (tree_int_cst_equal (lhs
, continue_value
));
3882 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
3883 lval
, non_constant_p
,
3885 VERIFY_CONSTANT (r
);
3889 /* REF is a COMPONENT_REF designating a particular field. V is a vector of
3890 CONSTRUCTOR elements to initialize (part of) an object containing that
3891 field. Return a pointer to the constructor_elt corresponding to the
3892 initialization of the field. */
3894 static constructor_elt
*
3895 base_field_constructor_elt (vec
<constructor_elt
, va_gc
> *v
, tree ref
)
3897 tree aggr
= TREE_OPERAND (ref
, 0);
3898 tree field
= TREE_OPERAND (ref
, 1);
3900 constructor_elt
*ce
;
3902 gcc_assert (TREE_CODE (ref
) == COMPONENT_REF
);
3904 if (TREE_CODE (aggr
) == COMPONENT_REF
)
3906 constructor_elt
*base_ce
3907 = base_field_constructor_elt (v
, aggr
);
3908 v
= CONSTRUCTOR_ELTS (base_ce
->value
);
3911 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
3912 if (ce
->index
== field
)
3919 /* Some of the expressions fed to the constexpr mechanism are calls to
3920 constructors, which have type void. In that case, return the type being
3921 initialized by the constructor. */
3924 initialized_type (tree t
)
3928 tree type
= TREE_TYPE (t
);
3929 if (TREE_CODE (t
) == CALL_EXPR
)
3931 /* A constructor call has void type, so we need to look deeper. */
3932 tree fn
= get_function_named_in_call (t
);
3933 if (fn
&& TREE_CODE (fn
) == FUNCTION_DECL
3934 && DECL_CXX_CONSTRUCTOR_P (fn
))
3935 type
= DECL_CONTEXT (fn
);
3937 else if (TREE_CODE (t
) == COMPOUND_EXPR
)
3938 return initialized_type (TREE_OPERAND (t
, 1));
3939 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
)
3940 type
= TREE_TYPE (AGGR_INIT_EXPR_SLOT (t
));
3941 return cv_unqualified (type
);
3944 /* We're about to initialize element INDEX of an array or class from VALUE.
3945 Set up NEW_CTX appropriately by adjusting .object to refer to the
3946 subobject and creating a new CONSTRUCTOR if the element is itself
3947 a class or array. */
3950 init_subob_ctx (const constexpr_ctx
*ctx
, constexpr_ctx
&new_ctx
,
3951 tree index
, tree
&value
)
3955 if (index
&& TREE_CODE (index
) != INTEGER_CST
3956 && TREE_CODE (index
) != FIELD_DECL
)
3957 /* This won't have an element in the new CONSTRUCTOR. */
3960 tree type
= initialized_type (value
);
3961 if (!AGGREGATE_TYPE_P (type
) && !VECTOR_TYPE_P (type
))
3962 /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
3965 /* The sub-aggregate initializer might contain a placeholder;
3966 update object to refer to the subobject and ctor to refer to
3967 the (newly created) sub-initializer. */
3969 new_ctx
.object
= build_ctor_subob_ref (index
, type
, ctx
->object
);
3970 tree elt
= build_constructor (type
, NULL
);
3971 CONSTRUCTOR_NO_CLEARING (elt
) = true;
3974 if (TREE_CODE (value
) == TARGET_EXPR
)
3975 /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
3976 value
= TARGET_EXPR_INITIAL (value
);
3979 /* We're about to process an initializer for a class or array TYPE. Make
3980 sure that CTX is set up appropriately. */
3983 verify_ctor_sanity (const constexpr_ctx
*ctx
, tree type
)
3985 /* We don't bother building a ctor for an empty base subobject. */
3986 if (is_empty_class (type
))
3989 /* We're in the middle of an initializer that might involve placeholders;
3990 our caller should have created a CONSTRUCTOR for us to put the
3991 initializer into. We will either return that constructor or T. */
3992 gcc_assert (ctx
->ctor
);
3993 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3994 (type
, TREE_TYPE (ctx
->ctor
)));
3995 /* We used to check that ctx->ctor was empty, but that isn't the case when
3996 the object is zero-initialized before calling the constructor. */
3999 tree otype
= TREE_TYPE (ctx
->object
);
4000 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type
, otype
)
4001 /* Handle flexible array members. */
4002 || (TREE_CODE (otype
) == ARRAY_TYPE
4003 && TYPE_DOMAIN (otype
) == NULL_TREE
4004 && TREE_CODE (type
) == ARRAY_TYPE
4005 && (same_type_ignoring_top_level_qualifiers_p
4006 (TREE_TYPE (type
), TREE_TYPE (otype
)))));
4008 gcc_assert (!ctx
->object
|| !DECL_P (ctx
->object
)
4009 || *(ctx
->global
->values
.get (ctx
->object
)) == ctx
->ctor
);
4012 /* Subroutine of cxx_eval_constant_expression.
4013 The expression tree T denotes a C-style array or a C-style
4014 aggregate. Reduce it to a constant expression. */
4017 cxx_eval_bare_aggregate (const constexpr_ctx
*ctx
, tree t
,
4019 bool *non_constant_p
, bool *overflow_p
)
4021 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (t
);
4022 bool changed
= false;
4023 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t
));
4024 tree type
= TREE_TYPE (t
);
4026 constexpr_ctx new_ctx
;
4027 if (TYPE_PTRMEMFUNC_P (type
) || VECTOR_TYPE_P (type
))
4029 /* We don't really need the ctx->ctor business for a PMF or
4030 vector, but it's simpler to use the same code. */
4032 new_ctx
.ctor
= build_constructor (type
, NULL
);
4033 new_ctx
.object
= NULL_TREE
;
4036 verify_ctor_sanity (ctx
, type
);
4037 vec
<constructor_elt
, va_gc
> **p
= &CONSTRUCTOR_ELTS (ctx
->ctor
);
4038 vec_alloc (*p
, vec_safe_length (v
));
4040 if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (t
))
4041 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx
->ctor
) = 1;
4045 bool constant_p
= true;
4046 bool side_effects_p
= false;
4047 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, index
, value
)
4049 tree orig_value
= value
;
4050 init_subob_ctx (ctx
, new_ctx
, index
, value
);
4052 if (new_ctx
.ctor
!= ctx
->ctor
)
4054 /* If we built a new CONSTRUCTOR, attach it now so that other
4055 initializers can refer to it. */
4056 constructor_elt
*cep
= get_or_insert_ctor_field (ctx
->ctor
, index
);
4057 cep
->value
= new_ctx
.ctor
;
4058 pos_hint
= cep
- (*p
)->begin();
4060 else if (TREE_CODE (type
) == UNION_TYPE
)
4061 /* Otherwise if we're constructing a non-aggregate union member, set
4062 the active union member now so that we can later detect and diagnose
4063 if its initializer attempts to activate another member. */
4064 get_or_insert_ctor_field (ctx
->ctor
, index
);
4065 tree elt
= cxx_eval_constant_expression (&new_ctx
, value
,
4067 non_constant_p
, overflow_p
);
4068 /* Don't VERIFY_CONSTANT here. */
4069 if (ctx
->quiet
&& *non_constant_p
)
4071 if (elt
!= orig_value
)
4074 if (!TREE_CONSTANT (elt
))
4076 if (TREE_SIDE_EFFECTS (elt
))
4077 side_effects_p
= true;
4078 if (index
&& TREE_CODE (index
) == COMPONENT_REF
)
4080 /* This is an initialization of a vfield inside a base
4081 subaggregate that we already initialized; push this
4082 initialization into the previous initialization. */
4083 constructor_elt
*inner
= base_field_constructor_elt (*p
, index
);
4088 && (TREE_CODE (index
) == NOP_EXPR
4089 || TREE_CODE (index
) == POINTER_PLUS_EXPR
))
4091 /* This is an initializer for an empty base; now that we've
4092 checked that it's constant, we can ignore it. */
4093 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index
))));
4098 if (TREE_CODE (type
) == UNION_TYPE
4099 && (*p
)->last().index
!= index
)
4100 /* The initializer erroneously changed the active union member that
4101 we're initializing. */
4102 gcc_assert (*non_constant_p
);
4105 /* The initializer might have mutated the underlying CONSTRUCTOR,
4106 so recompute the location of the target constructer_elt. */
4107 constructor_elt
*cep
4108 = get_or_insert_ctor_field (ctx
->ctor
, index
, pos_hint
);
4112 /* Adding or replacing an element might change the ctor's flags. */
4113 TREE_CONSTANT (ctx
->ctor
) = constant_p
;
4114 TREE_SIDE_EFFECTS (ctx
->ctor
) = side_effects_p
;
4117 if (*non_constant_p
|| !changed
)
4120 /* We're done building this CONSTRUCTOR, so now we can interpret an
4121 element without an explicit initializer as value-initialized. */
4122 CONSTRUCTOR_NO_CLEARING (t
) = false;
4123 TREE_CONSTANT (t
) = constant_p
;
4124 TREE_SIDE_EFFECTS (t
) = side_effects_p
;
4125 if (VECTOR_TYPE_P (type
))
4130 /* Subroutine of cxx_eval_constant_expression.
4131 The expression tree T is a VEC_INIT_EXPR which denotes the desired
4132 initialization of a non-static data member of array type. Reduce it to a
4135 Note that apart from value-initialization (when VALUE_INIT is true),
4136 this is only intended to support value-initialization and the
4137 initializations done by defaulted constructors for classes with
4138 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
4139 will either be NULL_TREE for the default constructor, or a COMPONENT_REF
4140 for the copy/move constructor. */
4143 cxx_eval_vec_init_1 (const constexpr_ctx
*ctx
, tree atype
, tree init
,
4144 bool value_init
, bool lval
,
4145 bool *non_constant_p
, bool *overflow_p
)
4147 tree elttype
= TREE_TYPE (atype
);
4148 verify_ctor_sanity (ctx
, atype
);
4149 vec
<constructor_elt
, va_gc
> **p
= &CONSTRUCTOR_ELTS (ctx
->ctor
);
4150 bool pre_init
= false;
4151 unsigned HOST_WIDE_INT i
;
4152 tsubst_flags_t complain
= ctx
->quiet
? tf_none
: tf_warning_or_error
;
4154 if (init
&& TREE_CODE (init
) == CONSTRUCTOR
)
4155 return cxx_eval_bare_aggregate (ctx
, init
, lval
,
4156 non_constant_p
, overflow_p
);
4158 /* For the default constructor, build up a call to the default
4159 constructor of the element type. We only need to handle class types
4160 here, as for a constructor to be constexpr, all members must be
4161 initialized, which for a defaulted default constructor means they must
4162 be of a class type with a constexpr default constructor. */
4163 if (TREE_CODE (elttype
) == ARRAY_TYPE
)
4164 /* We only do this at the lowest level. */;
4165 else if (value_init
)
4167 init
= build_value_init (elttype
, complain
);
4172 releasing_vec argvec
;
4173 init
= build_special_member_call (NULL_TREE
, complete_ctor_identifier
,
4174 &argvec
, elttype
, LOOKUP_NORMAL
,
4176 init
= build_aggr_init_expr (elttype
, init
);
4180 bool zeroed_out
= false;
4181 if (!CONSTRUCTOR_NO_CLEARING (ctx
->ctor
))
4183 /* We're initializing an array object that had been zero-initialized
4184 earlier. Truncate ctx->ctor, and propagate its zeroed state by
4185 clearing CONSTRUCTOR_NO_CLEARING on each of the aggregate element
4186 initializers we append to it. */
4187 gcc_checking_assert (initializer_zerop (ctx
->ctor
));
4189 vec_safe_truncate (*p
, 0);
4192 tree nelts
= get_array_or_vector_nelts (ctx
, atype
, non_constant_p
,
4194 unsigned HOST_WIDE_INT max
= tree_to_uhwi (nelts
);
4195 for (i
= 0; i
< max
; ++i
)
4197 tree idx
= build_int_cst (size_type_node
, i
);
4200 constexpr_ctx new_ctx
;
4201 init_subob_ctx (ctx
, new_ctx
, idx
, pre_init
? init
: elttype
);
4202 if (new_ctx
.ctor
!= ctx
->ctor
)
4205 CONSTRUCTOR_NO_CLEARING (new_ctx
.ctor
) = false;
4206 CONSTRUCTOR_APPEND_ELT (*p
, idx
, new_ctx
.ctor
);
4208 if (TREE_CODE (elttype
) == ARRAY_TYPE
)
4210 /* A multidimensional array; recurse. */
4211 if (value_init
|| init
== NULL_TREE
)
4213 eltinit
= NULL_TREE
;
4217 eltinit
= cp_build_array_ref (input_location
, init
, idx
, complain
);
4218 eltinit
= cxx_eval_vec_init_1 (&new_ctx
, elttype
, eltinit
, value_init
,
4220 non_constant_p
, overflow_p
);
4224 /* Initializing an element using value or default initialization
4225 we just pre-built above. */
4226 if (init
== void_node
)
4227 /* Trivial default-init, don't do anything to the CONSTRUCTOR. */
4229 eltinit
= cxx_eval_constant_expression (&new_ctx
, init
, lval
,
4230 non_constant_p
, overflow_p
);
4235 /* Copying an element. */
4236 gcc_assert (same_type_ignoring_top_level_qualifiers_p
4237 (atype
, TREE_TYPE (init
)));
4238 eltinit
= cp_build_array_ref (input_location
, init
, idx
, complain
);
4239 if (!lvalue_p (init
))
4240 eltinit
= move (eltinit
);
4241 eltinit
= force_rvalue (eltinit
, complain
);
4242 eltinit
= cxx_eval_constant_expression (&new_ctx
, eltinit
, lval
,
4243 non_constant_p
, overflow_p
);
4245 if (*non_constant_p
&& !ctx
->quiet
)
4247 if (new_ctx
.ctor
!= ctx
->ctor
)
4249 /* We appended this element above; update the value. */
4250 gcc_assert ((*p
)->last().index
== idx
);
4251 (*p
)->last().value
= eltinit
;
4254 CONSTRUCTOR_APPEND_ELT (*p
, idx
, eltinit
);
4255 /* Reuse the result of cxx_eval_constant_expression call
4256 from the first iteration to all others if it is a constant
4257 initializer that doesn't require relocations. */
4260 && (eltinit
== NULL_TREE
4261 || (initializer_constant_valid_p (eltinit
, TREE_TYPE (eltinit
))
4262 == null_pointer_node
)))
4264 if (new_ctx
.ctor
!= ctx
->ctor
)
4265 eltinit
= new_ctx
.ctor
;
4266 tree range
= build2 (RANGE_EXPR
, size_type_node
,
4267 build_int_cst (size_type_node
, 1),
4268 build_int_cst (size_type_node
, max
- 1));
4269 CONSTRUCTOR_APPEND_ELT (*p
, range
, unshare_constructor (eltinit
));
4273 vec_safe_reserve (*p
, max
);
4276 if (!*non_constant_p
)
4279 CONSTRUCTOR_NO_CLEARING (init
) = false;
4285 cxx_eval_vec_init (const constexpr_ctx
*ctx
, tree t
,
4287 bool *non_constant_p
, bool *overflow_p
)
4289 tree atype
= TREE_TYPE (t
);
4290 tree init
= VEC_INIT_EXPR_INIT (t
);
4291 tree r
= cxx_eval_vec_init_1 (ctx
, atype
, init
,
4292 VEC_INIT_EXPR_VALUE_INIT (t
),
4293 lval
, non_constant_p
, overflow_p
);
4294 if (*non_constant_p
)
4300 /* Like same_type_ignoring_top_level_qualifiers_p, but also handle the case
4301 where the desired type is an array of unknown bounds because the variable
4302 has had its bounds deduced since the wrapping expression was created. */
4305 same_type_ignoring_tlq_and_bounds_p (tree type1
, tree type2
)
4307 while (TREE_CODE (type1
) == ARRAY_TYPE
4308 && TREE_CODE (type2
) == ARRAY_TYPE
4309 && (!TYPE_DOMAIN (type1
) || !TYPE_DOMAIN (type2
)))
4311 type1
= TREE_TYPE (type1
);
4312 type2
= TREE_TYPE (type2
);
4314 return same_type_ignoring_top_level_qualifiers_p (type1
, type2
);
4317 /* Helper function for cxx_fold_indirect_ref_1, called recursively. */
4320 cxx_fold_indirect_ref_1 (location_t loc
, tree type
, tree op
,
4321 unsigned HOST_WIDE_INT off
, bool *empty_base
)
4323 tree optype
= TREE_TYPE (op
);
4324 unsigned HOST_WIDE_INT const_nunits
;
4327 if (similar_type_p (optype
, type
))
4329 /* Also handle conversion to an empty base class, which
4330 is represented with a NOP_EXPR. */
4331 /* *(foo *)&complexfoo => __real__ complexfoo */
4332 else if (TREE_CODE (optype
) == COMPLEX_TYPE
4333 && similar_type_p (type
, TREE_TYPE (optype
)))
4334 return build1_loc (loc
, REALPART_EXPR
, type
, op
);
4336 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
4337 else if (TREE_CODE (optype
) == COMPLEX_TYPE
4338 && similar_type_p (type
, TREE_TYPE (optype
))
4339 && tree_to_uhwi (TYPE_SIZE_UNIT (type
)) == off
)
4340 return build1_loc (loc
, IMAGPART_EXPR
, type
, op
);
4341 if (is_empty_class (type
)
4342 && CLASS_TYPE_P (optype
)
4343 && DERIVED_FROM_P (type
, optype
))
4348 /* ((foo*)&vectorfoo)[x] => BIT_FIELD_REF<vectorfoo,...> */
4349 else if (VECTOR_TYPE_P (optype
)
4350 && similar_type_p (type
, TREE_TYPE (optype
))
4351 && TYPE_VECTOR_SUBPARTS (optype
).is_constant (&const_nunits
))
4353 unsigned HOST_WIDE_INT part_width
= tree_to_uhwi (TYPE_SIZE_UNIT (type
));
4354 unsigned HOST_WIDE_INT max_offset
= part_width
* const_nunits
;
4355 if (off
< max_offset
&& off
% part_width
== 0)
4357 tree index
= bitsize_int (off
* BITS_PER_UNIT
);
4358 return build3_loc (loc
, BIT_FIELD_REF
, type
, op
,
4359 TYPE_SIZE (type
), index
);
4362 /* ((foo *)&fooarray)[x] => fooarray[x] */
4363 else if (TREE_CODE (optype
) == ARRAY_TYPE
4364 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (optype
)))
4365 && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (optype
))))
4367 tree type_domain
= TYPE_DOMAIN (optype
);
4368 tree min_val
= size_zero_node
;
4369 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
4370 min_val
= TYPE_MIN_VALUE (type_domain
);
4371 unsigned HOST_WIDE_INT el_sz
4372 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (optype
)));
4373 unsigned HOST_WIDE_INT idx
= off
/ el_sz
;
4374 unsigned HOST_WIDE_INT rem
= off
% el_sz
;
4375 if (tree_fits_uhwi_p (min_val
))
4377 tree index
= size_int (idx
+ tree_to_uhwi (min_val
));
4378 op
= build4_loc (loc
, ARRAY_REF
, TREE_TYPE (optype
), op
, index
,
4379 NULL_TREE
, NULL_TREE
);
4380 return cxx_fold_indirect_ref_1 (loc
, type
, op
, rem
,
4384 /* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */
4385 else if (TREE_CODE (optype
) == RECORD_TYPE
)
4387 for (tree field
= TYPE_FIELDS (optype
);
4388 field
; field
= DECL_CHAIN (field
))
4389 if (TREE_CODE (field
) == FIELD_DECL
4390 && TREE_TYPE (field
) != error_mark_node
4391 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (field
))))
4393 tree pos
= byte_position (field
);
4394 if (!tree_fits_uhwi_p (pos
))
4396 unsigned HOST_WIDE_INT upos
= tree_to_uhwi (pos
);
4398 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field
)));
4399 if (upos
<= off
&& off
< upos
+ el_sz
)
4401 tree cop
= build3 (COMPONENT_REF
, TREE_TYPE (field
),
4402 op
, field
, NULL_TREE
);
4403 if (tree ret
= cxx_fold_indirect_ref_1 (loc
, type
, cop
,
4414 /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
4415 match. We want to be less strict for simple *& folding; if we have a
4416 non-const temporary that we access through a const pointer, that should
4417 work. We handle this here rather than change fold_indirect_ref_1
4418 because we're dealing with things like ADDR_EXPR of INTEGER_CST which
4419 don't really make sense outside of constant expression evaluation. Also
4420 we want to allow folding to COMPONENT_REF, which could cause trouble
4421 with TBAA in fold_indirect_ref_1. */
4424 cxx_fold_indirect_ref (location_t loc
, tree type
, tree op0
, bool *empty_base
)
4428 poly_uint64 const_op01
;
4430 /* STRIP_NOPS, but stop if REINTERPRET_CAST_P. */
4431 while (CONVERT_EXPR_P (sub
) || TREE_CODE (sub
) == NON_LVALUE_EXPR
4432 || TREE_CODE (sub
) == VIEW_CONVERT_EXPR
)
4434 if (TREE_CODE (sub
) == NOP_EXPR
4435 && REINTERPRET_CAST_P (sub
))
4437 sub
= TREE_OPERAND (sub
, 0);
4440 subtype
= TREE_TYPE (sub
);
4441 if (!INDIRECT_TYPE_P (subtype
))
4444 if (TREE_CODE (sub
) == ADDR_EXPR
)
4446 tree op
= TREE_OPERAND (sub
, 0);
4447 tree optype
= TREE_TYPE (op
);
4449 /* *&CONST_DECL -> to the value of the const decl. */
4450 if (TREE_CODE (op
) == CONST_DECL
)
4451 return DECL_INITIAL (op
);
4452 /* *&p => p; make sure to handle *&"str"[cst] here. */
4453 if (similar_type_p (optype
, type
))
4455 tree fop
= fold_read_from_constant_string (op
);
4462 return cxx_fold_indirect_ref_1 (loc
, type
, op
, 0, empty_base
);
4464 else if (TREE_CODE (sub
) == POINTER_PLUS_EXPR
4465 && tree_fits_uhwi_p (TREE_OPERAND (sub
, 1)))
4467 tree op00
= TREE_OPERAND (sub
, 0);
4468 tree op01
= TREE_OPERAND (sub
, 1);
4471 if (TREE_CODE (op00
) == ADDR_EXPR
)
4472 return cxx_fold_indirect_ref_1 (loc
, type
, TREE_OPERAND (op00
, 0),
4473 tree_to_uhwi (op01
), empty_base
);
4475 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
4476 else if (TREE_CODE (TREE_TYPE (subtype
)) == ARRAY_TYPE
4477 && similar_type_p (type
, TREE_TYPE (TREE_TYPE (subtype
))))
4480 tree min_val
= size_zero_node
;
4482 = cxx_fold_indirect_ref (loc
, TREE_TYPE (subtype
), sub
, NULL
);
4486 sub
= build1_loc (loc
, INDIRECT_REF
, TREE_TYPE (subtype
), sub
);
4487 type_domain
= TYPE_DOMAIN (TREE_TYPE (sub
));
4488 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
4489 min_val
= TYPE_MIN_VALUE (type_domain
);
4490 return build4_loc (loc
, ARRAY_REF
, type
, sub
, min_val
, NULL_TREE
,
4498 cxx_eval_indirect_ref (const constexpr_ctx
*ctx
, tree t
,
4500 bool *non_constant_p
, bool *overflow_p
)
4502 tree orig_op0
= TREE_OPERAND (t
, 0);
4503 bool empty_base
= false;
4505 /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second
4506 operand is an integer-zero. Otherwise reject the MEM_REF for now. */
4508 if (TREE_CODE (t
) == MEM_REF
4509 && (!TREE_OPERAND (t
, 1) || !integer_zerop (TREE_OPERAND (t
, 1))))
4511 gcc_assert (ctx
->quiet
);
4512 *non_constant_p
= true;
4516 /* First try to simplify it directly. */
4517 tree r
= cxx_fold_indirect_ref (EXPR_LOCATION (t
), TREE_TYPE (t
), orig_op0
,
4521 /* If that didn't work, evaluate the operand first. */
4522 tree op0
= cxx_eval_constant_expression (ctx
, orig_op0
,
4523 /*lval*/false, non_constant_p
,
4525 /* Don't VERIFY_CONSTANT here. */
4526 if (*non_constant_p
)
4529 if (!lval
&& integer_zerop (op0
))
4532 error ("dereferencing a null pointer");
4533 *non_constant_p
= true;
4537 r
= cxx_fold_indirect_ref (EXPR_LOCATION (t
), TREE_TYPE (t
), op0
,
4541 /* We couldn't fold to a constant value. Make sure it's not
4542 something we should have been able to fold. */
4545 if (TREE_CODE (sub
) == ADDR_EXPR
)
4547 gcc_assert (!similar_type_p
4548 (TREE_TYPE (TREE_TYPE (sub
)), TREE_TYPE (t
)));
4549 /* DR 1188 says we don't have to deal with this. */
4551 error_at (cp_expr_loc_or_input_loc (t
),
4552 "accessing value of %qE through a %qT glvalue in a "
4553 "constant expression", build_fold_indirect_ref (sub
),
4555 *non_constant_p
= true;
4559 if (lval
&& op0
!= orig_op0
)
4560 return build1 (INDIRECT_REF
, TREE_TYPE (t
), op0
);
4562 VERIFY_CONSTANT (t
);
4567 r
= cxx_eval_constant_expression (ctx
, r
,
4568 lval
, non_constant_p
, overflow_p
);
4569 if (*non_constant_p
)
4572 /* If we're pulling out the value of an empty base, just return an empty
4574 if (empty_base
&& !lval
)
4576 r
= build_constructor (TREE_TYPE (t
), NULL
);
4577 TREE_CONSTANT (r
) = true;
4583 /* Complain about R, a VAR_DECL, not being usable in a constant expression.
4584 Shared between potential_constant_expression and
4585 cxx_eval_constant_expression. */
4588 non_const_var_error (location_t loc
, tree r
)
4590 auto_diagnostic_group d
;
4591 tree type
= TREE_TYPE (r
);
4592 if (DECL_NAME (r
) == heap_uninit_identifier
4593 || DECL_NAME (r
) == heap_identifier
)
4595 error_at (loc
, "the content of uninitialized storage is not usable "
4596 "in a constant expression");
4597 inform (DECL_SOURCE_LOCATION (r
), "allocated here");
4600 if (DECL_NAME (r
) == heap_deleted_identifier
)
4602 error_at (loc
, "use of allocated storage after deallocation in a "
4603 "constant expression");
4604 inform (DECL_SOURCE_LOCATION (r
), "allocated here");
4607 error_at (loc
, "the value of %qD is not usable in a constant "
4609 /* Avoid error cascade. */
4610 if (DECL_INITIAL (r
) == error_mark_node
)
4612 if (DECL_DECLARED_CONSTEXPR_P (r
))
4613 inform (DECL_SOURCE_LOCATION (r
),
4614 "%qD used in its own initializer", r
);
4615 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
4617 if (!CP_TYPE_CONST_P (type
))
4618 inform (DECL_SOURCE_LOCATION (r
),
4619 "%q#D is not const", r
);
4620 else if (CP_TYPE_VOLATILE_P (type
))
4621 inform (DECL_SOURCE_LOCATION (r
),
4622 "%q#D is volatile", r
);
4623 else if (!DECL_INITIAL (r
)
4624 || !TREE_CONSTANT (DECL_INITIAL (r
))
4625 || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
))
4626 inform (DECL_SOURCE_LOCATION (r
),
4627 "%qD was not initialized with a constant "
4632 else if (TYPE_REF_P (type
))
4633 inform (DECL_SOURCE_LOCATION (r
),
4634 "%qD was not initialized with a constant "
4638 if (cxx_dialect
>= cxx11
&& !DECL_DECLARED_CONSTEXPR_P (r
))
4639 inform (DECL_SOURCE_LOCATION (r
),
4640 "%qD was not declared %<constexpr%>", r
);
4642 inform (DECL_SOURCE_LOCATION (r
),
4643 "%qD does not have integral or enumeration type",
4648 /* Subroutine of cxx_eval_constant_expression.
4649 Like cxx_eval_unary_expression, except for trinary expressions. */
4652 cxx_eval_trinary_expression (const constexpr_ctx
*ctx
, tree t
,
4654 bool *non_constant_p
, bool *overflow_p
)
4660 for (i
= 0; i
< 3; i
++)
4662 args
[i
] = cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, i
),
4664 non_constant_p
, overflow_p
);
4665 VERIFY_CONSTANT (args
[i
]);
4668 val
= fold_ternary_loc (EXPR_LOCATION (t
), TREE_CODE (t
), TREE_TYPE (t
),
4669 args
[0], args
[1], args
[2]);
4670 if (val
== NULL_TREE
)
4672 VERIFY_CONSTANT (val
);
4676 /* True if T was declared in a function declared to be constexpr, and
4677 therefore potentially constant in C++14. */
4680 var_in_constexpr_fn (tree t
)
4682 tree ctx
= DECL_CONTEXT (t
);
4683 return (ctx
&& TREE_CODE (ctx
) == FUNCTION_DECL
4684 && DECL_DECLARED_CONSTEXPR_P (ctx
));
4687 /* True if T was declared in a function that might be constexpr: either a
4688 function that was declared constexpr, or a C++17 lambda op(). */
4691 var_in_maybe_constexpr_fn (tree t
)
4693 if (cxx_dialect
>= cxx17
4694 && DECL_FUNCTION_SCOPE_P (t
)
4695 && LAMBDA_FUNCTION_P (DECL_CONTEXT (t
)))
4697 return var_in_constexpr_fn (t
);
4700 /* We're assigning INIT to TARGET. In do_build_copy_constructor and
4701 build_over_call we implement trivial copy of a class with tail padding using
4702 assignment of character arrays, which is valid in normal code, but not in
4703 constexpr evaluation. We don't need to worry about clobbering tail padding
4704 in constexpr evaluation, so strip the type punning. */
4707 maybe_simplify_trivial_copy (tree
&target
, tree
&init
)
4709 if (TREE_CODE (target
) == MEM_REF
4710 && TREE_CODE (init
) == MEM_REF
4711 && TREE_TYPE (target
) == TREE_TYPE (init
)
4712 && TREE_CODE (TREE_TYPE (target
)) == ARRAY_TYPE
4713 && TREE_TYPE (TREE_TYPE (target
)) == unsigned_char_type_node
)
4715 target
= build_fold_indirect_ref (TREE_OPERAND (target
, 0));
4716 init
= build_fold_indirect_ref (TREE_OPERAND (init
, 0));
4720 /* Returns true if REF, which is a COMPONENT_REF, has any fields
4721 of constant type. This does not check for 'mutable', so the
4722 caller is expected to be mindful of that. */
4725 cref_has_const_field (tree ref
)
4727 while (TREE_CODE (ref
) == COMPONENT_REF
)
4729 if (CP_TYPE_CONST_P (TREE_TYPE (TREE_OPERAND (ref
, 1))))
4731 ref
= TREE_OPERAND (ref
, 0);
4736 /* Return true if we are modifying something that is const during constant
4737 expression evaluation. CODE is the code of the statement, OBJ is the
4738 object in question, MUTABLE_P is true if one of the subobjects were
4739 declared mutable. */
4742 modifying_const_object_p (tree_code code
, tree obj
, bool mutable_p
)
4744 /* If this is initialization, there's no problem. */
4745 if (code
!= MODIFY_EXPR
)
4748 /* [basic.type.qualifier] "A const object is an object of type
4749 const T or a non-mutable subobject of a const object." */
4753 if (TREE_READONLY (obj
))
4756 if (CP_TYPE_CONST_P (TREE_TYPE (obj
)))
4758 /* Although a COMPONENT_REF may have a const type, we should
4759 only consider it modifying a const object when any of the
4760 field components is const. This can happen when using
4761 constructs such as const_cast<const T &>(m), making something
4762 const even though it wasn't declared const. */
4763 if (TREE_CODE (obj
) == COMPONENT_REF
)
4764 return cref_has_const_field (obj
);
4772 /* Evaluate an INIT_EXPR or MODIFY_EXPR. */
4775 cxx_eval_store_expression (const constexpr_ctx
*ctx
, tree t
,
4777 bool *non_constant_p
, bool *overflow_p
)
4779 constexpr_ctx new_ctx
= *ctx
;
4781 tree init
= TREE_OPERAND (t
, 1);
4782 if (TREE_CLOBBER_P (init
))
4783 /* Just ignore clobbers. */
4786 /* First we figure out where we're storing to. */
4787 tree target
= TREE_OPERAND (t
, 0);
4789 maybe_simplify_trivial_copy (target
, init
);
4791 tree type
= TREE_TYPE (target
);
4792 bool preeval
= SCALAR_TYPE_P (type
) || TREE_CODE (t
) == MODIFY_EXPR
;
4795 /* Evaluate the value to be stored without knowing what object it will be
4796 stored in, so that any side-effects happen first. */
4797 if (!SCALAR_TYPE_P (type
))
4798 new_ctx
.ctor
= new_ctx
.object
= NULL_TREE
;
4799 init
= cxx_eval_constant_expression (&new_ctx
, init
, false,
4800 non_constant_p
, overflow_p
);
4801 if (*non_constant_p
)
4805 bool evaluated
= false;
4808 /* If we want to return a reference to the target, we need to evaluate it
4809 as a whole; otherwise, only evaluate the innermost piece to avoid
4810 building up unnecessary *_REFs. */
4811 target
= cxx_eval_constant_expression (ctx
, target
, true,
4812 non_constant_p
, overflow_p
);
4814 if (*non_constant_p
)
4818 /* Find the underlying variable. */
4820 tree object
= NULL_TREE
;
4821 /* If we're modifying a const object, save it. */
4822 tree const_object_being_modified
= NULL_TREE
;
4823 bool mutable_p
= false;
4824 for (tree probe
= target
; object
== NULL_TREE
; )
4826 switch (TREE_CODE (probe
))
4832 tree ob
= TREE_OPERAND (probe
, 0);
4833 tree elt
= TREE_OPERAND (probe
, 1);
4834 if (TREE_CODE (elt
) == FIELD_DECL
&& DECL_MUTABLE_P (elt
))
4836 if (TREE_CODE (probe
) == ARRAY_REF
)
4838 elt
= eval_and_check_array_index (ctx
, probe
, false,
4839 non_constant_p
, overflow_p
);
4840 if (*non_constant_p
)
4843 /* We don't check modifying_const_object_p for ARRAY_REFs. Given
4844 "int a[10]", an ARRAY_REF "a[2]" can be "const int", even though
4845 the array isn't const. Instead, check "a" in the next iteration;
4846 that will detect modifying "const int a[10]". */
4848 && modifying_const_object_p (TREE_CODE (t
), probe
,
4850 && const_object_being_modified
== NULL_TREE
)
4851 const_object_being_modified
= probe
;
4852 vec_safe_push (refs
, elt
);
4853 vec_safe_push (refs
, TREE_TYPE (probe
));
4863 probe
= cxx_eval_constant_expression (ctx
, probe
, true,
4864 non_constant_p
, overflow_p
);
4866 if (*non_constant_p
)
4873 if (modifying_const_object_p (TREE_CODE (t
), object
, mutable_p
)
4874 && const_object_being_modified
== NULL_TREE
)
4875 const_object_being_modified
= object
;
4877 /* And then find/build up our initializer for the path to the subobject
4878 we're initializing. */
4880 if (DECL_P (object
))
4881 valp
= ctx
->global
->values
.get (object
);
4886 /* A constant-expression cannot modify objects from outside the
4887 constant-expression. */
4889 error ("modification of %qE is not a constant expression", object
);
4890 *non_constant_p
= true;
4893 type
= TREE_TYPE (object
);
4894 bool no_zero_init
= true;
4896 releasing_vec ctors
, indexes
;
4897 auto_vec
<int> index_pos_hints
;
4898 bool activated_union_member_p
= false;
4899 while (!refs
->is_empty ())
4901 if (*valp
== NULL_TREE
)
4903 *valp
= build_constructor (type
, NULL
);
4904 CONSTRUCTOR_NO_CLEARING (*valp
) = no_zero_init
;
4906 else if (TREE_CODE (*valp
) == STRING_CST
)
4908 /* An array was initialized with a string constant, and now
4909 we're writing into one of its elements. Explode the
4910 single initialization into a set of element
4912 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
4914 tree string
= *valp
;
4915 tree elt_type
= TREE_TYPE (type
);
4916 unsigned chars_per_elt
= (TYPE_PRECISION (elt_type
)
4917 / TYPE_PRECISION (char_type_node
));
4918 unsigned num_elts
= TREE_STRING_LENGTH (string
) / chars_per_elt
;
4919 tree ary_ctor
= build_constructor (type
, NULL
);
4921 vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor
), num_elts
);
4922 for (unsigned ix
= 0; ix
!= num_elts
; ix
++)
4924 constructor_elt elt
=
4926 build_int_cst (size_type_node
, ix
),
4927 extract_string_elt (string
, chars_per_elt
, ix
)
4929 CONSTRUCTOR_ELTS (ary_ctor
)->quick_push (elt
);
4935 /* If the value of object is already zero-initialized, any new ctors for
4936 subobjects will also be zero-initialized. */
4937 no_zero_init
= CONSTRUCTOR_NO_CLEARING (*valp
);
4939 enum tree_code code
= TREE_CODE (type
);
4941 tree index
= refs
->pop();
4943 if (code
== UNION_TYPE
&& CONSTRUCTOR_NELTS (*valp
)
4944 && CONSTRUCTOR_ELT (*valp
, 0)->index
!= index
)
4946 if (cxx_dialect
< cxx20
)
4949 error_at (cp_expr_loc_or_input_loc (t
),
4950 "change of the active member of a union "
4952 CONSTRUCTOR_ELT (*valp
, 0)->index
,
4954 *non_constant_p
= true;
4956 else if (TREE_CODE (t
) == MODIFY_EXPR
4957 && CONSTRUCTOR_NO_CLEARING (*valp
))
4959 /* Diagnose changing the active union member while the union
4960 is in the process of being initialized. */
4962 error_at (cp_expr_loc_or_input_loc (t
),
4963 "change of the active member of a union "
4964 "from %qD to %qD during initialization",
4965 CONSTRUCTOR_ELT (*valp
, 0)->index
,
4967 *non_constant_p
= true;
4969 no_zero_init
= true;
4972 vec_safe_push (ctors
, *valp
);
4973 vec_safe_push (indexes
, index
);
4975 constructor_elt
*cep
4976 = get_or_insert_ctor_field (*valp
, index
);
4977 index_pos_hints
.safe_push (cep
- CONSTRUCTOR_ELTS (*valp
)->begin());
4979 if (code
== UNION_TYPE
)
4980 activated_union_member_p
= true;
4985 /* Detect modifying a constant object in constexpr evaluation.
4986 We have found a const object that is being modified. Figure out
4987 if we need to issue an error. Consider
4991 constexpr A() : n(1) { n = 2; } // #1
4995 constexpr B() { a.n = 3; } // #2
4999 #1 is OK, since we're modifying an object under construction, but
5000 #2 is wrong, since "a" is const and has been fully constructed.
5001 To track it, we use the TREE_READONLY bit in the object's CONSTRUCTOR
5002 which means that the object is read-only. For the example above, the
5003 *ctors stack at the point of #2 will look like:
5005 ctors[0] = {.a={.n=2}} TREE_READONLY = 0
5006 ctors[1] = {.n=2} TREE_READONLY = 1
5008 and we're modifying "b.a", so we search the stack and see if the
5009 constructor for "b.a" has already run. */
5010 if (const_object_being_modified
)
5014 = strip_array_types (TREE_TYPE (const_object_being_modified
));
5015 if (!CLASS_TYPE_P (const_objtype
))
5019 /* [class.ctor]p5 "A constructor can be invoked for a const,
5020 volatile, or const volatile object. const and volatile
5021 semantics are not applied on an object under construction.
5022 They come into effect when the constructor for the most
5023 derived object ends." */
5026 FOR_EACH_VEC_ELT (*ctors
, i
, elt
)
5027 if (same_type_ignoring_top_level_qualifiers_p
5028 (TREE_TYPE (const_object_being_modified
), TREE_TYPE (elt
)))
5030 fail
= TREE_READONLY (elt
);
5037 modifying_const_object_error (t
, const_object_being_modified
);
5038 *non_constant_p
= true;
5045 /* Create a new CONSTRUCTOR in case evaluation of the initializer
5046 wants to modify it. */
5047 if (*valp
== NULL_TREE
)
5049 *valp
= build_constructor (type
, NULL
);
5050 CONSTRUCTOR_NO_CLEARING (*valp
) = no_zero_init
;
5052 new_ctx
.ctor
= *valp
;
5053 new_ctx
.object
= target
;
5054 /* Avoid temporary materialization when initializing from a TARGET_EXPR.
5055 We don't need to mess with AGGR_EXPR_SLOT/VEC_INIT_EXPR_SLOT because
5056 expansion of those trees uses ctx instead. */
5057 if (TREE_CODE (init
) == TARGET_EXPR
)
5058 if (tree tinit
= TARGET_EXPR_INITIAL (init
))
5060 init
= cxx_eval_constant_expression (&new_ctx
, init
, false,
5061 non_constant_p
, overflow_p
);
5062 /* The hash table might have moved since the get earlier, and the
5063 initializer might have mutated the underlying CONSTRUCTORs, so we must
5065 valp
= ctx
->global
->values
.get (object
);
5066 for (unsigned i
= 0; i
< vec_safe_length (indexes
); i
++)
5068 constructor_elt
*cep
5069 = get_or_insert_ctor_field (*valp
, indexes
[i
], index_pos_hints
[i
]);
5074 /* Don't share a CONSTRUCTOR that might be changed later. */
5075 init
= unshare_constructor (init
);
5077 if (*valp
&& TREE_CODE (*valp
) == CONSTRUCTOR
5078 && TREE_CODE (init
) == CONSTRUCTOR
)
5080 /* An outer ctx->ctor might be pointing to *valp, so replace
5082 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init
),
5085 /* For initialization of an empty base, the original target will be
5086 *(base*)this, evaluation of which resolves to the object
5087 argument, which has the derived type rather than the base type. In
5088 this situation, just evaluate the initializer and return, since
5089 there's no actual data to store. */
5090 gcc_assert (is_empty_class (TREE_TYPE (init
)) && !lval
);
5093 CONSTRUCTOR_ELTS (*valp
) = CONSTRUCTOR_ELTS (init
);
5094 TREE_CONSTANT (*valp
) = TREE_CONSTANT (init
);
5095 TREE_SIDE_EFFECTS (*valp
) = TREE_SIDE_EFFECTS (init
);
5096 CONSTRUCTOR_NO_CLEARING (*valp
)
5097 = CONSTRUCTOR_NO_CLEARING (init
);
5102 /* After initialization, 'const' semantics apply to the value of the
5103 object. Make a note of this fact by marking the CONSTRUCTOR
5105 if (TREE_CODE (t
) == INIT_EXPR
5106 && TREE_CODE (*valp
) == CONSTRUCTOR
5107 && TYPE_READONLY (type
))
5109 if (INDIRECT_REF_P (target
)
5110 && (is_this_parameter
5111 (tree_strip_nop_conversions (TREE_OPERAND (target
, 0)))))
5112 /* We've just initialized '*this' (perhaps via the target
5113 constructor of a delegating constructor). Leave it up to the
5114 caller that set 'this' to set TREE_READONLY appropriately. */
5115 gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
5116 (TREE_TYPE (target
), type
));
5118 TREE_READONLY (*valp
) = true;
5121 /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing
5122 CONSTRUCTORs, if any. */
5125 bool c
= TREE_CONSTANT (init
);
5126 bool s
= TREE_SIDE_EFFECTS (init
);
5127 if (!c
|| s
|| activated_union_member_p
)
5128 FOR_EACH_VEC_ELT (*ctors
, i
, elt
)
5131 TREE_CONSTANT (elt
) = false;
5133 TREE_SIDE_EFFECTS (elt
) = true;
5134 /* Clear CONSTRUCTOR_NO_CLEARING since we've activated a member of
5136 if (TREE_CODE (TREE_TYPE (elt
)) == UNION_TYPE
)
5137 CONSTRUCTOR_NO_CLEARING (elt
) = false;
5140 if (*non_constant_p
)
5148 /* Evaluate a ++ or -- expression. */
5151 cxx_eval_increment_expression (const constexpr_ctx
*ctx
, tree t
,
5153 bool *non_constant_p
, bool *overflow_p
)
5155 enum tree_code code
= TREE_CODE (t
);
5156 tree type
= TREE_TYPE (t
);
5157 tree op
= TREE_OPERAND (t
, 0);
5158 tree offset
= TREE_OPERAND (t
, 1);
5159 gcc_assert (TREE_CONSTANT (offset
));
5161 /* OFFSET is constant, but perhaps not constant enough. We need to
5162 e.g. bash FLOAT_EXPRs to REAL_CSTs. */
5163 offset
= fold_simple (offset
);
5165 /* The operand as an lvalue. */
5166 op
= cxx_eval_constant_expression (ctx
, op
, true,
5167 non_constant_p
, overflow_p
);
5169 /* The operand as an rvalue. */
5171 = cxx_eval_constant_expression (ctx
, op
, false,
5172 non_constant_p
, overflow_p
);
5173 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
5174 a local array in a constexpr function. */
5175 bool ptr
= INDIRECT_TYPE_P (TREE_TYPE (val
));
5177 VERIFY_CONSTANT (val
);
5179 /* The modified value. */
5180 bool inc
= (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
);
5182 if (INDIRECT_TYPE_P (type
))
5184 /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
5185 offset
= convert_to_ptrofftype (offset
);
5187 offset
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (offset
), offset
);
5188 mod
= fold_build2 (POINTER_PLUS_EXPR
, type
, val
, offset
);
5191 mod
= fold_build2 (inc
? PLUS_EXPR
: MINUS_EXPR
, type
, val
, offset
);
5193 VERIFY_CONSTANT (mod
);
5195 /* Storing the modified value. */
5196 tree store
= build2_loc (cp_expr_loc_or_loc (t
, input_location
),
5197 MODIFY_EXPR
, type
, op
, mod
);
5198 cxx_eval_constant_expression (ctx
, store
,
5199 true, non_constant_p
, overflow_p
);
5202 /* And the value of the expression. */
5203 if (code
== PREINCREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
5205 /* Prefix ops are lvalues. */
5209 /* But we optimize when the caller wants an rvalue. */
5213 /* Postfix ops are rvalues. */
5217 /* Predicates for the meaning of *jump_target. */
5220 returns (tree
*jump_target
)
5223 && (TREE_CODE (*jump_target
) == RETURN_EXPR
5224 || (TREE_CODE (*jump_target
) == LABEL_DECL
5225 && LABEL_DECL_CDTOR (*jump_target
)));
5229 breaks (tree
*jump_target
)
5232 && ((TREE_CODE (*jump_target
) == LABEL_DECL
5233 && LABEL_DECL_BREAK (*jump_target
))
5234 || TREE_CODE (*jump_target
) == BREAK_STMT
5235 || TREE_CODE (*jump_target
) == EXIT_EXPR
);
5239 continues (tree
*jump_target
)
5242 && ((TREE_CODE (*jump_target
) == LABEL_DECL
5243 && LABEL_DECL_CONTINUE (*jump_target
))
5244 || TREE_CODE (*jump_target
) == CONTINUE_STMT
);
5249 switches (tree
*jump_target
)
5252 && TREE_CODE (*jump_target
) == INTEGER_CST
;
5255 /* Subroutine of cxx_eval_statement_list. Determine whether the statement
5256 STMT matches *jump_target. If we're looking for a case label and we see
5257 the default label, note it in ctx->css_state. */
5260 label_matches (const constexpr_ctx
*ctx
, tree
*jump_target
, tree stmt
)
5262 switch (TREE_CODE (*jump_target
))
5265 if (TREE_CODE (stmt
) == LABEL_EXPR
5266 && LABEL_EXPR_LABEL (stmt
) == *jump_target
)
5271 if (TREE_CODE (stmt
) == CASE_LABEL_EXPR
)
5273 gcc_assert (ctx
->css_state
!= NULL
);
5274 if (!CASE_LOW (stmt
))
5276 /* default: should appear just once in a SWITCH_EXPR
5277 body (excluding nested SWITCH_EXPR). */
5278 gcc_assert (*ctx
->css_state
!= css_default_seen
);
5279 /* When evaluating SWITCH_EXPR body for the second time,
5280 return true for the default: label. */
5281 if (*ctx
->css_state
== css_default_processing
)
5283 *ctx
->css_state
= css_default_seen
;
5285 else if (CASE_HIGH (stmt
))
5287 if (tree_int_cst_le (CASE_LOW (stmt
), *jump_target
)
5288 && tree_int_cst_le (*jump_target
, CASE_HIGH (stmt
)))
5291 else if (tree_int_cst_equal (*jump_target
, CASE_LOW (stmt
)))
5298 /* These two are handled directly in cxx_eval_loop_expr by testing
5299 breaks (jump_target) or continues (jump_target). */
5308 /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
5309 semantics, for switch, break, continue, and return. */
5312 cxx_eval_statement_list (const constexpr_ctx
*ctx
, tree t
,
5313 bool *non_constant_p
, bool *overflow_p
,
5316 tree_stmt_iterator i
;
5318 /* In a statement-expression we want to return the last value.
5319 For empty statement expression return void_node. */
5323 local_target
= NULL_TREE
;
5324 jump_target
= &local_target
;
5326 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
5328 tree stmt
= tsi_stmt (i
);
5329 /* We've found a continue, so skip everything until we reach
5330 the label its jumping to. */
5331 if (continues (jump_target
))
5333 if (label_matches (ctx
, jump_target
, stmt
))
5335 *jump_target
= NULL_TREE
;
5339 if (TREE_CODE (stmt
) == DEBUG_BEGIN_STMT
)
5341 r
= cxx_eval_constant_expression (ctx
, stmt
, false,
5342 non_constant_p
, overflow_p
,
5344 if (*non_constant_p
)
5346 if (returns (jump_target
) || breaks (jump_target
))
5349 if (*jump_target
&& jump_target
== &local_target
)
5351 /* We aren't communicating the jump to our caller, so give up. We don't
5352 need to support evaluation of jumps out of statement-exprs. */
5354 error_at (cp_expr_loc_or_input_loc (r
),
5355 "statement is not a constant expression");
5356 *non_constant_p
= true;
5361 /* Evaluate a LOOP_EXPR for side-effects. Handles break and return
5362 semantics; continue semantics are covered by cxx_eval_statement_list. */
5365 cxx_eval_loop_expr (const constexpr_ctx
*ctx
, tree t
,
5366 bool *non_constant_p
, bool *overflow_p
,
5369 constexpr_ctx new_ctx
= *ctx
;
5373 local_target
= NULL_TREE
;
5374 jump_target
= &local_target
;
5377 tree body
, cond
= NULL_TREE
, expr
= NULL_TREE
;
5379 switch (TREE_CODE (t
))
5382 body
= LOOP_EXPR_BODY (t
);
5389 body
= WHILE_BODY (t
);
5390 cond
= WHILE_COND (t
);
5394 if (FOR_INIT_STMT (t
))
5395 cxx_eval_constant_expression (ctx
, FOR_INIT_STMT (t
), /*lval*/false,
5396 non_constant_p
, overflow_p
, jump_target
);
5397 if (*non_constant_p
)
5399 body
= FOR_BODY (t
);
5400 cond
= FOR_COND (t
);
5401 expr
= FOR_EXPR (t
);
5407 auto_vec
<tree
, 10> save_exprs
;
5408 new_ctx
.save_exprs
= &save_exprs
;
5414 cxx_eval_constant_expression (&new_ctx
, body
, /*lval*/false,
5415 non_constant_p
, overflow_p
,
5417 if (breaks (jump_target
))
5419 *jump_target
= NULL_TREE
;
5423 if (TREE_CODE (t
) != LOOP_EXPR
&& continues (jump_target
))
5424 *jump_target
= NULL_TREE
;
5427 cxx_eval_constant_expression (&new_ctx
, expr
, /*lval*/false,
5428 non_constant_p
, overflow_p
,
5435 = cxx_eval_constant_expression (&new_ctx
, cond
, /*lval*/false,
5436 non_constant_p
, overflow_p
,
5440 if (verify_constant (res
, ctx
->quiet
, non_constant_p
,
5443 if (integer_zerop (res
))
5447 gcc_assert (*jump_target
);
5450 /* Forget saved values of SAVE_EXPRs and TARGET_EXPRs. */
5453 FOR_EACH_VEC_ELT (save_exprs
, i
, save_expr
)
5454 ctx
->global
->values
.remove (save_expr
);
5455 save_exprs
.truncate (0);
5457 if (++count
>= constexpr_loop_limit
)
5460 error_at (cp_expr_loc_or_input_loc (t
),
5461 "%<constexpr%> loop iteration count exceeds limit of %d "
5462 "(use %<-fconstexpr-loop-limit=%> to increase the limit)",
5463 constexpr_loop_limit
);
5464 *non_constant_p
= true;
5468 while (!returns (jump_target
)
5469 && !breaks (jump_target
)
5470 && !continues (jump_target
)
5471 && (!switches (jump_target
) || count
== 0)
5472 && !*non_constant_p
);
5474 /* Forget saved values of SAVE_EXPRs and TARGET_EXPRs. */
5477 FOR_EACH_VEC_ELT (save_exprs
, i
, save_expr
)
5478 ctx
->global
->values
.remove (save_expr
);
5483 /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
5487 cxx_eval_switch_expr (const constexpr_ctx
*ctx
, tree t
,
5488 bool *non_constant_p
, bool *overflow_p
,
5492 = TREE_CODE (t
) == SWITCH_STMT
? SWITCH_STMT_COND (t
) : SWITCH_COND (t
);
5493 cond
= cxx_eval_constant_expression (ctx
, cond
, false,
5494 non_constant_p
, overflow_p
);
5495 VERIFY_CONSTANT (cond
);
5496 *jump_target
= cond
;
5499 = TREE_CODE (t
) == SWITCH_STMT
? SWITCH_STMT_BODY (t
) : SWITCH_BODY (t
);
5500 constexpr_ctx new_ctx
= *ctx
;
5501 constexpr_switch_state css
= css_default_not_seen
;
5502 new_ctx
.css_state
= &css
;
5503 cxx_eval_constant_expression (&new_ctx
, body
, false,
5504 non_constant_p
, overflow_p
, jump_target
);
5505 if (switches (jump_target
) && css
== css_default_seen
)
5507 /* If the SWITCH_EXPR body has default: label, process it once again,
5508 this time instructing label_matches to return true for default:
5509 label on switches (jump_target). */
5510 css
= css_default_processing
;
5511 cxx_eval_constant_expression (&new_ctx
, body
, false,
5512 non_constant_p
, overflow_p
, jump_target
);
5514 if (breaks (jump_target
) || switches (jump_target
))
5515 *jump_target
= NULL_TREE
;
5519 /* Find the object of TYPE under initialization in CTX. */
5522 lookup_placeholder (const constexpr_ctx
*ctx
, bool lval
, tree type
)
5527 /* Prefer the outermost matching object, but don't cross
5528 CONSTRUCTOR_PLACEHOLDER_BOUNDARY constructors. */
5529 if (ctx
->ctor
&& !CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx
->ctor
))
5530 if (tree outer_ob
= lookup_placeholder (ctx
->parent
, lval
, type
))
5533 /* We could use ctx->object unconditionally, but using ctx->ctor when we
5534 can is a minor optimization. */
5535 if (!lval
&& ctx
->ctor
&& same_type_p (TREE_TYPE (ctx
->ctor
), type
))
5541 /* Since an object cannot have a field of its own type, we can search outward
5542 from ctx->object to find the unique containing object of TYPE. */
5543 tree ob
= ctx
->object
;
5546 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob
), type
))
5548 if (handled_component_p (ob
))
5549 ob
= TREE_OPERAND (ob
, 0);
5557 /* Complain about an attempt to evaluate inline assembly. */
5560 inline_asm_in_constexpr_error (location_t loc
)
5562 auto_diagnostic_group d
;
5563 error_at (loc
, "inline assembly is not a constant expression");
5564 inform (loc
, "only unevaluated inline assembly is allowed in a "
5565 "%<constexpr%> function in C++20");
5568 /* Attempt to reduce the expression T to a constant value.
5569 On failure, issue diagnostic and return error_mark_node. */
5570 /* FIXME unify with c_fully_fold */
5571 /* FIXME overflow_p is too global */
5574 cxx_eval_constant_expression (const constexpr_ctx
*ctx
, tree t
,
5576 bool *non_constant_p
, bool *overflow_p
,
5577 tree
*jump_target
/* = NULL */)
5579 if (jump_target
&& *jump_target
)
5581 /* If we are jumping, ignore all statements/expressions except those
5582 that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */
5583 switch (TREE_CODE (t
))
5586 case STATEMENT_LIST
:
5595 case CASE_LABEL_EXPR
:
5596 if (label_matches (ctx
, jump_target
, t
))
5598 *jump_target
= NULL_TREE
;
5604 if (error_operand_p (t
))
5606 *non_constant_p
= true;
5610 location_t loc
= cp_expr_loc_or_input_loc (t
);
5612 STRIP_ANY_LOCATION_WRAPPER (t
);
5614 if (CONSTANT_CLASS_P (t
))
5616 if (TREE_OVERFLOW (t
))
5619 permerror (input_location
, "overflow in constant expression");
5620 if (!flag_permissive
|| ctx
->quiet
)
5624 if (TREE_CODE (t
) == INTEGER_CST
5625 && TYPE_PTR_P (TREE_TYPE (t
))
5626 && !integer_zerop (t
))
5629 error ("value %qE of type %qT is not a constant expression",
5631 *non_constant_p
= true;
5637 /* Avoid excessively long constexpr evaluations. */
5638 if (++ctx
->global
->constexpr_ops_count
>= constexpr_ops_limit
)
5642 "%<constexpr%> evaluation operation count exceeds limit of "
5643 "%wd (use %<-fconstexpr-ops-limit=%> to increase the limit)",
5644 constexpr_ops_limit
);
5645 ctx
->global
->constexpr_ops_count
= INTTYPE_MINIMUM (HOST_WIDE_INT
);
5646 *non_constant_p
= true;
5650 constexpr_ctx new_ctx
;
5653 tree_code tcode
= TREE_CODE (t
);
5659 /* We ask for an rvalue for the RESULT_DECL when indirecting
5660 through an invisible reference, or in named return value
5662 if (tree
*p
= ctx
->global
->values
.get (t
))
5667 error ("%qE is not a constant expression", t
);
5668 *non_constant_p
= true;
5673 if (DECL_HAS_VALUE_EXPR_P (t
))
5675 if (is_normal_capture_proxy (t
)
5676 && current_function_decl
== DECL_CONTEXT (t
))
5678 /* Function parms aren't constexpr within the function
5679 definition, so don't try to look at the closure. But if the
5680 captured variable is constant, try to evaluate it directly. */
5681 r
= DECL_CAPTURED_VARIABLE (t
);
5682 tree type
= TREE_TYPE (t
);
5683 if (TYPE_REF_P (type
) != TYPE_REF_P (TREE_TYPE (r
)))
5685 /* Adjust r to match the reference-ness of t. */
5686 if (TYPE_REF_P (type
))
5687 r
= build_address (r
);
5689 r
= convert_from_reference (r
);
5693 r
= DECL_VALUE_EXPR (t
);
5694 return cxx_eval_constant_expression (ctx
, r
, lval
, non_constant_p
,
5699 /* We used to not check lval for CONST_DECL, but darwin.c uses
5700 CONST_DECL for aggregate constants. */
5703 else if (t
== ctx
->object
)
5706 if (tree
*p
= ctx
->global
->values
.get (t
))
5707 if (*p
!= NULL_TREE
)
5712 if (COMPLETE_TYPE_P (TREE_TYPE (t
))
5713 && is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
5715 /* If the class is empty, we aren't actually loading anything. */
5716 r
= build_constructor (TREE_TYPE (t
), NULL
);
5717 TREE_CONSTANT (r
) = true;
5719 else if (ctx
->strict
)
5720 r
= decl_really_constant_value (t
, /*unshare_p=*/false);
5722 r
= decl_constant_value (t
, /*unshare_p=*/false);
5723 if (TREE_CODE (r
) == TARGET_EXPR
5724 && TREE_CODE (TARGET_EXPR_INITIAL (r
)) == CONSTRUCTOR
)
5725 r
= TARGET_EXPR_INITIAL (r
);
5729 non_const_var_error (loc
, r
);
5730 *non_constant_p
= true;
5734 case DEBUG_BEGIN_STMT
:
5735 /* ??? It might be nice to retain this information somehow, so
5736 as to be able to step into a constexpr function call. */
5743 case CASE_LABEL_EXPR
:
5748 if (lval
&& !TYPE_REF_P (TREE_TYPE (t
)))
5750 else if (tree
*p
= ctx
->global
->values
.get (r
))
5753 /* Defer in case this is only used for its type. */;
5754 else if (COMPLETE_TYPE_P (TREE_TYPE (t
))
5755 && is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
5757 /* If the class is empty, we aren't actually loading anything. */
5758 r
= build_constructor (TREE_TYPE (t
), NULL
);
5759 TREE_CONSTANT (r
) = true;
5764 error ("%qE is not a constant expression", t
);
5765 *non_constant_p
= true;
5770 case AGGR_INIT_EXPR
:
5771 r
= cxx_eval_call_expression (ctx
, t
, lval
,
5772 non_constant_p
, overflow_p
);
5777 r
= DECL_EXPR_DECL (t
);
5778 if (TREE_CODE (r
) == USING_DECL
)
5783 if (AGGREGATE_TYPE_P (TREE_TYPE (r
))
5784 || VECTOR_TYPE_P (TREE_TYPE (r
)))
5788 new_ctx
.ctor
= build_constructor (TREE_TYPE (r
), NULL
);
5789 CONSTRUCTOR_NO_CLEARING (new_ctx
.ctor
) = true;
5790 ctx
->global
->values
.put (r
, new_ctx
.ctor
);
5794 if (tree init
= DECL_INITIAL (r
))
5796 init
= cxx_eval_constant_expression (ctx
, init
,
5798 non_constant_p
, overflow_p
);
5799 /* Don't share a CONSTRUCTOR that might be changed. */
5800 init
= unshare_constructor (init
);
5801 /* Remember that a constant object's constructor has already
5803 if (CLASS_TYPE_P (TREE_TYPE (r
))
5804 && CP_TYPE_CONST_P (TREE_TYPE (r
)))
5805 TREE_READONLY (init
) = true;
5806 ctx
->global
->values
.put (r
, init
);
5808 else if (ctx
== &new_ctx
)
5809 /* We gave it a CONSTRUCTOR above. */;
5811 ctx
->global
->values
.put (r
, NULL_TREE
);
5817 tree type
= TREE_TYPE (t
);
5819 if (!literal_type_p (type
))
5823 auto_diagnostic_group d
;
5824 error ("temporary of non-literal type %qT in a "
5825 "constant expression", type
);
5826 explain_non_literal_class (type
);
5828 *non_constant_p
= true;
5831 gcc_checking_assert (!TARGET_EXPR_DIRECT_INIT_P (t
));
5832 /* Avoid evaluating a TARGET_EXPR more than once. */
5833 tree slot
= TARGET_EXPR_SLOT (t
);
5834 if (tree
*p
= ctx
->global
->values
.get (slot
))
5841 if ((AGGREGATE_TYPE_P (type
) || VECTOR_TYPE_P (type
)))
5843 /* We're being expanded without an explicit target, so start
5844 initializing a new object; expansion with an explicit target
5845 strips the TARGET_EXPR before we get here. */
5847 /* Link CTX to NEW_CTX so that lookup_placeholder can resolve
5848 any PLACEHOLDER_EXPR within the initializer that refers to the
5849 former object under construction. */
5850 new_ctx
.parent
= ctx
;
5851 new_ctx
.ctor
= build_constructor (type
, NULL
);
5852 CONSTRUCTOR_NO_CLEARING (new_ctx
.ctor
) = true;
5853 new_ctx
.object
= slot
;
5854 ctx
->global
->values
.put (new_ctx
.object
, new_ctx
.ctor
);
5857 /* Pass false for 'lval' because this indicates
5858 initialization of a temporary. */
5859 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
5861 non_constant_p
, overflow_p
);
5862 if (*non_constant_p
)
5864 /* Adjust the type of the result to the type of the temporary. */
5865 r
= adjust_temp_type (type
, r
);
5866 if (TARGET_EXPR_CLEANUP (t
) && !CLEANUP_EH_ONLY (t
))
5867 ctx
->global
->cleanups
->safe_push (TARGET_EXPR_CLEANUP (t
));
5868 r
= unshare_constructor (r
);
5869 ctx
->global
->values
.put (slot
, r
);
5870 if (ctx
->save_exprs
)
5871 ctx
->save_exprs
->safe_push (slot
);
5879 gcc_assert (jump_target
== NULL
|| *jump_target
== NULL_TREE
);
5880 r
= cxx_eval_store_expression (ctx
, t
, lval
,
5881 non_constant_p
, overflow_p
);
5885 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
5887 non_constant_p
, overflow_p
);
5891 if (TREE_OPERAND (t
, 0) != NULL_TREE
)
5892 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
5894 non_constant_p
, overflow_p
);
5902 /* Can happen with ({ return true; }) && false; passed to
5903 maybe_constant_value. There is nothing to jump over in this
5904 case, and the bug will be diagnosed later. */
5905 gcc_assert (ctx
->quiet
);
5906 *non_constant_p
= true;
5911 /* Avoid evaluating a SAVE_EXPR more than once. */
5912 if (tree
*p
= ctx
->global
->values
.get (t
))
5916 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), false,
5917 non_constant_p
, overflow_p
);
5918 if (*non_constant_p
)
5920 ctx
->global
->values
.put (t
, r
);
5921 if (ctx
->save_exprs
)
5922 ctx
->save_exprs
->safe_push (t
);
5926 case TRY_CATCH_EXPR
:
5927 if (TREE_OPERAND (t
, 0) == NULL_TREE
)
5933 case NON_LVALUE_EXPR
:
5935 case MUST_NOT_THROW_EXPR
:
5938 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
5940 non_constant_p
, overflow_p
,
5944 case CLEANUP_POINT_EXPR
:
5946 auto_vec
<tree
, 2> cleanups
;
5947 vec
<tree
> *prev_cleanups
= ctx
->global
->cleanups
;
5948 ctx
->global
->cleanups
= &cleanups
;
5949 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
5951 non_constant_p
, overflow_p
,
5953 ctx
->global
->cleanups
= prev_cleanups
;
5956 /* Evaluate the cleanups. */
5957 FOR_EACH_VEC_ELT_REVERSE (cleanups
, i
, cleanup
)
5958 cxx_eval_constant_expression (ctx
, cleanup
, false,
5959 non_constant_p
, overflow_p
,
5964 case TRY_FINALLY_EXPR
:
5965 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), lval
,
5966 non_constant_p
, overflow_p
,
5968 if (!*non_constant_p
)
5969 /* Also evaluate the cleanup. */
5970 cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1), true,
5971 non_constant_p
, overflow_p
,
5977 tree initial_jump_target
= jump_target
? *jump_target
: NULL_TREE
;
5978 r
= cxx_eval_constant_expression (ctx
, CLEANUP_BODY (t
), lval
,
5979 non_constant_p
, overflow_p
,
5981 if (!CLEANUP_EH_ONLY (t
) && !*non_constant_p
)
5983 iloc_sentinel
ils (loc
);
5984 /* Also evaluate the cleanup. If we weren't skipping at the
5985 start of the CLEANUP_BODY, change jump_target temporarily
5986 to &initial_jump_target, so that even a return or break or
5987 continue in the body doesn't skip the cleanup. */
5988 cxx_eval_constant_expression (ctx
, CLEANUP_EXPR (t
), true,
5989 non_constant_p
, overflow_p
,
5990 jump_target
? &initial_jump_target
5996 /* These differ from cxx_eval_unary_expression in that this doesn't
5997 check for a constant operand or result; an address can be
5998 constant without its operand being, and vice versa. */
6001 r
= cxx_eval_indirect_ref (ctx
, t
, lval
,
6002 non_constant_p
, overflow_p
);
6007 tree oldop
= TREE_OPERAND (t
, 0);
6008 tree op
= cxx_eval_constant_expression (ctx
, oldop
,
6010 non_constant_p
, overflow_p
);
6011 /* Don't VERIFY_CONSTANT here. */
6012 if (*non_constant_p
)
6014 gcc_checking_assert (TREE_CODE (op
) != CONSTRUCTOR
);
6015 /* This function does more aggressive folding than fold itself. */
6016 r
= build_fold_addr_expr_with_type (op
, TREE_TYPE (t
));
6017 if (TREE_CODE (r
) == ADDR_EXPR
&& TREE_OPERAND (r
, 0) == oldop
)
6029 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), lval
,
6030 non_constant_p
, overflow_p
);
6031 if (r
== error_mark_node
)
6033 else if (r
== TREE_OPERAND (t
, 0))
6036 r
= fold_build1 (TREE_CODE (t
), TREE_TYPE (t
), r
);
6041 case FIX_TRUNC_EXPR
:
6047 case TRUTH_NOT_EXPR
:
6048 case FIXED_CONVERT_EXPR
:
6049 r
= cxx_eval_unary_expression (ctx
, t
, lval
,
6050 non_constant_p
, overflow_p
);
6054 r
= fold_sizeof_expr (t
);
6055 /* In a template, fold_sizeof_expr may merely create a new SIZEOF_EXPR,
6056 which could lead to an infinite recursion. */
6057 if (TREE_CODE (r
) != SIZEOF_EXPR
)
6058 r
= cxx_eval_constant_expression (ctx
, r
, lval
,
6059 non_constant_p
, overflow_p
,
6063 *non_constant_p
= true;
6064 gcc_assert (ctx
->quiet
);
6071 /* check_return_expr sometimes wraps a TARGET_EXPR in a
6072 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
6073 introduced by build_call_a. */
6074 tree op0
= TREE_OPERAND (t
, 0);
6075 tree op1
= TREE_OPERAND (t
, 1);
6077 if ((TREE_CODE (op0
) == TARGET_EXPR
&& op1
== TARGET_EXPR_SLOT (op0
))
6078 || TREE_CODE (op1
) == EMPTY_CLASS_EXPR
)
6079 r
= cxx_eval_constant_expression (ctx
, op0
,
6080 lval
, non_constant_p
, overflow_p
,
6084 /* Check that the LHS is constant and then discard it. */
6085 cxx_eval_constant_expression (ctx
, op0
,
6086 true, non_constant_p
, overflow_p
,
6088 if (*non_constant_p
)
6090 op1
= TREE_OPERAND (t
, 1);
6091 r
= cxx_eval_constant_expression (ctx
, op1
,
6092 lval
, non_constant_p
, overflow_p
,
6098 case POINTER_PLUS_EXPR
:
6099 case POINTER_DIFF_EXPR
:
6103 case TRUNC_DIV_EXPR
:
6105 case FLOOR_DIV_EXPR
:
6106 case ROUND_DIV_EXPR
:
6107 case TRUNC_MOD_EXPR
:
6109 case ROUND_MOD_EXPR
:
6111 case EXACT_DIV_EXPR
:
6121 case TRUTH_XOR_EXPR
:
6128 case SPACESHIP_EXPR
:
6129 case UNORDERED_EXPR
:
6139 r
= cxx_eval_binary_expression (ctx
, t
, lval
,
6140 non_constant_p
, overflow_p
);
6143 /* fold can introduce non-IF versions of these; still treat them as
6144 short-circuiting. */
6145 case TRUTH_AND_EXPR
:
6146 case TRUTH_ANDIF_EXPR
:
6147 r
= cxx_eval_logical_expression (ctx
, t
, boolean_false_node
,
6150 non_constant_p
, overflow_p
);
6154 case TRUTH_ORIF_EXPR
:
6155 r
= cxx_eval_logical_expression (ctx
, t
, boolean_true_node
,
6158 non_constant_p
, overflow_p
);
6162 r
= cxx_eval_array_reference (ctx
, t
, lval
,
6163 non_constant_p
, overflow_p
);
6167 if (is_overloaded_fn (t
))
6169 /* We can only get here in checking mode via
6170 build_non_dependent_expr, because any expression that
6171 calls or takes the address of the function will have
6172 pulled a FUNCTION_DECL out of the COMPONENT_REF. */
6173 gcc_checking_assert (ctx
->quiet
|| errorcount
);
6174 *non_constant_p
= true;
6177 r
= cxx_eval_component_reference (ctx
, t
, lval
,
6178 non_constant_p
, overflow_p
);
6182 r
= cxx_eval_bit_field_ref (ctx
, t
, lval
,
6183 non_constant_p
, overflow_p
);
6188 if (jump_target
&& *jump_target
)
6190 tree orig_jump
= *jump_target
;
6191 tree arg
= ((TREE_CODE (t
) != IF_STMT
|| TREE_OPERAND (t
, 1))
6192 ? TREE_OPERAND (t
, 1) : void_node
);
6193 /* When jumping to a label, the label might be either in the
6194 then or else blocks, so process then block first in skipping
6195 mode first, and if we are still in the skipping mode at its end,
6196 process the else block too. */
6197 r
= cxx_eval_constant_expression (ctx
, arg
, lval
, non_constant_p
,
6198 overflow_p
, jump_target
);
6199 /* It's possible that we found the label in the then block. But
6200 it could have been followed by another jumping statement, e.g.
6201 say we're looking for case 1:
6204 // skipped statements
6205 case 1:; // clears up *jump_target
6206 return 1; // and sets it to a RETURN_EXPR
6209 in which case we need not go looking to the else block.
6210 (goto is not allowed in a constexpr function.) */
6211 if (*jump_target
== orig_jump
)
6213 arg
= ((TREE_CODE (t
) != IF_STMT
|| TREE_OPERAND (t
, 2))
6214 ? TREE_OPERAND (t
, 2) : void_node
);
6215 r
= cxx_eval_constant_expression (ctx
, arg
, lval
, non_constant_p
,
6216 overflow_p
, jump_target
);
6220 r
= cxx_eval_conditional_expression (ctx
, t
, lval
,
6221 non_constant_p
, overflow_p
,
6225 r
= cxx_eval_vector_conditional_expression (ctx
, t
, non_constant_p
,
6230 if (TREE_CONSTANT (t
) && reduced_constant_expression_p (t
))
6232 /* Don't re-process a constant CONSTRUCTOR, but do fold it to
6233 VECTOR_CST if applicable. */
6234 verify_constructor_flags (t
);
6235 if (TREE_CONSTANT (t
))
6238 r
= cxx_eval_bare_aggregate (ctx
, t
, lval
,
6239 non_constant_p
, overflow_p
);
6243 /* We can get this in a defaulted constructor for a class with a
6244 non-static data member of array type. Either the initializer will
6245 be NULL, meaning default-initialization, or it will be an lvalue
6246 or xvalue of the same type, meaning direct-initialization from the
6247 corresponding member. */
6248 r
= cxx_eval_vec_init (ctx
, t
, lval
,
6249 non_constant_p
, overflow_p
);
6253 r
= cxx_eval_trinary_expression (ctx
, t
, lval
,
6254 non_constant_p
, overflow_p
);
6258 if (REINTERPRET_CAST_P (t
))
6262 "%<reinterpret_cast%> is not a constant expression");
6263 *non_constant_p
= true;
6268 case VIEW_CONVERT_EXPR
:
6269 case UNARY_PLUS_EXPR
:
6271 tree oldop
= TREE_OPERAND (t
, 0);
6273 tree op
= cxx_eval_constant_expression (ctx
, oldop
,
6275 non_constant_p
, overflow_p
);
6276 if (*non_constant_p
)
6278 tree type
= TREE_TYPE (t
);
6280 if (VOID_TYPE_P (type
))
6283 if (TREE_CODE (t
) == CONVERT_EXPR
6284 && ARITHMETIC_TYPE_P (type
)
6285 && INDIRECT_TYPE_P (TREE_TYPE (op
)))
6289 "conversion from pointer type %qT to arithmetic type "
6290 "%qT in a constant expression", TREE_TYPE (op
), type
);
6291 *non_constant_p
= true;
6295 if (TREE_CODE (op
) == PTRMEM_CST
&& !TYPE_PTRMEM_P (type
))
6296 op
= cplus_expand_constant (op
);
6298 if (TREE_CODE (op
) == PTRMEM_CST
&& tcode
== NOP_EXPR
)
6300 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (op
))
6301 && !can_convert_qual (type
, op
))
6302 op
= cplus_expand_constant (op
);
6303 return cp_fold_convert (type
, op
);
6306 if (INDIRECT_TYPE_P (type
) && TREE_CODE (op
) == INTEGER_CST
)
6308 if (integer_zerop (op
))
6310 if (TYPE_REF_P (type
))
6314 "dereferencing a null pointer");
6315 *non_constant_p
= true;
6318 else if (TYPE_PTR_P (TREE_TYPE (op
)))
6320 tree from
= TREE_TYPE (op
);
6322 if (!can_convert (type
, from
, tf_none
))
6326 "conversion of %qT null pointer to %qT "
6327 "is not a constant expression",
6329 *non_constant_p
= true;
6336 /* This detects for example:
6337 reinterpret_cast<void*>(sizeof 0)
6340 error_at (loc
, "%<reinterpret_cast<%T>(%E)%> is not "
6341 "a constant expression",
6343 *non_constant_p
= true;
6348 if (INDIRECT_TYPE_P (type
)
6349 && TREE_CODE (op
) == NOP_EXPR
6350 && TREE_TYPE (op
) == ptr_type_node
6351 && TREE_CODE (TREE_OPERAND (op
, 0)) == ADDR_EXPR
6352 && VAR_P (TREE_OPERAND (TREE_OPERAND (op
, 0), 0))
6353 && DECL_NAME (TREE_OPERAND (TREE_OPERAND (op
, 0),
6354 0)) == heap_uninit_identifier
)
6356 tree var
= TREE_OPERAND (TREE_OPERAND (op
, 0), 0);
6357 tree var_size
= TYPE_SIZE_UNIT (TREE_TYPE (var
));
6358 tree elt_type
= TREE_TYPE (type
);
6359 tree cookie_size
= NULL_TREE
;
6360 if (TREE_CODE (elt_type
) == RECORD_TYPE
6361 && TYPE_NAME (elt_type
) == heap_identifier
)
6363 tree fld1
= TYPE_FIELDS (elt_type
);
6364 tree fld2
= DECL_CHAIN (fld1
);
6365 elt_type
= TREE_TYPE (TREE_TYPE (fld2
));
6366 cookie_size
= TYPE_SIZE_UNIT (TREE_TYPE (fld1
));
6368 DECL_NAME (var
) = heap_identifier
;
6370 = build_new_constexpr_heap_type (elt_type
, cookie_size
,
6372 TREE_TYPE (TREE_OPERAND (op
, 0))
6373 = build_pointer_type (TREE_TYPE (var
));
6376 if (op
== oldop
&& tcode
!= UNARY_PLUS_EXPR
)
6377 /* We didn't fold at the top so we could check for ptr-int
6383 /* Handle an array's bounds having been deduced after we built
6384 the wrapping expression. */
6385 if (same_type_ignoring_tlq_and_bounds_p (type
, TREE_TYPE (op
)))
6387 else if (sop
= tree_strip_nop_conversions (op
),
6388 sop
!= op
&& (same_type_ignoring_tlq_and_bounds_p
6389 (type
, TREE_TYPE (sop
))))
6391 else if (tcode
== UNARY_PLUS_EXPR
)
6392 r
= fold_convert (TREE_TYPE (t
), op
);
6394 r
= fold_build1 (tcode
, type
, op
);
6396 /* Conversion of an out-of-range value has implementation-defined
6397 behavior; the language considers it different from arithmetic
6398 overflow, which is undefined. */
6399 if (TREE_OVERFLOW_P (r
) && !TREE_OVERFLOW_P (op
))
6400 TREE_OVERFLOW (r
) = false;
6404 case EMPTY_CLASS_EXPR
:
6405 /* This is good enough for a function argument that might not get
6406 used, and they can't do anything with it, so just return it. */
6409 case STATEMENT_LIST
:
6411 new_ctx
.ctor
= new_ctx
.object
= NULL_TREE
;
6412 return cxx_eval_statement_list (&new_ctx
, t
,
6413 non_constant_p
, overflow_p
, jump_target
);
6416 return cxx_eval_constant_expression (ctx
, BIND_EXPR_BODY (t
),
6418 non_constant_p
, overflow_p
,
6421 case PREINCREMENT_EXPR
:
6422 case POSTINCREMENT_EXPR
:
6423 case PREDECREMENT_EXPR
:
6424 case POSTDECREMENT_EXPR
:
6425 return cxx_eval_increment_expression (ctx
, t
,
6426 lval
, non_constant_p
, overflow_p
);
6432 case VEC_DELETE_EXPR
:
6435 /* GCC internal stuff. */
6437 case NON_DEPENDENT_EXPR
:
6441 error_at (loc
, "expression %qE is not a constant expression", t
);
6442 *non_constant_p
= true;
6446 /* Virtual function lookup. We don't need to do anything fancy. */
6447 return cxx_eval_constant_expression (ctx
, OBJ_TYPE_REF_EXPR (t
),
6448 lval
, non_constant_p
, overflow_p
);
6450 case PLACEHOLDER_EXPR
:
6451 /* Use of the value or address of the current object. */
6452 if (tree ctor
= lookup_placeholder (ctx
, lval
, TREE_TYPE (t
)))
6454 if (TREE_CODE (ctor
) == CONSTRUCTOR
)
6457 return cxx_eval_constant_expression (ctx
, ctor
, lval
,
6458 non_constant_p
, overflow_p
);
6460 /* A placeholder without a referent. We can get here when
6461 checking whether NSDMIs are noexcept, or in massage_init_elt;
6462 just say it's non-constant for now. */
6463 gcc_assert (ctx
->quiet
);
6464 *non_constant_p
= true;
6469 tree cond
= TREE_OPERAND (t
, 0);
6470 cond
= cxx_eval_constant_expression (ctx
, cond
, /*lval*/false,
6471 non_constant_p
, overflow_p
);
6472 VERIFY_CONSTANT (cond
);
6473 if (integer_nonzerop (cond
))
6479 *jump_target
= TREE_OPERAND (t
, 0);
6480 gcc_assert (breaks (jump_target
) || continues (jump_target
)
6481 /* Allow for jumping to a cdtor_label. */
6482 || returns (jump_target
));
6489 cxx_eval_loop_expr (ctx
, t
,
6490 non_constant_p
, overflow_p
, jump_target
);
6495 cxx_eval_switch_expr (ctx
, t
,
6496 non_constant_p
, overflow_p
, jump_target
);
6500 /* It's possible to get a requires-expression in a constant
6501 expression. For example:
6503 template<typename T> concept bool C() {
6504 return requires (T t) { t; };
6507 template<typename T> requires !C<T>() void f(T);
6509 Normalization leaves f with the associated constraint
6510 '!requires (T t) { ... }' which is not transformed into
6512 if (!processing_template_decl
)
6513 return satisfy_constraint_expression (t
);
6515 *non_constant_p
= true;
6519 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
6521 non_constant_p
, overflow_p
,
6529 case TEMPLATE_ID_EXPR
:
6531 /* We can evaluate template-id that refers to a concept only if
6532 the template arguments are non-dependent. */
6533 tree id
= unpack_concept_check (t
);
6534 tree tmpl
= TREE_OPERAND (id
, 0);
6535 if (!concept_definition_p (tmpl
))
6536 internal_error ("unexpected template-id %qE", t
);
6538 if (function_concept_p (tmpl
))
6541 error_at (cp_expr_loc_or_input_loc (t
),
6542 "function concept must be called");
6543 r
= error_mark_node
;
6547 if (!processing_template_decl
6548 && !uid_sensitive_constexpr_evaluation_p ())
6549 r
= evaluate_concept_check (t
, tf_warning_or_error
);
6551 *non_constant_p
= true;
6558 inline_asm_in_constexpr_error (loc
);
6559 *non_constant_p
= true;
6563 if (STATEMENT_CODE_P (TREE_CODE (t
)))
6565 /* This function doesn't know how to deal with pre-genericize
6566 statements; this can only happen with statement-expressions,
6567 so for now just fail. */
6569 error_at (EXPR_LOCATION (t
),
6570 "statement is not a constant expression");
6573 internal_error ("unexpected expression %qE of kind %s", t
,
6574 get_tree_code_name (TREE_CODE (t
)));
6575 *non_constant_p
= true;
6579 if (r
== error_mark_node
)
6580 *non_constant_p
= true;
6582 if (*non_constant_p
)
6588 /* P0859: A function is needed for constant evaluation if it is a constexpr
6589 function that is named by an expression ([basic.def.odr]) that is
6590 potentially constant evaluated.
6592 So we need to instantiate any constexpr functions mentioned by the
6593 expression even if the definition isn't needed for evaluating the
6597 instantiate_cx_fn_r (tree
*tp
, int *walk_subtrees
, void */
*data*/
)
6599 if (TREE_CODE (*tp
) == FUNCTION_DECL
6600 && DECL_DECLARED_CONSTEXPR_P (*tp
)
6601 && !DECL_INITIAL (*tp
)
6602 && !trivial_fn_p (*tp
)
6603 && DECL_TEMPLOID_INSTANTIATION (*tp
)
6604 && !uid_sensitive_constexpr_evaluation_p ())
6607 instantiate_decl (*tp
, /*defer_ok*/false, /*expl_inst*/false);
6610 else if (TREE_CODE (*tp
) == CALL_EXPR
6611 || TREE_CODE (*tp
) == AGGR_INIT_EXPR
)
6613 if (EXPR_HAS_LOCATION (*tp
))
6614 input_location
= EXPR_LOCATION (*tp
);
6624 instantiate_constexpr_fns (tree t
)
6626 location_t loc
= input_location
;
6627 cp_walk_tree_without_duplicates (&t
, instantiate_cx_fn_r
, NULL
);
6628 input_location
= loc
;
6631 /* Look for heap variables in the expression *TP. */
6634 find_heap_var_refs (tree
*tp
, int *walk_subtrees
, void */
*data*/
)
6637 && (DECL_NAME (*tp
) == heap_uninit_identifier
6638 || DECL_NAME (*tp
) == heap_identifier
6639 || DECL_NAME (*tp
) == heap_deleted_identifier
))
6647 /* Find immediate function decls in *TP if any. */
6650 find_immediate_fndecl (tree
*tp
, int */
*walk_subtrees*/
, void */
*data*/
)
6652 if (TREE_CODE (*tp
) == FUNCTION_DECL
&& DECL_IMMEDIATE_FUNCTION_P (*tp
))
6657 /* ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
6658 STRICT has the same sense as for constant_value_1: true if we only allow
6659 conforming C++ constant expressions, or false if we want a constant value
6660 even if it doesn't conform.
6661 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
6662 per P0595 even when ALLOW_NON_CONSTANT is true.
6663 CONSTEXPR_DTOR is true when evaluating the dtor of a constexpr variable.
6664 OBJECT must be non-NULL in that case. */
6667 cxx_eval_outermost_constant_expr (tree t
, bool allow_non_constant
,
6669 bool manifestly_const_eval
= false,
6670 bool constexpr_dtor
= false,
6671 tree object
= NULL_TREE
)
6673 auto_timevar
time (TV_CONSTEXPR
);
6675 bool non_constant_p
= false;
6676 bool overflow_p
= false;
6678 if (BRACE_ENCLOSED_INITIALIZER_P (t
))
6680 gcc_checking_assert (allow_non_constant
);
6684 constexpr_global_ctx global_ctx
;
6685 constexpr_ctx ctx
= { &global_ctx
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
6686 allow_non_constant
, strict
,
6687 manifestly_const_eval
|| !allow_non_constant
};
6689 /* Turn off -frounding-math for manifestly constant evaluation. */
6690 warning_sentinel
rm (flag_rounding_math
, ctx
.manifestly_const_eval
);
6691 tree type
= initialized_type (t
);
6693 bool is_consteval
= false;
6694 if (VOID_TYPE_P (type
))
6697 /* Used for destructors of array elements. */
6698 type
= TREE_TYPE (object
);
6701 if (cxx_dialect
< cxx20
)
6703 if (TREE_CODE (t
) != CALL_EXPR
&& TREE_CODE (t
) != AGGR_INIT_EXPR
)
6705 /* Calls to immediate functions returning void need to be
6707 tree fndecl
= cp_get_callee_fndecl_nofold (t
);
6708 if (fndecl
== NULL_TREE
|| !DECL_IMMEDIATE_FUNCTION_P (fndecl
))
6711 is_consteval
= true;
6714 else if (cxx_dialect
>= cxx20
6715 && (TREE_CODE (t
) == CALL_EXPR
6716 || TREE_CODE (t
) == AGGR_INIT_EXPR
6717 || TREE_CODE (t
) == TARGET_EXPR
))
6719 /* For non-concept checks, determine if it is consteval. */
6720 if (!concept_check_p (t
))
6723 if (TREE_CODE (x
) == TARGET_EXPR
)
6724 x
= TARGET_EXPR_INITIAL (x
);
6725 tree fndecl
= cp_get_callee_fndecl_nofold (x
);
6726 if (fndecl
&& DECL_IMMEDIATE_FUNCTION_P (fndecl
))
6727 is_consteval
= true;
6730 if (AGGREGATE_TYPE_P (type
) || VECTOR_TYPE_P (type
))
6732 /* In C++14 an NSDMI can participate in aggregate initialization,
6733 and can refer to the address of the object being initialized, so
6734 we need to pass in the relevant VAR_DECL if we want to do the
6735 evaluation in a single pass. The evaluation will dynamically
6736 update ctx.values for the VAR_DECL. We use the same strategy
6737 for C++11 constexpr constructors that refer to the object being
6741 gcc_assert (object
&& VAR_P (object
));
6742 gcc_assert (DECL_DECLARED_CONSTEXPR_P (object
));
6743 gcc_assert (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object
));
6744 if (error_operand_p (DECL_INITIAL (object
)))
6746 ctx
.ctor
= unshare_expr (DECL_INITIAL (object
));
6747 TREE_READONLY (ctx
.ctor
) = false;
6748 /* Temporarily force decl_really_constant_value to return false
6749 for it, we want to use ctx.ctor for the current value instead. */
6750 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object
) = false;
6754 ctx
.ctor
= build_constructor (type
, NULL
);
6755 CONSTRUCTOR_NO_CLEARING (ctx
.ctor
) = true;
6759 if (TREE_CODE (t
) == TARGET_EXPR
)
6760 object
= TARGET_EXPR_SLOT (t
);
6761 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
)
6762 object
= AGGR_INIT_EXPR_SLOT (t
);
6764 ctx
.object
= object
;
6766 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6767 (type
, TREE_TYPE (object
)));
6768 if (object
&& DECL_P (object
))
6769 global_ctx
.values
.put (object
, ctx
.ctor
);
6770 if (TREE_CODE (r
) == TARGET_EXPR
)
6771 /* Avoid creating another CONSTRUCTOR when we expand the
6773 r
= TARGET_EXPR_INITIAL (r
);
6776 auto_vec
<tree
, 16> cleanups
;
6777 global_ctx
.cleanups
= &cleanups
;
6779 instantiate_constexpr_fns (r
);
6780 r
= cxx_eval_constant_expression (&ctx
, r
,
6781 false, &non_constant_p
, &overflow_p
);
6783 if (!constexpr_dtor
)
6784 verify_constant (r
, allow_non_constant
, &non_constant_p
, &overflow_p
);
6786 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object
) = true;
6790 /* Evaluate the cleanups. */
6791 FOR_EACH_VEC_ELT_REVERSE (cleanups
, i
, cleanup
)
6792 cxx_eval_constant_expression (&ctx
, cleanup
, false,
6793 &non_constant_p
, &overflow_p
);
6795 /* Mutable logic is a bit tricky: we want to allow initialization of
6796 constexpr variables with mutable members, but we can't copy those
6797 members to another constexpr variable. */
6798 if (TREE_CODE (r
) == CONSTRUCTOR
&& CONSTRUCTOR_MUTABLE_POISON (r
))
6800 if (!allow_non_constant
)
6801 error ("%qE is not a constant expression because it refers to "
6802 "mutable subobjects of %qT", t
, type
);
6803 non_constant_p
= true;
6806 if (TREE_CODE (r
) == CONSTRUCTOR
&& CONSTRUCTOR_NO_CLEARING (r
))
6808 if (!allow_non_constant
)
6809 error ("%qE is not a constant expression because it refers to "
6810 "an incompletely initialized variable", t
);
6811 TREE_CONSTANT (r
) = false;
6812 non_constant_p
= true;
6815 if (!global_ctx
.heap_vars
.is_empty ())
6817 tree heap_var
= cp_walk_tree_without_duplicates (&r
, find_heap_var_refs
,
6822 if (!allow_non_constant
&& !non_constant_p
)
6823 error_at (DECL_SOURCE_LOCATION (heap_var
),
6824 "%qE is not a constant expression because it refers to "
6825 "a result of %<operator new%>", t
);
6827 non_constant_p
= true;
6829 FOR_EACH_VEC_ELT (global_ctx
.heap_vars
, i
, heap_var
)
6830 if (DECL_NAME (heap_var
) != heap_deleted_identifier
)
6832 if (!allow_non_constant
&& !non_constant_p
)
6833 error_at (DECL_SOURCE_LOCATION (heap_var
),
6834 "%qE is not a constant expression because allocated "
6835 "storage has not been deallocated", t
);
6837 non_constant_p
= true;
6841 /* Check that immediate invocation does not return an expression referencing
6842 any immediate function decls. They need to be allowed while parsing
6843 immediate functions, but can't leak outside of them. */
6846 && (current_function_decl
== NULL_TREE
6847 || !DECL_IMMEDIATE_FUNCTION_P (current_function_decl
)))
6848 if (tree immediate_fndecl
6849 = cp_walk_tree_without_duplicates (&r
, find_immediate_fndecl
,
6852 if (!allow_non_constant
&& !non_constant_p
)
6853 error_at (cp_expr_loc_or_input_loc (t
),
6854 "immediate evaluation returns address of immediate "
6855 "function %qD", immediate_fndecl
);
6857 non_constant_p
= true;
6861 if (!non_constant_p
&& overflow_p
)
6862 non_constant_p
= true;
6864 /* Unshare the result. */
6865 bool should_unshare
= true;
6866 if (r
== t
|| (TREE_CODE (t
) == TARGET_EXPR
6867 && TARGET_EXPR_INITIAL (t
) == r
))
6868 should_unshare
= false;
6870 if (non_constant_p
&& !allow_non_constant
)
6871 return error_mark_node
;
6872 else if (constexpr_dtor
)
6874 else if (non_constant_p
&& TREE_CONSTANT (r
))
6876 /* If __builtin_is_constant_evaluated () was evaluated to true
6877 and the result is not a valid constant expression, we need to
6879 if (manifestly_const_eval
)
6880 return cxx_eval_outermost_constant_expr (t
, true, strict
,
6881 false, false, object
);
6882 /* This isn't actually constant, so unset TREE_CONSTANT.
6883 Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires
6884 it to be set if it is invariant address, even when it is not
6885 a valid C++ constant expression. Wrap it with a NOP_EXPR
6887 if (EXPR_P (r
) && TREE_CODE (r
) != ADDR_EXPR
)
6889 else if (TREE_CODE (r
) == CONSTRUCTOR
)
6890 r
= build1 (VIEW_CONVERT_EXPR
, TREE_TYPE (r
), r
);
6892 r
= build_nop (TREE_TYPE (r
), r
);
6893 TREE_CONSTANT (r
) = false;
6895 else if (non_constant_p
)
6899 r
= unshare_expr (r
);
6901 if (TREE_CODE (r
) == CONSTRUCTOR
&& CLASS_TYPE_P (TREE_TYPE (r
)))
6903 r
= adjust_temp_type (type
, r
);
6904 if (TREE_CODE (t
) == TARGET_EXPR
6905 && TARGET_EXPR_INITIAL (t
) == r
)
6907 else if (TREE_CODE (t
) != CONSTRUCTOR
)
6909 r
= get_target_expr_sfinae (r
, tf_warning_or_error
| tf_no_cleanup
);
6910 TREE_CONSTANT (r
) = true;
6917 /* If T represents a constant expression returns its reduced value.
6918 Otherwise return error_mark_node. If T is dependent, then
6922 cxx_constant_value (tree t
, tree decl
)
6924 return cxx_eval_outermost_constant_expr (t
, false, true, true, false, decl
);
6927 /* Like cxx_constant_value, but used for evaluation of constexpr destructors
6928 of constexpr variables. The actual initializer of DECL is not modified. */
6931 cxx_constant_dtor (tree t
, tree decl
)
6933 cxx_eval_outermost_constant_expr (t
, false, true, true, true, decl
);
6936 /* Helper routine for fold_simple function. Either return simplified
6937 expression T, otherwise NULL_TREE.
6938 In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold
6939 even if we are within template-declaration. So be careful on call, as in
6940 such case types can be undefined. */
6943 fold_simple_1 (tree t
)
6946 enum tree_code code
= TREE_CODE (t
);
6958 return fold_sizeof_expr (t
);
6967 case TRUTH_NOT_EXPR
:
6969 case VIEW_CONVERT_EXPR
:
6972 case FIX_TRUNC_EXPR
:
6973 case FIXED_CONVERT_EXPR
:
6974 case ADDR_SPACE_CONVERT_EXPR
:
6976 op1
= TREE_OPERAND (t
, 0);
6978 t
= const_unop (code
, TREE_TYPE (t
), op1
);
6982 if (CONVERT_EXPR_CODE_P (code
)
6983 && TREE_OVERFLOW_P (t
) && !TREE_OVERFLOW_P (op1
))
6984 TREE_OVERFLOW (t
) = false;
6992 /* If T is a simple constant expression, returns its simplified value.
6993 Otherwise returns T. In contrast to maybe_constant_value we
6994 simplify only few operations on constant-expressions, and we don't
6995 try to simplify constexpressions. */
6998 fold_simple (tree t
)
7000 if (processing_template_decl
)
7003 tree r
= fold_simple_1 (t
);
7010 /* If T is a constant expression, returns its reduced value.
7011 Otherwise, if T does not have TREE_CONSTANT set, returns T.
7012 Otherwise, returns a version of T without TREE_CONSTANT.
7013 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated
7016 static GTY((deletable
)) hash_map
<tree
, tree
> *cv_cache
;
7019 maybe_constant_value (tree t
, tree decl
, bool manifestly_const_eval
)
7023 if (!is_nondependent_constant_expression (t
))
7025 if (TREE_OVERFLOW_P (t
))
7027 t
= build_nop (TREE_TYPE (t
), t
);
7028 TREE_CONSTANT (t
) = false;
7032 else if (CONSTANT_CLASS_P (t
))
7033 /* No caching or evaluation needed. */
7036 if (manifestly_const_eval
)
7037 return cxx_eval_outermost_constant_expr (t
, true, true, true, false, decl
);
7039 if (cv_cache
== NULL
)
7040 cv_cache
= hash_map
<tree
, tree
>::create_ggc (101);
7041 if (tree
*cached
= cv_cache
->get (t
))
7046 r
= break_out_target_exprs (r
, /*clear_loc*/true);
7047 protected_set_expr_location (r
, EXPR_LOCATION (t
));
7052 uid_sensitive_constexpr_evaluation_checker c
;
7053 r
= cxx_eval_outermost_constant_expr (t
, true, true, false, false, decl
);
7054 gcc_checking_assert (r
== t
7055 || CONVERT_EXPR_P (t
)
7056 || TREE_CODE (t
) == VIEW_CONVERT_EXPR
7057 || (TREE_CONSTANT (t
) && !TREE_CONSTANT (r
))
7058 || !cp_tree_equal (r
, t
));
7059 if (!c
.evaluation_restricted_p ())
7060 cv_cache
->put (t
, r
);
7064 /* Dispose of the whole CV_CACHE. */
7067 clear_cv_cache (void)
7069 if (cv_cache
!= NULL
)
7073 /* Dispose of the whole CV_CACHE, FOLD_CACHE, and satisfaction caches. */
7076 clear_cv_and_fold_caches (bool sat
/*= true*/)
7079 clear_fold_cache ();
7081 clear_satisfaction_cache ();
7084 /* Internal function handling expressions in templates for
7085 fold_non_dependent_expr and fold_non_dependent_init.
7087 If we're in a template, but T isn't value dependent, simplify
7088 it. We're supposed to treat:
7090 template <typename T> void f(T[1 + 1]);
7091 template <typename T> void f(T[2]);
7093 as two declarations of the same function, for example. */
7096 fold_non_dependent_expr_template (tree t
, tsubst_flags_t complain
,
7097 bool manifestly_const_eval
,
7100 gcc_assert (processing_template_decl
);
7102 if (is_nondependent_constant_expression (t
))
7104 processing_template_decl_sentinel s
;
7105 t
= instantiate_non_dependent_expr_internal (t
, complain
);
7107 if (type_unknown_p (t
) || BRACE_ENCLOSED_INITIALIZER_P (t
))
7109 if (TREE_OVERFLOW_P (t
))
7111 t
= build_nop (TREE_TYPE (t
), t
);
7112 TREE_CONSTANT (t
) = false;
7117 tree r
= cxx_eval_outermost_constant_expr (t
, true, true,
7118 manifestly_const_eval
,
7120 /* cp_tree_equal looks through NOPs, so allow them. */
7121 gcc_checking_assert (r
== t
7122 || CONVERT_EXPR_P (t
)
7123 || TREE_CODE (t
) == VIEW_CONVERT_EXPR
7124 || (TREE_CONSTANT (t
) && !TREE_CONSTANT (r
))
7125 || !cp_tree_equal (r
, t
));
7128 else if (TREE_OVERFLOW_P (t
))
7130 t
= build_nop (TREE_TYPE (t
), t
);
7131 TREE_CONSTANT (t
) = false;
7137 /* Like maybe_constant_value but first fully instantiate the argument.
7139 Note: this is equivalent to instantiate_non_dependent_expr_sfinae
7140 (t, complain) followed by maybe_constant_value but is more efficient,
7141 because it calls instantiation_dependent_expression_p and
7142 potential_constant_expression at most once.
7143 The manifestly_const_eval argument is passed to maybe_constant_value.
7145 Callers should generally pass their active complain, or if they are in a
7146 non-template, diagnosing context, they can use the default of
7147 tf_warning_or_error. Callers that might be within a template context, don't
7148 have a complain parameter, and aren't going to remember the result for long
7149 (e.g. null_ptr_cst_p), can pass tf_none and deal with error_mark_node
7153 fold_non_dependent_expr (tree t
,
7154 tsubst_flags_t complain
/* = tf_warning_or_error */,
7155 bool manifestly_const_eval
/* = false */,
7156 tree object
/* = NULL_TREE */)
7161 if (processing_template_decl
)
7162 return fold_non_dependent_expr_template (t
, complain
,
7163 manifestly_const_eval
, object
);
7165 return maybe_constant_value (t
, object
, manifestly_const_eval
);
7168 /* Like fold_non_dependent_expr, but if EXPR couldn't be folded to a constant,
7169 return the original expression. */
7172 maybe_fold_non_dependent_expr (tree expr
,
7173 tsubst_flags_t complain
/*=tf_warning_or_error*/)
7175 tree t
= fold_non_dependent_expr (expr
, complain
);
7176 if (t
&& TREE_CONSTANT (t
))
7182 /* Like maybe_constant_init but first fully instantiate the argument. */
7185 fold_non_dependent_init (tree t
,
7186 tsubst_flags_t complain
/*=tf_warning_or_error*/,
7187 bool manifestly_const_eval
/*=false*/)
7192 if (processing_template_decl
)
7194 t
= fold_non_dependent_expr_template (t
, complain
,
7195 manifestly_const_eval
, NULL_TREE
);
7196 /* maybe_constant_init does this stripping, so do it here too. */
7197 if (TREE_CODE (t
) == TARGET_EXPR
)
7199 tree init
= TARGET_EXPR_INITIAL (t
);
7200 if (TREE_CODE (init
) == CONSTRUCTOR
)
7206 return maybe_constant_init (t
, NULL_TREE
, manifestly_const_eval
);
7209 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
7210 than wrapped in a TARGET_EXPR.
7211 ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
7212 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
7213 per P0595 even when ALLOW_NON_CONSTANT is true. */
7216 maybe_constant_init_1 (tree t
, tree decl
, bool allow_non_constant
,
7217 bool manifestly_const_eval
)
7221 if (TREE_CODE (t
) == EXPR_STMT
)
7222 t
= TREE_OPERAND (t
, 0);
7223 if (TREE_CODE (t
) == CONVERT_EXPR
7224 && VOID_TYPE_P (TREE_TYPE (t
)))
7225 t
= TREE_OPERAND (t
, 0);
7226 if (TREE_CODE (t
) == INIT_EXPR
)
7227 t
= TREE_OPERAND (t
, 1);
7228 if (TREE_CODE (t
) == TARGET_EXPR
)
7229 t
= TARGET_EXPR_INITIAL (t
);
7230 if (!is_nondependent_static_init_expression (t
))
7231 /* Don't try to evaluate it. */;
7232 else if (CONSTANT_CLASS_P (t
) && allow_non_constant
)
7233 /* No evaluation needed. */;
7235 t
= cxx_eval_outermost_constant_expr (t
, allow_non_constant
,
7237 manifestly_const_eval
, false, decl
);
7238 if (TREE_CODE (t
) == TARGET_EXPR
)
7240 tree init
= TARGET_EXPR_INITIAL (t
);
7241 if (TREE_CODE (init
) == CONSTRUCTOR
)
7247 /* Wrapper for maybe_constant_init_1 which permits non constants. */
7250 maybe_constant_init (tree t
, tree decl
, bool manifestly_const_eval
)
7252 return maybe_constant_init_1 (t
, decl
, true, manifestly_const_eval
);
7255 /* Wrapper for maybe_constant_init_1 which does not permit non constants. */
7258 cxx_constant_init (tree t
, tree decl
)
7260 return maybe_constant_init_1 (t
, decl
, false, true);
7264 /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
7265 /* Return true if the object referred to by REF has automatic or thread
7268 enum { ck_ok
, ck_bad
, ck_unknown
};
7270 check_automatic_or_tls (tree ref
)
7273 poly_int64 bitsize
, bitpos
;
7275 int volatilep
= 0, unsignedp
= 0;
7276 tree decl
= get_inner_reference (ref
, &bitsize
, &bitpos
, &offset
,
7277 &mode
, &unsignedp
, &volatilep
, false);
7280 /* If there isn't a decl in the middle, we don't know the linkage here,
7281 and this isn't a constant expression anyway. */
7284 dk
= decl_storage_duration (decl
);
7285 return (dk
== dk_auto
|| dk
== dk_thread
) ? ck_bad
: ck_ok
;
7289 /* Data structure for passing data from potential_constant_expression_1
7290 to check_for_return_continue via cp_walk_tree. */
7291 struct check_for_return_continue_data
{
7292 hash_set
<tree
> *pset
;
7296 /* Helper function for potential_constant_expression_1 SWITCH_STMT handling,
7297 called through cp_walk_tree. Return the first RETURN_EXPR found, or note
7298 the first CONTINUE_STMT if RETURN_EXPR is not found. */
7300 check_for_return_continue (tree
*tp
, int *walk_subtrees
, void *data
)
7303 check_for_return_continue_data
*d
= (check_for_return_continue_data
*) data
;
7304 switch (TREE_CODE (t
))
7310 if (d
->continue_stmt
== NULL_TREE
)
7311 d
->continue_stmt
= t
;
7315 if (tree r = cp_walk_tree (&x, check_for_return_continue, data, \
7319 /* For loops, walk subtrees manually, so that continue stmts found
7320 inside of the bodies of the loops are ignored. */
7323 RECUR (DO_COND (t
));
7324 s
= d
->continue_stmt
;
7325 RECUR (DO_BODY (t
));
7326 d
->continue_stmt
= s
;
7331 RECUR (WHILE_COND (t
));
7332 s
= d
->continue_stmt
;
7333 RECUR (WHILE_BODY (t
));
7334 d
->continue_stmt
= s
;
7339 RECUR (FOR_INIT_STMT (t
));
7340 RECUR (FOR_COND (t
));
7341 RECUR (FOR_EXPR (t
));
7342 s
= d
->continue_stmt
;
7343 RECUR (FOR_BODY (t
));
7344 d
->continue_stmt
= s
;
7347 case RANGE_FOR_STMT
:
7349 RECUR (RANGE_FOR_EXPR (t
));
7350 s
= d
->continue_stmt
;
7351 RECUR (RANGE_FOR_BODY (t
));
7352 d
->continue_stmt
= s
;
7356 case STATEMENT_LIST
:
7369 /* Return true if T denotes a potentially constant expression. Issue
7370 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
7371 an lvalue-rvalue conversion is implied. If NOW is true, we want to
7372 consider the expression in the current context, independent of constexpr
7375 C++0x [expr.const] used to say
7377 6 An expression is a potential constant expression if it is
7378 a constant expression where all occurrences of function
7379 parameters are replaced by arbitrary constant expressions
7380 of the appropriate type.
7382 2 A conditional expression is a constant expression unless it
7383 involves one of the following as a potentially evaluated
7384 subexpression (3.2), but subexpressions of logical AND (5.14),
7385 logical OR (5.15), and conditional (5.16) operations that are
7386 not evaluated are not considered. */
7389 potential_constant_expression_1 (tree t
, bool want_rval
, bool strict
, bool now
,
7390 tsubst_flags_t flags
, tree
*jump_target
)
7392 #define RECUR(T,RV) \
7393 potential_constant_expression_1 ((T), (RV), strict, now, flags, jump_target)
7395 enum { any
= false, rval
= true };
7399 if (t
== error_mark_node
)
7403 location_t loc
= cp_expr_loc_or_input_loc (t
);
7406 /* If we are jumping, ignore everything. This is simpler than the
7407 cxx_eval_constant_expression handling because we only need to be
7408 conservatively correct, and we don't necessarily have a constant value
7409 available, so we don't bother with switch tracking. */
7412 if (TREE_THIS_VOLATILE (t
) && want_rval
)
7414 if (flags
& tf_error
)
7415 error_at (loc
, "lvalue-to-rvalue conversion of a volatile lvalue "
7416 "%qE with type %qT", t
, TREE_TYPE (t
));
7419 if (CONSTANT_CLASS_P (t
))
7421 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_TYPED
)
7422 && TREE_TYPE (t
) == error_mark_node
)
7425 switch (TREE_CODE (t
))
7431 case TEMPLATE_ID_EXPR
:
7434 case CASE_LABEL_EXPR
:
7441 case TEMPLATE_PARM_INDEX
:
7443 case IDENTIFIER_NODE
:
7444 case USERDEF_LITERAL
:
7445 /* We can see a FIELD_DECL in a pointer-to-member expression. */
7450 case PLACEHOLDER_EXPR
:
7453 case DEBUG_BEGIN_STMT
:
7457 if (!RECUR (TREE_OPERAND (t
, 0), any
))
7467 if (now
&& want_rval
)
7469 tree type
= TREE_TYPE (t
);
7470 if ((processing_template_decl
&& !COMPLETE_TYPE_P (type
))
7471 || dependent_type_p (type
)
7472 || is_really_empty_class (type
, /*ignore_vptr*/false))
7473 /* An empty class has no data to read. */
7475 if (flags
& tf_error
)
7476 error ("%qE is not a constant expression", t
);
7481 case AGGR_INIT_EXPR
:
7483 /* -- an invocation of a function other than a constexpr function
7484 or a constexpr constructor. */
7486 tree fun
= get_function_named_in_call (t
);
7487 const int nargs
= call_expr_nargs (t
);
7490 if (fun
== NULL_TREE
)
7492 /* Reset to allow the function to continue past the end
7493 of the block below. Otherwise return early. */
7496 if (TREE_CODE (t
) == CALL_EXPR
7497 && CALL_EXPR_FN (t
) == NULL_TREE
)
7498 switch (CALL_EXPR_IFN (t
))
7500 /* These should be ignored, they are optimized away from
7501 constexpr functions. */
7502 case IFN_UBSAN_NULL
:
7503 case IFN_UBSAN_BOUNDS
:
7504 case IFN_UBSAN_VPTR
:
7505 case IFN_FALLTHROUGH
:
7508 case IFN_ADD_OVERFLOW
:
7509 case IFN_SUB_OVERFLOW
:
7510 case IFN_MUL_OVERFLOW
:
7512 case IFN_VEC_CONVERT
:
7522 /* fold_call_expr can't do anything with IFN calls. */
7523 if (flags
& tf_error
)
7524 error_at (loc
, "call to internal function %qE", t
);
7529 if (fun
&& is_overloaded_fn (fun
))
7531 if (TREE_CODE (fun
) == FUNCTION_DECL
)
7533 if (builtin_valid_in_constant_expr_p (fun
))
7535 if (!DECL_DECLARED_CONSTEXPR_P (fun
)
7536 /* Allow any built-in function; if the expansion
7537 isn't constant, we'll deal with that then. */
7538 && !fndecl_built_in_p (fun
)
7539 /* In C++20, replaceable global allocation functions
7540 are constant expressions. */
7541 && (!cxx_replaceable_global_alloc_fn (fun
)
7542 || TREE_CODE (t
) != CALL_EXPR
7543 || (!CALL_FROM_NEW_OR_DELETE_P (t
)
7544 && (current_function_decl
== NULL_TREE
7545 || !is_std_allocator_allocate
7546 (current_function_decl
))))
7547 /* Allow placement new in std::construct_at. */
7548 && (!cxx_placement_new_fn (fun
)
7549 || TREE_CODE (t
) != CALL_EXPR
7550 || current_function_decl
== NULL_TREE
7551 || !is_std_construct_at (current_function_decl
))
7552 && !cxx_dynamic_cast_fn_p (fun
))
7554 if (flags
& tf_error
)
7556 error_at (loc
, "call to non-%<constexpr%> function %qD",
7558 explain_invalid_constexpr_fn (fun
);
7562 /* A call to a non-static member function takes the address
7563 of the object as the first argument. But in a constant
7564 expression the address will be folded away, so look
7566 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun
)
7567 && !DECL_CONSTRUCTOR_P (fun
))
7569 tree x
= get_nth_callarg (t
, 0);
7570 if (is_this_parameter (x
))
7572 /* Don't require an immediately constant value, as
7573 constexpr substitution might not use the value. */
7574 bool sub_now
= false;
7575 if (!potential_constant_expression_1 (x
, rval
, strict
,
7584 if (!RECUR (fun
, true))
7586 fun
= get_first_fn (fun
);
7588 /* Skip initial arguments to base constructors. */
7589 if (DECL_BASE_CONSTRUCTOR_P (fun
))
7590 i
= num_artificial_parms_for (fun
);
7591 fun
= DECL_ORIGIN (fun
);
7595 if (RECUR (fun
, rval
))
7596 /* Might end up being a constant function pointer. */;
7600 for (; i
< nargs
; ++i
)
7602 tree x
= get_nth_callarg (t
, i
);
7603 /* In a template, reference arguments haven't been converted to
7604 REFERENCE_TYPE and we might not even know if the parameter
7605 is a reference, so accept lvalue constants too. */
7606 bool rv
= processing_template_decl
? any
: rval
;
7607 /* Don't require an immediately constant value, as constexpr
7608 substitution might not use the value of the argument. */
7609 bool sub_now
= false;
7610 if (!potential_constant_expression_1 (x
, rv
, strict
,
7611 sub_now
, flags
, jump_target
))
7617 case NON_LVALUE_EXPR
:
7618 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
7619 -- an lvalue of integral type that refers to a non-volatile
7620 const variable or static data member initialized with
7621 constant expressions, or
7623 -- an lvalue of literal type that refers to non-volatile
7624 object defined with constexpr, or that refers to a
7625 sub-object of such an object; */
7626 return RECUR (TREE_OPERAND (t
, 0), rval
);
7629 if (DECL_HAS_VALUE_EXPR_P (t
))
7631 if (now
&& is_normal_capture_proxy (t
))
7633 /* -- in a lambda-expression, a reference to this or to a
7634 variable with automatic storage duration defined outside that
7635 lambda-expression, where the reference would be an
7639 /* Since we're doing an lvalue-rvalue conversion, this might
7640 not be an odr-use, so evaluate the variable directly. */
7641 return RECUR (DECL_CAPTURED_VARIABLE (t
), rval
);
7643 if (flags
& tf_error
)
7645 tree cap
= DECL_CAPTURED_VARIABLE (t
);
7646 error ("lambda capture of %qE is not a constant expression",
7648 if (decl_constant_var_p (cap
))
7649 inform (input_location
, "because it is used as a glvalue");
7653 return RECUR (DECL_VALUE_EXPR (t
), rval
);
7656 && !var_in_maybe_constexpr_fn (t
)
7657 && !type_dependent_expression_p (t
)
7658 && !decl_maybe_constant_var_p (t
)
7660 || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t
))
7661 || (DECL_INITIAL (t
)
7662 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t
)))
7663 && COMPLETE_TYPE_P (TREE_TYPE (t
))
7664 && !is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
7666 if (flags
& tf_error
)
7667 non_const_var_error (loc
, t
);
7673 if (REINTERPRET_CAST_P (t
))
7675 if (flags
& tf_error
)
7676 error_at (loc
, "%<reinterpret_cast%> is not a constant expression");
7681 case VIEW_CONVERT_EXPR
:
7682 /* -- a reinterpret_cast. FIXME not implemented, and this rule
7683 may change to something more specific to type-punning (DR 1312). */
7685 tree from
= TREE_OPERAND (t
, 0);
7686 if (location_wrapper_p (t
))
7687 return (RECUR (from
, want_rval
));
7688 if (INDIRECT_TYPE_P (TREE_TYPE (t
)))
7690 STRIP_ANY_LOCATION_WRAPPER (from
);
7691 if (TREE_CODE (from
) == INTEGER_CST
7692 && !integer_zerop (from
))
7694 if (flags
& tf_error
)
7696 "%<reinterpret_cast%> from integer to pointer");
7700 return (RECUR (from
, TREE_CODE (t
) != VIEW_CONVERT_EXPR
));
7703 case ADDRESSOF_EXPR
:
7704 /* This is like ADDR_EXPR, except it won't form pointer-to-member. */
7705 t
= TREE_OPERAND (t
, 0);
7706 goto handle_addr_expr
;
7709 /* -- a unary operator & that is applied to an lvalue that
7710 designates an object with thread or automatic storage
7712 t
= TREE_OPERAND (t
, 0);
7714 if (TREE_CODE (t
) == OFFSET_REF
&& PTRMEM_OK_P (t
))
7715 /* A pointer-to-member constant. */
7720 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
7721 any checking here, as we might dereference the pointer later. If
7722 we remove this code, also remove check_automatic_or_tls. */
7723 i
= check_automatic_or_tls (t
);
7728 if (flags
& tf_error
)
7729 error ("address-of an object %qE with thread local or "
7730 "automatic storage is not a constant expression", t
);
7734 return RECUR (t
, any
);
7739 /* -- a class member access unless its postfix-expression is
7740 of literal type or of pointer to literal type. */
7741 /* This test would be redundant, as it follows from the
7742 postfix-expression being a potential constant expression. */
7743 if (type_unknown_p (t
))
7745 if (is_overloaded_fn (t
))
7746 /* In a template, a COMPONENT_REF of a function expresses ob.fn(),
7747 which uses ob as an lvalue. */
7754 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
7756 case EXPR_PACK_EXPANSION
:
7757 return RECUR (PACK_EXPANSION_PATTERN (t
), want_rval
);
7761 tree x
= TREE_OPERAND (t
, 0);
7763 if (is_this_parameter (x
) && !is_capture_proxy (x
))
7765 if (!var_in_maybe_constexpr_fn (x
))
7767 if (flags
& tf_error
)
7768 error_at (loc
, "use of %<this%> in a constant expression");
7773 return RECUR (x
, rval
);
7776 case STATEMENT_LIST
:
7778 tree_stmt_iterator i
;
7779 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
7781 if (!RECUR (tsi_stmt (i
), any
))
7789 if (cxx_dialect
< cxx14
)
7791 if (!RECUR (TREE_OPERAND (t
, 0), any
))
7793 /* Just ignore clobbers. */
7794 if (TREE_CLOBBER_P (TREE_OPERAND (t
, 1)))
7796 if (!RECUR (TREE_OPERAND (t
, 1), rval
))
7801 if (cxx_dialect
< cxx14
)
7803 if (!RECUR (TREE_OPERAND (t
, 0), rval
))
7805 if (!RECUR (TREE_OPERAND (t
, 2), rval
))
7810 if (!RECUR (DO_COND (t
), rval
))
7812 if (!RECUR (DO_BODY (t
), any
))
7814 if (breaks (jump_target
) || continues (jump_target
))
7815 *jump_target
= NULL_TREE
;
7819 if (!RECUR (FOR_INIT_STMT (t
), any
))
7822 if (!RECUR (tmp
, rval
))
7826 if (!processing_template_decl
)
7827 tmp
= cxx_eval_outermost_constant_expr (tmp
, true);
7828 /* If we couldn't evaluate the condition, it might not ever be
7830 if (!integer_onep (tmp
))
7833 if (!RECUR (FOR_EXPR (t
), any
))
7835 if (!RECUR (FOR_BODY (t
), any
))
7837 if (breaks (jump_target
) || continues (jump_target
))
7838 *jump_target
= NULL_TREE
;
7841 case RANGE_FOR_STMT
:
7842 if (!RECUR (RANGE_FOR_INIT_STMT (t
), any
))
7844 if (!RECUR (RANGE_FOR_EXPR (t
), any
))
7846 if (!RECUR (RANGE_FOR_BODY (t
), any
))
7848 if (breaks (jump_target
) || continues (jump_target
))
7849 *jump_target
= NULL_TREE
;
7853 tmp
= WHILE_COND (t
);
7854 if (!RECUR (tmp
, rval
))
7856 if (!processing_template_decl
)
7857 tmp
= cxx_eval_outermost_constant_expr (tmp
, true);
7858 /* If we couldn't evaluate the condition, it might not ever be true. */
7859 if (!integer_onep (tmp
))
7861 if (!RECUR (WHILE_BODY (t
), any
))
7863 if (breaks (jump_target
) || continues (jump_target
))
7864 *jump_target
= NULL_TREE
;
7868 if (!RECUR (SWITCH_STMT_COND (t
), rval
))
7870 /* FIXME we don't check SWITCH_STMT_BODY currently, because even
7871 unreachable labels would be checked and it is enough if there is
7872 a single switch cond value for which it is a valid constant
7873 expression. We need to check if there are any RETURN_EXPRs
7874 or CONTINUE_STMTs inside of the body though, as in that case
7875 we need to set *jump_target. */
7878 hash_set
<tree
> pset
;
7879 check_for_return_continue_data data
= { &pset
, NULL_TREE
};
7881 = cp_walk_tree (&SWITCH_STMT_BODY (t
), check_for_return_continue
,
7883 /* The switch might return. */
7884 *jump_target
= ret_expr
;
7885 else if (data
.continue_stmt
)
7886 /* The switch can't return, but might continue. */
7887 *jump_target
= data
.continue_stmt
;
7892 return RECUR (STMT_EXPR_STMT (t
), rval
);
7895 if (cxx_dialect
>= cxx17
)
7896 /* In C++17 lambdas can be constexpr, don't give up yet. */
7898 else if (flags
& tf_error
)
7899 error_at (loc
, "lambda-expression is not a constant expression "
7903 case DYNAMIC_CAST_EXPR
:
7904 case PSEUDO_DTOR_EXPR
:
7908 case VEC_DELETE_EXPR
:
7914 case OMP_DISTRIBUTE
:
7918 case OMP_TARGET_DATA
:
7927 case OMP_TARGET_UPDATE
:
7928 case OMP_TARGET_ENTER_DATA
:
7929 case OMP_TARGET_EXIT_DATA
:
7931 case OMP_ATOMIC_READ
:
7932 case OMP_ATOMIC_CAPTURE_OLD
:
7933 case OMP_ATOMIC_CAPTURE_NEW
:
7939 case OACC_HOST_DATA
:
7943 case OACC_ENTER_DATA
:
7944 case OACC_EXIT_DATA
:
7946 /* GCC internal stuff. */
7948 case TRANSACTION_EXPR
:
7949 case AT_ENCODE_EXPR
:
7951 if (flags
& tf_error
)
7952 error_at (loc
, "expression %qE is not a constant expression", t
);
7956 if (flags
& tf_error
)
7957 inline_asm_in_constexpr_error (loc
);
7961 if (cxx_dialect
>= cxx20
)
7962 /* In C++20 virtual calls can be constexpr, don't give up yet. */
7964 else if (flags
& tf_error
)
7966 "virtual functions cannot be %<constexpr%> before C++20");
7970 /* In C++20, a typeid expression whose operand is of polymorphic
7971 class type can be constexpr. */
7973 tree e
= TREE_OPERAND (t
, 0);
7974 if (cxx_dialect
< cxx20
7977 && !type_dependent_expression_p (e
)
7978 && TYPE_POLYMORPHIC_P (TREE_TYPE (e
)))
7980 if (flags
& tf_error
)
7981 error_at (loc
, "%<typeid%> is not a constant expression "
7982 "because %qE is of polymorphic type", e
);
7988 case POINTER_DIFF_EXPR
:
7999 case SPACESHIP_EXPR
:
8003 case PREINCREMENT_EXPR
:
8004 case POSTINCREMENT_EXPR
:
8005 case PREDECREMENT_EXPR
:
8006 case POSTDECREMENT_EXPR
:
8007 if (cxx_dialect
< cxx14
)
8013 if (TYPE_P (TREE_OPERAND (t
, 0)))
8019 case FIX_TRUNC_EXPR
:
8024 case TRUTH_NOT_EXPR
:
8025 case FIXED_CONVERT_EXPR
:
8026 case UNARY_PLUS_EXPR
:
8027 case UNARY_LEFT_FOLD_EXPR
:
8028 case UNARY_RIGHT_FOLD_EXPR
:
8030 return RECUR (TREE_OPERAND (t
, 0), rval
);
8033 case CONST_CAST_EXPR
:
8034 case STATIC_CAST_EXPR
:
8035 case REINTERPRET_CAST_EXPR
:
8036 case IMPLICIT_CONV_EXPR
:
8037 if (cxx_dialect
< cxx11
8038 && !dependent_type_p (TREE_TYPE (t
))
8039 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
)))
8040 /* In C++98, a conversion to non-integral type can't be part of a
8041 constant expression. */
8043 if (flags
& tf_error
)
8045 "cast to non-integral type %qT in a constant expression",
8049 /* This might be a conversion from a class to a (potentially) literal
8050 type. Let's consider it potentially constant since the conversion
8051 might be a constexpr user-defined conversion. */
8052 else if (cxx_dialect
>= cxx11
8053 && (dependent_type_p (TREE_TYPE (t
))
8054 || !COMPLETE_TYPE_P (TREE_TYPE (t
))
8055 || literal_type_p (TREE_TYPE (t
)))
8056 && TREE_OPERAND (t
, 0))
8058 tree type
= TREE_TYPE (TREE_OPERAND (t
, 0));
8059 /* If this is a dependent type, it could end up being a class
8060 with conversions. */
8061 if (type
== NULL_TREE
|| WILDCARD_TYPE_P (type
))
8063 /* Or a non-dependent class which has conversions. */
8064 else if (CLASS_TYPE_P (type
)
8065 && (TYPE_HAS_CONVERSION (type
) || dependent_scope_p (type
)))
8069 return (RECUR (TREE_OPERAND (t
, 0),
8070 !TYPE_REF_P (TREE_TYPE (t
))));
8073 return RECUR (BIND_EXPR_BODY (t
), want_rval
);
8075 case CLEANUP_POINT_EXPR
:
8076 case MUST_NOT_THROW_EXPR
:
8077 case TRY_CATCH_EXPR
:
8082 case NON_DEPENDENT_EXPR
:
8083 /* For convenience. */
8086 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
8089 tmp
= DECL_EXPR_DECL (t
);
8090 if (VAR_P (tmp
) && !DECL_ARTIFICIAL (tmp
))
8092 if (TREE_STATIC (tmp
))
8094 if (flags
& tf_error
)
8095 error_at (DECL_SOURCE_LOCATION (tmp
), "%qD declared "
8096 "%<static%> in %<constexpr%> context", tmp
);
8099 else if (CP_DECL_THREAD_LOCAL_P (tmp
))
8101 if (flags
& tf_error
)
8102 error_at (DECL_SOURCE_LOCATION (tmp
), "%qD declared "
8103 "%<thread_local%> in %<constexpr%> context", tmp
);
8106 else if (!check_for_uninitialized_const_var
8107 (tmp
, /*constexpr_context_p=*/true, flags
))
8110 return RECUR (tmp
, want_rval
);
8112 case TRY_FINALLY_EXPR
:
8113 return (RECUR (TREE_OPERAND (t
, 0), want_rval
)
8114 && RECUR (TREE_OPERAND (t
, 1), any
));
8117 return RECUR (TREE_OPERAND (t
, 1), want_rval
);
8120 if (!TARGET_EXPR_DIRECT_INIT_P (t
)
8121 && !literal_type_p (TREE_TYPE (t
)))
8123 if (flags
& tf_error
)
8125 auto_diagnostic_group d
;
8126 error_at (loc
, "temporary of non-literal type %qT in a "
8127 "constant expression", TREE_TYPE (t
));
8128 explain_non_literal_class (TREE_TYPE (t
));
8134 return RECUR (TREE_OPERAND (t
, 1), rval
);
8138 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (t
);
8139 constructor_elt
*ce
;
8140 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
8141 if (!RECUR (ce
->value
, want_rval
))
8148 gcc_assert (TREE_PURPOSE (t
) == NULL_TREE
8149 || DECL_P (TREE_PURPOSE (t
)));
8150 if (!RECUR (TREE_VALUE (t
), want_rval
))
8152 if (TREE_CHAIN (t
) == NULL_TREE
)
8154 return RECUR (TREE_CHAIN (t
), want_rval
);
8157 case TRUNC_DIV_EXPR
:
8159 case FLOOR_DIV_EXPR
:
8160 case ROUND_DIV_EXPR
:
8161 case TRUNC_MOD_EXPR
:
8163 case ROUND_MOD_EXPR
:
8165 tree denom
= TREE_OPERAND (t
, 1);
8166 if (!RECUR (denom
, rval
))
8168 /* We can't call cxx_eval_outermost_constant_expr on an expression
8169 that hasn't been through instantiate_non_dependent_expr yet. */
8170 if (!processing_template_decl
)
8171 denom
= cxx_eval_outermost_constant_expr (denom
, true);
8172 if (integer_zerop (denom
))
8174 if (flags
& tf_error
)
8175 error ("division by zero is not a constant expression");
8181 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
8187 /* check_return_expr sometimes wraps a TARGET_EXPR in a
8188 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
8189 introduced by build_call_a. */
8190 tree op0
= TREE_OPERAND (t
, 0);
8191 tree op1
= TREE_OPERAND (t
, 1);
8193 if ((TREE_CODE (op0
) == TARGET_EXPR
&& op1
== TARGET_EXPR_SLOT (op0
))
8194 || TREE_CODE (op1
) == EMPTY_CLASS_EXPR
)
8195 return RECUR (op0
, want_rval
);
8200 /* If the first operand is the non-short-circuit constant, look at
8201 the second operand; otherwise we only care about the first one for
8203 case TRUTH_AND_EXPR
:
8204 case TRUTH_ANDIF_EXPR
:
8205 tmp
= boolean_true_node
;
8208 case TRUTH_ORIF_EXPR
:
8209 tmp
= boolean_false_node
;
8212 tree op
= TREE_OPERAND (t
, 0);
8213 if (!RECUR (op
, rval
))
8215 if (!processing_template_decl
)
8216 op
= cxx_eval_outermost_constant_expr (op
, true);
8217 if (tree_int_cst_equal (op
, tmp
))
8218 return RECUR (TREE_OPERAND (t
, 1), rval
);
8225 case POINTER_PLUS_EXPR
:
8227 case EXACT_DIV_EXPR
:
8237 case TRUTH_XOR_EXPR
:
8238 case UNORDERED_EXPR
:
8251 case ARRAY_RANGE_REF
:
8255 case BINARY_LEFT_FOLD_EXPR
:
8256 case BINARY_RIGHT_FOLD_EXPR
:
8258 for (i
= 0; i
< 2; ++i
)
8259 if (!RECUR (TREE_OPERAND (t
, i
), want_rval
))
8264 for (i
= 0; i
< 3; ++i
)
8265 if (!RECUR (TREE_OPERAND (t
, i
), true))
8270 if (COND_EXPR_IS_VEC_DELETE (t
) && cxx_dialect
< cxx20
)
8272 if (flags
& tf_error
)
8273 error_at (loc
, "%<delete[]%> is not a constant expression");
8279 /* If the condition is a known constant, we know which of the legs we
8280 care about; otherwise we only require that the condition and
8281 either of the legs be potentially constant. */
8282 tmp
= TREE_OPERAND (t
, 0);
8283 if (!RECUR (tmp
, rval
))
8285 if (!processing_template_decl
)
8286 tmp
= cxx_eval_outermost_constant_expr (tmp
, true);
8287 if (integer_zerop (tmp
))
8288 return RECUR (TREE_OPERAND (t
, 2), want_rval
);
8289 else if (TREE_CODE (tmp
) == INTEGER_CST
)
8290 return RECUR (TREE_OPERAND (t
, 1), want_rval
);
8291 for (i
= 1; i
< 3; ++i
)
8292 if (potential_constant_expression_1 (TREE_OPERAND (t
, i
),
8293 want_rval
, strict
, now
,
8294 tf_none
, jump_target
))
8296 if (flags
& tf_error
)
8297 error_at (loc
, "expression %qE is not a constant expression", t
);
8301 if (VEC_INIT_EXPR_IS_CONSTEXPR (t
))
8303 if (flags
& tf_error
)
8305 error_at (loc
, "non-constant array initialization");
8306 diagnose_non_constexpr_vec_init (t
);
8312 /* We can see these in statement-expressions. */
8316 if (!RECUR (CLEANUP_BODY (t
), any
))
8318 if (!CLEANUP_EH_ONLY (t
) && !RECUR (CLEANUP_EXPR (t
), any
))
8322 case EMPTY_CLASS_EXPR
:
8327 tree
*target
= &TREE_OPERAND (t
, 0);
8328 /* Gotos representing break and continue are OK. */
8329 if (breaks (target
) || continues (target
))
8331 *jump_target
= *target
;
8334 if (flags
& tf_error
)
8335 error_at (loc
, "%<goto%> is not a constant expression");
8340 return RECUR (TREE_OPERAND (t
, 0), rval
);
8342 /* Coroutine await, yield and return expressions are not. */
8345 case CO_RETURN_EXPR
:
8349 if (objc_is_property_ref (t
))
8352 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t
)));
8360 potential_constant_expression_1 (tree t
, bool want_rval
, bool strict
, bool now
,
8361 tsubst_flags_t flags
)
8363 tree target
= NULL_TREE
;
8364 return potential_constant_expression_1 (t
, want_rval
, strict
, now
,
8368 /* The main entry point to the above. */
8371 potential_constant_expression (tree t
)
8373 return potential_constant_expression_1 (t
, false, true, false, tf_none
);
8376 /* As above, but require a constant rvalue. */
8379 potential_rvalue_constant_expression (tree t
)
8381 return potential_constant_expression_1 (t
, true, true, false, tf_none
);
8384 /* Like above, but complain about non-constant expressions. */
8387 require_potential_constant_expression (tree t
)
8389 return potential_constant_expression_1 (t
, false, true, false,
8390 tf_warning_or_error
);
8393 /* Cross product of the above. */
8396 require_potential_rvalue_constant_expression (tree t
)
8398 return potential_constant_expression_1 (t
, true, true, false,
8399 tf_warning_or_error
);
8402 /* Like above, but don't consider PARM_DECL a potential_constant_expression. */
8405 require_rvalue_constant_expression (tree t
)
8407 return potential_constant_expression_1 (t
, true, true, true,
8408 tf_warning_or_error
);
8411 /* Like potential_constant_expression, but don't consider possible constexpr
8412 substitution of the current function. That is, PARM_DECL qualifies under
8413 potential_constant_expression, but not here.
8415 This is basically what you can check when any actual constant values might
8416 be value-dependent. */
8419 is_constant_expression (tree t
)
8421 return potential_constant_expression_1 (t
, false, true, true, tf_none
);
8424 /* As above, but expect an rvalue. */
8427 is_rvalue_constant_expression (tree t
)
8429 return potential_constant_expression_1 (t
, true, true, true, tf_none
);
8432 /* Like above, but complain about non-constant expressions. */
8435 require_constant_expression (tree t
)
8437 return potential_constant_expression_1 (t
, false, true, true,
8438 tf_warning_or_error
);
8441 /* Like is_constant_expression, but allow const variables that are not allowed
8442 under constexpr rules. */
8445 is_static_init_expression (tree t
)
8447 return potential_constant_expression_1 (t
, false, false, true, tf_none
);
8450 /* Returns true if T is a potential constant expression that is not
8451 instantiation-dependent, and therefore a candidate for constant folding even
8455 is_nondependent_constant_expression (tree t
)
8457 return (!type_unknown_p (t
)
8458 && is_constant_expression (t
)
8459 && !instantiation_dependent_expression_p (t
));
8462 /* Returns true if T is a potential static initializer expression that is not
8463 instantiation-dependent. */
8466 is_nondependent_static_init_expression (tree t
)
8468 return (!type_unknown_p (t
)
8469 && is_static_init_expression (t
)
8470 && !instantiation_dependent_expression_p (t
));
8473 /* Finalize constexpr processing after parsing. */
8476 fini_constexpr (void)
8478 /* The contexpr call and fundef copies tables are no longer needed. */
8479 constexpr_call_table
= NULL
;
8480 fundef_copies_table
= NULL
;
8483 #include "gt-cp-constexpr.h"