* constexpr.c (cxx_eval_vec_init_1): Pass tf_none if ctx->quiet.
[official-gcc.git] / gcc / cp / constexpr.c
blob8c6ec555ca9aecda603fd25fd581ca645c843894
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-2018 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)
12 any later version.
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/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "cp-tree.h"
27 #include "varasm.h"
28 #include "c-family/c-objc.h"
29 #include "tree-iterator.h"
30 #include "gimplify.h"
31 #include "builtins.h"
32 #include "tree-inline.h"
33 #include "ubsan.h"
34 #include "gimple-fold.h"
35 #include "timevar.h"
37 static bool verify_constant (tree, bool, bool *, bool *);
38 #define VERIFY_CONSTANT(X) \
39 do { \
40 if (verify_constant ((X), ctx->quiet, non_constant_p, overflow_p)) \
41 return t; \
42 } while (0)
44 /* Returns true iff FUN is an instantiation of a constexpr function
45 template or a defaulted constexpr function. */
47 bool
48 is_instantiation_of_constexpr (tree fun)
50 return ((DECL_TEMPLOID_INSTANTIATION (fun)
51 && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun)))
52 || (DECL_DEFAULTED_FN (fun)
53 && DECL_DECLARED_CONSTEXPR_P (fun)));
56 /* Return true if T is a literal type. */
58 bool
59 literal_type_p (tree t)
61 if (SCALAR_TYPE_P (t)
62 || VECTOR_TYPE_P (t)
63 || TYPE_REF_P (t)
64 || (VOID_TYPE_P (t) && cxx_dialect >= cxx14))
65 return true;
66 if (CLASS_TYPE_P (t))
68 t = complete_type (t);
69 gcc_assert (COMPLETE_TYPE_P (t) || errorcount);
70 return CLASSTYPE_LITERAL_P (t);
72 if (TREE_CODE (t) == ARRAY_TYPE)
73 return literal_type_p (strip_array_types (t));
74 return false;
77 /* If DECL is a variable declared `constexpr', require its type
78 be literal. Return error_mark_node if we give an error, the
79 DECL otherwise. */
81 tree
82 ensure_literal_type_for_constexpr_object (tree decl)
84 tree type = TREE_TYPE (decl);
85 if (VAR_P (decl)
86 && (DECL_DECLARED_CONSTEXPR_P (decl)
87 || var_in_constexpr_fn (decl))
88 && !processing_template_decl)
90 tree stype = strip_array_types (type);
91 if (CLASS_TYPE_P (stype) && !COMPLETE_TYPE_P (complete_type (stype)))
92 /* Don't complain here, we'll complain about incompleteness
93 when we try to initialize the variable. */;
94 else if (type_uses_auto (type))
95 /* We don't know the actual type yet. */;
96 else if (!literal_type_p (type))
98 if (DECL_DECLARED_CONSTEXPR_P (decl))
100 error ("the type %qT of %<constexpr%> variable %qD "
101 "is not literal", type, decl);
102 explain_non_literal_class (type);
103 decl = error_mark_node;
105 else
107 if (!is_instantiation_of_constexpr (current_function_decl))
109 error ("variable %qD of non-literal type %qT in %<constexpr%> "
110 "function", decl, type);
111 explain_non_literal_class (type);
112 decl = error_mark_node;
114 cp_function_chain->invalid_constexpr = true;
117 else if (DECL_DECLARED_CONSTEXPR_P (decl)
118 && variably_modified_type_p (type, NULL_TREE))
120 error ("%<constexpr%> variable %qD has variably-modified type %qT",
121 decl, type);
122 decl = error_mark_node;
125 return decl;
128 /* Representation of entries in the constexpr function definition table. */
130 struct GTY((for_user)) constexpr_fundef {
131 tree decl;
132 tree body;
135 struct constexpr_fundef_hasher : ggc_ptr_hash<constexpr_fundef>
137 static hashval_t hash (constexpr_fundef *);
138 static bool equal (constexpr_fundef *, constexpr_fundef *);
141 /* This table holds all constexpr function definitions seen in
142 the current translation unit. */
144 static GTY (()) hash_table<constexpr_fundef_hasher> *constexpr_fundef_table;
146 /* Utility function used for managing the constexpr function table.
147 Return true if the entries pointed to by P and Q are for the
148 same constexpr function. */
150 inline bool
151 constexpr_fundef_hasher::equal (constexpr_fundef *lhs, constexpr_fundef *rhs)
153 return lhs->decl == rhs->decl;
156 /* Utility function used for managing the constexpr function table.
157 Return a hash value for the entry pointed to by Q. */
159 inline hashval_t
160 constexpr_fundef_hasher::hash (constexpr_fundef *fundef)
162 return DECL_UID (fundef->decl);
165 /* Return a previously saved definition of function FUN. */
167 static constexpr_fundef *
168 retrieve_constexpr_fundef (tree fun)
170 constexpr_fundef fundef = { NULL, NULL };
171 if (constexpr_fundef_table == NULL)
172 return NULL;
174 fundef.decl = fun;
175 return constexpr_fundef_table->find (&fundef);
178 /* Check whether the parameter and return types of FUN are valid for a
179 constexpr function, and complain if COMPLAIN. */
181 bool
182 is_valid_constexpr_fn (tree fun, bool complain)
184 bool ret = true;
186 if (DECL_INHERITED_CTOR (fun)
187 && TREE_CODE (fun) == TEMPLATE_DECL)
189 ret = false;
190 if (complain)
191 error ("inherited constructor %qD is not %<constexpr%>",
192 DECL_INHERITED_CTOR (fun));
194 else
196 for (tree parm = FUNCTION_FIRST_USER_PARM (fun);
197 parm != NULL_TREE; parm = TREE_CHAIN (parm))
198 if (!literal_type_p (TREE_TYPE (parm)))
200 ret = false;
201 if (complain)
203 error ("invalid type for parameter %d of %<constexpr%> "
204 "function %q+#D", DECL_PARM_INDEX (parm), fun);
205 explain_non_literal_class (TREE_TYPE (parm));
210 if (LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun)) && cxx_dialect < cxx17)
212 ret = false;
213 if (complain)
214 inform (DECL_SOURCE_LOCATION (fun),
215 "lambdas are implicitly %<constexpr%> only in C++17 and later");
217 else if (!DECL_CONSTRUCTOR_P (fun))
219 tree rettype = TREE_TYPE (TREE_TYPE (fun));
220 if (!literal_type_p (rettype))
222 ret = false;
223 if (complain)
225 error ("invalid return type %qT of %<constexpr%> function %q+D",
226 rettype, fun);
227 explain_non_literal_class (rettype);
231 /* C++14 DR 1684 removed this restriction. */
232 if (cxx_dialect < cxx14
233 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
234 && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun)))
236 ret = false;
237 if (complain
238 && pedwarn (DECL_SOURCE_LOCATION (fun), OPT_Wpedantic,
239 "enclosing class of %<constexpr%> non-static member "
240 "function %q+#D is not a literal type", fun))
241 explain_non_literal_class (DECL_CONTEXT (fun));
244 else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun)))
246 ret = false;
247 if (complain)
248 error ("%q#T has virtual base classes", DECL_CONTEXT (fun));
251 return ret;
254 /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF
255 for a member of an anonymous aggregate, INIT is the initializer for that
256 member, and VEC_OUTER is the vector of constructor elements for the class
257 whose constructor we are processing. Add the initializer to the vector
258 and return true to indicate success. */
260 static bool
261 build_anon_member_initialization (tree member, tree init,
262 vec<constructor_elt, va_gc> **vec_outer)
264 /* MEMBER presents the relevant fields from the inside out, but we need
265 to build up the initializer from the outside in so that we can reuse
266 previously built CONSTRUCTORs if this is, say, the second field in an
267 anonymous struct. So we use a vec as a stack. */
268 auto_vec<tree, 2> fields;
271 fields.safe_push (TREE_OPERAND (member, 1));
272 member = TREE_OPERAND (member, 0);
274 while (ANON_AGGR_TYPE_P (TREE_TYPE (member))
275 && TREE_CODE (member) == COMPONENT_REF);
277 /* VEC has the constructor elements vector for the context of FIELD.
278 If FIELD is an anonymous aggregate, we will push inside it. */
279 vec<constructor_elt, va_gc> **vec = vec_outer;
280 tree field;
281 while (field = fields.pop(),
282 ANON_AGGR_TYPE_P (TREE_TYPE (field)))
284 tree ctor;
285 /* If there is already an outer constructor entry for the anonymous
286 aggregate FIELD, use it; otherwise, insert one. */
287 if (vec_safe_is_empty (*vec)
288 || (*vec)->last().index != field)
290 ctor = build_constructor (TREE_TYPE (field), NULL);
291 CONSTRUCTOR_APPEND_ELT (*vec, field, ctor);
293 else
294 ctor = (*vec)->last().value;
295 vec = &CONSTRUCTOR_ELTS (ctor);
298 /* Now we're at the innermost field, the one that isn't an anonymous
299 aggregate. Add its initializer to the CONSTRUCTOR and we're done. */
300 gcc_assert (fields.is_empty());
301 CONSTRUCTOR_APPEND_ELT (*vec, field, init);
303 return true;
306 /* Subroutine of build_constexpr_constructor_member_initializers.
307 The expression tree T represents a data member initialization
308 in a (constexpr) constructor definition. Build a pairing of
309 the data member with its initializer, and prepend that pair
310 to the existing initialization pair INITS. */
312 static bool
313 build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec)
315 tree member, init;
316 if (TREE_CODE (t) == CLEANUP_POINT_EXPR)
317 t = TREE_OPERAND (t, 0);
318 if (TREE_CODE (t) == EXPR_STMT)
319 t = TREE_OPERAND (t, 0);
320 if (t == error_mark_node)
321 return false;
322 if (TREE_CODE (t) == STATEMENT_LIST)
324 tree_stmt_iterator i;
325 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
327 if (! build_data_member_initialization (tsi_stmt (i), vec))
328 return false;
330 return true;
332 if (TREE_CODE (t) == CLEANUP_STMT)
334 /* We can't see a CLEANUP_STMT in a constructor for a literal class,
335 but we can in a constexpr constructor for a non-literal class. Just
336 ignore it; either all the initialization will be constant, in which
337 case the cleanup can't run, or it can't be constexpr.
338 Still recurse into CLEANUP_BODY. */
339 return build_data_member_initialization (CLEANUP_BODY (t), vec);
341 if (TREE_CODE (t) == CONVERT_EXPR)
342 t = TREE_OPERAND (t, 0);
343 if (TREE_CODE (t) == INIT_EXPR
344 /* vptr initialization shows up as a MODIFY_EXPR. In C++14 we only
345 use what this function builds for cx_check_missing_mem_inits, and
346 assignment in the ctor body doesn't count. */
347 || (cxx_dialect < cxx14 && TREE_CODE (t) == MODIFY_EXPR))
349 member = TREE_OPERAND (t, 0);
350 init = break_out_target_exprs (TREE_OPERAND (t, 1));
352 else if (TREE_CODE (t) == CALL_EXPR)
354 tree fn = get_callee_fndecl (t);
355 if (!fn || !DECL_CONSTRUCTOR_P (fn))
356 /* We're only interested in calls to subobject constructors. */
357 return true;
358 member = CALL_EXPR_ARG (t, 0);
359 /* We don't use build_cplus_new here because it complains about
360 abstract bases. Leaving the call unwrapped means that it has the
361 wrong type, but cxx_eval_constant_expression doesn't care. */
362 init = break_out_target_exprs (t);
364 else if (TREE_CODE (t) == BIND_EXPR)
365 return build_data_member_initialization (BIND_EXPR_BODY (t), vec);
366 else
367 /* Don't add anything else to the CONSTRUCTOR. */
368 return true;
369 if (INDIRECT_REF_P (member))
370 member = TREE_OPERAND (member, 0);
371 if (TREE_CODE (member) == NOP_EXPR)
373 tree op = member;
374 STRIP_NOPS (op);
375 if (TREE_CODE (op) == ADDR_EXPR)
377 gcc_assert (same_type_ignoring_top_level_qualifiers_p
378 (TREE_TYPE (TREE_TYPE (op)),
379 TREE_TYPE (TREE_TYPE (member))));
380 /* Initializing a cv-qualified member; we need to look through
381 the const_cast. */
382 member = op;
384 else if (op == current_class_ptr
385 && (same_type_ignoring_top_level_qualifiers_p
386 (TREE_TYPE (TREE_TYPE (member)),
387 current_class_type)))
388 /* Delegating constructor. */
389 member = op;
390 else
392 /* This is an initializer for an empty base; keep it for now so
393 we can check it in cxx_eval_bare_aggregate. */
394 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member))));
397 if (TREE_CODE (member) == ADDR_EXPR)
398 member = TREE_OPERAND (member, 0);
399 if (TREE_CODE (member) == COMPONENT_REF)
401 tree aggr = TREE_OPERAND (member, 0);
402 if (TREE_CODE (aggr) == VAR_DECL)
403 /* Initializing a local variable, don't add anything. */
404 return true;
405 if (TREE_CODE (aggr) != COMPONENT_REF)
406 /* Normal member initialization. */
407 member = TREE_OPERAND (member, 1);
408 else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr)))
409 /* Initializing a member of an anonymous union. */
410 return build_anon_member_initialization (member, init, vec);
411 else
412 /* We're initializing a vtable pointer in a base. Leave it as
413 COMPONENT_REF so we remember the path to get to the vfield. */
414 gcc_assert (TREE_TYPE (member) == vtbl_ptr_type_node);
417 /* Value-initialization can produce multiple initializers for the
418 same field; use the last one. */
419 if (!vec_safe_is_empty (*vec) && (*vec)->last().index == member)
420 (*vec)->last().value = init;
421 else
422 CONSTRUCTOR_APPEND_ELT (*vec, member, init);
423 return true;
426 /* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval.
427 In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a
428 BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */
430 static bool
431 check_constexpr_bind_expr_vars (tree t)
433 gcc_assert (TREE_CODE (t) == BIND_EXPR);
435 for (tree var = BIND_EXPR_VARS (t); var; var = DECL_CHAIN (var))
436 if (TREE_CODE (var) == TYPE_DECL
437 && DECL_IMPLICIT_TYPEDEF_P (var)
438 && !LAMBDA_TYPE_P (TREE_TYPE (var)))
439 return false;
440 return true;
443 /* Subroutine of check_constexpr_ctor_body. */
445 static bool
446 check_constexpr_ctor_body_1 (tree last, tree list)
448 switch (TREE_CODE (list))
450 case DECL_EXPR:
451 if (TREE_CODE (DECL_EXPR_DECL (list)) == USING_DECL
452 || TREE_CODE (DECL_EXPR_DECL (list)) == TYPE_DECL)
453 return true;
454 return false;
456 case CLEANUP_POINT_EXPR:
457 return check_constexpr_ctor_body (last, TREE_OPERAND (list, 0),
458 /*complain=*/false);
460 case BIND_EXPR:
461 if (!check_constexpr_bind_expr_vars (list)
462 || !check_constexpr_ctor_body (last, BIND_EXPR_BODY (list),
463 /*complain=*/false))
464 return false;
465 return true;
467 case USING_STMT:
468 case STATIC_ASSERT:
469 case DEBUG_BEGIN_STMT:
470 return true;
472 default:
473 return false;
477 /* Make sure that there are no statements after LAST in the constructor
478 body represented by LIST. */
480 bool
481 check_constexpr_ctor_body (tree last, tree list, bool complain)
483 /* C++14 doesn't require a constexpr ctor to have an empty body. */
484 if (cxx_dialect >= cxx14)
485 return true;
487 bool ok = true;
488 if (TREE_CODE (list) == STATEMENT_LIST)
490 tree_stmt_iterator i = tsi_last (list);
491 for (; !tsi_end_p (i); tsi_prev (&i))
493 tree t = tsi_stmt (i);
494 if (t == last)
495 break;
496 if (!check_constexpr_ctor_body_1 (last, t))
498 ok = false;
499 break;
503 else if (list != last
504 && !check_constexpr_ctor_body_1 (last, list))
505 ok = false;
506 if (!ok)
508 if (complain)
509 error ("%<constexpr%> constructor does not have empty body");
510 DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false;
512 return ok;
515 /* V is a vector of constructor elements built up for the base and member
516 initializers of a constructor for TYPE. They need to be in increasing
517 offset order, which they might not be yet if TYPE has a primary base
518 which is not first in the base-clause or a vptr and at least one base
519 all of which are non-primary. */
521 static vec<constructor_elt, va_gc> *
522 sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v)
524 tree pri = CLASSTYPE_PRIMARY_BINFO (type);
525 tree field_type;
526 unsigned i;
527 constructor_elt *ce;
529 if (pri)
530 field_type = BINFO_TYPE (pri);
531 else if (TYPE_CONTAINS_VPTR_P (type))
532 field_type = vtbl_ptr_type_node;
533 else
534 return v;
536 /* Find the element for the primary base or vptr and move it to the
537 beginning of the vec. */
538 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
539 if (TREE_TYPE (ce->index) == field_type)
540 break;
542 if (i > 0 && i < vec_safe_length (v))
544 vec<constructor_elt, va_gc> &vref = *v;
545 constructor_elt elt = vref[i];
546 for (; i > 0; --i)
547 vref[i] = vref[i-1];
548 vref[0] = elt;
551 return v;
554 /* Build compile-time evalable representations of member-initializer list
555 for a constexpr constructor. */
557 static tree
558 build_constexpr_constructor_member_initializers (tree type, tree body)
560 vec<constructor_elt, va_gc> *vec = NULL;
561 bool ok = true;
562 while (true)
563 switch (TREE_CODE (body))
565 case MUST_NOT_THROW_EXPR:
566 case EH_SPEC_BLOCK:
567 body = TREE_OPERAND (body, 0);
568 break;
570 case STATEMENT_LIST:
571 for (tree_stmt_iterator i = tsi_start (body);
572 !tsi_end_p (i); tsi_next (&i))
574 body = tsi_stmt (i);
575 if (TREE_CODE (body) == BIND_EXPR)
576 break;
578 break;
580 case BIND_EXPR:
581 body = BIND_EXPR_BODY (body);
582 goto found;
584 default:
585 gcc_unreachable ();
587 found:
588 if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
590 body = TREE_OPERAND (body, 0);
591 if (TREE_CODE (body) == EXPR_STMT)
592 body = TREE_OPERAND (body, 0);
593 if (TREE_CODE (body) == INIT_EXPR
594 && (same_type_ignoring_top_level_qualifiers_p
595 (TREE_TYPE (TREE_OPERAND (body, 0)),
596 current_class_type)))
598 /* Trivial copy. */
599 return TREE_OPERAND (body, 1);
601 ok = build_data_member_initialization (body, &vec);
603 else if (TREE_CODE (body) == STATEMENT_LIST)
605 tree_stmt_iterator i;
606 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
608 ok = build_data_member_initialization (tsi_stmt (i), &vec);
609 if (!ok)
610 break;
613 else if (TREE_CODE (body) == TRY_BLOCK)
615 error ("body of %<constexpr%> constructor cannot be "
616 "a function-try-block");
617 return error_mark_node;
619 else if (EXPR_P (body))
620 ok = build_data_member_initialization (body, &vec);
621 else
622 gcc_assert (errorcount > 0);
623 if (ok)
625 if (vec_safe_length (vec) > 0)
627 /* In a delegating constructor, return the target. */
628 constructor_elt *ce = &(*vec)[0];
629 if (ce->index == current_class_ptr)
631 body = ce->value;
632 vec_free (vec);
633 return body;
636 vec = sort_constexpr_mem_initializers (type, vec);
637 return build_constructor (type, vec);
639 else
640 return error_mark_node;
643 /* We have an expression tree T that represents a call, either CALL_EXPR
644 or AGGR_INIT_EXPR. If the call is lexically to a named function,
645 retrun the _DECL for that function. */
647 static tree
648 get_function_named_in_call (tree t)
650 tree fun = cp_get_callee (t);
651 if (fun && TREE_CODE (fun) == ADDR_EXPR
652 && TREE_CODE (TREE_OPERAND (fun, 0)) == FUNCTION_DECL)
653 fun = TREE_OPERAND (fun, 0);
654 return fun;
657 /* Subroutine of register_constexpr_fundef. BODY is the body of a function
658 declared to be constexpr, or a sub-statement thereof. Returns the
659 return value if suitable, error_mark_node for a statement not allowed in
660 a constexpr function, or NULL_TREE if no return value was found. */
662 tree
663 constexpr_fn_retval (tree body)
665 switch (TREE_CODE (body))
667 case STATEMENT_LIST:
669 tree_stmt_iterator i;
670 tree expr = NULL_TREE;
671 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
673 tree s = constexpr_fn_retval (tsi_stmt (i));
674 if (s == error_mark_node)
675 return error_mark_node;
676 else if (s == NULL_TREE)
677 /* Keep iterating. */;
678 else if (expr)
679 /* Multiple return statements. */
680 return error_mark_node;
681 else
682 expr = s;
684 return expr;
687 case RETURN_EXPR:
688 return break_out_target_exprs (TREE_OPERAND (body, 0));
690 case DECL_EXPR:
692 tree decl = DECL_EXPR_DECL (body);
693 if (TREE_CODE (decl) == USING_DECL
694 /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */
695 || DECL_ARTIFICIAL (decl))
696 return NULL_TREE;
697 return error_mark_node;
700 case CLEANUP_POINT_EXPR:
701 return constexpr_fn_retval (TREE_OPERAND (body, 0));
703 case BIND_EXPR:
704 if (!check_constexpr_bind_expr_vars (body))
705 return error_mark_node;
706 return constexpr_fn_retval (BIND_EXPR_BODY (body));
708 case USING_STMT:
709 case DEBUG_BEGIN_STMT:
710 return NULL_TREE;
712 case CALL_EXPR:
714 tree fun = get_function_named_in_call (body);
715 if (fun != NULL_TREE
716 && DECL_FUNCTION_CODE (fun) == BUILT_IN_UNREACHABLE)
717 return NULL_TREE;
719 /* Fallthru. */
721 default:
722 return error_mark_node;
726 /* Subroutine of register_constexpr_fundef. BODY is the DECL_SAVED_TREE of
727 FUN; do the necessary transformations to turn it into a single expression
728 that we can store in the hash table. */
730 static tree
731 massage_constexpr_body (tree fun, tree body)
733 if (DECL_CONSTRUCTOR_P (fun))
734 body = build_constexpr_constructor_member_initializers
735 (DECL_CONTEXT (fun), body);
736 else if (cxx_dialect < cxx14)
738 if (TREE_CODE (body) == EH_SPEC_BLOCK)
739 body = EH_SPEC_STMTS (body);
740 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
741 body = TREE_OPERAND (body, 0);
742 body = constexpr_fn_retval (body);
744 return body;
747 /* CTYPE is a type constructed from BODY. Return true if some
748 bases/fields are uninitialized, and complain if COMPLAIN. */
750 static bool
751 cx_check_missing_mem_inits (tree ctype, tree body, bool complain)
753 unsigned nelts = 0;
755 if (body)
757 if (TREE_CODE (body) != CONSTRUCTOR)
758 return false;
759 nelts = CONSTRUCTOR_NELTS (body);
761 tree field = TYPE_FIELDS (ctype);
763 if (TREE_CODE (ctype) == UNION_TYPE)
765 if (nelts == 0 && next_initializable_field (field))
767 if (complain)
768 error ("%<constexpr%> constructor for union %qT must "
769 "initialize exactly one non-static data member", ctype);
770 return true;
772 return false;
775 /* Iterate over the CONSTRUCTOR, checking any missing fields don't
776 need an explicit initialization. */
777 bool bad = false;
778 for (unsigned i = 0; i <= nelts; ++i)
780 tree index = NULL_TREE;
781 if (i < nelts)
783 index = CONSTRUCTOR_ELT (body, i)->index;
784 /* Skip base and vtable inits. */
785 if (TREE_CODE (index) != FIELD_DECL
786 || DECL_ARTIFICIAL (index))
787 continue;
790 for (; field != index; field = DECL_CHAIN (field))
792 tree ftype;
793 if (TREE_CODE (field) != FIELD_DECL)
794 continue;
795 if (DECL_UNNAMED_BIT_FIELD (field))
796 continue;
797 if (DECL_ARTIFICIAL (field))
798 continue;
799 if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
801 /* Recurse to check the anonummous aggregate member. */
802 bad |= cx_check_missing_mem_inits
803 (TREE_TYPE (field), NULL_TREE, complain);
804 if (bad && !complain)
805 return true;
806 continue;
808 ftype = strip_array_types (TREE_TYPE (field));
809 if (type_has_constexpr_default_constructor (ftype))
811 /* It's OK to skip a member with a trivial constexpr ctor.
812 A constexpr ctor that isn't trivial should have been
813 added in by now. */
814 gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype)
815 || errorcount != 0);
816 continue;
818 if (!complain)
819 return true;
820 error ("member %qD must be initialized by mem-initializer "
821 "in %<constexpr%> constructor", field);
822 inform (DECL_SOURCE_LOCATION (field), "declared here");
823 bad = true;
825 if (field == NULL_TREE)
826 break;
828 if (ANON_AGGR_TYPE_P (TREE_TYPE (index)))
830 /* Check the anonymous aggregate initializer is valid. */
831 bad |= cx_check_missing_mem_inits
832 (TREE_TYPE (index), CONSTRUCTOR_ELT (body, i)->value, complain);
833 if (bad && !complain)
834 return true;
836 field = DECL_CHAIN (field);
839 return bad;
842 /* We are processing the definition of the constexpr function FUN.
843 Check that its BODY fulfills the propriate requirements and
844 enter it in the constexpr function definition table.
845 For constructor BODY is actually the TREE_LIST of the
846 member-initializer list. */
848 tree
849 register_constexpr_fundef (tree fun, tree body)
851 constexpr_fundef entry;
852 constexpr_fundef **slot;
854 if (!is_valid_constexpr_fn (fun, !DECL_GENERATED_P (fun)))
855 return NULL;
857 tree massaged = massage_constexpr_body (fun, body);
858 if (massaged == NULL_TREE || massaged == error_mark_node)
860 if (!DECL_CONSTRUCTOR_P (fun))
861 error ("body of %<constexpr%> function %qD not a return-statement",
862 fun);
863 return NULL;
866 if (!potential_rvalue_constant_expression (massaged))
868 if (!DECL_GENERATED_P (fun))
869 require_potential_rvalue_constant_expression (massaged);
870 return NULL;
873 if (DECL_CONSTRUCTOR_P (fun)
874 && cx_check_missing_mem_inits (DECL_CONTEXT (fun),
875 massaged, !DECL_GENERATED_P (fun)))
876 return NULL;
878 /* Create the constexpr function table if necessary. */
879 if (constexpr_fundef_table == NULL)
880 constexpr_fundef_table
881 = hash_table<constexpr_fundef_hasher>::create_ggc (101);
883 entry.decl = fun;
884 entry.body = body;
885 slot = constexpr_fundef_table->find_slot (&entry, INSERT);
887 gcc_assert (*slot == NULL);
888 *slot = ggc_alloc<constexpr_fundef> ();
889 **slot = entry;
891 return fun;
894 /* FUN is a non-constexpr function called in a context that requires a
895 constant expression. If it comes from a constexpr template, explain why
896 the instantiation isn't constexpr. */
898 void
899 explain_invalid_constexpr_fn (tree fun)
901 static hash_set<tree> *diagnosed;
902 tree body;
903 location_t save_loc;
904 /* Only diagnose defaulted functions, lambdas, or instantiations. */
905 if (!DECL_DEFAULTED_FN (fun)
906 && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun))
907 && !is_instantiation_of_constexpr (fun))
908 return;
909 if (diagnosed == NULL)
910 diagnosed = new hash_set<tree>;
911 if (diagnosed->add (fun))
912 /* Already explained. */
913 return;
915 save_loc = input_location;
916 if (!lambda_static_thunk_p (fun))
918 /* Diagnostics should completely ignore the static thunk, so leave
919 input_location set to our caller's location. */
920 input_location = DECL_SOURCE_LOCATION (fun);
921 inform (input_location,
922 "%qD is not usable as a %<constexpr%> function because:", fun);
924 /* First check the declaration. */
925 if (is_valid_constexpr_fn (fun, true))
927 /* Then if it's OK, the body. */
928 if (!DECL_DECLARED_CONSTEXPR_P (fun)
929 && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun)))
930 explain_implicit_non_constexpr (fun);
931 else
933 body = massage_constexpr_body (fun, DECL_SAVED_TREE (fun));
934 require_potential_rvalue_constant_expression (body);
935 if (DECL_CONSTRUCTOR_P (fun))
936 cx_check_missing_mem_inits (DECL_CONTEXT (fun), body, true);
939 input_location = save_loc;
942 /* Objects of this type represent calls to constexpr functions
943 along with the bindings of parameters to their arguments, for
944 the purpose of compile time evaluation. */
946 struct GTY((for_user)) constexpr_call {
947 /* Description of the constexpr function definition. */
948 constexpr_fundef *fundef;
949 /* Parameter bindings environment. A TREE_LIST where each TREE_PURPOSE
950 is a parameter _DECL and the TREE_VALUE is the value of the parameter.
951 Note: This arrangement is made to accommodate the use of
952 iterative_hash_template_arg (see pt.c). If you change this
953 representation, also change the hash calculation in
954 cxx_eval_call_expression. */
955 tree bindings;
956 /* Result of the call.
957 NULL means the call is being evaluated.
958 error_mark_node means that the evaluation was erroneous;
959 otherwise, the actuall value of the call. */
960 tree result;
961 /* The hash of this call; we remember it here to avoid having to
962 recalculate it when expanding the hash table. */
963 hashval_t hash;
966 struct constexpr_call_hasher : ggc_ptr_hash<constexpr_call>
968 static hashval_t hash (constexpr_call *);
969 static bool equal (constexpr_call *, constexpr_call *);
972 enum constexpr_switch_state {
973 /* Used when processing a switch for the first time by cxx_eval_switch_expr
974 and default: label for that switch has not been seen yet. */
975 css_default_not_seen,
976 /* Used when processing a switch for the first time by cxx_eval_switch_expr
977 and default: label for that switch has been seen already. */
978 css_default_seen,
979 /* Used when processing a switch for the second time by
980 cxx_eval_switch_expr, where default: label should match. */
981 css_default_processing
984 /* The constexpr expansion context. CALL is the current function
985 expansion, CTOR is the current aggregate initializer, OBJECT is the
986 object being initialized by CTOR, either a VAR_DECL or a _REF. VALUES
987 is a map of values of variables initialized within the expression. */
989 struct constexpr_ctx {
990 /* The innermost call we're evaluating. */
991 constexpr_call *call;
992 /* Values for any temporaries or local variables within the
993 constant-expression. */
994 hash_map<tree,tree> *values;
995 /* SAVE_EXPRs that we've seen within the current LOOP_EXPR. NULL if we
996 aren't inside a loop. */
997 hash_set<tree> *save_exprs;
998 /* The CONSTRUCTOR we're currently building up for an aggregate
999 initializer. */
1000 tree ctor;
1001 /* The object we're building the CONSTRUCTOR for. */
1002 tree object;
1003 /* If inside SWITCH_EXPR. */
1004 constexpr_switch_state *css_state;
1005 /* Whether we should error on a non-constant expression or fail quietly. */
1006 bool quiet;
1007 /* Whether we are strictly conforming to constant expression rules or
1008 trying harder to get a constant value. */
1009 bool strict;
1012 /* A table of all constexpr calls that have been evaluated by the
1013 compiler in this translation unit. */
1015 static GTY (()) hash_table<constexpr_call_hasher> *constexpr_call_table;
1017 static tree cxx_eval_constant_expression (const constexpr_ctx *, tree,
1018 bool, bool *, bool *, tree * = NULL);
1020 /* Compute a hash value for a constexpr call representation. */
1022 inline hashval_t
1023 constexpr_call_hasher::hash (constexpr_call *info)
1025 return info->hash;
1028 /* Return true if the objects pointed to by P and Q represent calls
1029 to the same constexpr function with the same arguments.
1030 Otherwise, return false. */
1032 bool
1033 constexpr_call_hasher::equal (constexpr_call *lhs, constexpr_call *rhs)
1035 tree lhs_bindings;
1036 tree rhs_bindings;
1037 if (lhs == rhs)
1038 return true;
1039 if (lhs->hash != rhs->hash)
1040 return false;
1041 if (!constexpr_fundef_hasher::equal (lhs->fundef, rhs->fundef))
1042 return false;
1043 lhs_bindings = lhs->bindings;
1044 rhs_bindings = rhs->bindings;
1045 while (lhs_bindings != NULL && rhs_bindings != NULL)
1047 tree lhs_arg = TREE_VALUE (lhs_bindings);
1048 tree rhs_arg = TREE_VALUE (rhs_bindings);
1049 gcc_assert (TREE_TYPE (lhs_arg) == TREE_TYPE (rhs_arg));
1050 if (!cp_tree_equal (lhs_arg, rhs_arg))
1051 return false;
1052 lhs_bindings = TREE_CHAIN (lhs_bindings);
1053 rhs_bindings = TREE_CHAIN (rhs_bindings);
1055 return lhs_bindings == rhs_bindings;
1058 /* Initialize the constexpr call table, if needed. */
1060 static void
1061 maybe_initialize_constexpr_call_table (void)
1063 if (constexpr_call_table == NULL)
1064 constexpr_call_table = hash_table<constexpr_call_hasher>::create_ggc (101);
1067 /* During constexpr CALL_EXPR evaluation, to avoid issues with sharing when
1068 a function happens to get called recursively, we unshare the callee
1069 function's body and evaluate this unshared copy instead of evaluating the
1070 original body.
1072 FUNDEF_COPIES_TABLE is a per-function freelist of these unshared function
1073 copies. The underlying data structure of FUNDEF_COPIES_TABLE is a hash_map
1074 that's keyed off of the original FUNCTION_DECL and whose value is a
1075 TREE_LIST of this function's unused copies awaiting reuse.
1077 This is not GC-deletable to avoid GC affecting UID generation. */
1079 static GTY(()) hash_map<tree, tree> *fundef_copies_table;
1081 /* Initialize FUNDEF_COPIES_TABLE if it's not initialized. */
1083 static void
1084 maybe_initialize_fundef_copies_table ()
1086 if (fundef_copies_table == NULL)
1087 fundef_copies_table = hash_map<tree,tree>::create_ggc (101);
1090 /* Reuse a copy or create a new unshared copy of the function FUN.
1091 Return this copy. We use a TREE_LIST whose PURPOSE is body, VALUE
1092 is parms, TYPE is result. */
1094 static tree
1095 get_fundef_copy (tree fun)
1097 maybe_initialize_fundef_copies_table ();
1099 tree copy;
1100 bool existed;
1101 tree *slot = &fundef_copies_table->get_or_insert (fun, &existed);
1103 if (!existed)
1105 /* There is no cached function available, or in use. We can use
1106 the function directly. That the slot is now created records
1107 that this function is now in use. */
1108 copy = build_tree_list (DECL_SAVED_TREE (fun), DECL_ARGUMENTS (fun));
1109 TREE_TYPE (copy) = DECL_RESULT (fun);
1111 else if (*slot == NULL_TREE)
1113 /* We've already used the function itself, so make a copy. */
1114 copy = build_tree_list (NULL, NULL);
1115 TREE_PURPOSE (copy) = copy_fn (fun, TREE_VALUE (copy), TREE_TYPE (copy));
1117 else
1119 /* We have a cached function available. */
1120 copy = *slot;
1121 *slot = TREE_CHAIN (copy);
1124 return copy;
1127 /* Save the copy COPY of function FUN for later reuse by
1128 get_fundef_copy(). By construction, there will always be an entry
1129 to find. */
1131 static void
1132 save_fundef_copy (tree fun, tree copy)
1134 tree *slot = fundef_copies_table->get (fun);
1135 TREE_CHAIN (copy) = *slot;
1136 *slot = copy;
1139 /* We have an expression tree T that represents a call, either CALL_EXPR
1140 or AGGR_INIT_EXPR. Return the Nth argument. */
1142 static inline tree
1143 get_nth_callarg (tree t, int n)
1145 switch (TREE_CODE (t))
1147 case CALL_EXPR:
1148 return CALL_EXPR_ARG (t, n);
1150 case AGGR_INIT_EXPR:
1151 return AGGR_INIT_EXPR_ARG (t, n);
1153 default:
1154 gcc_unreachable ();
1155 return NULL;
1159 /* Attempt to evaluate T which represents a call to a builtin function.
1160 We assume here that all builtin functions evaluate to scalar types
1161 represented by _CST nodes. */
1163 static tree
1164 cxx_eval_builtin_function_call (const constexpr_ctx *ctx, tree t, tree fun,
1165 bool lval,
1166 bool *non_constant_p, bool *overflow_p)
1168 const int nargs = call_expr_nargs (t);
1169 tree *args = (tree *) alloca (nargs * sizeof (tree));
1170 tree new_call;
1171 int i;
1173 /* Don't fold __builtin_constant_p within a constexpr function. */
1174 bool bi_const_p = (DECL_FUNCTION_CODE (fun) == BUILT_IN_CONSTANT_P);
1176 /* If we aren't requiring a constant expression, defer __builtin_constant_p
1177 in a constexpr function until we have values for the parameters. */
1178 if (bi_const_p
1179 && ctx->quiet
1180 && current_function_decl
1181 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
1183 *non_constant_p = true;
1184 return t;
1187 /* Be permissive for arguments to built-ins; __builtin_constant_p should
1188 return constant false for a non-constant argument. */
1189 constexpr_ctx new_ctx = *ctx;
1190 new_ctx.quiet = true;
1191 bool dummy1 = false, dummy2 = false;
1192 for (i = 0; i < nargs; ++i)
1194 args[i] = CALL_EXPR_ARG (t, i);
1195 /* If builtin_valid_in_constant_expr_p is true,
1196 potential_constant_expression_1 has not recursed into the arguments
1197 of the builtin, verify it here. */
1198 if (!builtin_valid_in_constant_expr_p (fun)
1199 || potential_constant_expression (args[i]))
1200 args[i] = cxx_eval_constant_expression (&new_ctx, args[i], false,
1201 &dummy1, &dummy2);
1202 if (bi_const_p)
1203 /* For __built_in_constant_p, fold all expressions with constant values
1204 even if they aren't C++ constant-expressions. */
1205 args[i] = cp_fully_fold (args[i]);
1208 bool save_ffbcp = force_folding_builtin_constant_p;
1209 force_folding_builtin_constant_p = true;
1210 new_call = fold_builtin_call_array (EXPR_LOCATION (t), TREE_TYPE (t),
1211 CALL_EXPR_FN (t), nargs, args);
1212 force_folding_builtin_constant_p = save_ffbcp;
1213 if (new_call == NULL)
1215 if (!*non_constant_p && !ctx->quiet)
1217 /* Do not allow__builtin_unreachable in constexpr function.
1218 The __builtin_unreachable call with BUILTINS_LOCATION
1219 comes from cp_maybe_instrument_return. */
1220 if (DECL_FUNCTION_CODE (fun) == BUILT_IN_UNREACHABLE
1221 && EXPR_LOCATION (t) == BUILTINS_LOCATION)
1222 error ("%<constexpr%> call flows off the end of the function");
1223 else
1225 new_call = build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t),
1226 CALL_EXPR_FN (t), nargs, args);
1227 error ("%q+E is not a constant expression", new_call);
1230 *non_constant_p = true;
1231 return t;
1234 if (!is_constant_expression (new_call))
1236 if (!*non_constant_p && !ctx->quiet)
1237 error ("%q+E is not a constant expression", new_call);
1238 *non_constant_p = true;
1239 return t;
1242 return cxx_eval_constant_expression (&new_ctx, new_call, lval,
1243 non_constant_p, overflow_p);
1246 /* TEMP is the constant value of a temporary object of type TYPE. Adjust
1247 the type of the value to match. */
1249 static tree
1250 adjust_temp_type (tree type, tree temp)
1252 if (TREE_TYPE (temp) == type)
1253 return temp;
1254 /* Avoid wrapping an aggregate value in a NOP_EXPR. */
1255 if (TREE_CODE (temp) == CONSTRUCTOR)
1256 return build_constructor (type, CONSTRUCTOR_ELTS (temp));
1257 gcc_assert (scalarish_type_p (type));
1258 return cp_fold_convert (type, temp);
1261 /* Callback for walk_tree used by unshare_constructor. */
1263 static tree
1264 find_constructor (tree *tp, int *walk_subtrees, void *)
1266 if (TYPE_P (*tp))
1267 *walk_subtrees = 0;
1268 if (TREE_CODE (*tp) == CONSTRUCTOR)
1269 return *tp;
1270 return NULL_TREE;
1273 /* If T is a CONSTRUCTOR or an expression that has a CONSTRUCTOR node as a
1274 subexpression, return an unshared copy of T. Otherwise return T. */
1276 static tree
1277 unshare_constructor (tree t)
1279 tree ctor = walk_tree (&t, find_constructor, NULL, NULL);
1280 if (ctor != NULL_TREE)
1281 return unshare_expr (t);
1282 return t;
1285 /* Subroutine of cxx_eval_call_expression.
1286 We are processing a call expression (either CALL_EXPR or
1287 AGGR_INIT_EXPR) in the context of CTX. Evaluate
1288 all arguments and bind their values to correspondings
1289 parameters, making up the NEW_CALL context. */
1291 static void
1292 cxx_bind_parameters_in_call (const constexpr_ctx *ctx, tree t,
1293 constexpr_call *new_call,
1294 bool *non_constant_p, bool *overflow_p,
1295 bool *non_constant_args)
1297 const int nargs = call_expr_nargs (t);
1298 tree fun = new_call->fundef->decl;
1299 tree parms = DECL_ARGUMENTS (fun);
1300 int i;
1301 tree *p = &new_call->bindings;
1302 for (i = 0; i < nargs; ++i)
1304 tree x, arg;
1305 tree type = parms ? TREE_TYPE (parms) : void_type_node;
1306 x = get_nth_callarg (t, i);
1307 /* For member function, the first argument is a pointer to the implied
1308 object. For a constructor, it might still be a dummy object, in
1309 which case we get the real argument from ctx. */
1310 if (i == 0 && DECL_CONSTRUCTOR_P (fun)
1311 && is_dummy_object (x))
1313 x = ctx->object;
1314 x = build_address (x);
1316 arg = cxx_eval_constant_expression (ctx, x, /*lval=*/false,
1317 non_constant_p, overflow_p);
1318 /* Don't VERIFY_CONSTANT here. */
1319 if (*non_constant_p && ctx->quiet)
1320 return;
1321 /* Just discard ellipsis args after checking their constantitude. */
1322 if (!parms)
1323 continue;
1325 if (!*non_constant_p)
1327 /* Don't share a CONSTRUCTOR that might be changed. */
1328 arg = unshare_constructor (arg);
1329 /* Make sure the binding has the same type as the parm. But
1330 only for constant args. */
1331 if (!TYPE_REF_P (type))
1332 arg = adjust_temp_type (type, arg);
1333 if (!TREE_CONSTANT (arg))
1334 *non_constant_args = true;
1335 *p = build_tree_list (parms, arg);
1336 p = &TREE_CHAIN (*p);
1338 parms = TREE_CHAIN (parms);
1342 /* Variables and functions to manage constexpr call expansion context.
1343 These do not need to be marked for PCH or GC. */
1345 /* FIXME remember and print actual constant arguments. */
1346 static vec<tree> call_stack;
1347 static int call_stack_tick;
1348 static int last_cx_error_tick;
1350 static bool
1351 push_cx_call_context (tree call)
1353 ++call_stack_tick;
1354 if (!EXPR_HAS_LOCATION (call))
1355 SET_EXPR_LOCATION (call, input_location);
1356 call_stack.safe_push (call);
1357 if (call_stack.length () > (unsigned) max_constexpr_depth)
1358 return false;
1359 return true;
1362 static void
1363 pop_cx_call_context (void)
1365 ++call_stack_tick;
1366 call_stack.pop ();
1369 vec<tree>
1370 cx_error_context (void)
1372 vec<tree> r = vNULL;
1373 if (call_stack_tick != last_cx_error_tick
1374 && !call_stack.is_empty ())
1375 r = call_stack;
1376 last_cx_error_tick = call_stack_tick;
1377 return r;
1380 /* Evaluate a call T to a GCC internal function when possible and return
1381 the evaluated result or, under the control of CTX, give an error, set
1382 NON_CONSTANT_P, and return the unevaluated call T otherwise. */
1384 static tree
1385 cxx_eval_internal_function (const constexpr_ctx *ctx, tree t,
1386 bool lval,
1387 bool *non_constant_p, bool *overflow_p)
1389 enum tree_code opcode = ERROR_MARK;
1391 switch (CALL_EXPR_IFN (t))
1393 case IFN_UBSAN_NULL:
1394 case IFN_UBSAN_BOUNDS:
1395 case IFN_UBSAN_VPTR:
1396 case IFN_FALLTHROUGH:
1397 return void_node;
1399 case IFN_ADD_OVERFLOW:
1400 opcode = PLUS_EXPR;
1401 break;
1402 case IFN_SUB_OVERFLOW:
1403 opcode = MINUS_EXPR;
1404 break;
1405 case IFN_MUL_OVERFLOW:
1406 opcode = MULT_EXPR;
1407 break;
1409 case IFN_LAUNDER:
1410 return cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0),
1411 false, non_constant_p, overflow_p);
1413 default:
1414 if (!ctx->quiet)
1415 error_at (EXPR_LOC_OR_LOC (t, input_location),
1416 "call to internal function %qE", t);
1417 *non_constant_p = true;
1418 return t;
1421 /* Evaluate constant arguments using OPCODE and return a complex
1422 number containing the result and the overflow bit. */
1423 tree arg0 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), lval,
1424 non_constant_p, overflow_p);
1425 tree arg1 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 1), lval,
1426 non_constant_p, overflow_p);
1428 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1430 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
1431 tree type = TREE_TYPE (TREE_TYPE (t));
1432 tree result = fold_binary_loc (loc, opcode, type,
1433 fold_convert_loc (loc, type, arg0),
1434 fold_convert_loc (loc, type, arg1));
1435 tree ovf
1436 = build_int_cst (type, arith_overflowed_p (opcode, type, arg0, arg1));
1437 /* Reset TREE_OVERFLOW to avoid warnings for the overflow. */
1438 if (TREE_OVERFLOW (result))
1439 TREE_OVERFLOW (result) = 0;
1441 return build_complex (TREE_TYPE (t), result, ovf);
1444 *non_constant_p = true;
1445 return t;
1448 /* Clean CONSTRUCTOR_NO_IMPLICIT_ZERO from CTOR and its sub-aggregates. */
1450 static void
1451 clear_no_implicit_zero (tree ctor)
1453 if (CONSTRUCTOR_NO_IMPLICIT_ZERO (ctor))
1455 CONSTRUCTOR_NO_IMPLICIT_ZERO (ctor) = false;
1456 tree elt; unsigned HOST_WIDE_INT idx;
1457 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), idx, elt)
1458 if (TREE_CODE (elt) == CONSTRUCTOR)
1459 clear_no_implicit_zero (elt);
1463 /* Subroutine of cxx_eval_constant_expression.
1464 Evaluate the call expression tree T in the context of OLD_CALL expression
1465 evaluation. */
1467 static tree
1468 cxx_eval_call_expression (const constexpr_ctx *ctx, tree t,
1469 bool lval,
1470 bool *non_constant_p, bool *overflow_p)
1472 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
1473 tree fun = get_function_named_in_call (t);
1474 constexpr_call new_call = { NULL, NULL, NULL, 0 };
1475 bool depth_ok;
1477 if (fun == NULL_TREE)
1478 return cxx_eval_internal_function (ctx, t, lval,
1479 non_constant_p, overflow_p);
1481 if (TREE_CODE (fun) != FUNCTION_DECL)
1483 /* Might be a constexpr function pointer. */
1484 fun = cxx_eval_constant_expression (ctx, fun,
1485 /*lval*/false, non_constant_p,
1486 overflow_p);
1487 STRIP_NOPS (fun);
1488 if (TREE_CODE (fun) == ADDR_EXPR)
1489 fun = TREE_OPERAND (fun, 0);
1491 if (TREE_CODE (fun) != FUNCTION_DECL)
1493 if (!ctx->quiet && !*non_constant_p)
1494 error_at (loc, "expression %qE does not designate a %<constexpr%> "
1495 "function", fun);
1496 *non_constant_p = true;
1497 return t;
1499 if (DECL_CLONED_FUNCTION_P (fun))
1500 fun = DECL_CLONED_FUNCTION (fun);
1502 if (is_ubsan_builtin_p (fun))
1503 return void_node;
1505 if (is_builtin_fn (fun))
1506 return cxx_eval_builtin_function_call (ctx, t, fun,
1507 lval, non_constant_p, overflow_p);
1508 if (!DECL_DECLARED_CONSTEXPR_P (fun))
1510 if (!ctx->quiet)
1512 if (!lambda_static_thunk_p (fun))
1513 error_at (loc, "call to non-%<constexpr%> function %qD", fun);
1514 explain_invalid_constexpr_fn (fun);
1516 *non_constant_p = true;
1517 return t;
1520 constexpr_ctx new_ctx = *ctx;
1521 if (DECL_CONSTRUCTOR_P (fun) && !ctx->object
1522 && TREE_CODE (t) == AGGR_INIT_EXPR)
1524 /* We want to have an initialization target for an AGGR_INIT_EXPR.
1525 If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
1526 new_ctx.object = AGGR_INIT_EXPR_SLOT (t);
1527 tree ctor = new_ctx.ctor = build_constructor (DECL_CONTEXT (fun), NULL);
1528 CONSTRUCTOR_NO_IMPLICIT_ZERO (ctor) = true;
1529 ctx->values->put (new_ctx.object, ctor);
1530 ctx = &new_ctx;
1533 /* Shortcut trivial constructor/op=. */
1534 if (trivial_fn_p (fun))
1536 tree init = NULL_TREE;
1537 if (call_expr_nargs (t) == 2)
1538 init = convert_from_reference (get_nth_callarg (t, 1));
1539 else if (TREE_CODE (t) == AGGR_INIT_EXPR
1540 && AGGR_INIT_ZERO_FIRST (t))
1541 init = build_zero_init (DECL_CONTEXT (fun), NULL_TREE, false);
1542 if (init)
1544 tree op = get_nth_callarg (t, 0);
1545 if (is_dummy_object (op))
1546 op = ctx->object;
1547 else
1548 op = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (op)), op);
1549 tree set = build2 (MODIFY_EXPR, TREE_TYPE (op), op, init);
1550 new_ctx.call = &new_call;
1551 return cxx_eval_constant_expression (&new_ctx, set, lval,
1552 non_constant_p, overflow_p);
1556 /* We can't defer instantiating the function any longer. */
1557 if (!DECL_INITIAL (fun)
1558 && DECL_TEMPLOID_INSTANTIATION (fun))
1560 location_t save_loc = input_location;
1561 input_location = loc;
1562 ++function_depth;
1563 instantiate_decl (fun, /*defer_ok*/false, /*expl_inst*/false);
1564 --function_depth;
1565 input_location = save_loc;
1568 /* If in direct recursive call, optimize definition search. */
1569 if (ctx && ctx->call && ctx->call->fundef && ctx->call->fundef->decl == fun)
1570 new_call.fundef = ctx->call->fundef;
1571 else
1573 new_call.fundef = retrieve_constexpr_fundef (fun);
1574 if (new_call.fundef == NULL || new_call.fundef->body == NULL
1575 || fun == current_function_decl)
1577 if (!ctx->quiet)
1579 /* We need to check for current_function_decl here in case we're
1580 being called during cp_fold_function, because at that point
1581 DECL_INITIAL is set properly and we have a fundef but we
1582 haven't lowered invisirefs yet (c++/70344). */
1583 if (DECL_INITIAL (fun) == error_mark_node
1584 || fun == current_function_decl)
1585 error_at (loc, "%qD called in a constant expression before its "
1586 "definition is complete", fun);
1587 else if (DECL_INITIAL (fun))
1589 /* The definition of fun was somehow unsuitable. But pretend
1590 that lambda static thunks don't exist. */
1591 if (!lambda_static_thunk_p (fun))
1592 error_at (loc, "%qD called in a constant expression", fun);
1593 explain_invalid_constexpr_fn (fun);
1595 else
1596 error_at (loc, "%qD used before its definition", fun);
1598 *non_constant_p = true;
1599 return t;
1603 bool non_constant_args = false;
1604 cxx_bind_parameters_in_call (ctx, t, &new_call,
1605 non_constant_p, overflow_p, &non_constant_args);
1606 if (*non_constant_p)
1607 return t;
1609 depth_ok = push_cx_call_context (t);
1611 tree result = NULL_TREE;
1613 constexpr_call *entry = NULL;
1614 if (depth_ok && !non_constant_args && ctx->strict)
1616 new_call.hash = iterative_hash_template_arg
1617 (new_call.bindings, constexpr_fundef_hasher::hash (new_call.fundef));
1619 /* If we have seen this call before, we are done. */
1620 maybe_initialize_constexpr_call_table ();
1621 constexpr_call **slot
1622 = constexpr_call_table->find_slot (&new_call, INSERT);
1623 entry = *slot;
1624 if (entry == NULL)
1626 /* We need to keep a pointer to the entry, not just the slot, as the
1627 slot can move in the call to cxx_eval_builtin_function_call. */
1628 *slot = entry = ggc_alloc<constexpr_call> ();
1629 *entry = new_call;
1631 /* Calls that are in progress have their result set to NULL,
1632 so that we can detect circular dependencies. */
1633 else if (entry->result == NULL)
1635 if (!ctx->quiet)
1636 error ("call has circular dependency");
1637 *non_constant_p = true;
1638 entry->result = result = error_mark_node;
1640 else
1641 result = entry->result;
1644 if (!depth_ok)
1646 if (!ctx->quiet)
1647 error ("%<constexpr%> evaluation depth exceeds maximum of %d (use "
1648 "-fconstexpr-depth= to increase the maximum)",
1649 max_constexpr_depth);
1650 *non_constant_p = true;
1651 result = error_mark_node;
1653 else
1655 if (result && result != error_mark_node)
1656 /* OK */;
1657 else if (!DECL_SAVED_TREE (fun))
1659 /* When at_eof >= 2, cgraph has started throwing away
1660 DECL_SAVED_TREE, so fail quietly. FIXME we get here because of
1661 late code generation for VEC_INIT_EXPR, which needs to be
1662 completely reconsidered. */
1663 gcc_assert (at_eof >= 2 && ctx->quiet);
1664 *non_constant_p = true;
1666 else
1668 tree body, parms, res;
1670 /* Reuse or create a new unshared copy of this function's body. */
1671 tree copy = get_fundef_copy (fun);
1672 body = TREE_PURPOSE (copy);
1673 parms = TREE_VALUE (copy);
1674 res = TREE_TYPE (copy);
1676 /* Associate the bindings with the remapped parms. */
1677 tree bound = new_call.bindings;
1678 tree remapped = parms;
1679 while (bound)
1681 tree oparm = TREE_PURPOSE (bound);
1682 tree arg = TREE_VALUE (bound);
1683 gcc_assert (DECL_NAME (remapped) == DECL_NAME (oparm));
1684 /* Don't share a CONSTRUCTOR that might be changed. */
1685 arg = unshare_constructor (arg);
1686 ctx->values->put (remapped, arg);
1687 bound = TREE_CHAIN (bound);
1688 remapped = DECL_CHAIN (remapped);
1690 /* Add the RESULT_DECL to the values map, too. */
1691 tree slot = NULL_TREE;
1692 if (DECL_BY_REFERENCE (res))
1694 slot = AGGR_INIT_EXPR_SLOT (t);
1695 tree addr = build_address (slot);
1696 addr = build_nop (TREE_TYPE (res), addr);
1697 ctx->values->put (res, addr);
1698 ctx->values->put (slot, NULL_TREE);
1700 else
1701 ctx->values->put (res, NULL_TREE);
1703 /* Track the callee's evaluated SAVE_EXPRs so that we can forget
1704 their values after the call. */
1705 constexpr_ctx ctx_with_save_exprs = *ctx;
1706 hash_set<tree> save_exprs;
1707 ctx_with_save_exprs.save_exprs = &save_exprs;
1708 ctx_with_save_exprs.call = &new_call;
1710 tree jump_target = NULL_TREE;
1711 cxx_eval_constant_expression (&ctx_with_save_exprs, body,
1712 lval, non_constant_p, overflow_p,
1713 &jump_target);
1715 if (DECL_CONSTRUCTOR_P (fun))
1716 /* This can be null for a subobject constructor call, in
1717 which case what we care about is the initialization
1718 side-effects rather than the value. We could get at the
1719 value by evaluating *this, but we don't bother; there's
1720 no need to put such a call in the hash table. */
1721 result = lval ? ctx->object : ctx->ctor;
1722 else if (VOID_TYPE_P (TREE_TYPE (res)))
1723 result = void_node;
1724 else
1726 result = *ctx->values->get (slot ? slot : res);
1727 if (result == NULL_TREE && !*non_constant_p)
1729 if (!ctx->quiet)
1730 error ("%<constexpr%> call flows off the end "
1731 "of the function");
1732 *non_constant_p = true;
1736 /* Forget the saved values of the callee's SAVE_EXPRs. */
1737 for (hash_set<tree>::iterator iter = save_exprs.begin();
1738 iter != save_exprs.end(); ++iter)
1739 ctx_with_save_exprs.values->remove (*iter);
1741 /* Remove the parms/result from the values map. Is it worth
1742 bothering to do this when the map itself is only live for
1743 one constexpr evaluation? If so, maybe also clear out
1744 other vars from call, maybe in BIND_EXPR handling? */
1745 ctx->values->remove (res);
1746 if (slot)
1747 ctx->values->remove (slot);
1748 for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
1749 ctx->values->remove (parm);
1751 /* Make the unshared function copy we used available for re-use. */
1752 save_fundef_copy (fun, copy);
1755 if (result == error_mark_node)
1756 *non_constant_p = true;
1757 if (*non_constant_p || *overflow_p)
1758 result = error_mark_node;
1759 else if (!result)
1760 result = void_node;
1761 if (entry)
1762 entry->result = result;
1765 /* The result of a constexpr function must be completely initialized. */
1766 if (TREE_CODE (result) == CONSTRUCTOR)
1767 clear_no_implicit_zero (result);
1769 pop_cx_call_context ();
1770 return unshare_constructor (result);
1773 /* FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
1775 bool
1776 reduced_constant_expression_p (tree t)
1778 if (t == NULL_TREE)
1779 return false;
1781 switch (TREE_CODE (t))
1783 case PTRMEM_CST:
1784 /* Even if we can't lower this yet, it's constant. */
1785 return true;
1787 case CONSTRUCTOR:
1788 /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
1789 tree idx, val, field; unsigned HOST_WIDE_INT i;
1790 if (CONSTRUCTOR_NO_IMPLICIT_ZERO (t))
1792 if (TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
1793 /* An initialized vector would have a VECTOR_CST. */
1794 return false;
1795 else
1796 field = next_initializable_field (TYPE_FIELDS (TREE_TYPE (t)));
1798 else
1799 field = NULL_TREE;
1800 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), i, idx, val)
1802 /* If VAL is null, we're in the middle of initializing this
1803 element. */
1804 if (!reduced_constant_expression_p (val))
1805 return false;
1806 if (field)
1808 if (idx != field)
1809 return false;
1810 field = next_initializable_field (DECL_CHAIN (field));
1813 if (field)
1814 return false;
1815 else if (CONSTRUCTOR_NO_IMPLICIT_ZERO (t))
1816 /* All the fields are initialized. */
1817 CONSTRUCTOR_NO_IMPLICIT_ZERO (t) = false;
1818 return true;
1820 default:
1821 /* FIXME are we calling this too much? */
1822 return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE;
1826 /* Some expressions may have constant operands but are not constant
1827 themselves, such as 1/0. Call this function to check for that
1828 condition.
1830 We only call this in places that require an arithmetic constant, not in
1831 places where we might have a non-constant expression that can be a
1832 component of a constant expression, such as the address of a constexpr
1833 variable that might be dereferenced later. */
1835 static bool
1836 verify_constant (tree t, bool allow_non_constant, bool *non_constant_p,
1837 bool *overflow_p)
1839 if (!*non_constant_p && !reduced_constant_expression_p (t))
1841 if (!allow_non_constant)
1842 error ("%q+E is not a constant expression", t);
1843 *non_constant_p = true;
1845 if (TREE_OVERFLOW_P (t))
1847 if (!allow_non_constant)
1849 permerror (input_location, "overflow in constant expression");
1850 /* If we're being permissive (and are in an enforcing
1851 context), ignore the overflow. */
1852 if (flag_permissive)
1853 return *non_constant_p;
1855 *overflow_p = true;
1857 return *non_constant_p;
1860 /* Check whether the shift operation with code CODE and type TYPE on LHS
1861 and RHS is undefined. If it is, give an error with an explanation,
1862 and return true; return false otherwise. */
1864 static bool
1865 cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx,
1866 enum tree_code code, tree type, tree lhs, tree rhs)
1868 if ((code != LSHIFT_EXPR && code != RSHIFT_EXPR)
1869 || TREE_CODE (lhs) != INTEGER_CST
1870 || TREE_CODE (rhs) != INTEGER_CST)
1871 return false;
1873 tree lhstype = TREE_TYPE (lhs);
1874 unsigned HOST_WIDE_INT uprec = TYPE_PRECISION (TREE_TYPE (lhs));
1876 /* [expr.shift] The behavior is undefined if the right operand
1877 is negative, or greater than or equal to the length in bits
1878 of the promoted left operand. */
1879 if (tree_int_cst_sgn (rhs) == -1)
1881 if (!ctx->quiet)
1882 permerror (loc, "right operand of shift expression %q+E is negative",
1883 build2_loc (loc, code, type, lhs, rhs));
1884 return (!flag_permissive || ctx->quiet);
1886 if (compare_tree_int (rhs, uprec) >= 0)
1888 if (!ctx->quiet)
1889 permerror (loc, "right operand of shift expression %q+E is >= than "
1890 "the precision of the left operand",
1891 build2_loc (loc, code, type, lhs, rhs));
1892 return (!flag_permissive || ctx->quiet);
1895 /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
1896 if E1 has a signed type and non-negative value, and E1x2^E2 is
1897 representable in the corresponding unsigned type of the result type,
1898 then that value, converted to the result type, is the resulting value;
1899 otherwise, the behavior is undefined. */
1900 if (code == LSHIFT_EXPR && !TYPE_UNSIGNED (lhstype)
1901 && (cxx_dialect >= cxx11))
1903 if (tree_int_cst_sgn (lhs) == -1)
1905 if (!ctx->quiet)
1906 permerror (loc,
1907 "left operand of shift expression %q+E is negative",
1908 build2_loc (loc, code, type, lhs, rhs));
1909 return (!flag_permissive || ctx->quiet);
1911 /* For signed x << y the following:
1912 (unsigned) x >> ((prec (lhs) - 1) - y)
1913 if > 1, is undefined. The right-hand side of this formula
1914 is the highest bit of the LHS that can be set (starting from 0),
1915 so that the shift doesn't overflow. We then right-shift the LHS
1916 to see whether any other bit is set making the original shift
1917 undefined -- the result is not representable in the corresponding
1918 unsigned type. */
1919 tree t = build_int_cst (unsigned_type_node, uprec - 1);
1920 t = fold_build2 (MINUS_EXPR, unsigned_type_node, t, rhs);
1921 tree ulhs = fold_convert (unsigned_type_for (lhstype), lhs);
1922 t = fold_build2 (RSHIFT_EXPR, TREE_TYPE (ulhs), ulhs, t);
1923 if (tree_int_cst_lt (integer_one_node, t))
1925 if (!ctx->quiet)
1926 permerror (loc, "shift expression %q+E overflows",
1927 build2_loc (loc, code, type, lhs, rhs));
1928 return (!flag_permissive || ctx->quiet);
1931 return false;
1934 /* Subroutine of cxx_eval_constant_expression.
1935 Attempt to reduce the unary expression tree T to a compile time value.
1936 If successful, return the value. Otherwise issue a diagnostic
1937 and return error_mark_node. */
1939 static tree
1940 cxx_eval_unary_expression (const constexpr_ctx *ctx, tree t,
1941 bool /*lval*/,
1942 bool *non_constant_p, bool *overflow_p)
1944 tree r;
1945 tree orig_arg = TREE_OPERAND (t, 0);
1946 tree arg = cxx_eval_constant_expression (ctx, orig_arg, /*lval*/false,
1947 non_constant_p, overflow_p);
1948 VERIFY_CONSTANT (arg);
1949 location_t loc = EXPR_LOCATION (t);
1950 enum tree_code code = TREE_CODE (t);
1951 tree type = TREE_TYPE (t);
1952 r = fold_unary_loc (loc, code, type, arg);
1953 if (r == NULL_TREE)
1955 if (arg == orig_arg)
1956 r = t;
1957 else
1958 r = build1_loc (loc, code, type, arg);
1960 VERIFY_CONSTANT (r);
1961 return r;
1964 /* Helper function for cxx_eval_binary_expression. Try to optimize
1965 original POINTER_PLUS_EXPR T, LHS p+ RHS, return NULL_TREE if the
1966 generic folding should be used. */
1968 static tree
1969 cxx_fold_pointer_plus_expression (const constexpr_ctx *ctx, tree t,
1970 tree lhs, tree rhs, bool *non_constant_p,
1971 bool *overflow_p)
1973 STRIP_NOPS (lhs);
1974 if (TREE_CODE (lhs) != ADDR_EXPR)
1975 return NULL_TREE;
1977 lhs = TREE_OPERAND (lhs, 0);
1979 /* &A[i] p+ j => &A[i + j] */
1980 if (TREE_CODE (lhs) == ARRAY_REF
1981 && TREE_CODE (TREE_OPERAND (lhs, 1)) == INTEGER_CST
1982 && TREE_CODE (rhs) == INTEGER_CST
1983 && TYPE_SIZE_UNIT (TREE_TYPE (lhs))
1984 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs))) == INTEGER_CST)
1986 tree orig_type = TREE_TYPE (t);
1987 location_t loc = EXPR_LOCATION (t);
1988 tree type = TREE_TYPE (lhs);
1990 t = fold_convert_loc (loc, ssizetype, TREE_OPERAND (lhs, 1));
1991 tree nelts = array_type_nelts_top (TREE_TYPE (TREE_OPERAND (lhs, 0)));
1992 nelts = cxx_eval_constant_expression (ctx, nelts, false, non_constant_p,
1993 overflow_p);
1994 if (*non_constant_p)
1995 return NULL_TREE;
1996 /* Don't fold an out-of-bound access. */
1997 if (!tree_int_cst_le (t, nelts))
1998 return NULL_TREE;
1999 rhs = cp_fold_convert (ssizetype, rhs);
2000 /* Don't fold if rhs can't be divided exactly by TYPE_SIZE_UNIT.
2001 constexpr int A[1]; ... (char *)&A[0] + 1 */
2002 if (!integer_zerop (fold_build2_loc (loc, TRUNC_MOD_EXPR, sizetype,
2003 rhs, TYPE_SIZE_UNIT (type))))
2004 return NULL_TREE;
2005 /* Make sure to treat the second operand of POINTER_PLUS_EXPR
2006 as signed. */
2007 rhs = fold_build2_loc (loc, EXACT_DIV_EXPR, ssizetype, rhs,
2008 TYPE_SIZE_UNIT (type));
2009 t = size_binop_loc (loc, PLUS_EXPR, rhs, t);
2010 t = build4_loc (loc, ARRAY_REF, type, TREE_OPERAND (lhs, 0),
2011 t, NULL_TREE, NULL_TREE);
2012 t = cp_build_addr_expr (t, tf_warning_or_error);
2013 t = cp_fold_convert (orig_type, t);
2014 return cxx_eval_constant_expression (ctx, t, /*lval*/false,
2015 non_constant_p, overflow_p);
2018 return NULL_TREE;
2021 /* Subroutine of cxx_eval_constant_expression.
2022 Like cxx_eval_unary_expression, except for binary expressions. */
2024 static tree
2025 cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t,
2026 bool /*lval*/,
2027 bool *non_constant_p, bool *overflow_p)
2029 tree r = NULL_TREE;
2030 tree orig_lhs = TREE_OPERAND (t, 0);
2031 tree orig_rhs = TREE_OPERAND (t, 1);
2032 tree lhs, rhs;
2033 lhs = cxx_eval_constant_expression (ctx, orig_lhs, /*lval*/false,
2034 non_constant_p, overflow_p);
2035 /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer
2036 subtraction. */
2037 if (*non_constant_p)
2038 return t;
2039 rhs = cxx_eval_constant_expression (ctx, orig_rhs, /*lval*/false,
2040 non_constant_p, overflow_p);
2041 if (*non_constant_p)
2042 return t;
2044 location_t loc = EXPR_LOCATION (t);
2045 enum tree_code code = TREE_CODE (t);
2046 tree type = TREE_TYPE (t);
2048 if (code == EQ_EXPR || code == NE_EXPR)
2050 bool is_code_eq = (code == EQ_EXPR);
2052 if (TREE_CODE (lhs) == PTRMEM_CST
2053 && TREE_CODE (rhs) == PTRMEM_CST)
2054 r = constant_boolean_node (cp_tree_equal (lhs, rhs) == is_code_eq,
2055 type);
2056 else if ((TREE_CODE (lhs) == PTRMEM_CST
2057 || TREE_CODE (rhs) == PTRMEM_CST)
2058 && (null_member_pointer_value_p (lhs)
2059 || null_member_pointer_value_p (rhs)))
2060 r = constant_boolean_node (!is_code_eq, type);
2061 else if (TREE_CODE (lhs) == PTRMEM_CST)
2062 lhs = cplus_expand_constant (lhs);
2063 else if (TREE_CODE (rhs) == PTRMEM_CST)
2064 rhs = cplus_expand_constant (rhs);
2066 if (code == POINTER_PLUS_EXPR && !*non_constant_p
2067 && integer_zerop (lhs) && !integer_zerop (rhs))
2069 if (!ctx->quiet)
2070 error ("arithmetic involving a null pointer in %qE", lhs);
2071 return t;
2073 else if (code == POINTER_PLUS_EXPR)
2074 r = cxx_fold_pointer_plus_expression (ctx, t, lhs, rhs, non_constant_p,
2075 overflow_p);
2077 if (r == NULL_TREE)
2078 r = fold_binary_loc (loc, code, type, lhs, rhs);
2080 if (r == NULL_TREE)
2082 if (lhs == orig_lhs && rhs == orig_rhs)
2083 r = t;
2084 else
2085 r = build2_loc (loc, code, type, lhs, rhs);
2087 else if (cxx_eval_check_shift_p (loc, ctx, code, type, lhs, rhs))
2088 *non_constant_p = true;
2089 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
2090 a local array in a constexpr function. */
2091 bool ptr = POINTER_TYPE_P (TREE_TYPE (lhs));
2092 if (!ptr)
2093 VERIFY_CONSTANT (r);
2094 return r;
2097 /* Subroutine of cxx_eval_constant_expression.
2098 Attempt to evaluate condition expressions. Dead branches are not
2099 looked into. */
2101 static tree
2102 cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t,
2103 bool lval,
2104 bool *non_constant_p, bool *overflow_p,
2105 tree *jump_target)
2107 tree val = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
2108 /*lval*/false,
2109 non_constant_p, overflow_p);
2110 VERIFY_CONSTANT (val);
2111 /* Don't VERIFY_CONSTANT the other operands. */
2112 if (integer_zerop (val))
2113 return cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2),
2114 lval,
2115 non_constant_p, overflow_p,
2116 jump_target);
2117 return cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
2118 lval,
2119 non_constant_p, overflow_p,
2120 jump_target);
2123 /* Subroutine of cxx_eval_constant_expression.
2124 Attempt to evaluate vector condition expressions. Unlike
2125 cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal
2126 ternary arithmetics operation, where all 3 arguments have to be
2127 evaluated as constants and then folding computes the result from
2128 them. */
2130 static tree
2131 cxx_eval_vector_conditional_expression (const constexpr_ctx *ctx, tree t,
2132 bool *non_constant_p, bool *overflow_p)
2134 tree arg1 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
2135 /*lval*/false,
2136 non_constant_p, overflow_p);
2137 VERIFY_CONSTANT (arg1);
2138 tree arg2 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
2139 /*lval*/false,
2140 non_constant_p, overflow_p);
2141 VERIFY_CONSTANT (arg2);
2142 tree arg3 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2),
2143 /*lval*/false,
2144 non_constant_p, overflow_p);
2145 VERIFY_CONSTANT (arg3);
2146 location_t loc = EXPR_LOCATION (t);
2147 tree type = TREE_TYPE (t);
2148 tree r = fold_ternary_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
2149 if (r == NULL_TREE)
2151 if (arg1 == TREE_OPERAND (t, 0)
2152 && arg2 == TREE_OPERAND (t, 1)
2153 && arg3 == TREE_OPERAND (t, 2))
2154 r = t;
2155 else
2156 r = build3_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
2158 VERIFY_CONSTANT (r);
2159 return r;
2162 /* Returns less than, equal to, or greater than zero if KEY is found to be
2163 less than, to match, or to be greater than the constructor_elt's INDEX. */
2165 static int
2166 array_index_cmp (tree key, tree index)
2168 gcc_assert (TREE_CODE (key) == INTEGER_CST);
2170 switch (TREE_CODE (index))
2172 case INTEGER_CST:
2173 return tree_int_cst_compare (key, index);
2174 case RANGE_EXPR:
2176 tree lo = TREE_OPERAND (index, 0);
2177 tree hi = TREE_OPERAND (index, 1);
2178 if (tree_int_cst_lt (key, lo))
2179 return -1;
2180 else if (tree_int_cst_lt (hi, key))
2181 return 1;
2182 else
2183 return 0;
2185 default:
2186 gcc_unreachable ();
2190 /* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1
2191 if none. If INSERT is true, insert a matching element rather than fail. */
2193 static HOST_WIDE_INT
2194 find_array_ctor_elt (tree ary, tree dindex, bool insert = false)
2196 if (tree_int_cst_sgn (dindex) < 0)
2197 return -1;
2199 unsigned HOST_WIDE_INT i = tree_to_uhwi (dindex);
2200 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ary);
2201 unsigned HOST_WIDE_INT len = vec_safe_length (elts);
2203 unsigned HOST_WIDE_INT end = len;
2204 unsigned HOST_WIDE_INT begin = 0;
2206 /* If the last element of the CONSTRUCTOR has its own index, we can assume
2207 that the same is true of the other elements and index directly. */
2208 if (end > 0)
2210 tree cindex = (*elts)[end - 1].index;
2211 if (TREE_CODE (cindex) == INTEGER_CST
2212 && compare_tree_int (cindex, end - 1) == 0)
2214 if (i < end)
2215 return i;
2216 else
2217 begin = end;
2221 /* Otherwise, find a matching index by means of a binary search. */
2222 while (begin != end)
2224 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
2225 constructor_elt &elt = (*elts)[middle];
2226 tree idx = elt.index;
2228 int cmp = array_index_cmp (dindex, idx);
2229 if (cmp < 0)
2230 end = middle;
2231 else if (cmp > 0)
2232 begin = middle + 1;
2233 else
2235 if (insert && TREE_CODE (idx) == RANGE_EXPR)
2237 /* We need to split the range. */
2238 constructor_elt e;
2239 tree lo = TREE_OPERAND (idx, 0);
2240 tree hi = TREE_OPERAND (idx, 1);
2241 tree value = elt.value;
2242 dindex = fold_convert (sizetype, dindex);
2243 if (tree_int_cst_lt (lo, dindex))
2245 /* There are still some lower elts; shorten the range. */
2246 tree new_hi = int_const_binop (MINUS_EXPR, dindex,
2247 size_one_node);
2248 if (tree_int_cst_equal (lo, new_hi))
2249 /* Only one element left, no longer a range. */
2250 elt.index = lo;
2251 else
2252 TREE_OPERAND (idx, 1) = new_hi;
2253 /* Append the element we want to insert. */
2254 ++middle;
2255 e.index = dindex;
2256 e.value = unshare_constructor (value);
2257 vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e);
2259 else
2260 /* No lower elts, the range elt is now ours. */
2261 elt.index = dindex;
2263 if (tree_int_cst_lt (dindex, hi))
2265 /* There are still some higher elts; append a range. */
2266 tree new_lo = int_const_binop (PLUS_EXPR, dindex,
2267 size_one_node);
2268 if (tree_int_cst_equal (new_lo, hi))
2269 e.index = hi;
2270 else
2271 e.index = build2 (RANGE_EXPR, sizetype, new_lo, hi);
2272 e.value = unshare_constructor (value);
2273 vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e);
2276 return middle;
2280 if (insert)
2282 constructor_elt e = { dindex, NULL_TREE };
2283 vec_safe_insert (CONSTRUCTOR_ELTS (ary), end, e);
2284 return end;
2287 return -1;
2290 /* Under the control of CTX, issue a detailed diagnostic for
2291 an out-of-bounds subscript INDEX into the expression ARRAY. */
2293 static void
2294 diag_array_subscript (const constexpr_ctx *ctx, tree array, tree index)
2296 if (!ctx->quiet)
2298 tree arraytype = TREE_TYPE (array);
2300 /* Convert the unsigned array subscript to a signed integer to avoid
2301 printing huge numbers for small negative values. */
2302 tree sidx = fold_convert (ssizetype, index);
2303 if (DECL_P (array))
2305 if (TYPE_DOMAIN (arraytype))
2306 error ("array subscript value %qE is outside the bounds "
2307 "of array %qD of type %qT", sidx, array, arraytype);
2308 else
2309 error ("non-zero array subscript %qE is used with array %qD of "
2310 "type %qT with unknown bounds", sidx, array, arraytype);
2311 inform (DECL_SOURCE_LOCATION (array), "declared here");
2313 else if (TYPE_DOMAIN (arraytype))
2314 error ("array subscript value %qE is outside the bounds "
2315 "of array type %qT", sidx, arraytype);
2316 else
2317 error ("non-zero array subscript %qE is used with array of type %qT "
2318 "with unknown bounds", sidx, arraytype);
2322 /* Return the number of elements for TYPE (which is an ARRAY_TYPE or
2323 a VECTOR_TYPE). */
2325 static tree
2326 get_array_or_vector_nelts (const constexpr_ctx *ctx, tree type,
2327 bool *non_constant_p, bool *overflow_p)
2329 tree nelts;
2330 if (TREE_CODE (type) == ARRAY_TYPE)
2332 if (TYPE_DOMAIN (type))
2333 nelts = array_type_nelts_top (type);
2334 else
2335 nelts = size_zero_node;
2337 else if (VECTOR_TYPE_P (type))
2338 nelts = size_int (TYPE_VECTOR_SUBPARTS (type));
2339 else
2340 gcc_unreachable ();
2342 /* For VLAs, the number of elements won't be an integer constant. */
2343 nelts = cxx_eval_constant_expression (ctx, nelts, false,
2344 non_constant_p, overflow_p);
2345 return nelts;
2348 /* Extract element INDEX consisting of CHARS_PER_ELT chars from
2349 STRING_CST STRING. */
2351 static tree
2352 extract_string_elt (tree string, unsigned chars_per_elt, unsigned index)
2354 tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (string)));
2355 tree r;
2357 if (chars_per_elt == 1)
2358 r = build_int_cst (type, TREE_STRING_POINTER (string)[index]);
2359 else
2361 const unsigned char *ptr
2362 = ((const unsigned char *)TREE_STRING_POINTER (string)
2363 + index * chars_per_elt);
2364 r = native_interpret_expr (type, ptr, chars_per_elt);
2366 return r;
2369 /* Subroutine of cxx_eval_constant_expression.
2370 Attempt to reduce a reference to an array slot. */
2372 static tree
2373 cxx_eval_array_reference (const constexpr_ctx *ctx, tree t,
2374 bool lval,
2375 bool *non_constant_p, bool *overflow_p)
2377 tree oldary = TREE_OPERAND (t, 0);
2378 tree ary = cxx_eval_constant_expression (ctx, oldary,
2379 lval,
2380 non_constant_p, overflow_p);
2381 tree index, oldidx;
2382 HOST_WIDE_INT i = 0;
2383 tree elem_type = NULL_TREE;
2384 unsigned len = 0, elem_nchars = 1;
2385 if (*non_constant_p)
2386 return t;
2387 oldidx = TREE_OPERAND (t, 1);
2388 index = cxx_eval_constant_expression (ctx, oldidx,
2389 false,
2390 non_constant_p, overflow_p);
2391 VERIFY_CONSTANT (index);
2392 if (!lval)
2394 elem_type = TREE_TYPE (TREE_TYPE (ary));
2395 if (TREE_CODE (ary) == VIEW_CONVERT_EXPR
2396 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary, 0)))
2397 && TREE_TYPE (t) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary, 0))))
2398 ary = TREE_OPERAND (ary, 0);
2399 if (TREE_CODE (ary) == CONSTRUCTOR)
2400 len = CONSTRUCTOR_NELTS (ary);
2401 else if (TREE_CODE (ary) == STRING_CST)
2403 elem_nchars = (TYPE_PRECISION (elem_type)
2404 / TYPE_PRECISION (char_type_node));
2405 len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars;
2407 else if (TREE_CODE (ary) == VECTOR_CST)
2408 /* We don't create variable-length VECTOR_CSTs. */
2409 len = VECTOR_CST_NELTS (ary).to_constant ();
2410 else
2412 /* We can't do anything with other tree codes, so use
2413 VERIFY_CONSTANT to complain and fail. */
2414 VERIFY_CONSTANT (ary);
2415 gcc_unreachable ();
2418 if (!tree_fits_shwi_p (index)
2419 || (i = tree_to_shwi (index)) < 0)
2421 diag_array_subscript (ctx, ary, index);
2422 *non_constant_p = true;
2423 return t;
2427 tree nelts = get_array_or_vector_nelts (ctx, TREE_TYPE (ary), non_constant_p,
2428 overflow_p);
2429 VERIFY_CONSTANT (nelts);
2430 if ((lval
2431 ? !tree_int_cst_le (index, nelts)
2432 : !tree_int_cst_lt (index, nelts))
2433 || tree_int_cst_sgn (index) < 0)
2435 diag_array_subscript (ctx, ary, index);
2436 *non_constant_p = true;
2437 return t;
2440 if (lval && ary == oldary && index == oldidx)
2441 return t;
2442 else if (lval)
2443 return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL);
2445 bool found;
2446 if (TREE_CODE (ary) == CONSTRUCTOR)
2448 HOST_WIDE_INT ix = find_array_ctor_elt (ary, index);
2449 found = (ix >= 0);
2450 if (found)
2451 i = ix;
2453 else
2454 found = (i < len);
2456 if (found)
2458 tree r;
2459 if (TREE_CODE (ary) == CONSTRUCTOR)
2460 r = (*CONSTRUCTOR_ELTS (ary))[i].value;
2461 else if (TREE_CODE (ary) == VECTOR_CST)
2462 r = VECTOR_CST_ELT (ary, i);
2463 else
2464 r = extract_string_elt (ary, elem_nchars, i);
2466 if (r)
2467 /* Don't VERIFY_CONSTANT here. */
2468 return r;
2470 /* Otherwise the element doesn't have a value yet. */
2473 /* Not found. */
2475 if (TREE_CODE (ary) == CONSTRUCTOR
2476 && CONSTRUCTOR_NO_IMPLICIT_ZERO (ary))
2478 /* 'ary' is part of the aggregate initializer we're currently
2479 building; if there's no initializer for this element yet,
2480 that's an error. */
2481 if (!ctx->quiet)
2482 error ("accessing uninitialized array element");
2483 *non_constant_p = true;
2484 return t;
2487 /* If it's within the array bounds but doesn't have an explicit
2488 initializer, it's value-initialized. */
2489 tree val = build_value_init (elem_type, tf_warning_or_error);
2490 return cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
2491 overflow_p);
2494 /* Subroutine of cxx_eval_constant_expression.
2495 Attempt to reduce a field access of a value of class type. */
2497 static tree
2498 cxx_eval_component_reference (const constexpr_ctx *ctx, tree t,
2499 bool lval,
2500 bool *non_constant_p, bool *overflow_p)
2502 unsigned HOST_WIDE_INT i;
2503 tree field;
2504 tree value;
2505 tree part = TREE_OPERAND (t, 1);
2506 tree orig_whole = TREE_OPERAND (t, 0);
2507 tree whole = cxx_eval_constant_expression (ctx, orig_whole,
2508 lval,
2509 non_constant_p, overflow_p);
2510 if (INDIRECT_REF_P (whole)
2511 && integer_zerop (TREE_OPERAND (whole, 0))
2512 && !ctx->quiet)
2513 error ("dereferencing a null pointer in %qE", orig_whole);
2515 if (TREE_CODE (whole) == PTRMEM_CST)
2516 whole = cplus_expand_constant (whole);
2517 if (whole == orig_whole)
2518 return t;
2519 if (lval)
2520 return fold_build3 (COMPONENT_REF, TREE_TYPE (t),
2521 whole, part, NULL_TREE);
2522 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
2523 CONSTRUCTOR. */
2524 if (!*non_constant_p && TREE_CODE (whole) != CONSTRUCTOR)
2526 if (!ctx->quiet)
2527 error ("%qE is not a constant expression", orig_whole);
2528 *non_constant_p = true;
2530 if (DECL_MUTABLE_P (part))
2532 if (!ctx->quiet)
2533 error ("mutable %qD is not usable in a constant expression", part);
2534 *non_constant_p = true;
2536 if (*non_constant_p)
2537 return t;
2538 bool pmf = TYPE_PTRMEMFUNC_P (TREE_TYPE (whole));
2539 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
2541 /* Use name match for PMF fields, as a variant will have a
2542 different FIELD_DECL with a different type. */
2543 if (pmf ? DECL_NAME (field) == DECL_NAME (part)
2544 : field == part)
2546 if (value)
2547 return value;
2548 else
2549 /* We're in the middle of initializing it. */
2550 break;
2553 if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE
2554 && CONSTRUCTOR_NELTS (whole) > 0)
2556 /* DR 1188 says we don't have to deal with this. */
2557 if (!ctx->quiet)
2558 error ("accessing %qD member instead of initialized %qD member in "
2559 "constant expression", part, CONSTRUCTOR_ELT (whole, 0)->index);
2560 *non_constant_p = true;
2561 return t;
2564 /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty
2565 classes never get represented; throw together a value now. */
2566 if (is_really_empty_class (TREE_TYPE (t)))
2567 return build_constructor (TREE_TYPE (t), NULL);
2569 gcc_assert (DECL_CONTEXT (part) == TYPE_MAIN_VARIANT (TREE_TYPE (whole)));
2571 if (CONSTRUCTOR_NO_IMPLICIT_ZERO (whole))
2573 /* 'whole' is part of the aggregate initializer we're currently
2574 building; if there's no initializer for this member yet, that's an
2575 error. */
2576 if (!ctx->quiet)
2577 error ("accessing uninitialized member %qD", part);
2578 *non_constant_p = true;
2579 return t;
2582 /* If there's no explicit init for this field, it's value-initialized. */
2583 value = build_value_init (TREE_TYPE (t), tf_warning_or_error);
2584 return cxx_eval_constant_expression (ctx, value,
2585 lval,
2586 non_constant_p, overflow_p);
2589 /* Subroutine of cxx_eval_constant_expression.
2590 Attempt to reduce a field access of a value of class type that is
2591 expressed as a BIT_FIELD_REF. */
2593 static tree
2594 cxx_eval_bit_field_ref (const constexpr_ctx *ctx, tree t,
2595 bool lval,
2596 bool *non_constant_p, bool *overflow_p)
2598 tree orig_whole = TREE_OPERAND (t, 0);
2599 tree retval, fldval, utype, mask;
2600 bool fld_seen = false;
2601 HOST_WIDE_INT istart, isize;
2602 tree whole = cxx_eval_constant_expression (ctx, orig_whole,
2603 lval,
2604 non_constant_p, overflow_p);
2605 tree start, field, value;
2606 unsigned HOST_WIDE_INT i;
2608 if (whole == orig_whole)
2609 return t;
2610 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
2611 CONSTRUCTOR. */
2612 if (!*non_constant_p
2613 && TREE_CODE (whole) != VECTOR_CST
2614 && TREE_CODE (whole) != CONSTRUCTOR)
2616 if (!ctx->quiet)
2617 error ("%qE is not a constant expression", orig_whole);
2618 *non_constant_p = true;
2620 if (*non_constant_p)
2621 return t;
2623 if (TREE_CODE (whole) == VECTOR_CST)
2624 return fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
2625 TREE_OPERAND (t, 1), TREE_OPERAND (t, 2));
2627 start = TREE_OPERAND (t, 2);
2628 istart = tree_to_shwi (start);
2629 isize = tree_to_shwi (TREE_OPERAND (t, 1));
2630 utype = TREE_TYPE (t);
2631 if (!TYPE_UNSIGNED (utype))
2632 utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1);
2633 retval = build_int_cst (utype, 0);
2634 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
2636 tree bitpos = bit_position (field);
2637 if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1))
2638 return value;
2639 if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE
2640 && TREE_CODE (value) == INTEGER_CST
2641 && tree_fits_shwi_p (bitpos)
2642 && tree_fits_shwi_p (DECL_SIZE (field)))
2644 HOST_WIDE_INT bit = tree_to_shwi (bitpos);
2645 HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field));
2646 HOST_WIDE_INT shift;
2647 if (bit >= istart && bit + sz <= istart + isize)
2649 fldval = fold_convert (utype, value);
2650 mask = build_int_cst_type (utype, -1);
2651 mask = fold_build2 (LSHIFT_EXPR, utype, mask,
2652 size_int (TYPE_PRECISION (utype) - sz));
2653 mask = fold_build2 (RSHIFT_EXPR, utype, mask,
2654 size_int (TYPE_PRECISION (utype) - sz));
2655 fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask);
2656 shift = bit - istart;
2657 if (BYTES_BIG_ENDIAN)
2658 shift = TYPE_PRECISION (utype) - shift - sz;
2659 fldval = fold_build2 (LSHIFT_EXPR, utype, fldval,
2660 size_int (shift));
2661 retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval);
2662 fld_seen = true;
2666 if (fld_seen)
2667 return fold_convert (TREE_TYPE (t), retval);
2668 gcc_unreachable ();
2669 return error_mark_node;
2672 /* Subroutine of cxx_eval_constant_expression.
2673 Evaluate a short-circuited logical expression T in the context
2674 of a given constexpr CALL. BAILOUT_VALUE is the value for
2675 early return. CONTINUE_VALUE is used here purely for
2676 sanity check purposes. */
2678 static tree
2679 cxx_eval_logical_expression (const constexpr_ctx *ctx, tree t,
2680 tree bailout_value, tree continue_value,
2681 bool lval,
2682 bool *non_constant_p, bool *overflow_p)
2684 tree r;
2685 tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
2686 lval,
2687 non_constant_p, overflow_p);
2688 VERIFY_CONSTANT (lhs);
2689 if (tree_int_cst_equal (lhs, bailout_value))
2690 return lhs;
2691 gcc_assert (tree_int_cst_equal (lhs, continue_value));
2692 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
2693 lval, non_constant_p,
2694 overflow_p);
2695 VERIFY_CONSTANT (r);
2696 return r;
2699 /* REF is a COMPONENT_REF designating a particular field. V is a vector of
2700 CONSTRUCTOR elements to initialize (part of) an object containing that
2701 field. Return a pointer to the constructor_elt corresponding to the
2702 initialization of the field. */
2704 static constructor_elt *
2705 base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref)
2707 tree aggr = TREE_OPERAND (ref, 0);
2708 tree field = TREE_OPERAND (ref, 1);
2709 HOST_WIDE_INT i;
2710 constructor_elt *ce;
2712 gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
2714 if (TREE_CODE (aggr) == COMPONENT_REF)
2716 constructor_elt *base_ce
2717 = base_field_constructor_elt (v, aggr);
2718 v = CONSTRUCTOR_ELTS (base_ce->value);
2721 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
2722 if (ce->index == field)
2723 return ce;
2725 gcc_unreachable ();
2726 return NULL;
2729 /* Some of the expressions fed to the constexpr mechanism are calls to
2730 constructors, which have type void. In that case, return the type being
2731 initialized by the constructor. */
2733 static tree
2734 initialized_type (tree t)
2736 if (TYPE_P (t))
2737 return t;
2738 tree type = cv_unqualified (TREE_TYPE (t));
2739 if (TREE_CODE (t) == CALL_EXPR || TREE_CODE (t) == AGGR_INIT_EXPR)
2741 /* A constructor call has void type, so we need to look deeper. */
2742 tree fn = get_function_named_in_call (t);
2743 if (fn && TREE_CODE (fn) == FUNCTION_DECL
2744 && DECL_CXX_CONSTRUCTOR_P (fn))
2745 type = DECL_CONTEXT (fn);
2747 return type;
2750 /* We're about to initialize element INDEX of an array or class from VALUE.
2751 Set up NEW_CTX appropriately by adjusting .object to refer to the
2752 subobject and creating a new CONSTRUCTOR if the element is itself
2753 a class or array. */
2755 static void
2756 init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx,
2757 tree index, tree &value)
2759 new_ctx = *ctx;
2761 if (index && TREE_CODE (index) != INTEGER_CST
2762 && TREE_CODE (index) != FIELD_DECL)
2763 /* This won't have an element in the new CONSTRUCTOR. */
2764 return;
2766 tree type = initialized_type (value);
2767 if (!AGGREGATE_TYPE_P (type) && !VECTOR_TYPE_P (type))
2768 /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
2769 return;
2771 /* The sub-aggregate initializer might contain a placeholder;
2772 update object to refer to the subobject and ctor to refer to
2773 the (newly created) sub-initializer. */
2774 if (ctx->object)
2775 new_ctx.object = build_ctor_subob_ref (index, type, ctx->object);
2776 tree elt = build_constructor (type, NULL);
2777 CONSTRUCTOR_NO_IMPLICIT_ZERO (elt) = true;
2778 new_ctx.ctor = elt;
2780 if (TREE_CODE (value) == TARGET_EXPR)
2781 /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
2782 value = TARGET_EXPR_INITIAL (value);
2785 /* We're about to process an initializer for a class or array TYPE. Make
2786 sure that CTX is set up appropriately. */
2788 static void
2789 verify_ctor_sanity (const constexpr_ctx *ctx, tree type)
2791 /* We don't bother building a ctor for an empty base subobject. */
2792 if (is_empty_class (type))
2793 return;
2795 /* We're in the middle of an initializer that might involve placeholders;
2796 our caller should have created a CONSTRUCTOR for us to put the
2797 initializer into. We will either return that constructor or T. */
2798 gcc_assert (ctx->ctor);
2799 gcc_assert (same_type_ignoring_top_level_qualifiers_p
2800 (type, TREE_TYPE (ctx->ctor)));
2801 /* We used to check that ctx->ctor was empty, but that isn't the case when
2802 the object is zero-initialized before calling the constructor. */
2803 if (ctx->object)
2805 tree otype = TREE_TYPE (ctx->object);
2806 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, otype)
2807 /* Handle flexible array members. */
2808 || (TREE_CODE (otype) == ARRAY_TYPE
2809 && TYPE_DOMAIN (otype) == NULL_TREE
2810 && TREE_CODE (type) == ARRAY_TYPE
2811 && (same_type_ignoring_top_level_qualifiers_p
2812 (TREE_TYPE (type), TREE_TYPE (otype)))));
2814 gcc_assert (!ctx->object || !DECL_P (ctx->object)
2815 || *(ctx->values->get (ctx->object)) == ctx->ctor);
2818 /* Subroutine of cxx_eval_constant_expression.
2819 The expression tree T denotes a C-style array or a C-style
2820 aggregate. Reduce it to a constant expression. */
2822 static tree
2823 cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t,
2824 bool lval,
2825 bool *non_constant_p, bool *overflow_p)
2827 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
2828 bool changed = false;
2829 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t));
2830 tree type = TREE_TYPE (t);
2832 constexpr_ctx new_ctx;
2833 if (TYPE_PTRMEMFUNC_P (type) || VECTOR_TYPE_P (type))
2835 /* We don't really need the ctx->ctor business for a PMF or
2836 vector, but it's simpler to use the same code. */
2837 new_ctx = *ctx;
2838 new_ctx.ctor = build_constructor (type, NULL);
2839 new_ctx.object = NULL_TREE;
2840 ctx = &new_ctx;
2842 verify_ctor_sanity (ctx, type);
2843 vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
2844 vec_alloc (*p, vec_safe_length (v));
2846 unsigned i;
2847 tree index, value;
2848 bool constant_p = true;
2849 bool side_effects_p = false;
2850 FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value)
2852 tree orig_value = value;
2853 init_subob_ctx (ctx, new_ctx, index, value);
2854 if (new_ctx.ctor != ctx->ctor)
2855 /* If we built a new CONSTRUCTOR, attach it now so that other
2856 initializers can refer to it. */
2857 CONSTRUCTOR_APPEND_ELT (*p, index, new_ctx.ctor);
2858 tree elt = cxx_eval_constant_expression (&new_ctx, value,
2859 lval,
2860 non_constant_p, overflow_p);
2861 /* Don't VERIFY_CONSTANT here. */
2862 if (ctx->quiet && *non_constant_p)
2863 break;
2864 if (elt != orig_value)
2865 changed = true;
2867 if (!TREE_CONSTANT (elt))
2868 constant_p = false;
2869 if (TREE_SIDE_EFFECTS (elt))
2870 side_effects_p = true;
2871 if (index && TREE_CODE (index) == COMPONENT_REF)
2873 /* This is an initialization of a vfield inside a base
2874 subaggregate that we already initialized; push this
2875 initialization into the previous initialization. */
2876 constructor_elt *inner = base_field_constructor_elt (*p, index);
2877 inner->value = elt;
2878 changed = true;
2880 else if (index
2881 && (TREE_CODE (index) == NOP_EXPR
2882 || TREE_CODE (index) == POINTER_PLUS_EXPR))
2884 /* This is an initializer for an empty base; now that we've
2885 checked that it's constant, we can ignore it. */
2886 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index))));
2887 changed = true;
2889 else
2891 if (new_ctx.ctor != ctx->ctor)
2893 /* We appended this element above; update the value. */
2894 gcc_assert ((*p)->last().index == index);
2895 (*p)->last().value = elt;
2897 else
2898 CONSTRUCTOR_APPEND_ELT (*p, index, elt);
2899 /* Adding or replacing an element might change the ctor's flags. */
2900 TREE_CONSTANT (ctx->ctor) = constant_p;
2901 TREE_SIDE_EFFECTS (ctx->ctor) = side_effects_p;
2904 if (*non_constant_p || !changed)
2905 return t;
2906 t = ctx->ctor;
2907 /* We're done building this CONSTRUCTOR, so now we can interpret an
2908 element without an explicit initializer as value-initialized. */
2909 CONSTRUCTOR_NO_IMPLICIT_ZERO (t) = false;
2910 TREE_CONSTANT (t) = constant_p;
2911 TREE_SIDE_EFFECTS (t) = side_effects_p;
2912 if (VECTOR_TYPE_P (type))
2913 t = fold (t);
2914 return t;
2917 /* Subroutine of cxx_eval_constant_expression.
2918 The expression tree T is a VEC_INIT_EXPR which denotes the desired
2919 initialization of a non-static data member of array type. Reduce it to a
2920 CONSTRUCTOR.
2922 Note that apart from value-initialization (when VALUE_INIT is true),
2923 this is only intended to support value-initialization and the
2924 initializations done by defaulted constructors for classes with
2925 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
2926 will either be NULL_TREE for the default constructor, or a COMPONENT_REF
2927 for the copy/move constructor. */
2929 static tree
2930 cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init,
2931 bool value_init, bool lval,
2932 bool *non_constant_p, bool *overflow_p)
2934 tree elttype = TREE_TYPE (atype);
2935 verify_ctor_sanity (ctx, atype);
2936 vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
2937 bool pre_init = false;
2938 unsigned HOST_WIDE_INT i;
2939 tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
2941 /* For the default constructor, build up a call to the default
2942 constructor of the element type. We only need to handle class types
2943 here, as for a constructor to be constexpr, all members must be
2944 initialized, which for a defaulted default constructor means they must
2945 be of a class type with a constexpr default constructor. */
2946 if (TREE_CODE (elttype) == ARRAY_TYPE)
2947 /* We only do this at the lowest level. */;
2948 else if (value_init)
2950 init = build_value_init (elttype, complain);
2951 pre_init = true;
2953 else if (!init)
2955 vec<tree, va_gc> *argvec = make_tree_vector ();
2956 init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
2957 &argvec, elttype, LOOKUP_NORMAL,
2958 complain);
2959 release_tree_vector (argvec);
2960 init = build_aggr_init_expr (TREE_TYPE (init), init);
2961 pre_init = true;
2964 tree nelts = get_array_or_vector_nelts (ctx, atype, non_constant_p,
2965 overflow_p);
2966 unsigned HOST_WIDE_INT max = tree_to_uhwi (nelts);
2967 for (i = 0; i < max; ++i)
2969 tree idx = build_int_cst (size_type_node, i);
2970 tree eltinit;
2971 bool reuse = false;
2972 constexpr_ctx new_ctx;
2973 init_subob_ctx (ctx, new_ctx, idx, pre_init ? init : elttype);
2974 if (new_ctx.ctor != ctx->ctor)
2975 CONSTRUCTOR_APPEND_ELT (*p, idx, new_ctx.ctor);
2976 if (TREE_CODE (elttype) == ARRAY_TYPE)
2978 /* A multidimensional array; recurse. */
2979 if (value_init || init == NULL_TREE)
2981 eltinit = NULL_TREE;
2982 reuse = i == 0;
2984 else
2985 eltinit = cp_build_array_ref (input_location, init, idx, complain);
2986 eltinit = cxx_eval_vec_init_1 (&new_ctx, elttype, eltinit, value_init,
2987 lval,
2988 non_constant_p, overflow_p);
2990 else if (pre_init)
2992 /* Initializing an element using value or default initialization
2993 we just pre-built above. */
2994 eltinit = cxx_eval_constant_expression (&new_ctx, init, lval,
2995 non_constant_p, overflow_p);
2996 reuse = i == 0;
2998 else
3000 /* Copying an element. */
3001 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3002 (atype, TREE_TYPE (init)));
3003 eltinit = cp_build_array_ref (input_location, init, idx, complain);
3004 if (!lvalue_p (init))
3005 eltinit = move (eltinit);
3006 eltinit = force_rvalue (eltinit, complain);
3007 eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval,
3008 non_constant_p, overflow_p);
3010 if (*non_constant_p && !ctx->quiet)
3011 break;
3012 if (new_ctx.ctor != ctx->ctor)
3014 /* We appended this element above; update the value. */
3015 gcc_assert ((*p)->last().index == idx);
3016 (*p)->last().value = eltinit;
3018 else
3019 CONSTRUCTOR_APPEND_ELT (*p, idx, eltinit);
3020 /* Reuse the result of cxx_eval_constant_expression call
3021 from the first iteration to all others if it is a constant
3022 initializer that doesn't require relocations. */
3023 if (reuse
3024 && max > 1
3025 && (eltinit == NULL_TREE
3026 || (initializer_constant_valid_p (eltinit, TREE_TYPE (eltinit))
3027 == null_pointer_node)))
3029 if (new_ctx.ctor != ctx->ctor)
3030 eltinit = new_ctx.ctor;
3031 tree range = build2 (RANGE_EXPR, size_type_node,
3032 build_int_cst (size_type_node, 1),
3033 build_int_cst (size_type_node, max - 1));
3034 CONSTRUCTOR_APPEND_ELT (*p, range, unshare_constructor (eltinit));
3035 break;
3037 else if (i == 0)
3038 vec_safe_reserve (*p, max);
3041 if (!*non_constant_p)
3043 init = ctx->ctor;
3044 CONSTRUCTOR_NO_IMPLICIT_ZERO (init) = false;
3046 return init;
3049 static tree
3050 cxx_eval_vec_init (const constexpr_ctx *ctx, tree t,
3051 bool lval,
3052 bool *non_constant_p, bool *overflow_p)
3054 tree atype = TREE_TYPE (t);
3055 tree init = VEC_INIT_EXPR_INIT (t);
3056 tree r = cxx_eval_vec_init_1 (ctx, atype, init,
3057 VEC_INIT_EXPR_VALUE_INIT (t),
3058 lval, non_constant_p, overflow_p);
3059 if (*non_constant_p)
3060 return t;
3061 else
3062 return r;
3065 /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
3066 match. We want to be less strict for simple *& folding; if we have a
3067 non-const temporary that we access through a const pointer, that should
3068 work. We handle this here rather than change fold_indirect_ref_1
3069 because we're dealing with things like ADDR_EXPR of INTEGER_CST which
3070 don't really make sense outside of constant expression evaluation. Also
3071 we want to allow folding to COMPONENT_REF, which could cause trouble
3072 with TBAA in fold_indirect_ref_1.
3074 Try to keep this function synced with fold_indirect_ref_1. */
3076 static tree
3077 cxx_fold_indirect_ref (location_t loc, tree type, tree op0, bool *empty_base)
3079 tree sub = op0;
3080 tree subtype;
3081 poly_uint64 const_op01;
3083 STRIP_NOPS (sub);
3084 subtype = TREE_TYPE (sub);
3085 if (!POINTER_TYPE_P (subtype))
3086 return NULL_TREE;
3088 if (TREE_CODE (sub) == ADDR_EXPR)
3090 tree op = TREE_OPERAND (sub, 0);
3091 tree optype = TREE_TYPE (op);
3093 /* *&CONST_DECL -> to the value of the const decl. */
3094 if (TREE_CODE (op) == CONST_DECL)
3095 return DECL_INITIAL (op);
3096 /* *&p => p; make sure to handle *&"str"[cst] here. */
3097 if (same_type_ignoring_top_level_qualifiers_p (optype, type)
3098 /* Also handle the case where the desired type is an array of unknown
3099 bounds because the variable has had its bounds deduced since the
3100 ADDR_EXPR was created. */
3101 || (TREE_CODE (type) == ARRAY_TYPE
3102 && TREE_CODE (optype) == ARRAY_TYPE
3103 && TYPE_DOMAIN (type) == NULL_TREE
3104 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (optype),
3105 TREE_TYPE (type))))
3107 tree fop = fold_read_from_constant_string (op);
3108 if (fop)
3109 return fop;
3110 else
3111 return op;
3113 /* *(foo *)&fooarray => fooarray[0] */
3114 else if (TREE_CODE (optype) == ARRAY_TYPE
3115 && (same_type_ignoring_top_level_qualifiers_p
3116 (type, TREE_TYPE (optype))))
3118 tree type_domain = TYPE_DOMAIN (optype);
3119 tree min_val = size_zero_node;
3120 if (type_domain && TYPE_MIN_VALUE (type_domain))
3121 min_val = TYPE_MIN_VALUE (type_domain);
3122 return build4_loc (loc, ARRAY_REF, type, op, min_val,
3123 NULL_TREE, NULL_TREE);
3125 /* *(foo *)&complexfoo => __real__ complexfoo */
3126 else if (TREE_CODE (optype) == COMPLEX_TYPE
3127 && (same_type_ignoring_top_level_qualifiers_p
3128 (type, TREE_TYPE (optype))))
3129 return fold_build1_loc (loc, REALPART_EXPR, type, op);
3130 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
3131 else if (VECTOR_TYPE_P (optype)
3132 && (same_type_ignoring_top_level_qualifiers_p
3133 (type, TREE_TYPE (optype))))
3135 tree part_width = TYPE_SIZE (type);
3136 tree index = bitsize_int (0);
3137 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width,
3138 index);
3140 /* Also handle conversion to an empty base class, which
3141 is represented with a NOP_EXPR. */
3142 else if (is_empty_class (type)
3143 && CLASS_TYPE_P (optype)
3144 && DERIVED_FROM_P (type, optype))
3146 *empty_base = true;
3147 return op;
3149 /* *(foo *)&struct_with_foo_field => COMPONENT_REF */
3150 else if (RECORD_OR_UNION_TYPE_P (optype))
3152 tree field = TYPE_FIELDS (optype);
3153 for (; field; field = DECL_CHAIN (field))
3154 if (TREE_CODE (field) == FIELD_DECL
3155 && TREE_TYPE (field) != error_mark_node
3156 && integer_zerop (byte_position (field))
3157 && (same_type_ignoring_top_level_qualifiers_p
3158 (TREE_TYPE (field), type)))
3159 return fold_build3 (COMPONENT_REF, type, op, field, NULL_TREE);
3162 else if (TREE_CODE (sub) == POINTER_PLUS_EXPR
3163 && poly_int_tree_p (TREE_OPERAND (sub, 1), &const_op01))
3165 tree op00 = TREE_OPERAND (sub, 0);
3166 tree op01 = TREE_OPERAND (sub, 1);
3168 STRIP_NOPS (op00);
3169 if (TREE_CODE (op00) == ADDR_EXPR)
3171 tree op00type;
3172 op00 = TREE_OPERAND (op00, 0);
3173 op00type = TREE_TYPE (op00);
3175 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
3176 if (VECTOR_TYPE_P (op00type)
3177 && same_type_ignoring_top_level_qualifiers_p
3178 (type, TREE_TYPE (op00type))
3179 /* POINTER_PLUS_EXPR second operand is sizetype, unsigned,
3180 but we want to treat offsets with MSB set as negative.
3181 For the code below negative offsets are invalid and
3182 TYPE_SIZE of the element is something unsigned, so
3183 check whether op01 fits into poly_int64, which implies
3184 it is from 0 to INTTYPE_MAXIMUM (HOST_WIDE_INT), and
3185 then just use poly_uint64 because we want to treat the
3186 value as unsigned. */
3187 && tree_fits_poly_int64_p (op01))
3189 tree part_width = TYPE_SIZE (type);
3190 poly_uint64 max_offset
3191 = (tree_to_uhwi (part_width) / BITS_PER_UNIT
3192 * TYPE_VECTOR_SUBPARTS (op00type));
3193 if (known_lt (const_op01, max_offset))
3195 tree index = bitsize_int (const_op01 * BITS_PER_UNIT);
3196 return fold_build3_loc (loc,
3197 BIT_FIELD_REF, type, op00,
3198 part_width, index);
3201 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
3202 else if (TREE_CODE (op00type) == COMPLEX_TYPE
3203 && (same_type_ignoring_top_level_qualifiers_p
3204 (type, TREE_TYPE (op00type))))
3206 if (known_eq (wi::to_poly_offset (TYPE_SIZE_UNIT (type)),
3207 const_op01))
3208 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
3210 /* ((foo *)&fooarray)[1] => fooarray[1] */
3211 else if (TREE_CODE (op00type) == ARRAY_TYPE
3212 && (same_type_ignoring_top_level_qualifiers_p
3213 (type, TREE_TYPE (op00type))))
3215 tree type_domain = TYPE_DOMAIN (op00type);
3216 tree min_val = size_zero_node;
3217 if (type_domain && TYPE_MIN_VALUE (type_domain))
3218 min_val = TYPE_MIN_VALUE (type_domain);
3219 offset_int off = wi::to_offset (op01);
3220 offset_int el_sz = wi::to_offset (TYPE_SIZE_UNIT (type));
3221 offset_int remainder;
3222 off = wi::divmod_trunc (off, el_sz, SIGNED, &remainder);
3223 if (remainder == 0 && TREE_CODE (min_val) == INTEGER_CST)
3225 off = off + wi::to_offset (min_val);
3226 op01 = wide_int_to_tree (sizetype, off);
3227 return build4_loc (loc, ARRAY_REF, type, op00, op01,
3228 NULL_TREE, NULL_TREE);
3231 /* Also handle conversion to an empty base class, which
3232 is represented with a NOP_EXPR. */
3233 else if (is_empty_class (type)
3234 && CLASS_TYPE_P (op00type)
3235 && DERIVED_FROM_P (type, op00type))
3237 *empty_base = true;
3238 return op00;
3240 /* ((foo *)&struct_with_foo_field)[1] => COMPONENT_REF */
3241 else if (RECORD_OR_UNION_TYPE_P (op00type))
3243 tree field = TYPE_FIELDS (op00type);
3244 for (; field; field = DECL_CHAIN (field))
3245 if (TREE_CODE (field) == FIELD_DECL
3246 && TREE_TYPE (field) != error_mark_node
3247 && tree_int_cst_equal (byte_position (field), op01)
3248 && (same_type_ignoring_top_level_qualifiers_p
3249 (TREE_TYPE (field), type)))
3250 return fold_build3 (COMPONENT_REF, type, op00,
3251 field, NULL_TREE);
3255 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
3256 else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
3257 && (same_type_ignoring_top_level_qualifiers_p
3258 (type, TREE_TYPE (TREE_TYPE (subtype)))))
3260 tree type_domain;
3261 tree min_val = size_zero_node;
3262 tree newsub
3263 = cxx_fold_indirect_ref (loc, TREE_TYPE (subtype), sub, NULL);
3264 if (newsub)
3265 sub = newsub;
3266 else
3267 sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub);
3268 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
3269 if (type_domain && TYPE_MIN_VALUE (type_domain))
3270 min_val = TYPE_MIN_VALUE (type_domain);
3271 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
3272 NULL_TREE);
3275 return NULL_TREE;
3278 static tree
3279 cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t,
3280 bool lval,
3281 bool *non_constant_p, bool *overflow_p)
3283 tree orig_op0 = TREE_OPERAND (t, 0);
3284 bool empty_base = false;
3286 /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second
3287 operand is an integer-zero. Otherwise reject the MEM_REF for now. */
3289 if (TREE_CODE (t) == MEM_REF
3290 && (!TREE_OPERAND (t, 1) || !integer_zerop (TREE_OPERAND (t, 1))))
3292 gcc_assert (ctx->quiet);
3293 *non_constant_p = true;
3294 return t;
3297 /* First try to simplify it directly. */
3298 tree r = cxx_fold_indirect_ref (EXPR_LOCATION (t), TREE_TYPE (t), orig_op0,
3299 &empty_base);
3300 if (!r)
3302 /* If that didn't work, evaluate the operand first. */
3303 tree op0 = cxx_eval_constant_expression (ctx, orig_op0,
3304 /*lval*/false, non_constant_p,
3305 overflow_p);
3306 /* Don't VERIFY_CONSTANT here. */
3307 if (*non_constant_p)
3308 return t;
3310 if (!lval && integer_zerop (op0))
3312 if (!ctx->quiet)
3313 error ("dereferencing a null pointer");
3314 *non_constant_p = true;
3315 return t;
3318 r = cxx_fold_indirect_ref (EXPR_LOCATION (t), TREE_TYPE (t), op0,
3319 &empty_base);
3320 if (r == NULL_TREE)
3322 /* We couldn't fold to a constant value. Make sure it's not
3323 something we should have been able to fold. */
3324 tree sub = op0;
3325 STRIP_NOPS (sub);
3326 if (TREE_CODE (sub) == ADDR_EXPR)
3328 gcc_assert (!same_type_ignoring_top_level_qualifiers_p
3329 (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t)));
3330 /* DR 1188 says we don't have to deal with this. */
3331 if (!ctx->quiet)
3332 error ("accessing value of %qE through a %qT glvalue in a "
3333 "constant expression", build_fold_indirect_ref (sub),
3334 TREE_TYPE (t));
3335 *non_constant_p = true;
3336 return t;
3339 if (lval && op0 != orig_op0)
3340 return build1 (INDIRECT_REF, TREE_TYPE (t), op0);
3341 if (!lval)
3342 VERIFY_CONSTANT (t);
3343 return t;
3347 r = cxx_eval_constant_expression (ctx, r,
3348 lval, non_constant_p, overflow_p);
3349 if (*non_constant_p)
3350 return t;
3352 /* If we're pulling out the value of an empty base, just return an empty
3353 CONSTRUCTOR. */
3354 if (empty_base && !lval)
3356 r = build_constructor (TREE_TYPE (t), NULL);
3357 TREE_CONSTANT (r) = true;
3360 return r;
3363 /* Complain about R, a VAR_DECL, not being usable in a constant expression.
3364 Shared between potential_constant_expression and
3365 cxx_eval_constant_expression. */
3367 static void
3368 non_const_var_error (tree r)
3370 tree type = TREE_TYPE (r);
3371 error ("the value of %qD is not usable in a constant "
3372 "expression", r);
3373 /* Avoid error cascade. */
3374 if (DECL_INITIAL (r) == error_mark_node)
3375 return;
3376 if (DECL_DECLARED_CONSTEXPR_P (r))
3377 inform (DECL_SOURCE_LOCATION (r),
3378 "%qD used in its own initializer", r);
3379 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3381 if (!CP_TYPE_CONST_P (type))
3382 inform (DECL_SOURCE_LOCATION (r),
3383 "%q#D is not const", r);
3384 else if (CP_TYPE_VOLATILE_P (type))
3385 inform (DECL_SOURCE_LOCATION (r),
3386 "%q#D is volatile", r);
3387 else if (!DECL_INITIAL (r)
3388 || !TREE_CONSTANT (DECL_INITIAL (r))
3389 || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r))
3390 inform (DECL_SOURCE_LOCATION (r),
3391 "%qD was not initialized with a constant "
3392 "expression", r);
3393 else
3394 gcc_unreachable ();
3396 else if (TYPE_REF_P (type))
3397 inform (DECL_SOURCE_LOCATION (r),
3398 "%qD was not initialized with a constant "
3399 "expression", r);
3400 else
3402 if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r))
3403 inform (DECL_SOURCE_LOCATION (r),
3404 "%qD was not declared %<constexpr%>", r);
3405 else
3406 inform (DECL_SOURCE_LOCATION (r),
3407 "%qD does not have integral or enumeration type",
3412 /* Subroutine of cxx_eval_constant_expression.
3413 Like cxx_eval_unary_expression, except for trinary expressions. */
3415 static tree
3416 cxx_eval_trinary_expression (const constexpr_ctx *ctx, tree t,
3417 bool lval,
3418 bool *non_constant_p, bool *overflow_p)
3420 int i;
3421 tree args[3];
3422 tree val;
3424 for (i = 0; i < 3; i++)
3426 args[i] = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, i),
3427 lval,
3428 non_constant_p, overflow_p);
3429 VERIFY_CONSTANT (args[i]);
3432 val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t),
3433 args[0], args[1], args[2]);
3434 if (val == NULL_TREE)
3435 return t;
3436 VERIFY_CONSTANT (val);
3437 return val;
3440 /* True if T was declared in a function declared to be constexpr, and
3441 therefore potentially constant in C++14. */
3443 bool
3444 var_in_constexpr_fn (tree t)
3446 tree ctx = DECL_CONTEXT (t);
3447 return (ctx && TREE_CODE (ctx) == FUNCTION_DECL
3448 && DECL_DECLARED_CONSTEXPR_P (ctx));
3451 /* True if T was declared in a function that might be constexpr: either a
3452 function that was declared constexpr, or a C++17 lambda op(). */
3454 bool
3455 var_in_maybe_constexpr_fn (tree t)
3457 if (cxx_dialect >= cxx17
3458 && DECL_FUNCTION_SCOPE_P (t)
3459 && LAMBDA_FUNCTION_P (DECL_CONTEXT (t)))
3460 return true;
3461 return var_in_constexpr_fn (t);
3464 /* We're assigning INIT to TARGET. In do_build_copy_constructor and
3465 build_over_call we implement trivial copy of a class with tail padding using
3466 assignment of character arrays, which is valid in normal code, but not in
3467 constexpr evaluation. We don't need to worry about clobbering tail padding
3468 in constexpr evaluation, so strip the type punning. */
3470 static void
3471 maybe_simplify_trivial_copy (tree &target, tree &init)
3473 if (TREE_CODE (target) == MEM_REF
3474 && TREE_CODE (init) == MEM_REF
3475 && TREE_TYPE (target) == TREE_TYPE (init)
3476 && TREE_CODE (TREE_TYPE (target)) == ARRAY_TYPE
3477 && TREE_TYPE (TREE_TYPE (target)) == unsigned_char_type_node)
3479 target = build_fold_indirect_ref (TREE_OPERAND (target, 0));
3480 init = build_fold_indirect_ref (TREE_OPERAND (init, 0));
3484 /* Evaluate an INIT_EXPR or MODIFY_EXPR. */
3486 static tree
3487 cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
3488 bool lval,
3489 bool *non_constant_p, bool *overflow_p)
3491 constexpr_ctx new_ctx = *ctx;
3493 tree init = TREE_OPERAND (t, 1);
3494 if (TREE_CLOBBER_P (init))
3495 /* Just ignore clobbers. */
3496 return void_node;
3498 /* First we figure out where we're storing to. */
3499 tree target = TREE_OPERAND (t, 0);
3501 maybe_simplify_trivial_copy (target, init);
3503 tree type = TREE_TYPE (target);
3504 target = cxx_eval_constant_expression (ctx, target,
3505 true,
3506 non_constant_p, overflow_p);
3507 if (*non_constant_p)
3508 return t;
3510 /* cxx_eval_array_reference for lval = true allows references one past
3511 end of array, because it does not know if it is just taking address
3512 (which is valid), or actual dereference. Here we know it is
3513 a dereference, so diagnose it here. */
3514 for (tree probe = target; probe; )
3516 switch (TREE_CODE (probe))
3518 case ARRAY_REF:
3519 tree nelts, ary;
3520 ary = TREE_OPERAND (probe, 0);
3521 nelts = get_array_or_vector_nelts (ctx, TREE_TYPE (ary),
3522 non_constant_p, overflow_p);
3523 VERIFY_CONSTANT (nelts);
3524 gcc_assert (TREE_CODE (nelts) == INTEGER_CST
3525 && TREE_CODE (TREE_OPERAND (probe, 1)) == INTEGER_CST);
3526 if (wi::to_widest (TREE_OPERAND (probe, 1)) == wi::to_widest (nelts))
3528 diag_array_subscript (ctx, ary, TREE_OPERAND (probe, 1));
3529 *non_constant_p = true;
3530 return t;
3532 /* FALLTHRU */
3534 case BIT_FIELD_REF:
3535 case COMPONENT_REF:
3536 probe = TREE_OPERAND (probe, 0);
3537 continue;
3539 default:
3540 probe = NULL_TREE;
3541 continue;
3545 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (target), type))
3547 /* For initialization of an empty base, the original target will be
3548 *(base*)this, which the above evaluation resolves to the object
3549 argument, which has the derived type rather than the base type. In
3550 this situation, just evaluate the initializer and return, since
3551 there's no actual data to store. */
3552 gcc_assert (is_empty_class (type));
3553 return cxx_eval_constant_expression (ctx, init, false,
3554 non_constant_p, overflow_p);
3557 /* And then find the underlying variable. */
3558 vec<tree,va_gc> *refs = make_tree_vector();
3559 tree object = NULL_TREE;
3560 for (tree probe = target; object == NULL_TREE; )
3562 switch (TREE_CODE (probe))
3564 case BIT_FIELD_REF:
3565 case COMPONENT_REF:
3566 case ARRAY_REF:
3567 vec_safe_push (refs, TREE_OPERAND (probe, 1));
3568 vec_safe_push (refs, TREE_TYPE (probe));
3569 probe = TREE_OPERAND (probe, 0);
3570 break;
3572 default:
3573 object = probe;
3577 /* And then find/build up our initializer for the path to the subobject
3578 we're initializing. */
3579 tree *valp;
3580 if (object == ctx->object && VAR_P (object)
3581 && DECL_NAME (object) && ctx->call == NULL)
3582 /* The variable we're building up an aggregate initializer for is outside
3583 the constant-expression, so don't evaluate the store. We check
3584 DECL_NAME to handle TARGET_EXPR temporaries, which are fair game. */
3585 valp = NULL;
3586 else if (DECL_P (object))
3587 valp = ctx->values->get (object);
3588 else
3589 valp = NULL;
3590 if (!valp)
3592 /* A constant-expression cannot modify objects from outside the
3593 constant-expression. */
3594 if (!ctx->quiet)
3595 error ("modification of %qE is not a constant expression", object);
3596 *non_constant_p = true;
3597 return t;
3599 type = TREE_TYPE (object);
3600 bool no_zero_init = true;
3602 vec<tree,va_gc> *ctors = make_tree_vector ();
3603 while (!refs->is_empty())
3605 if (*valp == NULL_TREE)
3607 *valp = build_constructor (type, NULL);
3608 CONSTRUCTOR_NO_IMPLICIT_ZERO (*valp) = no_zero_init;
3610 else if (TREE_CODE (*valp) == STRING_CST)
3612 /* An array was initialized with a string constant, and now
3613 we're writing into one of its elements. Explode the
3614 single initialization into a set of element
3615 initializations. */
3616 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
3618 tree string = *valp;
3619 tree elt_type = TREE_TYPE (type);
3620 unsigned chars_per_elt = (TYPE_PRECISION (elt_type)
3621 / TYPE_PRECISION (char_type_node));
3622 unsigned num_elts = TREE_STRING_LENGTH (string) / chars_per_elt;
3623 tree ary_ctor = build_constructor (type, NULL);
3625 vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor), num_elts);
3626 for (unsigned ix = 0; ix != num_elts; ix++)
3628 constructor_elt elt =
3630 build_int_cst (size_type_node, ix),
3631 extract_string_elt (string, chars_per_elt, ix)
3633 CONSTRUCTOR_ELTS (ary_ctor)->quick_push (elt);
3636 *valp = ary_ctor;
3639 /* If the value of object is already zero-initialized, any new ctors for
3640 subobjects will also be zero-initialized. */
3641 no_zero_init = CONSTRUCTOR_NO_IMPLICIT_ZERO (*valp);
3643 vec_safe_push (ctors, *valp);
3645 enum tree_code code = TREE_CODE (type);
3646 type = refs->pop();
3647 tree index = refs->pop();
3649 constructor_elt *cep = NULL;
3650 if (code == ARRAY_TYPE)
3652 HOST_WIDE_INT i
3653 = find_array_ctor_elt (*valp, index, /*insert*/true);
3654 gcc_assert (i >= 0);
3655 cep = CONSTRUCTOR_ELT (*valp, i);
3656 gcc_assert (TREE_CODE (cep->index) != RANGE_EXPR);
3658 else
3660 gcc_assert (TREE_CODE (index) == FIELD_DECL);
3662 /* We must keep the CONSTRUCTOR's ELTS in FIELD order.
3663 Usually we meet initializers in that order, but it is
3664 possible for base types to be placed not in program
3665 order. */
3666 tree fields = TYPE_FIELDS (DECL_CONTEXT (index));
3667 unsigned HOST_WIDE_INT idx;
3669 if (code == UNION_TYPE && CONSTRUCTOR_NELTS (*valp)
3670 && CONSTRUCTOR_ELT (*valp, 0)->index != index)
3671 /* Changing active member. */
3672 vec_safe_truncate (CONSTRUCTOR_ELTS (*valp), 0);
3674 for (idx = 0;
3675 vec_safe_iterate (CONSTRUCTOR_ELTS (*valp), idx, &cep);
3676 idx++, fields = DECL_CHAIN (fields))
3678 if (index == cep->index)
3679 goto found;
3681 /* The field we're initializing must be on the field
3682 list. Look to see if it is present before the
3683 field the current ELT initializes. */
3684 for (; fields != cep->index; fields = DECL_CHAIN (fields))
3685 if (index == fields)
3686 goto insert;
3689 /* We fell off the end of the CONSTRUCTOR, so insert a new
3690 entry at the end. */
3691 insert:
3693 constructor_elt ce = { index, NULL_TREE };
3695 vec_safe_insert (CONSTRUCTOR_ELTS (*valp), idx, ce);
3696 cep = CONSTRUCTOR_ELT (*valp, idx);
3698 found:;
3700 valp = &cep->value;
3702 release_tree_vector (refs);
3704 if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
3706 /* Create a new CONSTRUCTOR in case evaluation of the initializer
3707 wants to modify it. */
3708 if (*valp == NULL_TREE)
3710 *valp = build_constructor (type, NULL);
3711 CONSTRUCTOR_NO_IMPLICIT_ZERO (*valp) = no_zero_init;
3713 else if (TREE_CODE (*valp) == PTRMEM_CST)
3714 *valp = cplus_expand_constant (*valp);
3715 new_ctx.ctor = *valp;
3716 new_ctx.object = target;
3719 init = cxx_eval_constant_expression (&new_ctx, init, false,
3720 non_constant_p, overflow_p);
3721 /* Don't share a CONSTRUCTOR that might be changed later. */
3722 init = unshare_constructor (init);
3723 if (target == object)
3724 /* The hash table might have moved since the get earlier. */
3725 valp = ctx->values->get (object);
3727 if (TREE_CODE (init) == CONSTRUCTOR)
3729 /* An outer ctx->ctor might be pointing to *valp, so replace
3730 its contents. */
3731 CONSTRUCTOR_ELTS (*valp) = CONSTRUCTOR_ELTS (init);
3732 TREE_CONSTANT (*valp) = TREE_CONSTANT (init);
3733 TREE_SIDE_EFFECTS (*valp) = TREE_SIDE_EFFECTS (init);
3734 CONSTRUCTOR_NO_IMPLICIT_ZERO (*valp)
3735 = CONSTRUCTOR_NO_IMPLICIT_ZERO (init);
3737 else
3738 *valp = init;
3740 /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing
3741 CONSTRUCTORs, if any. */
3742 tree elt;
3743 unsigned i;
3744 bool c = TREE_CONSTANT (init);
3745 bool s = TREE_SIDE_EFFECTS (init);
3746 if (!c || s)
3747 FOR_EACH_VEC_SAFE_ELT (ctors, i, elt)
3749 if (!c)
3750 TREE_CONSTANT (elt) = false;
3751 if (s)
3752 TREE_SIDE_EFFECTS (elt) = true;
3754 release_tree_vector (ctors);
3756 if (*non_constant_p)
3757 return t;
3758 else if (lval)
3759 return target;
3760 else
3761 return init;
3764 /* Evaluate a ++ or -- expression. */
3766 static tree
3767 cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t,
3768 bool lval,
3769 bool *non_constant_p, bool *overflow_p)
3771 enum tree_code code = TREE_CODE (t);
3772 tree type = TREE_TYPE (t);
3773 tree op = TREE_OPERAND (t, 0);
3774 tree offset = TREE_OPERAND (t, 1);
3775 gcc_assert (TREE_CONSTANT (offset));
3777 /* The operand as an lvalue. */
3778 op = cxx_eval_constant_expression (ctx, op, true,
3779 non_constant_p, overflow_p);
3781 /* The operand as an rvalue. */
3782 tree val
3783 = cxx_eval_constant_expression (ctx, op, false,
3784 non_constant_p, overflow_p);
3785 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
3786 a local array in a constexpr function. */
3787 bool ptr = POINTER_TYPE_P (TREE_TYPE (val));
3788 if (!ptr)
3789 VERIFY_CONSTANT (val);
3791 /* The modified value. */
3792 bool inc = (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR);
3793 tree mod;
3794 if (POINTER_TYPE_P (type))
3796 /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
3797 offset = convert_to_ptrofftype (offset);
3798 if (!inc)
3799 offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset);
3800 mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset);
3802 else
3803 mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset);
3804 if (!ptr)
3805 VERIFY_CONSTANT (mod);
3807 /* Storing the modified value. */
3808 tree store = build2 (MODIFY_EXPR, type, op, mod);
3809 cxx_eval_constant_expression (ctx, store,
3810 true, non_constant_p, overflow_p);
3812 /* And the value of the expression. */
3813 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
3815 /* Prefix ops are lvalues. */
3816 if (lval)
3817 return op;
3818 else
3819 /* But we optimize when the caller wants an rvalue. */
3820 return mod;
3822 else
3823 /* Postfix ops are rvalues. */
3824 return val;
3827 /* Predicates for the meaning of *jump_target. */
3829 static bool
3830 returns (tree *jump_target)
3832 return *jump_target
3833 && (TREE_CODE (*jump_target) == RETURN_EXPR
3834 || (TREE_CODE (*jump_target) == LABEL_DECL
3835 && LABEL_DECL_CDTOR (*jump_target)));
3838 static bool
3839 breaks (tree *jump_target)
3841 return *jump_target
3842 && ((TREE_CODE (*jump_target) == LABEL_DECL
3843 && LABEL_DECL_BREAK (*jump_target))
3844 || TREE_CODE (*jump_target) == EXIT_EXPR);
3847 static bool
3848 continues (tree *jump_target)
3850 return *jump_target
3851 && TREE_CODE (*jump_target) == LABEL_DECL
3852 && LABEL_DECL_CONTINUE (*jump_target);
3855 static bool
3856 switches (tree *jump_target)
3858 return *jump_target
3859 && TREE_CODE (*jump_target) == INTEGER_CST;
3862 /* Subroutine of cxx_eval_statement_list. Determine whether the statement
3863 STMT matches *jump_target. If we're looking for a case label and we see
3864 the default label, note it in ctx->css_state. */
3866 static bool
3867 label_matches (const constexpr_ctx *ctx, tree *jump_target, tree stmt)
3869 switch (TREE_CODE (*jump_target))
3871 case LABEL_DECL:
3872 if (TREE_CODE (stmt) == LABEL_EXPR
3873 && LABEL_EXPR_LABEL (stmt) == *jump_target)
3874 return true;
3875 break;
3877 case INTEGER_CST:
3878 if (TREE_CODE (stmt) == CASE_LABEL_EXPR)
3880 gcc_assert (ctx->css_state != NULL);
3881 if (!CASE_LOW (stmt))
3883 /* default: should appear just once in a SWITCH_EXPR
3884 body (excluding nested SWITCH_EXPR). */
3885 gcc_assert (*ctx->css_state != css_default_seen);
3886 /* When evaluating SWITCH_EXPR body for the second time,
3887 return true for the default: label. */
3888 if (*ctx->css_state == css_default_processing)
3889 return true;
3890 *ctx->css_state = css_default_seen;
3892 else if (CASE_HIGH (stmt))
3894 if (tree_int_cst_le (CASE_LOW (stmt), *jump_target)
3895 && tree_int_cst_le (*jump_target, CASE_HIGH (stmt)))
3896 return true;
3898 else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt)))
3899 return true;
3901 break;
3903 default:
3904 gcc_unreachable ();
3906 return false;
3909 /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
3910 semantics, for switch, break, continue, and return. */
3912 static tree
3913 cxx_eval_statement_list (const constexpr_ctx *ctx, tree t,
3914 bool *non_constant_p, bool *overflow_p,
3915 tree *jump_target)
3917 tree_stmt_iterator i;
3918 tree local_target;
3919 /* In a statement-expression we want to return the last value.
3920 For empty statement expression return void_node. */
3921 tree r = void_node;
3922 if (!jump_target)
3924 local_target = NULL_TREE;
3925 jump_target = &local_target;
3927 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
3929 tree stmt = tsi_stmt (i);
3930 if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT)
3931 continue;
3932 r = cxx_eval_constant_expression (ctx, stmt, false,
3933 non_constant_p, overflow_p,
3934 jump_target);
3935 if (*non_constant_p)
3936 break;
3937 if (returns (jump_target) || breaks (jump_target))
3938 break;
3940 return r;
3943 /* Evaluate a LOOP_EXPR for side-effects. Handles break and return
3944 semantics; continue semantics are covered by cxx_eval_statement_list. */
3946 static tree
3947 cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t,
3948 bool *non_constant_p, bool *overflow_p,
3949 tree *jump_target)
3951 constexpr_ctx new_ctx = *ctx;
3953 tree body = TREE_OPERAND (t, 0);
3954 int count = 0;
3957 hash_set<tree> save_exprs;
3958 new_ctx.save_exprs = &save_exprs;
3960 cxx_eval_constant_expression (&new_ctx, body, /*lval*/false,
3961 non_constant_p, overflow_p, jump_target);
3963 /* Forget saved values of SAVE_EXPRs. */
3964 for (hash_set<tree>::iterator iter = save_exprs.begin();
3965 iter != save_exprs.end(); ++iter)
3966 new_ctx.values->remove (*iter);
3967 if (++count >= constexpr_loop_limit)
3969 if (!ctx->quiet)
3970 error_at (EXPR_LOC_OR_LOC (t, input_location),
3971 "%<constexpr%> loop iteration count exceeds limit of %d "
3972 "(use -fconstexpr-loop-limit= to increase the limit)",
3973 constexpr_loop_limit);
3974 *non_constant_p = true;
3975 break;
3978 while (!returns (jump_target)
3979 && !breaks (jump_target)
3980 && !switches (jump_target)
3981 && !*non_constant_p);
3983 if (breaks (jump_target))
3984 *jump_target = NULL_TREE;
3986 return NULL_TREE;
3989 /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
3990 semantics. */
3992 static tree
3993 cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t,
3994 bool *non_constant_p, bool *overflow_p,
3995 tree *jump_target)
3997 tree cond = TREE_OPERAND (t, 0);
3998 cond = cxx_eval_constant_expression (ctx, cond, false,
3999 non_constant_p, overflow_p);
4000 VERIFY_CONSTANT (cond);
4001 *jump_target = cond;
4003 tree body = TREE_OPERAND (t, 1);
4004 constexpr_ctx new_ctx = *ctx;
4005 constexpr_switch_state css = css_default_not_seen;
4006 new_ctx.css_state = &css;
4007 cxx_eval_constant_expression (&new_ctx, body, false,
4008 non_constant_p, overflow_p, jump_target);
4009 if (switches (jump_target) && css == css_default_seen)
4011 /* If the SWITCH_EXPR body has default: label, process it once again,
4012 this time instructing label_matches to return true for default:
4013 label on switches (jump_target). */
4014 css = css_default_processing;
4015 cxx_eval_constant_expression (&new_ctx, body, false,
4016 non_constant_p, overflow_p, jump_target);
4018 if (breaks (jump_target) || switches (jump_target))
4019 *jump_target = NULL_TREE;
4020 return NULL_TREE;
4023 /* Find the object of TYPE under initialization in CTX. */
4025 static tree
4026 lookup_placeholder (const constexpr_ctx *ctx, bool lval, tree type)
4028 if (!ctx)
4029 return NULL_TREE;
4031 /* We could use ctx->object unconditionally, but using ctx->ctor when we
4032 can is a minor optimization. */
4033 if (!lval && ctx->ctor && same_type_p (TREE_TYPE (ctx->ctor), type))
4034 return ctx->ctor;
4036 if (!ctx->object)
4037 return NULL_TREE;
4039 /* Since an object cannot have a field of its own type, we can search outward
4040 from ctx->object to find the unique containing object of TYPE. */
4041 tree ob = ctx->object;
4042 while (ob)
4044 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob), type))
4045 break;
4046 if (handled_component_p (ob))
4047 ob = TREE_OPERAND (ob, 0);
4048 else
4049 ob = NULL_TREE;
4052 return ob;
4055 /* Attempt to reduce the expression T to a constant value.
4056 On failure, issue diagnostic and return error_mark_node. */
4057 /* FIXME unify with c_fully_fold */
4058 /* FIXME overflow_p is too global */
4060 static tree
4061 cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
4062 bool lval,
4063 bool *non_constant_p, bool *overflow_p,
4064 tree *jump_target)
4066 constexpr_ctx new_ctx;
4067 tree r = t;
4069 if (jump_target && *jump_target)
4071 /* If we are jumping, ignore all statements/expressions except those
4072 that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */
4073 switch (TREE_CODE (t))
4075 case BIND_EXPR:
4076 case STATEMENT_LIST:
4077 case LOOP_EXPR:
4078 case COND_EXPR:
4079 break;
4080 case LABEL_EXPR:
4081 case CASE_LABEL_EXPR:
4082 if (label_matches (ctx, jump_target, t))
4083 /* Found it. */
4084 *jump_target = NULL_TREE;
4085 return NULL_TREE;
4086 default:
4087 return NULL_TREE;
4090 if (t == error_mark_node)
4092 *non_constant_p = true;
4093 return t;
4095 if (CONSTANT_CLASS_P (t))
4097 if (TREE_OVERFLOW (t))
4099 if (!ctx->quiet)
4100 permerror (input_location, "overflow in constant expression");
4101 if (!flag_permissive || ctx->quiet)
4102 *overflow_p = true;
4105 if (TREE_CODE (t) == INTEGER_CST
4106 && TYPE_PTR_P (TREE_TYPE (t))
4107 && !integer_zerop (t))
4109 if (!ctx->quiet)
4110 error ("value %qE of type %qT is not a constant expression",
4111 t, TREE_TYPE (t));
4112 *non_constant_p = true;
4115 return t;
4118 tree_code tcode = TREE_CODE (t);
4119 switch (tcode)
4121 case RESULT_DECL:
4122 if (lval)
4123 return t;
4124 /* We ask for an rvalue for the RESULT_DECL when indirecting
4125 through an invisible reference, or in named return value
4126 optimization. */
4127 if (tree *p = ctx->values->get (t))
4128 return *p;
4129 else
4131 if (!ctx->quiet)
4132 error ("%qE is not a constant expression", t);
4133 *non_constant_p = true;
4135 break;
4137 case VAR_DECL:
4138 if (DECL_HAS_VALUE_EXPR_P (t))
4139 return cxx_eval_constant_expression (ctx, DECL_VALUE_EXPR (t),
4140 lval, non_constant_p, overflow_p);
4141 /* fall through */
4142 case CONST_DECL:
4143 /* We used to not check lval for CONST_DECL, but darwin.c uses
4144 CONST_DECL for aggregate constants. */
4145 if (lval)
4146 return t;
4147 if (COMPLETE_TYPE_P (TREE_TYPE (t))
4148 && is_really_empty_class (TREE_TYPE (t)))
4150 /* If the class is empty, we aren't actually loading anything. */
4151 r = build_constructor (TREE_TYPE (t), NULL);
4152 TREE_CONSTANT (r) = true;
4154 else if (ctx->strict)
4155 r = decl_really_constant_value (t);
4156 else
4157 r = decl_constant_value (t);
4158 if (TREE_CODE (r) == TARGET_EXPR
4159 && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR)
4160 r = TARGET_EXPR_INITIAL (r);
4161 if (VAR_P (r))
4162 if (tree *p = ctx->values->get (r))
4163 if (*p != NULL_TREE)
4164 r = *p;
4165 if (DECL_P (r))
4167 if (!ctx->quiet)
4168 non_const_var_error (r);
4169 *non_constant_p = true;
4171 break;
4173 case DEBUG_BEGIN_STMT:
4174 /* ??? It might be nice to retain this information somehow, so
4175 as to be able to step into a constexpr function call. */
4176 /* Fall through. */
4178 case FUNCTION_DECL:
4179 case TEMPLATE_DECL:
4180 case LABEL_DECL:
4181 case LABEL_EXPR:
4182 case CASE_LABEL_EXPR:
4183 case PREDICT_EXPR:
4184 return t;
4186 case PARM_DECL:
4187 if (lval && !TYPE_REF_P (TREE_TYPE (t)))
4188 /* glvalue use. */;
4189 else if (tree *p = ctx->values->get (r))
4190 r = *p;
4191 else if (lval)
4192 /* Defer in case this is only used for its type. */;
4193 else if (TYPE_REF_P (TREE_TYPE (t)))
4194 /* Defer, there's no lvalue->rvalue conversion. */;
4195 else if (COMPLETE_TYPE_P (TREE_TYPE (t))
4196 && is_really_empty_class (TREE_TYPE (t)))
4198 /* If the class is empty, we aren't actually loading anything. */
4199 r = build_constructor (TREE_TYPE (t), NULL);
4200 TREE_CONSTANT (r) = true;
4202 else
4204 if (!ctx->quiet)
4205 error ("%qE is not a constant expression", t);
4206 *non_constant_p = true;
4208 break;
4210 case CALL_EXPR:
4211 case AGGR_INIT_EXPR:
4212 r = cxx_eval_call_expression (ctx, t, lval,
4213 non_constant_p, overflow_p);
4214 break;
4216 case DECL_EXPR:
4218 r = DECL_EXPR_DECL (t);
4219 if (AGGREGATE_TYPE_P (TREE_TYPE (r))
4220 || VECTOR_TYPE_P (TREE_TYPE (r)))
4222 new_ctx = *ctx;
4223 new_ctx.object = r;
4224 new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL);
4225 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx.ctor) = true;
4226 new_ctx.values->put (r, new_ctx.ctor);
4227 ctx = &new_ctx;
4230 if (tree init = DECL_INITIAL (r))
4232 init = cxx_eval_constant_expression (ctx, init,
4233 false,
4234 non_constant_p, overflow_p);
4235 /* Don't share a CONSTRUCTOR that might be changed. */
4236 init = unshare_constructor (init);
4237 ctx->values->put (r, init);
4239 else if (ctx == &new_ctx)
4240 /* We gave it a CONSTRUCTOR above. */;
4241 else
4242 ctx->values->put (r, NULL_TREE);
4244 break;
4246 case TARGET_EXPR:
4247 if (!literal_type_p (TREE_TYPE (t)))
4249 if (!ctx->quiet)
4251 error ("temporary of non-literal type %qT in a "
4252 "constant expression", TREE_TYPE (t));
4253 explain_non_literal_class (TREE_TYPE (t));
4255 *non_constant_p = true;
4256 break;
4258 if ((AGGREGATE_TYPE_P (TREE_TYPE (t)) || VECTOR_TYPE_P (TREE_TYPE (t))))
4260 /* We're being expanded without an explicit target, so start
4261 initializing a new object; expansion with an explicit target
4262 strips the TARGET_EXPR before we get here. */
4263 new_ctx = *ctx;
4264 new_ctx.ctor = build_constructor (TREE_TYPE (t), NULL);
4265 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx.ctor) = true;
4266 new_ctx.object = TARGET_EXPR_SLOT (t);
4267 ctx->values->put (new_ctx.object, new_ctx.ctor);
4268 ctx = &new_ctx;
4270 /* Pass false for 'lval' because this indicates
4271 initialization of a temporary. */
4272 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
4273 false,
4274 non_constant_p, overflow_p);
4275 if (!*non_constant_p)
4276 /* Adjust the type of the result to the type of the temporary. */
4277 r = adjust_temp_type (TREE_TYPE (t), r);
4278 if (lval)
4280 tree slot = TARGET_EXPR_SLOT (t);
4281 r = unshare_constructor (r);
4282 ctx->values->put (slot, r);
4283 return slot;
4285 break;
4287 case INIT_EXPR:
4288 case MODIFY_EXPR:
4289 gcc_assert (jump_target == NULL || *jump_target == NULL_TREE);
4290 r = cxx_eval_store_expression (ctx, t, lval,
4291 non_constant_p, overflow_p);
4292 break;
4294 case SCOPE_REF:
4295 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
4296 lval,
4297 non_constant_p, overflow_p);
4298 break;
4300 case RETURN_EXPR:
4301 if (TREE_OPERAND (t, 0) != NULL_TREE)
4302 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
4303 lval,
4304 non_constant_p, overflow_p);
4305 if (jump_target)
4306 *jump_target = t;
4307 else
4309 /* Can happen with ({ return true; }) && false; passed to
4310 maybe_constant_value. There is nothing to jump over in this
4311 case, and the bug will be diagnosed later. */
4312 gcc_assert (ctx->quiet);
4313 *non_constant_p = true;
4315 break;
4317 case SAVE_EXPR:
4318 /* Avoid evaluating a SAVE_EXPR more than once. */
4319 if (tree *p = ctx->values->get (t))
4320 r = *p;
4321 else
4323 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), false,
4324 non_constant_p, overflow_p);
4325 ctx->values->put (t, r);
4326 if (ctx->save_exprs)
4327 ctx->save_exprs->add (t);
4329 break;
4331 case NON_LVALUE_EXPR:
4332 case TRY_CATCH_EXPR:
4333 case TRY_BLOCK:
4334 case CLEANUP_POINT_EXPR:
4335 case MUST_NOT_THROW_EXPR:
4336 case EXPR_STMT:
4337 case EH_SPEC_BLOCK:
4338 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
4339 lval,
4340 non_constant_p, overflow_p,
4341 jump_target);
4342 break;
4344 case TRY_FINALLY_EXPR:
4345 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
4346 non_constant_p, overflow_p,
4347 jump_target);
4348 if (!*non_constant_p)
4349 /* Also evaluate the cleanup. */
4350 cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), true,
4351 non_constant_p, overflow_p,
4352 jump_target);
4353 break;
4355 /* These differ from cxx_eval_unary_expression in that this doesn't
4356 check for a constant operand or result; an address can be
4357 constant without its operand being, and vice versa. */
4358 case MEM_REF:
4359 case INDIRECT_REF:
4360 r = cxx_eval_indirect_ref (ctx, t, lval,
4361 non_constant_p, overflow_p);
4362 break;
4364 case ADDR_EXPR:
4366 tree oldop = TREE_OPERAND (t, 0);
4367 tree op = cxx_eval_constant_expression (ctx, oldop,
4368 /*lval*/true,
4369 non_constant_p, overflow_p);
4370 /* Don't VERIFY_CONSTANT here. */
4371 if (*non_constant_p)
4372 return t;
4373 gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR);
4374 /* This function does more aggressive folding than fold itself. */
4375 r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
4376 if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
4377 return t;
4378 break;
4381 case REALPART_EXPR:
4382 case IMAGPART_EXPR:
4383 if (lval)
4385 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
4386 non_constant_p, overflow_p);
4387 if (r == error_mark_node)
4389 else if (r == TREE_OPERAND (t, 0))
4390 r = t;
4391 else
4392 r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), r);
4393 break;
4395 /* FALLTHRU */
4396 case CONJ_EXPR:
4397 case FIX_TRUNC_EXPR:
4398 case FLOAT_EXPR:
4399 case NEGATE_EXPR:
4400 case ABS_EXPR:
4401 case BIT_NOT_EXPR:
4402 case TRUTH_NOT_EXPR:
4403 case FIXED_CONVERT_EXPR:
4404 r = cxx_eval_unary_expression (ctx, t, lval,
4405 non_constant_p, overflow_p);
4406 break;
4408 case SIZEOF_EXPR:
4409 r = fold_sizeof_expr (t);
4410 VERIFY_CONSTANT (r);
4411 break;
4413 case COMPOUND_EXPR:
4415 /* check_return_expr sometimes wraps a TARGET_EXPR in a
4416 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
4417 introduced by build_call_a. */
4418 tree op0 = TREE_OPERAND (t, 0);
4419 tree op1 = TREE_OPERAND (t, 1);
4420 STRIP_NOPS (op1);
4421 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
4422 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
4423 r = cxx_eval_constant_expression (ctx, op0,
4424 lval, non_constant_p, overflow_p,
4425 jump_target);
4426 else
4428 /* Check that the LHS is constant and then discard it. */
4429 cxx_eval_constant_expression (ctx, op0,
4430 true, non_constant_p, overflow_p,
4431 jump_target);
4432 if (*non_constant_p)
4433 return t;
4434 op1 = TREE_OPERAND (t, 1);
4435 r = cxx_eval_constant_expression (ctx, op1,
4436 lval, non_constant_p, overflow_p,
4437 jump_target);
4440 break;
4442 case POINTER_PLUS_EXPR:
4443 case POINTER_DIFF_EXPR:
4444 case PLUS_EXPR:
4445 case MINUS_EXPR:
4446 case MULT_EXPR:
4447 case TRUNC_DIV_EXPR:
4448 case CEIL_DIV_EXPR:
4449 case FLOOR_DIV_EXPR:
4450 case ROUND_DIV_EXPR:
4451 case TRUNC_MOD_EXPR:
4452 case CEIL_MOD_EXPR:
4453 case ROUND_MOD_EXPR:
4454 case RDIV_EXPR:
4455 case EXACT_DIV_EXPR:
4456 case MIN_EXPR:
4457 case MAX_EXPR:
4458 case LSHIFT_EXPR:
4459 case RSHIFT_EXPR:
4460 case LROTATE_EXPR:
4461 case RROTATE_EXPR:
4462 case BIT_IOR_EXPR:
4463 case BIT_XOR_EXPR:
4464 case BIT_AND_EXPR:
4465 case TRUTH_XOR_EXPR:
4466 case LT_EXPR:
4467 case LE_EXPR:
4468 case GT_EXPR:
4469 case GE_EXPR:
4470 case EQ_EXPR:
4471 case NE_EXPR:
4472 case UNORDERED_EXPR:
4473 case ORDERED_EXPR:
4474 case UNLT_EXPR:
4475 case UNLE_EXPR:
4476 case UNGT_EXPR:
4477 case UNGE_EXPR:
4478 case UNEQ_EXPR:
4479 case LTGT_EXPR:
4480 case RANGE_EXPR:
4481 case COMPLEX_EXPR:
4482 r = cxx_eval_binary_expression (ctx, t, lval,
4483 non_constant_p, overflow_p);
4484 break;
4486 /* fold can introduce non-IF versions of these; still treat them as
4487 short-circuiting. */
4488 case TRUTH_AND_EXPR:
4489 case TRUTH_ANDIF_EXPR:
4490 r = cxx_eval_logical_expression (ctx, t, boolean_false_node,
4491 boolean_true_node,
4492 lval,
4493 non_constant_p, overflow_p);
4494 break;
4496 case TRUTH_OR_EXPR:
4497 case TRUTH_ORIF_EXPR:
4498 r = cxx_eval_logical_expression (ctx, t, boolean_true_node,
4499 boolean_false_node,
4500 lval,
4501 non_constant_p, overflow_p);
4502 break;
4504 case ARRAY_REF:
4505 r = cxx_eval_array_reference (ctx, t, lval,
4506 non_constant_p, overflow_p);
4507 break;
4509 case COMPONENT_REF:
4510 if (is_overloaded_fn (t))
4512 /* We can only get here in checking mode via
4513 build_non_dependent_expr, because any expression that
4514 calls or takes the address of the function will have
4515 pulled a FUNCTION_DECL out of the COMPONENT_REF. */
4516 gcc_checking_assert (ctx->quiet || errorcount);
4517 *non_constant_p = true;
4518 return t;
4520 r = cxx_eval_component_reference (ctx, t, lval,
4521 non_constant_p, overflow_p);
4522 break;
4524 case BIT_FIELD_REF:
4525 r = cxx_eval_bit_field_ref (ctx, t, lval,
4526 non_constant_p, overflow_p);
4527 break;
4529 case COND_EXPR:
4530 if (jump_target && *jump_target)
4532 /* When jumping to a label, the label might be either in the
4533 then or else blocks, so process then block first in skipping
4534 mode first, and if we are still in the skipping mode at its end,
4535 process the else block too. */
4536 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
4537 lval, non_constant_p, overflow_p,
4538 jump_target);
4539 if (*jump_target)
4540 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2),
4541 lval, non_constant_p, overflow_p,
4542 jump_target);
4543 break;
4545 r = cxx_eval_conditional_expression (ctx, t, lval,
4546 non_constant_p, overflow_p,
4547 jump_target);
4548 break;
4549 case VEC_COND_EXPR:
4550 r = cxx_eval_vector_conditional_expression (ctx, t, non_constant_p,
4551 overflow_p);
4552 break;
4554 case CONSTRUCTOR:
4555 if (TREE_CONSTANT (t))
4557 /* Don't re-process a constant CONSTRUCTOR, but do fold it to
4558 VECTOR_CST if applicable. */
4559 verify_constructor_flags (t);
4560 if (TREE_CONSTANT (t))
4561 return fold (t);
4563 r = cxx_eval_bare_aggregate (ctx, t, lval,
4564 non_constant_p, overflow_p);
4565 break;
4567 case VEC_INIT_EXPR:
4568 /* We can get this in a defaulted constructor for a class with a
4569 non-static data member of array type. Either the initializer will
4570 be NULL, meaning default-initialization, or it will be an lvalue
4571 or xvalue of the same type, meaning direct-initialization from the
4572 corresponding member. */
4573 r = cxx_eval_vec_init (ctx, t, lval,
4574 non_constant_p, overflow_p);
4575 break;
4577 case FMA_EXPR:
4578 case VEC_PERM_EXPR:
4579 r = cxx_eval_trinary_expression (ctx, t, lval,
4580 non_constant_p, overflow_p);
4581 break;
4583 case NOP_EXPR:
4584 if (REINTERPRET_CAST_P (t))
4586 if (!ctx->quiet)
4587 error_at (EXPR_LOC_OR_LOC (t, input_location),
4588 "a reinterpret_cast is not a constant expression");
4589 *non_constant_p = true;
4590 return t;
4592 /* FALLTHROUGH. */
4593 case CONVERT_EXPR:
4594 case VIEW_CONVERT_EXPR:
4595 case UNARY_PLUS_EXPR:
4597 tree oldop = TREE_OPERAND (t, 0);
4599 tree op = cxx_eval_constant_expression (ctx, oldop,
4600 lval,
4601 non_constant_p, overflow_p);
4602 if (*non_constant_p)
4603 return t;
4604 tree type = TREE_TYPE (t);
4605 if (TREE_CODE (op) == PTRMEM_CST
4606 && !TYPE_PTRMEM_P (type))
4607 op = cplus_expand_constant (op);
4609 if (TREE_CODE (op) == PTRMEM_CST && tcode == NOP_EXPR)
4611 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (op))
4612 && !can_convert_qual (type, op))
4613 op = cplus_expand_constant (op);
4614 return cp_fold_convert (type, op);
4617 if (POINTER_TYPE_P (type) && TREE_CODE (op) == INTEGER_CST)
4619 if (integer_zerop (op))
4621 if (TYPE_REF_P (type))
4623 if (!ctx->quiet)
4624 error_at (EXPR_LOC_OR_LOC (t, input_location),
4625 "dereferencing a null pointer");
4626 *non_constant_p = true;
4627 return t;
4629 else if (TYPE_PTR_P (TREE_TYPE (op)))
4631 tree from = TREE_TYPE (op);
4633 if (!can_convert (type, from, tf_none))
4635 if (!ctx->quiet)
4636 error_at (EXPR_LOC_OR_LOC (t, input_location),
4637 "conversion of %qT null pointer to %qT "
4638 "is not a constant expression",
4639 from, type);
4640 *non_constant_p = true;
4641 return t;
4645 else
4647 /* This detects for example:
4648 reinterpret_cast<void*>(sizeof 0)
4650 if (!ctx->quiet)
4651 error_at (EXPR_LOC_OR_LOC (t, input_location),
4652 "%<reinterpret_cast<%T>(%E)%> is not "
4653 "a constant expression",
4654 type, op);
4655 *non_constant_p = true;
4656 return t;
4660 if (op == oldop && tcode != UNARY_PLUS_EXPR)
4661 /* We didn't fold at the top so we could check for ptr-int
4662 conversion. */
4663 return fold (t);
4665 if (tcode == UNARY_PLUS_EXPR)
4666 r = fold_convert (TREE_TYPE (t), op);
4667 else
4668 r = fold_build1 (tcode, type, op);
4670 /* Conversion of an out-of-range value has implementation-defined
4671 behavior; the language considers it different from arithmetic
4672 overflow, which is undefined. */
4673 if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op))
4674 TREE_OVERFLOW (r) = false;
4676 break;
4678 case EMPTY_CLASS_EXPR:
4679 /* This is good enough for a function argument that might not get
4680 used, and they can't do anything with it, so just return it. */
4681 return t;
4683 case STATEMENT_LIST:
4684 new_ctx = *ctx;
4685 new_ctx.ctor = new_ctx.object = NULL_TREE;
4686 return cxx_eval_statement_list (&new_ctx, t,
4687 non_constant_p, overflow_p, jump_target);
4689 case BIND_EXPR:
4690 return cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t),
4691 lval,
4692 non_constant_p, overflow_p,
4693 jump_target);
4695 case PREINCREMENT_EXPR:
4696 case POSTINCREMENT_EXPR:
4697 case PREDECREMENT_EXPR:
4698 case POSTDECREMENT_EXPR:
4699 return cxx_eval_increment_expression (ctx, t,
4700 lval, non_constant_p, overflow_p);
4702 case LAMBDA_EXPR:
4703 case NEW_EXPR:
4704 case VEC_NEW_EXPR:
4705 case DELETE_EXPR:
4706 case VEC_DELETE_EXPR:
4707 case THROW_EXPR:
4708 case MODOP_EXPR:
4709 /* GCC internal stuff. */
4710 case VA_ARG_EXPR:
4711 case OBJ_TYPE_REF:
4712 case NON_DEPENDENT_EXPR:
4713 case BASELINK:
4714 case OFFSET_REF:
4715 if (!ctx->quiet)
4716 error_at (EXPR_LOC_OR_LOC (t, input_location),
4717 "expression %qE is not a constant expression", t);
4718 *non_constant_p = true;
4719 break;
4721 case PLACEHOLDER_EXPR:
4722 /* Use of the value or address of the current object. */
4723 if (tree ctor = lookup_placeholder (ctx, lval, TREE_TYPE (t)))
4724 return cxx_eval_constant_expression (ctx, ctor, lval,
4725 non_constant_p, overflow_p);
4726 /* A placeholder without a referent. We can get here when
4727 checking whether NSDMIs are noexcept, or in massage_init_elt;
4728 just say it's non-constant for now. */
4729 gcc_assert (ctx->quiet);
4730 *non_constant_p = true;
4731 break;
4733 case EXIT_EXPR:
4735 tree cond = TREE_OPERAND (t, 0);
4736 cond = cxx_eval_constant_expression (ctx, cond, /*lval*/false,
4737 non_constant_p, overflow_p);
4738 VERIFY_CONSTANT (cond);
4739 if (integer_nonzerop (cond))
4740 *jump_target = t;
4742 break;
4744 case GOTO_EXPR:
4745 *jump_target = TREE_OPERAND (t, 0);
4746 gcc_assert (breaks (jump_target) || continues (jump_target)
4747 /* Allow for jumping to a cdtor_label. */
4748 || returns (jump_target));
4749 break;
4751 case LOOP_EXPR:
4752 cxx_eval_loop_expr (ctx, t,
4753 non_constant_p, overflow_p, jump_target);
4754 break;
4756 case SWITCH_EXPR:
4757 cxx_eval_switch_expr (ctx, t,
4758 non_constant_p, overflow_p, jump_target);
4759 break;
4761 case REQUIRES_EXPR:
4762 /* It's possible to get a requires-expression in a constant
4763 expression. For example:
4765 template<typename T> concept bool C() {
4766 return requires (T t) { t; };
4769 template<typename T> requires !C<T>() void f(T);
4771 Normalization leaves f with the associated constraint
4772 '!requires (T t) { ... }' which is not transformed into
4773 a constraint. */
4774 if (!processing_template_decl)
4775 return evaluate_constraint_expression (t, NULL_TREE);
4776 else
4777 *non_constant_p = true;
4778 return t;
4780 case ANNOTATE_EXPR:
4781 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
4782 lval,
4783 non_constant_p, overflow_p,
4784 jump_target);
4785 break;
4787 case USING_STMT:
4788 r = void_node;
4789 break;
4791 default:
4792 if (STATEMENT_CODE_P (TREE_CODE (t)))
4794 /* This function doesn't know how to deal with pre-genericize
4795 statements; this can only happen with statement-expressions,
4796 so for now just fail. */
4797 if (!ctx->quiet)
4798 error_at (EXPR_LOCATION (t),
4799 "statement is not a constant expression");
4801 else
4802 internal_error ("unexpected expression %qE of kind %s", t,
4803 get_tree_code_name (TREE_CODE (t)));
4804 *non_constant_p = true;
4805 break;
4808 if (r == error_mark_node)
4809 *non_constant_p = true;
4811 if (*non_constant_p)
4812 return t;
4813 else
4814 return r;
4817 static tree
4818 cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant,
4819 bool strict = true, tree object = NULL_TREE)
4821 auto_timevar time (TV_CONSTEXPR);
4823 bool non_constant_p = false;
4824 bool overflow_p = false;
4825 hash_map<tree,tree> map;
4827 constexpr_ctx ctx = { NULL, &map, NULL, NULL, NULL, NULL,
4828 allow_non_constant, strict };
4830 tree type = initialized_type (t);
4831 tree r = t;
4832 if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
4834 /* In C++14 an NSDMI can participate in aggregate initialization,
4835 and can refer to the address of the object being initialized, so
4836 we need to pass in the relevant VAR_DECL if we want to do the
4837 evaluation in a single pass. The evaluation will dynamically
4838 update ctx.values for the VAR_DECL. We use the same strategy
4839 for C++11 constexpr constructors that refer to the object being
4840 initialized. */
4841 ctx.ctor = build_constructor (type, NULL);
4842 CONSTRUCTOR_NO_IMPLICIT_ZERO (ctx.ctor) = true;
4843 if (!object)
4845 if (TREE_CODE (t) == TARGET_EXPR)
4846 object = TARGET_EXPR_SLOT (t);
4847 else if (TREE_CODE (t) == AGGR_INIT_EXPR)
4848 object = AGGR_INIT_EXPR_SLOT (t);
4850 ctx.object = object;
4851 if (object)
4852 gcc_assert (same_type_ignoring_top_level_qualifiers_p
4853 (type, TREE_TYPE (object)));
4854 if (object && DECL_P (object))
4855 map.put (object, ctx.ctor);
4856 if (TREE_CODE (r) == TARGET_EXPR)
4857 /* Avoid creating another CONSTRUCTOR when we expand the
4858 TARGET_EXPR. */
4859 r = TARGET_EXPR_INITIAL (r);
4862 r = cxx_eval_constant_expression (&ctx, r,
4863 false, &non_constant_p, &overflow_p);
4865 verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p);
4867 /* Mutable logic is a bit tricky: we want to allow initialization of
4868 constexpr variables with mutable members, but we can't copy those
4869 members to another constexpr variable. */
4870 if (TREE_CODE (r) == CONSTRUCTOR
4871 && CONSTRUCTOR_MUTABLE_POISON (r))
4873 if (!allow_non_constant)
4874 error ("%qE is not a constant expression because it refers to "
4875 "mutable subobjects of %qT", t, type);
4876 non_constant_p = true;
4879 if (TREE_CODE (r) == CONSTRUCTOR
4880 && CONSTRUCTOR_NO_IMPLICIT_ZERO (r))
4882 if (!allow_non_constant)
4883 error ("%qE is not a constant expression because it refers to "
4884 "an incompletely initialized variable", t);
4885 TREE_CONSTANT (r) = false;
4886 non_constant_p = true;
4889 /* Technically we should check this for all subexpressions, but that
4890 runs into problems with our internal representation of pointer
4891 subtraction and the 5.19 rules are still in flux. */
4892 if (CONVERT_EXPR_CODE_P (TREE_CODE (r))
4893 && ARITHMETIC_TYPE_P (TREE_TYPE (r))
4894 && TREE_CODE (TREE_OPERAND (r, 0)) == ADDR_EXPR)
4896 if (!allow_non_constant)
4897 error ("conversion from pointer type %qT "
4898 "to arithmetic type %qT in a constant expression",
4899 TREE_TYPE (TREE_OPERAND (r, 0)), TREE_TYPE (r));
4900 non_constant_p = true;
4903 if (!non_constant_p && overflow_p)
4904 non_constant_p = true;
4906 /* Unshare the result unless it's a CONSTRUCTOR in which case it's already
4907 unshared. */
4908 bool should_unshare = true;
4909 if (r == t || TREE_CODE (r) == CONSTRUCTOR)
4910 should_unshare = false;
4912 if (non_constant_p && !allow_non_constant)
4913 return error_mark_node;
4914 else if (non_constant_p && TREE_CONSTANT (r))
4916 /* This isn't actually constant, so unset TREE_CONSTANT.
4917 Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires
4918 it to be set if it is invariant address, even when it is not
4919 a valid C++ constant expression. Wrap it with a NOP_EXPR
4920 instead. */
4921 if (EXPR_P (r) && TREE_CODE (r) != ADDR_EXPR)
4922 r = copy_node (r);
4923 else if (TREE_CODE (r) == CONSTRUCTOR)
4924 r = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (r), r);
4925 else
4926 r = build_nop (TREE_TYPE (r), r);
4927 TREE_CONSTANT (r) = false;
4929 else if (non_constant_p || r == t)
4930 return t;
4932 if (should_unshare)
4933 r = unshare_expr (r);
4935 if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
4937 if (TREE_CODE (t) == TARGET_EXPR
4938 && TARGET_EXPR_INITIAL (t) == r)
4939 return t;
4940 else
4942 r = get_target_expr (r);
4943 TREE_CONSTANT (r) = true;
4944 return r;
4947 else
4948 return r;
4951 /* Returns true if T is a valid subexpression of a constant expression,
4952 even if it isn't itself a constant expression. */
4954 bool
4955 is_sub_constant_expr (tree t)
4957 bool non_constant_p = false;
4958 bool overflow_p = false;
4959 hash_map <tree, tree> map;
4961 constexpr_ctx ctx = { NULL, &map, NULL, NULL, NULL, NULL, true, true };
4963 cxx_eval_constant_expression (&ctx, t, false, &non_constant_p,
4964 &overflow_p);
4965 return !non_constant_p && !overflow_p;
4968 /* If T represents a constant expression returns its reduced value.
4969 Otherwise return error_mark_node. If T is dependent, then
4970 return NULL. */
4972 tree
4973 cxx_constant_value (tree t, tree decl)
4975 return cxx_eval_outermost_constant_expr (t, false, true, decl);
4978 /* Helper routine for fold_simple function. Either return simplified
4979 expression T, otherwise NULL_TREE.
4980 In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold
4981 even if we are within template-declaration. So be careful on call, as in
4982 such case types can be undefined. */
4984 static tree
4985 fold_simple_1 (tree t)
4987 tree op1;
4988 enum tree_code code = TREE_CODE (t);
4990 switch (code)
4992 case INTEGER_CST:
4993 case REAL_CST:
4994 case VECTOR_CST:
4995 case FIXED_CST:
4996 case COMPLEX_CST:
4997 return t;
4999 case SIZEOF_EXPR:
5000 return fold_sizeof_expr (t);
5002 case ABS_EXPR:
5003 case CONJ_EXPR:
5004 case REALPART_EXPR:
5005 case IMAGPART_EXPR:
5006 case NEGATE_EXPR:
5007 case BIT_NOT_EXPR:
5008 case TRUTH_NOT_EXPR:
5009 case NOP_EXPR:
5010 case VIEW_CONVERT_EXPR:
5011 case CONVERT_EXPR:
5012 case FLOAT_EXPR:
5013 case FIX_TRUNC_EXPR:
5014 case FIXED_CONVERT_EXPR:
5015 case ADDR_SPACE_CONVERT_EXPR:
5017 op1 = TREE_OPERAND (t, 0);
5019 t = const_unop (code, TREE_TYPE (t), op1);
5020 if (!t)
5021 return NULL_TREE;
5023 if (CONVERT_EXPR_CODE_P (code)
5024 && TREE_OVERFLOW_P (t) && !TREE_OVERFLOW_P (op1))
5025 TREE_OVERFLOW (t) = false;
5026 return t;
5028 default:
5029 return NULL_TREE;
5033 /* If T is a simple constant expression, returns its simplified value.
5034 Otherwise returns T. In contrast to maybe_constant_value we
5035 simplify only few operations on constant-expressions, and we don't
5036 try to simplify constexpressions. */
5038 tree
5039 fold_simple (tree t)
5041 if (processing_template_decl)
5042 return t;
5044 tree r = fold_simple_1 (t);
5045 if (r)
5046 return r;
5048 return t;
5051 /* If T is a constant expression, returns its reduced value.
5052 Otherwise, if T does not have TREE_CONSTANT set, returns T.
5053 Otherwise, returns a version of T without TREE_CONSTANT. */
5055 static GTY((deletable)) hash_map<tree, tree> *cv_cache;
5057 tree
5058 maybe_constant_value (tree t, tree decl)
5060 tree r;
5062 if (!is_nondependent_constant_expression (t))
5064 if (TREE_OVERFLOW_P (t))
5066 t = build_nop (TREE_TYPE (t), t);
5067 TREE_CONSTANT (t) = false;
5069 return t;
5071 else if (CONSTANT_CLASS_P (t))
5072 /* No caching or evaluation needed. */
5073 return t;
5075 if (cv_cache == NULL)
5076 cv_cache = hash_map<tree, tree>::create_ggc (101);
5077 if (tree *cached = cv_cache->get (t))
5078 return *cached;
5080 r = cxx_eval_outermost_constant_expr (t, true, true, decl);
5081 gcc_checking_assert (r == t
5082 || CONVERT_EXPR_P (t)
5083 || TREE_CODE (t) == VIEW_CONVERT_EXPR
5084 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
5085 || !cp_tree_equal (r, t));
5086 cv_cache->put (t, r);
5087 return r;
5090 /* Dispose of the whole CV_CACHE. */
5092 static void
5093 clear_cv_cache (void)
5095 if (cv_cache != NULL)
5096 cv_cache->empty ();
5099 /* Dispose of the whole CV_CACHE and FOLD_CACHE. */
5101 void
5102 clear_cv_and_fold_caches (void)
5104 clear_cv_cache ();
5105 clear_fold_cache ();
5108 /* Like maybe_constant_value but first fully instantiate the argument.
5110 Note: this is equivalent to instantiate_non_dependent_expr_sfinae
5111 (t, tf_none) followed by maybe_constant_value but is more efficient,
5112 because calls instantiation_dependent_expression_p and
5113 potential_constant_expression at most once. */
5115 tree
5116 fold_non_dependent_expr (tree t)
5118 if (t == NULL_TREE)
5119 return NULL_TREE;
5121 /* If we're in a template, but T isn't value dependent, simplify
5122 it. We're supposed to treat:
5124 template <typename T> void f(T[1 + 1]);
5125 template <typename T> void f(T[2]);
5127 as two declarations of the same function, for example. */
5128 if (processing_template_decl)
5130 if (is_nondependent_constant_expression (t))
5132 processing_template_decl_sentinel s;
5133 t = instantiate_non_dependent_expr_internal (t, tf_none);
5135 if (type_unknown_p (t)
5136 || BRACE_ENCLOSED_INITIALIZER_P (t))
5138 if (TREE_OVERFLOW_P (t))
5140 t = build_nop (TREE_TYPE (t), t);
5141 TREE_CONSTANT (t) = false;
5143 return t;
5146 tree r = cxx_eval_outermost_constant_expr (t, true, true, NULL_TREE);
5147 /* cp_tree_equal looks through NOPs, so allow them. */
5148 gcc_checking_assert (r == t
5149 || CONVERT_EXPR_P (t)
5150 || TREE_CODE (t) == VIEW_CONVERT_EXPR
5151 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
5152 || !cp_tree_equal (r, t));
5153 return r;
5155 else if (TREE_OVERFLOW_P (t))
5157 t = build_nop (TREE_TYPE (t), t);
5158 TREE_CONSTANT (t) = false;
5160 return t;
5163 return maybe_constant_value (t);
5166 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
5167 than wrapped in a TARGET_EXPR. */
5169 static tree
5170 maybe_constant_init_1 (tree t, tree decl, bool allow_non_constant)
5172 if (!t)
5173 return t;
5174 if (TREE_CODE (t) == EXPR_STMT)
5175 t = TREE_OPERAND (t, 0);
5176 if (TREE_CODE (t) == CONVERT_EXPR
5177 && VOID_TYPE_P (TREE_TYPE (t)))
5178 t = TREE_OPERAND (t, 0);
5179 if (TREE_CODE (t) == INIT_EXPR)
5180 t = TREE_OPERAND (t, 1);
5181 if (TREE_CODE (t) == TARGET_EXPR)
5182 t = TARGET_EXPR_INITIAL (t);
5183 if (!is_nondependent_static_init_expression (t))
5184 /* Don't try to evaluate it. */;
5185 else if (CONSTANT_CLASS_P (t) && allow_non_constant)
5186 /* No evaluation needed. */;
5187 else
5188 t = cxx_eval_outermost_constant_expr (t, allow_non_constant, false, decl);
5189 if (TREE_CODE (t) == TARGET_EXPR)
5191 tree init = TARGET_EXPR_INITIAL (t);
5192 if (TREE_CODE (init) == CONSTRUCTOR)
5193 t = init;
5195 return t;
5198 /* Wrapper for maybe_constant_init_1 which permits non constants. */
5200 tree
5201 maybe_constant_init (tree t, tree decl)
5203 return maybe_constant_init_1 (t, decl, true);
5206 /* Wrapper for maybe_constant_init_1 which does not permit non constants. */
5208 tree
5209 cxx_constant_init (tree t, tree decl)
5211 return maybe_constant_init_1 (t, decl, false);
5214 #if 0
5215 /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
5216 /* Return true if the object referred to by REF has automatic or thread
5217 local storage. */
5219 enum { ck_ok, ck_bad, ck_unknown };
5220 static int
5221 check_automatic_or_tls (tree ref)
5223 machine_mode mode;
5224 poly_int64 bitsize, bitpos;
5225 tree offset;
5226 int volatilep = 0, unsignedp = 0;
5227 tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset,
5228 &mode, &unsignedp, &volatilep, false);
5229 duration_kind dk;
5231 /* If there isn't a decl in the middle, we don't know the linkage here,
5232 and this isn't a constant expression anyway. */
5233 if (!DECL_P (decl))
5234 return ck_unknown;
5235 dk = decl_storage_duration (decl);
5236 return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok;
5238 #endif
5240 /* Return true if T denotes a potentially constant expression. Issue
5241 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
5242 an lvalue-rvalue conversion is implied. If NOW is true, we want to
5243 consider the expression in the current context, independent of constexpr
5244 substitution.
5246 C++0x [expr.const] used to say
5248 6 An expression is a potential constant expression if it is
5249 a constant expression where all occurrences of function
5250 parameters are replaced by arbitrary constant expressions
5251 of the appropriate type.
5253 2 A conditional expression is a constant expression unless it
5254 involves one of the following as a potentially evaluated
5255 subexpression (3.2), but subexpressions of logical AND (5.14),
5256 logical OR (5.15), and conditional (5.16) operations that are
5257 not evaluated are not considered. */
5259 static bool
5260 potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
5261 tsubst_flags_t flags)
5263 #define RECUR(T,RV) \
5264 potential_constant_expression_1 ((T), (RV), strict, now, flags)
5266 enum { any = false, rval = true };
5267 int i;
5268 tree tmp;
5270 if (t == error_mark_node)
5271 return false;
5272 if (t == NULL_TREE)
5273 return true;
5274 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
5275 if (TREE_THIS_VOLATILE (t) && !DECL_P (t))
5277 if (flags & tf_error)
5278 error_at (loc, "expression %qE has side-effects", t);
5279 return false;
5281 if (CONSTANT_CLASS_P (t))
5282 return true;
5283 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED)
5284 && TREE_TYPE (t) == error_mark_node)
5285 return false;
5287 switch (TREE_CODE (t))
5289 case FUNCTION_DECL:
5290 case BASELINK:
5291 case TEMPLATE_DECL:
5292 case OVERLOAD:
5293 case TEMPLATE_ID_EXPR:
5294 case LABEL_DECL:
5295 case LABEL_EXPR:
5296 case CASE_LABEL_EXPR:
5297 case CONST_DECL:
5298 case SIZEOF_EXPR:
5299 case ALIGNOF_EXPR:
5300 case OFFSETOF_EXPR:
5301 case NOEXCEPT_EXPR:
5302 case TEMPLATE_PARM_INDEX:
5303 case TRAIT_EXPR:
5304 case IDENTIFIER_NODE:
5305 case USERDEF_LITERAL:
5306 /* We can see a FIELD_DECL in a pointer-to-member expression. */
5307 case FIELD_DECL:
5308 case RESULT_DECL:
5309 case USING_DECL:
5310 case USING_STMT:
5311 case PLACEHOLDER_EXPR:
5312 case BREAK_STMT:
5313 case CONTINUE_STMT:
5314 case REQUIRES_EXPR:
5315 case STATIC_ASSERT:
5316 case DEBUG_BEGIN_STMT:
5317 return true;
5319 case PARM_DECL:
5320 if (now)
5322 if (flags & tf_error)
5323 error ("%qE is not a constant expression", t);
5324 return false;
5326 return true;
5328 case AGGR_INIT_EXPR:
5329 case CALL_EXPR:
5330 /* -- an invocation of a function other than a constexpr function
5331 or a constexpr constructor. */
5333 tree fun = get_function_named_in_call (t);
5334 const int nargs = call_expr_nargs (t);
5335 i = 0;
5337 if (fun == NULL_TREE)
5339 /* Reset to allow the function to continue past the end
5340 of the block below. Otherwise return early. */
5341 bool bail = true;
5343 if (TREE_CODE (t) == CALL_EXPR
5344 && CALL_EXPR_FN (t) == NULL_TREE)
5345 switch (CALL_EXPR_IFN (t))
5347 /* These should be ignored, they are optimized away from
5348 constexpr functions. */
5349 case IFN_UBSAN_NULL:
5350 case IFN_UBSAN_BOUNDS:
5351 case IFN_UBSAN_VPTR:
5352 case IFN_FALLTHROUGH:
5353 return true;
5355 case IFN_ADD_OVERFLOW:
5356 case IFN_SUB_OVERFLOW:
5357 case IFN_MUL_OVERFLOW:
5358 case IFN_LAUNDER:
5359 bail = false;
5361 default:
5362 break;
5365 if (bail)
5367 /* fold_call_expr can't do anything with IFN calls. */
5368 if (flags & tf_error)
5369 error_at (loc, "call to internal function %qE", t);
5370 return false;
5374 if (fun && is_overloaded_fn (fun))
5376 if (TREE_CODE (fun) == FUNCTION_DECL)
5378 if (builtin_valid_in_constant_expr_p (fun))
5379 return true;
5380 if (!DECL_DECLARED_CONSTEXPR_P (fun)
5381 /* Allow any built-in function; if the expansion
5382 isn't constant, we'll deal with that then. */
5383 && !is_builtin_fn (fun))
5385 if (flags & tf_error)
5387 error_at (loc, "call to non-%<constexpr%> function %qD",
5388 fun);
5389 explain_invalid_constexpr_fn (fun);
5391 return false;
5393 /* A call to a non-static member function takes the address
5394 of the object as the first argument. But in a constant
5395 expression the address will be folded away, so look
5396 through it now. */
5397 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
5398 && !DECL_CONSTRUCTOR_P (fun))
5400 tree x = get_nth_callarg (t, 0);
5401 if (is_this_parameter (x))
5402 return true;
5403 /* Don't require an immediately constant value, as
5404 constexpr substitution might not use the value. */
5405 bool sub_now = false;
5406 if (!potential_constant_expression_1 (x, rval, strict,
5407 sub_now, flags))
5408 return false;
5409 i = 1;
5412 else
5414 if (!RECUR (fun, true))
5415 return false;
5416 fun = get_first_fn (fun);
5418 /* Skip initial arguments to base constructors. */
5419 if (DECL_BASE_CONSTRUCTOR_P (fun))
5420 i = num_artificial_parms_for (fun);
5421 fun = DECL_ORIGIN (fun);
5423 else if (fun)
5425 if (RECUR (fun, rval))
5426 /* Might end up being a constant function pointer. */;
5427 else
5428 return false;
5430 for (; i < nargs; ++i)
5432 tree x = get_nth_callarg (t, i);
5433 /* In a template, reference arguments haven't been converted to
5434 REFERENCE_TYPE and we might not even know if the parameter
5435 is a reference, so accept lvalue constants too. */
5436 bool rv = processing_template_decl ? any : rval;
5437 /* Don't require an immediately constant value, as constexpr
5438 substitution might not use the value of the argument. */
5439 bool sub_now = false;
5440 if (!potential_constant_expression_1 (x, rv, strict,
5441 sub_now, flags))
5442 return false;
5444 return true;
5447 case NON_LVALUE_EXPR:
5448 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
5449 -- an lvalue of integral type that refers to a non-volatile
5450 const variable or static data member initialized with
5451 constant expressions, or
5453 -- an lvalue of literal type that refers to non-volatile
5454 object defined with constexpr, or that refers to a
5455 sub-object of such an object; */
5456 return RECUR (TREE_OPERAND (t, 0), rval);
5458 case VAR_DECL:
5459 if (DECL_HAS_VALUE_EXPR_P (t))
5461 if (now && is_normal_capture_proxy (t))
5463 /* -- in a lambda-expression, a reference to this or to a
5464 variable with automatic storage duration defined outside that
5465 lambda-expression, where the reference would be an
5466 odr-use. */
5467 if (flags & tf_error)
5469 tree cap = DECL_CAPTURED_VARIABLE (t);
5470 error ("lambda capture of %qE is not a constant expression",
5471 cap);
5472 if (!want_rval && decl_constant_var_p (cap))
5473 inform (input_location, "because it is used as a glvalue");
5475 return false;
5477 return RECUR (DECL_VALUE_EXPR (t), rval);
5479 if (want_rval
5480 && !var_in_maybe_constexpr_fn (t)
5481 && !type_dependent_expression_p (t)
5482 && !decl_maybe_constant_var_p (t)
5483 && (strict
5484 || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t))
5485 || (DECL_INITIAL (t)
5486 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t)))
5487 && COMPLETE_TYPE_P (TREE_TYPE (t))
5488 && !is_really_empty_class (TREE_TYPE (t)))
5490 if (flags & tf_error)
5491 non_const_var_error (t);
5492 return false;
5494 return true;
5496 case NOP_EXPR:
5497 case CONVERT_EXPR:
5498 case VIEW_CONVERT_EXPR:
5499 /* -- a reinterpret_cast. FIXME not implemented, and this rule
5500 may change to something more specific to type-punning (DR 1312). */
5502 tree from = TREE_OPERAND (t, 0);
5503 if (POINTER_TYPE_P (TREE_TYPE (t))
5504 && TREE_CODE (from) == INTEGER_CST
5505 && !integer_zerop (from))
5507 if (flags & tf_error)
5508 error_at (loc, "reinterpret_cast from integer to pointer");
5509 return false;
5511 return (RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR));
5514 case ADDRESSOF_EXPR:
5515 /* This is like ADDR_EXPR, except it won't form pointer-to-member. */
5516 t = TREE_OPERAND (t, 0);
5517 goto handle_addr_expr;
5519 case ADDR_EXPR:
5520 /* -- a unary operator & that is applied to an lvalue that
5521 designates an object with thread or automatic storage
5522 duration; */
5523 t = TREE_OPERAND (t, 0);
5525 if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t))
5526 /* A pointer-to-member constant. */
5527 return true;
5529 handle_addr_expr:
5530 #if 0
5531 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
5532 any checking here, as we might dereference the pointer later. If
5533 we remove this code, also remove check_automatic_or_tls. */
5534 i = check_automatic_or_tls (t);
5535 if (i == ck_ok)
5536 return true;
5537 if (i == ck_bad)
5539 if (flags & tf_error)
5540 error ("address-of an object %qE with thread local or "
5541 "automatic storage is not a constant expression", t);
5542 return false;
5544 #endif
5545 return RECUR (t, any);
5547 case REALPART_EXPR:
5548 case IMAGPART_EXPR:
5549 case COMPONENT_REF:
5550 case BIT_FIELD_REF:
5551 case ARROW_EXPR:
5552 case OFFSET_REF:
5553 /* -- a class member access unless its postfix-expression is
5554 of literal type or of pointer to literal type. */
5555 /* This test would be redundant, as it follows from the
5556 postfix-expression being a potential constant expression. */
5557 if (type_unknown_p (t))
5558 return true;
5559 return RECUR (TREE_OPERAND (t, 0), want_rval);
5561 case EXPR_PACK_EXPANSION:
5562 return RECUR (PACK_EXPANSION_PATTERN (t), want_rval);
5564 case INDIRECT_REF:
5566 tree x = TREE_OPERAND (t, 0);
5567 STRIP_NOPS (x);
5568 if (is_this_parameter (x) && !is_capture_proxy (x))
5570 if (!var_in_maybe_constexpr_fn (x))
5572 if (flags & tf_error)
5573 error_at (loc, "use of %<this%> in a constant expression");
5574 return false;
5576 return true;
5578 return RECUR (x, rval);
5581 case STATEMENT_LIST:
5583 tree_stmt_iterator i;
5584 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
5586 if (!RECUR (tsi_stmt (i), any))
5587 return false;
5589 return true;
5591 break;
5593 case MODIFY_EXPR:
5594 if (cxx_dialect < cxx14)
5595 goto fail;
5596 if (!RECUR (TREE_OPERAND (t, 0), any))
5597 return false;
5598 if (!RECUR (TREE_OPERAND (t, 1), rval))
5599 return false;
5600 return true;
5602 case MODOP_EXPR:
5603 if (cxx_dialect < cxx14)
5604 goto fail;
5605 if (!RECUR (TREE_OPERAND (t, 0), rval))
5606 return false;
5607 if (!RECUR (TREE_OPERAND (t, 2), rval))
5608 return false;
5609 return true;
5611 case DO_STMT:
5612 if (!RECUR (DO_COND (t), rval))
5613 return false;
5614 if (!RECUR (DO_BODY (t), any))
5615 return false;
5616 return true;
5618 case FOR_STMT:
5619 if (!RECUR (FOR_INIT_STMT (t), any))
5620 return false;
5621 if (!RECUR (FOR_COND (t), rval))
5622 return false;
5623 if (!RECUR (FOR_EXPR (t), any))
5624 return false;
5625 if (!RECUR (FOR_BODY (t), any))
5626 return false;
5627 return true;
5629 case RANGE_FOR_STMT:
5630 if (!RECUR (RANGE_FOR_EXPR (t), any))
5631 return false;
5632 if (!RECUR (RANGE_FOR_BODY (t), any))
5633 return false;
5634 return true;
5636 case WHILE_STMT:
5637 if (!RECUR (WHILE_COND (t), rval))
5638 return false;
5639 if (!RECUR (WHILE_BODY (t), any))
5640 return false;
5641 return true;
5643 case SWITCH_STMT:
5644 if (!RECUR (SWITCH_STMT_COND (t), rval))
5645 return false;
5646 /* FIXME we don't check SWITCH_STMT_BODY currently, because even
5647 unreachable labels would be checked. */
5648 return true;
5650 case STMT_EXPR:
5651 return RECUR (STMT_EXPR_STMT (t), rval);
5653 case LAMBDA_EXPR:
5654 if (cxx_dialect >= cxx17)
5655 /* In C++17 lambdas can be constexpr, don't give up yet. */
5656 return true;
5657 else if (flags & tf_error)
5658 error_at (loc, "lambda-expression is not a constant expression "
5659 "before C++17");
5660 return false;
5662 case DYNAMIC_CAST_EXPR:
5663 case PSEUDO_DTOR_EXPR:
5664 case NEW_EXPR:
5665 case VEC_NEW_EXPR:
5666 case DELETE_EXPR:
5667 case VEC_DELETE_EXPR:
5668 case THROW_EXPR:
5669 case OMP_PARALLEL:
5670 case OMP_TASK:
5671 case OMP_FOR:
5672 case OMP_SIMD:
5673 case OMP_DISTRIBUTE:
5674 case OMP_TASKLOOP:
5675 case OMP_TEAMS:
5676 case OMP_TARGET_DATA:
5677 case OMP_TARGET:
5678 case OMP_SECTIONS:
5679 case OMP_ORDERED:
5680 case OMP_CRITICAL:
5681 case OMP_SINGLE:
5682 case OMP_SECTION:
5683 case OMP_MASTER:
5684 case OMP_TASKGROUP:
5685 case OMP_TARGET_UPDATE:
5686 case OMP_TARGET_ENTER_DATA:
5687 case OMP_TARGET_EXIT_DATA:
5688 case OMP_ATOMIC:
5689 case OMP_ATOMIC_READ:
5690 case OMP_ATOMIC_CAPTURE_OLD:
5691 case OMP_ATOMIC_CAPTURE_NEW:
5692 case OACC_PARALLEL:
5693 case OACC_KERNELS:
5694 case OACC_DATA:
5695 case OACC_HOST_DATA:
5696 case OACC_LOOP:
5697 case OACC_CACHE:
5698 case OACC_DECLARE:
5699 case OACC_ENTER_DATA:
5700 case OACC_EXIT_DATA:
5701 case OACC_UPDATE:
5702 /* GCC internal stuff. */
5703 case VA_ARG_EXPR:
5704 case OBJ_TYPE_REF:
5705 case TRANSACTION_EXPR:
5706 case ASM_EXPR:
5707 case AT_ENCODE_EXPR:
5708 fail:
5709 if (flags & tf_error)
5710 error_at (loc, "expression %qE is not a constant expression", t);
5711 return false;
5713 case TYPEID_EXPR:
5714 /* -- a typeid expression whose operand is of polymorphic
5715 class type; */
5717 tree e = TREE_OPERAND (t, 0);
5718 if (!TYPE_P (e) && !type_dependent_expression_p (e)
5719 && TYPE_POLYMORPHIC_P (TREE_TYPE (e)))
5721 if (flags & tf_error)
5722 error_at (loc, "typeid-expression is not a constant expression "
5723 "because %qE is of polymorphic type", e);
5724 return false;
5726 return true;
5729 case POINTER_DIFF_EXPR:
5730 case MINUS_EXPR:
5731 want_rval = true;
5732 goto binary;
5734 case LT_EXPR:
5735 case LE_EXPR:
5736 case GT_EXPR:
5737 case GE_EXPR:
5738 case EQ_EXPR:
5739 case NE_EXPR:
5740 want_rval = true;
5741 goto binary;
5743 case PREINCREMENT_EXPR:
5744 case POSTINCREMENT_EXPR:
5745 case PREDECREMENT_EXPR:
5746 case POSTDECREMENT_EXPR:
5747 if (cxx_dialect < cxx14)
5748 goto fail;
5749 goto unary;
5751 case BIT_NOT_EXPR:
5752 /* A destructor. */
5753 if (TYPE_P (TREE_OPERAND (t, 0)))
5754 return true;
5755 /* fall through. */
5757 case CONJ_EXPR:
5758 case SAVE_EXPR:
5759 case FIX_TRUNC_EXPR:
5760 case FLOAT_EXPR:
5761 case NEGATE_EXPR:
5762 case ABS_EXPR:
5763 case TRUTH_NOT_EXPR:
5764 case FIXED_CONVERT_EXPR:
5765 case UNARY_PLUS_EXPR:
5766 case UNARY_LEFT_FOLD_EXPR:
5767 case UNARY_RIGHT_FOLD_EXPR:
5768 unary:
5769 return RECUR (TREE_OPERAND (t, 0), rval);
5771 case CAST_EXPR:
5772 case CONST_CAST_EXPR:
5773 case STATIC_CAST_EXPR:
5774 case REINTERPRET_CAST_EXPR:
5775 case IMPLICIT_CONV_EXPR:
5776 if (cxx_dialect < cxx11
5777 && !dependent_type_p (TREE_TYPE (t))
5778 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t)))
5779 /* In C++98, a conversion to non-integral type can't be part of a
5780 constant expression. */
5782 if (flags & tf_error)
5783 error_at (loc,
5784 "cast to non-integral type %qT in a constant expression",
5785 TREE_TYPE (t));
5786 return false;
5788 /* This might be a conversion from a class to a (potentially) literal
5789 type. Let's consider it potentially constant since the conversion
5790 might be a constexpr user-defined conversion. */
5791 else if (cxx_dialect >= cxx11
5792 && (dependent_type_p (TREE_TYPE (t))
5793 || !COMPLETE_TYPE_P (TREE_TYPE (t))
5794 || literal_type_p (TREE_TYPE (t)))
5795 && TREE_OPERAND (t, 0))
5797 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
5798 /* If this is a dependent type, it could end up being a class
5799 with conversions. */
5800 if (type == NULL_TREE || WILDCARD_TYPE_P (type))
5801 return true;
5802 /* Or a non-dependent class which has conversions. */
5803 else if (CLASS_TYPE_P (type)
5804 && (TYPE_HAS_CONVERSION (type) || dependent_scope_p (type)))
5805 return true;
5808 return (RECUR (TREE_OPERAND (t, 0),
5809 !TYPE_REF_P (TREE_TYPE (t))));
5811 case BIND_EXPR:
5812 return RECUR (BIND_EXPR_BODY (t), want_rval);
5814 case CLEANUP_POINT_EXPR:
5815 case MUST_NOT_THROW_EXPR:
5816 case TRY_CATCH_EXPR:
5817 case TRY_BLOCK:
5818 case EH_SPEC_BLOCK:
5819 case EXPR_STMT:
5820 case PAREN_EXPR:
5821 case NON_DEPENDENT_EXPR:
5822 /* For convenience. */
5823 case RETURN_EXPR:
5824 case LOOP_EXPR:
5825 case EXIT_EXPR:
5826 return RECUR (TREE_OPERAND (t, 0), want_rval);
5828 case DECL_EXPR:
5829 tmp = DECL_EXPR_DECL (t);
5830 if (VAR_P (tmp) && !DECL_ARTIFICIAL (tmp))
5832 if (TREE_STATIC (tmp))
5834 if (flags & tf_error)
5835 error_at (DECL_SOURCE_LOCATION (tmp), "%qD declared "
5836 "%<static%> in %<constexpr%> context", tmp);
5837 return false;
5839 else if (CP_DECL_THREAD_LOCAL_P (tmp))
5841 if (flags & tf_error)
5842 error_at (DECL_SOURCE_LOCATION (tmp), "%qD declared "
5843 "%<thread_local%> in %<constexpr%> context", tmp);
5844 return false;
5846 else if (!check_for_uninitialized_const_var
5847 (tmp, /*constexpr_context_p=*/true, flags))
5848 return false;
5850 return RECUR (tmp, want_rval);
5852 case TRY_FINALLY_EXPR:
5853 return (RECUR (TREE_OPERAND (t, 0), want_rval)
5854 && RECUR (TREE_OPERAND (t, 1), any));
5856 case SCOPE_REF:
5857 return RECUR (TREE_OPERAND (t, 1), want_rval);
5859 case TARGET_EXPR:
5860 if (!TARGET_EXPR_DIRECT_INIT_P (t)
5861 && !literal_type_p (TREE_TYPE (t)))
5863 if (flags & tf_error)
5865 error_at (loc, "temporary of non-literal type %qT in a "
5866 "constant expression", TREE_TYPE (t));
5867 explain_non_literal_class (TREE_TYPE (t));
5869 return false;
5871 /* FALLTHRU */
5872 case INIT_EXPR:
5873 return RECUR (TREE_OPERAND (t, 1), rval);
5875 case CONSTRUCTOR:
5877 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
5878 constructor_elt *ce;
5879 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
5880 if (!RECUR (ce->value, want_rval))
5881 return false;
5882 return true;
5885 case TREE_LIST:
5887 gcc_assert (TREE_PURPOSE (t) == NULL_TREE
5888 || DECL_P (TREE_PURPOSE (t)));
5889 if (!RECUR (TREE_VALUE (t), want_rval))
5890 return false;
5891 if (TREE_CHAIN (t) == NULL_TREE)
5892 return true;
5893 return RECUR (TREE_CHAIN (t), want_rval);
5896 case TRUNC_DIV_EXPR:
5897 case CEIL_DIV_EXPR:
5898 case FLOOR_DIV_EXPR:
5899 case ROUND_DIV_EXPR:
5900 case TRUNC_MOD_EXPR:
5901 case CEIL_MOD_EXPR:
5902 case ROUND_MOD_EXPR:
5904 tree denom = TREE_OPERAND (t, 1);
5905 if (!RECUR (denom, rval))
5906 return false;
5907 /* We can't call cxx_eval_outermost_constant_expr on an expression
5908 that hasn't been through instantiate_non_dependent_expr yet. */
5909 if (!processing_template_decl)
5910 denom = cxx_eval_outermost_constant_expr (denom, true);
5911 if (integer_zerop (denom))
5913 if (flags & tf_error)
5914 error ("division by zero is not a constant expression");
5915 return false;
5917 else
5919 want_rval = true;
5920 return RECUR (TREE_OPERAND (t, 0), want_rval);
5924 case COMPOUND_EXPR:
5926 /* check_return_expr sometimes wraps a TARGET_EXPR in a
5927 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
5928 introduced by build_call_a. */
5929 tree op0 = TREE_OPERAND (t, 0);
5930 tree op1 = TREE_OPERAND (t, 1);
5931 STRIP_NOPS (op1);
5932 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
5933 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
5934 return RECUR (op0, want_rval);
5935 else
5936 goto binary;
5939 /* If the first operand is the non-short-circuit constant, look at
5940 the second operand; otherwise we only care about the first one for
5941 potentiality. */
5942 case TRUTH_AND_EXPR:
5943 case TRUTH_ANDIF_EXPR:
5944 tmp = boolean_true_node;
5945 goto truth;
5946 case TRUTH_OR_EXPR:
5947 case TRUTH_ORIF_EXPR:
5948 tmp = boolean_false_node;
5949 truth:
5951 tree op = TREE_OPERAND (t, 0);
5952 if (!RECUR (op, rval))
5953 return false;
5954 if (!processing_template_decl)
5955 op = cxx_eval_outermost_constant_expr (op, true);
5956 if (tree_int_cst_equal (op, tmp))
5957 return RECUR (TREE_OPERAND (t, 1), rval);
5958 else
5959 return true;
5962 case PLUS_EXPR:
5963 case MULT_EXPR:
5964 case POINTER_PLUS_EXPR:
5965 case RDIV_EXPR:
5966 case EXACT_DIV_EXPR:
5967 case MIN_EXPR:
5968 case MAX_EXPR:
5969 case LSHIFT_EXPR:
5970 case RSHIFT_EXPR:
5971 case LROTATE_EXPR:
5972 case RROTATE_EXPR:
5973 case BIT_IOR_EXPR:
5974 case BIT_XOR_EXPR:
5975 case BIT_AND_EXPR:
5976 case TRUTH_XOR_EXPR:
5977 case UNORDERED_EXPR:
5978 case ORDERED_EXPR:
5979 case UNLT_EXPR:
5980 case UNLE_EXPR:
5981 case UNGT_EXPR:
5982 case UNGE_EXPR:
5983 case UNEQ_EXPR:
5984 case LTGT_EXPR:
5985 case RANGE_EXPR:
5986 case COMPLEX_EXPR:
5987 want_rval = true;
5988 /* Fall through. */
5989 case ARRAY_REF:
5990 case ARRAY_RANGE_REF:
5991 case MEMBER_REF:
5992 case DOTSTAR_EXPR:
5993 case MEM_REF:
5994 case BINARY_LEFT_FOLD_EXPR:
5995 case BINARY_RIGHT_FOLD_EXPR:
5996 binary:
5997 for (i = 0; i < 2; ++i)
5998 if (!RECUR (TREE_OPERAND (t, i), want_rval))
5999 return false;
6000 return true;
6002 case FMA_EXPR:
6003 case VEC_PERM_EXPR:
6004 for (i = 0; i < 3; ++i)
6005 if (!RECUR (TREE_OPERAND (t, i), true))
6006 return false;
6007 return true;
6009 case COND_EXPR:
6010 if (COND_EXPR_IS_VEC_DELETE (t))
6012 if (flags & tf_error)
6013 error_at (loc, "%<delete[]%> is not a constant expression");
6014 return false;
6016 /* Fall through. */
6017 case IF_STMT:
6018 case VEC_COND_EXPR:
6019 /* If the condition is a known constant, we know which of the legs we
6020 care about; otherwise we only require that the condition and
6021 either of the legs be potentially constant. */
6022 tmp = TREE_OPERAND (t, 0);
6023 if (!RECUR (tmp, rval))
6024 return false;
6025 if (!processing_template_decl)
6026 tmp = cxx_eval_outermost_constant_expr (tmp, true);
6027 if (integer_zerop (tmp))
6028 return RECUR (TREE_OPERAND (t, 2), want_rval);
6029 else if (TREE_CODE (tmp) == INTEGER_CST)
6030 return RECUR (TREE_OPERAND (t, 1), want_rval);
6031 for (i = 1; i < 3; ++i)
6032 if (potential_constant_expression_1 (TREE_OPERAND (t, i),
6033 want_rval, strict, now, tf_none))
6034 return true;
6035 if (flags & tf_error)
6036 error_at (loc, "expression %qE is not a constant expression", t);
6037 return false;
6039 case VEC_INIT_EXPR:
6040 if (VEC_INIT_EXPR_IS_CONSTEXPR (t))
6041 return true;
6042 if (flags & tf_error)
6044 error_at (loc, "non-constant array initialization");
6045 diagnose_non_constexpr_vec_init (t);
6047 return false;
6049 case TYPE_DECL:
6050 case TAG_DEFN:
6051 /* We can see these in statement-expressions. */
6052 return true;
6054 case CLEANUP_STMT:
6055 case EMPTY_CLASS_EXPR:
6056 case PREDICT_EXPR:
6057 return false;
6059 case GOTO_EXPR:
6061 tree *target = &TREE_OPERAND (t, 0);
6062 /* Gotos representing break and continue are OK. */
6063 if (breaks (target) || continues (target))
6064 return true;
6065 if (flags & tf_error)
6066 error_at (loc, "%<goto%> is not a constant expression");
6067 return false;
6070 case ANNOTATE_EXPR:
6071 return RECUR (TREE_OPERAND (t, 0), rval);
6073 default:
6074 if (objc_is_property_ref (t))
6075 return false;
6077 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t)));
6078 gcc_unreachable ();
6079 return false;
6081 #undef RECUR
6084 /* The main entry point to the above. */
6086 bool
6087 potential_constant_expression (tree t)
6089 return potential_constant_expression_1 (t, false, true, false, tf_none);
6092 /* As above, but require a constant rvalue. */
6094 bool
6095 potential_rvalue_constant_expression (tree t)
6097 return potential_constant_expression_1 (t, true, true, false, tf_none);
6100 /* Like above, but complain about non-constant expressions. */
6102 bool
6103 require_potential_constant_expression (tree t)
6105 return potential_constant_expression_1 (t, false, true, false,
6106 tf_warning_or_error);
6109 /* Cross product of the above. */
6111 bool
6112 require_potential_rvalue_constant_expression (tree t)
6114 return potential_constant_expression_1 (t, true, true, false,
6115 tf_warning_or_error);
6118 /* Like above, but don't consider PARM_DECL a potential_constant_expression. */
6120 bool
6121 require_rvalue_constant_expression (tree t)
6123 return potential_constant_expression_1 (t, true, true, true,
6124 tf_warning_or_error);
6127 /* Like potential_constant_expression, but don't consider possible constexpr
6128 substitution of the current function. That is, PARM_DECL qualifies under
6129 potential_constant_expression, but not here.
6131 This is basically what you can check when any actual constant values might
6132 be value-dependent. */
6134 bool
6135 is_constant_expression (tree t)
6137 return potential_constant_expression_1 (t, false, true, true, tf_none);
6140 /* Like above, but complain about non-constant expressions. */
6142 bool
6143 require_constant_expression (tree t)
6145 return potential_constant_expression_1 (t, false, true, true,
6146 tf_warning_or_error);
6149 /* Like is_constant_expression, but allow const variables that are not allowed
6150 under constexpr rules. */
6152 bool
6153 is_static_init_expression (tree t)
6155 return potential_constant_expression_1 (t, false, false, true, tf_none);
6158 /* Returns true if T is a potential constant expression that is not
6159 instantiation-dependent, and therefore a candidate for constant folding even
6160 in a template. */
6162 bool
6163 is_nondependent_constant_expression (tree t)
6165 return (!type_unknown_p (t)
6166 && !BRACE_ENCLOSED_INITIALIZER_P (t)
6167 && is_constant_expression (t)
6168 && !instantiation_dependent_expression_p (t));
6171 /* Returns true if T is a potential static initializer expression that is not
6172 instantiation-dependent. */
6174 bool
6175 is_nondependent_static_init_expression (tree t)
6177 return (!type_unknown_p (t)
6178 && !BRACE_ENCLOSED_INITIALIZER_P (t)
6179 && is_static_init_expression (t)
6180 && !instantiation_dependent_expression_p (t));
6183 /* Finalize constexpr processing after parsing. */
6185 void
6186 fini_constexpr (void)
6188 /* The contexpr call and fundef copies tables are no longer needed. */
6189 constexpr_call_table = NULL;
6190 fundef_copies_table = NULL;
6193 #include "gt-cp-constexpr.h"