Daily bump.
[official-gcc.git] / gcc / cp / constexpr.c
blobb94b346c45c1f6198165c6cb3dc332721b2508b8
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-2016 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"
35 static bool verify_constant (tree, bool, bool *, bool *);
36 #define VERIFY_CONSTANT(X) \
37 do { \
38 if (verify_constant ((X), ctx->quiet, non_constant_p, overflow_p)) \
39 return t; \
40 } while (0)
42 /* Returns true iff FUN is an instantiation of a constexpr function
43 template or a defaulted constexpr function. */
45 bool
46 is_instantiation_of_constexpr (tree fun)
48 return ((DECL_TEMPLOID_INSTANTIATION (fun)
49 && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun)))
50 || (DECL_DEFAULTED_FN (fun)
51 && DECL_DECLARED_CONSTEXPR_P (fun)));
54 /* Return true if T is a literal type. */
56 bool
57 literal_type_p (tree t)
59 if (SCALAR_TYPE_P (t)
60 || VECTOR_TYPE_P (t)
61 || TREE_CODE (t) == REFERENCE_TYPE
62 || (VOID_TYPE_P (t) && cxx_dialect >= cxx14))
63 return true;
64 if (CLASS_TYPE_P (t))
66 t = complete_type (t);
67 gcc_assert (COMPLETE_TYPE_P (t) || errorcount);
68 return CLASSTYPE_LITERAL_P (t);
70 if (TREE_CODE (t) == ARRAY_TYPE)
71 return literal_type_p (strip_array_types (t));
72 return false;
75 /* If DECL is a variable declared `constexpr', require its type
76 be literal. Return the DECL if OK, otherwise NULL. */
78 tree
79 ensure_literal_type_for_constexpr_object (tree decl)
81 tree type = TREE_TYPE (decl);
82 if (VAR_P (decl)
83 && (DECL_DECLARED_CONSTEXPR_P (decl)
84 || var_in_constexpr_fn (decl))
85 && !processing_template_decl)
87 tree stype = strip_array_types (type);
88 if (CLASS_TYPE_P (stype) && !COMPLETE_TYPE_P (complete_type (stype)))
89 /* Don't complain here, we'll complain about incompleteness
90 when we try to initialize the variable. */;
91 else if (!literal_type_p (type))
93 if (DECL_DECLARED_CONSTEXPR_P (decl))
95 error ("the type %qT of constexpr variable %qD is not literal",
96 type, decl);
97 explain_non_literal_class (type);
99 else
101 if (!DECL_TEMPLATE_INSTANTIATION (current_function_decl))
103 error ("variable %qD of non-literal type %qT in %<constexpr%> "
104 "function", decl, type);
105 explain_non_literal_class (type);
107 cp_function_chain->invalid_constexpr = true;
109 return NULL;
112 return decl;
115 /* Representation of entries in the constexpr function definition table. */
117 struct GTY((for_user)) constexpr_fundef {
118 tree decl;
119 tree body;
122 struct constexpr_fundef_hasher : ggc_ptr_hash<constexpr_fundef>
124 static hashval_t hash (constexpr_fundef *);
125 static bool equal (constexpr_fundef *, constexpr_fundef *);
128 /* This table holds all constexpr function definitions seen in
129 the current translation unit. */
131 static GTY (()) hash_table<constexpr_fundef_hasher> *constexpr_fundef_table;
133 /* Utility function used for managing the constexpr function table.
134 Return true if the entries pointed to by P and Q are for the
135 same constexpr function. */
137 inline bool
138 constexpr_fundef_hasher::equal (constexpr_fundef *lhs, constexpr_fundef *rhs)
140 return lhs->decl == rhs->decl;
143 /* Utility function used for managing the constexpr function table.
144 Return a hash value for the entry pointed to by Q. */
146 inline hashval_t
147 constexpr_fundef_hasher::hash (constexpr_fundef *fundef)
149 return DECL_UID (fundef->decl);
152 /* Return a previously saved definition of function FUN. */
154 static constexpr_fundef *
155 retrieve_constexpr_fundef (tree fun)
157 constexpr_fundef fundef = { NULL, NULL };
158 if (constexpr_fundef_table == NULL)
159 return NULL;
161 fundef.decl = fun;
162 return constexpr_fundef_table->find (&fundef);
165 /* Check whether the parameter and return types of FUN are valid for a
166 constexpr function, and complain if COMPLAIN. */
168 static bool
169 is_valid_constexpr_fn (tree fun, bool complain)
171 bool ret = true;
173 if (DECL_INHERITED_CTOR_BASE (fun)
174 && TREE_CODE (fun) == TEMPLATE_DECL)
176 ret = false;
177 if (complain)
178 error ("inherited constructor %qD is not constexpr",
179 get_inherited_ctor (fun));
181 else
183 for (tree parm = FUNCTION_FIRST_USER_PARM (fun);
184 parm != NULL_TREE; parm = TREE_CHAIN (parm))
185 if (!literal_type_p (TREE_TYPE (parm)))
187 ret = false;
188 if (complain)
190 error ("invalid type for parameter %d of constexpr "
191 "function %q+#D", DECL_PARM_INDEX (parm), fun);
192 explain_non_literal_class (TREE_TYPE (parm));
197 if (!DECL_CONSTRUCTOR_P (fun))
199 tree rettype = TREE_TYPE (TREE_TYPE (fun));
200 if (!literal_type_p (rettype))
202 ret = false;
203 if (complain)
205 error ("invalid return type %qT of constexpr function %q+D",
206 rettype, fun);
207 explain_non_literal_class (rettype);
211 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
212 && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun)))
214 ret = false;
215 if (complain)
217 error ("enclosing class of constexpr non-static member "
218 "function %q+#D is not a literal type", fun);
219 explain_non_literal_class (DECL_CONTEXT (fun));
223 else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun)))
225 ret = false;
226 if (complain)
227 error ("%q#T has virtual base classes", DECL_CONTEXT (fun));
230 return ret;
233 /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF
234 for a member of an anonymous aggregate, INIT is the initializer for that
235 member, and VEC_OUTER is the vector of constructor elements for the class
236 whose constructor we are processing. Add the initializer to the vector
237 and return true to indicate success. */
239 static bool
240 build_anon_member_initialization (tree member, tree init,
241 vec<constructor_elt, va_gc> **vec_outer)
243 /* MEMBER presents the relevant fields from the inside out, but we need
244 to build up the initializer from the outside in so that we can reuse
245 previously built CONSTRUCTORs if this is, say, the second field in an
246 anonymous struct. So we use a vec as a stack. */
247 auto_vec<tree, 2> fields;
250 fields.safe_push (TREE_OPERAND (member, 1));
251 member = TREE_OPERAND (member, 0);
253 while (ANON_AGGR_TYPE_P (TREE_TYPE (member))
254 && TREE_CODE (member) == COMPONENT_REF);
256 /* VEC has the constructor elements vector for the context of FIELD.
257 If FIELD is an anonymous aggregate, we will push inside it. */
258 vec<constructor_elt, va_gc> **vec = vec_outer;
259 tree field;
260 while (field = fields.pop(),
261 ANON_AGGR_TYPE_P (TREE_TYPE (field)))
263 tree ctor;
264 /* If there is already an outer constructor entry for the anonymous
265 aggregate FIELD, use it; otherwise, insert one. */
266 if (vec_safe_is_empty (*vec)
267 || (*vec)->last().index != field)
269 ctor = build_constructor (TREE_TYPE (field), NULL);
270 CONSTRUCTOR_APPEND_ELT (*vec, field, ctor);
272 else
273 ctor = (*vec)->last().value;
274 vec = &CONSTRUCTOR_ELTS (ctor);
277 /* Now we're at the innermost field, the one that isn't an anonymous
278 aggregate. Add its initializer to the CONSTRUCTOR and we're done. */
279 gcc_assert (fields.is_empty());
280 CONSTRUCTOR_APPEND_ELT (*vec, field, init);
282 return true;
285 /* Subroutine of build_constexpr_constructor_member_initializers.
286 The expression tree T represents a data member initialization
287 in a (constexpr) constructor definition. Build a pairing of
288 the data member with its initializer, and prepend that pair
289 to the existing initialization pair INITS. */
291 static bool
292 build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec)
294 tree member, init;
295 if (TREE_CODE (t) == CLEANUP_POINT_EXPR)
296 t = TREE_OPERAND (t, 0);
297 if (TREE_CODE (t) == EXPR_STMT)
298 t = TREE_OPERAND (t, 0);
299 if (t == error_mark_node)
300 return false;
301 if (TREE_CODE (t) == STATEMENT_LIST)
303 tree_stmt_iterator i;
304 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
306 if (! build_data_member_initialization (tsi_stmt (i), vec))
307 return false;
309 return true;
311 if (TREE_CODE (t) == CLEANUP_STMT)
313 /* We can't see a CLEANUP_STMT in a constructor for a literal class,
314 but we can in a constexpr constructor for a non-literal class. Just
315 ignore it; either all the initialization will be constant, in which
316 case the cleanup can't run, or it can't be constexpr.
317 Still recurse into CLEANUP_BODY. */
318 return build_data_member_initialization (CLEANUP_BODY (t), vec);
320 if (TREE_CODE (t) == CONVERT_EXPR)
321 t = TREE_OPERAND (t, 0);
322 if (TREE_CODE (t) == INIT_EXPR
323 /* vptr initialization shows up as a MODIFY_EXPR. In C++14 we only
324 use what this function builds for cx_check_missing_mem_inits, and
325 assignment in the ctor body doesn't count. */
326 || (cxx_dialect < cxx14 && TREE_CODE (t) == MODIFY_EXPR))
328 member = TREE_OPERAND (t, 0);
329 init = break_out_target_exprs (TREE_OPERAND (t, 1));
331 else if (TREE_CODE (t) == CALL_EXPR)
333 tree fn = get_callee_fndecl (t);
334 if (!fn || !DECL_CONSTRUCTOR_P (fn))
335 /* We're only interested in calls to subobject constructors. */
336 return true;
337 member = CALL_EXPR_ARG (t, 0);
338 /* We don't use build_cplus_new here because it complains about
339 abstract bases. Leaving the call unwrapped means that it has the
340 wrong type, but cxx_eval_constant_expression doesn't care. */
341 init = break_out_target_exprs (t);
343 else if (TREE_CODE (t) == BIND_EXPR)
344 return build_data_member_initialization (BIND_EXPR_BODY (t), vec);
345 else
346 /* Don't add anything else to the CONSTRUCTOR. */
347 return true;
348 if (INDIRECT_REF_P (member))
349 member = TREE_OPERAND (member, 0);
350 if (TREE_CODE (member) == NOP_EXPR)
352 tree op = member;
353 STRIP_NOPS (op);
354 if (TREE_CODE (op) == ADDR_EXPR)
356 gcc_assert (same_type_ignoring_top_level_qualifiers_p
357 (TREE_TYPE (TREE_TYPE (op)),
358 TREE_TYPE (TREE_TYPE (member))));
359 /* Initializing a cv-qualified member; we need to look through
360 the const_cast. */
361 member = op;
363 else if (op == current_class_ptr
364 && (same_type_ignoring_top_level_qualifiers_p
365 (TREE_TYPE (TREE_TYPE (member)),
366 current_class_type)))
367 /* Delegating constructor. */
368 member = op;
369 else
371 /* This is an initializer for an empty base; keep it for now so
372 we can check it in cxx_eval_bare_aggregate. */
373 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member))));
376 if (TREE_CODE (member) == ADDR_EXPR)
377 member = TREE_OPERAND (member, 0);
378 if (TREE_CODE (member) == COMPONENT_REF)
380 tree aggr = TREE_OPERAND (member, 0);
381 if (TREE_CODE (aggr) != COMPONENT_REF)
382 /* Normal member initialization. */
383 member = TREE_OPERAND (member, 1);
384 else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr)))
385 /* Initializing a member of an anonymous union. */
386 return build_anon_member_initialization (member, init, vec);
387 else
388 /* We're initializing a vtable pointer in a base. Leave it as
389 COMPONENT_REF so we remember the path to get to the vfield. */
390 gcc_assert (TREE_TYPE (member) == vtbl_ptr_type_node);
393 CONSTRUCTOR_APPEND_ELT (*vec, member, init);
394 return true;
397 /* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval.
398 In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a
399 BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */
401 static bool
402 check_constexpr_bind_expr_vars (tree t)
404 gcc_assert (TREE_CODE (t) == BIND_EXPR);
406 for (tree var = BIND_EXPR_VARS (t); var; var = DECL_CHAIN (var))
407 if (TREE_CODE (var) == TYPE_DECL
408 && DECL_IMPLICIT_TYPEDEF_P (var)
409 && !LAMBDA_TYPE_P (TREE_TYPE (var)))
410 return false;
411 return true;
414 /* Subroutine of check_constexpr_ctor_body. */
416 static bool
417 check_constexpr_ctor_body_1 (tree last, tree list)
419 switch (TREE_CODE (list))
421 case DECL_EXPR:
422 if (TREE_CODE (DECL_EXPR_DECL (list)) == USING_DECL)
423 return true;
424 return false;
426 case CLEANUP_POINT_EXPR:
427 return check_constexpr_ctor_body (last, TREE_OPERAND (list, 0),
428 /*complain=*/false);
430 case BIND_EXPR:
431 if (!check_constexpr_bind_expr_vars (list)
432 || !check_constexpr_ctor_body (last, BIND_EXPR_BODY (list),
433 /*complain=*/false))
434 return false;
435 return true;
437 case USING_STMT:
438 case STATIC_ASSERT:
439 return true;
441 default:
442 return false;
446 /* Make sure that there are no statements after LAST in the constructor
447 body represented by LIST. */
449 bool
450 check_constexpr_ctor_body (tree last, tree list, bool complain)
452 /* C++14 doesn't require a constexpr ctor to have an empty body. */
453 if (cxx_dialect >= cxx14)
454 return true;
456 bool ok = true;
457 if (TREE_CODE (list) == STATEMENT_LIST)
459 tree_stmt_iterator i = tsi_last (list);
460 for (; !tsi_end_p (i); tsi_prev (&i))
462 tree t = tsi_stmt (i);
463 if (t == last)
464 break;
465 if (!check_constexpr_ctor_body_1 (last, t))
467 ok = false;
468 break;
472 else if (list != last
473 && !check_constexpr_ctor_body_1 (last, list))
474 ok = false;
475 if (!ok)
477 if (complain)
478 error ("constexpr constructor does not have empty body");
479 DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false;
481 return ok;
484 /* V is a vector of constructor elements built up for the base and member
485 initializers of a constructor for TYPE. They need to be in increasing
486 offset order, which they might not be yet if TYPE has a primary base
487 which is not first in the base-clause or a vptr and at least one base
488 all of which are non-primary. */
490 static vec<constructor_elt, va_gc> *
491 sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v)
493 tree pri = CLASSTYPE_PRIMARY_BINFO (type);
494 tree field_type;
495 unsigned i;
496 constructor_elt *ce;
498 if (pri)
499 field_type = BINFO_TYPE (pri);
500 else if (TYPE_CONTAINS_VPTR_P (type))
501 field_type = vtbl_ptr_type_node;
502 else
503 return v;
505 /* Find the element for the primary base or vptr and move it to the
506 beginning of the vec. */
507 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
508 if (TREE_TYPE (ce->index) == field_type)
509 break;
511 if (i > 0 && i < vec_safe_length (v))
513 vec<constructor_elt, va_gc> &vref = *v;
514 constructor_elt elt = vref[i];
515 for (; i > 0; --i)
516 vref[i] = vref[i-1];
517 vref[0] = elt;
520 return v;
523 /* Build compile-time evalable representations of member-initializer list
524 for a constexpr constructor. */
526 static tree
527 build_constexpr_constructor_member_initializers (tree type, tree body)
529 vec<constructor_elt, va_gc> *vec = NULL;
530 bool ok = true;
531 while (true)
532 switch (TREE_CODE (body))
534 case MUST_NOT_THROW_EXPR:
535 case EH_SPEC_BLOCK:
536 body = TREE_OPERAND (body, 0);
537 break;
539 case STATEMENT_LIST:
540 for (tree_stmt_iterator i = tsi_start (body);
541 !tsi_end_p (i); tsi_next (&i))
543 body = tsi_stmt (i);
544 if (TREE_CODE (body) == BIND_EXPR)
545 break;
547 break;
549 case BIND_EXPR:
550 body = BIND_EXPR_BODY (body);
551 goto found;
553 default:
554 gcc_unreachable ();
556 found:
557 if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
559 body = TREE_OPERAND (body, 0);
560 if (TREE_CODE (body) == EXPR_STMT)
561 body = TREE_OPERAND (body, 0);
562 if (TREE_CODE (body) == INIT_EXPR
563 && (same_type_ignoring_top_level_qualifiers_p
564 (TREE_TYPE (TREE_OPERAND (body, 0)),
565 current_class_type)))
567 /* Trivial copy. */
568 return TREE_OPERAND (body, 1);
570 ok = build_data_member_initialization (body, &vec);
572 else if (TREE_CODE (body) == STATEMENT_LIST)
574 tree_stmt_iterator i;
575 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
577 ok = build_data_member_initialization (tsi_stmt (i), &vec);
578 if (!ok)
579 break;
582 else if (TREE_CODE (body) == TRY_BLOCK)
584 error ("body of %<constexpr%> constructor cannot be "
585 "a function-try-block");
586 return error_mark_node;
588 else if (EXPR_P (body))
589 ok = build_data_member_initialization (body, &vec);
590 else
591 gcc_assert (errorcount > 0);
592 if (ok)
594 if (vec_safe_length (vec) > 0)
596 /* In a delegating constructor, return the target. */
597 constructor_elt *ce = &(*vec)[0];
598 if (ce->index == current_class_ptr)
600 body = ce->value;
601 vec_free (vec);
602 return body;
605 vec = sort_constexpr_mem_initializers (type, vec);
606 return build_constructor (type, vec);
608 else
609 return error_mark_node;
612 /* Subroutine of register_constexpr_fundef. BODY is the body of a function
613 declared to be constexpr, or a sub-statement thereof. Returns the
614 return value if suitable, error_mark_node for a statement not allowed in
615 a constexpr function, or NULL_TREE if no return value was found. */
617 static tree
618 constexpr_fn_retval (tree body)
620 switch (TREE_CODE (body))
622 case STATEMENT_LIST:
624 tree_stmt_iterator i;
625 tree expr = NULL_TREE;
626 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
628 tree s = constexpr_fn_retval (tsi_stmt (i));
629 if (s == error_mark_node)
630 return error_mark_node;
631 else if (s == NULL_TREE)
632 /* Keep iterating. */;
633 else if (expr)
634 /* Multiple return statements. */
635 return error_mark_node;
636 else
637 expr = s;
639 return expr;
642 case RETURN_EXPR:
643 return break_out_target_exprs (TREE_OPERAND (body, 0));
645 case DECL_EXPR:
647 tree decl = DECL_EXPR_DECL (body);
648 if (TREE_CODE (decl) == USING_DECL
649 /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */
650 || DECL_ARTIFICIAL (decl))
651 return NULL_TREE;
652 return error_mark_node;
655 case CLEANUP_POINT_EXPR:
656 return constexpr_fn_retval (TREE_OPERAND (body, 0));
658 case BIND_EXPR:
659 if (!check_constexpr_bind_expr_vars (body))
660 return error_mark_node;
661 return constexpr_fn_retval (BIND_EXPR_BODY (body));
663 case USING_STMT:
664 return NULL_TREE;
666 default:
667 return error_mark_node;
671 /* Subroutine of register_constexpr_fundef. BODY is the DECL_SAVED_TREE of
672 FUN; do the necessary transformations to turn it into a single expression
673 that we can store in the hash table. */
675 static tree
676 massage_constexpr_body (tree fun, tree body)
678 if (DECL_CONSTRUCTOR_P (fun))
679 body = build_constexpr_constructor_member_initializers
680 (DECL_CONTEXT (fun), body);
681 else if (cxx_dialect < cxx14)
683 if (TREE_CODE (body) == EH_SPEC_BLOCK)
684 body = EH_SPEC_STMTS (body);
685 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
686 body = TREE_OPERAND (body, 0);
687 body = constexpr_fn_retval (body);
689 return body;
692 /* FUN is a constexpr constructor with massaged body BODY. Return true
693 if some bases/fields are uninitialized, and complain if COMPLAIN. */
695 static bool
696 cx_check_missing_mem_inits (tree fun, tree body, bool complain)
698 bool bad;
699 tree field;
700 unsigned i, nelts;
701 tree ctype;
703 if (TREE_CODE (body) != CONSTRUCTOR)
704 return false;
706 nelts = CONSTRUCTOR_NELTS (body);
707 ctype = DECL_CONTEXT (fun);
708 field = TYPE_FIELDS (ctype);
710 if (TREE_CODE (ctype) == UNION_TYPE)
712 if (nelts == 0 && next_initializable_field (field))
714 if (complain)
715 error ("%<constexpr%> constructor for union %qT must "
716 "initialize exactly one non-static data member", ctype);
717 return true;
719 return false;
722 bad = false;
723 for (i = 0; i <= nelts; ++i)
725 tree index;
726 if (i == nelts)
727 index = NULL_TREE;
728 else
730 index = CONSTRUCTOR_ELT (body, i)->index;
731 /* Skip base and vtable inits. */
732 if (TREE_CODE (index) != FIELD_DECL
733 || DECL_ARTIFICIAL (index))
734 continue;
736 for (; field != index; field = DECL_CHAIN (field))
738 tree ftype;
739 if (TREE_CODE (field) != FIELD_DECL
740 || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
741 || DECL_ARTIFICIAL (field))
742 continue;
743 ftype = strip_array_types (TREE_TYPE (field));
744 if (type_has_constexpr_default_constructor (ftype))
746 /* It's OK to skip a member with a trivial constexpr ctor.
747 A constexpr ctor that isn't trivial should have been
748 added in by now. */
749 gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype)
750 || errorcount != 0);
751 continue;
753 if (!complain)
754 return true;
755 error ("member %qD must be initialized by mem-initializer "
756 "in %<constexpr%> constructor", field);
757 inform (DECL_SOURCE_LOCATION (field), "declared here");
758 bad = true;
760 if (field == NULL_TREE)
761 break;
762 field = DECL_CHAIN (field);
765 return bad;
768 /* We are processing the definition of the constexpr function FUN.
769 Check that its BODY fulfills the propriate requirements and
770 enter it in the constexpr function definition table.
771 For constructor BODY is actually the TREE_LIST of the
772 member-initializer list. */
774 tree
775 register_constexpr_fundef (tree fun, tree body)
777 constexpr_fundef entry;
778 constexpr_fundef **slot;
780 if (!is_valid_constexpr_fn (fun, !DECL_GENERATED_P (fun)))
781 return NULL;
783 tree massaged = massage_constexpr_body (fun, body);
784 if (massaged == NULL_TREE || massaged == error_mark_node)
786 if (!DECL_CONSTRUCTOR_P (fun))
787 error ("body of constexpr function %qD not a return-statement", fun);
788 return NULL;
791 if (!potential_rvalue_constant_expression (massaged))
793 if (!DECL_GENERATED_P (fun))
794 require_potential_rvalue_constant_expression (massaged);
795 return NULL;
798 if (DECL_CONSTRUCTOR_P (fun)
799 && cx_check_missing_mem_inits (fun, massaged, !DECL_GENERATED_P (fun)))
800 return NULL;
802 /* Create the constexpr function table if necessary. */
803 if (constexpr_fundef_table == NULL)
804 constexpr_fundef_table
805 = hash_table<constexpr_fundef_hasher>::create_ggc (101);
807 entry.decl = fun;
808 entry.body = body;
809 slot = constexpr_fundef_table->find_slot (&entry, INSERT);
811 gcc_assert (*slot == NULL);
812 *slot = ggc_alloc<constexpr_fundef> ();
813 **slot = entry;
815 return fun;
818 /* FUN is a non-constexpr function called in a context that requires a
819 constant expression. If it comes from a constexpr template, explain why
820 the instantiation isn't constexpr. */
822 void
823 explain_invalid_constexpr_fn (tree fun)
825 static hash_set<tree> *diagnosed;
826 tree body;
827 location_t save_loc;
828 /* Only diagnose defaulted functions or instantiations. */
829 if (!DECL_DEFAULTED_FN (fun)
830 && !is_instantiation_of_constexpr (fun))
831 return;
832 if (diagnosed == NULL)
833 diagnosed = new hash_set<tree>;
834 if (diagnosed->add (fun))
835 /* Already explained. */
836 return;
838 save_loc = input_location;
839 input_location = DECL_SOURCE_LOCATION (fun);
840 inform (input_location,
841 "%qD is not usable as a constexpr function because:", fun);
842 /* First check the declaration. */
843 if (is_valid_constexpr_fn (fun, true))
845 /* Then if it's OK, the body. */
846 if (!DECL_DECLARED_CONSTEXPR_P (fun))
847 explain_implicit_non_constexpr (fun);
848 else
850 body = massage_constexpr_body (fun, DECL_SAVED_TREE (fun));
851 require_potential_rvalue_constant_expression (body);
852 if (DECL_CONSTRUCTOR_P (fun))
853 cx_check_missing_mem_inits (fun, body, true);
856 input_location = save_loc;
859 /* Objects of this type represent calls to constexpr functions
860 along with the bindings of parameters to their arguments, for
861 the purpose of compile time evaluation. */
863 struct GTY((for_user)) constexpr_call {
864 /* Description of the constexpr function definition. */
865 constexpr_fundef *fundef;
866 /* Parameter bindings environment. A TREE_LIST where each TREE_PURPOSE
867 is a parameter _DECL and the TREE_VALUE is the value of the parameter.
868 Note: This arrangement is made to accommodate the use of
869 iterative_hash_template_arg (see pt.c). If you change this
870 representation, also change the hash calculation in
871 cxx_eval_call_expression. */
872 tree bindings;
873 /* Result of the call.
874 NULL means the call is being evaluated.
875 error_mark_node means that the evaluation was erroneous;
876 otherwise, the actuall value of the call. */
877 tree result;
878 /* The hash of this call; we remember it here to avoid having to
879 recalculate it when expanding the hash table. */
880 hashval_t hash;
883 struct constexpr_call_hasher : ggc_ptr_hash<constexpr_call>
885 static hashval_t hash (constexpr_call *);
886 static bool equal (constexpr_call *, constexpr_call *);
889 /* The constexpr expansion context. CALL is the current function
890 expansion, CTOR is the current aggregate initializer, OBJECT is the
891 object being initialized by CTOR, either a VAR_DECL or a _REF. VALUES
892 is a map of values of variables initialized within the expression. */
894 struct constexpr_ctx {
895 /* The innermost call we're evaluating. */
896 constexpr_call *call;
897 /* Values for any temporaries or local variables within the
898 constant-expression. */
899 hash_map<tree,tree> *values;
900 /* SAVE_EXPRs that we've seen within the current LOOP_EXPR. NULL if we
901 aren't inside a loop. */
902 hash_set<tree> *save_exprs;
903 /* The CONSTRUCTOR we're currently building up for an aggregate
904 initializer. */
905 tree ctor;
906 /* The object we're building the CONSTRUCTOR for. */
907 tree object;
908 /* Whether we should error on a non-constant expression or fail quietly. */
909 bool quiet;
910 /* Whether we are strictly conforming to constant expression rules or
911 trying harder to get a constant value. */
912 bool strict;
915 /* A table of all constexpr calls that have been evaluated by the
916 compiler in this translation unit. */
918 static GTY (()) hash_table<constexpr_call_hasher> *constexpr_call_table;
920 static tree cxx_eval_constant_expression (const constexpr_ctx *, tree,
921 bool, bool *, bool *, tree * = NULL);
923 /* Compute a hash value for a constexpr call representation. */
925 inline hashval_t
926 constexpr_call_hasher::hash (constexpr_call *info)
928 return info->hash;
931 /* Return true if the objects pointed to by P and Q represent calls
932 to the same constexpr function with the same arguments.
933 Otherwise, return false. */
935 bool
936 constexpr_call_hasher::equal (constexpr_call *lhs, constexpr_call *rhs)
938 tree lhs_bindings;
939 tree rhs_bindings;
940 if (lhs == rhs)
941 return 1;
942 if (!constexpr_fundef_hasher::equal (lhs->fundef, rhs->fundef))
943 return 0;
944 lhs_bindings = lhs->bindings;
945 rhs_bindings = rhs->bindings;
946 while (lhs_bindings != NULL && rhs_bindings != NULL)
948 tree lhs_arg = TREE_VALUE (lhs_bindings);
949 tree rhs_arg = TREE_VALUE (rhs_bindings);
950 gcc_assert (TREE_TYPE (lhs_arg) == TREE_TYPE (rhs_arg));
951 if (!cp_tree_equal (lhs_arg, rhs_arg))
952 return 0;
953 lhs_bindings = TREE_CHAIN (lhs_bindings);
954 rhs_bindings = TREE_CHAIN (rhs_bindings);
956 return lhs_bindings == rhs_bindings;
959 /* Initialize the constexpr call table, if needed. */
961 static void
962 maybe_initialize_constexpr_call_table (void)
964 if (constexpr_call_table == NULL)
965 constexpr_call_table = hash_table<constexpr_call_hasher>::create_ggc (101);
968 /* We have an expression tree T that represents a call, either CALL_EXPR
969 or AGGR_INIT_EXPR. If the call is lexically to a named function,
970 retrun the _DECL for that function. */
972 static tree
973 get_function_named_in_call (tree t)
975 tree fun = NULL;
976 switch (TREE_CODE (t))
978 case CALL_EXPR:
979 fun = CALL_EXPR_FN (t);
980 break;
982 case AGGR_INIT_EXPR:
983 fun = AGGR_INIT_EXPR_FN (t);
984 break;
986 default:
987 gcc_unreachable();
988 break;
990 if (fun && TREE_CODE (fun) == ADDR_EXPR
991 && TREE_CODE (TREE_OPERAND (fun, 0)) == FUNCTION_DECL)
992 fun = TREE_OPERAND (fun, 0);
993 return fun;
996 /* We have an expression tree T that represents a call, either CALL_EXPR
997 or AGGR_INIT_EXPR. Return the Nth argument. */
999 static inline tree
1000 get_nth_callarg (tree t, int n)
1002 switch (TREE_CODE (t))
1004 case CALL_EXPR:
1005 return CALL_EXPR_ARG (t, n);
1007 case AGGR_INIT_EXPR:
1008 return AGGR_INIT_EXPR_ARG (t, n);
1010 default:
1011 gcc_unreachable ();
1012 return NULL;
1016 /* Attempt to evaluate T which represents a call to a builtin function.
1017 We assume here that all builtin functions evaluate to scalar types
1018 represented by _CST nodes. */
1020 static tree
1021 cxx_eval_builtin_function_call (const constexpr_ctx *ctx, tree t, tree fun,
1022 bool lval,
1023 bool *non_constant_p, bool *overflow_p)
1025 const int nargs = call_expr_nargs (t);
1026 tree *args = (tree *) alloca (nargs * sizeof (tree));
1027 tree new_call;
1028 int i;
1030 /* Don't fold __builtin_constant_p within a constexpr function. */
1031 bool bi_const_p = (DECL_FUNCTION_CODE (fun) == BUILT_IN_CONSTANT_P);
1033 if (bi_const_p
1034 && current_function_decl
1035 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
1037 *non_constant_p = true;
1038 return t;
1041 /* Be permissive for arguments to built-ins; __builtin_constant_p should
1042 return constant false for a non-constant argument. */
1043 constexpr_ctx new_ctx = *ctx;
1044 new_ctx.quiet = true;
1045 bool dummy1 = false, dummy2 = false;
1046 for (i = 0; i < nargs; ++i)
1048 args[i] = cxx_eval_constant_expression (&new_ctx, CALL_EXPR_ARG (t, i),
1049 lval, &dummy1, &dummy2);
1050 if (bi_const_p)
1051 /* For __built_in_constant_p, fold all expressions with constant values
1052 even if they aren't C++ constant-expressions. */
1053 args[i] = cp_fully_fold (args[i]);
1056 bool save_ffbcp = force_folding_builtin_constant_p;
1057 force_folding_builtin_constant_p = true;
1058 new_call = fold_build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t),
1059 CALL_EXPR_FN (t), nargs, args);
1060 /* Fold away the NOP_EXPR from fold_builtin_n. */
1061 new_call = fold (new_call);
1062 force_folding_builtin_constant_p = save_ffbcp;
1063 VERIFY_CONSTANT (new_call);
1064 return new_call;
1067 /* TEMP is the constant value of a temporary object of type TYPE. Adjust
1068 the type of the value to match. */
1070 static tree
1071 adjust_temp_type (tree type, tree temp)
1073 if (TREE_TYPE (temp) == type)
1074 return temp;
1075 /* Avoid wrapping an aggregate value in a NOP_EXPR. */
1076 if (TREE_CODE (temp) == CONSTRUCTOR)
1077 return build_constructor (type, CONSTRUCTOR_ELTS (temp));
1078 gcc_assert (scalarish_type_p (type));
1079 return cp_fold_convert (type, temp);
1082 /* Subroutine of cxx_eval_call_expression.
1083 We are processing a call expression (either CALL_EXPR or
1084 AGGR_INIT_EXPR) in the context of CTX. Evaluate
1085 all arguments and bind their values to correspondings
1086 parameters, making up the NEW_CALL context. */
1088 static void
1089 cxx_bind_parameters_in_call (const constexpr_ctx *ctx, tree t,
1090 constexpr_call *new_call,
1091 bool *non_constant_p, bool *overflow_p,
1092 bool *non_constant_args)
1094 const int nargs = call_expr_nargs (t);
1095 tree fun = new_call->fundef->decl;
1096 tree parms = DECL_ARGUMENTS (fun);
1097 int i;
1098 tree *p = &new_call->bindings;
1099 for (i = 0; i < nargs; ++i)
1101 tree x, arg;
1102 tree type = parms ? TREE_TYPE (parms) : void_type_node;
1103 x = get_nth_callarg (t, i);
1104 /* For member function, the first argument is a pointer to the implied
1105 object. For a constructor, it might still be a dummy object, in
1106 which case we get the real argument from ctx. */
1107 if (i == 0 && DECL_CONSTRUCTOR_P (fun)
1108 && is_dummy_object (x))
1110 x = ctx->object;
1111 x = cp_build_addr_expr (x, tf_warning_or_error);
1113 bool lval = false;
1114 arg = cxx_eval_constant_expression (ctx, x, lval,
1115 non_constant_p, overflow_p);
1116 /* Don't VERIFY_CONSTANT here. */
1117 if (*non_constant_p && ctx->quiet)
1118 return;
1119 /* Just discard ellipsis args after checking their constantitude. */
1120 if (!parms)
1121 continue;
1122 if (*non_constant_p)
1123 /* Don't try to adjust the type of non-constant args. */
1124 goto next;
1126 /* Make sure the binding has the same type as the parm. */
1127 if (TREE_CODE (type) != REFERENCE_TYPE)
1128 arg = adjust_temp_type (type, arg);
1129 if (!TREE_CONSTANT (arg))
1130 *non_constant_args = true;
1131 *p = build_tree_list (parms, arg);
1132 p = &TREE_CHAIN (*p);
1133 next:
1134 parms = TREE_CHAIN (parms);
1138 /* Variables and functions to manage constexpr call expansion context.
1139 These do not need to be marked for PCH or GC. */
1141 /* FIXME remember and print actual constant arguments. */
1142 static vec<tree> call_stack = vNULL;
1143 static int call_stack_tick;
1144 static int last_cx_error_tick;
1146 static bool
1147 push_cx_call_context (tree call)
1149 ++call_stack_tick;
1150 if (!EXPR_HAS_LOCATION (call))
1151 SET_EXPR_LOCATION (call, input_location);
1152 call_stack.safe_push (call);
1153 if (call_stack.length () > (unsigned) max_constexpr_depth)
1154 return false;
1155 return true;
1158 static void
1159 pop_cx_call_context (void)
1161 ++call_stack_tick;
1162 call_stack.pop ();
1165 vec<tree>
1166 cx_error_context (void)
1168 vec<tree> r = vNULL;
1169 if (call_stack_tick != last_cx_error_tick
1170 && !call_stack.is_empty ())
1171 r = call_stack;
1172 last_cx_error_tick = call_stack_tick;
1173 return r;
1176 /* Subroutine of cxx_eval_constant_expression.
1177 Evaluate the call expression tree T in the context of OLD_CALL expression
1178 evaluation. */
1180 static tree
1181 cxx_eval_call_expression (const constexpr_ctx *ctx, tree t,
1182 bool lval,
1183 bool *non_constant_p, bool *overflow_p)
1185 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
1186 tree fun = get_function_named_in_call (t);
1187 constexpr_call new_call = { NULL, NULL, NULL, 0 };
1188 bool depth_ok;
1190 if (fun == NULL_TREE)
1191 switch (CALL_EXPR_IFN (t))
1193 case IFN_UBSAN_NULL:
1194 case IFN_UBSAN_BOUNDS:
1195 case IFN_UBSAN_VPTR:
1196 return void_node;
1197 default:
1198 if (!ctx->quiet)
1199 error_at (loc, "call to internal function");
1200 *non_constant_p = true;
1201 return t;
1204 if (TREE_CODE (fun) != FUNCTION_DECL)
1206 /* Might be a constexpr function pointer. */
1207 fun = cxx_eval_constant_expression (ctx, fun,
1208 /*lval*/false, non_constant_p,
1209 overflow_p);
1210 STRIP_NOPS (fun);
1211 if (TREE_CODE (fun) == ADDR_EXPR)
1212 fun = TREE_OPERAND (fun, 0);
1214 if (TREE_CODE (fun) != FUNCTION_DECL)
1216 if (!ctx->quiet && !*non_constant_p)
1217 error_at (loc, "expression %qE does not designate a constexpr "
1218 "function", fun);
1219 *non_constant_p = true;
1220 return t;
1222 if (DECL_CLONED_FUNCTION_P (fun))
1223 fun = DECL_CLONED_FUNCTION (fun);
1225 if (is_ubsan_builtin_p (fun))
1226 return void_node;
1228 if (is_builtin_fn (fun))
1229 return cxx_eval_builtin_function_call (ctx, t, fun,
1230 lval, non_constant_p, overflow_p);
1231 if (!DECL_DECLARED_CONSTEXPR_P (fun))
1233 if (!ctx->quiet)
1235 error_at (loc, "call to non-constexpr function %qD", fun);
1236 explain_invalid_constexpr_fn (fun);
1238 *non_constant_p = true;
1239 return t;
1242 constexpr_ctx new_ctx = *ctx;
1243 if (DECL_CONSTRUCTOR_P (fun) && !ctx->object
1244 && TREE_CODE (t) == AGGR_INIT_EXPR)
1246 /* We want to have an initialization target for an AGGR_INIT_EXPR.
1247 If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
1248 new_ctx.object = AGGR_INIT_EXPR_SLOT (t);
1249 tree ctor = new_ctx.ctor = build_constructor (DECL_CONTEXT (fun), NULL);
1250 CONSTRUCTOR_NO_IMPLICIT_ZERO (ctor) = true;
1251 ctx->values->put (new_ctx.object, ctor);
1252 ctx = &new_ctx;
1255 /* Shortcut trivial constructor/op=. */
1256 if (trivial_fn_p (fun))
1258 tree init = NULL_TREE;
1259 if (call_expr_nargs (t) == 2)
1260 init = convert_from_reference (get_nth_callarg (t, 1));
1261 else if (TREE_CODE (t) == AGGR_INIT_EXPR
1262 && AGGR_INIT_ZERO_FIRST (t))
1263 init = build_zero_init (DECL_CONTEXT (fun), NULL_TREE, false);
1264 if (init)
1266 tree op = get_nth_callarg (t, 0);
1267 if (is_dummy_object (op))
1268 op = ctx->object;
1269 else
1270 op = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (op)), op);
1271 tree set = build2 (MODIFY_EXPR, TREE_TYPE (op), op, init);
1272 return cxx_eval_constant_expression (ctx, set, lval,
1273 non_constant_p, overflow_p);
1277 /* We can't defer instantiating the function any longer. */
1278 if (!DECL_INITIAL (fun)
1279 && DECL_TEMPLOID_INSTANTIATION (fun))
1281 ++function_depth;
1282 instantiate_decl (fun, /*defer_ok*/false, /*expl_inst*/false);
1283 --function_depth;
1286 /* If in direct recursive call, optimize definition search. */
1287 if (ctx && ctx->call && ctx->call->fundef->decl == fun)
1288 new_call.fundef = ctx->call->fundef;
1289 else
1291 new_call.fundef = retrieve_constexpr_fundef (fun);
1292 if (new_call.fundef == NULL || new_call.fundef->body == NULL)
1294 if (!ctx->quiet)
1296 if (DECL_INITIAL (fun) == error_mark_node)
1297 error_at (loc, "%qD called in a constant expression before its "
1298 "definition is complete", fun);
1299 else if (DECL_INITIAL (fun))
1301 /* The definition of fun was somehow unsuitable. */
1302 error_at (loc, "%qD called in a constant expression", fun);
1303 explain_invalid_constexpr_fn (fun);
1305 else
1306 error_at (loc, "%qD used before its definition", fun);
1308 *non_constant_p = true;
1309 return t;
1313 bool non_constant_args = false;
1314 cxx_bind_parameters_in_call (ctx, t, &new_call,
1315 non_constant_p, overflow_p, &non_constant_args);
1316 if (*non_constant_p)
1317 return t;
1319 depth_ok = push_cx_call_context (t);
1321 tree result = NULL_TREE;
1323 constexpr_call *entry = NULL;
1324 if (depth_ok && !non_constant_args)
1326 new_call.hash = iterative_hash_template_arg
1327 (new_call.bindings, constexpr_fundef_hasher::hash (new_call.fundef));
1329 /* If we have seen this call before, we are done. */
1330 maybe_initialize_constexpr_call_table ();
1331 constexpr_call **slot
1332 = constexpr_call_table->find_slot (&new_call, INSERT);
1333 entry = *slot;
1334 if (entry == NULL)
1336 /* We need to keep a pointer to the entry, not just the slot, as the
1337 slot can move in the call to cxx_eval_builtin_function_call. */
1338 *slot = entry = ggc_alloc<constexpr_call> ();
1339 *entry = new_call;
1341 /* Calls which are in progress have their result set to NULL
1342 so that we can detect circular dependencies. */
1343 else if (entry->result == NULL)
1345 if (!ctx->quiet)
1346 error ("call has circular dependency");
1347 *non_constant_p = true;
1348 entry->result = result = error_mark_node;
1350 else
1351 result = entry->result;
1354 if (!depth_ok)
1356 if (!ctx->quiet)
1357 error ("constexpr evaluation depth exceeds maximum of %d (use "
1358 "-fconstexpr-depth= to increase the maximum)",
1359 max_constexpr_depth);
1360 *non_constant_p = true;
1361 result = error_mark_node;
1363 else
1365 if (!result || result == error_mark_node)
1367 gcc_assert (DECL_SAVED_TREE (fun));
1368 tree parms, res;
1370 /* Unshare the whole function body. */
1371 tree body = copy_fn (fun, parms, res);
1373 /* Associate the bindings with the remapped parms. */
1374 tree bound = new_call.bindings;
1375 tree remapped = parms;
1376 while (bound)
1378 tree oparm = TREE_PURPOSE (bound);
1379 tree arg = TREE_VALUE (bound);
1380 gcc_assert (DECL_NAME (remapped) == DECL_NAME (oparm));
1381 /* Don't share a CONSTRUCTOR that might be changed. */
1382 arg = unshare_expr (arg);
1383 ctx->values->put (remapped, arg);
1384 bound = TREE_CHAIN (bound);
1385 remapped = DECL_CHAIN (remapped);
1387 /* Add the RESULT_DECL to the values map, too. */
1388 tree slot = NULL_TREE;
1389 if (DECL_BY_REFERENCE (res))
1391 slot = AGGR_INIT_EXPR_SLOT (t);
1392 tree addr = build_address (slot);
1393 addr = build_nop (TREE_TYPE (res), addr);
1394 ctx->values->put (res, addr);
1395 ctx->values->put (slot, NULL_TREE);
1397 else
1398 ctx->values->put (res, NULL_TREE);
1400 tree jump_target = NULL_TREE;
1401 cxx_eval_constant_expression (ctx, body,
1402 lval, non_constant_p, overflow_p,
1403 &jump_target);
1405 if (DECL_CONSTRUCTOR_P (fun))
1406 /* This can be null for a subobject constructor call, in
1407 which case what we care about is the initialization
1408 side-effects rather than the value. We could get at the
1409 value by evaluating *this, but we don't bother; there's
1410 no need to put such a call in the hash table. */
1411 result = lval ? ctx->object : ctx->ctor;
1412 else if (VOID_TYPE_P (TREE_TYPE (res)))
1413 result = void_node;
1414 else
1416 result = *ctx->values->get (slot ? slot : res);
1417 if (result == NULL_TREE && !*non_constant_p)
1419 if (!ctx->quiet)
1420 error ("constexpr call flows off the end "
1421 "of the function");
1422 *non_constant_p = true;
1426 /* Remove the parms/result from the values map. Is it worth
1427 bothering to do this when the map itself is only live for
1428 one constexpr evaluation? If so, maybe also clear out
1429 other vars from call, maybe in BIND_EXPR handling? */
1430 ctx->values->remove (res);
1431 if (slot)
1432 ctx->values->remove (slot);
1433 for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
1434 ctx->values->remove (parm);
1437 if (result == error_mark_node)
1438 *non_constant_p = true;
1439 if (*non_constant_p || *overflow_p)
1440 result = error_mark_node;
1441 else if (!result)
1442 result = void_node;
1443 if (entry)
1444 entry->result = result;
1447 pop_cx_call_context ();
1448 return unshare_expr (result);
1451 /* FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
1453 bool
1454 reduced_constant_expression_p (tree t)
1456 switch (TREE_CODE (t))
1458 case PTRMEM_CST:
1459 /* Even if we can't lower this yet, it's constant. */
1460 return true;
1462 case CONSTRUCTOR:
1463 /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
1464 tree elt; unsigned HOST_WIDE_INT idx;
1465 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), idx, elt)
1466 if (!reduced_constant_expression_p (elt))
1467 return false;
1468 return true;
1470 default:
1471 /* FIXME are we calling this too much? */
1472 return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE;
1476 /* Some expressions may have constant operands but are not constant
1477 themselves, such as 1/0. Call this function (or rather, the macro
1478 following it) to check for that condition.
1480 We only call this in places that require an arithmetic constant, not in
1481 places where we might have a non-constant expression that can be a
1482 component of a constant expression, such as the address of a constexpr
1483 variable that might be dereferenced later. */
1485 static bool
1486 verify_constant (tree t, bool allow_non_constant, bool *non_constant_p,
1487 bool *overflow_p)
1489 if (!*non_constant_p && !reduced_constant_expression_p (t))
1491 if (!allow_non_constant)
1492 error ("%q+E is not a constant expression", t);
1493 *non_constant_p = true;
1495 if (TREE_OVERFLOW_P (t))
1497 if (!allow_non_constant)
1499 permerror (input_location, "overflow in constant expression");
1500 /* If we're being permissive (and are in an enforcing
1501 context), ignore the overflow. */
1502 if (flag_permissive)
1503 return *non_constant_p;
1505 *overflow_p = true;
1507 return *non_constant_p;
1510 /* Check whether the shift operation with code CODE and type TYPE on LHS
1511 and RHS is undefined. If it is, give an error with an explanation,
1512 and return true; return false otherwise. */
1514 static bool
1515 cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx,
1516 enum tree_code code, tree type, tree lhs, tree rhs)
1518 if ((code != LSHIFT_EXPR && code != RSHIFT_EXPR)
1519 || TREE_CODE (lhs) != INTEGER_CST
1520 || TREE_CODE (rhs) != INTEGER_CST)
1521 return false;
1523 tree lhstype = TREE_TYPE (lhs);
1524 unsigned HOST_WIDE_INT uprec = TYPE_PRECISION (TREE_TYPE (lhs));
1526 /* [expr.shift] The behavior is undefined if the right operand
1527 is negative, or greater than or equal to the length in bits
1528 of the promoted left operand. */
1529 if (tree_int_cst_sgn (rhs) == -1)
1531 if (!ctx->quiet)
1532 permerror (loc, "right operand of shift expression %q+E is negative",
1533 build2_loc (loc, code, type, lhs, rhs));
1534 return (!flag_permissive || ctx->quiet);
1536 if (compare_tree_int (rhs, uprec) >= 0)
1538 if (!ctx->quiet)
1539 permerror (loc, "right operand of shift expression %q+E is >= than "
1540 "the precision of the left operand",
1541 build2_loc (loc, code, type, lhs, rhs));
1542 return (!flag_permissive || ctx->quiet);
1545 /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
1546 if E1 has a signed type and non-negative value, and E1x2^E2 is
1547 representable in the corresponding unsigned type of the result type,
1548 then that value, converted to the result type, is the resulting value;
1549 otherwise, the behavior is undefined. */
1550 if (code == LSHIFT_EXPR && !TYPE_UNSIGNED (lhstype)
1551 && (cxx_dialect >= cxx11))
1553 if (tree_int_cst_sgn (lhs) == -1)
1555 if (!ctx->quiet)
1556 permerror (loc,
1557 "left operand of shift expression %q+E is negative",
1558 build2_loc (loc, code, type, lhs, rhs));
1559 return (!flag_permissive || ctx->quiet);
1561 /* For signed x << y the following:
1562 (unsigned) x >> ((prec (lhs) - 1) - y)
1563 if > 1, is undefined. The right-hand side of this formula
1564 is the highest bit of the LHS that can be set (starting from 0),
1565 so that the shift doesn't overflow. We then right-shift the LHS
1566 to see whether any other bit is set making the original shift
1567 undefined -- the result is not representable in the corresponding
1568 unsigned type. */
1569 tree t = build_int_cst (unsigned_type_node, uprec - 1);
1570 t = fold_build2 (MINUS_EXPR, unsigned_type_node, t, rhs);
1571 tree ulhs = fold_convert (unsigned_type_for (lhstype), lhs);
1572 t = fold_build2 (RSHIFT_EXPR, TREE_TYPE (ulhs), ulhs, t);
1573 if (tree_int_cst_lt (integer_one_node, t))
1575 if (!ctx->quiet)
1576 permerror (loc, "shift expression %q+E overflows",
1577 build2_loc (loc, code, type, lhs, rhs));
1578 return (!flag_permissive || ctx->quiet);
1581 return false;
1584 /* Subroutine of cxx_eval_constant_expression.
1585 Attempt to reduce the unary expression tree T to a compile time value.
1586 If successful, return the value. Otherwise issue a diagnostic
1587 and return error_mark_node. */
1589 static tree
1590 cxx_eval_unary_expression (const constexpr_ctx *ctx, tree t,
1591 bool /*lval*/,
1592 bool *non_constant_p, bool *overflow_p)
1594 tree r;
1595 tree orig_arg = TREE_OPERAND (t, 0);
1596 tree arg = cxx_eval_constant_expression (ctx, orig_arg, /*lval*/false,
1597 non_constant_p, overflow_p);
1598 VERIFY_CONSTANT (arg);
1599 location_t loc = EXPR_LOCATION (t);
1600 enum tree_code code = TREE_CODE (t);
1601 tree type = TREE_TYPE (t);
1602 r = fold_unary_loc (loc, code, type, arg);
1603 if (r == NULL_TREE)
1605 if (arg == orig_arg)
1606 r = t;
1607 else
1608 r = build1_loc (loc, code, type, arg);
1610 VERIFY_CONSTANT (r);
1611 return r;
1614 /* Subroutine of cxx_eval_constant_expression.
1615 Like cxx_eval_unary_expression, except for binary expressions. */
1617 static tree
1618 cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t,
1619 bool /*lval*/,
1620 bool *non_constant_p, bool *overflow_p)
1622 tree r = NULL_TREE;
1623 tree orig_lhs = TREE_OPERAND (t, 0);
1624 tree orig_rhs = TREE_OPERAND (t, 1);
1625 tree lhs, rhs;
1626 lhs = cxx_eval_constant_expression (ctx, orig_lhs, /*lval*/false,
1627 non_constant_p, overflow_p);
1628 /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer
1629 subtraction. */
1630 if (*non_constant_p)
1631 return t;
1632 rhs = cxx_eval_constant_expression (ctx, orig_rhs, /*lval*/false,
1633 non_constant_p, overflow_p);
1634 if (*non_constant_p)
1635 return t;
1637 location_t loc = EXPR_LOCATION (t);
1638 enum tree_code code = TREE_CODE (t);
1639 tree type = TREE_TYPE (t);
1641 if (code == EQ_EXPR || code == NE_EXPR)
1643 bool is_code_eq = (code == EQ_EXPR);
1645 if (TREE_CODE (lhs) == PTRMEM_CST
1646 && TREE_CODE (rhs) == PTRMEM_CST)
1647 r = constant_boolean_node (cp_tree_equal (lhs, rhs) == is_code_eq,
1648 type);
1649 else if ((TREE_CODE (lhs) == PTRMEM_CST
1650 || TREE_CODE (rhs) == PTRMEM_CST)
1651 && (null_member_pointer_value_p (lhs)
1652 || null_member_pointer_value_p (rhs)))
1653 r = constant_boolean_node (!is_code_eq, type);
1656 if (r == NULL_TREE)
1657 r = fold_binary_loc (loc, code, type, lhs, rhs);
1659 if (r == NULL_TREE)
1661 if (lhs == orig_lhs && rhs == orig_rhs)
1662 r = t;
1663 else
1664 r = build2_loc (loc, code, type, lhs, rhs);
1666 else if (cxx_eval_check_shift_p (loc, ctx, code, type, lhs, rhs))
1667 *non_constant_p = true;
1668 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
1669 a local array in a constexpr function. */
1670 bool ptr = POINTER_TYPE_P (TREE_TYPE (lhs));
1671 if (!ptr)
1672 VERIFY_CONSTANT (r);
1673 return r;
1676 /* Subroutine of cxx_eval_constant_expression.
1677 Attempt to evaluate condition expressions. Dead branches are not
1678 looked into. */
1680 static tree
1681 cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t,
1682 bool lval,
1683 bool *non_constant_p, bool *overflow_p,
1684 tree *jump_target)
1686 tree val = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
1687 /*lval*/false,
1688 non_constant_p, overflow_p);
1689 VERIFY_CONSTANT (val);
1690 /* Don't VERIFY_CONSTANT the other operands. */
1691 if (integer_zerop (val))
1692 return cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2),
1693 lval,
1694 non_constant_p, overflow_p,
1695 jump_target);
1696 return cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
1697 lval,
1698 non_constant_p, overflow_p,
1699 jump_target);
1702 /* Returns less than, equal to, or greater than zero if KEY is found to be
1703 less than, to match, or to be greater than the constructor_elt's INDEX. */
1705 static int
1706 array_index_cmp (tree key, tree index)
1708 gcc_assert (TREE_CODE (key) == INTEGER_CST);
1710 switch (TREE_CODE (index))
1712 case INTEGER_CST:
1713 return tree_int_cst_compare (key, index);
1714 case RANGE_EXPR:
1716 tree lo = TREE_OPERAND (index, 0);
1717 tree hi = TREE_OPERAND (index, 1);
1718 if (tree_int_cst_lt (key, lo))
1719 return -1;
1720 else if (tree_int_cst_lt (hi, key))
1721 return 1;
1722 else
1723 return 0;
1725 default:
1726 gcc_unreachable ();
1730 /* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1
1731 if none. If INSERT is true, insert a matching element rather than fail. */
1733 static HOST_WIDE_INT
1734 find_array_ctor_elt (tree ary, tree dindex, bool insert = false)
1736 if (tree_int_cst_sgn (dindex) < 0)
1737 return -1;
1739 unsigned HOST_WIDE_INT i = tree_to_uhwi (dindex);
1740 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ary);
1741 unsigned HOST_WIDE_INT len = vec_safe_length (elts);
1743 unsigned HOST_WIDE_INT end = len;
1744 unsigned HOST_WIDE_INT begin = 0;
1746 /* If the last element of the CONSTRUCTOR has its own index, we can assume
1747 that the same is true of the other elements and index directly. */
1748 if (end > 0)
1750 tree cindex = (*elts)[end-1].index;
1751 if (TREE_CODE (cindex) == INTEGER_CST
1752 && compare_tree_int (cindex, end-1) == 0)
1754 if (i < end)
1755 return i;
1756 else
1757 begin = end;
1761 /* Otherwise, find a matching index by means of a binary search. */
1762 while (begin != end)
1764 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
1765 constructor_elt &elt = (*elts)[middle];
1766 tree idx = elt.index;
1768 int cmp = array_index_cmp (dindex, idx);
1769 if (cmp < 0)
1770 end = middle;
1771 else if (cmp > 0)
1772 begin = middle + 1;
1773 else
1775 if (insert && TREE_CODE (idx) == RANGE_EXPR)
1777 /* We need to split the range. */
1778 constructor_elt e;
1779 tree lo = TREE_OPERAND (idx, 0);
1780 tree hi = TREE_OPERAND (idx, 1);
1781 if (tree_int_cst_lt (lo, dindex))
1783 /* There are still some lower elts; shorten the range. */
1784 tree new_hi = int_const_binop (MINUS_EXPR, dindex,
1785 size_one_node);
1786 if (tree_int_cst_equal (lo, new_hi))
1787 /* Only one element left, no longer a range. */
1788 elt.index = lo;
1789 else
1790 TREE_OPERAND (idx, 1) = new_hi;
1791 /* Append the element we want to insert. */
1792 ++middle;
1793 e.index = dindex;
1794 e.value = unshare_expr (elt.value);
1795 vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e);
1797 else
1798 /* No lower elts, the range elt is now ours. */
1799 elt.index = dindex;
1801 if (tree_int_cst_lt (dindex, hi))
1803 /* There are still some higher elts; append a range. */
1804 tree new_lo = int_const_binop (PLUS_EXPR, dindex,
1805 size_one_node);
1806 if (tree_int_cst_equal (new_lo, hi))
1807 e.index = hi;
1808 else
1809 e.index = build2 (RANGE_EXPR, sizetype, new_lo, hi);
1810 e.value = unshare_expr (elt.value);
1811 vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle+1, e);
1814 return middle;
1818 if (insert)
1820 constructor_elt e = { dindex, NULL_TREE };
1821 vec_safe_insert (CONSTRUCTOR_ELTS (ary), end, e);
1822 return end;
1825 return -1;
1828 /* Under the control of CTX, issue a detailed diagnostic for
1829 an out-of-bounds subscript INDEX into the expression ARRAY. */
1831 static void
1832 diag_array_subscript (const constexpr_ctx *ctx, tree array, tree index)
1834 if (!ctx->quiet)
1836 tree arraytype = TREE_TYPE (array);
1838 /* Convert the unsigned array subscript to a signed integer to avoid
1839 printing huge numbers for small negative values. */
1840 tree sidx = fold_convert (ssizetype, index);
1841 if (DECL_P (array))
1843 error ("array subscript value %qE is outside the bounds "
1844 "of array %qD of type %qT", sidx, array, arraytype);
1845 inform (DECL_SOURCE_LOCATION (array), "declared here");
1847 else
1848 error ("array subscript value %qE is outside the bounds "
1849 "of array type %qT", sidx, arraytype);
1853 /* Subroutine of cxx_eval_constant_expression.
1854 Attempt to reduce a reference to an array slot. */
1856 static tree
1857 cxx_eval_array_reference (const constexpr_ctx *ctx, tree t,
1858 bool lval,
1859 bool *non_constant_p, bool *overflow_p)
1861 tree oldary = TREE_OPERAND (t, 0);
1862 tree ary = cxx_eval_constant_expression (ctx, oldary,
1863 lval,
1864 non_constant_p, overflow_p);
1865 tree index, oldidx;
1866 HOST_WIDE_INT i;
1867 tree elem_type;
1868 unsigned len, elem_nchars = 1;
1869 if (*non_constant_p)
1870 return t;
1871 oldidx = TREE_OPERAND (t, 1);
1872 index = cxx_eval_constant_expression (ctx, oldidx,
1873 false,
1874 non_constant_p, overflow_p);
1875 VERIFY_CONSTANT (index);
1876 if (lval && ary == oldary && index == oldidx)
1877 return t;
1878 else if (lval)
1879 return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL);
1880 elem_type = TREE_TYPE (TREE_TYPE (ary));
1881 if (TREE_CODE (ary) == CONSTRUCTOR)
1882 len = CONSTRUCTOR_NELTS (ary);
1883 else if (TREE_CODE (ary) == STRING_CST)
1885 elem_nchars = (TYPE_PRECISION (elem_type)
1886 / TYPE_PRECISION (char_type_node));
1887 len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars;
1889 else
1891 /* We can't do anything with other tree codes, so use
1892 VERIFY_CONSTANT to complain and fail. */
1893 VERIFY_CONSTANT (ary);
1894 gcc_unreachable ();
1897 if (!tree_fits_shwi_p (index)
1898 || (i = tree_to_shwi (index)) < 0)
1900 diag_array_subscript (ctx, ary, index);
1901 *non_constant_p = true;
1902 return t;
1905 tree nelts = array_type_nelts_top (TREE_TYPE (ary));
1906 /* For VLAs, the number of elements won't be an integer constant. */
1907 nelts = cxx_eval_constant_expression (ctx, nelts, false, non_constant_p,
1908 overflow_p);
1909 VERIFY_CONSTANT (nelts);
1910 if (!tree_int_cst_lt (index, nelts))
1912 diag_array_subscript (ctx, ary, index);
1913 *non_constant_p = true;
1914 return t;
1917 bool found;
1918 if (TREE_CODE (ary) == CONSTRUCTOR)
1920 HOST_WIDE_INT ix = find_array_ctor_elt (ary, index);
1921 found = (ix >= 0);
1922 if (found)
1923 i = ix;
1925 else
1926 found = (i < len);
1928 if (!found)
1930 if (TREE_CODE (ary) == CONSTRUCTOR
1931 && CONSTRUCTOR_NO_IMPLICIT_ZERO (ary))
1933 /* 'ary' is part of the aggregate initializer we're currently
1934 building; if there's no initializer for this element yet,
1935 that's an error. */
1936 if (!ctx->quiet)
1937 error ("accessing uninitialized array element");
1938 *non_constant_p = true;
1939 return t;
1942 /* If it's within the array bounds but doesn't have an explicit
1943 initializer, it's value-initialized. */
1944 tree val = build_value_init (elem_type, tf_warning_or_error);
1945 return cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
1946 overflow_p);
1949 if (TREE_CODE (ary) == CONSTRUCTOR)
1950 return (*CONSTRUCTOR_ELTS (ary))[i].value;
1951 else if (elem_nchars == 1)
1952 return build_int_cst (cv_unqualified (TREE_TYPE (TREE_TYPE (ary))),
1953 TREE_STRING_POINTER (ary)[i]);
1954 else
1956 tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (ary)));
1957 return native_interpret_expr (type, (const unsigned char *)
1958 TREE_STRING_POINTER (ary)
1959 + i * elem_nchars, elem_nchars);
1961 /* Don't VERIFY_CONSTANT here. */
1964 /* Subroutine of cxx_eval_constant_expression.
1965 Attempt to reduce a field access of a value of class type. */
1967 static tree
1968 cxx_eval_component_reference (const constexpr_ctx *ctx, tree t,
1969 bool lval,
1970 bool *non_constant_p, bool *overflow_p)
1972 unsigned HOST_WIDE_INT i;
1973 tree field;
1974 tree value;
1975 tree part = TREE_OPERAND (t, 1);
1976 tree orig_whole = TREE_OPERAND (t, 0);
1977 tree whole = cxx_eval_constant_expression (ctx, orig_whole,
1978 lval,
1979 non_constant_p, overflow_p);
1980 if (TREE_CODE (whole) == PTRMEM_CST)
1981 whole = cplus_expand_constant (whole);
1982 if (whole == orig_whole)
1983 return t;
1984 if (lval)
1985 return fold_build3 (COMPONENT_REF, TREE_TYPE (t),
1986 whole, part, NULL_TREE);
1987 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
1988 CONSTRUCTOR. */
1989 if (!*non_constant_p && TREE_CODE (whole) != CONSTRUCTOR)
1991 if (!ctx->quiet)
1992 error ("%qE is not a constant expression", orig_whole);
1993 *non_constant_p = true;
1995 if (DECL_MUTABLE_P (part))
1997 if (!ctx->quiet)
1998 error ("mutable %qD is not usable in a constant expression", part);
1999 *non_constant_p = true;
2001 if (*non_constant_p)
2002 return t;
2003 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
2005 if (field == part)
2007 if (value)
2008 return value;
2009 else
2010 /* We're in the middle of initializing it. */
2011 break;
2014 if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE
2015 && CONSTRUCTOR_NELTS (whole) > 0)
2017 /* DR 1188 says we don't have to deal with this. */
2018 if (!ctx->quiet)
2019 error ("accessing %qD member instead of initialized %qD member in "
2020 "constant expression", part, CONSTRUCTOR_ELT (whole, 0)->index);
2021 *non_constant_p = true;
2022 return t;
2025 /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty
2026 classes never get represented; throw together a value now. */
2027 if (is_really_empty_class (TREE_TYPE (t)))
2028 return build_constructor (TREE_TYPE (t), NULL);
2030 if (CONSTRUCTOR_NO_IMPLICIT_ZERO (whole))
2032 /* 'whole' is part of the aggregate initializer we're currently
2033 building; if there's no initializer for this member yet, that's an
2034 error. */
2035 if (!ctx->quiet)
2036 error ("accessing uninitialized member %qD", part);
2037 *non_constant_p = true;
2038 return t;
2041 /* If there's no explicit init for this field, it's value-initialized. */
2042 value = build_value_init (TREE_TYPE (t), tf_warning_or_error);
2043 return cxx_eval_constant_expression (ctx, value,
2044 lval,
2045 non_constant_p, overflow_p);
2048 /* Subroutine of cxx_eval_constant_expression.
2049 Attempt to reduce a field access of a value of class type that is
2050 expressed as a BIT_FIELD_REF. */
2052 static tree
2053 cxx_eval_bit_field_ref (const constexpr_ctx *ctx, tree t,
2054 bool lval,
2055 bool *non_constant_p, bool *overflow_p)
2057 tree orig_whole = TREE_OPERAND (t, 0);
2058 tree retval, fldval, utype, mask;
2059 bool fld_seen = false;
2060 HOST_WIDE_INT istart, isize;
2061 tree whole = cxx_eval_constant_expression (ctx, orig_whole,
2062 lval,
2063 non_constant_p, overflow_p);
2064 tree start, field, value;
2065 unsigned HOST_WIDE_INT i;
2067 if (whole == orig_whole)
2068 return t;
2069 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
2070 CONSTRUCTOR. */
2071 if (!*non_constant_p
2072 && TREE_CODE (whole) != VECTOR_CST
2073 && TREE_CODE (whole) != CONSTRUCTOR)
2075 if (!ctx->quiet)
2076 error ("%qE is not a constant expression", orig_whole);
2077 *non_constant_p = true;
2079 if (*non_constant_p)
2080 return t;
2082 if (TREE_CODE (whole) == VECTOR_CST)
2083 return fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
2084 TREE_OPERAND (t, 1), TREE_OPERAND (t, 2));
2086 start = TREE_OPERAND (t, 2);
2087 istart = tree_to_shwi (start);
2088 isize = tree_to_shwi (TREE_OPERAND (t, 1));
2089 utype = TREE_TYPE (t);
2090 if (!TYPE_UNSIGNED (utype))
2091 utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1);
2092 retval = build_int_cst (utype, 0);
2093 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
2095 tree bitpos = bit_position (field);
2096 if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1))
2097 return value;
2098 if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE
2099 && TREE_CODE (value) == INTEGER_CST
2100 && tree_fits_shwi_p (bitpos)
2101 && tree_fits_shwi_p (DECL_SIZE (field)))
2103 HOST_WIDE_INT bit = tree_to_shwi (bitpos);
2104 HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field));
2105 HOST_WIDE_INT shift;
2106 if (bit >= istart && bit + sz <= istart + isize)
2108 fldval = fold_convert (utype, value);
2109 mask = build_int_cst_type (utype, -1);
2110 mask = fold_build2 (LSHIFT_EXPR, utype, mask,
2111 size_int (TYPE_PRECISION (utype) - sz));
2112 mask = fold_build2 (RSHIFT_EXPR, utype, mask,
2113 size_int (TYPE_PRECISION (utype) - sz));
2114 fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask);
2115 shift = bit - istart;
2116 if (BYTES_BIG_ENDIAN)
2117 shift = TYPE_PRECISION (utype) - shift - sz;
2118 fldval = fold_build2 (LSHIFT_EXPR, utype, fldval,
2119 size_int (shift));
2120 retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval);
2121 fld_seen = true;
2125 if (fld_seen)
2126 return fold_convert (TREE_TYPE (t), retval);
2127 gcc_unreachable ();
2128 return error_mark_node;
2131 /* Subroutine of cxx_eval_constant_expression.
2132 Evaluate a short-circuited logical expression T in the context
2133 of a given constexpr CALL. BAILOUT_VALUE is the value for
2134 early return. CONTINUE_VALUE is used here purely for
2135 sanity check purposes. */
2137 static tree
2138 cxx_eval_logical_expression (const constexpr_ctx *ctx, tree t,
2139 tree bailout_value, tree continue_value,
2140 bool lval,
2141 bool *non_constant_p, bool *overflow_p)
2143 tree r;
2144 tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
2145 lval,
2146 non_constant_p, overflow_p);
2147 VERIFY_CONSTANT (lhs);
2148 if (tree_int_cst_equal (lhs, bailout_value))
2149 return lhs;
2150 gcc_assert (tree_int_cst_equal (lhs, continue_value));
2151 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
2152 lval, non_constant_p,
2153 overflow_p);
2154 VERIFY_CONSTANT (r);
2155 return r;
2158 /* REF is a COMPONENT_REF designating a particular field. V is a vector of
2159 CONSTRUCTOR elements to initialize (part of) an object containing that
2160 field. Return a pointer to the constructor_elt corresponding to the
2161 initialization of the field. */
2163 static constructor_elt *
2164 base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref)
2166 tree aggr = TREE_OPERAND (ref, 0);
2167 tree field = TREE_OPERAND (ref, 1);
2168 HOST_WIDE_INT i;
2169 constructor_elt *ce;
2171 gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
2173 if (TREE_CODE (aggr) == COMPONENT_REF)
2175 constructor_elt *base_ce
2176 = base_field_constructor_elt (v, aggr);
2177 v = CONSTRUCTOR_ELTS (base_ce->value);
2180 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
2181 if (ce->index == field)
2182 return ce;
2184 gcc_unreachable ();
2185 return NULL;
2188 /* Some of the expressions fed to the constexpr mechanism are calls to
2189 constructors, which have type void. In that case, return the type being
2190 initialized by the constructor. */
2192 static tree
2193 initialized_type (tree t)
2195 if (TYPE_P (t))
2196 return t;
2197 tree type = cv_unqualified (TREE_TYPE (t));
2198 if (TREE_CODE (t) == CALL_EXPR || TREE_CODE (t) == AGGR_INIT_EXPR)
2200 /* A constructor call has void type, so we need to look deeper. */
2201 tree fn = get_function_named_in_call (t);
2202 if (fn && TREE_CODE (fn) == FUNCTION_DECL
2203 && DECL_CXX_CONSTRUCTOR_P (fn))
2204 type = DECL_CONTEXT (fn);
2206 return type;
2209 /* We're about to initialize element INDEX of an array or class from VALUE.
2210 Set up NEW_CTX appropriately by adjusting .object to refer to the
2211 subobject and creating a new CONSTRUCTOR if the element is itself
2212 a class or array. */
2214 static void
2215 init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx,
2216 tree index, tree &value)
2218 new_ctx = *ctx;
2220 if (index && TREE_CODE (index) != INTEGER_CST
2221 && TREE_CODE (index) != FIELD_DECL)
2222 /* This won't have an element in the new CONSTRUCTOR. */
2223 return;
2225 tree type = initialized_type (value);
2226 if (!AGGREGATE_TYPE_P (type) && !VECTOR_TYPE_P (type))
2227 /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
2228 return;
2230 /* The sub-aggregate initializer might contain a placeholder;
2231 update object to refer to the subobject and ctor to refer to
2232 the (newly created) sub-initializer. */
2233 if (ctx->object)
2234 new_ctx.object = build_ctor_subob_ref (index, type, ctx->object);
2235 tree elt = build_constructor (type, NULL);
2236 CONSTRUCTOR_NO_IMPLICIT_ZERO (elt) = true;
2237 new_ctx.ctor = elt;
2239 if (TREE_CODE (value) == TARGET_EXPR)
2240 /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
2241 value = TARGET_EXPR_INITIAL (value);
2244 /* We're about to process an initializer for a class or array TYPE. Make
2245 sure that CTX is set up appropriately. */
2247 static void
2248 verify_ctor_sanity (const constexpr_ctx *ctx, tree type)
2250 /* We don't bother building a ctor for an empty base subobject. */
2251 if (is_empty_class (type))
2252 return;
2254 /* We're in the middle of an initializer that might involve placeholders;
2255 our caller should have created a CONSTRUCTOR for us to put the
2256 initializer into. We will either return that constructor or T. */
2257 gcc_assert (ctx->ctor);
2258 gcc_assert (same_type_ignoring_top_level_qualifiers_p
2259 (type, TREE_TYPE (ctx->ctor)));
2260 /* We used to check that ctx->ctor was empty, but that isn't the case when
2261 the object is zero-initialized before calling the constructor. */
2262 if (ctx->object)
2263 gcc_assert (same_type_ignoring_top_level_qualifiers_p
2264 (type, TREE_TYPE (ctx->object)));
2265 gcc_assert (!ctx->object || !DECL_P (ctx->object)
2266 || *(ctx->values->get (ctx->object)) == ctx->ctor);
2269 /* Subroutine of cxx_eval_constant_expression.
2270 The expression tree T denotes a C-style array or a C-style
2271 aggregate. Reduce it to a constant expression. */
2273 static tree
2274 cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t,
2275 bool lval,
2276 bool *non_constant_p, bool *overflow_p)
2278 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
2279 bool changed = false;
2280 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t));
2281 tree type = TREE_TYPE (t);
2283 constexpr_ctx new_ctx;
2284 if (TYPE_PTRMEMFUNC_P (type))
2286 /* We don't really need the ctx->ctor business for a PMF, but it's
2287 simpler to use the same code. */
2288 new_ctx = *ctx;
2289 new_ctx.ctor = build_constructor (type, NULL);
2290 new_ctx.object = NULL_TREE;
2291 ctx = &new_ctx;
2293 verify_ctor_sanity (ctx, type);
2294 vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
2295 vec_alloc (*p, vec_safe_length (v));
2297 unsigned i;
2298 tree index, value;
2299 bool constant_p = true;
2300 bool side_effects_p = false;
2301 FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value)
2303 tree orig_value = value;
2304 init_subob_ctx (ctx, new_ctx, index, value);
2305 if (new_ctx.ctor != ctx->ctor)
2306 /* If we built a new CONSTRUCTOR, attach it now so that other
2307 initializers can refer to it. */
2308 CONSTRUCTOR_APPEND_ELT (*p, index, new_ctx.ctor);
2309 tree elt = cxx_eval_constant_expression (&new_ctx, value,
2310 lval,
2311 non_constant_p, overflow_p);
2312 /* Don't VERIFY_CONSTANT here. */
2313 if (ctx->quiet && *non_constant_p)
2314 break;
2315 if (elt != orig_value)
2316 changed = true;
2318 if (!TREE_CONSTANT (elt))
2319 constant_p = false;
2320 if (TREE_SIDE_EFFECTS (elt))
2321 side_effects_p = true;
2322 if (index && TREE_CODE (index) == COMPONENT_REF)
2324 /* This is an initialization of a vfield inside a base
2325 subaggregate that we already initialized; push this
2326 initialization into the previous initialization. */
2327 constructor_elt *inner = base_field_constructor_elt (*p, index);
2328 inner->value = elt;
2329 changed = true;
2331 else if (index
2332 && (TREE_CODE (index) == NOP_EXPR
2333 || TREE_CODE (index) == POINTER_PLUS_EXPR))
2335 /* This is an initializer for an empty base; now that we've
2336 checked that it's constant, we can ignore it. */
2337 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index))));
2338 changed = true;
2340 else if (new_ctx.ctor != ctx->ctor)
2342 /* We appended this element above; update the value. */
2343 gcc_assert ((*p)->last().index == index);
2344 (*p)->last().value = elt;
2346 else
2347 CONSTRUCTOR_APPEND_ELT (*p, index, elt);
2349 if (*non_constant_p || !changed)
2350 return t;
2351 t = ctx->ctor;
2352 /* We're done building this CONSTRUCTOR, so now we can interpret an
2353 element without an explicit initializer as value-initialized. */
2354 CONSTRUCTOR_NO_IMPLICIT_ZERO (t) = false;
2355 TREE_CONSTANT (t) = constant_p;
2356 TREE_SIDE_EFFECTS (t) = side_effects_p;
2357 if (VECTOR_TYPE_P (type))
2358 t = fold (t);
2359 return t;
2362 /* Subroutine of cxx_eval_constant_expression.
2363 The expression tree T is a VEC_INIT_EXPR which denotes the desired
2364 initialization of a non-static data member of array type. Reduce it to a
2365 CONSTRUCTOR.
2367 Note that apart from value-initialization (when VALUE_INIT is true),
2368 this is only intended to support value-initialization and the
2369 initializations done by defaulted constructors for classes with
2370 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
2371 will either be NULL_TREE for the default constructor, or a COMPONENT_REF
2372 for the copy/move constructor. */
2374 static tree
2375 cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init,
2376 bool value_init, bool lval,
2377 bool *non_constant_p, bool *overflow_p)
2379 tree elttype = TREE_TYPE (atype);
2380 unsigned HOST_WIDE_INT max = tree_to_uhwi (array_type_nelts_top (atype));
2381 verify_ctor_sanity (ctx, atype);
2382 vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
2383 vec_alloc (*p, max + 1);
2384 bool pre_init = false;
2385 unsigned HOST_WIDE_INT i;
2387 /* For the default constructor, build up a call to the default
2388 constructor of the element type. We only need to handle class types
2389 here, as for a constructor to be constexpr, all members must be
2390 initialized, which for a defaulted default constructor means they must
2391 be of a class type with a constexpr default constructor. */
2392 if (TREE_CODE (elttype) == ARRAY_TYPE)
2393 /* We only do this at the lowest level. */;
2394 else if (value_init)
2396 init = build_value_init (elttype, tf_warning_or_error);
2397 pre_init = true;
2399 else if (!init)
2401 vec<tree, va_gc> *argvec = make_tree_vector ();
2402 init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
2403 &argvec, elttype, LOOKUP_NORMAL,
2404 tf_warning_or_error);
2405 release_tree_vector (argvec);
2406 init = build_aggr_init_expr (TREE_TYPE (init), init);
2407 pre_init = true;
2410 for (i = 0; i < max; ++i)
2412 tree idx = build_int_cst (size_type_node, i);
2413 tree eltinit;
2414 bool reuse = false;
2415 constexpr_ctx new_ctx;
2416 init_subob_ctx (ctx, new_ctx, idx, pre_init ? init : elttype);
2417 if (new_ctx.ctor != ctx->ctor)
2418 CONSTRUCTOR_APPEND_ELT (*p, idx, new_ctx.ctor);
2419 if (TREE_CODE (elttype) == ARRAY_TYPE)
2421 /* A multidimensional array; recurse. */
2422 if (value_init || init == NULL_TREE)
2424 eltinit = NULL_TREE;
2425 reuse = i == 0;
2427 else
2428 eltinit = cp_build_array_ref (input_location, init, idx,
2429 tf_warning_or_error);
2430 eltinit = cxx_eval_vec_init_1 (&new_ctx, elttype, eltinit, value_init,
2431 lval,
2432 non_constant_p, overflow_p);
2434 else if (pre_init)
2436 /* Initializing an element using value or default initialization
2437 we just pre-built above. */
2438 eltinit = cxx_eval_constant_expression (&new_ctx, init, lval,
2439 non_constant_p, overflow_p);
2440 reuse = i == 0;
2442 else
2444 /* Copying an element. */
2445 gcc_assert (same_type_ignoring_top_level_qualifiers_p
2446 (atype, TREE_TYPE (init)));
2447 eltinit = cp_build_array_ref (input_location, init, idx,
2448 tf_warning_or_error);
2449 if (!real_lvalue_p (init))
2450 eltinit = move (eltinit);
2451 eltinit = force_rvalue (eltinit, tf_warning_or_error);
2452 eltinit = (cxx_eval_constant_expression
2453 (&new_ctx, eltinit, lval,
2454 non_constant_p, overflow_p));
2456 if (*non_constant_p && !ctx->quiet)
2457 break;
2458 if (new_ctx.ctor != ctx->ctor)
2460 /* We appended this element above; update the value. */
2461 gcc_assert ((*p)->last().index == idx);
2462 (*p)->last().value = eltinit;
2464 else
2465 CONSTRUCTOR_APPEND_ELT (*p, idx, eltinit);
2466 /* Reuse the result of cxx_eval_constant_expression call
2467 from the first iteration to all others if it is a constant
2468 initializer that doesn't require relocations. */
2469 if (reuse
2470 && max > 1
2471 && (initializer_constant_valid_p (eltinit, TREE_TYPE (eltinit))
2472 == null_pointer_node))
2474 if (new_ctx.ctor != ctx->ctor)
2475 eltinit = new_ctx.ctor;
2476 for (i = 1; i < max; ++i)
2478 idx = build_int_cst (size_type_node, i);
2479 CONSTRUCTOR_APPEND_ELT (*p, idx, unshare_expr (eltinit));
2481 break;
2485 if (!*non_constant_p)
2487 init = ctx->ctor;
2488 CONSTRUCTOR_NO_IMPLICIT_ZERO (init) = false;
2490 return init;
2493 static tree
2494 cxx_eval_vec_init (const constexpr_ctx *ctx, tree t,
2495 bool lval,
2496 bool *non_constant_p, bool *overflow_p)
2498 tree atype = TREE_TYPE (t);
2499 tree init = VEC_INIT_EXPR_INIT (t);
2500 tree r = cxx_eval_vec_init_1 (ctx, atype, init,
2501 VEC_INIT_EXPR_VALUE_INIT (t),
2502 lval, non_constant_p, overflow_p);
2503 if (*non_constant_p)
2504 return t;
2505 else
2506 return r;
2509 /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
2510 match. We want to be less strict for simple *& folding; if we have a
2511 non-const temporary that we access through a const pointer, that should
2512 work. We handle this here rather than change fold_indirect_ref_1
2513 because we're dealing with things like ADDR_EXPR of INTEGER_CST which
2514 don't really make sense outside of constant expression evaluation. Also
2515 we want to allow folding to COMPONENT_REF, which could cause trouble
2516 with TBAA in fold_indirect_ref_1.
2518 Try to keep this function synced with fold_indirect_ref_1. */
2520 static tree
2521 cxx_fold_indirect_ref (location_t loc, tree type, tree op0, bool *empty_base)
2523 tree sub, subtype;
2525 sub = op0;
2526 STRIP_NOPS (sub);
2527 subtype = TREE_TYPE (sub);
2528 if (!POINTER_TYPE_P (subtype))
2529 return NULL_TREE;
2531 if (TREE_CODE (sub) == ADDR_EXPR)
2533 tree op = TREE_OPERAND (sub, 0);
2534 tree optype = TREE_TYPE (op);
2536 /* *&CONST_DECL -> to the value of the const decl. */
2537 if (TREE_CODE (op) == CONST_DECL)
2538 return DECL_INITIAL (op);
2539 /* *&p => p; make sure to handle *&"str"[cst] here. */
2540 if (same_type_ignoring_top_level_qualifiers_p (optype, type)
2541 /* Also handle the case where the desired type is an array of unknown
2542 bounds because the variable has had its bounds deduced since the
2543 ADDR_EXPR was created. */
2544 || (TREE_CODE (type) == ARRAY_TYPE
2545 && TREE_CODE (optype) == ARRAY_TYPE
2546 && TYPE_DOMAIN (type) == NULL_TREE
2547 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (optype),
2548 TREE_TYPE (type))))
2550 tree fop = fold_read_from_constant_string (op);
2551 if (fop)
2552 return fop;
2553 else
2554 return op;
2556 /* *(foo *)&fooarray => fooarray[0] */
2557 else if (TREE_CODE (optype) == ARRAY_TYPE
2558 && (same_type_ignoring_top_level_qualifiers_p
2559 (type, TREE_TYPE (optype))))
2561 tree type_domain = TYPE_DOMAIN (optype);
2562 tree min_val = size_zero_node;
2563 if (type_domain && TYPE_MIN_VALUE (type_domain))
2564 min_val = TYPE_MIN_VALUE (type_domain);
2565 return build4_loc (loc, ARRAY_REF, type, op, min_val,
2566 NULL_TREE, NULL_TREE);
2568 /* *(foo *)&complexfoo => __real__ complexfoo */
2569 else if (TREE_CODE (optype) == COMPLEX_TYPE
2570 && (same_type_ignoring_top_level_qualifiers_p
2571 (type, TREE_TYPE (optype))))
2572 return fold_build1_loc (loc, REALPART_EXPR, type, op);
2573 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
2574 else if (VECTOR_TYPE_P (optype)
2575 && (same_type_ignoring_top_level_qualifiers_p
2576 (type, TREE_TYPE (optype))))
2578 tree part_width = TYPE_SIZE (type);
2579 tree index = bitsize_int (0);
2580 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
2582 /* Also handle conversion to an empty base class, which
2583 is represented with a NOP_EXPR. */
2584 else if (is_empty_class (type)
2585 && CLASS_TYPE_P (optype)
2586 && DERIVED_FROM_P (type, optype))
2588 *empty_base = true;
2589 return op;
2591 /* *(foo *)&struct_with_foo_field => COMPONENT_REF */
2592 else if (RECORD_OR_UNION_TYPE_P (optype))
2594 tree field = TYPE_FIELDS (optype);
2595 for (; field; field = DECL_CHAIN (field))
2596 if (TREE_CODE (field) == FIELD_DECL
2597 && integer_zerop (byte_position (field))
2598 && (same_type_ignoring_top_level_qualifiers_p
2599 (TREE_TYPE (field), type)))
2601 return fold_build3 (COMPONENT_REF, type, op, field, NULL_TREE);
2602 break;
2606 else if (TREE_CODE (sub) == POINTER_PLUS_EXPR
2607 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
2609 tree op00 = TREE_OPERAND (sub, 0);
2610 tree op01 = TREE_OPERAND (sub, 1);
2612 STRIP_NOPS (op00);
2613 if (TREE_CODE (op00) == ADDR_EXPR)
2615 tree op00type;
2616 op00 = TREE_OPERAND (op00, 0);
2617 op00type = TREE_TYPE (op00);
2619 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
2620 if (VECTOR_TYPE_P (op00type)
2621 && (same_type_ignoring_top_level_qualifiers_p
2622 (type, TREE_TYPE (op00type))))
2624 HOST_WIDE_INT offset = tree_to_shwi (op01);
2625 tree part_width = TYPE_SIZE (type);
2626 unsigned HOST_WIDE_INT part_widthi = tree_to_shwi (part_width)/BITS_PER_UNIT;
2627 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
2628 tree index = bitsize_int (indexi);
2630 if (offset / part_widthi < TYPE_VECTOR_SUBPARTS (op00type))
2631 return fold_build3_loc (loc,
2632 BIT_FIELD_REF, type, op00,
2633 part_width, index);
2636 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
2637 else if (TREE_CODE (op00type) == COMPLEX_TYPE
2638 && (same_type_ignoring_top_level_qualifiers_p
2639 (type, TREE_TYPE (op00type))))
2641 tree size = TYPE_SIZE_UNIT (type);
2642 if (tree_int_cst_equal (size, op01))
2643 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
2645 /* ((foo *)&fooarray)[1] => fooarray[1] */
2646 else if (TREE_CODE (op00type) == ARRAY_TYPE
2647 && (same_type_ignoring_top_level_qualifiers_p
2648 (type, TREE_TYPE (op00type))))
2650 tree type_domain = TYPE_DOMAIN (op00type);
2651 tree min_val = size_zero_node;
2652 if (type_domain && TYPE_MIN_VALUE (type_domain))
2653 min_val = TYPE_MIN_VALUE (type_domain);
2654 op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
2655 TYPE_SIZE_UNIT (type));
2656 op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
2657 return build4_loc (loc, ARRAY_REF, type, op00, op01,
2658 NULL_TREE, NULL_TREE);
2660 /* Also handle conversion to an empty base class, which
2661 is represented with a NOP_EXPR. */
2662 else if (is_empty_class (type)
2663 && CLASS_TYPE_P (op00type)
2664 && DERIVED_FROM_P (type, op00type))
2666 *empty_base = true;
2667 return op00;
2669 /* ((foo *)&struct_with_foo_field)[1] => COMPONENT_REF */
2670 else if (RECORD_OR_UNION_TYPE_P (op00type))
2672 tree field = TYPE_FIELDS (op00type);
2673 for (; field; field = DECL_CHAIN (field))
2674 if (TREE_CODE (field) == FIELD_DECL
2675 && tree_int_cst_equal (byte_position (field), op01)
2676 && (same_type_ignoring_top_level_qualifiers_p
2677 (TREE_TYPE (field), type)))
2679 return fold_build3 (COMPONENT_REF, type, op00,
2680 field, NULL_TREE);
2681 break;
2686 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
2687 else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
2688 && (same_type_ignoring_top_level_qualifiers_p
2689 (type, TREE_TYPE (TREE_TYPE (subtype)))))
2691 tree type_domain;
2692 tree min_val = size_zero_node;
2693 tree newsub = cxx_fold_indirect_ref (loc, TREE_TYPE (subtype), sub, NULL);
2694 if (newsub)
2695 sub = newsub;
2696 else
2697 sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub);
2698 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
2699 if (type_domain && TYPE_MIN_VALUE (type_domain))
2700 min_val = TYPE_MIN_VALUE (type_domain);
2701 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
2702 NULL_TREE);
2705 return NULL_TREE;
2708 static tree
2709 cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t,
2710 bool lval,
2711 bool *non_constant_p, bool *overflow_p)
2713 tree orig_op0 = TREE_OPERAND (t, 0);
2714 bool empty_base = false;
2716 /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second
2717 operand is an integer-zero. Otherwise reject the MEM_REF for now. */
2719 if (TREE_CODE (t) == MEM_REF
2720 && (!TREE_OPERAND (t, 1) || !integer_zerop (TREE_OPERAND (t, 1))))
2722 gcc_assert (ctx->quiet);
2723 *non_constant_p = true;
2724 return t;
2727 /* First try to simplify it directly. */
2728 tree r = cxx_fold_indirect_ref (EXPR_LOCATION (t), TREE_TYPE (t), orig_op0,
2729 &empty_base);
2730 if (!r)
2732 /* If that didn't work, evaluate the operand first. */
2733 tree op0 = cxx_eval_constant_expression (ctx, orig_op0,
2734 /*lval*/false, non_constant_p,
2735 overflow_p);
2736 /* Don't VERIFY_CONSTANT here. */
2737 if (*non_constant_p)
2738 return t;
2740 r = cxx_fold_indirect_ref (EXPR_LOCATION (t), TREE_TYPE (t), op0,
2741 &empty_base);
2742 if (r == NULL_TREE)
2744 /* We couldn't fold to a constant value. Make sure it's not
2745 something we should have been able to fold. */
2746 tree sub = op0;
2747 STRIP_NOPS (sub);
2748 if (TREE_CODE (sub) == ADDR_EXPR)
2750 gcc_assert (!same_type_ignoring_top_level_qualifiers_p
2751 (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t)));
2752 /* DR 1188 says we don't have to deal with this. */
2753 if (!ctx->quiet)
2754 error ("accessing value of %qE through a %qT glvalue in a "
2755 "constant expression", build_fold_indirect_ref (sub),
2756 TREE_TYPE (t));
2757 *non_constant_p = true;
2758 return t;
2761 if (lval && op0 != orig_op0)
2762 return build1 (INDIRECT_REF, TREE_TYPE (t), op0);
2763 if (!lval)
2764 VERIFY_CONSTANT (t);
2765 return t;
2769 r = cxx_eval_constant_expression (ctx, r,
2770 lval, non_constant_p, overflow_p);
2771 if (*non_constant_p)
2772 return t;
2774 /* If we're pulling out the value of an empty base, make sure
2775 that the whole object is constant and then return an empty
2776 CONSTRUCTOR. */
2777 if (empty_base && !lval)
2779 VERIFY_CONSTANT (r);
2780 r = build_constructor (TREE_TYPE (t), NULL);
2781 TREE_CONSTANT (r) = true;
2784 return r;
2787 /* Complain about R, a VAR_DECL, not being usable in a constant expression.
2788 Shared between potential_constant_expression and
2789 cxx_eval_constant_expression. */
2791 static void
2792 non_const_var_error (tree r)
2794 tree type = TREE_TYPE (r);
2795 error ("the value of %qD is not usable in a constant "
2796 "expression", r);
2797 /* Avoid error cascade. */
2798 if (DECL_INITIAL (r) == error_mark_node)
2799 return;
2800 if (DECL_DECLARED_CONSTEXPR_P (r))
2801 inform (DECL_SOURCE_LOCATION (r),
2802 "%qD used in its own initializer", r);
2803 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
2805 if (!CP_TYPE_CONST_P (type))
2806 inform (DECL_SOURCE_LOCATION (r),
2807 "%q#D is not const", r);
2808 else if (CP_TYPE_VOLATILE_P (type))
2809 inform (DECL_SOURCE_LOCATION (r),
2810 "%q#D is volatile", r);
2811 else if (!DECL_INITIAL (r)
2812 || !TREE_CONSTANT (DECL_INITIAL (r))
2813 || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r))
2814 inform (DECL_SOURCE_LOCATION (r),
2815 "%qD was not initialized with a constant "
2816 "expression", r);
2817 else
2818 gcc_unreachable ();
2820 else
2822 if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r))
2823 inform (DECL_SOURCE_LOCATION (r),
2824 "%qD was not declared %<constexpr%>", r);
2825 else
2826 inform (DECL_SOURCE_LOCATION (r),
2827 "%qD does not have integral or enumeration type",
2832 /* Subroutine of cxx_eval_constant_expression.
2833 Like cxx_eval_unary_expression, except for trinary expressions. */
2835 static tree
2836 cxx_eval_trinary_expression (const constexpr_ctx *ctx, tree t,
2837 bool lval,
2838 bool *non_constant_p, bool *overflow_p)
2840 int i;
2841 tree args[3];
2842 tree val;
2844 for (i = 0; i < 3; i++)
2846 args[i] = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, i),
2847 lval,
2848 non_constant_p, overflow_p);
2849 VERIFY_CONSTANT (args[i]);
2852 val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t),
2853 args[0], args[1], args[2]);
2854 if (val == NULL_TREE)
2855 return t;
2856 VERIFY_CONSTANT (val);
2857 return val;
2860 bool
2861 var_in_constexpr_fn (tree t)
2863 tree ctx = DECL_CONTEXT (t);
2864 return (cxx_dialect >= cxx14 && ctx && TREE_CODE (ctx) == FUNCTION_DECL
2865 && DECL_DECLARED_CONSTEXPR_P (ctx));
2868 /* Evaluate an INIT_EXPR or MODIFY_EXPR. */
2870 static tree
2871 cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
2872 bool lval,
2873 bool *non_constant_p, bool *overflow_p)
2875 constexpr_ctx new_ctx = *ctx;
2877 tree init = TREE_OPERAND (t, 1);
2878 if (TREE_CLOBBER_P (init))
2879 /* Just ignore clobbers. */
2880 return void_node;
2882 /* First we figure out where we're storing to. */
2883 tree target = TREE_OPERAND (t, 0);
2884 tree type = TREE_TYPE (target);
2885 target = cxx_eval_constant_expression (ctx, target,
2886 true,
2887 non_constant_p, overflow_p);
2888 if (*non_constant_p)
2889 return t;
2891 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (target), type))
2893 /* For initialization of an empty base, the original target will be
2894 *(base*)this, which the above evaluation resolves to the object
2895 argument, which has the derived type rather than the base type. In
2896 this situation, just evaluate the initializer and return, since
2897 there's no actual data to store. */
2898 gcc_assert (is_empty_class (type));
2899 return cxx_eval_constant_expression (ctx, init, false,
2900 non_constant_p, overflow_p);
2903 /* And then find the underlying variable. */
2904 vec<tree,va_gc> *refs = make_tree_vector();
2905 tree object = NULL_TREE;
2906 for (tree probe = target; object == NULL_TREE; )
2908 switch (TREE_CODE (probe))
2910 case BIT_FIELD_REF:
2911 case COMPONENT_REF:
2912 case ARRAY_REF:
2913 vec_safe_push (refs, TREE_OPERAND (probe, 1));
2914 vec_safe_push (refs, TREE_TYPE (probe));
2915 probe = TREE_OPERAND (probe, 0);
2916 break;
2918 default:
2919 object = probe;
2923 /* And then find/build up our initializer for the path to the subobject
2924 we're initializing. */
2925 tree *valp;
2926 if (DECL_P (object))
2927 valp = ctx->values->get (object);
2928 else
2929 valp = NULL;
2930 if (!valp)
2932 /* A constant-expression cannot modify objects from outside the
2933 constant-expression. */
2934 if (!ctx->quiet)
2935 error ("modification of %qE is not a constant-expression", object);
2936 *non_constant_p = true;
2937 return t;
2939 type = TREE_TYPE (object);
2940 bool no_zero_init = true;
2942 vec<tree,va_gc> *ctors = make_tree_vector ();
2943 while (!refs->is_empty())
2945 if (*valp == NULL_TREE)
2947 *valp = build_constructor (type, NULL);
2948 CONSTRUCTOR_NO_IMPLICIT_ZERO (*valp) = no_zero_init;
2950 /* If the value of object is already zero-initialized, any new ctors for
2951 subobjects will also be zero-initialized. */
2952 no_zero_init = CONSTRUCTOR_NO_IMPLICIT_ZERO (*valp);
2954 vec_safe_push (ctors, *valp);
2956 enum tree_code code = TREE_CODE (type);
2957 type = refs->pop();
2958 tree index = refs->pop();
2960 constructor_elt *cep = NULL;
2961 if (code == ARRAY_TYPE)
2963 HOST_WIDE_INT i
2964 = find_array_ctor_elt (*valp, index, /*insert*/true);
2965 gcc_assert (i >= 0);
2966 cep = CONSTRUCTOR_ELT (*valp, i);
2967 gcc_assert (TREE_CODE (cep->index) != RANGE_EXPR);
2969 else
2971 gcc_assert (TREE_CODE (index) == FIELD_DECL);
2973 /* We must keep the CONSTRUCTOR's ELTS in FIELD order.
2974 Usually we meet initializers in that order, but it is
2975 possible for base types to be placed not in program
2976 order. */
2977 tree fields = TYPE_FIELDS (DECL_CONTEXT (index));
2978 unsigned HOST_WIDE_INT idx;
2980 for (idx = 0;
2981 vec_safe_iterate (CONSTRUCTOR_ELTS (*valp), idx, &cep);
2982 idx++, fields = DECL_CHAIN (fields))
2984 if (index == cep->index)
2985 goto found;
2987 /* The field we're initializing must be on the field
2988 list. Look to see if it is present before the
2989 field the current ELT initializes. */
2990 for (; fields != cep->index; fields = DECL_CHAIN (fields))
2991 if (index == fields)
2992 goto insert;
2995 /* We fell off the end of the CONSTRUCTOR, so insert a new
2996 entry at the end. */
2997 insert:
2999 constructor_elt ce = { index, NULL_TREE };
3001 vec_safe_insert (CONSTRUCTOR_ELTS (*valp), idx, ce);
3002 cep = CONSTRUCTOR_ELT (*valp, idx);
3004 found:;
3006 valp = &cep->value;
3008 release_tree_vector (refs);
3010 if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
3012 /* Create a new CONSTRUCTOR in case evaluation of the initializer
3013 wants to modify it. */
3014 if (*valp == NULL_TREE)
3016 *valp = new_ctx.ctor = build_constructor (type, NULL);
3017 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx.ctor) = no_zero_init;
3019 else
3020 new_ctx.ctor = *valp;
3021 new_ctx.object = target;
3024 init = cxx_eval_constant_expression (&new_ctx, init, false,
3025 non_constant_p, overflow_p);
3026 /* Don't share a CONSTRUCTOR that might be changed later. */
3027 init = unshare_expr (init);
3028 if (target == object)
3029 /* The hash table might have moved since the get earlier. */
3030 valp = ctx->values->get (object);
3032 if (TREE_CODE (init) == CONSTRUCTOR)
3034 /* An outer ctx->ctor might be pointing to *valp, so replace
3035 its contents. */
3036 CONSTRUCTOR_ELTS (*valp) = CONSTRUCTOR_ELTS (init);
3037 TREE_CONSTANT (*valp) = TREE_CONSTANT (init);
3038 TREE_SIDE_EFFECTS (*valp) = TREE_SIDE_EFFECTS (init);
3040 else
3041 *valp = init;
3043 /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing
3044 CONSTRUCTORs, if any. */
3045 tree elt;
3046 unsigned i;
3047 bool c = TREE_CONSTANT (init);
3048 bool s = TREE_SIDE_EFFECTS (init);
3049 if (!c || s)
3050 FOR_EACH_VEC_SAFE_ELT (ctors, i, elt)
3052 if (!c)
3053 TREE_CONSTANT (elt) = false;
3054 if (s)
3055 TREE_SIDE_EFFECTS (elt) = true;
3057 release_tree_vector (ctors);
3059 if (*non_constant_p)
3060 return t;
3061 else if (lval)
3062 return target;
3063 else
3064 return init;
3067 /* Evaluate a ++ or -- expression. */
3069 static tree
3070 cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t,
3071 bool lval,
3072 bool *non_constant_p, bool *overflow_p)
3074 enum tree_code code = TREE_CODE (t);
3075 tree type = TREE_TYPE (t);
3076 tree op = TREE_OPERAND (t, 0);
3077 tree offset = TREE_OPERAND (t, 1);
3078 gcc_assert (TREE_CONSTANT (offset));
3080 /* The operand as an lvalue. */
3081 op = cxx_eval_constant_expression (ctx, op, true,
3082 non_constant_p, overflow_p);
3084 /* The operand as an rvalue. */
3085 tree val = rvalue (op);
3086 val = cxx_eval_constant_expression (ctx, val, false,
3087 non_constant_p, overflow_p);
3088 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
3089 a local array in a constexpr function. */
3090 bool ptr = POINTER_TYPE_P (TREE_TYPE (val));
3091 if (!ptr)
3092 VERIFY_CONSTANT (val);
3094 /* The modified value. */
3095 bool inc = (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR);
3096 tree mod;
3097 if (POINTER_TYPE_P (type))
3099 /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
3100 offset = convert_to_ptrofftype (offset);
3101 if (!inc)
3102 offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset);
3103 mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset);
3105 else
3106 mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset);
3107 if (!ptr)
3108 VERIFY_CONSTANT (mod);
3110 /* Storing the modified value. */
3111 tree store = build2 (MODIFY_EXPR, type, op, mod);
3112 cxx_eval_constant_expression (ctx, store,
3113 true, non_constant_p, overflow_p);
3115 /* And the value of the expression. */
3116 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
3118 /* Prefix ops are lvalues. */
3119 if (lval)
3120 return op;
3121 else
3122 /* But we optimize when the caller wants an rvalue. */
3123 return mod;
3125 else
3126 /* Postfix ops are rvalues. */
3127 return val;
3130 /* Predicates for the meaning of *jump_target. */
3132 static bool
3133 returns (tree *jump_target)
3135 return *jump_target
3136 && TREE_CODE (*jump_target) == RETURN_EXPR;
3139 static bool
3140 breaks (tree *jump_target)
3142 return *jump_target
3143 && TREE_CODE (*jump_target) == LABEL_DECL
3144 && LABEL_DECL_BREAK (*jump_target);
3147 static bool
3148 continues (tree *jump_target)
3150 return *jump_target
3151 && TREE_CODE (*jump_target) == LABEL_DECL
3152 && LABEL_DECL_CONTINUE (*jump_target);
3155 static bool
3156 switches (tree *jump_target)
3158 return *jump_target
3159 && TREE_CODE (*jump_target) == INTEGER_CST;
3162 /* Subroutine of cxx_eval_statement_list. Determine whether the statement
3163 at I matches *jump_target. If we're looking for a case label and we see
3164 the default label, copy I into DEFAULT_LABEL. */
3166 static bool
3167 label_matches (tree *jump_target, tree_stmt_iterator i,
3168 tree_stmt_iterator& default_label)
3170 tree stmt = tsi_stmt (i);
3171 switch (TREE_CODE (*jump_target))
3173 case LABEL_DECL:
3174 if (TREE_CODE (stmt) == LABEL_EXPR
3175 && LABEL_EXPR_LABEL (stmt) == *jump_target)
3176 return true;
3177 break;
3179 case INTEGER_CST:
3180 if (TREE_CODE (stmt) == CASE_LABEL_EXPR)
3182 if (!CASE_LOW (stmt))
3183 default_label = i;
3184 else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt)))
3185 return true;
3187 break;
3189 default:
3190 gcc_unreachable ();
3192 return false;
3195 /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
3196 semantics, for switch, break, continue, and return. */
3198 static tree
3199 cxx_eval_statement_list (const constexpr_ctx *ctx, tree t,
3200 bool *non_constant_p, bool *overflow_p,
3201 tree *jump_target)
3203 tree_stmt_iterator i;
3204 tree_stmt_iterator default_label = tree_stmt_iterator();
3205 tree local_target;
3206 /* In a statement-expression we want to return the last value. */
3207 tree r = NULL_TREE;
3208 if (!jump_target)
3210 local_target = NULL_TREE;
3211 jump_target = &local_target;
3213 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
3215 reenter:
3216 tree stmt = tsi_stmt (i);
3217 if (*jump_target)
3219 if (TREE_CODE (stmt) == STATEMENT_LIST)
3220 /* The label we want might be inside. */;
3221 else if (label_matches (jump_target, i, default_label))
3222 /* Found it. */
3223 *jump_target = NULL_TREE;
3224 else
3225 continue;
3227 r = cxx_eval_constant_expression (ctx, stmt, false,
3228 non_constant_p, overflow_p,
3229 jump_target);
3230 if (*non_constant_p)
3231 break;
3232 if (returns (jump_target) || breaks (jump_target))
3233 break;
3235 if (switches (jump_target) && !tsi_end_p (default_label))
3237 i = default_label;
3238 *jump_target = NULL_TREE;
3239 goto reenter;
3241 return r;
3244 /* Evaluate a LOOP_EXPR for side-effects. Handles break and return
3245 semantics; continue semantics are covered by cxx_eval_statement_list. */
3247 static tree
3248 cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t,
3249 bool *non_constant_p, bool *overflow_p,
3250 tree *jump_target)
3252 constexpr_ctx new_ctx = *ctx;
3254 tree body = TREE_OPERAND (t, 0);
3257 hash_set<tree> save_exprs;
3258 new_ctx.save_exprs = &save_exprs;
3260 cxx_eval_statement_list (&new_ctx, body,
3261 non_constant_p, overflow_p, jump_target);
3263 /* Forget saved values of SAVE_EXPRs. */
3264 for (hash_set<tree>::iterator iter = save_exprs.begin();
3265 iter != save_exprs.end(); ++iter)
3266 new_ctx.values->remove (*iter);
3268 while (!returns (jump_target) && !breaks (jump_target) && !*non_constant_p);
3270 if (breaks (jump_target))
3271 *jump_target = NULL_TREE;
3273 return NULL_TREE;
3276 /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
3277 semantics. */
3279 static tree
3280 cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t,
3281 bool *non_constant_p, bool *overflow_p,
3282 tree *jump_target)
3284 tree cond = TREE_OPERAND (t, 0);
3285 cond = cxx_eval_constant_expression (ctx, cond, false,
3286 non_constant_p, overflow_p);
3287 VERIFY_CONSTANT (cond);
3288 *jump_target = cond;
3290 tree body = TREE_OPERAND (t, 1);
3291 cxx_eval_statement_list (ctx, body,
3292 non_constant_p, overflow_p, jump_target);
3293 if (breaks (jump_target) || switches (jump_target))
3294 *jump_target = NULL_TREE;
3295 return NULL_TREE;
3298 /* Subroutine of cxx_eval_constant_expression.
3299 Attempt to reduce a POINTER_PLUS_EXPR expression T. */
3301 static tree
3302 cxx_eval_pointer_plus_expression (const constexpr_ctx *ctx, tree t,
3303 bool lval, bool *non_constant_p,
3304 bool *overflow_p)
3306 tree orig_type = TREE_TYPE (t);
3307 tree op00 = TREE_OPERAND (t, 0);
3308 tree op01 = TREE_OPERAND (t, 1);
3309 location_t loc = EXPR_LOCATION (t);
3311 op00 = cxx_eval_constant_expression (ctx, op00, lval,
3312 non_constant_p, overflow_p);
3314 STRIP_NOPS (op00);
3315 if (TREE_CODE (op00) != ADDR_EXPR)
3316 return NULL_TREE;
3318 op01 = cxx_eval_constant_expression (ctx, op01, lval,
3319 non_constant_p, overflow_p);
3320 op00 = TREE_OPERAND (op00, 0);
3322 /* &A[i] p+ j => &A[i + j] */
3323 if (TREE_CODE (op00) == ARRAY_REF
3324 && TREE_CODE (TREE_OPERAND (op00, 1)) == INTEGER_CST
3325 && TREE_CODE (op01) == INTEGER_CST
3326 && TYPE_SIZE_UNIT (TREE_TYPE (op00))
3327 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (op00))) == INTEGER_CST)
3329 tree type = TREE_TYPE (op00);
3330 t = fold_convert_loc (loc, ssizetype, TREE_OPERAND (op00, 1));
3331 tree nelts = array_type_nelts_top (TREE_TYPE (TREE_OPERAND (op00, 0)));
3332 /* Don't fold an out-of-bound access. */
3333 if (!tree_int_cst_le (t, nelts))
3334 return NULL_TREE;
3335 op01 = cp_fold_convert (ssizetype, op01);
3336 /* Don't fold if op01 can't be divided exactly by TYPE_SIZE_UNIT.
3337 constexpr int A[1]; ... (char *)&A[0] + 1 */
3338 if (!integer_zerop (fold_build2_loc (loc, TRUNC_MOD_EXPR, sizetype,
3339 op01, TYPE_SIZE_UNIT (type))))
3340 return NULL_TREE;
3341 /* Make sure to treat the second operand of POINTER_PLUS_EXPR
3342 as signed. */
3343 op01 = fold_build2_loc (loc, EXACT_DIV_EXPR, ssizetype, op01,
3344 TYPE_SIZE_UNIT (type));
3345 t = size_binop_loc (loc, PLUS_EXPR, op01, t);
3346 t = build4_loc (loc, ARRAY_REF, type, TREE_OPERAND (op00, 0),
3347 t, NULL_TREE, NULL_TREE);
3348 t = cp_build_addr_expr (t, tf_warning_or_error);
3349 t = cp_fold_convert (orig_type, t);
3350 return cxx_eval_constant_expression (ctx, t, lval, non_constant_p,
3351 overflow_p);
3354 return NULL_TREE;
3357 /* Attempt to reduce the expression T to a constant value.
3358 On failure, issue diagnostic and return error_mark_node. */
3359 /* FIXME unify with c_fully_fold */
3360 /* FIXME overflow_p is too global */
3362 static tree
3363 cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
3364 bool lval,
3365 bool *non_constant_p, bool *overflow_p,
3366 tree *jump_target)
3368 constexpr_ctx new_ctx;
3369 tree r = t;
3371 if (t == error_mark_node)
3373 *non_constant_p = true;
3374 return t;
3376 if (CONSTANT_CLASS_P (t))
3378 if (TREE_OVERFLOW (t))
3380 if (!ctx->quiet)
3381 permerror (input_location, "overflow in constant expression");
3382 if (!flag_permissive || ctx->quiet)
3383 *overflow_p = true;
3385 return t;
3388 switch (TREE_CODE (t))
3390 case RESULT_DECL:
3391 if (lval)
3392 return t;
3393 /* We ask for an rvalue for the RESULT_DECL when indirecting
3394 through an invisible reference, or in named return value
3395 optimization. */
3396 return (*ctx->values->get (t));
3398 case VAR_DECL:
3399 case CONST_DECL:
3400 /* We used to not check lval for CONST_DECL, but darwin.c uses
3401 CONST_DECL for aggregate constants. */
3402 if (lval)
3403 return t;
3404 if (ctx->strict)
3405 r = decl_really_constant_value (t);
3406 else
3407 r = decl_constant_value (t);
3408 if (TREE_CODE (r) == TARGET_EXPR
3409 && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR)
3410 r = TARGET_EXPR_INITIAL (r);
3411 if (VAR_P (r))
3412 if (tree *p = ctx->values->get (r))
3413 if (*p != NULL_TREE)
3414 r = *p;
3415 if (DECL_P (r))
3417 if (!ctx->quiet)
3418 non_const_var_error (r);
3419 *non_constant_p = true;
3421 break;
3423 case FUNCTION_DECL:
3424 case TEMPLATE_DECL:
3425 case LABEL_DECL:
3426 case LABEL_EXPR:
3427 case CASE_LABEL_EXPR:
3428 return t;
3430 case PARM_DECL:
3431 if (lval && TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
3432 /* glvalue use. */;
3433 else if (tree *p = ctx->values->get (r))
3434 r = *p;
3435 else if (lval)
3436 /* Defer in case this is only used for its type. */;
3437 else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
3438 /* Defer, there's no lvalue->rvalue conversion. */;
3439 else if (is_empty_class (TREE_TYPE (t)))
3441 /* If the class is empty, we aren't actually loading anything. */
3442 r = build_constructor (TREE_TYPE (t), NULL);
3443 TREE_CONSTANT (r) = true;
3445 else
3447 if (!ctx->quiet)
3448 error ("%qE is not a constant expression", t);
3449 *non_constant_p = true;
3451 break;
3453 case CALL_EXPR:
3454 case AGGR_INIT_EXPR:
3455 r = cxx_eval_call_expression (ctx, t, lval,
3456 non_constant_p, overflow_p);
3457 break;
3459 case DECL_EXPR:
3461 r = DECL_EXPR_DECL (t);
3462 if (AGGREGATE_TYPE_P (TREE_TYPE (r))
3463 || VECTOR_TYPE_P (TREE_TYPE (r)))
3465 new_ctx = *ctx;
3466 new_ctx.object = r;
3467 new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL);
3468 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx.ctor) = true;
3469 new_ctx.values->put (r, new_ctx.ctor);
3470 ctx = &new_ctx;
3473 if (tree init = DECL_INITIAL (r))
3475 init = cxx_eval_constant_expression (ctx, init,
3476 false,
3477 non_constant_p, overflow_p);
3478 /* Don't share a CONSTRUCTOR that might be changed. */
3479 init = unshare_expr (init);
3480 ctx->values->put (r, init);
3482 else if (ctx == &new_ctx)
3483 /* We gave it a CONSTRUCTOR above. */;
3484 else
3485 ctx->values->put (r, NULL_TREE);
3487 break;
3489 case TARGET_EXPR:
3490 if (!literal_type_p (TREE_TYPE (t)))
3492 if (!ctx->quiet)
3494 error ("temporary of non-literal type %qT in a "
3495 "constant expression", TREE_TYPE (t));
3496 explain_non_literal_class (TREE_TYPE (t));
3498 *non_constant_p = true;
3499 break;
3501 if ((AGGREGATE_TYPE_P (TREE_TYPE (t)) || VECTOR_TYPE_P (TREE_TYPE (t))))
3503 /* We're being expanded without an explicit target, so start
3504 initializing a new object; expansion with an explicit target
3505 strips the TARGET_EXPR before we get here. */
3506 new_ctx = *ctx;
3507 new_ctx.ctor = build_constructor (TREE_TYPE (t), NULL);
3508 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx.ctor) = true;
3509 new_ctx.object = TARGET_EXPR_SLOT (t);
3510 ctx->values->put (new_ctx.object, new_ctx.ctor);
3511 ctx = &new_ctx;
3513 /* Pass false for 'lval' because this indicates
3514 initialization of a temporary. */
3515 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
3516 false,
3517 non_constant_p, overflow_p);
3518 if (!*non_constant_p)
3519 /* Adjust the type of the result to the type of the temporary. */
3520 r = adjust_temp_type (TREE_TYPE (t), r);
3521 if (lval)
3523 tree slot = TARGET_EXPR_SLOT (t);
3524 r = unshare_expr (r);
3525 ctx->values->put (slot, r);
3526 return slot;
3528 break;
3530 case INIT_EXPR:
3531 case MODIFY_EXPR:
3532 r = cxx_eval_store_expression (ctx, t, lval,
3533 non_constant_p, overflow_p);
3534 break;
3536 case SCOPE_REF:
3537 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
3538 lval,
3539 non_constant_p, overflow_p);
3540 break;
3542 case RETURN_EXPR:
3543 if (TREE_OPERAND (t, 0) != NULL_TREE)
3544 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
3545 lval,
3546 non_constant_p, overflow_p);
3547 *jump_target = t;
3548 break;
3550 case SAVE_EXPR:
3551 /* Avoid evaluating a SAVE_EXPR more than once. */
3552 if (tree *p = ctx->values->get (t))
3553 r = *p;
3554 else
3556 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), false,
3557 non_constant_p, overflow_p);
3558 ctx->values->put (t, r);
3559 if (ctx->save_exprs)
3560 ctx->save_exprs->add (t);
3562 break;
3564 case NON_LVALUE_EXPR:
3565 case TRY_CATCH_EXPR:
3566 case TRY_BLOCK:
3567 case CLEANUP_POINT_EXPR:
3568 case MUST_NOT_THROW_EXPR:
3569 case EXPR_STMT:
3570 case EH_SPEC_BLOCK:
3571 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
3572 lval,
3573 non_constant_p, overflow_p,
3574 jump_target);
3575 break;
3577 case TRY_FINALLY_EXPR:
3578 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
3579 non_constant_p, overflow_p,
3580 jump_target);
3581 if (!*non_constant_p)
3582 /* Also evaluate the cleanup. */
3583 cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), true,
3584 non_constant_p, overflow_p,
3585 jump_target);
3586 break;
3588 /* These differ from cxx_eval_unary_expression in that this doesn't
3589 check for a constant operand or result; an address can be
3590 constant without its operand being, and vice versa. */
3591 case MEM_REF:
3592 case INDIRECT_REF:
3593 r = cxx_eval_indirect_ref (ctx, t, lval,
3594 non_constant_p, overflow_p);
3595 break;
3597 case ADDR_EXPR:
3599 tree oldop = TREE_OPERAND (t, 0);
3600 tree op = cxx_eval_constant_expression (ctx, oldop,
3601 /*lval*/true,
3602 non_constant_p, overflow_p);
3603 /* Don't VERIFY_CONSTANT here. */
3604 if (*non_constant_p)
3605 return t;
3606 gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR);
3607 /* This function does more aggressive folding than fold itself. */
3608 r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
3609 if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
3610 return t;
3611 break;
3614 case REALPART_EXPR:
3615 case IMAGPART_EXPR:
3616 case CONJ_EXPR:
3617 case FIX_TRUNC_EXPR:
3618 case FLOAT_EXPR:
3619 case NEGATE_EXPR:
3620 case ABS_EXPR:
3621 case BIT_NOT_EXPR:
3622 case TRUTH_NOT_EXPR:
3623 case FIXED_CONVERT_EXPR:
3624 r = cxx_eval_unary_expression (ctx, t, lval,
3625 non_constant_p, overflow_p);
3626 break;
3628 case SIZEOF_EXPR:
3629 r = fold_sizeof_expr (t);
3630 VERIFY_CONSTANT (r);
3631 break;
3633 case COMPOUND_EXPR:
3635 /* check_return_expr sometimes wraps a TARGET_EXPR in a
3636 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
3637 introduced by build_call_a. */
3638 tree op0 = TREE_OPERAND (t, 0);
3639 tree op1 = TREE_OPERAND (t, 1);
3640 STRIP_NOPS (op1);
3641 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
3642 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
3643 r = cxx_eval_constant_expression (ctx, op0,
3644 lval, non_constant_p, overflow_p,
3645 jump_target);
3646 else
3648 /* Check that the LHS is constant and then discard it. */
3649 cxx_eval_constant_expression (ctx, op0,
3650 true, non_constant_p, overflow_p,
3651 jump_target);
3652 op1 = TREE_OPERAND (t, 1);
3653 r = cxx_eval_constant_expression (ctx, op1,
3654 lval, non_constant_p, overflow_p,
3655 jump_target);
3658 break;
3660 case POINTER_PLUS_EXPR:
3661 r = cxx_eval_pointer_plus_expression (ctx, t, lval, non_constant_p,
3662 overflow_p);
3663 if (r)
3664 break;
3665 /* else fall through */
3667 case PLUS_EXPR:
3668 case MINUS_EXPR:
3669 case MULT_EXPR:
3670 case TRUNC_DIV_EXPR:
3671 case CEIL_DIV_EXPR:
3672 case FLOOR_DIV_EXPR:
3673 case ROUND_DIV_EXPR:
3674 case TRUNC_MOD_EXPR:
3675 case CEIL_MOD_EXPR:
3676 case ROUND_MOD_EXPR:
3677 case RDIV_EXPR:
3678 case EXACT_DIV_EXPR:
3679 case MIN_EXPR:
3680 case MAX_EXPR:
3681 case LSHIFT_EXPR:
3682 case RSHIFT_EXPR:
3683 case LROTATE_EXPR:
3684 case RROTATE_EXPR:
3685 case BIT_IOR_EXPR:
3686 case BIT_XOR_EXPR:
3687 case BIT_AND_EXPR:
3688 case TRUTH_XOR_EXPR:
3689 case LT_EXPR:
3690 case LE_EXPR:
3691 case GT_EXPR:
3692 case GE_EXPR:
3693 case EQ_EXPR:
3694 case NE_EXPR:
3695 case UNORDERED_EXPR:
3696 case ORDERED_EXPR:
3697 case UNLT_EXPR:
3698 case UNLE_EXPR:
3699 case UNGT_EXPR:
3700 case UNGE_EXPR:
3701 case UNEQ_EXPR:
3702 case LTGT_EXPR:
3703 case RANGE_EXPR:
3704 case COMPLEX_EXPR:
3705 r = cxx_eval_binary_expression (ctx, t, lval,
3706 non_constant_p, overflow_p);
3707 break;
3709 /* fold can introduce non-IF versions of these; still treat them as
3710 short-circuiting. */
3711 case TRUTH_AND_EXPR:
3712 case TRUTH_ANDIF_EXPR:
3713 r = cxx_eval_logical_expression (ctx, t, boolean_false_node,
3714 boolean_true_node,
3715 lval,
3716 non_constant_p, overflow_p);
3717 break;
3719 case TRUTH_OR_EXPR:
3720 case TRUTH_ORIF_EXPR:
3721 r = cxx_eval_logical_expression (ctx, t, boolean_true_node,
3722 boolean_false_node,
3723 lval,
3724 non_constant_p, overflow_p);
3725 break;
3727 case ARRAY_REF:
3728 r = cxx_eval_array_reference (ctx, t, lval,
3729 non_constant_p, overflow_p);
3730 break;
3732 case COMPONENT_REF:
3733 if (is_overloaded_fn (t))
3735 /* We can only get here in checking mode via
3736 build_non_dependent_expr, because any expression that
3737 calls or takes the address of the function will have
3738 pulled a FUNCTION_DECL out of the COMPONENT_REF. */
3739 gcc_checking_assert (ctx->quiet || errorcount);
3740 *non_constant_p = true;
3741 return t;
3743 r = cxx_eval_component_reference (ctx, t, lval,
3744 non_constant_p, overflow_p);
3745 break;
3747 case BIT_FIELD_REF:
3748 r = cxx_eval_bit_field_ref (ctx, t, lval,
3749 non_constant_p, overflow_p);
3750 break;
3752 case COND_EXPR:
3753 case VEC_COND_EXPR:
3754 r = cxx_eval_conditional_expression (ctx, t, lval,
3755 non_constant_p, overflow_p,
3756 jump_target);
3757 break;
3759 case CONSTRUCTOR:
3760 if (TREE_CONSTANT (t))
3762 /* Don't re-process a constant CONSTRUCTOR, but do fold it to
3763 VECTOR_CST if applicable. */
3764 /* FIXME after GCC 6 branches, make the verify unconditional. */
3765 if (CHECKING_P)
3766 verify_constructor_flags (t);
3767 else
3768 recompute_constructor_flags (t);
3769 if (TREE_CONSTANT (t))
3770 return fold (t);
3772 r = cxx_eval_bare_aggregate (ctx, t, lval,
3773 non_constant_p, overflow_p);
3774 break;
3776 case VEC_INIT_EXPR:
3777 /* We can get this in a defaulted constructor for a class with a
3778 non-static data member of array type. Either the initializer will
3779 be NULL, meaning default-initialization, or it will be an lvalue
3780 or xvalue of the same type, meaning direct-initialization from the
3781 corresponding member. */
3782 r = cxx_eval_vec_init (ctx, t, lval,
3783 non_constant_p, overflow_p);
3784 break;
3786 case FMA_EXPR:
3787 case VEC_PERM_EXPR:
3788 r = cxx_eval_trinary_expression (ctx, t, lval,
3789 non_constant_p, overflow_p);
3790 break;
3792 case CONVERT_EXPR:
3793 case VIEW_CONVERT_EXPR:
3794 case NOP_EXPR:
3795 case UNARY_PLUS_EXPR:
3797 enum tree_code tcode = TREE_CODE (t);
3798 tree oldop = TREE_OPERAND (t, 0);
3800 tree op = cxx_eval_constant_expression (ctx, oldop,
3801 lval,
3802 non_constant_p, overflow_p);
3803 if (*non_constant_p)
3804 return t;
3805 tree type = TREE_TYPE (t);
3806 if (TREE_CODE (op) == PTRMEM_CST
3807 && !TYPE_PTRMEM_P (type))
3808 op = cplus_expand_constant (op);
3809 if (TREE_CODE (op) == PTRMEM_CST && tcode == NOP_EXPR)
3811 if (same_type_ignoring_top_level_qualifiers_p (type,
3812 TREE_TYPE (op)))
3813 STRIP_NOPS (t);
3814 else
3816 if (!ctx->quiet)
3817 error_at (EXPR_LOC_OR_LOC (t, input_location),
3818 "a reinterpret_cast is not a constant-expression");
3819 *non_constant_p = true;
3820 return t;
3823 if (POINTER_TYPE_P (type)
3824 && TREE_CODE (op) == INTEGER_CST
3825 && !integer_zerop (op))
3827 if (!ctx->quiet)
3828 error_at (EXPR_LOC_OR_LOC (t, input_location),
3829 "reinterpret_cast from integer to pointer");
3830 *non_constant_p = true;
3831 return t;
3833 if (op == oldop && tcode != UNARY_PLUS_EXPR)
3834 /* We didn't fold at the top so we could check for ptr-int
3835 conversion. */
3836 return fold (t);
3837 if (tcode == UNARY_PLUS_EXPR)
3838 r = fold_convert (TREE_TYPE (t), op);
3839 else
3840 r = fold_build1 (tcode, type, op);
3841 /* Conversion of an out-of-range value has implementation-defined
3842 behavior; the language considers it different from arithmetic
3843 overflow, which is undefined. */
3844 if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op))
3845 TREE_OVERFLOW (r) = false;
3847 break;
3849 case EMPTY_CLASS_EXPR:
3850 /* This is good enough for a function argument that might not get
3851 used, and they can't do anything with it, so just return it. */
3852 return t;
3854 case STATEMENT_LIST:
3855 new_ctx = *ctx;
3856 new_ctx.ctor = new_ctx.object = NULL_TREE;
3857 return cxx_eval_statement_list (&new_ctx, t,
3858 non_constant_p, overflow_p, jump_target);
3860 case BIND_EXPR:
3861 return cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t),
3862 lval,
3863 non_constant_p, overflow_p,
3864 jump_target);
3866 case PREINCREMENT_EXPR:
3867 case POSTINCREMENT_EXPR:
3868 case PREDECREMENT_EXPR:
3869 case POSTDECREMENT_EXPR:
3870 return cxx_eval_increment_expression (ctx, t,
3871 lval, non_constant_p, overflow_p);
3873 case LAMBDA_EXPR:
3874 case NEW_EXPR:
3875 case VEC_NEW_EXPR:
3876 case DELETE_EXPR:
3877 case VEC_DELETE_EXPR:
3878 case THROW_EXPR:
3879 case MODOP_EXPR:
3880 /* GCC internal stuff. */
3881 case VA_ARG_EXPR:
3882 case OBJ_TYPE_REF:
3883 case WITH_CLEANUP_EXPR:
3884 case NON_DEPENDENT_EXPR:
3885 case BASELINK:
3886 case OFFSET_REF:
3887 if (!ctx->quiet)
3888 error_at (EXPR_LOC_OR_LOC (t, input_location),
3889 "expression %qE is not a constant-expression", t);
3890 *non_constant_p = true;
3891 break;
3893 case PLACEHOLDER_EXPR:
3894 if (!ctx || !ctx->ctor || (lval && !ctx->object)
3895 || !(same_type_ignoring_top_level_qualifiers_p
3896 (TREE_TYPE (t), TREE_TYPE (ctx->ctor))))
3898 /* A placeholder without a referent. We can get here when
3899 checking whether NSDMIs are noexcept, or in massage_init_elt;
3900 just say it's non-constant for now. */
3901 gcc_assert (ctx->quiet);
3902 *non_constant_p = true;
3903 break;
3905 else
3907 /* Use of the value or address of the current object. We could
3908 use ctx->object unconditionally, but using ctx->ctor when we
3909 can is a minor optimization. */
3910 tree ctor = lval ? ctx->object : ctx->ctor;
3911 return cxx_eval_constant_expression
3912 (ctx, ctor, lval,
3913 non_constant_p, overflow_p);
3915 break;
3917 case GOTO_EXPR:
3918 *jump_target = TREE_OPERAND (t, 0);
3919 gcc_assert (breaks (jump_target) || continues (jump_target));
3920 break;
3922 case LOOP_EXPR:
3923 cxx_eval_loop_expr (ctx, t,
3924 non_constant_p, overflow_p, jump_target);
3925 break;
3927 case SWITCH_EXPR:
3928 cxx_eval_switch_expr (ctx, t,
3929 non_constant_p, overflow_p, jump_target);
3930 break;
3932 case REQUIRES_EXPR:
3933 /* It's possible to get a requires-expression in a constant
3934 expression. For example:
3936 template<typename T> concept bool C() {
3937 return requires (T t) { t; };
3940 template<typename T> requires !C<T>() void f(T);
3942 Normalization leaves f with the associated constraint
3943 '!requires (T t) { ... }' which is not transformed into
3944 a constraint. */
3945 if (!processing_template_decl)
3946 return evaluate_constraint_expression (t, NULL_TREE);
3947 else
3948 *non_constant_p = true;
3949 return t;
3951 default:
3952 if (STATEMENT_CODE_P (TREE_CODE (t)))
3954 /* This function doesn't know how to deal with pre-genericize
3955 statements; this can only happen with statement-expressions,
3956 so for now just fail. */
3957 if (!ctx->quiet)
3958 error_at (EXPR_LOCATION (t),
3959 "statement is not a constant-expression");
3961 else
3962 internal_error ("unexpected expression %qE of kind %s", t,
3963 get_tree_code_name (TREE_CODE (t)));
3964 *non_constant_p = true;
3965 break;
3968 if (r == error_mark_node)
3969 *non_constant_p = true;
3971 if (*non_constant_p)
3972 return t;
3973 else
3974 return r;
3977 static tree
3978 cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant,
3979 bool strict = true, tree object = NULL_TREE)
3981 bool non_constant_p = false;
3982 bool overflow_p = false;
3983 hash_map<tree,tree> map;
3985 constexpr_ctx ctx = { NULL, &map, NULL, NULL, NULL,
3986 allow_non_constant, strict };
3988 tree type = initialized_type (t);
3989 tree r = t;
3990 if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
3992 /* In C++14 an NSDMI can participate in aggregate initialization,
3993 and can refer to the address of the object being initialized, so
3994 we need to pass in the relevant VAR_DECL if we want to do the
3995 evaluation in a single pass. The evaluation will dynamically
3996 update ctx.values for the VAR_DECL. We use the same strategy
3997 for C++11 constexpr constructors that refer to the object being
3998 initialized. */
3999 ctx.ctor = build_constructor (type, NULL);
4000 CONSTRUCTOR_NO_IMPLICIT_ZERO (ctx.ctor) = true;
4001 if (!object)
4003 if (TREE_CODE (t) == TARGET_EXPR)
4004 object = TARGET_EXPR_SLOT (t);
4005 else if (TREE_CODE (t) == AGGR_INIT_EXPR)
4006 object = AGGR_INIT_EXPR_SLOT (t);
4008 ctx.object = object;
4009 if (object)
4010 gcc_assert (same_type_ignoring_top_level_qualifiers_p
4011 (type, TREE_TYPE (object)));
4012 if (object && DECL_P (object))
4013 map.put (object, ctx.ctor);
4014 if (TREE_CODE (r) == TARGET_EXPR)
4015 /* Avoid creating another CONSTRUCTOR when we expand the
4016 TARGET_EXPR. */
4017 r = TARGET_EXPR_INITIAL (r);
4020 r = cxx_eval_constant_expression (&ctx, r,
4021 false, &non_constant_p, &overflow_p);
4023 verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p);
4025 /* Mutable logic is a bit tricky: we want to allow initialization of
4026 constexpr variables with mutable members, but we can't copy those
4027 members to another constexpr variable. */
4028 if (TREE_CODE (r) == CONSTRUCTOR
4029 && CONSTRUCTOR_MUTABLE_POISON (r))
4031 if (!allow_non_constant)
4032 error ("%qE is not a constant expression because it refers to "
4033 "mutable subobjects of %qT", t, type);
4034 non_constant_p = true;
4037 /* Technically we should check this for all subexpressions, but that
4038 runs into problems with our internal representation of pointer
4039 subtraction and the 5.19 rules are still in flux. */
4040 if (CONVERT_EXPR_CODE_P (TREE_CODE (r))
4041 && ARITHMETIC_TYPE_P (TREE_TYPE (r))
4042 && TREE_CODE (TREE_OPERAND (r, 0)) == ADDR_EXPR)
4044 if (!allow_non_constant)
4045 error ("conversion from pointer type %qT "
4046 "to arithmetic type %qT in a constant-expression",
4047 TREE_TYPE (TREE_OPERAND (r, 0)), TREE_TYPE (r));
4048 non_constant_p = true;
4051 if (!non_constant_p && overflow_p)
4052 non_constant_p = true;
4054 if (non_constant_p && !allow_non_constant)
4055 return error_mark_node;
4056 else if (non_constant_p && TREE_CONSTANT (r))
4058 /* This isn't actually constant, so unset TREE_CONSTANT. */
4059 if (EXPR_P (r))
4060 r = copy_node (r);
4061 else if (TREE_CODE (r) == CONSTRUCTOR)
4062 r = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (r), r);
4063 else
4064 r = build_nop (TREE_TYPE (r), r);
4065 TREE_CONSTANT (r) = false;
4067 else if (non_constant_p || r == t)
4068 return t;
4070 if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
4072 if (TREE_CODE (t) == TARGET_EXPR
4073 && TARGET_EXPR_INITIAL (t) == r)
4074 return t;
4075 else
4077 r = get_target_expr (r);
4078 TREE_CONSTANT (r) = true;
4079 return r;
4082 else
4083 return r;
4086 /* Returns true if T is a valid subexpression of a constant expression,
4087 even if it isn't itself a constant expression. */
4089 bool
4090 is_sub_constant_expr (tree t)
4092 bool non_constant_p = false;
4093 bool overflow_p = false;
4094 hash_map <tree, tree> map;
4096 constexpr_ctx ctx = { NULL, &map, NULL, NULL, NULL, true, true };
4098 cxx_eval_constant_expression (&ctx, t, false, &non_constant_p,
4099 &overflow_p);
4100 return !non_constant_p && !overflow_p;
4103 /* If T represents a constant expression returns its reduced value.
4104 Otherwise return error_mark_node. If T is dependent, then
4105 return NULL. */
4107 tree
4108 cxx_constant_value (tree t, tree decl)
4110 return cxx_eval_outermost_constant_expr (t, false, true, decl);
4113 /* Helper routine for fold_simple function. Either return simplified
4114 expression T, otherwise NULL_TREE.
4115 In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold
4116 even if we are within template-declaration. So be careful on call, as in
4117 such case types can be undefined. */
4119 static tree
4120 fold_simple_1 (tree t)
4122 tree op1;
4123 enum tree_code code = TREE_CODE (t);
4125 switch (code)
4127 case INTEGER_CST:
4128 case REAL_CST:
4129 case VECTOR_CST:
4130 case FIXED_CST:
4131 case COMPLEX_CST:
4132 return t;
4134 case SIZEOF_EXPR:
4135 return fold_sizeof_expr (t);
4137 case ABS_EXPR:
4138 case CONJ_EXPR:
4139 case REALPART_EXPR:
4140 case IMAGPART_EXPR:
4141 case NEGATE_EXPR:
4142 case BIT_NOT_EXPR:
4143 case TRUTH_NOT_EXPR:
4144 case NOP_EXPR:
4145 case VIEW_CONVERT_EXPR:
4146 case CONVERT_EXPR:
4147 case FLOAT_EXPR:
4148 case FIX_TRUNC_EXPR:
4149 case FIXED_CONVERT_EXPR:
4150 case ADDR_SPACE_CONVERT_EXPR:
4152 op1 = TREE_OPERAND (t, 0);
4154 t = const_unop (code, TREE_TYPE (t), op1);
4155 if (!t)
4156 return NULL_TREE;
4158 if (CONVERT_EXPR_CODE_P (code)
4159 && TREE_OVERFLOW_P (t) && !TREE_OVERFLOW_P (op1))
4160 TREE_OVERFLOW (t) = false;
4161 return t;
4163 default:
4164 return NULL_TREE;
4168 /* If T is a simple constant expression, returns its simplified value.
4169 Otherwise returns T. In contrast to maybe_constant_value do we
4170 simplify only few operations on constant-expressions, and we don't
4171 try to simplify constexpressions. */
4173 tree
4174 fold_simple (tree t)
4176 tree r = NULL_TREE;
4177 if (processing_template_decl)
4178 return t;
4180 r = fold_simple_1 (t);
4181 if (!r)
4182 r = t;
4184 return r;
4187 /* If T is a constant expression, returns its reduced value.
4188 Otherwise, if T does not have TREE_CONSTANT set, returns T.
4189 Otherwise, returns a version of T without TREE_CONSTANT. */
4191 static tree
4192 maybe_constant_value_1 (tree t, tree decl)
4194 tree r;
4196 if (instantiation_dependent_expression_p (t)
4197 || type_unknown_p (t)
4198 || BRACE_ENCLOSED_INITIALIZER_P (t)
4199 || !potential_constant_expression (t))
4201 if (TREE_OVERFLOW_P (t))
4203 t = build_nop (TREE_TYPE (t), t);
4204 TREE_CONSTANT (t) = false;
4206 return t;
4209 r = cxx_eval_outermost_constant_expr (t, true, true, decl);
4210 gcc_checking_assert (r == t
4211 || CONVERT_EXPR_P (t)
4212 || TREE_CODE (t) == VIEW_CONVERT_EXPR
4213 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
4214 || !cp_tree_equal (r, t));
4215 return r;
4218 static GTY((cache, deletable)) cache_map cv_cache;
4220 /* If T is a constant expression, returns its reduced value.
4221 Otherwise, if T does not have TREE_CONSTANT set, returns T.
4222 Otherwise, returns a version of T without TREE_CONSTANT. */
4224 tree
4225 maybe_constant_value (tree t, tree decl)
4227 tree ret = cv_cache.get (t);
4228 if (!ret)
4230 ret = maybe_constant_value_1 (t, decl);
4231 cv_cache.put (t, ret);
4233 return ret;
4236 /* Dispose of the whole CV_CACHE and FOLD_CACHE. */
4238 void
4239 clear_cv_and_fold_caches (void)
4241 gt_cleare_cache (cv_cache);
4242 clear_fold_cache ();
4245 /* Like maybe_constant_value but first fully instantiate the argument.
4247 Note: this is equivalent to instantiate_non_dependent_expr_sfinae
4248 (t, tf_none) followed by maybe_constant_value but is more efficient,
4249 because calls instantiation_dependent_expression_p and
4250 potential_constant_expression at most once. */
4252 tree
4253 fold_non_dependent_expr (tree t)
4255 if (t == NULL_TREE)
4256 return NULL_TREE;
4258 /* If we're in a template, but T isn't value dependent, simplify
4259 it. We're supposed to treat:
4261 template <typename T> void f(T[1 + 1]);
4262 template <typename T> void f(T[2]);
4264 as two declarations of the same function, for example. */
4265 if (processing_template_decl)
4267 if (!instantiation_dependent_expression_p (t)
4268 && potential_constant_expression (t))
4270 processing_template_decl_sentinel s;
4271 t = instantiate_non_dependent_expr_internal (t, tf_none);
4273 if (type_unknown_p (t)
4274 || BRACE_ENCLOSED_INITIALIZER_P (t))
4276 if (TREE_OVERFLOW_P (t))
4278 t = build_nop (TREE_TYPE (t), t);
4279 TREE_CONSTANT (t) = false;
4281 return t;
4284 tree r = cxx_eval_outermost_constant_expr (t, true, true, NULL_TREE);
4285 /* cp_tree_equal looks through NOPs, so allow them. */
4286 gcc_checking_assert (r == t
4287 || CONVERT_EXPR_P (t)
4288 || TREE_CODE (t) == VIEW_CONVERT_EXPR
4289 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
4290 || !cp_tree_equal (r, t));
4291 return r;
4293 else if (TREE_OVERFLOW_P (t))
4295 t = build_nop (TREE_TYPE (t), t);
4296 TREE_CONSTANT (t) = false;
4298 return t;
4301 return maybe_constant_value (t);
4304 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
4305 than wrapped in a TARGET_EXPR. */
4307 tree
4308 maybe_constant_init (tree t, tree decl)
4310 if (!t)
4311 return t;
4312 if (TREE_CODE (t) == EXPR_STMT)
4313 t = TREE_OPERAND (t, 0);
4314 if (TREE_CODE (t) == CONVERT_EXPR
4315 && VOID_TYPE_P (TREE_TYPE (t)))
4316 t = TREE_OPERAND (t, 0);
4317 if (TREE_CODE (t) == INIT_EXPR)
4318 t = TREE_OPERAND (t, 1);
4319 if (instantiation_dependent_expression_p (t)
4320 || type_unknown_p (t)
4321 || BRACE_ENCLOSED_INITIALIZER_P (t)
4322 || !potential_static_init_expression (t))
4323 /* Don't try to evaluate it. */;
4324 else
4325 t = cxx_eval_outermost_constant_expr (t, true, false, decl);
4326 if (TREE_CODE (t) == TARGET_EXPR)
4328 tree init = TARGET_EXPR_INITIAL (t);
4329 if (TREE_CODE (init) == CONSTRUCTOR)
4330 t = init;
4332 return t;
4335 #if 0
4336 /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
4337 /* Return true if the object referred to by REF has automatic or thread
4338 local storage. */
4340 enum { ck_ok, ck_bad, ck_unknown };
4341 static int
4342 check_automatic_or_tls (tree ref)
4344 machine_mode mode;
4345 HOST_WIDE_INT bitsize, bitpos;
4346 tree offset;
4347 int volatilep = 0, unsignedp = 0;
4348 tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset,
4349 &mode, &unsignedp, &volatilep, false);
4350 duration_kind dk;
4352 /* If there isn't a decl in the middle, we don't know the linkage here,
4353 and this isn't a constant expression anyway. */
4354 if (!DECL_P (decl))
4355 return ck_unknown;
4356 dk = decl_storage_duration (decl);
4357 return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok;
4359 #endif
4361 /* Return true if T denotes a potentially constant expression. Issue
4362 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
4363 an lvalue-rvalue conversion is implied.
4365 C++0x [expr.const] used to say
4367 6 An expression is a potential constant expression if it is
4368 a constant expression where all occurrences of function
4369 parameters are replaced by arbitrary constant expressions
4370 of the appropriate type.
4372 2 A conditional expression is a constant expression unless it
4373 involves one of the following as a potentially evaluated
4374 subexpression (3.2), but subexpressions of logical AND (5.14),
4375 logical OR (5.15), and conditional (5.16) operations that are
4376 not evaluated are not considered. */
4378 static bool
4379 potential_constant_expression_1 (tree t, bool want_rval, bool strict,
4380 tsubst_flags_t flags)
4382 #define RECUR(T,RV) potential_constant_expression_1 ((T), (RV), strict, flags)
4383 enum { any = false, rval = true };
4384 int i;
4385 tree tmp;
4387 if (t == error_mark_node)
4388 return false;
4389 if (t == NULL_TREE)
4390 return true;
4391 if (TREE_THIS_VOLATILE (t) && !DECL_P (t))
4393 if (flags & tf_error)
4394 error ("expression %qE has side-effects", t);
4395 return false;
4397 if (CONSTANT_CLASS_P (t))
4398 return true;
4400 switch (TREE_CODE (t))
4402 case FUNCTION_DECL:
4403 case BASELINK:
4404 case TEMPLATE_DECL:
4405 case OVERLOAD:
4406 case TEMPLATE_ID_EXPR:
4407 case LABEL_DECL:
4408 case LABEL_EXPR:
4409 case CASE_LABEL_EXPR:
4410 case CONST_DECL:
4411 case SIZEOF_EXPR:
4412 case ALIGNOF_EXPR:
4413 case OFFSETOF_EXPR:
4414 case NOEXCEPT_EXPR:
4415 case TEMPLATE_PARM_INDEX:
4416 case TRAIT_EXPR:
4417 case IDENTIFIER_NODE:
4418 case USERDEF_LITERAL:
4419 /* We can see a FIELD_DECL in a pointer-to-member expression. */
4420 case FIELD_DECL:
4421 case PARM_DECL:
4422 case RESULT_DECL:
4423 case USING_DECL:
4424 case USING_STMT:
4425 case PLACEHOLDER_EXPR:
4426 case BREAK_STMT:
4427 case CONTINUE_STMT:
4428 case REQUIRES_EXPR:
4429 return true;
4431 case AGGR_INIT_EXPR:
4432 case CALL_EXPR:
4433 /* -- an invocation of a function other than a constexpr function
4434 or a constexpr constructor. */
4436 tree fun = get_function_named_in_call (t);
4437 const int nargs = call_expr_nargs (t);
4438 i = 0;
4440 if (fun == NULL_TREE)
4442 if (TREE_CODE (t) == CALL_EXPR
4443 && CALL_EXPR_FN (t) == NULL_TREE)
4444 switch (CALL_EXPR_IFN (t))
4446 /* These should be ignored, they are optimized away from
4447 constexpr functions. */
4448 case IFN_UBSAN_NULL:
4449 case IFN_UBSAN_BOUNDS:
4450 case IFN_UBSAN_VPTR:
4451 return true;
4452 default:
4453 break;
4455 /* fold_call_expr can't do anything with IFN calls. */
4456 if (flags & tf_error)
4457 error_at (EXPR_LOC_OR_LOC (t, input_location),
4458 "call to internal function");
4459 return false;
4461 if (is_overloaded_fn (fun))
4463 if (TREE_CODE (fun) == FUNCTION_DECL)
4465 if (builtin_valid_in_constant_expr_p (fun))
4466 return true;
4467 if (!DECL_DECLARED_CONSTEXPR_P (fun)
4468 /* Allow any built-in function; if the expansion
4469 isn't constant, we'll deal with that then. */
4470 && !is_builtin_fn (fun))
4472 if (flags & tf_error)
4474 error_at (EXPR_LOC_OR_LOC (t, input_location),
4475 "call to non-constexpr function %qD", fun);
4476 explain_invalid_constexpr_fn (fun);
4478 return false;
4480 /* A call to a non-static member function takes the address
4481 of the object as the first argument. But in a constant
4482 expression the address will be folded away, so look
4483 through it now. */
4484 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
4485 && !DECL_CONSTRUCTOR_P (fun))
4487 tree x = get_nth_callarg (t, 0);
4488 if (is_this_parameter (x))
4489 return true;
4490 else if (!RECUR (x, rval))
4491 return false;
4492 i = 1;
4495 else
4497 if (!RECUR (fun, true))
4498 return false;
4499 fun = get_first_fn (fun);
4501 /* Skip initial arguments to base constructors. */
4502 if (DECL_BASE_CONSTRUCTOR_P (fun))
4503 i = num_artificial_parms_for (fun);
4504 fun = DECL_ORIGIN (fun);
4506 else
4508 if (RECUR (fun, rval))
4509 /* Might end up being a constant function pointer. */;
4510 else
4511 return false;
4513 for (; i < nargs; ++i)
4515 tree x = get_nth_callarg (t, i);
4516 /* In a template, reference arguments haven't been converted to
4517 REFERENCE_TYPE and we might not even know if the parameter
4518 is a reference, so accept lvalue constants too. */
4519 bool rv = processing_template_decl ? any : rval;
4520 if (!RECUR (x, rv))
4521 return false;
4523 return true;
4526 case NON_LVALUE_EXPR:
4527 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
4528 -- an lvalue of integral type that refers to a non-volatile
4529 const variable or static data member initialized with
4530 constant expressions, or
4532 -- an lvalue of literal type that refers to non-volatile
4533 object defined with constexpr, or that refers to a
4534 sub-object of such an object; */
4535 return RECUR (TREE_OPERAND (t, 0), rval);
4537 case VAR_DECL:
4538 if (want_rval
4539 && !decl_constant_var_p (t)
4540 && (strict
4541 || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t))
4542 || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t))
4543 && !var_in_constexpr_fn (t)
4544 && !type_dependent_expression_p (t))
4546 if (flags & tf_error)
4547 non_const_var_error (t);
4548 return false;
4550 return true;
4552 case NOP_EXPR:
4553 case CONVERT_EXPR:
4554 case VIEW_CONVERT_EXPR:
4555 /* -- a reinterpret_cast. FIXME not implemented, and this rule
4556 may change to something more specific to type-punning (DR 1312). */
4558 tree from = TREE_OPERAND (t, 0);
4559 if (POINTER_TYPE_P (TREE_TYPE (t))
4560 && TREE_CODE (from) == INTEGER_CST
4561 && !integer_zerop (from))
4563 if (flags & tf_error)
4564 error_at (EXPR_LOC_OR_LOC (t, input_location),
4565 "reinterpret_cast from integer to pointer");
4566 return false;
4568 return (RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR));
4571 case ADDR_EXPR:
4572 /* -- a unary operator & that is applied to an lvalue that
4573 designates an object with thread or automatic storage
4574 duration; */
4575 t = TREE_OPERAND (t, 0);
4577 if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t))
4578 /* A pointer-to-member constant. */
4579 return true;
4581 #if 0
4582 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
4583 any checking here, as we might dereference the pointer later. If
4584 we remove this code, also remove check_automatic_or_tls. */
4585 i = check_automatic_or_tls (t);
4586 if (i == ck_ok)
4587 return true;
4588 if (i == ck_bad)
4590 if (flags & tf_error)
4591 error ("address-of an object %qE with thread local or "
4592 "automatic storage is not a constant expression", t);
4593 return false;
4595 #endif
4596 return RECUR (t, any);
4598 case COMPONENT_REF:
4599 case BIT_FIELD_REF:
4600 case ARROW_EXPR:
4601 case OFFSET_REF:
4602 /* -- a class member access unless its postfix-expression is
4603 of literal type or of pointer to literal type. */
4604 /* This test would be redundant, as it follows from the
4605 postfix-expression being a potential constant expression. */
4606 if (type_unknown_p (t))
4607 return true;
4608 return RECUR (TREE_OPERAND (t, 0), want_rval);
4610 case EXPR_PACK_EXPANSION:
4611 return RECUR (PACK_EXPANSION_PATTERN (t), want_rval);
4613 case INDIRECT_REF:
4615 tree x = TREE_OPERAND (t, 0);
4616 STRIP_NOPS (x);
4617 if (is_this_parameter (x))
4619 if (DECL_CONTEXT (x)
4620 && !DECL_DECLARED_CONSTEXPR_P (DECL_CONTEXT (x)))
4622 if (flags & tf_error)
4623 error ("use of %<this%> in a constant expression");
4624 return false;
4626 return true;
4628 return RECUR (x, rval);
4631 case STATEMENT_LIST:
4633 tree_stmt_iterator i;
4634 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
4636 if (!RECUR (tsi_stmt (i), any))
4637 return false;
4639 return true;
4641 break;
4643 case MODIFY_EXPR:
4644 if (cxx_dialect < cxx14)
4645 goto fail;
4646 if (!RECUR (TREE_OPERAND (t, 0), any))
4647 return false;
4648 if (!RECUR (TREE_OPERAND (t, 1), rval))
4649 return false;
4650 return true;
4652 case MODOP_EXPR:
4653 if (cxx_dialect < cxx14)
4654 goto fail;
4655 if (!RECUR (TREE_OPERAND (t, 0), rval))
4656 return false;
4657 if (!RECUR (TREE_OPERAND (t, 2), rval))
4658 return false;
4659 return true;
4661 case DO_STMT:
4662 if (!RECUR (DO_COND (t), rval))
4663 return false;
4664 if (!RECUR (DO_BODY (t), any))
4665 return false;
4666 return true;
4668 case FOR_STMT:
4669 if (!RECUR (FOR_INIT_STMT (t), any))
4670 return false;
4671 if (!RECUR (FOR_COND (t), rval))
4672 return false;
4673 if (!RECUR (FOR_EXPR (t), any))
4674 return false;
4675 if (!RECUR (FOR_BODY (t), any))
4676 return false;
4677 return true;
4679 case WHILE_STMT:
4680 if (!RECUR (WHILE_COND (t), rval))
4681 return false;
4682 if (!RECUR (WHILE_BODY (t), any))
4683 return false;
4684 return true;
4686 case SWITCH_STMT:
4687 if (!RECUR (SWITCH_STMT_COND (t), rval))
4688 return false;
4689 /* FIXME we don't check SWITCH_STMT_BODY currently, because even
4690 unreachable labels would be checked. */
4691 return true;
4693 case STMT_EXPR:
4694 return RECUR (STMT_EXPR_STMT (t), rval);
4696 case LAMBDA_EXPR:
4697 case DYNAMIC_CAST_EXPR:
4698 case PSEUDO_DTOR_EXPR:
4699 case NEW_EXPR:
4700 case VEC_NEW_EXPR:
4701 case DELETE_EXPR:
4702 case VEC_DELETE_EXPR:
4703 case THROW_EXPR:
4704 case OMP_ATOMIC:
4705 case OMP_ATOMIC_READ:
4706 case OMP_ATOMIC_CAPTURE_OLD:
4707 case OMP_ATOMIC_CAPTURE_NEW:
4708 /* GCC internal stuff. */
4709 case VA_ARG_EXPR:
4710 case OBJ_TYPE_REF:
4711 case TRANSACTION_EXPR:
4712 case ASM_EXPR:
4713 case AT_ENCODE_EXPR:
4714 fail:
4715 if (flags & tf_error)
4716 error ("expression %qE is not a constant-expression", t);
4717 return false;
4719 case TYPEID_EXPR:
4720 /* -- a typeid expression whose operand is of polymorphic
4721 class type; */
4723 tree e = TREE_OPERAND (t, 0);
4724 if (!TYPE_P (e) && !type_dependent_expression_p (e)
4725 && TYPE_POLYMORPHIC_P (TREE_TYPE (e)))
4727 if (flags & tf_error)
4728 error ("typeid-expression is not a constant expression "
4729 "because %qE is of polymorphic type", e);
4730 return false;
4732 return true;
4735 case MINUS_EXPR:
4736 want_rval = true;
4737 goto binary;
4739 case LT_EXPR:
4740 case LE_EXPR:
4741 case GT_EXPR:
4742 case GE_EXPR:
4743 case EQ_EXPR:
4744 case NE_EXPR:
4745 want_rval = true;
4746 goto binary;
4748 case PREINCREMENT_EXPR:
4749 case POSTINCREMENT_EXPR:
4750 case PREDECREMENT_EXPR:
4751 case POSTDECREMENT_EXPR:
4752 if (cxx_dialect < cxx14)
4753 goto fail;
4754 goto unary;
4756 case BIT_NOT_EXPR:
4757 /* A destructor. */
4758 if (TYPE_P (TREE_OPERAND (t, 0)))
4759 return true;
4760 /* else fall through. */
4762 case REALPART_EXPR:
4763 case IMAGPART_EXPR:
4764 case CONJ_EXPR:
4765 case SAVE_EXPR:
4766 case FIX_TRUNC_EXPR:
4767 case FLOAT_EXPR:
4768 case NEGATE_EXPR:
4769 case ABS_EXPR:
4770 case TRUTH_NOT_EXPR:
4771 case FIXED_CONVERT_EXPR:
4772 case UNARY_PLUS_EXPR:
4773 case UNARY_LEFT_FOLD_EXPR:
4774 case UNARY_RIGHT_FOLD_EXPR:
4775 unary:
4776 return RECUR (TREE_OPERAND (t, 0), rval);
4778 case CAST_EXPR:
4779 case CONST_CAST_EXPR:
4780 case STATIC_CAST_EXPR:
4781 case REINTERPRET_CAST_EXPR:
4782 case IMPLICIT_CONV_EXPR:
4783 if (cxx_dialect < cxx11
4784 && !dependent_type_p (TREE_TYPE (t))
4785 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t)))
4786 /* In C++98, a conversion to non-integral type can't be part of a
4787 constant expression. */
4789 if (flags & tf_error)
4790 error ("cast to non-integral type %qT in a constant expression",
4791 TREE_TYPE (t));
4792 return false;
4795 return (RECUR (TREE_OPERAND (t, 0),
4796 TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE));
4798 case BIND_EXPR:
4799 return RECUR (BIND_EXPR_BODY (t), want_rval);
4801 case WITH_CLEANUP_EXPR:
4802 case CLEANUP_POINT_EXPR:
4803 case MUST_NOT_THROW_EXPR:
4804 case TRY_CATCH_EXPR:
4805 case TRY_BLOCK:
4806 case EH_SPEC_BLOCK:
4807 case EXPR_STMT:
4808 case PAREN_EXPR:
4809 case DECL_EXPR:
4810 case NON_DEPENDENT_EXPR:
4811 /* For convenience. */
4812 case RETURN_EXPR:
4813 return RECUR (TREE_OPERAND (t, 0), want_rval);
4815 case TRY_FINALLY_EXPR:
4816 return (RECUR (TREE_OPERAND (t, 0), want_rval)
4817 && RECUR (TREE_OPERAND (t, 1), any));
4819 case SCOPE_REF:
4820 return RECUR (TREE_OPERAND (t, 1), want_rval);
4822 case TARGET_EXPR:
4823 if (!literal_type_p (TREE_TYPE (t)))
4825 if (flags & tf_error)
4827 error ("temporary of non-literal type %qT in a "
4828 "constant expression", TREE_TYPE (t));
4829 explain_non_literal_class (TREE_TYPE (t));
4831 return false;
4833 case INIT_EXPR:
4834 return RECUR (TREE_OPERAND (t, 1), rval);
4836 case CONSTRUCTOR:
4838 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
4839 constructor_elt *ce;
4840 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
4841 if (!RECUR (ce->value, want_rval))
4842 return false;
4843 return true;
4846 case TREE_LIST:
4848 gcc_assert (TREE_PURPOSE (t) == NULL_TREE
4849 || DECL_P (TREE_PURPOSE (t)));
4850 if (!RECUR (TREE_VALUE (t), want_rval))
4851 return false;
4852 if (TREE_CHAIN (t) == NULL_TREE)
4853 return true;
4854 return RECUR (TREE_CHAIN (t), want_rval);
4857 case TRUNC_DIV_EXPR:
4858 case CEIL_DIV_EXPR:
4859 case FLOOR_DIV_EXPR:
4860 case ROUND_DIV_EXPR:
4861 case TRUNC_MOD_EXPR:
4862 case CEIL_MOD_EXPR:
4863 case ROUND_MOD_EXPR:
4865 tree denom = TREE_OPERAND (t, 1);
4866 if (!RECUR (denom, rval))
4867 return false;
4868 /* We can't call cxx_eval_outermost_constant_expr on an expression
4869 that hasn't been through instantiate_non_dependent_expr yet. */
4870 if (!processing_template_decl)
4871 denom = cxx_eval_outermost_constant_expr (denom, true);
4872 if (integer_zerop (denom))
4874 if (flags & tf_error)
4875 error ("division by zero is not a constant-expression");
4876 return false;
4878 else
4880 want_rval = true;
4881 return RECUR (TREE_OPERAND (t, 0), want_rval);
4885 case COMPOUND_EXPR:
4887 /* check_return_expr sometimes wraps a TARGET_EXPR in a
4888 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
4889 introduced by build_call_a. */
4890 tree op0 = TREE_OPERAND (t, 0);
4891 tree op1 = TREE_OPERAND (t, 1);
4892 STRIP_NOPS (op1);
4893 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
4894 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
4895 return RECUR (op0, want_rval);
4896 else
4897 goto binary;
4900 /* If the first operand is the non-short-circuit constant, look at
4901 the second operand; otherwise we only care about the first one for
4902 potentiality. */
4903 case TRUTH_AND_EXPR:
4904 case TRUTH_ANDIF_EXPR:
4905 tmp = boolean_true_node;
4906 goto truth;
4907 case TRUTH_OR_EXPR:
4908 case TRUTH_ORIF_EXPR:
4909 tmp = boolean_false_node;
4910 truth:
4912 tree op = TREE_OPERAND (t, 0);
4913 if (!RECUR (op, rval))
4914 return false;
4915 if (!processing_template_decl)
4916 op = cxx_eval_outermost_constant_expr (op, true);
4917 if (tree_int_cst_equal (op, tmp))
4918 return RECUR (TREE_OPERAND (t, 1), rval);
4919 else
4920 return true;
4923 case PLUS_EXPR:
4924 case MULT_EXPR:
4925 case POINTER_PLUS_EXPR:
4926 case RDIV_EXPR:
4927 case EXACT_DIV_EXPR:
4928 case MIN_EXPR:
4929 case MAX_EXPR:
4930 case LSHIFT_EXPR:
4931 case RSHIFT_EXPR:
4932 case LROTATE_EXPR:
4933 case RROTATE_EXPR:
4934 case BIT_IOR_EXPR:
4935 case BIT_XOR_EXPR:
4936 case BIT_AND_EXPR:
4937 case TRUTH_XOR_EXPR:
4938 case UNORDERED_EXPR:
4939 case ORDERED_EXPR:
4940 case UNLT_EXPR:
4941 case UNLE_EXPR:
4942 case UNGT_EXPR:
4943 case UNGE_EXPR:
4944 case UNEQ_EXPR:
4945 case LTGT_EXPR:
4946 case RANGE_EXPR:
4947 case COMPLEX_EXPR:
4948 want_rval = true;
4949 /* Fall through. */
4950 case ARRAY_REF:
4951 case ARRAY_RANGE_REF:
4952 case MEMBER_REF:
4953 case DOTSTAR_EXPR:
4954 case MEM_REF:
4955 case BINARY_LEFT_FOLD_EXPR:
4956 case BINARY_RIGHT_FOLD_EXPR:
4957 binary:
4958 for (i = 0; i < 2; ++i)
4959 if (!RECUR (TREE_OPERAND (t, i), want_rval))
4960 return false;
4961 return true;
4963 case CILK_SYNC_STMT:
4964 case CILK_SPAWN_STMT:
4965 case ARRAY_NOTATION_REF:
4966 return false;
4968 case FMA_EXPR:
4969 case VEC_PERM_EXPR:
4970 for (i = 0; i < 3; ++i)
4971 if (!RECUR (TREE_OPERAND (t, i), true))
4972 return false;
4973 return true;
4975 case COND_EXPR:
4976 if (COND_EXPR_IS_VEC_DELETE (t))
4978 if (flags & tf_error)
4979 error_at (location_of (t),
4980 "%<delete[]%> is not a constant-expression");
4981 return false;
4983 /* Fall through. */
4984 case IF_STMT:
4985 case VEC_COND_EXPR:
4986 /* If the condition is a known constant, we know which of the legs we
4987 care about; otherwise we only require that the condition and
4988 either of the legs be potentially constant. */
4989 tmp = TREE_OPERAND (t, 0);
4990 if (!RECUR (tmp, rval))
4991 return false;
4992 if (!processing_template_decl)
4993 tmp = cxx_eval_outermost_constant_expr (tmp, true);
4994 if (integer_zerop (tmp))
4995 return RECUR (TREE_OPERAND (t, 2), want_rval);
4996 else if (TREE_CODE (tmp) == INTEGER_CST)
4997 return RECUR (TREE_OPERAND (t, 1), want_rval);
4998 for (i = 1; i < 3; ++i)
4999 if (potential_constant_expression_1 (TREE_OPERAND (t, i),
5000 want_rval, strict, tf_none))
5001 return true;
5002 if (flags & tf_error)
5003 error ("expression %qE is not a constant-expression", t);
5004 return false;
5006 case VEC_INIT_EXPR:
5007 if (VEC_INIT_EXPR_IS_CONSTEXPR (t))
5008 return true;
5009 if (flags & tf_error)
5011 error ("non-constant array initialization");
5012 diagnose_non_constexpr_vec_init (t);
5014 return false;
5016 case TYPE_DECL:
5017 case TAG_DEFN:
5018 /* We can see these in statement-expressions. */
5019 return true;
5021 case EMPTY_CLASS_EXPR:
5022 return false;
5024 default:
5025 if (objc_is_property_ref (t))
5026 return false;
5028 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t)));
5029 gcc_unreachable();
5030 return false;
5032 #undef RECUR
5035 /* The main entry point to the above. */
5037 bool
5038 potential_constant_expression (tree t)
5040 return potential_constant_expression_1 (t, false, true, tf_none);
5043 bool
5044 potential_static_init_expression (tree t)
5046 return potential_constant_expression_1 (t, false, false, tf_none);
5049 /* As above, but require a constant rvalue. */
5051 bool
5052 potential_rvalue_constant_expression (tree t)
5054 return potential_constant_expression_1 (t, true, true, tf_none);
5057 /* Like above, but complain about non-constant expressions. */
5059 bool
5060 require_potential_constant_expression (tree t)
5062 return potential_constant_expression_1 (t, false, true, tf_warning_or_error);
5065 /* Cross product of the above. */
5067 bool
5068 require_potential_rvalue_constant_expression (tree t)
5070 return potential_constant_expression_1 (t, true, true, tf_warning_or_error);
5073 #include "gt-cp-constexpr.h"