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
;
1045 constexpr_global_ctx () : constexpr_ops_count (0), cleanups (NULL
) {}
1048 /* The constexpr expansion context. CALL is the current function
1049 expansion, CTOR is the current aggregate initializer, OBJECT is the
1050 object being initialized by CTOR, either a VAR_DECL or a _REF. */
1052 struct constexpr_ctx
{
1053 /* The part of the context that needs to be unique to the whole
1054 cxx_eval_outermost_constant_expr invocation. */
1055 constexpr_global_ctx
*global
;
1056 /* The innermost call we're evaluating. */
1057 constexpr_call
*call
;
1058 /* SAVE_EXPRs and TARGET_EXPR_SLOT vars of TARGET_EXPRs that we've seen
1059 within the current LOOP_EXPR. NULL if we aren't inside a loop. */
1060 vec
<tree
> *save_exprs
;
1061 /* The CONSTRUCTOR we're currently building up for an aggregate
1064 /* The object we're building the CONSTRUCTOR for. */
1066 /* If inside SWITCH_EXPR. */
1067 constexpr_switch_state
*css_state
;
1069 /* Whether we should error on a non-constant expression or fail quietly. */
1071 /* Whether we are strictly conforming to constant expression rules or
1072 trying harder to get a constant value. */
1074 /* Whether __builtin_is_constant_evaluated () should be true. */
1075 bool manifestly_const_eval
;
1078 /* A table of all constexpr calls that have been evaluated by the
1079 compiler in this translation unit. */
1081 static GTY (()) hash_table
<constexpr_call_hasher
> *constexpr_call_table
;
1083 static tree
cxx_eval_constant_expression (const constexpr_ctx
*, tree
,
1084 bool, bool *, bool *, tree
* = NULL
);
1086 /* Compute a hash value for a constexpr call representation. */
1089 constexpr_call_hasher::hash (constexpr_call
*info
)
1094 /* Return true if the objects pointed to by P and Q represent calls
1095 to the same constexpr function with the same arguments.
1096 Otherwise, return false. */
1099 constexpr_call_hasher::equal (constexpr_call
*lhs
, constexpr_call
*rhs
)
1103 if (lhs
->hash
!= rhs
->hash
)
1105 if (lhs
->manifestly_const_eval
!= rhs
->manifestly_const_eval
)
1107 if (!constexpr_fundef_hasher::equal (lhs
->fundef
, rhs
->fundef
))
1109 return cp_tree_equal (lhs
->bindings
, rhs
->bindings
);
1112 /* Initialize the constexpr call table, if needed. */
1115 maybe_initialize_constexpr_call_table (void)
1117 if (constexpr_call_table
== NULL
)
1118 constexpr_call_table
= hash_table
<constexpr_call_hasher
>::create_ggc (101);
1121 /* During constexpr CALL_EXPR evaluation, to avoid issues with sharing when
1122 a function happens to get called recursively, we unshare the callee
1123 function's body and evaluate this unshared copy instead of evaluating the
1126 FUNDEF_COPIES_TABLE is a per-function freelist of these unshared function
1127 copies. The underlying data structure of FUNDEF_COPIES_TABLE is a hash_map
1128 that's keyed off of the original FUNCTION_DECL and whose value is a
1129 TREE_LIST of this function's unused copies awaiting reuse.
1131 This is not GC-deletable to avoid GC affecting UID generation. */
1133 static GTY(()) hash_map
<tree
, tree
> *fundef_copies_table
;
1135 /* Reuse a copy or create a new unshared copy of the function FUN.
1136 Return this copy. We use a TREE_LIST whose PURPOSE is body, VALUE
1137 is parms, TYPE is result. */
1140 get_fundef_copy (constexpr_fundef
*fundef
)
1144 tree
*slot
= &(hash_map_safe_get_or_insert
<hm_ggc
>
1145 (fundef_copies_table
, fundef
->decl
, &existed
, 127));
1149 /* There is no cached function available, or in use. We can use
1150 the function directly. That the slot is now created records
1151 that this function is now in use. */
1152 copy
= build_tree_list (fundef
->body
, fundef
->parms
);
1153 TREE_TYPE (copy
) = fundef
->result
;
1155 else if (*slot
== NULL_TREE
)
1157 /* We've already used the function itself, so make a copy. */
1158 copy
= build_tree_list (NULL
, NULL
);
1159 tree saved_body
= DECL_SAVED_TREE (fundef
->decl
);
1160 tree saved_parms
= DECL_ARGUMENTS (fundef
->decl
);
1161 tree saved_result
= DECL_RESULT (fundef
->decl
);
1162 tree saved_fn
= current_function_decl
;
1163 DECL_SAVED_TREE (fundef
->decl
) = fundef
->body
;
1164 DECL_ARGUMENTS (fundef
->decl
) = fundef
->parms
;
1165 DECL_RESULT (fundef
->decl
) = fundef
->result
;
1166 current_function_decl
= fundef
->decl
;
1167 TREE_PURPOSE (copy
) = copy_fn (fundef
->decl
, TREE_VALUE (copy
),
1169 current_function_decl
= saved_fn
;
1170 DECL_RESULT (fundef
->decl
) = saved_result
;
1171 DECL_ARGUMENTS (fundef
->decl
) = saved_parms
;
1172 DECL_SAVED_TREE (fundef
->decl
) = saved_body
;
1176 /* We have a cached function available. */
1178 *slot
= TREE_CHAIN (copy
);
1184 /* Save the copy COPY of function FUN for later reuse by
1185 get_fundef_copy(). By construction, there will always be an entry
1189 save_fundef_copy (tree fun
, tree copy
)
1191 tree
*slot
= fundef_copies_table
->get (fun
);
1192 TREE_CHAIN (copy
) = *slot
;
1196 /* We have an expression tree T that represents a call, either CALL_EXPR
1197 or AGGR_INIT_EXPR. Return the Nth argument. */
1200 get_nth_callarg (tree t
, int n
)
1202 switch (TREE_CODE (t
))
1205 return CALL_EXPR_ARG (t
, n
);
1207 case AGGR_INIT_EXPR
:
1208 return AGGR_INIT_EXPR_ARG (t
, n
);
1216 /* Attempt to evaluate T which represents a call to a builtin function.
1217 We assume here that all builtin functions evaluate to scalar types
1218 represented by _CST nodes. */
1221 cxx_eval_builtin_function_call (const constexpr_ctx
*ctx
, tree t
, tree fun
,
1223 bool *non_constant_p
, bool *overflow_p
)
1225 const int nargs
= call_expr_nargs (t
);
1226 tree
*args
= (tree
*) alloca (nargs
* sizeof (tree
));
1230 /* Don't fold __builtin_constant_p within a constexpr function. */
1231 bool bi_const_p
= DECL_IS_BUILTIN_CONSTANT_P (fun
);
1233 /* If we aren't requiring a constant expression, defer __builtin_constant_p
1234 in a constexpr function until we have values for the parameters. */
1236 && !ctx
->manifestly_const_eval
1237 && current_function_decl
1238 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
1240 *non_constant_p
= true;
1244 /* For __builtin_is_constant_evaluated, defer it if not
1245 ctx->manifestly_const_eval, otherwise fold it to true. */
1246 if (fndecl_built_in_p (fun
, CP_BUILT_IN_IS_CONSTANT_EVALUATED
,
1249 if (!ctx
->manifestly_const_eval
)
1251 *non_constant_p
= true;
1254 return boolean_true_node
;
1257 if (fndecl_built_in_p (fun
, CP_BUILT_IN_SOURCE_LOCATION
, BUILT_IN_FRONTEND
))
1258 return fold_builtin_source_location (EXPR_LOCATION (t
));
1260 /* Be permissive for arguments to built-ins; __builtin_constant_p should
1261 return constant false for a non-constant argument. */
1262 constexpr_ctx new_ctx
= *ctx
;
1263 new_ctx
.quiet
= true;
1264 for (i
= 0; i
< nargs
; ++i
)
1266 args
[i
] = CALL_EXPR_ARG (t
, i
);
1267 /* If builtin_valid_in_constant_expr_p is true,
1268 potential_constant_expression_1 has not recursed into the arguments
1269 of the builtin, verify it here. */
1270 if (!builtin_valid_in_constant_expr_p (fun
)
1271 || potential_constant_expression (args
[i
]))
1273 bool dummy1
= false, dummy2
= false;
1274 args
[i
] = cxx_eval_constant_expression (&new_ctx
, args
[i
], false,
1279 /* For __builtin_constant_p, fold all expressions with constant values
1280 even if they aren't C++ constant-expressions. */
1281 args
[i
] = cp_fold_rvalue (args
[i
]);
1284 bool save_ffbcp
= force_folding_builtin_constant_p
;
1285 force_folding_builtin_constant_p
|= ctx
->manifestly_const_eval
;
1286 tree save_cur_fn
= current_function_decl
;
1287 /* Return name of ctx->call->fundef->decl for __builtin_FUNCTION (). */
1288 if (fndecl_built_in_p (fun
, BUILT_IN_FUNCTION
)
1290 && ctx
->call
->fundef
)
1291 current_function_decl
= ctx
->call
->fundef
->decl
;
1292 new_call
= fold_builtin_call_array (EXPR_LOCATION (t
), TREE_TYPE (t
),
1293 CALL_EXPR_FN (t
), nargs
, args
);
1294 current_function_decl
= save_cur_fn
;
1295 force_folding_builtin_constant_p
= save_ffbcp
;
1296 if (new_call
== NULL
)
1298 if (!*non_constant_p
&& !ctx
->quiet
)
1300 /* Do not allow__builtin_unreachable in constexpr function.
1301 The __builtin_unreachable call with BUILTINS_LOCATION
1302 comes from cp_maybe_instrument_return. */
1303 if (fndecl_built_in_p (fun
, BUILT_IN_UNREACHABLE
)
1304 && EXPR_LOCATION (t
) == BUILTINS_LOCATION
)
1305 error ("%<constexpr%> call flows off the end of the function");
1308 new_call
= build_call_array_loc (EXPR_LOCATION (t
), TREE_TYPE (t
),
1309 CALL_EXPR_FN (t
), nargs
, args
);
1310 error ("%q+E is not a constant expression", new_call
);
1313 *non_constant_p
= true;
1317 if (!potential_constant_expression (new_call
))
1319 if (!*non_constant_p
&& !ctx
->quiet
)
1320 error ("%q+E is not a constant expression", new_call
);
1321 *non_constant_p
= true;
1325 return cxx_eval_constant_expression (&new_ctx
, new_call
, lval
,
1326 non_constant_p
, overflow_p
);
1329 /* TEMP is the constant value of a temporary object of type TYPE. Adjust
1330 the type of the value to match. */
1333 adjust_temp_type (tree type
, tree temp
)
1335 if (same_type_p (TREE_TYPE (temp
), type
))
1337 /* Avoid wrapping an aggregate value in a NOP_EXPR. */
1338 if (TREE_CODE (temp
) == CONSTRUCTOR
)
1340 /* build_constructor wouldn't retain various CONSTRUCTOR flags. */
1341 tree t
= copy_node (temp
);
1342 TREE_TYPE (t
) = type
;
1345 if (TREE_CODE (temp
) == EMPTY_CLASS_EXPR
)
1346 return build0 (EMPTY_CLASS_EXPR
, type
);
1347 gcc_assert (scalarish_type_p (type
));
1348 /* Now we know we're dealing with a scalar, and a prvalue of non-class
1349 type is cv-unqualified. */
1350 return cp_fold_convert (cv_unqualified (type
), temp
);
1353 /* If T is a CONSTRUCTOR, return an unshared copy of T and any
1354 sub-CONSTRUCTORs. Otherwise return T.
1356 We use this whenever we initialize an object as a whole, whether it's a
1357 parameter, a local variable, or a subobject, so that subsequent
1358 modifications don't affect other places where it was used. */
1361 unshare_constructor (tree t MEM_STAT_DECL
)
1363 if (!t
|| TREE_CODE (t
) != CONSTRUCTOR
)
1365 auto_vec
<tree
*, 4> ptrs
;
1366 ptrs
.safe_push (&t
);
1367 while (!ptrs
.is_empty ())
1369 tree
*p
= ptrs
.pop ();
1370 tree n
= copy_node (*p PASS_MEM_STAT
);
1371 CONSTRUCTOR_ELTS (n
) = vec_safe_copy (CONSTRUCTOR_ELTS (*p
) PASS_MEM_STAT
);
1373 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (n
);
1374 constructor_elt
*ce
;
1375 for (HOST_WIDE_INT i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
1376 if (TREE_CODE (ce
->value
) == CONSTRUCTOR
)
1377 ptrs
.safe_push (&ce
->value
);
1382 /* If T is a CONSTRUCTOR, ggc_free T and any sub-CONSTRUCTORs. */
1385 free_constructor (tree t
)
1387 if (!t
|| TREE_CODE (t
) != CONSTRUCTOR
)
1389 releasing_vec ctors
;
1390 vec_safe_push (ctors
, t
);
1391 while (!ctors
->is_empty ())
1393 tree c
= ctors
->pop ();
1394 if (vec
<constructor_elt
, va_gc
> *elts
= CONSTRUCTOR_ELTS (c
))
1396 constructor_elt
*ce
;
1397 for (HOST_WIDE_INT i
= 0; vec_safe_iterate (elts
, i
, &ce
); ++i
)
1398 if (TREE_CODE (ce
->value
) == CONSTRUCTOR
)
1399 vec_safe_push (ctors
, ce
->value
);
1406 /* Subroutine of cxx_eval_call_expression.
1407 We are processing a call expression (either CALL_EXPR or
1408 AGGR_INIT_EXPR) in the context of CTX. Evaluate
1409 all arguments and bind their values to correspondings
1410 parameters, making up the NEW_CALL context. */
1413 cxx_bind_parameters_in_call (const constexpr_ctx
*ctx
, tree t
,
1414 constexpr_call
*new_call
,
1415 bool *non_constant_p
, bool *overflow_p
,
1416 bool *non_constant_args
)
1418 const int nargs
= call_expr_nargs (t
);
1419 tree fun
= new_call
->fundef
->decl
;
1420 tree parms
= new_call
->fundef
->parms
;
1422 /* We don't record ellipsis args below. */
1423 int nparms
= list_length (parms
);
1424 int nbinds
= nargs
< nparms
? nargs
: nparms
;
1425 tree binds
= new_call
->bindings
= make_tree_vec (nbinds
);
1426 for (i
= 0; i
< nargs
; ++i
)
1429 tree type
= parms
? TREE_TYPE (parms
) : void_type_node
;
1430 x
= get_nth_callarg (t
, i
);
1431 /* For member function, the first argument is a pointer to the implied
1432 object. For a constructor, it might still be a dummy object, in
1433 which case we get the real argument from ctx. */
1434 if (i
== 0 && DECL_CONSTRUCTOR_P (fun
)
1435 && is_dummy_object (x
))
1438 x
= build_address (x
);
1440 if (TREE_ADDRESSABLE (type
))
1441 /* Undo convert_for_arg_passing work here. */
1442 x
= convert_from_reference (x
);
1443 arg
= cxx_eval_constant_expression (ctx
, x
, /*lval=*/false,
1444 non_constant_p
, overflow_p
);
1445 /* Don't VERIFY_CONSTANT here. */
1446 if (*non_constant_p
&& ctx
->quiet
)
1448 /* Just discard ellipsis args after checking their constantitude. */
1452 if (!*non_constant_p
)
1454 /* Make sure the binding has the same type as the parm. But
1455 only for constant args. */
1456 if (!TYPE_REF_P (type
))
1457 arg
= adjust_temp_type (type
, arg
);
1458 if (!TREE_CONSTANT (arg
))
1459 *non_constant_args
= true;
1460 /* For virtual calls, adjust the this argument, so that it is
1461 the object on which the method is called, rather than
1462 one of its bases. */
1463 if (i
== 0 && DECL_VIRTUAL_P (fun
))
1467 if (TREE_CODE (addr
) == ADDR_EXPR
)
1469 tree obj
= TREE_OPERAND (addr
, 0);
1470 while (TREE_CODE (obj
) == COMPONENT_REF
1471 && DECL_FIELD_IS_BASE (TREE_OPERAND (obj
, 1))
1472 && !same_type_ignoring_top_level_qualifiers_p
1473 (TREE_TYPE (obj
), DECL_CONTEXT (fun
)))
1474 obj
= TREE_OPERAND (obj
, 0);
1475 if (obj
!= TREE_OPERAND (addr
, 0))
1476 arg
= build_fold_addr_expr_with_type (obj
,
1480 TREE_VEC_ELT (binds
, i
) = arg
;
1482 parms
= TREE_CHAIN (parms
);
1486 /* Variables and functions to manage constexpr call expansion context.
1487 These do not need to be marked for PCH or GC. */
1489 /* FIXME remember and print actual constant arguments. */
1490 static vec
<tree
> call_stack
;
1491 static int call_stack_tick
;
1492 static int last_cx_error_tick
;
1495 push_cx_call_context (tree call
)
1498 if (!EXPR_HAS_LOCATION (call
))
1499 SET_EXPR_LOCATION (call
, input_location
);
1500 call_stack
.safe_push (call
);
1501 int len
= call_stack
.length ();
1502 if (len
> max_constexpr_depth
)
1508 pop_cx_call_context (void)
1515 cx_error_context (void)
1517 vec
<tree
> r
= vNULL
;
1518 if (call_stack_tick
!= last_cx_error_tick
1519 && !call_stack
.is_empty ())
1521 last_cx_error_tick
= call_stack_tick
;
1525 /* Evaluate a call T to a GCC internal function when possible and return
1526 the evaluated result or, under the control of CTX, give an error, set
1527 NON_CONSTANT_P, and return the unevaluated call T otherwise. */
1530 cxx_eval_internal_function (const constexpr_ctx
*ctx
, tree t
,
1532 bool *non_constant_p
, bool *overflow_p
)
1534 enum tree_code opcode
= ERROR_MARK
;
1536 switch (CALL_EXPR_IFN (t
))
1538 case IFN_UBSAN_NULL
:
1539 case IFN_UBSAN_BOUNDS
:
1540 case IFN_UBSAN_VPTR
:
1541 case IFN_FALLTHROUGH
:
1544 case IFN_ADD_OVERFLOW
:
1547 case IFN_SUB_OVERFLOW
:
1548 opcode
= MINUS_EXPR
;
1550 case IFN_MUL_OVERFLOW
:
1555 return cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 0),
1556 false, non_constant_p
, overflow_p
);
1558 case IFN_VEC_CONVERT
:
1560 tree arg
= cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 0),
1561 false, non_constant_p
,
1563 if (TREE_CODE (arg
) == VECTOR_CST
)
1564 return fold_const_call (CFN_VEC_CONVERT
, TREE_TYPE (t
), arg
);
1567 *non_constant_p
= true;
1574 error_at (cp_expr_loc_or_input_loc (t
),
1575 "call to internal function %qE", t
);
1576 *non_constant_p
= true;
1580 /* Evaluate constant arguments using OPCODE and return a complex
1581 number containing the result and the overflow bit. */
1582 tree arg0
= cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 0), lval
,
1583 non_constant_p
, overflow_p
);
1584 tree arg1
= cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 1), lval
,
1585 non_constant_p
, overflow_p
);
1587 if (TREE_CODE (arg0
) == INTEGER_CST
&& TREE_CODE (arg1
) == INTEGER_CST
)
1589 location_t loc
= cp_expr_loc_or_input_loc (t
);
1590 tree type
= TREE_TYPE (TREE_TYPE (t
));
1591 tree result
= fold_binary_loc (loc
, opcode
, type
,
1592 fold_convert_loc (loc
, type
, arg0
),
1593 fold_convert_loc (loc
, type
, arg1
));
1595 = build_int_cst (type
, arith_overflowed_p (opcode
, type
, arg0
, arg1
));
1596 /* Reset TREE_OVERFLOW to avoid warnings for the overflow. */
1597 if (TREE_OVERFLOW (result
))
1598 TREE_OVERFLOW (result
) = 0;
1600 return build_complex (TREE_TYPE (t
), result
, ovf
);
1603 *non_constant_p
= true;
1607 /* Clean CONSTRUCTOR_NO_CLEARING from CTOR and its sub-aggregates. */
1610 clear_no_implicit_zero (tree ctor
)
1612 if (CONSTRUCTOR_NO_CLEARING (ctor
))
1614 CONSTRUCTOR_NO_CLEARING (ctor
) = false;
1615 tree elt
; unsigned HOST_WIDE_INT idx
;
1616 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor
), idx
, elt
)
1617 if (TREE_CODE (elt
) == CONSTRUCTOR
)
1618 clear_no_implicit_zero (elt
);
1622 /* Complain about a const object OBJ being modified in a constant expression.
1623 EXPR is the MODIFY_EXPR expression performing the modification. */
1626 modifying_const_object_error (tree expr
, tree obj
)
1628 location_t loc
= cp_expr_loc_or_input_loc (expr
);
1629 auto_diagnostic_group d
;
1630 error_at (loc
, "modifying a const object %qE is not allowed in "
1631 "a constant expression", TREE_OPERAND (expr
, 0));
1632 inform (location_of (obj
), "originally declared %<const%> here");
1635 /* Return true if FNDECL is a replaceable global allocation function that
1636 should be useable during constant expression evaluation. */
1639 cxx_replaceable_global_alloc_fn (tree fndecl
)
1641 return (cxx_dialect
>= cxx2a
1642 && IDENTIFIER_NEWDEL_OP_P (DECL_NAME (fndecl
))
1643 && CP_DECL_CONTEXT (fndecl
) == global_namespace
1644 && (DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl
)
1645 || DECL_IS_OPERATOR_DELETE_P (fndecl
)));
1648 /* Return true if FNDECL is a placement new function that should be
1649 useable during constant expression evaluation of std::construct_at. */
1652 cxx_placement_new_fn (tree fndecl
)
1654 if (cxx_dialect
>= cxx2a
1655 && IDENTIFIER_NEW_OP_P (DECL_NAME (fndecl
))
1656 && CP_DECL_CONTEXT (fndecl
) == global_namespace
1657 && !DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl
)
1658 && TREE_CODE (TREE_TYPE (fndecl
)) == FUNCTION_TYPE
)
1660 tree first_arg
= TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
1661 if (TREE_VALUE (first_arg
) == ptr_type_node
1662 && TREE_CHAIN (first_arg
) == void_list_node
)
1668 /* Return true if FNDECL is std::construct_at. */
1671 is_std_construct_at (tree fndecl
)
1673 if (!decl_in_std_namespace_p (fndecl
))
1676 tree name
= DECL_NAME (fndecl
);
1677 return name
&& id_equal (name
, "construct_at");
1680 /* Return true if FNDECL is std::allocator<T>::{,de}allocate. */
1683 is_std_allocator_allocate (tree fndecl
)
1685 tree name
= DECL_NAME (fndecl
);
1686 if (name
== NULL_TREE
1687 || !(id_equal (name
, "allocate") || id_equal (name
, "deallocate")))
1690 tree ctx
= DECL_CONTEXT (fndecl
);
1691 if (ctx
== NULL_TREE
|| !CLASS_TYPE_P (ctx
) || !TYPE_MAIN_DECL (ctx
))
1694 tree decl
= TYPE_MAIN_DECL (ctx
);
1695 name
= DECL_NAME (decl
);
1696 if (name
== NULL_TREE
|| !id_equal (name
, "allocator"))
1699 return decl_in_std_namespace_p (decl
);
1702 /* Return true if FNDECL is __dynamic_cast. */
1705 cxx_dynamic_cast_fn_p (tree fndecl
)
1707 return (cxx_dialect
>= cxx2a
1708 && id_equal (DECL_NAME (fndecl
), "__dynamic_cast")
1709 && CP_DECL_CONTEXT (fndecl
) == global_namespace
);
1712 /* Often, we have an expression in the form of address + offset, e.g.
1713 "&_ZTV1A + 16". Extract the object from it, i.e. "_ZTV1A". */
1716 extract_obj_from_addr_offset (tree expr
)
1718 if (TREE_CODE (expr
) == POINTER_PLUS_EXPR
)
1719 expr
= TREE_OPERAND (expr
, 0);
1721 if (TREE_CODE (expr
) == ADDR_EXPR
)
1722 expr
= TREE_OPERAND (expr
, 0);
1726 /* Given a PATH like
1728 g.D.2181.D.2154.D.2102.D.2093
1730 find a component with type TYPE. Return NULL_TREE if not found, and
1731 error_mark_node if the component is not accessible. If STOP is non-null,
1732 this function will return NULL_TREE if STOP is found before TYPE. */
1735 get_component_with_type (tree path
, tree type
, tree stop
)
1739 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path
), type
))
1743 && (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path
),
1746 else if (TREE_CODE (path
) == COMPONENT_REF
1747 && DECL_FIELD_IS_BASE (TREE_OPERAND (path
, 1)))
1749 /* We need to check that the component we're accessing is in fact
1751 if (TREE_PRIVATE (TREE_OPERAND (path
, 1))
1752 || TREE_PROTECTED (TREE_OPERAND (path
, 1)))
1753 return error_mark_node
;
1754 path
= TREE_OPERAND (path
, 0);
1761 /* Evaluate a call to __dynamic_cast (permitted by P1327R1).
1763 The declaration of __dynamic_cast is:
1765 void* __dynamic_cast (const void* __src_ptr,
1766 const __class_type_info* __src_type,
1767 const __class_type_info* __dst_type,
1768 ptrdiff_t __src2dst);
1770 where src2dst has the following possible values
1772 >-1: src_type is a unique public non-virtual base of dst_type
1773 dst_ptr + src2dst == src_ptr
1774 -1: unspecified relationship
1775 -2: src_type is not a public base of dst_type
1776 -3: src_type is a multiple public non-virtual base of dst_type
1778 Since literal types can't have virtual bases, we only expect hint >=0,
1782 cxx_eval_dynamic_cast_fn (const constexpr_ctx
*ctx
, tree call
,
1783 bool *non_constant_p
, bool *overflow_p
)
1785 /* T will be something like
1786 __dynamic_cast ((B*) b, &_ZTI1B, &_ZTI1D, 8)
1788 gcc_assert (call_expr_nargs (call
) == 4);
1789 tsubst_flags_t complain
= ctx
->quiet
? tf_none
: tf_warning_or_error
;
1790 tree obj
= CALL_EXPR_ARG (call
, 0);
1791 tree type
= CALL_EXPR_ARG (call
, 2);
1792 HOST_WIDE_INT hint
= int_cst_value (CALL_EXPR_ARG (call
, 3));
1793 location_t loc
= cp_expr_loc_or_input_loc (call
);
1795 /* Get the target type of the dynamic_cast. */
1796 gcc_assert (TREE_CODE (type
) == ADDR_EXPR
);
1797 type
= TREE_OPERAND (type
, 0);
1798 type
= TREE_TYPE (DECL_NAME (type
));
1800 /* TYPE can only be either T* or T&. We can't know which of these it
1801 is by looking at TYPE, but OBJ will be "(T*) x" in the first case,
1802 and something like "(T*)(T&)(T*) x" in the second case. */
1803 bool reference_p
= false;
1804 while (CONVERT_EXPR_P (obj
) || TREE_CODE (obj
) == SAVE_EXPR
)
1806 reference_p
|= TYPE_REF_P (TREE_TYPE (obj
));
1807 obj
= TREE_OPERAND (obj
, 0);
1810 /* Evaluate the object so that we know its dynamic type. */
1811 obj
= cxx_eval_constant_expression (ctx
, obj
, /*lval*/false, non_constant_p
,
1813 if (*non_constant_p
)
1816 /* We expect OBJ to be in form of &d.D.2102 when HINT == 0,
1817 but when HINT is > 0, it can also be something like
1818 &d.D.2102 + 18446744073709551608, which includes the BINFO_OFFSET. */
1819 obj
= extract_obj_from_addr_offset (obj
);
1820 const tree objtype
= TREE_TYPE (obj
);
1821 /* If OBJ doesn't refer to a base field, we're done. */
1822 if (tree t
= (TREE_CODE (obj
) == COMPONENT_REF
1823 ? TREE_OPERAND (obj
, 1) : obj
))
1824 if (TREE_CODE (t
) != FIELD_DECL
|| !DECL_FIELD_IS_BASE (t
))
1825 return integer_zero_node
;
1827 /* [class.cdtor] When a dynamic_cast is used in a constructor ...
1828 or in a destructor ... if the operand of the dynamic_cast refers
1829 to the object under construction or destruction, this object is
1830 considered to be a most derived object that has the type of the
1831 constructor or destructor's class. */
1832 tree vtable
= build_vfield_ref (obj
, TREE_TYPE (obj
));
1833 vtable
= cxx_eval_constant_expression (ctx
, vtable
, /*lval*/false,
1834 non_constant_p
, overflow_p
);
1835 if (*non_constant_p
)
1837 /* VTABLE will be &_ZTV1A + 16 or similar, get _ZTV1A. */
1838 vtable
= extract_obj_from_addr_offset (vtable
);
1839 const tree mdtype
= DECL_CONTEXT (vtable
);
1841 /* Given dynamic_cast<T>(v),
1843 [expr.dynamic.cast] If C is the class type to which T points or refers,
1844 the runtime check logically executes as follows:
1846 If, in the most derived object pointed (referred) to by v, v points
1847 (refers) to a public base class subobject of a C object, and if only
1848 one object of type C is derived from the subobject pointed (referred)
1849 to by v the result points (refers) to that C object.
1851 In this case, HINT >= 0 or -3. */
1852 if (hint
>= 0 || hint
== -3)
1854 /* Look for a component with type TYPE. */
1855 tree t
= get_component_with_type (obj
, type
, mdtype
);
1856 /* If not accessible, give an error. */
1857 if (t
== error_mark_node
)
1863 error_at (loc
, "reference %<dynamic_cast%> failed");
1864 inform (loc
, "static type %qT of its operand is a "
1865 "non-public base class of dynamic type %qT",
1869 *non_constant_p
= true;
1871 return integer_zero_node
;
1874 /* The result points to the TYPE object. */
1875 return cp_build_addr_expr (t
, complain
);
1876 /* Else, TYPE was not found, because the HINT turned out to be wrong.
1877 Fall through to the normal processing. */
1880 /* Otherwise, if v points (refers) to a public base class subobject of the
1881 most derived object, and the type of the most derived object has a base
1882 class, of type C, that is unambiguous and public, the result points
1883 (refers) to the C subobject of the most derived object.
1885 But it can also be an invalid case. */
1887 /* Get the most derived object. */
1888 obj
= get_component_with_type (obj
, mdtype
, NULL_TREE
);
1889 if (obj
== error_mark_node
)
1895 error_at (loc
, "reference %<dynamic_cast%> failed");
1896 inform (loc
, "static type %qT of its operand is a non-public"
1897 " base class of dynamic type %qT", objtype
, mdtype
);
1899 *non_constant_p
= true;
1901 return integer_zero_node
;
1906 /* Check that the type of the most derived object has a base class
1907 of type TYPE that is unambiguous and public. */
1909 tree binfo
= lookup_base (mdtype
, type
, ba_check
, &b_kind
, tf_none
);
1910 if (!binfo
|| binfo
== error_mark_node
)
1916 error_at (loc
, "reference %<dynamic_cast%> failed");
1917 if (b_kind
== bk_ambig
)
1918 inform (loc
, "%qT is an ambiguous base class of dynamic "
1919 "type %qT of its operand", type
, mdtype
);
1921 inform (loc
, "dynamic type %qT of its operand does not "
1922 "have an unambiguous public base class %qT",
1925 *non_constant_p
= true;
1927 return integer_zero_node
;
1929 /* If so, return the TYPE subobject of the most derived object. */
1930 obj
= convert_to_base_statically (obj
, binfo
);
1931 return cp_build_addr_expr (obj
, complain
);
1934 /* Subroutine of cxx_eval_constant_expression.
1935 Evaluate the call expression tree T in the context of OLD_CALL expression
1939 cxx_eval_call_expression (const constexpr_ctx
*ctx
, tree t
,
1941 bool *non_constant_p
, bool *overflow_p
)
1943 /* Handle concept checks separately. */
1944 if (concept_check_p (t
))
1945 return evaluate_concept_check (t
, tf_warning_or_error
);
1947 location_t loc
= cp_expr_loc_or_input_loc (t
);
1948 tree fun
= get_function_named_in_call (t
);
1949 constexpr_call new_call
1950 = { NULL
, NULL
, NULL
, 0, ctx
->manifestly_const_eval
};
1953 if (fun
== NULL_TREE
)
1954 return cxx_eval_internal_function (ctx
, t
, lval
,
1955 non_constant_p
, overflow_p
);
1957 if (TREE_CODE (fun
) != FUNCTION_DECL
)
1959 /* Might be a constexpr function pointer. */
1960 fun
= cxx_eval_constant_expression (ctx
, fun
,
1961 /*lval*/false, non_constant_p
,
1964 if (TREE_CODE (fun
) == ADDR_EXPR
)
1965 fun
= TREE_OPERAND (fun
, 0);
1966 /* For TARGET_VTABLE_USES_DESCRIPTORS targets, there is no
1967 indirection, the called expression is a pointer into the
1968 virtual table which should contain FDESC_EXPR. Extract the
1969 FUNCTION_DECL from there. */
1970 else if (TARGET_VTABLE_USES_DESCRIPTORS
1971 && TREE_CODE (fun
) == POINTER_PLUS_EXPR
1972 && TREE_CODE (TREE_OPERAND (fun
, 0)) == ADDR_EXPR
1973 && TREE_CODE (TREE_OPERAND (fun
, 1)) == INTEGER_CST
)
1975 tree d
= TREE_OPERAND (TREE_OPERAND (fun
, 0), 0);
1977 && DECL_VTABLE_OR_VTT_P (d
)
1978 && TREE_CODE (TREE_TYPE (d
)) == ARRAY_TYPE
1979 && TREE_TYPE (TREE_TYPE (d
)) == vtable_entry_type
1981 && TREE_CODE (DECL_INITIAL (d
)) == CONSTRUCTOR
)
1983 tree i
= int_const_binop (TRUNC_DIV_EXPR
, TREE_OPERAND (fun
, 1),
1984 TYPE_SIZE_UNIT (vtable_entry_type
));
1985 HOST_WIDE_INT idx
= find_array_ctor_elt (DECL_INITIAL (d
), i
);
1989 = (*CONSTRUCTOR_ELTS (DECL_INITIAL (d
)))[idx
].value
;
1990 if (TREE_CODE (fdesc
) == FDESC_EXPR
1991 && integer_zerop (TREE_OPERAND (fdesc
, 1)))
1992 fun
= TREE_OPERAND (fdesc
, 0);
1997 if (TREE_CODE (fun
) != FUNCTION_DECL
)
1999 if (!ctx
->quiet
&& !*non_constant_p
)
2000 error_at (loc
, "expression %qE does not designate a %<constexpr%> "
2002 *non_constant_p
= true;
2005 if (DECL_CLONED_FUNCTION_P (fun
))
2006 fun
= DECL_CLONED_FUNCTION (fun
);
2008 if (is_ubsan_builtin_p (fun
))
2011 if (fndecl_built_in_p (fun
))
2012 return cxx_eval_builtin_function_call (ctx
, t
, fun
,
2013 lval
, non_constant_p
, overflow_p
);
2014 if (!DECL_DECLARED_CONSTEXPR_P (fun
))
2016 if (TREE_CODE (t
) == CALL_EXPR
2017 && cxx_replaceable_global_alloc_fn (fun
)
2018 && (CALL_FROM_NEW_OR_DELETE_P (t
)
2020 && ctx
->call
->fundef
2021 && is_std_allocator_allocate (ctx
->call
->fundef
->decl
))))
2023 const int nargs
= call_expr_nargs (t
);
2024 tree arg0
= NULL_TREE
;
2025 for (int i
= 0; i
< nargs
; ++i
)
2027 tree arg
= CALL_EXPR_ARG (t
, i
);
2028 arg
= cxx_eval_constant_expression (ctx
, arg
, false,
2029 non_constant_p
, overflow_p
);
2030 VERIFY_CONSTANT (arg
);
2035 if (IDENTIFIER_NEW_OP_P (DECL_NAME (fun
)))
2037 tree type
= build_array_type_nelts (char_type_node
,
2038 tree_to_uhwi (arg0
));
2039 tree var
= build_decl (loc
, VAR_DECL
, heap_uninit_identifier
,
2041 DECL_ARTIFICIAL (var
) = 1;
2042 TREE_STATIC (var
) = 1;
2043 ctx
->global
->heap_vars
.safe_push (var
);
2044 ctx
->global
->values
.put (var
, NULL_TREE
);
2045 return fold_convert (ptr_type_node
, build_address (var
));
2050 if (TREE_CODE (arg0
) == ADDR_EXPR
2051 && VAR_P (TREE_OPERAND (arg0
, 0)))
2053 tree var
= TREE_OPERAND (arg0
, 0);
2054 if (DECL_NAME (var
) == heap_uninit_identifier
2055 || DECL_NAME (var
) == heap_identifier
)
2057 DECL_NAME (var
) = heap_deleted_identifier
;
2058 ctx
->global
->values
.remove (var
);
2061 else if (DECL_NAME (var
) == heap_deleted_identifier
)
2064 error_at (loc
, "deallocation of already deallocated "
2066 *non_constant_p
= true;
2071 error_at (loc
, "deallocation of storage that was "
2072 "not previously allocated");
2073 *non_constant_p
= true;
2077 /* Allow placement new in std::construct_at, just return the second
2079 if (TREE_CODE (t
) == CALL_EXPR
2080 && cxx_placement_new_fn (fun
)
2082 && ctx
->call
->fundef
2083 && is_std_construct_at (ctx
->call
->fundef
->decl
))
2085 const int nargs
= call_expr_nargs (t
);
2086 tree arg1
= NULL_TREE
;
2087 for (int i
= 0; i
< nargs
; ++i
)
2089 tree arg
= CALL_EXPR_ARG (t
, i
);
2090 arg
= cxx_eval_constant_expression (ctx
, arg
, false,
2091 non_constant_p
, overflow_p
);
2092 VERIFY_CONSTANT (arg
);
2099 else if (cxx_dynamic_cast_fn_p (fun
))
2100 return cxx_eval_dynamic_cast_fn (ctx
, t
, non_constant_p
, overflow_p
);
2104 if (!lambda_static_thunk_p (fun
))
2105 error_at (loc
, "call to non-%<constexpr%> function %qD", fun
);
2106 explain_invalid_constexpr_fn (fun
);
2108 *non_constant_p
= true;
2112 constexpr_ctx new_ctx
= *ctx
;
2113 if (DECL_CONSTRUCTOR_P (fun
) && !ctx
->object
2114 && TREE_CODE (t
) == AGGR_INIT_EXPR
)
2116 /* We want to have an initialization target for an AGGR_INIT_EXPR.
2117 If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
2118 new_ctx
.object
= AGGR_INIT_EXPR_SLOT (t
);
2119 tree ctor
= new_ctx
.ctor
= build_constructor (DECL_CONTEXT (fun
), NULL
);
2120 CONSTRUCTOR_NO_CLEARING (ctor
) = true;
2121 ctx
->global
->values
.put (new_ctx
.object
, ctor
);
2125 /* Shortcut trivial constructor/op=. */
2126 if (trivial_fn_p (fun
))
2128 tree init
= NULL_TREE
;
2129 if (call_expr_nargs (t
) == 2)
2130 init
= convert_from_reference (get_nth_callarg (t
, 1));
2131 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
2132 && AGGR_INIT_ZERO_FIRST (t
))
2133 init
= build_zero_init (DECL_CONTEXT (fun
), NULL_TREE
, false);
2136 tree op
= get_nth_callarg (t
, 0);
2137 if (is_dummy_object (op
))
2140 op
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (op
)), op
);
2141 tree set
= build2 (MODIFY_EXPR
, TREE_TYPE (op
), op
, init
);
2142 new_ctx
.call
= &new_call
;
2143 return cxx_eval_constant_expression (&new_ctx
, set
, lval
,
2144 non_constant_p
, overflow_p
);
2148 /* We can't defer instantiating the function any longer. */
2149 if (!DECL_INITIAL (fun
)
2150 && DECL_TEMPLOID_INSTANTIATION (fun
))
2152 location_t save_loc
= input_location
;
2153 input_location
= loc
;
2155 instantiate_decl (fun
, /*defer_ok*/false, /*expl_inst*/false);
2157 input_location
= save_loc
;
2160 /* If in direct recursive call, optimize definition search. */
2161 if (ctx
&& ctx
->call
&& ctx
->call
->fundef
&& ctx
->call
->fundef
->decl
== fun
)
2162 new_call
.fundef
= ctx
->call
->fundef
;
2165 new_call
.fundef
= retrieve_constexpr_fundef (fun
);
2166 if (new_call
.fundef
== NULL
|| new_call
.fundef
->body
== NULL
2167 || new_call
.fundef
->result
== error_mark_node
2168 || fun
== current_function_decl
)
2172 /* We need to check for current_function_decl here in case we're
2173 being called during cp_fold_function, because at that point
2174 DECL_INITIAL is set properly and we have a fundef but we
2175 haven't lowered invisirefs yet (c++/70344). */
2176 if (DECL_INITIAL (fun
) == error_mark_node
2177 || fun
== current_function_decl
)
2178 error_at (loc
, "%qD called in a constant expression before its "
2179 "definition is complete", fun
);
2180 else if (DECL_INITIAL (fun
))
2182 /* The definition of fun was somehow unsuitable. But pretend
2183 that lambda static thunks don't exist. */
2184 if (!lambda_static_thunk_p (fun
))
2185 error_at (loc
, "%qD called in a constant expression", fun
);
2186 explain_invalid_constexpr_fn (fun
);
2189 error_at (loc
, "%qD used before its definition", fun
);
2191 *non_constant_p
= true;
2196 bool non_constant_args
= false;
2197 cxx_bind_parameters_in_call (ctx
, t
, &new_call
,
2198 non_constant_p
, overflow_p
, &non_constant_args
);
2200 /* We build up the bindings list before we know whether we already have this
2201 call cached. If we don't end up saving these bindings, ggc_free them when
2202 this function exits. */
2207 free_bindings (tree
&b
): bindings (&b
) { }
2208 ~free_bindings () { if (bindings
) ggc_free (*bindings
); }
2209 void preserve () { bindings
= NULL
; }
2210 } fb (new_call
.bindings
);
2212 if (*non_constant_p
)
2215 depth_ok
= push_cx_call_context (t
);
2217 /* Remember the object we are constructing. */
2218 tree new_obj
= NULL_TREE
;
2219 if (DECL_CONSTRUCTOR_P (fun
))
2221 /* In a constructor, it should be the first `this' argument.
2222 At this point it has already been evaluated in the call
2223 to cxx_bind_parameters_in_call. */
2224 new_obj
= TREE_VEC_ELT (new_call
.bindings
, 0);
2225 STRIP_NOPS (new_obj
);
2226 if (TREE_CODE (new_obj
) == ADDR_EXPR
)
2227 new_obj
= TREE_OPERAND (new_obj
, 0);
2230 tree result
= NULL_TREE
;
2232 constexpr_call
*entry
= NULL
;
2233 if (depth_ok
&& !non_constant_args
&& ctx
->strict
)
2235 new_call
.hash
= constexpr_fundef_hasher::hash (new_call
.fundef
);
2237 = iterative_hash_template_arg (new_call
.bindings
, new_call
.hash
);
2239 = iterative_hash_object (ctx
->manifestly_const_eval
, new_call
.hash
);
2241 /* If we have seen this call before, we are done. */
2242 maybe_initialize_constexpr_call_table ();
2243 constexpr_call
**slot
2244 = constexpr_call_table
->find_slot (&new_call
, INSERT
);
2248 /* Only cache up to constexpr_cache_depth to limit memory use. */
2249 if (depth_ok
< constexpr_cache_depth
)
2251 /* We need to keep a pointer to the entry, not just the slot, as
2252 the slot can move during evaluation of the body. */
2253 *slot
= entry
= ggc_alloc
<constexpr_call
> ();
2258 /* Calls that are in progress have their result set to NULL, so that we
2259 can detect circular dependencies. Now that we only cache up to
2260 constexpr_cache_depth this won't catch circular dependencies that
2261 start deeper, but they'll hit the recursion or ops limit. */
2262 else if (entry
->result
== NULL
)
2265 error ("call has circular dependency");
2266 *non_constant_p
= true;
2267 entry
->result
= result
= error_mark_node
;
2270 result
= entry
->result
;
2276 error ("%<constexpr%> evaluation depth exceeds maximum of %d (use "
2277 "%<-fconstexpr-depth=%> to increase the maximum)",
2278 max_constexpr_depth
);
2279 *non_constant_p
= true;
2280 result
= error_mark_node
;
2284 if (result
&& result
!= error_mark_node
)
2286 else if (!DECL_SAVED_TREE (fun
))
2288 /* When at_eof >= 2, cgraph has started throwing away
2289 DECL_SAVED_TREE, so fail quietly. FIXME we get here because of
2290 late code generation for VEC_INIT_EXPR, which needs to be
2291 completely reconsidered. */
2292 gcc_assert (at_eof
>= 2 && ctx
->quiet
);
2293 *non_constant_p
= true;
2297 tree body
, parms
, res
;
2298 releasing_vec ctors
;
2300 /* Reuse or create a new unshared copy of this function's body. */
2301 tree copy
= get_fundef_copy (new_call
.fundef
);
2302 body
= TREE_PURPOSE (copy
);
2303 parms
= TREE_VALUE (copy
);
2304 res
= TREE_TYPE (copy
);
2306 /* Associate the bindings with the remapped parms. */
2307 tree bound
= new_call
.bindings
;
2308 tree remapped
= parms
;
2309 for (int i
= 0; i
< TREE_VEC_LENGTH (bound
); ++i
)
2311 tree arg
= TREE_VEC_ELT (bound
, i
);
2314 /* Unshare args going into the hash table to separate them
2315 from the caller's context, for better GC and to avoid
2316 problems with verify_gimple. */
2317 arg
= unshare_expr_without_location (arg
);
2318 TREE_VEC_ELT (bound
, i
) = arg
;
2320 /* Don't share a CONSTRUCTOR that might be changed. This is not
2321 redundant with the unshare just above; we also don't want to
2322 change the argument values in the hash table. XXX Could we
2323 unshare lazily in cxx_eval_store_expression? */
2324 arg
= unshare_constructor (arg
);
2325 if (TREE_CODE (arg
) == CONSTRUCTOR
)
2326 vec_safe_push (ctors
, arg
);
2327 ctx
->global
->values
.put (remapped
, arg
);
2328 remapped
= DECL_CHAIN (remapped
);
2330 /* Add the RESULT_DECL to the values map, too. */
2331 tree slot
= NULL_TREE
;
2332 if (DECL_BY_REFERENCE (res
))
2334 slot
= AGGR_INIT_EXPR_SLOT (t
);
2335 tree addr
= build_address (slot
);
2336 addr
= build_nop (TREE_TYPE (res
), addr
);
2337 ctx
->global
->values
.put (res
, addr
);
2338 ctx
->global
->values
.put (slot
, NULL_TREE
);
2341 ctx
->global
->values
.put (res
, NULL_TREE
);
2343 /* Track the callee's evaluated SAVE_EXPRs and TARGET_EXPRs so that
2344 we can forget their values after the call. */
2345 constexpr_ctx ctx_with_save_exprs
= *ctx
;
2346 auto_vec
<tree
, 10> save_exprs
;
2347 ctx_with_save_exprs
.save_exprs
= &save_exprs
;
2348 ctx_with_save_exprs
.call
= &new_call
;
2350 tree jump_target
= NULL_TREE
;
2351 cxx_eval_constant_expression (&ctx_with_save_exprs
, body
,
2352 lval
, non_constant_p
, overflow_p
,
2355 if (DECL_CONSTRUCTOR_P (fun
))
2356 /* This can be null for a subobject constructor call, in
2357 which case what we care about is the initialization
2358 side-effects rather than the value. We could get at the
2359 value by evaluating *this, but we don't bother; there's
2360 no need to put such a call in the hash table. */
2361 result
= lval
? ctx
->object
: ctx
->ctor
;
2362 else if (VOID_TYPE_P (TREE_TYPE (res
)))
2366 result
= *ctx
->global
->values
.get (slot
? slot
: res
);
2367 if (result
== NULL_TREE
&& !*non_constant_p
)
2370 error ("%<constexpr%> call flows off the end "
2372 *non_constant_p
= true;
2376 /* At this point, the object's constructor will have run, so
2377 the object is no longer under construction, and its possible
2378 'const' semantics now apply. Make a note of this fact by
2379 marking the CONSTRUCTOR TREE_READONLY. */
2381 && CLASS_TYPE_P (TREE_TYPE (new_obj
))
2382 && CP_TYPE_CONST_P (TREE_TYPE (new_obj
)))
2384 /* Subobjects might not be stored in ctx->global->values but we
2385 can get its CONSTRUCTOR by evaluating *this. */
2386 tree e
= cxx_eval_constant_expression (ctx
, new_obj
,
2390 TREE_READONLY (e
) = true;
2393 /* Forget the saved values of the callee's SAVE_EXPRs and
2397 FOR_EACH_VEC_ELT (save_exprs
, i
, save_expr
)
2398 ctx
->global
->values
.remove (save_expr
);
2400 /* Remove the parms/result from the values map. Is it worth
2401 bothering to do this when the map itself is only live for
2402 one constexpr evaluation? If so, maybe also clear out
2403 other vars from call, maybe in BIND_EXPR handling? */
2404 ctx
->global
->values
.remove (res
);
2406 ctx
->global
->values
.remove (slot
);
2407 for (tree parm
= parms
; parm
; parm
= TREE_CHAIN (parm
))
2408 ctx
->global
->values
.remove (parm
);
2410 /* Free any parameter CONSTRUCTORs we aren't returning directly. */
2411 while (!ctors
->is_empty ())
2413 tree c
= ctors
->pop ();
2415 free_constructor (c
);
2418 /* Make the unshared function copy we used available for re-use. */
2419 save_fundef_copy (fun
, copy
);
2422 if (result
== error_mark_node
)
2423 *non_constant_p
= true;
2424 if (*non_constant_p
|| *overflow_p
)
2425 result
= error_mark_node
;
2429 entry
->result
= result
;
2432 /* The result of a constexpr function must be completely initialized.
2434 However, in C++20, a constexpr constructor doesn't necessarily have
2435 to initialize all the fields, so we don't clear CONSTRUCTOR_NO_CLEARING
2436 in order to detect reading an unitialized object in constexpr instead
2437 of value-initializing it. (reduced_constant_expression_p is expected to
2438 take care of clearing the flag.) */
2439 if (TREE_CODE (result
) == CONSTRUCTOR
2440 && (cxx_dialect
< cxx2a
2441 || !DECL_CONSTRUCTOR_P (fun
)))
2442 clear_no_implicit_zero (result
);
2444 pop_cx_call_context ();
2448 /* Return true if T is a valid constant initializer. If a CONSTRUCTOR
2449 initializes all the members, the CONSTRUCTOR_NO_CLEARING flag will be
2451 FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
2454 reduced_constant_expression_p (tree t
)
2459 switch (TREE_CODE (t
))
2462 /* Even if we can't lower this yet, it's constant. */
2466 /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
2467 tree idx
, val
, field
; unsigned HOST_WIDE_INT i
;
2468 if (CONSTRUCTOR_NO_CLEARING (t
))
2470 if (TREE_CODE (TREE_TYPE (t
)) == VECTOR_TYPE
)
2471 /* An initialized vector would have a VECTOR_CST. */
2473 else if (cxx_dialect
>= cxx2a
2474 /* An ARRAY_TYPE doesn't have any TYPE_FIELDS. */
2475 && (TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
2476 /* A union only initializes one member. */
2477 || TREE_CODE (TREE_TYPE (t
)) == UNION_TYPE
))
2480 field
= next_initializable_field (TYPE_FIELDS (TREE_TYPE (t
)));
2484 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t
), i
, idx
, val
)
2486 /* If VAL is null, we're in the middle of initializing this
2488 if (!reduced_constant_expression_p (val
))
2492 /* Empty class field may or may not have an initializer. */
2493 for (; idx
!= field
;
2494 field
= next_initializable_field (DECL_CHAIN (field
)))
2495 if (!is_really_empty_class (TREE_TYPE (field
),
2496 /*ignore_vptr*/false))
2498 field
= next_initializable_field (DECL_CHAIN (field
));
2501 /* There could be a non-empty field at the end. */
2502 for (; field
; field
= next_initializable_field (DECL_CHAIN (field
)))
2503 if (!is_really_empty_class (TREE_TYPE (field
), /*ignore_vptr*/false))
2505 if (CONSTRUCTOR_NO_CLEARING (t
))
2506 /* All the fields are initialized. */
2507 CONSTRUCTOR_NO_CLEARING (t
) = false;
2511 /* FIXME are we calling this too much? */
2512 return initializer_constant_valid_p (t
, TREE_TYPE (t
)) != NULL_TREE
;
2516 /* Some expressions may have constant operands but are not constant
2517 themselves, such as 1/0. Call this function to check for that
2520 We only call this in places that require an arithmetic constant, not in
2521 places where we might have a non-constant expression that can be a
2522 component of a constant expression, such as the address of a constexpr
2523 variable that might be dereferenced later. */
2526 verify_constant (tree t
, bool allow_non_constant
, bool *non_constant_p
,
2529 if (!*non_constant_p
&& !reduced_constant_expression_p (t
))
2531 if (!allow_non_constant
)
2532 error ("%q+E is not a constant expression", t
);
2533 *non_constant_p
= true;
2535 if (TREE_OVERFLOW_P (t
))
2537 if (!allow_non_constant
)
2539 permerror (input_location
, "overflow in constant expression");
2540 /* If we're being permissive (and are in an enforcing
2541 context), ignore the overflow. */
2542 if (flag_permissive
)
2543 return *non_constant_p
;
2547 return *non_constant_p
;
2550 /* Check whether the shift operation with code CODE and type TYPE on LHS
2551 and RHS is undefined. If it is, give an error with an explanation,
2552 and return true; return false otherwise. */
2555 cxx_eval_check_shift_p (location_t loc
, const constexpr_ctx
*ctx
,
2556 enum tree_code code
, tree type
, tree lhs
, tree rhs
)
2558 if ((code
!= LSHIFT_EXPR
&& code
!= RSHIFT_EXPR
)
2559 || TREE_CODE (lhs
) != INTEGER_CST
2560 || TREE_CODE (rhs
) != INTEGER_CST
)
2563 tree lhstype
= TREE_TYPE (lhs
);
2564 unsigned HOST_WIDE_INT uprec
= TYPE_PRECISION (TREE_TYPE (lhs
));
2566 /* [expr.shift] The behavior is undefined if the right operand
2567 is negative, or greater than or equal to the length in bits
2568 of the promoted left operand. */
2569 if (tree_int_cst_sgn (rhs
) == -1)
2572 permerror (loc
, "right operand of shift expression %q+E is negative",
2573 build2_loc (loc
, code
, type
, lhs
, rhs
));
2574 return (!flag_permissive
|| ctx
->quiet
);
2576 if (compare_tree_int (rhs
, uprec
) >= 0)
2579 permerror (loc
, "right operand of shift expression %q+E is greater "
2580 "than or equal to the precision %wu of the left operand",
2581 build2_loc (loc
, code
, type
, lhs
, rhs
), uprec
);
2582 return (!flag_permissive
|| ctx
->quiet
);
2585 /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
2586 if E1 has a signed type and non-negative value, and E1x2^E2 is
2587 representable in the corresponding unsigned type of the result type,
2588 then that value, converted to the result type, is the resulting value;
2589 otherwise, the behavior is undefined.
2591 The value of E1 << E2 is the unique value congruent to E1 x 2^E2 modulo
2592 2^N, where N is the range exponent of the type of the result. */
2593 if (code
== LSHIFT_EXPR
2594 && !TYPE_UNSIGNED (lhstype
)
2595 && cxx_dialect
>= cxx11
2596 && cxx_dialect
< cxx2a
)
2598 if (tree_int_cst_sgn (lhs
) == -1)
2602 "left operand of shift expression %q+E is negative",
2603 build2_loc (loc
, code
, type
, lhs
, rhs
));
2604 return (!flag_permissive
|| ctx
->quiet
);
2606 /* For signed x << y the following:
2607 (unsigned) x >> ((prec (lhs) - 1) - y)
2608 if > 1, is undefined. The right-hand side of this formula
2609 is the highest bit of the LHS that can be set (starting from 0),
2610 so that the shift doesn't overflow. We then right-shift the LHS
2611 to see whether any other bit is set making the original shift
2612 undefined -- the result is not representable in the corresponding
2614 tree t
= build_int_cst (unsigned_type_node
, uprec
- 1);
2615 t
= fold_build2 (MINUS_EXPR
, unsigned_type_node
, t
, rhs
);
2616 tree ulhs
= fold_convert (unsigned_type_for (lhstype
), lhs
);
2617 t
= fold_build2 (RSHIFT_EXPR
, TREE_TYPE (ulhs
), ulhs
, t
);
2618 if (tree_int_cst_lt (integer_one_node
, t
))
2621 permerror (loc
, "shift expression %q+E overflows",
2622 build2_loc (loc
, code
, type
, lhs
, rhs
));
2623 return (!flag_permissive
|| ctx
->quiet
);
2629 /* Subroutine of cxx_eval_constant_expression.
2630 Attempt to reduce the unary expression tree T to a compile time value.
2631 If successful, return the value. Otherwise issue a diagnostic
2632 and return error_mark_node. */
2635 cxx_eval_unary_expression (const constexpr_ctx
*ctx
, tree t
,
2637 bool *non_constant_p
, bool *overflow_p
)
2640 tree orig_arg
= TREE_OPERAND (t
, 0);
2641 tree arg
= cxx_eval_constant_expression (ctx
, orig_arg
, /*lval*/false,
2642 non_constant_p
, overflow_p
);
2643 VERIFY_CONSTANT (arg
);
2644 location_t loc
= EXPR_LOCATION (t
);
2645 enum tree_code code
= TREE_CODE (t
);
2646 tree type
= TREE_TYPE (t
);
2647 r
= fold_unary_loc (loc
, code
, type
, arg
);
2650 if (arg
== orig_arg
)
2653 r
= build1_loc (loc
, code
, type
, arg
);
2655 VERIFY_CONSTANT (r
);
2659 /* Helper function for cxx_eval_binary_expression. Try to optimize
2660 original POINTER_PLUS_EXPR T, LHS p+ RHS, return NULL_TREE if the
2661 generic folding should be used. */
2664 cxx_fold_pointer_plus_expression (const constexpr_ctx
*ctx
, tree t
,
2665 tree lhs
, tree rhs
, bool *non_constant_p
,
2669 if (TREE_CODE (lhs
) != ADDR_EXPR
)
2672 lhs
= TREE_OPERAND (lhs
, 0);
2674 /* &A[i] p+ j => &A[i + j] */
2675 if (TREE_CODE (lhs
) == ARRAY_REF
2676 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == INTEGER_CST
2677 && TREE_CODE (rhs
) == INTEGER_CST
2678 && TYPE_SIZE_UNIT (TREE_TYPE (lhs
))
2679 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs
))) == INTEGER_CST
)
2681 tree orig_type
= TREE_TYPE (t
);
2682 location_t loc
= EXPR_LOCATION (t
);
2683 tree type
= TREE_TYPE (lhs
);
2685 t
= fold_convert_loc (loc
, ssizetype
, TREE_OPERAND (lhs
, 1));
2686 tree nelts
= array_type_nelts_top (TREE_TYPE (TREE_OPERAND (lhs
, 0)));
2687 nelts
= cxx_eval_constant_expression (ctx
, nelts
, false, non_constant_p
,
2689 if (*non_constant_p
)
2691 /* Don't fold an out-of-bound access. */
2692 if (!tree_int_cst_le (t
, nelts
))
2694 rhs
= cp_fold_convert (ssizetype
, rhs
);
2695 /* Don't fold if rhs can't be divided exactly by TYPE_SIZE_UNIT.
2696 constexpr int A[1]; ... (char *)&A[0] + 1 */
2697 if (!integer_zerop (fold_build2_loc (loc
, TRUNC_MOD_EXPR
, sizetype
,
2698 rhs
, TYPE_SIZE_UNIT (type
))))
2700 /* Make sure to treat the second operand of POINTER_PLUS_EXPR
2702 rhs
= fold_build2_loc (loc
, EXACT_DIV_EXPR
, ssizetype
, rhs
,
2703 TYPE_SIZE_UNIT (type
));
2704 t
= size_binop_loc (loc
, PLUS_EXPR
, rhs
, t
);
2705 t
= build4_loc (loc
, ARRAY_REF
, type
, TREE_OPERAND (lhs
, 0),
2706 t
, NULL_TREE
, NULL_TREE
);
2707 t
= cp_build_addr_expr (t
, tf_warning_or_error
);
2708 t
= cp_fold_convert (orig_type
, t
);
2709 return cxx_eval_constant_expression (ctx
, t
, /*lval*/false,
2710 non_constant_p
, overflow_p
);
2716 /* Subroutine of cxx_eval_constant_expression.
2717 Like cxx_eval_unary_expression, except for binary expressions. */
2720 cxx_eval_binary_expression (const constexpr_ctx
*ctx
, tree t
,
2722 bool *non_constant_p
, bool *overflow_p
)
2725 tree orig_lhs
= TREE_OPERAND (t
, 0);
2726 tree orig_rhs
= TREE_OPERAND (t
, 1);
2728 lhs
= cxx_eval_constant_expression (ctx
, orig_lhs
, /*lval*/false,
2729 non_constant_p
, overflow_p
);
2730 /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer
2732 if (*non_constant_p
)
2734 rhs
= cxx_eval_constant_expression (ctx
, orig_rhs
, /*lval*/false,
2735 non_constant_p
, overflow_p
);
2736 if (*non_constant_p
)
2739 location_t loc
= EXPR_LOCATION (t
);
2740 enum tree_code code
= TREE_CODE (t
);
2741 tree type
= TREE_TYPE (t
);
2743 if (code
== EQ_EXPR
|| code
== NE_EXPR
)
2745 bool is_code_eq
= (code
== EQ_EXPR
);
2747 if (TREE_CODE (lhs
) == PTRMEM_CST
2748 && TREE_CODE (rhs
) == PTRMEM_CST
)
2750 tree lmem
= PTRMEM_CST_MEMBER (lhs
);
2751 tree rmem
= PTRMEM_CST_MEMBER (rhs
);
2753 if (TREE_CODE (lmem
) == TREE_CODE (rmem
)
2754 && TREE_CODE (lmem
) == FIELD_DECL
2755 && TREE_CODE (DECL_CONTEXT (lmem
)) == UNION_TYPE
2756 && same_type_p (DECL_CONTEXT (lmem
),
2757 DECL_CONTEXT (rmem
)))
2758 /* If both refer to (possibly different) members of the same union
2759 (12.3), they compare equal. */
2762 eq
= cp_tree_equal (lhs
, rhs
);
2763 r
= constant_boolean_node (eq
== is_code_eq
, type
);
2765 else if ((TREE_CODE (lhs
) == PTRMEM_CST
2766 || TREE_CODE (rhs
) == PTRMEM_CST
)
2767 && (null_member_pointer_value_p (lhs
)
2768 || null_member_pointer_value_p (rhs
)))
2769 r
= constant_boolean_node (!is_code_eq
, type
);
2770 else if (TREE_CODE (lhs
) == PTRMEM_CST
)
2771 lhs
= cplus_expand_constant (lhs
);
2772 else if (TREE_CODE (rhs
) == PTRMEM_CST
)
2773 rhs
= cplus_expand_constant (rhs
);
2775 if (code
== POINTER_PLUS_EXPR
&& !*non_constant_p
2776 && integer_zerop (lhs
) && !integer_zerop (rhs
))
2779 error ("arithmetic involving a null pointer in %qE", lhs
);
2780 *non_constant_p
= true;
2783 else if (code
== POINTER_PLUS_EXPR
)
2784 r
= cxx_fold_pointer_plus_expression (ctx
, t
, lhs
, rhs
, non_constant_p
,
2786 else if (code
== SPACESHIP_EXPR
)
2788 r
= genericize_spaceship (type
, lhs
, rhs
);
2789 r
= cxx_eval_constant_expression (ctx
, r
, false, non_constant_p
,
2794 r
= fold_binary_loc (loc
, code
, type
, lhs
, rhs
);
2798 if (lhs
== orig_lhs
&& rhs
== orig_rhs
)
2801 r
= build2_loc (loc
, code
, type
, lhs
, rhs
);
2803 else if (cxx_eval_check_shift_p (loc
, ctx
, code
, type
, lhs
, rhs
))
2804 *non_constant_p
= true;
2805 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
2806 a local array in a constexpr function. */
2807 bool ptr
= INDIRECT_TYPE_P (TREE_TYPE (lhs
));
2809 VERIFY_CONSTANT (r
);
2813 /* Subroutine of cxx_eval_constant_expression.
2814 Attempt to evaluate condition expressions. Dead branches are not
2818 cxx_eval_conditional_expression (const constexpr_ctx
*ctx
, tree t
,
2820 bool *non_constant_p
, bool *overflow_p
,
2823 tree val
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
2825 non_constant_p
, overflow_p
);
2826 VERIFY_CONSTANT (val
);
2827 /* Don't VERIFY_CONSTANT the other operands. */
2828 if (integer_zerop (val
))
2829 val
= TREE_OPERAND (t
, 2);
2831 val
= TREE_OPERAND (t
, 1);
2832 if (TREE_CODE (t
) == IF_STMT
&& !val
)
2834 return cxx_eval_constant_expression (ctx
, val
, lval
, non_constant_p
,
2835 overflow_p
, jump_target
);
2838 /* Subroutine of cxx_eval_constant_expression.
2839 Attempt to evaluate vector condition expressions. Unlike
2840 cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal
2841 ternary arithmetics operation, where all 3 arguments have to be
2842 evaluated as constants and then folding computes the result from
2846 cxx_eval_vector_conditional_expression (const constexpr_ctx
*ctx
, tree t
,
2847 bool *non_constant_p
, bool *overflow_p
)
2849 tree arg1
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
2851 non_constant_p
, overflow_p
);
2852 VERIFY_CONSTANT (arg1
);
2853 tree arg2
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
2855 non_constant_p
, overflow_p
);
2856 VERIFY_CONSTANT (arg2
);
2857 tree arg3
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 2),
2859 non_constant_p
, overflow_p
);
2860 VERIFY_CONSTANT (arg3
);
2861 location_t loc
= EXPR_LOCATION (t
);
2862 tree type
= TREE_TYPE (t
);
2863 tree r
= fold_ternary_loc (loc
, VEC_COND_EXPR
, type
, arg1
, arg2
, arg3
);
2866 if (arg1
== TREE_OPERAND (t
, 0)
2867 && arg2
== TREE_OPERAND (t
, 1)
2868 && arg3
== TREE_OPERAND (t
, 2))
2871 r
= build3_loc (loc
, VEC_COND_EXPR
, type
, arg1
, arg2
, arg3
);
2873 VERIFY_CONSTANT (r
);
2877 /* Returns less than, equal to, or greater than zero if KEY is found to be
2878 less than, to match, or to be greater than the constructor_elt's INDEX. */
2881 array_index_cmp (tree key
, tree index
)
2883 gcc_assert (TREE_CODE (key
) == INTEGER_CST
);
2885 switch (TREE_CODE (index
))
2888 return tree_int_cst_compare (key
, index
);
2891 tree lo
= TREE_OPERAND (index
, 0);
2892 tree hi
= TREE_OPERAND (index
, 1);
2893 if (tree_int_cst_lt (key
, lo
))
2895 else if (tree_int_cst_lt (hi
, key
))
2905 /* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1
2906 if none. If INSERT is true, insert a matching element rather than fail. */
2908 static HOST_WIDE_INT
2909 find_array_ctor_elt (tree ary
, tree dindex
, bool insert
)
2911 if (tree_int_cst_sgn (dindex
) < 0)
2914 unsigned HOST_WIDE_INT i
= tree_to_uhwi (dindex
);
2915 vec
<constructor_elt
, va_gc
> *elts
= CONSTRUCTOR_ELTS (ary
);
2916 unsigned HOST_WIDE_INT len
= vec_safe_length (elts
);
2918 unsigned HOST_WIDE_INT end
= len
;
2919 unsigned HOST_WIDE_INT begin
= 0;
2921 /* If the last element of the CONSTRUCTOR has its own index, we can assume
2922 that the same is true of the other elements and index directly. */
2925 tree cindex
= (*elts
)[end
- 1].index
;
2926 if (TREE_CODE (cindex
) == INTEGER_CST
2927 && compare_tree_int (cindex
, end
- 1) == 0)
2936 /* Otherwise, find a matching index by means of a binary search. */
2937 while (begin
!= end
)
2939 unsigned HOST_WIDE_INT middle
= (begin
+ end
) / 2;
2940 constructor_elt
&elt
= (*elts
)[middle
];
2941 tree idx
= elt
.index
;
2943 int cmp
= array_index_cmp (dindex
, idx
);
2950 if (insert
&& TREE_CODE (idx
) == RANGE_EXPR
)
2952 /* We need to split the range. */
2954 tree lo
= TREE_OPERAND (idx
, 0);
2955 tree hi
= TREE_OPERAND (idx
, 1);
2956 tree value
= elt
.value
;
2957 dindex
= fold_convert (sizetype
, dindex
);
2958 if (tree_int_cst_lt (lo
, dindex
))
2960 /* There are still some lower elts; shorten the range. */
2961 tree new_hi
= int_const_binop (MINUS_EXPR
, dindex
,
2963 if (tree_int_cst_equal (lo
, new_hi
))
2964 /* Only one element left, no longer a range. */
2967 TREE_OPERAND (idx
, 1) = new_hi
;
2968 /* Append the element we want to insert. */
2971 e
.value
= unshare_constructor (value
);
2972 vec_safe_insert (CONSTRUCTOR_ELTS (ary
), middle
, e
);
2975 /* No lower elts, the range elt is now ours. */
2978 if (tree_int_cst_lt (dindex
, hi
))
2980 /* There are still some higher elts; append a range. */
2981 tree new_lo
= int_const_binop (PLUS_EXPR
, dindex
,
2983 if (tree_int_cst_equal (new_lo
, hi
))
2986 e
.index
= build2 (RANGE_EXPR
, sizetype
, new_lo
, hi
);
2987 e
.value
= unshare_constructor (value
);
2988 vec_safe_insert (CONSTRUCTOR_ELTS (ary
), middle
+ 1, e
);
2997 constructor_elt e
= { dindex
, NULL_TREE
};
2998 vec_safe_insert (CONSTRUCTOR_ELTS (ary
), end
, e
);
3005 /* Under the control of CTX, issue a detailed diagnostic for
3006 an out-of-bounds subscript INDEX into the expression ARRAY. */
3009 diag_array_subscript (location_t loc
, const constexpr_ctx
*ctx
, tree array
, tree index
)
3013 tree arraytype
= TREE_TYPE (array
);
3015 /* Convert the unsigned array subscript to a signed integer to avoid
3016 printing huge numbers for small negative values. */
3017 tree sidx
= fold_convert (ssizetype
, index
);
3018 STRIP_ANY_LOCATION_WRAPPER (array
);
3021 if (TYPE_DOMAIN (arraytype
))
3022 error_at (loc
, "array subscript value %qE is outside the bounds "
3023 "of array %qD of type %qT", sidx
, array
, arraytype
);
3025 error_at (loc
, "nonzero array subscript %qE is used with array %qD of "
3026 "type %qT with unknown bounds", sidx
, array
, arraytype
);
3027 inform (DECL_SOURCE_LOCATION (array
), "declared here");
3029 else if (TYPE_DOMAIN (arraytype
))
3030 error_at (loc
, "array subscript value %qE is outside the bounds "
3031 "of array type %qT", sidx
, arraytype
);
3033 error_at (loc
, "nonzero array subscript %qE is used with array of type %qT "
3034 "with unknown bounds", sidx
, arraytype
);
3038 /* Return the number of elements for TYPE (which is an ARRAY_TYPE or
3042 get_array_or_vector_nelts (const constexpr_ctx
*ctx
, tree type
,
3043 bool *non_constant_p
, bool *overflow_p
)
3046 if (TREE_CODE (type
) == ARRAY_TYPE
)
3048 if (TYPE_DOMAIN (type
))
3049 nelts
= array_type_nelts_top (type
);
3051 nelts
= size_zero_node
;
3053 else if (VECTOR_TYPE_P (type
))
3054 nelts
= size_int (TYPE_VECTOR_SUBPARTS (type
));
3058 /* For VLAs, the number of elements won't be an integer constant. */
3059 nelts
= cxx_eval_constant_expression (ctx
, nelts
, false,
3060 non_constant_p
, overflow_p
);
3064 /* Extract element INDEX consisting of CHARS_PER_ELT chars from
3065 STRING_CST STRING. */
3068 extract_string_elt (tree string
, unsigned chars_per_elt
, unsigned index
)
3070 tree type
= cv_unqualified (TREE_TYPE (TREE_TYPE (string
)));
3073 if (chars_per_elt
== 1)
3074 r
= build_int_cst (type
, TREE_STRING_POINTER (string
)[index
]);
3077 const unsigned char *ptr
3078 = ((const unsigned char *)TREE_STRING_POINTER (string
)
3079 + index
* chars_per_elt
);
3080 r
= native_interpret_expr (type
, ptr
, chars_per_elt
);
3085 /* Subroutine of cxx_eval_array_reference. T is an ARRAY_REF; evaluate the
3086 subscript, diagnose any problems with it, and return the result. */
3089 eval_and_check_array_index (const constexpr_ctx
*ctx
,
3090 tree t
, bool allow_one_past
,
3091 bool *non_constant_p
, bool *overflow_p
)
3093 location_t loc
= cp_expr_loc_or_input_loc (t
);
3094 tree ary
= TREE_OPERAND (t
, 0);
3095 t
= TREE_OPERAND (t
, 1);
3096 tree index
= cxx_eval_constant_expression (ctx
, t
, false,
3097 non_constant_p
, overflow_p
);
3098 VERIFY_CONSTANT (index
);
3100 if (!tree_fits_shwi_p (index
)
3101 || tree_int_cst_sgn (index
) < 0)
3103 diag_array_subscript (loc
, ctx
, ary
, index
);
3104 *non_constant_p
= true;
3108 tree nelts
= get_array_or_vector_nelts (ctx
, TREE_TYPE (ary
), non_constant_p
,
3110 VERIFY_CONSTANT (nelts
);
3112 ? !tree_int_cst_le (index
, nelts
)
3113 : !tree_int_cst_lt (index
, nelts
))
3115 diag_array_subscript (loc
, ctx
, ary
, index
);
3116 *non_constant_p
= true;
3123 /* Subroutine of cxx_eval_constant_expression.
3124 Attempt to reduce a reference to an array slot. */
3127 cxx_eval_array_reference (const constexpr_ctx
*ctx
, tree t
,
3129 bool *non_constant_p
, bool *overflow_p
)
3131 tree oldary
= TREE_OPERAND (t
, 0);
3132 tree ary
= cxx_eval_constant_expression (ctx
, oldary
,
3134 non_constant_p
, overflow_p
);
3135 if (*non_constant_p
)
3138 && TREE_CODE (ary
) == VIEW_CONVERT_EXPR
3139 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary
, 0)))
3140 && TREE_TYPE (t
) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary
, 0))))
3141 ary
= TREE_OPERAND (ary
, 0);
3143 tree oldidx
= TREE_OPERAND (t
, 1);
3144 tree index
= eval_and_check_array_index (ctx
, t
, lval
,
3145 non_constant_p
, overflow_p
);
3146 if (*non_constant_p
)
3149 if (lval
&& ary
== oldary
&& index
== oldidx
)
3152 return build4 (ARRAY_REF
, TREE_TYPE (t
), ary
, index
, NULL
, NULL
);
3154 unsigned len
= 0, elem_nchars
= 1;
3155 tree elem_type
= TREE_TYPE (TREE_TYPE (ary
));
3156 if (TREE_CODE (ary
) == CONSTRUCTOR
)
3157 len
= CONSTRUCTOR_NELTS (ary
);
3158 else if (TREE_CODE (ary
) == STRING_CST
)
3160 elem_nchars
= (TYPE_PRECISION (elem_type
)
3161 / TYPE_PRECISION (char_type_node
));
3162 len
= (unsigned) TREE_STRING_LENGTH (ary
) / elem_nchars
;
3164 else if (TREE_CODE (ary
) == VECTOR_CST
)
3165 /* We don't create variable-length VECTOR_CSTs. */
3166 len
= VECTOR_CST_NELTS (ary
).to_constant ();
3169 /* We can't do anything with other tree codes, so use
3170 VERIFY_CONSTANT to complain and fail. */
3171 VERIFY_CONSTANT (ary
);
3176 HOST_WIDE_INT i
= 0;
3177 if (TREE_CODE (ary
) == CONSTRUCTOR
)
3179 HOST_WIDE_INT ix
= find_array_ctor_elt (ary
, index
);
3186 i
= tree_to_shwi (index
);
3193 if (TREE_CODE (ary
) == CONSTRUCTOR
)
3194 r
= (*CONSTRUCTOR_ELTS (ary
))[i
].value
;
3195 else if (TREE_CODE (ary
) == VECTOR_CST
)
3196 r
= VECTOR_CST_ELT (ary
, i
);
3198 r
= extract_string_elt (ary
, elem_nchars
, i
);
3201 /* Don't VERIFY_CONSTANT here. */
3204 /* Otherwise the element doesn't have a value yet. */
3209 if (TREE_CODE (ary
) == CONSTRUCTOR
3210 && CONSTRUCTOR_NO_CLEARING (ary
))
3212 /* 'ary' is part of the aggregate initializer we're currently
3213 building; if there's no initializer for this element yet,
3216 error ("accessing uninitialized array element");
3217 *non_constant_p
= true;
3221 /* If it's within the array bounds but doesn't have an explicit
3222 initializer, it's value-initialized. */
3223 tree val
= build_value_init (elem_type
, tf_warning_or_error
);
3224 return cxx_eval_constant_expression (ctx
, val
, lval
, non_constant_p
,
3228 /* Subroutine of cxx_eval_constant_expression.
3229 Attempt to reduce a field access of a value of class type. */
3232 cxx_eval_component_reference (const constexpr_ctx
*ctx
, tree t
,
3234 bool *non_constant_p
, bool *overflow_p
)
3236 unsigned HOST_WIDE_INT i
;
3239 tree part
= TREE_OPERAND (t
, 1);
3240 tree orig_whole
= TREE_OPERAND (t
, 0);
3241 tree whole
= cxx_eval_constant_expression (ctx
, orig_whole
,
3243 non_constant_p
, overflow_p
);
3244 if (INDIRECT_REF_P (whole
)
3245 && integer_zerop (TREE_OPERAND (whole
, 0)))
3248 error ("dereferencing a null pointer in %qE", orig_whole
);
3249 *non_constant_p
= true;
3253 if (TREE_CODE (whole
) == PTRMEM_CST
)
3254 whole
= cplus_expand_constant (whole
);
3255 if (whole
== orig_whole
)
3258 return fold_build3 (COMPONENT_REF
, TREE_TYPE (t
),
3259 whole
, part
, NULL_TREE
);
3260 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
3262 if (!*non_constant_p
&& TREE_CODE (whole
) != CONSTRUCTOR
)
3265 error ("%qE is not a constant expression", orig_whole
);
3266 *non_constant_p
= true;
3268 if (DECL_MUTABLE_P (part
))
3271 error ("mutable %qD is not usable in a constant expression", part
);
3272 *non_constant_p
= true;
3274 if (*non_constant_p
)
3276 bool pmf
= TYPE_PTRMEMFUNC_P (TREE_TYPE (whole
));
3277 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole
), i
, field
, value
)
3279 /* Use name match for PMF fields, as a variant will have a
3280 different FIELD_DECL with a different type. */
3281 if (pmf
? DECL_NAME (field
) == DECL_NAME (part
)
3286 STRIP_ANY_LOCATION_WRAPPER (value
);
3290 /* We're in the middle of initializing it. */
3294 if (TREE_CODE (TREE_TYPE (whole
)) == UNION_TYPE
3295 && CONSTRUCTOR_NELTS (whole
) > 0)
3297 /* DR 1188 says we don't have to deal with this. */
3299 error ("accessing %qD member instead of initialized %qD member in "
3300 "constant expression", part
, CONSTRUCTOR_ELT (whole
, 0)->index
);
3301 *non_constant_p
= true;
3305 /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty
3306 classes never get represented; throw together a value now. */
3307 if (is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
3308 return build_constructor (TREE_TYPE (t
), NULL
);
3310 gcc_assert (DECL_CONTEXT (part
) == TYPE_MAIN_VARIANT (TREE_TYPE (whole
)));
3312 if (CONSTRUCTOR_NO_CLEARING (whole
))
3314 /* 'whole' is part of the aggregate initializer we're currently
3315 building; if there's no initializer for this member yet, that's an
3318 error ("accessing uninitialized member %qD", part
);
3319 *non_constant_p
= true;
3323 /* If there's no explicit init for this field, it's value-initialized. */
3324 value
= build_value_init (TREE_TYPE (t
), tf_warning_or_error
);
3325 return cxx_eval_constant_expression (ctx
, value
,
3327 non_constant_p
, overflow_p
);
3330 /* Subroutine of cxx_eval_constant_expression.
3331 Attempt to reduce a field access of a value of class type that is
3332 expressed as a BIT_FIELD_REF. */
3335 cxx_eval_bit_field_ref (const constexpr_ctx
*ctx
, tree t
,
3337 bool *non_constant_p
, bool *overflow_p
)
3339 tree orig_whole
= TREE_OPERAND (t
, 0);
3340 tree retval
, fldval
, utype
, mask
;
3341 bool fld_seen
= false;
3342 HOST_WIDE_INT istart
, isize
;
3343 tree whole
= cxx_eval_constant_expression (ctx
, orig_whole
,
3345 non_constant_p
, overflow_p
);
3346 tree start
, field
, value
;
3347 unsigned HOST_WIDE_INT i
;
3349 if (whole
== orig_whole
)
3351 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
3353 if (!*non_constant_p
3354 && TREE_CODE (whole
) != VECTOR_CST
3355 && TREE_CODE (whole
) != CONSTRUCTOR
)
3358 error ("%qE is not a constant expression", orig_whole
);
3359 *non_constant_p
= true;
3361 if (*non_constant_p
)
3364 if (TREE_CODE (whole
) == VECTOR_CST
)
3365 return fold_ternary (BIT_FIELD_REF
, TREE_TYPE (t
), whole
,
3366 TREE_OPERAND (t
, 1), TREE_OPERAND (t
, 2));
3368 start
= TREE_OPERAND (t
, 2);
3369 istart
= tree_to_shwi (start
);
3370 isize
= tree_to_shwi (TREE_OPERAND (t
, 1));
3371 utype
= TREE_TYPE (t
);
3372 if (!TYPE_UNSIGNED (utype
))
3373 utype
= build_nonstandard_integer_type (TYPE_PRECISION (utype
), 1);
3374 retval
= build_int_cst (utype
, 0);
3375 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole
), i
, field
, value
)
3377 tree bitpos
= bit_position (field
);
3378 STRIP_ANY_LOCATION_WRAPPER (value
);
3379 if (bitpos
== start
&& DECL_SIZE (field
) == TREE_OPERAND (t
, 1))
3381 if (TREE_CODE (TREE_TYPE (field
)) == INTEGER_TYPE
3382 && TREE_CODE (value
) == INTEGER_CST
3383 && tree_fits_shwi_p (bitpos
)
3384 && tree_fits_shwi_p (DECL_SIZE (field
)))
3386 HOST_WIDE_INT bit
= tree_to_shwi (bitpos
);
3387 HOST_WIDE_INT sz
= tree_to_shwi (DECL_SIZE (field
));
3388 HOST_WIDE_INT shift
;
3389 if (bit
>= istart
&& bit
+ sz
<= istart
+ isize
)
3391 fldval
= fold_convert (utype
, value
);
3392 mask
= build_int_cst_type (utype
, -1);
3393 mask
= fold_build2 (LSHIFT_EXPR
, utype
, mask
,
3394 size_int (TYPE_PRECISION (utype
) - sz
));
3395 mask
= fold_build2 (RSHIFT_EXPR
, utype
, mask
,
3396 size_int (TYPE_PRECISION (utype
) - sz
));
3397 fldval
= fold_build2 (BIT_AND_EXPR
, utype
, fldval
, mask
);
3398 shift
= bit
- istart
;
3399 if (BYTES_BIG_ENDIAN
)
3400 shift
= TYPE_PRECISION (utype
) - shift
- sz
;
3401 fldval
= fold_build2 (LSHIFT_EXPR
, utype
, fldval
,
3403 retval
= fold_build2 (BIT_IOR_EXPR
, utype
, retval
, fldval
);
3409 return fold_convert (TREE_TYPE (t
), retval
);
3411 return error_mark_node
;
3414 /* Subroutine of cxx_eval_constant_expression.
3415 Evaluate a short-circuited logical expression T in the context
3416 of a given constexpr CALL. BAILOUT_VALUE is the value for
3417 early return. CONTINUE_VALUE is used here purely for
3418 sanity check purposes. */
3421 cxx_eval_logical_expression (const constexpr_ctx
*ctx
, tree t
,
3422 tree bailout_value
, tree continue_value
,
3424 bool *non_constant_p
, bool *overflow_p
)
3427 tree lhs
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
3429 non_constant_p
, overflow_p
);
3430 VERIFY_CONSTANT (lhs
);
3431 if (tree_int_cst_equal (lhs
, bailout_value
))
3433 gcc_assert (tree_int_cst_equal (lhs
, continue_value
));
3434 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
3435 lval
, non_constant_p
,
3437 VERIFY_CONSTANT (r
);
3441 /* REF is a COMPONENT_REF designating a particular field. V is a vector of
3442 CONSTRUCTOR elements to initialize (part of) an object containing that
3443 field. Return a pointer to the constructor_elt corresponding to the
3444 initialization of the field. */
3446 static constructor_elt
*
3447 base_field_constructor_elt (vec
<constructor_elt
, va_gc
> *v
, tree ref
)
3449 tree aggr
= TREE_OPERAND (ref
, 0);
3450 tree field
= TREE_OPERAND (ref
, 1);
3452 constructor_elt
*ce
;
3454 gcc_assert (TREE_CODE (ref
) == COMPONENT_REF
);
3456 if (TREE_CODE (aggr
) == COMPONENT_REF
)
3458 constructor_elt
*base_ce
3459 = base_field_constructor_elt (v
, aggr
);
3460 v
= CONSTRUCTOR_ELTS (base_ce
->value
);
3463 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
3464 if (ce
->index
== field
)
3471 /* Some of the expressions fed to the constexpr mechanism are calls to
3472 constructors, which have type void. In that case, return the type being
3473 initialized by the constructor. */
3476 initialized_type (tree t
)
3480 tree type
= TREE_TYPE (t
);
3481 if (TREE_CODE (t
) == CALL_EXPR
)
3483 /* A constructor call has void type, so we need to look deeper. */
3484 tree fn
= get_function_named_in_call (t
);
3485 if (fn
&& TREE_CODE (fn
) == FUNCTION_DECL
3486 && DECL_CXX_CONSTRUCTOR_P (fn
))
3487 type
= DECL_CONTEXT (fn
);
3489 else if (TREE_CODE (t
) == COMPOUND_EXPR
)
3490 return initialized_type (TREE_OPERAND (t
, 1));
3491 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
)
3492 type
= TREE_TYPE (AGGR_INIT_EXPR_SLOT (t
));
3493 return cv_unqualified (type
);
3496 /* We're about to initialize element INDEX of an array or class from VALUE.
3497 Set up NEW_CTX appropriately by adjusting .object to refer to the
3498 subobject and creating a new CONSTRUCTOR if the element is itself
3499 a class or array. */
3502 init_subob_ctx (const constexpr_ctx
*ctx
, constexpr_ctx
&new_ctx
,
3503 tree index
, tree
&value
)
3507 if (index
&& TREE_CODE (index
) != INTEGER_CST
3508 && TREE_CODE (index
) != FIELD_DECL
)
3509 /* This won't have an element in the new CONSTRUCTOR. */
3512 tree type
= initialized_type (value
);
3513 if (!AGGREGATE_TYPE_P (type
) && !VECTOR_TYPE_P (type
))
3514 /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
3517 /* The sub-aggregate initializer might contain a placeholder;
3518 update object to refer to the subobject and ctor to refer to
3519 the (newly created) sub-initializer. */
3521 new_ctx
.object
= build_ctor_subob_ref (index
, type
, ctx
->object
);
3522 tree elt
= build_constructor (type
, NULL
);
3523 CONSTRUCTOR_NO_CLEARING (elt
) = true;
3526 if (TREE_CODE (value
) == TARGET_EXPR
)
3527 /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
3528 value
= TARGET_EXPR_INITIAL (value
);
3531 /* We're about to process an initializer for a class or array TYPE. Make
3532 sure that CTX is set up appropriately. */
3535 verify_ctor_sanity (const constexpr_ctx
*ctx
, tree type
)
3537 /* We don't bother building a ctor for an empty base subobject. */
3538 if (is_empty_class (type
))
3541 /* We're in the middle of an initializer that might involve placeholders;
3542 our caller should have created a CONSTRUCTOR for us to put the
3543 initializer into. We will either return that constructor or T. */
3544 gcc_assert (ctx
->ctor
);
3545 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3546 (type
, TREE_TYPE (ctx
->ctor
)));
3547 /* We used to check that ctx->ctor was empty, but that isn't the case when
3548 the object is zero-initialized before calling the constructor. */
3551 tree otype
= TREE_TYPE (ctx
->object
);
3552 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type
, otype
)
3553 /* Handle flexible array members. */
3554 || (TREE_CODE (otype
) == ARRAY_TYPE
3555 && TYPE_DOMAIN (otype
) == NULL_TREE
3556 && TREE_CODE (type
) == ARRAY_TYPE
3557 && (same_type_ignoring_top_level_qualifiers_p
3558 (TREE_TYPE (type
), TREE_TYPE (otype
)))));
3560 gcc_assert (!ctx
->object
|| !DECL_P (ctx
->object
)
3561 || *(ctx
->global
->values
.get (ctx
->object
)) == ctx
->ctor
);
3564 /* Subroutine of cxx_eval_constant_expression.
3565 The expression tree T denotes a C-style array or a C-style
3566 aggregate. Reduce it to a constant expression. */
3569 cxx_eval_bare_aggregate (const constexpr_ctx
*ctx
, tree t
,
3571 bool *non_constant_p
, bool *overflow_p
)
3573 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (t
);
3574 bool changed
= false;
3575 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t
));
3576 tree type
= TREE_TYPE (t
);
3578 constexpr_ctx new_ctx
;
3579 if (TYPE_PTRMEMFUNC_P (type
) || VECTOR_TYPE_P (type
))
3581 /* We don't really need the ctx->ctor business for a PMF or
3582 vector, but it's simpler to use the same code. */
3584 new_ctx
.ctor
= build_constructor (type
, NULL
);
3585 new_ctx
.object
= NULL_TREE
;
3588 verify_ctor_sanity (ctx
, type
);
3589 vec
<constructor_elt
, va_gc
> **p
= &CONSTRUCTOR_ELTS (ctx
->ctor
);
3590 vec_alloc (*p
, vec_safe_length (v
));
3594 bool constant_p
= true;
3595 bool side_effects_p
= false;
3596 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, index
, value
)
3598 tree orig_value
= value
;
3599 init_subob_ctx (ctx
, new_ctx
, index
, value
);
3600 if (new_ctx
.ctor
!= ctx
->ctor
)
3601 /* If we built a new CONSTRUCTOR, attach it now so that other
3602 initializers can refer to it. */
3603 CONSTRUCTOR_APPEND_ELT (*p
, index
, new_ctx
.ctor
);
3604 tree elt
= cxx_eval_constant_expression (&new_ctx
, value
,
3606 non_constant_p
, overflow_p
);
3607 /* Don't VERIFY_CONSTANT here. */
3608 if (ctx
->quiet
&& *non_constant_p
)
3610 if (elt
!= orig_value
)
3613 if (!TREE_CONSTANT (elt
))
3615 if (TREE_SIDE_EFFECTS (elt
))
3616 side_effects_p
= true;
3617 if (index
&& TREE_CODE (index
) == COMPONENT_REF
)
3619 /* This is an initialization of a vfield inside a base
3620 subaggregate that we already initialized; push this
3621 initialization into the previous initialization. */
3622 constructor_elt
*inner
= base_field_constructor_elt (*p
, index
);
3627 && (TREE_CODE (index
) == NOP_EXPR
3628 || TREE_CODE (index
) == POINTER_PLUS_EXPR
))
3630 /* This is an initializer for an empty base; now that we've
3631 checked that it's constant, we can ignore it. */
3632 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index
))));
3637 if (new_ctx
.ctor
!= ctx
->ctor
)
3639 /* We appended this element above; update the value. */
3640 gcc_assert ((*p
)->last().index
== index
);
3641 (*p
)->last().value
= elt
;
3644 CONSTRUCTOR_APPEND_ELT (*p
, index
, elt
);
3645 /* Adding or replacing an element might change the ctor's flags. */
3646 TREE_CONSTANT (ctx
->ctor
) = constant_p
;
3647 TREE_SIDE_EFFECTS (ctx
->ctor
) = side_effects_p
;
3650 if (*non_constant_p
|| !changed
)
3653 /* We're done building this CONSTRUCTOR, so now we can interpret an
3654 element without an explicit initializer as value-initialized. */
3655 CONSTRUCTOR_NO_CLEARING (t
) = false;
3656 TREE_CONSTANT (t
) = constant_p
;
3657 TREE_SIDE_EFFECTS (t
) = side_effects_p
;
3658 if (VECTOR_TYPE_P (type
))
3663 /* Subroutine of cxx_eval_constant_expression.
3664 The expression tree T is a VEC_INIT_EXPR which denotes the desired
3665 initialization of a non-static data member of array type. Reduce it to a
3668 Note that apart from value-initialization (when VALUE_INIT is true),
3669 this is only intended to support value-initialization and the
3670 initializations done by defaulted constructors for classes with
3671 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
3672 will either be NULL_TREE for the default constructor, or a COMPONENT_REF
3673 for the copy/move constructor. */
3676 cxx_eval_vec_init_1 (const constexpr_ctx
*ctx
, tree atype
, tree init
,
3677 bool value_init
, bool lval
,
3678 bool *non_constant_p
, bool *overflow_p
)
3680 tree elttype
= TREE_TYPE (atype
);
3681 verify_ctor_sanity (ctx
, atype
);
3682 vec
<constructor_elt
, va_gc
> **p
= &CONSTRUCTOR_ELTS (ctx
->ctor
);
3683 bool pre_init
= false;
3684 unsigned HOST_WIDE_INT i
;
3685 tsubst_flags_t complain
= ctx
->quiet
? tf_none
: tf_warning_or_error
;
3687 /* For the default constructor, build up a call to the default
3688 constructor of the element type. We only need to handle class types
3689 here, as for a constructor to be constexpr, all members must be
3690 initialized, which for a defaulted default constructor means they must
3691 be of a class type with a constexpr default constructor. */
3692 if (TREE_CODE (elttype
) == ARRAY_TYPE
)
3693 /* We only do this at the lowest level. */;
3694 else if (value_init
)
3696 init
= build_value_init (elttype
, complain
);
3701 releasing_vec argvec
;
3702 init
= build_special_member_call (NULL_TREE
, complete_ctor_identifier
,
3703 &argvec
, elttype
, LOOKUP_NORMAL
,
3705 init
= build_aggr_init_expr (elttype
, init
);
3709 tree nelts
= get_array_or_vector_nelts (ctx
, atype
, non_constant_p
,
3711 unsigned HOST_WIDE_INT max
= tree_to_uhwi (nelts
);
3712 for (i
= 0; i
< max
; ++i
)
3714 tree idx
= build_int_cst (size_type_node
, i
);
3717 constexpr_ctx new_ctx
;
3718 init_subob_ctx (ctx
, new_ctx
, idx
, pre_init
? init
: elttype
);
3719 if (new_ctx
.ctor
!= ctx
->ctor
)
3720 CONSTRUCTOR_APPEND_ELT (*p
, idx
, new_ctx
.ctor
);
3721 if (TREE_CODE (elttype
) == ARRAY_TYPE
)
3723 /* A multidimensional array; recurse. */
3724 if (value_init
|| init
== NULL_TREE
)
3726 eltinit
= NULL_TREE
;
3730 eltinit
= cp_build_array_ref (input_location
, init
, idx
, complain
);
3731 eltinit
= cxx_eval_vec_init_1 (&new_ctx
, elttype
, eltinit
, value_init
,
3733 non_constant_p
, overflow_p
);
3737 /* Initializing an element using value or default initialization
3738 we just pre-built above. */
3739 if (init
== void_node
)
3740 /* Trivial default-init, don't do anything to the CONSTRUCTOR. */
3742 eltinit
= cxx_eval_constant_expression (&new_ctx
, init
, lval
,
3743 non_constant_p
, overflow_p
);
3748 /* Copying an element. */
3749 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3750 (atype
, TREE_TYPE (init
)));
3751 eltinit
= cp_build_array_ref (input_location
, init
, idx
, complain
);
3752 if (!lvalue_p (init
))
3753 eltinit
= move (eltinit
);
3754 eltinit
= force_rvalue (eltinit
, complain
);
3755 eltinit
= cxx_eval_constant_expression (&new_ctx
, eltinit
, lval
,
3756 non_constant_p
, overflow_p
);
3758 if (*non_constant_p
&& !ctx
->quiet
)
3760 if (new_ctx
.ctor
!= ctx
->ctor
)
3762 /* We appended this element above; update the value. */
3763 gcc_assert ((*p
)->last().index
== idx
);
3764 (*p
)->last().value
= eltinit
;
3767 CONSTRUCTOR_APPEND_ELT (*p
, idx
, eltinit
);
3768 /* Reuse the result of cxx_eval_constant_expression call
3769 from the first iteration to all others if it is a constant
3770 initializer that doesn't require relocations. */
3773 && (eltinit
== NULL_TREE
3774 || (initializer_constant_valid_p (eltinit
, TREE_TYPE (eltinit
))
3775 == null_pointer_node
)))
3777 if (new_ctx
.ctor
!= ctx
->ctor
)
3778 eltinit
= new_ctx
.ctor
;
3779 tree range
= build2 (RANGE_EXPR
, size_type_node
,
3780 build_int_cst (size_type_node
, 1),
3781 build_int_cst (size_type_node
, max
- 1));
3782 CONSTRUCTOR_APPEND_ELT (*p
, range
, unshare_constructor (eltinit
));
3786 vec_safe_reserve (*p
, max
);
3789 if (!*non_constant_p
)
3792 CONSTRUCTOR_NO_CLEARING (init
) = false;
3798 cxx_eval_vec_init (const constexpr_ctx
*ctx
, tree t
,
3800 bool *non_constant_p
, bool *overflow_p
)
3802 tree atype
= TREE_TYPE (t
);
3803 tree init
= VEC_INIT_EXPR_INIT (t
);
3804 tree r
= cxx_eval_vec_init_1 (ctx
, atype
, init
,
3805 VEC_INIT_EXPR_VALUE_INIT (t
),
3806 lval
, non_constant_p
, overflow_p
);
3807 if (*non_constant_p
)
3813 /* Like same_type_ignoring_top_level_qualifiers_p, but also handle the case
3814 where the desired type is an array of unknown bounds because the variable
3815 has had its bounds deduced since the wrapping expression was created. */
3818 same_type_ignoring_tlq_and_bounds_p (tree type1
, tree type2
)
3820 while (TREE_CODE (type1
) == ARRAY_TYPE
3821 && TREE_CODE (type2
) == ARRAY_TYPE
3822 && (!TYPE_DOMAIN (type1
) || !TYPE_DOMAIN (type2
)))
3824 type1
= TREE_TYPE (type1
);
3825 type2
= TREE_TYPE (type2
);
3827 return same_type_ignoring_top_level_qualifiers_p (type1
, type2
);
3830 /* Helper function for cxx_fold_indirect_ref_1, called recursively. */
3833 cxx_fold_indirect_ref_1 (location_t loc
, tree type
, tree op
,
3834 unsigned HOST_WIDE_INT off
, bool *empty_base
)
3836 tree optype
= TREE_TYPE (op
);
3837 unsigned HOST_WIDE_INT const_nunits
;
3840 if (similar_type_p (optype
, type
))
3842 /* Also handle conversion to an empty base class, which
3843 is represented with a NOP_EXPR. */
3844 /* *(foo *)&complexfoo => __real__ complexfoo */
3845 else if (TREE_CODE (optype
) == COMPLEX_TYPE
3846 && similar_type_p (type
, TREE_TYPE (optype
)))
3847 return build1_loc (loc
, REALPART_EXPR
, type
, op
);
3849 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
3850 else if (TREE_CODE (optype
) == COMPLEX_TYPE
3851 && similar_type_p (type
, TREE_TYPE (optype
))
3852 && tree_to_uhwi (TYPE_SIZE_UNIT (type
)) == off
)
3853 return build1_loc (loc
, IMAGPART_EXPR
, type
, op
);
3854 if (is_empty_class (type
)
3855 && CLASS_TYPE_P (optype
)
3856 && DERIVED_FROM_P (type
, optype
))
3861 /* ((foo*)&vectorfoo)[x] => BIT_FIELD_REF<vectorfoo,...> */
3862 else if (VECTOR_TYPE_P (optype
)
3863 && similar_type_p (type
, TREE_TYPE (optype
))
3864 && TYPE_VECTOR_SUBPARTS (optype
).is_constant (&const_nunits
))
3866 unsigned HOST_WIDE_INT part_width
= tree_to_uhwi (TYPE_SIZE_UNIT (type
));
3867 unsigned HOST_WIDE_INT max_offset
= part_width
* const_nunits
;
3868 if (off
< max_offset
&& off
% part_width
== 0)
3870 tree index
= bitsize_int (off
* BITS_PER_UNIT
);
3871 return build3_loc (loc
, BIT_FIELD_REF
, type
, op
,
3872 TYPE_SIZE (type
), index
);
3875 /* ((foo *)&fooarray)[x] => fooarray[x] */
3876 else if (TREE_CODE (optype
) == ARRAY_TYPE
3877 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (optype
)))
3878 && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (optype
))))
3880 tree type_domain
= TYPE_DOMAIN (optype
);
3881 tree min_val
= size_zero_node
;
3882 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
3883 min_val
= TYPE_MIN_VALUE (type_domain
);
3884 unsigned HOST_WIDE_INT el_sz
3885 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (optype
)));
3886 unsigned HOST_WIDE_INT idx
= off
/ el_sz
;
3887 unsigned HOST_WIDE_INT rem
= off
% el_sz
;
3888 if (tree_fits_uhwi_p (min_val
))
3890 tree index
= size_int (idx
+ tree_to_uhwi (min_val
));
3891 op
= build4_loc (loc
, ARRAY_REF
, TREE_TYPE (optype
), op
, index
,
3892 NULL_TREE
, NULL_TREE
);
3893 return cxx_fold_indirect_ref_1 (loc
, type
, op
, rem
,
3897 /* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */
3898 else if (TREE_CODE (optype
) == RECORD_TYPE
)
3900 for (tree field
= TYPE_FIELDS (optype
);
3901 field
; field
= DECL_CHAIN (field
))
3902 if (TREE_CODE (field
) == FIELD_DECL
3903 && TREE_TYPE (field
) != error_mark_node
3904 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (field
))))
3906 tree pos
= byte_position (field
);
3907 if (!tree_fits_uhwi_p (pos
))
3909 unsigned HOST_WIDE_INT upos
= tree_to_uhwi (pos
);
3911 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field
)));
3912 if (upos
<= off
&& off
< upos
+ el_sz
)
3914 tree cop
= build3 (COMPONENT_REF
, TREE_TYPE (field
),
3915 op
, field
, NULL_TREE
);
3916 if (tree ret
= cxx_fold_indirect_ref_1 (loc
, type
, cop
,
3927 /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
3928 match. We want to be less strict for simple *& folding; if we have a
3929 non-const temporary that we access through a const pointer, that should
3930 work. We handle this here rather than change fold_indirect_ref_1
3931 because we're dealing with things like ADDR_EXPR of INTEGER_CST which
3932 don't really make sense outside of constant expression evaluation. Also
3933 we want to allow folding to COMPONENT_REF, which could cause trouble
3934 with TBAA in fold_indirect_ref_1. */
3937 cxx_fold_indirect_ref (location_t loc
, tree type
, tree op0
, bool *empty_base
)
3941 poly_uint64 const_op01
;
3944 subtype
= TREE_TYPE (sub
);
3945 if (!INDIRECT_TYPE_P (subtype
))
3948 if (TREE_CODE (sub
) == ADDR_EXPR
)
3950 tree op
= TREE_OPERAND (sub
, 0);
3951 tree optype
= TREE_TYPE (op
);
3953 /* *&CONST_DECL -> to the value of the const decl. */
3954 if (TREE_CODE (op
) == CONST_DECL
)
3955 return DECL_INITIAL (op
);
3956 /* *&p => p; make sure to handle *&"str"[cst] here. */
3957 if (similar_type_p (optype
, type
))
3959 tree fop
= fold_read_from_constant_string (op
);
3966 return cxx_fold_indirect_ref_1 (loc
, type
, op
, 0, empty_base
);
3968 else if (TREE_CODE (sub
) == POINTER_PLUS_EXPR
3969 && tree_fits_uhwi_p (TREE_OPERAND (sub
, 1)))
3971 tree op00
= TREE_OPERAND (sub
, 0);
3972 tree op01
= TREE_OPERAND (sub
, 1);
3975 if (TREE_CODE (op00
) == ADDR_EXPR
)
3976 return cxx_fold_indirect_ref_1 (loc
, type
, TREE_OPERAND (op00
, 0),
3977 tree_to_uhwi (op01
), empty_base
);
3979 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
3980 else if (TREE_CODE (TREE_TYPE (subtype
)) == ARRAY_TYPE
3981 && similar_type_p (type
, TREE_TYPE (TREE_TYPE (subtype
))))
3984 tree min_val
= size_zero_node
;
3986 = cxx_fold_indirect_ref (loc
, TREE_TYPE (subtype
), sub
, NULL
);
3990 sub
= build1_loc (loc
, INDIRECT_REF
, TREE_TYPE (subtype
), sub
);
3991 type_domain
= TYPE_DOMAIN (TREE_TYPE (sub
));
3992 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
3993 min_val
= TYPE_MIN_VALUE (type_domain
);
3994 return build4_loc (loc
, ARRAY_REF
, type
, sub
, min_val
, NULL_TREE
,
4002 cxx_eval_indirect_ref (const constexpr_ctx
*ctx
, tree t
,
4004 bool *non_constant_p
, bool *overflow_p
)
4006 tree orig_op0
= TREE_OPERAND (t
, 0);
4007 bool empty_base
= false;
4009 /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second
4010 operand is an integer-zero. Otherwise reject the MEM_REF for now. */
4012 if (TREE_CODE (t
) == MEM_REF
4013 && (!TREE_OPERAND (t
, 1) || !integer_zerop (TREE_OPERAND (t
, 1))))
4015 gcc_assert (ctx
->quiet
);
4016 *non_constant_p
= true;
4020 /* First try to simplify it directly. */
4021 tree r
= cxx_fold_indirect_ref (EXPR_LOCATION (t
), TREE_TYPE (t
), orig_op0
,
4025 /* If that didn't work, evaluate the operand first. */
4026 tree op0
= cxx_eval_constant_expression (ctx
, orig_op0
,
4027 /*lval*/false, non_constant_p
,
4029 /* Don't VERIFY_CONSTANT here. */
4030 if (*non_constant_p
)
4033 if (!lval
&& integer_zerop (op0
))
4036 error ("dereferencing a null pointer");
4037 *non_constant_p
= true;
4041 r
= cxx_fold_indirect_ref (EXPR_LOCATION (t
), TREE_TYPE (t
), op0
,
4045 /* We couldn't fold to a constant value. Make sure it's not
4046 something we should have been able to fold. */
4049 if (TREE_CODE (sub
) == ADDR_EXPR
)
4051 gcc_assert (!similar_type_p
4052 (TREE_TYPE (TREE_TYPE (sub
)), TREE_TYPE (t
)));
4053 /* DR 1188 says we don't have to deal with this. */
4055 error_at (cp_expr_loc_or_input_loc (t
),
4056 "accessing value of %qE through a %qT glvalue in a "
4057 "constant expression", build_fold_indirect_ref (sub
),
4059 *non_constant_p
= true;
4063 if (lval
&& op0
!= orig_op0
)
4064 return build1 (INDIRECT_REF
, TREE_TYPE (t
), op0
);
4066 VERIFY_CONSTANT (t
);
4071 r
= cxx_eval_constant_expression (ctx
, r
,
4072 lval
, non_constant_p
, overflow_p
);
4073 if (*non_constant_p
)
4076 /* If we're pulling out the value of an empty base, just return an empty
4078 if (empty_base
&& !lval
)
4080 r
= build_constructor (TREE_TYPE (t
), NULL
);
4081 TREE_CONSTANT (r
) = true;
4087 /* Complain about R, a VAR_DECL, not being usable in a constant expression.
4088 Shared between potential_constant_expression and
4089 cxx_eval_constant_expression. */
4092 non_const_var_error (location_t loc
, tree r
)
4094 auto_diagnostic_group d
;
4095 tree type
= TREE_TYPE (r
);
4096 if (DECL_NAME (r
) == heap_uninit_identifier
4097 || DECL_NAME (r
) == heap_identifier
)
4099 error_at (loc
, "the content of uninitialized storage is not usable "
4100 "in a constant expression");
4101 inform (DECL_SOURCE_LOCATION (r
), "allocated here");
4104 if (DECL_NAME (r
) == heap_deleted_identifier
)
4106 error_at (loc
, "use of allocated storage after deallocation in a "
4107 "constant expression");
4108 inform (DECL_SOURCE_LOCATION (r
), "allocated here");
4111 error_at (loc
, "the value of %qD is not usable in a constant "
4113 /* Avoid error cascade. */
4114 if (DECL_INITIAL (r
) == error_mark_node
)
4116 if (DECL_DECLARED_CONSTEXPR_P (r
))
4117 inform (DECL_SOURCE_LOCATION (r
),
4118 "%qD used in its own initializer", r
);
4119 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
4121 if (!CP_TYPE_CONST_P (type
))
4122 inform (DECL_SOURCE_LOCATION (r
),
4123 "%q#D is not const", r
);
4124 else if (CP_TYPE_VOLATILE_P (type
))
4125 inform (DECL_SOURCE_LOCATION (r
),
4126 "%q#D is volatile", r
);
4127 else if (!DECL_INITIAL (r
)
4128 || !TREE_CONSTANT (DECL_INITIAL (r
))
4129 || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
))
4130 inform (DECL_SOURCE_LOCATION (r
),
4131 "%qD was not initialized with a constant "
4136 else if (TYPE_REF_P (type
))
4137 inform (DECL_SOURCE_LOCATION (r
),
4138 "%qD was not initialized with a constant "
4142 if (cxx_dialect
>= cxx11
&& !DECL_DECLARED_CONSTEXPR_P (r
))
4143 inform (DECL_SOURCE_LOCATION (r
),
4144 "%qD was not declared %<constexpr%>", r
);
4146 inform (DECL_SOURCE_LOCATION (r
),
4147 "%qD does not have integral or enumeration type",
4152 /* Subroutine of cxx_eval_constant_expression.
4153 Like cxx_eval_unary_expression, except for trinary expressions. */
4156 cxx_eval_trinary_expression (const constexpr_ctx
*ctx
, tree t
,
4158 bool *non_constant_p
, bool *overflow_p
)
4164 for (i
= 0; i
< 3; i
++)
4166 args
[i
] = cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, i
),
4168 non_constant_p
, overflow_p
);
4169 VERIFY_CONSTANT (args
[i
]);
4172 val
= fold_ternary_loc (EXPR_LOCATION (t
), TREE_CODE (t
), TREE_TYPE (t
),
4173 args
[0], args
[1], args
[2]);
4174 if (val
== NULL_TREE
)
4176 VERIFY_CONSTANT (val
);
4180 /* True if T was declared in a function declared to be constexpr, and
4181 therefore potentially constant in C++14. */
4184 var_in_constexpr_fn (tree t
)
4186 tree ctx
= DECL_CONTEXT (t
);
4187 return (ctx
&& TREE_CODE (ctx
) == FUNCTION_DECL
4188 && DECL_DECLARED_CONSTEXPR_P (ctx
));
4191 /* True if T was declared in a function that might be constexpr: either a
4192 function that was declared constexpr, or a C++17 lambda op(). */
4195 var_in_maybe_constexpr_fn (tree t
)
4197 if (cxx_dialect
>= cxx17
4198 && DECL_FUNCTION_SCOPE_P (t
)
4199 && LAMBDA_FUNCTION_P (DECL_CONTEXT (t
)))
4201 return var_in_constexpr_fn (t
);
4204 /* We're assigning INIT to TARGET. In do_build_copy_constructor and
4205 build_over_call we implement trivial copy of a class with tail padding using
4206 assignment of character arrays, which is valid in normal code, but not in
4207 constexpr evaluation. We don't need to worry about clobbering tail padding
4208 in constexpr evaluation, so strip the type punning. */
4211 maybe_simplify_trivial_copy (tree
&target
, tree
&init
)
4213 if (TREE_CODE (target
) == MEM_REF
4214 && TREE_CODE (init
) == MEM_REF
4215 && TREE_TYPE (target
) == TREE_TYPE (init
)
4216 && TREE_CODE (TREE_TYPE (target
)) == ARRAY_TYPE
4217 && TREE_TYPE (TREE_TYPE (target
)) == unsigned_char_type_node
)
4219 target
= build_fold_indirect_ref (TREE_OPERAND (target
, 0));
4220 init
= build_fold_indirect_ref (TREE_OPERAND (init
, 0));
4224 /* Return true if we are modifying something that is const during constant
4225 expression evaluation. CODE is the code of the statement, OBJ is the
4226 object in question, MUTABLE_P is true if one of the subobjects were
4227 declared mutable. */
4230 modifying_const_object_p (tree_code code
, tree obj
, bool mutable_p
)
4232 /* If this is initialization, there's no problem. */
4233 if (code
!= MODIFY_EXPR
)
4236 /* [basic.type.qualifier] "A const object is an object of type
4237 const T or a non-mutable subobject of a const object." */
4241 return (TREE_READONLY (obj
) || CP_TYPE_CONST_P (TREE_TYPE (obj
)));
4244 /* Evaluate an INIT_EXPR or MODIFY_EXPR. */
4247 cxx_eval_store_expression (const constexpr_ctx
*ctx
, tree t
,
4249 bool *non_constant_p
, bool *overflow_p
)
4251 constexpr_ctx new_ctx
= *ctx
;
4253 tree init
= TREE_OPERAND (t
, 1);
4254 if (TREE_CLOBBER_P (init
))
4255 /* Just ignore clobbers. */
4258 /* First we figure out where we're storing to. */
4259 tree target
= TREE_OPERAND (t
, 0);
4261 maybe_simplify_trivial_copy (target
, init
);
4263 tree type
= TREE_TYPE (target
);
4264 bool preeval
= SCALAR_TYPE_P (type
) || TREE_CODE (t
) == MODIFY_EXPR
;
4267 /* Evaluate the value to be stored without knowing what object it will be
4268 stored in, so that any side-effects happen first. */
4269 if (!SCALAR_TYPE_P (type
))
4270 new_ctx
.ctor
= new_ctx
.object
= NULL_TREE
;
4271 init
= cxx_eval_constant_expression (&new_ctx
, init
, false,
4272 non_constant_p
, overflow_p
);
4273 if (*non_constant_p
)
4277 bool evaluated
= false;
4280 /* If we want to return a reference to the target, we need to evaluate it
4281 as a whole; otherwise, only evaluate the innermost piece to avoid
4282 building up unnecessary *_REFs. */
4283 target
= cxx_eval_constant_expression (ctx
, target
, true,
4284 non_constant_p
, overflow_p
);
4286 if (*non_constant_p
)
4290 /* Find the underlying variable. */
4292 tree object
= NULL_TREE
;
4293 /* If we're modifying a const object, save it. */
4294 tree const_object_being_modified
= NULL_TREE
;
4295 bool mutable_p
= false;
4296 for (tree probe
= target
; object
== NULL_TREE
; )
4298 switch (TREE_CODE (probe
))
4304 tree ob
= TREE_OPERAND (probe
, 0);
4305 tree elt
= TREE_OPERAND (probe
, 1);
4306 if (TREE_CODE (elt
) == FIELD_DECL
&& DECL_MUTABLE_P (elt
))
4308 if (TREE_CODE (probe
) == ARRAY_REF
)
4310 elt
= eval_and_check_array_index (ctx
, probe
, false,
4311 non_constant_p
, overflow_p
);
4312 if (*non_constant_p
)
4315 /* We don't check modifying_const_object_p for ARRAY_REFs. Given
4316 "int a[10]", an ARRAY_REF "a[2]" can be "const int", even though
4317 the array isn't const. Instead, check "a" in the next iteration;
4318 that will detect modifying "const int a[10]". */
4320 && modifying_const_object_p (TREE_CODE (t
), probe
,
4322 && const_object_being_modified
== NULL_TREE
)
4323 const_object_being_modified
= probe
;
4324 vec_safe_push (refs
, elt
);
4325 vec_safe_push (refs
, TREE_TYPE (probe
));
4335 probe
= cxx_eval_constant_expression (ctx
, probe
, true,
4336 non_constant_p
, overflow_p
);
4338 if (*non_constant_p
)
4345 if (modifying_const_object_p (TREE_CODE (t
), object
, mutable_p
)
4346 && const_object_being_modified
== NULL_TREE
)
4347 const_object_being_modified
= object
;
4349 /* And then find/build up our initializer for the path to the subobject
4350 we're initializing. */
4352 if (object
== ctx
->object
&& VAR_P (object
)
4353 && DECL_NAME (object
) && ctx
->call
== NULL
)
4354 /* The variable we're building up an aggregate initializer for is outside
4355 the constant-expression, so don't evaluate the store. We check
4356 DECL_NAME to handle TARGET_EXPR temporaries, which are fair game. */
4358 else if (DECL_P (object
))
4359 valp
= ctx
->global
->values
.get (object
);
4364 /* A constant-expression cannot modify objects from outside the
4365 constant-expression. */
4367 error ("modification of %qE is not a constant expression", object
);
4368 *non_constant_p
= true;
4371 type
= TREE_TYPE (object
);
4372 bool no_zero_init
= true;
4374 releasing_vec ctors
;
4375 while (!refs
->is_empty ())
4377 if (*valp
== NULL_TREE
)
4379 *valp
= build_constructor (type
, NULL
);
4380 CONSTRUCTOR_NO_CLEARING (*valp
) = no_zero_init
;
4382 else if (TREE_CODE (*valp
) == STRING_CST
)
4384 /* An array was initialized with a string constant, and now
4385 we're writing into one of its elements. Explode the
4386 single initialization into a set of element
4388 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
4390 tree string
= *valp
;
4391 tree elt_type
= TREE_TYPE (type
);
4392 unsigned chars_per_elt
= (TYPE_PRECISION (elt_type
)
4393 / TYPE_PRECISION (char_type_node
));
4394 unsigned num_elts
= TREE_STRING_LENGTH (string
) / chars_per_elt
;
4395 tree ary_ctor
= build_constructor (type
, NULL
);
4397 vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor
), num_elts
);
4398 for (unsigned ix
= 0; ix
!= num_elts
; ix
++)
4400 constructor_elt elt
=
4402 build_int_cst (size_type_node
, ix
),
4403 extract_string_elt (string
, chars_per_elt
, ix
)
4405 CONSTRUCTOR_ELTS (ary_ctor
)->quick_push (elt
);
4411 /* If the value of object is already zero-initialized, any new ctors for
4412 subobjects will also be zero-initialized. */
4413 no_zero_init
= CONSTRUCTOR_NO_CLEARING (*valp
);
4415 vec_safe_push (ctors
, *valp
);
4417 enum tree_code code
= TREE_CODE (type
);
4419 tree index
= refs
->pop();
4421 constructor_elt
*cep
= NULL
;
4422 if (code
== ARRAY_TYPE
)
4425 = find_array_ctor_elt (*valp
, index
, /*insert*/true);
4426 gcc_assert (i
>= 0);
4427 cep
= CONSTRUCTOR_ELT (*valp
, i
);
4428 gcc_assert (TREE_CODE (cep
->index
) != RANGE_EXPR
);
4432 gcc_assert (TREE_CODE (index
) == FIELD_DECL
);
4434 /* We must keep the CONSTRUCTOR's ELTS in FIELD order.
4435 Usually we meet initializers in that order, but it is
4436 possible for base types to be placed not in program
4438 tree fields
= TYPE_FIELDS (DECL_CONTEXT (index
));
4439 unsigned HOST_WIDE_INT idx
;
4441 if (code
== UNION_TYPE
&& CONSTRUCTOR_NELTS (*valp
)
4442 && CONSTRUCTOR_ELT (*valp
, 0)->index
!= index
)
4444 if (cxx_dialect
< cxx2a
)
4447 error_at (cp_expr_loc_or_input_loc (t
),
4448 "change of the active member of a union "
4450 CONSTRUCTOR_ELT (*valp
, 0)->index
,
4452 *non_constant_p
= true;
4454 /* Changing active member. */
4455 vec_safe_truncate (CONSTRUCTOR_ELTS (*valp
), 0);
4456 no_zero_init
= true;
4460 vec_safe_iterate (CONSTRUCTOR_ELTS (*valp
), idx
, &cep
);
4461 idx
++, fields
= DECL_CHAIN (fields
))
4463 if (index
== cep
->index
)
4466 /* The field we're initializing must be on the field
4467 list. Look to see if it is present before the
4468 field the current ELT initializes. */
4469 for (; fields
!= cep
->index
; fields
= DECL_CHAIN (fields
))
4470 if (index
== fields
)
4474 /* We fell off the end of the CONSTRUCTOR, so insert a new
4475 entry at the end. */
4478 constructor_elt ce
= { index
, NULL_TREE
};
4480 vec_safe_insert (CONSTRUCTOR_ELTS (*valp
), idx
, ce
);
4481 cep
= CONSTRUCTOR_ELT (*valp
, idx
);
4488 /* Detect modifying a constant object in constexpr evaluation.
4489 We have found a const object that is being modified. Figure out
4490 if we need to issue an error. Consider
4494 constexpr A() : n(1) { n = 2; } // #1
4498 constexpr B() { a.n = 3; } // #2
4502 #1 is OK, since we're modifying an object under construction, but
4503 #2 is wrong, since "a" is const and has been fully constructed.
4504 To track it, we use the TREE_READONLY bit in the object's CONSTRUCTOR
4505 which means that the object is read-only. For the example above, the
4506 *ctors stack at the point of #2 will look like:
4508 ctors[0] = {.a={.n=2}} TREE_READONLY = 0
4509 ctors[1] = {.n=2} TREE_READONLY = 1
4511 and we're modifying "b.a", so we search the stack and see if the
4512 constructor for "b.a" has already run. */
4513 if (const_object_being_modified
)
4517 = strip_array_types (TREE_TYPE (const_object_being_modified
));
4518 if (!CLASS_TYPE_P (const_objtype
))
4522 /* [class.ctor]p5 "A constructor can be invoked for a const,
4523 volatile, or const volatile object. const and volatile
4524 semantics are not applied on an object under construction.
4525 They come into effect when the constructor for the most
4526 derived object ends." */
4529 FOR_EACH_VEC_ELT (*ctors
, i
, elt
)
4530 if (same_type_ignoring_top_level_qualifiers_p
4531 (TREE_TYPE (const_object_being_modified
), TREE_TYPE (elt
)))
4533 fail
= TREE_READONLY (elt
);
4540 modifying_const_object_error (t
, const_object_being_modified
);
4541 *non_constant_p
= true;
4548 /* Create a new CONSTRUCTOR in case evaluation of the initializer
4549 wants to modify it. */
4550 if (*valp
== NULL_TREE
)
4552 *valp
= build_constructor (type
, NULL
);
4553 CONSTRUCTOR_NO_CLEARING (*valp
) = no_zero_init
;
4555 new_ctx
.ctor
= *valp
;
4556 new_ctx
.object
= target
;
4557 init
= cxx_eval_constant_expression (&new_ctx
, init
, false,
4558 non_constant_p
, overflow_p
);
4559 if (ctors
->is_empty())
4560 /* The hash table might have moved since the get earlier. */
4561 valp
= ctx
->global
->values
.get (object
);
4564 /* Don't share a CONSTRUCTOR that might be changed later. */
4565 init
= unshare_constructor (init
);
4567 if (*valp
&& TREE_CODE (*valp
) == CONSTRUCTOR
4568 && TREE_CODE (init
) == CONSTRUCTOR
)
4570 /* An outer ctx->ctor might be pointing to *valp, so replace
4572 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init
),
4575 /* For initialization of an empty base, the original target will be
4576 *(base*)this, evaluation of which resolves to the object
4577 argument, which has the derived type rather than the base type. In
4578 this situation, just evaluate the initializer and return, since
4579 there's no actual data to store. */
4580 gcc_assert (is_empty_class (TREE_TYPE (init
)) && !lval
);
4583 CONSTRUCTOR_ELTS (*valp
) = CONSTRUCTOR_ELTS (init
);
4584 TREE_CONSTANT (*valp
) = TREE_CONSTANT (init
);
4585 TREE_SIDE_EFFECTS (*valp
) = TREE_SIDE_EFFECTS (init
);
4586 CONSTRUCTOR_NO_CLEARING (*valp
)
4587 = CONSTRUCTOR_NO_CLEARING (init
);
4592 /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing
4593 CONSTRUCTORs, if any. */
4596 bool c
= TREE_CONSTANT (init
);
4597 bool s
= TREE_SIDE_EFFECTS (init
);
4599 FOR_EACH_VEC_ELT (*ctors
, i
, elt
)
4602 TREE_CONSTANT (elt
) = false;
4604 TREE_SIDE_EFFECTS (elt
) = true;
4607 if (*non_constant_p
)
4615 /* Evaluate a ++ or -- expression. */
4618 cxx_eval_increment_expression (const constexpr_ctx
*ctx
, tree t
,
4620 bool *non_constant_p
, bool *overflow_p
)
4622 enum tree_code code
= TREE_CODE (t
);
4623 tree type
= TREE_TYPE (t
);
4624 tree op
= TREE_OPERAND (t
, 0);
4625 tree offset
= TREE_OPERAND (t
, 1);
4626 gcc_assert (TREE_CONSTANT (offset
));
4628 /* OFFSET is constant, but perhaps not constant enough. We need to
4629 e.g. bash FLOAT_EXPRs to REAL_CSTs. */
4630 offset
= fold_simple (offset
);
4632 /* The operand as an lvalue. */
4633 op
= cxx_eval_constant_expression (ctx
, op
, true,
4634 non_constant_p
, overflow_p
);
4636 /* The operand as an rvalue. */
4638 = cxx_eval_constant_expression (ctx
, op
, false,
4639 non_constant_p
, overflow_p
);
4640 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
4641 a local array in a constexpr function. */
4642 bool ptr
= INDIRECT_TYPE_P (TREE_TYPE (val
));
4644 VERIFY_CONSTANT (val
);
4646 /* The modified value. */
4647 bool inc
= (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
);
4649 if (INDIRECT_TYPE_P (type
))
4651 /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
4652 offset
= convert_to_ptrofftype (offset
);
4654 offset
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (offset
), offset
);
4655 mod
= fold_build2 (POINTER_PLUS_EXPR
, type
, val
, offset
);
4658 mod
= fold_build2 (inc
? PLUS_EXPR
: MINUS_EXPR
, type
, val
, offset
);
4660 VERIFY_CONSTANT (mod
);
4662 /* Storing the modified value. */
4663 tree store
= build2_loc (cp_expr_loc_or_loc (t
, input_location
),
4664 MODIFY_EXPR
, type
, op
, mod
);
4665 cxx_eval_constant_expression (ctx
, store
,
4666 true, non_constant_p
, overflow_p
);
4669 /* And the value of the expression. */
4670 if (code
== PREINCREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
4672 /* Prefix ops are lvalues. */
4676 /* But we optimize when the caller wants an rvalue. */
4680 /* Postfix ops are rvalues. */
4684 /* Predicates for the meaning of *jump_target. */
4687 returns (tree
*jump_target
)
4690 && (TREE_CODE (*jump_target
) == RETURN_EXPR
4691 || (TREE_CODE (*jump_target
) == LABEL_DECL
4692 && LABEL_DECL_CDTOR (*jump_target
)));
4696 breaks (tree
*jump_target
)
4699 && ((TREE_CODE (*jump_target
) == LABEL_DECL
4700 && LABEL_DECL_BREAK (*jump_target
))
4701 || TREE_CODE (*jump_target
) == BREAK_STMT
4702 || TREE_CODE (*jump_target
) == EXIT_EXPR
);
4706 continues (tree
*jump_target
)
4709 && ((TREE_CODE (*jump_target
) == LABEL_DECL
4710 && LABEL_DECL_CONTINUE (*jump_target
))
4711 || TREE_CODE (*jump_target
) == CONTINUE_STMT
);
4716 switches (tree
*jump_target
)
4719 && TREE_CODE (*jump_target
) == INTEGER_CST
;
4722 /* Subroutine of cxx_eval_statement_list. Determine whether the statement
4723 STMT matches *jump_target. If we're looking for a case label and we see
4724 the default label, note it in ctx->css_state. */
4727 label_matches (const constexpr_ctx
*ctx
, tree
*jump_target
, tree stmt
)
4729 switch (TREE_CODE (*jump_target
))
4732 if (TREE_CODE (stmt
) == LABEL_EXPR
4733 && LABEL_EXPR_LABEL (stmt
) == *jump_target
)
4738 if (TREE_CODE (stmt
) == CASE_LABEL_EXPR
)
4740 gcc_assert (ctx
->css_state
!= NULL
);
4741 if (!CASE_LOW (stmt
))
4743 /* default: should appear just once in a SWITCH_EXPR
4744 body (excluding nested SWITCH_EXPR). */
4745 gcc_assert (*ctx
->css_state
!= css_default_seen
);
4746 /* When evaluating SWITCH_EXPR body for the second time,
4747 return true for the default: label. */
4748 if (*ctx
->css_state
== css_default_processing
)
4750 *ctx
->css_state
= css_default_seen
;
4752 else if (CASE_HIGH (stmt
))
4754 if (tree_int_cst_le (CASE_LOW (stmt
), *jump_target
)
4755 && tree_int_cst_le (*jump_target
, CASE_HIGH (stmt
)))
4758 else if (tree_int_cst_equal (*jump_target
, CASE_LOW (stmt
)))
4765 /* These two are handled directly in cxx_eval_loop_expr by testing
4766 breaks (jump_target) or continues (jump_target). */
4775 /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
4776 semantics, for switch, break, continue, and return. */
4779 cxx_eval_statement_list (const constexpr_ctx
*ctx
, tree t
,
4780 bool *non_constant_p
, bool *overflow_p
,
4783 tree_stmt_iterator i
;
4785 /* In a statement-expression we want to return the last value.
4786 For empty statement expression return void_node. */
4790 local_target
= NULL_TREE
;
4791 jump_target
= &local_target
;
4793 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
4795 tree stmt
= tsi_stmt (i
);
4796 /* We've found a continue, so skip everything until we reach
4797 the label its jumping to. */
4798 if (continues (jump_target
))
4800 if (label_matches (ctx
, jump_target
, stmt
))
4802 *jump_target
= NULL_TREE
;
4806 if (TREE_CODE (stmt
) == DEBUG_BEGIN_STMT
)
4808 r
= cxx_eval_constant_expression (ctx
, stmt
, false,
4809 non_constant_p
, overflow_p
,
4811 if (*non_constant_p
)
4813 if (returns (jump_target
) || breaks (jump_target
))
4816 if (*jump_target
&& jump_target
== &local_target
)
4818 /* We aren't communicating the jump to our caller, so give up. We don't
4819 need to support evaluation of jumps out of statement-exprs. */
4821 error_at (cp_expr_loc_or_input_loc (r
),
4822 "statement is not a constant expression");
4823 *non_constant_p
= true;
4828 /* Evaluate a LOOP_EXPR for side-effects. Handles break and return
4829 semantics; continue semantics are covered by cxx_eval_statement_list. */
4832 cxx_eval_loop_expr (const constexpr_ctx
*ctx
, tree t
,
4833 bool *non_constant_p
, bool *overflow_p
,
4836 constexpr_ctx new_ctx
= *ctx
;
4840 local_target
= NULL_TREE
;
4841 jump_target
= &local_target
;
4844 tree body
, cond
= NULL_TREE
, expr
= NULL_TREE
;
4846 switch (TREE_CODE (t
))
4849 body
= LOOP_EXPR_BODY (t
);
4856 body
= WHILE_BODY (t
);
4857 cond
= WHILE_COND (t
);
4861 if (FOR_INIT_STMT (t
))
4862 cxx_eval_constant_expression (ctx
, FOR_INIT_STMT (t
), /*lval*/false,
4863 non_constant_p
, overflow_p
, jump_target
);
4864 if (*non_constant_p
)
4866 body
= FOR_BODY (t
);
4867 cond
= FOR_COND (t
);
4868 expr
= FOR_EXPR (t
);
4874 auto_vec
<tree
, 10> save_exprs
;
4875 new_ctx
.save_exprs
= &save_exprs
;
4881 cxx_eval_constant_expression (&new_ctx
, body
, /*lval*/false,
4882 non_constant_p
, overflow_p
,
4884 if (breaks (jump_target
))
4886 *jump_target
= NULL_TREE
;
4890 if (TREE_CODE (t
) != LOOP_EXPR
&& continues (jump_target
))
4891 *jump_target
= NULL_TREE
;
4894 cxx_eval_constant_expression (&new_ctx
, expr
, /*lval*/false,
4895 non_constant_p
, overflow_p
,
4902 = cxx_eval_constant_expression (&new_ctx
, cond
, /*lval*/false,
4903 non_constant_p
, overflow_p
,
4907 if (verify_constant (res
, ctx
->quiet
, non_constant_p
,
4910 if (integer_zerop (res
))
4914 gcc_assert (*jump_target
);
4917 /* Forget saved values of SAVE_EXPRs and TARGET_EXPRs. */
4920 FOR_EACH_VEC_ELT (save_exprs
, i
, save_expr
)
4921 ctx
->global
->values
.remove (save_expr
);
4922 save_exprs
.truncate (0);
4924 if (++count
>= constexpr_loop_limit
)
4927 error_at (cp_expr_loc_or_input_loc (t
),
4928 "%<constexpr%> loop iteration count exceeds limit of %d "
4929 "(use %<-fconstexpr-loop-limit=%> to increase the limit)",
4930 constexpr_loop_limit
);
4931 *non_constant_p
= true;
4935 while (!returns (jump_target
)
4936 && !breaks (jump_target
)
4937 && !continues (jump_target
)
4938 && (!switches (jump_target
) || count
== 0)
4939 && !*non_constant_p
);
4941 /* Forget saved values of SAVE_EXPRs and TARGET_EXPRs. */
4944 FOR_EACH_VEC_ELT (save_exprs
, i
, save_expr
)
4945 ctx
->global
->values
.remove (save_expr
);
4950 /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
4954 cxx_eval_switch_expr (const constexpr_ctx
*ctx
, tree t
,
4955 bool *non_constant_p
, bool *overflow_p
,
4959 = TREE_CODE (t
) == SWITCH_STMT
? SWITCH_STMT_COND (t
) : SWITCH_COND (t
);
4960 cond
= cxx_eval_constant_expression (ctx
, cond
, false,
4961 non_constant_p
, overflow_p
);
4962 VERIFY_CONSTANT (cond
);
4963 *jump_target
= cond
;
4966 = TREE_CODE (t
) == SWITCH_STMT
? SWITCH_STMT_BODY (t
) : SWITCH_BODY (t
);
4967 constexpr_ctx new_ctx
= *ctx
;
4968 constexpr_switch_state css
= css_default_not_seen
;
4969 new_ctx
.css_state
= &css
;
4970 cxx_eval_constant_expression (&new_ctx
, body
, false,
4971 non_constant_p
, overflow_p
, jump_target
);
4972 if (switches (jump_target
) && css
== css_default_seen
)
4974 /* If the SWITCH_EXPR body has default: label, process it once again,
4975 this time instructing label_matches to return true for default:
4976 label on switches (jump_target). */
4977 css
= css_default_processing
;
4978 cxx_eval_constant_expression (&new_ctx
, body
, false,
4979 non_constant_p
, overflow_p
, jump_target
);
4981 if (breaks (jump_target
) || switches (jump_target
))
4982 *jump_target
= NULL_TREE
;
4986 /* Find the object of TYPE under initialization in CTX. */
4989 lookup_placeholder (const constexpr_ctx
*ctx
, bool lval
, tree type
)
4994 /* We could use ctx->object unconditionally, but using ctx->ctor when we
4995 can is a minor optimization. */
4996 if (!lval
&& ctx
->ctor
&& same_type_p (TREE_TYPE (ctx
->ctor
), type
))
5002 /* Since an object cannot have a field of its own type, we can search outward
5003 from ctx->object to find the unique containing object of TYPE. */
5004 tree ob
= ctx
->object
;
5007 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob
), type
))
5009 if (handled_component_p (ob
))
5010 ob
= TREE_OPERAND (ob
, 0);
5018 /* Complain about an attempt to evaluate inline assembly. */
5021 inline_asm_in_constexpr_error (location_t loc
)
5023 auto_diagnostic_group d
;
5024 error_at (loc
, "inline assembly is not a constant expression");
5025 inform (loc
, "only unevaluated inline assembly is allowed in a "
5026 "%<constexpr%> function in C++2a");
5029 /* Attempt to reduce the expression T to a constant value.
5030 On failure, issue diagnostic and return error_mark_node. */
5031 /* FIXME unify with c_fully_fold */
5032 /* FIXME overflow_p is too global */
5035 cxx_eval_constant_expression (const constexpr_ctx
*ctx
, tree t
,
5037 bool *non_constant_p
, bool *overflow_p
,
5038 tree
*jump_target
/* = NULL */)
5040 if (jump_target
&& *jump_target
)
5042 /* If we are jumping, ignore all statements/expressions except those
5043 that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */
5044 switch (TREE_CODE (t
))
5047 case STATEMENT_LIST
:
5056 case CASE_LABEL_EXPR
:
5057 if (label_matches (ctx
, jump_target
, t
))
5059 *jump_target
= NULL_TREE
;
5065 if (error_operand_p (t
))
5067 *non_constant_p
= true;
5071 location_t loc
= cp_expr_loc_or_input_loc (t
);
5073 STRIP_ANY_LOCATION_WRAPPER (t
);
5075 if (CONSTANT_CLASS_P (t
))
5077 if (TREE_OVERFLOW (t
))
5080 permerror (input_location
, "overflow in constant expression");
5081 if (!flag_permissive
|| ctx
->quiet
)
5085 if (TREE_CODE (t
) == INTEGER_CST
5086 && TYPE_PTR_P (TREE_TYPE (t
))
5087 && !integer_zerop (t
))
5090 error ("value %qE of type %qT is not a constant expression",
5092 *non_constant_p
= true;
5098 /* Avoid excessively long constexpr evaluations. */
5099 if (++ctx
->global
->constexpr_ops_count
>= constexpr_ops_limit
)
5103 "%<constexpr%> evaluation operation count exceeds limit of "
5104 "%wd (use %<-fconstexpr-ops-limit=%> to increase the limit)",
5105 constexpr_ops_limit
);
5106 ctx
->global
->constexpr_ops_count
= INTTYPE_MINIMUM (HOST_WIDE_INT
);
5107 *non_constant_p
= true;
5111 constexpr_ctx new_ctx
;
5114 tree_code tcode
= TREE_CODE (t
);
5120 /* We ask for an rvalue for the RESULT_DECL when indirecting
5121 through an invisible reference, or in named return value
5123 if (tree
*p
= ctx
->global
->values
.get (t
))
5128 error ("%qE is not a constant expression", t
);
5129 *non_constant_p
= true;
5134 if (DECL_HAS_VALUE_EXPR_P (t
))
5136 if (is_normal_capture_proxy (t
)
5137 && current_function_decl
== DECL_CONTEXT (t
))
5139 /* Function parms aren't constexpr within the function
5140 definition, so don't try to look at the closure. But if the
5141 captured variable is constant, try to evaluate it directly. */
5142 r
= DECL_CAPTURED_VARIABLE (t
);
5143 tree type
= TREE_TYPE (t
);
5144 if (TYPE_REF_P (type
) != TYPE_REF_P (TREE_TYPE (r
)))
5146 /* Adjust r to match the reference-ness of t. */
5147 if (TYPE_REF_P (type
))
5148 r
= build_address (r
);
5150 r
= convert_from_reference (r
);
5154 r
= DECL_VALUE_EXPR (t
);
5155 return cxx_eval_constant_expression (ctx
, r
, lval
, non_constant_p
,
5160 /* We used to not check lval for CONST_DECL, but darwin.c uses
5161 CONST_DECL for aggregate constants. */
5164 if (COMPLETE_TYPE_P (TREE_TYPE (t
))
5165 && is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
5167 /* If the class is empty, we aren't actually loading anything. */
5168 r
= build_constructor (TREE_TYPE (t
), NULL
);
5169 TREE_CONSTANT (r
) = true;
5171 else if (ctx
->strict
)
5172 r
= decl_really_constant_value (t
);
5174 r
= decl_constant_value (t
);
5175 if (TREE_CODE (r
) == TARGET_EXPR
5176 && TREE_CODE (TARGET_EXPR_INITIAL (r
)) == CONSTRUCTOR
)
5177 r
= TARGET_EXPR_INITIAL (r
);
5179 if (tree
*p
= ctx
->global
->values
.get (r
))
5180 if (*p
!= NULL_TREE
)
5185 non_const_var_error (loc
, r
);
5186 *non_constant_p
= true;
5190 case DEBUG_BEGIN_STMT
:
5191 /* ??? It might be nice to retain this information somehow, so
5192 as to be able to step into a constexpr function call. */
5199 case CASE_LABEL_EXPR
:
5204 if (lval
&& !TYPE_REF_P (TREE_TYPE (t
)))
5206 else if (tree
*p
= ctx
->global
->values
.get (r
))
5209 /* Defer in case this is only used for its type. */;
5210 else if (TYPE_REF_P (TREE_TYPE (t
)))
5211 /* Defer, there's no lvalue->rvalue conversion. */;
5212 else if (COMPLETE_TYPE_P (TREE_TYPE (t
))
5213 && is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
5215 /* If the class is empty, we aren't actually loading anything. */
5216 r
= build_constructor (TREE_TYPE (t
), NULL
);
5217 TREE_CONSTANT (r
) = true;
5222 error ("%qE is not a constant expression", t
);
5223 *non_constant_p
= true;
5228 case AGGR_INIT_EXPR
:
5229 r
= cxx_eval_call_expression (ctx
, t
, lval
,
5230 non_constant_p
, overflow_p
);
5235 r
= DECL_EXPR_DECL (t
);
5236 if (TREE_CODE (r
) == USING_DECL
)
5241 if (AGGREGATE_TYPE_P (TREE_TYPE (r
))
5242 || VECTOR_TYPE_P (TREE_TYPE (r
)))
5246 new_ctx
.ctor
= build_constructor (TREE_TYPE (r
), NULL
);
5247 CONSTRUCTOR_NO_CLEARING (new_ctx
.ctor
) = true;
5248 ctx
->global
->values
.put (r
, new_ctx
.ctor
);
5252 if (tree init
= DECL_INITIAL (r
))
5254 init
= cxx_eval_constant_expression (ctx
, init
,
5256 non_constant_p
, overflow_p
);
5257 /* Don't share a CONSTRUCTOR that might be changed. */
5258 init
= unshare_constructor (init
);
5259 /* Remember that a constant object's constructor has already
5261 if (CLASS_TYPE_P (TREE_TYPE (r
))
5262 && CP_TYPE_CONST_P (TREE_TYPE (r
)))
5263 TREE_READONLY (init
) = true;
5264 ctx
->global
->values
.put (r
, init
);
5266 else if (ctx
== &new_ctx
)
5267 /* We gave it a CONSTRUCTOR above. */;
5269 ctx
->global
->values
.put (r
, NULL_TREE
);
5274 if (!literal_type_p (TREE_TYPE (t
)))
5278 auto_diagnostic_group d
;
5279 error ("temporary of non-literal type %qT in a "
5280 "constant expression", TREE_TYPE (t
));
5281 explain_non_literal_class (TREE_TYPE (t
));
5283 *non_constant_p
= true;
5286 /* Avoid evaluating a TARGET_EXPR more than once. */
5287 if (tree
*p
= ctx
->global
->values
.get (TARGET_EXPR_SLOT (t
)))
5290 return TARGET_EXPR_SLOT (t
);
5294 if ((AGGREGATE_TYPE_P (TREE_TYPE (t
)) || VECTOR_TYPE_P (TREE_TYPE (t
))))
5296 /* We're being expanded without an explicit target, so start
5297 initializing a new object; expansion with an explicit target
5298 strips the TARGET_EXPR before we get here. */
5300 new_ctx
.ctor
= build_constructor (TREE_TYPE (t
), NULL
);
5301 CONSTRUCTOR_NO_CLEARING (new_ctx
.ctor
) = true;
5302 new_ctx
.object
= TARGET_EXPR_SLOT (t
);
5303 ctx
->global
->values
.put (new_ctx
.object
, new_ctx
.ctor
);
5306 /* Pass false for 'lval' because this indicates
5307 initialization of a temporary. */
5308 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
5310 non_constant_p
, overflow_p
);
5311 if (!*non_constant_p
)
5312 /* Adjust the type of the result to the type of the temporary. */
5313 r
= adjust_temp_type (TREE_TYPE (t
), r
);
5314 if (TARGET_EXPR_CLEANUP (t
) && !CLEANUP_EH_ONLY (t
))
5315 ctx
->global
->cleanups
->safe_push (TARGET_EXPR_CLEANUP (t
));
5316 r
= unshare_constructor (r
);
5317 ctx
->global
->values
.put (TARGET_EXPR_SLOT (t
), r
);
5318 if (ctx
->save_exprs
)
5319 ctx
->save_exprs
->safe_push (TARGET_EXPR_SLOT (t
));
5321 return TARGET_EXPR_SLOT (t
);
5326 gcc_assert (jump_target
== NULL
|| *jump_target
== NULL_TREE
);
5327 r
= cxx_eval_store_expression (ctx
, t
, lval
,
5328 non_constant_p
, overflow_p
);
5332 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
5334 non_constant_p
, overflow_p
);
5338 if (TREE_OPERAND (t
, 0) != NULL_TREE
)
5339 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
5341 non_constant_p
, overflow_p
);
5349 /* Can happen with ({ return true; }) && false; passed to
5350 maybe_constant_value. There is nothing to jump over in this
5351 case, and the bug will be diagnosed later. */
5352 gcc_assert (ctx
->quiet
);
5353 *non_constant_p
= true;
5358 /* Avoid evaluating a SAVE_EXPR more than once. */
5359 if (tree
*p
= ctx
->global
->values
.get (t
))
5363 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), false,
5364 non_constant_p
, overflow_p
);
5365 ctx
->global
->values
.put (t
, r
);
5366 if (ctx
->save_exprs
)
5367 ctx
->save_exprs
->safe_push (t
);
5371 case TRY_CATCH_EXPR
:
5372 if (TREE_OPERAND (t
, 0) == NULL_TREE
)
5378 case NON_LVALUE_EXPR
:
5380 case MUST_NOT_THROW_EXPR
:
5383 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
5385 non_constant_p
, overflow_p
,
5389 case CLEANUP_POINT_EXPR
:
5391 auto_vec
<tree
, 2> cleanups
;
5392 vec
<tree
> *prev_cleanups
= ctx
->global
->cleanups
;
5393 ctx
->global
->cleanups
= &cleanups
;
5394 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
5396 non_constant_p
, overflow_p
,
5398 ctx
->global
->cleanups
= prev_cleanups
;
5401 /* Evaluate the cleanups. */
5402 FOR_EACH_VEC_ELT_REVERSE (cleanups
, i
, cleanup
)
5403 cxx_eval_constant_expression (ctx
, cleanup
, false,
5404 non_constant_p
, overflow_p
,
5409 case TRY_FINALLY_EXPR
:
5410 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), lval
,
5411 non_constant_p
, overflow_p
,
5413 if (!*non_constant_p
)
5414 /* Also evaluate the cleanup. */
5415 cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1), true,
5416 non_constant_p
, overflow_p
,
5422 tree initial_jump_target
= jump_target
? *jump_target
: NULL_TREE
;
5423 r
= cxx_eval_constant_expression (ctx
, CLEANUP_BODY (t
), lval
,
5424 non_constant_p
, overflow_p
,
5426 if (!CLEANUP_EH_ONLY (t
) && !*non_constant_p
)
5428 iloc_sentinel
ils (loc
);
5429 /* Also evaluate the cleanup. If we weren't skipping at the
5430 start of the CLEANUP_BODY, change jump_target temporarily
5431 to &initial_jump_target, so that even a return or break or
5432 continue in the body doesn't skip the cleanup. */
5433 cxx_eval_constant_expression (ctx
, CLEANUP_EXPR (t
), true,
5434 non_constant_p
, overflow_p
,
5435 jump_target
? &initial_jump_target
5441 /* These differ from cxx_eval_unary_expression in that this doesn't
5442 check for a constant operand or result; an address can be
5443 constant without its operand being, and vice versa. */
5446 r
= cxx_eval_indirect_ref (ctx
, t
, lval
,
5447 non_constant_p
, overflow_p
);
5452 tree oldop
= TREE_OPERAND (t
, 0);
5453 tree op
= cxx_eval_constant_expression (ctx
, oldop
,
5455 non_constant_p
, overflow_p
);
5456 /* Don't VERIFY_CONSTANT here. */
5457 if (*non_constant_p
)
5459 gcc_checking_assert (TREE_CODE (op
) != CONSTRUCTOR
);
5460 /* This function does more aggressive folding than fold itself. */
5461 r
= build_fold_addr_expr_with_type (op
, TREE_TYPE (t
));
5462 if (TREE_CODE (r
) == ADDR_EXPR
&& TREE_OPERAND (r
, 0) == oldop
)
5474 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), lval
,
5475 non_constant_p
, overflow_p
);
5476 if (r
== error_mark_node
)
5478 else if (r
== TREE_OPERAND (t
, 0))
5481 r
= fold_build1 (TREE_CODE (t
), TREE_TYPE (t
), r
);
5486 case FIX_TRUNC_EXPR
:
5492 case TRUTH_NOT_EXPR
:
5493 case FIXED_CONVERT_EXPR
:
5494 r
= cxx_eval_unary_expression (ctx
, t
, lval
,
5495 non_constant_p
, overflow_p
);
5499 r
= fold_sizeof_expr (t
);
5500 /* In a template, fold_sizeof_expr may merely create a new SIZEOF_EXPR,
5501 which could lead to an infinite recursion. */
5502 if (TREE_CODE (r
) != SIZEOF_EXPR
)
5503 r
= cxx_eval_constant_expression (ctx
, r
, lval
,
5504 non_constant_p
, overflow_p
,
5508 *non_constant_p
= true;
5509 gcc_assert (ctx
->quiet
);
5516 /* check_return_expr sometimes wraps a TARGET_EXPR in a
5517 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
5518 introduced by build_call_a. */
5519 tree op0
= TREE_OPERAND (t
, 0);
5520 tree op1
= TREE_OPERAND (t
, 1);
5522 if ((TREE_CODE (op0
) == TARGET_EXPR
&& op1
== TARGET_EXPR_SLOT (op0
))
5523 || TREE_CODE (op1
) == EMPTY_CLASS_EXPR
)
5524 r
= cxx_eval_constant_expression (ctx
, op0
,
5525 lval
, non_constant_p
, overflow_p
,
5529 /* Check that the LHS is constant and then discard it. */
5530 cxx_eval_constant_expression (ctx
, op0
,
5531 true, non_constant_p
, overflow_p
,
5533 if (*non_constant_p
)
5535 op1
= TREE_OPERAND (t
, 1);
5536 r
= cxx_eval_constant_expression (ctx
, op1
,
5537 lval
, non_constant_p
, overflow_p
,
5543 case POINTER_PLUS_EXPR
:
5544 case POINTER_DIFF_EXPR
:
5548 case TRUNC_DIV_EXPR
:
5550 case FLOOR_DIV_EXPR
:
5551 case ROUND_DIV_EXPR
:
5552 case TRUNC_MOD_EXPR
:
5554 case ROUND_MOD_EXPR
:
5556 case EXACT_DIV_EXPR
:
5564 case TRUTH_XOR_EXPR
:
5571 case SPACESHIP_EXPR
:
5572 case UNORDERED_EXPR
:
5582 r
= cxx_eval_binary_expression (ctx
, t
, lval
,
5583 non_constant_p
, overflow_p
);
5586 /* fold can introduce non-IF versions of these; still treat them as
5587 short-circuiting. */
5588 case TRUTH_AND_EXPR
:
5589 case TRUTH_ANDIF_EXPR
:
5590 r
= cxx_eval_logical_expression (ctx
, t
, boolean_false_node
,
5593 non_constant_p
, overflow_p
);
5597 case TRUTH_ORIF_EXPR
:
5598 r
= cxx_eval_logical_expression (ctx
, t
, boolean_true_node
,
5601 non_constant_p
, overflow_p
);
5605 r
= cxx_eval_array_reference (ctx
, t
, lval
,
5606 non_constant_p
, overflow_p
);
5610 if (is_overloaded_fn (t
))
5612 /* We can only get here in checking mode via
5613 build_non_dependent_expr, because any expression that
5614 calls or takes the address of the function will have
5615 pulled a FUNCTION_DECL out of the COMPONENT_REF. */
5616 gcc_checking_assert (ctx
->quiet
|| errorcount
);
5617 *non_constant_p
= true;
5620 r
= cxx_eval_component_reference (ctx
, t
, lval
,
5621 non_constant_p
, overflow_p
);
5625 r
= cxx_eval_bit_field_ref (ctx
, t
, lval
,
5626 non_constant_p
, overflow_p
);
5631 if (jump_target
&& *jump_target
)
5633 tree orig_jump
= *jump_target
;
5634 tree arg
= ((TREE_CODE (t
) != IF_STMT
|| TREE_OPERAND (t
, 1))
5635 ? TREE_OPERAND (t
, 1) : void_node
);
5636 /* When jumping to a label, the label might be either in the
5637 then or else blocks, so process then block first in skipping
5638 mode first, and if we are still in the skipping mode at its end,
5639 process the else block too. */
5640 r
= cxx_eval_constant_expression (ctx
, arg
, lval
, non_constant_p
,
5641 overflow_p
, jump_target
);
5642 /* It's possible that we found the label in the then block. But
5643 it could have been followed by another jumping statement, e.g.
5644 say we're looking for case 1:
5647 // skipped statements
5648 case 1:; // clears up *jump_target
5649 return 1; // and sets it to a RETURN_EXPR
5652 in which case we need not go looking to the else block.
5653 (goto is not allowed in a constexpr function.) */
5654 if (*jump_target
== orig_jump
)
5656 arg
= ((TREE_CODE (t
) != IF_STMT
|| TREE_OPERAND (t
, 2))
5657 ? TREE_OPERAND (t
, 2) : void_node
);
5658 r
= cxx_eval_constant_expression (ctx
, arg
, lval
, non_constant_p
,
5659 overflow_p
, jump_target
);
5663 r
= cxx_eval_conditional_expression (ctx
, t
, lval
,
5664 non_constant_p
, overflow_p
,
5668 r
= cxx_eval_vector_conditional_expression (ctx
, t
, non_constant_p
,
5673 if (TREE_CONSTANT (t
) && reduced_constant_expression_p (t
))
5675 /* Don't re-process a constant CONSTRUCTOR, but do fold it to
5676 VECTOR_CST if applicable. */
5677 verify_constructor_flags (t
);
5678 if (TREE_CONSTANT (t
))
5681 r
= cxx_eval_bare_aggregate (ctx
, t
, lval
,
5682 non_constant_p
, overflow_p
);
5686 /* We can get this in a defaulted constructor for a class with a
5687 non-static data member of array type. Either the initializer will
5688 be NULL, meaning default-initialization, or it will be an lvalue
5689 or xvalue of the same type, meaning direct-initialization from the
5690 corresponding member. */
5691 r
= cxx_eval_vec_init (ctx
, t
, lval
,
5692 non_constant_p
, overflow_p
);
5696 r
= cxx_eval_trinary_expression (ctx
, t
, lval
,
5697 non_constant_p
, overflow_p
);
5701 if (REINTERPRET_CAST_P (t
))
5705 "%<reinterpret_cast%> is not a constant expression");
5706 *non_constant_p
= true;
5711 case VIEW_CONVERT_EXPR
:
5712 case UNARY_PLUS_EXPR
:
5714 tree oldop
= TREE_OPERAND (t
, 0);
5716 tree op
= cxx_eval_constant_expression (ctx
, oldop
,
5718 non_constant_p
, overflow_p
);
5719 if (*non_constant_p
)
5721 tree type
= TREE_TYPE (t
);
5723 if (VOID_TYPE_P (type
))
5726 if (TREE_CODE (op
) == PTRMEM_CST
&& !TYPE_PTRMEM_P (type
))
5727 op
= cplus_expand_constant (op
);
5729 if (TREE_CODE (op
) == PTRMEM_CST
&& tcode
== NOP_EXPR
)
5731 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (op
))
5732 && !can_convert_qual (type
, op
))
5733 op
= cplus_expand_constant (op
);
5734 return cp_fold_convert (type
, op
);
5737 if (INDIRECT_TYPE_P (type
) && TREE_CODE (op
) == INTEGER_CST
)
5739 if (integer_zerop (op
))
5741 if (TYPE_REF_P (type
))
5745 "dereferencing a null pointer");
5746 *non_constant_p
= true;
5749 else if (TYPE_PTR_P (TREE_TYPE (op
)))
5751 tree from
= TREE_TYPE (op
);
5753 if (!can_convert (type
, from
, tf_none
))
5757 "conversion of %qT null pointer to %qT "
5758 "is not a constant expression",
5760 *non_constant_p
= true;
5767 /* This detects for example:
5768 reinterpret_cast<void*>(sizeof 0)
5771 error_at (loc
, "%<reinterpret_cast<%T>(%E)%> is not "
5772 "a constant expression",
5774 *non_constant_p
= true;
5779 if (INDIRECT_TYPE_P (type
)
5780 && TREE_CODE (op
) == NOP_EXPR
5781 && TREE_TYPE (op
) == ptr_type_node
5782 && TREE_CODE (TREE_OPERAND (op
, 0)) == ADDR_EXPR
5783 && VAR_P (TREE_OPERAND (TREE_OPERAND (op
, 0), 0))
5784 && DECL_NAME (TREE_OPERAND (TREE_OPERAND (op
, 0),
5785 0)) == heap_uninit_identifier
)
5787 tree var
= TREE_OPERAND (TREE_OPERAND (op
, 0), 0);
5788 tree var_size
= TYPE_SIZE_UNIT (TREE_TYPE (var
));
5789 tree elt_type
= TREE_TYPE (type
);
5790 tree cookie_size
= NULL_TREE
;
5791 if (TREE_CODE (elt_type
) == RECORD_TYPE
5792 && TYPE_NAME (elt_type
) == heap_identifier
)
5794 tree fld1
= TYPE_FIELDS (elt_type
);
5795 tree fld2
= DECL_CHAIN (fld1
);
5796 elt_type
= TREE_TYPE (TREE_TYPE (fld2
));
5797 cookie_size
= TYPE_SIZE_UNIT (TREE_TYPE (fld1
));
5799 DECL_NAME (var
) = heap_identifier
;
5801 = build_new_constexpr_heap_type (elt_type
, cookie_size
,
5803 TREE_TYPE (TREE_OPERAND (op
, 0))
5804 = build_pointer_type (TREE_TYPE (var
));
5807 if (op
== oldop
&& tcode
!= UNARY_PLUS_EXPR
)
5808 /* We didn't fold at the top so we could check for ptr-int
5814 /* Handle an array's bounds having been deduced after we built
5815 the wrapping expression. */
5816 if (same_type_ignoring_tlq_and_bounds_p (type
, TREE_TYPE (op
)))
5818 else if (sop
= tree_strip_nop_conversions (op
),
5819 sop
!= op
&& (same_type_ignoring_tlq_and_bounds_p
5820 (type
, TREE_TYPE (sop
))))
5822 else if (tcode
== UNARY_PLUS_EXPR
)
5823 r
= fold_convert (TREE_TYPE (t
), op
);
5825 r
= fold_build1 (tcode
, type
, op
);
5827 /* Conversion of an out-of-range value has implementation-defined
5828 behavior; the language considers it different from arithmetic
5829 overflow, which is undefined. */
5830 if (TREE_OVERFLOW_P (r
) && !TREE_OVERFLOW_P (op
))
5831 TREE_OVERFLOW (r
) = false;
5835 case EMPTY_CLASS_EXPR
:
5836 /* This is good enough for a function argument that might not get
5837 used, and they can't do anything with it, so just return it. */
5840 case STATEMENT_LIST
:
5842 new_ctx
.ctor
= new_ctx
.object
= NULL_TREE
;
5843 return cxx_eval_statement_list (&new_ctx
, t
,
5844 non_constant_p
, overflow_p
, jump_target
);
5847 return cxx_eval_constant_expression (ctx
, BIND_EXPR_BODY (t
),
5849 non_constant_p
, overflow_p
,
5852 case PREINCREMENT_EXPR
:
5853 case POSTINCREMENT_EXPR
:
5854 case PREDECREMENT_EXPR
:
5855 case POSTDECREMENT_EXPR
:
5856 return cxx_eval_increment_expression (ctx
, t
,
5857 lval
, non_constant_p
, overflow_p
);
5863 case VEC_DELETE_EXPR
:
5866 /* GCC internal stuff. */
5868 case NON_DEPENDENT_EXPR
:
5872 error_at (loc
, "expression %qE is not a constant expression", t
);
5873 *non_constant_p
= true;
5878 /* Virtual function call. Let the constexpr machinery figure out
5879 the dynamic type. */
5880 int token
= tree_to_shwi (OBJ_TYPE_REF_TOKEN (t
));
5881 tree obj
= OBJ_TYPE_REF_OBJECT (t
);
5882 obj
= cxx_eval_constant_expression (ctx
, obj
, lval
, non_constant_p
,
5885 /* We expect something in the form of &x.D.2103.D.2094; get x. */
5886 if (TREE_CODE (obj
) != ADDR_EXPR
5887 || !DECL_P (get_base_address (TREE_OPERAND (obj
, 0))))
5890 error_at (loc
, "expression %qE is not a constant expression", t
);
5891 *non_constant_p
= true;
5894 obj
= TREE_OPERAND (obj
, 0);
5895 while (TREE_CODE (obj
) == COMPONENT_REF
5896 && DECL_FIELD_IS_BASE (TREE_OPERAND (obj
, 1)))
5897 obj
= TREE_OPERAND (obj
, 0);
5898 tree objtype
= TREE_TYPE (obj
);
5899 /* Find the function decl in the virtual functions list. TOKEN is
5900 the DECL_VINDEX that says which function we're looking for. */
5901 tree virtuals
= BINFO_VIRTUALS (TYPE_BINFO (objtype
));
5902 if (TARGET_VTABLE_USES_DESCRIPTORS
)
5903 token
/= MAX (TARGET_VTABLE_USES_DESCRIPTORS
, 1);
5904 r
= TREE_VALUE (chain_index (token
, virtuals
));
5908 case PLACEHOLDER_EXPR
:
5909 /* Use of the value or address of the current object. */
5910 if (tree ctor
= lookup_placeholder (ctx
, lval
, TREE_TYPE (t
)))
5911 return cxx_eval_constant_expression (ctx
, ctor
, lval
,
5912 non_constant_p
, overflow_p
);
5913 /* A placeholder without a referent. We can get here when
5914 checking whether NSDMIs are noexcept, or in massage_init_elt;
5915 just say it's non-constant for now. */
5916 gcc_assert (ctx
->quiet
);
5917 *non_constant_p
= true;
5922 tree cond
= TREE_OPERAND (t
, 0);
5923 cond
= cxx_eval_constant_expression (ctx
, cond
, /*lval*/false,
5924 non_constant_p
, overflow_p
);
5925 VERIFY_CONSTANT (cond
);
5926 if (integer_nonzerop (cond
))
5932 *jump_target
= TREE_OPERAND (t
, 0);
5933 gcc_assert (breaks (jump_target
) || continues (jump_target
)
5934 /* Allow for jumping to a cdtor_label. */
5935 || returns (jump_target
));
5942 cxx_eval_loop_expr (ctx
, t
,
5943 non_constant_p
, overflow_p
, jump_target
);
5948 cxx_eval_switch_expr (ctx
, t
,
5949 non_constant_p
, overflow_p
, jump_target
);
5953 /* It's possible to get a requires-expression in a constant
5954 expression. For example:
5956 template<typename T> concept bool C() {
5957 return requires (T t) { t; };
5960 template<typename T> requires !C<T>() void f(T);
5962 Normalization leaves f with the associated constraint
5963 '!requires (T t) { ... }' which is not transformed into
5965 if (!processing_template_decl
)
5966 return satisfy_constraint_expression (t
);
5968 *non_constant_p
= true;
5972 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
5974 non_constant_p
, overflow_p
,
5982 case TEMPLATE_ID_EXPR
:
5984 /* We can evaluate template-id that refers to a concept only if
5985 the template arguments are non-dependent. */
5986 tree id
= unpack_concept_check (t
);
5987 tree tmpl
= TREE_OPERAND (id
, 0);
5988 if (!concept_definition_p (tmpl
))
5989 internal_error ("unexpected template-id %qE", t
);
5991 if (function_concept_p (tmpl
))
5994 error_at (cp_expr_loc_or_input_loc (t
),
5995 "function concept must be called");
5996 r
= error_mark_node
;
6000 if (!processing_template_decl
)
6001 r
= evaluate_concept_check (t
, tf_warning_or_error
);
6003 *non_constant_p
= true;
6010 inline_asm_in_constexpr_error (loc
);
6011 *non_constant_p
= true;
6015 if (STATEMENT_CODE_P (TREE_CODE (t
)))
6017 /* This function doesn't know how to deal with pre-genericize
6018 statements; this can only happen with statement-expressions,
6019 so for now just fail. */
6021 error_at (EXPR_LOCATION (t
),
6022 "statement is not a constant expression");
6025 internal_error ("unexpected expression %qE of kind %s", t
,
6026 get_tree_code_name (TREE_CODE (t
)));
6027 *non_constant_p
= true;
6031 if (r
== error_mark_node
)
6032 *non_constant_p
= true;
6034 if (*non_constant_p
)
6040 /* P0859: A function is needed for constant evaluation if it is a constexpr
6041 function that is named by an expression ([basic.def.odr]) that is
6042 potentially constant evaluated.
6044 So we need to instantiate any constexpr functions mentioned by the
6045 expression even if the definition isn't needed for evaluating the
6049 instantiate_cx_fn_r (tree
*tp
, int *walk_subtrees
, void */
*data*/
)
6051 if (TREE_CODE (*tp
) == FUNCTION_DECL
6052 && DECL_DECLARED_CONSTEXPR_P (*tp
)
6053 && !DECL_INITIAL (*tp
)
6054 && !trivial_fn_p (*tp
)
6055 && DECL_TEMPLOID_INSTANTIATION (*tp
))
6058 instantiate_decl (*tp
, /*defer_ok*/false, /*expl_inst*/false);
6061 else if (TREE_CODE (*tp
) == CALL_EXPR
6062 || TREE_CODE (*tp
) == AGGR_INIT_EXPR
)
6064 if (EXPR_HAS_LOCATION (*tp
))
6065 input_location
= EXPR_LOCATION (*tp
);
6075 instantiate_constexpr_fns (tree t
)
6077 location_t loc
= input_location
;
6078 cp_walk_tree_without_duplicates (&t
, instantiate_cx_fn_r
, NULL
);
6079 input_location
= loc
;
6082 /* Look for heap variables in the expression *TP. */
6085 find_heap_var_refs (tree
*tp
, int *walk_subtrees
, void */
*data*/
)
6088 && (DECL_NAME (*tp
) == heap_uninit_identifier
6089 || DECL_NAME (*tp
) == heap_identifier
6090 || DECL_NAME (*tp
) == heap_deleted_identifier
))
6098 /* Find immediate function decls in *TP if any. */
6101 find_immediate_fndecl (tree
*tp
, int */
*walk_subtrees*/
, void */
*data*/
)
6103 if (TREE_CODE (*tp
) == FUNCTION_DECL
&& DECL_IMMEDIATE_FUNCTION_P (*tp
))
6108 /* ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
6109 STRICT has the same sense as for constant_value_1: true if we only allow
6110 conforming C++ constant expressions, or false if we want a constant value
6111 even if it doesn't conform.
6112 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
6113 per P0595 even when ALLOW_NON_CONSTANT is true.
6114 CONSTEXPR_DTOR is true when evaluating the dtor of a constexpr variable.
6115 OBJECT must be non-NULL in that case. */
6118 cxx_eval_outermost_constant_expr (tree t
, bool allow_non_constant
,
6120 bool manifestly_const_eval
= false,
6121 bool constexpr_dtor
= false,
6122 tree object
= NULL_TREE
)
6124 auto_timevar
time (TV_CONSTEXPR
);
6126 bool non_constant_p
= false;
6127 bool overflow_p
= false;
6129 constexpr_global_ctx global_ctx
;
6130 constexpr_ctx ctx
= { &global_ctx
, NULL
, NULL
, NULL
, NULL
, NULL
,
6131 allow_non_constant
, strict
,
6132 manifestly_const_eval
|| !allow_non_constant
};
6134 tree type
= initialized_type (t
);
6136 bool is_consteval
= false;
6137 if (VOID_TYPE_P (type
))
6140 /* Used for destructors of array elements. */
6141 type
= TREE_TYPE (object
);
6144 if (cxx_dialect
< cxx2a
)
6146 if (TREE_CODE (t
) != CALL_EXPR
&& TREE_CODE (t
) != AGGR_INIT_EXPR
)
6148 /* Calls to immediate functions returning void need to be
6150 tree fndecl
= cp_get_callee_fndecl_nofold (t
);
6151 if (fndecl
== NULL_TREE
|| !DECL_IMMEDIATE_FUNCTION_P (fndecl
))
6154 is_consteval
= true;
6157 else if (cxx_dialect
>= cxx2a
6158 && (TREE_CODE (t
) == CALL_EXPR
6159 || TREE_CODE (t
) == AGGR_INIT_EXPR
6160 || TREE_CODE (t
) == TARGET_EXPR
))
6162 /* For non-concept checks, determine if it is consteval. */
6163 if (!concept_check_p (t
))
6166 if (TREE_CODE (x
) == TARGET_EXPR
)
6167 x
= TARGET_EXPR_INITIAL (x
);
6168 tree fndecl
= cp_get_callee_fndecl_nofold (x
);
6169 if (fndecl
&& DECL_IMMEDIATE_FUNCTION_P (fndecl
))
6170 is_consteval
= true;
6173 if (AGGREGATE_TYPE_P (type
) || VECTOR_TYPE_P (type
))
6175 /* In C++14 an NSDMI can participate in aggregate initialization,
6176 and can refer to the address of the object being initialized, so
6177 we need to pass in the relevant VAR_DECL if we want to do the
6178 evaluation in a single pass. The evaluation will dynamically
6179 update ctx.values for the VAR_DECL. We use the same strategy
6180 for C++11 constexpr constructors that refer to the object being
6184 gcc_assert (object
&& VAR_P (object
));
6185 gcc_assert (DECL_DECLARED_CONSTEXPR_P (object
));
6186 gcc_assert (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object
));
6187 if (error_operand_p (DECL_INITIAL (object
)))
6189 ctx
.ctor
= unshare_expr (DECL_INITIAL (object
));
6190 TREE_READONLY (ctx
.ctor
) = false;
6191 /* Temporarily force decl_really_constant_value to return false
6192 for it, we want to use ctx.ctor for the current value instead. */
6193 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object
) = false;
6197 ctx
.ctor
= build_constructor (type
, NULL
);
6198 CONSTRUCTOR_NO_CLEARING (ctx
.ctor
) = true;
6202 if (TREE_CODE (t
) == TARGET_EXPR
)
6203 object
= TARGET_EXPR_SLOT (t
);
6204 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
)
6205 object
= AGGR_INIT_EXPR_SLOT (t
);
6207 ctx
.object
= object
;
6209 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6210 (type
, TREE_TYPE (object
)));
6211 if (object
&& DECL_P (object
))
6212 global_ctx
.values
.put (object
, ctx
.ctor
);
6213 if (TREE_CODE (r
) == TARGET_EXPR
)
6214 /* Avoid creating another CONSTRUCTOR when we expand the
6216 r
= TARGET_EXPR_INITIAL (r
);
6219 auto_vec
<tree
, 16> cleanups
;
6220 global_ctx
.cleanups
= &cleanups
;
6222 instantiate_constexpr_fns (r
);
6223 r
= cxx_eval_constant_expression (&ctx
, r
,
6224 false, &non_constant_p
, &overflow_p
);
6226 if (!constexpr_dtor
)
6227 verify_constant (r
, allow_non_constant
, &non_constant_p
, &overflow_p
);
6229 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object
) = true;
6233 /* Evaluate the cleanups. */
6234 FOR_EACH_VEC_ELT_REVERSE (cleanups
, i
, cleanup
)
6235 cxx_eval_constant_expression (&ctx
, cleanup
, false,
6236 &non_constant_p
, &overflow_p
);
6238 /* Mutable logic is a bit tricky: we want to allow initialization of
6239 constexpr variables with mutable members, but we can't copy those
6240 members to another constexpr variable. */
6241 if (TREE_CODE (r
) == CONSTRUCTOR
&& CONSTRUCTOR_MUTABLE_POISON (r
))
6243 if (!allow_non_constant
)
6244 error ("%qE is not a constant expression because it refers to "
6245 "mutable subobjects of %qT", t
, type
);
6246 non_constant_p
= true;
6249 if (TREE_CODE (r
) == CONSTRUCTOR
&& CONSTRUCTOR_NO_CLEARING (r
))
6251 if (!allow_non_constant
)
6252 error ("%qE is not a constant expression because it refers to "
6253 "an incompletely initialized variable", t
);
6254 TREE_CONSTANT (r
) = false;
6255 non_constant_p
= true;
6258 if (!global_ctx
.heap_vars
.is_empty ())
6260 tree heap_var
= cp_walk_tree_without_duplicates (&r
, find_heap_var_refs
,
6265 if (!allow_non_constant
&& !non_constant_p
)
6266 error_at (DECL_SOURCE_LOCATION (heap_var
),
6267 "%qE is not a constant expression because it refers to "
6268 "a result of %<operator new%>", t
);
6270 non_constant_p
= true;
6272 FOR_EACH_VEC_ELT (global_ctx
.heap_vars
, i
, heap_var
)
6273 if (DECL_NAME (heap_var
) != heap_deleted_identifier
)
6275 if (!allow_non_constant
&& !non_constant_p
)
6276 error_at (DECL_SOURCE_LOCATION (heap_var
),
6277 "%qE is not a constant expression because allocated "
6278 "storage has not been deallocated", t
);
6280 non_constant_p
= true;
6284 /* Check that immediate invocation does not return an expression referencing
6285 any immediate function decls. They need to be allowed while parsing
6286 immediate functions, but can't leak outside of them. */
6289 && (current_function_decl
== NULL_TREE
6290 || !DECL_IMMEDIATE_FUNCTION_P (current_function_decl
)))
6291 if (tree immediate_fndecl
6292 = cp_walk_tree_without_duplicates (&r
, find_immediate_fndecl
,
6295 if (!allow_non_constant
&& !non_constant_p
)
6296 error_at (cp_expr_loc_or_input_loc (t
),
6297 "immediate evaluation returns address of immediate "
6298 "function %qD", immediate_fndecl
);
6300 non_constant_p
= true;
6303 /* Technically we should check this for all subexpressions, but that
6304 runs into problems with our internal representation of pointer
6305 subtraction and the 5.19 rules are still in flux. */
6306 if (CONVERT_EXPR_CODE_P (TREE_CODE (r
))
6307 && ARITHMETIC_TYPE_P (TREE_TYPE (r
))
6308 && TREE_CODE (TREE_OPERAND (r
, 0)) == ADDR_EXPR
)
6310 if (!allow_non_constant
)
6311 error ("conversion from pointer type %qT "
6312 "to arithmetic type %qT in a constant expression",
6313 TREE_TYPE (TREE_OPERAND (r
, 0)), TREE_TYPE (r
));
6314 non_constant_p
= true;
6317 if (!non_constant_p
&& overflow_p
)
6318 non_constant_p
= true;
6320 /* Unshare the result unless it's a CONSTRUCTOR in which case it's already
6322 bool should_unshare
= true;
6323 if (r
== t
|| TREE_CODE (r
) == CONSTRUCTOR
)
6324 should_unshare
= false;
6326 if (non_constant_p
&& !allow_non_constant
)
6327 return error_mark_node
;
6328 else if (constexpr_dtor
)
6330 else if (non_constant_p
&& TREE_CONSTANT (r
))
6332 /* If __builtin_is_constant_evaluated () was evaluated to true
6333 and the result is not a valid constant expression, we need to
6335 if (manifestly_const_eval
)
6336 return cxx_eval_outermost_constant_expr (t
, true, strict
,
6337 false, false, object
);
6338 /* This isn't actually constant, so unset TREE_CONSTANT.
6339 Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires
6340 it to be set if it is invariant address, even when it is not
6341 a valid C++ constant expression. Wrap it with a NOP_EXPR
6343 if (EXPR_P (r
) && TREE_CODE (r
) != ADDR_EXPR
)
6345 else if (TREE_CODE (r
) == CONSTRUCTOR
)
6346 r
= build1 (VIEW_CONVERT_EXPR
, TREE_TYPE (r
), r
);
6348 r
= build_nop (TREE_TYPE (r
), r
);
6349 TREE_CONSTANT (r
) = false;
6351 else if (non_constant_p
)
6355 r
= unshare_expr (r
);
6357 if (TREE_CODE (r
) == CONSTRUCTOR
&& CLASS_TYPE_P (TREE_TYPE (r
)))
6359 r
= adjust_temp_type (type
, r
);
6360 if (TREE_CODE (t
) == TARGET_EXPR
6361 && TARGET_EXPR_INITIAL (t
) == r
)
6363 else if (TREE_CODE (t
) != CONSTRUCTOR
)
6365 r
= get_target_expr_sfinae (r
, tf_warning_or_error
| tf_no_cleanup
);
6366 TREE_CONSTANT (r
) = true;
6373 /* If T represents a constant expression returns its reduced value.
6374 Otherwise return error_mark_node. If T is dependent, then
6378 cxx_constant_value (tree t
, tree decl
)
6380 return cxx_eval_outermost_constant_expr (t
, false, true, true, false, decl
);
6383 /* Like cxx_constant_value, but used for evaluation of constexpr destructors
6384 of constexpr variables. The actual initializer of DECL is not modified. */
6387 cxx_constant_dtor (tree t
, tree decl
)
6389 cxx_eval_outermost_constant_expr (t
, false, true, true, true, decl
);
6392 /* Helper routine for fold_simple function. Either return simplified
6393 expression T, otherwise NULL_TREE.
6394 In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold
6395 even if we are within template-declaration. So be careful on call, as in
6396 such case types can be undefined. */
6399 fold_simple_1 (tree t
)
6402 enum tree_code code
= TREE_CODE (t
);
6414 return fold_sizeof_expr (t
);
6423 case TRUTH_NOT_EXPR
:
6425 case VIEW_CONVERT_EXPR
:
6428 case FIX_TRUNC_EXPR
:
6429 case FIXED_CONVERT_EXPR
:
6430 case ADDR_SPACE_CONVERT_EXPR
:
6432 op1
= TREE_OPERAND (t
, 0);
6434 t
= const_unop (code
, TREE_TYPE (t
), op1
);
6438 if (CONVERT_EXPR_CODE_P (code
)
6439 && TREE_OVERFLOW_P (t
) && !TREE_OVERFLOW_P (op1
))
6440 TREE_OVERFLOW (t
) = false;
6448 /* If T is a simple constant expression, returns its simplified value.
6449 Otherwise returns T. In contrast to maybe_constant_value we
6450 simplify only few operations on constant-expressions, and we don't
6451 try to simplify constexpressions. */
6454 fold_simple (tree t
)
6456 if (processing_template_decl
)
6459 tree r
= fold_simple_1 (t
);
6466 /* If T is a constant expression, returns its reduced value.
6467 Otherwise, if T does not have TREE_CONSTANT set, returns T.
6468 Otherwise, returns a version of T without TREE_CONSTANT.
6469 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated
6472 static GTY((deletable
)) hash_map
<tree
, tree
> *cv_cache
;
6475 maybe_constant_value (tree t
, tree decl
, bool manifestly_const_eval
)
6479 if (!is_nondependent_constant_expression (t
))
6481 if (TREE_OVERFLOW_P (t
))
6483 t
= build_nop (TREE_TYPE (t
), t
);
6484 TREE_CONSTANT (t
) = false;
6488 else if (CONSTANT_CLASS_P (t
))
6489 /* No caching or evaluation needed. */
6492 if (manifestly_const_eval
)
6493 return cxx_eval_outermost_constant_expr (t
, true, true, true, false, decl
);
6495 if (cv_cache
== NULL
)
6496 cv_cache
= hash_map
<tree
, tree
>::create_ggc (101);
6497 if (tree
*cached
= cv_cache
->get (t
))
6500 r
= cxx_eval_outermost_constant_expr (t
, true, true, false, false, decl
);
6501 gcc_checking_assert (r
== t
6502 || CONVERT_EXPR_P (t
)
6503 || TREE_CODE (t
) == VIEW_CONVERT_EXPR
6504 || (TREE_CONSTANT (t
) && !TREE_CONSTANT (r
))
6505 || !cp_tree_equal (r
, t
));
6506 cv_cache
->put (t
, r
);
6510 /* Dispose of the whole CV_CACHE. */
6513 clear_cv_cache (void)
6515 if (cv_cache
!= NULL
)
6519 /* Dispose of the whole CV_CACHE, FOLD_CACHE, and satisfaction caches. */
6522 clear_cv_and_fold_caches (bool sat
/*= true*/)
6525 clear_fold_cache ();
6527 clear_satisfaction_cache ();
6530 /* Internal function handling expressions in templates for
6531 fold_non_dependent_expr and fold_non_dependent_init.
6533 If we're in a template, but T isn't value dependent, simplify
6534 it. We're supposed to treat:
6536 template <typename T> void f(T[1 + 1]);
6537 template <typename T> void f(T[2]);
6539 as two declarations of the same function, for example. */
6542 fold_non_dependent_expr_template (tree t
, tsubst_flags_t complain
,
6543 bool manifestly_const_eval
,
6546 gcc_assert (processing_template_decl
);
6548 if (is_nondependent_constant_expression (t
))
6550 processing_template_decl_sentinel s
;
6551 t
= instantiate_non_dependent_expr_internal (t
, complain
);
6553 if (type_unknown_p (t
) || BRACE_ENCLOSED_INITIALIZER_P (t
))
6555 if (TREE_OVERFLOW_P (t
))
6557 t
= build_nop (TREE_TYPE (t
), t
);
6558 TREE_CONSTANT (t
) = false;
6563 tree r
= cxx_eval_outermost_constant_expr (t
, true, true,
6564 manifestly_const_eval
,
6566 /* cp_tree_equal looks through NOPs, so allow them. */
6567 gcc_checking_assert (r
== t
6568 || CONVERT_EXPR_P (t
)
6569 || TREE_CODE (t
) == VIEW_CONVERT_EXPR
6570 || (TREE_CONSTANT (t
) && !TREE_CONSTANT (r
))
6571 || !cp_tree_equal (r
, t
));
6574 else if (TREE_OVERFLOW_P (t
))
6576 t
= build_nop (TREE_TYPE (t
), t
);
6577 TREE_CONSTANT (t
) = false;
6583 /* Like maybe_constant_value but first fully instantiate the argument.
6585 Note: this is equivalent to instantiate_non_dependent_expr_sfinae
6586 (t, complain) followed by maybe_constant_value but is more efficient,
6587 because it calls instantiation_dependent_expression_p and
6588 potential_constant_expression at most once.
6589 The manifestly_const_eval argument is passed to maybe_constant_value.
6591 Callers should generally pass their active complain, or if they are in a
6592 non-template, diagnosing context, they can use the default of
6593 tf_warning_or_error. Callers that might be within a template context, don't
6594 have a complain parameter, and aren't going to remember the result for long
6595 (e.g. null_ptr_cst_p), can pass tf_none and deal with error_mark_node
6599 fold_non_dependent_expr (tree t
,
6600 tsubst_flags_t complain
/* = tf_warning_or_error */,
6601 bool manifestly_const_eval
/* = false */,
6602 tree object
/* = NULL_TREE */)
6607 if (processing_template_decl
)
6608 return fold_non_dependent_expr_template (t
, complain
,
6609 manifestly_const_eval
, object
);
6611 return maybe_constant_value (t
, object
, manifestly_const_eval
);
6615 /* Like maybe_constant_init but first fully instantiate the argument. */
6618 fold_non_dependent_init (tree t
,
6619 tsubst_flags_t complain
/*=tf_warning_or_error*/,
6620 bool manifestly_const_eval
/*=false*/)
6625 if (processing_template_decl
)
6627 t
= fold_non_dependent_expr_template (t
, complain
,
6628 manifestly_const_eval
, NULL_TREE
);
6629 /* maybe_constant_init does this stripping, so do it here too. */
6630 if (TREE_CODE (t
) == TARGET_EXPR
)
6632 tree init
= TARGET_EXPR_INITIAL (t
);
6633 if (TREE_CODE (init
) == CONSTRUCTOR
)
6639 return maybe_constant_init (t
, NULL_TREE
, manifestly_const_eval
);
6642 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
6643 than wrapped in a TARGET_EXPR.
6644 ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
6645 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
6646 per P0595 even when ALLOW_NON_CONSTANT is true. */
6649 maybe_constant_init_1 (tree t
, tree decl
, bool allow_non_constant
,
6650 bool manifestly_const_eval
)
6654 if (TREE_CODE (t
) == EXPR_STMT
)
6655 t
= TREE_OPERAND (t
, 0);
6656 if (TREE_CODE (t
) == CONVERT_EXPR
6657 && VOID_TYPE_P (TREE_TYPE (t
)))
6658 t
= TREE_OPERAND (t
, 0);
6659 if (TREE_CODE (t
) == INIT_EXPR
)
6660 t
= TREE_OPERAND (t
, 1);
6661 if (TREE_CODE (t
) == TARGET_EXPR
)
6662 t
= TARGET_EXPR_INITIAL (t
);
6663 if (!is_nondependent_static_init_expression (t
))
6664 /* Don't try to evaluate it. */;
6665 else if (CONSTANT_CLASS_P (t
) && allow_non_constant
)
6666 /* No evaluation needed. */;
6668 t
= cxx_eval_outermost_constant_expr (t
, allow_non_constant
,
6670 manifestly_const_eval
, false, decl
);
6671 if (TREE_CODE (t
) == TARGET_EXPR
)
6673 tree init
= TARGET_EXPR_INITIAL (t
);
6674 if (TREE_CODE (init
) == CONSTRUCTOR
)
6680 /* Wrapper for maybe_constant_init_1 which permits non constants. */
6683 maybe_constant_init (tree t
, tree decl
, bool manifestly_const_eval
)
6685 return maybe_constant_init_1 (t
, decl
, true, manifestly_const_eval
);
6688 /* Wrapper for maybe_constant_init_1 which does not permit non constants. */
6691 cxx_constant_init (tree t
, tree decl
)
6693 return maybe_constant_init_1 (t
, decl
, false, true);
6697 /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
6698 /* Return true if the object referred to by REF has automatic or thread
6701 enum { ck_ok
, ck_bad
, ck_unknown
};
6703 check_automatic_or_tls (tree ref
)
6706 poly_int64 bitsize
, bitpos
;
6708 int volatilep
= 0, unsignedp
= 0;
6709 tree decl
= get_inner_reference (ref
, &bitsize
, &bitpos
, &offset
,
6710 &mode
, &unsignedp
, &volatilep
, false);
6713 /* If there isn't a decl in the middle, we don't know the linkage here,
6714 and this isn't a constant expression anyway. */
6717 dk
= decl_storage_duration (decl
);
6718 return (dk
== dk_auto
|| dk
== dk_thread
) ? ck_bad
: ck_ok
;
6722 /* Data structure for passing data from potential_constant_expression_1
6723 to check_for_return_continue via cp_walk_tree. */
6724 struct check_for_return_continue_data
{
6725 hash_set
<tree
> *pset
;
6729 /* Helper function for potential_constant_expression_1 SWITCH_STMT handling,
6730 called through cp_walk_tree. Return the first RETURN_EXPR found, or note
6731 the first CONTINUE_STMT if RETURN_EXPR is not found. */
6733 check_for_return_continue (tree
*tp
, int *walk_subtrees
, void *data
)
6736 check_for_return_continue_data
*d
= (check_for_return_continue_data
*) data
;
6737 switch (TREE_CODE (t
))
6743 if (d
->continue_stmt
== NULL_TREE
)
6744 d
->continue_stmt
= t
;
6748 if (tree r = cp_walk_tree (&x, check_for_return_continue, data, \
6752 /* For loops, walk subtrees manually, so that continue stmts found
6753 inside of the bodies of the loops are ignored. */
6756 RECUR (DO_COND (t
));
6757 s
= d
->continue_stmt
;
6758 RECUR (DO_BODY (t
));
6759 d
->continue_stmt
= s
;
6764 RECUR (WHILE_COND (t
));
6765 s
= d
->continue_stmt
;
6766 RECUR (WHILE_BODY (t
));
6767 d
->continue_stmt
= s
;
6772 RECUR (FOR_INIT_STMT (t
));
6773 RECUR (FOR_COND (t
));
6774 RECUR (FOR_EXPR (t
));
6775 s
= d
->continue_stmt
;
6776 RECUR (FOR_BODY (t
));
6777 d
->continue_stmt
= s
;
6780 case RANGE_FOR_STMT
:
6782 RECUR (RANGE_FOR_EXPR (t
));
6783 s
= d
->continue_stmt
;
6784 RECUR (RANGE_FOR_BODY (t
));
6785 d
->continue_stmt
= s
;
6789 case STATEMENT_LIST
:
6802 /* Return true if T denotes a potentially constant expression. Issue
6803 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
6804 an lvalue-rvalue conversion is implied. If NOW is true, we want to
6805 consider the expression in the current context, independent of constexpr
6808 C++0x [expr.const] used to say
6810 6 An expression is a potential constant expression if it is
6811 a constant expression where all occurrences of function
6812 parameters are replaced by arbitrary constant expressions
6813 of the appropriate type.
6815 2 A conditional expression is a constant expression unless it
6816 involves one of the following as a potentially evaluated
6817 subexpression (3.2), but subexpressions of logical AND (5.14),
6818 logical OR (5.15), and conditional (5.16) operations that are
6819 not evaluated are not considered. */
6822 potential_constant_expression_1 (tree t
, bool want_rval
, bool strict
, bool now
,
6823 tsubst_flags_t flags
, tree
*jump_target
)
6825 #define RECUR(T,RV) \
6826 potential_constant_expression_1 ((T), (RV), strict, now, flags, jump_target)
6828 enum { any
= false, rval
= true };
6832 if (t
== error_mark_node
)
6836 location_t loc
= cp_expr_loc_or_input_loc (t
);
6839 /* If we are jumping, ignore everything. This is simpler than the
6840 cxx_eval_constant_expression handling because we only need to be
6841 conservatively correct, and we don't necessarily have a constant value
6842 available, so we don't bother with switch tracking. */
6845 if (TREE_THIS_VOLATILE (t
) && want_rval
)
6847 if (flags
& tf_error
)
6848 error_at (loc
, "lvalue-to-rvalue conversion of a volatile lvalue "
6849 "%qE with type %qT", t
, TREE_TYPE (t
));
6852 if (CONSTANT_CLASS_P (t
))
6854 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_TYPED
)
6855 && TREE_TYPE (t
) == error_mark_node
)
6858 switch (TREE_CODE (t
))
6864 case TEMPLATE_ID_EXPR
:
6867 case CASE_LABEL_EXPR
:
6874 case TEMPLATE_PARM_INDEX
:
6876 case IDENTIFIER_NODE
:
6877 case USERDEF_LITERAL
:
6878 /* We can see a FIELD_DECL in a pointer-to-member expression. */
6883 case PLACEHOLDER_EXPR
:
6886 case DEBUG_BEGIN_STMT
:
6890 if (!RECUR (TREE_OPERAND (t
, 0), any
))
6902 if (flags
& tf_error
)
6903 error ("%qE is not a constant expression", t
);
6908 case AGGR_INIT_EXPR
:
6910 /* -- an invocation of a function other than a constexpr function
6911 or a constexpr constructor. */
6913 tree fun
= get_function_named_in_call (t
);
6914 const int nargs
= call_expr_nargs (t
);
6917 if (fun
== NULL_TREE
)
6919 /* Reset to allow the function to continue past the end
6920 of the block below. Otherwise return early. */
6923 if (TREE_CODE (t
) == CALL_EXPR
6924 && CALL_EXPR_FN (t
) == NULL_TREE
)
6925 switch (CALL_EXPR_IFN (t
))
6927 /* These should be ignored, they are optimized away from
6928 constexpr functions. */
6929 case IFN_UBSAN_NULL
:
6930 case IFN_UBSAN_BOUNDS
:
6931 case IFN_UBSAN_VPTR
:
6932 case IFN_FALLTHROUGH
:
6935 case IFN_ADD_OVERFLOW
:
6936 case IFN_SUB_OVERFLOW
:
6937 case IFN_MUL_OVERFLOW
:
6939 case IFN_VEC_CONVERT
:
6949 /* fold_call_expr can't do anything with IFN calls. */
6950 if (flags
& tf_error
)
6951 error_at (loc
, "call to internal function %qE", t
);
6956 if (fun
&& is_overloaded_fn (fun
))
6958 if (TREE_CODE (fun
) == FUNCTION_DECL
)
6960 if (builtin_valid_in_constant_expr_p (fun
))
6962 if (!DECL_DECLARED_CONSTEXPR_P (fun
)
6963 /* Allow any built-in function; if the expansion
6964 isn't constant, we'll deal with that then. */
6965 && !fndecl_built_in_p (fun
)
6966 /* In C++2a, replaceable global allocation functions
6967 are constant expressions. */
6968 && (!cxx_replaceable_global_alloc_fn (fun
)
6969 || TREE_CODE (t
) != CALL_EXPR
6970 || (!CALL_FROM_NEW_OR_DELETE_P (t
)
6971 && (current_function_decl
== NULL_TREE
6972 || !is_std_allocator_allocate
6973 (current_function_decl
))))
6974 /* Allow placement new in std::construct_at. */
6975 && (!cxx_placement_new_fn (fun
)
6976 || TREE_CODE (t
) != CALL_EXPR
6977 || current_function_decl
== NULL_TREE
6978 || !is_std_construct_at (current_function_decl
))
6979 && !cxx_dynamic_cast_fn_p (fun
))
6981 if (flags
& tf_error
)
6983 error_at (loc
, "call to non-%<constexpr%> function %qD",
6985 explain_invalid_constexpr_fn (fun
);
6989 /* A call to a non-static member function takes the address
6990 of the object as the first argument. But in a constant
6991 expression the address will be folded away, so look
6993 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun
)
6994 && !DECL_CONSTRUCTOR_P (fun
))
6996 tree x
= get_nth_callarg (t
, 0);
6997 if (is_this_parameter (x
))
6999 /* Don't require an immediately constant value, as
7000 constexpr substitution might not use the value. */
7001 bool sub_now
= false;
7002 if (!potential_constant_expression_1 (x
, rval
, strict
,
7011 if (!RECUR (fun
, true))
7013 fun
= get_first_fn (fun
);
7015 /* Skip initial arguments to base constructors. */
7016 if (DECL_BASE_CONSTRUCTOR_P (fun
))
7017 i
= num_artificial_parms_for (fun
);
7018 fun
= DECL_ORIGIN (fun
);
7022 if (RECUR (fun
, rval
))
7023 /* Might end up being a constant function pointer. */;
7027 for (; i
< nargs
; ++i
)
7029 tree x
= get_nth_callarg (t
, i
);
7030 /* In a template, reference arguments haven't been converted to
7031 REFERENCE_TYPE and we might not even know if the parameter
7032 is a reference, so accept lvalue constants too. */
7033 bool rv
= processing_template_decl
? any
: rval
;
7034 /* Don't require an immediately constant value, as constexpr
7035 substitution might not use the value of the argument. */
7036 bool sub_now
= false;
7037 if (!potential_constant_expression_1 (x
, rv
, strict
,
7038 sub_now
, flags
, jump_target
))
7044 case NON_LVALUE_EXPR
:
7045 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
7046 -- an lvalue of integral type that refers to a non-volatile
7047 const variable or static data member initialized with
7048 constant expressions, or
7050 -- an lvalue of literal type that refers to non-volatile
7051 object defined with constexpr, or that refers to a
7052 sub-object of such an object; */
7053 return RECUR (TREE_OPERAND (t
, 0), rval
);
7056 if (DECL_HAS_VALUE_EXPR_P (t
))
7058 if (now
&& is_normal_capture_proxy (t
))
7060 /* -- in a lambda-expression, a reference to this or to a
7061 variable with automatic storage duration defined outside that
7062 lambda-expression, where the reference would be an
7064 if (flags
& tf_error
)
7066 tree cap
= DECL_CAPTURED_VARIABLE (t
);
7067 error ("lambda capture of %qE is not a constant expression",
7069 if (!want_rval
&& decl_constant_var_p (cap
))
7070 inform (input_location
, "because it is used as a glvalue");
7074 return RECUR (DECL_VALUE_EXPR (t
), rval
);
7077 && !var_in_maybe_constexpr_fn (t
)
7078 && !type_dependent_expression_p (t
)
7079 && !decl_maybe_constant_var_p (t
)
7081 || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t
))
7082 || (DECL_INITIAL (t
)
7083 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t
)))
7084 && COMPLETE_TYPE_P (TREE_TYPE (t
))
7085 && !is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
7087 if (flags
& tf_error
)
7088 non_const_var_error (loc
, t
);
7094 if (REINTERPRET_CAST_P (t
))
7096 if (flags
& tf_error
)
7097 error_at (loc
, "%<reinterpret_cast%> is not a constant expression");
7102 case VIEW_CONVERT_EXPR
:
7103 /* -- a reinterpret_cast. FIXME not implemented, and this rule
7104 may change to something more specific to type-punning (DR 1312). */
7106 tree from
= TREE_OPERAND (t
, 0);
7107 if (location_wrapper_p (t
))
7108 return (RECUR (from
, want_rval
));
7109 if (INDIRECT_TYPE_P (TREE_TYPE (t
)))
7111 STRIP_ANY_LOCATION_WRAPPER (from
);
7112 if (TREE_CODE (from
) == INTEGER_CST
7113 && !integer_zerop (from
))
7115 if (flags
& tf_error
)
7117 "%<reinterpret_cast%> from integer to pointer");
7121 return (RECUR (from
, TREE_CODE (t
) != VIEW_CONVERT_EXPR
));
7124 case ADDRESSOF_EXPR
:
7125 /* This is like ADDR_EXPR, except it won't form pointer-to-member. */
7126 t
= TREE_OPERAND (t
, 0);
7127 goto handle_addr_expr
;
7130 /* -- a unary operator & that is applied to an lvalue that
7131 designates an object with thread or automatic storage
7133 t
= TREE_OPERAND (t
, 0);
7135 if (TREE_CODE (t
) == OFFSET_REF
&& PTRMEM_OK_P (t
))
7136 /* A pointer-to-member constant. */
7141 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
7142 any checking here, as we might dereference the pointer later. If
7143 we remove this code, also remove check_automatic_or_tls. */
7144 i
= check_automatic_or_tls (t
);
7149 if (flags
& tf_error
)
7150 error ("address-of an object %qE with thread local or "
7151 "automatic storage is not a constant expression", t
);
7155 return RECUR (t
, any
);
7163 /* -- a class member access unless its postfix-expression is
7164 of literal type or of pointer to literal type. */
7165 /* This test would be redundant, as it follows from the
7166 postfix-expression being a potential constant expression. */
7167 if (type_unknown_p (t
))
7169 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
7171 case EXPR_PACK_EXPANSION
:
7172 return RECUR (PACK_EXPANSION_PATTERN (t
), want_rval
);
7176 tree x
= TREE_OPERAND (t
, 0);
7178 if (is_this_parameter (x
) && !is_capture_proxy (x
))
7180 if (!var_in_maybe_constexpr_fn (x
))
7182 if (flags
& tf_error
)
7183 error_at (loc
, "use of %<this%> in a constant expression");
7188 return RECUR (x
, rval
);
7191 case STATEMENT_LIST
:
7193 tree_stmt_iterator i
;
7194 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
7196 if (!RECUR (tsi_stmt (i
), any
))
7204 if (cxx_dialect
< cxx14
)
7206 if (!RECUR (TREE_OPERAND (t
, 0), any
))
7208 /* Just ignore clobbers. */
7209 if (TREE_CLOBBER_P (TREE_OPERAND (t
, 1)))
7211 if (!RECUR (TREE_OPERAND (t
, 1), rval
))
7216 if (cxx_dialect
< cxx14
)
7218 if (!RECUR (TREE_OPERAND (t
, 0), rval
))
7220 if (!RECUR (TREE_OPERAND (t
, 2), rval
))
7225 if (!RECUR (DO_COND (t
), rval
))
7227 if (!RECUR (DO_BODY (t
), any
))
7229 if (breaks (jump_target
) || continues (jump_target
))
7230 *jump_target
= NULL_TREE
;
7234 if (!RECUR (FOR_INIT_STMT (t
), any
))
7237 if (!RECUR (tmp
, rval
))
7241 if (!processing_template_decl
)
7242 tmp
= cxx_eval_outermost_constant_expr (tmp
, true);
7243 /* If we couldn't evaluate the condition, it might not ever be
7245 if (!integer_onep (tmp
))
7248 if (!RECUR (FOR_EXPR (t
), any
))
7250 if (!RECUR (FOR_BODY (t
), any
))
7252 if (breaks (jump_target
) || continues (jump_target
))
7253 *jump_target
= NULL_TREE
;
7256 case RANGE_FOR_STMT
:
7257 if (!RECUR (RANGE_FOR_INIT_STMT (t
), any
))
7259 if (!RECUR (RANGE_FOR_EXPR (t
), any
))
7261 if (!RECUR (RANGE_FOR_BODY (t
), any
))
7263 if (breaks (jump_target
) || continues (jump_target
))
7264 *jump_target
= NULL_TREE
;
7268 tmp
= WHILE_COND (t
);
7269 if (!RECUR (tmp
, rval
))
7271 if (!processing_template_decl
)
7272 tmp
= cxx_eval_outermost_constant_expr (tmp
, true);
7273 /* If we couldn't evaluate the condition, it might not ever be true. */
7274 if (!integer_onep (tmp
))
7276 if (!RECUR (WHILE_BODY (t
), any
))
7278 if (breaks (jump_target
) || continues (jump_target
))
7279 *jump_target
= NULL_TREE
;
7283 if (!RECUR (SWITCH_STMT_COND (t
), rval
))
7285 /* FIXME we don't check SWITCH_STMT_BODY currently, because even
7286 unreachable labels would be checked and it is enough if there is
7287 a single switch cond value for which it is a valid constant
7288 expression. We need to check if there are any RETURN_EXPRs
7289 or CONTINUE_STMTs inside of the body though, as in that case
7290 we need to set *jump_target. */
7293 hash_set
<tree
> pset
;
7294 check_for_return_continue_data data
= { &pset
, NULL_TREE
};
7296 = cp_walk_tree (&SWITCH_STMT_BODY (t
), check_for_return_continue
,
7298 /* The switch might return. */
7299 *jump_target
= ret_expr
;
7300 else if (data
.continue_stmt
)
7301 /* The switch can't return, but might continue. */
7302 *jump_target
= data
.continue_stmt
;
7307 return RECUR (STMT_EXPR_STMT (t
), rval
);
7310 if (cxx_dialect
>= cxx17
)
7311 /* In C++17 lambdas can be constexpr, don't give up yet. */
7313 else if (flags
& tf_error
)
7314 error_at (loc
, "lambda-expression is not a constant expression "
7318 case DYNAMIC_CAST_EXPR
:
7319 case PSEUDO_DTOR_EXPR
:
7323 case VEC_DELETE_EXPR
:
7329 case OMP_DISTRIBUTE
:
7333 case OMP_TARGET_DATA
:
7342 case OMP_TARGET_UPDATE
:
7343 case OMP_TARGET_ENTER_DATA
:
7344 case OMP_TARGET_EXIT_DATA
:
7346 case OMP_ATOMIC_READ
:
7347 case OMP_ATOMIC_CAPTURE_OLD
:
7348 case OMP_ATOMIC_CAPTURE_NEW
:
7354 case OACC_HOST_DATA
:
7358 case OACC_ENTER_DATA
:
7359 case OACC_EXIT_DATA
:
7361 /* GCC internal stuff. */
7363 case TRANSACTION_EXPR
:
7364 case AT_ENCODE_EXPR
:
7366 if (flags
& tf_error
)
7367 error_at (loc
, "expression %qE is not a constant expression", t
);
7371 if (flags
& tf_error
)
7372 inline_asm_in_constexpr_error (loc
);
7376 if (cxx_dialect
>= cxx2a
)
7377 /* In C++2a virtual calls can be constexpr, don't give up yet. */
7379 else if (flags
& tf_error
)
7381 "virtual functions cannot be %<constexpr%> before C++2a");
7385 /* In C++20, a typeid expression whose operand is of polymorphic
7386 class type can be constexpr. */
7388 tree e
= TREE_OPERAND (t
, 0);
7389 if (cxx_dialect
< cxx2a
7392 && !type_dependent_expression_p (e
)
7393 && TYPE_POLYMORPHIC_P (TREE_TYPE (e
)))
7395 if (flags
& tf_error
)
7396 error_at (loc
, "%<typeid%> is not a constant expression "
7397 "because %qE is of polymorphic type", e
);
7403 case POINTER_DIFF_EXPR
:
7414 case SPACESHIP_EXPR
:
7418 case PREINCREMENT_EXPR
:
7419 case POSTINCREMENT_EXPR
:
7420 case PREDECREMENT_EXPR
:
7421 case POSTDECREMENT_EXPR
:
7422 if (cxx_dialect
< cxx14
)
7428 if (TYPE_P (TREE_OPERAND (t
, 0)))
7434 case FIX_TRUNC_EXPR
:
7439 case TRUTH_NOT_EXPR
:
7440 case FIXED_CONVERT_EXPR
:
7441 case UNARY_PLUS_EXPR
:
7442 case UNARY_LEFT_FOLD_EXPR
:
7443 case UNARY_RIGHT_FOLD_EXPR
:
7445 return RECUR (TREE_OPERAND (t
, 0), rval
);
7448 case CONST_CAST_EXPR
:
7449 case STATIC_CAST_EXPR
:
7450 case REINTERPRET_CAST_EXPR
:
7451 case IMPLICIT_CONV_EXPR
:
7452 if (cxx_dialect
< cxx11
7453 && !dependent_type_p (TREE_TYPE (t
))
7454 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
)))
7455 /* In C++98, a conversion to non-integral type can't be part of a
7456 constant expression. */
7458 if (flags
& tf_error
)
7460 "cast to non-integral type %qT in a constant expression",
7464 /* This might be a conversion from a class to a (potentially) literal
7465 type. Let's consider it potentially constant since the conversion
7466 might be a constexpr user-defined conversion. */
7467 else if (cxx_dialect
>= cxx11
7468 && (dependent_type_p (TREE_TYPE (t
))
7469 || !COMPLETE_TYPE_P (TREE_TYPE (t
))
7470 || literal_type_p (TREE_TYPE (t
)))
7471 && TREE_OPERAND (t
, 0))
7473 tree type
= TREE_TYPE (TREE_OPERAND (t
, 0));
7474 /* If this is a dependent type, it could end up being a class
7475 with conversions. */
7476 if (type
== NULL_TREE
|| WILDCARD_TYPE_P (type
))
7478 /* Or a non-dependent class which has conversions. */
7479 else if (CLASS_TYPE_P (type
)
7480 && (TYPE_HAS_CONVERSION (type
) || dependent_scope_p (type
)))
7484 return (RECUR (TREE_OPERAND (t
, 0),
7485 !TYPE_REF_P (TREE_TYPE (t
))));
7488 return RECUR (BIND_EXPR_BODY (t
), want_rval
);
7490 case CLEANUP_POINT_EXPR
:
7491 case MUST_NOT_THROW_EXPR
:
7492 case TRY_CATCH_EXPR
:
7497 case NON_DEPENDENT_EXPR
:
7498 /* For convenience. */
7501 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
7504 tmp
= DECL_EXPR_DECL (t
);
7505 if (VAR_P (tmp
) && !DECL_ARTIFICIAL (tmp
))
7507 if (TREE_STATIC (tmp
))
7509 if (flags
& tf_error
)
7510 error_at (DECL_SOURCE_LOCATION (tmp
), "%qD declared "
7511 "%<static%> in %<constexpr%> context", tmp
);
7514 else if (CP_DECL_THREAD_LOCAL_P (tmp
))
7516 if (flags
& tf_error
)
7517 error_at (DECL_SOURCE_LOCATION (tmp
), "%qD declared "
7518 "%<thread_local%> in %<constexpr%> context", tmp
);
7521 else if (!check_for_uninitialized_const_var
7522 (tmp
, /*constexpr_context_p=*/true, flags
))
7525 return RECUR (tmp
, want_rval
);
7527 case TRY_FINALLY_EXPR
:
7528 return (RECUR (TREE_OPERAND (t
, 0), want_rval
)
7529 && RECUR (TREE_OPERAND (t
, 1), any
));
7532 return RECUR (TREE_OPERAND (t
, 1), want_rval
);
7535 if (!TARGET_EXPR_DIRECT_INIT_P (t
)
7536 && !literal_type_p (TREE_TYPE (t
)))
7538 if (flags
& tf_error
)
7540 auto_diagnostic_group d
;
7541 error_at (loc
, "temporary of non-literal type %qT in a "
7542 "constant expression", TREE_TYPE (t
));
7543 explain_non_literal_class (TREE_TYPE (t
));
7549 return RECUR (TREE_OPERAND (t
, 1), rval
);
7553 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (t
);
7554 constructor_elt
*ce
;
7555 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
7556 if (!RECUR (ce
->value
, want_rval
))
7563 gcc_assert (TREE_PURPOSE (t
) == NULL_TREE
7564 || DECL_P (TREE_PURPOSE (t
)));
7565 if (!RECUR (TREE_VALUE (t
), want_rval
))
7567 if (TREE_CHAIN (t
) == NULL_TREE
)
7569 return RECUR (TREE_CHAIN (t
), want_rval
);
7572 case TRUNC_DIV_EXPR
:
7574 case FLOOR_DIV_EXPR
:
7575 case ROUND_DIV_EXPR
:
7576 case TRUNC_MOD_EXPR
:
7578 case ROUND_MOD_EXPR
:
7580 tree denom
= TREE_OPERAND (t
, 1);
7581 if (!RECUR (denom
, rval
))
7583 /* We can't call cxx_eval_outermost_constant_expr on an expression
7584 that hasn't been through instantiate_non_dependent_expr yet. */
7585 if (!processing_template_decl
)
7586 denom
= cxx_eval_outermost_constant_expr (denom
, true);
7587 if (integer_zerop (denom
))
7589 if (flags
& tf_error
)
7590 error ("division by zero is not a constant expression");
7596 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
7602 /* check_return_expr sometimes wraps a TARGET_EXPR in a
7603 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
7604 introduced by build_call_a. */
7605 tree op0
= TREE_OPERAND (t
, 0);
7606 tree op1
= TREE_OPERAND (t
, 1);
7608 if ((TREE_CODE (op0
) == TARGET_EXPR
&& op1
== TARGET_EXPR_SLOT (op0
))
7609 || TREE_CODE (op1
) == EMPTY_CLASS_EXPR
)
7610 return RECUR (op0
, want_rval
);
7615 /* If the first operand is the non-short-circuit constant, look at
7616 the second operand; otherwise we only care about the first one for
7618 case TRUTH_AND_EXPR
:
7619 case TRUTH_ANDIF_EXPR
:
7620 tmp
= boolean_true_node
;
7623 case TRUTH_ORIF_EXPR
:
7624 tmp
= boolean_false_node
;
7627 tree op
= TREE_OPERAND (t
, 0);
7628 if (!RECUR (op
, rval
))
7630 if (!processing_template_decl
)
7631 op
= cxx_eval_outermost_constant_expr (op
, true);
7632 if (tree_int_cst_equal (op
, tmp
))
7633 return RECUR (TREE_OPERAND (t
, 1), rval
);
7640 case POINTER_PLUS_EXPR
:
7642 case EXACT_DIV_EXPR
:
7650 case TRUTH_XOR_EXPR
:
7651 case UNORDERED_EXPR
:
7664 case ARRAY_RANGE_REF
:
7668 case BINARY_LEFT_FOLD_EXPR
:
7669 case BINARY_RIGHT_FOLD_EXPR
:
7671 for (i
= 0; i
< 2; ++i
)
7672 if (!RECUR (TREE_OPERAND (t
, i
), want_rval
))
7677 for (i
= 0; i
< 3; ++i
)
7678 if (!RECUR (TREE_OPERAND (t
, i
), true))
7683 if (COND_EXPR_IS_VEC_DELETE (t
) && cxx_dialect
< cxx2a
)
7685 if (flags
& tf_error
)
7686 error_at (loc
, "%<delete[]%> is not a constant expression");
7692 /* If the condition is a known constant, we know which of the legs we
7693 care about; otherwise we only require that the condition and
7694 either of the legs be potentially constant. */
7695 tmp
= TREE_OPERAND (t
, 0);
7696 if (!RECUR (tmp
, rval
))
7698 if (!processing_template_decl
)
7699 tmp
= cxx_eval_outermost_constant_expr (tmp
, true);
7700 if (integer_zerop (tmp
))
7701 return RECUR (TREE_OPERAND (t
, 2), want_rval
);
7702 else if (TREE_CODE (tmp
) == INTEGER_CST
)
7703 return RECUR (TREE_OPERAND (t
, 1), want_rval
);
7704 for (i
= 1; i
< 3; ++i
)
7705 if (potential_constant_expression_1 (TREE_OPERAND (t
, i
),
7706 want_rval
, strict
, now
,
7707 tf_none
, jump_target
))
7709 if (flags
& tf_error
)
7710 error_at (loc
, "expression %qE is not a constant expression", t
);
7714 if (VEC_INIT_EXPR_IS_CONSTEXPR (t
))
7716 if (flags
& tf_error
)
7718 error_at (loc
, "non-constant array initialization");
7719 diagnose_non_constexpr_vec_init (t
);
7725 /* We can see these in statement-expressions. */
7729 if (!RECUR (CLEANUP_BODY (t
), any
))
7731 if (!CLEANUP_EH_ONLY (t
) && !RECUR (CLEANUP_EXPR (t
), any
))
7735 case EMPTY_CLASS_EXPR
:
7740 tree
*target
= &TREE_OPERAND (t
, 0);
7741 /* Gotos representing break and continue are OK. */
7742 if (breaks (target
) || continues (target
))
7744 *jump_target
= *target
;
7747 if (flags
& tf_error
)
7748 error_at (loc
, "%<goto%> is not a constant expression");
7753 return RECUR (TREE_OPERAND (t
, 0), rval
);
7756 if (objc_is_property_ref (t
))
7759 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t
)));
7767 potential_constant_expression_1 (tree t
, bool want_rval
, bool strict
, bool now
,
7768 tsubst_flags_t flags
)
7770 tree target
= NULL_TREE
;
7771 return potential_constant_expression_1 (t
, want_rval
, strict
, now
,
7775 /* The main entry point to the above. */
7778 potential_constant_expression (tree t
)
7780 return potential_constant_expression_1 (t
, false, true, false, tf_none
);
7783 /* As above, but require a constant rvalue. */
7786 potential_rvalue_constant_expression (tree t
)
7788 return potential_constant_expression_1 (t
, true, true, false, tf_none
);
7791 /* Like above, but complain about non-constant expressions. */
7794 require_potential_constant_expression (tree t
)
7796 return potential_constant_expression_1 (t
, false, true, false,
7797 tf_warning_or_error
);
7800 /* Cross product of the above. */
7803 require_potential_rvalue_constant_expression (tree t
)
7805 return potential_constant_expression_1 (t
, true, true, false,
7806 tf_warning_or_error
);
7809 /* Like above, but don't consider PARM_DECL a potential_constant_expression. */
7812 require_rvalue_constant_expression (tree t
)
7814 return potential_constant_expression_1 (t
, true, true, true,
7815 tf_warning_or_error
);
7818 /* Like potential_constant_expression, but don't consider possible constexpr
7819 substitution of the current function. That is, PARM_DECL qualifies under
7820 potential_constant_expression, but not here.
7822 This is basically what you can check when any actual constant values might
7823 be value-dependent. */
7826 is_constant_expression (tree t
)
7828 return potential_constant_expression_1 (t
, false, true, true, tf_none
);
7831 /* Like above, but complain about non-constant expressions. */
7834 require_constant_expression (tree t
)
7836 return potential_constant_expression_1 (t
, false, true, true,
7837 tf_warning_or_error
);
7840 /* Like is_constant_expression, but allow const variables that are not allowed
7841 under constexpr rules. */
7844 is_static_init_expression (tree t
)
7846 return potential_constant_expression_1 (t
, false, false, true, tf_none
);
7849 /* Returns true if T is a potential constant expression that is not
7850 instantiation-dependent, and therefore a candidate for constant folding even
7854 is_nondependent_constant_expression (tree t
)
7856 return (!type_unknown_p (t
)
7857 && !BRACE_ENCLOSED_INITIALIZER_P (t
)
7858 && is_constant_expression (t
)
7859 && !instantiation_dependent_expression_p (t
));
7862 /* Returns true if T is a potential static initializer expression that is not
7863 instantiation-dependent. */
7866 is_nondependent_static_init_expression (tree t
)
7868 return (!type_unknown_p (t
)
7869 && !BRACE_ENCLOSED_INITIALIZER_P (t
)
7870 && is_static_init_expression (t
)
7871 && !instantiation_dependent_expression_p (t
));
7874 /* Finalize constexpr processing after parsing. */
7877 fini_constexpr (void)
7879 /* The contexpr call and fundef copies tables are no longer needed. */
7880 constexpr_call_table
= NULL
;
7881 fundef_copies_table
= NULL
;
7884 #include "gt-cp-constexpr.h"