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 #include "stringpool.h"
43 static bool verify_constant (tree
, bool, bool *, bool *);
44 #define VERIFY_CONSTANT(X) \
46 if (verify_constant ((X), ctx->quiet, non_constant_p, overflow_p)) \
50 static HOST_WIDE_INT
find_array_ctor_elt (tree ary
, tree dindex
,
52 static int array_index_cmp (tree key
, tree index
);
54 /* Returns true iff FUN is an instantiation of a constexpr function
55 template or a defaulted constexpr function. */
58 is_instantiation_of_constexpr (tree fun
)
60 return ((DECL_TEMPLOID_INSTANTIATION (fun
)
61 && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun
)))
62 || (DECL_DEFAULTED_FN (fun
)
63 && DECL_DECLARED_CONSTEXPR_P (fun
)));
66 /* Return true if T is a literal type. */
69 literal_type_p (tree t
)
74 || (VOID_TYPE_P (t
) && cxx_dialect
>= cxx14
))
78 t
= complete_type (t
);
79 gcc_assert (COMPLETE_TYPE_P (t
) || errorcount
);
80 return CLASSTYPE_LITERAL_P (t
);
82 if (TREE_CODE (t
) == ARRAY_TYPE
)
83 return literal_type_p (strip_array_types (t
));
87 /* If DECL is a variable declared `constexpr', require its type
88 be literal. Return error_mark_node if we give an error, the
92 ensure_literal_type_for_constexpr_object (tree decl
)
94 tree type
= TREE_TYPE (decl
);
96 && (DECL_DECLARED_CONSTEXPR_P (decl
)
97 || var_in_constexpr_fn (decl
))
98 && !processing_template_decl
)
100 tree stype
= strip_array_types (type
);
101 if (CLASS_TYPE_P (stype
) && !COMPLETE_TYPE_P (complete_type (stype
)))
102 /* Don't complain here, we'll complain about incompleteness
103 when we try to initialize the variable. */;
104 else if (!literal_type_p (type
))
106 if (DECL_DECLARED_CONSTEXPR_P (decl
))
108 auto_diagnostic_group d
;
109 error_at (DECL_SOURCE_LOCATION (decl
),
110 "the type %qT of %<constexpr%> variable %qD "
111 "is not literal", type
, decl
);
112 explain_non_literal_class (type
);
113 decl
= error_mark_node
;
115 else if (cxx_dialect
< cxx23
)
117 if (!is_instantiation_of_constexpr (current_function_decl
))
119 auto_diagnostic_group d
;
120 error_at (DECL_SOURCE_LOCATION (decl
),
121 "variable %qD of non-literal type %qT in "
122 "%<constexpr%> function only available with "
123 "%<-std=c++2b%> or %<-std=gnu++2b%>", decl
, type
);
124 explain_non_literal_class (type
);
125 decl
= error_mark_node
;
127 cp_function_chain
->invalid_constexpr
= true;
130 else if (DECL_DECLARED_CONSTEXPR_P (decl
)
131 && variably_modified_type_p (type
, NULL_TREE
))
133 error_at (DECL_SOURCE_LOCATION (decl
),
134 "%<constexpr%> variable %qD has variably-modified "
135 "type %qT", decl
, type
);
136 decl
= error_mark_node
;
142 struct constexpr_fundef_hasher
: ggc_ptr_hash
<constexpr_fundef
>
144 static hashval_t
hash (const constexpr_fundef
*);
145 static bool equal (const constexpr_fundef
*, const constexpr_fundef
*);
148 /* This table holds all constexpr function definitions seen in
149 the current translation unit. */
151 static GTY (()) hash_table
<constexpr_fundef_hasher
> *constexpr_fundef_table
;
153 /* Utility function used for managing the constexpr function table.
154 Return true if the entries pointed to by P and Q are for the
155 same constexpr function. */
158 constexpr_fundef_hasher::equal (const constexpr_fundef
*lhs
,
159 const constexpr_fundef
*rhs
)
161 return lhs
->decl
== rhs
->decl
;
164 /* Utility function used for managing the constexpr function table.
165 Return a hash value for the entry pointed to by Q. */
168 constexpr_fundef_hasher::hash (const constexpr_fundef
*fundef
)
170 return DECL_UID (fundef
->decl
);
173 /* Return a previously saved definition of function FUN. */
176 retrieve_constexpr_fundef (tree fun
)
178 if (constexpr_fundef_table
== NULL
)
181 constexpr_fundef fundef
= { fun
, NULL_TREE
, NULL_TREE
, NULL_TREE
};
182 return constexpr_fundef_table
->find (&fundef
);
185 /* Check whether the parameter and return types of FUN are valid for a
186 constexpr function, and complain if COMPLAIN. */
189 is_valid_constexpr_fn (tree fun
, bool complain
)
193 if (DECL_INHERITED_CTOR (fun
)
194 && TREE_CODE (fun
) == TEMPLATE_DECL
)
198 error ("inherited constructor %qD is not %<constexpr%>",
199 DECL_INHERITED_CTOR (fun
));
203 for (tree parm
= FUNCTION_FIRST_USER_PARM (fun
);
204 parm
!= NULL_TREE
; parm
= TREE_CHAIN (parm
))
205 if (!literal_type_p (TREE_TYPE (parm
)))
210 auto_diagnostic_group d
;
211 error ("invalid type for parameter %d of %<constexpr%> "
212 "function %q+#D", DECL_PARM_INDEX (parm
), fun
);
213 explain_non_literal_class (TREE_TYPE (parm
));
218 if (LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun
)) && cxx_dialect
< cxx17
)
222 inform (DECL_SOURCE_LOCATION (fun
),
223 "lambdas are implicitly %<constexpr%> only in C++17 and later");
225 else if (DECL_DESTRUCTOR_P (fun
))
227 if (cxx_dialect
< cxx20
)
231 error_at (DECL_SOURCE_LOCATION (fun
),
232 "%<constexpr%> destructors only available"
233 " with %<-std=c++20%> or %<-std=gnu++20%>");
236 else if (!DECL_CONSTRUCTOR_P (fun
))
238 tree rettype
= TREE_TYPE (TREE_TYPE (fun
));
239 if (!literal_type_p (rettype
))
244 auto_diagnostic_group d
;
245 error ("invalid return type %qT of %<constexpr%> function %q+D",
247 explain_non_literal_class (rettype
);
251 /* C++14 DR 1684 removed this restriction. */
252 if (cxx_dialect
< cxx14
253 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fun
)
254 && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun
)))
259 auto_diagnostic_group d
;
260 if (pedwarn (DECL_SOURCE_LOCATION (fun
), OPT_Wpedantic
,
261 "enclosing class of %<constexpr%> non-static"
262 " member function %q+#D is not a literal type",
264 explain_non_literal_class (DECL_CONTEXT (fun
));
268 else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun
)))
272 error ("%q#T has virtual base classes", DECL_CONTEXT (fun
));
278 /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF
279 for a member of an anonymous aggregate, INIT is the initializer for that
280 member, and VEC_OUTER is the vector of constructor elements for the class
281 whose constructor we are processing. Add the initializer to the vector
282 and return true to indicate success. */
285 build_anon_member_initialization (tree member
, tree init
,
286 vec
<constructor_elt
, va_gc
> **vec_outer
)
288 /* MEMBER presents the relevant fields from the inside out, but we need
289 to build up the initializer from the outside in so that we can reuse
290 previously built CONSTRUCTORs if this is, say, the second field in an
291 anonymous struct. So we use a vec as a stack. */
292 auto_vec
<tree
, 2> fields
;
295 fields
.safe_push (TREE_OPERAND (member
, 1));
296 member
= TREE_OPERAND (member
, 0);
298 while (ANON_AGGR_TYPE_P (TREE_TYPE (member
))
299 && TREE_CODE (member
) == COMPONENT_REF
);
301 /* VEC has the constructor elements vector for the context of FIELD.
302 If FIELD is an anonymous aggregate, we will push inside it. */
303 vec
<constructor_elt
, va_gc
> **vec
= vec_outer
;
305 while (field
= fields
.pop(),
306 ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
309 /* If there is already an outer constructor entry for the anonymous
310 aggregate FIELD, use it; otherwise, insert one. */
311 if (vec_safe_is_empty (*vec
)
312 || (*vec
)->last().index
!= field
)
314 ctor
= build_constructor (TREE_TYPE (field
), NULL
);
315 CONSTRUCTOR_APPEND_ELT (*vec
, field
, ctor
);
318 ctor
= (*vec
)->last().value
;
319 vec
= &CONSTRUCTOR_ELTS (ctor
);
322 /* Now we're at the innermost field, the one that isn't an anonymous
323 aggregate. Add its initializer to the CONSTRUCTOR and we're done. */
324 gcc_assert (fields
.is_empty());
325 CONSTRUCTOR_APPEND_ELT (*vec
, field
, init
);
330 /* Subroutine of build_constexpr_constructor_member_initializers.
331 The expression tree T represents a data member initialization
332 in a (constexpr) constructor definition. Build a pairing of
333 the data member with its initializer, and prepend that pair
334 to the existing initialization pair INITS. */
337 build_data_member_initialization (tree t
, vec
<constructor_elt
, va_gc
> **vec
)
340 if (TREE_CODE (t
) == CLEANUP_POINT_EXPR
)
341 t
= TREE_OPERAND (t
, 0);
342 if (TREE_CODE (t
) == EXPR_STMT
)
343 t
= TREE_OPERAND (t
, 0);
344 if (t
== error_mark_node
)
346 if (TREE_CODE (t
) == STATEMENT_LIST
)
348 for (tree stmt
: tsi_range (t
))
349 if (! build_data_member_initialization (stmt
, vec
))
353 if (TREE_CODE (t
) == CLEANUP_STMT
)
355 /* We can't see a CLEANUP_STMT in a constructor for a literal class,
356 but we can in a constexpr constructor for a non-literal class. Just
357 ignore it; either all the initialization will be constant, in which
358 case the cleanup can't run, or it can't be constexpr.
359 Still recurse into CLEANUP_BODY. */
360 return build_data_member_initialization (CLEANUP_BODY (t
), vec
);
362 if (TREE_CODE (t
) == CONVERT_EXPR
)
363 t
= TREE_OPERAND (t
, 0);
364 if (TREE_CODE (t
) == INIT_EXPR
365 /* vptr initialization shows up as a MODIFY_EXPR. In C++14 we only
366 use what this function builds for cx_check_missing_mem_inits, and
367 assignment in the ctor body doesn't count. */
368 || (cxx_dialect
< cxx14
&& TREE_CODE (t
) == MODIFY_EXPR
))
370 member
= TREE_OPERAND (t
, 0);
371 init
= break_out_target_exprs (TREE_OPERAND (t
, 1));
373 else if (TREE_CODE (t
) == CALL_EXPR
)
375 tree fn
= get_callee_fndecl (t
);
376 if (!fn
|| !DECL_CONSTRUCTOR_P (fn
))
377 /* We're only interested in calls to subobject constructors. */
379 member
= CALL_EXPR_ARG (t
, 0);
380 /* We don't use build_cplus_new here because it complains about
381 abstract bases. Leaving the call unwrapped means that it has the
382 wrong type, but cxx_eval_constant_expression doesn't care. */
383 init
= break_out_target_exprs (t
);
385 else if (TREE_CODE (t
) == BIND_EXPR
)
386 return build_data_member_initialization (BIND_EXPR_BODY (t
), vec
);
388 /* Don't add anything else to the CONSTRUCTOR. */
390 if (INDIRECT_REF_P (member
))
391 member
= TREE_OPERAND (member
, 0);
392 if (TREE_CODE (member
) == NOP_EXPR
)
396 if (TREE_CODE (op
) == ADDR_EXPR
)
398 gcc_assert (same_type_ignoring_top_level_qualifiers_p
399 (TREE_TYPE (TREE_TYPE (op
)),
400 TREE_TYPE (TREE_TYPE (member
))));
401 /* Initializing a cv-qualified member; we need to look through
405 else if (op
== current_class_ptr
406 && (same_type_ignoring_top_level_qualifiers_p
407 (TREE_TYPE (TREE_TYPE (member
)),
408 current_class_type
)))
409 /* Delegating constructor. */
413 /* This is an initializer for an empty base; keep it for now so
414 we can check it in cxx_eval_bare_aggregate. */
415 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member
))));
418 if (TREE_CODE (member
) == ADDR_EXPR
)
419 member
= TREE_OPERAND (member
, 0);
420 if (TREE_CODE (member
) == COMPONENT_REF
)
422 tree aggr
= TREE_OPERAND (member
, 0);
423 if (TREE_CODE (aggr
) == VAR_DECL
)
424 /* Initializing a local variable, don't add anything. */
426 if (TREE_CODE (aggr
) != COMPONENT_REF
)
427 /* Normal member initialization. */
428 member
= TREE_OPERAND (member
, 1);
429 else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr
)))
430 /* Initializing a member of an anonymous union. */
431 return build_anon_member_initialization (member
, init
, vec
);
433 /* We're initializing a vtable pointer in a base. Leave it as
434 COMPONENT_REF so we remember the path to get to the vfield. */
435 gcc_assert (TREE_TYPE (member
) == vtbl_ptr_type_node
);
438 /* Value-initialization can produce multiple initializers for the
439 same field; use the last one. */
440 if (!vec_safe_is_empty (*vec
) && (*vec
)->last().index
== member
)
441 (*vec
)->last().value
= init
;
443 CONSTRUCTOR_APPEND_ELT (*vec
, member
, init
);
447 /* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval.
448 In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a
449 BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */
452 check_constexpr_bind_expr_vars (tree t
)
454 gcc_assert (TREE_CODE (t
) == BIND_EXPR
);
456 for (tree var
= BIND_EXPR_VARS (t
); var
; var
= DECL_CHAIN (var
))
457 if (TREE_CODE (var
) == TYPE_DECL
458 && DECL_IMPLICIT_TYPEDEF_P (var
)
459 && !LAMBDA_TYPE_P (TREE_TYPE (var
)))
464 /* Subroutine of check_constexpr_ctor_body. */
467 check_constexpr_ctor_body_1 (tree last
, tree list
)
469 switch (TREE_CODE (list
))
472 if (TREE_CODE (DECL_EXPR_DECL (list
)) == USING_DECL
473 || TREE_CODE (DECL_EXPR_DECL (list
)) == TYPE_DECL
)
477 case CLEANUP_POINT_EXPR
:
478 return check_constexpr_ctor_body (last
, TREE_OPERAND (list
, 0),
482 if (!check_constexpr_bind_expr_vars (list
)
483 || !check_constexpr_ctor_body (last
, BIND_EXPR_BODY (list
),
490 case DEBUG_BEGIN_STMT
:
498 /* Make sure that there are no statements after LAST in the constructor
499 body represented by LIST. */
502 check_constexpr_ctor_body (tree last
, tree list
, bool complain
)
504 /* C++14 doesn't require a constexpr ctor to have an empty body. */
505 if (cxx_dialect
>= cxx14
)
509 if (TREE_CODE (list
) == STATEMENT_LIST
)
511 tree_stmt_iterator i
= tsi_last (list
);
512 for (; !tsi_end_p (i
); tsi_prev (&i
))
514 tree t
= tsi_stmt (i
);
517 if (!check_constexpr_ctor_body_1 (last
, t
))
524 else if (list
!= last
525 && !check_constexpr_ctor_body_1 (last
, list
))
530 error ("%<constexpr%> constructor does not have empty body");
531 DECL_DECLARED_CONSTEXPR_P (current_function_decl
) = false;
536 /* V is a vector of constructor elements built up for the base and member
537 initializers of a constructor for TYPE. They need to be in increasing
538 offset order, which they might not be yet if TYPE has a primary base
539 which is not first in the base-clause or a vptr and at least one base
540 all of which are non-primary. */
542 static vec
<constructor_elt
, va_gc
> *
543 sort_constexpr_mem_initializers (tree type
, vec
<constructor_elt
, va_gc
> *v
)
545 tree pri
= CLASSTYPE_PRIMARY_BINFO (type
);
551 field_type
= BINFO_TYPE (pri
);
552 else if (TYPE_CONTAINS_VPTR_P (type
))
553 field_type
= vtbl_ptr_type_node
;
557 /* Find the element for the primary base or vptr and move it to the
558 beginning of the vec. */
559 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
560 if (TREE_TYPE (ce
->index
) == field_type
)
563 if (i
> 0 && i
< vec_safe_length (v
))
565 vec
<constructor_elt
, va_gc
> &vref
= *v
;
566 constructor_elt elt
= vref
[i
];
575 /* Build compile-time evalable representations of member-initializer list
576 for a constexpr constructor. */
579 build_constexpr_constructor_member_initializers (tree type
, tree body
)
581 vec
<constructor_elt
, va_gc
> *vec
= NULL
;
584 switch (TREE_CODE (body
))
586 case MUST_NOT_THROW_EXPR
:
588 body
= TREE_OPERAND (body
, 0);
592 for (tree stmt
: tsi_range (body
))
595 if (TREE_CODE (body
) == BIND_EXPR
)
601 body
= BIND_EXPR_BODY (body
);
608 if (TREE_CODE (body
) == TRY_BLOCK
)
610 body
= TREE_OPERAND (body
, 0);
611 if (TREE_CODE (body
) == BIND_EXPR
)
612 body
= BIND_EXPR_BODY (body
);
614 if (TREE_CODE (body
) == CLEANUP_POINT_EXPR
)
616 body
= TREE_OPERAND (body
, 0);
617 if (TREE_CODE (body
) == EXPR_STMT
)
618 body
= TREE_OPERAND (body
, 0);
619 if (TREE_CODE (body
) == INIT_EXPR
620 && (same_type_ignoring_top_level_qualifiers_p
621 (TREE_TYPE (TREE_OPERAND (body
, 0)),
622 current_class_type
)))
625 return TREE_OPERAND (body
, 1);
627 ok
= build_data_member_initialization (body
, &vec
);
629 else if (TREE_CODE (body
) == STATEMENT_LIST
)
631 for (tree stmt
: tsi_range (body
))
633 ok
= build_data_member_initialization (stmt
, &vec
);
638 else if (EXPR_P (body
))
639 ok
= build_data_member_initialization (body
, &vec
);
641 gcc_assert (errorcount
> 0);
644 if (vec_safe_length (vec
) > 0)
646 /* In a delegating constructor, return the target. */
647 constructor_elt
*ce
= &(*vec
)[0];
648 if (ce
->index
== current_class_ptr
)
655 vec
= sort_constexpr_mem_initializers (type
, vec
);
656 return build_constructor (type
, vec
);
659 return error_mark_node
;
662 /* We have an expression tree T that represents a call, either CALL_EXPR
663 or AGGR_INIT_EXPR. If the call is lexically to a named function,
664 retrun the _DECL for that function. */
667 get_function_named_in_call (tree t
)
669 tree fun
= cp_get_callee (t
);
670 if (fun
&& TREE_CODE (fun
) == ADDR_EXPR
671 && TREE_CODE (TREE_OPERAND (fun
, 0)) == FUNCTION_DECL
)
672 fun
= TREE_OPERAND (fun
, 0);
676 /* Subroutine of check_constexpr_fundef. BODY is the body of a function
677 declared to be constexpr, or a sub-statement thereof. Returns the
678 return value if suitable, error_mark_node for a statement not allowed in
679 a constexpr function, or NULL_TREE if no return value was found. */
682 constexpr_fn_retval (tree body
)
684 switch (TREE_CODE (body
))
688 tree expr
= NULL_TREE
;
689 for (tree stmt
: tsi_range (body
))
691 tree s
= constexpr_fn_retval (stmt
);
692 if (s
== error_mark_node
)
693 return error_mark_node
;
694 else if (s
== NULL_TREE
)
695 /* Keep iterating. */;
697 /* Multiple return statements. */
698 return error_mark_node
;
706 return break_out_target_exprs (TREE_OPERAND (body
, 0));
710 tree decl
= DECL_EXPR_DECL (body
);
711 if (TREE_CODE (decl
) == USING_DECL
712 /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */
713 || DECL_ARTIFICIAL (decl
))
715 return error_mark_node
;
718 case CLEANUP_POINT_EXPR
:
719 return constexpr_fn_retval (TREE_OPERAND (body
, 0));
722 if (!check_constexpr_bind_expr_vars (body
))
723 return error_mark_node
;
724 return constexpr_fn_retval (BIND_EXPR_BODY (body
));
727 case DEBUG_BEGIN_STMT
:
732 tree fun
= get_function_named_in_call (body
);
734 && fndecl_built_in_p (fun
, BUILT_IN_UNREACHABLE
))
740 return error_mark_node
;
744 /* Subroutine of check_constexpr_fundef. BODY is the DECL_SAVED_TREE of
745 FUN; do the necessary transformations to turn it into a single expression
746 that we can store in the hash table. */
749 massage_constexpr_body (tree fun
, tree body
)
751 if (DECL_CONSTRUCTOR_P (fun
))
752 body
= build_constexpr_constructor_member_initializers
753 (DECL_CONTEXT (fun
), body
);
754 else if (cxx_dialect
< cxx14
)
756 if (TREE_CODE (body
) == EH_SPEC_BLOCK
)
757 body
= EH_SPEC_STMTS (body
);
758 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
759 body
= TREE_OPERAND (body
, 0);
760 body
= constexpr_fn_retval (body
);
765 /* CTYPE is a type constructed from BODY. Return true if some
766 bases/fields are uninitialized, and complain if COMPLAIN. */
769 cx_check_missing_mem_inits (tree ctype
, tree body
, bool complain
)
771 /* We allow uninitialized bases/fields in C++20. */
772 if (cxx_dialect
>= cxx20
)
779 if (TREE_CODE (body
) != CONSTRUCTOR
)
781 nelts
= CONSTRUCTOR_NELTS (body
);
783 tree field
= TYPE_FIELDS (ctype
);
785 if (TREE_CODE (ctype
) == UNION_TYPE
)
787 if (nelts
== 0 && next_initializable_field (field
))
790 error ("%<constexpr%> constructor for union %qT must "
791 "initialize exactly one non-static data member", ctype
);
797 /* Iterate over the CONSTRUCTOR, checking any missing fields don't
798 need an explicit initialization. */
800 for (unsigned i
= 0; i
<= nelts
; ++i
)
802 tree index
= NULL_TREE
;
805 index
= CONSTRUCTOR_ELT (body
, i
)->index
;
806 /* Skip base and vtable inits. */
807 if (TREE_CODE (index
) != FIELD_DECL
808 || DECL_ARTIFICIAL (index
))
812 for (; field
!= index
; field
= DECL_CHAIN (field
))
815 if (TREE_CODE (field
) != FIELD_DECL
)
817 if (DECL_UNNAMED_BIT_FIELD (field
))
819 if (DECL_ARTIFICIAL (field
))
821 if (ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
823 /* Recurse to check the anonymous aggregate member. */
824 bad
|= cx_check_missing_mem_inits
825 (TREE_TYPE (field
), NULL_TREE
, complain
);
826 if (bad
&& !complain
)
830 ftype
= TREE_TYPE (field
);
831 if (!ftype
|| !TYPE_P (ftype
) || !COMPLETE_TYPE_P (ftype
))
832 /* A flexible array can't be intialized here, so don't complain
835 if (is_empty_field (field
))
836 /* An empty field doesn't need an initializer. */
838 ftype
= strip_array_types (ftype
);
839 if (type_has_constexpr_default_constructor (ftype
))
841 /* It's OK to skip a member with a trivial constexpr ctor.
842 A constexpr ctor that isn't trivial should have been
844 gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype
)
850 auto_diagnostic_group d
;
851 error ("member %qD must be initialized by mem-initializer "
852 "in %<constexpr%> constructor", field
);
853 inform (DECL_SOURCE_LOCATION (field
), "declared here");
856 if (field
== NULL_TREE
)
859 if (ANON_AGGR_TYPE_P (TREE_TYPE (index
)))
861 /* Check the anonymous aggregate initializer is valid. */
862 bad
|= cx_check_missing_mem_inits
863 (TREE_TYPE (index
), CONSTRUCTOR_ELT (body
, i
)->value
, complain
);
864 if (bad
&& !complain
)
867 field
= DECL_CHAIN (field
);
873 /* We are processing the definition of the constexpr function FUN.
874 Check that its body fulfills the apropriate requirements and
875 enter it in the constexpr function definition table. */
878 maybe_save_constexpr_fundef (tree fun
)
880 if (processing_template_decl
881 || cp_function_chain
->invalid_constexpr
882 || (DECL_CLONED_FUNCTION_P (fun
) && !DECL_DELETING_DESTRUCTOR_P (fun
)))
885 /* With -fimplicit-constexpr, try to make inlines constexpr. We'll
886 actually set DECL_DECLARED_CONSTEXPR_P below if the checks pass. */
887 bool implicit
= false;
888 if (flag_implicit_constexpr
)
890 if (DECL_DELETING_DESTRUCTOR_P (fun
)
891 && decl_implicit_constexpr_p (DECL_CLONED_FUNCTION (fun
)))
892 /* Don't inherit implicit constexpr from the non-deleting
894 DECL_DECLARED_CONSTEXPR_P (fun
) = false;
896 if (!DECL_DECLARED_CONSTEXPR_P (fun
)
897 && DECL_DECLARED_INLINE_P (fun
)
898 && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fun
)))
902 if (!DECL_DECLARED_CONSTEXPR_P (fun
) && !implicit
)
905 bool complain
= !DECL_GENERATED_P (fun
) && !implicit
;
907 if (!is_valid_constexpr_fn (fun
, complain
))
910 tree massaged
= massage_constexpr_body (fun
, DECL_SAVED_TREE (fun
));
911 if (massaged
== NULL_TREE
|| massaged
== error_mark_node
)
913 if (!DECL_CONSTRUCTOR_P (fun
) && complain
)
914 error ("body of %<constexpr%> function %qD not a return-statement",
919 bool potential
= potential_rvalue_constant_expression (massaged
);
920 if (!potential
&& complain
)
921 require_potential_rvalue_constant_expression (massaged
);
923 if (DECL_CONSTRUCTOR_P (fun
) && potential
)
925 if (cx_check_missing_mem_inits (DECL_CONTEXT (fun
),
928 else if (cxx_dialect
> cxx11
)
930 /* What we got from massage_constexpr_body is pretty much just the
931 ctor-initializer, also check the body. */
932 massaged
= DECL_SAVED_TREE (fun
);
933 potential
= potential_rvalue_constant_expression (massaged
);
934 if (!potential
&& complain
)
935 require_potential_rvalue_constant_expression (massaged
);
939 if (!potential
&& complain
)
946 DECL_DECLARED_CONSTEXPR_P (fun
) = true;
947 DECL_LANG_SPECIFIC (fun
)->u
.fn
.implicit_constexpr
= true;
948 if (DECL_CONSTRUCTOR_P (fun
))
949 TYPE_HAS_CONSTEXPR_CTOR (DECL_CONTEXT (fun
)) = true;
952 /* Don't bother keeping the pre-generic body of unsuitable functions
953 not explicitly declared constexpr. */
957 constexpr_fundef entry
= {fun
, NULL_TREE
, NULL_TREE
, NULL_TREE
};
958 bool clear_ctx
= false;
959 if (DECL_RESULT (fun
) && DECL_CONTEXT (DECL_RESULT (fun
)) == NULL_TREE
)
962 DECL_CONTEXT (DECL_RESULT (fun
)) = fun
;
964 tree saved_fn
= current_function_decl
;
965 current_function_decl
= fun
;
966 entry
.body
= copy_fn (entry
.decl
, entry
.parms
, entry
.result
);
967 current_function_decl
= saved_fn
;
969 DECL_CONTEXT (DECL_RESULT (entry
.decl
)) = NULL_TREE
;
971 /* For a template instantiation, we want to remember the pre-generic body
972 for explain_invalid_constexpr_fn, but do tell cxx_eval_call_expression
973 that it doesn't need to bother trying to expand the function. */
974 entry
.result
= error_mark_node
;
976 register_constexpr_fundef (entry
);
979 /* BODY is a validated and massaged definition of a constexpr
980 function. Register it in the hash table. */
983 register_constexpr_fundef (const constexpr_fundef
&value
)
985 /* Create the constexpr function table if necessary. */
986 if (constexpr_fundef_table
== NULL
)
987 constexpr_fundef_table
988 = hash_table
<constexpr_fundef_hasher
>::create_ggc (101);
990 constexpr_fundef
**slot
= constexpr_fundef_table
->find_slot
991 (const_cast<constexpr_fundef
*> (&value
), INSERT
);
993 gcc_assert (*slot
== NULL
);
994 *slot
= ggc_alloc
<constexpr_fundef
> ();
998 /* FUN is a non-constexpr function called in a context that requires a
999 constant expression. If it comes from a constexpr template, explain why
1000 the instantiation isn't constexpr. */
1003 explain_invalid_constexpr_fn (tree fun
)
1005 static hash_set
<tree
> *diagnosed
;
1007 /* Only diagnose defaulted functions, lambdas, or instantiations. */
1008 if (!DECL_DEFAULTED_FN (fun
)
1009 && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun
))
1010 && !is_instantiation_of_constexpr (fun
))
1012 inform (DECL_SOURCE_LOCATION (fun
), "%qD declared here", fun
);
1015 if (diagnosed
== NULL
)
1016 diagnosed
= new hash_set
<tree
>;
1017 if (diagnosed
->add (fun
))
1018 /* Already explained. */
1021 iloc_sentinel ils
= input_location
;
1022 if (!lambda_static_thunk_p (fun
))
1024 /* Diagnostics should completely ignore the static thunk, so leave
1025 input_location set to our caller's location. */
1026 input_location
= DECL_SOURCE_LOCATION (fun
);
1027 inform (input_location
,
1028 "%qD is not usable as a %<constexpr%> function because:", fun
);
1030 /* First check the declaration. */
1031 if (is_valid_constexpr_fn (fun
, true))
1033 /* Then if it's OK, the body. */
1034 if (!DECL_DECLARED_CONSTEXPR_P (fun
)
1035 && DECL_DEFAULTED_FN (fun
))
1036 explain_implicit_non_constexpr (fun
);
1039 if (constexpr_fundef
*fd
= retrieve_constexpr_fundef (fun
))
1042 body
= DECL_SAVED_TREE (fun
);
1043 body
= massage_constexpr_body (fun
, body
);
1044 require_potential_rvalue_constant_expression (body
);
1045 if (DECL_CONSTRUCTOR_P (fun
))
1046 cx_check_missing_mem_inits (DECL_CONTEXT (fun
), body
, true);
1051 /* Objects of this type represent calls to constexpr functions
1052 along with the bindings of parameters to their arguments, for
1053 the purpose of compile time evaluation. */
1055 struct GTY((for_user
)) constexpr_call
{
1056 /* Description of the constexpr function definition. */
1057 constexpr_fundef
*fundef
;
1058 /* Parameter bindings environment. A TREE_VEC of arguments. */
1060 /* Result of the call.
1061 NULL means the call is being evaluated.
1062 error_mark_node means that the evaluation was erroneous;
1063 otherwise, the actuall value of the call. */
1065 /* The hash of this call; we remember it here to avoid having to
1066 recalculate it when expanding the hash table. */
1068 /* Whether __builtin_is_constant_evaluated() should evaluate to true. */
1069 bool manifestly_const_eval
;
1072 struct constexpr_call_hasher
: ggc_ptr_hash
<constexpr_call
>
1074 static hashval_t
hash (constexpr_call
*);
1075 static bool equal (constexpr_call
*, constexpr_call
*);
1078 enum constexpr_switch_state
{
1079 /* Used when processing a switch for the first time by cxx_eval_switch_expr
1080 and default: label for that switch has not been seen yet. */
1081 css_default_not_seen
,
1082 /* Used when processing a switch for the first time by cxx_eval_switch_expr
1083 and default: label for that switch has been seen already. */
1085 /* Used when processing a switch for the second time by
1086 cxx_eval_switch_expr, where default: label should match. */
1087 css_default_processing
1090 /* The constexpr expansion context part which needs one instance per
1091 cxx_eval_outermost_constant_expr invocation. VALUES is a map of values of
1092 variables initialized within the expression. */
1094 struct constexpr_global_ctx
{
1095 /* Values for any temporaries or local variables within the
1096 constant-expression. */
1097 hash_map
<tree
,tree
> values
;
1098 /* Number of cxx_eval_constant_expression calls (except skipped ones,
1099 on simple constants or location wrappers) encountered during current
1100 cxx_eval_outermost_constant_expr call. */
1101 HOST_WIDE_INT constexpr_ops_count
;
1102 /* Heap VAR_DECLs created during the evaluation of the outermost constant
1104 auto_vec
<tree
, 16> heap_vars
;
1105 /* Cleanups that need to be evaluated at the end of CLEANUP_POINT_EXPR. */
1106 vec
<tree
> *cleanups
;
1107 /* Number of heap VAR_DECL deallocations. */
1108 unsigned heap_dealloc_count
;
1110 constexpr_global_ctx ()
1111 : constexpr_ops_count (0), cleanups (NULL
), heap_dealloc_count (0) {}
1114 /* The constexpr expansion context. CALL is the current function
1115 expansion, CTOR is the current aggregate initializer, OBJECT is the
1116 object being initialized by CTOR, either a VAR_DECL or a _REF. */
1118 struct constexpr_ctx
{
1119 /* The part of the context that needs to be unique to the whole
1120 cxx_eval_outermost_constant_expr invocation. */
1121 constexpr_global_ctx
*global
;
1122 /* The innermost call we're evaluating. */
1123 constexpr_call
*call
;
1124 /* SAVE_EXPRs and TARGET_EXPR_SLOT vars of TARGET_EXPRs that we've seen
1125 within the current LOOP_EXPR. NULL if we aren't inside a loop. */
1126 vec
<tree
> *save_exprs
;
1127 /* The CONSTRUCTOR we're currently building up for an aggregate
1130 /* The object we're building the CONSTRUCTOR for. */
1132 /* If inside SWITCH_EXPR. */
1133 constexpr_switch_state
*css_state
;
1134 /* The aggregate initialization context inside which this one is nested. This
1135 is used by lookup_placeholder to resolve PLACEHOLDER_EXPRs. */
1136 const constexpr_ctx
*parent
;
1138 /* Whether we should error on a non-constant expression or fail quietly.
1139 This flag needs to be here, but some of the others could move to global
1140 if they get larger than a word. */
1142 /* Whether we are strictly conforming to constant expression rules or
1143 trying harder to get a constant value. */
1145 /* Whether __builtin_is_constant_evaluated () should be true. */
1146 bool manifestly_const_eval
;
1149 /* This internal flag controls whether we should avoid doing anything during
1150 constexpr evaluation that would cause extra DECL_UID generation, such as
1151 template instantiation and function body copying. */
1153 static bool uid_sensitive_constexpr_evaluation_value
;
1155 /* An internal counter that keeps track of the number of times
1156 uid_sensitive_constexpr_evaluation_p returned true. */
1158 static unsigned uid_sensitive_constexpr_evaluation_true_counter
;
1160 /* The accessor for uid_sensitive_constexpr_evaluation_value which also
1161 increments the corresponding counter. */
1164 uid_sensitive_constexpr_evaluation_p ()
1166 if (uid_sensitive_constexpr_evaluation_value
)
1168 ++uid_sensitive_constexpr_evaluation_true_counter
;
1175 /* The default constructor for uid_sensitive_constexpr_evaluation_sentinel
1176 enables the internal flag for uid_sensitive_constexpr_evaluation_p
1177 during the lifetime of the sentinel object. Upon its destruction, the
1178 previous value of uid_sensitive_constexpr_evaluation_p is restored. */
1180 uid_sensitive_constexpr_evaluation_sentinel
1181 ::uid_sensitive_constexpr_evaluation_sentinel ()
1182 : ovr (uid_sensitive_constexpr_evaluation_value
, true)
1186 /* The default constructor for uid_sensitive_constexpr_evaluation_checker
1187 records the current number of times that uid_sensitive_constexpr_evaluation_p
1188 has been called and returned true. */
1190 uid_sensitive_constexpr_evaluation_checker
1191 ::uid_sensitive_constexpr_evaluation_checker ()
1192 : saved_counter (uid_sensitive_constexpr_evaluation_true_counter
)
1196 /* Returns true iff uid_sensitive_constexpr_evaluation_p is true, and
1197 some constexpr evaluation was restricted due to u_s_c_e_p being called
1198 and returning true during the lifetime of this checker object. */
1201 uid_sensitive_constexpr_evaluation_checker::evaluation_restricted_p () const
1203 return (uid_sensitive_constexpr_evaluation_value
1204 && saved_counter
!= uid_sensitive_constexpr_evaluation_true_counter
);
1208 /* A table of all constexpr calls that have been evaluated by the
1209 compiler in this translation unit. */
1211 static GTY (()) hash_table
<constexpr_call_hasher
> *constexpr_call_table
;
1213 static tree
cxx_eval_constant_expression (const constexpr_ctx
*, tree
,
1214 bool, bool *, bool *, tree
* = NULL
);
1216 /* Compute a hash value for a constexpr call representation. */
1219 constexpr_call_hasher::hash (constexpr_call
*info
)
1224 /* Return true if the objects pointed to by P and Q represent calls
1225 to the same constexpr function with the same arguments.
1226 Otherwise, return false. */
1229 constexpr_call_hasher::equal (constexpr_call
*lhs
, constexpr_call
*rhs
)
1233 if (lhs
->hash
!= rhs
->hash
)
1235 if (lhs
->manifestly_const_eval
!= rhs
->manifestly_const_eval
)
1237 if (!constexpr_fundef_hasher::equal (lhs
->fundef
, rhs
->fundef
))
1239 return cp_tree_equal (lhs
->bindings
, rhs
->bindings
);
1242 /* Initialize the constexpr call table, if needed. */
1245 maybe_initialize_constexpr_call_table (void)
1247 if (constexpr_call_table
== NULL
)
1248 constexpr_call_table
= hash_table
<constexpr_call_hasher
>::create_ggc (101);
1251 /* During constexpr CALL_EXPR evaluation, to avoid issues with sharing when
1252 a function happens to get called recursively, we unshare the callee
1253 function's body and evaluate this unshared copy instead of evaluating the
1256 FUNDEF_COPIES_TABLE is a per-function freelist of these unshared function
1257 copies. The underlying data structure of FUNDEF_COPIES_TABLE is a hash_map
1258 that's keyed off of the original FUNCTION_DECL and whose value is a
1259 TREE_LIST of this function's unused copies awaiting reuse.
1261 This is not GC-deletable to avoid GC affecting UID generation. */
1263 static GTY(()) decl_tree_map
*fundef_copies_table
;
1265 /* Reuse a copy or create a new unshared copy of the function FUN.
1266 Return this copy. We use a TREE_LIST whose PURPOSE is body, VALUE
1267 is parms, TYPE is result. */
1270 get_fundef_copy (constexpr_fundef
*fundef
)
1274 tree
*slot
= &(hash_map_safe_get_or_insert
<hm_ggc
>
1275 (fundef_copies_table
, fundef
->decl
, &existed
, 127));
1279 /* There is no cached function available, or in use. We can use
1280 the function directly. That the slot is now created records
1281 that this function is now in use. */
1282 copy
= build_tree_list (fundef
->body
, fundef
->parms
);
1283 TREE_TYPE (copy
) = fundef
->result
;
1285 else if (*slot
== NULL_TREE
)
1287 if (uid_sensitive_constexpr_evaluation_p ())
1290 /* We've already used the function itself, so make a copy. */
1291 copy
= build_tree_list (NULL
, NULL
);
1292 tree saved_body
= DECL_SAVED_TREE (fundef
->decl
);
1293 tree saved_parms
= DECL_ARGUMENTS (fundef
->decl
);
1294 tree saved_result
= DECL_RESULT (fundef
->decl
);
1295 tree saved_fn
= current_function_decl
;
1296 DECL_SAVED_TREE (fundef
->decl
) = fundef
->body
;
1297 DECL_ARGUMENTS (fundef
->decl
) = fundef
->parms
;
1298 DECL_RESULT (fundef
->decl
) = fundef
->result
;
1299 current_function_decl
= fundef
->decl
;
1300 TREE_PURPOSE (copy
) = copy_fn (fundef
->decl
, TREE_VALUE (copy
),
1302 current_function_decl
= saved_fn
;
1303 DECL_RESULT (fundef
->decl
) = saved_result
;
1304 DECL_ARGUMENTS (fundef
->decl
) = saved_parms
;
1305 DECL_SAVED_TREE (fundef
->decl
) = saved_body
;
1309 /* We have a cached function available. */
1311 *slot
= TREE_CHAIN (copy
);
1317 /* Save the copy COPY of function FUN for later reuse by
1318 get_fundef_copy(). By construction, there will always be an entry
1322 save_fundef_copy (tree fun
, tree copy
)
1324 tree
*slot
= fundef_copies_table
->get (fun
);
1325 TREE_CHAIN (copy
) = *slot
;
1329 /* We have an expression tree T that represents a call, either CALL_EXPR
1330 or AGGR_INIT_EXPR. Return the Nth argument. */
1333 get_nth_callarg (tree t
, int n
)
1335 switch (TREE_CODE (t
))
1338 return CALL_EXPR_ARG (t
, n
);
1340 case AGGR_INIT_EXPR
:
1341 return AGGR_INIT_EXPR_ARG (t
, n
);
1349 /* Attempt to evaluate T which represents a call to a builtin function.
1350 We assume here that all builtin functions evaluate to scalar types
1351 represented by _CST nodes. */
1354 cxx_eval_builtin_function_call (const constexpr_ctx
*ctx
, tree t
, tree fun
,
1356 bool *non_constant_p
, bool *overflow_p
)
1358 const int nargs
= call_expr_nargs (t
);
1359 tree
*args
= (tree
*) alloca (nargs
* sizeof (tree
));
1363 /* Don't fold __builtin_constant_p within a constexpr function. */
1364 bool bi_const_p
= DECL_IS_BUILTIN_CONSTANT_P (fun
);
1366 /* If we aren't requiring a constant expression, defer __builtin_constant_p
1367 in a constexpr function until we have values for the parameters. */
1369 && !ctx
->manifestly_const_eval
1370 && current_function_decl
1371 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
1373 *non_constant_p
= true;
1377 /* For __builtin_is_constant_evaluated, defer it if not
1378 ctx->manifestly_const_eval (as sometimes we try to constant evaluate
1379 without manifestly_const_eval even expressions or parts thereof which
1380 will later be manifestly const_eval evaluated), otherwise fold it to
1382 if (fndecl_built_in_p (fun
, CP_BUILT_IN_IS_CONSTANT_EVALUATED
,
1385 if (!ctx
->manifestly_const_eval
)
1387 *non_constant_p
= true;
1390 return boolean_true_node
;
1393 if (fndecl_built_in_p (fun
, CP_BUILT_IN_SOURCE_LOCATION
, BUILT_IN_FRONTEND
))
1395 temp_override
<tree
> ovr (current_function_decl
);
1396 if (ctx
->call
&& ctx
->call
->fundef
)
1397 current_function_decl
= ctx
->call
->fundef
->decl
;
1398 return fold_builtin_source_location (EXPR_LOCATION (t
));
1403 if (fndecl_built_in_p (fun
, BUILT_IN_NORMAL
))
1404 switch (DECL_FUNCTION_CODE (fun
))
1406 case BUILT_IN_STRLEN
:
1407 case BUILT_IN_STRNLEN
:
1410 case BUILT_IN_MEMCHR
:
1411 case BUILT_IN_STRCHR
:
1412 case BUILT_IN_STRRCHR
:
1416 case BUILT_IN_MEMCMP
:
1417 case BUILT_IN_STRCMP
:
1420 case BUILT_IN_STRSTR
:
1424 case BUILT_IN_ASAN_POINTER_COMPARE
:
1425 case BUILT_IN_ASAN_POINTER_SUBTRACT
:
1426 /* These builtins shall be ignored during constant expression
1433 /* Be permissive for arguments to built-ins; __builtin_constant_p should
1434 return constant false for a non-constant argument. */
1435 constexpr_ctx new_ctx
= *ctx
;
1436 new_ctx
.quiet
= true;
1437 for (i
= 0; i
< nargs
; ++i
)
1439 tree arg
= CALL_EXPR_ARG (t
, i
);
1442 /* To handle string built-ins we need to pass ADDR_EXPR<STRING_CST> since
1443 expand_builtin doesn't know how to look in the values table. */
1444 bool strop
= i
< strops
;
1448 if (TREE_CODE (arg
) == ADDR_EXPR
)
1449 arg
= TREE_OPERAND (arg
, 0);
1454 /* If builtin_valid_in_constant_expr_p is true,
1455 potential_constant_expression_1 has not recursed into the arguments
1456 of the builtin, verify it here. */
1457 if (!builtin_valid_in_constant_expr_p (fun
)
1458 || potential_constant_expression (arg
))
1460 bool dummy1
= false, dummy2
= false;
1461 arg
= cxx_eval_constant_expression (&new_ctx
, arg
, false,
1466 /* For __builtin_constant_p, fold all expressions with constant values
1467 even if they aren't C++ constant-expressions. */
1468 arg
= cp_fold_rvalue (arg
);
1471 if (TREE_CODE (arg
) == CONSTRUCTOR
)
1472 arg
= braced_lists_to_strings (TREE_TYPE (arg
), arg
);
1473 if (TREE_CODE (arg
) == STRING_CST
)
1474 arg
= build_address (arg
);
1482 bool save_ffbcp
= force_folding_builtin_constant_p
;
1483 force_folding_builtin_constant_p
|= ctx
->manifestly_const_eval
;
1484 tree save_cur_fn
= current_function_decl
;
1485 /* Return name of ctx->call->fundef->decl for __builtin_FUNCTION (). */
1486 if (fndecl_built_in_p (fun
, BUILT_IN_FUNCTION
)
1488 && ctx
->call
->fundef
)
1489 current_function_decl
= ctx
->call
->fundef
->decl
;
1490 if (fndecl_built_in_p (fun
,
1491 CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS
,
1494 location_t loc
= EXPR_LOCATION (t
);
1496 VERIFY_CONSTANT (args
[0]);
1498 = fold_builtin_is_pointer_inverconvertible_with_class (loc
, nargs
,
1501 else if (fndecl_built_in_p (fun
,
1502 CP_BUILT_IN_IS_CORRESPONDING_MEMBER
,
1505 location_t loc
= EXPR_LOCATION (t
);
1508 VERIFY_CONSTANT (args
[0]);
1509 VERIFY_CONSTANT (args
[1]);
1511 new_call
= fold_builtin_is_corresponding_member (loc
, nargs
, args
);
1514 new_call
= fold_builtin_call_array (EXPR_LOCATION (t
), TREE_TYPE (t
),
1515 CALL_EXPR_FN (t
), nargs
, args
);
1516 current_function_decl
= save_cur_fn
;
1517 force_folding_builtin_constant_p
= save_ffbcp
;
1518 if (new_call
== NULL
)
1520 if (!*non_constant_p
&& !ctx
->quiet
)
1522 /* Do not allow__builtin_unreachable in constexpr function.
1523 The __builtin_unreachable call with BUILTINS_LOCATION
1524 comes from cp_maybe_instrument_return. */
1525 if (fndecl_built_in_p (fun
, BUILT_IN_UNREACHABLE
)
1526 && EXPR_LOCATION (t
) == BUILTINS_LOCATION
)
1527 error ("%<constexpr%> call flows off the end of the function");
1530 new_call
= build_call_array_loc (EXPR_LOCATION (t
), TREE_TYPE (t
),
1531 CALL_EXPR_FN (t
), nargs
, args
);
1532 error ("%q+E is not a constant expression", new_call
);
1535 *non_constant_p
= true;
1539 if (!potential_constant_expression (new_call
))
1541 if (!*non_constant_p
&& !ctx
->quiet
)
1542 error ("%q+E is not a constant expression", new_call
);
1543 *non_constant_p
= true;
1549 /* memchr returns a pointer into the first argument, but we replaced the
1550 argument above with a STRING_CST; put it back it now. */
1551 tree op
= CALL_EXPR_ARG (t
, strret
-1);
1552 STRIP_NOPS (new_call
);
1553 if (TREE_CODE (new_call
) == POINTER_PLUS_EXPR
)
1554 TREE_OPERAND (new_call
, 0) = op
;
1555 else if (TREE_CODE (new_call
) == ADDR_EXPR
)
1559 return cxx_eval_constant_expression (&new_ctx
, new_call
, lval
,
1560 non_constant_p
, overflow_p
);
1563 /* TEMP is the constant value of a temporary object of type TYPE. Adjust
1564 the type of the value to match. */
1567 adjust_temp_type (tree type
, tree temp
)
1569 if (same_type_p (TREE_TYPE (temp
), type
))
1571 /* Avoid wrapping an aggregate value in a NOP_EXPR. */
1572 if (TREE_CODE (temp
) == CONSTRUCTOR
)
1574 /* build_constructor wouldn't retain various CONSTRUCTOR flags. */
1575 tree t
= copy_node (temp
);
1576 TREE_TYPE (t
) = type
;
1579 if (TREE_CODE (temp
) == EMPTY_CLASS_EXPR
)
1580 return build0 (EMPTY_CLASS_EXPR
, type
);
1581 gcc_assert (scalarish_type_p (type
));
1582 /* Now we know we're dealing with a scalar, and a prvalue of non-class
1583 type is cv-unqualified. */
1584 return cp_fold_convert (cv_unqualified (type
), temp
);
1587 /* If T is a CONSTRUCTOR, return an unshared copy of T and any
1588 sub-CONSTRUCTORs. Otherwise return T.
1590 We use this whenever we initialize an object as a whole, whether it's a
1591 parameter, a local variable, or a subobject, so that subsequent
1592 modifications don't affect other places where it was used. */
1595 unshare_constructor (tree t MEM_STAT_DECL
)
1597 if (!t
|| TREE_CODE (t
) != CONSTRUCTOR
)
1599 auto_vec
<tree
*, 4> ptrs
;
1600 ptrs
.safe_push (&t
);
1601 while (!ptrs
.is_empty ())
1603 tree
*p
= ptrs
.pop ();
1604 tree n
= copy_node (*p PASS_MEM_STAT
);
1605 CONSTRUCTOR_ELTS (n
) = vec_safe_copy (CONSTRUCTOR_ELTS (*p
) PASS_MEM_STAT
);
1607 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (n
);
1608 constructor_elt
*ce
;
1609 for (HOST_WIDE_INT i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
1610 if (ce
->value
&& TREE_CODE (ce
->value
) == CONSTRUCTOR
)
1611 ptrs
.safe_push (&ce
->value
);
1616 /* If T is a CONSTRUCTOR, ggc_free T and any sub-CONSTRUCTORs. */
1619 free_constructor (tree t
)
1621 if (!t
|| TREE_CODE (t
) != CONSTRUCTOR
)
1623 releasing_vec ctors
;
1624 vec_safe_push (ctors
, t
);
1625 while (!ctors
->is_empty ())
1627 tree c
= ctors
->pop ();
1628 if (vec
<constructor_elt
, va_gc
> *elts
= CONSTRUCTOR_ELTS (c
))
1630 constructor_elt
*ce
;
1631 for (HOST_WIDE_INT i
= 0; vec_safe_iterate (elts
, i
, &ce
); ++i
)
1632 if (TREE_CODE (ce
->value
) == CONSTRUCTOR
)
1633 vec_safe_push (ctors
, ce
->value
);
1640 /* Helper function of cxx_bind_parameters_in_call. Return non-NULL
1641 if *TP is address of a static variable (or part of it) currently being
1642 constructed or of a heap artificial variable. */
1645 addr_of_non_const_var (tree
*tp
, int *walk_subtrees
, void *data
)
1647 if (TREE_CODE (*tp
) == ADDR_EXPR
)
1648 if (tree var
= get_base_address (TREE_OPERAND (*tp
, 0)))
1649 if (VAR_P (var
) && TREE_STATIC (var
))
1651 if (DECL_NAME (var
) == heap_uninit_identifier
1652 || DECL_NAME (var
) == heap_identifier
1653 || DECL_NAME (var
) == heap_vec_uninit_identifier
1654 || DECL_NAME (var
) == heap_vec_identifier
)
1657 constexpr_global_ctx
*global
= (constexpr_global_ctx
*) data
;
1658 if (global
->values
.get (var
))
1662 *walk_subtrees
= false;
1666 /* Subroutine of cxx_eval_call_expression.
1667 We are processing a call expression (either CALL_EXPR or
1668 AGGR_INIT_EXPR) in the context of CTX. Evaluate
1669 all arguments and bind their values to correspondings
1670 parameters, making up the NEW_CALL context. */
1673 cxx_bind_parameters_in_call (const constexpr_ctx
*ctx
, tree t
, tree fun
,
1674 bool *non_constant_p
, bool *overflow_p
,
1675 bool *non_constant_args
)
1677 const int nargs
= call_expr_nargs (t
);
1678 tree parms
= DECL_ARGUMENTS (fun
);
1680 /* We don't record ellipsis args below. */
1681 int nparms
= list_length (parms
);
1682 int nbinds
= nargs
< nparms
? nargs
: nparms
;
1683 tree binds
= make_tree_vec (nbinds
);
1684 for (i
= 0; i
< nargs
; ++i
)
1687 tree type
= parms
? TREE_TYPE (parms
) : void_type_node
;
1688 if (parms
&& DECL_BY_REFERENCE (parms
))
1689 type
= TREE_TYPE (type
);
1690 x
= get_nth_callarg (t
, i
);
1691 /* For member function, the first argument is a pointer to the implied
1692 object. For a constructor, it might still be a dummy object, in
1693 which case we get the real argument from ctx. */
1694 if (i
== 0 && DECL_CONSTRUCTOR_P (fun
)
1695 && is_dummy_object (x
))
1698 x
= build_address (x
);
1700 if (TREE_ADDRESSABLE (type
))
1701 /* Undo convert_for_arg_passing work here. */
1702 x
= convert_from_reference (x
);
1703 /* Normally we would strip a TARGET_EXPR in an initialization context
1704 such as this, but here we do the elision differently: we keep the
1705 TARGET_EXPR, and use its CONSTRUCTOR as the value of the parm. */
1706 arg
= cxx_eval_constant_expression (ctx
, x
, /*lval=*/false,
1707 non_constant_p
, overflow_p
);
1708 /* Don't VERIFY_CONSTANT here. */
1709 if (*non_constant_p
&& ctx
->quiet
)
1711 /* Just discard ellipsis args after checking their constantitude. */
1715 if (!*non_constant_p
)
1717 /* Make sure the binding has the same type as the parm. But
1718 only for constant args. */
1719 if (!TYPE_REF_P (type
))
1720 arg
= adjust_temp_type (type
, arg
);
1721 if (!TREE_CONSTANT (arg
))
1722 *non_constant_args
= true;
1723 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
1724 /* The destructor needs to see any modifications the callee makes
1726 *non_constant_args
= true;
1727 /* If arg is or contains address of a heap artificial variable or
1728 of a static variable being constructed, avoid caching the
1729 function call, as those variables might be modified by the
1730 function, or might be modified by the callers in between
1731 the cached function and just read by the function. */
1732 else if (!*non_constant_args
1733 && cp_walk_tree (&arg
, addr_of_non_const_var
, ctx
->global
,
1735 *non_constant_args
= true;
1737 /* For virtual calls, adjust the this argument, so that it is
1738 the object on which the method is called, rather than
1739 one of its bases. */
1740 if (i
== 0 && DECL_VIRTUAL_P (fun
))
1744 if (TREE_CODE (addr
) == ADDR_EXPR
)
1746 tree obj
= TREE_OPERAND (addr
, 0);
1747 while (TREE_CODE (obj
) == COMPONENT_REF
1748 && DECL_FIELD_IS_BASE (TREE_OPERAND (obj
, 1))
1749 && !same_type_ignoring_top_level_qualifiers_p
1750 (TREE_TYPE (obj
), DECL_CONTEXT (fun
)))
1751 obj
= TREE_OPERAND (obj
, 0);
1752 if (obj
!= TREE_OPERAND (addr
, 0))
1753 arg
= build_fold_addr_expr_with_type (obj
,
1757 TREE_VEC_ELT (binds
, i
) = arg
;
1759 parms
= TREE_CHAIN (parms
);
1765 /* Variables and functions to manage constexpr call expansion context.
1766 These do not need to be marked for PCH or GC. */
1768 /* FIXME remember and print actual constant arguments. */
1769 static vec
<tree
> call_stack
;
1770 static int call_stack_tick
;
1771 static int last_cx_error_tick
;
1774 push_cx_call_context (tree call
)
1777 if (!EXPR_HAS_LOCATION (call
))
1778 SET_EXPR_LOCATION (call
, input_location
);
1779 call_stack
.safe_push (call
);
1780 int len
= call_stack
.length ();
1781 if (len
> max_constexpr_depth
)
1787 pop_cx_call_context (void)
1794 cx_error_context (void)
1796 vec
<tree
> r
= vNULL
;
1797 if (call_stack_tick
!= last_cx_error_tick
1798 && !call_stack
.is_empty ())
1800 last_cx_error_tick
= call_stack_tick
;
1804 /* Evaluate a call T to a GCC internal function when possible and return
1805 the evaluated result or, under the control of CTX, give an error, set
1806 NON_CONSTANT_P, and return the unevaluated call T otherwise. */
1809 cxx_eval_internal_function (const constexpr_ctx
*ctx
, tree t
,
1811 bool *non_constant_p
, bool *overflow_p
)
1813 enum tree_code opcode
= ERROR_MARK
;
1815 switch (CALL_EXPR_IFN (t
))
1817 case IFN_UBSAN_NULL
:
1818 case IFN_UBSAN_BOUNDS
:
1819 case IFN_UBSAN_VPTR
:
1820 case IFN_FALLTHROUGH
:
1823 case IFN_ADD_OVERFLOW
:
1826 case IFN_SUB_OVERFLOW
:
1827 opcode
= MINUS_EXPR
;
1829 case IFN_MUL_OVERFLOW
:
1834 return cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 0),
1835 false, non_constant_p
, overflow_p
);
1837 case IFN_VEC_CONVERT
:
1839 tree arg
= cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 0),
1840 false, non_constant_p
,
1842 if (TREE_CODE (arg
) == VECTOR_CST
)
1843 return fold_const_call (CFN_VEC_CONVERT
, TREE_TYPE (t
), arg
);
1846 *non_constant_p
= true;
1853 error_at (cp_expr_loc_or_input_loc (t
),
1854 "call to internal function %qE", t
);
1855 *non_constant_p
= true;
1859 /* Evaluate constant arguments using OPCODE and return a complex
1860 number containing the result and the overflow bit. */
1861 tree arg0
= cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 0), lval
,
1862 non_constant_p
, overflow_p
);
1863 tree arg1
= cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 1), lval
,
1864 non_constant_p
, overflow_p
);
1866 if (TREE_CODE (arg0
) == INTEGER_CST
&& TREE_CODE (arg1
) == INTEGER_CST
)
1868 location_t loc
= cp_expr_loc_or_input_loc (t
);
1869 tree type
= TREE_TYPE (TREE_TYPE (t
));
1870 tree result
= fold_binary_loc (loc
, opcode
, type
,
1871 fold_convert_loc (loc
, type
, arg0
),
1872 fold_convert_loc (loc
, type
, arg1
));
1874 = build_int_cst (type
, arith_overflowed_p (opcode
, type
, arg0
, arg1
));
1875 /* Reset TREE_OVERFLOW to avoid warnings for the overflow. */
1876 if (TREE_OVERFLOW (result
))
1877 TREE_OVERFLOW (result
) = 0;
1879 return build_complex (TREE_TYPE (t
), result
, ovf
);
1882 *non_constant_p
= true;
1886 /* Clean CONSTRUCTOR_NO_CLEARING from CTOR and its sub-aggregates. */
1889 clear_no_implicit_zero (tree ctor
)
1891 if (CONSTRUCTOR_NO_CLEARING (ctor
))
1893 CONSTRUCTOR_NO_CLEARING (ctor
) = false;
1894 for (auto &e
: CONSTRUCTOR_ELTS (ctor
))
1895 if (TREE_CODE (e
.value
) == CONSTRUCTOR
)
1896 clear_no_implicit_zero (e
.value
);
1900 /* Complain about a const object OBJ being modified in a constant expression.
1901 EXPR is the MODIFY_EXPR expression performing the modification. */
1904 modifying_const_object_error (tree expr
, tree obj
)
1906 location_t loc
= cp_expr_loc_or_input_loc (expr
);
1907 auto_diagnostic_group d
;
1908 error_at (loc
, "modifying a const object %qE is not allowed in "
1909 "a constant expression", TREE_OPERAND (expr
, 0));
1910 inform (location_of (obj
), "originally declared %<const%> here");
1913 /* Return true if FNDECL is a replaceable global allocation function that
1914 should be useable during constant expression evaluation. */
1917 cxx_replaceable_global_alloc_fn (tree fndecl
)
1919 return (cxx_dialect
>= cxx20
1920 && IDENTIFIER_NEWDEL_OP_P (DECL_NAME (fndecl
))
1921 && CP_DECL_CONTEXT (fndecl
) == global_namespace
1922 && (DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl
)
1923 || DECL_IS_OPERATOR_DELETE_P (fndecl
)));
1926 /* Return true if FNDECL is a placement new function that should be
1927 useable during constant expression evaluation of std::construct_at. */
1930 cxx_placement_new_fn (tree fndecl
)
1932 if (cxx_dialect
>= cxx20
1933 && IDENTIFIER_NEW_OP_P (DECL_NAME (fndecl
))
1934 && CP_DECL_CONTEXT (fndecl
) == global_namespace
1935 && !DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl
)
1936 && TREE_CODE (TREE_TYPE (fndecl
)) == FUNCTION_TYPE
)
1938 tree first_arg
= TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
1939 if (TREE_VALUE (first_arg
) == ptr_type_node
1940 && TREE_CHAIN (first_arg
) == void_list_node
)
1946 /* Return true if FNDECL is std::construct_at. */
1949 is_std_construct_at (tree fndecl
)
1951 if (!decl_in_std_namespace_p (fndecl
))
1954 tree name
= DECL_NAME (fndecl
);
1955 return name
&& id_equal (name
, "construct_at");
1958 /* Overload for the above taking constexpr_call*. */
1961 is_std_construct_at (const constexpr_call
*call
)
1965 && is_std_construct_at (call
->fundef
->decl
));
1968 /* Return true if FNDECL is std::allocator<T>::{,de}allocate. */
1971 is_std_allocator_allocate (tree fndecl
)
1973 tree name
= DECL_NAME (fndecl
);
1974 if (name
== NULL_TREE
1975 || !(id_equal (name
, "allocate") || id_equal (name
, "deallocate")))
1978 tree ctx
= DECL_CONTEXT (fndecl
);
1979 if (ctx
== NULL_TREE
|| !CLASS_TYPE_P (ctx
) || !TYPE_MAIN_DECL (ctx
))
1982 tree decl
= TYPE_MAIN_DECL (ctx
);
1983 name
= DECL_NAME (decl
);
1984 if (name
== NULL_TREE
|| !id_equal (name
, "allocator"))
1987 return decl_in_std_namespace_p (decl
);
1990 /* Overload for the above taking constexpr_call*. */
1993 is_std_allocator_allocate (const constexpr_call
*call
)
1997 && is_std_allocator_allocate (call
->fundef
->decl
));
2000 /* Return true if FNDECL is __dynamic_cast. */
2003 cxx_dynamic_cast_fn_p (tree fndecl
)
2005 return (cxx_dialect
>= cxx20
2006 && id_equal (DECL_NAME (fndecl
), "__dynamic_cast")
2007 && CP_DECL_CONTEXT (fndecl
) == global_namespace
);
2010 /* Often, we have an expression in the form of address + offset, e.g.
2011 "&_ZTV1A + 16". Extract the object from it, i.e. "_ZTV1A". */
2014 extract_obj_from_addr_offset (tree expr
)
2016 if (TREE_CODE (expr
) == POINTER_PLUS_EXPR
)
2017 expr
= TREE_OPERAND (expr
, 0);
2019 if (TREE_CODE (expr
) == ADDR_EXPR
)
2020 expr
= TREE_OPERAND (expr
, 0);
2024 /* Given a PATH like
2026 g.D.2181.D.2154.D.2102.D.2093
2028 find a component with type TYPE. Return NULL_TREE if not found, and
2029 error_mark_node if the component is not accessible. If STOP is non-null,
2030 this function will return NULL_TREE if STOP is found before TYPE. */
2033 get_component_with_type (tree path
, tree type
, tree stop
)
2037 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path
), type
))
2041 && (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path
),
2044 else if (TREE_CODE (path
) == COMPONENT_REF
2045 && DECL_FIELD_IS_BASE (TREE_OPERAND (path
, 1)))
2047 /* We need to check that the component we're accessing is in fact
2049 if (TREE_PRIVATE (TREE_OPERAND (path
, 1))
2050 || TREE_PROTECTED (TREE_OPERAND (path
, 1)))
2051 return error_mark_node
;
2052 path
= TREE_OPERAND (path
, 0);
2059 /* Evaluate a call to __dynamic_cast (permitted by P1327R1).
2061 The declaration of __dynamic_cast is:
2063 void* __dynamic_cast (const void* __src_ptr,
2064 const __class_type_info* __src_type,
2065 const __class_type_info* __dst_type,
2066 ptrdiff_t __src2dst);
2068 where src2dst has the following possible values
2070 >-1: src_type is a unique public non-virtual base of dst_type
2071 dst_ptr + src2dst == src_ptr
2072 -1: unspecified relationship
2073 -2: src_type is not a public base of dst_type
2074 -3: src_type is a multiple public non-virtual base of dst_type
2076 Since literal types can't have virtual bases, we only expect hint >=0,
2080 cxx_eval_dynamic_cast_fn (const constexpr_ctx
*ctx
, tree call
,
2081 bool *non_constant_p
, bool *overflow_p
)
2083 /* T will be something like
2084 __dynamic_cast ((B*) b, &_ZTI1B, &_ZTI1D, 8)
2086 gcc_assert (call_expr_nargs (call
) == 4);
2087 tsubst_flags_t complain
= ctx
->quiet
? tf_none
: tf_warning_or_error
;
2088 tree obj
= CALL_EXPR_ARG (call
, 0);
2089 tree type
= CALL_EXPR_ARG (call
, 2);
2090 HOST_WIDE_INT hint
= int_cst_value (CALL_EXPR_ARG (call
, 3));
2091 location_t loc
= cp_expr_loc_or_input_loc (call
);
2093 /* Get the target type of the dynamic_cast. */
2094 gcc_assert (TREE_CODE (type
) == ADDR_EXPR
);
2095 type
= TREE_OPERAND (type
, 0);
2096 type
= TREE_TYPE (DECL_NAME (type
));
2098 /* TYPE can only be either T* or T&. We can't know which of these it
2099 is by looking at TYPE, but OBJ will be "(T*) x" in the first case,
2100 and something like "(T*)(T&)(T*) x" in the second case. */
2101 bool reference_p
= false;
2102 while (CONVERT_EXPR_P (obj
) || TREE_CODE (obj
) == SAVE_EXPR
)
2104 reference_p
|= TYPE_REF_P (TREE_TYPE (obj
));
2105 obj
= TREE_OPERAND (obj
, 0);
2108 /* Evaluate the object so that we know its dynamic type. */
2109 obj
= cxx_eval_constant_expression (ctx
, obj
, /*lval*/false, non_constant_p
,
2111 if (*non_constant_p
)
2114 /* We expect OBJ to be in form of &d.D.2102 when HINT == 0,
2115 but when HINT is > 0, it can also be something like
2116 &d.D.2102 + 18446744073709551608, which includes the BINFO_OFFSET. */
2117 obj
= extract_obj_from_addr_offset (obj
);
2118 const tree objtype
= TREE_TYPE (obj
);
2119 /* If OBJ doesn't refer to a base field, we're done. */
2120 if (tree t
= (TREE_CODE (obj
) == COMPONENT_REF
2121 ? TREE_OPERAND (obj
, 1) : obj
))
2122 if (TREE_CODE (t
) != FIELD_DECL
|| !DECL_FIELD_IS_BASE (t
))
2128 error_at (loc
, "reference %<dynamic_cast%> failed");
2129 inform (loc
, "dynamic type %qT of its operand does "
2130 "not have a base class of type %qT",
2133 *non_constant_p
= true;
2135 return integer_zero_node
;
2138 /* [class.cdtor] When a dynamic_cast is used in a constructor ...
2139 or in a destructor ... if the operand of the dynamic_cast refers
2140 to the object under construction or destruction, this object is
2141 considered to be a most derived object that has the type of the
2142 constructor or destructor's class. */
2143 tree vtable
= build_vfield_ref (obj
, objtype
);
2144 vtable
= cxx_eval_constant_expression (ctx
, vtable
, /*lval*/false,
2145 non_constant_p
, overflow_p
);
2146 if (*non_constant_p
)
2148 /* With -fsanitize=vptr, we initialize all vtable pointers to null,
2149 so it's possible that we got a null pointer now. */
2150 if (integer_zerop (vtable
))
2153 error_at (loc
, "virtual table pointer is used uninitialized");
2154 *non_constant_p
= true;
2155 return integer_zero_node
;
2157 /* VTABLE will be &_ZTV1A + 16 or similar, get _ZTV1A. */
2158 vtable
= extract_obj_from_addr_offset (vtable
);
2159 const tree mdtype
= DECL_CONTEXT (vtable
);
2161 /* Given dynamic_cast<T>(v),
2163 [expr.dynamic.cast] If C is the class type to which T points or refers,
2164 the runtime check logically executes as follows:
2166 If, in the most derived object pointed (referred) to by v, v points
2167 (refers) to a public base class subobject of a C object, and if only
2168 one object of type C is derived from the subobject pointed (referred)
2169 to by v the result points (refers) to that C object.
2171 In this case, HINT >= 0 or -3. */
2172 if (hint
>= 0 || hint
== -3)
2174 /* Look for a component with type TYPE. */
2175 tree t
= get_component_with_type (obj
, type
, mdtype
);
2176 /* If not accessible, give an error. */
2177 if (t
== error_mark_node
)
2183 error_at (loc
, "reference %<dynamic_cast%> failed");
2184 inform (loc
, "static type %qT of its operand is a "
2185 "non-public base class of dynamic type %qT",
2189 *non_constant_p
= true;
2191 return integer_zero_node
;
2194 /* The result points to the TYPE object. */
2195 return cp_build_addr_expr (t
, complain
);
2196 /* Else, TYPE was not found, because the HINT turned out to be wrong.
2197 Fall through to the normal processing. */
2200 /* Otherwise, if v points (refers) to a public base class subobject of the
2201 most derived object, and the type of the most derived object has a base
2202 class, of type C, that is unambiguous and public, the result points
2203 (refers) to the C subobject of the most derived object.
2205 But it can also be an invalid case. */
2207 /* Get the most derived object. */
2208 obj
= get_component_with_type (obj
, mdtype
, NULL_TREE
);
2209 if (obj
== error_mark_node
)
2215 error_at (loc
, "reference %<dynamic_cast%> failed");
2216 inform (loc
, "static type %qT of its operand is a non-public"
2217 " base class of dynamic type %qT", objtype
, mdtype
);
2219 *non_constant_p
= true;
2221 return integer_zero_node
;
2226 /* Check that the type of the most derived object has a base class
2227 of type TYPE that is unambiguous and public. */
2229 tree binfo
= lookup_base (mdtype
, type
, ba_check
, &b_kind
, tf_none
);
2230 if (!binfo
|| binfo
== error_mark_node
)
2236 error_at (loc
, "reference %<dynamic_cast%> failed");
2237 if (b_kind
== bk_ambig
)
2238 inform (loc
, "%qT is an ambiguous base class of dynamic "
2239 "type %qT of its operand", type
, mdtype
);
2241 inform (loc
, "dynamic type %qT of its operand does not "
2242 "have an unambiguous public base class %qT",
2245 *non_constant_p
= true;
2247 return integer_zero_node
;
2249 /* If so, return the TYPE subobject of the most derived object. */
2250 obj
= convert_to_base_statically (obj
, binfo
);
2251 return cp_build_addr_expr (obj
, complain
);
2254 /* Data structure used by replace_result_decl and replace_result_decl_r. */
2256 struct replace_result_decl_data
2258 /* The RESULT_DECL we want to replace. */
2260 /* The replacement for DECL. */
2262 /* Whether we've performed any replacements. */
2266 /* Helper function for replace_result_decl, called through cp_walk_tree. */
2269 replace_result_decl_r (tree
*tp
, int *walk_subtrees
, void *data
)
2271 replace_result_decl_data
*d
= (replace_result_decl_data
*) data
;
2275 *tp
= unshare_expr (d
->replacement
);
2279 else if (TYPE_P (*tp
))
2285 /* Replace every occurrence of DECL, a RESULT_DECL, with (an unshared copy of)
2286 REPLACEMENT within the reduced constant expression *TP. Returns true iff a
2287 replacement was performed. */
2290 replace_result_decl (tree
*tp
, tree decl
, tree replacement
)
2292 gcc_checking_assert (TREE_CODE (decl
) == RESULT_DECL
2293 && (same_type_ignoring_top_level_qualifiers_p
2294 (TREE_TYPE (decl
), TREE_TYPE (replacement
))));
2295 replace_result_decl_data data
= { decl
, replacement
, false };
2296 cp_walk_tree_without_duplicates (tp
, replace_result_decl_r
, &data
);
2297 return data
.changed
;
2300 /* Evaluate the call T to virtual function thunk THUNK_FNDECL. */
2303 cxx_eval_thunk_call (const constexpr_ctx
*ctx
, tree t
, tree thunk_fndecl
,
2305 bool *non_constant_p
, bool *overflow_p
)
2307 tree function
= THUNK_TARGET (thunk_fndecl
);
2309 if (THUNK_VIRTUAL_OFFSET (thunk_fndecl
))
2313 if (!DECL_DECLARED_CONSTEXPR_P (function
))
2315 error ("call to non-%<constexpr%> function %qD", function
);
2316 explain_invalid_constexpr_fn (function
);
2319 /* virtual_offset is only set for virtual bases, which make the
2320 class non-literal, so we don't need to handle it here. */
2321 error ("calling constexpr member function %qD through virtual "
2322 "base subobject", function
);
2324 *non_constant_p
= true;
2328 tree new_call
= copy_node (t
);
2329 CALL_EXPR_FN (new_call
) = function
;
2330 TREE_TYPE (new_call
) = TREE_TYPE (TREE_TYPE (function
));
2332 tree offset
= size_int (THUNK_FIXED_OFFSET (thunk_fndecl
));
2334 if (DECL_THIS_THUNK_P (thunk_fndecl
))
2336 /* 'this'-adjusting thunk. */
2337 tree this_arg
= CALL_EXPR_ARG (t
, 0);
2338 this_arg
= build2 (POINTER_PLUS_EXPR
, TREE_TYPE (this_arg
),
2340 CALL_EXPR_ARG (new_call
, 0) = this_arg
;
2343 /* Return-adjusting thunk. */
2344 new_call
= build2 (POINTER_PLUS_EXPR
, TREE_TYPE (new_call
),
2347 return cxx_eval_constant_expression (ctx
, new_call
, lval
,
2348 non_constant_p
, overflow_p
);
2351 /* If OBJECT is of const class type, evaluate it to a CONSTRUCTOR and set
2352 its TREE_READONLY flag according to READONLY_P. Used for constexpr
2353 'tors to detect modifying const objects in a constexpr context. */
2356 cxx_set_object_constness (const constexpr_ctx
*ctx
, tree object
,
2357 bool readonly_p
, bool *non_constant_p
,
2360 if (CLASS_TYPE_P (TREE_TYPE (object
))
2361 && CP_TYPE_CONST_P (TREE_TYPE (object
)))
2363 /* Subobjects might not be stored in ctx->global->values but we
2364 can get its CONSTRUCTOR by evaluating *this. */
2365 tree e
= cxx_eval_constant_expression (ctx
, object
, /*lval*/false,
2366 non_constant_p
, overflow_p
);
2367 if (TREE_CODE (e
) == CONSTRUCTOR
&& !*non_constant_p
)
2368 TREE_READONLY (e
) = readonly_p
;
2372 /* Subroutine of cxx_eval_constant_expression.
2373 Evaluate the call expression tree T in the context of OLD_CALL expression
2377 cxx_eval_call_expression (const constexpr_ctx
*ctx
, tree t
,
2379 bool *non_constant_p
, bool *overflow_p
)
2381 /* Handle concept checks separately. */
2382 if (concept_check_p (t
))
2383 return evaluate_concept_check (t
);
2385 location_t loc
= cp_expr_loc_or_input_loc (t
);
2386 tree fun
= get_function_named_in_call (t
);
2387 constexpr_call new_call
2388 = { NULL
, NULL
, NULL
, 0, ctx
->manifestly_const_eval
};
2391 if (fun
== NULL_TREE
)
2392 return cxx_eval_internal_function (ctx
, t
, lval
,
2393 non_constant_p
, overflow_p
);
2395 if (TREE_CODE (fun
) != FUNCTION_DECL
)
2397 /* Might be a constexpr function pointer. */
2398 fun
= cxx_eval_constant_expression (ctx
, fun
,
2399 /*lval*/false, non_constant_p
,
2402 if (TREE_CODE (fun
) == ADDR_EXPR
)
2403 fun
= TREE_OPERAND (fun
, 0);
2404 /* For TARGET_VTABLE_USES_DESCRIPTORS targets, there is no
2405 indirection, the called expression is a pointer into the
2406 virtual table which should contain FDESC_EXPR. Extract the
2407 FUNCTION_DECL from there. */
2408 else if (TARGET_VTABLE_USES_DESCRIPTORS
2409 && TREE_CODE (fun
) == POINTER_PLUS_EXPR
2410 && TREE_CODE (TREE_OPERAND (fun
, 0)) == ADDR_EXPR
2411 && TREE_CODE (TREE_OPERAND (fun
, 1)) == INTEGER_CST
)
2413 tree d
= TREE_OPERAND (TREE_OPERAND (fun
, 0), 0);
2415 && DECL_VTABLE_OR_VTT_P (d
)
2416 && TREE_CODE (TREE_TYPE (d
)) == ARRAY_TYPE
2417 && TREE_TYPE (TREE_TYPE (d
)) == vtable_entry_type
2419 && TREE_CODE (DECL_INITIAL (d
)) == CONSTRUCTOR
)
2421 tree i
= int_const_binop (TRUNC_DIV_EXPR
, TREE_OPERAND (fun
, 1),
2422 TYPE_SIZE_UNIT (vtable_entry_type
));
2423 HOST_WIDE_INT idx
= find_array_ctor_elt (DECL_INITIAL (d
), i
);
2427 = (*CONSTRUCTOR_ELTS (DECL_INITIAL (d
)))[idx
].value
;
2428 if (TREE_CODE (fdesc
) == FDESC_EXPR
2429 && integer_zerop (TREE_OPERAND (fdesc
, 1)))
2430 fun
= TREE_OPERAND (fdesc
, 0);
2435 if (TREE_CODE (fun
) != FUNCTION_DECL
)
2437 if (!ctx
->quiet
&& !*non_constant_p
)
2438 error_at (loc
, "expression %qE does not designate a %<constexpr%> "
2440 *non_constant_p
= true;
2443 if (DECL_CLONED_FUNCTION_P (fun
) && !DECL_DELETING_DESTRUCTOR_P (fun
))
2444 fun
= DECL_CLONED_FUNCTION (fun
);
2446 if (is_ubsan_builtin_p (fun
))
2449 if (fndecl_built_in_p (fun
))
2450 return cxx_eval_builtin_function_call (ctx
, t
, fun
,
2451 lval
, non_constant_p
, overflow_p
);
2452 if (DECL_THUNK_P (fun
))
2453 return cxx_eval_thunk_call (ctx
, t
, fun
, lval
, non_constant_p
, overflow_p
);
2454 if (!maybe_constexpr_fn (fun
))
2456 if (TREE_CODE (t
) == CALL_EXPR
2457 && cxx_replaceable_global_alloc_fn (fun
)
2458 && (CALL_FROM_NEW_OR_DELETE_P (t
)
2459 || is_std_allocator_allocate (ctx
->call
)))
2461 const int nargs
= call_expr_nargs (t
);
2462 tree arg0
= NULL_TREE
;
2463 for (int i
= 0; i
< nargs
; ++i
)
2465 tree arg
= CALL_EXPR_ARG (t
, i
);
2466 arg
= cxx_eval_constant_expression (ctx
, arg
, false,
2467 non_constant_p
, overflow_p
);
2468 VERIFY_CONSTANT (arg
);
2473 if (IDENTIFIER_NEW_OP_P (DECL_NAME (fun
)))
2475 tree type
= build_array_type_nelts (char_type_node
,
2476 tree_to_uhwi (arg0
));
2477 tree var
= build_decl (loc
, VAR_DECL
,
2478 (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun
))
2480 ? heap_vec_uninit_identifier
2481 : heap_uninit_identifier
,
2483 DECL_ARTIFICIAL (var
) = 1;
2484 TREE_STATIC (var
) = 1;
2485 // Temporarily register the artificial var in varpool,
2486 // so that comparisons of its address against NULL are folded
2487 // through nonzero_address even with
2488 // -fno-delete-null-pointer-checks or that comparison of
2489 // addresses of different heap artificial vars is folded too.
2490 // See PR98988 and PR99031.
2491 varpool_node::finalize_decl (var
);
2492 ctx
->global
->heap_vars
.safe_push (var
);
2493 ctx
->global
->values
.put (var
, NULL_TREE
);
2494 return fold_convert (ptr_type_node
, build_address (var
));
2499 if (TREE_CODE (arg0
) == ADDR_EXPR
2500 && VAR_P (TREE_OPERAND (arg0
, 0)))
2502 tree var
= TREE_OPERAND (arg0
, 0);
2503 if (DECL_NAME (var
) == heap_uninit_identifier
2504 || DECL_NAME (var
) == heap_identifier
)
2506 if (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun
))
2511 error_at (loc
, "array deallocation of object "
2512 "allocated with non-array "
2514 inform (DECL_SOURCE_LOCATION (var
),
2515 "allocation performed here");
2517 *non_constant_p
= true;
2520 DECL_NAME (var
) = heap_deleted_identifier
;
2521 ctx
->global
->values
.remove (var
);
2522 ctx
->global
->heap_dealloc_count
++;
2525 else if (DECL_NAME (var
) == heap_vec_uninit_identifier
2526 || DECL_NAME (var
) == heap_vec_identifier
)
2528 if ((IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun
))
2529 & OVL_OP_FLAG_VEC
) == 0)
2533 error_at (loc
, "non-array deallocation of "
2534 "object allocated with array "
2536 inform (DECL_SOURCE_LOCATION (var
),
2537 "allocation performed here");
2539 *non_constant_p
= true;
2542 DECL_NAME (var
) = heap_deleted_identifier
;
2543 ctx
->global
->values
.remove (var
);
2544 ctx
->global
->heap_dealloc_count
++;
2547 else if (DECL_NAME (var
) == heap_deleted_identifier
)
2550 error_at (loc
, "deallocation of already deallocated "
2552 *non_constant_p
= true;
2557 error_at (loc
, "deallocation of storage that was "
2558 "not previously allocated");
2559 *non_constant_p
= true;
2563 /* Allow placement new in std::construct_at, just return the second
2565 if (TREE_CODE (t
) == CALL_EXPR
2566 && cxx_placement_new_fn (fun
)
2567 && is_std_construct_at (ctx
->call
))
2569 const int nargs
= call_expr_nargs (t
);
2570 tree arg1
= NULL_TREE
;
2571 for (int i
= 0; i
< nargs
; ++i
)
2573 tree arg
= CALL_EXPR_ARG (t
, i
);
2574 arg
= cxx_eval_constant_expression (ctx
, arg
, false,
2575 non_constant_p
, overflow_p
);
2579 VERIFY_CONSTANT (arg
);
2584 else if (cxx_dynamic_cast_fn_p (fun
))
2585 return cxx_eval_dynamic_cast_fn (ctx
, t
, non_constant_p
, overflow_p
);
2589 if (!lambda_static_thunk_p (fun
))
2590 error_at (loc
, "call to non-%<constexpr%> function %qD", fun
);
2591 explain_invalid_constexpr_fn (fun
);
2593 *non_constant_p
= true;
2597 constexpr_ctx new_ctx
= *ctx
;
2598 if (DECL_CONSTRUCTOR_P (fun
) && !ctx
->object
2599 && TREE_CODE (t
) == AGGR_INIT_EXPR
)
2601 /* We want to have an initialization target for an AGGR_INIT_EXPR.
2602 If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
2603 new_ctx
.object
= AGGR_INIT_EXPR_SLOT (t
);
2604 tree ctor
= new_ctx
.ctor
= build_constructor (DECL_CONTEXT (fun
), NULL
);
2605 CONSTRUCTOR_NO_CLEARING (ctor
) = true;
2606 ctx
->global
->values
.put (new_ctx
.object
, ctor
);
2610 /* Shortcut trivial constructor/op=. */
2611 if (trivial_fn_p (fun
))
2613 tree init
= NULL_TREE
;
2614 if (call_expr_nargs (t
) == 2)
2615 init
= convert_from_reference (get_nth_callarg (t
, 1));
2616 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
2617 && AGGR_INIT_ZERO_FIRST (t
))
2618 init
= build_zero_init (DECL_CONTEXT (fun
), NULL_TREE
, false);
2621 tree op
= get_nth_callarg (t
, 0);
2622 if (is_dummy_object (op
))
2625 op
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (op
)), op
);
2626 tree set
= build2 (MODIFY_EXPR
, TREE_TYPE (op
), op
, init
);
2627 new_ctx
.call
= &new_call
;
2628 return cxx_eval_constant_expression (&new_ctx
, set
, lval
,
2629 non_constant_p
, overflow_p
);
2633 bool non_constant_args
= false;
2635 = cxx_bind_parameters_in_call (ctx
, t
, fun
, non_constant_p
,
2636 overflow_p
, &non_constant_args
);
2638 /* We build up the bindings list before we know whether we already have this
2639 call cached. If we don't end up saving these bindings, ggc_free them when
2640 this function exits. */
2645 free_bindings (tree
&b
): bindings (&b
) { }
2646 ~free_bindings () { if (bindings
) ggc_free (*bindings
); }
2647 void preserve () { bindings
= NULL
; }
2648 } fb (new_call
.bindings
);
2650 if (*non_constant_p
)
2653 /* We can't defer instantiating the function any longer. */
2654 if (!DECL_INITIAL (fun
)
2655 && DECL_TEMPLOID_INSTANTIATION (fun
)
2656 && !uid_sensitive_constexpr_evaluation_p ())
2658 location_t save_loc
= input_location
;
2659 input_location
= loc
;
2661 if (ctx
->manifestly_const_eval
)
2662 FNDECL_MANIFESTLY_CONST_EVALUATED (fun
) = true;
2663 instantiate_decl (fun
, /*defer_ok*/false, /*expl_inst*/false);
2665 input_location
= save_loc
;
2668 /* If in direct recursive call, optimize definition search. */
2669 if (ctx
&& ctx
->call
&& ctx
->call
->fundef
&& ctx
->call
->fundef
->decl
== fun
)
2670 new_call
.fundef
= ctx
->call
->fundef
;
2673 new_call
.fundef
= retrieve_constexpr_fundef (fun
);
2674 if (new_call
.fundef
== NULL
|| new_call
.fundef
->body
== NULL
2675 || new_call
.fundef
->result
== error_mark_node
2676 || fun
== current_function_decl
)
2680 /* We need to check for current_function_decl here in case we're
2681 being called during cp_fold_function, because at that point
2682 DECL_INITIAL is set properly and we have a fundef but we
2683 haven't lowered invisirefs yet (c++/70344). */
2684 if (DECL_INITIAL (fun
) == error_mark_node
2685 || fun
== current_function_decl
)
2686 error_at (loc
, "%qD called in a constant expression before its "
2687 "definition is complete", fun
);
2688 else if (DECL_INITIAL (fun
))
2690 /* The definition of fun was somehow unsuitable. But pretend
2691 that lambda static thunks don't exist. */
2692 if (!lambda_static_thunk_p (fun
))
2693 error_at (loc
, "%qD called in a constant expression", fun
);
2694 explain_invalid_constexpr_fn (fun
);
2697 error_at (loc
, "%qD used before its definition", fun
);
2699 *non_constant_p
= true;
2704 depth_ok
= push_cx_call_context (t
);
2706 /* Remember the object we are constructing or destructing. */
2707 tree new_obj
= NULL_TREE
;
2708 if (DECL_CONSTRUCTOR_P (fun
) || DECL_DESTRUCTOR_P (fun
))
2710 /* In a cdtor, it should be the first `this' argument.
2711 At this point it has already been evaluated in the call
2712 to cxx_bind_parameters_in_call. */
2713 new_obj
= TREE_VEC_ELT (new_call
.bindings
, 0);
2714 STRIP_NOPS (new_obj
);
2715 if (TREE_CODE (new_obj
) == ADDR_EXPR
)
2716 new_obj
= TREE_OPERAND (new_obj
, 0);
2718 if (ctx
->call
&& ctx
->call
->fundef
2719 && DECL_CONSTRUCTOR_P (ctx
->call
->fundef
->decl
))
2721 tree cur_obj
= TREE_VEC_ELT (ctx
->call
->bindings
, 0);
2722 STRIP_NOPS (cur_obj
);
2723 if (TREE_CODE (cur_obj
) == ADDR_EXPR
)
2724 cur_obj
= TREE_OPERAND (cur_obj
, 0);
2725 if (new_obj
== cur_obj
)
2726 /* We're calling the target constructor of a delegating
2727 constructor, or accessing a base subobject through a
2728 NOP_EXPR as part of a call to a base constructor, so
2729 there is no new (sub)object. */
2730 new_obj
= NULL_TREE
;
2734 tree result
= NULL_TREE
;
2736 constexpr_call
*entry
= NULL
;
2737 if (depth_ok
&& !non_constant_args
&& ctx
->strict
)
2739 new_call
.hash
= constexpr_fundef_hasher::hash (new_call
.fundef
);
2741 = iterative_hash_template_arg (new_call
.bindings
, new_call
.hash
);
2743 = iterative_hash_object (ctx
->manifestly_const_eval
, new_call
.hash
);
2745 /* If we have seen this call before, we are done. */
2746 maybe_initialize_constexpr_call_table ();
2747 constexpr_call
**slot
2748 = constexpr_call_table
->find_slot (&new_call
, INSERT
);
2752 /* Only cache up to constexpr_cache_depth to limit memory use. */
2753 if (depth_ok
< constexpr_cache_depth
)
2755 /* We need to keep a pointer to the entry, not just the slot, as
2756 the slot can move during evaluation of the body. */
2757 *slot
= entry
= ggc_alloc
<constexpr_call
> ();
2762 /* Calls that are in progress have their result set to NULL, so that we
2763 can detect circular dependencies. Now that we only cache up to
2764 constexpr_cache_depth this won't catch circular dependencies that
2765 start deeper, but they'll hit the recursion or ops limit. */
2766 else if (entry
->result
== NULL
)
2769 error ("call has circular dependency");
2770 *non_constant_p
= true;
2771 entry
->result
= result
= error_mark_node
;
2774 result
= entry
->result
;
2780 error ("%<constexpr%> evaluation depth exceeds maximum of %d (use "
2781 "%<-fconstexpr-depth=%> to increase the maximum)",
2782 max_constexpr_depth
);
2783 *non_constant_p
= true;
2784 result
= error_mark_node
;
2788 bool cacheable
= true;
2789 if (result
&& result
!= error_mark_node
)
2791 else if (!DECL_SAVED_TREE (fun
))
2793 /* When at_eof >= 2, cgraph has started throwing away
2794 DECL_SAVED_TREE, so fail quietly. FIXME we get here because of
2795 late code generation for VEC_INIT_EXPR, which needs to be
2796 completely reconsidered. */
2797 gcc_assert (at_eof
>= 2 && ctx
->quiet
);
2798 *non_constant_p
= true;
2800 else if (tree copy
= get_fundef_copy (new_call
.fundef
))
2802 tree body
, parms
, res
;
2803 releasing_vec ctors
;
2805 /* Reuse or create a new unshared copy of this function's body. */
2806 body
= TREE_PURPOSE (copy
);
2807 parms
= TREE_VALUE (copy
);
2808 res
= TREE_TYPE (copy
);
2810 /* Associate the bindings with the remapped parms. */
2811 tree bound
= new_call
.bindings
;
2812 tree remapped
= parms
;
2813 for (int i
= 0; i
< TREE_VEC_LENGTH (bound
); ++i
)
2815 tree arg
= TREE_VEC_ELT (bound
, i
);
2818 /* Unshare args going into the hash table to separate them
2819 from the caller's context, for better GC and to avoid
2820 problems with verify_gimple. */
2821 arg
= unshare_expr_without_location (arg
);
2822 TREE_VEC_ELT (bound
, i
) = arg
;
2824 /* And then unshare again so the callee doesn't change the
2825 argument values in the hash table. XXX Could we unshare
2826 lazily in cxx_eval_store_expression? */
2827 arg
= unshare_constructor (arg
);
2828 if (TREE_CODE (arg
) == CONSTRUCTOR
)
2829 vec_safe_push (ctors
, arg
);
2831 ctx
->global
->values
.put (remapped
, arg
);
2832 remapped
= DECL_CHAIN (remapped
);
2834 /* Add the RESULT_DECL to the values map, too. */
2835 gcc_assert (!DECL_BY_REFERENCE (res
));
2836 ctx
->global
->values
.put (res
, NULL_TREE
);
2838 /* Track the callee's evaluated SAVE_EXPRs and TARGET_EXPRs so that
2839 we can forget their values after the call. */
2840 constexpr_ctx ctx_with_save_exprs
= *ctx
;
2841 auto_vec
<tree
, 10> save_exprs
;
2842 ctx_with_save_exprs
.save_exprs
= &save_exprs
;
2843 ctx_with_save_exprs
.call
= &new_call
;
2844 unsigned save_heap_alloc_count
= ctx
->global
->heap_vars
.length ();
2845 unsigned save_heap_dealloc_count
= ctx
->global
->heap_dealloc_count
;
2847 /* If this is a constexpr destructor, the object's const and volatile
2848 semantics are no longer in effect; see [class.dtor]p5. */
2849 if (new_obj
&& DECL_DESTRUCTOR_P (fun
))
2850 cxx_set_object_constness (ctx
, new_obj
, /*readonly_p=*/false,
2851 non_constant_p
, overflow_p
);
2853 tree jump_target
= NULL_TREE
;
2854 cxx_eval_constant_expression (&ctx_with_save_exprs
, body
,
2855 lval
, non_constant_p
, overflow_p
,
2858 if (DECL_CONSTRUCTOR_P (fun
))
2860 /* This can be null for a subobject constructor call, in
2861 which case what we care about is the initialization
2862 side-effects rather than the value. We could get at the
2863 value by evaluating *this, but we don't bother; there's
2864 no need to put such a call in the hash table. */
2865 result
= lval
? ctx
->object
: ctx
->ctor
;
2867 /* If we've just evaluated a subobject constructor call for an
2868 empty union member, it might not have produced a side effect
2869 that actually activated the union member. So produce such a
2870 side effect now to ensure the union appears initialized. */
2871 if (!result
&& new_obj
2872 && TREE_CODE (new_obj
) == COMPONENT_REF
2873 && TREE_CODE (TREE_TYPE
2874 (TREE_OPERAND (new_obj
, 0))) == UNION_TYPE
2875 && is_really_empty_class (TREE_TYPE (new_obj
),
2876 /*ignore_vptr*/false))
2878 tree activate
= build2 (MODIFY_EXPR
, TREE_TYPE (new_obj
),
2880 build_constructor (TREE_TYPE (new_obj
),
2882 cxx_eval_constant_expression (ctx
, activate
, lval
,
2883 non_constant_p
, overflow_p
);
2884 ggc_free (activate
);
2887 else if (VOID_TYPE_P (TREE_TYPE (res
)))
2891 result
= *ctx
->global
->values
.get (res
);
2892 if (result
== NULL_TREE
&& !*non_constant_p
)
2895 error ("%<constexpr%> call flows off the end "
2897 *non_constant_p
= true;
2901 /* At this point, the object's constructor will have run, so
2902 the object is no longer under construction, and its possible
2903 'const' semantics now apply. Make a note of this fact by
2904 marking the CONSTRUCTOR TREE_READONLY. */
2905 if (new_obj
&& DECL_CONSTRUCTOR_P (fun
))
2906 cxx_set_object_constness (ctx
, new_obj
, /*readonly_p=*/true,
2907 non_constant_p
, overflow_p
);
2909 /* Forget the saved values of the callee's SAVE_EXPRs and
2911 for (tree save_expr
: save_exprs
)
2912 ctx
->global
->values
.remove (save_expr
);
2914 /* Remove the parms/result from the values map. Is it worth
2915 bothering to do this when the map itself is only live for
2916 one constexpr evaluation? If so, maybe also clear out
2917 other vars from call, maybe in BIND_EXPR handling? */
2918 ctx
->global
->values
.remove (res
);
2919 for (tree parm
= parms
; parm
; parm
= TREE_CHAIN (parm
))
2920 ctx
->global
->values
.remove (parm
);
2922 /* Free any parameter CONSTRUCTORs we aren't returning directly. */
2923 while (!ctors
->is_empty ())
2925 tree c
= ctors
->pop ();
2927 free_constructor (c
);
2930 /* Make the unshared function copy we used available for re-use. */
2931 save_fundef_copy (fun
, copy
);
2933 /* If the call allocated some heap object that hasn't been
2934 deallocated during the call, or if it deallocated some heap
2935 object it has not allocated, the call isn't really stateless
2936 for the constexpr evaluation and should not be cached.
2937 It is fine if the call allocates something and deallocates it
2940 && (save_heap_alloc_count
!= ctx
->global
->heap_vars
.length ()
2941 || (save_heap_dealloc_count
2942 != ctx
->global
->heap_dealloc_count
)))
2946 if ((ctx
->global
->heap_vars
.length ()
2947 - ctx
->global
->heap_dealloc_count
)
2948 != save_heap_alloc_count
- save_heap_dealloc_count
)
2951 FOR_EACH_VEC_ELT_FROM (ctx
->global
->heap_vars
, i
, heap_var
,
2952 save_heap_alloc_count
)
2953 if (DECL_NAME (heap_var
) != heap_deleted_identifier
)
2960 /* Rewrite all occurrences of the function's RESULT_DECL with the
2961 current object under construction. */
2962 if (!*non_constant_p
&& ctx
->object
2963 && CLASS_TYPE_P (TREE_TYPE (res
))
2964 && !is_empty_class (TREE_TYPE (res
)))
2965 if (replace_result_decl (&result
, res
, ctx
->object
))
2969 /* Couldn't get a function copy to evaluate. */
2970 *non_constant_p
= true;
2972 if (result
== error_mark_node
)
2973 *non_constant_p
= true;
2974 if (*non_constant_p
|| *overflow_p
)
2975 result
= error_mark_node
;
2979 entry
->result
= cacheable
? result
: error_mark_node
;
2982 /* The result of a constexpr function must be completely initialized.
2984 However, in C++20, a constexpr constructor doesn't necessarily have
2985 to initialize all the fields, so we don't clear CONSTRUCTOR_NO_CLEARING
2986 in order to detect reading an unitialized object in constexpr instead
2987 of value-initializing it. (reduced_constant_expression_p is expected to
2988 take care of clearing the flag.) */
2989 if (TREE_CODE (result
) == CONSTRUCTOR
2990 && (cxx_dialect
< cxx20
2991 || !DECL_CONSTRUCTOR_P (fun
)))
2992 clear_no_implicit_zero (result
);
2994 pop_cx_call_context ();
2998 /* Return true if T is a valid constant initializer. If a CONSTRUCTOR
2999 initializes all the members, the CONSTRUCTOR_NO_CLEARING flag will be
3001 FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
3004 reduced_constant_expression_p (tree t
)
3009 switch (TREE_CODE (t
))
3012 /* Even if we can't lower this yet, it's constant. */
3016 /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
3018 if (CONSTRUCTOR_NO_CLEARING (t
))
3020 if (TREE_CODE (TREE_TYPE (t
)) == VECTOR_TYPE
)
3021 /* An initialized vector would have a VECTOR_CST. */
3023 else if (cxx_dialect
>= cxx20
3024 && TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
3026 /* There must be a valid constant initializer at every array
3028 tree min
= TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (t
)));
3029 tree max
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (t
)));
3031 for (auto &e
: CONSTRUCTOR_ELTS (t
))
3033 if (!reduced_constant_expression_p (e
.value
))
3035 if (array_index_cmp (cursor
, e
.index
) != 0)
3037 if (TREE_CODE (e
.index
) == RANGE_EXPR
)
3038 cursor
= TREE_OPERAND (e
.index
, 1);
3039 cursor
= int_const_binop (PLUS_EXPR
, cursor
, size_one_node
);
3041 if (find_array_ctor_elt (t
, max
) == -1)
3045 else if (cxx_dialect
>= cxx20
3046 && TREE_CODE (TREE_TYPE (t
)) == UNION_TYPE
)
3048 if (CONSTRUCTOR_NELTS (t
) == 0)
3049 /* An initialized union has a constructor element. */
3051 /* And it only initializes one member. */
3055 field
= next_initializable_field (TYPE_FIELDS (TREE_TYPE (t
)));
3059 for (auto &e
: CONSTRUCTOR_ELTS (t
))
3061 /* If VAL is null, we're in the middle of initializing this
3063 if (!reduced_constant_expression_p (e
.value
))
3065 /* Empty class field may or may not have an initializer. */
3066 for (; field
&& e
.index
!= field
;
3067 field
= next_initializable_field (DECL_CHAIN (field
)))
3068 if (!is_really_empty_class (TREE_TYPE (field
),
3069 /*ignore_vptr*/false))
3072 field
= next_initializable_field (DECL_CHAIN (field
));
3074 /* There could be a non-empty field at the end. */
3075 for (; field
; field
= next_initializable_field (DECL_CHAIN (field
)))
3076 if (!is_really_empty_class (TREE_TYPE (field
), /*ignore_vptr*/false))
3079 if (CONSTRUCTOR_NO_CLEARING (t
))
3080 /* All the fields are initialized. */
3081 CONSTRUCTOR_NO_CLEARING (t
) = false;
3085 /* FIXME are we calling this too much? */
3086 return initializer_constant_valid_p (t
, TREE_TYPE (t
)) != NULL_TREE
;
3090 /* Some expressions may have constant operands but are not constant
3091 themselves, such as 1/0. Call this function to check for that
3094 We only call this in places that require an arithmetic constant, not in
3095 places where we might have a non-constant expression that can be a
3096 component of a constant expression, such as the address of a constexpr
3097 variable that might be dereferenced later. */
3100 verify_constant (tree t
, bool allow_non_constant
, bool *non_constant_p
,
3103 if (!*non_constant_p
&& !reduced_constant_expression_p (t
)
3106 if (!allow_non_constant
)
3107 error ("%q+E is not a constant expression", t
);
3108 *non_constant_p
= true;
3110 if (TREE_OVERFLOW_P (t
))
3112 if (!allow_non_constant
)
3114 permerror (input_location
, "overflow in constant expression");
3115 /* If we're being permissive (and are in an enforcing
3116 context), ignore the overflow. */
3117 if (flag_permissive
)
3118 return *non_constant_p
;
3122 return *non_constant_p
;
3125 /* Check whether the shift operation with code CODE and type TYPE on LHS
3126 and RHS is undefined. If it is, give an error with an explanation,
3127 and return true; return false otherwise. */
3130 cxx_eval_check_shift_p (location_t loc
, const constexpr_ctx
*ctx
,
3131 enum tree_code code
, tree type
, tree lhs
, tree rhs
)
3133 if ((code
!= LSHIFT_EXPR
&& code
!= RSHIFT_EXPR
)
3134 || TREE_CODE (lhs
) != INTEGER_CST
3135 || TREE_CODE (rhs
) != INTEGER_CST
)
3138 tree lhstype
= TREE_TYPE (lhs
);
3139 unsigned HOST_WIDE_INT uprec
= TYPE_PRECISION (TREE_TYPE (lhs
));
3141 /* [expr.shift] The behavior is undefined if the right operand
3142 is negative, or greater than or equal to the length in bits
3143 of the promoted left operand. */
3144 if (tree_int_cst_sgn (rhs
) == -1)
3147 permerror (loc
, "right operand of shift expression %q+E is negative",
3148 build2_loc (loc
, code
, type
, lhs
, rhs
));
3149 return (!flag_permissive
|| ctx
->quiet
);
3151 if (compare_tree_int (rhs
, uprec
) >= 0)
3154 permerror (loc
, "right operand of shift expression %q+E is greater "
3155 "than or equal to the precision %wu of the left operand",
3156 build2_loc (loc
, code
, type
, lhs
, rhs
), uprec
);
3157 return (!flag_permissive
|| ctx
->quiet
);
3160 /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
3161 if E1 has a signed type and non-negative value, and E1x2^E2 is
3162 representable in the corresponding unsigned type of the result type,
3163 then that value, converted to the result type, is the resulting value;
3164 otherwise, the behavior is undefined.
3166 The value of E1 << E2 is the unique value congruent to E1 x 2^E2 modulo
3167 2^N, where N is the range exponent of the type of the result. */
3168 if (code
== LSHIFT_EXPR
3169 && !TYPE_UNSIGNED (lhstype
)
3170 && cxx_dialect
>= cxx11
3171 && cxx_dialect
< cxx20
)
3173 if (tree_int_cst_sgn (lhs
) == -1)
3177 "left operand of shift expression %q+E is negative",
3178 build2_loc (loc
, code
, type
, lhs
, rhs
));
3179 return (!flag_permissive
|| ctx
->quiet
);
3181 /* For signed x << y the following:
3182 (unsigned) x >> ((prec (lhs) - 1) - y)
3183 if > 1, is undefined. The right-hand side of this formula
3184 is the highest bit of the LHS that can be set (starting from 0),
3185 so that the shift doesn't overflow. We then right-shift the LHS
3186 to see whether any other bit is set making the original shift
3187 undefined -- the result is not representable in the corresponding
3189 tree t
= build_int_cst (unsigned_type_node
, uprec
- 1);
3190 t
= fold_build2 (MINUS_EXPR
, unsigned_type_node
, t
, rhs
);
3191 tree ulhs
= fold_convert (unsigned_type_for (lhstype
), lhs
);
3192 t
= fold_build2 (RSHIFT_EXPR
, TREE_TYPE (ulhs
), ulhs
, t
);
3193 if (tree_int_cst_lt (integer_one_node
, t
))
3196 permerror (loc
, "shift expression %q+E overflows",
3197 build2_loc (loc
, code
, type
, lhs
, rhs
));
3198 return (!flag_permissive
|| ctx
->quiet
);
3204 /* Subroutine of cxx_eval_constant_expression.
3205 Attempt to reduce the unary expression tree T to a compile time value.
3206 If successful, return the value. Otherwise issue a diagnostic
3207 and return error_mark_node. */
3210 cxx_eval_unary_expression (const constexpr_ctx
*ctx
, tree t
,
3212 bool *non_constant_p
, bool *overflow_p
)
3215 tree orig_arg
= TREE_OPERAND (t
, 0);
3216 tree arg
= cxx_eval_constant_expression (ctx
, orig_arg
, /*lval*/false,
3217 non_constant_p
, overflow_p
);
3218 VERIFY_CONSTANT (arg
);
3219 location_t loc
= EXPR_LOCATION (t
);
3220 enum tree_code code
= TREE_CODE (t
);
3221 tree type
= TREE_TYPE (t
);
3222 r
= fold_unary_loc (loc
, code
, type
, arg
);
3225 if (arg
== orig_arg
)
3228 r
= build1_loc (loc
, code
, type
, arg
);
3230 VERIFY_CONSTANT (r
);
3234 /* Helper function for cxx_eval_binary_expression. Try to optimize
3235 original POINTER_PLUS_EXPR T, LHS p+ RHS, return NULL_TREE if the
3236 generic folding should be used. */
3239 cxx_fold_pointer_plus_expression (const constexpr_ctx
*ctx
, tree t
,
3240 tree lhs
, tree rhs
, bool *non_constant_p
,
3244 if (TREE_CODE (lhs
) != ADDR_EXPR
)
3247 lhs
= TREE_OPERAND (lhs
, 0);
3249 /* &A[i] p+ j => &A[i + j] */
3250 if (TREE_CODE (lhs
) == ARRAY_REF
3251 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == INTEGER_CST
3252 && TREE_CODE (rhs
) == INTEGER_CST
3253 && TYPE_SIZE_UNIT (TREE_TYPE (lhs
))
3254 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs
))) == INTEGER_CST
)
3256 tree orig_type
= TREE_TYPE (t
);
3257 location_t loc
= EXPR_LOCATION (t
);
3258 tree type
= TREE_TYPE (lhs
);
3260 t
= fold_convert_loc (loc
, ssizetype
, TREE_OPERAND (lhs
, 1));
3261 tree nelts
= array_type_nelts_top (TREE_TYPE (TREE_OPERAND (lhs
, 0)));
3262 nelts
= cxx_eval_constant_expression (ctx
, nelts
, false, non_constant_p
,
3264 if (*non_constant_p
)
3266 /* Don't fold an out-of-bound access. */
3267 if (!tree_int_cst_le (t
, nelts
))
3269 rhs
= cp_fold_convert (ssizetype
, rhs
);
3270 /* Don't fold if rhs can't be divided exactly by TYPE_SIZE_UNIT.
3271 constexpr int A[1]; ... (char *)&A[0] + 1 */
3272 if (!integer_zerop (fold_build2_loc (loc
, TRUNC_MOD_EXPR
, sizetype
,
3273 rhs
, TYPE_SIZE_UNIT (type
))))
3275 /* Make sure to treat the second operand of POINTER_PLUS_EXPR
3277 rhs
= fold_build2_loc (loc
, EXACT_DIV_EXPR
, ssizetype
, rhs
,
3278 TYPE_SIZE_UNIT (type
));
3279 t
= size_binop_loc (loc
, PLUS_EXPR
, rhs
, t
);
3280 t
= build4_loc (loc
, ARRAY_REF
, type
, TREE_OPERAND (lhs
, 0),
3281 t
, NULL_TREE
, NULL_TREE
);
3282 t
= cp_build_addr_expr (t
, tf_warning_or_error
);
3283 t
= cp_fold_convert (orig_type
, t
);
3284 return cxx_eval_constant_expression (ctx
, t
, /*lval*/false,
3285 non_constant_p
, overflow_p
);
3291 /* Subroutine of cxx_eval_constant_expression.
3292 Like cxx_eval_unary_expression, except for binary expressions. */
3295 cxx_eval_binary_expression (const constexpr_ctx
*ctx
, tree t
,
3297 bool *non_constant_p
, bool *overflow_p
)
3300 tree orig_lhs
= TREE_OPERAND (t
, 0);
3301 tree orig_rhs
= TREE_OPERAND (t
, 1);
3303 lhs
= cxx_eval_constant_expression (ctx
, orig_lhs
, /*lval*/false,
3304 non_constant_p
, overflow_p
);
3305 /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer
3307 if (*non_constant_p
)
3309 rhs
= cxx_eval_constant_expression (ctx
, orig_rhs
, /*lval*/false,
3310 non_constant_p
, overflow_p
);
3311 if (*non_constant_p
)
3314 location_t loc
= EXPR_LOCATION (t
);
3315 enum tree_code code
= TREE_CODE (t
);
3316 tree type
= TREE_TYPE (t
);
3318 if (code
== EQ_EXPR
|| code
== NE_EXPR
)
3320 bool is_code_eq
= (code
== EQ_EXPR
);
3322 if (TREE_CODE (lhs
) == PTRMEM_CST
3323 && TREE_CODE (rhs
) == PTRMEM_CST
)
3325 tree lmem
= PTRMEM_CST_MEMBER (lhs
);
3326 tree rmem
= PTRMEM_CST_MEMBER (rhs
);
3328 if (TREE_CODE (lmem
) == TREE_CODE (rmem
)
3329 && TREE_CODE (lmem
) == FIELD_DECL
3330 && TREE_CODE (DECL_CONTEXT (lmem
)) == UNION_TYPE
3331 && same_type_p (DECL_CONTEXT (lmem
),
3332 DECL_CONTEXT (rmem
)))
3333 /* If both refer to (possibly different) members of the same union
3334 (12.3), they compare equal. */
3337 eq
= cp_tree_equal (lhs
, rhs
);
3338 r
= constant_boolean_node (eq
== is_code_eq
, type
);
3340 else if ((TREE_CODE (lhs
) == PTRMEM_CST
3341 || TREE_CODE (rhs
) == PTRMEM_CST
)
3342 && (null_member_pointer_value_p (lhs
)
3343 || null_member_pointer_value_p (rhs
)))
3344 r
= constant_boolean_node (!is_code_eq
, type
);
3345 else if (TREE_CODE (lhs
) == PTRMEM_CST
)
3346 lhs
= cplus_expand_constant (lhs
);
3347 else if (TREE_CODE (rhs
) == PTRMEM_CST
)
3348 rhs
= cplus_expand_constant (rhs
);
3350 if (code
== POINTER_PLUS_EXPR
&& !*non_constant_p
3351 && integer_zerop (lhs
) && !integer_zerop (rhs
))
3354 error ("arithmetic involving a null pointer in %qE", lhs
);
3355 *non_constant_p
= true;
3358 else if (code
== POINTER_PLUS_EXPR
)
3359 r
= cxx_fold_pointer_plus_expression (ctx
, t
, lhs
, rhs
, non_constant_p
,
3361 else if (code
== SPACESHIP_EXPR
)
3363 r
= genericize_spaceship (loc
, type
, lhs
, rhs
);
3364 return cxx_eval_constant_expression (ctx
, r
, lval
, non_constant_p
,
3370 if (ctx
->manifestly_const_eval
3371 && (flag_constexpr_fp_except
3372 || TREE_CODE (type
) != REAL_TYPE
))
3373 r
= fold_binary_initializer_loc (loc
, code
, type
, lhs
, rhs
);
3375 r
= fold_binary_loc (loc
, code
, type
, lhs
, rhs
);
3379 && (code
== LSHIFT_EXPR
|| code
== RSHIFT_EXPR
)
3380 && TREE_CODE (lhs
) == INTEGER_CST
3381 && TREE_CODE (rhs
) == INTEGER_CST
3382 && wi::neg_p (wi::to_wide (rhs
)))
3384 /* For diagnostics and -fpermissive emulate previous behavior of
3385 handling shifts by negative amount. */
3386 tree nrhs
= const_unop (NEGATE_EXPR
, TREE_TYPE (rhs
), rhs
);
3388 r
= fold_binary_loc (loc
,
3389 code
== LSHIFT_EXPR
? RSHIFT_EXPR
: LSHIFT_EXPR
,
3395 if (lhs
== orig_lhs
&& rhs
== orig_rhs
)
3398 r
= build2_loc (loc
, code
, type
, lhs
, rhs
);
3400 else if (cxx_eval_check_shift_p (loc
, ctx
, code
, type
, lhs
, rhs
))
3401 *non_constant_p
= true;
3402 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
3403 a local array in a constexpr function. */
3404 bool ptr
= INDIRECT_TYPE_P (TREE_TYPE (lhs
));
3406 VERIFY_CONSTANT (r
);
3410 /* Subroutine of cxx_eval_constant_expression.
3411 Attempt to evaluate condition expressions. Dead branches are not
3415 cxx_eval_conditional_expression (const constexpr_ctx
*ctx
, tree t
,
3417 bool *non_constant_p
, bool *overflow_p
,
3420 tree val
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
3422 non_constant_p
, overflow_p
);
3423 VERIFY_CONSTANT (val
);
3424 if (TREE_CODE (t
) == IF_STMT
&& IF_STMT_CONSTEVAL_P (t
))
3426 /* Evaluate the condition as if it was
3427 if (__builtin_is_constant_evaluated ()), i.e. defer it if not
3428 ctx->manifestly_const_eval (as sometimes we try to constant evaluate
3429 without manifestly_const_eval even expressions or parts thereof which
3430 will later be manifestly const_eval evaluated), otherwise fold it to
3432 if (ctx
->manifestly_const_eval
)
3433 val
= boolean_true_node
;
3436 *non_constant_p
= true;
3440 /* Don't VERIFY_CONSTANT the other operands. */
3441 if (integer_zerop (val
))
3442 val
= TREE_OPERAND (t
, 2);
3444 val
= TREE_OPERAND (t
, 1);
3445 if (TREE_CODE (t
) == IF_STMT
&& !val
)
3447 return cxx_eval_constant_expression (ctx
, val
, lval
, non_constant_p
,
3448 overflow_p
, jump_target
);
3451 /* Subroutine of cxx_eval_constant_expression.
3452 Attempt to evaluate vector condition expressions. Unlike
3453 cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal
3454 ternary arithmetics operation, where all 3 arguments have to be
3455 evaluated as constants and then folding computes the result from
3459 cxx_eval_vector_conditional_expression (const constexpr_ctx
*ctx
, tree t
,
3460 bool *non_constant_p
, bool *overflow_p
)
3462 tree arg1
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
3464 non_constant_p
, overflow_p
);
3465 VERIFY_CONSTANT (arg1
);
3466 tree arg2
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
3468 non_constant_p
, overflow_p
);
3469 VERIFY_CONSTANT (arg2
);
3470 tree arg3
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 2),
3472 non_constant_p
, overflow_p
);
3473 VERIFY_CONSTANT (arg3
);
3474 location_t loc
= EXPR_LOCATION (t
);
3475 tree type
= TREE_TYPE (t
);
3476 tree r
= fold_ternary_loc (loc
, VEC_COND_EXPR
, type
, arg1
, arg2
, arg3
);
3479 if (arg1
== TREE_OPERAND (t
, 0)
3480 && arg2
== TREE_OPERAND (t
, 1)
3481 && arg3
== TREE_OPERAND (t
, 2))
3484 r
= build3_loc (loc
, VEC_COND_EXPR
, type
, arg1
, arg2
, arg3
);
3486 VERIFY_CONSTANT (r
);
3490 /* Returns less than, equal to, or greater than zero if KEY is found to be
3491 less than, to match, or to be greater than the constructor_elt's INDEX. */
3494 array_index_cmp (tree key
, tree index
)
3496 gcc_assert (TREE_CODE (key
) == INTEGER_CST
);
3498 switch (TREE_CODE (index
))
3501 return tree_int_cst_compare (key
, index
);
3504 tree lo
= TREE_OPERAND (index
, 0);
3505 tree hi
= TREE_OPERAND (index
, 1);
3506 if (tree_int_cst_lt (key
, lo
))
3508 else if (tree_int_cst_lt (hi
, key
))
3518 /* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1
3519 if none. If INSERT is true, insert a matching element rather than fail. */
3521 static HOST_WIDE_INT
3522 find_array_ctor_elt (tree ary
, tree dindex
, bool insert
)
3524 if (tree_int_cst_sgn (dindex
) < 0)
3527 unsigned HOST_WIDE_INT i
= tree_to_uhwi (dindex
);
3528 vec
<constructor_elt
, va_gc
> *elts
= CONSTRUCTOR_ELTS (ary
);
3529 unsigned HOST_WIDE_INT len
= vec_safe_length (elts
);
3531 unsigned HOST_WIDE_INT end
= len
;
3532 unsigned HOST_WIDE_INT begin
= 0;
3534 /* If the last element of the CONSTRUCTOR has its own index, we can assume
3535 that the same is true of the other elements and index directly. */
3538 tree cindex
= (*elts
)[end
- 1].index
;
3539 if (cindex
== NULL_TREE
)
3541 /* Verify that if the last index is missing, all indexes
3544 for (unsigned int j
= 0; j
< len
- 1; ++j
)
3545 gcc_assert ((*elts
)[j
].index
== NULL_TREE
);
3552 /* If the element is to be added right at the end,
3553 make sure it is added with cleared index too. */
3556 /* Otherwise, in order not to break the assumption
3557 that CONSTRUCTOR either has all indexes or none,
3558 we need to add indexes to all elements. */
3559 for (unsigned int j
= 0; j
< len
; ++j
)
3560 (*elts
)[j
].index
= build_int_cst (TREE_TYPE (dindex
), j
);
3563 else if (TREE_CODE (cindex
) == INTEGER_CST
3564 && compare_tree_int (cindex
, end
- 1) == 0)
3573 /* Otherwise, find a matching index by means of a binary search. */
3574 while (begin
!= end
)
3576 unsigned HOST_WIDE_INT middle
= (begin
+ end
) / 2;
3577 constructor_elt
&elt
= (*elts
)[middle
];
3578 tree idx
= elt
.index
;
3580 int cmp
= array_index_cmp (dindex
, idx
);
3587 if (insert
&& TREE_CODE (idx
) == RANGE_EXPR
)
3589 /* We need to split the range. */
3591 tree lo
= TREE_OPERAND (idx
, 0);
3592 tree hi
= TREE_OPERAND (idx
, 1);
3593 tree value
= elt
.value
;
3594 dindex
= fold_convert (sizetype
, dindex
);
3595 if (tree_int_cst_lt (lo
, dindex
))
3597 /* There are still some lower elts; shorten the range. */
3598 tree new_hi
= int_const_binop (MINUS_EXPR
, dindex
,
3600 if (tree_int_cst_equal (lo
, new_hi
))
3601 /* Only one element left, no longer a range. */
3604 TREE_OPERAND (idx
, 1) = new_hi
;
3605 /* Append the element we want to insert. */
3608 e
.value
= unshare_constructor (value
);
3609 vec_safe_insert (CONSTRUCTOR_ELTS (ary
), middle
, e
);
3612 /* No lower elts, the range elt is now ours. */
3615 if (tree_int_cst_lt (dindex
, hi
))
3617 /* There are still some higher elts; append a range. */
3618 tree new_lo
= int_const_binop (PLUS_EXPR
, dindex
,
3620 if (tree_int_cst_equal (new_lo
, hi
))
3623 e
.index
= build2 (RANGE_EXPR
, sizetype
, new_lo
, hi
);
3624 e
.value
= unshare_constructor (value
);
3625 vec_safe_insert (CONSTRUCTOR_ELTS (ary
), middle
+ 1, e
);
3634 constructor_elt e
= { dindex
, NULL_TREE
};
3635 vec_safe_insert (CONSTRUCTOR_ELTS (ary
), end
, e
);
3642 /* Return a pointer to the constructor_elt of CTOR which matches INDEX. If no
3643 matching constructor_elt exists, then add one to CTOR.
3645 As an optimization, if POS_HINT is non-negative then it is used as a guess
3646 for the (integer) index of the matching constructor_elt within CTOR. */
3648 static constructor_elt
*
3649 get_or_insert_ctor_field (tree ctor
, tree index
, int pos_hint
= -1)
3651 /* Check the hint first. */
3652 if (pos_hint
>= 0 && (unsigned)pos_hint
< CONSTRUCTOR_NELTS (ctor
)
3653 && CONSTRUCTOR_ELT (ctor
, pos_hint
)->index
== index
)
3654 return CONSTRUCTOR_ELT (ctor
, pos_hint
);
3656 tree type
= TREE_TYPE (ctor
);
3657 if (TREE_CODE (type
) == VECTOR_TYPE
&& index
== NULL_TREE
)
3659 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (ctor
), index
, NULL_TREE
);
3660 return &CONSTRUCTOR_ELTS (ctor
)->last();
3662 else if (TREE_CODE (type
) == ARRAY_TYPE
|| TREE_CODE (type
) == VECTOR_TYPE
)
3664 if (TREE_CODE (index
) == RANGE_EXPR
)
3666 /* Support for RANGE_EXPR index lookups is currently limited to
3667 accessing an existing element via POS_HINT, or appending a new
3668 element to the end of CTOR. ??? Support for other access
3669 patterns may also be needed. */
3670 vec
<constructor_elt
, va_gc
> *elts
= CONSTRUCTOR_ELTS (ctor
);
3671 if (vec_safe_length (elts
))
3673 tree lo
= TREE_OPERAND (index
, 0);
3674 gcc_assert (array_index_cmp (elts
->last().index
, lo
) < 0);
3676 CONSTRUCTOR_APPEND_ELT (elts
, index
, NULL_TREE
);
3677 return &elts
->last();
3680 HOST_WIDE_INT i
= find_array_ctor_elt (ctor
, index
, /*insert*/true);
3681 gcc_assert (i
>= 0);
3682 constructor_elt
*cep
= CONSTRUCTOR_ELT (ctor
, i
);
3683 gcc_assert (cep
->index
== NULL_TREE
3684 || TREE_CODE (cep
->index
) != RANGE_EXPR
);
3689 gcc_assert (TREE_CODE (index
) == FIELD_DECL
3690 && (same_type_ignoring_top_level_qualifiers_p
3691 (DECL_CONTEXT (index
), TREE_TYPE (ctor
))));
3693 /* We must keep the CONSTRUCTOR's ELTS in FIELD order.
3694 Usually we meet initializers in that order, but it is
3695 possible for base types to be placed not in program
3697 tree fields
= TYPE_FIELDS (DECL_CONTEXT (index
));
3698 unsigned HOST_WIDE_INT idx
= 0;
3699 constructor_elt
*cep
= NULL
;
3701 /* Check if we're changing the active member of a union. */
3702 if (TREE_CODE (type
) == UNION_TYPE
&& CONSTRUCTOR_NELTS (ctor
)
3703 && CONSTRUCTOR_ELT (ctor
, 0)->index
!= index
)
3704 vec_safe_truncate (CONSTRUCTOR_ELTS (ctor
), 0);
3705 /* If the bit offset of INDEX is larger than that of the last
3706 constructor_elt, then we can just immediately append a new
3707 constructor_elt to the end of CTOR. */
3708 else if (CONSTRUCTOR_NELTS (ctor
)
3709 && tree_int_cst_compare (bit_position (index
),
3710 bit_position (CONSTRUCTOR_ELTS (ctor
)
3711 ->last().index
)) > 0)
3713 idx
= CONSTRUCTOR_NELTS (ctor
);
3717 /* Otherwise, we need to iterate over CTOR to find or insert INDEX
3720 for (; vec_safe_iterate (CONSTRUCTOR_ELTS (ctor
), idx
, &cep
);
3721 idx
++, fields
= DECL_CHAIN (fields
))
3723 if (index
== cep
->index
)
3726 /* The field we're initializing must be on the field
3727 list. Look to see if it is present before the
3728 field the current ELT initializes. */
3729 for (; fields
!= cep
->index
; fields
= DECL_CHAIN (fields
))
3730 if (index
== fields
)
3733 /* We fell off the end of the CONSTRUCTOR, so insert a new
3734 entry at the end. */
3738 constructor_elt ce
= { index
, NULL_TREE
};
3740 vec_safe_insert (CONSTRUCTOR_ELTS (ctor
), idx
, ce
);
3741 cep
= CONSTRUCTOR_ELT (ctor
, idx
);
3749 /* Under the control of CTX, issue a detailed diagnostic for
3750 an out-of-bounds subscript INDEX into the expression ARRAY. */
3753 diag_array_subscript (location_t loc
, const constexpr_ctx
*ctx
, tree array
, tree index
)
3757 tree arraytype
= TREE_TYPE (array
);
3759 /* Convert the unsigned array subscript to a signed integer to avoid
3760 printing huge numbers for small negative values. */
3761 tree sidx
= fold_convert (ssizetype
, index
);
3762 STRIP_ANY_LOCATION_WRAPPER (array
);
3765 if (TYPE_DOMAIN (arraytype
))
3766 error_at (loc
, "array subscript value %qE is outside the bounds "
3767 "of array %qD of type %qT", sidx
, array
, arraytype
);
3769 error_at (loc
, "nonzero array subscript %qE is used with array %qD of "
3770 "type %qT with unknown bounds", sidx
, array
, arraytype
);
3771 inform (DECL_SOURCE_LOCATION (array
), "declared here");
3773 else if (TYPE_DOMAIN (arraytype
))
3774 error_at (loc
, "array subscript value %qE is outside the bounds "
3775 "of array type %qT", sidx
, arraytype
);
3777 error_at (loc
, "nonzero array subscript %qE is used with array of type %qT "
3778 "with unknown bounds", sidx
, arraytype
);
3782 /* Return the number of elements for TYPE (which is an ARRAY_TYPE or
3786 get_array_or_vector_nelts (const constexpr_ctx
*ctx
, tree type
,
3787 bool *non_constant_p
, bool *overflow_p
)
3790 if (TREE_CODE (type
) == ARRAY_TYPE
)
3792 if (TYPE_DOMAIN (type
))
3793 nelts
= array_type_nelts_top (type
);
3795 nelts
= size_zero_node
;
3797 else if (VECTOR_TYPE_P (type
))
3798 nelts
= size_int (TYPE_VECTOR_SUBPARTS (type
));
3802 /* For VLAs, the number of elements won't be an integer constant. */
3803 nelts
= cxx_eval_constant_expression (ctx
, nelts
, false,
3804 non_constant_p
, overflow_p
);
3808 /* Extract element INDEX consisting of CHARS_PER_ELT chars from
3809 STRING_CST STRING. */
3812 extract_string_elt (tree string
, unsigned chars_per_elt
, unsigned index
)
3814 tree type
= cv_unqualified (TREE_TYPE (TREE_TYPE (string
)));
3817 if (chars_per_elt
== 1)
3818 r
= build_int_cst (type
, TREE_STRING_POINTER (string
)[index
]);
3821 const unsigned char *ptr
3822 = ((const unsigned char *)TREE_STRING_POINTER (string
)
3823 + index
* chars_per_elt
);
3824 r
= native_interpret_expr (type
, ptr
, chars_per_elt
);
3829 /* Subroutine of cxx_eval_array_reference. T is an ARRAY_REF; evaluate the
3830 subscript, diagnose any problems with it, and return the result. */
3833 eval_and_check_array_index (const constexpr_ctx
*ctx
,
3834 tree t
, bool allow_one_past
,
3835 bool *non_constant_p
, bool *overflow_p
)
3837 location_t loc
= cp_expr_loc_or_input_loc (t
);
3838 tree ary
= TREE_OPERAND (t
, 0);
3839 t
= TREE_OPERAND (t
, 1);
3840 tree index
= cxx_eval_constant_expression (ctx
, t
, false,
3841 non_constant_p
, overflow_p
);
3842 VERIFY_CONSTANT (index
);
3844 if (!tree_fits_shwi_p (index
)
3845 || tree_int_cst_sgn (index
) < 0)
3847 diag_array_subscript (loc
, ctx
, ary
, index
);
3848 *non_constant_p
= true;
3852 tree nelts
= get_array_or_vector_nelts (ctx
, TREE_TYPE (ary
), non_constant_p
,
3854 VERIFY_CONSTANT (nelts
);
3856 ? !tree_int_cst_le (index
, nelts
)
3857 : !tree_int_cst_lt (index
, nelts
))
3859 diag_array_subscript (loc
, ctx
, ary
, index
);
3860 *non_constant_p
= true;
3867 /* Subroutine of cxx_eval_constant_expression.
3868 Attempt to reduce a reference to an array slot. */
3871 cxx_eval_array_reference (const constexpr_ctx
*ctx
, tree t
,
3873 bool *non_constant_p
, bool *overflow_p
)
3875 tree oldary
= TREE_OPERAND (t
, 0);
3876 tree ary
= cxx_eval_constant_expression (ctx
, oldary
,
3878 non_constant_p
, overflow_p
);
3879 if (*non_constant_p
)
3882 && TREE_CODE (ary
) == VIEW_CONVERT_EXPR
3883 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary
, 0)))
3884 && TREE_TYPE (t
) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary
, 0))))
3885 ary
= TREE_OPERAND (ary
, 0);
3887 tree oldidx
= TREE_OPERAND (t
, 1);
3888 tree index
= eval_and_check_array_index (ctx
, t
, lval
,
3889 non_constant_p
, overflow_p
);
3890 if (*non_constant_p
)
3893 if (lval
&& ary
== oldary
&& index
== oldidx
)
3896 return build4 (ARRAY_REF
, TREE_TYPE (t
), ary
, index
, NULL
, NULL
);
3898 unsigned len
= 0, elem_nchars
= 1;
3899 tree elem_type
= TREE_TYPE (TREE_TYPE (ary
));
3900 if (TREE_CODE (ary
) == CONSTRUCTOR
)
3901 len
= CONSTRUCTOR_NELTS (ary
);
3902 else if (TREE_CODE (ary
) == STRING_CST
)
3904 elem_nchars
= (TYPE_PRECISION (elem_type
)
3905 / TYPE_PRECISION (char_type_node
));
3906 len
= (unsigned) TREE_STRING_LENGTH (ary
) / elem_nchars
;
3908 else if (TREE_CODE (ary
) == VECTOR_CST
)
3909 /* We don't create variable-length VECTOR_CSTs. */
3910 len
= VECTOR_CST_NELTS (ary
).to_constant ();
3913 /* We can't do anything with other tree codes, so use
3914 VERIFY_CONSTANT to complain and fail. */
3915 VERIFY_CONSTANT (ary
);
3920 HOST_WIDE_INT i
= 0;
3921 if (TREE_CODE (ary
) == CONSTRUCTOR
)
3923 HOST_WIDE_INT ix
= find_array_ctor_elt (ary
, index
);
3930 i
= tree_to_shwi (index
);
3937 if (TREE_CODE (ary
) == CONSTRUCTOR
)
3938 r
= (*CONSTRUCTOR_ELTS (ary
))[i
].value
;
3939 else if (TREE_CODE (ary
) == VECTOR_CST
)
3940 r
= VECTOR_CST_ELT (ary
, i
);
3942 r
= extract_string_elt (ary
, elem_nchars
, i
);
3945 /* Don't VERIFY_CONSTANT here. */
3948 /* Otherwise the element doesn't have a value yet. */
3953 if (TREE_CODE (ary
) == CONSTRUCTOR
3954 && CONSTRUCTOR_NO_CLEARING (ary
))
3956 /* 'ary' is part of the aggregate initializer we're currently
3957 building; if there's no initializer for this element yet,
3960 error ("accessing uninitialized array element");
3961 *non_constant_p
= true;
3965 /* If it's within the array bounds but doesn't have an explicit
3966 initializer, it's initialized from {}. But use build_value_init
3967 directly for non-aggregates to avoid creating a garbage CONSTRUCTOR. */
3969 constexpr_ctx new_ctx
;
3970 if (is_really_empty_class (elem_type
, /*ignore_vptr*/false))
3971 return build_constructor (elem_type
, NULL
);
3972 else if (CP_AGGREGATE_TYPE_P (elem_type
))
3974 tree empty_ctor
= build_constructor (init_list_type_node
, NULL
);
3975 val
= digest_init (elem_type
, empty_ctor
, tf_warning_or_error
);
3978 val
= build_value_init (elem_type
, tf_warning_or_error
);
3980 if (!SCALAR_TYPE_P (elem_type
))
3984 /* If there was no object, don't add one: it could confuse us
3985 into thinking we're modifying a const object. */
3987 new_ctx
.ctor
= build_constructor (elem_type
, NULL
);
3990 t
= cxx_eval_constant_expression (ctx
, val
, lval
, non_constant_p
,
3992 if (!SCALAR_TYPE_P (elem_type
) && t
!= ctx
->ctor
)
3993 free_constructor (ctx
->ctor
);
3997 /* Subroutine of cxx_eval_constant_expression.
3998 Attempt to reduce a field access of a value of class type. */
4001 cxx_eval_component_reference (const constexpr_ctx
*ctx
, tree t
,
4003 bool *non_constant_p
, bool *overflow_p
)
4005 unsigned HOST_WIDE_INT i
;
4008 tree part
= TREE_OPERAND (t
, 1);
4009 tree orig_whole
= TREE_OPERAND (t
, 0);
4010 tree whole
= cxx_eval_constant_expression (ctx
, orig_whole
,
4012 non_constant_p
, overflow_p
);
4013 if (INDIRECT_REF_P (whole
)
4014 && integer_zerop (TREE_OPERAND (whole
, 0)))
4017 error ("dereferencing a null pointer in %qE", orig_whole
);
4018 *non_constant_p
= true;
4022 if (TREE_CODE (whole
) == PTRMEM_CST
)
4023 whole
= cplus_expand_constant (whole
);
4024 if (whole
== orig_whole
)
4027 return fold_build3 (COMPONENT_REF
, TREE_TYPE (t
),
4028 whole
, part
, NULL_TREE
);
4029 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
4031 if (!*non_constant_p
&& TREE_CODE (whole
) != CONSTRUCTOR
)
4034 error ("%qE is not a constant expression", orig_whole
);
4035 *non_constant_p
= true;
4037 if (DECL_MUTABLE_P (part
))
4040 error ("mutable %qD is not usable in a constant expression", part
);
4041 *non_constant_p
= true;
4043 if (*non_constant_p
)
4045 bool pmf
= TYPE_PTRMEMFUNC_P (TREE_TYPE (whole
));
4046 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole
), i
, field
, value
)
4048 /* Use name match for PMF fields, as a variant will have a
4049 different FIELD_DECL with a different type. */
4050 if (pmf
? DECL_NAME (field
) == DECL_NAME (part
)
4055 STRIP_ANY_LOCATION_WRAPPER (value
);
4059 /* We're in the middle of initializing it. */
4063 if (TREE_CODE (TREE_TYPE (whole
)) == UNION_TYPE
4064 && CONSTRUCTOR_NELTS (whole
) > 0)
4066 /* DR 1188 says we don't have to deal with this. */
4069 constructor_elt
*cep
= CONSTRUCTOR_ELT (whole
, 0);
4070 if (cep
->value
== NULL_TREE
)
4071 error ("accessing uninitialized member %qD", part
);
4073 error ("accessing %qD member instead of initialized %qD member in "
4074 "constant expression", part
, cep
->index
);
4076 *non_constant_p
= true;
4080 /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty
4081 classes never get represented; throw together a value now. */
4082 if (is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
4083 return build_constructor (TREE_TYPE (t
), NULL
);
4085 gcc_assert (DECL_CONTEXT (part
) == TYPE_MAIN_VARIANT (TREE_TYPE (whole
)));
4087 if (CONSTRUCTOR_NO_CLEARING (whole
))
4089 /* 'whole' is part of the aggregate initializer we're currently
4090 building; if there's no initializer for this member yet, that's an
4093 error ("accessing uninitialized member %qD", part
);
4094 *non_constant_p
= true;
4098 /* If there's no explicit init for this field, it's value-initialized. */
4099 value
= build_value_init (TREE_TYPE (t
), tf_warning_or_error
);
4100 return cxx_eval_constant_expression (ctx
, value
,
4102 non_constant_p
, overflow_p
);
4105 /* Subroutine of cxx_eval_constant_expression.
4106 Attempt to reduce a field access of a value of class type that is
4107 expressed as a BIT_FIELD_REF. */
4110 cxx_eval_bit_field_ref (const constexpr_ctx
*ctx
, tree t
,
4112 bool *non_constant_p
, bool *overflow_p
)
4114 tree orig_whole
= TREE_OPERAND (t
, 0);
4115 tree retval
, fldval
, utype
, mask
;
4116 bool fld_seen
= false;
4117 HOST_WIDE_INT istart
, isize
;
4118 tree whole
= cxx_eval_constant_expression (ctx
, orig_whole
,
4120 non_constant_p
, overflow_p
);
4121 tree start
, field
, value
;
4122 unsigned HOST_WIDE_INT i
;
4124 if (whole
== orig_whole
)
4126 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
4128 if (!*non_constant_p
4129 && TREE_CODE (whole
) != VECTOR_CST
4130 && TREE_CODE (whole
) != CONSTRUCTOR
)
4133 error ("%qE is not a constant expression", orig_whole
);
4134 *non_constant_p
= true;
4136 if (*non_constant_p
)
4139 if (TREE_CODE (whole
) == VECTOR_CST
)
4140 return fold_ternary (BIT_FIELD_REF
, TREE_TYPE (t
), whole
,
4141 TREE_OPERAND (t
, 1), TREE_OPERAND (t
, 2));
4143 start
= TREE_OPERAND (t
, 2);
4144 istart
= tree_to_shwi (start
);
4145 isize
= tree_to_shwi (TREE_OPERAND (t
, 1));
4146 utype
= TREE_TYPE (t
);
4147 if (!TYPE_UNSIGNED (utype
))
4148 utype
= build_nonstandard_integer_type (TYPE_PRECISION (utype
), 1);
4149 retval
= build_int_cst (utype
, 0);
4150 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole
), i
, field
, value
)
4152 tree bitpos
= bit_position (field
);
4153 STRIP_ANY_LOCATION_WRAPPER (value
);
4154 if (bitpos
== start
&& DECL_SIZE (field
) == TREE_OPERAND (t
, 1))
4156 if (TREE_CODE (TREE_TYPE (field
)) == INTEGER_TYPE
4157 && TREE_CODE (value
) == INTEGER_CST
4158 && tree_fits_shwi_p (bitpos
)
4159 && tree_fits_shwi_p (DECL_SIZE (field
)))
4161 HOST_WIDE_INT bit
= tree_to_shwi (bitpos
);
4162 HOST_WIDE_INT sz
= tree_to_shwi (DECL_SIZE (field
));
4163 HOST_WIDE_INT shift
;
4164 if (bit
>= istart
&& bit
+ sz
<= istart
+ isize
)
4166 fldval
= fold_convert (utype
, value
);
4167 mask
= build_int_cst_type (utype
, -1);
4168 mask
= fold_build2 (LSHIFT_EXPR
, utype
, mask
,
4169 size_int (TYPE_PRECISION (utype
) - sz
));
4170 mask
= fold_build2 (RSHIFT_EXPR
, utype
, mask
,
4171 size_int (TYPE_PRECISION (utype
) - sz
));
4172 fldval
= fold_build2 (BIT_AND_EXPR
, utype
, fldval
, mask
);
4173 shift
= bit
- istart
;
4174 if (BYTES_BIG_ENDIAN
)
4175 shift
= TYPE_PRECISION (utype
) - shift
- sz
;
4176 fldval
= fold_build2 (LSHIFT_EXPR
, utype
, fldval
,
4178 retval
= fold_build2 (BIT_IOR_EXPR
, utype
, retval
, fldval
);
4184 return fold_convert (TREE_TYPE (t
), retval
);
4186 return error_mark_node
;
4189 /* Helper for cxx_eval_bit_cast.
4190 Check [bit.cast]/3 rules, bit_cast is constexpr only if the To and From
4191 types and types of all subobjects have is_union_v<T>, is_pointer_v<T>,
4192 is_member_pointer_v<T>, is_volatile_v<T> false and has no non-static
4193 data members of reference type. */
4196 check_bit_cast_type (const constexpr_ctx
*ctx
, location_t loc
, tree type
,
4199 if (TREE_CODE (type
) == UNION_TYPE
)
4203 if (type
== orig_type
)
4204 error_at (loc
, "%qs is not a constant expression because %qT is "
4205 "a union type", "__builtin_bit_cast", type
);
4207 error_at (loc
, "%qs is not a constant expression because %qT "
4208 "contains a union type", "__builtin_bit_cast",
4213 if (TREE_CODE (type
) == POINTER_TYPE
)
4217 if (type
== orig_type
)
4218 error_at (loc
, "%qs is not a constant expression because %qT is "
4219 "a pointer type", "__builtin_bit_cast", type
);
4221 error_at (loc
, "%qs is not a constant expression because %qT "
4222 "contains a pointer type", "__builtin_bit_cast",
4227 if (TREE_CODE (type
) == REFERENCE_TYPE
)
4231 if (type
== orig_type
)
4232 error_at (loc
, "%qs is not a constant expression because %qT is "
4233 "a reference type", "__builtin_bit_cast", type
);
4235 error_at (loc
, "%qs is not a constant expression because %qT "
4236 "contains a reference type", "__builtin_bit_cast",
4241 if (TYPE_PTRMEM_P (type
))
4245 if (type
== orig_type
)
4246 error_at (loc
, "%qs is not a constant expression because %qT is "
4247 "a pointer to member type", "__builtin_bit_cast",
4250 error_at (loc
, "%qs is not a constant expression because %qT "
4251 "contains a pointer to member type",
4252 "__builtin_bit_cast", orig_type
);
4256 if (TYPE_VOLATILE (type
))
4260 if (type
== orig_type
)
4261 error_at (loc
, "%qs is not a constant expression because %qT is "
4262 "volatile", "__builtin_bit_cast", type
);
4264 error_at (loc
, "%qs is not a constant expression because %qT "
4265 "contains a volatile subobject",
4266 "__builtin_bit_cast", orig_type
);
4270 if (TREE_CODE (type
) == RECORD_TYPE
)
4271 for (tree field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
4272 if (TREE_CODE (field
) == FIELD_DECL
4273 && check_bit_cast_type (ctx
, loc
, TREE_TYPE (field
), orig_type
))
4278 /* Helper function for cxx_eval_bit_cast. For unsigned char or
4279 std::byte members of CONSTRUCTOR (recursively) if they contain
4280 some indeterminate bits (as set in MASK), remove the ctor elts,
4281 mark the CONSTRUCTOR as CONSTRUCTOR_NO_CLEARING and clear the
4285 clear_uchar_or_std_byte_in_mask (location_t loc
, tree t
, unsigned char *mask
)
4287 if (TREE_CODE (t
) != CONSTRUCTOR
)
4292 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t
), i
, index
, value
)
4294 tree type
= TREE_TYPE (value
);
4295 if (TREE_CODE (TREE_TYPE (t
)) != ARRAY_TYPE
4296 && DECL_BIT_FIELD_TYPE (index
) != NULL_TREE
)
4298 if (is_byte_access_type_not_plain_char (DECL_BIT_FIELD_TYPE (index
)))
4300 HOST_WIDE_INT fldsz
= TYPE_PRECISION (TREE_TYPE (index
));
4301 gcc_assert (fldsz
!= 0);
4302 HOST_WIDE_INT pos
= int_byte_position (index
);
4304 = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (index
));
4305 bpos
%= BITS_PER_UNIT
;
4307 = ROUND_UP (bpos
+ fldsz
, BITS_PER_UNIT
) / BITS_PER_UNIT
;
4308 gcc_assert (end
== 1 || end
== 2);
4309 unsigned char *p
= mask
+ pos
;
4310 unsigned char mask_save
[2];
4311 mask_save
[0] = mask
[pos
];
4312 mask_save
[1] = end
== 2 ? mask
[pos
+ 1] : 0;
4313 if (BYTES_BIG_ENDIAN
!= WORDS_BIG_ENDIAN
)
4314 sorry_at (loc
, "PDP11 bit-field handling unsupported"
4315 " in %qs", "__builtin_bit_cast");
4316 else if (BYTES_BIG_ENDIAN
)
4319 if (bpos
+ fldsz
<= BITS_PER_UNIT
)
4320 *p
&= ~(((1 << fldsz
) - 1)
4321 << (BITS_PER_UNIT
- bpos
- fldsz
));
4325 *p
&= ~(((1U << BITS_PER_UNIT
) - 1) >> bpos
);
4327 fldsz
-= BITS_PER_UNIT
- bpos
;
4328 gcc_assert (fldsz
&& fldsz
< BITS_PER_UNIT
);
4329 *p
&= ((1U << BITS_PER_UNIT
) - 1) >> fldsz
;
4334 /* Little endian. */
4335 if (bpos
+ fldsz
<= BITS_PER_UNIT
)
4336 *p
&= ~(((1 << fldsz
) - 1) << bpos
);
4340 *p
&= ~(((1 << BITS_PER_UNIT
) - 1) << bpos
);
4342 fldsz
-= BITS_PER_UNIT
- bpos
;
4343 gcc_assert (fldsz
&& fldsz
< BITS_PER_UNIT
);
4344 *p
&= ~((1 << fldsz
) - 1);
4347 if (mask_save
[0] != mask
[pos
]
4348 || (end
== 2 && mask_save
[1] != mask
[pos
+ 1]))
4350 CONSTRUCTOR_NO_CLEARING (t
) = 1;
4355 else if (is_byte_access_type_not_plain_char (type
))
4358 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
4359 pos
= tree_to_shwi (index
);
4361 pos
= int_byte_position (index
);
4364 CONSTRUCTOR_NO_CLEARING (t
) = 1;
4369 if (TREE_CODE (value
) == CONSTRUCTOR
)
4372 if (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
4373 pos
= tree_to_shwi (index
)
4374 * tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t
))));
4376 pos
= int_byte_position (index
);
4377 clear_uchar_or_std_byte_in_mask (loc
, value
, mask
+ pos
);
4381 CONSTRUCTOR_ELT (t
, j
)->index
= index
;
4382 CONSTRUCTOR_ELT (t
, j
)->value
= value
;
4386 if (CONSTRUCTOR_NELTS (t
) != j
)
4387 vec_safe_truncate (CONSTRUCTOR_ELTS (t
), j
);
4390 /* Subroutine of cxx_eval_constant_expression.
4391 Attempt to evaluate a BIT_CAST_EXPR. */
4394 cxx_eval_bit_cast (const constexpr_ctx
*ctx
, tree t
, bool *non_constant_p
,
4397 if (check_bit_cast_type (ctx
, EXPR_LOCATION (t
), TREE_TYPE (t
),
4399 || check_bit_cast_type (ctx
, cp_expr_loc_or_loc (TREE_OPERAND (t
, 0),
4401 TREE_TYPE (TREE_OPERAND (t
, 0)),
4402 TREE_TYPE (TREE_OPERAND (t
, 0))))
4404 *non_constant_p
= true;
4408 tree op
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), false,
4409 non_constant_p
, overflow_p
);
4410 if (*non_constant_p
)
4413 location_t loc
= EXPR_LOCATION (t
);
4414 if (BITS_PER_UNIT
!= 8 || CHAR_BIT
!= 8)
4417 sorry_at (loc
, "%qs cannot be constant evaluated on the target",
4418 "__builtin_bit_cast");
4419 *non_constant_p
= true;
4423 if (!tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (t
))))
4426 sorry_at (loc
, "%qs cannot be constant evaluated because the "
4427 "type is too large", "__builtin_bit_cast");
4428 *non_constant_p
= true;
4432 HOST_WIDE_INT len
= tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (t
)));
4433 if (len
< 0 || (int) len
!= len
)
4436 sorry_at (loc
, "%qs cannot be constant evaluated because the "
4437 "type is too large", "__builtin_bit_cast");
4438 *non_constant_p
= true;
4442 unsigned char buf
[64];
4443 unsigned char *ptr
, *mask
;
4444 size_t alen
= (size_t) len
* 2;
4445 if (alen
<= sizeof (buf
))
4448 ptr
= XNEWVEC (unsigned char, alen
);
4449 mask
= ptr
+ (size_t) len
;
4450 /* At the beginning consider everything indeterminate. */
4451 memset (mask
, ~0, (size_t) len
);
4453 if (native_encode_initializer (op
, ptr
, len
, 0, mask
) != len
)
4456 sorry_at (loc
, "%qs cannot be constant evaluated because the "
4457 "argument cannot be encoded", "__builtin_bit_cast");
4458 *non_constant_p
= true;
4465 if (can_native_interpret_type_p (TREE_TYPE (t
)))
4467 r
= native_interpret_expr (TREE_TYPE (t
), ptr
, len
);
4468 if (is_byte_access_type_not_plain_char (TREE_TYPE (t
)))
4470 gcc_assert (len
== 1);
4473 memset (mask
, 0, len
);
4474 r
= build_constructor (TREE_TYPE (r
), NULL
);
4475 CONSTRUCTOR_NO_CLEARING (r
) = 1;
4479 else if (TREE_CODE (TREE_TYPE (t
)) == RECORD_TYPE
)
4481 r
= native_interpret_aggregate (TREE_TYPE (t
), ptr
, 0, len
);
4484 clear_type_padding_in_mask (TREE_TYPE (t
), mask
);
4485 clear_uchar_or_std_byte_in_mask (loc
, r
, mask
);
4491 for (int i
= 0; i
< len
; i
++)
4495 error_at (loc
, "%qs accessing uninitialized byte at offset %d",
4496 "__builtin_bit_cast", i
);
4497 *non_constant_p
= true;
4507 sorry_at (loc
, "%qs cannot be constant evaluated because the "
4508 "argument cannot be interpreted", "__builtin_bit_cast");
4509 *non_constant_p
= true;
4515 /* Subroutine of cxx_eval_constant_expression.
4516 Evaluate a short-circuited logical expression T in the context
4517 of a given constexpr CALL. BAILOUT_VALUE is the value for
4518 early return. CONTINUE_VALUE is used here purely for
4519 sanity check purposes. */
4522 cxx_eval_logical_expression (const constexpr_ctx
*ctx
, tree t
,
4523 tree bailout_value
, tree continue_value
,
4525 bool *non_constant_p
, bool *overflow_p
)
4528 tree lhs
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
4530 non_constant_p
, overflow_p
);
4531 VERIFY_CONSTANT (lhs
);
4532 if (tree_int_cst_equal (lhs
, bailout_value
))
4534 gcc_assert (tree_int_cst_equal (lhs
, continue_value
));
4535 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
4536 lval
, non_constant_p
,
4538 VERIFY_CONSTANT (r
);
4542 /* REF is a COMPONENT_REF designating a particular field. V is a vector of
4543 CONSTRUCTOR elements to initialize (part of) an object containing that
4544 field. Return a pointer to the constructor_elt corresponding to the
4545 initialization of the field. */
4547 static constructor_elt
*
4548 base_field_constructor_elt (vec
<constructor_elt
, va_gc
> *v
, tree ref
)
4550 tree aggr
= TREE_OPERAND (ref
, 0);
4551 tree field
= TREE_OPERAND (ref
, 1);
4553 constructor_elt
*ce
;
4555 gcc_assert (TREE_CODE (ref
) == COMPONENT_REF
);
4557 if (TREE_CODE (aggr
) == COMPONENT_REF
)
4559 constructor_elt
*base_ce
4560 = base_field_constructor_elt (v
, aggr
);
4561 v
= CONSTRUCTOR_ELTS (base_ce
->value
);
4564 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
4565 if (ce
->index
== field
)
4572 /* Some of the expressions fed to the constexpr mechanism are calls to
4573 constructors, which have type void. In that case, return the type being
4574 initialized by the constructor. */
4577 initialized_type (tree t
)
4581 tree type
= TREE_TYPE (t
);
4582 if (TREE_CODE (t
) == CALL_EXPR
)
4584 /* A constructor call has void type, so we need to look deeper. */
4585 tree fn
= get_function_named_in_call (t
);
4586 if (fn
&& TREE_CODE (fn
) == FUNCTION_DECL
4587 && DECL_CXX_CONSTRUCTOR_P (fn
))
4588 type
= DECL_CONTEXT (fn
);
4590 else if (TREE_CODE (t
) == COMPOUND_EXPR
)
4591 return initialized_type (TREE_OPERAND (t
, 1));
4592 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
)
4593 type
= TREE_TYPE (AGGR_INIT_EXPR_SLOT (t
));
4594 return cv_unqualified (type
);
4597 /* We're about to initialize element INDEX of an array or class from VALUE.
4598 Set up NEW_CTX appropriately by adjusting .object to refer to the
4599 subobject and creating a new CONSTRUCTOR if the element is itself
4600 a class or array. */
4603 init_subob_ctx (const constexpr_ctx
*ctx
, constexpr_ctx
&new_ctx
,
4604 tree index
, tree
&value
)
4608 if (index
&& TREE_CODE (index
) != INTEGER_CST
4609 && TREE_CODE (index
) != FIELD_DECL
4610 && TREE_CODE (index
) != RANGE_EXPR
)
4611 /* This won't have an element in the new CONSTRUCTOR. */
4614 tree type
= initialized_type (value
);
4615 if (!AGGREGATE_TYPE_P (type
) && !VECTOR_TYPE_P (type
))
4616 /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
4619 /* The sub-aggregate initializer might contain a placeholder;
4620 update object to refer to the subobject and ctor to refer to
4621 the (newly created) sub-initializer. */
4624 if (index
== NULL_TREE
|| TREE_CODE (index
) == RANGE_EXPR
)
4625 /* There's no well-defined subobject for this index. */
4626 new_ctx
.object
= NULL_TREE
;
4628 new_ctx
.object
= build_ctor_subob_ref (index
, type
, ctx
->object
);
4630 tree elt
= build_constructor (type
, NULL
);
4631 CONSTRUCTOR_NO_CLEARING (elt
) = true;
4634 if (TREE_CODE (value
) == TARGET_EXPR
)
4635 /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
4636 value
= TARGET_EXPR_INITIAL (value
);
4639 /* We're about to process an initializer for a class or array TYPE. Make
4640 sure that CTX is set up appropriately. */
4643 verify_ctor_sanity (const constexpr_ctx
*ctx
, tree type
)
4645 /* We don't bother building a ctor for an empty base subobject. */
4646 if (is_empty_class (type
))
4649 /* We're in the middle of an initializer that might involve placeholders;
4650 our caller should have created a CONSTRUCTOR for us to put the
4651 initializer into. We will either return that constructor or T. */
4652 gcc_assert (ctx
->ctor
);
4653 gcc_assert (same_type_ignoring_top_level_qualifiers_p
4654 (type
, TREE_TYPE (ctx
->ctor
)));
4655 /* We used to check that ctx->ctor was empty, but that isn't the case when
4656 the object is zero-initialized before calling the constructor. */
4659 tree otype
= TREE_TYPE (ctx
->object
);
4660 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type
, otype
)
4661 /* Handle flexible array members. */
4662 || (TREE_CODE (otype
) == ARRAY_TYPE
4663 && TYPE_DOMAIN (otype
) == NULL_TREE
4664 && TREE_CODE (type
) == ARRAY_TYPE
4665 && (same_type_ignoring_top_level_qualifiers_p
4666 (TREE_TYPE (type
), TREE_TYPE (otype
)))));
4668 gcc_assert (!ctx
->object
|| !DECL_P (ctx
->object
)
4669 || *(ctx
->global
->values
.get (ctx
->object
)) == ctx
->ctor
);
4672 /* Subroutine of cxx_eval_constant_expression.
4673 The expression tree T denotes a C-style array or a C-style
4674 aggregate. Reduce it to a constant expression. */
4677 cxx_eval_bare_aggregate (const constexpr_ctx
*ctx
, tree t
,
4679 bool *non_constant_p
, bool *overflow_p
)
4681 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (t
);
4682 bool changed
= false;
4683 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t
));
4684 tree type
= TREE_TYPE (t
);
4686 constexpr_ctx new_ctx
;
4687 if (TYPE_PTRMEMFUNC_P (type
) || VECTOR_TYPE_P (type
))
4689 /* We don't really need the ctx->ctor business for a PMF or
4690 vector, but it's simpler to use the same code. */
4692 new_ctx
.ctor
= build_constructor (type
, NULL
);
4693 new_ctx
.object
= NULL_TREE
;
4696 verify_ctor_sanity (ctx
, type
);
4697 vec
<constructor_elt
, va_gc
> **p
= &CONSTRUCTOR_ELTS (ctx
->ctor
);
4698 vec_alloc (*p
, vec_safe_length (v
));
4700 if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (t
))
4701 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx
->ctor
) = 1;
4705 bool constant_p
= true;
4706 bool side_effects_p
= false;
4707 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, index
, value
)
4709 tree orig_value
= value
;
4710 /* Like in cxx_eval_store_expression, omit entries for empty fields. */
4711 bool no_slot
= TREE_CODE (type
) == RECORD_TYPE
&& is_empty_field (index
);
4715 init_subob_ctx (ctx
, new_ctx
, index
, value
);
4717 if (new_ctx
.ctor
!= ctx
->ctor
)
4719 /* If we built a new CONSTRUCTOR, attach it now so that other
4720 initializers can refer to it. */
4721 constructor_elt
*cep
= get_or_insert_ctor_field (ctx
->ctor
, index
);
4722 cep
->value
= new_ctx
.ctor
;
4723 pos_hint
= cep
- (*p
)->begin();
4725 else if (TREE_CODE (type
) == UNION_TYPE
)
4726 /* Otherwise if we're constructing a non-aggregate union member, set
4727 the active union member now so that we can later detect and diagnose
4728 if its initializer attempts to activate another member. */
4729 get_or_insert_ctor_field (ctx
->ctor
, index
);
4730 tree elt
= cxx_eval_constant_expression (&new_ctx
, value
,
4732 non_constant_p
, overflow_p
);
4733 /* Don't VERIFY_CONSTANT here. */
4734 if (ctx
->quiet
&& *non_constant_p
)
4736 if (elt
!= orig_value
)
4739 if (!TREE_CONSTANT (elt
))
4741 if (TREE_SIDE_EFFECTS (elt
))
4742 side_effects_p
= true;
4743 if (index
&& TREE_CODE (index
) == COMPONENT_REF
)
4745 /* This is an initialization of a vfield inside a base
4746 subaggregate that we already initialized; push this
4747 initialization into the previous initialization. */
4748 constructor_elt
*inner
= base_field_constructor_elt (*p
, index
);
4753 && (TREE_CODE (index
) == NOP_EXPR
4754 || TREE_CODE (index
) == POINTER_PLUS_EXPR
))
4756 /* This is an initializer for an empty base; now that we've
4757 checked that it's constant, we can ignore it. */
4758 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index
))));
4765 if (TREE_CODE (type
) == UNION_TYPE
4766 && (*p
)->last().index
!= index
)
4767 /* The initializer erroneously changed the active union member that
4768 we're initializing. */
4769 gcc_assert (*non_constant_p
);
4772 /* The initializer might have mutated the underlying CONSTRUCTOR,
4773 so recompute the location of the target constructer_elt. */
4774 constructor_elt
*cep
4775 = get_or_insert_ctor_field (ctx
->ctor
, index
, pos_hint
);
4779 /* Adding or replacing an element might change the ctor's flags. */
4780 TREE_CONSTANT (ctx
->ctor
) = constant_p
;
4781 TREE_SIDE_EFFECTS (ctx
->ctor
) = side_effects_p
;
4784 if (*non_constant_p
|| !changed
)
4787 /* We're done building this CONSTRUCTOR, so now we can interpret an
4788 element without an explicit initializer as value-initialized. */
4789 CONSTRUCTOR_NO_CLEARING (t
) = false;
4790 TREE_CONSTANT (t
) = constant_p
;
4791 TREE_SIDE_EFFECTS (t
) = side_effects_p
;
4792 if (VECTOR_TYPE_P (type
))
4797 /* Subroutine of cxx_eval_constant_expression.
4798 The expression tree T is a VEC_INIT_EXPR which denotes the desired
4799 initialization of a non-static data member of array type. Reduce it to a
4802 Note that apart from value-initialization (when VALUE_INIT is true),
4803 this is only intended to support value-initialization and the
4804 initializations done by defaulted constructors for classes with
4805 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
4806 will either be NULL_TREE for the default constructor, or a COMPONENT_REF
4807 for the copy/move constructor. */
4810 cxx_eval_vec_init_1 (const constexpr_ctx
*ctx
, tree atype
, tree init
,
4811 bool value_init
, bool lval
,
4812 bool *non_constant_p
, bool *overflow_p
)
4814 tree elttype
= TREE_TYPE (atype
);
4815 verify_ctor_sanity (ctx
, atype
);
4816 vec
<constructor_elt
, va_gc
> **p
= &CONSTRUCTOR_ELTS (ctx
->ctor
);
4817 bool pre_init
= false;
4818 unsigned HOST_WIDE_INT i
;
4819 tsubst_flags_t complain
= ctx
->quiet
? tf_none
: tf_warning_or_error
;
4821 if (init
&& TREE_CODE (init
) == CONSTRUCTOR
)
4822 return cxx_eval_bare_aggregate (ctx
, init
, lval
,
4823 non_constant_p
, overflow_p
);
4825 /* For the default constructor, build up a call to the default
4826 constructor of the element type. We only need to handle class types
4827 here, as for a constructor to be constexpr, all members must be
4828 initialized, which for a defaulted default constructor means they must
4829 be of a class type with a constexpr default constructor. */
4830 if (TREE_CODE (elttype
) == ARRAY_TYPE
)
4831 /* We only do this at the lowest level. */;
4832 else if (value_init
)
4834 init
= build_value_init (elttype
, complain
);
4839 releasing_vec argvec
;
4840 init
= build_special_member_call (NULL_TREE
, complete_ctor_identifier
,
4841 &argvec
, elttype
, LOOKUP_NORMAL
,
4843 init
= build_aggr_init_expr (elttype
, init
);
4847 bool zeroed_out
= false;
4848 if (!CONSTRUCTOR_NO_CLEARING (ctx
->ctor
))
4850 /* We're initializing an array object that had been zero-initialized
4851 earlier. Truncate ctx->ctor, and propagate its zeroed state by
4852 clearing CONSTRUCTOR_NO_CLEARING on each of the aggregate element
4853 initializers we append to it. */
4854 gcc_checking_assert (initializer_zerop (ctx
->ctor
));
4856 vec_safe_truncate (*p
, 0);
4859 tree nelts
= get_array_or_vector_nelts (ctx
, atype
, non_constant_p
,
4861 unsigned HOST_WIDE_INT max
= tree_to_uhwi (nelts
);
4862 for (i
= 0; i
< max
; ++i
)
4864 tree idx
= build_int_cst (size_type_node
, i
);
4867 constexpr_ctx new_ctx
;
4868 init_subob_ctx (ctx
, new_ctx
, idx
, pre_init
? init
: elttype
);
4869 if (new_ctx
.ctor
!= ctx
->ctor
)
4872 CONSTRUCTOR_NO_CLEARING (new_ctx
.ctor
) = false;
4873 CONSTRUCTOR_APPEND_ELT (*p
, idx
, new_ctx
.ctor
);
4875 if (TREE_CODE (elttype
) == ARRAY_TYPE
)
4877 /* A multidimensional array; recurse. */
4878 if (value_init
|| init
== NULL_TREE
)
4880 eltinit
= NULL_TREE
;
4884 eltinit
= cp_build_array_ref (input_location
, init
, idx
, complain
);
4885 eltinit
= cxx_eval_vec_init_1 (&new_ctx
, elttype
, eltinit
, value_init
,
4887 non_constant_p
, overflow_p
);
4891 /* Initializing an element using value or default initialization
4892 we just pre-built above. */
4893 if (init
== void_node
)
4894 /* Trivial default-init, don't do anything to the CONSTRUCTOR. */
4896 eltinit
= cxx_eval_constant_expression (&new_ctx
, init
, lval
,
4897 non_constant_p
, overflow_p
);
4902 /* Copying an element. */
4903 gcc_assert (same_type_ignoring_top_level_qualifiers_p
4904 (atype
, TREE_TYPE (init
)));
4905 eltinit
= cp_build_array_ref (input_location
, init
, idx
, complain
);
4906 if (!lvalue_p (init
))
4907 eltinit
= move (eltinit
);
4908 eltinit
= force_rvalue (eltinit
, complain
);
4909 eltinit
= cxx_eval_constant_expression (&new_ctx
, eltinit
, lval
,
4910 non_constant_p
, overflow_p
);
4912 if (*non_constant_p
)
4914 if (new_ctx
.ctor
!= ctx
->ctor
)
4916 /* We appended this element above; update the value. */
4917 gcc_assert ((*p
)->last().index
== idx
);
4918 (*p
)->last().value
= eltinit
;
4921 CONSTRUCTOR_APPEND_ELT (*p
, idx
, eltinit
);
4922 /* Reuse the result of cxx_eval_constant_expression call
4923 from the first iteration to all others if it is a constant
4924 initializer that doesn't require relocations. */
4927 && (eltinit
== NULL_TREE
4928 || (initializer_constant_valid_p (eltinit
, TREE_TYPE (eltinit
))
4929 == null_pointer_node
)))
4931 if (new_ctx
.ctor
!= ctx
->ctor
)
4932 eltinit
= new_ctx
.ctor
;
4933 tree range
= build2 (RANGE_EXPR
, size_type_node
,
4934 build_int_cst (size_type_node
, 1),
4935 build_int_cst (size_type_node
, max
- 1));
4936 CONSTRUCTOR_APPEND_ELT (*p
, range
, unshare_constructor (eltinit
));
4940 vec_safe_reserve (*p
, max
);
4943 if (!*non_constant_p
)
4946 CONSTRUCTOR_NO_CLEARING (init
) = false;
4952 cxx_eval_vec_init (const constexpr_ctx
*ctx
, tree t
,
4954 bool *non_constant_p
, bool *overflow_p
)
4956 tree atype
= TREE_TYPE (t
);
4957 tree init
= VEC_INIT_EXPR_INIT (t
);
4958 tree r
= cxx_eval_vec_init_1 (ctx
, atype
, init
,
4959 VEC_INIT_EXPR_VALUE_INIT (t
),
4960 lval
, non_constant_p
, overflow_p
);
4961 if (*non_constant_p
)
4967 /* Like same_type_ignoring_top_level_qualifiers_p, but also handle the case
4968 where the desired type is an array of unknown bounds because the variable
4969 has had its bounds deduced since the wrapping expression was created. */
4972 same_type_ignoring_tlq_and_bounds_p (tree type1
, tree type2
)
4974 while (TREE_CODE (type1
) == ARRAY_TYPE
4975 && TREE_CODE (type2
) == ARRAY_TYPE
4976 && (!TYPE_DOMAIN (type1
) || !TYPE_DOMAIN (type2
)))
4978 type1
= TREE_TYPE (type1
);
4979 type2
= TREE_TYPE (type2
);
4981 return same_type_ignoring_top_level_qualifiers_p (type1
, type2
);
4984 /* Try to determine the currently active union member for an expression
4985 with UNION_TYPE. If it can be determined, return the FIELD_DECL,
4986 otherwise return NULL_TREE. */
4989 cxx_union_active_member (const constexpr_ctx
*ctx
, tree t
)
4991 constexpr_ctx new_ctx
= *ctx
;
4992 new_ctx
.quiet
= true;
4993 bool non_constant_p
= false, overflow_p
= false;
4994 tree ctor
= cxx_eval_constant_expression (&new_ctx
, t
, false,
4997 if (TREE_CODE (ctor
) == CONSTRUCTOR
4998 && CONSTRUCTOR_NELTS (ctor
) == 1
4999 && CONSTRUCTOR_ELT (ctor
, 0)->index
5000 && TREE_CODE (CONSTRUCTOR_ELT (ctor
, 0)->index
) == FIELD_DECL
)
5001 return CONSTRUCTOR_ELT (ctor
, 0)->index
;
5005 /* Helper function for cxx_fold_indirect_ref_1, called recursively. */
5008 cxx_fold_indirect_ref_1 (const constexpr_ctx
*ctx
, location_t loc
, tree type
,
5009 tree op
, unsigned HOST_WIDE_INT off
, bool *empty_base
)
5011 tree optype
= TREE_TYPE (op
);
5012 unsigned HOST_WIDE_INT const_nunits
;
5013 if (off
== 0 && similar_type_p (optype
, type
))
5015 else if (TREE_CODE (optype
) == COMPLEX_TYPE
5016 && similar_type_p (type
, TREE_TYPE (optype
)))
5018 /* *(foo *)&complexfoo => __real__ complexfoo */
5020 return build1_loc (loc
, REALPART_EXPR
, type
, op
);
5021 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
5022 else if (tree_to_uhwi (TYPE_SIZE_UNIT (type
)) == off
)
5023 return build1_loc (loc
, IMAGPART_EXPR
, type
, op
);
5025 /* ((foo*)&vectorfoo)[x] => BIT_FIELD_REF<vectorfoo,...> */
5026 else if (VECTOR_TYPE_P (optype
)
5027 && similar_type_p (type
, TREE_TYPE (optype
))
5028 && TYPE_VECTOR_SUBPARTS (optype
).is_constant (&const_nunits
))
5030 unsigned HOST_WIDE_INT part_width
= tree_to_uhwi (TYPE_SIZE_UNIT (type
));
5031 unsigned HOST_WIDE_INT max_offset
= part_width
* const_nunits
;
5032 if (off
< max_offset
&& off
% part_width
== 0)
5034 tree index
= bitsize_int (off
* BITS_PER_UNIT
);
5035 return build3_loc (loc
, BIT_FIELD_REF
, type
, op
,
5036 TYPE_SIZE (type
), index
);
5039 /* ((foo *)&fooarray)[x] => fooarray[x] */
5040 else if (TREE_CODE (optype
) == ARRAY_TYPE
5041 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (optype
)))
5042 && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (optype
))))
5044 tree type_domain
= TYPE_DOMAIN (optype
);
5045 tree min_val
= size_zero_node
;
5046 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
5047 min_val
= TYPE_MIN_VALUE (type_domain
);
5048 unsigned HOST_WIDE_INT el_sz
5049 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (optype
)));
5050 unsigned HOST_WIDE_INT idx
= off
/ el_sz
;
5051 unsigned HOST_WIDE_INT rem
= off
% el_sz
;
5052 if (tree_fits_uhwi_p (min_val
))
5054 tree index
= size_int (idx
+ tree_to_uhwi (min_val
));
5055 op
= build4_loc (loc
, ARRAY_REF
, TREE_TYPE (optype
), op
, index
,
5056 NULL_TREE
, NULL_TREE
);
5057 return cxx_fold_indirect_ref_1 (ctx
, loc
, type
, op
, rem
,
5061 /* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */
5062 else if (TREE_CODE (optype
) == RECORD_TYPE
5063 || TREE_CODE (optype
) == UNION_TYPE
)
5065 if (TREE_CODE (optype
) == UNION_TYPE
)
5066 /* For unions prefer the currently active member. */
5067 if (tree field
= cxx_union_active_member (ctx
, op
))
5069 unsigned HOST_WIDE_INT el_sz
5070 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field
)));
5073 tree cop
= build3 (COMPONENT_REF
, TREE_TYPE (field
),
5074 op
, field
, NULL_TREE
);
5075 if (tree ret
= cxx_fold_indirect_ref_1 (ctx
, loc
, type
, cop
,
5080 for (tree field
= TYPE_FIELDS (optype
);
5081 field
; field
= DECL_CHAIN (field
))
5082 if (TREE_CODE (field
) == FIELD_DECL
5083 && TREE_TYPE (field
) != error_mark_node
5084 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (field
))))
5086 tree pos
= byte_position (field
);
5087 if (!tree_fits_uhwi_p (pos
))
5089 unsigned HOST_WIDE_INT upos
= tree_to_uhwi (pos
);
5090 unsigned HOST_WIDE_INT el_sz
5091 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field
)));
5092 if (upos
<= off
&& off
< upos
+ el_sz
)
5094 tree cop
= build3 (COMPONENT_REF
, TREE_TYPE (field
),
5095 op
, field
, NULL_TREE
);
5096 if (tree ret
= cxx_fold_indirect_ref_1 (ctx
, loc
, type
, cop
,
5102 /* Also handle conversion to an empty base class, which
5103 is represented with a NOP_EXPR. */
5104 if (is_empty_class (type
)
5105 && CLASS_TYPE_P (optype
)
5106 && DERIVED_FROM_P (type
, optype
))
5116 /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
5117 match. We want to be less strict for simple *& folding; if we have a
5118 non-const temporary that we access through a const pointer, that should
5119 work. We handle this here rather than change fold_indirect_ref_1
5120 because we're dealing with things like ADDR_EXPR of INTEGER_CST which
5121 don't really make sense outside of constant expression evaluation. Also
5122 we want to allow folding to COMPONENT_REF, which could cause trouble
5123 with TBAA in fold_indirect_ref_1. */
5126 cxx_fold_indirect_ref (const constexpr_ctx
*ctx
, location_t loc
, tree type
,
5127 tree op0
, bool *empty_base
)
5131 poly_uint64 const_op01
;
5133 /* STRIP_NOPS, but stop if REINTERPRET_CAST_P. */
5134 while (CONVERT_EXPR_P (sub
) || TREE_CODE (sub
) == NON_LVALUE_EXPR
5135 || TREE_CODE (sub
) == VIEW_CONVERT_EXPR
)
5137 if (TREE_CODE (sub
) == NOP_EXPR
5138 && REINTERPRET_CAST_P (sub
))
5140 sub
= TREE_OPERAND (sub
, 0);
5143 subtype
= TREE_TYPE (sub
);
5144 if (!INDIRECT_TYPE_P (subtype
))
5147 if (TREE_CODE (sub
) == ADDR_EXPR
)
5149 tree op
= TREE_OPERAND (sub
, 0);
5150 tree optype
= TREE_TYPE (op
);
5152 /* *&CONST_DECL -> to the value of the const decl. */
5153 if (TREE_CODE (op
) == CONST_DECL
)
5154 return DECL_INITIAL (op
);
5155 /* *&p => p; make sure to handle *&"str"[cst] here. */
5156 if (similar_type_p (optype
, type
))
5158 tree fop
= fold_read_from_constant_string (op
);
5165 return cxx_fold_indirect_ref_1 (ctx
, loc
, type
, op
, 0, empty_base
);
5167 else if (TREE_CODE (sub
) == POINTER_PLUS_EXPR
5168 && tree_fits_uhwi_p (TREE_OPERAND (sub
, 1)))
5170 tree op00
= TREE_OPERAND (sub
, 0);
5171 tree off
= TREE_OPERAND (sub
, 1);
5174 if (TREE_CODE (op00
) == ADDR_EXPR
)
5176 tree obj
= TREE_OPERAND (op00
, 0);
5177 while (TREE_CODE (obj
) == COMPONENT_REF
5178 && tree_int_cst_sign_bit (off
))
5180 /* Canonicalize this object/offset pair by iteratively absorbing
5181 the innermost component into the offset until the offset is
5182 nonnegative, so that cxx_fold_indirect_ref_1 can identify
5183 more folding opportunities. */
5184 tree field
= TREE_OPERAND (obj
, 1);
5185 off
= int_const_binop (PLUS_EXPR
, off
, byte_position (field
));
5186 obj
= TREE_OPERAND (obj
, 0);
5188 return cxx_fold_indirect_ref_1 (ctx
, loc
, type
, obj
,
5189 tree_to_uhwi (off
), empty_base
);
5192 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
5193 else if (TREE_CODE (TREE_TYPE (subtype
)) == ARRAY_TYPE
5194 && similar_type_p (type
, TREE_TYPE (TREE_TYPE (subtype
))))
5197 tree min_val
= size_zero_node
;
5199 = cxx_fold_indirect_ref (ctx
, loc
, TREE_TYPE (subtype
), sub
, NULL
);
5203 sub
= build1_loc (loc
, INDIRECT_REF
, TREE_TYPE (subtype
), sub
);
5204 type_domain
= TYPE_DOMAIN (TREE_TYPE (sub
));
5205 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
5206 min_val
= TYPE_MIN_VALUE (type_domain
);
5207 return build4_loc (loc
, ARRAY_REF
, type
, sub
, min_val
, NULL_TREE
,
5215 cxx_eval_indirect_ref (const constexpr_ctx
*ctx
, tree t
,
5217 bool *non_constant_p
, bool *overflow_p
)
5219 tree orig_op0
= TREE_OPERAND (t
, 0);
5220 bool empty_base
= false;
5222 /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second
5223 operand is an integer-zero. Otherwise reject the MEM_REF for now. */
5225 if (TREE_CODE (t
) == MEM_REF
5226 && (!TREE_OPERAND (t
, 1) || !integer_zerop (TREE_OPERAND (t
, 1))))
5228 gcc_assert (ctx
->quiet
);
5229 *non_constant_p
= true;
5233 /* First try to simplify it directly. */
5234 tree r
= cxx_fold_indirect_ref (ctx
, EXPR_LOCATION (t
), TREE_TYPE (t
),
5235 orig_op0
, &empty_base
);
5238 /* If that didn't work, evaluate the operand first. */
5239 tree op0
= cxx_eval_constant_expression (ctx
, orig_op0
,
5240 /*lval*/false, non_constant_p
,
5242 /* Don't VERIFY_CONSTANT here. */
5243 if (*non_constant_p
)
5246 if (!lval
&& integer_zerop (op0
))
5249 error ("dereferencing a null pointer");
5250 *non_constant_p
= true;
5254 r
= cxx_fold_indirect_ref (ctx
, EXPR_LOCATION (t
), TREE_TYPE (t
), op0
,
5258 /* We couldn't fold to a constant value. Make sure it's not
5259 something we should have been able to fold. */
5262 if (TREE_CODE (sub
) == ADDR_EXPR
)
5264 gcc_assert (!similar_type_p
5265 (TREE_TYPE (TREE_TYPE (sub
)), TREE_TYPE (t
)));
5266 /* DR 1188 says we don't have to deal with this. */
5268 error_at (cp_expr_loc_or_input_loc (t
),
5269 "accessing value of %qE through a %qT glvalue in a "
5270 "constant expression", build_fold_indirect_ref (sub
),
5272 *non_constant_p
= true;
5276 if (lval
&& op0
!= orig_op0
)
5277 return build1 (INDIRECT_REF
, TREE_TYPE (t
), op0
);
5279 VERIFY_CONSTANT (t
);
5284 r
= cxx_eval_constant_expression (ctx
, r
,
5285 lval
, non_constant_p
, overflow_p
);
5286 if (*non_constant_p
)
5289 /* If we're pulling out the value of an empty base, just return an empty
5291 if (empty_base
&& !lval
)
5293 r
= build_constructor (TREE_TYPE (t
), NULL
);
5294 TREE_CONSTANT (r
) = true;
5300 /* Complain about R, a VAR_DECL, not being usable in a constant expression.
5301 Shared between potential_constant_expression and
5302 cxx_eval_constant_expression. */
5305 non_const_var_error (location_t loc
, tree r
)
5307 auto_diagnostic_group d
;
5308 tree type
= TREE_TYPE (r
);
5309 if (DECL_NAME (r
) == heap_uninit_identifier
5310 || DECL_NAME (r
) == heap_identifier
5311 || DECL_NAME (r
) == heap_vec_uninit_identifier
5312 || DECL_NAME (r
) == heap_vec_identifier
)
5314 error_at (loc
, "the content of uninitialized storage is not usable "
5315 "in a constant expression");
5316 inform (DECL_SOURCE_LOCATION (r
), "allocated here");
5319 if (DECL_NAME (r
) == heap_deleted_identifier
)
5321 error_at (loc
, "use of allocated storage after deallocation in a "
5322 "constant expression");
5323 inform (DECL_SOURCE_LOCATION (r
), "allocated here");
5326 error_at (loc
, "the value of %qD is not usable in a constant "
5328 /* Avoid error cascade. */
5329 if (DECL_INITIAL (r
) == error_mark_node
)
5331 if (DECL_DECLARED_CONSTEXPR_P (r
))
5332 inform (DECL_SOURCE_LOCATION (r
),
5333 "%qD used in its own initializer", r
);
5334 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5336 if (!CP_TYPE_CONST_P (type
))
5337 inform (DECL_SOURCE_LOCATION (r
),
5338 "%q#D is not const", r
);
5339 else if (CP_TYPE_VOLATILE_P (type
))
5340 inform (DECL_SOURCE_LOCATION (r
),
5341 "%q#D is volatile", r
);
5342 else if (!DECL_INITIAL (r
)
5343 || !TREE_CONSTANT (DECL_INITIAL (r
))
5344 || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
))
5345 inform (DECL_SOURCE_LOCATION (r
),
5346 "%qD was not initialized with a constant "
5351 else if (TYPE_REF_P (type
))
5352 inform (DECL_SOURCE_LOCATION (r
),
5353 "%qD was not initialized with a constant "
5357 if (cxx_dialect
>= cxx11
&& !DECL_DECLARED_CONSTEXPR_P (r
))
5358 inform (DECL_SOURCE_LOCATION (r
),
5359 "%qD was not declared %<constexpr%>", r
);
5361 inform (DECL_SOURCE_LOCATION (r
),
5362 "%qD does not have integral or enumeration type",
5367 /* Subroutine of cxx_eval_constant_expression.
5368 Like cxx_eval_unary_expression, except for trinary expressions. */
5371 cxx_eval_trinary_expression (const constexpr_ctx
*ctx
, tree t
,
5373 bool *non_constant_p
, bool *overflow_p
)
5379 for (i
= 0; i
< 3; i
++)
5381 args
[i
] = cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, i
),
5383 non_constant_p
, overflow_p
);
5384 VERIFY_CONSTANT (args
[i
]);
5387 val
= fold_ternary_loc (EXPR_LOCATION (t
), TREE_CODE (t
), TREE_TYPE (t
),
5388 args
[0], args
[1], args
[2]);
5389 if (val
== NULL_TREE
)
5391 VERIFY_CONSTANT (val
);
5395 /* True if T was declared in a function declared to be constexpr, and
5396 therefore potentially constant in C++14. */
5399 var_in_constexpr_fn (tree t
)
5401 tree ctx
= DECL_CONTEXT (t
);
5402 return (ctx
&& TREE_CODE (ctx
) == FUNCTION_DECL
5403 && DECL_DECLARED_CONSTEXPR_P (ctx
));
5406 /* True if a function might be constexpr: either a function that was
5407 declared constexpr, or a C++17 lambda op(). */
5410 maybe_constexpr_fn (tree t
)
5412 return (DECL_DECLARED_CONSTEXPR_P (t
)
5413 || (cxx_dialect
>= cxx17
&& LAMBDA_FUNCTION_P (t
))
5414 || (flag_implicit_constexpr
5415 && DECL_DECLARED_INLINE_P (STRIP_TEMPLATE (t
))));
5418 /* True if T was declared in a function that might be constexpr: either a
5419 function that was declared constexpr, or a C++17 lambda op(). */
5422 var_in_maybe_constexpr_fn (tree t
)
5424 return (DECL_FUNCTION_SCOPE_P (t
)
5425 && maybe_constexpr_fn (DECL_CONTEXT (t
)));
5428 /* We're assigning INIT to TARGET. In do_build_copy_constructor and
5429 build_over_call we implement trivial copy of a class with tail padding using
5430 assignment of character arrays, which is valid in normal code, but not in
5431 constexpr evaluation. We don't need to worry about clobbering tail padding
5432 in constexpr evaluation, so strip the type punning. */
5435 maybe_simplify_trivial_copy (tree
&target
, tree
&init
)
5437 if (TREE_CODE (target
) == MEM_REF
5438 && TREE_CODE (init
) == MEM_REF
5439 && TREE_TYPE (target
) == TREE_TYPE (init
)
5440 && TREE_CODE (TREE_TYPE (target
)) == ARRAY_TYPE
5441 && TREE_TYPE (TREE_TYPE (target
)) == unsigned_char_type_node
)
5443 target
= build_fold_indirect_ref (TREE_OPERAND (target
, 0));
5444 init
= build_fold_indirect_ref (TREE_OPERAND (init
, 0));
5448 /* Returns true if REF, which is a COMPONENT_REF, has any fields
5449 of constant type. This does not check for 'mutable', so the
5450 caller is expected to be mindful of that. */
5453 cref_has_const_field (tree ref
)
5455 while (TREE_CODE (ref
) == COMPONENT_REF
)
5457 if (CP_TYPE_CONST_P (TREE_TYPE (TREE_OPERAND (ref
, 1))))
5459 ref
= TREE_OPERAND (ref
, 0);
5464 /* Return true if we are modifying something that is const during constant
5465 expression evaluation. CODE is the code of the statement, OBJ is the
5466 object in question, MUTABLE_P is true if one of the subobjects were
5467 declared mutable. */
5470 modifying_const_object_p (tree_code code
, tree obj
, bool mutable_p
)
5472 /* If this is initialization, there's no problem. */
5473 if (code
!= MODIFY_EXPR
)
5476 /* [basic.type.qualifier] "A const object is an object of type
5477 const T or a non-mutable subobject of a const object." */
5481 if (TREE_READONLY (obj
))
5484 if (CP_TYPE_CONST_P (TREE_TYPE (obj
)))
5486 /* Although a COMPONENT_REF may have a const type, we should
5487 only consider it modifying a const object when any of the
5488 field components is const. This can happen when using
5489 constructs such as const_cast<const T &>(m), making something
5490 const even though it wasn't declared const. */
5491 if (TREE_CODE (obj
) == COMPONENT_REF
)
5492 return cref_has_const_field (obj
);
5500 /* Evaluate an INIT_EXPR or MODIFY_EXPR. */
5503 cxx_eval_store_expression (const constexpr_ctx
*ctx
, tree t
,
5505 bool *non_constant_p
, bool *overflow_p
)
5507 constexpr_ctx new_ctx
= *ctx
;
5509 tree init
= TREE_OPERAND (t
, 1);
5510 if (TREE_CLOBBER_P (init
))
5511 /* Just ignore clobbers. */
5514 /* First we figure out where we're storing to. */
5515 tree target
= TREE_OPERAND (t
, 0);
5517 maybe_simplify_trivial_copy (target
, init
);
5519 tree type
= TREE_TYPE (target
);
5520 bool preeval
= SCALAR_TYPE_P (type
) || TREE_CODE (t
) == MODIFY_EXPR
;
5523 /* Evaluate the value to be stored without knowing what object it will be
5524 stored in, so that any side-effects happen first. */
5525 if (!SCALAR_TYPE_P (type
))
5526 new_ctx
.ctor
= new_ctx
.object
= NULL_TREE
;
5527 init
= cxx_eval_constant_expression (&new_ctx
, init
, false,
5528 non_constant_p
, overflow_p
);
5529 if (*non_constant_p
)
5533 bool evaluated
= false;
5536 /* If we want to return a reference to the target, we need to evaluate it
5537 as a whole; otherwise, only evaluate the innermost piece to avoid
5538 building up unnecessary *_REFs. */
5539 target
= cxx_eval_constant_expression (ctx
, target
, true,
5540 non_constant_p
, overflow_p
);
5542 if (*non_constant_p
)
5546 /* Find the underlying variable. */
5548 tree object
= NULL_TREE
;
5549 /* If we're modifying a const object, save it. */
5550 tree const_object_being_modified
= NULL_TREE
;
5551 bool mutable_p
= false;
5552 for (tree probe
= target
; object
== NULL_TREE
; )
5554 switch (TREE_CODE (probe
))
5560 tree ob
= TREE_OPERAND (probe
, 0);
5561 tree elt
= TREE_OPERAND (probe
, 1);
5562 if (TREE_CODE (elt
) == FIELD_DECL
&& DECL_MUTABLE_P (elt
))
5564 if (TREE_CODE (probe
) == ARRAY_REF
)
5566 elt
= eval_and_check_array_index (ctx
, probe
, false,
5567 non_constant_p
, overflow_p
);
5568 if (*non_constant_p
)
5571 /* We don't check modifying_const_object_p for ARRAY_REFs. Given
5572 "int a[10]", an ARRAY_REF "a[2]" can be "const int", even though
5573 the array isn't const. Instead, check "a" in the next iteration;
5574 that will detect modifying "const int a[10]". */
5576 && modifying_const_object_p (TREE_CODE (t
), probe
,
5578 && const_object_being_modified
== NULL_TREE
)
5579 const_object_being_modified
= probe
;
5580 vec_safe_push (refs
, elt
);
5581 vec_safe_push (refs
, TREE_TYPE (probe
));
5591 probe
= cxx_eval_constant_expression (ctx
, probe
, true,
5592 non_constant_p
, overflow_p
);
5594 if (*non_constant_p
)
5601 if (modifying_const_object_p (TREE_CODE (t
), object
, mutable_p
)
5602 && const_object_being_modified
== NULL_TREE
)
5603 const_object_being_modified
= object
;
5605 /* And then find/build up our initializer for the path to the subobject
5606 we're initializing. */
5608 if (DECL_P (object
))
5609 valp
= ctx
->global
->values
.get (object
);
5614 /* A constant-expression cannot modify objects from outside the
5615 constant-expression. */
5617 error ("modification of %qE is not a constant expression", object
);
5618 *non_constant_p
= true;
5621 type
= TREE_TYPE (object
);
5622 bool no_zero_init
= true;
5624 releasing_vec ctors
, indexes
;
5625 auto_vec
<int> index_pos_hints
;
5626 bool activated_union_member_p
= false;
5627 while (!refs
->is_empty ())
5629 if (*valp
== NULL_TREE
)
5631 *valp
= build_constructor (type
, NULL
);
5632 CONSTRUCTOR_NO_CLEARING (*valp
) = no_zero_init
;
5634 else if (TREE_CODE (*valp
) == STRING_CST
)
5636 /* An array was initialized with a string constant, and now
5637 we're writing into one of its elements. Explode the
5638 single initialization into a set of element
5640 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
5642 tree string
= *valp
;
5643 tree elt_type
= TREE_TYPE (type
);
5644 unsigned chars_per_elt
= (TYPE_PRECISION (elt_type
)
5645 / TYPE_PRECISION (char_type_node
));
5646 unsigned num_elts
= TREE_STRING_LENGTH (string
) / chars_per_elt
;
5647 tree ary_ctor
= build_constructor (type
, NULL
);
5649 vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor
), num_elts
);
5650 for (unsigned ix
= 0; ix
!= num_elts
; ix
++)
5652 constructor_elt elt
=
5654 build_int_cst (size_type_node
, ix
),
5655 extract_string_elt (string
, chars_per_elt
, ix
)
5657 CONSTRUCTOR_ELTS (ary_ctor
)->quick_push (elt
);
5663 /* If the value of object is already zero-initialized, any new ctors for
5664 subobjects will also be zero-initialized. */
5665 no_zero_init
= CONSTRUCTOR_NO_CLEARING (*valp
);
5667 enum tree_code code
= TREE_CODE (type
);
5669 tree index
= refs
->pop();
5671 if (code
== RECORD_TYPE
&& is_empty_field (index
))
5672 /* Don't build a sub-CONSTRUCTOR for an empty base or field, as they
5673 have no data and might have an offset lower than previously declared
5674 fields, which confuses the middle-end. The code below will notice
5675 that we don't have a CONSTRUCTOR for our inner target and just
5679 if (code
== UNION_TYPE
&& CONSTRUCTOR_NELTS (*valp
)
5680 && CONSTRUCTOR_ELT (*valp
, 0)->index
!= index
)
5682 if (cxx_dialect
< cxx20
)
5685 error_at (cp_expr_loc_or_input_loc (t
),
5686 "change of the active member of a union "
5688 CONSTRUCTOR_ELT (*valp
, 0)->index
,
5690 *non_constant_p
= true;
5692 else if (TREE_CODE (t
) == MODIFY_EXPR
5693 && CONSTRUCTOR_NO_CLEARING (*valp
))
5695 /* Diagnose changing the active union member while the union
5696 is in the process of being initialized. */
5698 error_at (cp_expr_loc_or_input_loc (t
),
5699 "change of the active member of a union "
5700 "from %qD to %qD during initialization",
5701 CONSTRUCTOR_ELT (*valp
, 0)->index
,
5703 *non_constant_p
= true;
5705 no_zero_init
= true;
5708 vec_safe_push (ctors
, *valp
);
5709 vec_safe_push (indexes
, index
);
5711 constructor_elt
*cep
5712 = get_or_insert_ctor_field (*valp
, index
);
5713 index_pos_hints
.safe_push (cep
- CONSTRUCTOR_ELTS (*valp
)->begin());
5715 if (code
== UNION_TYPE
)
5716 activated_union_member_p
= true;
5721 /* Detect modifying a constant object in constexpr evaluation.
5722 We have found a const object that is being modified. Figure out
5723 if we need to issue an error. Consider
5727 constexpr A() : n(1) { n = 2; } // #1
5731 constexpr B() { a.n = 3; } // #2
5735 #1 is OK, since we're modifying an object under construction, but
5736 #2 is wrong, since "a" is const and has been fully constructed.
5737 To track it, we use the TREE_READONLY bit in the object's CONSTRUCTOR
5738 which means that the object is read-only. For the example above, the
5739 *ctors stack at the point of #2 will look like:
5741 ctors[0] = {.a={.n=2}} TREE_READONLY = 0
5742 ctors[1] = {.n=2} TREE_READONLY = 1
5744 and we're modifying "b.a", so we search the stack and see if the
5745 constructor for "b.a" has already run. */
5746 if (const_object_being_modified
)
5750 = strip_array_types (TREE_TYPE (const_object_being_modified
));
5751 if (!CLASS_TYPE_P (const_objtype
))
5755 /* [class.ctor]p5 "A constructor can be invoked for a const,
5756 volatile, or const volatile object. const and volatile
5757 semantics are not applied on an object under construction.
5758 They come into effect when the constructor for the most
5759 derived object ends." */
5760 for (tree elt
: *ctors
)
5761 if (same_type_ignoring_top_level_qualifiers_p
5762 (TREE_TYPE (const_object_being_modified
), TREE_TYPE (elt
)))
5764 fail
= TREE_READONLY (elt
);
5771 modifying_const_object_error (t
, const_object_being_modified
);
5772 *non_constant_p
= true;
5779 /* We're handling an INIT_EXPR of class type, so the value of the
5780 initializer can depend on the object it's initializing. */
5782 /* Create a new CONSTRUCTOR in case evaluation of the initializer
5783 wants to modify it. */
5784 if (*valp
== NULL_TREE
)
5786 *valp
= build_constructor (type
, NULL
);
5787 CONSTRUCTOR_NO_CLEARING (*valp
) = no_zero_init
;
5789 new_ctx
.ctor
= *valp
;
5790 new_ctx
.object
= target
;
5791 /* Avoid temporary materialization when initializing from a TARGET_EXPR.
5792 We don't need to mess with AGGR_EXPR_SLOT/VEC_INIT_EXPR_SLOT because
5793 expansion of those trees uses ctx instead. */
5794 if (TREE_CODE (init
) == TARGET_EXPR
)
5795 if (tree tinit
= TARGET_EXPR_INITIAL (init
))
5797 init
= cxx_eval_constant_expression (&new_ctx
, init
, false,
5798 non_constant_p
, overflow_p
);
5799 /* The hash table might have moved since the get earlier, and the
5800 initializer might have mutated the underlying CONSTRUCTORs, so we must
5802 valp
= ctx
->global
->values
.get (object
);
5803 for (unsigned i
= 0; i
< vec_safe_length (indexes
); i
++)
5805 constructor_elt
*cep
5806 = get_or_insert_ctor_field (*valp
, indexes
[i
], index_pos_hints
[i
]);
5811 /* Don't share a CONSTRUCTOR that might be changed later. */
5812 init
= unshare_constructor (init
);
5814 if (*valp
&& TREE_CODE (*valp
) == CONSTRUCTOR
5815 && TREE_CODE (init
) == CONSTRUCTOR
)
5817 /* An outer ctx->ctor might be pointing to *valp, so replace
5819 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init
),
5822 /* For initialization of an empty base, the original target will be
5823 *(base*)this, evaluation of which resolves to the object
5824 argument, which has the derived type rather than the base type. In
5825 this situation, just evaluate the initializer and return, since
5826 there's no actual data to store. */
5827 gcc_assert (is_empty_class (TREE_TYPE (init
)));
5828 return lval
? target
: init
;
5830 CONSTRUCTOR_ELTS (*valp
) = CONSTRUCTOR_ELTS (init
);
5831 TREE_CONSTANT (*valp
) = TREE_CONSTANT (init
);
5832 TREE_SIDE_EFFECTS (*valp
) = TREE_SIDE_EFFECTS (init
);
5833 CONSTRUCTOR_NO_CLEARING (*valp
)
5834 = CONSTRUCTOR_NO_CLEARING (init
);
5836 else if (TREE_CODE (init
) == CONSTRUCTOR
5837 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init
),
5840 /* See above on initialization of empty bases. */
5841 gcc_assert (is_empty_class (TREE_TYPE (init
)) && !lval
);
5847 /* After initialization, 'const' semantics apply to the value of the
5848 object. Make a note of this fact by marking the CONSTRUCTOR
5850 if (TREE_CODE (t
) == INIT_EXPR
5851 && TREE_CODE (*valp
) == CONSTRUCTOR
5852 && TYPE_READONLY (type
))
5854 if (INDIRECT_REF_P (target
)
5855 && (is_this_parameter
5856 (tree_strip_nop_conversions (TREE_OPERAND (target
, 0)))))
5857 /* We've just initialized '*this' (perhaps via the target
5858 constructor of a delegating constructor). Leave it up to the
5859 caller that set 'this' to set TREE_READONLY appropriately. */
5860 gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
5861 (TREE_TYPE (target
), type
));
5863 TREE_READONLY (*valp
) = true;
5866 /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing
5867 CONSTRUCTORs, if any. */
5868 bool c
= TREE_CONSTANT (init
);
5869 bool s
= TREE_SIDE_EFFECTS (init
);
5870 if (!c
|| s
|| activated_union_member_p
)
5871 for (tree elt
: *ctors
)
5874 TREE_CONSTANT (elt
) = false;
5876 TREE_SIDE_EFFECTS (elt
) = true;
5877 /* Clear CONSTRUCTOR_NO_CLEARING since we've activated a member of
5879 if (TREE_CODE (TREE_TYPE (elt
)) == UNION_TYPE
)
5880 CONSTRUCTOR_NO_CLEARING (elt
) = false;
5883 if (*non_constant_p
)
5891 /* Evaluate a ++ or -- expression. */
5894 cxx_eval_increment_expression (const constexpr_ctx
*ctx
, tree t
,
5896 bool *non_constant_p
, bool *overflow_p
)
5898 enum tree_code code
= TREE_CODE (t
);
5899 tree type
= TREE_TYPE (t
);
5900 tree op
= TREE_OPERAND (t
, 0);
5901 tree offset
= TREE_OPERAND (t
, 1);
5902 gcc_assert (TREE_CONSTANT (offset
));
5904 /* OFFSET is constant, but perhaps not constant enough. We need to
5905 e.g. bash FLOAT_EXPRs to REAL_CSTs. */
5906 offset
= fold_simple (offset
);
5908 /* The operand as an lvalue. */
5909 op
= cxx_eval_constant_expression (ctx
, op
, true,
5910 non_constant_p
, overflow_p
);
5912 /* The operand as an rvalue. */
5914 = cxx_eval_constant_expression (ctx
, op
, false,
5915 non_constant_p
, overflow_p
);
5916 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
5917 a local array in a constexpr function. */
5918 bool ptr
= INDIRECT_TYPE_P (TREE_TYPE (val
));
5920 VERIFY_CONSTANT (val
);
5922 /* The modified value. */
5923 bool inc
= (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
);
5925 if (INDIRECT_TYPE_P (type
))
5927 /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
5928 offset
= convert_to_ptrofftype (offset
);
5930 offset
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (offset
), offset
);
5931 mod
= fold_build2 (POINTER_PLUS_EXPR
, type
, val
, offset
);
5934 mod
= fold_build2 (inc
? PLUS_EXPR
: MINUS_EXPR
, type
, val
, offset
);
5936 VERIFY_CONSTANT (mod
);
5938 /* Storing the modified value. */
5939 tree store
= build2_loc (cp_expr_loc_or_loc (t
, input_location
),
5940 MODIFY_EXPR
, type
, op
, mod
);
5941 mod
= cxx_eval_constant_expression (ctx
, store
, lval
,
5942 non_constant_p
, overflow_p
);
5944 if (*non_constant_p
)
5947 /* And the value of the expression. */
5948 if (code
== PREINCREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
5949 /* Prefix ops are lvalues, but the caller might want an rvalue;
5950 lval has already been taken into account in the store above. */
5953 /* Postfix ops are rvalues. */
5957 /* Predicates for the meaning of *jump_target. */
5960 returns (tree
*jump_target
)
5963 && (TREE_CODE (*jump_target
) == RETURN_EXPR
5964 || (TREE_CODE (*jump_target
) == LABEL_DECL
5965 && LABEL_DECL_CDTOR (*jump_target
)));
5969 breaks (tree
*jump_target
)
5972 && ((TREE_CODE (*jump_target
) == LABEL_DECL
5973 && LABEL_DECL_BREAK (*jump_target
))
5974 || TREE_CODE (*jump_target
) == BREAK_STMT
5975 || TREE_CODE (*jump_target
) == EXIT_EXPR
);
5979 continues (tree
*jump_target
)
5982 && ((TREE_CODE (*jump_target
) == LABEL_DECL
5983 && LABEL_DECL_CONTINUE (*jump_target
))
5984 || TREE_CODE (*jump_target
) == CONTINUE_STMT
);
5989 switches (tree
*jump_target
)
5992 && TREE_CODE (*jump_target
) == INTEGER_CST
;
5995 /* Subroutine of cxx_eval_statement_list. Determine whether the statement
5996 STMT matches *jump_target. If we're looking for a case label and we see
5997 the default label, note it in ctx->css_state. */
6000 label_matches (const constexpr_ctx
*ctx
, tree
*jump_target
, tree stmt
)
6002 switch (TREE_CODE (*jump_target
))
6005 if (TREE_CODE (stmt
) == LABEL_EXPR
6006 && LABEL_EXPR_LABEL (stmt
) == *jump_target
)
6011 if (TREE_CODE (stmt
) == CASE_LABEL_EXPR
)
6013 gcc_assert (ctx
->css_state
!= NULL
);
6014 if (!CASE_LOW (stmt
))
6016 /* default: should appear just once in a SWITCH_EXPR
6017 body (excluding nested SWITCH_EXPR). */
6018 gcc_assert (*ctx
->css_state
!= css_default_seen
);
6019 /* When evaluating SWITCH_EXPR body for the second time,
6020 return true for the default: label. */
6021 if (*ctx
->css_state
== css_default_processing
)
6023 *ctx
->css_state
= css_default_seen
;
6025 else if (CASE_HIGH (stmt
))
6027 if (tree_int_cst_le (CASE_LOW (stmt
), *jump_target
)
6028 && tree_int_cst_le (*jump_target
, CASE_HIGH (stmt
)))
6031 else if (tree_int_cst_equal (*jump_target
, CASE_LOW (stmt
)))
6038 /* These two are handled directly in cxx_eval_loop_expr by testing
6039 breaks (jump_target) or continues (jump_target). */
6048 /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
6049 semantics, for switch, break, continue, and return. */
6052 cxx_eval_statement_list (const constexpr_ctx
*ctx
, tree t
,
6053 bool *non_constant_p
, bool *overflow_p
,
6057 /* In a statement-expression we want to return the last value.
6058 For empty statement expression return void_node. */
6062 local_target
= NULL_TREE
;
6063 jump_target
= &local_target
;
6065 for (tree stmt
: tsi_range (t
))
6067 /* We've found a continue, so skip everything until we reach
6068 the label its jumping to. */
6069 if (continues (jump_target
))
6071 if (label_matches (ctx
, jump_target
, stmt
))
6073 *jump_target
= NULL_TREE
;
6077 if (TREE_CODE (stmt
) == DEBUG_BEGIN_STMT
)
6079 r
= cxx_eval_constant_expression (ctx
, stmt
, false,
6080 non_constant_p
, overflow_p
,
6082 if (*non_constant_p
)
6084 if (returns (jump_target
) || breaks (jump_target
))
6087 if (*jump_target
&& jump_target
== &local_target
)
6089 /* We aren't communicating the jump to our caller, so give up. We don't
6090 need to support evaluation of jumps out of statement-exprs. */
6092 error_at (cp_expr_loc_or_input_loc (r
),
6093 "statement is not a constant expression");
6094 *non_constant_p
= true;
6099 /* Evaluate a LOOP_EXPR for side-effects. Handles break and return
6100 semantics; continue semantics are covered by cxx_eval_statement_list. */
6103 cxx_eval_loop_expr (const constexpr_ctx
*ctx
, tree t
,
6104 bool *non_constant_p
, bool *overflow_p
,
6107 constexpr_ctx new_ctx
= *ctx
;
6111 local_target
= NULL_TREE
;
6112 jump_target
= &local_target
;
6115 tree body
, cond
= NULL_TREE
, expr
= NULL_TREE
;
6117 switch (TREE_CODE (t
))
6120 body
= LOOP_EXPR_BODY (t
);
6127 body
= WHILE_BODY (t
);
6128 cond
= WHILE_COND (t
);
6132 if (FOR_INIT_STMT (t
))
6133 cxx_eval_constant_expression (ctx
, FOR_INIT_STMT (t
), /*lval*/false,
6134 non_constant_p
, overflow_p
, jump_target
);
6135 if (*non_constant_p
)
6137 body
= FOR_BODY (t
);
6138 cond
= FOR_COND (t
);
6139 expr
= FOR_EXPR (t
);
6145 auto_vec
<tree
, 10> save_exprs
;
6146 new_ctx
.save_exprs
= &save_exprs
;
6152 cxx_eval_constant_expression (&new_ctx
, body
, /*lval*/false,
6153 non_constant_p
, overflow_p
,
6155 if (breaks (jump_target
))
6157 *jump_target
= NULL_TREE
;
6161 if (TREE_CODE (t
) != LOOP_EXPR
&& continues (jump_target
))
6162 *jump_target
= NULL_TREE
;
6165 cxx_eval_constant_expression (&new_ctx
, expr
, /*lval*/false,
6166 non_constant_p
, overflow_p
,
6173 = cxx_eval_constant_expression (&new_ctx
, cond
, /*lval*/false,
6174 non_constant_p
, overflow_p
,
6178 if (verify_constant (res
, ctx
->quiet
, non_constant_p
,
6181 if (integer_zerop (res
))
6185 gcc_assert (*jump_target
);
6188 /* Forget saved values of SAVE_EXPRs and TARGET_EXPRs. */
6189 for (tree save_expr
: save_exprs
)
6190 ctx
->global
->values
.remove (save_expr
);
6191 save_exprs
.truncate (0);
6193 if (++count
>= constexpr_loop_limit
)
6196 error_at (cp_expr_loc_or_input_loc (t
),
6197 "%<constexpr%> loop iteration count exceeds limit of %d "
6198 "(use %<-fconstexpr-loop-limit=%> to increase the limit)",
6199 constexpr_loop_limit
);
6200 *non_constant_p
= true;
6204 while (!returns (jump_target
)
6205 && !breaks (jump_target
)
6206 && !continues (jump_target
)
6207 && (!switches (jump_target
) || count
== 0)
6208 && !*non_constant_p
);
6210 /* Forget saved values of SAVE_EXPRs and TARGET_EXPRs. */
6211 for (tree save_expr
: save_exprs
)
6212 ctx
->global
->values
.remove (save_expr
);
6217 /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
6221 cxx_eval_switch_expr (const constexpr_ctx
*ctx
, tree t
,
6222 bool *non_constant_p
, bool *overflow_p
,
6226 = TREE_CODE (t
) == SWITCH_STMT
? SWITCH_STMT_COND (t
) : SWITCH_COND (t
);
6227 cond
= cxx_eval_constant_expression (ctx
, cond
, false,
6228 non_constant_p
, overflow_p
);
6229 VERIFY_CONSTANT (cond
);
6230 *jump_target
= cond
;
6233 = TREE_CODE (t
) == SWITCH_STMT
? SWITCH_STMT_BODY (t
) : SWITCH_BODY (t
);
6234 constexpr_ctx new_ctx
= *ctx
;
6235 constexpr_switch_state css
= css_default_not_seen
;
6236 new_ctx
.css_state
= &css
;
6237 cxx_eval_constant_expression (&new_ctx
, body
, false,
6238 non_constant_p
, overflow_p
, jump_target
);
6239 if (switches (jump_target
) && css
== css_default_seen
)
6241 /* If the SWITCH_EXPR body has default: label, process it once again,
6242 this time instructing label_matches to return true for default:
6243 label on switches (jump_target). */
6244 css
= css_default_processing
;
6245 cxx_eval_constant_expression (&new_ctx
, body
, false,
6246 non_constant_p
, overflow_p
, jump_target
);
6248 if (breaks (jump_target
) || switches (jump_target
))
6249 *jump_target
= NULL_TREE
;
6253 /* Find the object of TYPE under initialization in CTX. */
6256 lookup_placeholder (const constexpr_ctx
*ctx
, bool lval
, tree type
)
6261 /* Prefer the outermost matching object, but don't cross
6262 CONSTRUCTOR_PLACEHOLDER_BOUNDARY constructors. */
6263 if (ctx
->ctor
&& !CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx
->ctor
))
6264 if (tree outer_ob
= lookup_placeholder (ctx
->parent
, lval
, type
))
6267 /* We could use ctx->object unconditionally, but using ctx->ctor when we
6268 can is a minor optimization. */
6269 if (!lval
&& ctx
->ctor
&& same_type_p (TREE_TYPE (ctx
->ctor
), type
))
6275 /* Since an object cannot have a field of its own type, we can search outward
6276 from ctx->object to find the unique containing object of TYPE. */
6277 tree ob
= ctx
->object
;
6280 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob
), type
))
6282 if (handled_component_p (ob
))
6283 ob
= TREE_OPERAND (ob
, 0);
6291 /* Complain about an attempt to evaluate inline assembly. */
6294 inline_asm_in_constexpr_error (location_t loc
)
6296 auto_diagnostic_group d
;
6297 error_at (loc
, "inline assembly is not a constant expression");
6298 inform (loc
, "only unevaluated inline assembly is allowed in a "
6299 "%<constexpr%> function in C++20");
6302 /* We're getting the constant value of DECL in a manifestly constant-evaluated
6303 context; maybe complain about that. */
6306 maybe_warn_about_constant_value (location_t loc
, tree decl
)
6308 static bool explained
= false;
6309 if (cxx_dialect
>= cxx17
6310 && warn_interference_size
6311 && !OPTION_SET_P (param_destruct_interfere_size
)
6312 && DECL_CONTEXT (decl
) == std_node
6313 && id_equal (DECL_NAME (decl
), "hardware_destructive_interference_size")
6314 && (LOCATION_FILE (input_location
) != main_input_filename
6315 || module_exporting_p ())
6316 && warning_at (loc
, OPT_Winterference_size
, "use of %qD", decl
)
6320 inform (loc
, "its value can vary between compiler versions or "
6321 "with different %<-mtune%> or %<-mcpu%> flags");
6322 inform (loc
, "if this use is part of a public ABI, change it to "
6323 "instead use a constant variable you define");
6324 inform (loc
, "the default value for the current CPU tuning "
6325 "is %d bytes", param_destruct_interfere_size
);
6326 inform (loc
, "you can stabilize this value with %<--param "
6327 "hardware_destructive_interference_size=%d%>, or disable "
6328 "this warning with %<-Wno-interference-size%>",
6329 param_destruct_interfere_size
);
6333 /* Attempt to reduce the expression T to a constant value.
6334 On failure, issue diagnostic and return error_mark_node. */
6335 /* FIXME unify with c_fully_fold */
6336 /* FIXME overflow_p is too global */
6339 cxx_eval_constant_expression (const constexpr_ctx
*ctx
, tree t
,
6341 bool *non_constant_p
, bool *overflow_p
,
6342 tree
*jump_target
/* = NULL */)
6344 if (jump_target
&& *jump_target
)
6346 /* If we are jumping, ignore all statements/expressions except those
6347 that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */
6348 switch (TREE_CODE (t
))
6351 case STATEMENT_LIST
:
6360 case CASE_LABEL_EXPR
:
6361 if (label_matches (ctx
, jump_target
, t
))
6363 *jump_target
= NULL_TREE
;
6369 if (error_operand_p (t
))
6371 *non_constant_p
= true;
6375 location_t loc
= cp_expr_loc_or_input_loc (t
);
6377 STRIP_ANY_LOCATION_WRAPPER (t
);
6379 if (CONSTANT_CLASS_P (t
))
6381 if (TREE_OVERFLOW (t
))
6384 permerror (input_location
, "overflow in constant expression");
6385 if (!flag_permissive
|| ctx
->quiet
)
6389 if (TREE_CODE (t
) == INTEGER_CST
6390 && TYPE_PTR_P (TREE_TYPE (t
))
6391 /* INTEGER_CST with pointer-to-method type is only used
6392 for a virtual method in a pointer to member function.
6393 Don't reject those. */
6394 && TREE_CODE (TREE_TYPE (TREE_TYPE (t
))) != METHOD_TYPE
6395 && !integer_zerop (t
))
6398 error ("value %qE of type %qT is not a constant expression",
6400 *non_constant_p
= true;
6406 /* Avoid excessively long constexpr evaluations. */
6407 if (++ctx
->global
->constexpr_ops_count
>= constexpr_ops_limit
)
6411 "%<constexpr%> evaluation operation count exceeds limit of "
6412 "%wd (use %<-fconstexpr-ops-limit=%> to increase the limit)",
6413 constexpr_ops_limit
);
6414 ctx
->global
->constexpr_ops_count
= INTTYPE_MINIMUM (HOST_WIDE_INT
);
6415 *non_constant_p
= true;
6419 constexpr_ctx new_ctx
;
6422 tree_code tcode
= TREE_CODE (t
);
6428 /* We ask for an rvalue for the RESULT_DECL when indirecting
6429 through an invisible reference, or in named return value
6431 if (tree
*p
= ctx
->global
->values
.get (t
))
6436 error ("%qE is not a constant expression", t
);
6437 *non_constant_p
= true;
6442 if (DECL_HAS_VALUE_EXPR_P (t
))
6444 if (is_normal_capture_proxy (t
)
6445 && current_function_decl
== DECL_CONTEXT (t
))
6447 /* Function parms aren't constexpr within the function
6448 definition, so don't try to look at the closure. But if the
6449 captured variable is constant, try to evaluate it directly. */
6450 r
= DECL_CAPTURED_VARIABLE (t
);
6451 tree type
= TREE_TYPE (t
);
6452 if (TYPE_REF_P (type
) != TYPE_REF_P (TREE_TYPE (r
)))
6454 /* Adjust r to match the reference-ness of t. */
6455 if (TYPE_REF_P (type
))
6456 r
= build_address (r
);
6458 r
= convert_from_reference (r
);
6462 r
= DECL_VALUE_EXPR (t
);
6463 return cxx_eval_constant_expression (ctx
, r
, lval
, non_constant_p
,
6468 /* We used to not check lval for CONST_DECL, but darwin.c uses
6469 CONST_DECL for aggregate constants. */
6472 else if (t
== ctx
->object
)
6475 if (tree
*p
= ctx
->global
->values
.get (t
))
6476 if (*p
!= NULL_TREE
)
6481 if (ctx
->manifestly_const_eval
)
6482 maybe_warn_about_constant_value (loc
, t
);
6483 if (COMPLETE_TYPE_P (TREE_TYPE (t
))
6484 && is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
6486 /* If the class is empty, we aren't actually loading anything. */
6487 r
= build_constructor (TREE_TYPE (t
), NULL
);
6488 TREE_CONSTANT (r
) = true;
6490 else if (ctx
->strict
)
6491 r
= decl_really_constant_value (t
, /*unshare_p=*/false);
6493 r
= decl_constant_value (t
, /*unshare_p=*/false);
6494 if (TREE_CODE (r
) == TARGET_EXPR
6495 && TREE_CODE (TARGET_EXPR_INITIAL (r
)) == CONSTRUCTOR
)
6496 r
= TARGET_EXPR_INITIAL (r
);
6500 non_const_var_error (loc
, r
);
6501 *non_constant_p
= true;
6505 case DEBUG_BEGIN_STMT
:
6506 /* ??? It might be nice to retain this information somehow, so
6507 as to be able to step into a constexpr function call. */
6514 case CASE_LABEL_EXPR
:
6519 if (lval
&& !TYPE_REF_P (TREE_TYPE (t
)))
6521 else if (tree
*p
= ctx
->global
->values
.get (r
))
6524 /* Defer in case this is only used for its type. */;
6525 else if (COMPLETE_TYPE_P (TREE_TYPE (t
))
6526 && is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
6528 /* If the class is empty, we aren't actually loading anything. */
6529 r
= build_constructor (TREE_TYPE (t
), NULL
);
6530 TREE_CONSTANT (r
) = true;
6535 error ("%qE is not a constant expression", t
);
6536 *non_constant_p
= true;
6541 case AGGR_INIT_EXPR
:
6542 r
= cxx_eval_call_expression (ctx
, t
, lval
,
6543 non_constant_p
, overflow_p
);
6548 r
= DECL_EXPR_DECL (t
);
6549 if (TREE_CODE (r
) == USING_DECL
)
6556 && (TREE_STATIC (r
) || CP_DECL_THREAD_LOCAL_P (r
))
6557 /* Allow __FUNCTION__ etc. */
6558 && !DECL_ARTIFICIAL (r
))
6560 gcc_assert (cxx_dialect
>= cxx23
);
6563 if (CP_DECL_THREAD_LOCAL_P (r
))
6564 error_at (loc
, "control passes through declaration of %qD "
6565 "with thread storage duration", r
);
6567 error_at (loc
, "control passes through declaration of %qD "
6568 "with static storage duration", r
);
6570 *non_constant_p
= true;
6574 if (AGGREGATE_TYPE_P (TREE_TYPE (r
))
6575 || VECTOR_TYPE_P (TREE_TYPE (r
)))
6579 new_ctx
.ctor
= build_constructor (TREE_TYPE (r
), NULL
);
6580 CONSTRUCTOR_NO_CLEARING (new_ctx
.ctor
) = true;
6581 ctx
->global
->values
.put (r
, new_ctx
.ctor
);
6585 if (tree init
= DECL_INITIAL (r
))
6587 init
= cxx_eval_constant_expression (ctx
, init
,
6589 non_constant_p
, overflow_p
);
6590 /* Don't share a CONSTRUCTOR that might be changed. */
6591 init
= unshare_constructor (init
);
6592 /* Remember that a constant object's constructor has already
6594 if (CLASS_TYPE_P (TREE_TYPE (r
))
6595 && CP_TYPE_CONST_P (TREE_TYPE (r
)))
6596 TREE_READONLY (init
) = true;
6597 ctx
->global
->values
.put (r
, init
);
6599 else if (ctx
== &new_ctx
)
6600 /* We gave it a CONSTRUCTOR above. */;
6602 ctx
->global
->values
.put (r
, NULL_TREE
);
6608 tree type
= TREE_TYPE (t
);
6610 if (!literal_type_p (type
))
6614 auto_diagnostic_group d
;
6615 error ("temporary of non-literal type %qT in a "
6616 "constant expression", type
);
6617 explain_non_literal_class (type
);
6619 *non_constant_p
= true;
6622 gcc_checking_assert (!TARGET_EXPR_DIRECT_INIT_P (t
));
6623 /* Avoid evaluating a TARGET_EXPR more than once. */
6624 tree slot
= TARGET_EXPR_SLOT (t
);
6625 if (tree
*p
= ctx
->global
->values
.get (slot
))
6632 if ((AGGREGATE_TYPE_P (type
) || VECTOR_TYPE_P (type
)))
6634 /* We're being expanded without an explicit target, so start
6635 initializing a new object; expansion with an explicit target
6636 strips the TARGET_EXPR before we get here. */
6638 /* Link CTX to NEW_CTX so that lookup_placeholder can resolve
6639 any PLACEHOLDER_EXPR within the initializer that refers to the
6640 former object under construction. */
6641 new_ctx
.parent
= ctx
;
6642 new_ctx
.ctor
= build_constructor (type
, NULL
);
6643 CONSTRUCTOR_NO_CLEARING (new_ctx
.ctor
) = true;
6644 new_ctx
.object
= slot
;
6645 ctx
->global
->values
.put (new_ctx
.object
, new_ctx
.ctor
);
6648 /* Pass false for 'lval' because this indicates
6649 initialization of a temporary. */
6650 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
6652 non_constant_p
, overflow_p
);
6653 if (*non_constant_p
)
6655 /* Adjust the type of the result to the type of the temporary. */
6656 r
= adjust_temp_type (type
, r
);
6657 if (TARGET_EXPR_CLEANUP (t
) && !CLEANUP_EH_ONLY (t
))
6658 ctx
->global
->cleanups
->safe_push (TARGET_EXPR_CLEANUP (t
));
6659 r
= unshare_constructor (r
);
6660 ctx
->global
->values
.put (slot
, r
);
6661 if (ctx
->save_exprs
)
6662 ctx
->save_exprs
->safe_push (slot
);
6670 gcc_assert (jump_target
== NULL
|| *jump_target
== NULL_TREE
);
6671 r
= cxx_eval_store_expression (ctx
, t
, lval
,
6672 non_constant_p
, overflow_p
);
6676 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
6678 non_constant_p
, overflow_p
);
6682 if (TREE_OPERAND (t
, 0) != NULL_TREE
)
6683 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
6685 non_constant_p
, overflow_p
);
6693 /* Can happen with ({ return true; }) && false; passed to
6694 maybe_constant_value. There is nothing to jump over in this
6695 case, and the bug will be diagnosed later. */
6696 gcc_assert (ctx
->quiet
);
6697 *non_constant_p
= true;
6702 /* Avoid evaluating a SAVE_EXPR more than once. */
6703 if (tree
*p
= ctx
->global
->values
.get (t
))
6707 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), false,
6708 non_constant_p
, overflow_p
);
6709 if (*non_constant_p
)
6711 ctx
->global
->values
.put (t
, r
);
6712 if (ctx
->save_exprs
)
6713 ctx
->save_exprs
->safe_push (t
);
6717 case TRY_CATCH_EXPR
:
6718 if (TREE_OPERAND (t
, 0) == NULL_TREE
)
6724 case NON_LVALUE_EXPR
:
6726 case MUST_NOT_THROW_EXPR
:
6729 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
6731 non_constant_p
, overflow_p
,
6735 case CLEANUP_POINT_EXPR
:
6737 auto_vec
<tree
, 2> cleanups
;
6738 vec
<tree
> *prev_cleanups
= ctx
->global
->cleanups
;
6739 ctx
->global
->cleanups
= &cleanups
;
6740 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
6742 non_constant_p
, overflow_p
,
6744 ctx
->global
->cleanups
= prev_cleanups
;
6747 /* Evaluate the cleanups. */
6748 FOR_EACH_VEC_ELT_REVERSE (cleanups
, i
, cleanup
)
6749 cxx_eval_constant_expression (ctx
, cleanup
, false,
6750 non_constant_p
, overflow_p
);
6754 case TRY_FINALLY_EXPR
:
6755 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), lval
,
6756 non_constant_p
, overflow_p
,
6758 if (!*non_constant_p
)
6759 /* Also evaluate the cleanup. */
6760 cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1), true,
6761 non_constant_p
, overflow_p
);
6765 r
= cxx_eval_constant_expression (ctx
, CLEANUP_BODY (t
), lval
,
6766 non_constant_p
, overflow_p
,
6768 if (!CLEANUP_EH_ONLY (t
) && !*non_constant_p
)
6770 iloc_sentinel
ils (loc
);
6771 /* Also evaluate the cleanup. */
6772 cxx_eval_constant_expression (ctx
, CLEANUP_EXPR (t
), true,
6773 non_constant_p
, overflow_p
);
6777 /* These differ from cxx_eval_unary_expression in that this doesn't
6778 check for a constant operand or result; an address can be
6779 constant without its operand being, and vice versa. */
6782 r
= cxx_eval_indirect_ref (ctx
, t
, lval
,
6783 non_constant_p
, overflow_p
);
6788 tree oldop
= TREE_OPERAND (t
, 0);
6789 tree op
= cxx_eval_constant_expression (ctx
, oldop
,
6791 non_constant_p
, overflow_p
);
6792 /* Don't VERIFY_CONSTANT here. */
6793 if (*non_constant_p
)
6795 gcc_checking_assert (TREE_CODE (op
) != CONSTRUCTOR
);
6796 /* This function does more aggressive folding than fold itself. */
6797 r
= build_fold_addr_expr_with_type (op
, TREE_TYPE (t
));
6798 if (TREE_CODE (r
) == ADDR_EXPR
&& TREE_OPERAND (r
, 0) == oldop
)
6810 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), lval
,
6811 non_constant_p
, overflow_p
);
6812 if (r
== error_mark_node
)
6814 else if (r
== TREE_OPERAND (t
, 0))
6817 r
= fold_build1 (TREE_CODE (t
), TREE_TYPE (t
), r
);
6822 case FIX_TRUNC_EXPR
:
6828 case TRUTH_NOT_EXPR
:
6829 case FIXED_CONVERT_EXPR
:
6830 r
= cxx_eval_unary_expression (ctx
, t
, lval
,
6831 non_constant_p
, overflow_p
);
6835 r
= fold_sizeof_expr (t
);
6836 /* In a template, fold_sizeof_expr may merely create a new SIZEOF_EXPR,
6837 which could lead to an infinite recursion. */
6838 if (TREE_CODE (r
) != SIZEOF_EXPR
)
6839 r
= cxx_eval_constant_expression (ctx
, r
, lval
,
6840 non_constant_p
, overflow_p
,
6844 *non_constant_p
= true;
6845 gcc_assert (ctx
->quiet
);
6852 /* check_return_expr sometimes wraps a TARGET_EXPR in a
6853 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
6854 introduced by build_call_a. */
6855 tree op0
= TREE_OPERAND (t
, 0);
6856 tree op1
= TREE_OPERAND (t
, 1);
6858 if ((TREE_CODE (op0
) == TARGET_EXPR
&& op1
== TARGET_EXPR_SLOT (op0
))
6859 || TREE_CODE (op1
) == EMPTY_CLASS_EXPR
)
6860 r
= cxx_eval_constant_expression (ctx
, op0
,
6861 lval
, non_constant_p
, overflow_p
,
6865 /* Check that the LHS is constant and then discard it. */
6866 cxx_eval_constant_expression (ctx
, op0
,
6867 true, non_constant_p
, overflow_p
,
6869 if (*non_constant_p
)
6871 op1
= TREE_OPERAND (t
, 1);
6872 r
= cxx_eval_constant_expression (ctx
, op1
,
6873 lval
, non_constant_p
, overflow_p
,
6879 case POINTER_PLUS_EXPR
:
6880 case POINTER_DIFF_EXPR
:
6884 case TRUNC_DIV_EXPR
:
6886 case FLOOR_DIV_EXPR
:
6887 case ROUND_DIV_EXPR
:
6888 case TRUNC_MOD_EXPR
:
6890 case ROUND_MOD_EXPR
:
6892 case EXACT_DIV_EXPR
:
6902 case TRUTH_XOR_EXPR
:
6909 case SPACESHIP_EXPR
:
6910 case UNORDERED_EXPR
:
6920 r
= cxx_eval_binary_expression (ctx
, t
, lval
,
6921 non_constant_p
, overflow_p
);
6924 /* fold can introduce non-IF versions of these; still treat them as
6925 short-circuiting. */
6926 case TRUTH_AND_EXPR
:
6927 case TRUTH_ANDIF_EXPR
:
6928 r
= cxx_eval_logical_expression (ctx
, t
, boolean_false_node
,
6931 non_constant_p
, overflow_p
);
6935 case TRUTH_ORIF_EXPR
:
6936 r
= cxx_eval_logical_expression (ctx
, t
, boolean_true_node
,
6939 non_constant_p
, overflow_p
);
6943 r
= cxx_eval_array_reference (ctx
, t
, lval
,
6944 non_constant_p
, overflow_p
);
6948 if (is_overloaded_fn (t
))
6950 /* We can only get here in checking mode via
6951 build_non_dependent_expr, because any expression that
6952 calls or takes the address of the function will have
6953 pulled a FUNCTION_DECL out of the COMPONENT_REF. */
6954 gcc_checking_assert (ctx
->quiet
|| errorcount
);
6955 *non_constant_p
= true;
6958 r
= cxx_eval_component_reference (ctx
, t
, lval
,
6959 non_constant_p
, overflow_p
);
6963 r
= cxx_eval_bit_field_ref (ctx
, t
, lval
,
6964 non_constant_p
, overflow_p
);
6969 if (jump_target
&& *jump_target
)
6971 tree orig_jump
= *jump_target
;
6972 tree arg
= ((TREE_CODE (t
) != IF_STMT
|| TREE_OPERAND (t
, 1))
6973 ? TREE_OPERAND (t
, 1) : void_node
);
6974 /* When jumping to a label, the label might be either in the
6975 then or else blocks, so process then block first in skipping
6976 mode first, and if we are still in the skipping mode at its end,
6977 process the else block too. */
6978 r
= cxx_eval_constant_expression (ctx
, arg
, lval
, non_constant_p
,
6979 overflow_p
, jump_target
);
6980 /* It's possible that we found the label in the then block. But
6981 it could have been followed by another jumping statement, e.g.
6982 say we're looking for case 1:
6985 // skipped statements
6986 case 1:; // clears up *jump_target
6987 return 1; // and sets it to a RETURN_EXPR
6990 in which case we need not go looking to the else block.
6991 (goto is not allowed in a constexpr function.) */
6992 if (*jump_target
== orig_jump
)
6994 arg
= ((TREE_CODE (t
) != IF_STMT
|| TREE_OPERAND (t
, 2))
6995 ? TREE_OPERAND (t
, 2) : void_node
);
6996 r
= cxx_eval_constant_expression (ctx
, arg
, lval
, non_constant_p
,
6997 overflow_p
, jump_target
);
7001 r
= cxx_eval_conditional_expression (ctx
, t
, lval
,
7002 non_constant_p
, overflow_p
,
7006 r
= cxx_eval_vector_conditional_expression (ctx
, t
, non_constant_p
,
7011 if (TREE_CONSTANT (t
) && reduced_constant_expression_p (t
))
7013 /* Don't re-process a constant CONSTRUCTOR, but do fold it to
7014 VECTOR_CST if applicable. */
7015 verify_constructor_flags (t
);
7016 if (TREE_CONSTANT (t
))
7019 r
= cxx_eval_bare_aggregate (ctx
, t
, lval
,
7020 non_constant_p
, overflow_p
);
7024 /* We can get this in a defaulted constructor for a class with a
7025 non-static data member of array type. Either the initializer will
7026 be NULL, meaning default-initialization, or it will be an lvalue
7027 or xvalue of the same type, meaning direct-initialization from the
7028 corresponding member. */
7029 r
= cxx_eval_vec_init (ctx
, t
, lval
,
7030 non_constant_p
, overflow_p
);
7034 r
= cxx_eval_trinary_expression (ctx
, t
, lval
,
7035 non_constant_p
, overflow_p
);
7039 gcc_assert (!REF_PARENTHESIZED_P (t
));
7040 /* A PAREN_EXPR resulting from __builtin_assoc_barrier has no effect in
7041 constant expressions since it's unaffected by -fassociative-math. */
7042 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), lval
,
7043 non_constant_p
, overflow_p
);
7047 if (REINTERPRET_CAST_P (t
))
7051 "%<reinterpret_cast%> is not a constant expression");
7052 *non_constant_p
= true;
7057 case VIEW_CONVERT_EXPR
:
7058 case UNARY_PLUS_EXPR
:
7060 tree oldop
= TREE_OPERAND (t
, 0);
7062 tree op
= cxx_eval_constant_expression (ctx
, oldop
,
7064 non_constant_p
, overflow_p
);
7065 if (*non_constant_p
)
7067 tree type
= TREE_TYPE (t
);
7069 if (VOID_TYPE_P (type
))
7072 if (TREE_CODE (t
) == CONVERT_EXPR
7073 && ARITHMETIC_TYPE_P (type
)
7074 && INDIRECT_TYPE_P (TREE_TYPE (op
))
7075 && ctx
->manifestly_const_eval
)
7079 "conversion from pointer type %qT to arithmetic type "
7080 "%qT in a constant expression", TREE_TYPE (op
), type
);
7081 *non_constant_p
= true;
7085 /* [expr.const]: a conversion from type cv void* to a pointer-to-object
7086 type cannot be part of a core constant expression as a resolution to
7088 if (TYPE_PTROB_P (type
)
7089 && TYPE_PTR_P (TREE_TYPE (op
))
7090 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (op
)))
7091 /* Inside a call to std::construct_at or to
7092 std::allocator<T>::{,de}allocate, we permit casting from void*
7093 because that is compiler-generated code. */
7094 && !is_std_construct_at (ctx
->call
)
7095 && !is_std_allocator_allocate (ctx
->call
))
7097 /* Likewise, don't error when casting from void* when OP is
7098 &heap uninit and similar. */
7099 tree sop
= tree_strip_nop_conversions (op
);
7100 if (TREE_CODE (sop
) == ADDR_EXPR
7101 && VAR_P (TREE_OPERAND (sop
, 0))
7102 && DECL_ARTIFICIAL (TREE_OPERAND (sop
, 0)))
7107 error_at (loc
, "cast from %qT is not allowed",
7109 *non_constant_p
= true;
7114 if (TREE_CODE (op
) == PTRMEM_CST
&& !TYPE_PTRMEM_P (type
))
7115 op
= cplus_expand_constant (op
);
7117 if (TREE_CODE (op
) == PTRMEM_CST
&& tcode
== NOP_EXPR
)
7119 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (op
))
7120 && !can_convert_qual (type
, op
))
7121 op
= cplus_expand_constant (op
);
7122 return cp_fold_convert (type
, op
);
7125 if (INDIRECT_TYPE_P (type
) && TREE_CODE (op
) == INTEGER_CST
)
7127 if (integer_zerop (op
))
7129 if (TYPE_REF_P (type
))
7132 error_at (loc
, "dereferencing a null pointer");
7133 *non_constant_p
= true;
7139 /* This detects for example:
7140 reinterpret_cast<void*>(sizeof 0)
7143 error_at (loc
, "%<reinterpret_cast<%T>(%E)%> is not "
7144 "a constant expression",
7146 *non_constant_p
= true;
7151 if (INDIRECT_TYPE_P (type
)
7152 && TREE_CODE (op
) == NOP_EXPR
7153 && TREE_TYPE (op
) == ptr_type_node
7154 && TREE_CODE (TREE_OPERAND (op
, 0)) == ADDR_EXPR
7155 && VAR_P (TREE_OPERAND (TREE_OPERAND (op
, 0), 0))
7156 && (DECL_NAME (TREE_OPERAND (TREE_OPERAND (op
, 0),
7157 0)) == heap_uninit_identifier
7158 || DECL_NAME (TREE_OPERAND (TREE_OPERAND (op
, 0),
7159 0)) == heap_vec_uninit_identifier
))
7161 tree var
= TREE_OPERAND (TREE_OPERAND (op
, 0), 0);
7162 tree var_size
= TYPE_SIZE_UNIT (TREE_TYPE (var
));
7163 tree elt_type
= TREE_TYPE (type
);
7164 tree cookie_size
= NULL_TREE
;
7165 if (TREE_CODE (elt_type
) == RECORD_TYPE
7166 && TYPE_NAME (elt_type
) == heap_identifier
)
7168 tree fld1
= TYPE_FIELDS (elt_type
);
7169 tree fld2
= DECL_CHAIN (fld1
);
7170 elt_type
= TREE_TYPE (TREE_TYPE (fld2
));
7171 cookie_size
= TYPE_SIZE_UNIT (TREE_TYPE (fld1
));
7174 = (DECL_NAME (var
) == heap_uninit_identifier
7175 ? heap_identifier
: heap_vec_identifier
);
7177 = build_new_constexpr_heap_type (elt_type
, cookie_size
,
7179 TREE_TYPE (TREE_OPERAND (op
, 0))
7180 = build_pointer_type (TREE_TYPE (var
));
7183 if (op
== oldop
&& tcode
!= UNARY_PLUS_EXPR
)
7184 /* We didn't fold at the top so we could check for ptr-int
7190 /* Handle an array's bounds having been deduced after we built
7191 the wrapping expression. */
7192 if (same_type_ignoring_tlq_and_bounds_p (type
, TREE_TYPE (op
)))
7194 else if (sop
= tree_strip_nop_conversions (op
),
7195 sop
!= op
&& (same_type_ignoring_tlq_and_bounds_p
7196 (type
, TREE_TYPE (sop
))))
7198 else if (tcode
== UNARY_PLUS_EXPR
)
7199 r
= fold_convert (TREE_TYPE (t
), op
);
7201 r
= fold_build1 (tcode
, type
, op
);
7203 /* Conversion of an out-of-range value has implementation-defined
7204 behavior; the language considers it different from arithmetic
7205 overflow, which is undefined. */
7206 if (TREE_OVERFLOW_P (r
) && !TREE_OVERFLOW_P (op
))
7207 TREE_OVERFLOW (r
) = false;
7211 case EMPTY_CLASS_EXPR
:
7212 /* Handle EMPTY_CLASS_EXPR produced by build_call_a by lowering
7213 it to an appropriate CONSTRUCTOR. */
7214 return build_constructor (TREE_TYPE (t
), NULL
);
7216 case STATEMENT_LIST
:
7218 new_ctx
.ctor
= new_ctx
.object
= NULL_TREE
;
7219 return cxx_eval_statement_list (&new_ctx
, t
,
7220 non_constant_p
, overflow_p
, jump_target
);
7223 return cxx_eval_constant_expression (ctx
, BIND_EXPR_BODY (t
),
7225 non_constant_p
, overflow_p
,
7228 case PREINCREMENT_EXPR
:
7229 case POSTINCREMENT_EXPR
:
7230 case PREDECREMENT_EXPR
:
7231 case POSTDECREMENT_EXPR
:
7232 return cxx_eval_increment_expression (ctx
, t
,
7233 lval
, non_constant_p
, overflow_p
);
7239 case VEC_DELETE_EXPR
:
7242 /* GCC internal stuff. */
7244 case NON_DEPENDENT_EXPR
:
7248 error_at (loc
, "expression %qE is not a constant expression", t
);
7249 *non_constant_p
= true;
7253 /* Virtual function lookup. We don't need to do anything fancy. */
7254 return cxx_eval_constant_expression (ctx
, OBJ_TYPE_REF_EXPR (t
),
7255 lval
, non_constant_p
, overflow_p
);
7257 case PLACEHOLDER_EXPR
:
7258 /* Use of the value or address of the current object. */
7259 if (tree ctor
= lookup_placeholder (ctx
, lval
, TREE_TYPE (t
)))
7261 if (TREE_CODE (ctor
) == CONSTRUCTOR
)
7264 return cxx_eval_constant_expression (ctx
, ctor
, lval
,
7265 non_constant_p
, overflow_p
);
7267 /* A placeholder without a referent. We can get here when
7268 checking whether NSDMIs are noexcept, or in massage_init_elt;
7269 just say it's non-constant for now. */
7270 gcc_assert (ctx
->quiet
);
7271 *non_constant_p
= true;
7276 tree cond
= TREE_OPERAND (t
, 0);
7277 cond
= cxx_eval_constant_expression (ctx
, cond
, /*lval*/false,
7278 non_constant_p
, overflow_p
);
7279 VERIFY_CONSTANT (cond
);
7280 if (integer_nonzerop (cond
))
7286 if (breaks (&TREE_OPERAND (t
, 0))
7287 || continues (&TREE_OPERAND (t
, 0))
7288 /* Allow for jumping to a cdtor_label. */
7289 || returns (&TREE_OPERAND (t
, 0)))
7290 *jump_target
= TREE_OPERAND (t
, 0);
7293 gcc_assert (cxx_dialect
>= cxx23
);
7295 error_at (loc
, "%<goto%> is not a constant expression");
7296 *non_constant_p
= true;
7304 cxx_eval_loop_expr (ctx
, t
,
7305 non_constant_p
, overflow_p
, jump_target
);
7310 cxx_eval_switch_expr (ctx
, t
,
7311 non_constant_p
, overflow_p
, jump_target
);
7315 /* It's possible to get a requires-expression in a constant
7316 expression. For example:
7318 template<typename T> concept bool C() {
7319 return requires (T t) { t; };
7322 template<typename T> requires !C<T>() void f(T);
7324 Normalization leaves f with the associated constraint
7325 '!requires (T t) { ... }' which is not transformed into
7327 if (!processing_template_decl
)
7328 return evaluate_requires_expr (t
);
7330 *non_constant_p
= true;
7334 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
7336 non_constant_p
, overflow_p
,
7344 case TEMPLATE_ID_EXPR
:
7346 /* We can evaluate template-id that refers to a concept only if
7347 the template arguments are non-dependent. */
7348 tree id
= unpack_concept_check (t
);
7349 tree tmpl
= TREE_OPERAND (id
, 0);
7350 if (!concept_definition_p (tmpl
))
7351 internal_error ("unexpected template-id %qE", t
);
7353 if (function_concept_p (tmpl
))
7356 error_at (cp_expr_loc_or_input_loc (t
),
7357 "function concept must be called");
7358 r
= error_mark_node
;
7362 if (!value_dependent_expression_p (t
)
7363 && !uid_sensitive_constexpr_evaluation_p ())
7364 r
= evaluate_concept_check (t
);
7366 *non_constant_p
= true;
7373 inline_asm_in_constexpr_error (loc
);
7374 *non_constant_p
= true;
7381 error_at (EXPR_LOCATION (t
),
7382 "address of a call to %qs is not a constant expression",
7383 "__builtin_bit_cast");
7384 *non_constant_p
= true;
7387 r
= cxx_eval_bit_cast (ctx
, t
, non_constant_p
, overflow_p
);
7391 if (STATEMENT_CODE_P (TREE_CODE (t
)))
7393 /* This function doesn't know how to deal with pre-genericize
7394 statements; this can only happen with statement-expressions,
7395 so for now just fail. */
7397 error_at (EXPR_LOCATION (t
),
7398 "statement is not a constant expression");
7401 internal_error ("unexpected expression %qE of kind %s", t
,
7402 get_tree_code_name (TREE_CODE (t
)));
7403 *non_constant_p
= true;
7407 if (r
== error_mark_node
)
7408 *non_constant_p
= true;
7410 if (*non_constant_p
)
7416 /* P0859: A function is needed for constant evaluation if it is a constexpr
7417 function that is named by an expression ([basic.def.odr]) that is
7418 potentially constant evaluated.
7420 So we need to instantiate any constexpr functions mentioned by the
7421 expression even if the definition isn't needed for evaluating the
7425 instantiate_cx_fn_r (tree
*tp
, int *walk_subtrees
, void */
*data*/
)
7427 if (TREE_CODE (*tp
) == FUNCTION_DECL
7428 && DECL_DECLARED_CONSTEXPR_P (*tp
)
7429 && !DECL_INITIAL (*tp
)
7430 && !trivial_fn_p (*tp
)
7431 && DECL_TEMPLOID_INSTANTIATION (*tp
)
7432 && !uid_sensitive_constexpr_evaluation_p ())
7435 instantiate_decl (*tp
, /*defer_ok*/false, /*expl_inst*/false);
7438 else if (TREE_CODE (*tp
) == CALL_EXPR
7439 || TREE_CODE (*tp
) == AGGR_INIT_EXPR
)
7441 if (EXPR_HAS_LOCATION (*tp
))
7442 input_location
= EXPR_LOCATION (*tp
);
7452 instantiate_constexpr_fns (tree t
)
7454 location_t loc
= input_location
;
7455 cp_walk_tree_without_duplicates (&t
, instantiate_cx_fn_r
, NULL
);
7456 input_location
= loc
;
7459 /* Look for heap variables in the expression *TP. */
7462 find_heap_var_refs (tree
*tp
, int *walk_subtrees
, void */
*data*/
)
7465 && (DECL_NAME (*tp
) == heap_uninit_identifier
7466 || DECL_NAME (*tp
) == heap_identifier
7467 || DECL_NAME (*tp
) == heap_vec_uninit_identifier
7468 || DECL_NAME (*tp
) == heap_vec_identifier
7469 || DECL_NAME (*tp
) == heap_deleted_identifier
))
7477 /* Find immediate function decls in *TP if any. */
7480 find_immediate_fndecl (tree
*tp
, int */
*walk_subtrees*/
, void */
*data*/
)
7482 if (TREE_CODE (*tp
) == FUNCTION_DECL
&& DECL_IMMEDIATE_FUNCTION_P (*tp
))
7484 if (TREE_CODE (*tp
) == PTRMEM_CST
7485 && TREE_CODE (PTRMEM_CST_MEMBER (*tp
)) == FUNCTION_DECL
7486 && DECL_IMMEDIATE_FUNCTION_P (PTRMEM_CST_MEMBER (*tp
)))
7487 return PTRMEM_CST_MEMBER (*tp
);
7491 /* ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
7492 STRICT has the same sense as for constant_value_1: true if we only allow
7493 conforming C++ constant expressions, or false if we want a constant value
7494 even if it doesn't conform.
7495 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
7496 per P0595 even when ALLOW_NON_CONSTANT is true.
7497 CONSTEXPR_DTOR is true when evaluating the dtor of a constexpr variable.
7498 OBJECT must be non-NULL in that case. */
7501 cxx_eval_outermost_constant_expr (tree t
, bool allow_non_constant
,
7503 bool manifestly_const_eval
= false,
7504 bool constexpr_dtor
= false,
7505 tree object
= NULL_TREE
)
7507 auto_timevar
time (TV_CONSTEXPR
);
7509 bool non_constant_p
= false;
7510 bool overflow_p
= false;
7512 if (BRACE_ENCLOSED_INITIALIZER_P (t
))
7514 gcc_checking_assert (allow_non_constant
);
7518 constexpr_global_ctx global_ctx
;
7519 constexpr_ctx ctx
= { &global_ctx
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
7520 allow_non_constant
, strict
,
7521 manifestly_const_eval
|| !allow_non_constant
};
7523 /* Turn off -frounding-math for manifestly constant evaluation. */
7524 warning_sentinel
rm (flag_rounding_math
, ctx
.manifestly_const_eval
);
7525 tree type
= initialized_type (t
);
7527 bool is_consteval
= false;
7528 if (VOID_TYPE_P (type
))
7531 /* Used for destructors of array elements. */
7532 type
= TREE_TYPE (object
);
7535 if (cxx_dialect
< cxx20
)
7537 if (TREE_CODE (t
) != CALL_EXPR
&& TREE_CODE (t
) != AGGR_INIT_EXPR
)
7539 /* Calls to immediate functions returning void need to be
7541 tree fndecl
= cp_get_callee_fndecl_nofold (t
);
7542 if (fndecl
== NULL_TREE
|| !DECL_IMMEDIATE_FUNCTION_P (fndecl
))
7545 is_consteval
= true;
7548 else if (cxx_dialect
>= cxx20
7549 && (TREE_CODE (t
) == CALL_EXPR
7550 || TREE_CODE (t
) == AGGR_INIT_EXPR
7551 || TREE_CODE (t
) == TARGET_EXPR
))
7553 /* For non-concept checks, determine if it is consteval. */
7554 if (!concept_check_p (t
))
7557 if (TREE_CODE (x
) == TARGET_EXPR
)
7558 x
= TARGET_EXPR_INITIAL (x
);
7559 tree fndecl
= cp_get_callee_fndecl_nofold (x
);
7560 if (fndecl
&& DECL_IMMEDIATE_FUNCTION_P (fndecl
))
7561 is_consteval
= true;
7564 if (AGGREGATE_TYPE_P (type
) || VECTOR_TYPE_P (type
))
7566 /* In C++14 an NSDMI can participate in aggregate initialization,
7567 and can refer to the address of the object being initialized, so
7568 we need to pass in the relevant VAR_DECL if we want to do the
7569 evaluation in a single pass. The evaluation will dynamically
7570 update ctx.values for the VAR_DECL. We use the same strategy
7571 for C++11 constexpr constructors that refer to the object being
7575 gcc_assert (object
&& VAR_P (object
));
7576 gcc_assert (DECL_DECLARED_CONSTEXPR_P (object
));
7577 gcc_assert (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object
));
7578 if (error_operand_p (DECL_INITIAL (object
)))
7580 ctx
.ctor
= unshare_expr (DECL_INITIAL (object
));
7581 TREE_READONLY (ctx
.ctor
) = false;
7582 /* Temporarily force decl_really_constant_value to return false
7583 for it, we want to use ctx.ctor for the current value instead. */
7584 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object
) = false;
7588 ctx
.ctor
= build_constructor (type
, NULL
);
7589 CONSTRUCTOR_NO_CLEARING (ctx
.ctor
) = true;
7593 if (TREE_CODE (t
) == TARGET_EXPR
)
7594 object
= TARGET_EXPR_SLOT (t
);
7595 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
)
7596 object
= AGGR_INIT_EXPR_SLOT (t
);
7598 ctx
.object
= object
;
7600 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7601 (type
, TREE_TYPE (object
)));
7602 if (object
&& DECL_P (object
))
7603 global_ctx
.values
.put (object
, ctx
.ctor
);
7604 if (TREE_CODE (r
) == TARGET_EXPR
)
7605 /* Avoid creating another CONSTRUCTOR when we expand the
7607 r
= TARGET_EXPR_INITIAL (r
);
7610 auto_vec
<tree
, 16> cleanups
;
7611 global_ctx
.cleanups
= &cleanups
;
7613 if (manifestly_const_eval
)
7614 instantiate_constexpr_fns (r
);
7615 r
= cxx_eval_constant_expression (&ctx
, r
,
7616 false, &non_constant_p
, &overflow_p
);
7618 if (!constexpr_dtor
)
7619 verify_constant (r
, allow_non_constant
, &non_constant_p
, &overflow_p
);
7621 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object
) = true;
7625 /* Evaluate the cleanups. */
7626 FOR_EACH_VEC_ELT_REVERSE (cleanups
, i
, cleanup
)
7627 cxx_eval_constant_expression (&ctx
, cleanup
, false,
7628 &non_constant_p
, &overflow_p
);
7630 /* Mutable logic is a bit tricky: we want to allow initialization of
7631 constexpr variables with mutable members, but we can't copy those
7632 members to another constexpr variable. */
7633 if (TREE_CODE (r
) == CONSTRUCTOR
&& CONSTRUCTOR_MUTABLE_POISON (r
))
7635 if (!allow_non_constant
)
7636 error ("%qE is not a constant expression because it refers to "
7637 "mutable subobjects of %qT", t
, type
);
7638 non_constant_p
= true;
7641 if (TREE_CODE (r
) == CONSTRUCTOR
&& CONSTRUCTOR_NO_CLEARING (r
))
7643 if (!allow_non_constant
)
7644 error ("%qE is not a constant expression because it refers to "
7645 "an incompletely initialized variable", t
);
7646 TREE_CONSTANT (r
) = false;
7647 non_constant_p
= true;
7650 if (!global_ctx
.heap_vars
.is_empty ())
7652 tree heap_var
= cp_walk_tree_without_duplicates (&r
, find_heap_var_refs
,
7657 if (!allow_non_constant
&& !non_constant_p
)
7658 error_at (DECL_SOURCE_LOCATION (heap_var
),
7659 "%qE is not a constant expression because it refers to "
7660 "a result of %<operator new%>", t
);
7662 non_constant_p
= true;
7664 FOR_EACH_VEC_ELT (global_ctx
.heap_vars
, i
, heap_var
)
7666 if (DECL_NAME (heap_var
) != heap_deleted_identifier
)
7668 if (!allow_non_constant
&& !non_constant_p
)
7669 error_at (DECL_SOURCE_LOCATION (heap_var
),
7670 "%qE is not a constant expression because allocated "
7671 "storage has not been deallocated", t
);
7673 non_constant_p
= true;
7675 varpool_node::get (heap_var
)->remove ();
7679 /* Check that immediate invocation does not return an expression referencing
7680 any immediate function decls. */
7681 if (is_consteval
|| in_immediate_context ())
7682 if (tree immediate_fndecl
7683 = cp_walk_tree_without_duplicates (&r
, find_immediate_fndecl
,
7686 if (!allow_non_constant
&& !non_constant_p
)
7687 error_at (cp_expr_loc_or_input_loc (t
),
7688 "immediate evaluation returns address of immediate "
7689 "function %qD", immediate_fndecl
);
7691 non_constant_p
= true;
7695 /* If we saw something bad, go back to our argument. The wrapping below is
7696 only for the cases of TREE_CONSTANT argument or overflow. */
7699 if (!non_constant_p
&& overflow_p
)
7700 non_constant_p
= true;
7702 /* Unshare the result. */
7703 bool should_unshare
= true;
7704 if (r
== t
|| (TREE_CODE (t
) == TARGET_EXPR
7705 && TARGET_EXPR_INITIAL (t
) == r
))
7706 should_unshare
= false;
7708 if (non_constant_p
&& !allow_non_constant
)
7709 return error_mark_node
;
7710 else if (constexpr_dtor
)
7712 else if (non_constant_p
&& TREE_CONSTANT (r
))
7714 /* This isn't actually constant, so unset TREE_CONSTANT.
7715 Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires
7716 it to be set if it is invariant address, even when it is not
7717 a valid C++ constant expression. Wrap it with a NOP_EXPR
7719 if (EXPR_P (r
) && TREE_CODE (r
) != ADDR_EXPR
)
7721 else if (TREE_CODE (r
) == CONSTRUCTOR
)
7722 r
= build1 (VIEW_CONVERT_EXPR
, TREE_TYPE (r
), r
);
7724 r
= build_nop (TREE_TYPE (r
), r
);
7725 TREE_CONSTANT (r
) = false;
7727 else if (non_constant_p
)
7731 r
= unshare_expr (r
);
7733 if (TREE_CODE (r
) == CONSTRUCTOR
&& CLASS_TYPE_P (TREE_TYPE (r
)))
7735 r
= adjust_temp_type (type
, r
);
7736 if (TREE_CODE (t
) == TARGET_EXPR
7737 && TARGET_EXPR_INITIAL (t
) == r
)
7739 else if (TREE_CODE (t
) != CONSTRUCTOR
)
7741 r
= get_target_expr_sfinae (r
, tf_warning_or_error
| tf_no_cleanup
);
7742 TREE_CONSTANT (r
) = true;
7746 /* Remember the original location if that wouldn't need a wrapper. */
7747 if (location_t loc
= EXPR_LOCATION (t
))
7748 protected_set_expr_location (r
, loc
);
7753 /* If T represents a constant expression returns its reduced value.
7754 Otherwise return error_mark_node. If T is dependent, then
7758 cxx_constant_value (tree t
, tree decl
)
7760 return cxx_eval_outermost_constant_expr (t
, false, true, true, false, decl
);
7763 /* As above, but respect SFINAE. */
7766 cxx_constant_value_sfinae (tree t
, tsubst_flags_t complain
)
7768 bool sfinae
= !(complain
& tf_error
);
7769 tree r
= cxx_eval_outermost_constant_expr (t
, sfinae
, true, true);
7770 if (sfinae
&& !TREE_CONSTANT (r
))
7771 r
= error_mark_node
;
7775 /* Like cxx_constant_value, but used for evaluation of constexpr destructors
7776 of constexpr variables. The actual initializer of DECL is not modified. */
7779 cxx_constant_dtor (tree t
, tree decl
)
7781 cxx_eval_outermost_constant_expr (t
, false, true, true, true, decl
);
7784 /* Helper routine for fold_simple function. Either return simplified
7785 expression T, otherwise NULL_TREE.
7786 In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold
7787 even if we are within template-declaration. So be careful on call, as in
7788 such case types can be undefined. */
7791 fold_simple_1 (tree t
)
7794 enum tree_code code
= TREE_CODE (t
);
7806 return fold_sizeof_expr (t
);
7815 case TRUTH_NOT_EXPR
:
7817 case VIEW_CONVERT_EXPR
:
7820 case FIX_TRUNC_EXPR
:
7821 case FIXED_CONVERT_EXPR
:
7822 case ADDR_SPACE_CONVERT_EXPR
:
7824 op1
= TREE_OPERAND (t
, 0);
7826 t
= const_unop (code
, TREE_TYPE (t
), op1
);
7830 if (CONVERT_EXPR_CODE_P (code
)
7831 && TREE_OVERFLOW_P (t
) && !TREE_OVERFLOW_P (op1
))
7832 TREE_OVERFLOW (t
) = false;
7840 /* If T is a simple constant expression, returns its simplified value.
7841 Otherwise returns T. In contrast to maybe_constant_value we
7842 simplify only few operations on constant-expressions, and we don't
7843 try to simplify constexpressions. */
7846 fold_simple (tree t
)
7848 if (processing_template_decl
)
7851 tree r
= fold_simple_1 (t
);
7858 /* If T is a constant expression, returns its reduced value.
7859 Otherwise, if T does not have TREE_CONSTANT set, returns T.
7860 Otherwise, returns a version of T without TREE_CONSTANT.
7861 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated
7864 static GTY((deletable
)) hash_map
<tree
, tree
> *cv_cache
;
7867 maybe_constant_value (tree t
, tree decl
, bool manifestly_const_eval
)
7871 if (!is_nondependent_constant_expression (t
))
7873 if (TREE_OVERFLOW_P (t
))
7875 t
= build_nop (TREE_TYPE (t
), t
);
7876 TREE_CONSTANT (t
) = false;
7880 else if (CONSTANT_CLASS_P (t
))
7881 /* No caching or evaluation needed. */
7884 if (manifestly_const_eval
)
7885 return cxx_eval_outermost_constant_expr (t
, true, true, true, false, decl
);
7887 if (cv_cache
== NULL
)
7888 cv_cache
= hash_map
<tree
, tree
>::create_ggc (101);
7889 if (tree
*cached
= cv_cache
->get (t
))
7894 r
= break_out_target_exprs (r
, /*clear_loc*/true);
7895 protected_set_expr_location (r
, EXPR_LOCATION (t
));
7900 /* Don't evaluate an unevaluated operand. */
7901 if (cp_unevaluated_operand
)
7904 uid_sensitive_constexpr_evaluation_checker c
;
7905 r
= cxx_eval_outermost_constant_expr (t
, true, true, false, false, decl
);
7906 gcc_checking_assert (r
== t
7907 || CONVERT_EXPR_P (t
)
7908 || TREE_CODE (t
) == VIEW_CONVERT_EXPR
7909 || (TREE_CONSTANT (t
) && !TREE_CONSTANT (r
))
7910 || !cp_tree_equal (r
, t
));
7911 if (!c
.evaluation_restricted_p ())
7912 cv_cache
->put (t
, r
);
7916 /* Dispose of the whole CV_CACHE. */
7919 clear_cv_cache (void)
7921 if (cv_cache
!= NULL
)
7925 /* Dispose of the whole CV_CACHE and FOLD_CACHE. */
7928 clear_cv_and_fold_caches ()
7931 clear_fold_cache ();
7934 /* Internal function handling expressions in templates for
7935 fold_non_dependent_expr and fold_non_dependent_init.
7937 If we're in a template, but T isn't value dependent, simplify
7938 it. We're supposed to treat:
7940 template <typename T> void f(T[1 + 1]);
7941 template <typename T> void f(T[2]);
7943 as two declarations of the same function, for example. */
7946 fold_non_dependent_expr_template (tree t
, tsubst_flags_t complain
,
7947 bool manifestly_const_eval
,
7950 gcc_assert (processing_template_decl
);
7952 if (is_nondependent_constant_expression (t
))
7954 processing_template_decl_sentinel s
;
7955 t
= instantiate_non_dependent_expr_internal (t
, complain
);
7957 if (type_unknown_p (t
) || BRACE_ENCLOSED_INITIALIZER_P (t
))
7959 if (TREE_OVERFLOW_P (t
))
7961 t
= build_nop (TREE_TYPE (t
), t
);
7962 TREE_CONSTANT (t
) = false;
7967 if (cp_unevaluated_operand
&& !manifestly_const_eval
)
7970 tree r
= cxx_eval_outermost_constant_expr (t
, true, true,
7971 manifestly_const_eval
,
7973 /* cp_tree_equal looks through NOPs, so allow them. */
7974 gcc_checking_assert (r
== t
7975 || CONVERT_EXPR_P (t
)
7976 || TREE_CODE (t
) == VIEW_CONVERT_EXPR
7977 || (TREE_CONSTANT (t
) && !TREE_CONSTANT (r
))
7978 || !cp_tree_equal (r
, t
));
7981 else if (TREE_OVERFLOW_P (t
))
7983 t
= build_nop (TREE_TYPE (t
), t
);
7984 TREE_CONSTANT (t
) = false;
7990 /* Like maybe_constant_value but first fully instantiate the argument.
7992 Note: this is equivalent to instantiate_non_dependent_expr_sfinae
7993 (t, complain) followed by maybe_constant_value but is more efficient,
7994 because it calls instantiation_dependent_expression_p and
7995 potential_constant_expression at most once.
7996 The manifestly_const_eval argument is passed to maybe_constant_value.
7998 Callers should generally pass their active complain, or if they are in a
7999 non-template, diagnosing context, they can use the default of
8000 tf_warning_or_error. Callers that might be within a template context, don't
8001 have a complain parameter, and aren't going to remember the result for long
8002 (e.g. null_ptr_cst_p), can pass tf_none and deal with error_mark_node
8006 fold_non_dependent_expr (tree t
,
8007 tsubst_flags_t complain
/* = tf_warning_or_error */,
8008 bool manifestly_const_eval
/* = false */,
8009 tree object
/* = NULL_TREE */)
8014 if (processing_template_decl
)
8015 return fold_non_dependent_expr_template (t
, complain
,
8016 manifestly_const_eval
, object
);
8018 return maybe_constant_value (t
, object
, manifestly_const_eval
);
8021 /* Like fold_non_dependent_expr, but if EXPR couldn't be folded to a constant,
8022 return the original expression. */
8025 maybe_fold_non_dependent_expr (tree expr
,
8026 tsubst_flags_t complain
/*=tf_warning_or_error*/)
8028 tree t
= fold_non_dependent_expr (expr
, complain
);
8029 if (t
&& TREE_CONSTANT (t
))
8035 /* Like maybe_constant_init but first fully instantiate the argument. */
8038 fold_non_dependent_init (tree t
,
8039 tsubst_flags_t complain
/*=tf_warning_or_error*/,
8040 bool manifestly_const_eval
/*=false*/,
8041 tree object
/* = NULL_TREE */)
8046 if (processing_template_decl
)
8048 t
= fold_non_dependent_expr_template (t
, complain
,
8049 manifestly_const_eval
, object
);
8050 /* maybe_constant_init does this stripping, so do it here too. */
8051 if (TREE_CODE (t
) == TARGET_EXPR
)
8053 tree init
= TARGET_EXPR_INITIAL (t
);
8054 if (TREE_CODE (init
) == CONSTRUCTOR
)
8060 return maybe_constant_init (t
, object
, manifestly_const_eval
);
8063 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
8064 than wrapped in a TARGET_EXPR.
8065 ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
8066 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
8067 per P0595 even when ALLOW_NON_CONSTANT is true. */
8070 maybe_constant_init_1 (tree t
, tree decl
, bool allow_non_constant
,
8071 bool manifestly_const_eval
)
8075 if (TREE_CODE (t
) == EXPR_STMT
)
8076 t
= TREE_OPERAND (t
, 0);
8077 if (TREE_CODE (t
) == CONVERT_EXPR
8078 && VOID_TYPE_P (TREE_TYPE (t
)))
8079 t
= TREE_OPERAND (t
, 0);
8080 if (TREE_CODE (t
) == INIT_EXPR
)
8081 t
= TREE_OPERAND (t
, 1);
8082 if (TREE_CODE (t
) == TARGET_EXPR
)
8083 t
= TARGET_EXPR_INITIAL (t
);
8084 if (!is_nondependent_static_init_expression (t
))
8085 /* Don't try to evaluate it. */;
8086 else if (CONSTANT_CLASS_P (t
) && allow_non_constant
)
8087 /* No evaluation needed. */;
8089 t
= cxx_eval_outermost_constant_expr (t
, allow_non_constant
,
8091 manifestly_const_eval
, false, decl
);
8092 if (TREE_CODE (t
) == TARGET_EXPR
)
8094 tree init
= TARGET_EXPR_INITIAL (t
);
8095 if (TREE_CODE (init
) == CONSTRUCTOR
)
8101 /* Wrapper for maybe_constant_init_1 which permits non constants. */
8104 maybe_constant_init (tree t
, tree decl
, bool manifestly_const_eval
)
8106 return maybe_constant_init_1 (t
, decl
, true, manifestly_const_eval
);
8109 /* Wrapper for maybe_constant_init_1 which does not permit non constants. */
8112 cxx_constant_init (tree t
, tree decl
)
8114 return maybe_constant_init_1 (t
, decl
, false, true);
8118 /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
8119 /* Return true if the object referred to by REF has automatic or thread
8122 enum { ck_ok
, ck_bad
, ck_unknown
};
8124 check_automatic_or_tls (tree ref
)
8127 poly_int64 bitsize
, bitpos
;
8129 int volatilep
= 0, unsignedp
= 0;
8130 tree decl
= get_inner_reference (ref
, &bitsize
, &bitpos
, &offset
,
8131 &mode
, &unsignedp
, &volatilep
, false);
8134 /* If there isn't a decl in the middle, we don't know the linkage here,
8135 and this isn't a constant expression anyway. */
8138 dk
= decl_storage_duration (decl
);
8139 return (dk
== dk_auto
|| dk
== dk_thread
) ? ck_bad
: ck_ok
;
8143 /* Data structure for passing data from potential_constant_expression_1
8144 to check_for_return_continue via cp_walk_tree. */
8145 struct check_for_return_continue_data
{
8146 hash_set
<tree
> *pset
;
8151 /* Helper function for potential_constant_expression_1 SWITCH_STMT handling,
8152 called through cp_walk_tree. Return the first RETURN_EXPR found, or note
8153 the first CONTINUE_STMT and/or BREAK_STMT if RETURN_EXPR is not found. */
8155 check_for_return_continue (tree
*tp
, int *walk_subtrees
, void *data
)
8158 check_for_return_continue_data
*d
= (check_for_return_continue_data
*) data
;
8159 switch (TREE_CODE (t
))
8165 if (d
->continue_stmt
== NULL_TREE
)
8166 d
->continue_stmt
= t
;
8170 if (d
->break_stmt
== NULL_TREE
)
8175 if (tree r = cp_walk_tree (&x, check_for_return_continue, data, \
8179 /* For loops, walk subtrees manually, so that continue stmts found
8180 inside of the bodies of the loops are ignored. */
8183 RECUR (DO_COND (t
));
8184 s
= d
->continue_stmt
;
8186 RECUR (DO_BODY (t
));
8187 d
->continue_stmt
= s
;
8193 RECUR (WHILE_COND (t
));
8194 s
= d
->continue_stmt
;
8196 RECUR (WHILE_BODY (t
));
8197 d
->continue_stmt
= s
;
8203 RECUR (FOR_INIT_STMT (t
));
8204 RECUR (FOR_COND (t
));
8205 RECUR (FOR_EXPR (t
));
8206 s
= d
->continue_stmt
;
8208 RECUR (FOR_BODY (t
));
8209 d
->continue_stmt
= s
;
8213 case RANGE_FOR_STMT
:
8215 RECUR (RANGE_FOR_EXPR (t
));
8216 s
= d
->continue_stmt
;
8218 RECUR (RANGE_FOR_BODY (t
));
8219 d
->continue_stmt
= s
;
8225 RECUR (SWITCH_STMT_COND (t
));
8227 RECUR (SWITCH_STMT_BODY (t
));
8232 case STATEMENT_LIST
:
8245 /* Return true if T denotes a potentially constant expression. Issue
8246 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
8247 an lvalue-rvalue conversion is implied. If NOW is true, we want to
8248 consider the expression in the current context, independent of constexpr
8251 C++0x [expr.const] used to say
8253 6 An expression is a potential constant expression if it is
8254 a constant expression where all occurrences of function
8255 parameters are replaced by arbitrary constant expressions
8256 of the appropriate type.
8258 2 A conditional expression is a constant expression unless it
8259 involves one of the following as a potentially evaluated
8260 subexpression (3.2), but subexpressions of logical AND (5.14),
8261 logical OR (5.15), and conditional (5.16) operations that are
8262 not evaluated are not considered. */
8265 potential_constant_expression_1 (tree t
, bool want_rval
, bool strict
, bool now
,
8266 tsubst_flags_t flags
, tree
*jump_target
)
8268 #define RECUR(T,RV) \
8269 potential_constant_expression_1 ((T), (RV), strict, now, flags, jump_target)
8271 enum { any
= false, rval
= true };
8275 if (t
== error_mark_node
)
8279 location_t loc
= cp_expr_loc_or_input_loc (t
);
8282 /* If we are jumping, ignore everything. This is simpler than the
8283 cxx_eval_constant_expression handling because we only need to be
8284 conservatively correct, and we don't necessarily have a constant value
8285 available, so we don't bother with switch tracking. */
8288 if (TREE_THIS_VOLATILE (t
) && want_rval
)
8290 if (flags
& tf_error
)
8291 error_at (loc
, "lvalue-to-rvalue conversion of a volatile lvalue "
8292 "%qE with type %qT", t
, TREE_TYPE (t
));
8295 if (CONSTANT_CLASS_P (t
))
8297 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_TYPED
)
8298 && TREE_TYPE (t
) == error_mark_node
)
8301 switch (TREE_CODE (t
))
8307 case TEMPLATE_ID_EXPR
:
8309 case CASE_LABEL_EXPR
:
8316 case TEMPLATE_PARM_INDEX
:
8318 case IDENTIFIER_NODE
:
8319 case USERDEF_LITERAL
:
8320 /* We can see a FIELD_DECL in a pointer-to-member expression. */
8325 case PLACEHOLDER_EXPR
:
8328 case DEBUG_BEGIN_STMT
:
8332 if (!RECUR (TREE_OPERAND (t
, 0), any
))
8342 if (now
&& want_rval
)
8344 tree type
= TREE_TYPE (t
);
8345 if ((processing_template_decl
&& !COMPLETE_TYPE_P (type
))
8346 || dependent_type_p (type
)
8347 || is_really_empty_class (type
, /*ignore_vptr*/false))
8348 /* An empty class has no data to read. */
8350 if (flags
& tf_error
)
8351 error ("%qE is not a constant expression", t
);
8356 case AGGR_INIT_EXPR
:
8358 /* -- an invocation of a function other than a constexpr function
8359 or a constexpr constructor. */
8361 tree fun
= get_function_named_in_call (t
);
8362 const int nargs
= call_expr_nargs (t
);
8365 if (fun
== NULL_TREE
)
8367 /* Reset to allow the function to continue past the end
8368 of the block below. Otherwise return early. */
8371 if (TREE_CODE (t
) == CALL_EXPR
8372 && CALL_EXPR_FN (t
) == NULL_TREE
)
8373 switch (CALL_EXPR_IFN (t
))
8375 /* These should be ignored, they are optimized away from
8376 constexpr functions. */
8377 case IFN_UBSAN_NULL
:
8378 case IFN_UBSAN_BOUNDS
:
8379 case IFN_UBSAN_VPTR
:
8380 case IFN_FALLTHROUGH
:
8383 case IFN_ADD_OVERFLOW
:
8384 case IFN_SUB_OVERFLOW
:
8385 case IFN_MUL_OVERFLOW
:
8387 case IFN_VEC_CONVERT
:
8397 /* fold_call_expr can't do anything with IFN calls. */
8398 if (flags
& tf_error
)
8399 error_at (loc
, "call to internal function %qE", t
);
8404 if (fun
&& is_overloaded_fn (fun
))
8406 if (TREE_CODE (fun
) == FUNCTION_DECL
)
8408 if (builtin_valid_in_constant_expr_p (fun
))
8410 if (!maybe_constexpr_fn (fun
)
8411 /* Allow any built-in function; if the expansion
8412 isn't constant, we'll deal with that then. */
8413 && !fndecl_built_in_p (fun
)
8414 /* In C++20, replaceable global allocation functions
8415 are constant expressions. */
8416 && (!cxx_replaceable_global_alloc_fn (fun
)
8417 || TREE_CODE (t
) != CALL_EXPR
8418 || (!CALL_FROM_NEW_OR_DELETE_P (t
)
8419 && (current_function_decl
== NULL_TREE
8420 || !is_std_allocator_allocate
8421 (current_function_decl
))))
8422 /* Allow placement new in std::construct_at. */
8423 && (!cxx_placement_new_fn (fun
)
8424 || TREE_CODE (t
) != CALL_EXPR
8425 || current_function_decl
== NULL_TREE
8426 || !is_std_construct_at (current_function_decl
))
8427 && !cxx_dynamic_cast_fn_p (fun
))
8429 if (flags
& tf_error
)
8431 error_at (loc
, "call to non-%<constexpr%> function %qD",
8433 explain_invalid_constexpr_fn (fun
);
8437 /* A call to a non-static member function takes the address
8438 of the object as the first argument. But in a constant
8439 expression the address will be folded away, so look
8441 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun
)
8442 && !DECL_CONSTRUCTOR_P (fun
))
8444 tree x
= get_nth_callarg (t
, 0);
8445 if (is_this_parameter (x
))
8447 /* Don't require an immediately constant value, as
8448 constexpr substitution might not use the value. */
8449 bool sub_now
= false;
8450 if (!potential_constant_expression_1 (x
, rval
, strict
,
8459 if (!RECUR (fun
, true))
8461 fun
= get_first_fn (fun
);
8463 /* Skip initial arguments to base constructors. */
8464 if (DECL_BASE_CONSTRUCTOR_P (fun
))
8465 i
= num_artificial_parms_for (fun
);
8466 fun
= DECL_ORIGIN (fun
);
8470 if (RECUR (fun
, rval
))
8471 /* Might end up being a constant function pointer. */;
8475 for (; i
< nargs
; ++i
)
8477 tree x
= get_nth_callarg (t
, i
);
8478 /* In a template, reference arguments haven't been converted to
8479 REFERENCE_TYPE and we might not even know if the parameter
8480 is a reference, so accept lvalue constants too. */
8481 bool rv
= processing_template_decl
? any
: rval
;
8482 /* Don't require an immediately constant value, as constexpr
8483 substitution might not use the value of the argument. */
8484 bool sub_now
= false;
8485 if (!potential_constant_expression_1 (x
, rv
, strict
,
8486 sub_now
, flags
, jump_target
))
8492 case NON_LVALUE_EXPR
:
8493 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
8494 -- an lvalue of integral type that refers to a non-volatile
8495 const variable or static data member initialized with
8496 constant expressions, or
8498 -- an lvalue of literal type that refers to non-volatile
8499 object defined with constexpr, or that refers to a
8500 sub-object of such an object; */
8501 return RECUR (TREE_OPERAND (t
, 0), rval
);
8504 if (DECL_HAS_VALUE_EXPR_P (t
))
8506 if (now
&& is_normal_capture_proxy (t
))
8508 /* -- in a lambda-expression, a reference to this or to a
8509 variable with automatic storage duration defined outside that
8510 lambda-expression, where the reference would be an
8514 /* Since we're doing an lvalue-rvalue conversion, this might
8515 not be an odr-use, so evaluate the variable directly. */
8516 return RECUR (DECL_CAPTURED_VARIABLE (t
), rval
);
8518 if (flags
& tf_error
)
8520 tree cap
= DECL_CAPTURED_VARIABLE (t
);
8521 error ("lambda capture of %qE is not a constant expression",
8523 if (decl_constant_var_p (cap
))
8524 inform (input_location
, "because it is used as a glvalue");
8528 /* Treat __PRETTY_FUNCTION__ inside a template function as
8529 potentially-constant. */
8530 else if (DECL_PRETTY_FUNCTION_P (t
)
8531 && DECL_VALUE_EXPR (t
) == error_mark_node
)
8533 return RECUR (DECL_VALUE_EXPR (t
), rval
);
8536 && !var_in_maybe_constexpr_fn (t
)
8537 && !type_dependent_expression_p (t
)
8538 && !decl_maybe_constant_var_p (t
)
8540 || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t
))
8541 || (DECL_INITIAL (t
)
8542 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t
)))
8543 && COMPLETE_TYPE_P (TREE_TYPE (t
))
8544 && !is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
8546 if (flags
& tf_error
)
8547 non_const_var_error (loc
, t
);
8553 if (REINTERPRET_CAST_P (t
))
8555 if (flags
& tf_error
)
8556 error_at (loc
, "%<reinterpret_cast%> is not a constant expression");
8561 case VIEW_CONVERT_EXPR
:
8562 /* -- a reinterpret_cast. FIXME not implemented, and this rule
8563 may change to something more specific to type-punning (DR 1312). */
8565 tree from
= TREE_OPERAND (t
, 0);
8566 if (location_wrapper_p (t
))
8567 return (RECUR (from
, want_rval
));
8568 if (INDIRECT_TYPE_P (TREE_TYPE (t
)))
8570 STRIP_ANY_LOCATION_WRAPPER (from
);
8571 if (TREE_CODE (from
) == INTEGER_CST
8572 && !integer_zerop (from
))
8574 if (flags
& tf_error
)
8576 "%<reinterpret_cast%> from integer to pointer");
8580 return (RECUR (from
, TREE_CODE (t
) != VIEW_CONVERT_EXPR
));
8583 case ADDRESSOF_EXPR
:
8584 /* This is like ADDR_EXPR, except it won't form pointer-to-member. */
8585 t
= TREE_OPERAND (t
, 0);
8586 goto handle_addr_expr
;
8589 /* -- a unary operator & that is applied to an lvalue that
8590 designates an object with thread or automatic storage
8592 t
= TREE_OPERAND (t
, 0);
8594 if (TREE_CODE (t
) == OFFSET_REF
&& PTRMEM_OK_P (t
))
8595 /* A pointer-to-member constant. */
8600 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
8601 any checking here, as we might dereference the pointer later. If
8602 we remove this code, also remove check_automatic_or_tls. */
8603 i
= check_automatic_or_tls (t
);
8608 if (flags
& tf_error
)
8609 error ("address-of an object %qE with thread local or "
8610 "automatic storage is not a constant expression", t
);
8614 return RECUR (t
, any
);
8619 /* -- a class member access unless its postfix-expression is
8620 of literal type or of pointer to literal type. */
8621 /* This test would be redundant, as it follows from the
8622 postfix-expression being a potential constant expression. */
8623 if (type_unknown_p (t
))
8625 if (is_overloaded_fn (t
))
8626 /* In a template, a COMPONENT_REF of a function expresses ob.fn(),
8627 which uses ob as an lvalue. */
8634 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
8636 case EXPR_PACK_EXPANSION
:
8637 return RECUR (PACK_EXPANSION_PATTERN (t
), want_rval
);
8641 tree x
= TREE_OPERAND (t
, 0);
8643 if (is_this_parameter (x
) && !is_capture_proxy (x
))
8645 if (!var_in_maybe_constexpr_fn (x
))
8647 if (flags
& tf_error
)
8648 error_at (loc
, "use of %<this%> in a constant expression");
8653 return RECUR (x
, rval
);
8656 case STATEMENT_LIST
:
8657 for (tree stmt
: tsi_range (t
))
8658 if (!RECUR (stmt
, any
))
8663 if (cxx_dialect
< cxx14
)
8665 if (!RECUR (TREE_OPERAND (t
, 0), any
))
8667 /* Just ignore clobbers. */
8668 if (TREE_CLOBBER_P (TREE_OPERAND (t
, 1)))
8670 if (!RECUR (TREE_OPERAND (t
, 1), rval
))
8675 if (cxx_dialect
< cxx14
)
8677 if (!RECUR (TREE_OPERAND (t
, 0), rval
))
8679 if (!RECUR (TREE_OPERAND (t
, 2), rval
))
8684 if (!RECUR (DO_COND (t
), rval
))
8686 if (!RECUR (DO_BODY (t
), any
))
8688 if (breaks (jump_target
) || continues (jump_target
))
8689 *jump_target
= NULL_TREE
;
8693 if (!RECUR (FOR_INIT_STMT (t
), any
))
8696 if (!RECUR (tmp
, rval
))
8700 if (!processing_template_decl
)
8701 tmp
= cxx_eval_outermost_constant_expr (tmp
, true);
8702 /* If we couldn't evaluate the condition, it might not ever be
8704 if (!integer_onep (tmp
))
8706 /* Before returning true, check if the for body can contain
8708 hash_set
<tree
> pset
;
8709 check_for_return_continue_data data
= { &pset
, NULL_TREE
,
8712 = cp_walk_tree (&FOR_BODY (t
), check_for_return_continue
,
8714 *jump_target
= ret_expr
;
8718 if (!RECUR (FOR_EXPR (t
), any
))
8720 if (!RECUR (FOR_BODY (t
), any
))
8722 if (breaks (jump_target
) || continues (jump_target
))
8723 *jump_target
= NULL_TREE
;
8726 case RANGE_FOR_STMT
:
8727 if (!RECUR (RANGE_FOR_INIT_STMT (t
), any
))
8729 if (!RECUR (RANGE_FOR_EXPR (t
), any
))
8731 if (!RECUR (RANGE_FOR_BODY (t
), any
))
8733 if (breaks (jump_target
) || continues (jump_target
))
8734 *jump_target
= NULL_TREE
;
8738 tmp
= WHILE_COND (t
);
8739 if (!RECUR (tmp
, rval
))
8741 if (!processing_template_decl
)
8742 tmp
= cxx_eval_outermost_constant_expr (tmp
, true);
8743 /* If we couldn't evaluate the condition, it might not ever be true. */
8744 if (!integer_onep (tmp
))
8746 /* Before returning true, check if the while body can contain
8748 hash_set
<tree
> pset
;
8749 check_for_return_continue_data data
= { &pset
, NULL_TREE
,
8752 = cp_walk_tree (&WHILE_BODY (t
), check_for_return_continue
,
8754 *jump_target
= ret_expr
;
8757 if (!RECUR (WHILE_BODY (t
), any
))
8759 if (breaks (jump_target
) || continues (jump_target
))
8760 *jump_target
= NULL_TREE
;
8764 if (!RECUR (SWITCH_STMT_COND (t
), rval
))
8766 /* FIXME we don't check SWITCH_STMT_BODY currently, because even
8767 unreachable labels would be checked and it is enough if there is
8768 a single switch cond value for which it is a valid constant
8769 expression. We need to check if there are any RETURN_EXPRs
8770 or CONTINUE_STMTs inside of the body though, as in that case
8771 we need to set *jump_target. */
8774 hash_set
<tree
> pset
;
8775 check_for_return_continue_data data
= { &pset
, NULL_TREE
,
8778 = cp_walk_tree (&SWITCH_STMT_BODY (t
), check_for_return_continue
,
8780 /* The switch might return. */
8781 *jump_target
= ret_expr
;
8782 else if (data
.continue_stmt
)
8783 /* The switch can't return, but might continue. */
8784 *jump_target
= data
.continue_stmt
;
8789 return RECUR (STMT_EXPR_STMT (t
), rval
);
8792 if (cxx_dialect
>= cxx17
)
8793 /* In C++17 lambdas can be constexpr, don't give up yet. */
8795 else if (flags
& tf_error
)
8796 error_at (loc
, "lambda-expression is not a constant expression "
8800 case DYNAMIC_CAST_EXPR
:
8801 case PSEUDO_DTOR_EXPR
:
8805 case VEC_DELETE_EXPR
:
8811 case OMP_DISTRIBUTE
:
8815 case OMP_TARGET_DATA
:
8825 case OMP_TARGET_UPDATE
:
8826 case OMP_TARGET_ENTER_DATA
:
8827 case OMP_TARGET_EXIT_DATA
:
8829 case OMP_ATOMIC_READ
:
8830 case OMP_ATOMIC_CAPTURE_OLD
:
8831 case OMP_ATOMIC_CAPTURE_NEW
:
8837 case OACC_HOST_DATA
:
8841 case OACC_ENTER_DATA
:
8842 case OACC_EXIT_DATA
:
8844 /* GCC internal stuff. */
8846 case TRANSACTION_EXPR
:
8847 case AT_ENCODE_EXPR
:
8849 if (flags
& tf_error
)
8850 error_at (loc
, "expression %qE is not a constant expression", t
);
8854 if (flags
& tf_error
)
8855 inline_asm_in_constexpr_error (loc
);
8859 if (cxx_dialect
>= cxx20
)
8860 /* In C++20 virtual calls can be constexpr, don't give up yet. */
8862 else if (flags
& tf_error
)
8864 "virtual functions cannot be %<constexpr%> before C++20");
8868 /* In C++20, a typeid expression whose operand is of polymorphic
8869 class type can be constexpr. */
8871 tree e
= TREE_OPERAND (t
, 0);
8872 if (cxx_dialect
< cxx20
8875 && !type_dependent_expression_p (e
)
8876 && TYPE_POLYMORPHIC_P (TREE_TYPE (e
)))
8878 if (flags
& tf_error
)
8879 error_at (loc
, "%<typeid%> is not a constant expression "
8880 "because %qE is of polymorphic type", e
);
8886 case POINTER_DIFF_EXPR
:
8897 case SPACESHIP_EXPR
:
8901 case PREINCREMENT_EXPR
:
8902 case POSTINCREMENT_EXPR
:
8903 case PREDECREMENT_EXPR
:
8904 case POSTDECREMENT_EXPR
:
8905 if (cxx_dialect
< cxx14
)
8911 if (TYPE_P (TREE_OPERAND (t
, 0)))
8917 case FIX_TRUNC_EXPR
:
8922 case TRUTH_NOT_EXPR
:
8923 case FIXED_CONVERT_EXPR
:
8924 case UNARY_PLUS_EXPR
:
8925 case UNARY_LEFT_FOLD_EXPR
:
8926 case UNARY_RIGHT_FOLD_EXPR
:
8928 return RECUR (TREE_OPERAND (t
, 0), rval
);
8931 case CONST_CAST_EXPR
:
8932 case STATIC_CAST_EXPR
:
8933 case REINTERPRET_CAST_EXPR
:
8934 case IMPLICIT_CONV_EXPR
:
8935 if (cxx_dialect
< cxx11
8936 && !dependent_type_p (TREE_TYPE (t
))
8937 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
)))
8938 /* In C++98, a conversion to non-integral type can't be part of a
8939 constant expression. */
8941 if (flags
& tf_error
)
8943 "cast to non-integral type %qT in a constant expression",
8947 /* This might be a conversion from a class to a (potentially) literal
8948 type. Let's consider it potentially constant since the conversion
8949 might be a constexpr user-defined conversion. */
8950 else if (cxx_dialect
>= cxx11
8951 && (dependent_type_p (TREE_TYPE (t
))
8952 || !COMPLETE_TYPE_P (TREE_TYPE (t
))
8953 || literal_type_p (TREE_TYPE (t
)))
8954 && TREE_OPERAND (t
, 0))
8956 tree type
= TREE_TYPE (TREE_OPERAND (t
, 0));
8957 /* If this is a dependent type, it could end up being a class
8958 with conversions. */
8959 if (type
== NULL_TREE
|| WILDCARD_TYPE_P (type
))
8961 /* Or a non-dependent class which has conversions. */
8962 else if (CLASS_TYPE_P (type
)
8963 && (TYPE_HAS_CONVERSION (type
) || dependent_scope_p (type
)))
8967 return (RECUR (TREE_OPERAND (t
, 0),
8968 !TYPE_REF_P (TREE_TYPE (t
))));
8971 return RECUR (BIND_EXPR_BODY (t
), want_rval
);
8973 case CLEANUP_POINT_EXPR
:
8974 case MUST_NOT_THROW_EXPR
:
8975 case TRY_CATCH_EXPR
:
8980 case NON_DEPENDENT_EXPR
:
8981 /* For convenience. */
8984 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
8987 tmp
= DECL_EXPR_DECL (t
);
8988 if (VAR_P (tmp
) && !DECL_ARTIFICIAL (tmp
))
8990 if (CP_DECL_THREAD_LOCAL_P (tmp
))
8992 if (flags
& tf_error
)
8993 error_at (DECL_SOURCE_LOCATION (tmp
), "%qD declared "
8994 "%<thread_local%> in %<constexpr%> context", tmp
);
8997 else if (TREE_STATIC (tmp
))
8999 if (flags
& tf_error
)
9000 error_at (DECL_SOURCE_LOCATION (tmp
), "%qD declared "
9001 "%<static%> in %<constexpr%> context", tmp
);
9004 else if (!check_for_uninitialized_const_var
9005 (tmp
, /*constexpr_context_p=*/true, flags
))
9008 return RECUR (tmp
, want_rval
);
9010 case TRY_FINALLY_EXPR
:
9011 return (RECUR (TREE_OPERAND (t
, 0), want_rval
)
9012 && RECUR (TREE_OPERAND (t
, 1), any
));
9015 return RECUR (TREE_OPERAND (t
, 1), want_rval
);
9018 if (!TARGET_EXPR_DIRECT_INIT_P (t
)
9019 && !literal_type_p (TREE_TYPE (t
)))
9021 if (flags
& tf_error
)
9023 auto_diagnostic_group d
;
9024 error_at (loc
, "temporary of non-literal type %qT in a "
9025 "constant expression", TREE_TYPE (t
));
9026 explain_non_literal_class (TREE_TYPE (t
));
9032 return RECUR (TREE_OPERAND (t
, 1), rval
);
9036 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (t
);
9037 constructor_elt
*ce
;
9038 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
9039 if (!RECUR (ce
->value
, want_rval
))
9046 gcc_assert (TREE_PURPOSE (t
) == NULL_TREE
9047 || DECL_P (TREE_PURPOSE (t
)));
9048 if (!RECUR (TREE_VALUE (t
), want_rval
))
9050 if (TREE_CHAIN (t
) == NULL_TREE
)
9052 return RECUR (TREE_CHAIN (t
), want_rval
);
9055 case TRUNC_DIV_EXPR
:
9057 case FLOOR_DIV_EXPR
:
9058 case ROUND_DIV_EXPR
:
9059 case TRUNC_MOD_EXPR
:
9061 case ROUND_MOD_EXPR
:
9063 tree denom
= TREE_OPERAND (t
, 1);
9064 if (!RECUR (denom
, rval
))
9066 /* We can't call cxx_eval_outermost_constant_expr on an expression
9067 that hasn't been through instantiate_non_dependent_expr yet. */
9068 if (!processing_template_decl
)
9069 denom
= cxx_eval_outermost_constant_expr (denom
, true);
9070 if (integer_zerop (denom
))
9072 if (flags
& tf_error
)
9073 error ("division by zero is not a constant expression");
9079 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
9085 /* check_return_expr sometimes wraps a TARGET_EXPR in a
9086 COMPOUND_EXPR; don't get confused. */
9087 tree op0
= TREE_OPERAND (t
, 0);
9088 tree op1
= TREE_OPERAND (t
, 1);
9090 if (TREE_CODE (op0
) == TARGET_EXPR
&& op1
== TARGET_EXPR_SLOT (op0
))
9091 return RECUR (op0
, want_rval
);
9096 /* If the first operand is the non-short-circuit constant, look at
9097 the second operand; otherwise we only care about the first one for
9099 case TRUTH_AND_EXPR
:
9100 case TRUTH_ANDIF_EXPR
:
9101 tmp
= boolean_true_node
;
9104 case TRUTH_ORIF_EXPR
:
9105 tmp
= boolean_false_node
;
9108 tree op0
= TREE_OPERAND (t
, 0);
9109 tree op1
= TREE_OPERAND (t
, 1);
9110 if (!RECUR (op0
, rval
))
9112 if (!(flags
& tf_error
) && RECUR (op1
, rval
))
9113 /* When quiet, try to avoid expensive trial evaluation by first
9114 checking potentiality of the second operand. */
9116 if (!processing_template_decl
)
9117 op0
= cxx_eval_outermost_constant_expr (op0
, true);
9118 if (tree_int_cst_equal (op0
, tmp
))
9119 return (flags
& tf_error
) ? RECUR (op1
, rval
) : false;
9126 case POINTER_PLUS_EXPR
:
9128 case EXACT_DIV_EXPR
:
9138 case TRUTH_XOR_EXPR
:
9139 case UNORDERED_EXPR
:
9152 case ARRAY_RANGE_REF
:
9156 case BINARY_LEFT_FOLD_EXPR
:
9157 case BINARY_RIGHT_FOLD_EXPR
:
9159 for (i
= 0; i
< 2; ++i
)
9160 if (!RECUR (TREE_OPERAND (t
, i
), want_rval
))
9165 for (i
= 0; i
< 3; ++i
)
9166 if (!RECUR (TREE_OPERAND (t
, i
), true))
9171 if (COND_EXPR_IS_VEC_DELETE (t
) && cxx_dialect
< cxx20
)
9173 if (flags
& tf_error
)
9174 error_at (loc
, "%<delete[]%> is not a constant expression");
9180 /* If the condition is a known constant, we know which of the legs we
9181 care about; otherwise we only require that the condition and
9182 either of the legs be potentially constant. */
9183 tmp
= TREE_OPERAND (t
, 0);
9184 if (!RECUR (tmp
, rval
))
9186 if (!processing_template_decl
)
9187 tmp
= cxx_eval_outermost_constant_expr (tmp
, true);
9188 /* potential_constant_expression* isn't told if it is called for
9189 manifestly_const_eval or not, so for consteval if always
9190 process both branches as if the condition is not a known
9192 if (TREE_CODE (t
) != IF_STMT
|| !IF_STMT_CONSTEVAL_P (t
))
9194 if (integer_zerop (tmp
))
9195 return RECUR (TREE_OPERAND (t
, 2), want_rval
);
9196 else if (TREE_CODE (tmp
) == INTEGER_CST
)
9197 return RECUR (TREE_OPERAND (t
, 1), want_rval
);
9200 for (i
= 1; i
< 3; ++i
)
9202 tree this_jump_target
= tmp
;
9203 if (potential_constant_expression_1 (TREE_OPERAND (t
, i
),
9204 want_rval
, strict
, now
,
9205 tf_none
, &this_jump_target
))
9207 if (returns (&this_jump_target
))
9208 *jump_target
= this_jump_target
;
9209 else if (!returns (jump_target
))
9211 if (breaks (&this_jump_target
)
9212 || continues (&this_jump_target
))
9213 *jump_target
= this_jump_target
;
9216 /* If the then branch is potentially constant, but
9217 does not return, check if the else branch
9218 couldn't return, break or continue. */
9219 hash_set
<tree
> pset
;
9220 check_for_return_continue_data data
= { &pset
, NULL_TREE
,
9223 = cp_walk_tree (&TREE_OPERAND (t
, 2),
9224 check_for_return_continue
, &data
,
9226 *jump_target
= ret_expr
;
9227 else if (*jump_target
== NULL_TREE
)
9229 if (data
.continue_stmt
)
9230 *jump_target
= data
.continue_stmt
;
9231 else if (data
.break_stmt
)
9232 *jump_target
= data
.break_stmt
;
9239 if (flags
& tf_error
)
9240 error_at (loc
, "expression %qE is not a constant expression", t
);
9244 if (VEC_INIT_EXPR_IS_CONSTEXPR (t
))
9246 if (flags
& tf_error
)
9248 error_at (loc
, "non-constant array initialization");
9249 diagnose_non_constexpr_vec_init (t
);
9255 /* We can see these in statement-expressions. */
9259 if (!RECUR (CLEANUP_BODY (t
), any
))
9261 if (!CLEANUP_EH_ONLY (t
) && !RECUR (CLEANUP_EXPR (t
), any
))
9265 case EMPTY_CLASS_EXPR
:
9270 tree
*target
= &TREE_OPERAND (t
, 0);
9271 /* Gotos representing break and continue are OK. */
9272 if (breaks (target
) || continues (target
))
9274 *jump_target
= *target
;
9277 if (flags
& tf_error
)
9278 error_at (loc
, "%<goto%> is not a constant expression");
9283 t
= LABEL_EXPR_LABEL (t
);
9284 if (DECL_ARTIFICIAL (t
) || cxx_dialect
>= cxx23
)
9286 else if (flags
& tf_error
)
9287 error_at (loc
, "label definition in %<constexpr%> function only "
9288 "available with %<-std=c++2b%> or %<-std=gnu++2b%>");
9292 return RECUR (TREE_OPERAND (t
, 0), rval
);
9295 return RECUR (TREE_OPERAND (t
, 0), rval
);
9297 /* Coroutine await, yield and return expressions are not. */
9300 case CO_RETURN_EXPR
:
9303 case NONTYPE_ARGUMENT_PACK
:
9305 tree args
= ARGUMENT_PACK_ARGS (t
);
9306 int len
= TREE_VEC_LENGTH (args
);
9307 for (int i
= 0; i
< len
; ++i
)
9308 if (!RECUR (TREE_VEC_ELT (args
, i
), any
))
9314 if (objc_non_constant_expr_p (t
))
9317 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t
)));
9325 potential_constant_expression_1 (tree t
, bool want_rval
, bool strict
, bool now
,
9326 tsubst_flags_t flags
)
9328 if (flags
& tf_error
)
9330 /* Check potentiality quietly first, as that could be performed more
9331 efficiently in some cases (currently only for TRUTH_*_EXPR). If
9332 that fails, replay the check noisily to give errors. */
9334 if (potential_constant_expression_1 (t
, want_rval
, strict
, now
, flags
))
9339 tree target
= NULL_TREE
;
9340 return potential_constant_expression_1 (t
, want_rval
, strict
, now
,
9344 /* The main entry point to the above. */
9347 potential_constant_expression (tree t
)
9349 return potential_constant_expression_1 (t
, false, true, false, tf_none
);
9352 /* As above, but require a constant rvalue. */
9355 potential_rvalue_constant_expression (tree t
)
9357 return potential_constant_expression_1 (t
, true, true, false, tf_none
);
9360 /* Like above, but complain about non-constant expressions. */
9363 require_potential_constant_expression (tree t
)
9365 return potential_constant_expression_1 (t
, false, true, false,
9366 tf_warning_or_error
);
9369 /* Cross product of the above. */
9372 require_potential_rvalue_constant_expression (tree t
)
9374 return potential_constant_expression_1 (t
, true, true, false,
9375 tf_warning_or_error
);
9378 /* Like above, but don't consider PARM_DECL a potential_constant_expression. */
9381 require_rvalue_constant_expression (tree t
)
9383 return potential_constant_expression_1 (t
, true, true, true,
9384 tf_warning_or_error
);
9387 /* Like potential_constant_expression, but don't consider possible constexpr
9388 substitution of the current function. That is, PARM_DECL qualifies under
9389 potential_constant_expression, but not here.
9391 This is basically what you can check when any actual constant values might
9392 be value-dependent. */
9395 is_constant_expression (tree t
)
9397 return potential_constant_expression_1 (t
, false, true, true, tf_none
);
9400 /* As above, but expect an rvalue. */
9403 is_rvalue_constant_expression (tree t
)
9405 return potential_constant_expression_1 (t
, true, true, true, tf_none
);
9408 /* Like above, but complain about non-constant expressions. */
9411 require_constant_expression (tree t
)
9413 return potential_constant_expression_1 (t
, false, true, true,
9414 tf_warning_or_error
);
9417 /* Like is_constant_expression, but allow const variables that are not allowed
9418 under constexpr rules. */
9421 is_static_init_expression (tree t
)
9423 return potential_constant_expression_1 (t
, false, false, true, tf_none
);
9426 /* Returns true if T is a potential constant expression that is not
9427 instantiation-dependent, and therefore a candidate for constant folding even
9431 is_nondependent_constant_expression (tree t
)
9433 return (!type_unknown_p (t
)
9434 && is_constant_expression (t
)
9435 && !instantiation_dependent_expression_p (t
));
9438 /* Returns true if T is a potential static initializer expression that is not
9439 instantiation-dependent. */
9442 is_nondependent_static_init_expression (tree t
)
9444 return (!type_unknown_p (t
)
9445 && is_static_init_expression (t
)
9446 && !instantiation_dependent_expression_p (t
));
9449 /* True iff FN is an implicitly constexpr function. */
9452 decl_implicit_constexpr_p (tree fn
)
9454 if (!(flag_implicit_constexpr
9455 && TREE_CODE (fn
) == FUNCTION_DECL
9456 && DECL_DECLARED_CONSTEXPR_P (fn
)))
9459 if (DECL_CLONED_FUNCTION_P (fn
))
9460 fn
= DECL_CLONED_FUNCTION (fn
);
9462 return (DECL_LANG_SPECIFIC (fn
)
9463 && DECL_LANG_SPECIFIC (fn
)->u
.fn
.implicit_constexpr
);
9466 /* Finalize constexpr processing after parsing. */
9469 fini_constexpr (void)
9471 /* The contexpr call and fundef copies tables are no longer needed. */
9472 constexpr_call_table
= NULL
;
9473 fundef_copies_table
= NULL
;
9476 #include "gt-cp-constexpr.h"