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"
39 static bool verify_constant (tree
, bool, bool *, bool *);
40 #define VERIFY_CONSTANT(X) \
42 if (verify_constant ((X), ctx->quiet, non_constant_p, overflow_p)) \
46 static HOST_WIDE_INT
find_array_ctor_elt (tree ary
, tree dindex
,
49 /* Returns true iff FUN is an instantiation of a constexpr function
50 template or a defaulted constexpr function. */
53 is_instantiation_of_constexpr (tree fun
)
55 return ((DECL_TEMPLOID_INSTANTIATION (fun
)
56 && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun
)))
57 || (DECL_DEFAULTED_FN (fun
)
58 && DECL_DECLARED_CONSTEXPR_P (fun
)));
61 /* Return true if T is a literal type. */
64 literal_type_p (tree t
)
69 || (VOID_TYPE_P (t
) && cxx_dialect
>= cxx14
))
73 t
= complete_type (t
);
74 gcc_assert (COMPLETE_TYPE_P (t
) || errorcount
);
75 return CLASSTYPE_LITERAL_P (t
);
77 if (TREE_CODE (t
) == ARRAY_TYPE
)
78 return literal_type_p (strip_array_types (t
));
82 /* If DECL is a variable declared `constexpr', require its type
83 be literal. Return error_mark_node if we give an error, the
87 ensure_literal_type_for_constexpr_object (tree decl
)
89 tree type
= TREE_TYPE (decl
);
91 && (DECL_DECLARED_CONSTEXPR_P (decl
)
92 || var_in_constexpr_fn (decl
))
93 && !processing_template_decl
)
95 tree stype
= strip_array_types (type
);
96 if (CLASS_TYPE_P (stype
) && !COMPLETE_TYPE_P (complete_type (stype
)))
97 /* Don't complain here, we'll complain about incompleteness
98 when we try to initialize the variable. */;
99 else if (!literal_type_p (type
))
101 if (DECL_DECLARED_CONSTEXPR_P (decl
))
103 auto_diagnostic_group d
;
104 error_at (DECL_SOURCE_LOCATION (decl
),
105 "the type %qT of %<constexpr%> variable %qD "
106 "is not literal", type
, decl
);
107 explain_non_literal_class (type
);
108 decl
= error_mark_node
;
112 if (!is_instantiation_of_constexpr (current_function_decl
))
114 auto_diagnostic_group d
;
115 error_at (DECL_SOURCE_LOCATION (decl
),
116 "variable %qD of non-literal type %qT in "
117 "%<constexpr%> function", decl
, type
);
118 explain_non_literal_class (type
);
119 decl
= error_mark_node
;
121 cp_function_chain
->invalid_constexpr
= true;
124 else if (DECL_DECLARED_CONSTEXPR_P (decl
)
125 && variably_modified_type_p (type
, NULL_TREE
))
127 error_at (DECL_SOURCE_LOCATION (decl
),
128 "%<constexpr%> variable %qD has variably-modified "
129 "type %qT", decl
, type
);
130 decl
= error_mark_node
;
136 struct constexpr_fundef_hasher
: ggc_ptr_hash
<constexpr_fundef
>
138 static hashval_t
hash (const constexpr_fundef
*);
139 static bool equal (const constexpr_fundef
*, const constexpr_fundef
*);
142 /* This table holds all constexpr function definitions seen in
143 the current translation unit. */
145 static GTY (()) hash_table
<constexpr_fundef_hasher
> *constexpr_fundef_table
;
147 /* Utility function used for managing the constexpr function table.
148 Return true if the entries pointed to by P and Q are for the
149 same constexpr function. */
152 constexpr_fundef_hasher::equal (const constexpr_fundef
*lhs
,
153 const constexpr_fundef
*rhs
)
155 return lhs
->decl
== rhs
->decl
;
158 /* Utility function used for managing the constexpr function table.
159 Return a hash value for the entry pointed to by Q. */
162 constexpr_fundef_hasher::hash (const constexpr_fundef
*fundef
)
164 return DECL_UID (fundef
->decl
);
167 /* Return a previously saved definition of function FUN. */
170 retrieve_constexpr_fundef (tree fun
)
172 if (constexpr_fundef_table
== NULL
)
175 constexpr_fundef fundef
= { fun
, NULL_TREE
, NULL_TREE
, NULL_TREE
};
176 return constexpr_fundef_table
->find (&fundef
);
179 /* Check whether the parameter and return types of FUN are valid for a
180 constexpr function, and complain if COMPLAIN. */
183 is_valid_constexpr_fn (tree fun
, bool complain
)
187 if (DECL_INHERITED_CTOR (fun
)
188 && TREE_CODE (fun
) == TEMPLATE_DECL
)
192 error ("inherited constructor %qD is not %<constexpr%>",
193 DECL_INHERITED_CTOR (fun
));
197 for (tree parm
= FUNCTION_FIRST_USER_PARM (fun
);
198 parm
!= NULL_TREE
; parm
= TREE_CHAIN (parm
))
199 if (!literal_type_p (TREE_TYPE (parm
)))
204 auto_diagnostic_group d
;
205 error ("invalid type for parameter %d of %<constexpr%> "
206 "function %q+#D", DECL_PARM_INDEX (parm
), fun
);
207 explain_non_literal_class (TREE_TYPE (parm
));
212 if (LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun
)) && cxx_dialect
< cxx17
)
216 inform (DECL_SOURCE_LOCATION (fun
),
217 "lambdas are implicitly %<constexpr%> only in C++17 and later");
219 else if (!DECL_CONSTRUCTOR_P (fun
))
221 tree rettype
= TREE_TYPE (TREE_TYPE (fun
));
222 if (!literal_type_p (rettype
))
227 auto_diagnostic_group d
;
228 error ("invalid return type %qT of %<constexpr%> function %q+D",
230 explain_non_literal_class (rettype
);
234 /* C++14 DR 1684 removed this restriction. */
235 if (cxx_dialect
< cxx14
236 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fun
)
237 && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun
)))
242 auto_diagnostic_group d
;
243 if (pedwarn (DECL_SOURCE_LOCATION (fun
), OPT_Wpedantic
,
244 "enclosing class of %<constexpr%> non-static"
245 " member function %q+#D is not a literal type",
247 explain_non_literal_class (DECL_CONTEXT (fun
));
251 else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun
)))
255 error ("%q#T has virtual base classes", DECL_CONTEXT (fun
));
261 /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF
262 for a member of an anonymous aggregate, INIT is the initializer for that
263 member, and VEC_OUTER is the vector of constructor elements for the class
264 whose constructor we are processing. Add the initializer to the vector
265 and return true to indicate success. */
268 build_anon_member_initialization (tree member
, tree init
,
269 vec
<constructor_elt
, va_gc
> **vec_outer
)
271 /* MEMBER presents the relevant fields from the inside out, but we need
272 to build up the initializer from the outside in so that we can reuse
273 previously built CONSTRUCTORs if this is, say, the second field in an
274 anonymous struct. So we use a vec as a stack. */
275 auto_vec
<tree
, 2> fields
;
278 fields
.safe_push (TREE_OPERAND (member
, 1));
279 member
= TREE_OPERAND (member
, 0);
281 while (ANON_AGGR_TYPE_P (TREE_TYPE (member
))
282 && TREE_CODE (member
) == COMPONENT_REF
);
284 /* VEC has the constructor elements vector for the context of FIELD.
285 If FIELD is an anonymous aggregate, we will push inside it. */
286 vec
<constructor_elt
, va_gc
> **vec
= vec_outer
;
288 while (field
= fields
.pop(),
289 ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
292 /* If there is already an outer constructor entry for the anonymous
293 aggregate FIELD, use it; otherwise, insert one. */
294 if (vec_safe_is_empty (*vec
)
295 || (*vec
)->last().index
!= field
)
297 ctor
= build_constructor (TREE_TYPE (field
), NULL
);
298 CONSTRUCTOR_APPEND_ELT (*vec
, field
, ctor
);
301 ctor
= (*vec
)->last().value
;
302 vec
= &CONSTRUCTOR_ELTS (ctor
);
305 /* Now we're at the innermost field, the one that isn't an anonymous
306 aggregate. Add its initializer to the CONSTRUCTOR and we're done. */
307 gcc_assert (fields
.is_empty());
308 CONSTRUCTOR_APPEND_ELT (*vec
, field
, init
);
313 /* Subroutine of build_constexpr_constructor_member_initializers.
314 The expression tree T represents a data member initialization
315 in a (constexpr) constructor definition. Build a pairing of
316 the data member with its initializer, and prepend that pair
317 to the existing initialization pair INITS. */
320 build_data_member_initialization (tree t
, vec
<constructor_elt
, va_gc
> **vec
)
323 if (TREE_CODE (t
) == CLEANUP_POINT_EXPR
)
324 t
= TREE_OPERAND (t
, 0);
325 if (TREE_CODE (t
) == EXPR_STMT
)
326 t
= TREE_OPERAND (t
, 0);
327 if (t
== error_mark_node
)
329 if (TREE_CODE (t
) == STATEMENT_LIST
)
331 tree_stmt_iterator i
;
332 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
334 if (! build_data_member_initialization (tsi_stmt (i
), vec
))
339 if (TREE_CODE (t
) == CLEANUP_STMT
)
341 /* We can't see a CLEANUP_STMT in a constructor for a literal class,
342 but we can in a constexpr constructor for a non-literal class. Just
343 ignore it; either all the initialization will be constant, in which
344 case the cleanup can't run, or it can't be constexpr.
345 Still recurse into CLEANUP_BODY. */
346 return build_data_member_initialization (CLEANUP_BODY (t
), vec
);
348 if (TREE_CODE (t
) == CONVERT_EXPR
)
349 t
= TREE_OPERAND (t
, 0);
350 if (TREE_CODE (t
) == INIT_EXPR
351 /* vptr initialization shows up as a MODIFY_EXPR. In C++14 we only
352 use what this function builds for cx_check_missing_mem_inits, and
353 assignment in the ctor body doesn't count. */
354 || (cxx_dialect
< cxx14
&& TREE_CODE (t
) == MODIFY_EXPR
))
356 member
= TREE_OPERAND (t
, 0);
357 init
= break_out_target_exprs (TREE_OPERAND (t
, 1));
359 else if (TREE_CODE (t
) == CALL_EXPR
)
361 tree fn
= get_callee_fndecl (t
);
362 if (!fn
|| !DECL_CONSTRUCTOR_P (fn
))
363 /* We're only interested in calls to subobject constructors. */
365 member
= CALL_EXPR_ARG (t
, 0);
366 /* We don't use build_cplus_new here because it complains about
367 abstract bases. Leaving the call unwrapped means that it has the
368 wrong type, but cxx_eval_constant_expression doesn't care. */
369 init
= break_out_target_exprs (t
);
371 else if (TREE_CODE (t
) == BIND_EXPR
)
372 return build_data_member_initialization (BIND_EXPR_BODY (t
), vec
);
374 /* Don't add anything else to the CONSTRUCTOR. */
376 if (INDIRECT_REF_P (member
))
377 member
= TREE_OPERAND (member
, 0);
378 if (TREE_CODE (member
) == NOP_EXPR
)
382 if (TREE_CODE (op
) == ADDR_EXPR
)
384 gcc_assert (same_type_ignoring_top_level_qualifiers_p
385 (TREE_TYPE (TREE_TYPE (op
)),
386 TREE_TYPE (TREE_TYPE (member
))));
387 /* Initializing a cv-qualified member; we need to look through
391 else if (op
== current_class_ptr
392 && (same_type_ignoring_top_level_qualifiers_p
393 (TREE_TYPE (TREE_TYPE (member
)),
394 current_class_type
)))
395 /* Delegating constructor. */
399 /* This is an initializer for an empty base; keep it for now so
400 we can check it in cxx_eval_bare_aggregate. */
401 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member
))));
404 if (TREE_CODE (member
) == ADDR_EXPR
)
405 member
= TREE_OPERAND (member
, 0);
406 if (TREE_CODE (member
) == COMPONENT_REF
)
408 tree aggr
= TREE_OPERAND (member
, 0);
409 if (TREE_CODE (aggr
) == VAR_DECL
)
410 /* Initializing a local variable, don't add anything. */
412 if (TREE_CODE (aggr
) != COMPONENT_REF
)
413 /* Normal member initialization. */
414 member
= TREE_OPERAND (member
, 1);
415 else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr
)))
416 /* Initializing a member of an anonymous union. */
417 return build_anon_member_initialization (member
, init
, vec
);
419 /* We're initializing a vtable pointer in a base. Leave it as
420 COMPONENT_REF so we remember the path to get to the vfield. */
421 gcc_assert (TREE_TYPE (member
) == vtbl_ptr_type_node
);
424 /* Value-initialization can produce multiple initializers for the
425 same field; use the last one. */
426 if (!vec_safe_is_empty (*vec
) && (*vec
)->last().index
== member
)
427 (*vec
)->last().value
= init
;
429 CONSTRUCTOR_APPEND_ELT (*vec
, member
, init
);
433 /* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval.
434 In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a
435 BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */
438 check_constexpr_bind_expr_vars (tree t
)
440 gcc_assert (TREE_CODE (t
) == BIND_EXPR
);
442 for (tree var
= BIND_EXPR_VARS (t
); var
; var
= DECL_CHAIN (var
))
443 if (TREE_CODE (var
) == TYPE_DECL
444 && DECL_IMPLICIT_TYPEDEF_P (var
)
445 && !LAMBDA_TYPE_P (TREE_TYPE (var
)))
450 /* Subroutine of check_constexpr_ctor_body. */
453 check_constexpr_ctor_body_1 (tree last
, tree list
)
455 switch (TREE_CODE (list
))
458 if (TREE_CODE (DECL_EXPR_DECL (list
)) == USING_DECL
459 || TREE_CODE (DECL_EXPR_DECL (list
)) == TYPE_DECL
)
463 case CLEANUP_POINT_EXPR
:
464 return check_constexpr_ctor_body (last
, TREE_OPERAND (list
, 0),
468 if (!check_constexpr_bind_expr_vars (list
)
469 || !check_constexpr_ctor_body (last
, BIND_EXPR_BODY (list
),
476 case DEBUG_BEGIN_STMT
:
484 /* Make sure that there are no statements after LAST in the constructor
485 body represented by LIST. */
488 check_constexpr_ctor_body (tree last
, tree list
, bool complain
)
490 /* C++14 doesn't require a constexpr ctor to have an empty body. */
491 if (cxx_dialect
>= cxx14
)
495 if (TREE_CODE (list
) == STATEMENT_LIST
)
497 tree_stmt_iterator i
= tsi_last (list
);
498 for (; !tsi_end_p (i
); tsi_prev (&i
))
500 tree t
= tsi_stmt (i
);
503 if (!check_constexpr_ctor_body_1 (last
, t
))
510 else if (list
!= last
511 && !check_constexpr_ctor_body_1 (last
, list
))
516 error ("%<constexpr%> constructor does not have empty body");
517 DECL_DECLARED_CONSTEXPR_P (current_function_decl
) = false;
522 /* V is a vector of constructor elements built up for the base and member
523 initializers of a constructor for TYPE. They need to be in increasing
524 offset order, which they might not be yet if TYPE has a primary base
525 which is not first in the base-clause or a vptr and at least one base
526 all of which are non-primary. */
528 static vec
<constructor_elt
, va_gc
> *
529 sort_constexpr_mem_initializers (tree type
, vec
<constructor_elt
, va_gc
> *v
)
531 tree pri
= CLASSTYPE_PRIMARY_BINFO (type
);
537 field_type
= BINFO_TYPE (pri
);
538 else if (TYPE_CONTAINS_VPTR_P (type
))
539 field_type
= vtbl_ptr_type_node
;
543 /* Find the element for the primary base or vptr and move it to the
544 beginning of the vec. */
545 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
546 if (TREE_TYPE (ce
->index
) == field_type
)
549 if (i
> 0 && i
< vec_safe_length (v
))
551 vec
<constructor_elt
, va_gc
> &vref
= *v
;
552 constructor_elt elt
= vref
[i
];
561 /* Build compile-time evalable representations of member-initializer list
562 for a constexpr constructor. */
565 build_constexpr_constructor_member_initializers (tree type
, tree body
)
567 vec
<constructor_elt
, va_gc
> *vec
= NULL
;
570 switch (TREE_CODE (body
))
572 case MUST_NOT_THROW_EXPR
:
574 body
= TREE_OPERAND (body
, 0);
578 for (tree_stmt_iterator i
= tsi_start (body
);
579 !tsi_end_p (i
); tsi_next (&i
))
582 if (TREE_CODE (body
) == BIND_EXPR
)
588 body
= BIND_EXPR_BODY (body
);
595 if (TREE_CODE (body
) == TRY_BLOCK
)
597 body
= TREE_OPERAND (body
, 0);
598 if (TREE_CODE (body
) == BIND_EXPR
)
599 body
= BIND_EXPR_BODY (body
);
601 if (TREE_CODE (body
) == CLEANUP_POINT_EXPR
)
603 body
= TREE_OPERAND (body
, 0);
604 if (TREE_CODE (body
) == EXPR_STMT
)
605 body
= TREE_OPERAND (body
, 0);
606 if (TREE_CODE (body
) == INIT_EXPR
607 && (same_type_ignoring_top_level_qualifiers_p
608 (TREE_TYPE (TREE_OPERAND (body
, 0)),
609 current_class_type
)))
612 return TREE_OPERAND (body
, 1);
614 ok
= build_data_member_initialization (body
, &vec
);
616 else if (TREE_CODE (body
) == STATEMENT_LIST
)
618 tree_stmt_iterator i
;
619 for (i
= tsi_start (body
); !tsi_end_p (i
); tsi_next (&i
))
621 ok
= build_data_member_initialization (tsi_stmt (i
), &vec
);
626 else if (EXPR_P (body
))
627 ok
= build_data_member_initialization (body
, &vec
);
629 gcc_assert (errorcount
> 0);
632 if (vec_safe_length (vec
) > 0)
634 /* In a delegating constructor, return the target. */
635 constructor_elt
*ce
= &(*vec
)[0];
636 if (ce
->index
== current_class_ptr
)
643 vec
= sort_constexpr_mem_initializers (type
, vec
);
644 return build_constructor (type
, vec
);
647 return error_mark_node
;
650 /* We have an expression tree T that represents a call, either CALL_EXPR
651 or AGGR_INIT_EXPR. If the call is lexically to a named function,
652 retrun the _DECL for that function. */
655 get_function_named_in_call (tree t
)
657 tree fun
= cp_get_callee (t
);
658 if (fun
&& TREE_CODE (fun
) == ADDR_EXPR
659 && TREE_CODE (TREE_OPERAND (fun
, 0)) == FUNCTION_DECL
)
660 fun
= TREE_OPERAND (fun
, 0);
664 /* Subroutine of check_constexpr_fundef. BODY is the body of a function
665 declared to be constexpr, or a sub-statement thereof. Returns the
666 return value if suitable, error_mark_node for a statement not allowed in
667 a constexpr function, or NULL_TREE if no return value was found. */
670 constexpr_fn_retval (tree body
)
672 switch (TREE_CODE (body
))
676 tree_stmt_iterator i
;
677 tree expr
= NULL_TREE
;
678 for (i
= tsi_start (body
); !tsi_end_p (i
); tsi_next (&i
))
680 tree s
= constexpr_fn_retval (tsi_stmt (i
));
681 if (s
== error_mark_node
)
682 return error_mark_node
;
683 else if (s
== NULL_TREE
)
684 /* Keep iterating. */;
686 /* Multiple return statements. */
687 return error_mark_node
;
695 return break_out_target_exprs (TREE_OPERAND (body
, 0));
699 tree decl
= DECL_EXPR_DECL (body
);
700 if (TREE_CODE (decl
) == USING_DECL
701 /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */
702 || DECL_ARTIFICIAL (decl
))
704 return error_mark_node
;
707 case CLEANUP_POINT_EXPR
:
708 return constexpr_fn_retval (TREE_OPERAND (body
, 0));
711 if (!check_constexpr_bind_expr_vars (body
))
712 return error_mark_node
;
713 return constexpr_fn_retval (BIND_EXPR_BODY (body
));
716 case DEBUG_BEGIN_STMT
:
721 tree fun
= get_function_named_in_call (body
);
723 && fndecl_built_in_p (fun
, BUILT_IN_UNREACHABLE
))
729 return error_mark_node
;
733 /* Subroutine of check_constexpr_fundef. BODY is the DECL_SAVED_TREE of
734 FUN; do the necessary transformations to turn it into a single expression
735 that we can store in the hash table. */
738 massage_constexpr_body (tree fun
, tree body
)
740 if (DECL_CONSTRUCTOR_P (fun
))
741 body
= build_constexpr_constructor_member_initializers
742 (DECL_CONTEXT (fun
), body
);
743 else if (cxx_dialect
< cxx14
)
745 if (TREE_CODE (body
) == EH_SPEC_BLOCK
)
746 body
= EH_SPEC_STMTS (body
);
747 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
748 body
= TREE_OPERAND (body
, 0);
749 body
= constexpr_fn_retval (body
);
754 /* CTYPE is a type constructed from BODY. Return true if some
755 bases/fields are uninitialized, and complain if COMPLAIN. */
758 cx_check_missing_mem_inits (tree ctype
, tree body
, bool complain
)
760 /* We allow uninitialized bases/fields in C++20. */
761 if (cxx_dialect
>= cxx20
)
768 if (TREE_CODE (body
) != CONSTRUCTOR
)
770 nelts
= CONSTRUCTOR_NELTS (body
);
772 tree field
= TYPE_FIELDS (ctype
);
774 if (TREE_CODE (ctype
) == UNION_TYPE
)
776 if (nelts
== 0 && next_initializable_field (field
))
779 error ("%<constexpr%> constructor for union %qT must "
780 "initialize exactly one non-static data member", ctype
);
786 /* Iterate over the CONSTRUCTOR, checking any missing fields don't
787 need an explicit initialization. */
789 for (unsigned i
= 0; i
<= nelts
; ++i
)
791 tree index
= NULL_TREE
;
794 index
= CONSTRUCTOR_ELT (body
, i
)->index
;
795 /* Skip base and vtable inits. */
796 if (TREE_CODE (index
) != FIELD_DECL
797 || DECL_ARTIFICIAL (index
))
801 for (; field
!= index
; field
= DECL_CHAIN (field
))
804 if (TREE_CODE (field
) != FIELD_DECL
)
806 if (DECL_UNNAMED_BIT_FIELD (field
))
808 if (DECL_ARTIFICIAL (field
))
810 if (ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
812 /* Recurse to check the anonymous aggregate member. */
813 bad
|= cx_check_missing_mem_inits
814 (TREE_TYPE (field
), NULL_TREE
, complain
);
815 if (bad
&& !complain
)
819 ftype
= TREE_TYPE (field
);
820 if (!ftype
|| !TYPE_P (ftype
) || !COMPLETE_TYPE_P (ftype
))
821 /* A flexible array can't be intialized here, so don't complain
824 if (DECL_SIZE (field
) && integer_zerop (DECL_SIZE (field
)))
825 /* An empty field doesn't need an initializer. */
827 ftype
= strip_array_types (ftype
);
828 if (type_has_constexpr_default_constructor (ftype
))
830 /* It's OK to skip a member with a trivial constexpr ctor.
831 A constexpr ctor that isn't trivial should have been
833 gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype
)
839 auto_diagnostic_group d
;
840 error ("member %qD must be initialized by mem-initializer "
841 "in %<constexpr%> constructor", field
);
842 inform (DECL_SOURCE_LOCATION (field
), "declared here");
845 if (field
== NULL_TREE
)
848 if (ANON_AGGR_TYPE_P (TREE_TYPE (index
)))
850 /* Check the anonymous aggregate initializer is valid. */
851 bad
|= cx_check_missing_mem_inits
852 (TREE_TYPE (index
), CONSTRUCTOR_ELT (body
, i
)->value
, complain
);
853 if (bad
&& !complain
)
856 field
= DECL_CHAIN (field
);
862 /* We are processing the definition of the constexpr function FUN.
863 Check that its body fulfills the apropriate requirements and
864 enter it in the constexpr function definition table. */
867 maybe_save_constexpr_fundef (tree fun
)
869 if (processing_template_decl
870 || !DECL_DECLARED_CONSTEXPR_P (fun
)
871 || cp_function_chain
->invalid_constexpr
872 || DECL_CLONED_FUNCTION_P (fun
))
875 if (!is_valid_constexpr_fn (fun
, !DECL_GENERATED_P (fun
)))
878 tree massaged
= massage_constexpr_body (fun
, DECL_SAVED_TREE (fun
));
879 if (massaged
== NULL_TREE
|| massaged
== error_mark_node
)
881 if (!DECL_CONSTRUCTOR_P (fun
))
882 error ("body of %<constexpr%> function %qD not a return-statement",
887 bool potential
= potential_rvalue_constant_expression (massaged
);
888 if (!potential
&& !DECL_GENERATED_P (fun
))
889 require_potential_rvalue_constant_expression (massaged
);
891 if (DECL_CONSTRUCTOR_P (fun
)
892 && cx_check_missing_mem_inits (DECL_CONTEXT (fun
),
893 massaged
, !DECL_GENERATED_P (fun
)))
896 if (!potential
&& !DECL_GENERATED_P (fun
))
899 constexpr_fundef entry
= {fun
, NULL_TREE
, NULL_TREE
, NULL_TREE
};
900 bool clear_ctx
= false;
901 if (DECL_RESULT (fun
) && DECL_CONTEXT (DECL_RESULT (fun
)) == NULL_TREE
)
904 DECL_CONTEXT (DECL_RESULT (fun
)) = fun
;
906 tree saved_fn
= current_function_decl
;
907 current_function_decl
= fun
;
908 entry
.body
= copy_fn (entry
.decl
, entry
.parms
, entry
.result
);
909 current_function_decl
= saved_fn
;
911 DECL_CONTEXT (DECL_RESULT (entry
.decl
)) = NULL_TREE
;
913 /* For a template instantiation, we want to remember the pre-generic body
914 for explain_invalid_constexpr_fn, but do tell cxx_eval_call_expression
915 that it doesn't need to bother trying to expand the function. */
916 entry
.result
= error_mark_node
;
918 register_constexpr_fundef (entry
);
921 /* BODY is a validated and massaged definition of a constexpr
922 function. Register it in the hash table. */
925 register_constexpr_fundef (const constexpr_fundef
&value
)
927 /* Create the constexpr function table if necessary. */
928 if (constexpr_fundef_table
== NULL
)
929 constexpr_fundef_table
930 = hash_table
<constexpr_fundef_hasher
>::create_ggc (101);
932 constexpr_fundef
**slot
= constexpr_fundef_table
->find_slot
933 (const_cast<constexpr_fundef
*> (&value
), INSERT
);
935 gcc_assert (*slot
== NULL
);
936 *slot
= ggc_alloc
<constexpr_fundef
> ();
940 /* FUN is a non-constexpr function called in a context that requires a
941 constant expression. If it comes from a constexpr template, explain why
942 the instantiation isn't constexpr. */
945 explain_invalid_constexpr_fn (tree fun
)
947 static hash_set
<tree
> *diagnosed
;
950 /* Only diagnose defaulted functions, lambdas, or instantiations. */
951 if (!DECL_DEFAULTED_FN (fun
)
952 && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun
))
953 && !is_instantiation_of_constexpr (fun
))
955 inform (DECL_SOURCE_LOCATION (fun
), "%qD declared here", fun
);
958 if (diagnosed
== NULL
)
959 diagnosed
= new hash_set
<tree
>;
960 if (diagnosed
->add (fun
))
961 /* Already explained. */
964 save_loc
= input_location
;
965 if (!lambda_static_thunk_p (fun
))
967 /* Diagnostics should completely ignore the static thunk, so leave
968 input_location set to our caller's location. */
969 input_location
= DECL_SOURCE_LOCATION (fun
);
970 inform (input_location
,
971 "%qD is not usable as a %<constexpr%> function because:", fun
);
973 /* First check the declaration. */
974 if (is_valid_constexpr_fn (fun
, true))
976 /* Then if it's OK, the body. */
977 if (!DECL_DECLARED_CONSTEXPR_P (fun
)
978 && DECL_DEFAULTED_FN (fun
))
979 explain_implicit_non_constexpr (fun
);
982 if (constexpr_fundef
*fd
= retrieve_constexpr_fundef (fun
))
985 body
= DECL_SAVED_TREE (fun
);
986 body
= massage_constexpr_body (fun
, body
);
987 require_potential_rvalue_constant_expression (body
);
988 if (DECL_CONSTRUCTOR_P (fun
))
989 cx_check_missing_mem_inits (DECL_CONTEXT (fun
), body
, true);
992 input_location
= save_loc
;
995 /* Objects of this type represent calls to constexpr functions
996 along with the bindings of parameters to their arguments, for
997 the purpose of compile time evaluation. */
999 struct GTY((for_user
)) constexpr_call
{
1000 /* Description of the constexpr function definition. */
1001 constexpr_fundef
*fundef
;
1002 /* Parameter bindings environment. A TREE_VEC of arguments. */
1004 /* Result of the call.
1005 NULL means the call is being evaluated.
1006 error_mark_node means that the evaluation was erroneous;
1007 otherwise, the actuall value of the call. */
1009 /* The hash of this call; we remember it here to avoid having to
1010 recalculate it when expanding the hash table. */
1012 /* Whether __builtin_is_constant_evaluated() should evaluate to true. */
1013 bool manifestly_const_eval
;
1016 struct constexpr_call_hasher
: ggc_ptr_hash
<constexpr_call
>
1018 static hashval_t
hash (constexpr_call
*);
1019 static bool equal (constexpr_call
*, constexpr_call
*);
1022 enum constexpr_switch_state
{
1023 /* Used when processing a switch for the first time by cxx_eval_switch_expr
1024 and default: label for that switch has not been seen yet. */
1025 css_default_not_seen
,
1026 /* Used when processing a switch for the first time by cxx_eval_switch_expr
1027 and default: label for that switch has been seen already. */
1029 /* Used when processing a switch for the second time by
1030 cxx_eval_switch_expr, where default: label should match. */
1031 css_default_processing
1034 /* The constexpr expansion context part which needs one instance per
1035 cxx_eval_outermost_constant_expr invocation. VALUES is a map of values of
1036 variables initialized within the expression. */
1038 struct constexpr_global_ctx
{
1039 /* Values for any temporaries or local variables within the
1040 constant-expression. */
1041 hash_map
<tree
,tree
> values
;
1042 /* Number of cxx_eval_constant_expression calls (except skipped ones,
1043 on simple constants or location wrappers) encountered during current
1044 cxx_eval_outermost_constant_expr call. */
1045 HOST_WIDE_INT constexpr_ops_count
;
1046 /* Heap VAR_DECLs created during the evaluation of the outermost constant
1048 auto_vec
<tree
, 16> heap_vars
;
1049 /* Cleanups that need to be evaluated at the end of CLEANUP_POINT_EXPR. */
1050 vec
<tree
> *cleanups
;
1051 /* Number of heap VAR_DECL deallocations. */
1052 unsigned heap_dealloc_count
;
1054 constexpr_global_ctx ()
1055 : constexpr_ops_count (0), cleanups (NULL
), heap_dealloc_count (0) {}
1058 /* The constexpr expansion context. CALL is the current function
1059 expansion, CTOR is the current aggregate initializer, OBJECT is the
1060 object being initialized by CTOR, either a VAR_DECL or a _REF. */
1062 struct constexpr_ctx
{
1063 /* The part of the context that needs to be unique to the whole
1064 cxx_eval_outermost_constant_expr invocation. */
1065 constexpr_global_ctx
*global
;
1066 /* The innermost call we're evaluating. */
1067 constexpr_call
*call
;
1068 /* SAVE_EXPRs and TARGET_EXPR_SLOT vars of TARGET_EXPRs that we've seen
1069 within the current LOOP_EXPR. NULL if we aren't inside a loop. */
1070 vec
<tree
> *save_exprs
;
1071 /* The CONSTRUCTOR we're currently building up for an aggregate
1074 /* The object we're building the CONSTRUCTOR for. */
1076 /* If inside SWITCH_EXPR. */
1077 constexpr_switch_state
*css_state
;
1078 /* The aggregate initialization context inside which this one is nested. This
1079 is used by lookup_placeholder to resolve PLACEHOLDER_EXPRs. */
1080 const constexpr_ctx
*parent
;
1082 /* Whether we should error on a non-constant expression or fail quietly.
1083 This flag needs to be here, but some of the others could move to global
1084 if they get larger than a word. */
1086 /* Whether we are strictly conforming to constant expression rules or
1087 trying harder to get a constant value. */
1089 /* Whether __builtin_is_constant_evaluated () should be true. */
1090 bool manifestly_const_eval
;
1093 /* This internal flag controls whether we should avoid doing anything during
1094 constexpr evaluation that would cause extra DECL_UID generation, such as
1095 template instantiation and function body copying. */
1097 static bool uid_sensitive_constexpr_evaluation_value
;
1099 /* An internal counter that keeps track of the number of times
1100 uid_sensitive_constexpr_evaluation_p returned true. */
1102 static unsigned uid_sensitive_constexpr_evaluation_true_counter
;
1104 /* The accessor for uid_sensitive_constexpr_evaluation_value which also
1105 increments the corresponding counter. */
1108 uid_sensitive_constexpr_evaluation_p ()
1110 if (uid_sensitive_constexpr_evaluation_value
)
1112 ++uid_sensitive_constexpr_evaluation_true_counter
;
1119 /* The default constructor for uid_sensitive_constexpr_evaluation_sentinel
1120 enables the internal flag for uid_sensitive_constexpr_evaluation_p
1121 during the lifetime of the sentinel object. Upon its destruction, the
1122 previous value of uid_sensitive_constexpr_evaluation_p is restored. */
1124 uid_sensitive_constexpr_evaluation_sentinel
1125 ::uid_sensitive_constexpr_evaluation_sentinel ()
1126 : ovr (uid_sensitive_constexpr_evaluation_value
, true)
1130 /* The default constructor for uid_sensitive_constexpr_evaluation_checker
1131 records the current number of times that uid_sensitive_constexpr_evaluation_p
1132 has been called and returned true. */
1134 uid_sensitive_constexpr_evaluation_checker
1135 ::uid_sensitive_constexpr_evaluation_checker ()
1136 : saved_counter (uid_sensitive_constexpr_evaluation_true_counter
)
1140 /* Returns true iff uid_sensitive_constexpr_evaluation_p is true, and
1141 some constexpr evaluation was restricted due to u_s_c_e_p being called
1142 and returning true during the lifetime of this checker object. */
1145 uid_sensitive_constexpr_evaluation_checker::evaluation_restricted_p () const
1147 return (uid_sensitive_constexpr_evaluation_value
1148 && saved_counter
!= uid_sensitive_constexpr_evaluation_true_counter
);
1152 /* A table of all constexpr calls that have been evaluated by the
1153 compiler in this translation unit. */
1155 static GTY (()) hash_table
<constexpr_call_hasher
> *constexpr_call_table
;
1157 static tree
cxx_eval_constant_expression (const constexpr_ctx
*, tree
,
1158 bool, bool *, bool *, tree
* = NULL
);
1160 /* Compute a hash value for a constexpr call representation. */
1163 constexpr_call_hasher::hash (constexpr_call
*info
)
1168 /* Return true if the objects pointed to by P and Q represent calls
1169 to the same constexpr function with the same arguments.
1170 Otherwise, return false. */
1173 constexpr_call_hasher::equal (constexpr_call
*lhs
, constexpr_call
*rhs
)
1177 if (lhs
->hash
!= rhs
->hash
)
1179 if (lhs
->manifestly_const_eval
!= rhs
->manifestly_const_eval
)
1181 if (!constexpr_fundef_hasher::equal (lhs
->fundef
, rhs
->fundef
))
1183 return cp_tree_equal (lhs
->bindings
, rhs
->bindings
);
1186 /* Initialize the constexpr call table, if needed. */
1189 maybe_initialize_constexpr_call_table (void)
1191 if (constexpr_call_table
== NULL
)
1192 constexpr_call_table
= hash_table
<constexpr_call_hasher
>::create_ggc (101);
1195 /* During constexpr CALL_EXPR evaluation, to avoid issues with sharing when
1196 a function happens to get called recursively, we unshare the callee
1197 function's body and evaluate this unshared copy instead of evaluating the
1200 FUNDEF_COPIES_TABLE is a per-function freelist of these unshared function
1201 copies. The underlying data structure of FUNDEF_COPIES_TABLE is a hash_map
1202 that's keyed off of the original FUNCTION_DECL and whose value is a
1203 TREE_LIST of this function's unused copies awaiting reuse.
1205 This is not GC-deletable to avoid GC affecting UID generation. */
1207 static GTY(()) decl_tree_map
*fundef_copies_table
;
1209 /* Reuse a copy or create a new unshared copy of the function FUN.
1210 Return this copy. We use a TREE_LIST whose PURPOSE is body, VALUE
1211 is parms, TYPE is result. */
1214 get_fundef_copy (constexpr_fundef
*fundef
)
1218 tree
*slot
= &(hash_map_safe_get_or_insert
<hm_ggc
>
1219 (fundef_copies_table
, fundef
->decl
, &existed
, 127));
1223 /* There is no cached function available, or in use. We can use
1224 the function directly. That the slot is now created records
1225 that this function is now in use. */
1226 copy
= build_tree_list (fundef
->body
, fundef
->parms
);
1227 TREE_TYPE (copy
) = fundef
->result
;
1229 else if (*slot
== NULL_TREE
)
1231 if (uid_sensitive_constexpr_evaluation_p ())
1234 /* We've already used the function itself, so make a copy. */
1235 copy
= build_tree_list (NULL
, NULL
);
1236 tree saved_body
= DECL_SAVED_TREE (fundef
->decl
);
1237 tree saved_parms
= DECL_ARGUMENTS (fundef
->decl
);
1238 tree saved_result
= DECL_RESULT (fundef
->decl
);
1239 tree saved_fn
= current_function_decl
;
1240 DECL_SAVED_TREE (fundef
->decl
) = fundef
->body
;
1241 DECL_ARGUMENTS (fundef
->decl
) = fundef
->parms
;
1242 DECL_RESULT (fundef
->decl
) = fundef
->result
;
1243 current_function_decl
= fundef
->decl
;
1244 TREE_PURPOSE (copy
) = copy_fn (fundef
->decl
, TREE_VALUE (copy
),
1246 current_function_decl
= saved_fn
;
1247 DECL_RESULT (fundef
->decl
) = saved_result
;
1248 DECL_ARGUMENTS (fundef
->decl
) = saved_parms
;
1249 DECL_SAVED_TREE (fundef
->decl
) = saved_body
;
1253 /* We have a cached function available. */
1255 *slot
= TREE_CHAIN (copy
);
1261 /* Save the copy COPY of function FUN for later reuse by
1262 get_fundef_copy(). By construction, there will always be an entry
1266 save_fundef_copy (tree fun
, tree copy
)
1268 tree
*slot
= fundef_copies_table
->get (fun
);
1269 TREE_CHAIN (copy
) = *slot
;
1273 /* We have an expression tree T that represents a call, either CALL_EXPR
1274 or AGGR_INIT_EXPR. Return the Nth argument. */
1277 get_nth_callarg (tree t
, int n
)
1279 switch (TREE_CODE (t
))
1282 return CALL_EXPR_ARG (t
, n
);
1284 case AGGR_INIT_EXPR
:
1285 return AGGR_INIT_EXPR_ARG (t
, n
);
1293 /* Attempt to evaluate T which represents a call to a builtin function.
1294 We assume here that all builtin functions evaluate to scalar types
1295 represented by _CST nodes. */
1298 cxx_eval_builtin_function_call (const constexpr_ctx
*ctx
, tree t
, tree fun
,
1300 bool *non_constant_p
, bool *overflow_p
)
1302 const int nargs
= call_expr_nargs (t
);
1303 tree
*args
= (tree
*) alloca (nargs
* sizeof (tree
));
1307 /* Don't fold __builtin_constant_p within a constexpr function. */
1308 bool bi_const_p
= DECL_IS_BUILTIN_CONSTANT_P (fun
);
1310 /* If we aren't requiring a constant expression, defer __builtin_constant_p
1311 in a constexpr function until we have values for the parameters. */
1313 && !ctx
->manifestly_const_eval
1314 && current_function_decl
1315 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
1317 *non_constant_p
= true;
1321 /* For __builtin_is_constant_evaluated, defer it if not
1322 ctx->manifestly_const_eval, otherwise fold it to true. */
1323 if (fndecl_built_in_p (fun
, CP_BUILT_IN_IS_CONSTANT_EVALUATED
,
1326 if (!ctx
->manifestly_const_eval
)
1328 *non_constant_p
= true;
1331 return boolean_true_node
;
1334 if (fndecl_built_in_p (fun
, CP_BUILT_IN_SOURCE_LOCATION
, BUILT_IN_FRONTEND
))
1336 temp_override
<tree
> ovr (current_function_decl
);
1337 if (ctx
->call
&& ctx
->call
->fundef
)
1338 current_function_decl
= ctx
->call
->fundef
->decl
;
1339 return fold_builtin_source_location (EXPR_LOCATION (t
));
1344 if (fndecl_built_in_p (fun
, BUILT_IN_NORMAL
))
1345 switch (DECL_FUNCTION_CODE (fun
))
1347 case BUILT_IN_STRLEN
:
1348 case BUILT_IN_STRNLEN
:
1351 case BUILT_IN_MEMCHR
:
1352 case BUILT_IN_STRCHR
:
1353 case BUILT_IN_STRRCHR
:
1357 case BUILT_IN_MEMCMP
:
1358 case BUILT_IN_STRCMP
:
1361 case BUILT_IN_STRSTR
:
1365 case BUILT_IN_ASAN_POINTER_COMPARE
:
1366 case BUILT_IN_ASAN_POINTER_SUBTRACT
:
1367 /* These builtins shall be ignored during constant expression
1374 /* Be permissive for arguments to built-ins; __builtin_constant_p should
1375 return constant false for a non-constant argument. */
1376 constexpr_ctx new_ctx
= *ctx
;
1377 new_ctx
.quiet
= true;
1378 for (i
= 0; i
< nargs
; ++i
)
1380 tree arg
= CALL_EXPR_ARG (t
, i
);
1383 /* To handle string built-ins we need to pass ADDR_EXPR<STRING_CST> since
1384 expand_builtin doesn't know how to look in the values table. */
1385 bool strop
= i
< strops
;
1389 if (TREE_CODE (arg
) == ADDR_EXPR
)
1390 arg
= TREE_OPERAND (arg
, 0);
1395 /* If builtin_valid_in_constant_expr_p is true,
1396 potential_constant_expression_1 has not recursed into the arguments
1397 of the builtin, verify it here. */
1398 if (!builtin_valid_in_constant_expr_p (fun
)
1399 || potential_constant_expression (arg
))
1401 bool dummy1
= false, dummy2
= false;
1402 arg
= cxx_eval_constant_expression (&new_ctx
, arg
, false,
1407 /* For __builtin_constant_p, fold all expressions with constant values
1408 even if they aren't C++ constant-expressions. */
1409 arg
= cp_fold_rvalue (arg
);
1412 if (TREE_CODE (arg
) == CONSTRUCTOR
)
1413 arg
= braced_lists_to_strings (TREE_TYPE (arg
), arg
);
1414 if (TREE_CODE (arg
) == STRING_CST
)
1415 arg
= build_address (arg
);
1423 bool save_ffbcp
= force_folding_builtin_constant_p
;
1424 force_folding_builtin_constant_p
|= ctx
->manifestly_const_eval
;
1425 tree save_cur_fn
= current_function_decl
;
1426 /* Return name of ctx->call->fundef->decl for __builtin_FUNCTION (). */
1427 if (fndecl_built_in_p (fun
, BUILT_IN_FUNCTION
)
1429 && ctx
->call
->fundef
)
1430 current_function_decl
= ctx
->call
->fundef
->decl
;
1431 new_call
= fold_builtin_call_array (EXPR_LOCATION (t
), TREE_TYPE (t
),
1432 CALL_EXPR_FN (t
), nargs
, args
);
1433 current_function_decl
= save_cur_fn
;
1434 force_folding_builtin_constant_p
= save_ffbcp
;
1435 if (new_call
== NULL
)
1437 if (!*non_constant_p
&& !ctx
->quiet
)
1439 /* Do not allow__builtin_unreachable in constexpr function.
1440 The __builtin_unreachable call with BUILTINS_LOCATION
1441 comes from cp_maybe_instrument_return. */
1442 if (fndecl_built_in_p (fun
, BUILT_IN_UNREACHABLE
)
1443 && EXPR_LOCATION (t
) == BUILTINS_LOCATION
)
1444 error ("%<constexpr%> call flows off the end of the function");
1447 new_call
= build_call_array_loc (EXPR_LOCATION (t
), TREE_TYPE (t
),
1448 CALL_EXPR_FN (t
), nargs
, args
);
1449 error ("%q+E is not a constant expression", new_call
);
1452 *non_constant_p
= true;
1456 if (!potential_constant_expression (new_call
))
1458 if (!*non_constant_p
&& !ctx
->quiet
)
1459 error ("%q+E is not a constant expression", new_call
);
1460 *non_constant_p
= true;
1466 /* memchr returns a pointer into the first argument, but we replaced the
1467 argument above with a STRING_CST; put it back it now. */
1468 tree op
= CALL_EXPR_ARG (t
, strret
-1);
1469 STRIP_NOPS (new_call
);
1470 if (TREE_CODE (new_call
) == POINTER_PLUS_EXPR
)
1471 TREE_OPERAND (new_call
, 0) = op
;
1472 else if (TREE_CODE (new_call
) == ADDR_EXPR
)
1476 return cxx_eval_constant_expression (&new_ctx
, new_call
, lval
,
1477 non_constant_p
, overflow_p
);
1480 /* TEMP is the constant value of a temporary object of type TYPE. Adjust
1481 the type of the value to match. */
1484 adjust_temp_type (tree type
, tree temp
)
1486 if (same_type_p (TREE_TYPE (temp
), type
))
1488 /* Avoid wrapping an aggregate value in a NOP_EXPR. */
1489 if (TREE_CODE (temp
) == CONSTRUCTOR
)
1491 /* build_constructor wouldn't retain various CONSTRUCTOR flags. */
1492 tree t
= copy_node (temp
);
1493 TREE_TYPE (t
) = type
;
1496 if (TREE_CODE (temp
) == EMPTY_CLASS_EXPR
)
1497 return build0 (EMPTY_CLASS_EXPR
, type
);
1498 gcc_assert (scalarish_type_p (type
));
1499 /* Now we know we're dealing with a scalar, and a prvalue of non-class
1500 type is cv-unqualified. */
1501 return cp_fold_convert (cv_unqualified (type
), temp
);
1504 /* If T is a CONSTRUCTOR, return an unshared copy of T and any
1505 sub-CONSTRUCTORs. Otherwise return T.
1507 We use this whenever we initialize an object as a whole, whether it's a
1508 parameter, a local variable, or a subobject, so that subsequent
1509 modifications don't affect other places where it was used. */
1512 unshare_constructor (tree t MEM_STAT_DECL
)
1514 if (!t
|| TREE_CODE (t
) != CONSTRUCTOR
)
1516 auto_vec
<tree
*, 4> ptrs
;
1517 ptrs
.safe_push (&t
);
1518 while (!ptrs
.is_empty ())
1520 tree
*p
= ptrs
.pop ();
1521 tree n
= copy_node (*p PASS_MEM_STAT
);
1522 CONSTRUCTOR_ELTS (n
) = vec_safe_copy (CONSTRUCTOR_ELTS (*p
) PASS_MEM_STAT
);
1524 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (n
);
1525 constructor_elt
*ce
;
1526 for (HOST_WIDE_INT i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
1527 if (ce
->value
&& TREE_CODE (ce
->value
) == CONSTRUCTOR
)
1528 ptrs
.safe_push (&ce
->value
);
1533 /* If T is a CONSTRUCTOR, ggc_free T and any sub-CONSTRUCTORs. */
1536 free_constructor (tree t
)
1538 if (!t
|| TREE_CODE (t
) != CONSTRUCTOR
)
1540 releasing_vec ctors
;
1541 vec_safe_push (ctors
, t
);
1542 while (!ctors
->is_empty ())
1544 tree c
= ctors
->pop ();
1545 if (vec
<constructor_elt
, va_gc
> *elts
= CONSTRUCTOR_ELTS (c
))
1547 constructor_elt
*ce
;
1548 for (HOST_WIDE_INT i
= 0; vec_safe_iterate (elts
, i
, &ce
); ++i
)
1549 if (TREE_CODE (ce
->value
) == CONSTRUCTOR
)
1550 vec_safe_push (ctors
, ce
->value
);
1557 /* Subroutine of cxx_eval_call_expression.
1558 We are processing a call expression (either CALL_EXPR or
1559 AGGR_INIT_EXPR) in the context of CTX. Evaluate
1560 all arguments and bind their values to correspondings
1561 parameters, making up the NEW_CALL context. */
1564 cxx_bind_parameters_in_call (const constexpr_ctx
*ctx
, tree t
,
1565 constexpr_call
*new_call
,
1566 bool *non_constant_p
, bool *overflow_p
,
1567 bool *non_constant_args
)
1569 const int nargs
= call_expr_nargs (t
);
1570 tree fun
= new_call
->fundef
->decl
;
1571 tree parms
= new_call
->fundef
->parms
;
1573 /* We don't record ellipsis args below. */
1574 int nparms
= list_length (parms
);
1575 int nbinds
= nargs
< nparms
? nargs
: nparms
;
1576 tree binds
= new_call
->bindings
= make_tree_vec (nbinds
);
1577 for (i
= 0; i
< nargs
; ++i
)
1580 tree type
= parms
? TREE_TYPE (parms
) : void_type_node
;
1581 x
= get_nth_callarg (t
, i
);
1582 /* For member function, the first argument is a pointer to the implied
1583 object. For a constructor, it might still be a dummy object, in
1584 which case we get the real argument from ctx. */
1585 if (i
== 0 && DECL_CONSTRUCTOR_P (fun
)
1586 && is_dummy_object (x
))
1589 x
= build_address (x
);
1591 if (TREE_ADDRESSABLE (type
))
1592 /* Undo convert_for_arg_passing work here. */
1593 x
= convert_from_reference (x
);
1594 arg
= cxx_eval_constant_expression (ctx
, x
, /*lval=*/false,
1595 non_constant_p
, overflow_p
);
1596 /* Don't VERIFY_CONSTANT here. */
1597 if (*non_constant_p
&& ctx
->quiet
)
1599 /* Just discard ellipsis args after checking their constantitude. */
1603 if (!*non_constant_p
)
1605 /* Make sure the binding has the same type as the parm. But
1606 only for constant args. */
1607 if (!TYPE_REF_P (type
))
1608 arg
= adjust_temp_type (type
, arg
);
1609 if (!TREE_CONSTANT (arg
))
1610 *non_constant_args
= true;
1611 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
1612 /* The destructor needs to see any modifications the callee makes
1614 *non_constant_args
= true;
1616 /* For virtual calls, adjust the this argument, so that it is
1617 the object on which the method is called, rather than
1618 one of its bases. */
1619 if (i
== 0 && DECL_VIRTUAL_P (fun
))
1623 if (TREE_CODE (addr
) == ADDR_EXPR
)
1625 tree obj
= TREE_OPERAND (addr
, 0);
1626 while (TREE_CODE (obj
) == COMPONENT_REF
1627 && DECL_FIELD_IS_BASE (TREE_OPERAND (obj
, 1))
1628 && !same_type_ignoring_top_level_qualifiers_p
1629 (TREE_TYPE (obj
), DECL_CONTEXT (fun
)))
1630 obj
= TREE_OPERAND (obj
, 0);
1631 if (obj
!= TREE_OPERAND (addr
, 0))
1632 arg
= build_fold_addr_expr_with_type (obj
,
1636 TREE_VEC_ELT (binds
, i
) = arg
;
1638 parms
= TREE_CHAIN (parms
);
1642 /* Variables and functions to manage constexpr call expansion context.
1643 These do not need to be marked for PCH or GC. */
1645 /* FIXME remember and print actual constant arguments. */
1646 static vec
<tree
> call_stack
;
1647 static int call_stack_tick
;
1648 static int last_cx_error_tick
;
1651 push_cx_call_context (tree call
)
1654 if (!EXPR_HAS_LOCATION (call
))
1655 SET_EXPR_LOCATION (call
, input_location
);
1656 call_stack
.safe_push (call
);
1657 int len
= call_stack
.length ();
1658 if (len
> max_constexpr_depth
)
1664 pop_cx_call_context (void)
1671 cx_error_context (void)
1673 vec
<tree
> r
= vNULL
;
1674 if (call_stack_tick
!= last_cx_error_tick
1675 && !call_stack
.is_empty ())
1677 last_cx_error_tick
= call_stack_tick
;
1681 /* Evaluate a call T to a GCC internal function when possible and return
1682 the evaluated result or, under the control of CTX, give an error, set
1683 NON_CONSTANT_P, and return the unevaluated call T otherwise. */
1686 cxx_eval_internal_function (const constexpr_ctx
*ctx
, tree t
,
1688 bool *non_constant_p
, bool *overflow_p
)
1690 enum tree_code opcode
= ERROR_MARK
;
1692 switch (CALL_EXPR_IFN (t
))
1694 case IFN_UBSAN_NULL
:
1695 case IFN_UBSAN_BOUNDS
:
1696 case IFN_UBSAN_VPTR
:
1697 case IFN_FALLTHROUGH
:
1700 case IFN_ADD_OVERFLOW
:
1703 case IFN_SUB_OVERFLOW
:
1704 opcode
= MINUS_EXPR
;
1706 case IFN_MUL_OVERFLOW
:
1711 return cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 0),
1712 false, non_constant_p
, overflow_p
);
1714 case IFN_VEC_CONVERT
:
1716 tree arg
= cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 0),
1717 false, non_constant_p
,
1719 if (TREE_CODE (arg
) == VECTOR_CST
)
1720 return fold_const_call (CFN_VEC_CONVERT
, TREE_TYPE (t
), arg
);
1723 *non_constant_p
= true;
1730 error_at (cp_expr_loc_or_input_loc (t
),
1731 "call to internal function %qE", t
);
1732 *non_constant_p
= true;
1736 /* Evaluate constant arguments using OPCODE and return a complex
1737 number containing the result and the overflow bit. */
1738 tree arg0
= cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 0), lval
,
1739 non_constant_p
, overflow_p
);
1740 tree arg1
= cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 1), lval
,
1741 non_constant_p
, overflow_p
);
1743 if (TREE_CODE (arg0
) == INTEGER_CST
&& TREE_CODE (arg1
) == INTEGER_CST
)
1745 location_t loc
= cp_expr_loc_or_input_loc (t
);
1746 tree type
= TREE_TYPE (TREE_TYPE (t
));
1747 tree result
= fold_binary_loc (loc
, opcode
, type
,
1748 fold_convert_loc (loc
, type
, arg0
),
1749 fold_convert_loc (loc
, type
, arg1
));
1751 = build_int_cst (type
, arith_overflowed_p (opcode
, type
, arg0
, arg1
));
1752 /* Reset TREE_OVERFLOW to avoid warnings for the overflow. */
1753 if (TREE_OVERFLOW (result
))
1754 TREE_OVERFLOW (result
) = 0;
1756 return build_complex (TREE_TYPE (t
), result
, ovf
);
1759 *non_constant_p
= true;
1763 /* Clean CONSTRUCTOR_NO_CLEARING from CTOR and its sub-aggregates. */
1766 clear_no_implicit_zero (tree ctor
)
1768 if (CONSTRUCTOR_NO_CLEARING (ctor
))
1770 CONSTRUCTOR_NO_CLEARING (ctor
) = false;
1771 tree elt
; unsigned HOST_WIDE_INT idx
;
1772 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor
), idx
, elt
)
1773 if (TREE_CODE (elt
) == CONSTRUCTOR
)
1774 clear_no_implicit_zero (elt
);
1778 /* Complain about a const object OBJ being modified in a constant expression.
1779 EXPR is the MODIFY_EXPR expression performing the modification. */
1782 modifying_const_object_error (tree expr
, tree obj
)
1784 location_t loc
= cp_expr_loc_or_input_loc (expr
);
1785 auto_diagnostic_group d
;
1786 error_at (loc
, "modifying a const object %qE is not allowed in "
1787 "a constant expression", TREE_OPERAND (expr
, 0));
1788 inform (location_of (obj
), "originally declared %<const%> here");
1791 /* Return true if FNDECL is a replaceable global allocation function that
1792 should be useable during constant expression evaluation. */
1795 cxx_replaceable_global_alloc_fn (tree fndecl
)
1797 return (cxx_dialect
>= cxx20
1798 && IDENTIFIER_NEWDEL_OP_P (DECL_NAME (fndecl
))
1799 && CP_DECL_CONTEXT (fndecl
) == global_namespace
1800 && (DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl
)
1801 || DECL_IS_OPERATOR_DELETE_P (fndecl
)));
1804 /* Return true if FNDECL is a placement new function that should be
1805 useable during constant expression evaluation of std::construct_at. */
1808 cxx_placement_new_fn (tree fndecl
)
1810 if (cxx_dialect
>= cxx20
1811 && IDENTIFIER_NEW_OP_P (DECL_NAME (fndecl
))
1812 && CP_DECL_CONTEXT (fndecl
) == global_namespace
1813 && !DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl
)
1814 && TREE_CODE (TREE_TYPE (fndecl
)) == FUNCTION_TYPE
)
1816 tree first_arg
= TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
1817 if (TREE_VALUE (first_arg
) == ptr_type_node
1818 && TREE_CHAIN (first_arg
) == void_list_node
)
1824 /* Return true if FNDECL is std::construct_at. */
1827 is_std_construct_at (tree fndecl
)
1829 if (!decl_in_std_namespace_p (fndecl
))
1832 tree name
= DECL_NAME (fndecl
);
1833 return name
&& id_equal (name
, "construct_at");
1836 /* Return true if FNDECL is std::allocator<T>::{,de}allocate. */
1839 is_std_allocator_allocate (tree fndecl
)
1841 tree name
= DECL_NAME (fndecl
);
1842 if (name
== NULL_TREE
1843 || !(id_equal (name
, "allocate") || id_equal (name
, "deallocate")))
1846 tree ctx
= DECL_CONTEXT (fndecl
);
1847 if (ctx
== NULL_TREE
|| !CLASS_TYPE_P (ctx
) || !TYPE_MAIN_DECL (ctx
))
1850 tree decl
= TYPE_MAIN_DECL (ctx
);
1851 name
= DECL_NAME (decl
);
1852 if (name
== NULL_TREE
|| !id_equal (name
, "allocator"))
1855 return decl_in_std_namespace_p (decl
);
1858 /* Return true if FNDECL is __dynamic_cast. */
1861 cxx_dynamic_cast_fn_p (tree fndecl
)
1863 return (cxx_dialect
>= cxx20
1864 && id_equal (DECL_NAME (fndecl
), "__dynamic_cast")
1865 && CP_DECL_CONTEXT (fndecl
) == global_namespace
);
1868 /* Often, we have an expression in the form of address + offset, e.g.
1869 "&_ZTV1A + 16". Extract the object from it, i.e. "_ZTV1A". */
1872 extract_obj_from_addr_offset (tree expr
)
1874 if (TREE_CODE (expr
) == POINTER_PLUS_EXPR
)
1875 expr
= TREE_OPERAND (expr
, 0);
1877 if (TREE_CODE (expr
) == ADDR_EXPR
)
1878 expr
= TREE_OPERAND (expr
, 0);
1882 /* Given a PATH like
1884 g.D.2181.D.2154.D.2102.D.2093
1886 find a component with type TYPE. Return NULL_TREE if not found, and
1887 error_mark_node if the component is not accessible. If STOP is non-null,
1888 this function will return NULL_TREE if STOP is found before TYPE. */
1891 get_component_with_type (tree path
, tree type
, tree stop
)
1895 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path
), type
))
1899 && (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path
),
1902 else if (TREE_CODE (path
) == COMPONENT_REF
1903 && DECL_FIELD_IS_BASE (TREE_OPERAND (path
, 1)))
1905 /* We need to check that the component we're accessing is in fact
1907 if (TREE_PRIVATE (TREE_OPERAND (path
, 1))
1908 || TREE_PROTECTED (TREE_OPERAND (path
, 1)))
1909 return error_mark_node
;
1910 path
= TREE_OPERAND (path
, 0);
1917 /* Evaluate a call to __dynamic_cast (permitted by P1327R1).
1919 The declaration of __dynamic_cast is:
1921 void* __dynamic_cast (const void* __src_ptr,
1922 const __class_type_info* __src_type,
1923 const __class_type_info* __dst_type,
1924 ptrdiff_t __src2dst);
1926 where src2dst has the following possible values
1928 >-1: src_type is a unique public non-virtual base of dst_type
1929 dst_ptr + src2dst == src_ptr
1930 -1: unspecified relationship
1931 -2: src_type is not a public base of dst_type
1932 -3: src_type is a multiple public non-virtual base of dst_type
1934 Since literal types can't have virtual bases, we only expect hint >=0,
1938 cxx_eval_dynamic_cast_fn (const constexpr_ctx
*ctx
, tree call
,
1939 bool *non_constant_p
, bool *overflow_p
)
1941 /* T will be something like
1942 __dynamic_cast ((B*) b, &_ZTI1B, &_ZTI1D, 8)
1944 gcc_assert (call_expr_nargs (call
) == 4);
1945 tsubst_flags_t complain
= ctx
->quiet
? tf_none
: tf_warning_or_error
;
1946 tree obj
= CALL_EXPR_ARG (call
, 0);
1947 tree type
= CALL_EXPR_ARG (call
, 2);
1948 HOST_WIDE_INT hint
= int_cst_value (CALL_EXPR_ARG (call
, 3));
1949 location_t loc
= cp_expr_loc_or_input_loc (call
);
1951 /* Get the target type of the dynamic_cast. */
1952 gcc_assert (TREE_CODE (type
) == ADDR_EXPR
);
1953 type
= TREE_OPERAND (type
, 0);
1954 type
= TREE_TYPE (DECL_NAME (type
));
1956 /* TYPE can only be either T* or T&. We can't know which of these it
1957 is by looking at TYPE, but OBJ will be "(T*) x" in the first case,
1958 and something like "(T*)(T&)(T*) x" in the second case. */
1959 bool reference_p
= false;
1960 while (CONVERT_EXPR_P (obj
) || TREE_CODE (obj
) == SAVE_EXPR
)
1962 reference_p
|= TYPE_REF_P (TREE_TYPE (obj
));
1963 obj
= TREE_OPERAND (obj
, 0);
1966 /* Evaluate the object so that we know its dynamic type. */
1967 obj
= cxx_eval_constant_expression (ctx
, obj
, /*lval*/false, non_constant_p
,
1969 if (*non_constant_p
)
1972 /* We expect OBJ to be in form of &d.D.2102 when HINT == 0,
1973 but when HINT is > 0, it can also be something like
1974 &d.D.2102 + 18446744073709551608, which includes the BINFO_OFFSET. */
1975 obj
= extract_obj_from_addr_offset (obj
);
1976 const tree objtype
= TREE_TYPE (obj
);
1977 /* If OBJ doesn't refer to a base field, we're done. */
1978 if (tree t
= (TREE_CODE (obj
) == COMPONENT_REF
1979 ? TREE_OPERAND (obj
, 1) : obj
))
1980 if (TREE_CODE (t
) != FIELD_DECL
|| !DECL_FIELD_IS_BASE (t
))
1986 error_at (loc
, "reference %<dynamic_cast%> failed");
1987 inform (loc
, "dynamic type %qT of its operand does "
1988 "not have a base class of type %qT",
1991 *non_constant_p
= true;
1993 return integer_zero_node
;
1996 /* [class.cdtor] When a dynamic_cast is used in a constructor ...
1997 or in a destructor ... if the operand of the dynamic_cast refers
1998 to the object under construction or destruction, this object is
1999 considered to be a most derived object that has the type of the
2000 constructor or destructor's class. */
2001 tree vtable
= build_vfield_ref (obj
, objtype
);
2002 vtable
= cxx_eval_constant_expression (ctx
, vtable
, /*lval*/false,
2003 non_constant_p
, overflow_p
);
2004 if (*non_constant_p
)
2006 /* With -fsanitize=vptr, we initialize all vtable pointers to null,
2007 so it's possible that we got a null pointer now. */
2008 if (integer_zerop (vtable
))
2011 error_at (loc
, "virtual table pointer is used uninitialized");
2012 *non_constant_p
= true;
2013 return integer_zero_node
;
2015 /* VTABLE will be &_ZTV1A + 16 or similar, get _ZTV1A. */
2016 vtable
= extract_obj_from_addr_offset (vtable
);
2017 const tree mdtype
= DECL_CONTEXT (vtable
);
2019 /* Given dynamic_cast<T>(v),
2021 [expr.dynamic.cast] If C is the class type to which T points or refers,
2022 the runtime check logically executes as follows:
2024 If, in the most derived object pointed (referred) to by v, v points
2025 (refers) to a public base class subobject of a C object, and if only
2026 one object of type C is derived from the subobject pointed (referred)
2027 to by v the result points (refers) to that C object.
2029 In this case, HINT >= 0 or -3. */
2030 if (hint
>= 0 || hint
== -3)
2032 /* Look for a component with type TYPE. */
2033 tree t
= get_component_with_type (obj
, type
, mdtype
);
2034 /* If not accessible, give an error. */
2035 if (t
== error_mark_node
)
2041 error_at (loc
, "reference %<dynamic_cast%> failed");
2042 inform (loc
, "static type %qT of its operand is a "
2043 "non-public base class of dynamic type %qT",
2047 *non_constant_p
= true;
2049 return integer_zero_node
;
2052 /* The result points to the TYPE object. */
2053 return cp_build_addr_expr (t
, complain
);
2054 /* Else, TYPE was not found, because the HINT turned out to be wrong.
2055 Fall through to the normal processing. */
2058 /* Otherwise, if v points (refers) to a public base class subobject of the
2059 most derived object, and the type of the most derived object has a base
2060 class, of type C, that is unambiguous and public, the result points
2061 (refers) to the C subobject of the most derived object.
2063 But it can also be an invalid case. */
2065 /* Get the most derived object. */
2066 obj
= get_component_with_type (obj
, mdtype
, NULL_TREE
);
2067 if (obj
== error_mark_node
)
2073 error_at (loc
, "reference %<dynamic_cast%> failed");
2074 inform (loc
, "static type %qT of its operand is a non-public"
2075 " base class of dynamic type %qT", objtype
, mdtype
);
2077 *non_constant_p
= true;
2079 return integer_zero_node
;
2084 /* Check that the type of the most derived object has a base class
2085 of type TYPE that is unambiguous and public. */
2087 tree binfo
= lookup_base (mdtype
, type
, ba_check
, &b_kind
, tf_none
);
2088 if (!binfo
|| binfo
== error_mark_node
)
2094 error_at (loc
, "reference %<dynamic_cast%> failed");
2095 if (b_kind
== bk_ambig
)
2096 inform (loc
, "%qT is an ambiguous base class of dynamic "
2097 "type %qT of its operand", type
, mdtype
);
2099 inform (loc
, "dynamic type %qT of its operand does not "
2100 "have an unambiguous public base class %qT",
2103 *non_constant_p
= true;
2105 return integer_zero_node
;
2107 /* If so, return the TYPE subobject of the most derived object. */
2108 obj
= convert_to_base_statically (obj
, binfo
);
2109 return cp_build_addr_expr (obj
, complain
);
2112 /* Data structure used by replace_result_decl and replace_result_decl_r. */
2114 struct replace_result_decl_data
2116 /* The RESULT_DECL we want to replace. */
2118 /* The replacement for DECL. */
2120 /* Whether we've performed any replacements. */
2124 /* Helper function for replace_result_decl, called through cp_walk_tree. */
2127 replace_result_decl_r (tree
*tp
, int *walk_subtrees
, void *data
)
2129 replace_result_decl_data
*d
= (replace_result_decl_data
*) data
;
2133 *tp
= unshare_expr (d
->replacement
);
2137 else if (TYPE_P (*tp
))
2143 /* Replace every occurrence of DECL, a RESULT_DECL, with (an unshared copy of)
2144 REPLACEMENT within the reduced constant expression *TP. Returns true iff a
2145 replacement was performed. */
2148 replace_result_decl (tree
*tp
, tree decl
, tree replacement
)
2150 gcc_checking_assert (TREE_CODE (decl
) == RESULT_DECL
2151 && (same_type_ignoring_top_level_qualifiers_p
2152 (TREE_TYPE (decl
), TREE_TYPE (replacement
))));
2153 replace_result_decl_data data
= { decl
, replacement
, false };
2154 cp_walk_tree_without_duplicates (tp
, replace_result_decl_r
, &data
);
2155 return data
.changed
;
2158 /* Evaluate the call T to virtual function thunk THUNK_FNDECL. */
2161 cxx_eval_thunk_call (const constexpr_ctx
*ctx
, tree t
, tree thunk_fndecl
,
2163 bool *non_constant_p
, bool *overflow_p
)
2165 tree function
= THUNK_TARGET (thunk_fndecl
);
2167 /* virtual_offset is only set in the presence of virtual bases, which make
2168 the class non-literal, so we don't need to handle it here. */
2169 if (THUNK_VIRTUAL_OFFSET (thunk_fndecl
))
2171 gcc_assert (!DECL_DECLARED_CONSTEXPR_P (function
));
2174 error ("call to non-%<constexpr%> function %qD", function
);
2175 explain_invalid_constexpr_fn (function
);
2177 *non_constant_p
= true;
2181 tree new_call
= copy_node (t
);
2182 CALL_EXPR_FN (new_call
) = function
;
2183 TREE_TYPE (new_call
) = TREE_TYPE (TREE_TYPE (function
));
2185 tree offset
= size_int (THUNK_FIXED_OFFSET (thunk_fndecl
));
2187 if (DECL_THIS_THUNK_P (thunk_fndecl
))
2189 /* 'this'-adjusting thunk. */
2190 tree this_arg
= CALL_EXPR_ARG (t
, 0);
2191 this_arg
= build2 (POINTER_PLUS_EXPR
, TREE_TYPE (this_arg
),
2193 CALL_EXPR_ARG (new_call
, 0) = this_arg
;
2196 /* Return-adjusting thunk. */
2197 new_call
= build2 (POINTER_PLUS_EXPR
, TREE_TYPE (new_call
),
2200 return cxx_eval_constant_expression (ctx
, new_call
, lval
,
2201 non_constant_p
, overflow_p
);
2204 /* If OBJECT is of const class type, evaluate it to a CONSTRUCTOR and set
2205 its TREE_READONLY flag according to READONLY_P. Used for constexpr
2206 'tors to detect modifying const objects in a constexpr context. */
2209 cxx_set_object_constness (const constexpr_ctx
*ctx
, tree object
,
2210 bool readonly_p
, bool *non_constant_p
,
2213 if (CLASS_TYPE_P (TREE_TYPE (object
))
2214 && CP_TYPE_CONST_P (TREE_TYPE (object
)))
2216 /* Subobjects might not be stored in ctx->global->values but we
2217 can get its CONSTRUCTOR by evaluating *this. */
2218 tree e
= cxx_eval_constant_expression (ctx
, object
, /*lval*/false,
2219 non_constant_p
, overflow_p
);
2220 if (TREE_CODE (e
) == CONSTRUCTOR
&& !*non_constant_p
)
2221 TREE_READONLY (e
) = readonly_p
;
2225 /* Subroutine of cxx_eval_constant_expression.
2226 Evaluate the call expression tree T in the context of OLD_CALL expression
2230 cxx_eval_call_expression (const constexpr_ctx
*ctx
, tree t
,
2232 bool *non_constant_p
, bool *overflow_p
)
2234 /* Handle concept checks separately. */
2235 if (concept_check_p (t
))
2236 return evaluate_concept_check (t
, tf_warning_or_error
);
2238 location_t loc
= cp_expr_loc_or_input_loc (t
);
2239 tree fun
= get_function_named_in_call (t
);
2240 constexpr_call new_call
2241 = { NULL
, NULL
, NULL
, 0, ctx
->manifestly_const_eval
};
2244 if (fun
== NULL_TREE
)
2245 return cxx_eval_internal_function (ctx
, t
, lval
,
2246 non_constant_p
, overflow_p
);
2248 if (TREE_CODE (fun
) != FUNCTION_DECL
)
2250 /* Might be a constexpr function pointer. */
2251 fun
= cxx_eval_constant_expression (ctx
, fun
,
2252 /*lval*/false, non_constant_p
,
2255 if (TREE_CODE (fun
) == ADDR_EXPR
)
2256 fun
= TREE_OPERAND (fun
, 0);
2257 /* For TARGET_VTABLE_USES_DESCRIPTORS targets, there is no
2258 indirection, the called expression is a pointer into the
2259 virtual table which should contain FDESC_EXPR. Extract the
2260 FUNCTION_DECL from there. */
2261 else if (TARGET_VTABLE_USES_DESCRIPTORS
2262 && TREE_CODE (fun
) == POINTER_PLUS_EXPR
2263 && TREE_CODE (TREE_OPERAND (fun
, 0)) == ADDR_EXPR
2264 && TREE_CODE (TREE_OPERAND (fun
, 1)) == INTEGER_CST
)
2266 tree d
= TREE_OPERAND (TREE_OPERAND (fun
, 0), 0);
2268 && DECL_VTABLE_OR_VTT_P (d
)
2269 && TREE_CODE (TREE_TYPE (d
)) == ARRAY_TYPE
2270 && TREE_TYPE (TREE_TYPE (d
)) == vtable_entry_type
2272 && TREE_CODE (DECL_INITIAL (d
)) == CONSTRUCTOR
)
2274 tree i
= int_const_binop (TRUNC_DIV_EXPR
, TREE_OPERAND (fun
, 1),
2275 TYPE_SIZE_UNIT (vtable_entry_type
));
2276 HOST_WIDE_INT idx
= find_array_ctor_elt (DECL_INITIAL (d
), i
);
2280 = (*CONSTRUCTOR_ELTS (DECL_INITIAL (d
)))[idx
].value
;
2281 if (TREE_CODE (fdesc
) == FDESC_EXPR
2282 && integer_zerop (TREE_OPERAND (fdesc
, 1)))
2283 fun
= TREE_OPERAND (fdesc
, 0);
2288 if (TREE_CODE (fun
) != FUNCTION_DECL
)
2290 if (!ctx
->quiet
&& !*non_constant_p
)
2291 error_at (loc
, "expression %qE does not designate a %<constexpr%> "
2293 *non_constant_p
= true;
2296 if (DECL_CLONED_FUNCTION_P (fun
))
2297 fun
= DECL_CLONED_FUNCTION (fun
);
2299 if (is_ubsan_builtin_p (fun
))
2302 if (fndecl_built_in_p (fun
))
2303 return cxx_eval_builtin_function_call (ctx
, t
, fun
,
2304 lval
, non_constant_p
, overflow_p
);
2305 if (DECL_THUNK_P (fun
))
2306 return cxx_eval_thunk_call (ctx
, t
, fun
, lval
, non_constant_p
, overflow_p
);
2307 if (!DECL_DECLARED_CONSTEXPR_P (fun
))
2309 if (TREE_CODE (t
) == CALL_EXPR
2310 && cxx_replaceable_global_alloc_fn (fun
)
2311 && (CALL_FROM_NEW_OR_DELETE_P (t
)
2313 && ctx
->call
->fundef
2314 && is_std_allocator_allocate (ctx
->call
->fundef
->decl
))))
2316 const int nargs
= call_expr_nargs (t
);
2317 tree arg0
= NULL_TREE
;
2318 for (int i
= 0; i
< nargs
; ++i
)
2320 tree arg
= CALL_EXPR_ARG (t
, i
);
2321 arg
= cxx_eval_constant_expression (ctx
, arg
, false,
2322 non_constant_p
, overflow_p
);
2323 VERIFY_CONSTANT (arg
);
2328 if (IDENTIFIER_NEW_OP_P (DECL_NAME (fun
)))
2330 tree type
= build_array_type_nelts (char_type_node
,
2331 tree_to_uhwi (arg0
));
2332 tree var
= build_decl (loc
, VAR_DECL
,
2333 (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun
))
2335 ? heap_vec_uninit_identifier
2336 : heap_uninit_identifier
,
2338 DECL_ARTIFICIAL (var
) = 1;
2339 TREE_STATIC (var
) = 1;
2340 ctx
->global
->heap_vars
.safe_push (var
);
2341 ctx
->global
->values
.put (var
, NULL_TREE
);
2342 return fold_convert (ptr_type_node
, build_address (var
));
2347 if (TREE_CODE (arg0
) == ADDR_EXPR
2348 && VAR_P (TREE_OPERAND (arg0
, 0)))
2350 tree var
= TREE_OPERAND (arg0
, 0);
2351 if (DECL_NAME (var
) == heap_uninit_identifier
2352 || DECL_NAME (var
) == heap_identifier
)
2354 if (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun
))
2359 error_at (loc
, "array deallocation of object "
2360 "allocated with non-array "
2362 inform (DECL_SOURCE_LOCATION (var
),
2363 "allocation performed here");
2365 *non_constant_p
= true;
2368 DECL_NAME (var
) = heap_deleted_identifier
;
2369 ctx
->global
->values
.remove (var
);
2370 ctx
->global
->heap_dealloc_count
++;
2373 else if (DECL_NAME (var
) == heap_vec_uninit_identifier
2374 || DECL_NAME (var
) == heap_vec_identifier
)
2376 if ((IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun
))
2377 & OVL_OP_FLAG_VEC
) == 0)
2381 error_at (loc
, "non-array deallocation of "
2382 "object allocated with array "
2384 inform (DECL_SOURCE_LOCATION (var
),
2385 "allocation performed here");
2387 *non_constant_p
= true;
2390 DECL_NAME (var
) = heap_deleted_identifier
;
2391 ctx
->global
->values
.remove (var
);
2392 ctx
->global
->heap_dealloc_count
++;
2395 else if (DECL_NAME (var
) == heap_deleted_identifier
)
2398 error_at (loc
, "deallocation of already deallocated "
2400 *non_constant_p
= true;
2405 error_at (loc
, "deallocation of storage that was "
2406 "not previously allocated");
2407 *non_constant_p
= true;
2411 /* Allow placement new in std::construct_at, just return the second
2413 if (TREE_CODE (t
) == CALL_EXPR
2414 && cxx_placement_new_fn (fun
)
2416 && ctx
->call
->fundef
2417 && is_std_construct_at (ctx
->call
->fundef
->decl
))
2419 const int nargs
= call_expr_nargs (t
);
2420 tree arg1
= NULL_TREE
;
2421 for (int i
= 0; i
< nargs
; ++i
)
2423 tree arg
= CALL_EXPR_ARG (t
, i
);
2424 arg
= cxx_eval_constant_expression (ctx
, arg
, false,
2425 non_constant_p
, overflow_p
);
2429 VERIFY_CONSTANT (arg
);
2434 else if (cxx_dynamic_cast_fn_p (fun
))
2435 return cxx_eval_dynamic_cast_fn (ctx
, t
, non_constant_p
, overflow_p
);
2439 if (!lambda_static_thunk_p (fun
))
2440 error_at (loc
, "call to non-%<constexpr%> function %qD", fun
);
2441 explain_invalid_constexpr_fn (fun
);
2443 *non_constant_p
= true;
2447 constexpr_ctx new_ctx
= *ctx
;
2448 if (DECL_CONSTRUCTOR_P (fun
) && !ctx
->object
2449 && TREE_CODE (t
) == AGGR_INIT_EXPR
)
2451 /* We want to have an initialization target for an AGGR_INIT_EXPR.
2452 If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
2453 new_ctx
.object
= AGGR_INIT_EXPR_SLOT (t
);
2454 tree ctor
= new_ctx
.ctor
= build_constructor (DECL_CONTEXT (fun
), NULL
);
2455 CONSTRUCTOR_NO_CLEARING (ctor
) = true;
2456 ctx
->global
->values
.put (new_ctx
.object
, ctor
);
2460 /* Shortcut trivial constructor/op=. */
2461 if (trivial_fn_p (fun
))
2463 tree init
= NULL_TREE
;
2464 if (call_expr_nargs (t
) == 2)
2465 init
= convert_from_reference (get_nth_callarg (t
, 1));
2466 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
2467 && AGGR_INIT_ZERO_FIRST (t
))
2468 init
= build_zero_init (DECL_CONTEXT (fun
), NULL_TREE
, false);
2471 tree op
= get_nth_callarg (t
, 0);
2472 if (is_dummy_object (op
))
2475 op
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (op
)), op
);
2476 tree set
= build2 (MODIFY_EXPR
, TREE_TYPE (op
), op
, init
);
2477 new_ctx
.call
= &new_call
;
2478 return cxx_eval_constant_expression (&new_ctx
, set
, lval
,
2479 non_constant_p
, overflow_p
);
2483 /* We can't defer instantiating the function any longer. */
2484 if (!DECL_INITIAL (fun
)
2485 && DECL_TEMPLOID_INSTANTIATION (fun
)
2486 && !uid_sensitive_constexpr_evaluation_p ())
2488 location_t save_loc
= input_location
;
2489 input_location
= loc
;
2491 instantiate_decl (fun
, /*defer_ok*/false, /*expl_inst*/false);
2493 input_location
= save_loc
;
2496 /* If in direct recursive call, optimize definition search. */
2497 if (ctx
&& ctx
->call
&& ctx
->call
->fundef
&& ctx
->call
->fundef
->decl
== fun
)
2498 new_call
.fundef
= ctx
->call
->fundef
;
2501 new_call
.fundef
= retrieve_constexpr_fundef (fun
);
2502 if (new_call
.fundef
== NULL
|| new_call
.fundef
->body
== NULL
2503 || new_call
.fundef
->result
== error_mark_node
2504 || fun
== current_function_decl
)
2508 /* We need to check for current_function_decl here in case we're
2509 being called during cp_fold_function, because at that point
2510 DECL_INITIAL is set properly and we have a fundef but we
2511 haven't lowered invisirefs yet (c++/70344). */
2512 if (DECL_INITIAL (fun
) == error_mark_node
2513 || fun
== current_function_decl
)
2514 error_at (loc
, "%qD called in a constant expression before its "
2515 "definition is complete", fun
);
2516 else if (DECL_INITIAL (fun
))
2518 /* The definition of fun was somehow unsuitable. But pretend
2519 that lambda static thunks don't exist. */
2520 if (!lambda_static_thunk_p (fun
))
2521 error_at (loc
, "%qD called in a constant expression", fun
);
2522 explain_invalid_constexpr_fn (fun
);
2525 error_at (loc
, "%qD used before its definition", fun
);
2527 *non_constant_p
= true;
2532 bool non_constant_args
= false;
2533 cxx_bind_parameters_in_call (ctx
, t
, &new_call
,
2534 non_constant_p
, overflow_p
, &non_constant_args
);
2536 /* We build up the bindings list before we know whether we already have this
2537 call cached. If we don't end up saving these bindings, ggc_free them when
2538 this function exits. */
2543 free_bindings (tree
&b
): bindings (&b
) { }
2544 ~free_bindings () { if (bindings
) ggc_free (*bindings
); }
2545 void preserve () { bindings
= NULL
; }
2546 } fb (new_call
.bindings
);
2548 if (*non_constant_p
)
2551 depth_ok
= push_cx_call_context (t
);
2553 /* Remember the object we are constructing or destructing. */
2554 tree new_obj
= NULL_TREE
;
2555 if (DECL_CONSTRUCTOR_P (fun
) || DECL_DESTRUCTOR_P (fun
))
2557 /* In a cdtor, it should be the first `this' argument.
2558 At this point it has already been evaluated in the call
2559 to cxx_bind_parameters_in_call. */
2560 new_obj
= TREE_VEC_ELT (new_call
.bindings
, 0);
2561 STRIP_NOPS (new_obj
);
2562 if (TREE_CODE (new_obj
) == ADDR_EXPR
)
2563 new_obj
= TREE_OPERAND (new_obj
, 0);
2565 if (ctx
->call
&& ctx
->call
->fundef
2566 && DECL_CONSTRUCTOR_P (ctx
->call
->fundef
->decl
))
2568 tree cur_obj
= TREE_VEC_ELT (ctx
->call
->bindings
, 0);
2569 STRIP_NOPS (cur_obj
);
2570 if (TREE_CODE (cur_obj
) == ADDR_EXPR
)
2571 cur_obj
= TREE_OPERAND (cur_obj
, 0);
2572 if (new_obj
== cur_obj
)
2573 /* We're calling the target constructor of a delegating
2574 constructor, or accessing a base subobject through a
2575 NOP_EXPR as part of a call to a base constructor, so
2576 there is no new (sub)object. */
2577 new_obj
= NULL_TREE
;
2581 tree result
= NULL_TREE
;
2583 constexpr_call
*entry
= NULL
;
2584 if (depth_ok
&& !non_constant_args
&& ctx
->strict
)
2586 new_call
.hash
= constexpr_fundef_hasher::hash (new_call
.fundef
);
2588 = iterative_hash_template_arg (new_call
.bindings
, new_call
.hash
);
2590 = iterative_hash_object (ctx
->manifestly_const_eval
, new_call
.hash
);
2592 /* If we have seen this call before, we are done. */
2593 maybe_initialize_constexpr_call_table ();
2594 constexpr_call
**slot
2595 = constexpr_call_table
->find_slot (&new_call
, INSERT
);
2599 /* Only cache up to constexpr_cache_depth to limit memory use. */
2600 if (depth_ok
< constexpr_cache_depth
)
2602 /* We need to keep a pointer to the entry, not just the slot, as
2603 the slot can move during evaluation of the body. */
2604 *slot
= entry
= ggc_alloc
<constexpr_call
> ();
2609 /* Calls that are in progress have their result set to NULL, so that we
2610 can detect circular dependencies. Now that we only cache up to
2611 constexpr_cache_depth this won't catch circular dependencies that
2612 start deeper, but they'll hit the recursion or ops limit. */
2613 else if (entry
->result
== NULL
)
2616 error ("call has circular dependency");
2617 *non_constant_p
= true;
2618 entry
->result
= result
= error_mark_node
;
2621 result
= entry
->result
;
2627 error ("%<constexpr%> evaluation depth exceeds maximum of %d (use "
2628 "%<-fconstexpr-depth=%> to increase the maximum)",
2629 max_constexpr_depth
);
2630 *non_constant_p
= true;
2631 result
= error_mark_node
;
2635 bool cacheable
= true;
2636 if (result
&& result
!= error_mark_node
)
2638 else if (!DECL_SAVED_TREE (fun
))
2640 /* When at_eof >= 2, cgraph has started throwing away
2641 DECL_SAVED_TREE, so fail quietly. FIXME we get here because of
2642 late code generation for VEC_INIT_EXPR, which needs to be
2643 completely reconsidered. */
2644 gcc_assert (at_eof
>= 2 && ctx
->quiet
);
2645 *non_constant_p
= true;
2647 else if (tree copy
= get_fundef_copy (new_call
.fundef
))
2649 tree body
, parms
, res
;
2650 releasing_vec ctors
;
2652 /* Reuse or create a new unshared copy of this function's body. */
2653 body
= TREE_PURPOSE (copy
);
2654 parms
= TREE_VALUE (copy
);
2655 res
= TREE_TYPE (copy
);
2657 /* Associate the bindings with the remapped parms. */
2658 tree bound
= new_call
.bindings
;
2659 tree remapped
= parms
;
2660 for (int i
= 0; i
< TREE_VEC_LENGTH (bound
); ++i
)
2662 tree arg
= TREE_VEC_ELT (bound
, i
);
2665 /* Unshare args going into the hash table to separate them
2666 from the caller's context, for better GC and to avoid
2667 problems with verify_gimple. */
2668 arg
= unshare_expr_without_location (arg
);
2669 TREE_VEC_ELT (bound
, i
) = arg
;
2671 /* And then unshare again so the callee doesn't change the
2672 argument values in the hash table. XXX Could we unshare
2673 lazily in cxx_eval_store_expression? */
2674 arg
= unshare_constructor (arg
);
2675 if (TREE_CODE (arg
) == CONSTRUCTOR
)
2676 vec_safe_push (ctors
, arg
);
2678 ctx
->global
->values
.put (remapped
, arg
);
2679 remapped
= DECL_CHAIN (remapped
);
2681 /* Add the RESULT_DECL to the values map, too. */
2682 gcc_assert (!DECL_BY_REFERENCE (res
));
2683 ctx
->global
->values
.put (res
, NULL_TREE
);
2685 /* Track the callee's evaluated SAVE_EXPRs and TARGET_EXPRs so that
2686 we can forget their values after the call. */
2687 constexpr_ctx ctx_with_save_exprs
= *ctx
;
2688 auto_vec
<tree
, 10> save_exprs
;
2689 ctx_with_save_exprs
.save_exprs
= &save_exprs
;
2690 ctx_with_save_exprs
.call
= &new_call
;
2691 unsigned save_heap_alloc_count
= ctx
->global
->heap_vars
.length ();
2692 unsigned save_heap_dealloc_count
= ctx
->global
->heap_dealloc_count
;
2694 /* If this is a constexpr destructor, the object's const and volatile
2695 semantics are no longer in effect; see [class.dtor]p5. */
2696 if (new_obj
&& DECL_DESTRUCTOR_P (fun
))
2697 cxx_set_object_constness (ctx
, new_obj
, /*readonly_p=*/false,
2698 non_constant_p
, overflow_p
);
2700 tree jump_target
= NULL_TREE
;
2701 cxx_eval_constant_expression (&ctx_with_save_exprs
, body
,
2702 lval
, non_constant_p
, overflow_p
,
2705 if (DECL_CONSTRUCTOR_P (fun
))
2706 /* This can be null for a subobject constructor call, in
2707 which case what we care about is the initialization
2708 side-effects rather than the value. We could get at the
2709 value by evaluating *this, but we don't bother; there's
2710 no need to put such a call in the hash table. */
2711 result
= lval
? ctx
->object
: ctx
->ctor
;
2712 else if (VOID_TYPE_P (TREE_TYPE (res
)))
2716 result
= *ctx
->global
->values
.get (res
);
2717 if (result
== NULL_TREE
&& !*non_constant_p
)
2720 error ("%<constexpr%> call flows off the end "
2722 *non_constant_p
= true;
2726 /* At this point, the object's constructor will have run, so
2727 the object is no longer under construction, and its possible
2728 'const' semantics now apply. Make a note of this fact by
2729 marking the CONSTRUCTOR TREE_READONLY. */
2730 if (new_obj
&& DECL_CONSTRUCTOR_P (fun
))
2731 cxx_set_object_constness (ctx
, new_obj
, /*readonly_p=*/true,
2732 non_constant_p
, overflow_p
);
2734 /* Forget the saved values of the callee's SAVE_EXPRs and
2738 FOR_EACH_VEC_ELT (save_exprs
, i
, save_expr
)
2739 ctx
->global
->values
.remove (save_expr
);
2741 /* Remove the parms/result from the values map. Is it worth
2742 bothering to do this when the map itself is only live for
2743 one constexpr evaluation? If so, maybe also clear out
2744 other vars from call, maybe in BIND_EXPR handling? */
2745 ctx
->global
->values
.remove (res
);
2746 for (tree parm
= parms
; parm
; parm
= TREE_CHAIN (parm
))
2747 ctx
->global
->values
.remove (parm
);
2749 /* Free any parameter CONSTRUCTORs we aren't returning directly. */
2750 while (!ctors
->is_empty ())
2752 tree c
= ctors
->pop ();
2754 free_constructor (c
);
2757 /* Make the unshared function copy we used available for re-use. */
2758 save_fundef_copy (fun
, copy
);
2760 /* If the call allocated some heap object that hasn't been
2761 deallocated during the call, or if it deallocated some heap
2762 object it has not allocated, the call isn't really stateless
2763 for the constexpr evaluation and should not be cached.
2764 It is fine if the call allocates something and deallocates it
2767 && (save_heap_alloc_count
!= ctx
->global
->heap_vars
.length ()
2768 || (save_heap_dealloc_count
2769 != ctx
->global
->heap_dealloc_count
)))
2773 if ((ctx
->global
->heap_vars
.length ()
2774 - ctx
->global
->heap_dealloc_count
)
2775 != save_heap_alloc_count
- save_heap_dealloc_count
)
2778 FOR_EACH_VEC_ELT_FROM (ctx
->global
->heap_vars
, i
, heap_var
,
2779 save_heap_alloc_count
)
2780 if (DECL_NAME (heap_var
) != heap_deleted_identifier
)
2787 /* Rewrite all occurrences of the function's RESULT_DECL with the
2788 current object under construction. */
2789 if (!*non_constant_p
&& ctx
->object
2790 && AGGREGATE_TYPE_P (TREE_TYPE (res
))
2791 && !is_empty_class (TREE_TYPE (res
)))
2792 if (replace_result_decl (&result
, res
, ctx
->object
))
2796 /* Couldn't get a function copy to evaluate. */
2797 *non_constant_p
= true;
2799 if (result
== error_mark_node
)
2800 *non_constant_p
= true;
2801 if (*non_constant_p
|| *overflow_p
)
2802 result
= error_mark_node
;
2806 entry
->result
= cacheable
? result
: error_mark_node
;
2809 /* The result of a constexpr function must be completely initialized.
2811 However, in C++20, a constexpr constructor doesn't necessarily have
2812 to initialize all the fields, so we don't clear CONSTRUCTOR_NO_CLEARING
2813 in order to detect reading an unitialized object in constexpr instead
2814 of value-initializing it. (reduced_constant_expression_p is expected to
2815 take care of clearing the flag.) */
2816 if (TREE_CODE (result
) == CONSTRUCTOR
2817 && (cxx_dialect
< cxx20
2818 || !DECL_CONSTRUCTOR_P (fun
)))
2819 clear_no_implicit_zero (result
);
2821 pop_cx_call_context ();
2825 /* Return true if T is a valid constant initializer. If a CONSTRUCTOR
2826 initializes all the members, the CONSTRUCTOR_NO_CLEARING flag will be
2828 FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
2831 reduced_constant_expression_p (tree t
)
2836 switch (TREE_CODE (t
))
2839 /* Even if we can't lower this yet, it's constant. */
2843 /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
2844 tree idx
, val
, field
; unsigned HOST_WIDE_INT i
;
2845 if (CONSTRUCTOR_NO_CLEARING (t
))
2847 if (TREE_CODE (TREE_TYPE (t
)) == VECTOR_TYPE
)
2848 /* An initialized vector would have a VECTOR_CST. */
2850 else if (cxx_dialect
>= cxx20
2851 /* An ARRAY_TYPE doesn't have any TYPE_FIELDS. */
2852 && TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
2854 else if (cxx_dialect
>= cxx20
2855 && TREE_CODE (TREE_TYPE (t
)) == UNION_TYPE
)
2857 if (CONSTRUCTOR_NELTS (t
) == 0)
2858 /* An initialized union has a constructor element. */
2860 /* And it only initializes one member. */
2864 field
= next_initializable_field (TYPE_FIELDS (TREE_TYPE (t
)));
2868 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t
), i
, idx
, val
)
2870 /* If VAL is null, we're in the middle of initializing this
2872 if (!reduced_constant_expression_p (val
))
2874 /* Empty class field may or may not have an initializer. */
2875 for (; field
&& idx
!= field
;
2876 field
= next_initializable_field (DECL_CHAIN (field
)))
2877 if (!is_really_empty_class (TREE_TYPE (field
),
2878 /*ignore_vptr*/false))
2881 field
= next_initializable_field (DECL_CHAIN (field
));
2883 /* There could be a non-empty field at the end. */
2884 for (; field
; field
= next_initializable_field (DECL_CHAIN (field
)))
2885 if (!is_really_empty_class (TREE_TYPE (field
), /*ignore_vptr*/false))
2887 if (CONSTRUCTOR_NO_CLEARING (t
))
2888 /* All the fields are initialized. */
2889 CONSTRUCTOR_NO_CLEARING (t
) = false;
2893 /* FIXME are we calling this too much? */
2894 return initializer_constant_valid_p (t
, TREE_TYPE (t
)) != NULL_TREE
;
2898 /* Some expressions may have constant operands but are not constant
2899 themselves, such as 1/0. Call this function to check for that
2902 We only call this in places that require an arithmetic constant, not in
2903 places where we might have a non-constant expression that can be a
2904 component of a constant expression, such as the address of a constexpr
2905 variable that might be dereferenced later. */
2908 verify_constant (tree t
, bool allow_non_constant
, bool *non_constant_p
,
2911 if (!*non_constant_p
&& !reduced_constant_expression_p (t
)
2914 if (!allow_non_constant
)
2915 error ("%q+E is not a constant expression", t
);
2916 *non_constant_p
= true;
2918 if (TREE_OVERFLOW_P (t
))
2920 if (!allow_non_constant
)
2922 permerror (input_location
, "overflow in constant expression");
2923 /* If we're being permissive (and are in an enforcing
2924 context), ignore the overflow. */
2925 if (flag_permissive
)
2926 return *non_constant_p
;
2930 return *non_constant_p
;
2933 /* Check whether the shift operation with code CODE and type TYPE on LHS
2934 and RHS is undefined. If it is, give an error with an explanation,
2935 and return true; return false otherwise. */
2938 cxx_eval_check_shift_p (location_t loc
, const constexpr_ctx
*ctx
,
2939 enum tree_code code
, tree type
, tree lhs
, tree rhs
)
2941 if ((code
!= LSHIFT_EXPR
&& code
!= RSHIFT_EXPR
)
2942 || TREE_CODE (lhs
) != INTEGER_CST
2943 || TREE_CODE (rhs
) != INTEGER_CST
)
2946 tree lhstype
= TREE_TYPE (lhs
);
2947 unsigned HOST_WIDE_INT uprec
= TYPE_PRECISION (TREE_TYPE (lhs
));
2949 /* [expr.shift] The behavior is undefined if the right operand
2950 is negative, or greater than or equal to the length in bits
2951 of the promoted left operand. */
2952 if (tree_int_cst_sgn (rhs
) == -1)
2955 permerror (loc
, "right operand of shift expression %q+E is negative",
2956 build2_loc (loc
, code
, type
, lhs
, rhs
));
2957 return (!flag_permissive
|| ctx
->quiet
);
2959 if (compare_tree_int (rhs
, uprec
) >= 0)
2962 permerror (loc
, "right operand of shift expression %q+E is greater "
2963 "than or equal to the precision %wu of the left operand",
2964 build2_loc (loc
, code
, type
, lhs
, rhs
), uprec
);
2965 return (!flag_permissive
|| ctx
->quiet
);
2968 /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
2969 if E1 has a signed type and non-negative value, and E1x2^E2 is
2970 representable in the corresponding unsigned type of the result type,
2971 then that value, converted to the result type, is the resulting value;
2972 otherwise, the behavior is undefined.
2974 The value of E1 << E2 is the unique value congruent to E1 x 2^E2 modulo
2975 2^N, where N is the range exponent of the type of the result. */
2976 if (code
== LSHIFT_EXPR
2977 && !TYPE_UNSIGNED (lhstype
)
2978 && cxx_dialect
>= cxx11
2979 && cxx_dialect
< cxx20
)
2981 if (tree_int_cst_sgn (lhs
) == -1)
2985 "left operand of shift expression %q+E is negative",
2986 build2_loc (loc
, code
, type
, lhs
, rhs
));
2987 return (!flag_permissive
|| ctx
->quiet
);
2989 /* For signed x << y the following:
2990 (unsigned) x >> ((prec (lhs) - 1) - y)
2991 if > 1, is undefined. The right-hand side of this formula
2992 is the highest bit of the LHS that can be set (starting from 0),
2993 so that the shift doesn't overflow. We then right-shift the LHS
2994 to see whether any other bit is set making the original shift
2995 undefined -- the result is not representable in the corresponding
2997 tree t
= build_int_cst (unsigned_type_node
, uprec
- 1);
2998 t
= fold_build2 (MINUS_EXPR
, unsigned_type_node
, t
, rhs
);
2999 tree ulhs
= fold_convert (unsigned_type_for (lhstype
), lhs
);
3000 t
= fold_build2 (RSHIFT_EXPR
, TREE_TYPE (ulhs
), ulhs
, t
);
3001 if (tree_int_cst_lt (integer_one_node
, t
))
3004 permerror (loc
, "shift expression %q+E overflows",
3005 build2_loc (loc
, code
, type
, lhs
, rhs
));
3006 return (!flag_permissive
|| ctx
->quiet
);
3012 /* Subroutine of cxx_eval_constant_expression.
3013 Attempt to reduce the unary expression tree T to a compile time value.
3014 If successful, return the value. Otherwise issue a diagnostic
3015 and return error_mark_node. */
3018 cxx_eval_unary_expression (const constexpr_ctx
*ctx
, tree t
,
3020 bool *non_constant_p
, bool *overflow_p
)
3023 tree orig_arg
= TREE_OPERAND (t
, 0);
3024 tree arg
= cxx_eval_constant_expression (ctx
, orig_arg
, /*lval*/false,
3025 non_constant_p
, overflow_p
);
3026 VERIFY_CONSTANT (arg
);
3027 location_t loc
= EXPR_LOCATION (t
);
3028 enum tree_code code
= TREE_CODE (t
);
3029 tree type
= TREE_TYPE (t
);
3030 r
= fold_unary_loc (loc
, code
, type
, arg
);
3033 if (arg
== orig_arg
)
3036 r
= build1_loc (loc
, code
, type
, arg
);
3038 VERIFY_CONSTANT (r
);
3042 /* Helper function for cxx_eval_binary_expression. Try to optimize
3043 original POINTER_PLUS_EXPR T, LHS p+ RHS, return NULL_TREE if the
3044 generic folding should be used. */
3047 cxx_fold_pointer_plus_expression (const constexpr_ctx
*ctx
, tree t
,
3048 tree lhs
, tree rhs
, bool *non_constant_p
,
3052 if (TREE_CODE (lhs
) != ADDR_EXPR
)
3055 lhs
= TREE_OPERAND (lhs
, 0);
3057 /* &A[i] p+ j => &A[i + j] */
3058 if (TREE_CODE (lhs
) == ARRAY_REF
3059 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == INTEGER_CST
3060 && TREE_CODE (rhs
) == INTEGER_CST
3061 && TYPE_SIZE_UNIT (TREE_TYPE (lhs
))
3062 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs
))) == INTEGER_CST
)
3064 tree orig_type
= TREE_TYPE (t
);
3065 location_t loc
= EXPR_LOCATION (t
);
3066 tree type
= TREE_TYPE (lhs
);
3068 t
= fold_convert_loc (loc
, ssizetype
, TREE_OPERAND (lhs
, 1));
3069 tree nelts
= array_type_nelts_top (TREE_TYPE (TREE_OPERAND (lhs
, 0)));
3070 nelts
= cxx_eval_constant_expression (ctx
, nelts
, false, non_constant_p
,
3072 if (*non_constant_p
)
3074 /* Don't fold an out-of-bound access. */
3075 if (!tree_int_cst_le (t
, nelts
))
3077 rhs
= cp_fold_convert (ssizetype
, rhs
);
3078 /* Don't fold if rhs can't be divided exactly by TYPE_SIZE_UNIT.
3079 constexpr int A[1]; ... (char *)&A[0] + 1 */
3080 if (!integer_zerop (fold_build2_loc (loc
, TRUNC_MOD_EXPR
, sizetype
,
3081 rhs
, TYPE_SIZE_UNIT (type
))))
3083 /* Make sure to treat the second operand of POINTER_PLUS_EXPR
3085 rhs
= fold_build2_loc (loc
, EXACT_DIV_EXPR
, ssizetype
, rhs
,
3086 TYPE_SIZE_UNIT (type
));
3087 t
= size_binop_loc (loc
, PLUS_EXPR
, rhs
, t
);
3088 t
= build4_loc (loc
, ARRAY_REF
, type
, TREE_OPERAND (lhs
, 0),
3089 t
, NULL_TREE
, NULL_TREE
);
3090 t
= cp_build_addr_expr (t
, tf_warning_or_error
);
3091 t
= cp_fold_convert (orig_type
, t
);
3092 return cxx_eval_constant_expression (ctx
, t
, /*lval*/false,
3093 non_constant_p
, overflow_p
);
3099 /* Subroutine of cxx_eval_constant_expression.
3100 Like cxx_eval_unary_expression, except for binary expressions. */
3103 cxx_eval_binary_expression (const constexpr_ctx
*ctx
, tree t
,
3105 bool *non_constant_p
, bool *overflow_p
)
3108 tree orig_lhs
= TREE_OPERAND (t
, 0);
3109 tree orig_rhs
= TREE_OPERAND (t
, 1);
3111 lhs
= cxx_eval_constant_expression (ctx
, orig_lhs
, /*lval*/false,
3112 non_constant_p
, overflow_p
);
3113 /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer
3115 if (*non_constant_p
)
3117 rhs
= cxx_eval_constant_expression (ctx
, orig_rhs
, /*lval*/false,
3118 non_constant_p
, overflow_p
);
3119 if (*non_constant_p
)
3122 location_t loc
= EXPR_LOCATION (t
);
3123 enum tree_code code
= TREE_CODE (t
);
3124 tree type
= TREE_TYPE (t
);
3126 if (code
== EQ_EXPR
|| code
== NE_EXPR
)
3128 bool is_code_eq
= (code
== EQ_EXPR
);
3130 if (TREE_CODE (lhs
) == PTRMEM_CST
3131 && TREE_CODE (rhs
) == PTRMEM_CST
)
3133 tree lmem
= PTRMEM_CST_MEMBER (lhs
);
3134 tree rmem
= PTRMEM_CST_MEMBER (rhs
);
3136 if (TREE_CODE (lmem
) == TREE_CODE (rmem
)
3137 && TREE_CODE (lmem
) == FIELD_DECL
3138 && TREE_CODE (DECL_CONTEXT (lmem
)) == UNION_TYPE
3139 && same_type_p (DECL_CONTEXT (lmem
),
3140 DECL_CONTEXT (rmem
)))
3141 /* If both refer to (possibly different) members of the same union
3142 (12.3), they compare equal. */
3145 eq
= cp_tree_equal (lhs
, rhs
);
3146 r
= constant_boolean_node (eq
== is_code_eq
, type
);
3148 else if ((TREE_CODE (lhs
) == PTRMEM_CST
3149 || TREE_CODE (rhs
) == PTRMEM_CST
)
3150 && (null_member_pointer_value_p (lhs
)
3151 || null_member_pointer_value_p (rhs
)))
3152 r
= constant_boolean_node (!is_code_eq
, type
);
3153 else if (TREE_CODE (lhs
) == PTRMEM_CST
)
3154 lhs
= cplus_expand_constant (lhs
);
3155 else if (TREE_CODE (rhs
) == PTRMEM_CST
)
3156 rhs
= cplus_expand_constant (rhs
);
3158 if (code
== POINTER_PLUS_EXPR
&& !*non_constant_p
3159 && integer_zerop (lhs
) && !integer_zerop (rhs
))
3162 error ("arithmetic involving a null pointer in %qE", lhs
);
3163 *non_constant_p
= true;
3166 else if (code
== POINTER_PLUS_EXPR
)
3167 r
= cxx_fold_pointer_plus_expression (ctx
, t
, lhs
, rhs
, non_constant_p
,
3169 else if (code
== SPACESHIP_EXPR
)
3171 r
= genericize_spaceship (loc
, type
, lhs
, rhs
);
3172 return cxx_eval_constant_expression (ctx
, r
, lval
, non_constant_p
,
3177 r
= fold_binary_loc (loc
, code
, type
, lhs
, rhs
);
3180 && (code
== LSHIFT_EXPR
|| code
== RSHIFT_EXPR
)
3181 && TREE_CODE (lhs
) == INTEGER_CST
3182 && TREE_CODE (rhs
) == INTEGER_CST
3183 && wi::neg_p (wi::to_wide (rhs
)))
3185 /* For diagnostics and -fpermissive emulate previous behavior of
3186 handling shifts by negative amount. */
3187 tree nrhs
= const_unop (NEGATE_EXPR
, TREE_TYPE (rhs
), rhs
);
3189 r
= fold_binary_loc (loc
,
3190 code
== LSHIFT_EXPR
? RSHIFT_EXPR
: LSHIFT_EXPR
,
3196 if (lhs
== orig_lhs
&& rhs
== orig_rhs
)
3199 r
= build2_loc (loc
, code
, type
, lhs
, rhs
);
3201 else if (cxx_eval_check_shift_p (loc
, ctx
, code
, type
, lhs
, rhs
))
3202 *non_constant_p
= true;
3203 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
3204 a local array in a constexpr function. */
3205 bool ptr
= INDIRECT_TYPE_P (TREE_TYPE (lhs
));
3207 VERIFY_CONSTANT (r
);
3211 /* Subroutine of cxx_eval_constant_expression.
3212 Attempt to evaluate condition expressions. Dead branches are not
3216 cxx_eval_conditional_expression (const constexpr_ctx
*ctx
, tree t
,
3218 bool *non_constant_p
, bool *overflow_p
,
3221 tree val
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
3223 non_constant_p
, overflow_p
);
3224 VERIFY_CONSTANT (val
);
3225 /* Don't VERIFY_CONSTANT the other operands. */
3226 if (integer_zerop (val
))
3227 val
= TREE_OPERAND (t
, 2);
3229 val
= TREE_OPERAND (t
, 1);
3230 if (TREE_CODE (t
) == IF_STMT
&& !val
)
3232 return cxx_eval_constant_expression (ctx
, val
, lval
, non_constant_p
,
3233 overflow_p
, jump_target
);
3236 /* Subroutine of cxx_eval_constant_expression.
3237 Attempt to evaluate vector condition expressions. Unlike
3238 cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal
3239 ternary arithmetics operation, where all 3 arguments have to be
3240 evaluated as constants and then folding computes the result from
3244 cxx_eval_vector_conditional_expression (const constexpr_ctx
*ctx
, tree t
,
3245 bool *non_constant_p
, bool *overflow_p
)
3247 tree arg1
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
3249 non_constant_p
, overflow_p
);
3250 VERIFY_CONSTANT (arg1
);
3251 tree arg2
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
3253 non_constant_p
, overflow_p
);
3254 VERIFY_CONSTANT (arg2
);
3255 tree arg3
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 2),
3257 non_constant_p
, overflow_p
);
3258 VERIFY_CONSTANT (arg3
);
3259 location_t loc
= EXPR_LOCATION (t
);
3260 tree type
= TREE_TYPE (t
);
3261 tree r
= fold_ternary_loc (loc
, VEC_COND_EXPR
, type
, arg1
, arg2
, arg3
);
3264 if (arg1
== TREE_OPERAND (t
, 0)
3265 && arg2
== TREE_OPERAND (t
, 1)
3266 && arg3
== TREE_OPERAND (t
, 2))
3269 r
= build3_loc (loc
, VEC_COND_EXPR
, type
, arg1
, arg2
, arg3
);
3271 VERIFY_CONSTANT (r
);
3275 /* Returns less than, equal to, or greater than zero if KEY is found to be
3276 less than, to match, or to be greater than the constructor_elt's INDEX. */
3279 array_index_cmp (tree key
, tree index
)
3281 gcc_assert (TREE_CODE (key
) == INTEGER_CST
);
3283 switch (TREE_CODE (index
))
3286 return tree_int_cst_compare (key
, index
);
3289 tree lo
= TREE_OPERAND (index
, 0);
3290 tree hi
= TREE_OPERAND (index
, 1);
3291 if (tree_int_cst_lt (key
, lo
))
3293 else if (tree_int_cst_lt (hi
, key
))
3303 /* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1
3304 if none. If INSERT is true, insert a matching element rather than fail. */
3306 static HOST_WIDE_INT
3307 find_array_ctor_elt (tree ary
, tree dindex
, bool insert
)
3309 if (tree_int_cst_sgn (dindex
) < 0)
3312 unsigned HOST_WIDE_INT i
= tree_to_uhwi (dindex
);
3313 vec
<constructor_elt
, va_gc
> *elts
= CONSTRUCTOR_ELTS (ary
);
3314 unsigned HOST_WIDE_INT len
= vec_safe_length (elts
);
3316 unsigned HOST_WIDE_INT end
= len
;
3317 unsigned HOST_WIDE_INT begin
= 0;
3319 /* If the last element of the CONSTRUCTOR has its own index, we can assume
3320 that the same is true of the other elements and index directly. */
3323 tree cindex
= (*elts
)[end
- 1].index
;
3324 if (cindex
== NULL_TREE
)
3326 /* Verify that if the last index is missing, all indexes
3329 for (unsigned int j
= 0; j
< len
- 1; ++j
)
3330 gcc_assert ((*elts
)[j
].index
== NULL_TREE
);
3337 /* If the element is to be added right at the end,
3338 make sure it is added with cleared index too. */
3341 /* Otherwise, in order not to break the assumption
3342 that CONSTRUCTOR either has all indexes or none,
3343 we need to add indexes to all elements. */
3344 for (unsigned int j
= 0; j
< len
; ++j
)
3345 (*elts
)[j
].index
= build_int_cst (TREE_TYPE (dindex
), j
);
3348 else if (TREE_CODE (cindex
) == INTEGER_CST
3349 && compare_tree_int (cindex
, end
- 1) == 0)
3358 /* Otherwise, find a matching index by means of a binary search. */
3359 while (begin
!= end
)
3361 unsigned HOST_WIDE_INT middle
= (begin
+ end
) / 2;
3362 constructor_elt
&elt
= (*elts
)[middle
];
3363 tree idx
= elt
.index
;
3365 int cmp
= array_index_cmp (dindex
, idx
);
3372 if (insert
&& TREE_CODE (idx
) == RANGE_EXPR
)
3374 /* We need to split the range. */
3376 tree lo
= TREE_OPERAND (idx
, 0);
3377 tree hi
= TREE_OPERAND (idx
, 1);
3378 tree value
= elt
.value
;
3379 dindex
= fold_convert (sizetype
, dindex
);
3380 if (tree_int_cst_lt (lo
, dindex
))
3382 /* There are still some lower elts; shorten the range. */
3383 tree new_hi
= int_const_binop (MINUS_EXPR
, dindex
,
3385 if (tree_int_cst_equal (lo
, new_hi
))
3386 /* Only one element left, no longer a range. */
3389 TREE_OPERAND (idx
, 1) = new_hi
;
3390 /* Append the element we want to insert. */
3393 e
.value
= unshare_constructor (value
);
3394 vec_safe_insert (CONSTRUCTOR_ELTS (ary
), middle
, e
);
3397 /* No lower elts, the range elt is now ours. */
3400 if (tree_int_cst_lt (dindex
, hi
))
3402 /* There are still some higher elts; append a range. */
3403 tree new_lo
= int_const_binop (PLUS_EXPR
, dindex
,
3405 if (tree_int_cst_equal (new_lo
, hi
))
3408 e
.index
= build2 (RANGE_EXPR
, sizetype
, new_lo
, hi
);
3409 e
.value
= unshare_constructor (value
);
3410 vec_safe_insert (CONSTRUCTOR_ELTS (ary
), middle
+ 1, e
);
3419 constructor_elt e
= { dindex
, NULL_TREE
};
3420 vec_safe_insert (CONSTRUCTOR_ELTS (ary
), end
, e
);
3427 /* Return a pointer to the constructor_elt of CTOR which matches INDEX. If no
3428 matching constructor_elt exists, then add one to CTOR.
3430 As an optimization, if POS_HINT is non-negative then it is used as a guess
3431 for the (integer) index of the matching constructor_elt within CTOR. */
3433 static constructor_elt
*
3434 get_or_insert_ctor_field (tree ctor
, tree index
, int pos_hint
= -1)
3436 /* Check the hint first. */
3437 if (pos_hint
>= 0 && (unsigned)pos_hint
< CONSTRUCTOR_NELTS (ctor
)
3438 && CONSTRUCTOR_ELT (ctor
, pos_hint
)->index
== index
)
3439 return CONSTRUCTOR_ELT (ctor
, pos_hint
);
3441 tree type
= TREE_TYPE (ctor
);
3442 if (TREE_CODE (type
) == VECTOR_TYPE
&& index
== NULL_TREE
)
3444 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (ctor
), index
, NULL_TREE
);
3445 return &CONSTRUCTOR_ELTS (ctor
)->last();
3447 else if (TREE_CODE (type
) == ARRAY_TYPE
|| TREE_CODE (type
) == VECTOR_TYPE
)
3449 if (TREE_CODE (index
) == RANGE_EXPR
)
3451 /* Support for RANGE_EXPR index lookups is currently limited to
3452 accessing an existing element via POS_HINT, or appending a new
3453 element to the end of CTOR. ??? Support for other access
3454 patterns may also be needed. */
3455 vec
<constructor_elt
, va_gc
> *elts
= CONSTRUCTOR_ELTS (ctor
);
3456 if (vec_safe_length (elts
))
3458 tree lo
= TREE_OPERAND (index
, 0);
3459 gcc_assert (array_index_cmp (elts
->last().index
, lo
) < 0);
3461 CONSTRUCTOR_APPEND_ELT (elts
, index
, NULL_TREE
);
3462 return &elts
->last();
3465 HOST_WIDE_INT i
= find_array_ctor_elt (ctor
, index
, /*insert*/true);
3466 gcc_assert (i
>= 0);
3467 constructor_elt
*cep
= CONSTRUCTOR_ELT (ctor
, i
);
3468 gcc_assert (cep
->index
== NULL_TREE
3469 || TREE_CODE (cep
->index
) != RANGE_EXPR
);
3474 gcc_assert (TREE_CODE (index
) == FIELD_DECL
);
3476 /* We must keep the CONSTRUCTOR's ELTS in FIELD order.
3477 Usually we meet initializers in that order, but it is
3478 possible for base types to be placed not in program
3480 tree fields
= TYPE_FIELDS (DECL_CONTEXT (index
));
3481 unsigned HOST_WIDE_INT idx
= 0;
3482 constructor_elt
*cep
= NULL
;
3484 /* Check if we're changing the active member of a union. */
3485 if (TREE_CODE (type
) == UNION_TYPE
&& CONSTRUCTOR_NELTS (ctor
)
3486 && CONSTRUCTOR_ELT (ctor
, 0)->index
!= index
)
3487 vec_safe_truncate (CONSTRUCTOR_ELTS (ctor
), 0);
3488 /* If the bit offset of INDEX is larger than that of the last
3489 constructor_elt, then we can just immediately append a new
3490 constructor_elt to the end of CTOR. */
3491 else if (CONSTRUCTOR_NELTS (ctor
)
3492 && tree_int_cst_compare (bit_position (index
),
3493 bit_position (CONSTRUCTOR_ELTS (ctor
)
3494 ->last().index
)) > 0)
3496 idx
= CONSTRUCTOR_NELTS (ctor
);
3500 /* Otherwise, we need to iterate over CTOR to find or insert INDEX
3503 for (; vec_safe_iterate (CONSTRUCTOR_ELTS (ctor
), idx
, &cep
);
3504 idx
++, fields
= DECL_CHAIN (fields
))
3506 if (index
== cep
->index
)
3509 /* The field we're initializing must be on the field
3510 list. Look to see if it is present before the
3511 field the current ELT initializes. */
3512 for (; fields
!= cep
->index
; fields
= DECL_CHAIN (fields
))
3513 if (index
== fields
)
3516 /* We fell off the end of the CONSTRUCTOR, so insert a new
3517 entry at the end. */
3521 constructor_elt ce
= { index
, NULL_TREE
};
3523 vec_safe_insert (CONSTRUCTOR_ELTS (ctor
), idx
, ce
);
3524 cep
= CONSTRUCTOR_ELT (ctor
, idx
);
3532 /* Under the control of CTX, issue a detailed diagnostic for
3533 an out-of-bounds subscript INDEX into the expression ARRAY. */
3536 diag_array_subscript (location_t loc
, const constexpr_ctx
*ctx
, tree array
, tree index
)
3540 tree arraytype
= TREE_TYPE (array
);
3542 /* Convert the unsigned array subscript to a signed integer to avoid
3543 printing huge numbers for small negative values. */
3544 tree sidx
= fold_convert (ssizetype
, index
);
3545 STRIP_ANY_LOCATION_WRAPPER (array
);
3548 if (TYPE_DOMAIN (arraytype
))
3549 error_at (loc
, "array subscript value %qE is outside the bounds "
3550 "of array %qD of type %qT", sidx
, array
, arraytype
);
3552 error_at (loc
, "nonzero array subscript %qE is used with array %qD of "
3553 "type %qT with unknown bounds", sidx
, array
, arraytype
);
3554 inform (DECL_SOURCE_LOCATION (array
), "declared here");
3556 else if (TYPE_DOMAIN (arraytype
))
3557 error_at (loc
, "array subscript value %qE is outside the bounds "
3558 "of array type %qT", sidx
, arraytype
);
3560 error_at (loc
, "nonzero array subscript %qE is used with array of type %qT "
3561 "with unknown bounds", sidx
, arraytype
);
3565 /* Return the number of elements for TYPE (which is an ARRAY_TYPE or
3569 get_array_or_vector_nelts (const constexpr_ctx
*ctx
, tree type
,
3570 bool *non_constant_p
, bool *overflow_p
)
3573 if (TREE_CODE (type
) == ARRAY_TYPE
)
3575 if (TYPE_DOMAIN (type
))
3576 nelts
= array_type_nelts_top (type
);
3578 nelts
= size_zero_node
;
3580 else if (VECTOR_TYPE_P (type
))
3581 nelts
= size_int (TYPE_VECTOR_SUBPARTS (type
));
3585 /* For VLAs, the number of elements won't be an integer constant. */
3586 nelts
= cxx_eval_constant_expression (ctx
, nelts
, false,
3587 non_constant_p
, overflow_p
);
3591 /* Extract element INDEX consisting of CHARS_PER_ELT chars from
3592 STRING_CST STRING. */
3595 extract_string_elt (tree string
, unsigned chars_per_elt
, unsigned index
)
3597 tree type
= cv_unqualified (TREE_TYPE (TREE_TYPE (string
)));
3600 if (chars_per_elt
== 1)
3601 r
= build_int_cst (type
, TREE_STRING_POINTER (string
)[index
]);
3604 const unsigned char *ptr
3605 = ((const unsigned char *)TREE_STRING_POINTER (string
)
3606 + index
* chars_per_elt
);
3607 r
= native_interpret_expr (type
, ptr
, chars_per_elt
);
3612 /* Subroutine of cxx_eval_array_reference. T is an ARRAY_REF; evaluate the
3613 subscript, diagnose any problems with it, and return the result. */
3616 eval_and_check_array_index (const constexpr_ctx
*ctx
,
3617 tree t
, bool allow_one_past
,
3618 bool *non_constant_p
, bool *overflow_p
)
3620 location_t loc
= cp_expr_loc_or_input_loc (t
);
3621 tree ary
= TREE_OPERAND (t
, 0);
3622 t
= TREE_OPERAND (t
, 1);
3623 tree index
= cxx_eval_constant_expression (ctx
, t
, false,
3624 non_constant_p
, overflow_p
);
3625 VERIFY_CONSTANT (index
);
3627 if (!tree_fits_shwi_p (index
)
3628 || tree_int_cst_sgn (index
) < 0)
3630 diag_array_subscript (loc
, ctx
, ary
, index
);
3631 *non_constant_p
= true;
3635 tree nelts
= get_array_or_vector_nelts (ctx
, TREE_TYPE (ary
), non_constant_p
,
3637 VERIFY_CONSTANT (nelts
);
3639 ? !tree_int_cst_le (index
, nelts
)
3640 : !tree_int_cst_lt (index
, nelts
))
3642 diag_array_subscript (loc
, ctx
, ary
, index
);
3643 *non_constant_p
= true;
3650 /* Subroutine of cxx_eval_constant_expression.
3651 Attempt to reduce a reference to an array slot. */
3654 cxx_eval_array_reference (const constexpr_ctx
*ctx
, tree t
,
3656 bool *non_constant_p
, bool *overflow_p
)
3658 tree oldary
= TREE_OPERAND (t
, 0);
3659 tree ary
= cxx_eval_constant_expression (ctx
, oldary
,
3661 non_constant_p
, overflow_p
);
3662 if (*non_constant_p
)
3665 && TREE_CODE (ary
) == VIEW_CONVERT_EXPR
3666 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary
, 0)))
3667 && TREE_TYPE (t
) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary
, 0))))
3668 ary
= TREE_OPERAND (ary
, 0);
3670 tree oldidx
= TREE_OPERAND (t
, 1);
3671 tree index
= eval_and_check_array_index (ctx
, t
, lval
,
3672 non_constant_p
, overflow_p
);
3673 if (*non_constant_p
)
3676 if (lval
&& ary
== oldary
&& index
== oldidx
)
3679 return build4 (ARRAY_REF
, TREE_TYPE (t
), ary
, index
, NULL
, NULL
);
3681 unsigned len
= 0, elem_nchars
= 1;
3682 tree elem_type
= TREE_TYPE (TREE_TYPE (ary
));
3683 if (TREE_CODE (ary
) == CONSTRUCTOR
)
3684 len
= CONSTRUCTOR_NELTS (ary
);
3685 else if (TREE_CODE (ary
) == STRING_CST
)
3687 elem_nchars
= (TYPE_PRECISION (elem_type
)
3688 / TYPE_PRECISION (char_type_node
));
3689 len
= (unsigned) TREE_STRING_LENGTH (ary
) / elem_nchars
;
3691 else if (TREE_CODE (ary
) == VECTOR_CST
)
3692 /* We don't create variable-length VECTOR_CSTs. */
3693 len
= VECTOR_CST_NELTS (ary
).to_constant ();
3696 /* We can't do anything with other tree codes, so use
3697 VERIFY_CONSTANT to complain and fail. */
3698 VERIFY_CONSTANT (ary
);
3703 HOST_WIDE_INT i
= 0;
3704 if (TREE_CODE (ary
) == CONSTRUCTOR
)
3706 HOST_WIDE_INT ix
= find_array_ctor_elt (ary
, index
);
3713 i
= tree_to_shwi (index
);
3720 if (TREE_CODE (ary
) == CONSTRUCTOR
)
3721 r
= (*CONSTRUCTOR_ELTS (ary
))[i
].value
;
3722 else if (TREE_CODE (ary
) == VECTOR_CST
)
3723 r
= VECTOR_CST_ELT (ary
, i
);
3725 r
= extract_string_elt (ary
, elem_nchars
, i
);
3728 /* Don't VERIFY_CONSTANT here. */
3731 /* Otherwise the element doesn't have a value yet. */
3736 if (TREE_CODE (ary
) == CONSTRUCTOR
3737 && CONSTRUCTOR_NO_CLEARING (ary
))
3739 /* 'ary' is part of the aggregate initializer we're currently
3740 building; if there's no initializer for this element yet,
3743 error ("accessing uninitialized array element");
3744 *non_constant_p
= true;
3748 /* If it's within the array bounds but doesn't have an explicit
3749 initializer, it's initialized from {}. But use build_value_init
3750 directly for non-aggregates to avoid creating a garbage CONSTRUCTOR. */
3752 constexpr_ctx new_ctx
;
3753 if (CP_AGGREGATE_TYPE_P (elem_type
))
3755 tree empty_ctor
= build_constructor (init_list_type_node
, NULL
);
3756 val
= digest_init (elem_type
, empty_ctor
, tf_warning_or_error
);
3758 new_ctx
.ctor
= build_constructor (elem_type
, NULL
);
3762 val
= build_value_init (elem_type
, tf_warning_or_error
);
3763 t
= cxx_eval_constant_expression (ctx
, val
, lval
, non_constant_p
,
3765 if (CP_AGGREGATE_TYPE_P (elem_type
) && t
!= ctx
->ctor
)
3766 free_constructor (ctx
->ctor
);
3770 /* Subroutine of cxx_eval_constant_expression.
3771 Attempt to reduce a field access of a value of class type. */
3774 cxx_eval_component_reference (const constexpr_ctx
*ctx
, tree t
,
3776 bool *non_constant_p
, bool *overflow_p
)
3778 unsigned HOST_WIDE_INT i
;
3781 tree part
= TREE_OPERAND (t
, 1);
3782 tree orig_whole
= TREE_OPERAND (t
, 0);
3783 tree whole
= cxx_eval_constant_expression (ctx
, orig_whole
,
3785 non_constant_p
, overflow_p
);
3786 if (INDIRECT_REF_P (whole
)
3787 && integer_zerop (TREE_OPERAND (whole
, 0)))
3790 error ("dereferencing a null pointer in %qE", orig_whole
);
3791 *non_constant_p
= true;
3795 if (TREE_CODE (whole
) == PTRMEM_CST
)
3796 whole
= cplus_expand_constant (whole
);
3797 if (whole
== orig_whole
)
3800 return fold_build3 (COMPONENT_REF
, TREE_TYPE (t
),
3801 whole
, part
, NULL_TREE
);
3802 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
3804 if (!*non_constant_p
&& TREE_CODE (whole
) != CONSTRUCTOR
)
3807 error ("%qE is not a constant expression", orig_whole
);
3808 *non_constant_p
= true;
3810 if (DECL_MUTABLE_P (part
))
3813 error ("mutable %qD is not usable in a constant expression", part
);
3814 *non_constant_p
= true;
3816 if (*non_constant_p
)
3818 bool pmf
= TYPE_PTRMEMFUNC_P (TREE_TYPE (whole
));
3819 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole
), i
, field
, value
)
3821 /* Use name match for PMF fields, as a variant will have a
3822 different FIELD_DECL with a different type. */
3823 if (pmf
? DECL_NAME (field
) == DECL_NAME (part
)
3828 STRIP_ANY_LOCATION_WRAPPER (value
);
3832 /* We're in the middle of initializing it. */
3836 if (TREE_CODE (TREE_TYPE (whole
)) == UNION_TYPE
3837 && CONSTRUCTOR_NELTS (whole
) > 0)
3839 /* DR 1188 says we don't have to deal with this. */
3842 constructor_elt
*cep
= CONSTRUCTOR_ELT (whole
, 0);
3843 if (cep
->value
== NULL_TREE
)
3844 error ("accessing uninitialized member %qD", part
);
3846 error ("accessing %qD member instead of initialized %qD member in "
3847 "constant expression", part
, cep
->index
);
3849 *non_constant_p
= true;
3853 /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty
3854 classes never get represented; throw together a value now. */
3855 if (is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
3856 return build_constructor (TREE_TYPE (t
), NULL
);
3858 gcc_assert (DECL_CONTEXT (part
) == TYPE_MAIN_VARIANT (TREE_TYPE (whole
)));
3860 if (CONSTRUCTOR_NO_CLEARING (whole
))
3862 /* 'whole' is part of the aggregate initializer we're currently
3863 building; if there's no initializer for this member yet, that's an
3866 error ("accessing uninitialized member %qD", part
);
3867 *non_constant_p
= true;
3871 /* If there's no explicit init for this field, it's value-initialized. */
3872 value
= build_value_init (TREE_TYPE (t
), tf_warning_or_error
);
3873 return cxx_eval_constant_expression (ctx
, value
,
3875 non_constant_p
, overflow_p
);
3878 /* Subroutine of cxx_eval_constant_expression.
3879 Attempt to reduce a field access of a value of class type that is
3880 expressed as a BIT_FIELD_REF. */
3883 cxx_eval_bit_field_ref (const constexpr_ctx
*ctx
, tree t
,
3885 bool *non_constant_p
, bool *overflow_p
)
3887 tree orig_whole
= TREE_OPERAND (t
, 0);
3888 tree retval
, fldval
, utype
, mask
;
3889 bool fld_seen
= false;
3890 HOST_WIDE_INT istart
, isize
;
3891 tree whole
= cxx_eval_constant_expression (ctx
, orig_whole
,
3893 non_constant_p
, overflow_p
);
3894 tree start
, field
, value
;
3895 unsigned HOST_WIDE_INT i
;
3897 if (whole
== orig_whole
)
3899 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
3901 if (!*non_constant_p
3902 && TREE_CODE (whole
) != VECTOR_CST
3903 && TREE_CODE (whole
) != CONSTRUCTOR
)
3906 error ("%qE is not a constant expression", orig_whole
);
3907 *non_constant_p
= true;
3909 if (*non_constant_p
)
3912 if (TREE_CODE (whole
) == VECTOR_CST
)
3913 return fold_ternary (BIT_FIELD_REF
, TREE_TYPE (t
), whole
,
3914 TREE_OPERAND (t
, 1), TREE_OPERAND (t
, 2));
3916 start
= TREE_OPERAND (t
, 2);
3917 istart
= tree_to_shwi (start
);
3918 isize
= tree_to_shwi (TREE_OPERAND (t
, 1));
3919 utype
= TREE_TYPE (t
);
3920 if (!TYPE_UNSIGNED (utype
))
3921 utype
= build_nonstandard_integer_type (TYPE_PRECISION (utype
), 1);
3922 retval
= build_int_cst (utype
, 0);
3923 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole
), i
, field
, value
)
3925 tree bitpos
= bit_position (field
);
3926 STRIP_ANY_LOCATION_WRAPPER (value
);
3927 if (bitpos
== start
&& DECL_SIZE (field
) == TREE_OPERAND (t
, 1))
3929 if (TREE_CODE (TREE_TYPE (field
)) == INTEGER_TYPE
3930 && TREE_CODE (value
) == INTEGER_CST
3931 && tree_fits_shwi_p (bitpos
)
3932 && tree_fits_shwi_p (DECL_SIZE (field
)))
3934 HOST_WIDE_INT bit
= tree_to_shwi (bitpos
);
3935 HOST_WIDE_INT sz
= tree_to_shwi (DECL_SIZE (field
));
3936 HOST_WIDE_INT shift
;
3937 if (bit
>= istart
&& bit
+ sz
<= istart
+ isize
)
3939 fldval
= fold_convert (utype
, value
);
3940 mask
= build_int_cst_type (utype
, -1);
3941 mask
= fold_build2 (LSHIFT_EXPR
, utype
, mask
,
3942 size_int (TYPE_PRECISION (utype
) - sz
));
3943 mask
= fold_build2 (RSHIFT_EXPR
, utype
, mask
,
3944 size_int (TYPE_PRECISION (utype
) - sz
));
3945 fldval
= fold_build2 (BIT_AND_EXPR
, utype
, fldval
, mask
);
3946 shift
= bit
- istart
;
3947 if (BYTES_BIG_ENDIAN
)
3948 shift
= TYPE_PRECISION (utype
) - shift
- sz
;
3949 fldval
= fold_build2 (LSHIFT_EXPR
, utype
, fldval
,
3951 retval
= fold_build2 (BIT_IOR_EXPR
, utype
, retval
, fldval
);
3957 return fold_convert (TREE_TYPE (t
), retval
);
3959 return error_mark_node
;
3962 /* Helper for cxx_eval_bit_cast.
3963 Check [bit.cast]/3 rules, bit_cast is constexpr only if the To and From
3964 types and types of all subobjects have is_union_v<T>, is_pointer_v<T>,
3965 is_member_pointer_v<T>, is_volatile_v<T> false and has no non-static
3966 data members of reference type. */
3969 check_bit_cast_type (const constexpr_ctx
*ctx
, location_t loc
, tree type
,
3972 if (TREE_CODE (type
) == UNION_TYPE
)
3976 if (type
== orig_type
)
3977 error_at (loc
, "%qs is not a constant expression because %qT is "
3978 "a union type", "__builtin_bit_cast", type
);
3980 error_at (loc
, "%qs is not a constant expression because %qT "
3981 "contains a union type", "__builtin_bit_cast",
3986 if (TREE_CODE (type
) == POINTER_TYPE
)
3990 if (type
== orig_type
)
3991 error_at (loc
, "%qs is not a constant expression because %qT is "
3992 "a pointer type", "__builtin_bit_cast", type
);
3994 error_at (loc
, "%qs is not a constant expression because %qT "
3995 "contains a pointer type", "__builtin_bit_cast",
4000 if (TREE_CODE (type
) == REFERENCE_TYPE
)
4004 if (type
== orig_type
)
4005 error_at (loc
, "%qs is not a constant expression because %qT is "
4006 "a reference type", "__builtin_bit_cast", type
);
4008 error_at (loc
, "%qs is not a constant expression because %qT "
4009 "contains a reference type", "__builtin_bit_cast",
4014 if (TYPE_PTRMEM_P (type
))
4018 if (type
== orig_type
)
4019 error_at (loc
, "%qs is not a constant expression because %qT is "
4020 "a pointer to member type", "__builtin_bit_cast",
4023 error_at (loc
, "%qs is not a constant expression because %qT "
4024 "contains a pointer to member type",
4025 "__builtin_bit_cast", orig_type
);
4029 if (TYPE_VOLATILE (type
))
4033 if (type
== orig_type
)
4034 error_at (loc
, "%qs is not a constant expression because %qT is "
4035 "volatile", "__builtin_bit_cast", type
);
4037 error_at (loc
, "%qs is not a constant expression because %qT "
4038 "contains a volatile subobject",
4039 "__builtin_bit_cast", orig_type
);
4043 if (TREE_CODE (type
) == RECORD_TYPE
)
4044 for (tree field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
4045 if (TREE_CODE (field
) == FIELD_DECL
4046 && check_bit_cast_type (ctx
, loc
, TREE_TYPE (field
), orig_type
))
4051 /* Subroutine of cxx_eval_constant_expression.
4052 Attempt to evaluate a BIT_CAST_EXPR. */
4055 cxx_eval_bit_cast (const constexpr_ctx
*ctx
, tree t
, bool *non_constant_p
,
4058 if (check_bit_cast_type (ctx
, EXPR_LOCATION (t
), TREE_TYPE (t
),
4060 || check_bit_cast_type (ctx
, cp_expr_loc_or_loc (TREE_OPERAND (t
, 0),
4062 TREE_TYPE (TREE_OPERAND (t
, 0)),
4063 TREE_TYPE (TREE_OPERAND (t
, 0))))
4065 *non_constant_p
= true;
4069 tree op
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), false,
4070 non_constant_p
, overflow_p
);
4071 if (*non_constant_p
)
4074 location_t loc
= EXPR_LOCATION (t
);
4075 if (BITS_PER_UNIT
!= 8 || CHAR_BIT
!= 8)
4078 sorry_at (loc
, "%qs cannot be constant evaluated on the target",
4079 "__builtin_bit_cast");
4080 *non_constant_p
= true;
4084 if (!tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (t
))))
4087 sorry_at (loc
, "%qs cannot be constant evaluated because the "
4088 "type is too large", "__builtin_bit_cast");
4089 *non_constant_p
= true;
4093 HOST_WIDE_INT len
= tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (t
)));
4094 if (len
< 0 || (int) len
!= len
)
4097 sorry_at (loc
, "%qs cannot be constant evaluated because the "
4098 "type is too large", "__builtin_bit_cast");
4099 *non_constant_p
= true;
4103 unsigned char buf
[64];
4104 unsigned char *ptr
, *mask
;
4105 size_t alen
= (size_t) len
* 2;
4106 if (alen
<= sizeof (buf
))
4109 ptr
= XNEWVEC (unsigned char, alen
);
4110 mask
= ptr
+ (size_t) len
;
4111 /* At the beginning consider everything indeterminate. */
4112 memset (mask
, ~0, (size_t) len
);
4114 if (native_encode_initializer (op
, ptr
, len
, 0, mask
) != len
)
4117 sorry_at (loc
, "%qs cannot be constant evaluated because the "
4118 "argument cannot be encoded", "__builtin_bit_cast");
4119 *non_constant_p
= true;
4126 if (can_native_interpret_type_p (TREE_TYPE (t
)))
4127 r
= native_interpret_expr (TREE_TYPE (t
), ptr
, len
);
4128 else if (TREE_CODE (TREE_TYPE (t
)) == RECORD_TYPE
)
4130 r
= native_interpret_aggregate (TREE_TYPE (t
), ptr
, 0, len
);
4132 clear_type_padding_in_mask (TREE_TYPE (t
), mask
);
4137 for (int i
= 0; i
< len
; i
++)
4141 error_at (loc
, "%qs accessing uninitialized byte at offset %d",
4142 "__builtin_bit_cast", i
);
4143 *non_constant_p
= true;
4153 sorry_at (loc
, "%qs cannot be constant evaluated because the "
4154 "argument cannot be interpreted", "__builtin_bit_cast");
4155 *non_constant_p
= true;
4161 /* Subroutine of cxx_eval_constant_expression.
4162 Evaluate a short-circuited logical expression T in the context
4163 of a given constexpr CALL. BAILOUT_VALUE is the value for
4164 early return. CONTINUE_VALUE is used here purely for
4165 sanity check purposes. */
4168 cxx_eval_logical_expression (const constexpr_ctx
*ctx
, tree t
,
4169 tree bailout_value
, tree continue_value
,
4171 bool *non_constant_p
, bool *overflow_p
)
4174 tree lhs
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
4176 non_constant_p
, overflow_p
);
4177 VERIFY_CONSTANT (lhs
);
4178 if (tree_int_cst_equal (lhs
, bailout_value
))
4180 gcc_assert (tree_int_cst_equal (lhs
, continue_value
));
4181 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
4182 lval
, non_constant_p
,
4184 VERIFY_CONSTANT (r
);
4188 /* REF is a COMPONENT_REF designating a particular field. V is a vector of
4189 CONSTRUCTOR elements to initialize (part of) an object containing that
4190 field. Return a pointer to the constructor_elt corresponding to the
4191 initialization of the field. */
4193 static constructor_elt
*
4194 base_field_constructor_elt (vec
<constructor_elt
, va_gc
> *v
, tree ref
)
4196 tree aggr
= TREE_OPERAND (ref
, 0);
4197 tree field
= TREE_OPERAND (ref
, 1);
4199 constructor_elt
*ce
;
4201 gcc_assert (TREE_CODE (ref
) == COMPONENT_REF
);
4203 if (TREE_CODE (aggr
) == COMPONENT_REF
)
4205 constructor_elt
*base_ce
4206 = base_field_constructor_elt (v
, aggr
);
4207 v
= CONSTRUCTOR_ELTS (base_ce
->value
);
4210 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
4211 if (ce
->index
== field
)
4218 /* Some of the expressions fed to the constexpr mechanism are calls to
4219 constructors, which have type void. In that case, return the type being
4220 initialized by the constructor. */
4223 initialized_type (tree t
)
4227 tree type
= TREE_TYPE (t
);
4228 if (TREE_CODE (t
) == CALL_EXPR
)
4230 /* A constructor call has void type, so we need to look deeper. */
4231 tree fn
= get_function_named_in_call (t
);
4232 if (fn
&& TREE_CODE (fn
) == FUNCTION_DECL
4233 && DECL_CXX_CONSTRUCTOR_P (fn
))
4234 type
= DECL_CONTEXT (fn
);
4236 else if (TREE_CODE (t
) == COMPOUND_EXPR
)
4237 return initialized_type (TREE_OPERAND (t
, 1));
4238 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
)
4239 type
= TREE_TYPE (AGGR_INIT_EXPR_SLOT (t
));
4240 return cv_unqualified (type
);
4243 /* We're about to initialize element INDEX of an array or class from VALUE.
4244 Set up NEW_CTX appropriately by adjusting .object to refer to the
4245 subobject and creating a new CONSTRUCTOR if the element is itself
4246 a class or array. */
4249 init_subob_ctx (const constexpr_ctx
*ctx
, constexpr_ctx
&new_ctx
,
4250 tree index
, tree
&value
)
4254 if (index
&& TREE_CODE (index
) != INTEGER_CST
4255 && TREE_CODE (index
) != FIELD_DECL
4256 && TREE_CODE (index
) != RANGE_EXPR
)
4257 /* This won't have an element in the new CONSTRUCTOR. */
4260 tree type
= initialized_type (value
);
4261 if (!AGGREGATE_TYPE_P (type
) && !VECTOR_TYPE_P (type
))
4262 /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
4265 /* The sub-aggregate initializer might contain a placeholder;
4266 update object to refer to the subobject and ctor to refer to
4267 the (newly created) sub-initializer. */
4270 if (index
== NULL_TREE
|| TREE_CODE (index
) == RANGE_EXPR
)
4271 /* There's no well-defined subobject for this index. */
4272 new_ctx
.object
= NULL_TREE
;
4274 new_ctx
.object
= build_ctor_subob_ref (index
, type
, ctx
->object
);
4276 tree elt
= build_constructor (type
, NULL
);
4277 CONSTRUCTOR_NO_CLEARING (elt
) = true;
4280 if (TREE_CODE (value
) == TARGET_EXPR
)
4281 /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
4282 value
= TARGET_EXPR_INITIAL (value
);
4285 /* We're about to process an initializer for a class or array TYPE. Make
4286 sure that CTX is set up appropriately. */
4289 verify_ctor_sanity (const constexpr_ctx
*ctx
, tree type
)
4291 /* We don't bother building a ctor for an empty base subobject. */
4292 if (is_empty_class (type
))
4295 /* We're in the middle of an initializer that might involve placeholders;
4296 our caller should have created a CONSTRUCTOR for us to put the
4297 initializer into. We will either return that constructor or T. */
4298 gcc_assert (ctx
->ctor
);
4299 gcc_assert (same_type_ignoring_top_level_qualifiers_p
4300 (type
, TREE_TYPE (ctx
->ctor
)));
4301 /* We used to check that ctx->ctor was empty, but that isn't the case when
4302 the object is zero-initialized before calling the constructor. */
4305 tree otype
= TREE_TYPE (ctx
->object
);
4306 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type
, otype
)
4307 /* Handle flexible array members. */
4308 || (TREE_CODE (otype
) == ARRAY_TYPE
4309 && TYPE_DOMAIN (otype
) == NULL_TREE
4310 && TREE_CODE (type
) == ARRAY_TYPE
4311 && (same_type_ignoring_top_level_qualifiers_p
4312 (TREE_TYPE (type
), TREE_TYPE (otype
)))));
4314 gcc_assert (!ctx
->object
|| !DECL_P (ctx
->object
)
4315 || *(ctx
->global
->values
.get (ctx
->object
)) == ctx
->ctor
);
4318 /* Subroutine of cxx_eval_constant_expression.
4319 The expression tree T denotes a C-style array or a C-style
4320 aggregate. Reduce it to a constant expression. */
4323 cxx_eval_bare_aggregate (const constexpr_ctx
*ctx
, tree t
,
4325 bool *non_constant_p
, bool *overflow_p
)
4327 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (t
);
4328 bool changed
= false;
4329 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t
));
4330 tree type
= TREE_TYPE (t
);
4332 constexpr_ctx new_ctx
;
4333 if (TYPE_PTRMEMFUNC_P (type
) || VECTOR_TYPE_P (type
))
4335 /* We don't really need the ctx->ctor business for a PMF or
4336 vector, but it's simpler to use the same code. */
4338 new_ctx
.ctor
= build_constructor (type
, NULL
);
4339 new_ctx
.object
= NULL_TREE
;
4342 verify_ctor_sanity (ctx
, type
);
4343 vec
<constructor_elt
, va_gc
> **p
= &CONSTRUCTOR_ELTS (ctx
->ctor
);
4344 vec_alloc (*p
, vec_safe_length (v
));
4346 if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (t
))
4347 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx
->ctor
) = 1;
4351 bool constant_p
= true;
4352 bool side_effects_p
= false;
4353 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, index
, value
)
4355 tree orig_value
= value
;
4356 init_subob_ctx (ctx
, new_ctx
, index
, value
);
4358 if (new_ctx
.ctor
!= ctx
->ctor
)
4360 /* If we built a new CONSTRUCTOR, attach it now so that other
4361 initializers can refer to it. */
4362 constructor_elt
*cep
= get_or_insert_ctor_field (ctx
->ctor
, index
);
4363 cep
->value
= new_ctx
.ctor
;
4364 pos_hint
= cep
- (*p
)->begin();
4366 else if (TREE_CODE (type
) == UNION_TYPE
)
4367 /* Otherwise if we're constructing a non-aggregate union member, set
4368 the active union member now so that we can later detect and diagnose
4369 if its initializer attempts to activate another member. */
4370 get_or_insert_ctor_field (ctx
->ctor
, index
);
4371 tree elt
= cxx_eval_constant_expression (&new_ctx
, value
,
4373 non_constant_p
, overflow_p
);
4374 /* Don't VERIFY_CONSTANT here. */
4375 if (ctx
->quiet
&& *non_constant_p
)
4377 if (elt
!= orig_value
)
4380 if (!TREE_CONSTANT (elt
))
4382 if (TREE_SIDE_EFFECTS (elt
))
4383 side_effects_p
= true;
4384 if (index
&& TREE_CODE (index
) == COMPONENT_REF
)
4386 /* This is an initialization of a vfield inside a base
4387 subaggregate that we already initialized; push this
4388 initialization into the previous initialization. */
4389 constructor_elt
*inner
= base_field_constructor_elt (*p
, index
);
4394 && (TREE_CODE (index
) == NOP_EXPR
4395 || TREE_CODE (index
) == POINTER_PLUS_EXPR
))
4397 /* This is an initializer for an empty base; now that we've
4398 checked that it's constant, we can ignore it. */
4399 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index
))));
4404 if (TREE_CODE (type
) == UNION_TYPE
4405 && (*p
)->last().index
!= index
)
4406 /* The initializer erroneously changed the active union member that
4407 we're initializing. */
4408 gcc_assert (*non_constant_p
);
4411 /* The initializer might have mutated the underlying CONSTRUCTOR,
4412 so recompute the location of the target constructer_elt. */
4413 constructor_elt
*cep
4414 = get_or_insert_ctor_field (ctx
->ctor
, index
, pos_hint
);
4418 /* Adding or replacing an element might change the ctor's flags. */
4419 TREE_CONSTANT (ctx
->ctor
) = constant_p
;
4420 TREE_SIDE_EFFECTS (ctx
->ctor
) = side_effects_p
;
4423 if (*non_constant_p
|| !changed
)
4426 /* We're done building this CONSTRUCTOR, so now we can interpret an
4427 element without an explicit initializer as value-initialized. */
4428 CONSTRUCTOR_NO_CLEARING (t
) = false;
4429 TREE_CONSTANT (t
) = constant_p
;
4430 TREE_SIDE_EFFECTS (t
) = side_effects_p
;
4431 if (VECTOR_TYPE_P (type
))
4436 /* Subroutine of cxx_eval_constant_expression.
4437 The expression tree T is a VEC_INIT_EXPR which denotes the desired
4438 initialization of a non-static data member of array type. Reduce it to a
4441 Note that apart from value-initialization (when VALUE_INIT is true),
4442 this is only intended to support value-initialization and the
4443 initializations done by defaulted constructors for classes with
4444 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
4445 will either be NULL_TREE for the default constructor, or a COMPONENT_REF
4446 for the copy/move constructor. */
4449 cxx_eval_vec_init_1 (const constexpr_ctx
*ctx
, tree atype
, tree init
,
4450 bool value_init
, bool lval
,
4451 bool *non_constant_p
, bool *overflow_p
)
4453 tree elttype
= TREE_TYPE (atype
);
4454 verify_ctor_sanity (ctx
, atype
);
4455 vec
<constructor_elt
, va_gc
> **p
= &CONSTRUCTOR_ELTS (ctx
->ctor
);
4456 bool pre_init
= false;
4457 unsigned HOST_WIDE_INT i
;
4458 tsubst_flags_t complain
= ctx
->quiet
? tf_none
: tf_warning_or_error
;
4460 if (init
&& TREE_CODE (init
) == CONSTRUCTOR
)
4461 return cxx_eval_bare_aggregate (ctx
, init
, lval
,
4462 non_constant_p
, overflow_p
);
4464 /* For the default constructor, build up a call to the default
4465 constructor of the element type. We only need to handle class types
4466 here, as for a constructor to be constexpr, all members must be
4467 initialized, which for a defaulted default constructor means they must
4468 be of a class type with a constexpr default constructor. */
4469 if (TREE_CODE (elttype
) == ARRAY_TYPE
)
4470 /* We only do this at the lowest level. */;
4471 else if (value_init
)
4473 init
= build_value_init (elttype
, complain
);
4478 releasing_vec argvec
;
4479 init
= build_special_member_call (NULL_TREE
, complete_ctor_identifier
,
4480 &argvec
, elttype
, LOOKUP_NORMAL
,
4482 init
= build_aggr_init_expr (elttype
, init
);
4486 bool zeroed_out
= false;
4487 if (!CONSTRUCTOR_NO_CLEARING (ctx
->ctor
))
4489 /* We're initializing an array object that had been zero-initialized
4490 earlier. Truncate ctx->ctor, and propagate its zeroed state by
4491 clearing CONSTRUCTOR_NO_CLEARING on each of the aggregate element
4492 initializers we append to it. */
4493 gcc_checking_assert (initializer_zerop (ctx
->ctor
));
4495 vec_safe_truncate (*p
, 0);
4498 tree nelts
= get_array_or_vector_nelts (ctx
, atype
, non_constant_p
,
4500 unsigned HOST_WIDE_INT max
= tree_to_uhwi (nelts
);
4501 for (i
= 0; i
< max
; ++i
)
4503 tree idx
= build_int_cst (size_type_node
, i
);
4506 constexpr_ctx new_ctx
;
4507 init_subob_ctx (ctx
, new_ctx
, idx
, pre_init
? init
: elttype
);
4508 if (new_ctx
.ctor
!= ctx
->ctor
)
4511 CONSTRUCTOR_NO_CLEARING (new_ctx
.ctor
) = false;
4512 CONSTRUCTOR_APPEND_ELT (*p
, idx
, new_ctx
.ctor
);
4514 if (TREE_CODE (elttype
) == ARRAY_TYPE
)
4516 /* A multidimensional array; recurse. */
4517 if (value_init
|| init
== NULL_TREE
)
4519 eltinit
= NULL_TREE
;
4523 eltinit
= cp_build_array_ref (input_location
, init
, idx
, complain
);
4524 eltinit
= cxx_eval_vec_init_1 (&new_ctx
, elttype
, eltinit
, value_init
,
4526 non_constant_p
, overflow_p
);
4530 /* Initializing an element using value or default initialization
4531 we just pre-built above. */
4532 if (init
== void_node
)
4533 /* Trivial default-init, don't do anything to the CONSTRUCTOR. */
4535 eltinit
= cxx_eval_constant_expression (&new_ctx
, init
, lval
,
4536 non_constant_p
, overflow_p
);
4541 /* Copying an element. */
4542 gcc_assert (same_type_ignoring_top_level_qualifiers_p
4543 (atype
, TREE_TYPE (init
)));
4544 eltinit
= cp_build_array_ref (input_location
, init
, idx
, complain
);
4545 if (!lvalue_p (init
))
4546 eltinit
= move (eltinit
);
4547 eltinit
= force_rvalue (eltinit
, complain
);
4548 eltinit
= cxx_eval_constant_expression (&new_ctx
, eltinit
, lval
,
4549 non_constant_p
, overflow_p
);
4551 if (*non_constant_p
)
4553 if (new_ctx
.ctor
!= ctx
->ctor
)
4555 /* We appended this element above; update the value. */
4556 gcc_assert ((*p
)->last().index
== idx
);
4557 (*p
)->last().value
= eltinit
;
4560 CONSTRUCTOR_APPEND_ELT (*p
, idx
, eltinit
);
4561 /* Reuse the result of cxx_eval_constant_expression call
4562 from the first iteration to all others if it is a constant
4563 initializer that doesn't require relocations. */
4566 && (eltinit
== NULL_TREE
4567 || (initializer_constant_valid_p (eltinit
, TREE_TYPE (eltinit
))
4568 == null_pointer_node
)))
4570 if (new_ctx
.ctor
!= ctx
->ctor
)
4571 eltinit
= new_ctx
.ctor
;
4572 tree range
= build2 (RANGE_EXPR
, size_type_node
,
4573 build_int_cst (size_type_node
, 1),
4574 build_int_cst (size_type_node
, max
- 1));
4575 CONSTRUCTOR_APPEND_ELT (*p
, range
, unshare_constructor (eltinit
));
4579 vec_safe_reserve (*p
, max
);
4582 if (!*non_constant_p
)
4585 CONSTRUCTOR_NO_CLEARING (init
) = false;
4591 cxx_eval_vec_init (const constexpr_ctx
*ctx
, tree t
,
4593 bool *non_constant_p
, bool *overflow_p
)
4595 tree atype
= TREE_TYPE (t
);
4596 tree init
= VEC_INIT_EXPR_INIT (t
);
4597 tree r
= cxx_eval_vec_init_1 (ctx
, atype
, init
,
4598 VEC_INIT_EXPR_VALUE_INIT (t
),
4599 lval
, non_constant_p
, overflow_p
);
4600 if (*non_constant_p
)
4606 /* Like same_type_ignoring_top_level_qualifiers_p, but also handle the case
4607 where the desired type is an array of unknown bounds because the variable
4608 has had its bounds deduced since the wrapping expression was created. */
4611 same_type_ignoring_tlq_and_bounds_p (tree type1
, tree type2
)
4613 while (TREE_CODE (type1
) == ARRAY_TYPE
4614 && TREE_CODE (type2
) == ARRAY_TYPE
4615 && (!TYPE_DOMAIN (type1
) || !TYPE_DOMAIN (type2
)))
4617 type1
= TREE_TYPE (type1
);
4618 type2
= TREE_TYPE (type2
);
4620 return same_type_ignoring_top_level_qualifiers_p (type1
, type2
);
4623 /* Try to determine the currently active union member for an expression
4624 with UNION_TYPE. If it can be determined, return the FIELD_DECL,
4625 otherwise return NULL_TREE. */
4628 cxx_union_active_member (const constexpr_ctx
*ctx
, tree t
)
4630 constexpr_ctx new_ctx
= *ctx
;
4631 new_ctx
.quiet
= true;
4632 bool non_constant_p
= false, overflow_p
= false;
4633 tree ctor
= cxx_eval_constant_expression (&new_ctx
, t
, false,
4636 if (TREE_CODE (ctor
) == CONSTRUCTOR
4637 && CONSTRUCTOR_NELTS (ctor
) == 1
4638 && CONSTRUCTOR_ELT (ctor
, 0)->index
4639 && TREE_CODE (CONSTRUCTOR_ELT (ctor
, 0)->index
) == FIELD_DECL
)
4640 return CONSTRUCTOR_ELT (ctor
, 0)->index
;
4644 /* Helper function for cxx_fold_indirect_ref_1, called recursively. */
4647 cxx_fold_indirect_ref_1 (const constexpr_ctx
*ctx
, location_t loc
, tree type
,
4648 tree op
, unsigned HOST_WIDE_INT off
, bool *empty_base
)
4650 tree optype
= TREE_TYPE (op
);
4651 unsigned HOST_WIDE_INT const_nunits
;
4654 if (similar_type_p (optype
, type
))
4656 /* Also handle conversion to an empty base class, which
4657 is represented with a NOP_EXPR. */
4658 /* *(foo *)&complexfoo => __real__ complexfoo */
4659 else if (TREE_CODE (optype
) == COMPLEX_TYPE
4660 && similar_type_p (type
, TREE_TYPE (optype
)))
4661 return build1_loc (loc
, REALPART_EXPR
, type
, op
);
4663 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
4664 else if (TREE_CODE (optype
) == COMPLEX_TYPE
4665 && similar_type_p (type
, TREE_TYPE (optype
))
4666 && tree_to_uhwi (TYPE_SIZE_UNIT (type
)) == off
)
4667 return build1_loc (loc
, IMAGPART_EXPR
, type
, op
);
4668 if (is_empty_class (type
)
4669 && CLASS_TYPE_P (optype
)
4670 && DERIVED_FROM_P (type
, optype
))
4675 /* ((foo*)&vectorfoo)[x] => BIT_FIELD_REF<vectorfoo,...> */
4676 else if (VECTOR_TYPE_P (optype
)
4677 && similar_type_p (type
, TREE_TYPE (optype
))
4678 && TYPE_VECTOR_SUBPARTS (optype
).is_constant (&const_nunits
))
4680 unsigned HOST_WIDE_INT part_width
= tree_to_uhwi (TYPE_SIZE_UNIT (type
));
4681 unsigned HOST_WIDE_INT max_offset
= part_width
* const_nunits
;
4682 if (off
< max_offset
&& off
% part_width
== 0)
4684 tree index
= bitsize_int (off
* BITS_PER_UNIT
);
4685 return build3_loc (loc
, BIT_FIELD_REF
, type
, op
,
4686 TYPE_SIZE (type
), index
);
4689 /* ((foo *)&fooarray)[x] => fooarray[x] */
4690 else if (TREE_CODE (optype
) == ARRAY_TYPE
4691 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (optype
)))
4692 && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (optype
))))
4694 tree type_domain
= TYPE_DOMAIN (optype
);
4695 tree min_val
= size_zero_node
;
4696 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
4697 min_val
= TYPE_MIN_VALUE (type_domain
);
4698 unsigned HOST_WIDE_INT el_sz
4699 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (optype
)));
4700 unsigned HOST_WIDE_INT idx
= off
/ el_sz
;
4701 unsigned HOST_WIDE_INT rem
= off
% el_sz
;
4702 if (tree_fits_uhwi_p (min_val
))
4704 tree index
= size_int (idx
+ tree_to_uhwi (min_val
));
4705 op
= build4_loc (loc
, ARRAY_REF
, TREE_TYPE (optype
), op
, index
,
4706 NULL_TREE
, NULL_TREE
);
4707 return cxx_fold_indirect_ref_1 (ctx
, loc
, type
, op
, rem
,
4711 /* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */
4712 else if (TREE_CODE (optype
) == RECORD_TYPE
4713 || TREE_CODE (optype
) == UNION_TYPE
)
4715 if (TREE_CODE (optype
) == UNION_TYPE
)
4716 /* For unions prefer the currently active member. */
4717 if (tree field
= cxx_union_active_member (ctx
, op
))
4719 unsigned HOST_WIDE_INT el_sz
4720 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field
)));
4723 tree cop
= build3 (COMPONENT_REF
, TREE_TYPE (field
),
4724 op
, field
, NULL_TREE
);
4725 if (tree ret
= cxx_fold_indirect_ref_1 (ctx
, loc
, type
, cop
,
4730 for (tree field
= TYPE_FIELDS (optype
);
4731 field
; field
= DECL_CHAIN (field
))
4732 if (TREE_CODE (field
) == FIELD_DECL
4733 && TREE_TYPE (field
) != error_mark_node
4734 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (field
))))
4736 tree pos
= byte_position (field
);
4737 if (!tree_fits_uhwi_p (pos
))
4739 unsigned HOST_WIDE_INT upos
= tree_to_uhwi (pos
);
4740 unsigned HOST_WIDE_INT el_sz
4741 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field
)));
4742 if (upos
<= off
&& off
< upos
+ el_sz
)
4744 tree cop
= build3 (COMPONENT_REF
, TREE_TYPE (field
),
4745 op
, field
, NULL_TREE
);
4746 if (tree ret
= cxx_fold_indirect_ref_1 (ctx
, loc
, type
, cop
,
4757 /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
4758 match. We want to be less strict for simple *& folding; if we have a
4759 non-const temporary that we access through a const pointer, that should
4760 work. We handle this here rather than change fold_indirect_ref_1
4761 because we're dealing with things like ADDR_EXPR of INTEGER_CST which
4762 don't really make sense outside of constant expression evaluation. Also
4763 we want to allow folding to COMPONENT_REF, which could cause trouble
4764 with TBAA in fold_indirect_ref_1. */
4767 cxx_fold_indirect_ref (const constexpr_ctx
*ctx
, location_t loc
, tree type
,
4768 tree op0
, bool *empty_base
)
4772 poly_uint64 const_op01
;
4774 /* STRIP_NOPS, but stop if REINTERPRET_CAST_P. */
4775 while (CONVERT_EXPR_P (sub
) || TREE_CODE (sub
) == NON_LVALUE_EXPR
4776 || TREE_CODE (sub
) == VIEW_CONVERT_EXPR
)
4778 if (TREE_CODE (sub
) == NOP_EXPR
4779 && REINTERPRET_CAST_P (sub
))
4781 sub
= TREE_OPERAND (sub
, 0);
4784 subtype
= TREE_TYPE (sub
);
4785 if (!INDIRECT_TYPE_P (subtype
))
4788 if (TREE_CODE (sub
) == ADDR_EXPR
)
4790 tree op
= TREE_OPERAND (sub
, 0);
4791 tree optype
= TREE_TYPE (op
);
4793 /* *&CONST_DECL -> to the value of the const decl. */
4794 if (TREE_CODE (op
) == CONST_DECL
)
4795 return DECL_INITIAL (op
);
4796 /* *&p => p; make sure to handle *&"str"[cst] here. */
4797 if (similar_type_p (optype
, type
))
4799 tree fop
= fold_read_from_constant_string (op
);
4806 return cxx_fold_indirect_ref_1 (ctx
, loc
, type
, op
, 0, empty_base
);
4808 else if (TREE_CODE (sub
) == POINTER_PLUS_EXPR
4809 && tree_fits_uhwi_p (TREE_OPERAND (sub
, 1)))
4811 tree op00
= TREE_OPERAND (sub
, 0);
4812 tree op01
= TREE_OPERAND (sub
, 1);
4815 if (TREE_CODE (op00
) == ADDR_EXPR
)
4816 return cxx_fold_indirect_ref_1 (ctx
, loc
, type
, TREE_OPERAND (op00
, 0),
4817 tree_to_uhwi (op01
), empty_base
);
4819 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
4820 else if (TREE_CODE (TREE_TYPE (subtype
)) == ARRAY_TYPE
4821 && similar_type_p (type
, TREE_TYPE (TREE_TYPE (subtype
))))
4824 tree min_val
= size_zero_node
;
4826 = cxx_fold_indirect_ref (ctx
, loc
, TREE_TYPE (subtype
), sub
, NULL
);
4830 sub
= build1_loc (loc
, INDIRECT_REF
, TREE_TYPE (subtype
), sub
);
4831 type_domain
= TYPE_DOMAIN (TREE_TYPE (sub
));
4832 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
4833 min_val
= TYPE_MIN_VALUE (type_domain
);
4834 return build4_loc (loc
, ARRAY_REF
, type
, sub
, min_val
, NULL_TREE
,
4842 cxx_eval_indirect_ref (const constexpr_ctx
*ctx
, tree t
,
4844 bool *non_constant_p
, bool *overflow_p
)
4846 tree orig_op0
= TREE_OPERAND (t
, 0);
4847 bool empty_base
= false;
4849 /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second
4850 operand is an integer-zero. Otherwise reject the MEM_REF for now. */
4852 if (TREE_CODE (t
) == MEM_REF
4853 && (!TREE_OPERAND (t
, 1) || !integer_zerop (TREE_OPERAND (t
, 1))))
4855 gcc_assert (ctx
->quiet
);
4856 *non_constant_p
= true;
4860 /* First try to simplify it directly. */
4861 tree r
= cxx_fold_indirect_ref (ctx
, EXPR_LOCATION (t
), TREE_TYPE (t
),
4862 orig_op0
, &empty_base
);
4865 /* If that didn't work, evaluate the operand first. */
4866 tree op0
= cxx_eval_constant_expression (ctx
, orig_op0
,
4867 /*lval*/false, non_constant_p
,
4869 /* Don't VERIFY_CONSTANT here. */
4870 if (*non_constant_p
)
4873 if (!lval
&& integer_zerop (op0
))
4876 error ("dereferencing a null pointer");
4877 *non_constant_p
= true;
4881 r
= cxx_fold_indirect_ref (ctx
, EXPR_LOCATION (t
), TREE_TYPE (t
), op0
,
4885 /* We couldn't fold to a constant value. Make sure it's not
4886 something we should have been able to fold. */
4889 if (TREE_CODE (sub
) == ADDR_EXPR
)
4891 gcc_assert (!similar_type_p
4892 (TREE_TYPE (TREE_TYPE (sub
)), TREE_TYPE (t
)));
4893 /* DR 1188 says we don't have to deal with this. */
4895 error_at (cp_expr_loc_or_input_loc (t
),
4896 "accessing value of %qE through a %qT glvalue in a "
4897 "constant expression", build_fold_indirect_ref (sub
),
4899 *non_constant_p
= true;
4903 if (lval
&& op0
!= orig_op0
)
4904 return build1 (INDIRECT_REF
, TREE_TYPE (t
), op0
);
4906 VERIFY_CONSTANT (t
);
4911 r
= cxx_eval_constant_expression (ctx
, r
,
4912 lval
, non_constant_p
, overflow_p
);
4913 if (*non_constant_p
)
4916 /* If we're pulling out the value of an empty base, just return an empty
4918 if (empty_base
&& !lval
)
4920 r
= build_constructor (TREE_TYPE (t
), NULL
);
4921 TREE_CONSTANT (r
) = true;
4927 /* Complain about R, a VAR_DECL, not being usable in a constant expression.
4928 Shared between potential_constant_expression and
4929 cxx_eval_constant_expression. */
4932 non_const_var_error (location_t loc
, tree r
)
4934 auto_diagnostic_group d
;
4935 tree type
= TREE_TYPE (r
);
4936 if (DECL_NAME (r
) == heap_uninit_identifier
4937 || DECL_NAME (r
) == heap_identifier
4938 || DECL_NAME (r
) == heap_vec_uninit_identifier
4939 || DECL_NAME (r
) == heap_vec_identifier
)
4941 error_at (loc
, "the content of uninitialized storage is not usable "
4942 "in a constant expression");
4943 inform (DECL_SOURCE_LOCATION (r
), "allocated here");
4946 if (DECL_NAME (r
) == heap_deleted_identifier
)
4948 error_at (loc
, "use of allocated storage after deallocation in a "
4949 "constant expression");
4950 inform (DECL_SOURCE_LOCATION (r
), "allocated here");
4953 error_at (loc
, "the value of %qD is not usable in a constant "
4955 /* Avoid error cascade. */
4956 if (DECL_INITIAL (r
) == error_mark_node
)
4958 if (DECL_DECLARED_CONSTEXPR_P (r
))
4959 inform (DECL_SOURCE_LOCATION (r
),
4960 "%qD used in its own initializer", r
);
4961 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
4963 if (!CP_TYPE_CONST_P (type
))
4964 inform (DECL_SOURCE_LOCATION (r
),
4965 "%q#D is not const", r
);
4966 else if (CP_TYPE_VOLATILE_P (type
))
4967 inform (DECL_SOURCE_LOCATION (r
),
4968 "%q#D is volatile", r
);
4969 else if (!DECL_INITIAL (r
)
4970 || !TREE_CONSTANT (DECL_INITIAL (r
))
4971 || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
))
4972 inform (DECL_SOURCE_LOCATION (r
),
4973 "%qD was not initialized with a constant "
4978 else if (TYPE_REF_P (type
))
4979 inform (DECL_SOURCE_LOCATION (r
),
4980 "%qD was not initialized with a constant "
4984 if (cxx_dialect
>= cxx11
&& !DECL_DECLARED_CONSTEXPR_P (r
))
4985 inform (DECL_SOURCE_LOCATION (r
),
4986 "%qD was not declared %<constexpr%>", r
);
4988 inform (DECL_SOURCE_LOCATION (r
),
4989 "%qD does not have integral or enumeration type",
4994 /* Subroutine of cxx_eval_constant_expression.
4995 Like cxx_eval_unary_expression, except for trinary expressions. */
4998 cxx_eval_trinary_expression (const constexpr_ctx
*ctx
, tree t
,
5000 bool *non_constant_p
, bool *overflow_p
)
5006 for (i
= 0; i
< 3; i
++)
5008 args
[i
] = cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, i
),
5010 non_constant_p
, overflow_p
);
5011 VERIFY_CONSTANT (args
[i
]);
5014 val
= fold_ternary_loc (EXPR_LOCATION (t
), TREE_CODE (t
), TREE_TYPE (t
),
5015 args
[0], args
[1], args
[2]);
5016 if (val
== NULL_TREE
)
5018 VERIFY_CONSTANT (val
);
5022 /* True if T was declared in a function declared to be constexpr, and
5023 therefore potentially constant in C++14. */
5026 var_in_constexpr_fn (tree t
)
5028 tree ctx
= DECL_CONTEXT (t
);
5029 return (ctx
&& TREE_CODE (ctx
) == FUNCTION_DECL
5030 && DECL_DECLARED_CONSTEXPR_P (ctx
));
5033 /* True if T was declared in a function that might be constexpr: either a
5034 function that was declared constexpr, or a C++17 lambda op(). */
5037 var_in_maybe_constexpr_fn (tree t
)
5039 if (cxx_dialect
>= cxx17
5040 && DECL_FUNCTION_SCOPE_P (t
)
5041 && LAMBDA_FUNCTION_P (DECL_CONTEXT (t
)))
5043 return var_in_constexpr_fn (t
);
5046 /* We're assigning INIT to TARGET. In do_build_copy_constructor and
5047 build_over_call we implement trivial copy of a class with tail padding using
5048 assignment of character arrays, which is valid in normal code, but not in
5049 constexpr evaluation. We don't need to worry about clobbering tail padding
5050 in constexpr evaluation, so strip the type punning. */
5053 maybe_simplify_trivial_copy (tree
&target
, tree
&init
)
5055 if (TREE_CODE (target
) == MEM_REF
5056 && TREE_CODE (init
) == MEM_REF
5057 && TREE_TYPE (target
) == TREE_TYPE (init
)
5058 && TREE_CODE (TREE_TYPE (target
)) == ARRAY_TYPE
5059 && TREE_TYPE (TREE_TYPE (target
)) == unsigned_char_type_node
)
5061 target
= build_fold_indirect_ref (TREE_OPERAND (target
, 0));
5062 init
= build_fold_indirect_ref (TREE_OPERAND (init
, 0));
5066 /* Returns true if REF, which is a COMPONENT_REF, has any fields
5067 of constant type. This does not check for 'mutable', so the
5068 caller is expected to be mindful of that. */
5071 cref_has_const_field (tree ref
)
5073 while (TREE_CODE (ref
) == COMPONENT_REF
)
5075 if (CP_TYPE_CONST_P (TREE_TYPE (TREE_OPERAND (ref
, 1))))
5077 ref
= TREE_OPERAND (ref
, 0);
5082 /* Return true if we are modifying something that is const during constant
5083 expression evaluation. CODE is the code of the statement, OBJ is the
5084 object in question, MUTABLE_P is true if one of the subobjects were
5085 declared mutable. */
5088 modifying_const_object_p (tree_code code
, tree obj
, bool mutable_p
)
5090 /* If this is initialization, there's no problem. */
5091 if (code
!= MODIFY_EXPR
)
5094 /* [basic.type.qualifier] "A const object is an object of type
5095 const T or a non-mutable subobject of a const object." */
5099 if (TREE_READONLY (obj
))
5102 if (CP_TYPE_CONST_P (TREE_TYPE (obj
)))
5104 /* Although a COMPONENT_REF may have a const type, we should
5105 only consider it modifying a const object when any of the
5106 field components is const. This can happen when using
5107 constructs such as const_cast<const T &>(m), making something
5108 const even though it wasn't declared const. */
5109 if (TREE_CODE (obj
) == COMPONENT_REF
)
5110 return cref_has_const_field (obj
);
5118 /* Evaluate an INIT_EXPR or MODIFY_EXPR. */
5121 cxx_eval_store_expression (const constexpr_ctx
*ctx
, tree t
,
5123 bool *non_constant_p
, bool *overflow_p
)
5125 constexpr_ctx new_ctx
= *ctx
;
5127 tree init
= TREE_OPERAND (t
, 1);
5128 if (TREE_CLOBBER_P (init
))
5129 /* Just ignore clobbers. */
5132 /* First we figure out where we're storing to. */
5133 tree target
= TREE_OPERAND (t
, 0);
5135 maybe_simplify_trivial_copy (target
, init
);
5137 tree type
= TREE_TYPE (target
);
5138 bool preeval
= SCALAR_TYPE_P (type
) || TREE_CODE (t
) == MODIFY_EXPR
;
5141 /* Evaluate the value to be stored without knowing what object it will be
5142 stored in, so that any side-effects happen first. */
5143 if (!SCALAR_TYPE_P (type
))
5144 new_ctx
.ctor
= new_ctx
.object
= NULL_TREE
;
5145 init
= cxx_eval_constant_expression (&new_ctx
, init
, false,
5146 non_constant_p
, overflow_p
);
5147 if (*non_constant_p
)
5151 bool evaluated
= false;
5154 /* If we want to return a reference to the target, we need to evaluate it
5155 as a whole; otherwise, only evaluate the innermost piece to avoid
5156 building up unnecessary *_REFs. */
5157 target
= cxx_eval_constant_expression (ctx
, target
, true,
5158 non_constant_p
, overflow_p
);
5160 if (*non_constant_p
)
5164 /* Find the underlying variable. */
5166 tree object
= NULL_TREE
;
5167 /* If we're modifying a const object, save it. */
5168 tree const_object_being_modified
= NULL_TREE
;
5169 bool mutable_p
= false;
5170 for (tree probe
= target
; object
== NULL_TREE
; )
5172 switch (TREE_CODE (probe
))
5178 tree ob
= TREE_OPERAND (probe
, 0);
5179 tree elt
= TREE_OPERAND (probe
, 1);
5180 if (TREE_CODE (elt
) == FIELD_DECL
&& DECL_MUTABLE_P (elt
))
5182 if (TREE_CODE (probe
) == ARRAY_REF
)
5184 elt
= eval_and_check_array_index (ctx
, probe
, false,
5185 non_constant_p
, overflow_p
);
5186 if (*non_constant_p
)
5189 /* We don't check modifying_const_object_p for ARRAY_REFs. Given
5190 "int a[10]", an ARRAY_REF "a[2]" can be "const int", even though
5191 the array isn't const. Instead, check "a" in the next iteration;
5192 that will detect modifying "const int a[10]". */
5194 && modifying_const_object_p (TREE_CODE (t
), probe
,
5196 && const_object_being_modified
== NULL_TREE
)
5197 const_object_being_modified
= probe
;
5198 vec_safe_push (refs
, elt
);
5199 vec_safe_push (refs
, TREE_TYPE (probe
));
5209 probe
= cxx_eval_constant_expression (ctx
, probe
, true,
5210 non_constant_p
, overflow_p
);
5212 if (*non_constant_p
)
5219 if (modifying_const_object_p (TREE_CODE (t
), object
, mutable_p
)
5220 && const_object_being_modified
== NULL_TREE
)
5221 const_object_being_modified
= object
;
5223 /* And then find/build up our initializer for the path to the subobject
5224 we're initializing. */
5226 if (DECL_P (object
))
5227 valp
= ctx
->global
->values
.get (object
);
5232 /* A constant-expression cannot modify objects from outside the
5233 constant-expression. */
5235 error ("modification of %qE is not a constant expression", object
);
5236 *non_constant_p
= true;
5239 type
= TREE_TYPE (object
);
5240 bool no_zero_init
= true;
5242 releasing_vec ctors
, indexes
;
5243 auto_vec
<int> index_pos_hints
;
5244 bool activated_union_member_p
= false;
5245 while (!refs
->is_empty ())
5247 if (*valp
== NULL_TREE
)
5249 *valp
= build_constructor (type
, NULL
);
5250 CONSTRUCTOR_NO_CLEARING (*valp
) = no_zero_init
;
5252 else if (TREE_CODE (*valp
) == STRING_CST
)
5254 /* An array was initialized with a string constant, and now
5255 we're writing into one of its elements. Explode the
5256 single initialization into a set of element
5258 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
5260 tree string
= *valp
;
5261 tree elt_type
= TREE_TYPE (type
);
5262 unsigned chars_per_elt
= (TYPE_PRECISION (elt_type
)
5263 / TYPE_PRECISION (char_type_node
));
5264 unsigned num_elts
= TREE_STRING_LENGTH (string
) / chars_per_elt
;
5265 tree ary_ctor
= build_constructor (type
, NULL
);
5267 vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor
), num_elts
);
5268 for (unsigned ix
= 0; ix
!= num_elts
; ix
++)
5270 constructor_elt elt
=
5272 build_int_cst (size_type_node
, ix
),
5273 extract_string_elt (string
, chars_per_elt
, ix
)
5275 CONSTRUCTOR_ELTS (ary_ctor
)->quick_push (elt
);
5281 /* If the value of object is already zero-initialized, any new ctors for
5282 subobjects will also be zero-initialized. */
5283 no_zero_init
= CONSTRUCTOR_NO_CLEARING (*valp
);
5285 enum tree_code code
= TREE_CODE (type
);
5287 tree index
= refs
->pop();
5289 if (code
== UNION_TYPE
&& CONSTRUCTOR_NELTS (*valp
)
5290 && CONSTRUCTOR_ELT (*valp
, 0)->index
!= index
)
5292 if (cxx_dialect
< cxx20
)
5295 error_at (cp_expr_loc_or_input_loc (t
),
5296 "change of the active member of a union "
5298 CONSTRUCTOR_ELT (*valp
, 0)->index
,
5300 *non_constant_p
= true;
5302 else if (TREE_CODE (t
) == MODIFY_EXPR
5303 && CONSTRUCTOR_NO_CLEARING (*valp
))
5305 /* Diagnose changing the active union member while the union
5306 is in the process of being initialized. */
5308 error_at (cp_expr_loc_or_input_loc (t
),
5309 "change of the active member of a union "
5310 "from %qD to %qD during initialization",
5311 CONSTRUCTOR_ELT (*valp
, 0)->index
,
5313 *non_constant_p
= true;
5315 no_zero_init
= true;
5318 vec_safe_push (ctors
, *valp
);
5319 vec_safe_push (indexes
, index
);
5321 constructor_elt
*cep
5322 = get_or_insert_ctor_field (*valp
, index
);
5323 index_pos_hints
.safe_push (cep
- CONSTRUCTOR_ELTS (*valp
)->begin());
5325 if (code
== UNION_TYPE
)
5326 activated_union_member_p
= true;
5331 /* Detect modifying a constant object in constexpr evaluation.
5332 We have found a const object that is being modified. Figure out
5333 if we need to issue an error. Consider
5337 constexpr A() : n(1) { n = 2; } // #1
5341 constexpr B() { a.n = 3; } // #2
5345 #1 is OK, since we're modifying an object under construction, but
5346 #2 is wrong, since "a" is const and has been fully constructed.
5347 To track it, we use the TREE_READONLY bit in the object's CONSTRUCTOR
5348 which means that the object is read-only. For the example above, the
5349 *ctors stack at the point of #2 will look like:
5351 ctors[0] = {.a={.n=2}} TREE_READONLY = 0
5352 ctors[1] = {.n=2} TREE_READONLY = 1
5354 and we're modifying "b.a", so we search the stack and see if the
5355 constructor for "b.a" has already run. */
5356 if (const_object_being_modified
)
5360 = strip_array_types (TREE_TYPE (const_object_being_modified
));
5361 if (!CLASS_TYPE_P (const_objtype
))
5365 /* [class.ctor]p5 "A constructor can be invoked for a const,
5366 volatile, or const volatile object. const and volatile
5367 semantics are not applied on an object under construction.
5368 They come into effect when the constructor for the most
5369 derived object ends." */
5372 FOR_EACH_VEC_ELT (*ctors
, i
, elt
)
5373 if (same_type_ignoring_top_level_qualifiers_p
5374 (TREE_TYPE (const_object_being_modified
), TREE_TYPE (elt
)))
5376 fail
= TREE_READONLY (elt
);
5383 modifying_const_object_error (t
, const_object_being_modified
);
5384 *non_constant_p
= true;
5391 /* Create a new CONSTRUCTOR in case evaluation of the initializer
5392 wants to modify it. */
5393 if (*valp
== NULL_TREE
)
5395 *valp
= build_constructor (type
, NULL
);
5396 CONSTRUCTOR_NO_CLEARING (*valp
) = no_zero_init
;
5398 new_ctx
.ctor
= *valp
;
5399 new_ctx
.object
= target
;
5400 /* Avoid temporary materialization when initializing from a TARGET_EXPR.
5401 We don't need to mess with AGGR_EXPR_SLOT/VEC_INIT_EXPR_SLOT because
5402 expansion of those trees uses ctx instead. */
5403 if (TREE_CODE (init
) == TARGET_EXPR
)
5404 if (tree tinit
= TARGET_EXPR_INITIAL (init
))
5406 init
= cxx_eval_constant_expression (&new_ctx
, init
, false,
5407 non_constant_p
, overflow_p
);
5408 /* The hash table might have moved since the get earlier, and the
5409 initializer might have mutated the underlying CONSTRUCTORs, so we must
5411 valp
= ctx
->global
->values
.get (object
);
5412 for (unsigned i
= 0; i
< vec_safe_length (indexes
); i
++)
5414 constructor_elt
*cep
5415 = get_or_insert_ctor_field (*valp
, indexes
[i
], index_pos_hints
[i
]);
5420 /* Don't share a CONSTRUCTOR that might be changed later. */
5421 init
= unshare_constructor (init
);
5423 if (*valp
&& TREE_CODE (*valp
) == CONSTRUCTOR
5424 && TREE_CODE (init
) == CONSTRUCTOR
)
5426 /* An outer ctx->ctor might be pointing to *valp, so replace
5428 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init
),
5431 /* For initialization of an empty base, the original target will be
5432 *(base*)this, evaluation of which resolves to the object
5433 argument, which has the derived type rather than the base type. In
5434 this situation, just evaluate the initializer and return, since
5435 there's no actual data to store. */
5436 gcc_assert (is_empty_class (TREE_TYPE (init
)) && !lval
);
5439 CONSTRUCTOR_ELTS (*valp
) = CONSTRUCTOR_ELTS (init
);
5440 TREE_CONSTANT (*valp
) = TREE_CONSTANT (init
);
5441 TREE_SIDE_EFFECTS (*valp
) = TREE_SIDE_EFFECTS (init
);
5442 CONSTRUCTOR_NO_CLEARING (*valp
)
5443 = CONSTRUCTOR_NO_CLEARING (init
);
5448 /* After initialization, 'const' semantics apply to the value of the
5449 object. Make a note of this fact by marking the CONSTRUCTOR
5451 if (TREE_CODE (t
) == INIT_EXPR
5452 && TREE_CODE (*valp
) == CONSTRUCTOR
5453 && TYPE_READONLY (type
))
5455 if (INDIRECT_REF_P (target
)
5456 && (is_this_parameter
5457 (tree_strip_nop_conversions (TREE_OPERAND (target
, 0)))))
5458 /* We've just initialized '*this' (perhaps via the target
5459 constructor of a delegating constructor). Leave it up to the
5460 caller that set 'this' to set TREE_READONLY appropriately. */
5461 gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
5462 (TREE_TYPE (target
), type
));
5464 TREE_READONLY (*valp
) = true;
5467 /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing
5468 CONSTRUCTORs, if any. */
5471 bool c
= TREE_CONSTANT (init
);
5472 bool s
= TREE_SIDE_EFFECTS (init
);
5473 if (!c
|| s
|| activated_union_member_p
)
5474 FOR_EACH_VEC_ELT (*ctors
, i
, elt
)
5477 TREE_CONSTANT (elt
) = false;
5479 TREE_SIDE_EFFECTS (elt
) = true;
5480 /* Clear CONSTRUCTOR_NO_CLEARING since we've activated a member of
5482 if (TREE_CODE (TREE_TYPE (elt
)) == UNION_TYPE
)
5483 CONSTRUCTOR_NO_CLEARING (elt
) = false;
5486 if (*non_constant_p
)
5494 /* Evaluate a ++ or -- expression. */
5497 cxx_eval_increment_expression (const constexpr_ctx
*ctx
, tree t
,
5499 bool *non_constant_p
, bool *overflow_p
)
5501 enum tree_code code
= TREE_CODE (t
);
5502 tree type
= TREE_TYPE (t
);
5503 tree op
= TREE_OPERAND (t
, 0);
5504 tree offset
= TREE_OPERAND (t
, 1);
5505 gcc_assert (TREE_CONSTANT (offset
));
5507 /* OFFSET is constant, but perhaps not constant enough. We need to
5508 e.g. bash FLOAT_EXPRs to REAL_CSTs. */
5509 offset
= fold_simple (offset
);
5511 /* The operand as an lvalue. */
5512 op
= cxx_eval_constant_expression (ctx
, op
, true,
5513 non_constant_p
, overflow_p
);
5515 /* The operand as an rvalue. */
5517 = cxx_eval_constant_expression (ctx
, op
, false,
5518 non_constant_p
, overflow_p
);
5519 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
5520 a local array in a constexpr function. */
5521 bool ptr
= INDIRECT_TYPE_P (TREE_TYPE (val
));
5523 VERIFY_CONSTANT (val
);
5525 /* The modified value. */
5526 bool inc
= (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
);
5528 if (INDIRECT_TYPE_P (type
))
5530 /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
5531 offset
= convert_to_ptrofftype (offset
);
5533 offset
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (offset
), offset
);
5534 mod
= fold_build2 (POINTER_PLUS_EXPR
, type
, val
, offset
);
5537 mod
= fold_build2 (inc
? PLUS_EXPR
: MINUS_EXPR
, type
, val
, offset
);
5539 VERIFY_CONSTANT (mod
);
5541 /* Storing the modified value. */
5542 tree store
= build2_loc (cp_expr_loc_or_loc (t
, input_location
),
5543 MODIFY_EXPR
, type
, op
, mod
);
5544 cxx_eval_constant_expression (ctx
, store
,
5545 true, non_constant_p
, overflow_p
);
5548 /* And the value of the expression. */
5549 if (code
== PREINCREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
5551 /* Prefix ops are lvalues. */
5555 /* But we optimize when the caller wants an rvalue. */
5559 /* Postfix ops are rvalues. */
5563 /* Predicates for the meaning of *jump_target. */
5566 returns (tree
*jump_target
)
5569 && (TREE_CODE (*jump_target
) == RETURN_EXPR
5570 || (TREE_CODE (*jump_target
) == LABEL_DECL
5571 && LABEL_DECL_CDTOR (*jump_target
)));
5575 breaks (tree
*jump_target
)
5578 && ((TREE_CODE (*jump_target
) == LABEL_DECL
5579 && LABEL_DECL_BREAK (*jump_target
))
5580 || TREE_CODE (*jump_target
) == BREAK_STMT
5581 || TREE_CODE (*jump_target
) == EXIT_EXPR
);
5585 continues (tree
*jump_target
)
5588 && ((TREE_CODE (*jump_target
) == LABEL_DECL
5589 && LABEL_DECL_CONTINUE (*jump_target
))
5590 || TREE_CODE (*jump_target
) == CONTINUE_STMT
);
5595 switches (tree
*jump_target
)
5598 && TREE_CODE (*jump_target
) == INTEGER_CST
;
5601 /* Subroutine of cxx_eval_statement_list. Determine whether the statement
5602 STMT matches *jump_target. If we're looking for a case label and we see
5603 the default label, note it in ctx->css_state. */
5606 label_matches (const constexpr_ctx
*ctx
, tree
*jump_target
, tree stmt
)
5608 switch (TREE_CODE (*jump_target
))
5611 if (TREE_CODE (stmt
) == LABEL_EXPR
5612 && LABEL_EXPR_LABEL (stmt
) == *jump_target
)
5617 if (TREE_CODE (stmt
) == CASE_LABEL_EXPR
)
5619 gcc_assert (ctx
->css_state
!= NULL
);
5620 if (!CASE_LOW (stmt
))
5622 /* default: should appear just once in a SWITCH_EXPR
5623 body (excluding nested SWITCH_EXPR). */
5624 gcc_assert (*ctx
->css_state
!= css_default_seen
);
5625 /* When evaluating SWITCH_EXPR body for the second time,
5626 return true for the default: label. */
5627 if (*ctx
->css_state
== css_default_processing
)
5629 *ctx
->css_state
= css_default_seen
;
5631 else if (CASE_HIGH (stmt
))
5633 if (tree_int_cst_le (CASE_LOW (stmt
), *jump_target
)
5634 && tree_int_cst_le (*jump_target
, CASE_HIGH (stmt
)))
5637 else if (tree_int_cst_equal (*jump_target
, CASE_LOW (stmt
)))
5644 /* These two are handled directly in cxx_eval_loop_expr by testing
5645 breaks (jump_target) or continues (jump_target). */
5654 /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
5655 semantics, for switch, break, continue, and return. */
5658 cxx_eval_statement_list (const constexpr_ctx
*ctx
, tree t
,
5659 bool *non_constant_p
, bool *overflow_p
,
5662 tree_stmt_iterator i
;
5664 /* In a statement-expression we want to return the last value.
5665 For empty statement expression return void_node. */
5669 local_target
= NULL_TREE
;
5670 jump_target
= &local_target
;
5672 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
5674 tree stmt
= tsi_stmt (i
);
5675 /* We've found a continue, so skip everything until we reach
5676 the label its jumping to. */
5677 if (continues (jump_target
))
5679 if (label_matches (ctx
, jump_target
, stmt
))
5681 *jump_target
= NULL_TREE
;
5685 if (TREE_CODE (stmt
) == DEBUG_BEGIN_STMT
)
5687 r
= cxx_eval_constant_expression (ctx
, stmt
, false,
5688 non_constant_p
, overflow_p
,
5690 if (*non_constant_p
)
5692 if (returns (jump_target
) || breaks (jump_target
))
5695 if (*jump_target
&& jump_target
== &local_target
)
5697 /* We aren't communicating the jump to our caller, so give up. We don't
5698 need to support evaluation of jumps out of statement-exprs. */
5700 error_at (cp_expr_loc_or_input_loc (r
),
5701 "statement is not a constant expression");
5702 *non_constant_p
= true;
5707 /* Evaluate a LOOP_EXPR for side-effects. Handles break and return
5708 semantics; continue semantics are covered by cxx_eval_statement_list. */
5711 cxx_eval_loop_expr (const constexpr_ctx
*ctx
, tree t
,
5712 bool *non_constant_p
, bool *overflow_p
,
5715 constexpr_ctx new_ctx
= *ctx
;
5719 local_target
= NULL_TREE
;
5720 jump_target
= &local_target
;
5723 tree body
, cond
= NULL_TREE
, expr
= NULL_TREE
;
5725 switch (TREE_CODE (t
))
5728 body
= LOOP_EXPR_BODY (t
);
5735 body
= WHILE_BODY (t
);
5736 cond
= WHILE_COND (t
);
5740 if (FOR_INIT_STMT (t
))
5741 cxx_eval_constant_expression (ctx
, FOR_INIT_STMT (t
), /*lval*/false,
5742 non_constant_p
, overflow_p
, jump_target
);
5743 if (*non_constant_p
)
5745 body
= FOR_BODY (t
);
5746 cond
= FOR_COND (t
);
5747 expr
= FOR_EXPR (t
);
5753 auto_vec
<tree
, 10> save_exprs
;
5754 new_ctx
.save_exprs
= &save_exprs
;
5760 cxx_eval_constant_expression (&new_ctx
, body
, /*lval*/false,
5761 non_constant_p
, overflow_p
,
5763 if (breaks (jump_target
))
5765 *jump_target
= NULL_TREE
;
5769 if (TREE_CODE (t
) != LOOP_EXPR
&& continues (jump_target
))
5770 *jump_target
= NULL_TREE
;
5773 cxx_eval_constant_expression (&new_ctx
, expr
, /*lval*/false,
5774 non_constant_p
, overflow_p
,
5781 = cxx_eval_constant_expression (&new_ctx
, cond
, /*lval*/false,
5782 non_constant_p
, overflow_p
,
5786 if (verify_constant (res
, ctx
->quiet
, non_constant_p
,
5789 if (integer_zerop (res
))
5793 gcc_assert (*jump_target
);
5796 /* Forget saved values of SAVE_EXPRs and TARGET_EXPRs. */
5799 FOR_EACH_VEC_ELT (save_exprs
, i
, save_expr
)
5800 ctx
->global
->values
.remove (save_expr
);
5801 save_exprs
.truncate (0);
5803 if (++count
>= constexpr_loop_limit
)
5806 error_at (cp_expr_loc_or_input_loc (t
),
5807 "%<constexpr%> loop iteration count exceeds limit of %d "
5808 "(use %<-fconstexpr-loop-limit=%> to increase the limit)",
5809 constexpr_loop_limit
);
5810 *non_constant_p
= true;
5814 while (!returns (jump_target
)
5815 && !breaks (jump_target
)
5816 && !continues (jump_target
)
5817 && (!switches (jump_target
) || count
== 0)
5818 && !*non_constant_p
);
5820 /* Forget saved values of SAVE_EXPRs and TARGET_EXPRs. */
5823 FOR_EACH_VEC_ELT (save_exprs
, i
, save_expr
)
5824 ctx
->global
->values
.remove (save_expr
);
5829 /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
5833 cxx_eval_switch_expr (const constexpr_ctx
*ctx
, tree t
,
5834 bool *non_constant_p
, bool *overflow_p
,
5838 = TREE_CODE (t
) == SWITCH_STMT
? SWITCH_STMT_COND (t
) : SWITCH_COND (t
);
5839 cond
= cxx_eval_constant_expression (ctx
, cond
, false,
5840 non_constant_p
, overflow_p
);
5841 VERIFY_CONSTANT (cond
);
5842 *jump_target
= cond
;
5845 = TREE_CODE (t
) == SWITCH_STMT
? SWITCH_STMT_BODY (t
) : SWITCH_BODY (t
);
5846 constexpr_ctx new_ctx
= *ctx
;
5847 constexpr_switch_state css
= css_default_not_seen
;
5848 new_ctx
.css_state
= &css
;
5849 cxx_eval_constant_expression (&new_ctx
, body
, false,
5850 non_constant_p
, overflow_p
, jump_target
);
5851 if (switches (jump_target
) && css
== css_default_seen
)
5853 /* If the SWITCH_EXPR body has default: label, process it once again,
5854 this time instructing label_matches to return true for default:
5855 label on switches (jump_target). */
5856 css
= css_default_processing
;
5857 cxx_eval_constant_expression (&new_ctx
, body
, false,
5858 non_constant_p
, overflow_p
, jump_target
);
5860 if (breaks (jump_target
) || switches (jump_target
))
5861 *jump_target
= NULL_TREE
;
5865 /* Find the object of TYPE under initialization in CTX. */
5868 lookup_placeholder (const constexpr_ctx
*ctx
, bool lval
, tree type
)
5873 /* Prefer the outermost matching object, but don't cross
5874 CONSTRUCTOR_PLACEHOLDER_BOUNDARY constructors. */
5875 if (ctx
->ctor
&& !CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx
->ctor
))
5876 if (tree outer_ob
= lookup_placeholder (ctx
->parent
, lval
, type
))
5879 /* We could use ctx->object unconditionally, but using ctx->ctor when we
5880 can is a minor optimization. */
5881 if (!lval
&& ctx
->ctor
&& same_type_p (TREE_TYPE (ctx
->ctor
), type
))
5887 /* Since an object cannot have a field of its own type, we can search outward
5888 from ctx->object to find the unique containing object of TYPE. */
5889 tree ob
= ctx
->object
;
5892 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob
), type
))
5894 if (handled_component_p (ob
))
5895 ob
= TREE_OPERAND (ob
, 0);
5903 /* Complain about an attempt to evaluate inline assembly. */
5906 inline_asm_in_constexpr_error (location_t loc
)
5908 auto_diagnostic_group d
;
5909 error_at (loc
, "inline assembly is not a constant expression");
5910 inform (loc
, "only unevaluated inline assembly is allowed in a "
5911 "%<constexpr%> function in C++20");
5914 /* Attempt to reduce the expression T to a constant value.
5915 On failure, issue diagnostic and return error_mark_node. */
5916 /* FIXME unify with c_fully_fold */
5917 /* FIXME overflow_p is too global */
5920 cxx_eval_constant_expression (const constexpr_ctx
*ctx
, tree t
,
5922 bool *non_constant_p
, bool *overflow_p
,
5923 tree
*jump_target
/* = NULL */)
5925 if (jump_target
&& *jump_target
)
5927 /* If we are jumping, ignore all statements/expressions except those
5928 that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */
5929 switch (TREE_CODE (t
))
5932 case STATEMENT_LIST
:
5941 case CASE_LABEL_EXPR
:
5942 if (label_matches (ctx
, jump_target
, t
))
5944 *jump_target
= NULL_TREE
;
5950 if (error_operand_p (t
))
5952 *non_constant_p
= true;
5956 location_t loc
= cp_expr_loc_or_input_loc (t
);
5958 STRIP_ANY_LOCATION_WRAPPER (t
);
5960 if (CONSTANT_CLASS_P (t
))
5962 if (TREE_OVERFLOW (t
))
5965 permerror (input_location
, "overflow in constant expression");
5966 if (!flag_permissive
|| ctx
->quiet
)
5970 if (TREE_CODE (t
) == INTEGER_CST
5971 && TYPE_PTR_P (TREE_TYPE (t
))
5972 && !integer_zerop (t
))
5975 error ("value %qE of type %qT is not a constant expression",
5977 *non_constant_p
= true;
5983 /* Avoid excessively long constexpr evaluations. */
5984 if (++ctx
->global
->constexpr_ops_count
>= constexpr_ops_limit
)
5988 "%<constexpr%> evaluation operation count exceeds limit of "
5989 "%wd (use %<-fconstexpr-ops-limit=%> to increase the limit)",
5990 constexpr_ops_limit
);
5991 ctx
->global
->constexpr_ops_count
= INTTYPE_MINIMUM (HOST_WIDE_INT
);
5992 *non_constant_p
= true;
5996 constexpr_ctx new_ctx
;
5999 tree_code tcode
= TREE_CODE (t
);
6005 /* We ask for an rvalue for the RESULT_DECL when indirecting
6006 through an invisible reference, or in named return value
6008 if (tree
*p
= ctx
->global
->values
.get (t
))
6013 error ("%qE is not a constant expression", t
);
6014 *non_constant_p
= true;
6019 if (DECL_HAS_VALUE_EXPR_P (t
))
6021 if (is_normal_capture_proxy (t
)
6022 && current_function_decl
== DECL_CONTEXT (t
))
6024 /* Function parms aren't constexpr within the function
6025 definition, so don't try to look at the closure. But if the
6026 captured variable is constant, try to evaluate it directly. */
6027 r
= DECL_CAPTURED_VARIABLE (t
);
6028 tree type
= TREE_TYPE (t
);
6029 if (TYPE_REF_P (type
) != TYPE_REF_P (TREE_TYPE (r
)))
6031 /* Adjust r to match the reference-ness of t. */
6032 if (TYPE_REF_P (type
))
6033 r
= build_address (r
);
6035 r
= convert_from_reference (r
);
6039 r
= DECL_VALUE_EXPR (t
);
6040 return cxx_eval_constant_expression (ctx
, r
, lval
, non_constant_p
,
6045 /* We used to not check lval for CONST_DECL, but darwin.c uses
6046 CONST_DECL for aggregate constants. */
6049 else if (t
== ctx
->object
)
6052 if (tree
*p
= ctx
->global
->values
.get (t
))
6053 if (*p
!= NULL_TREE
)
6058 if (COMPLETE_TYPE_P (TREE_TYPE (t
))
6059 && is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
6061 /* If the class is empty, we aren't actually loading anything. */
6062 r
= build_constructor (TREE_TYPE (t
), NULL
);
6063 TREE_CONSTANT (r
) = true;
6065 else if (ctx
->strict
)
6066 r
= decl_really_constant_value (t
, /*unshare_p=*/false);
6068 r
= decl_constant_value (t
, /*unshare_p=*/false);
6069 if (TREE_CODE (r
) == TARGET_EXPR
6070 && TREE_CODE (TARGET_EXPR_INITIAL (r
)) == CONSTRUCTOR
)
6071 r
= TARGET_EXPR_INITIAL (r
);
6075 non_const_var_error (loc
, r
);
6076 *non_constant_p
= true;
6080 case DEBUG_BEGIN_STMT
:
6081 /* ??? It might be nice to retain this information somehow, so
6082 as to be able to step into a constexpr function call. */
6089 case CASE_LABEL_EXPR
:
6094 if (lval
&& !TYPE_REF_P (TREE_TYPE (t
)))
6096 else if (tree
*p
= ctx
->global
->values
.get (r
))
6099 /* Defer in case this is only used for its type. */;
6100 else if (COMPLETE_TYPE_P (TREE_TYPE (t
))
6101 && is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
6103 /* If the class is empty, we aren't actually loading anything. */
6104 r
= build_constructor (TREE_TYPE (t
), NULL
);
6105 TREE_CONSTANT (r
) = true;
6110 error ("%qE is not a constant expression", t
);
6111 *non_constant_p
= true;
6116 case AGGR_INIT_EXPR
:
6117 r
= cxx_eval_call_expression (ctx
, t
, lval
,
6118 non_constant_p
, overflow_p
);
6123 r
= DECL_EXPR_DECL (t
);
6124 if (TREE_CODE (r
) == USING_DECL
)
6129 if (AGGREGATE_TYPE_P (TREE_TYPE (r
))
6130 || VECTOR_TYPE_P (TREE_TYPE (r
)))
6134 new_ctx
.ctor
= build_constructor (TREE_TYPE (r
), NULL
);
6135 CONSTRUCTOR_NO_CLEARING (new_ctx
.ctor
) = true;
6136 ctx
->global
->values
.put (r
, new_ctx
.ctor
);
6140 if (tree init
= DECL_INITIAL (r
))
6142 init
= cxx_eval_constant_expression (ctx
, init
,
6144 non_constant_p
, overflow_p
);
6145 /* Don't share a CONSTRUCTOR that might be changed. */
6146 init
= unshare_constructor (init
);
6147 /* Remember that a constant object's constructor has already
6149 if (CLASS_TYPE_P (TREE_TYPE (r
))
6150 && CP_TYPE_CONST_P (TREE_TYPE (r
)))
6151 TREE_READONLY (init
) = true;
6152 ctx
->global
->values
.put (r
, init
);
6154 else if (ctx
== &new_ctx
)
6155 /* We gave it a CONSTRUCTOR above. */;
6157 ctx
->global
->values
.put (r
, NULL_TREE
);
6163 tree type
= TREE_TYPE (t
);
6165 if (!literal_type_p (type
))
6169 auto_diagnostic_group d
;
6170 error ("temporary of non-literal type %qT in a "
6171 "constant expression", type
);
6172 explain_non_literal_class (type
);
6174 *non_constant_p
= true;
6177 gcc_checking_assert (!TARGET_EXPR_DIRECT_INIT_P (t
));
6178 /* Avoid evaluating a TARGET_EXPR more than once. */
6179 tree slot
= TARGET_EXPR_SLOT (t
);
6180 if (tree
*p
= ctx
->global
->values
.get (slot
))
6187 if ((AGGREGATE_TYPE_P (type
) || VECTOR_TYPE_P (type
)))
6189 /* We're being expanded without an explicit target, so start
6190 initializing a new object; expansion with an explicit target
6191 strips the TARGET_EXPR before we get here. */
6193 /* Link CTX to NEW_CTX so that lookup_placeholder can resolve
6194 any PLACEHOLDER_EXPR within the initializer that refers to the
6195 former object under construction. */
6196 new_ctx
.parent
= ctx
;
6197 new_ctx
.ctor
= build_constructor (type
, NULL
);
6198 CONSTRUCTOR_NO_CLEARING (new_ctx
.ctor
) = true;
6199 new_ctx
.object
= slot
;
6200 ctx
->global
->values
.put (new_ctx
.object
, new_ctx
.ctor
);
6203 /* Pass false for 'lval' because this indicates
6204 initialization of a temporary. */
6205 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
6207 non_constant_p
, overflow_p
);
6208 if (*non_constant_p
)
6210 /* Adjust the type of the result to the type of the temporary. */
6211 r
= adjust_temp_type (type
, r
);
6212 if (TARGET_EXPR_CLEANUP (t
) && !CLEANUP_EH_ONLY (t
))
6213 ctx
->global
->cleanups
->safe_push (TARGET_EXPR_CLEANUP (t
));
6214 r
= unshare_constructor (r
);
6215 ctx
->global
->values
.put (slot
, r
);
6216 if (ctx
->save_exprs
)
6217 ctx
->save_exprs
->safe_push (slot
);
6225 gcc_assert (jump_target
== NULL
|| *jump_target
== NULL_TREE
);
6226 r
= cxx_eval_store_expression (ctx
, t
, lval
,
6227 non_constant_p
, overflow_p
);
6231 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
6233 non_constant_p
, overflow_p
);
6237 if (TREE_OPERAND (t
, 0) != NULL_TREE
)
6238 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
6240 non_constant_p
, overflow_p
);
6248 /* Can happen with ({ return true; }) && false; passed to
6249 maybe_constant_value. There is nothing to jump over in this
6250 case, and the bug will be diagnosed later. */
6251 gcc_assert (ctx
->quiet
);
6252 *non_constant_p
= true;
6257 /* Avoid evaluating a SAVE_EXPR more than once. */
6258 if (tree
*p
= ctx
->global
->values
.get (t
))
6262 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), false,
6263 non_constant_p
, overflow_p
);
6264 if (*non_constant_p
)
6266 ctx
->global
->values
.put (t
, r
);
6267 if (ctx
->save_exprs
)
6268 ctx
->save_exprs
->safe_push (t
);
6272 case TRY_CATCH_EXPR
:
6273 if (TREE_OPERAND (t
, 0) == NULL_TREE
)
6279 case NON_LVALUE_EXPR
:
6281 case MUST_NOT_THROW_EXPR
:
6284 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
6286 non_constant_p
, overflow_p
,
6290 case CLEANUP_POINT_EXPR
:
6292 auto_vec
<tree
, 2> cleanups
;
6293 vec
<tree
> *prev_cleanups
= ctx
->global
->cleanups
;
6294 ctx
->global
->cleanups
= &cleanups
;
6295 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
6297 non_constant_p
, overflow_p
,
6299 ctx
->global
->cleanups
= prev_cleanups
;
6302 /* Evaluate the cleanups. */
6303 FOR_EACH_VEC_ELT_REVERSE (cleanups
, i
, cleanup
)
6304 cxx_eval_constant_expression (ctx
, cleanup
, false,
6305 non_constant_p
, overflow_p
);
6309 case TRY_FINALLY_EXPR
:
6310 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), lval
,
6311 non_constant_p
, overflow_p
,
6313 if (!*non_constant_p
)
6314 /* Also evaluate the cleanup. */
6315 cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1), true,
6316 non_constant_p
, overflow_p
);
6320 r
= cxx_eval_constant_expression (ctx
, CLEANUP_BODY (t
), lval
,
6321 non_constant_p
, overflow_p
,
6323 if (!CLEANUP_EH_ONLY (t
) && !*non_constant_p
)
6325 iloc_sentinel
ils (loc
);
6326 /* Also evaluate the cleanup. */
6327 cxx_eval_constant_expression (ctx
, CLEANUP_EXPR (t
), true,
6328 non_constant_p
, overflow_p
);
6332 /* These differ from cxx_eval_unary_expression in that this doesn't
6333 check for a constant operand or result; an address can be
6334 constant without its operand being, and vice versa. */
6337 r
= cxx_eval_indirect_ref (ctx
, t
, lval
,
6338 non_constant_p
, overflow_p
);
6343 tree oldop
= TREE_OPERAND (t
, 0);
6344 tree op
= cxx_eval_constant_expression (ctx
, oldop
,
6346 non_constant_p
, overflow_p
);
6347 /* Don't VERIFY_CONSTANT here. */
6348 if (*non_constant_p
)
6350 gcc_checking_assert (TREE_CODE (op
) != CONSTRUCTOR
);
6351 /* This function does more aggressive folding than fold itself. */
6352 r
= build_fold_addr_expr_with_type (op
, TREE_TYPE (t
));
6353 if (TREE_CODE (r
) == ADDR_EXPR
&& TREE_OPERAND (r
, 0) == oldop
)
6365 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), lval
,
6366 non_constant_p
, overflow_p
);
6367 if (r
== error_mark_node
)
6369 else if (r
== TREE_OPERAND (t
, 0))
6372 r
= fold_build1 (TREE_CODE (t
), TREE_TYPE (t
), r
);
6377 case FIX_TRUNC_EXPR
:
6383 case TRUTH_NOT_EXPR
:
6384 case FIXED_CONVERT_EXPR
:
6385 r
= cxx_eval_unary_expression (ctx
, t
, lval
,
6386 non_constant_p
, overflow_p
);
6390 r
= fold_sizeof_expr (t
);
6391 /* In a template, fold_sizeof_expr may merely create a new SIZEOF_EXPR,
6392 which could lead to an infinite recursion. */
6393 if (TREE_CODE (r
) != SIZEOF_EXPR
)
6394 r
= cxx_eval_constant_expression (ctx
, r
, lval
,
6395 non_constant_p
, overflow_p
,
6399 *non_constant_p
= true;
6400 gcc_assert (ctx
->quiet
);
6407 /* check_return_expr sometimes wraps a TARGET_EXPR in a
6408 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
6409 introduced by build_call_a. */
6410 tree op0
= TREE_OPERAND (t
, 0);
6411 tree op1
= TREE_OPERAND (t
, 1);
6413 if ((TREE_CODE (op0
) == TARGET_EXPR
&& op1
== TARGET_EXPR_SLOT (op0
))
6414 || TREE_CODE (op1
) == EMPTY_CLASS_EXPR
)
6415 r
= cxx_eval_constant_expression (ctx
, op0
,
6416 lval
, non_constant_p
, overflow_p
,
6420 /* Check that the LHS is constant and then discard it. */
6421 cxx_eval_constant_expression (ctx
, op0
,
6422 true, non_constant_p
, overflow_p
,
6424 if (*non_constant_p
)
6426 op1
= TREE_OPERAND (t
, 1);
6427 r
= cxx_eval_constant_expression (ctx
, op1
,
6428 lval
, non_constant_p
, overflow_p
,
6434 case POINTER_PLUS_EXPR
:
6435 case POINTER_DIFF_EXPR
:
6439 case TRUNC_DIV_EXPR
:
6441 case FLOOR_DIV_EXPR
:
6442 case ROUND_DIV_EXPR
:
6443 case TRUNC_MOD_EXPR
:
6445 case ROUND_MOD_EXPR
:
6447 case EXACT_DIV_EXPR
:
6457 case TRUTH_XOR_EXPR
:
6464 case SPACESHIP_EXPR
:
6465 case UNORDERED_EXPR
:
6475 r
= cxx_eval_binary_expression (ctx
, t
, lval
,
6476 non_constant_p
, overflow_p
);
6479 /* fold can introduce non-IF versions of these; still treat them as
6480 short-circuiting. */
6481 case TRUTH_AND_EXPR
:
6482 case TRUTH_ANDIF_EXPR
:
6483 r
= cxx_eval_logical_expression (ctx
, t
, boolean_false_node
,
6486 non_constant_p
, overflow_p
);
6490 case TRUTH_ORIF_EXPR
:
6491 r
= cxx_eval_logical_expression (ctx
, t
, boolean_true_node
,
6494 non_constant_p
, overflow_p
);
6498 r
= cxx_eval_array_reference (ctx
, t
, lval
,
6499 non_constant_p
, overflow_p
);
6503 if (is_overloaded_fn (t
))
6505 /* We can only get here in checking mode via
6506 build_non_dependent_expr, because any expression that
6507 calls or takes the address of the function will have
6508 pulled a FUNCTION_DECL out of the COMPONENT_REF. */
6509 gcc_checking_assert (ctx
->quiet
|| errorcount
);
6510 *non_constant_p
= true;
6513 r
= cxx_eval_component_reference (ctx
, t
, lval
,
6514 non_constant_p
, overflow_p
);
6518 r
= cxx_eval_bit_field_ref (ctx
, t
, lval
,
6519 non_constant_p
, overflow_p
);
6524 if (jump_target
&& *jump_target
)
6526 tree orig_jump
= *jump_target
;
6527 tree arg
= ((TREE_CODE (t
) != IF_STMT
|| TREE_OPERAND (t
, 1))
6528 ? TREE_OPERAND (t
, 1) : void_node
);
6529 /* When jumping to a label, the label might be either in the
6530 then or else blocks, so process then block first in skipping
6531 mode first, and if we are still in the skipping mode at its end,
6532 process the else block too. */
6533 r
= cxx_eval_constant_expression (ctx
, arg
, lval
, non_constant_p
,
6534 overflow_p
, jump_target
);
6535 /* It's possible that we found the label in the then block. But
6536 it could have been followed by another jumping statement, e.g.
6537 say we're looking for case 1:
6540 // skipped statements
6541 case 1:; // clears up *jump_target
6542 return 1; // and sets it to a RETURN_EXPR
6545 in which case we need not go looking to the else block.
6546 (goto is not allowed in a constexpr function.) */
6547 if (*jump_target
== orig_jump
)
6549 arg
= ((TREE_CODE (t
) != IF_STMT
|| TREE_OPERAND (t
, 2))
6550 ? TREE_OPERAND (t
, 2) : void_node
);
6551 r
= cxx_eval_constant_expression (ctx
, arg
, lval
, non_constant_p
,
6552 overflow_p
, jump_target
);
6556 r
= cxx_eval_conditional_expression (ctx
, t
, lval
,
6557 non_constant_p
, overflow_p
,
6561 r
= cxx_eval_vector_conditional_expression (ctx
, t
, non_constant_p
,
6566 if (TREE_CONSTANT (t
) && reduced_constant_expression_p (t
))
6568 /* Don't re-process a constant CONSTRUCTOR, but do fold it to
6569 VECTOR_CST if applicable. */
6570 verify_constructor_flags (t
);
6571 if (TREE_CONSTANT (t
))
6574 r
= cxx_eval_bare_aggregate (ctx
, t
, lval
,
6575 non_constant_p
, overflow_p
);
6579 /* We can get this in a defaulted constructor for a class with a
6580 non-static data member of array type. Either the initializer will
6581 be NULL, meaning default-initialization, or it will be an lvalue
6582 or xvalue of the same type, meaning direct-initialization from the
6583 corresponding member. */
6584 r
= cxx_eval_vec_init (ctx
, t
, lval
,
6585 non_constant_p
, overflow_p
);
6589 r
= cxx_eval_trinary_expression (ctx
, t
, lval
,
6590 non_constant_p
, overflow_p
);
6594 if (REINTERPRET_CAST_P (t
))
6598 "%<reinterpret_cast%> is not a constant expression");
6599 *non_constant_p
= true;
6604 case VIEW_CONVERT_EXPR
:
6605 case UNARY_PLUS_EXPR
:
6607 tree oldop
= TREE_OPERAND (t
, 0);
6609 tree op
= cxx_eval_constant_expression (ctx
, oldop
,
6611 non_constant_p
, overflow_p
);
6612 if (*non_constant_p
)
6614 tree type
= TREE_TYPE (t
);
6616 if (VOID_TYPE_P (type
))
6619 if (TREE_CODE (t
) == CONVERT_EXPR
6620 && ARITHMETIC_TYPE_P (type
)
6621 && INDIRECT_TYPE_P (TREE_TYPE (op
)))
6625 "conversion from pointer type %qT to arithmetic type "
6626 "%qT in a constant expression", TREE_TYPE (op
), type
);
6627 *non_constant_p
= true;
6631 if (TREE_CODE (op
) == PTRMEM_CST
&& !TYPE_PTRMEM_P (type
))
6632 op
= cplus_expand_constant (op
);
6634 if (TREE_CODE (op
) == PTRMEM_CST
&& tcode
== NOP_EXPR
)
6636 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (op
))
6637 && !can_convert_qual (type
, op
))
6638 op
= cplus_expand_constant (op
);
6639 return cp_fold_convert (type
, op
);
6642 if (INDIRECT_TYPE_P (type
) && TREE_CODE (op
) == INTEGER_CST
)
6644 if (integer_zerop (op
))
6646 if (TYPE_REF_P (type
))
6650 "dereferencing a null pointer");
6651 *non_constant_p
= true;
6654 else if (TYPE_PTR_P (TREE_TYPE (op
)))
6656 tree from
= TREE_TYPE (op
);
6658 if (!can_convert (type
, from
, tf_none
))
6662 "conversion of %qT null pointer to %qT "
6663 "is not a constant expression",
6665 *non_constant_p
= true;
6672 /* This detects for example:
6673 reinterpret_cast<void*>(sizeof 0)
6676 error_at (loc
, "%<reinterpret_cast<%T>(%E)%> is not "
6677 "a constant expression",
6679 *non_constant_p
= true;
6684 if (INDIRECT_TYPE_P (type
)
6685 && TREE_CODE (op
) == NOP_EXPR
6686 && TREE_TYPE (op
) == ptr_type_node
6687 && TREE_CODE (TREE_OPERAND (op
, 0)) == ADDR_EXPR
6688 && VAR_P (TREE_OPERAND (TREE_OPERAND (op
, 0), 0))
6689 && (DECL_NAME (TREE_OPERAND (TREE_OPERAND (op
, 0),
6690 0)) == heap_uninit_identifier
6691 || DECL_NAME (TREE_OPERAND (TREE_OPERAND (op
, 0),
6692 0)) == heap_vec_uninit_identifier
))
6694 tree var
= TREE_OPERAND (TREE_OPERAND (op
, 0), 0);
6695 tree var_size
= TYPE_SIZE_UNIT (TREE_TYPE (var
));
6696 tree elt_type
= TREE_TYPE (type
);
6697 tree cookie_size
= NULL_TREE
;
6698 if (TREE_CODE (elt_type
) == RECORD_TYPE
6699 && TYPE_NAME (elt_type
) == heap_identifier
)
6701 tree fld1
= TYPE_FIELDS (elt_type
);
6702 tree fld2
= DECL_CHAIN (fld1
);
6703 elt_type
= TREE_TYPE (TREE_TYPE (fld2
));
6704 cookie_size
= TYPE_SIZE_UNIT (TREE_TYPE (fld1
));
6707 = (DECL_NAME (var
) == heap_uninit_identifier
6708 ? heap_identifier
: heap_vec_identifier
);
6710 = build_new_constexpr_heap_type (elt_type
, cookie_size
,
6712 TREE_TYPE (TREE_OPERAND (op
, 0))
6713 = build_pointer_type (TREE_TYPE (var
));
6716 if (op
== oldop
&& tcode
!= UNARY_PLUS_EXPR
)
6717 /* We didn't fold at the top so we could check for ptr-int
6723 /* Handle an array's bounds having been deduced after we built
6724 the wrapping expression. */
6725 if (same_type_ignoring_tlq_and_bounds_p (type
, TREE_TYPE (op
)))
6727 else if (sop
= tree_strip_nop_conversions (op
),
6728 sop
!= op
&& (same_type_ignoring_tlq_and_bounds_p
6729 (type
, TREE_TYPE (sop
))))
6731 else if (tcode
== UNARY_PLUS_EXPR
)
6732 r
= fold_convert (TREE_TYPE (t
), op
);
6734 r
= fold_build1 (tcode
, type
, op
);
6736 /* Conversion of an out-of-range value has implementation-defined
6737 behavior; the language considers it different from arithmetic
6738 overflow, which is undefined. */
6739 if (TREE_OVERFLOW_P (r
) && !TREE_OVERFLOW_P (op
))
6740 TREE_OVERFLOW (r
) = false;
6744 case EMPTY_CLASS_EXPR
:
6745 /* Handle EMPTY_CLASS_EXPR produced by build_call_a by lowering
6746 it to an appropriate CONSTRUCTOR. */
6747 return build_constructor (TREE_TYPE (t
), NULL
);
6749 case STATEMENT_LIST
:
6751 new_ctx
.ctor
= new_ctx
.object
= NULL_TREE
;
6752 return cxx_eval_statement_list (&new_ctx
, t
,
6753 non_constant_p
, overflow_p
, jump_target
);
6756 return cxx_eval_constant_expression (ctx
, BIND_EXPR_BODY (t
),
6758 non_constant_p
, overflow_p
,
6761 case PREINCREMENT_EXPR
:
6762 case POSTINCREMENT_EXPR
:
6763 case PREDECREMENT_EXPR
:
6764 case POSTDECREMENT_EXPR
:
6765 return cxx_eval_increment_expression (ctx
, t
,
6766 lval
, non_constant_p
, overflow_p
);
6772 case VEC_DELETE_EXPR
:
6775 /* GCC internal stuff. */
6777 case NON_DEPENDENT_EXPR
:
6781 error_at (loc
, "expression %qE is not a constant expression", t
);
6782 *non_constant_p
= true;
6786 /* Virtual function lookup. We don't need to do anything fancy. */
6787 return cxx_eval_constant_expression (ctx
, OBJ_TYPE_REF_EXPR (t
),
6788 lval
, non_constant_p
, overflow_p
);
6790 case PLACEHOLDER_EXPR
:
6791 /* Use of the value or address of the current object. */
6792 if (tree ctor
= lookup_placeholder (ctx
, lval
, TREE_TYPE (t
)))
6794 if (TREE_CODE (ctor
) == CONSTRUCTOR
)
6797 return cxx_eval_constant_expression (ctx
, ctor
, lval
,
6798 non_constant_p
, overflow_p
);
6800 /* A placeholder without a referent. We can get here when
6801 checking whether NSDMIs are noexcept, or in massage_init_elt;
6802 just say it's non-constant for now. */
6803 gcc_assert (ctx
->quiet
);
6804 *non_constant_p
= true;
6809 tree cond
= TREE_OPERAND (t
, 0);
6810 cond
= cxx_eval_constant_expression (ctx
, cond
, /*lval*/false,
6811 non_constant_p
, overflow_p
);
6812 VERIFY_CONSTANT (cond
);
6813 if (integer_nonzerop (cond
))
6819 *jump_target
= TREE_OPERAND (t
, 0);
6820 gcc_assert (breaks (jump_target
) || continues (jump_target
)
6821 /* Allow for jumping to a cdtor_label. */
6822 || returns (jump_target
));
6829 cxx_eval_loop_expr (ctx
, t
,
6830 non_constant_p
, overflow_p
, jump_target
);
6835 cxx_eval_switch_expr (ctx
, t
,
6836 non_constant_p
, overflow_p
, jump_target
);
6840 /* It's possible to get a requires-expression in a constant
6841 expression. For example:
6843 template<typename T> concept bool C() {
6844 return requires (T t) { t; };
6847 template<typename T> requires !C<T>() void f(T);
6849 Normalization leaves f with the associated constraint
6850 '!requires (T t) { ... }' which is not transformed into
6852 if (!processing_template_decl
)
6853 return satisfy_constraint_expression (t
);
6855 *non_constant_p
= true;
6859 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
6861 non_constant_p
, overflow_p
,
6869 case TEMPLATE_ID_EXPR
:
6871 /* We can evaluate template-id that refers to a concept only if
6872 the template arguments are non-dependent. */
6873 tree id
= unpack_concept_check (t
);
6874 tree tmpl
= TREE_OPERAND (id
, 0);
6875 if (!concept_definition_p (tmpl
))
6876 internal_error ("unexpected template-id %qE", t
);
6878 if (function_concept_p (tmpl
))
6881 error_at (cp_expr_loc_or_input_loc (t
),
6882 "function concept must be called");
6883 r
= error_mark_node
;
6887 if (!processing_template_decl
6888 && !uid_sensitive_constexpr_evaluation_p ())
6889 r
= evaluate_concept_check (t
, tf_warning_or_error
);
6891 *non_constant_p
= true;
6898 inline_asm_in_constexpr_error (loc
);
6899 *non_constant_p
= true;
6903 r
= cxx_eval_bit_cast (ctx
, t
, non_constant_p
, overflow_p
);
6907 if (STATEMENT_CODE_P (TREE_CODE (t
)))
6909 /* This function doesn't know how to deal with pre-genericize
6910 statements; this can only happen with statement-expressions,
6911 so for now just fail. */
6913 error_at (EXPR_LOCATION (t
),
6914 "statement is not a constant expression");
6917 internal_error ("unexpected expression %qE of kind %s", t
,
6918 get_tree_code_name (TREE_CODE (t
)));
6919 *non_constant_p
= true;
6923 if (r
== error_mark_node
)
6924 *non_constant_p
= true;
6926 if (*non_constant_p
)
6932 /* P0859: A function is needed for constant evaluation if it is a constexpr
6933 function that is named by an expression ([basic.def.odr]) that is
6934 potentially constant evaluated.
6936 So we need to instantiate any constexpr functions mentioned by the
6937 expression even if the definition isn't needed for evaluating the
6941 instantiate_cx_fn_r (tree
*tp
, int *walk_subtrees
, void */
*data*/
)
6943 if (TREE_CODE (*tp
) == FUNCTION_DECL
6944 && DECL_DECLARED_CONSTEXPR_P (*tp
)
6945 && !DECL_INITIAL (*tp
)
6946 && !trivial_fn_p (*tp
)
6947 && DECL_TEMPLOID_INSTANTIATION (*tp
)
6948 && !uid_sensitive_constexpr_evaluation_p ())
6951 instantiate_decl (*tp
, /*defer_ok*/false, /*expl_inst*/false);
6954 else if (TREE_CODE (*tp
) == CALL_EXPR
6955 || TREE_CODE (*tp
) == AGGR_INIT_EXPR
)
6957 if (EXPR_HAS_LOCATION (*tp
))
6958 input_location
= EXPR_LOCATION (*tp
);
6968 instantiate_constexpr_fns (tree t
)
6970 location_t loc
= input_location
;
6971 cp_walk_tree_without_duplicates (&t
, instantiate_cx_fn_r
, NULL
);
6972 input_location
= loc
;
6975 /* Look for heap variables in the expression *TP. */
6978 find_heap_var_refs (tree
*tp
, int *walk_subtrees
, void */
*data*/
)
6981 && (DECL_NAME (*tp
) == heap_uninit_identifier
6982 || DECL_NAME (*tp
) == heap_identifier
6983 || DECL_NAME (*tp
) == heap_vec_uninit_identifier
6984 || DECL_NAME (*tp
) == heap_vec_identifier
6985 || DECL_NAME (*tp
) == heap_deleted_identifier
))
6993 /* Find immediate function decls in *TP if any. */
6996 find_immediate_fndecl (tree
*tp
, int */
*walk_subtrees*/
, void */
*data*/
)
6998 if (TREE_CODE (*tp
) == FUNCTION_DECL
&& DECL_IMMEDIATE_FUNCTION_P (*tp
))
7003 /* ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
7004 STRICT has the same sense as for constant_value_1: true if we only allow
7005 conforming C++ constant expressions, or false if we want a constant value
7006 even if it doesn't conform.
7007 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
7008 per P0595 even when ALLOW_NON_CONSTANT is true.
7009 CONSTEXPR_DTOR is true when evaluating the dtor of a constexpr variable.
7010 OBJECT must be non-NULL in that case. */
7013 cxx_eval_outermost_constant_expr (tree t
, bool allow_non_constant
,
7015 bool manifestly_const_eval
= false,
7016 bool constexpr_dtor
= false,
7017 tree object
= NULL_TREE
)
7019 auto_timevar
time (TV_CONSTEXPR
);
7021 bool non_constant_p
= false;
7022 bool overflow_p
= false;
7024 if (BRACE_ENCLOSED_INITIALIZER_P (t
))
7026 gcc_checking_assert (allow_non_constant
);
7030 constexpr_global_ctx global_ctx
;
7031 constexpr_ctx ctx
= { &global_ctx
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
7032 allow_non_constant
, strict
,
7033 manifestly_const_eval
|| !allow_non_constant
};
7035 /* Turn off -frounding-math for manifestly constant evaluation. */
7036 warning_sentinel
rm (flag_rounding_math
, ctx
.manifestly_const_eval
);
7037 tree type
= initialized_type (t
);
7039 bool is_consteval
= false;
7040 if (VOID_TYPE_P (type
))
7043 /* Used for destructors of array elements. */
7044 type
= TREE_TYPE (object
);
7047 if (cxx_dialect
< cxx20
)
7049 if (TREE_CODE (t
) != CALL_EXPR
&& TREE_CODE (t
) != AGGR_INIT_EXPR
)
7051 /* Calls to immediate functions returning void need to be
7053 tree fndecl
= cp_get_callee_fndecl_nofold (t
);
7054 if (fndecl
== NULL_TREE
|| !DECL_IMMEDIATE_FUNCTION_P (fndecl
))
7057 is_consteval
= true;
7060 else if (cxx_dialect
>= cxx20
7061 && (TREE_CODE (t
) == CALL_EXPR
7062 || TREE_CODE (t
) == AGGR_INIT_EXPR
7063 || TREE_CODE (t
) == TARGET_EXPR
))
7065 /* For non-concept checks, determine if it is consteval. */
7066 if (!concept_check_p (t
))
7069 if (TREE_CODE (x
) == TARGET_EXPR
)
7070 x
= TARGET_EXPR_INITIAL (x
);
7071 tree fndecl
= cp_get_callee_fndecl_nofold (x
);
7072 if (fndecl
&& DECL_IMMEDIATE_FUNCTION_P (fndecl
))
7073 is_consteval
= true;
7076 if (AGGREGATE_TYPE_P (type
) || VECTOR_TYPE_P (type
))
7078 /* In C++14 an NSDMI can participate in aggregate initialization,
7079 and can refer to the address of the object being initialized, so
7080 we need to pass in the relevant VAR_DECL if we want to do the
7081 evaluation in a single pass. The evaluation will dynamically
7082 update ctx.values for the VAR_DECL. We use the same strategy
7083 for C++11 constexpr constructors that refer to the object being
7087 gcc_assert (object
&& VAR_P (object
));
7088 gcc_assert (DECL_DECLARED_CONSTEXPR_P (object
));
7089 gcc_assert (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object
));
7090 if (error_operand_p (DECL_INITIAL (object
)))
7092 ctx
.ctor
= unshare_expr (DECL_INITIAL (object
));
7093 TREE_READONLY (ctx
.ctor
) = false;
7094 /* Temporarily force decl_really_constant_value to return false
7095 for it, we want to use ctx.ctor for the current value instead. */
7096 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object
) = false;
7100 ctx
.ctor
= build_constructor (type
, NULL
);
7101 CONSTRUCTOR_NO_CLEARING (ctx
.ctor
) = true;
7105 if (TREE_CODE (t
) == TARGET_EXPR
)
7106 object
= TARGET_EXPR_SLOT (t
);
7107 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
)
7108 object
= AGGR_INIT_EXPR_SLOT (t
);
7110 ctx
.object
= object
;
7112 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7113 (type
, TREE_TYPE (object
)));
7114 if (object
&& DECL_P (object
))
7115 global_ctx
.values
.put (object
, ctx
.ctor
);
7116 if (TREE_CODE (r
) == TARGET_EXPR
)
7117 /* Avoid creating another CONSTRUCTOR when we expand the
7119 r
= TARGET_EXPR_INITIAL (r
);
7122 auto_vec
<tree
, 16> cleanups
;
7123 global_ctx
.cleanups
= &cleanups
;
7125 instantiate_constexpr_fns (r
);
7126 r
= cxx_eval_constant_expression (&ctx
, r
,
7127 false, &non_constant_p
, &overflow_p
);
7129 if (!constexpr_dtor
)
7130 verify_constant (r
, allow_non_constant
, &non_constant_p
, &overflow_p
);
7132 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object
) = true;
7136 /* Evaluate the cleanups. */
7137 FOR_EACH_VEC_ELT_REVERSE (cleanups
, i
, cleanup
)
7138 cxx_eval_constant_expression (&ctx
, cleanup
, false,
7139 &non_constant_p
, &overflow_p
);
7141 /* Mutable logic is a bit tricky: we want to allow initialization of
7142 constexpr variables with mutable members, but we can't copy those
7143 members to another constexpr variable. */
7144 if (TREE_CODE (r
) == CONSTRUCTOR
&& CONSTRUCTOR_MUTABLE_POISON (r
))
7146 if (!allow_non_constant
)
7147 error ("%qE is not a constant expression because it refers to "
7148 "mutable subobjects of %qT", t
, type
);
7149 non_constant_p
= true;
7152 if (TREE_CODE (r
) == CONSTRUCTOR
&& CONSTRUCTOR_NO_CLEARING (r
))
7154 if (!allow_non_constant
)
7155 error ("%qE is not a constant expression because it refers to "
7156 "an incompletely initialized variable", t
);
7157 TREE_CONSTANT (r
) = false;
7158 non_constant_p
= true;
7161 if (!global_ctx
.heap_vars
.is_empty ())
7163 tree heap_var
= cp_walk_tree_without_duplicates (&r
, find_heap_var_refs
,
7168 if (!allow_non_constant
&& !non_constant_p
)
7169 error_at (DECL_SOURCE_LOCATION (heap_var
),
7170 "%qE is not a constant expression because it refers to "
7171 "a result of %<operator new%>", t
);
7173 non_constant_p
= true;
7175 FOR_EACH_VEC_ELT (global_ctx
.heap_vars
, i
, heap_var
)
7176 if (DECL_NAME (heap_var
) != heap_deleted_identifier
)
7178 if (!allow_non_constant
&& !non_constant_p
)
7179 error_at (DECL_SOURCE_LOCATION (heap_var
),
7180 "%qE is not a constant expression because allocated "
7181 "storage has not been deallocated", t
);
7183 non_constant_p
= true;
7187 /* Check that immediate invocation does not return an expression referencing
7188 any immediate function decls. They need to be allowed while parsing
7189 immediate functions, but can't leak outside of them. */
7192 && (current_function_decl
== NULL_TREE
7193 || !DECL_IMMEDIATE_FUNCTION_P (current_function_decl
)))
7194 if (tree immediate_fndecl
7195 = cp_walk_tree_without_duplicates (&r
, find_immediate_fndecl
,
7198 if (!allow_non_constant
&& !non_constant_p
)
7199 error_at (cp_expr_loc_or_input_loc (t
),
7200 "immediate evaluation returns address of immediate "
7201 "function %qD", immediate_fndecl
);
7203 non_constant_p
= true;
7207 /* If we saw something bad, go back to our argument. The wrapping below is
7208 only for the cases of TREE_CONSTANT argument or overflow. */
7211 if (!non_constant_p
&& overflow_p
)
7212 non_constant_p
= true;
7214 /* Unshare the result. */
7215 bool should_unshare
= true;
7216 if (r
== t
|| (TREE_CODE (t
) == TARGET_EXPR
7217 && TARGET_EXPR_INITIAL (t
) == r
))
7218 should_unshare
= false;
7220 if (non_constant_p
&& !allow_non_constant
)
7221 return error_mark_node
;
7222 else if (constexpr_dtor
)
7224 else if (non_constant_p
&& TREE_CONSTANT (r
))
7226 /* This isn't actually constant, so unset TREE_CONSTANT.
7227 Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires
7228 it to be set if it is invariant address, even when it is not
7229 a valid C++ constant expression. Wrap it with a NOP_EXPR
7231 if (EXPR_P (r
) && TREE_CODE (r
) != ADDR_EXPR
)
7233 else if (TREE_CODE (r
) == CONSTRUCTOR
)
7234 r
= build1 (VIEW_CONVERT_EXPR
, TREE_TYPE (r
), r
);
7236 r
= build_nop (TREE_TYPE (r
), r
);
7237 TREE_CONSTANT (r
) = false;
7239 else if (non_constant_p
)
7243 r
= unshare_expr (r
);
7245 if (TREE_CODE (r
) == CONSTRUCTOR
&& CLASS_TYPE_P (TREE_TYPE (r
)))
7247 r
= adjust_temp_type (type
, r
);
7248 if (TREE_CODE (t
) == TARGET_EXPR
7249 && TARGET_EXPR_INITIAL (t
) == r
)
7251 else if (TREE_CODE (t
) != CONSTRUCTOR
)
7253 r
= get_target_expr_sfinae (r
, tf_warning_or_error
| tf_no_cleanup
);
7254 TREE_CONSTANT (r
) = true;
7261 /* If T represents a constant expression returns its reduced value.
7262 Otherwise return error_mark_node. If T is dependent, then
7266 cxx_constant_value (tree t
, tree decl
)
7268 return cxx_eval_outermost_constant_expr (t
, false, true, true, false, decl
);
7271 /* Like cxx_constant_value, but used for evaluation of constexpr destructors
7272 of constexpr variables. The actual initializer of DECL is not modified. */
7275 cxx_constant_dtor (tree t
, tree decl
)
7277 cxx_eval_outermost_constant_expr (t
, false, true, true, true, decl
);
7280 /* Helper routine for fold_simple function. Either return simplified
7281 expression T, otherwise NULL_TREE.
7282 In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold
7283 even if we are within template-declaration. So be careful on call, as in
7284 such case types can be undefined. */
7287 fold_simple_1 (tree t
)
7290 enum tree_code code
= TREE_CODE (t
);
7302 return fold_sizeof_expr (t
);
7311 case TRUTH_NOT_EXPR
:
7313 case VIEW_CONVERT_EXPR
:
7316 case FIX_TRUNC_EXPR
:
7317 case FIXED_CONVERT_EXPR
:
7318 case ADDR_SPACE_CONVERT_EXPR
:
7320 op1
= TREE_OPERAND (t
, 0);
7322 t
= const_unop (code
, TREE_TYPE (t
), op1
);
7326 if (CONVERT_EXPR_CODE_P (code
)
7327 && TREE_OVERFLOW_P (t
) && !TREE_OVERFLOW_P (op1
))
7328 TREE_OVERFLOW (t
) = false;
7336 /* If T is a simple constant expression, returns its simplified value.
7337 Otherwise returns T. In contrast to maybe_constant_value we
7338 simplify only few operations on constant-expressions, and we don't
7339 try to simplify constexpressions. */
7342 fold_simple (tree t
)
7344 if (processing_template_decl
)
7347 tree r
= fold_simple_1 (t
);
7354 /* If T is a constant expression, returns its reduced value.
7355 Otherwise, if T does not have TREE_CONSTANT set, returns T.
7356 Otherwise, returns a version of T without TREE_CONSTANT.
7357 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated
7360 static GTY((deletable
)) hash_map
<tree
, tree
> *cv_cache
;
7363 maybe_constant_value (tree t
, tree decl
, bool manifestly_const_eval
)
7367 if (!is_nondependent_constant_expression (t
))
7369 if (TREE_OVERFLOW_P (t
))
7371 t
= build_nop (TREE_TYPE (t
), t
);
7372 TREE_CONSTANT (t
) = false;
7376 else if (CONSTANT_CLASS_P (t
))
7377 /* No caching or evaluation needed. */
7380 if (manifestly_const_eval
)
7381 return cxx_eval_outermost_constant_expr (t
, true, true, true, false, decl
);
7383 if (cv_cache
== NULL
)
7384 cv_cache
= hash_map
<tree
, tree
>::create_ggc (101);
7385 if (tree
*cached
= cv_cache
->get (t
))
7390 r
= break_out_target_exprs (r
, /*clear_loc*/true);
7391 protected_set_expr_location (r
, EXPR_LOCATION (t
));
7396 uid_sensitive_constexpr_evaluation_checker c
;
7397 r
= cxx_eval_outermost_constant_expr (t
, true, true, false, false, decl
);
7398 gcc_checking_assert (r
== t
7399 || CONVERT_EXPR_P (t
)
7400 || TREE_CODE (t
) == VIEW_CONVERT_EXPR
7401 || (TREE_CONSTANT (t
) && !TREE_CONSTANT (r
))
7402 || !cp_tree_equal (r
, t
));
7403 if (!c
.evaluation_restricted_p ())
7404 cv_cache
->put (t
, r
);
7408 /* Dispose of the whole CV_CACHE. */
7411 clear_cv_cache (void)
7413 if (cv_cache
!= NULL
)
7417 /* Dispose of the whole CV_CACHE and FOLD_CACHE. */
7420 clear_cv_and_fold_caches ()
7423 clear_fold_cache ();
7426 /* Internal function handling expressions in templates for
7427 fold_non_dependent_expr and fold_non_dependent_init.
7429 If we're in a template, but T isn't value dependent, simplify
7430 it. We're supposed to treat:
7432 template <typename T> void f(T[1 + 1]);
7433 template <typename T> void f(T[2]);
7435 as two declarations of the same function, for example. */
7438 fold_non_dependent_expr_template (tree t
, tsubst_flags_t complain
,
7439 bool manifestly_const_eval
,
7442 gcc_assert (processing_template_decl
);
7444 if (is_nondependent_constant_expression (t
))
7446 processing_template_decl_sentinel s
;
7447 t
= instantiate_non_dependent_expr_internal (t
, complain
);
7449 if (type_unknown_p (t
) || BRACE_ENCLOSED_INITIALIZER_P (t
))
7451 if (TREE_OVERFLOW_P (t
))
7453 t
= build_nop (TREE_TYPE (t
), t
);
7454 TREE_CONSTANT (t
) = false;
7459 tree r
= cxx_eval_outermost_constant_expr (t
, true, true,
7460 manifestly_const_eval
,
7462 /* cp_tree_equal looks through NOPs, so allow them. */
7463 gcc_checking_assert (r
== t
7464 || CONVERT_EXPR_P (t
)
7465 || TREE_CODE (t
) == VIEW_CONVERT_EXPR
7466 || (TREE_CONSTANT (t
) && !TREE_CONSTANT (r
))
7467 || !cp_tree_equal (r
, t
));
7470 else if (TREE_OVERFLOW_P (t
))
7472 t
= build_nop (TREE_TYPE (t
), t
);
7473 TREE_CONSTANT (t
) = false;
7479 /* Like maybe_constant_value but first fully instantiate the argument.
7481 Note: this is equivalent to instantiate_non_dependent_expr_sfinae
7482 (t, complain) followed by maybe_constant_value but is more efficient,
7483 because it calls instantiation_dependent_expression_p and
7484 potential_constant_expression at most once.
7485 The manifestly_const_eval argument is passed to maybe_constant_value.
7487 Callers should generally pass their active complain, or if they are in a
7488 non-template, diagnosing context, they can use the default of
7489 tf_warning_or_error. Callers that might be within a template context, don't
7490 have a complain parameter, and aren't going to remember the result for long
7491 (e.g. null_ptr_cst_p), can pass tf_none and deal with error_mark_node
7495 fold_non_dependent_expr (tree t
,
7496 tsubst_flags_t complain
/* = tf_warning_or_error */,
7497 bool manifestly_const_eval
/* = false */,
7498 tree object
/* = NULL_TREE */)
7503 if (processing_template_decl
)
7504 return fold_non_dependent_expr_template (t
, complain
,
7505 manifestly_const_eval
, object
);
7507 return maybe_constant_value (t
, object
, manifestly_const_eval
);
7510 /* Like fold_non_dependent_expr, but if EXPR couldn't be folded to a constant,
7511 return the original expression. */
7514 maybe_fold_non_dependent_expr (tree expr
,
7515 tsubst_flags_t complain
/*=tf_warning_or_error*/)
7517 tree t
= fold_non_dependent_expr (expr
, complain
);
7518 if (t
&& TREE_CONSTANT (t
))
7524 /* Like maybe_constant_init but first fully instantiate the argument. */
7527 fold_non_dependent_init (tree t
,
7528 tsubst_flags_t complain
/*=tf_warning_or_error*/,
7529 bool manifestly_const_eval
/*=false*/,
7530 tree object
/* = NULL_TREE */)
7535 if (processing_template_decl
)
7537 t
= fold_non_dependent_expr_template (t
, complain
,
7538 manifestly_const_eval
, object
);
7539 /* maybe_constant_init does this stripping, so do it here too. */
7540 if (TREE_CODE (t
) == TARGET_EXPR
)
7542 tree init
= TARGET_EXPR_INITIAL (t
);
7543 if (TREE_CODE (init
) == CONSTRUCTOR
)
7549 return maybe_constant_init (t
, object
, manifestly_const_eval
);
7552 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
7553 than wrapped in a TARGET_EXPR.
7554 ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
7555 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
7556 per P0595 even when ALLOW_NON_CONSTANT is true. */
7559 maybe_constant_init_1 (tree t
, tree decl
, bool allow_non_constant
,
7560 bool manifestly_const_eval
)
7564 if (TREE_CODE (t
) == EXPR_STMT
)
7565 t
= TREE_OPERAND (t
, 0);
7566 if (TREE_CODE (t
) == CONVERT_EXPR
7567 && VOID_TYPE_P (TREE_TYPE (t
)))
7568 t
= TREE_OPERAND (t
, 0);
7569 if (TREE_CODE (t
) == INIT_EXPR
)
7570 t
= TREE_OPERAND (t
, 1);
7571 if (TREE_CODE (t
) == TARGET_EXPR
)
7572 t
= TARGET_EXPR_INITIAL (t
);
7573 if (!is_nondependent_static_init_expression (t
))
7574 /* Don't try to evaluate it. */;
7575 else if (CONSTANT_CLASS_P (t
) && allow_non_constant
)
7576 /* No evaluation needed. */;
7578 t
= cxx_eval_outermost_constant_expr (t
, allow_non_constant
,
7580 manifestly_const_eval
, false, decl
);
7581 if (TREE_CODE (t
) == TARGET_EXPR
)
7583 tree init
= TARGET_EXPR_INITIAL (t
);
7584 if (TREE_CODE (init
) == CONSTRUCTOR
)
7590 /* Wrapper for maybe_constant_init_1 which permits non constants. */
7593 maybe_constant_init (tree t
, tree decl
, bool manifestly_const_eval
)
7595 return maybe_constant_init_1 (t
, decl
, true, manifestly_const_eval
);
7598 /* Wrapper for maybe_constant_init_1 which does not permit non constants. */
7601 cxx_constant_init (tree t
, tree decl
)
7603 return maybe_constant_init_1 (t
, decl
, false, true);
7607 /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
7608 /* Return true if the object referred to by REF has automatic or thread
7611 enum { ck_ok
, ck_bad
, ck_unknown
};
7613 check_automatic_or_tls (tree ref
)
7616 poly_int64 bitsize
, bitpos
;
7618 int volatilep
= 0, unsignedp
= 0;
7619 tree decl
= get_inner_reference (ref
, &bitsize
, &bitpos
, &offset
,
7620 &mode
, &unsignedp
, &volatilep
, false);
7623 /* If there isn't a decl in the middle, we don't know the linkage here,
7624 and this isn't a constant expression anyway. */
7627 dk
= decl_storage_duration (decl
);
7628 return (dk
== dk_auto
|| dk
== dk_thread
) ? ck_bad
: ck_ok
;
7632 /* Data structure for passing data from potential_constant_expression_1
7633 to check_for_return_continue via cp_walk_tree. */
7634 struct check_for_return_continue_data
{
7635 hash_set
<tree
> *pset
;
7639 /* Helper function for potential_constant_expression_1 SWITCH_STMT handling,
7640 called through cp_walk_tree. Return the first RETURN_EXPR found, or note
7641 the first CONTINUE_STMT if RETURN_EXPR is not found. */
7643 check_for_return_continue (tree
*tp
, int *walk_subtrees
, void *data
)
7646 check_for_return_continue_data
*d
= (check_for_return_continue_data
*) data
;
7647 switch (TREE_CODE (t
))
7653 if (d
->continue_stmt
== NULL_TREE
)
7654 d
->continue_stmt
= t
;
7658 if (tree r = cp_walk_tree (&x, check_for_return_continue, data, \
7662 /* For loops, walk subtrees manually, so that continue stmts found
7663 inside of the bodies of the loops are ignored. */
7666 RECUR (DO_COND (t
));
7667 s
= d
->continue_stmt
;
7668 RECUR (DO_BODY (t
));
7669 d
->continue_stmt
= s
;
7674 RECUR (WHILE_COND (t
));
7675 s
= d
->continue_stmt
;
7676 RECUR (WHILE_BODY (t
));
7677 d
->continue_stmt
= s
;
7682 RECUR (FOR_INIT_STMT (t
));
7683 RECUR (FOR_COND (t
));
7684 RECUR (FOR_EXPR (t
));
7685 s
= d
->continue_stmt
;
7686 RECUR (FOR_BODY (t
));
7687 d
->continue_stmt
= s
;
7690 case RANGE_FOR_STMT
:
7692 RECUR (RANGE_FOR_EXPR (t
));
7693 s
= d
->continue_stmt
;
7694 RECUR (RANGE_FOR_BODY (t
));
7695 d
->continue_stmt
= s
;
7699 case STATEMENT_LIST
:
7712 /* Return true if T denotes a potentially constant expression. Issue
7713 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
7714 an lvalue-rvalue conversion is implied. If NOW is true, we want to
7715 consider the expression in the current context, independent of constexpr
7718 C++0x [expr.const] used to say
7720 6 An expression is a potential constant expression if it is
7721 a constant expression where all occurrences of function
7722 parameters are replaced by arbitrary constant expressions
7723 of the appropriate type.
7725 2 A conditional expression is a constant expression unless it
7726 involves one of the following as a potentially evaluated
7727 subexpression (3.2), but subexpressions of logical AND (5.14),
7728 logical OR (5.15), and conditional (5.16) operations that are
7729 not evaluated are not considered. */
7732 potential_constant_expression_1 (tree t
, bool want_rval
, bool strict
, bool now
,
7733 tsubst_flags_t flags
, tree
*jump_target
)
7735 #define RECUR(T,RV) \
7736 potential_constant_expression_1 ((T), (RV), strict, now, flags, jump_target)
7738 enum { any
= false, rval
= true };
7742 if (t
== error_mark_node
)
7746 location_t loc
= cp_expr_loc_or_input_loc (t
);
7749 /* If we are jumping, ignore everything. This is simpler than the
7750 cxx_eval_constant_expression handling because we only need to be
7751 conservatively correct, and we don't necessarily have a constant value
7752 available, so we don't bother with switch tracking. */
7755 if (TREE_THIS_VOLATILE (t
) && want_rval
)
7757 if (flags
& tf_error
)
7758 error_at (loc
, "lvalue-to-rvalue conversion of a volatile lvalue "
7759 "%qE with type %qT", t
, TREE_TYPE (t
));
7762 if (CONSTANT_CLASS_P (t
))
7764 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_TYPED
)
7765 && TREE_TYPE (t
) == error_mark_node
)
7768 switch (TREE_CODE (t
))
7774 case TEMPLATE_ID_EXPR
:
7776 case CASE_LABEL_EXPR
:
7783 case TEMPLATE_PARM_INDEX
:
7785 case IDENTIFIER_NODE
:
7786 case USERDEF_LITERAL
:
7787 /* We can see a FIELD_DECL in a pointer-to-member expression. */
7792 case PLACEHOLDER_EXPR
:
7795 case DEBUG_BEGIN_STMT
:
7799 if (!RECUR (TREE_OPERAND (t
, 0), any
))
7809 if (now
&& want_rval
)
7811 tree type
= TREE_TYPE (t
);
7812 if ((processing_template_decl
&& !COMPLETE_TYPE_P (type
))
7813 || dependent_type_p (type
)
7814 || is_really_empty_class (type
, /*ignore_vptr*/false))
7815 /* An empty class has no data to read. */
7817 if (flags
& tf_error
)
7818 error ("%qE is not a constant expression", t
);
7823 case AGGR_INIT_EXPR
:
7825 /* -- an invocation of a function other than a constexpr function
7826 or a constexpr constructor. */
7828 tree fun
= get_function_named_in_call (t
);
7829 const int nargs
= call_expr_nargs (t
);
7832 if (fun
== NULL_TREE
)
7834 /* Reset to allow the function to continue past the end
7835 of the block below. Otherwise return early. */
7838 if (TREE_CODE (t
) == CALL_EXPR
7839 && CALL_EXPR_FN (t
) == NULL_TREE
)
7840 switch (CALL_EXPR_IFN (t
))
7842 /* These should be ignored, they are optimized away from
7843 constexpr functions. */
7844 case IFN_UBSAN_NULL
:
7845 case IFN_UBSAN_BOUNDS
:
7846 case IFN_UBSAN_VPTR
:
7847 case IFN_FALLTHROUGH
:
7850 case IFN_ADD_OVERFLOW
:
7851 case IFN_SUB_OVERFLOW
:
7852 case IFN_MUL_OVERFLOW
:
7854 case IFN_VEC_CONVERT
:
7864 /* fold_call_expr can't do anything with IFN calls. */
7865 if (flags
& tf_error
)
7866 error_at (loc
, "call to internal function %qE", t
);
7871 if (fun
&& is_overloaded_fn (fun
))
7873 if (TREE_CODE (fun
) == FUNCTION_DECL
)
7875 if (builtin_valid_in_constant_expr_p (fun
))
7877 if (!DECL_DECLARED_CONSTEXPR_P (fun
)
7878 /* Allow any built-in function; if the expansion
7879 isn't constant, we'll deal with that then. */
7880 && !fndecl_built_in_p (fun
)
7881 /* In C++20, replaceable global allocation functions
7882 are constant expressions. */
7883 && (!cxx_replaceable_global_alloc_fn (fun
)
7884 || TREE_CODE (t
) != CALL_EXPR
7885 || (!CALL_FROM_NEW_OR_DELETE_P (t
)
7886 && (current_function_decl
== NULL_TREE
7887 || !is_std_allocator_allocate
7888 (current_function_decl
))))
7889 /* Allow placement new in std::construct_at. */
7890 && (!cxx_placement_new_fn (fun
)
7891 || TREE_CODE (t
) != CALL_EXPR
7892 || current_function_decl
== NULL_TREE
7893 || !is_std_construct_at (current_function_decl
))
7894 && !cxx_dynamic_cast_fn_p (fun
))
7896 if (flags
& tf_error
)
7898 error_at (loc
, "call to non-%<constexpr%> function %qD",
7900 explain_invalid_constexpr_fn (fun
);
7904 /* A call to a non-static member function takes the address
7905 of the object as the first argument. But in a constant
7906 expression the address will be folded away, so look
7908 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun
)
7909 && !DECL_CONSTRUCTOR_P (fun
))
7911 tree x
= get_nth_callarg (t
, 0);
7912 if (is_this_parameter (x
))
7914 /* Don't require an immediately constant value, as
7915 constexpr substitution might not use the value. */
7916 bool sub_now
= false;
7917 if (!potential_constant_expression_1 (x
, rval
, strict
,
7926 if (!RECUR (fun
, true))
7928 fun
= get_first_fn (fun
);
7930 /* Skip initial arguments to base constructors. */
7931 if (DECL_BASE_CONSTRUCTOR_P (fun
))
7932 i
= num_artificial_parms_for (fun
);
7933 fun
= DECL_ORIGIN (fun
);
7937 if (RECUR (fun
, rval
))
7938 /* Might end up being a constant function pointer. */;
7942 for (; i
< nargs
; ++i
)
7944 tree x
= get_nth_callarg (t
, i
);
7945 /* In a template, reference arguments haven't been converted to
7946 REFERENCE_TYPE and we might not even know if the parameter
7947 is a reference, so accept lvalue constants too. */
7948 bool rv
= processing_template_decl
? any
: rval
;
7949 /* Don't require an immediately constant value, as constexpr
7950 substitution might not use the value of the argument. */
7951 bool sub_now
= false;
7952 if (!potential_constant_expression_1 (x
, rv
, strict
,
7953 sub_now
, flags
, jump_target
))
7959 case NON_LVALUE_EXPR
:
7960 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
7961 -- an lvalue of integral type that refers to a non-volatile
7962 const variable or static data member initialized with
7963 constant expressions, or
7965 -- an lvalue of literal type that refers to non-volatile
7966 object defined with constexpr, or that refers to a
7967 sub-object of such an object; */
7968 return RECUR (TREE_OPERAND (t
, 0), rval
);
7971 if (DECL_HAS_VALUE_EXPR_P (t
))
7973 if (now
&& is_normal_capture_proxy (t
))
7975 /* -- in a lambda-expression, a reference to this or to a
7976 variable with automatic storage duration defined outside that
7977 lambda-expression, where the reference would be an
7981 /* Since we're doing an lvalue-rvalue conversion, this might
7982 not be an odr-use, so evaluate the variable directly. */
7983 return RECUR (DECL_CAPTURED_VARIABLE (t
), rval
);
7985 if (flags
& tf_error
)
7987 tree cap
= DECL_CAPTURED_VARIABLE (t
);
7988 error ("lambda capture of %qE is not a constant expression",
7990 if (decl_constant_var_p (cap
))
7991 inform (input_location
, "because it is used as a glvalue");
7995 /* Treat __PRETTY_FUNCTION__ inside a template function as
7996 potentially-constant. */
7997 else if (DECL_PRETTY_FUNCTION_P (t
)
7998 && DECL_VALUE_EXPR (t
) == error_mark_node
)
8000 return RECUR (DECL_VALUE_EXPR (t
), rval
);
8003 && !var_in_maybe_constexpr_fn (t
)
8004 && !type_dependent_expression_p (t
)
8005 && !decl_maybe_constant_var_p (t
)
8007 || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t
))
8008 || (DECL_INITIAL (t
)
8009 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t
)))
8010 && COMPLETE_TYPE_P (TREE_TYPE (t
))
8011 && !is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
8013 if (flags
& tf_error
)
8014 non_const_var_error (loc
, t
);
8020 if (REINTERPRET_CAST_P (t
))
8022 if (flags
& tf_error
)
8023 error_at (loc
, "%<reinterpret_cast%> is not a constant expression");
8028 case VIEW_CONVERT_EXPR
:
8029 /* -- a reinterpret_cast. FIXME not implemented, and this rule
8030 may change to something more specific to type-punning (DR 1312). */
8032 tree from
= TREE_OPERAND (t
, 0);
8033 if (location_wrapper_p (t
))
8034 return (RECUR (from
, want_rval
));
8035 if (INDIRECT_TYPE_P (TREE_TYPE (t
)))
8037 STRIP_ANY_LOCATION_WRAPPER (from
);
8038 if (TREE_CODE (from
) == INTEGER_CST
8039 && !integer_zerop (from
))
8041 if (flags
& tf_error
)
8043 "%<reinterpret_cast%> from integer to pointer");
8047 return (RECUR (from
, TREE_CODE (t
) != VIEW_CONVERT_EXPR
));
8050 case ADDRESSOF_EXPR
:
8051 /* This is like ADDR_EXPR, except it won't form pointer-to-member. */
8052 t
= TREE_OPERAND (t
, 0);
8053 goto handle_addr_expr
;
8056 /* -- a unary operator & that is applied to an lvalue that
8057 designates an object with thread or automatic storage
8059 t
= TREE_OPERAND (t
, 0);
8061 if (TREE_CODE (t
) == OFFSET_REF
&& PTRMEM_OK_P (t
))
8062 /* A pointer-to-member constant. */
8067 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
8068 any checking here, as we might dereference the pointer later. If
8069 we remove this code, also remove check_automatic_or_tls. */
8070 i
= check_automatic_or_tls (t
);
8075 if (flags
& tf_error
)
8076 error ("address-of an object %qE with thread local or "
8077 "automatic storage is not a constant expression", t
);
8081 return RECUR (t
, any
);
8086 /* -- a class member access unless its postfix-expression is
8087 of literal type or of pointer to literal type. */
8088 /* This test would be redundant, as it follows from the
8089 postfix-expression being a potential constant expression. */
8090 if (type_unknown_p (t
))
8092 if (is_overloaded_fn (t
))
8093 /* In a template, a COMPONENT_REF of a function expresses ob.fn(),
8094 which uses ob as an lvalue. */
8101 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
8103 case EXPR_PACK_EXPANSION
:
8104 return RECUR (PACK_EXPANSION_PATTERN (t
), want_rval
);
8108 tree x
= TREE_OPERAND (t
, 0);
8110 if (is_this_parameter (x
) && !is_capture_proxy (x
))
8112 if (!var_in_maybe_constexpr_fn (x
))
8114 if (flags
& tf_error
)
8115 error_at (loc
, "use of %<this%> in a constant expression");
8120 return RECUR (x
, rval
);
8123 case STATEMENT_LIST
:
8125 tree_stmt_iterator i
;
8126 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
8128 if (!RECUR (tsi_stmt (i
), any
))
8136 if (cxx_dialect
< cxx14
)
8138 if (!RECUR (TREE_OPERAND (t
, 0), any
))
8140 /* Just ignore clobbers. */
8141 if (TREE_CLOBBER_P (TREE_OPERAND (t
, 1)))
8143 if (!RECUR (TREE_OPERAND (t
, 1), rval
))
8148 if (cxx_dialect
< cxx14
)
8150 if (!RECUR (TREE_OPERAND (t
, 0), rval
))
8152 if (!RECUR (TREE_OPERAND (t
, 2), rval
))
8157 if (!RECUR (DO_COND (t
), rval
))
8159 if (!RECUR (DO_BODY (t
), any
))
8161 if (breaks (jump_target
) || continues (jump_target
))
8162 *jump_target
= NULL_TREE
;
8166 if (!RECUR (FOR_INIT_STMT (t
), any
))
8169 if (!RECUR (tmp
, rval
))
8173 if (!processing_template_decl
)
8174 tmp
= cxx_eval_outermost_constant_expr (tmp
, true);
8175 /* If we couldn't evaluate the condition, it might not ever be
8177 if (!integer_onep (tmp
))
8180 if (!RECUR (FOR_EXPR (t
), any
))
8182 if (!RECUR (FOR_BODY (t
), any
))
8184 if (breaks (jump_target
) || continues (jump_target
))
8185 *jump_target
= NULL_TREE
;
8188 case RANGE_FOR_STMT
:
8189 if (!RECUR (RANGE_FOR_INIT_STMT (t
), any
))
8191 if (!RECUR (RANGE_FOR_EXPR (t
), any
))
8193 if (!RECUR (RANGE_FOR_BODY (t
), any
))
8195 if (breaks (jump_target
) || continues (jump_target
))
8196 *jump_target
= NULL_TREE
;
8200 tmp
= WHILE_COND (t
);
8201 if (!RECUR (tmp
, rval
))
8203 if (!processing_template_decl
)
8204 tmp
= cxx_eval_outermost_constant_expr (tmp
, true);
8205 /* If we couldn't evaluate the condition, it might not ever be true. */
8206 if (!integer_onep (tmp
))
8208 if (!RECUR (WHILE_BODY (t
), any
))
8210 if (breaks (jump_target
) || continues (jump_target
))
8211 *jump_target
= NULL_TREE
;
8215 if (!RECUR (SWITCH_STMT_COND (t
), rval
))
8217 /* FIXME we don't check SWITCH_STMT_BODY currently, because even
8218 unreachable labels would be checked and it is enough if there is
8219 a single switch cond value for which it is a valid constant
8220 expression. We need to check if there are any RETURN_EXPRs
8221 or CONTINUE_STMTs inside of the body though, as in that case
8222 we need to set *jump_target. */
8225 hash_set
<tree
> pset
;
8226 check_for_return_continue_data data
= { &pset
, NULL_TREE
};
8228 = cp_walk_tree (&SWITCH_STMT_BODY (t
), check_for_return_continue
,
8230 /* The switch might return. */
8231 *jump_target
= ret_expr
;
8232 else if (data
.continue_stmt
)
8233 /* The switch can't return, but might continue. */
8234 *jump_target
= data
.continue_stmt
;
8239 return RECUR (STMT_EXPR_STMT (t
), rval
);
8242 if (cxx_dialect
>= cxx17
)
8243 /* In C++17 lambdas can be constexpr, don't give up yet. */
8245 else if (flags
& tf_error
)
8246 error_at (loc
, "lambda-expression is not a constant expression "
8250 case DYNAMIC_CAST_EXPR
:
8251 case PSEUDO_DTOR_EXPR
:
8255 case VEC_DELETE_EXPR
:
8261 case OMP_DISTRIBUTE
:
8265 case OMP_TARGET_DATA
:
8274 case OMP_TARGET_UPDATE
:
8275 case OMP_TARGET_ENTER_DATA
:
8276 case OMP_TARGET_EXIT_DATA
:
8278 case OMP_ATOMIC_READ
:
8279 case OMP_ATOMIC_CAPTURE_OLD
:
8280 case OMP_ATOMIC_CAPTURE_NEW
:
8286 case OACC_HOST_DATA
:
8290 case OACC_ENTER_DATA
:
8291 case OACC_EXIT_DATA
:
8293 /* GCC internal stuff. */
8295 case TRANSACTION_EXPR
:
8296 case AT_ENCODE_EXPR
:
8298 if (flags
& tf_error
)
8299 error_at (loc
, "expression %qE is not a constant expression", t
);
8303 if (flags
& tf_error
)
8304 inline_asm_in_constexpr_error (loc
);
8308 if (cxx_dialect
>= cxx20
)
8309 /* In C++20 virtual calls can be constexpr, don't give up yet. */
8311 else if (flags
& tf_error
)
8313 "virtual functions cannot be %<constexpr%> before C++20");
8317 /* In C++20, a typeid expression whose operand is of polymorphic
8318 class type can be constexpr. */
8320 tree e
= TREE_OPERAND (t
, 0);
8321 if (cxx_dialect
< cxx20
8324 && !type_dependent_expression_p (e
)
8325 && TYPE_POLYMORPHIC_P (TREE_TYPE (e
)))
8327 if (flags
& tf_error
)
8328 error_at (loc
, "%<typeid%> is not a constant expression "
8329 "because %qE is of polymorphic type", e
);
8335 case POINTER_DIFF_EXPR
:
8346 case SPACESHIP_EXPR
:
8350 case PREINCREMENT_EXPR
:
8351 case POSTINCREMENT_EXPR
:
8352 case PREDECREMENT_EXPR
:
8353 case POSTDECREMENT_EXPR
:
8354 if (cxx_dialect
< cxx14
)
8360 if (TYPE_P (TREE_OPERAND (t
, 0)))
8366 case FIX_TRUNC_EXPR
:
8371 case TRUTH_NOT_EXPR
:
8372 case FIXED_CONVERT_EXPR
:
8373 case UNARY_PLUS_EXPR
:
8374 case UNARY_LEFT_FOLD_EXPR
:
8375 case UNARY_RIGHT_FOLD_EXPR
:
8377 return RECUR (TREE_OPERAND (t
, 0), rval
);
8380 case CONST_CAST_EXPR
:
8381 case STATIC_CAST_EXPR
:
8382 case REINTERPRET_CAST_EXPR
:
8383 case IMPLICIT_CONV_EXPR
:
8384 if (cxx_dialect
< cxx11
8385 && !dependent_type_p (TREE_TYPE (t
))
8386 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
)))
8387 /* In C++98, a conversion to non-integral type can't be part of a
8388 constant expression. */
8390 if (flags
& tf_error
)
8392 "cast to non-integral type %qT in a constant expression",
8396 /* This might be a conversion from a class to a (potentially) literal
8397 type. Let's consider it potentially constant since the conversion
8398 might be a constexpr user-defined conversion. */
8399 else if (cxx_dialect
>= cxx11
8400 && (dependent_type_p (TREE_TYPE (t
))
8401 || !COMPLETE_TYPE_P (TREE_TYPE (t
))
8402 || literal_type_p (TREE_TYPE (t
)))
8403 && TREE_OPERAND (t
, 0))
8405 tree type
= TREE_TYPE (TREE_OPERAND (t
, 0));
8406 /* If this is a dependent type, it could end up being a class
8407 with conversions. */
8408 if (type
== NULL_TREE
|| WILDCARD_TYPE_P (type
))
8410 /* Or a non-dependent class which has conversions. */
8411 else if (CLASS_TYPE_P (type
)
8412 && (TYPE_HAS_CONVERSION (type
) || dependent_scope_p (type
)))
8416 return (RECUR (TREE_OPERAND (t
, 0),
8417 !TYPE_REF_P (TREE_TYPE (t
))));
8420 return RECUR (BIND_EXPR_BODY (t
), want_rval
);
8422 case CLEANUP_POINT_EXPR
:
8423 case MUST_NOT_THROW_EXPR
:
8424 case TRY_CATCH_EXPR
:
8429 case NON_DEPENDENT_EXPR
:
8430 /* For convenience. */
8433 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
8436 tmp
= DECL_EXPR_DECL (t
);
8437 if (VAR_P (tmp
) && !DECL_ARTIFICIAL (tmp
))
8439 if (TREE_STATIC (tmp
))
8441 if (flags
& tf_error
)
8442 error_at (DECL_SOURCE_LOCATION (tmp
), "%qD declared "
8443 "%<static%> in %<constexpr%> context", tmp
);
8446 else if (CP_DECL_THREAD_LOCAL_P (tmp
))
8448 if (flags
& tf_error
)
8449 error_at (DECL_SOURCE_LOCATION (tmp
), "%qD declared "
8450 "%<thread_local%> in %<constexpr%> context", tmp
);
8453 else if (!check_for_uninitialized_const_var
8454 (tmp
, /*constexpr_context_p=*/true, flags
))
8457 return RECUR (tmp
, want_rval
);
8459 case TRY_FINALLY_EXPR
:
8460 return (RECUR (TREE_OPERAND (t
, 0), want_rval
)
8461 && RECUR (TREE_OPERAND (t
, 1), any
));
8464 return RECUR (TREE_OPERAND (t
, 1), want_rval
);
8467 if (!TARGET_EXPR_DIRECT_INIT_P (t
)
8468 && !literal_type_p (TREE_TYPE (t
)))
8470 if (flags
& tf_error
)
8472 auto_diagnostic_group d
;
8473 error_at (loc
, "temporary of non-literal type %qT in a "
8474 "constant expression", TREE_TYPE (t
));
8475 explain_non_literal_class (TREE_TYPE (t
));
8481 return RECUR (TREE_OPERAND (t
, 1), rval
);
8485 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (t
);
8486 constructor_elt
*ce
;
8487 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
8488 if (!RECUR (ce
->value
, want_rval
))
8495 gcc_assert (TREE_PURPOSE (t
) == NULL_TREE
8496 || DECL_P (TREE_PURPOSE (t
)));
8497 if (!RECUR (TREE_VALUE (t
), want_rval
))
8499 if (TREE_CHAIN (t
) == NULL_TREE
)
8501 return RECUR (TREE_CHAIN (t
), want_rval
);
8504 case TRUNC_DIV_EXPR
:
8506 case FLOOR_DIV_EXPR
:
8507 case ROUND_DIV_EXPR
:
8508 case TRUNC_MOD_EXPR
:
8510 case ROUND_MOD_EXPR
:
8512 tree denom
= TREE_OPERAND (t
, 1);
8513 if (!RECUR (denom
, rval
))
8515 /* We can't call cxx_eval_outermost_constant_expr on an expression
8516 that hasn't been through instantiate_non_dependent_expr yet. */
8517 if (!processing_template_decl
)
8518 denom
= cxx_eval_outermost_constant_expr (denom
, true);
8519 if (integer_zerop (denom
))
8521 if (flags
& tf_error
)
8522 error ("division by zero is not a constant expression");
8528 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
8534 /* check_return_expr sometimes wraps a TARGET_EXPR in a
8535 COMPOUND_EXPR; don't get confused. */
8536 tree op0
= TREE_OPERAND (t
, 0);
8537 tree op1
= TREE_OPERAND (t
, 1);
8539 if (TREE_CODE (op0
) == TARGET_EXPR
&& op1
== TARGET_EXPR_SLOT (op0
))
8540 return RECUR (op0
, want_rval
);
8545 /* If the first operand is the non-short-circuit constant, look at
8546 the second operand; otherwise we only care about the first one for
8548 case TRUTH_AND_EXPR
:
8549 case TRUTH_ANDIF_EXPR
:
8550 tmp
= boolean_true_node
;
8553 case TRUTH_ORIF_EXPR
:
8554 tmp
= boolean_false_node
;
8557 tree op
= TREE_OPERAND (t
, 0);
8558 if (!RECUR (op
, rval
))
8560 if (!processing_template_decl
)
8561 op
= cxx_eval_outermost_constant_expr (op
, true);
8562 if (tree_int_cst_equal (op
, tmp
))
8563 return RECUR (TREE_OPERAND (t
, 1), rval
);
8570 case POINTER_PLUS_EXPR
:
8572 case EXACT_DIV_EXPR
:
8582 case TRUTH_XOR_EXPR
:
8583 case UNORDERED_EXPR
:
8596 case ARRAY_RANGE_REF
:
8600 case BINARY_LEFT_FOLD_EXPR
:
8601 case BINARY_RIGHT_FOLD_EXPR
:
8603 for (i
= 0; i
< 2; ++i
)
8604 if (!RECUR (TREE_OPERAND (t
, i
), want_rval
))
8609 for (i
= 0; i
< 3; ++i
)
8610 if (!RECUR (TREE_OPERAND (t
, i
), true))
8615 if (COND_EXPR_IS_VEC_DELETE (t
) && cxx_dialect
< cxx20
)
8617 if (flags
& tf_error
)
8618 error_at (loc
, "%<delete[]%> is not a constant expression");
8624 /* If the condition is a known constant, we know which of the legs we
8625 care about; otherwise we only require that the condition and
8626 either of the legs be potentially constant. */
8627 tmp
= TREE_OPERAND (t
, 0);
8628 if (!RECUR (tmp
, rval
))
8630 if (!processing_template_decl
)
8631 tmp
= cxx_eval_outermost_constant_expr (tmp
, true);
8632 if (integer_zerop (tmp
))
8633 return RECUR (TREE_OPERAND (t
, 2), want_rval
);
8634 else if (TREE_CODE (tmp
) == INTEGER_CST
)
8635 return RECUR (TREE_OPERAND (t
, 1), want_rval
);
8636 for (i
= 1; i
< 3; ++i
)
8637 if (potential_constant_expression_1 (TREE_OPERAND (t
, i
),
8638 want_rval
, strict
, now
,
8639 tf_none
, jump_target
))
8641 if (flags
& tf_error
)
8642 error_at (loc
, "expression %qE is not a constant expression", t
);
8646 if (VEC_INIT_EXPR_IS_CONSTEXPR (t
))
8648 if (flags
& tf_error
)
8650 error_at (loc
, "non-constant array initialization");
8651 diagnose_non_constexpr_vec_init (t
);
8657 /* We can see these in statement-expressions. */
8661 if (!RECUR (CLEANUP_BODY (t
), any
))
8663 if (!CLEANUP_EH_ONLY (t
) && !RECUR (CLEANUP_EXPR (t
), any
))
8667 case EMPTY_CLASS_EXPR
:
8672 tree
*target
= &TREE_OPERAND (t
, 0);
8673 /* Gotos representing break and continue are OK. */
8674 if (breaks (target
) || continues (target
))
8676 *jump_target
= *target
;
8679 if (flags
& tf_error
)
8680 error_at (loc
, "%<goto%> is not a constant expression");
8685 t
= LABEL_EXPR_LABEL (t
);
8686 if (DECL_ARTIFICIAL (t
))
8688 else if (flags
& tf_error
)
8689 error_at (loc
, "label definition is not a constant expression");
8693 return RECUR (TREE_OPERAND (t
, 0), rval
);
8696 return RECUR (TREE_OPERAND (t
, 0), rval
);
8698 /* Coroutine await, yield and return expressions are not. */
8701 case CO_RETURN_EXPR
:
8705 if (objc_non_constant_expr_p (t
))
8708 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t
)));
8716 potential_constant_expression_1 (tree t
, bool want_rval
, bool strict
, bool now
,
8717 tsubst_flags_t flags
)
8719 tree target
= NULL_TREE
;
8720 return potential_constant_expression_1 (t
, want_rval
, strict
, now
,
8724 /* The main entry point to the above. */
8727 potential_constant_expression (tree t
)
8729 return potential_constant_expression_1 (t
, false, true, false, tf_none
);
8732 /* As above, but require a constant rvalue. */
8735 potential_rvalue_constant_expression (tree t
)
8737 return potential_constant_expression_1 (t
, true, true, false, tf_none
);
8740 /* Like above, but complain about non-constant expressions. */
8743 require_potential_constant_expression (tree t
)
8745 return potential_constant_expression_1 (t
, false, true, false,
8746 tf_warning_or_error
);
8749 /* Cross product of the above. */
8752 require_potential_rvalue_constant_expression (tree t
)
8754 return potential_constant_expression_1 (t
, true, true, false,
8755 tf_warning_or_error
);
8758 /* Like above, but don't consider PARM_DECL a potential_constant_expression. */
8761 require_rvalue_constant_expression (tree t
)
8763 return potential_constant_expression_1 (t
, true, true, true,
8764 tf_warning_or_error
);
8767 /* Like potential_constant_expression, but don't consider possible constexpr
8768 substitution of the current function. That is, PARM_DECL qualifies under
8769 potential_constant_expression, but not here.
8771 This is basically what you can check when any actual constant values might
8772 be value-dependent. */
8775 is_constant_expression (tree t
)
8777 return potential_constant_expression_1 (t
, false, true, true, tf_none
);
8780 /* As above, but expect an rvalue. */
8783 is_rvalue_constant_expression (tree t
)
8785 return potential_constant_expression_1 (t
, true, true, true, tf_none
);
8788 /* Like above, but complain about non-constant expressions. */
8791 require_constant_expression (tree t
)
8793 return potential_constant_expression_1 (t
, false, true, true,
8794 tf_warning_or_error
);
8797 /* Like is_constant_expression, but allow const variables that are not allowed
8798 under constexpr rules. */
8801 is_static_init_expression (tree t
)
8803 return potential_constant_expression_1 (t
, false, false, true, tf_none
);
8806 /* Returns true if T is a potential constant expression that is not
8807 instantiation-dependent, and therefore a candidate for constant folding even
8811 is_nondependent_constant_expression (tree t
)
8813 return (!type_unknown_p (t
)
8814 && is_constant_expression (t
)
8815 && !instantiation_dependent_expression_p (t
));
8818 /* Returns true if T is a potential static initializer expression that is not
8819 instantiation-dependent. */
8822 is_nondependent_static_init_expression (tree t
)
8824 return (!type_unknown_p (t
)
8825 && is_static_init_expression (t
)
8826 && !instantiation_dependent_expression_p (t
));
8829 /* Finalize constexpr processing after parsing. */
8832 fini_constexpr (void)
8834 /* The contexpr call and fundef copies tables are no longer needed. */
8835 constexpr_call_table
= NULL
;
8836 fundef_copies_table
= NULL
;
8839 #include "gt-cp-constexpr.h"