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-2015 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"
30 #include "c-family/c-objc.h"
31 #include "tree-iterator.h"
34 #include "tree-inline.h"
37 static bool verify_constant (tree, bool, bool *, bool *);
38 #define VERIFY_CONSTANT(X) \
40 if (verify_constant ((X), ctx->quiet, non_constant_p, overflow_p)) \
44 /* Returns true iff FUN is an instantiation of a constexpr function
45 template or a defaulted constexpr function. */
48 is_instantiation_of_constexpr (tree fun)
50 return ((DECL_TEMPLOID_INSTANTIATION (fun)
51 && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun)))
52 || (DECL_DEFAULTED_FN (fun)
53 && DECL_DECLARED_CONSTEXPR_P (fun)));
56 /* Return true if T is a literal type. */
59 literal_type_p (tree t)
63 || TREE_CODE (t) == REFERENCE_TYPE
64 || (VOID_TYPE_P (t) && cxx_dialect >= cxx14))
68 t = complete_type (t);
69 gcc_assert (COMPLETE_TYPE_P (t) || errorcount);
70 return CLASSTYPE_LITERAL_P (t);
72 if (TREE_CODE (t) == ARRAY_TYPE)
73 return literal_type_p (strip_array_types (t));
77 /* If DECL is a variable declared `constexpr', require its type
78 be literal. Return the DECL if OK, otherwise NULL. */
81 ensure_literal_type_for_constexpr_object (tree decl)
83 tree type = TREE_TYPE (decl);
85 && (DECL_DECLARED_CONSTEXPR_P (decl)
86 || var_in_constexpr_fn (decl))
87 && !processing_template_decl)
89 tree stype = strip_array_types (type);
90 if (CLASS_TYPE_P (stype) && !COMPLETE_TYPE_P (complete_type (stype)))
91 /* Don't complain here, we'll complain about incompleteness
92 when we try to initialize the variable. */;
93 else if (!literal_type_p (type))
95 if (DECL_DECLARED_CONSTEXPR_P (decl))
97 error ("the type %qT of constexpr variable %qD is not literal",
99 explain_non_literal_class (type);
103 if (!DECL_TEMPLATE_INSTANTIATION (current_function_decl))
105 error ("variable %qD of non-literal type %qT in %<constexpr%> "
106 "function", decl, type);
107 explain_non_literal_class (type);
109 cp_function_chain->invalid_constexpr = true;
117 /* Representation of entries in the constexpr function definition table. */
119 struct GTY((for_user)) constexpr_fundef {
124 struct constexpr_fundef_hasher : ggc_ptr_hash<constexpr_fundef>
126 static hashval_t hash (constexpr_fundef *);
127 static bool equal (constexpr_fundef *, constexpr_fundef *);
130 /* This table holds all constexpr function definitions seen in
131 the current translation unit. */
133 static GTY (()) hash_table<constexpr_fundef_hasher> *constexpr_fundef_table;
135 /* Utility function used for managing the constexpr function table.
136 Return true if the entries pointed to by P and Q are for the
137 same constexpr function. */
140 constexpr_fundef_hasher::equal (constexpr_fundef *lhs, constexpr_fundef *rhs)
142 return lhs->decl == rhs->decl;
145 /* Utility function used for managing the constexpr function table.
146 Return a hash value for the entry pointed to by Q. */
149 constexpr_fundef_hasher::hash (constexpr_fundef *fundef)
151 return DECL_UID (fundef->decl);
154 /* Return a previously saved definition of function FUN. */
156 static constexpr_fundef *
157 retrieve_constexpr_fundef (tree fun)
159 constexpr_fundef fundef = { NULL, NULL };
160 if (constexpr_fundef_table == NULL)
164 return constexpr_fundef_table->find (&fundef);
167 /* Check whether the parameter and return types of FUN are valid for a
168 constexpr function, and complain if COMPLAIN. */
171 is_valid_constexpr_fn (tree fun, bool complain)
175 if (DECL_INHERITED_CTOR_BASE (fun)
176 && TREE_CODE (fun) == TEMPLATE_DECL)
180 error ("inherited constructor %qD is not constexpr",
181 get_inherited_ctor (fun));
185 for (tree parm = FUNCTION_FIRST_USER_PARM (fun);
186 parm != NULL_TREE; parm = TREE_CHAIN (parm))
187 if (!literal_type_p (TREE_TYPE (parm)))
192 error ("invalid type for parameter %d of constexpr "
193 "function %q+#D", DECL_PARM_INDEX (parm), fun);
194 explain_non_literal_class (TREE_TYPE (parm));
199 if (!DECL_CONSTRUCTOR_P (fun))
201 tree rettype = TREE_TYPE (TREE_TYPE (fun));
202 if (!literal_type_p (rettype))
207 error ("invalid return type %qT of constexpr function %q+D",
209 explain_non_literal_class (rettype);
213 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
214 && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun)))
219 error ("enclosing class of constexpr non-static member "
220 "function %q+#D is not a literal type", fun);
221 explain_non_literal_class (DECL_CONTEXT (fun));
225 else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun)))
229 error ("%q#T has virtual base classes", DECL_CONTEXT (fun));
235 /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF
236 for a member of an anonymous aggregate, INIT is the initializer for that
237 member, and VEC_OUTER is the vector of constructor elements for the class
238 whose constructor we are processing. Add the initializer to the vector
239 and return true to indicate success. */
242 build_anon_member_initialization (tree member, tree init,
243 vec<constructor_elt, va_gc> **vec_outer)
245 /* MEMBER presents the relevant fields from the inside out, but we need
246 to build up the initializer from the outside in so that we can reuse
247 previously built CONSTRUCTORs if this is, say, the second field in an
248 anonymous struct. So we use a vec as a stack. */
249 auto_vec<tree, 2> fields;
252 fields.safe_push (TREE_OPERAND (member, 1));
253 member = TREE_OPERAND (member, 0);
255 while (ANON_AGGR_TYPE_P (TREE_TYPE (member))
256 && TREE_CODE (member) == COMPONENT_REF);
258 /* VEC has the constructor elements vector for the context of FIELD.
259 If FIELD is an anonymous aggregate, we will push inside it. */
260 vec<constructor_elt, va_gc> **vec = vec_outer;
262 while (field = fields.pop(),
263 ANON_AGGR_TYPE_P (TREE_TYPE (field)))
266 /* If there is already an outer constructor entry for the anonymous
267 aggregate FIELD, use it; otherwise, insert one. */
268 if (vec_safe_is_empty (*vec)
269 || (*vec)->last().index != field)
271 ctor = build_constructor (TREE_TYPE (field), NULL);
272 CONSTRUCTOR_APPEND_ELT (*vec, field, ctor);
275 ctor = (*vec)->last().value;
276 vec = &CONSTRUCTOR_ELTS (ctor);
279 /* Now we're at the innermost field, the one that isn't an anonymous
280 aggregate. Add its initializer to the CONSTRUCTOR and we're done. */
281 gcc_assert (fields.is_empty());
282 CONSTRUCTOR_APPEND_ELT (*vec, field, init);
287 /* Subroutine of build_constexpr_constructor_member_initializers.
288 The expression tree T represents a data member initialization
289 in a (constexpr) constructor definition. Build a pairing of
290 the data member with its initializer, and prepend that pair
291 to the existing initialization pair INITS. */
294 build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec)
297 if (TREE_CODE (t) == CLEANUP_POINT_EXPR)
298 t = TREE_OPERAND (t, 0);
299 if (TREE_CODE (t) == EXPR_STMT)
300 t = TREE_OPERAND (t, 0);
301 if (t == error_mark_node)
303 if (TREE_CODE (t) == STATEMENT_LIST)
305 tree_stmt_iterator i;
306 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
308 if (! build_data_member_initialization (tsi_stmt (i), vec))
313 if (TREE_CODE (t) == CLEANUP_STMT)
315 /* We can't see a CLEANUP_STMT in a constructor for a literal class,
316 but we can in a constexpr constructor for a non-literal class. Just
317 ignore it; either all the initialization will be constant, in which
318 case the cleanup can't run, or it can't be constexpr.
319 Still recurse into CLEANUP_BODY. */
320 return build_data_member_initialization (CLEANUP_BODY (t), vec);
322 if (TREE_CODE (t) == CONVERT_EXPR)
323 t = TREE_OPERAND (t, 0);
324 if (TREE_CODE (t) == INIT_EXPR
325 /* vptr initialization shows up as a MODIFY_EXPR. In C++14 we only
326 use what this function builds for cx_check_missing_mem_inits, and
327 assignment in the ctor body doesn't count. */
328 || (cxx_dialect < cxx14 && TREE_CODE (t) == MODIFY_EXPR))
330 member = TREE_OPERAND (t, 0);
331 init = break_out_target_exprs (TREE_OPERAND (t, 1));
333 else if (TREE_CODE (t) == CALL_EXPR)
335 tree fn = get_callee_fndecl (t);
336 if (!fn || !DECL_CONSTRUCTOR_P (fn))
337 /* We're only interested in calls to subobject constructors. */
339 member = CALL_EXPR_ARG (t, 0);
340 /* We don't use build_cplus_new here because it complains about
341 abstract bases. Leaving the call unwrapped means that it has the
342 wrong type, but cxx_eval_constant_expression doesn't care. */
343 init = break_out_target_exprs (t);
345 else if (TREE_CODE (t) == BIND_EXPR)
346 return build_data_member_initialization (BIND_EXPR_BODY (t), vec);
348 /* Don't add anything else to the CONSTRUCTOR. */
350 if (INDIRECT_REF_P (member))
351 member = TREE_OPERAND (member, 0);
352 if (TREE_CODE (member) == NOP_EXPR)
356 if (TREE_CODE (op) == ADDR_EXPR)
358 gcc_assert (same_type_ignoring_top_level_qualifiers_p
359 (TREE_TYPE (TREE_TYPE (op)),
360 TREE_TYPE (TREE_TYPE (member))));
361 /* Initializing a cv-qualified member; we need to look through
365 else if (op == current_class_ptr
366 && (same_type_ignoring_top_level_qualifiers_p
367 (TREE_TYPE (TREE_TYPE (member)),
368 current_class_type)))
369 /* Delegating constructor. */
373 /* This is an initializer for an empty base; keep it for now so
374 we can check it in cxx_eval_bare_aggregate. */
375 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member))));
378 if (TREE_CODE (member) == ADDR_EXPR)
379 member = TREE_OPERAND (member, 0);
380 if (TREE_CODE (member) == COMPONENT_REF)
382 tree aggr = TREE_OPERAND (member, 0);
383 if (TREE_CODE (aggr) != COMPONENT_REF)
384 /* Normal member initialization. */
385 member = TREE_OPERAND (member, 1);
386 else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr)))
387 /* Initializing a member of an anonymous union. */
388 return build_anon_member_initialization (member, init, vec);
390 /* We're initializing a vtable pointer in a base. Leave it as
391 COMPONENT_REF so we remember the path to get to the vfield. */
392 gcc_assert (TREE_TYPE (member) == vtbl_ptr_type_node);
395 CONSTRUCTOR_APPEND_ELT (*vec, member, init);
399 /* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval.
400 In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a
401 BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */
404 check_constexpr_bind_expr_vars (tree t)
406 gcc_assert (TREE_CODE (t) == BIND_EXPR);
408 for (tree var = BIND_EXPR_VARS (t); var; var = DECL_CHAIN (var))
409 if (TREE_CODE (var) == TYPE_DECL
410 && DECL_IMPLICIT_TYPEDEF_P (var)
411 && !LAMBDA_TYPE_P (TREE_TYPE (var)))
416 /* Subroutine of check_constexpr_ctor_body. */
419 check_constexpr_ctor_body_1 (tree last, tree list)
421 switch (TREE_CODE (list))
424 if (TREE_CODE (DECL_EXPR_DECL (list)) == USING_DECL)
428 case CLEANUP_POINT_EXPR:
429 return check_constexpr_ctor_body (last, TREE_OPERAND (list, 0),
433 if (!check_constexpr_bind_expr_vars (list)
434 || !check_constexpr_ctor_body (last, BIND_EXPR_BODY (list),
448 /* Make sure that there are no statements after LAST in the constructor
449 body represented by LIST. */
452 check_constexpr_ctor_body (tree last, tree list, bool complain)
454 /* C++14 doesn't require a constexpr ctor to have an empty body. */
455 if (cxx_dialect >= cxx14)
459 if (TREE_CODE (list) == STATEMENT_LIST)
461 tree_stmt_iterator i = tsi_last (list);
462 for (; !tsi_end_p (i); tsi_prev (&i))
464 tree t = tsi_stmt (i);
467 if (!check_constexpr_ctor_body_1 (last, t))
474 else if (list != last
475 && !check_constexpr_ctor_body_1 (last, list))
480 error ("constexpr constructor does not have empty body");
481 DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false;
486 /* V is a vector of constructor elements built up for the base and member
487 initializers of a constructor for TYPE. They need to be in increasing
488 offset order, which they might not be yet if TYPE has a primary base
489 which is not first in the base-clause or a vptr and at least one base
490 all of which are non-primary. */
492 static vec<constructor_elt, va_gc> *
493 sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v)
495 tree pri = CLASSTYPE_PRIMARY_BINFO (type);
501 field_type = BINFO_TYPE (pri);
502 else if (TYPE_CONTAINS_VPTR_P (type))
503 field_type = vtbl_ptr_type_node;
507 /* Find the element for the primary base or vptr and move it to the
508 beginning of the vec. */
509 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
510 if (TREE_TYPE (ce->index) == field_type)
513 if (i > 0 && i < vec_safe_length (v))
515 vec<constructor_elt, va_gc> &vref = *v;
516 constructor_elt elt = vref[i];
525 /* Build compile-time evalable representations of member-initializer list
526 for a constexpr constructor. */
529 build_constexpr_constructor_member_initializers (tree type, tree body)
531 vec<constructor_elt, va_gc> *vec = NULL;
533 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR
534 || TREE_CODE (body) == EH_SPEC_BLOCK)
535 body = TREE_OPERAND (body, 0);
536 if (TREE_CODE (body) == STATEMENT_LIST)
538 for (tree_stmt_iterator i = tsi_start (body);
539 !tsi_end_p (i); tsi_next (&i))
542 if (TREE_CODE (body) == BIND_EXPR)
546 if (TREE_CODE (body) == BIND_EXPR)
547 body = BIND_EXPR_BODY (body);
548 if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
550 body = TREE_OPERAND (body, 0);
551 if (TREE_CODE (body) == EXPR_STMT)
552 body = TREE_OPERAND (body, 0);
553 if (TREE_CODE (body) == INIT_EXPR
554 && (same_type_ignoring_top_level_qualifiers_p
555 (TREE_TYPE (TREE_OPERAND (body, 0)),
556 current_class_type)))
559 return TREE_OPERAND (body, 1);
561 ok = build_data_member_initialization (body, &vec);
563 else if (TREE_CODE (body) == STATEMENT_LIST)
565 tree_stmt_iterator i;
566 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
568 ok = build_data_member_initialization (tsi_stmt (i), &vec);
573 else if (TREE_CODE (body) == TRY_BLOCK)
575 error ("body of %<constexpr%> constructor cannot be "
576 "a function-try-block");
577 return error_mark_node;
579 else if (EXPR_P (body))
580 ok = build_data_member_initialization (body, &vec);
582 gcc_assert (errorcount > 0);
585 if (vec_safe_length (vec) > 0)
587 /* In a delegating constructor, return the target. */
588 constructor_elt *ce = &(*vec)[0];
589 if (ce->index == current_class_ptr)
596 vec = sort_constexpr_mem_initializers (type, vec);
597 return build_constructor (type, vec);
600 return error_mark_node;
603 /* Subroutine of register_constexpr_fundef. BODY is the body of a function
604 declared to be constexpr, or a sub-statement thereof. Returns the
605 return value if suitable, error_mark_node for a statement not allowed in
606 a constexpr function, or NULL_TREE if no return value was found. */
609 constexpr_fn_retval (tree body)
611 switch (TREE_CODE (body))
615 tree_stmt_iterator i;
616 tree expr = NULL_TREE;
617 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
619 tree s = constexpr_fn_retval (tsi_stmt (i));
620 if (s == error_mark_node)
621 return error_mark_node;
622 else if (s == NULL_TREE)
623 /* Keep iterating. */;
625 /* Multiple return statements. */
626 return error_mark_node;
634 return break_out_target_exprs (TREE_OPERAND (body, 0));
638 tree decl = DECL_EXPR_DECL (body);
639 if (TREE_CODE (decl) == USING_DECL
640 /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */
641 || DECL_ARTIFICIAL (decl))
643 return error_mark_node;
646 case CLEANUP_POINT_EXPR:
647 return constexpr_fn_retval (TREE_OPERAND (body, 0));
650 if (!check_constexpr_bind_expr_vars (body))
651 return error_mark_node;
652 return constexpr_fn_retval (BIND_EXPR_BODY (body));
658 return error_mark_node;
662 /* Subroutine of register_constexpr_fundef. BODY is the DECL_SAVED_TREE of
663 FUN; do the necessary transformations to turn it into a single expression
664 that we can store in the hash table. */
667 massage_constexpr_body (tree fun, tree body)
669 if (DECL_CONSTRUCTOR_P (fun))
670 body = build_constexpr_constructor_member_initializers
671 (DECL_CONTEXT (fun), body);
672 else if (cxx_dialect < cxx14)
674 if (TREE_CODE (body) == EH_SPEC_BLOCK)
675 body = EH_SPEC_STMTS (body);
676 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
677 body = TREE_OPERAND (body, 0);
678 body = constexpr_fn_retval (body);
683 /* FUN is a constexpr constructor with massaged body BODY. Return true
684 if some bases/fields are uninitialized, and complain if COMPLAIN. */
687 cx_check_missing_mem_inits (tree fun, tree body, bool complain)
694 if (TREE_CODE (body) != CONSTRUCTOR)
697 nelts = CONSTRUCTOR_NELTS (body);
698 ctype = DECL_CONTEXT (fun);
699 field = TYPE_FIELDS (ctype);
701 if (TREE_CODE (ctype) == UNION_TYPE)
703 if (nelts == 0 && next_initializable_field (field))
706 error ("%<constexpr%> constructor for union %qT must "
707 "initialize exactly one non-static data member", ctype);
714 for (i = 0; i <= nelts; ++i)
721 index = CONSTRUCTOR_ELT (body, i)->index;
722 /* Skip base and vtable inits. */
723 if (TREE_CODE (index) != FIELD_DECL
724 || DECL_ARTIFICIAL (index))
727 for (; field != index; field = DECL_CHAIN (field))
730 if (TREE_CODE (field) != FIELD_DECL
731 || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
732 || DECL_ARTIFICIAL (field))
734 ftype = strip_array_types (TREE_TYPE (field));
735 if (type_has_constexpr_default_constructor (ftype))
737 /* It's OK to skip a member with a trivial constexpr ctor.
738 A constexpr ctor that isn't trivial should have been
740 gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype)
746 error ("member %qD must be initialized by mem-initializer "
747 "in %<constexpr%> constructor", field);
748 inform (DECL_SOURCE_LOCATION (field), "declared here");
751 if (field == NULL_TREE)
753 field = DECL_CHAIN (field);
759 /* We are processing the definition of the constexpr function FUN.
760 Check that its BODY fulfills the propriate requirements and
761 enter it in the constexpr function definition table.
762 For constructor BODY is actually the TREE_LIST of the
763 member-initializer list. */
766 register_constexpr_fundef (tree fun, tree body)
768 constexpr_fundef entry;
769 constexpr_fundef **slot;
771 if (!is_valid_constexpr_fn (fun, !DECL_GENERATED_P (fun)))
774 body = massage_constexpr_body (fun, body);
775 if (body == NULL_TREE || body == error_mark_node)
777 if (!DECL_CONSTRUCTOR_P (fun))
778 error ("body of constexpr function %qD not a return-statement", fun);
782 if (!potential_rvalue_constant_expression (body))
784 if (!DECL_GENERATED_P (fun))
785 require_potential_rvalue_constant_expression (body);
789 if (DECL_CONSTRUCTOR_P (fun)
790 && cx_check_missing_mem_inits (fun, body, !DECL_GENERATED_P (fun)))
793 /* Create the constexpr function table if necessary. */
794 if (constexpr_fundef_table == NULL)
795 constexpr_fundef_table
796 = hash_table<constexpr_fundef_hasher>::create_ggc (101);
800 slot = constexpr_fundef_table->find_slot (&entry, INSERT);
802 gcc_assert (*slot == NULL);
803 *slot = ggc_alloc<constexpr_fundef> ();
809 /* FUN is a non-constexpr function called in a context that requires a
810 constant expression. If it comes from a constexpr template, explain why
811 the instantiation isn't constexpr. */
814 explain_invalid_constexpr_fn (tree fun)
816 static hash_set<tree> *diagnosed;
819 /* Only diagnose defaulted functions or instantiations. */
820 if (!DECL_DEFAULTED_FN (fun)
821 && !is_instantiation_of_constexpr (fun))
823 if (diagnosed == NULL)
824 diagnosed = new hash_set<tree>;
825 if (diagnosed->add (fun))
826 /* Already explained. */
829 save_loc = input_location;
830 input_location = DECL_SOURCE_LOCATION (fun);
831 inform (input_location,
832 "%qD is not usable as a constexpr function because:", fun);
833 /* First check the declaration. */
834 if (is_valid_constexpr_fn (fun, true))
836 /* Then if it's OK, the body. */
837 if (!DECL_DECLARED_CONSTEXPR_P (fun))
838 explain_implicit_non_constexpr (fun);
841 body = massage_constexpr_body (fun, DECL_SAVED_TREE (fun));
842 require_potential_rvalue_constant_expression (body);
843 if (DECL_CONSTRUCTOR_P (fun))
844 cx_check_missing_mem_inits (fun, body, true);
847 input_location = save_loc;
850 /* Objects of this type represent calls to constexpr functions
851 along with the bindings of parameters to their arguments, for
852 the purpose of compile time evaluation. */
854 struct GTY((for_user)) constexpr_call {
855 /* Description of the constexpr function definition. */
856 constexpr_fundef *fundef;
857 /* Parameter bindings environment. A TREE_LIST where each TREE_PURPOSE
858 is a parameter _DECL and the TREE_VALUE is the value of the parameter.
859 Note: This arrangement is made to accommodate the use of
860 iterative_hash_template_arg (see pt.c). If you change this
861 representation, also change the hash calculation in
862 cxx_eval_call_expression. */
864 /* Result of the call.
865 NULL means the call is being evaluated.
866 error_mark_node means that the evaluation was erroneous;
867 otherwise, the actuall value of the call. */
869 /* The hash of this call; we remember it here to avoid having to
870 recalculate it when expanding the hash table. */
874 struct constexpr_call_hasher : ggc_ptr_hash<constexpr_call>
876 static hashval_t hash (constexpr_call *);
877 static bool equal (constexpr_call *, constexpr_call *);
880 /* The constexpr expansion context. CALL is the current function
881 expansion, CTOR is the current aggregate initializer, OBJECT is the
882 object being initialized by CTOR, either a VAR_DECL or a _REF. VALUES
883 is a map of values of variables initialized within the expression. */
885 struct constexpr_ctx {
886 /* The innermost call we're evaluating. */
887 constexpr_call *call;
888 /* Values for any temporaries or local variables within the
889 constant-expression. */
890 hash_map<tree,tree> *values;
891 /* The CONSTRUCTOR we're currently building up for an aggregate
894 /* The object we're building the CONSTRUCTOR for. */
896 /* Whether we should error on a non-constant expression or fail quietly. */
898 /* Whether we are strictly conforming to constant expression rules or
899 trying harder to get a constant value. */
903 /* A table of all constexpr calls that have been evaluated by the
904 compiler in this translation unit. */
906 static GTY (()) hash_table<constexpr_call_hasher> *constexpr_call_table;
908 static tree cxx_eval_constant_expression (const constexpr_ctx *, tree,
909 bool, bool *, bool *, tree * = NULL);
911 /* Compute a hash value for a constexpr call representation. */
914 constexpr_call_hasher::hash (constexpr_call *info)
919 /* Return true if the objects pointed to by P and Q represent calls
920 to the same constexpr function with the same arguments.
921 Otherwise, return false. */
924 constexpr_call_hasher::equal (constexpr_call *lhs, constexpr_call *rhs)
930 if (!constexpr_fundef_hasher::equal (lhs->fundef, rhs->fundef))
932 lhs_bindings = lhs->bindings;
933 rhs_bindings = rhs->bindings;
934 while (lhs_bindings != NULL && rhs_bindings != NULL)
936 tree lhs_arg = TREE_VALUE (lhs_bindings);
937 tree rhs_arg = TREE_VALUE (rhs_bindings);
938 gcc_assert (TREE_TYPE (lhs_arg) == TREE_TYPE (rhs_arg));
939 if (!cp_tree_equal (lhs_arg, rhs_arg))
941 lhs_bindings = TREE_CHAIN (lhs_bindings);
942 rhs_bindings = TREE_CHAIN (rhs_bindings);
944 return lhs_bindings == rhs_bindings;
947 /* Initialize the constexpr call table, if needed. */
950 maybe_initialize_constexpr_call_table (void)
952 if (constexpr_call_table == NULL)
953 constexpr_call_table = hash_table<constexpr_call_hasher>::create_ggc (101);
956 /* We have an expression tree T that represents a call, either CALL_EXPR
957 or AGGR_INIT_EXPR. If the call is lexically to a named function,
958 retrun the _DECL for that function. */
961 get_function_named_in_call (tree t)
964 switch (TREE_CODE (t))
967 fun = CALL_EXPR_FN (t);
971 fun = AGGR_INIT_EXPR_FN (t);
978 if (fun && TREE_CODE (fun) == ADDR_EXPR
979 && TREE_CODE (TREE_OPERAND (fun, 0)) == FUNCTION_DECL)
980 fun = TREE_OPERAND (fun, 0);
984 /* We have an expression tree T that represents a call, either CALL_EXPR
985 or AGGR_INIT_EXPR. Return the Nth argument. */
988 get_nth_callarg (tree t, int n)
990 switch (TREE_CODE (t))
993 return CALL_EXPR_ARG (t, n);
996 return AGGR_INIT_EXPR_ARG (t, n);
1004 /* Attempt to evaluate T which represents a call to a builtin function.
1005 We assume here that all builtin functions evaluate to scalar types
1006 represented by _CST nodes. */
1009 cxx_eval_builtin_function_call (const constexpr_ctx *ctx, tree t, tree fun,
1011 bool *non_constant_p, bool *overflow_p)
1013 const int nargs = call_expr_nargs (t);
1014 tree *args = (tree *) alloca (nargs * sizeof (tree));
1018 /* Don't fold __builtin_constant_p within a constexpr function. */
1019 if (DECL_FUNCTION_CODE (fun) == BUILT_IN_CONSTANT_P
1020 && current_function_decl
1021 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
1023 *non_constant_p = true;
1027 /* Be permissive for arguments to built-ins; __builtin_constant_p should
1028 return constant false for a non-constant argument. */
1029 constexpr_ctx new_ctx = *ctx;
1030 new_ctx.quiet = true;
1031 bool dummy1 = false, dummy2 = false;
1032 for (i = 0; i < nargs; ++i)
1033 args[i] = cxx_eval_constant_expression (&new_ctx, CALL_EXPR_ARG (t, i),
1034 lval, &dummy1, &dummy2);
1036 bool save_ffbcp = force_folding_builtin_constant_p;
1037 force_folding_builtin_constant_p = true;
1038 new_call = fold_build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t),
1039 CALL_EXPR_FN (t), nargs, args);
1040 force_folding_builtin_constant_p = save_ffbcp;
1041 VERIFY_CONSTANT (new_call);
1045 /* TEMP is the constant value of a temporary object of type TYPE. Adjust
1046 the type of the value to match. */
1049 adjust_temp_type (tree type, tree temp)
1051 if (TREE_TYPE (temp) == type)
1053 /* Avoid wrapping an aggregate value in a NOP_EXPR. */
1054 if (TREE_CODE (temp) == CONSTRUCTOR)
1055 return build_constructor (type, CONSTRUCTOR_ELTS (temp));
1056 gcc_assert (scalarish_type_p (type));
1057 return cp_fold_convert (type, temp);
1060 /* Subroutine of cxx_eval_call_expression.
1061 We are processing a call expression (either CALL_EXPR or
1062 AGGR_INIT_EXPR) in the context of CTX. Evaluate
1063 all arguments and bind their values to correspondings
1064 parameters, making up the NEW_CALL context. */
1067 cxx_bind_parameters_in_call (const constexpr_ctx *ctx, tree t,
1068 constexpr_call *new_call,
1069 bool *non_constant_p, bool *overflow_p,
1070 bool *non_constant_args)
1072 const int nargs = call_expr_nargs (t);
1073 tree fun = new_call->fundef->decl;
1074 tree parms = DECL_ARGUMENTS (fun);
1076 tree *p = &new_call->bindings;
1077 for (i = 0; i < nargs; ++i)
1080 tree type = parms ? TREE_TYPE (parms) : void_type_node;
1081 x = get_nth_callarg (t, i);
1082 /* For member function, the first argument is a pointer to the implied
1083 object. For a constructor, it might still be a dummy object, in
1084 which case we get the real argument from ctx. */
1085 if (i == 0 && DECL_CONSTRUCTOR_P (fun)
1086 && is_dummy_object (x))
1089 x = cp_build_addr_expr (x, tf_warning_or_error);
1092 arg = cxx_eval_constant_expression (ctx, x, lval,
1093 non_constant_p, overflow_p);
1094 /* Don't VERIFY_CONSTANT here. */
1095 if (*non_constant_p && ctx->quiet)
1097 /* Just discard ellipsis args after checking their constantitude. */
1100 if (*non_constant_p)
1101 /* Don't try to adjust the type of non-constant args. */
1104 /* Make sure the binding has the same type as the parm. */
1105 if (TREE_CODE (type) != REFERENCE_TYPE)
1106 arg = adjust_temp_type (type, arg);
1107 if (!TREE_CONSTANT (arg))
1108 *non_constant_args = true;
1109 *p = build_tree_list (parms, arg);
1110 p = &TREE_CHAIN (*p);
1112 parms = TREE_CHAIN (parms);
1116 /* Variables and functions to manage constexpr call expansion context.
1117 These do not need to be marked for PCH or GC. */
1119 /* FIXME remember and print actual constant arguments. */
1120 static vec<tree> call_stack = vNULL;
1121 static int call_stack_tick;
1122 static int last_cx_error_tick;
1125 push_cx_call_context (tree call)
1128 if (!EXPR_HAS_LOCATION (call))
1129 SET_EXPR_LOCATION (call, input_location);
1130 call_stack.safe_push (call);
1131 if (call_stack.length () > (unsigned) max_constexpr_depth)
1137 pop_cx_call_context (void)
1144 cx_error_context (void)
1146 vec<tree> r = vNULL;
1147 if (call_stack_tick != last_cx_error_tick
1148 && !call_stack.is_empty ())
1150 last_cx_error_tick = call_stack_tick;
1154 /* Subroutine of cxx_eval_constant_expression.
1155 Evaluate the call expression tree T in the context of OLD_CALL expression
1159 cxx_eval_call_expression (const constexpr_ctx *ctx, tree t,
1161 bool *non_constant_p, bool *overflow_p)
1163 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
1164 tree fun = get_function_named_in_call (t);
1165 constexpr_call new_call = { NULL, NULL, NULL, 0 };
1168 if (fun == NULL_TREE)
1169 switch (CALL_EXPR_IFN (t))
1171 case IFN_UBSAN_NULL:
1172 case IFN_UBSAN_BOUNDS:
1173 case IFN_UBSAN_VPTR:
1177 error_at (loc, "call to internal function");
1178 *non_constant_p = true;
1182 if (TREE_CODE (fun) != FUNCTION_DECL)
1184 /* Might be a constexpr function pointer. */
1185 fun = cxx_eval_constant_expression (ctx, fun,
1186 /*lval*/false, non_constant_p,
1189 if (TREE_CODE (fun) == ADDR_EXPR)
1190 fun = TREE_OPERAND (fun, 0);
1192 if (TREE_CODE (fun) != FUNCTION_DECL)
1194 if (!ctx->quiet && !*non_constant_p)
1195 error_at (loc, "expression %qE does not designate a constexpr "
1197 *non_constant_p = true;
1200 if (DECL_CLONED_FUNCTION_P (fun))
1201 fun = DECL_CLONED_FUNCTION (fun);
1203 if (is_ubsan_builtin_p (fun))
1206 if (is_builtin_fn (fun))
1207 return cxx_eval_builtin_function_call (ctx, t, fun,
1208 lval, non_constant_p, overflow_p);
1209 if (!DECL_DECLARED_CONSTEXPR_P (fun))
1213 error_at (loc, "call to non-constexpr function %qD", fun);
1214 explain_invalid_constexpr_fn (fun);
1216 *non_constant_p = true;
1220 /* Shortcut trivial constructor/op=. */
1221 if (trivial_fn_p (fun))
1223 if (call_expr_nargs (t) == 2)
1225 tree arg = convert_from_reference (get_nth_callarg (t, 1));
1226 return cxx_eval_constant_expression (ctx, arg,
1227 lval, non_constant_p,
1230 else if (TREE_CODE (t) == AGGR_INIT_EXPR
1231 && AGGR_INIT_ZERO_FIRST (t))
1232 return build_zero_init (DECL_CONTEXT (fun), NULL_TREE, false);
1235 /* We can't defer instantiating the function any longer. */
1236 if (!DECL_INITIAL (fun)
1237 && DECL_TEMPLOID_INSTANTIATION (fun))
1240 instantiate_decl (fun, /*defer_ok*/false, /*expl_inst*/false);
1244 /* If in direct recursive call, optimize definition search. */
1245 if (ctx && ctx->call && ctx->call->fundef->decl == fun)
1246 new_call.fundef = ctx->call->fundef;
1249 new_call.fundef = retrieve_constexpr_fundef (fun);
1250 if (new_call.fundef == NULL || new_call.fundef->body == NULL)
1254 if (DECL_INITIAL (fun))
1256 /* The definition of fun was somehow unsuitable. */
1257 error_at (loc, "%qD called in a constant expression", fun);
1258 explain_invalid_constexpr_fn (fun);
1261 error_at (loc, "%qD used before its definition", fun);
1263 *non_constant_p = true;
1268 constexpr_ctx new_ctx = *ctx;
1269 if (DECL_CONSTRUCTOR_P (fun) && !ctx->object
1270 && TREE_CODE (t) == AGGR_INIT_EXPR)
1272 /* We want to have an initialization target for an AGGR_INIT_EXPR.
1273 If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
1274 new_ctx.object = AGGR_INIT_EXPR_SLOT (t);
1275 tree ctor = new_ctx.ctor = build_constructor (DECL_CONTEXT (fun), NULL);
1276 CONSTRUCTOR_NO_IMPLICIT_ZERO (ctor) = true;
1277 ctx->values->put (new_ctx.object, ctor);
1281 bool non_constant_args = false;
1282 cxx_bind_parameters_in_call (ctx, t, &new_call,
1283 non_constant_p, overflow_p, &non_constant_args);
1284 if (*non_constant_p)
1287 depth_ok = push_cx_call_context (t);
1289 tree result = NULL_TREE;
1291 constexpr_call *entry = NULL;
1292 if (depth_ok && !non_constant_args)
1294 new_call.hash = iterative_hash_template_arg
1295 (new_call.bindings, constexpr_fundef_hasher::hash (new_call.fundef));
1297 /* If we have seen this call before, we are done. */
1298 maybe_initialize_constexpr_call_table ();
1299 constexpr_call **slot
1300 = constexpr_call_table->find_slot (&new_call, INSERT);
1304 /* We need to keep a pointer to the entry, not just the slot, as the
1305 slot can move in the call to cxx_eval_builtin_function_call. */
1306 *slot = entry = ggc_alloc<constexpr_call> ();
1309 /* Calls which are in progress have their result set to NULL
1310 so that we can detect circular dependencies. */
1311 else if (entry->result == NULL)
1314 error ("call has circular dependency");
1315 *non_constant_p = true;
1316 entry->result = result = error_mark_node;
1319 result = entry->result;
1325 error ("constexpr evaluation depth exceeds maximum of %d (use "
1326 "-fconstexpr-depth= to increase the maximum)",
1327 max_constexpr_depth);
1328 *non_constant_p = true;
1329 result = error_mark_node;
1333 if (!result || result == error_mark_node)
1335 if (DECL_SAVED_TREE (fun) == NULL_TREE
1336 && (DECL_CONSTRUCTOR_P (fun) || DECL_DESTRUCTOR_P (fun)))
1337 /* The maybe-in-charge 'tor had its DECL_SAVED_TREE
1338 cleared, try a clone. */
1339 for (fun = DECL_CHAIN (fun);
1340 fun && DECL_CLONED_FUNCTION_P (fun);
1341 fun = DECL_CHAIN (fun))
1342 if (DECL_SAVED_TREE (fun))
1344 gcc_assert (DECL_SAVED_TREE (fun));
1347 /* Unshare the whole function body. */
1348 tree body = copy_fn (fun, parms, res);
1350 /* Associate the bindings with the remapped parms. */
1351 tree bound = new_call.bindings;
1352 tree remapped = parms;
1355 tree oparm = TREE_PURPOSE (bound);
1356 tree arg = TREE_VALUE (bound);
1357 gcc_assert (DECL_NAME (remapped) == DECL_NAME (oparm));
1358 ctx->values->put (remapped, arg);
1359 bound = TREE_CHAIN (bound);
1360 remapped = DECL_CHAIN (remapped);
1362 /* Add the RESULT_DECL to the values map, too. */
1363 tree slot = NULL_TREE;
1364 if (DECL_BY_REFERENCE (res))
1366 slot = AGGR_INIT_EXPR_SLOT (t);
1367 tree addr = build_address (slot);
1368 addr = build_nop (TREE_TYPE (res), addr);
1369 ctx->values->put (res, addr);
1370 ctx->values->put (slot, NULL_TREE);
1373 ctx->values->put (res, NULL_TREE);
1375 tree jump_target = NULL_TREE;
1376 cxx_eval_constant_expression (ctx, body,
1377 lval, non_constant_p, overflow_p,
1380 if (DECL_CONSTRUCTOR_P (fun))
1381 /* This can be null for a subobject constructor call, in
1382 which case what we care about is the initialization
1383 side-effects rather than the value. We could get at the
1384 value by evaluating *this, but we don't bother; there's
1385 no need to put such a call in the hash table. */
1386 result = lval ? ctx->object : ctx->ctor;
1387 else if (VOID_TYPE_P (TREE_TYPE (res)))
1391 result = *ctx->values->get (slot ? slot : res);
1392 if (result == NULL_TREE && !*non_constant_p)
1395 error ("constexpr call flows off the end "
1397 *non_constant_p = true;
1401 /* Remove the parms/result from the values map. Is it worth
1402 bothering to do this when the map itself is only live for
1403 one constexpr evaluation? If so, maybe also clear out
1404 other vars from call, maybe in BIND_EXPR handling? */
1405 ctx->values->remove (res);
1407 ctx->values->remove (slot);
1408 for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
1409 ctx->values->remove (parm);
1412 if (result == error_mark_node)
1413 *non_constant_p = true;
1414 if (*non_constant_p)
1415 result = error_mark_node;
1419 entry->result = result;
1422 pop_cx_call_context ();
1423 return unshare_expr (result);
1426 /* FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
1429 reduced_constant_expression_p (tree t)
1431 switch (TREE_CODE (t))
1434 /* Even if we can't lower this yet, it's constant. */
1438 /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
1439 tree elt; unsigned HOST_WIDE_INT idx;
1440 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), idx, elt)
1441 if (!reduced_constant_expression_p (elt))
1446 /* FIXME are we calling this too much? */
1447 return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE;
1451 /* Some expressions may have constant operands but are not constant
1452 themselves, such as 1/0. Call this function (or rather, the macro
1453 following it) to check for that condition.
1455 We only call this in places that require an arithmetic constant, not in
1456 places where we might have a non-constant expression that can be a
1457 component of a constant expression, such as the address of a constexpr
1458 variable that might be dereferenced later. */
1461 verify_constant (tree t, bool allow_non_constant, bool *non_constant_p,
1464 if (!*non_constant_p && !reduced_constant_expression_p (t))
1466 if (!allow_non_constant)
1467 error ("%q+E is not a constant expression", t);
1468 *non_constant_p = true;
1470 if (TREE_OVERFLOW_P (t))
1472 if (!allow_non_constant)
1474 permerror (input_location, "overflow in constant expression");
1475 /* If we're being permissive (and are in an enforcing
1476 context), ignore the overflow. */
1477 if (flag_permissive)
1478 return *non_constant_p;
1482 return *non_constant_p;
1485 /* Check whether the shift operation with code CODE and type TYPE on LHS
1486 and RHS is undefined. If it is, give an error with an explanation,
1487 and return true; return false otherwise. */
1490 cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx,
1491 enum tree_code code, tree type, tree lhs, tree rhs)
1493 if ((code != LSHIFT_EXPR && code != RSHIFT_EXPR)
1494 || TREE_CODE (lhs) != INTEGER_CST
1495 || TREE_CODE (rhs) != INTEGER_CST)
1498 tree lhstype = TREE_TYPE (lhs);
1499 unsigned HOST_WIDE_INT uprec = TYPE_PRECISION (TREE_TYPE (lhs));
1501 /* [expr.shift] The behavior is undefined if the right operand
1502 is negative, or greater than or equal to the length in bits
1503 of the promoted left operand. */
1504 if (tree_int_cst_sgn (rhs) == -1)
1507 error_at (loc, "right operand of shift expression %q+E is negative",
1508 build2_loc (loc, code, type, lhs, rhs));
1511 if (compare_tree_int (rhs, uprec) >= 0)
1514 error_at (loc, "right operand of shift expression %q+E is >= than "
1515 "the precision of the left operand",
1516 build2_loc (loc, code, type, lhs, rhs));
1520 /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
1521 if E1 has a signed type and non-negative value, and E1x2^E2 is
1522 representable in the corresponding unsigned type of the result type,
1523 then that value, converted to the result type, is the resulting value;
1524 otherwise, the behavior is undefined. */
1525 if (code == LSHIFT_EXPR && !TYPE_UNSIGNED (lhstype)
1526 && (cxx_dialect >= cxx11))
1528 if (tree_int_cst_sgn (lhs) == -1)
1531 error_at (loc, "left operand of shift expression %q+E is negative",
1532 build2_loc (loc, code, type, lhs, rhs));
1535 /* For signed x << y the following:
1536 (unsigned) x >> ((prec (lhs) - 1) - y)
1537 if > 1, is undefined. The right-hand side of this formula
1538 is the highest bit of the LHS that can be set (starting from 0),
1539 so that the shift doesn't overflow. We then right-shift the LHS
1540 to see whether any other bit is set making the original shift
1541 undefined -- the result is not representable in the corresponding
1543 tree t = build_int_cst (unsigned_type_node, uprec - 1);
1544 t = fold_build2 (MINUS_EXPR, unsigned_type_node, t, rhs);
1545 tree ulhs = fold_convert (unsigned_type_for (lhstype), lhs);
1546 t = fold_build2 (RSHIFT_EXPR, TREE_TYPE (ulhs), ulhs, t);
1547 if (tree_int_cst_lt (integer_one_node, t))
1550 error_at (loc, "shift expression %q+E overflows",
1551 build2_loc (loc, code, type, lhs, rhs));
1558 /* Subroutine of cxx_eval_constant_expression.
1559 Attempt to reduce the unary expression tree T to a compile time value.
1560 If successful, return the value. Otherwise issue a diagnostic
1561 and return error_mark_node. */
1564 cxx_eval_unary_expression (const constexpr_ctx *ctx, tree t,
1566 bool *non_constant_p, bool *overflow_p)
1569 tree orig_arg = TREE_OPERAND (t, 0);
1570 tree arg = cxx_eval_constant_expression (ctx, orig_arg, /*lval*/false,
1571 non_constant_p, overflow_p);
1572 VERIFY_CONSTANT (arg);
1573 location_t loc = EXPR_LOCATION (t);
1574 enum tree_code code = TREE_CODE (t);
1575 tree type = TREE_TYPE (t);
1576 r = fold_unary_loc (loc, code, type, arg);
1579 if (arg == orig_arg)
1582 r = build1_loc (loc, code, type, arg);
1584 VERIFY_CONSTANT (r);
1588 /* Subroutine of cxx_eval_constant_expression.
1589 Like cxx_eval_unary_expression, except for binary expressions. */
1592 cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t,
1594 bool *non_constant_p, bool *overflow_p)
1597 tree orig_lhs = TREE_OPERAND (t, 0);
1598 tree orig_rhs = TREE_OPERAND (t, 1);
1600 lhs = cxx_eval_constant_expression (ctx, orig_lhs, /*lval*/false,
1601 non_constant_p, overflow_p);
1602 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
1603 a local array in a constexpr function. */
1604 bool ptr = POINTER_TYPE_P (TREE_TYPE (lhs));
1606 VERIFY_CONSTANT (lhs);
1607 rhs = cxx_eval_constant_expression (ctx, orig_rhs, /*lval*/false,
1608 non_constant_p, overflow_p);
1610 VERIFY_CONSTANT (rhs);
1612 location_t loc = EXPR_LOCATION (t);
1613 enum tree_code code = TREE_CODE (t);
1614 tree type = TREE_TYPE (t);
1615 r = fold_binary_loc (loc, code, type, lhs, rhs);
1618 if (lhs == orig_lhs && rhs == orig_rhs)
1621 r = build2_loc (loc, code, type, lhs, rhs);
1623 else if (cxx_eval_check_shift_p (loc, ctx, code, type, lhs, rhs))
1624 *non_constant_p = true;
1626 VERIFY_CONSTANT (r);
1630 /* Subroutine of cxx_eval_constant_expression.
1631 Attempt to evaluate condition expressions. Dead branches are not
1635 cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t,
1637 bool *non_constant_p, bool *overflow_p,
1640 tree val = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
1642 non_constant_p, overflow_p);
1643 VERIFY_CONSTANT (val);
1644 /* Don't VERIFY_CONSTANT the other operands. */
1645 if (integer_zerop (val))
1646 return cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2),
1648 non_constant_p, overflow_p,
1650 return cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
1652 non_constant_p, overflow_p,
1656 /* Returns less than, equal to, or greater than zero if KEY is found to be
1657 less than, to match, or to be greater than the constructor_elt's INDEX. */
1660 array_index_cmp (tree key, tree index)
1662 gcc_assert (TREE_CODE (key) == INTEGER_CST);
1664 switch (TREE_CODE (index))
1667 return tree_int_cst_compare (key, index);
1670 tree lo = TREE_OPERAND (index, 0);
1671 tree hi = TREE_OPERAND (index, 1);
1672 if (tree_int_cst_lt (key, lo))
1674 else if (tree_int_cst_lt (hi, key))
1684 /* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1
1685 if none. If INSERT is true, insert a matching element rather than fail. */
1687 static HOST_WIDE_INT
1688 find_array_ctor_elt (tree ary, tree dindex, bool insert = false)
1690 if (tree_int_cst_sgn (dindex) < 0)
1693 unsigned HOST_WIDE_INT i = tree_to_uhwi (dindex);
1694 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ary);
1695 unsigned HOST_WIDE_INT len = vec_safe_length (elts);
1697 unsigned HOST_WIDE_INT end = len;
1698 unsigned HOST_WIDE_INT begin = 0;
1700 /* If the last element of the CONSTRUCTOR has its own index, we can assume
1701 that the same is true of the other elements and index directly. */
1704 tree cindex = (*elts)[end-1].index;
1705 if (TREE_CODE (cindex) == INTEGER_CST
1706 && compare_tree_int (cindex, end-1) == 0)
1715 /* Otherwise, find a matching index by means of a binary search. */
1716 while (begin != end)
1718 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
1720 int cmp = array_index_cmp (dindex, (*elts)[middle].index);
1731 constructor_elt e = { dindex, NULL_TREE };
1732 vec_safe_insert (CONSTRUCTOR_ELTS (ary), end, e);
1740 /* Subroutine of cxx_eval_constant_expression.
1741 Attempt to reduce a reference to an array slot. */
1744 cxx_eval_array_reference (const constexpr_ctx *ctx, tree t,
1746 bool *non_constant_p, bool *overflow_p)
1748 tree oldary = TREE_OPERAND (t, 0);
1749 tree ary = cxx_eval_constant_expression (ctx, oldary,
1751 non_constant_p, overflow_p);
1755 unsigned len, elem_nchars = 1;
1756 if (*non_constant_p)
1758 oldidx = TREE_OPERAND (t, 1);
1759 index = cxx_eval_constant_expression (ctx, oldidx,
1761 non_constant_p, overflow_p);
1762 VERIFY_CONSTANT (index);
1763 if (lval && ary == oldary && index == oldidx)
1766 return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL);
1767 elem_type = TREE_TYPE (TREE_TYPE (ary));
1768 if (TREE_CODE (ary) == CONSTRUCTOR)
1769 len = CONSTRUCTOR_NELTS (ary);
1770 else if (TREE_CODE (ary) == STRING_CST)
1772 elem_nchars = (TYPE_PRECISION (elem_type)
1773 / TYPE_PRECISION (char_type_node));
1774 len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars;
1778 /* We can't do anything with other tree codes, so use
1779 VERIFY_CONSTANT to complain and fail. */
1780 VERIFY_CONSTANT (ary);
1784 i = tree_to_shwi (index);
1788 error ("negative array subscript");
1789 *non_constant_p = true;
1794 if (TREE_CODE (ary) == CONSTRUCTOR)
1796 HOST_WIDE_INT ix = find_array_ctor_elt (ary, index);
1806 if (tree_int_cst_lt (index, array_type_nelts_top (TREE_TYPE (ary))))
1808 if (TREE_CODE (ary) == CONSTRUCTOR
1809 && CONSTRUCTOR_NO_IMPLICIT_ZERO (ary))
1811 /* 'ary' is part of the aggregate initializer we're currently
1812 building; if there's no initializer for this element yet,
1815 error ("accessing uninitialized array element");
1816 *non_constant_p = true;
1820 /* If it's within the array bounds but doesn't have an explicit
1821 initializer, it's value-initialized. */
1822 tree val = build_value_init (elem_type, tf_warning_or_error);
1823 return cxx_eval_constant_expression (ctx, val,
1825 non_constant_p, overflow_p);
1829 error ("array subscript out of bound");
1830 *non_constant_p = true;
1834 if (TREE_CODE (ary) == CONSTRUCTOR)
1835 return (*CONSTRUCTOR_ELTS (ary))[i].value;
1836 else if (elem_nchars == 1)
1837 return build_int_cst (cv_unqualified (TREE_TYPE (TREE_TYPE (ary))),
1838 TREE_STRING_POINTER (ary)[i]);
1841 tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (ary)));
1842 return native_interpret_expr (type, (const unsigned char *)
1843 TREE_STRING_POINTER (ary)
1844 + i * elem_nchars, elem_nchars);
1846 /* Don't VERIFY_CONSTANT here. */
1849 /* Subroutine of cxx_eval_constant_expression.
1850 Attempt to reduce a field access of a value of class type. */
1853 cxx_eval_component_reference (const constexpr_ctx *ctx, tree t,
1855 bool *non_constant_p, bool *overflow_p)
1857 unsigned HOST_WIDE_INT i;
1860 tree part = TREE_OPERAND (t, 1);
1861 tree orig_whole = TREE_OPERAND (t, 0);
1862 tree whole = cxx_eval_constant_expression (ctx, orig_whole,
1864 non_constant_p, overflow_p);
1865 if (TREE_CODE (whole) == PTRMEM_CST)
1866 whole = cplus_expand_constant (whole);
1867 if (whole == orig_whole)
1870 return fold_build3 (COMPONENT_REF, TREE_TYPE (t),
1871 whole, part, NULL_TREE);
1872 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
1874 if (!*non_constant_p && TREE_CODE (whole) != CONSTRUCTOR)
1877 error ("%qE is not a constant expression", orig_whole);
1878 *non_constant_p = true;
1880 if (DECL_MUTABLE_P (part))
1883 error ("mutable %qD is not usable in a constant expression", part);
1884 *non_constant_p = true;
1886 if (*non_constant_p)
1888 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
1895 /* We're in the middle of initializing it. */
1899 if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE
1900 && CONSTRUCTOR_NELTS (whole) > 0)
1902 /* DR 1188 says we don't have to deal with this. */
1904 error ("accessing %qD member instead of initialized %qD member in "
1905 "constant expression", part, CONSTRUCTOR_ELT (whole, 0)->index);
1906 *non_constant_p = true;
1910 if (CONSTRUCTOR_NO_IMPLICIT_ZERO (whole))
1912 /* 'whole' is part of the aggregate initializer we're currently
1913 building; if there's no initializer for this member yet, that's an
1916 error ("accessing uninitialized member %qD", part);
1917 *non_constant_p = true;
1921 /* If there's no explicit init for this field, it's value-initialized. */
1922 value = build_value_init (TREE_TYPE (t), tf_warning_or_error);
1923 return cxx_eval_constant_expression (ctx, value,
1925 non_constant_p, overflow_p);
1928 /* Subroutine of cxx_eval_constant_expression.
1929 Attempt to reduce a field access of a value of class type that is
1930 expressed as a BIT_FIELD_REF. */
1933 cxx_eval_bit_field_ref (const constexpr_ctx *ctx, tree t,
1935 bool *non_constant_p, bool *overflow_p)
1937 tree orig_whole = TREE_OPERAND (t, 0);
1938 tree retval, fldval, utype, mask;
1939 bool fld_seen = false;
1940 HOST_WIDE_INT istart, isize;
1941 tree whole = cxx_eval_constant_expression (ctx, orig_whole,
1943 non_constant_p, overflow_p);
1944 tree start, field, value;
1945 unsigned HOST_WIDE_INT i;
1947 if (whole == orig_whole)
1949 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
1951 if (!*non_constant_p
1952 && TREE_CODE (whole) != VECTOR_CST
1953 && TREE_CODE (whole) != CONSTRUCTOR)
1956 error ("%qE is not a constant expression", orig_whole);
1957 *non_constant_p = true;
1959 if (*non_constant_p)
1962 if (TREE_CODE (whole) == VECTOR_CST)
1963 return fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
1964 TREE_OPERAND (t, 1), TREE_OPERAND (t, 2));
1966 start = TREE_OPERAND (t, 2);
1967 istart = tree_to_shwi (start);
1968 isize = tree_to_shwi (TREE_OPERAND (t, 1));
1969 utype = TREE_TYPE (t);
1970 if (!TYPE_UNSIGNED (utype))
1971 utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1);
1972 retval = build_int_cst (utype, 0);
1973 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
1975 tree bitpos = bit_position (field);
1976 if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1))
1978 if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE
1979 && TREE_CODE (value) == INTEGER_CST
1980 && tree_fits_shwi_p (bitpos)
1981 && tree_fits_shwi_p (DECL_SIZE (field)))
1983 HOST_WIDE_INT bit = tree_to_shwi (bitpos);
1984 HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field));
1985 HOST_WIDE_INT shift;
1986 if (bit >= istart && bit + sz <= istart + isize)
1988 fldval = fold_convert (utype, value);
1989 mask = build_int_cst_type (utype, -1);
1990 mask = fold_build2 (LSHIFT_EXPR, utype, mask,
1991 size_int (TYPE_PRECISION (utype) - sz));
1992 mask = fold_build2 (RSHIFT_EXPR, utype, mask,
1993 size_int (TYPE_PRECISION (utype) - sz));
1994 fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask);
1995 shift = bit - istart;
1996 if (BYTES_BIG_ENDIAN)
1997 shift = TYPE_PRECISION (utype) - shift - sz;
1998 fldval = fold_build2 (LSHIFT_EXPR, utype, fldval,
2000 retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval);
2006 return fold_convert (TREE_TYPE (t), retval);
2008 return error_mark_node;
2011 /* Subroutine of cxx_eval_constant_expression.
2012 Evaluate a short-circuited logical expression T in the context
2013 of a given constexpr CALL. BAILOUT_VALUE is the value for
2014 early return. CONTINUE_VALUE is used here purely for
2015 sanity check purposes. */
2018 cxx_eval_logical_expression (const constexpr_ctx *ctx, tree t,
2019 tree bailout_value, tree continue_value,
2021 bool *non_constant_p, bool *overflow_p)
2024 tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
2026 non_constant_p, overflow_p);
2027 VERIFY_CONSTANT (lhs);
2028 if (tree_int_cst_equal (lhs, bailout_value))
2030 gcc_assert (tree_int_cst_equal (lhs, continue_value));
2031 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
2032 lval, non_constant_p,
2034 VERIFY_CONSTANT (r);
2038 /* REF is a COMPONENT_REF designating a particular field. V is a vector of
2039 CONSTRUCTOR elements to initialize (part of) an object containing that
2040 field. Return a pointer to the constructor_elt corresponding to the
2041 initialization of the field. */
2043 static constructor_elt *
2044 base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref)
2046 tree aggr = TREE_OPERAND (ref, 0);
2047 tree field = TREE_OPERAND (ref, 1);
2049 constructor_elt *ce;
2051 gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
2053 if (TREE_CODE (aggr) == COMPONENT_REF)
2055 constructor_elt *base_ce
2056 = base_field_constructor_elt (v, aggr);
2057 v = CONSTRUCTOR_ELTS (base_ce->value);
2060 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
2061 if (ce->index == field)
2068 /* Some of the expressions fed to the constexpr mechanism are calls to
2069 constructors, which have type void. In that case, return the type being
2070 initialized by the constructor. */
2073 initialized_type (tree t)
2077 tree type = cv_unqualified (TREE_TYPE (t));
2078 if (TREE_CODE (t) == CALL_EXPR || TREE_CODE (t) == AGGR_INIT_EXPR)
2080 /* A constructor call has void type, so we need to look deeper. */
2081 tree fn = get_function_named_in_call (t);
2082 if (fn && TREE_CODE (fn) == FUNCTION_DECL
2083 && DECL_CXX_CONSTRUCTOR_P (fn))
2084 type = DECL_CONTEXT (fn);
2089 /* We're about to initialize element INDEX of an array or class from VALUE.
2090 Set up NEW_CTX appropriately by adjusting .object to refer to the
2091 subobject and creating a new CONSTRUCTOR if the element is itself
2092 a class or array. */
2095 init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx,
2096 tree index, tree &value)
2100 if (index && TREE_CODE (index) != INTEGER_CST
2101 && TREE_CODE (index) != FIELD_DECL)
2102 /* This won't have an element in the new CONSTRUCTOR. */
2105 tree type = initialized_type (value);
2106 if (!AGGREGATE_TYPE_P (type) && !VECTOR_TYPE_P (type))
2107 /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
2110 /* The sub-aggregate initializer might contain a placeholder;
2111 update object to refer to the subobject and ctor to refer to
2112 the (newly created) sub-initializer. */
2114 new_ctx.object = build_ctor_subob_ref (index, type, ctx->object);
2115 tree elt = build_constructor (type, NULL);
2116 CONSTRUCTOR_NO_IMPLICIT_ZERO (elt) = true;
2119 if (TREE_CODE (value) == TARGET_EXPR)
2120 /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
2121 value = TARGET_EXPR_INITIAL (value);
2124 /* We're about to process an initializer for a class or array TYPE. Make
2125 sure that CTX is set up appropriately. */
2128 verify_ctor_sanity (const constexpr_ctx *ctx, tree type)
2130 /* We don't bother building a ctor for an empty base subobject. */
2131 if (is_empty_class (type))
2134 /* We're in the middle of an initializer that might involve placeholders;
2135 our caller should have created a CONSTRUCTOR for us to put the
2136 initializer into. We will either return that constructor or T. */
2137 gcc_assert (ctx->ctor);
2138 gcc_assert (same_type_ignoring_top_level_qualifiers_p
2139 (type, TREE_TYPE (ctx->ctor)));
2140 gcc_assert (CONSTRUCTOR_NELTS (ctx->ctor) == 0);
2142 gcc_assert (same_type_ignoring_top_level_qualifiers_p
2143 (type, TREE_TYPE (ctx->object)));
2144 gcc_assert (!ctx->object || !DECL_P (ctx->object)
2145 || *(ctx->values->get (ctx->object)) == ctx->ctor);
2148 /* Subroutine of cxx_eval_constant_expression.
2149 The expression tree T denotes a C-style array or a C-style
2150 aggregate. Reduce it to a constant expression. */
2153 cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t,
2155 bool *non_constant_p, bool *overflow_p)
2157 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
2158 bool changed = false;
2159 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t));
2161 verify_ctor_sanity (ctx, TREE_TYPE (t));
2162 vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
2163 vec_alloc (*p, vec_safe_length (v));
2165 unsigned i; tree index, value;
2166 FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value)
2168 constexpr_ctx new_ctx;
2169 init_subob_ctx (ctx, new_ctx, index, value);
2170 if (new_ctx.ctor != ctx->ctor)
2171 /* If we built a new CONSTRUCTOR, attach it now so that other
2172 initializers can refer to it. */
2173 CONSTRUCTOR_APPEND_ELT (*p, index, new_ctx.ctor);
2174 tree elt = cxx_eval_constant_expression (&new_ctx, value,
2176 non_constant_p, overflow_p);
2177 /* Don't VERIFY_CONSTANT here. */
2178 if (ctx->quiet && *non_constant_p)
2182 if (index && TREE_CODE (index) == COMPONENT_REF)
2184 /* This is an initialization of a vfield inside a base
2185 subaggregate that we already initialized; push this
2186 initialization into the previous initialization. */
2187 constructor_elt *inner = base_field_constructor_elt (*p, index);
2192 && (TREE_CODE (index) == NOP_EXPR
2193 || TREE_CODE (index) == POINTER_PLUS_EXPR))
2195 /* This is an initializer for an empty base; now that we've
2196 checked that it's constant, we can ignore it. */
2197 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index))));
2200 else if (new_ctx.ctor != ctx->ctor)
2202 /* We appended this element above; update the value. */
2203 gcc_assert ((*p)->last().index == index);
2204 (*p)->last().value = elt;
2207 CONSTRUCTOR_APPEND_ELT (*p, index, elt);
2209 if (*non_constant_p || !changed)
2212 /* We're done building this CONSTRUCTOR, so now we can interpret an
2213 element without an explicit initializer as value-initialized. */
2214 CONSTRUCTOR_NO_IMPLICIT_ZERO (t) = false;
2215 if (VECTOR_TYPE_P (TREE_TYPE (t)))
2220 /* Subroutine of cxx_eval_constant_expression.
2221 The expression tree T is a VEC_INIT_EXPR which denotes the desired
2222 initialization of a non-static data member of array type. Reduce it to a
2225 Note that apart from value-initialization (when VALUE_INIT is true),
2226 this is only intended to support value-initialization and the
2227 initializations done by defaulted constructors for classes with
2228 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
2229 will either be NULL_TREE for the default constructor, or a COMPONENT_REF
2230 for the copy/move constructor. */
2233 cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init,
2234 bool value_init, bool lval,
2235 bool *non_constant_p, bool *overflow_p)
2237 tree elttype = TREE_TYPE (atype);
2238 unsigned HOST_WIDE_INT max = tree_to_uhwi (array_type_nelts_top (atype));
2239 verify_ctor_sanity (ctx, atype);
2240 vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
2241 vec_alloc (*p, max + 1);
2242 bool pre_init = false;
2243 unsigned HOST_WIDE_INT i;
2245 /* For the default constructor, build up a call to the default
2246 constructor of the element type. We only need to handle class types
2247 here, as for a constructor to be constexpr, all members must be
2248 initialized, which for a defaulted default constructor means they must
2249 be of a class type with a constexpr default constructor. */
2250 if (TREE_CODE (elttype) == ARRAY_TYPE)
2251 /* We only do this at the lowest level. */;
2252 else if (value_init)
2254 init = build_value_init (elttype, tf_warning_or_error);
2259 vec<tree, va_gc> *argvec = make_tree_vector ();
2260 init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
2261 &argvec, elttype, LOOKUP_NORMAL,
2262 tf_warning_or_error);
2263 release_tree_vector (argvec);
2264 init = build_aggr_init_expr (TREE_TYPE (init), init);
2268 for (i = 0; i < max; ++i)
2270 tree idx = build_int_cst (size_type_node, i);
2272 constexpr_ctx new_ctx;
2273 init_subob_ctx (ctx, new_ctx, idx, pre_init ? init : elttype);
2274 if (new_ctx.ctor != ctx->ctor)
2275 CONSTRUCTOR_APPEND_ELT (*p, idx, new_ctx.ctor);
2276 if (TREE_CODE (elttype) == ARRAY_TYPE)
2278 /* A multidimensional array; recurse. */
2279 if (value_init || init == NULL_TREE)
2280 eltinit = NULL_TREE;
2282 eltinit = cp_build_array_ref (input_location, init, idx,
2283 tf_warning_or_error);
2284 eltinit = cxx_eval_vec_init_1 (&new_ctx, elttype, eltinit, value_init,
2286 non_constant_p, overflow_p);
2290 /* Initializing an element using value or default initialization
2291 we just pre-built above. */
2292 eltinit = (cxx_eval_constant_expression
2294 lval, non_constant_p, overflow_p));
2298 /* Copying an element. */
2299 gcc_assert (same_type_ignoring_top_level_qualifiers_p
2300 (atype, TREE_TYPE (init)));
2301 eltinit = cp_build_array_ref (input_location, init, idx,
2302 tf_warning_or_error);
2303 if (!real_lvalue_p (init))
2304 eltinit = move (eltinit);
2305 eltinit = force_rvalue (eltinit, tf_warning_or_error);
2306 eltinit = (cxx_eval_constant_expression
2307 (&new_ctx, eltinit, lval,
2308 non_constant_p, overflow_p));
2310 if (*non_constant_p && !ctx->quiet)
2312 if (new_ctx.ctor != ctx->ctor)
2314 /* We appended this element above; update the value. */
2315 gcc_assert ((*p)->last().index == idx);
2316 (*p)->last().value = eltinit;
2319 CONSTRUCTOR_APPEND_ELT (*p, idx, eltinit);
2322 if (!*non_constant_p)
2325 CONSTRUCTOR_NO_IMPLICIT_ZERO (init) = false;
2331 cxx_eval_vec_init (const constexpr_ctx *ctx, tree t,
2333 bool *non_constant_p, bool *overflow_p)
2335 tree atype = TREE_TYPE (t);
2336 tree init = VEC_INIT_EXPR_INIT (t);
2337 tree r = cxx_eval_vec_init_1 (ctx, atype, init,
2338 VEC_INIT_EXPR_VALUE_INIT (t),
2339 lval, non_constant_p, overflow_p);
2340 if (*non_constant_p)
2346 /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
2347 match. We want to be less strict for simple *& folding; if we have a
2348 non-const temporary that we access through a const pointer, that should
2349 work. We handle this here rather than change fold_indirect_ref_1
2350 because we're dealing with things like ADDR_EXPR of INTEGER_CST which
2351 don't really make sense outside of constant expression evaluation. Also
2352 we want to allow folding to COMPONENT_REF, which could cause trouble
2353 with TBAA in fold_indirect_ref_1.
2355 Try to keep this function synced with fold_indirect_ref_1. */
2358 cxx_fold_indirect_ref (location_t loc, tree type, tree op0, bool *empty_base)
2364 subtype = TREE_TYPE (sub);
2365 if (!POINTER_TYPE_P (subtype))
2368 if (TREE_CODE (sub) == ADDR_EXPR)
2370 tree op = TREE_OPERAND (sub, 0);
2371 tree optype = TREE_TYPE (op);
2373 /* *&CONST_DECL -> to the value of the const decl. */
2374 if (TREE_CODE (op) == CONST_DECL)
2375 return DECL_INITIAL (op);
2376 /* *&p => p; make sure to handle *&"str"[cst] here. */
2377 if (same_type_ignoring_top_level_qualifiers_p (optype, type))
2379 tree fop = fold_read_from_constant_string (op);
2385 /* *(foo *)&fooarray => fooarray[0] */
2386 else if (TREE_CODE (optype) == ARRAY_TYPE
2387 && (same_type_ignoring_top_level_qualifiers_p
2388 (type, TREE_TYPE (optype))))
2390 tree type_domain = TYPE_DOMAIN (optype);
2391 tree min_val = size_zero_node;
2392 if (type_domain && TYPE_MIN_VALUE (type_domain))
2393 min_val = TYPE_MIN_VALUE (type_domain);
2394 return build4_loc (loc, ARRAY_REF, type, op, min_val,
2395 NULL_TREE, NULL_TREE);
2397 /* *(foo *)&complexfoo => __real__ complexfoo */
2398 else if (TREE_CODE (optype) == COMPLEX_TYPE
2399 && (same_type_ignoring_top_level_qualifiers_p
2400 (type, TREE_TYPE (optype))))
2401 return fold_build1_loc (loc, REALPART_EXPR, type, op);
2402 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
2403 else if (VECTOR_TYPE_P (optype)
2404 && (same_type_ignoring_top_level_qualifiers_p
2405 (type, TREE_TYPE (optype))))
2407 tree part_width = TYPE_SIZE (type);
2408 tree index = bitsize_int (0);
2409 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
2411 /* Also handle conversion to an empty base class, which
2412 is represented with a NOP_EXPR. */
2413 else if (is_empty_class (type)
2414 && CLASS_TYPE_P (optype)
2415 && DERIVED_FROM_P (type, optype))
2420 /* *(foo *)&struct_with_foo_field => COMPONENT_REF */
2421 else if (RECORD_OR_UNION_TYPE_P (optype))
2423 tree field = TYPE_FIELDS (optype);
2424 for (; field; field = DECL_CHAIN (field))
2425 if (TREE_CODE (field) == FIELD_DECL
2426 && integer_zerop (byte_position (field))
2427 && (same_type_ignoring_top_level_qualifiers_p
2428 (TREE_TYPE (field), type)))
2430 return fold_build3 (COMPONENT_REF, type, op, field, NULL_TREE);
2435 else if (TREE_CODE (sub) == POINTER_PLUS_EXPR
2436 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
2438 tree op00 = TREE_OPERAND (sub, 0);
2439 tree op01 = TREE_OPERAND (sub, 1);
2442 if (TREE_CODE (op00) == ADDR_EXPR)
2445 op00 = TREE_OPERAND (op00, 0);
2446 op00type = TREE_TYPE (op00);
2448 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
2449 if (VECTOR_TYPE_P (op00type)
2450 && (same_type_ignoring_top_level_qualifiers_p
2451 (type, TREE_TYPE (op00type))))
2453 HOST_WIDE_INT offset = tree_to_shwi (op01);
2454 tree part_width = TYPE_SIZE (type);
2455 unsigned HOST_WIDE_INT part_widthi = tree_to_shwi (part_width)/BITS_PER_UNIT;
2456 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
2457 tree index = bitsize_int (indexi);
2459 if (offset / part_widthi < TYPE_VECTOR_SUBPARTS (op00type))
2460 return fold_build3_loc (loc,
2461 BIT_FIELD_REF, type, op00,
2465 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
2466 else if (TREE_CODE (op00type) == COMPLEX_TYPE
2467 && (same_type_ignoring_top_level_qualifiers_p
2468 (type, TREE_TYPE (op00type))))
2470 tree size = TYPE_SIZE_UNIT (type);
2471 if (tree_int_cst_equal (size, op01))
2472 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
2474 /* ((foo *)&fooarray)[1] => fooarray[1] */
2475 else if (TREE_CODE (op00type) == ARRAY_TYPE
2476 && (same_type_ignoring_top_level_qualifiers_p
2477 (type, TREE_TYPE (op00type))))
2479 tree type_domain = TYPE_DOMAIN (op00type);
2480 tree min_val = size_zero_node;
2481 if (type_domain && TYPE_MIN_VALUE (type_domain))
2482 min_val = TYPE_MIN_VALUE (type_domain);
2483 op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
2484 TYPE_SIZE_UNIT (type));
2485 op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
2486 return build4_loc (loc, ARRAY_REF, type, op00, op01,
2487 NULL_TREE, NULL_TREE);
2489 /* Also handle conversion to an empty base class, which
2490 is represented with a NOP_EXPR. */
2491 else if (is_empty_class (type)
2492 && CLASS_TYPE_P (op00type)
2493 && DERIVED_FROM_P (type, op00type))
2498 /* ((foo *)&struct_with_foo_field)[1] => COMPONENT_REF */
2499 else if (RECORD_OR_UNION_TYPE_P (op00type))
2501 tree field = TYPE_FIELDS (op00type);
2502 for (; field; field = DECL_CHAIN (field))
2503 if (TREE_CODE (field) == FIELD_DECL
2504 && tree_int_cst_equal (byte_position (field), op01)
2505 && (same_type_ignoring_top_level_qualifiers_p
2506 (TREE_TYPE (field), type)))
2508 return fold_build3 (COMPONENT_REF, type, op00,
2515 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
2516 else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
2517 && (same_type_ignoring_top_level_qualifiers_p
2518 (type, TREE_TYPE (TREE_TYPE (subtype)))))
2521 tree min_val = size_zero_node;
2522 tree newsub = cxx_fold_indirect_ref (loc, TREE_TYPE (subtype), sub, NULL);
2526 sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub);
2527 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
2528 if (type_domain && TYPE_MIN_VALUE (type_domain))
2529 min_val = TYPE_MIN_VALUE (type_domain);
2530 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
2538 cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t,
2540 bool *non_constant_p, bool *overflow_p)
2542 tree orig_op0 = TREE_OPERAND (t, 0);
2543 bool empty_base = false;
2545 /* First try to simplify it directly. */
2546 tree r = cxx_fold_indirect_ref (EXPR_LOCATION (t), TREE_TYPE (t), orig_op0,
2550 /* If that didn't work, evaluate the operand first. */
2551 tree op0 = cxx_eval_constant_expression (ctx, orig_op0,
2552 /*lval*/false, non_constant_p,
2554 /* Don't VERIFY_CONSTANT here. */
2555 if (*non_constant_p)
2558 r = cxx_fold_indirect_ref (EXPR_LOCATION (t), TREE_TYPE (t), op0,
2562 /* We couldn't fold to a constant value. Make sure it's not
2563 something we should have been able to fold. */
2566 if (TREE_CODE (sub) == ADDR_EXPR)
2568 gcc_assert (!same_type_ignoring_top_level_qualifiers_p
2569 (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t)));
2570 /* DR 1188 says we don't have to deal with this. */
2572 error ("accessing value of %qE through a %qT glvalue in a "
2573 "constant expression", build_fold_indirect_ref (sub),
2575 *non_constant_p = true;
2579 if (lval && op0 != orig_op0)
2580 return build1 (INDIRECT_REF, TREE_TYPE (t), op0);
2582 VERIFY_CONSTANT (t);
2587 r = cxx_eval_constant_expression (ctx, r,
2588 lval, non_constant_p, overflow_p);
2589 if (*non_constant_p)
2592 /* If we're pulling out the value of an empty base, make sure
2593 that the whole object is constant and then return an empty
2595 if (empty_base && !lval)
2597 VERIFY_CONSTANT (r);
2598 r = build_constructor (TREE_TYPE (t), NULL);
2599 TREE_CONSTANT (r) = true;
2605 /* Complain about R, a VAR_DECL, not being usable in a constant expression.
2606 Shared between potential_constant_expression and
2607 cxx_eval_constant_expression. */
2610 non_const_var_error (tree r)
2612 tree type = TREE_TYPE (r);
2613 error ("the value of %qD is not usable in a constant "
2615 /* Avoid error cascade. */
2616 if (DECL_INITIAL (r) == error_mark_node)
2618 if (DECL_DECLARED_CONSTEXPR_P (r))
2619 inform (DECL_SOURCE_LOCATION (r),
2620 "%qD used in its own initializer", r);
2621 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
2623 if (!CP_TYPE_CONST_P (type))
2624 inform (DECL_SOURCE_LOCATION (r),
2625 "%q#D is not const", r);
2626 else if (CP_TYPE_VOLATILE_P (type))
2627 inform (DECL_SOURCE_LOCATION (r),
2628 "%q#D is volatile", r);
2629 else if (!DECL_INITIAL (r)
2630 || !TREE_CONSTANT (DECL_INITIAL (r)))
2631 inform (DECL_SOURCE_LOCATION (r),
2632 "%qD was not initialized with a constant "
2639 if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r))
2640 inform (DECL_SOURCE_LOCATION (r),
2641 "%qD was not declared %<constexpr%>", r);
2643 inform (DECL_SOURCE_LOCATION (r),
2644 "%qD does not have integral or enumeration type",
2649 /* Subroutine of cxx_eval_constant_expression.
2650 Like cxx_eval_unary_expression, except for trinary expressions. */
2653 cxx_eval_trinary_expression (const constexpr_ctx *ctx, tree t,
2655 bool *non_constant_p, bool *overflow_p)
2661 for (i = 0; i < 3; i++)
2663 args[i] = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, i),
2665 non_constant_p, overflow_p);
2666 VERIFY_CONSTANT (args[i]);
2669 val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t),
2670 args[0], args[1], args[2]);
2671 if (val == NULL_TREE)
2673 VERIFY_CONSTANT (val);
2678 var_in_constexpr_fn (tree t)
2680 tree ctx = DECL_CONTEXT (t);
2681 return (cxx_dialect >= cxx14 && ctx && TREE_CODE (ctx) == FUNCTION_DECL
2682 && DECL_DECLARED_CONSTEXPR_P (ctx));
2685 /* Evaluate an INIT_EXPR or MODIFY_EXPR. */
2688 cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
2690 bool *non_constant_p, bool *overflow_p)
2692 constexpr_ctx new_ctx = *ctx;
2694 tree init = TREE_OPERAND (t, 1);
2695 if (TREE_CLOBBER_P (init))
2696 /* Just ignore clobbers. */
2699 /* First we figure out where we're storing to. */
2700 tree target = TREE_OPERAND (t, 0);
2701 tree type = TREE_TYPE (target);
2702 target = cxx_eval_constant_expression (ctx, target,
2704 non_constant_p, overflow_p);
2705 if (*non_constant_p)
2708 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (target), type))
2710 /* For initialization of an empty base, the original target will be
2711 *(base*)this, which the above evaluation resolves to the object
2712 argument, which has the derived type rather than the base type. In
2713 this situation, just evaluate the initializer and return, since
2714 there's no actual data to store. */
2715 gcc_assert (is_empty_class (type));
2716 return cxx_eval_constant_expression (ctx, init, false,
2717 non_constant_p, overflow_p);
2720 /* And then find the underlying variable. */
2721 vec<tree,va_gc> *refs = make_tree_vector();
2722 tree object = NULL_TREE;
2723 for (tree probe = target; object == NULL_TREE; )
2725 switch (TREE_CODE (probe))
2730 vec_safe_push (refs, TREE_OPERAND (probe, 1));
2731 vec_safe_push (refs, TREE_TYPE (probe));
2732 probe = TREE_OPERAND (probe, 0);
2740 /* And then find/build up our initializer for the path to the subobject
2741 we're initializing. */
2743 if (DECL_P (object))
2744 valp = ctx->values->get (object);
2749 /* A constant-expression cannot modify objects from outside the
2750 constant-expression. */
2752 error ("modification of %qE is not a constant-expression", object);
2753 *non_constant_p = true;
2756 type = TREE_TYPE (object);
2757 bool no_zero_init = true;
2758 while (!refs->is_empty())
2760 if (*valp == NULL_TREE)
2762 *valp = build_constructor (type, NULL);
2763 CONSTRUCTOR_NO_IMPLICIT_ZERO (*valp) = no_zero_init;
2765 /* If the value of object is already zero-initialized, any new ctors for
2766 subobjects will also be zero-initialized. */
2767 no_zero_init = CONSTRUCTOR_NO_IMPLICIT_ZERO (*valp);
2769 enum tree_code code = TREE_CODE (type);
2771 tree index = refs->pop();
2773 constructor_elt *cep = NULL;
2774 if (code == ARRAY_TYPE)
2777 = find_array_ctor_elt (*valp, index, /*insert*/true);
2778 gcc_assert (i >= 0);
2779 cep = CONSTRUCTOR_ELT (*valp, i);
2780 gcc_assert (TREE_CODE (cep->index) != RANGE_EXPR);
2784 gcc_assert (TREE_CODE (index) == FIELD_DECL);
2785 for (unsigned HOST_WIDE_INT idx = 0;
2786 vec_safe_iterate (CONSTRUCTOR_ELTS (*valp), idx, &cep);
2788 if (index == cep->index)
2792 constructor_elt ce = { index, NULL_TREE };
2793 cep = vec_safe_push (CONSTRUCTOR_ELTS (*valp), ce);
2798 release_tree_vector (refs);
2800 if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
2802 /* Create a new CONSTRUCTOR in case evaluation of the initializer
2803 wants to modify it. */
2804 if (*valp == NULL_TREE)
2806 *valp = new_ctx.ctor = build_constructor (type, NULL);
2807 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx.ctor) = no_zero_init;
2810 new_ctx.ctor = *valp;
2811 new_ctx.object = target;
2814 init = cxx_eval_constant_expression (&new_ctx, init, false,
2815 non_constant_p, overflow_p);
2816 if (target == object)
2818 /* The hash table might have moved since the get earlier. */
2819 valp = ctx->values->get (object);
2820 if (TREE_CODE (init) == CONSTRUCTOR)
2821 /* An outer ctx->ctor might be pointing to *valp, so just replace
2823 CONSTRUCTOR_ELTS (*valp) = CONSTRUCTOR_ELTS (init);
2830 if (*non_constant_p)
2838 /* Evaluate a ++ or -- expression. */
2841 cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t,
2843 bool *non_constant_p, bool *overflow_p)
2845 enum tree_code code = TREE_CODE (t);
2846 tree type = TREE_TYPE (t);
2847 tree op = TREE_OPERAND (t, 0);
2848 tree offset = TREE_OPERAND (t, 1);
2849 gcc_assert (TREE_CONSTANT (offset));
2851 /* The operand as an lvalue. */
2852 op = cxx_eval_constant_expression (ctx, op, true,
2853 non_constant_p, overflow_p);
2855 /* The operand as an rvalue. */
2856 tree val = rvalue (op);
2857 val = cxx_eval_constant_expression (ctx, val, false,
2858 non_constant_p, overflow_p);
2859 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
2860 a local array in a constexpr function. */
2861 bool ptr = POINTER_TYPE_P (TREE_TYPE (val));
2863 VERIFY_CONSTANT (val);
2865 /* The modified value. */
2866 bool inc = (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR);
2868 if (POINTER_TYPE_P (type))
2870 /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
2871 offset = convert_to_ptrofftype (offset);
2873 offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset);
2874 mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset);
2877 mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset);
2879 VERIFY_CONSTANT (mod);
2881 /* Storing the modified value. */
2882 tree store = build2 (MODIFY_EXPR, type, op, mod);
2883 cxx_eval_constant_expression (ctx, store,
2884 true, non_constant_p, overflow_p);
2886 /* And the value of the expression. */
2887 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
2889 /* Prefix ops are lvalues. */
2893 /* But we optimize when the caller wants an rvalue. */
2897 /* Postfix ops are rvalues. */
2901 /* Predicates for the meaning of *jump_target. */
2904 returns (tree *jump_target)
2907 && TREE_CODE (*jump_target) == RETURN_EXPR;
2911 breaks (tree *jump_target)
2914 && TREE_CODE (*jump_target) == LABEL_DECL
2915 && LABEL_DECL_BREAK (*jump_target);
2919 continues (tree *jump_target)
2922 && TREE_CODE (*jump_target) == LABEL_DECL
2923 && LABEL_DECL_CONTINUE (*jump_target);
2927 switches (tree *jump_target)
2930 && TREE_CODE (*jump_target) == INTEGER_CST;
2933 /* Subroutine of cxx_eval_statement_list. Determine whether the statement
2934 at I matches *jump_target. If we're looking for a case label and we see
2935 the default label, copy I into DEFAULT_LABEL. */
2938 label_matches (tree *jump_target, tree_stmt_iterator i,
2939 tree_stmt_iterator& default_label)
2941 tree stmt = tsi_stmt (i);
2942 switch (TREE_CODE (*jump_target))
2945 if (TREE_CODE (stmt) == LABEL_EXPR
2946 && LABEL_EXPR_LABEL (stmt) == *jump_target)
2951 if (TREE_CODE (stmt) == CASE_LABEL_EXPR)
2953 if (!CASE_LOW (stmt))
2955 else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt)))
2966 /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
2967 semantics, for switch, break, continue, and return. */
2970 cxx_eval_statement_list (const constexpr_ctx *ctx, tree t,
2971 bool *non_constant_p, bool *overflow_p,
2974 tree_stmt_iterator i;
2975 tree_stmt_iterator default_label = tree_stmt_iterator();
2977 /* In a statement-expression we want to return the last value. */
2981 local_target = NULL_TREE;
2982 jump_target = &local_target;
2984 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
2987 tree stmt = tsi_stmt (i);
2990 if (TREE_CODE (stmt) == STATEMENT_LIST)
2991 /* The label we want might be inside. */;
2992 else if (label_matches (jump_target, i, default_label))
2994 *jump_target = NULL_TREE;
2998 r = cxx_eval_constant_expression (ctx, stmt, false,
2999 non_constant_p, overflow_p,
3001 if (*non_constant_p)
3003 if (returns (jump_target) || breaks (jump_target))
3006 if (switches (jump_target) && !tsi_end_p (default_label))
3009 *jump_target = NULL_TREE;
3015 /* Evaluate a LOOP_EXPR for side-effects. Handles break and return
3016 semantics; continue semantics are covered by cxx_eval_statement_list. */
3019 cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t,
3020 bool *non_constant_p, bool *overflow_p,
3023 tree body = TREE_OPERAND (t, 0);
3026 cxx_eval_statement_list (ctx, body,
3027 non_constant_p, overflow_p, jump_target);
3028 if (returns (jump_target) || breaks (jump_target) || *non_constant_p)
3031 if (breaks (jump_target))
3032 *jump_target = NULL_TREE;
3036 /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
3040 cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t,
3041 bool *non_constant_p, bool *overflow_p,
3044 tree cond = TREE_OPERAND (t, 0);
3045 cond = cxx_eval_constant_expression (ctx, cond, false,
3046 non_constant_p, overflow_p);
3047 VERIFY_CONSTANT (cond);
3048 *jump_target = cond;
3050 tree body = TREE_OPERAND (t, 1);
3051 cxx_eval_statement_list (ctx, body,
3052 non_constant_p, overflow_p, jump_target);
3053 if (breaks (jump_target) || switches (jump_target))
3054 *jump_target = NULL_TREE;
3058 /* Subroutine of cxx_eval_constant_expression.
3059 Attempt to reduce a POINTER_PLUS_EXPR expression T. */
3062 cxx_eval_pointer_plus_expression (const constexpr_ctx *ctx, tree t,
3063 bool lval, bool *non_constant_p,
3066 tree orig_type = TREE_TYPE (t);
3067 tree op00 = TREE_OPERAND (t, 0);
3068 tree op01 = TREE_OPERAND (t, 1);
3069 location_t loc = EXPR_LOCATION (t);
3071 op00 = cxx_eval_constant_expression (ctx, op00, lval,
3072 non_constant_p, overflow_p);
3075 if (TREE_CODE (op00) != ADDR_EXPR)
3078 op00 = TREE_OPERAND (op00, 0);
3080 /* &A[i] p+ j => &A[i + j] */
3081 if (TREE_CODE (op00) == ARRAY_REF
3082 && TREE_CODE (TREE_OPERAND (op00, 1)) == INTEGER_CST
3083 && TREE_CODE (op01) == INTEGER_CST
3084 && TYPE_SIZE_UNIT (TREE_TYPE (op00))
3085 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (op00))) == INTEGER_CST)
3087 tree type = TREE_TYPE (op00);
3088 t = fold_convert_loc (loc, ssizetype, TREE_OPERAND (op00, 1));
3089 tree nelts = array_type_nelts_top (TREE_TYPE (TREE_OPERAND (op00, 0)));
3090 /* Don't fold an out-of-bound access. */
3091 if (!tree_int_cst_le (t, nelts))
3093 op01 = cp_fold_convert (ssizetype, op01);
3094 /* Don't fold if op01 can't be divided exactly by TYPE_SIZE_UNIT.
3095 constexpr int A[1]; ... (char *)&A[0] + 1 */
3096 if (!integer_zerop (fold_build2_loc (loc, TRUNC_MOD_EXPR, sizetype,
3097 op01, TYPE_SIZE_UNIT (type))))
3099 /* Make sure to treat the second operand of POINTER_PLUS_EXPR
3101 op01 = fold_build2_loc (loc, EXACT_DIV_EXPR, ssizetype, op01,
3102 TYPE_SIZE_UNIT (type));
3103 t = size_binop_loc (loc, PLUS_EXPR, op01, t);
3104 t = build4_loc (loc, ARRAY_REF, type, TREE_OPERAND (op00, 0),
3105 t, NULL_TREE, NULL_TREE);
3106 t = cp_build_addr_expr (t, tf_warning_or_error);
3107 t = cp_fold_convert (orig_type, t);
3108 return cxx_eval_constant_expression (ctx, t, lval, non_constant_p,
3115 /* Attempt to reduce the expression T to a constant value.
3116 On failure, issue diagnostic and return error_mark_node. */
3117 /* FIXME unify with c_fully_fold */
3118 /* FIXME overflow_p is too global */
3121 cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
3123 bool *non_constant_p, bool *overflow_p,
3126 constexpr_ctx new_ctx;
3129 if (t == error_mark_node)
3131 *non_constant_p = true;
3134 if (CONSTANT_CLASS_P (t))
3136 if (TREE_OVERFLOW (t) && (!flag_permissive || ctx->quiet))
3141 switch (TREE_CODE (t))
3146 /* We ask for an rvalue for the RESULT_DECL when indirecting
3147 through an invisible reference, or in named return value
3149 return (*ctx->values->get (t));
3153 /* We used to not check lval for CONST_DECL, but darwin.c uses
3154 CONST_DECL for aggregate constants. */
3158 r = decl_really_constant_value (t);
3160 r = decl_constant_value (t);
3161 if (TREE_CODE (r) == TARGET_EXPR
3162 && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR)
3163 r = TARGET_EXPR_INITIAL (r);
3165 if (tree *p = ctx->values->get (r))
3170 non_const_var_error (r);
3171 *non_constant_p = true;
3179 case CASE_LABEL_EXPR:
3183 if (lval && TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
3185 else if (tree *p = ctx->values->get (r))
3188 /* Defer in case this is only used for its type. */;
3189 else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
3190 /* Defer, there's no lvalue->rvalue conversion. */;
3191 else if (is_empty_class (TREE_TYPE (t)))
3193 /* If the class is empty, we aren't actually loading anything. */
3194 r = build_constructor (TREE_TYPE (t), NULL);
3195 TREE_CONSTANT (r) = true;
3200 error ("%qE is not a constant expression", t);
3201 *non_constant_p = true;
3206 case AGGR_INIT_EXPR:
3207 r = cxx_eval_call_expression (ctx, t, lval,
3208 non_constant_p, overflow_p);
3213 r = DECL_EXPR_DECL (t);
3214 if (AGGREGATE_TYPE_P (TREE_TYPE (r))
3215 || VECTOR_TYPE_P (TREE_TYPE (r)))
3219 new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL);
3220 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx.ctor) = true;
3221 new_ctx.values->put (r, new_ctx.ctor);
3225 if (tree init = DECL_INITIAL (r))
3227 init = cxx_eval_constant_expression (ctx, init,
3229 non_constant_p, overflow_p);
3230 ctx->values->put (r, init);
3232 else if (ctx == &new_ctx)
3233 /* We gave it a CONSTRUCTOR above. */;
3235 ctx->values->put (r, NULL_TREE);
3240 if (!literal_type_p (TREE_TYPE (t)))
3244 error ("temporary of non-literal type %qT in a "
3245 "constant expression", TREE_TYPE (t));
3246 explain_non_literal_class (TREE_TYPE (t));
3248 *non_constant_p = true;
3251 if ((AGGREGATE_TYPE_P (TREE_TYPE (t)) || VECTOR_TYPE_P (TREE_TYPE (t))))
3253 /* We're being expanded without an explicit target, so start
3254 initializing a new object; expansion with an explicit target
3255 strips the TARGET_EXPR before we get here. */
3257 new_ctx.ctor = build_constructor (TREE_TYPE (t), NULL);
3258 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx.ctor) = true;
3259 new_ctx.object = TARGET_EXPR_SLOT (t);
3260 ctx->values->put (new_ctx.object, new_ctx.ctor);
3263 /* Pass false for 'lval' because this indicates
3264 initialization of a temporary. */
3265 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
3267 non_constant_p, overflow_p);
3268 if (!*non_constant_p)
3269 /* Adjust the type of the result to the type of the temporary. */
3270 r = adjust_temp_type (TREE_TYPE (t), r);
3273 tree slot = TARGET_EXPR_SLOT (t);
3274 ctx->values->put (slot, r);
3281 r = cxx_eval_store_expression (ctx, t, lval,
3282 non_constant_p, overflow_p);
3286 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
3288 non_constant_p, overflow_p);
3292 if (TREE_OPERAND (t, 0) != NULL_TREE)
3293 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
3295 non_constant_p, overflow_p);
3300 /* Avoid evaluating a SAVE_EXPR more than once. */
3301 if (tree *p = ctx->values->get (t))
3305 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), false,
3306 non_constant_p, overflow_p);
3307 ctx->values->put (t, r);
3311 case NON_LVALUE_EXPR:
3312 case TRY_CATCH_EXPR:
3314 case CLEANUP_POINT_EXPR:
3315 case MUST_NOT_THROW_EXPR:
3318 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
3320 non_constant_p, overflow_p,
3324 case TRY_FINALLY_EXPR:
3325 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
3326 non_constant_p, overflow_p,
3328 if (!*non_constant_p)
3329 /* Also evaluate the cleanup. */
3330 cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), true,
3331 non_constant_p, overflow_p,
3335 /* These differ from cxx_eval_unary_expression in that this doesn't
3336 check for a constant operand or result; an address can be
3337 constant without its operand being, and vice versa. */
3339 r = cxx_eval_indirect_ref (ctx, t, lval,
3340 non_constant_p, overflow_p);
3345 tree oldop = TREE_OPERAND (t, 0);
3346 tree op = cxx_eval_constant_expression (ctx, oldop,
3348 non_constant_p, overflow_p);
3349 /* Don't VERIFY_CONSTANT here. */
3350 if (*non_constant_p)
3352 gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR);
3353 /* This function does more aggressive folding than fold itself. */
3354 r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
3355 if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
3363 case FIX_TRUNC_EXPR:
3368 case TRUTH_NOT_EXPR:
3369 case FIXED_CONVERT_EXPR:
3370 r = cxx_eval_unary_expression (ctx, t, lval,
3371 non_constant_p, overflow_p);
3375 if (SIZEOF_EXPR_TYPE_P (t))
3376 r = cxx_sizeof_or_alignof_type (TREE_TYPE (TREE_OPERAND (t, 0)),
3377 SIZEOF_EXPR, false);
3378 else if (TYPE_P (TREE_OPERAND (t, 0)))
3379 r = cxx_sizeof_or_alignof_type (TREE_OPERAND (t, 0), SIZEOF_EXPR,
3382 r = cxx_sizeof_or_alignof_expr (TREE_OPERAND (t, 0), SIZEOF_EXPR,
3384 if (r == error_mark_node)
3386 VERIFY_CONSTANT (r);
3391 /* check_return_expr sometimes wraps a TARGET_EXPR in a
3392 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
3393 introduced by build_call_a. */
3394 tree op0 = TREE_OPERAND (t, 0);
3395 tree op1 = TREE_OPERAND (t, 1);
3397 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
3398 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
3399 r = cxx_eval_constant_expression (ctx, op0,
3400 lval, non_constant_p, overflow_p,
3404 /* Check that the LHS is constant and then discard it. */
3405 cxx_eval_constant_expression (ctx, op0,
3406 true, non_constant_p, overflow_p,
3408 op1 = TREE_OPERAND (t, 1);
3409 r = cxx_eval_constant_expression (ctx, op1,
3410 lval, non_constant_p, overflow_p,
3416 case POINTER_PLUS_EXPR:
3417 r = cxx_eval_pointer_plus_expression (ctx, t, lval, non_constant_p,
3421 /* else fall through */
3426 case TRUNC_DIV_EXPR:
3428 case FLOOR_DIV_EXPR:
3429 case ROUND_DIV_EXPR:
3430 case TRUNC_MOD_EXPR:
3432 case ROUND_MOD_EXPR:
3434 case EXACT_DIV_EXPR:
3444 case TRUTH_XOR_EXPR:
3451 case UNORDERED_EXPR:
3461 r = cxx_eval_binary_expression (ctx, t, lval,
3462 non_constant_p, overflow_p);
3465 /* fold can introduce non-IF versions of these; still treat them as
3466 short-circuiting. */
3467 case TRUTH_AND_EXPR:
3468 case TRUTH_ANDIF_EXPR:
3469 r = cxx_eval_logical_expression (ctx, t, boolean_false_node,
3472 non_constant_p, overflow_p);
3476 case TRUTH_ORIF_EXPR:
3477 r = cxx_eval_logical_expression (ctx, t, boolean_true_node,
3480 non_constant_p, overflow_p);
3484 r = cxx_eval_array_reference (ctx, t, lval,
3485 non_constant_p, overflow_p);
3489 if (is_overloaded_fn (t))
3491 /* We can only get here in checking mode via
3492 build_non_dependent_expr, because any expression that
3493 calls or takes the address of the function will have
3494 pulled a FUNCTION_DECL out of the COMPONENT_REF. */
3495 gcc_checking_assert (ctx->quiet || errorcount);
3496 *non_constant_p = true;
3499 r = cxx_eval_component_reference (ctx, t, lval,
3500 non_constant_p, overflow_p);
3504 r = cxx_eval_bit_field_ref (ctx, t, lval,
3505 non_constant_p, overflow_p);
3510 r = cxx_eval_conditional_expression (ctx, t, lval,
3511 non_constant_p, overflow_p,
3516 if (TREE_CONSTANT (t))
3517 /* Don't re-process a constant CONSTRUCTOR, but do fold it to
3518 VECTOR_CST if applicable. */
3520 r = cxx_eval_bare_aggregate (ctx, t, lval,
3521 non_constant_p, overflow_p);
3525 /* We can get this in a defaulted constructor for a class with a
3526 non-static data member of array type. Either the initializer will
3527 be NULL, meaning default-initialization, or it will be an lvalue
3528 or xvalue of the same type, meaning direct-initialization from the
3529 corresponding member. */
3530 r = cxx_eval_vec_init (ctx, t, lval,
3531 non_constant_p, overflow_p);
3536 r = cxx_eval_trinary_expression (ctx, t, lval,
3537 non_constant_p, overflow_p);
3541 case VIEW_CONVERT_EXPR:
3544 tree oldop = TREE_OPERAND (t, 0);
3545 tree op = cxx_eval_constant_expression (ctx, oldop,
3547 non_constant_p, overflow_p);
3548 if (*non_constant_p)
3550 tree type = TREE_TYPE (t);
3551 if (TREE_CODE (op) == PTRMEM_CST
3552 && !TYPE_PTRMEM_P (type))
3553 op = cplus_expand_constant (op);
3554 if (POINTER_TYPE_P (type)
3555 && TREE_CODE (op) == INTEGER_CST
3556 && !integer_zerop (op))
3559 error_at (EXPR_LOC_OR_LOC (t, input_location),
3560 "reinterpret_cast from integer to pointer");
3561 *non_constant_p = true;
3565 /* We didn't fold at the top so we could check for ptr-int
3568 r = fold_build1 (TREE_CODE (t), type, op);
3569 /* Conversion of an out-of-range value has implementation-defined
3570 behavior; the language considers it different from arithmetic
3571 overflow, which is undefined. */
3572 if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op))
3573 TREE_OVERFLOW (r) = false;
3577 case EMPTY_CLASS_EXPR:
3578 /* This is good enough for a function argument that might not get
3579 used, and they can't do anything with it, so just return it. */
3582 case STATEMENT_LIST:
3584 new_ctx.ctor = new_ctx.object = NULL_TREE;
3585 return cxx_eval_statement_list (&new_ctx, t,
3586 non_constant_p, overflow_p, jump_target);
3589 return cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t),
3591 non_constant_p, overflow_p,
3594 case PREINCREMENT_EXPR:
3595 case POSTINCREMENT_EXPR:
3596 case PREDECREMENT_EXPR:
3597 case POSTDECREMENT_EXPR:
3598 return cxx_eval_increment_expression (ctx, t,
3599 lval, non_constant_p, overflow_p);
3605 case VEC_DELETE_EXPR:
3608 /* GCC internal stuff. */
3611 case WITH_CLEANUP_EXPR:
3612 case NON_DEPENDENT_EXPR:
3616 error_at (EXPR_LOC_OR_LOC (t, input_location),
3617 "expression %qE is not a constant-expression", t);
3618 *non_constant_p = true;
3621 case PLACEHOLDER_EXPR:
3622 if (!ctx || !ctx->ctor || (lval && !ctx->object)
3623 || !(same_type_ignoring_top_level_qualifiers_p
3624 (TREE_TYPE (t), TREE_TYPE (ctx->ctor))))
3626 /* A placeholder without a referent. We can get here when
3627 checking whether NSDMIs are noexcept, or in massage_init_elt;
3628 just say it's non-constant for now. */
3629 gcc_assert (ctx->quiet);
3630 *non_constant_p = true;
3635 /* Use of the value or address of the current object. We could
3636 use ctx->object unconditionally, but using ctx->ctor when we
3637 can is a minor optimization. */
3638 tree ctor = lval ? ctx->object : ctx->ctor;
3639 return cxx_eval_constant_expression
3641 non_constant_p, overflow_p);
3646 *jump_target = TREE_OPERAND (t, 0);
3647 gcc_assert (breaks (jump_target) || continues (jump_target));
3651 cxx_eval_loop_expr (ctx, t,
3652 non_constant_p, overflow_p, jump_target);
3656 cxx_eval_switch_expr (ctx, t,
3657 non_constant_p, overflow_p, jump_target);
3661 /* It's possible to get a requires-expression in a constant
3662 expression. For example:
3664 template<typename T> concept bool C() {
3665 return requires (T t) { t; };
3668 template<typename T> requires !C<T>() void f(T);
3670 Normalization leaves f with the associated constraint
3671 '!requires (T t) { ... }' which is not transformed into
3673 if (!processing_template_decl)
3674 return evaluate_constraint_expression (t, NULL_TREE);
3676 *non_constant_p = true;
3680 if (STATEMENT_CODE_P (TREE_CODE (t)))
3682 /* This function doesn't know how to deal with pre-genericize
3683 statements; this can only happen with statement-expressions,
3684 so for now just fail. */
3686 error_at (EXPR_LOCATION (t),
3687 "statement is not a constant-expression");
3690 internal_error ("unexpected expression %qE of kind %s", t,
3691 get_tree_code_name (TREE_CODE (t)));
3692 *non_constant_p = true;
3696 if (r == error_mark_node)
3697 *non_constant_p = true;
3699 if (*non_constant_p)
3706 cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant,
3707 bool strict = true, tree object = NULL_TREE)
3709 bool non_constant_p = false;
3710 bool overflow_p = false;
3711 hash_map<tree,tree> map;
3712 constexpr_ctx ctx = { NULL, &map, NULL, NULL, allow_non_constant, strict };
3713 tree type = initialized_type (t);
3715 if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
3717 /* In C++14 an NSDMI can participate in aggregate initialization,
3718 and can refer to the address of the object being initialized, so
3719 we need to pass in the relevant VAR_DECL if we want to do the
3720 evaluation in a single pass. The evaluation will dynamically
3721 update ctx.values for the VAR_DECL. We use the same strategy
3722 for C++11 constexpr constructors that refer to the object being
3724 ctx.ctor = build_constructor (type, NULL);
3725 CONSTRUCTOR_NO_IMPLICIT_ZERO (ctx.ctor) = true;
3728 if (TREE_CODE (t) == TARGET_EXPR)
3729 object = TARGET_EXPR_SLOT (t);
3730 else if (TREE_CODE (t) == AGGR_INIT_EXPR)
3731 object = AGGR_INIT_EXPR_SLOT (t);
3733 ctx.object = object;
3735 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3736 (type, TREE_TYPE (object)));
3737 if (object && DECL_P (object))
3738 map.put (object, ctx.ctor);
3739 if (TREE_CODE (r) == TARGET_EXPR)
3740 /* Avoid creating another CONSTRUCTOR when we expand the
3742 r = TARGET_EXPR_INITIAL (r);
3745 r = cxx_eval_constant_expression (&ctx, r,
3746 false, &non_constant_p, &overflow_p);
3748 verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p);
3750 /* Mutable logic is a bit tricky: we want to allow initialization of
3751 constexpr variables with mutable members, but we can't copy those
3752 members to another constexpr variable. */
3753 if (TREE_CODE (r) == CONSTRUCTOR
3754 && CONSTRUCTOR_MUTABLE_POISON (r))
3756 if (!allow_non_constant)
3757 error ("%qE is not a constant expression because it refers to "
3758 "mutable subobjects of %qT", t, type);
3759 non_constant_p = true;
3762 /* Technically we should check this for all subexpressions, but that
3763 runs into problems with our internal representation of pointer
3764 subtraction and the 5.19 rules are still in flux. */
3765 if (CONVERT_EXPR_CODE_P (TREE_CODE (r))
3766 && ARITHMETIC_TYPE_P (TREE_TYPE (r))
3767 && TREE_CODE (TREE_OPERAND (r, 0)) == ADDR_EXPR)
3769 if (!allow_non_constant)
3770 error ("conversion from pointer type %qT "
3771 "to arithmetic type %qT in a constant-expression",
3772 TREE_TYPE (TREE_OPERAND (r, 0)), TREE_TYPE (r));
3773 non_constant_p = true;
3776 if (!non_constant_p && overflow_p)
3777 non_constant_p = true;
3779 if (non_constant_p && !allow_non_constant)
3780 return error_mark_node;
3781 else if (non_constant_p && TREE_CONSTANT (r))
3783 /* This isn't actually constant, so unset TREE_CONSTANT. */
3786 else if (TREE_CODE (r) == CONSTRUCTOR)
3787 r = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (r), r);
3789 r = build_nop (TREE_TYPE (r), r);
3790 TREE_CONSTANT (r) = false;
3792 else if (non_constant_p || r == t)
3795 if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
3797 if (TREE_CODE (t) == TARGET_EXPR
3798 && TARGET_EXPR_INITIAL (t) == r)
3802 r = get_target_expr (r);
3803 TREE_CONSTANT (r) = true;
3811 /* Returns true if T is a valid subexpression of a constant expression,
3812 even if it isn't itself a constant expression. */
3815 is_sub_constant_expr (tree t)
3817 bool non_constant_p = false;
3818 bool overflow_p = false;
3819 hash_map <tree, tree> map;
3820 constexpr_ctx ctx = { NULL, &map, NULL, NULL, true, true };
3821 cxx_eval_constant_expression (&ctx, t, false, &non_constant_p,
3823 return !non_constant_p && !overflow_p;
3826 /* If T represents a constant expression returns its reduced value.
3827 Otherwise return error_mark_node. If T is dependent, then
3831 cxx_constant_value (tree t, tree decl)
3833 return cxx_eval_outermost_constant_expr (t, false, true, decl);
3836 /* If T is a constant expression, returns its reduced value.
3837 Otherwise, if T does not have TREE_CONSTANT set, returns T.
3838 Otherwise, returns a version of T without TREE_CONSTANT. */
3841 maybe_constant_value (tree t, tree decl)
3845 if (instantiation_dependent_expression_p (t)
3846 || type_unknown_p (t)
3847 || BRACE_ENCLOSED_INITIALIZER_P (t)
3848 || !potential_constant_expression (t))
3850 if (TREE_OVERFLOW_P (t))
3852 t = build_nop (TREE_TYPE (t), t);
3853 TREE_CONSTANT (t) = false;
3858 r = cxx_eval_outermost_constant_expr (t, true, true, decl);
3859 #ifdef ENABLE_CHECKING
3861 || CONVERT_EXPR_P (t)
3862 || TREE_CODE (t) == VIEW_CONVERT_EXPR
3863 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
3864 || !cp_tree_equal (r, t));
3869 /* Like maybe_constant_value but first fully instantiate the argument.
3871 Note: this is equivalent to instantiate_non_dependent_expr_sfinae
3872 (t, tf_none) followed by maybe_constant_value but is more efficient,
3873 because calls instantiation_dependent_expression_p and
3874 potential_constant_expression at most once. */
3877 fold_non_dependent_expr (tree t)
3882 /* If we're in a template, but T isn't value dependent, simplify
3883 it. We're supposed to treat:
3885 template <typename T> void f(T[1 + 1]);
3886 template <typename T> void f(T[2]);
3888 as two declarations of the same function, for example. */
3889 if (processing_template_decl)
3891 if (!instantiation_dependent_expression_p (t)
3892 && potential_constant_expression (t))
3894 processing_template_decl_sentinel s;
3895 t = instantiate_non_dependent_expr_internal (t, tf_none);
3897 if (type_unknown_p (t)
3898 || BRACE_ENCLOSED_INITIALIZER_P (t))
3900 if (TREE_OVERFLOW_P (t))
3902 t = build_nop (TREE_TYPE (t), t);
3903 TREE_CONSTANT (t) = false;
3908 tree r = cxx_eval_outermost_constant_expr (t, true, true, NULL_TREE);
3909 #ifdef ENABLE_CHECKING
3910 /* cp_tree_equal looks through NOPs, so allow them. */
3912 || CONVERT_EXPR_P (t)
3913 || TREE_CODE (t) == VIEW_CONVERT_EXPR
3914 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
3915 || !cp_tree_equal (r, t));
3919 else if (TREE_OVERFLOW_P (t))
3921 t = build_nop (TREE_TYPE (t), t);
3922 TREE_CONSTANT (t) = false;
3927 return maybe_constant_value (t);
3930 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
3931 than wrapped in a TARGET_EXPR. */
3934 maybe_constant_init (tree t, tree decl)
3936 if (TREE_CODE (t) == EXPR_STMT)
3937 t = TREE_OPERAND (t, 0);
3938 if (TREE_CODE (t) == CONVERT_EXPR
3939 && VOID_TYPE_P (TREE_TYPE (t)))
3940 t = TREE_OPERAND (t, 0);
3941 if (TREE_CODE (t) == INIT_EXPR)
3942 t = TREE_OPERAND (t, 1);
3943 if (instantiation_dependent_expression_p (t)
3944 || type_unknown_p (t)
3945 || BRACE_ENCLOSED_INITIALIZER_P (t)
3946 || !potential_static_init_expression (t))
3947 /* Don't try to evaluate it. */;
3949 t = cxx_eval_outermost_constant_expr (t, true, false, decl);
3950 if (TREE_CODE (t) == TARGET_EXPR)
3952 tree init = TARGET_EXPR_INITIAL (t);
3953 if (TREE_CODE (init) == CONSTRUCTOR)
3960 /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
3961 /* Return true if the object referred to by REF has automatic or thread
3964 enum { ck_ok, ck_bad, ck_unknown };
3966 check_automatic_or_tls (tree ref)
3969 HOST_WIDE_INT bitsize, bitpos;
3971 int volatilep = 0, unsignedp = 0;
3972 tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset,
3973 &mode, &unsignedp, &volatilep, false);
3976 /* If there isn't a decl in the middle, we don't know the linkage here,
3977 and this isn't a constant expression anyway. */
3980 dk = decl_storage_duration (decl);
3981 return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok;
3985 /* Return true if T denotes a potentially constant expression. Issue
3986 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
3987 an lvalue-rvalue conversion is implied.
3989 C++0x [expr.const] used to say
3991 6 An expression is a potential constant expression if it is
3992 a constant expression where all occurrences of function
3993 parameters are replaced by arbitrary constant expressions
3994 of the appropriate type.
3996 2 A conditional expression is a constant expression unless it
3997 involves one of the following as a potentially evaluated
3998 subexpression (3.2), but subexpressions of logical AND (5.14),
3999 logical OR (5.15), and conditional (5.16) operations that are
4000 not evaluated are not considered. */
4003 potential_constant_expression_1 (tree t, bool want_rval, bool strict,
4004 tsubst_flags_t flags)
4006 #define RECUR(T,RV) potential_constant_expression_1 ((T), (RV), strict, flags)
4007 enum { any = false, rval = true };
4011 if (t == error_mark_node)
4015 if (TREE_THIS_VOLATILE (t))
4017 if (flags & tf_error)
4018 error ("expression %qE has side-effects", t);
4021 if (CONSTANT_CLASS_P (t))
4024 switch (TREE_CODE (t))
4030 case TEMPLATE_ID_EXPR:
4033 case CASE_LABEL_EXPR:
4039 case TEMPLATE_PARM_INDEX:
4041 case IDENTIFIER_NODE:
4042 case USERDEF_LITERAL:
4043 /* We can see a FIELD_DECL in a pointer-to-member expression. */
4048 case PLACEHOLDER_EXPR:
4054 case AGGR_INIT_EXPR:
4056 /* -- an invocation of a function other than a constexpr function
4057 or a constexpr constructor. */
4059 tree fun = get_function_named_in_call (t);
4060 const int nargs = call_expr_nargs (t);
4063 if (fun == NULL_TREE)
4065 if (TREE_CODE (t) == CALL_EXPR
4066 && CALL_EXPR_FN (t) == NULL_TREE)
4067 switch (CALL_EXPR_IFN (t))
4069 /* These should be ignored, they are optimized away from
4070 constexpr functions. */
4071 case IFN_UBSAN_NULL:
4072 case IFN_UBSAN_BOUNDS:
4073 case IFN_UBSAN_VPTR:
4078 /* fold_call_expr can't do anything with IFN calls. */
4079 if (flags & tf_error)
4080 error_at (EXPR_LOC_OR_LOC (t, input_location),
4081 "call to internal function");
4084 if (is_overloaded_fn (fun))
4086 if (TREE_CODE (fun) == FUNCTION_DECL)
4088 if (builtin_valid_in_constant_expr_p (fun))
4090 if (!DECL_DECLARED_CONSTEXPR_P (fun)
4091 /* Allow any built-in function; if the expansion
4092 isn't constant, we'll deal with that then. */
4093 && !is_builtin_fn (fun))
4095 if (flags & tf_error)
4097 error_at (EXPR_LOC_OR_LOC (t, input_location),
4098 "call to non-constexpr function %qD", fun);
4099 explain_invalid_constexpr_fn (fun);
4103 /* A call to a non-static member function takes the address
4104 of the object as the first argument. But in a constant
4105 expression the address will be folded away, so look
4107 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
4108 && !DECL_CONSTRUCTOR_P (fun))
4110 tree x = get_nth_callarg (t, 0);
4111 if (is_this_parameter (x))
4113 else if (!RECUR (x, rval))
4120 if (!RECUR (fun, true))
4122 fun = get_first_fn (fun);
4124 /* Skip initial arguments to base constructors. */
4125 if (DECL_BASE_CONSTRUCTOR_P (fun))
4126 i = num_artificial_parms_for (fun);
4127 fun = DECL_ORIGIN (fun);
4131 if (RECUR (fun, rval))
4132 /* Might end up being a constant function pointer. */;
4136 for (; i < nargs; ++i)
4138 tree x = get_nth_callarg (t, i);
4139 /* In a template, reference arguments haven't been converted to
4140 REFERENCE_TYPE and we might not even know if the parameter
4141 is a reference, so accept lvalue constants too. */
4142 bool rv = processing_template_decl ? any : rval;
4149 case NON_LVALUE_EXPR:
4150 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
4151 -- an lvalue of integral type that refers to a non-volatile
4152 const variable or static data member initialized with
4153 constant expressions, or
4155 -- an lvalue of literal type that refers to non-volatile
4156 object defined with constexpr, or that refers to a
4157 sub-object of such an object; */
4158 return RECUR (TREE_OPERAND (t, 0), rval);
4162 && !decl_constant_var_p (t)
4164 || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t))
4165 || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t))
4166 && !var_in_constexpr_fn (t)
4167 && !type_dependent_expression_p (t))
4169 if (flags & tf_error)
4170 non_const_var_error (t);
4177 case VIEW_CONVERT_EXPR:
4178 /* -- a reinterpret_cast. FIXME not implemented, and this rule
4179 may change to something more specific to type-punning (DR 1312). */
4181 tree from = TREE_OPERAND (t, 0);
4182 if (POINTER_TYPE_P (TREE_TYPE (t))
4183 && TREE_CODE (from) == INTEGER_CST
4184 && !integer_zerop (from))
4186 if (flags & tf_error)
4187 error_at (EXPR_LOC_OR_LOC (t, input_location),
4188 "reinterpret_cast from integer to pointer");
4191 return (RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR));
4195 /* -- a unary operator & that is applied to an lvalue that
4196 designates an object with thread or automatic storage
4198 t = TREE_OPERAND (t, 0);
4200 if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t))
4201 /* A pointer-to-member constant. */
4205 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
4206 any checking here, as we might dereference the pointer later. If
4207 we remove this code, also remove check_automatic_or_tls. */
4208 i = check_automatic_or_tls (t);
4213 if (flags & tf_error)
4214 error ("address-of an object %qE with thread local or "
4215 "automatic storage is not a constant expression", t);
4219 return RECUR (t, any);
4225 /* -- a class member access unless its postfix-expression is
4226 of literal type or of pointer to literal type. */
4227 /* This test would be redundant, as it follows from the
4228 postfix-expression being a potential constant expression. */
4229 return RECUR (TREE_OPERAND (t, 0), want_rval);
4231 case EXPR_PACK_EXPANSION:
4232 return RECUR (PACK_EXPANSION_PATTERN (t), want_rval);
4236 tree x = TREE_OPERAND (t, 0);
4238 if (is_this_parameter (x))
4240 if (DECL_CONTEXT (x)
4241 && !DECL_DECLARED_CONSTEXPR_P (DECL_CONTEXT (x)))
4243 if (flags & tf_error)
4244 error ("use of %<this%> in a constant expression");
4249 return RECUR (x, rval);
4252 case STATEMENT_LIST:
4254 tree_stmt_iterator i;
4255 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
4257 if (!RECUR (tsi_stmt (i), any))
4265 if (cxx_dialect < cxx14)
4267 if (!RECUR (TREE_OPERAND (t, 0), any))
4269 if (!RECUR (TREE_OPERAND (t, 1), rval))
4274 if (cxx_dialect < cxx14)
4276 if (!RECUR (TREE_OPERAND (t, 0), rval))
4278 if (!RECUR (TREE_OPERAND (t, 2), rval))
4283 if (!RECUR (DO_COND (t), rval))
4285 if (!RECUR (DO_BODY (t), any))
4290 if (!RECUR (FOR_INIT_STMT (t), any))
4292 if (!RECUR (FOR_COND (t), rval))
4294 if (!RECUR (FOR_EXPR (t), any))
4296 if (!RECUR (FOR_BODY (t), any))
4301 if (!RECUR (WHILE_COND (t), rval))
4303 if (!RECUR (WHILE_BODY (t), any))
4308 if (!RECUR (SWITCH_STMT_COND (t), rval))
4310 /* FIXME we don't check SWITCH_STMT_BODY currently, because even
4311 unreachable labels would be checked. */
4315 return RECUR (STMT_EXPR_STMT (t), rval);
4318 case DYNAMIC_CAST_EXPR:
4319 case PSEUDO_DTOR_EXPR:
4323 case VEC_DELETE_EXPR:
4326 case OMP_ATOMIC_READ:
4327 case OMP_ATOMIC_CAPTURE_OLD:
4328 case OMP_ATOMIC_CAPTURE_NEW:
4329 /* GCC internal stuff. */
4332 case TRANSACTION_EXPR:
4334 case AT_ENCODE_EXPR:
4336 if (flags & tf_error)
4337 error ("expression %qE is not a constant-expression", t);
4341 /* -- a typeid expression whose operand is of polymorphic
4344 tree e = TREE_OPERAND (t, 0);
4345 if (!TYPE_P (e) && !type_dependent_expression_p (e)
4346 && TYPE_POLYMORPHIC_P (TREE_TYPE (e)))
4348 if (flags & tf_error)
4349 error ("typeid-expression is not a constant expression "
4350 "because %qE is of polymorphic type", e);
4369 case PREINCREMENT_EXPR:
4370 case POSTINCREMENT_EXPR:
4371 case PREDECREMENT_EXPR:
4372 case POSTDECREMENT_EXPR:
4373 if (cxx_dialect < cxx14)
4379 if (TYPE_P (TREE_OPERAND (t, 0)))
4381 /* else fall through. */
4387 case FIX_TRUNC_EXPR:
4391 case TRUTH_NOT_EXPR:
4392 case FIXED_CONVERT_EXPR:
4393 case UNARY_PLUS_EXPR:
4394 case UNARY_LEFT_FOLD_EXPR:
4395 case UNARY_RIGHT_FOLD_EXPR:
4397 return RECUR (TREE_OPERAND (t, 0), rval);
4400 case CONST_CAST_EXPR:
4401 case STATIC_CAST_EXPR:
4402 case REINTERPRET_CAST_EXPR:
4403 case IMPLICIT_CONV_EXPR:
4404 if (cxx_dialect < cxx11
4405 && !dependent_type_p (TREE_TYPE (t))
4406 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t)))
4407 /* In C++98, a conversion to non-integral type can't be part of a
4408 constant expression. */
4410 if (flags & tf_error)
4411 error ("cast to non-integral type %qT in a constant expression",
4416 return (RECUR (TREE_OPERAND (t, 0),
4417 TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE));
4420 return RECUR (BIND_EXPR_BODY (t), want_rval);
4422 case WITH_CLEANUP_EXPR:
4423 case CLEANUP_POINT_EXPR:
4424 case MUST_NOT_THROW_EXPR:
4425 case TRY_CATCH_EXPR:
4431 case NON_DEPENDENT_EXPR:
4432 /* For convenience. */
4434 return RECUR (TREE_OPERAND (t, 0), want_rval);
4436 case TRY_FINALLY_EXPR:
4437 return (RECUR (TREE_OPERAND (t, 0), want_rval)
4438 && RECUR (TREE_OPERAND (t, 1), any));
4441 return RECUR (TREE_OPERAND (t, 1), want_rval);
4444 if (!literal_type_p (TREE_TYPE (t)))
4446 if (flags & tf_error)
4448 error ("temporary of non-literal type %qT in a "
4449 "constant expression", TREE_TYPE (t));
4450 explain_non_literal_class (TREE_TYPE (t));
4455 return RECUR (TREE_OPERAND (t, 1), rval);
4459 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
4460 constructor_elt *ce;
4461 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
4462 if (!RECUR (ce->value, want_rval))
4469 gcc_assert (TREE_PURPOSE (t) == NULL_TREE
4470 || DECL_P (TREE_PURPOSE (t)));
4471 if (!RECUR (TREE_VALUE (t), want_rval))
4473 if (TREE_CHAIN (t) == NULL_TREE)
4475 return RECUR (TREE_CHAIN (t), want_rval);
4478 case TRUNC_DIV_EXPR:
4480 case FLOOR_DIV_EXPR:
4481 case ROUND_DIV_EXPR:
4482 case TRUNC_MOD_EXPR:
4484 case ROUND_MOD_EXPR:
4486 tree denom = TREE_OPERAND (t, 1);
4487 if (!RECUR (denom, rval))
4489 /* We can't call cxx_eval_outermost_constant_expr on an expression
4490 that hasn't been through instantiate_non_dependent_expr yet. */
4491 if (!processing_template_decl)
4492 denom = cxx_eval_outermost_constant_expr (denom, true);
4493 if (integer_zerop (denom))
4495 if (flags & tf_error)
4496 error ("division by zero is not a constant-expression");
4502 return RECUR (TREE_OPERAND (t, 0), want_rval);
4508 /* check_return_expr sometimes wraps a TARGET_EXPR in a
4509 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
4510 introduced by build_call_a. */
4511 tree op0 = TREE_OPERAND (t, 0);
4512 tree op1 = TREE_OPERAND (t, 1);
4514 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
4515 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
4516 return RECUR (op0, want_rval);
4521 /* If the first operand is the non-short-circuit constant, look at
4522 the second operand; otherwise we only care about the first one for
4524 case TRUTH_AND_EXPR:
4525 case TRUTH_ANDIF_EXPR:
4526 tmp = boolean_true_node;
4529 case TRUTH_ORIF_EXPR:
4530 tmp = boolean_false_node;
4533 tree op = TREE_OPERAND (t, 0);
4534 if (!RECUR (op, rval))
4536 if (!processing_template_decl)
4537 op = cxx_eval_outermost_constant_expr (op, true);
4538 if (tree_int_cst_equal (op, tmp))
4539 return RECUR (TREE_OPERAND (t, 1), rval);
4546 case POINTER_PLUS_EXPR:
4548 case EXACT_DIV_EXPR:
4558 case TRUTH_XOR_EXPR:
4559 case UNORDERED_EXPR:
4572 case ARRAY_RANGE_REF:
4576 case BINARY_LEFT_FOLD_EXPR:
4577 case BINARY_RIGHT_FOLD_EXPR:
4579 for (i = 0; i < 2; ++i)
4580 if (!RECUR (TREE_OPERAND (t, i), want_rval))
4584 case CILK_SYNC_STMT:
4585 case CILK_SPAWN_STMT:
4586 case ARRAY_NOTATION_REF:
4591 for (i = 0; i < 3; ++i)
4592 if (!RECUR (TREE_OPERAND (t, i), true))
4599 /* If the condition is a known constant, we know which of the legs we
4600 care about; otherwise we only require that the condition and
4601 either of the legs be potentially constant. */
4602 tmp = TREE_OPERAND (t, 0);
4603 if (!RECUR (tmp, rval))
4605 if (!processing_template_decl)
4606 tmp = cxx_eval_outermost_constant_expr (tmp, true);
4607 if (integer_zerop (tmp))
4608 return RECUR (TREE_OPERAND (t, 2), want_rval);
4609 else if (TREE_CODE (tmp) == INTEGER_CST)
4610 return RECUR (TREE_OPERAND (t, 1), want_rval);
4611 for (i = 1; i < 3; ++i)
4612 if (potential_constant_expression_1 (TREE_OPERAND (t, i),
4613 want_rval, strict, tf_none))
4615 if (flags & tf_error)
4616 error ("expression %qE is not a constant-expression", t);
4620 if (VEC_INIT_EXPR_IS_CONSTEXPR (t))
4622 if (flags & tf_error)
4624 error ("non-constant array initialization");
4625 diagnose_non_constexpr_vec_init (t);
4631 /* We can see these in statement-expressions. */
4635 if (objc_is_property_ref (t))
4638 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t)));
4645 /* The main entry point to the above. */
4648 potential_constant_expression (tree t)
4650 return potential_constant_expression_1 (t, false, true, tf_none);
4654 potential_static_init_expression (tree t)
4656 return potential_constant_expression_1 (t, false, false, tf_none);
4659 /* As above, but require a constant rvalue. */
4662 potential_rvalue_constant_expression (tree t)
4664 return potential_constant_expression_1 (t, true, true, tf_none);
4667 /* Like above, but complain about non-constant expressions. */
4670 require_potential_constant_expression (tree t)
4672 return potential_constant_expression_1 (t, false, true, tf_warning_or_error);
4675 /* Cross product of the above. */
4678 require_potential_rvalue_constant_expression (tree t)
4680 return potential_constant_expression_1 (t, true, true, tf_warning_or_error);
4683 #include "gt-cp-constexpr.h"