1 /* Perform -*- C++ -*- constant expression evaluation, including calls to
2 constexpr functions. These routines are used both during actual parsing
3 and during the instantiation of template functions.
5 Copyright (C) 1998-2021 Free Software Foundation, Inc.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
14 GCC is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
25 #include "coretypes.h"
28 #include "c-family/c-objc.h"
29 #include "tree-iterator.h"
32 #include "tree-inline.h"
34 #include "gimple-fold.h"
36 #include "fold-const-call.h"
37 #include "stor-layout.h"
40 static bool verify_constant (tree
, bool, bool *, bool *);
41 #define VERIFY_CONSTANT(X) \
43 if (verify_constant ((X), ctx->quiet, non_constant_p, overflow_p)) \
47 static HOST_WIDE_INT
find_array_ctor_elt (tree ary
, tree dindex
,
49 static int array_index_cmp (tree key
, tree index
);
51 /* Returns true iff FUN is an instantiation of a constexpr function
52 template or a defaulted constexpr function. */
55 is_instantiation_of_constexpr (tree fun
)
57 return ((DECL_TEMPLOID_INSTANTIATION (fun
)
58 && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun
)))
59 || (DECL_DEFAULTED_FN (fun
)
60 && DECL_DECLARED_CONSTEXPR_P (fun
)));
63 /* Return true if T is a literal type. */
66 literal_type_p (tree t
)
71 || (VOID_TYPE_P (t
) && cxx_dialect
>= cxx14
))
75 t
= complete_type (t
);
76 gcc_assert (COMPLETE_TYPE_P (t
) || errorcount
);
77 return CLASSTYPE_LITERAL_P (t
);
79 if (TREE_CODE (t
) == ARRAY_TYPE
)
80 return literal_type_p (strip_array_types (t
));
84 /* If DECL is a variable declared `constexpr', require its type
85 be literal. Return error_mark_node if we give an error, the
89 ensure_literal_type_for_constexpr_object (tree decl
)
91 tree type
= TREE_TYPE (decl
);
93 && (DECL_DECLARED_CONSTEXPR_P (decl
)
94 || var_in_constexpr_fn (decl
))
95 && !processing_template_decl
)
97 tree stype
= strip_array_types (type
);
98 if (CLASS_TYPE_P (stype
) && !COMPLETE_TYPE_P (complete_type (stype
)))
99 /* Don't complain here, we'll complain about incompleteness
100 when we try to initialize the variable. */;
101 else if (!literal_type_p (type
))
103 if (DECL_DECLARED_CONSTEXPR_P (decl
))
105 auto_diagnostic_group d
;
106 error_at (DECL_SOURCE_LOCATION (decl
),
107 "the type %qT of %<constexpr%> variable %qD "
108 "is not literal", type
, decl
);
109 explain_non_literal_class (type
);
110 decl
= error_mark_node
;
114 if (!is_instantiation_of_constexpr (current_function_decl
))
116 auto_diagnostic_group d
;
117 error_at (DECL_SOURCE_LOCATION (decl
),
118 "variable %qD of non-literal type %qT in "
119 "%<constexpr%> function", decl
, type
);
120 explain_non_literal_class (type
);
121 decl
= error_mark_node
;
123 cp_function_chain
->invalid_constexpr
= true;
126 else if (DECL_DECLARED_CONSTEXPR_P (decl
)
127 && variably_modified_type_p (type
, NULL_TREE
))
129 error_at (DECL_SOURCE_LOCATION (decl
),
130 "%<constexpr%> variable %qD has variably-modified "
131 "type %qT", decl
, type
);
132 decl
= error_mark_node
;
138 struct constexpr_fundef_hasher
: ggc_ptr_hash
<constexpr_fundef
>
140 static hashval_t
hash (const constexpr_fundef
*);
141 static bool equal (const constexpr_fundef
*, const constexpr_fundef
*);
144 /* This table holds all constexpr function definitions seen in
145 the current translation unit. */
147 static GTY (()) hash_table
<constexpr_fundef_hasher
> *constexpr_fundef_table
;
149 /* Utility function used for managing the constexpr function table.
150 Return true if the entries pointed to by P and Q are for the
151 same constexpr function. */
154 constexpr_fundef_hasher::equal (const constexpr_fundef
*lhs
,
155 const constexpr_fundef
*rhs
)
157 return lhs
->decl
== rhs
->decl
;
160 /* Utility function used for managing the constexpr function table.
161 Return a hash value for the entry pointed to by Q. */
164 constexpr_fundef_hasher::hash (const constexpr_fundef
*fundef
)
166 return DECL_UID (fundef
->decl
);
169 /* Return a previously saved definition of function FUN. */
172 retrieve_constexpr_fundef (tree fun
)
174 if (constexpr_fundef_table
== NULL
)
177 constexpr_fundef fundef
= { fun
, NULL_TREE
, NULL_TREE
, NULL_TREE
};
178 return constexpr_fundef_table
->find (&fundef
);
181 /* Check whether the parameter and return types of FUN are valid for a
182 constexpr function, and complain if COMPLAIN. */
185 is_valid_constexpr_fn (tree fun
, bool complain
)
189 if (DECL_INHERITED_CTOR (fun
)
190 && TREE_CODE (fun
) == TEMPLATE_DECL
)
194 error ("inherited constructor %qD is not %<constexpr%>",
195 DECL_INHERITED_CTOR (fun
));
199 for (tree parm
= FUNCTION_FIRST_USER_PARM (fun
);
200 parm
!= NULL_TREE
; parm
= TREE_CHAIN (parm
))
201 if (!literal_type_p (TREE_TYPE (parm
)))
206 auto_diagnostic_group d
;
207 error ("invalid type for parameter %d of %<constexpr%> "
208 "function %q+#D", DECL_PARM_INDEX (parm
), fun
);
209 explain_non_literal_class (TREE_TYPE (parm
));
214 if (LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun
)) && cxx_dialect
< cxx17
)
218 inform (DECL_SOURCE_LOCATION (fun
),
219 "lambdas are implicitly %<constexpr%> only in C++17 and later");
221 else if (!DECL_CONSTRUCTOR_P (fun
))
223 tree rettype
= TREE_TYPE (TREE_TYPE (fun
));
224 if (!literal_type_p (rettype
))
229 auto_diagnostic_group d
;
230 error ("invalid return type %qT of %<constexpr%> function %q+D",
232 explain_non_literal_class (rettype
);
236 /* C++14 DR 1684 removed this restriction. */
237 if (cxx_dialect
< cxx14
238 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fun
)
239 && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun
)))
244 auto_diagnostic_group d
;
245 if (pedwarn (DECL_SOURCE_LOCATION (fun
), OPT_Wpedantic
,
246 "enclosing class of %<constexpr%> non-static"
247 " member function %q+#D is not a literal type",
249 explain_non_literal_class (DECL_CONTEXT (fun
));
253 else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun
)))
257 error ("%q#T has virtual base classes", DECL_CONTEXT (fun
));
263 /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF
264 for a member of an anonymous aggregate, INIT is the initializer for that
265 member, and VEC_OUTER is the vector of constructor elements for the class
266 whose constructor we are processing. Add the initializer to the vector
267 and return true to indicate success. */
270 build_anon_member_initialization (tree member
, tree init
,
271 vec
<constructor_elt
, va_gc
> **vec_outer
)
273 /* MEMBER presents the relevant fields from the inside out, but we need
274 to build up the initializer from the outside in so that we can reuse
275 previously built CONSTRUCTORs if this is, say, the second field in an
276 anonymous struct. So we use a vec as a stack. */
277 auto_vec
<tree
, 2> fields
;
280 fields
.safe_push (TREE_OPERAND (member
, 1));
281 member
= TREE_OPERAND (member
, 0);
283 while (ANON_AGGR_TYPE_P (TREE_TYPE (member
))
284 && TREE_CODE (member
) == COMPONENT_REF
);
286 /* VEC has the constructor elements vector for the context of FIELD.
287 If FIELD is an anonymous aggregate, we will push inside it. */
288 vec
<constructor_elt
, va_gc
> **vec
= vec_outer
;
290 while (field
= fields
.pop(),
291 ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
294 /* If there is already an outer constructor entry for the anonymous
295 aggregate FIELD, use it; otherwise, insert one. */
296 if (vec_safe_is_empty (*vec
)
297 || (*vec
)->last().index
!= field
)
299 ctor
= build_constructor (TREE_TYPE (field
), NULL
);
300 CONSTRUCTOR_APPEND_ELT (*vec
, field
, ctor
);
303 ctor
= (*vec
)->last().value
;
304 vec
= &CONSTRUCTOR_ELTS (ctor
);
307 /* Now we're at the innermost field, the one that isn't an anonymous
308 aggregate. Add its initializer to the CONSTRUCTOR and we're done. */
309 gcc_assert (fields
.is_empty());
310 CONSTRUCTOR_APPEND_ELT (*vec
, field
, init
);
315 /* Subroutine of build_constexpr_constructor_member_initializers.
316 The expression tree T represents a data member initialization
317 in a (constexpr) constructor definition. Build a pairing of
318 the data member with its initializer, and prepend that pair
319 to the existing initialization pair INITS. */
322 build_data_member_initialization (tree t
, vec
<constructor_elt
, va_gc
> **vec
)
325 if (TREE_CODE (t
) == CLEANUP_POINT_EXPR
)
326 t
= TREE_OPERAND (t
, 0);
327 if (TREE_CODE (t
) == EXPR_STMT
)
328 t
= TREE_OPERAND (t
, 0);
329 if (t
== error_mark_node
)
331 if (TREE_CODE (t
) == STATEMENT_LIST
)
333 for (tree stmt
: tsi_range (t
))
334 if (! build_data_member_initialization (stmt
, vec
))
338 if (TREE_CODE (t
) == CLEANUP_STMT
)
340 /* We can't see a CLEANUP_STMT in a constructor for a literal class,
341 but we can in a constexpr constructor for a non-literal class. Just
342 ignore it; either all the initialization will be constant, in which
343 case the cleanup can't run, or it can't be constexpr.
344 Still recurse into CLEANUP_BODY. */
345 return build_data_member_initialization (CLEANUP_BODY (t
), vec
);
347 if (TREE_CODE (t
) == CONVERT_EXPR
)
348 t
= TREE_OPERAND (t
, 0);
349 if (TREE_CODE (t
) == INIT_EXPR
350 /* vptr initialization shows up as a MODIFY_EXPR. In C++14 we only
351 use what this function builds for cx_check_missing_mem_inits, and
352 assignment in the ctor body doesn't count. */
353 || (cxx_dialect
< cxx14
&& TREE_CODE (t
) == MODIFY_EXPR
))
355 member
= TREE_OPERAND (t
, 0);
356 init
= break_out_target_exprs (TREE_OPERAND (t
, 1));
358 else if (TREE_CODE (t
) == CALL_EXPR
)
360 tree fn
= get_callee_fndecl (t
);
361 if (!fn
|| !DECL_CONSTRUCTOR_P (fn
))
362 /* We're only interested in calls to subobject constructors. */
364 member
= CALL_EXPR_ARG (t
, 0);
365 /* We don't use build_cplus_new here because it complains about
366 abstract bases. Leaving the call unwrapped means that it has the
367 wrong type, but cxx_eval_constant_expression doesn't care. */
368 init
= break_out_target_exprs (t
);
370 else if (TREE_CODE (t
) == BIND_EXPR
)
371 return build_data_member_initialization (BIND_EXPR_BODY (t
), vec
);
373 /* Don't add anything else to the CONSTRUCTOR. */
375 if (INDIRECT_REF_P (member
))
376 member
= TREE_OPERAND (member
, 0);
377 if (TREE_CODE (member
) == NOP_EXPR
)
381 if (TREE_CODE (op
) == ADDR_EXPR
)
383 gcc_assert (same_type_ignoring_top_level_qualifiers_p
384 (TREE_TYPE (TREE_TYPE (op
)),
385 TREE_TYPE (TREE_TYPE (member
))));
386 /* Initializing a cv-qualified member; we need to look through
390 else if (op
== current_class_ptr
391 && (same_type_ignoring_top_level_qualifiers_p
392 (TREE_TYPE (TREE_TYPE (member
)),
393 current_class_type
)))
394 /* Delegating constructor. */
398 /* This is an initializer for an empty base; keep it for now so
399 we can check it in cxx_eval_bare_aggregate. */
400 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member
))));
403 if (TREE_CODE (member
) == ADDR_EXPR
)
404 member
= TREE_OPERAND (member
, 0);
405 if (TREE_CODE (member
) == COMPONENT_REF
)
407 tree aggr
= TREE_OPERAND (member
, 0);
408 if (TREE_CODE (aggr
) == VAR_DECL
)
409 /* Initializing a local variable, don't add anything. */
411 if (TREE_CODE (aggr
) != COMPONENT_REF
)
412 /* Normal member initialization. */
413 member
= TREE_OPERAND (member
, 1);
414 else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr
)))
415 /* Initializing a member of an anonymous union. */
416 return build_anon_member_initialization (member
, init
, vec
);
418 /* We're initializing a vtable pointer in a base. Leave it as
419 COMPONENT_REF so we remember the path to get to the vfield. */
420 gcc_assert (TREE_TYPE (member
) == vtbl_ptr_type_node
);
423 /* Value-initialization can produce multiple initializers for the
424 same field; use the last one. */
425 if (!vec_safe_is_empty (*vec
) && (*vec
)->last().index
== member
)
426 (*vec
)->last().value
= init
;
428 CONSTRUCTOR_APPEND_ELT (*vec
, member
, init
);
432 /* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval.
433 In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a
434 BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */
437 check_constexpr_bind_expr_vars (tree t
)
439 gcc_assert (TREE_CODE (t
) == BIND_EXPR
);
441 for (tree var
= BIND_EXPR_VARS (t
); var
; var
= DECL_CHAIN (var
))
442 if (TREE_CODE (var
) == TYPE_DECL
443 && DECL_IMPLICIT_TYPEDEF_P (var
)
444 && !LAMBDA_TYPE_P (TREE_TYPE (var
)))
449 /* Subroutine of check_constexpr_ctor_body. */
452 check_constexpr_ctor_body_1 (tree last
, tree list
)
454 switch (TREE_CODE (list
))
457 if (TREE_CODE (DECL_EXPR_DECL (list
)) == USING_DECL
458 || TREE_CODE (DECL_EXPR_DECL (list
)) == TYPE_DECL
)
462 case CLEANUP_POINT_EXPR
:
463 return check_constexpr_ctor_body (last
, TREE_OPERAND (list
, 0),
467 if (!check_constexpr_bind_expr_vars (list
)
468 || !check_constexpr_ctor_body (last
, BIND_EXPR_BODY (list
),
475 case DEBUG_BEGIN_STMT
:
483 /* Make sure that there are no statements after LAST in the constructor
484 body represented by LIST. */
487 check_constexpr_ctor_body (tree last
, tree list
, bool complain
)
489 /* C++14 doesn't require a constexpr ctor to have an empty body. */
490 if (cxx_dialect
>= cxx14
)
494 if (TREE_CODE (list
) == STATEMENT_LIST
)
496 tree_stmt_iterator i
= tsi_last (list
);
497 for (; !tsi_end_p (i
); tsi_prev (&i
))
499 tree t
= tsi_stmt (i
);
502 if (!check_constexpr_ctor_body_1 (last
, t
))
509 else if (list
!= last
510 && !check_constexpr_ctor_body_1 (last
, list
))
515 error ("%<constexpr%> constructor does not have empty body");
516 DECL_DECLARED_CONSTEXPR_P (current_function_decl
) = false;
521 /* V is a vector of constructor elements built up for the base and member
522 initializers of a constructor for TYPE. They need to be in increasing
523 offset order, which they might not be yet if TYPE has a primary base
524 which is not first in the base-clause or a vptr and at least one base
525 all of which are non-primary. */
527 static vec
<constructor_elt
, va_gc
> *
528 sort_constexpr_mem_initializers (tree type
, vec
<constructor_elt
, va_gc
> *v
)
530 tree pri
= CLASSTYPE_PRIMARY_BINFO (type
);
536 field_type
= BINFO_TYPE (pri
);
537 else if (TYPE_CONTAINS_VPTR_P (type
))
538 field_type
= vtbl_ptr_type_node
;
542 /* Find the element for the primary base or vptr and move it to the
543 beginning of the vec. */
544 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
545 if (TREE_TYPE (ce
->index
) == field_type
)
548 if (i
> 0 && i
< vec_safe_length (v
))
550 vec
<constructor_elt
, va_gc
> &vref
= *v
;
551 constructor_elt elt
= vref
[i
];
560 /* Build compile-time evalable representations of member-initializer list
561 for a constexpr constructor. */
564 build_constexpr_constructor_member_initializers (tree type
, tree body
)
566 vec
<constructor_elt
, va_gc
> *vec
= NULL
;
569 switch (TREE_CODE (body
))
571 case MUST_NOT_THROW_EXPR
:
573 body
= TREE_OPERAND (body
, 0);
577 for (tree stmt
: tsi_range (body
))
580 if (TREE_CODE (body
) == BIND_EXPR
)
586 body
= BIND_EXPR_BODY (body
);
593 if (TREE_CODE (body
) == TRY_BLOCK
)
595 body
= TREE_OPERAND (body
, 0);
596 if (TREE_CODE (body
) == BIND_EXPR
)
597 body
= BIND_EXPR_BODY (body
);
599 if (TREE_CODE (body
) == CLEANUP_POINT_EXPR
)
601 body
= TREE_OPERAND (body
, 0);
602 if (TREE_CODE (body
) == EXPR_STMT
)
603 body
= TREE_OPERAND (body
, 0);
604 if (TREE_CODE (body
) == INIT_EXPR
605 && (same_type_ignoring_top_level_qualifiers_p
606 (TREE_TYPE (TREE_OPERAND (body
, 0)),
607 current_class_type
)))
610 return TREE_OPERAND (body
, 1);
612 ok
= build_data_member_initialization (body
, &vec
);
614 else if (TREE_CODE (body
) == STATEMENT_LIST
)
616 for (tree stmt
: tsi_range (body
))
618 ok
= build_data_member_initialization (stmt
, &vec
);
623 else if (EXPR_P (body
))
624 ok
= build_data_member_initialization (body
, &vec
);
626 gcc_assert (errorcount
> 0);
629 if (vec_safe_length (vec
) > 0)
631 /* In a delegating constructor, return the target. */
632 constructor_elt
*ce
= &(*vec
)[0];
633 if (ce
->index
== current_class_ptr
)
640 vec
= sort_constexpr_mem_initializers (type
, vec
);
641 return build_constructor (type
, vec
);
644 return error_mark_node
;
647 /* We have an expression tree T that represents a call, either CALL_EXPR
648 or AGGR_INIT_EXPR. If the call is lexically to a named function,
649 retrun the _DECL for that function. */
652 get_function_named_in_call (tree t
)
654 tree fun
= cp_get_callee (t
);
655 if (fun
&& TREE_CODE (fun
) == ADDR_EXPR
656 && TREE_CODE (TREE_OPERAND (fun
, 0)) == FUNCTION_DECL
)
657 fun
= TREE_OPERAND (fun
, 0);
661 /* Subroutine of check_constexpr_fundef. BODY is the body of a function
662 declared to be constexpr, or a sub-statement thereof. Returns the
663 return value if suitable, error_mark_node for a statement not allowed in
664 a constexpr function, or NULL_TREE if no return value was found. */
667 constexpr_fn_retval (tree body
)
669 switch (TREE_CODE (body
))
673 tree expr
= NULL_TREE
;
674 for (tree stmt
: tsi_range (body
))
676 tree s
= constexpr_fn_retval (stmt
);
677 if (s
== error_mark_node
)
678 return error_mark_node
;
679 else if (s
== NULL_TREE
)
680 /* Keep iterating. */;
682 /* Multiple return statements. */
683 return error_mark_node
;
691 return break_out_target_exprs (TREE_OPERAND (body
, 0));
695 tree decl
= DECL_EXPR_DECL (body
);
696 if (TREE_CODE (decl
) == USING_DECL
697 /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */
698 || DECL_ARTIFICIAL (decl
))
700 return error_mark_node
;
703 case CLEANUP_POINT_EXPR
:
704 return constexpr_fn_retval (TREE_OPERAND (body
, 0));
707 if (!check_constexpr_bind_expr_vars (body
))
708 return error_mark_node
;
709 return constexpr_fn_retval (BIND_EXPR_BODY (body
));
712 case DEBUG_BEGIN_STMT
:
717 tree fun
= get_function_named_in_call (body
);
719 && fndecl_built_in_p (fun
, BUILT_IN_UNREACHABLE
))
725 return error_mark_node
;
729 /* Subroutine of check_constexpr_fundef. BODY is the DECL_SAVED_TREE of
730 FUN; do the necessary transformations to turn it into a single expression
731 that we can store in the hash table. */
734 massage_constexpr_body (tree fun
, tree body
)
736 if (DECL_CONSTRUCTOR_P (fun
))
737 body
= build_constexpr_constructor_member_initializers
738 (DECL_CONTEXT (fun
), body
);
739 else if (cxx_dialect
< cxx14
)
741 if (TREE_CODE (body
) == EH_SPEC_BLOCK
)
742 body
= EH_SPEC_STMTS (body
);
743 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
744 body
= TREE_OPERAND (body
, 0);
745 body
= constexpr_fn_retval (body
);
750 /* CTYPE is a type constructed from BODY. Return true if some
751 bases/fields are uninitialized, and complain if COMPLAIN. */
754 cx_check_missing_mem_inits (tree ctype
, tree body
, bool complain
)
756 /* We allow uninitialized bases/fields in C++20. */
757 if (cxx_dialect
>= cxx20
)
764 if (TREE_CODE (body
) != CONSTRUCTOR
)
766 nelts
= CONSTRUCTOR_NELTS (body
);
768 tree field
= TYPE_FIELDS (ctype
);
770 if (TREE_CODE (ctype
) == UNION_TYPE
)
772 if (nelts
== 0 && next_initializable_field (field
))
775 error ("%<constexpr%> constructor for union %qT must "
776 "initialize exactly one non-static data member", ctype
);
782 /* Iterate over the CONSTRUCTOR, checking any missing fields don't
783 need an explicit initialization. */
785 for (unsigned i
= 0; i
<= nelts
; ++i
)
787 tree index
= NULL_TREE
;
790 index
= CONSTRUCTOR_ELT (body
, i
)->index
;
791 /* Skip base and vtable inits. */
792 if (TREE_CODE (index
) != FIELD_DECL
793 || DECL_ARTIFICIAL (index
))
797 for (; field
!= index
; field
= DECL_CHAIN (field
))
800 if (TREE_CODE (field
) != FIELD_DECL
)
802 if (DECL_UNNAMED_BIT_FIELD (field
))
804 if (DECL_ARTIFICIAL (field
))
806 if (ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
808 /* Recurse to check the anonymous aggregate member. */
809 bad
|= cx_check_missing_mem_inits
810 (TREE_TYPE (field
), NULL_TREE
, complain
);
811 if (bad
&& !complain
)
815 ftype
= TREE_TYPE (field
);
816 if (!ftype
|| !TYPE_P (ftype
) || !COMPLETE_TYPE_P (ftype
))
817 /* A flexible array can't be intialized here, so don't complain
820 if (is_empty_field (field
))
821 /* An empty field doesn't need an initializer. */
823 ftype
= strip_array_types (ftype
);
824 if (type_has_constexpr_default_constructor (ftype
))
826 /* It's OK to skip a member with a trivial constexpr ctor.
827 A constexpr ctor that isn't trivial should have been
829 gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype
)
835 auto_diagnostic_group d
;
836 error ("member %qD must be initialized by mem-initializer "
837 "in %<constexpr%> constructor", field
);
838 inform (DECL_SOURCE_LOCATION (field
), "declared here");
841 if (field
== NULL_TREE
)
844 if (ANON_AGGR_TYPE_P (TREE_TYPE (index
)))
846 /* Check the anonymous aggregate initializer is valid. */
847 bad
|= cx_check_missing_mem_inits
848 (TREE_TYPE (index
), CONSTRUCTOR_ELT (body
, i
)->value
, complain
);
849 if (bad
&& !complain
)
852 field
= DECL_CHAIN (field
);
858 /* We are processing the definition of the constexpr function FUN.
859 Check that its body fulfills the apropriate requirements and
860 enter it in the constexpr function definition table. */
863 maybe_save_constexpr_fundef (tree fun
)
865 if (processing_template_decl
866 || !DECL_DECLARED_CONSTEXPR_P (fun
)
867 || cp_function_chain
->invalid_constexpr
868 || DECL_CLONED_FUNCTION_P (fun
))
871 if (!is_valid_constexpr_fn (fun
, !DECL_GENERATED_P (fun
)))
874 tree massaged
= massage_constexpr_body (fun
, DECL_SAVED_TREE (fun
));
875 if (massaged
== NULL_TREE
|| massaged
== error_mark_node
)
877 if (!DECL_CONSTRUCTOR_P (fun
))
878 error ("body of %<constexpr%> function %qD not a return-statement",
883 bool potential
= potential_rvalue_constant_expression (massaged
);
884 if (!potential
&& !DECL_GENERATED_P (fun
))
885 require_potential_rvalue_constant_expression (massaged
);
887 if (DECL_CONSTRUCTOR_P (fun
)
888 && cx_check_missing_mem_inits (DECL_CONTEXT (fun
),
889 massaged
, !DECL_GENERATED_P (fun
)))
892 if (!potential
&& !DECL_GENERATED_P (fun
))
895 constexpr_fundef entry
= {fun
, NULL_TREE
, NULL_TREE
, NULL_TREE
};
896 bool clear_ctx
= false;
897 if (DECL_RESULT (fun
) && DECL_CONTEXT (DECL_RESULT (fun
)) == NULL_TREE
)
900 DECL_CONTEXT (DECL_RESULT (fun
)) = fun
;
902 tree saved_fn
= current_function_decl
;
903 current_function_decl
= fun
;
904 entry
.body
= copy_fn (entry
.decl
, entry
.parms
, entry
.result
);
905 current_function_decl
= saved_fn
;
907 DECL_CONTEXT (DECL_RESULT (entry
.decl
)) = NULL_TREE
;
909 /* For a template instantiation, we want to remember the pre-generic body
910 for explain_invalid_constexpr_fn, but do tell cxx_eval_call_expression
911 that it doesn't need to bother trying to expand the function. */
912 entry
.result
= error_mark_node
;
914 register_constexpr_fundef (entry
);
917 /* BODY is a validated and massaged definition of a constexpr
918 function. Register it in the hash table. */
921 register_constexpr_fundef (const constexpr_fundef
&value
)
923 /* Create the constexpr function table if necessary. */
924 if (constexpr_fundef_table
== NULL
)
925 constexpr_fundef_table
926 = hash_table
<constexpr_fundef_hasher
>::create_ggc (101);
928 constexpr_fundef
**slot
= constexpr_fundef_table
->find_slot
929 (const_cast<constexpr_fundef
*> (&value
), INSERT
);
931 gcc_assert (*slot
== NULL
);
932 *slot
= ggc_alloc
<constexpr_fundef
> ();
936 /* FUN is a non-constexpr function called in a context that requires a
937 constant expression. If it comes from a constexpr template, explain why
938 the instantiation isn't constexpr. */
941 explain_invalid_constexpr_fn (tree fun
)
943 static hash_set
<tree
> *diagnosed
;
946 /* Only diagnose defaulted functions, lambdas, or instantiations. */
947 if (!DECL_DEFAULTED_FN (fun
)
948 && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun
))
949 && !is_instantiation_of_constexpr (fun
))
951 inform (DECL_SOURCE_LOCATION (fun
), "%qD declared here", fun
);
954 if (diagnosed
== NULL
)
955 diagnosed
= new hash_set
<tree
>;
956 if (diagnosed
->add (fun
))
957 /* Already explained. */
960 save_loc
= input_location
;
961 if (!lambda_static_thunk_p (fun
))
963 /* Diagnostics should completely ignore the static thunk, so leave
964 input_location set to our caller's location. */
965 input_location
= DECL_SOURCE_LOCATION (fun
);
966 inform (input_location
,
967 "%qD is not usable as a %<constexpr%> function because:", fun
);
969 /* First check the declaration. */
970 if (is_valid_constexpr_fn (fun
, true))
972 /* Then if it's OK, the body. */
973 if (!DECL_DECLARED_CONSTEXPR_P (fun
)
974 && DECL_DEFAULTED_FN (fun
))
975 explain_implicit_non_constexpr (fun
);
978 if (constexpr_fundef
*fd
= retrieve_constexpr_fundef (fun
))
981 body
= DECL_SAVED_TREE (fun
);
982 body
= massage_constexpr_body (fun
, body
);
983 require_potential_rvalue_constant_expression (body
);
984 if (DECL_CONSTRUCTOR_P (fun
))
985 cx_check_missing_mem_inits (DECL_CONTEXT (fun
), body
, true);
988 input_location
= save_loc
;
991 /* Objects of this type represent calls to constexpr functions
992 along with the bindings of parameters to their arguments, for
993 the purpose of compile time evaluation. */
995 struct GTY((for_user
)) constexpr_call
{
996 /* Description of the constexpr function definition. */
997 constexpr_fundef
*fundef
;
998 /* Parameter bindings environment. A TREE_VEC of arguments. */
1000 /* Result of the call.
1001 NULL means the call is being evaluated.
1002 error_mark_node means that the evaluation was erroneous;
1003 otherwise, the actuall value of the call. */
1005 /* The hash of this call; we remember it here to avoid having to
1006 recalculate it when expanding the hash table. */
1008 /* Whether __builtin_is_constant_evaluated() should evaluate to true. */
1009 bool manifestly_const_eval
;
1012 struct constexpr_call_hasher
: ggc_ptr_hash
<constexpr_call
>
1014 static hashval_t
hash (constexpr_call
*);
1015 static bool equal (constexpr_call
*, constexpr_call
*);
1018 enum constexpr_switch_state
{
1019 /* Used when processing a switch for the first time by cxx_eval_switch_expr
1020 and default: label for that switch has not been seen yet. */
1021 css_default_not_seen
,
1022 /* Used when processing a switch for the first time by cxx_eval_switch_expr
1023 and default: label for that switch has been seen already. */
1025 /* Used when processing a switch for the second time by
1026 cxx_eval_switch_expr, where default: label should match. */
1027 css_default_processing
1030 /* The constexpr expansion context part which needs one instance per
1031 cxx_eval_outermost_constant_expr invocation. VALUES is a map of values of
1032 variables initialized within the expression. */
1034 struct constexpr_global_ctx
{
1035 /* Values for any temporaries or local variables within the
1036 constant-expression. */
1037 hash_map
<tree
,tree
> values
;
1038 /* Number of cxx_eval_constant_expression calls (except skipped ones,
1039 on simple constants or location wrappers) encountered during current
1040 cxx_eval_outermost_constant_expr call. */
1041 HOST_WIDE_INT constexpr_ops_count
;
1042 /* Heap VAR_DECLs created during the evaluation of the outermost constant
1044 auto_vec
<tree
, 16> heap_vars
;
1045 /* Cleanups that need to be evaluated at the end of CLEANUP_POINT_EXPR. */
1046 vec
<tree
> *cleanups
;
1047 /* Number of heap VAR_DECL deallocations. */
1048 unsigned heap_dealloc_count
;
1050 constexpr_global_ctx ()
1051 : constexpr_ops_count (0), cleanups (NULL
), heap_dealloc_count (0) {}
1054 /* The constexpr expansion context. CALL is the current function
1055 expansion, CTOR is the current aggregate initializer, OBJECT is the
1056 object being initialized by CTOR, either a VAR_DECL or a _REF. */
1058 struct constexpr_ctx
{
1059 /* The part of the context that needs to be unique to the whole
1060 cxx_eval_outermost_constant_expr invocation. */
1061 constexpr_global_ctx
*global
;
1062 /* The innermost call we're evaluating. */
1063 constexpr_call
*call
;
1064 /* SAVE_EXPRs and TARGET_EXPR_SLOT vars of TARGET_EXPRs that we've seen
1065 within the current LOOP_EXPR. NULL if we aren't inside a loop. */
1066 vec
<tree
> *save_exprs
;
1067 /* The CONSTRUCTOR we're currently building up for an aggregate
1070 /* The object we're building the CONSTRUCTOR for. */
1072 /* If inside SWITCH_EXPR. */
1073 constexpr_switch_state
*css_state
;
1074 /* The aggregate initialization context inside which this one is nested. This
1075 is used by lookup_placeholder to resolve PLACEHOLDER_EXPRs. */
1076 const constexpr_ctx
*parent
;
1078 /* Whether we should error on a non-constant expression or fail quietly.
1079 This flag needs to be here, but some of the others could move to global
1080 if they get larger than a word. */
1082 /* Whether we are strictly conforming to constant expression rules or
1083 trying harder to get a constant value. */
1085 /* Whether __builtin_is_constant_evaluated () should be true. */
1086 bool manifestly_const_eval
;
1089 /* This internal flag controls whether we should avoid doing anything during
1090 constexpr evaluation that would cause extra DECL_UID generation, such as
1091 template instantiation and function body copying. */
1093 static bool uid_sensitive_constexpr_evaluation_value
;
1095 /* An internal counter that keeps track of the number of times
1096 uid_sensitive_constexpr_evaluation_p returned true. */
1098 static unsigned uid_sensitive_constexpr_evaluation_true_counter
;
1100 /* The accessor for uid_sensitive_constexpr_evaluation_value which also
1101 increments the corresponding counter. */
1104 uid_sensitive_constexpr_evaluation_p ()
1106 if (uid_sensitive_constexpr_evaluation_value
)
1108 ++uid_sensitive_constexpr_evaluation_true_counter
;
1115 /* The default constructor for uid_sensitive_constexpr_evaluation_sentinel
1116 enables the internal flag for uid_sensitive_constexpr_evaluation_p
1117 during the lifetime of the sentinel object. Upon its destruction, the
1118 previous value of uid_sensitive_constexpr_evaluation_p is restored. */
1120 uid_sensitive_constexpr_evaluation_sentinel
1121 ::uid_sensitive_constexpr_evaluation_sentinel ()
1122 : ovr (uid_sensitive_constexpr_evaluation_value
, true)
1126 /* The default constructor for uid_sensitive_constexpr_evaluation_checker
1127 records the current number of times that uid_sensitive_constexpr_evaluation_p
1128 has been called and returned true. */
1130 uid_sensitive_constexpr_evaluation_checker
1131 ::uid_sensitive_constexpr_evaluation_checker ()
1132 : saved_counter (uid_sensitive_constexpr_evaluation_true_counter
)
1136 /* Returns true iff uid_sensitive_constexpr_evaluation_p is true, and
1137 some constexpr evaluation was restricted due to u_s_c_e_p being called
1138 and returning true during the lifetime of this checker object. */
1141 uid_sensitive_constexpr_evaluation_checker::evaluation_restricted_p () const
1143 return (uid_sensitive_constexpr_evaluation_value
1144 && saved_counter
!= uid_sensitive_constexpr_evaluation_true_counter
);
1148 /* A table of all constexpr calls that have been evaluated by the
1149 compiler in this translation unit. */
1151 static GTY (()) hash_table
<constexpr_call_hasher
> *constexpr_call_table
;
1153 static tree
cxx_eval_constant_expression (const constexpr_ctx
*, tree
,
1154 bool, bool *, bool *, tree
* = NULL
);
1156 /* Compute a hash value for a constexpr call representation. */
1159 constexpr_call_hasher::hash (constexpr_call
*info
)
1164 /* Return true if the objects pointed to by P and Q represent calls
1165 to the same constexpr function with the same arguments.
1166 Otherwise, return false. */
1169 constexpr_call_hasher::equal (constexpr_call
*lhs
, constexpr_call
*rhs
)
1173 if (lhs
->hash
!= rhs
->hash
)
1175 if (lhs
->manifestly_const_eval
!= rhs
->manifestly_const_eval
)
1177 if (!constexpr_fundef_hasher::equal (lhs
->fundef
, rhs
->fundef
))
1179 return cp_tree_equal (lhs
->bindings
, rhs
->bindings
);
1182 /* Initialize the constexpr call table, if needed. */
1185 maybe_initialize_constexpr_call_table (void)
1187 if (constexpr_call_table
== NULL
)
1188 constexpr_call_table
= hash_table
<constexpr_call_hasher
>::create_ggc (101);
1191 /* During constexpr CALL_EXPR evaluation, to avoid issues with sharing when
1192 a function happens to get called recursively, we unshare the callee
1193 function's body and evaluate this unshared copy instead of evaluating the
1196 FUNDEF_COPIES_TABLE is a per-function freelist of these unshared function
1197 copies. The underlying data structure of FUNDEF_COPIES_TABLE is a hash_map
1198 that's keyed off of the original FUNCTION_DECL and whose value is a
1199 TREE_LIST of this function's unused copies awaiting reuse.
1201 This is not GC-deletable to avoid GC affecting UID generation. */
1203 static GTY(()) decl_tree_map
*fundef_copies_table
;
1205 /* Reuse a copy or create a new unshared copy of the function FUN.
1206 Return this copy. We use a TREE_LIST whose PURPOSE is body, VALUE
1207 is parms, TYPE is result. */
1210 get_fundef_copy (constexpr_fundef
*fundef
)
1214 tree
*slot
= &(hash_map_safe_get_or_insert
<hm_ggc
>
1215 (fundef_copies_table
, fundef
->decl
, &existed
, 127));
1219 /* There is no cached function available, or in use. We can use
1220 the function directly. That the slot is now created records
1221 that this function is now in use. */
1222 copy
= build_tree_list (fundef
->body
, fundef
->parms
);
1223 TREE_TYPE (copy
) = fundef
->result
;
1225 else if (*slot
== NULL_TREE
)
1227 if (uid_sensitive_constexpr_evaluation_p ())
1230 /* We've already used the function itself, so make a copy. */
1231 copy
= build_tree_list (NULL
, NULL
);
1232 tree saved_body
= DECL_SAVED_TREE (fundef
->decl
);
1233 tree saved_parms
= DECL_ARGUMENTS (fundef
->decl
);
1234 tree saved_result
= DECL_RESULT (fundef
->decl
);
1235 tree saved_fn
= current_function_decl
;
1236 DECL_SAVED_TREE (fundef
->decl
) = fundef
->body
;
1237 DECL_ARGUMENTS (fundef
->decl
) = fundef
->parms
;
1238 DECL_RESULT (fundef
->decl
) = fundef
->result
;
1239 current_function_decl
= fundef
->decl
;
1240 TREE_PURPOSE (copy
) = copy_fn (fundef
->decl
, TREE_VALUE (copy
),
1242 current_function_decl
= saved_fn
;
1243 DECL_RESULT (fundef
->decl
) = saved_result
;
1244 DECL_ARGUMENTS (fundef
->decl
) = saved_parms
;
1245 DECL_SAVED_TREE (fundef
->decl
) = saved_body
;
1249 /* We have a cached function available. */
1251 *slot
= TREE_CHAIN (copy
);
1257 /* Save the copy COPY of function FUN for later reuse by
1258 get_fundef_copy(). By construction, there will always be an entry
1262 save_fundef_copy (tree fun
, tree copy
)
1264 tree
*slot
= fundef_copies_table
->get (fun
);
1265 TREE_CHAIN (copy
) = *slot
;
1269 /* We have an expression tree T that represents a call, either CALL_EXPR
1270 or AGGR_INIT_EXPR. Return the Nth argument. */
1273 get_nth_callarg (tree t
, int n
)
1275 switch (TREE_CODE (t
))
1278 return CALL_EXPR_ARG (t
, n
);
1280 case AGGR_INIT_EXPR
:
1281 return AGGR_INIT_EXPR_ARG (t
, n
);
1289 /* Attempt to evaluate T which represents a call to a builtin function.
1290 We assume here that all builtin functions evaluate to scalar types
1291 represented by _CST nodes. */
1294 cxx_eval_builtin_function_call (const constexpr_ctx
*ctx
, tree t
, tree fun
,
1296 bool *non_constant_p
, bool *overflow_p
)
1298 const int nargs
= call_expr_nargs (t
);
1299 tree
*args
= (tree
*) alloca (nargs
* sizeof (tree
));
1303 /* Don't fold __builtin_constant_p within a constexpr function. */
1304 bool bi_const_p
= DECL_IS_BUILTIN_CONSTANT_P (fun
);
1306 /* If we aren't requiring a constant expression, defer __builtin_constant_p
1307 in a constexpr function until we have values for the parameters. */
1309 && !ctx
->manifestly_const_eval
1310 && current_function_decl
1311 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
1313 *non_constant_p
= true;
1317 /* For __builtin_is_constant_evaluated, defer it if not
1318 ctx->manifestly_const_eval (as sometimes we try to constant evaluate
1319 without manifestly_const_eval even expressions or parts thereof which
1320 will later be manifestly const_eval evaluated), otherwise fold it to
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
))
1335 temp_override
<tree
> ovr (current_function_decl
);
1336 if (ctx
->call
&& ctx
->call
->fundef
)
1337 current_function_decl
= ctx
->call
->fundef
->decl
;
1338 return fold_builtin_source_location (EXPR_LOCATION (t
));
1343 if (fndecl_built_in_p (fun
, BUILT_IN_NORMAL
))
1344 switch (DECL_FUNCTION_CODE (fun
))
1346 case BUILT_IN_STRLEN
:
1347 case BUILT_IN_STRNLEN
:
1350 case BUILT_IN_MEMCHR
:
1351 case BUILT_IN_STRCHR
:
1352 case BUILT_IN_STRRCHR
:
1356 case BUILT_IN_MEMCMP
:
1357 case BUILT_IN_STRCMP
:
1360 case BUILT_IN_STRSTR
:
1364 case BUILT_IN_ASAN_POINTER_COMPARE
:
1365 case BUILT_IN_ASAN_POINTER_SUBTRACT
:
1366 /* These builtins shall be ignored during constant expression
1373 /* Be permissive for arguments to built-ins; __builtin_constant_p should
1374 return constant false for a non-constant argument. */
1375 constexpr_ctx new_ctx
= *ctx
;
1376 new_ctx
.quiet
= true;
1377 for (i
= 0; i
< nargs
; ++i
)
1379 tree arg
= CALL_EXPR_ARG (t
, i
);
1382 /* To handle string built-ins we need to pass ADDR_EXPR<STRING_CST> since
1383 expand_builtin doesn't know how to look in the values table. */
1384 bool strop
= i
< strops
;
1388 if (TREE_CODE (arg
) == ADDR_EXPR
)
1389 arg
= TREE_OPERAND (arg
, 0);
1394 /* If builtin_valid_in_constant_expr_p is true,
1395 potential_constant_expression_1 has not recursed into the arguments
1396 of the builtin, verify it here. */
1397 if (!builtin_valid_in_constant_expr_p (fun
)
1398 || potential_constant_expression (arg
))
1400 bool dummy1
= false, dummy2
= false;
1401 arg
= cxx_eval_constant_expression (&new_ctx
, arg
, false,
1406 /* For __builtin_constant_p, fold all expressions with constant values
1407 even if they aren't C++ constant-expressions. */
1408 arg
= cp_fold_rvalue (arg
);
1411 if (TREE_CODE (arg
) == CONSTRUCTOR
)
1412 arg
= braced_lists_to_strings (TREE_TYPE (arg
), arg
);
1413 if (TREE_CODE (arg
) == STRING_CST
)
1414 arg
= build_address (arg
);
1422 bool save_ffbcp
= force_folding_builtin_constant_p
;
1423 force_folding_builtin_constant_p
|= ctx
->manifestly_const_eval
;
1424 tree save_cur_fn
= current_function_decl
;
1425 /* Return name of ctx->call->fundef->decl for __builtin_FUNCTION (). */
1426 if (fndecl_built_in_p (fun
, BUILT_IN_FUNCTION
)
1428 && ctx
->call
->fundef
)
1429 current_function_decl
= ctx
->call
->fundef
->decl
;
1430 new_call
= fold_builtin_call_array (EXPR_LOCATION (t
), TREE_TYPE (t
),
1431 CALL_EXPR_FN (t
), nargs
, args
);
1432 current_function_decl
= save_cur_fn
;
1433 force_folding_builtin_constant_p
= save_ffbcp
;
1434 if (new_call
== NULL
)
1436 if (!*non_constant_p
&& !ctx
->quiet
)
1438 /* Do not allow__builtin_unreachable in constexpr function.
1439 The __builtin_unreachable call with BUILTINS_LOCATION
1440 comes from cp_maybe_instrument_return. */
1441 if (fndecl_built_in_p (fun
, BUILT_IN_UNREACHABLE
)
1442 && EXPR_LOCATION (t
) == BUILTINS_LOCATION
)
1443 error ("%<constexpr%> call flows off the end of the function");
1446 new_call
= build_call_array_loc (EXPR_LOCATION (t
), TREE_TYPE (t
),
1447 CALL_EXPR_FN (t
), nargs
, args
);
1448 error ("%q+E is not a constant expression", new_call
);
1451 *non_constant_p
= true;
1455 if (!potential_constant_expression (new_call
))
1457 if (!*non_constant_p
&& !ctx
->quiet
)
1458 error ("%q+E is not a constant expression", new_call
);
1459 *non_constant_p
= true;
1465 /* memchr returns a pointer into the first argument, but we replaced the
1466 argument above with a STRING_CST; put it back it now. */
1467 tree op
= CALL_EXPR_ARG (t
, strret
-1);
1468 STRIP_NOPS (new_call
);
1469 if (TREE_CODE (new_call
) == POINTER_PLUS_EXPR
)
1470 TREE_OPERAND (new_call
, 0) = op
;
1471 else if (TREE_CODE (new_call
) == ADDR_EXPR
)
1475 return cxx_eval_constant_expression (&new_ctx
, new_call
, lval
,
1476 non_constant_p
, overflow_p
);
1479 /* TEMP is the constant value of a temporary object of type TYPE. Adjust
1480 the type of the value to match. */
1483 adjust_temp_type (tree type
, tree temp
)
1485 if (same_type_p (TREE_TYPE (temp
), type
))
1487 /* Avoid wrapping an aggregate value in a NOP_EXPR. */
1488 if (TREE_CODE (temp
) == CONSTRUCTOR
)
1490 /* build_constructor wouldn't retain various CONSTRUCTOR flags. */
1491 tree t
= copy_node (temp
);
1492 TREE_TYPE (t
) = type
;
1495 if (TREE_CODE (temp
) == EMPTY_CLASS_EXPR
)
1496 return build0 (EMPTY_CLASS_EXPR
, type
);
1497 gcc_assert (scalarish_type_p (type
));
1498 /* Now we know we're dealing with a scalar, and a prvalue of non-class
1499 type is cv-unqualified. */
1500 return cp_fold_convert (cv_unqualified (type
), temp
);
1503 /* If T is a CONSTRUCTOR, return an unshared copy of T and any
1504 sub-CONSTRUCTORs. Otherwise return T.
1506 We use this whenever we initialize an object as a whole, whether it's a
1507 parameter, a local variable, or a subobject, so that subsequent
1508 modifications don't affect other places where it was used. */
1511 unshare_constructor (tree t MEM_STAT_DECL
)
1513 if (!t
|| TREE_CODE (t
) != CONSTRUCTOR
)
1515 auto_vec
<tree
*, 4> ptrs
;
1516 ptrs
.safe_push (&t
);
1517 while (!ptrs
.is_empty ())
1519 tree
*p
= ptrs
.pop ();
1520 tree n
= copy_node (*p PASS_MEM_STAT
);
1521 CONSTRUCTOR_ELTS (n
) = vec_safe_copy (CONSTRUCTOR_ELTS (*p
) PASS_MEM_STAT
);
1523 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (n
);
1524 constructor_elt
*ce
;
1525 for (HOST_WIDE_INT i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
1526 if (ce
->value
&& TREE_CODE (ce
->value
) == CONSTRUCTOR
)
1527 ptrs
.safe_push (&ce
->value
);
1532 /* If T is a CONSTRUCTOR, ggc_free T and any sub-CONSTRUCTORs. */
1535 free_constructor (tree t
)
1537 if (!t
|| TREE_CODE (t
) != CONSTRUCTOR
)
1539 releasing_vec ctors
;
1540 vec_safe_push (ctors
, t
);
1541 while (!ctors
->is_empty ())
1543 tree c
= ctors
->pop ();
1544 if (vec
<constructor_elt
, va_gc
> *elts
= CONSTRUCTOR_ELTS (c
))
1546 constructor_elt
*ce
;
1547 for (HOST_WIDE_INT i
= 0; vec_safe_iterate (elts
, i
, &ce
); ++i
)
1548 if (TREE_CODE (ce
->value
) == CONSTRUCTOR
)
1549 vec_safe_push (ctors
, ce
->value
);
1556 /* Helper function of cxx_bind_parameters_in_call. Return non-NULL
1557 if *TP is address of a static variable (or part of it) currently being
1558 constructed or of a heap artificial variable. */
1561 addr_of_non_const_var (tree
*tp
, int *walk_subtrees
, void *data
)
1563 if (TREE_CODE (*tp
) == ADDR_EXPR
)
1564 if (tree var
= get_base_address (TREE_OPERAND (*tp
, 0)))
1565 if (VAR_P (var
) && TREE_STATIC (var
))
1567 if (DECL_NAME (var
) == heap_uninit_identifier
1568 || DECL_NAME (var
) == heap_identifier
1569 || DECL_NAME (var
) == heap_vec_uninit_identifier
1570 || DECL_NAME (var
) == heap_vec_identifier
)
1573 constexpr_global_ctx
*global
= (constexpr_global_ctx
*) data
;
1574 if (global
->values
.get (var
))
1578 *walk_subtrees
= false;
1582 /* Subroutine of cxx_eval_call_expression.
1583 We are processing a call expression (either CALL_EXPR or
1584 AGGR_INIT_EXPR) in the context of CTX. Evaluate
1585 all arguments and bind their values to correspondings
1586 parameters, making up the NEW_CALL context. */
1589 cxx_bind_parameters_in_call (const constexpr_ctx
*ctx
, tree t
,
1590 constexpr_call
*new_call
,
1591 bool *non_constant_p
, bool *overflow_p
,
1592 bool *non_constant_args
)
1594 const int nargs
= call_expr_nargs (t
);
1595 tree fun
= new_call
->fundef
->decl
;
1596 tree parms
= new_call
->fundef
->parms
;
1598 /* We don't record ellipsis args below. */
1599 int nparms
= list_length (parms
);
1600 int nbinds
= nargs
< nparms
? nargs
: nparms
;
1601 tree binds
= new_call
->bindings
= make_tree_vec (nbinds
);
1602 for (i
= 0; i
< nargs
; ++i
)
1605 tree type
= parms
? TREE_TYPE (parms
) : void_type_node
;
1606 x
= get_nth_callarg (t
, i
);
1607 /* For member function, the first argument is a pointer to the implied
1608 object. For a constructor, it might still be a dummy object, in
1609 which case we get the real argument from ctx. */
1610 if (i
== 0 && DECL_CONSTRUCTOR_P (fun
)
1611 && is_dummy_object (x
))
1614 x
= build_address (x
);
1616 if (TREE_ADDRESSABLE (type
))
1617 /* Undo convert_for_arg_passing work here. */
1618 x
= convert_from_reference (x
);
1619 /* Normally we would strip a TARGET_EXPR in an initialization context
1620 such as this, but here we do the elision differently: we keep the
1621 TARGET_EXPR, and use its CONSTRUCTOR as the value of the parm. */
1622 arg
= cxx_eval_constant_expression (ctx
, x
, /*lval=*/false,
1623 non_constant_p
, overflow_p
);
1624 /* Don't VERIFY_CONSTANT here. */
1625 if (*non_constant_p
&& ctx
->quiet
)
1627 /* Just discard ellipsis args after checking their constantitude. */
1631 if (!*non_constant_p
)
1633 /* Make sure the binding has the same type as the parm. But
1634 only for constant args. */
1635 if (!TYPE_REF_P (type
))
1636 arg
= adjust_temp_type (type
, arg
);
1637 if (!TREE_CONSTANT (arg
))
1638 *non_constant_args
= true;
1639 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
1640 /* The destructor needs to see any modifications the callee makes
1642 *non_constant_args
= true;
1643 /* If arg is or contains address of a heap artificial variable or
1644 of a static variable being constructed, avoid caching the
1645 function call, as those variables might be modified by the
1646 function, or might be modified by the callers in between
1647 the cached function and just read by the function. */
1648 else if (!*non_constant_args
1649 && cp_walk_tree (&arg
, addr_of_non_const_var
, ctx
->global
,
1651 *non_constant_args
= true;
1653 /* For virtual calls, adjust the this argument, so that it is
1654 the object on which the method is called, rather than
1655 one of its bases. */
1656 if (i
== 0 && DECL_VIRTUAL_P (fun
))
1660 if (TREE_CODE (addr
) == ADDR_EXPR
)
1662 tree obj
= TREE_OPERAND (addr
, 0);
1663 while (TREE_CODE (obj
) == COMPONENT_REF
1664 && DECL_FIELD_IS_BASE (TREE_OPERAND (obj
, 1))
1665 && !same_type_ignoring_top_level_qualifiers_p
1666 (TREE_TYPE (obj
), DECL_CONTEXT (fun
)))
1667 obj
= TREE_OPERAND (obj
, 0);
1668 if (obj
!= TREE_OPERAND (addr
, 0))
1669 arg
= build_fold_addr_expr_with_type (obj
,
1673 TREE_VEC_ELT (binds
, i
) = arg
;
1675 parms
= TREE_CHAIN (parms
);
1679 /* Variables and functions to manage constexpr call expansion context.
1680 These do not need to be marked for PCH or GC. */
1682 /* FIXME remember and print actual constant arguments. */
1683 static vec
<tree
> call_stack
;
1684 static int call_stack_tick
;
1685 static int last_cx_error_tick
;
1688 push_cx_call_context (tree call
)
1691 if (!EXPR_HAS_LOCATION (call
))
1692 SET_EXPR_LOCATION (call
, input_location
);
1693 call_stack
.safe_push (call
);
1694 int len
= call_stack
.length ();
1695 if (len
> max_constexpr_depth
)
1701 pop_cx_call_context (void)
1708 cx_error_context (void)
1710 vec
<tree
> r
= vNULL
;
1711 if (call_stack_tick
!= last_cx_error_tick
1712 && !call_stack
.is_empty ())
1714 last_cx_error_tick
= call_stack_tick
;
1718 /* Evaluate a call T to a GCC internal function when possible and return
1719 the evaluated result or, under the control of CTX, give an error, set
1720 NON_CONSTANT_P, and return the unevaluated call T otherwise. */
1723 cxx_eval_internal_function (const constexpr_ctx
*ctx
, tree t
,
1725 bool *non_constant_p
, bool *overflow_p
)
1727 enum tree_code opcode
= ERROR_MARK
;
1729 switch (CALL_EXPR_IFN (t
))
1731 case IFN_UBSAN_NULL
:
1732 case IFN_UBSAN_BOUNDS
:
1733 case IFN_UBSAN_VPTR
:
1734 case IFN_FALLTHROUGH
:
1737 case IFN_ADD_OVERFLOW
:
1740 case IFN_SUB_OVERFLOW
:
1741 opcode
= MINUS_EXPR
;
1743 case IFN_MUL_OVERFLOW
:
1748 return cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 0),
1749 false, non_constant_p
, overflow_p
);
1751 case IFN_VEC_CONVERT
:
1753 tree arg
= cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 0),
1754 false, non_constant_p
,
1756 if (TREE_CODE (arg
) == VECTOR_CST
)
1757 return fold_const_call (CFN_VEC_CONVERT
, TREE_TYPE (t
), arg
);
1760 *non_constant_p
= true;
1767 error_at (cp_expr_loc_or_input_loc (t
),
1768 "call to internal function %qE", t
);
1769 *non_constant_p
= true;
1773 /* Evaluate constant arguments using OPCODE and return a complex
1774 number containing the result and the overflow bit. */
1775 tree arg0
= cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 0), lval
,
1776 non_constant_p
, overflow_p
);
1777 tree arg1
= cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 1), lval
,
1778 non_constant_p
, overflow_p
);
1780 if (TREE_CODE (arg0
) == INTEGER_CST
&& TREE_CODE (arg1
) == INTEGER_CST
)
1782 location_t loc
= cp_expr_loc_or_input_loc (t
);
1783 tree type
= TREE_TYPE (TREE_TYPE (t
));
1784 tree result
= fold_binary_loc (loc
, opcode
, type
,
1785 fold_convert_loc (loc
, type
, arg0
),
1786 fold_convert_loc (loc
, type
, arg1
));
1788 = build_int_cst (type
, arith_overflowed_p (opcode
, type
, arg0
, arg1
));
1789 /* Reset TREE_OVERFLOW to avoid warnings for the overflow. */
1790 if (TREE_OVERFLOW (result
))
1791 TREE_OVERFLOW (result
) = 0;
1793 return build_complex (TREE_TYPE (t
), result
, ovf
);
1796 *non_constant_p
= true;
1800 /* Clean CONSTRUCTOR_NO_CLEARING from CTOR and its sub-aggregates. */
1803 clear_no_implicit_zero (tree ctor
)
1805 if (CONSTRUCTOR_NO_CLEARING (ctor
))
1807 CONSTRUCTOR_NO_CLEARING (ctor
) = false;
1808 tree elt
; unsigned HOST_WIDE_INT idx
;
1809 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor
), idx
, elt
)
1810 if (TREE_CODE (elt
) == CONSTRUCTOR
)
1811 clear_no_implicit_zero (elt
);
1815 /* Complain about a const object OBJ being modified in a constant expression.
1816 EXPR is the MODIFY_EXPR expression performing the modification. */
1819 modifying_const_object_error (tree expr
, tree obj
)
1821 location_t loc
= cp_expr_loc_or_input_loc (expr
);
1822 auto_diagnostic_group d
;
1823 error_at (loc
, "modifying a const object %qE is not allowed in "
1824 "a constant expression", TREE_OPERAND (expr
, 0));
1825 inform (location_of (obj
), "originally declared %<const%> here");
1828 /* Return true if FNDECL is a replaceable global allocation function that
1829 should be useable during constant expression evaluation. */
1832 cxx_replaceable_global_alloc_fn (tree fndecl
)
1834 return (cxx_dialect
>= cxx20
1835 && IDENTIFIER_NEWDEL_OP_P (DECL_NAME (fndecl
))
1836 && CP_DECL_CONTEXT (fndecl
) == global_namespace
1837 && (DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl
)
1838 || DECL_IS_OPERATOR_DELETE_P (fndecl
)));
1841 /* Return true if FNDECL is a placement new function that should be
1842 useable during constant expression evaluation of std::construct_at. */
1845 cxx_placement_new_fn (tree fndecl
)
1847 if (cxx_dialect
>= cxx20
1848 && IDENTIFIER_NEW_OP_P (DECL_NAME (fndecl
))
1849 && CP_DECL_CONTEXT (fndecl
) == global_namespace
1850 && !DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl
)
1851 && TREE_CODE (TREE_TYPE (fndecl
)) == FUNCTION_TYPE
)
1853 tree first_arg
= TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
1854 if (TREE_VALUE (first_arg
) == ptr_type_node
1855 && TREE_CHAIN (first_arg
) == void_list_node
)
1861 /* Return true if FNDECL is std::construct_at. */
1864 is_std_construct_at (tree fndecl
)
1866 if (!decl_in_std_namespace_p (fndecl
))
1869 tree name
= DECL_NAME (fndecl
);
1870 return name
&& id_equal (name
, "construct_at");
1873 /* Overload for the above taking constexpr_call*. */
1876 is_std_construct_at (const constexpr_call
*call
)
1880 && is_std_construct_at (call
->fundef
->decl
));
1883 /* Return true if FNDECL is std::allocator<T>::{,de}allocate. */
1886 is_std_allocator_allocate (tree fndecl
)
1888 tree name
= DECL_NAME (fndecl
);
1889 if (name
== NULL_TREE
1890 || !(id_equal (name
, "allocate") || id_equal (name
, "deallocate")))
1893 tree ctx
= DECL_CONTEXT (fndecl
);
1894 if (ctx
== NULL_TREE
|| !CLASS_TYPE_P (ctx
) || !TYPE_MAIN_DECL (ctx
))
1897 tree decl
= TYPE_MAIN_DECL (ctx
);
1898 name
= DECL_NAME (decl
);
1899 if (name
== NULL_TREE
|| !id_equal (name
, "allocator"))
1902 return decl_in_std_namespace_p (decl
);
1905 /* Overload for the above taking constexpr_call*. */
1908 is_std_allocator_allocate (const constexpr_call
*call
)
1912 && is_std_allocator_allocate (call
->fundef
->decl
));
1915 /* Return true if FNDECL is __dynamic_cast. */
1918 cxx_dynamic_cast_fn_p (tree fndecl
)
1920 return (cxx_dialect
>= cxx20
1921 && id_equal (DECL_NAME (fndecl
), "__dynamic_cast")
1922 && CP_DECL_CONTEXT (fndecl
) == global_namespace
);
1925 /* Often, we have an expression in the form of address + offset, e.g.
1926 "&_ZTV1A + 16". Extract the object from it, i.e. "_ZTV1A". */
1929 extract_obj_from_addr_offset (tree expr
)
1931 if (TREE_CODE (expr
) == POINTER_PLUS_EXPR
)
1932 expr
= TREE_OPERAND (expr
, 0);
1934 if (TREE_CODE (expr
) == ADDR_EXPR
)
1935 expr
= TREE_OPERAND (expr
, 0);
1939 /* Given a PATH like
1941 g.D.2181.D.2154.D.2102.D.2093
1943 find a component with type TYPE. Return NULL_TREE if not found, and
1944 error_mark_node if the component is not accessible. If STOP is non-null,
1945 this function will return NULL_TREE if STOP is found before TYPE. */
1948 get_component_with_type (tree path
, tree type
, tree stop
)
1952 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path
), type
))
1956 && (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path
),
1959 else if (TREE_CODE (path
) == COMPONENT_REF
1960 && DECL_FIELD_IS_BASE (TREE_OPERAND (path
, 1)))
1962 /* We need to check that the component we're accessing is in fact
1964 if (TREE_PRIVATE (TREE_OPERAND (path
, 1))
1965 || TREE_PROTECTED (TREE_OPERAND (path
, 1)))
1966 return error_mark_node
;
1967 path
= TREE_OPERAND (path
, 0);
1974 /* Evaluate a call to __dynamic_cast (permitted by P1327R1).
1976 The declaration of __dynamic_cast is:
1978 void* __dynamic_cast (const void* __src_ptr,
1979 const __class_type_info* __src_type,
1980 const __class_type_info* __dst_type,
1981 ptrdiff_t __src2dst);
1983 where src2dst has the following possible values
1985 >-1: src_type is a unique public non-virtual base of dst_type
1986 dst_ptr + src2dst == src_ptr
1987 -1: unspecified relationship
1988 -2: src_type is not a public base of dst_type
1989 -3: src_type is a multiple public non-virtual base of dst_type
1991 Since literal types can't have virtual bases, we only expect hint >=0,
1995 cxx_eval_dynamic_cast_fn (const constexpr_ctx
*ctx
, tree call
,
1996 bool *non_constant_p
, bool *overflow_p
)
1998 /* T will be something like
1999 __dynamic_cast ((B*) b, &_ZTI1B, &_ZTI1D, 8)
2001 gcc_assert (call_expr_nargs (call
) == 4);
2002 tsubst_flags_t complain
= ctx
->quiet
? tf_none
: tf_warning_or_error
;
2003 tree obj
= CALL_EXPR_ARG (call
, 0);
2004 tree type
= CALL_EXPR_ARG (call
, 2);
2005 HOST_WIDE_INT hint
= int_cst_value (CALL_EXPR_ARG (call
, 3));
2006 location_t loc
= cp_expr_loc_or_input_loc (call
);
2008 /* Get the target type of the dynamic_cast. */
2009 gcc_assert (TREE_CODE (type
) == ADDR_EXPR
);
2010 type
= TREE_OPERAND (type
, 0);
2011 type
= TREE_TYPE (DECL_NAME (type
));
2013 /* TYPE can only be either T* or T&. We can't know which of these it
2014 is by looking at TYPE, but OBJ will be "(T*) x" in the first case,
2015 and something like "(T*)(T&)(T*) x" in the second case. */
2016 bool reference_p
= false;
2017 while (CONVERT_EXPR_P (obj
) || TREE_CODE (obj
) == SAVE_EXPR
)
2019 reference_p
|= TYPE_REF_P (TREE_TYPE (obj
));
2020 obj
= TREE_OPERAND (obj
, 0);
2023 /* Evaluate the object so that we know its dynamic type. */
2024 obj
= cxx_eval_constant_expression (ctx
, obj
, /*lval*/false, non_constant_p
,
2026 if (*non_constant_p
)
2029 /* We expect OBJ to be in form of &d.D.2102 when HINT == 0,
2030 but when HINT is > 0, it can also be something like
2031 &d.D.2102 + 18446744073709551608, which includes the BINFO_OFFSET. */
2032 obj
= extract_obj_from_addr_offset (obj
);
2033 const tree objtype
= TREE_TYPE (obj
);
2034 /* If OBJ doesn't refer to a base field, we're done. */
2035 if (tree t
= (TREE_CODE (obj
) == COMPONENT_REF
2036 ? TREE_OPERAND (obj
, 1) : obj
))
2037 if (TREE_CODE (t
) != FIELD_DECL
|| !DECL_FIELD_IS_BASE (t
))
2043 error_at (loc
, "reference %<dynamic_cast%> failed");
2044 inform (loc
, "dynamic type %qT of its operand does "
2045 "not have a base class of type %qT",
2048 *non_constant_p
= true;
2050 return integer_zero_node
;
2053 /* [class.cdtor] When a dynamic_cast is used in a constructor ...
2054 or in a destructor ... if the operand of the dynamic_cast refers
2055 to the object under construction or destruction, this object is
2056 considered to be a most derived object that has the type of the
2057 constructor or destructor's class. */
2058 tree vtable
= build_vfield_ref (obj
, objtype
);
2059 vtable
= cxx_eval_constant_expression (ctx
, vtable
, /*lval*/false,
2060 non_constant_p
, overflow_p
);
2061 if (*non_constant_p
)
2063 /* With -fsanitize=vptr, we initialize all vtable pointers to null,
2064 so it's possible that we got a null pointer now. */
2065 if (integer_zerop (vtable
))
2068 error_at (loc
, "virtual table pointer is used uninitialized");
2069 *non_constant_p
= true;
2070 return integer_zero_node
;
2072 /* VTABLE will be &_ZTV1A + 16 or similar, get _ZTV1A. */
2073 vtable
= extract_obj_from_addr_offset (vtable
);
2074 const tree mdtype
= DECL_CONTEXT (vtable
);
2076 /* Given dynamic_cast<T>(v),
2078 [expr.dynamic.cast] If C is the class type to which T points or refers,
2079 the runtime check logically executes as follows:
2081 If, in the most derived object pointed (referred) to by v, v points
2082 (refers) to a public base class subobject of a C object, and if only
2083 one object of type C is derived from the subobject pointed (referred)
2084 to by v the result points (refers) to that C object.
2086 In this case, HINT >= 0 or -3. */
2087 if (hint
>= 0 || hint
== -3)
2089 /* Look for a component with type TYPE. */
2090 tree t
= get_component_with_type (obj
, type
, mdtype
);
2091 /* If not accessible, give an error. */
2092 if (t
== error_mark_node
)
2098 error_at (loc
, "reference %<dynamic_cast%> failed");
2099 inform (loc
, "static type %qT of its operand is a "
2100 "non-public base class of dynamic type %qT",
2104 *non_constant_p
= true;
2106 return integer_zero_node
;
2109 /* The result points to the TYPE object. */
2110 return cp_build_addr_expr (t
, complain
);
2111 /* Else, TYPE was not found, because the HINT turned out to be wrong.
2112 Fall through to the normal processing. */
2115 /* Otherwise, if v points (refers) to a public base class subobject of the
2116 most derived object, and the type of the most derived object has a base
2117 class, of type C, that is unambiguous and public, the result points
2118 (refers) to the C subobject of the most derived object.
2120 But it can also be an invalid case. */
2122 /* Get the most derived object. */
2123 obj
= get_component_with_type (obj
, mdtype
, NULL_TREE
);
2124 if (obj
== error_mark_node
)
2130 error_at (loc
, "reference %<dynamic_cast%> failed");
2131 inform (loc
, "static type %qT of its operand is a non-public"
2132 " base class of dynamic type %qT", objtype
, mdtype
);
2134 *non_constant_p
= true;
2136 return integer_zero_node
;
2141 /* Check that the type of the most derived object has a base class
2142 of type TYPE that is unambiguous and public. */
2144 tree binfo
= lookup_base (mdtype
, type
, ba_check
, &b_kind
, tf_none
);
2145 if (!binfo
|| binfo
== error_mark_node
)
2151 error_at (loc
, "reference %<dynamic_cast%> failed");
2152 if (b_kind
== bk_ambig
)
2153 inform (loc
, "%qT is an ambiguous base class of dynamic "
2154 "type %qT of its operand", type
, mdtype
);
2156 inform (loc
, "dynamic type %qT of its operand does not "
2157 "have an unambiguous public base class %qT",
2160 *non_constant_p
= true;
2162 return integer_zero_node
;
2164 /* If so, return the TYPE subobject of the most derived object. */
2165 obj
= convert_to_base_statically (obj
, binfo
);
2166 return cp_build_addr_expr (obj
, complain
);
2169 /* Data structure used by replace_result_decl and replace_result_decl_r. */
2171 struct replace_result_decl_data
2173 /* The RESULT_DECL we want to replace. */
2175 /* The replacement for DECL. */
2177 /* Whether we've performed any replacements. */
2181 /* Helper function for replace_result_decl, called through cp_walk_tree. */
2184 replace_result_decl_r (tree
*tp
, int *walk_subtrees
, void *data
)
2186 replace_result_decl_data
*d
= (replace_result_decl_data
*) data
;
2190 *tp
= unshare_expr (d
->replacement
);
2194 else if (TYPE_P (*tp
))
2200 /* Replace every occurrence of DECL, a RESULT_DECL, with (an unshared copy of)
2201 REPLACEMENT within the reduced constant expression *TP. Returns true iff a
2202 replacement was performed. */
2205 replace_result_decl (tree
*tp
, tree decl
, tree replacement
)
2207 gcc_checking_assert (TREE_CODE (decl
) == RESULT_DECL
2208 && (same_type_ignoring_top_level_qualifiers_p
2209 (TREE_TYPE (decl
), TREE_TYPE (replacement
))));
2210 replace_result_decl_data data
= { decl
, replacement
, false };
2211 cp_walk_tree_without_duplicates (tp
, replace_result_decl_r
, &data
);
2212 return data
.changed
;
2215 /* Evaluate the call T to virtual function thunk THUNK_FNDECL. */
2218 cxx_eval_thunk_call (const constexpr_ctx
*ctx
, tree t
, tree thunk_fndecl
,
2220 bool *non_constant_p
, bool *overflow_p
)
2222 tree function
= THUNK_TARGET (thunk_fndecl
);
2224 /* virtual_offset is only set in the presence of virtual bases, which make
2225 the class non-literal, so we don't need to handle it here. */
2226 if (THUNK_VIRTUAL_OFFSET (thunk_fndecl
))
2228 gcc_assert (!DECL_DECLARED_CONSTEXPR_P (function
));
2231 error ("call to non-%<constexpr%> function %qD", function
);
2232 explain_invalid_constexpr_fn (function
);
2234 *non_constant_p
= true;
2238 tree new_call
= copy_node (t
);
2239 CALL_EXPR_FN (new_call
) = function
;
2240 TREE_TYPE (new_call
) = TREE_TYPE (TREE_TYPE (function
));
2242 tree offset
= size_int (THUNK_FIXED_OFFSET (thunk_fndecl
));
2244 if (DECL_THIS_THUNK_P (thunk_fndecl
))
2246 /* 'this'-adjusting thunk. */
2247 tree this_arg
= CALL_EXPR_ARG (t
, 0);
2248 this_arg
= build2 (POINTER_PLUS_EXPR
, TREE_TYPE (this_arg
),
2250 CALL_EXPR_ARG (new_call
, 0) = this_arg
;
2253 /* Return-adjusting thunk. */
2254 new_call
= build2 (POINTER_PLUS_EXPR
, TREE_TYPE (new_call
),
2257 return cxx_eval_constant_expression (ctx
, new_call
, lval
,
2258 non_constant_p
, overflow_p
);
2261 /* If OBJECT is of const class type, evaluate it to a CONSTRUCTOR and set
2262 its TREE_READONLY flag according to READONLY_P. Used for constexpr
2263 'tors to detect modifying const objects in a constexpr context. */
2266 cxx_set_object_constness (const constexpr_ctx
*ctx
, tree object
,
2267 bool readonly_p
, bool *non_constant_p
,
2270 if (CLASS_TYPE_P (TREE_TYPE (object
))
2271 && CP_TYPE_CONST_P (TREE_TYPE (object
)))
2273 /* Subobjects might not be stored in ctx->global->values but we
2274 can get its CONSTRUCTOR by evaluating *this. */
2275 tree e
= cxx_eval_constant_expression (ctx
, object
, /*lval*/false,
2276 non_constant_p
, overflow_p
);
2277 if (TREE_CODE (e
) == CONSTRUCTOR
&& !*non_constant_p
)
2278 TREE_READONLY (e
) = readonly_p
;
2282 /* Subroutine of cxx_eval_constant_expression.
2283 Evaluate the call expression tree T in the context of OLD_CALL expression
2287 cxx_eval_call_expression (const constexpr_ctx
*ctx
, tree t
,
2289 bool *non_constant_p
, bool *overflow_p
)
2291 /* Handle concept checks separately. */
2292 if (concept_check_p (t
))
2293 return evaluate_concept_check (t
);
2295 location_t loc
= cp_expr_loc_or_input_loc (t
);
2296 tree fun
= get_function_named_in_call (t
);
2297 constexpr_call new_call
2298 = { NULL
, NULL
, NULL
, 0, ctx
->manifestly_const_eval
};
2301 if (fun
== NULL_TREE
)
2302 return cxx_eval_internal_function (ctx
, t
, lval
,
2303 non_constant_p
, overflow_p
);
2305 if (TREE_CODE (fun
) != FUNCTION_DECL
)
2307 /* Might be a constexpr function pointer. */
2308 fun
= cxx_eval_constant_expression (ctx
, fun
,
2309 /*lval*/false, non_constant_p
,
2312 if (TREE_CODE (fun
) == ADDR_EXPR
)
2313 fun
= TREE_OPERAND (fun
, 0);
2314 /* For TARGET_VTABLE_USES_DESCRIPTORS targets, there is no
2315 indirection, the called expression is a pointer into the
2316 virtual table which should contain FDESC_EXPR. Extract the
2317 FUNCTION_DECL from there. */
2318 else if (TARGET_VTABLE_USES_DESCRIPTORS
2319 && TREE_CODE (fun
) == POINTER_PLUS_EXPR
2320 && TREE_CODE (TREE_OPERAND (fun
, 0)) == ADDR_EXPR
2321 && TREE_CODE (TREE_OPERAND (fun
, 1)) == INTEGER_CST
)
2323 tree d
= TREE_OPERAND (TREE_OPERAND (fun
, 0), 0);
2325 && DECL_VTABLE_OR_VTT_P (d
)
2326 && TREE_CODE (TREE_TYPE (d
)) == ARRAY_TYPE
2327 && TREE_TYPE (TREE_TYPE (d
)) == vtable_entry_type
2329 && TREE_CODE (DECL_INITIAL (d
)) == CONSTRUCTOR
)
2331 tree i
= int_const_binop (TRUNC_DIV_EXPR
, TREE_OPERAND (fun
, 1),
2332 TYPE_SIZE_UNIT (vtable_entry_type
));
2333 HOST_WIDE_INT idx
= find_array_ctor_elt (DECL_INITIAL (d
), i
);
2337 = (*CONSTRUCTOR_ELTS (DECL_INITIAL (d
)))[idx
].value
;
2338 if (TREE_CODE (fdesc
) == FDESC_EXPR
2339 && integer_zerop (TREE_OPERAND (fdesc
, 1)))
2340 fun
= TREE_OPERAND (fdesc
, 0);
2345 if (TREE_CODE (fun
) != FUNCTION_DECL
)
2347 if (!ctx
->quiet
&& !*non_constant_p
)
2348 error_at (loc
, "expression %qE does not designate a %<constexpr%> "
2350 *non_constant_p
= true;
2353 if (DECL_CLONED_FUNCTION_P (fun
))
2354 fun
= DECL_CLONED_FUNCTION (fun
);
2356 if (is_ubsan_builtin_p (fun
))
2359 if (fndecl_built_in_p (fun
))
2360 return cxx_eval_builtin_function_call (ctx
, t
, fun
,
2361 lval
, non_constant_p
, overflow_p
);
2362 if (DECL_THUNK_P (fun
))
2363 return cxx_eval_thunk_call (ctx
, t
, fun
, lval
, non_constant_p
, overflow_p
);
2364 if (!DECL_DECLARED_CONSTEXPR_P (fun
))
2366 if (TREE_CODE (t
) == CALL_EXPR
2367 && cxx_replaceable_global_alloc_fn (fun
)
2368 && (CALL_FROM_NEW_OR_DELETE_P (t
)
2369 || is_std_allocator_allocate (ctx
->call
)))
2371 const int nargs
= call_expr_nargs (t
);
2372 tree arg0
= NULL_TREE
;
2373 for (int i
= 0; i
< nargs
; ++i
)
2375 tree arg
= CALL_EXPR_ARG (t
, i
);
2376 arg
= cxx_eval_constant_expression (ctx
, arg
, false,
2377 non_constant_p
, overflow_p
);
2378 VERIFY_CONSTANT (arg
);
2383 if (IDENTIFIER_NEW_OP_P (DECL_NAME (fun
)))
2385 tree type
= build_array_type_nelts (char_type_node
,
2386 tree_to_uhwi (arg0
));
2387 tree var
= build_decl (loc
, VAR_DECL
,
2388 (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun
))
2390 ? heap_vec_uninit_identifier
2391 : heap_uninit_identifier
,
2393 DECL_ARTIFICIAL (var
) = 1;
2394 TREE_STATIC (var
) = 1;
2395 // Temporarily register the artificial var in varpool,
2396 // so that comparisons of its address against NULL are folded
2397 // through nonzero_address even with
2398 // -fno-delete-null-pointer-checks or that comparison of
2399 // addresses of different heap artificial vars is folded too.
2400 // See PR98988 and PR99031.
2401 varpool_node::finalize_decl (var
);
2402 ctx
->global
->heap_vars
.safe_push (var
);
2403 ctx
->global
->values
.put (var
, NULL_TREE
);
2404 return fold_convert (ptr_type_node
, build_address (var
));
2409 if (TREE_CODE (arg0
) == ADDR_EXPR
2410 && VAR_P (TREE_OPERAND (arg0
, 0)))
2412 tree var
= TREE_OPERAND (arg0
, 0);
2413 if (DECL_NAME (var
) == heap_uninit_identifier
2414 || DECL_NAME (var
) == heap_identifier
)
2416 if (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun
))
2421 error_at (loc
, "array deallocation of object "
2422 "allocated with non-array "
2424 inform (DECL_SOURCE_LOCATION (var
),
2425 "allocation performed here");
2427 *non_constant_p
= true;
2430 DECL_NAME (var
) = heap_deleted_identifier
;
2431 ctx
->global
->values
.remove (var
);
2432 ctx
->global
->heap_dealloc_count
++;
2435 else if (DECL_NAME (var
) == heap_vec_uninit_identifier
2436 || DECL_NAME (var
) == heap_vec_identifier
)
2438 if ((IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun
))
2439 & OVL_OP_FLAG_VEC
) == 0)
2443 error_at (loc
, "non-array deallocation of "
2444 "object allocated with array "
2446 inform (DECL_SOURCE_LOCATION (var
),
2447 "allocation performed here");
2449 *non_constant_p
= true;
2452 DECL_NAME (var
) = heap_deleted_identifier
;
2453 ctx
->global
->values
.remove (var
);
2454 ctx
->global
->heap_dealloc_count
++;
2457 else if (DECL_NAME (var
) == heap_deleted_identifier
)
2460 error_at (loc
, "deallocation of already deallocated "
2462 *non_constant_p
= true;
2467 error_at (loc
, "deallocation of storage that was "
2468 "not previously allocated");
2469 *non_constant_p
= true;
2473 /* Allow placement new in std::construct_at, just return the second
2475 if (TREE_CODE (t
) == CALL_EXPR
2476 && cxx_placement_new_fn (fun
)
2477 && is_std_construct_at (ctx
->call
))
2479 const int nargs
= call_expr_nargs (t
);
2480 tree arg1
= NULL_TREE
;
2481 for (int i
= 0; i
< nargs
; ++i
)
2483 tree arg
= CALL_EXPR_ARG (t
, i
);
2484 arg
= cxx_eval_constant_expression (ctx
, arg
, false,
2485 non_constant_p
, overflow_p
);
2489 VERIFY_CONSTANT (arg
);
2494 else if (cxx_dynamic_cast_fn_p (fun
))
2495 return cxx_eval_dynamic_cast_fn (ctx
, t
, non_constant_p
, overflow_p
);
2499 if (!lambda_static_thunk_p (fun
))
2500 error_at (loc
, "call to non-%<constexpr%> function %qD", fun
);
2501 explain_invalid_constexpr_fn (fun
);
2503 *non_constant_p
= true;
2507 constexpr_ctx new_ctx
= *ctx
;
2508 if (DECL_CONSTRUCTOR_P (fun
) && !ctx
->object
2509 && TREE_CODE (t
) == AGGR_INIT_EXPR
)
2511 /* We want to have an initialization target for an AGGR_INIT_EXPR.
2512 If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
2513 new_ctx
.object
= AGGR_INIT_EXPR_SLOT (t
);
2514 tree ctor
= new_ctx
.ctor
= build_constructor (DECL_CONTEXT (fun
), NULL
);
2515 CONSTRUCTOR_NO_CLEARING (ctor
) = true;
2516 ctx
->global
->values
.put (new_ctx
.object
, ctor
);
2520 /* Shortcut trivial constructor/op=. */
2521 if (trivial_fn_p (fun
))
2523 tree init
= NULL_TREE
;
2524 if (call_expr_nargs (t
) == 2)
2525 init
= convert_from_reference (get_nth_callarg (t
, 1));
2526 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
2527 && AGGR_INIT_ZERO_FIRST (t
))
2528 init
= build_zero_init (DECL_CONTEXT (fun
), NULL_TREE
, false);
2531 tree op
= get_nth_callarg (t
, 0);
2532 if (is_dummy_object (op
))
2535 op
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (op
)), op
);
2536 tree set
= build2 (MODIFY_EXPR
, TREE_TYPE (op
), op
, init
);
2537 new_ctx
.call
= &new_call
;
2538 return cxx_eval_constant_expression (&new_ctx
, set
, lval
,
2539 non_constant_p
, overflow_p
);
2543 /* We can't defer instantiating the function any longer. */
2544 if (!DECL_INITIAL (fun
)
2545 && DECL_TEMPLOID_INSTANTIATION (fun
)
2546 && !uid_sensitive_constexpr_evaluation_p ())
2548 location_t save_loc
= input_location
;
2549 input_location
= loc
;
2551 instantiate_decl (fun
, /*defer_ok*/false, /*expl_inst*/false);
2553 input_location
= save_loc
;
2556 /* If in direct recursive call, optimize definition search. */
2557 if (ctx
&& ctx
->call
&& ctx
->call
->fundef
&& ctx
->call
->fundef
->decl
== fun
)
2558 new_call
.fundef
= ctx
->call
->fundef
;
2561 new_call
.fundef
= retrieve_constexpr_fundef (fun
);
2562 if (new_call
.fundef
== NULL
|| new_call
.fundef
->body
== NULL
2563 || new_call
.fundef
->result
== error_mark_node
2564 || fun
== current_function_decl
)
2568 /* We need to check for current_function_decl here in case we're
2569 being called during cp_fold_function, because at that point
2570 DECL_INITIAL is set properly and we have a fundef but we
2571 haven't lowered invisirefs yet (c++/70344). */
2572 if (DECL_INITIAL (fun
) == error_mark_node
2573 || fun
== current_function_decl
)
2574 error_at (loc
, "%qD called in a constant expression before its "
2575 "definition is complete", fun
);
2576 else if (DECL_INITIAL (fun
))
2578 /* The definition of fun was somehow unsuitable. But pretend
2579 that lambda static thunks don't exist. */
2580 if (!lambda_static_thunk_p (fun
))
2581 error_at (loc
, "%qD called in a constant expression", fun
);
2582 explain_invalid_constexpr_fn (fun
);
2585 error_at (loc
, "%qD used before its definition", fun
);
2587 *non_constant_p
= true;
2592 bool non_constant_args
= false;
2593 cxx_bind_parameters_in_call (ctx
, t
, &new_call
,
2594 non_constant_p
, overflow_p
, &non_constant_args
);
2596 /* We build up the bindings list before we know whether we already have this
2597 call cached. If we don't end up saving these bindings, ggc_free them when
2598 this function exits. */
2603 free_bindings (tree
&b
): bindings (&b
) { }
2604 ~free_bindings () { if (bindings
) ggc_free (*bindings
); }
2605 void preserve () { bindings
= NULL
; }
2606 } fb (new_call
.bindings
);
2608 if (*non_constant_p
)
2611 depth_ok
= push_cx_call_context (t
);
2613 /* Remember the object we are constructing or destructing. */
2614 tree new_obj
= NULL_TREE
;
2615 if (DECL_CONSTRUCTOR_P (fun
) || DECL_DESTRUCTOR_P (fun
))
2617 /* In a cdtor, it should be the first `this' argument.
2618 At this point it has already been evaluated in the call
2619 to cxx_bind_parameters_in_call. */
2620 new_obj
= TREE_VEC_ELT (new_call
.bindings
, 0);
2621 STRIP_NOPS (new_obj
);
2622 if (TREE_CODE (new_obj
) == ADDR_EXPR
)
2623 new_obj
= TREE_OPERAND (new_obj
, 0);
2625 if (ctx
->call
&& ctx
->call
->fundef
2626 && DECL_CONSTRUCTOR_P (ctx
->call
->fundef
->decl
))
2628 tree cur_obj
= TREE_VEC_ELT (ctx
->call
->bindings
, 0);
2629 STRIP_NOPS (cur_obj
);
2630 if (TREE_CODE (cur_obj
) == ADDR_EXPR
)
2631 cur_obj
= TREE_OPERAND (cur_obj
, 0);
2632 if (new_obj
== cur_obj
)
2633 /* We're calling the target constructor of a delegating
2634 constructor, or accessing a base subobject through a
2635 NOP_EXPR as part of a call to a base constructor, so
2636 there is no new (sub)object. */
2637 new_obj
= NULL_TREE
;
2641 tree result
= NULL_TREE
;
2643 constexpr_call
*entry
= NULL
;
2644 if (depth_ok
&& !non_constant_args
&& ctx
->strict
)
2646 new_call
.hash
= constexpr_fundef_hasher::hash (new_call
.fundef
);
2648 = iterative_hash_template_arg (new_call
.bindings
, new_call
.hash
);
2650 = iterative_hash_object (ctx
->manifestly_const_eval
, new_call
.hash
);
2652 /* If we have seen this call before, we are done. */
2653 maybe_initialize_constexpr_call_table ();
2654 constexpr_call
**slot
2655 = constexpr_call_table
->find_slot (&new_call
, INSERT
);
2659 /* Only cache up to constexpr_cache_depth to limit memory use. */
2660 if (depth_ok
< constexpr_cache_depth
)
2662 /* We need to keep a pointer to the entry, not just the slot, as
2663 the slot can move during evaluation of the body. */
2664 *slot
= entry
= ggc_alloc
<constexpr_call
> ();
2669 /* Calls that are in progress have their result set to NULL, so that we
2670 can detect circular dependencies. Now that we only cache up to
2671 constexpr_cache_depth this won't catch circular dependencies that
2672 start deeper, but they'll hit the recursion or ops limit. */
2673 else if (entry
->result
== NULL
)
2676 error ("call has circular dependency");
2677 *non_constant_p
= true;
2678 entry
->result
= result
= error_mark_node
;
2681 result
= entry
->result
;
2687 error ("%<constexpr%> evaluation depth exceeds maximum of %d (use "
2688 "%<-fconstexpr-depth=%> to increase the maximum)",
2689 max_constexpr_depth
);
2690 *non_constant_p
= true;
2691 result
= error_mark_node
;
2695 bool cacheable
= true;
2696 if (result
&& result
!= error_mark_node
)
2698 else if (!DECL_SAVED_TREE (fun
))
2700 /* When at_eof >= 2, cgraph has started throwing away
2701 DECL_SAVED_TREE, so fail quietly. FIXME we get here because of
2702 late code generation for VEC_INIT_EXPR, which needs to be
2703 completely reconsidered. */
2704 gcc_assert (at_eof
>= 2 && ctx
->quiet
);
2705 *non_constant_p
= true;
2707 else if (tree copy
= get_fundef_copy (new_call
.fundef
))
2709 tree body
, parms
, res
;
2710 releasing_vec ctors
;
2712 /* Reuse or create a new unshared copy of this function's body. */
2713 body
= TREE_PURPOSE (copy
);
2714 parms
= TREE_VALUE (copy
);
2715 res
= TREE_TYPE (copy
);
2717 /* Associate the bindings with the remapped parms. */
2718 tree bound
= new_call
.bindings
;
2719 tree remapped
= parms
;
2720 for (int i
= 0; i
< TREE_VEC_LENGTH (bound
); ++i
)
2722 tree arg
= TREE_VEC_ELT (bound
, i
);
2725 /* Unshare args going into the hash table to separate them
2726 from the caller's context, for better GC and to avoid
2727 problems with verify_gimple. */
2728 arg
= unshare_expr_without_location (arg
);
2729 TREE_VEC_ELT (bound
, i
) = arg
;
2731 /* And then unshare again so the callee doesn't change the
2732 argument values in the hash table. XXX Could we unshare
2733 lazily in cxx_eval_store_expression? */
2734 arg
= unshare_constructor (arg
);
2735 if (TREE_CODE (arg
) == CONSTRUCTOR
)
2736 vec_safe_push (ctors
, arg
);
2738 ctx
->global
->values
.put (remapped
, arg
);
2739 remapped
= DECL_CHAIN (remapped
);
2741 /* Add the RESULT_DECL to the values map, too. */
2742 gcc_assert (!DECL_BY_REFERENCE (res
));
2743 ctx
->global
->values
.put (res
, NULL_TREE
);
2745 /* Track the callee's evaluated SAVE_EXPRs and TARGET_EXPRs so that
2746 we can forget their values after the call. */
2747 constexpr_ctx ctx_with_save_exprs
= *ctx
;
2748 auto_vec
<tree
, 10> save_exprs
;
2749 ctx_with_save_exprs
.save_exprs
= &save_exprs
;
2750 ctx_with_save_exprs
.call
= &new_call
;
2751 unsigned save_heap_alloc_count
= ctx
->global
->heap_vars
.length ();
2752 unsigned save_heap_dealloc_count
= ctx
->global
->heap_dealloc_count
;
2754 /* If this is a constexpr destructor, the object's const and volatile
2755 semantics are no longer in effect; see [class.dtor]p5. */
2756 if (new_obj
&& DECL_DESTRUCTOR_P (fun
))
2757 cxx_set_object_constness (ctx
, new_obj
, /*readonly_p=*/false,
2758 non_constant_p
, overflow_p
);
2760 tree jump_target
= NULL_TREE
;
2761 cxx_eval_constant_expression (&ctx_with_save_exprs
, body
,
2762 lval
, non_constant_p
, overflow_p
,
2765 if (DECL_CONSTRUCTOR_P (fun
))
2766 /* This can be null for a subobject constructor call, in
2767 which case what we care about is the initialization
2768 side-effects rather than the value. We could get at the
2769 value by evaluating *this, but we don't bother; there's
2770 no need to put such a call in the hash table. */
2771 result
= lval
? ctx
->object
: ctx
->ctor
;
2772 else if (VOID_TYPE_P (TREE_TYPE (res
)))
2776 result
= *ctx
->global
->values
.get (res
);
2777 if (result
== NULL_TREE
&& !*non_constant_p
)
2780 error ("%<constexpr%> call flows off the end "
2782 *non_constant_p
= true;
2786 /* At this point, the object's constructor will have run, so
2787 the object is no longer under construction, and its possible
2788 'const' semantics now apply. Make a note of this fact by
2789 marking the CONSTRUCTOR TREE_READONLY. */
2790 if (new_obj
&& DECL_CONSTRUCTOR_P (fun
))
2791 cxx_set_object_constness (ctx
, new_obj
, /*readonly_p=*/true,
2792 non_constant_p
, overflow_p
);
2794 /* Forget the saved values of the callee's SAVE_EXPRs and
2796 for (tree save_expr
: save_exprs
)
2797 ctx
->global
->values
.remove (save_expr
);
2799 /* Remove the parms/result from the values map. Is it worth
2800 bothering to do this when the map itself is only live for
2801 one constexpr evaluation? If so, maybe also clear out
2802 other vars from call, maybe in BIND_EXPR handling? */
2803 ctx
->global
->values
.remove (res
);
2804 for (tree parm
= parms
; parm
; parm
= TREE_CHAIN (parm
))
2805 ctx
->global
->values
.remove (parm
);
2807 /* Free any parameter CONSTRUCTORs we aren't returning directly. */
2808 while (!ctors
->is_empty ())
2810 tree c
= ctors
->pop ();
2812 free_constructor (c
);
2815 /* Make the unshared function copy we used available for re-use. */
2816 save_fundef_copy (fun
, copy
);
2818 /* If the call allocated some heap object that hasn't been
2819 deallocated during the call, or if it deallocated some heap
2820 object it has not allocated, the call isn't really stateless
2821 for the constexpr evaluation and should not be cached.
2822 It is fine if the call allocates something and deallocates it
2825 && (save_heap_alloc_count
!= ctx
->global
->heap_vars
.length ()
2826 || (save_heap_dealloc_count
2827 != ctx
->global
->heap_dealloc_count
)))
2831 if ((ctx
->global
->heap_vars
.length ()
2832 - ctx
->global
->heap_dealloc_count
)
2833 != save_heap_alloc_count
- save_heap_dealloc_count
)
2836 FOR_EACH_VEC_ELT_FROM (ctx
->global
->heap_vars
, i
, heap_var
,
2837 save_heap_alloc_count
)
2838 if (DECL_NAME (heap_var
) != heap_deleted_identifier
)
2845 /* Rewrite all occurrences of the function's RESULT_DECL with the
2846 current object under construction. */
2847 if (!*non_constant_p
&& ctx
->object
2848 && CLASS_TYPE_P (TREE_TYPE (res
))
2849 && !is_empty_class (TREE_TYPE (res
)))
2850 if (replace_result_decl (&result
, res
, ctx
->object
))
2854 /* Couldn't get a function copy to evaluate. */
2855 *non_constant_p
= true;
2857 if (result
== error_mark_node
)
2858 *non_constant_p
= true;
2859 if (*non_constant_p
|| *overflow_p
)
2860 result
= error_mark_node
;
2864 entry
->result
= cacheable
? result
: error_mark_node
;
2867 /* The result of a constexpr function must be completely initialized.
2869 However, in C++20, a constexpr constructor doesn't necessarily have
2870 to initialize all the fields, so we don't clear CONSTRUCTOR_NO_CLEARING
2871 in order to detect reading an unitialized object in constexpr instead
2872 of value-initializing it. (reduced_constant_expression_p is expected to
2873 take care of clearing the flag.) */
2874 if (TREE_CODE (result
) == CONSTRUCTOR
2875 && (cxx_dialect
< cxx20
2876 || !DECL_CONSTRUCTOR_P (fun
)))
2877 clear_no_implicit_zero (result
);
2879 pop_cx_call_context ();
2883 /* Return true if T is a valid constant initializer. If a CONSTRUCTOR
2884 initializes all the members, the CONSTRUCTOR_NO_CLEARING flag will be
2886 FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
2889 reduced_constant_expression_p (tree t
)
2894 switch (TREE_CODE (t
))
2897 /* Even if we can't lower this yet, it's constant. */
2901 /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
2902 tree idx
, val
, field
; unsigned HOST_WIDE_INT i
;
2903 if (CONSTRUCTOR_NO_CLEARING (t
))
2905 if (TREE_CODE (TREE_TYPE (t
)) == VECTOR_TYPE
)
2906 /* An initialized vector would have a VECTOR_CST. */
2908 else if (cxx_dialect
>= cxx20
2909 && TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
2911 /* There must be a valid constant initializer at every array
2913 tree min
= TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (t
)));
2914 tree max
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (t
)));
2916 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t
), i
, idx
, val
)
2918 if (!reduced_constant_expression_p (val
))
2920 if (array_index_cmp (cursor
, idx
) != 0)
2922 if (TREE_CODE (idx
) == RANGE_EXPR
)
2923 cursor
= TREE_OPERAND (idx
, 1);
2924 cursor
= int_const_binop (PLUS_EXPR
, cursor
, size_one_node
);
2926 if (find_array_ctor_elt (t
, max
) == -1)
2930 else if (cxx_dialect
>= cxx20
2931 && TREE_CODE (TREE_TYPE (t
)) == UNION_TYPE
)
2933 if (CONSTRUCTOR_NELTS (t
) == 0)
2934 /* An initialized union has a constructor element. */
2936 /* And it only initializes one member. */
2940 field
= next_initializable_field (TYPE_FIELDS (TREE_TYPE (t
)));
2944 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t
), i
, idx
, val
)
2946 /* If VAL is null, we're in the middle of initializing this
2948 if (!reduced_constant_expression_p (val
))
2950 /* Empty class field may or may not have an initializer. */
2951 for (; field
&& idx
!= field
;
2952 field
= next_initializable_field (DECL_CHAIN (field
)))
2953 if (!is_really_empty_class (TREE_TYPE (field
),
2954 /*ignore_vptr*/false))
2957 field
= next_initializable_field (DECL_CHAIN (field
));
2959 /* There could be a non-empty field at the end. */
2960 for (; field
; field
= next_initializable_field (DECL_CHAIN (field
)))
2961 if (!is_really_empty_class (TREE_TYPE (field
), /*ignore_vptr*/false))
2964 if (CONSTRUCTOR_NO_CLEARING (t
))
2965 /* All the fields are initialized. */
2966 CONSTRUCTOR_NO_CLEARING (t
) = false;
2970 /* FIXME are we calling this too much? */
2971 return initializer_constant_valid_p (t
, TREE_TYPE (t
)) != NULL_TREE
;
2975 /* Some expressions may have constant operands but are not constant
2976 themselves, such as 1/0. Call this function to check for that
2979 We only call this in places that require an arithmetic constant, not in
2980 places where we might have a non-constant expression that can be a
2981 component of a constant expression, such as the address of a constexpr
2982 variable that might be dereferenced later. */
2985 verify_constant (tree t
, bool allow_non_constant
, bool *non_constant_p
,
2988 if (!*non_constant_p
&& !reduced_constant_expression_p (t
)
2991 if (!allow_non_constant
)
2992 error ("%q+E is not a constant expression", t
);
2993 *non_constant_p
= true;
2995 if (TREE_OVERFLOW_P (t
))
2997 if (!allow_non_constant
)
2999 permerror (input_location
, "overflow in constant expression");
3000 /* If we're being permissive (and are in an enforcing
3001 context), ignore the overflow. */
3002 if (flag_permissive
)
3003 return *non_constant_p
;
3007 return *non_constant_p
;
3010 /* Check whether the shift operation with code CODE and type TYPE on LHS
3011 and RHS is undefined. If it is, give an error with an explanation,
3012 and return true; return false otherwise. */
3015 cxx_eval_check_shift_p (location_t loc
, const constexpr_ctx
*ctx
,
3016 enum tree_code code
, tree type
, tree lhs
, tree rhs
)
3018 if ((code
!= LSHIFT_EXPR
&& code
!= RSHIFT_EXPR
)
3019 || TREE_CODE (lhs
) != INTEGER_CST
3020 || TREE_CODE (rhs
) != INTEGER_CST
)
3023 tree lhstype
= TREE_TYPE (lhs
);
3024 unsigned HOST_WIDE_INT uprec
= TYPE_PRECISION (TREE_TYPE (lhs
));
3026 /* [expr.shift] The behavior is undefined if the right operand
3027 is negative, or greater than or equal to the length in bits
3028 of the promoted left operand. */
3029 if (tree_int_cst_sgn (rhs
) == -1)
3032 permerror (loc
, "right operand of shift expression %q+E is negative",
3033 build2_loc (loc
, code
, type
, lhs
, rhs
));
3034 return (!flag_permissive
|| ctx
->quiet
);
3036 if (compare_tree_int (rhs
, uprec
) >= 0)
3039 permerror (loc
, "right operand of shift expression %q+E is greater "
3040 "than or equal to the precision %wu of the left operand",
3041 build2_loc (loc
, code
, type
, lhs
, rhs
), uprec
);
3042 return (!flag_permissive
|| ctx
->quiet
);
3045 /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
3046 if E1 has a signed type and non-negative value, and E1x2^E2 is
3047 representable in the corresponding unsigned type of the result type,
3048 then that value, converted to the result type, is the resulting value;
3049 otherwise, the behavior is undefined.
3051 The value of E1 << E2 is the unique value congruent to E1 x 2^E2 modulo
3052 2^N, where N is the range exponent of the type of the result. */
3053 if (code
== LSHIFT_EXPR
3054 && !TYPE_UNSIGNED (lhstype
)
3055 && cxx_dialect
>= cxx11
3056 && cxx_dialect
< cxx20
)
3058 if (tree_int_cst_sgn (lhs
) == -1)
3062 "left operand of shift expression %q+E is negative",
3063 build2_loc (loc
, code
, type
, lhs
, rhs
));
3064 return (!flag_permissive
|| ctx
->quiet
);
3066 /* For signed x << y the following:
3067 (unsigned) x >> ((prec (lhs) - 1) - y)
3068 if > 1, is undefined. The right-hand side of this formula
3069 is the highest bit of the LHS that can be set (starting from 0),
3070 so that the shift doesn't overflow. We then right-shift the LHS
3071 to see whether any other bit is set making the original shift
3072 undefined -- the result is not representable in the corresponding
3074 tree t
= build_int_cst (unsigned_type_node
, uprec
- 1);
3075 t
= fold_build2 (MINUS_EXPR
, unsigned_type_node
, t
, rhs
);
3076 tree ulhs
= fold_convert (unsigned_type_for (lhstype
), lhs
);
3077 t
= fold_build2 (RSHIFT_EXPR
, TREE_TYPE (ulhs
), ulhs
, t
);
3078 if (tree_int_cst_lt (integer_one_node
, t
))
3081 permerror (loc
, "shift expression %q+E overflows",
3082 build2_loc (loc
, code
, type
, lhs
, rhs
));
3083 return (!flag_permissive
|| ctx
->quiet
);
3089 /* Subroutine of cxx_eval_constant_expression.
3090 Attempt to reduce the unary expression tree T to a compile time value.
3091 If successful, return the value. Otherwise issue a diagnostic
3092 and return error_mark_node. */
3095 cxx_eval_unary_expression (const constexpr_ctx
*ctx
, tree t
,
3097 bool *non_constant_p
, bool *overflow_p
)
3100 tree orig_arg
= TREE_OPERAND (t
, 0);
3101 tree arg
= cxx_eval_constant_expression (ctx
, orig_arg
, /*lval*/false,
3102 non_constant_p
, overflow_p
);
3103 VERIFY_CONSTANT (arg
);
3104 location_t loc
= EXPR_LOCATION (t
);
3105 enum tree_code code
= TREE_CODE (t
);
3106 tree type
= TREE_TYPE (t
);
3107 r
= fold_unary_loc (loc
, code
, type
, arg
);
3110 if (arg
== orig_arg
)
3113 r
= build1_loc (loc
, code
, type
, arg
);
3115 VERIFY_CONSTANT (r
);
3119 /* Helper function for cxx_eval_binary_expression. Try to optimize
3120 original POINTER_PLUS_EXPR T, LHS p+ RHS, return NULL_TREE if the
3121 generic folding should be used. */
3124 cxx_fold_pointer_plus_expression (const constexpr_ctx
*ctx
, tree t
,
3125 tree lhs
, tree rhs
, bool *non_constant_p
,
3129 if (TREE_CODE (lhs
) != ADDR_EXPR
)
3132 lhs
= TREE_OPERAND (lhs
, 0);
3134 /* &A[i] p+ j => &A[i + j] */
3135 if (TREE_CODE (lhs
) == ARRAY_REF
3136 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == INTEGER_CST
3137 && TREE_CODE (rhs
) == INTEGER_CST
3138 && TYPE_SIZE_UNIT (TREE_TYPE (lhs
))
3139 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs
))) == INTEGER_CST
)
3141 tree orig_type
= TREE_TYPE (t
);
3142 location_t loc
= EXPR_LOCATION (t
);
3143 tree type
= TREE_TYPE (lhs
);
3145 t
= fold_convert_loc (loc
, ssizetype
, TREE_OPERAND (lhs
, 1));
3146 tree nelts
= array_type_nelts_top (TREE_TYPE (TREE_OPERAND (lhs
, 0)));
3147 nelts
= cxx_eval_constant_expression (ctx
, nelts
, false, non_constant_p
,
3149 if (*non_constant_p
)
3151 /* Don't fold an out-of-bound access. */
3152 if (!tree_int_cst_le (t
, nelts
))
3154 rhs
= cp_fold_convert (ssizetype
, rhs
);
3155 /* Don't fold if rhs can't be divided exactly by TYPE_SIZE_UNIT.
3156 constexpr int A[1]; ... (char *)&A[0] + 1 */
3157 if (!integer_zerop (fold_build2_loc (loc
, TRUNC_MOD_EXPR
, sizetype
,
3158 rhs
, TYPE_SIZE_UNIT (type
))))
3160 /* Make sure to treat the second operand of POINTER_PLUS_EXPR
3162 rhs
= fold_build2_loc (loc
, EXACT_DIV_EXPR
, ssizetype
, rhs
,
3163 TYPE_SIZE_UNIT (type
));
3164 t
= size_binop_loc (loc
, PLUS_EXPR
, rhs
, t
);
3165 t
= build4_loc (loc
, ARRAY_REF
, type
, TREE_OPERAND (lhs
, 0),
3166 t
, NULL_TREE
, NULL_TREE
);
3167 t
= cp_build_addr_expr (t
, tf_warning_or_error
);
3168 t
= cp_fold_convert (orig_type
, t
);
3169 return cxx_eval_constant_expression (ctx
, t
, /*lval*/false,
3170 non_constant_p
, overflow_p
);
3176 /* Subroutine of cxx_eval_constant_expression.
3177 Like cxx_eval_unary_expression, except for binary expressions. */
3180 cxx_eval_binary_expression (const constexpr_ctx
*ctx
, tree t
,
3182 bool *non_constant_p
, bool *overflow_p
)
3185 tree orig_lhs
= TREE_OPERAND (t
, 0);
3186 tree orig_rhs
= TREE_OPERAND (t
, 1);
3188 lhs
= cxx_eval_constant_expression (ctx
, orig_lhs
, /*lval*/false,
3189 non_constant_p
, overflow_p
);
3190 /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer
3192 if (*non_constant_p
)
3194 rhs
= cxx_eval_constant_expression (ctx
, orig_rhs
, /*lval*/false,
3195 non_constant_p
, overflow_p
);
3196 if (*non_constant_p
)
3199 location_t loc
= EXPR_LOCATION (t
);
3200 enum tree_code code
= TREE_CODE (t
);
3201 tree type
= TREE_TYPE (t
);
3203 if (code
== EQ_EXPR
|| code
== NE_EXPR
)
3205 bool is_code_eq
= (code
== EQ_EXPR
);
3207 if (TREE_CODE (lhs
) == PTRMEM_CST
3208 && TREE_CODE (rhs
) == PTRMEM_CST
)
3210 tree lmem
= PTRMEM_CST_MEMBER (lhs
);
3211 tree rmem
= PTRMEM_CST_MEMBER (rhs
);
3213 if (TREE_CODE (lmem
) == TREE_CODE (rmem
)
3214 && TREE_CODE (lmem
) == FIELD_DECL
3215 && TREE_CODE (DECL_CONTEXT (lmem
)) == UNION_TYPE
3216 && same_type_p (DECL_CONTEXT (lmem
),
3217 DECL_CONTEXT (rmem
)))
3218 /* If both refer to (possibly different) members of the same union
3219 (12.3), they compare equal. */
3222 eq
= cp_tree_equal (lhs
, rhs
);
3223 r
= constant_boolean_node (eq
== is_code_eq
, type
);
3225 else if ((TREE_CODE (lhs
) == PTRMEM_CST
3226 || TREE_CODE (rhs
) == PTRMEM_CST
)
3227 && (null_member_pointer_value_p (lhs
)
3228 || null_member_pointer_value_p (rhs
)))
3229 r
= constant_boolean_node (!is_code_eq
, type
);
3230 else if (TREE_CODE (lhs
) == PTRMEM_CST
)
3231 lhs
= cplus_expand_constant (lhs
);
3232 else if (TREE_CODE (rhs
) == PTRMEM_CST
)
3233 rhs
= cplus_expand_constant (rhs
);
3235 if (code
== POINTER_PLUS_EXPR
&& !*non_constant_p
3236 && integer_zerop (lhs
) && !integer_zerop (rhs
))
3239 error ("arithmetic involving a null pointer in %qE", lhs
);
3240 *non_constant_p
= true;
3243 else if (code
== POINTER_PLUS_EXPR
)
3244 r
= cxx_fold_pointer_plus_expression (ctx
, t
, lhs
, rhs
, non_constant_p
,
3246 else if (code
== SPACESHIP_EXPR
)
3248 r
= genericize_spaceship (loc
, type
, lhs
, rhs
);
3249 return cxx_eval_constant_expression (ctx
, r
, lval
, non_constant_p
,
3254 r
= fold_binary_loc (loc
, code
, type
, lhs
, rhs
);
3257 && (code
== LSHIFT_EXPR
|| code
== RSHIFT_EXPR
)
3258 && TREE_CODE (lhs
) == INTEGER_CST
3259 && TREE_CODE (rhs
) == INTEGER_CST
3260 && wi::neg_p (wi::to_wide (rhs
)))
3262 /* For diagnostics and -fpermissive emulate previous behavior of
3263 handling shifts by negative amount. */
3264 tree nrhs
= const_unop (NEGATE_EXPR
, TREE_TYPE (rhs
), rhs
);
3266 r
= fold_binary_loc (loc
,
3267 code
== LSHIFT_EXPR
? RSHIFT_EXPR
: LSHIFT_EXPR
,
3273 if (lhs
== orig_lhs
&& rhs
== orig_rhs
)
3276 r
= build2_loc (loc
, code
, type
, lhs
, rhs
);
3278 else if (cxx_eval_check_shift_p (loc
, ctx
, code
, type
, lhs
, rhs
))
3279 *non_constant_p
= true;
3280 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
3281 a local array in a constexpr function. */
3282 bool ptr
= INDIRECT_TYPE_P (TREE_TYPE (lhs
));
3284 VERIFY_CONSTANT (r
);
3288 /* Subroutine of cxx_eval_constant_expression.
3289 Attempt to evaluate condition expressions. Dead branches are not
3293 cxx_eval_conditional_expression (const constexpr_ctx
*ctx
, tree t
,
3295 bool *non_constant_p
, bool *overflow_p
,
3298 tree val
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
3300 non_constant_p
, overflow_p
);
3301 VERIFY_CONSTANT (val
);
3302 if (TREE_CODE (t
) == IF_STMT
&& IF_STMT_CONSTEVAL_P (t
))
3304 /* Evaluate the condition as if it was
3305 if (__builtin_is_constant_evaluated ()), i.e. defer it if not
3306 ctx->manifestly_const_eval (as sometimes we try to constant evaluate
3307 without manifestly_const_eval even expressions or parts thereof which
3308 will later be manifestly const_eval evaluated), otherwise fold it to
3310 if (ctx
->manifestly_const_eval
)
3311 val
= boolean_true_node
;
3314 *non_constant_p
= true;
3318 /* Don't VERIFY_CONSTANT the other operands. */
3319 if (integer_zerop (val
))
3320 val
= TREE_OPERAND (t
, 2);
3322 val
= TREE_OPERAND (t
, 1);
3323 if (TREE_CODE (t
) == IF_STMT
&& !val
)
3325 return cxx_eval_constant_expression (ctx
, val
, lval
, non_constant_p
,
3326 overflow_p
, jump_target
);
3329 /* Subroutine of cxx_eval_constant_expression.
3330 Attempt to evaluate vector condition expressions. Unlike
3331 cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal
3332 ternary arithmetics operation, where all 3 arguments have to be
3333 evaluated as constants and then folding computes the result from
3337 cxx_eval_vector_conditional_expression (const constexpr_ctx
*ctx
, tree t
,
3338 bool *non_constant_p
, bool *overflow_p
)
3340 tree arg1
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
3342 non_constant_p
, overflow_p
);
3343 VERIFY_CONSTANT (arg1
);
3344 tree arg2
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
3346 non_constant_p
, overflow_p
);
3347 VERIFY_CONSTANT (arg2
);
3348 tree arg3
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 2),
3350 non_constant_p
, overflow_p
);
3351 VERIFY_CONSTANT (arg3
);
3352 location_t loc
= EXPR_LOCATION (t
);
3353 tree type
= TREE_TYPE (t
);
3354 tree r
= fold_ternary_loc (loc
, VEC_COND_EXPR
, type
, arg1
, arg2
, arg3
);
3357 if (arg1
== TREE_OPERAND (t
, 0)
3358 && arg2
== TREE_OPERAND (t
, 1)
3359 && arg3
== TREE_OPERAND (t
, 2))
3362 r
= build3_loc (loc
, VEC_COND_EXPR
, type
, arg1
, arg2
, arg3
);
3364 VERIFY_CONSTANT (r
);
3368 /* Returns less than, equal to, or greater than zero if KEY is found to be
3369 less than, to match, or to be greater than the constructor_elt's INDEX. */
3372 array_index_cmp (tree key
, tree index
)
3374 gcc_assert (TREE_CODE (key
) == INTEGER_CST
);
3376 switch (TREE_CODE (index
))
3379 return tree_int_cst_compare (key
, index
);
3382 tree lo
= TREE_OPERAND (index
, 0);
3383 tree hi
= TREE_OPERAND (index
, 1);
3384 if (tree_int_cst_lt (key
, lo
))
3386 else if (tree_int_cst_lt (hi
, key
))
3396 /* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1
3397 if none. If INSERT is true, insert a matching element rather than fail. */
3399 static HOST_WIDE_INT
3400 find_array_ctor_elt (tree ary
, tree dindex
, bool insert
)
3402 if (tree_int_cst_sgn (dindex
) < 0)
3405 unsigned HOST_WIDE_INT i
= tree_to_uhwi (dindex
);
3406 vec
<constructor_elt
, va_gc
> *elts
= CONSTRUCTOR_ELTS (ary
);
3407 unsigned HOST_WIDE_INT len
= vec_safe_length (elts
);
3409 unsigned HOST_WIDE_INT end
= len
;
3410 unsigned HOST_WIDE_INT begin
= 0;
3412 /* If the last element of the CONSTRUCTOR has its own index, we can assume
3413 that the same is true of the other elements and index directly. */
3416 tree cindex
= (*elts
)[end
- 1].index
;
3417 if (cindex
== NULL_TREE
)
3419 /* Verify that if the last index is missing, all indexes
3422 for (unsigned int j
= 0; j
< len
- 1; ++j
)
3423 gcc_assert ((*elts
)[j
].index
== NULL_TREE
);
3430 /* If the element is to be added right at the end,
3431 make sure it is added with cleared index too. */
3434 /* Otherwise, in order not to break the assumption
3435 that CONSTRUCTOR either has all indexes or none,
3436 we need to add indexes to all elements. */
3437 for (unsigned int j
= 0; j
< len
; ++j
)
3438 (*elts
)[j
].index
= build_int_cst (TREE_TYPE (dindex
), j
);
3441 else if (TREE_CODE (cindex
) == INTEGER_CST
3442 && compare_tree_int (cindex
, end
- 1) == 0)
3451 /* Otherwise, find a matching index by means of a binary search. */
3452 while (begin
!= end
)
3454 unsigned HOST_WIDE_INT middle
= (begin
+ end
) / 2;
3455 constructor_elt
&elt
= (*elts
)[middle
];
3456 tree idx
= elt
.index
;
3458 int cmp
= array_index_cmp (dindex
, idx
);
3465 if (insert
&& TREE_CODE (idx
) == RANGE_EXPR
)
3467 /* We need to split the range. */
3469 tree lo
= TREE_OPERAND (idx
, 0);
3470 tree hi
= TREE_OPERAND (idx
, 1);
3471 tree value
= elt
.value
;
3472 dindex
= fold_convert (sizetype
, dindex
);
3473 if (tree_int_cst_lt (lo
, dindex
))
3475 /* There are still some lower elts; shorten the range. */
3476 tree new_hi
= int_const_binop (MINUS_EXPR
, dindex
,
3478 if (tree_int_cst_equal (lo
, new_hi
))
3479 /* Only one element left, no longer a range. */
3482 TREE_OPERAND (idx
, 1) = new_hi
;
3483 /* Append the element we want to insert. */
3486 e
.value
= unshare_constructor (value
);
3487 vec_safe_insert (CONSTRUCTOR_ELTS (ary
), middle
, e
);
3490 /* No lower elts, the range elt is now ours. */
3493 if (tree_int_cst_lt (dindex
, hi
))
3495 /* There are still some higher elts; append a range. */
3496 tree new_lo
= int_const_binop (PLUS_EXPR
, dindex
,
3498 if (tree_int_cst_equal (new_lo
, hi
))
3501 e
.index
= build2 (RANGE_EXPR
, sizetype
, new_lo
, hi
);
3502 e
.value
= unshare_constructor (value
);
3503 vec_safe_insert (CONSTRUCTOR_ELTS (ary
), middle
+ 1, e
);
3512 constructor_elt e
= { dindex
, NULL_TREE
};
3513 vec_safe_insert (CONSTRUCTOR_ELTS (ary
), end
, e
);
3520 /* Return a pointer to the constructor_elt of CTOR which matches INDEX. If no
3521 matching constructor_elt exists, then add one to CTOR.
3523 As an optimization, if POS_HINT is non-negative then it is used as a guess
3524 for the (integer) index of the matching constructor_elt within CTOR. */
3526 static constructor_elt
*
3527 get_or_insert_ctor_field (tree ctor
, tree index
, int pos_hint
= -1)
3529 /* Check the hint first. */
3530 if (pos_hint
>= 0 && (unsigned)pos_hint
< CONSTRUCTOR_NELTS (ctor
)
3531 && CONSTRUCTOR_ELT (ctor
, pos_hint
)->index
== index
)
3532 return CONSTRUCTOR_ELT (ctor
, pos_hint
);
3534 tree type
= TREE_TYPE (ctor
);
3535 if (TREE_CODE (type
) == VECTOR_TYPE
&& index
== NULL_TREE
)
3537 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (ctor
), index
, NULL_TREE
);
3538 return &CONSTRUCTOR_ELTS (ctor
)->last();
3540 else if (TREE_CODE (type
) == ARRAY_TYPE
|| TREE_CODE (type
) == VECTOR_TYPE
)
3542 if (TREE_CODE (index
) == RANGE_EXPR
)
3544 /* Support for RANGE_EXPR index lookups is currently limited to
3545 accessing an existing element via POS_HINT, or appending a new
3546 element to the end of CTOR. ??? Support for other access
3547 patterns may also be needed. */
3548 vec
<constructor_elt
, va_gc
> *elts
= CONSTRUCTOR_ELTS (ctor
);
3549 if (vec_safe_length (elts
))
3551 tree lo
= TREE_OPERAND (index
, 0);
3552 gcc_assert (array_index_cmp (elts
->last().index
, lo
) < 0);
3554 CONSTRUCTOR_APPEND_ELT (elts
, index
, NULL_TREE
);
3555 return &elts
->last();
3558 HOST_WIDE_INT i
= find_array_ctor_elt (ctor
, index
, /*insert*/true);
3559 gcc_assert (i
>= 0);
3560 constructor_elt
*cep
= CONSTRUCTOR_ELT (ctor
, i
);
3561 gcc_assert (cep
->index
== NULL_TREE
3562 || TREE_CODE (cep
->index
) != RANGE_EXPR
);
3567 gcc_assert (TREE_CODE (index
) == FIELD_DECL
3568 && (same_type_ignoring_top_level_qualifiers_p
3569 (DECL_CONTEXT (index
), TREE_TYPE (ctor
))));
3571 /* We must keep the CONSTRUCTOR's ELTS in FIELD order.
3572 Usually we meet initializers in that order, but it is
3573 possible for base types to be placed not in program
3575 tree fields
= TYPE_FIELDS (DECL_CONTEXT (index
));
3576 unsigned HOST_WIDE_INT idx
= 0;
3577 constructor_elt
*cep
= NULL
;
3579 /* Check if we're changing the active member of a union. */
3580 if (TREE_CODE (type
) == UNION_TYPE
&& CONSTRUCTOR_NELTS (ctor
)
3581 && CONSTRUCTOR_ELT (ctor
, 0)->index
!= index
)
3582 vec_safe_truncate (CONSTRUCTOR_ELTS (ctor
), 0);
3583 /* If the bit offset of INDEX is larger than that of the last
3584 constructor_elt, then we can just immediately append a new
3585 constructor_elt to the end of CTOR. */
3586 else if (CONSTRUCTOR_NELTS (ctor
)
3587 && tree_int_cst_compare (bit_position (index
),
3588 bit_position (CONSTRUCTOR_ELTS (ctor
)
3589 ->last().index
)) > 0)
3591 idx
= CONSTRUCTOR_NELTS (ctor
);
3595 /* Otherwise, we need to iterate over CTOR to find or insert INDEX
3598 for (; vec_safe_iterate (CONSTRUCTOR_ELTS (ctor
), idx
, &cep
);
3599 idx
++, fields
= DECL_CHAIN (fields
))
3601 if (index
== cep
->index
)
3604 /* The field we're initializing must be on the field
3605 list. Look to see if it is present before the
3606 field the current ELT initializes. */
3607 for (; fields
!= cep
->index
; fields
= DECL_CHAIN (fields
))
3608 if (index
== fields
)
3611 /* We fell off the end of the CONSTRUCTOR, so insert a new
3612 entry at the end. */
3616 constructor_elt ce
= { index
, NULL_TREE
};
3618 vec_safe_insert (CONSTRUCTOR_ELTS (ctor
), idx
, ce
);
3619 cep
= CONSTRUCTOR_ELT (ctor
, idx
);
3627 /* Under the control of CTX, issue a detailed diagnostic for
3628 an out-of-bounds subscript INDEX into the expression ARRAY. */
3631 diag_array_subscript (location_t loc
, const constexpr_ctx
*ctx
, tree array
, tree index
)
3635 tree arraytype
= TREE_TYPE (array
);
3637 /* Convert the unsigned array subscript to a signed integer to avoid
3638 printing huge numbers for small negative values. */
3639 tree sidx
= fold_convert (ssizetype
, index
);
3640 STRIP_ANY_LOCATION_WRAPPER (array
);
3643 if (TYPE_DOMAIN (arraytype
))
3644 error_at (loc
, "array subscript value %qE is outside the bounds "
3645 "of array %qD of type %qT", sidx
, array
, arraytype
);
3647 error_at (loc
, "nonzero array subscript %qE is used with array %qD of "
3648 "type %qT with unknown bounds", sidx
, array
, arraytype
);
3649 inform (DECL_SOURCE_LOCATION (array
), "declared here");
3651 else if (TYPE_DOMAIN (arraytype
))
3652 error_at (loc
, "array subscript value %qE is outside the bounds "
3653 "of array type %qT", sidx
, arraytype
);
3655 error_at (loc
, "nonzero array subscript %qE is used with array of type %qT "
3656 "with unknown bounds", sidx
, arraytype
);
3660 /* Return the number of elements for TYPE (which is an ARRAY_TYPE or
3664 get_array_or_vector_nelts (const constexpr_ctx
*ctx
, tree type
,
3665 bool *non_constant_p
, bool *overflow_p
)
3668 if (TREE_CODE (type
) == ARRAY_TYPE
)
3670 if (TYPE_DOMAIN (type
))
3671 nelts
= array_type_nelts_top (type
);
3673 nelts
= size_zero_node
;
3675 else if (VECTOR_TYPE_P (type
))
3676 nelts
= size_int (TYPE_VECTOR_SUBPARTS (type
));
3680 /* For VLAs, the number of elements won't be an integer constant. */
3681 nelts
= cxx_eval_constant_expression (ctx
, nelts
, false,
3682 non_constant_p
, overflow_p
);
3686 /* Extract element INDEX consisting of CHARS_PER_ELT chars from
3687 STRING_CST STRING. */
3690 extract_string_elt (tree string
, unsigned chars_per_elt
, unsigned index
)
3692 tree type
= cv_unqualified (TREE_TYPE (TREE_TYPE (string
)));
3695 if (chars_per_elt
== 1)
3696 r
= build_int_cst (type
, TREE_STRING_POINTER (string
)[index
]);
3699 const unsigned char *ptr
3700 = ((const unsigned char *)TREE_STRING_POINTER (string
)
3701 + index
* chars_per_elt
);
3702 r
= native_interpret_expr (type
, ptr
, chars_per_elt
);
3707 /* Subroutine of cxx_eval_array_reference. T is an ARRAY_REF; evaluate the
3708 subscript, diagnose any problems with it, and return the result. */
3711 eval_and_check_array_index (const constexpr_ctx
*ctx
,
3712 tree t
, bool allow_one_past
,
3713 bool *non_constant_p
, bool *overflow_p
)
3715 location_t loc
= cp_expr_loc_or_input_loc (t
);
3716 tree ary
= TREE_OPERAND (t
, 0);
3717 t
= TREE_OPERAND (t
, 1);
3718 tree index
= cxx_eval_constant_expression (ctx
, t
, false,
3719 non_constant_p
, overflow_p
);
3720 VERIFY_CONSTANT (index
);
3722 if (!tree_fits_shwi_p (index
)
3723 || tree_int_cst_sgn (index
) < 0)
3725 diag_array_subscript (loc
, ctx
, ary
, index
);
3726 *non_constant_p
= true;
3730 tree nelts
= get_array_or_vector_nelts (ctx
, TREE_TYPE (ary
), non_constant_p
,
3732 VERIFY_CONSTANT (nelts
);
3734 ? !tree_int_cst_le (index
, nelts
)
3735 : !tree_int_cst_lt (index
, nelts
))
3737 diag_array_subscript (loc
, ctx
, ary
, index
);
3738 *non_constant_p
= true;
3745 /* Subroutine of cxx_eval_constant_expression.
3746 Attempt to reduce a reference to an array slot. */
3749 cxx_eval_array_reference (const constexpr_ctx
*ctx
, tree t
,
3751 bool *non_constant_p
, bool *overflow_p
)
3753 tree oldary
= TREE_OPERAND (t
, 0);
3754 tree ary
= cxx_eval_constant_expression (ctx
, oldary
,
3756 non_constant_p
, overflow_p
);
3757 if (*non_constant_p
)
3760 && TREE_CODE (ary
) == VIEW_CONVERT_EXPR
3761 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary
, 0)))
3762 && TREE_TYPE (t
) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary
, 0))))
3763 ary
= TREE_OPERAND (ary
, 0);
3765 tree oldidx
= TREE_OPERAND (t
, 1);
3766 tree index
= eval_and_check_array_index (ctx
, t
, lval
,
3767 non_constant_p
, overflow_p
);
3768 if (*non_constant_p
)
3771 if (lval
&& ary
== oldary
&& index
== oldidx
)
3774 return build4 (ARRAY_REF
, TREE_TYPE (t
), ary
, index
, NULL
, NULL
);
3776 unsigned len
= 0, elem_nchars
= 1;
3777 tree elem_type
= TREE_TYPE (TREE_TYPE (ary
));
3778 if (TREE_CODE (ary
) == CONSTRUCTOR
)
3779 len
= CONSTRUCTOR_NELTS (ary
);
3780 else if (TREE_CODE (ary
) == STRING_CST
)
3782 elem_nchars
= (TYPE_PRECISION (elem_type
)
3783 / TYPE_PRECISION (char_type_node
));
3784 len
= (unsigned) TREE_STRING_LENGTH (ary
) / elem_nchars
;
3786 else if (TREE_CODE (ary
) == VECTOR_CST
)
3787 /* We don't create variable-length VECTOR_CSTs. */
3788 len
= VECTOR_CST_NELTS (ary
).to_constant ();
3791 /* We can't do anything with other tree codes, so use
3792 VERIFY_CONSTANT to complain and fail. */
3793 VERIFY_CONSTANT (ary
);
3798 HOST_WIDE_INT i
= 0;
3799 if (TREE_CODE (ary
) == CONSTRUCTOR
)
3801 HOST_WIDE_INT ix
= find_array_ctor_elt (ary
, index
);
3808 i
= tree_to_shwi (index
);
3815 if (TREE_CODE (ary
) == CONSTRUCTOR
)
3816 r
= (*CONSTRUCTOR_ELTS (ary
))[i
].value
;
3817 else if (TREE_CODE (ary
) == VECTOR_CST
)
3818 r
= VECTOR_CST_ELT (ary
, i
);
3820 r
= extract_string_elt (ary
, elem_nchars
, i
);
3823 /* Don't VERIFY_CONSTANT here. */
3826 /* Otherwise the element doesn't have a value yet. */
3831 if (TREE_CODE (ary
) == CONSTRUCTOR
3832 && CONSTRUCTOR_NO_CLEARING (ary
))
3834 /* 'ary' is part of the aggregate initializer we're currently
3835 building; if there's no initializer for this element yet,
3838 error ("accessing uninitialized array element");
3839 *non_constant_p
= true;
3843 /* If it's within the array bounds but doesn't have an explicit
3844 initializer, it's initialized from {}. But use build_value_init
3845 directly for non-aggregates to avoid creating a garbage CONSTRUCTOR. */
3847 constexpr_ctx new_ctx
;
3848 if (is_really_empty_class (elem_type
, /*ignore_vptr*/false))
3849 return build_constructor (elem_type
, NULL
);
3850 else if (CP_AGGREGATE_TYPE_P (elem_type
))
3852 tree empty_ctor
= build_constructor (init_list_type_node
, NULL
);
3853 val
= digest_init (elem_type
, empty_ctor
, tf_warning_or_error
);
3856 val
= build_value_init (elem_type
, tf_warning_or_error
);
3858 if (!SCALAR_TYPE_P (elem_type
))
3862 /* If there was no object, don't add one: it could confuse us
3863 into thinking we're modifying a const object. */
3865 new_ctx
.ctor
= build_constructor (elem_type
, NULL
);
3868 t
= cxx_eval_constant_expression (ctx
, val
, lval
, non_constant_p
,
3870 if (!SCALAR_TYPE_P (elem_type
) && t
!= ctx
->ctor
)
3871 free_constructor (ctx
->ctor
);
3875 /* Subroutine of cxx_eval_constant_expression.
3876 Attempt to reduce a field access of a value of class type. */
3879 cxx_eval_component_reference (const constexpr_ctx
*ctx
, tree t
,
3881 bool *non_constant_p
, bool *overflow_p
)
3883 unsigned HOST_WIDE_INT i
;
3886 tree part
= TREE_OPERAND (t
, 1);
3887 tree orig_whole
= TREE_OPERAND (t
, 0);
3888 tree whole
= cxx_eval_constant_expression (ctx
, orig_whole
,
3890 non_constant_p
, overflow_p
);
3891 if (INDIRECT_REF_P (whole
)
3892 && integer_zerop (TREE_OPERAND (whole
, 0)))
3895 error ("dereferencing a null pointer in %qE", orig_whole
);
3896 *non_constant_p
= true;
3900 if (TREE_CODE (whole
) == PTRMEM_CST
)
3901 whole
= cplus_expand_constant (whole
);
3902 if (whole
== orig_whole
)
3905 return fold_build3 (COMPONENT_REF
, TREE_TYPE (t
),
3906 whole
, part
, NULL_TREE
);
3907 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
3909 if (!*non_constant_p
&& TREE_CODE (whole
) != CONSTRUCTOR
)
3912 error ("%qE is not a constant expression", orig_whole
);
3913 *non_constant_p
= true;
3915 if (DECL_MUTABLE_P (part
))
3918 error ("mutable %qD is not usable in a constant expression", part
);
3919 *non_constant_p
= true;
3921 if (*non_constant_p
)
3923 bool pmf
= TYPE_PTRMEMFUNC_P (TREE_TYPE (whole
));
3924 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole
), i
, field
, value
)
3926 /* Use name match for PMF fields, as a variant will have a
3927 different FIELD_DECL with a different type. */
3928 if (pmf
? DECL_NAME (field
) == DECL_NAME (part
)
3933 STRIP_ANY_LOCATION_WRAPPER (value
);
3937 /* We're in the middle of initializing it. */
3941 if (TREE_CODE (TREE_TYPE (whole
)) == UNION_TYPE
3942 && CONSTRUCTOR_NELTS (whole
) > 0)
3944 /* DR 1188 says we don't have to deal with this. */
3947 constructor_elt
*cep
= CONSTRUCTOR_ELT (whole
, 0);
3948 if (cep
->value
== NULL_TREE
)
3949 error ("accessing uninitialized member %qD", part
);
3951 error ("accessing %qD member instead of initialized %qD member in "
3952 "constant expression", part
, cep
->index
);
3954 *non_constant_p
= true;
3958 /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty
3959 classes never get represented; throw together a value now. */
3960 if (is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
3961 return build_constructor (TREE_TYPE (t
), NULL
);
3963 gcc_assert (DECL_CONTEXT (part
) == TYPE_MAIN_VARIANT (TREE_TYPE (whole
)));
3965 if (CONSTRUCTOR_NO_CLEARING (whole
))
3967 /* 'whole' is part of the aggregate initializer we're currently
3968 building; if there's no initializer for this member yet, that's an
3971 error ("accessing uninitialized member %qD", part
);
3972 *non_constant_p
= true;
3976 /* If there's no explicit init for this field, it's value-initialized. */
3977 value
= build_value_init (TREE_TYPE (t
), tf_warning_or_error
);
3978 return cxx_eval_constant_expression (ctx
, value
,
3980 non_constant_p
, overflow_p
);
3983 /* Subroutine of cxx_eval_constant_expression.
3984 Attempt to reduce a field access of a value of class type that is
3985 expressed as a BIT_FIELD_REF. */
3988 cxx_eval_bit_field_ref (const constexpr_ctx
*ctx
, tree t
,
3990 bool *non_constant_p
, bool *overflow_p
)
3992 tree orig_whole
= TREE_OPERAND (t
, 0);
3993 tree retval
, fldval
, utype
, mask
;
3994 bool fld_seen
= false;
3995 HOST_WIDE_INT istart
, isize
;
3996 tree whole
= cxx_eval_constant_expression (ctx
, orig_whole
,
3998 non_constant_p
, overflow_p
);
3999 tree start
, field
, value
;
4000 unsigned HOST_WIDE_INT i
;
4002 if (whole
== orig_whole
)
4004 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
4006 if (!*non_constant_p
4007 && TREE_CODE (whole
) != VECTOR_CST
4008 && TREE_CODE (whole
) != CONSTRUCTOR
)
4011 error ("%qE is not a constant expression", orig_whole
);
4012 *non_constant_p
= true;
4014 if (*non_constant_p
)
4017 if (TREE_CODE (whole
) == VECTOR_CST
)
4018 return fold_ternary (BIT_FIELD_REF
, TREE_TYPE (t
), whole
,
4019 TREE_OPERAND (t
, 1), TREE_OPERAND (t
, 2));
4021 start
= TREE_OPERAND (t
, 2);
4022 istart
= tree_to_shwi (start
);
4023 isize
= tree_to_shwi (TREE_OPERAND (t
, 1));
4024 utype
= TREE_TYPE (t
);
4025 if (!TYPE_UNSIGNED (utype
))
4026 utype
= build_nonstandard_integer_type (TYPE_PRECISION (utype
), 1);
4027 retval
= build_int_cst (utype
, 0);
4028 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole
), i
, field
, value
)
4030 tree bitpos
= bit_position (field
);
4031 STRIP_ANY_LOCATION_WRAPPER (value
);
4032 if (bitpos
== start
&& DECL_SIZE (field
) == TREE_OPERAND (t
, 1))
4034 if (TREE_CODE (TREE_TYPE (field
)) == INTEGER_TYPE
4035 && TREE_CODE (value
) == INTEGER_CST
4036 && tree_fits_shwi_p (bitpos
)
4037 && tree_fits_shwi_p (DECL_SIZE (field
)))
4039 HOST_WIDE_INT bit
= tree_to_shwi (bitpos
);
4040 HOST_WIDE_INT sz
= tree_to_shwi (DECL_SIZE (field
));
4041 HOST_WIDE_INT shift
;
4042 if (bit
>= istart
&& bit
+ sz
<= istart
+ isize
)
4044 fldval
= fold_convert (utype
, value
);
4045 mask
= build_int_cst_type (utype
, -1);
4046 mask
= fold_build2 (LSHIFT_EXPR
, utype
, mask
,
4047 size_int (TYPE_PRECISION (utype
) - sz
));
4048 mask
= fold_build2 (RSHIFT_EXPR
, utype
, mask
,
4049 size_int (TYPE_PRECISION (utype
) - sz
));
4050 fldval
= fold_build2 (BIT_AND_EXPR
, utype
, fldval
, mask
);
4051 shift
= bit
- istart
;
4052 if (BYTES_BIG_ENDIAN
)
4053 shift
= TYPE_PRECISION (utype
) - shift
- sz
;
4054 fldval
= fold_build2 (LSHIFT_EXPR
, utype
, fldval
,
4056 retval
= fold_build2 (BIT_IOR_EXPR
, utype
, retval
, fldval
);
4062 return fold_convert (TREE_TYPE (t
), retval
);
4064 return error_mark_node
;
4067 /* Helper for cxx_eval_bit_cast.
4068 Check [bit.cast]/3 rules, bit_cast is constexpr only if the To and From
4069 types and types of all subobjects have is_union_v<T>, is_pointer_v<T>,
4070 is_member_pointer_v<T>, is_volatile_v<T> false and has no non-static
4071 data members of reference type. */
4074 check_bit_cast_type (const constexpr_ctx
*ctx
, location_t loc
, tree type
,
4077 if (TREE_CODE (type
) == UNION_TYPE
)
4081 if (type
== orig_type
)
4082 error_at (loc
, "%qs is not a constant expression because %qT is "
4083 "a union type", "__builtin_bit_cast", type
);
4085 error_at (loc
, "%qs is not a constant expression because %qT "
4086 "contains a union type", "__builtin_bit_cast",
4091 if (TREE_CODE (type
) == POINTER_TYPE
)
4095 if (type
== orig_type
)
4096 error_at (loc
, "%qs is not a constant expression because %qT is "
4097 "a pointer type", "__builtin_bit_cast", type
);
4099 error_at (loc
, "%qs is not a constant expression because %qT "
4100 "contains a pointer type", "__builtin_bit_cast",
4105 if (TREE_CODE (type
) == REFERENCE_TYPE
)
4109 if (type
== orig_type
)
4110 error_at (loc
, "%qs is not a constant expression because %qT is "
4111 "a reference type", "__builtin_bit_cast", type
);
4113 error_at (loc
, "%qs is not a constant expression because %qT "
4114 "contains a reference type", "__builtin_bit_cast",
4119 if (TYPE_PTRMEM_P (type
))
4123 if (type
== orig_type
)
4124 error_at (loc
, "%qs is not a constant expression because %qT is "
4125 "a pointer to member type", "__builtin_bit_cast",
4128 error_at (loc
, "%qs is not a constant expression because %qT "
4129 "contains a pointer to member type",
4130 "__builtin_bit_cast", orig_type
);
4134 if (TYPE_VOLATILE (type
))
4138 if (type
== orig_type
)
4139 error_at (loc
, "%qs is not a constant expression because %qT is "
4140 "volatile", "__builtin_bit_cast", type
);
4142 error_at (loc
, "%qs is not a constant expression because %qT "
4143 "contains a volatile subobject",
4144 "__builtin_bit_cast", orig_type
);
4148 if (TREE_CODE (type
) == RECORD_TYPE
)
4149 for (tree field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
4150 if (TREE_CODE (field
) == FIELD_DECL
4151 && check_bit_cast_type (ctx
, loc
, TREE_TYPE (field
), orig_type
))
4156 /* Subroutine of cxx_eval_constant_expression.
4157 Attempt to evaluate a BIT_CAST_EXPR. */
4160 cxx_eval_bit_cast (const constexpr_ctx
*ctx
, tree t
, bool *non_constant_p
,
4163 if (check_bit_cast_type (ctx
, EXPR_LOCATION (t
), TREE_TYPE (t
),
4165 || check_bit_cast_type (ctx
, cp_expr_loc_or_loc (TREE_OPERAND (t
, 0),
4167 TREE_TYPE (TREE_OPERAND (t
, 0)),
4168 TREE_TYPE (TREE_OPERAND (t
, 0))))
4170 *non_constant_p
= true;
4174 tree op
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), false,
4175 non_constant_p
, overflow_p
);
4176 if (*non_constant_p
)
4179 location_t loc
= EXPR_LOCATION (t
);
4180 if (BITS_PER_UNIT
!= 8 || CHAR_BIT
!= 8)
4183 sorry_at (loc
, "%qs cannot be constant evaluated on the target",
4184 "__builtin_bit_cast");
4185 *non_constant_p
= true;
4189 if (!tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (t
))))
4192 sorry_at (loc
, "%qs cannot be constant evaluated because the "
4193 "type is too large", "__builtin_bit_cast");
4194 *non_constant_p
= true;
4198 HOST_WIDE_INT len
= tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (t
)));
4199 if (len
< 0 || (int) len
!= len
)
4202 sorry_at (loc
, "%qs cannot be constant evaluated because the "
4203 "type is too large", "__builtin_bit_cast");
4204 *non_constant_p
= true;
4208 unsigned char buf
[64];
4209 unsigned char *ptr
, *mask
;
4210 size_t alen
= (size_t) len
* 2;
4211 if (alen
<= sizeof (buf
))
4214 ptr
= XNEWVEC (unsigned char, alen
);
4215 mask
= ptr
+ (size_t) len
;
4216 /* At the beginning consider everything indeterminate. */
4217 memset (mask
, ~0, (size_t) len
);
4219 if (native_encode_initializer (op
, ptr
, len
, 0, mask
) != len
)
4222 sorry_at (loc
, "%qs cannot be constant evaluated because the "
4223 "argument cannot be encoded", "__builtin_bit_cast");
4224 *non_constant_p
= true;
4231 if (can_native_interpret_type_p (TREE_TYPE (t
)))
4232 r
= native_interpret_expr (TREE_TYPE (t
), ptr
, len
);
4233 else if (TREE_CODE (TREE_TYPE (t
)) == RECORD_TYPE
)
4235 r
= native_interpret_aggregate (TREE_TYPE (t
), ptr
, 0, len
);
4237 clear_type_padding_in_mask (TREE_TYPE (t
), mask
);
4242 for (int i
= 0; i
< len
; i
++)
4246 error_at (loc
, "%qs accessing uninitialized byte at offset %d",
4247 "__builtin_bit_cast", i
);
4248 *non_constant_p
= true;
4258 sorry_at (loc
, "%qs cannot be constant evaluated because the "
4259 "argument cannot be interpreted", "__builtin_bit_cast");
4260 *non_constant_p
= true;
4266 /* Subroutine of cxx_eval_constant_expression.
4267 Evaluate a short-circuited logical expression T in the context
4268 of a given constexpr CALL. BAILOUT_VALUE is the value for
4269 early return. CONTINUE_VALUE is used here purely for
4270 sanity check purposes. */
4273 cxx_eval_logical_expression (const constexpr_ctx
*ctx
, tree t
,
4274 tree bailout_value
, tree continue_value
,
4276 bool *non_constant_p
, bool *overflow_p
)
4279 tree lhs
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
4281 non_constant_p
, overflow_p
);
4282 VERIFY_CONSTANT (lhs
);
4283 if (tree_int_cst_equal (lhs
, bailout_value
))
4285 gcc_assert (tree_int_cst_equal (lhs
, continue_value
));
4286 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
4287 lval
, non_constant_p
,
4289 VERIFY_CONSTANT (r
);
4293 /* REF is a COMPONENT_REF designating a particular field. V is a vector of
4294 CONSTRUCTOR elements to initialize (part of) an object containing that
4295 field. Return a pointer to the constructor_elt corresponding to the
4296 initialization of the field. */
4298 static constructor_elt
*
4299 base_field_constructor_elt (vec
<constructor_elt
, va_gc
> *v
, tree ref
)
4301 tree aggr
= TREE_OPERAND (ref
, 0);
4302 tree field
= TREE_OPERAND (ref
, 1);
4304 constructor_elt
*ce
;
4306 gcc_assert (TREE_CODE (ref
) == COMPONENT_REF
);
4308 if (TREE_CODE (aggr
) == COMPONENT_REF
)
4310 constructor_elt
*base_ce
4311 = base_field_constructor_elt (v
, aggr
);
4312 v
= CONSTRUCTOR_ELTS (base_ce
->value
);
4315 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
4316 if (ce
->index
== field
)
4323 /* Some of the expressions fed to the constexpr mechanism are calls to
4324 constructors, which have type void. In that case, return the type being
4325 initialized by the constructor. */
4328 initialized_type (tree t
)
4332 tree type
= TREE_TYPE (t
);
4333 if (TREE_CODE (t
) == CALL_EXPR
)
4335 /* A constructor call has void type, so we need to look deeper. */
4336 tree fn
= get_function_named_in_call (t
);
4337 if (fn
&& TREE_CODE (fn
) == FUNCTION_DECL
4338 && DECL_CXX_CONSTRUCTOR_P (fn
))
4339 type
= DECL_CONTEXT (fn
);
4341 else if (TREE_CODE (t
) == COMPOUND_EXPR
)
4342 return initialized_type (TREE_OPERAND (t
, 1));
4343 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
)
4344 type
= TREE_TYPE (AGGR_INIT_EXPR_SLOT (t
));
4345 return cv_unqualified (type
);
4348 /* We're about to initialize element INDEX of an array or class from VALUE.
4349 Set up NEW_CTX appropriately by adjusting .object to refer to the
4350 subobject and creating a new CONSTRUCTOR if the element is itself
4351 a class or array. */
4354 init_subob_ctx (const constexpr_ctx
*ctx
, constexpr_ctx
&new_ctx
,
4355 tree index
, tree
&value
)
4359 if (index
&& TREE_CODE (index
) != INTEGER_CST
4360 && TREE_CODE (index
) != FIELD_DECL
4361 && TREE_CODE (index
) != RANGE_EXPR
)
4362 /* This won't have an element in the new CONSTRUCTOR. */
4365 tree type
= initialized_type (value
);
4366 if (!AGGREGATE_TYPE_P (type
) && !VECTOR_TYPE_P (type
))
4367 /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
4370 /* The sub-aggregate initializer might contain a placeholder;
4371 update object to refer to the subobject and ctor to refer to
4372 the (newly created) sub-initializer. */
4375 if (index
== NULL_TREE
|| TREE_CODE (index
) == RANGE_EXPR
)
4376 /* There's no well-defined subobject for this index. */
4377 new_ctx
.object
= NULL_TREE
;
4379 new_ctx
.object
= build_ctor_subob_ref (index
, type
, ctx
->object
);
4381 tree elt
= build_constructor (type
, NULL
);
4382 CONSTRUCTOR_NO_CLEARING (elt
) = true;
4385 if (TREE_CODE (value
) == TARGET_EXPR
)
4386 /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
4387 value
= TARGET_EXPR_INITIAL (value
);
4390 /* We're about to process an initializer for a class or array TYPE. Make
4391 sure that CTX is set up appropriately. */
4394 verify_ctor_sanity (const constexpr_ctx
*ctx
, tree type
)
4396 /* We don't bother building a ctor for an empty base subobject. */
4397 if (is_empty_class (type
))
4400 /* We're in the middle of an initializer that might involve placeholders;
4401 our caller should have created a CONSTRUCTOR for us to put the
4402 initializer into. We will either return that constructor or T. */
4403 gcc_assert (ctx
->ctor
);
4404 gcc_assert (same_type_ignoring_top_level_qualifiers_p
4405 (type
, TREE_TYPE (ctx
->ctor
)));
4406 /* We used to check that ctx->ctor was empty, but that isn't the case when
4407 the object is zero-initialized before calling the constructor. */
4410 tree otype
= TREE_TYPE (ctx
->object
);
4411 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type
, otype
)
4412 /* Handle flexible array members. */
4413 || (TREE_CODE (otype
) == ARRAY_TYPE
4414 && TYPE_DOMAIN (otype
) == NULL_TREE
4415 && TREE_CODE (type
) == ARRAY_TYPE
4416 && (same_type_ignoring_top_level_qualifiers_p
4417 (TREE_TYPE (type
), TREE_TYPE (otype
)))));
4419 gcc_assert (!ctx
->object
|| !DECL_P (ctx
->object
)
4420 || *(ctx
->global
->values
.get (ctx
->object
)) == ctx
->ctor
);
4423 /* Subroutine of cxx_eval_constant_expression.
4424 The expression tree T denotes a C-style array or a C-style
4425 aggregate. Reduce it to a constant expression. */
4428 cxx_eval_bare_aggregate (const constexpr_ctx
*ctx
, tree t
,
4430 bool *non_constant_p
, bool *overflow_p
)
4432 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (t
);
4433 bool changed
= false;
4434 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t
));
4435 tree type
= TREE_TYPE (t
);
4437 constexpr_ctx new_ctx
;
4438 if (TYPE_PTRMEMFUNC_P (type
) || VECTOR_TYPE_P (type
))
4440 /* We don't really need the ctx->ctor business for a PMF or
4441 vector, but it's simpler to use the same code. */
4443 new_ctx
.ctor
= build_constructor (type
, NULL
);
4444 new_ctx
.object
= NULL_TREE
;
4447 verify_ctor_sanity (ctx
, type
);
4448 vec
<constructor_elt
, va_gc
> **p
= &CONSTRUCTOR_ELTS (ctx
->ctor
);
4449 vec_alloc (*p
, vec_safe_length (v
));
4451 if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (t
))
4452 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx
->ctor
) = 1;
4456 bool constant_p
= true;
4457 bool side_effects_p
= false;
4458 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, index
, value
)
4460 tree orig_value
= value
;
4461 /* Like in cxx_eval_store_expression, omit entries for empty fields. */
4462 bool no_slot
= TREE_CODE (type
) == RECORD_TYPE
&& is_empty_field (index
);
4466 init_subob_ctx (ctx
, new_ctx
, index
, value
);
4468 if (new_ctx
.ctor
!= ctx
->ctor
)
4470 /* If we built a new CONSTRUCTOR, attach it now so that other
4471 initializers can refer to it. */
4472 constructor_elt
*cep
= get_or_insert_ctor_field (ctx
->ctor
, index
);
4473 cep
->value
= new_ctx
.ctor
;
4474 pos_hint
= cep
- (*p
)->begin();
4476 else if (TREE_CODE (type
) == UNION_TYPE
)
4477 /* Otherwise if we're constructing a non-aggregate union member, set
4478 the active union member now so that we can later detect and diagnose
4479 if its initializer attempts to activate another member. */
4480 get_or_insert_ctor_field (ctx
->ctor
, index
);
4481 tree elt
= cxx_eval_constant_expression (&new_ctx
, value
,
4483 non_constant_p
, overflow_p
);
4484 /* Don't VERIFY_CONSTANT here. */
4485 if (ctx
->quiet
&& *non_constant_p
)
4487 if (elt
!= orig_value
)
4490 if (!TREE_CONSTANT (elt
))
4492 if (TREE_SIDE_EFFECTS (elt
))
4493 side_effects_p
= true;
4494 if (index
&& TREE_CODE (index
) == COMPONENT_REF
)
4496 /* This is an initialization of a vfield inside a base
4497 subaggregate that we already initialized; push this
4498 initialization into the previous initialization. */
4499 constructor_elt
*inner
= base_field_constructor_elt (*p
, index
);
4504 && (TREE_CODE (index
) == NOP_EXPR
4505 || TREE_CODE (index
) == POINTER_PLUS_EXPR
))
4507 /* This is an initializer for an empty base; now that we've
4508 checked that it's constant, we can ignore it. */
4509 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index
))));
4516 if (TREE_CODE (type
) == UNION_TYPE
4517 && (*p
)->last().index
!= index
)
4518 /* The initializer erroneously changed the active union member that
4519 we're initializing. */
4520 gcc_assert (*non_constant_p
);
4523 /* The initializer might have mutated the underlying CONSTRUCTOR,
4524 so recompute the location of the target constructer_elt. */
4525 constructor_elt
*cep
4526 = get_or_insert_ctor_field (ctx
->ctor
, index
, pos_hint
);
4530 /* Adding or replacing an element might change the ctor's flags. */
4531 TREE_CONSTANT (ctx
->ctor
) = constant_p
;
4532 TREE_SIDE_EFFECTS (ctx
->ctor
) = side_effects_p
;
4535 if (*non_constant_p
|| !changed
)
4538 /* We're done building this CONSTRUCTOR, so now we can interpret an
4539 element without an explicit initializer as value-initialized. */
4540 CONSTRUCTOR_NO_CLEARING (t
) = false;
4541 TREE_CONSTANT (t
) = constant_p
;
4542 TREE_SIDE_EFFECTS (t
) = side_effects_p
;
4543 if (VECTOR_TYPE_P (type
))
4548 /* Subroutine of cxx_eval_constant_expression.
4549 The expression tree T is a VEC_INIT_EXPR which denotes the desired
4550 initialization of a non-static data member of array type. Reduce it to a
4553 Note that apart from value-initialization (when VALUE_INIT is true),
4554 this is only intended to support value-initialization and the
4555 initializations done by defaulted constructors for classes with
4556 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
4557 will either be NULL_TREE for the default constructor, or a COMPONENT_REF
4558 for the copy/move constructor. */
4561 cxx_eval_vec_init_1 (const constexpr_ctx
*ctx
, tree atype
, tree init
,
4562 bool value_init
, bool lval
,
4563 bool *non_constant_p
, bool *overflow_p
)
4565 tree elttype
= TREE_TYPE (atype
);
4566 verify_ctor_sanity (ctx
, atype
);
4567 vec
<constructor_elt
, va_gc
> **p
= &CONSTRUCTOR_ELTS (ctx
->ctor
);
4568 bool pre_init
= false;
4569 unsigned HOST_WIDE_INT i
;
4570 tsubst_flags_t complain
= ctx
->quiet
? tf_none
: tf_warning_or_error
;
4572 if (init
&& TREE_CODE (init
) == CONSTRUCTOR
)
4573 return cxx_eval_bare_aggregate (ctx
, init
, lval
,
4574 non_constant_p
, overflow_p
);
4576 /* For the default constructor, build up a call to the default
4577 constructor of the element type. We only need to handle class types
4578 here, as for a constructor to be constexpr, all members must be
4579 initialized, which for a defaulted default constructor means they must
4580 be of a class type with a constexpr default constructor. */
4581 if (TREE_CODE (elttype
) == ARRAY_TYPE
)
4582 /* We only do this at the lowest level. */;
4583 else if (value_init
)
4585 init
= build_value_init (elttype
, complain
);
4590 releasing_vec argvec
;
4591 init
= build_special_member_call (NULL_TREE
, complete_ctor_identifier
,
4592 &argvec
, elttype
, LOOKUP_NORMAL
,
4594 init
= build_aggr_init_expr (elttype
, init
);
4598 bool zeroed_out
= false;
4599 if (!CONSTRUCTOR_NO_CLEARING (ctx
->ctor
))
4601 /* We're initializing an array object that had been zero-initialized
4602 earlier. Truncate ctx->ctor, and propagate its zeroed state by
4603 clearing CONSTRUCTOR_NO_CLEARING on each of the aggregate element
4604 initializers we append to it. */
4605 gcc_checking_assert (initializer_zerop (ctx
->ctor
));
4607 vec_safe_truncate (*p
, 0);
4610 tree nelts
= get_array_or_vector_nelts (ctx
, atype
, non_constant_p
,
4612 unsigned HOST_WIDE_INT max
= tree_to_uhwi (nelts
);
4613 for (i
= 0; i
< max
; ++i
)
4615 tree idx
= build_int_cst (size_type_node
, i
);
4618 constexpr_ctx new_ctx
;
4619 init_subob_ctx (ctx
, new_ctx
, idx
, pre_init
? init
: elttype
);
4620 if (new_ctx
.ctor
!= ctx
->ctor
)
4623 CONSTRUCTOR_NO_CLEARING (new_ctx
.ctor
) = false;
4624 CONSTRUCTOR_APPEND_ELT (*p
, idx
, new_ctx
.ctor
);
4626 if (TREE_CODE (elttype
) == ARRAY_TYPE
)
4628 /* A multidimensional array; recurse. */
4629 if (value_init
|| init
== NULL_TREE
)
4631 eltinit
= NULL_TREE
;
4635 eltinit
= cp_build_array_ref (input_location
, init
, idx
, complain
);
4636 eltinit
= cxx_eval_vec_init_1 (&new_ctx
, elttype
, eltinit
, value_init
,
4638 non_constant_p
, overflow_p
);
4642 /* Initializing an element using value or default initialization
4643 we just pre-built above. */
4644 if (init
== void_node
)
4645 /* Trivial default-init, don't do anything to the CONSTRUCTOR. */
4647 eltinit
= cxx_eval_constant_expression (&new_ctx
, init
, lval
,
4648 non_constant_p
, overflow_p
);
4653 /* Copying an element. */
4654 gcc_assert (same_type_ignoring_top_level_qualifiers_p
4655 (atype
, TREE_TYPE (init
)));
4656 eltinit
= cp_build_array_ref (input_location
, init
, idx
, complain
);
4657 if (!lvalue_p (init
))
4658 eltinit
= move (eltinit
);
4659 eltinit
= force_rvalue (eltinit
, complain
);
4660 eltinit
= cxx_eval_constant_expression (&new_ctx
, eltinit
, lval
,
4661 non_constant_p
, overflow_p
);
4663 if (*non_constant_p
)
4665 if (new_ctx
.ctor
!= ctx
->ctor
)
4667 /* We appended this element above; update the value. */
4668 gcc_assert ((*p
)->last().index
== idx
);
4669 (*p
)->last().value
= eltinit
;
4672 CONSTRUCTOR_APPEND_ELT (*p
, idx
, eltinit
);
4673 /* Reuse the result of cxx_eval_constant_expression call
4674 from the first iteration to all others if it is a constant
4675 initializer that doesn't require relocations. */
4678 && (eltinit
== NULL_TREE
4679 || (initializer_constant_valid_p (eltinit
, TREE_TYPE (eltinit
))
4680 == null_pointer_node
)))
4682 if (new_ctx
.ctor
!= ctx
->ctor
)
4683 eltinit
= new_ctx
.ctor
;
4684 tree range
= build2 (RANGE_EXPR
, size_type_node
,
4685 build_int_cst (size_type_node
, 1),
4686 build_int_cst (size_type_node
, max
- 1));
4687 CONSTRUCTOR_APPEND_ELT (*p
, range
, unshare_constructor (eltinit
));
4691 vec_safe_reserve (*p
, max
);
4694 if (!*non_constant_p
)
4697 CONSTRUCTOR_NO_CLEARING (init
) = false;
4703 cxx_eval_vec_init (const constexpr_ctx
*ctx
, tree t
,
4705 bool *non_constant_p
, bool *overflow_p
)
4707 tree atype
= TREE_TYPE (t
);
4708 tree init
= VEC_INIT_EXPR_INIT (t
);
4709 tree r
= cxx_eval_vec_init_1 (ctx
, atype
, init
,
4710 VEC_INIT_EXPR_VALUE_INIT (t
),
4711 lval
, non_constant_p
, overflow_p
);
4712 if (*non_constant_p
)
4718 /* Like same_type_ignoring_top_level_qualifiers_p, but also handle the case
4719 where the desired type is an array of unknown bounds because the variable
4720 has had its bounds deduced since the wrapping expression was created. */
4723 same_type_ignoring_tlq_and_bounds_p (tree type1
, tree type2
)
4725 while (TREE_CODE (type1
) == ARRAY_TYPE
4726 && TREE_CODE (type2
) == ARRAY_TYPE
4727 && (!TYPE_DOMAIN (type1
) || !TYPE_DOMAIN (type2
)))
4729 type1
= TREE_TYPE (type1
);
4730 type2
= TREE_TYPE (type2
);
4732 return same_type_ignoring_top_level_qualifiers_p (type1
, type2
);
4735 /* Try to determine the currently active union member for an expression
4736 with UNION_TYPE. If it can be determined, return the FIELD_DECL,
4737 otherwise return NULL_TREE. */
4740 cxx_union_active_member (const constexpr_ctx
*ctx
, tree t
)
4742 constexpr_ctx new_ctx
= *ctx
;
4743 new_ctx
.quiet
= true;
4744 bool non_constant_p
= false, overflow_p
= false;
4745 tree ctor
= cxx_eval_constant_expression (&new_ctx
, t
, false,
4748 if (TREE_CODE (ctor
) == CONSTRUCTOR
4749 && CONSTRUCTOR_NELTS (ctor
) == 1
4750 && CONSTRUCTOR_ELT (ctor
, 0)->index
4751 && TREE_CODE (CONSTRUCTOR_ELT (ctor
, 0)->index
) == FIELD_DECL
)
4752 return CONSTRUCTOR_ELT (ctor
, 0)->index
;
4756 /* Helper function for cxx_fold_indirect_ref_1, called recursively. */
4759 cxx_fold_indirect_ref_1 (const constexpr_ctx
*ctx
, location_t loc
, tree type
,
4760 tree op
, unsigned HOST_WIDE_INT off
, bool *empty_base
)
4762 tree optype
= TREE_TYPE (op
);
4763 unsigned HOST_WIDE_INT const_nunits
;
4764 if (off
== 0 && similar_type_p (optype
, type
))
4766 else if (TREE_CODE (optype
) == COMPLEX_TYPE
4767 && similar_type_p (type
, TREE_TYPE (optype
)))
4769 /* *(foo *)&complexfoo => __real__ complexfoo */
4771 return build1_loc (loc
, REALPART_EXPR
, type
, op
);
4772 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
4773 else if (tree_to_uhwi (TYPE_SIZE_UNIT (type
)) == off
)
4774 return build1_loc (loc
, IMAGPART_EXPR
, type
, op
);
4776 /* ((foo*)&vectorfoo)[x] => BIT_FIELD_REF<vectorfoo,...> */
4777 else if (VECTOR_TYPE_P (optype
)
4778 && similar_type_p (type
, TREE_TYPE (optype
))
4779 && TYPE_VECTOR_SUBPARTS (optype
).is_constant (&const_nunits
))
4781 unsigned HOST_WIDE_INT part_width
= tree_to_uhwi (TYPE_SIZE_UNIT (type
));
4782 unsigned HOST_WIDE_INT max_offset
= part_width
* const_nunits
;
4783 if (off
< max_offset
&& off
% part_width
== 0)
4785 tree index
= bitsize_int (off
* BITS_PER_UNIT
);
4786 return build3_loc (loc
, BIT_FIELD_REF
, type
, op
,
4787 TYPE_SIZE (type
), index
);
4790 /* ((foo *)&fooarray)[x] => fooarray[x] */
4791 else if (TREE_CODE (optype
) == ARRAY_TYPE
4792 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (optype
)))
4793 && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (optype
))))
4795 tree type_domain
= TYPE_DOMAIN (optype
);
4796 tree min_val
= size_zero_node
;
4797 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
4798 min_val
= TYPE_MIN_VALUE (type_domain
);
4799 unsigned HOST_WIDE_INT el_sz
4800 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (optype
)));
4801 unsigned HOST_WIDE_INT idx
= off
/ el_sz
;
4802 unsigned HOST_WIDE_INT rem
= off
% el_sz
;
4803 if (tree_fits_uhwi_p (min_val
))
4805 tree index
= size_int (idx
+ tree_to_uhwi (min_val
));
4806 op
= build4_loc (loc
, ARRAY_REF
, TREE_TYPE (optype
), op
, index
,
4807 NULL_TREE
, NULL_TREE
);
4808 return cxx_fold_indirect_ref_1 (ctx
, loc
, type
, op
, rem
,
4812 /* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */
4813 else if (TREE_CODE (optype
) == RECORD_TYPE
4814 || TREE_CODE (optype
) == UNION_TYPE
)
4816 if (TREE_CODE (optype
) == UNION_TYPE
)
4817 /* For unions prefer the currently active member. */
4818 if (tree field
= cxx_union_active_member (ctx
, op
))
4820 unsigned HOST_WIDE_INT el_sz
4821 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field
)));
4824 tree cop
= build3 (COMPONENT_REF
, TREE_TYPE (field
),
4825 op
, field
, NULL_TREE
);
4826 if (tree ret
= cxx_fold_indirect_ref_1 (ctx
, loc
, type
, cop
,
4831 for (tree field
= TYPE_FIELDS (optype
);
4832 field
; field
= DECL_CHAIN (field
))
4833 if (TREE_CODE (field
) == FIELD_DECL
4834 && TREE_TYPE (field
) != error_mark_node
4835 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (field
))))
4837 tree pos
= byte_position (field
);
4838 if (!tree_fits_uhwi_p (pos
))
4840 unsigned HOST_WIDE_INT upos
= tree_to_uhwi (pos
);
4841 unsigned HOST_WIDE_INT el_sz
4842 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field
)));
4843 if (upos
<= off
&& off
< upos
+ el_sz
)
4845 tree cop
= build3 (COMPONENT_REF
, TREE_TYPE (field
),
4846 op
, field
, NULL_TREE
);
4847 if (tree ret
= cxx_fold_indirect_ref_1 (ctx
, loc
, type
, cop
,
4853 /* Also handle conversion to an empty base class, which
4854 is represented with a NOP_EXPR. */
4855 if (is_empty_class (type
)
4856 && CLASS_TYPE_P (optype
)
4857 && DERIVED_FROM_P (type
, optype
))
4867 /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
4868 match. We want to be less strict for simple *& folding; if we have a
4869 non-const temporary that we access through a const pointer, that should
4870 work. We handle this here rather than change fold_indirect_ref_1
4871 because we're dealing with things like ADDR_EXPR of INTEGER_CST which
4872 don't really make sense outside of constant expression evaluation. Also
4873 we want to allow folding to COMPONENT_REF, which could cause trouble
4874 with TBAA in fold_indirect_ref_1. */
4877 cxx_fold_indirect_ref (const constexpr_ctx
*ctx
, location_t loc
, tree type
,
4878 tree op0
, bool *empty_base
)
4882 poly_uint64 const_op01
;
4884 /* STRIP_NOPS, but stop if REINTERPRET_CAST_P. */
4885 while (CONVERT_EXPR_P (sub
) || TREE_CODE (sub
) == NON_LVALUE_EXPR
4886 || TREE_CODE (sub
) == VIEW_CONVERT_EXPR
)
4888 if (TREE_CODE (sub
) == NOP_EXPR
4889 && REINTERPRET_CAST_P (sub
))
4891 sub
= TREE_OPERAND (sub
, 0);
4894 subtype
= TREE_TYPE (sub
);
4895 if (!INDIRECT_TYPE_P (subtype
))
4898 if (TREE_CODE (sub
) == ADDR_EXPR
)
4900 tree op
= TREE_OPERAND (sub
, 0);
4901 tree optype
= TREE_TYPE (op
);
4903 /* *&CONST_DECL -> to the value of the const decl. */
4904 if (TREE_CODE (op
) == CONST_DECL
)
4905 return DECL_INITIAL (op
);
4906 /* *&p => p; make sure to handle *&"str"[cst] here. */
4907 if (similar_type_p (optype
, type
))
4909 tree fop
= fold_read_from_constant_string (op
);
4916 return cxx_fold_indirect_ref_1 (ctx
, loc
, type
, op
, 0, empty_base
);
4918 else if (TREE_CODE (sub
) == POINTER_PLUS_EXPR
4919 && tree_fits_uhwi_p (TREE_OPERAND (sub
, 1)))
4921 tree op00
= TREE_OPERAND (sub
, 0);
4922 tree off
= TREE_OPERAND (sub
, 1);
4925 if (TREE_CODE (op00
) == ADDR_EXPR
)
4927 tree obj
= TREE_OPERAND (op00
, 0);
4928 while (TREE_CODE (obj
) == COMPONENT_REF
4929 && tree_int_cst_sign_bit (off
))
4931 /* Canonicalize this object/offset pair by iteratively absorbing
4932 the innermost component into the offset until the offset is
4933 nonnegative, so that cxx_fold_indirect_ref_1 can identify
4934 more folding opportunities. */
4935 tree field
= TREE_OPERAND (obj
, 1);
4936 off
= int_const_binop (PLUS_EXPR
, off
, byte_position (field
));
4937 obj
= TREE_OPERAND (obj
, 0);
4939 return cxx_fold_indirect_ref_1 (ctx
, loc
, type
, obj
,
4940 tree_to_uhwi (off
), empty_base
);
4943 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
4944 else if (TREE_CODE (TREE_TYPE (subtype
)) == ARRAY_TYPE
4945 && similar_type_p (type
, TREE_TYPE (TREE_TYPE (subtype
))))
4948 tree min_val
= size_zero_node
;
4950 = cxx_fold_indirect_ref (ctx
, loc
, TREE_TYPE (subtype
), sub
, NULL
);
4954 sub
= build1_loc (loc
, INDIRECT_REF
, TREE_TYPE (subtype
), sub
);
4955 type_domain
= TYPE_DOMAIN (TREE_TYPE (sub
));
4956 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
4957 min_val
= TYPE_MIN_VALUE (type_domain
);
4958 return build4_loc (loc
, ARRAY_REF
, type
, sub
, min_val
, NULL_TREE
,
4966 cxx_eval_indirect_ref (const constexpr_ctx
*ctx
, tree t
,
4968 bool *non_constant_p
, bool *overflow_p
)
4970 tree orig_op0
= TREE_OPERAND (t
, 0);
4971 bool empty_base
= false;
4973 /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second
4974 operand is an integer-zero. Otherwise reject the MEM_REF for now. */
4976 if (TREE_CODE (t
) == MEM_REF
4977 && (!TREE_OPERAND (t
, 1) || !integer_zerop (TREE_OPERAND (t
, 1))))
4979 gcc_assert (ctx
->quiet
);
4980 *non_constant_p
= true;
4984 /* First try to simplify it directly. */
4985 tree r
= cxx_fold_indirect_ref (ctx
, EXPR_LOCATION (t
), TREE_TYPE (t
),
4986 orig_op0
, &empty_base
);
4989 /* If that didn't work, evaluate the operand first. */
4990 tree op0
= cxx_eval_constant_expression (ctx
, orig_op0
,
4991 /*lval*/false, non_constant_p
,
4993 /* Don't VERIFY_CONSTANT here. */
4994 if (*non_constant_p
)
4997 if (!lval
&& integer_zerop (op0
))
5000 error ("dereferencing a null pointer");
5001 *non_constant_p
= true;
5005 r
= cxx_fold_indirect_ref (ctx
, EXPR_LOCATION (t
), TREE_TYPE (t
), op0
,
5009 /* We couldn't fold to a constant value. Make sure it's not
5010 something we should have been able to fold. */
5013 if (TREE_CODE (sub
) == ADDR_EXPR
)
5015 gcc_assert (!similar_type_p
5016 (TREE_TYPE (TREE_TYPE (sub
)), TREE_TYPE (t
)));
5017 /* DR 1188 says we don't have to deal with this. */
5019 error_at (cp_expr_loc_or_input_loc (t
),
5020 "accessing value of %qE through a %qT glvalue in a "
5021 "constant expression", build_fold_indirect_ref (sub
),
5023 *non_constant_p
= true;
5027 if (lval
&& op0
!= orig_op0
)
5028 return build1 (INDIRECT_REF
, TREE_TYPE (t
), op0
);
5030 VERIFY_CONSTANT (t
);
5035 r
= cxx_eval_constant_expression (ctx
, r
,
5036 lval
, non_constant_p
, overflow_p
);
5037 if (*non_constant_p
)
5040 /* If we're pulling out the value of an empty base, just return an empty
5042 if (empty_base
&& !lval
)
5044 r
= build_constructor (TREE_TYPE (t
), NULL
);
5045 TREE_CONSTANT (r
) = true;
5051 /* Complain about R, a VAR_DECL, not being usable in a constant expression.
5052 Shared between potential_constant_expression and
5053 cxx_eval_constant_expression. */
5056 non_const_var_error (location_t loc
, tree r
)
5058 auto_diagnostic_group d
;
5059 tree type
= TREE_TYPE (r
);
5060 if (DECL_NAME (r
) == heap_uninit_identifier
5061 || DECL_NAME (r
) == heap_identifier
5062 || DECL_NAME (r
) == heap_vec_uninit_identifier
5063 || DECL_NAME (r
) == heap_vec_identifier
)
5065 error_at (loc
, "the content of uninitialized storage is not usable "
5066 "in a constant expression");
5067 inform (DECL_SOURCE_LOCATION (r
), "allocated here");
5070 if (DECL_NAME (r
) == heap_deleted_identifier
)
5072 error_at (loc
, "use of allocated storage after deallocation in a "
5073 "constant expression");
5074 inform (DECL_SOURCE_LOCATION (r
), "allocated here");
5077 error_at (loc
, "the value of %qD is not usable in a constant "
5079 /* Avoid error cascade. */
5080 if (DECL_INITIAL (r
) == error_mark_node
)
5082 if (DECL_DECLARED_CONSTEXPR_P (r
))
5083 inform (DECL_SOURCE_LOCATION (r
),
5084 "%qD used in its own initializer", r
);
5085 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5087 if (!CP_TYPE_CONST_P (type
))
5088 inform (DECL_SOURCE_LOCATION (r
),
5089 "%q#D is not const", r
);
5090 else if (CP_TYPE_VOLATILE_P (type
))
5091 inform (DECL_SOURCE_LOCATION (r
),
5092 "%q#D is volatile", r
);
5093 else if (!DECL_INITIAL (r
)
5094 || !TREE_CONSTANT (DECL_INITIAL (r
))
5095 || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
))
5096 inform (DECL_SOURCE_LOCATION (r
),
5097 "%qD was not initialized with a constant "
5102 else if (TYPE_REF_P (type
))
5103 inform (DECL_SOURCE_LOCATION (r
),
5104 "%qD was not initialized with a constant "
5108 if (cxx_dialect
>= cxx11
&& !DECL_DECLARED_CONSTEXPR_P (r
))
5109 inform (DECL_SOURCE_LOCATION (r
),
5110 "%qD was not declared %<constexpr%>", r
);
5112 inform (DECL_SOURCE_LOCATION (r
),
5113 "%qD does not have integral or enumeration type",
5118 /* Subroutine of cxx_eval_constant_expression.
5119 Like cxx_eval_unary_expression, except for trinary expressions. */
5122 cxx_eval_trinary_expression (const constexpr_ctx
*ctx
, tree t
,
5124 bool *non_constant_p
, bool *overflow_p
)
5130 for (i
= 0; i
< 3; i
++)
5132 args
[i
] = cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, i
),
5134 non_constant_p
, overflow_p
);
5135 VERIFY_CONSTANT (args
[i
]);
5138 val
= fold_ternary_loc (EXPR_LOCATION (t
), TREE_CODE (t
), TREE_TYPE (t
),
5139 args
[0], args
[1], args
[2]);
5140 if (val
== NULL_TREE
)
5142 VERIFY_CONSTANT (val
);
5146 /* True if T was declared in a function declared to be constexpr, and
5147 therefore potentially constant in C++14. */
5150 var_in_constexpr_fn (tree t
)
5152 tree ctx
= DECL_CONTEXT (t
);
5153 return (ctx
&& TREE_CODE (ctx
) == FUNCTION_DECL
5154 && DECL_DECLARED_CONSTEXPR_P (ctx
));
5157 /* True if a function might be constexpr: either a function that was
5158 declared constexpr, or a C++17 lambda op(). */
5161 maybe_constexpr_fn (tree t
)
5163 return (DECL_DECLARED_CONSTEXPR_P (t
)
5164 || (cxx_dialect
>= cxx17
&& LAMBDA_FUNCTION_P (t
)));
5167 /* True if T was declared in a function that might be constexpr: either a
5168 function that was declared constexpr, or a C++17 lambda op(). */
5171 var_in_maybe_constexpr_fn (tree t
)
5173 if (cxx_dialect
>= cxx17
5174 && DECL_FUNCTION_SCOPE_P (t
)
5175 && LAMBDA_FUNCTION_P (DECL_CONTEXT (t
)))
5177 return var_in_constexpr_fn (t
);
5180 /* We're assigning INIT to TARGET. In do_build_copy_constructor and
5181 build_over_call we implement trivial copy of a class with tail padding using
5182 assignment of character arrays, which is valid in normal code, but not in
5183 constexpr evaluation. We don't need to worry about clobbering tail padding
5184 in constexpr evaluation, so strip the type punning. */
5187 maybe_simplify_trivial_copy (tree
&target
, tree
&init
)
5189 if (TREE_CODE (target
) == MEM_REF
5190 && TREE_CODE (init
) == MEM_REF
5191 && TREE_TYPE (target
) == TREE_TYPE (init
)
5192 && TREE_CODE (TREE_TYPE (target
)) == ARRAY_TYPE
5193 && TREE_TYPE (TREE_TYPE (target
)) == unsigned_char_type_node
)
5195 target
= build_fold_indirect_ref (TREE_OPERAND (target
, 0));
5196 init
= build_fold_indirect_ref (TREE_OPERAND (init
, 0));
5200 /* Returns true if REF, which is a COMPONENT_REF, has any fields
5201 of constant type. This does not check for 'mutable', so the
5202 caller is expected to be mindful of that. */
5205 cref_has_const_field (tree ref
)
5207 while (TREE_CODE (ref
) == COMPONENT_REF
)
5209 if (CP_TYPE_CONST_P (TREE_TYPE (TREE_OPERAND (ref
, 1))))
5211 ref
= TREE_OPERAND (ref
, 0);
5216 /* Return true if we are modifying something that is const during constant
5217 expression evaluation. CODE is the code of the statement, OBJ is the
5218 object in question, MUTABLE_P is true if one of the subobjects were
5219 declared mutable. */
5222 modifying_const_object_p (tree_code code
, tree obj
, bool mutable_p
)
5224 /* If this is initialization, there's no problem. */
5225 if (code
!= MODIFY_EXPR
)
5228 /* [basic.type.qualifier] "A const object is an object of type
5229 const T or a non-mutable subobject of a const object." */
5233 if (TREE_READONLY (obj
))
5236 if (CP_TYPE_CONST_P (TREE_TYPE (obj
)))
5238 /* Although a COMPONENT_REF may have a const type, we should
5239 only consider it modifying a const object when any of the
5240 field components is const. This can happen when using
5241 constructs such as const_cast<const T &>(m), making something
5242 const even though it wasn't declared const. */
5243 if (TREE_CODE (obj
) == COMPONENT_REF
)
5244 return cref_has_const_field (obj
);
5252 /* Evaluate an INIT_EXPR or MODIFY_EXPR. */
5255 cxx_eval_store_expression (const constexpr_ctx
*ctx
, tree t
,
5257 bool *non_constant_p
, bool *overflow_p
)
5259 constexpr_ctx new_ctx
= *ctx
;
5261 tree init
= TREE_OPERAND (t
, 1);
5262 if (TREE_CLOBBER_P (init
))
5263 /* Just ignore clobbers. */
5266 /* First we figure out where we're storing to. */
5267 tree target
= TREE_OPERAND (t
, 0);
5269 maybe_simplify_trivial_copy (target
, init
);
5271 tree type
= TREE_TYPE (target
);
5272 bool preeval
= SCALAR_TYPE_P (type
) || TREE_CODE (t
) == MODIFY_EXPR
;
5275 /* Evaluate the value to be stored without knowing what object it will be
5276 stored in, so that any side-effects happen first. */
5277 if (!SCALAR_TYPE_P (type
))
5278 new_ctx
.ctor
= new_ctx
.object
= NULL_TREE
;
5279 init
= cxx_eval_constant_expression (&new_ctx
, init
, false,
5280 non_constant_p
, overflow_p
);
5281 if (*non_constant_p
)
5285 bool evaluated
= false;
5288 /* If we want to return a reference to the target, we need to evaluate it
5289 as a whole; otherwise, only evaluate the innermost piece to avoid
5290 building up unnecessary *_REFs. */
5291 target
= cxx_eval_constant_expression (ctx
, target
, true,
5292 non_constant_p
, overflow_p
);
5294 if (*non_constant_p
)
5298 /* Find the underlying variable. */
5300 tree object
= NULL_TREE
;
5301 /* If we're modifying a const object, save it. */
5302 tree const_object_being_modified
= NULL_TREE
;
5303 bool mutable_p
= false;
5304 for (tree probe
= target
; object
== NULL_TREE
; )
5306 switch (TREE_CODE (probe
))
5312 tree ob
= TREE_OPERAND (probe
, 0);
5313 tree elt
= TREE_OPERAND (probe
, 1);
5314 if (TREE_CODE (elt
) == FIELD_DECL
&& DECL_MUTABLE_P (elt
))
5316 if (TREE_CODE (probe
) == ARRAY_REF
)
5318 elt
= eval_and_check_array_index (ctx
, probe
, false,
5319 non_constant_p
, overflow_p
);
5320 if (*non_constant_p
)
5323 /* We don't check modifying_const_object_p for ARRAY_REFs. Given
5324 "int a[10]", an ARRAY_REF "a[2]" can be "const int", even though
5325 the array isn't const. Instead, check "a" in the next iteration;
5326 that will detect modifying "const int a[10]". */
5328 && modifying_const_object_p (TREE_CODE (t
), probe
,
5330 && const_object_being_modified
== NULL_TREE
)
5331 const_object_being_modified
= probe
;
5332 vec_safe_push (refs
, elt
);
5333 vec_safe_push (refs
, TREE_TYPE (probe
));
5343 probe
= cxx_eval_constant_expression (ctx
, probe
, true,
5344 non_constant_p
, overflow_p
);
5346 if (*non_constant_p
)
5353 if (modifying_const_object_p (TREE_CODE (t
), object
, mutable_p
)
5354 && const_object_being_modified
== NULL_TREE
)
5355 const_object_being_modified
= object
;
5357 /* And then find/build up our initializer for the path to the subobject
5358 we're initializing. */
5360 if (DECL_P (object
))
5361 valp
= ctx
->global
->values
.get (object
);
5366 /* A constant-expression cannot modify objects from outside the
5367 constant-expression. */
5369 error ("modification of %qE is not a constant expression", object
);
5370 *non_constant_p
= true;
5373 type
= TREE_TYPE (object
);
5374 bool no_zero_init
= true;
5376 releasing_vec ctors
, indexes
;
5377 auto_vec
<int> index_pos_hints
;
5378 bool activated_union_member_p
= false;
5379 while (!refs
->is_empty ())
5381 if (*valp
== NULL_TREE
)
5383 *valp
= build_constructor (type
, NULL
);
5384 CONSTRUCTOR_NO_CLEARING (*valp
) = no_zero_init
;
5386 else if (TREE_CODE (*valp
) == STRING_CST
)
5388 /* An array was initialized with a string constant, and now
5389 we're writing into one of its elements. Explode the
5390 single initialization into a set of element
5392 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
5394 tree string
= *valp
;
5395 tree elt_type
= TREE_TYPE (type
);
5396 unsigned chars_per_elt
= (TYPE_PRECISION (elt_type
)
5397 / TYPE_PRECISION (char_type_node
));
5398 unsigned num_elts
= TREE_STRING_LENGTH (string
) / chars_per_elt
;
5399 tree ary_ctor
= build_constructor (type
, NULL
);
5401 vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor
), num_elts
);
5402 for (unsigned ix
= 0; ix
!= num_elts
; ix
++)
5404 constructor_elt elt
=
5406 build_int_cst (size_type_node
, ix
),
5407 extract_string_elt (string
, chars_per_elt
, ix
)
5409 CONSTRUCTOR_ELTS (ary_ctor
)->quick_push (elt
);
5415 /* If the value of object is already zero-initialized, any new ctors for
5416 subobjects will also be zero-initialized. */
5417 no_zero_init
= CONSTRUCTOR_NO_CLEARING (*valp
);
5419 enum tree_code code
= TREE_CODE (type
);
5421 tree index
= refs
->pop();
5423 if (code
== RECORD_TYPE
&& is_empty_field (index
))
5424 /* Don't build a sub-CONSTRUCTOR for an empty base or field, as they
5425 have no data and might have an offset lower than previously declared
5426 fields, which confuses the middle-end. The code below will notice
5427 that we don't have a CONSTRUCTOR for our inner target and just
5431 if (code
== UNION_TYPE
&& CONSTRUCTOR_NELTS (*valp
)
5432 && CONSTRUCTOR_ELT (*valp
, 0)->index
!= index
)
5434 if (cxx_dialect
< cxx20
)
5437 error_at (cp_expr_loc_or_input_loc (t
),
5438 "change of the active member of a union "
5440 CONSTRUCTOR_ELT (*valp
, 0)->index
,
5442 *non_constant_p
= true;
5444 else if (TREE_CODE (t
) == MODIFY_EXPR
5445 && CONSTRUCTOR_NO_CLEARING (*valp
))
5447 /* Diagnose changing the active union member while the union
5448 is in the process of being initialized. */
5450 error_at (cp_expr_loc_or_input_loc (t
),
5451 "change of the active member of a union "
5452 "from %qD to %qD during initialization",
5453 CONSTRUCTOR_ELT (*valp
, 0)->index
,
5455 *non_constant_p
= true;
5457 no_zero_init
= true;
5460 vec_safe_push (ctors
, *valp
);
5461 vec_safe_push (indexes
, index
);
5463 constructor_elt
*cep
5464 = get_or_insert_ctor_field (*valp
, index
);
5465 index_pos_hints
.safe_push (cep
- CONSTRUCTOR_ELTS (*valp
)->begin());
5467 if (code
== UNION_TYPE
)
5468 activated_union_member_p
= true;
5473 /* Detect modifying a constant object in constexpr evaluation.
5474 We have found a const object that is being modified. Figure out
5475 if we need to issue an error. Consider
5479 constexpr A() : n(1) { n = 2; } // #1
5483 constexpr B() { a.n = 3; } // #2
5487 #1 is OK, since we're modifying an object under construction, but
5488 #2 is wrong, since "a" is const and has been fully constructed.
5489 To track it, we use the TREE_READONLY bit in the object's CONSTRUCTOR
5490 which means that the object is read-only. For the example above, the
5491 *ctors stack at the point of #2 will look like:
5493 ctors[0] = {.a={.n=2}} TREE_READONLY = 0
5494 ctors[1] = {.n=2} TREE_READONLY = 1
5496 and we're modifying "b.a", so we search the stack and see if the
5497 constructor for "b.a" has already run. */
5498 if (const_object_being_modified
)
5502 = strip_array_types (TREE_TYPE (const_object_being_modified
));
5503 if (!CLASS_TYPE_P (const_objtype
))
5507 /* [class.ctor]p5 "A constructor can be invoked for a const,
5508 volatile, or const volatile object. const and volatile
5509 semantics are not applied on an object under construction.
5510 They come into effect when the constructor for the most
5511 derived object ends." */
5512 for (tree elt
: *ctors
)
5513 if (same_type_ignoring_top_level_qualifiers_p
5514 (TREE_TYPE (const_object_being_modified
), TREE_TYPE (elt
)))
5516 fail
= TREE_READONLY (elt
);
5523 modifying_const_object_error (t
, const_object_being_modified
);
5524 *non_constant_p
= true;
5531 /* We're handling an INIT_EXPR of class type, so the value of the
5532 initializer can depend on the object it's initializing. */
5534 /* Create a new CONSTRUCTOR in case evaluation of the initializer
5535 wants to modify it. */
5536 if (*valp
== NULL_TREE
)
5538 *valp
= build_constructor (type
, NULL
);
5539 CONSTRUCTOR_NO_CLEARING (*valp
) = no_zero_init
;
5541 new_ctx
.ctor
= *valp
;
5542 new_ctx
.object
= target
;
5543 /* Avoid temporary materialization when initializing from a TARGET_EXPR.
5544 We don't need to mess with AGGR_EXPR_SLOT/VEC_INIT_EXPR_SLOT because
5545 expansion of those trees uses ctx instead. */
5546 if (TREE_CODE (init
) == TARGET_EXPR
)
5547 if (tree tinit
= TARGET_EXPR_INITIAL (init
))
5549 init
= cxx_eval_constant_expression (&new_ctx
, init
, false,
5550 non_constant_p
, overflow_p
);
5551 /* The hash table might have moved since the get earlier, and the
5552 initializer might have mutated the underlying CONSTRUCTORs, so we must
5554 valp
= ctx
->global
->values
.get (object
);
5555 for (unsigned i
= 0; i
< vec_safe_length (indexes
); i
++)
5557 constructor_elt
*cep
5558 = get_or_insert_ctor_field (*valp
, indexes
[i
], index_pos_hints
[i
]);
5563 /* Don't share a CONSTRUCTOR that might be changed later. */
5564 init
= unshare_constructor (init
);
5566 if (*valp
&& TREE_CODE (*valp
) == CONSTRUCTOR
5567 && TREE_CODE (init
) == CONSTRUCTOR
)
5569 /* An outer ctx->ctor might be pointing to *valp, so replace
5571 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init
),
5574 /* For initialization of an empty base, the original target will be
5575 *(base*)this, evaluation of which resolves to the object
5576 argument, which has the derived type rather than the base type. In
5577 this situation, just evaluate the initializer and return, since
5578 there's no actual data to store. */
5579 gcc_assert (is_empty_class (TREE_TYPE (init
)) && !lval
);
5582 CONSTRUCTOR_ELTS (*valp
) = CONSTRUCTOR_ELTS (init
);
5583 TREE_CONSTANT (*valp
) = TREE_CONSTANT (init
);
5584 TREE_SIDE_EFFECTS (*valp
) = TREE_SIDE_EFFECTS (init
);
5585 CONSTRUCTOR_NO_CLEARING (*valp
)
5586 = CONSTRUCTOR_NO_CLEARING (init
);
5588 else if (TREE_CODE (init
) == CONSTRUCTOR
5589 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init
),
5592 /* See above on initialization of empty bases. */
5593 gcc_assert (is_empty_class (TREE_TYPE (init
)) && !lval
);
5599 /* After initialization, 'const' semantics apply to the value of the
5600 object. Make a note of this fact by marking the CONSTRUCTOR
5602 if (TREE_CODE (t
) == INIT_EXPR
5603 && TREE_CODE (*valp
) == CONSTRUCTOR
5604 && TYPE_READONLY (type
))
5606 if (INDIRECT_REF_P (target
)
5607 && (is_this_parameter
5608 (tree_strip_nop_conversions (TREE_OPERAND (target
, 0)))))
5609 /* We've just initialized '*this' (perhaps via the target
5610 constructor of a delegating constructor). Leave it up to the
5611 caller that set 'this' to set TREE_READONLY appropriately. */
5612 gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
5613 (TREE_TYPE (target
), type
));
5615 TREE_READONLY (*valp
) = true;
5618 /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing
5619 CONSTRUCTORs, if any. */
5620 bool c
= TREE_CONSTANT (init
);
5621 bool s
= TREE_SIDE_EFFECTS (init
);
5622 if (!c
|| s
|| activated_union_member_p
)
5623 for (tree elt
: *ctors
)
5626 TREE_CONSTANT (elt
) = false;
5628 TREE_SIDE_EFFECTS (elt
) = true;
5629 /* Clear CONSTRUCTOR_NO_CLEARING since we've activated a member of
5631 if (TREE_CODE (TREE_TYPE (elt
)) == UNION_TYPE
)
5632 CONSTRUCTOR_NO_CLEARING (elt
) = false;
5635 if (*non_constant_p
)
5643 /* Evaluate a ++ or -- expression. */
5646 cxx_eval_increment_expression (const constexpr_ctx
*ctx
, tree t
,
5648 bool *non_constant_p
, bool *overflow_p
)
5650 enum tree_code code
= TREE_CODE (t
);
5651 tree type
= TREE_TYPE (t
);
5652 tree op
= TREE_OPERAND (t
, 0);
5653 tree offset
= TREE_OPERAND (t
, 1);
5654 gcc_assert (TREE_CONSTANT (offset
));
5656 /* OFFSET is constant, but perhaps not constant enough. We need to
5657 e.g. bash FLOAT_EXPRs to REAL_CSTs. */
5658 offset
= fold_simple (offset
);
5660 /* The operand as an lvalue. */
5661 op
= cxx_eval_constant_expression (ctx
, op
, true,
5662 non_constant_p
, overflow_p
);
5664 /* The operand as an rvalue. */
5666 = cxx_eval_constant_expression (ctx
, op
, false,
5667 non_constant_p
, overflow_p
);
5668 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
5669 a local array in a constexpr function. */
5670 bool ptr
= INDIRECT_TYPE_P (TREE_TYPE (val
));
5672 VERIFY_CONSTANT (val
);
5674 /* The modified value. */
5675 bool inc
= (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
);
5677 if (INDIRECT_TYPE_P (type
))
5679 /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
5680 offset
= convert_to_ptrofftype (offset
);
5682 offset
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (offset
), offset
);
5683 mod
= fold_build2 (POINTER_PLUS_EXPR
, type
, val
, offset
);
5686 mod
= fold_build2 (inc
? PLUS_EXPR
: MINUS_EXPR
, type
, val
, offset
);
5688 VERIFY_CONSTANT (mod
);
5690 /* Storing the modified value. */
5691 tree store
= build2_loc (cp_expr_loc_or_loc (t
, input_location
),
5692 MODIFY_EXPR
, type
, op
, mod
);
5693 mod
= cxx_eval_constant_expression (ctx
, store
, lval
,
5694 non_constant_p
, overflow_p
);
5696 if (*non_constant_p
)
5699 /* And the value of the expression. */
5700 if (code
== PREINCREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
5701 /* Prefix ops are lvalues, but the caller might want an rvalue;
5702 lval has already been taken into account in the store above. */
5705 /* Postfix ops are rvalues. */
5709 /* Predicates for the meaning of *jump_target. */
5712 returns (tree
*jump_target
)
5715 && (TREE_CODE (*jump_target
) == RETURN_EXPR
5716 || (TREE_CODE (*jump_target
) == LABEL_DECL
5717 && LABEL_DECL_CDTOR (*jump_target
)));
5721 breaks (tree
*jump_target
)
5724 && ((TREE_CODE (*jump_target
) == LABEL_DECL
5725 && LABEL_DECL_BREAK (*jump_target
))
5726 || TREE_CODE (*jump_target
) == BREAK_STMT
5727 || TREE_CODE (*jump_target
) == EXIT_EXPR
);
5731 continues (tree
*jump_target
)
5734 && ((TREE_CODE (*jump_target
) == LABEL_DECL
5735 && LABEL_DECL_CONTINUE (*jump_target
))
5736 || TREE_CODE (*jump_target
) == CONTINUE_STMT
);
5741 switches (tree
*jump_target
)
5744 && TREE_CODE (*jump_target
) == INTEGER_CST
;
5747 /* Subroutine of cxx_eval_statement_list. Determine whether the statement
5748 STMT matches *jump_target. If we're looking for a case label and we see
5749 the default label, note it in ctx->css_state. */
5752 label_matches (const constexpr_ctx
*ctx
, tree
*jump_target
, tree stmt
)
5754 switch (TREE_CODE (*jump_target
))
5757 if (TREE_CODE (stmt
) == LABEL_EXPR
5758 && LABEL_EXPR_LABEL (stmt
) == *jump_target
)
5763 if (TREE_CODE (stmt
) == CASE_LABEL_EXPR
)
5765 gcc_assert (ctx
->css_state
!= NULL
);
5766 if (!CASE_LOW (stmt
))
5768 /* default: should appear just once in a SWITCH_EXPR
5769 body (excluding nested SWITCH_EXPR). */
5770 gcc_assert (*ctx
->css_state
!= css_default_seen
);
5771 /* When evaluating SWITCH_EXPR body for the second time,
5772 return true for the default: label. */
5773 if (*ctx
->css_state
== css_default_processing
)
5775 *ctx
->css_state
= css_default_seen
;
5777 else if (CASE_HIGH (stmt
))
5779 if (tree_int_cst_le (CASE_LOW (stmt
), *jump_target
)
5780 && tree_int_cst_le (*jump_target
, CASE_HIGH (stmt
)))
5783 else if (tree_int_cst_equal (*jump_target
, CASE_LOW (stmt
)))
5790 /* These two are handled directly in cxx_eval_loop_expr by testing
5791 breaks (jump_target) or continues (jump_target). */
5800 /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
5801 semantics, for switch, break, continue, and return. */
5804 cxx_eval_statement_list (const constexpr_ctx
*ctx
, tree t
,
5805 bool *non_constant_p
, bool *overflow_p
,
5809 /* In a statement-expression we want to return the last value.
5810 For empty statement expression return void_node. */
5814 local_target
= NULL_TREE
;
5815 jump_target
= &local_target
;
5817 for (tree stmt
: tsi_range (t
))
5819 /* We've found a continue, so skip everything until we reach
5820 the label its jumping to. */
5821 if (continues (jump_target
))
5823 if (label_matches (ctx
, jump_target
, stmt
))
5825 *jump_target
= NULL_TREE
;
5829 if (TREE_CODE (stmt
) == DEBUG_BEGIN_STMT
)
5831 r
= cxx_eval_constant_expression (ctx
, stmt
, false,
5832 non_constant_p
, overflow_p
,
5834 if (*non_constant_p
)
5836 if (returns (jump_target
) || breaks (jump_target
))
5839 if (*jump_target
&& jump_target
== &local_target
)
5841 /* We aren't communicating the jump to our caller, so give up. We don't
5842 need to support evaluation of jumps out of statement-exprs. */
5844 error_at (cp_expr_loc_or_input_loc (r
),
5845 "statement is not a constant expression");
5846 *non_constant_p
= true;
5851 /* Evaluate a LOOP_EXPR for side-effects. Handles break and return
5852 semantics; continue semantics are covered by cxx_eval_statement_list. */
5855 cxx_eval_loop_expr (const constexpr_ctx
*ctx
, tree t
,
5856 bool *non_constant_p
, bool *overflow_p
,
5859 constexpr_ctx new_ctx
= *ctx
;
5863 local_target
= NULL_TREE
;
5864 jump_target
= &local_target
;
5867 tree body
, cond
= NULL_TREE
, expr
= NULL_TREE
;
5869 switch (TREE_CODE (t
))
5872 body
= LOOP_EXPR_BODY (t
);
5879 body
= WHILE_BODY (t
);
5880 cond
= WHILE_COND (t
);
5884 if (FOR_INIT_STMT (t
))
5885 cxx_eval_constant_expression (ctx
, FOR_INIT_STMT (t
), /*lval*/false,
5886 non_constant_p
, overflow_p
, jump_target
);
5887 if (*non_constant_p
)
5889 body
= FOR_BODY (t
);
5890 cond
= FOR_COND (t
);
5891 expr
= FOR_EXPR (t
);
5897 auto_vec
<tree
, 10> save_exprs
;
5898 new_ctx
.save_exprs
= &save_exprs
;
5904 cxx_eval_constant_expression (&new_ctx
, body
, /*lval*/false,
5905 non_constant_p
, overflow_p
,
5907 if (breaks (jump_target
))
5909 *jump_target
= NULL_TREE
;
5913 if (TREE_CODE (t
) != LOOP_EXPR
&& continues (jump_target
))
5914 *jump_target
= NULL_TREE
;
5917 cxx_eval_constant_expression (&new_ctx
, expr
, /*lval*/false,
5918 non_constant_p
, overflow_p
,
5925 = cxx_eval_constant_expression (&new_ctx
, cond
, /*lval*/false,
5926 non_constant_p
, overflow_p
,
5930 if (verify_constant (res
, ctx
->quiet
, non_constant_p
,
5933 if (integer_zerop (res
))
5937 gcc_assert (*jump_target
);
5940 /* Forget saved values of SAVE_EXPRs and TARGET_EXPRs. */
5941 for (tree save_expr
: save_exprs
)
5942 ctx
->global
->values
.remove (save_expr
);
5943 save_exprs
.truncate (0);
5945 if (++count
>= constexpr_loop_limit
)
5948 error_at (cp_expr_loc_or_input_loc (t
),
5949 "%<constexpr%> loop iteration count exceeds limit of %d "
5950 "(use %<-fconstexpr-loop-limit=%> to increase the limit)",
5951 constexpr_loop_limit
);
5952 *non_constant_p
= true;
5956 while (!returns (jump_target
)
5957 && !breaks (jump_target
)
5958 && !continues (jump_target
)
5959 && (!switches (jump_target
) || count
== 0)
5960 && !*non_constant_p
);
5962 /* Forget saved values of SAVE_EXPRs and TARGET_EXPRs. */
5963 for (tree save_expr
: save_exprs
)
5964 ctx
->global
->values
.remove (save_expr
);
5969 /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
5973 cxx_eval_switch_expr (const constexpr_ctx
*ctx
, tree t
,
5974 bool *non_constant_p
, bool *overflow_p
,
5978 = TREE_CODE (t
) == SWITCH_STMT
? SWITCH_STMT_COND (t
) : SWITCH_COND (t
);
5979 cond
= cxx_eval_constant_expression (ctx
, cond
, false,
5980 non_constant_p
, overflow_p
);
5981 VERIFY_CONSTANT (cond
);
5982 *jump_target
= cond
;
5985 = TREE_CODE (t
) == SWITCH_STMT
? SWITCH_STMT_BODY (t
) : SWITCH_BODY (t
);
5986 constexpr_ctx new_ctx
= *ctx
;
5987 constexpr_switch_state css
= css_default_not_seen
;
5988 new_ctx
.css_state
= &css
;
5989 cxx_eval_constant_expression (&new_ctx
, body
, false,
5990 non_constant_p
, overflow_p
, jump_target
);
5991 if (switches (jump_target
) && css
== css_default_seen
)
5993 /* If the SWITCH_EXPR body has default: label, process it once again,
5994 this time instructing label_matches to return true for default:
5995 label on switches (jump_target). */
5996 css
= css_default_processing
;
5997 cxx_eval_constant_expression (&new_ctx
, body
, false,
5998 non_constant_p
, overflow_p
, jump_target
);
6000 if (breaks (jump_target
) || switches (jump_target
))
6001 *jump_target
= NULL_TREE
;
6005 /* Find the object of TYPE under initialization in CTX. */
6008 lookup_placeholder (const constexpr_ctx
*ctx
, bool lval
, tree type
)
6013 /* Prefer the outermost matching object, but don't cross
6014 CONSTRUCTOR_PLACEHOLDER_BOUNDARY constructors. */
6015 if (ctx
->ctor
&& !CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx
->ctor
))
6016 if (tree outer_ob
= lookup_placeholder (ctx
->parent
, lval
, type
))
6019 /* We could use ctx->object unconditionally, but using ctx->ctor when we
6020 can is a minor optimization. */
6021 if (!lval
&& ctx
->ctor
&& same_type_p (TREE_TYPE (ctx
->ctor
), type
))
6027 /* Since an object cannot have a field of its own type, we can search outward
6028 from ctx->object to find the unique containing object of TYPE. */
6029 tree ob
= ctx
->object
;
6032 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob
), type
))
6034 if (handled_component_p (ob
))
6035 ob
= TREE_OPERAND (ob
, 0);
6043 /* Complain about an attempt to evaluate inline assembly. */
6046 inline_asm_in_constexpr_error (location_t loc
)
6048 auto_diagnostic_group d
;
6049 error_at (loc
, "inline assembly is not a constant expression");
6050 inform (loc
, "only unevaluated inline assembly is allowed in a "
6051 "%<constexpr%> function in C++20");
6054 /* Attempt to reduce the expression T to a constant value.
6055 On failure, issue diagnostic and return error_mark_node. */
6056 /* FIXME unify with c_fully_fold */
6057 /* FIXME overflow_p is too global */
6060 cxx_eval_constant_expression (const constexpr_ctx
*ctx
, tree t
,
6062 bool *non_constant_p
, bool *overflow_p
,
6063 tree
*jump_target
/* = NULL */)
6065 if (jump_target
&& *jump_target
)
6067 /* If we are jumping, ignore all statements/expressions except those
6068 that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */
6069 switch (TREE_CODE (t
))
6072 case STATEMENT_LIST
:
6081 case CASE_LABEL_EXPR
:
6082 if (label_matches (ctx
, jump_target
, t
))
6084 *jump_target
= NULL_TREE
;
6090 if (error_operand_p (t
))
6092 *non_constant_p
= true;
6096 location_t loc
= cp_expr_loc_or_input_loc (t
);
6098 STRIP_ANY_LOCATION_WRAPPER (t
);
6100 if (CONSTANT_CLASS_P (t
))
6102 if (TREE_OVERFLOW (t
))
6105 permerror (input_location
, "overflow in constant expression");
6106 if (!flag_permissive
|| ctx
->quiet
)
6110 if (TREE_CODE (t
) == INTEGER_CST
6111 && TYPE_PTR_P (TREE_TYPE (t
))
6112 && !integer_zerop (t
))
6115 error ("value %qE of type %qT is not a constant expression",
6117 *non_constant_p
= true;
6123 /* Avoid excessively long constexpr evaluations. */
6124 if (++ctx
->global
->constexpr_ops_count
>= constexpr_ops_limit
)
6128 "%<constexpr%> evaluation operation count exceeds limit of "
6129 "%wd (use %<-fconstexpr-ops-limit=%> to increase the limit)",
6130 constexpr_ops_limit
);
6131 ctx
->global
->constexpr_ops_count
= INTTYPE_MINIMUM (HOST_WIDE_INT
);
6132 *non_constant_p
= true;
6136 constexpr_ctx new_ctx
;
6139 tree_code tcode
= TREE_CODE (t
);
6145 /* We ask for an rvalue for the RESULT_DECL when indirecting
6146 through an invisible reference, or in named return value
6148 if (tree
*p
= ctx
->global
->values
.get (t
))
6153 error ("%qE is not a constant expression", t
);
6154 *non_constant_p
= true;
6159 if (DECL_HAS_VALUE_EXPR_P (t
))
6161 if (is_normal_capture_proxy (t
)
6162 && current_function_decl
== DECL_CONTEXT (t
))
6164 /* Function parms aren't constexpr within the function
6165 definition, so don't try to look at the closure. But if the
6166 captured variable is constant, try to evaluate it directly. */
6167 r
= DECL_CAPTURED_VARIABLE (t
);
6168 tree type
= TREE_TYPE (t
);
6169 if (TYPE_REF_P (type
) != TYPE_REF_P (TREE_TYPE (r
)))
6171 /* Adjust r to match the reference-ness of t. */
6172 if (TYPE_REF_P (type
))
6173 r
= build_address (r
);
6175 r
= convert_from_reference (r
);
6179 r
= DECL_VALUE_EXPR (t
);
6180 return cxx_eval_constant_expression (ctx
, r
, lval
, non_constant_p
,
6185 /* We used to not check lval for CONST_DECL, but darwin.c uses
6186 CONST_DECL for aggregate constants. */
6189 else if (t
== ctx
->object
)
6192 if (tree
*p
= ctx
->global
->values
.get (t
))
6193 if (*p
!= NULL_TREE
)
6198 if (COMPLETE_TYPE_P (TREE_TYPE (t
))
6199 && is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
6201 /* If the class is empty, we aren't actually loading anything. */
6202 r
= build_constructor (TREE_TYPE (t
), NULL
);
6203 TREE_CONSTANT (r
) = true;
6205 else if (ctx
->strict
)
6206 r
= decl_really_constant_value (t
, /*unshare_p=*/false);
6208 r
= decl_constant_value (t
, /*unshare_p=*/false);
6209 if (TREE_CODE (r
) == TARGET_EXPR
6210 && TREE_CODE (TARGET_EXPR_INITIAL (r
)) == CONSTRUCTOR
)
6211 r
= TARGET_EXPR_INITIAL (r
);
6215 non_const_var_error (loc
, r
);
6216 *non_constant_p
= true;
6220 case DEBUG_BEGIN_STMT
:
6221 /* ??? It might be nice to retain this information somehow, so
6222 as to be able to step into a constexpr function call. */
6229 case CASE_LABEL_EXPR
:
6234 if (lval
&& !TYPE_REF_P (TREE_TYPE (t
)))
6236 else if (tree
*p
= ctx
->global
->values
.get (r
))
6239 /* Defer in case this is only used for its type. */;
6240 else if (COMPLETE_TYPE_P (TREE_TYPE (t
))
6241 && is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
6243 /* If the class is empty, we aren't actually loading anything. */
6244 r
= build_constructor (TREE_TYPE (t
), NULL
);
6245 TREE_CONSTANT (r
) = true;
6250 error ("%qE is not a constant expression", t
);
6251 *non_constant_p
= true;
6256 case AGGR_INIT_EXPR
:
6257 r
= cxx_eval_call_expression (ctx
, t
, lval
,
6258 non_constant_p
, overflow_p
);
6263 r
= DECL_EXPR_DECL (t
);
6264 if (TREE_CODE (r
) == USING_DECL
)
6269 if (AGGREGATE_TYPE_P (TREE_TYPE (r
))
6270 || VECTOR_TYPE_P (TREE_TYPE (r
)))
6274 new_ctx
.ctor
= build_constructor (TREE_TYPE (r
), NULL
);
6275 CONSTRUCTOR_NO_CLEARING (new_ctx
.ctor
) = true;
6276 ctx
->global
->values
.put (r
, new_ctx
.ctor
);
6280 if (tree init
= DECL_INITIAL (r
))
6282 init
= cxx_eval_constant_expression (ctx
, init
,
6284 non_constant_p
, overflow_p
);
6285 /* Don't share a CONSTRUCTOR that might be changed. */
6286 init
= unshare_constructor (init
);
6287 /* Remember that a constant object's constructor has already
6289 if (CLASS_TYPE_P (TREE_TYPE (r
))
6290 && CP_TYPE_CONST_P (TREE_TYPE (r
)))
6291 TREE_READONLY (init
) = true;
6292 ctx
->global
->values
.put (r
, init
);
6294 else if (ctx
== &new_ctx
)
6295 /* We gave it a CONSTRUCTOR above. */;
6297 ctx
->global
->values
.put (r
, NULL_TREE
);
6303 tree type
= TREE_TYPE (t
);
6305 if (!literal_type_p (type
))
6309 auto_diagnostic_group d
;
6310 error ("temporary of non-literal type %qT in a "
6311 "constant expression", type
);
6312 explain_non_literal_class (type
);
6314 *non_constant_p
= true;
6317 gcc_checking_assert (!TARGET_EXPR_DIRECT_INIT_P (t
));
6318 /* Avoid evaluating a TARGET_EXPR more than once. */
6319 tree slot
= TARGET_EXPR_SLOT (t
);
6320 if (tree
*p
= ctx
->global
->values
.get (slot
))
6327 if ((AGGREGATE_TYPE_P (type
) || VECTOR_TYPE_P (type
)))
6329 /* We're being expanded without an explicit target, so start
6330 initializing a new object; expansion with an explicit target
6331 strips the TARGET_EXPR before we get here. */
6333 /* Link CTX to NEW_CTX so that lookup_placeholder can resolve
6334 any PLACEHOLDER_EXPR within the initializer that refers to the
6335 former object under construction. */
6336 new_ctx
.parent
= ctx
;
6337 new_ctx
.ctor
= build_constructor (type
, NULL
);
6338 CONSTRUCTOR_NO_CLEARING (new_ctx
.ctor
) = true;
6339 new_ctx
.object
= slot
;
6340 ctx
->global
->values
.put (new_ctx
.object
, new_ctx
.ctor
);
6343 /* Pass false for 'lval' because this indicates
6344 initialization of a temporary. */
6345 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
6347 non_constant_p
, overflow_p
);
6348 if (*non_constant_p
)
6350 /* Adjust the type of the result to the type of the temporary. */
6351 r
= adjust_temp_type (type
, r
);
6352 if (TARGET_EXPR_CLEANUP (t
) && !CLEANUP_EH_ONLY (t
))
6353 ctx
->global
->cleanups
->safe_push (TARGET_EXPR_CLEANUP (t
));
6354 r
= unshare_constructor (r
);
6355 ctx
->global
->values
.put (slot
, r
);
6356 if (ctx
->save_exprs
)
6357 ctx
->save_exprs
->safe_push (slot
);
6365 gcc_assert (jump_target
== NULL
|| *jump_target
== NULL_TREE
);
6366 r
= cxx_eval_store_expression (ctx
, t
, lval
,
6367 non_constant_p
, overflow_p
);
6371 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
6373 non_constant_p
, overflow_p
);
6377 if (TREE_OPERAND (t
, 0) != NULL_TREE
)
6378 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
6380 non_constant_p
, overflow_p
);
6388 /* Can happen with ({ return true; }) && false; passed to
6389 maybe_constant_value. There is nothing to jump over in this
6390 case, and the bug will be diagnosed later. */
6391 gcc_assert (ctx
->quiet
);
6392 *non_constant_p
= true;
6397 /* Avoid evaluating a SAVE_EXPR more than once. */
6398 if (tree
*p
= ctx
->global
->values
.get (t
))
6402 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), false,
6403 non_constant_p
, overflow_p
);
6404 if (*non_constant_p
)
6406 ctx
->global
->values
.put (t
, r
);
6407 if (ctx
->save_exprs
)
6408 ctx
->save_exprs
->safe_push (t
);
6412 case TRY_CATCH_EXPR
:
6413 if (TREE_OPERAND (t
, 0) == NULL_TREE
)
6419 case NON_LVALUE_EXPR
:
6421 case MUST_NOT_THROW_EXPR
:
6424 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
6426 non_constant_p
, overflow_p
,
6430 case CLEANUP_POINT_EXPR
:
6432 auto_vec
<tree
, 2> cleanups
;
6433 vec
<tree
> *prev_cleanups
= ctx
->global
->cleanups
;
6434 ctx
->global
->cleanups
= &cleanups
;
6435 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
6437 non_constant_p
, overflow_p
,
6439 ctx
->global
->cleanups
= prev_cleanups
;
6442 /* Evaluate the cleanups. */
6443 FOR_EACH_VEC_ELT_REVERSE (cleanups
, i
, cleanup
)
6444 cxx_eval_constant_expression (ctx
, cleanup
, false,
6445 non_constant_p
, overflow_p
);
6449 case TRY_FINALLY_EXPR
:
6450 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), lval
,
6451 non_constant_p
, overflow_p
,
6453 if (!*non_constant_p
)
6454 /* Also evaluate the cleanup. */
6455 cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1), true,
6456 non_constant_p
, overflow_p
);
6460 r
= cxx_eval_constant_expression (ctx
, CLEANUP_BODY (t
), lval
,
6461 non_constant_p
, overflow_p
,
6463 if (!CLEANUP_EH_ONLY (t
) && !*non_constant_p
)
6465 iloc_sentinel
ils (loc
);
6466 /* Also evaluate the cleanup. */
6467 cxx_eval_constant_expression (ctx
, CLEANUP_EXPR (t
), true,
6468 non_constant_p
, overflow_p
);
6472 /* These differ from cxx_eval_unary_expression in that this doesn't
6473 check for a constant operand or result; an address can be
6474 constant without its operand being, and vice versa. */
6477 r
= cxx_eval_indirect_ref (ctx
, t
, lval
,
6478 non_constant_p
, overflow_p
);
6483 tree oldop
= TREE_OPERAND (t
, 0);
6484 tree op
= cxx_eval_constant_expression (ctx
, oldop
,
6486 non_constant_p
, overflow_p
);
6487 /* Don't VERIFY_CONSTANT here. */
6488 if (*non_constant_p
)
6490 gcc_checking_assert (TREE_CODE (op
) != CONSTRUCTOR
);
6491 /* This function does more aggressive folding than fold itself. */
6492 r
= build_fold_addr_expr_with_type (op
, TREE_TYPE (t
));
6493 if (TREE_CODE (r
) == ADDR_EXPR
&& TREE_OPERAND (r
, 0) == oldop
)
6505 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), lval
,
6506 non_constant_p
, overflow_p
);
6507 if (r
== error_mark_node
)
6509 else if (r
== TREE_OPERAND (t
, 0))
6512 r
= fold_build1 (TREE_CODE (t
), TREE_TYPE (t
), r
);
6517 case FIX_TRUNC_EXPR
:
6523 case TRUTH_NOT_EXPR
:
6524 case FIXED_CONVERT_EXPR
:
6525 r
= cxx_eval_unary_expression (ctx
, t
, lval
,
6526 non_constant_p
, overflow_p
);
6530 r
= fold_sizeof_expr (t
);
6531 /* In a template, fold_sizeof_expr may merely create a new SIZEOF_EXPR,
6532 which could lead to an infinite recursion. */
6533 if (TREE_CODE (r
) != SIZEOF_EXPR
)
6534 r
= cxx_eval_constant_expression (ctx
, r
, lval
,
6535 non_constant_p
, overflow_p
,
6539 *non_constant_p
= true;
6540 gcc_assert (ctx
->quiet
);
6547 /* check_return_expr sometimes wraps a TARGET_EXPR in a
6548 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
6549 introduced by build_call_a. */
6550 tree op0
= TREE_OPERAND (t
, 0);
6551 tree op1
= TREE_OPERAND (t
, 1);
6553 if ((TREE_CODE (op0
) == TARGET_EXPR
&& op1
== TARGET_EXPR_SLOT (op0
))
6554 || TREE_CODE (op1
) == EMPTY_CLASS_EXPR
)
6555 r
= cxx_eval_constant_expression (ctx
, op0
,
6556 lval
, non_constant_p
, overflow_p
,
6560 /* Check that the LHS is constant and then discard it. */
6561 cxx_eval_constant_expression (ctx
, op0
,
6562 true, non_constant_p
, overflow_p
,
6564 if (*non_constant_p
)
6566 op1
= TREE_OPERAND (t
, 1);
6567 r
= cxx_eval_constant_expression (ctx
, op1
,
6568 lval
, non_constant_p
, overflow_p
,
6574 case POINTER_PLUS_EXPR
:
6575 case POINTER_DIFF_EXPR
:
6579 case TRUNC_DIV_EXPR
:
6581 case FLOOR_DIV_EXPR
:
6582 case ROUND_DIV_EXPR
:
6583 case TRUNC_MOD_EXPR
:
6585 case ROUND_MOD_EXPR
:
6587 case EXACT_DIV_EXPR
:
6597 case TRUTH_XOR_EXPR
:
6604 case SPACESHIP_EXPR
:
6605 case UNORDERED_EXPR
:
6615 r
= cxx_eval_binary_expression (ctx
, t
, lval
,
6616 non_constant_p
, overflow_p
);
6619 /* fold can introduce non-IF versions of these; still treat them as
6620 short-circuiting. */
6621 case TRUTH_AND_EXPR
:
6622 case TRUTH_ANDIF_EXPR
:
6623 r
= cxx_eval_logical_expression (ctx
, t
, boolean_false_node
,
6626 non_constant_p
, overflow_p
);
6630 case TRUTH_ORIF_EXPR
:
6631 r
= cxx_eval_logical_expression (ctx
, t
, boolean_true_node
,
6634 non_constant_p
, overflow_p
);
6638 r
= cxx_eval_array_reference (ctx
, t
, lval
,
6639 non_constant_p
, overflow_p
);
6643 if (is_overloaded_fn (t
))
6645 /* We can only get here in checking mode via
6646 build_non_dependent_expr, because any expression that
6647 calls or takes the address of the function will have
6648 pulled a FUNCTION_DECL out of the COMPONENT_REF. */
6649 gcc_checking_assert (ctx
->quiet
|| errorcount
);
6650 *non_constant_p
= true;
6653 r
= cxx_eval_component_reference (ctx
, t
, lval
,
6654 non_constant_p
, overflow_p
);
6658 r
= cxx_eval_bit_field_ref (ctx
, t
, lval
,
6659 non_constant_p
, overflow_p
);
6664 if (jump_target
&& *jump_target
)
6666 tree orig_jump
= *jump_target
;
6667 tree arg
= ((TREE_CODE (t
) != IF_STMT
|| TREE_OPERAND (t
, 1))
6668 ? TREE_OPERAND (t
, 1) : void_node
);
6669 /* When jumping to a label, the label might be either in the
6670 then or else blocks, so process then block first in skipping
6671 mode first, and if we are still in the skipping mode at its end,
6672 process the else block too. */
6673 r
= cxx_eval_constant_expression (ctx
, arg
, lval
, non_constant_p
,
6674 overflow_p
, jump_target
);
6675 /* It's possible that we found the label in the then block. But
6676 it could have been followed by another jumping statement, e.g.
6677 say we're looking for case 1:
6680 // skipped statements
6681 case 1:; // clears up *jump_target
6682 return 1; // and sets it to a RETURN_EXPR
6685 in which case we need not go looking to the else block.
6686 (goto is not allowed in a constexpr function.) */
6687 if (*jump_target
== orig_jump
)
6689 arg
= ((TREE_CODE (t
) != IF_STMT
|| TREE_OPERAND (t
, 2))
6690 ? TREE_OPERAND (t
, 2) : void_node
);
6691 r
= cxx_eval_constant_expression (ctx
, arg
, lval
, non_constant_p
,
6692 overflow_p
, jump_target
);
6696 r
= cxx_eval_conditional_expression (ctx
, t
, lval
,
6697 non_constant_p
, overflow_p
,
6701 r
= cxx_eval_vector_conditional_expression (ctx
, t
, non_constant_p
,
6706 if (TREE_CONSTANT (t
) && reduced_constant_expression_p (t
))
6708 /* Don't re-process a constant CONSTRUCTOR, but do fold it to
6709 VECTOR_CST if applicable. */
6710 verify_constructor_flags (t
);
6711 if (TREE_CONSTANT (t
))
6714 r
= cxx_eval_bare_aggregate (ctx
, t
, lval
,
6715 non_constant_p
, overflow_p
);
6719 /* We can get this in a defaulted constructor for a class with a
6720 non-static data member of array type. Either the initializer will
6721 be NULL, meaning default-initialization, or it will be an lvalue
6722 or xvalue of the same type, meaning direct-initialization from the
6723 corresponding member. */
6724 r
= cxx_eval_vec_init (ctx
, t
, lval
,
6725 non_constant_p
, overflow_p
);
6729 r
= cxx_eval_trinary_expression (ctx
, t
, lval
,
6730 non_constant_p
, overflow_p
);
6734 if (REINTERPRET_CAST_P (t
))
6738 "%<reinterpret_cast%> is not a constant expression");
6739 *non_constant_p
= true;
6744 case VIEW_CONVERT_EXPR
:
6745 case UNARY_PLUS_EXPR
:
6747 tree oldop
= TREE_OPERAND (t
, 0);
6749 tree op
= cxx_eval_constant_expression (ctx
, oldop
,
6751 non_constant_p
, overflow_p
);
6752 if (*non_constant_p
)
6754 tree type
= TREE_TYPE (t
);
6756 if (VOID_TYPE_P (type
))
6759 if (TREE_CODE (t
) == CONVERT_EXPR
6760 && ARITHMETIC_TYPE_P (type
)
6761 && INDIRECT_TYPE_P (TREE_TYPE (op
))
6762 && ctx
->manifestly_const_eval
)
6766 "conversion from pointer type %qT to arithmetic type "
6767 "%qT in a constant expression", TREE_TYPE (op
), type
);
6768 *non_constant_p
= true;
6772 /* [expr.const]: a conversion from type cv void* to a pointer-to-object
6773 type cannot be part of a core constant expression as a resolution to
6775 if (TYPE_PTROB_P (type
)
6776 && TYPE_PTR_P (TREE_TYPE (op
))
6777 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (op
)))
6778 /* Inside a call to std::construct_at or to
6779 std::allocator<T>::{,de}allocate, we permit casting from void*
6780 because that is compiler-generated code. */
6781 && !is_std_construct_at (ctx
->call
)
6782 && !is_std_allocator_allocate (ctx
->call
))
6784 /* Likewise, don't error when casting from void* when OP is
6785 &heap uninit and similar. */
6786 tree sop
= tree_strip_nop_conversions (op
);
6787 if (TREE_CODE (sop
) == ADDR_EXPR
6788 && VAR_P (TREE_OPERAND (sop
, 0))
6789 && DECL_ARTIFICIAL (TREE_OPERAND (sop
, 0)))
6794 error_at (loc
, "cast from %qT is not allowed",
6796 *non_constant_p
= true;
6801 if (TREE_CODE (op
) == PTRMEM_CST
&& !TYPE_PTRMEM_P (type
))
6802 op
= cplus_expand_constant (op
);
6804 if (TREE_CODE (op
) == PTRMEM_CST
&& tcode
== NOP_EXPR
)
6806 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (op
))
6807 && !can_convert_qual (type
, op
))
6808 op
= cplus_expand_constant (op
);
6809 return cp_fold_convert (type
, op
);
6812 if (INDIRECT_TYPE_P (type
) && TREE_CODE (op
) == INTEGER_CST
)
6814 if (integer_zerop (op
))
6816 if (TYPE_REF_P (type
))
6819 error_at (loc
, "dereferencing a null pointer");
6820 *non_constant_p
= true;
6826 /* This detects for example:
6827 reinterpret_cast<void*>(sizeof 0)
6830 error_at (loc
, "%<reinterpret_cast<%T>(%E)%> is not "
6831 "a constant expression",
6833 *non_constant_p
= true;
6838 if (INDIRECT_TYPE_P (type
)
6839 && TREE_CODE (op
) == NOP_EXPR
6840 && TREE_TYPE (op
) == ptr_type_node
6841 && TREE_CODE (TREE_OPERAND (op
, 0)) == ADDR_EXPR
6842 && VAR_P (TREE_OPERAND (TREE_OPERAND (op
, 0), 0))
6843 && (DECL_NAME (TREE_OPERAND (TREE_OPERAND (op
, 0),
6844 0)) == heap_uninit_identifier
6845 || DECL_NAME (TREE_OPERAND (TREE_OPERAND (op
, 0),
6846 0)) == heap_vec_uninit_identifier
))
6848 tree var
= TREE_OPERAND (TREE_OPERAND (op
, 0), 0);
6849 tree var_size
= TYPE_SIZE_UNIT (TREE_TYPE (var
));
6850 tree elt_type
= TREE_TYPE (type
);
6851 tree cookie_size
= NULL_TREE
;
6852 if (TREE_CODE (elt_type
) == RECORD_TYPE
6853 && TYPE_NAME (elt_type
) == heap_identifier
)
6855 tree fld1
= TYPE_FIELDS (elt_type
);
6856 tree fld2
= DECL_CHAIN (fld1
);
6857 elt_type
= TREE_TYPE (TREE_TYPE (fld2
));
6858 cookie_size
= TYPE_SIZE_UNIT (TREE_TYPE (fld1
));
6861 = (DECL_NAME (var
) == heap_uninit_identifier
6862 ? heap_identifier
: heap_vec_identifier
);
6864 = build_new_constexpr_heap_type (elt_type
, cookie_size
,
6866 TREE_TYPE (TREE_OPERAND (op
, 0))
6867 = build_pointer_type (TREE_TYPE (var
));
6870 if (op
== oldop
&& tcode
!= UNARY_PLUS_EXPR
)
6871 /* We didn't fold at the top so we could check for ptr-int
6877 /* Handle an array's bounds having been deduced after we built
6878 the wrapping expression. */
6879 if (same_type_ignoring_tlq_and_bounds_p (type
, TREE_TYPE (op
)))
6881 else if (sop
= tree_strip_nop_conversions (op
),
6882 sop
!= op
&& (same_type_ignoring_tlq_and_bounds_p
6883 (type
, TREE_TYPE (sop
))))
6885 else if (tcode
== UNARY_PLUS_EXPR
)
6886 r
= fold_convert (TREE_TYPE (t
), op
);
6888 r
= fold_build1 (tcode
, type
, op
);
6890 /* Conversion of an out-of-range value has implementation-defined
6891 behavior; the language considers it different from arithmetic
6892 overflow, which is undefined. */
6893 if (TREE_OVERFLOW_P (r
) && !TREE_OVERFLOW_P (op
))
6894 TREE_OVERFLOW (r
) = false;
6898 case EMPTY_CLASS_EXPR
:
6899 /* Handle EMPTY_CLASS_EXPR produced by build_call_a by lowering
6900 it to an appropriate CONSTRUCTOR. */
6901 return build_constructor (TREE_TYPE (t
), NULL
);
6903 case STATEMENT_LIST
:
6905 new_ctx
.ctor
= new_ctx
.object
= NULL_TREE
;
6906 return cxx_eval_statement_list (&new_ctx
, t
,
6907 non_constant_p
, overflow_p
, jump_target
);
6910 return cxx_eval_constant_expression (ctx
, BIND_EXPR_BODY (t
),
6912 non_constant_p
, overflow_p
,
6915 case PREINCREMENT_EXPR
:
6916 case POSTINCREMENT_EXPR
:
6917 case PREDECREMENT_EXPR
:
6918 case POSTDECREMENT_EXPR
:
6919 return cxx_eval_increment_expression (ctx
, t
,
6920 lval
, non_constant_p
, overflow_p
);
6926 case VEC_DELETE_EXPR
:
6929 /* GCC internal stuff. */
6931 case NON_DEPENDENT_EXPR
:
6935 error_at (loc
, "expression %qE is not a constant expression", t
);
6936 *non_constant_p
= true;
6940 /* Virtual function lookup. We don't need to do anything fancy. */
6941 return cxx_eval_constant_expression (ctx
, OBJ_TYPE_REF_EXPR (t
),
6942 lval
, non_constant_p
, overflow_p
);
6944 case PLACEHOLDER_EXPR
:
6945 /* Use of the value or address of the current object. */
6946 if (tree ctor
= lookup_placeholder (ctx
, lval
, TREE_TYPE (t
)))
6948 if (TREE_CODE (ctor
) == CONSTRUCTOR
)
6951 return cxx_eval_constant_expression (ctx
, ctor
, lval
,
6952 non_constant_p
, overflow_p
);
6954 /* A placeholder without a referent. We can get here when
6955 checking whether NSDMIs are noexcept, or in massage_init_elt;
6956 just say it's non-constant for now. */
6957 gcc_assert (ctx
->quiet
);
6958 *non_constant_p
= true;
6963 tree cond
= TREE_OPERAND (t
, 0);
6964 cond
= cxx_eval_constant_expression (ctx
, cond
, /*lval*/false,
6965 non_constant_p
, overflow_p
);
6966 VERIFY_CONSTANT (cond
);
6967 if (integer_nonzerop (cond
))
6973 *jump_target
= TREE_OPERAND (t
, 0);
6974 gcc_assert (breaks (jump_target
) || continues (jump_target
)
6975 /* Allow for jumping to a cdtor_label. */
6976 || returns (jump_target
));
6983 cxx_eval_loop_expr (ctx
, t
,
6984 non_constant_p
, overflow_p
, jump_target
);
6989 cxx_eval_switch_expr (ctx
, t
,
6990 non_constant_p
, overflow_p
, jump_target
);
6994 /* It's possible to get a requires-expression in a constant
6995 expression. For example:
6997 template<typename T> concept bool C() {
6998 return requires (T t) { t; };
7001 template<typename T> requires !C<T>() void f(T);
7003 Normalization leaves f with the associated constraint
7004 '!requires (T t) { ... }' which is not transformed into
7006 if (!processing_template_decl
)
7007 return evaluate_requires_expr (t
);
7009 *non_constant_p
= true;
7013 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
7015 non_constant_p
, overflow_p
,
7023 case TEMPLATE_ID_EXPR
:
7025 /* We can evaluate template-id that refers to a concept only if
7026 the template arguments are non-dependent. */
7027 tree id
= unpack_concept_check (t
);
7028 tree tmpl
= TREE_OPERAND (id
, 0);
7029 if (!concept_definition_p (tmpl
))
7030 internal_error ("unexpected template-id %qE", t
);
7032 if (function_concept_p (tmpl
))
7035 error_at (cp_expr_loc_or_input_loc (t
),
7036 "function concept must be called");
7037 r
= error_mark_node
;
7041 if (!processing_template_decl
7042 && !uid_sensitive_constexpr_evaluation_p ())
7043 r
= evaluate_concept_check (t
);
7045 *non_constant_p
= true;
7052 inline_asm_in_constexpr_error (loc
);
7053 *non_constant_p
= true;
7060 error_at (EXPR_LOCATION (t
),
7061 "address of a call to %qs is not a constant expression",
7062 "__builtin_bit_cast");
7063 *non_constant_p
= true;
7066 r
= cxx_eval_bit_cast (ctx
, t
, non_constant_p
, overflow_p
);
7070 if (STATEMENT_CODE_P (TREE_CODE (t
)))
7072 /* This function doesn't know how to deal with pre-genericize
7073 statements; this can only happen with statement-expressions,
7074 so for now just fail. */
7076 error_at (EXPR_LOCATION (t
),
7077 "statement is not a constant expression");
7080 internal_error ("unexpected expression %qE of kind %s", t
,
7081 get_tree_code_name (TREE_CODE (t
)));
7082 *non_constant_p
= true;
7086 if (r
== error_mark_node
)
7087 *non_constant_p
= true;
7089 if (*non_constant_p
)
7095 /* P0859: A function is needed for constant evaluation if it is a constexpr
7096 function that is named by an expression ([basic.def.odr]) that is
7097 potentially constant evaluated.
7099 So we need to instantiate any constexpr functions mentioned by the
7100 expression even if the definition isn't needed for evaluating the
7104 instantiate_cx_fn_r (tree
*tp
, int *walk_subtrees
, void */
*data*/
)
7106 if (TREE_CODE (*tp
) == FUNCTION_DECL
7107 && DECL_DECLARED_CONSTEXPR_P (*tp
)
7108 && !DECL_INITIAL (*tp
)
7109 && !trivial_fn_p (*tp
)
7110 && DECL_TEMPLOID_INSTANTIATION (*tp
)
7111 && !uid_sensitive_constexpr_evaluation_p ())
7114 instantiate_decl (*tp
, /*defer_ok*/false, /*expl_inst*/false);
7117 else if (TREE_CODE (*tp
) == CALL_EXPR
7118 || TREE_CODE (*tp
) == AGGR_INIT_EXPR
)
7120 if (EXPR_HAS_LOCATION (*tp
))
7121 input_location
= EXPR_LOCATION (*tp
);
7131 instantiate_constexpr_fns (tree t
)
7133 location_t loc
= input_location
;
7134 cp_walk_tree_without_duplicates (&t
, instantiate_cx_fn_r
, NULL
);
7135 input_location
= loc
;
7138 /* Look for heap variables in the expression *TP. */
7141 find_heap_var_refs (tree
*tp
, int *walk_subtrees
, void */
*data*/
)
7144 && (DECL_NAME (*tp
) == heap_uninit_identifier
7145 || DECL_NAME (*tp
) == heap_identifier
7146 || DECL_NAME (*tp
) == heap_vec_uninit_identifier
7147 || DECL_NAME (*tp
) == heap_vec_identifier
7148 || DECL_NAME (*tp
) == heap_deleted_identifier
))
7156 /* Find immediate function decls in *TP if any. */
7159 find_immediate_fndecl (tree
*tp
, int */
*walk_subtrees*/
, void */
*data*/
)
7161 if (TREE_CODE (*tp
) == FUNCTION_DECL
&& DECL_IMMEDIATE_FUNCTION_P (*tp
))
7166 /* ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
7167 STRICT has the same sense as for constant_value_1: true if we only allow
7168 conforming C++ constant expressions, or false if we want a constant value
7169 even if it doesn't conform.
7170 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
7171 per P0595 even when ALLOW_NON_CONSTANT is true.
7172 CONSTEXPR_DTOR is true when evaluating the dtor of a constexpr variable.
7173 OBJECT must be non-NULL in that case. */
7176 cxx_eval_outermost_constant_expr (tree t
, bool allow_non_constant
,
7178 bool manifestly_const_eval
= false,
7179 bool constexpr_dtor
= false,
7180 tree object
= NULL_TREE
)
7182 auto_timevar
time (TV_CONSTEXPR
);
7184 bool non_constant_p
= false;
7185 bool overflow_p
= false;
7187 if (BRACE_ENCLOSED_INITIALIZER_P (t
))
7189 gcc_checking_assert (allow_non_constant
);
7193 constexpr_global_ctx global_ctx
;
7194 constexpr_ctx ctx
= { &global_ctx
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
7195 allow_non_constant
, strict
,
7196 manifestly_const_eval
|| !allow_non_constant
};
7198 /* Turn off -frounding-math for manifestly constant evaluation. */
7199 warning_sentinel
rm (flag_rounding_math
, ctx
.manifestly_const_eval
);
7200 tree type
= initialized_type (t
);
7202 bool is_consteval
= false;
7203 if (VOID_TYPE_P (type
))
7206 /* Used for destructors of array elements. */
7207 type
= TREE_TYPE (object
);
7210 if (cxx_dialect
< cxx20
)
7212 if (TREE_CODE (t
) != CALL_EXPR
&& TREE_CODE (t
) != AGGR_INIT_EXPR
)
7214 /* Calls to immediate functions returning void need to be
7216 tree fndecl
= cp_get_callee_fndecl_nofold (t
);
7217 if (fndecl
== NULL_TREE
|| !DECL_IMMEDIATE_FUNCTION_P (fndecl
))
7220 is_consteval
= true;
7223 else if (cxx_dialect
>= cxx20
7224 && (TREE_CODE (t
) == CALL_EXPR
7225 || TREE_CODE (t
) == AGGR_INIT_EXPR
7226 || TREE_CODE (t
) == TARGET_EXPR
))
7228 /* For non-concept checks, determine if it is consteval. */
7229 if (!concept_check_p (t
))
7232 if (TREE_CODE (x
) == TARGET_EXPR
)
7233 x
= TARGET_EXPR_INITIAL (x
);
7234 tree fndecl
= cp_get_callee_fndecl_nofold (x
);
7235 if (fndecl
&& DECL_IMMEDIATE_FUNCTION_P (fndecl
))
7236 is_consteval
= true;
7239 if (AGGREGATE_TYPE_P (type
) || VECTOR_TYPE_P (type
))
7241 /* In C++14 an NSDMI can participate in aggregate initialization,
7242 and can refer to the address of the object being initialized, so
7243 we need to pass in the relevant VAR_DECL if we want to do the
7244 evaluation in a single pass. The evaluation will dynamically
7245 update ctx.values for the VAR_DECL. We use the same strategy
7246 for C++11 constexpr constructors that refer to the object being
7250 gcc_assert (object
&& VAR_P (object
));
7251 gcc_assert (DECL_DECLARED_CONSTEXPR_P (object
));
7252 gcc_assert (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object
));
7253 if (error_operand_p (DECL_INITIAL (object
)))
7255 ctx
.ctor
= unshare_expr (DECL_INITIAL (object
));
7256 TREE_READONLY (ctx
.ctor
) = false;
7257 /* Temporarily force decl_really_constant_value to return false
7258 for it, we want to use ctx.ctor for the current value instead. */
7259 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object
) = false;
7263 ctx
.ctor
= build_constructor (type
, NULL
);
7264 CONSTRUCTOR_NO_CLEARING (ctx
.ctor
) = true;
7268 if (TREE_CODE (t
) == TARGET_EXPR
)
7269 object
= TARGET_EXPR_SLOT (t
);
7270 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
)
7271 object
= AGGR_INIT_EXPR_SLOT (t
);
7273 ctx
.object
= object
;
7275 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7276 (type
, TREE_TYPE (object
)));
7277 if (object
&& DECL_P (object
))
7278 global_ctx
.values
.put (object
, ctx
.ctor
);
7279 if (TREE_CODE (r
) == TARGET_EXPR
)
7280 /* Avoid creating another CONSTRUCTOR when we expand the
7282 r
= TARGET_EXPR_INITIAL (r
);
7285 auto_vec
<tree
, 16> cleanups
;
7286 global_ctx
.cleanups
= &cleanups
;
7288 instantiate_constexpr_fns (r
);
7289 r
= cxx_eval_constant_expression (&ctx
, r
,
7290 false, &non_constant_p
, &overflow_p
);
7292 if (!constexpr_dtor
)
7293 verify_constant (r
, allow_non_constant
, &non_constant_p
, &overflow_p
);
7295 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object
) = true;
7299 /* Evaluate the cleanups. */
7300 FOR_EACH_VEC_ELT_REVERSE (cleanups
, i
, cleanup
)
7301 cxx_eval_constant_expression (&ctx
, cleanup
, false,
7302 &non_constant_p
, &overflow_p
);
7304 /* Mutable logic is a bit tricky: we want to allow initialization of
7305 constexpr variables with mutable members, but we can't copy those
7306 members to another constexpr variable. */
7307 if (TREE_CODE (r
) == CONSTRUCTOR
&& CONSTRUCTOR_MUTABLE_POISON (r
))
7309 if (!allow_non_constant
)
7310 error ("%qE is not a constant expression because it refers to "
7311 "mutable subobjects of %qT", t
, type
);
7312 non_constant_p
= true;
7315 if (TREE_CODE (r
) == CONSTRUCTOR
&& CONSTRUCTOR_NO_CLEARING (r
))
7317 if (!allow_non_constant
)
7318 error ("%qE is not a constant expression because it refers to "
7319 "an incompletely initialized variable", t
);
7320 TREE_CONSTANT (r
) = false;
7321 non_constant_p
= true;
7324 if (!global_ctx
.heap_vars
.is_empty ())
7326 tree heap_var
= cp_walk_tree_without_duplicates (&r
, find_heap_var_refs
,
7331 if (!allow_non_constant
&& !non_constant_p
)
7332 error_at (DECL_SOURCE_LOCATION (heap_var
),
7333 "%qE is not a constant expression because it refers to "
7334 "a result of %<operator new%>", t
);
7336 non_constant_p
= true;
7338 FOR_EACH_VEC_ELT (global_ctx
.heap_vars
, i
, heap_var
)
7340 if (DECL_NAME (heap_var
) != heap_deleted_identifier
)
7342 if (!allow_non_constant
&& !non_constant_p
)
7343 error_at (DECL_SOURCE_LOCATION (heap_var
),
7344 "%qE is not a constant expression because allocated "
7345 "storage has not been deallocated", t
);
7347 non_constant_p
= true;
7349 varpool_node::get (heap_var
)->remove ();
7353 /* Check that immediate invocation does not return an expression referencing
7354 any immediate function decls. They need to be allowed while parsing
7355 immediate functions, but can't leak outside of them. */
7358 && (current_function_decl
== NULL_TREE
7359 || !DECL_IMMEDIATE_FUNCTION_P (current_function_decl
)))
7360 if (tree immediate_fndecl
7361 = cp_walk_tree_without_duplicates (&r
, find_immediate_fndecl
,
7364 if (!allow_non_constant
&& !non_constant_p
)
7365 error_at (cp_expr_loc_or_input_loc (t
),
7366 "immediate evaluation returns address of immediate "
7367 "function %qD", immediate_fndecl
);
7369 non_constant_p
= true;
7373 /* If we saw something bad, go back to our argument. The wrapping below is
7374 only for the cases of TREE_CONSTANT argument or overflow. */
7377 if (!non_constant_p
&& overflow_p
)
7378 non_constant_p
= true;
7380 /* Unshare the result. */
7381 bool should_unshare
= true;
7382 if (r
== t
|| (TREE_CODE (t
) == TARGET_EXPR
7383 && TARGET_EXPR_INITIAL (t
) == r
))
7384 should_unshare
= false;
7386 if (non_constant_p
&& !allow_non_constant
)
7387 return error_mark_node
;
7388 else if (constexpr_dtor
)
7390 else if (non_constant_p
&& TREE_CONSTANT (r
))
7392 /* This isn't actually constant, so unset TREE_CONSTANT.
7393 Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires
7394 it to be set if it is invariant address, even when it is not
7395 a valid C++ constant expression. Wrap it with a NOP_EXPR
7397 if (EXPR_P (r
) && TREE_CODE (r
) != ADDR_EXPR
)
7399 else if (TREE_CODE (r
) == CONSTRUCTOR
)
7400 r
= build1 (VIEW_CONVERT_EXPR
, TREE_TYPE (r
), r
);
7402 r
= build_nop (TREE_TYPE (r
), r
);
7403 TREE_CONSTANT (r
) = false;
7405 else if (non_constant_p
)
7409 r
= unshare_expr (r
);
7411 if (TREE_CODE (r
) == CONSTRUCTOR
&& CLASS_TYPE_P (TREE_TYPE (r
)))
7413 r
= adjust_temp_type (type
, r
);
7414 if (TREE_CODE (t
) == TARGET_EXPR
7415 && TARGET_EXPR_INITIAL (t
) == r
)
7417 else if (TREE_CODE (t
) != CONSTRUCTOR
)
7419 r
= get_target_expr_sfinae (r
, tf_warning_or_error
| tf_no_cleanup
);
7420 TREE_CONSTANT (r
) = true;
7427 /* If T represents a constant expression returns its reduced value.
7428 Otherwise return error_mark_node. If T is dependent, then
7432 cxx_constant_value (tree t
, tree decl
)
7434 return cxx_eval_outermost_constant_expr (t
, false, true, true, false, decl
);
7437 /* Like cxx_constant_value, but used for evaluation of constexpr destructors
7438 of constexpr variables. The actual initializer of DECL is not modified. */
7441 cxx_constant_dtor (tree t
, tree decl
)
7443 cxx_eval_outermost_constant_expr (t
, false, true, true, true, decl
);
7446 /* Helper routine for fold_simple function. Either return simplified
7447 expression T, otherwise NULL_TREE.
7448 In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold
7449 even if we are within template-declaration. So be careful on call, as in
7450 such case types can be undefined. */
7453 fold_simple_1 (tree t
)
7456 enum tree_code code
= TREE_CODE (t
);
7468 return fold_sizeof_expr (t
);
7477 case TRUTH_NOT_EXPR
:
7479 case VIEW_CONVERT_EXPR
:
7482 case FIX_TRUNC_EXPR
:
7483 case FIXED_CONVERT_EXPR
:
7484 case ADDR_SPACE_CONVERT_EXPR
:
7486 op1
= TREE_OPERAND (t
, 0);
7488 t
= const_unop (code
, TREE_TYPE (t
), op1
);
7492 if (CONVERT_EXPR_CODE_P (code
)
7493 && TREE_OVERFLOW_P (t
) && !TREE_OVERFLOW_P (op1
))
7494 TREE_OVERFLOW (t
) = false;
7502 /* If T is a simple constant expression, returns its simplified value.
7503 Otherwise returns T. In contrast to maybe_constant_value we
7504 simplify only few operations on constant-expressions, and we don't
7505 try to simplify constexpressions. */
7508 fold_simple (tree t
)
7510 if (processing_template_decl
)
7513 tree r
= fold_simple_1 (t
);
7520 /* If T is a constant expression, returns its reduced value.
7521 Otherwise, if T does not have TREE_CONSTANT set, returns T.
7522 Otherwise, returns a version of T without TREE_CONSTANT.
7523 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated
7526 static GTY((deletable
)) hash_map
<tree
, tree
> *cv_cache
;
7529 maybe_constant_value (tree t
, tree decl
, bool manifestly_const_eval
)
7533 if (!is_nondependent_constant_expression (t
))
7535 if (TREE_OVERFLOW_P (t
))
7537 t
= build_nop (TREE_TYPE (t
), t
);
7538 TREE_CONSTANT (t
) = false;
7542 else if (CONSTANT_CLASS_P (t
))
7543 /* No caching or evaluation needed. */
7546 if (manifestly_const_eval
)
7547 return cxx_eval_outermost_constant_expr (t
, true, true, true, false, decl
);
7549 if (cv_cache
== NULL
)
7550 cv_cache
= hash_map
<tree
, tree
>::create_ggc (101);
7551 if (tree
*cached
= cv_cache
->get (t
))
7556 r
= break_out_target_exprs (r
, /*clear_loc*/true);
7557 protected_set_expr_location (r
, EXPR_LOCATION (t
));
7562 uid_sensitive_constexpr_evaluation_checker c
;
7563 r
= cxx_eval_outermost_constant_expr (t
, true, true, false, false, decl
);
7564 gcc_checking_assert (r
== t
7565 || CONVERT_EXPR_P (t
)
7566 || TREE_CODE (t
) == VIEW_CONVERT_EXPR
7567 || (TREE_CONSTANT (t
) && !TREE_CONSTANT (r
))
7568 || !cp_tree_equal (r
, t
));
7569 if (!c
.evaluation_restricted_p ())
7570 cv_cache
->put (t
, r
);
7574 /* Dispose of the whole CV_CACHE. */
7577 clear_cv_cache (void)
7579 if (cv_cache
!= NULL
)
7583 /* Dispose of the whole CV_CACHE and FOLD_CACHE. */
7586 clear_cv_and_fold_caches ()
7589 clear_fold_cache ();
7592 /* Internal function handling expressions in templates for
7593 fold_non_dependent_expr and fold_non_dependent_init.
7595 If we're in a template, but T isn't value dependent, simplify
7596 it. We're supposed to treat:
7598 template <typename T> void f(T[1 + 1]);
7599 template <typename T> void f(T[2]);
7601 as two declarations of the same function, for example. */
7604 fold_non_dependent_expr_template (tree t
, tsubst_flags_t complain
,
7605 bool manifestly_const_eval
,
7608 gcc_assert (processing_template_decl
);
7610 if (is_nondependent_constant_expression (t
))
7612 processing_template_decl_sentinel s
;
7613 t
= instantiate_non_dependent_expr_internal (t
, complain
);
7615 if (type_unknown_p (t
) || BRACE_ENCLOSED_INITIALIZER_P (t
))
7617 if (TREE_OVERFLOW_P (t
))
7619 t
= build_nop (TREE_TYPE (t
), t
);
7620 TREE_CONSTANT (t
) = false;
7625 tree r
= cxx_eval_outermost_constant_expr (t
, true, true,
7626 manifestly_const_eval
,
7628 /* cp_tree_equal looks through NOPs, so allow them. */
7629 gcc_checking_assert (r
== t
7630 || CONVERT_EXPR_P (t
)
7631 || TREE_CODE (t
) == VIEW_CONVERT_EXPR
7632 || (TREE_CONSTANT (t
) && !TREE_CONSTANT (r
))
7633 || !cp_tree_equal (r
, t
));
7636 else if (TREE_OVERFLOW_P (t
))
7638 t
= build_nop (TREE_TYPE (t
), t
);
7639 TREE_CONSTANT (t
) = false;
7645 /* Like maybe_constant_value but first fully instantiate the argument.
7647 Note: this is equivalent to instantiate_non_dependent_expr_sfinae
7648 (t, complain) followed by maybe_constant_value but is more efficient,
7649 because it calls instantiation_dependent_expression_p and
7650 potential_constant_expression at most once.
7651 The manifestly_const_eval argument is passed to maybe_constant_value.
7653 Callers should generally pass their active complain, or if they are in a
7654 non-template, diagnosing context, they can use the default of
7655 tf_warning_or_error. Callers that might be within a template context, don't
7656 have a complain parameter, and aren't going to remember the result for long
7657 (e.g. null_ptr_cst_p), can pass tf_none and deal with error_mark_node
7661 fold_non_dependent_expr (tree t
,
7662 tsubst_flags_t complain
/* = tf_warning_or_error */,
7663 bool manifestly_const_eval
/* = false */,
7664 tree object
/* = NULL_TREE */)
7669 if (processing_template_decl
)
7670 return fold_non_dependent_expr_template (t
, complain
,
7671 manifestly_const_eval
, object
);
7673 return maybe_constant_value (t
, object
, manifestly_const_eval
);
7676 /* Like fold_non_dependent_expr, but if EXPR couldn't be folded to a constant,
7677 return the original expression. */
7680 maybe_fold_non_dependent_expr (tree expr
,
7681 tsubst_flags_t complain
/*=tf_warning_or_error*/)
7683 tree t
= fold_non_dependent_expr (expr
, complain
);
7684 if (t
&& TREE_CONSTANT (t
))
7690 /* Like maybe_constant_init but first fully instantiate the argument. */
7693 fold_non_dependent_init (tree t
,
7694 tsubst_flags_t complain
/*=tf_warning_or_error*/,
7695 bool manifestly_const_eval
/*=false*/,
7696 tree object
/* = NULL_TREE */)
7701 if (processing_template_decl
)
7703 t
= fold_non_dependent_expr_template (t
, complain
,
7704 manifestly_const_eval
, object
);
7705 /* maybe_constant_init does this stripping, so do it here too. */
7706 if (TREE_CODE (t
) == TARGET_EXPR
)
7708 tree init
= TARGET_EXPR_INITIAL (t
);
7709 if (TREE_CODE (init
) == CONSTRUCTOR
)
7715 return maybe_constant_init (t
, object
, manifestly_const_eval
);
7718 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
7719 than wrapped in a TARGET_EXPR.
7720 ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
7721 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
7722 per P0595 even when ALLOW_NON_CONSTANT is true. */
7725 maybe_constant_init_1 (tree t
, tree decl
, bool allow_non_constant
,
7726 bool manifestly_const_eval
)
7730 if (TREE_CODE (t
) == EXPR_STMT
)
7731 t
= TREE_OPERAND (t
, 0);
7732 if (TREE_CODE (t
) == CONVERT_EXPR
7733 && VOID_TYPE_P (TREE_TYPE (t
)))
7734 t
= TREE_OPERAND (t
, 0);
7735 if (TREE_CODE (t
) == INIT_EXPR
)
7736 t
= TREE_OPERAND (t
, 1);
7737 if (TREE_CODE (t
) == TARGET_EXPR
)
7738 t
= TARGET_EXPR_INITIAL (t
);
7739 if (!is_nondependent_static_init_expression (t
))
7740 /* Don't try to evaluate it. */;
7741 else if (CONSTANT_CLASS_P (t
) && allow_non_constant
)
7742 /* No evaluation needed. */;
7744 t
= cxx_eval_outermost_constant_expr (t
, allow_non_constant
,
7746 manifestly_const_eval
, false, decl
);
7747 if (TREE_CODE (t
) == TARGET_EXPR
)
7749 tree init
= TARGET_EXPR_INITIAL (t
);
7750 if (TREE_CODE (init
) == CONSTRUCTOR
)
7756 /* Wrapper for maybe_constant_init_1 which permits non constants. */
7759 maybe_constant_init (tree t
, tree decl
, bool manifestly_const_eval
)
7761 return maybe_constant_init_1 (t
, decl
, true, manifestly_const_eval
);
7764 /* Wrapper for maybe_constant_init_1 which does not permit non constants. */
7767 cxx_constant_init (tree t
, tree decl
)
7769 return maybe_constant_init_1 (t
, decl
, false, true);
7773 /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
7774 /* Return true if the object referred to by REF has automatic or thread
7777 enum { ck_ok
, ck_bad
, ck_unknown
};
7779 check_automatic_or_tls (tree ref
)
7782 poly_int64 bitsize
, bitpos
;
7784 int volatilep
= 0, unsignedp
= 0;
7785 tree decl
= get_inner_reference (ref
, &bitsize
, &bitpos
, &offset
,
7786 &mode
, &unsignedp
, &volatilep
, false);
7789 /* If there isn't a decl in the middle, we don't know the linkage here,
7790 and this isn't a constant expression anyway. */
7793 dk
= decl_storage_duration (decl
);
7794 return (dk
== dk_auto
|| dk
== dk_thread
) ? ck_bad
: ck_ok
;
7798 /* Data structure for passing data from potential_constant_expression_1
7799 to check_for_return_continue via cp_walk_tree. */
7800 struct check_for_return_continue_data
{
7801 hash_set
<tree
> *pset
;
7806 /* Helper function for potential_constant_expression_1 SWITCH_STMT handling,
7807 called through cp_walk_tree. Return the first RETURN_EXPR found, or note
7808 the first CONTINUE_STMT and/or BREAK_STMT if RETURN_EXPR is not found. */
7810 check_for_return_continue (tree
*tp
, int *walk_subtrees
, void *data
)
7813 check_for_return_continue_data
*d
= (check_for_return_continue_data
*) data
;
7814 switch (TREE_CODE (t
))
7820 if (d
->continue_stmt
== NULL_TREE
)
7821 d
->continue_stmt
= t
;
7825 if (d
->break_stmt
== NULL_TREE
)
7830 if (tree r = cp_walk_tree (&x, check_for_return_continue, data, \
7834 /* For loops, walk subtrees manually, so that continue stmts found
7835 inside of the bodies of the loops are ignored. */
7838 RECUR (DO_COND (t
));
7839 s
= d
->continue_stmt
;
7841 RECUR (DO_BODY (t
));
7842 d
->continue_stmt
= s
;
7848 RECUR (WHILE_COND (t
));
7849 s
= d
->continue_stmt
;
7851 RECUR (WHILE_BODY (t
));
7852 d
->continue_stmt
= s
;
7858 RECUR (FOR_INIT_STMT (t
));
7859 RECUR (FOR_COND (t
));
7860 RECUR (FOR_EXPR (t
));
7861 s
= d
->continue_stmt
;
7863 RECUR (FOR_BODY (t
));
7864 d
->continue_stmt
= s
;
7868 case RANGE_FOR_STMT
:
7870 RECUR (RANGE_FOR_EXPR (t
));
7871 s
= d
->continue_stmt
;
7873 RECUR (RANGE_FOR_BODY (t
));
7874 d
->continue_stmt
= s
;
7880 RECUR (SWITCH_STMT_COND (t
));
7882 RECUR (SWITCH_STMT_BODY (t
));
7887 case STATEMENT_LIST
:
7900 /* Return true if T denotes a potentially constant expression. Issue
7901 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
7902 an lvalue-rvalue conversion is implied. If NOW is true, we want to
7903 consider the expression in the current context, independent of constexpr
7906 C++0x [expr.const] used to say
7908 6 An expression is a potential constant expression if it is
7909 a constant expression where all occurrences of function
7910 parameters are replaced by arbitrary constant expressions
7911 of the appropriate type.
7913 2 A conditional expression is a constant expression unless it
7914 involves one of the following as a potentially evaluated
7915 subexpression (3.2), but subexpressions of logical AND (5.14),
7916 logical OR (5.15), and conditional (5.16) operations that are
7917 not evaluated are not considered. */
7920 potential_constant_expression_1 (tree t
, bool want_rval
, bool strict
, bool now
,
7921 tsubst_flags_t flags
, tree
*jump_target
)
7923 #define RECUR(T,RV) \
7924 potential_constant_expression_1 ((T), (RV), strict, now, flags, jump_target)
7926 enum { any
= false, rval
= true };
7930 if (t
== error_mark_node
)
7934 location_t loc
= cp_expr_loc_or_input_loc (t
);
7937 /* If we are jumping, ignore everything. This is simpler than the
7938 cxx_eval_constant_expression handling because we only need to be
7939 conservatively correct, and we don't necessarily have a constant value
7940 available, so we don't bother with switch tracking. */
7943 if (TREE_THIS_VOLATILE (t
) && want_rval
)
7945 if (flags
& tf_error
)
7946 error_at (loc
, "lvalue-to-rvalue conversion of a volatile lvalue "
7947 "%qE with type %qT", t
, TREE_TYPE (t
));
7950 if (CONSTANT_CLASS_P (t
))
7952 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_TYPED
)
7953 && TREE_TYPE (t
) == error_mark_node
)
7956 switch (TREE_CODE (t
))
7962 case TEMPLATE_ID_EXPR
:
7964 case CASE_LABEL_EXPR
:
7971 case TEMPLATE_PARM_INDEX
:
7973 case IDENTIFIER_NODE
:
7974 case USERDEF_LITERAL
:
7975 /* We can see a FIELD_DECL in a pointer-to-member expression. */
7980 case PLACEHOLDER_EXPR
:
7983 case DEBUG_BEGIN_STMT
:
7987 if (!RECUR (TREE_OPERAND (t
, 0), any
))
7997 if (now
&& want_rval
)
7999 tree type
= TREE_TYPE (t
);
8000 if ((processing_template_decl
&& !COMPLETE_TYPE_P (type
))
8001 || dependent_type_p (type
)
8002 || is_really_empty_class (type
, /*ignore_vptr*/false))
8003 /* An empty class has no data to read. */
8005 if (flags
& tf_error
)
8006 error ("%qE is not a constant expression", t
);
8011 case AGGR_INIT_EXPR
:
8013 /* -- an invocation of a function other than a constexpr function
8014 or a constexpr constructor. */
8016 tree fun
= get_function_named_in_call (t
);
8017 const int nargs
= call_expr_nargs (t
);
8020 if (fun
== NULL_TREE
)
8022 /* Reset to allow the function to continue past the end
8023 of the block below. Otherwise return early. */
8026 if (TREE_CODE (t
) == CALL_EXPR
8027 && CALL_EXPR_FN (t
) == NULL_TREE
)
8028 switch (CALL_EXPR_IFN (t
))
8030 /* These should be ignored, they are optimized away from
8031 constexpr functions. */
8032 case IFN_UBSAN_NULL
:
8033 case IFN_UBSAN_BOUNDS
:
8034 case IFN_UBSAN_VPTR
:
8035 case IFN_FALLTHROUGH
:
8038 case IFN_ADD_OVERFLOW
:
8039 case IFN_SUB_OVERFLOW
:
8040 case IFN_MUL_OVERFLOW
:
8042 case IFN_VEC_CONVERT
:
8052 /* fold_call_expr can't do anything with IFN calls. */
8053 if (flags
& tf_error
)
8054 error_at (loc
, "call to internal function %qE", t
);
8059 if (fun
&& is_overloaded_fn (fun
))
8061 if (TREE_CODE (fun
) == FUNCTION_DECL
)
8063 if (builtin_valid_in_constant_expr_p (fun
))
8065 if (!DECL_DECLARED_CONSTEXPR_P (fun
)
8066 /* Allow any built-in function; if the expansion
8067 isn't constant, we'll deal with that then. */
8068 && !fndecl_built_in_p (fun
)
8069 /* In C++20, replaceable global allocation functions
8070 are constant expressions. */
8071 && (!cxx_replaceable_global_alloc_fn (fun
)
8072 || TREE_CODE (t
) != CALL_EXPR
8073 || (!CALL_FROM_NEW_OR_DELETE_P (t
)
8074 && (current_function_decl
== NULL_TREE
8075 || !is_std_allocator_allocate
8076 (current_function_decl
))))
8077 /* Allow placement new in std::construct_at. */
8078 && (!cxx_placement_new_fn (fun
)
8079 || TREE_CODE (t
) != CALL_EXPR
8080 || current_function_decl
== NULL_TREE
8081 || !is_std_construct_at (current_function_decl
))
8082 && !cxx_dynamic_cast_fn_p (fun
))
8084 if (flags
& tf_error
)
8086 error_at (loc
, "call to non-%<constexpr%> function %qD",
8088 explain_invalid_constexpr_fn (fun
);
8092 /* A call to a non-static member function takes the address
8093 of the object as the first argument. But in a constant
8094 expression the address will be folded away, so look
8096 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun
)
8097 && !DECL_CONSTRUCTOR_P (fun
))
8099 tree x
= get_nth_callarg (t
, 0);
8100 if (is_this_parameter (x
))
8102 /* Don't require an immediately constant value, as
8103 constexpr substitution might not use the value. */
8104 bool sub_now
= false;
8105 if (!potential_constant_expression_1 (x
, rval
, strict
,
8114 if (!RECUR (fun
, true))
8116 fun
= get_first_fn (fun
);
8118 /* Skip initial arguments to base constructors. */
8119 if (DECL_BASE_CONSTRUCTOR_P (fun
))
8120 i
= num_artificial_parms_for (fun
);
8121 fun
= DECL_ORIGIN (fun
);
8125 if (RECUR (fun
, rval
))
8126 /* Might end up being a constant function pointer. */;
8130 for (; i
< nargs
; ++i
)
8132 tree x
= get_nth_callarg (t
, i
);
8133 /* In a template, reference arguments haven't been converted to
8134 REFERENCE_TYPE and we might not even know if the parameter
8135 is a reference, so accept lvalue constants too. */
8136 bool rv
= processing_template_decl
? any
: rval
;
8137 /* Don't require an immediately constant value, as constexpr
8138 substitution might not use the value of the argument. */
8139 bool sub_now
= false;
8140 if (!potential_constant_expression_1 (x
, rv
, strict
,
8141 sub_now
, flags
, jump_target
))
8147 case NON_LVALUE_EXPR
:
8148 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
8149 -- an lvalue of integral type that refers to a non-volatile
8150 const variable or static data member initialized with
8151 constant expressions, or
8153 -- an lvalue of literal type that refers to non-volatile
8154 object defined with constexpr, or that refers to a
8155 sub-object of such an object; */
8156 return RECUR (TREE_OPERAND (t
, 0), rval
);
8159 if (DECL_HAS_VALUE_EXPR_P (t
))
8161 if (now
&& is_normal_capture_proxy (t
))
8163 /* -- in a lambda-expression, a reference to this or to a
8164 variable with automatic storage duration defined outside that
8165 lambda-expression, where the reference would be an
8169 /* Since we're doing an lvalue-rvalue conversion, this might
8170 not be an odr-use, so evaluate the variable directly. */
8171 return RECUR (DECL_CAPTURED_VARIABLE (t
), rval
);
8173 if (flags
& tf_error
)
8175 tree cap
= DECL_CAPTURED_VARIABLE (t
);
8176 error ("lambda capture of %qE is not a constant expression",
8178 if (decl_constant_var_p (cap
))
8179 inform (input_location
, "because it is used as a glvalue");
8183 /* Treat __PRETTY_FUNCTION__ inside a template function as
8184 potentially-constant. */
8185 else if (DECL_PRETTY_FUNCTION_P (t
)
8186 && DECL_VALUE_EXPR (t
) == error_mark_node
)
8188 return RECUR (DECL_VALUE_EXPR (t
), rval
);
8191 && !var_in_maybe_constexpr_fn (t
)
8192 && !type_dependent_expression_p (t
)
8193 && !decl_maybe_constant_var_p (t
)
8195 || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t
))
8196 || (DECL_INITIAL (t
)
8197 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t
)))
8198 && COMPLETE_TYPE_P (TREE_TYPE (t
))
8199 && !is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
8201 if (flags
& tf_error
)
8202 non_const_var_error (loc
, t
);
8208 if (REINTERPRET_CAST_P (t
))
8210 if (flags
& tf_error
)
8211 error_at (loc
, "%<reinterpret_cast%> is not a constant expression");
8216 case VIEW_CONVERT_EXPR
:
8217 /* -- a reinterpret_cast. FIXME not implemented, and this rule
8218 may change to something more specific to type-punning (DR 1312). */
8220 tree from
= TREE_OPERAND (t
, 0);
8221 if (location_wrapper_p (t
))
8222 return (RECUR (from
, want_rval
));
8223 if (INDIRECT_TYPE_P (TREE_TYPE (t
)))
8225 STRIP_ANY_LOCATION_WRAPPER (from
);
8226 if (TREE_CODE (from
) == INTEGER_CST
8227 && !integer_zerop (from
))
8229 if (flags
& tf_error
)
8231 "%<reinterpret_cast%> from integer to pointer");
8235 return (RECUR (from
, TREE_CODE (t
) != VIEW_CONVERT_EXPR
));
8238 case ADDRESSOF_EXPR
:
8239 /* This is like ADDR_EXPR, except it won't form pointer-to-member. */
8240 t
= TREE_OPERAND (t
, 0);
8241 goto handle_addr_expr
;
8244 /* -- a unary operator & that is applied to an lvalue that
8245 designates an object with thread or automatic storage
8247 t
= TREE_OPERAND (t
, 0);
8249 if (TREE_CODE (t
) == OFFSET_REF
&& PTRMEM_OK_P (t
))
8250 /* A pointer-to-member constant. */
8255 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
8256 any checking here, as we might dereference the pointer later. If
8257 we remove this code, also remove check_automatic_or_tls. */
8258 i
= check_automatic_or_tls (t
);
8263 if (flags
& tf_error
)
8264 error ("address-of an object %qE with thread local or "
8265 "automatic storage is not a constant expression", t
);
8269 return RECUR (t
, any
);
8274 /* -- a class member access unless its postfix-expression is
8275 of literal type or of pointer to literal type. */
8276 /* This test would be redundant, as it follows from the
8277 postfix-expression being a potential constant expression. */
8278 if (type_unknown_p (t
))
8280 if (is_overloaded_fn (t
))
8281 /* In a template, a COMPONENT_REF of a function expresses ob.fn(),
8282 which uses ob as an lvalue. */
8289 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
8291 case EXPR_PACK_EXPANSION
:
8292 return RECUR (PACK_EXPANSION_PATTERN (t
), want_rval
);
8296 tree x
= TREE_OPERAND (t
, 0);
8298 if (is_this_parameter (x
) && !is_capture_proxy (x
))
8300 if (!var_in_maybe_constexpr_fn (x
))
8302 if (flags
& tf_error
)
8303 error_at (loc
, "use of %<this%> in a constant expression");
8308 return RECUR (x
, rval
);
8311 case STATEMENT_LIST
:
8312 for (tree stmt
: tsi_range (t
))
8313 if (!RECUR (stmt
, any
))
8318 if (cxx_dialect
< cxx14
)
8320 if (!RECUR (TREE_OPERAND (t
, 0), any
))
8322 /* Just ignore clobbers. */
8323 if (TREE_CLOBBER_P (TREE_OPERAND (t
, 1)))
8325 if (!RECUR (TREE_OPERAND (t
, 1), rval
))
8330 if (cxx_dialect
< cxx14
)
8332 if (!RECUR (TREE_OPERAND (t
, 0), rval
))
8334 if (!RECUR (TREE_OPERAND (t
, 2), rval
))
8339 if (!RECUR (DO_COND (t
), rval
))
8341 if (!RECUR (DO_BODY (t
), any
))
8343 if (breaks (jump_target
) || continues (jump_target
))
8344 *jump_target
= NULL_TREE
;
8348 if (!RECUR (FOR_INIT_STMT (t
), any
))
8351 if (!RECUR (tmp
, rval
))
8355 if (!processing_template_decl
)
8356 tmp
= cxx_eval_outermost_constant_expr (tmp
, true);
8357 /* If we couldn't evaluate the condition, it might not ever be
8359 if (!integer_onep (tmp
))
8361 /* Before returning true, check if the for body can contain
8363 hash_set
<tree
> pset
;
8364 check_for_return_continue_data data
= { &pset
, NULL_TREE
,
8367 = cp_walk_tree (&FOR_BODY (t
), check_for_return_continue
,
8369 *jump_target
= ret_expr
;
8373 if (!RECUR (FOR_EXPR (t
), any
))
8375 if (!RECUR (FOR_BODY (t
), any
))
8377 if (breaks (jump_target
) || continues (jump_target
))
8378 *jump_target
= NULL_TREE
;
8381 case RANGE_FOR_STMT
:
8382 if (!RECUR (RANGE_FOR_INIT_STMT (t
), any
))
8384 if (!RECUR (RANGE_FOR_EXPR (t
), any
))
8386 if (!RECUR (RANGE_FOR_BODY (t
), any
))
8388 if (breaks (jump_target
) || continues (jump_target
))
8389 *jump_target
= NULL_TREE
;
8393 tmp
= WHILE_COND (t
);
8394 if (!RECUR (tmp
, rval
))
8396 if (!processing_template_decl
)
8397 tmp
= cxx_eval_outermost_constant_expr (tmp
, true);
8398 /* If we couldn't evaluate the condition, it might not ever be true. */
8399 if (!integer_onep (tmp
))
8401 /* Before returning true, check if the while body can contain
8403 hash_set
<tree
> pset
;
8404 check_for_return_continue_data data
= { &pset
, NULL_TREE
,
8407 = cp_walk_tree (&WHILE_BODY (t
), check_for_return_continue
,
8409 *jump_target
= ret_expr
;
8412 if (!RECUR (WHILE_BODY (t
), any
))
8414 if (breaks (jump_target
) || continues (jump_target
))
8415 *jump_target
= NULL_TREE
;
8419 if (!RECUR (SWITCH_STMT_COND (t
), rval
))
8421 /* FIXME we don't check SWITCH_STMT_BODY currently, because even
8422 unreachable labels would be checked and it is enough if there is
8423 a single switch cond value for which it is a valid constant
8424 expression. We need to check if there are any RETURN_EXPRs
8425 or CONTINUE_STMTs inside of the body though, as in that case
8426 we need to set *jump_target. */
8429 hash_set
<tree
> pset
;
8430 check_for_return_continue_data data
= { &pset
, NULL_TREE
,
8433 = cp_walk_tree (&SWITCH_STMT_BODY (t
), check_for_return_continue
,
8435 /* The switch might return. */
8436 *jump_target
= ret_expr
;
8437 else if (data
.continue_stmt
)
8438 /* The switch can't return, but might continue. */
8439 *jump_target
= data
.continue_stmt
;
8444 return RECUR (STMT_EXPR_STMT (t
), rval
);
8447 if (cxx_dialect
>= cxx17
)
8448 /* In C++17 lambdas can be constexpr, don't give up yet. */
8450 else if (flags
& tf_error
)
8451 error_at (loc
, "lambda-expression is not a constant expression "
8455 case DYNAMIC_CAST_EXPR
:
8456 case PSEUDO_DTOR_EXPR
:
8460 case VEC_DELETE_EXPR
:
8466 case OMP_DISTRIBUTE
:
8470 case OMP_TARGET_DATA
:
8479 case OMP_TARGET_UPDATE
:
8480 case OMP_TARGET_ENTER_DATA
:
8481 case OMP_TARGET_EXIT_DATA
:
8483 case OMP_ATOMIC_READ
:
8484 case OMP_ATOMIC_CAPTURE_OLD
:
8485 case OMP_ATOMIC_CAPTURE_NEW
:
8491 case OACC_HOST_DATA
:
8495 case OACC_ENTER_DATA
:
8496 case OACC_EXIT_DATA
:
8498 /* GCC internal stuff. */
8500 case TRANSACTION_EXPR
:
8501 case AT_ENCODE_EXPR
:
8503 if (flags
& tf_error
)
8504 error_at (loc
, "expression %qE is not a constant expression", t
);
8508 if (flags
& tf_error
)
8509 inline_asm_in_constexpr_error (loc
);
8513 if (cxx_dialect
>= cxx20
)
8514 /* In C++20 virtual calls can be constexpr, don't give up yet. */
8516 else if (flags
& tf_error
)
8518 "virtual functions cannot be %<constexpr%> before C++20");
8522 /* In C++20, a typeid expression whose operand is of polymorphic
8523 class type can be constexpr. */
8525 tree e
= TREE_OPERAND (t
, 0);
8526 if (cxx_dialect
< cxx20
8529 && !type_dependent_expression_p (e
)
8530 && TYPE_POLYMORPHIC_P (TREE_TYPE (e
)))
8532 if (flags
& tf_error
)
8533 error_at (loc
, "%<typeid%> is not a constant expression "
8534 "because %qE is of polymorphic type", e
);
8540 case POINTER_DIFF_EXPR
:
8551 case SPACESHIP_EXPR
:
8555 case PREINCREMENT_EXPR
:
8556 case POSTINCREMENT_EXPR
:
8557 case PREDECREMENT_EXPR
:
8558 case POSTDECREMENT_EXPR
:
8559 if (cxx_dialect
< cxx14
)
8565 if (TYPE_P (TREE_OPERAND (t
, 0)))
8571 case FIX_TRUNC_EXPR
:
8576 case TRUTH_NOT_EXPR
:
8577 case FIXED_CONVERT_EXPR
:
8578 case UNARY_PLUS_EXPR
:
8579 case UNARY_LEFT_FOLD_EXPR
:
8580 case UNARY_RIGHT_FOLD_EXPR
:
8582 return RECUR (TREE_OPERAND (t
, 0), rval
);
8585 case CONST_CAST_EXPR
:
8586 case STATIC_CAST_EXPR
:
8587 case REINTERPRET_CAST_EXPR
:
8588 case IMPLICIT_CONV_EXPR
:
8589 if (cxx_dialect
< cxx11
8590 && !dependent_type_p (TREE_TYPE (t
))
8591 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
)))
8592 /* In C++98, a conversion to non-integral type can't be part of a
8593 constant expression. */
8595 if (flags
& tf_error
)
8597 "cast to non-integral type %qT in a constant expression",
8601 /* This might be a conversion from a class to a (potentially) literal
8602 type. Let's consider it potentially constant since the conversion
8603 might be a constexpr user-defined conversion. */
8604 else if (cxx_dialect
>= cxx11
8605 && (dependent_type_p (TREE_TYPE (t
))
8606 || !COMPLETE_TYPE_P (TREE_TYPE (t
))
8607 || literal_type_p (TREE_TYPE (t
)))
8608 && TREE_OPERAND (t
, 0))
8610 tree type
= TREE_TYPE (TREE_OPERAND (t
, 0));
8611 /* If this is a dependent type, it could end up being a class
8612 with conversions. */
8613 if (type
== NULL_TREE
|| WILDCARD_TYPE_P (type
))
8615 /* Or a non-dependent class which has conversions. */
8616 else if (CLASS_TYPE_P (type
)
8617 && (TYPE_HAS_CONVERSION (type
) || dependent_scope_p (type
)))
8621 return (RECUR (TREE_OPERAND (t
, 0),
8622 !TYPE_REF_P (TREE_TYPE (t
))));
8625 return RECUR (BIND_EXPR_BODY (t
), want_rval
);
8627 case CLEANUP_POINT_EXPR
:
8628 case MUST_NOT_THROW_EXPR
:
8629 case TRY_CATCH_EXPR
:
8634 case NON_DEPENDENT_EXPR
:
8635 /* For convenience. */
8638 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
8641 tmp
= DECL_EXPR_DECL (t
);
8642 if (VAR_P (tmp
) && !DECL_ARTIFICIAL (tmp
))
8644 if (TREE_STATIC (tmp
))
8646 if (flags
& tf_error
)
8647 error_at (DECL_SOURCE_LOCATION (tmp
), "%qD declared "
8648 "%<static%> in %<constexpr%> context", tmp
);
8651 else if (CP_DECL_THREAD_LOCAL_P (tmp
))
8653 if (flags
& tf_error
)
8654 error_at (DECL_SOURCE_LOCATION (tmp
), "%qD declared "
8655 "%<thread_local%> in %<constexpr%> context", tmp
);
8658 else if (!check_for_uninitialized_const_var
8659 (tmp
, /*constexpr_context_p=*/true, flags
))
8662 return RECUR (tmp
, want_rval
);
8664 case TRY_FINALLY_EXPR
:
8665 return (RECUR (TREE_OPERAND (t
, 0), want_rval
)
8666 && RECUR (TREE_OPERAND (t
, 1), any
));
8669 return RECUR (TREE_OPERAND (t
, 1), want_rval
);
8672 if (!TARGET_EXPR_DIRECT_INIT_P (t
)
8673 && !literal_type_p (TREE_TYPE (t
)))
8675 if (flags
& tf_error
)
8677 auto_diagnostic_group d
;
8678 error_at (loc
, "temporary of non-literal type %qT in a "
8679 "constant expression", TREE_TYPE (t
));
8680 explain_non_literal_class (TREE_TYPE (t
));
8686 return RECUR (TREE_OPERAND (t
, 1), rval
);
8690 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (t
);
8691 constructor_elt
*ce
;
8692 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
8693 if (!RECUR (ce
->value
, want_rval
))
8700 gcc_assert (TREE_PURPOSE (t
) == NULL_TREE
8701 || DECL_P (TREE_PURPOSE (t
)));
8702 if (!RECUR (TREE_VALUE (t
), want_rval
))
8704 if (TREE_CHAIN (t
) == NULL_TREE
)
8706 return RECUR (TREE_CHAIN (t
), want_rval
);
8709 case TRUNC_DIV_EXPR
:
8711 case FLOOR_DIV_EXPR
:
8712 case ROUND_DIV_EXPR
:
8713 case TRUNC_MOD_EXPR
:
8715 case ROUND_MOD_EXPR
:
8717 tree denom
= TREE_OPERAND (t
, 1);
8718 if (!RECUR (denom
, rval
))
8720 /* We can't call cxx_eval_outermost_constant_expr on an expression
8721 that hasn't been through instantiate_non_dependent_expr yet. */
8722 if (!processing_template_decl
)
8723 denom
= cxx_eval_outermost_constant_expr (denom
, true);
8724 if (integer_zerop (denom
))
8726 if (flags
& tf_error
)
8727 error ("division by zero is not a constant expression");
8733 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
8739 /* check_return_expr sometimes wraps a TARGET_EXPR in a
8740 COMPOUND_EXPR; don't get confused. */
8741 tree op0
= TREE_OPERAND (t
, 0);
8742 tree op1
= TREE_OPERAND (t
, 1);
8744 if (TREE_CODE (op0
) == TARGET_EXPR
&& op1
== TARGET_EXPR_SLOT (op0
))
8745 return RECUR (op0
, want_rval
);
8750 /* If the first operand is the non-short-circuit constant, look at
8751 the second operand; otherwise we only care about the first one for
8753 case TRUTH_AND_EXPR
:
8754 case TRUTH_ANDIF_EXPR
:
8755 tmp
= boolean_true_node
;
8758 case TRUTH_ORIF_EXPR
:
8759 tmp
= boolean_false_node
;
8762 tree op
= TREE_OPERAND (t
, 0);
8763 if (!RECUR (op
, rval
))
8765 if (!processing_template_decl
)
8766 op
= cxx_eval_outermost_constant_expr (op
, true);
8767 if (tree_int_cst_equal (op
, tmp
))
8768 return RECUR (TREE_OPERAND (t
, 1), rval
);
8775 case POINTER_PLUS_EXPR
:
8777 case EXACT_DIV_EXPR
:
8787 case TRUTH_XOR_EXPR
:
8788 case UNORDERED_EXPR
:
8801 case ARRAY_RANGE_REF
:
8805 case BINARY_LEFT_FOLD_EXPR
:
8806 case BINARY_RIGHT_FOLD_EXPR
:
8808 for (i
= 0; i
< 2; ++i
)
8809 if (!RECUR (TREE_OPERAND (t
, i
), want_rval
))
8814 for (i
= 0; i
< 3; ++i
)
8815 if (!RECUR (TREE_OPERAND (t
, i
), true))
8820 if (COND_EXPR_IS_VEC_DELETE (t
) && cxx_dialect
< cxx20
)
8822 if (flags
& tf_error
)
8823 error_at (loc
, "%<delete[]%> is not a constant expression");
8829 /* If the condition is a known constant, we know which of the legs we
8830 care about; otherwise we only require that the condition and
8831 either of the legs be potentially constant. */
8832 tmp
= TREE_OPERAND (t
, 0);
8833 if (!RECUR (tmp
, rval
))
8835 if (!processing_template_decl
)
8836 tmp
= cxx_eval_outermost_constant_expr (tmp
, true);
8837 /* potential_constant_expression* isn't told if it is called for
8838 manifestly_const_eval or not, so for consteval if always
8839 process both branches as if the condition is not a known
8841 if (TREE_CODE (t
) != IF_STMT
|| !IF_STMT_CONSTEVAL_P (t
))
8843 if (integer_zerop (tmp
))
8844 return RECUR (TREE_OPERAND (t
, 2), want_rval
);
8845 else if (TREE_CODE (tmp
) == INTEGER_CST
)
8846 return RECUR (TREE_OPERAND (t
, 1), want_rval
);
8849 for (i
= 1; i
< 3; ++i
)
8851 tree this_jump_target
= tmp
;
8852 if (potential_constant_expression_1 (TREE_OPERAND (t
, i
),
8853 want_rval
, strict
, now
,
8854 tf_none
, &this_jump_target
))
8856 if (returns (&this_jump_target
))
8857 *jump_target
= this_jump_target
;
8858 else if (!returns (jump_target
))
8860 if (breaks (&this_jump_target
)
8861 || continues (&this_jump_target
))
8862 *jump_target
= this_jump_target
;
8865 /* If the then branch is potentially constant, but
8866 does not return, check if the else branch
8867 couldn't return, break or continue. */
8868 hash_set
<tree
> pset
;
8869 check_for_return_continue_data data
= { &pset
, NULL_TREE
,
8872 = cp_walk_tree (&TREE_OPERAND (t
, 2),
8873 check_for_return_continue
, &data
,
8875 *jump_target
= ret_expr
;
8876 else if (*jump_target
== NULL_TREE
)
8878 if (data
.continue_stmt
)
8879 *jump_target
= data
.continue_stmt
;
8880 else if (data
.break_stmt
)
8881 *jump_target
= data
.break_stmt
;
8888 if (flags
& tf_error
)
8889 error_at (loc
, "expression %qE is not a constant expression", t
);
8893 if (VEC_INIT_EXPR_IS_CONSTEXPR (t
))
8895 if (flags
& tf_error
)
8897 error_at (loc
, "non-constant array initialization");
8898 diagnose_non_constexpr_vec_init (t
);
8904 /* We can see these in statement-expressions. */
8908 if (!RECUR (CLEANUP_BODY (t
), any
))
8910 if (!CLEANUP_EH_ONLY (t
) && !RECUR (CLEANUP_EXPR (t
), any
))
8914 case EMPTY_CLASS_EXPR
:
8919 tree
*target
= &TREE_OPERAND (t
, 0);
8920 /* Gotos representing break and continue are OK. */
8921 if (breaks (target
) || continues (target
))
8923 *jump_target
= *target
;
8926 if (flags
& tf_error
)
8927 error_at (loc
, "%<goto%> is not a constant expression");
8932 t
= LABEL_EXPR_LABEL (t
);
8933 if (DECL_ARTIFICIAL (t
))
8935 else if (flags
& tf_error
)
8936 error_at (loc
, "label definition is not a constant expression");
8940 return RECUR (TREE_OPERAND (t
, 0), rval
);
8943 return RECUR (TREE_OPERAND (t
, 0), rval
);
8945 /* Coroutine await, yield and return expressions are not. */
8948 case CO_RETURN_EXPR
:
8952 if (objc_non_constant_expr_p (t
))
8955 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t
)));
8963 potential_constant_expression_1 (tree t
, bool want_rval
, bool strict
, bool now
,
8964 tsubst_flags_t flags
)
8966 tree target
= NULL_TREE
;
8967 return potential_constant_expression_1 (t
, want_rval
, strict
, now
,
8971 /* The main entry point to the above. */
8974 potential_constant_expression (tree t
)
8976 return potential_constant_expression_1 (t
, false, true, false, tf_none
);
8979 /* As above, but require a constant rvalue. */
8982 potential_rvalue_constant_expression (tree t
)
8984 return potential_constant_expression_1 (t
, true, true, false, tf_none
);
8987 /* Like above, but complain about non-constant expressions. */
8990 require_potential_constant_expression (tree t
)
8992 return potential_constant_expression_1 (t
, false, true, false,
8993 tf_warning_or_error
);
8996 /* Cross product of the above. */
8999 require_potential_rvalue_constant_expression (tree t
)
9001 return potential_constant_expression_1 (t
, true, true, false,
9002 tf_warning_or_error
);
9005 /* Like above, but don't consider PARM_DECL a potential_constant_expression. */
9008 require_rvalue_constant_expression (tree t
)
9010 return potential_constant_expression_1 (t
, true, true, true,
9011 tf_warning_or_error
);
9014 /* Like potential_constant_expression, but don't consider possible constexpr
9015 substitution of the current function. That is, PARM_DECL qualifies under
9016 potential_constant_expression, but not here.
9018 This is basically what you can check when any actual constant values might
9019 be value-dependent. */
9022 is_constant_expression (tree t
)
9024 return potential_constant_expression_1 (t
, false, true, true, tf_none
);
9027 /* As above, but expect an rvalue. */
9030 is_rvalue_constant_expression (tree t
)
9032 return potential_constant_expression_1 (t
, true, true, true, tf_none
);
9035 /* Like above, but complain about non-constant expressions. */
9038 require_constant_expression (tree t
)
9040 return potential_constant_expression_1 (t
, false, true, true,
9041 tf_warning_or_error
);
9044 /* Like is_constant_expression, but allow const variables that are not allowed
9045 under constexpr rules. */
9048 is_static_init_expression (tree t
)
9050 return potential_constant_expression_1 (t
, false, false, true, tf_none
);
9053 /* Returns true if T is a potential constant expression that is not
9054 instantiation-dependent, and therefore a candidate for constant folding even
9058 is_nondependent_constant_expression (tree t
)
9060 return (!type_unknown_p (t
)
9061 && is_constant_expression (t
)
9062 && !instantiation_dependent_expression_p (t
));
9065 /* Returns true if T is a potential static initializer expression that is not
9066 instantiation-dependent. */
9069 is_nondependent_static_init_expression (tree t
)
9071 return (!type_unknown_p (t
)
9072 && is_static_init_expression (t
)
9073 && !instantiation_dependent_expression_p (t
));
9076 /* Finalize constexpr processing after parsing. */
9079 fini_constexpr (void)
9081 /* The contexpr call and fundef copies tables are no longer needed. */
9082 constexpr_call_table
= NULL
;
9083 fundef_copies_table
= NULL
;
9086 #include "gt-cp-constexpr.h"