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))
424 /* Subroutine of check_constexpr_ctor_body. */
427 check_constexpr_ctor_body_1 (tree last, tree list)
429 switch (TREE_CODE (list))
432 if (TREE_CODE (DECL_EXPR_DECL (list)) == USING_DECL)
436 case CLEANUP_POINT_EXPR:
437 return check_constexpr_ctor_body (last, TREE_OPERAND (list, 0),
441 if (!check_constexpr_bind_expr_vars (list)
442 || !check_constexpr_ctor_body (last, BIND_EXPR_BODY (list),
456 /* Make sure that there are no statements after LAST in the constructor
457 body represented by LIST. */
460 check_constexpr_ctor_body (tree last, tree list, bool complain)
462 /* C++14 doesn't require a constexpr ctor to have an empty body. */
463 if (cxx_dialect >= cxx14)
467 if (TREE_CODE (list) == STATEMENT_LIST)
469 tree_stmt_iterator i = tsi_last (list);
470 for (; !tsi_end_p (i); tsi_prev (&i))
472 tree t = tsi_stmt (i);
475 if (!check_constexpr_ctor_body_1 (last, t))
482 else if (list != last
483 && !check_constexpr_ctor_body_1 (last, list))
488 error ("constexpr constructor does not have empty body");
489 DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false;
494 /* V is a vector of constructor elements built up for the base and member
495 initializers of a constructor for TYPE. They need to be in increasing
496 offset order, which they might not be yet if TYPE has a primary base
497 which is not first in the base-clause or a vptr and at least one base
498 all of which are non-primary. */
500 static vec<constructor_elt, va_gc> *
501 sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v)
503 tree pri = CLASSTYPE_PRIMARY_BINFO (type);
509 field_type = BINFO_TYPE (pri);
510 else if (TYPE_CONTAINS_VPTR_P (type))
511 field_type = vtbl_ptr_type_node;
515 /* Find the element for the primary base or vptr and move it to the
516 beginning of the vec. */
517 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
518 if (TREE_TYPE (ce->index) == field_type)
521 if (i > 0 && i < vec_safe_length (v))
523 vec<constructor_elt, va_gc> &vref = *v;
524 constructor_elt elt = vref[i];
533 /* Build compile-time evalable representations of member-initializer list
534 for a constexpr constructor. */
537 build_constexpr_constructor_member_initializers (tree type, tree body)
539 vec<constructor_elt, va_gc> *vec = NULL;
541 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR
542 || TREE_CODE (body) == EH_SPEC_BLOCK)
543 body = TREE_OPERAND (body, 0);
544 if (TREE_CODE (body) == STATEMENT_LIST)
545 body = STATEMENT_LIST_HEAD (body)->stmt;
546 body = BIND_EXPR_BODY (body);
547 if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
549 body = TREE_OPERAND (body, 0);
550 if (TREE_CODE (body) == EXPR_STMT)
551 body = TREE_OPERAND (body, 0);
552 if (TREE_CODE (body) == INIT_EXPR
553 && (same_type_ignoring_top_level_qualifiers_p
554 (TREE_TYPE (TREE_OPERAND (body, 0)),
555 current_class_type)))
558 return TREE_OPERAND (body, 1);
560 ok = build_data_member_initialization (body, &vec);
562 else if (TREE_CODE (body) == STATEMENT_LIST)
564 tree_stmt_iterator i;
565 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
567 ok = build_data_member_initialization (tsi_stmt (i), &vec);
572 else if (TREE_CODE (body) == TRY_BLOCK)
574 error ("body of %<constexpr%> constructor cannot be "
575 "a function-try-block");
576 return error_mark_node;
578 else if (EXPR_P (body))
579 ok = build_data_member_initialization (body, &vec);
581 gcc_assert (errorcount > 0);
584 if (vec_safe_length (vec) > 0)
586 /* In a delegating constructor, return the target. */
587 constructor_elt *ce = &(*vec)[0];
588 if (ce->index == current_class_ptr)
595 vec = sort_constexpr_mem_initializers (type, vec);
596 return build_constructor (type, vec);
599 return error_mark_node;
602 /* Subroutine of register_constexpr_fundef. BODY is the body of a function
603 declared to be constexpr, or a sub-statement thereof. Returns the
604 return value if suitable, error_mark_node for a statement not allowed in
605 a constexpr function, or NULL_TREE if no return value was found. */
608 constexpr_fn_retval (tree body)
610 switch (TREE_CODE (body))
614 tree_stmt_iterator i;
615 tree expr = NULL_TREE;
616 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
618 tree s = constexpr_fn_retval (tsi_stmt (i));
619 if (s == error_mark_node)
620 return error_mark_node;
621 else if (s == NULL_TREE)
622 /* Keep iterating. */;
624 /* Multiple return statements. */
625 return error_mark_node;
633 return break_out_target_exprs (TREE_OPERAND (body, 0));
637 tree decl = DECL_EXPR_DECL (body);
638 if (TREE_CODE (decl) == USING_DECL
639 /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */
640 || DECL_ARTIFICIAL (decl))
642 return error_mark_node;
645 case CLEANUP_POINT_EXPR:
646 return constexpr_fn_retval (TREE_OPERAND (body, 0));
649 if (!check_constexpr_bind_expr_vars (body))
650 return error_mark_node;
651 return constexpr_fn_retval (BIND_EXPR_BODY (body));
657 return error_mark_node;
661 /* Subroutine of register_constexpr_fundef. BODY is the DECL_SAVED_TREE of
662 FUN; do the necessary transformations to turn it into a single expression
663 that we can store in the hash table. */
666 massage_constexpr_body (tree fun, tree body)
668 if (DECL_CONSTRUCTOR_P (fun))
669 body = build_constexpr_constructor_member_initializers
670 (DECL_CONTEXT (fun), body);
671 else if (cxx_dialect < cxx14)
673 if (TREE_CODE (body) == EH_SPEC_BLOCK)
674 body = EH_SPEC_STMTS (body);
675 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
676 body = TREE_OPERAND (body, 0);
677 body = constexpr_fn_retval (body);
682 /* FUN is a constexpr constructor with massaged body BODY. Return true
683 if some bases/fields are uninitialized, and complain if COMPLAIN. */
686 cx_check_missing_mem_inits (tree fun, tree body, bool complain)
693 if (TREE_CODE (body) != CONSTRUCTOR)
696 nelts = CONSTRUCTOR_NELTS (body);
697 ctype = DECL_CONTEXT (fun);
698 field = TYPE_FIELDS (ctype);
700 if (TREE_CODE (ctype) == UNION_TYPE)
702 if (nelts == 0 && next_initializable_field (field))
705 error ("%<constexpr%> constructor for union %qT must "
706 "initialize exactly one non-static data member", ctype);
713 for (i = 0; i <= nelts; ++i)
720 index = CONSTRUCTOR_ELT (body, i)->index;
721 /* Skip base and vtable inits. */
722 if (TREE_CODE (index) != FIELD_DECL
723 || DECL_ARTIFICIAL (index))
726 for (; field != index; field = DECL_CHAIN (field))
729 if (TREE_CODE (field) != FIELD_DECL
730 || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
731 || DECL_ARTIFICIAL (field))
733 ftype = strip_array_types (TREE_TYPE (field));
734 if (type_has_constexpr_default_constructor (ftype))
736 /* It's OK to skip a member with a trivial constexpr ctor.
737 A constexpr ctor that isn't trivial should have been
739 gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype)
745 error ("member %qD must be initialized by mem-initializer "
746 "in %<constexpr%> constructor", field);
747 inform (DECL_SOURCE_LOCATION (field), "declared here");
750 if (field == NULL_TREE)
752 field = DECL_CHAIN (field);
758 /* We are processing the definition of the constexpr function FUN.
759 Check that its BODY fulfills the propriate requirements and
760 enter it in the constexpr function definition table.
761 For constructor BODY is actually the TREE_LIST of the
762 member-initializer list. */
765 register_constexpr_fundef (tree fun, tree body)
767 constexpr_fundef entry;
768 constexpr_fundef **slot;
770 if (!is_valid_constexpr_fn (fun, !DECL_GENERATED_P (fun)))
773 body = massage_constexpr_body (fun, body);
774 if (body == NULL_TREE || body == error_mark_node)
776 if (!DECL_CONSTRUCTOR_P (fun))
777 error ("body of constexpr function %qD not a return-statement", fun);
781 if (!potential_rvalue_constant_expression (body))
783 if (!DECL_GENERATED_P (fun))
784 require_potential_rvalue_constant_expression (body);
788 if (DECL_CONSTRUCTOR_P (fun)
789 && cx_check_missing_mem_inits (fun, body, !DECL_GENERATED_P (fun)))
792 /* Create the constexpr function table if necessary. */
793 if (constexpr_fundef_table == NULL)
794 constexpr_fundef_table
795 = hash_table<constexpr_fundef_hasher>::create_ggc (101);
799 slot = constexpr_fundef_table->find_slot (&entry, INSERT);
801 gcc_assert (*slot == NULL);
802 *slot = ggc_alloc<constexpr_fundef> ();
808 /* FUN is a non-constexpr function called in a context that requires a
809 constant expression. If it comes from a constexpr template, explain why
810 the instantiation isn't constexpr. */
813 explain_invalid_constexpr_fn (tree fun)
815 static hash_set<tree> *diagnosed;
818 /* Only diagnose defaulted functions or instantiations. */
819 if (!DECL_DEFAULTED_FN (fun)
820 && !is_instantiation_of_constexpr (fun))
822 if (diagnosed == NULL)
823 diagnosed = new hash_set<tree>;
824 if (diagnosed->add (fun))
825 /* Already explained. */
828 save_loc = input_location;
829 input_location = DECL_SOURCE_LOCATION (fun);
830 inform (0, "%q+D is not usable as a constexpr function because:", fun);
831 /* First check the declaration. */
832 if (is_valid_constexpr_fn (fun, true))
834 /* Then if it's OK, the body. */
835 if (!DECL_DECLARED_CONSTEXPR_P (fun))
836 explain_implicit_non_constexpr (fun);
839 body = massage_constexpr_body (fun, DECL_SAVED_TREE (fun));
840 require_potential_rvalue_constant_expression (body);
841 if (DECL_CONSTRUCTOR_P (fun))
842 cx_check_missing_mem_inits (fun, body, true);
845 input_location = save_loc;
848 /* Objects of this type represent calls to constexpr functions
849 along with the bindings of parameters to their arguments, for
850 the purpose of compile time evaluation. */
852 struct GTY((for_user)) constexpr_call {
853 /* Description of the constexpr function definition. */
854 constexpr_fundef *fundef;
855 /* Parameter bindings environment. A TREE_LIST where each TREE_PURPOSE
856 is a parameter _DECL and the TREE_VALUE is the value of the parameter.
857 Note: This arrangement is made to accommodate the use of
858 iterative_hash_template_arg (see pt.c). If you change this
859 representation, also change the hash calculation in
860 cxx_eval_call_expression. */
862 /* Result of the call.
863 NULL means the call is being evaluated.
864 error_mark_node means that the evaluation was erroneous;
865 otherwise, the actuall value of the call. */
867 /* The hash of this call; we remember it here to avoid having to
868 recalculate it when expanding the hash table. */
872 struct constexpr_call_hasher : ggc_hasher<constexpr_call *>
874 static hashval_t hash (constexpr_call *);
875 static bool equal (constexpr_call *, constexpr_call *);
878 /* The constexpr expansion context. CALL is the current function
879 expansion, CTOR is the current aggregate initializer, OBJECT is the
880 object being initialized by CTOR, either a VAR_DECL or a _REF. VALUES
881 is a map of values of variables initialized within the expression. */
883 struct constexpr_ctx {
884 /* The innermost call we're evaluating. */
885 constexpr_call *call;
886 /* Values for any temporaries or local variables within the
887 constant-expression. */
888 hash_map<tree,tree> *values;
889 /* The CONSTRUCTOR we're currently building up for an aggregate
892 /* The object we're building the CONSTRUCTOR for. */
894 /* Whether we should error on a non-constant expression or fail quietly. */
896 /* Whether we are strictly conforming to constant expression rules or
897 trying harder to get a constant value. */
901 /* A table of all constexpr calls that have been evaluated by the
902 compiler in this translation unit. */
904 static GTY (()) hash_table<constexpr_call_hasher> *constexpr_call_table;
906 static tree cxx_eval_constant_expression (const constexpr_ctx *, tree,
907 bool, bool *, bool *, tree * = NULL);
909 /* Compute a hash value for a constexpr call representation. */
912 constexpr_call_hasher::hash (constexpr_call *info)
917 /* Return true if the objects pointed to by P and Q represent calls
918 to the same constexpr function with the same arguments.
919 Otherwise, return false. */
922 constexpr_call_hasher::equal (constexpr_call *lhs, constexpr_call *rhs)
928 if (!constexpr_fundef_hasher::equal (lhs->fundef, rhs->fundef))
930 lhs_bindings = lhs->bindings;
931 rhs_bindings = rhs->bindings;
932 while (lhs_bindings != NULL && rhs_bindings != NULL)
934 tree lhs_arg = TREE_VALUE (lhs_bindings);
935 tree rhs_arg = TREE_VALUE (rhs_bindings);
936 gcc_assert (TREE_TYPE (lhs_arg) == TREE_TYPE (rhs_arg));
937 if (!cp_tree_equal (lhs_arg, rhs_arg))
939 lhs_bindings = TREE_CHAIN (lhs_bindings);
940 rhs_bindings = TREE_CHAIN (rhs_bindings);
942 return lhs_bindings == rhs_bindings;
945 /* Initialize the constexpr call table, if needed. */
948 maybe_initialize_constexpr_call_table (void)
950 if (constexpr_call_table == NULL)
951 constexpr_call_table = hash_table<constexpr_call_hasher>::create_ggc (101);
954 /* We have an expression tree T that represents a call, either CALL_EXPR
955 or AGGR_INIT_EXPR. If the call is lexically to a named function,
956 retrun the _DECL for that function. */
959 get_function_named_in_call (tree t)
962 switch (TREE_CODE (t))
965 fun = CALL_EXPR_FN (t);
969 fun = AGGR_INIT_EXPR_FN (t);
976 if (fun && TREE_CODE (fun) == ADDR_EXPR
977 && TREE_CODE (TREE_OPERAND (fun, 0)) == FUNCTION_DECL)
978 fun = TREE_OPERAND (fun, 0);
982 /* We have an expression tree T that represents a call, either CALL_EXPR
983 or AGGR_INIT_EXPR. Return the Nth argument. */
986 get_nth_callarg (tree t, int n)
988 switch (TREE_CODE (t))
991 return CALL_EXPR_ARG (t, n);
994 return AGGR_INIT_EXPR_ARG (t, n);
1002 /* Look up the binding of the function parameter T in a constexpr
1003 function call context CALL. */
1006 lookup_parameter_binding (const constexpr_call *call, tree t)
1008 tree b = purpose_member (t, call->bindings);
1009 return TREE_VALUE (b);
1012 /* Attempt to evaluate T which represents a call to a builtin function.
1013 We assume here that all builtin functions evaluate to scalar types
1014 represented by _CST nodes. */
1017 cxx_eval_builtin_function_call (const constexpr_ctx *ctx, tree t,
1019 bool *non_constant_p, bool *overflow_p)
1021 const int nargs = call_expr_nargs (t);
1022 tree *args = (tree *) alloca (nargs * sizeof (tree));
1025 for (i = 0; i < nargs; ++i)
1027 args[i] = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, i),
1029 non_constant_p, overflow_p);
1030 if (ctx->quiet && *non_constant_p)
1033 if (*non_constant_p)
1035 new_call = fold_build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t),
1036 CALL_EXPR_FN (t), nargs, args);
1037 VERIFY_CONSTANT (new_call);
1041 /* TEMP is the constant value of a temporary object of type TYPE. Adjust
1042 the type of the value to match. */
1045 adjust_temp_type (tree type, tree temp)
1047 if (TREE_TYPE (temp) == type)
1049 /* Avoid wrapping an aggregate value in a NOP_EXPR. */
1050 if (TREE_CODE (temp) == CONSTRUCTOR)
1051 return build_constructor (type, CONSTRUCTOR_ELTS (temp));
1052 gcc_assert (scalarish_type_p (type));
1053 return cp_fold_convert (type, temp);
1056 /* True if we want to use the new handling of constexpr calls based on
1058 #define use_new_call true
1060 /* Subroutine of cxx_eval_call_expression.
1061 We are processing a call expression (either CALL_EXPR or
1062 AGGR_INIT_EXPR) in the context of CTX. Evaluate
1063 all arguments and bind their values to correspondings
1064 parameters, making up the NEW_CALL context. */
1067 cxx_bind_parameters_in_call (const constexpr_ctx *ctx, tree t,
1068 constexpr_call *new_call,
1069 bool *non_constant_p, bool *overflow_p,
1070 bool *non_constant_args)
1072 const int nargs = call_expr_nargs (t);
1073 tree fun = new_call->fundef->decl;
1074 tree parms = DECL_ARGUMENTS (fun);
1076 tree *p = &new_call->bindings;
1077 for (i = 0; i < nargs; ++i)
1080 tree type = parms ? TREE_TYPE (parms) : void_type_node;
1081 x = get_nth_callarg (t, i);
1082 /* For member function, the first argument is a pointer to the implied
1083 object. For a constructor, it might still be a dummy object, in
1084 which case we get the real argument from ctx. */
1085 if (i == 0 && DECL_CONSTRUCTOR_P (fun)
1086 && is_dummy_object (x))
1089 x = cp_build_addr_expr (x, tf_warning_or_error);
1092 if (parms && DECL_BY_REFERENCE (parms) && !use_new_call)
1094 /* cp_genericize made this a reference for argument passing, but
1095 we don't want to treat it like one for C++11 constexpr
1096 evaluation. C++14 constexpr evaluation uses the genericized
1098 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
1099 gcc_assert (TREE_CODE (TREE_TYPE (x)) == REFERENCE_TYPE);
1100 type = TREE_TYPE (type);
1101 x = convert_from_reference (x);
1104 arg = cxx_eval_constant_expression (ctx, x, lval,
1105 non_constant_p, overflow_p);
1106 /* Don't VERIFY_CONSTANT here. */
1107 if (*non_constant_p && ctx->quiet)
1109 /* Just discard ellipsis args after checking their constantitude. */
1112 if (*non_constant_p)
1113 /* Don't try to adjust the type of non-constant args. */
1116 /* Make sure the binding has the same type as the parm. */
1117 if (TREE_CODE (type) != REFERENCE_TYPE)
1118 arg = adjust_temp_type (type, arg);
1119 if (!TREE_CONSTANT (arg))
1120 *non_constant_args = true;
1121 *p = build_tree_list (parms, arg);
1122 p = &TREE_CHAIN (*p);
1124 parms = TREE_CHAIN (parms);
1128 /* Variables and functions to manage constexpr call expansion context.
1129 These do not need to be marked for PCH or GC. */
1131 /* FIXME remember and print actual constant arguments. */
1132 static vec<tree> call_stack = vNULL;
1133 static int call_stack_tick;
1134 static int last_cx_error_tick;
1137 push_cx_call_context (tree call)
1140 if (!EXPR_HAS_LOCATION (call))
1141 SET_EXPR_LOCATION (call, input_location);
1142 call_stack.safe_push (call);
1143 if (call_stack.length () > (unsigned) max_constexpr_depth)
1149 pop_cx_call_context (void)
1156 cx_error_context (void)
1158 vec<tree> r = vNULL;
1159 if (call_stack_tick != last_cx_error_tick
1160 && !call_stack.is_empty ())
1162 last_cx_error_tick = call_stack_tick;
1166 /* Subroutine of cxx_eval_constant_expression.
1167 Evaluate the call expression tree T in the context of OLD_CALL expression
1171 cxx_eval_call_expression (const constexpr_ctx *ctx, tree t,
1173 bool *non_constant_p, bool *overflow_p)
1175 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
1176 tree fun = get_function_named_in_call (t);
1177 constexpr_call new_call = { NULL, NULL, NULL, 0 };
1180 if (fun == NULL_TREE)
1181 switch (CALL_EXPR_IFN (t))
1183 case IFN_UBSAN_NULL:
1184 case IFN_UBSAN_BOUNDS:
1185 case IFN_UBSAN_VPTR:
1189 error_at (loc, "call to internal function");
1190 *non_constant_p = true;
1194 if (TREE_CODE (fun) != FUNCTION_DECL)
1196 /* Might be a constexpr function pointer. */
1197 fun = cxx_eval_constant_expression (ctx, fun,
1198 /*lval*/false, non_constant_p,
1201 if (TREE_CODE (fun) == ADDR_EXPR)
1202 fun = TREE_OPERAND (fun, 0);
1204 if (TREE_CODE (fun) != FUNCTION_DECL)
1206 if (!ctx->quiet && !*non_constant_p)
1207 error_at (loc, "expression %qE does not designate a constexpr "
1209 *non_constant_p = true;
1212 if (DECL_CLONED_FUNCTION_P (fun))
1213 fun = DECL_CLONED_FUNCTION (fun);
1215 if (is_ubsan_builtin_p (fun))
1218 if (is_builtin_fn (fun))
1219 return cxx_eval_builtin_function_call (ctx, t,
1220 lval, non_constant_p, overflow_p);
1221 if (!DECL_DECLARED_CONSTEXPR_P (fun))
1225 error_at (loc, "call to non-constexpr function %qD", fun);
1226 explain_invalid_constexpr_fn (fun);
1228 *non_constant_p = true;
1232 /* Shortcut trivial constructor/op=. */
1233 if (trivial_fn_p (fun))
1235 if (call_expr_nargs (t) == 2)
1237 tree arg = convert_from_reference (get_nth_callarg (t, 1));
1238 return cxx_eval_constant_expression (ctx, arg,
1239 lval, non_constant_p,
1242 else if (TREE_CODE (t) == AGGR_INIT_EXPR
1243 && AGGR_INIT_ZERO_FIRST (t))
1244 return build_zero_init (DECL_CONTEXT (fun), NULL_TREE, false);
1247 /* If in direct recursive call, optimize definition search. */
1248 if (ctx && ctx->call && ctx->call->fundef->decl == fun)
1249 new_call.fundef = ctx->call->fundef;
1252 new_call.fundef = retrieve_constexpr_fundef (fun);
1253 if (new_call.fundef == NULL || new_call.fundef->body == NULL)
1257 if (DECL_INITIAL (fun))
1259 /* The definition of fun was somehow unsuitable. */
1260 error_at (loc, "%qD called in a constant expression", fun);
1261 explain_invalid_constexpr_fn (fun);
1264 error_at (loc, "%qD used before its definition", fun);
1266 *non_constant_p = true;
1271 constexpr_ctx new_ctx = *ctx;
1272 if (DECL_CONSTRUCTOR_P (fun) && !ctx->object
1273 && TREE_CODE (t) == AGGR_INIT_EXPR)
1275 /* We want to have an initialization target for an AGGR_INIT_EXPR.
1276 If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
1277 new_ctx.object = AGGR_INIT_EXPR_SLOT (t);
1278 tree ctor = new_ctx.ctor = build_constructor (DECL_CONTEXT (fun), NULL);
1279 CONSTRUCTOR_NO_IMPLICIT_ZERO (ctor) = true;
1280 ctx->values->put (new_ctx.object, ctor);
1284 bool non_constant_args = false;
1285 cxx_bind_parameters_in_call (ctx, t, &new_call,
1286 non_constant_p, overflow_p, &non_constant_args);
1287 if (*non_constant_p)
1290 depth_ok = push_cx_call_context (t);
1292 tree result = NULL_TREE;
1294 constexpr_call *entry = NULL;
1295 if (!non_constant_args)
1297 new_call.hash = iterative_hash_template_arg
1298 (new_call.bindings, constexpr_fundef_hasher::hash (new_call.fundef));
1300 /* If we have seen this call before, we are done. */
1301 maybe_initialize_constexpr_call_table ();
1302 constexpr_call **slot
1303 = constexpr_call_table->find_slot (&new_call, INSERT);
1307 /* We need to keep a pointer to the entry, not just the slot, as the
1308 slot can move in the call to cxx_eval_builtin_function_call. */
1309 *slot = entry = ggc_alloc<constexpr_call> ();
1312 /* Calls which are in progress have their result set to NULL
1313 so that we can detect circular dependencies. */
1314 else if (entry->result == NULL)
1317 error ("call has circular dependency");
1318 *non_constant_p = true;
1319 entry->result = result = error_mark_node;
1322 result = entry->result;
1328 error ("constexpr evaluation depth exceeds maximum of %d (use "
1329 "-fconstexpr-depth= to increase the maximum)",
1330 max_constexpr_depth);
1331 *non_constant_p = true;
1332 result = error_mark_node;
1336 if (!result || result == error_mark_node)
1340 new_ctx.call = &new_call;
1341 result = (cxx_eval_constant_expression
1342 (&new_ctx, new_call.fundef->body,
1344 non_constant_p, overflow_p));
1348 if (DECL_SAVED_TREE (fun) == NULL_TREE
1349 && (DECL_CONSTRUCTOR_P (fun) || DECL_DESTRUCTOR_P (fun)))
1350 /* The maybe-in-charge 'tor had its DECL_SAVED_TREE
1351 cleared, try the first clone. */
1352 fun = DECL_CHAIN (fun);
1353 gcc_assert (DECL_SAVED_TREE (fun));
1356 /* Unshare the whole function body. */
1357 tree body = copy_fn (fun, parms, res);
1359 /* Associate the bindings with the remapped parms. */
1360 tree bound = new_call.bindings;
1361 tree remapped = parms;
1364 tree oparm = TREE_PURPOSE (bound);
1365 tree arg = TREE_VALUE (bound);
1366 gcc_assert (DECL_NAME (remapped) == DECL_NAME (oparm));
1367 ctx->values->put (remapped, arg);
1368 bound = TREE_CHAIN (bound);
1369 remapped = DECL_CHAIN (remapped);
1371 /* Add the RESULT_DECL to the values map, too. */
1372 tree slot = NULL_TREE;
1373 if (DECL_BY_REFERENCE (res))
1375 slot = AGGR_INIT_EXPR_SLOT (t);
1376 tree addr = build_address (slot);
1377 addr = build_nop (TREE_TYPE (res), addr);
1378 ctx->values->put (res, addr);
1379 ctx->values->put (slot, NULL_TREE);
1382 ctx->values->put (res, NULL_TREE);
1384 tree jump_target = NULL_TREE;
1385 cxx_eval_constant_expression (ctx, body,
1386 lval, non_constant_p, overflow_p,
1389 if (DECL_CONSTRUCTOR_P (fun))
1390 /* This can be null for a subobject constructor call, in
1391 which case what we care about is the initialization
1392 side-effects rather than the value. We could get at the
1393 value by evaluating *this, but we don't bother; there's
1394 no need to put such a call in the hash table. */
1395 result = lval ? ctx->object : ctx->ctor;
1396 else if (VOID_TYPE_P (TREE_TYPE (res)))
1400 result = *ctx->values->get (slot ? slot : res);
1401 if (result == NULL_TREE && !*non_constant_p)
1404 error ("constexpr call flows off the end "
1406 *non_constant_p = true;
1410 /* Remove the parms/result from the values map. Is it worth
1411 bothering to do this when the map itself is only live for
1412 one constexpr evaluation? If so, maybe also clear out
1413 other vars from call, maybe in BIND_EXPR handling? */
1414 ctx->values->remove (res);
1416 ctx->values->remove (slot);
1417 for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
1418 ctx->values->remove (parm);
1422 if (result == error_mark_node)
1423 *non_constant_p = true;
1424 if (*non_constant_p)
1425 result = error_mark_node;
1428 /* If this was a call to initialize an object, set the type of
1429 the CONSTRUCTOR to the type of that object. */
1430 if (DECL_CONSTRUCTOR_P (fun) && !use_new_call)
1432 tree ob_arg = get_nth_callarg (t, 0);
1433 STRIP_NOPS (ob_arg);
1434 gcc_assert (TYPE_PTR_P (TREE_TYPE (ob_arg))
1435 && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (ob_arg))));
1436 result = adjust_temp_type (TREE_TYPE (TREE_TYPE (ob_arg)),
1443 entry->result = result;
1446 pop_cx_call_context ();
1447 return unshare_expr (result);
1450 /* FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
1453 reduced_constant_expression_p (tree t)
1455 switch (TREE_CODE (t))
1458 /* Even if we can't lower this yet, it's constant. */
1462 /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
1463 tree elt; unsigned HOST_WIDE_INT idx;
1464 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), idx, elt)
1465 if (!reduced_constant_expression_p (elt))
1470 /* FIXME are we calling this too much? */
1471 return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE;
1475 /* Some expressions may have constant operands but are not constant
1476 themselves, such as 1/0. Call this function (or rather, the macro
1477 following it) to check for that condition.
1479 We only call this in places that require an arithmetic constant, not in
1480 places where we might have a non-constant expression that can be a
1481 component of a constant expression, such as the address of a constexpr
1482 variable that might be dereferenced later. */
1485 verify_constant (tree t, bool allow_non_constant, bool *non_constant_p,
1488 if (!*non_constant_p && !reduced_constant_expression_p (t))
1490 if (!allow_non_constant)
1491 error ("%q+E is not a constant expression", t);
1492 *non_constant_p = true;
1494 if (TREE_OVERFLOW_P (t))
1496 if (!allow_non_constant)
1498 permerror (input_location, "overflow in constant expression");
1499 /* If we're being permissive (and are in an enforcing
1500 context), ignore the overflow. */
1501 if (flag_permissive)
1502 return *non_constant_p;
1506 return *non_constant_p;
1509 /* Check whether the shift operation with code CODE and type TYPE on LHS
1510 and RHS is undefined. If it is, give an error with an explanation,
1511 and return true; return false otherwise. */
1514 cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx,
1515 enum tree_code code, tree type, tree lhs, tree rhs)
1517 if ((code != LSHIFT_EXPR && code != RSHIFT_EXPR)
1518 || TREE_CODE (lhs) != INTEGER_CST
1519 || TREE_CODE (rhs) != INTEGER_CST)
1522 tree lhstype = TREE_TYPE (lhs);
1523 unsigned HOST_WIDE_INT uprec = TYPE_PRECISION (TREE_TYPE (lhs));
1525 /* [expr.shift] The behavior is undefined if the right operand
1526 is negative, or greater than or equal to the length in bits
1527 of the promoted left operand. */
1528 if (tree_int_cst_sgn (rhs) == -1)
1531 error_at (loc, "right operand of shift expression %q+E is negative",
1532 build2_loc (loc, code, type, lhs, rhs));
1535 if (compare_tree_int (rhs, uprec) >= 0)
1538 error_at (loc, "right operand of shift expression %q+E is >= than "
1539 "the precision of the left operand",
1540 build2_loc (loc, code, type, lhs, rhs));
1544 /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
1545 if E1 has a signed type and non-negative value, and E1x2^E2 is
1546 representable in the corresponding unsigned type of the result type,
1547 then that value, converted to the result type, is the resulting value;
1548 otherwise, the behavior is undefined. */
1549 if (code == LSHIFT_EXPR && !TYPE_UNSIGNED (lhstype)
1550 && (cxx_dialect >= cxx11))
1552 if (tree_int_cst_sgn (lhs) == -1)
1555 error_at (loc, "left operand of shift expression %q+E is negative",
1556 build2_loc (loc, code, type, lhs, rhs));
1559 /* For signed x << y the following:
1560 (unsigned) x >> ((prec (lhs) - 1) - y)
1561 if > 1, is undefined. The right-hand side of this formula
1562 is the highest bit of the LHS that can be set (starting from 0),
1563 so that the shift doesn't overflow. We then right-shift the LHS
1564 to see whether any other bit is set making the original shift
1565 undefined -- the result is not representable in the corresponding
1567 tree t = build_int_cst (unsigned_type_node, uprec - 1);
1568 t = fold_build2 (MINUS_EXPR, unsigned_type_node, t, rhs);
1569 tree ulhs = fold_convert (unsigned_type_for (lhstype), lhs);
1570 t = fold_build2 (RSHIFT_EXPR, TREE_TYPE (ulhs), ulhs, t);
1571 if (tree_int_cst_lt (integer_one_node, t))
1574 error_at (loc, "shift expression %q+E overflows",
1575 build2_loc (loc, code, type, lhs, rhs));
1582 /* Subroutine of cxx_eval_constant_expression.
1583 Attempt to reduce the unary expression tree T to a compile time value.
1584 If successful, return the value. Otherwise issue a diagnostic
1585 and return error_mark_node. */
1588 cxx_eval_unary_expression (const constexpr_ctx *ctx, tree t,
1590 bool *non_constant_p, bool *overflow_p)
1593 tree orig_arg = TREE_OPERAND (t, 0);
1594 tree arg = cxx_eval_constant_expression (ctx, orig_arg, /*lval*/false,
1595 non_constant_p, overflow_p);
1596 VERIFY_CONSTANT (arg);
1597 location_t loc = EXPR_LOCATION (t);
1598 enum tree_code code = TREE_CODE (t);
1599 tree type = TREE_TYPE (t);
1600 r = fold_unary_loc (loc, code, type, arg);
1603 if (arg == orig_arg)
1606 r = build1_loc (loc, code, type, arg);
1608 VERIFY_CONSTANT (r);
1612 /* Subroutine of cxx_eval_constant_expression.
1613 Like cxx_eval_unary_expression, except for binary expressions. */
1616 cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t,
1618 bool *non_constant_p, bool *overflow_p)
1621 tree orig_lhs = TREE_OPERAND (t, 0);
1622 tree orig_rhs = TREE_OPERAND (t, 1);
1624 lhs = cxx_eval_constant_expression (ctx, orig_lhs, /*lval*/false,
1625 non_constant_p, overflow_p);
1626 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
1627 a local array in a constexpr function. */
1628 bool ptr = POINTER_TYPE_P (TREE_TYPE (lhs));
1630 VERIFY_CONSTANT (lhs);
1631 rhs = cxx_eval_constant_expression (ctx, orig_rhs, /*lval*/false,
1632 non_constant_p, overflow_p);
1634 VERIFY_CONSTANT (rhs);
1636 location_t loc = EXPR_LOCATION (t);
1637 enum tree_code code = TREE_CODE (t);
1638 tree type = TREE_TYPE (t);
1639 r = fold_binary_loc (loc, code, type, lhs, rhs);
1642 if (lhs == orig_lhs && rhs == orig_rhs)
1645 r = build2_loc (loc, code, type, lhs, rhs);
1647 else if (cxx_eval_check_shift_p (loc, ctx, code, type, lhs, rhs))
1648 *non_constant_p = true;
1650 VERIFY_CONSTANT (r);
1654 /* Subroutine of cxx_eval_constant_expression.
1655 Attempt to evaluate condition expressions. Dead branches are not
1659 cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t,
1661 bool *non_constant_p, bool *overflow_p,
1664 tree val = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
1666 non_constant_p, overflow_p);
1667 VERIFY_CONSTANT (val);
1668 /* Don't VERIFY_CONSTANT the other operands. */
1669 if (integer_zerop (val))
1670 return cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2),
1672 non_constant_p, overflow_p,
1674 return cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
1676 non_constant_p, overflow_p,
1680 /* Subroutine of cxx_eval_constant_expression.
1681 Attempt to reduce a reference to an array slot. */
1684 cxx_eval_array_reference (const constexpr_ctx *ctx, tree t,
1686 bool *non_constant_p, bool *overflow_p)
1688 tree oldary = TREE_OPERAND (t, 0);
1689 tree ary = cxx_eval_constant_expression (ctx, oldary,
1691 non_constant_p, overflow_p);
1695 unsigned len, elem_nchars = 1;
1696 if (*non_constant_p)
1698 oldidx = TREE_OPERAND (t, 1);
1699 index = cxx_eval_constant_expression (ctx, oldidx,
1701 non_constant_p, overflow_p);
1702 VERIFY_CONSTANT (index);
1703 if (lval && ary == oldary && index == oldidx)
1706 return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL);
1707 elem_type = TREE_TYPE (TREE_TYPE (ary));
1708 if (TREE_CODE (ary) == CONSTRUCTOR)
1709 len = CONSTRUCTOR_NELTS (ary);
1710 else if (TREE_CODE (ary) == STRING_CST)
1712 elem_nchars = (TYPE_PRECISION (elem_type)
1713 / TYPE_PRECISION (char_type_node));
1714 len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars;
1718 /* We can't do anything with other tree codes, so use
1719 VERIFY_CONSTANT to complain and fail. */
1720 VERIFY_CONSTANT (ary);
1723 if (compare_tree_int (index, len) >= 0)
1725 if (tree_int_cst_lt (index, array_type_nelts_top (TREE_TYPE (ary))))
1727 /* If it's within the array bounds but doesn't have an explicit
1728 initializer, it's value-initialized. */
1729 tree val = build_value_init (elem_type, tf_warning_or_error);
1730 return cxx_eval_constant_expression (ctx, val,
1732 non_constant_p, overflow_p);
1736 error ("array subscript out of bound");
1737 *non_constant_p = true;
1740 else if (tree_int_cst_lt (index, integer_zero_node))
1743 error ("negative array subscript");
1744 *non_constant_p = true;
1747 i = tree_to_shwi (index);
1748 if (TREE_CODE (ary) == CONSTRUCTOR)
1749 return (*CONSTRUCTOR_ELTS (ary))[i].value;
1750 else if (elem_nchars == 1)
1751 return build_int_cst (cv_unqualified (TREE_TYPE (TREE_TYPE (ary))),
1752 TREE_STRING_POINTER (ary)[i]);
1755 tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (ary)));
1756 return native_interpret_expr (type, (const unsigned char *)
1757 TREE_STRING_POINTER (ary)
1758 + i * elem_nchars, elem_nchars);
1760 /* Don't VERIFY_CONSTANT here. */
1763 /* Subroutine of cxx_eval_constant_expression.
1764 Attempt to reduce a field access of a value of class type. */
1767 cxx_eval_component_reference (const constexpr_ctx *ctx, tree t,
1769 bool *non_constant_p, bool *overflow_p)
1771 unsigned HOST_WIDE_INT i;
1774 tree part = TREE_OPERAND (t, 1);
1775 tree orig_whole = TREE_OPERAND (t, 0);
1776 tree whole = cxx_eval_constant_expression (ctx, orig_whole,
1778 non_constant_p, overflow_p);
1779 if (whole == orig_whole)
1782 return fold_build3 (COMPONENT_REF, TREE_TYPE (t),
1783 whole, part, NULL_TREE);
1784 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
1786 if (!*non_constant_p && TREE_CODE (whole) != CONSTRUCTOR)
1789 error ("%qE is not a constant expression", orig_whole);
1790 *non_constant_p = true;
1792 if (DECL_MUTABLE_P (part))
1795 error ("mutable %qD is not usable in a constant expression", part);
1796 *non_constant_p = true;
1798 if (*non_constant_p)
1800 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
1807 /* We're in the middle of initializing it. */
1811 if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE
1812 && CONSTRUCTOR_NELTS (whole) > 0)
1814 /* DR 1188 says we don't have to deal with this. */
1816 error ("accessing %qD member instead of initialized %qD member in "
1817 "constant expression", part, CONSTRUCTOR_ELT (whole, 0)->index);
1818 *non_constant_p = true;
1822 if (CONSTRUCTOR_NO_IMPLICIT_ZERO (whole))
1824 /* 'whole' is part of the aggregate initializer we're currently
1825 building; if there's no initializer for this member yet, that's an
1828 error ("accessing uninitialized member %qD", part);
1829 *non_constant_p = true;
1833 /* If there's no explicit init for this field, it's value-initialized. */
1834 value = build_value_init (TREE_TYPE (t), tf_warning_or_error);
1835 return cxx_eval_constant_expression (ctx, value,
1837 non_constant_p, overflow_p);
1840 /* Subroutine of cxx_eval_constant_expression.
1841 Attempt to reduce a field access of a value of class type that is
1842 expressed as a BIT_FIELD_REF. */
1845 cxx_eval_bit_field_ref (const constexpr_ctx *ctx, tree t,
1847 bool *non_constant_p, bool *overflow_p)
1849 tree orig_whole = TREE_OPERAND (t, 0);
1850 tree retval, fldval, utype, mask;
1851 bool fld_seen = false;
1852 HOST_WIDE_INT istart, isize;
1853 tree whole = cxx_eval_constant_expression (ctx, orig_whole,
1855 non_constant_p, overflow_p);
1856 tree start, field, value;
1857 unsigned HOST_WIDE_INT i;
1859 if (whole == orig_whole)
1861 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
1863 if (!*non_constant_p
1864 && TREE_CODE (whole) != VECTOR_CST
1865 && TREE_CODE (whole) != CONSTRUCTOR)
1868 error ("%qE is not a constant expression", orig_whole);
1869 *non_constant_p = true;
1871 if (*non_constant_p)
1874 if (TREE_CODE (whole) == VECTOR_CST)
1875 return fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
1876 TREE_OPERAND (t, 1), TREE_OPERAND (t, 2));
1878 start = TREE_OPERAND (t, 2);
1879 istart = tree_to_shwi (start);
1880 isize = tree_to_shwi (TREE_OPERAND (t, 1));
1881 utype = TREE_TYPE (t);
1882 if (!TYPE_UNSIGNED (utype))
1883 utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1);
1884 retval = build_int_cst (utype, 0);
1885 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
1887 tree bitpos = bit_position (field);
1888 if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1))
1890 if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE
1891 && TREE_CODE (value) == INTEGER_CST
1892 && tree_fits_shwi_p (bitpos)
1893 && tree_fits_shwi_p (DECL_SIZE (field)))
1895 HOST_WIDE_INT bit = tree_to_shwi (bitpos);
1896 HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field));
1897 HOST_WIDE_INT shift;
1898 if (bit >= istart && bit + sz <= istart + isize)
1900 fldval = fold_convert (utype, value);
1901 mask = build_int_cst_type (utype, -1);
1902 mask = fold_build2 (LSHIFT_EXPR, utype, mask,
1903 size_int (TYPE_PRECISION (utype) - sz));
1904 mask = fold_build2 (RSHIFT_EXPR, utype, mask,
1905 size_int (TYPE_PRECISION (utype) - sz));
1906 fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask);
1907 shift = bit - istart;
1908 if (BYTES_BIG_ENDIAN)
1909 shift = TYPE_PRECISION (utype) - shift - sz;
1910 fldval = fold_build2 (LSHIFT_EXPR, utype, fldval,
1912 retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval);
1918 return fold_convert (TREE_TYPE (t), retval);
1920 return error_mark_node;
1923 /* Subroutine of cxx_eval_constant_expression.
1924 Evaluate a short-circuited logical expression T in the context
1925 of a given constexpr CALL. BAILOUT_VALUE is the value for
1926 early return. CONTINUE_VALUE is used here purely for
1927 sanity check purposes. */
1930 cxx_eval_logical_expression (const constexpr_ctx *ctx, tree t,
1931 tree bailout_value, tree continue_value,
1933 bool *non_constant_p, bool *overflow_p)
1936 tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
1938 non_constant_p, overflow_p);
1939 VERIFY_CONSTANT (lhs);
1940 if (tree_int_cst_equal (lhs, bailout_value))
1942 gcc_assert (tree_int_cst_equal (lhs, continue_value));
1943 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
1944 lval, non_constant_p,
1946 VERIFY_CONSTANT (r);
1950 /* REF is a COMPONENT_REF designating a particular field. V is a vector of
1951 CONSTRUCTOR elements to initialize (part of) an object containing that
1952 field. Return a pointer to the constructor_elt corresponding to the
1953 initialization of the field. */
1955 static constructor_elt *
1956 base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref)
1958 tree aggr = TREE_OPERAND (ref, 0);
1959 tree field = TREE_OPERAND (ref, 1);
1961 constructor_elt *ce;
1963 gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
1965 if (TREE_CODE (aggr) == COMPONENT_REF)
1967 constructor_elt *base_ce
1968 = base_field_constructor_elt (v, aggr);
1969 v = CONSTRUCTOR_ELTS (base_ce->value);
1972 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
1973 if (ce->index == field)
1980 /* Some of the expressions fed to the constexpr mechanism are calls to
1981 constructors, which have type void. In that case, return the type being
1982 initialized by the constructor. */
1985 initialized_type (tree t)
1989 tree type = cv_unqualified (TREE_TYPE (t));
1990 if (TREE_CODE (t) == CALL_EXPR || TREE_CODE (t) == AGGR_INIT_EXPR)
1992 /* A constructor call has void type, so we need to look deeper. */
1993 tree fn = get_function_named_in_call (t);
1994 if (fn && TREE_CODE (fn) == FUNCTION_DECL
1995 && DECL_CXX_CONSTRUCTOR_P (fn))
1996 type = DECL_CONTEXT (fn);
2001 /* We're about to initialize element INDEX of an array or class from VALUE.
2002 Set up NEW_CTX appropriately by adjusting .object to refer to the
2003 subobject and creating a new CONSTRUCTOR if the element is itself
2004 a class or array. */
2007 init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx,
2008 tree index, tree &value)
2012 if (index && TREE_CODE (index) != INTEGER_CST
2013 && TREE_CODE (index) != FIELD_DECL)
2014 /* This won't have an element in the new CONSTRUCTOR. */
2017 tree type = initialized_type (value);
2018 if (!AGGREGATE_TYPE_P (type) && !VECTOR_TYPE_P (type))
2019 /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
2022 /* The sub-aggregate initializer might contain a placeholder;
2023 update object to refer to the subobject and ctor to refer to
2024 the (newly created) sub-initializer. */
2026 new_ctx.object = build_ctor_subob_ref (index, type, ctx->object);
2027 tree elt = build_constructor (type, NULL);
2028 CONSTRUCTOR_NO_IMPLICIT_ZERO (elt) = true;
2031 if (TREE_CODE (value) == TARGET_EXPR)
2032 /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
2033 value = TARGET_EXPR_INITIAL (value);
2036 /* We're about to process an initializer for a class or array TYPE. Make
2037 sure that CTX is set up appropriately. */
2040 verify_ctor_sanity (const constexpr_ctx *ctx, tree type)
2042 /* We don't bother building a ctor for an empty base subobject. */
2043 if (is_empty_class (type))
2046 /* We're in the middle of an initializer that might involve placeholders;
2047 our caller should have created a CONSTRUCTOR for us to put the
2048 initializer into. We will either return that constructor or T. */
2049 gcc_assert (ctx->ctor);
2050 gcc_assert (same_type_ignoring_top_level_qualifiers_p
2051 (type, TREE_TYPE (ctx->ctor)));
2052 gcc_assert (CONSTRUCTOR_NELTS (ctx->ctor) == 0);
2054 gcc_assert (same_type_ignoring_top_level_qualifiers_p
2055 (type, TREE_TYPE (ctx->object)));
2056 gcc_assert (!ctx->object || !DECL_P (ctx->object)
2057 || *(ctx->values->get (ctx->object)) == ctx->ctor);
2060 /* Subroutine of cxx_eval_constant_expression.
2061 The expression tree T denotes a C-style array or a C-style
2062 aggregate. Reduce it to a constant expression. */
2065 cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t,
2067 bool *non_constant_p, bool *overflow_p)
2069 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
2070 bool changed = false;
2071 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t));
2073 verify_ctor_sanity (ctx, TREE_TYPE (t));
2074 vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
2075 vec_alloc (*p, vec_safe_length (v));
2077 unsigned i; tree index, value;
2078 FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value)
2080 constexpr_ctx new_ctx;
2081 init_subob_ctx (ctx, new_ctx, index, value);
2082 if (new_ctx.ctor != ctx->ctor)
2083 /* If we built a new CONSTRUCTOR, attach it now so that other
2084 initializers can refer to it. */
2085 CONSTRUCTOR_APPEND_ELT (*p, index, new_ctx.ctor);
2086 tree elt = cxx_eval_constant_expression (&new_ctx, value,
2088 non_constant_p, overflow_p);
2089 /* Don't VERIFY_CONSTANT here. */
2090 if (ctx->quiet && *non_constant_p)
2094 if (index && TREE_CODE (index) == COMPONENT_REF)
2096 /* This is an initialization of a vfield inside a base
2097 subaggregate that we already initialized; push this
2098 initialization into the previous initialization. */
2099 constructor_elt *inner = base_field_constructor_elt (*p, index);
2104 && (TREE_CODE (index) == NOP_EXPR
2105 || TREE_CODE (index) == POINTER_PLUS_EXPR))
2107 /* This is an initializer for an empty base; now that we've
2108 checked that it's constant, we can ignore it. */
2109 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index))));
2112 else if (new_ctx.ctor != ctx->ctor)
2114 /* We appended this element above; update the value. */
2115 gcc_assert ((*p)->last().index == index);
2116 (*p)->last().value = elt;
2119 CONSTRUCTOR_APPEND_ELT (*p, index, elt);
2121 if (*non_constant_p || !changed)
2124 /* We're done building this CONSTRUCTOR, so now we can interpret an
2125 element without an explicit initializer as value-initialized. */
2126 CONSTRUCTOR_NO_IMPLICIT_ZERO (t) = false;
2127 if (TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
2132 /* Subroutine of cxx_eval_constant_expression.
2133 The expression tree T is a VEC_INIT_EXPR which denotes the desired
2134 initialization of a non-static data member of array type. Reduce it to a
2137 Note that apart from value-initialization (when VALUE_INIT is true),
2138 this is only intended to support value-initialization and the
2139 initializations done by defaulted constructors for classes with
2140 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
2141 will either be NULL_TREE for the default constructor, or a COMPONENT_REF
2142 for the copy/move constructor. */
2145 cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init,
2146 bool value_init, bool lval,
2147 bool *non_constant_p, bool *overflow_p)
2149 tree elttype = TREE_TYPE (atype);
2150 unsigned HOST_WIDE_INT max = tree_to_uhwi (array_type_nelts_top (atype));
2151 verify_ctor_sanity (ctx, atype);
2152 vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
2153 vec_alloc (*p, max + 1);
2154 bool pre_init = false;
2155 unsigned HOST_WIDE_INT i;
2157 /* For the default constructor, build up a call to the default
2158 constructor of the element type. We only need to handle class types
2159 here, as for a constructor to be constexpr, all members must be
2160 initialized, which for a defaulted default constructor means they must
2161 be of a class type with a constexpr default constructor. */
2162 if (TREE_CODE (elttype) == ARRAY_TYPE)
2163 /* We only do this at the lowest level. */;
2164 else if (value_init)
2166 init = build_value_init (elttype, tf_warning_or_error);
2171 vec<tree, va_gc> *argvec = make_tree_vector ();
2172 init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
2173 &argvec, elttype, LOOKUP_NORMAL,
2174 tf_warning_or_error);
2175 release_tree_vector (argvec);
2176 init = build_aggr_init_expr (TREE_TYPE (init), init);
2180 for (i = 0; i < max; ++i)
2182 tree idx = build_int_cst (size_type_node, i);
2184 constexpr_ctx new_ctx;
2185 init_subob_ctx (ctx, new_ctx, idx, pre_init ? init : elttype);
2186 if (new_ctx.ctor != ctx->ctor)
2187 CONSTRUCTOR_APPEND_ELT (*p, idx, new_ctx.ctor);
2188 if (TREE_CODE (elttype) == ARRAY_TYPE)
2190 /* A multidimensional array; recurse. */
2191 if (value_init || init == NULL_TREE)
2192 eltinit = NULL_TREE;
2194 eltinit = cp_build_array_ref (input_location, init, idx,
2195 tf_warning_or_error);
2196 eltinit = cxx_eval_vec_init_1 (&new_ctx, elttype, eltinit, value_init,
2198 non_constant_p, overflow_p);
2202 /* Initializing an element using value or default initialization
2203 we just pre-built above. */
2204 eltinit = (cxx_eval_constant_expression
2206 lval, non_constant_p, overflow_p));
2210 /* Copying an element. */
2211 gcc_assert (same_type_ignoring_top_level_qualifiers_p
2212 (atype, TREE_TYPE (init)));
2213 eltinit = cp_build_array_ref (input_location, init, idx,
2214 tf_warning_or_error);
2215 if (!real_lvalue_p (init))
2216 eltinit = move (eltinit);
2217 eltinit = force_rvalue (eltinit, tf_warning_or_error);
2218 eltinit = (cxx_eval_constant_expression
2219 (&new_ctx, eltinit, lval,
2220 non_constant_p, overflow_p));
2222 if (*non_constant_p && !ctx->quiet)
2224 if (new_ctx.ctor != ctx->ctor)
2226 /* We appended this element above; update the value. */
2227 gcc_assert ((*p)->last().index == idx);
2228 (*p)->last().value = eltinit;
2231 CONSTRUCTOR_APPEND_ELT (*p, idx, eltinit);
2234 if (!*non_constant_p)
2237 CONSTRUCTOR_NO_IMPLICIT_ZERO (init) = false;
2243 cxx_eval_vec_init (const constexpr_ctx *ctx, tree t,
2245 bool *non_constant_p, bool *overflow_p)
2247 tree atype = TREE_TYPE (t);
2248 tree init = VEC_INIT_EXPR_INIT (t);
2249 tree r = cxx_eval_vec_init_1 (ctx, atype, init,
2250 VEC_INIT_EXPR_VALUE_INIT (t),
2251 lval, non_constant_p, overflow_p);
2252 if (*non_constant_p)
2258 /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
2259 match. We want to be less strict for simple *& folding; if we have a
2260 non-const temporary that we access through a const pointer, that should
2261 work. We handle this here rather than change fold_indirect_ref_1
2262 because we're dealing with things like ADDR_EXPR of INTEGER_CST which
2263 don't really make sense outside of constant expression evaluation. Also
2264 we want to allow folding to COMPONENT_REF, which could cause trouble
2265 with TBAA in fold_indirect_ref_1.
2267 Try to keep this function synced with fold_indirect_ref_1. */
2270 cxx_fold_indirect_ref (location_t loc, tree type, tree op0, bool *empty_base)
2276 subtype = TREE_TYPE (sub);
2277 if (!POINTER_TYPE_P (subtype))
2280 if (TREE_CODE (sub) == ADDR_EXPR)
2282 tree op = TREE_OPERAND (sub, 0);
2283 tree optype = TREE_TYPE (op);
2285 /* *&CONST_DECL -> to the value of the const decl. */
2286 if (TREE_CODE (op) == CONST_DECL)
2287 return DECL_INITIAL (op);
2288 /* *&p => p; make sure to handle *&"str"[cst] here. */
2289 if (same_type_ignoring_top_level_qualifiers_p (optype, type))
2291 tree fop = fold_read_from_constant_string (op);
2297 /* *(foo *)&fooarray => fooarray[0] */
2298 else if (TREE_CODE (optype) == ARRAY_TYPE
2299 && (same_type_ignoring_top_level_qualifiers_p
2300 (type, TREE_TYPE (optype))))
2302 tree type_domain = TYPE_DOMAIN (optype);
2303 tree min_val = size_zero_node;
2304 if (type_domain && TYPE_MIN_VALUE (type_domain))
2305 min_val = TYPE_MIN_VALUE (type_domain);
2306 return build4_loc (loc, ARRAY_REF, type, op, min_val,
2307 NULL_TREE, NULL_TREE);
2309 /* *(foo *)&complexfoo => __real__ complexfoo */
2310 else if (TREE_CODE (optype) == COMPLEX_TYPE
2311 && (same_type_ignoring_top_level_qualifiers_p
2312 (type, TREE_TYPE (optype))))
2313 return fold_build1_loc (loc, REALPART_EXPR, type, op);
2314 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
2315 else if (TREE_CODE (optype) == VECTOR_TYPE
2316 && (same_type_ignoring_top_level_qualifiers_p
2317 (type, TREE_TYPE (optype))))
2319 tree part_width = TYPE_SIZE (type);
2320 tree index = bitsize_int (0);
2321 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
2323 /* Also handle conversion to an empty base class, which
2324 is represented with a NOP_EXPR. */
2325 else if (is_empty_class (type)
2326 && CLASS_TYPE_P (optype)
2327 && DERIVED_FROM_P (type, optype))
2332 /* *(foo *)&struct_with_foo_field => COMPONENT_REF */
2333 else if (RECORD_OR_UNION_TYPE_P (optype))
2335 tree field = TYPE_FIELDS (optype);
2336 for (; field; field = DECL_CHAIN (field))
2337 if (TREE_CODE (field) == FIELD_DECL
2338 && integer_zerop (byte_position (field))
2339 && (same_type_ignoring_top_level_qualifiers_p
2340 (TREE_TYPE (field), type)))
2342 return fold_build3 (COMPONENT_REF, type, op, field, NULL_TREE);
2347 else if (TREE_CODE (sub) == POINTER_PLUS_EXPR
2348 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
2350 tree op00 = TREE_OPERAND (sub, 0);
2351 tree op01 = TREE_OPERAND (sub, 1);
2354 if (TREE_CODE (op00) == ADDR_EXPR)
2357 op00 = TREE_OPERAND (op00, 0);
2358 op00type = TREE_TYPE (op00);
2360 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
2361 if (TREE_CODE (op00type) == VECTOR_TYPE
2362 && (same_type_ignoring_top_level_qualifiers_p
2363 (type, TREE_TYPE (op00type))))
2365 HOST_WIDE_INT offset = tree_to_shwi (op01);
2366 tree part_width = TYPE_SIZE (type);
2367 unsigned HOST_WIDE_INT part_widthi = tree_to_shwi (part_width)/BITS_PER_UNIT;
2368 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
2369 tree index = bitsize_int (indexi);
2371 if (offset / part_widthi < TYPE_VECTOR_SUBPARTS (op00type))
2372 return fold_build3_loc (loc,
2373 BIT_FIELD_REF, type, op00,
2377 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
2378 else if (TREE_CODE (op00type) == COMPLEX_TYPE
2379 && (same_type_ignoring_top_level_qualifiers_p
2380 (type, TREE_TYPE (op00type))))
2382 tree size = TYPE_SIZE_UNIT (type);
2383 if (tree_int_cst_equal (size, op01))
2384 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
2386 /* ((foo *)&fooarray)[1] => fooarray[1] */
2387 else if (TREE_CODE (op00type) == ARRAY_TYPE
2388 && (same_type_ignoring_top_level_qualifiers_p
2389 (type, TREE_TYPE (op00type))))
2391 tree type_domain = TYPE_DOMAIN (op00type);
2392 tree min_val = size_zero_node;
2393 if (type_domain && TYPE_MIN_VALUE (type_domain))
2394 min_val = TYPE_MIN_VALUE (type_domain);
2395 op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
2396 TYPE_SIZE_UNIT (type));
2397 op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
2398 return build4_loc (loc, ARRAY_REF, type, op00, op01,
2399 NULL_TREE, NULL_TREE);
2401 /* Also handle conversion to an empty base class, which
2402 is represented with a NOP_EXPR. */
2403 else if (is_empty_class (type)
2404 && CLASS_TYPE_P (op00type)
2405 && DERIVED_FROM_P (type, op00type))
2410 /* ((foo *)&struct_with_foo_field)[1] => COMPONENT_REF */
2411 else if (RECORD_OR_UNION_TYPE_P (op00type))
2413 tree field = TYPE_FIELDS (op00type);
2414 for (; field; field = DECL_CHAIN (field))
2415 if (TREE_CODE (field) == FIELD_DECL
2416 && tree_int_cst_equal (byte_position (field), op01)
2417 && (same_type_ignoring_top_level_qualifiers_p
2418 (TREE_TYPE (field), type)))
2420 return fold_build3 (COMPONENT_REF, type, op00,
2427 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
2428 else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
2429 && (same_type_ignoring_top_level_qualifiers_p
2430 (type, TREE_TYPE (TREE_TYPE (subtype)))))
2433 tree min_val = size_zero_node;
2434 tree newsub = cxx_fold_indirect_ref (loc, TREE_TYPE (subtype), sub, NULL);
2438 sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub);
2439 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
2440 if (type_domain && TYPE_MIN_VALUE (type_domain))
2441 min_val = TYPE_MIN_VALUE (type_domain);
2442 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
2450 cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t,
2452 bool *non_constant_p, bool *overflow_p)
2454 tree orig_op0 = TREE_OPERAND (t, 0);
2455 tree op0 = cxx_eval_constant_expression (ctx, orig_op0,
2456 /*lval*/false, non_constant_p,
2458 bool empty_base = false;
2461 /* Don't VERIFY_CONSTANT here. */
2462 if (*non_constant_p)
2465 r = cxx_fold_indirect_ref (EXPR_LOCATION (t), TREE_TYPE (t), op0,
2469 r = cxx_eval_constant_expression (ctx, r,
2470 lval, non_constant_p, overflow_p);
2475 if (TREE_CODE (sub) == ADDR_EXPR)
2477 /* We couldn't fold to a constant value. Make sure it's not
2478 something we should have been able to fold. */
2479 gcc_assert (!same_type_ignoring_top_level_qualifiers_p
2480 (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t)));
2481 /* DR 1188 says we don't have to deal with this. */
2483 error ("accessing value of %qE through a %qT glvalue in a "
2484 "constant expression", build_fold_indirect_ref (sub),
2486 *non_constant_p = true;
2491 /* If we're pulling out the value of an empty base, make sure
2492 that the whole object is constant and then return an empty
2494 if (empty_base && !lval)
2496 VERIFY_CONSTANT (r);
2497 r = build_constructor (TREE_TYPE (t), NULL);
2498 TREE_CONSTANT (r) = true;
2503 if (lval && op0 != orig_op0)
2504 return build1 (INDIRECT_REF, TREE_TYPE (t), op0);
2506 VERIFY_CONSTANT (t);
2512 /* Complain about R, a VAR_DECL, not being usable in a constant expression.
2513 Shared between potential_constant_expression and
2514 cxx_eval_constant_expression. */
2517 non_const_var_error (tree r)
2519 tree type = TREE_TYPE (r);
2520 error ("the value of %qD is not usable in a constant "
2522 /* Avoid error cascade. */
2523 if (DECL_INITIAL (r) == error_mark_node)
2525 if (DECL_DECLARED_CONSTEXPR_P (r))
2526 inform (DECL_SOURCE_LOCATION (r),
2527 "%qD used in its own initializer", r);
2528 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
2530 if (!CP_TYPE_CONST_P (type))
2531 inform (DECL_SOURCE_LOCATION (r),
2532 "%q#D is not const", r);
2533 else if (CP_TYPE_VOLATILE_P (type))
2534 inform (DECL_SOURCE_LOCATION (r),
2535 "%q#D is volatile", r);
2536 else if (!DECL_INITIAL (r)
2537 || !TREE_CONSTANT (DECL_INITIAL (r)))
2538 inform (DECL_SOURCE_LOCATION (r),
2539 "%qD was not initialized with a constant "
2546 if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r))
2547 inform (DECL_SOURCE_LOCATION (r),
2548 "%qD was not declared %<constexpr%>", r);
2550 inform (DECL_SOURCE_LOCATION (r),
2551 "%qD does not have integral or enumeration type",
2556 /* Subroutine of cxx_eval_constant_expression.
2557 Like cxx_eval_unary_expression, except for trinary expressions. */
2560 cxx_eval_trinary_expression (const constexpr_ctx *ctx, tree t,
2562 bool *non_constant_p, bool *overflow_p)
2568 for (i = 0; i < 3; i++)
2570 args[i] = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, i),
2572 non_constant_p, overflow_p);
2573 VERIFY_CONSTANT (args[i]);
2576 val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t),
2577 args[0], args[1], args[2]);
2578 if (val == NULL_TREE)
2580 VERIFY_CONSTANT (val);
2585 var_in_constexpr_fn (tree t)
2587 tree ctx = DECL_CONTEXT (t);
2588 return (cxx_dialect >= cxx14 && ctx && TREE_CODE (ctx) == FUNCTION_DECL
2589 && DECL_DECLARED_CONSTEXPR_P (ctx));
2592 /* Evaluate an INIT_EXPR or MODIFY_EXPR. */
2595 cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
2597 bool *non_constant_p, bool *overflow_p)
2599 constexpr_ctx new_ctx = *ctx;
2601 /* First we figure out where we're storing to. */
2602 tree target = TREE_OPERAND (t, 0);
2603 target = cxx_eval_constant_expression (ctx, target,
2605 non_constant_p, overflow_p);
2606 if (*non_constant_p)
2609 /* And then find the underlying variable. */
2610 vec<tree,va_gc> *refs = make_tree_vector();
2611 tree object = NULL_TREE;
2612 for (tree probe = target; object == NULL_TREE; )
2614 switch (TREE_CODE (probe))
2619 vec_safe_push (refs, TREE_OPERAND (probe, 1));
2620 vec_safe_push (refs, TREE_TYPE (probe));
2621 probe = TREE_OPERAND (probe, 0);
2629 /* And then find/build up our initializer for the path to the subobject
2630 we're initializing. */
2632 if (DECL_P (object))
2633 valp = ctx->values->get (object);
2638 /* A constant-expression cannot modify objects from outside the
2639 constant-expression. */
2641 error ("modification of %qE is not a constant-expression", object);
2642 *non_constant_p = true;
2645 tree type = TREE_TYPE (object);
2646 while (!refs->is_empty())
2648 if (*valp == NULL_TREE)
2650 *valp = build_constructor (type, NULL);
2651 CONSTRUCTOR_NO_IMPLICIT_ZERO (*valp) = true;
2656 ce.index = refs->pop();
2657 ce.value = NULL_TREE;
2659 unsigned HOST_WIDE_INT idx = 0;
2660 constructor_elt *cep = NULL;
2662 vec_safe_iterate (CONSTRUCTOR_ELTS (*valp), idx, &cep);
2665 if (cp_tree_equal (ce.index, cep->index))
2668 cep = vec_safe_push (CONSTRUCTOR_ELTS (*valp), ce);
2671 release_tree_vector (refs);
2673 if ((AGGREGATE_TYPE_P (TREE_TYPE (t)) || VECTOR_TYPE_P (TREE_TYPE (t))))
2675 /* Create a new CONSTRUCTOR in case evaluation of the initializer
2676 wants to modify it. */
2677 *valp = new_ctx.ctor = build_constructor (TREE_TYPE (t), NULL);
2678 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx.ctor) = true;
2679 new_ctx.object = target;
2682 tree init = cxx_eval_constant_expression (&new_ctx, TREE_OPERAND (t, 1),
2684 non_constant_p, overflow_p);
2685 if (target == object)
2686 /* The hash table might have moved since the get earlier. */
2687 ctx->values->put (object, init);
2691 if (*non_constant_p)
2699 /* Evaluate a ++ or -- expression. */
2702 cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t,
2704 bool *non_constant_p, bool *overflow_p)
2706 enum tree_code code = TREE_CODE (t);
2707 tree type = TREE_TYPE (t);
2708 tree op = TREE_OPERAND (t, 0);
2709 tree offset = TREE_OPERAND (t, 1);
2710 gcc_assert (TREE_CONSTANT (offset));
2712 /* The operand as an lvalue. */
2713 op = cxx_eval_constant_expression (ctx, op, true,
2714 non_constant_p, overflow_p);
2716 /* The operand as an rvalue. */
2717 tree val = rvalue (op);
2718 val = cxx_eval_constant_expression (ctx, val, false,
2719 non_constant_p, overflow_p);
2720 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
2721 a local array in a constexpr function. */
2722 bool ptr = POINTER_TYPE_P (TREE_TYPE (val));
2724 VERIFY_CONSTANT (val);
2726 /* The modified value. */
2727 bool inc = (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR);
2729 if (POINTER_TYPE_P (type))
2731 /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
2732 offset = convert_to_ptrofftype (offset);
2734 offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset);
2735 mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset);
2738 mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset);
2740 VERIFY_CONSTANT (mod);
2742 /* Storing the modified value. */
2743 tree store = build2 (MODIFY_EXPR, type, op, mod);
2744 cxx_eval_constant_expression (ctx, store,
2745 true, non_constant_p, overflow_p);
2747 /* And the value of the expression. */
2748 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
2750 /* Prefix ops are lvalues. */
2754 /* But we optimize when the caller wants an rvalue. */
2758 /* Postfix ops are rvalues. */
2762 /* Predicates for the meaning of *jump_target. */
2765 returns (tree *jump_target)
2768 && TREE_CODE (*jump_target) == RETURN_EXPR;
2772 breaks (tree *jump_target)
2775 && TREE_CODE (*jump_target) == LABEL_DECL
2776 && LABEL_DECL_BREAK (*jump_target);
2780 continues (tree *jump_target)
2783 && TREE_CODE (*jump_target) == LABEL_DECL
2784 && LABEL_DECL_CONTINUE (*jump_target);
2788 switches (tree *jump_target)
2791 && TREE_CODE (*jump_target) == INTEGER_CST;
2794 /* Subroutine of cxx_eval_statement_list. Determine whether the statement
2795 at I matches *jump_target. If we're looking for a case label and we see
2796 the default label, copy I into DEFAULT_LABEL. */
2799 label_matches (tree *jump_target, tree_stmt_iterator i,
2800 tree_stmt_iterator& default_label)
2802 tree stmt = tsi_stmt (i);
2803 switch (TREE_CODE (*jump_target))
2806 if (TREE_CODE (stmt) == LABEL_EXPR
2807 && LABEL_EXPR_LABEL (stmt) == *jump_target)
2812 if (TREE_CODE (stmt) == CASE_LABEL_EXPR)
2814 if (!CASE_LOW (stmt))
2816 else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt)))
2827 /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
2828 semantics, for switch, break, continue, and return. */
2831 cxx_eval_statement_list (const constexpr_ctx *ctx, tree t,
2832 bool *non_constant_p, bool *overflow_p,
2835 tree_stmt_iterator i;
2836 tree_stmt_iterator default_label = tree_stmt_iterator();
2838 /* In a statement-expression we want to return the last value. */
2842 local_target = NULL_TREE;
2843 jump_target = &local_target;
2845 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
2848 tree stmt = tsi_stmt (i);
2851 if (TREE_CODE (stmt) == STATEMENT_LIST)
2852 /* The label we want might be inside. */;
2853 else if (label_matches (jump_target, i, default_label))
2855 *jump_target = NULL_TREE;
2859 r = cxx_eval_constant_expression (ctx, stmt, false,
2860 non_constant_p, overflow_p,
2862 if (*non_constant_p)
2864 if (returns (jump_target) || breaks (jump_target))
2867 if (switches (jump_target) && !tsi_end_p (default_label))
2870 *jump_target = NULL_TREE;
2876 /* Evaluate a LOOP_EXPR for side-effects. Handles break and return
2877 semantics; continue semantics are covered by cxx_eval_statement_list. */
2880 cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t,
2881 bool *non_constant_p, bool *overflow_p,
2884 tree body = TREE_OPERAND (t, 0);
2887 cxx_eval_statement_list (ctx, body,
2888 non_constant_p, overflow_p, jump_target);
2889 if (returns (jump_target) || breaks (jump_target) || *non_constant_p)
2892 if (breaks (jump_target))
2893 *jump_target = NULL_TREE;
2897 /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
2901 cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t,
2902 bool *non_constant_p, bool *overflow_p,
2905 tree cond = TREE_OPERAND (t, 0);
2906 cond = cxx_eval_constant_expression (ctx, cond, false,
2907 non_constant_p, overflow_p);
2908 VERIFY_CONSTANT (cond);
2909 *jump_target = cond;
2911 tree body = TREE_OPERAND (t, 1);
2912 cxx_eval_statement_list (ctx, body,
2913 non_constant_p, overflow_p, jump_target);
2914 if (breaks (jump_target) || switches (jump_target))
2915 *jump_target = NULL_TREE;
2919 /* Attempt to reduce the expression T to a constant value.
2920 On failure, issue diagnostic and return error_mark_node. */
2921 /* FIXME unify with c_fully_fold */
2922 /* FIXME overflow_p is too global */
2925 cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
2927 bool *non_constant_p, bool *overflow_p,
2930 constexpr_ctx new_ctx;
2933 if (t == error_mark_node)
2935 *non_constant_p = true;
2938 if (CONSTANT_CLASS_P (t))
2940 if (TREE_CODE (t) == PTRMEM_CST)
2941 t = cplus_expand_constant (t);
2942 else if (TREE_OVERFLOW (t) && (!flag_permissive || ctx->quiet))
2947 switch (TREE_CODE (t))
2952 /* We ask for an rvalue for the RESULT_DECL when indirecting
2953 through an invisible reference. */
2954 gcc_assert (DECL_BY_REFERENCE (t));
2955 return (*ctx->values->get (t));
2959 /* We used to not check lval for CONST_DECL, but darwin.c uses
2960 CONST_DECL for aggregate constants. */
2964 r = decl_really_constant_value (t);
2966 r = decl_constant_value (t);
2967 if (TREE_CODE (r) == TARGET_EXPR
2968 && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR)
2969 r = TARGET_EXPR_INITIAL (r);
2970 if (TREE_CODE (r) == VAR_DECL)
2971 if (tree *p = ctx->values->get (r))
2976 non_const_var_error (r);
2977 *non_constant_p = true;
2985 case CASE_LABEL_EXPR:
2989 if (!use_new_call && ctx
2990 && ctx->call && DECL_CONTEXT (t) == ctx->call->fundef->decl)
2991 r = lookup_parameter_binding (ctx->call, t);
2992 else if (lval && TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
2994 else if (tree *p = ctx->values->get (r))
2997 /* Defer in case this is only used for its type. */;
2998 else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
2999 /* Defer, there's no lvalue->rvalue conversion. */;
3000 else if (is_empty_class (TREE_TYPE (t)))
3002 /* If the class is empty, we aren't actually loading anything. */
3003 r = build_constructor (TREE_TYPE (t), NULL);
3004 TREE_CONSTANT (r) = true;
3009 error ("%qE is not a constant expression", t);
3010 *non_constant_p = true;
3015 case AGGR_INIT_EXPR:
3016 r = cxx_eval_call_expression (ctx, t, lval,
3017 non_constant_p, overflow_p);
3022 r = DECL_EXPR_DECL (t);
3023 if (AGGREGATE_TYPE_P (TREE_TYPE (r))
3024 || VECTOR_TYPE_P (TREE_TYPE (r)))
3028 new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL);
3029 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx.ctor) = true;
3030 new_ctx.values->put (r, new_ctx.ctor);
3034 if (tree init = DECL_INITIAL (r))
3036 init = cxx_eval_constant_expression (ctx, init,
3038 non_constant_p, overflow_p);
3039 ctx->values->put (r, init);
3041 else if (ctx == &new_ctx)
3042 /* We gave it a CONSTRUCTOR above. */;
3044 ctx->values->put (r, NULL_TREE);
3049 if (!literal_type_p (TREE_TYPE (t)))
3053 error ("temporary of non-literal type %qT in a "
3054 "constant expression", TREE_TYPE (t));
3055 explain_non_literal_class (TREE_TYPE (t));
3057 *non_constant_p = true;
3060 if ((AGGREGATE_TYPE_P (TREE_TYPE (t)) || VECTOR_TYPE_P (TREE_TYPE (t))))
3062 /* We're being expanded without an explicit target, so start
3063 initializing a new object; expansion with an explicit target
3064 strips the TARGET_EXPR before we get here. */
3066 new_ctx.ctor = build_constructor (TREE_TYPE (t), NULL);
3067 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx.ctor) = true;
3068 new_ctx.object = TARGET_EXPR_SLOT (t);
3069 ctx->values->put (new_ctx.object, new_ctx.ctor);
3072 /* Pass false for 'lval' because this indicates
3073 initialization of a temporary. */
3074 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
3076 non_constant_p, overflow_p);
3077 if (!*non_constant_p)
3078 /* Adjust the type of the result to the type of the temporary. */
3079 r = adjust_temp_type (TREE_TYPE (t), r);
3082 tree slot = TARGET_EXPR_SLOT (t);
3083 ctx->values->put (slot, r);
3091 /* In C++11 constexpr evaluation we are looking for the value,
3092 not the side-effect of the initialization. */
3093 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
3095 non_constant_p, overflow_p);
3098 /* else fall through */
3100 r = cxx_eval_store_expression (ctx, t, lval,
3101 non_constant_p, overflow_p);
3105 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
3107 non_constant_p, overflow_p);
3111 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
3113 non_constant_p, overflow_p);
3118 /* Avoid evaluating a SAVE_EXPR more than once. */
3119 if (tree *p = ctx->values->get (t))
3123 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), false,
3124 non_constant_p, overflow_p);
3125 ctx->values->put (t, r);
3129 case NON_LVALUE_EXPR:
3130 case TRY_CATCH_EXPR:
3131 case CLEANUP_POINT_EXPR:
3132 case MUST_NOT_THROW_EXPR:
3135 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
3137 non_constant_p, overflow_p,
3141 /* These differ from cxx_eval_unary_expression in that this doesn't
3142 check for a constant operand or result; an address can be
3143 constant without its operand being, and vice versa. */
3145 r = cxx_eval_indirect_ref (ctx, t, lval,
3146 non_constant_p, overflow_p);
3151 tree oldop = TREE_OPERAND (t, 0);
3152 tree op = cxx_eval_constant_expression (ctx, oldop,
3154 non_constant_p, overflow_p);
3155 /* Don't VERIFY_CONSTANT here. */
3156 if (*non_constant_p)
3158 gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR);
3159 /* This function does more aggressive folding than fold itself. */
3160 r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
3161 if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
3169 case FIX_TRUNC_EXPR:
3174 case TRUTH_NOT_EXPR:
3175 case FIXED_CONVERT_EXPR:
3176 r = cxx_eval_unary_expression (ctx, t, lval,
3177 non_constant_p, overflow_p);
3181 if (SIZEOF_EXPR_TYPE_P (t))
3182 r = cxx_sizeof_or_alignof_type (TREE_TYPE (TREE_OPERAND (t, 0)),
3183 SIZEOF_EXPR, false);
3184 else if (TYPE_P (TREE_OPERAND (t, 0)))
3185 r = cxx_sizeof_or_alignof_type (TREE_OPERAND (t, 0), SIZEOF_EXPR,
3188 r = cxx_sizeof_or_alignof_expr (TREE_OPERAND (t, 0), SIZEOF_EXPR,
3190 if (r == error_mark_node)
3192 VERIFY_CONSTANT (r);
3197 /* check_return_expr sometimes wraps a TARGET_EXPR in a
3198 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
3199 introduced by build_call_a. */
3200 tree op0 = TREE_OPERAND (t, 0);
3201 tree op1 = TREE_OPERAND (t, 1);
3203 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
3204 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
3205 r = cxx_eval_constant_expression (ctx, op0,
3206 lval, non_constant_p, overflow_p,
3210 /* Check that the LHS is constant and then discard it. */
3211 cxx_eval_constant_expression (ctx, op0,
3212 true, non_constant_p, overflow_p,
3214 op1 = TREE_OPERAND (t, 1);
3215 r = cxx_eval_constant_expression (ctx, op1,
3216 lval, non_constant_p, overflow_p,
3222 case POINTER_PLUS_EXPR:
3226 case TRUNC_DIV_EXPR:
3228 case FLOOR_DIV_EXPR:
3229 case ROUND_DIV_EXPR:
3230 case TRUNC_MOD_EXPR:
3232 case ROUND_MOD_EXPR:
3234 case EXACT_DIV_EXPR:
3244 case TRUTH_XOR_EXPR:
3251 case UNORDERED_EXPR:
3261 r = cxx_eval_binary_expression (ctx, t, lval,
3262 non_constant_p, overflow_p);
3265 /* fold can introduce non-IF versions of these; still treat them as
3266 short-circuiting. */
3267 case TRUTH_AND_EXPR:
3268 case TRUTH_ANDIF_EXPR:
3269 r = cxx_eval_logical_expression (ctx, t, boolean_false_node,
3272 non_constant_p, overflow_p);
3276 case TRUTH_ORIF_EXPR:
3277 r = cxx_eval_logical_expression (ctx, t, boolean_true_node,
3280 non_constant_p, overflow_p);
3284 r = cxx_eval_array_reference (ctx, t, lval,
3285 non_constant_p, overflow_p);
3289 if (is_overloaded_fn (t))
3291 /* We can only get here in checking mode via
3292 build_non_dependent_expr, because any expression that
3293 calls or takes the address of the function will have
3294 pulled a FUNCTION_DECL out of the COMPONENT_REF. */
3295 gcc_checking_assert (ctx->quiet || errorcount);
3296 *non_constant_p = true;
3299 r = cxx_eval_component_reference (ctx, t, lval,
3300 non_constant_p, overflow_p);
3304 r = cxx_eval_bit_field_ref (ctx, t, lval,
3305 non_constant_p, overflow_p);
3310 r = cxx_eval_conditional_expression (ctx, t, lval,
3311 non_constant_p, overflow_p,
3316 if (TREE_CONSTANT (t))
3317 /* Don't re-process a constant CONSTRUCTOR, but do fold it to
3318 VECTOR_CST if applicable. */
3320 r = cxx_eval_bare_aggregate (ctx, t, lval,
3321 non_constant_p, overflow_p);
3325 /* We can get this in a defaulted constructor for a class with a
3326 non-static data member of array type. Either the initializer will
3327 be NULL, meaning default-initialization, or it will be an lvalue
3328 or xvalue of the same type, meaning direct-initialization from the
3329 corresponding member. */
3330 r = cxx_eval_vec_init (ctx, t, lval,
3331 non_constant_p, overflow_p);
3336 r = cxx_eval_trinary_expression (ctx, t, lval,
3337 non_constant_p, overflow_p);
3341 case VIEW_CONVERT_EXPR:
3344 tree oldop = TREE_OPERAND (t, 0);
3345 tree op = cxx_eval_constant_expression (ctx, oldop,
3347 non_constant_p, overflow_p);
3348 if (*non_constant_p)
3350 if (POINTER_TYPE_P (TREE_TYPE (t))
3351 && TREE_CODE (op) == INTEGER_CST
3352 && !integer_zerop (op))
3355 error_at (EXPR_LOC_OR_LOC (t, input_location),
3356 "reinterpret_cast from integer to pointer");
3357 *non_constant_p = true;
3361 /* We didn't fold at the top so we could check for ptr-int
3364 r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), op);
3365 /* Conversion of an out-of-range value has implementation-defined
3366 behavior; the language considers it different from arithmetic
3367 overflow, which is undefined. */
3368 if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op))
3369 TREE_OVERFLOW (r) = false;
3373 case EMPTY_CLASS_EXPR:
3374 /* This is good enough for a function argument that might not get
3375 used, and they can't do anything with it, so just return it. */
3378 case STATEMENT_LIST:
3380 new_ctx.ctor = new_ctx.object = NULL_TREE;
3381 return cxx_eval_statement_list (&new_ctx, t,
3382 non_constant_p, overflow_p, jump_target);
3385 return cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t),
3387 non_constant_p, overflow_p,
3390 case PREINCREMENT_EXPR:
3391 case POSTINCREMENT_EXPR:
3392 case PREDECREMENT_EXPR:
3393 case POSTDECREMENT_EXPR:
3394 return cxx_eval_increment_expression (ctx, t,
3395 lval, non_constant_p, overflow_p);
3401 case VEC_DELETE_EXPR:
3404 /* GCC internal stuff. */
3407 case WITH_CLEANUP_EXPR:
3408 case NON_DEPENDENT_EXPR:
3412 error_at (EXPR_LOC_OR_LOC (t, input_location),
3413 "expression %qE is not a constant-expression", t);
3414 *non_constant_p = true;
3417 case PLACEHOLDER_EXPR:
3418 if (!ctx || !ctx->ctor || (lval && !ctx->object))
3420 /* A placeholder without a referent. We can get here when
3421 checking whether NSDMIs are noexcept, or in massage_init_elt;
3422 just say it's non-constant for now. */
3423 gcc_assert (ctx->quiet);
3424 *non_constant_p = true;
3429 /* Use of the value or address of the current object. We could
3430 use ctx->object unconditionally, but using ctx->ctor when we
3431 can is a minor optimization. */
3432 tree ctor = lval ? ctx->object : ctx->ctor;
3433 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3434 (TREE_TYPE (t), TREE_TYPE (ctor)));
3435 return cxx_eval_constant_expression
3437 non_constant_p, overflow_p);
3442 *jump_target = TREE_OPERAND (t, 0);
3443 gcc_assert (breaks (jump_target) || continues (jump_target));
3447 cxx_eval_loop_expr (ctx, t,
3448 non_constant_p, overflow_p, jump_target);
3452 cxx_eval_switch_expr (ctx, t,
3453 non_constant_p, overflow_p, jump_target);
3457 if (STATEMENT_CODE_P (TREE_CODE (t)))
3459 /* This function doesn't know how to deal with pre-genericize
3460 statements; this can only happen with statement-expressions,
3461 so for now just fail. */
3463 error_at (EXPR_LOCATION (t),
3464 "statement is not a constant-expression");
3467 internal_error ("unexpected expression %qE of kind %s", t,
3468 get_tree_code_name (TREE_CODE (t)));
3469 *non_constant_p = true;
3473 if (r == error_mark_node)
3474 *non_constant_p = true;
3476 if (*non_constant_p)
3483 cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant,
3484 bool strict = true, tree object = NULL_TREE)
3486 bool non_constant_p = false;
3487 bool overflow_p = false;
3488 hash_map<tree,tree> map;
3489 constexpr_ctx ctx = { NULL, &map, NULL, NULL, allow_non_constant, strict };
3490 tree type = initialized_type (t);
3492 if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
3494 /* In C++14 an NSDMI can participate in aggregate initialization,
3495 and can refer to the address of the object being initialized, so
3496 we need to pass in the relevant VAR_DECL if we want to do the
3497 evaluation in a single pass. The evaluation will dynamically
3498 update ctx.values for the VAR_DECL. We use the same strategy
3499 for C++11 constexpr constructors that refer to the object being
3501 ctx.ctor = build_constructor (type, NULL);
3502 CONSTRUCTOR_NO_IMPLICIT_ZERO (ctx.ctor) = true;
3505 if (TREE_CODE (t) == TARGET_EXPR)
3506 object = TARGET_EXPR_SLOT (t);
3507 else if (TREE_CODE (t) == AGGR_INIT_EXPR)
3508 object = AGGR_INIT_EXPR_SLOT (t);
3510 ctx.object = object;
3512 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3513 (type, TREE_TYPE (object)));
3514 if (object && DECL_P (object))
3515 map.put (object, ctx.ctor);
3516 if (TREE_CODE (r) == TARGET_EXPR)
3517 /* Avoid creating another CONSTRUCTOR when we expand the
3519 r = TARGET_EXPR_INITIAL (r);
3522 r = cxx_eval_constant_expression (&ctx, r,
3523 false, &non_constant_p, &overflow_p);
3525 verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p);
3527 /* Mutable logic is a bit tricky: we want to allow initialization of
3528 constexpr variables with mutable members, but we can't copy those
3529 members to another constexpr variable. */
3530 if (TREE_CODE (r) == CONSTRUCTOR
3531 && CONSTRUCTOR_MUTABLE_POISON (r))
3533 if (!allow_non_constant)
3534 error ("%qE is not a constant expression because it refers to "
3535 "mutable subobjects of %qT", t, type);
3536 non_constant_p = true;
3539 /* Technically we should check this for all subexpressions, but that
3540 runs into problems with our internal representation of pointer
3541 subtraction and the 5.19 rules are still in flux. */
3542 if (CONVERT_EXPR_CODE_P (TREE_CODE (r))
3543 && ARITHMETIC_TYPE_P (TREE_TYPE (r))
3544 && TREE_CODE (TREE_OPERAND (r, 0)) == ADDR_EXPR)
3546 if (!allow_non_constant)
3547 error ("conversion from pointer type %qT "
3548 "to arithmetic type %qT in a constant-expression",
3549 TREE_TYPE (TREE_OPERAND (r, 0)), TREE_TYPE (r));
3550 non_constant_p = true;
3553 if (!non_constant_p && overflow_p)
3554 non_constant_p = true;
3556 if (non_constant_p && !allow_non_constant)
3557 return error_mark_node;
3558 else if (non_constant_p && TREE_CONSTANT (r))
3560 /* This isn't actually constant, so unset TREE_CONSTANT. */
3563 else if (TREE_CODE (r) == CONSTRUCTOR)
3564 r = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (r), r);
3566 r = build_nop (TREE_TYPE (r), r);
3567 TREE_CONSTANT (r) = false;
3569 else if (non_constant_p || r == t)
3572 if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
3574 if (TREE_CODE (t) == TARGET_EXPR
3575 && TARGET_EXPR_INITIAL (t) == r)
3579 r = get_target_expr (r);
3580 TREE_CONSTANT (r) = true;
3588 /* Returns true if T is a valid subexpression of a constant expression,
3589 even if it isn't itself a constant expression. */
3592 is_sub_constant_expr (tree t)
3594 bool non_constant_p = false;
3595 bool overflow_p = false;
3596 hash_map <tree, tree> map;
3597 constexpr_ctx ctx = { NULL, &map, NULL, NULL, true, true };
3598 cxx_eval_constant_expression (&ctx, t, false, &non_constant_p,
3600 return !non_constant_p && !overflow_p;
3603 /* If T represents a constant expression returns its reduced value.
3604 Otherwise return error_mark_node. If T is dependent, then
3608 cxx_constant_value (tree t, tree decl)
3610 return cxx_eval_outermost_constant_expr (t, false, true, decl);
3613 /* If T is a constant expression, returns its reduced value.
3614 Otherwise, if T does not have TREE_CONSTANT set, returns T.
3615 Otherwise, returns a version of T without TREE_CONSTANT. */
3618 maybe_constant_value (tree t, tree decl)
3622 if (instantiation_dependent_expression_p (t)
3623 || type_unknown_p (t)
3624 || BRACE_ENCLOSED_INITIALIZER_P (t)
3625 || !potential_constant_expression (t))
3627 if (TREE_OVERFLOW_P (t))
3629 t = build_nop (TREE_TYPE (t), t);
3630 TREE_CONSTANT (t) = false;
3635 r = cxx_eval_outermost_constant_expr (t, true, true, decl);
3636 #ifdef ENABLE_CHECKING
3637 /* cp_tree_equal looks through NOPs, so allow them. */
3639 || CONVERT_EXPR_P (t)
3640 || TREE_CODE (t) == VIEW_CONVERT_EXPR
3641 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
3642 || !cp_tree_equal (r, t));
3647 /* Like maybe_constant_value but first fully instantiate the argument.
3649 Note: this is equivalent to instantiate_non_dependent_expr_sfinae
3650 (t, tf_none) followed by maybe_constant_value but is more efficient,
3651 because calls instantiation_dependent_expression_p and
3652 potential_constant_expression at most once. */
3655 fold_non_dependent_expr (tree t)
3660 /* If we're in a template, but T isn't value dependent, simplify
3661 it. We're supposed to treat:
3663 template <typename T> void f(T[1 + 1]);
3664 template <typename T> void f(T[2]);
3666 as two declarations of the same function, for example. */
3667 if (processing_template_decl)
3669 if (!instantiation_dependent_expression_p (t)
3670 && potential_constant_expression (t))
3672 processing_template_decl_sentinel s;
3673 t = instantiate_non_dependent_expr_internal (t, tf_none);
3675 if (type_unknown_p (t)
3676 || BRACE_ENCLOSED_INITIALIZER_P (t))
3678 if (TREE_OVERFLOW_P (t))
3680 t = build_nop (TREE_TYPE (t), t);
3681 TREE_CONSTANT (t) = false;
3686 tree r = cxx_eval_outermost_constant_expr (t, true, true, NULL_TREE);
3687 #ifdef ENABLE_CHECKING
3688 /* cp_tree_equal looks through NOPs, so allow them. */
3690 || CONVERT_EXPR_P (t)
3691 || TREE_CODE (t) == VIEW_CONVERT_EXPR
3692 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
3693 || !cp_tree_equal (r, t));
3697 else if (TREE_OVERFLOW_P (t))
3699 t = build_nop (TREE_TYPE (t), t);
3700 TREE_CONSTANT (t) = false;
3705 return maybe_constant_value (t);
3708 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
3709 than wrapped in a TARGET_EXPR. */
3712 maybe_constant_init (tree t, tree decl)
3714 if (TREE_CODE (t) == EXPR_STMT)
3715 t = TREE_OPERAND (t, 0);
3716 if (TREE_CODE (t) == CONVERT_EXPR
3717 && VOID_TYPE_P (TREE_TYPE (t)))
3718 t = TREE_OPERAND (t, 0);
3719 if (TREE_CODE (t) == INIT_EXPR)
3720 t = TREE_OPERAND (t, 1);
3721 if (instantiation_dependent_expression_p (t)
3722 || type_unknown_p (t)
3723 || BRACE_ENCLOSED_INITIALIZER_P (t)
3724 || !potential_static_init_expression (t))
3725 /* Don't try to evaluate it. */;
3727 t = cxx_eval_outermost_constant_expr (t, true, false, decl);
3728 if (TREE_CODE (t) == TARGET_EXPR)
3730 tree init = TARGET_EXPR_INITIAL (t);
3731 if (TREE_CODE (init) == CONSTRUCTOR)
3738 /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
3739 /* Return true if the object referred to by REF has automatic or thread
3742 enum { ck_ok, ck_bad, ck_unknown };
3744 check_automatic_or_tls (tree ref)
3747 HOST_WIDE_INT bitsize, bitpos;
3749 int volatilep = 0, unsignedp = 0;
3750 tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset,
3751 &mode, &unsignedp, &volatilep, false);
3754 /* If there isn't a decl in the middle, we don't know the linkage here,
3755 and this isn't a constant expression anyway. */
3758 dk = decl_storage_duration (decl);
3759 return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok;
3763 /* Return true if T denotes a potentially constant expression. Issue
3764 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
3765 an lvalue-rvalue conversion is implied.
3767 C++0x [expr.const] used to say
3769 6 An expression is a potential constant expression if it is
3770 a constant expression where all occurrences of function
3771 parameters are replaced by arbitrary constant expressions
3772 of the appropriate type.
3774 2 A conditional expression is a constant expression unless it
3775 involves one of the following as a potentially evaluated
3776 subexpression (3.2), but subexpressions of logical AND (5.14),
3777 logical OR (5.15), and conditional (5.16) operations that are
3778 not evaluated are not considered. */
3781 potential_constant_expression_1 (tree t, bool want_rval, bool strict,
3782 tsubst_flags_t flags)
3784 #define RECUR(T,RV) potential_constant_expression_1 ((T), (RV), strict, flags)
3785 enum { any = false, rval = true };
3789 if (t == error_mark_node)
3793 if (TREE_THIS_VOLATILE (t))
3795 if (flags & tf_error)
3796 error ("expression %qE has side-effects", t);
3799 if (CONSTANT_CLASS_P (t))
3802 switch (TREE_CODE (t))
3808 case TEMPLATE_ID_EXPR:
3811 case CASE_LABEL_EXPR:
3817 case TEMPLATE_PARM_INDEX:
3819 case IDENTIFIER_NODE:
3820 case USERDEF_LITERAL:
3821 /* We can see a FIELD_DECL in a pointer-to-member expression. */
3826 case PLACEHOLDER_EXPR:
3831 case AGGR_INIT_EXPR:
3833 /* -- an invocation of a function other than a constexpr function
3834 or a constexpr constructor. */
3836 tree fun = get_function_named_in_call (t);
3837 const int nargs = call_expr_nargs (t);
3840 if (fun == NULL_TREE)
3842 if (TREE_CODE (t) == CALL_EXPR
3843 && CALL_EXPR_FN (t) == NULL_TREE)
3844 switch (CALL_EXPR_IFN (t))
3846 /* These should be ignored, they are optimized away from
3847 constexpr functions. */
3848 case IFN_UBSAN_NULL:
3849 case IFN_UBSAN_BOUNDS:
3850 case IFN_UBSAN_VPTR:
3855 /* fold_call_expr can't do anything with IFN calls. */
3856 if (flags & tf_error)
3857 error_at (EXPR_LOC_OR_LOC (t, input_location),
3858 "call to internal function");
3861 if (is_overloaded_fn (fun))
3863 if (TREE_CODE (fun) == FUNCTION_DECL)
3865 if (builtin_valid_in_constant_expr_p (fun))
3867 if (!DECL_DECLARED_CONSTEXPR_P (fun)
3868 /* Allow any built-in function; if the expansion
3869 isn't constant, we'll deal with that then. */
3870 && !is_builtin_fn (fun))
3872 if (flags & tf_error)
3874 error_at (EXPR_LOC_OR_LOC (t, input_location),
3875 "call to non-constexpr function %qD", fun);
3876 explain_invalid_constexpr_fn (fun);
3880 /* A call to a non-static member function takes the address
3881 of the object as the first argument. But in a constant
3882 expression the address will be folded away, so look
3884 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
3885 && !DECL_CONSTRUCTOR_P (fun))
3887 tree x = get_nth_callarg (t, 0);
3888 if (is_this_parameter (x))
3890 else if (!RECUR (x, rval))
3897 if (!RECUR (fun, true))
3899 fun = get_first_fn (fun);
3901 /* Skip initial arguments to base constructors. */
3902 if (DECL_BASE_CONSTRUCTOR_P (fun))
3903 i = num_artificial_parms_for (fun);
3904 fun = DECL_ORIGIN (fun);
3908 if (RECUR (fun, rval))
3909 /* Might end up being a constant function pointer. */;
3913 for (; i < nargs; ++i)
3915 tree x = get_nth_callarg (t, i);
3916 /* In a template, reference arguments haven't been converted to
3917 REFERENCE_TYPE and we might not even know if the parameter
3918 is a reference, so accept lvalue constants too. */
3919 bool rv = processing_template_decl ? any : rval;
3926 case NON_LVALUE_EXPR:
3927 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
3928 -- an lvalue of integral type that refers to a non-volatile
3929 const variable or static data member initialized with
3930 constant expressions, or
3932 -- an lvalue of literal type that refers to non-volatile
3933 object defined with constexpr, or that refers to a
3934 sub-object of such an object; */
3935 return RECUR (TREE_OPERAND (t, 0), rval);
3939 && !decl_constant_var_p (t)
3941 || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t))
3942 || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t))
3943 && !var_in_constexpr_fn (t)
3944 && !type_dependent_expression_p (t))
3946 if (flags & tf_error)
3947 non_const_var_error (t);
3954 case VIEW_CONVERT_EXPR:
3955 /* -- a reinterpret_cast. FIXME not implemented, and this rule
3956 may change to something more specific to type-punning (DR 1312). */
3958 tree from = TREE_OPERAND (t, 0);
3959 if (POINTER_TYPE_P (TREE_TYPE (t))
3960 && TREE_CODE (from) == INTEGER_CST
3961 && !integer_zerop (from))
3963 if (flags & tf_error)
3964 error_at (EXPR_LOC_OR_LOC (t, input_location),
3965 "reinterpret_cast from integer to pointer");
3968 return (RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR));
3972 /* -- a unary operator & that is applied to an lvalue that
3973 designates an object with thread or automatic storage
3975 t = TREE_OPERAND (t, 0);
3977 if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t))
3978 /* A pointer-to-member constant. */
3982 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
3983 any checking here, as we might dereference the pointer later. If
3984 we remove this code, also remove check_automatic_or_tls. */
3985 i = check_automatic_or_tls (t);
3990 if (flags & tf_error)
3991 error ("address-of an object %qE with thread local or "
3992 "automatic storage is not a constant expression", t);
3996 return RECUR (t, any);
4002 /* -- a class member access unless its postfix-expression is
4003 of literal type or of pointer to literal type. */
4004 /* This test would be redundant, as it follows from the
4005 postfix-expression being a potential constant expression. */
4006 return RECUR (TREE_OPERAND (t, 0), want_rval);
4008 case EXPR_PACK_EXPANSION:
4009 return RECUR (PACK_EXPANSION_PATTERN (t), want_rval);
4013 tree x = TREE_OPERAND (t, 0);
4015 if (is_this_parameter (x))
4017 if (DECL_CONTEXT (x)
4018 && !DECL_DECLARED_CONSTEXPR_P (DECL_CONTEXT (x)))
4020 if (flags & tf_error)
4021 error ("use of %<this%> in a constant expression");
4026 return RECUR (x, rval);
4029 case STATEMENT_LIST:
4031 tree_stmt_iterator i;
4032 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
4034 if (!RECUR (tsi_stmt (i), any))
4042 if (cxx_dialect < cxx14)
4044 if (!RECUR (TREE_OPERAND (t, 0), any))
4046 if (!RECUR (TREE_OPERAND (t, 1), rval))
4051 if (cxx_dialect < cxx14)
4053 if (!RECUR (TREE_OPERAND (t, 0), rval))
4055 if (!RECUR (TREE_OPERAND (t, 2), rval))
4060 if (!RECUR (IF_COND (t), rval))
4062 if (!RECUR (THEN_CLAUSE (t), any))
4064 if (!RECUR (ELSE_CLAUSE (t), any))
4069 if (!RECUR (DO_COND (t), rval))
4071 if (!RECUR (DO_BODY (t), any))
4076 if (!RECUR (FOR_INIT_STMT (t), any))
4078 if (!RECUR (FOR_COND (t), rval))
4080 if (!RECUR (FOR_EXPR (t), any))
4082 if (!RECUR (FOR_BODY (t), any))
4087 if (!RECUR (WHILE_COND (t), rval))
4089 if (!RECUR (WHILE_BODY (t), any))
4094 if (!RECUR (SWITCH_STMT_COND (t), rval))
4096 if (!RECUR (SWITCH_STMT_BODY (t), any))
4101 return RECUR (STMT_EXPR_STMT (t), rval);
4104 case DYNAMIC_CAST_EXPR:
4105 case PSEUDO_DTOR_EXPR:
4109 case VEC_DELETE_EXPR:
4112 case OMP_ATOMIC_READ:
4113 case OMP_ATOMIC_CAPTURE_OLD:
4114 case OMP_ATOMIC_CAPTURE_NEW:
4115 /* GCC internal stuff. */
4118 case TRANSACTION_EXPR:
4121 if (flags & tf_error)
4122 error ("expression %qE is not a constant-expression", t);
4126 /* -- a typeid expression whose operand is of polymorphic
4129 tree e = TREE_OPERAND (t, 0);
4130 if (!TYPE_P (e) && !type_dependent_expression_p (e)
4131 && TYPE_POLYMORPHIC_P (TREE_TYPE (e)))
4133 if (flags & tf_error)
4134 error ("typeid-expression is not a constant expression "
4135 "because %qE is of polymorphic type", e);
4142 /* -- a subtraction where both operands are pointers. */
4143 if (TYPE_PTR_P (TREE_OPERAND (t, 0))
4144 && TYPE_PTR_P (TREE_OPERAND (t, 1)))
4146 if (flags & tf_error)
4147 error ("difference of two pointer expressions is not "
4148 "a constant expression");
4160 /* -- a relational or equality operator where at least
4161 one of the operands is a pointer. */
4162 if (TYPE_PTR_P (TREE_OPERAND (t, 0))
4163 || TYPE_PTR_P (TREE_OPERAND (t, 1)))
4165 if (flags & tf_error)
4166 error ("pointer comparison expression is not a "
4167 "constant expression");
4173 case PREINCREMENT_EXPR:
4174 case POSTINCREMENT_EXPR:
4175 case PREDECREMENT_EXPR:
4176 case POSTDECREMENT_EXPR:
4177 if (cxx_dialect < cxx14)
4183 if (TYPE_P (TREE_OPERAND (t, 0)))
4185 /* else fall through. */
4191 case FIX_TRUNC_EXPR:
4195 case TRUTH_NOT_EXPR:
4196 case FIXED_CONVERT_EXPR:
4197 case UNARY_PLUS_EXPR:
4199 return RECUR (TREE_OPERAND (t, 0), rval);
4202 case CONST_CAST_EXPR:
4203 case STATIC_CAST_EXPR:
4204 case REINTERPRET_CAST_EXPR:
4205 case IMPLICIT_CONV_EXPR:
4206 if (cxx_dialect < cxx11
4207 && !dependent_type_p (TREE_TYPE (t))
4208 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t)))
4209 /* In C++98, a conversion to non-integral type can't be part of a
4210 constant expression. */
4212 if (flags & tf_error)
4213 error ("cast to non-integral type %qT in a constant expression",
4218 return (RECUR (TREE_OPERAND (t, 0),
4219 TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE));
4222 return RECUR (BIND_EXPR_BODY (t), want_rval);
4224 case WITH_CLEANUP_EXPR:
4225 case CLEANUP_POINT_EXPR:
4226 case MUST_NOT_THROW_EXPR:
4227 case TRY_CATCH_EXPR:
4232 case NON_DEPENDENT_EXPR:
4233 /* For convenience. */
4235 return RECUR (TREE_OPERAND (t, 0), want_rval);
4238 return RECUR (TREE_OPERAND (t, 1), want_rval);
4241 if (!literal_type_p (TREE_TYPE (t)))
4243 if (flags & tf_error)
4245 error ("temporary of non-literal type %qT in a "
4246 "constant expression", TREE_TYPE (t));
4247 explain_non_literal_class (TREE_TYPE (t));
4252 return RECUR (TREE_OPERAND (t, 1), rval);
4256 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
4257 constructor_elt *ce;
4258 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
4259 if (!RECUR (ce->value, want_rval))
4266 gcc_assert (TREE_PURPOSE (t) == NULL_TREE
4267 || DECL_P (TREE_PURPOSE (t)));
4268 if (!RECUR (TREE_VALUE (t), want_rval))
4270 if (TREE_CHAIN (t) == NULL_TREE)
4272 return RECUR (TREE_CHAIN (t), want_rval);
4275 case TRUNC_DIV_EXPR:
4277 case FLOOR_DIV_EXPR:
4278 case ROUND_DIV_EXPR:
4279 case TRUNC_MOD_EXPR:
4281 case ROUND_MOD_EXPR:
4283 tree denom = TREE_OPERAND (t, 1);
4284 if (!RECUR (denom, rval))
4286 /* We can't call cxx_eval_outermost_constant_expr on an expression
4287 that hasn't been through instantiate_non_dependent_expr yet. */
4288 if (!processing_template_decl)
4289 denom = cxx_eval_outermost_constant_expr (denom, true);
4290 if (integer_zerop (denom))
4292 if (flags & tf_error)
4293 error ("division by zero is not a constant-expression");
4299 return RECUR (TREE_OPERAND (t, 0), want_rval);
4305 /* check_return_expr sometimes wraps a TARGET_EXPR in a
4306 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
4307 introduced by build_call_a. */
4308 tree op0 = TREE_OPERAND (t, 0);
4309 tree op1 = TREE_OPERAND (t, 1);
4311 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
4312 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
4313 return RECUR (op0, want_rval);
4318 /* If the first operand is the non-short-circuit constant, look at
4319 the second operand; otherwise we only care about the first one for
4321 case TRUTH_AND_EXPR:
4322 case TRUTH_ANDIF_EXPR:
4323 tmp = boolean_true_node;
4326 case TRUTH_ORIF_EXPR:
4327 tmp = boolean_false_node;
4330 tree op = TREE_OPERAND (t, 0);
4331 if (!RECUR (op, rval))
4333 if (!processing_template_decl)
4334 op = cxx_eval_outermost_constant_expr (op, true);
4335 if (tree_int_cst_equal (op, tmp))
4336 return RECUR (TREE_OPERAND (t, 1), rval);
4343 case POINTER_PLUS_EXPR:
4345 case EXACT_DIV_EXPR:
4355 case TRUTH_XOR_EXPR:
4356 case UNORDERED_EXPR:
4369 case ARRAY_RANGE_REF:
4373 for (i = 0; i < 2; ++i)
4374 if (!RECUR (TREE_OPERAND (t, i), want_rval))
4378 case CILK_SYNC_STMT:
4379 case CILK_SPAWN_STMT:
4380 case ARRAY_NOTATION_REF:
4385 for (i = 0; i < 3; ++i)
4386 if (!RECUR (TREE_OPERAND (t, i), true))
4392 /* If the condition is a known constant, we know which of the legs we
4393 care about; otherwise we only require that the condition and
4394 either of the legs be potentially constant. */
4395 tmp = TREE_OPERAND (t, 0);
4396 if (!RECUR (tmp, rval))
4398 if (!processing_template_decl)
4399 tmp = cxx_eval_outermost_constant_expr (tmp, true);
4400 if (integer_zerop (tmp))
4401 return RECUR (TREE_OPERAND (t, 2), want_rval);
4402 else if (TREE_CODE (tmp) == INTEGER_CST)
4403 return RECUR (TREE_OPERAND (t, 1), want_rval);
4404 for (i = 1; i < 3; ++i)
4405 if (potential_constant_expression_1 (TREE_OPERAND (t, i),
4406 want_rval, strict, tf_none))
4408 if (flags & tf_error)
4409 error ("expression %qE is not a constant-expression", t);
4413 if (VEC_INIT_EXPR_IS_CONSTEXPR (t))
4415 if (flags & tf_error)
4417 error ("non-constant array initialization");
4418 diagnose_non_constexpr_vec_init (t);
4423 if (objc_is_property_ref (t))
4426 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t)));
4433 /* The main entry point to the above. */
4436 potential_constant_expression (tree t)
4438 return potential_constant_expression_1 (t, false, true, tf_none);
4442 potential_static_init_expression (tree t)
4444 return potential_constant_expression_1 (t, false, false, tf_none);
4447 /* As above, but require a constant rvalue. */
4450 potential_rvalue_constant_expression (tree t)
4452 return potential_constant_expression_1 (t, true, true, tf_none);
4455 /* Like above, but complain about non-constant expressions. */
4458 require_potential_constant_expression (tree t)
4460 return potential_constant_expression_1 (t, false, true, tf_warning_or_error);
4463 /* Cross product of the above. */
4466 require_potential_rvalue_constant_expression (tree t)
4468 return potential_constant_expression_1 (t, true, true, tf_warning_or_error);
4471 #include "gt-cp-constexpr.h"