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"
29 #include "double-int.h"
39 #include "c-family/c-objc.h"
40 #include "tree-iterator.h"
43 #include "tree-inline.h"
46 static bool verify_constant (tree, bool, bool *, bool *);
47 #define VERIFY_CONSTANT(X) \
49 if (verify_constant ((X), ctx->quiet, non_constant_p, overflow_p)) \
53 /* Returns true iff FUN is an instantiation of a constexpr function
54 template or a defaulted constexpr function. */
57 is_instantiation_of_constexpr (tree fun)
59 return ((DECL_TEMPLOID_INSTANTIATION (fun)
60 && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun)))
61 || (DECL_DEFAULTED_FN (fun)
62 && DECL_DECLARED_CONSTEXPR_P (fun)));
65 /* Return true if T is a literal type. */
68 literal_type_p (tree t)
71 || TREE_CODE (t) == VECTOR_TYPE
72 || TREE_CODE (t) == REFERENCE_TYPE
73 || (VOID_TYPE_P (t) && cxx_dialect >= cxx14))
77 t = complete_type (t);
78 gcc_assert (COMPLETE_TYPE_P (t) || errorcount);
79 return CLASSTYPE_LITERAL_P (t);
81 if (TREE_CODE (t) == ARRAY_TYPE)
82 return literal_type_p (strip_array_types (t));
86 /* If DECL is a variable declared `constexpr', require its type
87 be literal. Return the DECL if OK, otherwise NULL. */
90 ensure_literal_type_for_constexpr_object (tree decl)
92 tree type = TREE_TYPE (decl);
94 && (DECL_DECLARED_CONSTEXPR_P (decl)
95 || var_in_constexpr_fn (decl))
96 && !processing_template_decl)
98 tree stype = strip_array_types (type);
99 if (CLASS_TYPE_P (stype) && !COMPLETE_TYPE_P (complete_type (stype)))
100 /* Don't complain here, we'll complain about incompleteness
101 when we try to initialize the variable. */;
102 else if (!literal_type_p (type))
104 if (DECL_DECLARED_CONSTEXPR_P (decl))
106 error ("the type %qT of constexpr variable %qD is not literal",
108 explain_non_literal_class (type);
112 if (!DECL_TEMPLATE_INSTANTIATION (current_function_decl))
114 error ("variable %qD of non-literal type %qT in %<constexpr%> "
115 "function", decl, type);
116 explain_non_literal_class (type);
118 cp_function_chain->invalid_constexpr = true;
126 /* Representation of entries in the constexpr function definition table. */
128 struct GTY((for_user)) constexpr_fundef {
133 struct constexpr_fundef_hasher : ggc_hasher<constexpr_fundef *>
135 static hashval_t hash (constexpr_fundef *);
136 static bool equal (constexpr_fundef *, constexpr_fundef *);
139 /* This table holds all constexpr function definitions seen in
140 the current translation unit. */
142 static GTY (()) hash_table<constexpr_fundef_hasher> *constexpr_fundef_table;
144 /* Utility function used for managing the constexpr function table.
145 Return true if the entries pointed to by P and Q are for the
146 same constexpr function. */
149 constexpr_fundef_hasher::equal (constexpr_fundef *lhs, constexpr_fundef *rhs)
151 return lhs->decl == rhs->decl;
154 /* Utility function used for managing the constexpr function table.
155 Return a hash value for the entry pointed to by Q. */
158 constexpr_fundef_hasher::hash (constexpr_fundef *fundef)
160 return DECL_UID (fundef->decl);
163 /* Return a previously saved definition of function FUN. */
165 static constexpr_fundef *
166 retrieve_constexpr_fundef (tree fun)
168 constexpr_fundef fundef = { NULL, NULL };
169 if (constexpr_fundef_table == NULL)
173 return constexpr_fundef_table->find (&fundef);
176 /* Check whether the parameter and return types of FUN are valid for a
177 constexpr function, and complain if COMPLAIN. */
180 is_valid_constexpr_fn (tree fun, bool complain)
184 if (DECL_INHERITED_CTOR_BASE (fun)
185 && TREE_CODE (fun) == TEMPLATE_DECL)
189 error ("inherited constructor %qD is not constexpr",
190 get_inherited_ctor (fun));
194 for (tree parm = FUNCTION_FIRST_USER_PARM (fun);
195 parm != NULL_TREE; parm = TREE_CHAIN (parm))
196 if (!literal_type_p (TREE_TYPE (parm)))
201 error ("invalid type for parameter %d of constexpr "
202 "function %q+#D", DECL_PARM_INDEX (parm), fun);
203 explain_non_literal_class (TREE_TYPE (parm));
208 if (!DECL_CONSTRUCTOR_P (fun))
210 tree rettype = TREE_TYPE (TREE_TYPE (fun));
211 if (!literal_type_p (rettype))
216 error ("invalid return type %qT of constexpr function %q+D",
218 explain_non_literal_class (rettype);
222 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
223 && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun)))
228 error ("enclosing class of constexpr non-static member "
229 "function %q+#D is not a literal type", fun);
230 explain_non_literal_class (DECL_CONTEXT (fun));
234 else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun)))
238 error ("%q#T has virtual base classes", DECL_CONTEXT (fun));
244 /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF
245 for a member of an anonymous aggregate, INIT is the initializer for that
246 member, and VEC_OUTER is the vector of constructor elements for the class
247 whose constructor we are processing. Add the initializer to the vector
248 and return true to indicate success. */
251 build_anon_member_initialization (tree member, tree init,
252 vec<constructor_elt, va_gc> **vec_outer)
254 /* MEMBER presents the relevant fields from the inside out, but we need
255 to build up the initializer from the outside in so that we can reuse
256 previously built CONSTRUCTORs if this is, say, the second field in an
257 anonymous struct. So we use a vec as a stack. */
258 auto_vec<tree, 2> fields;
261 fields.safe_push (TREE_OPERAND (member, 1));
262 member = TREE_OPERAND (member, 0);
264 while (ANON_AGGR_TYPE_P (TREE_TYPE (member))
265 && TREE_CODE (member) == COMPONENT_REF);
267 /* VEC has the constructor elements vector for the context of FIELD.
268 If FIELD is an anonymous aggregate, we will push inside it. */
269 vec<constructor_elt, va_gc> **vec = vec_outer;
271 while (field = fields.pop(),
272 ANON_AGGR_TYPE_P (TREE_TYPE (field)))
275 /* If there is already an outer constructor entry for the anonymous
276 aggregate FIELD, use it; otherwise, insert one. */
277 if (vec_safe_is_empty (*vec)
278 || (*vec)->last().index != field)
280 ctor = build_constructor (TREE_TYPE (field), NULL);
281 CONSTRUCTOR_APPEND_ELT (*vec, field, ctor);
284 ctor = (*vec)->last().value;
285 vec = &CONSTRUCTOR_ELTS (ctor);
288 /* Now we're at the innermost field, the one that isn't an anonymous
289 aggregate. Add its initializer to the CONSTRUCTOR and we're done. */
290 gcc_assert (fields.is_empty());
291 CONSTRUCTOR_APPEND_ELT (*vec, field, init);
296 /* Subroutine of build_constexpr_constructor_member_initializers.
297 The expression tree T represents a data member initialization
298 in a (constexpr) constructor definition. Build a pairing of
299 the data member with its initializer, and prepend that pair
300 to the existing initialization pair INITS. */
303 build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec)
306 if (TREE_CODE (t) == CLEANUP_POINT_EXPR)
307 t = TREE_OPERAND (t, 0);
308 if (TREE_CODE (t) == EXPR_STMT)
309 t = TREE_OPERAND (t, 0);
310 if (t == error_mark_node)
312 if (TREE_CODE (t) == STATEMENT_LIST)
314 tree_stmt_iterator i;
315 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
317 if (! build_data_member_initialization (tsi_stmt (i), vec))
322 if (TREE_CODE (t) == CLEANUP_STMT)
324 /* We can't see a CLEANUP_STMT in a constructor for a literal class,
325 but we can in a constexpr constructor for a non-literal class. Just
326 ignore it; either all the initialization will be constant, in which
327 case the cleanup can't run, or it can't be constexpr.
328 Still recurse into CLEANUP_BODY. */
329 return build_data_member_initialization (CLEANUP_BODY (t), vec);
331 if (TREE_CODE (t) == CONVERT_EXPR)
332 t = TREE_OPERAND (t, 0);
333 if (TREE_CODE (t) == INIT_EXPR
334 /* vptr initialization shows up as a MODIFY_EXPR. In C++14 we only
335 use what this function builds for cx_check_missing_mem_inits, and
336 assignment in the ctor body doesn't count. */
337 || (cxx_dialect < cxx14 && TREE_CODE (t) == MODIFY_EXPR))
339 member = TREE_OPERAND (t, 0);
340 init = break_out_target_exprs (TREE_OPERAND (t, 1));
342 else if (TREE_CODE (t) == CALL_EXPR)
344 tree fn = get_callee_fndecl (t);
345 if (!fn || !DECL_CONSTRUCTOR_P (fn))
346 /* We're only interested in calls to subobject constructors. */
348 member = CALL_EXPR_ARG (t, 0);
349 /* We don't use build_cplus_new here because it complains about
350 abstract bases. Leaving the call unwrapped means that it has the
351 wrong type, but cxx_eval_constant_expression doesn't care. */
352 init = break_out_target_exprs (t);
354 else if (TREE_CODE (t) == BIND_EXPR)
355 return build_data_member_initialization (BIND_EXPR_BODY (t), vec);
357 /* Don't add anything else to the CONSTRUCTOR. */
359 if (INDIRECT_REF_P (member))
360 member = TREE_OPERAND (member, 0);
361 if (TREE_CODE (member) == NOP_EXPR)
365 if (TREE_CODE (op) == ADDR_EXPR)
367 gcc_assert (same_type_ignoring_top_level_qualifiers_p
368 (TREE_TYPE (TREE_TYPE (op)),
369 TREE_TYPE (TREE_TYPE (member))));
370 /* Initializing a cv-qualified member; we need to look through
374 else if (op == current_class_ptr
375 && (same_type_ignoring_top_level_qualifiers_p
376 (TREE_TYPE (TREE_TYPE (member)),
377 current_class_type)))
378 /* Delegating constructor. */
382 /* This is an initializer for an empty base; keep it for now so
383 we can check it in cxx_eval_bare_aggregate. */
384 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member))));
387 if (TREE_CODE (member) == ADDR_EXPR)
388 member = TREE_OPERAND (member, 0);
389 if (TREE_CODE (member) == COMPONENT_REF)
391 tree aggr = TREE_OPERAND (member, 0);
392 if (TREE_CODE (aggr) != COMPONENT_REF)
393 /* Normal member initialization. */
394 member = TREE_OPERAND (member, 1);
395 else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr)))
396 /* Initializing a member of an anonymous union. */
397 return build_anon_member_initialization (member, init, vec);
399 /* We're initializing a vtable pointer in a base. Leave it as
400 COMPONENT_REF so we remember the path to get to the vfield. */
401 gcc_assert (TREE_TYPE (member) == vtbl_ptr_type_node);
404 CONSTRUCTOR_APPEND_ELT (*vec, member, init);
408 /* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval.
409 In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a
410 BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */
413 check_constexpr_bind_expr_vars (tree t)
415 gcc_assert (TREE_CODE (t) == BIND_EXPR);
417 for (tree var = BIND_EXPR_VARS (t); var; var = DECL_CHAIN (var))
418 if (TREE_CODE (var) == TYPE_DECL
419 && DECL_IMPLICIT_TYPEDEF_P (var)
420 && !LAMBDA_TYPE_P (TREE_TYPE (var)))
425 /* Subroutine of check_constexpr_ctor_body. */
428 check_constexpr_ctor_body_1 (tree last, tree list)
430 switch (TREE_CODE (list))
433 if (TREE_CODE (DECL_EXPR_DECL (list)) == USING_DECL)
437 case CLEANUP_POINT_EXPR:
438 return check_constexpr_ctor_body (last, TREE_OPERAND (list, 0),
442 if (!check_constexpr_bind_expr_vars (list)
443 || !check_constexpr_ctor_body (last, BIND_EXPR_BODY (list),
457 /* Make sure that there are no statements after LAST in the constructor
458 body represented by LIST. */
461 check_constexpr_ctor_body (tree last, tree list, bool complain)
463 /* C++14 doesn't require a constexpr ctor to have an empty body. */
464 if (cxx_dialect >= cxx14)
468 if (TREE_CODE (list) == STATEMENT_LIST)
470 tree_stmt_iterator i = tsi_last (list);
471 for (; !tsi_end_p (i); tsi_prev (&i))
473 tree t = tsi_stmt (i);
476 if (!check_constexpr_ctor_body_1 (last, t))
483 else if (list != last
484 && !check_constexpr_ctor_body_1 (last, list))
489 error ("constexpr constructor does not have empty body");
490 DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false;
495 /* V is a vector of constructor elements built up for the base and member
496 initializers of a constructor for TYPE. They need to be in increasing
497 offset order, which they might not be yet if TYPE has a primary base
498 which is not first in the base-clause or a vptr and at least one base
499 all of which are non-primary. */
501 static vec<constructor_elt, va_gc> *
502 sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v)
504 tree pri = CLASSTYPE_PRIMARY_BINFO (type);
510 field_type = BINFO_TYPE (pri);
511 else if (TYPE_CONTAINS_VPTR_P (type))
512 field_type = vtbl_ptr_type_node;
516 /* Find the element for the primary base or vptr and move it to the
517 beginning of the vec. */
518 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
519 if (TREE_TYPE (ce->index) == field_type)
522 if (i > 0 && i < vec_safe_length (v))
524 vec<constructor_elt, va_gc> &vref = *v;
525 constructor_elt elt = vref[i];
534 /* Build compile-time evalable representations of member-initializer list
535 for a constexpr constructor. */
538 build_constexpr_constructor_member_initializers (tree type, tree body)
540 vec<constructor_elt, va_gc> *vec = NULL;
543 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR
544 || TREE_CODE (body) == EH_SPEC_BLOCK)
545 body = TREE_OPERAND (body, 0);
546 if (TREE_CODE (body) == STATEMENT_LIST)
548 for (tree_stmt_iterator i = tsi_start (body);
549 !tsi_end_p (i); tsi_next (&i))
552 if (TREE_CODE (body) == BIND_EXPR)
554 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
558 if (TREE_CODE (body) == BIND_EXPR)
559 body = BIND_EXPR_BODY (body);
560 if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
562 body = TREE_OPERAND (body, 0);
563 if (TREE_CODE (body) == EXPR_STMT)
564 body = TREE_OPERAND (body, 0);
565 if (TREE_CODE (body) == INIT_EXPR
566 && (same_type_ignoring_top_level_qualifiers_p
567 (TREE_TYPE (TREE_OPERAND (body, 0)),
568 current_class_type)))
571 return TREE_OPERAND (body, 1);
573 ok = build_data_member_initialization (body, &vec);
575 else if (TREE_CODE (body) == STATEMENT_LIST)
577 tree_stmt_iterator i;
578 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
580 ok = build_data_member_initialization (tsi_stmt (i), &vec);
585 else if (TREE_CODE (body) == TRY_BLOCK)
587 error ("body of %<constexpr%> constructor cannot be "
588 "a function-try-block");
589 return error_mark_node;
591 else if (EXPR_P (body))
592 ok = build_data_member_initialization (body, &vec);
594 gcc_assert (errorcount > 0);
597 if (vec_safe_length (vec) > 0)
599 /* In a delegating constructor, return the target. */
600 constructor_elt *ce = &(*vec)[0];
601 if (ce->index == current_class_ptr)
608 vec = sort_constexpr_mem_initializers (type, vec);
609 return build_constructor (type, vec);
612 return error_mark_node;
615 /* Subroutine of register_constexpr_fundef. BODY is the body of a function
616 declared to be constexpr, or a sub-statement thereof. Returns the
617 return value if suitable, error_mark_node for a statement not allowed in
618 a constexpr function, or NULL_TREE if no return value was found. */
621 constexpr_fn_retval (tree body)
623 switch (TREE_CODE (body))
627 tree_stmt_iterator i;
628 tree expr = NULL_TREE;
629 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
631 tree s = constexpr_fn_retval (tsi_stmt (i));
632 if (s == error_mark_node)
633 return error_mark_node;
634 else if (s == NULL_TREE)
635 /* Keep iterating. */;
637 /* Multiple return statements. */
638 return error_mark_node;
646 return break_out_target_exprs (TREE_OPERAND (body, 0));
650 tree decl = DECL_EXPR_DECL (body);
651 if (TREE_CODE (decl) == USING_DECL
652 /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */
653 || DECL_ARTIFICIAL (decl))
655 return error_mark_node;
658 case CLEANUP_POINT_EXPR:
659 return constexpr_fn_retval (TREE_OPERAND (body, 0));
662 if (!check_constexpr_bind_expr_vars (body))
663 return error_mark_node;
664 return constexpr_fn_retval (BIND_EXPR_BODY (body));
670 return error_mark_node;
674 /* Subroutine of register_constexpr_fundef. BODY is the DECL_SAVED_TREE of
675 FUN; do the necessary transformations to turn it into a single expression
676 that we can store in the hash table. */
679 massage_constexpr_body (tree fun, tree body)
681 if (DECL_CONSTRUCTOR_P (fun))
682 body = build_constexpr_constructor_member_initializers
683 (DECL_CONTEXT (fun), body);
684 else if (cxx_dialect < cxx14)
686 if (TREE_CODE (body) == EH_SPEC_BLOCK)
687 body = EH_SPEC_STMTS (body);
688 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
689 body = TREE_OPERAND (body, 0);
690 body = constexpr_fn_retval (body);
695 /* FUN is a constexpr constructor with massaged body BODY. Return true
696 if some bases/fields are uninitialized, and complain if COMPLAIN. */
699 cx_check_missing_mem_inits (tree fun, tree body, bool complain)
706 if (TREE_CODE (body) != CONSTRUCTOR)
709 nelts = CONSTRUCTOR_NELTS (body);
710 ctype = DECL_CONTEXT (fun);
711 field = TYPE_FIELDS (ctype);
713 if (TREE_CODE (ctype) == UNION_TYPE)
715 if (nelts == 0 && next_initializable_field (field))
718 error ("%<constexpr%> constructor for union %qT must "
719 "initialize exactly one non-static data member", ctype);
726 for (i = 0; i <= nelts; ++i)
733 index = CONSTRUCTOR_ELT (body, i)->index;
734 /* Skip base and vtable inits. */
735 if (TREE_CODE (index) != FIELD_DECL
736 || DECL_ARTIFICIAL (index))
739 for (; field != index; field = DECL_CHAIN (field))
742 if (TREE_CODE (field) != FIELD_DECL
743 || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
744 || DECL_ARTIFICIAL (field))
746 ftype = strip_array_types (TREE_TYPE (field));
747 if (type_has_constexpr_default_constructor (ftype))
749 /* It's OK to skip a member with a trivial constexpr ctor.
750 A constexpr ctor that isn't trivial should have been
752 gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype)
758 error ("member %qD must be initialized by mem-initializer "
759 "in %<constexpr%> constructor", field);
760 inform (DECL_SOURCE_LOCATION (field), "declared here");
763 if (field == NULL_TREE)
765 field = DECL_CHAIN (field);
771 /* We are processing the definition of the constexpr function FUN.
772 Check that its BODY fulfills the propriate requirements and
773 enter it in the constexpr function definition table.
774 For constructor BODY is actually the TREE_LIST of the
775 member-initializer list. */
778 register_constexpr_fundef (tree fun, tree body)
780 constexpr_fundef entry;
781 constexpr_fundef **slot;
783 if (!is_valid_constexpr_fn (fun, !DECL_GENERATED_P (fun)))
786 body = massage_constexpr_body (fun, body);
787 if (body == NULL_TREE || body == error_mark_node)
789 if (!DECL_CONSTRUCTOR_P (fun))
790 error ("body of constexpr function %qD not a return-statement", fun);
794 if (!potential_rvalue_constant_expression (body))
796 if (!DECL_GENERATED_P (fun))
797 require_potential_rvalue_constant_expression (body);
801 if (DECL_CONSTRUCTOR_P (fun)
802 && cx_check_missing_mem_inits (fun, body, !DECL_GENERATED_P (fun)))
805 /* Create the constexpr function table if necessary. */
806 if (constexpr_fundef_table == NULL)
807 constexpr_fundef_table
808 = hash_table<constexpr_fundef_hasher>::create_ggc (101);
812 slot = constexpr_fundef_table->find_slot (&entry, INSERT);
814 gcc_assert (*slot == NULL);
815 *slot = ggc_alloc<constexpr_fundef> ();
821 /* FUN is a non-constexpr function called in a context that requires a
822 constant expression. If it comes from a constexpr template, explain why
823 the instantiation isn't constexpr. */
826 explain_invalid_constexpr_fn (tree fun)
828 static hash_set<tree> *diagnosed;
831 /* Only diagnose defaulted functions or instantiations. */
832 if (!DECL_DEFAULTED_FN (fun)
833 && !is_instantiation_of_constexpr (fun))
835 if (diagnosed == NULL)
836 diagnosed = new hash_set<tree>;
837 if (diagnosed->add (fun))
838 /* Already explained. */
841 save_loc = input_location;
842 input_location = DECL_SOURCE_LOCATION (fun);
843 inform (0, "%q+D is not usable as a constexpr function because:", fun);
844 /* First check the declaration. */
845 if (is_valid_constexpr_fn (fun, true))
847 /* Then if it's OK, the body. */
848 if (!DECL_DECLARED_CONSTEXPR_P (fun))
849 explain_implicit_non_constexpr (fun);
852 body = massage_constexpr_body (fun, DECL_SAVED_TREE (fun));
853 require_potential_rvalue_constant_expression (body);
854 if (DECL_CONSTRUCTOR_P (fun))
855 cx_check_missing_mem_inits (fun, body, true);
858 input_location = save_loc;
861 /* Objects of this type represent calls to constexpr functions
862 along with the bindings of parameters to their arguments, for
863 the purpose of compile time evaluation. */
865 struct GTY((for_user)) constexpr_call {
866 /* Description of the constexpr function definition. */
867 constexpr_fundef *fundef;
868 /* Parameter bindings environment. A TREE_LIST where each TREE_PURPOSE
869 is a parameter _DECL and the TREE_VALUE is the value of the parameter.
870 Note: This arrangement is made to accommodate the use of
871 iterative_hash_template_arg (see pt.c). If you change this
872 representation, also change the hash calculation in
873 cxx_eval_call_expression. */
875 /* Result of the call.
876 NULL means the call is being evaluated.
877 error_mark_node means that the evaluation was erroneous;
878 otherwise, the actuall value of the call. */
880 /* The hash of this call; we remember it here to avoid having to
881 recalculate it when expanding the hash table. */
885 struct constexpr_call_hasher : ggc_hasher<constexpr_call *>
887 static hashval_t hash (constexpr_call *);
888 static bool equal (constexpr_call *, constexpr_call *);
891 /* The constexpr expansion context. CALL is the current function
892 expansion, CTOR is the current aggregate initializer, OBJECT is the
893 object being initialized by CTOR, either a VAR_DECL or a _REF. VALUES
894 is a map of values of variables initialized within the expression. */
896 struct constexpr_ctx {
897 /* The innermost call we're evaluating. */
898 constexpr_call *call;
899 /* Values for any temporaries or local variables within the
900 constant-expression. */
901 hash_map<tree,tree> *values;
902 /* SAVE_EXPRs that we've seen within the current LOOP_EXPR. NULL if we
903 aren't inside a loop. */
904 hash_set<tree> *save_exprs;
905 /* The CONSTRUCTOR we're currently building up for an aggregate
908 /* The object we're building the CONSTRUCTOR for. */
910 /* Whether we should error on a non-constant expression or fail quietly. */
912 /* Whether we are strictly conforming to constant expression rules or
913 trying harder to get a constant value. */
917 /* A table of all constexpr calls that have been evaluated by the
918 compiler in this translation unit. */
920 static GTY (()) hash_table<constexpr_call_hasher> *constexpr_call_table;
922 static tree cxx_eval_constant_expression (const constexpr_ctx *, tree,
923 bool, bool *, bool *, tree * = NULL);
925 /* Compute a hash value for a constexpr call representation. */
928 constexpr_call_hasher::hash (constexpr_call *info)
933 /* Return true if the objects pointed to by P and Q represent calls
934 to the same constexpr function with the same arguments.
935 Otherwise, return false. */
938 constexpr_call_hasher::equal (constexpr_call *lhs, constexpr_call *rhs)
944 if (!constexpr_fundef_hasher::equal (lhs->fundef, rhs->fundef))
946 lhs_bindings = lhs->bindings;
947 rhs_bindings = rhs->bindings;
948 while (lhs_bindings != NULL && rhs_bindings != NULL)
950 tree lhs_arg = TREE_VALUE (lhs_bindings);
951 tree rhs_arg = TREE_VALUE (rhs_bindings);
952 gcc_assert (TREE_TYPE (lhs_arg) == TREE_TYPE (rhs_arg));
953 if (!cp_tree_equal (lhs_arg, rhs_arg))
955 lhs_bindings = TREE_CHAIN (lhs_bindings);
956 rhs_bindings = TREE_CHAIN (rhs_bindings);
958 return lhs_bindings == rhs_bindings;
961 /* Initialize the constexpr call table, if needed. */
964 maybe_initialize_constexpr_call_table (void)
966 if (constexpr_call_table == NULL)
967 constexpr_call_table = hash_table<constexpr_call_hasher>::create_ggc (101);
970 /* We have an expression tree T that represents a call, either CALL_EXPR
971 or AGGR_INIT_EXPR. If the call is lexically to a named function,
972 retrun the _DECL for that function. */
975 get_function_named_in_call (tree t)
978 switch (TREE_CODE (t))
981 fun = CALL_EXPR_FN (t);
985 fun = AGGR_INIT_EXPR_FN (t);
992 if (fun && TREE_CODE (fun) == ADDR_EXPR
993 && TREE_CODE (TREE_OPERAND (fun, 0)) == FUNCTION_DECL)
994 fun = TREE_OPERAND (fun, 0);
998 /* We have an expression tree T that represents a call, either CALL_EXPR
999 or AGGR_INIT_EXPR. Return the Nth argument. */
1002 get_nth_callarg (tree t, int n)
1004 switch (TREE_CODE (t))
1007 return CALL_EXPR_ARG (t, n);
1009 case AGGR_INIT_EXPR:
1010 return AGGR_INIT_EXPR_ARG (t, n);
1018 /* Look up the binding of the function parameter T in a constexpr
1019 function call context CALL. */
1022 lookup_parameter_binding (const constexpr_call *call, tree t)
1024 tree b = purpose_member (t, call->bindings);
1025 return TREE_VALUE (b);
1028 /* Attempt to evaluate T which represents a call to a builtin function.
1029 We assume here that all builtin functions evaluate to scalar types
1030 represented by _CST nodes. */
1033 cxx_eval_builtin_function_call (const constexpr_ctx *ctx, tree t,
1035 bool *non_constant_p, bool *overflow_p)
1037 const int nargs = call_expr_nargs (t);
1038 tree *args = (tree *) alloca (nargs * sizeof (tree));
1041 for (i = 0; i < nargs; ++i)
1043 args[i] = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, i),
1045 non_constant_p, overflow_p);
1046 if (ctx->quiet && *non_constant_p)
1049 if (*non_constant_p)
1051 new_call = fold_build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t),
1052 CALL_EXPR_FN (t), nargs, args);
1053 VERIFY_CONSTANT (new_call);
1057 /* TEMP is the constant value of a temporary object of type TYPE. Adjust
1058 the type of the value to match. */
1061 adjust_temp_type (tree type, tree temp)
1063 if (TREE_TYPE (temp) == type)
1065 /* Avoid wrapping an aggregate value in a NOP_EXPR. */
1066 if (TREE_CODE (temp) == CONSTRUCTOR)
1067 return build_constructor (type, CONSTRUCTOR_ELTS (temp));
1068 gcc_assert (scalarish_type_p (type));
1069 return cp_fold_convert (type, temp);
1072 /* True if we want to use the new handling of constexpr calls based on
1074 #define use_new_call true
1076 /* Subroutine of cxx_eval_call_expression.
1077 We are processing a call expression (either CALL_EXPR or
1078 AGGR_INIT_EXPR) in the context of CTX. Evaluate
1079 all arguments and bind their values to correspondings
1080 parameters, making up the NEW_CALL context. */
1083 cxx_bind_parameters_in_call (const constexpr_ctx *ctx, tree t,
1084 constexpr_call *new_call,
1085 bool *non_constant_p, bool *overflow_p,
1086 bool *non_constant_args)
1088 const int nargs = call_expr_nargs (t);
1089 tree fun = new_call->fundef->decl;
1090 tree parms = DECL_ARGUMENTS (fun);
1092 tree *p = &new_call->bindings;
1093 for (i = 0; i < nargs; ++i)
1096 tree type = parms ? TREE_TYPE (parms) : void_type_node;
1097 x = get_nth_callarg (t, i);
1098 /* For member function, the first argument is a pointer to the implied
1099 object. For a constructor, it might still be a dummy object, in
1100 which case we get the real argument from ctx. */
1101 if (i == 0 && DECL_CONSTRUCTOR_P (fun)
1102 && is_dummy_object (x))
1105 x = cp_build_addr_expr (x, tf_warning_or_error);
1108 if (parms && DECL_BY_REFERENCE (parms) && !use_new_call)
1110 /* cp_genericize made this a reference for argument passing, but
1111 we don't want to treat it like one for C++11 constexpr
1112 evaluation. C++14 constexpr evaluation uses the genericized
1114 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
1115 gcc_assert (TREE_CODE (TREE_TYPE (x)) == REFERENCE_TYPE);
1116 type = TREE_TYPE (type);
1117 x = convert_from_reference (x);
1120 arg = cxx_eval_constant_expression (ctx, x, lval,
1121 non_constant_p, overflow_p);
1122 /* Don't VERIFY_CONSTANT here. */
1123 if (*non_constant_p && ctx->quiet)
1125 /* Just discard ellipsis args after checking their constantitude. */
1128 if (*non_constant_p)
1129 /* Don't try to adjust the type of non-constant args. */
1132 /* Make sure the binding has the same type as the parm. */
1133 if (TREE_CODE (type) != REFERENCE_TYPE)
1134 arg = adjust_temp_type (type, arg);
1135 if (!TREE_CONSTANT (arg))
1136 *non_constant_args = true;
1137 *p = build_tree_list (parms, arg);
1138 p = &TREE_CHAIN (*p);
1140 parms = TREE_CHAIN (parms);
1144 /* Variables and functions to manage constexpr call expansion context.
1145 These do not need to be marked for PCH or GC. */
1147 /* FIXME remember and print actual constant arguments. */
1148 static vec<tree> call_stack = vNULL;
1149 static int call_stack_tick;
1150 static int last_cx_error_tick;
1153 push_cx_call_context (tree call)
1156 if (!EXPR_HAS_LOCATION (call))
1157 SET_EXPR_LOCATION (call, input_location);
1158 call_stack.safe_push (call);
1159 if (call_stack.length () > (unsigned) max_constexpr_depth)
1165 pop_cx_call_context (void)
1172 cx_error_context (void)
1174 vec<tree> r = vNULL;
1175 if (call_stack_tick != last_cx_error_tick
1176 && !call_stack.is_empty ())
1178 last_cx_error_tick = call_stack_tick;
1182 /* Subroutine of cxx_eval_constant_expression.
1183 Evaluate the call expression tree T in the context of OLD_CALL expression
1187 cxx_eval_call_expression (const constexpr_ctx *ctx, tree t,
1189 bool *non_constant_p, bool *overflow_p)
1191 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
1192 tree fun = get_function_named_in_call (t);
1193 constexpr_call new_call = { NULL, NULL, NULL, 0 };
1196 if (fun == NULL_TREE)
1197 switch (CALL_EXPR_IFN (t))
1199 case IFN_UBSAN_NULL:
1200 case IFN_UBSAN_BOUNDS:
1201 case IFN_UBSAN_VPTR:
1205 error_at (loc, "call to internal function");
1206 *non_constant_p = true;
1210 if (TREE_CODE (fun) != FUNCTION_DECL)
1212 /* Might be a constexpr function pointer. */
1213 fun = cxx_eval_constant_expression (ctx, fun,
1214 /*lval*/false, non_constant_p,
1217 if (TREE_CODE (fun) == ADDR_EXPR)
1218 fun = TREE_OPERAND (fun, 0);
1220 if (TREE_CODE (fun) != FUNCTION_DECL)
1222 if (!ctx->quiet && !*non_constant_p)
1223 error_at (loc, "expression %qE does not designate a constexpr "
1225 *non_constant_p = true;
1228 if (DECL_CLONED_FUNCTION_P (fun))
1229 fun = DECL_CLONED_FUNCTION (fun);
1231 if (is_ubsan_builtin_p (fun))
1234 if (is_builtin_fn (fun))
1235 return cxx_eval_builtin_function_call (ctx, t,
1236 lval, non_constant_p, overflow_p);
1237 if (!DECL_DECLARED_CONSTEXPR_P (fun))
1241 error_at (loc, "call to non-constexpr function %qD", fun);
1242 explain_invalid_constexpr_fn (fun);
1244 *non_constant_p = true;
1248 /* We can't defer instantiating the function any longer. */
1249 if (!DECL_INITIAL (fun)
1250 && DECL_TEMPLOID_INSTANTIATION (fun))
1253 instantiate_decl (fun, /*defer_ok*/false, /*expl_inst*/false);
1257 /* If in direct recursive call, optimize definition search. */
1258 if (ctx && ctx->call && ctx->call->fundef->decl == fun)
1259 new_call.fundef = ctx->call->fundef;
1262 new_call.fundef = retrieve_constexpr_fundef (fun);
1263 if (new_call.fundef == NULL || new_call.fundef->body == NULL)
1267 if (DECL_INITIAL (fun))
1269 /* The definition of fun was somehow unsuitable. */
1270 error_at (loc, "%qD called in a constant expression", fun);
1271 explain_invalid_constexpr_fn (fun);
1274 error_at (loc, "%qD used before its definition", fun);
1276 *non_constant_p = true;
1281 constexpr_ctx new_ctx = *ctx;
1282 if (DECL_CONSTRUCTOR_P (fun) && !ctx->object
1283 && TREE_CODE (t) == AGGR_INIT_EXPR)
1285 /* We want to have an initialization target for an AGGR_INIT_EXPR.
1286 If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
1287 new_ctx.object = AGGR_INIT_EXPR_SLOT (t);
1288 tree ctor = new_ctx.ctor = build_constructor (DECL_CONTEXT (fun), NULL);
1289 CONSTRUCTOR_NO_IMPLICIT_ZERO (ctor) = true;
1290 ctx->values->put (new_ctx.object, ctor);
1294 bool non_constant_args = false;
1295 cxx_bind_parameters_in_call (ctx, t, &new_call,
1296 non_constant_p, overflow_p, &non_constant_args);
1297 if (*non_constant_p)
1300 depth_ok = push_cx_call_context (t);
1302 tree result = NULL_TREE;
1304 constexpr_call *entry = NULL;
1305 if (!non_constant_args)
1307 new_call.hash = iterative_hash_template_arg
1308 (new_call.bindings, constexpr_fundef_hasher::hash (new_call.fundef));
1310 /* If we have seen this call before, we are done. */
1311 maybe_initialize_constexpr_call_table ();
1312 constexpr_call **slot
1313 = constexpr_call_table->find_slot (&new_call, INSERT);
1317 /* We need to keep a pointer to the entry, not just the slot, as the
1318 slot can move in the call to cxx_eval_builtin_function_call. */
1319 *slot = entry = ggc_alloc<constexpr_call> ();
1322 /* Calls which are in progress have their result set to NULL
1323 so that we can detect circular dependencies. */
1324 else if (entry->result == NULL)
1327 error ("call has circular dependency");
1328 *non_constant_p = true;
1329 entry->result = result = error_mark_node;
1332 result = entry->result;
1338 error ("constexpr evaluation depth exceeds maximum of %d (use "
1339 "-fconstexpr-depth= to increase the maximum)",
1340 max_constexpr_depth);
1341 *non_constant_p = true;
1342 result = error_mark_node;
1346 if (!result || result == error_mark_node)
1350 new_ctx.call = &new_call;
1351 result = (cxx_eval_constant_expression
1352 (&new_ctx, new_call.fundef->body,
1354 non_constant_p, overflow_p));
1358 if (DECL_SAVED_TREE (fun) == NULL_TREE
1359 && (DECL_CONSTRUCTOR_P (fun) || DECL_DESTRUCTOR_P (fun)))
1360 /* The maybe-in-charge 'tor had its DECL_SAVED_TREE
1361 cleared, try a clone. */
1362 for (fun = DECL_CHAIN (fun);
1363 fun && DECL_CLONED_FUNCTION_P (fun);
1364 fun = DECL_CHAIN (fun))
1365 if (DECL_SAVED_TREE (fun))
1367 if (!DECL_SAVED_TREE (fun))
1369 /* cgraph/gimplification have released the DECL_SAVED_TREE
1370 for this function. Fail gracefully. */
1371 gcc_assert (ctx->quiet);
1372 *non_constant_p = true;
1377 /* Unshare the whole function body. */
1378 tree body = copy_fn (fun, parms, res);
1380 /* Associate the bindings with the remapped parms. */
1381 tree bound = new_call.bindings;
1382 tree remapped = parms;
1385 tree oparm = TREE_PURPOSE (bound);
1386 tree arg = TREE_VALUE (bound);
1387 gcc_assert (DECL_NAME (remapped) == DECL_NAME (oparm));
1388 /* Don't share a CONSTRUCTOR that might be changed. */
1389 arg = unshare_expr (arg);
1390 ctx->values->put (remapped, arg);
1391 bound = TREE_CHAIN (bound);
1392 remapped = DECL_CHAIN (remapped);
1394 /* Add the RESULT_DECL to the values map, too. */
1395 tree slot = NULL_TREE;
1396 if (DECL_BY_REFERENCE (res))
1398 slot = AGGR_INIT_EXPR_SLOT (t);
1399 tree addr = build_address (slot);
1400 addr = build_nop (TREE_TYPE (res), addr);
1401 ctx->values->put (res, addr);
1402 ctx->values->put (slot, NULL_TREE);
1405 ctx->values->put (res, NULL_TREE);
1407 tree jump_target = NULL_TREE;
1408 cxx_eval_constant_expression (ctx, body,
1409 lval, non_constant_p, overflow_p,
1412 if (DECL_CONSTRUCTOR_P (fun))
1413 /* This can be null for a subobject constructor call, in
1414 which case what we care about is the initialization
1415 side-effects rather than the value. We could get at the
1416 value by evaluating *this, but we don't bother; there's
1417 no need to put such a call in the hash table. */
1418 result = lval ? ctx->object : ctx->ctor;
1419 else if (VOID_TYPE_P (TREE_TYPE (res)))
1423 result = *ctx->values->get (slot ? slot : res);
1424 if (result == NULL_TREE && !*non_constant_p)
1427 error ("constexpr call flows off the end "
1429 *non_constant_p = true;
1433 /* Remove the parms/result from the values map. Is it worth
1434 bothering to do this when the map itself is only live for
1435 one constexpr evaluation? If so, maybe also clear out
1436 other vars from call, maybe in BIND_EXPR handling? */
1437 ctx->values->remove (res);
1439 ctx->values->remove (slot);
1440 for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
1441 ctx->values->remove (parm);
1445 if (result == error_mark_node)
1446 *non_constant_p = true;
1447 if (*non_constant_p)
1448 result = error_mark_node;
1451 /* If this was a call to initialize an object, set the type of
1452 the CONSTRUCTOR to the type of that object. */
1453 if (DECL_CONSTRUCTOR_P (fun) && !use_new_call)
1455 tree ob_arg = get_nth_callarg (t, 0);
1456 STRIP_NOPS (ob_arg);
1457 gcc_assert (TYPE_PTR_P (TREE_TYPE (ob_arg))
1458 && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (ob_arg))));
1459 result = adjust_temp_type (TREE_TYPE (TREE_TYPE (ob_arg)),
1466 entry->result = result;
1469 pop_cx_call_context ();
1470 return unshare_expr (result);
1473 /* FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
1476 reduced_constant_expression_p (tree t)
1478 switch (TREE_CODE (t))
1481 /* Even if we can't lower this yet, it's constant. */
1485 /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
1486 tree elt; unsigned HOST_WIDE_INT idx;
1487 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), idx, elt)
1488 if (!reduced_constant_expression_p (elt))
1493 /* FIXME are we calling this too much? */
1494 return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE;
1498 /* Some expressions may have constant operands but are not constant
1499 themselves, such as 1/0. Call this function (or rather, the macro
1500 following it) to check for that condition.
1502 We only call this in places that require an arithmetic constant, not in
1503 places where we might have a non-constant expression that can be a
1504 component of a constant expression, such as the address of a constexpr
1505 variable that might be dereferenced later. */
1508 verify_constant (tree t, bool allow_non_constant, bool *non_constant_p,
1511 if (!*non_constant_p && !reduced_constant_expression_p (t))
1513 if (!allow_non_constant)
1514 error ("%q+E is not a constant expression", t);
1515 *non_constant_p = true;
1517 if (TREE_OVERFLOW_P (t))
1519 if (!allow_non_constant)
1521 permerror (input_location, "overflow in constant expression");
1522 /* If we're being permissive (and are in an enforcing
1523 context), ignore the overflow. */
1524 if (flag_permissive)
1525 return *non_constant_p;
1529 return *non_constant_p;
1532 /* Check whether the shift operation with code CODE and type TYPE on LHS
1533 and RHS is undefined. If it is, give an error with an explanation,
1534 and return true; return false otherwise. */
1537 cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx,
1538 enum tree_code code, tree type, tree lhs, tree rhs)
1540 if ((code != LSHIFT_EXPR && code != RSHIFT_EXPR)
1541 || TREE_CODE (lhs) != INTEGER_CST
1542 || TREE_CODE (rhs) != INTEGER_CST)
1545 tree lhstype = TREE_TYPE (lhs);
1546 unsigned HOST_WIDE_INT uprec = TYPE_PRECISION (TREE_TYPE (lhs));
1548 /* [expr.shift] The behavior is undefined if the right operand
1549 is negative, or greater than or equal to the length in bits
1550 of the promoted left operand. */
1551 if (tree_int_cst_sgn (rhs) == -1)
1554 error_at (loc, "right operand of shift expression %q+E is negative",
1555 build2_loc (loc, code, type, lhs, rhs));
1558 if (compare_tree_int (rhs, uprec) >= 0)
1561 error_at (loc, "right operand of shift expression %q+E is >= than "
1562 "the precision of the left operand",
1563 build2_loc (loc, code, type, lhs, rhs));
1567 /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
1568 if E1 has a signed type and non-negative value, and E1x2^E2 is
1569 representable in the corresponding unsigned type of the result type,
1570 then that value, converted to the result type, is the resulting value;
1571 otherwise, the behavior is undefined. */
1572 if (code == LSHIFT_EXPR && !TYPE_UNSIGNED (lhstype)
1573 && (cxx_dialect >= cxx11))
1575 if (tree_int_cst_sgn (lhs) == -1)
1578 error_at (loc, "left operand of shift expression %q+E is negative",
1579 build2_loc (loc, code, type, lhs, rhs));
1582 /* For signed x << y the following:
1583 (unsigned) x >> ((prec (lhs) - 1) - y)
1584 if > 1, is undefined. The right-hand side of this formula
1585 is the highest bit of the LHS that can be set (starting from 0),
1586 so that the shift doesn't overflow. We then right-shift the LHS
1587 to see whether any other bit is set making the original shift
1588 undefined -- the result is not representable in the corresponding
1590 tree t = build_int_cst (unsigned_type_node, uprec - 1);
1591 t = fold_build2 (MINUS_EXPR, unsigned_type_node, t, rhs);
1592 tree ulhs = fold_convert (unsigned_type_for (lhstype), lhs);
1593 t = fold_build2 (RSHIFT_EXPR, TREE_TYPE (ulhs), ulhs, t);
1594 if (tree_int_cst_lt (integer_one_node, t))
1597 error_at (loc, "shift expression %q+E overflows",
1598 build2_loc (loc, code, type, lhs, rhs));
1605 /* Subroutine of cxx_eval_constant_expression.
1606 Attempt to reduce the unary expression tree T to a compile time value.
1607 If successful, return the value. Otherwise issue a diagnostic
1608 and return error_mark_node. */
1611 cxx_eval_unary_expression (const constexpr_ctx *ctx, tree t,
1613 bool *non_constant_p, bool *overflow_p)
1616 tree orig_arg = TREE_OPERAND (t, 0);
1617 tree arg = cxx_eval_constant_expression (ctx, orig_arg, /*lval*/false,
1618 non_constant_p, overflow_p);
1619 VERIFY_CONSTANT (arg);
1620 location_t loc = EXPR_LOCATION (t);
1621 enum tree_code code = TREE_CODE (t);
1622 tree type = TREE_TYPE (t);
1623 r = fold_unary_loc (loc, code, type, arg);
1626 if (arg == orig_arg)
1629 r = build1_loc (loc, code, type, arg);
1631 VERIFY_CONSTANT (r);
1635 /* Subroutine of cxx_eval_constant_expression.
1636 Like cxx_eval_unary_expression, except for binary expressions. */
1639 cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t,
1641 bool *non_constant_p, bool *overflow_p)
1644 tree orig_lhs = TREE_OPERAND (t, 0);
1645 tree orig_rhs = TREE_OPERAND (t, 1);
1647 lhs = cxx_eval_constant_expression (ctx, orig_lhs, /*lval*/false,
1648 non_constant_p, overflow_p);
1649 /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer
1651 if (*non_constant_p)
1653 rhs = cxx_eval_constant_expression (ctx, orig_rhs, /*lval*/false,
1654 non_constant_p, overflow_p);
1655 if (*non_constant_p)
1658 location_t loc = EXPR_LOCATION (t);
1659 enum tree_code code = TREE_CODE (t);
1660 tree type = TREE_TYPE (t);
1661 r = fold_binary_loc (loc, code, type, lhs, rhs);
1664 if (lhs == orig_lhs && rhs == orig_rhs)
1667 r = build2_loc (loc, code, type, lhs, rhs);
1669 else if (cxx_eval_check_shift_p (loc, ctx, code, type, lhs, rhs))
1670 *non_constant_p = true;
1671 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
1672 a local array in a constexpr function. */
1673 bool ptr = POINTER_TYPE_P (TREE_TYPE (lhs));
1675 VERIFY_CONSTANT (r);
1679 /* Subroutine of cxx_eval_constant_expression.
1680 Attempt to evaluate condition expressions. Dead branches are not
1684 cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t,
1686 bool *non_constant_p, bool *overflow_p,
1689 tree val = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
1691 non_constant_p, overflow_p);
1692 VERIFY_CONSTANT (val);
1693 /* Don't VERIFY_CONSTANT the other operands. */
1694 if (integer_zerop (val))
1695 return cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2),
1697 non_constant_p, overflow_p,
1699 return cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
1701 non_constant_p, overflow_p,
1705 /* Returns less than, equal to, or greater than zero if KEY is found to be
1706 less than, to match, or to be greater than the constructor_elt's INDEX. */
1709 array_index_cmp (tree key, tree index)
1711 gcc_assert (TREE_CODE (key) == INTEGER_CST);
1713 switch (TREE_CODE (index))
1716 return tree_int_cst_compare (key, index);
1719 tree lo = TREE_OPERAND (index, 0);
1720 tree hi = TREE_OPERAND (index, 1);
1721 if (tree_int_cst_lt (key, lo))
1723 else if (tree_int_cst_lt (hi, key))
1733 /* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1
1734 if none. If INSERT is true, insert a matching element rather than fail. */
1736 static HOST_WIDE_INT
1737 find_array_ctor_elt (tree ary, tree dindex, bool insert = false)
1739 if (tree_int_cst_sgn (dindex) < 0)
1742 unsigned HOST_WIDE_INT i = tree_to_uhwi (dindex);
1743 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ary);
1744 unsigned HOST_WIDE_INT len = vec_safe_length (elts);
1746 unsigned HOST_WIDE_INT end = len;
1747 unsigned HOST_WIDE_INT begin = 0;
1749 /* If the last element of the CONSTRUCTOR has its own index, we can assume
1750 that the same is true of the other elements and index directly. */
1753 tree cindex = (*elts)[end-1].index;
1754 if (TREE_CODE (cindex) == INTEGER_CST
1755 && compare_tree_int (cindex, end-1) == 0)
1764 /* Otherwise, find a matching index by means of a binary search. */
1765 while (begin != end)
1767 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
1768 constructor_elt &elt = (*elts)[middle];
1769 tree idx = elt.index;
1771 int cmp = array_index_cmp (dindex, idx);
1778 if (insert && TREE_CODE (idx) == RANGE_EXPR)
1780 /* We need to split the range. */
1782 tree lo = TREE_OPERAND (idx, 0);
1783 tree hi = TREE_OPERAND (idx, 1);
1784 if (tree_int_cst_lt (lo, dindex))
1786 /* There are still some lower elts; shorten the range. */
1787 tree new_hi = int_const_binop (MINUS_EXPR, dindex,
1789 if (tree_int_cst_equal (lo, new_hi))
1790 /* Only one element left, no longer a range. */
1793 TREE_OPERAND (idx, 1) = new_hi;
1794 /* Append the element we want to insert. */
1797 e.value = unshare_expr (elt.value);
1798 vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e);
1801 /* No lower elts, the range elt is now ours. */
1804 if (tree_int_cst_lt (dindex, hi))
1806 /* There are still some higher elts; append a range. */
1807 tree new_lo = int_const_binop (PLUS_EXPR, dindex,
1809 if (tree_int_cst_equal (new_lo, hi))
1812 e.index = build2 (RANGE_EXPR, sizetype, new_lo, hi);
1813 e.value = unshare_expr (elt.value);
1814 vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle+1, e);
1823 constructor_elt e = { dindex, NULL_TREE };
1824 vec_safe_insert (CONSTRUCTOR_ELTS (ary), end, e);
1832 /* Subroutine of cxx_eval_constant_expression.
1833 Attempt to reduce a reference to an array slot. */
1836 cxx_eval_array_reference (const constexpr_ctx *ctx, tree t,
1838 bool *non_constant_p, bool *overflow_p)
1840 tree oldary = TREE_OPERAND (t, 0);
1841 tree ary = cxx_eval_constant_expression (ctx, oldary,
1843 non_constant_p, overflow_p);
1847 unsigned len, elem_nchars = 1;
1848 if (*non_constant_p)
1850 oldidx = TREE_OPERAND (t, 1);
1851 index = cxx_eval_constant_expression (ctx, oldidx,
1853 non_constant_p, overflow_p);
1854 VERIFY_CONSTANT (index);
1855 if (lval && ary == oldary && index == oldidx)
1858 return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL);
1859 elem_type = TREE_TYPE (TREE_TYPE (ary));
1860 if (TREE_CODE (ary) == CONSTRUCTOR)
1861 len = CONSTRUCTOR_NELTS (ary);
1862 else if (TREE_CODE (ary) == STRING_CST)
1864 elem_nchars = (TYPE_PRECISION (elem_type)
1865 / TYPE_PRECISION (char_type_node));
1866 len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars;
1870 /* We can't do anything with other tree codes, so use
1871 VERIFY_CONSTANT to complain and fail. */
1872 VERIFY_CONSTANT (ary);
1876 if (!tree_fits_shwi_p (index)
1877 || (i = tree_to_shwi (index)) < 0)
1880 error ("negative array subscript");
1881 *non_constant_p = true;
1886 if (TREE_CODE (ary) == CONSTRUCTOR)
1888 HOST_WIDE_INT ix = find_array_ctor_elt (ary, index);
1898 if (tree_int_cst_lt (index, array_type_nelts_top (TREE_TYPE (ary))))
1900 /* If it's within the array bounds but doesn't have an explicit
1901 initializer, it's value-initialized. */
1902 tree val = build_value_init (elem_type, tf_warning_or_error);
1903 return cxx_eval_constant_expression (ctx, val,
1905 non_constant_p, overflow_p);
1909 error ("array subscript out of bound");
1910 *non_constant_p = true;
1914 if (TREE_CODE (ary) == CONSTRUCTOR)
1915 return (*CONSTRUCTOR_ELTS (ary))[i].value;
1916 else if (elem_nchars == 1)
1917 return build_int_cst (cv_unqualified (TREE_TYPE (TREE_TYPE (ary))),
1918 TREE_STRING_POINTER (ary)[i]);
1921 tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (ary)));
1922 return native_interpret_expr (type, (const unsigned char *)
1923 TREE_STRING_POINTER (ary)
1924 + i * elem_nchars, elem_nchars);
1926 /* Don't VERIFY_CONSTANT here. */
1929 /* Subroutine of cxx_eval_constant_expression.
1930 Attempt to reduce a field access of a value of class type. */
1933 cxx_eval_component_reference (const constexpr_ctx *ctx, tree t,
1935 bool *non_constant_p, bool *overflow_p)
1937 unsigned HOST_WIDE_INT i;
1940 tree part = TREE_OPERAND (t, 1);
1941 tree orig_whole = TREE_OPERAND (t, 0);
1942 tree whole = cxx_eval_constant_expression (ctx, orig_whole,
1944 non_constant_p, overflow_p);
1945 if (whole == orig_whole)
1948 return fold_build3 (COMPONENT_REF, TREE_TYPE (t),
1949 whole, part, NULL_TREE);
1950 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
1952 if (!*non_constant_p && TREE_CODE (whole) != CONSTRUCTOR)
1955 error ("%qE is not a constant expression", orig_whole);
1956 *non_constant_p = true;
1958 if (DECL_MUTABLE_P (part))
1961 error ("mutable %qD is not usable in a constant expression", part);
1962 *non_constant_p = true;
1964 if (*non_constant_p)
1966 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
1973 /* We're in the middle of initializing it. */
1977 if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE
1978 && CONSTRUCTOR_NELTS (whole) > 0)
1980 /* DR 1188 says we don't have to deal with this. */
1982 error ("accessing %qD member instead of initialized %qD member in "
1983 "constant expression", part, CONSTRUCTOR_ELT (whole, 0)->index);
1984 *non_constant_p = true;
1988 /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty
1989 classes never get represented; throw together a value now. */
1990 if (is_really_empty_class (TREE_TYPE (t)))
1991 return build_constructor (TREE_TYPE (t), NULL);
1993 if (CONSTRUCTOR_NO_IMPLICIT_ZERO (whole))
1995 /* 'whole' is part of the aggregate initializer we're currently
1996 building; if there's no initializer for this member yet, that's an
1999 error ("accessing uninitialized member %qD", part);
2000 *non_constant_p = true;
2004 /* If there's no explicit init for this field, it's value-initialized. */
2005 value = build_value_init (TREE_TYPE (t), tf_warning_or_error);
2006 return cxx_eval_constant_expression (ctx, value,
2008 non_constant_p, overflow_p);
2011 /* Subroutine of cxx_eval_constant_expression.
2012 Attempt to reduce a field access of a value of class type that is
2013 expressed as a BIT_FIELD_REF. */
2016 cxx_eval_bit_field_ref (const constexpr_ctx *ctx, tree t,
2018 bool *non_constant_p, bool *overflow_p)
2020 tree orig_whole = TREE_OPERAND (t, 0);
2021 tree retval, fldval, utype, mask;
2022 bool fld_seen = false;
2023 HOST_WIDE_INT istart, isize;
2024 tree whole = cxx_eval_constant_expression (ctx, orig_whole,
2026 non_constant_p, overflow_p);
2027 tree start, field, value;
2028 unsigned HOST_WIDE_INT i;
2030 if (whole == orig_whole)
2032 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
2034 if (!*non_constant_p
2035 && TREE_CODE (whole) != VECTOR_CST
2036 && TREE_CODE (whole) != CONSTRUCTOR)
2039 error ("%qE is not a constant expression", orig_whole);
2040 *non_constant_p = true;
2042 if (*non_constant_p)
2045 if (TREE_CODE (whole) == VECTOR_CST)
2046 return fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
2047 TREE_OPERAND (t, 1), TREE_OPERAND (t, 2));
2049 start = TREE_OPERAND (t, 2);
2050 istart = tree_to_shwi (start);
2051 isize = tree_to_shwi (TREE_OPERAND (t, 1));
2052 utype = TREE_TYPE (t);
2053 if (!TYPE_UNSIGNED (utype))
2054 utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1);
2055 retval = build_int_cst (utype, 0);
2056 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
2058 tree bitpos = bit_position (field);
2059 if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1))
2061 if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE
2062 && TREE_CODE (value) == INTEGER_CST
2063 && tree_fits_shwi_p (bitpos)
2064 && tree_fits_shwi_p (DECL_SIZE (field)))
2066 HOST_WIDE_INT bit = tree_to_shwi (bitpos);
2067 HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field));
2068 HOST_WIDE_INT shift;
2069 if (bit >= istart && bit + sz <= istart + isize)
2071 fldval = fold_convert (utype, value);
2072 mask = build_int_cst_type (utype, -1);
2073 mask = fold_build2 (LSHIFT_EXPR, utype, mask,
2074 size_int (TYPE_PRECISION (utype) - sz));
2075 mask = fold_build2 (RSHIFT_EXPR, utype, mask,
2076 size_int (TYPE_PRECISION (utype) - sz));
2077 fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask);
2078 shift = bit - istart;
2079 if (BYTES_BIG_ENDIAN)
2080 shift = TYPE_PRECISION (utype) - shift - sz;
2081 fldval = fold_build2 (LSHIFT_EXPR, utype, fldval,
2083 retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval);
2089 return fold_convert (TREE_TYPE (t), retval);
2091 return error_mark_node;
2094 /* Subroutine of cxx_eval_constant_expression.
2095 Evaluate a short-circuited logical expression T in the context
2096 of a given constexpr CALL. BAILOUT_VALUE is the value for
2097 early return. CONTINUE_VALUE is used here purely for
2098 sanity check purposes. */
2101 cxx_eval_logical_expression (const constexpr_ctx *ctx, tree t,
2102 tree bailout_value, tree continue_value,
2104 bool *non_constant_p, bool *overflow_p)
2107 tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
2109 non_constant_p, overflow_p);
2110 VERIFY_CONSTANT (lhs);
2111 if (tree_int_cst_equal (lhs, bailout_value))
2113 gcc_assert (tree_int_cst_equal (lhs, continue_value));
2114 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
2115 lval, non_constant_p,
2117 VERIFY_CONSTANT (r);
2121 /* REF is a COMPONENT_REF designating a particular field. V is a vector of
2122 CONSTRUCTOR elements to initialize (part of) an object containing that
2123 field. Return a pointer to the constructor_elt corresponding to the
2124 initialization of the field. */
2126 static constructor_elt *
2127 base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref)
2129 tree aggr = TREE_OPERAND (ref, 0);
2130 tree field = TREE_OPERAND (ref, 1);
2132 constructor_elt *ce;
2134 gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
2136 if (TREE_CODE (aggr) == COMPONENT_REF)
2138 constructor_elt *base_ce
2139 = base_field_constructor_elt (v, aggr);
2140 v = CONSTRUCTOR_ELTS (base_ce->value);
2143 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
2144 if (ce->index == field)
2151 /* Some of the expressions fed to the constexpr mechanism are calls to
2152 constructors, which have type void. In that case, return the type being
2153 initialized by the constructor. */
2156 initialized_type (tree t)
2160 tree type = cv_unqualified (TREE_TYPE (t));
2161 if (TREE_CODE (t) == CALL_EXPR || TREE_CODE (t) == AGGR_INIT_EXPR)
2163 /* A constructor call has void type, so we need to look deeper. */
2164 tree fn = get_function_named_in_call (t);
2165 if (fn && TREE_CODE (fn) == FUNCTION_DECL
2166 && DECL_CXX_CONSTRUCTOR_P (fn))
2167 type = DECL_CONTEXT (fn);
2172 /* We're about to initialize element INDEX of an array or class from VALUE.
2173 Set up NEW_CTX appropriately by adjusting .object to refer to the
2174 subobject and creating a new CONSTRUCTOR if the element is itself
2175 a class or array. */
2178 init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx,
2179 tree index, tree &value)
2183 if (index && TREE_CODE (index) != INTEGER_CST
2184 && TREE_CODE (index) != FIELD_DECL)
2185 /* This won't have an element in the new CONSTRUCTOR. */
2188 tree type = initialized_type (value);
2189 if (!AGGREGATE_TYPE_P (type) && !VECTOR_TYPE_P (type))
2190 /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
2193 /* The sub-aggregate initializer might contain a placeholder;
2194 update object to refer to the subobject and ctor to refer to
2195 the (newly created) sub-initializer. */
2197 new_ctx.object = build_ctor_subob_ref (index, type, ctx->object);
2198 tree elt = build_constructor (type, NULL);
2199 CONSTRUCTOR_NO_IMPLICIT_ZERO (elt) = true;
2202 if (TREE_CODE (value) == TARGET_EXPR)
2203 /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
2204 value = TARGET_EXPR_INITIAL (value);
2207 /* We're about to process an initializer for a class or array TYPE. Make
2208 sure that CTX is set up appropriately. */
2211 verify_ctor_sanity (const constexpr_ctx *ctx, tree type)
2213 /* We don't bother building a ctor for an empty base subobject. */
2214 if (is_empty_class (type))
2217 /* We're in the middle of an initializer that might involve placeholders;
2218 our caller should have created a CONSTRUCTOR for us to put the
2219 initializer into. We will either return that constructor or T. */
2220 gcc_assert (ctx->ctor);
2221 gcc_assert (same_type_ignoring_top_level_qualifiers_p
2222 (type, TREE_TYPE (ctx->ctor)));
2223 /* We used to check that ctx->ctor was empty, but that isn't the case when
2224 the object is zero-initialized before calling the constructor. */
2226 gcc_assert (same_type_ignoring_top_level_qualifiers_p
2227 (type, TREE_TYPE (ctx->object)));
2228 gcc_assert (!ctx->object || !DECL_P (ctx->object)
2229 || *(ctx->values->get (ctx->object)) == ctx->ctor);
2232 /* Subroutine of cxx_eval_constant_expression.
2233 The expression tree T denotes a C-style array or a C-style
2234 aggregate. Reduce it to a constant expression. */
2237 cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t,
2239 bool *non_constant_p, bool *overflow_p)
2241 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
2242 bool changed = false;
2243 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t));
2245 verify_ctor_sanity (ctx, TREE_TYPE (t));
2246 vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
2247 vec_alloc (*p, vec_safe_length (v));
2249 unsigned i; tree index, value;
2250 FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value)
2252 tree orig_value = value;
2253 constexpr_ctx new_ctx;
2254 init_subob_ctx (ctx, new_ctx, index, value);
2255 if (new_ctx.ctor != ctx->ctor)
2256 /* If we built a new CONSTRUCTOR, attach it now so that other
2257 initializers can refer to it. */
2258 CONSTRUCTOR_APPEND_ELT (*p, index, new_ctx.ctor);
2259 tree elt = cxx_eval_constant_expression (&new_ctx, value,
2261 non_constant_p, overflow_p);
2262 /* Don't VERIFY_CONSTANT here. */
2263 if (ctx->quiet && *non_constant_p)
2265 if (elt != orig_value)
2267 if (index && TREE_CODE (index) == COMPONENT_REF)
2269 /* This is an initialization of a vfield inside a base
2270 subaggregate that we already initialized; push this
2271 initialization into the previous initialization. */
2272 constructor_elt *inner = base_field_constructor_elt (*p, index);
2277 && (TREE_CODE (index) == NOP_EXPR
2278 || TREE_CODE (index) == POINTER_PLUS_EXPR))
2280 /* This is an initializer for an empty base; now that we've
2281 checked that it's constant, we can ignore it. */
2282 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index))));
2285 else if (new_ctx.ctor != ctx->ctor)
2287 /* We appended this element above; update the value. */
2288 gcc_assert ((*p)->last().index == index);
2289 (*p)->last().value = elt;
2292 CONSTRUCTOR_APPEND_ELT (*p, index, elt);
2294 if (*non_constant_p || !changed)
2297 /* We're done building this CONSTRUCTOR, so now we can interpret an
2298 element without an explicit initializer as value-initialized. */
2299 CONSTRUCTOR_NO_IMPLICIT_ZERO (t) = false;
2300 if (TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
2305 /* Subroutine of cxx_eval_constant_expression.
2306 The expression tree T is a VEC_INIT_EXPR which denotes the desired
2307 initialization of a non-static data member of array type. Reduce it to a
2310 Note that apart from value-initialization (when VALUE_INIT is true),
2311 this is only intended to support value-initialization and the
2312 initializations done by defaulted constructors for classes with
2313 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
2314 will either be NULL_TREE for the default constructor, or a COMPONENT_REF
2315 for the copy/move constructor. */
2318 cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init,
2319 bool value_init, bool lval,
2320 bool *non_constant_p, bool *overflow_p)
2322 tree elttype = TREE_TYPE (atype);
2323 unsigned HOST_WIDE_INT max = tree_to_uhwi (array_type_nelts_top (atype));
2324 verify_ctor_sanity (ctx, atype);
2325 vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
2326 vec_alloc (*p, max + 1);
2327 bool pre_init = false;
2328 unsigned HOST_WIDE_INT i;
2330 /* For the default constructor, build up a call to the default
2331 constructor of the element type. We only need to handle class types
2332 here, as for a constructor to be constexpr, all members must be
2333 initialized, which for a defaulted default constructor means they must
2334 be of a class type with a constexpr default constructor. */
2335 if (TREE_CODE (elttype) == ARRAY_TYPE)
2336 /* We only do this at the lowest level. */;
2337 else if (value_init)
2339 init = build_value_init (elttype, tf_warning_or_error);
2344 vec<tree, va_gc> *argvec = make_tree_vector ();
2345 init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
2346 &argvec, elttype, LOOKUP_NORMAL,
2347 tf_warning_or_error);
2348 release_tree_vector (argvec);
2349 init = build_aggr_init_expr (TREE_TYPE (init), init);
2353 for (i = 0; i < max; ++i)
2355 tree idx = build_int_cst (size_type_node, i);
2357 constexpr_ctx new_ctx;
2358 init_subob_ctx (ctx, new_ctx, idx, pre_init ? init : elttype);
2359 if (new_ctx.ctor != ctx->ctor)
2360 CONSTRUCTOR_APPEND_ELT (*p, idx, new_ctx.ctor);
2361 if (TREE_CODE (elttype) == ARRAY_TYPE)
2363 /* A multidimensional array; recurse. */
2364 if (value_init || init == NULL_TREE)
2365 eltinit = NULL_TREE;
2367 eltinit = cp_build_array_ref (input_location, init, idx,
2368 tf_warning_or_error);
2369 eltinit = cxx_eval_vec_init_1 (&new_ctx, elttype, eltinit, value_init,
2371 non_constant_p, overflow_p);
2375 /* Initializing an element using value or default initialization
2376 we just pre-built above. */
2377 eltinit = (cxx_eval_constant_expression
2379 lval, non_constant_p, overflow_p));
2383 /* Copying an element. */
2384 gcc_assert (same_type_ignoring_top_level_qualifiers_p
2385 (atype, TREE_TYPE (init)));
2386 eltinit = cp_build_array_ref (input_location, init, idx,
2387 tf_warning_or_error);
2388 if (!real_lvalue_p (init))
2389 eltinit = move (eltinit);
2390 eltinit = force_rvalue (eltinit, tf_warning_or_error);
2391 eltinit = (cxx_eval_constant_expression
2392 (&new_ctx, eltinit, lval,
2393 non_constant_p, overflow_p));
2395 if (*non_constant_p && !ctx->quiet)
2397 if (new_ctx.ctor != ctx->ctor)
2399 /* We appended this element above; update the value. */
2400 gcc_assert ((*p)->last().index == idx);
2401 (*p)->last().value = eltinit;
2404 CONSTRUCTOR_APPEND_ELT (*p, idx, eltinit);
2407 if (!*non_constant_p)
2410 CONSTRUCTOR_NO_IMPLICIT_ZERO (init) = false;
2416 cxx_eval_vec_init (const constexpr_ctx *ctx, tree t,
2418 bool *non_constant_p, bool *overflow_p)
2420 tree atype = TREE_TYPE (t);
2421 tree init = VEC_INIT_EXPR_INIT (t);
2422 tree r = cxx_eval_vec_init_1 (ctx, atype, init,
2423 VEC_INIT_EXPR_VALUE_INIT (t),
2424 lval, non_constant_p, overflow_p);
2425 if (*non_constant_p)
2431 /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
2432 match. We want to be less strict for simple *& folding; if we have a
2433 non-const temporary that we access through a const pointer, that should
2434 work. We handle this here rather than change fold_indirect_ref_1
2435 because we're dealing with things like ADDR_EXPR of INTEGER_CST which
2436 don't really make sense outside of constant expression evaluation. Also
2437 we want to allow folding to COMPONENT_REF, which could cause trouble
2438 with TBAA in fold_indirect_ref_1.
2440 Try to keep this function synced with fold_indirect_ref_1. */
2443 cxx_fold_indirect_ref (location_t loc, tree type, tree op0, bool *empty_base)
2449 subtype = TREE_TYPE (sub);
2450 if (!POINTER_TYPE_P (subtype))
2453 if (TREE_CODE (sub) == ADDR_EXPR)
2455 tree op = TREE_OPERAND (sub, 0);
2456 tree optype = TREE_TYPE (op);
2458 /* *&CONST_DECL -> to the value of the const decl. */
2459 if (TREE_CODE (op) == CONST_DECL)
2460 return DECL_INITIAL (op);
2461 /* *&p => p; make sure to handle *&"str"[cst] here. */
2462 if (same_type_ignoring_top_level_qualifiers_p (optype, type))
2464 tree fop = fold_read_from_constant_string (op);
2470 /* *(foo *)&fooarray => fooarray[0] */
2471 else if (TREE_CODE (optype) == ARRAY_TYPE
2472 && (same_type_ignoring_top_level_qualifiers_p
2473 (type, TREE_TYPE (optype))))
2475 tree type_domain = TYPE_DOMAIN (optype);
2476 tree min_val = size_zero_node;
2477 if (type_domain && TYPE_MIN_VALUE (type_domain))
2478 min_val = TYPE_MIN_VALUE (type_domain);
2479 return build4_loc (loc, ARRAY_REF, type, op, min_val,
2480 NULL_TREE, NULL_TREE);
2482 /* *(foo *)&complexfoo => __real__ complexfoo */
2483 else if (TREE_CODE (optype) == COMPLEX_TYPE
2484 && (same_type_ignoring_top_level_qualifiers_p
2485 (type, TREE_TYPE (optype))))
2486 return fold_build1_loc (loc, REALPART_EXPR, type, op);
2487 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
2488 else if (TREE_CODE (optype) == VECTOR_TYPE
2489 && (same_type_ignoring_top_level_qualifiers_p
2490 (type, TREE_TYPE (optype))))
2492 tree part_width = TYPE_SIZE (type);
2493 tree index = bitsize_int (0);
2494 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
2496 /* Also handle conversion to an empty base class, which
2497 is represented with a NOP_EXPR. */
2498 else if (is_empty_class (type)
2499 && CLASS_TYPE_P (optype)
2500 && DERIVED_FROM_P (type, optype))
2505 /* *(foo *)&struct_with_foo_field => COMPONENT_REF */
2506 else if (RECORD_OR_UNION_TYPE_P (optype))
2508 tree field = TYPE_FIELDS (optype);
2509 for (; field; field = DECL_CHAIN (field))
2510 if (TREE_CODE (field) == FIELD_DECL
2511 && integer_zerop (byte_position (field))
2512 && (same_type_ignoring_top_level_qualifiers_p
2513 (TREE_TYPE (field), type)))
2515 return fold_build3 (COMPONENT_REF, type, op, field, NULL_TREE);
2520 else if (TREE_CODE (sub) == POINTER_PLUS_EXPR
2521 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
2523 tree op00 = TREE_OPERAND (sub, 0);
2524 tree op01 = TREE_OPERAND (sub, 1);
2527 if (TREE_CODE (op00) == ADDR_EXPR)
2530 op00 = TREE_OPERAND (op00, 0);
2531 op00type = TREE_TYPE (op00);
2533 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
2534 if (TREE_CODE (op00type) == VECTOR_TYPE
2535 && (same_type_ignoring_top_level_qualifiers_p
2536 (type, TREE_TYPE (op00type))))
2538 HOST_WIDE_INT offset = tree_to_shwi (op01);
2539 tree part_width = TYPE_SIZE (type);
2540 unsigned HOST_WIDE_INT part_widthi = tree_to_shwi (part_width)/BITS_PER_UNIT;
2541 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
2542 tree index = bitsize_int (indexi);
2544 if (offset / part_widthi < TYPE_VECTOR_SUBPARTS (op00type))
2545 return fold_build3_loc (loc,
2546 BIT_FIELD_REF, type, op00,
2550 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
2551 else if (TREE_CODE (op00type) == COMPLEX_TYPE
2552 && (same_type_ignoring_top_level_qualifiers_p
2553 (type, TREE_TYPE (op00type))))
2555 tree size = TYPE_SIZE_UNIT (type);
2556 if (tree_int_cst_equal (size, op01))
2557 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
2559 /* ((foo *)&fooarray)[1] => fooarray[1] */
2560 else if (TREE_CODE (op00type) == ARRAY_TYPE
2561 && (same_type_ignoring_top_level_qualifiers_p
2562 (type, TREE_TYPE (op00type))))
2564 tree type_domain = TYPE_DOMAIN (op00type);
2565 tree min_val = size_zero_node;
2566 if (type_domain && TYPE_MIN_VALUE (type_domain))
2567 min_val = TYPE_MIN_VALUE (type_domain);
2568 op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
2569 TYPE_SIZE_UNIT (type));
2570 op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
2571 return build4_loc (loc, ARRAY_REF, type, op00, op01,
2572 NULL_TREE, NULL_TREE);
2574 /* Also handle conversion to an empty base class, which
2575 is represented with a NOP_EXPR. */
2576 else if (is_empty_class (type)
2577 && CLASS_TYPE_P (op00type)
2578 && DERIVED_FROM_P (type, op00type))
2583 /* ((foo *)&struct_with_foo_field)[1] => COMPONENT_REF */
2584 else if (RECORD_OR_UNION_TYPE_P (op00type))
2586 tree field = TYPE_FIELDS (op00type);
2587 for (; field; field = DECL_CHAIN (field))
2588 if (TREE_CODE (field) == FIELD_DECL
2589 && tree_int_cst_equal (byte_position (field), op01)
2590 && (same_type_ignoring_top_level_qualifiers_p
2591 (TREE_TYPE (field), type)))
2593 return fold_build3 (COMPONENT_REF, type, op00,
2600 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
2601 else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
2602 && (same_type_ignoring_top_level_qualifiers_p
2603 (type, TREE_TYPE (TREE_TYPE (subtype)))))
2606 tree min_val = size_zero_node;
2607 tree newsub = cxx_fold_indirect_ref (loc, TREE_TYPE (subtype), sub, NULL);
2611 sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub);
2612 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
2613 if (type_domain && TYPE_MIN_VALUE (type_domain))
2614 min_val = TYPE_MIN_VALUE (type_domain);
2615 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
2623 cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t,
2625 bool *non_constant_p, bool *overflow_p)
2627 tree orig_op0 = TREE_OPERAND (t, 0);
2628 tree op0 = cxx_eval_constant_expression (ctx, orig_op0,
2629 /*lval*/false, non_constant_p,
2631 bool empty_base = false;
2634 /* Don't VERIFY_CONSTANT here. */
2635 if (*non_constant_p)
2638 r = cxx_fold_indirect_ref (EXPR_LOCATION (t), TREE_TYPE (t), op0,
2642 r = cxx_eval_constant_expression (ctx, r,
2643 lval, non_constant_p, overflow_p);
2648 if (TREE_CODE (sub) == ADDR_EXPR)
2650 /* We couldn't fold to a constant value. Make sure it's not
2651 something we should have been able to fold. */
2652 gcc_assert (!same_type_ignoring_top_level_qualifiers_p
2653 (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t)));
2654 /* DR 1188 says we don't have to deal with this. */
2656 error ("accessing value of %qE through a %qT glvalue in a "
2657 "constant expression", build_fold_indirect_ref (sub),
2659 *non_constant_p = true;
2664 /* If we're pulling out the value of an empty base, make sure
2665 that the whole object is constant and then return an empty
2667 if (empty_base && !lval)
2669 VERIFY_CONSTANT (r);
2670 r = build_constructor (TREE_TYPE (t), NULL);
2671 TREE_CONSTANT (r) = true;
2676 if (lval && op0 != orig_op0)
2677 return build1 (INDIRECT_REF, TREE_TYPE (t), op0);
2679 VERIFY_CONSTANT (t);
2685 /* Complain about R, a VAR_DECL, not being usable in a constant expression.
2686 Shared between potential_constant_expression and
2687 cxx_eval_constant_expression. */
2690 non_const_var_error (tree r)
2692 tree type = TREE_TYPE (r);
2693 error ("the value of %qD is not usable in a constant "
2695 /* Avoid error cascade. */
2696 if (DECL_INITIAL (r) == error_mark_node)
2698 if (DECL_DECLARED_CONSTEXPR_P (r))
2699 inform (DECL_SOURCE_LOCATION (r),
2700 "%qD used in its own initializer", r);
2701 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
2703 if (!CP_TYPE_CONST_P (type))
2704 inform (DECL_SOURCE_LOCATION (r),
2705 "%q#D is not const", r);
2706 else if (CP_TYPE_VOLATILE_P (type))
2707 inform (DECL_SOURCE_LOCATION (r),
2708 "%q#D is volatile", r);
2709 else if (!DECL_INITIAL (r)
2710 || !TREE_CONSTANT (DECL_INITIAL (r)))
2711 inform (DECL_SOURCE_LOCATION (r),
2712 "%qD was not initialized with a constant "
2719 if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r))
2720 inform (DECL_SOURCE_LOCATION (r),
2721 "%qD was not declared %<constexpr%>", r);
2723 inform (DECL_SOURCE_LOCATION (r),
2724 "%qD does not have integral or enumeration type",
2729 /* Subroutine of cxx_eval_constant_expression.
2730 Like cxx_eval_unary_expression, except for trinary expressions. */
2733 cxx_eval_trinary_expression (const constexpr_ctx *ctx, tree t,
2735 bool *non_constant_p, bool *overflow_p)
2741 for (i = 0; i < 3; i++)
2743 args[i] = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, i),
2745 non_constant_p, overflow_p);
2746 VERIFY_CONSTANT (args[i]);
2749 val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t),
2750 args[0], args[1], args[2]);
2751 if (val == NULL_TREE)
2753 VERIFY_CONSTANT (val);
2758 var_in_constexpr_fn (tree t)
2760 tree ctx = DECL_CONTEXT (t);
2761 return (cxx_dialect >= cxx14 && ctx && TREE_CODE (ctx) == FUNCTION_DECL
2762 && DECL_DECLARED_CONSTEXPR_P (ctx));
2765 /* Evaluate an INIT_EXPR or MODIFY_EXPR. */
2768 cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
2770 bool *non_constant_p, bool *overflow_p)
2772 constexpr_ctx new_ctx = *ctx;
2774 tree init = TREE_OPERAND (t, 1);
2776 /* First we figure out where we're storing to. */
2777 tree target = TREE_OPERAND (t, 0);
2778 tree type = TREE_TYPE (target);
2779 target = cxx_eval_constant_expression (ctx, target,
2781 non_constant_p, overflow_p);
2782 if (*non_constant_p)
2785 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (target), type)
2786 && is_empty_class (type))
2788 /* For initialization of an empty base, the original target will be
2789 *(base*)this, which the above evaluation resolves to the object
2790 argument, which has the derived type rather than the base type. In
2791 this situation, just evaluate the initializer and return, since
2792 there's no actual data to store. */
2793 return cxx_eval_constant_expression (ctx, init, false,
2794 non_constant_p, overflow_p);
2797 /* And then find the underlying variable. */
2798 vec<tree,va_gc> *refs = make_tree_vector();
2799 tree object = NULL_TREE;
2800 for (tree probe = target; object == NULL_TREE; )
2802 switch (TREE_CODE (probe))
2807 vec_safe_push (refs, TREE_OPERAND (probe, 1));
2808 vec_safe_push (refs, TREE_TYPE (probe));
2809 probe = TREE_OPERAND (probe, 0);
2817 /* And then find/build up our initializer for the path to the subobject
2818 we're initializing. */
2820 if (DECL_P (object))
2821 valp = ctx->values->get (object);
2826 /* A constant-expression cannot modify objects from outside the
2827 constant-expression. */
2829 error ("modification of %qE is not a constant-expression", object);
2830 *non_constant_p = true;
2833 type = TREE_TYPE (object);
2834 while (!refs->is_empty())
2836 if (*valp == NULL_TREE)
2838 *valp = build_constructor (type, NULL);
2839 CONSTRUCTOR_NO_IMPLICIT_ZERO (*valp) = true;
2842 enum tree_code code = TREE_CODE (type);
2844 tree index = refs->pop();
2846 constructor_elt *cep = NULL;
2847 if (code == ARRAY_TYPE)
2850 = find_array_ctor_elt (*valp, index, /*insert*/true);
2851 gcc_assert (i >= 0);
2852 cep = CONSTRUCTOR_ELT (*valp, i);
2853 gcc_assert (TREE_CODE (cep->index) != RANGE_EXPR);
2857 gcc_assert (TREE_CODE (index) == FIELD_DECL);
2859 /* We must keep the CONSTRUCTOR's ELTS in FIELD order.
2860 Usually we meet initializers in that order, but it is
2861 possible for base types to be placed not in program
2863 tree fields = TYPE_FIELDS (DECL_CONTEXT (index));
2864 unsigned HOST_WIDE_INT idx;
2867 vec_safe_iterate (CONSTRUCTOR_ELTS (*valp), idx, &cep);
2870 if (index == cep->index)
2873 /* The field we're initializing must be on the field
2874 list. Look to see if it is present before the
2875 field the current ELT initializes. */
2876 for (; fields != cep->index; fields = DECL_CHAIN (fields))
2877 if (index == fields)
2881 /* We fell off the end of the CONSTRUCTOR, so insert a new
2882 entry at the end. */
2885 constructor_elt ce = { index, NULL_TREE };
2887 vec_safe_insert (CONSTRUCTOR_ELTS (*valp), idx, ce);
2888 cep = CONSTRUCTOR_ELT (*valp, idx);
2894 release_tree_vector (refs);
2896 if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
2898 /* Create a new CONSTRUCTOR in case evaluation of the initializer
2899 wants to modify it. */
2900 if (*valp == NULL_TREE)
2902 *valp = new_ctx.ctor = build_constructor (type, NULL);
2903 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx.ctor) = true;
2906 new_ctx.ctor = *valp;
2907 new_ctx.object = target;
2910 init = cxx_eval_constant_expression (&new_ctx, init, false,
2911 non_constant_p, overflow_p);
2912 /* Don't share a CONSTRUCTOR that might be changed later. */
2913 init = unshare_expr (init);
2914 if (target == object)
2915 /* The hash table might have moved since the get earlier. */
2916 valp = ctx->values->get (object);
2918 if (TREE_CODE (init) == CONSTRUCTOR)
2920 /* An outer ctx->ctor might be pointing to *valp, so just replace
2922 CONSTRUCTOR_ELTS (*valp) = CONSTRUCTOR_ELTS (init);
2923 CONSTRUCTOR_NO_IMPLICIT_ZERO (*valp)
2924 = CONSTRUCTOR_NO_IMPLICIT_ZERO (init);
2929 if (*non_constant_p)
2937 /* Evaluate a ++ or -- expression. */
2940 cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t,
2942 bool *non_constant_p, bool *overflow_p)
2944 enum tree_code code = TREE_CODE (t);
2945 tree type = TREE_TYPE (t);
2946 tree op = TREE_OPERAND (t, 0);
2947 tree offset = TREE_OPERAND (t, 1);
2948 gcc_assert (TREE_CONSTANT (offset));
2950 /* The operand as an lvalue. */
2951 op = cxx_eval_constant_expression (ctx, op, true,
2952 non_constant_p, overflow_p);
2954 /* The operand as an rvalue. */
2955 tree val = rvalue (op);
2956 val = cxx_eval_constant_expression (ctx, val, false,
2957 non_constant_p, overflow_p);
2958 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
2959 a local array in a constexpr function. */
2960 bool ptr = POINTER_TYPE_P (TREE_TYPE (val));
2962 VERIFY_CONSTANT (val);
2964 /* The modified value. */
2965 bool inc = (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR);
2967 if (POINTER_TYPE_P (type))
2969 /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
2970 offset = convert_to_ptrofftype (offset);
2972 offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset);
2973 mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset);
2976 mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset);
2978 VERIFY_CONSTANT (mod);
2980 /* Storing the modified value. */
2981 tree store = build2 (MODIFY_EXPR, type, op, mod);
2982 cxx_eval_constant_expression (ctx, store,
2983 true, non_constant_p, overflow_p);
2985 /* And the value of the expression. */
2986 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
2988 /* Prefix ops are lvalues. */
2992 /* But we optimize when the caller wants an rvalue. */
2996 /* Postfix ops are rvalues. */
3000 /* Predicates for the meaning of *jump_target. */
3003 returns (tree *jump_target)
3006 && TREE_CODE (*jump_target) == RETURN_EXPR;
3010 breaks (tree *jump_target)
3013 && TREE_CODE (*jump_target) == LABEL_DECL
3014 && LABEL_DECL_BREAK (*jump_target);
3018 continues (tree *jump_target)
3021 && TREE_CODE (*jump_target) == LABEL_DECL
3022 && LABEL_DECL_CONTINUE (*jump_target);
3026 switches (tree *jump_target)
3029 && TREE_CODE (*jump_target) == INTEGER_CST;
3032 /* Subroutine of cxx_eval_statement_list. Determine whether the statement
3033 at I matches *jump_target. If we're looking for a case label and we see
3034 the default label, copy I into DEFAULT_LABEL. */
3037 label_matches (tree *jump_target, tree_stmt_iterator i,
3038 tree_stmt_iterator& default_label)
3040 tree stmt = tsi_stmt (i);
3041 switch (TREE_CODE (*jump_target))
3044 if (TREE_CODE (stmt) == LABEL_EXPR
3045 && LABEL_EXPR_LABEL (stmt) == *jump_target)
3050 if (TREE_CODE (stmt) == CASE_LABEL_EXPR)
3052 if (!CASE_LOW (stmt))
3054 else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt)))
3065 /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
3066 semantics, for switch, break, continue, and return. */
3069 cxx_eval_statement_list (const constexpr_ctx *ctx, tree t,
3070 bool *non_constant_p, bool *overflow_p,
3073 tree_stmt_iterator i;
3074 tree_stmt_iterator default_label = tree_stmt_iterator();
3076 /* In a statement-expression we want to return the last value. */
3080 local_target = NULL_TREE;
3081 jump_target = &local_target;
3083 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
3086 tree stmt = tsi_stmt (i);
3089 if (TREE_CODE (stmt) == STATEMENT_LIST)
3090 /* The label we want might be inside. */;
3091 else if (label_matches (jump_target, i, default_label))
3093 *jump_target = NULL_TREE;
3097 r = cxx_eval_constant_expression (ctx, stmt, false,
3098 non_constant_p, overflow_p,
3100 if (*non_constant_p)
3102 if (returns (jump_target) || breaks (jump_target))
3105 if (switches (jump_target) && !tsi_end_p (default_label))
3108 *jump_target = NULL_TREE;
3116 save_exprs_remover (const tree &expr, constexpr_ctx &ctx)
3118 ctx.values->remove (expr);
3123 /* Evaluate a LOOP_EXPR for side-effects. Handles break and return
3124 semantics; continue semantics are covered by cxx_eval_statement_list. */
3127 cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t,
3128 bool *non_constant_p, bool *overflow_p,
3131 constexpr_ctx new_ctx = *ctx;
3133 tree body = TREE_OPERAND (t, 0);
3136 hash_set<tree> save_exprs;
3137 new_ctx.save_exprs = &save_exprs;
3139 cxx_eval_statement_list (&new_ctx, body,
3140 non_constant_p, overflow_p, jump_target);
3142 /* Forget saved values of SAVE_EXPRs. */
3143 save_exprs.traverse<constexpr_ctx &, save_exprs_remover>(new_ctx);
3145 while (!returns (jump_target) && !breaks (jump_target) && !*non_constant_p);
3147 if (breaks (jump_target))
3148 *jump_target = NULL_TREE;
3153 /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
3157 cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t,
3158 bool *non_constant_p, bool *overflow_p,
3161 tree cond = TREE_OPERAND (t, 0);
3162 cond = cxx_eval_constant_expression (ctx, cond, false,
3163 non_constant_p, overflow_p);
3164 VERIFY_CONSTANT (cond);
3165 *jump_target = cond;
3167 tree body = TREE_OPERAND (t, 1);
3168 cxx_eval_statement_list (ctx, body,
3169 non_constant_p, overflow_p, jump_target);
3170 if (breaks (jump_target) || switches (jump_target))
3171 *jump_target = NULL_TREE;
3175 /* Subroutine of cxx_eval_constant_expression.
3176 Attempt to reduce a POINTER_PLUS_EXPR expression T. */
3179 cxx_eval_pointer_plus_expression (const constexpr_ctx *ctx, tree t,
3180 bool lval, bool *non_constant_p,
3183 tree orig_type = TREE_TYPE (t);
3184 tree op00 = TREE_OPERAND (t, 0);
3185 tree op01 = TREE_OPERAND (t, 1);
3186 location_t loc = EXPR_LOCATION (t);
3188 op00 = cxx_eval_constant_expression (ctx, op00, lval,
3189 non_constant_p, overflow_p);
3192 if (TREE_CODE (op00) != ADDR_EXPR)
3195 op00 = TREE_OPERAND (op00, 0);
3197 /* &A[i] p+ j => &A[i + j] */
3198 if (TREE_CODE (op00) == ARRAY_REF
3199 && TREE_CODE (TREE_OPERAND (op00, 1)) == INTEGER_CST
3200 && TREE_CODE (op01) == INTEGER_CST
3201 && TYPE_SIZE_UNIT (TREE_TYPE (op00))
3202 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (op00))) == INTEGER_CST)
3204 tree type = TREE_TYPE (op00);
3205 t = fold_convert_loc (loc, ssizetype, TREE_OPERAND (op00, 1));
3206 tree nelts = array_type_nelts_top (TREE_TYPE (TREE_OPERAND (op00, 0)));
3207 /* Don't fold an out-of-bound access. */
3208 if (!tree_int_cst_le (t, nelts))
3210 op01 = cp_fold_convert (ssizetype, op01);
3211 /* Don't fold if op01 can't be divided exactly by TYPE_SIZE_UNIT.
3212 constexpr int A[1]; ... (char *)&A[0] + 1 */
3213 if (!integer_zerop (fold_build2_loc (loc, TRUNC_MOD_EXPR, sizetype,
3214 op01, TYPE_SIZE_UNIT (type))))
3216 /* Make sure to treat the second operand of POINTER_PLUS_EXPR
3218 op01 = fold_build2_loc (loc, EXACT_DIV_EXPR, ssizetype, op01,
3219 TYPE_SIZE_UNIT (type));
3220 t = size_binop_loc (loc, PLUS_EXPR, op01, t);
3221 t = build4_loc (loc, ARRAY_REF, type, TREE_OPERAND (op00, 0),
3222 t, NULL_TREE, NULL_TREE);
3223 t = cp_build_addr_expr (t, tf_warning_or_error);
3224 t = cp_fold_convert (orig_type, t);
3225 return cxx_eval_constant_expression (ctx, t, lval, non_constant_p,
3232 /* Attempt to reduce the expression T to a constant value.
3233 On failure, issue diagnostic and return error_mark_node. */
3234 /* FIXME unify with c_fully_fold */
3235 /* FIXME overflow_p is too global */
3238 cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
3240 bool *non_constant_p, bool *overflow_p,
3243 constexpr_ctx new_ctx;
3246 if (t == error_mark_node)
3248 *non_constant_p = true;
3251 if (CONSTANT_CLASS_P (t))
3253 if (TREE_CODE (t) == PTRMEM_CST)
3254 t = cplus_expand_constant (t);
3255 else if (TREE_OVERFLOW (t) && (!flag_permissive || ctx->quiet))
3260 switch (TREE_CODE (t))
3265 /* We ask for an rvalue for the RESULT_DECL when indirecting
3266 through an invisible reference, or in named return value
3268 return (*ctx->values->get (t));
3272 /* We used to not check lval for CONST_DECL, but darwin.c uses
3273 CONST_DECL for aggregate constants. */
3277 r = decl_really_constant_value (t);
3279 r = decl_constant_value (t);
3280 if (TREE_CODE (r) == TARGET_EXPR
3281 && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR)
3282 r = TARGET_EXPR_INITIAL (r);
3283 if (TREE_CODE (r) == VAR_DECL)
3284 if (tree *p = ctx->values->get (r))
3285 if (*p != NULL_TREE)
3290 non_const_var_error (r);
3291 *non_constant_p = true;
3299 case CASE_LABEL_EXPR:
3303 if (!use_new_call && ctx
3304 && ctx->call && DECL_CONTEXT (t) == ctx->call->fundef->decl)
3305 r = lookup_parameter_binding (ctx->call, t);
3306 else if (lval && TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
3308 else if (tree *p = ctx->values->get (r))
3311 /* Defer in case this is only used for its type. */;
3312 else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
3313 /* Defer, there's no lvalue->rvalue conversion. */;
3314 else if (is_empty_class (TREE_TYPE (t)))
3316 /* If the class is empty, we aren't actually loading anything. */
3317 r = build_constructor (TREE_TYPE (t), NULL);
3318 TREE_CONSTANT (r) = true;
3323 error ("%qE is not a constant expression", t);
3324 *non_constant_p = true;
3329 case AGGR_INIT_EXPR:
3330 r = cxx_eval_call_expression (ctx, t, lval,
3331 non_constant_p, overflow_p);
3336 r = DECL_EXPR_DECL (t);
3337 if (AGGREGATE_TYPE_P (TREE_TYPE (r))
3338 || VECTOR_TYPE_P (TREE_TYPE (r)))
3342 new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL);
3343 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx.ctor) = true;
3344 new_ctx.values->put (r, new_ctx.ctor);
3348 if (tree init = DECL_INITIAL (r))
3350 init = cxx_eval_constant_expression (ctx, init,
3352 non_constant_p, overflow_p);
3353 /* Don't share a CONSTRUCTOR that might be changed. */
3354 init = unshare_expr (init);
3355 ctx->values->put (r, init);
3357 else if (ctx == &new_ctx)
3358 /* We gave it a CONSTRUCTOR above. */;
3360 ctx->values->put (r, NULL_TREE);
3365 if (!literal_type_p (TREE_TYPE (t)))
3369 error ("temporary of non-literal type %qT in a "
3370 "constant expression", TREE_TYPE (t));
3371 explain_non_literal_class (TREE_TYPE (t));
3373 *non_constant_p = true;
3376 if ((AGGREGATE_TYPE_P (TREE_TYPE (t)) || VECTOR_TYPE_P (TREE_TYPE (t))))
3378 /* We're being expanded without an explicit target, so start
3379 initializing a new object; expansion with an explicit target
3380 strips the TARGET_EXPR before we get here. */
3382 new_ctx.ctor = build_constructor (TREE_TYPE (t), NULL);
3383 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx.ctor) = true;
3384 new_ctx.object = TARGET_EXPR_SLOT (t);
3385 ctx->values->put (new_ctx.object, new_ctx.ctor);
3388 /* Pass false for 'lval' because this indicates
3389 initialization of a temporary. */
3390 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
3392 non_constant_p, overflow_p);
3393 if (!*non_constant_p)
3394 /* Adjust the type of the result to the type of the temporary. */
3395 r = adjust_temp_type (TREE_TYPE (t), r);
3398 tree slot = TARGET_EXPR_SLOT (t);
3399 r = unshare_expr (r);
3400 ctx->values->put (slot, r);
3408 /* In C++11 constexpr evaluation we are looking for the value,
3409 not the side-effect of the initialization. */
3410 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
3412 non_constant_p, overflow_p);
3415 /* else fall through */
3417 r = cxx_eval_store_expression (ctx, t, lval,
3418 non_constant_p, overflow_p);
3422 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
3424 non_constant_p, overflow_p);
3428 if (TREE_OPERAND (t, 0) != NULL_TREE)
3429 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
3431 non_constant_p, overflow_p);
3436 /* Avoid evaluating a SAVE_EXPR more than once. */
3437 if (tree *p = ctx->values->get (t))
3441 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), false,
3442 non_constant_p, overflow_p);
3443 ctx->values->put (t, r);
3444 if (ctx->save_exprs)
3445 ctx->save_exprs->add (t);
3449 case NON_LVALUE_EXPR:
3450 case TRY_CATCH_EXPR:
3452 case CLEANUP_POINT_EXPR:
3453 case MUST_NOT_THROW_EXPR:
3456 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
3458 non_constant_p, overflow_p,
3462 case TRY_FINALLY_EXPR:
3463 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
3464 non_constant_p, overflow_p,
3466 if (!*non_constant_p)
3467 /* Also evaluate the cleanup. */
3468 cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), true,
3469 non_constant_p, overflow_p,
3473 /* These differ from cxx_eval_unary_expression in that this doesn't
3474 check for a constant operand or result; an address can be
3475 constant without its operand being, and vice versa. */
3477 r = cxx_eval_indirect_ref (ctx, t, lval,
3478 non_constant_p, overflow_p);
3483 tree oldop = TREE_OPERAND (t, 0);
3484 tree op = cxx_eval_constant_expression (ctx, oldop,
3486 non_constant_p, overflow_p);
3487 /* Don't VERIFY_CONSTANT here. */
3488 if (*non_constant_p)
3490 gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR);
3491 /* This function does more aggressive folding than fold itself. */
3492 r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
3493 if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
3501 case FIX_TRUNC_EXPR:
3506 case TRUTH_NOT_EXPR:
3507 case FIXED_CONVERT_EXPR:
3508 r = cxx_eval_unary_expression (ctx, t, lval,
3509 non_constant_p, overflow_p);
3513 if (SIZEOF_EXPR_TYPE_P (t))
3514 r = cxx_sizeof_or_alignof_type (TREE_TYPE (TREE_OPERAND (t, 0)),
3515 SIZEOF_EXPR, false);
3516 else if (TYPE_P (TREE_OPERAND (t, 0)))
3517 r = cxx_sizeof_or_alignof_type (TREE_OPERAND (t, 0), SIZEOF_EXPR,
3520 r = cxx_sizeof_or_alignof_expr (TREE_OPERAND (t, 0), SIZEOF_EXPR,
3522 if (r == error_mark_node)
3524 VERIFY_CONSTANT (r);
3529 /* check_return_expr sometimes wraps a TARGET_EXPR in a
3530 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
3531 introduced by build_call_a. */
3532 tree op0 = TREE_OPERAND (t, 0);
3533 tree op1 = TREE_OPERAND (t, 1);
3535 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
3536 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
3537 r = cxx_eval_constant_expression (ctx, op0,
3538 lval, non_constant_p, overflow_p,
3542 /* Check that the LHS is constant and then discard it. */
3543 cxx_eval_constant_expression (ctx, op0,
3544 true, non_constant_p, overflow_p,
3546 op1 = TREE_OPERAND (t, 1);
3547 r = cxx_eval_constant_expression (ctx, op1,
3548 lval, non_constant_p, overflow_p,
3554 case POINTER_PLUS_EXPR:
3555 r = cxx_eval_pointer_plus_expression (ctx, t, lval, non_constant_p,
3559 /* else fall through */
3564 case TRUNC_DIV_EXPR:
3566 case FLOOR_DIV_EXPR:
3567 case ROUND_DIV_EXPR:
3568 case TRUNC_MOD_EXPR:
3570 case ROUND_MOD_EXPR:
3572 case EXACT_DIV_EXPR:
3582 case TRUTH_XOR_EXPR:
3589 case UNORDERED_EXPR:
3599 r = cxx_eval_binary_expression (ctx, t, lval,
3600 non_constant_p, overflow_p);
3603 /* fold can introduce non-IF versions of these; still treat them as
3604 short-circuiting. */
3605 case TRUTH_AND_EXPR:
3606 case TRUTH_ANDIF_EXPR:
3607 r = cxx_eval_logical_expression (ctx, t, boolean_false_node,
3610 non_constant_p, overflow_p);
3614 case TRUTH_ORIF_EXPR:
3615 r = cxx_eval_logical_expression (ctx, t, boolean_true_node,
3618 non_constant_p, overflow_p);
3622 r = cxx_eval_array_reference (ctx, t, lval,
3623 non_constant_p, overflow_p);
3627 if (is_overloaded_fn (t))
3629 /* We can only get here in checking mode via
3630 build_non_dependent_expr, because any expression that
3631 calls or takes the address of the function will have
3632 pulled a FUNCTION_DECL out of the COMPONENT_REF. */
3633 gcc_checking_assert (ctx->quiet || errorcount);
3634 *non_constant_p = true;
3637 r = cxx_eval_component_reference (ctx, t, lval,
3638 non_constant_p, overflow_p);
3642 r = cxx_eval_bit_field_ref (ctx, t, lval,
3643 non_constant_p, overflow_p);
3648 r = cxx_eval_conditional_expression (ctx, t, lval,
3649 non_constant_p, overflow_p,
3654 if (TREE_CONSTANT (t))
3655 /* Don't re-process a constant CONSTRUCTOR, but do fold it to
3656 VECTOR_CST if applicable. */
3658 r = cxx_eval_bare_aggregate (ctx, t, lval,
3659 non_constant_p, overflow_p);
3663 /* We can get this in a defaulted constructor for a class with a
3664 non-static data member of array type. Either the initializer will
3665 be NULL, meaning default-initialization, or it will be an lvalue
3666 or xvalue of the same type, meaning direct-initialization from the
3667 corresponding member. */
3668 r = cxx_eval_vec_init (ctx, t, lval,
3669 non_constant_p, overflow_p);
3674 r = cxx_eval_trinary_expression (ctx, t, lval,
3675 non_constant_p, overflow_p);
3679 case VIEW_CONVERT_EXPR:
3682 tree oldop = TREE_OPERAND (t, 0);
3683 tree op = cxx_eval_constant_expression (ctx, oldop,
3685 non_constant_p, overflow_p);
3686 if (*non_constant_p)
3688 if (POINTER_TYPE_P (TREE_TYPE (t))
3689 && TREE_CODE (op) == INTEGER_CST
3690 && !integer_zerop (op))
3693 error_at (EXPR_LOC_OR_LOC (t, input_location),
3694 "reinterpret_cast from integer to pointer");
3695 *non_constant_p = true;
3699 /* We didn't fold at the top so we could check for ptr-int
3702 r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), op);
3703 /* Conversion of an out-of-range value has implementation-defined
3704 behavior; the language considers it different from arithmetic
3705 overflow, which is undefined. */
3706 if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op))
3707 TREE_OVERFLOW (r) = false;
3711 case EMPTY_CLASS_EXPR:
3712 /* This is good enough for a function argument that might not get
3713 used, and they can't do anything with it, so just return it. */
3716 case STATEMENT_LIST:
3718 new_ctx.ctor = new_ctx.object = NULL_TREE;
3719 return cxx_eval_statement_list (&new_ctx, t,
3720 non_constant_p, overflow_p, jump_target);
3723 return cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t),
3725 non_constant_p, overflow_p,
3728 case PREINCREMENT_EXPR:
3729 case POSTINCREMENT_EXPR:
3730 case PREDECREMENT_EXPR:
3731 case POSTDECREMENT_EXPR:
3732 return cxx_eval_increment_expression (ctx, t,
3733 lval, non_constant_p, overflow_p);
3739 case VEC_DELETE_EXPR:
3742 /* GCC internal stuff. */
3745 case WITH_CLEANUP_EXPR:
3746 case NON_DEPENDENT_EXPR:
3750 error_at (EXPR_LOC_OR_LOC (t, input_location),
3751 "expression %qE is not a constant-expression", t);
3752 *non_constant_p = true;
3755 case PLACEHOLDER_EXPR:
3756 if (!ctx || !ctx->ctor || (lval && !ctx->object)
3757 || !(same_type_ignoring_top_level_qualifiers_p
3758 (TREE_TYPE (t), TREE_TYPE (ctx->ctor))))
3760 /* A placeholder without a referent. We can get here when
3761 checking whether NSDMIs are noexcept, or in massage_init_elt;
3762 just say it's non-constant for now. */
3763 gcc_assert (ctx->quiet);
3764 *non_constant_p = true;
3769 /* Use of the value or address of the current object. We could
3770 use ctx->object unconditionally, but using ctx->ctor when we
3771 can is a minor optimization. */
3772 tree ctor = lval ? ctx->object : ctx->ctor;
3773 return cxx_eval_constant_expression
3775 non_constant_p, overflow_p);
3780 *jump_target = TREE_OPERAND (t, 0);
3781 gcc_assert (breaks (jump_target) || continues (jump_target));
3785 cxx_eval_loop_expr (ctx, t,
3786 non_constant_p, overflow_p, jump_target);
3790 cxx_eval_switch_expr (ctx, t,
3791 non_constant_p, overflow_p, jump_target);
3795 if (STATEMENT_CODE_P (TREE_CODE (t)))
3797 /* This function doesn't know how to deal with pre-genericize
3798 statements; this can only happen with statement-expressions,
3799 so for now just fail. */
3801 error_at (EXPR_LOCATION (t),
3802 "statement is not a constant-expression");
3805 internal_error ("unexpected expression %qE of kind %s", t,
3806 get_tree_code_name (TREE_CODE (t)));
3807 *non_constant_p = true;
3811 if (r == error_mark_node)
3812 *non_constant_p = true;
3814 if (*non_constant_p)
3821 cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant,
3822 bool strict = true, tree object = NULL_TREE)
3824 bool non_constant_p = false;
3825 bool overflow_p = false;
3826 hash_map<tree,tree> map;
3828 constexpr_ctx ctx = { NULL, &map, NULL, NULL, NULL,
3829 allow_non_constant, strict };
3831 tree type = initialized_type (t);
3833 if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
3835 /* In C++14 an NSDMI can participate in aggregate initialization,
3836 and can refer to the address of the object being initialized, so
3837 we need to pass in the relevant VAR_DECL if we want to do the
3838 evaluation in a single pass. The evaluation will dynamically
3839 update ctx.values for the VAR_DECL. We use the same strategy
3840 for C++11 constexpr constructors that refer to the object being
3842 ctx.ctor = build_constructor (type, NULL);
3843 CONSTRUCTOR_NO_IMPLICIT_ZERO (ctx.ctor) = true;
3846 if (TREE_CODE (t) == TARGET_EXPR)
3847 object = TARGET_EXPR_SLOT (t);
3848 else if (TREE_CODE (t) == AGGR_INIT_EXPR)
3849 object = AGGR_INIT_EXPR_SLOT (t);
3851 ctx.object = object;
3853 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3854 (type, TREE_TYPE (object)));
3855 if (object && DECL_P (object))
3856 map.put (object, ctx.ctor);
3857 if (TREE_CODE (r) == TARGET_EXPR)
3858 /* Avoid creating another CONSTRUCTOR when we expand the
3860 r = TARGET_EXPR_INITIAL (r);
3863 r = cxx_eval_constant_expression (&ctx, r,
3864 false, &non_constant_p, &overflow_p);
3866 verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p);
3868 /* Mutable logic is a bit tricky: we want to allow initialization of
3869 constexpr variables with mutable members, but we can't copy those
3870 members to another constexpr variable. */
3871 if (TREE_CODE (r) == CONSTRUCTOR
3872 && CONSTRUCTOR_MUTABLE_POISON (r))
3874 if (!allow_non_constant)
3875 error ("%qE is not a constant expression because it refers to "
3876 "mutable subobjects of %qT", t, type);
3877 non_constant_p = true;
3880 /* Technically we should check this for all subexpressions, but that
3881 runs into problems with our internal representation of pointer
3882 subtraction and the 5.19 rules are still in flux. */
3883 if (CONVERT_EXPR_CODE_P (TREE_CODE (r))
3884 && ARITHMETIC_TYPE_P (TREE_TYPE (r))
3885 && TREE_CODE (TREE_OPERAND (r, 0)) == ADDR_EXPR)
3887 if (!allow_non_constant)
3888 error ("conversion from pointer type %qT "
3889 "to arithmetic type %qT in a constant-expression",
3890 TREE_TYPE (TREE_OPERAND (r, 0)), TREE_TYPE (r));
3891 non_constant_p = true;
3894 if (!non_constant_p && overflow_p)
3895 non_constant_p = true;
3897 if (non_constant_p && !allow_non_constant)
3898 return error_mark_node;
3899 else if (non_constant_p && TREE_CONSTANT (r))
3901 /* This isn't actually constant, so unset TREE_CONSTANT. */
3904 else if (TREE_CODE (r) == CONSTRUCTOR)
3905 r = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (r), r);
3907 r = build_nop (TREE_TYPE (r), r);
3908 TREE_CONSTANT (r) = false;
3910 else if (non_constant_p || r == t)
3913 if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
3915 if (TREE_CODE (t) == TARGET_EXPR
3916 && TARGET_EXPR_INITIAL (t) == r)
3920 r = get_target_expr (r);
3921 TREE_CONSTANT (r) = true;
3929 /* Returns true if T is a valid subexpression of a constant expression,
3930 even if it isn't itself a constant expression. */
3933 is_sub_constant_expr (tree t)
3935 bool non_constant_p = false;
3936 bool overflow_p = false;
3937 hash_map <tree, tree> map;
3939 constexpr_ctx ctx = { NULL, &map, NULL, NULL, NULL, true, true };
3941 cxx_eval_constant_expression (&ctx, t, false, &non_constant_p,
3943 return !non_constant_p && !overflow_p;
3946 /* If T represents a constant expression returns its reduced value.
3947 Otherwise return error_mark_node. If T is dependent, then
3951 cxx_constant_value (tree t, tree decl)
3953 return cxx_eval_outermost_constant_expr (t, false, true, decl);
3956 /* If T is a constant expression, returns its reduced value.
3957 Otherwise, if T does not have TREE_CONSTANT set, returns T.
3958 Otherwise, returns a version of T without TREE_CONSTANT. */
3961 maybe_constant_value (tree t, tree decl)
3965 if (instantiation_dependent_expression_p (t)
3966 || type_unknown_p (t)
3967 || BRACE_ENCLOSED_INITIALIZER_P (t)
3968 || !potential_constant_expression (t))
3970 if (TREE_OVERFLOW_P (t))
3972 t = build_nop (TREE_TYPE (t), t);
3973 TREE_CONSTANT (t) = false;
3978 r = cxx_eval_outermost_constant_expr (t, true, true, decl);
3979 #ifdef ENABLE_CHECKING
3981 || CONVERT_EXPR_P (t)
3982 || TREE_CODE (t) == VIEW_CONVERT_EXPR
3983 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
3984 || !cp_tree_equal (r, t));
3989 /* Like maybe_constant_value but first fully instantiate the argument.
3991 Note: this is equivalent to instantiate_non_dependent_expr_sfinae
3992 (t, tf_none) followed by maybe_constant_value but is more efficient,
3993 because calls instantiation_dependent_expression_p and
3994 potential_constant_expression at most once. */
3997 fold_non_dependent_expr (tree t)
4002 /* If we're in a template, but T isn't value dependent, simplify
4003 it. We're supposed to treat:
4005 template <typename T> void f(T[1 + 1]);
4006 template <typename T> void f(T[2]);
4008 as two declarations of the same function, for example. */
4009 if (processing_template_decl)
4011 if (!instantiation_dependent_expression_p (t)
4012 && potential_constant_expression (t))
4014 processing_template_decl_sentinel s;
4015 t = instantiate_non_dependent_expr_internal (t, tf_none);
4017 if (type_unknown_p (t)
4018 || BRACE_ENCLOSED_INITIALIZER_P (t))
4020 if (TREE_OVERFLOW_P (t))
4022 t = build_nop (TREE_TYPE (t), t);
4023 TREE_CONSTANT (t) = false;
4028 tree r = cxx_eval_outermost_constant_expr (t, true, true, NULL_TREE);
4029 #ifdef ENABLE_CHECKING
4030 /* cp_tree_equal looks through NOPs, so allow them. */
4032 || CONVERT_EXPR_P (t)
4033 || TREE_CODE (t) == VIEW_CONVERT_EXPR
4034 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
4035 || !cp_tree_equal (r, t));
4039 else if (TREE_OVERFLOW_P (t))
4041 t = build_nop (TREE_TYPE (t), t);
4042 TREE_CONSTANT (t) = false;
4047 return maybe_constant_value (t);
4050 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
4051 than wrapped in a TARGET_EXPR. */
4054 maybe_constant_init (tree t, tree decl)
4056 if (TREE_CODE (t) == EXPR_STMT)
4057 t = TREE_OPERAND (t, 0);
4058 if (TREE_CODE (t) == CONVERT_EXPR
4059 && VOID_TYPE_P (TREE_TYPE (t)))
4060 t = TREE_OPERAND (t, 0);
4061 if (TREE_CODE (t) == INIT_EXPR)
4062 t = TREE_OPERAND (t, 1);
4063 if (instantiation_dependent_expression_p (t)
4064 || type_unknown_p (t)
4065 || BRACE_ENCLOSED_INITIALIZER_P (t)
4066 || !potential_static_init_expression (t))
4067 /* Don't try to evaluate it. */;
4069 t = cxx_eval_outermost_constant_expr (t, true, false, decl);
4070 if (TREE_CODE (t) == TARGET_EXPR)
4072 tree init = TARGET_EXPR_INITIAL (t);
4073 if (TREE_CODE (init) == CONSTRUCTOR)
4080 /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
4081 /* Return true if the object referred to by REF has automatic or thread
4084 enum { ck_ok, ck_bad, ck_unknown };
4086 check_automatic_or_tls (tree ref)
4089 HOST_WIDE_INT bitsize, bitpos;
4091 int volatilep = 0, unsignedp = 0;
4092 tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset,
4093 &mode, &unsignedp, &volatilep, false);
4096 /* If there isn't a decl in the middle, we don't know the linkage here,
4097 and this isn't a constant expression anyway. */
4100 dk = decl_storage_duration (decl);
4101 return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok;
4105 /* Return true if T denotes a potentially constant expression. Issue
4106 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
4107 an lvalue-rvalue conversion is implied.
4109 C++0x [expr.const] used to say
4111 6 An expression is a potential constant expression if it is
4112 a constant expression where all occurrences of function
4113 parameters are replaced by arbitrary constant expressions
4114 of the appropriate type.
4116 2 A conditional expression is a constant expression unless it
4117 involves one of the following as a potentially evaluated
4118 subexpression (3.2), but subexpressions of logical AND (5.14),
4119 logical OR (5.15), and conditional (5.16) operations that are
4120 not evaluated are not considered. */
4123 potential_constant_expression_1 (tree t, bool want_rval, bool strict,
4124 tsubst_flags_t flags)
4126 #define RECUR(T,RV) potential_constant_expression_1 ((T), (RV), strict, flags)
4127 enum { any = false, rval = true };
4131 if (t == error_mark_node)
4135 if (TREE_THIS_VOLATILE (t))
4137 if (flags & tf_error)
4138 error ("expression %qE has side-effects", t);
4141 if (CONSTANT_CLASS_P (t))
4144 switch (TREE_CODE (t))
4150 case TEMPLATE_ID_EXPR:
4153 case CASE_LABEL_EXPR:
4159 case TEMPLATE_PARM_INDEX:
4161 case IDENTIFIER_NODE:
4162 case USERDEF_LITERAL:
4163 /* We can see a FIELD_DECL in a pointer-to-member expression. */
4168 case PLACEHOLDER_EXPR:
4173 case AGGR_INIT_EXPR:
4175 /* -- an invocation of a function other than a constexpr function
4176 or a constexpr constructor. */
4178 tree fun = get_function_named_in_call (t);
4179 const int nargs = call_expr_nargs (t);
4182 if (fun == NULL_TREE)
4184 if (TREE_CODE (t) == CALL_EXPR
4185 && CALL_EXPR_FN (t) == NULL_TREE)
4186 switch (CALL_EXPR_IFN (t))
4188 /* These should be ignored, they are optimized away from
4189 constexpr functions. */
4190 case IFN_UBSAN_NULL:
4191 case IFN_UBSAN_BOUNDS:
4192 case IFN_UBSAN_VPTR:
4197 /* fold_call_expr can't do anything with IFN calls. */
4198 if (flags & tf_error)
4199 error_at (EXPR_LOC_OR_LOC (t, input_location),
4200 "call to internal function");
4203 if (is_overloaded_fn (fun))
4205 if (TREE_CODE (fun) == FUNCTION_DECL)
4207 if (builtin_valid_in_constant_expr_p (fun))
4209 if (!DECL_DECLARED_CONSTEXPR_P (fun)
4210 /* Allow any built-in function; if the expansion
4211 isn't constant, we'll deal with that then. */
4212 && !is_builtin_fn (fun))
4214 if (flags & tf_error)
4216 error_at (EXPR_LOC_OR_LOC (t, input_location),
4217 "call to non-constexpr function %qD", fun);
4218 explain_invalid_constexpr_fn (fun);
4222 /* A call to a non-static member function takes the address
4223 of the object as the first argument. But in a constant
4224 expression the address will be folded away, so look
4226 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
4227 && !DECL_CONSTRUCTOR_P (fun))
4229 tree x = get_nth_callarg (t, 0);
4230 if (is_this_parameter (x))
4232 else if (!RECUR (x, rval))
4239 if (!RECUR (fun, true))
4241 fun = get_first_fn (fun);
4243 /* Skip initial arguments to base constructors. */
4244 if (DECL_BASE_CONSTRUCTOR_P (fun))
4245 i = num_artificial_parms_for (fun);
4246 fun = DECL_ORIGIN (fun);
4250 if (RECUR (fun, rval))
4251 /* Might end up being a constant function pointer. */;
4255 for (; i < nargs; ++i)
4257 tree x = get_nth_callarg (t, i);
4258 /* In a template, reference arguments haven't been converted to
4259 REFERENCE_TYPE and we might not even know if the parameter
4260 is a reference, so accept lvalue constants too. */
4261 bool rv = processing_template_decl ? any : rval;
4268 case NON_LVALUE_EXPR:
4269 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
4270 -- an lvalue of integral type that refers to a non-volatile
4271 const variable or static data member initialized with
4272 constant expressions, or
4274 -- an lvalue of literal type that refers to non-volatile
4275 object defined with constexpr, or that refers to a
4276 sub-object of such an object; */
4277 return RECUR (TREE_OPERAND (t, 0), rval);
4281 && !decl_constant_var_p (t)
4283 || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t))
4284 || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t))
4285 && !var_in_constexpr_fn (t)
4286 && !type_dependent_expression_p (t))
4288 if (flags & tf_error)
4289 non_const_var_error (t);
4296 case VIEW_CONVERT_EXPR:
4297 /* -- a reinterpret_cast. FIXME not implemented, and this rule
4298 may change to something more specific to type-punning (DR 1312). */
4300 tree from = TREE_OPERAND (t, 0);
4301 if (POINTER_TYPE_P (TREE_TYPE (t))
4302 && TREE_CODE (from) == INTEGER_CST
4303 && !integer_zerop (from))
4305 if (flags & tf_error)
4306 error_at (EXPR_LOC_OR_LOC (t, input_location),
4307 "reinterpret_cast from integer to pointer");
4310 return (RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR));
4314 /* -- a unary operator & that is applied to an lvalue that
4315 designates an object with thread or automatic storage
4317 t = TREE_OPERAND (t, 0);
4319 if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t))
4320 /* A pointer-to-member constant. */
4324 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
4325 any checking here, as we might dereference the pointer later. If
4326 we remove this code, also remove check_automatic_or_tls. */
4327 i = check_automatic_or_tls (t);
4332 if (flags & tf_error)
4333 error ("address-of an object %qE with thread local or "
4334 "automatic storage is not a constant expression", t);
4338 return RECUR (t, any);
4344 /* -- a class member access unless its postfix-expression is
4345 of literal type or of pointer to literal type. */
4346 /* This test would be redundant, as it follows from the
4347 postfix-expression being a potential constant expression. */
4348 return RECUR (TREE_OPERAND (t, 0), want_rval);
4350 case EXPR_PACK_EXPANSION:
4351 return RECUR (PACK_EXPANSION_PATTERN (t), want_rval);
4355 tree x = TREE_OPERAND (t, 0);
4357 if (is_this_parameter (x))
4359 if (DECL_CONTEXT (x)
4360 && !DECL_DECLARED_CONSTEXPR_P (DECL_CONTEXT (x)))
4362 if (flags & tf_error)
4363 error ("use of %<this%> in a constant expression");
4368 return RECUR (x, rval);
4371 case STATEMENT_LIST:
4373 tree_stmt_iterator i;
4374 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
4376 if (!RECUR (tsi_stmt (i), any))
4384 if (cxx_dialect < cxx14)
4386 if (!RECUR (TREE_OPERAND (t, 0), any))
4388 if (!RECUR (TREE_OPERAND (t, 1), rval))
4393 if (cxx_dialect < cxx14)
4395 if (!RECUR (TREE_OPERAND (t, 0), rval))
4397 if (!RECUR (TREE_OPERAND (t, 2), rval))
4402 if (!RECUR (IF_COND (t), rval))
4404 if (!RECUR (THEN_CLAUSE (t), any))
4406 if (!RECUR (ELSE_CLAUSE (t), any))
4411 if (!RECUR (DO_COND (t), rval))
4413 if (!RECUR (DO_BODY (t), any))
4418 if (!RECUR (FOR_INIT_STMT (t), any))
4420 if (!RECUR (FOR_COND (t), rval))
4422 if (!RECUR (FOR_EXPR (t), any))
4424 if (!RECUR (FOR_BODY (t), any))
4429 if (!RECUR (WHILE_COND (t), rval))
4431 if (!RECUR (WHILE_BODY (t), any))
4436 if (!RECUR (SWITCH_STMT_COND (t), rval))
4438 if (!RECUR (SWITCH_STMT_BODY (t), any))
4443 return RECUR (STMT_EXPR_STMT (t), rval);
4446 case DYNAMIC_CAST_EXPR:
4447 case PSEUDO_DTOR_EXPR:
4451 case VEC_DELETE_EXPR:
4454 case OMP_ATOMIC_READ:
4455 case OMP_ATOMIC_CAPTURE_OLD:
4456 case OMP_ATOMIC_CAPTURE_NEW:
4457 /* GCC internal stuff. */
4460 case TRANSACTION_EXPR:
4463 if (flags & tf_error)
4464 error ("expression %qE is not a constant-expression", t);
4468 /* -- a typeid expression whose operand is of polymorphic
4471 tree e = TREE_OPERAND (t, 0);
4472 if (!TYPE_P (e) && !type_dependent_expression_p (e)
4473 && TYPE_POLYMORPHIC_P (TREE_TYPE (e)))
4475 if (flags & tf_error)
4476 error ("typeid-expression is not a constant expression "
4477 "because %qE is of polymorphic type", e);
4484 /* -- a subtraction where both operands are pointers. */
4485 if (TYPE_PTR_P (TREE_OPERAND (t, 0))
4486 && TYPE_PTR_P (TREE_OPERAND (t, 1)))
4488 if (flags & tf_error)
4489 error ("difference of two pointer expressions is not "
4490 "a constant expression");
4502 /* -- a relational or equality operator where at least
4503 one of the operands is a pointer. */
4504 if (TYPE_PTR_P (TREE_OPERAND (t, 0))
4505 || TYPE_PTR_P (TREE_OPERAND (t, 1)))
4507 if (flags & tf_error)
4508 error ("pointer comparison expression is not a "
4509 "constant expression");
4515 case PREINCREMENT_EXPR:
4516 case POSTINCREMENT_EXPR:
4517 case PREDECREMENT_EXPR:
4518 case POSTDECREMENT_EXPR:
4519 if (cxx_dialect < cxx14)
4525 if (TYPE_P (TREE_OPERAND (t, 0)))
4527 /* else fall through. */
4533 case FIX_TRUNC_EXPR:
4537 case TRUTH_NOT_EXPR:
4538 case FIXED_CONVERT_EXPR:
4539 case UNARY_PLUS_EXPR:
4541 return RECUR (TREE_OPERAND (t, 0), rval);
4544 case CONST_CAST_EXPR:
4545 case STATIC_CAST_EXPR:
4546 case REINTERPRET_CAST_EXPR:
4547 case IMPLICIT_CONV_EXPR:
4548 if (cxx_dialect < cxx11
4549 && !dependent_type_p (TREE_TYPE (t))
4550 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t)))
4551 /* In C++98, a conversion to non-integral type can't be part of a
4552 constant expression. */
4554 if (flags & tf_error)
4555 error ("cast to non-integral type %qT in a constant expression",
4560 return (RECUR (TREE_OPERAND (t, 0),
4561 TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE));
4564 return RECUR (BIND_EXPR_BODY (t), want_rval);
4566 case WITH_CLEANUP_EXPR:
4567 case CLEANUP_POINT_EXPR:
4568 case MUST_NOT_THROW_EXPR:
4569 case TRY_CATCH_EXPR:
4575 case NON_DEPENDENT_EXPR:
4576 /* For convenience. */
4578 return RECUR (TREE_OPERAND (t, 0), want_rval);
4580 case TRY_FINALLY_EXPR:
4581 return (RECUR (TREE_OPERAND (t, 0), want_rval)
4582 && RECUR (TREE_OPERAND (t, 1), any));
4585 return RECUR (TREE_OPERAND (t, 1), want_rval);
4588 if (!literal_type_p (TREE_TYPE (t)))
4590 if (flags & tf_error)
4592 error ("temporary of non-literal type %qT in a "
4593 "constant expression", TREE_TYPE (t));
4594 explain_non_literal_class (TREE_TYPE (t));
4599 return RECUR (TREE_OPERAND (t, 1), rval);
4603 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
4604 constructor_elt *ce;
4605 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
4606 if (!RECUR (ce->value, want_rval))
4613 gcc_assert (TREE_PURPOSE (t) == NULL_TREE
4614 || DECL_P (TREE_PURPOSE (t)));
4615 if (!RECUR (TREE_VALUE (t), want_rval))
4617 if (TREE_CHAIN (t) == NULL_TREE)
4619 return RECUR (TREE_CHAIN (t), want_rval);
4622 case TRUNC_DIV_EXPR:
4624 case FLOOR_DIV_EXPR:
4625 case ROUND_DIV_EXPR:
4626 case TRUNC_MOD_EXPR:
4628 case ROUND_MOD_EXPR:
4630 tree denom = TREE_OPERAND (t, 1);
4631 if (!RECUR (denom, rval))
4633 /* We can't call cxx_eval_outermost_constant_expr on an expression
4634 that hasn't been through instantiate_non_dependent_expr yet. */
4635 if (!processing_template_decl)
4636 denom = cxx_eval_outermost_constant_expr (denom, true);
4637 if (integer_zerop (denom))
4639 if (flags & tf_error)
4640 error ("division by zero is not a constant-expression");
4646 return RECUR (TREE_OPERAND (t, 0), want_rval);
4652 /* check_return_expr sometimes wraps a TARGET_EXPR in a
4653 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
4654 introduced by build_call_a. */
4655 tree op0 = TREE_OPERAND (t, 0);
4656 tree op1 = TREE_OPERAND (t, 1);
4658 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
4659 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
4660 return RECUR (op0, want_rval);
4665 /* If the first operand is the non-short-circuit constant, look at
4666 the second operand; otherwise we only care about the first one for
4668 case TRUTH_AND_EXPR:
4669 case TRUTH_ANDIF_EXPR:
4670 tmp = boolean_true_node;
4673 case TRUTH_ORIF_EXPR:
4674 tmp = boolean_false_node;
4677 tree op = TREE_OPERAND (t, 0);
4678 if (!RECUR (op, rval))
4680 if (!processing_template_decl)
4681 op = cxx_eval_outermost_constant_expr (op, true);
4682 if (tree_int_cst_equal (op, tmp))
4683 return RECUR (TREE_OPERAND (t, 1), rval);
4690 case POINTER_PLUS_EXPR:
4692 case EXACT_DIV_EXPR:
4702 case TRUTH_XOR_EXPR:
4703 case UNORDERED_EXPR:
4716 case ARRAY_RANGE_REF:
4721 for (i = 0; i < 2; ++i)
4722 if (!RECUR (TREE_OPERAND (t, i), want_rval))
4726 case CILK_SYNC_STMT:
4727 case CILK_SPAWN_STMT:
4728 case ARRAY_NOTATION_REF:
4733 for (i = 0; i < 3; ++i)
4734 if (!RECUR (TREE_OPERAND (t, i), true))
4740 /* If the condition is a known constant, we know which of the legs we
4741 care about; otherwise we only require that the condition and
4742 either of the legs be potentially constant. */
4743 tmp = TREE_OPERAND (t, 0);
4744 if (!RECUR (tmp, rval))
4746 if (!processing_template_decl)
4747 tmp = cxx_eval_outermost_constant_expr (tmp, true);
4748 if (integer_zerop (tmp))
4749 return RECUR (TREE_OPERAND (t, 2), want_rval);
4750 else if (TREE_CODE (tmp) == INTEGER_CST)
4751 return RECUR (TREE_OPERAND (t, 1), want_rval);
4752 for (i = 1; i < 3; ++i)
4753 if (potential_constant_expression_1 (TREE_OPERAND (t, i),
4754 want_rval, strict, tf_none))
4756 if (flags & tf_error)
4757 error ("expression %qE is not a constant-expression", t);
4761 if (VEC_INIT_EXPR_IS_CONSTEXPR (t))
4763 if (flags & tf_error)
4765 error ("non-constant array initialization");
4766 diagnose_non_constexpr_vec_init (t);
4772 /* We can see these in statement-expressions. */
4776 if (objc_is_property_ref (t))
4779 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t)));
4786 /* The main entry point to the above. */
4789 potential_constant_expression (tree t)
4791 return potential_constant_expression_1 (t, false, true, tf_none);
4795 potential_static_init_expression (tree t)
4797 return potential_constant_expression_1 (t, false, false, tf_none);
4800 /* As above, but require a constant rvalue. */
4803 potential_rvalue_constant_expression (tree t)
4805 return potential_constant_expression_1 (t, true, true, tf_none);
4808 /* Like above, but complain about non-constant expressions. */
4811 require_potential_constant_expression (tree t)
4813 return potential_constant_expression_1 (t, false, true, tf_warning_or_error);
4816 /* Cross product of the above. */
4819 require_potential_rvalue_constant_expression (tree t)
4821 return potential_constant_expression_1 (t, true, true, tf_warning_or_error);
4824 #include "gt-cp-constexpr.h"