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 /* Normally we would strip a TARGET_EXPR in an initialization context
1595 such as this, but here we do the elision differently: we keep the
1596 TARGET_EXPR, and use its CONSTRUCTOR as the value of the parm. */
1597 arg
= cxx_eval_constant_expression (ctx
, x
, /*lval=*/false,
1598 non_constant_p
, overflow_p
);
1599 /* Don't VERIFY_CONSTANT here. */
1600 if (*non_constant_p
&& ctx
->quiet
)
1602 /* Just discard ellipsis args after checking their constantitude. */
1606 if (!*non_constant_p
)
1608 /* Make sure the binding has the same type as the parm. But
1609 only for constant args. */
1610 if (!TYPE_REF_P (type
))
1611 arg
= adjust_temp_type (type
, arg
);
1612 if (!TREE_CONSTANT (arg
))
1613 *non_constant_args
= true;
1614 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
1615 /* The destructor needs to see any modifications the callee makes
1617 *non_constant_args
= true;
1619 /* For virtual calls, adjust the this argument, so that it is
1620 the object on which the method is called, rather than
1621 one of its bases. */
1622 if (i
== 0 && DECL_VIRTUAL_P (fun
))
1626 if (TREE_CODE (addr
) == ADDR_EXPR
)
1628 tree obj
= TREE_OPERAND (addr
, 0);
1629 while (TREE_CODE (obj
) == COMPONENT_REF
1630 && DECL_FIELD_IS_BASE (TREE_OPERAND (obj
, 1))
1631 && !same_type_ignoring_top_level_qualifiers_p
1632 (TREE_TYPE (obj
), DECL_CONTEXT (fun
)))
1633 obj
= TREE_OPERAND (obj
, 0);
1634 if (obj
!= TREE_OPERAND (addr
, 0))
1635 arg
= build_fold_addr_expr_with_type (obj
,
1639 TREE_VEC_ELT (binds
, i
) = arg
;
1641 parms
= TREE_CHAIN (parms
);
1645 /* Variables and functions to manage constexpr call expansion context.
1646 These do not need to be marked for PCH or GC. */
1648 /* FIXME remember and print actual constant arguments. */
1649 static vec
<tree
> call_stack
;
1650 static int call_stack_tick
;
1651 static int last_cx_error_tick
;
1654 push_cx_call_context (tree call
)
1657 if (!EXPR_HAS_LOCATION (call
))
1658 SET_EXPR_LOCATION (call
, input_location
);
1659 call_stack
.safe_push (call
);
1660 int len
= call_stack
.length ();
1661 if (len
> max_constexpr_depth
)
1667 pop_cx_call_context (void)
1674 cx_error_context (void)
1676 vec
<tree
> r
= vNULL
;
1677 if (call_stack_tick
!= last_cx_error_tick
1678 && !call_stack
.is_empty ())
1680 last_cx_error_tick
= call_stack_tick
;
1684 /* Evaluate a call T to a GCC internal function when possible and return
1685 the evaluated result or, under the control of CTX, give an error, set
1686 NON_CONSTANT_P, and return the unevaluated call T otherwise. */
1689 cxx_eval_internal_function (const constexpr_ctx
*ctx
, tree t
,
1691 bool *non_constant_p
, bool *overflow_p
)
1693 enum tree_code opcode
= ERROR_MARK
;
1695 switch (CALL_EXPR_IFN (t
))
1697 case IFN_UBSAN_NULL
:
1698 case IFN_UBSAN_BOUNDS
:
1699 case IFN_UBSAN_VPTR
:
1700 case IFN_FALLTHROUGH
:
1703 case IFN_ADD_OVERFLOW
:
1706 case IFN_SUB_OVERFLOW
:
1707 opcode
= MINUS_EXPR
;
1709 case IFN_MUL_OVERFLOW
:
1714 return cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 0),
1715 false, non_constant_p
, overflow_p
);
1717 case IFN_VEC_CONVERT
:
1719 tree arg
= cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 0),
1720 false, non_constant_p
,
1722 if (TREE_CODE (arg
) == VECTOR_CST
)
1723 return fold_const_call (CFN_VEC_CONVERT
, TREE_TYPE (t
), arg
);
1726 *non_constant_p
= true;
1733 error_at (cp_expr_loc_or_input_loc (t
),
1734 "call to internal function %qE", t
);
1735 *non_constant_p
= true;
1739 /* Evaluate constant arguments using OPCODE and return a complex
1740 number containing the result and the overflow bit. */
1741 tree arg0
= cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 0), lval
,
1742 non_constant_p
, overflow_p
);
1743 tree arg1
= cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 1), lval
,
1744 non_constant_p
, overflow_p
);
1746 if (TREE_CODE (arg0
) == INTEGER_CST
&& TREE_CODE (arg1
) == INTEGER_CST
)
1748 location_t loc
= cp_expr_loc_or_input_loc (t
);
1749 tree type
= TREE_TYPE (TREE_TYPE (t
));
1750 tree result
= fold_binary_loc (loc
, opcode
, type
,
1751 fold_convert_loc (loc
, type
, arg0
),
1752 fold_convert_loc (loc
, type
, arg1
));
1754 = build_int_cst (type
, arith_overflowed_p (opcode
, type
, arg0
, arg1
));
1755 /* Reset TREE_OVERFLOW to avoid warnings for the overflow. */
1756 if (TREE_OVERFLOW (result
))
1757 TREE_OVERFLOW (result
) = 0;
1759 return build_complex (TREE_TYPE (t
), result
, ovf
);
1762 *non_constant_p
= true;
1766 /* Clean CONSTRUCTOR_NO_CLEARING from CTOR and its sub-aggregates. */
1769 clear_no_implicit_zero (tree ctor
)
1771 if (CONSTRUCTOR_NO_CLEARING (ctor
))
1773 CONSTRUCTOR_NO_CLEARING (ctor
) = false;
1774 tree elt
; unsigned HOST_WIDE_INT idx
;
1775 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor
), idx
, elt
)
1776 if (TREE_CODE (elt
) == CONSTRUCTOR
)
1777 clear_no_implicit_zero (elt
);
1781 /* Complain about a const object OBJ being modified in a constant expression.
1782 EXPR is the MODIFY_EXPR expression performing the modification. */
1785 modifying_const_object_error (tree expr
, tree obj
)
1787 location_t loc
= cp_expr_loc_or_input_loc (expr
);
1788 auto_diagnostic_group d
;
1789 error_at (loc
, "modifying a const object %qE is not allowed in "
1790 "a constant expression", TREE_OPERAND (expr
, 0));
1791 inform (location_of (obj
), "originally declared %<const%> here");
1794 /* Return true if FNDECL is a replaceable global allocation function that
1795 should be useable during constant expression evaluation. */
1798 cxx_replaceable_global_alloc_fn (tree fndecl
)
1800 return (cxx_dialect
>= cxx20
1801 && IDENTIFIER_NEWDEL_OP_P (DECL_NAME (fndecl
))
1802 && CP_DECL_CONTEXT (fndecl
) == global_namespace
1803 && (DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl
)
1804 || DECL_IS_OPERATOR_DELETE_P (fndecl
)));
1807 /* Return true if FNDECL is a placement new function that should be
1808 useable during constant expression evaluation of std::construct_at. */
1811 cxx_placement_new_fn (tree fndecl
)
1813 if (cxx_dialect
>= cxx20
1814 && IDENTIFIER_NEW_OP_P (DECL_NAME (fndecl
))
1815 && CP_DECL_CONTEXT (fndecl
) == global_namespace
1816 && !DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl
)
1817 && TREE_CODE (TREE_TYPE (fndecl
)) == FUNCTION_TYPE
)
1819 tree first_arg
= TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
1820 if (TREE_VALUE (first_arg
) == ptr_type_node
1821 && TREE_CHAIN (first_arg
) == void_list_node
)
1827 /* Return true if FNDECL is std::construct_at. */
1830 is_std_construct_at (tree fndecl
)
1832 if (!decl_in_std_namespace_p (fndecl
))
1835 tree name
= DECL_NAME (fndecl
);
1836 return name
&& id_equal (name
, "construct_at");
1839 /* Return true if FNDECL is std::allocator<T>::{,de}allocate. */
1842 is_std_allocator_allocate (tree fndecl
)
1844 tree name
= DECL_NAME (fndecl
);
1845 if (name
== NULL_TREE
1846 || !(id_equal (name
, "allocate") || id_equal (name
, "deallocate")))
1849 tree ctx
= DECL_CONTEXT (fndecl
);
1850 if (ctx
== NULL_TREE
|| !CLASS_TYPE_P (ctx
) || !TYPE_MAIN_DECL (ctx
))
1853 tree decl
= TYPE_MAIN_DECL (ctx
);
1854 name
= DECL_NAME (decl
);
1855 if (name
== NULL_TREE
|| !id_equal (name
, "allocator"))
1858 return decl_in_std_namespace_p (decl
);
1861 /* Return true if FNDECL is __dynamic_cast. */
1864 cxx_dynamic_cast_fn_p (tree fndecl
)
1866 return (cxx_dialect
>= cxx20
1867 && id_equal (DECL_NAME (fndecl
), "__dynamic_cast")
1868 && CP_DECL_CONTEXT (fndecl
) == global_namespace
);
1871 /* Often, we have an expression in the form of address + offset, e.g.
1872 "&_ZTV1A + 16". Extract the object from it, i.e. "_ZTV1A". */
1875 extract_obj_from_addr_offset (tree expr
)
1877 if (TREE_CODE (expr
) == POINTER_PLUS_EXPR
)
1878 expr
= TREE_OPERAND (expr
, 0);
1880 if (TREE_CODE (expr
) == ADDR_EXPR
)
1881 expr
= TREE_OPERAND (expr
, 0);
1885 /* Given a PATH like
1887 g.D.2181.D.2154.D.2102.D.2093
1889 find a component with type TYPE. Return NULL_TREE if not found, and
1890 error_mark_node if the component is not accessible. If STOP is non-null,
1891 this function will return NULL_TREE if STOP is found before TYPE. */
1894 get_component_with_type (tree path
, tree type
, tree stop
)
1898 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path
), type
))
1902 && (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path
),
1905 else if (TREE_CODE (path
) == COMPONENT_REF
1906 && DECL_FIELD_IS_BASE (TREE_OPERAND (path
, 1)))
1908 /* We need to check that the component we're accessing is in fact
1910 if (TREE_PRIVATE (TREE_OPERAND (path
, 1))
1911 || TREE_PROTECTED (TREE_OPERAND (path
, 1)))
1912 return error_mark_node
;
1913 path
= TREE_OPERAND (path
, 0);
1920 /* Evaluate a call to __dynamic_cast (permitted by P1327R1).
1922 The declaration of __dynamic_cast is:
1924 void* __dynamic_cast (const void* __src_ptr,
1925 const __class_type_info* __src_type,
1926 const __class_type_info* __dst_type,
1927 ptrdiff_t __src2dst);
1929 where src2dst has the following possible values
1931 >-1: src_type is a unique public non-virtual base of dst_type
1932 dst_ptr + src2dst == src_ptr
1933 -1: unspecified relationship
1934 -2: src_type is not a public base of dst_type
1935 -3: src_type is a multiple public non-virtual base of dst_type
1937 Since literal types can't have virtual bases, we only expect hint >=0,
1941 cxx_eval_dynamic_cast_fn (const constexpr_ctx
*ctx
, tree call
,
1942 bool *non_constant_p
, bool *overflow_p
)
1944 /* T will be something like
1945 __dynamic_cast ((B*) b, &_ZTI1B, &_ZTI1D, 8)
1947 gcc_assert (call_expr_nargs (call
) == 4);
1948 tsubst_flags_t complain
= ctx
->quiet
? tf_none
: tf_warning_or_error
;
1949 tree obj
= CALL_EXPR_ARG (call
, 0);
1950 tree type
= CALL_EXPR_ARG (call
, 2);
1951 HOST_WIDE_INT hint
= int_cst_value (CALL_EXPR_ARG (call
, 3));
1952 location_t loc
= cp_expr_loc_or_input_loc (call
);
1954 /* Get the target type of the dynamic_cast. */
1955 gcc_assert (TREE_CODE (type
) == ADDR_EXPR
);
1956 type
= TREE_OPERAND (type
, 0);
1957 type
= TREE_TYPE (DECL_NAME (type
));
1959 /* TYPE can only be either T* or T&. We can't know which of these it
1960 is by looking at TYPE, but OBJ will be "(T*) x" in the first case,
1961 and something like "(T*)(T&)(T*) x" in the second case. */
1962 bool reference_p
= false;
1963 while (CONVERT_EXPR_P (obj
) || TREE_CODE (obj
) == SAVE_EXPR
)
1965 reference_p
|= TYPE_REF_P (TREE_TYPE (obj
));
1966 obj
= TREE_OPERAND (obj
, 0);
1969 /* Evaluate the object so that we know its dynamic type. */
1970 obj
= cxx_eval_constant_expression (ctx
, obj
, /*lval*/false, non_constant_p
,
1972 if (*non_constant_p
)
1975 /* We expect OBJ to be in form of &d.D.2102 when HINT == 0,
1976 but when HINT is > 0, it can also be something like
1977 &d.D.2102 + 18446744073709551608, which includes the BINFO_OFFSET. */
1978 obj
= extract_obj_from_addr_offset (obj
);
1979 const tree objtype
= TREE_TYPE (obj
);
1980 /* If OBJ doesn't refer to a base field, we're done. */
1981 if (tree t
= (TREE_CODE (obj
) == COMPONENT_REF
1982 ? TREE_OPERAND (obj
, 1) : obj
))
1983 if (TREE_CODE (t
) != FIELD_DECL
|| !DECL_FIELD_IS_BASE (t
))
1989 error_at (loc
, "reference %<dynamic_cast%> failed");
1990 inform (loc
, "dynamic type %qT of its operand does "
1991 "not have a base class of type %qT",
1994 *non_constant_p
= true;
1996 return integer_zero_node
;
1999 /* [class.cdtor] When a dynamic_cast is used in a constructor ...
2000 or in a destructor ... if the operand of the dynamic_cast refers
2001 to the object under construction or destruction, this object is
2002 considered to be a most derived object that has the type of the
2003 constructor or destructor's class. */
2004 tree vtable
= build_vfield_ref (obj
, objtype
);
2005 vtable
= cxx_eval_constant_expression (ctx
, vtable
, /*lval*/false,
2006 non_constant_p
, overflow_p
);
2007 if (*non_constant_p
)
2009 /* With -fsanitize=vptr, we initialize all vtable pointers to null,
2010 so it's possible that we got a null pointer now. */
2011 if (integer_zerop (vtable
))
2014 error_at (loc
, "virtual table pointer is used uninitialized");
2015 *non_constant_p
= true;
2016 return integer_zero_node
;
2018 /* VTABLE will be &_ZTV1A + 16 or similar, get _ZTV1A. */
2019 vtable
= extract_obj_from_addr_offset (vtable
);
2020 const tree mdtype
= DECL_CONTEXT (vtable
);
2022 /* Given dynamic_cast<T>(v),
2024 [expr.dynamic.cast] If C is the class type to which T points or refers,
2025 the runtime check logically executes as follows:
2027 If, in the most derived object pointed (referred) to by v, v points
2028 (refers) to a public base class subobject of a C object, and if only
2029 one object of type C is derived from the subobject pointed (referred)
2030 to by v the result points (refers) to that C object.
2032 In this case, HINT >= 0 or -3. */
2033 if (hint
>= 0 || hint
== -3)
2035 /* Look for a component with type TYPE. */
2036 tree t
= get_component_with_type (obj
, type
, mdtype
);
2037 /* If not accessible, give an error. */
2038 if (t
== error_mark_node
)
2044 error_at (loc
, "reference %<dynamic_cast%> failed");
2045 inform (loc
, "static type %qT of its operand is a "
2046 "non-public base class of dynamic type %qT",
2050 *non_constant_p
= true;
2052 return integer_zero_node
;
2055 /* The result points to the TYPE object. */
2056 return cp_build_addr_expr (t
, complain
);
2057 /* Else, TYPE was not found, because the HINT turned out to be wrong.
2058 Fall through to the normal processing. */
2061 /* Otherwise, if v points (refers) to a public base class subobject of the
2062 most derived object, and the type of the most derived object has a base
2063 class, of type C, that is unambiguous and public, the result points
2064 (refers) to the C subobject of the most derived object.
2066 But it can also be an invalid case. */
2068 /* Get the most derived object. */
2069 obj
= get_component_with_type (obj
, mdtype
, NULL_TREE
);
2070 if (obj
== error_mark_node
)
2076 error_at (loc
, "reference %<dynamic_cast%> failed");
2077 inform (loc
, "static type %qT of its operand is a non-public"
2078 " base class of dynamic type %qT", objtype
, mdtype
);
2080 *non_constant_p
= true;
2082 return integer_zero_node
;
2087 /* Check that the type of the most derived object has a base class
2088 of type TYPE that is unambiguous and public. */
2090 tree binfo
= lookup_base (mdtype
, type
, ba_check
, &b_kind
, tf_none
);
2091 if (!binfo
|| binfo
== error_mark_node
)
2097 error_at (loc
, "reference %<dynamic_cast%> failed");
2098 if (b_kind
== bk_ambig
)
2099 inform (loc
, "%qT is an ambiguous base class of dynamic "
2100 "type %qT of its operand", type
, mdtype
);
2102 inform (loc
, "dynamic type %qT of its operand does not "
2103 "have an unambiguous public base class %qT",
2106 *non_constant_p
= true;
2108 return integer_zero_node
;
2110 /* If so, return the TYPE subobject of the most derived object. */
2111 obj
= convert_to_base_statically (obj
, binfo
);
2112 return cp_build_addr_expr (obj
, complain
);
2115 /* Data structure used by replace_result_decl and replace_result_decl_r. */
2117 struct replace_result_decl_data
2119 /* The RESULT_DECL we want to replace. */
2121 /* The replacement for DECL. */
2123 /* Whether we've performed any replacements. */
2127 /* Helper function for replace_result_decl, called through cp_walk_tree. */
2130 replace_result_decl_r (tree
*tp
, int *walk_subtrees
, void *data
)
2132 replace_result_decl_data
*d
= (replace_result_decl_data
*) data
;
2136 *tp
= unshare_expr (d
->replacement
);
2140 else if (TYPE_P (*tp
))
2146 /* Replace every occurrence of DECL, a RESULT_DECL, with (an unshared copy of)
2147 REPLACEMENT within the reduced constant expression *TP. Returns true iff a
2148 replacement was performed. */
2151 replace_result_decl (tree
*tp
, tree decl
, tree replacement
)
2153 gcc_checking_assert (TREE_CODE (decl
) == RESULT_DECL
2154 && (same_type_ignoring_top_level_qualifiers_p
2155 (TREE_TYPE (decl
), TREE_TYPE (replacement
))));
2156 replace_result_decl_data data
= { decl
, replacement
, false };
2157 cp_walk_tree_without_duplicates (tp
, replace_result_decl_r
, &data
);
2158 return data
.changed
;
2161 /* Evaluate the call T to virtual function thunk THUNK_FNDECL. */
2164 cxx_eval_thunk_call (const constexpr_ctx
*ctx
, tree t
, tree thunk_fndecl
,
2166 bool *non_constant_p
, bool *overflow_p
)
2168 tree function
= THUNK_TARGET (thunk_fndecl
);
2170 /* virtual_offset is only set in the presence of virtual bases, which make
2171 the class non-literal, so we don't need to handle it here. */
2172 if (THUNK_VIRTUAL_OFFSET (thunk_fndecl
))
2174 gcc_assert (!DECL_DECLARED_CONSTEXPR_P (function
));
2177 error ("call to non-%<constexpr%> function %qD", function
);
2178 explain_invalid_constexpr_fn (function
);
2180 *non_constant_p
= true;
2184 tree new_call
= copy_node (t
);
2185 CALL_EXPR_FN (new_call
) = function
;
2186 TREE_TYPE (new_call
) = TREE_TYPE (TREE_TYPE (function
));
2188 tree offset
= size_int (THUNK_FIXED_OFFSET (thunk_fndecl
));
2190 if (DECL_THIS_THUNK_P (thunk_fndecl
))
2192 /* 'this'-adjusting thunk. */
2193 tree this_arg
= CALL_EXPR_ARG (t
, 0);
2194 this_arg
= build2 (POINTER_PLUS_EXPR
, TREE_TYPE (this_arg
),
2196 CALL_EXPR_ARG (new_call
, 0) = this_arg
;
2199 /* Return-adjusting thunk. */
2200 new_call
= build2 (POINTER_PLUS_EXPR
, TREE_TYPE (new_call
),
2203 return cxx_eval_constant_expression (ctx
, new_call
, lval
,
2204 non_constant_p
, overflow_p
);
2207 /* If OBJECT is of const class type, evaluate it to a CONSTRUCTOR and set
2208 its TREE_READONLY flag according to READONLY_P. Used for constexpr
2209 'tors to detect modifying const objects in a constexpr context. */
2212 cxx_set_object_constness (const constexpr_ctx
*ctx
, tree object
,
2213 bool readonly_p
, bool *non_constant_p
,
2216 if (CLASS_TYPE_P (TREE_TYPE (object
))
2217 && CP_TYPE_CONST_P (TREE_TYPE (object
)))
2219 /* Subobjects might not be stored in ctx->global->values but we
2220 can get its CONSTRUCTOR by evaluating *this. */
2221 tree e
= cxx_eval_constant_expression (ctx
, object
, /*lval*/false,
2222 non_constant_p
, overflow_p
);
2223 if (TREE_CODE (e
) == CONSTRUCTOR
&& !*non_constant_p
)
2224 TREE_READONLY (e
) = readonly_p
;
2228 /* Subroutine of cxx_eval_constant_expression.
2229 Evaluate the call expression tree T in the context of OLD_CALL expression
2233 cxx_eval_call_expression (const constexpr_ctx
*ctx
, tree t
,
2235 bool *non_constant_p
, bool *overflow_p
)
2237 /* Handle concept checks separately. */
2238 if (concept_check_p (t
))
2239 return evaluate_concept_check (t
, tf_warning_or_error
);
2241 location_t loc
= cp_expr_loc_or_input_loc (t
);
2242 tree fun
= get_function_named_in_call (t
);
2243 constexpr_call new_call
2244 = { NULL
, NULL
, NULL
, 0, ctx
->manifestly_const_eval
};
2247 if (fun
== NULL_TREE
)
2248 return cxx_eval_internal_function (ctx
, t
, lval
,
2249 non_constant_p
, overflow_p
);
2251 if (TREE_CODE (fun
) != FUNCTION_DECL
)
2253 /* Might be a constexpr function pointer. */
2254 fun
= cxx_eval_constant_expression (ctx
, fun
,
2255 /*lval*/false, non_constant_p
,
2258 if (TREE_CODE (fun
) == ADDR_EXPR
)
2259 fun
= TREE_OPERAND (fun
, 0);
2260 /* For TARGET_VTABLE_USES_DESCRIPTORS targets, there is no
2261 indirection, the called expression is a pointer into the
2262 virtual table which should contain FDESC_EXPR. Extract the
2263 FUNCTION_DECL from there. */
2264 else if (TARGET_VTABLE_USES_DESCRIPTORS
2265 && TREE_CODE (fun
) == POINTER_PLUS_EXPR
2266 && TREE_CODE (TREE_OPERAND (fun
, 0)) == ADDR_EXPR
2267 && TREE_CODE (TREE_OPERAND (fun
, 1)) == INTEGER_CST
)
2269 tree d
= TREE_OPERAND (TREE_OPERAND (fun
, 0), 0);
2271 && DECL_VTABLE_OR_VTT_P (d
)
2272 && TREE_CODE (TREE_TYPE (d
)) == ARRAY_TYPE
2273 && TREE_TYPE (TREE_TYPE (d
)) == vtable_entry_type
2275 && TREE_CODE (DECL_INITIAL (d
)) == CONSTRUCTOR
)
2277 tree i
= int_const_binop (TRUNC_DIV_EXPR
, TREE_OPERAND (fun
, 1),
2278 TYPE_SIZE_UNIT (vtable_entry_type
));
2279 HOST_WIDE_INT idx
= find_array_ctor_elt (DECL_INITIAL (d
), i
);
2283 = (*CONSTRUCTOR_ELTS (DECL_INITIAL (d
)))[idx
].value
;
2284 if (TREE_CODE (fdesc
) == FDESC_EXPR
2285 && integer_zerop (TREE_OPERAND (fdesc
, 1)))
2286 fun
= TREE_OPERAND (fdesc
, 0);
2291 if (TREE_CODE (fun
) != FUNCTION_DECL
)
2293 if (!ctx
->quiet
&& !*non_constant_p
)
2294 error_at (loc
, "expression %qE does not designate a %<constexpr%> "
2296 *non_constant_p
= true;
2299 if (DECL_CLONED_FUNCTION_P (fun
))
2300 fun
= DECL_CLONED_FUNCTION (fun
);
2302 if (is_ubsan_builtin_p (fun
))
2305 if (fndecl_built_in_p (fun
))
2306 return cxx_eval_builtin_function_call (ctx
, t
, fun
,
2307 lval
, non_constant_p
, overflow_p
);
2308 if (DECL_THUNK_P (fun
))
2309 return cxx_eval_thunk_call (ctx
, t
, fun
, lval
, non_constant_p
, overflow_p
);
2310 if (!DECL_DECLARED_CONSTEXPR_P (fun
))
2312 if (TREE_CODE (t
) == CALL_EXPR
2313 && cxx_replaceable_global_alloc_fn (fun
)
2314 && (CALL_FROM_NEW_OR_DELETE_P (t
)
2316 && ctx
->call
->fundef
2317 && is_std_allocator_allocate (ctx
->call
->fundef
->decl
))))
2319 const int nargs
= call_expr_nargs (t
);
2320 tree arg0
= NULL_TREE
;
2321 for (int i
= 0; i
< nargs
; ++i
)
2323 tree arg
= CALL_EXPR_ARG (t
, i
);
2324 arg
= cxx_eval_constant_expression (ctx
, arg
, false,
2325 non_constant_p
, overflow_p
);
2326 VERIFY_CONSTANT (arg
);
2331 if (IDENTIFIER_NEW_OP_P (DECL_NAME (fun
)))
2333 tree type
= build_array_type_nelts (char_type_node
,
2334 tree_to_uhwi (arg0
));
2335 tree var
= build_decl (loc
, VAR_DECL
,
2336 (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun
))
2338 ? heap_vec_uninit_identifier
2339 : heap_uninit_identifier
,
2341 DECL_ARTIFICIAL (var
) = 1;
2342 TREE_STATIC (var
) = 1;
2343 ctx
->global
->heap_vars
.safe_push (var
);
2344 ctx
->global
->values
.put (var
, NULL_TREE
);
2345 return fold_convert (ptr_type_node
, build_address (var
));
2350 if (TREE_CODE (arg0
) == ADDR_EXPR
2351 && VAR_P (TREE_OPERAND (arg0
, 0)))
2353 tree var
= TREE_OPERAND (arg0
, 0);
2354 if (DECL_NAME (var
) == heap_uninit_identifier
2355 || DECL_NAME (var
) == heap_identifier
)
2357 if (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun
))
2362 error_at (loc
, "array deallocation of object "
2363 "allocated with non-array "
2365 inform (DECL_SOURCE_LOCATION (var
),
2366 "allocation performed here");
2368 *non_constant_p
= true;
2371 DECL_NAME (var
) = heap_deleted_identifier
;
2372 ctx
->global
->values
.remove (var
);
2373 ctx
->global
->heap_dealloc_count
++;
2376 else if (DECL_NAME (var
) == heap_vec_uninit_identifier
2377 || DECL_NAME (var
) == heap_vec_identifier
)
2379 if ((IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun
))
2380 & OVL_OP_FLAG_VEC
) == 0)
2384 error_at (loc
, "non-array deallocation of "
2385 "object allocated with array "
2387 inform (DECL_SOURCE_LOCATION (var
),
2388 "allocation performed here");
2390 *non_constant_p
= true;
2393 DECL_NAME (var
) = heap_deleted_identifier
;
2394 ctx
->global
->values
.remove (var
);
2395 ctx
->global
->heap_dealloc_count
++;
2398 else if (DECL_NAME (var
) == heap_deleted_identifier
)
2401 error_at (loc
, "deallocation of already deallocated "
2403 *non_constant_p
= true;
2408 error_at (loc
, "deallocation of storage that was "
2409 "not previously allocated");
2410 *non_constant_p
= true;
2414 /* Allow placement new in std::construct_at, just return the second
2416 if (TREE_CODE (t
) == CALL_EXPR
2417 && cxx_placement_new_fn (fun
)
2419 && ctx
->call
->fundef
2420 && is_std_construct_at (ctx
->call
->fundef
->decl
))
2422 const int nargs
= call_expr_nargs (t
);
2423 tree arg1
= NULL_TREE
;
2424 for (int i
= 0; i
< nargs
; ++i
)
2426 tree arg
= CALL_EXPR_ARG (t
, i
);
2427 arg
= cxx_eval_constant_expression (ctx
, arg
, false,
2428 non_constant_p
, overflow_p
);
2432 VERIFY_CONSTANT (arg
);
2437 else if (cxx_dynamic_cast_fn_p (fun
))
2438 return cxx_eval_dynamic_cast_fn (ctx
, t
, non_constant_p
, overflow_p
);
2442 if (!lambda_static_thunk_p (fun
))
2443 error_at (loc
, "call to non-%<constexpr%> function %qD", fun
);
2444 explain_invalid_constexpr_fn (fun
);
2446 *non_constant_p
= true;
2450 constexpr_ctx new_ctx
= *ctx
;
2451 if (DECL_CONSTRUCTOR_P (fun
) && !ctx
->object
2452 && TREE_CODE (t
) == AGGR_INIT_EXPR
)
2454 /* We want to have an initialization target for an AGGR_INIT_EXPR.
2455 If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
2456 new_ctx
.object
= AGGR_INIT_EXPR_SLOT (t
);
2457 tree ctor
= new_ctx
.ctor
= build_constructor (DECL_CONTEXT (fun
), NULL
);
2458 CONSTRUCTOR_NO_CLEARING (ctor
) = true;
2459 ctx
->global
->values
.put (new_ctx
.object
, ctor
);
2463 /* Shortcut trivial constructor/op=. */
2464 if (trivial_fn_p (fun
))
2466 tree init
= NULL_TREE
;
2467 if (call_expr_nargs (t
) == 2)
2468 init
= convert_from_reference (get_nth_callarg (t
, 1));
2469 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
2470 && AGGR_INIT_ZERO_FIRST (t
))
2471 init
= build_zero_init (DECL_CONTEXT (fun
), NULL_TREE
, false);
2474 tree op
= get_nth_callarg (t
, 0);
2475 if (is_dummy_object (op
))
2478 op
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (op
)), op
);
2479 tree set
= build2 (MODIFY_EXPR
, TREE_TYPE (op
), op
, init
);
2480 new_ctx
.call
= &new_call
;
2481 return cxx_eval_constant_expression (&new_ctx
, set
, lval
,
2482 non_constant_p
, overflow_p
);
2486 /* We can't defer instantiating the function any longer. */
2487 if (!DECL_INITIAL (fun
)
2488 && DECL_TEMPLOID_INSTANTIATION (fun
)
2489 && !uid_sensitive_constexpr_evaluation_p ())
2491 location_t save_loc
= input_location
;
2492 input_location
= loc
;
2494 instantiate_decl (fun
, /*defer_ok*/false, /*expl_inst*/false);
2496 input_location
= save_loc
;
2499 /* If in direct recursive call, optimize definition search. */
2500 if (ctx
&& ctx
->call
&& ctx
->call
->fundef
&& ctx
->call
->fundef
->decl
== fun
)
2501 new_call
.fundef
= ctx
->call
->fundef
;
2504 new_call
.fundef
= retrieve_constexpr_fundef (fun
);
2505 if (new_call
.fundef
== NULL
|| new_call
.fundef
->body
== NULL
2506 || new_call
.fundef
->result
== error_mark_node
2507 || fun
== current_function_decl
)
2511 /* We need to check for current_function_decl here in case we're
2512 being called during cp_fold_function, because at that point
2513 DECL_INITIAL is set properly and we have a fundef but we
2514 haven't lowered invisirefs yet (c++/70344). */
2515 if (DECL_INITIAL (fun
) == error_mark_node
2516 || fun
== current_function_decl
)
2517 error_at (loc
, "%qD called in a constant expression before its "
2518 "definition is complete", fun
);
2519 else if (DECL_INITIAL (fun
))
2521 /* The definition of fun was somehow unsuitable. But pretend
2522 that lambda static thunks don't exist. */
2523 if (!lambda_static_thunk_p (fun
))
2524 error_at (loc
, "%qD called in a constant expression", fun
);
2525 explain_invalid_constexpr_fn (fun
);
2528 error_at (loc
, "%qD used before its definition", fun
);
2530 *non_constant_p
= true;
2535 bool non_constant_args
= false;
2536 cxx_bind_parameters_in_call (ctx
, t
, &new_call
,
2537 non_constant_p
, overflow_p
, &non_constant_args
);
2539 /* We build up the bindings list before we know whether we already have this
2540 call cached. If we don't end up saving these bindings, ggc_free them when
2541 this function exits. */
2546 free_bindings (tree
&b
): bindings (&b
) { }
2547 ~free_bindings () { if (bindings
) ggc_free (*bindings
); }
2548 void preserve () { bindings
= NULL
; }
2549 } fb (new_call
.bindings
);
2551 if (*non_constant_p
)
2554 depth_ok
= push_cx_call_context (t
);
2556 /* Remember the object we are constructing or destructing. */
2557 tree new_obj
= NULL_TREE
;
2558 if (DECL_CONSTRUCTOR_P (fun
) || DECL_DESTRUCTOR_P (fun
))
2560 /* In a cdtor, it should be the first `this' argument.
2561 At this point it has already been evaluated in the call
2562 to cxx_bind_parameters_in_call. */
2563 new_obj
= TREE_VEC_ELT (new_call
.bindings
, 0);
2564 STRIP_NOPS (new_obj
);
2565 if (TREE_CODE (new_obj
) == ADDR_EXPR
)
2566 new_obj
= TREE_OPERAND (new_obj
, 0);
2568 if (ctx
->call
&& ctx
->call
->fundef
2569 && DECL_CONSTRUCTOR_P (ctx
->call
->fundef
->decl
))
2571 tree cur_obj
= TREE_VEC_ELT (ctx
->call
->bindings
, 0);
2572 STRIP_NOPS (cur_obj
);
2573 if (TREE_CODE (cur_obj
) == ADDR_EXPR
)
2574 cur_obj
= TREE_OPERAND (cur_obj
, 0);
2575 if (new_obj
== cur_obj
)
2576 /* We're calling the target constructor of a delegating
2577 constructor, or accessing a base subobject through a
2578 NOP_EXPR as part of a call to a base constructor, so
2579 there is no new (sub)object. */
2580 new_obj
= NULL_TREE
;
2584 tree result
= NULL_TREE
;
2586 constexpr_call
*entry
= NULL
;
2587 if (depth_ok
&& !non_constant_args
&& ctx
->strict
)
2589 new_call
.hash
= constexpr_fundef_hasher::hash (new_call
.fundef
);
2591 = iterative_hash_template_arg (new_call
.bindings
, new_call
.hash
);
2593 = iterative_hash_object (ctx
->manifestly_const_eval
, new_call
.hash
);
2595 /* If we have seen this call before, we are done. */
2596 maybe_initialize_constexpr_call_table ();
2597 constexpr_call
**slot
2598 = constexpr_call_table
->find_slot (&new_call
, INSERT
);
2602 /* Only cache up to constexpr_cache_depth to limit memory use. */
2603 if (depth_ok
< constexpr_cache_depth
)
2605 /* We need to keep a pointer to the entry, not just the slot, as
2606 the slot can move during evaluation of the body. */
2607 *slot
= entry
= ggc_alloc
<constexpr_call
> ();
2612 /* Calls that are in progress have their result set to NULL, so that we
2613 can detect circular dependencies. Now that we only cache up to
2614 constexpr_cache_depth this won't catch circular dependencies that
2615 start deeper, but they'll hit the recursion or ops limit. */
2616 else if (entry
->result
== NULL
)
2619 error ("call has circular dependency");
2620 *non_constant_p
= true;
2621 entry
->result
= result
= error_mark_node
;
2624 result
= entry
->result
;
2630 error ("%<constexpr%> evaluation depth exceeds maximum of %d (use "
2631 "%<-fconstexpr-depth=%> to increase the maximum)",
2632 max_constexpr_depth
);
2633 *non_constant_p
= true;
2634 result
= error_mark_node
;
2638 bool cacheable
= true;
2639 if (result
&& result
!= error_mark_node
)
2641 else if (!DECL_SAVED_TREE (fun
))
2643 /* When at_eof >= 2, cgraph has started throwing away
2644 DECL_SAVED_TREE, so fail quietly. FIXME we get here because of
2645 late code generation for VEC_INIT_EXPR, which needs to be
2646 completely reconsidered. */
2647 gcc_assert (at_eof
>= 2 && ctx
->quiet
);
2648 *non_constant_p
= true;
2650 else if (tree copy
= get_fundef_copy (new_call
.fundef
))
2652 tree body
, parms
, res
;
2653 releasing_vec ctors
;
2655 /* Reuse or create a new unshared copy of this function's body. */
2656 body
= TREE_PURPOSE (copy
);
2657 parms
= TREE_VALUE (copy
);
2658 res
= TREE_TYPE (copy
);
2660 /* Associate the bindings with the remapped parms. */
2661 tree bound
= new_call
.bindings
;
2662 tree remapped
= parms
;
2663 for (int i
= 0; i
< TREE_VEC_LENGTH (bound
); ++i
)
2665 tree arg
= TREE_VEC_ELT (bound
, i
);
2668 /* Unshare args going into the hash table to separate them
2669 from the caller's context, for better GC and to avoid
2670 problems with verify_gimple. */
2671 arg
= unshare_expr_without_location (arg
);
2672 TREE_VEC_ELT (bound
, i
) = arg
;
2674 /* And then unshare again so the callee doesn't change the
2675 argument values in the hash table. XXX Could we unshare
2676 lazily in cxx_eval_store_expression? */
2677 arg
= unshare_constructor (arg
);
2678 if (TREE_CODE (arg
) == CONSTRUCTOR
)
2679 vec_safe_push (ctors
, arg
);
2681 ctx
->global
->values
.put (remapped
, arg
);
2682 remapped
= DECL_CHAIN (remapped
);
2684 /* Add the RESULT_DECL to the values map, too. */
2685 gcc_assert (!DECL_BY_REFERENCE (res
));
2686 ctx
->global
->values
.put (res
, NULL_TREE
);
2688 /* Track the callee's evaluated SAVE_EXPRs and TARGET_EXPRs so that
2689 we can forget their values after the call. */
2690 constexpr_ctx ctx_with_save_exprs
= *ctx
;
2691 auto_vec
<tree
, 10> save_exprs
;
2692 ctx_with_save_exprs
.save_exprs
= &save_exprs
;
2693 ctx_with_save_exprs
.call
= &new_call
;
2694 unsigned save_heap_alloc_count
= ctx
->global
->heap_vars
.length ();
2695 unsigned save_heap_dealloc_count
= ctx
->global
->heap_dealloc_count
;
2697 /* If this is a constexpr destructor, the object's const and volatile
2698 semantics are no longer in effect; see [class.dtor]p5. */
2699 if (new_obj
&& DECL_DESTRUCTOR_P (fun
))
2700 cxx_set_object_constness (ctx
, new_obj
, /*readonly_p=*/false,
2701 non_constant_p
, overflow_p
);
2703 tree jump_target
= NULL_TREE
;
2704 cxx_eval_constant_expression (&ctx_with_save_exprs
, body
,
2705 lval
, non_constant_p
, overflow_p
,
2708 if (DECL_CONSTRUCTOR_P (fun
))
2709 /* This can be null for a subobject constructor call, in
2710 which case what we care about is the initialization
2711 side-effects rather than the value. We could get at the
2712 value by evaluating *this, but we don't bother; there's
2713 no need to put such a call in the hash table. */
2714 result
= lval
? ctx
->object
: ctx
->ctor
;
2715 else if (VOID_TYPE_P (TREE_TYPE (res
)))
2719 result
= *ctx
->global
->values
.get (res
);
2720 if (result
== NULL_TREE
&& !*non_constant_p
)
2723 error ("%<constexpr%> call flows off the end "
2725 *non_constant_p
= true;
2729 /* At this point, the object's constructor will have run, so
2730 the object is no longer under construction, and its possible
2731 'const' semantics now apply. Make a note of this fact by
2732 marking the CONSTRUCTOR TREE_READONLY. */
2733 if (new_obj
&& DECL_CONSTRUCTOR_P (fun
))
2734 cxx_set_object_constness (ctx
, new_obj
, /*readonly_p=*/true,
2735 non_constant_p
, overflow_p
);
2737 /* Forget the saved values of the callee's SAVE_EXPRs and
2741 FOR_EACH_VEC_ELT (save_exprs
, i
, save_expr
)
2742 ctx
->global
->values
.remove (save_expr
);
2744 /* Remove the parms/result from the values map. Is it worth
2745 bothering to do this when the map itself is only live for
2746 one constexpr evaluation? If so, maybe also clear out
2747 other vars from call, maybe in BIND_EXPR handling? */
2748 ctx
->global
->values
.remove (res
);
2749 for (tree parm
= parms
; parm
; parm
= TREE_CHAIN (parm
))
2750 ctx
->global
->values
.remove (parm
);
2752 /* Free any parameter CONSTRUCTORs we aren't returning directly. */
2753 while (!ctors
->is_empty ())
2755 tree c
= ctors
->pop ();
2757 free_constructor (c
);
2760 /* Make the unshared function copy we used available for re-use. */
2761 save_fundef_copy (fun
, copy
);
2763 /* If the call allocated some heap object that hasn't been
2764 deallocated during the call, or if it deallocated some heap
2765 object it has not allocated, the call isn't really stateless
2766 for the constexpr evaluation and should not be cached.
2767 It is fine if the call allocates something and deallocates it
2770 && (save_heap_alloc_count
!= ctx
->global
->heap_vars
.length ()
2771 || (save_heap_dealloc_count
2772 != ctx
->global
->heap_dealloc_count
)))
2776 if ((ctx
->global
->heap_vars
.length ()
2777 - ctx
->global
->heap_dealloc_count
)
2778 != save_heap_alloc_count
- save_heap_dealloc_count
)
2781 FOR_EACH_VEC_ELT_FROM (ctx
->global
->heap_vars
, i
, heap_var
,
2782 save_heap_alloc_count
)
2783 if (DECL_NAME (heap_var
) != heap_deleted_identifier
)
2790 /* Rewrite all occurrences of the function's RESULT_DECL with the
2791 current object under construction. */
2792 if (!*non_constant_p
&& ctx
->object
2793 && CLASS_TYPE_P (TREE_TYPE (res
))
2794 && !is_empty_class (TREE_TYPE (res
)))
2795 if (replace_result_decl (&result
, res
, ctx
->object
))
2799 /* Couldn't get a function copy to evaluate. */
2800 *non_constant_p
= true;
2802 if (result
== error_mark_node
)
2803 *non_constant_p
= true;
2804 if (*non_constant_p
|| *overflow_p
)
2805 result
= error_mark_node
;
2809 entry
->result
= cacheable
? result
: error_mark_node
;
2812 /* The result of a constexpr function must be completely initialized.
2814 However, in C++20, a constexpr constructor doesn't necessarily have
2815 to initialize all the fields, so we don't clear CONSTRUCTOR_NO_CLEARING
2816 in order to detect reading an unitialized object in constexpr instead
2817 of value-initializing it. (reduced_constant_expression_p is expected to
2818 take care of clearing the flag.) */
2819 if (TREE_CODE (result
) == CONSTRUCTOR
2820 && (cxx_dialect
< cxx20
2821 || !DECL_CONSTRUCTOR_P (fun
)))
2822 clear_no_implicit_zero (result
);
2824 pop_cx_call_context ();
2828 /* Return true if T is a valid constant initializer. If a CONSTRUCTOR
2829 initializes all the members, the CONSTRUCTOR_NO_CLEARING flag will be
2831 FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
2834 reduced_constant_expression_p (tree t
)
2839 switch (TREE_CODE (t
))
2842 /* Even if we can't lower this yet, it's constant. */
2846 /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
2847 tree idx
, val
, field
; unsigned HOST_WIDE_INT i
;
2848 if (CONSTRUCTOR_NO_CLEARING (t
))
2850 if (TREE_CODE (TREE_TYPE (t
)) == VECTOR_TYPE
)
2851 /* An initialized vector would have a VECTOR_CST. */
2853 else if (cxx_dialect
>= cxx20
2854 /* An ARRAY_TYPE doesn't have any TYPE_FIELDS. */
2855 && TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
)
2857 else if (cxx_dialect
>= cxx20
2858 && TREE_CODE (TREE_TYPE (t
)) == UNION_TYPE
)
2860 if (CONSTRUCTOR_NELTS (t
) == 0)
2861 /* An initialized union has a constructor element. */
2863 /* And it only initializes one member. */
2867 field
= next_initializable_field (TYPE_FIELDS (TREE_TYPE (t
)));
2871 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t
), i
, idx
, val
)
2873 /* If VAL is null, we're in the middle of initializing this
2875 if (!reduced_constant_expression_p (val
))
2877 /* Empty class field may or may not have an initializer. */
2878 for (; field
&& idx
!= field
;
2879 field
= next_initializable_field (DECL_CHAIN (field
)))
2880 if (!is_really_empty_class (TREE_TYPE (field
),
2881 /*ignore_vptr*/false))
2884 field
= next_initializable_field (DECL_CHAIN (field
));
2886 /* There could be a non-empty field at the end. */
2887 for (; field
; field
= next_initializable_field (DECL_CHAIN (field
)))
2888 if (!is_really_empty_class (TREE_TYPE (field
), /*ignore_vptr*/false))
2890 if (CONSTRUCTOR_NO_CLEARING (t
))
2891 /* All the fields are initialized. */
2892 CONSTRUCTOR_NO_CLEARING (t
) = false;
2896 /* FIXME are we calling this too much? */
2897 return initializer_constant_valid_p (t
, TREE_TYPE (t
)) != NULL_TREE
;
2901 /* Some expressions may have constant operands but are not constant
2902 themselves, such as 1/0. Call this function to check for that
2905 We only call this in places that require an arithmetic constant, not in
2906 places where we might have a non-constant expression that can be a
2907 component of a constant expression, such as the address of a constexpr
2908 variable that might be dereferenced later. */
2911 verify_constant (tree t
, bool allow_non_constant
, bool *non_constant_p
,
2914 if (!*non_constant_p
&& !reduced_constant_expression_p (t
)
2917 if (!allow_non_constant
)
2918 error ("%q+E is not a constant expression", t
);
2919 *non_constant_p
= true;
2921 if (TREE_OVERFLOW_P (t
))
2923 if (!allow_non_constant
)
2925 permerror (input_location
, "overflow in constant expression");
2926 /* If we're being permissive (and are in an enforcing
2927 context), ignore the overflow. */
2928 if (flag_permissive
)
2929 return *non_constant_p
;
2933 return *non_constant_p
;
2936 /* Check whether the shift operation with code CODE and type TYPE on LHS
2937 and RHS is undefined. If it is, give an error with an explanation,
2938 and return true; return false otherwise. */
2941 cxx_eval_check_shift_p (location_t loc
, const constexpr_ctx
*ctx
,
2942 enum tree_code code
, tree type
, tree lhs
, tree rhs
)
2944 if ((code
!= LSHIFT_EXPR
&& code
!= RSHIFT_EXPR
)
2945 || TREE_CODE (lhs
) != INTEGER_CST
2946 || TREE_CODE (rhs
) != INTEGER_CST
)
2949 tree lhstype
= TREE_TYPE (lhs
);
2950 unsigned HOST_WIDE_INT uprec
= TYPE_PRECISION (TREE_TYPE (lhs
));
2952 /* [expr.shift] The behavior is undefined if the right operand
2953 is negative, or greater than or equal to the length in bits
2954 of the promoted left operand. */
2955 if (tree_int_cst_sgn (rhs
) == -1)
2958 permerror (loc
, "right operand of shift expression %q+E is negative",
2959 build2_loc (loc
, code
, type
, lhs
, rhs
));
2960 return (!flag_permissive
|| ctx
->quiet
);
2962 if (compare_tree_int (rhs
, uprec
) >= 0)
2965 permerror (loc
, "right operand of shift expression %q+E is greater "
2966 "than or equal to the precision %wu of the left operand",
2967 build2_loc (loc
, code
, type
, lhs
, rhs
), uprec
);
2968 return (!flag_permissive
|| ctx
->quiet
);
2971 /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
2972 if E1 has a signed type and non-negative value, and E1x2^E2 is
2973 representable in the corresponding unsigned type of the result type,
2974 then that value, converted to the result type, is the resulting value;
2975 otherwise, the behavior is undefined.
2977 The value of E1 << E2 is the unique value congruent to E1 x 2^E2 modulo
2978 2^N, where N is the range exponent of the type of the result. */
2979 if (code
== LSHIFT_EXPR
2980 && !TYPE_UNSIGNED (lhstype
)
2981 && cxx_dialect
>= cxx11
2982 && cxx_dialect
< cxx20
)
2984 if (tree_int_cst_sgn (lhs
) == -1)
2988 "left operand of shift expression %q+E is negative",
2989 build2_loc (loc
, code
, type
, lhs
, rhs
));
2990 return (!flag_permissive
|| ctx
->quiet
);
2992 /* For signed x << y the following:
2993 (unsigned) x >> ((prec (lhs) - 1) - y)
2994 if > 1, is undefined. The right-hand side of this formula
2995 is the highest bit of the LHS that can be set (starting from 0),
2996 so that the shift doesn't overflow. We then right-shift the LHS
2997 to see whether any other bit is set making the original shift
2998 undefined -- the result is not representable in the corresponding
3000 tree t
= build_int_cst (unsigned_type_node
, uprec
- 1);
3001 t
= fold_build2 (MINUS_EXPR
, unsigned_type_node
, t
, rhs
);
3002 tree ulhs
= fold_convert (unsigned_type_for (lhstype
), lhs
);
3003 t
= fold_build2 (RSHIFT_EXPR
, TREE_TYPE (ulhs
), ulhs
, t
);
3004 if (tree_int_cst_lt (integer_one_node
, t
))
3007 permerror (loc
, "shift expression %q+E overflows",
3008 build2_loc (loc
, code
, type
, lhs
, rhs
));
3009 return (!flag_permissive
|| ctx
->quiet
);
3015 /* Subroutine of cxx_eval_constant_expression.
3016 Attempt to reduce the unary expression tree T to a compile time value.
3017 If successful, return the value. Otherwise issue a diagnostic
3018 and return error_mark_node. */
3021 cxx_eval_unary_expression (const constexpr_ctx
*ctx
, tree t
,
3023 bool *non_constant_p
, bool *overflow_p
)
3026 tree orig_arg
= TREE_OPERAND (t
, 0);
3027 tree arg
= cxx_eval_constant_expression (ctx
, orig_arg
, /*lval*/false,
3028 non_constant_p
, overflow_p
);
3029 VERIFY_CONSTANT (arg
);
3030 location_t loc
= EXPR_LOCATION (t
);
3031 enum tree_code code
= TREE_CODE (t
);
3032 tree type
= TREE_TYPE (t
);
3033 r
= fold_unary_loc (loc
, code
, type
, arg
);
3036 if (arg
== orig_arg
)
3039 r
= build1_loc (loc
, code
, type
, arg
);
3041 VERIFY_CONSTANT (r
);
3045 /* Helper function for cxx_eval_binary_expression. Try to optimize
3046 original POINTER_PLUS_EXPR T, LHS p+ RHS, return NULL_TREE if the
3047 generic folding should be used. */
3050 cxx_fold_pointer_plus_expression (const constexpr_ctx
*ctx
, tree t
,
3051 tree lhs
, tree rhs
, bool *non_constant_p
,
3055 if (TREE_CODE (lhs
) != ADDR_EXPR
)
3058 lhs
= TREE_OPERAND (lhs
, 0);
3060 /* &A[i] p+ j => &A[i + j] */
3061 if (TREE_CODE (lhs
) == ARRAY_REF
3062 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == INTEGER_CST
3063 && TREE_CODE (rhs
) == INTEGER_CST
3064 && TYPE_SIZE_UNIT (TREE_TYPE (lhs
))
3065 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs
))) == INTEGER_CST
)
3067 tree orig_type
= TREE_TYPE (t
);
3068 location_t loc
= EXPR_LOCATION (t
);
3069 tree type
= TREE_TYPE (lhs
);
3071 t
= fold_convert_loc (loc
, ssizetype
, TREE_OPERAND (lhs
, 1));
3072 tree nelts
= array_type_nelts_top (TREE_TYPE (TREE_OPERAND (lhs
, 0)));
3073 nelts
= cxx_eval_constant_expression (ctx
, nelts
, false, non_constant_p
,
3075 if (*non_constant_p
)
3077 /* Don't fold an out-of-bound access. */
3078 if (!tree_int_cst_le (t
, nelts
))
3080 rhs
= cp_fold_convert (ssizetype
, rhs
);
3081 /* Don't fold if rhs can't be divided exactly by TYPE_SIZE_UNIT.
3082 constexpr int A[1]; ... (char *)&A[0] + 1 */
3083 if (!integer_zerop (fold_build2_loc (loc
, TRUNC_MOD_EXPR
, sizetype
,
3084 rhs
, TYPE_SIZE_UNIT (type
))))
3086 /* Make sure to treat the second operand of POINTER_PLUS_EXPR
3088 rhs
= fold_build2_loc (loc
, EXACT_DIV_EXPR
, ssizetype
, rhs
,
3089 TYPE_SIZE_UNIT (type
));
3090 t
= size_binop_loc (loc
, PLUS_EXPR
, rhs
, t
);
3091 t
= build4_loc (loc
, ARRAY_REF
, type
, TREE_OPERAND (lhs
, 0),
3092 t
, NULL_TREE
, NULL_TREE
);
3093 t
= cp_build_addr_expr (t
, tf_warning_or_error
);
3094 t
= cp_fold_convert (orig_type
, t
);
3095 return cxx_eval_constant_expression (ctx
, t
, /*lval*/false,
3096 non_constant_p
, overflow_p
);
3102 /* Subroutine of cxx_eval_constant_expression.
3103 Like cxx_eval_unary_expression, except for binary expressions. */
3106 cxx_eval_binary_expression (const constexpr_ctx
*ctx
, tree t
,
3108 bool *non_constant_p
, bool *overflow_p
)
3111 tree orig_lhs
= TREE_OPERAND (t
, 0);
3112 tree orig_rhs
= TREE_OPERAND (t
, 1);
3114 lhs
= cxx_eval_constant_expression (ctx
, orig_lhs
, /*lval*/false,
3115 non_constant_p
, overflow_p
);
3116 /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer
3118 if (*non_constant_p
)
3120 rhs
= cxx_eval_constant_expression (ctx
, orig_rhs
, /*lval*/false,
3121 non_constant_p
, overflow_p
);
3122 if (*non_constant_p
)
3125 location_t loc
= EXPR_LOCATION (t
);
3126 enum tree_code code
= TREE_CODE (t
);
3127 tree type
= TREE_TYPE (t
);
3129 if (code
== EQ_EXPR
|| code
== NE_EXPR
)
3131 bool is_code_eq
= (code
== EQ_EXPR
);
3133 if (TREE_CODE (lhs
) == PTRMEM_CST
3134 && TREE_CODE (rhs
) == PTRMEM_CST
)
3136 tree lmem
= PTRMEM_CST_MEMBER (lhs
);
3137 tree rmem
= PTRMEM_CST_MEMBER (rhs
);
3139 if (TREE_CODE (lmem
) == TREE_CODE (rmem
)
3140 && TREE_CODE (lmem
) == FIELD_DECL
3141 && TREE_CODE (DECL_CONTEXT (lmem
)) == UNION_TYPE
3142 && same_type_p (DECL_CONTEXT (lmem
),
3143 DECL_CONTEXT (rmem
)))
3144 /* If both refer to (possibly different) members of the same union
3145 (12.3), they compare equal. */
3148 eq
= cp_tree_equal (lhs
, rhs
);
3149 r
= constant_boolean_node (eq
== is_code_eq
, type
);
3151 else if ((TREE_CODE (lhs
) == PTRMEM_CST
3152 || TREE_CODE (rhs
) == PTRMEM_CST
)
3153 && (null_member_pointer_value_p (lhs
)
3154 || null_member_pointer_value_p (rhs
)))
3155 r
= constant_boolean_node (!is_code_eq
, type
);
3156 else if (TREE_CODE (lhs
) == PTRMEM_CST
)
3157 lhs
= cplus_expand_constant (lhs
);
3158 else if (TREE_CODE (rhs
) == PTRMEM_CST
)
3159 rhs
= cplus_expand_constant (rhs
);
3161 if (code
== POINTER_PLUS_EXPR
&& !*non_constant_p
3162 && integer_zerop (lhs
) && !integer_zerop (rhs
))
3165 error ("arithmetic involving a null pointer in %qE", lhs
);
3166 *non_constant_p
= true;
3169 else if (code
== POINTER_PLUS_EXPR
)
3170 r
= cxx_fold_pointer_plus_expression (ctx
, t
, lhs
, rhs
, non_constant_p
,
3172 else if (code
== SPACESHIP_EXPR
)
3174 r
= genericize_spaceship (loc
, type
, lhs
, rhs
);
3175 return cxx_eval_constant_expression (ctx
, r
, lval
, non_constant_p
,
3180 r
= fold_binary_loc (loc
, code
, type
, lhs
, rhs
);
3183 && (code
== LSHIFT_EXPR
|| code
== RSHIFT_EXPR
)
3184 && TREE_CODE (lhs
) == INTEGER_CST
3185 && TREE_CODE (rhs
) == INTEGER_CST
3186 && wi::neg_p (wi::to_wide (rhs
)))
3188 /* For diagnostics and -fpermissive emulate previous behavior of
3189 handling shifts by negative amount. */
3190 tree nrhs
= const_unop (NEGATE_EXPR
, TREE_TYPE (rhs
), rhs
);
3192 r
= fold_binary_loc (loc
,
3193 code
== LSHIFT_EXPR
? RSHIFT_EXPR
: LSHIFT_EXPR
,
3199 if (lhs
== orig_lhs
&& rhs
== orig_rhs
)
3202 r
= build2_loc (loc
, code
, type
, lhs
, rhs
);
3204 else if (cxx_eval_check_shift_p (loc
, ctx
, code
, type
, lhs
, rhs
))
3205 *non_constant_p
= true;
3206 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
3207 a local array in a constexpr function. */
3208 bool ptr
= INDIRECT_TYPE_P (TREE_TYPE (lhs
));
3210 VERIFY_CONSTANT (r
);
3214 /* Subroutine of cxx_eval_constant_expression.
3215 Attempt to evaluate condition expressions. Dead branches are not
3219 cxx_eval_conditional_expression (const constexpr_ctx
*ctx
, tree t
,
3221 bool *non_constant_p
, bool *overflow_p
,
3224 tree val
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
3226 non_constant_p
, overflow_p
);
3227 VERIFY_CONSTANT (val
);
3228 /* Don't VERIFY_CONSTANT the other operands. */
3229 if (integer_zerop (val
))
3230 val
= TREE_OPERAND (t
, 2);
3232 val
= TREE_OPERAND (t
, 1);
3233 if (TREE_CODE (t
) == IF_STMT
&& !val
)
3235 return cxx_eval_constant_expression (ctx
, val
, lval
, non_constant_p
,
3236 overflow_p
, jump_target
);
3239 /* Subroutine of cxx_eval_constant_expression.
3240 Attempt to evaluate vector condition expressions. Unlike
3241 cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal
3242 ternary arithmetics operation, where all 3 arguments have to be
3243 evaluated as constants and then folding computes the result from
3247 cxx_eval_vector_conditional_expression (const constexpr_ctx
*ctx
, tree t
,
3248 bool *non_constant_p
, bool *overflow_p
)
3250 tree arg1
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
3252 non_constant_p
, overflow_p
);
3253 VERIFY_CONSTANT (arg1
);
3254 tree arg2
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
3256 non_constant_p
, overflow_p
);
3257 VERIFY_CONSTANT (arg2
);
3258 tree arg3
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 2),
3260 non_constant_p
, overflow_p
);
3261 VERIFY_CONSTANT (arg3
);
3262 location_t loc
= EXPR_LOCATION (t
);
3263 tree type
= TREE_TYPE (t
);
3264 tree r
= fold_ternary_loc (loc
, VEC_COND_EXPR
, type
, arg1
, arg2
, arg3
);
3267 if (arg1
== TREE_OPERAND (t
, 0)
3268 && arg2
== TREE_OPERAND (t
, 1)
3269 && arg3
== TREE_OPERAND (t
, 2))
3272 r
= build3_loc (loc
, VEC_COND_EXPR
, type
, arg1
, arg2
, arg3
);
3274 VERIFY_CONSTANT (r
);
3278 /* Returns less than, equal to, or greater than zero if KEY is found to be
3279 less than, to match, or to be greater than the constructor_elt's INDEX. */
3282 array_index_cmp (tree key
, tree index
)
3284 gcc_assert (TREE_CODE (key
) == INTEGER_CST
);
3286 switch (TREE_CODE (index
))
3289 return tree_int_cst_compare (key
, index
);
3292 tree lo
= TREE_OPERAND (index
, 0);
3293 tree hi
= TREE_OPERAND (index
, 1);
3294 if (tree_int_cst_lt (key
, lo
))
3296 else if (tree_int_cst_lt (hi
, key
))
3306 /* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1
3307 if none. If INSERT is true, insert a matching element rather than fail. */
3309 static HOST_WIDE_INT
3310 find_array_ctor_elt (tree ary
, tree dindex
, bool insert
)
3312 if (tree_int_cst_sgn (dindex
) < 0)
3315 unsigned HOST_WIDE_INT i
= tree_to_uhwi (dindex
);
3316 vec
<constructor_elt
, va_gc
> *elts
= CONSTRUCTOR_ELTS (ary
);
3317 unsigned HOST_WIDE_INT len
= vec_safe_length (elts
);
3319 unsigned HOST_WIDE_INT end
= len
;
3320 unsigned HOST_WIDE_INT begin
= 0;
3322 /* If the last element of the CONSTRUCTOR has its own index, we can assume
3323 that the same is true of the other elements and index directly. */
3326 tree cindex
= (*elts
)[end
- 1].index
;
3327 if (cindex
== NULL_TREE
)
3329 /* Verify that if the last index is missing, all indexes
3332 for (unsigned int j
= 0; j
< len
- 1; ++j
)
3333 gcc_assert ((*elts
)[j
].index
== NULL_TREE
);
3340 /* If the element is to be added right at the end,
3341 make sure it is added with cleared index too. */
3344 /* Otherwise, in order not to break the assumption
3345 that CONSTRUCTOR either has all indexes or none,
3346 we need to add indexes to all elements. */
3347 for (unsigned int j
= 0; j
< len
; ++j
)
3348 (*elts
)[j
].index
= build_int_cst (TREE_TYPE (dindex
), j
);
3351 else if (TREE_CODE (cindex
) == INTEGER_CST
3352 && compare_tree_int (cindex
, end
- 1) == 0)
3361 /* Otherwise, find a matching index by means of a binary search. */
3362 while (begin
!= end
)
3364 unsigned HOST_WIDE_INT middle
= (begin
+ end
) / 2;
3365 constructor_elt
&elt
= (*elts
)[middle
];
3366 tree idx
= elt
.index
;
3368 int cmp
= array_index_cmp (dindex
, idx
);
3375 if (insert
&& TREE_CODE (idx
) == RANGE_EXPR
)
3377 /* We need to split the range. */
3379 tree lo
= TREE_OPERAND (idx
, 0);
3380 tree hi
= TREE_OPERAND (idx
, 1);
3381 tree value
= elt
.value
;
3382 dindex
= fold_convert (sizetype
, dindex
);
3383 if (tree_int_cst_lt (lo
, dindex
))
3385 /* There are still some lower elts; shorten the range. */
3386 tree new_hi
= int_const_binop (MINUS_EXPR
, dindex
,
3388 if (tree_int_cst_equal (lo
, new_hi
))
3389 /* Only one element left, no longer a range. */
3392 TREE_OPERAND (idx
, 1) = new_hi
;
3393 /* Append the element we want to insert. */
3396 e
.value
= unshare_constructor (value
);
3397 vec_safe_insert (CONSTRUCTOR_ELTS (ary
), middle
, e
);
3400 /* No lower elts, the range elt is now ours. */
3403 if (tree_int_cst_lt (dindex
, hi
))
3405 /* There are still some higher elts; append a range. */
3406 tree new_lo
= int_const_binop (PLUS_EXPR
, dindex
,
3408 if (tree_int_cst_equal (new_lo
, hi
))
3411 e
.index
= build2 (RANGE_EXPR
, sizetype
, new_lo
, hi
);
3412 e
.value
= unshare_constructor (value
);
3413 vec_safe_insert (CONSTRUCTOR_ELTS (ary
), middle
+ 1, e
);
3422 constructor_elt e
= { dindex
, NULL_TREE
};
3423 vec_safe_insert (CONSTRUCTOR_ELTS (ary
), end
, e
);
3430 /* Return a pointer to the constructor_elt of CTOR which matches INDEX. If no
3431 matching constructor_elt exists, then add one to CTOR.
3433 As an optimization, if POS_HINT is non-negative then it is used as a guess
3434 for the (integer) index of the matching constructor_elt within CTOR. */
3436 static constructor_elt
*
3437 get_or_insert_ctor_field (tree ctor
, tree index
, int pos_hint
= -1)
3439 /* Check the hint first. */
3440 if (pos_hint
>= 0 && (unsigned)pos_hint
< CONSTRUCTOR_NELTS (ctor
)
3441 && CONSTRUCTOR_ELT (ctor
, pos_hint
)->index
== index
)
3442 return CONSTRUCTOR_ELT (ctor
, pos_hint
);
3444 tree type
= TREE_TYPE (ctor
);
3445 if (TREE_CODE (type
) == VECTOR_TYPE
&& index
== NULL_TREE
)
3447 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (ctor
), index
, NULL_TREE
);
3448 return &CONSTRUCTOR_ELTS (ctor
)->last();
3450 else if (TREE_CODE (type
) == ARRAY_TYPE
|| TREE_CODE (type
) == VECTOR_TYPE
)
3452 if (TREE_CODE (index
) == RANGE_EXPR
)
3454 /* Support for RANGE_EXPR index lookups is currently limited to
3455 accessing an existing element via POS_HINT, or appending a new
3456 element to the end of CTOR. ??? Support for other access
3457 patterns may also be needed. */
3458 vec
<constructor_elt
, va_gc
> *elts
= CONSTRUCTOR_ELTS (ctor
);
3459 if (vec_safe_length (elts
))
3461 tree lo
= TREE_OPERAND (index
, 0);
3462 gcc_assert (array_index_cmp (elts
->last().index
, lo
) < 0);
3464 CONSTRUCTOR_APPEND_ELT (elts
, index
, NULL_TREE
);
3465 return &elts
->last();
3468 HOST_WIDE_INT i
= find_array_ctor_elt (ctor
, index
, /*insert*/true);
3469 gcc_assert (i
>= 0);
3470 constructor_elt
*cep
= CONSTRUCTOR_ELT (ctor
, i
);
3471 gcc_assert (cep
->index
== NULL_TREE
3472 || TREE_CODE (cep
->index
) != RANGE_EXPR
);
3477 gcc_assert (TREE_CODE (index
) == FIELD_DECL
);
3479 /* We must keep the CONSTRUCTOR's ELTS in FIELD order.
3480 Usually we meet initializers in that order, but it is
3481 possible for base types to be placed not in program
3483 tree fields
= TYPE_FIELDS (DECL_CONTEXT (index
));
3484 unsigned HOST_WIDE_INT idx
= 0;
3485 constructor_elt
*cep
= NULL
;
3487 /* Check if we're changing the active member of a union. */
3488 if (TREE_CODE (type
) == UNION_TYPE
&& CONSTRUCTOR_NELTS (ctor
)
3489 && CONSTRUCTOR_ELT (ctor
, 0)->index
!= index
)
3490 vec_safe_truncate (CONSTRUCTOR_ELTS (ctor
), 0);
3491 /* If the bit offset of INDEX is larger than that of the last
3492 constructor_elt, then we can just immediately append a new
3493 constructor_elt to the end of CTOR. */
3494 else if (CONSTRUCTOR_NELTS (ctor
)
3495 && tree_int_cst_compare (bit_position (index
),
3496 bit_position (CONSTRUCTOR_ELTS (ctor
)
3497 ->last().index
)) > 0)
3499 idx
= CONSTRUCTOR_NELTS (ctor
);
3503 /* Otherwise, we need to iterate over CTOR to find or insert INDEX
3506 for (; vec_safe_iterate (CONSTRUCTOR_ELTS (ctor
), idx
, &cep
);
3507 idx
++, fields
= DECL_CHAIN (fields
))
3509 if (index
== cep
->index
)
3512 /* The field we're initializing must be on the field
3513 list. Look to see if it is present before the
3514 field the current ELT initializes. */
3515 for (; fields
!= cep
->index
; fields
= DECL_CHAIN (fields
))
3516 if (index
== fields
)
3519 /* We fell off the end of the CONSTRUCTOR, so insert a new
3520 entry at the end. */
3524 constructor_elt ce
= { index
, NULL_TREE
};
3526 vec_safe_insert (CONSTRUCTOR_ELTS (ctor
), idx
, ce
);
3527 cep
= CONSTRUCTOR_ELT (ctor
, idx
);
3535 /* Under the control of CTX, issue a detailed diagnostic for
3536 an out-of-bounds subscript INDEX into the expression ARRAY. */
3539 diag_array_subscript (location_t loc
, const constexpr_ctx
*ctx
, tree array
, tree index
)
3543 tree arraytype
= TREE_TYPE (array
);
3545 /* Convert the unsigned array subscript to a signed integer to avoid
3546 printing huge numbers for small negative values. */
3547 tree sidx
= fold_convert (ssizetype
, index
);
3548 STRIP_ANY_LOCATION_WRAPPER (array
);
3551 if (TYPE_DOMAIN (arraytype
))
3552 error_at (loc
, "array subscript value %qE is outside the bounds "
3553 "of array %qD of type %qT", sidx
, array
, arraytype
);
3555 error_at (loc
, "nonzero array subscript %qE is used with array %qD of "
3556 "type %qT with unknown bounds", sidx
, array
, arraytype
);
3557 inform (DECL_SOURCE_LOCATION (array
), "declared here");
3559 else if (TYPE_DOMAIN (arraytype
))
3560 error_at (loc
, "array subscript value %qE is outside the bounds "
3561 "of array type %qT", sidx
, arraytype
);
3563 error_at (loc
, "nonzero array subscript %qE is used with array of type %qT "
3564 "with unknown bounds", sidx
, arraytype
);
3568 /* Return the number of elements for TYPE (which is an ARRAY_TYPE or
3572 get_array_or_vector_nelts (const constexpr_ctx
*ctx
, tree type
,
3573 bool *non_constant_p
, bool *overflow_p
)
3576 if (TREE_CODE (type
) == ARRAY_TYPE
)
3578 if (TYPE_DOMAIN (type
))
3579 nelts
= array_type_nelts_top (type
);
3581 nelts
= size_zero_node
;
3583 else if (VECTOR_TYPE_P (type
))
3584 nelts
= size_int (TYPE_VECTOR_SUBPARTS (type
));
3588 /* For VLAs, the number of elements won't be an integer constant. */
3589 nelts
= cxx_eval_constant_expression (ctx
, nelts
, false,
3590 non_constant_p
, overflow_p
);
3594 /* Extract element INDEX consisting of CHARS_PER_ELT chars from
3595 STRING_CST STRING. */
3598 extract_string_elt (tree string
, unsigned chars_per_elt
, unsigned index
)
3600 tree type
= cv_unqualified (TREE_TYPE (TREE_TYPE (string
)));
3603 if (chars_per_elt
== 1)
3604 r
= build_int_cst (type
, TREE_STRING_POINTER (string
)[index
]);
3607 const unsigned char *ptr
3608 = ((const unsigned char *)TREE_STRING_POINTER (string
)
3609 + index
* chars_per_elt
);
3610 r
= native_interpret_expr (type
, ptr
, chars_per_elt
);
3615 /* Subroutine of cxx_eval_array_reference. T is an ARRAY_REF; evaluate the
3616 subscript, diagnose any problems with it, and return the result. */
3619 eval_and_check_array_index (const constexpr_ctx
*ctx
,
3620 tree t
, bool allow_one_past
,
3621 bool *non_constant_p
, bool *overflow_p
)
3623 location_t loc
= cp_expr_loc_or_input_loc (t
);
3624 tree ary
= TREE_OPERAND (t
, 0);
3625 t
= TREE_OPERAND (t
, 1);
3626 tree index
= cxx_eval_constant_expression (ctx
, t
, false,
3627 non_constant_p
, overflow_p
);
3628 VERIFY_CONSTANT (index
);
3630 if (!tree_fits_shwi_p (index
)
3631 || tree_int_cst_sgn (index
) < 0)
3633 diag_array_subscript (loc
, ctx
, ary
, index
);
3634 *non_constant_p
= true;
3638 tree nelts
= get_array_or_vector_nelts (ctx
, TREE_TYPE (ary
), non_constant_p
,
3640 VERIFY_CONSTANT (nelts
);
3642 ? !tree_int_cst_le (index
, nelts
)
3643 : !tree_int_cst_lt (index
, nelts
))
3645 diag_array_subscript (loc
, ctx
, ary
, index
);
3646 *non_constant_p
= true;
3653 /* Subroutine of cxx_eval_constant_expression.
3654 Attempt to reduce a reference to an array slot. */
3657 cxx_eval_array_reference (const constexpr_ctx
*ctx
, tree t
,
3659 bool *non_constant_p
, bool *overflow_p
)
3661 tree oldary
= TREE_OPERAND (t
, 0);
3662 tree ary
= cxx_eval_constant_expression (ctx
, oldary
,
3664 non_constant_p
, overflow_p
);
3665 if (*non_constant_p
)
3668 && TREE_CODE (ary
) == VIEW_CONVERT_EXPR
3669 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary
, 0)))
3670 && TREE_TYPE (t
) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary
, 0))))
3671 ary
= TREE_OPERAND (ary
, 0);
3673 tree oldidx
= TREE_OPERAND (t
, 1);
3674 tree index
= eval_and_check_array_index (ctx
, t
, lval
,
3675 non_constant_p
, overflow_p
);
3676 if (*non_constant_p
)
3679 if (lval
&& ary
== oldary
&& index
== oldidx
)
3682 return build4 (ARRAY_REF
, TREE_TYPE (t
), ary
, index
, NULL
, NULL
);
3684 unsigned len
= 0, elem_nchars
= 1;
3685 tree elem_type
= TREE_TYPE (TREE_TYPE (ary
));
3686 if (TREE_CODE (ary
) == CONSTRUCTOR
)
3687 len
= CONSTRUCTOR_NELTS (ary
);
3688 else if (TREE_CODE (ary
) == STRING_CST
)
3690 elem_nchars
= (TYPE_PRECISION (elem_type
)
3691 / TYPE_PRECISION (char_type_node
));
3692 len
= (unsigned) TREE_STRING_LENGTH (ary
) / elem_nchars
;
3694 else if (TREE_CODE (ary
) == VECTOR_CST
)
3695 /* We don't create variable-length VECTOR_CSTs. */
3696 len
= VECTOR_CST_NELTS (ary
).to_constant ();
3699 /* We can't do anything with other tree codes, so use
3700 VERIFY_CONSTANT to complain and fail. */
3701 VERIFY_CONSTANT (ary
);
3706 HOST_WIDE_INT i
= 0;
3707 if (TREE_CODE (ary
) == CONSTRUCTOR
)
3709 HOST_WIDE_INT ix
= find_array_ctor_elt (ary
, index
);
3716 i
= tree_to_shwi (index
);
3723 if (TREE_CODE (ary
) == CONSTRUCTOR
)
3724 r
= (*CONSTRUCTOR_ELTS (ary
))[i
].value
;
3725 else if (TREE_CODE (ary
) == VECTOR_CST
)
3726 r
= VECTOR_CST_ELT (ary
, i
);
3728 r
= extract_string_elt (ary
, elem_nchars
, i
);
3731 /* Don't VERIFY_CONSTANT here. */
3734 /* Otherwise the element doesn't have a value yet. */
3739 if (TREE_CODE (ary
) == CONSTRUCTOR
3740 && CONSTRUCTOR_NO_CLEARING (ary
))
3742 /* 'ary' is part of the aggregate initializer we're currently
3743 building; if there's no initializer for this element yet,
3746 error ("accessing uninitialized array element");
3747 *non_constant_p
= true;
3751 /* If it's within the array bounds but doesn't have an explicit
3752 initializer, it's initialized from {}. But use build_value_init
3753 directly for non-aggregates to avoid creating a garbage CONSTRUCTOR. */
3755 constexpr_ctx new_ctx
;
3756 if (CP_AGGREGATE_TYPE_P (elem_type
))
3758 tree empty_ctor
= build_constructor (init_list_type_node
, NULL
);
3759 val
= digest_init (elem_type
, empty_ctor
, tf_warning_or_error
);
3761 new_ctx
.ctor
= build_constructor (elem_type
, NULL
);
3765 val
= build_value_init (elem_type
, tf_warning_or_error
);
3766 t
= cxx_eval_constant_expression (ctx
, val
, lval
, non_constant_p
,
3768 if (CP_AGGREGATE_TYPE_P (elem_type
) && t
!= ctx
->ctor
)
3769 free_constructor (ctx
->ctor
);
3773 /* Subroutine of cxx_eval_constant_expression.
3774 Attempt to reduce a field access of a value of class type. */
3777 cxx_eval_component_reference (const constexpr_ctx
*ctx
, tree t
,
3779 bool *non_constant_p
, bool *overflow_p
)
3781 unsigned HOST_WIDE_INT i
;
3784 tree part
= TREE_OPERAND (t
, 1);
3785 tree orig_whole
= TREE_OPERAND (t
, 0);
3786 tree whole
= cxx_eval_constant_expression (ctx
, orig_whole
,
3788 non_constant_p
, overflow_p
);
3789 if (INDIRECT_REF_P (whole
)
3790 && integer_zerop (TREE_OPERAND (whole
, 0)))
3793 error ("dereferencing a null pointer in %qE", orig_whole
);
3794 *non_constant_p
= true;
3798 if (TREE_CODE (whole
) == PTRMEM_CST
)
3799 whole
= cplus_expand_constant (whole
);
3800 if (whole
== orig_whole
)
3803 return fold_build3 (COMPONENT_REF
, TREE_TYPE (t
),
3804 whole
, part
, NULL_TREE
);
3805 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
3807 if (!*non_constant_p
&& TREE_CODE (whole
) != CONSTRUCTOR
)
3810 error ("%qE is not a constant expression", orig_whole
);
3811 *non_constant_p
= true;
3813 if (DECL_MUTABLE_P (part
))
3816 error ("mutable %qD is not usable in a constant expression", part
);
3817 *non_constant_p
= true;
3819 if (*non_constant_p
)
3821 bool pmf
= TYPE_PTRMEMFUNC_P (TREE_TYPE (whole
));
3822 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole
), i
, field
, value
)
3824 /* Use name match for PMF fields, as a variant will have a
3825 different FIELD_DECL with a different type. */
3826 if (pmf
? DECL_NAME (field
) == DECL_NAME (part
)
3831 STRIP_ANY_LOCATION_WRAPPER (value
);
3835 /* We're in the middle of initializing it. */
3839 if (TREE_CODE (TREE_TYPE (whole
)) == UNION_TYPE
3840 && CONSTRUCTOR_NELTS (whole
) > 0)
3842 /* DR 1188 says we don't have to deal with this. */
3845 constructor_elt
*cep
= CONSTRUCTOR_ELT (whole
, 0);
3846 if (cep
->value
== NULL_TREE
)
3847 error ("accessing uninitialized member %qD", part
);
3849 error ("accessing %qD member instead of initialized %qD member in "
3850 "constant expression", part
, cep
->index
);
3852 *non_constant_p
= true;
3856 /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty
3857 classes never get represented; throw together a value now. */
3858 if (is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
3859 return build_constructor (TREE_TYPE (t
), NULL
);
3861 gcc_assert (DECL_CONTEXT (part
) == TYPE_MAIN_VARIANT (TREE_TYPE (whole
)));
3863 if (CONSTRUCTOR_NO_CLEARING (whole
))
3865 /* 'whole' is part of the aggregate initializer we're currently
3866 building; if there's no initializer for this member yet, that's an
3869 error ("accessing uninitialized member %qD", part
);
3870 *non_constant_p
= true;
3874 /* If there's no explicit init for this field, it's value-initialized. */
3875 value
= build_value_init (TREE_TYPE (t
), tf_warning_or_error
);
3876 return cxx_eval_constant_expression (ctx
, value
,
3878 non_constant_p
, overflow_p
);
3881 /* Subroutine of cxx_eval_constant_expression.
3882 Attempt to reduce a field access of a value of class type that is
3883 expressed as a BIT_FIELD_REF. */
3886 cxx_eval_bit_field_ref (const constexpr_ctx
*ctx
, tree t
,
3888 bool *non_constant_p
, bool *overflow_p
)
3890 tree orig_whole
= TREE_OPERAND (t
, 0);
3891 tree retval
, fldval
, utype
, mask
;
3892 bool fld_seen
= false;
3893 HOST_WIDE_INT istart
, isize
;
3894 tree whole
= cxx_eval_constant_expression (ctx
, orig_whole
,
3896 non_constant_p
, overflow_p
);
3897 tree start
, field
, value
;
3898 unsigned HOST_WIDE_INT i
;
3900 if (whole
== orig_whole
)
3902 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
3904 if (!*non_constant_p
3905 && TREE_CODE (whole
) != VECTOR_CST
3906 && TREE_CODE (whole
) != CONSTRUCTOR
)
3909 error ("%qE is not a constant expression", orig_whole
);
3910 *non_constant_p
= true;
3912 if (*non_constant_p
)
3915 if (TREE_CODE (whole
) == VECTOR_CST
)
3916 return fold_ternary (BIT_FIELD_REF
, TREE_TYPE (t
), whole
,
3917 TREE_OPERAND (t
, 1), TREE_OPERAND (t
, 2));
3919 start
= TREE_OPERAND (t
, 2);
3920 istart
= tree_to_shwi (start
);
3921 isize
= tree_to_shwi (TREE_OPERAND (t
, 1));
3922 utype
= TREE_TYPE (t
);
3923 if (!TYPE_UNSIGNED (utype
))
3924 utype
= build_nonstandard_integer_type (TYPE_PRECISION (utype
), 1);
3925 retval
= build_int_cst (utype
, 0);
3926 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole
), i
, field
, value
)
3928 tree bitpos
= bit_position (field
);
3929 STRIP_ANY_LOCATION_WRAPPER (value
);
3930 if (bitpos
== start
&& DECL_SIZE (field
) == TREE_OPERAND (t
, 1))
3932 if (TREE_CODE (TREE_TYPE (field
)) == INTEGER_TYPE
3933 && TREE_CODE (value
) == INTEGER_CST
3934 && tree_fits_shwi_p (bitpos
)
3935 && tree_fits_shwi_p (DECL_SIZE (field
)))
3937 HOST_WIDE_INT bit
= tree_to_shwi (bitpos
);
3938 HOST_WIDE_INT sz
= tree_to_shwi (DECL_SIZE (field
));
3939 HOST_WIDE_INT shift
;
3940 if (bit
>= istart
&& bit
+ sz
<= istart
+ isize
)
3942 fldval
= fold_convert (utype
, value
);
3943 mask
= build_int_cst_type (utype
, -1);
3944 mask
= fold_build2 (LSHIFT_EXPR
, utype
, mask
,
3945 size_int (TYPE_PRECISION (utype
) - sz
));
3946 mask
= fold_build2 (RSHIFT_EXPR
, utype
, mask
,
3947 size_int (TYPE_PRECISION (utype
) - sz
));
3948 fldval
= fold_build2 (BIT_AND_EXPR
, utype
, fldval
, mask
);
3949 shift
= bit
- istart
;
3950 if (BYTES_BIG_ENDIAN
)
3951 shift
= TYPE_PRECISION (utype
) - shift
- sz
;
3952 fldval
= fold_build2 (LSHIFT_EXPR
, utype
, fldval
,
3954 retval
= fold_build2 (BIT_IOR_EXPR
, utype
, retval
, fldval
);
3960 return fold_convert (TREE_TYPE (t
), retval
);
3962 return error_mark_node
;
3965 /* Helper for cxx_eval_bit_cast.
3966 Check [bit.cast]/3 rules, bit_cast is constexpr only if the To and From
3967 types and types of all subobjects have is_union_v<T>, is_pointer_v<T>,
3968 is_member_pointer_v<T>, is_volatile_v<T> false and has no non-static
3969 data members of reference type. */
3972 check_bit_cast_type (const constexpr_ctx
*ctx
, location_t loc
, tree type
,
3975 if (TREE_CODE (type
) == UNION_TYPE
)
3979 if (type
== orig_type
)
3980 error_at (loc
, "%qs is not a constant expression because %qT is "
3981 "a union type", "__builtin_bit_cast", type
);
3983 error_at (loc
, "%qs is not a constant expression because %qT "
3984 "contains a union type", "__builtin_bit_cast",
3989 if (TREE_CODE (type
) == POINTER_TYPE
)
3993 if (type
== orig_type
)
3994 error_at (loc
, "%qs is not a constant expression because %qT is "
3995 "a pointer type", "__builtin_bit_cast", type
);
3997 error_at (loc
, "%qs is not a constant expression because %qT "
3998 "contains a pointer type", "__builtin_bit_cast",
4003 if (TREE_CODE (type
) == REFERENCE_TYPE
)
4007 if (type
== orig_type
)
4008 error_at (loc
, "%qs is not a constant expression because %qT is "
4009 "a reference type", "__builtin_bit_cast", type
);
4011 error_at (loc
, "%qs is not a constant expression because %qT "
4012 "contains a reference type", "__builtin_bit_cast",
4017 if (TYPE_PTRMEM_P (type
))
4021 if (type
== orig_type
)
4022 error_at (loc
, "%qs is not a constant expression because %qT is "
4023 "a pointer to member type", "__builtin_bit_cast",
4026 error_at (loc
, "%qs is not a constant expression because %qT "
4027 "contains a pointer to member type",
4028 "__builtin_bit_cast", orig_type
);
4032 if (TYPE_VOLATILE (type
))
4036 if (type
== orig_type
)
4037 error_at (loc
, "%qs is not a constant expression because %qT is "
4038 "volatile", "__builtin_bit_cast", type
);
4040 error_at (loc
, "%qs is not a constant expression because %qT "
4041 "contains a volatile subobject",
4042 "__builtin_bit_cast", orig_type
);
4046 if (TREE_CODE (type
) == RECORD_TYPE
)
4047 for (tree field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
4048 if (TREE_CODE (field
) == FIELD_DECL
4049 && check_bit_cast_type (ctx
, loc
, TREE_TYPE (field
), orig_type
))
4054 /* Subroutine of cxx_eval_constant_expression.
4055 Attempt to evaluate a BIT_CAST_EXPR. */
4058 cxx_eval_bit_cast (const constexpr_ctx
*ctx
, tree t
, bool *non_constant_p
,
4061 if (check_bit_cast_type (ctx
, EXPR_LOCATION (t
), TREE_TYPE (t
),
4063 || check_bit_cast_type (ctx
, cp_expr_loc_or_loc (TREE_OPERAND (t
, 0),
4065 TREE_TYPE (TREE_OPERAND (t
, 0)),
4066 TREE_TYPE (TREE_OPERAND (t
, 0))))
4068 *non_constant_p
= true;
4072 tree op
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), false,
4073 non_constant_p
, overflow_p
);
4074 if (*non_constant_p
)
4077 location_t loc
= EXPR_LOCATION (t
);
4078 if (BITS_PER_UNIT
!= 8 || CHAR_BIT
!= 8)
4081 sorry_at (loc
, "%qs cannot be constant evaluated on the target",
4082 "__builtin_bit_cast");
4083 *non_constant_p
= true;
4087 if (!tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (t
))))
4090 sorry_at (loc
, "%qs cannot be constant evaluated because the "
4091 "type is too large", "__builtin_bit_cast");
4092 *non_constant_p
= true;
4096 HOST_WIDE_INT len
= tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (t
)));
4097 if (len
< 0 || (int) len
!= len
)
4100 sorry_at (loc
, "%qs cannot be constant evaluated because the "
4101 "type is too large", "__builtin_bit_cast");
4102 *non_constant_p
= true;
4106 unsigned char buf
[64];
4107 unsigned char *ptr
, *mask
;
4108 size_t alen
= (size_t) len
* 2;
4109 if (alen
<= sizeof (buf
))
4112 ptr
= XNEWVEC (unsigned char, alen
);
4113 mask
= ptr
+ (size_t) len
;
4114 /* At the beginning consider everything indeterminate. */
4115 memset (mask
, ~0, (size_t) len
);
4117 if (native_encode_initializer (op
, ptr
, len
, 0, mask
) != len
)
4120 sorry_at (loc
, "%qs cannot be constant evaluated because the "
4121 "argument cannot be encoded", "__builtin_bit_cast");
4122 *non_constant_p
= true;
4129 if (can_native_interpret_type_p (TREE_TYPE (t
)))
4130 r
= native_interpret_expr (TREE_TYPE (t
), ptr
, len
);
4131 else if (TREE_CODE (TREE_TYPE (t
)) == RECORD_TYPE
)
4133 r
= native_interpret_aggregate (TREE_TYPE (t
), ptr
, 0, len
);
4135 clear_type_padding_in_mask (TREE_TYPE (t
), mask
);
4140 for (int i
= 0; i
< len
; i
++)
4144 error_at (loc
, "%qs accessing uninitialized byte at offset %d",
4145 "__builtin_bit_cast", i
);
4146 *non_constant_p
= true;
4156 sorry_at (loc
, "%qs cannot be constant evaluated because the "
4157 "argument cannot be interpreted", "__builtin_bit_cast");
4158 *non_constant_p
= true;
4164 /* Subroutine of cxx_eval_constant_expression.
4165 Evaluate a short-circuited logical expression T in the context
4166 of a given constexpr CALL. BAILOUT_VALUE is the value for
4167 early return. CONTINUE_VALUE is used here purely for
4168 sanity check purposes. */
4171 cxx_eval_logical_expression (const constexpr_ctx
*ctx
, tree t
,
4172 tree bailout_value
, tree continue_value
,
4174 bool *non_constant_p
, bool *overflow_p
)
4177 tree lhs
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
4179 non_constant_p
, overflow_p
);
4180 VERIFY_CONSTANT (lhs
);
4181 if (tree_int_cst_equal (lhs
, bailout_value
))
4183 gcc_assert (tree_int_cst_equal (lhs
, continue_value
));
4184 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
4185 lval
, non_constant_p
,
4187 VERIFY_CONSTANT (r
);
4191 /* REF is a COMPONENT_REF designating a particular field. V is a vector of
4192 CONSTRUCTOR elements to initialize (part of) an object containing that
4193 field. Return a pointer to the constructor_elt corresponding to the
4194 initialization of the field. */
4196 static constructor_elt
*
4197 base_field_constructor_elt (vec
<constructor_elt
, va_gc
> *v
, tree ref
)
4199 tree aggr
= TREE_OPERAND (ref
, 0);
4200 tree field
= TREE_OPERAND (ref
, 1);
4202 constructor_elt
*ce
;
4204 gcc_assert (TREE_CODE (ref
) == COMPONENT_REF
);
4206 if (TREE_CODE (aggr
) == COMPONENT_REF
)
4208 constructor_elt
*base_ce
4209 = base_field_constructor_elt (v
, aggr
);
4210 v
= CONSTRUCTOR_ELTS (base_ce
->value
);
4213 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
4214 if (ce
->index
== field
)
4221 /* Some of the expressions fed to the constexpr mechanism are calls to
4222 constructors, which have type void. In that case, return the type being
4223 initialized by the constructor. */
4226 initialized_type (tree t
)
4230 tree type
= TREE_TYPE (t
);
4231 if (TREE_CODE (t
) == CALL_EXPR
)
4233 /* A constructor call has void type, so we need to look deeper. */
4234 tree fn
= get_function_named_in_call (t
);
4235 if (fn
&& TREE_CODE (fn
) == FUNCTION_DECL
4236 && DECL_CXX_CONSTRUCTOR_P (fn
))
4237 type
= DECL_CONTEXT (fn
);
4239 else if (TREE_CODE (t
) == COMPOUND_EXPR
)
4240 return initialized_type (TREE_OPERAND (t
, 1));
4241 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
)
4242 type
= TREE_TYPE (AGGR_INIT_EXPR_SLOT (t
));
4243 return cv_unqualified (type
);
4246 /* We're about to initialize element INDEX of an array or class from VALUE.
4247 Set up NEW_CTX appropriately by adjusting .object to refer to the
4248 subobject and creating a new CONSTRUCTOR if the element is itself
4249 a class or array. */
4252 init_subob_ctx (const constexpr_ctx
*ctx
, constexpr_ctx
&new_ctx
,
4253 tree index
, tree
&value
)
4257 if (index
&& TREE_CODE (index
) != INTEGER_CST
4258 && TREE_CODE (index
) != FIELD_DECL
4259 && TREE_CODE (index
) != RANGE_EXPR
)
4260 /* This won't have an element in the new CONSTRUCTOR. */
4263 tree type
= initialized_type (value
);
4264 if (!AGGREGATE_TYPE_P (type
) && !VECTOR_TYPE_P (type
))
4265 /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
4268 /* The sub-aggregate initializer might contain a placeholder;
4269 update object to refer to the subobject and ctor to refer to
4270 the (newly created) sub-initializer. */
4273 if (index
== NULL_TREE
|| TREE_CODE (index
) == RANGE_EXPR
)
4274 /* There's no well-defined subobject for this index. */
4275 new_ctx
.object
= NULL_TREE
;
4277 new_ctx
.object
= build_ctor_subob_ref (index
, type
, ctx
->object
);
4279 tree elt
= build_constructor (type
, NULL
);
4280 CONSTRUCTOR_NO_CLEARING (elt
) = true;
4283 if (TREE_CODE (value
) == TARGET_EXPR
)
4284 /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
4285 value
= TARGET_EXPR_INITIAL (value
);
4288 /* We're about to process an initializer for a class or array TYPE. Make
4289 sure that CTX is set up appropriately. */
4292 verify_ctor_sanity (const constexpr_ctx
*ctx
, tree type
)
4294 /* We don't bother building a ctor for an empty base subobject. */
4295 if (is_empty_class (type
))
4298 /* We're in the middle of an initializer that might involve placeholders;
4299 our caller should have created a CONSTRUCTOR for us to put the
4300 initializer into. We will either return that constructor or T. */
4301 gcc_assert (ctx
->ctor
);
4302 gcc_assert (same_type_ignoring_top_level_qualifiers_p
4303 (type
, TREE_TYPE (ctx
->ctor
)));
4304 /* We used to check that ctx->ctor was empty, but that isn't the case when
4305 the object is zero-initialized before calling the constructor. */
4308 tree otype
= TREE_TYPE (ctx
->object
);
4309 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type
, otype
)
4310 /* Handle flexible array members. */
4311 || (TREE_CODE (otype
) == ARRAY_TYPE
4312 && TYPE_DOMAIN (otype
) == NULL_TREE
4313 && TREE_CODE (type
) == ARRAY_TYPE
4314 && (same_type_ignoring_top_level_qualifiers_p
4315 (TREE_TYPE (type
), TREE_TYPE (otype
)))));
4317 gcc_assert (!ctx
->object
|| !DECL_P (ctx
->object
)
4318 || *(ctx
->global
->values
.get (ctx
->object
)) == ctx
->ctor
);
4321 /* Subroutine of cxx_eval_constant_expression.
4322 The expression tree T denotes a C-style array or a C-style
4323 aggregate. Reduce it to a constant expression. */
4326 cxx_eval_bare_aggregate (const constexpr_ctx
*ctx
, tree t
,
4328 bool *non_constant_p
, bool *overflow_p
)
4330 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (t
);
4331 bool changed
= false;
4332 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t
));
4333 tree type
= TREE_TYPE (t
);
4335 constexpr_ctx new_ctx
;
4336 if (TYPE_PTRMEMFUNC_P (type
) || VECTOR_TYPE_P (type
))
4338 /* We don't really need the ctx->ctor business for a PMF or
4339 vector, but it's simpler to use the same code. */
4341 new_ctx
.ctor
= build_constructor (type
, NULL
);
4342 new_ctx
.object
= NULL_TREE
;
4345 verify_ctor_sanity (ctx
, type
);
4346 vec
<constructor_elt
, va_gc
> **p
= &CONSTRUCTOR_ELTS (ctx
->ctor
);
4347 vec_alloc (*p
, vec_safe_length (v
));
4349 if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (t
))
4350 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx
->ctor
) = 1;
4354 bool constant_p
= true;
4355 bool side_effects_p
= false;
4356 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, index
, value
)
4358 tree orig_value
= value
;
4359 init_subob_ctx (ctx
, new_ctx
, index
, value
);
4361 if (new_ctx
.ctor
!= ctx
->ctor
)
4363 /* If we built a new CONSTRUCTOR, attach it now so that other
4364 initializers can refer to it. */
4365 constructor_elt
*cep
= get_or_insert_ctor_field (ctx
->ctor
, index
);
4366 cep
->value
= new_ctx
.ctor
;
4367 pos_hint
= cep
- (*p
)->begin();
4369 else if (TREE_CODE (type
) == UNION_TYPE
)
4370 /* Otherwise if we're constructing a non-aggregate union member, set
4371 the active union member now so that we can later detect and diagnose
4372 if its initializer attempts to activate another member. */
4373 get_or_insert_ctor_field (ctx
->ctor
, index
);
4374 tree elt
= cxx_eval_constant_expression (&new_ctx
, value
,
4376 non_constant_p
, overflow_p
);
4377 /* Don't VERIFY_CONSTANT here. */
4378 if (ctx
->quiet
&& *non_constant_p
)
4380 if (elt
!= orig_value
)
4383 if (!TREE_CONSTANT (elt
))
4385 if (TREE_SIDE_EFFECTS (elt
))
4386 side_effects_p
= true;
4387 if (index
&& TREE_CODE (index
) == COMPONENT_REF
)
4389 /* This is an initialization of a vfield inside a base
4390 subaggregate that we already initialized; push this
4391 initialization into the previous initialization. */
4392 constructor_elt
*inner
= base_field_constructor_elt (*p
, index
);
4397 && (TREE_CODE (index
) == NOP_EXPR
4398 || TREE_CODE (index
) == POINTER_PLUS_EXPR
))
4400 /* This is an initializer for an empty base; now that we've
4401 checked that it's constant, we can ignore it. */
4402 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index
))));
4407 if (TREE_CODE (type
) == UNION_TYPE
4408 && (*p
)->last().index
!= index
)
4409 /* The initializer erroneously changed the active union member that
4410 we're initializing. */
4411 gcc_assert (*non_constant_p
);
4414 /* The initializer might have mutated the underlying CONSTRUCTOR,
4415 so recompute the location of the target constructer_elt. */
4416 constructor_elt
*cep
4417 = get_or_insert_ctor_field (ctx
->ctor
, index
, pos_hint
);
4421 /* Adding or replacing an element might change the ctor's flags. */
4422 TREE_CONSTANT (ctx
->ctor
) = constant_p
;
4423 TREE_SIDE_EFFECTS (ctx
->ctor
) = side_effects_p
;
4426 if (*non_constant_p
|| !changed
)
4429 /* We're done building this CONSTRUCTOR, so now we can interpret an
4430 element without an explicit initializer as value-initialized. */
4431 CONSTRUCTOR_NO_CLEARING (t
) = false;
4432 TREE_CONSTANT (t
) = constant_p
;
4433 TREE_SIDE_EFFECTS (t
) = side_effects_p
;
4434 if (VECTOR_TYPE_P (type
))
4439 /* Subroutine of cxx_eval_constant_expression.
4440 The expression tree T is a VEC_INIT_EXPR which denotes the desired
4441 initialization of a non-static data member of array type. Reduce it to a
4444 Note that apart from value-initialization (when VALUE_INIT is true),
4445 this is only intended to support value-initialization and the
4446 initializations done by defaulted constructors for classes with
4447 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
4448 will either be NULL_TREE for the default constructor, or a COMPONENT_REF
4449 for the copy/move constructor. */
4452 cxx_eval_vec_init_1 (const constexpr_ctx
*ctx
, tree atype
, tree init
,
4453 bool value_init
, bool lval
,
4454 bool *non_constant_p
, bool *overflow_p
)
4456 tree elttype
= TREE_TYPE (atype
);
4457 verify_ctor_sanity (ctx
, atype
);
4458 vec
<constructor_elt
, va_gc
> **p
= &CONSTRUCTOR_ELTS (ctx
->ctor
);
4459 bool pre_init
= false;
4460 unsigned HOST_WIDE_INT i
;
4461 tsubst_flags_t complain
= ctx
->quiet
? tf_none
: tf_warning_or_error
;
4463 if (init
&& TREE_CODE (init
) == CONSTRUCTOR
)
4464 return cxx_eval_bare_aggregate (ctx
, init
, lval
,
4465 non_constant_p
, overflow_p
);
4467 /* For the default constructor, build up a call to the default
4468 constructor of the element type. We only need to handle class types
4469 here, as for a constructor to be constexpr, all members must be
4470 initialized, which for a defaulted default constructor means they must
4471 be of a class type with a constexpr default constructor. */
4472 if (TREE_CODE (elttype
) == ARRAY_TYPE
)
4473 /* We only do this at the lowest level. */;
4474 else if (value_init
)
4476 init
= build_value_init (elttype
, complain
);
4481 releasing_vec argvec
;
4482 init
= build_special_member_call (NULL_TREE
, complete_ctor_identifier
,
4483 &argvec
, elttype
, LOOKUP_NORMAL
,
4485 init
= build_aggr_init_expr (elttype
, init
);
4489 bool zeroed_out
= false;
4490 if (!CONSTRUCTOR_NO_CLEARING (ctx
->ctor
))
4492 /* We're initializing an array object that had been zero-initialized
4493 earlier. Truncate ctx->ctor, and propagate its zeroed state by
4494 clearing CONSTRUCTOR_NO_CLEARING on each of the aggregate element
4495 initializers we append to it. */
4496 gcc_checking_assert (initializer_zerop (ctx
->ctor
));
4498 vec_safe_truncate (*p
, 0);
4501 tree nelts
= get_array_or_vector_nelts (ctx
, atype
, non_constant_p
,
4503 unsigned HOST_WIDE_INT max
= tree_to_uhwi (nelts
);
4504 for (i
= 0; i
< max
; ++i
)
4506 tree idx
= build_int_cst (size_type_node
, i
);
4509 constexpr_ctx new_ctx
;
4510 init_subob_ctx (ctx
, new_ctx
, idx
, pre_init
? init
: elttype
);
4511 if (new_ctx
.ctor
!= ctx
->ctor
)
4514 CONSTRUCTOR_NO_CLEARING (new_ctx
.ctor
) = false;
4515 CONSTRUCTOR_APPEND_ELT (*p
, idx
, new_ctx
.ctor
);
4517 if (TREE_CODE (elttype
) == ARRAY_TYPE
)
4519 /* A multidimensional array; recurse. */
4520 if (value_init
|| init
== NULL_TREE
)
4522 eltinit
= NULL_TREE
;
4526 eltinit
= cp_build_array_ref (input_location
, init
, idx
, complain
);
4527 eltinit
= cxx_eval_vec_init_1 (&new_ctx
, elttype
, eltinit
, value_init
,
4529 non_constant_p
, overflow_p
);
4533 /* Initializing an element using value or default initialization
4534 we just pre-built above. */
4535 if (init
== void_node
)
4536 /* Trivial default-init, don't do anything to the CONSTRUCTOR. */
4538 eltinit
= cxx_eval_constant_expression (&new_ctx
, init
, lval
,
4539 non_constant_p
, overflow_p
);
4544 /* Copying an element. */
4545 gcc_assert (same_type_ignoring_top_level_qualifiers_p
4546 (atype
, TREE_TYPE (init
)));
4547 eltinit
= cp_build_array_ref (input_location
, init
, idx
, complain
);
4548 if (!lvalue_p (init
))
4549 eltinit
= move (eltinit
);
4550 eltinit
= force_rvalue (eltinit
, complain
);
4551 eltinit
= cxx_eval_constant_expression (&new_ctx
, eltinit
, lval
,
4552 non_constant_p
, overflow_p
);
4554 if (*non_constant_p
)
4556 if (new_ctx
.ctor
!= ctx
->ctor
)
4558 /* We appended this element above; update the value. */
4559 gcc_assert ((*p
)->last().index
== idx
);
4560 (*p
)->last().value
= eltinit
;
4563 CONSTRUCTOR_APPEND_ELT (*p
, idx
, eltinit
);
4564 /* Reuse the result of cxx_eval_constant_expression call
4565 from the first iteration to all others if it is a constant
4566 initializer that doesn't require relocations. */
4569 && (eltinit
== NULL_TREE
4570 || (initializer_constant_valid_p (eltinit
, TREE_TYPE (eltinit
))
4571 == null_pointer_node
)))
4573 if (new_ctx
.ctor
!= ctx
->ctor
)
4574 eltinit
= new_ctx
.ctor
;
4575 tree range
= build2 (RANGE_EXPR
, size_type_node
,
4576 build_int_cst (size_type_node
, 1),
4577 build_int_cst (size_type_node
, max
- 1));
4578 CONSTRUCTOR_APPEND_ELT (*p
, range
, unshare_constructor (eltinit
));
4582 vec_safe_reserve (*p
, max
);
4585 if (!*non_constant_p
)
4588 CONSTRUCTOR_NO_CLEARING (init
) = false;
4594 cxx_eval_vec_init (const constexpr_ctx
*ctx
, tree t
,
4596 bool *non_constant_p
, bool *overflow_p
)
4598 tree atype
= TREE_TYPE (t
);
4599 tree init
= VEC_INIT_EXPR_INIT (t
);
4600 tree r
= cxx_eval_vec_init_1 (ctx
, atype
, init
,
4601 VEC_INIT_EXPR_VALUE_INIT (t
),
4602 lval
, non_constant_p
, overflow_p
);
4603 if (*non_constant_p
)
4609 /* Like same_type_ignoring_top_level_qualifiers_p, but also handle the case
4610 where the desired type is an array of unknown bounds because the variable
4611 has had its bounds deduced since the wrapping expression was created. */
4614 same_type_ignoring_tlq_and_bounds_p (tree type1
, tree type2
)
4616 while (TREE_CODE (type1
) == ARRAY_TYPE
4617 && TREE_CODE (type2
) == ARRAY_TYPE
4618 && (!TYPE_DOMAIN (type1
) || !TYPE_DOMAIN (type2
)))
4620 type1
= TREE_TYPE (type1
);
4621 type2
= TREE_TYPE (type2
);
4623 return same_type_ignoring_top_level_qualifiers_p (type1
, type2
);
4626 /* Try to determine the currently active union member for an expression
4627 with UNION_TYPE. If it can be determined, return the FIELD_DECL,
4628 otherwise return NULL_TREE. */
4631 cxx_union_active_member (const constexpr_ctx
*ctx
, tree t
)
4633 constexpr_ctx new_ctx
= *ctx
;
4634 new_ctx
.quiet
= true;
4635 bool non_constant_p
= false, overflow_p
= false;
4636 tree ctor
= cxx_eval_constant_expression (&new_ctx
, t
, false,
4639 if (TREE_CODE (ctor
) == CONSTRUCTOR
4640 && CONSTRUCTOR_NELTS (ctor
) == 1
4641 && CONSTRUCTOR_ELT (ctor
, 0)->index
4642 && TREE_CODE (CONSTRUCTOR_ELT (ctor
, 0)->index
) == FIELD_DECL
)
4643 return CONSTRUCTOR_ELT (ctor
, 0)->index
;
4647 /* Helper function for cxx_fold_indirect_ref_1, called recursively. */
4650 cxx_fold_indirect_ref_1 (const constexpr_ctx
*ctx
, location_t loc
, tree type
,
4651 tree op
, unsigned HOST_WIDE_INT off
, bool *empty_base
)
4653 tree optype
= TREE_TYPE (op
);
4654 unsigned HOST_WIDE_INT const_nunits
;
4657 if (similar_type_p (optype
, type
))
4659 /* Also handle conversion to an empty base class, which
4660 is represented with a NOP_EXPR. */
4661 /* *(foo *)&complexfoo => __real__ complexfoo */
4662 else if (TREE_CODE (optype
) == COMPLEX_TYPE
4663 && similar_type_p (type
, TREE_TYPE (optype
)))
4664 return build1_loc (loc
, REALPART_EXPR
, type
, op
);
4666 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
4667 else if (TREE_CODE (optype
) == COMPLEX_TYPE
4668 && similar_type_p (type
, TREE_TYPE (optype
))
4669 && tree_to_uhwi (TYPE_SIZE_UNIT (type
)) == off
)
4670 return build1_loc (loc
, IMAGPART_EXPR
, type
, op
);
4671 if (is_empty_class (type
)
4672 && CLASS_TYPE_P (optype
)
4673 && DERIVED_FROM_P (type
, optype
))
4678 /* ((foo*)&vectorfoo)[x] => BIT_FIELD_REF<vectorfoo,...> */
4679 else if (VECTOR_TYPE_P (optype
)
4680 && similar_type_p (type
, TREE_TYPE (optype
))
4681 && TYPE_VECTOR_SUBPARTS (optype
).is_constant (&const_nunits
))
4683 unsigned HOST_WIDE_INT part_width
= tree_to_uhwi (TYPE_SIZE_UNIT (type
));
4684 unsigned HOST_WIDE_INT max_offset
= part_width
* const_nunits
;
4685 if (off
< max_offset
&& off
% part_width
== 0)
4687 tree index
= bitsize_int (off
* BITS_PER_UNIT
);
4688 return build3_loc (loc
, BIT_FIELD_REF
, type
, op
,
4689 TYPE_SIZE (type
), index
);
4692 /* ((foo *)&fooarray)[x] => fooarray[x] */
4693 else if (TREE_CODE (optype
) == ARRAY_TYPE
4694 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (optype
)))
4695 && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (optype
))))
4697 tree type_domain
= TYPE_DOMAIN (optype
);
4698 tree min_val
= size_zero_node
;
4699 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
4700 min_val
= TYPE_MIN_VALUE (type_domain
);
4701 unsigned HOST_WIDE_INT el_sz
4702 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (optype
)));
4703 unsigned HOST_WIDE_INT idx
= off
/ el_sz
;
4704 unsigned HOST_WIDE_INT rem
= off
% el_sz
;
4705 if (tree_fits_uhwi_p (min_val
))
4707 tree index
= size_int (idx
+ tree_to_uhwi (min_val
));
4708 op
= build4_loc (loc
, ARRAY_REF
, TREE_TYPE (optype
), op
, index
,
4709 NULL_TREE
, NULL_TREE
);
4710 return cxx_fold_indirect_ref_1 (ctx
, loc
, type
, op
, rem
,
4714 /* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */
4715 else if (TREE_CODE (optype
) == RECORD_TYPE
4716 || TREE_CODE (optype
) == UNION_TYPE
)
4718 if (TREE_CODE (optype
) == UNION_TYPE
)
4719 /* For unions prefer the currently active member. */
4720 if (tree field
= cxx_union_active_member (ctx
, op
))
4722 unsigned HOST_WIDE_INT el_sz
4723 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field
)));
4726 tree cop
= build3 (COMPONENT_REF
, TREE_TYPE (field
),
4727 op
, field
, NULL_TREE
);
4728 if (tree ret
= cxx_fold_indirect_ref_1 (ctx
, loc
, type
, cop
,
4733 for (tree field
= TYPE_FIELDS (optype
);
4734 field
; field
= DECL_CHAIN (field
))
4735 if (TREE_CODE (field
) == FIELD_DECL
4736 && TREE_TYPE (field
) != error_mark_node
4737 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (field
))))
4739 tree pos
= byte_position (field
);
4740 if (!tree_fits_uhwi_p (pos
))
4742 unsigned HOST_WIDE_INT upos
= tree_to_uhwi (pos
);
4743 unsigned HOST_WIDE_INT el_sz
4744 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field
)));
4745 if (upos
<= off
&& off
< upos
+ el_sz
)
4747 tree cop
= build3 (COMPONENT_REF
, TREE_TYPE (field
),
4748 op
, field
, NULL_TREE
);
4749 if (tree ret
= cxx_fold_indirect_ref_1 (ctx
, loc
, type
, cop
,
4760 /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
4761 match. We want to be less strict for simple *& folding; if we have a
4762 non-const temporary that we access through a const pointer, that should
4763 work. We handle this here rather than change fold_indirect_ref_1
4764 because we're dealing with things like ADDR_EXPR of INTEGER_CST which
4765 don't really make sense outside of constant expression evaluation. Also
4766 we want to allow folding to COMPONENT_REF, which could cause trouble
4767 with TBAA in fold_indirect_ref_1. */
4770 cxx_fold_indirect_ref (const constexpr_ctx
*ctx
, location_t loc
, tree type
,
4771 tree op0
, bool *empty_base
)
4775 poly_uint64 const_op01
;
4777 /* STRIP_NOPS, but stop if REINTERPRET_CAST_P. */
4778 while (CONVERT_EXPR_P (sub
) || TREE_CODE (sub
) == NON_LVALUE_EXPR
4779 || TREE_CODE (sub
) == VIEW_CONVERT_EXPR
)
4781 if (TREE_CODE (sub
) == NOP_EXPR
4782 && REINTERPRET_CAST_P (sub
))
4784 sub
= TREE_OPERAND (sub
, 0);
4787 subtype
= TREE_TYPE (sub
);
4788 if (!INDIRECT_TYPE_P (subtype
))
4791 if (TREE_CODE (sub
) == ADDR_EXPR
)
4793 tree op
= TREE_OPERAND (sub
, 0);
4794 tree optype
= TREE_TYPE (op
);
4796 /* *&CONST_DECL -> to the value of the const decl. */
4797 if (TREE_CODE (op
) == CONST_DECL
)
4798 return DECL_INITIAL (op
);
4799 /* *&p => p; make sure to handle *&"str"[cst] here. */
4800 if (similar_type_p (optype
, type
))
4802 tree fop
= fold_read_from_constant_string (op
);
4809 return cxx_fold_indirect_ref_1 (ctx
, loc
, type
, op
, 0, empty_base
);
4811 else if (TREE_CODE (sub
) == POINTER_PLUS_EXPR
4812 && tree_fits_uhwi_p (TREE_OPERAND (sub
, 1)))
4814 tree op00
= TREE_OPERAND (sub
, 0);
4815 tree op01
= TREE_OPERAND (sub
, 1);
4818 if (TREE_CODE (op00
) == ADDR_EXPR
)
4819 return cxx_fold_indirect_ref_1 (ctx
, loc
, type
, TREE_OPERAND (op00
, 0),
4820 tree_to_uhwi (op01
), empty_base
);
4822 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
4823 else if (TREE_CODE (TREE_TYPE (subtype
)) == ARRAY_TYPE
4824 && similar_type_p (type
, TREE_TYPE (TREE_TYPE (subtype
))))
4827 tree min_val
= size_zero_node
;
4829 = cxx_fold_indirect_ref (ctx
, loc
, TREE_TYPE (subtype
), sub
, NULL
);
4833 sub
= build1_loc (loc
, INDIRECT_REF
, TREE_TYPE (subtype
), sub
);
4834 type_domain
= TYPE_DOMAIN (TREE_TYPE (sub
));
4835 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
4836 min_val
= TYPE_MIN_VALUE (type_domain
);
4837 return build4_loc (loc
, ARRAY_REF
, type
, sub
, min_val
, NULL_TREE
,
4845 cxx_eval_indirect_ref (const constexpr_ctx
*ctx
, tree t
,
4847 bool *non_constant_p
, bool *overflow_p
)
4849 tree orig_op0
= TREE_OPERAND (t
, 0);
4850 bool empty_base
= false;
4852 /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second
4853 operand is an integer-zero. Otherwise reject the MEM_REF for now. */
4855 if (TREE_CODE (t
) == MEM_REF
4856 && (!TREE_OPERAND (t
, 1) || !integer_zerop (TREE_OPERAND (t
, 1))))
4858 gcc_assert (ctx
->quiet
);
4859 *non_constant_p
= true;
4863 /* First try to simplify it directly. */
4864 tree r
= cxx_fold_indirect_ref (ctx
, EXPR_LOCATION (t
), TREE_TYPE (t
),
4865 orig_op0
, &empty_base
);
4868 /* If that didn't work, evaluate the operand first. */
4869 tree op0
= cxx_eval_constant_expression (ctx
, orig_op0
,
4870 /*lval*/false, non_constant_p
,
4872 /* Don't VERIFY_CONSTANT here. */
4873 if (*non_constant_p
)
4876 if (!lval
&& integer_zerop (op0
))
4879 error ("dereferencing a null pointer");
4880 *non_constant_p
= true;
4884 r
= cxx_fold_indirect_ref (ctx
, EXPR_LOCATION (t
), TREE_TYPE (t
), op0
,
4888 /* We couldn't fold to a constant value. Make sure it's not
4889 something we should have been able to fold. */
4892 if (TREE_CODE (sub
) == ADDR_EXPR
)
4894 gcc_assert (!similar_type_p
4895 (TREE_TYPE (TREE_TYPE (sub
)), TREE_TYPE (t
)));
4896 /* DR 1188 says we don't have to deal with this. */
4898 error_at (cp_expr_loc_or_input_loc (t
),
4899 "accessing value of %qE through a %qT glvalue in a "
4900 "constant expression", build_fold_indirect_ref (sub
),
4902 *non_constant_p
= true;
4906 if (lval
&& op0
!= orig_op0
)
4907 return build1 (INDIRECT_REF
, TREE_TYPE (t
), op0
);
4909 VERIFY_CONSTANT (t
);
4914 r
= cxx_eval_constant_expression (ctx
, r
,
4915 lval
, non_constant_p
, overflow_p
);
4916 if (*non_constant_p
)
4919 /* If we're pulling out the value of an empty base, just return an empty
4921 if (empty_base
&& !lval
)
4923 r
= build_constructor (TREE_TYPE (t
), NULL
);
4924 TREE_CONSTANT (r
) = true;
4930 /* Complain about R, a VAR_DECL, not being usable in a constant expression.
4931 Shared between potential_constant_expression and
4932 cxx_eval_constant_expression. */
4935 non_const_var_error (location_t loc
, tree r
)
4937 auto_diagnostic_group d
;
4938 tree type
= TREE_TYPE (r
);
4939 if (DECL_NAME (r
) == heap_uninit_identifier
4940 || DECL_NAME (r
) == heap_identifier
4941 || DECL_NAME (r
) == heap_vec_uninit_identifier
4942 || DECL_NAME (r
) == heap_vec_identifier
)
4944 error_at (loc
, "the content of uninitialized storage is not usable "
4945 "in a constant expression");
4946 inform (DECL_SOURCE_LOCATION (r
), "allocated here");
4949 if (DECL_NAME (r
) == heap_deleted_identifier
)
4951 error_at (loc
, "use of allocated storage after deallocation in a "
4952 "constant expression");
4953 inform (DECL_SOURCE_LOCATION (r
), "allocated here");
4956 error_at (loc
, "the value of %qD is not usable in a constant "
4958 /* Avoid error cascade. */
4959 if (DECL_INITIAL (r
) == error_mark_node
)
4961 if (DECL_DECLARED_CONSTEXPR_P (r
))
4962 inform (DECL_SOURCE_LOCATION (r
),
4963 "%qD used in its own initializer", r
);
4964 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
4966 if (!CP_TYPE_CONST_P (type
))
4967 inform (DECL_SOURCE_LOCATION (r
),
4968 "%q#D is not const", r
);
4969 else if (CP_TYPE_VOLATILE_P (type
))
4970 inform (DECL_SOURCE_LOCATION (r
),
4971 "%q#D is volatile", r
);
4972 else if (!DECL_INITIAL (r
)
4973 || !TREE_CONSTANT (DECL_INITIAL (r
))
4974 || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
))
4975 inform (DECL_SOURCE_LOCATION (r
),
4976 "%qD was not initialized with a constant "
4981 else if (TYPE_REF_P (type
))
4982 inform (DECL_SOURCE_LOCATION (r
),
4983 "%qD was not initialized with a constant "
4987 if (cxx_dialect
>= cxx11
&& !DECL_DECLARED_CONSTEXPR_P (r
))
4988 inform (DECL_SOURCE_LOCATION (r
),
4989 "%qD was not declared %<constexpr%>", r
);
4991 inform (DECL_SOURCE_LOCATION (r
),
4992 "%qD does not have integral or enumeration type",
4997 /* Subroutine of cxx_eval_constant_expression.
4998 Like cxx_eval_unary_expression, except for trinary expressions. */
5001 cxx_eval_trinary_expression (const constexpr_ctx
*ctx
, tree t
,
5003 bool *non_constant_p
, bool *overflow_p
)
5009 for (i
= 0; i
< 3; i
++)
5011 args
[i
] = cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, i
),
5013 non_constant_p
, overflow_p
);
5014 VERIFY_CONSTANT (args
[i
]);
5017 val
= fold_ternary_loc (EXPR_LOCATION (t
), TREE_CODE (t
), TREE_TYPE (t
),
5018 args
[0], args
[1], args
[2]);
5019 if (val
== NULL_TREE
)
5021 VERIFY_CONSTANT (val
);
5025 /* True if T was declared in a function declared to be constexpr, and
5026 therefore potentially constant in C++14. */
5029 var_in_constexpr_fn (tree t
)
5031 tree ctx
= DECL_CONTEXT (t
);
5032 return (ctx
&& TREE_CODE (ctx
) == FUNCTION_DECL
5033 && DECL_DECLARED_CONSTEXPR_P (ctx
));
5036 /* True if T was declared in a function that might be constexpr: either a
5037 function that was declared constexpr, or a C++17 lambda op(). */
5040 var_in_maybe_constexpr_fn (tree t
)
5042 if (cxx_dialect
>= cxx17
5043 && DECL_FUNCTION_SCOPE_P (t
)
5044 && LAMBDA_FUNCTION_P (DECL_CONTEXT (t
)))
5046 return var_in_constexpr_fn (t
);
5049 /* We're assigning INIT to TARGET. In do_build_copy_constructor and
5050 build_over_call we implement trivial copy of a class with tail padding using
5051 assignment of character arrays, which is valid in normal code, but not in
5052 constexpr evaluation. We don't need to worry about clobbering tail padding
5053 in constexpr evaluation, so strip the type punning. */
5056 maybe_simplify_trivial_copy (tree
&target
, tree
&init
)
5058 if (TREE_CODE (target
) == MEM_REF
5059 && TREE_CODE (init
) == MEM_REF
5060 && TREE_TYPE (target
) == TREE_TYPE (init
)
5061 && TREE_CODE (TREE_TYPE (target
)) == ARRAY_TYPE
5062 && TREE_TYPE (TREE_TYPE (target
)) == unsigned_char_type_node
)
5064 target
= build_fold_indirect_ref (TREE_OPERAND (target
, 0));
5065 init
= build_fold_indirect_ref (TREE_OPERAND (init
, 0));
5069 /* Returns true if REF, which is a COMPONENT_REF, has any fields
5070 of constant type. This does not check for 'mutable', so the
5071 caller is expected to be mindful of that. */
5074 cref_has_const_field (tree ref
)
5076 while (TREE_CODE (ref
) == COMPONENT_REF
)
5078 if (CP_TYPE_CONST_P (TREE_TYPE (TREE_OPERAND (ref
, 1))))
5080 ref
= TREE_OPERAND (ref
, 0);
5085 /* Return true if we are modifying something that is const during constant
5086 expression evaluation. CODE is the code of the statement, OBJ is the
5087 object in question, MUTABLE_P is true if one of the subobjects were
5088 declared mutable. */
5091 modifying_const_object_p (tree_code code
, tree obj
, bool mutable_p
)
5093 /* If this is initialization, there's no problem. */
5094 if (code
!= MODIFY_EXPR
)
5097 /* [basic.type.qualifier] "A const object is an object of type
5098 const T or a non-mutable subobject of a const object." */
5102 if (TREE_READONLY (obj
))
5105 if (CP_TYPE_CONST_P (TREE_TYPE (obj
)))
5107 /* Although a COMPONENT_REF may have a const type, we should
5108 only consider it modifying a const object when any of the
5109 field components is const. This can happen when using
5110 constructs such as const_cast<const T &>(m), making something
5111 const even though it wasn't declared const. */
5112 if (TREE_CODE (obj
) == COMPONENT_REF
)
5113 return cref_has_const_field (obj
);
5121 /* Evaluate an INIT_EXPR or MODIFY_EXPR. */
5124 cxx_eval_store_expression (const constexpr_ctx
*ctx
, tree t
,
5126 bool *non_constant_p
, bool *overflow_p
)
5128 constexpr_ctx new_ctx
= *ctx
;
5130 tree init
= TREE_OPERAND (t
, 1);
5131 if (TREE_CLOBBER_P (init
))
5132 /* Just ignore clobbers. */
5135 /* First we figure out where we're storing to. */
5136 tree target
= TREE_OPERAND (t
, 0);
5138 maybe_simplify_trivial_copy (target
, init
);
5140 tree type
= TREE_TYPE (target
);
5141 bool preeval
= SCALAR_TYPE_P (type
) || TREE_CODE (t
) == MODIFY_EXPR
;
5144 /* Evaluate the value to be stored without knowing what object it will be
5145 stored in, so that any side-effects happen first. */
5146 if (!SCALAR_TYPE_P (type
))
5147 new_ctx
.ctor
= new_ctx
.object
= NULL_TREE
;
5148 init
= cxx_eval_constant_expression (&new_ctx
, init
, false,
5149 non_constant_p
, overflow_p
);
5150 if (*non_constant_p
)
5154 bool evaluated
= false;
5157 /* If we want to return a reference to the target, we need to evaluate it
5158 as a whole; otherwise, only evaluate the innermost piece to avoid
5159 building up unnecessary *_REFs. */
5160 target
= cxx_eval_constant_expression (ctx
, target
, true,
5161 non_constant_p
, overflow_p
);
5163 if (*non_constant_p
)
5167 /* Find the underlying variable. */
5169 tree object
= NULL_TREE
;
5170 /* If we're modifying a const object, save it. */
5171 tree const_object_being_modified
= NULL_TREE
;
5172 bool mutable_p
= false;
5173 for (tree probe
= target
; object
== NULL_TREE
; )
5175 switch (TREE_CODE (probe
))
5181 tree ob
= TREE_OPERAND (probe
, 0);
5182 tree elt
= TREE_OPERAND (probe
, 1);
5183 if (TREE_CODE (elt
) == FIELD_DECL
&& DECL_MUTABLE_P (elt
))
5185 if (TREE_CODE (probe
) == ARRAY_REF
)
5187 elt
= eval_and_check_array_index (ctx
, probe
, false,
5188 non_constant_p
, overflow_p
);
5189 if (*non_constant_p
)
5192 /* We don't check modifying_const_object_p for ARRAY_REFs. Given
5193 "int a[10]", an ARRAY_REF "a[2]" can be "const int", even though
5194 the array isn't const. Instead, check "a" in the next iteration;
5195 that will detect modifying "const int a[10]". */
5197 && modifying_const_object_p (TREE_CODE (t
), probe
,
5199 && const_object_being_modified
== NULL_TREE
)
5200 const_object_being_modified
= probe
;
5201 vec_safe_push (refs
, elt
);
5202 vec_safe_push (refs
, TREE_TYPE (probe
));
5212 probe
= cxx_eval_constant_expression (ctx
, probe
, true,
5213 non_constant_p
, overflow_p
);
5215 if (*non_constant_p
)
5222 if (modifying_const_object_p (TREE_CODE (t
), object
, mutable_p
)
5223 && const_object_being_modified
== NULL_TREE
)
5224 const_object_being_modified
= object
;
5226 /* And then find/build up our initializer for the path to the subobject
5227 we're initializing. */
5229 if (DECL_P (object
))
5230 valp
= ctx
->global
->values
.get (object
);
5235 /* A constant-expression cannot modify objects from outside the
5236 constant-expression. */
5238 error ("modification of %qE is not a constant expression", object
);
5239 *non_constant_p
= true;
5242 type
= TREE_TYPE (object
);
5243 bool no_zero_init
= true;
5245 releasing_vec ctors
, indexes
;
5246 auto_vec
<int> index_pos_hints
;
5247 bool activated_union_member_p
= false;
5248 while (!refs
->is_empty ())
5250 if (*valp
== NULL_TREE
)
5252 *valp
= build_constructor (type
, NULL
);
5253 CONSTRUCTOR_NO_CLEARING (*valp
) = no_zero_init
;
5255 else if (TREE_CODE (*valp
) == STRING_CST
)
5257 /* An array was initialized with a string constant, and now
5258 we're writing into one of its elements. Explode the
5259 single initialization into a set of element
5261 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
5263 tree string
= *valp
;
5264 tree elt_type
= TREE_TYPE (type
);
5265 unsigned chars_per_elt
= (TYPE_PRECISION (elt_type
)
5266 / TYPE_PRECISION (char_type_node
));
5267 unsigned num_elts
= TREE_STRING_LENGTH (string
) / chars_per_elt
;
5268 tree ary_ctor
= build_constructor (type
, NULL
);
5270 vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor
), num_elts
);
5271 for (unsigned ix
= 0; ix
!= num_elts
; ix
++)
5273 constructor_elt elt
=
5275 build_int_cst (size_type_node
, ix
),
5276 extract_string_elt (string
, chars_per_elt
, ix
)
5278 CONSTRUCTOR_ELTS (ary_ctor
)->quick_push (elt
);
5284 /* If the value of object is already zero-initialized, any new ctors for
5285 subobjects will also be zero-initialized. */
5286 no_zero_init
= CONSTRUCTOR_NO_CLEARING (*valp
);
5288 enum tree_code code
= TREE_CODE (type
);
5290 tree index
= refs
->pop();
5292 if (code
== UNION_TYPE
&& CONSTRUCTOR_NELTS (*valp
)
5293 && CONSTRUCTOR_ELT (*valp
, 0)->index
!= index
)
5295 if (cxx_dialect
< cxx20
)
5298 error_at (cp_expr_loc_or_input_loc (t
),
5299 "change of the active member of a union "
5301 CONSTRUCTOR_ELT (*valp
, 0)->index
,
5303 *non_constant_p
= true;
5305 else if (TREE_CODE (t
) == MODIFY_EXPR
5306 && CONSTRUCTOR_NO_CLEARING (*valp
))
5308 /* Diagnose changing the active union member while the union
5309 is in the process of being initialized. */
5311 error_at (cp_expr_loc_or_input_loc (t
),
5312 "change of the active member of a union "
5313 "from %qD to %qD during initialization",
5314 CONSTRUCTOR_ELT (*valp
, 0)->index
,
5316 *non_constant_p
= true;
5318 no_zero_init
= true;
5321 vec_safe_push (ctors
, *valp
);
5322 vec_safe_push (indexes
, index
);
5324 constructor_elt
*cep
5325 = get_or_insert_ctor_field (*valp
, index
);
5326 index_pos_hints
.safe_push (cep
- CONSTRUCTOR_ELTS (*valp
)->begin());
5328 if (code
== UNION_TYPE
)
5329 activated_union_member_p
= true;
5334 /* Detect modifying a constant object in constexpr evaluation.
5335 We have found a const object that is being modified. Figure out
5336 if we need to issue an error. Consider
5340 constexpr A() : n(1) { n = 2; } // #1
5344 constexpr B() { a.n = 3; } // #2
5348 #1 is OK, since we're modifying an object under construction, but
5349 #2 is wrong, since "a" is const and has been fully constructed.
5350 To track it, we use the TREE_READONLY bit in the object's CONSTRUCTOR
5351 which means that the object is read-only. For the example above, the
5352 *ctors stack at the point of #2 will look like:
5354 ctors[0] = {.a={.n=2}} TREE_READONLY = 0
5355 ctors[1] = {.n=2} TREE_READONLY = 1
5357 and we're modifying "b.a", so we search the stack and see if the
5358 constructor for "b.a" has already run. */
5359 if (const_object_being_modified
)
5363 = strip_array_types (TREE_TYPE (const_object_being_modified
));
5364 if (!CLASS_TYPE_P (const_objtype
))
5368 /* [class.ctor]p5 "A constructor can be invoked for a const,
5369 volatile, or const volatile object. const and volatile
5370 semantics are not applied on an object under construction.
5371 They come into effect when the constructor for the most
5372 derived object ends." */
5375 FOR_EACH_VEC_ELT (*ctors
, i
, elt
)
5376 if (same_type_ignoring_top_level_qualifiers_p
5377 (TREE_TYPE (const_object_being_modified
), TREE_TYPE (elt
)))
5379 fail
= TREE_READONLY (elt
);
5386 modifying_const_object_error (t
, const_object_being_modified
);
5387 *non_constant_p
= true;
5394 /* We're handling an INIT_EXPR of class type, so the value of the
5395 initializer can depend on the object it's initializing. */
5397 /* Create a new CONSTRUCTOR in case evaluation of the initializer
5398 wants to modify it. */
5399 if (*valp
== NULL_TREE
)
5401 *valp
= build_constructor (type
, NULL
);
5402 CONSTRUCTOR_NO_CLEARING (*valp
) = no_zero_init
;
5404 new_ctx
.ctor
= *valp
;
5405 new_ctx
.object
= target
;
5406 /* Avoid temporary materialization when initializing from a TARGET_EXPR.
5407 We don't need to mess with AGGR_EXPR_SLOT/VEC_INIT_EXPR_SLOT because
5408 expansion of those trees uses ctx instead. */
5409 if (TREE_CODE (init
) == TARGET_EXPR
)
5410 if (tree tinit
= TARGET_EXPR_INITIAL (init
))
5412 init
= cxx_eval_constant_expression (&new_ctx
, init
, false,
5413 non_constant_p
, overflow_p
);
5414 /* The hash table might have moved since the get earlier, and the
5415 initializer might have mutated the underlying CONSTRUCTORs, so we must
5417 valp
= ctx
->global
->values
.get (object
);
5418 for (unsigned i
= 0; i
< vec_safe_length (indexes
); i
++)
5420 constructor_elt
*cep
5421 = get_or_insert_ctor_field (*valp
, indexes
[i
], index_pos_hints
[i
]);
5426 /* Don't share a CONSTRUCTOR that might be changed later. */
5427 init
= unshare_constructor (init
);
5429 if (*valp
&& TREE_CODE (*valp
) == CONSTRUCTOR
5430 && TREE_CODE (init
) == CONSTRUCTOR
)
5432 /* An outer ctx->ctor might be pointing to *valp, so replace
5434 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init
),
5437 /* For initialization of an empty base, the original target will be
5438 *(base*)this, evaluation of which resolves to the object
5439 argument, which has the derived type rather than the base type. In
5440 this situation, just evaluate the initializer and return, since
5441 there's no actual data to store. */
5442 gcc_assert (is_empty_class (TREE_TYPE (init
)) && !lval
);
5445 CONSTRUCTOR_ELTS (*valp
) = CONSTRUCTOR_ELTS (init
);
5446 TREE_CONSTANT (*valp
) = TREE_CONSTANT (init
);
5447 TREE_SIDE_EFFECTS (*valp
) = TREE_SIDE_EFFECTS (init
);
5448 CONSTRUCTOR_NO_CLEARING (*valp
)
5449 = CONSTRUCTOR_NO_CLEARING (init
);
5454 /* After initialization, 'const' semantics apply to the value of the
5455 object. Make a note of this fact by marking the CONSTRUCTOR
5457 if (TREE_CODE (t
) == INIT_EXPR
5458 && TREE_CODE (*valp
) == CONSTRUCTOR
5459 && TYPE_READONLY (type
))
5461 if (INDIRECT_REF_P (target
)
5462 && (is_this_parameter
5463 (tree_strip_nop_conversions (TREE_OPERAND (target
, 0)))))
5464 /* We've just initialized '*this' (perhaps via the target
5465 constructor of a delegating constructor). Leave it up to the
5466 caller that set 'this' to set TREE_READONLY appropriately. */
5467 gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
5468 (TREE_TYPE (target
), type
));
5470 TREE_READONLY (*valp
) = true;
5473 /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing
5474 CONSTRUCTORs, if any. */
5477 bool c
= TREE_CONSTANT (init
);
5478 bool s
= TREE_SIDE_EFFECTS (init
);
5479 if (!c
|| s
|| activated_union_member_p
)
5480 FOR_EACH_VEC_ELT (*ctors
, i
, elt
)
5483 TREE_CONSTANT (elt
) = false;
5485 TREE_SIDE_EFFECTS (elt
) = true;
5486 /* Clear CONSTRUCTOR_NO_CLEARING since we've activated a member of
5488 if (TREE_CODE (TREE_TYPE (elt
)) == UNION_TYPE
)
5489 CONSTRUCTOR_NO_CLEARING (elt
) = false;
5492 if (*non_constant_p
)
5500 /* Evaluate a ++ or -- expression. */
5503 cxx_eval_increment_expression (const constexpr_ctx
*ctx
, tree t
,
5505 bool *non_constant_p
, bool *overflow_p
)
5507 enum tree_code code
= TREE_CODE (t
);
5508 tree type
= TREE_TYPE (t
);
5509 tree op
= TREE_OPERAND (t
, 0);
5510 tree offset
= TREE_OPERAND (t
, 1);
5511 gcc_assert (TREE_CONSTANT (offset
));
5513 /* OFFSET is constant, but perhaps not constant enough. We need to
5514 e.g. bash FLOAT_EXPRs to REAL_CSTs. */
5515 offset
= fold_simple (offset
);
5517 /* The operand as an lvalue. */
5518 op
= cxx_eval_constant_expression (ctx
, op
, true,
5519 non_constant_p
, overflow_p
);
5521 /* The operand as an rvalue. */
5523 = cxx_eval_constant_expression (ctx
, op
, false,
5524 non_constant_p
, overflow_p
);
5525 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
5526 a local array in a constexpr function. */
5527 bool ptr
= INDIRECT_TYPE_P (TREE_TYPE (val
));
5529 VERIFY_CONSTANT (val
);
5531 /* The modified value. */
5532 bool inc
= (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
);
5534 if (INDIRECT_TYPE_P (type
))
5536 /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
5537 offset
= convert_to_ptrofftype (offset
);
5539 offset
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (offset
), offset
);
5540 mod
= fold_build2 (POINTER_PLUS_EXPR
, type
, val
, offset
);
5543 mod
= fold_build2 (inc
? PLUS_EXPR
: MINUS_EXPR
, type
, val
, offset
);
5545 VERIFY_CONSTANT (mod
);
5547 /* Storing the modified value. */
5548 tree store
= build2_loc (cp_expr_loc_or_loc (t
, input_location
),
5549 MODIFY_EXPR
, type
, op
, mod
);
5550 cxx_eval_constant_expression (ctx
, store
,
5551 true, non_constant_p
, overflow_p
);
5554 /* And the value of the expression. */
5555 if (code
== PREINCREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
5557 /* Prefix ops are lvalues. */
5561 /* But we optimize when the caller wants an rvalue. */
5565 /* Postfix ops are rvalues. */
5569 /* Predicates for the meaning of *jump_target. */
5572 returns (tree
*jump_target
)
5575 && (TREE_CODE (*jump_target
) == RETURN_EXPR
5576 || (TREE_CODE (*jump_target
) == LABEL_DECL
5577 && LABEL_DECL_CDTOR (*jump_target
)));
5581 breaks (tree
*jump_target
)
5584 && ((TREE_CODE (*jump_target
) == LABEL_DECL
5585 && LABEL_DECL_BREAK (*jump_target
))
5586 || TREE_CODE (*jump_target
) == BREAK_STMT
5587 || TREE_CODE (*jump_target
) == EXIT_EXPR
);
5591 continues (tree
*jump_target
)
5594 && ((TREE_CODE (*jump_target
) == LABEL_DECL
5595 && LABEL_DECL_CONTINUE (*jump_target
))
5596 || TREE_CODE (*jump_target
) == CONTINUE_STMT
);
5601 switches (tree
*jump_target
)
5604 && TREE_CODE (*jump_target
) == INTEGER_CST
;
5607 /* Subroutine of cxx_eval_statement_list. Determine whether the statement
5608 STMT matches *jump_target. If we're looking for a case label and we see
5609 the default label, note it in ctx->css_state. */
5612 label_matches (const constexpr_ctx
*ctx
, tree
*jump_target
, tree stmt
)
5614 switch (TREE_CODE (*jump_target
))
5617 if (TREE_CODE (stmt
) == LABEL_EXPR
5618 && LABEL_EXPR_LABEL (stmt
) == *jump_target
)
5623 if (TREE_CODE (stmt
) == CASE_LABEL_EXPR
)
5625 gcc_assert (ctx
->css_state
!= NULL
);
5626 if (!CASE_LOW (stmt
))
5628 /* default: should appear just once in a SWITCH_EXPR
5629 body (excluding nested SWITCH_EXPR). */
5630 gcc_assert (*ctx
->css_state
!= css_default_seen
);
5631 /* When evaluating SWITCH_EXPR body for the second time,
5632 return true for the default: label. */
5633 if (*ctx
->css_state
== css_default_processing
)
5635 *ctx
->css_state
= css_default_seen
;
5637 else if (CASE_HIGH (stmt
))
5639 if (tree_int_cst_le (CASE_LOW (stmt
), *jump_target
)
5640 && tree_int_cst_le (*jump_target
, CASE_HIGH (stmt
)))
5643 else if (tree_int_cst_equal (*jump_target
, CASE_LOW (stmt
)))
5650 /* These two are handled directly in cxx_eval_loop_expr by testing
5651 breaks (jump_target) or continues (jump_target). */
5660 /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
5661 semantics, for switch, break, continue, and return. */
5664 cxx_eval_statement_list (const constexpr_ctx
*ctx
, tree t
,
5665 bool *non_constant_p
, bool *overflow_p
,
5668 tree_stmt_iterator i
;
5670 /* In a statement-expression we want to return the last value.
5671 For empty statement expression return void_node. */
5675 local_target
= NULL_TREE
;
5676 jump_target
= &local_target
;
5678 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
5680 tree stmt
= tsi_stmt (i
);
5681 /* We've found a continue, so skip everything until we reach
5682 the label its jumping to. */
5683 if (continues (jump_target
))
5685 if (label_matches (ctx
, jump_target
, stmt
))
5687 *jump_target
= NULL_TREE
;
5691 if (TREE_CODE (stmt
) == DEBUG_BEGIN_STMT
)
5693 r
= cxx_eval_constant_expression (ctx
, stmt
, false,
5694 non_constant_p
, overflow_p
,
5696 if (*non_constant_p
)
5698 if (returns (jump_target
) || breaks (jump_target
))
5701 if (*jump_target
&& jump_target
== &local_target
)
5703 /* We aren't communicating the jump to our caller, so give up. We don't
5704 need to support evaluation of jumps out of statement-exprs. */
5706 error_at (cp_expr_loc_or_input_loc (r
),
5707 "statement is not a constant expression");
5708 *non_constant_p
= true;
5713 /* Evaluate a LOOP_EXPR for side-effects. Handles break and return
5714 semantics; continue semantics are covered by cxx_eval_statement_list. */
5717 cxx_eval_loop_expr (const constexpr_ctx
*ctx
, tree t
,
5718 bool *non_constant_p
, bool *overflow_p
,
5721 constexpr_ctx new_ctx
= *ctx
;
5725 local_target
= NULL_TREE
;
5726 jump_target
= &local_target
;
5729 tree body
, cond
= NULL_TREE
, expr
= NULL_TREE
;
5731 switch (TREE_CODE (t
))
5734 body
= LOOP_EXPR_BODY (t
);
5741 body
= WHILE_BODY (t
);
5742 cond
= WHILE_COND (t
);
5746 if (FOR_INIT_STMT (t
))
5747 cxx_eval_constant_expression (ctx
, FOR_INIT_STMT (t
), /*lval*/false,
5748 non_constant_p
, overflow_p
, jump_target
);
5749 if (*non_constant_p
)
5751 body
= FOR_BODY (t
);
5752 cond
= FOR_COND (t
);
5753 expr
= FOR_EXPR (t
);
5759 auto_vec
<tree
, 10> save_exprs
;
5760 new_ctx
.save_exprs
= &save_exprs
;
5766 cxx_eval_constant_expression (&new_ctx
, body
, /*lval*/false,
5767 non_constant_p
, overflow_p
,
5769 if (breaks (jump_target
))
5771 *jump_target
= NULL_TREE
;
5775 if (TREE_CODE (t
) != LOOP_EXPR
&& continues (jump_target
))
5776 *jump_target
= NULL_TREE
;
5779 cxx_eval_constant_expression (&new_ctx
, expr
, /*lval*/false,
5780 non_constant_p
, overflow_p
,
5787 = cxx_eval_constant_expression (&new_ctx
, cond
, /*lval*/false,
5788 non_constant_p
, overflow_p
,
5792 if (verify_constant (res
, ctx
->quiet
, non_constant_p
,
5795 if (integer_zerop (res
))
5799 gcc_assert (*jump_target
);
5802 /* Forget saved values of SAVE_EXPRs and TARGET_EXPRs. */
5805 FOR_EACH_VEC_ELT (save_exprs
, i
, save_expr
)
5806 ctx
->global
->values
.remove (save_expr
);
5807 save_exprs
.truncate (0);
5809 if (++count
>= constexpr_loop_limit
)
5812 error_at (cp_expr_loc_or_input_loc (t
),
5813 "%<constexpr%> loop iteration count exceeds limit of %d "
5814 "(use %<-fconstexpr-loop-limit=%> to increase the limit)",
5815 constexpr_loop_limit
);
5816 *non_constant_p
= true;
5820 while (!returns (jump_target
)
5821 && !breaks (jump_target
)
5822 && !continues (jump_target
)
5823 && (!switches (jump_target
) || count
== 0)
5824 && !*non_constant_p
);
5826 /* Forget saved values of SAVE_EXPRs and TARGET_EXPRs. */
5829 FOR_EACH_VEC_ELT (save_exprs
, i
, save_expr
)
5830 ctx
->global
->values
.remove (save_expr
);
5835 /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
5839 cxx_eval_switch_expr (const constexpr_ctx
*ctx
, tree t
,
5840 bool *non_constant_p
, bool *overflow_p
,
5844 = TREE_CODE (t
) == SWITCH_STMT
? SWITCH_STMT_COND (t
) : SWITCH_COND (t
);
5845 cond
= cxx_eval_constant_expression (ctx
, cond
, false,
5846 non_constant_p
, overflow_p
);
5847 VERIFY_CONSTANT (cond
);
5848 *jump_target
= cond
;
5851 = TREE_CODE (t
) == SWITCH_STMT
? SWITCH_STMT_BODY (t
) : SWITCH_BODY (t
);
5852 constexpr_ctx new_ctx
= *ctx
;
5853 constexpr_switch_state css
= css_default_not_seen
;
5854 new_ctx
.css_state
= &css
;
5855 cxx_eval_constant_expression (&new_ctx
, body
, false,
5856 non_constant_p
, overflow_p
, jump_target
);
5857 if (switches (jump_target
) && css
== css_default_seen
)
5859 /* If the SWITCH_EXPR body has default: label, process it once again,
5860 this time instructing label_matches to return true for default:
5861 label on switches (jump_target). */
5862 css
= css_default_processing
;
5863 cxx_eval_constant_expression (&new_ctx
, body
, false,
5864 non_constant_p
, overflow_p
, jump_target
);
5866 if (breaks (jump_target
) || switches (jump_target
))
5867 *jump_target
= NULL_TREE
;
5871 /* Find the object of TYPE under initialization in CTX. */
5874 lookup_placeholder (const constexpr_ctx
*ctx
, bool lval
, tree type
)
5879 /* Prefer the outermost matching object, but don't cross
5880 CONSTRUCTOR_PLACEHOLDER_BOUNDARY constructors. */
5881 if (ctx
->ctor
&& !CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx
->ctor
))
5882 if (tree outer_ob
= lookup_placeholder (ctx
->parent
, lval
, type
))
5885 /* We could use ctx->object unconditionally, but using ctx->ctor when we
5886 can is a minor optimization. */
5887 if (!lval
&& ctx
->ctor
&& same_type_p (TREE_TYPE (ctx
->ctor
), type
))
5893 /* Since an object cannot have a field of its own type, we can search outward
5894 from ctx->object to find the unique containing object of TYPE. */
5895 tree ob
= ctx
->object
;
5898 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob
), type
))
5900 if (handled_component_p (ob
))
5901 ob
= TREE_OPERAND (ob
, 0);
5909 /* Complain about an attempt to evaluate inline assembly. */
5912 inline_asm_in_constexpr_error (location_t loc
)
5914 auto_diagnostic_group d
;
5915 error_at (loc
, "inline assembly is not a constant expression");
5916 inform (loc
, "only unevaluated inline assembly is allowed in a "
5917 "%<constexpr%> function in C++20");
5920 /* Attempt to reduce the expression T to a constant value.
5921 On failure, issue diagnostic and return error_mark_node. */
5922 /* FIXME unify with c_fully_fold */
5923 /* FIXME overflow_p is too global */
5926 cxx_eval_constant_expression (const constexpr_ctx
*ctx
, tree t
,
5928 bool *non_constant_p
, bool *overflow_p
,
5929 tree
*jump_target
/* = NULL */)
5931 if (jump_target
&& *jump_target
)
5933 /* If we are jumping, ignore all statements/expressions except those
5934 that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */
5935 switch (TREE_CODE (t
))
5938 case STATEMENT_LIST
:
5947 case CASE_LABEL_EXPR
:
5948 if (label_matches (ctx
, jump_target
, t
))
5950 *jump_target
= NULL_TREE
;
5956 if (error_operand_p (t
))
5958 *non_constant_p
= true;
5962 location_t loc
= cp_expr_loc_or_input_loc (t
);
5964 STRIP_ANY_LOCATION_WRAPPER (t
);
5966 if (CONSTANT_CLASS_P (t
))
5968 if (TREE_OVERFLOW (t
))
5971 permerror (input_location
, "overflow in constant expression");
5972 if (!flag_permissive
|| ctx
->quiet
)
5976 if (TREE_CODE (t
) == INTEGER_CST
5977 && TYPE_PTR_P (TREE_TYPE (t
))
5978 && !integer_zerop (t
))
5981 error ("value %qE of type %qT is not a constant expression",
5983 *non_constant_p
= true;
5989 /* Avoid excessively long constexpr evaluations. */
5990 if (++ctx
->global
->constexpr_ops_count
>= constexpr_ops_limit
)
5994 "%<constexpr%> evaluation operation count exceeds limit of "
5995 "%wd (use %<-fconstexpr-ops-limit=%> to increase the limit)",
5996 constexpr_ops_limit
);
5997 ctx
->global
->constexpr_ops_count
= INTTYPE_MINIMUM (HOST_WIDE_INT
);
5998 *non_constant_p
= true;
6002 constexpr_ctx new_ctx
;
6005 tree_code tcode
= TREE_CODE (t
);
6011 /* We ask for an rvalue for the RESULT_DECL when indirecting
6012 through an invisible reference, or in named return value
6014 if (tree
*p
= ctx
->global
->values
.get (t
))
6019 error ("%qE is not a constant expression", t
);
6020 *non_constant_p
= true;
6025 if (DECL_HAS_VALUE_EXPR_P (t
))
6027 if (is_normal_capture_proxy (t
)
6028 && current_function_decl
== DECL_CONTEXT (t
))
6030 /* Function parms aren't constexpr within the function
6031 definition, so don't try to look at the closure. But if the
6032 captured variable is constant, try to evaluate it directly. */
6033 r
= DECL_CAPTURED_VARIABLE (t
);
6034 tree type
= TREE_TYPE (t
);
6035 if (TYPE_REF_P (type
) != TYPE_REF_P (TREE_TYPE (r
)))
6037 /* Adjust r to match the reference-ness of t. */
6038 if (TYPE_REF_P (type
))
6039 r
= build_address (r
);
6041 r
= convert_from_reference (r
);
6045 r
= DECL_VALUE_EXPR (t
);
6046 return cxx_eval_constant_expression (ctx
, r
, lval
, non_constant_p
,
6051 /* We used to not check lval for CONST_DECL, but darwin.c uses
6052 CONST_DECL for aggregate constants. */
6055 else if (t
== ctx
->object
)
6058 if (tree
*p
= ctx
->global
->values
.get (t
))
6059 if (*p
!= NULL_TREE
)
6064 if (COMPLETE_TYPE_P (TREE_TYPE (t
))
6065 && is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
6067 /* If the class is empty, we aren't actually loading anything. */
6068 r
= build_constructor (TREE_TYPE (t
), NULL
);
6069 TREE_CONSTANT (r
) = true;
6071 else if (ctx
->strict
)
6072 r
= decl_really_constant_value (t
, /*unshare_p=*/false);
6074 r
= decl_constant_value (t
, /*unshare_p=*/false);
6075 if (TREE_CODE (r
) == TARGET_EXPR
6076 && TREE_CODE (TARGET_EXPR_INITIAL (r
)) == CONSTRUCTOR
)
6077 r
= TARGET_EXPR_INITIAL (r
);
6081 non_const_var_error (loc
, r
);
6082 *non_constant_p
= true;
6086 case DEBUG_BEGIN_STMT
:
6087 /* ??? It might be nice to retain this information somehow, so
6088 as to be able to step into a constexpr function call. */
6095 case CASE_LABEL_EXPR
:
6100 if (lval
&& !TYPE_REF_P (TREE_TYPE (t
)))
6102 else if (tree
*p
= ctx
->global
->values
.get (r
))
6105 /* Defer in case this is only used for its type. */;
6106 else if (COMPLETE_TYPE_P (TREE_TYPE (t
))
6107 && is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
6109 /* If the class is empty, we aren't actually loading anything. */
6110 r
= build_constructor (TREE_TYPE (t
), NULL
);
6111 TREE_CONSTANT (r
) = true;
6116 error ("%qE is not a constant expression", t
);
6117 *non_constant_p
= true;
6122 case AGGR_INIT_EXPR
:
6123 r
= cxx_eval_call_expression (ctx
, t
, lval
,
6124 non_constant_p
, overflow_p
);
6129 r
= DECL_EXPR_DECL (t
);
6130 if (TREE_CODE (r
) == USING_DECL
)
6135 if (AGGREGATE_TYPE_P (TREE_TYPE (r
))
6136 || VECTOR_TYPE_P (TREE_TYPE (r
)))
6140 new_ctx
.ctor
= build_constructor (TREE_TYPE (r
), NULL
);
6141 CONSTRUCTOR_NO_CLEARING (new_ctx
.ctor
) = true;
6142 ctx
->global
->values
.put (r
, new_ctx
.ctor
);
6146 if (tree init
= DECL_INITIAL (r
))
6148 init
= cxx_eval_constant_expression (ctx
, init
,
6150 non_constant_p
, overflow_p
);
6151 /* Don't share a CONSTRUCTOR that might be changed. */
6152 init
= unshare_constructor (init
);
6153 /* Remember that a constant object's constructor has already
6155 if (CLASS_TYPE_P (TREE_TYPE (r
))
6156 && CP_TYPE_CONST_P (TREE_TYPE (r
)))
6157 TREE_READONLY (init
) = true;
6158 ctx
->global
->values
.put (r
, init
);
6160 else if (ctx
== &new_ctx
)
6161 /* We gave it a CONSTRUCTOR above. */;
6163 ctx
->global
->values
.put (r
, NULL_TREE
);
6169 tree type
= TREE_TYPE (t
);
6171 if (!literal_type_p (type
))
6175 auto_diagnostic_group d
;
6176 error ("temporary of non-literal type %qT in a "
6177 "constant expression", type
);
6178 explain_non_literal_class (type
);
6180 *non_constant_p
= true;
6183 gcc_checking_assert (!TARGET_EXPR_DIRECT_INIT_P (t
));
6184 /* Avoid evaluating a TARGET_EXPR more than once. */
6185 tree slot
= TARGET_EXPR_SLOT (t
);
6186 if (tree
*p
= ctx
->global
->values
.get (slot
))
6193 if ((AGGREGATE_TYPE_P (type
) || VECTOR_TYPE_P (type
)))
6195 /* We're being expanded without an explicit target, so start
6196 initializing a new object; expansion with an explicit target
6197 strips the TARGET_EXPR before we get here. */
6199 /* Link CTX to NEW_CTX so that lookup_placeholder can resolve
6200 any PLACEHOLDER_EXPR within the initializer that refers to the
6201 former object under construction. */
6202 new_ctx
.parent
= ctx
;
6203 new_ctx
.ctor
= build_constructor (type
, NULL
);
6204 CONSTRUCTOR_NO_CLEARING (new_ctx
.ctor
) = true;
6205 new_ctx
.object
= slot
;
6206 ctx
->global
->values
.put (new_ctx
.object
, new_ctx
.ctor
);
6209 /* Pass false for 'lval' because this indicates
6210 initialization of a temporary. */
6211 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
6213 non_constant_p
, overflow_p
);
6214 if (*non_constant_p
)
6216 /* Adjust the type of the result to the type of the temporary. */
6217 r
= adjust_temp_type (type
, r
);
6218 if (TARGET_EXPR_CLEANUP (t
) && !CLEANUP_EH_ONLY (t
))
6219 ctx
->global
->cleanups
->safe_push (TARGET_EXPR_CLEANUP (t
));
6220 r
= unshare_constructor (r
);
6221 ctx
->global
->values
.put (slot
, r
);
6222 if (ctx
->save_exprs
)
6223 ctx
->save_exprs
->safe_push (slot
);
6231 gcc_assert (jump_target
== NULL
|| *jump_target
== NULL_TREE
);
6232 r
= cxx_eval_store_expression (ctx
, t
, lval
,
6233 non_constant_p
, overflow_p
);
6237 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
6239 non_constant_p
, overflow_p
);
6243 if (TREE_OPERAND (t
, 0) != NULL_TREE
)
6244 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
6246 non_constant_p
, overflow_p
);
6254 /* Can happen with ({ return true; }) && false; passed to
6255 maybe_constant_value. There is nothing to jump over in this
6256 case, and the bug will be diagnosed later. */
6257 gcc_assert (ctx
->quiet
);
6258 *non_constant_p
= true;
6263 /* Avoid evaluating a SAVE_EXPR more than once. */
6264 if (tree
*p
= ctx
->global
->values
.get (t
))
6268 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), false,
6269 non_constant_p
, overflow_p
);
6270 if (*non_constant_p
)
6272 ctx
->global
->values
.put (t
, r
);
6273 if (ctx
->save_exprs
)
6274 ctx
->save_exprs
->safe_push (t
);
6278 case TRY_CATCH_EXPR
:
6279 if (TREE_OPERAND (t
, 0) == NULL_TREE
)
6285 case NON_LVALUE_EXPR
:
6287 case MUST_NOT_THROW_EXPR
:
6290 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
6292 non_constant_p
, overflow_p
,
6296 case CLEANUP_POINT_EXPR
:
6298 auto_vec
<tree
, 2> cleanups
;
6299 vec
<tree
> *prev_cleanups
= ctx
->global
->cleanups
;
6300 ctx
->global
->cleanups
= &cleanups
;
6301 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
6303 non_constant_p
, overflow_p
,
6305 ctx
->global
->cleanups
= prev_cleanups
;
6308 /* Evaluate the cleanups. */
6309 FOR_EACH_VEC_ELT_REVERSE (cleanups
, i
, cleanup
)
6310 cxx_eval_constant_expression (ctx
, cleanup
, false,
6311 non_constant_p
, overflow_p
);
6315 case TRY_FINALLY_EXPR
:
6316 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), lval
,
6317 non_constant_p
, overflow_p
,
6319 if (!*non_constant_p
)
6320 /* Also evaluate the cleanup. */
6321 cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1), true,
6322 non_constant_p
, overflow_p
);
6326 r
= cxx_eval_constant_expression (ctx
, CLEANUP_BODY (t
), lval
,
6327 non_constant_p
, overflow_p
,
6329 if (!CLEANUP_EH_ONLY (t
) && !*non_constant_p
)
6331 iloc_sentinel
ils (loc
);
6332 /* Also evaluate the cleanup. */
6333 cxx_eval_constant_expression (ctx
, CLEANUP_EXPR (t
), true,
6334 non_constant_p
, overflow_p
);
6338 /* These differ from cxx_eval_unary_expression in that this doesn't
6339 check for a constant operand or result; an address can be
6340 constant without its operand being, and vice versa. */
6343 r
= cxx_eval_indirect_ref (ctx
, t
, lval
,
6344 non_constant_p
, overflow_p
);
6349 tree oldop
= TREE_OPERAND (t
, 0);
6350 tree op
= cxx_eval_constant_expression (ctx
, oldop
,
6352 non_constant_p
, overflow_p
);
6353 /* Don't VERIFY_CONSTANT here. */
6354 if (*non_constant_p
)
6356 gcc_checking_assert (TREE_CODE (op
) != CONSTRUCTOR
);
6357 /* This function does more aggressive folding than fold itself. */
6358 r
= build_fold_addr_expr_with_type (op
, TREE_TYPE (t
));
6359 if (TREE_CODE (r
) == ADDR_EXPR
&& TREE_OPERAND (r
, 0) == oldop
)
6371 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), lval
,
6372 non_constant_p
, overflow_p
);
6373 if (r
== error_mark_node
)
6375 else if (r
== TREE_OPERAND (t
, 0))
6378 r
= fold_build1 (TREE_CODE (t
), TREE_TYPE (t
), r
);
6383 case FIX_TRUNC_EXPR
:
6389 case TRUTH_NOT_EXPR
:
6390 case FIXED_CONVERT_EXPR
:
6391 r
= cxx_eval_unary_expression (ctx
, t
, lval
,
6392 non_constant_p
, overflow_p
);
6396 r
= fold_sizeof_expr (t
);
6397 /* In a template, fold_sizeof_expr may merely create a new SIZEOF_EXPR,
6398 which could lead to an infinite recursion. */
6399 if (TREE_CODE (r
) != SIZEOF_EXPR
)
6400 r
= cxx_eval_constant_expression (ctx
, r
, lval
,
6401 non_constant_p
, overflow_p
,
6405 *non_constant_p
= true;
6406 gcc_assert (ctx
->quiet
);
6413 /* check_return_expr sometimes wraps a TARGET_EXPR in a
6414 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
6415 introduced by build_call_a. */
6416 tree op0
= TREE_OPERAND (t
, 0);
6417 tree op1
= TREE_OPERAND (t
, 1);
6419 if ((TREE_CODE (op0
) == TARGET_EXPR
&& op1
== TARGET_EXPR_SLOT (op0
))
6420 || TREE_CODE (op1
) == EMPTY_CLASS_EXPR
)
6421 r
= cxx_eval_constant_expression (ctx
, op0
,
6422 lval
, non_constant_p
, overflow_p
,
6426 /* Check that the LHS is constant and then discard it. */
6427 cxx_eval_constant_expression (ctx
, op0
,
6428 true, non_constant_p
, overflow_p
,
6430 if (*non_constant_p
)
6432 op1
= TREE_OPERAND (t
, 1);
6433 r
= cxx_eval_constant_expression (ctx
, op1
,
6434 lval
, non_constant_p
, overflow_p
,
6440 case POINTER_PLUS_EXPR
:
6441 case POINTER_DIFF_EXPR
:
6445 case TRUNC_DIV_EXPR
:
6447 case FLOOR_DIV_EXPR
:
6448 case ROUND_DIV_EXPR
:
6449 case TRUNC_MOD_EXPR
:
6451 case ROUND_MOD_EXPR
:
6453 case EXACT_DIV_EXPR
:
6463 case TRUTH_XOR_EXPR
:
6470 case SPACESHIP_EXPR
:
6471 case UNORDERED_EXPR
:
6481 r
= cxx_eval_binary_expression (ctx
, t
, lval
,
6482 non_constant_p
, overflow_p
);
6485 /* fold can introduce non-IF versions of these; still treat them as
6486 short-circuiting. */
6487 case TRUTH_AND_EXPR
:
6488 case TRUTH_ANDIF_EXPR
:
6489 r
= cxx_eval_logical_expression (ctx
, t
, boolean_false_node
,
6492 non_constant_p
, overflow_p
);
6496 case TRUTH_ORIF_EXPR
:
6497 r
= cxx_eval_logical_expression (ctx
, t
, boolean_true_node
,
6500 non_constant_p
, overflow_p
);
6504 r
= cxx_eval_array_reference (ctx
, t
, lval
,
6505 non_constant_p
, overflow_p
);
6509 if (is_overloaded_fn (t
))
6511 /* We can only get here in checking mode via
6512 build_non_dependent_expr, because any expression that
6513 calls or takes the address of the function will have
6514 pulled a FUNCTION_DECL out of the COMPONENT_REF. */
6515 gcc_checking_assert (ctx
->quiet
|| errorcount
);
6516 *non_constant_p
= true;
6519 r
= cxx_eval_component_reference (ctx
, t
, lval
,
6520 non_constant_p
, overflow_p
);
6524 r
= cxx_eval_bit_field_ref (ctx
, t
, lval
,
6525 non_constant_p
, overflow_p
);
6530 if (jump_target
&& *jump_target
)
6532 tree orig_jump
= *jump_target
;
6533 tree arg
= ((TREE_CODE (t
) != IF_STMT
|| TREE_OPERAND (t
, 1))
6534 ? TREE_OPERAND (t
, 1) : void_node
);
6535 /* When jumping to a label, the label might be either in the
6536 then or else blocks, so process then block first in skipping
6537 mode first, and if we are still in the skipping mode at its end,
6538 process the else block too. */
6539 r
= cxx_eval_constant_expression (ctx
, arg
, lval
, non_constant_p
,
6540 overflow_p
, jump_target
);
6541 /* It's possible that we found the label in the then block. But
6542 it could have been followed by another jumping statement, e.g.
6543 say we're looking for case 1:
6546 // skipped statements
6547 case 1:; // clears up *jump_target
6548 return 1; // and sets it to a RETURN_EXPR
6551 in which case we need not go looking to the else block.
6552 (goto is not allowed in a constexpr function.) */
6553 if (*jump_target
== orig_jump
)
6555 arg
= ((TREE_CODE (t
) != IF_STMT
|| TREE_OPERAND (t
, 2))
6556 ? TREE_OPERAND (t
, 2) : void_node
);
6557 r
= cxx_eval_constant_expression (ctx
, arg
, lval
, non_constant_p
,
6558 overflow_p
, jump_target
);
6562 r
= cxx_eval_conditional_expression (ctx
, t
, lval
,
6563 non_constant_p
, overflow_p
,
6567 r
= cxx_eval_vector_conditional_expression (ctx
, t
, non_constant_p
,
6572 if (TREE_CONSTANT (t
) && reduced_constant_expression_p (t
))
6574 /* Don't re-process a constant CONSTRUCTOR, but do fold it to
6575 VECTOR_CST if applicable. */
6576 verify_constructor_flags (t
);
6577 if (TREE_CONSTANT (t
))
6580 r
= cxx_eval_bare_aggregate (ctx
, t
, lval
,
6581 non_constant_p
, overflow_p
);
6585 /* We can get this in a defaulted constructor for a class with a
6586 non-static data member of array type. Either the initializer will
6587 be NULL, meaning default-initialization, or it will be an lvalue
6588 or xvalue of the same type, meaning direct-initialization from the
6589 corresponding member. */
6590 r
= cxx_eval_vec_init (ctx
, t
, lval
,
6591 non_constant_p
, overflow_p
);
6595 r
= cxx_eval_trinary_expression (ctx
, t
, lval
,
6596 non_constant_p
, overflow_p
);
6600 if (REINTERPRET_CAST_P (t
))
6604 "%<reinterpret_cast%> is not a constant expression");
6605 *non_constant_p
= true;
6610 case VIEW_CONVERT_EXPR
:
6611 case UNARY_PLUS_EXPR
:
6613 tree oldop
= TREE_OPERAND (t
, 0);
6615 tree op
= cxx_eval_constant_expression (ctx
, oldop
,
6617 non_constant_p
, overflow_p
);
6618 if (*non_constant_p
)
6620 tree type
= TREE_TYPE (t
);
6622 if (VOID_TYPE_P (type
))
6625 if (TREE_CODE (t
) == CONVERT_EXPR
6626 && ARITHMETIC_TYPE_P (type
)
6627 && INDIRECT_TYPE_P (TREE_TYPE (op
)))
6631 "conversion from pointer type %qT to arithmetic type "
6632 "%qT in a constant expression", TREE_TYPE (op
), type
);
6633 *non_constant_p
= true;
6637 if (TREE_CODE (op
) == PTRMEM_CST
&& !TYPE_PTRMEM_P (type
))
6638 op
= cplus_expand_constant (op
);
6640 if (TREE_CODE (op
) == PTRMEM_CST
&& tcode
== NOP_EXPR
)
6642 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (op
))
6643 && !can_convert_qual (type
, op
))
6644 op
= cplus_expand_constant (op
);
6645 return cp_fold_convert (type
, op
);
6648 if (INDIRECT_TYPE_P (type
) && TREE_CODE (op
) == INTEGER_CST
)
6650 if (integer_zerop (op
))
6652 if (TYPE_REF_P (type
))
6656 "dereferencing a null pointer");
6657 *non_constant_p
= true;
6660 else if (TYPE_PTR_P (TREE_TYPE (op
)))
6662 tree from
= TREE_TYPE (op
);
6664 if (!can_convert (type
, from
, tf_none
))
6668 "conversion of %qT null pointer to %qT "
6669 "is not a constant expression",
6671 *non_constant_p
= true;
6678 /* This detects for example:
6679 reinterpret_cast<void*>(sizeof 0)
6682 error_at (loc
, "%<reinterpret_cast<%T>(%E)%> is not "
6683 "a constant expression",
6685 *non_constant_p
= true;
6690 if (INDIRECT_TYPE_P (type
)
6691 && TREE_CODE (op
) == NOP_EXPR
6692 && TREE_TYPE (op
) == ptr_type_node
6693 && TREE_CODE (TREE_OPERAND (op
, 0)) == ADDR_EXPR
6694 && VAR_P (TREE_OPERAND (TREE_OPERAND (op
, 0), 0))
6695 && (DECL_NAME (TREE_OPERAND (TREE_OPERAND (op
, 0),
6696 0)) == heap_uninit_identifier
6697 || DECL_NAME (TREE_OPERAND (TREE_OPERAND (op
, 0),
6698 0)) == heap_vec_uninit_identifier
))
6700 tree var
= TREE_OPERAND (TREE_OPERAND (op
, 0), 0);
6701 tree var_size
= TYPE_SIZE_UNIT (TREE_TYPE (var
));
6702 tree elt_type
= TREE_TYPE (type
);
6703 tree cookie_size
= NULL_TREE
;
6704 if (TREE_CODE (elt_type
) == RECORD_TYPE
6705 && TYPE_NAME (elt_type
) == heap_identifier
)
6707 tree fld1
= TYPE_FIELDS (elt_type
);
6708 tree fld2
= DECL_CHAIN (fld1
);
6709 elt_type
= TREE_TYPE (TREE_TYPE (fld2
));
6710 cookie_size
= TYPE_SIZE_UNIT (TREE_TYPE (fld1
));
6713 = (DECL_NAME (var
) == heap_uninit_identifier
6714 ? heap_identifier
: heap_vec_identifier
);
6716 = build_new_constexpr_heap_type (elt_type
, cookie_size
,
6718 TREE_TYPE (TREE_OPERAND (op
, 0))
6719 = build_pointer_type (TREE_TYPE (var
));
6722 if (op
== oldop
&& tcode
!= UNARY_PLUS_EXPR
)
6723 /* We didn't fold at the top so we could check for ptr-int
6729 /* Handle an array's bounds having been deduced after we built
6730 the wrapping expression. */
6731 if (same_type_ignoring_tlq_and_bounds_p (type
, TREE_TYPE (op
)))
6733 else if (sop
= tree_strip_nop_conversions (op
),
6734 sop
!= op
&& (same_type_ignoring_tlq_and_bounds_p
6735 (type
, TREE_TYPE (sop
))))
6737 else if (tcode
== UNARY_PLUS_EXPR
)
6738 r
= fold_convert (TREE_TYPE (t
), op
);
6740 r
= fold_build1 (tcode
, type
, op
);
6742 /* Conversion of an out-of-range value has implementation-defined
6743 behavior; the language considers it different from arithmetic
6744 overflow, which is undefined. */
6745 if (TREE_OVERFLOW_P (r
) && !TREE_OVERFLOW_P (op
))
6746 TREE_OVERFLOW (r
) = false;
6750 case EMPTY_CLASS_EXPR
:
6751 /* Handle EMPTY_CLASS_EXPR produced by build_call_a by lowering
6752 it to an appropriate CONSTRUCTOR. */
6753 return build_constructor (TREE_TYPE (t
), NULL
);
6755 case STATEMENT_LIST
:
6757 new_ctx
.ctor
= new_ctx
.object
= NULL_TREE
;
6758 return cxx_eval_statement_list (&new_ctx
, t
,
6759 non_constant_p
, overflow_p
, jump_target
);
6762 return cxx_eval_constant_expression (ctx
, BIND_EXPR_BODY (t
),
6764 non_constant_p
, overflow_p
,
6767 case PREINCREMENT_EXPR
:
6768 case POSTINCREMENT_EXPR
:
6769 case PREDECREMENT_EXPR
:
6770 case POSTDECREMENT_EXPR
:
6771 return cxx_eval_increment_expression (ctx
, t
,
6772 lval
, non_constant_p
, overflow_p
);
6778 case VEC_DELETE_EXPR
:
6781 /* GCC internal stuff. */
6783 case NON_DEPENDENT_EXPR
:
6787 error_at (loc
, "expression %qE is not a constant expression", t
);
6788 *non_constant_p
= true;
6792 /* Virtual function lookup. We don't need to do anything fancy. */
6793 return cxx_eval_constant_expression (ctx
, OBJ_TYPE_REF_EXPR (t
),
6794 lval
, non_constant_p
, overflow_p
);
6796 case PLACEHOLDER_EXPR
:
6797 /* Use of the value or address of the current object. */
6798 if (tree ctor
= lookup_placeholder (ctx
, lval
, TREE_TYPE (t
)))
6800 if (TREE_CODE (ctor
) == CONSTRUCTOR
)
6803 return cxx_eval_constant_expression (ctx
, ctor
, lval
,
6804 non_constant_p
, overflow_p
);
6806 /* A placeholder without a referent. We can get here when
6807 checking whether NSDMIs are noexcept, or in massage_init_elt;
6808 just say it's non-constant for now. */
6809 gcc_assert (ctx
->quiet
);
6810 *non_constant_p
= true;
6815 tree cond
= TREE_OPERAND (t
, 0);
6816 cond
= cxx_eval_constant_expression (ctx
, cond
, /*lval*/false,
6817 non_constant_p
, overflow_p
);
6818 VERIFY_CONSTANT (cond
);
6819 if (integer_nonzerop (cond
))
6825 *jump_target
= TREE_OPERAND (t
, 0);
6826 gcc_assert (breaks (jump_target
) || continues (jump_target
)
6827 /* Allow for jumping to a cdtor_label. */
6828 || returns (jump_target
));
6835 cxx_eval_loop_expr (ctx
, t
,
6836 non_constant_p
, overflow_p
, jump_target
);
6841 cxx_eval_switch_expr (ctx
, t
,
6842 non_constant_p
, overflow_p
, jump_target
);
6846 /* It's possible to get a requires-expression in a constant
6847 expression. For example:
6849 template<typename T> concept bool C() {
6850 return requires (T t) { t; };
6853 template<typename T> requires !C<T>() void f(T);
6855 Normalization leaves f with the associated constraint
6856 '!requires (T t) { ... }' which is not transformed into
6858 if (!processing_template_decl
)
6859 return satisfy_constraint_expression (t
);
6861 *non_constant_p
= true;
6865 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
6867 non_constant_p
, overflow_p
,
6875 case TEMPLATE_ID_EXPR
:
6877 /* We can evaluate template-id that refers to a concept only if
6878 the template arguments are non-dependent. */
6879 tree id
= unpack_concept_check (t
);
6880 tree tmpl
= TREE_OPERAND (id
, 0);
6881 if (!concept_definition_p (tmpl
))
6882 internal_error ("unexpected template-id %qE", t
);
6884 if (function_concept_p (tmpl
))
6887 error_at (cp_expr_loc_or_input_loc (t
),
6888 "function concept must be called");
6889 r
= error_mark_node
;
6893 if (!processing_template_decl
6894 && !uid_sensitive_constexpr_evaluation_p ())
6895 r
= evaluate_concept_check (t
, tf_warning_or_error
);
6897 *non_constant_p
= true;
6904 inline_asm_in_constexpr_error (loc
);
6905 *non_constant_p
= true;
6912 error_at (EXPR_LOCATION (t
),
6913 "address of a call to %qs is not a constant expression",
6914 "__builtin_bit_cast");
6915 *non_constant_p
= true;
6918 r
= cxx_eval_bit_cast (ctx
, t
, non_constant_p
, overflow_p
);
6922 if (STATEMENT_CODE_P (TREE_CODE (t
)))
6924 /* This function doesn't know how to deal with pre-genericize
6925 statements; this can only happen with statement-expressions,
6926 so for now just fail. */
6928 error_at (EXPR_LOCATION (t
),
6929 "statement is not a constant expression");
6932 internal_error ("unexpected expression %qE of kind %s", t
,
6933 get_tree_code_name (TREE_CODE (t
)));
6934 *non_constant_p
= true;
6938 if (r
== error_mark_node
)
6939 *non_constant_p
= true;
6941 if (*non_constant_p
)
6947 /* P0859: A function is needed for constant evaluation if it is a constexpr
6948 function that is named by an expression ([basic.def.odr]) that is
6949 potentially constant evaluated.
6951 So we need to instantiate any constexpr functions mentioned by the
6952 expression even if the definition isn't needed for evaluating the
6956 instantiate_cx_fn_r (tree
*tp
, int *walk_subtrees
, void */
*data*/
)
6958 if (TREE_CODE (*tp
) == FUNCTION_DECL
6959 && DECL_DECLARED_CONSTEXPR_P (*tp
)
6960 && !DECL_INITIAL (*tp
)
6961 && !trivial_fn_p (*tp
)
6962 && DECL_TEMPLOID_INSTANTIATION (*tp
)
6963 && !uid_sensitive_constexpr_evaluation_p ())
6966 instantiate_decl (*tp
, /*defer_ok*/false, /*expl_inst*/false);
6969 else if (TREE_CODE (*tp
) == CALL_EXPR
6970 || TREE_CODE (*tp
) == AGGR_INIT_EXPR
)
6972 if (EXPR_HAS_LOCATION (*tp
))
6973 input_location
= EXPR_LOCATION (*tp
);
6983 instantiate_constexpr_fns (tree t
)
6985 location_t loc
= input_location
;
6986 cp_walk_tree_without_duplicates (&t
, instantiate_cx_fn_r
, NULL
);
6987 input_location
= loc
;
6990 /* Look for heap variables in the expression *TP. */
6993 find_heap_var_refs (tree
*tp
, int *walk_subtrees
, void */
*data*/
)
6996 && (DECL_NAME (*tp
) == heap_uninit_identifier
6997 || DECL_NAME (*tp
) == heap_identifier
6998 || DECL_NAME (*tp
) == heap_vec_uninit_identifier
6999 || DECL_NAME (*tp
) == heap_vec_identifier
7000 || DECL_NAME (*tp
) == heap_deleted_identifier
))
7008 /* Find immediate function decls in *TP if any. */
7011 find_immediate_fndecl (tree
*tp
, int */
*walk_subtrees*/
, void */
*data*/
)
7013 if (TREE_CODE (*tp
) == FUNCTION_DECL
&& DECL_IMMEDIATE_FUNCTION_P (*tp
))
7018 /* ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
7019 STRICT has the same sense as for constant_value_1: true if we only allow
7020 conforming C++ constant expressions, or false if we want a constant value
7021 even if it doesn't conform.
7022 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
7023 per P0595 even when ALLOW_NON_CONSTANT is true.
7024 CONSTEXPR_DTOR is true when evaluating the dtor of a constexpr variable.
7025 OBJECT must be non-NULL in that case. */
7028 cxx_eval_outermost_constant_expr (tree t
, bool allow_non_constant
,
7030 bool manifestly_const_eval
= false,
7031 bool constexpr_dtor
= false,
7032 tree object
= NULL_TREE
)
7034 auto_timevar
time (TV_CONSTEXPR
);
7036 bool non_constant_p
= false;
7037 bool overflow_p
= false;
7039 if (BRACE_ENCLOSED_INITIALIZER_P (t
))
7041 gcc_checking_assert (allow_non_constant
);
7045 constexpr_global_ctx global_ctx
;
7046 constexpr_ctx ctx
= { &global_ctx
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
7047 allow_non_constant
, strict
,
7048 manifestly_const_eval
|| !allow_non_constant
};
7050 /* Turn off -frounding-math for manifestly constant evaluation. */
7051 warning_sentinel
rm (flag_rounding_math
, ctx
.manifestly_const_eval
);
7052 tree type
= initialized_type (t
);
7054 bool is_consteval
= false;
7055 if (VOID_TYPE_P (type
))
7058 /* Used for destructors of array elements. */
7059 type
= TREE_TYPE (object
);
7062 if (cxx_dialect
< cxx20
)
7064 if (TREE_CODE (t
) != CALL_EXPR
&& TREE_CODE (t
) != AGGR_INIT_EXPR
)
7066 /* Calls to immediate functions returning void need to be
7068 tree fndecl
= cp_get_callee_fndecl_nofold (t
);
7069 if (fndecl
== NULL_TREE
|| !DECL_IMMEDIATE_FUNCTION_P (fndecl
))
7072 is_consteval
= true;
7075 else if (cxx_dialect
>= cxx20
7076 && (TREE_CODE (t
) == CALL_EXPR
7077 || TREE_CODE (t
) == AGGR_INIT_EXPR
7078 || TREE_CODE (t
) == TARGET_EXPR
))
7080 /* For non-concept checks, determine if it is consteval. */
7081 if (!concept_check_p (t
))
7084 if (TREE_CODE (x
) == TARGET_EXPR
)
7085 x
= TARGET_EXPR_INITIAL (x
);
7086 tree fndecl
= cp_get_callee_fndecl_nofold (x
);
7087 if (fndecl
&& DECL_IMMEDIATE_FUNCTION_P (fndecl
))
7088 is_consteval
= true;
7091 if (AGGREGATE_TYPE_P (type
) || VECTOR_TYPE_P (type
))
7093 /* In C++14 an NSDMI can participate in aggregate initialization,
7094 and can refer to the address of the object being initialized, so
7095 we need to pass in the relevant VAR_DECL if we want to do the
7096 evaluation in a single pass. The evaluation will dynamically
7097 update ctx.values for the VAR_DECL. We use the same strategy
7098 for C++11 constexpr constructors that refer to the object being
7102 gcc_assert (object
&& VAR_P (object
));
7103 gcc_assert (DECL_DECLARED_CONSTEXPR_P (object
));
7104 gcc_assert (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object
));
7105 if (error_operand_p (DECL_INITIAL (object
)))
7107 ctx
.ctor
= unshare_expr (DECL_INITIAL (object
));
7108 TREE_READONLY (ctx
.ctor
) = false;
7109 /* Temporarily force decl_really_constant_value to return false
7110 for it, we want to use ctx.ctor for the current value instead. */
7111 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object
) = false;
7115 ctx
.ctor
= build_constructor (type
, NULL
);
7116 CONSTRUCTOR_NO_CLEARING (ctx
.ctor
) = true;
7120 if (TREE_CODE (t
) == TARGET_EXPR
)
7121 object
= TARGET_EXPR_SLOT (t
);
7122 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
)
7123 object
= AGGR_INIT_EXPR_SLOT (t
);
7125 ctx
.object
= object
;
7127 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7128 (type
, TREE_TYPE (object
)));
7129 if (object
&& DECL_P (object
))
7130 global_ctx
.values
.put (object
, ctx
.ctor
);
7131 if (TREE_CODE (r
) == TARGET_EXPR
)
7132 /* Avoid creating another CONSTRUCTOR when we expand the
7134 r
= TARGET_EXPR_INITIAL (r
);
7137 auto_vec
<tree
, 16> cleanups
;
7138 global_ctx
.cleanups
= &cleanups
;
7140 instantiate_constexpr_fns (r
);
7141 r
= cxx_eval_constant_expression (&ctx
, r
,
7142 false, &non_constant_p
, &overflow_p
);
7144 if (!constexpr_dtor
)
7145 verify_constant (r
, allow_non_constant
, &non_constant_p
, &overflow_p
);
7147 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object
) = true;
7151 /* Evaluate the cleanups. */
7152 FOR_EACH_VEC_ELT_REVERSE (cleanups
, i
, cleanup
)
7153 cxx_eval_constant_expression (&ctx
, cleanup
, false,
7154 &non_constant_p
, &overflow_p
);
7156 /* Mutable logic is a bit tricky: we want to allow initialization of
7157 constexpr variables with mutable members, but we can't copy those
7158 members to another constexpr variable. */
7159 if (TREE_CODE (r
) == CONSTRUCTOR
&& CONSTRUCTOR_MUTABLE_POISON (r
))
7161 if (!allow_non_constant
)
7162 error ("%qE is not a constant expression because it refers to "
7163 "mutable subobjects of %qT", t
, type
);
7164 non_constant_p
= true;
7167 if (TREE_CODE (r
) == CONSTRUCTOR
&& CONSTRUCTOR_NO_CLEARING (r
))
7169 if (!allow_non_constant
)
7170 error ("%qE is not a constant expression because it refers to "
7171 "an incompletely initialized variable", t
);
7172 TREE_CONSTANT (r
) = false;
7173 non_constant_p
= true;
7176 if (!global_ctx
.heap_vars
.is_empty ())
7178 tree heap_var
= cp_walk_tree_without_duplicates (&r
, find_heap_var_refs
,
7183 if (!allow_non_constant
&& !non_constant_p
)
7184 error_at (DECL_SOURCE_LOCATION (heap_var
),
7185 "%qE is not a constant expression because it refers to "
7186 "a result of %<operator new%>", t
);
7188 non_constant_p
= true;
7190 FOR_EACH_VEC_ELT (global_ctx
.heap_vars
, i
, heap_var
)
7191 if (DECL_NAME (heap_var
) != heap_deleted_identifier
)
7193 if (!allow_non_constant
&& !non_constant_p
)
7194 error_at (DECL_SOURCE_LOCATION (heap_var
),
7195 "%qE is not a constant expression because allocated "
7196 "storage has not been deallocated", t
);
7198 non_constant_p
= true;
7202 /* Check that immediate invocation does not return an expression referencing
7203 any immediate function decls. They need to be allowed while parsing
7204 immediate functions, but can't leak outside of them. */
7207 && (current_function_decl
== NULL_TREE
7208 || !DECL_IMMEDIATE_FUNCTION_P (current_function_decl
)))
7209 if (tree immediate_fndecl
7210 = cp_walk_tree_without_duplicates (&r
, find_immediate_fndecl
,
7213 if (!allow_non_constant
&& !non_constant_p
)
7214 error_at (cp_expr_loc_or_input_loc (t
),
7215 "immediate evaluation returns address of immediate "
7216 "function %qD", immediate_fndecl
);
7218 non_constant_p
= true;
7222 /* If we saw something bad, go back to our argument. The wrapping below is
7223 only for the cases of TREE_CONSTANT argument or overflow. */
7226 if (!non_constant_p
&& overflow_p
)
7227 non_constant_p
= true;
7229 /* Unshare the result. */
7230 bool should_unshare
= true;
7231 if (r
== t
|| (TREE_CODE (t
) == TARGET_EXPR
7232 && TARGET_EXPR_INITIAL (t
) == r
))
7233 should_unshare
= false;
7235 if (non_constant_p
&& !allow_non_constant
)
7236 return error_mark_node
;
7237 else if (constexpr_dtor
)
7239 else if (non_constant_p
&& TREE_CONSTANT (r
))
7241 /* This isn't actually constant, so unset TREE_CONSTANT.
7242 Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires
7243 it to be set if it is invariant address, even when it is not
7244 a valid C++ constant expression. Wrap it with a NOP_EXPR
7246 if (EXPR_P (r
) && TREE_CODE (r
) != ADDR_EXPR
)
7248 else if (TREE_CODE (r
) == CONSTRUCTOR
)
7249 r
= build1 (VIEW_CONVERT_EXPR
, TREE_TYPE (r
), r
);
7251 r
= build_nop (TREE_TYPE (r
), r
);
7252 TREE_CONSTANT (r
) = false;
7254 else if (non_constant_p
)
7258 r
= unshare_expr (r
);
7260 if (TREE_CODE (r
) == CONSTRUCTOR
&& CLASS_TYPE_P (TREE_TYPE (r
)))
7262 r
= adjust_temp_type (type
, r
);
7263 if (TREE_CODE (t
) == TARGET_EXPR
7264 && TARGET_EXPR_INITIAL (t
) == r
)
7266 else if (TREE_CODE (t
) != CONSTRUCTOR
)
7268 r
= get_target_expr_sfinae (r
, tf_warning_or_error
| tf_no_cleanup
);
7269 TREE_CONSTANT (r
) = true;
7276 /* If T represents a constant expression returns its reduced value.
7277 Otherwise return error_mark_node. If T is dependent, then
7281 cxx_constant_value (tree t
, tree decl
)
7283 return cxx_eval_outermost_constant_expr (t
, false, true, true, false, decl
);
7286 /* Like cxx_constant_value, but used for evaluation of constexpr destructors
7287 of constexpr variables. The actual initializer of DECL is not modified. */
7290 cxx_constant_dtor (tree t
, tree decl
)
7292 cxx_eval_outermost_constant_expr (t
, false, true, true, true, decl
);
7295 /* Helper routine for fold_simple function. Either return simplified
7296 expression T, otherwise NULL_TREE.
7297 In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold
7298 even if we are within template-declaration. So be careful on call, as in
7299 such case types can be undefined. */
7302 fold_simple_1 (tree t
)
7305 enum tree_code code
= TREE_CODE (t
);
7317 return fold_sizeof_expr (t
);
7326 case TRUTH_NOT_EXPR
:
7328 case VIEW_CONVERT_EXPR
:
7331 case FIX_TRUNC_EXPR
:
7332 case FIXED_CONVERT_EXPR
:
7333 case ADDR_SPACE_CONVERT_EXPR
:
7335 op1
= TREE_OPERAND (t
, 0);
7337 t
= const_unop (code
, TREE_TYPE (t
), op1
);
7341 if (CONVERT_EXPR_CODE_P (code
)
7342 && TREE_OVERFLOW_P (t
) && !TREE_OVERFLOW_P (op1
))
7343 TREE_OVERFLOW (t
) = false;
7351 /* If T is a simple constant expression, returns its simplified value.
7352 Otherwise returns T. In contrast to maybe_constant_value we
7353 simplify only few operations on constant-expressions, and we don't
7354 try to simplify constexpressions. */
7357 fold_simple (tree t
)
7359 if (processing_template_decl
)
7362 tree r
= fold_simple_1 (t
);
7369 /* If T is a constant expression, returns its reduced value.
7370 Otherwise, if T does not have TREE_CONSTANT set, returns T.
7371 Otherwise, returns a version of T without TREE_CONSTANT.
7372 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated
7375 static GTY((deletable
)) hash_map
<tree
, tree
> *cv_cache
;
7378 maybe_constant_value (tree t
, tree decl
, bool manifestly_const_eval
)
7382 if (!is_nondependent_constant_expression (t
))
7384 if (TREE_OVERFLOW_P (t
))
7386 t
= build_nop (TREE_TYPE (t
), t
);
7387 TREE_CONSTANT (t
) = false;
7391 else if (CONSTANT_CLASS_P (t
))
7392 /* No caching or evaluation needed. */
7395 if (manifestly_const_eval
)
7396 return cxx_eval_outermost_constant_expr (t
, true, true, true, false, decl
);
7398 if (cv_cache
== NULL
)
7399 cv_cache
= hash_map
<tree
, tree
>::create_ggc (101);
7400 if (tree
*cached
= cv_cache
->get (t
))
7405 r
= break_out_target_exprs (r
, /*clear_loc*/true);
7406 protected_set_expr_location (r
, EXPR_LOCATION (t
));
7411 uid_sensitive_constexpr_evaluation_checker c
;
7412 r
= cxx_eval_outermost_constant_expr (t
, true, true, false, false, decl
);
7413 gcc_checking_assert (r
== t
7414 || CONVERT_EXPR_P (t
)
7415 || TREE_CODE (t
) == VIEW_CONVERT_EXPR
7416 || (TREE_CONSTANT (t
) && !TREE_CONSTANT (r
))
7417 || !cp_tree_equal (r
, t
));
7418 if (!c
.evaluation_restricted_p ())
7419 cv_cache
->put (t
, r
);
7423 /* Dispose of the whole CV_CACHE. */
7426 clear_cv_cache (void)
7428 if (cv_cache
!= NULL
)
7432 /* Dispose of the whole CV_CACHE and FOLD_CACHE. */
7435 clear_cv_and_fold_caches ()
7438 clear_fold_cache ();
7441 /* Internal function handling expressions in templates for
7442 fold_non_dependent_expr and fold_non_dependent_init.
7444 If we're in a template, but T isn't value dependent, simplify
7445 it. We're supposed to treat:
7447 template <typename T> void f(T[1 + 1]);
7448 template <typename T> void f(T[2]);
7450 as two declarations of the same function, for example. */
7453 fold_non_dependent_expr_template (tree t
, tsubst_flags_t complain
,
7454 bool manifestly_const_eval
,
7457 gcc_assert (processing_template_decl
);
7459 if (is_nondependent_constant_expression (t
))
7461 processing_template_decl_sentinel s
;
7462 t
= instantiate_non_dependent_expr_internal (t
, complain
);
7464 if (type_unknown_p (t
) || BRACE_ENCLOSED_INITIALIZER_P (t
))
7466 if (TREE_OVERFLOW_P (t
))
7468 t
= build_nop (TREE_TYPE (t
), t
);
7469 TREE_CONSTANT (t
) = false;
7474 tree r
= cxx_eval_outermost_constant_expr (t
, true, true,
7475 manifestly_const_eval
,
7477 /* cp_tree_equal looks through NOPs, so allow them. */
7478 gcc_checking_assert (r
== t
7479 || CONVERT_EXPR_P (t
)
7480 || TREE_CODE (t
) == VIEW_CONVERT_EXPR
7481 || (TREE_CONSTANT (t
) && !TREE_CONSTANT (r
))
7482 || !cp_tree_equal (r
, t
));
7485 else if (TREE_OVERFLOW_P (t
))
7487 t
= build_nop (TREE_TYPE (t
), t
);
7488 TREE_CONSTANT (t
) = false;
7494 /* Like maybe_constant_value but first fully instantiate the argument.
7496 Note: this is equivalent to instantiate_non_dependent_expr_sfinae
7497 (t, complain) followed by maybe_constant_value but is more efficient,
7498 because it calls instantiation_dependent_expression_p and
7499 potential_constant_expression at most once.
7500 The manifestly_const_eval argument is passed to maybe_constant_value.
7502 Callers should generally pass their active complain, or if they are in a
7503 non-template, diagnosing context, they can use the default of
7504 tf_warning_or_error. Callers that might be within a template context, don't
7505 have a complain parameter, and aren't going to remember the result for long
7506 (e.g. null_ptr_cst_p), can pass tf_none and deal with error_mark_node
7510 fold_non_dependent_expr (tree t
,
7511 tsubst_flags_t complain
/* = tf_warning_or_error */,
7512 bool manifestly_const_eval
/* = false */,
7513 tree object
/* = NULL_TREE */)
7518 if (processing_template_decl
)
7519 return fold_non_dependent_expr_template (t
, complain
,
7520 manifestly_const_eval
, object
);
7522 return maybe_constant_value (t
, object
, manifestly_const_eval
);
7525 /* Like fold_non_dependent_expr, but if EXPR couldn't be folded to a constant,
7526 return the original expression. */
7529 maybe_fold_non_dependent_expr (tree expr
,
7530 tsubst_flags_t complain
/*=tf_warning_or_error*/)
7532 tree t
= fold_non_dependent_expr (expr
, complain
);
7533 if (t
&& TREE_CONSTANT (t
))
7539 /* Like maybe_constant_init but first fully instantiate the argument. */
7542 fold_non_dependent_init (tree t
,
7543 tsubst_flags_t complain
/*=tf_warning_or_error*/,
7544 bool manifestly_const_eval
/*=false*/,
7545 tree object
/* = NULL_TREE */)
7550 if (processing_template_decl
)
7552 t
= fold_non_dependent_expr_template (t
, complain
,
7553 manifestly_const_eval
, object
);
7554 /* maybe_constant_init does this stripping, so do it here too. */
7555 if (TREE_CODE (t
) == TARGET_EXPR
)
7557 tree init
= TARGET_EXPR_INITIAL (t
);
7558 if (TREE_CODE (init
) == CONSTRUCTOR
)
7564 return maybe_constant_init (t
, object
, manifestly_const_eval
);
7567 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
7568 than wrapped in a TARGET_EXPR.
7569 ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
7570 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
7571 per P0595 even when ALLOW_NON_CONSTANT is true. */
7574 maybe_constant_init_1 (tree t
, tree decl
, bool allow_non_constant
,
7575 bool manifestly_const_eval
)
7579 if (TREE_CODE (t
) == EXPR_STMT
)
7580 t
= TREE_OPERAND (t
, 0);
7581 if (TREE_CODE (t
) == CONVERT_EXPR
7582 && VOID_TYPE_P (TREE_TYPE (t
)))
7583 t
= TREE_OPERAND (t
, 0);
7584 if (TREE_CODE (t
) == INIT_EXPR
)
7585 t
= TREE_OPERAND (t
, 1);
7586 if (TREE_CODE (t
) == TARGET_EXPR
)
7587 t
= TARGET_EXPR_INITIAL (t
);
7588 if (!is_nondependent_static_init_expression (t
))
7589 /* Don't try to evaluate it. */;
7590 else if (CONSTANT_CLASS_P (t
) && allow_non_constant
)
7591 /* No evaluation needed. */;
7593 t
= cxx_eval_outermost_constant_expr (t
, allow_non_constant
,
7595 manifestly_const_eval
, false, decl
);
7596 if (TREE_CODE (t
) == TARGET_EXPR
)
7598 tree init
= TARGET_EXPR_INITIAL (t
);
7599 if (TREE_CODE (init
) == CONSTRUCTOR
)
7605 /* Wrapper for maybe_constant_init_1 which permits non constants. */
7608 maybe_constant_init (tree t
, tree decl
, bool manifestly_const_eval
)
7610 return maybe_constant_init_1 (t
, decl
, true, manifestly_const_eval
);
7613 /* Wrapper for maybe_constant_init_1 which does not permit non constants. */
7616 cxx_constant_init (tree t
, tree decl
)
7618 return maybe_constant_init_1 (t
, decl
, false, true);
7622 /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
7623 /* Return true if the object referred to by REF has automatic or thread
7626 enum { ck_ok
, ck_bad
, ck_unknown
};
7628 check_automatic_or_tls (tree ref
)
7631 poly_int64 bitsize
, bitpos
;
7633 int volatilep
= 0, unsignedp
= 0;
7634 tree decl
= get_inner_reference (ref
, &bitsize
, &bitpos
, &offset
,
7635 &mode
, &unsignedp
, &volatilep
, false);
7638 /* If there isn't a decl in the middle, we don't know the linkage here,
7639 and this isn't a constant expression anyway. */
7642 dk
= decl_storage_duration (decl
);
7643 return (dk
== dk_auto
|| dk
== dk_thread
) ? ck_bad
: ck_ok
;
7647 /* Data structure for passing data from potential_constant_expression_1
7648 to check_for_return_continue via cp_walk_tree. */
7649 struct check_for_return_continue_data
{
7650 hash_set
<tree
> *pset
;
7655 /* Helper function for potential_constant_expression_1 SWITCH_STMT handling,
7656 called through cp_walk_tree. Return the first RETURN_EXPR found, or note
7657 the first CONTINUE_STMT and/or BREAK_STMT if RETURN_EXPR is not found. */
7659 check_for_return_continue (tree
*tp
, int *walk_subtrees
, void *data
)
7662 check_for_return_continue_data
*d
= (check_for_return_continue_data
*) data
;
7663 switch (TREE_CODE (t
))
7669 if (d
->continue_stmt
== NULL_TREE
)
7670 d
->continue_stmt
= t
;
7674 if (d
->break_stmt
== NULL_TREE
)
7679 if (tree r = cp_walk_tree (&x, check_for_return_continue, data, \
7683 /* For loops, walk subtrees manually, so that continue stmts found
7684 inside of the bodies of the loops are ignored. */
7687 RECUR (DO_COND (t
));
7688 s
= d
->continue_stmt
;
7690 RECUR (DO_BODY (t
));
7691 d
->continue_stmt
= s
;
7697 RECUR (WHILE_COND (t
));
7698 s
= d
->continue_stmt
;
7700 RECUR (WHILE_BODY (t
));
7701 d
->continue_stmt
= s
;
7707 RECUR (FOR_INIT_STMT (t
));
7708 RECUR (FOR_COND (t
));
7709 RECUR (FOR_EXPR (t
));
7710 s
= d
->continue_stmt
;
7712 RECUR (FOR_BODY (t
));
7713 d
->continue_stmt
= s
;
7717 case RANGE_FOR_STMT
:
7719 RECUR (RANGE_FOR_EXPR (t
));
7720 s
= d
->continue_stmt
;
7722 RECUR (RANGE_FOR_BODY (t
));
7723 d
->continue_stmt
= s
;
7729 RECUR (SWITCH_STMT_COND (t
));
7731 RECUR (SWITCH_STMT_BODY (t
));
7736 case STATEMENT_LIST
:
7749 /* Return true if T denotes a potentially constant expression. Issue
7750 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
7751 an lvalue-rvalue conversion is implied. If NOW is true, we want to
7752 consider the expression in the current context, independent of constexpr
7755 C++0x [expr.const] used to say
7757 6 An expression is a potential constant expression if it is
7758 a constant expression where all occurrences of function
7759 parameters are replaced by arbitrary constant expressions
7760 of the appropriate type.
7762 2 A conditional expression is a constant expression unless it
7763 involves one of the following as a potentially evaluated
7764 subexpression (3.2), but subexpressions of logical AND (5.14),
7765 logical OR (5.15), and conditional (5.16) operations that are
7766 not evaluated are not considered. */
7769 potential_constant_expression_1 (tree t
, bool want_rval
, bool strict
, bool now
,
7770 tsubst_flags_t flags
, tree
*jump_target
)
7772 #define RECUR(T,RV) \
7773 potential_constant_expression_1 ((T), (RV), strict, now, flags, jump_target)
7775 enum { any
= false, rval
= true };
7779 if (t
== error_mark_node
)
7783 location_t loc
= cp_expr_loc_or_input_loc (t
);
7786 /* If we are jumping, ignore everything. This is simpler than the
7787 cxx_eval_constant_expression handling because we only need to be
7788 conservatively correct, and we don't necessarily have a constant value
7789 available, so we don't bother with switch tracking. */
7792 if (TREE_THIS_VOLATILE (t
) && want_rval
)
7794 if (flags
& tf_error
)
7795 error_at (loc
, "lvalue-to-rvalue conversion of a volatile lvalue "
7796 "%qE with type %qT", t
, TREE_TYPE (t
));
7799 if (CONSTANT_CLASS_P (t
))
7801 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_TYPED
)
7802 && TREE_TYPE (t
) == error_mark_node
)
7805 switch (TREE_CODE (t
))
7811 case TEMPLATE_ID_EXPR
:
7813 case CASE_LABEL_EXPR
:
7820 case TEMPLATE_PARM_INDEX
:
7822 case IDENTIFIER_NODE
:
7823 case USERDEF_LITERAL
:
7824 /* We can see a FIELD_DECL in a pointer-to-member expression. */
7829 case PLACEHOLDER_EXPR
:
7832 case DEBUG_BEGIN_STMT
:
7836 if (!RECUR (TREE_OPERAND (t
, 0), any
))
7846 if (now
&& want_rval
)
7848 tree type
= TREE_TYPE (t
);
7849 if ((processing_template_decl
&& !COMPLETE_TYPE_P (type
))
7850 || dependent_type_p (type
)
7851 || is_really_empty_class (type
, /*ignore_vptr*/false))
7852 /* An empty class has no data to read. */
7854 if (flags
& tf_error
)
7855 error ("%qE is not a constant expression", t
);
7860 case AGGR_INIT_EXPR
:
7862 /* -- an invocation of a function other than a constexpr function
7863 or a constexpr constructor. */
7865 tree fun
= get_function_named_in_call (t
);
7866 const int nargs
= call_expr_nargs (t
);
7869 if (fun
== NULL_TREE
)
7871 /* Reset to allow the function to continue past the end
7872 of the block below. Otherwise return early. */
7875 if (TREE_CODE (t
) == CALL_EXPR
7876 && CALL_EXPR_FN (t
) == NULL_TREE
)
7877 switch (CALL_EXPR_IFN (t
))
7879 /* These should be ignored, they are optimized away from
7880 constexpr functions. */
7881 case IFN_UBSAN_NULL
:
7882 case IFN_UBSAN_BOUNDS
:
7883 case IFN_UBSAN_VPTR
:
7884 case IFN_FALLTHROUGH
:
7887 case IFN_ADD_OVERFLOW
:
7888 case IFN_SUB_OVERFLOW
:
7889 case IFN_MUL_OVERFLOW
:
7891 case IFN_VEC_CONVERT
:
7901 /* fold_call_expr can't do anything with IFN calls. */
7902 if (flags
& tf_error
)
7903 error_at (loc
, "call to internal function %qE", t
);
7908 if (fun
&& is_overloaded_fn (fun
))
7910 if (TREE_CODE (fun
) == FUNCTION_DECL
)
7912 if (builtin_valid_in_constant_expr_p (fun
))
7914 if (!DECL_DECLARED_CONSTEXPR_P (fun
)
7915 /* Allow any built-in function; if the expansion
7916 isn't constant, we'll deal with that then. */
7917 && !fndecl_built_in_p (fun
)
7918 /* In C++20, replaceable global allocation functions
7919 are constant expressions. */
7920 && (!cxx_replaceable_global_alloc_fn (fun
)
7921 || TREE_CODE (t
) != CALL_EXPR
7922 || (!CALL_FROM_NEW_OR_DELETE_P (t
)
7923 && (current_function_decl
== NULL_TREE
7924 || !is_std_allocator_allocate
7925 (current_function_decl
))))
7926 /* Allow placement new in std::construct_at. */
7927 && (!cxx_placement_new_fn (fun
)
7928 || TREE_CODE (t
) != CALL_EXPR
7929 || current_function_decl
== NULL_TREE
7930 || !is_std_construct_at (current_function_decl
))
7931 && !cxx_dynamic_cast_fn_p (fun
))
7933 if (flags
& tf_error
)
7935 error_at (loc
, "call to non-%<constexpr%> function %qD",
7937 explain_invalid_constexpr_fn (fun
);
7941 /* A call to a non-static member function takes the address
7942 of the object as the first argument. But in a constant
7943 expression the address will be folded away, so look
7945 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun
)
7946 && !DECL_CONSTRUCTOR_P (fun
))
7948 tree x
= get_nth_callarg (t
, 0);
7949 if (is_this_parameter (x
))
7951 /* Don't require an immediately constant value, as
7952 constexpr substitution might not use the value. */
7953 bool sub_now
= false;
7954 if (!potential_constant_expression_1 (x
, rval
, strict
,
7963 if (!RECUR (fun
, true))
7965 fun
= get_first_fn (fun
);
7967 /* Skip initial arguments to base constructors. */
7968 if (DECL_BASE_CONSTRUCTOR_P (fun
))
7969 i
= num_artificial_parms_for (fun
);
7970 fun
= DECL_ORIGIN (fun
);
7974 if (RECUR (fun
, rval
))
7975 /* Might end up being a constant function pointer. */;
7979 for (; i
< nargs
; ++i
)
7981 tree x
= get_nth_callarg (t
, i
);
7982 /* In a template, reference arguments haven't been converted to
7983 REFERENCE_TYPE and we might not even know if the parameter
7984 is a reference, so accept lvalue constants too. */
7985 bool rv
= processing_template_decl
? any
: rval
;
7986 /* Don't require an immediately constant value, as constexpr
7987 substitution might not use the value of the argument. */
7988 bool sub_now
= false;
7989 if (!potential_constant_expression_1 (x
, rv
, strict
,
7990 sub_now
, flags
, jump_target
))
7996 case NON_LVALUE_EXPR
:
7997 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
7998 -- an lvalue of integral type that refers to a non-volatile
7999 const variable or static data member initialized with
8000 constant expressions, or
8002 -- an lvalue of literal type that refers to non-volatile
8003 object defined with constexpr, or that refers to a
8004 sub-object of such an object; */
8005 return RECUR (TREE_OPERAND (t
, 0), rval
);
8008 if (DECL_HAS_VALUE_EXPR_P (t
))
8010 if (now
&& is_normal_capture_proxy (t
))
8012 /* -- in a lambda-expression, a reference to this or to a
8013 variable with automatic storage duration defined outside that
8014 lambda-expression, where the reference would be an
8018 /* Since we're doing an lvalue-rvalue conversion, this might
8019 not be an odr-use, so evaluate the variable directly. */
8020 return RECUR (DECL_CAPTURED_VARIABLE (t
), rval
);
8022 if (flags
& tf_error
)
8024 tree cap
= DECL_CAPTURED_VARIABLE (t
);
8025 error ("lambda capture of %qE is not a constant expression",
8027 if (decl_constant_var_p (cap
))
8028 inform (input_location
, "because it is used as a glvalue");
8032 /* Treat __PRETTY_FUNCTION__ inside a template function as
8033 potentially-constant. */
8034 else if (DECL_PRETTY_FUNCTION_P (t
)
8035 && DECL_VALUE_EXPR (t
) == error_mark_node
)
8037 return RECUR (DECL_VALUE_EXPR (t
), rval
);
8040 && !var_in_maybe_constexpr_fn (t
)
8041 && !type_dependent_expression_p (t
)
8042 && !decl_maybe_constant_var_p (t
)
8044 || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t
))
8045 || (DECL_INITIAL (t
)
8046 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t
)))
8047 && COMPLETE_TYPE_P (TREE_TYPE (t
))
8048 && !is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
8050 if (flags
& tf_error
)
8051 non_const_var_error (loc
, t
);
8057 if (REINTERPRET_CAST_P (t
))
8059 if (flags
& tf_error
)
8060 error_at (loc
, "%<reinterpret_cast%> is not a constant expression");
8065 case VIEW_CONVERT_EXPR
:
8066 /* -- a reinterpret_cast. FIXME not implemented, and this rule
8067 may change to something more specific to type-punning (DR 1312). */
8069 tree from
= TREE_OPERAND (t
, 0);
8070 if (location_wrapper_p (t
))
8071 return (RECUR (from
, want_rval
));
8072 if (INDIRECT_TYPE_P (TREE_TYPE (t
)))
8074 STRIP_ANY_LOCATION_WRAPPER (from
);
8075 if (TREE_CODE (from
) == INTEGER_CST
8076 && !integer_zerop (from
))
8078 if (flags
& tf_error
)
8080 "%<reinterpret_cast%> from integer to pointer");
8084 return (RECUR (from
, TREE_CODE (t
) != VIEW_CONVERT_EXPR
));
8087 case ADDRESSOF_EXPR
:
8088 /* This is like ADDR_EXPR, except it won't form pointer-to-member. */
8089 t
= TREE_OPERAND (t
, 0);
8090 goto handle_addr_expr
;
8093 /* -- a unary operator & that is applied to an lvalue that
8094 designates an object with thread or automatic storage
8096 t
= TREE_OPERAND (t
, 0);
8098 if (TREE_CODE (t
) == OFFSET_REF
&& PTRMEM_OK_P (t
))
8099 /* A pointer-to-member constant. */
8104 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
8105 any checking here, as we might dereference the pointer later. If
8106 we remove this code, also remove check_automatic_or_tls. */
8107 i
= check_automatic_or_tls (t
);
8112 if (flags
& tf_error
)
8113 error ("address-of an object %qE with thread local or "
8114 "automatic storage is not a constant expression", t
);
8118 return RECUR (t
, any
);
8123 /* -- a class member access unless its postfix-expression is
8124 of literal type or of pointer to literal type. */
8125 /* This test would be redundant, as it follows from the
8126 postfix-expression being a potential constant expression. */
8127 if (type_unknown_p (t
))
8129 if (is_overloaded_fn (t
))
8130 /* In a template, a COMPONENT_REF of a function expresses ob.fn(),
8131 which uses ob as an lvalue. */
8138 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
8140 case EXPR_PACK_EXPANSION
:
8141 return RECUR (PACK_EXPANSION_PATTERN (t
), want_rval
);
8145 tree x
= TREE_OPERAND (t
, 0);
8147 if (is_this_parameter (x
) && !is_capture_proxy (x
))
8149 if (!var_in_maybe_constexpr_fn (x
))
8151 if (flags
& tf_error
)
8152 error_at (loc
, "use of %<this%> in a constant expression");
8157 return RECUR (x
, rval
);
8160 case STATEMENT_LIST
:
8162 tree_stmt_iterator i
;
8163 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
8165 if (!RECUR (tsi_stmt (i
), any
))
8173 if (cxx_dialect
< cxx14
)
8175 if (!RECUR (TREE_OPERAND (t
, 0), any
))
8177 /* Just ignore clobbers. */
8178 if (TREE_CLOBBER_P (TREE_OPERAND (t
, 1)))
8180 if (!RECUR (TREE_OPERAND (t
, 1), rval
))
8185 if (cxx_dialect
< cxx14
)
8187 if (!RECUR (TREE_OPERAND (t
, 0), rval
))
8189 if (!RECUR (TREE_OPERAND (t
, 2), rval
))
8194 if (!RECUR (DO_COND (t
), rval
))
8196 if (!RECUR (DO_BODY (t
), any
))
8198 if (breaks (jump_target
) || continues (jump_target
))
8199 *jump_target
= NULL_TREE
;
8203 if (!RECUR (FOR_INIT_STMT (t
), any
))
8206 if (!RECUR (tmp
, rval
))
8210 if (!processing_template_decl
)
8211 tmp
= cxx_eval_outermost_constant_expr (tmp
, true);
8212 /* If we couldn't evaluate the condition, it might not ever be
8214 if (!integer_onep (tmp
))
8216 /* Before returning true, check if the for body can contain
8218 hash_set
<tree
> pset
;
8219 check_for_return_continue_data data
= { &pset
, NULL_TREE
,
8222 = cp_walk_tree (&FOR_BODY (t
), check_for_return_continue
,
8224 *jump_target
= ret_expr
;
8228 if (!RECUR (FOR_EXPR (t
), any
))
8230 if (!RECUR (FOR_BODY (t
), any
))
8232 if (breaks (jump_target
) || continues (jump_target
))
8233 *jump_target
= NULL_TREE
;
8236 case RANGE_FOR_STMT
:
8237 if (!RECUR (RANGE_FOR_INIT_STMT (t
), any
))
8239 if (!RECUR (RANGE_FOR_EXPR (t
), any
))
8241 if (!RECUR (RANGE_FOR_BODY (t
), any
))
8243 if (breaks (jump_target
) || continues (jump_target
))
8244 *jump_target
= NULL_TREE
;
8248 tmp
= WHILE_COND (t
);
8249 if (!RECUR (tmp
, rval
))
8251 if (!processing_template_decl
)
8252 tmp
= cxx_eval_outermost_constant_expr (tmp
, true);
8253 /* If we couldn't evaluate the condition, it might not ever be true. */
8254 if (!integer_onep (tmp
))
8256 /* Before returning true, check if the while body can contain
8258 hash_set
<tree
> pset
;
8259 check_for_return_continue_data data
= { &pset
, NULL_TREE
,
8262 = cp_walk_tree (&WHILE_BODY (t
), check_for_return_continue
,
8264 *jump_target
= ret_expr
;
8267 if (!RECUR (WHILE_BODY (t
), any
))
8269 if (breaks (jump_target
) || continues (jump_target
))
8270 *jump_target
= NULL_TREE
;
8274 if (!RECUR (SWITCH_STMT_COND (t
), rval
))
8276 /* FIXME we don't check SWITCH_STMT_BODY currently, because even
8277 unreachable labels would be checked and it is enough if there is
8278 a single switch cond value for which it is a valid constant
8279 expression. We need to check if there are any RETURN_EXPRs
8280 or CONTINUE_STMTs inside of the body though, as in that case
8281 we need to set *jump_target. */
8284 hash_set
<tree
> pset
;
8285 check_for_return_continue_data data
= { &pset
, NULL_TREE
,
8288 = cp_walk_tree (&SWITCH_STMT_BODY (t
), check_for_return_continue
,
8290 /* The switch might return. */
8291 *jump_target
= ret_expr
;
8292 else if (data
.continue_stmt
)
8293 /* The switch can't return, but might continue. */
8294 *jump_target
= data
.continue_stmt
;
8299 return RECUR (STMT_EXPR_STMT (t
), rval
);
8302 if (cxx_dialect
>= cxx17
)
8303 /* In C++17 lambdas can be constexpr, don't give up yet. */
8305 else if (flags
& tf_error
)
8306 error_at (loc
, "lambda-expression is not a constant expression "
8310 case DYNAMIC_CAST_EXPR
:
8311 case PSEUDO_DTOR_EXPR
:
8315 case VEC_DELETE_EXPR
:
8321 case OMP_DISTRIBUTE
:
8325 case OMP_TARGET_DATA
:
8334 case OMP_TARGET_UPDATE
:
8335 case OMP_TARGET_ENTER_DATA
:
8336 case OMP_TARGET_EXIT_DATA
:
8338 case OMP_ATOMIC_READ
:
8339 case OMP_ATOMIC_CAPTURE_OLD
:
8340 case OMP_ATOMIC_CAPTURE_NEW
:
8346 case OACC_HOST_DATA
:
8350 case OACC_ENTER_DATA
:
8351 case OACC_EXIT_DATA
:
8353 /* GCC internal stuff. */
8355 case TRANSACTION_EXPR
:
8356 case AT_ENCODE_EXPR
:
8358 if (flags
& tf_error
)
8359 error_at (loc
, "expression %qE is not a constant expression", t
);
8363 if (flags
& tf_error
)
8364 inline_asm_in_constexpr_error (loc
);
8368 if (cxx_dialect
>= cxx20
)
8369 /* In C++20 virtual calls can be constexpr, don't give up yet. */
8371 else if (flags
& tf_error
)
8373 "virtual functions cannot be %<constexpr%> before C++20");
8377 /* In C++20, a typeid expression whose operand is of polymorphic
8378 class type can be constexpr. */
8380 tree e
= TREE_OPERAND (t
, 0);
8381 if (cxx_dialect
< cxx20
8384 && !type_dependent_expression_p (e
)
8385 && TYPE_POLYMORPHIC_P (TREE_TYPE (e
)))
8387 if (flags
& tf_error
)
8388 error_at (loc
, "%<typeid%> is not a constant expression "
8389 "because %qE is of polymorphic type", e
);
8395 case POINTER_DIFF_EXPR
:
8406 case SPACESHIP_EXPR
:
8410 case PREINCREMENT_EXPR
:
8411 case POSTINCREMENT_EXPR
:
8412 case PREDECREMENT_EXPR
:
8413 case POSTDECREMENT_EXPR
:
8414 if (cxx_dialect
< cxx14
)
8420 if (TYPE_P (TREE_OPERAND (t
, 0)))
8426 case FIX_TRUNC_EXPR
:
8431 case TRUTH_NOT_EXPR
:
8432 case FIXED_CONVERT_EXPR
:
8433 case UNARY_PLUS_EXPR
:
8434 case UNARY_LEFT_FOLD_EXPR
:
8435 case UNARY_RIGHT_FOLD_EXPR
:
8437 return RECUR (TREE_OPERAND (t
, 0), rval
);
8440 case CONST_CAST_EXPR
:
8441 case STATIC_CAST_EXPR
:
8442 case REINTERPRET_CAST_EXPR
:
8443 case IMPLICIT_CONV_EXPR
:
8444 if (cxx_dialect
< cxx11
8445 && !dependent_type_p (TREE_TYPE (t
))
8446 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
)))
8447 /* In C++98, a conversion to non-integral type can't be part of a
8448 constant expression. */
8450 if (flags
& tf_error
)
8452 "cast to non-integral type %qT in a constant expression",
8456 /* This might be a conversion from a class to a (potentially) literal
8457 type. Let's consider it potentially constant since the conversion
8458 might be a constexpr user-defined conversion. */
8459 else if (cxx_dialect
>= cxx11
8460 && (dependent_type_p (TREE_TYPE (t
))
8461 || !COMPLETE_TYPE_P (TREE_TYPE (t
))
8462 || literal_type_p (TREE_TYPE (t
)))
8463 && TREE_OPERAND (t
, 0))
8465 tree type
= TREE_TYPE (TREE_OPERAND (t
, 0));
8466 /* If this is a dependent type, it could end up being a class
8467 with conversions. */
8468 if (type
== NULL_TREE
|| WILDCARD_TYPE_P (type
))
8470 /* Or a non-dependent class which has conversions. */
8471 else if (CLASS_TYPE_P (type
)
8472 && (TYPE_HAS_CONVERSION (type
) || dependent_scope_p (type
)))
8476 return (RECUR (TREE_OPERAND (t
, 0),
8477 !TYPE_REF_P (TREE_TYPE (t
))));
8480 return RECUR (BIND_EXPR_BODY (t
), want_rval
);
8482 case CLEANUP_POINT_EXPR
:
8483 case MUST_NOT_THROW_EXPR
:
8484 case TRY_CATCH_EXPR
:
8489 case NON_DEPENDENT_EXPR
:
8490 /* For convenience. */
8493 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
8496 tmp
= DECL_EXPR_DECL (t
);
8497 if (VAR_P (tmp
) && !DECL_ARTIFICIAL (tmp
))
8499 if (TREE_STATIC (tmp
))
8501 if (flags
& tf_error
)
8502 error_at (DECL_SOURCE_LOCATION (tmp
), "%qD declared "
8503 "%<static%> in %<constexpr%> context", tmp
);
8506 else if (CP_DECL_THREAD_LOCAL_P (tmp
))
8508 if (flags
& tf_error
)
8509 error_at (DECL_SOURCE_LOCATION (tmp
), "%qD declared "
8510 "%<thread_local%> in %<constexpr%> context", tmp
);
8513 else if (!check_for_uninitialized_const_var
8514 (tmp
, /*constexpr_context_p=*/true, flags
))
8517 return RECUR (tmp
, want_rval
);
8519 case TRY_FINALLY_EXPR
:
8520 return (RECUR (TREE_OPERAND (t
, 0), want_rval
)
8521 && RECUR (TREE_OPERAND (t
, 1), any
));
8524 return RECUR (TREE_OPERAND (t
, 1), want_rval
);
8527 if (!TARGET_EXPR_DIRECT_INIT_P (t
)
8528 && !literal_type_p (TREE_TYPE (t
)))
8530 if (flags
& tf_error
)
8532 auto_diagnostic_group d
;
8533 error_at (loc
, "temporary of non-literal type %qT in a "
8534 "constant expression", TREE_TYPE (t
));
8535 explain_non_literal_class (TREE_TYPE (t
));
8541 return RECUR (TREE_OPERAND (t
, 1), rval
);
8545 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (t
);
8546 constructor_elt
*ce
;
8547 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
8548 if (!RECUR (ce
->value
, want_rval
))
8555 gcc_assert (TREE_PURPOSE (t
) == NULL_TREE
8556 || DECL_P (TREE_PURPOSE (t
)));
8557 if (!RECUR (TREE_VALUE (t
), want_rval
))
8559 if (TREE_CHAIN (t
) == NULL_TREE
)
8561 return RECUR (TREE_CHAIN (t
), want_rval
);
8564 case TRUNC_DIV_EXPR
:
8566 case FLOOR_DIV_EXPR
:
8567 case ROUND_DIV_EXPR
:
8568 case TRUNC_MOD_EXPR
:
8570 case ROUND_MOD_EXPR
:
8572 tree denom
= TREE_OPERAND (t
, 1);
8573 if (!RECUR (denom
, rval
))
8575 /* We can't call cxx_eval_outermost_constant_expr on an expression
8576 that hasn't been through instantiate_non_dependent_expr yet. */
8577 if (!processing_template_decl
)
8578 denom
= cxx_eval_outermost_constant_expr (denom
, true);
8579 if (integer_zerop (denom
))
8581 if (flags
& tf_error
)
8582 error ("division by zero is not a constant expression");
8588 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
8594 /* check_return_expr sometimes wraps a TARGET_EXPR in a
8595 COMPOUND_EXPR; don't get confused. */
8596 tree op0
= TREE_OPERAND (t
, 0);
8597 tree op1
= TREE_OPERAND (t
, 1);
8599 if (TREE_CODE (op0
) == TARGET_EXPR
&& op1
== TARGET_EXPR_SLOT (op0
))
8600 return RECUR (op0
, want_rval
);
8605 /* If the first operand is the non-short-circuit constant, look at
8606 the second operand; otherwise we only care about the first one for
8608 case TRUTH_AND_EXPR
:
8609 case TRUTH_ANDIF_EXPR
:
8610 tmp
= boolean_true_node
;
8613 case TRUTH_ORIF_EXPR
:
8614 tmp
= boolean_false_node
;
8617 tree op
= TREE_OPERAND (t
, 0);
8618 if (!RECUR (op
, rval
))
8620 if (!processing_template_decl
)
8621 op
= cxx_eval_outermost_constant_expr (op
, true);
8622 if (tree_int_cst_equal (op
, tmp
))
8623 return RECUR (TREE_OPERAND (t
, 1), rval
);
8630 case POINTER_PLUS_EXPR
:
8632 case EXACT_DIV_EXPR
:
8642 case TRUTH_XOR_EXPR
:
8643 case UNORDERED_EXPR
:
8656 case ARRAY_RANGE_REF
:
8660 case BINARY_LEFT_FOLD_EXPR
:
8661 case BINARY_RIGHT_FOLD_EXPR
:
8663 for (i
= 0; i
< 2; ++i
)
8664 if (!RECUR (TREE_OPERAND (t
, i
), want_rval
))
8669 for (i
= 0; i
< 3; ++i
)
8670 if (!RECUR (TREE_OPERAND (t
, i
), true))
8675 if (COND_EXPR_IS_VEC_DELETE (t
) && cxx_dialect
< cxx20
)
8677 if (flags
& tf_error
)
8678 error_at (loc
, "%<delete[]%> is not a constant expression");
8684 /* If the condition is a known constant, we know which of the legs we
8685 care about; otherwise we only require that the condition and
8686 either of the legs be potentially constant. */
8687 tmp
= TREE_OPERAND (t
, 0);
8688 if (!RECUR (tmp
, rval
))
8690 if (!processing_template_decl
)
8691 tmp
= cxx_eval_outermost_constant_expr (tmp
, true);
8692 if (integer_zerop (tmp
))
8693 return RECUR (TREE_OPERAND (t
, 2), want_rval
);
8694 else if (TREE_CODE (tmp
) == INTEGER_CST
)
8695 return RECUR (TREE_OPERAND (t
, 1), want_rval
);
8697 for (i
= 1; i
< 3; ++i
)
8699 tree this_jump_target
= tmp
;
8700 if (potential_constant_expression_1 (TREE_OPERAND (t
, i
),
8701 want_rval
, strict
, now
,
8702 tf_none
, &this_jump_target
))
8704 if (returns (&this_jump_target
))
8705 *jump_target
= this_jump_target
;
8706 else if (!returns (jump_target
))
8708 if (breaks (&this_jump_target
)
8709 || continues (&this_jump_target
))
8710 *jump_target
= this_jump_target
;
8713 /* If the then branch is potentially constant, but
8714 does not return, check if the else branch
8715 couldn't return, break or continue. */
8716 hash_set
<tree
> pset
;
8717 check_for_return_continue_data data
= { &pset
, NULL_TREE
,
8720 = cp_walk_tree (&TREE_OPERAND (t
, 2),
8721 check_for_return_continue
, &data
,
8723 *jump_target
= ret_expr
;
8724 else if (*jump_target
== NULL_TREE
)
8726 if (data
.continue_stmt
)
8727 *jump_target
= data
.continue_stmt
;
8728 else if (data
.break_stmt
)
8729 *jump_target
= data
.break_stmt
;
8736 if (flags
& tf_error
)
8737 error_at (loc
, "expression %qE is not a constant expression", t
);
8741 if (VEC_INIT_EXPR_IS_CONSTEXPR (t
))
8743 if (flags
& tf_error
)
8745 error_at (loc
, "non-constant array initialization");
8746 diagnose_non_constexpr_vec_init (t
);
8752 /* We can see these in statement-expressions. */
8756 if (!RECUR (CLEANUP_BODY (t
), any
))
8758 if (!CLEANUP_EH_ONLY (t
) && !RECUR (CLEANUP_EXPR (t
), any
))
8762 case EMPTY_CLASS_EXPR
:
8767 tree
*target
= &TREE_OPERAND (t
, 0);
8768 /* Gotos representing break and continue are OK. */
8769 if (breaks (target
) || continues (target
))
8771 *jump_target
= *target
;
8774 if (flags
& tf_error
)
8775 error_at (loc
, "%<goto%> is not a constant expression");
8780 t
= LABEL_EXPR_LABEL (t
);
8781 if (DECL_ARTIFICIAL (t
))
8783 else if (flags
& tf_error
)
8784 error_at (loc
, "label definition is not a constant expression");
8788 return RECUR (TREE_OPERAND (t
, 0), rval
);
8791 return RECUR (TREE_OPERAND (t
, 0), rval
);
8793 /* Coroutine await, yield and return expressions are not. */
8796 case CO_RETURN_EXPR
:
8800 if (objc_non_constant_expr_p (t
))
8803 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t
)));
8811 potential_constant_expression_1 (tree t
, bool want_rval
, bool strict
, bool now
,
8812 tsubst_flags_t flags
)
8814 tree target
= NULL_TREE
;
8815 return potential_constant_expression_1 (t
, want_rval
, strict
, now
,
8819 /* The main entry point to the above. */
8822 potential_constant_expression (tree t
)
8824 return potential_constant_expression_1 (t
, false, true, false, tf_none
);
8827 /* As above, but require a constant rvalue. */
8830 potential_rvalue_constant_expression (tree t
)
8832 return potential_constant_expression_1 (t
, true, true, false, tf_none
);
8835 /* Like above, but complain about non-constant expressions. */
8838 require_potential_constant_expression (tree t
)
8840 return potential_constant_expression_1 (t
, false, true, false,
8841 tf_warning_or_error
);
8844 /* Cross product of the above. */
8847 require_potential_rvalue_constant_expression (tree t
)
8849 return potential_constant_expression_1 (t
, true, true, false,
8850 tf_warning_or_error
);
8853 /* Like above, but don't consider PARM_DECL a potential_constant_expression. */
8856 require_rvalue_constant_expression (tree t
)
8858 return potential_constant_expression_1 (t
, true, true, true,
8859 tf_warning_or_error
);
8862 /* Like potential_constant_expression, but don't consider possible constexpr
8863 substitution of the current function. That is, PARM_DECL qualifies under
8864 potential_constant_expression, but not here.
8866 This is basically what you can check when any actual constant values might
8867 be value-dependent. */
8870 is_constant_expression (tree t
)
8872 return potential_constant_expression_1 (t
, false, true, true, tf_none
);
8875 /* As above, but expect an rvalue. */
8878 is_rvalue_constant_expression (tree t
)
8880 return potential_constant_expression_1 (t
, true, true, true, tf_none
);
8883 /* Like above, but complain about non-constant expressions. */
8886 require_constant_expression (tree t
)
8888 return potential_constant_expression_1 (t
, false, true, true,
8889 tf_warning_or_error
);
8892 /* Like is_constant_expression, but allow const variables that are not allowed
8893 under constexpr rules. */
8896 is_static_init_expression (tree t
)
8898 return potential_constant_expression_1 (t
, false, false, true, tf_none
);
8901 /* Returns true if T is a potential constant expression that is not
8902 instantiation-dependent, and therefore a candidate for constant folding even
8906 is_nondependent_constant_expression (tree t
)
8908 return (!type_unknown_p (t
)
8909 && is_constant_expression (t
)
8910 && !instantiation_dependent_expression_p (t
));
8913 /* Returns true if T is a potential static initializer expression that is not
8914 instantiation-dependent. */
8917 is_nondependent_static_init_expression (tree t
)
8919 return (!type_unknown_p (t
)
8920 && is_static_init_expression (t
)
8921 && !instantiation_dependent_expression_p (t
));
8924 /* Finalize constexpr processing after parsing. */
8927 fini_constexpr (void)
8929 /* The contexpr call and fundef copies tables are no longer needed. */
8930 constexpr_call_table
= NULL
;
8931 fundef_copies_table
= NULL
;
8934 #include "gt-cp-constexpr.h"