1 /* Perform -*- C++ -*- constant expression evaluation, including calls to
2 constexpr functions. These routines are used both during actual parsing
3 and during the instantiation of template functions.
5 Copyright (C) 1998-2020 Free Software Foundation, Inc.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
14 GCC is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
25 #include "coretypes.h"
28 #include "c-family/c-objc.h"
29 #include "tree-iterator.h"
32 #include "tree-inline.h"
34 #include "gimple-fold.h"
36 #include "fold-const-call.h"
37 #include "stor-layout.h"
39 static bool verify_constant (tree
, bool, bool *, bool *);
40 #define VERIFY_CONSTANT(X) \
42 if (verify_constant ((X), ctx->quiet, non_constant_p, overflow_p)) \
46 static HOST_WIDE_INT
find_array_ctor_elt (tree ary
, tree dindex
,
49 /* Returns true iff FUN is an instantiation of a constexpr function
50 template or a defaulted constexpr function. */
53 is_instantiation_of_constexpr (tree fun
)
55 return ((DECL_TEMPLOID_INSTANTIATION (fun
)
56 && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun
)))
57 || (DECL_DEFAULTED_FN (fun
)
58 && DECL_DECLARED_CONSTEXPR_P (fun
)));
61 /* Return true if T is a literal type. */
64 literal_type_p (tree t
)
69 || (VOID_TYPE_P (t
) && cxx_dialect
>= cxx14
))
73 t
= complete_type (t
);
74 gcc_assert (COMPLETE_TYPE_P (t
) || errorcount
);
75 return CLASSTYPE_LITERAL_P (t
);
77 if (TREE_CODE (t
) == ARRAY_TYPE
)
78 return literal_type_p (strip_array_types (t
));
82 /* If DECL is a variable declared `constexpr', require its type
83 be literal. Return error_mark_node if we give an error, the
87 ensure_literal_type_for_constexpr_object (tree decl
)
89 tree type
= TREE_TYPE (decl
);
91 && (DECL_DECLARED_CONSTEXPR_P (decl
)
92 || var_in_constexpr_fn (decl
))
93 && !processing_template_decl
)
95 tree stype
= strip_array_types (type
);
96 if (CLASS_TYPE_P (stype
) && !COMPLETE_TYPE_P (complete_type (stype
)))
97 /* Don't complain here, we'll complain about incompleteness
98 when we try to initialize the variable. */;
99 else if (type_uses_auto (type
))
100 /* We don't know the actual type yet. */;
101 else if (!literal_type_p (type
))
103 if (DECL_DECLARED_CONSTEXPR_P (decl
))
105 auto_diagnostic_group d
;
106 error_at (DECL_SOURCE_LOCATION (decl
),
107 "the type %qT of %<constexpr%> variable %qD "
108 "is not literal", type
, decl
);
109 explain_non_literal_class (type
);
110 decl
= error_mark_node
;
114 if (!is_instantiation_of_constexpr (current_function_decl
))
116 auto_diagnostic_group d
;
117 error_at (DECL_SOURCE_LOCATION (decl
),
118 "variable %qD of non-literal type %qT in "
119 "%<constexpr%> function", decl
, type
);
120 explain_non_literal_class (type
);
121 decl
= error_mark_node
;
123 cp_function_chain
->invalid_constexpr
= true;
126 else if (DECL_DECLARED_CONSTEXPR_P (decl
)
127 && variably_modified_type_p (type
, NULL_TREE
))
129 error_at (DECL_SOURCE_LOCATION (decl
),
130 "%<constexpr%> variable %qD has variably-modified "
131 "type %qT", decl
, type
);
132 decl
= error_mark_node
;
138 /* Representation of entries in the constexpr function definition table. */
140 struct GTY((for_user
)) constexpr_fundef
{
147 struct constexpr_fundef_hasher
: ggc_ptr_hash
<constexpr_fundef
>
149 static hashval_t
hash (constexpr_fundef
*);
150 static bool equal (constexpr_fundef
*, constexpr_fundef
*);
153 /* This table holds all constexpr function definitions seen in
154 the current translation unit. */
156 static GTY (()) hash_table
<constexpr_fundef_hasher
> *constexpr_fundef_table
;
158 /* Utility function used for managing the constexpr function table.
159 Return true if the entries pointed to by P and Q are for the
160 same constexpr function. */
163 constexpr_fundef_hasher::equal (constexpr_fundef
*lhs
, constexpr_fundef
*rhs
)
165 return lhs
->decl
== rhs
->decl
;
168 /* Utility function used for managing the constexpr function table.
169 Return a hash value for the entry pointed to by Q. */
172 constexpr_fundef_hasher::hash (constexpr_fundef
*fundef
)
174 return DECL_UID (fundef
->decl
);
177 /* Return a previously saved definition of function FUN. */
179 static constexpr_fundef
*
180 retrieve_constexpr_fundef (tree fun
)
182 if (constexpr_fundef_table
== NULL
)
185 constexpr_fundef fundef
= { fun
, NULL
, NULL
, NULL
};
186 return constexpr_fundef_table
->find (&fundef
);
189 /* Check whether the parameter and return types of FUN are valid for a
190 constexpr function, and complain if COMPLAIN. */
193 is_valid_constexpr_fn (tree fun
, bool complain
)
197 if (DECL_INHERITED_CTOR (fun
)
198 && TREE_CODE (fun
) == TEMPLATE_DECL
)
202 error ("inherited constructor %qD is not %<constexpr%>",
203 DECL_INHERITED_CTOR (fun
));
207 for (tree parm
= FUNCTION_FIRST_USER_PARM (fun
);
208 parm
!= NULL_TREE
; parm
= TREE_CHAIN (parm
))
209 if (!literal_type_p (TREE_TYPE (parm
)))
214 auto_diagnostic_group d
;
215 error ("invalid type for parameter %d of %<constexpr%> "
216 "function %q+#D", DECL_PARM_INDEX (parm
), fun
);
217 explain_non_literal_class (TREE_TYPE (parm
));
222 if (LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun
)) && cxx_dialect
< cxx17
)
226 inform (DECL_SOURCE_LOCATION (fun
),
227 "lambdas are implicitly %<constexpr%> only in C++17 and later");
229 else if (!DECL_CONSTRUCTOR_P (fun
))
231 tree rettype
= TREE_TYPE (TREE_TYPE (fun
));
232 if (!literal_type_p (rettype
))
237 auto_diagnostic_group d
;
238 error ("invalid return type %qT of %<constexpr%> function %q+D",
240 explain_non_literal_class (rettype
);
244 /* C++14 DR 1684 removed this restriction. */
245 if (cxx_dialect
< cxx14
246 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fun
)
247 && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun
)))
252 auto_diagnostic_group d
;
253 if (pedwarn (DECL_SOURCE_LOCATION (fun
), OPT_Wpedantic
,
254 "enclosing class of %<constexpr%> non-static"
255 " member function %q+#D is not a literal type",
257 explain_non_literal_class (DECL_CONTEXT (fun
));
261 else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun
)))
265 error ("%q#T has virtual base classes", DECL_CONTEXT (fun
));
271 /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF
272 for a member of an anonymous aggregate, INIT is the initializer for that
273 member, and VEC_OUTER is the vector of constructor elements for the class
274 whose constructor we are processing. Add the initializer to the vector
275 and return true to indicate success. */
278 build_anon_member_initialization (tree member
, tree init
,
279 vec
<constructor_elt
, va_gc
> **vec_outer
)
281 /* MEMBER presents the relevant fields from the inside out, but we need
282 to build up the initializer from the outside in so that we can reuse
283 previously built CONSTRUCTORs if this is, say, the second field in an
284 anonymous struct. So we use a vec as a stack. */
285 auto_vec
<tree
, 2> fields
;
288 fields
.safe_push (TREE_OPERAND (member
, 1));
289 member
= TREE_OPERAND (member
, 0);
291 while (ANON_AGGR_TYPE_P (TREE_TYPE (member
))
292 && TREE_CODE (member
) == COMPONENT_REF
);
294 /* VEC has the constructor elements vector for the context of FIELD.
295 If FIELD is an anonymous aggregate, we will push inside it. */
296 vec
<constructor_elt
, va_gc
> **vec
= vec_outer
;
298 while (field
= fields
.pop(),
299 ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
302 /* If there is already an outer constructor entry for the anonymous
303 aggregate FIELD, use it; otherwise, insert one. */
304 if (vec_safe_is_empty (*vec
)
305 || (*vec
)->last().index
!= field
)
307 ctor
= build_constructor (TREE_TYPE (field
), NULL
);
308 CONSTRUCTOR_APPEND_ELT (*vec
, field
, ctor
);
311 ctor
= (*vec
)->last().value
;
312 vec
= &CONSTRUCTOR_ELTS (ctor
);
315 /* Now we're at the innermost field, the one that isn't an anonymous
316 aggregate. Add its initializer to the CONSTRUCTOR and we're done. */
317 gcc_assert (fields
.is_empty());
318 CONSTRUCTOR_APPEND_ELT (*vec
, field
, init
);
323 /* Subroutine of build_constexpr_constructor_member_initializers.
324 The expression tree T represents a data member initialization
325 in a (constexpr) constructor definition. Build a pairing of
326 the data member with its initializer, and prepend that pair
327 to the existing initialization pair INITS. */
330 build_data_member_initialization (tree t
, vec
<constructor_elt
, va_gc
> **vec
)
333 if (TREE_CODE (t
) == CLEANUP_POINT_EXPR
)
334 t
= TREE_OPERAND (t
, 0);
335 if (TREE_CODE (t
) == EXPR_STMT
)
336 t
= TREE_OPERAND (t
, 0);
337 if (t
== error_mark_node
)
339 if (TREE_CODE (t
) == STATEMENT_LIST
)
341 tree_stmt_iterator i
;
342 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
344 if (! build_data_member_initialization (tsi_stmt (i
), vec
))
349 if (TREE_CODE (t
) == CLEANUP_STMT
)
351 /* We can't see a CLEANUP_STMT in a constructor for a literal class,
352 but we can in a constexpr constructor for a non-literal class. Just
353 ignore it; either all the initialization will be constant, in which
354 case the cleanup can't run, or it can't be constexpr.
355 Still recurse into CLEANUP_BODY. */
356 return build_data_member_initialization (CLEANUP_BODY (t
), vec
);
358 if (TREE_CODE (t
) == CONVERT_EXPR
)
359 t
= TREE_OPERAND (t
, 0);
360 if (TREE_CODE (t
) == INIT_EXPR
361 /* vptr initialization shows up as a MODIFY_EXPR. In C++14 we only
362 use what this function builds for cx_check_missing_mem_inits, and
363 assignment in the ctor body doesn't count. */
364 || (cxx_dialect
< cxx14
&& TREE_CODE (t
) == MODIFY_EXPR
))
366 member
= TREE_OPERAND (t
, 0);
367 init
= break_out_target_exprs (TREE_OPERAND (t
, 1));
369 else if (TREE_CODE (t
) == CALL_EXPR
)
371 tree fn
= get_callee_fndecl (t
);
372 if (!fn
|| !DECL_CONSTRUCTOR_P (fn
))
373 /* We're only interested in calls to subobject constructors. */
375 member
= CALL_EXPR_ARG (t
, 0);
376 /* We don't use build_cplus_new here because it complains about
377 abstract bases. Leaving the call unwrapped means that it has the
378 wrong type, but cxx_eval_constant_expression doesn't care. */
379 init
= break_out_target_exprs (t
);
381 else if (TREE_CODE (t
) == BIND_EXPR
)
382 return build_data_member_initialization (BIND_EXPR_BODY (t
), vec
);
384 /* Don't add anything else to the CONSTRUCTOR. */
386 if (INDIRECT_REF_P (member
))
387 member
= TREE_OPERAND (member
, 0);
388 if (TREE_CODE (member
) == NOP_EXPR
)
392 if (TREE_CODE (op
) == ADDR_EXPR
)
394 gcc_assert (same_type_ignoring_top_level_qualifiers_p
395 (TREE_TYPE (TREE_TYPE (op
)),
396 TREE_TYPE (TREE_TYPE (member
))));
397 /* Initializing a cv-qualified member; we need to look through
401 else if (op
== current_class_ptr
402 && (same_type_ignoring_top_level_qualifiers_p
403 (TREE_TYPE (TREE_TYPE (member
)),
404 current_class_type
)))
405 /* Delegating constructor. */
409 /* This is an initializer for an empty base; keep it for now so
410 we can check it in cxx_eval_bare_aggregate. */
411 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member
))));
414 if (TREE_CODE (member
) == ADDR_EXPR
)
415 member
= TREE_OPERAND (member
, 0);
416 if (TREE_CODE (member
) == COMPONENT_REF
)
418 tree aggr
= TREE_OPERAND (member
, 0);
419 if (TREE_CODE (aggr
) == VAR_DECL
)
420 /* Initializing a local variable, don't add anything. */
422 if (TREE_CODE (aggr
) != COMPONENT_REF
)
423 /* Normal member initialization. */
424 member
= TREE_OPERAND (member
, 1);
425 else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr
)))
426 /* Initializing a member of an anonymous union. */
427 return build_anon_member_initialization (member
, init
, vec
);
429 /* We're initializing a vtable pointer in a base. Leave it as
430 COMPONENT_REF so we remember the path to get to the vfield. */
431 gcc_assert (TREE_TYPE (member
) == vtbl_ptr_type_node
);
434 /* Value-initialization can produce multiple initializers for the
435 same field; use the last one. */
436 if (!vec_safe_is_empty (*vec
) && (*vec
)->last().index
== member
)
437 (*vec
)->last().value
= init
;
439 CONSTRUCTOR_APPEND_ELT (*vec
, member
, init
);
443 /* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval.
444 In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a
445 BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */
448 check_constexpr_bind_expr_vars (tree t
)
450 gcc_assert (TREE_CODE (t
) == BIND_EXPR
);
452 for (tree var
= BIND_EXPR_VARS (t
); var
; var
= DECL_CHAIN (var
))
453 if (TREE_CODE (var
) == TYPE_DECL
454 && DECL_IMPLICIT_TYPEDEF_P (var
)
455 && !LAMBDA_TYPE_P (TREE_TYPE (var
)))
460 /* Subroutine of check_constexpr_ctor_body. */
463 check_constexpr_ctor_body_1 (tree last
, tree list
)
465 switch (TREE_CODE (list
))
468 if (TREE_CODE (DECL_EXPR_DECL (list
)) == USING_DECL
469 || TREE_CODE (DECL_EXPR_DECL (list
)) == TYPE_DECL
)
473 case CLEANUP_POINT_EXPR
:
474 return check_constexpr_ctor_body (last
, TREE_OPERAND (list
, 0),
478 if (!check_constexpr_bind_expr_vars (list
)
479 || !check_constexpr_ctor_body (last
, BIND_EXPR_BODY (list
),
486 case DEBUG_BEGIN_STMT
:
494 /* Make sure that there are no statements after LAST in the constructor
495 body represented by LIST. */
498 check_constexpr_ctor_body (tree last
, tree list
, bool complain
)
500 /* C++14 doesn't require a constexpr ctor to have an empty body. */
501 if (cxx_dialect
>= cxx14
)
505 if (TREE_CODE (list
) == STATEMENT_LIST
)
507 tree_stmt_iterator i
= tsi_last (list
);
508 for (; !tsi_end_p (i
); tsi_prev (&i
))
510 tree t
= tsi_stmt (i
);
513 if (!check_constexpr_ctor_body_1 (last
, t
))
520 else if (list
!= last
521 && !check_constexpr_ctor_body_1 (last
, list
))
526 error ("%<constexpr%> constructor does not have empty body");
527 DECL_DECLARED_CONSTEXPR_P (current_function_decl
) = false;
532 /* V is a vector of constructor elements built up for the base and member
533 initializers of a constructor for TYPE. They need to be in increasing
534 offset order, which they might not be yet if TYPE has a primary base
535 which is not first in the base-clause or a vptr and at least one base
536 all of which are non-primary. */
538 static vec
<constructor_elt
, va_gc
> *
539 sort_constexpr_mem_initializers (tree type
, vec
<constructor_elt
, va_gc
> *v
)
541 tree pri
= CLASSTYPE_PRIMARY_BINFO (type
);
547 field_type
= BINFO_TYPE (pri
);
548 else if (TYPE_CONTAINS_VPTR_P (type
))
549 field_type
= vtbl_ptr_type_node
;
553 /* Find the element for the primary base or vptr and move it to the
554 beginning of the vec. */
555 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
556 if (TREE_TYPE (ce
->index
) == field_type
)
559 if (i
> 0 && i
< vec_safe_length (v
))
561 vec
<constructor_elt
, va_gc
> &vref
= *v
;
562 constructor_elt elt
= vref
[i
];
571 /* Build compile-time evalable representations of member-initializer list
572 for a constexpr constructor. */
575 build_constexpr_constructor_member_initializers (tree type
, tree body
)
577 vec
<constructor_elt
, va_gc
> *vec
= NULL
;
580 switch (TREE_CODE (body
))
582 case MUST_NOT_THROW_EXPR
:
584 body
= TREE_OPERAND (body
, 0);
588 for (tree_stmt_iterator i
= tsi_start (body
);
589 !tsi_end_p (i
); tsi_next (&i
))
592 if (TREE_CODE (body
) == BIND_EXPR
)
598 body
= BIND_EXPR_BODY (body
);
605 if (TREE_CODE (body
) == TRY_BLOCK
)
607 body
= TREE_OPERAND (body
, 0);
608 if (TREE_CODE (body
) == BIND_EXPR
)
609 body
= BIND_EXPR_BODY (body
);
611 if (TREE_CODE (body
) == CLEANUP_POINT_EXPR
)
613 body
= TREE_OPERAND (body
, 0);
614 if (TREE_CODE (body
) == EXPR_STMT
)
615 body
= TREE_OPERAND (body
, 0);
616 if (TREE_CODE (body
) == INIT_EXPR
617 && (same_type_ignoring_top_level_qualifiers_p
618 (TREE_TYPE (TREE_OPERAND (body
, 0)),
619 current_class_type
)))
622 return TREE_OPERAND (body
, 1);
624 ok
= build_data_member_initialization (body
, &vec
);
626 else if (TREE_CODE (body
) == STATEMENT_LIST
)
628 tree_stmt_iterator i
;
629 for (i
= tsi_start (body
); !tsi_end_p (i
); tsi_next (&i
))
631 ok
= build_data_member_initialization (tsi_stmt (i
), &vec
);
636 else if (EXPR_P (body
))
637 ok
= build_data_member_initialization (body
, &vec
);
639 gcc_assert (errorcount
> 0);
642 if (vec_safe_length (vec
) > 0)
644 /* In a delegating constructor, return the target. */
645 constructor_elt
*ce
= &(*vec
)[0];
646 if (ce
->index
== current_class_ptr
)
653 vec
= sort_constexpr_mem_initializers (type
, vec
);
654 return build_constructor (type
, vec
);
657 return error_mark_node
;
660 /* We have an expression tree T that represents a call, either CALL_EXPR
661 or AGGR_INIT_EXPR. If the call is lexically to a named function,
662 retrun the _DECL for that function. */
665 get_function_named_in_call (tree t
)
667 tree fun
= cp_get_callee (t
);
668 if (fun
&& TREE_CODE (fun
) == ADDR_EXPR
669 && TREE_CODE (TREE_OPERAND (fun
, 0)) == FUNCTION_DECL
)
670 fun
= TREE_OPERAND (fun
, 0);
674 /* Subroutine of register_constexpr_fundef. BODY is the body of a function
675 declared to be constexpr, or a sub-statement thereof. Returns the
676 return value if suitable, error_mark_node for a statement not allowed in
677 a constexpr function, or NULL_TREE if no return value was found. */
680 constexpr_fn_retval (tree body
)
682 switch (TREE_CODE (body
))
686 tree_stmt_iterator i
;
687 tree expr
= NULL_TREE
;
688 for (i
= tsi_start (body
); !tsi_end_p (i
); tsi_next (&i
))
690 tree s
= constexpr_fn_retval (tsi_stmt (i
));
691 if (s
== error_mark_node
)
692 return error_mark_node
;
693 else if (s
== NULL_TREE
)
694 /* Keep iterating. */;
696 /* Multiple return statements. */
697 return error_mark_node
;
705 return break_out_target_exprs (TREE_OPERAND (body
, 0));
709 tree decl
= DECL_EXPR_DECL (body
);
710 if (TREE_CODE (decl
) == USING_DECL
711 /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */
712 || DECL_ARTIFICIAL (decl
))
714 return error_mark_node
;
717 case CLEANUP_POINT_EXPR
:
718 return constexpr_fn_retval (TREE_OPERAND (body
, 0));
721 if (!check_constexpr_bind_expr_vars (body
))
722 return error_mark_node
;
723 return constexpr_fn_retval (BIND_EXPR_BODY (body
));
726 case DEBUG_BEGIN_STMT
:
731 tree fun
= get_function_named_in_call (body
);
733 && fndecl_built_in_p (fun
, BUILT_IN_UNREACHABLE
))
739 return error_mark_node
;
743 /* Subroutine of register_constexpr_fundef. BODY is the DECL_SAVED_TREE of
744 FUN; do the necessary transformations to turn it into a single expression
745 that we can store in the hash table. */
748 massage_constexpr_body (tree fun
, tree body
)
750 if (DECL_CONSTRUCTOR_P (fun
))
751 body
= build_constexpr_constructor_member_initializers
752 (DECL_CONTEXT (fun
), body
);
753 else if (cxx_dialect
< cxx14
)
755 if (TREE_CODE (body
) == EH_SPEC_BLOCK
)
756 body
= EH_SPEC_STMTS (body
);
757 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
758 body
= TREE_OPERAND (body
, 0);
759 body
= constexpr_fn_retval (body
);
764 /* CTYPE is a type constructed from BODY. Return true if some
765 bases/fields are uninitialized, and complain if COMPLAIN. */
768 cx_check_missing_mem_inits (tree ctype
, tree body
, bool complain
)
770 /* We allow uninitialized bases/fields in C++20. */
771 if (cxx_dialect
>= cxx2a
)
778 if (TREE_CODE (body
) != CONSTRUCTOR
)
780 nelts
= CONSTRUCTOR_NELTS (body
);
782 tree field
= TYPE_FIELDS (ctype
);
784 if (TREE_CODE (ctype
) == UNION_TYPE
)
786 if (nelts
== 0 && next_initializable_field (field
))
789 error ("%<constexpr%> constructor for union %qT must "
790 "initialize exactly one non-static data member", ctype
);
796 /* Iterate over the CONSTRUCTOR, checking any missing fields don't
797 need an explicit initialization. */
799 for (unsigned i
= 0; i
<= nelts
; ++i
)
801 tree index
= NULL_TREE
;
804 index
= CONSTRUCTOR_ELT (body
, i
)->index
;
805 /* Skip base and vtable inits. */
806 if (TREE_CODE (index
) != FIELD_DECL
807 || DECL_ARTIFICIAL (index
))
811 for (; field
!= index
; field
= DECL_CHAIN (field
))
814 if (TREE_CODE (field
) != FIELD_DECL
)
816 if (DECL_UNNAMED_BIT_FIELD (field
))
818 if (DECL_ARTIFICIAL (field
))
820 if (ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
822 /* Recurse to check the anonymous aggregate member. */
823 bad
|= cx_check_missing_mem_inits
824 (TREE_TYPE (field
), NULL_TREE
, complain
);
825 if (bad
&& !complain
)
829 ftype
= strip_array_types (TREE_TYPE (field
));
830 if (type_has_constexpr_default_constructor (ftype
))
832 /* It's OK to skip a member with a trivial constexpr ctor.
833 A constexpr ctor that isn't trivial should have been
835 gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype
)
841 auto_diagnostic_group d
;
842 error ("member %qD must be initialized by mem-initializer "
843 "in %<constexpr%> constructor", field
);
844 inform (DECL_SOURCE_LOCATION (field
), "declared here");
847 if (field
== NULL_TREE
)
850 if (ANON_AGGR_TYPE_P (TREE_TYPE (index
)))
852 /* Check the anonymous aggregate initializer is valid. */
853 bad
|= cx_check_missing_mem_inits
854 (TREE_TYPE (index
), CONSTRUCTOR_ELT (body
, i
)->value
, complain
);
855 if (bad
&& !complain
)
858 field
= DECL_CHAIN (field
);
864 /* We are processing the definition of the constexpr function FUN.
865 Check that its BODY fulfills the propriate requirements and
866 enter it in the constexpr function definition table.
867 For constructor BODY is actually the TREE_LIST of the
868 member-initializer list. */
871 register_constexpr_fundef (tree fun
, tree body
)
873 constexpr_fundef entry
;
874 constexpr_fundef
**slot
;
876 if (!is_valid_constexpr_fn (fun
, !DECL_GENERATED_P (fun
)))
879 tree massaged
= massage_constexpr_body (fun
, body
);
880 if (massaged
== NULL_TREE
|| massaged
== error_mark_node
)
882 if (!DECL_CONSTRUCTOR_P (fun
))
883 error ("body of %<constexpr%> function %qD not a return-statement",
888 bool potential
= potential_rvalue_constant_expression (massaged
);
889 if (!potential
&& !DECL_GENERATED_P (fun
))
890 require_potential_rvalue_constant_expression (massaged
);
892 if (DECL_CONSTRUCTOR_P (fun
)
893 && cx_check_missing_mem_inits (DECL_CONTEXT (fun
),
894 massaged
, !DECL_GENERATED_P (fun
)))
897 if (!potential
&& !DECL_GENERATED_P (fun
))
900 /* Create the constexpr function table if necessary. */
901 if (constexpr_fundef_table
== NULL
)
902 constexpr_fundef_table
903 = hash_table
<constexpr_fundef_hasher
>::create_ggc (101);
906 tree saved_fn
= current_function_decl
;
907 bool clear_ctx
= false;
908 current_function_decl
= fun
;
909 if (DECL_RESULT (fun
) && DECL_CONTEXT (DECL_RESULT (fun
)) == NULL_TREE
)
912 DECL_CONTEXT (DECL_RESULT (fun
)) = fun
;
914 entry
.body
= copy_fn (fun
, entry
.parms
, entry
.result
);
915 current_function_decl
= saved_fn
;
916 slot
= constexpr_fundef_table
->find_slot (&entry
, INSERT
);
918 DECL_CONTEXT (DECL_RESULT (fun
)) = NULL_TREE
;
921 /* For a template instantiation, we want to remember the pre-generic body
922 for explain_invalid_constexpr_fn, but do tell cxx_eval_call_expression
923 that it doesn't need to bother trying to expand the function. */
924 entry
.result
= error_mark_node
;
926 gcc_assert (*slot
== NULL
);
927 *slot
= ggc_alloc
<constexpr_fundef
> ();
933 /* FUN is a non-constexpr function called in a context that requires a
934 constant expression. If it comes from a constexpr template, explain why
935 the instantiation isn't constexpr. */
938 explain_invalid_constexpr_fn (tree fun
)
940 static hash_set
<tree
> *diagnosed
;
943 /* Only diagnose defaulted functions, lambdas, or instantiations. */
944 if (!DECL_DEFAULTED_FN (fun
)
945 && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun
))
946 && !is_instantiation_of_constexpr (fun
))
948 inform (DECL_SOURCE_LOCATION (fun
), "%qD declared here", fun
);
951 if (diagnosed
== NULL
)
952 diagnosed
= new hash_set
<tree
>;
953 if (diagnosed
->add (fun
))
954 /* Already explained. */
957 save_loc
= input_location
;
958 if (!lambda_static_thunk_p (fun
))
960 /* Diagnostics should completely ignore the static thunk, so leave
961 input_location set to our caller's location. */
962 input_location
= DECL_SOURCE_LOCATION (fun
);
963 inform (input_location
,
964 "%qD is not usable as a %<constexpr%> function because:", fun
);
966 /* First check the declaration. */
967 if (is_valid_constexpr_fn (fun
, true))
969 /* Then if it's OK, the body. */
970 if (!DECL_DECLARED_CONSTEXPR_P (fun
)
971 && DECL_DEFAULTED_FN (fun
))
972 explain_implicit_non_constexpr (fun
);
975 if (constexpr_fundef
*fd
= retrieve_constexpr_fundef (fun
))
978 body
= DECL_SAVED_TREE (fun
);
979 body
= massage_constexpr_body (fun
, body
);
980 require_potential_rvalue_constant_expression (body
);
981 if (DECL_CONSTRUCTOR_P (fun
))
982 cx_check_missing_mem_inits (DECL_CONTEXT (fun
), body
, true);
985 input_location
= save_loc
;
988 /* Objects of this type represent calls to constexpr functions
989 along with the bindings of parameters to their arguments, for
990 the purpose of compile time evaluation. */
992 struct GTY((for_user
)) constexpr_call
{
993 /* Description of the constexpr function definition. */
994 constexpr_fundef
*fundef
;
995 /* Parameter bindings environment. A TREE_VEC of arguments. */
997 /* Result of the call.
998 NULL means the call is being evaluated.
999 error_mark_node means that the evaluation was erroneous;
1000 otherwise, the actuall value of the call. */
1002 /* The hash of this call; we remember it here to avoid having to
1003 recalculate it when expanding the hash table. */
1005 /* Whether __builtin_is_constant_evaluated() should evaluate to true. */
1006 bool manifestly_const_eval
;
1009 struct constexpr_call_hasher
: ggc_ptr_hash
<constexpr_call
>
1011 static hashval_t
hash (constexpr_call
*);
1012 static bool equal (constexpr_call
*, constexpr_call
*);
1015 enum constexpr_switch_state
{
1016 /* Used when processing a switch for the first time by cxx_eval_switch_expr
1017 and default: label for that switch has not been seen yet. */
1018 css_default_not_seen
,
1019 /* Used when processing a switch for the first time by cxx_eval_switch_expr
1020 and default: label for that switch has been seen already. */
1022 /* Used when processing a switch for the second time by
1023 cxx_eval_switch_expr, where default: label should match. */
1024 css_default_processing
1027 /* The constexpr expansion context part which needs one instance per
1028 cxx_eval_outermost_constant_expr invocation. VALUES is a map of values of
1029 variables initialized within the expression. */
1031 struct constexpr_global_ctx
{
1032 /* Values for any temporaries or local variables within the
1033 constant-expression. */
1034 hash_map
<tree
,tree
> values
;
1035 /* Number of cxx_eval_constant_expression calls (except skipped ones,
1036 on simple constants or location wrappers) encountered during current
1037 cxx_eval_outermost_constant_expr call. */
1038 HOST_WIDE_INT constexpr_ops_count
;
1039 /* Heap VAR_DECLs created during the evaluation of the outermost constant
1041 auto_vec
<tree
, 16> heap_vars
;
1042 /* Cleanups that need to be evaluated at the end of CLEANUP_POINT_EXPR. */
1043 vec
<tree
> *cleanups
;
1044 /* Number of heap VAR_DECL deallocations. */
1045 unsigned heap_dealloc_count
;
1047 constexpr_global_ctx ()
1048 : constexpr_ops_count (0), cleanups (NULL
), heap_dealloc_count (0) {}
1051 /* The constexpr expansion context. CALL is the current function
1052 expansion, CTOR is the current aggregate initializer, OBJECT is the
1053 object being initialized by CTOR, either a VAR_DECL or a _REF. */
1055 struct constexpr_ctx
{
1056 /* The part of the context that needs to be unique to the whole
1057 cxx_eval_outermost_constant_expr invocation. */
1058 constexpr_global_ctx
*global
;
1059 /* The innermost call we're evaluating. */
1060 constexpr_call
*call
;
1061 /* SAVE_EXPRs and TARGET_EXPR_SLOT vars of TARGET_EXPRs that we've seen
1062 within the current LOOP_EXPR. NULL if we aren't inside a loop. */
1063 vec
<tree
> *save_exprs
;
1064 /* The CONSTRUCTOR we're currently building up for an aggregate
1067 /* The object we're building the CONSTRUCTOR for. */
1069 /* If inside SWITCH_EXPR. */
1070 constexpr_switch_state
*css_state
;
1072 /* Whether we should error on a non-constant expression or fail quietly. */
1074 /* Whether we are strictly conforming to constant expression rules or
1075 trying harder to get a constant value. */
1077 /* Whether __builtin_is_constant_evaluated () should be true. */
1078 bool manifestly_const_eval
;
1081 /* A table of all constexpr calls that have been evaluated by the
1082 compiler in this translation unit. */
1084 static GTY (()) hash_table
<constexpr_call_hasher
> *constexpr_call_table
;
1086 static tree
cxx_eval_constant_expression (const constexpr_ctx
*, tree
,
1087 bool, bool *, bool *, tree
* = NULL
);
1089 /* Compute a hash value for a constexpr call representation. */
1092 constexpr_call_hasher::hash (constexpr_call
*info
)
1097 /* Return true if the objects pointed to by P and Q represent calls
1098 to the same constexpr function with the same arguments.
1099 Otherwise, return false. */
1102 constexpr_call_hasher::equal (constexpr_call
*lhs
, constexpr_call
*rhs
)
1106 if (lhs
->hash
!= rhs
->hash
)
1108 if (lhs
->manifestly_const_eval
!= rhs
->manifestly_const_eval
)
1110 if (!constexpr_fundef_hasher::equal (lhs
->fundef
, rhs
->fundef
))
1112 return cp_tree_equal (lhs
->bindings
, rhs
->bindings
);
1115 /* Initialize the constexpr call table, if needed. */
1118 maybe_initialize_constexpr_call_table (void)
1120 if (constexpr_call_table
== NULL
)
1121 constexpr_call_table
= hash_table
<constexpr_call_hasher
>::create_ggc (101);
1124 /* During constexpr CALL_EXPR evaluation, to avoid issues with sharing when
1125 a function happens to get called recursively, we unshare the callee
1126 function's body and evaluate this unshared copy instead of evaluating the
1129 FUNDEF_COPIES_TABLE is a per-function freelist of these unshared function
1130 copies. The underlying data structure of FUNDEF_COPIES_TABLE is a hash_map
1131 that's keyed off of the original FUNCTION_DECL and whose value is a
1132 TREE_LIST of this function's unused copies awaiting reuse.
1134 This is not GC-deletable to avoid GC affecting UID generation. */
1136 static GTY(()) hash_map
<tree
, tree
> *fundef_copies_table
;
1138 /* Reuse a copy or create a new unshared copy of the function FUN.
1139 Return this copy. We use a TREE_LIST whose PURPOSE is body, VALUE
1140 is parms, TYPE is result. */
1143 get_fundef_copy (constexpr_fundef
*fundef
)
1147 tree
*slot
= &(hash_map_safe_get_or_insert
<hm_ggc
>
1148 (fundef_copies_table
, fundef
->decl
, &existed
, 127));
1152 /* There is no cached function available, or in use. We can use
1153 the function directly. That the slot is now created records
1154 that this function is now in use. */
1155 copy
= build_tree_list (fundef
->body
, fundef
->parms
);
1156 TREE_TYPE (copy
) = fundef
->result
;
1158 else if (*slot
== NULL_TREE
)
1160 /* We've already used the function itself, so make a copy. */
1161 copy
= build_tree_list (NULL
, NULL
);
1162 tree saved_body
= DECL_SAVED_TREE (fundef
->decl
);
1163 tree saved_parms
= DECL_ARGUMENTS (fundef
->decl
);
1164 tree saved_result
= DECL_RESULT (fundef
->decl
);
1165 tree saved_fn
= current_function_decl
;
1166 DECL_SAVED_TREE (fundef
->decl
) = fundef
->body
;
1167 DECL_ARGUMENTS (fundef
->decl
) = fundef
->parms
;
1168 DECL_RESULT (fundef
->decl
) = fundef
->result
;
1169 current_function_decl
= fundef
->decl
;
1170 TREE_PURPOSE (copy
) = copy_fn (fundef
->decl
, TREE_VALUE (copy
),
1172 current_function_decl
= saved_fn
;
1173 DECL_RESULT (fundef
->decl
) = saved_result
;
1174 DECL_ARGUMENTS (fundef
->decl
) = saved_parms
;
1175 DECL_SAVED_TREE (fundef
->decl
) = saved_body
;
1179 /* We have a cached function available. */
1181 *slot
= TREE_CHAIN (copy
);
1187 /* Save the copy COPY of function FUN for later reuse by
1188 get_fundef_copy(). By construction, there will always be an entry
1192 save_fundef_copy (tree fun
, tree copy
)
1194 tree
*slot
= fundef_copies_table
->get (fun
);
1195 TREE_CHAIN (copy
) = *slot
;
1199 /* We have an expression tree T that represents a call, either CALL_EXPR
1200 or AGGR_INIT_EXPR. Return the Nth argument. */
1203 get_nth_callarg (tree t
, int n
)
1205 switch (TREE_CODE (t
))
1208 return CALL_EXPR_ARG (t
, n
);
1210 case AGGR_INIT_EXPR
:
1211 return AGGR_INIT_EXPR_ARG (t
, n
);
1219 /* Attempt to evaluate T which represents a call to a builtin function.
1220 We assume here that all builtin functions evaluate to scalar types
1221 represented by _CST nodes. */
1224 cxx_eval_builtin_function_call (const constexpr_ctx
*ctx
, tree t
, tree fun
,
1226 bool *non_constant_p
, bool *overflow_p
)
1228 const int nargs
= call_expr_nargs (t
);
1229 tree
*args
= (tree
*) alloca (nargs
* sizeof (tree
));
1233 /* Don't fold __builtin_constant_p within a constexpr function. */
1234 bool bi_const_p
= DECL_IS_BUILTIN_CONSTANT_P (fun
);
1236 /* If we aren't requiring a constant expression, defer __builtin_constant_p
1237 in a constexpr function until we have values for the parameters. */
1239 && !ctx
->manifestly_const_eval
1240 && current_function_decl
1241 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
1243 *non_constant_p
= true;
1247 /* For __builtin_is_constant_evaluated, defer it if not
1248 ctx->manifestly_const_eval, otherwise fold it to true. */
1249 if (fndecl_built_in_p (fun
, CP_BUILT_IN_IS_CONSTANT_EVALUATED
,
1252 if (!ctx
->manifestly_const_eval
)
1254 *non_constant_p
= true;
1257 return boolean_true_node
;
1260 if (fndecl_built_in_p (fun
, CP_BUILT_IN_SOURCE_LOCATION
, BUILT_IN_FRONTEND
))
1261 return fold_builtin_source_location (EXPR_LOCATION (t
));
1265 if (fndecl_built_in_p (fun
, BUILT_IN_NORMAL
))
1266 switch (DECL_FUNCTION_CODE (fun
))
1268 case BUILT_IN_STRLEN
:
1269 case BUILT_IN_STRNLEN
:
1272 case BUILT_IN_MEMCHR
:
1273 case BUILT_IN_STRCHR
:
1274 case BUILT_IN_STRRCHR
:
1278 case BUILT_IN_MEMCMP
:
1279 case BUILT_IN_STRCMP
:
1282 case BUILT_IN_STRSTR
:
1289 /* Be permissive for arguments to built-ins; __builtin_constant_p should
1290 return constant false for a non-constant argument. */
1291 constexpr_ctx new_ctx
= *ctx
;
1292 new_ctx
.quiet
= true;
1293 for (i
= 0; i
< nargs
; ++i
)
1295 tree arg
= CALL_EXPR_ARG (t
, i
);
1297 /* To handle string built-ins we need to pass ADDR_EXPR<STRING_CST> since
1298 expand_builtin doesn't know how to look in the values table. */
1299 bool strop
= i
< strops
;
1303 if (TREE_CODE (arg
) == ADDR_EXPR
)
1304 arg
= TREE_OPERAND (arg
, 0);
1309 /* If builtin_valid_in_constant_expr_p is true,
1310 potential_constant_expression_1 has not recursed into the arguments
1311 of the builtin, verify it here. */
1312 if (!builtin_valid_in_constant_expr_p (fun
)
1313 || potential_constant_expression (arg
))
1315 bool dummy1
= false, dummy2
= false;
1316 arg
= cxx_eval_constant_expression (&new_ctx
, arg
, false,
1321 /* For __builtin_constant_p, fold all expressions with constant values
1322 even if they aren't C++ constant-expressions. */
1323 arg
= cp_fold_rvalue (arg
);
1326 if (TREE_CODE (arg
) == CONSTRUCTOR
)
1327 arg
= braced_lists_to_strings (TREE_TYPE (arg
), arg
);
1328 if (TREE_CODE (arg
) == STRING_CST
)
1329 arg
= build_address (arg
);
1335 bool save_ffbcp
= force_folding_builtin_constant_p
;
1336 force_folding_builtin_constant_p
|= ctx
->manifestly_const_eval
;
1337 tree save_cur_fn
= current_function_decl
;
1338 /* Return name of ctx->call->fundef->decl for __builtin_FUNCTION (). */
1339 if (fndecl_built_in_p (fun
, BUILT_IN_FUNCTION
)
1341 && ctx
->call
->fundef
)
1342 current_function_decl
= ctx
->call
->fundef
->decl
;
1343 new_call
= fold_builtin_call_array (EXPR_LOCATION (t
), TREE_TYPE (t
),
1344 CALL_EXPR_FN (t
), nargs
, args
);
1345 current_function_decl
= save_cur_fn
;
1346 force_folding_builtin_constant_p
= save_ffbcp
;
1347 if (new_call
== NULL
)
1349 if (!*non_constant_p
&& !ctx
->quiet
)
1351 /* Do not allow__builtin_unreachable in constexpr function.
1352 The __builtin_unreachable call with BUILTINS_LOCATION
1353 comes from cp_maybe_instrument_return. */
1354 if (fndecl_built_in_p (fun
, BUILT_IN_UNREACHABLE
)
1355 && EXPR_LOCATION (t
) == BUILTINS_LOCATION
)
1356 error ("%<constexpr%> call flows off the end of the function");
1359 new_call
= build_call_array_loc (EXPR_LOCATION (t
), TREE_TYPE (t
),
1360 CALL_EXPR_FN (t
), nargs
, args
);
1361 error ("%q+E is not a constant expression", new_call
);
1364 *non_constant_p
= true;
1368 if (!potential_constant_expression (new_call
))
1370 if (!*non_constant_p
&& !ctx
->quiet
)
1371 error ("%q+E is not a constant expression", new_call
);
1372 *non_constant_p
= true;
1378 /* memchr returns a pointer into the first argument, but we replaced the
1379 argument above with a STRING_CST; put it back it now. */
1380 tree op
= CALL_EXPR_ARG (t
, strret
-1);
1381 STRIP_NOPS (new_call
);
1382 if (TREE_CODE (new_call
) == POINTER_PLUS_EXPR
)
1383 TREE_OPERAND (new_call
, 0) = op
;
1384 else if (TREE_CODE (new_call
) == ADDR_EXPR
)
1388 return cxx_eval_constant_expression (&new_ctx
, new_call
, lval
,
1389 non_constant_p
, overflow_p
);
1392 /* TEMP is the constant value of a temporary object of type TYPE. Adjust
1393 the type of the value to match. */
1396 adjust_temp_type (tree type
, tree temp
)
1398 if (same_type_p (TREE_TYPE (temp
), type
))
1400 /* Avoid wrapping an aggregate value in a NOP_EXPR. */
1401 if (TREE_CODE (temp
) == CONSTRUCTOR
)
1403 /* build_constructor wouldn't retain various CONSTRUCTOR flags. */
1404 tree t
= copy_node (temp
);
1405 TREE_TYPE (t
) = type
;
1408 if (TREE_CODE (temp
) == EMPTY_CLASS_EXPR
)
1409 return build0 (EMPTY_CLASS_EXPR
, type
);
1410 gcc_assert (scalarish_type_p (type
));
1411 /* Now we know we're dealing with a scalar, and a prvalue of non-class
1412 type is cv-unqualified. */
1413 return cp_fold_convert (cv_unqualified (type
), temp
);
1416 /* If T is a CONSTRUCTOR, return an unshared copy of T and any
1417 sub-CONSTRUCTORs. Otherwise return T.
1419 We use this whenever we initialize an object as a whole, whether it's a
1420 parameter, a local variable, or a subobject, so that subsequent
1421 modifications don't affect other places where it was used. */
1424 unshare_constructor (tree t MEM_STAT_DECL
)
1426 if (!t
|| TREE_CODE (t
) != CONSTRUCTOR
)
1428 auto_vec
<tree
*, 4> ptrs
;
1429 ptrs
.safe_push (&t
);
1430 while (!ptrs
.is_empty ())
1432 tree
*p
= ptrs
.pop ();
1433 tree n
= copy_node (*p PASS_MEM_STAT
);
1434 CONSTRUCTOR_ELTS (n
) = vec_safe_copy (CONSTRUCTOR_ELTS (*p
) PASS_MEM_STAT
);
1436 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (n
);
1437 constructor_elt
*ce
;
1438 for (HOST_WIDE_INT i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
1439 if (TREE_CODE (ce
->value
) == CONSTRUCTOR
)
1440 ptrs
.safe_push (&ce
->value
);
1445 /* If T is a CONSTRUCTOR, ggc_free T and any sub-CONSTRUCTORs. */
1448 free_constructor (tree t
)
1450 if (!t
|| TREE_CODE (t
) != CONSTRUCTOR
)
1452 releasing_vec ctors
;
1453 vec_safe_push (ctors
, t
);
1454 while (!ctors
->is_empty ())
1456 tree c
= ctors
->pop ();
1457 if (vec
<constructor_elt
, va_gc
> *elts
= CONSTRUCTOR_ELTS (c
))
1459 constructor_elt
*ce
;
1460 for (HOST_WIDE_INT i
= 0; vec_safe_iterate (elts
, i
, &ce
); ++i
)
1461 if (TREE_CODE (ce
->value
) == CONSTRUCTOR
)
1462 vec_safe_push (ctors
, ce
->value
);
1469 /* Subroutine of cxx_eval_call_expression.
1470 We are processing a call expression (either CALL_EXPR or
1471 AGGR_INIT_EXPR) in the context of CTX. Evaluate
1472 all arguments and bind their values to correspondings
1473 parameters, making up the NEW_CALL context. */
1476 cxx_bind_parameters_in_call (const constexpr_ctx
*ctx
, tree t
,
1477 constexpr_call
*new_call
,
1478 bool *non_constant_p
, bool *overflow_p
,
1479 bool *non_constant_args
)
1481 const int nargs
= call_expr_nargs (t
);
1482 tree fun
= new_call
->fundef
->decl
;
1483 tree parms
= new_call
->fundef
->parms
;
1485 /* We don't record ellipsis args below. */
1486 int nparms
= list_length (parms
);
1487 int nbinds
= nargs
< nparms
? nargs
: nparms
;
1488 tree binds
= new_call
->bindings
= make_tree_vec (nbinds
);
1489 for (i
= 0; i
< nargs
; ++i
)
1492 tree type
= parms
? TREE_TYPE (parms
) : void_type_node
;
1493 x
= get_nth_callarg (t
, i
);
1494 /* For member function, the first argument is a pointer to the implied
1495 object. For a constructor, it might still be a dummy object, in
1496 which case we get the real argument from ctx. */
1497 if (i
== 0 && DECL_CONSTRUCTOR_P (fun
)
1498 && is_dummy_object (x
))
1501 x
= build_address (x
);
1503 if (TREE_ADDRESSABLE (type
))
1504 /* Undo convert_for_arg_passing work here. */
1505 x
= convert_from_reference (x
);
1506 arg
= cxx_eval_constant_expression (ctx
, x
, /*lval=*/false,
1507 non_constant_p
, overflow_p
);
1508 /* Don't VERIFY_CONSTANT here. */
1509 if (*non_constant_p
&& ctx
->quiet
)
1511 /* Just discard ellipsis args after checking their constantitude. */
1515 if (!*non_constant_p
)
1517 /* Make sure the binding has the same type as the parm. But
1518 only for constant args. */
1519 if (!TYPE_REF_P (type
))
1520 arg
= adjust_temp_type (type
, arg
);
1521 if (!TREE_CONSTANT (arg
))
1522 *non_constant_args
= true;
1523 /* For virtual calls, adjust the this argument, so that it is
1524 the object on which the method is called, rather than
1525 one of its bases. */
1526 if (i
== 0 && DECL_VIRTUAL_P (fun
))
1530 if (TREE_CODE (addr
) == ADDR_EXPR
)
1532 tree obj
= TREE_OPERAND (addr
, 0);
1533 while (TREE_CODE (obj
) == COMPONENT_REF
1534 && DECL_FIELD_IS_BASE (TREE_OPERAND (obj
, 1))
1535 && !same_type_ignoring_top_level_qualifiers_p
1536 (TREE_TYPE (obj
), DECL_CONTEXT (fun
)))
1537 obj
= TREE_OPERAND (obj
, 0);
1538 if (obj
!= TREE_OPERAND (addr
, 0))
1539 arg
= build_fold_addr_expr_with_type (obj
,
1543 TREE_VEC_ELT (binds
, i
) = arg
;
1545 parms
= TREE_CHAIN (parms
);
1549 /* Variables and functions to manage constexpr call expansion context.
1550 These do not need to be marked for PCH or GC. */
1552 /* FIXME remember and print actual constant arguments. */
1553 static vec
<tree
> call_stack
;
1554 static int call_stack_tick
;
1555 static int last_cx_error_tick
;
1558 push_cx_call_context (tree call
)
1561 if (!EXPR_HAS_LOCATION (call
))
1562 SET_EXPR_LOCATION (call
, input_location
);
1563 call_stack
.safe_push (call
);
1564 int len
= call_stack
.length ();
1565 if (len
> max_constexpr_depth
)
1571 pop_cx_call_context (void)
1578 cx_error_context (void)
1580 vec
<tree
> r
= vNULL
;
1581 if (call_stack_tick
!= last_cx_error_tick
1582 && !call_stack
.is_empty ())
1584 last_cx_error_tick
= call_stack_tick
;
1588 /* Evaluate a call T to a GCC internal function when possible and return
1589 the evaluated result or, under the control of CTX, give an error, set
1590 NON_CONSTANT_P, and return the unevaluated call T otherwise. */
1593 cxx_eval_internal_function (const constexpr_ctx
*ctx
, tree t
,
1595 bool *non_constant_p
, bool *overflow_p
)
1597 enum tree_code opcode
= ERROR_MARK
;
1599 switch (CALL_EXPR_IFN (t
))
1601 case IFN_UBSAN_NULL
:
1602 case IFN_UBSAN_BOUNDS
:
1603 case IFN_UBSAN_VPTR
:
1604 case IFN_FALLTHROUGH
:
1607 case IFN_ADD_OVERFLOW
:
1610 case IFN_SUB_OVERFLOW
:
1611 opcode
= MINUS_EXPR
;
1613 case IFN_MUL_OVERFLOW
:
1618 return cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 0),
1619 false, non_constant_p
, overflow_p
);
1621 case IFN_VEC_CONVERT
:
1623 tree arg
= cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 0),
1624 false, non_constant_p
,
1626 if (TREE_CODE (arg
) == VECTOR_CST
)
1627 return fold_const_call (CFN_VEC_CONVERT
, TREE_TYPE (t
), arg
);
1630 *non_constant_p
= true;
1637 error_at (cp_expr_loc_or_input_loc (t
),
1638 "call to internal function %qE", t
);
1639 *non_constant_p
= true;
1643 /* Evaluate constant arguments using OPCODE and return a complex
1644 number containing the result and the overflow bit. */
1645 tree arg0
= cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 0), lval
,
1646 non_constant_p
, overflow_p
);
1647 tree arg1
= cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 1), lval
,
1648 non_constant_p
, overflow_p
);
1650 if (TREE_CODE (arg0
) == INTEGER_CST
&& TREE_CODE (arg1
) == INTEGER_CST
)
1652 location_t loc
= cp_expr_loc_or_input_loc (t
);
1653 tree type
= TREE_TYPE (TREE_TYPE (t
));
1654 tree result
= fold_binary_loc (loc
, opcode
, type
,
1655 fold_convert_loc (loc
, type
, arg0
),
1656 fold_convert_loc (loc
, type
, arg1
));
1658 = build_int_cst (type
, arith_overflowed_p (opcode
, type
, arg0
, arg1
));
1659 /* Reset TREE_OVERFLOW to avoid warnings for the overflow. */
1660 if (TREE_OVERFLOW (result
))
1661 TREE_OVERFLOW (result
) = 0;
1663 return build_complex (TREE_TYPE (t
), result
, ovf
);
1666 *non_constant_p
= true;
1670 /* Clean CONSTRUCTOR_NO_CLEARING from CTOR and its sub-aggregates. */
1673 clear_no_implicit_zero (tree ctor
)
1675 if (CONSTRUCTOR_NO_CLEARING (ctor
))
1677 CONSTRUCTOR_NO_CLEARING (ctor
) = false;
1678 tree elt
; unsigned HOST_WIDE_INT idx
;
1679 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor
), idx
, elt
)
1680 if (TREE_CODE (elt
) == CONSTRUCTOR
)
1681 clear_no_implicit_zero (elt
);
1685 /* Complain about a const object OBJ being modified in a constant expression.
1686 EXPR is the MODIFY_EXPR expression performing the modification. */
1689 modifying_const_object_error (tree expr
, tree obj
)
1691 location_t loc
= cp_expr_loc_or_input_loc (expr
);
1692 auto_diagnostic_group d
;
1693 error_at (loc
, "modifying a const object %qE is not allowed in "
1694 "a constant expression", TREE_OPERAND (expr
, 0));
1695 inform (location_of (obj
), "originally declared %<const%> here");
1698 /* Return true if FNDECL is a replaceable global allocation function that
1699 should be useable during constant expression evaluation. */
1702 cxx_replaceable_global_alloc_fn (tree fndecl
)
1704 return (cxx_dialect
>= cxx2a
1705 && IDENTIFIER_NEWDEL_OP_P (DECL_NAME (fndecl
))
1706 && CP_DECL_CONTEXT (fndecl
) == global_namespace
1707 && (DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl
)
1708 || DECL_IS_OPERATOR_DELETE_P (fndecl
)));
1711 /* Return true if FNDECL is a placement new function that should be
1712 useable during constant expression evaluation of std::construct_at. */
1715 cxx_placement_new_fn (tree fndecl
)
1717 if (cxx_dialect
>= cxx2a
1718 && IDENTIFIER_NEW_OP_P (DECL_NAME (fndecl
))
1719 && CP_DECL_CONTEXT (fndecl
) == global_namespace
1720 && !DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl
)
1721 && TREE_CODE (TREE_TYPE (fndecl
)) == FUNCTION_TYPE
)
1723 tree first_arg
= TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
1724 if (TREE_VALUE (first_arg
) == ptr_type_node
1725 && TREE_CHAIN (first_arg
) == void_list_node
)
1731 /* Return true if FNDECL is std::construct_at. */
1734 is_std_construct_at (tree fndecl
)
1736 if (!decl_in_std_namespace_p (fndecl
))
1739 tree name
= DECL_NAME (fndecl
);
1740 return name
&& id_equal (name
, "construct_at");
1743 /* Return true if FNDECL is std::allocator<T>::{,de}allocate. */
1746 is_std_allocator_allocate (tree fndecl
)
1748 tree name
= DECL_NAME (fndecl
);
1749 if (name
== NULL_TREE
1750 || !(id_equal (name
, "allocate") || id_equal (name
, "deallocate")))
1753 tree ctx
= DECL_CONTEXT (fndecl
);
1754 if (ctx
== NULL_TREE
|| !CLASS_TYPE_P (ctx
) || !TYPE_MAIN_DECL (ctx
))
1757 tree decl
= TYPE_MAIN_DECL (ctx
);
1758 name
= DECL_NAME (decl
);
1759 if (name
== NULL_TREE
|| !id_equal (name
, "allocator"))
1762 return decl_in_std_namespace_p (decl
);
1765 /* Return true if FNDECL is __dynamic_cast. */
1768 cxx_dynamic_cast_fn_p (tree fndecl
)
1770 return (cxx_dialect
>= cxx2a
1771 && id_equal (DECL_NAME (fndecl
), "__dynamic_cast")
1772 && CP_DECL_CONTEXT (fndecl
) == global_namespace
);
1775 /* Often, we have an expression in the form of address + offset, e.g.
1776 "&_ZTV1A + 16". Extract the object from it, i.e. "_ZTV1A". */
1779 extract_obj_from_addr_offset (tree expr
)
1781 if (TREE_CODE (expr
) == POINTER_PLUS_EXPR
)
1782 expr
= TREE_OPERAND (expr
, 0);
1784 if (TREE_CODE (expr
) == ADDR_EXPR
)
1785 expr
= TREE_OPERAND (expr
, 0);
1789 /* Given a PATH like
1791 g.D.2181.D.2154.D.2102.D.2093
1793 find a component with type TYPE. Return NULL_TREE if not found, and
1794 error_mark_node if the component is not accessible. If STOP is non-null,
1795 this function will return NULL_TREE if STOP is found before TYPE. */
1798 get_component_with_type (tree path
, tree type
, tree stop
)
1802 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path
), type
))
1806 && (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path
),
1809 else if (TREE_CODE (path
) == COMPONENT_REF
1810 && DECL_FIELD_IS_BASE (TREE_OPERAND (path
, 1)))
1812 /* We need to check that the component we're accessing is in fact
1814 if (TREE_PRIVATE (TREE_OPERAND (path
, 1))
1815 || TREE_PROTECTED (TREE_OPERAND (path
, 1)))
1816 return error_mark_node
;
1817 path
= TREE_OPERAND (path
, 0);
1824 /* Evaluate a call to __dynamic_cast (permitted by P1327R1).
1826 The declaration of __dynamic_cast is:
1828 void* __dynamic_cast (const void* __src_ptr,
1829 const __class_type_info* __src_type,
1830 const __class_type_info* __dst_type,
1831 ptrdiff_t __src2dst);
1833 where src2dst has the following possible values
1835 >-1: src_type is a unique public non-virtual base of dst_type
1836 dst_ptr + src2dst == src_ptr
1837 -1: unspecified relationship
1838 -2: src_type is not a public base of dst_type
1839 -3: src_type is a multiple public non-virtual base of dst_type
1841 Since literal types can't have virtual bases, we only expect hint >=0,
1845 cxx_eval_dynamic_cast_fn (const constexpr_ctx
*ctx
, tree call
,
1846 bool *non_constant_p
, bool *overflow_p
)
1848 /* T will be something like
1849 __dynamic_cast ((B*) b, &_ZTI1B, &_ZTI1D, 8)
1851 gcc_assert (call_expr_nargs (call
) == 4);
1852 tsubst_flags_t complain
= ctx
->quiet
? tf_none
: tf_warning_or_error
;
1853 tree obj
= CALL_EXPR_ARG (call
, 0);
1854 tree type
= CALL_EXPR_ARG (call
, 2);
1855 HOST_WIDE_INT hint
= int_cst_value (CALL_EXPR_ARG (call
, 3));
1856 location_t loc
= cp_expr_loc_or_input_loc (call
);
1858 /* Get the target type of the dynamic_cast. */
1859 gcc_assert (TREE_CODE (type
) == ADDR_EXPR
);
1860 type
= TREE_OPERAND (type
, 0);
1861 type
= TREE_TYPE (DECL_NAME (type
));
1863 /* TYPE can only be either T* or T&. We can't know which of these it
1864 is by looking at TYPE, but OBJ will be "(T*) x" in the first case,
1865 and something like "(T*)(T&)(T*) x" in the second case. */
1866 bool reference_p
= false;
1867 while (CONVERT_EXPR_P (obj
) || TREE_CODE (obj
) == SAVE_EXPR
)
1869 reference_p
|= TYPE_REF_P (TREE_TYPE (obj
));
1870 obj
= TREE_OPERAND (obj
, 0);
1873 /* Evaluate the object so that we know its dynamic type. */
1874 obj
= cxx_eval_constant_expression (ctx
, obj
, /*lval*/false, non_constant_p
,
1876 if (*non_constant_p
)
1879 /* We expect OBJ to be in form of &d.D.2102 when HINT == 0,
1880 but when HINT is > 0, it can also be something like
1881 &d.D.2102 + 18446744073709551608, which includes the BINFO_OFFSET. */
1882 obj
= extract_obj_from_addr_offset (obj
);
1883 const tree objtype
= TREE_TYPE (obj
);
1884 /* If OBJ doesn't refer to a base field, we're done. */
1885 if (tree t
= (TREE_CODE (obj
) == COMPONENT_REF
1886 ? TREE_OPERAND (obj
, 1) : obj
))
1887 if (TREE_CODE (t
) != FIELD_DECL
|| !DECL_FIELD_IS_BASE (t
))
1888 return integer_zero_node
;
1890 /* [class.cdtor] When a dynamic_cast is used in a constructor ...
1891 or in a destructor ... if the operand of the dynamic_cast refers
1892 to the object under construction or destruction, this object is
1893 considered to be a most derived object that has the type of the
1894 constructor or destructor's class. */
1895 tree vtable
= build_vfield_ref (obj
, TREE_TYPE (obj
));
1896 vtable
= cxx_eval_constant_expression (ctx
, vtable
, /*lval*/false,
1897 non_constant_p
, overflow_p
);
1898 if (*non_constant_p
)
1900 /* VTABLE will be &_ZTV1A + 16 or similar, get _ZTV1A. */
1901 vtable
= extract_obj_from_addr_offset (vtable
);
1902 const tree mdtype
= DECL_CONTEXT (vtable
);
1904 /* Given dynamic_cast<T>(v),
1906 [expr.dynamic.cast] If C is the class type to which T points or refers,
1907 the runtime check logically executes as follows:
1909 If, in the most derived object pointed (referred) to by v, v points
1910 (refers) to a public base class subobject of a C object, and if only
1911 one object of type C is derived from the subobject pointed (referred)
1912 to by v the result points (refers) to that C object.
1914 In this case, HINT >= 0 or -3. */
1915 if (hint
>= 0 || hint
== -3)
1917 /* Look for a component with type TYPE. */
1918 tree t
= get_component_with_type (obj
, type
, mdtype
);
1919 /* If not accessible, give an error. */
1920 if (t
== error_mark_node
)
1926 error_at (loc
, "reference %<dynamic_cast%> failed");
1927 inform (loc
, "static type %qT of its operand is a "
1928 "non-public base class of dynamic type %qT",
1932 *non_constant_p
= true;
1934 return integer_zero_node
;
1937 /* The result points to the TYPE object. */
1938 return cp_build_addr_expr (t
, complain
);
1939 /* Else, TYPE was not found, because the HINT turned out to be wrong.
1940 Fall through to the normal processing. */
1943 /* Otherwise, if v points (refers) to a public base class subobject of the
1944 most derived object, and the type of the most derived object has a base
1945 class, of type C, that is unambiguous and public, the result points
1946 (refers) to the C subobject of the most derived object.
1948 But it can also be an invalid case. */
1950 /* Get the most derived object. */
1951 obj
= get_component_with_type (obj
, mdtype
, NULL_TREE
);
1952 if (obj
== error_mark_node
)
1958 error_at (loc
, "reference %<dynamic_cast%> failed");
1959 inform (loc
, "static type %qT of its operand is a non-public"
1960 " base class of dynamic type %qT", objtype
, mdtype
);
1962 *non_constant_p
= true;
1964 return integer_zero_node
;
1969 /* Check that the type of the most derived object has a base class
1970 of type TYPE that is unambiguous and public. */
1972 tree binfo
= lookup_base (mdtype
, type
, ba_check
, &b_kind
, tf_none
);
1973 if (!binfo
|| binfo
== error_mark_node
)
1979 error_at (loc
, "reference %<dynamic_cast%> failed");
1980 if (b_kind
== bk_ambig
)
1981 inform (loc
, "%qT is an ambiguous base class of dynamic "
1982 "type %qT of its operand", type
, mdtype
);
1984 inform (loc
, "dynamic type %qT of its operand does not "
1985 "have an unambiguous public base class %qT",
1988 *non_constant_p
= true;
1990 return integer_zero_node
;
1992 /* If so, return the TYPE subobject of the most derived object. */
1993 obj
= convert_to_base_statically (obj
, binfo
);
1994 return cp_build_addr_expr (obj
, complain
);
1997 /* Subroutine of cxx_eval_constant_expression.
1998 Evaluate the call expression tree T in the context of OLD_CALL expression
2002 cxx_eval_call_expression (const constexpr_ctx
*ctx
, tree t
,
2004 bool *non_constant_p
, bool *overflow_p
)
2006 /* Handle concept checks separately. */
2007 if (concept_check_p (t
))
2008 return evaluate_concept_check (t
, tf_warning_or_error
);
2010 location_t loc
= cp_expr_loc_or_input_loc (t
);
2011 tree fun
= get_function_named_in_call (t
);
2012 constexpr_call new_call
2013 = { NULL
, NULL
, NULL
, 0, ctx
->manifestly_const_eval
};
2016 if (fun
== NULL_TREE
)
2017 return cxx_eval_internal_function (ctx
, t
, lval
,
2018 non_constant_p
, overflow_p
);
2020 if (TREE_CODE (fun
) != FUNCTION_DECL
)
2022 /* Might be a constexpr function pointer. */
2023 fun
= cxx_eval_constant_expression (ctx
, fun
,
2024 /*lval*/false, non_constant_p
,
2027 if (TREE_CODE (fun
) == ADDR_EXPR
)
2028 fun
= TREE_OPERAND (fun
, 0);
2029 /* For TARGET_VTABLE_USES_DESCRIPTORS targets, there is no
2030 indirection, the called expression is a pointer into the
2031 virtual table which should contain FDESC_EXPR. Extract the
2032 FUNCTION_DECL from there. */
2033 else if (TARGET_VTABLE_USES_DESCRIPTORS
2034 && TREE_CODE (fun
) == POINTER_PLUS_EXPR
2035 && TREE_CODE (TREE_OPERAND (fun
, 0)) == ADDR_EXPR
2036 && TREE_CODE (TREE_OPERAND (fun
, 1)) == INTEGER_CST
)
2038 tree d
= TREE_OPERAND (TREE_OPERAND (fun
, 0), 0);
2040 && DECL_VTABLE_OR_VTT_P (d
)
2041 && TREE_CODE (TREE_TYPE (d
)) == ARRAY_TYPE
2042 && TREE_TYPE (TREE_TYPE (d
)) == vtable_entry_type
2044 && TREE_CODE (DECL_INITIAL (d
)) == CONSTRUCTOR
)
2046 tree i
= int_const_binop (TRUNC_DIV_EXPR
, TREE_OPERAND (fun
, 1),
2047 TYPE_SIZE_UNIT (vtable_entry_type
));
2048 HOST_WIDE_INT idx
= find_array_ctor_elt (DECL_INITIAL (d
), i
);
2052 = (*CONSTRUCTOR_ELTS (DECL_INITIAL (d
)))[idx
].value
;
2053 if (TREE_CODE (fdesc
) == FDESC_EXPR
2054 && integer_zerop (TREE_OPERAND (fdesc
, 1)))
2055 fun
= TREE_OPERAND (fdesc
, 0);
2060 if (TREE_CODE (fun
) != FUNCTION_DECL
)
2062 if (!ctx
->quiet
&& !*non_constant_p
)
2063 error_at (loc
, "expression %qE does not designate a %<constexpr%> "
2065 *non_constant_p
= true;
2068 if (DECL_CLONED_FUNCTION_P (fun
))
2069 fun
= DECL_CLONED_FUNCTION (fun
);
2071 if (is_ubsan_builtin_p (fun
))
2074 if (fndecl_built_in_p (fun
))
2075 return cxx_eval_builtin_function_call (ctx
, t
, fun
,
2076 lval
, non_constant_p
, overflow_p
);
2077 if (!DECL_DECLARED_CONSTEXPR_P (fun
))
2079 if (TREE_CODE (t
) == CALL_EXPR
2080 && cxx_replaceable_global_alloc_fn (fun
)
2081 && (CALL_FROM_NEW_OR_DELETE_P (t
)
2083 && ctx
->call
->fundef
2084 && is_std_allocator_allocate (ctx
->call
->fundef
->decl
))))
2086 const int nargs
= call_expr_nargs (t
);
2087 tree arg0
= NULL_TREE
;
2088 for (int i
= 0; i
< nargs
; ++i
)
2090 tree arg
= CALL_EXPR_ARG (t
, i
);
2091 arg
= cxx_eval_constant_expression (ctx
, arg
, false,
2092 non_constant_p
, overflow_p
);
2093 VERIFY_CONSTANT (arg
);
2098 if (IDENTIFIER_NEW_OP_P (DECL_NAME (fun
)))
2100 tree type
= build_array_type_nelts (char_type_node
,
2101 tree_to_uhwi (arg0
));
2102 tree var
= build_decl (loc
, VAR_DECL
, heap_uninit_identifier
,
2104 DECL_ARTIFICIAL (var
) = 1;
2105 TREE_STATIC (var
) = 1;
2106 ctx
->global
->heap_vars
.safe_push (var
);
2107 ctx
->global
->values
.put (var
, NULL_TREE
);
2108 return fold_convert (ptr_type_node
, build_address (var
));
2113 if (TREE_CODE (arg0
) == ADDR_EXPR
2114 && VAR_P (TREE_OPERAND (arg0
, 0)))
2116 tree var
= TREE_OPERAND (arg0
, 0);
2117 if (DECL_NAME (var
) == heap_uninit_identifier
2118 || DECL_NAME (var
) == heap_identifier
)
2120 DECL_NAME (var
) = heap_deleted_identifier
;
2121 ctx
->global
->values
.remove (var
);
2122 ctx
->global
->heap_dealloc_count
++;
2125 else if (DECL_NAME (var
) == heap_deleted_identifier
)
2128 error_at (loc
, "deallocation of already deallocated "
2130 *non_constant_p
= true;
2135 error_at (loc
, "deallocation of storage that was "
2136 "not previously allocated");
2137 *non_constant_p
= true;
2141 /* Allow placement new in std::construct_at, just return the second
2143 if (TREE_CODE (t
) == CALL_EXPR
2144 && cxx_placement_new_fn (fun
)
2146 && ctx
->call
->fundef
2147 && is_std_construct_at (ctx
->call
->fundef
->decl
))
2149 const int nargs
= call_expr_nargs (t
);
2150 tree arg1
= NULL_TREE
;
2151 for (int i
= 0; i
< nargs
; ++i
)
2153 tree arg
= CALL_EXPR_ARG (t
, i
);
2154 arg
= cxx_eval_constant_expression (ctx
, arg
, false,
2155 non_constant_p
, overflow_p
);
2156 VERIFY_CONSTANT (arg
);
2163 else if (cxx_dynamic_cast_fn_p (fun
))
2164 return cxx_eval_dynamic_cast_fn (ctx
, t
, non_constant_p
, overflow_p
);
2168 if (!lambda_static_thunk_p (fun
))
2169 error_at (loc
, "call to non-%<constexpr%> function %qD", fun
);
2170 explain_invalid_constexpr_fn (fun
);
2172 *non_constant_p
= true;
2176 constexpr_ctx new_ctx
= *ctx
;
2177 if (DECL_CONSTRUCTOR_P (fun
) && !ctx
->object
2178 && TREE_CODE (t
) == AGGR_INIT_EXPR
)
2180 /* We want to have an initialization target for an AGGR_INIT_EXPR.
2181 If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
2182 new_ctx
.object
= AGGR_INIT_EXPR_SLOT (t
);
2183 tree ctor
= new_ctx
.ctor
= build_constructor (DECL_CONTEXT (fun
), NULL
);
2184 CONSTRUCTOR_NO_CLEARING (ctor
) = true;
2185 ctx
->global
->values
.put (new_ctx
.object
, ctor
);
2189 /* Shortcut trivial constructor/op=. */
2190 if (trivial_fn_p (fun
))
2192 tree init
= NULL_TREE
;
2193 if (call_expr_nargs (t
) == 2)
2194 init
= convert_from_reference (get_nth_callarg (t
, 1));
2195 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
2196 && AGGR_INIT_ZERO_FIRST (t
))
2197 init
= build_zero_init (DECL_CONTEXT (fun
), NULL_TREE
, false);
2200 tree op
= get_nth_callarg (t
, 0);
2201 if (is_dummy_object (op
))
2204 op
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (op
)), op
);
2205 tree set
= build2 (MODIFY_EXPR
, TREE_TYPE (op
), op
, init
);
2206 new_ctx
.call
= &new_call
;
2207 return cxx_eval_constant_expression (&new_ctx
, set
, lval
,
2208 non_constant_p
, overflow_p
);
2212 /* We can't defer instantiating the function any longer. */
2213 if (!DECL_INITIAL (fun
)
2214 && DECL_TEMPLOID_INSTANTIATION (fun
))
2216 location_t save_loc
= input_location
;
2217 input_location
= loc
;
2219 instantiate_decl (fun
, /*defer_ok*/false, /*expl_inst*/false);
2221 input_location
= save_loc
;
2224 /* If in direct recursive call, optimize definition search. */
2225 if (ctx
&& ctx
->call
&& ctx
->call
->fundef
&& ctx
->call
->fundef
->decl
== fun
)
2226 new_call
.fundef
= ctx
->call
->fundef
;
2229 new_call
.fundef
= retrieve_constexpr_fundef (fun
);
2230 if (new_call
.fundef
== NULL
|| new_call
.fundef
->body
== NULL
2231 || new_call
.fundef
->result
== error_mark_node
2232 || fun
== current_function_decl
)
2236 /* We need to check for current_function_decl here in case we're
2237 being called during cp_fold_function, because at that point
2238 DECL_INITIAL is set properly and we have a fundef but we
2239 haven't lowered invisirefs yet (c++/70344). */
2240 if (DECL_INITIAL (fun
) == error_mark_node
2241 || fun
== current_function_decl
)
2242 error_at (loc
, "%qD called in a constant expression before its "
2243 "definition is complete", fun
);
2244 else if (DECL_INITIAL (fun
))
2246 /* The definition of fun was somehow unsuitable. But pretend
2247 that lambda static thunks don't exist. */
2248 if (!lambda_static_thunk_p (fun
))
2249 error_at (loc
, "%qD called in a constant expression", fun
);
2250 explain_invalid_constexpr_fn (fun
);
2253 error_at (loc
, "%qD used before its definition", fun
);
2255 *non_constant_p
= true;
2260 bool non_constant_args
= false;
2261 cxx_bind_parameters_in_call (ctx
, t
, &new_call
,
2262 non_constant_p
, overflow_p
, &non_constant_args
);
2264 /* We build up the bindings list before we know whether we already have this
2265 call cached. If we don't end up saving these bindings, ggc_free them when
2266 this function exits. */
2271 free_bindings (tree
&b
): bindings (&b
) { }
2272 ~free_bindings () { if (bindings
) ggc_free (*bindings
); }
2273 void preserve () { bindings
= NULL
; }
2274 } fb (new_call
.bindings
);
2276 if (*non_constant_p
)
2279 depth_ok
= push_cx_call_context (t
);
2281 /* Remember the object we are constructing. */
2282 tree new_obj
= NULL_TREE
;
2283 if (DECL_CONSTRUCTOR_P (fun
))
2285 /* In a constructor, it should be the first `this' argument.
2286 At this point it has already been evaluated in the call
2287 to cxx_bind_parameters_in_call. */
2288 new_obj
= TREE_VEC_ELT (new_call
.bindings
, 0);
2289 STRIP_NOPS (new_obj
);
2290 if (TREE_CODE (new_obj
) == ADDR_EXPR
)
2291 new_obj
= TREE_OPERAND (new_obj
, 0);
2294 tree result
= NULL_TREE
;
2296 constexpr_call
*entry
= NULL
;
2297 if (depth_ok
&& !non_constant_args
&& ctx
->strict
)
2299 new_call
.hash
= constexpr_fundef_hasher::hash (new_call
.fundef
);
2301 = iterative_hash_template_arg (new_call
.bindings
, new_call
.hash
);
2303 = iterative_hash_object (ctx
->manifestly_const_eval
, new_call
.hash
);
2305 /* If we have seen this call before, we are done. */
2306 maybe_initialize_constexpr_call_table ();
2307 constexpr_call
**slot
2308 = constexpr_call_table
->find_slot (&new_call
, INSERT
);
2312 /* Only cache up to constexpr_cache_depth to limit memory use. */
2313 if (depth_ok
< constexpr_cache_depth
)
2315 /* We need to keep a pointer to the entry, not just the slot, as
2316 the slot can move during evaluation of the body. */
2317 *slot
= entry
= ggc_alloc
<constexpr_call
> ();
2322 /* Calls that are in progress have their result set to NULL, so that we
2323 can detect circular dependencies. Now that we only cache up to
2324 constexpr_cache_depth this won't catch circular dependencies that
2325 start deeper, but they'll hit the recursion or ops limit. */
2326 else if (entry
->result
== NULL
)
2329 error ("call has circular dependency");
2330 *non_constant_p
= true;
2331 entry
->result
= result
= error_mark_node
;
2334 result
= entry
->result
;
2340 error ("%<constexpr%> evaluation depth exceeds maximum of %d (use "
2341 "%<-fconstexpr-depth=%> to increase the maximum)",
2342 max_constexpr_depth
);
2343 *non_constant_p
= true;
2344 result
= error_mark_node
;
2348 bool cacheable
= true;
2349 if (result
&& result
!= error_mark_node
)
2351 else if (!DECL_SAVED_TREE (fun
))
2353 /* When at_eof >= 2, cgraph has started throwing away
2354 DECL_SAVED_TREE, so fail quietly. FIXME we get here because of
2355 late code generation for VEC_INIT_EXPR, which needs to be
2356 completely reconsidered. */
2357 gcc_assert (at_eof
>= 2 && ctx
->quiet
);
2358 *non_constant_p
= true;
2362 tree body
, parms
, res
;
2363 releasing_vec ctors
;
2365 /* Reuse or create a new unshared copy of this function's body. */
2366 tree copy
= get_fundef_copy (new_call
.fundef
);
2367 body
= TREE_PURPOSE (copy
);
2368 parms
= TREE_VALUE (copy
);
2369 res
= TREE_TYPE (copy
);
2371 /* Associate the bindings with the remapped parms. */
2372 tree bound
= new_call
.bindings
;
2373 tree remapped
= parms
;
2374 for (int i
= 0; i
< TREE_VEC_LENGTH (bound
); ++i
)
2376 tree arg
= TREE_VEC_ELT (bound
, i
);
2379 /* Unshare args going into the hash table to separate them
2380 from the caller's context, for better GC and to avoid
2381 problems with verify_gimple. */
2382 arg
= unshare_expr_without_location (arg
);
2383 TREE_VEC_ELT (bound
, i
) = arg
;
2385 /* Don't share a CONSTRUCTOR that might be changed. This is not
2386 redundant with the unshare just above; we also don't want to
2387 change the argument values in the hash table. XXX Could we
2388 unshare lazily in cxx_eval_store_expression? */
2389 arg
= unshare_constructor (arg
);
2390 if (TREE_CODE (arg
) == CONSTRUCTOR
)
2391 vec_safe_push (ctors
, arg
);
2392 ctx
->global
->values
.put (remapped
, arg
);
2393 remapped
= DECL_CHAIN (remapped
);
2395 /* Add the RESULT_DECL to the values map, too. */
2396 gcc_assert (!DECL_BY_REFERENCE (res
));
2397 ctx
->global
->values
.put (res
, NULL_TREE
);
2399 /* Track the callee's evaluated SAVE_EXPRs and TARGET_EXPRs so that
2400 we can forget their values after the call. */
2401 constexpr_ctx ctx_with_save_exprs
= *ctx
;
2402 auto_vec
<tree
, 10> save_exprs
;
2403 ctx_with_save_exprs
.save_exprs
= &save_exprs
;
2404 ctx_with_save_exprs
.call
= &new_call
;
2405 unsigned save_heap_alloc_count
= ctx
->global
->heap_vars
.length ();
2406 unsigned save_heap_dealloc_count
= ctx
->global
->heap_dealloc_count
;
2408 tree jump_target
= NULL_TREE
;
2409 cxx_eval_constant_expression (&ctx_with_save_exprs
, body
,
2410 lval
, non_constant_p
, overflow_p
,
2413 if (DECL_CONSTRUCTOR_P (fun
))
2414 /* This can be null for a subobject constructor call, in
2415 which case what we care about is the initialization
2416 side-effects rather than the value. We could get at the
2417 value by evaluating *this, but we don't bother; there's
2418 no need to put such a call in the hash table. */
2419 result
= lval
? ctx
->object
: ctx
->ctor
;
2420 else if (VOID_TYPE_P (TREE_TYPE (res
)))
2424 result
= *ctx
->global
->values
.get (res
);
2425 if (result
== NULL_TREE
&& !*non_constant_p
)
2428 error ("%<constexpr%> call flows off the end "
2430 *non_constant_p
= true;
2434 /* At this point, the object's constructor will have run, so
2435 the object is no longer under construction, and its possible
2436 'const' semantics now apply. Make a note of this fact by
2437 marking the CONSTRUCTOR TREE_READONLY. */
2439 && CLASS_TYPE_P (TREE_TYPE (new_obj
))
2440 && CP_TYPE_CONST_P (TREE_TYPE (new_obj
)))
2442 /* Subobjects might not be stored in ctx->global->values but we
2443 can get its CONSTRUCTOR by evaluating *this. */
2444 tree e
= cxx_eval_constant_expression (ctx
, new_obj
,
2448 TREE_READONLY (e
) = true;
2451 /* Forget the saved values of the callee's SAVE_EXPRs and
2455 FOR_EACH_VEC_ELT (save_exprs
, i
, save_expr
)
2456 ctx
->global
->values
.remove (save_expr
);
2458 /* Remove the parms/result from the values map. Is it worth
2459 bothering to do this when the map itself is only live for
2460 one constexpr evaluation? If so, maybe also clear out
2461 other vars from call, maybe in BIND_EXPR handling? */
2462 ctx
->global
->values
.remove (res
);
2463 for (tree parm
= parms
; parm
; parm
= TREE_CHAIN (parm
))
2464 ctx
->global
->values
.remove (parm
);
2466 /* Free any parameter CONSTRUCTORs we aren't returning directly. */
2467 while (!ctors
->is_empty ())
2469 tree c
= ctors
->pop ();
2471 free_constructor (c
);
2474 /* Make the unshared function copy we used available for re-use. */
2475 save_fundef_copy (fun
, copy
);
2477 /* If the call allocated some heap object that hasn't been
2478 deallocated during the call, or if it deallocated some heap
2479 object it has not allocated, the call isn't really stateless
2480 for the constexpr evaluation and should not be cached.
2481 It is fine if the call allocates something and deallocates it
2484 && (save_heap_alloc_count
!= ctx
->global
->heap_vars
.length ()
2485 || (save_heap_dealloc_count
2486 != ctx
->global
->heap_dealloc_count
)))
2490 if ((ctx
->global
->heap_vars
.length ()
2491 - ctx
->global
->heap_dealloc_count
)
2492 != save_heap_alloc_count
- save_heap_dealloc_count
)
2495 FOR_EACH_VEC_ELT_FROM (ctx
->global
->heap_vars
, i
, heap_var
,
2496 save_heap_alloc_count
)
2497 if (DECL_NAME (heap_var
) != heap_deleted_identifier
)
2505 if (result
== error_mark_node
)
2506 *non_constant_p
= true;
2507 if (*non_constant_p
|| *overflow_p
)
2508 result
= error_mark_node
;
2512 entry
->result
= cacheable
? result
: error_mark_node
;
2515 /* The result of a constexpr function must be completely initialized.
2517 However, in C++20, a constexpr constructor doesn't necessarily have
2518 to initialize all the fields, so we don't clear CONSTRUCTOR_NO_CLEARING
2519 in order to detect reading an unitialized object in constexpr instead
2520 of value-initializing it. (reduced_constant_expression_p is expected to
2521 take care of clearing the flag.) */
2522 if (TREE_CODE (result
) == CONSTRUCTOR
2523 && (cxx_dialect
< cxx2a
2524 || !DECL_CONSTRUCTOR_P (fun
)))
2525 clear_no_implicit_zero (result
);
2527 pop_cx_call_context ();
2531 /* Return true if T is a valid constant initializer. If a CONSTRUCTOR
2532 initializes all the members, the CONSTRUCTOR_NO_CLEARING flag will be
2534 FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
2537 reduced_constant_expression_p (tree t
)
2542 switch (TREE_CODE (t
))
2545 /* Even if we can't lower this yet, it's constant. */
2549 /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
2550 tree idx
, val
, field
; unsigned HOST_WIDE_INT i
;
2551 if (CONSTRUCTOR_NO_CLEARING (t
))
2553 if (TREE_CODE (TREE_TYPE (t
)) == VECTOR_TYPE
)
2554 /* An initialized vector would have a VECTOR_CST. */
2556 else if (cxx_dialect
>= cxx2a
2557 /* An ARRAY_TYPE doesn't have any TYPE_FIELDS. */
2558 && (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
2559 /* A union only initializes one member. */
2560 || TREE_CODE (TREE_TYPE (t
)) == UNION_TYPE
))
2563 field
= next_initializable_field (TYPE_FIELDS (TREE_TYPE (t
)));
2567 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t
), i
, idx
, val
)
2569 /* If VAL is null, we're in the middle of initializing this
2571 if (!reduced_constant_expression_p (val
))
2575 /* Empty class field may or may not have an initializer. */
2576 for (; idx
!= field
;
2577 field
= next_initializable_field (DECL_CHAIN (field
)))
2578 if (!is_really_empty_class (TREE_TYPE (field
),
2579 /*ignore_vptr*/false))
2581 field
= next_initializable_field (DECL_CHAIN (field
));
2584 /* There could be a non-empty field at the end. */
2585 for (; field
; field
= next_initializable_field (DECL_CHAIN (field
)))
2586 if (!is_really_empty_class (TREE_TYPE (field
), /*ignore_vptr*/false))
2588 if (CONSTRUCTOR_NO_CLEARING (t
))
2589 /* All the fields are initialized. */
2590 CONSTRUCTOR_NO_CLEARING (t
) = false;
2594 /* FIXME are we calling this too much? */
2595 return initializer_constant_valid_p (t
, TREE_TYPE (t
)) != NULL_TREE
;
2599 /* Some expressions may have constant operands but are not constant
2600 themselves, such as 1/0. Call this function to check for that
2603 We only call this in places that require an arithmetic constant, not in
2604 places where we might have a non-constant expression that can be a
2605 component of a constant expression, such as the address of a constexpr
2606 variable that might be dereferenced later. */
2609 verify_constant (tree t
, bool allow_non_constant
, bool *non_constant_p
,
2612 if (!*non_constant_p
&& !reduced_constant_expression_p (t
)
2615 if (!allow_non_constant
)
2616 error ("%q+E is not a constant expression", t
);
2617 *non_constant_p
= true;
2619 if (TREE_OVERFLOW_P (t
))
2621 if (!allow_non_constant
)
2623 permerror (input_location
, "overflow in constant expression");
2624 /* If we're being permissive (and are in an enforcing
2625 context), ignore the overflow. */
2626 if (flag_permissive
)
2627 return *non_constant_p
;
2631 return *non_constant_p
;
2634 /* Check whether the shift operation with code CODE and type TYPE on LHS
2635 and RHS is undefined. If it is, give an error with an explanation,
2636 and return true; return false otherwise. */
2639 cxx_eval_check_shift_p (location_t loc
, const constexpr_ctx
*ctx
,
2640 enum tree_code code
, tree type
, tree lhs
, tree rhs
)
2642 if ((code
!= LSHIFT_EXPR
&& code
!= RSHIFT_EXPR
)
2643 || TREE_CODE (lhs
) != INTEGER_CST
2644 || TREE_CODE (rhs
) != INTEGER_CST
)
2647 tree lhstype
= TREE_TYPE (lhs
);
2648 unsigned HOST_WIDE_INT uprec
= TYPE_PRECISION (TREE_TYPE (lhs
));
2650 /* [expr.shift] The behavior is undefined if the right operand
2651 is negative, or greater than or equal to the length in bits
2652 of the promoted left operand. */
2653 if (tree_int_cst_sgn (rhs
) == -1)
2656 permerror (loc
, "right operand of shift expression %q+E is negative",
2657 build2_loc (loc
, code
, type
, lhs
, rhs
));
2658 return (!flag_permissive
|| ctx
->quiet
);
2660 if (compare_tree_int (rhs
, uprec
) >= 0)
2663 permerror (loc
, "right operand of shift expression %q+E is greater "
2664 "than or equal to the precision %wu of the left operand",
2665 build2_loc (loc
, code
, type
, lhs
, rhs
), uprec
);
2666 return (!flag_permissive
|| ctx
->quiet
);
2669 /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
2670 if E1 has a signed type and non-negative value, and E1x2^E2 is
2671 representable in the corresponding unsigned type of the result type,
2672 then that value, converted to the result type, is the resulting value;
2673 otherwise, the behavior is undefined.
2675 The value of E1 << E2 is the unique value congruent to E1 x 2^E2 modulo
2676 2^N, where N is the range exponent of the type of the result. */
2677 if (code
== LSHIFT_EXPR
2678 && !TYPE_UNSIGNED (lhstype
)
2679 && cxx_dialect
>= cxx11
2680 && cxx_dialect
< cxx2a
)
2682 if (tree_int_cst_sgn (lhs
) == -1)
2686 "left operand of shift expression %q+E is negative",
2687 build2_loc (loc
, code
, type
, lhs
, rhs
));
2688 return (!flag_permissive
|| ctx
->quiet
);
2690 /* For signed x << y the following:
2691 (unsigned) x >> ((prec (lhs) - 1) - y)
2692 if > 1, is undefined. The right-hand side of this formula
2693 is the highest bit of the LHS that can be set (starting from 0),
2694 so that the shift doesn't overflow. We then right-shift the LHS
2695 to see whether any other bit is set making the original shift
2696 undefined -- the result is not representable in the corresponding
2698 tree t
= build_int_cst (unsigned_type_node
, uprec
- 1);
2699 t
= fold_build2 (MINUS_EXPR
, unsigned_type_node
, t
, rhs
);
2700 tree ulhs
= fold_convert (unsigned_type_for (lhstype
), lhs
);
2701 t
= fold_build2 (RSHIFT_EXPR
, TREE_TYPE (ulhs
), ulhs
, t
);
2702 if (tree_int_cst_lt (integer_one_node
, t
))
2705 permerror (loc
, "shift expression %q+E overflows",
2706 build2_loc (loc
, code
, type
, lhs
, rhs
));
2707 return (!flag_permissive
|| ctx
->quiet
);
2713 /* Subroutine of cxx_eval_constant_expression.
2714 Attempt to reduce the unary expression tree T to a compile time value.
2715 If successful, return the value. Otherwise issue a diagnostic
2716 and return error_mark_node. */
2719 cxx_eval_unary_expression (const constexpr_ctx
*ctx
, tree t
,
2721 bool *non_constant_p
, bool *overflow_p
)
2724 tree orig_arg
= TREE_OPERAND (t
, 0);
2725 tree arg
= cxx_eval_constant_expression (ctx
, orig_arg
, /*lval*/false,
2726 non_constant_p
, overflow_p
);
2727 VERIFY_CONSTANT (arg
);
2728 location_t loc
= EXPR_LOCATION (t
);
2729 enum tree_code code
= TREE_CODE (t
);
2730 tree type
= TREE_TYPE (t
);
2731 r
= fold_unary_loc (loc
, code
, type
, arg
);
2734 if (arg
== orig_arg
)
2737 r
= build1_loc (loc
, code
, type
, arg
);
2739 VERIFY_CONSTANT (r
);
2743 /* Helper function for cxx_eval_binary_expression. Try to optimize
2744 original POINTER_PLUS_EXPR T, LHS p+ RHS, return NULL_TREE if the
2745 generic folding should be used. */
2748 cxx_fold_pointer_plus_expression (const constexpr_ctx
*ctx
, tree t
,
2749 tree lhs
, tree rhs
, bool *non_constant_p
,
2753 if (TREE_CODE (lhs
) != ADDR_EXPR
)
2756 lhs
= TREE_OPERAND (lhs
, 0);
2758 /* &A[i] p+ j => &A[i + j] */
2759 if (TREE_CODE (lhs
) == ARRAY_REF
2760 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == INTEGER_CST
2761 && TREE_CODE (rhs
) == INTEGER_CST
2762 && TYPE_SIZE_UNIT (TREE_TYPE (lhs
))
2763 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs
))) == INTEGER_CST
)
2765 tree orig_type
= TREE_TYPE (t
);
2766 location_t loc
= EXPR_LOCATION (t
);
2767 tree type
= TREE_TYPE (lhs
);
2769 t
= fold_convert_loc (loc
, ssizetype
, TREE_OPERAND (lhs
, 1));
2770 tree nelts
= array_type_nelts_top (TREE_TYPE (TREE_OPERAND (lhs
, 0)));
2771 nelts
= cxx_eval_constant_expression (ctx
, nelts
, false, non_constant_p
,
2773 if (*non_constant_p
)
2775 /* Don't fold an out-of-bound access. */
2776 if (!tree_int_cst_le (t
, nelts
))
2778 rhs
= cp_fold_convert (ssizetype
, rhs
);
2779 /* Don't fold if rhs can't be divided exactly by TYPE_SIZE_UNIT.
2780 constexpr int A[1]; ... (char *)&A[0] + 1 */
2781 if (!integer_zerop (fold_build2_loc (loc
, TRUNC_MOD_EXPR
, sizetype
,
2782 rhs
, TYPE_SIZE_UNIT (type
))))
2784 /* Make sure to treat the second operand of POINTER_PLUS_EXPR
2786 rhs
= fold_build2_loc (loc
, EXACT_DIV_EXPR
, ssizetype
, rhs
,
2787 TYPE_SIZE_UNIT (type
));
2788 t
= size_binop_loc (loc
, PLUS_EXPR
, rhs
, t
);
2789 t
= build4_loc (loc
, ARRAY_REF
, type
, TREE_OPERAND (lhs
, 0),
2790 t
, NULL_TREE
, NULL_TREE
);
2791 t
= cp_build_addr_expr (t
, tf_warning_or_error
);
2792 t
= cp_fold_convert (orig_type
, t
);
2793 return cxx_eval_constant_expression (ctx
, t
, /*lval*/false,
2794 non_constant_p
, overflow_p
);
2800 /* Subroutine of cxx_eval_constant_expression.
2801 Like cxx_eval_unary_expression, except for binary expressions. */
2804 cxx_eval_binary_expression (const constexpr_ctx
*ctx
, tree t
,
2806 bool *non_constant_p
, bool *overflow_p
)
2809 tree orig_lhs
= TREE_OPERAND (t
, 0);
2810 tree orig_rhs
= TREE_OPERAND (t
, 1);
2812 lhs
= cxx_eval_constant_expression (ctx
, orig_lhs
, /*lval*/false,
2813 non_constant_p
, overflow_p
);
2814 /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer
2816 if (*non_constant_p
)
2818 rhs
= cxx_eval_constant_expression (ctx
, orig_rhs
, /*lval*/false,
2819 non_constant_p
, overflow_p
);
2820 if (*non_constant_p
)
2823 location_t loc
= EXPR_LOCATION (t
);
2824 enum tree_code code
= TREE_CODE (t
);
2825 tree type
= TREE_TYPE (t
);
2827 if (code
== EQ_EXPR
|| code
== NE_EXPR
)
2829 bool is_code_eq
= (code
== EQ_EXPR
);
2831 if (TREE_CODE (lhs
) == PTRMEM_CST
2832 && TREE_CODE (rhs
) == PTRMEM_CST
)
2834 tree lmem
= PTRMEM_CST_MEMBER (lhs
);
2835 tree rmem
= PTRMEM_CST_MEMBER (rhs
);
2837 if (TREE_CODE (lmem
) == TREE_CODE (rmem
)
2838 && TREE_CODE (lmem
) == FIELD_DECL
2839 && TREE_CODE (DECL_CONTEXT (lmem
)) == UNION_TYPE
2840 && same_type_p (DECL_CONTEXT (lmem
),
2841 DECL_CONTEXT (rmem
)))
2842 /* If both refer to (possibly different) members of the same union
2843 (12.3), they compare equal. */
2846 eq
= cp_tree_equal (lhs
, rhs
);
2847 r
= constant_boolean_node (eq
== is_code_eq
, type
);
2849 else if ((TREE_CODE (lhs
) == PTRMEM_CST
2850 || TREE_CODE (rhs
) == PTRMEM_CST
)
2851 && (null_member_pointer_value_p (lhs
)
2852 || null_member_pointer_value_p (rhs
)))
2853 r
= constant_boolean_node (!is_code_eq
, type
);
2854 else if (TREE_CODE (lhs
) == PTRMEM_CST
)
2855 lhs
= cplus_expand_constant (lhs
);
2856 else if (TREE_CODE (rhs
) == PTRMEM_CST
)
2857 rhs
= cplus_expand_constant (rhs
);
2859 if (code
== POINTER_PLUS_EXPR
&& !*non_constant_p
2860 && integer_zerop (lhs
) && !integer_zerop (rhs
))
2863 error ("arithmetic involving a null pointer in %qE", lhs
);
2864 *non_constant_p
= true;
2867 else if (code
== POINTER_PLUS_EXPR
)
2868 r
= cxx_fold_pointer_plus_expression (ctx
, t
, lhs
, rhs
, non_constant_p
,
2870 else if (code
== SPACESHIP_EXPR
)
2872 r
= genericize_spaceship (type
, lhs
, rhs
);
2873 r
= cxx_eval_constant_expression (ctx
, r
, false, non_constant_p
,
2878 r
= fold_binary_loc (loc
, code
, type
, lhs
, rhs
);
2882 if (lhs
== orig_lhs
&& rhs
== orig_rhs
)
2885 r
= build2_loc (loc
, code
, type
, lhs
, rhs
);
2887 else if (cxx_eval_check_shift_p (loc
, ctx
, code
, type
, lhs
, rhs
))
2888 *non_constant_p
= true;
2889 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
2890 a local array in a constexpr function. */
2891 bool ptr
= INDIRECT_TYPE_P (TREE_TYPE (lhs
));
2893 VERIFY_CONSTANT (r
);
2897 /* Subroutine of cxx_eval_constant_expression.
2898 Attempt to evaluate condition expressions. Dead branches are not
2902 cxx_eval_conditional_expression (const constexpr_ctx
*ctx
, tree t
,
2904 bool *non_constant_p
, bool *overflow_p
,
2907 tree val
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
2909 non_constant_p
, overflow_p
);
2910 VERIFY_CONSTANT (val
);
2911 /* Don't VERIFY_CONSTANT the other operands. */
2912 if (integer_zerop (val
))
2913 val
= TREE_OPERAND (t
, 2);
2915 val
= TREE_OPERAND (t
, 1);
2916 if (TREE_CODE (t
) == IF_STMT
&& !val
)
2918 return cxx_eval_constant_expression (ctx
, val
, lval
, non_constant_p
,
2919 overflow_p
, jump_target
);
2922 /* Subroutine of cxx_eval_constant_expression.
2923 Attempt to evaluate vector condition expressions. Unlike
2924 cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal
2925 ternary arithmetics operation, where all 3 arguments have to be
2926 evaluated as constants and then folding computes the result from
2930 cxx_eval_vector_conditional_expression (const constexpr_ctx
*ctx
, tree t
,
2931 bool *non_constant_p
, bool *overflow_p
)
2933 tree arg1
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
2935 non_constant_p
, overflow_p
);
2936 VERIFY_CONSTANT (arg1
);
2937 tree arg2
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
2939 non_constant_p
, overflow_p
);
2940 VERIFY_CONSTANT (arg2
);
2941 tree arg3
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 2),
2943 non_constant_p
, overflow_p
);
2944 VERIFY_CONSTANT (arg3
);
2945 location_t loc
= EXPR_LOCATION (t
);
2946 tree type
= TREE_TYPE (t
);
2947 tree r
= fold_ternary_loc (loc
, VEC_COND_EXPR
, type
, arg1
, arg2
, arg3
);
2950 if (arg1
== TREE_OPERAND (t
, 0)
2951 && arg2
== TREE_OPERAND (t
, 1)
2952 && arg3
== TREE_OPERAND (t
, 2))
2955 r
= build3_loc (loc
, VEC_COND_EXPR
, type
, arg1
, arg2
, arg3
);
2957 VERIFY_CONSTANT (r
);
2961 /* Returns less than, equal to, or greater than zero if KEY is found to be
2962 less than, to match, or to be greater than the constructor_elt's INDEX. */
2965 array_index_cmp (tree key
, tree index
)
2967 gcc_assert (TREE_CODE (key
) == INTEGER_CST
);
2969 switch (TREE_CODE (index
))
2972 return tree_int_cst_compare (key
, index
);
2975 tree lo
= TREE_OPERAND (index
, 0);
2976 tree hi
= TREE_OPERAND (index
, 1);
2977 if (tree_int_cst_lt (key
, lo
))
2979 else if (tree_int_cst_lt (hi
, key
))
2989 /* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1
2990 if none. If INSERT is true, insert a matching element rather than fail. */
2992 static HOST_WIDE_INT
2993 find_array_ctor_elt (tree ary
, tree dindex
, bool insert
)
2995 if (tree_int_cst_sgn (dindex
) < 0)
2998 unsigned HOST_WIDE_INT i
= tree_to_uhwi (dindex
);
2999 vec
<constructor_elt
, va_gc
> *elts
= CONSTRUCTOR_ELTS (ary
);
3000 unsigned HOST_WIDE_INT len
= vec_safe_length (elts
);
3002 unsigned HOST_WIDE_INT end
= len
;
3003 unsigned HOST_WIDE_INT begin
= 0;
3005 /* If the last element of the CONSTRUCTOR has its own index, we can assume
3006 that the same is true of the other elements and index directly. */
3009 tree cindex
= (*elts
)[end
- 1].index
;
3010 if (TREE_CODE (cindex
) == INTEGER_CST
3011 && compare_tree_int (cindex
, end
- 1) == 0)
3020 /* Otherwise, find a matching index by means of a binary search. */
3021 while (begin
!= end
)
3023 unsigned HOST_WIDE_INT middle
= (begin
+ end
) / 2;
3024 constructor_elt
&elt
= (*elts
)[middle
];
3025 tree idx
= elt
.index
;
3027 int cmp
= array_index_cmp (dindex
, idx
);
3034 if (insert
&& TREE_CODE (idx
) == RANGE_EXPR
)
3036 /* We need to split the range. */
3038 tree lo
= TREE_OPERAND (idx
, 0);
3039 tree hi
= TREE_OPERAND (idx
, 1);
3040 tree value
= elt
.value
;
3041 dindex
= fold_convert (sizetype
, dindex
);
3042 if (tree_int_cst_lt (lo
, dindex
))
3044 /* There are still some lower elts; shorten the range. */
3045 tree new_hi
= int_const_binop (MINUS_EXPR
, dindex
,
3047 if (tree_int_cst_equal (lo
, new_hi
))
3048 /* Only one element left, no longer a range. */
3051 TREE_OPERAND (idx
, 1) = new_hi
;
3052 /* Append the element we want to insert. */
3055 e
.value
= unshare_constructor (value
);
3056 vec_safe_insert (CONSTRUCTOR_ELTS (ary
), middle
, e
);
3059 /* No lower elts, the range elt is now ours. */
3062 if (tree_int_cst_lt (dindex
, hi
))
3064 /* There are still some higher elts; append a range. */
3065 tree new_lo
= int_const_binop (PLUS_EXPR
, dindex
,
3067 if (tree_int_cst_equal (new_lo
, hi
))
3070 e
.index
= build2 (RANGE_EXPR
, sizetype
, new_lo
, hi
);
3071 e
.value
= unshare_constructor (value
);
3072 vec_safe_insert (CONSTRUCTOR_ELTS (ary
), middle
+ 1, e
);
3081 constructor_elt e
= { dindex
, NULL_TREE
};
3082 vec_safe_insert (CONSTRUCTOR_ELTS (ary
), end
, e
);
3089 /* Under the control of CTX, issue a detailed diagnostic for
3090 an out-of-bounds subscript INDEX into the expression ARRAY. */
3093 diag_array_subscript (location_t loc
, const constexpr_ctx
*ctx
, tree array
, tree index
)
3097 tree arraytype
= TREE_TYPE (array
);
3099 /* Convert the unsigned array subscript to a signed integer to avoid
3100 printing huge numbers for small negative values. */
3101 tree sidx
= fold_convert (ssizetype
, index
);
3102 STRIP_ANY_LOCATION_WRAPPER (array
);
3105 if (TYPE_DOMAIN (arraytype
))
3106 error_at (loc
, "array subscript value %qE is outside the bounds "
3107 "of array %qD of type %qT", sidx
, array
, arraytype
);
3109 error_at (loc
, "nonzero array subscript %qE is used with array %qD of "
3110 "type %qT with unknown bounds", sidx
, array
, arraytype
);
3111 inform (DECL_SOURCE_LOCATION (array
), "declared here");
3113 else if (TYPE_DOMAIN (arraytype
))
3114 error_at (loc
, "array subscript value %qE is outside the bounds "
3115 "of array type %qT", sidx
, arraytype
);
3117 error_at (loc
, "nonzero array subscript %qE is used with array of type %qT "
3118 "with unknown bounds", sidx
, arraytype
);
3122 /* Return the number of elements for TYPE (which is an ARRAY_TYPE or
3126 get_array_or_vector_nelts (const constexpr_ctx
*ctx
, tree type
,
3127 bool *non_constant_p
, bool *overflow_p
)
3130 if (TREE_CODE (type
) == ARRAY_TYPE
)
3132 if (TYPE_DOMAIN (type
))
3133 nelts
= array_type_nelts_top (type
);
3135 nelts
= size_zero_node
;
3137 else if (VECTOR_TYPE_P (type
))
3138 nelts
= size_int (TYPE_VECTOR_SUBPARTS (type
));
3142 /* For VLAs, the number of elements won't be an integer constant. */
3143 nelts
= cxx_eval_constant_expression (ctx
, nelts
, false,
3144 non_constant_p
, overflow_p
);
3148 /* Extract element INDEX consisting of CHARS_PER_ELT chars from
3149 STRING_CST STRING. */
3152 extract_string_elt (tree string
, unsigned chars_per_elt
, unsigned index
)
3154 tree type
= cv_unqualified (TREE_TYPE (TREE_TYPE (string
)));
3157 if (chars_per_elt
== 1)
3158 r
= build_int_cst (type
, TREE_STRING_POINTER (string
)[index
]);
3161 const unsigned char *ptr
3162 = ((const unsigned char *)TREE_STRING_POINTER (string
)
3163 + index
* chars_per_elt
);
3164 r
= native_interpret_expr (type
, ptr
, chars_per_elt
);
3169 /* Subroutine of cxx_eval_array_reference. T is an ARRAY_REF; evaluate the
3170 subscript, diagnose any problems with it, and return the result. */
3173 eval_and_check_array_index (const constexpr_ctx
*ctx
,
3174 tree t
, bool allow_one_past
,
3175 bool *non_constant_p
, bool *overflow_p
)
3177 location_t loc
= cp_expr_loc_or_input_loc (t
);
3178 tree ary
= TREE_OPERAND (t
, 0);
3179 t
= TREE_OPERAND (t
, 1);
3180 tree index
= cxx_eval_constant_expression (ctx
, t
, false,
3181 non_constant_p
, overflow_p
);
3182 VERIFY_CONSTANT (index
);
3184 if (!tree_fits_shwi_p (index
)
3185 || tree_int_cst_sgn (index
) < 0)
3187 diag_array_subscript (loc
, ctx
, ary
, index
);
3188 *non_constant_p
= true;
3192 tree nelts
= get_array_or_vector_nelts (ctx
, TREE_TYPE (ary
), non_constant_p
,
3194 VERIFY_CONSTANT (nelts
);
3196 ? !tree_int_cst_le (index
, nelts
)
3197 : !tree_int_cst_lt (index
, nelts
))
3199 diag_array_subscript (loc
, ctx
, ary
, index
);
3200 *non_constant_p
= true;
3207 /* Subroutine of cxx_eval_constant_expression.
3208 Attempt to reduce a reference to an array slot. */
3211 cxx_eval_array_reference (const constexpr_ctx
*ctx
, tree t
,
3213 bool *non_constant_p
, bool *overflow_p
)
3215 tree oldary
= TREE_OPERAND (t
, 0);
3216 tree ary
= cxx_eval_constant_expression (ctx
, oldary
,
3218 non_constant_p
, overflow_p
);
3219 if (*non_constant_p
)
3222 && TREE_CODE (ary
) == VIEW_CONVERT_EXPR
3223 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary
, 0)))
3224 && TREE_TYPE (t
) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary
, 0))))
3225 ary
= TREE_OPERAND (ary
, 0);
3227 tree oldidx
= TREE_OPERAND (t
, 1);
3228 tree index
= eval_and_check_array_index (ctx
, t
, lval
,
3229 non_constant_p
, overflow_p
);
3230 if (*non_constant_p
)
3233 if (lval
&& ary
== oldary
&& index
== oldidx
)
3236 return build4 (ARRAY_REF
, TREE_TYPE (t
), ary
, index
, NULL
, NULL
);
3238 unsigned len
= 0, elem_nchars
= 1;
3239 tree elem_type
= TREE_TYPE (TREE_TYPE (ary
));
3240 if (TREE_CODE (ary
) == CONSTRUCTOR
)
3241 len
= CONSTRUCTOR_NELTS (ary
);
3242 else if (TREE_CODE (ary
) == STRING_CST
)
3244 elem_nchars
= (TYPE_PRECISION (elem_type
)
3245 / TYPE_PRECISION (char_type_node
));
3246 len
= (unsigned) TREE_STRING_LENGTH (ary
) / elem_nchars
;
3248 else if (TREE_CODE (ary
) == VECTOR_CST
)
3249 /* We don't create variable-length VECTOR_CSTs. */
3250 len
= VECTOR_CST_NELTS (ary
).to_constant ();
3253 /* We can't do anything with other tree codes, so use
3254 VERIFY_CONSTANT to complain and fail. */
3255 VERIFY_CONSTANT (ary
);
3260 HOST_WIDE_INT i
= 0;
3261 if (TREE_CODE (ary
) == CONSTRUCTOR
)
3263 HOST_WIDE_INT ix
= find_array_ctor_elt (ary
, index
);
3270 i
= tree_to_shwi (index
);
3277 if (TREE_CODE (ary
) == CONSTRUCTOR
)
3278 r
= (*CONSTRUCTOR_ELTS (ary
))[i
].value
;
3279 else if (TREE_CODE (ary
) == VECTOR_CST
)
3280 r
= VECTOR_CST_ELT (ary
, i
);
3282 r
= extract_string_elt (ary
, elem_nchars
, i
);
3285 /* Don't VERIFY_CONSTANT here. */
3288 /* Otherwise the element doesn't have a value yet. */
3293 if (TREE_CODE (ary
) == CONSTRUCTOR
3294 && CONSTRUCTOR_NO_CLEARING (ary
))
3296 /* 'ary' is part of the aggregate initializer we're currently
3297 building; if there's no initializer for this element yet,
3300 error ("accessing uninitialized array element");
3301 *non_constant_p
= true;
3305 /* If it's within the array bounds but doesn't have an explicit
3306 initializer, it's value-initialized. */
3307 tree val
= build_value_init (elem_type
, tf_warning_or_error
);
3308 return cxx_eval_constant_expression (ctx
, val
, lval
, non_constant_p
,
3312 /* Subroutine of cxx_eval_constant_expression.
3313 Attempt to reduce a field access of a value of class type. */
3316 cxx_eval_component_reference (const constexpr_ctx
*ctx
, tree t
,
3318 bool *non_constant_p
, bool *overflow_p
)
3320 unsigned HOST_WIDE_INT i
;
3323 tree part
= TREE_OPERAND (t
, 1);
3324 tree orig_whole
= TREE_OPERAND (t
, 0);
3325 tree whole
= cxx_eval_constant_expression (ctx
, orig_whole
,
3327 non_constant_p
, overflow_p
);
3328 if (INDIRECT_REF_P (whole
)
3329 && integer_zerop (TREE_OPERAND (whole
, 0)))
3332 error ("dereferencing a null pointer in %qE", orig_whole
);
3333 *non_constant_p
= true;
3337 if (TREE_CODE (whole
) == PTRMEM_CST
)
3338 whole
= cplus_expand_constant (whole
);
3339 if (whole
== orig_whole
)
3342 return fold_build3 (COMPONENT_REF
, TREE_TYPE (t
),
3343 whole
, part
, NULL_TREE
);
3344 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
3346 if (!*non_constant_p
&& TREE_CODE (whole
) != CONSTRUCTOR
)
3349 error ("%qE is not a constant expression", orig_whole
);
3350 *non_constant_p
= true;
3352 if (DECL_MUTABLE_P (part
))
3355 error ("mutable %qD is not usable in a constant expression", part
);
3356 *non_constant_p
= true;
3358 if (*non_constant_p
)
3360 bool pmf
= TYPE_PTRMEMFUNC_P (TREE_TYPE (whole
));
3361 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole
), i
, field
, value
)
3363 /* Use name match for PMF fields, as a variant will have a
3364 different FIELD_DECL with a different type. */
3365 if (pmf
? DECL_NAME (field
) == DECL_NAME (part
)
3370 STRIP_ANY_LOCATION_WRAPPER (value
);
3374 /* We're in the middle of initializing it. */
3378 if (TREE_CODE (TREE_TYPE (whole
)) == UNION_TYPE
3379 && CONSTRUCTOR_NELTS (whole
) > 0)
3381 /* DR 1188 says we don't have to deal with this. */
3383 error ("accessing %qD member instead of initialized %qD member in "
3384 "constant expression", part
, CONSTRUCTOR_ELT (whole
, 0)->index
);
3385 *non_constant_p
= true;
3389 /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty
3390 classes never get represented; throw together a value now. */
3391 if (is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
3392 return build_constructor (TREE_TYPE (t
), NULL
);
3394 gcc_assert (DECL_CONTEXT (part
) == TYPE_MAIN_VARIANT (TREE_TYPE (whole
)));
3396 if (CONSTRUCTOR_NO_CLEARING (whole
))
3398 /* 'whole' is part of the aggregate initializer we're currently
3399 building; if there's no initializer for this member yet, that's an
3402 error ("accessing uninitialized member %qD", part
);
3403 *non_constant_p
= true;
3407 /* If there's no explicit init for this field, it's value-initialized. */
3408 value
= build_value_init (TREE_TYPE (t
), tf_warning_or_error
);
3409 return cxx_eval_constant_expression (ctx
, value
,
3411 non_constant_p
, overflow_p
);
3414 /* Subroutine of cxx_eval_constant_expression.
3415 Attempt to reduce a field access of a value of class type that is
3416 expressed as a BIT_FIELD_REF. */
3419 cxx_eval_bit_field_ref (const constexpr_ctx
*ctx
, tree t
,
3421 bool *non_constant_p
, bool *overflow_p
)
3423 tree orig_whole
= TREE_OPERAND (t
, 0);
3424 tree retval
, fldval
, utype
, mask
;
3425 bool fld_seen
= false;
3426 HOST_WIDE_INT istart
, isize
;
3427 tree whole
= cxx_eval_constant_expression (ctx
, orig_whole
,
3429 non_constant_p
, overflow_p
);
3430 tree start
, field
, value
;
3431 unsigned HOST_WIDE_INT i
;
3433 if (whole
== orig_whole
)
3435 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
3437 if (!*non_constant_p
3438 && TREE_CODE (whole
) != VECTOR_CST
3439 && TREE_CODE (whole
) != CONSTRUCTOR
)
3442 error ("%qE is not a constant expression", orig_whole
);
3443 *non_constant_p
= true;
3445 if (*non_constant_p
)
3448 if (TREE_CODE (whole
) == VECTOR_CST
)
3449 return fold_ternary (BIT_FIELD_REF
, TREE_TYPE (t
), whole
,
3450 TREE_OPERAND (t
, 1), TREE_OPERAND (t
, 2));
3452 start
= TREE_OPERAND (t
, 2);
3453 istart
= tree_to_shwi (start
);
3454 isize
= tree_to_shwi (TREE_OPERAND (t
, 1));
3455 utype
= TREE_TYPE (t
);
3456 if (!TYPE_UNSIGNED (utype
))
3457 utype
= build_nonstandard_integer_type (TYPE_PRECISION (utype
), 1);
3458 retval
= build_int_cst (utype
, 0);
3459 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole
), i
, field
, value
)
3461 tree bitpos
= bit_position (field
);
3462 STRIP_ANY_LOCATION_WRAPPER (value
);
3463 if (bitpos
== start
&& DECL_SIZE (field
) == TREE_OPERAND (t
, 1))
3465 if (TREE_CODE (TREE_TYPE (field
)) == INTEGER_TYPE
3466 && TREE_CODE (value
) == INTEGER_CST
3467 && tree_fits_shwi_p (bitpos
)
3468 && tree_fits_shwi_p (DECL_SIZE (field
)))
3470 HOST_WIDE_INT bit
= tree_to_shwi (bitpos
);
3471 HOST_WIDE_INT sz
= tree_to_shwi (DECL_SIZE (field
));
3472 HOST_WIDE_INT shift
;
3473 if (bit
>= istart
&& bit
+ sz
<= istart
+ isize
)
3475 fldval
= fold_convert (utype
, value
);
3476 mask
= build_int_cst_type (utype
, -1);
3477 mask
= fold_build2 (LSHIFT_EXPR
, utype
, mask
,
3478 size_int (TYPE_PRECISION (utype
) - sz
));
3479 mask
= fold_build2 (RSHIFT_EXPR
, utype
, mask
,
3480 size_int (TYPE_PRECISION (utype
) - sz
));
3481 fldval
= fold_build2 (BIT_AND_EXPR
, utype
, fldval
, mask
);
3482 shift
= bit
- istart
;
3483 if (BYTES_BIG_ENDIAN
)
3484 shift
= TYPE_PRECISION (utype
) - shift
- sz
;
3485 fldval
= fold_build2 (LSHIFT_EXPR
, utype
, fldval
,
3487 retval
= fold_build2 (BIT_IOR_EXPR
, utype
, retval
, fldval
);
3493 return fold_convert (TREE_TYPE (t
), retval
);
3495 return error_mark_node
;
3498 /* Subroutine of cxx_eval_constant_expression.
3499 Evaluate a short-circuited logical expression T in the context
3500 of a given constexpr CALL. BAILOUT_VALUE is the value for
3501 early return. CONTINUE_VALUE is used here purely for
3502 sanity check purposes. */
3505 cxx_eval_logical_expression (const constexpr_ctx
*ctx
, tree t
,
3506 tree bailout_value
, tree continue_value
,
3508 bool *non_constant_p
, bool *overflow_p
)
3511 tree lhs
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
3513 non_constant_p
, overflow_p
);
3514 VERIFY_CONSTANT (lhs
);
3515 if (tree_int_cst_equal (lhs
, bailout_value
))
3517 gcc_assert (tree_int_cst_equal (lhs
, continue_value
));
3518 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
3519 lval
, non_constant_p
,
3521 VERIFY_CONSTANT (r
);
3525 /* REF is a COMPONENT_REF designating a particular field. V is a vector of
3526 CONSTRUCTOR elements to initialize (part of) an object containing that
3527 field. Return a pointer to the constructor_elt corresponding to the
3528 initialization of the field. */
3530 static constructor_elt
*
3531 base_field_constructor_elt (vec
<constructor_elt
, va_gc
> *v
, tree ref
)
3533 tree aggr
= TREE_OPERAND (ref
, 0);
3534 tree field
= TREE_OPERAND (ref
, 1);
3536 constructor_elt
*ce
;
3538 gcc_assert (TREE_CODE (ref
) == COMPONENT_REF
);
3540 if (TREE_CODE (aggr
) == COMPONENT_REF
)
3542 constructor_elt
*base_ce
3543 = base_field_constructor_elt (v
, aggr
);
3544 v
= CONSTRUCTOR_ELTS (base_ce
->value
);
3547 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
3548 if (ce
->index
== field
)
3555 /* Some of the expressions fed to the constexpr mechanism are calls to
3556 constructors, which have type void. In that case, return the type being
3557 initialized by the constructor. */
3560 initialized_type (tree t
)
3564 tree type
= TREE_TYPE (t
);
3565 if (TREE_CODE (t
) == CALL_EXPR
)
3567 /* A constructor call has void type, so we need to look deeper. */
3568 tree fn
= get_function_named_in_call (t
);
3569 if (fn
&& TREE_CODE (fn
) == FUNCTION_DECL
3570 && DECL_CXX_CONSTRUCTOR_P (fn
))
3571 type
= DECL_CONTEXT (fn
);
3573 else if (TREE_CODE (t
) == COMPOUND_EXPR
)
3574 return initialized_type (TREE_OPERAND (t
, 1));
3575 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
)
3576 type
= TREE_TYPE (AGGR_INIT_EXPR_SLOT (t
));
3577 return cv_unqualified (type
);
3580 /* We're about to initialize element INDEX of an array or class from VALUE.
3581 Set up NEW_CTX appropriately by adjusting .object to refer to the
3582 subobject and creating a new CONSTRUCTOR if the element is itself
3583 a class or array. */
3586 init_subob_ctx (const constexpr_ctx
*ctx
, constexpr_ctx
&new_ctx
,
3587 tree index
, tree
&value
)
3591 if (index
&& TREE_CODE (index
) != INTEGER_CST
3592 && TREE_CODE (index
) != FIELD_DECL
)
3593 /* This won't have an element in the new CONSTRUCTOR. */
3596 tree type
= initialized_type (value
);
3597 if (!AGGREGATE_TYPE_P (type
) && !VECTOR_TYPE_P (type
))
3598 /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
3601 /* The sub-aggregate initializer might contain a placeholder;
3602 update object to refer to the subobject and ctor to refer to
3603 the (newly created) sub-initializer. */
3605 new_ctx
.object
= build_ctor_subob_ref (index
, type
, ctx
->object
);
3606 tree elt
= build_constructor (type
, NULL
);
3607 CONSTRUCTOR_NO_CLEARING (elt
) = true;
3610 if (TREE_CODE (value
) == TARGET_EXPR
)
3611 /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
3612 value
= TARGET_EXPR_INITIAL (value
);
3615 /* We're about to process an initializer for a class or array TYPE. Make
3616 sure that CTX is set up appropriately. */
3619 verify_ctor_sanity (const constexpr_ctx
*ctx
, tree type
)
3621 /* We don't bother building a ctor for an empty base subobject. */
3622 if (is_empty_class (type
))
3625 /* We're in the middle of an initializer that might involve placeholders;
3626 our caller should have created a CONSTRUCTOR for us to put the
3627 initializer into. We will either return that constructor or T. */
3628 gcc_assert (ctx
->ctor
);
3629 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3630 (type
, TREE_TYPE (ctx
->ctor
)));
3631 /* We used to check that ctx->ctor was empty, but that isn't the case when
3632 the object is zero-initialized before calling the constructor. */
3635 tree otype
= TREE_TYPE (ctx
->object
);
3636 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type
, otype
)
3637 /* Handle flexible array members. */
3638 || (TREE_CODE (otype
) == ARRAY_TYPE
3639 && TYPE_DOMAIN (otype
) == NULL_TREE
3640 && TREE_CODE (type
) == ARRAY_TYPE
3641 && (same_type_ignoring_top_level_qualifiers_p
3642 (TREE_TYPE (type
), TREE_TYPE (otype
)))));
3644 gcc_assert (!ctx
->object
|| !DECL_P (ctx
->object
)
3645 || *(ctx
->global
->values
.get (ctx
->object
)) == ctx
->ctor
);
3648 /* Subroutine of cxx_eval_constant_expression.
3649 The expression tree T denotes a C-style array or a C-style
3650 aggregate. Reduce it to a constant expression. */
3653 cxx_eval_bare_aggregate (const constexpr_ctx
*ctx
, tree t
,
3655 bool *non_constant_p
, bool *overflow_p
)
3657 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (t
);
3658 bool changed
= false;
3659 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t
));
3660 tree type
= TREE_TYPE (t
);
3662 constexpr_ctx new_ctx
;
3663 if (TYPE_PTRMEMFUNC_P (type
) || VECTOR_TYPE_P (type
))
3665 /* We don't really need the ctx->ctor business for a PMF or
3666 vector, but it's simpler to use the same code. */
3668 new_ctx
.ctor
= build_constructor (type
, NULL
);
3669 new_ctx
.object
= NULL_TREE
;
3672 verify_ctor_sanity (ctx
, type
);
3673 vec
<constructor_elt
, va_gc
> **p
= &CONSTRUCTOR_ELTS (ctx
->ctor
);
3674 vec_alloc (*p
, vec_safe_length (v
));
3678 bool constant_p
= true;
3679 bool side_effects_p
= false;
3680 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, index
, value
)
3682 tree orig_value
= value
;
3683 init_subob_ctx (ctx
, new_ctx
, index
, value
);
3684 if (new_ctx
.ctor
!= ctx
->ctor
)
3685 /* If we built a new CONSTRUCTOR, attach it now so that other
3686 initializers can refer to it. */
3687 CONSTRUCTOR_APPEND_ELT (*p
, index
, new_ctx
.ctor
);
3688 tree elt
= cxx_eval_constant_expression (&new_ctx
, value
,
3690 non_constant_p
, overflow_p
);
3691 /* Don't VERIFY_CONSTANT here. */
3692 if (ctx
->quiet
&& *non_constant_p
)
3694 if (elt
!= orig_value
)
3697 if (!TREE_CONSTANT (elt
))
3699 if (TREE_SIDE_EFFECTS (elt
))
3700 side_effects_p
= true;
3701 if (index
&& TREE_CODE (index
) == COMPONENT_REF
)
3703 /* This is an initialization of a vfield inside a base
3704 subaggregate that we already initialized; push this
3705 initialization into the previous initialization. */
3706 constructor_elt
*inner
= base_field_constructor_elt (*p
, index
);
3711 && (TREE_CODE (index
) == NOP_EXPR
3712 || TREE_CODE (index
) == POINTER_PLUS_EXPR
))
3714 /* This is an initializer for an empty base; now that we've
3715 checked that it's constant, we can ignore it. */
3716 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index
))));
3721 if (new_ctx
.ctor
!= ctx
->ctor
)
3723 /* We appended this element above; update the value. */
3724 gcc_assert ((*p
)->last().index
== index
);
3725 (*p
)->last().value
= elt
;
3728 CONSTRUCTOR_APPEND_ELT (*p
, index
, elt
);
3729 /* Adding or replacing an element might change the ctor's flags. */
3730 TREE_CONSTANT (ctx
->ctor
) = constant_p
;
3731 TREE_SIDE_EFFECTS (ctx
->ctor
) = side_effects_p
;
3734 if (*non_constant_p
|| !changed
)
3737 /* We're done building this CONSTRUCTOR, so now we can interpret an
3738 element without an explicit initializer as value-initialized. */
3739 CONSTRUCTOR_NO_CLEARING (t
) = false;
3740 TREE_CONSTANT (t
) = constant_p
;
3741 TREE_SIDE_EFFECTS (t
) = side_effects_p
;
3742 if (VECTOR_TYPE_P (type
))
3747 /* Subroutine of cxx_eval_constant_expression.
3748 The expression tree T is a VEC_INIT_EXPR which denotes the desired
3749 initialization of a non-static data member of array type. Reduce it to a
3752 Note that apart from value-initialization (when VALUE_INIT is true),
3753 this is only intended to support value-initialization and the
3754 initializations done by defaulted constructors for classes with
3755 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
3756 will either be NULL_TREE for the default constructor, or a COMPONENT_REF
3757 for the copy/move constructor. */
3760 cxx_eval_vec_init_1 (const constexpr_ctx
*ctx
, tree atype
, tree init
,
3761 bool value_init
, bool lval
,
3762 bool *non_constant_p
, bool *overflow_p
)
3764 tree elttype
= TREE_TYPE (atype
);
3765 verify_ctor_sanity (ctx
, atype
);
3766 vec
<constructor_elt
, va_gc
> **p
= &CONSTRUCTOR_ELTS (ctx
->ctor
);
3767 bool pre_init
= false;
3768 unsigned HOST_WIDE_INT i
;
3769 tsubst_flags_t complain
= ctx
->quiet
? tf_none
: tf_warning_or_error
;
3771 /* For the default constructor, build up a call to the default
3772 constructor of the element type. We only need to handle class types
3773 here, as for a constructor to be constexpr, all members must be
3774 initialized, which for a defaulted default constructor means they must
3775 be of a class type with a constexpr default constructor. */
3776 if (TREE_CODE (elttype
) == ARRAY_TYPE
)
3777 /* We only do this at the lowest level. */;
3778 else if (value_init
)
3780 init
= build_value_init (elttype
, complain
);
3785 releasing_vec argvec
;
3786 init
= build_special_member_call (NULL_TREE
, complete_ctor_identifier
,
3787 &argvec
, elttype
, LOOKUP_NORMAL
,
3789 init
= build_aggr_init_expr (elttype
, init
);
3793 tree nelts
= get_array_or_vector_nelts (ctx
, atype
, non_constant_p
,
3795 unsigned HOST_WIDE_INT max
= tree_to_uhwi (nelts
);
3796 for (i
= 0; i
< max
; ++i
)
3798 tree idx
= build_int_cst (size_type_node
, i
);
3801 constexpr_ctx new_ctx
;
3802 init_subob_ctx (ctx
, new_ctx
, idx
, pre_init
? init
: elttype
);
3803 if (new_ctx
.ctor
!= ctx
->ctor
)
3804 CONSTRUCTOR_APPEND_ELT (*p
, idx
, new_ctx
.ctor
);
3805 if (TREE_CODE (elttype
) == ARRAY_TYPE
)
3807 /* A multidimensional array; recurse. */
3808 if (value_init
|| init
== NULL_TREE
)
3810 eltinit
= NULL_TREE
;
3814 eltinit
= cp_build_array_ref (input_location
, init
, idx
, complain
);
3815 eltinit
= cxx_eval_vec_init_1 (&new_ctx
, elttype
, eltinit
, value_init
,
3817 non_constant_p
, overflow_p
);
3821 /* Initializing an element using value or default initialization
3822 we just pre-built above. */
3823 if (init
== void_node
)
3824 /* Trivial default-init, don't do anything to the CONSTRUCTOR. */
3826 eltinit
= cxx_eval_constant_expression (&new_ctx
, init
, lval
,
3827 non_constant_p
, overflow_p
);
3832 /* Copying an element. */
3833 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3834 (atype
, TREE_TYPE (init
)));
3835 eltinit
= cp_build_array_ref (input_location
, init
, idx
, complain
);
3836 if (!lvalue_p (init
))
3837 eltinit
= move (eltinit
);
3838 eltinit
= force_rvalue (eltinit
, complain
);
3839 eltinit
= cxx_eval_constant_expression (&new_ctx
, eltinit
, lval
,
3840 non_constant_p
, overflow_p
);
3842 if (*non_constant_p
&& !ctx
->quiet
)
3844 if (new_ctx
.ctor
!= ctx
->ctor
)
3846 /* We appended this element above; update the value. */
3847 gcc_assert ((*p
)->last().index
== idx
);
3848 (*p
)->last().value
= eltinit
;
3851 CONSTRUCTOR_APPEND_ELT (*p
, idx
, eltinit
);
3852 /* Reuse the result of cxx_eval_constant_expression call
3853 from the first iteration to all others if it is a constant
3854 initializer that doesn't require relocations. */
3857 && (eltinit
== NULL_TREE
3858 || (initializer_constant_valid_p (eltinit
, TREE_TYPE (eltinit
))
3859 == null_pointer_node
)))
3861 if (new_ctx
.ctor
!= ctx
->ctor
)
3862 eltinit
= new_ctx
.ctor
;
3863 tree range
= build2 (RANGE_EXPR
, size_type_node
,
3864 build_int_cst (size_type_node
, 1),
3865 build_int_cst (size_type_node
, max
- 1));
3866 CONSTRUCTOR_APPEND_ELT (*p
, range
, unshare_constructor (eltinit
));
3870 vec_safe_reserve (*p
, max
);
3873 if (!*non_constant_p
)
3876 CONSTRUCTOR_NO_CLEARING (init
) = false;
3882 cxx_eval_vec_init (const constexpr_ctx
*ctx
, tree t
,
3884 bool *non_constant_p
, bool *overflow_p
)
3886 tree atype
= TREE_TYPE (t
);
3887 tree init
= VEC_INIT_EXPR_INIT (t
);
3888 tree r
= cxx_eval_vec_init_1 (ctx
, atype
, init
,
3889 VEC_INIT_EXPR_VALUE_INIT (t
),
3890 lval
, non_constant_p
, overflow_p
);
3891 if (*non_constant_p
)
3897 /* Like same_type_ignoring_top_level_qualifiers_p, but also handle the case
3898 where the desired type is an array of unknown bounds because the variable
3899 has had its bounds deduced since the wrapping expression was created. */
3902 same_type_ignoring_tlq_and_bounds_p (tree type1
, tree type2
)
3904 while (TREE_CODE (type1
) == ARRAY_TYPE
3905 && TREE_CODE (type2
) == ARRAY_TYPE
3906 && (!TYPE_DOMAIN (type1
) || !TYPE_DOMAIN (type2
)))
3908 type1
= TREE_TYPE (type1
);
3909 type2
= TREE_TYPE (type2
);
3911 return same_type_ignoring_top_level_qualifiers_p (type1
, type2
);
3914 /* Helper function for cxx_fold_indirect_ref_1, called recursively. */
3917 cxx_fold_indirect_ref_1 (location_t loc
, tree type
, tree op
,
3918 unsigned HOST_WIDE_INT off
, bool *empty_base
)
3920 tree optype
= TREE_TYPE (op
);
3921 unsigned HOST_WIDE_INT const_nunits
;
3924 if (similar_type_p (optype
, type
))
3926 /* Also handle conversion to an empty base class, which
3927 is represented with a NOP_EXPR. */
3928 /* *(foo *)&complexfoo => __real__ complexfoo */
3929 else if (TREE_CODE (optype
) == COMPLEX_TYPE
3930 && similar_type_p (type
, TREE_TYPE (optype
)))
3931 return build1_loc (loc
, REALPART_EXPR
, type
, op
);
3933 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
3934 else if (TREE_CODE (optype
) == COMPLEX_TYPE
3935 && similar_type_p (type
, TREE_TYPE (optype
))
3936 && tree_to_uhwi (TYPE_SIZE_UNIT (type
)) == off
)
3937 return build1_loc (loc
, IMAGPART_EXPR
, type
, op
);
3938 if (is_empty_class (type
)
3939 && CLASS_TYPE_P (optype
)
3940 && DERIVED_FROM_P (type
, optype
))
3945 /* ((foo*)&vectorfoo)[x] => BIT_FIELD_REF<vectorfoo,...> */
3946 else if (VECTOR_TYPE_P (optype
)
3947 && similar_type_p (type
, TREE_TYPE (optype
))
3948 && TYPE_VECTOR_SUBPARTS (optype
).is_constant (&const_nunits
))
3950 unsigned HOST_WIDE_INT part_width
= tree_to_uhwi (TYPE_SIZE_UNIT (type
));
3951 unsigned HOST_WIDE_INT max_offset
= part_width
* const_nunits
;
3952 if (off
< max_offset
&& off
% part_width
== 0)
3954 tree index
= bitsize_int (off
* BITS_PER_UNIT
);
3955 return build3_loc (loc
, BIT_FIELD_REF
, type
, op
,
3956 TYPE_SIZE (type
), index
);
3959 /* ((foo *)&fooarray)[x] => fooarray[x] */
3960 else if (TREE_CODE (optype
) == ARRAY_TYPE
3961 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (optype
)))
3962 && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (optype
))))
3964 tree type_domain
= TYPE_DOMAIN (optype
);
3965 tree min_val
= size_zero_node
;
3966 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
3967 min_val
= TYPE_MIN_VALUE (type_domain
);
3968 unsigned HOST_WIDE_INT el_sz
3969 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (optype
)));
3970 unsigned HOST_WIDE_INT idx
= off
/ el_sz
;
3971 unsigned HOST_WIDE_INT rem
= off
% el_sz
;
3972 if (tree_fits_uhwi_p (min_val
))
3974 tree index
= size_int (idx
+ tree_to_uhwi (min_val
));
3975 op
= build4_loc (loc
, ARRAY_REF
, TREE_TYPE (optype
), op
, index
,
3976 NULL_TREE
, NULL_TREE
);
3977 return cxx_fold_indirect_ref_1 (loc
, type
, op
, rem
,
3981 /* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */
3982 else if (TREE_CODE (optype
) == RECORD_TYPE
)
3984 for (tree field
= TYPE_FIELDS (optype
);
3985 field
; field
= DECL_CHAIN (field
))
3986 if (TREE_CODE (field
) == FIELD_DECL
3987 && TREE_TYPE (field
) != error_mark_node
3988 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (field
))))
3990 tree pos
= byte_position (field
);
3991 if (!tree_fits_uhwi_p (pos
))
3993 unsigned HOST_WIDE_INT upos
= tree_to_uhwi (pos
);
3995 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field
)));
3996 if (upos
<= off
&& off
< upos
+ el_sz
)
3998 tree cop
= build3 (COMPONENT_REF
, TREE_TYPE (field
),
3999 op
, field
, NULL_TREE
);
4000 if (tree ret
= cxx_fold_indirect_ref_1 (loc
, type
, cop
,
4011 /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
4012 match. We want to be less strict for simple *& folding; if we have a
4013 non-const temporary that we access through a const pointer, that should
4014 work. We handle this here rather than change fold_indirect_ref_1
4015 because we're dealing with things like ADDR_EXPR of INTEGER_CST which
4016 don't really make sense outside of constant expression evaluation. Also
4017 we want to allow folding to COMPONENT_REF, which could cause trouble
4018 with TBAA in fold_indirect_ref_1. */
4021 cxx_fold_indirect_ref (location_t loc
, tree type
, tree op0
, bool *empty_base
)
4025 poly_uint64 const_op01
;
4027 /* STRIP_NOPS, but stop if REINTERPRET_CAST_P. */
4028 while (CONVERT_EXPR_P (sub
) || TREE_CODE (sub
) == NON_LVALUE_EXPR
4029 || TREE_CODE (sub
) == VIEW_CONVERT_EXPR
)
4031 if (TREE_CODE (sub
) == NOP_EXPR
4032 && REINTERPRET_CAST_P (sub
))
4034 sub
= TREE_OPERAND (sub
, 0);
4037 subtype
= TREE_TYPE (sub
);
4038 if (!INDIRECT_TYPE_P (subtype
))
4041 if (TREE_CODE (sub
) == ADDR_EXPR
)
4043 tree op
= TREE_OPERAND (sub
, 0);
4044 tree optype
= TREE_TYPE (op
);
4046 /* *&CONST_DECL -> to the value of the const decl. */
4047 if (TREE_CODE (op
) == CONST_DECL
)
4048 return DECL_INITIAL (op
);
4049 /* *&p => p; make sure to handle *&"str"[cst] here. */
4050 if (similar_type_p (optype
, type
))
4052 tree fop
= fold_read_from_constant_string (op
);
4059 return cxx_fold_indirect_ref_1 (loc
, type
, op
, 0, empty_base
);
4061 else if (TREE_CODE (sub
) == POINTER_PLUS_EXPR
4062 && tree_fits_uhwi_p (TREE_OPERAND (sub
, 1)))
4064 tree op00
= TREE_OPERAND (sub
, 0);
4065 tree op01
= TREE_OPERAND (sub
, 1);
4068 if (TREE_CODE (op00
) == ADDR_EXPR
)
4069 return cxx_fold_indirect_ref_1 (loc
, type
, TREE_OPERAND (op00
, 0),
4070 tree_to_uhwi (op01
), empty_base
);
4072 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
4073 else if (TREE_CODE (TREE_TYPE (subtype
)) == ARRAY_TYPE
4074 && similar_type_p (type
, TREE_TYPE (TREE_TYPE (subtype
))))
4077 tree min_val
= size_zero_node
;
4079 = cxx_fold_indirect_ref (loc
, TREE_TYPE (subtype
), sub
, NULL
);
4083 sub
= build1_loc (loc
, INDIRECT_REF
, TREE_TYPE (subtype
), sub
);
4084 type_domain
= TYPE_DOMAIN (TREE_TYPE (sub
));
4085 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
4086 min_val
= TYPE_MIN_VALUE (type_domain
);
4087 return build4_loc (loc
, ARRAY_REF
, type
, sub
, min_val
, NULL_TREE
,
4095 cxx_eval_indirect_ref (const constexpr_ctx
*ctx
, tree t
,
4097 bool *non_constant_p
, bool *overflow_p
)
4099 tree orig_op0
= TREE_OPERAND (t
, 0);
4100 bool empty_base
= false;
4102 /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second
4103 operand is an integer-zero. Otherwise reject the MEM_REF for now. */
4105 if (TREE_CODE (t
) == MEM_REF
4106 && (!TREE_OPERAND (t
, 1) || !integer_zerop (TREE_OPERAND (t
, 1))))
4108 gcc_assert (ctx
->quiet
);
4109 *non_constant_p
= true;
4113 /* First try to simplify it directly. */
4114 tree r
= cxx_fold_indirect_ref (EXPR_LOCATION (t
), TREE_TYPE (t
), orig_op0
,
4118 /* If that didn't work, evaluate the operand first. */
4119 tree op0
= cxx_eval_constant_expression (ctx
, orig_op0
,
4120 /*lval*/false, non_constant_p
,
4122 /* Don't VERIFY_CONSTANT here. */
4123 if (*non_constant_p
)
4126 if (!lval
&& integer_zerop (op0
))
4129 error ("dereferencing a null pointer");
4130 *non_constant_p
= true;
4134 r
= cxx_fold_indirect_ref (EXPR_LOCATION (t
), TREE_TYPE (t
), op0
,
4138 /* We couldn't fold to a constant value. Make sure it's not
4139 something we should have been able to fold. */
4142 if (TREE_CODE (sub
) == ADDR_EXPR
)
4144 gcc_assert (!similar_type_p
4145 (TREE_TYPE (TREE_TYPE (sub
)), TREE_TYPE (t
)));
4146 /* DR 1188 says we don't have to deal with this. */
4148 error_at (cp_expr_loc_or_input_loc (t
),
4149 "accessing value of %qE through a %qT glvalue in a "
4150 "constant expression", build_fold_indirect_ref (sub
),
4152 *non_constant_p
= true;
4156 if (lval
&& op0
!= orig_op0
)
4157 return build1 (INDIRECT_REF
, TREE_TYPE (t
), op0
);
4159 VERIFY_CONSTANT (t
);
4164 r
= cxx_eval_constant_expression (ctx
, r
,
4165 lval
, non_constant_p
, overflow_p
);
4166 if (*non_constant_p
)
4169 /* If we're pulling out the value of an empty base, just return an empty
4171 if (empty_base
&& !lval
)
4173 r
= build_constructor (TREE_TYPE (t
), NULL
);
4174 TREE_CONSTANT (r
) = true;
4180 /* Complain about R, a VAR_DECL, not being usable in a constant expression.
4181 Shared between potential_constant_expression and
4182 cxx_eval_constant_expression. */
4185 non_const_var_error (location_t loc
, tree r
)
4187 auto_diagnostic_group d
;
4188 tree type
= TREE_TYPE (r
);
4189 if (DECL_NAME (r
) == heap_uninit_identifier
4190 || DECL_NAME (r
) == heap_identifier
)
4192 error_at (loc
, "the content of uninitialized storage is not usable "
4193 "in a constant expression");
4194 inform (DECL_SOURCE_LOCATION (r
), "allocated here");
4197 if (DECL_NAME (r
) == heap_deleted_identifier
)
4199 error_at (loc
, "use of allocated storage after deallocation in a "
4200 "constant expression");
4201 inform (DECL_SOURCE_LOCATION (r
), "allocated here");
4204 error_at (loc
, "the value of %qD is not usable in a constant "
4206 /* Avoid error cascade. */
4207 if (DECL_INITIAL (r
) == error_mark_node
)
4209 if (DECL_DECLARED_CONSTEXPR_P (r
))
4210 inform (DECL_SOURCE_LOCATION (r
),
4211 "%qD used in its own initializer", r
);
4212 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
4214 if (!CP_TYPE_CONST_P (type
))
4215 inform (DECL_SOURCE_LOCATION (r
),
4216 "%q#D is not const", r
);
4217 else if (CP_TYPE_VOLATILE_P (type
))
4218 inform (DECL_SOURCE_LOCATION (r
),
4219 "%q#D is volatile", r
);
4220 else if (!DECL_INITIAL (r
)
4221 || !TREE_CONSTANT (DECL_INITIAL (r
))
4222 || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
))
4223 inform (DECL_SOURCE_LOCATION (r
),
4224 "%qD was not initialized with a constant "
4229 else if (TYPE_REF_P (type
))
4230 inform (DECL_SOURCE_LOCATION (r
),
4231 "%qD was not initialized with a constant "
4235 if (cxx_dialect
>= cxx11
&& !DECL_DECLARED_CONSTEXPR_P (r
))
4236 inform (DECL_SOURCE_LOCATION (r
),
4237 "%qD was not declared %<constexpr%>", r
);
4239 inform (DECL_SOURCE_LOCATION (r
),
4240 "%qD does not have integral or enumeration type",
4245 /* Subroutine of cxx_eval_constant_expression.
4246 Like cxx_eval_unary_expression, except for trinary expressions. */
4249 cxx_eval_trinary_expression (const constexpr_ctx
*ctx
, tree t
,
4251 bool *non_constant_p
, bool *overflow_p
)
4257 for (i
= 0; i
< 3; i
++)
4259 args
[i
] = cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, i
),
4261 non_constant_p
, overflow_p
);
4262 VERIFY_CONSTANT (args
[i
]);
4265 val
= fold_ternary_loc (EXPR_LOCATION (t
), TREE_CODE (t
), TREE_TYPE (t
),
4266 args
[0], args
[1], args
[2]);
4267 if (val
== NULL_TREE
)
4269 VERIFY_CONSTANT (val
);
4273 /* True if T was declared in a function declared to be constexpr, and
4274 therefore potentially constant in C++14. */
4277 var_in_constexpr_fn (tree t
)
4279 tree ctx
= DECL_CONTEXT (t
);
4280 return (ctx
&& TREE_CODE (ctx
) == FUNCTION_DECL
4281 && DECL_DECLARED_CONSTEXPR_P (ctx
));
4284 /* True if T was declared in a function that might be constexpr: either a
4285 function that was declared constexpr, or a C++17 lambda op(). */
4288 var_in_maybe_constexpr_fn (tree t
)
4290 if (cxx_dialect
>= cxx17
4291 && DECL_FUNCTION_SCOPE_P (t
)
4292 && LAMBDA_FUNCTION_P (DECL_CONTEXT (t
)))
4294 return var_in_constexpr_fn (t
);
4297 /* We're assigning INIT to TARGET. In do_build_copy_constructor and
4298 build_over_call we implement trivial copy of a class with tail padding using
4299 assignment of character arrays, which is valid in normal code, but not in
4300 constexpr evaluation. We don't need to worry about clobbering tail padding
4301 in constexpr evaluation, so strip the type punning. */
4304 maybe_simplify_trivial_copy (tree
&target
, tree
&init
)
4306 if (TREE_CODE (target
) == MEM_REF
4307 && TREE_CODE (init
) == MEM_REF
4308 && TREE_TYPE (target
) == TREE_TYPE (init
)
4309 && TREE_CODE (TREE_TYPE (target
)) == ARRAY_TYPE
4310 && TREE_TYPE (TREE_TYPE (target
)) == unsigned_char_type_node
)
4312 target
= build_fold_indirect_ref (TREE_OPERAND (target
, 0));
4313 init
= build_fold_indirect_ref (TREE_OPERAND (init
, 0));
4317 /* Return true if we are modifying something that is const during constant
4318 expression evaluation. CODE is the code of the statement, OBJ is the
4319 object in question, MUTABLE_P is true if one of the subobjects were
4320 declared mutable. */
4323 modifying_const_object_p (tree_code code
, tree obj
, bool mutable_p
)
4325 /* If this is initialization, there's no problem. */
4326 if (code
!= MODIFY_EXPR
)
4329 /* [basic.type.qualifier] "A const object is an object of type
4330 const T or a non-mutable subobject of a const object." */
4334 return (TREE_READONLY (obj
) || CP_TYPE_CONST_P (TREE_TYPE (obj
)));
4337 /* Evaluate an INIT_EXPR or MODIFY_EXPR. */
4340 cxx_eval_store_expression (const constexpr_ctx
*ctx
, tree t
,
4342 bool *non_constant_p
, bool *overflow_p
)
4344 constexpr_ctx new_ctx
= *ctx
;
4346 tree init
= TREE_OPERAND (t
, 1);
4347 if (TREE_CLOBBER_P (init
))
4348 /* Just ignore clobbers. */
4351 /* First we figure out where we're storing to. */
4352 tree target
= TREE_OPERAND (t
, 0);
4354 maybe_simplify_trivial_copy (target
, init
);
4356 tree type
= TREE_TYPE (target
);
4357 bool preeval
= SCALAR_TYPE_P (type
) || TREE_CODE (t
) == MODIFY_EXPR
;
4360 /* Evaluate the value to be stored without knowing what object it will be
4361 stored in, so that any side-effects happen first. */
4362 if (!SCALAR_TYPE_P (type
))
4363 new_ctx
.ctor
= new_ctx
.object
= NULL_TREE
;
4364 init
= cxx_eval_constant_expression (&new_ctx
, init
, false,
4365 non_constant_p
, overflow_p
);
4366 if (*non_constant_p
)
4370 bool evaluated
= false;
4373 /* If we want to return a reference to the target, we need to evaluate it
4374 as a whole; otherwise, only evaluate the innermost piece to avoid
4375 building up unnecessary *_REFs. */
4376 target
= cxx_eval_constant_expression (ctx
, target
, true,
4377 non_constant_p
, overflow_p
);
4379 if (*non_constant_p
)
4383 /* Find the underlying variable. */
4385 tree object
= NULL_TREE
;
4386 /* If we're modifying a const object, save it. */
4387 tree const_object_being_modified
= NULL_TREE
;
4388 bool mutable_p
= false;
4389 for (tree probe
= target
; object
== NULL_TREE
; )
4391 switch (TREE_CODE (probe
))
4397 tree ob
= TREE_OPERAND (probe
, 0);
4398 tree elt
= TREE_OPERAND (probe
, 1);
4399 if (TREE_CODE (elt
) == FIELD_DECL
&& DECL_MUTABLE_P (elt
))
4401 if (TREE_CODE (probe
) == ARRAY_REF
)
4403 elt
= eval_and_check_array_index (ctx
, probe
, false,
4404 non_constant_p
, overflow_p
);
4405 if (*non_constant_p
)
4408 /* We don't check modifying_const_object_p for ARRAY_REFs. Given
4409 "int a[10]", an ARRAY_REF "a[2]" can be "const int", even though
4410 the array isn't const. Instead, check "a" in the next iteration;
4411 that will detect modifying "const int a[10]". */
4413 && modifying_const_object_p (TREE_CODE (t
), probe
,
4415 && const_object_being_modified
== NULL_TREE
)
4416 const_object_being_modified
= probe
;
4417 vec_safe_push (refs
, elt
);
4418 vec_safe_push (refs
, TREE_TYPE (probe
));
4428 probe
= cxx_eval_constant_expression (ctx
, probe
, true,
4429 non_constant_p
, overflow_p
);
4431 if (*non_constant_p
)
4438 if (modifying_const_object_p (TREE_CODE (t
), object
, mutable_p
)
4439 && const_object_being_modified
== NULL_TREE
)
4440 const_object_being_modified
= object
;
4442 /* And then find/build up our initializer for the path to the subobject
4443 we're initializing. */
4445 if (object
== ctx
->object
&& VAR_P (object
)
4446 && DECL_NAME (object
) && ctx
->call
== NULL
)
4447 /* The variable we're building up an aggregate initializer for is outside
4448 the constant-expression, so don't evaluate the store. We check
4449 DECL_NAME to handle TARGET_EXPR temporaries, which are fair game. */
4451 else if (DECL_P (object
))
4452 valp
= ctx
->global
->values
.get (object
);
4457 /* A constant-expression cannot modify objects from outside the
4458 constant-expression. */
4460 error ("modification of %qE is not a constant expression", object
);
4461 *non_constant_p
= true;
4464 type
= TREE_TYPE (object
);
4465 bool no_zero_init
= true;
4467 releasing_vec ctors
;
4468 while (!refs
->is_empty ())
4470 if (*valp
== NULL_TREE
)
4472 *valp
= build_constructor (type
, NULL
);
4473 CONSTRUCTOR_NO_CLEARING (*valp
) = no_zero_init
;
4475 else if (TREE_CODE (*valp
) == STRING_CST
)
4477 /* An array was initialized with a string constant, and now
4478 we're writing into one of its elements. Explode the
4479 single initialization into a set of element
4481 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
4483 tree string
= *valp
;
4484 tree elt_type
= TREE_TYPE (type
);
4485 unsigned chars_per_elt
= (TYPE_PRECISION (elt_type
)
4486 / TYPE_PRECISION (char_type_node
));
4487 unsigned num_elts
= TREE_STRING_LENGTH (string
) / chars_per_elt
;
4488 tree ary_ctor
= build_constructor (type
, NULL
);
4490 vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor
), num_elts
);
4491 for (unsigned ix
= 0; ix
!= num_elts
; ix
++)
4493 constructor_elt elt
=
4495 build_int_cst (size_type_node
, ix
),
4496 extract_string_elt (string
, chars_per_elt
, ix
)
4498 CONSTRUCTOR_ELTS (ary_ctor
)->quick_push (elt
);
4504 /* If the value of object is already zero-initialized, any new ctors for
4505 subobjects will also be zero-initialized. */
4506 no_zero_init
= CONSTRUCTOR_NO_CLEARING (*valp
);
4508 vec_safe_push (ctors
, *valp
);
4510 enum tree_code code
= TREE_CODE (type
);
4512 tree index
= refs
->pop();
4514 constructor_elt
*cep
= NULL
;
4515 if (code
== ARRAY_TYPE
)
4518 = find_array_ctor_elt (*valp
, index
, /*insert*/true);
4519 gcc_assert (i
>= 0);
4520 cep
= CONSTRUCTOR_ELT (*valp
, i
);
4521 gcc_assert (TREE_CODE (cep
->index
) != RANGE_EXPR
);
4525 gcc_assert (TREE_CODE (index
) == FIELD_DECL
);
4527 /* We must keep the CONSTRUCTOR's ELTS in FIELD order.
4528 Usually we meet initializers in that order, but it is
4529 possible for base types to be placed not in program
4531 tree fields
= TYPE_FIELDS (DECL_CONTEXT (index
));
4532 unsigned HOST_WIDE_INT idx
;
4534 if (code
== UNION_TYPE
&& CONSTRUCTOR_NELTS (*valp
)
4535 && CONSTRUCTOR_ELT (*valp
, 0)->index
!= index
)
4537 if (cxx_dialect
< cxx2a
)
4540 error_at (cp_expr_loc_or_input_loc (t
),
4541 "change of the active member of a union "
4543 CONSTRUCTOR_ELT (*valp
, 0)->index
,
4545 *non_constant_p
= true;
4547 /* Changing active member. */
4548 vec_safe_truncate (CONSTRUCTOR_ELTS (*valp
), 0);
4549 no_zero_init
= true;
4553 vec_safe_iterate (CONSTRUCTOR_ELTS (*valp
), idx
, &cep
);
4554 idx
++, fields
= DECL_CHAIN (fields
))
4556 if (index
== cep
->index
)
4559 /* The field we're initializing must be on the field
4560 list. Look to see if it is present before the
4561 field the current ELT initializes. */
4562 for (; fields
!= cep
->index
; fields
= DECL_CHAIN (fields
))
4563 if (index
== fields
)
4567 /* We fell off the end of the CONSTRUCTOR, so insert a new
4568 entry at the end. */
4571 constructor_elt ce
= { index
, NULL_TREE
};
4573 vec_safe_insert (CONSTRUCTOR_ELTS (*valp
), idx
, ce
);
4574 cep
= CONSTRUCTOR_ELT (*valp
, idx
);
4581 /* Detect modifying a constant object in constexpr evaluation.
4582 We have found a const object that is being modified. Figure out
4583 if we need to issue an error. Consider
4587 constexpr A() : n(1) { n = 2; } // #1
4591 constexpr B() { a.n = 3; } // #2
4595 #1 is OK, since we're modifying an object under construction, but
4596 #2 is wrong, since "a" is const and has been fully constructed.
4597 To track it, we use the TREE_READONLY bit in the object's CONSTRUCTOR
4598 which means that the object is read-only. For the example above, the
4599 *ctors stack at the point of #2 will look like:
4601 ctors[0] = {.a={.n=2}} TREE_READONLY = 0
4602 ctors[1] = {.n=2} TREE_READONLY = 1
4604 and we're modifying "b.a", so we search the stack and see if the
4605 constructor for "b.a" has already run. */
4606 if (const_object_being_modified
)
4610 = strip_array_types (TREE_TYPE (const_object_being_modified
));
4611 if (!CLASS_TYPE_P (const_objtype
))
4615 /* [class.ctor]p5 "A constructor can be invoked for a const,
4616 volatile, or const volatile object. const and volatile
4617 semantics are not applied on an object under construction.
4618 They come into effect when the constructor for the most
4619 derived object ends." */
4622 FOR_EACH_VEC_ELT (*ctors
, i
, elt
)
4623 if (same_type_ignoring_top_level_qualifiers_p
4624 (TREE_TYPE (const_object_being_modified
), TREE_TYPE (elt
)))
4626 fail
= TREE_READONLY (elt
);
4633 modifying_const_object_error (t
, const_object_being_modified
);
4634 *non_constant_p
= true;
4641 /* Create a new CONSTRUCTOR in case evaluation of the initializer
4642 wants to modify it. */
4643 if (*valp
== NULL_TREE
)
4645 *valp
= build_constructor (type
, NULL
);
4646 CONSTRUCTOR_NO_CLEARING (*valp
) = no_zero_init
;
4648 new_ctx
.ctor
= *valp
;
4649 new_ctx
.object
= target
;
4650 /* Avoid temporary materialization when initializing from a TARGET_EXPR.
4651 We don't need to mess with AGGR_EXPR_SLOT/VEC_INIT_EXPR_SLOT because
4652 expansion of those trees uses ctx instead. */
4653 if (TREE_CODE (init
) == TARGET_EXPR
)
4654 if (tree tinit
= TARGET_EXPR_INITIAL (init
))
4656 init
= cxx_eval_constant_expression (&new_ctx
, init
, false,
4657 non_constant_p
, overflow_p
);
4658 if (ctors
->is_empty())
4659 /* The hash table might have moved since the get earlier. */
4660 valp
= ctx
->global
->values
.get (object
);
4663 /* Don't share a CONSTRUCTOR that might be changed later. */
4664 init
= unshare_constructor (init
);
4666 if (*valp
&& TREE_CODE (*valp
) == CONSTRUCTOR
4667 && TREE_CODE (init
) == CONSTRUCTOR
)
4669 /* An outer ctx->ctor might be pointing to *valp, so replace
4671 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init
),
4674 /* For initialization of an empty base, the original target will be
4675 *(base*)this, evaluation of which resolves to the object
4676 argument, which has the derived type rather than the base type. In
4677 this situation, just evaluate the initializer and return, since
4678 there's no actual data to store. */
4679 gcc_assert (is_empty_class (TREE_TYPE (init
)) && !lval
);
4682 CONSTRUCTOR_ELTS (*valp
) = CONSTRUCTOR_ELTS (init
);
4683 TREE_CONSTANT (*valp
) = TREE_CONSTANT (init
);
4684 TREE_SIDE_EFFECTS (*valp
) = TREE_SIDE_EFFECTS (init
);
4685 CONSTRUCTOR_NO_CLEARING (*valp
)
4686 = CONSTRUCTOR_NO_CLEARING (init
);
4691 /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing
4692 CONSTRUCTORs, if any. */
4695 bool c
= TREE_CONSTANT (init
);
4696 bool s
= TREE_SIDE_EFFECTS (init
);
4698 FOR_EACH_VEC_ELT (*ctors
, i
, elt
)
4701 TREE_CONSTANT (elt
) = false;
4703 TREE_SIDE_EFFECTS (elt
) = true;
4706 if (*non_constant_p
)
4714 /* Evaluate a ++ or -- expression. */
4717 cxx_eval_increment_expression (const constexpr_ctx
*ctx
, tree t
,
4719 bool *non_constant_p
, bool *overflow_p
)
4721 enum tree_code code
= TREE_CODE (t
);
4722 tree type
= TREE_TYPE (t
);
4723 tree op
= TREE_OPERAND (t
, 0);
4724 tree offset
= TREE_OPERAND (t
, 1);
4725 gcc_assert (TREE_CONSTANT (offset
));
4727 /* OFFSET is constant, but perhaps not constant enough. We need to
4728 e.g. bash FLOAT_EXPRs to REAL_CSTs. */
4729 offset
= fold_simple (offset
);
4731 /* The operand as an lvalue. */
4732 op
= cxx_eval_constant_expression (ctx
, op
, true,
4733 non_constant_p
, overflow_p
);
4735 /* The operand as an rvalue. */
4737 = cxx_eval_constant_expression (ctx
, op
, false,
4738 non_constant_p
, overflow_p
);
4739 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
4740 a local array in a constexpr function. */
4741 bool ptr
= INDIRECT_TYPE_P (TREE_TYPE (val
));
4743 VERIFY_CONSTANT (val
);
4745 /* The modified value. */
4746 bool inc
= (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
);
4748 if (INDIRECT_TYPE_P (type
))
4750 /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
4751 offset
= convert_to_ptrofftype (offset
);
4753 offset
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (offset
), offset
);
4754 mod
= fold_build2 (POINTER_PLUS_EXPR
, type
, val
, offset
);
4757 mod
= fold_build2 (inc
? PLUS_EXPR
: MINUS_EXPR
, type
, val
, offset
);
4759 VERIFY_CONSTANT (mod
);
4761 /* Storing the modified value. */
4762 tree store
= build2_loc (cp_expr_loc_or_loc (t
, input_location
),
4763 MODIFY_EXPR
, type
, op
, mod
);
4764 cxx_eval_constant_expression (ctx
, store
,
4765 true, non_constant_p
, overflow_p
);
4768 /* And the value of the expression. */
4769 if (code
== PREINCREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
4771 /* Prefix ops are lvalues. */
4775 /* But we optimize when the caller wants an rvalue. */
4779 /* Postfix ops are rvalues. */
4783 /* Predicates for the meaning of *jump_target. */
4786 returns (tree
*jump_target
)
4789 && (TREE_CODE (*jump_target
) == RETURN_EXPR
4790 || (TREE_CODE (*jump_target
) == LABEL_DECL
4791 && LABEL_DECL_CDTOR (*jump_target
)));
4795 breaks (tree
*jump_target
)
4798 && ((TREE_CODE (*jump_target
) == LABEL_DECL
4799 && LABEL_DECL_BREAK (*jump_target
))
4800 || TREE_CODE (*jump_target
) == BREAK_STMT
4801 || TREE_CODE (*jump_target
) == EXIT_EXPR
);
4805 continues (tree
*jump_target
)
4808 && ((TREE_CODE (*jump_target
) == LABEL_DECL
4809 && LABEL_DECL_CONTINUE (*jump_target
))
4810 || TREE_CODE (*jump_target
) == CONTINUE_STMT
);
4815 switches (tree
*jump_target
)
4818 && TREE_CODE (*jump_target
) == INTEGER_CST
;
4821 /* Subroutine of cxx_eval_statement_list. Determine whether the statement
4822 STMT matches *jump_target. If we're looking for a case label and we see
4823 the default label, note it in ctx->css_state. */
4826 label_matches (const constexpr_ctx
*ctx
, tree
*jump_target
, tree stmt
)
4828 switch (TREE_CODE (*jump_target
))
4831 if (TREE_CODE (stmt
) == LABEL_EXPR
4832 && LABEL_EXPR_LABEL (stmt
) == *jump_target
)
4837 if (TREE_CODE (stmt
) == CASE_LABEL_EXPR
)
4839 gcc_assert (ctx
->css_state
!= NULL
);
4840 if (!CASE_LOW (stmt
))
4842 /* default: should appear just once in a SWITCH_EXPR
4843 body (excluding nested SWITCH_EXPR). */
4844 gcc_assert (*ctx
->css_state
!= css_default_seen
);
4845 /* When evaluating SWITCH_EXPR body for the second time,
4846 return true for the default: label. */
4847 if (*ctx
->css_state
== css_default_processing
)
4849 *ctx
->css_state
= css_default_seen
;
4851 else if (CASE_HIGH (stmt
))
4853 if (tree_int_cst_le (CASE_LOW (stmt
), *jump_target
)
4854 && tree_int_cst_le (*jump_target
, CASE_HIGH (stmt
)))
4857 else if (tree_int_cst_equal (*jump_target
, CASE_LOW (stmt
)))
4864 /* These two are handled directly in cxx_eval_loop_expr by testing
4865 breaks (jump_target) or continues (jump_target). */
4874 /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
4875 semantics, for switch, break, continue, and return. */
4878 cxx_eval_statement_list (const constexpr_ctx
*ctx
, tree t
,
4879 bool *non_constant_p
, bool *overflow_p
,
4882 tree_stmt_iterator i
;
4884 /* In a statement-expression we want to return the last value.
4885 For empty statement expression return void_node. */
4889 local_target
= NULL_TREE
;
4890 jump_target
= &local_target
;
4892 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
4894 tree stmt
= tsi_stmt (i
);
4895 /* We've found a continue, so skip everything until we reach
4896 the label its jumping to. */
4897 if (continues (jump_target
))
4899 if (label_matches (ctx
, jump_target
, stmt
))
4901 *jump_target
= NULL_TREE
;
4905 if (TREE_CODE (stmt
) == DEBUG_BEGIN_STMT
)
4907 r
= cxx_eval_constant_expression (ctx
, stmt
, false,
4908 non_constant_p
, overflow_p
,
4910 if (*non_constant_p
)
4912 if (returns (jump_target
) || breaks (jump_target
))
4915 if (*jump_target
&& jump_target
== &local_target
)
4917 /* We aren't communicating the jump to our caller, so give up. We don't
4918 need to support evaluation of jumps out of statement-exprs. */
4920 error_at (cp_expr_loc_or_input_loc (r
),
4921 "statement is not a constant expression");
4922 *non_constant_p
= true;
4927 /* Evaluate a LOOP_EXPR for side-effects. Handles break and return
4928 semantics; continue semantics are covered by cxx_eval_statement_list. */
4931 cxx_eval_loop_expr (const constexpr_ctx
*ctx
, tree t
,
4932 bool *non_constant_p
, bool *overflow_p
,
4935 constexpr_ctx new_ctx
= *ctx
;
4939 local_target
= NULL_TREE
;
4940 jump_target
= &local_target
;
4943 tree body
, cond
= NULL_TREE
, expr
= NULL_TREE
;
4945 switch (TREE_CODE (t
))
4948 body
= LOOP_EXPR_BODY (t
);
4955 body
= WHILE_BODY (t
);
4956 cond
= WHILE_COND (t
);
4960 if (FOR_INIT_STMT (t
))
4961 cxx_eval_constant_expression (ctx
, FOR_INIT_STMT (t
), /*lval*/false,
4962 non_constant_p
, overflow_p
, jump_target
);
4963 if (*non_constant_p
)
4965 body
= FOR_BODY (t
);
4966 cond
= FOR_COND (t
);
4967 expr
= FOR_EXPR (t
);
4973 auto_vec
<tree
, 10> save_exprs
;
4974 new_ctx
.save_exprs
= &save_exprs
;
4980 cxx_eval_constant_expression (&new_ctx
, body
, /*lval*/false,
4981 non_constant_p
, overflow_p
,
4983 if (breaks (jump_target
))
4985 *jump_target
= NULL_TREE
;
4989 if (TREE_CODE (t
) != LOOP_EXPR
&& continues (jump_target
))
4990 *jump_target
= NULL_TREE
;
4993 cxx_eval_constant_expression (&new_ctx
, expr
, /*lval*/false,
4994 non_constant_p
, overflow_p
,
5001 = cxx_eval_constant_expression (&new_ctx
, cond
, /*lval*/false,
5002 non_constant_p
, overflow_p
,
5006 if (verify_constant (res
, ctx
->quiet
, non_constant_p
,
5009 if (integer_zerop (res
))
5013 gcc_assert (*jump_target
);
5016 /* Forget saved values of SAVE_EXPRs and TARGET_EXPRs. */
5019 FOR_EACH_VEC_ELT (save_exprs
, i
, save_expr
)
5020 ctx
->global
->values
.remove (save_expr
);
5021 save_exprs
.truncate (0);
5023 if (++count
>= constexpr_loop_limit
)
5026 error_at (cp_expr_loc_or_input_loc (t
),
5027 "%<constexpr%> loop iteration count exceeds limit of %d "
5028 "(use %<-fconstexpr-loop-limit=%> to increase the limit)",
5029 constexpr_loop_limit
);
5030 *non_constant_p
= true;
5034 while (!returns (jump_target
)
5035 && !breaks (jump_target
)
5036 && !continues (jump_target
)
5037 && (!switches (jump_target
) || count
== 0)
5038 && !*non_constant_p
);
5040 /* Forget saved values of SAVE_EXPRs and TARGET_EXPRs. */
5043 FOR_EACH_VEC_ELT (save_exprs
, i
, save_expr
)
5044 ctx
->global
->values
.remove (save_expr
);
5049 /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
5053 cxx_eval_switch_expr (const constexpr_ctx
*ctx
, tree t
,
5054 bool *non_constant_p
, bool *overflow_p
,
5058 = TREE_CODE (t
) == SWITCH_STMT
? SWITCH_STMT_COND (t
) : SWITCH_COND (t
);
5059 cond
= cxx_eval_constant_expression (ctx
, cond
, false,
5060 non_constant_p
, overflow_p
);
5061 VERIFY_CONSTANT (cond
);
5062 *jump_target
= cond
;
5065 = TREE_CODE (t
) == SWITCH_STMT
? SWITCH_STMT_BODY (t
) : SWITCH_BODY (t
);
5066 constexpr_ctx new_ctx
= *ctx
;
5067 constexpr_switch_state css
= css_default_not_seen
;
5068 new_ctx
.css_state
= &css
;
5069 cxx_eval_constant_expression (&new_ctx
, body
, false,
5070 non_constant_p
, overflow_p
, jump_target
);
5071 if (switches (jump_target
) && css
== css_default_seen
)
5073 /* If the SWITCH_EXPR body has default: label, process it once again,
5074 this time instructing label_matches to return true for default:
5075 label on switches (jump_target). */
5076 css
= css_default_processing
;
5077 cxx_eval_constant_expression (&new_ctx
, body
, false,
5078 non_constant_p
, overflow_p
, jump_target
);
5080 if (breaks (jump_target
) || switches (jump_target
))
5081 *jump_target
= NULL_TREE
;
5085 /* Find the object of TYPE under initialization in CTX. */
5088 lookup_placeholder (const constexpr_ctx
*ctx
, bool lval
, tree type
)
5093 /* We could use ctx->object unconditionally, but using ctx->ctor when we
5094 can is a minor optimization. */
5095 if (!lval
&& ctx
->ctor
&& same_type_p (TREE_TYPE (ctx
->ctor
), type
))
5101 /* Since an object cannot have a field of its own type, we can search outward
5102 from ctx->object to find the unique containing object of TYPE. */
5103 tree ob
= ctx
->object
;
5106 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob
), type
))
5108 if (handled_component_p (ob
))
5109 ob
= TREE_OPERAND (ob
, 0);
5117 /* Complain about an attempt to evaluate inline assembly. */
5120 inline_asm_in_constexpr_error (location_t loc
)
5122 auto_diagnostic_group d
;
5123 error_at (loc
, "inline assembly is not a constant expression");
5124 inform (loc
, "only unevaluated inline assembly is allowed in a "
5125 "%<constexpr%> function in C++2a");
5128 /* Attempt to reduce the expression T to a constant value.
5129 On failure, issue diagnostic and return error_mark_node. */
5130 /* FIXME unify with c_fully_fold */
5131 /* FIXME overflow_p is too global */
5134 cxx_eval_constant_expression (const constexpr_ctx
*ctx
, tree t
,
5136 bool *non_constant_p
, bool *overflow_p
,
5137 tree
*jump_target
/* = NULL */)
5139 if (jump_target
&& *jump_target
)
5141 /* If we are jumping, ignore all statements/expressions except those
5142 that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */
5143 switch (TREE_CODE (t
))
5146 case STATEMENT_LIST
:
5155 case CASE_LABEL_EXPR
:
5156 if (label_matches (ctx
, jump_target
, t
))
5158 *jump_target
= NULL_TREE
;
5164 if (error_operand_p (t
))
5166 *non_constant_p
= true;
5170 location_t loc
= cp_expr_loc_or_input_loc (t
);
5172 STRIP_ANY_LOCATION_WRAPPER (t
);
5174 if (CONSTANT_CLASS_P (t
))
5176 if (TREE_OVERFLOW (t
))
5179 permerror (input_location
, "overflow in constant expression");
5180 if (!flag_permissive
|| ctx
->quiet
)
5184 if (TREE_CODE (t
) == INTEGER_CST
5185 && TYPE_PTR_P (TREE_TYPE (t
))
5186 && !integer_zerop (t
))
5189 error ("value %qE of type %qT is not a constant expression",
5191 *non_constant_p
= true;
5197 /* Avoid excessively long constexpr evaluations. */
5198 if (++ctx
->global
->constexpr_ops_count
>= constexpr_ops_limit
)
5202 "%<constexpr%> evaluation operation count exceeds limit of "
5203 "%wd (use %<-fconstexpr-ops-limit=%> to increase the limit)",
5204 constexpr_ops_limit
);
5205 ctx
->global
->constexpr_ops_count
= INTTYPE_MINIMUM (HOST_WIDE_INT
);
5206 *non_constant_p
= true;
5210 constexpr_ctx new_ctx
;
5213 tree_code tcode
= TREE_CODE (t
);
5219 /* We ask for an rvalue for the RESULT_DECL when indirecting
5220 through an invisible reference, or in named return value
5222 if (tree
*p
= ctx
->global
->values
.get (t
))
5227 error ("%qE is not a constant expression", t
);
5228 *non_constant_p
= true;
5233 if (DECL_HAS_VALUE_EXPR_P (t
))
5235 if (is_normal_capture_proxy (t
)
5236 && current_function_decl
== DECL_CONTEXT (t
))
5238 /* Function parms aren't constexpr within the function
5239 definition, so don't try to look at the closure. But if the
5240 captured variable is constant, try to evaluate it directly. */
5241 r
= DECL_CAPTURED_VARIABLE (t
);
5242 tree type
= TREE_TYPE (t
);
5243 if (TYPE_REF_P (type
) != TYPE_REF_P (TREE_TYPE (r
)))
5245 /* Adjust r to match the reference-ness of t. */
5246 if (TYPE_REF_P (type
))
5247 r
= build_address (r
);
5249 r
= convert_from_reference (r
);
5253 r
= DECL_VALUE_EXPR (t
);
5254 return cxx_eval_constant_expression (ctx
, r
, lval
, non_constant_p
,
5259 /* We used to not check lval for CONST_DECL, but darwin.c uses
5260 CONST_DECL for aggregate constants. */
5263 if (COMPLETE_TYPE_P (TREE_TYPE (t
))
5264 && is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
5266 /* If the class is empty, we aren't actually loading anything. */
5267 r
= build_constructor (TREE_TYPE (t
), NULL
);
5268 TREE_CONSTANT (r
) = true;
5270 else if (ctx
->strict
)
5271 r
= decl_really_constant_value (t
);
5273 r
= decl_constant_value (t
);
5274 if (TREE_CODE (r
) == TARGET_EXPR
5275 && TREE_CODE (TARGET_EXPR_INITIAL (r
)) == CONSTRUCTOR
)
5276 r
= TARGET_EXPR_INITIAL (r
);
5278 if (tree
*p
= ctx
->global
->values
.get (r
))
5279 if (*p
!= NULL_TREE
)
5284 non_const_var_error (loc
, r
);
5285 *non_constant_p
= true;
5289 case DEBUG_BEGIN_STMT
:
5290 /* ??? It might be nice to retain this information somehow, so
5291 as to be able to step into a constexpr function call. */
5298 case CASE_LABEL_EXPR
:
5303 if (lval
&& !TYPE_REF_P (TREE_TYPE (t
)))
5305 else if (tree
*p
= ctx
->global
->values
.get (r
))
5308 /* Defer in case this is only used for its type. */;
5309 else if (TYPE_REF_P (TREE_TYPE (t
)))
5310 /* Defer, there's no lvalue->rvalue conversion. */;
5311 else if (COMPLETE_TYPE_P (TREE_TYPE (t
))
5312 && is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
5314 /* If the class is empty, we aren't actually loading anything. */
5315 r
= build_constructor (TREE_TYPE (t
), NULL
);
5316 TREE_CONSTANT (r
) = true;
5321 error ("%qE is not a constant expression", t
);
5322 *non_constant_p
= true;
5327 case AGGR_INIT_EXPR
:
5328 r
= cxx_eval_call_expression (ctx
, t
, lval
,
5329 non_constant_p
, overflow_p
);
5334 r
= DECL_EXPR_DECL (t
);
5335 if (TREE_CODE (r
) == USING_DECL
)
5340 if (AGGREGATE_TYPE_P (TREE_TYPE (r
))
5341 || VECTOR_TYPE_P (TREE_TYPE (r
)))
5345 new_ctx
.ctor
= build_constructor (TREE_TYPE (r
), NULL
);
5346 CONSTRUCTOR_NO_CLEARING (new_ctx
.ctor
) = true;
5347 ctx
->global
->values
.put (r
, new_ctx
.ctor
);
5351 if (tree init
= DECL_INITIAL (r
))
5353 init
= cxx_eval_constant_expression (ctx
, init
,
5355 non_constant_p
, overflow_p
);
5356 /* Don't share a CONSTRUCTOR that might be changed. */
5357 init
= unshare_constructor (init
);
5358 /* Remember that a constant object's constructor has already
5360 if (CLASS_TYPE_P (TREE_TYPE (r
))
5361 && CP_TYPE_CONST_P (TREE_TYPE (r
)))
5362 TREE_READONLY (init
) = true;
5363 ctx
->global
->values
.put (r
, init
);
5365 else if (ctx
== &new_ctx
)
5366 /* We gave it a CONSTRUCTOR above. */;
5368 ctx
->global
->values
.put (r
, NULL_TREE
);
5373 if (!literal_type_p (TREE_TYPE (t
)))
5377 auto_diagnostic_group d
;
5378 error ("temporary of non-literal type %qT in a "
5379 "constant expression", TREE_TYPE (t
));
5380 explain_non_literal_class (TREE_TYPE (t
));
5382 *non_constant_p
= true;
5385 gcc_checking_assert (!TARGET_EXPR_DIRECT_INIT_P (t
));
5386 /* Avoid evaluating a TARGET_EXPR more than once. */
5387 if (tree
*p
= ctx
->global
->values
.get (TARGET_EXPR_SLOT (t
)))
5390 return TARGET_EXPR_SLOT (t
);
5394 if ((AGGREGATE_TYPE_P (TREE_TYPE (t
)) || VECTOR_TYPE_P (TREE_TYPE (t
))))
5396 /* We're being expanded without an explicit target, so start
5397 initializing a new object; expansion with an explicit target
5398 strips the TARGET_EXPR before we get here. */
5400 new_ctx
.ctor
= build_constructor (TREE_TYPE (t
), NULL
);
5401 CONSTRUCTOR_NO_CLEARING (new_ctx
.ctor
) = true;
5402 new_ctx
.object
= TARGET_EXPR_SLOT (t
);
5403 ctx
->global
->values
.put (new_ctx
.object
, new_ctx
.ctor
);
5406 /* Pass false for 'lval' because this indicates
5407 initialization of a temporary. */
5408 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
5410 non_constant_p
, overflow_p
);
5411 if (!*non_constant_p
)
5412 /* Adjust the type of the result to the type of the temporary. */
5413 r
= adjust_temp_type (TREE_TYPE (t
), r
);
5414 if (TARGET_EXPR_CLEANUP (t
) && !CLEANUP_EH_ONLY (t
))
5415 ctx
->global
->cleanups
->safe_push (TARGET_EXPR_CLEANUP (t
));
5416 r
= unshare_constructor (r
);
5417 ctx
->global
->values
.put (TARGET_EXPR_SLOT (t
), r
);
5418 if (ctx
->save_exprs
)
5419 ctx
->save_exprs
->safe_push (TARGET_EXPR_SLOT (t
));
5421 return TARGET_EXPR_SLOT (t
);
5426 gcc_assert (jump_target
== NULL
|| *jump_target
== NULL_TREE
);
5427 r
= cxx_eval_store_expression (ctx
, t
, lval
,
5428 non_constant_p
, overflow_p
);
5432 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
5434 non_constant_p
, overflow_p
);
5438 if (TREE_OPERAND (t
, 0) != NULL_TREE
)
5439 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
5441 non_constant_p
, overflow_p
);
5449 /* Can happen with ({ return true; }) && false; passed to
5450 maybe_constant_value. There is nothing to jump over in this
5451 case, and the bug will be diagnosed later. */
5452 gcc_assert (ctx
->quiet
);
5453 *non_constant_p
= true;
5458 /* Avoid evaluating a SAVE_EXPR more than once. */
5459 if (tree
*p
= ctx
->global
->values
.get (t
))
5463 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), false,
5464 non_constant_p
, overflow_p
);
5465 ctx
->global
->values
.put (t
, r
);
5466 if (ctx
->save_exprs
)
5467 ctx
->save_exprs
->safe_push (t
);
5471 case TRY_CATCH_EXPR
:
5472 if (TREE_OPERAND (t
, 0) == NULL_TREE
)
5478 case NON_LVALUE_EXPR
:
5480 case MUST_NOT_THROW_EXPR
:
5483 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
5485 non_constant_p
, overflow_p
,
5489 case CLEANUP_POINT_EXPR
:
5491 auto_vec
<tree
, 2> cleanups
;
5492 vec
<tree
> *prev_cleanups
= ctx
->global
->cleanups
;
5493 ctx
->global
->cleanups
= &cleanups
;
5494 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
5496 non_constant_p
, overflow_p
,
5498 ctx
->global
->cleanups
= prev_cleanups
;
5501 /* Evaluate the cleanups. */
5502 FOR_EACH_VEC_ELT_REVERSE (cleanups
, i
, cleanup
)
5503 cxx_eval_constant_expression (ctx
, cleanup
, false,
5504 non_constant_p
, overflow_p
,
5509 case TRY_FINALLY_EXPR
:
5510 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), lval
,
5511 non_constant_p
, overflow_p
,
5513 if (!*non_constant_p
)
5514 /* Also evaluate the cleanup. */
5515 cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1), true,
5516 non_constant_p
, overflow_p
,
5522 tree initial_jump_target
= jump_target
? *jump_target
: NULL_TREE
;
5523 r
= cxx_eval_constant_expression (ctx
, CLEANUP_BODY (t
), lval
,
5524 non_constant_p
, overflow_p
,
5526 if (!CLEANUP_EH_ONLY (t
) && !*non_constant_p
)
5528 iloc_sentinel
ils (loc
);
5529 /* Also evaluate the cleanup. If we weren't skipping at the
5530 start of the CLEANUP_BODY, change jump_target temporarily
5531 to &initial_jump_target, so that even a return or break or
5532 continue in the body doesn't skip the cleanup. */
5533 cxx_eval_constant_expression (ctx
, CLEANUP_EXPR (t
), true,
5534 non_constant_p
, overflow_p
,
5535 jump_target
? &initial_jump_target
5541 /* These differ from cxx_eval_unary_expression in that this doesn't
5542 check for a constant operand or result; an address can be
5543 constant without its operand being, and vice versa. */
5546 r
= cxx_eval_indirect_ref (ctx
, t
, lval
,
5547 non_constant_p
, overflow_p
);
5552 tree oldop
= TREE_OPERAND (t
, 0);
5553 tree op
= cxx_eval_constant_expression (ctx
, oldop
,
5555 non_constant_p
, overflow_p
);
5556 /* Don't VERIFY_CONSTANT here. */
5557 if (*non_constant_p
)
5559 gcc_checking_assert (TREE_CODE (op
) != CONSTRUCTOR
);
5560 /* This function does more aggressive folding than fold itself. */
5561 r
= build_fold_addr_expr_with_type (op
, TREE_TYPE (t
));
5562 if (TREE_CODE (r
) == ADDR_EXPR
&& TREE_OPERAND (r
, 0) == oldop
)
5574 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), lval
,
5575 non_constant_p
, overflow_p
);
5576 if (r
== error_mark_node
)
5578 else if (r
== TREE_OPERAND (t
, 0))
5581 r
= fold_build1 (TREE_CODE (t
), TREE_TYPE (t
), r
);
5586 case FIX_TRUNC_EXPR
:
5592 case TRUTH_NOT_EXPR
:
5593 case FIXED_CONVERT_EXPR
:
5594 r
= cxx_eval_unary_expression (ctx
, t
, lval
,
5595 non_constant_p
, overflow_p
);
5599 r
= fold_sizeof_expr (t
);
5600 /* In a template, fold_sizeof_expr may merely create a new SIZEOF_EXPR,
5601 which could lead to an infinite recursion. */
5602 if (TREE_CODE (r
) != SIZEOF_EXPR
)
5603 r
= cxx_eval_constant_expression (ctx
, r
, lval
,
5604 non_constant_p
, overflow_p
,
5608 *non_constant_p
= true;
5609 gcc_assert (ctx
->quiet
);
5616 /* check_return_expr sometimes wraps a TARGET_EXPR in a
5617 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
5618 introduced by build_call_a. */
5619 tree op0
= TREE_OPERAND (t
, 0);
5620 tree op1
= TREE_OPERAND (t
, 1);
5622 if ((TREE_CODE (op0
) == TARGET_EXPR
&& op1
== TARGET_EXPR_SLOT (op0
))
5623 || TREE_CODE (op1
) == EMPTY_CLASS_EXPR
)
5624 r
= cxx_eval_constant_expression (ctx
, op0
,
5625 lval
, non_constant_p
, overflow_p
,
5629 /* Check that the LHS is constant and then discard it. */
5630 cxx_eval_constant_expression (ctx
, op0
,
5631 true, non_constant_p
, overflow_p
,
5633 if (*non_constant_p
)
5635 op1
= TREE_OPERAND (t
, 1);
5636 r
= cxx_eval_constant_expression (ctx
, op1
,
5637 lval
, non_constant_p
, overflow_p
,
5643 case POINTER_PLUS_EXPR
:
5644 case POINTER_DIFF_EXPR
:
5648 case TRUNC_DIV_EXPR
:
5650 case FLOOR_DIV_EXPR
:
5651 case ROUND_DIV_EXPR
:
5652 case TRUNC_MOD_EXPR
:
5654 case ROUND_MOD_EXPR
:
5656 case EXACT_DIV_EXPR
:
5664 case TRUTH_XOR_EXPR
:
5671 case SPACESHIP_EXPR
:
5672 case UNORDERED_EXPR
:
5682 r
= cxx_eval_binary_expression (ctx
, t
, lval
,
5683 non_constant_p
, overflow_p
);
5686 /* fold can introduce non-IF versions of these; still treat them as
5687 short-circuiting. */
5688 case TRUTH_AND_EXPR
:
5689 case TRUTH_ANDIF_EXPR
:
5690 r
= cxx_eval_logical_expression (ctx
, t
, boolean_false_node
,
5693 non_constant_p
, overflow_p
);
5697 case TRUTH_ORIF_EXPR
:
5698 r
= cxx_eval_logical_expression (ctx
, t
, boolean_true_node
,
5701 non_constant_p
, overflow_p
);
5705 r
= cxx_eval_array_reference (ctx
, t
, lval
,
5706 non_constant_p
, overflow_p
);
5710 if (is_overloaded_fn (t
))
5712 /* We can only get here in checking mode via
5713 build_non_dependent_expr, because any expression that
5714 calls or takes the address of the function will have
5715 pulled a FUNCTION_DECL out of the COMPONENT_REF. */
5716 gcc_checking_assert (ctx
->quiet
|| errorcount
);
5717 *non_constant_p
= true;
5720 r
= cxx_eval_component_reference (ctx
, t
, lval
,
5721 non_constant_p
, overflow_p
);
5725 r
= cxx_eval_bit_field_ref (ctx
, t
, lval
,
5726 non_constant_p
, overflow_p
);
5731 if (jump_target
&& *jump_target
)
5733 tree orig_jump
= *jump_target
;
5734 tree arg
= ((TREE_CODE (t
) != IF_STMT
|| TREE_OPERAND (t
, 1))
5735 ? TREE_OPERAND (t
, 1) : void_node
);
5736 /* When jumping to a label, the label might be either in the
5737 then or else blocks, so process then block first in skipping
5738 mode first, and if we are still in the skipping mode at its end,
5739 process the else block too. */
5740 r
= cxx_eval_constant_expression (ctx
, arg
, lval
, non_constant_p
,
5741 overflow_p
, jump_target
);
5742 /* It's possible that we found the label in the then block. But
5743 it could have been followed by another jumping statement, e.g.
5744 say we're looking for case 1:
5747 // skipped statements
5748 case 1:; // clears up *jump_target
5749 return 1; // and sets it to a RETURN_EXPR
5752 in which case we need not go looking to the else block.
5753 (goto is not allowed in a constexpr function.) */
5754 if (*jump_target
== orig_jump
)
5756 arg
= ((TREE_CODE (t
) != IF_STMT
|| TREE_OPERAND (t
, 2))
5757 ? TREE_OPERAND (t
, 2) : void_node
);
5758 r
= cxx_eval_constant_expression (ctx
, arg
, lval
, non_constant_p
,
5759 overflow_p
, jump_target
);
5763 r
= cxx_eval_conditional_expression (ctx
, t
, lval
,
5764 non_constant_p
, overflow_p
,
5768 r
= cxx_eval_vector_conditional_expression (ctx
, t
, non_constant_p
,
5773 if (TREE_CONSTANT (t
) && reduced_constant_expression_p (t
))
5775 /* Don't re-process a constant CONSTRUCTOR, but do fold it to
5776 VECTOR_CST if applicable. */
5777 verify_constructor_flags (t
);
5778 if (TREE_CONSTANT (t
))
5781 r
= cxx_eval_bare_aggregate (ctx
, t
, lval
,
5782 non_constant_p
, overflow_p
);
5786 /* We can get this in a defaulted constructor for a class with a
5787 non-static data member of array type. Either the initializer will
5788 be NULL, meaning default-initialization, or it will be an lvalue
5789 or xvalue of the same type, meaning direct-initialization from the
5790 corresponding member. */
5791 r
= cxx_eval_vec_init (ctx
, t
, lval
,
5792 non_constant_p
, overflow_p
);
5796 r
= cxx_eval_trinary_expression (ctx
, t
, lval
,
5797 non_constant_p
, overflow_p
);
5801 if (REINTERPRET_CAST_P (t
))
5805 "%<reinterpret_cast%> is not a constant expression");
5806 *non_constant_p
= true;
5811 case VIEW_CONVERT_EXPR
:
5812 case UNARY_PLUS_EXPR
:
5814 tree oldop
= TREE_OPERAND (t
, 0);
5816 tree op
= cxx_eval_constant_expression (ctx
, oldop
,
5818 non_constant_p
, overflow_p
);
5819 if (*non_constant_p
)
5821 tree type
= TREE_TYPE (t
);
5823 if (VOID_TYPE_P (type
))
5826 if (TREE_CODE (op
) == PTRMEM_CST
&& !TYPE_PTRMEM_P (type
))
5827 op
= cplus_expand_constant (op
);
5829 if (TREE_CODE (op
) == PTRMEM_CST
&& tcode
== NOP_EXPR
)
5831 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (op
))
5832 && !can_convert_qual (type
, op
))
5833 op
= cplus_expand_constant (op
);
5834 return cp_fold_convert (type
, op
);
5837 if (INDIRECT_TYPE_P (type
) && TREE_CODE (op
) == INTEGER_CST
)
5839 if (integer_zerop (op
))
5841 if (TYPE_REF_P (type
))
5845 "dereferencing a null pointer");
5846 *non_constant_p
= true;
5849 else if (TYPE_PTR_P (TREE_TYPE (op
)))
5851 tree from
= TREE_TYPE (op
);
5853 if (!can_convert (type
, from
, tf_none
))
5857 "conversion of %qT null pointer to %qT "
5858 "is not a constant expression",
5860 *non_constant_p
= true;
5867 /* This detects for example:
5868 reinterpret_cast<void*>(sizeof 0)
5871 error_at (loc
, "%<reinterpret_cast<%T>(%E)%> is not "
5872 "a constant expression",
5874 *non_constant_p
= true;
5879 if (INDIRECT_TYPE_P (type
)
5880 && TREE_CODE (op
) == NOP_EXPR
5881 && TREE_TYPE (op
) == ptr_type_node
5882 && TREE_CODE (TREE_OPERAND (op
, 0)) == ADDR_EXPR
5883 && VAR_P (TREE_OPERAND (TREE_OPERAND (op
, 0), 0))
5884 && DECL_NAME (TREE_OPERAND (TREE_OPERAND (op
, 0),
5885 0)) == heap_uninit_identifier
)
5887 tree var
= TREE_OPERAND (TREE_OPERAND (op
, 0), 0);
5888 tree var_size
= TYPE_SIZE_UNIT (TREE_TYPE (var
));
5889 tree elt_type
= TREE_TYPE (type
);
5890 tree cookie_size
= NULL_TREE
;
5891 if (TREE_CODE (elt_type
) == RECORD_TYPE
5892 && TYPE_NAME (elt_type
) == heap_identifier
)
5894 tree fld1
= TYPE_FIELDS (elt_type
);
5895 tree fld2
= DECL_CHAIN (fld1
);
5896 elt_type
= TREE_TYPE (TREE_TYPE (fld2
));
5897 cookie_size
= TYPE_SIZE_UNIT (TREE_TYPE (fld1
));
5899 DECL_NAME (var
) = heap_identifier
;
5901 = build_new_constexpr_heap_type (elt_type
, cookie_size
,
5903 TREE_TYPE (TREE_OPERAND (op
, 0))
5904 = build_pointer_type (TREE_TYPE (var
));
5907 if (op
== oldop
&& tcode
!= UNARY_PLUS_EXPR
)
5908 /* We didn't fold at the top so we could check for ptr-int
5914 /* Handle an array's bounds having been deduced after we built
5915 the wrapping expression. */
5916 if (same_type_ignoring_tlq_and_bounds_p (type
, TREE_TYPE (op
)))
5918 else if (sop
= tree_strip_nop_conversions (op
),
5919 sop
!= op
&& (same_type_ignoring_tlq_and_bounds_p
5920 (type
, TREE_TYPE (sop
))))
5922 else if (tcode
== UNARY_PLUS_EXPR
)
5923 r
= fold_convert (TREE_TYPE (t
), op
);
5925 r
= fold_build1 (tcode
, type
, op
);
5927 /* Conversion of an out-of-range value has implementation-defined
5928 behavior; the language considers it different from arithmetic
5929 overflow, which is undefined. */
5930 if (TREE_OVERFLOW_P (r
) && !TREE_OVERFLOW_P (op
))
5931 TREE_OVERFLOW (r
) = false;
5935 case EMPTY_CLASS_EXPR
:
5936 /* This is good enough for a function argument that might not get
5937 used, and they can't do anything with it, so just return it. */
5940 case STATEMENT_LIST
:
5942 new_ctx
.ctor
= new_ctx
.object
= NULL_TREE
;
5943 return cxx_eval_statement_list (&new_ctx
, t
,
5944 non_constant_p
, overflow_p
, jump_target
);
5947 return cxx_eval_constant_expression (ctx
, BIND_EXPR_BODY (t
),
5949 non_constant_p
, overflow_p
,
5952 case PREINCREMENT_EXPR
:
5953 case POSTINCREMENT_EXPR
:
5954 case PREDECREMENT_EXPR
:
5955 case POSTDECREMENT_EXPR
:
5956 return cxx_eval_increment_expression (ctx
, t
,
5957 lval
, non_constant_p
, overflow_p
);
5963 case VEC_DELETE_EXPR
:
5966 /* GCC internal stuff. */
5968 case NON_DEPENDENT_EXPR
:
5972 error_at (loc
, "expression %qE is not a constant expression", t
);
5973 *non_constant_p
= true;
5978 /* Virtual function call. Let the constexpr machinery figure out
5979 the dynamic type. */
5980 int token
= tree_to_shwi (OBJ_TYPE_REF_TOKEN (t
));
5981 tree obj
= OBJ_TYPE_REF_OBJECT (t
);
5982 obj
= cxx_eval_constant_expression (ctx
, obj
, lval
, non_constant_p
,
5985 /* We expect something in the form of &x.D.2103.D.2094; get x. */
5986 if (TREE_CODE (obj
) != ADDR_EXPR
5987 || !DECL_P (get_base_address (TREE_OPERAND (obj
, 0))))
5990 error_at (loc
, "expression %qE is not a constant expression", t
);
5991 *non_constant_p
= true;
5994 obj
= TREE_OPERAND (obj
, 0);
5995 while (TREE_CODE (obj
) == COMPONENT_REF
5996 && DECL_FIELD_IS_BASE (TREE_OPERAND (obj
, 1)))
5997 obj
= TREE_OPERAND (obj
, 0);
5998 tree objtype
= TREE_TYPE (obj
);
5999 /* Find the function decl in the virtual functions list. TOKEN is
6000 the DECL_VINDEX that says which function we're looking for. */
6001 tree virtuals
= BINFO_VIRTUALS (TYPE_BINFO (objtype
));
6002 if (TARGET_VTABLE_USES_DESCRIPTORS
)
6003 token
/= MAX (TARGET_VTABLE_USES_DESCRIPTORS
, 1);
6004 r
= TREE_VALUE (chain_index (token
, virtuals
));
6008 case PLACEHOLDER_EXPR
:
6009 /* Use of the value or address of the current object. */
6010 if (tree ctor
= lookup_placeholder (ctx
, lval
, TREE_TYPE (t
)))
6011 return cxx_eval_constant_expression (ctx
, ctor
, lval
,
6012 non_constant_p
, overflow_p
);
6013 /* A placeholder without a referent. We can get here when
6014 checking whether NSDMIs are noexcept, or in massage_init_elt;
6015 just say it's non-constant for now. */
6016 gcc_assert (ctx
->quiet
);
6017 *non_constant_p
= true;
6022 tree cond
= TREE_OPERAND (t
, 0);
6023 cond
= cxx_eval_constant_expression (ctx
, cond
, /*lval*/false,
6024 non_constant_p
, overflow_p
);
6025 VERIFY_CONSTANT (cond
);
6026 if (integer_nonzerop (cond
))
6032 *jump_target
= TREE_OPERAND (t
, 0);
6033 gcc_assert (breaks (jump_target
) || continues (jump_target
)
6034 /* Allow for jumping to a cdtor_label. */
6035 || returns (jump_target
));
6042 cxx_eval_loop_expr (ctx
, t
,
6043 non_constant_p
, overflow_p
, jump_target
);
6048 cxx_eval_switch_expr (ctx
, t
,
6049 non_constant_p
, overflow_p
, jump_target
);
6053 /* It's possible to get a requires-expression in a constant
6054 expression. For example:
6056 template<typename T> concept bool C() {
6057 return requires (T t) { t; };
6060 template<typename T> requires !C<T>() void f(T);
6062 Normalization leaves f with the associated constraint
6063 '!requires (T t) { ... }' which is not transformed into
6065 if (!processing_template_decl
)
6066 return satisfy_constraint_expression (t
);
6068 *non_constant_p
= true;
6072 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
6074 non_constant_p
, overflow_p
,
6082 case TEMPLATE_ID_EXPR
:
6084 /* We can evaluate template-id that refers to a concept only if
6085 the template arguments are non-dependent. */
6086 tree id
= unpack_concept_check (t
);
6087 tree tmpl
= TREE_OPERAND (id
, 0);
6088 if (!concept_definition_p (tmpl
))
6089 internal_error ("unexpected template-id %qE", t
);
6091 if (function_concept_p (tmpl
))
6094 error_at (cp_expr_loc_or_input_loc (t
),
6095 "function concept must be called");
6096 r
= error_mark_node
;
6100 if (!processing_template_decl
)
6101 r
= evaluate_concept_check (t
, tf_warning_or_error
);
6103 *non_constant_p
= true;
6110 inline_asm_in_constexpr_error (loc
);
6111 *non_constant_p
= true;
6115 if (STATEMENT_CODE_P (TREE_CODE (t
)))
6117 /* This function doesn't know how to deal with pre-genericize
6118 statements; this can only happen with statement-expressions,
6119 so for now just fail. */
6121 error_at (EXPR_LOCATION (t
),
6122 "statement is not a constant expression");
6125 internal_error ("unexpected expression %qE of kind %s", t
,
6126 get_tree_code_name (TREE_CODE (t
)));
6127 *non_constant_p
= true;
6131 if (r
== error_mark_node
)
6132 *non_constant_p
= true;
6134 if (*non_constant_p
)
6140 /* P0859: A function is needed for constant evaluation if it is a constexpr
6141 function that is named by an expression ([basic.def.odr]) that is
6142 potentially constant evaluated.
6144 So we need to instantiate any constexpr functions mentioned by the
6145 expression even if the definition isn't needed for evaluating the
6149 instantiate_cx_fn_r (tree
*tp
, int *walk_subtrees
, void */
*data*/
)
6151 if (TREE_CODE (*tp
) == FUNCTION_DECL
6152 && DECL_DECLARED_CONSTEXPR_P (*tp
)
6153 && !DECL_INITIAL (*tp
)
6154 && !trivial_fn_p (*tp
)
6155 && DECL_TEMPLOID_INSTANTIATION (*tp
))
6158 instantiate_decl (*tp
, /*defer_ok*/false, /*expl_inst*/false);
6161 else if (TREE_CODE (*tp
) == CALL_EXPR
6162 || TREE_CODE (*tp
) == AGGR_INIT_EXPR
)
6164 if (EXPR_HAS_LOCATION (*tp
))
6165 input_location
= EXPR_LOCATION (*tp
);
6175 instantiate_constexpr_fns (tree t
)
6177 location_t loc
= input_location
;
6178 cp_walk_tree_without_duplicates (&t
, instantiate_cx_fn_r
, NULL
);
6179 input_location
= loc
;
6182 /* Look for heap variables in the expression *TP. */
6185 find_heap_var_refs (tree
*tp
, int *walk_subtrees
, void */
*data*/
)
6188 && (DECL_NAME (*tp
) == heap_uninit_identifier
6189 || DECL_NAME (*tp
) == heap_identifier
6190 || DECL_NAME (*tp
) == heap_deleted_identifier
))
6198 /* Find immediate function decls in *TP if any. */
6201 find_immediate_fndecl (tree
*tp
, int */
*walk_subtrees*/
, void */
*data*/
)
6203 if (TREE_CODE (*tp
) == FUNCTION_DECL
&& DECL_IMMEDIATE_FUNCTION_P (*tp
))
6208 /* ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
6209 STRICT has the same sense as for constant_value_1: true if we only allow
6210 conforming C++ constant expressions, or false if we want a constant value
6211 even if it doesn't conform.
6212 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
6213 per P0595 even when ALLOW_NON_CONSTANT is true.
6214 CONSTEXPR_DTOR is true when evaluating the dtor of a constexpr variable.
6215 OBJECT must be non-NULL in that case. */
6218 cxx_eval_outermost_constant_expr (tree t
, bool allow_non_constant
,
6220 bool manifestly_const_eval
= false,
6221 bool constexpr_dtor
= false,
6222 tree object
= NULL_TREE
)
6224 auto_timevar
time (TV_CONSTEXPR
);
6226 bool non_constant_p
= false;
6227 bool overflow_p
= false;
6229 constexpr_global_ctx global_ctx
;
6230 constexpr_ctx ctx
= { &global_ctx
, NULL
, NULL
, NULL
, NULL
, NULL
,
6231 allow_non_constant
, strict
,
6232 manifestly_const_eval
|| !allow_non_constant
};
6234 tree type
= initialized_type (t
);
6236 bool is_consteval
= false;
6237 if (VOID_TYPE_P (type
))
6240 /* Used for destructors of array elements. */
6241 type
= TREE_TYPE (object
);
6244 if (cxx_dialect
< cxx2a
)
6246 if (TREE_CODE (t
) != CALL_EXPR
&& TREE_CODE (t
) != AGGR_INIT_EXPR
)
6248 /* Calls to immediate functions returning void need to be
6250 tree fndecl
= cp_get_callee_fndecl_nofold (t
);
6251 if (fndecl
== NULL_TREE
|| !DECL_IMMEDIATE_FUNCTION_P (fndecl
))
6254 is_consteval
= true;
6257 else if (cxx_dialect
>= cxx2a
6258 && (TREE_CODE (t
) == CALL_EXPR
6259 || TREE_CODE (t
) == AGGR_INIT_EXPR
6260 || TREE_CODE (t
) == TARGET_EXPR
))
6262 /* For non-concept checks, determine if it is consteval. */
6263 if (!concept_check_p (t
))
6266 if (TREE_CODE (x
) == TARGET_EXPR
)
6267 x
= TARGET_EXPR_INITIAL (x
);
6268 tree fndecl
= cp_get_callee_fndecl_nofold (x
);
6269 if (fndecl
&& DECL_IMMEDIATE_FUNCTION_P (fndecl
))
6270 is_consteval
= true;
6273 if (AGGREGATE_TYPE_P (type
) || VECTOR_TYPE_P (type
))
6275 /* In C++14 an NSDMI can participate in aggregate initialization,
6276 and can refer to the address of the object being initialized, so
6277 we need to pass in the relevant VAR_DECL if we want to do the
6278 evaluation in a single pass. The evaluation will dynamically
6279 update ctx.values for the VAR_DECL. We use the same strategy
6280 for C++11 constexpr constructors that refer to the object being
6284 gcc_assert (object
&& VAR_P (object
));
6285 gcc_assert (DECL_DECLARED_CONSTEXPR_P (object
));
6286 gcc_assert (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object
));
6287 if (error_operand_p (DECL_INITIAL (object
)))
6289 ctx
.ctor
= unshare_expr (DECL_INITIAL (object
));
6290 TREE_READONLY (ctx
.ctor
) = false;
6291 /* Temporarily force decl_really_constant_value to return false
6292 for it, we want to use ctx.ctor for the current value instead. */
6293 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object
) = false;
6297 ctx
.ctor
= build_constructor (type
, NULL
);
6298 CONSTRUCTOR_NO_CLEARING (ctx
.ctor
) = true;
6302 if (TREE_CODE (t
) == TARGET_EXPR
)
6303 object
= TARGET_EXPR_SLOT (t
);
6304 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
)
6305 object
= AGGR_INIT_EXPR_SLOT (t
);
6307 ctx
.object
= object
;
6309 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6310 (type
, TREE_TYPE (object
)));
6311 if (object
&& DECL_P (object
))
6312 global_ctx
.values
.put (object
, ctx
.ctor
);
6313 if (TREE_CODE (r
) == TARGET_EXPR
)
6314 /* Avoid creating another CONSTRUCTOR when we expand the
6316 r
= TARGET_EXPR_INITIAL (r
);
6319 auto_vec
<tree
, 16> cleanups
;
6320 global_ctx
.cleanups
= &cleanups
;
6322 instantiate_constexpr_fns (r
);
6323 r
= cxx_eval_constant_expression (&ctx
, r
,
6324 false, &non_constant_p
, &overflow_p
);
6326 if (!constexpr_dtor
)
6327 verify_constant (r
, allow_non_constant
, &non_constant_p
, &overflow_p
);
6329 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object
) = true;
6333 /* Evaluate the cleanups. */
6334 FOR_EACH_VEC_ELT_REVERSE (cleanups
, i
, cleanup
)
6335 cxx_eval_constant_expression (&ctx
, cleanup
, false,
6336 &non_constant_p
, &overflow_p
);
6338 /* Mutable logic is a bit tricky: we want to allow initialization of
6339 constexpr variables with mutable members, but we can't copy those
6340 members to another constexpr variable. */
6341 if (TREE_CODE (r
) == CONSTRUCTOR
&& CONSTRUCTOR_MUTABLE_POISON (r
))
6343 if (!allow_non_constant
)
6344 error ("%qE is not a constant expression because it refers to "
6345 "mutable subobjects of %qT", t
, type
);
6346 non_constant_p
= true;
6349 if (TREE_CODE (r
) == CONSTRUCTOR
&& CONSTRUCTOR_NO_CLEARING (r
))
6351 if (!allow_non_constant
)
6352 error ("%qE is not a constant expression because it refers to "
6353 "an incompletely initialized variable", t
);
6354 TREE_CONSTANT (r
) = false;
6355 non_constant_p
= true;
6358 if (!global_ctx
.heap_vars
.is_empty ())
6360 tree heap_var
= cp_walk_tree_without_duplicates (&r
, find_heap_var_refs
,
6365 if (!allow_non_constant
&& !non_constant_p
)
6366 error_at (DECL_SOURCE_LOCATION (heap_var
),
6367 "%qE is not a constant expression because it refers to "
6368 "a result of %<operator new%>", t
);
6370 non_constant_p
= true;
6372 FOR_EACH_VEC_ELT (global_ctx
.heap_vars
, i
, heap_var
)
6373 if (DECL_NAME (heap_var
) != heap_deleted_identifier
)
6375 if (!allow_non_constant
&& !non_constant_p
)
6376 error_at (DECL_SOURCE_LOCATION (heap_var
),
6377 "%qE is not a constant expression because allocated "
6378 "storage has not been deallocated", t
);
6380 non_constant_p
= true;
6384 /* Check that immediate invocation does not return an expression referencing
6385 any immediate function decls. They need to be allowed while parsing
6386 immediate functions, but can't leak outside of them. */
6389 && (current_function_decl
== NULL_TREE
6390 || !DECL_IMMEDIATE_FUNCTION_P (current_function_decl
)))
6391 if (tree immediate_fndecl
6392 = cp_walk_tree_without_duplicates (&r
, find_immediate_fndecl
,
6395 if (!allow_non_constant
&& !non_constant_p
)
6396 error_at (cp_expr_loc_or_input_loc (t
),
6397 "immediate evaluation returns address of immediate "
6398 "function %qD", immediate_fndecl
);
6400 non_constant_p
= true;
6403 /* Technically we should check this for all subexpressions, but that
6404 runs into problems with our internal representation of pointer
6405 subtraction and the 5.19 rules are still in flux. */
6406 if (CONVERT_EXPR_CODE_P (TREE_CODE (r
))
6407 && ARITHMETIC_TYPE_P (TREE_TYPE (r
))
6408 && TREE_CODE (TREE_OPERAND (r
, 0)) == ADDR_EXPR
)
6410 if (!allow_non_constant
)
6411 error ("conversion from pointer type %qT "
6412 "to arithmetic type %qT in a constant expression",
6413 TREE_TYPE (TREE_OPERAND (r
, 0)), TREE_TYPE (r
));
6414 non_constant_p
= true;
6417 if (!non_constant_p
&& overflow_p
)
6418 non_constant_p
= true;
6420 /* Unshare the result. */
6421 bool should_unshare
= true;
6422 if (r
== t
|| (TREE_CODE (t
) == TARGET_EXPR
6423 && TARGET_EXPR_INITIAL (t
) == r
))
6424 should_unshare
= false;
6426 if (non_constant_p
&& !allow_non_constant
)
6427 return error_mark_node
;
6428 else if (constexpr_dtor
)
6430 else if (non_constant_p
&& TREE_CONSTANT (r
))
6432 /* If __builtin_is_constant_evaluated () was evaluated to true
6433 and the result is not a valid constant expression, we need to
6435 if (manifestly_const_eval
)
6436 return cxx_eval_outermost_constant_expr (t
, true, strict
,
6437 false, false, object
);
6438 /* This isn't actually constant, so unset TREE_CONSTANT.
6439 Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires
6440 it to be set if it is invariant address, even when it is not
6441 a valid C++ constant expression. Wrap it with a NOP_EXPR
6443 if (EXPR_P (r
) && TREE_CODE (r
) != ADDR_EXPR
)
6445 else if (TREE_CODE (r
) == CONSTRUCTOR
)
6446 r
= build1 (VIEW_CONVERT_EXPR
, TREE_TYPE (r
), r
);
6448 r
= build_nop (TREE_TYPE (r
), r
);
6449 TREE_CONSTANT (r
) = false;
6451 else if (non_constant_p
)
6455 r
= unshare_expr (r
);
6457 if (TREE_CODE (r
) == CONSTRUCTOR
&& CLASS_TYPE_P (TREE_TYPE (r
)))
6459 r
= adjust_temp_type (type
, r
);
6460 if (TREE_CODE (t
) == TARGET_EXPR
6461 && TARGET_EXPR_INITIAL (t
) == r
)
6463 else if (TREE_CODE (t
) != CONSTRUCTOR
)
6465 r
= get_target_expr_sfinae (r
, tf_warning_or_error
| tf_no_cleanup
);
6466 TREE_CONSTANT (r
) = true;
6473 /* If T represents a constant expression returns its reduced value.
6474 Otherwise return error_mark_node. If T is dependent, then
6478 cxx_constant_value (tree t
, tree decl
)
6480 return cxx_eval_outermost_constant_expr (t
, false, true, true, false, decl
);
6483 /* Like cxx_constant_value, but used for evaluation of constexpr destructors
6484 of constexpr variables. The actual initializer of DECL is not modified. */
6487 cxx_constant_dtor (tree t
, tree decl
)
6489 cxx_eval_outermost_constant_expr (t
, false, true, true, true, decl
);
6492 /* Helper routine for fold_simple function. Either return simplified
6493 expression T, otherwise NULL_TREE.
6494 In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold
6495 even if we are within template-declaration. So be careful on call, as in
6496 such case types can be undefined. */
6499 fold_simple_1 (tree t
)
6502 enum tree_code code
= TREE_CODE (t
);
6514 return fold_sizeof_expr (t
);
6523 case TRUTH_NOT_EXPR
:
6525 case VIEW_CONVERT_EXPR
:
6528 case FIX_TRUNC_EXPR
:
6529 case FIXED_CONVERT_EXPR
:
6530 case ADDR_SPACE_CONVERT_EXPR
:
6532 op1
= TREE_OPERAND (t
, 0);
6534 t
= const_unop (code
, TREE_TYPE (t
), op1
);
6538 if (CONVERT_EXPR_CODE_P (code
)
6539 && TREE_OVERFLOW_P (t
) && !TREE_OVERFLOW_P (op1
))
6540 TREE_OVERFLOW (t
) = false;
6548 /* If T is a simple constant expression, returns its simplified value.
6549 Otherwise returns T. In contrast to maybe_constant_value we
6550 simplify only few operations on constant-expressions, and we don't
6551 try to simplify constexpressions. */
6554 fold_simple (tree t
)
6556 if (processing_template_decl
)
6559 tree r
= fold_simple_1 (t
);
6566 /* If T is a constant expression, returns its reduced value.
6567 Otherwise, if T does not have TREE_CONSTANT set, returns T.
6568 Otherwise, returns a version of T without TREE_CONSTANT.
6569 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated
6572 static GTY((deletable
)) hash_map
<tree
, tree
> *cv_cache
;
6575 maybe_constant_value (tree t
, tree decl
, bool manifestly_const_eval
)
6579 if (!is_nondependent_constant_expression (t
))
6581 if (TREE_OVERFLOW_P (t
))
6583 t
= build_nop (TREE_TYPE (t
), t
);
6584 TREE_CONSTANT (t
) = false;
6588 else if (CONSTANT_CLASS_P (t
))
6589 /* No caching or evaluation needed. */
6592 if (manifestly_const_eval
)
6593 return cxx_eval_outermost_constant_expr (t
, true, true, true, false, decl
);
6595 if (cv_cache
== NULL
)
6596 cv_cache
= hash_map
<tree
, tree
>::create_ggc (101);
6597 if (tree
*cached
= cv_cache
->get (t
))
6600 r
= cxx_eval_outermost_constant_expr (t
, true, true, false, false, decl
);
6601 gcc_checking_assert (r
== t
6602 || CONVERT_EXPR_P (t
)
6603 || TREE_CODE (t
) == VIEW_CONVERT_EXPR
6604 || (TREE_CONSTANT (t
) && !TREE_CONSTANT (r
))
6605 || !cp_tree_equal (r
, t
));
6606 cv_cache
->put (t
, r
);
6610 /* Dispose of the whole CV_CACHE. */
6613 clear_cv_cache (void)
6615 if (cv_cache
!= NULL
)
6619 /* Dispose of the whole CV_CACHE, FOLD_CACHE, and satisfaction caches. */
6622 clear_cv_and_fold_caches (bool sat
/*= true*/)
6625 clear_fold_cache ();
6627 clear_satisfaction_cache ();
6630 /* Internal function handling expressions in templates for
6631 fold_non_dependent_expr and fold_non_dependent_init.
6633 If we're in a template, but T isn't value dependent, simplify
6634 it. We're supposed to treat:
6636 template <typename T> void f(T[1 + 1]);
6637 template <typename T> void f(T[2]);
6639 as two declarations of the same function, for example. */
6642 fold_non_dependent_expr_template (tree t
, tsubst_flags_t complain
,
6643 bool manifestly_const_eval
,
6646 gcc_assert (processing_template_decl
);
6648 if (is_nondependent_constant_expression (t
))
6650 processing_template_decl_sentinel s
;
6651 t
= instantiate_non_dependent_expr_internal (t
, complain
);
6653 if (type_unknown_p (t
) || BRACE_ENCLOSED_INITIALIZER_P (t
))
6655 if (TREE_OVERFLOW_P (t
))
6657 t
= build_nop (TREE_TYPE (t
), t
);
6658 TREE_CONSTANT (t
) = false;
6663 tree r
= cxx_eval_outermost_constant_expr (t
, true, true,
6664 manifestly_const_eval
,
6666 /* cp_tree_equal looks through NOPs, so allow them. */
6667 gcc_checking_assert (r
== t
6668 || CONVERT_EXPR_P (t
)
6669 || TREE_CODE (t
) == VIEW_CONVERT_EXPR
6670 || (TREE_CONSTANT (t
) && !TREE_CONSTANT (r
))
6671 || !cp_tree_equal (r
, t
));
6674 else if (TREE_OVERFLOW_P (t
))
6676 t
= build_nop (TREE_TYPE (t
), t
);
6677 TREE_CONSTANT (t
) = false;
6683 /* Like maybe_constant_value but first fully instantiate the argument.
6685 Note: this is equivalent to instantiate_non_dependent_expr_sfinae
6686 (t, complain) followed by maybe_constant_value but is more efficient,
6687 because it calls instantiation_dependent_expression_p and
6688 potential_constant_expression at most once.
6689 The manifestly_const_eval argument is passed to maybe_constant_value.
6691 Callers should generally pass their active complain, or if they are in a
6692 non-template, diagnosing context, they can use the default of
6693 tf_warning_or_error. Callers that might be within a template context, don't
6694 have a complain parameter, and aren't going to remember the result for long
6695 (e.g. null_ptr_cst_p), can pass tf_none and deal with error_mark_node
6699 fold_non_dependent_expr (tree t
,
6700 tsubst_flags_t complain
/* = tf_warning_or_error */,
6701 bool manifestly_const_eval
/* = false */,
6702 tree object
/* = NULL_TREE */)
6707 if (processing_template_decl
)
6708 return fold_non_dependent_expr_template (t
, complain
,
6709 manifestly_const_eval
, object
);
6711 return maybe_constant_value (t
, object
, manifestly_const_eval
);
6715 /* Like maybe_constant_init but first fully instantiate the argument. */
6718 fold_non_dependent_init (tree t
,
6719 tsubst_flags_t complain
/*=tf_warning_or_error*/,
6720 bool manifestly_const_eval
/*=false*/)
6725 if (processing_template_decl
)
6727 t
= fold_non_dependent_expr_template (t
, complain
,
6728 manifestly_const_eval
, NULL_TREE
);
6729 /* maybe_constant_init does this stripping, so do it here too. */
6730 if (TREE_CODE (t
) == TARGET_EXPR
)
6732 tree init
= TARGET_EXPR_INITIAL (t
);
6733 if (TREE_CODE (init
) == CONSTRUCTOR
)
6739 return maybe_constant_init (t
, NULL_TREE
, manifestly_const_eval
);
6742 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
6743 than wrapped in a TARGET_EXPR.
6744 ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
6745 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
6746 per P0595 even when ALLOW_NON_CONSTANT is true. */
6749 maybe_constant_init_1 (tree t
, tree decl
, bool allow_non_constant
,
6750 bool manifestly_const_eval
)
6754 if (TREE_CODE (t
) == EXPR_STMT
)
6755 t
= TREE_OPERAND (t
, 0);
6756 if (TREE_CODE (t
) == CONVERT_EXPR
6757 && VOID_TYPE_P (TREE_TYPE (t
)))
6758 t
= TREE_OPERAND (t
, 0);
6759 if (TREE_CODE (t
) == INIT_EXPR
)
6760 t
= TREE_OPERAND (t
, 1);
6761 if (TREE_CODE (t
) == TARGET_EXPR
)
6762 t
= TARGET_EXPR_INITIAL (t
);
6763 if (!is_nondependent_static_init_expression (t
))
6764 /* Don't try to evaluate it. */;
6765 else if (CONSTANT_CLASS_P (t
) && allow_non_constant
)
6766 /* No evaluation needed. */;
6768 t
= cxx_eval_outermost_constant_expr (t
, allow_non_constant
,
6770 manifestly_const_eval
, false, decl
);
6771 if (TREE_CODE (t
) == TARGET_EXPR
)
6773 tree init
= TARGET_EXPR_INITIAL (t
);
6774 if (TREE_CODE (init
) == CONSTRUCTOR
)
6780 /* Wrapper for maybe_constant_init_1 which permits non constants. */
6783 maybe_constant_init (tree t
, tree decl
, bool manifestly_const_eval
)
6785 return maybe_constant_init_1 (t
, decl
, true, manifestly_const_eval
);
6788 /* Wrapper for maybe_constant_init_1 which does not permit non constants. */
6791 cxx_constant_init (tree t
, tree decl
)
6793 return maybe_constant_init_1 (t
, decl
, false, true);
6797 /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
6798 /* Return true if the object referred to by REF has automatic or thread
6801 enum { ck_ok
, ck_bad
, ck_unknown
};
6803 check_automatic_or_tls (tree ref
)
6806 poly_int64 bitsize
, bitpos
;
6808 int volatilep
= 0, unsignedp
= 0;
6809 tree decl
= get_inner_reference (ref
, &bitsize
, &bitpos
, &offset
,
6810 &mode
, &unsignedp
, &volatilep
, false);
6813 /* If there isn't a decl in the middle, we don't know the linkage here,
6814 and this isn't a constant expression anyway. */
6817 dk
= decl_storage_duration (decl
);
6818 return (dk
== dk_auto
|| dk
== dk_thread
) ? ck_bad
: ck_ok
;
6822 /* Data structure for passing data from potential_constant_expression_1
6823 to check_for_return_continue via cp_walk_tree. */
6824 struct check_for_return_continue_data
{
6825 hash_set
<tree
> *pset
;
6829 /* Helper function for potential_constant_expression_1 SWITCH_STMT handling,
6830 called through cp_walk_tree. Return the first RETURN_EXPR found, or note
6831 the first CONTINUE_STMT if RETURN_EXPR is not found. */
6833 check_for_return_continue (tree
*tp
, int *walk_subtrees
, void *data
)
6836 check_for_return_continue_data
*d
= (check_for_return_continue_data
*) data
;
6837 switch (TREE_CODE (t
))
6843 if (d
->continue_stmt
== NULL_TREE
)
6844 d
->continue_stmt
= t
;
6848 if (tree r = cp_walk_tree (&x, check_for_return_continue, data, \
6852 /* For loops, walk subtrees manually, so that continue stmts found
6853 inside of the bodies of the loops are ignored. */
6856 RECUR (DO_COND (t
));
6857 s
= d
->continue_stmt
;
6858 RECUR (DO_BODY (t
));
6859 d
->continue_stmt
= s
;
6864 RECUR (WHILE_COND (t
));
6865 s
= d
->continue_stmt
;
6866 RECUR (WHILE_BODY (t
));
6867 d
->continue_stmt
= s
;
6872 RECUR (FOR_INIT_STMT (t
));
6873 RECUR (FOR_COND (t
));
6874 RECUR (FOR_EXPR (t
));
6875 s
= d
->continue_stmt
;
6876 RECUR (FOR_BODY (t
));
6877 d
->continue_stmt
= s
;
6880 case RANGE_FOR_STMT
:
6882 RECUR (RANGE_FOR_EXPR (t
));
6883 s
= d
->continue_stmt
;
6884 RECUR (RANGE_FOR_BODY (t
));
6885 d
->continue_stmt
= s
;
6889 case STATEMENT_LIST
:
6902 /* Return true if T denotes a potentially constant expression. Issue
6903 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
6904 an lvalue-rvalue conversion is implied. If NOW is true, we want to
6905 consider the expression in the current context, independent of constexpr
6908 C++0x [expr.const] used to say
6910 6 An expression is a potential constant expression if it is
6911 a constant expression where all occurrences of function
6912 parameters are replaced by arbitrary constant expressions
6913 of the appropriate type.
6915 2 A conditional expression is a constant expression unless it
6916 involves one of the following as a potentially evaluated
6917 subexpression (3.2), but subexpressions of logical AND (5.14),
6918 logical OR (5.15), and conditional (5.16) operations that are
6919 not evaluated are not considered. */
6922 potential_constant_expression_1 (tree t
, bool want_rval
, bool strict
, bool now
,
6923 tsubst_flags_t flags
, tree
*jump_target
)
6925 #define RECUR(T,RV) \
6926 potential_constant_expression_1 ((T), (RV), strict, now, flags, jump_target)
6928 enum { any
= false, rval
= true };
6932 if (t
== error_mark_node
)
6936 location_t loc
= cp_expr_loc_or_input_loc (t
);
6939 /* If we are jumping, ignore everything. This is simpler than the
6940 cxx_eval_constant_expression handling because we only need to be
6941 conservatively correct, and we don't necessarily have a constant value
6942 available, so we don't bother with switch tracking. */
6945 if (TREE_THIS_VOLATILE (t
) && want_rval
)
6947 if (flags
& tf_error
)
6948 error_at (loc
, "lvalue-to-rvalue conversion of a volatile lvalue "
6949 "%qE with type %qT", t
, TREE_TYPE (t
));
6952 if (CONSTANT_CLASS_P (t
))
6954 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_TYPED
)
6955 && TREE_TYPE (t
) == error_mark_node
)
6958 switch (TREE_CODE (t
))
6964 case TEMPLATE_ID_EXPR
:
6967 case CASE_LABEL_EXPR
:
6974 case TEMPLATE_PARM_INDEX
:
6976 case IDENTIFIER_NODE
:
6977 case USERDEF_LITERAL
:
6978 /* We can see a FIELD_DECL in a pointer-to-member expression. */
6983 case PLACEHOLDER_EXPR
:
6986 case DEBUG_BEGIN_STMT
:
6990 if (!RECUR (TREE_OPERAND (t
, 0), any
))
7002 if (flags
& tf_error
)
7003 error ("%qE is not a constant expression", t
);
7008 case AGGR_INIT_EXPR
:
7010 /* -- an invocation of a function other than a constexpr function
7011 or a constexpr constructor. */
7013 tree fun
= get_function_named_in_call (t
);
7014 const int nargs
= call_expr_nargs (t
);
7017 if (fun
== NULL_TREE
)
7019 /* Reset to allow the function to continue past the end
7020 of the block below. Otherwise return early. */
7023 if (TREE_CODE (t
) == CALL_EXPR
7024 && CALL_EXPR_FN (t
) == NULL_TREE
)
7025 switch (CALL_EXPR_IFN (t
))
7027 /* These should be ignored, they are optimized away from
7028 constexpr functions. */
7029 case IFN_UBSAN_NULL
:
7030 case IFN_UBSAN_BOUNDS
:
7031 case IFN_UBSAN_VPTR
:
7032 case IFN_FALLTHROUGH
:
7035 case IFN_ADD_OVERFLOW
:
7036 case IFN_SUB_OVERFLOW
:
7037 case IFN_MUL_OVERFLOW
:
7039 case IFN_VEC_CONVERT
:
7049 /* fold_call_expr can't do anything with IFN calls. */
7050 if (flags
& tf_error
)
7051 error_at (loc
, "call to internal function %qE", t
);
7056 if (fun
&& is_overloaded_fn (fun
))
7058 if (TREE_CODE (fun
) == FUNCTION_DECL
)
7060 if (builtin_valid_in_constant_expr_p (fun
))
7062 if (!DECL_DECLARED_CONSTEXPR_P (fun
)
7063 /* Allow any built-in function; if the expansion
7064 isn't constant, we'll deal with that then. */
7065 && !fndecl_built_in_p (fun
)
7066 /* In C++2a, replaceable global allocation functions
7067 are constant expressions. */
7068 && (!cxx_replaceable_global_alloc_fn (fun
)
7069 || TREE_CODE (t
) != CALL_EXPR
7070 || (!CALL_FROM_NEW_OR_DELETE_P (t
)
7071 && (current_function_decl
== NULL_TREE
7072 || !is_std_allocator_allocate
7073 (current_function_decl
))))
7074 /* Allow placement new in std::construct_at. */
7075 && (!cxx_placement_new_fn (fun
)
7076 || TREE_CODE (t
) != CALL_EXPR
7077 || current_function_decl
== NULL_TREE
7078 || !is_std_construct_at (current_function_decl
))
7079 && !cxx_dynamic_cast_fn_p (fun
))
7081 if (flags
& tf_error
)
7083 error_at (loc
, "call to non-%<constexpr%> function %qD",
7085 explain_invalid_constexpr_fn (fun
);
7089 /* A call to a non-static member function takes the address
7090 of the object as the first argument. But in a constant
7091 expression the address will be folded away, so look
7093 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun
)
7094 && !DECL_CONSTRUCTOR_P (fun
))
7096 tree x
= get_nth_callarg (t
, 0);
7097 if (is_this_parameter (x
))
7099 /* Don't require an immediately constant value, as
7100 constexpr substitution might not use the value. */
7101 bool sub_now
= false;
7102 if (!potential_constant_expression_1 (x
, rval
, strict
,
7111 if (!RECUR (fun
, true))
7113 fun
= get_first_fn (fun
);
7115 /* Skip initial arguments to base constructors. */
7116 if (DECL_BASE_CONSTRUCTOR_P (fun
))
7117 i
= num_artificial_parms_for (fun
);
7118 fun
= DECL_ORIGIN (fun
);
7122 if (RECUR (fun
, rval
))
7123 /* Might end up being a constant function pointer. */;
7127 for (; i
< nargs
; ++i
)
7129 tree x
= get_nth_callarg (t
, i
);
7130 /* In a template, reference arguments haven't been converted to
7131 REFERENCE_TYPE and we might not even know if the parameter
7132 is a reference, so accept lvalue constants too. */
7133 bool rv
= processing_template_decl
? any
: rval
;
7134 /* Don't require an immediately constant value, as constexpr
7135 substitution might not use the value of the argument. */
7136 bool sub_now
= false;
7137 if (!potential_constant_expression_1 (x
, rv
, strict
,
7138 sub_now
, flags
, jump_target
))
7144 case NON_LVALUE_EXPR
:
7145 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
7146 -- an lvalue of integral type that refers to a non-volatile
7147 const variable or static data member initialized with
7148 constant expressions, or
7150 -- an lvalue of literal type that refers to non-volatile
7151 object defined with constexpr, or that refers to a
7152 sub-object of such an object; */
7153 return RECUR (TREE_OPERAND (t
, 0), rval
);
7156 if (DECL_HAS_VALUE_EXPR_P (t
))
7158 if (now
&& is_normal_capture_proxy (t
))
7160 /* -- in a lambda-expression, a reference to this or to a
7161 variable with automatic storage duration defined outside that
7162 lambda-expression, where the reference would be an
7164 if (flags
& tf_error
)
7166 tree cap
= DECL_CAPTURED_VARIABLE (t
);
7167 error ("lambda capture of %qE is not a constant expression",
7169 if (!want_rval
&& decl_constant_var_p (cap
))
7170 inform (input_location
, "because it is used as a glvalue");
7174 return RECUR (DECL_VALUE_EXPR (t
), rval
);
7177 && !var_in_maybe_constexpr_fn (t
)
7178 && !type_dependent_expression_p (t
)
7179 && !decl_maybe_constant_var_p (t
)
7181 || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t
))
7182 || (DECL_INITIAL (t
)
7183 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t
)))
7184 && COMPLETE_TYPE_P (TREE_TYPE (t
))
7185 && !is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
7187 if (flags
& tf_error
)
7188 non_const_var_error (loc
, t
);
7194 if (REINTERPRET_CAST_P (t
))
7196 if (flags
& tf_error
)
7197 error_at (loc
, "%<reinterpret_cast%> is not a constant expression");
7202 case VIEW_CONVERT_EXPR
:
7203 /* -- a reinterpret_cast. FIXME not implemented, and this rule
7204 may change to something more specific to type-punning (DR 1312). */
7206 tree from
= TREE_OPERAND (t
, 0);
7207 if (location_wrapper_p (t
))
7208 return (RECUR (from
, want_rval
));
7209 if (INDIRECT_TYPE_P (TREE_TYPE (t
)))
7211 STRIP_ANY_LOCATION_WRAPPER (from
);
7212 if (TREE_CODE (from
) == INTEGER_CST
7213 && !integer_zerop (from
))
7215 if (flags
& tf_error
)
7217 "%<reinterpret_cast%> from integer to pointer");
7221 return (RECUR (from
, TREE_CODE (t
) != VIEW_CONVERT_EXPR
));
7224 case ADDRESSOF_EXPR
:
7225 /* This is like ADDR_EXPR, except it won't form pointer-to-member. */
7226 t
= TREE_OPERAND (t
, 0);
7227 goto handle_addr_expr
;
7230 /* -- a unary operator & that is applied to an lvalue that
7231 designates an object with thread or automatic storage
7233 t
= TREE_OPERAND (t
, 0);
7235 if (TREE_CODE (t
) == OFFSET_REF
&& PTRMEM_OK_P (t
))
7236 /* A pointer-to-member constant. */
7241 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
7242 any checking here, as we might dereference the pointer later. If
7243 we remove this code, also remove check_automatic_or_tls. */
7244 i
= check_automatic_or_tls (t
);
7249 if (flags
& tf_error
)
7250 error ("address-of an object %qE with thread local or "
7251 "automatic storage is not a constant expression", t
);
7255 return RECUR (t
, any
);
7263 /* -- a class member access unless its postfix-expression is
7264 of literal type or of pointer to literal type. */
7265 /* This test would be redundant, as it follows from the
7266 postfix-expression being a potential constant expression. */
7267 if (type_unknown_p (t
))
7269 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
7271 case EXPR_PACK_EXPANSION
:
7272 return RECUR (PACK_EXPANSION_PATTERN (t
), want_rval
);
7276 tree x
= TREE_OPERAND (t
, 0);
7278 if (is_this_parameter (x
) && !is_capture_proxy (x
))
7280 if (!var_in_maybe_constexpr_fn (x
))
7282 if (flags
& tf_error
)
7283 error_at (loc
, "use of %<this%> in a constant expression");
7288 return RECUR (x
, rval
);
7291 case STATEMENT_LIST
:
7293 tree_stmt_iterator i
;
7294 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
7296 if (!RECUR (tsi_stmt (i
), any
))
7304 if (cxx_dialect
< cxx14
)
7306 if (!RECUR (TREE_OPERAND (t
, 0), any
))
7308 /* Just ignore clobbers. */
7309 if (TREE_CLOBBER_P (TREE_OPERAND (t
, 1)))
7311 if (!RECUR (TREE_OPERAND (t
, 1), rval
))
7316 if (cxx_dialect
< cxx14
)
7318 if (!RECUR (TREE_OPERAND (t
, 0), rval
))
7320 if (!RECUR (TREE_OPERAND (t
, 2), rval
))
7325 if (!RECUR (DO_COND (t
), rval
))
7327 if (!RECUR (DO_BODY (t
), any
))
7329 if (breaks (jump_target
) || continues (jump_target
))
7330 *jump_target
= NULL_TREE
;
7334 if (!RECUR (FOR_INIT_STMT (t
), any
))
7337 if (!RECUR (tmp
, rval
))
7341 if (!processing_template_decl
)
7342 tmp
= cxx_eval_outermost_constant_expr (tmp
, true);
7343 /* If we couldn't evaluate the condition, it might not ever be
7345 if (!integer_onep (tmp
))
7348 if (!RECUR (FOR_EXPR (t
), any
))
7350 if (!RECUR (FOR_BODY (t
), any
))
7352 if (breaks (jump_target
) || continues (jump_target
))
7353 *jump_target
= NULL_TREE
;
7356 case RANGE_FOR_STMT
:
7357 if (!RECUR (RANGE_FOR_INIT_STMT (t
), any
))
7359 if (!RECUR (RANGE_FOR_EXPR (t
), any
))
7361 if (!RECUR (RANGE_FOR_BODY (t
), any
))
7363 if (breaks (jump_target
) || continues (jump_target
))
7364 *jump_target
= NULL_TREE
;
7368 tmp
= WHILE_COND (t
);
7369 if (!RECUR (tmp
, rval
))
7371 if (!processing_template_decl
)
7372 tmp
= cxx_eval_outermost_constant_expr (tmp
, true);
7373 /* If we couldn't evaluate the condition, it might not ever be true. */
7374 if (!integer_onep (tmp
))
7376 if (!RECUR (WHILE_BODY (t
), any
))
7378 if (breaks (jump_target
) || continues (jump_target
))
7379 *jump_target
= NULL_TREE
;
7383 if (!RECUR (SWITCH_STMT_COND (t
), rval
))
7385 /* FIXME we don't check SWITCH_STMT_BODY currently, because even
7386 unreachable labels would be checked and it is enough if there is
7387 a single switch cond value for which it is a valid constant
7388 expression. We need to check if there are any RETURN_EXPRs
7389 or CONTINUE_STMTs inside of the body though, as in that case
7390 we need to set *jump_target. */
7393 hash_set
<tree
> pset
;
7394 check_for_return_continue_data data
= { &pset
, NULL_TREE
};
7396 = cp_walk_tree (&SWITCH_STMT_BODY (t
), check_for_return_continue
,
7398 /* The switch might return. */
7399 *jump_target
= ret_expr
;
7400 else if (data
.continue_stmt
)
7401 /* The switch can't return, but might continue. */
7402 *jump_target
= data
.continue_stmt
;
7407 return RECUR (STMT_EXPR_STMT (t
), rval
);
7410 if (cxx_dialect
>= cxx17
)
7411 /* In C++17 lambdas can be constexpr, don't give up yet. */
7413 else if (flags
& tf_error
)
7414 error_at (loc
, "lambda-expression is not a constant expression "
7418 case DYNAMIC_CAST_EXPR
:
7419 case PSEUDO_DTOR_EXPR
:
7423 case VEC_DELETE_EXPR
:
7429 case OMP_DISTRIBUTE
:
7433 case OMP_TARGET_DATA
:
7442 case OMP_TARGET_UPDATE
:
7443 case OMP_TARGET_ENTER_DATA
:
7444 case OMP_TARGET_EXIT_DATA
:
7446 case OMP_ATOMIC_READ
:
7447 case OMP_ATOMIC_CAPTURE_OLD
:
7448 case OMP_ATOMIC_CAPTURE_NEW
:
7454 case OACC_HOST_DATA
:
7458 case OACC_ENTER_DATA
:
7459 case OACC_EXIT_DATA
:
7461 /* GCC internal stuff. */
7463 case TRANSACTION_EXPR
:
7464 case AT_ENCODE_EXPR
:
7466 if (flags
& tf_error
)
7467 error_at (loc
, "expression %qE is not a constant expression", t
);
7471 if (flags
& tf_error
)
7472 inline_asm_in_constexpr_error (loc
);
7476 if (cxx_dialect
>= cxx2a
)
7477 /* In C++2a virtual calls can be constexpr, don't give up yet. */
7479 else if (flags
& tf_error
)
7481 "virtual functions cannot be %<constexpr%> before C++2a");
7485 /* In C++20, a typeid expression whose operand is of polymorphic
7486 class type can be constexpr. */
7488 tree e
= TREE_OPERAND (t
, 0);
7489 if (cxx_dialect
< cxx2a
7492 && !type_dependent_expression_p (e
)
7493 && TYPE_POLYMORPHIC_P (TREE_TYPE (e
)))
7495 if (flags
& tf_error
)
7496 error_at (loc
, "%<typeid%> is not a constant expression "
7497 "because %qE is of polymorphic type", e
);
7503 case POINTER_DIFF_EXPR
:
7514 case SPACESHIP_EXPR
:
7518 case PREINCREMENT_EXPR
:
7519 case POSTINCREMENT_EXPR
:
7520 case PREDECREMENT_EXPR
:
7521 case POSTDECREMENT_EXPR
:
7522 if (cxx_dialect
< cxx14
)
7528 if (TYPE_P (TREE_OPERAND (t
, 0)))
7534 case FIX_TRUNC_EXPR
:
7539 case TRUTH_NOT_EXPR
:
7540 case FIXED_CONVERT_EXPR
:
7541 case UNARY_PLUS_EXPR
:
7542 case UNARY_LEFT_FOLD_EXPR
:
7543 case UNARY_RIGHT_FOLD_EXPR
:
7545 return RECUR (TREE_OPERAND (t
, 0), rval
);
7548 case CONST_CAST_EXPR
:
7549 case STATIC_CAST_EXPR
:
7550 case REINTERPRET_CAST_EXPR
:
7551 case IMPLICIT_CONV_EXPR
:
7552 if (cxx_dialect
< cxx11
7553 && !dependent_type_p (TREE_TYPE (t
))
7554 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
)))
7555 /* In C++98, a conversion to non-integral type can't be part of a
7556 constant expression. */
7558 if (flags
& tf_error
)
7560 "cast to non-integral type %qT in a constant expression",
7564 /* This might be a conversion from a class to a (potentially) literal
7565 type. Let's consider it potentially constant since the conversion
7566 might be a constexpr user-defined conversion. */
7567 else if (cxx_dialect
>= cxx11
7568 && (dependent_type_p (TREE_TYPE (t
))
7569 || !COMPLETE_TYPE_P (TREE_TYPE (t
))
7570 || literal_type_p (TREE_TYPE (t
)))
7571 && TREE_OPERAND (t
, 0))
7573 tree type
= TREE_TYPE (TREE_OPERAND (t
, 0));
7574 /* If this is a dependent type, it could end up being a class
7575 with conversions. */
7576 if (type
== NULL_TREE
|| WILDCARD_TYPE_P (type
))
7578 /* Or a non-dependent class which has conversions. */
7579 else if (CLASS_TYPE_P (type
)
7580 && (TYPE_HAS_CONVERSION (type
) || dependent_scope_p (type
)))
7584 return (RECUR (TREE_OPERAND (t
, 0),
7585 !TYPE_REF_P (TREE_TYPE (t
))));
7588 return RECUR (BIND_EXPR_BODY (t
), want_rval
);
7590 case CLEANUP_POINT_EXPR
:
7591 case MUST_NOT_THROW_EXPR
:
7592 case TRY_CATCH_EXPR
:
7597 case NON_DEPENDENT_EXPR
:
7598 /* For convenience. */
7601 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
7604 tmp
= DECL_EXPR_DECL (t
);
7605 if (VAR_P (tmp
) && !DECL_ARTIFICIAL (tmp
))
7607 if (TREE_STATIC (tmp
))
7609 if (flags
& tf_error
)
7610 error_at (DECL_SOURCE_LOCATION (tmp
), "%qD declared "
7611 "%<static%> in %<constexpr%> context", tmp
);
7614 else if (CP_DECL_THREAD_LOCAL_P (tmp
))
7616 if (flags
& tf_error
)
7617 error_at (DECL_SOURCE_LOCATION (tmp
), "%qD declared "
7618 "%<thread_local%> in %<constexpr%> context", tmp
);
7621 else if (!check_for_uninitialized_const_var
7622 (tmp
, /*constexpr_context_p=*/true, flags
))
7625 return RECUR (tmp
, want_rval
);
7627 case TRY_FINALLY_EXPR
:
7628 return (RECUR (TREE_OPERAND (t
, 0), want_rval
)
7629 && RECUR (TREE_OPERAND (t
, 1), any
));
7632 return RECUR (TREE_OPERAND (t
, 1), want_rval
);
7635 if (!TARGET_EXPR_DIRECT_INIT_P (t
)
7636 && !literal_type_p (TREE_TYPE (t
)))
7638 if (flags
& tf_error
)
7640 auto_diagnostic_group d
;
7641 error_at (loc
, "temporary of non-literal type %qT in a "
7642 "constant expression", TREE_TYPE (t
));
7643 explain_non_literal_class (TREE_TYPE (t
));
7649 return RECUR (TREE_OPERAND (t
, 1), rval
);
7653 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (t
);
7654 constructor_elt
*ce
;
7655 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
7656 if (!RECUR (ce
->value
, want_rval
))
7663 gcc_assert (TREE_PURPOSE (t
) == NULL_TREE
7664 || DECL_P (TREE_PURPOSE (t
)));
7665 if (!RECUR (TREE_VALUE (t
), want_rval
))
7667 if (TREE_CHAIN (t
) == NULL_TREE
)
7669 return RECUR (TREE_CHAIN (t
), want_rval
);
7672 case TRUNC_DIV_EXPR
:
7674 case FLOOR_DIV_EXPR
:
7675 case ROUND_DIV_EXPR
:
7676 case TRUNC_MOD_EXPR
:
7678 case ROUND_MOD_EXPR
:
7680 tree denom
= TREE_OPERAND (t
, 1);
7681 if (!RECUR (denom
, rval
))
7683 /* We can't call cxx_eval_outermost_constant_expr on an expression
7684 that hasn't been through instantiate_non_dependent_expr yet. */
7685 if (!processing_template_decl
)
7686 denom
= cxx_eval_outermost_constant_expr (denom
, true);
7687 if (integer_zerop (denom
))
7689 if (flags
& tf_error
)
7690 error ("division by zero is not a constant expression");
7696 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
7702 /* check_return_expr sometimes wraps a TARGET_EXPR in a
7703 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
7704 introduced by build_call_a. */
7705 tree op0
= TREE_OPERAND (t
, 0);
7706 tree op1
= TREE_OPERAND (t
, 1);
7708 if ((TREE_CODE (op0
) == TARGET_EXPR
&& op1
== TARGET_EXPR_SLOT (op0
))
7709 || TREE_CODE (op1
) == EMPTY_CLASS_EXPR
)
7710 return RECUR (op0
, want_rval
);
7715 /* If the first operand is the non-short-circuit constant, look at
7716 the second operand; otherwise we only care about the first one for
7718 case TRUTH_AND_EXPR
:
7719 case TRUTH_ANDIF_EXPR
:
7720 tmp
= boolean_true_node
;
7723 case TRUTH_ORIF_EXPR
:
7724 tmp
= boolean_false_node
;
7727 tree op
= TREE_OPERAND (t
, 0);
7728 if (!RECUR (op
, rval
))
7730 if (!processing_template_decl
)
7731 op
= cxx_eval_outermost_constant_expr (op
, true);
7732 if (tree_int_cst_equal (op
, tmp
))
7733 return RECUR (TREE_OPERAND (t
, 1), rval
);
7740 case POINTER_PLUS_EXPR
:
7742 case EXACT_DIV_EXPR
:
7750 case TRUTH_XOR_EXPR
:
7751 case UNORDERED_EXPR
:
7764 case ARRAY_RANGE_REF
:
7768 case BINARY_LEFT_FOLD_EXPR
:
7769 case BINARY_RIGHT_FOLD_EXPR
:
7771 for (i
= 0; i
< 2; ++i
)
7772 if (!RECUR (TREE_OPERAND (t
, i
), want_rval
))
7777 for (i
= 0; i
< 3; ++i
)
7778 if (!RECUR (TREE_OPERAND (t
, i
), true))
7783 if (COND_EXPR_IS_VEC_DELETE (t
) && cxx_dialect
< cxx2a
)
7785 if (flags
& tf_error
)
7786 error_at (loc
, "%<delete[]%> is not a constant expression");
7792 /* If the condition is a known constant, we know which of the legs we
7793 care about; otherwise we only require that the condition and
7794 either of the legs be potentially constant. */
7795 tmp
= TREE_OPERAND (t
, 0);
7796 if (!RECUR (tmp
, rval
))
7798 if (!processing_template_decl
)
7799 tmp
= cxx_eval_outermost_constant_expr (tmp
, true);
7800 if (integer_zerop (tmp
))
7801 return RECUR (TREE_OPERAND (t
, 2), want_rval
);
7802 else if (TREE_CODE (tmp
) == INTEGER_CST
)
7803 return RECUR (TREE_OPERAND (t
, 1), want_rval
);
7804 for (i
= 1; i
< 3; ++i
)
7805 if (potential_constant_expression_1 (TREE_OPERAND (t
, i
),
7806 want_rval
, strict
, now
,
7807 tf_none
, jump_target
))
7809 if (flags
& tf_error
)
7810 error_at (loc
, "expression %qE is not a constant expression", t
);
7814 if (VEC_INIT_EXPR_IS_CONSTEXPR (t
))
7816 if (flags
& tf_error
)
7818 error_at (loc
, "non-constant array initialization");
7819 diagnose_non_constexpr_vec_init (t
);
7825 /* We can see these in statement-expressions. */
7829 if (!RECUR (CLEANUP_BODY (t
), any
))
7831 if (!CLEANUP_EH_ONLY (t
) && !RECUR (CLEANUP_EXPR (t
), any
))
7835 case EMPTY_CLASS_EXPR
:
7840 tree
*target
= &TREE_OPERAND (t
, 0);
7841 /* Gotos representing break and continue are OK. */
7842 if (breaks (target
) || continues (target
))
7844 *jump_target
= *target
;
7847 if (flags
& tf_error
)
7848 error_at (loc
, "%<goto%> is not a constant expression");
7853 return RECUR (TREE_OPERAND (t
, 0), rval
);
7856 if (objc_is_property_ref (t
))
7859 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t
)));
7867 potential_constant_expression_1 (tree t
, bool want_rval
, bool strict
, bool now
,
7868 tsubst_flags_t flags
)
7870 tree target
= NULL_TREE
;
7871 return potential_constant_expression_1 (t
, want_rval
, strict
, now
,
7875 /* The main entry point to the above. */
7878 potential_constant_expression (tree t
)
7880 return potential_constant_expression_1 (t
, false, true, false, tf_none
);
7883 /* As above, but require a constant rvalue. */
7886 potential_rvalue_constant_expression (tree t
)
7888 return potential_constant_expression_1 (t
, true, true, false, tf_none
);
7891 /* Like above, but complain about non-constant expressions. */
7894 require_potential_constant_expression (tree t
)
7896 return potential_constant_expression_1 (t
, false, true, false,
7897 tf_warning_or_error
);
7900 /* Cross product of the above. */
7903 require_potential_rvalue_constant_expression (tree t
)
7905 return potential_constant_expression_1 (t
, true, true, false,
7906 tf_warning_or_error
);
7909 /* Like above, but don't consider PARM_DECL a potential_constant_expression. */
7912 require_rvalue_constant_expression (tree t
)
7914 return potential_constant_expression_1 (t
, true, true, true,
7915 tf_warning_or_error
);
7918 /* Like potential_constant_expression, but don't consider possible constexpr
7919 substitution of the current function. That is, PARM_DECL qualifies under
7920 potential_constant_expression, but not here.
7922 This is basically what you can check when any actual constant values might
7923 be value-dependent. */
7926 is_constant_expression (tree t
)
7928 return potential_constant_expression_1 (t
, false, true, true, tf_none
);
7931 /* Like above, but complain about non-constant expressions. */
7934 require_constant_expression (tree t
)
7936 return potential_constant_expression_1 (t
, false, true, true,
7937 tf_warning_or_error
);
7940 /* Like is_constant_expression, but allow const variables that are not allowed
7941 under constexpr rules. */
7944 is_static_init_expression (tree t
)
7946 return potential_constant_expression_1 (t
, false, false, true, tf_none
);
7949 /* Returns true if T is a potential constant expression that is not
7950 instantiation-dependent, and therefore a candidate for constant folding even
7954 is_nondependent_constant_expression (tree t
)
7956 return (!type_unknown_p (t
)
7957 && !BRACE_ENCLOSED_INITIALIZER_P (t
)
7958 && is_constant_expression (t
)
7959 && !instantiation_dependent_expression_p (t
));
7962 /* Returns true if T is a potential static initializer expression that is not
7963 instantiation-dependent. */
7966 is_nondependent_static_init_expression (tree t
)
7968 return (!type_unknown_p (t
)
7969 && !BRACE_ENCLOSED_INITIALIZER_P (t
)
7970 && is_static_init_expression (t
)
7971 && !instantiation_dependent_expression_p (t
));
7974 /* Finalize constexpr processing after parsing. */
7977 fini_constexpr (void)
7979 /* The contexpr call and fundef copies tables are no longer needed. */
7980 constexpr_call_table
= NULL
;
7981 fundef_copies_table
= NULL
;
7984 #include "gt-cp-constexpr.h"