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-2017 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"
37 static bool verify_constant (tree
, bool, bool *, bool *);
38 #define VERIFY_CONSTANT(X) \
40 if (verify_constant ((X), ctx->quiet, non_constant_p, overflow_p)) \
44 /* Returns true iff FUN is an instantiation of a constexpr function
45 template or a defaulted constexpr function. */
48 is_instantiation_of_constexpr (tree fun
)
50 return ((DECL_TEMPLOID_INSTANTIATION (fun
)
51 && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun
)))
52 || (DECL_DEFAULTED_FN (fun
)
53 && DECL_DECLARED_CONSTEXPR_P (fun
)));
56 /* Return true if T is a literal type. */
59 literal_type_p (tree t
)
63 || TREE_CODE (t
) == REFERENCE_TYPE
64 || (VOID_TYPE_P (t
) && cxx_dialect
>= cxx14
))
68 t
= complete_type (t
);
69 gcc_assert (COMPLETE_TYPE_P (t
) || errorcount
);
70 return CLASSTYPE_LITERAL_P (t
);
72 if (TREE_CODE (t
) == ARRAY_TYPE
)
73 return literal_type_p (strip_array_types (t
));
77 /* If DECL is a variable declared `constexpr', require its type
78 be literal. Return the DECL if OK, otherwise NULL. */
81 ensure_literal_type_for_constexpr_object (tree decl
)
83 tree type
= TREE_TYPE (decl
);
85 && (DECL_DECLARED_CONSTEXPR_P (decl
)
86 || var_in_constexpr_fn (decl
))
87 && !processing_template_decl
)
89 tree stype
= strip_array_types (type
);
90 if (CLASS_TYPE_P (stype
) && !COMPLETE_TYPE_P (complete_type (stype
)))
91 /* Don't complain here, we'll complain about incompleteness
92 when we try to initialize the variable. */;
93 else if (!literal_type_p (type
))
95 if (DECL_DECLARED_CONSTEXPR_P (decl
))
97 error ("the type %qT of %<constexpr%> variable %qD "
98 "is not literal", type
, decl
);
99 explain_non_literal_class (type
);
103 if (!is_instantiation_of_constexpr (current_function_decl
))
105 error ("variable %qD of non-literal type %qT in %<constexpr%> "
106 "function", decl
, type
);
107 explain_non_literal_class (type
);
109 cp_function_chain
->invalid_constexpr
= true;
117 /* Representation of entries in the constexpr function definition table. */
119 struct GTY((for_user
)) constexpr_fundef
{
124 struct constexpr_fundef_hasher
: ggc_ptr_hash
<constexpr_fundef
>
126 static hashval_t
hash (constexpr_fundef
*);
127 static bool equal (constexpr_fundef
*, constexpr_fundef
*);
130 /* This table holds all constexpr function definitions seen in
131 the current translation unit. */
133 static GTY (()) hash_table
<constexpr_fundef_hasher
> *constexpr_fundef_table
;
135 /* Utility function used for managing the constexpr function table.
136 Return true if the entries pointed to by P and Q are for the
137 same constexpr function. */
140 constexpr_fundef_hasher::equal (constexpr_fundef
*lhs
, constexpr_fundef
*rhs
)
142 return lhs
->decl
== rhs
->decl
;
145 /* Utility function used for managing the constexpr function table.
146 Return a hash value for the entry pointed to by Q. */
149 constexpr_fundef_hasher::hash (constexpr_fundef
*fundef
)
151 return DECL_UID (fundef
->decl
);
154 /* Return a previously saved definition of function FUN. */
156 static constexpr_fundef
*
157 retrieve_constexpr_fundef (tree fun
)
159 constexpr_fundef fundef
= { NULL
, NULL
};
160 if (constexpr_fundef_table
== NULL
)
164 return constexpr_fundef_table
->find (&fundef
);
167 /* Check whether the parameter and return types of FUN are valid for a
168 constexpr function, and complain if COMPLAIN. */
171 is_valid_constexpr_fn (tree fun
, bool complain
)
175 if (DECL_INHERITED_CTOR (fun
)
176 && TREE_CODE (fun
) == TEMPLATE_DECL
)
180 error ("inherited constructor %qD is not %<constexpr%>",
181 DECL_INHERITED_CTOR (fun
));
185 for (tree parm
= FUNCTION_FIRST_USER_PARM (fun
);
186 parm
!= NULL_TREE
; parm
= TREE_CHAIN (parm
))
187 if (!literal_type_p (TREE_TYPE (parm
)))
192 error ("invalid type for parameter %d of %<constexpr%> "
193 "function %q+#D", DECL_PARM_INDEX (parm
), fun
);
194 explain_non_literal_class (TREE_TYPE (parm
));
199 if (LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun
)) && cxx_dialect
< cxx17
)
203 inform (DECL_SOURCE_LOCATION (fun
),
204 "lambdas are implicitly %<constexpr%> only in C++17 and later");
206 else if (!DECL_CONSTRUCTOR_P (fun
))
208 tree rettype
= TREE_TYPE (TREE_TYPE (fun
));
209 if (!literal_type_p (rettype
))
214 error ("invalid return type %qT of %<constexpr%> function %q+D",
216 explain_non_literal_class (rettype
);
220 /* C++14 DR 1684 removed this restriction. */
221 if (cxx_dialect
< cxx14
222 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fun
)
223 && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun
)))
227 && pedwarn (DECL_SOURCE_LOCATION (fun
), OPT_Wpedantic
,
228 "enclosing class of %<constexpr%> non-static member "
229 "function %q+#D is not a literal type", fun
))
230 explain_non_literal_class (DECL_CONTEXT (fun
));
233 else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun
)))
237 error ("%q#T has virtual base classes", DECL_CONTEXT (fun
));
243 /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF
244 for a member of an anonymous aggregate, INIT is the initializer for that
245 member, and VEC_OUTER is the vector of constructor elements for the class
246 whose constructor we are processing. Add the initializer to the vector
247 and return true to indicate success. */
250 build_anon_member_initialization (tree member
, tree init
,
251 vec
<constructor_elt
, va_gc
> **vec_outer
)
253 /* MEMBER presents the relevant fields from the inside out, but we need
254 to build up the initializer from the outside in so that we can reuse
255 previously built CONSTRUCTORs if this is, say, the second field in an
256 anonymous struct. So we use a vec as a stack. */
257 auto_vec
<tree
, 2> fields
;
260 fields
.safe_push (TREE_OPERAND (member
, 1));
261 member
= TREE_OPERAND (member
, 0);
263 while (ANON_AGGR_TYPE_P (TREE_TYPE (member
))
264 && TREE_CODE (member
) == COMPONENT_REF
);
266 /* VEC has the constructor elements vector for the context of FIELD.
267 If FIELD is an anonymous aggregate, we will push inside it. */
268 vec
<constructor_elt
, va_gc
> **vec
= vec_outer
;
270 while (field
= fields
.pop(),
271 ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
274 /* If there is already an outer constructor entry for the anonymous
275 aggregate FIELD, use it; otherwise, insert one. */
276 if (vec_safe_is_empty (*vec
)
277 || (*vec
)->last().index
!= field
)
279 ctor
= build_constructor (TREE_TYPE (field
), NULL
);
280 CONSTRUCTOR_APPEND_ELT (*vec
, field
, ctor
);
283 ctor
= (*vec
)->last().value
;
284 vec
= &CONSTRUCTOR_ELTS (ctor
);
287 /* Now we're at the innermost field, the one that isn't an anonymous
288 aggregate. Add its initializer to the CONSTRUCTOR and we're done. */
289 gcc_assert (fields
.is_empty());
290 CONSTRUCTOR_APPEND_ELT (*vec
, field
, init
);
295 /* Subroutine of build_constexpr_constructor_member_initializers.
296 The expression tree T represents a data member initialization
297 in a (constexpr) constructor definition. Build a pairing of
298 the data member with its initializer, and prepend that pair
299 to the existing initialization pair INITS. */
302 build_data_member_initialization (tree t
, vec
<constructor_elt
, va_gc
> **vec
)
305 if (TREE_CODE (t
) == CLEANUP_POINT_EXPR
)
306 t
= TREE_OPERAND (t
, 0);
307 if (TREE_CODE (t
) == EXPR_STMT
)
308 t
= TREE_OPERAND (t
, 0);
309 if (t
== error_mark_node
)
311 if (TREE_CODE (t
) == STATEMENT_LIST
)
313 tree_stmt_iterator i
;
314 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
316 if (! build_data_member_initialization (tsi_stmt (i
), vec
))
321 if (TREE_CODE (t
) == CLEANUP_STMT
)
323 /* We can't see a CLEANUP_STMT in a constructor for a literal class,
324 but we can in a constexpr constructor for a non-literal class. Just
325 ignore it; either all the initialization will be constant, in which
326 case the cleanup can't run, or it can't be constexpr.
327 Still recurse into CLEANUP_BODY. */
328 return build_data_member_initialization (CLEANUP_BODY (t
), vec
);
330 if (TREE_CODE (t
) == CONVERT_EXPR
)
331 t
= TREE_OPERAND (t
, 0);
332 if (TREE_CODE (t
) == INIT_EXPR
333 /* vptr initialization shows up as a MODIFY_EXPR. In C++14 we only
334 use what this function builds for cx_check_missing_mem_inits, and
335 assignment in the ctor body doesn't count. */
336 || (cxx_dialect
< cxx14
&& TREE_CODE (t
) == MODIFY_EXPR
))
338 member
= TREE_OPERAND (t
, 0);
339 init
= break_out_target_exprs (TREE_OPERAND (t
, 1));
341 else if (TREE_CODE (t
) == CALL_EXPR
)
343 tree fn
= get_callee_fndecl (t
);
344 if (!fn
|| !DECL_CONSTRUCTOR_P (fn
))
345 /* We're only interested in calls to subobject constructors. */
347 member
= CALL_EXPR_ARG (t
, 0);
348 /* We don't use build_cplus_new here because it complains about
349 abstract bases. Leaving the call unwrapped means that it has the
350 wrong type, but cxx_eval_constant_expression doesn't care. */
351 init
= break_out_target_exprs (t
);
353 else if (TREE_CODE (t
) == BIND_EXPR
)
354 return build_data_member_initialization (BIND_EXPR_BODY (t
), vec
);
356 /* Don't add anything else to the CONSTRUCTOR. */
358 if (INDIRECT_REF_P (member
))
359 member
= TREE_OPERAND (member
, 0);
360 if (TREE_CODE (member
) == NOP_EXPR
)
364 if (TREE_CODE (op
) == ADDR_EXPR
)
366 gcc_assert (same_type_ignoring_top_level_qualifiers_p
367 (TREE_TYPE (TREE_TYPE (op
)),
368 TREE_TYPE (TREE_TYPE (member
))));
369 /* Initializing a cv-qualified member; we need to look through
373 else if (op
== current_class_ptr
374 && (same_type_ignoring_top_level_qualifiers_p
375 (TREE_TYPE (TREE_TYPE (member
)),
376 current_class_type
)))
377 /* Delegating constructor. */
381 /* This is an initializer for an empty base; keep it for now so
382 we can check it in cxx_eval_bare_aggregate. */
383 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member
))));
386 if (TREE_CODE (member
) == ADDR_EXPR
)
387 member
= TREE_OPERAND (member
, 0);
388 if (TREE_CODE (member
) == COMPONENT_REF
)
390 tree aggr
= TREE_OPERAND (member
, 0);
391 if (TREE_CODE (aggr
) == VAR_DECL
)
392 /* Initializing a local variable, don't add anything. */
394 if (TREE_CODE (aggr
) != COMPONENT_REF
)
395 /* Normal member initialization. */
396 member
= TREE_OPERAND (member
, 1);
397 else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr
)))
398 /* Initializing a member of an anonymous union. */
399 return build_anon_member_initialization (member
, init
, vec
);
401 /* We're initializing a vtable pointer in a base. Leave it as
402 COMPONENT_REF so we remember the path to get to the vfield. */
403 gcc_assert (TREE_TYPE (member
) == vtbl_ptr_type_node
);
406 /* Value-initialization can produce multiple initializers for the
407 same field; use the last one. */
408 if (!vec_safe_is_empty (*vec
) && (*vec
)->last().index
== member
)
409 (*vec
)->last().value
= init
;
411 CONSTRUCTOR_APPEND_ELT (*vec
, member
, init
);
415 /* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval.
416 In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a
417 BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */
420 check_constexpr_bind_expr_vars (tree t
)
422 gcc_assert (TREE_CODE (t
) == BIND_EXPR
);
424 for (tree var
= BIND_EXPR_VARS (t
); var
; var
= DECL_CHAIN (var
))
425 if (TREE_CODE (var
) == TYPE_DECL
426 && DECL_IMPLICIT_TYPEDEF_P (var
)
427 && !LAMBDA_TYPE_P (TREE_TYPE (var
)))
432 /* Subroutine of check_constexpr_ctor_body. */
435 check_constexpr_ctor_body_1 (tree last
, tree list
)
437 switch (TREE_CODE (list
))
440 if (TREE_CODE (DECL_EXPR_DECL (list
)) == USING_DECL
441 || TREE_CODE (DECL_EXPR_DECL (list
)) == TYPE_DECL
)
445 case CLEANUP_POINT_EXPR
:
446 return check_constexpr_ctor_body (last
, TREE_OPERAND (list
, 0),
450 if (!check_constexpr_bind_expr_vars (list
)
451 || !check_constexpr_ctor_body (last
, BIND_EXPR_BODY (list
),
465 /* Make sure that there are no statements after LAST in the constructor
466 body represented by LIST. */
469 check_constexpr_ctor_body (tree last
, tree list
, bool complain
)
471 /* C++14 doesn't require a constexpr ctor to have an empty body. */
472 if (cxx_dialect
>= cxx14
)
476 if (TREE_CODE (list
) == STATEMENT_LIST
)
478 tree_stmt_iterator i
= tsi_last (list
);
479 for (; !tsi_end_p (i
); tsi_prev (&i
))
481 tree t
= tsi_stmt (i
);
484 if (!check_constexpr_ctor_body_1 (last
, t
))
491 else if (list
!= last
492 && !check_constexpr_ctor_body_1 (last
, list
))
497 error ("%<constexpr%> constructor does not have empty body");
498 DECL_DECLARED_CONSTEXPR_P (current_function_decl
) = false;
503 /* V is a vector of constructor elements built up for the base and member
504 initializers of a constructor for TYPE. They need to be in increasing
505 offset order, which they might not be yet if TYPE has a primary base
506 which is not first in the base-clause or a vptr and at least one base
507 all of which are non-primary. */
509 static vec
<constructor_elt
, va_gc
> *
510 sort_constexpr_mem_initializers (tree type
, vec
<constructor_elt
, va_gc
> *v
)
512 tree pri
= CLASSTYPE_PRIMARY_BINFO (type
);
518 field_type
= BINFO_TYPE (pri
);
519 else if (TYPE_CONTAINS_VPTR_P (type
))
520 field_type
= vtbl_ptr_type_node
;
524 /* Find the element for the primary base or vptr and move it to the
525 beginning of the vec. */
526 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
527 if (TREE_TYPE (ce
->index
) == field_type
)
530 if (i
> 0 && i
< vec_safe_length (v
))
532 vec
<constructor_elt
, va_gc
> &vref
= *v
;
533 constructor_elt elt
= vref
[i
];
542 /* Build compile-time evalable representations of member-initializer list
543 for a constexpr constructor. */
546 build_constexpr_constructor_member_initializers (tree type
, tree body
)
548 vec
<constructor_elt
, va_gc
> *vec
= NULL
;
551 switch (TREE_CODE (body
))
553 case MUST_NOT_THROW_EXPR
:
555 body
= TREE_OPERAND (body
, 0);
559 for (tree_stmt_iterator i
= tsi_start (body
);
560 !tsi_end_p (i
); tsi_next (&i
))
563 if (TREE_CODE (body
) == BIND_EXPR
)
569 body
= BIND_EXPR_BODY (body
);
576 if (TREE_CODE (body
) == CLEANUP_POINT_EXPR
)
578 body
= TREE_OPERAND (body
, 0);
579 if (TREE_CODE (body
) == EXPR_STMT
)
580 body
= TREE_OPERAND (body
, 0);
581 if (TREE_CODE (body
) == INIT_EXPR
582 && (same_type_ignoring_top_level_qualifiers_p
583 (TREE_TYPE (TREE_OPERAND (body
, 0)),
584 current_class_type
)))
587 return TREE_OPERAND (body
, 1);
589 ok
= build_data_member_initialization (body
, &vec
);
591 else if (TREE_CODE (body
) == STATEMENT_LIST
)
593 tree_stmt_iterator i
;
594 for (i
= tsi_start (body
); !tsi_end_p (i
); tsi_next (&i
))
596 ok
= build_data_member_initialization (tsi_stmt (i
), &vec
);
601 else if (TREE_CODE (body
) == TRY_BLOCK
)
603 error ("body of %<constexpr%> constructor cannot be "
604 "a function-try-block");
605 return error_mark_node
;
607 else if (EXPR_P (body
))
608 ok
= build_data_member_initialization (body
, &vec
);
610 gcc_assert (errorcount
> 0);
613 if (vec_safe_length (vec
) > 0)
615 /* In a delegating constructor, return the target. */
616 constructor_elt
*ce
= &(*vec
)[0];
617 if (ce
->index
== current_class_ptr
)
624 vec
= sort_constexpr_mem_initializers (type
, vec
);
625 return build_constructor (type
, vec
);
628 return error_mark_node
;
631 /* We have an expression tree T that represents a call, either CALL_EXPR
632 or AGGR_INIT_EXPR. If the call is lexically to a named function,
633 retrun the _DECL for that function. */
636 get_function_named_in_call (tree t
)
638 tree fun
= cp_get_callee (t
);
639 if (fun
&& TREE_CODE (fun
) == ADDR_EXPR
640 && TREE_CODE (TREE_OPERAND (fun
, 0)) == FUNCTION_DECL
)
641 fun
= TREE_OPERAND (fun
, 0);
645 /* Subroutine of register_constexpr_fundef. BODY is the body of a function
646 declared to be constexpr, or a sub-statement thereof. Returns the
647 return value if suitable, error_mark_node for a statement not allowed in
648 a constexpr function, or NULL_TREE if no return value was found. */
651 constexpr_fn_retval (tree body
)
653 switch (TREE_CODE (body
))
657 tree_stmt_iterator i
;
658 tree expr
= NULL_TREE
;
659 for (i
= tsi_start (body
); !tsi_end_p (i
); tsi_next (&i
))
661 tree s
= constexpr_fn_retval (tsi_stmt (i
));
662 if (s
== error_mark_node
)
663 return error_mark_node
;
664 else if (s
== NULL_TREE
)
665 /* Keep iterating. */;
667 /* Multiple return statements. */
668 return error_mark_node
;
676 return break_out_target_exprs (TREE_OPERAND (body
, 0));
680 tree decl
= DECL_EXPR_DECL (body
);
681 if (TREE_CODE (decl
) == USING_DECL
682 /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */
683 || DECL_ARTIFICIAL (decl
))
685 return error_mark_node
;
688 case CLEANUP_POINT_EXPR
:
689 return constexpr_fn_retval (TREE_OPERAND (body
, 0));
692 if (!check_constexpr_bind_expr_vars (body
))
693 return error_mark_node
;
694 return constexpr_fn_retval (BIND_EXPR_BODY (body
));
701 tree fun
= get_function_named_in_call (body
);
703 && DECL_FUNCTION_CODE (fun
) == BUILT_IN_UNREACHABLE
)
709 return error_mark_node
;
713 /* Subroutine of register_constexpr_fundef. BODY is the DECL_SAVED_TREE of
714 FUN; do the necessary transformations to turn it into a single expression
715 that we can store in the hash table. */
718 massage_constexpr_body (tree fun
, tree body
)
720 if (DECL_CONSTRUCTOR_P (fun
))
721 body
= build_constexpr_constructor_member_initializers
722 (DECL_CONTEXT (fun
), body
);
723 else if (cxx_dialect
< cxx14
)
725 if (TREE_CODE (body
) == EH_SPEC_BLOCK
)
726 body
= EH_SPEC_STMTS (body
);
727 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
728 body
= TREE_OPERAND (body
, 0);
729 body
= constexpr_fn_retval (body
);
734 /* CTYPE is a type constructed from BODY. Return true if some
735 bases/fields are uninitialized, and complain if COMPLAIN. */
738 cx_check_missing_mem_inits (tree ctype
, tree body
, bool complain
)
744 if (TREE_CODE (body
) != CONSTRUCTOR
)
746 nelts
= CONSTRUCTOR_NELTS (body
);
748 tree field
= TYPE_FIELDS (ctype
);
750 if (TREE_CODE (ctype
) == UNION_TYPE
)
752 if (nelts
== 0 && next_initializable_field (field
))
755 error ("%<constexpr%> constructor for union %qT must "
756 "initialize exactly one non-static data member", ctype
);
762 /* Iterate over the CONSTRUCTOR, checking any missing fields don't
763 need an explicit initialization. */
765 for (unsigned i
= 0; i
<= nelts
; ++i
)
767 tree index
= NULL_TREE
;
770 index
= CONSTRUCTOR_ELT (body
, i
)->index
;
771 /* Skip base and vtable inits. */
772 if (TREE_CODE (index
) != FIELD_DECL
773 || DECL_ARTIFICIAL (index
))
777 for (; field
!= index
; field
= DECL_CHAIN (field
))
780 if (TREE_CODE (field
) != FIELD_DECL
)
782 if (DECL_C_BIT_FIELD (field
) && !DECL_NAME (field
))
784 if (DECL_ARTIFICIAL (field
))
786 if (ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
788 /* Recurse to check the anonummous aggregate member. */
789 bad
|= cx_check_missing_mem_inits
790 (TREE_TYPE (field
), NULL_TREE
, complain
);
791 if (bad
&& !complain
)
795 ftype
= strip_array_types (TREE_TYPE (field
));
796 if (type_has_constexpr_default_constructor (ftype
))
798 /* It's OK to skip a member with a trivial constexpr ctor.
799 A constexpr ctor that isn't trivial should have been
801 gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype
)
807 error ("member %qD must be initialized by mem-initializer "
808 "in %<constexpr%> constructor", field
);
809 inform (DECL_SOURCE_LOCATION (field
), "declared here");
812 if (field
== NULL_TREE
)
815 if (ANON_AGGR_TYPE_P (TREE_TYPE (index
)))
817 /* Check the anonymous aggregate initializer is valid. */
818 bad
|= cx_check_missing_mem_inits
819 (TREE_TYPE (index
), CONSTRUCTOR_ELT (body
, i
)->value
, complain
);
820 if (bad
&& !complain
)
823 field
= DECL_CHAIN (field
);
829 /* We are processing the definition of the constexpr function FUN.
830 Check that its BODY fulfills the propriate requirements and
831 enter it in the constexpr function definition table.
832 For constructor BODY is actually the TREE_LIST of the
833 member-initializer list. */
836 register_constexpr_fundef (tree fun
, tree body
)
838 constexpr_fundef entry
;
839 constexpr_fundef
**slot
;
841 if (!is_valid_constexpr_fn (fun
, !DECL_GENERATED_P (fun
)))
844 tree massaged
= massage_constexpr_body (fun
, body
);
845 if (massaged
== NULL_TREE
|| massaged
== error_mark_node
)
847 if (!DECL_CONSTRUCTOR_P (fun
))
848 error ("body of %<constexpr%> function %qD not a return-statement",
853 if (!potential_rvalue_constant_expression (massaged
))
855 if (!DECL_GENERATED_P (fun
))
856 require_potential_rvalue_constant_expression (massaged
);
860 if (DECL_CONSTRUCTOR_P (fun
)
861 && cx_check_missing_mem_inits (DECL_CONTEXT (fun
),
862 massaged
, !DECL_GENERATED_P (fun
)))
865 /* Create the constexpr function table if necessary. */
866 if (constexpr_fundef_table
== NULL
)
867 constexpr_fundef_table
868 = hash_table
<constexpr_fundef_hasher
>::create_ggc (101);
872 slot
= constexpr_fundef_table
->find_slot (&entry
, INSERT
);
874 gcc_assert (*slot
== NULL
);
875 *slot
= ggc_alloc
<constexpr_fundef
> ();
881 /* FUN is a non-constexpr function called in a context that requires a
882 constant expression. If it comes from a constexpr template, explain why
883 the instantiation isn't constexpr. */
886 explain_invalid_constexpr_fn (tree fun
)
888 static hash_set
<tree
> *diagnosed
;
891 /* Only diagnose defaulted functions, lambdas, or instantiations. */
892 if (!DECL_DEFAULTED_FN (fun
)
893 && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun
))
894 && !is_instantiation_of_constexpr (fun
))
896 if (diagnosed
== NULL
)
897 diagnosed
= new hash_set
<tree
>;
898 if (diagnosed
->add (fun
))
899 /* Already explained. */
902 save_loc
= input_location
;
903 if (!lambda_static_thunk_p (fun
))
905 /* Diagnostics should completely ignore the static thunk, so leave
906 input_location set to our caller's location. */
907 input_location
= DECL_SOURCE_LOCATION (fun
);
908 inform (input_location
,
909 "%qD is not usable as a %<constexpr%> function because:", fun
);
911 /* First check the declaration. */
912 if (is_valid_constexpr_fn (fun
, true))
914 /* Then if it's OK, the body. */
915 if (!DECL_DECLARED_CONSTEXPR_P (fun
)
916 && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun
)))
917 explain_implicit_non_constexpr (fun
);
920 body
= massage_constexpr_body (fun
, DECL_SAVED_TREE (fun
));
921 require_potential_rvalue_constant_expression (body
);
922 if (DECL_CONSTRUCTOR_P (fun
))
923 cx_check_missing_mem_inits (DECL_CONTEXT (fun
), body
, true);
926 input_location
= save_loc
;
929 /* Objects of this type represent calls to constexpr functions
930 along with the bindings of parameters to their arguments, for
931 the purpose of compile time evaluation. */
933 struct GTY((for_user
)) constexpr_call
{
934 /* Description of the constexpr function definition. */
935 constexpr_fundef
*fundef
;
936 /* Parameter bindings environment. A TREE_LIST where each TREE_PURPOSE
937 is a parameter _DECL and the TREE_VALUE is the value of the parameter.
938 Note: This arrangement is made to accommodate the use of
939 iterative_hash_template_arg (see pt.c). If you change this
940 representation, also change the hash calculation in
941 cxx_eval_call_expression. */
943 /* Result of the call.
944 NULL means the call is being evaluated.
945 error_mark_node means that the evaluation was erroneous;
946 otherwise, the actuall value of the call. */
948 /* The hash of this call; we remember it here to avoid having to
949 recalculate it when expanding the hash table. */
953 struct constexpr_call_hasher
: ggc_ptr_hash
<constexpr_call
>
955 static hashval_t
hash (constexpr_call
*);
956 static bool equal (constexpr_call
*, constexpr_call
*);
959 enum constexpr_switch_state
{
960 /* Used when processing a switch for the first time by cxx_eval_switch_expr
961 and default: label for that switch has not been seen yet. */
962 css_default_not_seen
,
963 /* Used when processing a switch for the first time by cxx_eval_switch_expr
964 and default: label for that switch has been seen already. */
966 /* Used when processing a switch for the second time by
967 cxx_eval_switch_expr, where default: label should match. */
968 css_default_processing
971 /* The constexpr expansion context. CALL is the current function
972 expansion, CTOR is the current aggregate initializer, OBJECT is the
973 object being initialized by CTOR, either a VAR_DECL or a _REF. VALUES
974 is a map of values of variables initialized within the expression. */
976 struct constexpr_ctx
{
977 /* The innermost call we're evaluating. */
978 constexpr_call
*call
;
979 /* Values for any temporaries or local variables within the
980 constant-expression. */
981 hash_map
<tree
,tree
> *values
;
982 /* SAVE_EXPRs that we've seen within the current LOOP_EXPR. NULL if we
983 aren't inside a loop. */
984 hash_set
<tree
> *save_exprs
;
985 /* The CONSTRUCTOR we're currently building up for an aggregate
988 /* The object we're building the CONSTRUCTOR for. */
990 /* If inside SWITCH_EXPR. */
991 constexpr_switch_state
*css_state
;
992 /* Whether we should error on a non-constant expression or fail quietly. */
994 /* Whether we are strictly conforming to constant expression rules or
995 trying harder to get a constant value. */
999 /* A table of all constexpr calls that have been evaluated by the
1000 compiler in this translation unit. */
1002 static GTY (()) hash_table
<constexpr_call_hasher
> *constexpr_call_table
;
1004 static tree
cxx_eval_constant_expression (const constexpr_ctx
*, tree
,
1005 bool, bool *, bool *, tree
* = NULL
);
1007 /* Compute a hash value for a constexpr call representation. */
1010 constexpr_call_hasher::hash (constexpr_call
*info
)
1015 /* Return true if the objects pointed to by P and Q represent calls
1016 to the same constexpr function with the same arguments.
1017 Otherwise, return false. */
1020 constexpr_call_hasher::equal (constexpr_call
*lhs
, constexpr_call
*rhs
)
1026 if (!constexpr_fundef_hasher::equal (lhs
->fundef
, rhs
->fundef
))
1028 lhs_bindings
= lhs
->bindings
;
1029 rhs_bindings
= rhs
->bindings
;
1030 while (lhs_bindings
!= NULL
&& rhs_bindings
!= NULL
)
1032 tree lhs_arg
= TREE_VALUE (lhs_bindings
);
1033 tree rhs_arg
= TREE_VALUE (rhs_bindings
);
1034 gcc_assert (TREE_TYPE (lhs_arg
) == TREE_TYPE (rhs_arg
));
1035 if (!cp_tree_equal (lhs_arg
, rhs_arg
))
1037 lhs_bindings
= TREE_CHAIN (lhs_bindings
);
1038 rhs_bindings
= TREE_CHAIN (rhs_bindings
);
1040 return lhs_bindings
== rhs_bindings
;
1043 /* Initialize the constexpr call table, if needed. */
1046 maybe_initialize_constexpr_call_table (void)
1048 if (constexpr_call_table
== NULL
)
1049 constexpr_call_table
= hash_table
<constexpr_call_hasher
>::create_ggc (101);
1052 /* During constexpr CALL_EXPR evaluation, to avoid issues with sharing when
1053 a function happens to get called recursively, we unshare the callee
1054 function's body and evaluate this unshared copy instead of evaluating the
1057 FUNDEF_COPIES_TABLE is a per-function freelist of these unshared function
1058 copies. The underlying data structure of FUNDEF_COPIES_TABLE is a hash_map
1059 that's keyed off of the original FUNCTION_DECL and whose value is a
1060 TREE_LIST of this function's unused copies awaiting reuse.
1062 This is not GC-deletable to avoid GC affecting UID generation. */
1064 static GTY(()) hash_map
<tree
, tree
> *fundef_copies_table
;
1066 /* Initialize FUNDEF_COPIES_TABLE if it's not initialized. */
1069 maybe_initialize_fundef_copies_table ()
1071 if (fundef_copies_table
== NULL
)
1072 fundef_copies_table
= hash_map
<tree
,tree
>::create_ggc (101);
1075 /* Reuse a copy or create a new unshared copy of the function FUN.
1076 Return this copy. We use a TREE_LIST whose PURPOSE is body, VALUE
1077 is parms, TYPE is result. */
1080 get_fundef_copy (tree fun
)
1082 maybe_initialize_fundef_copies_table ();
1086 tree
*slot
= &fundef_copies_table
->get_or_insert (fun
, &existed
);
1090 /* There is no cached function available, or in use. We can use
1091 the function directly. That the slot is now created records
1092 that this function is now in use. */
1093 copy
= build_tree_list (DECL_SAVED_TREE (fun
), DECL_ARGUMENTS (fun
));
1094 TREE_TYPE (copy
) = DECL_RESULT (fun
);
1096 else if (*slot
== NULL_TREE
)
1098 /* We've already used the function itself, so make a copy. */
1099 copy
= build_tree_list (NULL
, NULL
);
1100 TREE_PURPOSE (copy
) = copy_fn (fun
, TREE_VALUE (copy
), TREE_TYPE (copy
));
1104 /* We have a cached function available. */
1106 *slot
= TREE_CHAIN (copy
);
1112 /* Save the copy COPY of function FUN for later reuse by
1113 get_fundef_copy(). By construction, there will always be an entry
1117 save_fundef_copy (tree fun
, tree copy
)
1119 tree
*slot
= fundef_copies_table
->get (fun
);
1120 TREE_CHAIN (copy
) = *slot
;
1124 /* We have an expression tree T that represents a call, either CALL_EXPR
1125 or AGGR_INIT_EXPR. Return the Nth argument. */
1128 get_nth_callarg (tree t
, int n
)
1130 switch (TREE_CODE (t
))
1133 return CALL_EXPR_ARG (t
, n
);
1135 case AGGR_INIT_EXPR
:
1136 return AGGR_INIT_EXPR_ARG (t
, n
);
1144 /* Attempt to evaluate T which represents a call to a builtin function.
1145 We assume here that all builtin functions evaluate to scalar types
1146 represented by _CST nodes. */
1149 cxx_eval_builtin_function_call (const constexpr_ctx
*ctx
, tree t
, tree fun
,
1151 bool *non_constant_p
, bool *overflow_p
)
1153 const int nargs
= call_expr_nargs (t
);
1154 tree
*args
= (tree
*) alloca (nargs
* sizeof (tree
));
1158 /* Don't fold __builtin_constant_p within a constexpr function. */
1159 bool bi_const_p
= (DECL_FUNCTION_CODE (fun
) == BUILT_IN_CONSTANT_P
);
1162 && current_function_decl
1163 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
1165 *non_constant_p
= true;
1169 /* Be permissive for arguments to built-ins; __builtin_constant_p should
1170 return constant false for a non-constant argument. */
1171 constexpr_ctx new_ctx
= *ctx
;
1172 new_ctx
.quiet
= true;
1173 bool dummy1
= false, dummy2
= false;
1174 for (i
= 0; i
< nargs
; ++i
)
1176 args
[i
] = cxx_eval_constant_expression (&new_ctx
, CALL_EXPR_ARG (t
, i
),
1177 false, &dummy1
, &dummy2
);
1179 /* For __built_in_constant_p, fold all expressions with constant values
1180 even if they aren't C++ constant-expressions. */
1181 args
[i
] = cp_fully_fold (args
[i
]);
1184 bool save_ffbcp
= force_folding_builtin_constant_p
;
1185 force_folding_builtin_constant_p
= true;
1186 new_call
= fold_builtin_call_array (EXPR_LOCATION (t
), TREE_TYPE (t
),
1187 CALL_EXPR_FN (t
), nargs
, args
);
1188 force_folding_builtin_constant_p
= save_ffbcp
;
1189 if (new_call
== NULL
)
1191 if (!*non_constant_p
&& !ctx
->quiet
)
1193 /* Do not allow__builtin_unreachable in constexpr function.
1194 The __builtin_unreachable call with BUILTINS_LOCATION
1195 comes from cp_maybe_instrument_return. */
1196 if (DECL_FUNCTION_CODE (fun
) == BUILT_IN_UNREACHABLE
1197 && EXPR_LOCATION (t
) == BUILTINS_LOCATION
)
1198 error ("%<constexpr%> call flows off the end of the function");
1201 new_call
= build_call_array_loc (EXPR_LOCATION (t
), TREE_TYPE (t
),
1202 CALL_EXPR_FN (t
), nargs
, args
);
1203 error ("%q+E is not a constant expression", new_call
);
1206 *non_constant_p
= true;
1210 if (!is_constant_expression (new_call
))
1212 if (!*non_constant_p
&& !ctx
->quiet
)
1213 error ("%q+E is not a constant expression", new_call
);
1214 *non_constant_p
= true;
1218 return cxx_eval_constant_expression (&new_ctx
, new_call
, lval
,
1219 non_constant_p
, overflow_p
);
1222 /* TEMP is the constant value of a temporary object of type TYPE. Adjust
1223 the type of the value to match. */
1226 adjust_temp_type (tree type
, tree temp
)
1228 if (TREE_TYPE (temp
) == type
)
1230 /* Avoid wrapping an aggregate value in a NOP_EXPR. */
1231 if (TREE_CODE (temp
) == CONSTRUCTOR
)
1232 return build_constructor (type
, CONSTRUCTOR_ELTS (temp
));
1233 gcc_assert (scalarish_type_p (type
));
1234 return cp_fold_convert (type
, temp
);
1237 /* Callback for walk_tree used by unshare_constructor. */
1240 find_constructor (tree
*tp
, int *walk_subtrees
, void *)
1244 if (TREE_CODE (*tp
) == CONSTRUCTOR
)
1249 /* If T is a CONSTRUCTOR or an expression that has a CONSTRUCTOR node as a
1250 subexpression, return an unshared copy of T. Otherwise return T. */
1253 unshare_constructor (tree t
)
1255 tree ctor
= walk_tree (&t
, find_constructor
, NULL
, NULL
);
1256 if (ctor
!= NULL_TREE
)
1257 return unshare_expr (t
);
1261 /* Subroutine of cxx_eval_call_expression.
1262 We are processing a call expression (either CALL_EXPR or
1263 AGGR_INIT_EXPR) in the context of CTX. Evaluate
1264 all arguments and bind their values to correspondings
1265 parameters, making up the NEW_CALL context. */
1268 cxx_bind_parameters_in_call (const constexpr_ctx
*ctx
, tree t
,
1269 constexpr_call
*new_call
,
1270 bool *non_constant_p
, bool *overflow_p
,
1271 bool *non_constant_args
)
1273 const int nargs
= call_expr_nargs (t
);
1274 tree fun
= new_call
->fundef
->decl
;
1275 tree parms
= DECL_ARGUMENTS (fun
);
1277 tree
*p
= &new_call
->bindings
;
1278 for (i
= 0; i
< nargs
; ++i
)
1281 tree type
= parms
? TREE_TYPE (parms
) : void_type_node
;
1282 x
= get_nth_callarg (t
, i
);
1283 /* For member function, the first argument is a pointer to the implied
1284 object. For a constructor, it might still be a dummy object, in
1285 which case we get the real argument from ctx. */
1286 if (i
== 0 && DECL_CONSTRUCTOR_P (fun
)
1287 && is_dummy_object (x
))
1290 x
= build_address (x
);
1293 arg
= cxx_eval_constant_expression (ctx
, x
, lval
,
1294 non_constant_p
, overflow_p
);
1295 /* Don't VERIFY_CONSTANT here. */
1296 if (*non_constant_p
&& ctx
->quiet
)
1298 /* Just discard ellipsis args after checking their constantitude. */
1302 if (!*non_constant_p
)
1304 /* Make sure the binding has the same type as the parm. But
1305 only for constant args. */
1306 if (TREE_CODE (type
) != REFERENCE_TYPE
)
1307 arg
= adjust_temp_type (type
, arg
);
1308 if (!TREE_CONSTANT (arg
))
1309 *non_constant_args
= true;
1310 *p
= build_tree_list (parms
, arg
);
1311 p
= &TREE_CHAIN (*p
);
1313 parms
= TREE_CHAIN (parms
);
1317 /* Variables and functions to manage constexpr call expansion context.
1318 These do not need to be marked for PCH or GC. */
1320 /* FIXME remember and print actual constant arguments. */
1321 static vec
<tree
> call_stack
;
1322 static int call_stack_tick
;
1323 static int last_cx_error_tick
;
1326 push_cx_call_context (tree call
)
1329 if (!EXPR_HAS_LOCATION (call
))
1330 SET_EXPR_LOCATION (call
, input_location
);
1331 call_stack
.safe_push (call
);
1332 if (call_stack
.length () > (unsigned) max_constexpr_depth
)
1338 pop_cx_call_context (void)
1345 cx_error_context (void)
1347 vec
<tree
> r
= vNULL
;
1348 if (call_stack_tick
!= last_cx_error_tick
1349 && !call_stack
.is_empty ())
1351 last_cx_error_tick
= call_stack_tick
;
1355 /* Evaluate a call T to a GCC internal function when possible and return
1356 the evaluated result or, under the control of CTX, give an error, set
1357 NON_CONSTANT_P, and return the unevaluated call T otherwise. */
1360 cxx_eval_internal_function (const constexpr_ctx
*ctx
, tree t
,
1362 bool *non_constant_p
, bool *overflow_p
)
1364 enum tree_code opcode
= ERROR_MARK
;
1366 switch (CALL_EXPR_IFN (t
))
1368 case IFN_UBSAN_NULL
:
1369 case IFN_UBSAN_BOUNDS
:
1370 case IFN_UBSAN_VPTR
:
1371 case IFN_FALLTHROUGH
:
1374 case IFN_ADD_OVERFLOW
:
1377 case IFN_SUB_OVERFLOW
:
1378 opcode
= MINUS_EXPR
;
1380 case IFN_MUL_OVERFLOW
:
1385 return cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 0),
1386 false, non_constant_p
, overflow_p
);
1390 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
1391 "call to internal function %qE", t
);
1392 *non_constant_p
= true;
1396 /* Evaluate constant arguments using OPCODE and return a complex
1397 number containing the result and the overflow bit. */
1398 tree arg0
= cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 0), lval
,
1399 non_constant_p
, overflow_p
);
1400 tree arg1
= cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 1), lval
,
1401 non_constant_p
, overflow_p
);
1403 if (TREE_CODE (arg0
) == INTEGER_CST
&& TREE_CODE (arg1
) == INTEGER_CST
)
1405 location_t loc
= EXPR_LOC_OR_LOC (t
, input_location
);
1406 tree type
= TREE_TYPE (TREE_TYPE (t
));
1407 tree result
= fold_binary_loc (loc
, opcode
, type
,
1408 fold_convert_loc (loc
, type
, arg0
),
1409 fold_convert_loc (loc
, type
, arg1
));
1411 = build_int_cst (type
, arith_overflowed_p (opcode
, type
, arg0
, arg1
));
1412 /* Reset TREE_OVERFLOW to avoid warnings for the overflow. */
1413 if (TREE_OVERFLOW (result
))
1414 TREE_OVERFLOW (result
) = 0;
1416 return build_complex (TREE_TYPE (t
), result
, ovf
);
1419 *non_constant_p
= true;
1423 /* Clean CONSTRUCTOR_NO_IMPLICIT_ZERO from CTOR and its sub-aggregates. */
1426 clear_no_implicit_zero (tree ctor
)
1428 if (CONSTRUCTOR_NO_IMPLICIT_ZERO (ctor
))
1430 CONSTRUCTOR_NO_IMPLICIT_ZERO (ctor
) = false;
1431 tree elt
; unsigned HOST_WIDE_INT idx
;
1432 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor
), idx
, elt
)
1433 if (TREE_CODE (elt
) == CONSTRUCTOR
)
1434 clear_no_implicit_zero (elt
);
1438 /* Subroutine of cxx_eval_constant_expression.
1439 Evaluate the call expression tree T in the context of OLD_CALL expression
1443 cxx_eval_call_expression (const constexpr_ctx
*ctx
, tree t
,
1445 bool *non_constant_p
, bool *overflow_p
)
1447 location_t loc
= EXPR_LOC_OR_LOC (t
, input_location
);
1448 tree fun
= get_function_named_in_call (t
);
1449 constexpr_call new_call
= { NULL
, NULL
, NULL
, 0 };
1452 if (fun
== NULL_TREE
)
1453 return cxx_eval_internal_function (ctx
, t
, lval
,
1454 non_constant_p
, overflow_p
);
1456 if (TREE_CODE (fun
) != FUNCTION_DECL
)
1458 /* Might be a constexpr function pointer. */
1459 fun
= cxx_eval_constant_expression (ctx
, fun
,
1460 /*lval*/false, non_constant_p
,
1463 if (TREE_CODE (fun
) == ADDR_EXPR
)
1464 fun
= TREE_OPERAND (fun
, 0);
1466 if (TREE_CODE (fun
) != FUNCTION_DECL
)
1468 if (!ctx
->quiet
&& !*non_constant_p
)
1469 error_at (loc
, "expression %qE does not designate a %<constexpr%> "
1471 *non_constant_p
= true;
1474 if (DECL_CLONED_FUNCTION_P (fun
))
1475 fun
= DECL_CLONED_FUNCTION (fun
);
1477 if (is_ubsan_builtin_p (fun
))
1480 if (is_builtin_fn (fun
))
1481 return cxx_eval_builtin_function_call (ctx
, t
, fun
,
1482 lval
, non_constant_p
, overflow_p
);
1483 if (!DECL_DECLARED_CONSTEXPR_P (fun
))
1487 if (!lambda_static_thunk_p (fun
))
1488 error_at (loc
, "call to non-%<constexpr%> function %qD", fun
);
1489 explain_invalid_constexpr_fn (fun
);
1491 *non_constant_p
= true;
1495 constexpr_ctx new_ctx
= *ctx
;
1496 if (DECL_CONSTRUCTOR_P (fun
) && !ctx
->object
1497 && TREE_CODE (t
) == AGGR_INIT_EXPR
)
1499 /* We want to have an initialization target for an AGGR_INIT_EXPR.
1500 If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
1501 new_ctx
.object
= AGGR_INIT_EXPR_SLOT (t
);
1502 tree ctor
= new_ctx
.ctor
= build_constructor (DECL_CONTEXT (fun
), NULL
);
1503 CONSTRUCTOR_NO_IMPLICIT_ZERO (ctor
) = true;
1504 ctx
->values
->put (new_ctx
.object
, ctor
);
1508 /* Shortcut trivial constructor/op=. */
1509 if (trivial_fn_p (fun
))
1511 tree init
= NULL_TREE
;
1512 if (call_expr_nargs (t
) == 2)
1513 init
= convert_from_reference (get_nth_callarg (t
, 1));
1514 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
1515 && AGGR_INIT_ZERO_FIRST (t
))
1516 init
= build_zero_init (DECL_CONTEXT (fun
), NULL_TREE
, false);
1519 tree op
= get_nth_callarg (t
, 0);
1520 if (is_dummy_object (op
))
1523 op
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (op
)), op
);
1524 tree set
= build2 (MODIFY_EXPR
, TREE_TYPE (op
), op
, init
);
1525 new_ctx
.call
= &new_call
;
1526 return cxx_eval_constant_expression (&new_ctx
, set
, lval
,
1527 non_constant_p
, overflow_p
);
1531 /* We can't defer instantiating the function any longer. */
1532 if (!DECL_INITIAL (fun
)
1533 && DECL_TEMPLOID_INSTANTIATION (fun
))
1535 location_t save_loc
= input_location
;
1536 input_location
= loc
;
1538 instantiate_decl (fun
, /*defer_ok*/false, /*expl_inst*/false);
1540 input_location
= save_loc
;
1543 /* If in direct recursive call, optimize definition search. */
1544 if (ctx
&& ctx
->call
&& ctx
->call
->fundef
&& ctx
->call
->fundef
->decl
== fun
)
1545 new_call
.fundef
= ctx
->call
->fundef
;
1548 new_call
.fundef
= retrieve_constexpr_fundef (fun
);
1549 if (new_call
.fundef
== NULL
|| new_call
.fundef
->body
== NULL
1550 || fun
== current_function_decl
)
1554 /* We need to check for current_function_decl here in case we're
1555 being called during cp_fold_function, because at that point
1556 DECL_INITIAL is set properly and we have a fundef but we
1557 haven't lowered invisirefs yet (c++/70344). */
1558 if (DECL_INITIAL (fun
) == error_mark_node
1559 || fun
== current_function_decl
)
1560 error_at (loc
, "%qD called in a constant expression before its "
1561 "definition is complete", fun
);
1562 else if (DECL_INITIAL (fun
))
1564 /* The definition of fun was somehow unsuitable. But pretend
1565 that lambda static thunks don't exist. */
1566 if (!lambda_static_thunk_p (fun
))
1567 error_at (loc
, "%qD called in a constant expression", fun
);
1568 explain_invalid_constexpr_fn (fun
);
1571 error_at (loc
, "%qD used before its definition", fun
);
1573 *non_constant_p
= true;
1578 bool non_constant_args
= false;
1579 cxx_bind_parameters_in_call (ctx
, t
, &new_call
,
1580 non_constant_p
, overflow_p
, &non_constant_args
);
1581 if (*non_constant_p
)
1584 depth_ok
= push_cx_call_context (t
);
1586 tree result
= NULL_TREE
;
1588 constexpr_call
*entry
= NULL
;
1589 if (depth_ok
&& !non_constant_args
)
1591 new_call
.hash
= iterative_hash_template_arg
1592 (new_call
.bindings
, constexpr_fundef_hasher::hash (new_call
.fundef
));
1594 /* If we have seen this call before, we are done. */
1595 maybe_initialize_constexpr_call_table ();
1596 constexpr_call
**slot
1597 = constexpr_call_table
->find_slot (&new_call
, INSERT
);
1601 /* We need to keep a pointer to the entry, not just the slot, as the
1602 slot can move in the call to cxx_eval_builtin_function_call. */
1603 *slot
= entry
= ggc_alloc
<constexpr_call
> ();
1606 /* Calls that are in progress have their result set to NULL,
1607 so that we can detect circular dependencies. */
1608 else if (entry
->result
== NULL
)
1611 error ("call has circular dependency");
1612 *non_constant_p
= true;
1613 entry
->result
= result
= error_mark_node
;
1616 result
= entry
->result
;
1622 error ("%<constexpr%> evaluation depth exceeds maximum of %d (use "
1623 "-fconstexpr-depth= to increase the maximum)",
1624 max_constexpr_depth
);
1625 *non_constant_p
= true;
1626 result
= error_mark_node
;
1630 if (result
&& result
!= error_mark_node
)
1632 else if (!DECL_SAVED_TREE (fun
))
1634 /* When at_eof >= 2, cgraph has started throwing away
1635 DECL_SAVED_TREE, so fail quietly. FIXME we get here because of
1636 late code generation for VEC_INIT_EXPR, which needs to be
1637 completely reconsidered. */
1638 gcc_assert (at_eof
>= 2 && ctx
->quiet
);
1639 *non_constant_p
= true;
1643 tree body
, parms
, res
;
1645 /* Reuse or create a new unshared copy of this function's body. */
1646 tree copy
= get_fundef_copy (fun
);
1647 body
= TREE_PURPOSE (copy
);
1648 parms
= TREE_VALUE (copy
);
1649 res
= TREE_TYPE (copy
);
1651 /* Associate the bindings with the remapped parms. */
1652 tree bound
= new_call
.bindings
;
1653 tree remapped
= parms
;
1656 tree oparm
= TREE_PURPOSE (bound
);
1657 tree arg
= TREE_VALUE (bound
);
1658 gcc_assert (DECL_NAME (remapped
) == DECL_NAME (oparm
));
1659 /* Don't share a CONSTRUCTOR that might be changed. */
1660 arg
= unshare_constructor (arg
);
1661 ctx
->values
->put (remapped
, arg
);
1662 bound
= TREE_CHAIN (bound
);
1663 remapped
= DECL_CHAIN (remapped
);
1665 /* Add the RESULT_DECL to the values map, too. */
1666 tree slot
= NULL_TREE
;
1667 if (DECL_BY_REFERENCE (res
))
1669 slot
= AGGR_INIT_EXPR_SLOT (t
);
1670 tree addr
= build_address (slot
);
1671 addr
= build_nop (TREE_TYPE (res
), addr
);
1672 ctx
->values
->put (res
, addr
);
1673 ctx
->values
->put (slot
, NULL_TREE
);
1676 ctx
->values
->put (res
, NULL_TREE
);
1678 /* Track the callee's evaluated SAVE_EXPRs so that we can forget
1679 their values after the call. */
1680 constexpr_ctx ctx_with_save_exprs
= *ctx
;
1681 hash_set
<tree
> save_exprs
;
1682 ctx_with_save_exprs
.save_exprs
= &save_exprs
;
1683 ctx_with_save_exprs
.call
= &new_call
;
1685 tree jump_target
= NULL_TREE
;
1686 cxx_eval_constant_expression (&ctx_with_save_exprs
, body
,
1687 lval
, non_constant_p
, overflow_p
,
1690 if (DECL_CONSTRUCTOR_P (fun
))
1691 /* This can be null for a subobject constructor call, in
1692 which case what we care about is the initialization
1693 side-effects rather than the value. We could get at the
1694 value by evaluating *this, but we don't bother; there's
1695 no need to put such a call in the hash table. */
1696 result
= lval
? ctx
->object
: ctx
->ctor
;
1697 else if (VOID_TYPE_P (TREE_TYPE (res
)))
1701 result
= *ctx
->values
->get (slot
? slot
: res
);
1702 if (result
== NULL_TREE
&& !*non_constant_p
)
1705 error ("%<constexpr%> call flows off the end "
1707 *non_constant_p
= true;
1711 /* Forget the saved values of the callee's SAVE_EXPRs. */
1712 for (hash_set
<tree
>::iterator iter
= save_exprs
.begin();
1713 iter
!= save_exprs
.end(); ++iter
)
1714 ctx_with_save_exprs
.values
->remove (*iter
);
1716 /* Remove the parms/result from the values map. Is it worth
1717 bothering to do this when the map itself is only live for
1718 one constexpr evaluation? If so, maybe also clear out
1719 other vars from call, maybe in BIND_EXPR handling? */
1720 ctx
->values
->remove (res
);
1722 ctx
->values
->remove (slot
);
1723 for (tree parm
= parms
; parm
; parm
= TREE_CHAIN (parm
))
1724 ctx
->values
->remove (parm
);
1726 /* Make the unshared function copy we used available for re-use. */
1727 save_fundef_copy (fun
, copy
);
1730 if (result
== error_mark_node
)
1731 *non_constant_p
= true;
1732 if (*non_constant_p
|| *overflow_p
)
1733 result
= error_mark_node
;
1737 entry
->result
= result
;
1740 /* The result of a constexpr function must be completely initialized. */
1741 if (TREE_CODE (result
) == CONSTRUCTOR
)
1742 clear_no_implicit_zero (result
);
1744 pop_cx_call_context ();
1745 return unshare_constructor (result
);
1748 /* FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
1751 reduced_constant_expression_p (tree t
)
1753 switch (TREE_CODE (t
))
1756 /* Even if we can't lower this yet, it's constant. */
1760 /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
1761 tree idx
, val
, field
; unsigned HOST_WIDE_INT i
;
1762 if (CONSTRUCTOR_NO_IMPLICIT_ZERO (t
))
1763 field
= next_initializable_field (TYPE_FIELDS (TREE_TYPE (t
)));
1766 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t
), i
, idx
, val
)
1769 /* We're in the middle of initializing this element. */
1771 if (!reduced_constant_expression_p (val
))
1777 field
= next_initializable_field (DECL_CHAIN (field
));
1782 else if (CONSTRUCTOR_NO_IMPLICIT_ZERO (t
))
1783 /* All the fields are initialized. */
1784 CONSTRUCTOR_NO_IMPLICIT_ZERO (t
) = false;
1788 /* FIXME are we calling this too much? */
1789 return initializer_constant_valid_p (t
, TREE_TYPE (t
)) != NULL_TREE
;
1793 /* Some expressions may have constant operands but are not constant
1794 themselves, such as 1/0. Call this function (or rather, the macro
1795 following it) to check for that condition.
1797 We only call this in places that require an arithmetic constant, not in
1798 places where we might have a non-constant expression that can be a
1799 component of a constant expression, such as the address of a constexpr
1800 variable that might be dereferenced later. */
1803 verify_constant (tree t
, bool allow_non_constant
, bool *non_constant_p
,
1806 if (!*non_constant_p
&& !reduced_constant_expression_p (t
))
1808 if (!allow_non_constant
)
1809 error ("%q+E is not a constant expression", t
);
1810 *non_constant_p
= true;
1812 if (TREE_OVERFLOW_P (t
))
1814 if (!allow_non_constant
)
1816 permerror (input_location
, "overflow in constant expression");
1817 /* If we're being permissive (and are in an enforcing
1818 context), ignore the overflow. */
1819 if (flag_permissive
)
1820 return *non_constant_p
;
1824 return *non_constant_p
;
1827 /* Check whether the shift operation with code CODE and type TYPE on LHS
1828 and RHS is undefined. If it is, give an error with an explanation,
1829 and return true; return false otherwise. */
1832 cxx_eval_check_shift_p (location_t loc
, const constexpr_ctx
*ctx
,
1833 enum tree_code code
, tree type
, tree lhs
, tree rhs
)
1835 if ((code
!= LSHIFT_EXPR
&& code
!= RSHIFT_EXPR
)
1836 || TREE_CODE (lhs
) != INTEGER_CST
1837 || TREE_CODE (rhs
) != INTEGER_CST
)
1840 tree lhstype
= TREE_TYPE (lhs
);
1841 unsigned HOST_WIDE_INT uprec
= TYPE_PRECISION (TREE_TYPE (lhs
));
1843 /* [expr.shift] The behavior is undefined if the right operand
1844 is negative, or greater than or equal to the length in bits
1845 of the promoted left operand. */
1846 if (tree_int_cst_sgn (rhs
) == -1)
1849 permerror (loc
, "right operand of shift expression %q+E is negative",
1850 build2_loc (loc
, code
, type
, lhs
, rhs
));
1851 return (!flag_permissive
|| ctx
->quiet
);
1853 if (compare_tree_int (rhs
, uprec
) >= 0)
1856 permerror (loc
, "right operand of shift expression %q+E is >= than "
1857 "the precision of the left operand",
1858 build2_loc (loc
, code
, type
, lhs
, rhs
));
1859 return (!flag_permissive
|| ctx
->quiet
);
1862 /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
1863 if E1 has a signed type and non-negative value, and E1x2^E2 is
1864 representable in the corresponding unsigned type of the result type,
1865 then that value, converted to the result type, is the resulting value;
1866 otherwise, the behavior is undefined. */
1867 if (code
== LSHIFT_EXPR
&& !TYPE_UNSIGNED (lhstype
)
1868 && (cxx_dialect
>= cxx11
))
1870 if (tree_int_cst_sgn (lhs
) == -1)
1874 "left operand of shift expression %q+E is negative",
1875 build2_loc (loc
, code
, type
, lhs
, rhs
));
1876 return (!flag_permissive
|| ctx
->quiet
);
1878 /* For signed x << y the following:
1879 (unsigned) x >> ((prec (lhs) - 1) - y)
1880 if > 1, is undefined. The right-hand side of this formula
1881 is the highest bit of the LHS that can be set (starting from 0),
1882 so that the shift doesn't overflow. We then right-shift the LHS
1883 to see whether any other bit is set making the original shift
1884 undefined -- the result is not representable in the corresponding
1886 tree t
= build_int_cst (unsigned_type_node
, uprec
- 1);
1887 t
= fold_build2 (MINUS_EXPR
, unsigned_type_node
, t
, rhs
);
1888 tree ulhs
= fold_convert (unsigned_type_for (lhstype
), lhs
);
1889 t
= fold_build2 (RSHIFT_EXPR
, TREE_TYPE (ulhs
), ulhs
, t
);
1890 if (tree_int_cst_lt (integer_one_node
, t
))
1893 permerror (loc
, "shift expression %q+E overflows",
1894 build2_loc (loc
, code
, type
, lhs
, rhs
));
1895 return (!flag_permissive
|| ctx
->quiet
);
1901 /* Subroutine of cxx_eval_constant_expression.
1902 Attempt to reduce the unary expression tree T to a compile time value.
1903 If successful, return the value. Otherwise issue a diagnostic
1904 and return error_mark_node. */
1907 cxx_eval_unary_expression (const constexpr_ctx
*ctx
, tree t
,
1909 bool *non_constant_p
, bool *overflow_p
)
1912 tree orig_arg
= TREE_OPERAND (t
, 0);
1913 tree arg
= cxx_eval_constant_expression (ctx
, orig_arg
, /*lval*/false,
1914 non_constant_p
, overflow_p
);
1915 VERIFY_CONSTANT (arg
);
1916 location_t loc
= EXPR_LOCATION (t
);
1917 enum tree_code code
= TREE_CODE (t
);
1918 tree type
= TREE_TYPE (t
);
1919 r
= fold_unary_loc (loc
, code
, type
, arg
);
1922 if (arg
== orig_arg
)
1925 r
= build1_loc (loc
, code
, type
, arg
);
1927 VERIFY_CONSTANT (r
);
1931 /* Helper function for cxx_eval_binary_expression. Try to optimize
1932 original POINTER_PLUS_EXPR T, LHS p+ RHS, return NULL_TREE if the
1933 generic folding should be used. */
1936 cxx_fold_pointer_plus_expression (const constexpr_ctx
*ctx
, tree t
,
1937 tree lhs
, tree rhs
, bool *non_constant_p
,
1941 if (TREE_CODE (lhs
) != ADDR_EXPR
)
1944 lhs
= TREE_OPERAND (lhs
, 0);
1946 /* &A[i] p+ j => &A[i + j] */
1947 if (TREE_CODE (lhs
) == ARRAY_REF
1948 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == INTEGER_CST
1949 && TREE_CODE (rhs
) == INTEGER_CST
1950 && TYPE_SIZE_UNIT (TREE_TYPE (lhs
))
1951 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs
))) == INTEGER_CST
)
1953 tree orig_type
= TREE_TYPE (t
);
1954 location_t loc
= EXPR_LOCATION (t
);
1955 tree type
= TREE_TYPE (lhs
);
1957 t
= fold_convert_loc (loc
, ssizetype
, TREE_OPERAND (lhs
, 1));
1958 tree nelts
= array_type_nelts_top (TREE_TYPE (TREE_OPERAND (lhs
, 0)));
1959 nelts
= cxx_eval_constant_expression (ctx
, nelts
, false, non_constant_p
,
1961 if (*non_constant_p
)
1963 /* Don't fold an out-of-bound access. */
1964 if (!tree_int_cst_le (t
, nelts
))
1966 rhs
= cp_fold_convert (ssizetype
, rhs
);
1967 /* Don't fold if rhs can't be divided exactly by TYPE_SIZE_UNIT.
1968 constexpr int A[1]; ... (char *)&A[0] + 1 */
1969 if (!integer_zerop (fold_build2_loc (loc
, TRUNC_MOD_EXPR
, sizetype
,
1970 rhs
, TYPE_SIZE_UNIT (type
))))
1972 /* Make sure to treat the second operand of POINTER_PLUS_EXPR
1974 rhs
= fold_build2_loc (loc
, EXACT_DIV_EXPR
, ssizetype
, rhs
,
1975 TYPE_SIZE_UNIT (type
));
1976 t
= size_binop_loc (loc
, PLUS_EXPR
, rhs
, t
);
1977 t
= build4_loc (loc
, ARRAY_REF
, type
, TREE_OPERAND (lhs
, 0),
1978 t
, NULL_TREE
, NULL_TREE
);
1979 t
= cp_build_addr_expr (t
, tf_warning_or_error
);
1980 t
= cp_fold_convert (orig_type
, t
);
1981 return cxx_eval_constant_expression (ctx
, t
, /*lval*/false,
1982 non_constant_p
, overflow_p
);
1988 /* Subroutine of cxx_eval_constant_expression.
1989 Like cxx_eval_unary_expression, except for binary expressions. */
1992 cxx_eval_binary_expression (const constexpr_ctx
*ctx
, tree t
,
1994 bool *non_constant_p
, bool *overflow_p
)
1997 tree orig_lhs
= TREE_OPERAND (t
, 0);
1998 tree orig_rhs
= TREE_OPERAND (t
, 1);
2000 lhs
= cxx_eval_constant_expression (ctx
, orig_lhs
, /*lval*/false,
2001 non_constant_p
, overflow_p
);
2002 /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer
2004 if (*non_constant_p
)
2006 rhs
= cxx_eval_constant_expression (ctx
, orig_rhs
, /*lval*/false,
2007 non_constant_p
, overflow_p
);
2008 if (*non_constant_p
)
2011 location_t loc
= EXPR_LOCATION (t
);
2012 enum tree_code code
= TREE_CODE (t
);
2013 tree type
= TREE_TYPE (t
);
2015 if (code
== EQ_EXPR
|| code
== NE_EXPR
)
2017 bool is_code_eq
= (code
== EQ_EXPR
);
2019 if (TREE_CODE (lhs
) == PTRMEM_CST
2020 && TREE_CODE (rhs
) == PTRMEM_CST
)
2021 r
= constant_boolean_node (cp_tree_equal (lhs
, rhs
) == is_code_eq
,
2023 else if ((TREE_CODE (lhs
) == PTRMEM_CST
2024 || TREE_CODE (rhs
) == PTRMEM_CST
)
2025 && (null_member_pointer_value_p (lhs
)
2026 || null_member_pointer_value_p (rhs
)))
2027 r
= constant_boolean_node (!is_code_eq
, type
);
2028 else if (TREE_CODE (lhs
) == PTRMEM_CST
)
2029 lhs
= cplus_expand_constant (lhs
);
2030 else if (TREE_CODE (rhs
) == PTRMEM_CST
)
2031 rhs
= cplus_expand_constant (rhs
);
2033 if (code
== POINTER_PLUS_EXPR
&& !*non_constant_p
2034 && integer_zerop (lhs
) && !integer_zerop (rhs
))
2037 error ("arithmetic involving a null pointer in %qE", lhs
);
2040 else if (code
== POINTER_PLUS_EXPR
)
2041 r
= cxx_fold_pointer_plus_expression (ctx
, t
, lhs
, rhs
, non_constant_p
,
2045 r
= fold_binary_loc (loc
, code
, type
, lhs
, rhs
);
2049 if (lhs
== orig_lhs
&& rhs
== orig_rhs
)
2052 r
= build2_loc (loc
, code
, type
, lhs
, rhs
);
2054 else if (cxx_eval_check_shift_p (loc
, ctx
, code
, type
, lhs
, rhs
))
2055 *non_constant_p
= true;
2056 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
2057 a local array in a constexpr function. */
2058 bool ptr
= POINTER_TYPE_P (TREE_TYPE (lhs
));
2060 VERIFY_CONSTANT (r
);
2064 /* Subroutine of cxx_eval_constant_expression.
2065 Attempt to evaluate condition expressions. Dead branches are not
2069 cxx_eval_conditional_expression (const constexpr_ctx
*ctx
, tree t
,
2071 bool *non_constant_p
, bool *overflow_p
,
2074 tree val
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
2076 non_constant_p
, overflow_p
);
2077 VERIFY_CONSTANT (val
);
2078 /* Don't VERIFY_CONSTANT the other operands. */
2079 if (integer_zerop (val
))
2080 return cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 2),
2082 non_constant_p
, overflow_p
,
2084 return cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
2086 non_constant_p
, overflow_p
,
2090 /* Subroutine of cxx_eval_constant_expression.
2091 Attempt to evaluate vector condition expressions. Unlike
2092 cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal
2093 ternary arithmetics operation, where all 3 arguments have to be
2094 evaluated as constants and then folding computes the result from
2098 cxx_eval_vector_conditional_expression (const constexpr_ctx
*ctx
, tree t
,
2099 bool *non_constant_p
, bool *overflow_p
)
2101 tree arg1
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
2103 non_constant_p
, overflow_p
);
2104 VERIFY_CONSTANT (arg1
);
2105 tree arg2
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
2107 non_constant_p
, overflow_p
);
2108 VERIFY_CONSTANT (arg2
);
2109 tree arg3
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 2),
2111 non_constant_p
, overflow_p
);
2112 VERIFY_CONSTANT (arg3
);
2113 location_t loc
= EXPR_LOCATION (t
);
2114 tree type
= TREE_TYPE (t
);
2115 tree r
= fold_ternary_loc (loc
, VEC_COND_EXPR
, type
, arg1
, arg2
, arg3
);
2118 if (arg1
== TREE_OPERAND (t
, 0)
2119 && arg2
== TREE_OPERAND (t
, 1)
2120 && arg3
== TREE_OPERAND (t
, 2))
2123 r
= build3_loc (loc
, VEC_COND_EXPR
, type
, arg1
, arg2
, arg3
);
2125 VERIFY_CONSTANT (r
);
2129 /* Returns less than, equal to, or greater than zero if KEY is found to be
2130 less than, to match, or to be greater than the constructor_elt's INDEX. */
2133 array_index_cmp (tree key
, tree index
)
2135 gcc_assert (TREE_CODE (key
) == INTEGER_CST
);
2137 switch (TREE_CODE (index
))
2140 return tree_int_cst_compare (key
, index
);
2143 tree lo
= TREE_OPERAND (index
, 0);
2144 tree hi
= TREE_OPERAND (index
, 1);
2145 if (tree_int_cst_lt (key
, lo
))
2147 else if (tree_int_cst_lt (hi
, key
))
2157 /* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1
2158 if none. If INSERT is true, insert a matching element rather than fail. */
2160 static HOST_WIDE_INT
2161 find_array_ctor_elt (tree ary
, tree dindex
, bool insert
= false)
2163 if (tree_int_cst_sgn (dindex
) < 0)
2166 unsigned HOST_WIDE_INT i
= tree_to_uhwi (dindex
);
2167 vec
<constructor_elt
, va_gc
> *elts
= CONSTRUCTOR_ELTS (ary
);
2168 unsigned HOST_WIDE_INT len
= vec_safe_length (elts
);
2170 unsigned HOST_WIDE_INT end
= len
;
2171 unsigned HOST_WIDE_INT begin
= 0;
2173 /* If the last element of the CONSTRUCTOR has its own index, we can assume
2174 that the same is true of the other elements and index directly. */
2177 tree cindex
= (*elts
)[end
-1].index
;
2178 if (TREE_CODE (cindex
) == INTEGER_CST
2179 && compare_tree_int (cindex
, end
-1) == 0)
2188 /* Otherwise, find a matching index by means of a binary search. */
2189 while (begin
!= end
)
2191 unsigned HOST_WIDE_INT middle
= (begin
+ end
) / 2;
2192 constructor_elt
&elt
= (*elts
)[middle
];
2193 tree idx
= elt
.index
;
2195 int cmp
= array_index_cmp (dindex
, idx
);
2202 if (insert
&& TREE_CODE (idx
) == RANGE_EXPR
)
2204 /* We need to split the range. */
2206 tree lo
= TREE_OPERAND (idx
, 0);
2207 tree hi
= TREE_OPERAND (idx
, 1);
2208 if (tree_int_cst_lt (lo
, dindex
))
2210 /* There are still some lower elts; shorten the range. */
2211 tree new_hi
= int_const_binop (MINUS_EXPR
, dindex
,
2213 if (tree_int_cst_equal (lo
, new_hi
))
2214 /* Only one element left, no longer a range. */
2217 TREE_OPERAND (idx
, 1) = new_hi
;
2218 /* Append the element we want to insert. */
2221 e
.value
= unshare_constructor (elt
.value
);
2222 vec_safe_insert (CONSTRUCTOR_ELTS (ary
), middle
, e
);
2225 /* No lower elts, the range elt is now ours. */
2228 if (tree_int_cst_lt (dindex
, hi
))
2230 /* There are still some higher elts; append a range. */
2231 tree new_lo
= int_const_binop (PLUS_EXPR
, dindex
,
2233 if (tree_int_cst_equal (new_lo
, hi
))
2236 e
.index
= build2 (RANGE_EXPR
, sizetype
, new_lo
, hi
);
2237 e
.value
= unshare_constructor (elt
.value
);
2238 vec_safe_insert (CONSTRUCTOR_ELTS (ary
), middle
+1, e
);
2247 constructor_elt e
= { dindex
, NULL_TREE
};
2248 vec_safe_insert (CONSTRUCTOR_ELTS (ary
), end
, e
);
2255 /* Under the control of CTX, issue a detailed diagnostic for
2256 an out-of-bounds subscript INDEX into the expression ARRAY. */
2259 diag_array_subscript (const constexpr_ctx
*ctx
, tree array
, tree index
)
2263 tree arraytype
= TREE_TYPE (array
);
2265 /* Convert the unsigned array subscript to a signed integer to avoid
2266 printing huge numbers for small negative values. */
2267 tree sidx
= fold_convert (ssizetype
, index
);
2270 error ("array subscript value %qE is outside the bounds "
2271 "of array %qD of type %qT", sidx
, array
, arraytype
);
2272 inform (DECL_SOURCE_LOCATION (array
), "declared here");
2275 error ("array subscript value %qE is outside the bounds "
2276 "of array type %qT", sidx
, arraytype
);
2280 /* Extract element INDEX consisting of CHARS_PER_ELT chars from
2281 STRING_CST STRING. */
2284 extract_string_elt (tree string
, unsigned chars_per_elt
, unsigned index
)
2286 tree type
= cv_unqualified (TREE_TYPE (TREE_TYPE (string
)));
2289 if (chars_per_elt
== 1)
2290 r
= build_int_cst (type
, TREE_STRING_POINTER (string
)[index
]);
2293 const unsigned char *ptr
2294 = ((const unsigned char *)TREE_STRING_POINTER (string
)
2295 + index
* chars_per_elt
);
2296 r
= native_interpret_expr (type
, ptr
, chars_per_elt
);
2301 /* Subroutine of cxx_eval_constant_expression.
2302 Attempt to reduce a reference to an array slot. */
2305 cxx_eval_array_reference (const constexpr_ctx
*ctx
, tree t
,
2307 bool *non_constant_p
, bool *overflow_p
)
2309 tree oldary
= TREE_OPERAND (t
, 0);
2310 tree ary
= cxx_eval_constant_expression (ctx
, oldary
,
2312 non_constant_p
, overflow_p
);
2314 HOST_WIDE_INT i
= 0;
2315 tree elem_type
= NULL_TREE
;
2316 unsigned len
= 0, elem_nchars
= 1;
2317 if (*non_constant_p
)
2319 oldidx
= TREE_OPERAND (t
, 1);
2320 index
= cxx_eval_constant_expression (ctx
, oldidx
,
2322 non_constant_p
, overflow_p
);
2323 VERIFY_CONSTANT (index
);
2326 elem_type
= TREE_TYPE (TREE_TYPE (ary
));
2327 if (TREE_CODE (ary
) == VIEW_CONVERT_EXPR
2328 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary
, 0)))
2329 && TREE_TYPE (t
) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary
, 0))))
2330 ary
= TREE_OPERAND (ary
, 0);
2331 if (TREE_CODE (ary
) == CONSTRUCTOR
)
2332 len
= CONSTRUCTOR_NELTS (ary
);
2333 else if (TREE_CODE (ary
) == STRING_CST
)
2335 elem_nchars
= (TYPE_PRECISION (elem_type
)
2336 / TYPE_PRECISION (char_type_node
));
2337 len
= (unsigned) TREE_STRING_LENGTH (ary
) / elem_nchars
;
2339 else if (TREE_CODE (ary
) == VECTOR_CST
)
2340 len
= VECTOR_CST_NELTS (ary
);
2343 /* We can't do anything with other tree codes, so use
2344 VERIFY_CONSTANT to complain and fail. */
2345 VERIFY_CONSTANT (ary
);
2349 if (!tree_fits_shwi_p (index
)
2350 || (i
= tree_to_shwi (index
)) < 0)
2352 diag_array_subscript (ctx
, ary
, index
);
2353 *non_constant_p
= true;
2359 if (TREE_CODE (TREE_TYPE (ary
)) == ARRAY_TYPE
)
2360 nelts
= array_type_nelts_top (TREE_TYPE (ary
));
2361 else if (VECTOR_TYPE_P (TREE_TYPE (ary
)))
2362 nelts
= size_int (TYPE_VECTOR_SUBPARTS (TREE_TYPE (ary
)));
2366 /* For VLAs, the number of elements won't be an integer constant. */
2367 nelts
= cxx_eval_constant_expression (ctx
, nelts
, false, non_constant_p
,
2369 VERIFY_CONSTANT (nelts
);
2371 ? !tree_int_cst_le (index
, nelts
)
2372 : !tree_int_cst_lt (index
, nelts
))
2373 || tree_int_cst_sgn (index
) < 0)
2375 diag_array_subscript (ctx
, ary
, index
);
2376 *non_constant_p
= true;
2380 if (lval
&& ary
== oldary
&& index
== oldidx
)
2383 return build4 (ARRAY_REF
, TREE_TYPE (t
), ary
, index
, NULL
, NULL
);
2386 if (TREE_CODE (ary
) == CONSTRUCTOR
)
2388 HOST_WIDE_INT ix
= find_array_ctor_elt (ary
, index
);
2399 if (TREE_CODE (ary
) == CONSTRUCTOR
)
2400 r
= (*CONSTRUCTOR_ELTS (ary
))[i
].value
;
2401 else if (TREE_CODE (ary
) == VECTOR_CST
)
2402 r
= VECTOR_CST_ELT (ary
, i
);
2404 r
= extract_string_elt (ary
, elem_nchars
, i
);
2407 /* Don't VERIFY_CONSTANT here. */
2410 /* Otherwise the element doesn't have a value yet. */
2415 if (TREE_CODE (ary
) == CONSTRUCTOR
2416 && CONSTRUCTOR_NO_IMPLICIT_ZERO (ary
))
2418 /* 'ary' is part of the aggregate initializer we're currently
2419 building; if there's no initializer for this element yet,
2422 error ("accessing uninitialized array element");
2423 *non_constant_p
= true;
2427 /* If it's within the array bounds but doesn't have an explicit
2428 initializer, it's value-initialized. */
2429 tree val
= build_value_init (elem_type
, tf_warning_or_error
);
2430 return cxx_eval_constant_expression (ctx
, val
, lval
, non_constant_p
,
2434 /* Subroutine of cxx_eval_constant_expression.
2435 Attempt to reduce a field access of a value of class type. */
2438 cxx_eval_component_reference (const constexpr_ctx
*ctx
, tree t
,
2440 bool *non_constant_p
, bool *overflow_p
)
2442 unsigned HOST_WIDE_INT i
;
2445 tree part
= TREE_OPERAND (t
, 1);
2446 tree orig_whole
= TREE_OPERAND (t
, 0);
2447 tree whole
= cxx_eval_constant_expression (ctx
, orig_whole
,
2449 non_constant_p
, overflow_p
);
2450 if (TREE_CODE (whole
) == INDIRECT_REF
2451 && integer_zerop (TREE_OPERAND (whole
, 0))
2453 error ("dereferencing a null pointer in %qE", orig_whole
);
2455 if (TREE_CODE (whole
) == PTRMEM_CST
)
2456 whole
= cplus_expand_constant (whole
);
2457 if (whole
== orig_whole
)
2460 return fold_build3 (COMPONENT_REF
, TREE_TYPE (t
),
2461 whole
, part
, NULL_TREE
);
2462 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
2464 if (!*non_constant_p
&& TREE_CODE (whole
) != CONSTRUCTOR
)
2467 error ("%qE is not a constant expression", orig_whole
);
2468 *non_constant_p
= true;
2470 if (DECL_MUTABLE_P (part
))
2473 error ("mutable %qD is not usable in a constant expression", part
);
2474 *non_constant_p
= true;
2476 if (*non_constant_p
)
2478 bool pmf
= TYPE_PTRMEMFUNC_P (TREE_TYPE (whole
));
2479 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole
), i
, field
, value
)
2481 /* Use name match for PMF fields, as a variant will have a
2482 different FIELD_DECL with a different type. */
2483 if (pmf
? DECL_NAME (field
) == DECL_NAME (part
)
2489 /* We're in the middle of initializing it. */
2493 if (TREE_CODE (TREE_TYPE (whole
)) == UNION_TYPE
2494 && CONSTRUCTOR_NELTS (whole
) > 0)
2496 /* DR 1188 says we don't have to deal with this. */
2498 error ("accessing %qD member instead of initialized %qD member in "
2499 "constant expression", part
, CONSTRUCTOR_ELT (whole
, 0)->index
);
2500 *non_constant_p
= true;
2504 /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty
2505 classes never get represented; throw together a value now. */
2506 if (is_really_empty_class (TREE_TYPE (t
)))
2507 return build_constructor (TREE_TYPE (t
), NULL
);
2509 gcc_assert (DECL_CONTEXT (part
) == TYPE_MAIN_VARIANT (TREE_TYPE (whole
)));
2511 if (CONSTRUCTOR_NO_IMPLICIT_ZERO (whole
))
2513 /* 'whole' is part of the aggregate initializer we're currently
2514 building; if there's no initializer for this member yet, that's an
2517 error ("accessing uninitialized member %qD", part
);
2518 *non_constant_p
= true;
2522 /* If there's no explicit init for this field, it's value-initialized. */
2523 value
= build_value_init (TREE_TYPE (t
), tf_warning_or_error
);
2524 return cxx_eval_constant_expression (ctx
, value
,
2526 non_constant_p
, overflow_p
);
2529 /* Subroutine of cxx_eval_constant_expression.
2530 Attempt to reduce a field access of a value of class type that is
2531 expressed as a BIT_FIELD_REF. */
2534 cxx_eval_bit_field_ref (const constexpr_ctx
*ctx
, tree t
,
2536 bool *non_constant_p
, bool *overflow_p
)
2538 tree orig_whole
= TREE_OPERAND (t
, 0);
2539 tree retval
, fldval
, utype
, mask
;
2540 bool fld_seen
= false;
2541 HOST_WIDE_INT istart
, isize
;
2542 tree whole
= cxx_eval_constant_expression (ctx
, orig_whole
,
2544 non_constant_p
, overflow_p
);
2545 tree start
, field
, value
;
2546 unsigned HOST_WIDE_INT i
;
2548 if (whole
== orig_whole
)
2550 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
2552 if (!*non_constant_p
2553 && TREE_CODE (whole
) != VECTOR_CST
2554 && TREE_CODE (whole
) != CONSTRUCTOR
)
2557 error ("%qE is not a constant expression", orig_whole
);
2558 *non_constant_p
= true;
2560 if (*non_constant_p
)
2563 if (TREE_CODE (whole
) == VECTOR_CST
)
2564 return fold_ternary (BIT_FIELD_REF
, TREE_TYPE (t
), whole
,
2565 TREE_OPERAND (t
, 1), TREE_OPERAND (t
, 2));
2567 start
= TREE_OPERAND (t
, 2);
2568 istart
= tree_to_shwi (start
);
2569 isize
= tree_to_shwi (TREE_OPERAND (t
, 1));
2570 utype
= TREE_TYPE (t
);
2571 if (!TYPE_UNSIGNED (utype
))
2572 utype
= build_nonstandard_integer_type (TYPE_PRECISION (utype
), 1);
2573 retval
= build_int_cst (utype
, 0);
2574 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole
), i
, field
, value
)
2576 tree bitpos
= bit_position (field
);
2577 if (bitpos
== start
&& DECL_SIZE (field
) == TREE_OPERAND (t
, 1))
2579 if (TREE_CODE (TREE_TYPE (field
)) == INTEGER_TYPE
2580 && TREE_CODE (value
) == INTEGER_CST
2581 && tree_fits_shwi_p (bitpos
)
2582 && tree_fits_shwi_p (DECL_SIZE (field
)))
2584 HOST_WIDE_INT bit
= tree_to_shwi (bitpos
);
2585 HOST_WIDE_INT sz
= tree_to_shwi (DECL_SIZE (field
));
2586 HOST_WIDE_INT shift
;
2587 if (bit
>= istart
&& bit
+ sz
<= istart
+ isize
)
2589 fldval
= fold_convert (utype
, value
);
2590 mask
= build_int_cst_type (utype
, -1);
2591 mask
= fold_build2 (LSHIFT_EXPR
, utype
, mask
,
2592 size_int (TYPE_PRECISION (utype
) - sz
));
2593 mask
= fold_build2 (RSHIFT_EXPR
, utype
, mask
,
2594 size_int (TYPE_PRECISION (utype
) - sz
));
2595 fldval
= fold_build2 (BIT_AND_EXPR
, utype
, fldval
, mask
);
2596 shift
= bit
- istart
;
2597 if (BYTES_BIG_ENDIAN
)
2598 shift
= TYPE_PRECISION (utype
) - shift
- sz
;
2599 fldval
= fold_build2 (LSHIFT_EXPR
, utype
, fldval
,
2601 retval
= fold_build2 (BIT_IOR_EXPR
, utype
, retval
, fldval
);
2607 return fold_convert (TREE_TYPE (t
), retval
);
2609 return error_mark_node
;
2612 /* Subroutine of cxx_eval_constant_expression.
2613 Evaluate a short-circuited logical expression T in the context
2614 of a given constexpr CALL. BAILOUT_VALUE is the value for
2615 early return. CONTINUE_VALUE is used here purely for
2616 sanity check purposes. */
2619 cxx_eval_logical_expression (const constexpr_ctx
*ctx
, tree t
,
2620 tree bailout_value
, tree continue_value
,
2622 bool *non_constant_p
, bool *overflow_p
)
2625 tree lhs
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
2627 non_constant_p
, overflow_p
);
2628 VERIFY_CONSTANT (lhs
);
2629 if (tree_int_cst_equal (lhs
, bailout_value
))
2631 gcc_assert (tree_int_cst_equal (lhs
, continue_value
));
2632 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
2633 lval
, non_constant_p
,
2635 VERIFY_CONSTANT (r
);
2639 /* REF is a COMPONENT_REF designating a particular field. V is a vector of
2640 CONSTRUCTOR elements to initialize (part of) an object containing that
2641 field. Return a pointer to the constructor_elt corresponding to the
2642 initialization of the field. */
2644 static constructor_elt
*
2645 base_field_constructor_elt (vec
<constructor_elt
, va_gc
> *v
, tree ref
)
2647 tree aggr
= TREE_OPERAND (ref
, 0);
2648 tree field
= TREE_OPERAND (ref
, 1);
2650 constructor_elt
*ce
;
2652 gcc_assert (TREE_CODE (ref
) == COMPONENT_REF
);
2654 if (TREE_CODE (aggr
) == COMPONENT_REF
)
2656 constructor_elt
*base_ce
2657 = base_field_constructor_elt (v
, aggr
);
2658 v
= CONSTRUCTOR_ELTS (base_ce
->value
);
2661 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
2662 if (ce
->index
== field
)
2669 /* Some of the expressions fed to the constexpr mechanism are calls to
2670 constructors, which have type void. In that case, return the type being
2671 initialized by the constructor. */
2674 initialized_type (tree t
)
2678 tree type
= cv_unqualified (TREE_TYPE (t
));
2679 if (TREE_CODE (t
) == CALL_EXPR
|| TREE_CODE (t
) == AGGR_INIT_EXPR
)
2681 /* A constructor call has void type, so we need to look deeper. */
2682 tree fn
= get_function_named_in_call (t
);
2683 if (fn
&& TREE_CODE (fn
) == FUNCTION_DECL
2684 && DECL_CXX_CONSTRUCTOR_P (fn
))
2685 type
= DECL_CONTEXT (fn
);
2690 /* We're about to initialize element INDEX of an array or class from VALUE.
2691 Set up NEW_CTX appropriately by adjusting .object to refer to the
2692 subobject and creating a new CONSTRUCTOR if the element is itself
2693 a class or array. */
2696 init_subob_ctx (const constexpr_ctx
*ctx
, constexpr_ctx
&new_ctx
,
2697 tree index
, tree
&value
)
2701 if (index
&& TREE_CODE (index
) != INTEGER_CST
2702 && TREE_CODE (index
) != FIELD_DECL
)
2703 /* This won't have an element in the new CONSTRUCTOR. */
2706 tree type
= initialized_type (value
);
2707 if (!AGGREGATE_TYPE_P (type
) && !VECTOR_TYPE_P (type
))
2708 /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
2711 /* The sub-aggregate initializer might contain a placeholder;
2712 update object to refer to the subobject and ctor to refer to
2713 the (newly created) sub-initializer. */
2715 new_ctx
.object
= build_ctor_subob_ref (index
, type
, ctx
->object
);
2716 tree elt
= build_constructor (type
, NULL
);
2717 CONSTRUCTOR_NO_IMPLICIT_ZERO (elt
) = true;
2720 if (TREE_CODE (value
) == TARGET_EXPR
)
2721 /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
2722 value
= TARGET_EXPR_INITIAL (value
);
2725 /* We're about to process an initializer for a class or array TYPE. Make
2726 sure that CTX is set up appropriately. */
2729 verify_ctor_sanity (const constexpr_ctx
*ctx
, tree type
)
2731 /* We don't bother building a ctor for an empty base subobject. */
2732 if (is_empty_class (type
))
2735 /* We're in the middle of an initializer that might involve placeholders;
2736 our caller should have created a CONSTRUCTOR for us to put the
2737 initializer into. We will either return that constructor or T. */
2738 gcc_assert (ctx
->ctor
);
2739 gcc_assert (same_type_ignoring_top_level_qualifiers_p
2740 (type
, TREE_TYPE (ctx
->ctor
)));
2741 /* We used to check that ctx->ctor was empty, but that isn't the case when
2742 the object is zero-initialized before calling the constructor. */
2745 tree otype
= TREE_TYPE (ctx
->object
);
2746 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type
, otype
)
2747 /* Handle flexible array members. */
2748 || (TREE_CODE (otype
) == ARRAY_TYPE
2749 && TYPE_DOMAIN (otype
) == NULL_TREE
2750 && TREE_CODE (type
) == ARRAY_TYPE
2751 && (same_type_ignoring_top_level_qualifiers_p
2752 (TREE_TYPE (type
), TREE_TYPE (otype
)))));
2754 gcc_assert (!ctx
->object
|| !DECL_P (ctx
->object
)
2755 || *(ctx
->values
->get (ctx
->object
)) == ctx
->ctor
);
2758 /* Subroutine of cxx_eval_constant_expression.
2759 The expression tree T denotes a C-style array or a C-style
2760 aggregate. Reduce it to a constant expression. */
2763 cxx_eval_bare_aggregate (const constexpr_ctx
*ctx
, tree t
,
2765 bool *non_constant_p
, bool *overflow_p
)
2767 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (t
);
2768 bool changed
= false;
2769 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t
));
2770 tree type
= TREE_TYPE (t
);
2772 constexpr_ctx new_ctx
;
2773 if (TYPE_PTRMEMFUNC_P (type
) || VECTOR_TYPE_P (type
))
2775 /* We don't really need the ctx->ctor business for a PMF or
2776 vector, but it's simpler to use the same code. */
2778 new_ctx
.ctor
= build_constructor (type
, NULL
);
2779 new_ctx
.object
= NULL_TREE
;
2782 verify_ctor_sanity (ctx
, type
);
2783 vec
<constructor_elt
, va_gc
> **p
= &CONSTRUCTOR_ELTS (ctx
->ctor
);
2784 vec_alloc (*p
, vec_safe_length (v
));
2788 bool constant_p
= true;
2789 bool side_effects_p
= false;
2790 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, index
, value
)
2792 tree orig_value
= value
;
2793 init_subob_ctx (ctx
, new_ctx
, index
, value
);
2794 if (new_ctx
.ctor
!= ctx
->ctor
)
2795 /* If we built a new CONSTRUCTOR, attach it now so that other
2796 initializers can refer to it. */
2797 CONSTRUCTOR_APPEND_ELT (*p
, index
, new_ctx
.ctor
);
2798 tree elt
= cxx_eval_constant_expression (&new_ctx
, value
,
2800 non_constant_p
, overflow_p
);
2801 /* Don't VERIFY_CONSTANT here. */
2802 if (ctx
->quiet
&& *non_constant_p
)
2804 if (elt
!= orig_value
)
2807 if (!TREE_CONSTANT (elt
))
2809 if (TREE_SIDE_EFFECTS (elt
))
2810 side_effects_p
= true;
2811 if (index
&& TREE_CODE (index
) == COMPONENT_REF
)
2813 /* This is an initialization of a vfield inside a base
2814 subaggregate that we already initialized; push this
2815 initialization into the previous initialization. */
2816 constructor_elt
*inner
= base_field_constructor_elt (*p
, index
);
2821 && (TREE_CODE (index
) == NOP_EXPR
2822 || TREE_CODE (index
) == POINTER_PLUS_EXPR
))
2824 /* This is an initializer for an empty base; now that we've
2825 checked that it's constant, we can ignore it. */
2826 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index
))));
2829 else if (new_ctx
.ctor
!= ctx
->ctor
)
2831 /* We appended this element above; update the value. */
2832 gcc_assert ((*p
)->last().index
== index
);
2833 (*p
)->last().value
= elt
;
2836 CONSTRUCTOR_APPEND_ELT (*p
, index
, elt
);
2838 if (*non_constant_p
|| !changed
)
2841 /* We're done building this CONSTRUCTOR, so now we can interpret an
2842 element without an explicit initializer as value-initialized. */
2843 CONSTRUCTOR_NO_IMPLICIT_ZERO (t
) = false;
2844 TREE_CONSTANT (t
) = constant_p
;
2845 TREE_SIDE_EFFECTS (t
) = side_effects_p
;
2846 if (VECTOR_TYPE_P (type
))
2851 /* Subroutine of cxx_eval_constant_expression.
2852 The expression tree T is a VEC_INIT_EXPR which denotes the desired
2853 initialization of a non-static data member of array type. Reduce it to a
2856 Note that apart from value-initialization (when VALUE_INIT is true),
2857 this is only intended to support value-initialization and the
2858 initializations done by defaulted constructors for classes with
2859 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
2860 will either be NULL_TREE for the default constructor, or a COMPONENT_REF
2861 for the copy/move constructor. */
2864 cxx_eval_vec_init_1 (const constexpr_ctx
*ctx
, tree atype
, tree init
,
2865 bool value_init
, bool lval
,
2866 bool *non_constant_p
, bool *overflow_p
)
2868 tree elttype
= TREE_TYPE (atype
);
2869 unsigned HOST_WIDE_INT max
= tree_to_uhwi (array_type_nelts_top (atype
));
2870 verify_ctor_sanity (ctx
, atype
);
2871 vec
<constructor_elt
, va_gc
> **p
= &CONSTRUCTOR_ELTS (ctx
->ctor
);
2872 vec_alloc (*p
, max
+ 1);
2873 bool pre_init
= false;
2874 unsigned HOST_WIDE_INT i
;
2876 /* For the default constructor, build up a call to the default
2877 constructor of the element type. We only need to handle class types
2878 here, as for a constructor to be constexpr, all members must be
2879 initialized, which for a defaulted default constructor means they must
2880 be of a class type with a constexpr default constructor. */
2881 if (TREE_CODE (elttype
) == ARRAY_TYPE
)
2882 /* We only do this at the lowest level. */;
2883 else if (value_init
)
2885 init
= build_value_init (elttype
, tf_warning_or_error
);
2890 vec
<tree
, va_gc
> *argvec
= make_tree_vector ();
2891 init
= build_special_member_call (NULL_TREE
, complete_ctor_identifier
,
2892 &argvec
, elttype
, LOOKUP_NORMAL
,
2893 tf_warning_or_error
);
2894 release_tree_vector (argvec
);
2895 init
= build_aggr_init_expr (TREE_TYPE (init
), init
);
2899 for (i
= 0; i
< max
; ++i
)
2901 tree idx
= build_int_cst (size_type_node
, i
);
2904 constexpr_ctx new_ctx
;
2905 init_subob_ctx (ctx
, new_ctx
, idx
, pre_init
? init
: elttype
);
2906 if (new_ctx
.ctor
!= ctx
->ctor
)
2907 CONSTRUCTOR_APPEND_ELT (*p
, idx
, new_ctx
.ctor
);
2908 if (TREE_CODE (elttype
) == ARRAY_TYPE
)
2910 /* A multidimensional array; recurse. */
2911 if (value_init
|| init
== NULL_TREE
)
2913 eltinit
= NULL_TREE
;
2917 eltinit
= cp_build_array_ref (input_location
, init
, idx
,
2918 tf_warning_or_error
);
2919 eltinit
= cxx_eval_vec_init_1 (&new_ctx
, elttype
, eltinit
, value_init
,
2921 non_constant_p
, overflow_p
);
2925 /* Initializing an element using value or default initialization
2926 we just pre-built above. */
2927 eltinit
= cxx_eval_constant_expression (&new_ctx
, init
, lval
,
2928 non_constant_p
, overflow_p
);
2933 /* Copying an element. */
2934 gcc_assert (same_type_ignoring_top_level_qualifiers_p
2935 (atype
, TREE_TYPE (init
)));
2936 eltinit
= cp_build_array_ref (input_location
, init
, idx
,
2937 tf_warning_or_error
);
2938 if (!lvalue_p (init
))
2939 eltinit
= move (eltinit
);
2940 eltinit
= force_rvalue (eltinit
, tf_warning_or_error
);
2941 eltinit
= (cxx_eval_constant_expression
2942 (&new_ctx
, eltinit
, lval
,
2943 non_constant_p
, overflow_p
));
2945 if (*non_constant_p
&& !ctx
->quiet
)
2947 if (new_ctx
.ctor
!= ctx
->ctor
)
2949 /* We appended this element above; update the value. */
2950 gcc_assert ((*p
)->last().index
== idx
);
2951 (*p
)->last().value
= eltinit
;
2954 CONSTRUCTOR_APPEND_ELT (*p
, idx
, eltinit
);
2955 /* Reuse the result of cxx_eval_constant_expression call
2956 from the first iteration to all others if it is a constant
2957 initializer that doesn't require relocations. */
2960 && (initializer_constant_valid_p (eltinit
, TREE_TYPE (eltinit
))
2961 == null_pointer_node
))
2963 if (new_ctx
.ctor
!= ctx
->ctor
)
2964 eltinit
= new_ctx
.ctor
;
2965 for (i
= 1; i
< max
; ++i
)
2967 idx
= build_int_cst (size_type_node
, i
);
2968 CONSTRUCTOR_APPEND_ELT (*p
, idx
, unshare_constructor (eltinit
));
2974 if (!*non_constant_p
)
2977 CONSTRUCTOR_NO_IMPLICIT_ZERO (init
) = false;
2983 cxx_eval_vec_init (const constexpr_ctx
*ctx
, tree t
,
2985 bool *non_constant_p
, bool *overflow_p
)
2987 tree atype
= TREE_TYPE (t
);
2988 tree init
= VEC_INIT_EXPR_INIT (t
);
2989 tree r
= cxx_eval_vec_init_1 (ctx
, atype
, init
,
2990 VEC_INIT_EXPR_VALUE_INIT (t
),
2991 lval
, non_constant_p
, overflow_p
);
2992 if (*non_constant_p
)
2998 /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
2999 match. We want to be less strict for simple *& folding; if we have a
3000 non-const temporary that we access through a const pointer, that should
3001 work. We handle this here rather than change fold_indirect_ref_1
3002 because we're dealing with things like ADDR_EXPR of INTEGER_CST which
3003 don't really make sense outside of constant expression evaluation. Also
3004 we want to allow folding to COMPONENT_REF, which could cause trouble
3005 with TBAA in fold_indirect_ref_1.
3007 Try to keep this function synced with fold_indirect_ref_1. */
3010 cxx_fold_indirect_ref (location_t loc
, tree type
, tree op0
, bool *empty_base
)
3016 subtype
= TREE_TYPE (sub
);
3017 if (!POINTER_TYPE_P (subtype
))
3020 if (TREE_CODE (sub
) == ADDR_EXPR
)
3022 tree op
= TREE_OPERAND (sub
, 0);
3023 tree optype
= TREE_TYPE (op
);
3025 /* *&CONST_DECL -> to the value of the const decl. */
3026 if (TREE_CODE (op
) == CONST_DECL
)
3027 return DECL_INITIAL (op
);
3028 /* *&p => p; make sure to handle *&"str"[cst] here. */
3029 if (same_type_ignoring_top_level_qualifiers_p (optype
, type
)
3030 /* Also handle the case where the desired type is an array of unknown
3031 bounds because the variable has had its bounds deduced since the
3032 ADDR_EXPR was created. */
3033 || (TREE_CODE (type
) == ARRAY_TYPE
3034 && TREE_CODE (optype
) == ARRAY_TYPE
3035 && TYPE_DOMAIN (type
) == NULL_TREE
3036 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (optype
),
3039 tree fop
= fold_read_from_constant_string (op
);
3045 /* *(foo *)&fooarray => fooarray[0] */
3046 else if (TREE_CODE (optype
) == ARRAY_TYPE
3047 && (same_type_ignoring_top_level_qualifiers_p
3048 (type
, TREE_TYPE (optype
))))
3050 tree type_domain
= TYPE_DOMAIN (optype
);
3051 tree min_val
= size_zero_node
;
3052 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
3053 min_val
= TYPE_MIN_VALUE (type_domain
);
3054 return build4_loc (loc
, ARRAY_REF
, type
, op
, min_val
,
3055 NULL_TREE
, NULL_TREE
);
3057 /* *(foo *)&complexfoo => __real__ complexfoo */
3058 else if (TREE_CODE (optype
) == COMPLEX_TYPE
3059 && (same_type_ignoring_top_level_qualifiers_p
3060 (type
, TREE_TYPE (optype
))))
3061 return fold_build1_loc (loc
, REALPART_EXPR
, type
, op
);
3062 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
3063 else if (VECTOR_TYPE_P (optype
)
3064 && (same_type_ignoring_top_level_qualifiers_p
3065 (type
, TREE_TYPE (optype
))))
3067 tree part_width
= TYPE_SIZE (type
);
3068 tree index
= bitsize_int (0);
3069 return fold_build3_loc (loc
, BIT_FIELD_REF
, type
, op
, part_width
, index
);
3071 /* Also handle conversion to an empty base class, which
3072 is represented with a NOP_EXPR. */
3073 else if (is_empty_class (type
)
3074 && CLASS_TYPE_P (optype
)
3075 && DERIVED_FROM_P (type
, optype
))
3080 /* *(foo *)&struct_with_foo_field => COMPONENT_REF */
3081 else if (RECORD_OR_UNION_TYPE_P (optype
))
3083 tree field
= TYPE_FIELDS (optype
);
3084 for (; field
; field
= DECL_CHAIN (field
))
3085 if (TREE_CODE (field
) == FIELD_DECL
3086 && TREE_TYPE (field
) != error_mark_node
3087 && integer_zerop (byte_position (field
))
3088 && (same_type_ignoring_top_level_qualifiers_p
3089 (TREE_TYPE (field
), type
)))
3090 return fold_build3 (COMPONENT_REF
, type
, op
, field
, NULL_TREE
);
3093 else if (TREE_CODE (sub
) == POINTER_PLUS_EXPR
3094 && TREE_CODE (TREE_OPERAND (sub
, 1)) == INTEGER_CST
)
3096 tree op00
= TREE_OPERAND (sub
, 0);
3097 tree op01
= TREE_OPERAND (sub
, 1);
3100 if (TREE_CODE (op00
) == ADDR_EXPR
)
3103 op00
= TREE_OPERAND (op00
, 0);
3104 op00type
= TREE_TYPE (op00
);
3106 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
3107 if (VECTOR_TYPE_P (op00type
)
3108 && (same_type_ignoring_top_level_qualifiers_p
3109 (type
, TREE_TYPE (op00type
))))
3111 HOST_WIDE_INT offset
= tree_to_shwi (op01
);
3112 tree part_width
= TYPE_SIZE (type
);
3113 unsigned HOST_WIDE_INT part_widthi
= tree_to_shwi (part_width
)/BITS_PER_UNIT
;
3114 unsigned HOST_WIDE_INT indexi
= offset
* BITS_PER_UNIT
;
3115 tree index
= bitsize_int (indexi
);
3117 if (offset
/ part_widthi
< TYPE_VECTOR_SUBPARTS (op00type
))
3118 return fold_build3_loc (loc
,
3119 BIT_FIELD_REF
, type
, op00
,
3123 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
3124 else if (TREE_CODE (op00type
) == COMPLEX_TYPE
3125 && (same_type_ignoring_top_level_qualifiers_p
3126 (type
, TREE_TYPE (op00type
))))
3128 tree size
= TYPE_SIZE_UNIT (type
);
3129 if (tree_int_cst_equal (size
, op01
))
3130 return fold_build1_loc (loc
, IMAGPART_EXPR
, type
, op00
);
3132 /* ((foo *)&fooarray)[1] => fooarray[1] */
3133 else if (TREE_CODE (op00type
) == ARRAY_TYPE
3134 && (same_type_ignoring_top_level_qualifiers_p
3135 (type
, TREE_TYPE (op00type
))))
3137 tree type_domain
= TYPE_DOMAIN (op00type
);
3138 tree min_val
= size_zero_node
;
3139 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
3140 min_val
= TYPE_MIN_VALUE (type_domain
);
3141 op01
= size_binop_loc (loc
, EXACT_DIV_EXPR
, op01
,
3142 TYPE_SIZE_UNIT (type
));
3143 op01
= size_binop_loc (loc
, PLUS_EXPR
, op01
, min_val
);
3144 return build4_loc (loc
, ARRAY_REF
, type
, op00
, op01
,
3145 NULL_TREE
, NULL_TREE
);
3147 /* Also handle conversion to an empty base class, which
3148 is represented with a NOP_EXPR. */
3149 else if (is_empty_class (type
)
3150 && CLASS_TYPE_P (op00type
)
3151 && DERIVED_FROM_P (type
, op00type
))
3156 /* ((foo *)&struct_with_foo_field)[1] => COMPONENT_REF */
3157 else if (RECORD_OR_UNION_TYPE_P (op00type
))
3159 tree field
= TYPE_FIELDS (op00type
);
3160 for (; field
; field
= DECL_CHAIN (field
))
3161 if (TREE_CODE (field
) == FIELD_DECL
3162 && TREE_TYPE (field
) != error_mark_node
3163 && tree_int_cst_equal (byte_position (field
), op01
)
3164 && (same_type_ignoring_top_level_qualifiers_p
3165 (TREE_TYPE (field
), type
)))
3166 return fold_build3 (COMPONENT_REF
, type
, op00
,
3171 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
3172 else if (TREE_CODE (TREE_TYPE (subtype
)) == ARRAY_TYPE
3173 && (same_type_ignoring_top_level_qualifiers_p
3174 (type
, TREE_TYPE (TREE_TYPE (subtype
)))))
3177 tree min_val
= size_zero_node
;
3178 tree newsub
= cxx_fold_indirect_ref (loc
, TREE_TYPE (subtype
), sub
, NULL
);
3182 sub
= build1_loc (loc
, INDIRECT_REF
, TREE_TYPE (subtype
), sub
);
3183 type_domain
= TYPE_DOMAIN (TREE_TYPE (sub
));
3184 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
3185 min_val
= TYPE_MIN_VALUE (type_domain
);
3186 return build4_loc (loc
, ARRAY_REF
, type
, sub
, min_val
, NULL_TREE
,
3194 cxx_eval_indirect_ref (const constexpr_ctx
*ctx
, tree t
,
3196 bool *non_constant_p
, bool *overflow_p
)
3198 tree orig_op0
= TREE_OPERAND (t
, 0);
3199 bool empty_base
= false;
3201 /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second
3202 operand is an integer-zero. Otherwise reject the MEM_REF for now. */
3204 if (TREE_CODE (t
) == MEM_REF
3205 && (!TREE_OPERAND (t
, 1) || !integer_zerop (TREE_OPERAND (t
, 1))))
3207 gcc_assert (ctx
->quiet
);
3208 *non_constant_p
= true;
3212 /* First try to simplify it directly. */
3213 tree r
= cxx_fold_indirect_ref (EXPR_LOCATION (t
), TREE_TYPE (t
), orig_op0
,
3217 /* If that didn't work, evaluate the operand first. */
3218 tree op0
= cxx_eval_constant_expression (ctx
, orig_op0
,
3219 /*lval*/false, non_constant_p
,
3221 /* Don't VERIFY_CONSTANT here. */
3222 if (*non_constant_p
)
3225 if (!lval
&& integer_zerop (op0
))
3228 error ("dereferencing a null pointer");
3229 *non_constant_p
= true;
3233 r
= cxx_fold_indirect_ref (EXPR_LOCATION (t
), TREE_TYPE (t
), op0
,
3237 /* We couldn't fold to a constant value. Make sure it's not
3238 something we should have been able to fold. */
3241 if (TREE_CODE (sub
) == ADDR_EXPR
)
3243 gcc_assert (!same_type_ignoring_top_level_qualifiers_p
3244 (TREE_TYPE (TREE_TYPE (sub
)), TREE_TYPE (t
)));
3245 /* DR 1188 says we don't have to deal with this. */
3247 error ("accessing value of %qE through a %qT glvalue in a "
3248 "constant expression", build_fold_indirect_ref (sub
),
3250 *non_constant_p
= true;
3254 if (lval
&& op0
!= orig_op0
)
3255 return build1 (INDIRECT_REF
, TREE_TYPE (t
), op0
);
3257 VERIFY_CONSTANT (t
);
3262 r
= cxx_eval_constant_expression (ctx
, r
,
3263 lval
, non_constant_p
, overflow_p
);
3264 if (*non_constant_p
)
3267 /* If we're pulling out the value of an empty base, just return an empty
3269 if (empty_base
&& !lval
)
3271 r
= build_constructor (TREE_TYPE (t
), NULL
);
3272 TREE_CONSTANT (r
) = true;
3278 /* Complain about R, a VAR_DECL, not being usable in a constant expression.
3279 Shared between potential_constant_expression and
3280 cxx_eval_constant_expression. */
3283 non_const_var_error (tree r
)
3285 tree type
= TREE_TYPE (r
);
3286 error ("the value of %qD is not usable in a constant "
3288 /* Avoid error cascade. */
3289 if (DECL_INITIAL (r
) == error_mark_node
)
3291 if (DECL_DECLARED_CONSTEXPR_P (r
))
3292 inform (DECL_SOURCE_LOCATION (r
),
3293 "%qD used in its own initializer", r
);
3294 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
3296 if (!CP_TYPE_CONST_P (type
))
3297 inform (DECL_SOURCE_LOCATION (r
),
3298 "%q#D is not const", r
);
3299 else if (CP_TYPE_VOLATILE_P (type
))
3300 inform (DECL_SOURCE_LOCATION (r
),
3301 "%q#D is volatile", r
);
3302 else if (!DECL_INITIAL (r
)
3303 || !TREE_CONSTANT (DECL_INITIAL (r
))
3304 || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
))
3305 inform (DECL_SOURCE_LOCATION (r
),
3306 "%qD was not initialized with a constant "
3311 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
3312 inform (DECL_SOURCE_LOCATION (r
),
3313 "%qD was not initialized with a constant "
3317 if (cxx_dialect
>= cxx11
&& !DECL_DECLARED_CONSTEXPR_P (r
))
3318 inform (DECL_SOURCE_LOCATION (r
),
3319 "%qD was not declared %<constexpr%>", r
);
3321 inform (DECL_SOURCE_LOCATION (r
),
3322 "%qD does not have integral or enumeration type",
3327 /* Subroutine of cxx_eval_constant_expression.
3328 Like cxx_eval_unary_expression, except for trinary expressions. */
3331 cxx_eval_trinary_expression (const constexpr_ctx
*ctx
, tree t
,
3333 bool *non_constant_p
, bool *overflow_p
)
3339 for (i
= 0; i
< 3; i
++)
3341 args
[i
] = cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, i
),
3343 non_constant_p
, overflow_p
);
3344 VERIFY_CONSTANT (args
[i
]);
3347 val
= fold_ternary_loc (EXPR_LOCATION (t
), TREE_CODE (t
), TREE_TYPE (t
),
3348 args
[0], args
[1], args
[2]);
3349 if (val
== NULL_TREE
)
3351 VERIFY_CONSTANT (val
);
3355 /* True if T was declared in a function declared to be constexpr, and
3356 therefore potentially constant in C++14. */
3359 var_in_constexpr_fn (tree t
)
3361 tree ctx
= DECL_CONTEXT (t
);
3362 return (ctx
&& TREE_CODE (ctx
) == FUNCTION_DECL
3363 && DECL_DECLARED_CONSTEXPR_P (ctx
));
3366 /* True if T was declared in a function that might be constexpr: either a
3367 function that was declared constexpr, or a C++17 lambda op(). */
3370 var_in_maybe_constexpr_fn (tree t
)
3372 if (cxx_dialect
>= cxx17
3373 && DECL_FUNCTION_SCOPE_P (t
)
3374 && LAMBDA_FUNCTION_P (DECL_CONTEXT (t
)))
3376 return var_in_constexpr_fn (t
);
3379 /* We're assigning INIT to TARGET. In do_build_copy_constructor and
3380 build_over_call we implement trivial copy of a class with tail padding using
3381 assignment of character arrays, which is valid in normal code, but not in
3382 constexpr evaluation. We don't need to worry about clobbering tail padding
3383 in constexpr evaluation, so strip the type punning. */
3386 maybe_simplify_trivial_copy (tree
&target
, tree
&init
)
3388 if (TREE_CODE (target
) == MEM_REF
3389 && TREE_CODE (init
) == MEM_REF
3390 && TREE_TYPE (target
) == TREE_TYPE (init
)
3391 && TREE_CODE (TREE_TYPE (target
)) == ARRAY_TYPE
3392 && TREE_TYPE (TREE_TYPE (target
)) == unsigned_char_type_node
)
3394 target
= build_fold_indirect_ref (TREE_OPERAND (target
, 0));
3395 init
= build_fold_indirect_ref (TREE_OPERAND (init
, 0));
3399 /* Evaluate an INIT_EXPR or MODIFY_EXPR. */
3402 cxx_eval_store_expression (const constexpr_ctx
*ctx
, tree t
,
3404 bool *non_constant_p
, bool *overflow_p
)
3406 constexpr_ctx new_ctx
= *ctx
;
3408 tree init
= TREE_OPERAND (t
, 1);
3409 if (TREE_CLOBBER_P (init
))
3410 /* Just ignore clobbers. */
3413 /* First we figure out where we're storing to. */
3414 tree target
= TREE_OPERAND (t
, 0);
3416 maybe_simplify_trivial_copy (target
, init
);
3418 tree type
= TREE_TYPE (target
);
3419 target
= cxx_eval_constant_expression (ctx
, target
,
3421 non_constant_p
, overflow_p
);
3422 if (*non_constant_p
)
3425 /* cxx_eval_array_reference for lval = true allows references one past
3426 end of array, because it does not know if it is just taking address
3427 (which is valid), or actual dereference. Here we know it is
3428 a dereference, so diagnose it here. */
3429 for (tree probe
= target
; probe
; )
3431 switch (TREE_CODE (probe
))
3435 ary
= TREE_OPERAND (probe
, 0);
3436 if (TREE_CODE (TREE_TYPE (ary
)) == ARRAY_TYPE
)
3437 nelts
= array_type_nelts_top (TREE_TYPE (ary
));
3438 else if (VECTOR_TYPE_P (TREE_TYPE (ary
)))
3439 nelts
= size_int (TYPE_VECTOR_SUBPARTS (TREE_TYPE (ary
)));
3442 nelts
= cxx_eval_constant_expression (ctx
, nelts
, false,
3443 non_constant_p
, overflow_p
);
3444 VERIFY_CONSTANT (nelts
);
3445 gcc_assert (TREE_CODE (nelts
) == INTEGER_CST
3446 && TREE_CODE (TREE_OPERAND (probe
, 1)) == INTEGER_CST
);
3447 if (wi::to_widest (TREE_OPERAND (probe
, 1)) == wi::to_widest (nelts
))
3449 diag_array_subscript (ctx
, ary
, TREE_OPERAND (probe
, 1));
3450 *non_constant_p
= true;
3457 probe
= TREE_OPERAND (probe
, 0);
3466 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (target
), type
))
3468 /* For initialization of an empty base, the original target will be
3469 *(base*)this, which the above evaluation resolves to the object
3470 argument, which has the derived type rather than the base type. In
3471 this situation, just evaluate the initializer and return, since
3472 there's no actual data to store. */
3473 gcc_assert (is_empty_class (type
));
3474 return cxx_eval_constant_expression (ctx
, init
, false,
3475 non_constant_p
, overflow_p
);
3478 /* And then find the underlying variable. */
3479 vec
<tree
,va_gc
> *refs
= make_tree_vector();
3480 tree object
= NULL_TREE
;
3481 for (tree probe
= target
; object
== NULL_TREE
; )
3483 switch (TREE_CODE (probe
))
3488 vec_safe_push (refs
, TREE_OPERAND (probe
, 1));
3489 vec_safe_push (refs
, TREE_TYPE (probe
));
3490 probe
= TREE_OPERAND (probe
, 0);
3498 /* And then find/build up our initializer for the path to the subobject
3499 we're initializing. */
3501 if (object
== ctx
->object
&& VAR_P (object
)
3502 && DECL_NAME (object
) && ctx
->call
== NULL
)
3503 /* The variable we're building up an aggregate initializer for is outside
3504 the constant-expression, so don't evaluate the store. We check
3505 DECL_NAME to handle TARGET_EXPR temporaries, which are fair game. */
3507 else if (DECL_P (object
))
3508 valp
= ctx
->values
->get (object
);
3513 /* A constant-expression cannot modify objects from outside the
3514 constant-expression. */
3516 error ("modification of %qE is not a constant expression", object
);
3517 *non_constant_p
= true;
3520 type
= TREE_TYPE (object
);
3521 bool no_zero_init
= true;
3523 vec
<tree
,va_gc
> *ctors
= make_tree_vector ();
3524 while (!refs
->is_empty())
3526 if (*valp
== NULL_TREE
)
3528 *valp
= build_constructor (type
, NULL
);
3529 CONSTRUCTOR_NO_IMPLICIT_ZERO (*valp
) = no_zero_init
;
3531 else if (TREE_CODE (*valp
) == STRING_CST
)
3533 /* An array was initialized with a string constant, and now
3534 we're writing into one of its elements. Explode the
3535 single initialization into a set of element
3537 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
3539 tree string
= *valp
;
3540 tree elt_type
= TREE_TYPE (type
);
3541 unsigned chars_per_elt
= (TYPE_PRECISION (elt_type
)
3542 / TYPE_PRECISION (char_type_node
));
3543 unsigned num_elts
= TREE_STRING_LENGTH (string
) / chars_per_elt
;
3544 tree ary_ctor
= build_constructor (type
, NULL
);
3546 vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor
), num_elts
);
3547 for (unsigned ix
= 0; ix
!= num_elts
; ix
++)
3549 constructor_elt elt
=
3551 build_int_cst (size_type_node
, ix
),
3552 extract_string_elt (string
, chars_per_elt
, ix
)
3554 CONSTRUCTOR_ELTS (ary_ctor
)->quick_push (elt
);
3560 /* If the value of object is already zero-initialized, any new ctors for
3561 subobjects will also be zero-initialized. */
3562 no_zero_init
= CONSTRUCTOR_NO_IMPLICIT_ZERO (*valp
);
3564 vec_safe_push (ctors
, *valp
);
3566 enum tree_code code
= TREE_CODE (type
);
3568 tree index
= refs
->pop();
3570 constructor_elt
*cep
= NULL
;
3571 if (code
== ARRAY_TYPE
)
3574 = find_array_ctor_elt (*valp
, index
, /*insert*/true);
3575 gcc_assert (i
>= 0);
3576 cep
= CONSTRUCTOR_ELT (*valp
, i
);
3577 gcc_assert (TREE_CODE (cep
->index
) != RANGE_EXPR
);
3581 gcc_assert (TREE_CODE (index
) == FIELD_DECL
);
3583 /* We must keep the CONSTRUCTOR's ELTS in FIELD order.
3584 Usually we meet initializers in that order, but it is
3585 possible for base types to be placed not in program
3587 tree fields
= TYPE_FIELDS (DECL_CONTEXT (index
));
3588 unsigned HOST_WIDE_INT idx
;
3590 if (code
== UNION_TYPE
&& CONSTRUCTOR_NELTS (*valp
)
3591 && CONSTRUCTOR_ELT (*valp
, 0)->index
!= index
)
3592 /* Changing active member. */
3593 vec_safe_truncate (CONSTRUCTOR_ELTS (*valp
), 0);
3596 vec_safe_iterate (CONSTRUCTOR_ELTS (*valp
), idx
, &cep
);
3597 idx
++, fields
= DECL_CHAIN (fields
))
3599 if (index
== cep
->index
)
3602 /* The field we're initializing must be on the field
3603 list. Look to see if it is present before the
3604 field the current ELT initializes. */
3605 for (; fields
!= cep
->index
; fields
= DECL_CHAIN (fields
))
3606 if (index
== fields
)
3610 /* We fell off the end of the CONSTRUCTOR, so insert a new
3611 entry at the end. */
3614 constructor_elt ce
= { index
, NULL_TREE
};
3616 vec_safe_insert (CONSTRUCTOR_ELTS (*valp
), idx
, ce
);
3617 cep
= CONSTRUCTOR_ELT (*valp
, idx
);
3623 release_tree_vector (refs
);
3625 if (AGGREGATE_TYPE_P (type
) || VECTOR_TYPE_P (type
))
3627 /* Create a new CONSTRUCTOR in case evaluation of the initializer
3628 wants to modify it. */
3629 if (*valp
== NULL_TREE
)
3631 *valp
= build_constructor (type
, NULL
);
3632 CONSTRUCTOR_NO_IMPLICIT_ZERO (*valp
) = no_zero_init
;
3634 else if (TREE_CODE (*valp
) == PTRMEM_CST
)
3635 *valp
= cplus_expand_constant (*valp
);
3636 new_ctx
.ctor
= *valp
;
3637 new_ctx
.object
= target
;
3640 init
= cxx_eval_constant_expression (&new_ctx
, init
, false,
3641 non_constant_p
, overflow_p
);
3642 /* Don't share a CONSTRUCTOR that might be changed later. */
3643 init
= unshare_constructor (init
);
3644 if (target
== object
)
3645 /* The hash table might have moved since the get earlier. */
3646 valp
= ctx
->values
->get (object
);
3648 if (TREE_CODE (init
) == CONSTRUCTOR
)
3650 /* An outer ctx->ctor might be pointing to *valp, so replace
3652 CONSTRUCTOR_ELTS (*valp
) = CONSTRUCTOR_ELTS (init
);
3653 TREE_CONSTANT (*valp
) = TREE_CONSTANT (init
);
3654 TREE_SIDE_EFFECTS (*valp
) = TREE_SIDE_EFFECTS (init
);
3655 CONSTRUCTOR_NO_IMPLICIT_ZERO (*valp
)
3656 = CONSTRUCTOR_NO_IMPLICIT_ZERO (init
);
3661 /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing
3662 CONSTRUCTORs, if any. */
3665 bool c
= TREE_CONSTANT (init
);
3666 bool s
= TREE_SIDE_EFFECTS (init
);
3668 FOR_EACH_VEC_SAFE_ELT (ctors
, i
, elt
)
3671 TREE_CONSTANT (elt
) = false;
3673 TREE_SIDE_EFFECTS (elt
) = true;
3675 release_tree_vector (ctors
);
3677 if (*non_constant_p
)
3685 /* Evaluate a ++ or -- expression. */
3688 cxx_eval_increment_expression (const constexpr_ctx
*ctx
, tree t
,
3690 bool *non_constant_p
, bool *overflow_p
)
3692 enum tree_code code
= TREE_CODE (t
);
3693 tree type
= TREE_TYPE (t
);
3694 tree op
= TREE_OPERAND (t
, 0);
3695 tree offset
= TREE_OPERAND (t
, 1);
3696 gcc_assert (TREE_CONSTANT (offset
));
3698 /* The operand as an lvalue. */
3699 op
= cxx_eval_constant_expression (ctx
, op
, true,
3700 non_constant_p
, overflow_p
);
3702 /* The operand as an rvalue. */
3704 = cxx_eval_constant_expression (ctx
, op
, false,
3705 non_constant_p
, overflow_p
);
3706 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
3707 a local array in a constexpr function. */
3708 bool ptr
= POINTER_TYPE_P (TREE_TYPE (val
));
3710 VERIFY_CONSTANT (val
);
3712 /* The modified value. */
3713 bool inc
= (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
);
3715 if (POINTER_TYPE_P (type
))
3717 /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
3718 offset
= convert_to_ptrofftype (offset
);
3720 offset
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (offset
), offset
);
3721 mod
= fold_build2 (POINTER_PLUS_EXPR
, type
, val
, offset
);
3724 mod
= fold_build2 (inc
? PLUS_EXPR
: MINUS_EXPR
, type
, val
, offset
);
3726 VERIFY_CONSTANT (mod
);
3728 /* Storing the modified value. */
3729 tree store
= build2 (MODIFY_EXPR
, type
, op
, mod
);
3730 cxx_eval_constant_expression (ctx
, store
,
3731 true, non_constant_p
, overflow_p
);
3733 /* And the value of the expression. */
3734 if (code
== PREINCREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
3736 /* Prefix ops are lvalues. */
3740 /* But we optimize when the caller wants an rvalue. */
3744 /* Postfix ops are rvalues. */
3748 /* Predicates for the meaning of *jump_target. */
3751 returns (tree
*jump_target
)
3754 && (TREE_CODE (*jump_target
) == RETURN_EXPR
3755 || (TREE_CODE (*jump_target
) == LABEL_DECL
3756 && LABEL_DECL_CDTOR (*jump_target
)));
3760 breaks (tree
*jump_target
)
3763 && ((TREE_CODE (*jump_target
) == LABEL_DECL
3764 && LABEL_DECL_BREAK (*jump_target
))
3765 || TREE_CODE (*jump_target
) == EXIT_EXPR
);
3769 continues (tree
*jump_target
)
3772 && TREE_CODE (*jump_target
) == LABEL_DECL
3773 && LABEL_DECL_CONTINUE (*jump_target
);
3777 switches (tree
*jump_target
)
3780 && TREE_CODE (*jump_target
) == INTEGER_CST
;
3783 /* Subroutine of cxx_eval_statement_list. Determine whether the statement
3784 STMT matches *jump_target. If we're looking for a case label and we see
3785 the default label, note it in ctx->css_state. */
3788 label_matches (const constexpr_ctx
*ctx
, tree
*jump_target
, tree stmt
)
3790 switch (TREE_CODE (*jump_target
))
3793 if (TREE_CODE (stmt
) == LABEL_EXPR
3794 && LABEL_EXPR_LABEL (stmt
) == *jump_target
)
3799 if (TREE_CODE (stmt
) == CASE_LABEL_EXPR
)
3801 gcc_assert (ctx
->css_state
!= NULL
);
3802 if (!CASE_LOW (stmt
))
3804 /* default: should appear just once in a SWITCH_EXPR
3805 body (excluding nested SWITCH_EXPR). */
3806 gcc_assert (*ctx
->css_state
!= css_default_seen
);
3807 /* When evaluating SWITCH_EXPR body for the second time,
3808 return true for the default: label. */
3809 if (*ctx
->css_state
== css_default_processing
)
3811 *ctx
->css_state
= css_default_seen
;
3813 else if (CASE_HIGH (stmt
))
3815 if (tree_int_cst_le (CASE_LOW (stmt
), *jump_target
)
3816 && tree_int_cst_le (*jump_target
, CASE_HIGH (stmt
)))
3819 else if (tree_int_cst_equal (*jump_target
, CASE_LOW (stmt
)))
3830 /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
3831 semantics, for switch, break, continue, and return. */
3834 cxx_eval_statement_list (const constexpr_ctx
*ctx
, tree t
,
3835 bool *non_constant_p
, bool *overflow_p
,
3838 tree_stmt_iterator i
;
3840 /* In a statement-expression we want to return the last value.
3841 For empty statement expression return void_node. */
3845 local_target
= NULL_TREE
;
3846 jump_target
= &local_target
;
3848 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
3850 tree stmt
= tsi_stmt (i
);
3851 r
= cxx_eval_constant_expression (ctx
, stmt
, false,
3852 non_constant_p
, overflow_p
,
3854 if (*non_constant_p
)
3856 if (returns (jump_target
) || breaks (jump_target
))
3862 /* Evaluate a LOOP_EXPR for side-effects. Handles break and return
3863 semantics; continue semantics are covered by cxx_eval_statement_list. */
3866 cxx_eval_loop_expr (const constexpr_ctx
*ctx
, tree t
,
3867 bool *non_constant_p
, bool *overflow_p
,
3870 constexpr_ctx new_ctx
= *ctx
;
3872 tree body
= TREE_OPERAND (t
, 0);
3876 hash_set
<tree
> save_exprs
;
3877 new_ctx
.save_exprs
= &save_exprs
;
3879 cxx_eval_constant_expression (&new_ctx
, body
, /*lval*/false,
3880 non_constant_p
, overflow_p
, jump_target
);
3882 /* Forget saved values of SAVE_EXPRs. */
3883 for (hash_set
<tree
>::iterator iter
= save_exprs
.begin();
3884 iter
!= save_exprs
.end(); ++iter
)
3885 new_ctx
.values
->remove (*iter
);
3886 if (++count
>= constexpr_loop_limit
)
3889 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
3890 "%<constexpr%> loop iteration count exceeds limit of %d "
3891 "(use -fconstexpr-loop-limit= to increase the limit)",
3892 constexpr_loop_limit
);
3893 *non_constant_p
= true;
3897 while (!returns (jump_target
)
3898 && !breaks (jump_target
)
3899 && !switches (jump_target
)
3900 && !*non_constant_p
);
3902 if (breaks (jump_target
))
3903 *jump_target
= NULL_TREE
;
3908 /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
3912 cxx_eval_switch_expr (const constexpr_ctx
*ctx
, tree t
,
3913 bool *non_constant_p
, bool *overflow_p
,
3916 tree cond
= TREE_OPERAND (t
, 0);
3917 cond
= cxx_eval_constant_expression (ctx
, cond
, false,
3918 non_constant_p
, overflow_p
);
3919 VERIFY_CONSTANT (cond
);
3920 *jump_target
= cond
;
3922 tree body
= TREE_OPERAND (t
, 1);
3923 constexpr_ctx new_ctx
= *ctx
;
3924 constexpr_switch_state css
= css_default_not_seen
;
3925 new_ctx
.css_state
= &css
;
3926 cxx_eval_constant_expression (&new_ctx
, body
, false,
3927 non_constant_p
, overflow_p
, jump_target
);
3928 if (switches (jump_target
) && css
== css_default_seen
)
3930 /* If the SWITCH_EXPR body has default: label, process it once again,
3931 this time instructing label_matches to return true for default:
3932 label on switches (jump_target). */
3933 css
= css_default_processing
;
3934 cxx_eval_constant_expression (&new_ctx
, body
, false,
3935 non_constant_p
, overflow_p
, jump_target
);
3937 if (breaks (jump_target
) || switches (jump_target
))
3938 *jump_target
= NULL_TREE
;
3942 /* Find the object of TYPE under initialization in CTX. */
3945 lookup_placeholder (const constexpr_ctx
*ctx
, bool lval
, tree type
)
3950 /* We could use ctx->object unconditionally, but using ctx->ctor when we
3951 can is a minor optimization. */
3952 if (!lval
&& ctx
->ctor
&& same_type_p (TREE_TYPE (ctx
->ctor
), type
))
3958 /* Since an object cannot have a field of its own type, we can search outward
3959 from ctx->object to find the unique containing object of TYPE. */
3960 tree ob
= ctx
->object
;
3963 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob
), type
))
3965 if (handled_component_p (ob
))
3966 ob
= TREE_OPERAND (ob
, 0);
3974 /* Attempt to reduce the expression T to a constant value.
3975 On failure, issue diagnostic and return error_mark_node. */
3976 /* FIXME unify with c_fully_fold */
3977 /* FIXME overflow_p is too global */
3980 cxx_eval_constant_expression (const constexpr_ctx
*ctx
, tree t
,
3982 bool *non_constant_p
, bool *overflow_p
,
3985 constexpr_ctx new_ctx
;
3988 if (jump_target
&& *jump_target
)
3990 /* If we are jumping, ignore all statements/expressions except those
3991 that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */
3992 switch (TREE_CODE (t
))
3995 case STATEMENT_LIST
:
4000 case CASE_LABEL_EXPR
:
4001 if (label_matches (ctx
, jump_target
, t
))
4003 *jump_target
= NULL_TREE
;
4009 if (t
== error_mark_node
)
4011 *non_constant_p
= true;
4014 if (CONSTANT_CLASS_P (t
))
4016 if (TREE_OVERFLOW (t
))
4019 permerror (input_location
, "overflow in constant expression");
4020 if (!flag_permissive
|| ctx
->quiet
)
4024 if (TREE_CODE (t
) == INTEGER_CST
4025 && TREE_CODE (TREE_TYPE (t
)) == POINTER_TYPE
4026 && !integer_zerop (t
))
4029 error ("value %qE of type %qT is not a constant expression",
4031 *non_constant_p
= true;
4037 tree_code tcode
= TREE_CODE (t
);
4043 /* We ask for an rvalue for the RESULT_DECL when indirecting
4044 through an invisible reference, or in named return value
4046 return (*ctx
->values
->get (t
));
4049 if (DECL_HAS_VALUE_EXPR_P (t
))
4050 return cxx_eval_constant_expression (ctx
, DECL_VALUE_EXPR (t
),
4051 lval
, non_constant_p
, overflow_p
);
4054 /* We used to not check lval for CONST_DECL, but darwin.c uses
4055 CONST_DECL for aggregate constants. */
4058 if (COMPLETE_TYPE_P (TREE_TYPE (t
))
4059 && is_really_empty_class (TREE_TYPE (t
)))
4061 /* If the class is empty, we aren't actually loading anything. */
4062 r
= build_constructor (TREE_TYPE (t
), NULL
);
4063 TREE_CONSTANT (r
) = true;
4065 else if (ctx
->strict
)
4066 r
= decl_really_constant_value (t
);
4068 r
= decl_constant_value (t
);
4069 if (TREE_CODE (r
) == TARGET_EXPR
4070 && TREE_CODE (TARGET_EXPR_INITIAL (r
)) == CONSTRUCTOR
)
4071 r
= TARGET_EXPR_INITIAL (r
);
4073 if (tree
*p
= ctx
->values
->get (r
))
4074 if (*p
!= NULL_TREE
)
4079 non_const_var_error (r
);
4080 *non_constant_p
= true;
4088 case CASE_LABEL_EXPR
:
4093 if (lval
&& TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
4095 else if (tree
*p
= ctx
->values
->get (r
))
4098 /* Defer in case this is only used for its type. */;
4099 else if (TREE_CODE (TREE_TYPE (t
)) == REFERENCE_TYPE
)
4100 /* Defer, there's no lvalue->rvalue conversion. */;
4101 else if (COMPLETE_TYPE_P (TREE_TYPE (t
))
4102 && is_really_empty_class (TREE_TYPE (t
)))
4104 /* If the class is empty, we aren't actually loading anything. */
4105 r
= build_constructor (TREE_TYPE (t
), NULL
);
4106 TREE_CONSTANT (r
) = true;
4111 error ("%qE is not a constant expression", t
);
4112 *non_constant_p
= true;
4117 case AGGR_INIT_EXPR
:
4118 r
= cxx_eval_call_expression (ctx
, t
, lval
,
4119 non_constant_p
, overflow_p
);
4124 r
= DECL_EXPR_DECL (t
);
4125 if (AGGREGATE_TYPE_P (TREE_TYPE (r
))
4126 || VECTOR_TYPE_P (TREE_TYPE (r
)))
4130 new_ctx
.ctor
= build_constructor (TREE_TYPE (r
), NULL
);
4131 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx
.ctor
) = true;
4132 new_ctx
.values
->put (r
, new_ctx
.ctor
);
4136 if (tree init
= DECL_INITIAL (r
))
4138 init
= cxx_eval_constant_expression (ctx
, init
,
4140 non_constant_p
, overflow_p
);
4141 /* Don't share a CONSTRUCTOR that might be changed. */
4142 init
= unshare_constructor (init
);
4143 ctx
->values
->put (r
, init
);
4145 else if (ctx
== &new_ctx
)
4146 /* We gave it a CONSTRUCTOR above. */;
4148 ctx
->values
->put (r
, NULL_TREE
);
4153 if (!literal_type_p (TREE_TYPE (t
)))
4157 error ("temporary of non-literal type %qT in a "
4158 "constant expression", TREE_TYPE (t
));
4159 explain_non_literal_class (TREE_TYPE (t
));
4161 *non_constant_p
= true;
4164 if ((AGGREGATE_TYPE_P (TREE_TYPE (t
)) || VECTOR_TYPE_P (TREE_TYPE (t
))))
4166 /* We're being expanded without an explicit target, so start
4167 initializing a new object; expansion with an explicit target
4168 strips the TARGET_EXPR before we get here. */
4170 new_ctx
.ctor
= build_constructor (TREE_TYPE (t
), NULL
);
4171 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx
.ctor
) = true;
4172 new_ctx
.object
= TARGET_EXPR_SLOT (t
);
4173 ctx
->values
->put (new_ctx
.object
, new_ctx
.ctor
);
4176 /* Pass false for 'lval' because this indicates
4177 initialization of a temporary. */
4178 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
4180 non_constant_p
, overflow_p
);
4181 if (!*non_constant_p
)
4182 /* Adjust the type of the result to the type of the temporary. */
4183 r
= adjust_temp_type (TREE_TYPE (t
), r
);
4186 tree slot
= TARGET_EXPR_SLOT (t
);
4187 r
= unshare_constructor (r
);
4188 ctx
->values
->put (slot
, r
);
4195 gcc_assert (jump_target
== NULL
|| *jump_target
== NULL_TREE
);
4196 r
= cxx_eval_store_expression (ctx
, t
, lval
,
4197 non_constant_p
, overflow_p
);
4201 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
4203 non_constant_p
, overflow_p
);
4207 if (TREE_OPERAND (t
, 0) != NULL_TREE
)
4208 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
4210 non_constant_p
, overflow_p
);
4215 /* Avoid evaluating a SAVE_EXPR more than once. */
4216 if (tree
*p
= ctx
->values
->get (t
))
4220 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), false,
4221 non_constant_p
, overflow_p
);
4222 ctx
->values
->put (t
, r
);
4223 if (ctx
->save_exprs
)
4224 ctx
->save_exprs
->add (t
);
4228 case NON_LVALUE_EXPR
:
4229 case TRY_CATCH_EXPR
:
4231 case CLEANUP_POINT_EXPR
:
4232 case MUST_NOT_THROW_EXPR
:
4235 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
4237 non_constant_p
, overflow_p
,
4241 case TRY_FINALLY_EXPR
:
4242 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), lval
,
4243 non_constant_p
, overflow_p
,
4245 if (!*non_constant_p
)
4246 /* Also evaluate the cleanup. */
4247 cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1), true,
4248 non_constant_p
, overflow_p
,
4252 /* These differ from cxx_eval_unary_expression in that this doesn't
4253 check for a constant operand or result; an address can be
4254 constant without its operand being, and vice versa. */
4257 r
= cxx_eval_indirect_ref (ctx
, t
, lval
,
4258 non_constant_p
, overflow_p
);
4263 tree oldop
= TREE_OPERAND (t
, 0);
4264 tree op
= cxx_eval_constant_expression (ctx
, oldop
,
4266 non_constant_p
, overflow_p
);
4267 /* Don't VERIFY_CONSTANT here. */
4268 if (*non_constant_p
)
4270 gcc_checking_assert (TREE_CODE (op
) != CONSTRUCTOR
);
4271 /* This function does more aggressive folding than fold itself. */
4272 r
= build_fold_addr_expr_with_type (op
, TREE_TYPE (t
));
4273 if (TREE_CODE (r
) == ADDR_EXPR
&& TREE_OPERAND (r
, 0) == oldop
)
4282 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), lval
,
4283 non_constant_p
, overflow_p
);
4284 if (r
== error_mark_node
)
4286 else if (r
== TREE_OPERAND (t
, 0))
4289 r
= fold_build1 (TREE_CODE (t
), TREE_TYPE (t
), r
);
4294 case FIX_TRUNC_EXPR
:
4299 case TRUTH_NOT_EXPR
:
4300 case FIXED_CONVERT_EXPR
:
4301 r
= cxx_eval_unary_expression (ctx
, t
, lval
,
4302 non_constant_p
, overflow_p
);
4306 r
= fold_sizeof_expr (t
);
4307 VERIFY_CONSTANT (r
);
4312 /* check_return_expr sometimes wraps a TARGET_EXPR in a
4313 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
4314 introduced by build_call_a. */
4315 tree op0
= TREE_OPERAND (t
, 0);
4316 tree op1
= TREE_OPERAND (t
, 1);
4318 if ((TREE_CODE (op0
) == TARGET_EXPR
&& op1
== TARGET_EXPR_SLOT (op0
))
4319 || TREE_CODE (op1
) == EMPTY_CLASS_EXPR
)
4320 r
= cxx_eval_constant_expression (ctx
, op0
,
4321 lval
, non_constant_p
, overflow_p
,
4325 /* Check that the LHS is constant and then discard it. */
4326 cxx_eval_constant_expression (ctx
, op0
,
4327 true, non_constant_p
, overflow_p
,
4329 if (*non_constant_p
)
4331 op1
= TREE_OPERAND (t
, 1);
4332 r
= cxx_eval_constant_expression (ctx
, op1
,
4333 lval
, non_constant_p
, overflow_p
,
4339 case POINTER_PLUS_EXPR
:
4340 case POINTER_DIFF_EXPR
:
4344 case TRUNC_DIV_EXPR
:
4346 case FLOOR_DIV_EXPR
:
4347 case ROUND_DIV_EXPR
:
4348 case TRUNC_MOD_EXPR
:
4350 case ROUND_MOD_EXPR
:
4352 case EXACT_DIV_EXPR
:
4362 case TRUTH_XOR_EXPR
:
4369 case UNORDERED_EXPR
:
4379 r
= cxx_eval_binary_expression (ctx
, t
, lval
,
4380 non_constant_p
, overflow_p
);
4383 /* fold can introduce non-IF versions of these; still treat them as
4384 short-circuiting. */
4385 case TRUTH_AND_EXPR
:
4386 case TRUTH_ANDIF_EXPR
:
4387 r
= cxx_eval_logical_expression (ctx
, t
, boolean_false_node
,
4390 non_constant_p
, overflow_p
);
4394 case TRUTH_ORIF_EXPR
:
4395 r
= cxx_eval_logical_expression (ctx
, t
, boolean_true_node
,
4398 non_constant_p
, overflow_p
);
4402 r
= cxx_eval_array_reference (ctx
, t
, lval
,
4403 non_constant_p
, overflow_p
);
4407 if (is_overloaded_fn (t
))
4409 /* We can only get here in checking mode via
4410 build_non_dependent_expr, because any expression that
4411 calls or takes the address of the function will have
4412 pulled a FUNCTION_DECL out of the COMPONENT_REF. */
4413 gcc_checking_assert (ctx
->quiet
|| errorcount
);
4414 *non_constant_p
= true;
4417 r
= cxx_eval_component_reference (ctx
, t
, lval
,
4418 non_constant_p
, overflow_p
);
4422 r
= cxx_eval_bit_field_ref (ctx
, t
, lval
,
4423 non_constant_p
, overflow_p
);
4427 if (jump_target
&& *jump_target
)
4429 /* When jumping to a label, the label might be either in the
4430 then or else blocks, so process then block first in skipping
4431 mode first, and if we are still in the skipping mode at its end,
4432 process the else block too. */
4433 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
4434 lval
, non_constant_p
, overflow_p
,
4437 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 2),
4438 lval
, non_constant_p
, overflow_p
,
4442 r
= cxx_eval_conditional_expression (ctx
, t
, lval
,
4443 non_constant_p
, overflow_p
,
4447 r
= cxx_eval_vector_conditional_expression (ctx
, t
, non_constant_p
,
4452 if (TREE_CONSTANT (t
))
4454 /* Don't re-process a constant CONSTRUCTOR, but do fold it to
4455 VECTOR_CST if applicable. */
4456 /* FIXME after GCC 6 branches, make the verify unconditional. */
4458 verify_constructor_flags (t
);
4460 recompute_constructor_flags (t
);
4461 if (TREE_CONSTANT (t
))
4464 r
= cxx_eval_bare_aggregate (ctx
, t
, lval
,
4465 non_constant_p
, overflow_p
);
4469 /* We can get this in a defaulted constructor for a class with a
4470 non-static data member of array type. Either the initializer will
4471 be NULL, meaning default-initialization, or it will be an lvalue
4472 or xvalue of the same type, meaning direct-initialization from the
4473 corresponding member. */
4474 r
= cxx_eval_vec_init (ctx
, t
, lval
,
4475 non_constant_p
, overflow_p
);
4480 r
= cxx_eval_trinary_expression (ctx
, t
, lval
,
4481 non_constant_p
, overflow_p
);
4485 case VIEW_CONVERT_EXPR
:
4487 case UNARY_PLUS_EXPR
:
4489 tree oldop
= TREE_OPERAND (t
, 0);
4491 tree op
= cxx_eval_constant_expression (ctx
, oldop
,
4493 non_constant_p
, overflow_p
);
4494 if (*non_constant_p
)
4496 tree type
= TREE_TYPE (t
);
4497 if (TREE_CODE (op
) == PTRMEM_CST
4498 && !TYPE_PTRMEM_P (type
))
4499 op
= cplus_expand_constant (op
);
4500 if (TREE_CODE (op
) == PTRMEM_CST
&& tcode
== NOP_EXPR
)
4502 if (same_type_ignoring_top_level_qualifiers_p (type
,
4504 || can_convert_qual (type
, op
))
4505 return cp_fold_convert (type
, op
);
4509 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
4510 "a reinterpret_cast is not a constant expression");
4511 *non_constant_p
= true;
4516 if (POINTER_TYPE_P (type
) && TREE_CODE (op
) == INTEGER_CST
)
4518 if (integer_zerop (op
))
4520 if (TREE_CODE (type
) == REFERENCE_TYPE
)
4523 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
4524 "dereferencing a null pointer");
4525 *non_constant_p
= true;
4528 else if (TREE_CODE (TREE_TYPE (op
)) == POINTER_TYPE
)
4530 tree from
= TREE_TYPE (op
);
4532 if (!can_convert (type
, from
, tf_none
))
4535 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
4536 "conversion of %qT null pointer to %qT "
4537 "is not a constant expression",
4539 *non_constant_p
= true;
4546 /* This detects for example:
4547 reinterpret_cast<void*>(sizeof 0)
4550 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
4551 "%<reinterpret_cast<%T>(%E)%> is not "
4552 "a constant expression",
4554 *non_constant_p
= true;
4558 if (op
== oldop
&& tcode
!= UNARY_PLUS_EXPR
)
4559 /* We didn't fold at the top so we could check for ptr-int
4562 if (tcode
== UNARY_PLUS_EXPR
)
4563 r
= fold_convert (TREE_TYPE (t
), op
);
4565 r
= fold_build1 (tcode
, type
, op
);
4566 /* Conversion of an out-of-range value has implementation-defined
4567 behavior; the language considers it different from arithmetic
4568 overflow, which is undefined. */
4569 if (TREE_OVERFLOW_P (r
) && !TREE_OVERFLOW_P (op
))
4570 TREE_OVERFLOW (r
) = false;
4574 case EMPTY_CLASS_EXPR
:
4575 /* This is good enough for a function argument that might not get
4576 used, and they can't do anything with it, so just return it. */
4579 case STATEMENT_LIST
:
4581 new_ctx
.ctor
= new_ctx
.object
= NULL_TREE
;
4582 return cxx_eval_statement_list (&new_ctx
, t
,
4583 non_constant_p
, overflow_p
, jump_target
);
4586 return cxx_eval_constant_expression (ctx
, BIND_EXPR_BODY (t
),
4588 non_constant_p
, overflow_p
,
4591 case PREINCREMENT_EXPR
:
4592 case POSTINCREMENT_EXPR
:
4593 case PREDECREMENT_EXPR
:
4594 case POSTDECREMENT_EXPR
:
4595 return cxx_eval_increment_expression (ctx
, t
,
4596 lval
, non_constant_p
, overflow_p
);
4602 case VEC_DELETE_EXPR
:
4605 /* GCC internal stuff. */
4608 case NON_DEPENDENT_EXPR
:
4612 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
4613 "expression %qE is not a constant expression", t
);
4614 *non_constant_p
= true;
4617 case PLACEHOLDER_EXPR
:
4618 /* Use of the value or address of the current object. */
4619 if (tree ctor
= lookup_placeholder (ctx
, lval
, TREE_TYPE (t
)))
4620 return cxx_eval_constant_expression (ctx
, ctor
, lval
,
4621 non_constant_p
, overflow_p
);
4622 /* A placeholder without a referent. We can get here when
4623 checking whether NSDMIs are noexcept, or in massage_init_elt;
4624 just say it's non-constant for now. */
4625 gcc_assert (ctx
->quiet
);
4626 *non_constant_p
= true;
4631 tree cond
= TREE_OPERAND (t
, 0);
4632 cond
= cxx_eval_constant_expression (ctx
, cond
, /*lval*/false,
4633 non_constant_p
, overflow_p
);
4634 VERIFY_CONSTANT (cond
);
4635 if (integer_nonzerop (cond
))
4641 *jump_target
= TREE_OPERAND (t
, 0);
4642 gcc_assert (breaks (jump_target
) || continues (jump_target
)
4643 /* Allow for jumping to a cdtor_label. */
4644 || returns (jump_target
));
4648 cxx_eval_loop_expr (ctx
, t
,
4649 non_constant_p
, overflow_p
, jump_target
);
4653 cxx_eval_switch_expr (ctx
, t
,
4654 non_constant_p
, overflow_p
, jump_target
);
4658 /* It's possible to get a requires-expression in a constant
4659 expression. For example:
4661 template<typename T> concept bool C() {
4662 return requires (T t) { t; };
4665 template<typename T> requires !C<T>() void f(T);
4667 Normalization leaves f with the associated constraint
4668 '!requires (T t) { ... }' which is not transformed into
4670 if (!processing_template_decl
)
4671 return evaluate_constraint_expression (t
, NULL_TREE
);
4673 *non_constant_p
= true;
4677 gcc_assert (tree_to_uhwi (TREE_OPERAND (t
, 1)) == annot_expr_ivdep_kind
);
4678 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
4680 non_constant_p
, overflow_p
,
4685 if (STATEMENT_CODE_P (TREE_CODE (t
)))
4687 /* This function doesn't know how to deal with pre-genericize
4688 statements; this can only happen with statement-expressions,
4689 so for now just fail. */
4691 error_at (EXPR_LOCATION (t
),
4692 "statement is not a constant expression");
4695 internal_error ("unexpected expression %qE of kind %s", t
,
4696 get_tree_code_name (TREE_CODE (t
)));
4697 *non_constant_p
= true;
4701 if (r
== error_mark_node
)
4702 *non_constant_p
= true;
4704 if (*non_constant_p
)
4711 cxx_eval_outermost_constant_expr (tree t
, bool allow_non_constant
,
4712 bool strict
= true, tree object
= NULL_TREE
)
4714 auto_timevar
time (TV_CONSTEXPR
);
4716 bool non_constant_p
= false;
4717 bool overflow_p
= false;
4718 hash_map
<tree
,tree
> map
;
4720 constexpr_ctx ctx
= { NULL
, &map
, NULL
, NULL
, NULL
, NULL
,
4721 allow_non_constant
, strict
};
4723 tree type
= initialized_type (t
);
4725 if (AGGREGATE_TYPE_P (type
) || VECTOR_TYPE_P (type
))
4727 /* In C++14 an NSDMI can participate in aggregate initialization,
4728 and can refer to the address of the object being initialized, so
4729 we need to pass in the relevant VAR_DECL if we want to do the
4730 evaluation in a single pass. The evaluation will dynamically
4731 update ctx.values for the VAR_DECL. We use the same strategy
4732 for C++11 constexpr constructors that refer to the object being
4734 ctx
.ctor
= build_constructor (type
, NULL
);
4735 CONSTRUCTOR_NO_IMPLICIT_ZERO (ctx
.ctor
) = true;
4738 if (TREE_CODE (t
) == TARGET_EXPR
)
4739 object
= TARGET_EXPR_SLOT (t
);
4740 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
)
4741 object
= AGGR_INIT_EXPR_SLOT (t
);
4743 ctx
.object
= object
;
4745 gcc_assert (same_type_ignoring_top_level_qualifiers_p
4746 (type
, TREE_TYPE (object
)));
4747 if (object
&& DECL_P (object
))
4748 map
.put (object
, ctx
.ctor
);
4749 if (TREE_CODE (r
) == TARGET_EXPR
)
4750 /* Avoid creating another CONSTRUCTOR when we expand the
4752 r
= TARGET_EXPR_INITIAL (r
);
4755 r
= cxx_eval_constant_expression (&ctx
, r
,
4756 false, &non_constant_p
, &overflow_p
);
4758 verify_constant (r
, allow_non_constant
, &non_constant_p
, &overflow_p
);
4760 /* Mutable logic is a bit tricky: we want to allow initialization of
4761 constexpr variables with mutable members, but we can't copy those
4762 members to another constexpr variable. */
4763 if (TREE_CODE (r
) == CONSTRUCTOR
4764 && CONSTRUCTOR_MUTABLE_POISON (r
))
4766 if (!allow_non_constant
)
4767 error ("%qE is not a constant expression because it refers to "
4768 "mutable subobjects of %qT", t
, type
);
4769 non_constant_p
= true;
4772 if (TREE_CODE (r
) == CONSTRUCTOR
4773 && CONSTRUCTOR_NO_IMPLICIT_ZERO (r
))
4775 if (!allow_non_constant
)
4776 error ("%qE is not a constant expression because it refers to "
4777 "an incompletely initialized variable", t
);
4778 TREE_CONSTANT (r
) = false;
4779 non_constant_p
= true;
4782 /* Technically we should check this for all subexpressions, but that
4783 runs into problems with our internal representation of pointer
4784 subtraction and the 5.19 rules are still in flux. */
4785 if (CONVERT_EXPR_CODE_P (TREE_CODE (r
))
4786 && ARITHMETIC_TYPE_P (TREE_TYPE (r
))
4787 && TREE_CODE (TREE_OPERAND (r
, 0)) == ADDR_EXPR
)
4789 if (!allow_non_constant
)
4790 error ("conversion from pointer type %qT "
4791 "to arithmetic type %qT in a constant expression",
4792 TREE_TYPE (TREE_OPERAND (r
, 0)), TREE_TYPE (r
));
4793 non_constant_p
= true;
4796 if (!non_constant_p
&& overflow_p
)
4797 non_constant_p
= true;
4799 /* Unshare the result unless it's a CONSTRUCTOR in which case it's already
4801 bool should_unshare
= true;
4802 if (r
== t
|| TREE_CODE (r
) == CONSTRUCTOR
)
4803 should_unshare
= false;
4805 if (non_constant_p
&& !allow_non_constant
)
4806 return error_mark_node
;
4807 else if (non_constant_p
&& TREE_CONSTANT (r
))
4809 /* This isn't actually constant, so unset TREE_CONSTANT. */
4812 else if (TREE_CODE (r
) == CONSTRUCTOR
)
4813 r
= build1 (VIEW_CONVERT_EXPR
, TREE_TYPE (r
), r
);
4815 r
= build_nop (TREE_TYPE (r
), r
);
4816 TREE_CONSTANT (r
) = false;
4818 else if (non_constant_p
|| r
== t
)
4822 r
= unshare_expr (r
);
4824 if (TREE_CODE (r
) == CONSTRUCTOR
&& CLASS_TYPE_P (TREE_TYPE (r
)))
4826 if (TREE_CODE (t
) == TARGET_EXPR
4827 && TARGET_EXPR_INITIAL (t
) == r
)
4831 r
= get_target_expr (r
);
4832 TREE_CONSTANT (r
) = true;
4840 /* Returns true if T is a valid subexpression of a constant expression,
4841 even if it isn't itself a constant expression. */
4844 is_sub_constant_expr (tree t
)
4846 bool non_constant_p
= false;
4847 bool overflow_p
= false;
4848 hash_map
<tree
, tree
> map
;
4850 constexpr_ctx ctx
= { NULL
, &map
, NULL
, NULL
, NULL
, NULL
, true, true };
4852 cxx_eval_constant_expression (&ctx
, t
, false, &non_constant_p
,
4854 return !non_constant_p
&& !overflow_p
;
4857 /* If T represents a constant expression returns its reduced value.
4858 Otherwise return error_mark_node. If T is dependent, then
4862 cxx_constant_value (tree t
, tree decl
)
4864 return cxx_eval_outermost_constant_expr (t
, false, true, decl
);
4867 /* Helper routine for fold_simple function. Either return simplified
4868 expression T, otherwise NULL_TREE.
4869 In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold
4870 even if we are within template-declaration. So be careful on call, as in
4871 such case types can be undefined. */
4874 fold_simple_1 (tree t
)
4877 enum tree_code code
= TREE_CODE (t
);
4889 return fold_sizeof_expr (t
);
4897 case TRUTH_NOT_EXPR
:
4899 case VIEW_CONVERT_EXPR
:
4902 case FIX_TRUNC_EXPR
:
4903 case FIXED_CONVERT_EXPR
:
4904 case ADDR_SPACE_CONVERT_EXPR
:
4906 op1
= TREE_OPERAND (t
, 0);
4908 t
= const_unop (code
, TREE_TYPE (t
), op1
);
4912 if (CONVERT_EXPR_CODE_P (code
)
4913 && TREE_OVERFLOW_P (t
) && !TREE_OVERFLOW_P (op1
))
4914 TREE_OVERFLOW (t
) = false;
4922 /* If T is a simple constant expression, returns its simplified value.
4923 Otherwise returns T. In contrast to maybe_constant_value do we
4924 simplify only few operations on constant-expressions, and we don't
4925 try to simplify constexpressions. */
4928 fold_simple (tree t
)
4931 if (processing_template_decl
)
4934 r
= fold_simple_1 (t
);
4941 /* If T is a constant expression, returns its reduced value.
4942 Otherwise, if T does not have TREE_CONSTANT set, returns T.
4943 Otherwise, returns a version of T without TREE_CONSTANT. */
4945 static GTY((deletable
)) hash_map
<tree
, tree
> *cv_cache
;
4948 maybe_constant_value (tree t
, tree decl
)
4952 if (!is_nondependent_constant_expression (t
))
4954 if (TREE_OVERFLOW_P (t
))
4956 t
= build_nop (TREE_TYPE (t
), t
);
4957 TREE_CONSTANT (t
) = false;
4961 else if (CONSTANT_CLASS_P (t
))
4962 /* No caching or evaluation needed. */
4965 if (cv_cache
== NULL
)
4966 cv_cache
= hash_map
<tree
, tree
>::create_ggc (101);
4967 if (tree
*cached
= cv_cache
->get (t
))
4970 r
= cxx_eval_outermost_constant_expr (t
, true, true, decl
);
4971 gcc_checking_assert (r
== t
4972 || CONVERT_EXPR_P (t
)
4973 || TREE_CODE (t
) == VIEW_CONVERT_EXPR
4974 || (TREE_CONSTANT (t
) && !TREE_CONSTANT (r
))
4975 || !cp_tree_equal (r
, t
));
4976 cv_cache
->put (t
, r
);
4980 /* Dispose of the whole CV_CACHE. */
4983 clear_cv_cache (void)
4985 if (cv_cache
!= NULL
)
4989 /* Dispose of the whole CV_CACHE and FOLD_CACHE. */
4992 clear_cv_and_fold_caches (void)
4995 clear_fold_cache ();
4998 /* Like maybe_constant_value but first fully instantiate the argument.
5000 Note: this is equivalent to instantiate_non_dependent_expr_sfinae
5001 (t, tf_none) followed by maybe_constant_value but is more efficient,
5002 because calls instantiation_dependent_expression_p and
5003 potential_constant_expression at most once. */
5006 fold_non_dependent_expr (tree t
)
5011 /* If we're in a template, but T isn't value dependent, simplify
5012 it. We're supposed to treat:
5014 template <typename T> void f(T[1 + 1]);
5015 template <typename T> void f(T[2]);
5017 as two declarations of the same function, for example. */
5018 if (processing_template_decl
)
5020 if (is_nondependent_constant_expression (t
))
5022 processing_template_decl_sentinel s
;
5023 t
= instantiate_non_dependent_expr_internal (t
, tf_none
);
5025 if (type_unknown_p (t
)
5026 || BRACE_ENCLOSED_INITIALIZER_P (t
))
5028 if (TREE_OVERFLOW_P (t
))
5030 t
= build_nop (TREE_TYPE (t
), t
);
5031 TREE_CONSTANT (t
) = false;
5036 tree r
= cxx_eval_outermost_constant_expr (t
, true, true, NULL_TREE
);
5037 /* cp_tree_equal looks through NOPs, so allow them. */
5038 gcc_checking_assert (r
== t
5039 || CONVERT_EXPR_P (t
)
5040 || TREE_CODE (t
) == VIEW_CONVERT_EXPR
5041 || (TREE_CONSTANT (t
) && !TREE_CONSTANT (r
))
5042 || !cp_tree_equal (r
, t
));
5045 else if (TREE_OVERFLOW_P (t
))
5047 t
= build_nop (TREE_TYPE (t
), t
);
5048 TREE_CONSTANT (t
) = false;
5053 return maybe_constant_value (t
);
5056 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
5057 than wrapped in a TARGET_EXPR. */
5060 maybe_constant_init (tree t
, tree decl
)
5064 if (TREE_CODE (t
) == EXPR_STMT
)
5065 t
= TREE_OPERAND (t
, 0);
5066 if (TREE_CODE (t
) == CONVERT_EXPR
5067 && VOID_TYPE_P (TREE_TYPE (t
)))
5068 t
= TREE_OPERAND (t
, 0);
5069 if (TREE_CODE (t
) == INIT_EXPR
)
5070 t
= TREE_OPERAND (t
, 1);
5071 if (TREE_CODE (t
) == TARGET_EXPR
)
5072 t
= TARGET_EXPR_INITIAL (t
);
5073 if (!is_nondependent_static_init_expression (t
))
5074 /* Don't try to evaluate it. */;
5075 else if (CONSTANT_CLASS_P (t
))
5076 /* No evaluation needed. */;
5078 t
= cxx_eval_outermost_constant_expr (t
, true, false, decl
);
5079 if (TREE_CODE (t
) == TARGET_EXPR
)
5081 tree init
= TARGET_EXPR_INITIAL (t
);
5082 if (TREE_CODE (init
) == CONSTRUCTOR
)
5089 /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
5090 /* Return true if the object referred to by REF has automatic or thread
5093 enum { ck_ok
, ck_bad
, ck_unknown
};
5095 check_automatic_or_tls (tree ref
)
5098 HOST_WIDE_INT bitsize
, bitpos
;
5100 int volatilep
= 0, unsignedp
= 0;
5101 tree decl
= get_inner_reference (ref
, &bitsize
, &bitpos
, &offset
,
5102 &mode
, &unsignedp
, &volatilep
, false);
5105 /* If there isn't a decl in the middle, we don't know the linkage here,
5106 and this isn't a constant expression anyway. */
5109 dk
= decl_storage_duration (decl
);
5110 return (dk
== dk_auto
|| dk
== dk_thread
) ? ck_bad
: ck_ok
;
5114 /* Return true if T denotes a potentially constant expression. Issue
5115 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
5116 an lvalue-rvalue conversion is implied. If NOW is true, we want to
5117 consider the expression in the current context, independent of constexpr
5120 C++0x [expr.const] used to say
5122 6 An expression is a potential constant expression if it is
5123 a constant expression where all occurrences of function
5124 parameters are replaced by arbitrary constant expressions
5125 of the appropriate type.
5127 2 A conditional expression is a constant expression unless it
5128 involves one of the following as a potentially evaluated
5129 subexpression (3.2), but subexpressions of logical AND (5.14),
5130 logical OR (5.15), and conditional (5.16) operations that are
5131 not evaluated are not considered. */
5134 potential_constant_expression_1 (tree t
, bool want_rval
, bool strict
, bool now
,
5135 tsubst_flags_t flags
)
5137 #define RECUR(T,RV) \
5138 potential_constant_expression_1 ((T), (RV), strict, now, flags)
5140 enum { any
= false, rval
= true };
5144 if (t
== error_mark_node
)
5148 location_t loc
= EXPR_LOC_OR_LOC (t
, input_location
);
5149 if (TREE_THIS_VOLATILE (t
) && !DECL_P (t
))
5151 if (flags
& tf_error
)
5152 error_at (loc
, "expression %qE has side-effects", t
);
5155 if (CONSTANT_CLASS_P (t
))
5157 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_TYPED
)
5158 && TREE_TYPE (t
) == error_mark_node
)
5161 switch (TREE_CODE (t
))
5167 case TEMPLATE_ID_EXPR
:
5170 case CASE_LABEL_EXPR
:
5176 case TEMPLATE_PARM_INDEX
:
5178 case IDENTIFIER_NODE
:
5179 case USERDEF_LITERAL
:
5180 /* We can see a FIELD_DECL in a pointer-to-member expression. */
5185 case PLACEHOLDER_EXPR
:
5195 if (flags
& tf_error
)
5196 error ("%qE is not a constant expression", t
);
5201 case AGGR_INIT_EXPR
:
5203 /* -- an invocation of a function other than a constexpr function
5204 or a constexpr constructor. */
5206 tree fun
= get_function_named_in_call (t
);
5207 const int nargs
= call_expr_nargs (t
);
5210 if (fun
== NULL_TREE
)
5212 /* Reset to allow the function to continue past the end
5213 of the block below. Otherwise return early. */
5216 if (TREE_CODE (t
) == CALL_EXPR
5217 && CALL_EXPR_FN (t
) == NULL_TREE
)
5218 switch (CALL_EXPR_IFN (t
))
5220 /* These should be ignored, they are optimized away from
5221 constexpr functions. */
5222 case IFN_UBSAN_NULL
:
5223 case IFN_UBSAN_BOUNDS
:
5224 case IFN_UBSAN_VPTR
:
5225 case IFN_FALLTHROUGH
:
5228 case IFN_ADD_OVERFLOW
:
5229 case IFN_SUB_OVERFLOW
:
5230 case IFN_MUL_OVERFLOW
:
5240 /* fold_call_expr can't do anything with IFN calls. */
5241 if (flags
& tf_error
)
5242 error_at (loc
, "call to internal function %qE", t
);
5247 if (fun
&& is_overloaded_fn (fun
))
5249 if (TREE_CODE (fun
) == FUNCTION_DECL
)
5251 if (builtin_valid_in_constant_expr_p (fun
))
5253 if (!DECL_DECLARED_CONSTEXPR_P (fun
)
5254 /* Allow any built-in function; if the expansion
5255 isn't constant, we'll deal with that then. */
5256 && !is_builtin_fn (fun
))
5258 if (flags
& tf_error
)
5260 error_at (loc
, "call to non-%<constexpr%> function %qD",
5262 explain_invalid_constexpr_fn (fun
);
5266 /* A call to a non-static member function takes the address
5267 of the object as the first argument. But in a constant
5268 expression the address will be folded away, so look
5270 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun
)
5271 && !DECL_CONSTRUCTOR_P (fun
))
5273 tree x
= get_nth_callarg (t
, 0);
5274 if (is_this_parameter (x
))
5276 /* Don't require an immediately constant value, as
5277 constexpr substitution might not use the value. */
5278 bool sub_now
= false;
5279 if (!potential_constant_expression_1 (x
, rval
, strict
,
5287 if (!RECUR (fun
, true))
5289 fun
= get_first_fn (fun
);
5291 /* Skip initial arguments to base constructors. */
5292 if (DECL_BASE_CONSTRUCTOR_P (fun
))
5293 i
= num_artificial_parms_for (fun
);
5294 fun
= DECL_ORIGIN (fun
);
5298 if (RECUR (fun
, rval
))
5299 /* Might end up being a constant function pointer. */;
5303 for (; i
< nargs
; ++i
)
5305 tree x
= get_nth_callarg (t
, i
);
5306 /* In a template, reference arguments haven't been converted to
5307 REFERENCE_TYPE and we might not even know if the parameter
5308 is a reference, so accept lvalue constants too. */
5309 bool rv
= processing_template_decl
? any
: rval
;
5310 /* Don't require an immediately constant value, as constexpr
5311 substitution might not use the value of the argument. */
5312 bool sub_now
= false;
5313 if (!potential_constant_expression_1 (x
, rv
, strict
,
5320 case NON_LVALUE_EXPR
:
5321 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
5322 -- an lvalue of integral type that refers to a non-volatile
5323 const variable or static data member initialized with
5324 constant expressions, or
5326 -- an lvalue of literal type that refers to non-volatile
5327 object defined with constexpr, or that refers to a
5328 sub-object of such an object; */
5329 return RECUR (TREE_OPERAND (t
, 0), rval
);
5332 if (DECL_HAS_VALUE_EXPR_P (t
))
5334 if (now
&& is_normal_capture_proxy (t
))
5336 /* -- in a lambda-expression, a reference to this or to a
5337 variable with automatic storage duration defined outside that
5338 lambda-expression, where the reference would be an
5340 if (flags
& tf_error
)
5342 tree cap
= DECL_CAPTURED_VARIABLE (t
);
5343 error ("lambda capture of %qE is not a constant expression",
5345 if (!want_rval
&& decl_constant_var_p (cap
))
5346 inform (input_location
, "because it is used as a glvalue");
5350 return RECUR (DECL_VALUE_EXPR (t
), rval
);
5353 && !var_in_maybe_constexpr_fn (t
)
5354 && !type_dependent_expression_p (t
)
5355 && !decl_maybe_constant_var_p (t
)
5357 || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t
))
5358 || (DECL_INITIAL (t
)
5359 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t
)))
5360 && COMPLETE_TYPE_P (TREE_TYPE (t
))
5361 && !is_really_empty_class (TREE_TYPE (t
)))
5363 if (flags
& tf_error
)
5364 non_const_var_error (t
);
5371 case VIEW_CONVERT_EXPR
:
5372 /* -- a reinterpret_cast. FIXME not implemented, and this rule
5373 may change to something more specific to type-punning (DR 1312). */
5375 tree from
= TREE_OPERAND (t
, 0);
5376 if (POINTER_TYPE_P (TREE_TYPE (t
))
5377 && TREE_CODE (from
) == INTEGER_CST
5378 && !integer_zerop (from
))
5380 if (flags
& tf_error
)
5381 error_at (loc
, "reinterpret_cast from integer to pointer");
5384 return (RECUR (from
, TREE_CODE (t
) != VIEW_CONVERT_EXPR
));
5387 case ADDRESSOF_EXPR
:
5388 /* This is like ADDR_EXPR, except it won't form pointer-to-member. */
5389 t
= TREE_OPERAND (t
, 0);
5390 goto handle_addr_expr
;
5393 /* -- a unary operator & that is applied to an lvalue that
5394 designates an object with thread or automatic storage
5396 t
= TREE_OPERAND (t
, 0);
5398 if (TREE_CODE (t
) == OFFSET_REF
&& PTRMEM_OK_P (t
))
5399 /* A pointer-to-member constant. */
5404 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
5405 any checking here, as we might dereference the pointer later. If
5406 we remove this code, also remove check_automatic_or_tls. */
5407 i
= check_automatic_or_tls (t
);
5412 if (flags
& tf_error
)
5413 error ("address-of an object %qE with thread local or "
5414 "automatic storage is not a constant expression", t
);
5418 return RECUR (t
, any
);
5426 /* -- a class member access unless its postfix-expression is
5427 of literal type or of pointer to literal type. */
5428 /* This test would be redundant, as it follows from the
5429 postfix-expression being a potential constant expression. */
5430 if (type_unknown_p (t
))
5432 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
5434 case EXPR_PACK_EXPANSION
:
5435 return RECUR (PACK_EXPANSION_PATTERN (t
), want_rval
);
5439 tree x
= TREE_OPERAND (t
, 0);
5441 if (is_this_parameter (x
) && !is_capture_proxy (x
))
5443 if (!var_in_maybe_constexpr_fn (x
))
5445 if (flags
& tf_error
)
5446 error_at (loc
, "use of %<this%> in a constant expression");
5451 return RECUR (x
, rval
);
5454 case STATEMENT_LIST
:
5456 tree_stmt_iterator i
;
5457 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
5459 if (!RECUR (tsi_stmt (i
), any
))
5467 if (cxx_dialect
< cxx14
)
5469 if (!RECUR (TREE_OPERAND (t
, 0), any
))
5471 if (!RECUR (TREE_OPERAND (t
, 1), rval
))
5476 if (cxx_dialect
< cxx14
)
5478 if (!RECUR (TREE_OPERAND (t
, 0), rval
))
5480 if (!RECUR (TREE_OPERAND (t
, 2), rval
))
5485 if (!RECUR (DO_COND (t
), rval
))
5487 if (!RECUR (DO_BODY (t
), any
))
5492 if (!RECUR (FOR_INIT_STMT (t
), any
))
5494 if (!RECUR (FOR_COND (t
), rval
))
5496 if (!RECUR (FOR_EXPR (t
), any
))
5498 if (!RECUR (FOR_BODY (t
), any
))
5502 case RANGE_FOR_STMT
:
5503 if (!RECUR (RANGE_FOR_EXPR (t
), any
))
5505 if (!RECUR (RANGE_FOR_BODY (t
), any
))
5510 if (!RECUR (WHILE_COND (t
), rval
))
5512 if (!RECUR (WHILE_BODY (t
), any
))
5517 if (!RECUR (SWITCH_STMT_COND (t
), rval
))
5519 /* FIXME we don't check SWITCH_STMT_BODY currently, because even
5520 unreachable labels would be checked. */
5524 return RECUR (STMT_EXPR_STMT (t
), rval
);
5527 if (cxx_dialect
>= cxx17
)
5528 /* In C++17 lambdas can be constexpr, don't give up yet. */
5530 else if (flags
& tf_error
)
5531 error_at (loc
, "lambda-expression is not a constant expression "
5535 case DYNAMIC_CAST_EXPR
:
5536 case PSEUDO_DTOR_EXPR
:
5540 case VEC_DELETE_EXPR
:
5545 case OMP_DISTRIBUTE
:
5548 case OMP_TARGET_DATA
:
5557 case OMP_TARGET_UPDATE
:
5558 case OMP_TARGET_ENTER_DATA
:
5559 case OMP_TARGET_EXIT_DATA
:
5561 case OMP_ATOMIC_READ
:
5562 case OMP_ATOMIC_CAPTURE_OLD
:
5563 case OMP_ATOMIC_CAPTURE_NEW
:
5567 case OACC_HOST_DATA
:
5571 case OACC_ENTER_DATA
:
5572 case OACC_EXIT_DATA
:
5574 /* GCC internal stuff. */
5577 case TRANSACTION_EXPR
:
5579 case AT_ENCODE_EXPR
:
5581 if (flags
& tf_error
)
5582 error_at (loc
, "expression %qE is not a constant expression", t
);
5586 /* -- a typeid expression whose operand is of polymorphic
5589 tree e
= TREE_OPERAND (t
, 0);
5590 if (!TYPE_P (e
) && !type_dependent_expression_p (e
)
5591 && TYPE_POLYMORPHIC_P (TREE_TYPE (e
)))
5593 if (flags
& tf_error
)
5594 error_at (loc
, "typeid-expression is not a constant expression "
5595 "because %qE is of polymorphic type", e
);
5601 case POINTER_DIFF_EXPR
:
5615 case PREINCREMENT_EXPR
:
5616 case POSTINCREMENT_EXPR
:
5617 case PREDECREMENT_EXPR
:
5618 case POSTDECREMENT_EXPR
:
5619 if (cxx_dialect
< cxx14
)
5625 if (TYPE_P (TREE_OPERAND (t
, 0)))
5631 case FIX_TRUNC_EXPR
:
5635 case TRUTH_NOT_EXPR
:
5636 case FIXED_CONVERT_EXPR
:
5637 case UNARY_PLUS_EXPR
:
5638 case UNARY_LEFT_FOLD_EXPR
:
5639 case UNARY_RIGHT_FOLD_EXPR
:
5641 return RECUR (TREE_OPERAND (t
, 0), rval
);
5644 case CONST_CAST_EXPR
:
5645 case STATIC_CAST_EXPR
:
5646 case REINTERPRET_CAST_EXPR
:
5647 case IMPLICIT_CONV_EXPR
:
5648 if (cxx_dialect
< cxx11
5649 && !dependent_type_p (TREE_TYPE (t
))
5650 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
)))
5651 /* In C++98, a conversion to non-integral type can't be part of a
5652 constant expression. */
5654 if (flags
& tf_error
)
5656 "cast to non-integral type %qT in a constant expression",
5661 return (RECUR (TREE_OPERAND (t
, 0),
5662 TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
));
5665 return RECUR (BIND_EXPR_BODY (t
), want_rval
);
5667 case CLEANUP_POINT_EXPR
:
5668 case MUST_NOT_THROW_EXPR
:
5669 case TRY_CATCH_EXPR
:
5674 case NON_DEPENDENT_EXPR
:
5675 /* For convenience. */
5679 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
5682 tmp
= DECL_EXPR_DECL (t
);
5683 if (VAR_P (tmp
) && !DECL_ARTIFICIAL (tmp
))
5685 if (TREE_STATIC (tmp
))
5687 if (flags
& tf_error
)
5688 error_at (DECL_SOURCE_LOCATION (tmp
), "%qD declared "
5689 "%<static%> in %<constexpr%> context", tmp
);
5692 else if (CP_DECL_THREAD_LOCAL_P (tmp
))
5694 if (flags
& tf_error
)
5695 error_at (DECL_SOURCE_LOCATION (tmp
), "%qD declared "
5696 "%<thread_local%> in %<constexpr%> context", tmp
);
5699 else if (!DECL_NONTRIVIALLY_INITIALIZED_P (tmp
))
5701 if (flags
& tf_error
)
5702 error_at (DECL_SOURCE_LOCATION (tmp
), "uninitialized "
5703 "variable %qD in %<constexpr%> context", tmp
);
5707 return RECUR (tmp
, want_rval
);
5709 case TRY_FINALLY_EXPR
:
5710 return (RECUR (TREE_OPERAND (t
, 0), want_rval
)
5711 && RECUR (TREE_OPERAND (t
, 1), any
));
5714 return RECUR (TREE_OPERAND (t
, 1), want_rval
);
5717 if (!literal_type_p (TREE_TYPE (t
)))
5719 if (flags
& tf_error
)
5721 error_at (loc
, "temporary of non-literal type %qT in a "
5722 "constant expression", TREE_TYPE (t
));
5723 explain_non_literal_class (TREE_TYPE (t
));
5729 return RECUR (TREE_OPERAND (t
, 1), rval
);
5733 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (t
);
5734 constructor_elt
*ce
;
5735 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
5736 if (!RECUR (ce
->value
, want_rval
))
5743 gcc_assert (TREE_PURPOSE (t
) == NULL_TREE
5744 || DECL_P (TREE_PURPOSE (t
)));
5745 if (!RECUR (TREE_VALUE (t
), want_rval
))
5747 if (TREE_CHAIN (t
) == NULL_TREE
)
5749 return RECUR (TREE_CHAIN (t
), want_rval
);
5752 case TRUNC_DIV_EXPR
:
5754 case FLOOR_DIV_EXPR
:
5755 case ROUND_DIV_EXPR
:
5756 case TRUNC_MOD_EXPR
:
5758 case ROUND_MOD_EXPR
:
5760 tree denom
= TREE_OPERAND (t
, 1);
5761 if (!RECUR (denom
, rval
))
5763 /* We can't call cxx_eval_outermost_constant_expr on an expression
5764 that hasn't been through instantiate_non_dependent_expr yet. */
5765 if (!processing_template_decl
)
5766 denom
= cxx_eval_outermost_constant_expr (denom
, true);
5767 if (integer_zerop (denom
))
5769 if (flags
& tf_error
)
5770 error ("division by zero is not a constant expression");
5776 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
5782 /* check_return_expr sometimes wraps a TARGET_EXPR in a
5783 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
5784 introduced by build_call_a. */
5785 tree op0
= TREE_OPERAND (t
, 0);
5786 tree op1
= TREE_OPERAND (t
, 1);
5788 if ((TREE_CODE (op0
) == TARGET_EXPR
&& op1
== TARGET_EXPR_SLOT (op0
))
5789 || TREE_CODE (op1
) == EMPTY_CLASS_EXPR
)
5790 return RECUR (op0
, want_rval
);
5795 /* If the first operand is the non-short-circuit constant, look at
5796 the second operand; otherwise we only care about the first one for
5798 case TRUTH_AND_EXPR
:
5799 case TRUTH_ANDIF_EXPR
:
5800 tmp
= boolean_true_node
;
5803 case TRUTH_ORIF_EXPR
:
5804 tmp
= boolean_false_node
;
5807 tree op
= TREE_OPERAND (t
, 0);
5808 if (!RECUR (op
, rval
))
5810 if (!processing_template_decl
)
5811 op
= cxx_eval_outermost_constant_expr (op
, true);
5812 if (tree_int_cst_equal (op
, tmp
))
5813 return RECUR (TREE_OPERAND (t
, 1), rval
);
5820 case POINTER_PLUS_EXPR
:
5822 case EXACT_DIV_EXPR
:
5832 case TRUTH_XOR_EXPR
:
5833 case UNORDERED_EXPR
:
5846 case ARRAY_RANGE_REF
:
5850 case BINARY_LEFT_FOLD_EXPR
:
5851 case BINARY_RIGHT_FOLD_EXPR
:
5853 for (i
= 0; i
< 2; ++i
)
5854 if (!RECUR (TREE_OPERAND (t
, i
), want_rval
))
5860 for (i
= 0; i
< 3; ++i
)
5861 if (!RECUR (TREE_OPERAND (t
, i
), true))
5866 if (COND_EXPR_IS_VEC_DELETE (t
))
5868 if (flags
& tf_error
)
5869 error_at (loc
, "%<delete[]%> is not a constant expression");
5875 /* If the condition is a known constant, we know which of the legs we
5876 care about; otherwise we only require that the condition and
5877 either of the legs be potentially constant. */
5878 tmp
= TREE_OPERAND (t
, 0);
5879 if (!RECUR (tmp
, rval
))
5881 if (!processing_template_decl
)
5882 tmp
= cxx_eval_outermost_constant_expr (tmp
, true);
5883 if (integer_zerop (tmp
))
5884 return RECUR (TREE_OPERAND (t
, 2), want_rval
);
5885 else if (TREE_CODE (tmp
) == INTEGER_CST
)
5886 return RECUR (TREE_OPERAND (t
, 1), want_rval
);
5887 for (i
= 1; i
< 3; ++i
)
5888 if (potential_constant_expression_1 (TREE_OPERAND (t
, i
),
5889 want_rval
, strict
, now
, tf_none
))
5891 if (flags
& tf_error
)
5892 error_at (loc
, "expression %qE is not a constant expression", t
);
5896 if (VEC_INIT_EXPR_IS_CONSTEXPR (t
))
5898 if (flags
& tf_error
)
5900 error_at (loc
, "non-constant array initialization");
5901 diagnose_non_constexpr_vec_init (t
);
5907 /* We can see these in statement-expressions. */
5911 case EMPTY_CLASS_EXPR
:
5917 tree
*target
= &TREE_OPERAND (t
, 0);
5918 /* Gotos representing break and continue are OK. */
5919 if (breaks (target
) || continues (target
))
5921 if (flags
& tf_error
)
5922 error_at (loc
, "%<goto%> is not a constant expression");
5927 gcc_assert (tree_to_uhwi (TREE_OPERAND (t
, 1)) == annot_expr_ivdep_kind
);
5928 return RECUR (TREE_OPERAND (t
, 0), rval
);
5931 if (objc_is_property_ref (t
))
5934 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t
)));
5941 /* The main entry point to the above. */
5944 potential_constant_expression (tree t
)
5946 return potential_constant_expression_1 (t
, false, true, false, tf_none
);
5949 /* As above, but require a constant rvalue. */
5952 potential_rvalue_constant_expression (tree t
)
5954 return potential_constant_expression_1 (t
, true, true, false, tf_none
);
5957 /* Like above, but complain about non-constant expressions. */
5960 require_potential_constant_expression (tree t
)
5962 return potential_constant_expression_1 (t
, false, true, false, tf_warning_or_error
);
5965 /* Cross product of the above. */
5968 require_potential_rvalue_constant_expression (tree t
)
5970 return potential_constant_expression_1 (t
, true, true, false, tf_warning_or_error
);
5973 /* Like potential_constant_expression, but don't consider possible constexpr
5974 substitution of the current function. That is, PARM_DECL qualifies under
5975 potential_constant_expression, but not here.
5977 This is basically what you can check when any actual constant values might
5978 be value-dependent. */
5981 is_constant_expression (tree t
)
5983 return potential_constant_expression_1 (t
, false, true, true, tf_none
);
5986 /* Like above, but complain about non-constant expressions. */
5989 require_constant_expression (tree t
)
5991 return potential_constant_expression_1 (t
, false, true, true,
5992 tf_warning_or_error
);
5995 /* Like is_constant_expression, but allow const variables that are not allowed
5996 under constexpr rules. */
5999 is_static_init_expression (tree t
)
6001 return potential_constant_expression_1 (t
, false, false, true, tf_none
);
6004 /* Returns true if T is a potential constant expression that is not
6005 instantiation-dependent, and therefore a candidate for constant folding even
6009 is_nondependent_constant_expression (tree t
)
6011 return (!type_unknown_p (t
)
6012 && !BRACE_ENCLOSED_INITIALIZER_P (t
)
6013 && is_constant_expression (t
)
6014 && !instantiation_dependent_expression_p (t
));
6017 /* Returns true if T is a potential static initializer expression that is not
6018 instantiation-dependent. */
6021 is_nondependent_static_init_expression (tree t
)
6023 return (!type_unknown_p (t
)
6024 && !BRACE_ENCLOSED_INITIALIZER_P (t
)
6025 && is_static_init_expression (t
)
6026 && !instantiation_dependent_expression_p (t
));
6029 /* Finalize constexpr processing after parsing. */
6032 fini_constexpr (void)
6034 /* The contexpr call and fundef copies tables are no longer needed. */
6035 constexpr_call_table
= NULL
;
6036 fundef_copies_table
= NULL
;
6039 #include "gt-cp-constexpr.h"