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-2016 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"
35 static bool verify_constant (tree
, bool, bool *, bool *);
36 #define VERIFY_CONSTANT(X) \
38 if (verify_constant ((X), ctx->quiet, non_constant_p, overflow_p)) \
42 /* Returns true iff FUN is an instantiation of a constexpr function
43 template or a defaulted constexpr function. */
46 is_instantiation_of_constexpr (tree fun
)
48 return ((DECL_TEMPLOID_INSTANTIATION (fun
)
49 && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun
)))
50 || (DECL_DEFAULTED_FN (fun
)
51 && DECL_DECLARED_CONSTEXPR_P (fun
)));
54 /* Return true if T is a literal type. */
57 literal_type_p (tree t
)
61 || TREE_CODE (t
) == REFERENCE_TYPE
62 || (VOID_TYPE_P (t
) && cxx_dialect
>= cxx14
))
66 t
= complete_type (t
);
67 gcc_assert (COMPLETE_TYPE_P (t
) || errorcount
);
68 return CLASSTYPE_LITERAL_P (t
);
70 if (TREE_CODE (t
) == ARRAY_TYPE
)
71 return literal_type_p (strip_array_types (t
));
75 /* If DECL is a variable declared `constexpr', require its type
76 be literal. Return the DECL if OK, otherwise NULL. */
79 ensure_literal_type_for_constexpr_object (tree decl
)
81 tree type
= TREE_TYPE (decl
);
83 && (DECL_DECLARED_CONSTEXPR_P (decl
)
84 || var_in_constexpr_fn (decl
))
85 && !processing_template_decl
)
87 tree stype
= strip_array_types (type
);
88 if (CLASS_TYPE_P (stype
) && !COMPLETE_TYPE_P (complete_type (stype
)))
89 /* Don't complain here, we'll complain about incompleteness
90 when we try to initialize the variable. */;
91 else if (!literal_type_p (type
))
93 if (DECL_DECLARED_CONSTEXPR_P (decl
))
95 error ("the type %qT of constexpr variable %qD is not literal",
97 explain_non_literal_class (type
);
101 if (!DECL_TEMPLATE_INSTANTIATION (current_function_decl
))
103 error ("variable %qD of non-literal type %qT in %<constexpr%> "
104 "function", decl
, type
);
105 explain_non_literal_class (type
);
107 cp_function_chain
->invalid_constexpr
= true;
115 /* Representation of entries in the constexpr function definition table. */
117 struct GTY((for_user
)) constexpr_fundef
{
122 struct constexpr_fundef_hasher
: ggc_ptr_hash
<constexpr_fundef
>
124 static hashval_t
hash (constexpr_fundef
*);
125 static bool equal (constexpr_fundef
*, constexpr_fundef
*);
128 /* This table holds all constexpr function definitions seen in
129 the current translation unit. */
131 static GTY (()) hash_table
<constexpr_fundef_hasher
> *constexpr_fundef_table
;
133 /* Utility function used for managing the constexpr function table.
134 Return true if the entries pointed to by P and Q are for the
135 same constexpr function. */
138 constexpr_fundef_hasher::equal (constexpr_fundef
*lhs
, constexpr_fundef
*rhs
)
140 return lhs
->decl
== rhs
->decl
;
143 /* Utility function used for managing the constexpr function table.
144 Return a hash value for the entry pointed to by Q. */
147 constexpr_fundef_hasher::hash (constexpr_fundef
*fundef
)
149 return DECL_UID (fundef
->decl
);
152 /* Return a previously saved definition of function FUN. */
154 static constexpr_fundef
*
155 retrieve_constexpr_fundef (tree fun
)
157 constexpr_fundef fundef
= { NULL
, NULL
};
158 if (constexpr_fundef_table
== NULL
)
162 return constexpr_fundef_table
->find (&fundef
);
165 /* Check whether the parameter and return types of FUN are valid for a
166 constexpr function, and complain if COMPLAIN. */
169 is_valid_constexpr_fn (tree fun
, bool complain
)
173 if (DECL_INHERITED_CTOR_BASE (fun
)
174 && TREE_CODE (fun
) == TEMPLATE_DECL
)
178 error ("inherited constructor %qD is not constexpr",
179 get_inherited_ctor (fun
));
183 for (tree parm
= FUNCTION_FIRST_USER_PARM (fun
);
184 parm
!= NULL_TREE
; parm
= TREE_CHAIN (parm
))
185 if (!literal_type_p (TREE_TYPE (parm
)))
190 error ("invalid type for parameter %d of constexpr "
191 "function %q+#D", DECL_PARM_INDEX (parm
), fun
);
192 explain_non_literal_class (TREE_TYPE (parm
));
197 if (!DECL_CONSTRUCTOR_P (fun
))
199 tree rettype
= TREE_TYPE (TREE_TYPE (fun
));
200 if (!literal_type_p (rettype
))
205 error ("invalid return type %qT of constexpr function %q+D",
207 explain_non_literal_class (rettype
);
211 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun
)
212 && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun
)))
217 error ("enclosing class of constexpr non-static member "
218 "function %q+#D is not a literal type", fun
);
219 explain_non_literal_class (DECL_CONTEXT (fun
));
223 else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun
)))
227 error ("%q#T has virtual base classes", DECL_CONTEXT (fun
));
233 /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF
234 for a member of an anonymous aggregate, INIT is the initializer for that
235 member, and VEC_OUTER is the vector of constructor elements for the class
236 whose constructor we are processing. Add the initializer to the vector
237 and return true to indicate success. */
240 build_anon_member_initialization (tree member
, tree init
,
241 vec
<constructor_elt
, va_gc
> **vec_outer
)
243 /* MEMBER presents the relevant fields from the inside out, but we need
244 to build up the initializer from the outside in so that we can reuse
245 previously built CONSTRUCTORs if this is, say, the second field in an
246 anonymous struct. So we use a vec as a stack. */
247 auto_vec
<tree
, 2> fields
;
250 fields
.safe_push (TREE_OPERAND (member
, 1));
251 member
= TREE_OPERAND (member
, 0);
253 while (ANON_AGGR_TYPE_P (TREE_TYPE (member
))
254 && TREE_CODE (member
) == COMPONENT_REF
);
256 /* VEC has the constructor elements vector for the context of FIELD.
257 If FIELD is an anonymous aggregate, we will push inside it. */
258 vec
<constructor_elt
, va_gc
> **vec
= vec_outer
;
260 while (field
= fields
.pop(),
261 ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
264 /* If there is already an outer constructor entry for the anonymous
265 aggregate FIELD, use it; otherwise, insert one. */
266 if (vec_safe_is_empty (*vec
)
267 || (*vec
)->last().index
!= field
)
269 ctor
= build_constructor (TREE_TYPE (field
), NULL
);
270 CONSTRUCTOR_APPEND_ELT (*vec
, field
, ctor
);
273 ctor
= (*vec
)->last().value
;
274 vec
= &CONSTRUCTOR_ELTS (ctor
);
277 /* Now we're at the innermost field, the one that isn't an anonymous
278 aggregate. Add its initializer to the CONSTRUCTOR and we're done. */
279 gcc_assert (fields
.is_empty());
280 CONSTRUCTOR_APPEND_ELT (*vec
, field
, init
);
285 /* Subroutine of build_constexpr_constructor_member_initializers.
286 The expression tree T represents a data member initialization
287 in a (constexpr) constructor definition. Build a pairing of
288 the data member with its initializer, and prepend that pair
289 to the existing initialization pair INITS. */
292 build_data_member_initialization (tree t
, vec
<constructor_elt
, va_gc
> **vec
)
295 if (TREE_CODE (t
) == CLEANUP_POINT_EXPR
)
296 t
= TREE_OPERAND (t
, 0);
297 if (TREE_CODE (t
) == EXPR_STMT
)
298 t
= TREE_OPERAND (t
, 0);
299 if (t
== error_mark_node
)
301 if (TREE_CODE (t
) == STATEMENT_LIST
)
303 tree_stmt_iterator i
;
304 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
306 if (! build_data_member_initialization (tsi_stmt (i
), vec
))
311 if (TREE_CODE (t
) == CLEANUP_STMT
)
313 /* We can't see a CLEANUP_STMT in a constructor for a literal class,
314 but we can in a constexpr constructor for a non-literal class. Just
315 ignore it; either all the initialization will be constant, in which
316 case the cleanup can't run, or it can't be constexpr.
317 Still recurse into CLEANUP_BODY. */
318 return build_data_member_initialization (CLEANUP_BODY (t
), vec
);
320 if (TREE_CODE (t
) == CONVERT_EXPR
)
321 t
= TREE_OPERAND (t
, 0);
322 if (TREE_CODE (t
) == INIT_EXPR
323 /* vptr initialization shows up as a MODIFY_EXPR. In C++14 we only
324 use what this function builds for cx_check_missing_mem_inits, and
325 assignment in the ctor body doesn't count. */
326 || (cxx_dialect
< cxx14
&& TREE_CODE (t
) == MODIFY_EXPR
))
328 member
= TREE_OPERAND (t
, 0);
329 init
= break_out_target_exprs (TREE_OPERAND (t
, 1));
331 else if (TREE_CODE (t
) == CALL_EXPR
)
333 tree fn
= get_callee_fndecl (t
);
334 if (!fn
|| !DECL_CONSTRUCTOR_P (fn
))
335 /* We're only interested in calls to subobject constructors. */
337 member
= CALL_EXPR_ARG (t
, 0);
338 /* We don't use build_cplus_new here because it complains about
339 abstract bases. Leaving the call unwrapped means that it has the
340 wrong type, but cxx_eval_constant_expression doesn't care. */
341 init
= break_out_target_exprs (t
);
343 else if (TREE_CODE (t
) == BIND_EXPR
)
344 return build_data_member_initialization (BIND_EXPR_BODY (t
), vec
);
346 /* Don't add anything else to the CONSTRUCTOR. */
348 if (INDIRECT_REF_P (member
))
349 member
= TREE_OPERAND (member
, 0);
350 if (TREE_CODE (member
) == NOP_EXPR
)
354 if (TREE_CODE (op
) == ADDR_EXPR
)
356 gcc_assert (same_type_ignoring_top_level_qualifiers_p
357 (TREE_TYPE (TREE_TYPE (op
)),
358 TREE_TYPE (TREE_TYPE (member
))));
359 /* Initializing a cv-qualified member; we need to look through
363 else if (op
== current_class_ptr
364 && (same_type_ignoring_top_level_qualifiers_p
365 (TREE_TYPE (TREE_TYPE (member
)),
366 current_class_type
)))
367 /* Delegating constructor. */
371 /* This is an initializer for an empty base; keep it for now so
372 we can check it in cxx_eval_bare_aggregate. */
373 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member
))));
376 if (TREE_CODE (member
) == ADDR_EXPR
)
377 member
= TREE_OPERAND (member
, 0);
378 if (TREE_CODE (member
) == COMPONENT_REF
)
380 tree aggr
= TREE_OPERAND (member
, 0);
381 if (TREE_CODE (aggr
) != COMPONENT_REF
)
382 /* Normal member initialization. */
383 member
= TREE_OPERAND (member
, 1);
384 else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr
)))
385 /* Initializing a member of an anonymous union. */
386 return build_anon_member_initialization (member
, init
, vec
);
388 /* We're initializing a vtable pointer in a base. Leave it as
389 COMPONENT_REF so we remember the path to get to the vfield. */
390 gcc_assert (TREE_TYPE (member
) == vtbl_ptr_type_node
);
393 CONSTRUCTOR_APPEND_ELT (*vec
, member
, init
);
397 /* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval.
398 In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a
399 BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */
402 check_constexpr_bind_expr_vars (tree t
)
404 gcc_assert (TREE_CODE (t
) == BIND_EXPR
);
406 for (tree var
= BIND_EXPR_VARS (t
); var
; var
= DECL_CHAIN (var
))
407 if (TREE_CODE (var
) == TYPE_DECL
408 && DECL_IMPLICIT_TYPEDEF_P (var
)
409 && !LAMBDA_TYPE_P (TREE_TYPE (var
)))
414 /* Subroutine of check_constexpr_ctor_body. */
417 check_constexpr_ctor_body_1 (tree last
, tree list
)
419 switch (TREE_CODE (list
))
422 if (TREE_CODE (DECL_EXPR_DECL (list
)) == USING_DECL
)
426 case CLEANUP_POINT_EXPR
:
427 return check_constexpr_ctor_body (last
, TREE_OPERAND (list
, 0),
431 if (!check_constexpr_bind_expr_vars (list
)
432 || !check_constexpr_ctor_body (last
, BIND_EXPR_BODY (list
),
446 /* Make sure that there are no statements after LAST in the constructor
447 body represented by LIST. */
450 check_constexpr_ctor_body (tree last
, tree list
, bool complain
)
452 /* C++14 doesn't require a constexpr ctor to have an empty body. */
453 if (cxx_dialect
>= cxx14
)
457 if (TREE_CODE (list
) == STATEMENT_LIST
)
459 tree_stmt_iterator i
= tsi_last (list
);
460 for (; !tsi_end_p (i
); tsi_prev (&i
))
462 tree t
= tsi_stmt (i
);
465 if (!check_constexpr_ctor_body_1 (last
, t
))
472 else if (list
!= last
473 && !check_constexpr_ctor_body_1 (last
, list
))
478 error ("constexpr constructor does not have empty body");
479 DECL_DECLARED_CONSTEXPR_P (current_function_decl
) = false;
484 /* V is a vector of constructor elements built up for the base and member
485 initializers of a constructor for TYPE. They need to be in increasing
486 offset order, which they might not be yet if TYPE has a primary base
487 which is not first in the base-clause or a vptr and at least one base
488 all of which are non-primary. */
490 static vec
<constructor_elt
, va_gc
> *
491 sort_constexpr_mem_initializers (tree type
, vec
<constructor_elt
, va_gc
> *v
)
493 tree pri
= CLASSTYPE_PRIMARY_BINFO (type
);
499 field_type
= BINFO_TYPE (pri
);
500 else if (TYPE_CONTAINS_VPTR_P (type
))
501 field_type
= vtbl_ptr_type_node
;
505 /* Find the element for the primary base or vptr and move it to the
506 beginning of the vec. */
507 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
508 if (TREE_TYPE (ce
->index
) == field_type
)
511 if (i
> 0 && i
< vec_safe_length (v
))
513 vec
<constructor_elt
, va_gc
> &vref
= *v
;
514 constructor_elt elt
= vref
[i
];
523 /* Build compile-time evalable representations of member-initializer list
524 for a constexpr constructor. */
527 build_constexpr_constructor_member_initializers (tree type
, tree body
)
529 vec
<constructor_elt
, va_gc
> *vec
= NULL
;
532 switch (TREE_CODE (body
))
534 case MUST_NOT_THROW_EXPR
:
536 body
= TREE_OPERAND (body
, 0);
540 for (tree_stmt_iterator i
= tsi_start (body
);
541 !tsi_end_p (i
); tsi_next (&i
))
544 if (TREE_CODE (body
) == BIND_EXPR
)
550 body
= BIND_EXPR_BODY (body
);
557 if (TREE_CODE (body
) == CLEANUP_POINT_EXPR
)
559 body
= TREE_OPERAND (body
, 0);
560 if (TREE_CODE (body
) == EXPR_STMT
)
561 body
= TREE_OPERAND (body
, 0);
562 if (TREE_CODE (body
) == INIT_EXPR
563 && (same_type_ignoring_top_level_qualifiers_p
564 (TREE_TYPE (TREE_OPERAND (body
, 0)),
565 current_class_type
)))
568 return TREE_OPERAND (body
, 1);
570 ok
= build_data_member_initialization (body
, &vec
);
572 else if (TREE_CODE (body
) == STATEMENT_LIST
)
574 tree_stmt_iterator i
;
575 for (i
= tsi_start (body
); !tsi_end_p (i
); tsi_next (&i
))
577 ok
= build_data_member_initialization (tsi_stmt (i
), &vec
);
582 else if (TREE_CODE (body
) == TRY_BLOCK
)
584 error ("body of %<constexpr%> constructor cannot be "
585 "a function-try-block");
586 return error_mark_node
;
588 else if (EXPR_P (body
))
589 ok
= build_data_member_initialization (body
, &vec
);
591 gcc_assert (errorcount
> 0);
594 if (vec_safe_length (vec
) > 0)
596 /* In a delegating constructor, return the target. */
597 constructor_elt
*ce
= &(*vec
)[0];
598 if (ce
->index
== current_class_ptr
)
605 vec
= sort_constexpr_mem_initializers (type
, vec
);
606 return build_constructor (type
, vec
);
609 return error_mark_node
;
612 /* Subroutine of register_constexpr_fundef. BODY is the body of a function
613 declared to be constexpr, or a sub-statement thereof. Returns the
614 return value if suitable, error_mark_node for a statement not allowed in
615 a constexpr function, or NULL_TREE if no return value was found. */
618 constexpr_fn_retval (tree body
)
620 switch (TREE_CODE (body
))
624 tree_stmt_iterator i
;
625 tree expr
= NULL_TREE
;
626 for (i
= tsi_start (body
); !tsi_end_p (i
); tsi_next (&i
))
628 tree s
= constexpr_fn_retval (tsi_stmt (i
));
629 if (s
== error_mark_node
)
630 return error_mark_node
;
631 else if (s
== NULL_TREE
)
632 /* Keep iterating. */;
634 /* Multiple return statements. */
635 return error_mark_node
;
643 return break_out_target_exprs (TREE_OPERAND (body
, 0));
647 tree decl
= DECL_EXPR_DECL (body
);
648 if (TREE_CODE (decl
) == USING_DECL
649 /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */
650 || DECL_ARTIFICIAL (decl
))
652 return error_mark_node
;
655 case CLEANUP_POINT_EXPR
:
656 return constexpr_fn_retval (TREE_OPERAND (body
, 0));
659 if (!check_constexpr_bind_expr_vars (body
))
660 return error_mark_node
;
661 return constexpr_fn_retval (BIND_EXPR_BODY (body
));
667 return error_mark_node
;
671 /* Subroutine of register_constexpr_fundef. BODY is the DECL_SAVED_TREE of
672 FUN; do the necessary transformations to turn it into a single expression
673 that we can store in the hash table. */
676 massage_constexpr_body (tree fun
, tree body
)
678 if (DECL_CONSTRUCTOR_P (fun
))
679 body
= build_constexpr_constructor_member_initializers
680 (DECL_CONTEXT (fun
), body
);
681 else if (cxx_dialect
< cxx14
)
683 if (TREE_CODE (body
) == EH_SPEC_BLOCK
)
684 body
= EH_SPEC_STMTS (body
);
685 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
686 body
= TREE_OPERAND (body
, 0);
687 body
= constexpr_fn_retval (body
);
692 /* FUN is a constexpr constructor with massaged body BODY. Return true
693 if some bases/fields are uninitialized, and complain if COMPLAIN. */
696 cx_check_missing_mem_inits (tree fun
, tree body
, bool complain
)
703 if (TREE_CODE (body
) != CONSTRUCTOR
)
706 nelts
= CONSTRUCTOR_NELTS (body
);
707 ctype
= DECL_CONTEXT (fun
);
708 field
= TYPE_FIELDS (ctype
);
710 if (TREE_CODE (ctype
) == UNION_TYPE
)
712 if (nelts
== 0 && next_initializable_field (field
))
715 error ("%<constexpr%> constructor for union %qT must "
716 "initialize exactly one non-static data member", ctype
);
723 for (i
= 0; i
<= nelts
; ++i
)
730 index
= CONSTRUCTOR_ELT (body
, i
)->index
;
731 /* Skip base and vtable inits. */
732 if (TREE_CODE (index
) != FIELD_DECL
733 || DECL_ARTIFICIAL (index
))
736 for (; field
!= index
; field
= DECL_CHAIN (field
))
739 if (TREE_CODE (field
) != FIELD_DECL
740 || (DECL_C_BIT_FIELD (field
) && !DECL_NAME (field
))
741 || DECL_ARTIFICIAL (field
))
743 ftype
= strip_array_types (TREE_TYPE (field
));
744 if (type_has_constexpr_default_constructor (ftype
))
746 /* It's OK to skip a member with a trivial constexpr ctor.
747 A constexpr ctor that isn't trivial should have been
749 gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype
)
755 error ("member %qD must be initialized by mem-initializer "
756 "in %<constexpr%> constructor", field
);
757 inform (DECL_SOURCE_LOCATION (field
), "declared here");
760 if (field
== NULL_TREE
)
762 field
= DECL_CHAIN (field
);
768 /* We are processing the definition of the constexpr function FUN.
769 Check that its BODY fulfills the propriate requirements and
770 enter it in the constexpr function definition table.
771 For constructor BODY is actually the TREE_LIST of the
772 member-initializer list. */
775 register_constexpr_fundef (tree fun
, tree body
)
777 constexpr_fundef entry
;
778 constexpr_fundef
**slot
;
780 if (!is_valid_constexpr_fn (fun
, !DECL_GENERATED_P (fun
)))
783 tree massaged
= massage_constexpr_body (fun
, body
);
784 if (massaged
== NULL_TREE
|| massaged
== error_mark_node
)
786 if (!DECL_CONSTRUCTOR_P (fun
))
787 error ("body of constexpr function %qD not a return-statement", fun
);
791 if (!potential_rvalue_constant_expression (massaged
))
793 if (!DECL_GENERATED_P (fun
))
794 require_potential_rvalue_constant_expression (massaged
);
798 if (DECL_CONSTRUCTOR_P (fun
)
799 && cx_check_missing_mem_inits (fun
, massaged
, !DECL_GENERATED_P (fun
)))
802 /* Create the constexpr function table if necessary. */
803 if (constexpr_fundef_table
== NULL
)
804 constexpr_fundef_table
805 = hash_table
<constexpr_fundef_hasher
>::create_ggc (101);
809 slot
= constexpr_fundef_table
->find_slot (&entry
, INSERT
);
811 gcc_assert (*slot
== NULL
);
812 *slot
= ggc_alloc
<constexpr_fundef
> ();
818 /* FUN is a non-constexpr function called in a context that requires a
819 constant expression. If it comes from a constexpr template, explain why
820 the instantiation isn't constexpr. */
823 explain_invalid_constexpr_fn (tree fun
)
825 static hash_set
<tree
> *diagnosed
;
828 /* Only diagnose defaulted functions or instantiations. */
829 if (!DECL_DEFAULTED_FN (fun
)
830 && !is_instantiation_of_constexpr (fun
))
832 if (diagnosed
== NULL
)
833 diagnosed
= new hash_set
<tree
>;
834 if (diagnosed
->add (fun
))
835 /* Already explained. */
838 save_loc
= input_location
;
839 input_location
= DECL_SOURCE_LOCATION (fun
);
840 inform (input_location
,
841 "%qD is not usable as a constexpr function because:", fun
);
842 /* First check the declaration. */
843 if (is_valid_constexpr_fn (fun
, true))
845 /* Then if it's OK, the body. */
846 if (!DECL_DECLARED_CONSTEXPR_P (fun
))
847 explain_implicit_non_constexpr (fun
);
850 body
= massage_constexpr_body (fun
, DECL_SAVED_TREE (fun
));
851 require_potential_rvalue_constant_expression (body
);
852 if (DECL_CONSTRUCTOR_P (fun
))
853 cx_check_missing_mem_inits (fun
, body
, true);
856 input_location
= save_loc
;
859 /* Objects of this type represent calls to constexpr functions
860 along with the bindings of parameters to their arguments, for
861 the purpose of compile time evaluation. */
863 struct GTY((for_user
)) constexpr_call
{
864 /* Description of the constexpr function definition. */
865 constexpr_fundef
*fundef
;
866 /* Parameter bindings environment. A TREE_LIST where each TREE_PURPOSE
867 is a parameter _DECL and the TREE_VALUE is the value of the parameter.
868 Note: This arrangement is made to accommodate the use of
869 iterative_hash_template_arg (see pt.c). If you change this
870 representation, also change the hash calculation in
871 cxx_eval_call_expression. */
873 /* Result of the call.
874 NULL means the call is being evaluated.
875 error_mark_node means that the evaluation was erroneous;
876 otherwise, the actuall value of the call. */
878 /* The hash of this call; we remember it here to avoid having to
879 recalculate it when expanding the hash table. */
883 struct constexpr_call_hasher
: ggc_ptr_hash
<constexpr_call
>
885 static hashval_t
hash (constexpr_call
*);
886 static bool equal (constexpr_call
*, constexpr_call
*);
889 /* The constexpr expansion context. CALL is the current function
890 expansion, CTOR is the current aggregate initializer, OBJECT is the
891 object being initialized by CTOR, either a VAR_DECL or a _REF. VALUES
892 is a map of values of variables initialized within the expression. */
894 struct constexpr_ctx
{
895 /* The innermost call we're evaluating. */
896 constexpr_call
*call
;
897 /* Values for any temporaries or local variables within the
898 constant-expression. */
899 hash_map
<tree
,tree
> *values
;
900 /* SAVE_EXPRs that we've seen within the current LOOP_EXPR. NULL if we
901 aren't inside a loop. */
902 hash_set
<tree
> *save_exprs
;
903 /* The CONSTRUCTOR we're currently building up for an aggregate
906 /* The object we're building the CONSTRUCTOR for. */
908 /* Whether we should error on a non-constant expression or fail quietly. */
910 /* Whether we are strictly conforming to constant expression rules or
911 trying harder to get a constant value. */
915 /* A table of all constexpr calls that have been evaluated by the
916 compiler in this translation unit. */
918 static GTY (()) hash_table
<constexpr_call_hasher
> *constexpr_call_table
;
920 static tree
cxx_eval_constant_expression (const constexpr_ctx
*, tree
,
921 bool, bool *, bool *, tree
* = NULL
);
923 /* Compute a hash value for a constexpr call representation. */
926 constexpr_call_hasher::hash (constexpr_call
*info
)
931 /* Return true if the objects pointed to by P and Q represent calls
932 to the same constexpr function with the same arguments.
933 Otherwise, return false. */
936 constexpr_call_hasher::equal (constexpr_call
*lhs
, constexpr_call
*rhs
)
942 if (!constexpr_fundef_hasher::equal (lhs
->fundef
, rhs
->fundef
))
944 lhs_bindings
= lhs
->bindings
;
945 rhs_bindings
= rhs
->bindings
;
946 while (lhs_bindings
!= NULL
&& rhs_bindings
!= NULL
)
948 tree lhs_arg
= TREE_VALUE (lhs_bindings
);
949 tree rhs_arg
= TREE_VALUE (rhs_bindings
);
950 gcc_assert (TREE_TYPE (lhs_arg
) == TREE_TYPE (rhs_arg
));
951 if (!cp_tree_equal (lhs_arg
, rhs_arg
))
953 lhs_bindings
= TREE_CHAIN (lhs_bindings
);
954 rhs_bindings
= TREE_CHAIN (rhs_bindings
);
956 return lhs_bindings
== rhs_bindings
;
959 /* Initialize the constexpr call table, if needed. */
962 maybe_initialize_constexpr_call_table (void)
964 if (constexpr_call_table
== NULL
)
965 constexpr_call_table
= hash_table
<constexpr_call_hasher
>::create_ggc (101);
968 /* We have an expression tree T that represents a call, either CALL_EXPR
969 or AGGR_INIT_EXPR. If the call is lexically to a named function,
970 retrun the _DECL for that function. */
973 get_function_named_in_call (tree t
)
976 switch (TREE_CODE (t
))
979 fun
= CALL_EXPR_FN (t
);
983 fun
= AGGR_INIT_EXPR_FN (t
);
990 if (fun
&& TREE_CODE (fun
) == ADDR_EXPR
991 && TREE_CODE (TREE_OPERAND (fun
, 0)) == FUNCTION_DECL
)
992 fun
= TREE_OPERAND (fun
, 0);
996 /* We have an expression tree T that represents a call, either CALL_EXPR
997 or AGGR_INIT_EXPR. Return the Nth argument. */
1000 get_nth_callarg (tree t
, int n
)
1002 switch (TREE_CODE (t
))
1005 return CALL_EXPR_ARG (t
, n
);
1007 case AGGR_INIT_EXPR
:
1008 return AGGR_INIT_EXPR_ARG (t
, n
);
1016 /* Attempt to evaluate T which represents a call to a builtin function.
1017 We assume here that all builtin functions evaluate to scalar types
1018 represented by _CST nodes. */
1021 cxx_eval_builtin_function_call (const constexpr_ctx
*ctx
, tree t
, tree fun
,
1023 bool *non_constant_p
, bool *overflow_p
)
1025 const int nargs
= call_expr_nargs (t
);
1026 tree
*args
= (tree
*) alloca (nargs
* sizeof (tree
));
1030 /* Don't fold __builtin_constant_p within a constexpr function. */
1031 bool bi_const_p
= (DECL_FUNCTION_CODE (fun
) == BUILT_IN_CONSTANT_P
);
1034 && current_function_decl
1035 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
1037 *non_constant_p
= true;
1041 /* Be permissive for arguments to built-ins; __builtin_constant_p should
1042 return constant false for a non-constant argument. */
1043 constexpr_ctx new_ctx
= *ctx
;
1044 new_ctx
.quiet
= true;
1045 bool dummy1
= false, dummy2
= false;
1046 for (i
= 0; i
< nargs
; ++i
)
1048 args
[i
] = cxx_eval_constant_expression (&new_ctx
, CALL_EXPR_ARG (t
, i
),
1049 lval
, &dummy1
, &dummy2
);
1051 /* For __built_in_constant_p, fold all expressions with constant values
1052 even if they aren't C++ constant-expressions. */
1053 args
[i
] = cp_fully_fold (args
[i
]);
1056 bool save_ffbcp
= force_folding_builtin_constant_p
;
1057 force_folding_builtin_constant_p
= true;
1058 new_call
= fold_build_call_array_loc (EXPR_LOCATION (t
), TREE_TYPE (t
),
1059 CALL_EXPR_FN (t
), nargs
, args
);
1060 /* Fold away the NOP_EXPR from fold_builtin_n. */
1061 new_call
= fold (new_call
);
1062 force_folding_builtin_constant_p
= save_ffbcp
;
1063 VERIFY_CONSTANT (new_call
);
1067 /* TEMP is the constant value of a temporary object of type TYPE. Adjust
1068 the type of the value to match. */
1071 adjust_temp_type (tree type
, tree temp
)
1073 if (TREE_TYPE (temp
) == type
)
1075 /* Avoid wrapping an aggregate value in a NOP_EXPR. */
1076 if (TREE_CODE (temp
) == CONSTRUCTOR
)
1077 return build_constructor (type
, CONSTRUCTOR_ELTS (temp
));
1078 gcc_assert (scalarish_type_p (type
));
1079 return cp_fold_convert (type
, temp
);
1082 /* Subroutine of cxx_eval_call_expression.
1083 We are processing a call expression (either CALL_EXPR or
1084 AGGR_INIT_EXPR) in the context of CTX. Evaluate
1085 all arguments and bind their values to correspondings
1086 parameters, making up the NEW_CALL context. */
1089 cxx_bind_parameters_in_call (const constexpr_ctx
*ctx
, tree t
,
1090 constexpr_call
*new_call
,
1091 bool *non_constant_p
, bool *overflow_p
,
1092 bool *non_constant_args
)
1094 const int nargs
= call_expr_nargs (t
);
1095 tree fun
= new_call
->fundef
->decl
;
1096 tree parms
= DECL_ARGUMENTS (fun
);
1098 tree
*p
= &new_call
->bindings
;
1099 for (i
= 0; i
< nargs
; ++i
)
1102 tree type
= parms
? TREE_TYPE (parms
) : void_type_node
;
1103 x
= get_nth_callarg (t
, i
);
1104 /* For member function, the first argument is a pointer to the implied
1105 object. For a constructor, it might still be a dummy object, in
1106 which case we get the real argument from ctx. */
1107 if (i
== 0 && DECL_CONSTRUCTOR_P (fun
)
1108 && is_dummy_object (x
))
1111 x
= cp_build_addr_expr (x
, tf_warning_or_error
);
1114 arg
= cxx_eval_constant_expression (ctx
, x
, lval
,
1115 non_constant_p
, overflow_p
);
1116 /* Don't VERIFY_CONSTANT here. */
1117 if (*non_constant_p
&& ctx
->quiet
)
1119 /* Just discard ellipsis args after checking their constantitude. */
1122 if (*non_constant_p
)
1123 /* Don't try to adjust the type of non-constant args. */
1126 /* Make sure the binding has the same type as the parm. */
1127 if (TREE_CODE (type
) != REFERENCE_TYPE
)
1128 arg
= adjust_temp_type (type
, arg
);
1129 if (!TREE_CONSTANT (arg
))
1130 *non_constant_args
= true;
1131 *p
= build_tree_list (parms
, arg
);
1132 p
= &TREE_CHAIN (*p
);
1134 parms
= TREE_CHAIN (parms
);
1138 /* Variables and functions to manage constexpr call expansion context.
1139 These do not need to be marked for PCH or GC. */
1141 /* FIXME remember and print actual constant arguments. */
1142 static vec
<tree
> call_stack
= vNULL
;
1143 static int call_stack_tick
;
1144 static int last_cx_error_tick
;
1147 push_cx_call_context (tree call
)
1150 if (!EXPR_HAS_LOCATION (call
))
1151 SET_EXPR_LOCATION (call
, input_location
);
1152 call_stack
.safe_push (call
);
1153 if (call_stack
.length () > (unsigned) max_constexpr_depth
)
1159 pop_cx_call_context (void)
1166 cx_error_context (void)
1168 vec
<tree
> r
= vNULL
;
1169 if (call_stack_tick
!= last_cx_error_tick
1170 && !call_stack
.is_empty ())
1172 last_cx_error_tick
= call_stack_tick
;
1176 /* Subroutine of cxx_eval_constant_expression.
1177 Evaluate the call expression tree T in the context of OLD_CALL expression
1181 cxx_eval_call_expression (const constexpr_ctx
*ctx
, tree t
,
1183 bool *non_constant_p
, bool *overflow_p
)
1185 location_t loc
= EXPR_LOC_OR_LOC (t
, input_location
);
1186 tree fun
= get_function_named_in_call (t
);
1187 constexpr_call new_call
= { NULL
, NULL
, NULL
, 0 };
1190 if (fun
== NULL_TREE
)
1191 switch (CALL_EXPR_IFN (t
))
1193 case IFN_UBSAN_NULL
:
1194 case IFN_UBSAN_BOUNDS
:
1195 case IFN_UBSAN_VPTR
:
1199 error_at (loc
, "call to internal function");
1200 *non_constant_p
= true;
1204 if (TREE_CODE (fun
) != FUNCTION_DECL
)
1206 /* Might be a constexpr function pointer. */
1207 fun
= cxx_eval_constant_expression (ctx
, fun
,
1208 /*lval*/false, non_constant_p
,
1211 if (TREE_CODE (fun
) == ADDR_EXPR
)
1212 fun
= TREE_OPERAND (fun
, 0);
1214 if (TREE_CODE (fun
) != FUNCTION_DECL
)
1216 if (!ctx
->quiet
&& !*non_constant_p
)
1217 error_at (loc
, "expression %qE does not designate a constexpr "
1219 *non_constant_p
= true;
1222 if (DECL_CLONED_FUNCTION_P (fun
))
1223 fun
= DECL_CLONED_FUNCTION (fun
);
1225 if (is_ubsan_builtin_p (fun
))
1228 if (is_builtin_fn (fun
))
1229 return cxx_eval_builtin_function_call (ctx
, t
, fun
,
1230 lval
, non_constant_p
, overflow_p
);
1231 if (!DECL_DECLARED_CONSTEXPR_P (fun
))
1235 error_at (loc
, "call to non-constexpr function %qD", fun
);
1236 explain_invalid_constexpr_fn (fun
);
1238 *non_constant_p
= true;
1242 constexpr_ctx new_ctx
= *ctx
;
1243 if (DECL_CONSTRUCTOR_P (fun
) && !ctx
->object
1244 && TREE_CODE (t
) == AGGR_INIT_EXPR
)
1246 /* We want to have an initialization target for an AGGR_INIT_EXPR.
1247 If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
1248 new_ctx
.object
= AGGR_INIT_EXPR_SLOT (t
);
1249 tree ctor
= new_ctx
.ctor
= build_constructor (DECL_CONTEXT (fun
), NULL
);
1250 CONSTRUCTOR_NO_IMPLICIT_ZERO (ctor
) = true;
1251 ctx
->values
->put (new_ctx
.object
, ctor
);
1255 /* Shortcut trivial constructor/op=. */
1256 if (trivial_fn_p (fun
))
1258 tree init
= NULL_TREE
;
1259 if (call_expr_nargs (t
) == 2)
1260 init
= convert_from_reference (get_nth_callarg (t
, 1));
1261 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
1262 && AGGR_INIT_ZERO_FIRST (t
))
1263 init
= build_zero_init (DECL_CONTEXT (fun
), NULL_TREE
, false);
1266 tree op
= get_nth_callarg (t
, 0);
1267 if (is_dummy_object (op
))
1270 op
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (op
)), op
);
1271 tree set
= build2 (MODIFY_EXPR
, TREE_TYPE (op
), op
, init
);
1272 return cxx_eval_constant_expression (ctx
, set
, lval
,
1273 non_constant_p
, overflow_p
);
1277 /* We can't defer instantiating the function any longer. */
1278 if (!DECL_INITIAL (fun
)
1279 && DECL_TEMPLOID_INSTANTIATION (fun
))
1282 instantiate_decl (fun
, /*defer_ok*/false, /*expl_inst*/false);
1286 /* If in direct recursive call, optimize definition search. */
1287 if (ctx
&& ctx
->call
&& ctx
->call
->fundef
->decl
== fun
)
1288 new_call
.fundef
= ctx
->call
->fundef
;
1291 new_call
.fundef
= retrieve_constexpr_fundef (fun
);
1292 if (new_call
.fundef
== NULL
|| new_call
.fundef
->body
== NULL
)
1296 if (DECL_INITIAL (fun
) == error_mark_node
)
1297 error_at (loc
, "%qD called in a constant expression before its "
1298 "definition is complete", fun
);
1299 else if (DECL_INITIAL (fun
))
1301 /* The definition of fun was somehow unsuitable. */
1302 error_at (loc
, "%qD called in a constant expression", fun
);
1303 explain_invalid_constexpr_fn (fun
);
1306 error_at (loc
, "%qD used before its definition", fun
);
1308 *non_constant_p
= true;
1313 bool non_constant_args
= false;
1314 cxx_bind_parameters_in_call (ctx
, t
, &new_call
,
1315 non_constant_p
, overflow_p
, &non_constant_args
);
1316 if (*non_constant_p
)
1319 depth_ok
= push_cx_call_context (t
);
1321 tree result
= NULL_TREE
;
1323 constexpr_call
*entry
= NULL
;
1324 if (depth_ok
&& !non_constant_args
)
1326 new_call
.hash
= iterative_hash_template_arg
1327 (new_call
.bindings
, constexpr_fundef_hasher::hash (new_call
.fundef
));
1329 /* If we have seen this call before, we are done. */
1330 maybe_initialize_constexpr_call_table ();
1331 constexpr_call
**slot
1332 = constexpr_call_table
->find_slot (&new_call
, INSERT
);
1336 /* We need to keep a pointer to the entry, not just the slot, as the
1337 slot can move in the call to cxx_eval_builtin_function_call. */
1338 *slot
= entry
= ggc_alloc
<constexpr_call
> ();
1341 /* Calls which are in progress have their result set to NULL
1342 so that we can detect circular dependencies. */
1343 else if (entry
->result
== NULL
)
1346 error ("call has circular dependency");
1347 *non_constant_p
= true;
1348 entry
->result
= result
= error_mark_node
;
1351 result
= entry
->result
;
1357 error ("constexpr evaluation depth exceeds maximum of %d (use "
1358 "-fconstexpr-depth= to increase the maximum)",
1359 max_constexpr_depth
);
1360 *non_constant_p
= true;
1361 result
= error_mark_node
;
1365 if (!result
|| result
== error_mark_node
)
1367 gcc_assert (DECL_SAVED_TREE (fun
));
1370 /* Unshare the whole function body. */
1371 tree body
= copy_fn (fun
, parms
, res
);
1373 /* Associate the bindings with the remapped parms. */
1374 tree bound
= new_call
.bindings
;
1375 tree remapped
= parms
;
1378 tree oparm
= TREE_PURPOSE (bound
);
1379 tree arg
= TREE_VALUE (bound
);
1380 gcc_assert (DECL_NAME (remapped
) == DECL_NAME (oparm
));
1381 /* Don't share a CONSTRUCTOR that might be changed. */
1382 arg
= unshare_expr (arg
);
1383 ctx
->values
->put (remapped
, arg
);
1384 bound
= TREE_CHAIN (bound
);
1385 remapped
= DECL_CHAIN (remapped
);
1387 /* Add the RESULT_DECL to the values map, too. */
1388 tree slot
= NULL_TREE
;
1389 if (DECL_BY_REFERENCE (res
))
1391 slot
= AGGR_INIT_EXPR_SLOT (t
);
1392 tree addr
= build_address (slot
);
1393 addr
= build_nop (TREE_TYPE (res
), addr
);
1394 ctx
->values
->put (res
, addr
);
1395 ctx
->values
->put (slot
, NULL_TREE
);
1398 ctx
->values
->put (res
, NULL_TREE
);
1400 tree jump_target
= NULL_TREE
;
1401 cxx_eval_constant_expression (ctx
, body
,
1402 lval
, non_constant_p
, overflow_p
,
1405 if (DECL_CONSTRUCTOR_P (fun
))
1406 /* This can be null for a subobject constructor call, in
1407 which case what we care about is the initialization
1408 side-effects rather than the value. We could get at the
1409 value by evaluating *this, but we don't bother; there's
1410 no need to put such a call in the hash table. */
1411 result
= lval
? ctx
->object
: ctx
->ctor
;
1412 else if (VOID_TYPE_P (TREE_TYPE (res
)))
1416 result
= *ctx
->values
->get (slot
? slot
: res
);
1417 if (result
== NULL_TREE
&& !*non_constant_p
)
1420 error ("constexpr call flows off the end "
1422 *non_constant_p
= true;
1426 /* Remove the parms/result from the values map. Is it worth
1427 bothering to do this when the map itself is only live for
1428 one constexpr evaluation? If so, maybe also clear out
1429 other vars from call, maybe in BIND_EXPR handling? */
1430 ctx
->values
->remove (res
);
1432 ctx
->values
->remove (slot
);
1433 for (tree parm
= parms
; parm
; parm
= TREE_CHAIN (parm
))
1434 ctx
->values
->remove (parm
);
1437 if (result
== error_mark_node
)
1438 *non_constant_p
= true;
1439 if (*non_constant_p
|| *overflow_p
)
1440 result
= error_mark_node
;
1444 entry
->result
= result
;
1447 pop_cx_call_context ();
1448 return unshare_expr (result
);
1451 /* FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
1454 reduced_constant_expression_p (tree t
)
1456 switch (TREE_CODE (t
))
1459 /* Even if we can't lower this yet, it's constant. */
1463 /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
1464 tree elt
; unsigned HOST_WIDE_INT idx
;
1465 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t
), idx
, elt
)
1466 if (!reduced_constant_expression_p (elt
))
1471 /* FIXME are we calling this too much? */
1472 return initializer_constant_valid_p (t
, TREE_TYPE (t
)) != NULL_TREE
;
1476 /* Some expressions may have constant operands but are not constant
1477 themselves, such as 1/0. Call this function (or rather, the macro
1478 following it) to check for that condition.
1480 We only call this in places that require an arithmetic constant, not in
1481 places where we might have a non-constant expression that can be a
1482 component of a constant expression, such as the address of a constexpr
1483 variable that might be dereferenced later. */
1486 verify_constant (tree t
, bool allow_non_constant
, bool *non_constant_p
,
1489 if (!*non_constant_p
&& !reduced_constant_expression_p (t
))
1491 if (!allow_non_constant
)
1492 error ("%q+E is not a constant expression", t
);
1493 *non_constant_p
= true;
1495 if (TREE_OVERFLOW_P (t
))
1497 if (!allow_non_constant
)
1499 permerror (input_location
, "overflow in constant expression");
1500 /* If we're being permissive (and are in an enforcing
1501 context), ignore the overflow. */
1502 if (flag_permissive
)
1503 return *non_constant_p
;
1507 return *non_constant_p
;
1510 /* Check whether the shift operation with code CODE and type TYPE on LHS
1511 and RHS is undefined. If it is, give an error with an explanation,
1512 and return true; return false otherwise. */
1515 cxx_eval_check_shift_p (location_t loc
, const constexpr_ctx
*ctx
,
1516 enum tree_code code
, tree type
, tree lhs
, tree rhs
)
1518 if ((code
!= LSHIFT_EXPR
&& code
!= RSHIFT_EXPR
)
1519 || TREE_CODE (lhs
) != INTEGER_CST
1520 || TREE_CODE (rhs
) != INTEGER_CST
)
1523 tree lhstype
= TREE_TYPE (lhs
);
1524 unsigned HOST_WIDE_INT uprec
= TYPE_PRECISION (TREE_TYPE (lhs
));
1526 /* [expr.shift] The behavior is undefined if the right operand
1527 is negative, or greater than or equal to the length in bits
1528 of the promoted left operand. */
1529 if (tree_int_cst_sgn (rhs
) == -1)
1532 permerror (loc
, "right operand of shift expression %q+E is negative",
1533 build2_loc (loc
, code
, type
, lhs
, rhs
));
1534 return (!flag_permissive
|| ctx
->quiet
);
1536 if (compare_tree_int (rhs
, uprec
) >= 0)
1539 permerror (loc
, "right operand of shift expression %q+E is >= than "
1540 "the precision of the left operand",
1541 build2_loc (loc
, code
, type
, lhs
, rhs
));
1542 return (!flag_permissive
|| ctx
->quiet
);
1545 /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
1546 if E1 has a signed type and non-negative value, and E1x2^E2 is
1547 representable in the corresponding unsigned type of the result type,
1548 then that value, converted to the result type, is the resulting value;
1549 otherwise, the behavior is undefined. */
1550 if (code
== LSHIFT_EXPR
&& !TYPE_UNSIGNED (lhstype
)
1551 && (cxx_dialect
>= cxx11
))
1553 if (tree_int_cst_sgn (lhs
) == -1)
1557 "left operand of shift expression %q+E is negative",
1558 build2_loc (loc
, code
, type
, lhs
, rhs
));
1559 return (!flag_permissive
|| ctx
->quiet
);
1561 /* For signed x << y the following:
1562 (unsigned) x >> ((prec (lhs) - 1) - y)
1563 if > 1, is undefined. The right-hand side of this formula
1564 is the highest bit of the LHS that can be set (starting from 0),
1565 so that the shift doesn't overflow. We then right-shift the LHS
1566 to see whether any other bit is set making the original shift
1567 undefined -- the result is not representable in the corresponding
1569 tree t
= build_int_cst (unsigned_type_node
, uprec
- 1);
1570 t
= fold_build2 (MINUS_EXPR
, unsigned_type_node
, t
, rhs
);
1571 tree ulhs
= fold_convert (unsigned_type_for (lhstype
), lhs
);
1572 t
= fold_build2 (RSHIFT_EXPR
, TREE_TYPE (ulhs
), ulhs
, t
);
1573 if (tree_int_cst_lt (integer_one_node
, t
))
1576 permerror (loc
, "shift expression %q+E overflows",
1577 build2_loc (loc
, code
, type
, lhs
, rhs
));
1578 return (!flag_permissive
|| ctx
->quiet
);
1584 /* Subroutine of cxx_eval_constant_expression.
1585 Attempt to reduce the unary expression tree T to a compile time value.
1586 If successful, return the value. Otherwise issue a diagnostic
1587 and return error_mark_node. */
1590 cxx_eval_unary_expression (const constexpr_ctx
*ctx
, tree t
,
1592 bool *non_constant_p
, bool *overflow_p
)
1595 tree orig_arg
= TREE_OPERAND (t
, 0);
1596 tree arg
= cxx_eval_constant_expression (ctx
, orig_arg
, /*lval*/false,
1597 non_constant_p
, overflow_p
);
1598 VERIFY_CONSTANT (arg
);
1599 location_t loc
= EXPR_LOCATION (t
);
1600 enum tree_code code
= TREE_CODE (t
);
1601 tree type
= TREE_TYPE (t
);
1602 r
= fold_unary_loc (loc
, code
, type
, arg
);
1605 if (arg
== orig_arg
)
1608 r
= build1_loc (loc
, code
, type
, arg
);
1610 VERIFY_CONSTANT (r
);
1614 /* Subroutine of cxx_eval_constant_expression.
1615 Like cxx_eval_unary_expression, except for binary expressions. */
1618 cxx_eval_binary_expression (const constexpr_ctx
*ctx
, tree t
,
1620 bool *non_constant_p
, bool *overflow_p
)
1623 tree orig_lhs
= TREE_OPERAND (t
, 0);
1624 tree orig_rhs
= TREE_OPERAND (t
, 1);
1626 lhs
= cxx_eval_constant_expression (ctx
, orig_lhs
, /*lval*/false,
1627 non_constant_p
, overflow_p
);
1628 /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer
1630 if (*non_constant_p
)
1632 rhs
= cxx_eval_constant_expression (ctx
, orig_rhs
, /*lval*/false,
1633 non_constant_p
, overflow_p
);
1634 if (*non_constant_p
)
1637 location_t loc
= EXPR_LOCATION (t
);
1638 enum tree_code code
= TREE_CODE (t
);
1639 tree type
= TREE_TYPE (t
);
1641 if (code
== EQ_EXPR
|| code
== NE_EXPR
)
1643 bool is_code_eq
= (code
== EQ_EXPR
);
1645 if (TREE_CODE (lhs
) == PTRMEM_CST
1646 && TREE_CODE (rhs
) == PTRMEM_CST
)
1647 r
= constant_boolean_node (cp_tree_equal (lhs
, rhs
) == is_code_eq
,
1649 else if ((TREE_CODE (lhs
) == PTRMEM_CST
1650 || TREE_CODE (rhs
) == PTRMEM_CST
)
1651 && (null_member_pointer_value_p (lhs
)
1652 || null_member_pointer_value_p (rhs
)))
1653 r
= constant_boolean_node (!is_code_eq
, type
);
1657 r
= fold_binary_loc (loc
, code
, type
, lhs
, rhs
);
1661 if (lhs
== orig_lhs
&& rhs
== orig_rhs
)
1664 r
= build2_loc (loc
, code
, type
, lhs
, rhs
);
1666 else if (cxx_eval_check_shift_p (loc
, ctx
, code
, type
, lhs
, rhs
))
1667 *non_constant_p
= true;
1668 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
1669 a local array in a constexpr function. */
1670 bool ptr
= POINTER_TYPE_P (TREE_TYPE (lhs
));
1672 VERIFY_CONSTANT (r
);
1676 /* Subroutine of cxx_eval_constant_expression.
1677 Attempt to evaluate condition expressions. Dead branches are not
1681 cxx_eval_conditional_expression (const constexpr_ctx
*ctx
, tree t
,
1683 bool *non_constant_p
, bool *overflow_p
,
1686 tree val
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
1688 non_constant_p
, overflow_p
);
1689 VERIFY_CONSTANT (val
);
1690 /* Don't VERIFY_CONSTANT the other operands. */
1691 if (integer_zerop (val
))
1692 return cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 2),
1694 non_constant_p
, overflow_p
,
1696 return cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
1698 non_constant_p
, overflow_p
,
1702 /* Returns less than, equal to, or greater than zero if KEY is found to be
1703 less than, to match, or to be greater than the constructor_elt's INDEX. */
1706 array_index_cmp (tree key
, tree index
)
1708 gcc_assert (TREE_CODE (key
) == INTEGER_CST
);
1710 switch (TREE_CODE (index
))
1713 return tree_int_cst_compare (key
, index
);
1716 tree lo
= TREE_OPERAND (index
, 0);
1717 tree hi
= TREE_OPERAND (index
, 1);
1718 if (tree_int_cst_lt (key
, lo
))
1720 else if (tree_int_cst_lt (hi
, key
))
1730 /* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1
1731 if none. If INSERT is true, insert a matching element rather than fail. */
1733 static HOST_WIDE_INT
1734 find_array_ctor_elt (tree ary
, tree dindex
, bool insert
= false)
1736 if (tree_int_cst_sgn (dindex
) < 0)
1739 unsigned HOST_WIDE_INT i
= tree_to_uhwi (dindex
);
1740 vec
<constructor_elt
, va_gc
> *elts
= CONSTRUCTOR_ELTS (ary
);
1741 unsigned HOST_WIDE_INT len
= vec_safe_length (elts
);
1743 unsigned HOST_WIDE_INT end
= len
;
1744 unsigned HOST_WIDE_INT begin
= 0;
1746 /* If the last element of the CONSTRUCTOR has its own index, we can assume
1747 that the same is true of the other elements and index directly. */
1750 tree cindex
= (*elts
)[end
-1].index
;
1751 if (TREE_CODE (cindex
) == INTEGER_CST
1752 && compare_tree_int (cindex
, end
-1) == 0)
1761 /* Otherwise, find a matching index by means of a binary search. */
1762 while (begin
!= end
)
1764 unsigned HOST_WIDE_INT middle
= (begin
+ end
) / 2;
1765 constructor_elt
&elt
= (*elts
)[middle
];
1766 tree idx
= elt
.index
;
1768 int cmp
= array_index_cmp (dindex
, idx
);
1775 if (insert
&& TREE_CODE (idx
) == RANGE_EXPR
)
1777 /* We need to split the range. */
1779 tree lo
= TREE_OPERAND (idx
, 0);
1780 tree hi
= TREE_OPERAND (idx
, 1);
1781 if (tree_int_cst_lt (lo
, dindex
))
1783 /* There are still some lower elts; shorten the range. */
1784 tree new_hi
= int_const_binop (MINUS_EXPR
, dindex
,
1786 if (tree_int_cst_equal (lo
, new_hi
))
1787 /* Only one element left, no longer a range. */
1790 TREE_OPERAND (idx
, 1) = new_hi
;
1791 /* Append the element we want to insert. */
1794 e
.value
= unshare_expr (elt
.value
);
1795 vec_safe_insert (CONSTRUCTOR_ELTS (ary
), middle
, e
);
1798 /* No lower elts, the range elt is now ours. */
1801 if (tree_int_cst_lt (dindex
, hi
))
1803 /* There are still some higher elts; append a range. */
1804 tree new_lo
= int_const_binop (PLUS_EXPR
, dindex
,
1806 if (tree_int_cst_equal (new_lo
, hi
))
1809 e
.index
= build2 (RANGE_EXPR
, sizetype
, new_lo
, hi
);
1810 e
.value
= unshare_expr (elt
.value
);
1811 vec_safe_insert (CONSTRUCTOR_ELTS (ary
), middle
+1, e
);
1820 constructor_elt e
= { dindex
, NULL_TREE
};
1821 vec_safe_insert (CONSTRUCTOR_ELTS (ary
), end
, e
);
1828 /* Under the control of CTX, issue a detailed diagnostic for
1829 an out-of-bounds subscript INDEX into the expression ARRAY. */
1832 diag_array_subscript (const constexpr_ctx
*ctx
, tree array
, tree index
)
1836 tree arraytype
= TREE_TYPE (array
);
1838 /* Convert the unsigned array subscript to a signed integer to avoid
1839 printing huge numbers for small negative values. */
1840 tree sidx
= fold_convert (ssizetype
, index
);
1843 error ("array subscript value %qE is outside the bounds "
1844 "of array %qD of type %qT", sidx
, array
, arraytype
);
1845 inform (DECL_SOURCE_LOCATION (array
), "declared here");
1848 error ("array subscript value %qE is outside the bounds "
1849 "of array type %qT", sidx
, arraytype
);
1853 /* Subroutine of cxx_eval_constant_expression.
1854 Attempt to reduce a reference to an array slot. */
1857 cxx_eval_array_reference (const constexpr_ctx
*ctx
, tree t
,
1859 bool *non_constant_p
, bool *overflow_p
)
1861 tree oldary
= TREE_OPERAND (t
, 0);
1862 tree ary
= cxx_eval_constant_expression (ctx
, oldary
,
1864 non_constant_p
, overflow_p
);
1868 unsigned len
, elem_nchars
= 1;
1869 if (*non_constant_p
)
1871 oldidx
= TREE_OPERAND (t
, 1);
1872 index
= cxx_eval_constant_expression (ctx
, oldidx
,
1874 non_constant_p
, overflow_p
);
1875 VERIFY_CONSTANT (index
);
1876 if (lval
&& ary
== oldary
&& index
== oldidx
)
1879 return build4 (ARRAY_REF
, TREE_TYPE (t
), ary
, index
, NULL
, NULL
);
1880 elem_type
= TREE_TYPE (TREE_TYPE (ary
));
1881 if (TREE_CODE (ary
) == CONSTRUCTOR
)
1882 len
= CONSTRUCTOR_NELTS (ary
);
1883 else if (TREE_CODE (ary
) == STRING_CST
)
1885 elem_nchars
= (TYPE_PRECISION (elem_type
)
1886 / TYPE_PRECISION (char_type_node
));
1887 len
= (unsigned) TREE_STRING_LENGTH (ary
) / elem_nchars
;
1891 /* We can't do anything with other tree codes, so use
1892 VERIFY_CONSTANT to complain and fail. */
1893 VERIFY_CONSTANT (ary
);
1897 if (!tree_fits_shwi_p (index
)
1898 || (i
= tree_to_shwi (index
)) < 0)
1900 diag_array_subscript (ctx
, ary
, index
);
1901 *non_constant_p
= true;
1905 tree nelts
= array_type_nelts_top (TREE_TYPE (ary
));
1906 /* For VLAs, the number of elements won't be an integer constant. */
1907 nelts
= cxx_eval_constant_expression (ctx
, nelts
, false, non_constant_p
,
1909 VERIFY_CONSTANT (nelts
);
1910 if (!tree_int_cst_lt (index
, nelts
))
1912 diag_array_subscript (ctx
, ary
, index
);
1913 *non_constant_p
= true;
1918 if (TREE_CODE (ary
) == CONSTRUCTOR
)
1920 HOST_WIDE_INT ix
= find_array_ctor_elt (ary
, index
);
1930 if (TREE_CODE (ary
) == CONSTRUCTOR
1931 && CONSTRUCTOR_NO_IMPLICIT_ZERO (ary
))
1933 /* 'ary' is part of the aggregate initializer we're currently
1934 building; if there's no initializer for this element yet,
1937 error ("accessing uninitialized array element");
1938 *non_constant_p
= true;
1942 /* If it's within the array bounds but doesn't have an explicit
1943 initializer, it's value-initialized. */
1944 tree val
= build_value_init (elem_type
, tf_warning_or_error
);
1945 return cxx_eval_constant_expression (ctx
, val
, lval
, non_constant_p
,
1949 if (TREE_CODE (ary
) == CONSTRUCTOR
)
1950 return (*CONSTRUCTOR_ELTS (ary
))[i
].value
;
1951 else if (elem_nchars
== 1)
1952 return build_int_cst (cv_unqualified (TREE_TYPE (TREE_TYPE (ary
))),
1953 TREE_STRING_POINTER (ary
)[i
]);
1956 tree type
= cv_unqualified (TREE_TYPE (TREE_TYPE (ary
)));
1957 return native_interpret_expr (type
, (const unsigned char *)
1958 TREE_STRING_POINTER (ary
)
1959 + i
* elem_nchars
, elem_nchars
);
1961 /* Don't VERIFY_CONSTANT here. */
1964 /* Subroutine of cxx_eval_constant_expression.
1965 Attempt to reduce a field access of a value of class type. */
1968 cxx_eval_component_reference (const constexpr_ctx
*ctx
, tree t
,
1970 bool *non_constant_p
, bool *overflow_p
)
1972 unsigned HOST_WIDE_INT i
;
1975 tree part
= TREE_OPERAND (t
, 1);
1976 tree orig_whole
= TREE_OPERAND (t
, 0);
1977 tree whole
= cxx_eval_constant_expression (ctx
, orig_whole
,
1979 non_constant_p
, overflow_p
);
1980 if (TREE_CODE (whole
) == PTRMEM_CST
)
1981 whole
= cplus_expand_constant (whole
);
1982 if (whole
== orig_whole
)
1985 return fold_build3 (COMPONENT_REF
, TREE_TYPE (t
),
1986 whole
, part
, NULL_TREE
);
1987 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
1989 if (!*non_constant_p
&& TREE_CODE (whole
) != CONSTRUCTOR
)
1992 error ("%qE is not a constant expression", orig_whole
);
1993 *non_constant_p
= true;
1995 if (DECL_MUTABLE_P (part
))
1998 error ("mutable %qD is not usable in a constant expression", part
);
1999 *non_constant_p
= true;
2001 if (*non_constant_p
)
2003 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole
), i
, field
, value
)
2010 /* We're in the middle of initializing it. */
2014 if (TREE_CODE (TREE_TYPE (whole
)) == UNION_TYPE
2015 && CONSTRUCTOR_NELTS (whole
) > 0)
2017 /* DR 1188 says we don't have to deal with this. */
2019 error ("accessing %qD member instead of initialized %qD member in "
2020 "constant expression", part
, CONSTRUCTOR_ELT (whole
, 0)->index
);
2021 *non_constant_p
= true;
2025 /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty
2026 classes never get represented; throw together a value now. */
2027 if (is_really_empty_class (TREE_TYPE (t
)))
2028 return build_constructor (TREE_TYPE (t
), NULL
);
2030 if (CONSTRUCTOR_NO_IMPLICIT_ZERO (whole
))
2032 /* 'whole' is part of the aggregate initializer we're currently
2033 building; if there's no initializer for this member yet, that's an
2036 error ("accessing uninitialized member %qD", part
);
2037 *non_constant_p
= true;
2041 /* If there's no explicit init for this field, it's value-initialized. */
2042 value
= build_value_init (TREE_TYPE (t
), tf_warning_or_error
);
2043 return cxx_eval_constant_expression (ctx
, value
,
2045 non_constant_p
, overflow_p
);
2048 /* Subroutine of cxx_eval_constant_expression.
2049 Attempt to reduce a field access of a value of class type that is
2050 expressed as a BIT_FIELD_REF. */
2053 cxx_eval_bit_field_ref (const constexpr_ctx
*ctx
, tree t
,
2055 bool *non_constant_p
, bool *overflow_p
)
2057 tree orig_whole
= TREE_OPERAND (t
, 0);
2058 tree retval
, fldval
, utype
, mask
;
2059 bool fld_seen
= false;
2060 HOST_WIDE_INT istart
, isize
;
2061 tree whole
= cxx_eval_constant_expression (ctx
, orig_whole
,
2063 non_constant_p
, overflow_p
);
2064 tree start
, field
, value
;
2065 unsigned HOST_WIDE_INT i
;
2067 if (whole
== orig_whole
)
2069 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
2071 if (!*non_constant_p
2072 && TREE_CODE (whole
) != VECTOR_CST
2073 && TREE_CODE (whole
) != CONSTRUCTOR
)
2076 error ("%qE is not a constant expression", orig_whole
);
2077 *non_constant_p
= true;
2079 if (*non_constant_p
)
2082 if (TREE_CODE (whole
) == VECTOR_CST
)
2083 return fold_ternary (BIT_FIELD_REF
, TREE_TYPE (t
), whole
,
2084 TREE_OPERAND (t
, 1), TREE_OPERAND (t
, 2));
2086 start
= TREE_OPERAND (t
, 2);
2087 istart
= tree_to_shwi (start
);
2088 isize
= tree_to_shwi (TREE_OPERAND (t
, 1));
2089 utype
= TREE_TYPE (t
);
2090 if (!TYPE_UNSIGNED (utype
))
2091 utype
= build_nonstandard_integer_type (TYPE_PRECISION (utype
), 1);
2092 retval
= build_int_cst (utype
, 0);
2093 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole
), i
, field
, value
)
2095 tree bitpos
= bit_position (field
);
2096 if (bitpos
== start
&& DECL_SIZE (field
) == TREE_OPERAND (t
, 1))
2098 if (TREE_CODE (TREE_TYPE (field
)) == INTEGER_TYPE
2099 && TREE_CODE (value
) == INTEGER_CST
2100 && tree_fits_shwi_p (bitpos
)
2101 && tree_fits_shwi_p (DECL_SIZE (field
)))
2103 HOST_WIDE_INT bit
= tree_to_shwi (bitpos
);
2104 HOST_WIDE_INT sz
= tree_to_shwi (DECL_SIZE (field
));
2105 HOST_WIDE_INT shift
;
2106 if (bit
>= istart
&& bit
+ sz
<= istart
+ isize
)
2108 fldval
= fold_convert (utype
, value
);
2109 mask
= build_int_cst_type (utype
, -1);
2110 mask
= fold_build2 (LSHIFT_EXPR
, utype
, mask
,
2111 size_int (TYPE_PRECISION (utype
) - sz
));
2112 mask
= fold_build2 (RSHIFT_EXPR
, utype
, mask
,
2113 size_int (TYPE_PRECISION (utype
) - sz
));
2114 fldval
= fold_build2 (BIT_AND_EXPR
, utype
, fldval
, mask
);
2115 shift
= bit
- istart
;
2116 if (BYTES_BIG_ENDIAN
)
2117 shift
= TYPE_PRECISION (utype
) - shift
- sz
;
2118 fldval
= fold_build2 (LSHIFT_EXPR
, utype
, fldval
,
2120 retval
= fold_build2 (BIT_IOR_EXPR
, utype
, retval
, fldval
);
2126 return fold_convert (TREE_TYPE (t
), retval
);
2128 return error_mark_node
;
2131 /* Subroutine of cxx_eval_constant_expression.
2132 Evaluate a short-circuited logical expression T in the context
2133 of a given constexpr CALL. BAILOUT_VALUE is the value for
2134 early return. CONTINUE_VALUE is used here purely for
2135 sanity check purposes. */
2138 cxx_eval_logical_expression (const constexpr_ctx
*ctx
, tree t
,
2139 tree bailout_value
, tree continue_value
,
2141 bool *non_constant_p
, bool *overflow_p
)
2144 tree lhs
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
2146 non_constant_p
, overflow_p
);
2147 VERIFY_CONSTANT (lhs
);
2148 if (tree_int_cst_equal (lhs
, bailout_value
))
2150 gcc_assert (tree_int_cst_equal (lhs
, continue_value
));
2151 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
2152 lval
, non_constant_p
,
2154 VERIFY_CONSTANT (r
);
2158 /* REF is a COMPONENT_REF designating a particular field. V is a vector of
2159 CONSTRUCTOR elements to initialize (part of) an object containing that
2160 field. Return a pointer to the constructor_elt corresponding to the
2161 initialization of the field. */
2163 static constructor_elt
*
2164 base_field_constructor_elt (vec
<constructor_elt
, va_gc
> *v
, tree ref
)
2166 tree aggr
= TREE_OPERAND (ref
, 0);
2167 tree field
= TREE_OPERAND (ref
, 1);
2169 constructor_elt
*ce
;
2171 gcc_assert (TREE_CODE (ref
) == COMPONENT_REF
);
2173 if (TREE_CODE (aggr
) == COMPONENT_REF
)
2175 constructor_elt
*base_ce
2176 = base_field_constructor_elt (v
, aggr
);
2177 v
= CONSTRUCTOR_ELTS (base_ce
->value
);
2180 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
2181 if (ce
->index
== field
)
2188 /* Some of the expressions fed to the constexpr mechanism are calls to
2189 constructors, which have type void. In that case, return the type being
2190 initialized by the constructor. */
2193 initialized_type (tree t
)
2197 tree type
= cv_unqualified (TREE_TYPE (t
));
2198 if (TREE_CODE (t
) == CALL_EXPR
|| TREE_CODE (t
) == AGGR_INIT_EXPR
)
2200 /* A constructor call has void type, so we need to look deeper. */
2201 tree fn
= get_function_named_in_call (t
);
2202 if (fn
&& TREE_CODE (fn
) == FUNCTION_DECL
2203 && DECL_CXX_CONSTRUCTOR_P (fn
))
2204 type
= DECL_CONTEXT (fn
);
2209 /* We're about to initialize element INDEX of an array or class from VALUE.
2210 Set up NEW_CTX appropriately by adjusting .object to refer to the
2211 subobject and creating a new CONSTRUCTOR if the element is itself
2212 a class or array. */
2215 init_subob_ctx (const constexpr_ctx
*ctx
, constexpr_ctx
&new_ctx
,
2216 tree index
, tree
&value
)
2220 if (index
&& TREE_CODE (index
) != INTEGER_CST
2221 && TREE_CODE (index
) != FIELD_DECL
)
2222 /* This won't have an element in the new CONSTRUCTOR. */
2225 tree type
= initialized_type (value
);
2226 if (!AGGREGATE_TYPE_P (type
) && !VECTOR_TYPE_P (type
))
2227 /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
2230 /* The sub-aggregate initializer might contain a placeholder;
2231 update object to refer to the subobject and ctor to refer to
2232 the (newly created) sub-initializer. */
2234 new_ctx
.object
= build_ctor_subob_ref (index
, type
, ctx
->object
);
2235 tree elt
= build_constructor (type
, NULL
);
2236 CONSTRUCTOR_NO_IMPLICIT_ZERO (elt
) = true;
2239 if (TREE_CODE (value
) == TARGET_EXPR
)
2240 /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
2241 value
= TARGET_EXPR_INITIAL (value
);
2244 /* We're about to process an initializer for a class or array TYPE. Make
2245 sure that CTX is set up appropriately. */
2248 verify_ctor_sanity (const constexpr_ctx
*ctx
, tree type
)
2250 /* We don't bother building a ctor for an empty base subobject. */
2251 if (is_empty_class (type
))
2254 /* We're in the middle of an initializer that might involve placeholders;
2255 our caller should have created a CONSTRUCTOR for us to put the
2256 initializer into. We will either return that constructor or T. */
2257 gcc_assert (ctx
->ctor
);
2258 gcc_assert (same_type_ignoring_top_level_qualifiers_p
2259 (type
, TREE_TYPE (ctx
->ctor
)));
2260 /* We used to check that ctx->ctor was empty, but that isn't the case when
2261 the object is zero-initialized before calling the constructor. */
2263 gcc_assert (same_type_ignoring_top_level_qualifiers_p
2264 (type
, TREE_TYPE (ctx
->object
)));
2265 gcc_assert (!ctx
->object
|| !DECL_P (ctx
->object
)
2266 || *(ctx
->values
->get (ctx
->object
)) == ctx
->ctor
);
2269 /* Subroutine of cxx_eval_constant_expression.
2270 The expression tree T denotes a C-style array or a C-style
2271 aggregate. Reduce it to a constant expression. */
2274 cxx_eval_bare_aggregate (const constexpr_ctx
*ctx
, tree t
,
2276 bool *non_constant_p
, bool *overflow_p
)
2278 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (t
);
2279 bool changed
= false;
2280 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t
));
2281 tree type
= TREE_TYPE (t
);
2283 constexpr_ctx new_ctx
;
2284 if (TYPE_PTRMEMFUNC_P (type
))
2286 /* We don't really need the ctx->ctor business for a PMF, but it's
2287 simpler to use the same code. */
2289 new_ctx
.ctor
= build_constructor (type
, NULL
);
2290 new_ctx
.object
= NULL_TREE
;
2293 verify_ctor_sanity (ctx
, type
);
2294 vec
<constructor_elt
, va_gc
> **p
= &CONSTRUCTOR_ELTS (ctx
->ctor
);
2295 vec_alloc (*p
, vec_safe_length (v
));
2299 bool constant_p
= true;
2300 bool side_effects_p
= false;
2301 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, index
, value
)
2303 tree orig_value
= value
;
2304 init_subob_ctx (ctx
, new_ctx
, index
, value
);
2305 if (new_ctx
.ctor
!= ctx
->ctor
)
2306 /* If we built a new CONSTRUCTOR, attach it now so that other
2307 initializers can refer to it. */
2308 CONSTRUCTOR_APPEND_ELT (*p
, index
, new_ctx
.ctor
);
2309 tree elt
= cxx_eval_constant_expression (&new_ctx
, value
,
2311 non_constant_p
, overflow_p
);
2312 /* Don't VERIFY_CONSTANT here. */
2313 if (ctx
->quiet
&& *non_constant_p
)
2315 if (elt
!= orig_value
)
2318 if (!TREE_CONSTANT (elt
))
2320 if (TREE_SIDE_EFFECTS (elt
))
2321 side_effects_p
= true;
2322 if (index
&& TREE_CODE (index
) == COMPONENT_REF
)
2324 /* This is an initialization of a vfield inside a base
2325 subaggregate that we already initialized; push this
2326 initialization into the previous initialization. */
2327 constructor_elt
*inner
= base_field_constructor_elt (*p
, index
);
2332 && (TREE_CODE (index
) == NOP_EXPR
2333 || TREE_CODE (index
) == POINTER_PLUS_EXPR
))
2335 /* This is an initializer for an empty base; now that we've
2336 checked that it's constant, we can ignore it. */
2337 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index
))));
2340 else if (new_ctx
.ctor
!= ctx
->ctor
)
2342 /* We appended this element above; update the value. */
2343 gcc_assert ((*p
)->last().index
== index
);
2344 (*p
)->last().value
= elt
;
2347 CONSTRUCTOR_APPEND_ELT (*p
, index
, elt
);
2349 if (*non_constant_p
|| !changed
)
2352 /* We're done building this CONSTRUCTOR, so now we can interpret an
2353 element without an explicit initializer as value-initialized. */
2354 CONSTRUCTOR_NO_IMPLICIT_ZERO (t
) = false;
2355 TREE_CONSTANT (t
) = constant_p
;
2356 TREE_SIDE_EFFECTS (t
) = side_effects_p
;
2357 if (VECTOR_TYPE_P (type
))
2362 /* Subroutine of cxx_eval_constant_expression.
2363 The expression tree T is a VEC_INIT_EXPR which denotes the desired
2364 initialization of a non-static data member of array type. Reduce it to a
2367 Note that apart from value-initialization (when VALUE_INIT is true),
2368 this is only intended to support value-initialization and the
2369 initializations done by defaulted constructors for classes with
2370 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
2371 will either be NULL_TREE for the default constructor, or a COMPONENT_REF
2372 for the copy/move constructor. */
2375 cxx_eval_vec_init_1 (const constexpr_ctx
*ctx
, tree atype
, tree init
,
2376 bool value_init
, bool lval
,
2377 bool *non_constant_p
, bool *overflow_p
)
2379 tree elttype
= TREE_TYPE (atype
);
2380 unsigned HOST_WIDE_INT max
= tree_to_uhwi (array_type_nelts_top (atype
));
2381 verify_ctor_sanity (ctx
, atype
);
2382 vec
<constructor_elt
, va_gc
> **p
= &CONSTRUCTOR_ELTS (ctx
->ctor
);
2383 vec_alloc (*p
, max
+ 1);
2384 bool pre_init
= false;
2385 unsigned HOST_WIDE_INT i
;
2387 /* For the default constructor, build up a call to the default
2388 constructor of the element type. We only need to handle class types
2389 here, as for a constructor to be constexpr, all members must be
2390 initialized, which for a defaulted default constructor means they must
2391 be of a class type with a constexpr default constructor. */
2392 if (TREE_CODE (elttype
) == ARRAY_TYPE
)
2393 /* We only do this at the lowest level. */;
2394 else if (value_init
)
2396 init
= build_value_init (elttype
, tf_warning_or_error
);
2401 vec
<tree
, va_gc
> *argvec
= make_tree_vector ();
2402 init
= build_special_member_call (NULL_TREE
, complete_ctor_identifier
,
2403 &argvec
, elttype
, LOOKUP_NORMAL
,
2404 tf_warning_or_error
);
2405 release_tree_vector (argvec
);
2406 init
= build_aggr_init_expr (TREE_TYPE (init
), init
);
2410 for (i
= 0; i
< max
; ++i
)
2412 tree idx
= build_int_cst (size_type_node
, i
);
2415 constexpr_ctx new_ctx
;
2416 init_subob_ctx (ctx
, new_ctx
, idx
, pre_init
? init
: elttype
);
2417 if (new_ctx
.ctor
!= ctx
->ctor
)
2418 CONSTRUCTOR_APPEND_ELT (*p
, idx
, new_ctx
.ctor
);
2419 if (TREE_CODE (elttype
) == ARRAY_TYPE
)
2421 /* A multidimensional array; recurse. */
2422 if (value_init
|| init
== NULL_TREE
)
2424 eltinit
= NULL_TREE
;
2428 eltinit
= cp_build_array_ref (input_location
, init
, idx
,
2429 tf_warning_or_error
);
2430 eltinit
= cxx_eval_vec_init_1 (&new_ctx
, elttype
, eltinit
, value_init
,
2432 non_constant_p
, overflow_p
);
2436 /* Initializing an element using value or default initialization
2437 we just pre-built above. */
2438 eltinit
= cxx_eval_constant_expression (&new_ctx
, init
, lval
,
2439 non_constant_p
, overflow_p
);
2444 /* Copying an element. */
2445 gcc_assert (same_type_ignoring_top_level_qualifiers_p
2446 (atype
, TREE_TYPE (init
)));
2447 eltinit
= cp_build_array_ref (input_location
, init
, idx
,
2448 tf_warning_or_error
);
2449 if (!real_lvalue_p (init
))
2450 eltinit
= move (eltinit
);
2451 eltinit
= force_rvalue (eltinit
, tf_warning_or_error
);
2452 eltinit
= (cxx_eval_constant_expression
2453 (&new_ctx
, eltinit
, lval
,
2454 non_constant_p
, overflow_p
));
2456 if (*non_constant_p
&& !ctx
->quiet
)
2458 if (new_ctx
.ctor
!= ctx
->ctor
)
2460 /* We appended this element above; update the value. */
2461 gcc_assert ((*p
)->last().index
== idx
);
2462 (*p
)->last().value
= eltinit
;
2465 CONSTRUCTOR_APPEND_ELT (*p
, idx
, eltinit
);
2466 /* Reuse the result of cxx_eval_constant_expression call
2467 from the first iteration to all others if it is a constant
2468 initializer that doesn't require relocations. */
2471 && (initializer_constant_valid_p (eltinit
, TREE_TYPE (eltinit
))
2472 == null_pointer_node
))
2474 if (new_ctx
.ctor
!= ctx
->ctor
)
2475 eltinit
= new_ctx
.ctor
;
2476 for (i
= 1; i
< max
; ++i
)
2478 idx
= build_int_cst (size_type_node
, i
);
2479 CONSTRUCTOR_APPEND_ELT (*p
, idx
, unshare_expr (eltinit
));
2485 if (!*non_constant_p
)
2488 CONSTRUCTOR_NO_IMPLICIT_ZERO (init
) = false;
2494 cxx_eval_vec_init (const constexpr_ctx
*ctx
, tree t
,
2496 bool *non_constant_p
, bool *overflow_p
)
2498 tree atype
= TREE_TYPE (t
);
2499 tree init
= VEC_INIT_EXPR_INIT (t
);
2500 tree r
= cxx_eval_vec_init_1 (ctx
, atype
, init
,
2501 VEC_INIT_EXPR_VALUE_INIT (t
),
2502 lval
, non_constant_p
, overflow_p
);
2503 if (*non_constant_p
)
2509 /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
2510 match. We want to be less strict for simple *& folding; if we have a
2511 non-const temporary that we access through a const pointer, that should
2512 work. We handle this here rather than change fold_indirect_ref_1
2513 because we're dealing with things like ADDR_EXPR of INTEGER_CST which
2514 don't really make sense outside of constant expression evaluation. Also
2515 we want to allow folding to COMPONENT_REF, which could cause trouble
2516 with TBAA in fold_indirect_ref_1.
2518 Try to keep this function synced with fold_indirect_ref_1. */
2521 cxx_fold_indirect_ref (location_t loc
, tree type
, tree op0
, bool *empty_base
)
2527 subtype
= TREE_TYPE (sub
);
2528 if (!POINTER_TYPE_P (subtype
))
2531 if (TREE_CODE (sub
) == ADDR_EXPR
)
2533 tree op
= TREE_OPERAND (sub
, 0);
2534 tree optype
= TREE_TYPE (op
);
2536 /* *&CONST_DECL -> to the value of the const decl. */
2537 if (TREE_CODE (op
) == CONST_DECL
)
2538 return DECL_INITIAL (op
);
2539 /* *&p => p; make sure to handle *&"str"[cst] here. */
2540 if (same_type_ignoring_top_level_qualifiers_p (optype
, type
)
2541 /* Also handle the case where the desired type is an array of unknown
2542 bounds because the variable has had its bounds deduced since the
2543 ADDR_EXPR was created. */
2544 || (TREE_CODE (type
) == ARRAY_TYPE
2545 && TREE_CODE (optype
) == ARRAY_TYPE
2546 && TYPE_DOMAIN (type
) == NULL_TREE
2547 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (optype
),
2550 tree fop
= fold_read_from_constant_string (op
);
2556 /* *(foo *)&fooarray => fooarray[0] */
2557 else if (TREE_CODE (optype
) == ARRAY_TYPE
2558 && (same_type_ignoring_top_level_qualifiers_p
2559 (type
, TREE_TYPE (optype
))))
2561 tree type_domain
= TYPE_DOMAIN (optype
);
2562 tree min_val
= size_zero_node
;
2563 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
2564 min_val
= TYPE_MIN_VALUE (type_domain
);
2565 return build4_loc (loc
, ARRAY_REF
, type
, op
, min_val
,
2566 NULL_TREE
, NULL_TREE
);
2568 /* *(foo *)&complexfoo => __real__ complexfoo */
2569 else if (TREE_CODE (optype
) == COMPLEX_TYPE
2570 && (same_type_ignoring_top_level_qualifiers_p
2571 (type
, TREE_TYPE (optype
))))
2572 return fold_build1_loc (loc
, REALPART_EXPR
, type
, op
);
2573 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
2574 else if (VECTOR_TYPE_P (optype
)
2575 && (same_type_ignoring_top_level_qualifiers_p
2576 (type
, TREE_TYPE (optype
))))
2578 tree part_width
= TYPE_SIZE (type
);
2579 tree index
= bitsize_int (0);
2580 return fold_build3_loc (loc
, BIT_FIELD_REF
, type
, op
, part_width
, index
);
2582 /* Also handle conversion to an empty base class, which
2583 is represented with a NOP_EXPR. */
2584 else if (is_empty_class (type
)
2585 && CLASS_TYPE_P (optype
)
2586 && DERIVED_FROM_P (type
, optype
))
2591 /* *(foo *)&struct_with_foo_field => COMPONENT_REF */
2592 else if (RECORD_OR_UNION_TYPE_P (optype
))
2594 tree field
= TYPE_FIELDS (optype
);
2595 for (; field
; field
= DECL_CHAIN (field
))
2596 if (TREE_CODE (field
) == FIELD_DECL
2597 && integer_zerop (byte_position (field
))
2598 && (same_type_ignoring_top_level_qualifiers_p
2599 (TREE_TYPE (field
), type
)))
2601 return fold_build3 (COMPONENT_REF
, type
, op
, field
, NULL_TREE
);
2606 else if (TREE_CODE (sub
) == POINTER_PLUS_EXPR
2607 && TREE_CODE (TREE_OPERAND (sub
, 1)) == INTEGER_CST
)
2609 tree op00
= TREE_OPERAND (sub
, 0);
2610 tree op01
= TREE_OPERAND (sub
, 1);
2613 if (TREE_CODE (op00
) == ADDR_EXPR
)
2616 op00
= TREE_OPERAND (op00
, 0);
2617 op00type
= TREE_TYPE (op00
);
2619 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
2620 if (VECTOR_TYPE_P (op00type
)
2621 && (same_type_ignoring_top_level_qualifiers_p
2622 (type
, TREE_TYPE (op00type
))))
2624 HOST_WIDE_INT offset
= tree_to_shwi (op01
);
2625 tree part_width
= TYPE_SIZE (type
);
2626 unsigned HOST_WIDE_INT part_widthi
= tree_to_shwi (part_width
)/BITS_PER_UNIT
;
2627 unsigned HOST_WIDE_INT indexi
= offset
* BITS_PER_UNIT
;
2628 tree index
= bitsize_int (indexi
);
2630 if (offset
/ part_widthi
< TYPE_VECTOR_SUBPARTS (op00type
))
2631 return fold_build3_loc (loc
,
2632 BIT_FIELD_REF
, type
, op00
,
2636 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
2637 else if (TREE_CODE (op00type
) == COMPLEX_TYPE
2638 && (same_type_ignoring_top_level_qualifiers_p
2639 (type
, TREE_TYPE (op00type
))))
2641 tree size
= TYPE_SIZE_UNIT (type
);
2642 if (tree_int_cst_equal (size
, op01
))
2643 return fold_build1_loc (loc
, IMAGPART_EXPR
, type
, op00
);
2645 /* ((foo *)&fooarray)[1] => fooarray[1] */
2646 else if (TREE_CODE (op00type
) == ARRAY_TYPE
2647 && (same_type_ignoring_top_level_qualifiers_p
2648 (type
, TREE_TYPE (op00type
))))
2650 tree type_domain
= TYPE_DOMAIN (op00type
);
2651 tree min_val
= size_zero_node
;
2652 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
2653 min_val
= TYPE_MIN_VALUE (type_domain
);
2654 op01
= size_binop_loc (loc
, EXACT_DIV_EXPR
, op01
,
2655 TYPE_SIZE_UNIT (type
));
2656 op01
= size_binop_loc (loc
, PLUS_EXPR
, op01
, min_val
);
2657 return build4_loc (loc
, ARRAY_REF
, type
, op00
, op01
,
2658 NULL_TREE
, NULL_TREE
);
2660 /* Also handle conversion to an empty base class, which
2661 is represented with a NOP_EXPR. */
2662 else if (is_empty_class (type
)
2663 && CLASS_TYPE_P (op00type
)
2664 && DERIVED_FROM_P (type
, op00type
))
2669 /* ((foo *)&struct_with_foo_field)[1] => COMPONENT_REF */
2670 else if (RECORD_OR_UNION_TYPE_P (op00type
))
2672 tree field
= TYPE_FIELDS (op00type
);
2673 for (; field
; field
= DECL_CHAIN (field
))
2674 if (TREE_CODE (field
) == FIELD_DECL
2675 && tree_int_cst_equal (byte_position (field
), op01
)
2676 && (same_type_ignoring_top_level_qualifiers_p
2677 (TREE_TYPE (field
), type
)))
2679 return fold_build3 (COMPONENT_REF
, type
, op00
,
2686 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
2687 else if (TREE_CODE (TREE_TYPE (subtype
)) == ARRAY_TYPE
2688 && (same_type_ignoring_top_level_qualifiers_p
2689 (type
, TREE_TYPE (TREE_TYPE (subtype
)))))
2692 tree min_val
= size_zero_node
;
2693 tree newsub
= cxx_fold_indirect_ref (loc
, TREE_TYPE (subtype
), sub
, NULL
);
2697 sub
= build1_loc (loc
, INDIRECT_REF
, TREE_TYPE (subtype
), sub
);
2698 type_domain
= TYPE_DOMAIN (TREE_TYPE (sub
));
2699 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
2700 min_val
= TYPE_MIN_VALUE (type_domain
);
2701 return build4_loc (loc
, ARRAY_REF
, type
, sub
, min_val
, NULL_TREE
,
2709 cxx_eval_indirect_ref (const constexpr_ctx
*ctx
, tree t
,
2711 bool *non_constant_p
, bool *overflow_p
)
2713 tree orig_op0
= TREE_OPERAND (t
, 0);
2714 bool empty_base
= false;
2716 /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second
2717 operand is an integer-zero. Otherwise reject the MEM_REF for now. */
2719 if (TREE_CODE (t
) == MEM_REF
2720 && (!TREE_OPERAND (t
, 1) || !integer_zerop (TREE_OPERAND (t
, 1))))
2722 gcc_assert (ctx
->quiet
);
2723 *non_constant_p
= true;
2727 /* First try to simplify it directly. */
2728 tree r
= cxx_fold_indirect_ref (EXPR_LOCATION (t
), TREE_TYPE (t
), orig_op0
,
2732 /* If that didn't work, evaluate the operand first. */
2733 tree op0
= cxx_eval_constant_expression (ctx
, orig_op0
,
2734 /*lval*/false, non_constant_p
,
2736 /* Don't VERIFY_CONSTANT here. */
2737 if (*non_constant_p
)
2740 r
= cxx_fold_indirect_ref (EXPR_LOCATION (t
), TREE_TYPE (t
), op0
,
2744 /* We couldn't fold to a constant value. Make sure it's not
2745 something we should have been able to fold. */
2748 if (TREE_CODE (sub
) == ADDR_EXPR
)
2750 gcc_assert (!same_type_ignoring_top_level_qualifiers_p
2751 (TREE_TYPE (TREE_TYPE (sub
)), TREE_TYPE (t
)));
2752 /* DR 1188 says we don't have to deal with this. */
2754 error ("accessing value of %qE through a %qT glvalue in a "
2755 "constant expression", build_fold_indirect_ref (sub
),
2757 *non_constant_p
= true;
2761 if (lval
&& op0
!= orig_op0
)
2762 return build1 (INDIRECT_REF
, TREE_TYPE (t
), op0
);
2764 VERIFY_CONSTANT (t
);
2769 r
= cxx_eval_constant_expression (ctx
, r
,
2770 lval
, non_constant_p
, overflow_p
);
2771 if (*non_constant_p
)
2774 /* If we're pulling out the value of an empty base, make sure
2775 that the whole object is constant and then return an empty
2777 if (empty_base
&& !lval
)
2779 VERIFY_CONSTANT (r
);
2780 r
= build_constructor (TREE_TYPE (t
), NULL
);
2781 TREE_CONSTANT (r
) = true;
2787 /* Complain about R, a VAR_DECL, not being usable in a constant expression.
2788 Shared between potential_constant_expression and
2789 cxx_eval_constant_expression. */
2792 non_const_var_error (tree r
)
2794 tree type
= TREE_TYPE (r
);
2795 error ("the value of %qD is not usable in a constant "
2797 /* Avoid error cascade. */
2798 if (DECL_INITIAL (r
) == error_mark_node
)
2800 if (DECL_DECLARED_CONSTEXPR_P (r
))
2801 inform (DECL_SOURCE_LOCATION (r
),
2802 "%qD used in its own initializer", r
);
2803 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
2805 if (!CP_TYPE_CONST_P (type
))
2806 inform (DECL_SOURCE_LOCATION (r
),
2807 "%q#D is not const", r
);
2808 else if (CP_TYPE_VOLATILE_P (type
))
2809 inform (DECL_SOURCE_LOCATION (r
),
2810 "%q#D is volatile", r
);
2811 else if (!DECL_INITIAL (r
)
2812 || !TREE_CONSTANT (DECL_INITIAL (r
))
2813 || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
))
2814 inform (DECL_SOURCE_LOCATION (r
),
2815 "%qD was not initialized with a constant "
2822 if (cxx_dialect
>= cxx11
&& !DECL_DECLARED_CONSTEXPR_P (r
))
2823 inform (DECL_SOURCE_LOCATION (r
),
2824 "%qD was not declared %<constexpr%>", r
);
2826 inform (DECL_SOURCE_LOCATION (r
),
2827 "%qD does not have integral or enumeration type",
2832 /* Subroutine of cxx_eval_constant_expression.
2833 Like cxx_eval_unary_expression, except for trinary expressions. */
2836 cxx_eval_trinary_expression (const constexpr_ctx
*ctx
, tree t
,
2838 bool *non_constant_p
, bool *overflow_p
)
2844 for (i
= 0; i
< 3; i
++)
2846 args
[i
] = cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, i
),
2848 non_constant_p
, overflow_p
);
2849 VERIFY_CONSTANT (args
[i
]);
2852 val
= fold_ternary_loc (EXPR_LOCATION (t
), TREE_CODE (t
), TREE_TYPE (t
),
2853 args
[0], args
[1], args
[2]);
2854 if (val
== NULL_TREE
)
2856 VERIFY_CONSTANT (val
);
2861 var_in_constexpr_fn (tree t
)
2863 tree ctx
= DECL_CONTEXT (t
);
2864 return (cxx_dialect
>= cxx14
&& ctx
&& TREE_CODE (ctx
) == FUNCTION_DECL
2865 && DECL_DECLARED_CONSTEXPR_P (ctx
));
2868 /* Evaluate an INIT_EXPR or MODIFY_EXPR. */
2871 cxx_eval_store_expression (const constexpr_ctx
*ctx
, tree t
,
2873 bool *non_constant_p
, bool *overflow_p
)
2875 constexpr_ctx new_ctx
= *ctx
;
2877 tree init
= TREE_OPERAND (t
, 1);
2878 if (TREE_CLOBBER_P (init
))
2879 /* Just ignore clobbers. */
2882 /* First we figure out where we're storing to. */
2883 tree target
= TREE_OPERAND (t
, 0);
2884 tree type
= TREE_TYPE (target
);
2885 target
= cxx_eval_constant_expression (ctx
, target
,
2887 non_constant_p
, overflow_p
);
2888 if (*non_constant_p
)
2891 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (target
), type
))
2893 /* For initialization of an empty base, the original target will be
2894 *(base*)this, which the above evaluation resolves to the object
2895 argument, which has the derived type rather than the base type. In
2896 this situation, just evaluate the initializer and return, since
2897 there's no actual data to store. */
2898 gcc_assert (is_empty_class (type
));
2899 return cxx_eval_constant_expression (ctx
, init
, false,
2900 non_constant_p
, overflow_p
);
2903 /* And then find the underlying variable. */
2904 vec
<tree
,va_gc
> *refs
= make_tree_vector();
2905 tree object
= NULL_TREE
;
2906 for (tree probe
= target
; object
== NULL_TREE
; )
2908 switch (TREE_CODE (probe
))
2913 vec_safe_push (refs
, TREE_OPERAND (probe
, 1));
2914 vec_safe_push (refs
, TREE_TYPE (probe
));
2915 probe
= TREE_OPERAND (probe
, 0);
2923 /* And then find/build up our initializer for the path to the subobject
2924 we're initializing. */
2926 if (DECL_P (object
))
2927 valp
= ctx
->values
->get (object
);
2932 /* A constant-expression cannot modify objects from outside the
2933 constant-expression. */
2935 error ("modification of %qE is not a constant-expression", object
);
2936 *non_constant_p
= true;
2939 type
= TREE_TYPE (object
);
2940 bool no_zero_init
= true;
2942 vec
<tree
,va_gc
> *ctors
= make_tree_vector ();
2943 while (!refs
->is_empty())
2945 if (*valp
== NULL_TREE
)
2947 *valp
= build_constructor (type
, NULL
);
2948 CONSTRUCTOR_NO_IMPLICIT_ZERO (*valp
) = no_zero_init
;
2950 /* If the value of object is already zero-initialized, any new ctors for
2951 subobjects will also be zero-initialized. */
2952 no_zero_init
= CONSTRUCTOR_NO_IMPLICIT_ZERO (*valp
);
2954 vec_safe_push (ctors
, *valp
);
2956 enum tree_code code
= TREE_CODE (type
);
2958 tree index
= refs
->pop();
2960 constructor_elt
*cep
= NULL
;
2961 if (code
== ARRAY_TYPE
)
2964 = find_array_ctor_elt (*valp
, index
, /*insert*/true);
2965 gcc_assert (i
>= 0);
2966 cep
= CONSTRUCTOR_ELT (*valp
, i
);
2967 gcc_assert (TREE_CODE (cep
->index
) != RANGE_EXPR
);
2971 gcc_assert (TREE_CODE (index
) == FIELD_DECL
);
2973 /* We must keep the CONSTRUCTOR's ELTS in FIELD order.
2974 Usually we meet initializers in that order, but it is
2975 possible for base types to be placed not in program
2977 tree fields
= TYPE_FIELDS (DECL_CONTEXT (index
));
2978 unsigned HOST_WIDE_INT idx
;
2981 vec_safe_iterate (CONSTRUCTOR_ELTS (*valp
), idx
, &cep
);
2982 idx
++, fields
= DECL_CHAIN (fields
))
2984 if (index
== cep
->index
)
2987 /* The field we're initializing must be on the field
2988 list. Look to see if it is present before the
2989 field the current ELT initializes. */
2990 for (; fields
!= cep
->index
; fields
= DECL_CHAIN (fields
))
2991 if (index
== fields
)
2995 /* We fell off the end of the CONSTRUCTOR, so insert a new
2996 entry at the end. */
2999 constructor_elt ce
= { index
, NULL_TREE
};
3001 vec_safe_insert (CONSTRUCTOR_ELTS (*valp
), idx
, ce
);
3002 cep
= CONSTRUCTOR_ELT (*valp
, idx
);
3008 release_tree_vector (refs
);
3010 if (AGGREGATE_TYPE_P (type
) || VECTOR_TYPE_P (type
))
3012 /* Create a new CONSTRUCTOR in case evaluation of the initializer
3013 wants to modify it. */
3014 if (*valp
== NULL_TREE
)
3016 *valp
= new_ctx
.ctor
= build_constructor (type
, NULL
);
3017 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx
.ctor
) = no_zero_init
;
3020 new_ctx
.ctor
= *valp
;
3021 new_ctx
.object
= target
;
3024 init
= cxx_eval_constant_expression (&new_ctx
, init
, false,
3025 non_constant_p
, overflow_p
);
3026 /* Don't share a CONSTRUCTOR that might be changed later. */
3027 init
= unshare_expr (init
);
3028 if (target
== object
)
3029 /* The hash table might have moved since the get earlier. */
3030 valp
= ctx
->values
->get (object
);
3032 if (TREE_CODE (init
) == CONSTRUCTOR
)
3034 /* An outer ctx->ctor might be pointing to *valp, so replace
3036 CONSTRUCTOR_ELTS (*valp
) = CONSTRUCTOR_ELTS (init
);
3037 TREE_CONSTANT (*valp
) = TREE_CONSTANT (init
);
3038 TREE_SIDE_EFFECTS (*valp
) = TREE_SIDE_EFFECTS (init
);
3043 /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing
3044 CONSTRUCTORs, if any. */
3047 bool c
= TREE_CONSTANT (init
);
3048 bool s
= TREE_SIDE_EFFECTS (init
);
3050 FOR_EACH_VEC_SAFE_ELT (ctors
, i
, elt
)
3053 TREE_CONSTANT (elt
) = false;
3055 TREE_SIDE_EFFECTS (elt
) = true;
3057 release_tree_vector (ctors
);
3059 if (*non_constant_p
)
3067 /* Evaluate a ++ or -- expression. */
3070 cxx_eval_increment_expression (const constexpr_ctx
*ctx
, tree t
,
3072 bool *non_constant_p
, bool *overflow_p
)
3074 enum tree_code code
= TREE_CODE (t
);
3075 tree type
= TREE_TYPE (t
);
3076 tree op
= TREE_OPERAND (t
, 0);
3077 tree offset
= TREE_OPERAND (t
, 1);
3078 gcc_assert (TREE_CONSTANT (offset
));
3080 /* The operand as an lvalue. */
3081 op
= cxx_eval_constant_expression (ctx
, op
, true,
3082 non_constant_p
, overflow_p
);
3084 /* The operand as an rvalue. */
3085 tree val
= rvalue (op
);
3086 val
= cxx_eval_constant_expression (ctx
, val
, false,
3087 non_constant_p
, overflow_p
);
3088 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
3089 a local array in a constexpr function. */
3090 bool ptr
= POINTER_TYPE_P (TREE_TYPE (val
));
3092 VERIFY_CONSTANT (val
);
3094 /* The modified value. */
3095 bool inc
= (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
);
3097 if (POINTER_TYPE_P (type
))
3099 /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
3100 offset
= convert_to_ptrofftype (offset
);
3102 offset
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (offset
), offset
);
3103 mod
= fold_build2 (POINTER_PLUS_EXPR
, type
, val
, offset
);
3106 mod
= fold_build2 (inc
? PLUS_EXPR
: MINUS_EXPR
, type
, val
, offset
);
3108 VERIFY_CONSTANT (mod
);
3110 /* Storing the modified value. */
3111 tree store
= build2 (MODIFY_EXPR
, type
, op
, mod
);
3112 cxx_eval_constant_expression (ctx
, store
,
3113 true, non_constant_p
, overflow_p
);
3115 /* And the value of the expression. */
3116 if (code
== PREINCREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
3118 /* Prefix ops are lvalues. */
3122 /* But we optimize when the caller wants an rvalue. */
3126 /* Postfix ops are rvalues. */
3130 /* Predicates for the meaning of *jump_target. */
3133 returns (tree
*jump_target
)
3136 && TREE_CODE (*jump_target
) == RETURN_EXPR
;
3140 breaks (tree
*jump_target
)
3143 && TREE_CODE (*jump_target
) == LABEL_DECL
3144 && LABEL_DECL_BREAK (*jump_target
);
3148 continues (tree
*jump_target
)
3151 && TREE_CODE (*jump_target
) == LABEL_DECL
3152 && LABEL_DECL_CONTINUE (*jump_target
);
3156 switches (tree
*jump_target
)
3159 && TREE_CODE (*jump_target
) == INTEGER_CST
;
3162 /* Subroutine of cxx_eval_statement_list. Determine whether the statement
3163 at I matches *jump_target. If we're looking for a case label and we see
3164 the default label, copy I into DEFAULT_LABEL. */
3167 label_matches (tree
*jump_target
, tree_stmt_iterator i
,
3168 tree_stmt_iterator
& default_label
)
3170 tree stmt
= tsi_stmt (i
);
3171 switch (TREE_CODE (*jump_target
))
3174 if (TREE_CODE (stmt
) == LABEL_EXPR
3175 && LABEL_EXPR_LABEL (stmt
) == *jump_target
)
3180 if (TREE_CODE (stmt
) == CASE_LABEL_EXPR
)
3182 if (!CASE_LOW (stmt
))
3184 else if (tree_int_cst_equal (*jump_target
, CASE_LOW (stmt
)))
3195 /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
3196 semantics, for switch, break, continue, and return. */
3199 cxx_eval_statement_list (const constexpr_ctx
*ctx
, tree t
,
3200 bool *non_constant_p
, bool *overflow_p
,
3203 tree_stmt_iterator i
;
3204 tree_stmt_iterator default_label
= tree_stmt_iterator();
3206 /* In a statement-expression we want to return the last value. */
3210 local_target
= NULL_TREE
;
3211 jump_target
= &local_target
;
3213 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
3216 tree stmt
= tsi_stmt (i
);
3219 if (TREE_CODE (stmt
) == STATEMENT_LIST
)
3220 /* The label we want might be inside. */;
3221 else if (label_matches (jump_target
, i
, default_label
))
3223 *jump_target
= NULL_TREE
;
3227 r
= cxx_eval_constant_expression (ctx
, stmt
, false,
3228 non_constant_p
, overflow_p
,
3230 if (*non_constant_p
)
3232 if (returns (jump_target
) || breaks (jump_target
))
3235 if (switches (jump_target
) && !tsi_end_p (default_label
))
3238 *jump_target
= NULL_TREE
;
3244 /* Evaluate a LOOP_EXPR for side-effects. Handles break and return
3245 semantics; continue semantics are covered by cxx_eval_statement_list. */
3248 cxx_eval_loop_expr (const constexpr_ctx
*ctx
, tree t
,
3249 bool *non_constant_p
, bool *overflow_p
,
3252 constexpr_ctx new_ctx
= *ctx
;
3254 tree body
= TREE_OPERAND (t
, 0);
3257 hash_set
<tree
> save_exprs
;
3258 new_ctx
.save_exprs
= &save_exprs
;
3260 cxx_eval_statement_list (&new_ctx
, body
,
3261 non_constant_p
, overflow_p
, jump_target
);
3263 /* Forget saved values of SAVE_EXPRs. */
3264 for (hash_set
<tree
>::iterator iter
= save_exprs
.begin();
3265 iter
!= save_exprs
.end(); ++iter
)
3266 new_ctx
.values
->remove (*iter
);
3268 while (!returns (jump_target
) && !breaks (jump_target
) && !*non_constant_p
);
3270 if (breaks (jump_target
))
3271 *jump_target
= NULL_TREE
;
3276 /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
3280 cxx_eval_switch_expr (const constexpr_ctx
*ctx
, tree t
,
3281 bool *non_constant_p
, bool *overflow_p
,
3284 tree cond
= TREE_OPERAND (t
, 0);
3285 cond
= cxx_eval_constant_expression (ctx
, cond
, false,
3286 non_constant_p
, overflow_p
);
3287 VERIFY_CONSTANT (cond
);
3288 *jump_target
= cond
;
3290 tree body
= TREE_OPERAND (t
, 1);
3291 cxx_eval_statement_list (ctx
, body
,
3292 non_constant_p
, overflow_p
, jump_target
);
3293 if (breaks (jump_target
) || switches (jump_target
))
3294 *jump_target
= NULL_TREE
;
3298 /* Subroutine of cxx_eval_constant_expression.
3299 Attempt to reduce a POINTER_PLUS_EXPR expression T. */
3302 cxx_eval_pointer_plus_expression (const constexpr_ctx
*ctx
, tree t
,
3303 bool lval
, bool *non_constant_p
,
3306 tree orig_type
= TREE_TYPE (t
);
3307 tree op00
= TREE_OPERAND (t
, 0);
3308 tree op01
= TREE_OPERAND (t
, 1);
3309 location_t loc
= EXPR_LOCATION (t
);
3311 op00
= cxx_eval_constant_expression (ctx
, op00
, lval
,
3312 non_constant_p
, overflow_p
);
3315 if (TREE_CODE (op00
) != ADDR_EXPR
)
3318 op01
= cxx_eval_constant_expression (ctx
, op01
, lval
,
3319 non_constant_p
, overflow_p
);
3320 op00
= TREE_OPERAND (op00
, 0);
3322 /* &A[i] p+ j => &A[i + j] */
3323 if (TREE_CODE (op00
) == ARRAY_REF
3324 && TREE_CODE (TREE_OPERAND (op00
, 1)) == INTEGER_CST
3325 && TREE_CODE (op01
) == INTEGER_CST
3326 && TYPE_SIZE_UNIT (TREE_TYPE (op00
))
3327 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (op00
))) == INTEGER_CST
)
3329 tree type
= TREE_TYPE (op00
);
3330 t
= fold_convert_loc (loc
, ssizetype
, TREE_OPERAND (op00
, 1));
3331 tree nelts
= array_type_nelts_top (TREE_TYPE (TREE_OPERAND (op00
, 0)));
3332 /* Don't fold an out-of-bound access. */
3333 if (!tree_int_cst_le (t
, nelts
))
3335 op01
= cp_fold_convert (ssizetype
, op01
);
3336 /* Don't fold if op01 can't be divided exactly by TYPE_SIZE_UNIT.
3337 constexpr int A[1]; ... (char *)&A[0] + 1 */
3338 if (!integer_zerop (fold_build2_loc (loc
, TRUNC_MOD_EXPR
, sizetype
,
3339 op01
, TYPE_SIZE_UNIT (type
))))
3341 /* Make sure to treat the second operand of POINTER_PLUS_EXPR
3343 op01
= fold_build2_loc (loc
, EXACT_DIV_EXPR
, ssizetype
, op01
,
3344 TYPE_SIZE_UNIT (type
));
3345 t
= size_binop_loc (loc
, PLUS_EXPR
, op01
, t
);
3346 t
= build4_loc (loc
, ARRAY_REF
, type
, TREE_OPERAND (op00
, 0),
3347 t
, NULL_TREE
, NULL_TREE
);
3348 t
= cp_build_addr_expr (t
, tf_warning_or_error
);
3349 t
= cp_fold_convert (orig_type
, t
);
3350 return cxx_eval_constant_expression (ctx
, t
, lval
, non_constant_p
,
3357 /* Attempt to reduce the expression T to a constant value.
3358 On failure, issue diagnostic and return error_mark_node. */
3359 /* FIXME unify with c_fully_fold */
3360 /* FIXME overflow_p is too global */
3363 cxx_eval_constant_expression (const constexpr_ctx
*ctx
, tree t
,
3365 bool *non_constant_p
, bool *overflow_p
,
3368 constexpr_ctx new_ctx
;
3371 if (t
== error_mark_node
)
3373 *non_constant_p
= true;
3376 if (CONSTANT_CLASS_P (t
))
3378 if (TREE_OVERFLOW (t
))
3381 permerror (input_location
, "overflow in constant expression");
3382 if (!flag_permissive
|| ctx
->quiet
)
3388 switch (TREE_CODE (t
))
3393 /* We ask for an rvalue for the RESULT_DECL when indirecting
3394 through an invisible reference, or in named return value
3396 return (*ctx
->values
->get (t
));
3400 /* We used to not check lval for CONST_DECL, but darwin.c uses
3401 CONST_DECL for aggregate constants. */
3405 r
= decl_really_constant_value (t
);
3407 r
= decl_constant_value (t
);
3408 if (TREE_CODE (r
) == TARGET_EXPR
3409 && TREE_CODE (TARGET_EXPR_INITIAL (r
)) == CONSTRUCTOR
)
3410 r
= TARGET_EXPR_INITIAL (r
);
3412 if (tree
*p
= ctx
->values
->get (r
))
3413 if (*p
!= NULL_TREE
)
3418 non_const_var_error (r
);
3419 *non_constant_p
= true;
3427 case CASE_LABEL_EXPR
:
3431 if (lval
&& TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
3433 else if (tree
*p
= ctx
->values
->get (r
))
3436 /* Defer in case this is only used for its type. */;
3437 else if (TREE_CODE (TREE_TYPE (t
)) == REFERENCE_TYPE
)
3438 /* Defer, there's no lvalue->rvalue conversion. */;
3439 else if (is_empty_class (TREE_TYPE (t
)))
3441 /* If the class is empty, we aren't actually loading anything. */
3442 r
= build_constructor (TREE_TYPE (t
), NULL
);
3443 TREE_CONSTANT (r
) = true;
3448 error ("%qE is not a constant expression", t
);
3449 *non_constant_p
= true;
3454 case AGGR_INIT_EXPR
:
3455 r
= cxx_eval_call_expression (ctx
, t
, lval
,
3456 non_constant_p
, overflow_p
);
3461 r
= DECL_EXPR_DECL (t
);
3462 if (AGGREGATE_TYPE_P (TREE_TYPE (r
))
3463 || VECTOR_TYPE_P (TREE_TYPE (r
)))
3467 new_ctx
.ctor
= build_constructor (TREE_TYPE (r
), NULL
);
3468 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx
.ctor
) = true;
3469 new_ctx
.values
->put (r
, new_ctx
.ctor
);
3473 if (tree init
= DECL_INITIAL (r
))
3475 init
= cxx_eval_constant_expression (ctx
, init
,
3477 non_constant_p
, overflow_p
);
3478 /* Don't share a CONSTRUCTOR that might be changed. */
3479 init
= unshare_expr (init
);
3480 ctx
->values
->put (r
, init
);
3482 else if (ctx
== &new_ctx
)
3483 /* We gave it a CONSTRUCTOR above. */;
3485 ctx
->values
->put (r
, NULL_TREE
);
3490 if (!literal_type_p (TREE_TYPE (t
)))
3494 error ("temporary of non-literal type %qT in a "
3495 "constant expression", TREE_TYPE (t
));
3496 explain_non_literal_class (TREE_TYPE (t
));
3498 *non_constant_p
= true;
3501 if ((AGGREGATE_TYPE_P (TREE_TYPE (t
)) || VECTOR_TYPE_P (TREE_TYPE (t
))))
3503 /* We're being expanded without an explicit target, so start
3504 initializing a new object; expansion with an explicit target
3505 strips the TARGET_EXPR before we get here. */
3507 new_ctx
.ctor
= build_constructor (TREE_TYPE (t
), NULL
);
3508 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx
.ctor
) = true;
3509 new_ctx
.object
= TARGET_EXPR_SLOT (t
);
3510 ctx
->values
->put (new_ctx
.object
, new_ctx
.ctor
);
3513 /* Pass false for 'lval' because this indicates
3514 initialization of a temporary. */
3515 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
3517 non_constant_p
, overflow_p
);
3518 if (!*non_constant_p
)
3519 /* Adjust the type of the result to the type of the temporary. */
3520 r
= adjust_temp_type (TREE_TYPE (t
), r
);
3523 tree slot
= TARGET_EXPR_SLOT (t
);
3524 r
= unshare_expr (r
);
3525 ctx
->values
->put (slot
, r
);
3532 r
= cxx_eval_store_expression (ctx
, t
, lval
,
3533 non_constant_p
, overflow_p
);
3537 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
3539 non_constant_p
, overflow_p
);
3543 if (TREE_OPERAND (t
, 0) != NULL_TREE
)
3544 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
3546 non_constant_p
, overflow_p
);
3551 /* Avoid evaluating a SAVE_EXPR more than once. */
3552 if (tree
*p
= ctx
->values
->get (t
))
3556 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), false,
3557 non_constant_p
, overflow_p
);
3558 ctx
->values
->put (t
, r
);
3559 if (ctx
->save_exprs
)
3560 ctx
->save_exprs
->add (t
);
3564 case NON_LVALUE_EXPR
:
3565 case TRY_CATCH_EXPR
:
3567 case CLEANUP_POINT_EXPR
:
3568 case MUST_NOT_THROW_EXPR
:
3571 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
3573 non_constant_p
, overflow_p
,
3577 case TRY_FINALLY_EXPR
:
3578 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), lval
,
3579 non_constant_p
, overflow_p
,
3581 if (!*non_constant_p
)
3582 /* Also evaluate the cleanup. */
3583 cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1), true,
3584 non_constant_p
, overflow_p
,
3588 /* These differ from cxx_eval_unary_expression in that this doesn't
3589 check for a constant operand or result; an address can be
3590 constant without its operand being, and vice versa. */
3593 r
= cxx_eval_indirect_ref (ctx
, t
, lval
,
3594 non_constant_p
, overflow_p
);
3599 tree oldop
= TREE_OPERAND (t
, 0);
3600 tree op
= cxx_eval_constant_expression (ctx
, oldop
,
3602 non_constant_p
, overflow_p
);
3603 /* Don't VERIFY_CONSTANT here. */
3604 if (*non_constant_p
)
3606 gcc_checking_assert (TREE_CODE (op
) != CONSTRUCTOR
);
3607 /* This function does more aggressive folding than fold itself. */
3608 r
= build_fold_addr_expr_with_type (op
, TREE_TYPE (t
));
3609 if (TREE_CODE (r
) == ADDR_EXPR
&& TREE_OPERAND (r
, 0) == oldop
)
3617 case FIX_TRUNC_EXPR
:
3622 case TRUTH_NOT_EXPR
:
3623 case FIXED_CONVERT_EXPR
:
3624 r
= cxx_eval_unary_expression (ctx
, t
, lval
,
3625 non_constant_p
, overflow_p
);
3629 r
= fold_sizeof_expr (t
);
3630 VERIFY_CONSTANT (r
);
3635 /* check_return_expr sometimes wraps a TARGET_EXPR in a
3636 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
3637 introduced by build_call_a. */
3638 tree op0
= TREE_OPERAND (t
, 0);
3639 tree op1
= TREE_OPERAND (t
, 1);
3641 if ((TREE_CODE (op0
) == TARGET_EXPR
&& op1
== TARGET_EXPR_SLOT (op0
))
3642 || TREE_CODE (op1
) == EMPTY_CLASS_EXPR
)
3643 r
= cxx_eval_constant_expression (ctx
, op0
,
3644 lval
, non_constant_p
, overflow_p
,
3648 /* Check that the LHS is constant and then discard it. */
3649 cxx_eval_constant_expression (ctx
, op0
,
3650 true, non_constant_p
, overflow_p
,
3652 op1
= TREE_OPERAND (t
, 1);
3653 r
= cxx_eval_constant_expression (ctx
, op1
,
3654 lval
, non_constant_p
, overflow_p
,
3660 case POINTER_PLUS_EXPR
:
3661 r
= cxx_eval_pointer_plus_expression (ctx
, t
, lval
, non_constant_p
,
3665 /* else fall through */
3670 case TRUNC_DIV_EXPR
:
3672 case FLOOR_DIV_EXPR
:
3673 case ROUND_DIV_EXPR
:
3674 case TRUNC_MOD_EXPR
:
3676 case ROUND_MOD_EXPR
:
3678 case EXACT_DIV_EXPR
:
3688 case TRUTH_XOR_EXPR
:
3695 case UNORDERED_EXPR
:
3705 r
= cxx_eval_binary_expression (ctx
, t
, lval
,
3706 non_constant_p
, overflow_p
);
3709 /* fold can introduce non-IF versions of these; still treat them as
3710 short-circuiting. */
3711 case TRUTH_AND_EXPR
:
3712 case TRUTH_ANDIF_EXPR
:
3713 r
= cxx_eval_logical_expression (ctx
, t
, boolean_false_node
,
3716 non_constant_p
, overflow_p
);
3720 case TRUTH_ORIF_EXPR
:
3721 r
= cxx_eval_logical_expression (ctx
, t
, boolean_true_node
,
3724 non_constant_p
, overflow_p
);
3728 r
= cxx_eval_array_reference (ctx
, t
, lval
,
3729 non_constant_p
, overflow_p
);
3733 if (is_overloaded_fn (t
))
3735 /* We can only get here in checking mode via
3736 build_non_dependent_expr, because any expression that
3737 calls or takes the address of the function will have
3738 pulled a FUNCTION_DECL out of the COMPONENT_REF. */
3739 gcc_checking_assert (ctx
->quiet
|| errorcount
);
3740 *non_constant_p
= true;
3743 r
= cxx_eval_component_reference (ctx
, t
, lval
,
3744 non_constant_p
, overflow_p
);
3748 r
= cxx_eval_bit_field_ref (ctx
, t
, lval
,
3749 non_constant_p
, overflow_p
);
3754 r
= cxx_eval_conditional_expression (ctx
, t
, lval
,
3755 non_constant_p
, overflow_p
,
3760 if (TREE_CONSTANT (t
))
3762 /* Don't re-process a constant CONSTRUCTOR, but do fold it to
3763 VECTOR_CST if applicable. */
3764 /* FIXME after GCC 6 branches, make the verify unconditional. */
3766 verify_constructor_flags (t
);
3768 recompute_constructor_flags (t
);
3769 if (TREE_CONSTANT (t
))
3772 r
= cxx_eval_bare_aggregate (ctx
, t
, lval
,
3773 non_constant_p
, overflow_p
);
3777 /* We can get this in a defaulted constructor for a class with a
3778 non-static data member of array type. Either the initializer will
3779 be NULL, meaning default-initialization, or it will be an lvalue
3780 or xvalue of the same type, meaning direct-initialization from the
3781 corresponding member. */
3782 r
= cxx_eval_vec_init (ctx
, t
, lval
,
3783 non_constant_p
, overflow_p
);
3788 r
= cxx_eval_trinary_expression (ctx
, t
, lval
,
3789 non_constant_p
, overflow_p
);
3793 case VIEW_CONVERT_EXPR
:
3795 case UNARY_PLUS_EXPR
:
3797 enum tree_code tcode
= TREE_CODE (t
);
3798 tree oldop
= TREE_OPERAND (t
, 0);
3800 tree op
= cxx_eval_constant_expression (ctx
, oldop
,
3802 non_constant_p
, overflow_p
);
3803 if (*non_constant_p
)
3805 tree type
= TREE_TYPE (t
);
3806 if (TREE_CODE (op
) == PTRMEM_CST
3807 && !TYPE_PTRMEM_P (type
))
3808 op
= cplus_expand_constant (op
);
3809 if (TREE_CODE (op
) == PTRMEM_CST
&& tcode
== NOP_EXPR
)
3811 if (same_type_ignoring_top_level_qualifiers_p (type
,
3817 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
3818 "a reinterpret_cast is not a constant-expression");
3819 *non_constant_p
= true;
3823 if (POINTER_TYPE_P (type
)
3824 && TREE_CODE (op
) == INTEGER_CST
3825 && !integer_zerop (op
))
3828 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
3829 "reinterpret_cast from integer to pointer");
3830 *non_constant_p
= true;
3833 if (op
== oldop
&& tcode
!= UNARY_PLUS_EXPR
)
3834 /* We didn't fold at the top so we could check for ptr-int
3837 if (tcode
== UNARY_PLUS_EXPR
)
3838 r
= fold_convert (TREE_TYPE (t
), op
);
3840 r
= fold_build1 (tcode
, type
, op
);
3841 /* Conversion of an out-of-range value has implementation-defined
3842 behavior; the language considers it different from arithmetic
3843 overflow, which is undefined. */
3844 if (TREE_OVERFLOW_P (r
) && !TREE_OVERFLOW_P (op
))
3845 TREE_OVERFLOW (r
) = false;
3849 case EMPTY_CLASS_EXPR
:
3850 /* This is good enough for a function argument that might not get
3851 used, and they can't do anything with it, so just return it. */
3854 case STATEMENT_LIST
:
3856 new_ctx
.ctor
= new_ctx
.object
= NULL_TREE
;
3857 return cxx_eval_statement_list (&new_ctx
, t
,
3858 non_constant_p
, overflow_p
, jump_target
);
3861 return cxx_eval_constant_expression (ctx
, BIND_EXPR_BODY (t
),
3863 non_constant_p
, overflow_p
,
3866 case PREINCREMENT_EXPR
:
3867 case POSTINCREMENT_EXPR
:
3868 case PREDECREMENT_EXPR
:
3869 case POSTDECREMENT_EXPR
:
3870 return cxx_eval_increment_expression (ctx
, t
,
3871 lval
, non_constant_p
, overflow_p
);
3877 case VEC_DELETE_EXPR
:
3880 /* GCC internal stuff. */
3883 case WITH_CLEANUP_EXPR
:
3884 case NON_DEPENDENT_EXPR
:
3888 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
3889 "expression %qE is not a constant-expression", t
);
3890 *non_constant_p
= true;
3893 case PLACEHOLDER_EXPR
:
3894 if (!ctx
|| !ctx
->ctor
|| (lval
&& !ctx
->object
)
3895 || !(same_type_ignoring_top_level_qualifiers_p
3896 (TREE_TYPE (t
), TREE_TYPE (ctx
->ctor
))))
3898 /* A placeholder without a referent. We can get here when
3899 checking whether NSDMIs are noexcept, or in massage_init_elt;
3900 just say it's non-constant for now. */
3901 gcc_assert (ctx
->quiet
);
3902 *non_constant_p
= true;
3907 /* Use of the value or address of the current object. We could
3908 use ctx->object unconditionally, but using ctx->ctor when we
3909 can is a minor optimization. */
3910 tree ctor
= lval
? ctx
->object
: ctx
->ctor
;
3911 return cxx_eval_constant_expression
3913 non_constant_p
, overflow_p
);
3918 *jump_target
= TREE_OPERAND (t
, 0);
3919 gcc_assert (breaks (jump_target
) || continues (jump_target
));
3923 cxx_eval_loop_expr (ctx
, t
,
3924 non_constant_p
, overflow_p
, jump_target
);
3928 cxx_eval_switch_expr (ctx
, t
,
3929 non_constant_p
, overflow_p
, jump_target
);
3933 /* It's possible to get a requires-expression in a constant
3934 expression. For example:
3936 template<typename T> concept bool C() {
3937 return requires (T t) { t; };
3940 template<typename T> requires !C<T>() void f(T);
3942 Normalization leaves f with the associated constraint
3943 '!requires (T t) { ... }' which is not transformed into
3945 if (!processing_template_decl
)
3946 return evaluate_constraint_expression (t
, NULL_TREE
);
3948 *non_constant_p
= true;
3952 if (STATEMENT_CODE_P (TREE_CODE (t
)))
3954 /* This function doesn't know how to deal with pre-genericize
3955 statements; this can only happen with statement-expressions,
3956 so for now just fail. */
3958 error_at (EXPR_LOCATION (t
),
3959 "statement is not a constant-expression");
3962 internal_error ("unexpected expression %qE of kind %s", t
,
3963 get_tree_code_name (TREE_CODE (t
)));
3964 *non_constant_p
= true;
3968 if (r
== error_mark_node
)
3969 *non_constant_p
= true;
3971 if (*non_constant_p
)
3978 cxx_eval_outermost_constant_expr (tree t
, bool allow_non_constant
,
3979 bool strict
= true, tree object
= NULL_TREE
)
3981 bool non_constant_p
= false;
3982 bool overflow_p
= false;
3983 hash_map
<tree
,tree
> map
;
3985 constexpr_ctx ctx
= { NULL
, &map
, NULL
, NULL
, NULL
,
3986 allow_non_constant
, strict
};
3988 tree type
= initialized_type (t
);
3990 if (AGGREGATE_TYPE_P (type
) || VECTOR_TYPE_P (type
))
3992 /* In C++14 an NSDMI can participate in aggregate initialization,
3993 and can refer to the address of the object being initialized, so
3994 we need to pass in the relevant VAR_DECL if we want to do the
3995 evaluation in a single pass. The evaluation will dynamically
3996 update ctx.values for the VAR_DECL. We use the same strategy
3997 for C++11 constexpr constructors that refer to the object being
3999 ctx
.ctor
= build_constructor (type
, NULL
);
4000 CONSTRUCTOR_NO_IMPLICIT_ZERO (ctx
.ctor
) = true;
4003 if (TREE_CODE (t
) == TARGET_EXPR
)
4004 object
= TARGET_EXPR_SLOT (t
);
4005 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
)
4006 object
= AGGR_INIT_EXPR_SLOT (t
);
4008 ctx
.object
= object
;
4010 gcc_assert (same_type_ignoring_top_level_qualifiers_p
4011 (type
, TREE_TYPE (object
)));
4012 if (object
&& DECL_P (object
))
4013 map
.put (object
, ctx
.ctor
);
4014 if (TREE_CODE (r
) == TARGET_EXPR
)
4015 /* Avoid creating another CONSTRUCTOR when we expand the
4017 r
= TARGET_EXPR_INITIAL (r
);
4020 r
= cxx_eval_constant_expression (&ctx
, r
,
4021 false, &non_constant_p
, &overflow_p
);
4023 verify_constant (r
, allow_non_constant
, &non_constant_p
, &overflow_p
);
4025 /* Mutable logic is a bit tricky: we want to allow initialization of
4026 constexpr variables with mutable members, but we can't copy those
4027 members to another constexpr variable. */
4028 if (TREE_CODE (r
) == CONSTRUCTOR
4029 && CONSTRUCTOR_MUTABLE_POISON (r
))
4031 if (!allow_non_constant
)
4032 error ("%qE is not a constant expression because it refers to "
4033 "mutable subobjects of %qT", t
, type
);
4034 non_constant_p
= true;
4037 /* Technically we should check this for all subexpressions, but that
4038 runs into problems with our internal representation of pointer
4039 subtraction and the 5.19 rules are still in flux. */
4040 if (CONVERT_EXPR_CODE_P (TREE_CODE (r
))
4041 && ARITHMETIC_TYPE_P (TREE_TYPE (r
))
4042 && TREE_CODE (TREE_OPERAND (r
, 0)) == ADDR_EXPR
)
4044 if (!allow_non_constant
)
4045 error ("conversion from pointer type %qT "
4046 "to arithmetic type %qT in a constant-expression",
4047 TREE_TYPE (TREE_OPERAND (r
, 0)), TREE_TYPE (r
));
4048 non_constant_p
= true;
4051 if (!non_constant_p
&& overflow_p
)
4052 non_constant_p
= true;
4054 if (non_constant_p
&& !allow_non_constant
)
4055 return error_mark_node
;
4056 else if (non_constant_p
&& TREE_CONSTANT (r
))
4058 /* This isn't actually constant, so unset TREE_CONSTANT. */
4061 else if (TREE_CODE (r
) == CONSTRUCTOR
)
4062 r
= build1 (VIEW_CONVERT_EXPR
, TREE_TYPE (r
), r
);
4064 r
= build_nop (TREE_TYPE (r
), r
);
4065 TREE_CONSTANT (r
) = false;
4067 else if (non_constant_p
|| r
== t
)
4070 if (TREE_CODE (r
) == CONSTRUCTOR
&& CLASS_TYPE_P (TREE_TYPE (r
)))
4072 if (TREE_CODE (t
) == TARGET_EXPR
4073 && TARGET_EXPR_INITIAL (t
) == r
)
4077 r
= get_target_expr (r
);
4078 TREE_CONSTANT (r
) = true;
4086 /* Returns true if T is a valid subexpression of a constant expression,
4087 even if it isn't itself a constant expression. */
4090 is_sub_constant_expr (tree t
)
4092 bool non_constant_p
= false;
4093 bool overflow_p
= false;
4094 hash_map
<tree
, tree
> map
;
4096 constexpr_ctx ctx
= { NULL
, &map
, NULL
, NULL
, NULL
, true, true };
4098 cxx_eval_constant_expression (&ctx
, t
, false, &non_constant_p
,
4100 return !non_constant_p
&& !overflow_p
;
4103 /* If T represents a constant expression returns its reduced value.
4104 Otherwise return error_mark_node. If T is dependent, then
4108 cxx_constant_value (tree t
, tree decl
)
4110 return cxx_eval_outermost_constant_expr (t
, false, true, decl
);
4113 /* Helper routine for fold_simple function. Either return simplified
4114 expression T, otherwise NULL_TREE.
4115 In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold
4116 even if we are within template-declaration. So be careful on call, as in
4117 such case types can be undefined. */
4120 fold_simple_1 (tree t
)
4123 enum tree_code code
= TREE_CODE (t
);
4135 return fold_sizeof_expr (t
);
4143 case TRUTH_NOT_EXPR
:
4145 case VIEW_CONVERT_EXPR
:
4148 case FIX_TRUNC_EXPR
:
4149 case FIXED_CONVERT_EXPR
:
4150 case ADDR_SPACE_CONVERT_EXPR
:
4152 op1
= TREE_OPERAND (t
, 0);
4154 t
= const_unop (code
, TREE_TYPE (t
), op1
);
4158 if (CONVERT_EXPR_CODE_P (code
)
4159 && TREE_OVERFLOW_P (t
) && !TREE_OVERFLOW_P (op1
))
4160 TREE_OVERFLOW (t
) = false;
4168 /* If T is a simple constant expression, returns its simplified value.
4169 Otherwise returns T. In contrast to maybe_constant_value do we
4170 simplify only few operations on constant-expressions, and we don't
4171 try to simplify constexpressions. */
4174 fold_simple (tree t
)
4177 if (processing_template_decl
)
4180 r
= fold_simple_1 (t
);
4187 /* If T is a constant expression, returns its reduced value.
4188 Otherwise, if T does not have TREE_CONSTANT set, returns T.
4189 Otherwise, returns a version of T without TREE_CONSTANT. */
4192 maybe_constant_value_1 (tree t
, tree decl
)
4196 if (instantiation_dependent_expression_p (t
)
4197 || type_unknown_p (t
)
4198 || BRACE_ENCLOSED_INITIALIZER_P (t
)
4199 || !potential_constant_expression (t
))
4201 if (TREE_OVERFLOW_P (t
))
4203 t
= build_nop (TREE_TYPE (t
), t
);
4204 TREE_CONSTANT (t
) = false;
4209 r
= cxx_eval_outermost_constant_expr (t
, true, true, decl
);
4210 gcc_checking_assert (r
== t
4211 || CONVERT_EXPR_P (t
)
4212 || TREE_CODE (t
) == VIEW_CONVERT_EXPR
4213 || (TREE_CONSTANT (t
) && !TREE_CONSTANT (r
))
4214 || !cp_tree_equal (r
, t
));
4218 static GTY((cache
, deletable
)) cache_map cv_cache
;
4220 /* If T is a constant expression, returns its reduced value.
4221 Otherwise, if T does not have TREE_CONSTANT set, returns T.
4222 Otherwise, returns a version of T without TREE_CONSTANT. */
4225 maybe_constant_value (tree t
, tree decl
)
4227 tree ret
= cv_cache
.get (t
);
4230 ret
= maybe_constant_value_1 (t
, decl
);
4231 cv_cache
.put (t
, ret
);
4236 /* Dispose of the whole CV_CACHE and FOLD_CACHE. */
4239 clear_cv_and_fold_caches (void)
4241 gt_cleare_cache (cv_cache
);
4242 clear_fold_cache ();
4245 /* Like maybe_constant_value but first fully instantiate the argument.
4247 Note: this is equivalent to instantiate_non_dependent_expr_sfinae
4248 (t, tf_none) followed by maybe_constant_value but is more efficient,
4249 because calls instantiation_dependent_expression_p and
4250 potential_constant_expression at most once. */
4253 fold_non_dependent_expr (tree t
)
4258 /* If we're in a template, but T isn't value dependent, simplify
4259 it. We're supposed to treat:
4261 template <typename T> void f(T[1 + 1]);
4262 template <typename T> void f(T[2]);
4264 as two declarations of the same function, for example. */
4265 if (processing_template_decl
)
4267 if (!instantiation_dependent_expression_p (t
)
4268 && potential_constant_expression (t
))
4270 processing_template_decl_sentinel s
;
4271 t
= instantiate_non_dependent_expr_internal (t
, tf_none
);
4273 if (type_unknown_p (t
)
4274 || BRACE_ENCLOSED_INITIALIZER_P (t
))
4276 if (TREE_OVERFLOW_P (t
))
4278 t
= build_nop (TREE_TYPE (t
), t
);
4279 TREE_CONSTANT (t
) = false;
4284 tree r
= cxx_eval_outermost_constant_expr (t
, true, true, NULL_TREE
);
4285 /* cp_tree_equal looks through NOPs, so allow them. */
4286 gcc_checking_assert (r
== t
4287 || CONVERT_EXPR_P (t
)
4288 || TREE_CODE (t
) == VIEW_CONVERT_EXPR
4289 || (TREE_CONSTANT (t
) && !TREE_CONSTANT (r
))
4290 || !cp_tree_equal (r
, t
));
4293 else if (TREE_OVERFLOW_P (t
))
4295 t
= build_nop (TREE_TYPE (t
), t
);
4296 TREE_CONSTANT (t
) = false;
4301 return maybe_constant_value (t
);
4304 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
4305 than wrapped in a TARGET_EXPR. */
4308 maybe_constant_init (tree t
, tree decl
)
4312 if (TREE_CODE (t
) == EXPR_STMT
)
4313 t
= TREE_OPERAND (t
, 0);
4314 if (TREE_CODE (t
) == CONVERT_EXPR
4315 && VOID_TYPE_P (TREE_TYPE (t
)))
4316 t
= TREE_OPERAND (t
, 0);
4317 if (TREE_CODE (t
) == INIT_EXPR
)
4318 t
= TREE_OPERAND (t
, 1);
4319 if (instantiation_dependent_expression_p (t
)
4320 || type_unknown_p (t
)
4321 || BRACE_ENCLOSED_INITIALIZER_P (t
)
4322 || !potential_static_init_expression (t
))
4323 /* Don't try to evaluate it. */;
4325 t
= cxx_eval_outermost_constant_expr (t
, true, false, decl
);
4326 if (TREE_CODE (t
) == TARGET_EXPR
)
4328 tree init
= TARGET_EXPR_INITIAL (t
);
4329 if (TREE_CODE (init
) == CONSTRUCTOR
)
4336 /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
4337 /* Return true if the object referred to by REF has automatic or thread
4340 enum { ck_ok
, ck_bad
, ck_unknown
};
4342 check_automatic_or_tls (tree ref
)
4345 HOST_WIDE_INT bitsize
, bitpos
;
4347 int volatilep
= 0, unsignedp
= 0;
4348 tree decl
= get_inner_reference (ref
, &bitsize
, &bitpos
, &offset
,
4349 &mode
, &unsignedp
, &volatilep
, false);
4352 /* If there isn't a decl in the middle, we don't know the linkage here,
4353 and this isn't a constant expression anyway. */
4356 dk
= decl_storage_duration (decl
);
4357 return (dk
== dk_auto
|| dk
== dk_thread
) ? ck_bad
: ck_ok
;
4361 /* Return true if T denotes a potentially constant expression. Issue
4362 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
4363 an lvalue-rvalue conversion is implied.
4365 C++0x [expr.const] used to say
4367 6 An expression is a potential constant expression if it is
4368 a constant expression where all occurrences of function
4369 parameters are replaced by arbitrary constant expressions
4370 of the appropriate type.
4372 2 A conditional expression is a constant expression unless it
4373 involves one of the following as a potentially evaluated
4374 subexpression (3.2), but subexpressions of logical AND (5.14),
4375 logical OR (5.15), and conditional (5.16) operations that are
4376 not evaluated are not considered. */
4379 potential_constant_expression_1 (tree t
, bool want_rval
, bool strict
,
4380 tsubst_flags_t flags
)
4382 #define RECUR(T,RV) potential_constant_expression_1 ((T), (RV), strict, flags)
4383 enum { any
= false, rval
= true };
4387 if (t
== error_mark_node
)
4391 if (TREE_THIS_VOLATILE (t
) && !DECL_P (t
))
4393 if (flags
& tf_error
)
4394 error ("expression %qE has side-effects", t
);
4397 if (CONSTANT_CLASS_P (t
))
4400 switch (TREE_CODE (t
))
4406 case TEMPLATE_ID_EXPR
:
4409 case CASE_LABEL_EXPR
:
4415 case TEMPLATE_PARM_INDEX
:
4417 case IDENTIFIER_NODE
:
4418 case USERDEF_LITERAL
:
4419 /* We can see a FIELD_DECL in a pointer-to-member expression. */
4425 case PLACEHOLDER_EXPR
:
4431 case AGGR_INIT_EXPR
:
4433 /* -- an invocation of a function other than a constexpr function
4434 or a constexpr constructor. */
4436 tree fun
= get_function_named_in_call (t
);
4437 const int nargs
= call_expr_nargs (t
);
4440 if (fun
== NULL_TREE
)
4442 if (TREE_CODE (t
) == CALL_EXPR
4443 && CALL_EXPR_FN (t
) == NULL_TREE
)
4444 switch (CALL_EXPR_IFN (t
))
4446 /* These should be ignored, they are optimized away from
4447 constexpr functions. */
4448 case IFN_UBSAN_NULL
:
4449 case IFN_UBSAN_BOUNDS
:
4450 case IFN_UBSAN_VPTR
:
4455 /* fold_call_expr can't do anything with IFN calls. */
4456 if (flags
& tf_error
)
4457 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
4458 "call to internal function");
4461 if (is_overloaded_fn (fun
))
4463 if (TREE_CODE (fun
) == FUNCTION_DECL
)
4465 if (builtin_valid_in_constant_expr_p (fun
))
4467 if (!DECL_DECLARED_CONSTEXPR_P (fun
)
4468 /* Allow any built-in function; if the expansion
4469 isn't constant, we'll deal with that then. */
4470 && !is_builtin_fn (fun
))
4472 if (flags
& tf_error
)
4474 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
4475 "call to non-constexpr function %qD", fun
);
4476 explain_invalid_constexpr_fn (fun
);
4480 /* A call to a non-static member function takes the address
4481 of the object as the first argument. But in a constant
4482 expression the address will be folded away, so look
4484 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun
)
4485 && !DECL_CONSTRUCTOR_P (fun
))
4487 tree x
= get_nth_callarg (t
, 0);
4488 if (is_this_parameter (x
))
4490 else if (!RECUR (x
, rval
))
4497 if (!RECUR (fun
, true))
4499 fun
= get_first_fn (fun
);
4501 /* Skip initial arguments to base constructors. */
4502 if (DECL_BASE_CONSTRUCTOR_P (fun
))
4503 i
= num_artificial_parms_for (fun
);
4504 fun
= DECL_ORIGIN (fun
);
4508 if (RECUR (fun
, rval
))
4509 /* Might end up being a constant function pointer. */;
4513 for (; i
< nargs
; ++i
)
4515 tree x
= get_nth_callarg (t
, i
);
4516 /* In a template, reference arguments haven't been converted to
4517 REFERENCE_TYPE and we might not even know if the parameter
4518 is a reference, so accept lvalue constants too. */
4519 bool rv
= processing_template_decl
? any
: rval
;
4526 case NON_LVALUE_EXPR
:
4527 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
4528 -- an lvalue of integral type that refers to a non-volatile
4529 const variable or static data member initialized with
4530 constant expressions, or
4532 -- an lvalue of literal type that refers to non-volatile
4533 object defined with constexpr, or that refers to a
4534 sub-object of such an object; */
4535 return RECUR (TREE_OPERAND (t
, 0), rval
);
4539 && !decl_constant_var_p (t
)
4541 || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t
))
4542 || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t
))
4543 && !var_in_constexpr_fn (t
)
4544 && !type_dependent_expression_p (t
))
4546 if (flags
& tf_error
)
4547 non_const_var_error (t
);
4554 case VIEW_CONVERT_EXPR
:
4555 /* -- a reinterpret_cast. FIXME not implemented, and this rule
4556 may change to something more specific to type-punning (DR 1312). */
4558 tree from
= TREE_OPERAND (t
, 0);
4559 if (POINTER_TYPE_P (TREE_TYPE (t
))
4560 && TREE_CODE (from
) == INTEGER_CST
4561 && !integer_zerop (from
))
4563 if (flags
& tf_error
)
4564 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
4565 "reinterpret_cast from integer to pointer");
4568 return (RECUR (from
, TREE_CODE (t
) != VIEW_CONVERT_EXPR
));
4572 /* -- a unary operator & that is applied to an lvalue that
4573 designates an object with thread or automatic storage
4575 t
= TREE_OPERAND (t
, 0);
4577 if (TREE_CODE (t
) == OFFSET_REF
&& PTRMEM_OK_P (t
))
4578 /* A pointer-to-member constant. */
4582 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
4583 any checking here, as we might dereference the pointer later. If
4584 we remove this code, also remove check_automatic_or_tls. */
4585 i
= check_automatic_or_tls (t
);
4590 if (flags
& tf_error
)
4591 error ("address-of an object %qE with thread local or "
4592 "automatic storage is not a constant expression", t
);
4596 return RECUR (t
, any
);
4602 /* -- a class member access unless its postfix-expression is
4603 of literal type or of pointer to literal type. */
4604 /* This test would be redundant, as it follows from the
4605 postfix-expression being a potential constant expression. */
4606 if (type_unknown_p (t
))
4608 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
4610 case EXPR_PACK_EXPANSION
:
4611 return RECUR (PACK_EXPANSION_PATTERN (t
), want_rval
);
4615 tree x
= TREE_OPERAND (t
, 0);
4617 if (is_this_parameter (x
))
4619 if (DECL_CONTEXT (x
)
4620 && !DECL_DECLARED_CONSTEXPR_P (DECL_CONTEXT (x
)))
4622 if (flags
& tf_error
)
4623 error ("use of %<this%> in a constant expression");
4628 return RECUR (x
, rval
);
4631 case STATEMENT_LIST
:
4633 tree_stmt_iterator i
;
4634 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
4636 if (!RECUR (tsi_stmt (i
), any
))
4644 if (cxx_dialect
< cxx14
)
4646 if (!RECUR (TREE_OPERAND (t
, 0), any
))
4648 if (!RECUR (TREE_OPERAND (t
, 1), rval
))
4653 if (cxx_dialect
< cxx14
)
4655 if (!RECUR (TREE_OPERAND (t
, 0), rval
))
4657 if (!RECUR (TREE_OPERAND (t
, 2), rval
))
4662 if (!RECUR (DO_COND (t
), rval
))
4664 if (!RECUR (DO_BODY (t
), any
))
4669 if (!RECUR (FOR_INIT_STMT (t
), any
))
4671 if (!RECUR (FOR_COND (t
), rval
))
4673 if (!RECUR (FOR_EXPR (t
), any
))
4675 if (!RECUR (FOR_BODY (t
), any
))
4680 if (!RECUR (WHILE_COND (t
), rval
))
4682 if (!RECUR (WHILE_BODY (t
), any
))
4687 if (!RECUR (SWITCH_STMT_COND (t
), rval
))
4689 /* FIXME we don't check SWITCH_STMT_BODY currently, because even
4690 unreachable labels would be checked. */
4694 return RECUR (STMT_EXPR_STMT (t
), rval
);
4697 case DYNAMIC_CAST_EXPR
:
4698 case PSEUDO_DTOR_EXPR
:
4702 case VEC_DELETE_EXPR
:
4705 case OMP_ATOMIC_READ
:
4706 case OMP_ATOMIC_CAPTURE_OLD
:
4707 case OMP_ATOMIC_CAPTURE_NEW
:
4708 /* GCC internal stuff. */
4711 case TRANSACTION_EXPR
:
4713 case AT_ENCODE_EXPR
:
4715 if (flags
& tf_error
)
4716 error ("expression %qE is not a constant-expression", t
);
4720 /* -- a typeid expression whose operand is of polymorphic
4723 tree e
= TREE_OPERAND (t
, 0);
4724 if (!TYPE_P (e
) && !type_dependent_expression_p (e
)
4725 && TYPE_POLYMORPHIC_P (TREE_TYPE (e
)))
4727 if (flags
& tf_error
)
4728 error ("typeid-expression is not a constant expression "
4729 "because %qE is of polymorphic type", e
);
4748 case PREINCREMENT_EXPR
:
4749 case POSTINCREMENT_EXPR
:
4750 case PREDECREMENT_EXPR
:
4751 case POSTDECREMENT_EXPR
:
4752 if (cxx_dialect
< cxx14
)
4758 if (TYPE_P (TREE_OPERAND (t
, 0)))
4760 /* else fall through. */
4766 case FIX_TRUNC_EXPR
:
4770 case TRUTH_NOT_EXPR
:
4771 case FIXED_CONVERT_EXPR
:
4772 case UNARY_PLUS_EXPR
:
4773 case UNARY_LEFT_FOLD_EXPR
:
4774 case UNARY_RIGHT_FOLD_EXPR
:
4776 return RECUR (TREE_OPERAND (t
, 0), rval
);
4779 case CONST_CAST_EXPR
:
4780 case STATIC_CAST_EXPR
:
4781 case REINTERPRET_CAST_EXPR
:
4782 case IMPLICIT_CONV_EXPR
:
4783 if (cxx_dialect
< cxx11
4784 && !dependent_type_p (TREE_TYPE (t
))
4785 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
)))
4786 /* In C++98, a conversion to non-integral type can't be part of a
4787 constant expression. */
4789 if (flags
& tf_error
)
4790 error ("cast to non-integral type %qT in a constant expression",
4795 return (RECUR (TREE_OPERAND (t
, 0),
4796 TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
));
4799 return RECUR (BIND_EXPR_BODY (t
), want_rval
);
4801 case WITH_CLEANUP_EXPR
:
4802 case CLEANUP_POINT_EXPR
:
4803 case MUST_NOT_THROW_EXPR
:
4804 case TRY_CATCH_EXPR
:
4810 case NON_DEPENDENT_EXPR
:
4811 /* For convenience. */
4813 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
4815 case TRY_FINALLY_EXPR
:
4816 return (RECUR (TREE_OPERAND (t
, 0), want_rval
)
4817 && RECUR (TREE_OPERAND (t
, 1), any
));
4820 return RECUR (TREE_OPERAND (t
, 1), want_rval
);
4823 if (!literal_type_p (TREE_TYPE (t
)))
4825 if (flags
& tf_error
)
4827 error ("temporary of non-literal type %qT in a "
4828 "constant expression", TREE_TYPE (t
));
4829 explain_non_literal_class (TREE_TYPE (t
));
4834 return RECUR (TREE_OPERAND (t
, 1), rval
);
4838 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (t
);
4839 constructor_elt
*ce
;
4840 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
4841 if (!RECUR (ce
->value
, want_rval
))
4848 gcc_assert (TREE_PURPOSE (t
) == NULL_TREE
4849 || DECL_P (TREE_PURPOSE (t
)));
4850 if (!RECUR (TREE_VALUE (t
), want_rval
))
4852 if (TREE_CHAIN (t
) == NULL_TREE
)
4854 return RECUR (TREE_CHAIN (t
), want_rval
);
4857 case TRUNC_DIV_EXPR
:
4859 case FLOOR_DIV_EXPR
:
4860 case ROUND_DIV_EXPR
:
4861 case TRUNC_MOD_EXPR
:
4863 case ROUND_MOD_EXPR
:
4865 tree denom
= TREE_OPERAND (t
, 1);
4866 if (!RECUR (denom
, rval
))
4868 /* We can't call cxx_eval_outermost_constant_expr on an expression
4869 that hasn't been through instantiate_non_dependent_expr yet. */
4870 if (!processing_template_decl
)
4871 denom
= cxx_eval_outermost_constant_expr (denom
, true);
4872 if (integer_zerop (denom
))
4874 if (flags
& tf_error
)
4875 error ("division by zero is not a constant-expression");
4881 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
4887 /* check_return_expr sometimes wraps a TARGET_EXPR in a
4888 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
4889 introduced by build_call_a. */
4890 tree op0
= TREE_OPERAND (t
, 0);
4891 tree op1
= TREE_OPERAND (t
, 1);
4893 if ((TREE_CODE (op0
) == TARGET_EXPR
&& op1
== TARGET_EXPR_SLOT (op0
))
4894 || TREE_CODE (op1
) == EMPTY_CLASS_EXPR
)
4895 return RECUR (op0
, want_rval
);
4900 /* If the first operand is the non-short-circuit constant, look at
4901 the second operand; otherwise we only care about the first one for
4903 case TRUTH_AND_EXPR
:
4904 case TRUTH_ANDIF_EXPR
:
4905 tmp
= boolean_true_node
;
4908 case TRUTH_ORIF_EXPR
:
4909 tmp
= boolean_false_node
;
4912 tree op
= TREE_OPERAND (t
, 0);
4913 if (!RECUR (op
, rval
))
4915 if (!processing_template_decl
)
4916 op
= cxx_eval_outermost_constant_expr (op
, true);
4917 if (tree_int_cst_equal (op
, tmp
))
4918 return RECUR (TREE_OPERAND (t
, 1), rval
);
4925 case POINTER_PLUS_EXPR
:
4927 case EXACT_DIV_EXPR
:
4937 case TRUTH_XOR_EXPR
:
4938 case UNORDERED_EXPR
:
4951 case ARRAY_RANGE_REF
:
4955 case BINARY_LEFT_FOLD_EXPR
:
4956 case BINARY_RIGHT_FOLD_EXPR
:
4958 for (i
= 0; i
< 2; ++i
)
4959 if (!RECUR (TREE_OPERAND (t
, i
), want_rval
))
4963 case CILK_SYNC_STMT
:
4964 case CILK_SPAWN_STMT
:
4965 case ARRAY_NOTATION_REF
:
4970 for (i
= 0; i
< 3; ++i
)
4971 if (!RECUR (TREE_OPERAND (t
, i
), true))
4976 if (COND_EXPR_IS_VEC_DELETE (t
))
4978 if (flags
& tf_error
)
4979 error_at (location_of (t
),
4980 "%<delete[]%> is not a constant-expression");
4986 /* If the condition is a known constant, we know which of the legs we
4987 care about; otherwise we only require that the condition and
4988 either of the legs be potentially constant. */
4989 tmp
= TREE_OPERAND (t
, 0);
4990 if (!RECUR (tmp
, rval
))
4992 if (!processing_template_decl
)
4993 tmp
= cxx_eval_outermost_constant_expr (tmp
, true);
4994 if (integer_zerop (tmp
))
4995 return RECUR (TREE_OPERAND (t
, 2), want_rval
);
4996 else if (TREE_CODE (tmp
) == INTEGER_CST
)
4997 return RECUR (TREE_OPERAND (t
, 1), want_rval
);
4998 for (i
= 1; i
< 3; ++i
)
4999 if (potential_constant_expression_1 (TREE_OPERAND (t
, i
),
5000 want_rval
, strict
, tf_none
))
5002 if (flags
& tf_error
)
5003 error ("expression %qE is not a constant-expression", t
);
5007 if (VEC_INIT_EXPR_IS_CONSTEXPR (t
))
5009 if (flags
& tf_error
)
5011 error ("non-constant array initialization");
5012 diagnose_non_constexpr_vec_init (t
);
5018 /* We can see these in statement-expressions. */
5021 case EMPTY_CLASS_EXPR
:
5025 if (objc_is_property_ref (t
))
5028 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t
)));
5035 /* The main entry point to the above. */
5038 potential_constant_expression (tree t
)
5040 return potential_constant_expression_1 (t
, false, true, tf_none
);
5044 potential_static_init_expression (tree t
)
5046 return potential_constant_expression_1 (t
, false, false, tf_none
);
5049 /* As above, but require a constant rvalue. */
5052 potential_rvalue_constant_expression (tree t
)
5054 return potential_constant_expression_1 (t
, true, true, tf_none
);
5057 /* Like above, but complain about non-constant expressions. */
5060 require_potential_constant_expression (tree t
)
5062 return potential_constant_expression_1 (t
, false, true, tf_warning_or_error
);
5065 /* Cross product of the above. */
5068 require_potential_rvalue_constant_expression (tree t
)
5070 return potential_constant_expression_1 (t
, true, true, tf_warning_or_error
);
5073 #include "gt-cp-constexpr.h"