* pt.c (tsubst) [TEMPLATE_TYPE_PARM]: Use TEMPLATE_PARM_DESCENDANTS.
[official-gcc.git] / gcc / cp / constexpr.c
blob44f3093ab471e78c0d7838481edfd5b506db1c6d
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 (cp_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 = cp_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_CLEARING from CTOR and its sub-aggregates. */
1450 static void
1451 clear_no_implicit_zero (tree ctor)
1453 if (CONSTRUCTOR_NO_CLEARING (ctor))
1455 CONSTRUCTOR_NO_CLEARING (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 = cp_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_CLEARING (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_CLEARING (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_CLEARING (t))
1816 /* All the fields are initialized. */
1817 CONSTRUCTOR_NO_CLEARING (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)
2055 tree lmem = PTRMEM_CST_MEMBER (lhs);
2056 tree rmem = PTRMEM_CST_MEMBER (rhs);
2057 bool eq;
2058 if (TREE_CODE (lmem) == TREE_CODE (rmem)
2059 && TREE_CODE (lmem) == FIELD_DECL
2060 && TREE_CODE (DECL_CONTEXT (lmem)) == UNION_TYPE
2061 && same_type_p (DECL_CONTEXT (lmem),
2062 DECL_CONTEXT (rmem)))
2063 /* If both refer to (possibly different) members of the same union
2064 (12.3), they compare equal. */
2065 eq = true;
2066 else
2067 eq = cp_tree_equal (lhs, rhs);
2068 r = constant_boolean_node (eq == is_code_eq, type);
2070 else if ((TREE_CODE (lhs) == PTRMEM_CST
2071 || TREE_CODE (rhs) == PTRMEM_CST)
2072 && (null_member_pointer_value_p (lhs)
2073 || null_member_pointer_value_p (rhs)))
2074 r = constant_boolean_node (!is_code_eq, type);
2075 else if (TREE_CODE (lhs) == PTRMEM_CST)
2076 lhs = cplus_expand_constant (lhs);
2077 else if (TREE_CODE (rhs) == PTRMEM_CST)
2078 rhs = cplus_expand_constant (rhs);
2080 if (code == POINTER_PLUS_EXPR && !*non_constant_p
2081 && integer_zerop (lhs) && !integer_zerop (rhs))
2083 if (!ctx->quiet)
2084 error ("arithmetic involving a null pointer in %qE", lhs);
2085 return t;
2087 else if (code == POINTER_PLUS_EXPR)
2088 r = cxx_fold_pointer_plus_expression (ctx, t, lhs, rhs, non_constant_p,
2089 overflow_p);
2091 if (r == NULL_TREE)
2092 r = fold_binary_loc (loc, code, type, lhs, rhs);
2094 if (r == NULL_TREE)
2096 if (lhs == orig_lhs && rhs == orig_rhs)
2097 r = t;
2098 else
2099 r = build2_loc (loc, code, type, lhs, rhs);
2101 else if (cxx_eval_check_shift_p (loc, ctx, code, type, lhs, rhs))
2102 *non_constant_p = true;
2103 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
2104 a local array in a constexpr function. */
2105 bool ptr = INDIRECT_TYPE_P (TREE_TYPE (lhs));
2106 if (!ptr)
2107 VERIFY_CONSTANT (r);
2108 return r;
2111 /* Subroutine of cxx_eval_constant_expression.
2112 Attempt to evaluate condition expressions. Dead branches are not
2113 looked into. */
2115 static tree
2116 cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t,
2117 bool lval,
2118 bool *non_constant_p, bool *overflow_p,
2119 tree *jump_target)
2121 tree val = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
2122 /*lval*/false,
2123 non_constant_p, overflow_p);
2124 VERIFY_CONSTANT (val);
2125 /* Don't VERIFY_CONSTANT the other operands. */
2126 if (integer_zerop (val))
2127 return cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2),
2128 lval,
2129 non_constant_p, overflow_p,
2130 jump_target);
2131 return cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
2132 lval,
2133 non_constant_p, overflow_p,
2134 jump_target);
2137 /* Subroutine of cxx_eval_constant_expression.
2138 Attempt to evaluate vector condition expressions. Unlike
2139 cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal
2140 ternary arithmetics operation, where all 3 arguments have to be
2141 evaluated as constants and then folding computes the result from
2142 them. */
2144 static tree
2145 cxx_eval_vector_conditional_expression (const constexpr_ctx *ctx, tree t,
2146 bool *non_constant_p, bool *overflow_p)
2148 tree arg1 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
2149 /*lval*/false,
2150 non_constant_p, overflow_p);
2151 VERIFY_CONSTANT (arg1);
2152 tree arg2 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
2153 /*lval*/false,
2154 non_constant_p, overflow_p);
2155 VERIFY_CONSTANT (arg2);
2156 tree arg3 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2),
2157 /*lval*/false,
2158 non_constant_p, overflow_p);
2159 VERIFY_CONSTANT (arg3);
2160 location_t loc = EXPR_LOCATION (t);
2161 tree type = TREE_TYPE (t);
2162 tree r = fold_ternary_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
2163 if (r == NULL_TREE)
2165 if (arg1 == TREE_OPERAND (t, 0)
2166 && arg2 == TREE_OPERAND (t, 1)
2167 && arg3 == TREE_OPERAND (t, 2))
2168 r = t;
2169 else
2170 r = build3_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
2172 VERIFY_CONSTANT (r);
2173 return r;
2176 /* Returns less than, equal to, or greater than zero if KEY is found to be
2177 less than, to match, or to be greater than the constructor_elt's INDEX. */
2179 static int
2180 array_index_cmp (tree key, tree index)
2182 gcc_assert (TREE_CODE (key) == INTEGER_CST);
2184 switch (TREE_CODE (index))
2186 case INTEGER_CST:
2187 return tree_int_cst_compare (key, index);
2188 case RANGE_EXPR:
2190 tree lo = TREE_OPERAND (index, 0);
2191 tree hi = TREE_OPERAND (index, 1);
2192 if (tree_int_cst_lt (key, lo))
2193 return -1;
2194 else if (tree_int_cst_lt (hi, key))
2195 return 1;
2196 else
2197 return 0;
2199 default:
2200 gcc_unreachable ();
2204 /* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1
2205 if none. If INSERT is true, insert a matching element rather than fail. */
2207 static HOST_WIDE_INT
2208 find_array_ctor_elt (tree ary, tree dindex, bool insert = false)
2210 if (tree_int_cst_sgn (dindex) < 0)
2211 return -1;
2213 unsigned HOST_WIDE_INT i = tree_to_uhwi (dindex);
2214 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ary);
2215 unsigned HOST_WIDE_INT len = vec_safe_length (elts);
2217 unsigned HOST_WIDE_INT end = len;
2218 unsigned HOST_WIDE_INT begin = 0;
2220 /* If the last element of the CONSTRUCTOR has its own index, we can assume
2221 that the same is true of the other elements and index directly. */
2222 if (end > 0)
2224 tree cindex = (*elts)[end - 1].index;
2225 if (TREE_CODE (cindex) == INTEGER_CST
2226 && compare_tree_int (cindex, end - 1) == 0)
2228 if (i < end)
2229 return i;
2230 else
2231 begin = end;
2235 /* Otherwise, find a matching index by means of a binary search. */
2236 while (begin != end)
2238 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
2239 constructor_elt &elt = (*elts)[middle];
2240 tree idx = elt.index;
2242 int cmp = array_index_cmp (dindex, idx);
2243 if (cmp < 0)
2244 end = middle;
2245 else if (cmp > 0)
2246 begin = middle + 1;
2247 else
2249 if (insert && TREE_CODE (idx) == RANGE_EXPR)
2251 /* We need to split the range. */
2252 constructor_elt e;
2253 tree lo = TREE_OPERAND (idx, 0);
2254 tree hi = TREE_OPERAND (idx, 1);
2255 tree value = elt.value;
2256 dindex = fold_convert (sizetype, dindex);
2257 if (tree_int_cst_lt (lo, dindex))
2259 /* There are still some lower elts; shorten the range. */
2260 tree new_hi = int_const_binop (MINUS_EXPR, dindex,
2261 size_one_node);
2262 if (tree_int_cst_equal (lo, new_hi))
2263 /* Only one element left, no longer a range. */
2264 elt.index = lo;
2265 else
2266 TREE_OPERAND (idx, 1) = new_hi;
2267 /* Append the element we want to insert. */
2268 ++middle;
2269 e.index = dindex;
2270 e.value = unshare_constructor (value);
2271 vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e);
2273 else
2274 /* No lower elts, the range elt is now ours. */
2275 elt.index = dindex;
2277 if (tree_int_cst_lt (dindex, hi))
2279 /* There are still some higher elts; append a range. */
2280 tree new_lo = int_const_binop (PLUS_EXPR, dindex,
2281 size_one_node);
2282 if (tree_int_cst_equal (new_lo, hi))
2283 e.index = hi;
2284 else
2285 e.index = build2 (RANGE_EXPR, sizetype, new_lo, hi);
2286 e.value = unshare_constructor (value);
2287 vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e);
2290 return middle;
2294 if (insert)
2296 constructor_elt e = { dindex, NULL_TREE };
2297 vec_safe_insert (CONSTRUCTOR_ELTS (ary), end, e);
2298 return end;
2301 return -1;
2304 /* Under the control of CTX, issue a detailed diagnostic for
2305 an out-of-bounds subscript INDEX into the expression ARRAY. */
2307 static void
2308 diag_array_subscript (const constexpr_ctx *ctx, tree array, tree index)
2310 if (!ctx->quiet)
2312 tree arraytype = TREE_TYPE (array);
2314 /* Convert the unsigned array subscript to a signed integer to avoid
2315 printing huge numbers for small negative values. */
2316 tree sidx = fold_convert (ssizetype, index);
2317 if (DECL_P (array))
2319 if (TYPE_DOMAIN (arraytype))
2320 error ("array subscript value %qE is outside the bounds "
2321 "of array %qD of type %qT", sidx, array, arraytype);
2322 else
2323 error ("non-zero array subscript %qE is used with array %qD of "
2324 "type %qT with unknown bounds", sidx, array, arraytype);
2325 inform (DECL_SOURCE_LOCATION (array), "declared here");
2327 else if (TYPE_DOMAIN (arraytype))
2328 error ("array subscript value %qE is outside the bounds "
2329 "of array type %qT", sidx, arraytype);
2330 else
2331 error ("non-zero array subscript %qE is used with array of type %qT "
2332 "with unknown bounds", sidx, arraytype);
2336 /* Return the number of elements for TYPE (which is an ARRAY_TYPE or
2337 a VECTOR_TYPE). */
2339 static tree
2340 get_array_or_vector_nelts (const constexpr_ctx *ctx, tree type,
2341 bool *non_constant_p, bool *overflow_p)
2343 tree nelts;
2344 if (TREE_CODE (type) == ARRAY_TYPE)
2346 if (TYPE_DOMAIN (type))
2347 nelts = array_type_nelts_top (type);
2348 else
2349 nelts = size_zero_node;
2351 else if (VECTOR_TYPE_P (type))
2352 nelts = size_int (TYPE_VECTOR_SUBPARTS (type));
2353 else
2354 gcc_unreachable ();
2356 /* For VLAs, the number of elements won't be an integer constant. */
2357 nelts = cxx_eval_constant_expression (ctx, nelts, false,
2358 non_constant_p, overflow_p);
2359 return nelts;
2362 /* Extract element INDEX consisting of CHARS_PER_ELT chars from
2363 STRING_CST STRING. */
2365 static tree
2366 extract_string_elt (tree string, unsigned chars_per_elt, unsigned index)
2368 tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (string)));
2369 tree r;
2371 if (chars_per_elt == 1)
2372 r = build_int_cst (type, TREE_STRING_POINTER (string)[index]);
2373 else
2375 const unsigned char *ptr
2376 = ((const unsigned char *)TREE_STRING_POINTER (string)
2377 + index * chars_per_elt);
2378 r = native_interpret_expr (type, ptr, chars_per_elt);
2380 return r;
2383 /* Subroutine of cxx_eval_constant_expression.
2384 Attempt to reduce a reference to an array slot. */
2386 static tree
2387 cxx_eval_array_reference (const constexpr_ctx *ctx, tree t,
2388 bool lval,
2389 bool *non_constant_p, bool *overflow_p)
2391 tree oldary = TREE_OPERAND (t, 0);
2392 tree ary = cxx_eval_constant_expression (ctx, oldary,
2393 lval,
2394 non_constant_p, overflow_p);
2395 tree index, oldidx;
2396 HOST_WIDE_INT i = 0;
2397 tree elem_type = NULL_TREE;
2398 unsigned len = 0, elem_nchars = 1;
2399 if (*non_constant_p)
2400 return t;
2401 oldidx = TREE_OPERAND (t, 1);
2402 index = cxx_eval_constant_expression (ctx, oldidx,
2403 false,
2404 non_constant_p, overflow_p);
2405 VERIFY_CONSTANT (index);
2406 if (!lval)
2408 elem_type = TREE_TYPE (TREE_TYPE (ary));
2409 if (TREE_CODE (ary) == VIEW_CONVERT_EXPR
2410 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary, 0)))
2411 && TREE_TYPE (t) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary, 0))))
2412 ary = TREE_OPERAND (ary, 0);
2413 if (TREE_CODE (ary) == CONSTRUCTOR)
2414 len = CONSTRUCTOR_NELTS (ary);
2415 else if (TREE_CODE (ary) == STRING_CST)
2417 elem_nchars = (TYPE_PRECISION (elem_type)
2418 / TYPE_PRECISION (char_type_node));
2419 len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars;
2421 else if (TREE_CODE (ary) == VECTOR_CST)
2422 /* We don't create variable-length VECTOR_CSTs. */
2423 len = VECTOR_CST_NELTS (ary).to_constant ();
2424 else
2426 /* We can't do anything with other tree codes, so use
2427 VERIFY_CONSTANT to complain and fail. */
2428 VERIFY_CONSTANT (ary);
2429 gcc_unreachable ();
2432 if (!tree_fits_shwi_p (index)
2433 || (i = tree_to_shwi (index)) < 0)
2435 diag_array_subscript (ctx, ary, index);
2436 *non_constant_p = true;
2437 return t;
2441 tree nelts = get_array_or_vector_nelts (ctx, TREE_TYPE (ary), non_constant_p,
2442 overflow_p);
2443 VERIFY_CONSTANT (nelts);
2444 if ((lval
2445 ? !tree_int_cst_le (index, nelts)
2446 : !tree_int_cst_lt (index, nelts))
2447 || tree_int_cst_sgn (index) < 0)
2449 diag_array_subscript (ctx, ary, index);
2450 *non_constant_p = true;
2451 return t;
2454 if (lval && ary == oldary && index == oldidx)
2455 return t;
2456 else if (lval)
2457 return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL);
2459 bool found;
2460 if (TREE_CODE (ary) == CONSTRUCTOR)
2462 HOST_WIDE_INT ix = find_array_ctor_elt (ary, index);
2463 found = (ix >= 0);
2464 if (found)
2465 i = ix;
2467 else
2468 found = (i < len);
2470 if (found)
2472 tree r;
2473 if (TREE_CODE (ary) == CONSTRUCTOR)
2474 r = (*CONSTRUCTOR_ELTS (ary))[i].value;
2475 else if (TREE_CODE (ary) == VECTOR_CST)
2476 r = VECTOR_CST_ELT (ary, i);
2477 else
2478 r = extract_string_elt (ary, elem_nchars, i);
2480 if (r)
2481 /* Don't VERIFY_CONSTANT here. */
2482 return r;
2484 /* Otherwise the element doesn't have a value yet. */
2487 /* Not found. */
2489 if (TREE_CODE (ary) == CONSTRUCTOR
2490 && CONSTRUCTOR_NO_CLEARING (ary))
2492 /* 'ary' is part of the aggregate initializer we're currently
2493 building; if there's no initializer for this element yet,
2494 that's an error. */
2495 if (!ctx->quiet)
2496 error ("accessing uninitialized array element");
2497 *non_constant_p = true;
2498 return t;
2501 /* If it's within the array bounds but doesn't have an explicit
2502 initializer, it's value-initialized. */
2503 tree val = build_value_init (elem_type, tf_warning_or_error);
2504 return cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
2505 overflow_p);
2508 /* Subroutine of cxx_eval_constant_expression.
2509 Attempt to reduce a field access of a value of class type. */
2511 static tree
2512 cxx_eval_component_reference (const constexpr_ctx *ctx, tree t,
2513 bool lval,
2514 bool *non_constant_p, bool *overflow_p)
2516 unsigned HOST_WIDE_INT i;
2517 tree field;
2518 tree value;
2519 tree part = TREE_OPERAND (t, 1);
2520 tree orig_whole = TREE_OPERAND (t, 0);
2521 tree whole = cxx_eval_constant_expression (ctx, orig_whole,
2522 lval,
2523 non_constant_p, overflow_p);
2524 if (INDIRECT_REF_P (whole)
2525 && integer_zerop (TREE_OPERAND (whole, 0))
2526 && !ctx->quiet)
2527 error ("dereferencing a null pointer in %qE", orig_whole);
2529 if (TREE_CODE (whole) == PTRMEM_CST)
2530 whole = cplus_expand_constant (whole);
2531 if (whole == orig_whole)
2532 return t;
2533 if (lval)
2534 return fold_build3 (COMPONENT_REF, TREE_TYPE (t),
2535 whole, part, NULL_TREE);
2536 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
2537 CONSTRUCTOR. */
2538 if (!*non_constant_p && TREE_CODE (whole) != CONSTRUCTOR)
2540 if (!ctx->quiet)
2541 error ("%qE is not a constant expression", orig_whole);
2542 *non_constant_p = true;
2544 if (DECL_MUTABLE_P (part))
2546 if (!ctx->quiet)
2547 error ("mutable %qD is not usable in a constant expression", part);
2548 *non_constant_p = true;
2550 if (*non_constant_p)
2551 return t;
2552 bool pmf = TYPE_PTRMEMFUNC_P (TREE_TYPE (whole));
2553 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
2555 /* Use name match for PMF fields, as a variant will have a
2556 different FIELD_DECL with a different type. */
2557 if (pmf ? DECL_NAME (field) == DECL_NAME (part)
2558 : field == part)
2560 if (value)
2561 return value;
2562 else
2563 /* We're in the middle of initializing it. */
2564 break;
2567 if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE
2568 && CONSTRUCTOR_NELTS (whole) > 0)
2570 /* DR 1188 says we don't have to deal with this. */
2571 if (!ctx->quiet)
2572 error ("accessing %qD member instead of initialized %qD member in "
2573 "constant expression", part, CONSTRUCTOR_ELT (whole, 0)->index);
2574 *non_constant_p = true;
2575 return t;
2578 /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty
2579 classes never get represented; throw together a value now. */
2580 if (is_really_empty_class (TREE_TYPE (t)))
2581 return build_constructor (TREE_TYPE (t), NULL);
2583 gcc_assert (DECL_CONTEXT (part) == TYPE_MAIN_VARIANT (TREE_TYPE (whole)));
2585 if (CONSTRUCTOR_NO_CLEARING (whole))
2587 /* 'whole' is part of the aggregate initializer we're currently
2588 building; if there's no initializer for this member yet, that's an
2589 error. */
2590 if (!ctx->quiet)
2591 error ("accessing uninitialized member %qD", part);
2592 *non_constant_p = true;
2593 return t;
2596 /* If there's no explicit init for this field, it's value-initialized. */
2597 value = build_value_init (TREE_TYPE (t), tf_warning_or_error);
2598 return cxx_eval_constant_expression (ctx, value,
2599 lval,
2600 non_constant_p, overflow_p);
2603 /* Subroutine of cxx_eval_constant_expression.
2604 Attempt to reduce a field access of a value of class type that is
2605 expressed as a BIT_FIELD_REF. */
2607 static tree
2608 cxx_eval_bit_field_ref (const constexpr_ctx *ctx, tree t,
2609 bool lval,
2610 bool *non_constant_p, bool *overflow_p)
2612 tree orig_whole = TREE_OPERAND (t, 0);
2613 tree retval, fldval, utype, mask;
2614 bool fld_seen = false;
2615 HOST_WIDE_INT istart, isize;
2616 tree whole = cxx_eval_constant_expression (ctx, orig_whole,
2617 lval,
2618 non_constant_p, overflow_p);
2619 tree start, field, value;
2620 unsigned HOST_WIDE_INT i;
2622 if (whole == orig_whole)
2623 return t;
2624 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
2625 CONSTRUCTOR. */
2626 if (!*non_constant_p
2627 && TREE_CODE (whole) != VECTOR_CST
2628 && TREE_CODE (whole) != CONSTRUCTOR)
2630 if (!ctx->quiet)
2631 error ("%qE is not a constant expression", orig_whole);
2632 *non_constant_p = true;
2634 if (*non_constant_p)
2635 return t;
2637 if (TREE_CODE (whole) == VECTOR_CST)
2638 return fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
2639 TREE_OPERAND (t, 1), TREE_OPERAND (t, 2));
2641 start = TREE_OPERAND (t, 2);
2642 istart = tree_to_shwi (start);
2643 isize = tree_to_shwi (TREE_OPERAND (t, 1));
2644 utype = TREE_TYPE (t);
2645 if (!TYPE_UNSIGNED (utype))
2646 utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1);
2647 retval = build_int_cst (utype, 0);
2648 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
2650 tree bitpos = bit_position (field);
2651 if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1))
2652 return value;
2653 if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE
2654 && TREE_CODE (value) == INTEGER_CST
2655 && tree_fits_shwi_p (bitpos)
2656 && tree_fits_shwi_p (DECL_SIZE (field)))
2658 HOST_WIDE_INT bit = tree_to_shwi (bitpos);
2659 HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field));
2660 HOST_WIDE_INT shift;
2661 if (bit >= istart && bit + sz <= istart + isize)
2663 fldval = fold_convert (utype, value);
2664 mask = build_int_cst_type (utype, -1);
2665 mask = fold_build2 (LSHIFT_EXPR, utype, mask,
2666 size_int (TYPE_PRECISION (utype) - sz));
2667 mask = fold_build2 (RSHIFT_EXPR, utype, mask,
2668 size_int (TYPE_PRECISION (utype) - sz));
2669 fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask);
2670 shift = bit - istart;
2671 if (BYTES_BIG_ENDIAN)
2672 shift = TYPE_PRECISION (utype) - shift - sz;
2673 fldval = fold_build2 (LSHIFT_EXPR, utype, fldval,
2674 size_int (shift));
2675 retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval);
2676 fld_seen = true;
2680 if (fld_seen)
2681 return fold_convert (TREE_TYPE (t), retval);
2682 gcc_unreachable ();
2683 return error_mark_node;
2686 /* Subroutine of cxx_eval_constant_expression.
2687 Evaluate a short-circuited logical expression T in the context
2688 of a given constexpr CALL. BAILOUT_VALUE is the value for
2689 early return. CONTINUE_VALUE is used here purely for
2690 sanity check purposes. */
2692 static tree
2693 cxx_eval_logical_expression (const constexpr_ctx *ctx, tree t,
2694 tree bailout_value, tree continue_value,
2695 bool lval,
2696 bool *non_constant_p, bool *overflow_p)
2698 tree r;
2699 tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
2700 lval,
2701 non_constant_p, overflow_p);
2702 VERIFY_CONSTANT (lhs);
2703 if (tree_int_cst_equal (lhs, bailout_value))
2704 return lhs;
2705 gcc_assert (tree_int_cst_equal (lhs, continue_value));
2706 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
2707 lval, non_constant_p,
2708 overflow_p);
2709 VERIFY_CONSTANT (r);
2710 return r;
2713 /* REF is a COMPONENT_REF designating a particular field. V is a vector of
2714 CONSTRUCTOR elements to initialize (part of) an object containing that
2715 field. Return a pointer to the constructor_elt corresponding to the
2716 initialization of the field. */
2718 static constructor_elt *
2719 base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref)
2721 tree aggr = TREE_OPERAND (ref, 0);
2722 tree field = TREE_OPERAND (ref, 1);
2723 HOST_WIDE_INT i;
2724 constructor_elt *ce;
2726 gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
2728 if (TREE_CODE (aggr) == COMPONENT_REF)
2730 constructor_elt *base_ce
2731 = base_field_constructor_elt (v, aggr);
2732 v = CONSTRUCTOR_ELTS (base_ce->value);
2735 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
2736 if (ce->index == field)
2737 return ce;
2739 gcc_unreachable ();
2740 return NULL;
2743 /* Some of the expressions fed to the constexpr mechanism are calls to
2744 constructors, which have type void. In that case, return the type being
2745 initialized by the constructor. */
2747 static tree
2748 initialized_type (tree t)
2750 if (TYPE_P (t))
2751 return t;
2752 tree type = cv_unqualified (TREE_TYPE (t));
2753 if (TREE_CODE (t) == CALL_EXPR || TREE_CODE (t) == AGGR_INIT_EXPR)
2755 /* A constructor call has void type, so we need to look deeper. */
2756 tree fn = get_function_named_in_call (t);
2757 if (fn && TREE_CODE (fn) == FUNCTION_DECL
2758 && DECL_CXX_CONSTRUCTOR_P (fn))
2759 type = DECL_CONTEXT (fn);
2761 return type;
2764 /* We're about to initialize element INDEX of an array or class from VALUE.
2765 Set up NEW_CTX appropriately by adjusting .object to refer to the
2766 subobject and creating a new CONSTRUCTOR if the element is itself
2767 a class or array. */
2769 static void
2770 init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx,
2771 tree index, tree &value)
2773 new_ctx = *ctx;
2775 if (index && TREE_CODE (index) != INTEGER_CST
2776 && TREE_CODE (index) != FIELD_DECL)
2777 /* This won't have an element in the new CONSTRUCTOR. */
2778 return;
2780 tree type = initialized_type (value);
2781 if (!AGGREGATE_TYPE_P (type) && !VECTOR_TYPE_P (type))
2782 /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
2783 return;
2785 /* The sub-aggregate initializer might contain a placeholder;
2786 update object to refer to the subobject and ctor to refer to
2787 the (newly created) sub-initializer. */
2788 if (ctx->object)
2789 new_ctx.object = build_ctor_subob_ref (index, type, ctx->object);
2790 tree elt = build_constructor (type, NULL);
2791 CONSTRUCTOR_NO_CLEARING (elt) = true;
2792 new_ctx.ctor = elt;
2794 if (TREE_CODE (value) == TARGET_EXPR)
2795 /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
2796 value = TARGET_EXPR_INITIAL (value);
2799 /* We're about to process an initializer for a class or array TYPE. Make
2800 sure that CTX is set up appropriately. */
2802 static void
2803 verify_ctor_sanity (const constexpr_ctx *ctx, tree type)
2805 /* We don't bother building a ctor for an empty base subobject. */
2806 if (is_empty_class (type))
2807 return;
2809 /* We're in the middle of an initializer that might involve placeholders;
2810 our caller should have created a CONSTRUCTOR for us to put the
2811 initializer into. We will either return that constructor or T. */
2812 gcc_assert (ctx->ctor);
2813 gcc_assert (same_type_ignoring_top_level_qualifiers_p
2814 (type, TREE_TYPE (ctx->ctor)));
2815 /* We used to check that ctx->ctor was empty, but that isn't the case when
2816 the object is zero-initialized before calling the constructor. */
2817 if (ctx->object)
2819 tree otype = TREE_TYPE (ctx->object);
2820 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, otype)
2821 /* Handle flexible array members. */
2822 || (TREE_CODE (otype) == ARRAY_TYPE
2823 && TYPE_DOMAIN (otype) == NULL_TREE
2824 && TREE_CODE (type) == ARRAY_TYPE
2825 && (same_type_ignoring_top_level_qualifiers_p
2826 (TREE_TYPE (type), TREE_TYPE (otype)))));
2828 gcc_assert (!ctx->object || !DECL_P (ctx->object)
2829 || *(ctx->values->get (ctx->object)) == ctx->ctor);
2832 /* Subroutine of cxx_eval_constant_expression.
2833 The expression tree T denotes a C-style array or a C-style
2834 aggregate. Reduce it to a constant expression. */
2836 static tree
2837 cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t,
2838 bool lval,
2839 bool *non_constant_p, bool *overflow_p)
2841 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
2842 bool changed = false;
2843 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t));
2844 tree type = TREE_TYPE (t);
2846 constexpr_ctx new_ctx;
2847 if (TYPE_PTRMEMFUNC_P (type) || VECTOR_TYPE_P (type))
2849 /* We don't really need the ctx->ctor business for a PMF or
2850 vector, but it's simpler to use the same code. */
2851 new_ctx = *ctx;
2852 new_ctx.ctor = build_constructor (type, NULL);
2853 new_ctx.object = NULL_TREE;
2854 ctx = &new_ctx;
2856 verify_ctor_sanity (ctx, type);
2857 vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
2858 vec_alloc (*p, vec_safe_length (v));
2860 unsigned i;
2861 tree index, value;
2862 bool constant_p = true;
2863 bool side_effects_p = false;
2864 FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value)
2866 tree orig_value = value;
2867 init_subob_ctx (ctx, new_ctx, index, value);
2868 if (new_ctx.ctor != ctx->ctor)
2869 /* If we built a new CONSTRUCTOR, attach it now so that other
2870 initializers can refer to it. */
2871 CONSTRUCTOR_APPEND_ELT (*p, index, new_ctx.ctor);
2872 tree elt = cxx_eval_constant_expression (&new_ctx, value,
2873 lval,
2874 non_constant_p, overflow_p);
2875 /* Don't VERIFY_CONSTANT here. */
2876 if (ctx->quiet && *non_constant_p)
2877 break;
2878 if (elt != orig_value)
2879 changed = true;
2881 if (!TREE_CONSTANT (elt))
2882 constant_p = false;
2883 if (TREE_SIDE_EFFECTS (elt))
2884 side_effects_p = true;
2885 if (index && TREE_CODE (index) == COMPONENT_REF)
2887 /* This is an initialization of a vfield inside a base
2888 subaggregate that we already initialized; push this
2889 initialization into the previous initialization. */
2890 constructor_elt *inner = base_field_constructor_elt (*p, index);
2891 inner->value = elt;
2892 changed = true;
2894 else if (index
2895 && (TREE_CODE (index) == NOP_EXPR
2896 || TREE_CODE (index) == POINTER_PLUS_EXPR))
2898 /* This is an initializer for an empty base; now that we've
2899 checked that it's constant, we can ignore it. */
2900 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index))));
2901 changed = true;
2903 else
2905 if (new_ctx.ctor != ctx->ctor)
2907 /* We appended this element above; update the value. */
2908 gcc_assert ((*p)->last().index == index);
2909 (*p)->last().value = elt;
2911 else
2912 CONSTRUCTOR_APPEND_ELT (*p, index, elt);
2913 /* Adding or replacing an element might change the ctor's flags. */
2914 TREE_CONSTANT (ctx->ctor) = constant_p;
2915 TREE_SIDE_EFFECTS (ctx->ctor) = side_effects_p;
2918 if (*non_constant_p || !changed)
2919 return t;
2920 t = ctx->ctor;
2921 /* We're done building this CONSTRUCTOR, so now we can interpret an
2922 element without an explicit initializer as value-initialized. */
2923 CONSTRUCTOR_NO_CLEARING (t) = false;
2924 TREE_CONSTANT (t) = constant_p;
2925 TREE_SIDE_EFFECTS (t) = side_effects_p;
2926 if (VECTOR_TYPE_P (type))
2927 t = fold (t);
2928 return t;
2931 /* Subroutine of cxx_eval_constant_expression.
2932 The expression tree T is a VEC_INIT_EXPR which denotes the desired
2933 initialization of a non-static data member of array type. Reduce it to a
2934 CONSTRUCTOR.
2936 Note that apart from value-initialization (when VALUE_INIT is true),
2937 this is only intended to support value-initialization and the
2938 initializations done by defaulted constructors for classes with
2939 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
2940 will either be NULL_TREE for the default constructor, or a COMPONENT_REF
2941 for the copy/move constructor. */
2943 static tree
2944 cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init,
2945 bool value_init, bool lval,
2946 bool *non_constant_p, bool *overflow_p)
2948 tree elttype = TREE_TYPE (atype);
2949 verify_ctor_sanity (ctx, atype);
2950 vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
2951 bool pre_init = false;
2952 unsigned HOST_WIDE_INT i;
2953 tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
2955 /* For the default constructor, build up a call to the default
2956 constructor of the element type. We only need to handle class types
2957 here, as for a constructor to be constexpr, all members must be
2958 initialized, which for a defaulted default constructor means they must
2959 be of a class type with a constexpr default constructor. */
2960 if (TREE_CODE (elttype) == ARRAY_TYPE)
2961 /* We only do this at the lowest level. */;
2962 else if (value_init)
2964 init = build_value_init (elttype, complain);
2965 pre_init = true;
2967 else if (!init)
2969 vec<tree, va_gc> *argvec = make_tree_vector ();
2970 init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
2971 &argvec, elttype, LOOKUP_NORMAL,
2972 complain);
2973 release_tree_vector (argvec);
2974 init = build_aggr_init_expr (TREE_TYPE (init), init);
2975 pre_init = true;
2978 tree nelts = get_array_or_vector_nelts (ctx, atype, non_constant_p,
2979 overflow_p);
2980 unsigned HOST_WIDE_INT max = tree_to_uhwi (nelts);
2981 for (i = 0; i < max; ++i)
2983 tree idx = build_int_cst (size_type_node, i);
2984 tree eltinit;
2985 bool reuse = false;
2986 constexpr_ctx new_ctx;
2987 init_subob_ctx (ctx, new_ctx, idx, pre_init ? init : elttype);
2988 if (new_ctx.ctor != ctx->ctor)
2989 CONSTRUCTOR_APPEND_ELT (*p, idx, new_ctx.ctor);
2990 if (TREE_CODE (elttype) == ARRAY_TYPE)
2992 /* A multidimensional array; recurse. */
2993 if (value_init || init == NULL_TREE)
2995 eltinit = NULL_TREE;
2996 reuse = i == 0;
2998 else
2999 eltinit = cp_build_array_ref (input_location, init, idx, complain);
3000 eltinit = cxx_eval_vec_init_1 (&new_ctx, elttype, eltinit, value_init,
3001 lval,
3002 non_constant_p, overflow_p);
3004 else if (pre_init)
3006 /* Initializing an element using value or default initialization
3007 we just pre-built above. */
3008 eltinit = cxx_eval_constant_expression (&new_ctx, init, lval,
3009 non_constant_p, overflow_p);
3010 reuse = i == 0;
3012 else
3014 /* Copying an element. */
3015 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3016 (atype, TREE_TYPE (init)));
3017 eltinit = cp_build_array_ref (input_location, init, idx, complain);
3018 if (!lvalue_p (init))
3019 eltinit = move (eltinit);
3020 eltinit = force_rvalue (eltinit, complain);
3021 eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval,
3022 non_constant_p, overflow_p);
3024 if (*non_constant_p && !ctx->quiet)
3025 break;
3026 if (new_ctx.ctor != ctx->ctor)
3028 /* We appended this element above; update the value. */
3029 gcc_assert ((*p)->last().index == idx);
3030 (*p)->last().value = eltinit;
3032 else
3033 CONSTRUCTOR_APPEND_ELT (*p, idx, eltinit);
3034 /* Reuse the result of cxx_eval_constant_expression call
3035 from the first iteration to all others if it is a constant
3036 initializer that doesn't require relocations. */
3037 if (reuse
3038 && max > 1
3039 && (eltinit == NULL_TREE
3040 || (initializer_constant_valid_p (eltinit, TREE_TYPE (eltinit))
3041 == null_pointer_node)))
3043 if (new_ctx.ctor != ctx->ctor)
3044 eltinit = new_ctx.ctor;
3045 tree range = build2 (RANGE_EXPR, size_type_node,
3046 build_int_cst (size_type_node, 1),
3047 build_int_cst (size_type_node, max - 1));
3048 CONSTRUCTOR_APPEND_ELT (*p, range, unshare_constructor (eltinit));
3049 break;
3051 else if (i == 0)
3052 vec_safe_reserve (*p, max);
3055 if (!*non_constant_p)
3057 init = ctx->ctor;
3058 CONSTRUCTOR_NO_CLEARING (init) = false;
3060 return init;
3063 static tree
3064 cxx_eval_vec_init (const constexpr_ctx *ctx, tree t,
3065 bool lval,
3066 bool *non_constant_p, bool *overflow_p)
3068 tree atype = TREE_TYPE (t);
3069 tree init = VEC_INIT_EXPR_INIT (t);
3070 tree r = cxx_eval_vec_init_1 (ctx, atype, init,
3071 VEC_INIT_EXPR_VALUE_INIT (t),
3072 lval, non_constant_p, overflow_p);
3073 if (*non_constant_p)
3074 return t;
3075 else
3076 return r;
3079 /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
3080 match. We want to be less strict for simple *& folding; if we have a
3081 non-const temporary that we access through a const pointer, that should
3082 work. We handle this here rather than change fold_indirect_ref_1
3083 because we're dealing with things like ADDR_EXPR of INTEGER_CST which
3084 don't really make sense outside of constant expression evaluation. Also
3085 we want to allow folding to COMPONENT_REF, which could cause trouble
3086 with TBAA in fold_indirect_ref_1.
3088 Try to keep this function synced with fold_indirect_ref_1. */
3090 static tree
3091 cxx_fold_indirect_ref (location_t loc, tree type, tree op0, bool *empty_base)
3093 tree sub = op0;
3094 tree subtype;
3095 poly_uint64 const_op01;
3097 STRIP_NOPS (sub);
3098 subtype = TREE_TYPE (sub);
3099 if (!INDIRECT_TYPE_P (subtype))
3100 return NULL_TREE;
3102 if (TREE_CODE (sub) == ADDR_EXPR)
3104 tree op = TREE_OPERAND (sub, 0);
3105 tree optype = TREE_TYPE (op);
3107 /* *&CONST_DECL -> to the value of the const decl. */
3108 if (TREE_CODE (op) == CONST_DECL)
3109 return DECL_INITIAL (op);
3110 /* *&p => p; make sure to handle *&"str"[cst] here. */
3111 if (same_type_ignoring_top_level_qualifiers_p (optype, type)
3112 /* Also handle the case where the desired type is an array of unknown
3113 bounds because the variable has had its bounds deduced since the
3114 ADDR_EXPR was created. */
3115 || (TREE_CODE (type) == ARRAY_TYPE
3116 && TREE_CODE (optype) == ARRAY_TYPE
3117 && TYPE_DOMAIN (type) == NULL_TREE
3118 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (optype),
3119 TREE_TYPE (type))))
3121 tree fop = fold_read_from_constant_string (op);
3122 if (fop)
3123 return fop;
3124 else
3125 return op;
3127 /* *(foo *)&fooarray => fooarray[0] */
3128 else if (TREE_CODE (optype) == ARRAY_TYPE
3129 && (same_type_ignoring_top_level_qualifiers_p
3130 (type, TREE_TYPE (optype))))
3132 tree type_domain = TYPE_DOMAIN (optype);
3133 tree min_val = size_zero_node;
3134 if (type_domain && TYPE_MIN_VALUE (type_domain))
3135 min_val = TYPE_MIN_VALUE (type_domain);
3136 return build4_loc (loc, ARRAY_REF, type, op, min_val,
3137 NULL_TREE, NULL_TREE);
3139 /* *(foo *)&complexfoo => __real__ complexfoo */
3140 else if (TREE_CODE (optype) == COMPLEX_TYPE
3141 && (same_type_ignoring_top_level_qualifiers_p
3142 (type, TREE_TYPE (optype))))
3143 return fold_build1_loc (loc, REALPART_EXPR, type, op);
3144 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
3145 else if (VECTOR_TYPE_P (optype)
3146 && (same_type_ignoring_top_level_qualifiers_p
3147 (type, TREE_TYPE (optype))))
3149 tree part_width = TYPE_SIZE (type);
3150 tree index = bitsize_int (0);
3151 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width,
3152 index);
3154 /* Also handle conversion to an empty base class, which
3155 is represented with a NOP_EXPR. */
3156 else if (is_empty_class (type)
3157 && CLASS_TYPE_P (optype)
3158 && DERIVED_FROM_P (type, optype))
3160 *empty_base = true;
3161 return op;
3163 /* *(foo *)&struct_with_foo_field => COMPONENT_REF */
3164 else if (RECORD_OR_UNION_TYPE_P (optype))
3166 tree field = TYPE_FIELDS (optype);
3167 for (; field; field = DECL_CHAIN (field))
3168 if (TREE_CODE (field) == FIELD_DECL
3169 && TREE_TYPE (field) != error_mark_node
3170 && integer_zerop (byte_position (field))
3171 && (same_type_ignoring_top_level_qualifiers_p
3172 (TREE_TYPE (field), type)))
3173 return fold_build3 (COMPONENT_REF, type, op, field, NULL_TREE);
3176 else if (TREE_CODE (sub) == POINTER_PLUS_EXPR
3177 && poly_int_tree_p (TREE_OPERAND (sub, 1), &const_op01))
3179 tree op00 = TREE_OPERAND (sub, 0);
3180 tree op01 = TREE_OPERAND (sub, 1);
3182 STRIP_NOPS (op00);
3183 if (TREE_CODE (op00) == ADDR_EXPR)
3185 tree op00type;
3186 op00 = TREE_OPERAND (op00, 0);
3187 op00type = TREE_TYPE (op00);
3189 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
3190 if (VECTOR_TYPE_P (op00type)
3191 && same_type_ignoring_top_level_qualifiers_p
3192 (type, TREE_TYPE (op00type))
3193 /* POINTER_PLUS_EXPR second operand is sizetype, unsigned,
3194 but we want to treat offsets with MSB set as negative.
3195 For the code below negative offsets are invalid and
3196 TYPE_SIZE of the element is something unsigned, so
3197 check whether op01 fits into poly_int64, which implies
3198 it is from 0 to INTTYPE_MAXIMUM (HOST_WIDE_INT), and
3199 then just use poly_uint64 because we want to treat the
3200 value as unsigned. */
3201 && tree_fits_poly_int64_p (op01))
3203 tree part_width = TYPE_SIZE (type);
3204 poly_uint64 max_offset
3205 = (tree_to_uhwi (part_width) / BITS_PER_UNIT
3206 * TYPE_VECTOR_SUBPARTS (op00type));
3207 if (known_lt (const_op01, max_offset))
3209 tree index = bitsize_int (const_op01 * BITS_PER_UNIT);
3210 return fold_build3_loc (loc,
3211 BIT_FIELD_REF, type, op00,
3212 part_width, index);
3215 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
3216 else if (TREE_CODE (op00type) == COMPLEX_TYPE
3217 && (same_type_ignoring_top_level_qualifiers_p
3218 (type, TREE_TYPE (op00type))))
3220 if (known_eq (wi::to_poly_offset (TYPE_SIZE_UNIT (type)),
3221 const_op01))
3222 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
3224 /* ((foo *)&fooarray)[1] => fooarray[1] */
3225 else if (TREE_CODE (op00type) == ARRAY_TYPE
3226 && (same_type_ignoring_top_level_qualifiers_p
3227 (type, TREE_TYPE (op00type))))
3229 tree type_domain = TYPE_DOMAIN (op00type);
3230 tree min_val = size_zero_node;
3231 if (type_domain && TYPE_MIN_VALUE (type_domain))
3232 min_val = TYPE_MIN_VALUE (type_domain);
3233 offset_int off = wi::to_offset (op01);
3234 offset_int el_sz = wi::to_offset (TYPE_SIZE_UNIT (type));
3235 offset_int remainder;
3236 off = wi::divmod_trunc (off, el_sz, SIGNED, &remainder);
3237 if (remainder == 0 && TREE_CODE (min_val) == INTEGER_CST)
3239 off = off + wi::to_offset (min_val);
3240 op01 = wide_int_to_tree (sizetype, off);
3241 return build4_loc (loc, ARRAY_REF, type, op00, op01,
3242 NULL_TREE, NULL_TREE);
3245 /* Also handle conversion to an empty base class, which
3246 is represented with a NOP_EXPR. */
3247 else if (is_empty_class (type)
3248 && CLASS_TYPE_P (op00type)
3249 && DERIVED_FROM_P (type, op00type))
3251 *empty_base = true;
3252 return op00;
3254 /* ((foo *)&struct_with_foo_field)[1] => COMPONENT_REF */
3255 else if (RECORD_OR_UNION_TYPE_P (op00type))
3257 tree field = TYPE_FIELDS (op00type);
3258 for (; field; field = DECL_CHAIN (field))
3259 if (TREE_CODE (field) == FIELD_DECL
3260 && TREE_TYPE (field) != error_mark_node
3261 && tree_int_cst_equal (byte_position (field), op01)
3262 && (same_type_ignoring_top_level_qualifiers_p
3263 (TREE_TYPE (field), type)))
3264 return fold_build3 (COMPONENT_REF, type, op00,
3265 field, NULL_TREE);
3269 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
3270 else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
3271 && (same_type_ignoring_top_level_qualifiers_p
3272 (type, TREE_TYPE (TREE_TYPE (subtype)))))
3274 tree type_domain;
3275 tree min_val = size_zero_node;
3276 tree newsub
3277 = cxx_fold_indirect_ref (loc, TREE_TYPE (subtype), sub, NULL);
3278 if (newsub)
3279 sub = newsub;
3280 else
3281 sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub);
3282 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
3283 if (type_domain && TYPE_MIN_VALUE (type_domain))
3284 min_val = TYPE_MIN_VALUE (type_domain);
3285 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
3286 NULL_TREE);
3289 return NULL_TREE;
3292 static tree
3293 cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t,
3294 bool lval,
3295 bool *non_constant_p, bool *overflow_p)
3297 tree orig_op0 = TREE_OPERAND (t, 0);
3298 bool empty_base = false;
3300 /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second
3301 operand is an integer-zero. Otherwise reject the MEM_REF for now. */
3303 if (TREE_CODE (t) == MEM_REF
3304 && (!TREE_OPERAND (t, 1) || !integer_zerop (TREE_OPERAND (t, 1))))
3306 gcc_assert (ctx->quiet);
3307 *non_constant_p = true;
3308 return t;
3311 /* First try to simplify it directly. */
3312 tree r = cxx_fold_indirect_ref (EXPR_LOCATION (t), TREE_TYPE (t), orig_op0,
3313 &empty_base);
3314 if (!r)
3316 /* If that didn't work, evaluate the operand first. */
3317 tree op0 = cxx_eval_constant_expression (ctx, orig_op0,
3318 /*lval*/false, non_constant_p,
3319 overflow_p);
3320 /* Don't VERIFY_CONSTANT here. */
3321 if (*non_constant_p)
3322 return t;
3324 if (!lval && integer_zerop (op0))
3326 if (!ctx->quiet)
3327 error ("dereferencing a null pointer");
3328 *non_constant_p = true;
3329 return t;
3332 r = cxx_fold_indirect_ref (EXPR_LOCATION (t), TREE_TYPE (t), op0,
3333 &empty_base);
3334 if (r == NULL_TREE)
3336 /* We couldn't fold to a constant value. Make sure it's not
3337 something we should have been able to fold. */
3338 tree sub = op0;
3339 STRIP_NOPS (sub);
3340 if (TREE_CODE (sub) == ADDR_EXPR)
3342 gcc_assert (!same_type_ignoring_top_level_qualifiers_p
3343 (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t)));
3344 /* DR 1188 says we don't have to deal with this. */
3345 if (!ctx->quiet)
3346 error ("accessing value of %qE through a %qT glvalue in a "
3347 "constant expression", build_fold_indirect_ref (sub),
3348 TREE_TYPE (t));
3349 *non_constant_p = true;
3350 return t;
3353 if (lval && op0 != orig_op0)
3354 return build1 (INDIRECT_REF, TREE_TYPE (t), op0);
3355 if (!lval)
3356 VERIFY_CONSTANT (t);
3357 return t;
3361 r = cxx_eval_constant_expression (ctx, r,
3362 lval, non_constant_p, overflow_p);
3363 if (*non_constant_p)
3364 return t;
3366 /* If we're pulling out the value of an empty base, just return an empty
3367 CONSTRUCTOR. */
3368 if (empty_base && !lval)
3370 r = build_constructor (TREE_TYPE (t), NULL);
3371 TREE_CONSTANT (r) = true;
3374 return r;
3377 /* Complain about R, a VAR_DECL, not being usable in a constant expression.
3378 Shared between potential_constant_expression and
3379 cxx_eval_constant_expression. */
3381 static void
3382 non_const_var_error (tree r)
3384 tree type = TREE_TYPE (r);
3385 error ("the value of %qD is not usable in a constant "
3386 "expression", r);
3387 /* Avoid error cascade. */
3388 if (DECL_INITIAL (r) == error_mark_node)
3389 return;
3390 if (DECL_DECLARED_CONSTEXPR_P (r))
3391 inform (DECL_SOURCE_LOCATION (r),
3392 "%qD used in its own initializer", r);
3393 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3395 if (!CP_TYPE_CONST_P (type))
3396 inform (DECL_SOURCE_LOCATION (r),
3397 "%q#D is not const", r);
3398 else if (CP_TYPE_VOLATILE_P (type))
3399 inform (DECL_SOURCE_LOCATION (r),
3400 "%q#D is volatile", r);
3401 else if (!DECL_INITIAL (r)
3402 || !TREE_CONSTANT (DECL_INITIAL (r))
3403 || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r))
3404 inform (DECL_SOURCE_LOCATION (r),
3405 "%qD was not initialized with a constant "
3406 "expression", r);
3407 else
3408 gcc_unreachable ();
3410 else if (TYPE_REF_P (type))
3411 inform (DECL_SOURCE_LOCATION (r),
3412 "%qD was not initialized with a constant "
3413 "expression", r);
3414 else
3416 if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r))
3417 inform (DECL_SOURCE_LOCATION (r),
3418 "%qD was not declared %<constexpr%>", r);
3419 else
3420 inform (DECL_SOURCE_LOCATION (r),
3421 "%qD does not have integral or enumeration type",
3426 /* Subroutine of cxx_eval_constant_expression.
3427 Like cxx_eval_unary_expression, except for trinary expressions. */
3429 static tree
3430 cxx_eval_trinary_expression (const constexpr_ctx *ctx, tree t,
3431 bool lval,
3432 bool *non_constant_p, bool *overflow_p)
3434 int i;
3435 tree args[3];
3436 tree val;
3438 for (i = 0; i < 3; i++)
3440 args[i] = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, i),
3441 lval,
3442 non_constant_p, overflow_p);
3443 VERIFY_CONSTANT (args[i]);
3446 val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t),
3447 args[0], args[1], args[2]);
3448 if (val == NULL_TREE)
3449 return t;
3450 VERIFY_CONSTANT (val);
3451 return val;
3454 /* True if T was declared in a function declared to be constexpr, and
3455 therefore potentially constant in C++14. */
3457 bool
3458 var_in_constexpr_fn (tree t)
3460 tree ctx = DECL_CONTEXT (t);
3461 return (ctx && TREE_CODE (ctx) == FUNCTION_DECL
3462 && DECL_DECLARED_CONSTEXPR_P (ctx));
3465 /* True if T was declared in a function that might be constexpr: either a
3466 function that was declared constexpr, or a C++17 lambda op(). */
3468 bool
3469 var_in_maybe_constexpr_fn (tree t)
3471 if (cxx_dialect >= cxx17
3472 && DECL_FUNCTION_SCOPE_P (t)
3473 && LAMBDA_FUNCTION_P (DECL_CONTEXT (t)))
3474 return true;
3475 return var_in_constexpr_fn (t);
3478 /* We're assigning INIT to TARGET. In do_build_copy_constructor and
3479 build_over_call we implement trivial copy of a class with tail padding using
3480 assignment of character arrays, which is valid in normal code, but not in
3481 constexpr evaluation. We don't need to worry about clobbering tail padding
3482 in constexpr evaluation, so strip the type punning. */
3484 static void
3485 maybe_simplify_trivial_copy (tree &target, tree &init)
3487 if (TREE_CODE (target) == MEM_REF
3488 && TREE_CODE (init) == MEM_REF
3489 && TREE_TYPE (target) == TREE_TYPE (init)
3490 && TREE_CODE (TREE_TYPE (target)) == ARRAY_TYPE
3491 && TREE_TYPE (TREE_TYPE (target)) == unsigned_char_type_node)
3493 target = build_fold_indirect_ref (TREE_OPERAND (target, 0));
3494 init = build_fold_indirect_ref (TREE_OPERAND (init, 0));
3498 /* Evaluate an INIT_EXPR or MODIFY_EXPR. */
3500 static tree
3501 cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
3502 bool lval,
3503 bool *non_constant_p, bool *overflow_p)
3505 constexpr_ctx new_ctx = *ctx;
3507 tree init = TREE_OPERAND (t, 1);
3508 if (TREE_CLOBBER_P (init))
3509 /* Just ignore clobbers. */
3510 return void_node;
3512 /* First we figure out where we're storing to. */
3513 tree target = TREE_OPERAND (t, 0);
3515 maybe_simplify_trivial_copy (target, init);
3517 tree type = TREE_TYPE (target);
3518 target = cxx_eval_constant_expression (ctx, target,
3519 true,
3520 non_constant_p, overflow_p);
3521 if (*non_constant_p)
3522 return t;
3524 /* cxx_eval_array_reference for lval = true allows references one past
3525 end of array, because it does not know if it is just taking address
3526 (which is valid), or actual dereference. Here we know it is
3527 a dereference, so diagnose it here. */
3528 for (tree probe = target; probe; )
3530 switch (TREE_CODE (probe))
3532 case ARRAY_REF:
3533 tree nelts, ary;
3534 ary = TREE_OPERAND (probe, 0);
3535 nelts = get_array_or_vector_nelts (ctx, TREE_TYPE (ary),
3536 non_constant_p, overflow_p);
3537 VERIFY_CONSTANT (nelts);
3538 gcc_assert (TREE_CODE (nelts) == INTEGER_CST
3539 && TREE_CODE (TREE_OPERAND (probe, 1)) == INTEGER_CST);
3540 if (wi::to_widest (TREE_OPERAND (probe, 1)) == wi::to_widest (nelts))
3542 diag_array_subscript (ctx, ary, TREE_OPERAND (probe, 1));
3543 *non_constant_p = true;
3544 return t;
3546 /* FALLTHRU */
3548 case BIT_FIELD_REF:
3549 case COMPONENT_REF:
3550 probe = TREE_OPERAND (probe, 0);
3551 continue;
3553 default:
3554 probe = NULL_TREE;
3555 continue;
3559 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (target), type))
3561 /* For initialization of an empty base, the original target will be
3562 *(base*)this, which the above evaluation resolves to the object
3563 argument, which has the derived type rather than the base type. In
3564 this situation, just evaluate the initializer and return, since
3565 there's no actual data to store. */
3566 gcc_assert (is_empty_class (type));
3567 return cxx_eval_constant_expression (ctx, init, false,
3568 non_constant_p, overflow_p);
3571 /* And then find the underlying variable. */
3572 vec<tree,va_gc> *refs = make_tree_vector();
3573 tree object = NULL_TREE;
3574 for (tree probe = target; object == NULL_TREE; )
3576 switch (TREE_CODE (probe))
3578 case BIT_FIELD_REF:
3579 case COMPONENT_REF:
3580 case ARRAY_REF:
3581 vec_safe_push (refs, TREE_OPERAND (probe, 1));
3582 vec_safe_push (refs, TREE_TYPE (probe));
3583 probe = TREE_OPERAND (probe, 0);
3584 break;
3586 default:
3587 object = probe;
3591 /* And then find/build up our initializer for the path to the subobject
3592 we're initializing. */
3593 tree *valp;
3594 if (object == ctx->object && VAR_P (object)
3595 && DECL_NAME (object) && ctx->call == NULL)
3596 /* The variable we're building up an aggregate initializer for is outside
3597 the constant-expression, so don't evaluate the store. We check
3598 DECL_NAME to handle TARGET_EXPR temporaries, which are fair game. */
3599 valp = NULL;
3600 else if (DECL_P (object))
3601 valp = ctx->values->get (object);
3602 else
3603 valp = NULL;
3604 if (!valp)
3606 /* A constant-expression cannot modify objects from outside the
3607 constant-expression. */
3608 if (!ctx->quiet)
3609 error ("modification of %qE is not a constant expression", object);
3610 *non_constant_p = true;
3611 return t;
3613 type = TREE_TYPE (object);
3614 bool no_zero_init = true;
3616 vec<tree,va_gc> *ctors = make_tree_vector ();
3617 while (!refs->is_empty())
3619 if (*valp == NULL_TREE)
3621 *valp = build_constructor (type, NULL);
3622 CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
3624 else if (TREE_CODE (*valp) == STRING_CST)
3626 /* An array was initialized with a string constant, and now
3627 we're writing into one of its elements. Explode the
3628 single initialization into a set of element
3629 initializations. */
3630 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
3632 tree string = *valp;
3633 tree elt_type = TREE_TYPE (type);
3634 unsigned chars_per_elt = (TYPE_PRECISION (elt_type)
3635 / TYPE_PRECISION (char_type_node));
3636 unsigned num_elts = TREE_STRING_LENGTH (string) / chars_per_elt;
3637 tree ary_ctor = build_constructor (type, NULL);
3639 vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor), num_elts);
3640 for (unsigned ix = 0; ix != num_elts; ix++)
3642 constructor_elt elt =
3644 build_int_cst (size_type_node, ix),
3645 extract_string_elt (string, chars_per_elt, ix)
3647 CONSTRUCTOR_ELTS (ary_ctor)->quick_push (elt);
3650 *valp = ary_ctor;
3653 /* If the value of object is already zero-initialized, any new ctors for
3654 subobjects will also be zero-initialized. */
3655 no_zero_init = CONSTRUCTOR_NO_CLEARING (*valp);
3657 vec_safe_push (ctors, *valp);
3659 enum tree_code code = TREE_CODE (type);
3660 type = refs->pop();
3661 tree index = refs->pop();
3663 constructor_elt *cep = NULL;
3664 if (code == ARRAY_TYPE)
3666 HOST_WIDE_INT i
3667 = find_array_ctor_elt (*valp, index, /*insert*/true);
3668 gcc_assert (i >= 0);
3669 cep = CONSTRUCTOR_ELT (*valp, i);
3670 gcc_assert (TREE_CODE (cep->index) != RANGE_EXPR);
3672 else
3674 gcc_assert (TREE_CODE (index) == FIELD_DECL);
3676 /* We must keep the CONSTRUCTOR's ELTS in FIELD order.
3677 Usually we meet initializers in that order, but it is
3678 possible for base types to be placed not in program
3679 order. */
3680 tree fields = TYPE_FIELDS (DECL_CONTEXT (index));
3681 unsigned HOST_WIDE_INT idx;
3683 if (code == UNION_TYPE && CONSTRUCTOR_NELTS (*valp)
3684 && CONSTRUCTOR_ELT (*valp, 0)->index != index)
3685 /* Changing active member. */
3686 vec_safe_truncate (CONSTRUCTOR_ELTS (*valp), 0);
3688 for (idx = 0;
3689 vec_safe_iterate (CONSTRUCTOR_ELTS (*valp), idx, &cep);
3690 idx++, fields = DECL_CHAIN (fields))
3692 if (index == cep->index)
3693 goto found;
3695 /* The field we're initializing must be on the field
3696 list. Look to see if it is present before the
3697 field the current ELT initializes. */
3698 for (; fields != cep->index; fields = DECL_CHAIN (fields))
3699 if (index == fields)
3700 goto insert;
3703 /* We fell off the end of the CONSTRUCTOR, so insert a new
3704 entry at the end. */
3705 insert:
3707 constructor_elt ce = { index, NULL_TREE };
3709 vec_safe_insert (CONSTRUCTOR_ELTS (*valp), idx, ce);
3710 cep = CONSTRUCTOR_ELT (*valp, idx);
3712 found:;
3714 valp = &cep->value;
3716 release_tree_vector (refs);
3718 if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
3720 /* Create a new CONSTRUCTOR in case evaluation of the initializer
3721 wants to modify it. */
3722 if (*valp == NULL_TREE)
3724 *valp = build_constructor (type, NULL);
3725 CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
3727 else if (TREE_CODE (*valp) == PTRMEM_CST)
3728 *valp = cplus_expand_constant (*valp);
3729 new_ctx.ctor = *valp;
3730 new_ctx.object = target;
3733 init = cxx_eval_constant_expression (&new_ctx, init, false,
3734 non_constant_p, overflow_p);
3735 /* Don't share a CONSTRUCTOR that might be changed later. */
3736 init = unshare_constructor (init);
3737 if (target == object)
3738 /* The hash table might have moved since the get earlier. */
3739 valp = ctx->values->get (object);
3741 if (TREE_CODE (init) == CONSTRUCTOR)
3743 /* An outer ctx->ctor might be pointing to *valp, so replace
3744 its contents. */
3745 CONSTRUCTOR_ELTS (*valp) = CONSTRUCTOR_ELTS (init);
3746 TREE_CONSTANT (*valp) = TREE_CONSTANT (init);
3747 TREE_SIDE_EFFECTS (*valp) = TREE_SIDE_EFFECTS (init);
3748 CONSTRUCTOR_NO_CLEARING (*valp)
3749 = CONSTRUCTOR_NO_CLEARING (init);
3751 else
3752 *valp = init;
3754 /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing
3755 CONSTRUCTORs, if any. */
3756 tree elt;
3757 unsigned i;
3758 bool c = TREE_CONSTANT (init);
3759 bool s = TREE_SIDE_EFFECTS (init);
3760 if (!c || s)
3761 FOR_EACH_VEC_SAFE_ELT (ctors, i, elt)
3763 if (!c)
3764 TREE_CONSTANT (elt) = false;
3765 if (s)
3766 TREE_SIDE_EFFECTS (elt) = true;
3768 release_tree_vector (ctors);
3770 if (*non_constant_p)
3771 return t;
3772 else if (lval)
3773 return target;
3774 else
3775 return init;
3778 /* Evaluate a ++ or -- expression. */
3780 static tree
3781 cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t,
3782 bool lval,
3783 bool *non_constant_p, bool *overflow_p)
3785 enum tree_code code = TREE_CODE (t);
3786 tree type = TREE_TYPE (t);
3787 tree op = TREE_OPERAND (t, 0);
3788 tree offset = TREE_OPERAND (t, 1);
3789 gcc_assert (TREE_CONSTANT (offset));
3791 /* The operand as an lvalue. */
3792 op = cxx_eval_constant_expression (ctx, op, true,
3793 non_constant_p, overflow_p);
3795 /* The operand as an rvalue. */
3796 tree val
3797 = cxx_eval_constant_expression (ctx, op, false,
3798 non_constant_p, overflow_p);
3799 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
3800 a local array in a constexpr function. */
3801 bool ptr = INDIRECT_TYPE_P (TREE_TYPE (val));
3802 if (!ptr)
3803 VERIFY_CONSTANT (val);
3805 /* The modified value. */
3806 bool inc = (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR);
3807 tree mod;
3808 if (INDIRECT_TYPE_P (type))
3810 /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
3811 offset = convert_to_ptrofftype (offset);
3812 if (!inc)
3813 offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset);
3814 mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset);
3816 else
3817 mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset);
3818 if (!ptr)
3819 VERIFY_CONSTANT (mod);
3821 /* Storing the modified value. */
3822 tree store = build2 (MODIFY_EXPR, type, op, mod);
3823 cxx_eval_constant_expression (ctx, store,
3824 true, non_constant_p, overflow_p);
3826 /* And the value of the expression. */
3827 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
3829 /* Prefix ops are lvalues. */
3830 if (lval)
3831 return op;
3832 else
3833 /* But we optimize when the caller wants an rvalue. */
3834 return mod;
3836 else
3837 /* Postfix ops are rvalues. */
3838 return val;
3841 /* Predicates for the meaning of *jump_target. */
3843 static bool
3844 returns (tree *jump_target)
3846 return *jump_target
3847 && (TREE_CODE (*jump_target) == RETURN_EXPR
3848 || (TREE_CODE (*jump_target) == LABEL_DECL
3849 && LABEL_DECL_CDTOR (*jump_target)));
3852 static bool
3853 breaks (tree *jump_target)
3855 return *jump_target
3856 && ((TREE_CODE (*jump_target) == LABEL_DECL
3857 && LABEL_DECL_BREAK (*jump_target))
3858 || TREE_CODE (*jump_target) == EXIT_EXPR);
3861 static bool
3862 continues (tree *jump_target)
3864 return *jump_target
3865 && TREE_CODE (*jump_target) == LABEL_DECL
3866 && LABEL_DECL_CONTINUE (*jump_target);
3869 static bool
3870 switches (tree *jump_target)
3872 return *jump_target
3873 && TREE_CODE (*jump_target) == INTEGER_CST;
3876 /* Subroutine of cxx_eval_statement_list. Determine whether the statement
3877 STMT matches *jump_target. If we're looking for a case label and we see
3878 the default label, note it in ctx->css_state. */
3880 static bool
3881 label_matches (const constexpr_ctx *ctx, tree *jump_target, tree stmt)
3883 switch (TREE_CODE (*jump_target))
3885 case LABEL_DECL:
3886 if (TREE_CODE (stmt) == LABEL_EXPR
3887 && LABEL_EXPR_LABEL (stmt) == *jump_target)
3888 return true;
3889 break;
3891 case INTEGER_CST:
3892 if (TREE_CODE (stmt) == CASE_LABEL_EXPR)
3894 gcc_assert (ctx->css_state != NULL);
3895 if (!CASE_LOW (stmt))
3897 /* default: should appear just once in a SWITCH_EXPR
3898 body (excluding nested SWITCH_EXPR). */
3899 gcc_assert (*ctx->css_state != css_default_seen);
3900 /* When evaluating SWITCH_EXPR body for the second time,
3901 return true for the default: label. */
3902 if (*ctx->css_state == css_default_processing)
3903 return true;
3904 *ctx->css_state = css_default_seen;
3906 else if (CASE_HIGH (stmt))
3908 if (tree_int_cst_le (CASE_LOW (stmt), *jump_target)
3909 && tree_int_cst_le (*jump_target, CASE_HIGH (stmt)))
3910 return true;
3912 else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt)))
3913 return true;
3915 break;
3917 default:
3918 gcc_unreachable ();
3920 return false;
3923 /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
3924 semantics, for switch, break, continue, and return. */
3926 static tree
3927 cxx_eval_statement_list (const constexpr_ctx *ctx, tree t,
3928 bool *non_constant_p, bool *overflow_p,
3929 tree *jump_target)
3931 tree_stmt_iterator i;
3932 tree local_target;
3933 /* In a statement-expression we want to return the last value.
3934 For empty statement expression return void_node. */
3935 tree r = void_node;
3936 if (!jump_target)
3938 local_target = NULL_TREE;
3939 jump_target = &local_target;
3941 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
3943 tree stmt = tsi_stmt (i);
3944 if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT)
3945 continue;
3946 r = cxx_eval_constant_expression (ctx, stmt, false,
3947 non_constant_p, overflow_p,
3948 jump_target);
3949 if (*non_constant_p)
3950 break;
3951 if (returns (jump_target) || breaks (jump_target))
3952 break;
3954 return r;
3957 /* Evaluate a LOOP_EXPR for side-effects. Handles break and return
3958 semantics; continue semantics are covered by cxx_eval_statement_list. */
3960 static tree
3961 cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t,
3962 bool *non_constant_p, bool *overflow_p,
3963 tree *jump_target)
3965 constexpr_ctx new_ctx = *ctx;
3967 tree body = TREE_OPERAND (t, 0);
3968 int count = 0;
3971 hash_set<tree> save_exprs;
3972 new_ctx.save_exprs = &save_exprs;
3974 cxx_eval_constant_expression (&new_ctx, body, /*lval*/false,
3975 non_constant_p, overflow_p, jump_target);
3977 /* Forget saved values of SAVE_EXPRs. */
3978 for (hash_set<tree>::iterator iter = save_exprs.begin();
3979 iter != save_exprs.end(); ++iter)
3980 new_ctx.values->remove (*iter);
3981 if (++count >= constexpr_loop_limit)
3983 if (!ctx->quiet)
3984 error_at (cp_expr_loc_or_loc (t, input_location),
3985 "%<constexpr%> loop iteration count exceeds limit of %d "
3986 "(use -fconstexpr-loop-limit= to increase the limit)",
3987 constexpr_loop_limit);
3988 *non_constant_p = true;
3989 break;
3992 while (!returns (jump_target)
3993 && !breaks (jump_target)
3994 && !switches (jump_target)
3995 && !*non_constant_p);
3997 if (breaks (jump_target))
3998 *jump_target = NULL_TREE;
4000 return NULL_TREE;
4003 /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
4004 semantics. */
4006 static tree
4007 cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t,
4008 bool *non_constant_p, bool *overflow_p,
4009 tree *jump_target)
4011 tree cond = TREE_OPERAND (t, 0);
4012 cond = cxx_eval_constant_expression (ctx, cond, false,
4013 non_constant_p, overflow_p);
4014 VERIFY_CONSTANT (cond);
4015 *jump_target = cond;
4017 tree body = TREE_OPERAND (t, 1);
4018 constexpr_ctx new_ctx = *ctx;
4019 constexpr_switch_state css = css_default_not_seen;
4020 new_ctx.css_state = &css;
4021 cxx_eval_constant_expression (&new_ctx, body, false,
4022 non_constant_p, overflow_p, jump_target);
4023 if (switches (jump_target) && css == css_default_seen)
4025 /* If the SWITCH_EXPR body has default: label, process it once again,
4026 this time instructing label_matches to return true for default:
4027 label on switches (jump_target). */
4028 css = css_default_processing;
4029 cxx_eval_constant_expression (&new_ctx, body, false,
4030 non_constant_p, overflow_p, jump_target);
4032 if (breaks (jump_target) || switches (jump_target))
4033 *jump_target = NULL_TREE;
4034 return NULL_TREE;
4037 /* Find the object of TYPE under initialization in CTX. */
4039 static tree
4040 lookup_placeholder (const constexpr_ctx *ctx, bool lval, tree type)
4042 if (!ctx)
4043 return NULL_TREE;
4045 /* We could use ctx->object unconditionally, but using ctx->ctor when we
4046 can is a minor optimization. */
4047 if (!lval && ctx->ctor && same_type_p (TREE_TYPE (ctx->ctor), type))
4048 return ctx->ctor;
4050 if (!ctx->object)
4051 return NULL_TREE;
4053 /* Since an object cannot have a field of its own type, we can search outward
4054 from ctx->object to find the unique containing object of TYPE. */
4055 tree ob = ctx->object;
4056 while (ob)
4058 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob), type))
4059 break;
4060 if (handled_component_p (ob))
4061 ob = TREE_OPERAND (ob, 0);
4062 else
4063 ob = NULL_TREE;
4066 return ob;
4069 /* Attempt to reduce the expression T to a constant value.
4070 On failure, issue diagnostic and return error_mark_node. */
4071 /* FIXME unify with c_fully_fold */
4072 /* FIXME overflow_p is too global */
4074 static tree
4075 cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
4076 bool lval,
4077 bool *non_constant_p, bool *overflow_p,
4078 tree *jump_target)
4080 constexpr_ctx new_ctx;
4081 tree r = t;
4083 if (jump_target && *jump_target)
4085 /* If we are jumping, ignore all statements/expressions except those
4086 that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */
4087 switch (TREE_CODE (t))
4089 case BIND_EXPR:
4090 case STATEMENT_LIST:
4091 case LOOP_EXPR:
4092 case COND_EXPR:
4093 break;
4094 case LABEL_EXPR:
4095 case CASE_LABEL_EXPR:
4096 if (label_matches (ctx, jump_target, t))
4097 /* Found it. */
4098 *jump_target = NULL_TREE;
4099 return NULL_TREE;
4100 default:
4101 return NULL_TREE;
4104 if (t == error_mark_node)
4106 *non_constant_p = true;
4107 return t;
4109 if (CONSTANT_CLASS_P (t))
4111 if (TREE_OVERFLOW (t))
4113 if (!ctx->quiet)
4114 permerror (input_location, "overflow in constant expression");
4115 if (!flag_permissive || ctx->quiet)
4116 *overflow_p = true;
4119 if (TREE_CODE (t) == INTEGER_CST
4120 && TYPE_PTR_P (TREE_TYPE (t))
4121 && !integer_zerop (t))
4123 if (!ctx->quiet)
4124 error ("value %qE of type %qT is not a constant expression",
4125 t, TREE_TYPE (t));
4126 *non_constant_p = true;
4129 return t;
4132 tree_code tcode = TREE_CODE (t);
4133 switch (tcode)
4135 case RESULT_DECL:
4136 if (lval)
4137 return t;
4138 /* We ask for an rvalue for the RESULT_DECL when indirecting
4139 through an invisible reference, or in named return value
4140 optimization. */
4141 if (tree *p = ctx->values->get (t))
4142 return *p;
4143 else
4145 if (!ctx->quiet)
4146 error ("%qE is not a constant expression", t);
4147 *non_constant_p = true;
4149 break;
4151 case VAR_DECL:
4152 if (DECL_HAS_VALUE_EXPR_P (t))
4153 return cxx_eval_constant_expression (ctx, DECL_VALUE_EXPR (t),
4154 lval, non_constant_p, overflow_p);
4155 /* fall through */
4156 case CONST_DECL:
4157 /* We used to not check lval for CONST_DECL, but darwin.c uses
4158 CONST_DECL for aggregate constants. */
4159 if (lval)
4160 return t;
4161 if (COMPLETE_TYPE_P (TREE_TYPE (t))
4162 && is_really_empty_class (TREE_TYPE (t)))
4164 /* If the class is empty, we aren't actually loading anything. */
4165 r = build_constructor (TREE_TYPE (t), NULL);
4166 TREE_CONSTANT (r) = true;
4168 else if (ctx->strict)
4169 r = decl_really_constant_value (t);
4170 else
4171 r = decl_constant_value (t);
4172 if (TREE_CODE (r) == TARGET_EXPR
4173 && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR)
4174 r = TARGET_EXPR_INITIAL (r);
4175 if (VAR_P (r))
4176 if (tree *p = ctx->values->get (r))
4177 if (*p != NULL_TREE)
4178 r = *p;
4179 if (DECL_P (r))
4181 if (!ctx->quiet)
4182 non_const_var_error (r);
4183 *non_constant_p = true;
4185 break;
4187 case DEBUG_BEGIN_STMT:
4188 /* ??? It might be nice to retain this information somehow, so
4189 as to be able to step into a constexpr function call. */
4190 /* Fall through. */
4192 case FUNCTION_DECL:
4193 case TEMPLATE_DECL:
4194 case LABEL_DECL:
4195 case LABEL_EXPR:
4196 case CASE_LABEL_EXPR:
4197 case PREDICT_EXPR:
4198 return t;
4200 case PARM_DECL:
4201 if (lval && !TYPE_REF_P (TREE_TYPE (t)))
4202 /* glvalue use. */;
4203 else if (tree *p = ctx->values->get (r))
4204 r = *p;
4205 else if (lval)
4206 /* Defer in case this is only used for its type. */;
4207 else if (TYPE_REF_P (TREE_TYPE (t)))
4208 /* Defer, there's no lvalue->rvalue conversion. */;
4209 else if (COMPLETE_TYPE_P (TREE_TYPE (t))
4210 && is_really_empty_class (TREE_TYPE (t)))
4212 /* If the class is empty, we aren't actually loading anything. */
4213 r = build_constructor (TREE_TYPE (t), NULL);
4214 TREE_CONSTANT (r) = true;
4216 else
4218 if (!ctx->quiet)
4219 error ("%qE is not a constant expression", t);
4220 *non_constant_p = true;
4222 break;
4224 case CALL_EXPR:
4225 case AGGR_INIT_EXPR:
4226 r = cxx_eval_call_expression (ctx, t, lval,
4227 non_constant_p, overflow_p);
4228 break;
4230 case DECL_EXPR:
4232 r = DECL_EXPR_DECL (t);
4233 if (AGGREGATE_TYPE_P (TREE_TYPE (r))
4234 || VECTOR_TYPE_P (TREE_TYPE (r)))
4236 new_ctx = *ctx;
4237 new_ctx.object = r;
4238 new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL);
4239 CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
4240 new_ctx.values->put (r, new_ctx.ctor);
4241 ctx = &new_ctx;
4244 if (tree init = DECL_INITIAL (r))
4246 init = cxx_eval_constant_expression (ctx, init,
4247 false,
4248 non_constant_p, overflow_p);
4249 /* Don't share a CONSTRUCTOR that might be changed. */
4250 init = unshare_constructor (init);
4251 ctx->values->put (r, init);
4253 else if (ctx == &new_ctx)
4254 /* We gave it a CONSTRUCTOR above. */;
4255 else
4256 ctx->values->put (r, NULL_TREE);
4258 break;
4260 case TARGET_EXPR:
4261 if (!literal_type_p (TREE_TYPE (t)))
4263 if (!ctx->quiet)
4265 error ("temporary of non-literal type %qT in a "
4266 "constant expression", TREE_TYPE (t));
4267 explain_non_literal_class (TREE_TYPE (t));
4269 *non_constant_p = true;
4270 break;
4272 if ((AGGREGATE_TYPE_P (TREE_TYPE (t)) || VECTOR_TYPE_P (TREE_TYPE (t))))
4274 /* We're being expanded without an explicit target, so start
4275 initializing a new object; expansion with an explicit target
4276 strips the TARGET_EXPR before we get here. */
4277 new_ctx = *ctx;
4278 new_ctx.ctor = build_constructor (TREE_TYPE (t), NULL);
4279 CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
4280 new_ctx.object = TARGET_EXPR_SLOT (t);
4281 ctx->values->put (new_ctx.object, new_ctx.ctor);
4282 ctx = &new_ctx;
4284 /* Pass false for 'lval' because this indicates
4285 initialization of a temporary. */
4286 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
4287 false,
4288 non_constant_p, overflow_p);
4289 if (!*non_constant_p)
4290 /* Adjust the type of the result to the type of the temporary. */
4291 r = adjust_temp_type (TREE_TYPE (t), r);
4292 if (lval)
4294 tree slot = TARGET_EXPR_SLOT (t);
4295 r = unshare_constructor (r);
4296 ctx->values->put (slot, r);
4297 return slot;
4299 break;
4301 case INIT_EXPR:
4302 case MODIFY_EXPR:
4303 gcc_assert (jump_target == NULL || *jump_target == NULL_TREE);
4304 r = cxx_eval_store_expression (ctx, t, lval,
4305 non_constant_p, overflow_p);
4306 break;
4308 case SCOPE_REF:
4309 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
4310 lval,
4311 non_constant_p, overflow_p);
4312 break;
4314 case RETURN_EXPR:
4315 if (TREE_OPERAND (t, 0) != NULL_TREE)
4316 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
4317 lval,
4318 non_constant_p, overflow_p);
4319 if (jump_target)
4320 *jump_target = t;
4321 else
4323 /* Can happen with ({ return true; }) && false; passed to
4324 maybe_constant_value. There is nothing to jump over in this
4325 case, and the bug will be diagnosed later. */
4326 gcc_assert (ctx->quiet);
4327 *non_constant_p = true;
4329 break;
4331 case SAVE_EXPR:
4332 /* Avoid evaluating a SAVE_EXPR more than once. */
4333 if (tree *p = ctx->values->get (t))
4334 r = *p;
4335 else
4337 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), false,
4338 non_constant_p, overflow_p);
4339 ctx->values->put (t, r);
4340 if (ctx->save_exprs)
4341 ctx->save_exprs->add (t);
4343 break;
4345 case NON_LVALUE_EXPR:
4346 case TRY_CATCH_EXPR:
4347 case TRY_BLOCK:
4348 case CLEANUP_POINT_EXPR:
4349 case MUST_NOT_THROW_EXPR:
4350 case EXPR_STMT:
4351 case EH_SPEC_BLOCK:
4352 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
4353 lval,
4354 non_constant_p, overflow_p,
4355 jump_target);
4356 break;
4358 case TRY_FINALLY_EXPR:
4359 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
4360 non_constant_p, overflow_p,
4361 jump_target);
4362 if (!*non_constant_p)
4363 /* Also evaluate the cleanup. */
4364 cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), true,
4365 non_constant_p, overflow_p,
4366 jump_target);
4367 break;
4369 /* These differ from cxx_eval_unary_expression in that this doesn't
4370 check for a constant operand or result; an address can be
4371 constant without its operand being, and vice versa. */
4372 case MEM_REF:
4373 case INDIRECT_REF:
4374 r = cxx_eval_indirect_ref (ctx, t, lval,
4375 non_constant_p, overflow_p);
4376 break;
4378 case ADDR_EXPR:
4380 tree oldop = TREE_OPERAND (t, 0);
4381 tree op = cxx_eval_constant_expression (ctx, oldop,
4382 /*lval*/true,
4383 non_constant_p, overflow_p);
4384 /* Don't VERIFY_CONSTANT here. */
4385 if (*non_constant_p)
4386 return t;
4387 gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR);
4388 /* This function does more aggressive folding than fold itself. */
4389 r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
4390 if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
4391 return t;
4392 break;
4395 case REALPART_EXPR:
4396 case IMAGPART_EXPR:
4397 if (lval)
4399 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
4400 non_constant_p, overflow_p);
4401 if (r == error_mark_node)
4403 else if (r == TREE_OPERAND (t, 0))
4404 r = t;
4405 else
4406 r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), r);
4407 break;
4409 /* FALLTHRU */
4410 case CONJ_EXPR:
4411 case FIX_TRUNC_EXPR:
4412 case FLOAT_EXPR:
4413 case NEGATE_EXPR:
4414 case ABS_EXPR:
4415 case ABSU_EXPR:
4416 case BIT_NOT_EXPR:
4417 case TRUTH_NOT_EXPR:
4418 case FIXED_CONVERT_EXPR:
4419 r = cxx_eval_unary_expression (ctx, t, lval,
4420 non_constant_p, overflow_p);
4421 break;
4423 case SIZEOF_EXPR:
4424 r = fold_sizeof_expr (t);
4425 VERIFY_CONSTANT (r);
4426 break;
4428 case COMPOUND_EXPR:
4430 /* check_return_expr sometimes wraps a TARGET_EXPR in a
4431 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
4432 introduced by build_call_a. */
4433 tree op0 = TREE_OPERAND (t, 0);
4434 tree op1 = TREE_OPERAND (t, 1);
4435 STRIP_NOPS (op1);
4436 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
4437 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
4438 r = cxx_eval_constant_expression (ctx, op0,
4439 lval, non_constant_p, overflow_p,
4440 jump_target);
4441 else
4443 /* Check that the LHS is constant and then discard it. */
4444 cxx_eval_constant_expression (ctx, op0,
4445 true, non_constant_p, overflow_p,
4446 jump_target);
4447 if (*non_constant_p)
4448 return t;
4449 op1 = TREE_OPERAND (t, 1);
4450 r = cxx_eval_constant_expression (ctx, op1,
4451 lval, non_constant_p, overflow_p,
4452 jump_target);
4455 break;
4457 case POINTER_PLUS_EXPR:
4458 case POINTER_DIFF_EXPR:
4459 case PLUS_EXPR:
4460 case MINUS_EXPR:
4461 case MULT_EXPR:
4462 case TRUNC_DIV_EXPR:
4463 case CEIL_DIV_EXPR:
4464 case FLOOR_DIV_EXPR:
4465 case ROUND_DIV_EXPR:
4466 case TRUNC_MOD_EXPR:
4467 case CEIL_MOD_EXPR:
4468 case ROUND_MOD_EXPR:
4469 case RDIV_EXPR:
4470 case EXACT_DIV_EXPR:
4471 case MIN_EXPR:
4472 case MAX_EXPR:
4473 case LSHIFT_EXPR:
4474 case RSHIFT_EXPR:
4475 case LROTATE_EXPR:
4476 case RROTATE_EXPR:
4477 case BIT_IOR_EXPR:
4478 case BIT_XOR_EXPR:
4479 case BIT_AND_EXPR:
4480 case TRUTH_XOR_EXPR:
4481 case LT_EXPR:
4482 case LE_EXPR:
4483 case GT_EXPR:
4484 case GE_EXPR:
4485 case EQ_EXPR:
4486 case NE_EXPR:
4487 case UNORDERED_EXPR:
4488 case ORDERED_EXPR:
4489 case UNLT_EXPR:
4490 case UNLE_EXPR:
4491 case UNGT_EXPR:
4492 case UNGE_EXPR:
4493 case UNEQ_EXPR:
4494 case LTGT_EXPR:
4495 case RANGE_EXPR:
4496 case COMPLEX_EXPR:
4497 r = cxx_eval_binary_expression (ctx, t, lval,
4498 non_constant_p, overflow_p);
4499 break;
4501 /* fold can introduce non-IF versions of these; still treat them as
4502 short-circuiting. */
4503 case TRUTH_AND_EXPR:
4504 case TRUTH_ANDIF_EXPR:
4505 r = cxx_eval_logical_expression (ctx, t, boolean_false_node,
4506 boolean_true_node,
4507 lval,
4508 non_constant_p, overflow_p);
4509 break;
4511 case TRUTH_OR_EXPR:
4512 case TRUTH_ORIF_EXPR:
4513 r = cxx_eval_logical_expression (ctx, t, boolean_true_node,
4514 boolean_false_node,
4515 lval,
4516 non_constant_p, overflow_p);
4517 break;
4519 case ARRAY_REF:
4520 r = cxx_eval_array_reference (ctx, t, lval,
4521 non_constant_p, overflow_p);
4522 break;
4524 case COMPONENT_REF:
4525 if (is_overloaded_fn (t))
4527 /* We can only get here in checking mode via
4528 build_non_dependent_expr, because any expression that
4529 calls or takes the address of the function will have
4530 pulled a FUNCTION_DECL out of the COMPONENT_REF. */
4531 gcc_checking_assert (ctx->quiet || errorcount);
4532 *non_constant_p = true;
4533 return t;
4535 r = cxx_eval_component_reference (ctx, t, lval,
4536 non_constant_p, overflow_p);
4537 break;
4539 case BIT_FIELD_REF:
4540 r = cxx_eval_bit_field_ref (ctx, t, lval,
4541 non_constant_p, overflow_p);
4542 break;
4544 case COND_EXPR:
4545 if (jump_target && *jump_target)
4547 /* When jumping to a label, the label might be either in the
4548 then or else blocks, so process then block first in skipping
4549 mode first, and if we are still in the skipping mode at its end,
4550 process the else block too. */
4551 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
4552 lval, non_constant_p, overflow_p,
4553 jump_target);
4554 if (*jump_target)
4555 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2),
4556 lval, non_constant_p, overflow_p,
4557 jump_target);
4558 break;
4560 r = cxx_eval_conditional_expression (ctx, t, lval,
4561 non_constant_p, overflow_p,
4562 jump_target);
4563 break;
4564 case VEC_COND_EXPR:
4565 r = cxx_eval_vector_conditional_expression (ctx, t, non_constant_p,
4566 overflow_p);
4567 break;
4569 case CONSTRUCTOR:
4570 if (TREE_CONSTANT (t))
4572 /* Don't re-process a constant CONSTRUCTOR, but do fold it to
4573 VECTOR_CST if applicable. */
4574 verify_constructor_flags (t);
4575 if (TREE_CONSTANT (t))
4576 return fold (t);
4578 r = cxx_eval_bare_aggregate (ctx, t, lval,
4579 non_constant_p, overflow_p);
4580 break;
4582 case VEC_INIT_EXPR:
4583 /* We can get this in a defaulted constructor for a class with a
4584 non-static data member of array type. Either the initializer will
4585 be NULL, meaning default-initialization, or it will be an lvalue
4586 or xvalue of the same type, meaning direct-initialization from the
4587 corresponding member. */
4588 r = cxx_eval_vec_init (ctx, t, lval,
4589 non_constant_p, overflow_p);
4590 break;
4592 case VEC_PERM_EXPR:
4593 r = cxx_eval_trinary_expression (ctx, t, lval,
4594 non_constant_p, overflow_p);
4595 break;
4597 case NOP_EXPR:
4598 if (REINTERPRET_CAST_P (t))
4600 if (!ctx->quiet)
4601 error_at (cp_expr_loc_or_loc (t, input_location),
4602 "a reinterpret_cast is not a constant expression");
4603 *non_constant_p = true;
4604 return t;
4606 /* FALLTHROUGH. */
4607 case CONVERT_EXPR:
4608 case VIEW_CONVERT_EXPR:
4609 case UNARY_PLUS_EXPR:
4611 tree oldop = TREE_OPERAND (t, 0);
4613 tree op = cxx_eval_constant_expression (ctx, oldop,
4614 lval,
4615 non_constant_p, overflow_p);
4616 if (*non_constant_p)
4617 return t;
4618 tree type = TREE_TYPE (t);
4619 if (TREE_CODE (op) == PTRMEM_CST
4620 && !TYPE_PTRMEM_P (type))
4621 op = cplus_expand_constant (op);
4623 if (TREE_CODE (op) == PTRMEM_CST && tcode == NOP_EXPR)
4625 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (op))
4626 && !can_convert_qual (type, op))
4627 op = cplus_expand_constant (op);
4628 return cp_fold_convert (type, op);
4631 if (INDIRECT_TYPE_P (type) && TREE_CODE (op) == INTEGER_CST)
4633 if (integer_zerop (op))
4635 if (TYPE_REF_P (type))
4637 if (!ctx->quiet)
4638 error_at (cp_expr_loc_or_loc (t, input_location),
4639 "dereferencing a null pointer");
4640 *non_constant_p = true;
4641 return t;
4643 else if (TYPE_PTR_P (TREE_TYPE (op)))
4645 tree from = TREE_TYPE (op);
4647 if (!can_convert (type, from, tf_none))
4649 if (!ctx->quiet)
4650 error_at (cp_expr_loc_or_loc (t, input_location),
4651 "conversion of %qT null pointer to %qT "
4652 "is not a constant expression",
4653 from, type);
4654 *non_constant_p = true;
4655 return t;
4659 else
4661 /* This detects for example:
4662 reinterpret_cast<void*>(sizeof 0)
4664 if (!ctx->quiet)
4665 error_at (cp_expr_loc_or_loc (t, input_location),
4666 "%<reinterpret_cast<%T>(%E)%> is not "
4667 "a constant expression",
4668 type, op);
4669 *non_constant_p = true;
4670 return t;
4674 if (op == oldop && tcode != UNARY_PLUS_EXPR)
4675 /* We didn't fold at the top so we could check for ptr-int
4676 conversion. */
4677 return fold (t);
4679 if (tcode == UNARY_PLUS_EXPR)
4680 r = fold_convert (TREE_TYPE (t), op);
4681 else
4682 r = fold_build1 (tcode, type, op);
4684 /* Conversion of an out-of-range value has implementation-defined
4685 behavior; the language considers it different from arithmetic
4686 overflow, which is undefined. */
4687 if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op))
4688 TREE_OVERFLOW (r) = false;
4690 break;
4692 case EMPTY_CLASS_EXPR:
4693 /* This is good enough for a function argument that might not get
4694 used, and they can't do anything with it, so just return it. */
4695 return t;
4697 case STATEMENT_LIST:
4698 new_ctx = *ctx;
4699 new_ctx.ctor = new_ctx.object = NULL_TREE;
4700 return cxx_eval_statement_list (&new_ctx, t,
4701 non_constant_p, overflow_p, jump_target);
4703 case BIND_EXPR:
4704 return cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t),
4705 lval,
4706 non_constant_p, overflow_p,
4707 jump_target);
4709 case PREINCREMENT_EXPR:
4710 case POSTINCREMENT_EXPR:
4711 case PREDECREMENT_EXPR:
4712 case POSTDECREMENT_EXPR:
4713 return cxx_eval_increment_expression (ctx, t,
4714 lval, non_constant_p, overflow_p);
4716 case LAMBDA_EXPR:
4717 case NEW_EXPR:
4718 case VEC_NEW_EXPR:
4719 case DELETE_EXPR:
4720 case VEC_DELETE_EXPR:
4721 case THROW_EXPR:
4722 case MODOP_EXPR:
4723 /* GCC internal stuff. */
4724 case VA_ARG_EXPR:
4725 case OBJ_TYPE_REF:
4726 case NON_DEPENDENT_EXPR:
4727 case BASELINK:
4728 case OFFSET_REF:
4729 if (!ctx->quiet)
4730 error_at (cp_expr_loc_or_loc (t, input_location),
4731 "expression %qE is not a constant expression", t);
4732 *non_constant_p = true;
4733 break;
4735 case PLACEHOLDER_EXPR:
4736 /* Use of the value or address of the current object. */
4737 if (tree ctor = lookup_placeholder (ctx, lval, TREE_TYPE (t)))
4738 return cxx_eval_constant_expression (ctx, ctor, lval,
4739 non_constant_p, overflow_p);
4740 /* A placeholder without a referent. We can get here when
4741 checking whether NSDMIs are noexcept, or in massage_init_elt;
4742 just say it's non-constant for now. */
4743 gcc_assert (ctx->quiet);
4744 *non_constant_p = true;
4745 break;
4747 case EXIT_EXPR:
4749 tree cond = TREE_OPERAND (t, 0);
4750 cond = cxx_eval_constant_expression (ctx, cond, /*lval*/false,
4751 non_constant_p, overflow_p);
4752 VERIFY_CONSTANT (cond);
4753 if (integer_nonzerop (cond))
4754 *jump_target = t;
4756 break;
4758 case GOTO_EXPR:
4759 *jump_target = TREE_OPERAND (t, 0);
4760 gcc_assert (breaks (jump_target) || continues (jump_target)
4761 /* Allow for jumping to a cdtor_label. */
4762 || returns (jump_target));
4763 break;
4765 case LOOP_EXPR:
4766 cxx_eval_loop_expr (ctx, t,
4767 non_constant_p, overflow_p, jump_target);
4768 break;
4770 case SWITCH_EXPR:
4771 cxx_eval_switch_expr (ctx, t,
4772 non_constant_p, overflow_p, jump_target);
4773 break;
4775 case REQUIRES_EXPR:
4776 /* It's possible to get a requires-expression in a constant
4777 expression. For example:
4779 template<typename T> concept bool C() {
4780 return requires (T t) { t; };
4783 template<typename T> requires !C<T>() void f(T);
4785 Normalization leaves f with the associated constraint
4786 '!requires (T t) { ... }' which is not transformed into
4787 a constraint. */
4788 if (!processing_template_decl)
4789 return evaluate_constraint_expression (t, NULL_TREE);
4790 else
4791 *non_constant_p = true;
4792 return t;
4794 case ANNOTATE_EXPR:
4795 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
4796 lval,
4797 non_constant_p, overflow_p,
4798 jump_target);
4799 break;
4801 case USING_STMT:
4802 r = void_node;
4803 break;
4805 default:
4806 if (STATEMENT_CODE_P (TREE_CODE (t)))
4808 /* This function doesn't know how to deal with pre-genericize
4809 statements; this can only happen with statement-expressions,
4810 so for now just fail. */
4811 if (!ctx->quiet)
4812 error_at (EXPR_LOCATION (t),
4813 "statement is not a constant expression");
4815 else
4816 internal_error ("unexpected expression %qE of kind %s", t,
4817 get_tree_code_name (TREE_CODE (t)));
4818 *non_constant_p = true;
4819 break;
4822 if (r == error_mark_node)
4823 *non_constant_p = true;
4825 if (*non_constant_p)
4826 return t;
4827 else
4828 return r;
4831 /* P0859: A function is needed for constant evaluation if it is a constexpr
4832 function that is named by an expression ([basic.def.odr]) that is
4833 potentially constant evaluated.
4835 So we need to instantiate any constexpr functions mentioned by the
4836 expression even if the definition isn't needed for evaluating the
4837 expression. */
4839 static tree
4840 instantiate_cx_fn_r (tree *tp, int *walk_subtrees, void */*data*/)
4842 if (TREE_CODE (*tp) == FUNCTION_DECL
4843 && DECL_DECLARED_CONSTEXPR_P (*tp)
4844 && !DECL_INITIAL (*tp)
4845 && !trivial_fn_p (*tp)
4846 && DECL_TEMPLOID_INSTANTIATION (*tp))
4848 ++function_depth;
4849 instantiate_decl (*tp, /*defer_ok*/false, /*expl_inst*/false);
4850 --function_depth;
4852 else if (TREE_CODE (*tp) == CALL_EXPR
4853 || TREE_CODE (*tp) == AGGR_INIT_EXPR)
4855 if (EXPR_HAS_LOCATION (*tp))
4856 input_location = EXPR_LOCATION (*tp);
4859 if (!EXPR_P (*tp))
4860 *walk_subtrees = 0;
4862 return NULL_TREE;
4864 static void
4865 instantiate_constexpr_fns (tree t)
4867 location_t loc = input_location;
4868 cp_walk_tree_without_duplicates (&t, instantiate_cx_fn_r, NULL);
4869 input_location = loc;
4872 static tree
4873 cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant,
4874 bool strict = true, tree object = NULL_TREE)
4876 auto_timevar time (TV_CONSTEXPR);
4878 bool non_constant_p = false;
4879 bool overflow_p = false;
4880 hash_map<tree,tree> map;
4882 constexpr_ctx ctx = { NULL, &map, NULL, NULL, NULL, NULL,
4883 allow_non_constant, strict };
4885 tree type = initialized_type (t);
4886 tree r = t;
4887 if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
4889 /* In C++14 an NSDMI can participate in aggregate initialization,
4890 and can refer to the address of the object being initialized, so
4891 we need to pass in the relevant VAR_DECL if we want to do the
4892 evaluation in a single pass. The evaluation will dynamically
4893 update ctx.values for the VAR_DECL. We use the same strategy
4894 for C++11 constexpr constructors that refer to the object being
4895 initialized. */
4896 ctx.ctor = build_constructor (type, NULL);
4897 CONSTRUCTOR_NO_CLEARING (ctx.ctor) = true;
4898 if (!object)
4900 if (TREE_CODE (t) == TARGET_EXPR)
4901 object = TARGET_EXPR_SLOT (t);
4902 else if (TREE_CODE (t) == AGGR_INIT_EXPR)
4903 object = AGGR_INIT_EXPR_SLOT (t);
4905 ctx.object = object;
4906 if (object)
4907 gcc_assert (same_type_ignoring_top_level_qualifiers_p
4908 (type, TREE_TYPE (object)));
4909 if (object && DECL_P (object))
4910 map.put (object, ctx.ctor);
4911 if (TREE_CODE (r) == TARGET_EXPR)
4912 /* Avoid creating another CONSTRUCTOR when we expand the
4913 TARGET_EXPR. */
4914 r = TARGET_EXPR_INITIAL (r);
4917 instantiate_constexpr_fns (r);
4918 r = cxx_eval_constant_expression (&ctx, r,
4919 false, &non_constant_p, &overflow_p);
4921 verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p);
4923 /* Mutable logic is a bit tricky: we want to allow initialization of
4924 constexpr variables with mutable members, but we can't copy those
4925 members to another constexpr variable. */
4926 if (TREE_CODE (r) == CONSTRUCTOR
4927 && CONSTRUCTOR_MUTABLE_POISON (r))
4929 if (!allow_non_constant)
4930 error ("%qE is not a constant expression because it refers to "
4931 "mutable subobjects of %qT", t, type);
4932 non_constant_p = true;
4935 if (TREE_CODE (r) == CONSTRUCTOR
4936 && CONSTRUCTOR_NO_CLEARING (r))
4938 if (!allow_non_constant)
4939 error ("%qE is not a constant expression because it refers to "
4940 "an incompletely initialized variable", t);
4941 TREE_CONSTANT (r) = false;
4942 non_constant_p = true;
4945 /* Technically we should check this for all subexpressions, but that
4946 runs into problems with our internal representation of pointer
4947 subtraction and the 5.19 rules are still in flux. */
4948 if (CONVERT_EXPR_CODE_P (TREE_CODE (r))
4949 && ARITHMETIC_TYPE_P (TREE_TYPE (r))
4950 && TREE_CODE (TREE_OPERAND (r, 0)) == ADDR_EXPR)
4952 if (!allow_non_constant)
4953 error ("conversion from pointer type %qT "
4954 "to arithmetic type %qT in a constant expression",
4955 TREE_TYPE (TREE_OPERAND (r, 0)), TREE_TYPE (r));
4956 non_constant_p = true;
4959 if (!non_constant_p && overflow_p)
4960 non_constant_p = true;
4962 /* Unshare the result unless it's a CONSTRUCTOR in which case it's already
4963 unshared. */
4964 bool should_unshare = true;
4965 if (r == t || TREE_CODE (r) == CONSTRUCTOR)
4966 should_unshare = false;
4968 if (non_constant_p && !allow_non_constant)
4969 return error_mark_node;
4970 else if (non_constant_p && TREE_CONSTANT (r))
4972 /* This isn't actually constant, so unset TREE_CONSTANT.
4973 Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires
4974 it to be set if it is invariant address, even when it is not
4975 a valid C++ constant expression. Wrap it with a NOP_EXPR
4976 instead. */
4977 if (EXPR_P (r) && TREE_CODE (r) != ADDR_EXPR)
4978 r = copy_node (r);
4979 else if (TREE_CODE (r) == CONSTRUCTOR)
4980 r = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (r), r);
4981 else
4982 r = build_nop (TREE_TYPE (r), r);
4983 TREE_CONSTANT (r) = false;
4985 else if (non_constant_p || r == t)
4986 return t;
4988 if (should_unshare)
4989 r = unshare_expr (r);
4991 if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
4993 if (TREE_CODE (t) == TARGET_EXPR
4994 && TARGET_EXPR_INITIAL (t) == r)
4995 return t;
4996 else
4998 r = get_target_expr (r);
4999 TREE_CONSTANT (r) = true;
5000 return r;
5003 else
5004 return r;
5007 /* Returns true if T is a valid subexpression of a constant expression,
5008 even if it isn't itself a constant expression. */
5010 bool
5011 is_sub_constant_expr (tree t)
5013 bool non_constant_p = false;
5014 bool overflow_p = false;
5015 hash_map <tree, tree> map;
5017 constexpr_ctx ctx = { NULL, &map, NULL, NULL, NULL, NULL, true, true };
5019 instantiate_constexpr_fns (t);
5020 cxx_eval_constant_expression (&ctx, t, false, &non_constant_p,
5021 &overflow_p);
5022 return !non_constant_p && !overflow_p;
5025 /* If T represents a constant expression returns its reduced value.
5026 Otherwise return error_mark_node. If T is dependent, then
5027 return NULL. */
5029 tree
5030 cxx_constant_value (tree t, tree decl)
5032 return cxx_eval_outermost_constant_expr (t, false, true, decl);
5035 /* Helper routine for fold_simple function. Either return simplified
5036 expression T, otherwise NULL_TREE.
5037 In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold
5038 even if we are within template-declaration. So be careful on call, as in
5039 such case types can be undefined. */
5041 static tree
5042 fold_simple_1 (tree t)
5044 tree op1;
5045 enum tree_code code = TREE_CODE (t);
5047 switch (code)
5049 case INTEGER_CST:
5050 case REAL_CST:
5051 case VECTOR_CST:
5052 case FIXED_CST:
5053 case COMPLEX_CST:
5054 return t;
5056 case SIZEOF_EXPR:
5057 return fold_sizeof_expr (t);
5059 case ABS_EXPR:
5060 case ABSU_EXPR:
5061 case CONJ_EXPR:
5062 case REALPART_EXPR:
5063 case IMAGPART_EXPR:
5064 case NEGATE_EXPR:
5065 case BIT_NOT_EXPR:
5066 case TRUTH_NOT_EXPR:
5067 case NOP_EXPR:
5068 case VIEW_CONVERT_EXPR:
5069 case CONVERT_EXPR:
5070 case FLOAT_EXPR:
5071 case FIX_TRUNC_EXPR:
5072 case FIXED_CONVERT_EXPR:
5073 case ADDR_SPACE_CONVERT_EXPR:
5075 op1 = TREE_OPERAND (t, 0);
5077 t = const_unop (code, TREE_TYPE (t), op1);
5078 if (!t)
5079 return NULL_TREE;
5081 if (CONVERT_EXPR_CODE_P (code)
5082 && TREE_OVERFLOW_P (t) && !TREE_OVERFLOW_P (op1))
5083 TREE_OVERFLOW (t) = false;
5084 return t;
5086 default:
5087 return NULL_TREE;
5091 /* If T is a simple constant expression, returns its simplified value.
5092 Otherwise returns T. In contrast to maybe_constant_value we
5093 simplify only few operations on constant-expressions, and we don't
5094 try to simplify constexpressions. */
5096 tree
5097 fold_simple (tree t)
5099 if (processing_template_decl)
5100 return t;
5102 tree r = fold_simple_1 (t);
5103 if (r)
5104 return r;
5106 return t;
5109 /* If T is a constant expression, returns its reduced value.
5110 Otherwise, if T does not have TREE_CONSTANT set, returns T.
5111 Otherwise, returns a version of T without TREE_CONSTANT. */
5113 static GTY((deletable)) hash_map<tree, tree> *cv_cache;
5115 tree
5116 maybe_constant_value (tree t, tree decl)
5118 tree r;
5120 if (!is_nondependent_constant_expression (t))
5122 if (TREE_OVERFLOW_P (t))
5124 t = build_nop (TREE_TYPE (t), t);
5125 TREE_CONSTANT (t) = false;
5127 return t;
5129 else if (CONSTANT_CLASS_P (t))
5130 /* No caching or evaluation needed. */
5131 return t;
5133 if (cv_cache == NULL)
5134 cv_cache = hash_map<tree, tree>::create_ggc (101);
5135 if (tree *cached = cv_cache->get (t))
5136 return *cached;
5138 r = cxx_eval_outermost_constant_expr (t, true, true, decl);
5139 gcc_checking_assert (r == t
5140 || CONVERT_EXPR_P (t)
5141 || TREE_CODE (t) == VIEW_CONVERT_EXPR
5142 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
5143 || !cp_tree_equal (r, t));
5144 cv_cache->put (t, r);
5145 return r;
5148 /* Dispose of the whole CV_CACHE. */
5150 static void
5151 clear_cv_cache (void)
5153 if (cv_cache != NULL)
5154 cv_cache->empty ();
5157 /* Dispose of the whole CV_CACHE and FOLD_CACHE. */
5159 void
5160 clear_cv_and_fold_caches (void)
5162 clear_cv_cache ();
5163 clear_fold_cache ();
5166 /* Like maybe_constant_value but first fully instantiate the argument.
5168 Note: this is equivalent to instantiate_non_dependent_expr_sfinae
5169 (t, tf_none) followed by maybe_constant_value but is more efficient,
5170 because calls instantiation_dependent_expression_p and
5171 potential_constant_expression at most once. */
5173 tree
5174 fold_non_dependent_expr (tree t)
5176 if (t == NULL_TREE)
5177 return NULL_TREE;
5179 /* If we're in a template, but T isn't value dependent, simplify
5180 it. We're supposed to treat:
5182 template <typename T> void f(T[1 + 1]);
5183 template <typename T> void f(T[2]);
5185 as two declarations of the same function, for example. */
5186 if (processing_template_decl)
5188 if (is_nondependent_constant_expression (t))
5190 processing_template_decl_sentinel s;
5191 t = instantiate_non_dependent_expr_internal (t, tf_none);
5193 if (type_unknown_p (t)
5194 || BRACE_ENCLOSED_INITIALIZER_P (t))
5196 if (TREE_OVERFLOW_P (t))
5198 t = build_nop (TREE_TYPE (t), t);
5199 TREE_CONSTANT (t) = false;
5201 return t;
5204 tree r = cxx_eval_outermost_constant_expr (t, true, true, NULL_TREE);
5205 /* cp_tree_equal looks through NOPs, so allow them. */
5206 gcc_checking_assert (r == t
5207 || CONVERT_EXPR_P (t)
5208 || TREE_CODE (t) == VIEW_CONVERT_EXPR
5209 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
5210 || !cp_tree_equal (r, t));
5211 return r;
5213 else if (TREE_OVERFLOW_P (t))
5215 t = build_nop (TREE_TYPE (t), t);
5216 TREE_CONSTANT (t) = false;
5218 return t;
5221 return maybe_constant_value (t);
5224 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
5225 than wrapped in a TARGET_EXPR. */
5227 static tree
5228 maybe_constant_init_1 (tree t, tree decl, bool allow_non_constant)
5230 if (!t)
5231 return t;
5232 if (TREE_CODE (t) == EXPR_STMT)
5233 t = TREE_OPERAND (t, 0);
5234 if (TREE_CODE (t) == CONVERT_EXPR
5235 && VOID_TYPE_P (TREE_TYPE (t)))
5236 t = TREE_OPERAND (t, 0);
5237 if (TREE_CODE (t) == INIT_EXPR)
5238 t = TREE_OPERAND (t, 1);
5239 if (TREE_CODE (t) == TARGET_EXPR)
5240 t = TARGET_EXPR_INITIAL (t);
5241 if (!is_nondependent_static_init_expression (t))
5242 /* Don't try to evaluate it. */;
5243 else if (CONSTANT_CLASS_P (t) && allow_non_constant)
5244 /* No evaluation needed. */;
5245 else
5246 t = cxx_eval_outermost_constant_expr (t, allow_non_constant, false, decl);
5247 if (TREE_CODE (t) == TARGET_EXPR)
5249 tree init = TARGET_EXPR_INITIAL (t);
5250 if (TREE_CODE (init) == CONSTRUCTOR)
5251 t = init;
5253 return t;
5256 /* Wrapper for maybe_constant_init_1 which permits non constants. */
5258 tree
5259 maybe_constant_init (tree t, tree decl)
5261 return maybe_constant_init_1 (t, decl, true);
5264 /* Wrapper for maybe_constant_init_1 which does not permit non constants. */
5266 tree
5267 cxx_constant_init (tree t, tree decl)
5269 return maybe_constant_init_1 (t, decl, false);
5272 #if 0
5273 /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
5274 /* Return true if the object referred to by REF has automatic or thread
5275 local storage. */
5277 enum { ck_ok, ck_bad, ck_unknown };
5278 static int
5279 check_automatic_or_tls (tree ref)
5281 machine_mode mode;
5282 poly_int64 bitsize, bitpos;
5283 tree offset;
5284 int volatilep = 0, unsignedp = 0;
5285 tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset,
5286 &mode, &unsignedp, &volatilep, false);
5287 duration_kind dk;
5289 /* If there isn't a decl in the middle, we don't know the linkage here,
5290 and this isn't a constant expression anyway. */
5291 if (!DECL_P (decl))
5292 return ck_unknown;
5293 dk = decl_storage_duration (decl);
5294 return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok;
5296 #endif
5298 /* Return true if T denotes a potentially constant expression. Issue
5299 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
5300 an lvalue-rvalue conversion is implied. If NOW is true, we want to
5301 consider the expression in the current context, independent of constexpr
5302 substitution.
5304 C++0x [expr.const] used to say
5306 6 An expression is a potential constant expression if it is
5307 a constant expression where all occurrences of function
5308 parameters are replaced by arbitrary constant expressions
5309 of the appropriate type.
5311 2 A conditional expression is a constant expression unless it
5312 involves one of the following as a potentially evaluated
5313 subexpression (3.2), but subexpressions of logical AND (5.14),
5314 logical OR (5.15), and conditional (5.16) operations that are
5315 not evaluated are not considered. */
5317 static bool
5318 potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
5319 tsubst_flags_t flags)
5321 #define RECUR(T,RV) \
5322 potential_constant_expression_1 ((T), (RV), strict, now, flags)
5324 enum { any = false, rval = true };
5325 int i;
5326 tree tmp;
5328 if (t == error_mark_node)
5329 return false;
5330 if (t == NULL_TREE)
5331 return true;
5332 location_t loc = cp_expr_loc_or_loc (t, input_location);
5333 if (TREE_THIS_VOLATILE (t) && !DECL_P (t))
5335 if (flags & tf_error)
5336 error_at (loc, "expression %qE has side-effects", t);
5337 return false;
5339 if (CONSTANT_CLASS_P (t))
5340 return true;
5341 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED)
5342 && TREE_TYPE (t) == error_mark_node)
5343 return false;
5345 switch (TREE_CODE (t))
5347 case FUNCTION_DECL:
5348 case BASELINK:
5349 case TEMPLATE_DECL:
5350 case OVERLOAD:
5351 case TEMPLATE_ID_EXPR:
5352 case LABEL_DECL:
5353 case LABEL_EXPR:
5354 case CASE_LABEL_EXPR:
5355 case CONST_DECL:
5356 case SIZEOF_EXPR:
5357 case ALIGNOF_EXPR:
5358 case OFFSETOF_EXPR:
5359 case NOEXCEPT_EXPR:
5360 case TEMPLATE_PARM_INDEX:
5361 case TRAIT_EXPR:
5362 case IDENTIFIER_NODE:
5363 case USERDEF_LITERAL:
5364 /* We can see a FIELD_DECL in a pointer-to-member expression. */
5365 case FIELD_DECL:
5366 case RESULT_DECL:
5367 case USING_DECL:
5368 case USING_STMT:
5369 case PLACEHOLDER_EXPR:
5370 case BREAK_STMT:
5371 case CONTINUE_STMT:
5372 case REQUIRES_EXPR:
5373 case STATIC_ASSERT:
5374 case DEBUG_BEGIN_STMT:
5375 return true;
5377 case PARM_DECL:
5378 if (now)
5380 if (flags & tf_error)
5381 error ("%qE is not a constant expression", t);
5382 return false;
5384 return true;
5386 case AGGR_INIT_EXPR:
5387 case CALL_EXPR:
5388 /* -- an invocation of a function other than a constexpr function
5389 or a constexpr constructor. */
5391 tree fun = get_function_named_in_call (t);
5392 const int nargs = call_expr_nargs (t);
5393 i = 0;
5395 if (fun == NULL_TREE)
5397 /* Reset to allow the function to continue past the end
5398 of the block below. Otherwise return early. */
5399 bool bail = true;
5401 if (TREE_CODE (t) == CALL_EXPR
5402 && CALL_EXPR_FN (t) == NULL_TREE)
5403 switch (CALL_EXPR_IFN (t))
5405 /* These should be ignored, they are optimized away from
5406 constexpr functions. */
5407 case IFN_UBSAN_NULL:
5408 case IFN_UBSAN_BOUNDS:
5409 case IFN_UBSAN_VPTR:
5410 case IFN_FALLTHROUGH:
5411 return true;
5413 case IFN_ADD_OVERFLOW:
5414 case IFN_SUB_OVERFLOW:
5415 case IFN_MUL_OVERFLOW:
5416 case IFN_LAUNDER:
5417 bail = false;
5419 default:
5420 break;
5423 if (bail)
5425 /* fold_call_expr can't do anything with IFN calls. */
5426 if (flags & tf_error)
5427 error_at (loc, "call to internal function %qE", t);
5428 return false;
5432 if (fun && is_overloaded_fn (fun))
5434 if (TREE_CODE (fun) == FUNCTION_DECL)
5436 if (builtin_valid_in_constant_expr_p (fun))
5437 return true;
5438 if (!DECL_DECLARED_CONSTEXPR_P (fun)
5439 /* Allow any built-in function; if the expansion
5440 isn't constant, we'll deal with that then. */
5441 && !is_builtin_fn (fun))
5443 if (flags & tf_error)
5445 error_at (loc, "call to non-%<constexpr%> function %qD",
5446 fun);
5447 explain_invalid_constexpr_fn (fun);
5449 return false;
5451 /* A call to a non-static member function takes the address
5452 of the object as the first argument. But in a constant
5453 expression the address will be folded away, so look
5454 through it now. */
5455 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
5456 && !DECL_CONSTRUCTOR_P (fun))
5458 tree x = get_nth_callarg (t, 0);
5459 if (is_this_parameter (x))
5460 return true;
5461 /* Don't require an immediately constant value, as
5462 constexpr substitution might not use the value. */
5463 bool sub_now = false;
5464 if (!potential_constant_expression_1 (x, rval, strict,
5465 sub_now, flags))
5466 return false;
5467 i = 1;
5470 else
5472 if (!RECUR (fun, true))
5473 return false;
5474 fun = get_first_fn (fun);
5476 /* Skip initial arguments to base constructors. */
5477 if (DECL_BASE_CONSTRUCTOR_P (fun))
5478 i = num_artificial_parms_for (fun);
5479 fun = DECL_ORIGIN (fun);
5481 else if (fun)
5483 if (RECUR (fun, rval))
5484 /* Might end up being a constant function pointer. */;
5485 else
5486 return false;
5488 for (; i < nargs; ++i)
5490 tree x = get_nth_callarg (t, i);
5491 /* In a template, reference arguments haven't been converted to
5492 REFERENCE_TYPE and we might not even know if the parameter
5493 is a reference, so accept lvalue constants too. */
5494 bool rv = processing_template_decl ? any : rval;
5495 /* Don't require an immediately constant value, as constexpr
5496 substitution might not use the value of the argument. */
5497 bool sub_now = false;
5498 if (!potential_constant_expression_1 (x, rv, strict,
5499 sub_now, flags))
5500 return false;
5502 return true;
5505 case NON_LVALUE_EXPR:
5506 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
5507 -- an lvalue of integral type that refers to a non-volatile
5508 const variable or static data member initialized with
5509 constant expressions, or
5511 -- an lvalue of literal type that refers to non-volatile
5512 object defined with constexpr, or that refers to a
5513 sub-object of such an object; */
5514 return RECUR (TREE_OPERAND (t, 0), rval);
5516 case VAR_DECL:
5517 if (DECL_HAS_VALUE_EXPR_P (t))
5519 if (now && is_normal_capture_proxy (t))
5521 /* -- in a lambda-expression, a reference to this or to a
5522 variable with automatic storage duration defined outside that
5523 lambda-expression, where the reference would be an
5524 odr-use. */
5525 if (flags & tf_error)
5527 tree cap = DECL_CAPTURED_VARIABLE (t);
5528 error ("lambda capture of %qE is not a constant expression",
5529 cap);
5530 if (!want_rval && decl_constant_var_p (cap))
5531 inform (input_location, "because it is used as a glvalue");
5533 return false;
5535 return RECUR (DECL_VALUE_EXPR (t), rval);
5537 if (want_rval
5538 && !var_in_maybe_constexpr_fn (t)
5539 && !type_dependent_expression_p (t)
5540 && !decl_maybe_constant_var_p (t)
5541 && (strict
5542 || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t))
5543 || (DECL_INITIAL (t)
5544 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t)))
5545 && COMPLETE_TYPE_P (TREE_TYPE (t))
5546 && !is_really_empty_class (TREE_TYPE (t)))
5548 if (flags & tf_error)
5549 non_const_var_error (t);
5550 return false;
5552 return true;
5554 case NOP_EXPR:
5555 case CONVERT_EXPR:
5556 case VIEW_CONVERT_EXPR:
5557 /* -- a reinterpret_cast. FIXME not implemented, and this rule
5558 may change to something more specific to type-punning (DR 1312). */
5560 tree from = TREE_OPERAND (t, 0);
5561 if (INDIRECT_TYPE_P (TREE_TYPE (t))
5562 && TREE_CODE (from) == INTEGER_CST
5563 && !integer_zerop (from))
5565 if (flags & tf_error)
5566 error_at (loc, "reinterpret_cast from integer to pointer");
5567 return false;
5569 return (RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR));
5572 case ADDRESSOF_EXPR:
5573 /* This is like ADDR_EXPR, except it won't form pointer-to-member. */
5574 t = TREE_OPERAND (t, 0);
5575 goto handle_addr_expr;
5577 case ADDR_EXPR:
5578 /* -- a unary operator & that is applied to an lvalue that
5579 designates an object with thread or automatic storage
5580 duration; */
5581 t = TREE_OPERAND (t, 0);
5583 if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t))
5584 /* A pointer-to-member constant. */
5585 return true;
5587 handle_addr_expr:
5588 #if 0
5589 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
5590 any checking here, as we might dereference the pointer later. If
5591 we remove this code, also remove check_automatic_or_tls. */
5592 i = check_automatic_or_tls (t);
5593 if (i == ck_ok)
5594 return true;
5595 if (i == ck_bad)
5597 if (flags & tf_error)
5598 error ("address-of an object %qE with thread local or "
5599 "automatic storage is not a constant expression", t);
5600 return false;
5602 #endif
5603 return RECUR (t, any);
5605 case REALPART_EXPR:
5606 case IMAGPART_EXPR:
5607 case COMPONENT_REF:
5608 case BIT_FIELD_REF:
5609 case ARROW_EXPR:
5610 case OFFSET_REF:
5611 /* -- a class member access unless its postfix-expression is
5612 of literal type or of pointer to literal type. */
5613 /* This test would be redundant, as it follows from the
5614 postfix-expression being a potential constant expression. */
5615 if (type_unknown_p (t))
5616 return true;
5617 return RECUR (TREE_OPERAND (t, 0), want_rval);
5619 case EXPR_PACK_EXPANSION:
5620 return RECUR (PACK_EXPANSION_PATTERN (t), want_rval);
5622 case INDIRECT_REF:
5624 tree x = TREE_OPERAND (t, 0);
5625 STRIP_NOPS (x);
5626 if (is_this_parameter (x) && !is_capture_proxy (x))
5628 if (!var_in_maybe_constexpr_fn (x))
5630 if (flags & tf_error)
5631 error_at (loc, "use of %<this%> in a constant expression");
5632 return false;
5634 return true;
5636 return RECUR (x, rval);
5639 case STATEMENT_LIST:
5641 tree_stmt_iterator i;
5642 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
5644 if (!RECUR (tsi_stmt (i), any))
5645 return false;
5647 return true;
5649 break;
5651 case MODIFY_EXPR:
5652 if (cxx_dialect < cxx14)
5653 goto fail;
5654 if (!RECUR (TREE_OPERAND (t, 0), any))
5655 return false;
5656 if (!RECUR (TREE_OPERAND (t, 1), rval))
5657 return false;
5658 return true;
5660 case MODOP_EXPR:
5661 if (cxx_dialect < cxx14)
5662 goto fail;
5663 if (!RECUR (TREE_OPERAND (t, 0), rval))
5664 return false;
5665 if (!RECUR (TREE_OPERAND (t, 2), rval))
5666 return false;
5667 return true;
5669 case DO_STMT:
5670 if (!RECUR (DO_COND (t), rval))
5671 return false;
5672 if (!RECUR (DO_BODY (t), any))
5673 return false;
5674 return true;
5676 case FOR_STMT:
5677 if (!RECUR (FOR_INIT_STMT (t), any))
5678 return false;
5679 if (!RECUR (FOR_COND (t), rval))
5680 return false;
5681 if (!RECUR (FOR_EXPR (t), any))
5682 return false;
5683 if (!RECUR (FOR_BODY (t), any))
5684 return false;
5685 return true;
5687 case RANGE_FOR_STMT:
5688 if (!RECUR (RANGE_FOR_EXPR (t), any))
5689 return false;
5690 if (!RECUR (RANGE_FOR_BODY (t), any))
5691 return false;
5692 return true;
5694 case WHILE_STMT:
5695 if (!RECUR (WHILE_COND (t), rval))
5696 return false;
5697 if (!RECUR (WHILE_BODY (t), any))
5698 return false;
5699 return true;
5701 case SWITCH_STMT:
5702 if (!RECUR (SWITCH_STMT_COND (t), rval))
5703 return false;
5704 /* FIXME we don't check SWITCH_STMT_BODY currently, because even
5705 unreachable labels would be checked. */
5706 return true;
5708 case STMT_EXPR:
5709 return RECUR (STMT_EXPR_STMT (t), rval);
5711 case LAMBDA_EXPR:
5712 if (cxx_dialect >= cxx17)
5713 /* In C++17 lambdas can be constexpr, don't give up yet. */
5714 return true;
5715 else if (flags & tf_error)
5716 error_at (loc, "lambda-expression is not a constant expression "
5717 "before C++17");
5718 return false;
5720 case DYNAMIC_CAST_EXPR:
5721 case PSEUDO_DTOR_EXPR:
5722 case NEW_EXPR:
5723 case VEC_NEW_EXPR:
5724 case DELETE_EXPR:
5725 case VEC_DELETE_EXPR:
5726 case THROW_EXPR:
5727 case OMP_PARALLEL:
5728 case OMP_TASK:
5729 case OMP_FOR:
5730 case OMP_SIMD:
5731 case OMP_DISTRIBUTE:
5732 case OMP_TASKLOOP:
5733 case OMP_TEAMS:
5734 case OMP_TARGET_DATA:
5735 case OMP_TARGET:
5736 case OMP_SECTIONS:
5737 case OMP_ORDERED:
5738 case OMP_CRITICAL:
5739 case OMP_SINGLE:
5740 case OMP_SECTION:
5741 case OMP_MASTER:
5742 case OMP_TASKGROUP:
5743 case OMP_TARGET_UPDATE:
5744 case OMP_TARGET_ENTER_DATA:
5745 case OMP_TARGET_EXIT_DATA:
5746 case OMP_ATOMIC:
5747 case OMP_ATOMIC_READ:
5748 case OMP_ATOMIC_CAPTURE_OLD:
5749 case OMP_ATOMIC_CAPTURE_NEW:
5750 case OACC_PARALLEL:
5751 case OACC_KERNELS:
5752 case OACC_DATA:
5753 case OACC_HOST_DATA:
5754 case OACC_LOOP:
5755 case OACC_CACHE:
5756 case OACC_DECLARE:
5757 case OACC_ENTER_DATA:
5758 case OACC_EXIT_DATA:
5759 case OACC_UPDATE:
5760 /* GCC internal stuff. */
5761 case VA_ARG_EXPR:
5762 case OBJ_TYPE_REF:
5763 case TRANSACTION_EXPR:
5764 case ASM_EXPR:
5765 case AT_ENCODE_EXPR:
5766 fail:
5767 if (flags & tf_error)
5768 error_at (loc, "expression %qE is not a constant expression", t);
5769 return false;
5771 case TYPEID_EXPR:
5772 /* -- a typeid expression whose operand is of polymorphic
5773 class type; */
5775 tree e = TREE_OPERAND (t, 0);
5776 if (!TYPE_P (e) && !type_dependent_expression_p (e)
5777 && TYPE_POLYMORPHIC_P (TREE_TYPE (e)))
5779 if (flags & tf_error)
5780 error_at (loc, "typeid-expression is not a constant expression "
5781 "because %qE is of polymorphic type", e);
5782 return false;
5784 return true;
5787 case POINTER_DIFF_EXPR:
5788 case MINUS_EXPR:
5789 want_rval = true;
5790 goto binary;
5792 case LT_EXPR:
5793 case LE_EXPR:
5794 case GT_EXPR:
5795 case GE_EXPR:
5796 case EQ_EXPR:
5797 case NE_EXPR:
5798 want_rval = true;
5799 goto binary;
5801 case PREINCREMENT_EXPR:
5802 case POSTINCREMENT_EXPR:
5803 case PREDECREMENT_EXPR:
5804 case POSTDECREMENT_EXPR:
5805 if (cxx_dialect < cxx14)
5806 goto fail;
5807 goto unary;
5809 case BIT_NOT_EXPR:
5810 /* A destructor. */
5811 if (TYPE_P (TREE_OPERAND (t, 0)))
5812 return true;
5813 /* fall through. */
5815 case CONJ_EXPR:
5816 case SAVE_EXPR:
5817 case FIX_TRUNC_EXPR:
5818 case FLOAT_EXPR:
5819 case NEGATE_EXPR:
5820 case ABS_EXPR:
5821 case ABSU_EXPR:
5822 case TRUTH_NOT_EXPR:
5823 case FIXED_CONVERT_EXPR:
5824 case UNARY_PLUS_EXPR:
5825 case UNARY_LEFT_FOLD_EXPR:
5826 case UNARY_RIGHT_FOLD_EXPR:
5827 unary:
5828 return RECUR (TREE_OPERAND (t, 0), rval);
5830 case CAST_EXPR:
5831 case CONST_CAST_EXPR:
5832 case STATIC_CAST_EXPR:
5833 case REINTERPRET_CAST_EXPR:
5834 case IMPLICIT_CONV_EXPR:
5835 if (cxx_dialect < cxx11
5836 && !dependent_type_p (TREE_TYPE (t))
5837 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t)))
5838 /* In C++98, a conversion to non-integral type can't be part of a
5839 constant expression. */
5841 if (flags & tf_error)
5842 error_at (loc,
5843 "cast to non-integral type %qT in a constant expression",
5844 TREE_TYPE (t));
5845 return false;
5847 /* This might be a conversion from a class to a (potentially) literal
5848 type. Let's consider it potentially constant since the conversion
5849 might be a constexpr user-defined conversion. */
5850 else if (cxx_dialect >= cxx11
5851 && (dependent_type_p (TREE_TYPE (t))
5852 || !COMPLETE_TYPE_P (TREE_TYPE (t))
5853 || literal_type_p (TREE_TYPE (t)))
5854 && TREE_OPERAND (t, 0))
5856 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
5857 /* If this is a dependent type, it could end up being a class
5858 with conversions. */
5859 if (type == NULL_TREE || WILDCARD_TYPE_P (type))
5860 return true;
5861 /* Or a non-dependent class which has conversions. */
5862 else if (CLASS_TYPE_P (type)
5863 && (TYPE_HAS_CONVERSION (type) || dependent_scope_p (type)))
5864 return true;
5867 return (RECUR (TREE_OPERAND (t, 0),
5868 !TYPE_REF_P (TREE_TYPE (t))));
5870 case BIND_EXPR:
5871 return RECUR (BIND_EXPR_BODY (t), want_rval);
5873 case CLEANUP_POINT_EXPR:
5874 case MUST_NOT_THROW_EXPR:
5875 case TRY_CATCH_EXPR:
5876 case TRY_BLOCK:
5877 case EH_SPEC_BLOCK:
5878 case EXPR_STMT:
5879 case PAREN_EXPR:
5880 case NON_DEPENDENT_EXPR:
5881 /* For convenience. */
5882 case RETURN_EXPR:
5883 case LOOP_EXPR:
5884 case EXIT_EXPR:
5885 return RECUR (TREE_OPERAND (t, 0), want_rval);
5887 case DECL_EXPR:
5888 tmp = DECL_EXPR_DECL (t);
5889 if (VAR_P (tmp) && !DECL_ARTIFICIAL (tmp))
5891 if (TREE_STATIC (tmp))
5893 if (flags & tf_error)
5894 error_at (DECL_SOURCE_LOCATION (tmp), "%qD declared "
5895 "%<static%> in %<constexpr%> context", tmp);
5896 return false;
5898 else if (CP_DECL_THREAD_LOCAL_P (tmp))
5900 if (flags & tf_error)
5901 error_at (DECL_SOURCE_LOCATION (tmp), "%qD declared "
5902 "%<thread_local%> in %<constexpr%> context", tmp);
5903 return false;
5905 else if (!check_for_uninitialized_const_var
5906 (tmp, /*constexpr_context_p=*/true, flags))
5907 return false;
5909 return RECUR (tmp, want_rval);
5911 case TRY_FINALLY_EXPR:
5912 return (RECUR (TREE_OPERAND (t, 0), want_rval)
5913 && RECUR (TREE_OPERAND (t, 1), any));
5915 case SCOPE_REF:
5916 return RECUR (TREE_OPERAND (t, 1), want_rval);
5918 case TARGET_EXPR:
5919 if (!TARGET_EXPR_DIRECT_INIT_P (t)
5920 && !literal_type_p (TREE_TYPE (t)))
5922 if (flags & tf_error)
5924 error_at (loc, "temporary of non-literal type %qT in a "
5925 "constant expression", TREE_TYPE (t));
5926 explain_non_literal_class (TREE_TYPE (t));
5928 return false;
5930 /* FALLTHRU */
5931 case INIT_EXPR:
5932 return RECUR (TREE_OPERAND (t, 1), rval);
5934 case CONSTRUCTOR:
5936 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
5937 constructor_elt *ce;
5938 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
5939 if (!RECUR (ce->value, want_rval))
5940 return false;
5941 return true;
5944 case TREE_LIST:
5946 gcc_assert (TREE_PURPOSE (t) == NULL_TREE
5947 || DECL_P (TREE_PURPOSE (t)));
5948 if (!RECUR (TREE_VALUE (t), want_rval))
5949 return false;
5950 if (TREE_CHAIN (t) == NULL_TREE)
5951 return true;
5952 return RECUR (TREE_CHAIN (t), want_rval);
5955 case TRUNC_DIV_EXPR:
5956 case CEIL_DIV_EXPR:
5957 case FLOOR_DIV_EXPR:
5958 case ROUND_DIV_EXPR:
5959 case TRUNC_MOD_EXPR:
5960 case CEIL_MOD_EXPR:
5961 case ROUND_MOD_EXPR:
5963 tree denom = TREE_OPERAND (t, 1);
5964 if (!RECUR (denom, rval))
5965 return false;
5966 /* We can't call cxx_eval_outermost_constant_expr on an expression
5967 that hasn't been through instantiate_non_dependent_expr yet. */
5968 if (!processing_template_decl)
5969 denom = cxx_eval_outermost_constant_expr (denom, true);
5970 if (integer_zerop (denom))
5972 if (flags & tf_error)
5973 error ("division by zero is not a constant expression");
5974 return false;
5976 else
5978 want_rval = true;
5979 return RECUR (TREE_OPERAND (t, 0), want_rval);
5983 case COMPOUND_EXPR:
5985 /* check_return_expr sometimes wraps a TARGET_EXPR in a
5986 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
5987 introduced by build_call_a. */
5988 tree op0 = TREE_OPERAND (t, 0);
5989 tree op1 = TREE_OPERAND (t, 1);
5990 STRIP_NOPS (op1);
5991 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
5992 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
5993 return RECUR (op0, want_rval);
5994 else
5995 goto binary;
5998 /* If the first operand is the non-short-circuit constant, look at
5999 the second operand; otherwise we only care about the first one for
6000 potentiality. */
6001 case TRUTH_AND_EXPR:
6002 case TRUTH_ANDIF_EXPR:
6003 tmp = boolean_true_node;
6004 goto truth;
6005 case TRUTH_OR_EXPR:
6006 case TRUTH_ORIF_EXPR:
6007 tmp = boolean_false_node;
6008 truth:
6010 tree op = TREE_OPERAND (t, 0);
6011 if (!RECUR (op, rval))
6012 return false;
6013 if (!processing_template_decl)
6014 op = cxx_eval_outermost_constant_expr (op, true);
6015 if (tree_int_cst_equal (op, tmp))
6016 return RECUR (TREE_OPERAND (t, 1), rval);
6017 else
6018 return true;
6021 case PLUS_EXPR:
6022 case MULT_EXPR:
6023 case POINTER_PLUS_EXPR:
6024 case RDIV_EXPR:
6025 case EXACT_DIV_EXPR:
6026 case MIN_EXPR:
6027 case MAX_EXPR:
6028 case LSHIFT_EXPR:
6029 case RSHIFT_EXPR:
6030 case LROTATE_EXPR:
6031 case RROTATE_EXPR:
6032 case BIT_IOR_EXPR:
6033 case BIT_XOR_EXPR:
6034 case BIT_AND_EXPR:
6035 case TRUTH_XOR_EXPR:
6036 case UNORDERED_EXPR:
6037 case ORDERED_EXPR:
6038 case UNLT_EXPR:
6039 case UNLE_EXPR:
6040 case UNGT_EXPR:
6041 case UNGE_EXPR:
6042 case UNEQ_EXPR:
6043 case LTGT_EXPR:
6044 case RANGE_EXPR:
6045 case COMPLEX_EXPR:
6046 want_rval = true;
6047 /* Fall through. */
6048 case ARRAY_REF:
6049 case ARRAY_RANGE_REF:
6050 case MEMBER_REF:
6051 case DOTSTAR_EXPR:
6052 case MEM_REF:
6053 case BINARY_LEFT_FOLD_EXPR:
6054 case BINARY_RIGHT_FOLD_EXPR:
6055 binary:
6056 for (i = 0; i < 2; ++i)
6057 if (!RECUR (TREE_OPERAND (t, i), want_rval))
6058 return false;
6059 return true;
6061 case VEC_PERM_EXPR:
6062 for (i = 0; i < 3; ++i)
6063 if (!RECUR (TREE_OPERAND (t, i), true))
6064 return false;
6065 return true;
6067 case COND_EXPR:
6068 if (COND_EXPR_IS_VEC_DELETE (t))
6070 if (flags & tf_error)
6071 error_at (loc, "%<delete[]%> is not a constant expression");
6072 return false;
6074 /* Fall through. */
6075 case IF_STMT:
6076 case VEC_COND_EXPR:
6077 /* If the condition is a known constant, we know which of the legs we
6078 care about; otherwise we only require that the condition and
6079 either of the legs be potentially constant. */
6080 tmp = TREE_OPERAND (t, 0);
6081 if (!RECUR (tmp, rval))
6082 return false;
6083 if (!processing_template_decl)
6084 tmp = cxx_eval_outermost_constant_expr (tmp, true);
6085 if (integer_zerop (tmp))
6086 return RECUR (TREE_OPERAND (t, 2), want_rval);
6087 else if (TREE_CODE (tmp) == INTEGER_CST)
6088 return RECUR (TREE_OPERAND (t, 1), want_rval);
6089 for (i = 1; i < 3; ++i)
6090 if (potential_constant_expression_1 (TREE_OPERAND (t, i),
6091 want_rval, strict, now, tf_none))
6092 return true;
6093 if (flags & tf_error)
6094 error_at (loc, "expression %qE is not a constant expression", t);
6095 return false;
6097 case VEC_INIT_EXPR:
6098 if (VEC_INIT_EXPR_IS_CONSTEXPR (t))
6099 return true;
6100 if (flags & tf_error)
6102 error_at (loc, "non-constant array initialization");
6103 diagnose_non_constexpr_vec_init (t);
6105 return false;
6107 case TYPE_DECL:
6108 case TAG_DEFN:
6109 /* We can see these in statement-expressions. */
6110 return true;
6112 case CLEANUP_STMT:
6113 case EMPTY_CLASS_EXPR:
6114 case PREDICT_EXPR:
6115 return false;
6117 case GOTO_EXPR:
6119 tree *target = &TREE_OPERAND (t, 0);
6120 /* Gotos representing break and continue are OK. */
6121 if (breaks (target) || continues (target))
6122 return true;
6123 if (flags & tf_error)
6124 error_at (loc, "%<goto%> is not a constant expression");
6125 return false;
6128 case ANNOTATE_EXPR:
6129 return RECUR (TREE_OPERAND (t, 0), rval);
6131 default:
6132 if (objc_is_property_ref (t))
6133 return false;
6135 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t)));
6136 gcc_unreachable ();
6137 return false;
6139 #undef RECUR
6142 /* The main entry point to the above. */
6144 bool
6145 potential_constant_expression (tree t)
6147 return potential_constant_expression_1 (t, false, true, false, tf_none);
6150 /* As above, but require a constant rvalue. */
6152 bool
6153 potential_rvalue_constant_expression (tree t)
6155 return potential_constant_expression_1 (t, true, true, false, tf_none);
6158 /* Like above, but complain about non-constant expressions. */
6160 bool
6161 require_potential_constant_expression (tree t)
6163 return potential_constant_expression_1 (t, false, true, false,
6164 tf_warning_or_error);
6167 /* Cross product of the above. */
6169 bool
6170 require_potential_rvalue_constant_expression (tree t)
6172 return potential_constant_expression_1 (t, true, true, false,
6173 tf_warning_or_error);
6176 /* Like above, but don't consider PARM_DECL a potential_constant_expression. */
6178 bool
6179 require_rvalue_constant_expression (tree t)
6181 return potential_constant_expression_1 (t, true, true, true,
6182 tf_warning_or_error);
6185 /* Like potential_constant_expression, but don't consider possible constexpr
6186 substitution of the current function. That is, PARM_DECL qualifies under
6187 potential_constant_expression, but not here.
6189 This is basically what you can check when any actual constant values might
6190 be value-dependent. */
6192 bool
6193 is_constant_expression (tree t)
6195 return potential_constant_expression_1 (t, false, true, true, tf_none);
6198 /* Like above, but complain about non-constant expressions. */
6200 bool
6201 require_constant_expression (tree t)
6203 return potential_constant_expression_1 (t, false, true, true,
6204 tf_warning_or_error);
6207 /* Like is_constant_expression, but allow const variables that are not allowed
6208 under constexpr rules. */
6210 bool
6211 is_static_init_expression (tree t)
6213 return potential_constant_expression_1 (t, false, false, true, tf_none);
6216 /* Returns true if T is a potential constant expression that is not
6217 instantiation-dependent, and therefore a candidate for constant folding even
6218 in a template. */
6220 bool
6221 is_nondependent_constant_expression (tree t)
6223 return (!type_unknown_p (t)
6224 && !BRACE_ENCLOSED_INITIALIZER_P (t)
6225 && is_constant_expression (t)
6226 && !instantiation_dependent_expression_p (t));
6229 /* Returns true if T is a potential static initializer expression that is not
6230 instantiation-dependent. */
6232 bool
6233 is_nondependent_static_init_expression (tree t)
6235 return (!type_unknown_p (t)
6236 && !BRACE_ENCLOSED_INITIALIZER_P (t)
6237 && is_static_init_expression (t)
6238 && !instantiation_dependent_expression_p (t));
6241 /* Finalize constexpr processing after parsing. */
6243 void
6244 fini_constexpr (void)
6246 /* The contexpr call and fundef copies tables are no longer needed. */
6247 constexpr_call_table = NULL;
6248 fundef_copies_table = NULL;
6251 #include "gt-cp-constexpr.h"