2014-12-12 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / cp / constexpr.c
blob9426d8558e2398974705d8fce60d6b8ce29bf626
1 /* Perform the semantic phase of constexpr parsing, i.e., the process of
2 building tree structure, checking semantic consistency, and
3 building RTL. These routines are used both during actual parsing
4 and during the instantiation of template functions.
6 Copyright (C) 1998-2014 Free Software Foundation, Inc.
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it
11 under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
13 any later version.
15 GCC is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tree.h"
28 #include "varasm.h"
29 #include "cp-tree.h"
30 #include "c-family/c-objc.h"
31 #include "tree-iterator.h"
32 #include "gimplify.h"
33 #include "builtins.h"
34 #include "tree-inline.h"
35 #include "ubsan.h"
37 static bool verify_constant (tree, bool, bool *, bool *);
38 #define VERIFY_CONSTANT(X) \
39 do { \
40 if (verify_constant ((X), ctx->quiet, non_constant_p, overflow_p)) \
41 return t; \
42 } while (0)
44 /* Returns true iff FUN is an instantiation of a constexpr function
45 template or a defaulted constexpr function. */
47 bool
48 is_instantiation_of_constexpr (tree fun)
50 return ((DECL_TEMPLOID_INSTANTIATION (fun)
51 && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun)))
52 || (DECL_DEFAULTED_FN (fun)
53 && DECL_DECLARED_CONSTEXPR_P (fun)));
56 /* Return true if T is a literal type. */
58 bool
59 literal_type_p (tree t)
61 if (SCALAR_TYPE_P (t)
62 || TREE_CODE (t) == VECTOR_TYPE
63 || TREE_CODE (t) == REFERENCE_TYPE
64 || (VOID_TYPE_P (t) && cxx_dialect >= cxx14))
65 return true;
66 if (CLASS_TYPE_P (t))
68 t = complete_type (t);
69 gcc_assert (COMPLETE_TYPE_P (t) || errorcount);
70 return CLASSTYPE_LITERAL_P (t);
72 if (TREE_CODE (t) == ARRAY_TYPE)
73 return literal_type_p (strip_array_types (t));
74 return false;
77 /* If DECL is a variable declared `constexpr', require its type
78 be literal. Return the DECL if OK, otherwise NULL. */
80 tree
81 ensure_literal_type_for_constexpr_object (tree decl)
83 tree type = TREE_TYPE (decl);
84 if (VAR_P (decl)
85 && (DECL_DECLARED_CONSTEXPR_P (decl)
86 || var_in_constexpr_fn (decl))
87 && !processing_template_decl)
89 tree stype = strip_array_types (type);
90 if (CLASS_TYPE_P (stype) && !COMPLETE_TYPE_P (complete_type (stype)))
91 /* Don't complain here, we'll complain about incompleteness
92 when we try to initialize the variable. */;
93 else if (!literal_type_p (type))
95 if (DECL_DECLARED_CONSTEXPR_P (decl))
96 error ("the type %qT of constexpr variable %qD is not literal",
97 type, decl);
98 else
100 error ("variable %qD of non-literal type %qT in %<constexpr%> "
101 "function", decl, type);
102 cp_function_chain->invalid_constexpr = true;
104 explain_non_literal_class (type);
105 return NULL;
108 return decl;
111 /* Representation of entries in the constexpr function definition table. */
113 struct GTY((for_user)) constexpr_fundef {
114 tree decl;
115 tree body;
118 struct constexpr_fundef_hasher : ggc_hasher<constexpr_fundef *>
120 static hashval_t hash (constexpr_fundef *);
121 static bool equal (constexpr_fundef *, constexpr_fundef *);
124 /* This table holds all constexpr function definitions seen in
125 the current translation unit. */
127 static GTY (()) hash_table<constexpr_fundef_hasher> *constexpr_fundef_table;
129 /* Utility function used for managing the constexpr function table.
130 Return true if the entries pointed to by P and Q are for the
131 same constexpr function. */
133 inline bool
134 constexpr_fundef_hasher::equal (constexpr_fundef *lhs, constexpr_fundef *rhs)
136 return lhs->decl == rhs->decl;
139 /* Utility function used for managing the constexpr function table.
140 Return a hash value for the entry pointed to by Q. */
142 inline hashval_t
143 constexpr_fundef_hasher::hash (constexpr_fundef *fundef)
145 return DECL_UID (fundef->decl);
148 /* Return a previously saved definition of function FUN. */
150 static constexpr_fundef *
151 retrieve_constexpr_fundef (tree fun)
153 constexpr_fundef fundef = { NULL, NULL };
154 if (constexpr_fundef_table == NULL)
155 return NULL;
157 fundef.decl = fun;
158 return constexpr_fundef_table->find (&fundef);
161 /* Check whether the parameter and return types of FUN are valid for a
162 constexpr function, and complain if COMPLAIN. */
164 static bool
165 is_valid_constexpr_fn (tree fun, bool complain)
167 bool ret = true;
169 if (DECL_INHERITED_CTOR_BASE (fun)
170 && TREE_CODE (fun) == TEMPLATE_DECL)
172 ret = false;
173 if (complain)
174 error ("inherited constructor %qD is not constexpr",
175 get_inherited_ctor (fun));
177 else
179 for (tree parm = FUNCTION_FIRST_USER_PARM (fun);
180 parm != NULL_TREE; parm = TREE_CHAIN (parm))
181 if (!literal_type_p (TREE_TYPE (parm)))
183 ret = false;
184 if (complain)
186 error ("invalid type for parameter %d of constexpr "
187 "function %q+#D", DECL_PARM_INDEX (parm), fun);
188 explain_non_literal_class (TREE_TYPE (parm));
193 if (!DECL_CONSTRUCTOR_P (fun))
195 tree rettype = TREE_TYPE (TREE_TYPE (fun));
196 if (!literal_type_p (rettype))
198 ret = false;
199 if (complain)
201 error ("invalid return type %qT of constexpr function %q+D",
202 rettype, fun);
203 explain_non_literal_class (rettype);
207 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
208 && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun)))
210 ret = false;
211 if (complain)
213 error ("enclosing class of constexpr non-static member "
214 "function %q+#D is not a literal type", fun);
215 explain_non_literal_class (DECL_CONTEXT (fun));
219 else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun)))
221 ret = false;
222 if (complain)
223 error ("%q#T has virtual base classes", DECL_CONTEXT (fun));
226 return ret;
229 /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF
230 for a member of an anonymous aggregate, INIT is the initializer for that
231 member, and VEC_OUTER is the vector of constructor elements for the class
232 whose constructor we are processing. Add the initializer to the vector
233 and return true to indicate success. */
235 static bool
236 build_anon_member_initialization (tree member, tree init,
237 vec<constructor_elt, va_gc> **vec_outer)
239 /* MEMBER presents the relevant fields from the inside out, but we need
240 to build up the initializer from the outside in so that we can reuse
241 previously built CONSTRUCTORs if this is, say, the second field in an
242 anonymous struct. So we use a vec as a stack. */
243 auto_vec<tree, 2> fields;
246 fields.safe_push (TREE_OPERAND (member, 1));
247 member = TREE_OPERAND (member, 0);
249 while (ANON_AGGR_TYPE_P (TREE_TYPE (member))
250 && TREE_CODE (member) == COMPONENT_REF);
252 /* VEC has the constructor elements vector for the context of FIELD.
253 If FIELD is an anonymous aggregate, we will push inside it. */
254 vec<constructor_elt, va_gc> **vec = vec_outer;
255 tree field;
256 while (field = fields.pop(),
257 ANON_AGGR_TYPE_P (TREE_TYPE (field)))
259 tree ctor;
260 /* If there is already an outer constructor entry for the anonymous
261 aggregate FIELD, use it; otherwise, insert one. */
262 if (vec_safe_is_empty (*vec)
263 || (*vec)->last().index != field)
265 ctor = build_constructor (TREE_TYPE (field), NULL);
266 CONSTRUCTOR_APPEND_ELT (*vec, field, ctor);
268 else
269 ctor = (*vec)->last().value;
270 vec = &CONSTRUCTOR_ELTS (ctor);
273 /* Now we're at the innermost field, the one that isn't an anonymous
274 aggregate. Add its initializer to the CONSTRUCTOR and we're done. */
275 gcc_assert (fields.is_empty());
276 CONSTRUCTOR_APPEND_ELT (*vec, field, init);
278 return true;
281 /* Subroutine of build_constexpr_constructor_member_initializers.
282 The expression tree T represents a data member initialization
283 in a (constexpr) constructor definition. Build a pairing of
284 the data member with its initializer, and prepend that pair
285 to the existing initialization pair INITS. */
287 static bool
288 build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec)
290 tree member, init;
291 if (TREE_CODE (t) == CLEANUP_POINT_EXPR)
292 t = TREE_OPERAND (t, 0);
293 if (TREE_CODE (t) == EXPR_STMT)
294 t = TREE_OPERAND (t, 0);
295 if (t == error_mark_node)
296 return false;
297 if (TREE_CODE (t) == STATEMENT_LIST)
299 tree_stmt_iterator i;
300 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
302 if (! build_data_member_initialization (tsi_stmt (i), vec))
303 return false;
305 return true;
307 if (TREE_CODE (t) == CLEANUP_STMT)
309 /* We can't see a CLEANUP_STMT in a constructor for a literal class,
310 but we can in a constexpr constructor for a non-literal class. Just
311 ignore it; either all the initialization will be constant, in which
312 case the cleanup can't run, or it can't be constexpr.
313 Still recurse into CLEANUP_BODY. */
314 return build_data_member_initialization (CLEANUP_BODY (t), vec);
316 if (TREE_CODE (t) == CONVERT_EXPR)
317 t = TREE_OPERAND (t, 0);
318 if (TREE_CODE (t) == INIT_EXPR
319 /* vptr initialization shows up as a MODIFY_EXPR. In C++14 we only
320 use what this function builds for cx_check_missing_mem_inits, and
321 assignment in the ctor body doesn't count. */
322 || (cxx_dialect < cxx14 && TREE_CODE (t) == MODIFY_EXPR))
324 member = TREE_OPERAND (t, 0);
325 init = break_out_target_exprs (TREE_OPERAND (t, 1));
327 else if (TREE_CODE (t) == CALL_EXPR)
329 tree fn = get_callee_fndecl (t);
330 if (!fn || !DECL_CONSTRUCTOR_P (fn))
331 /* We're only interested in calls to subobject constructors. */
332 return true;
333 member = CALL_EXPR_ARG (t, 0);
334 /* We don't use build_cplus_new here because it complains about
335 abstract bases. Leaving the call unwrapped means that it has the
336 wrong type, but cxx_eval_constant_expression doesn't care. */
337 init = break_out_target_exprs (t);
339 else if (TREE_CODE (t) == BIND_EXPR)
340 return build_data_member_initialization (BIND_EXPR_BODY (t), vec);
341 else
342 /* Don't add anything else to the CONSTRUCTOR. */
343 return true;
344 if (INDIRECT_REF_P (member))
345 member = TREE_OPERAND (member, 0);
346 if (TREE_CODE (member) == NOP_EXPR)
348 tree op = member;
349 STRIP_NOPS (op);
350 if (TREE_CODE (op) == ADDR_EXPR)
352 gcc_assert (same_type_ignoring_top_level_qualifiers_p
353 (TREE_TYPE (TREE_TYPE (op)),
354 TREE_TYPE (TREE_TYPE (member))));
355 /* Initializing a cv-qualified member; we need to look through
356 the const_cast. */
357 member = op;
359 else if (op == current_class_ptr
360 && (same_type_ignoring_top_level_qualifiers_p
361 (TREE_TYPE (TREE_TYPE (member)),
362 current_class_type)))
363 /* Delegating constructor. */
364 member = op;
365 else
367 /* This is an initializer for an empty base; keep it for now so
368 we can check it in cxx_eval_bare_aggregate. */
369 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member))));
372 if (TREE_CODE (member) == ADDR_EXPR)
373 member = TREE_OPERAND (member, 0);
374 if (TREE_CODE (member) == COMPONENT_REF)
376 tree aggr = TREE_OPERAND (member, 0);
377 if (TREE_CODE (aggr) != COMPONENT_REF)
378 /* Normal member initialization. */
379 member = TREE_OPERAND (member, 1);
380 else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr)))
381 /* Initializing a member of an anonymous union. */
382 return build_anon_member_initialization (member, init, vec);
383 else
384 /* We're initializing a vtable pointer in a base. Leave it as
385 COMPONENT_REF so we remember the path to get to the vfield. */
386 gcc_assert (TREE_TYPE (member) == vtbl_ptr_type_node);
389 CONSTRUCTOR_APPEND_ELT (*vec, member, init);
390 return true;
393 /* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval.
394 In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a
395 BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */
397 static bool
398 check_constexpr_bind_expr_vars (tree t)
400 gcc_assert (TREE_CODE (t) == BIND_EXPR);
402 for (tree var = BIND_EXPR_VARS (t); var; var = DECL_CHAIN (var))
403 if (TREE_CODE (var) == TYPE_DECL
404 && DECL_IMPLICIT_TYPEDEF_P (var))
405 return false;
406 return true;
409 /* Subroutine of check_constexpr_ctor_body. */
411 static bool
412 check_constexpr_ctor_body_1 (tree last, tree list)
414 switch (TREE_CODE (list))
416 case DECL_EXPR:
417 if (TREE_CODE (DECL_EXPR_DECL (list)) == USING_DECL)
418 return true;
419 return false;
421 case CLEANUP_POINT_EXPR:
422 return check_constexpr_ctor_body (last, TREE_OPERAND (list, 0),
423 /*complain=*/false);
425 case BIND_EXPR:
426 if (!check_constexpr_bind_expr_vars (list)
427 || !check_constexpr_ctor_body (last, BIND_EXPR_BODY (list),
428 /*complain=*/false))
429 return false;
430 return true;
432 case USING_STMT:
433 case STATIC_ASSERT:
434 return true;
436 default:
437 return false;
441 /* Make sure that there are no statements after LAST in the constructor
442 body represented by LIST. */
444 bool
445 check_constexpr_ctor_body (tree last, tree list, bool complain)
447 /* C++14 doesn't require a constexpr ctor to have an empty body. */
448 if (cxx_dialect >= cxx14)
449 return true;
451 bool ok = true;
452 if (TREE_CODE (list) == STATEMENT_LIST)
454 tree_stmt_iterator i = tsi_last (list);
455 for (; !tsi_end_p (i); tsi_prev (&i))
457 tree t = tsi_stmt (i);
458 if (t == last)
459 break;
460 if (!check_constexpr_ctor_body_1 (last, t))
462 ok = false;
463 break;
467 else if (list != last
468 && !check_constexpr_ctor_body_1 (last, list))
469 ok = false;
470 if (!ok)
472 if (complain)
473 error ("constexpr constructor does not have empty body");
474 DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false;
476 return ok;
479 /* V is a vector of constructor elements built up for the base and member
480 initializers of a constructor for TYPE. They need to be in increasing
481 offset order, which they might not be yet if TYPE has a primary base
482 which is not first in the base-clause or a vptr and at least one base
483 all of which are non-primary. */
485 static vec<constructor_elt, va_gc> *
486 sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v)
488 tree pri = CLASSTYPE_PRIMARY_BINFO (type);
489 tree field_type;
490 unsigned i;
491 constructor_elt *ce;
493 if (pri)
494 field_type = BINFO_TYPE (pri);
495 else if (TYPE_CONTAINS_VPTR_P (type))
496 field_type = vtbl_ptr_type_node;
497 else
498 return v;
500 /* Find the element for the primary base or vptr and move it to the
501 beginning of the vec. */
502 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
503 if (TREE_TYPE (ce->index) == field_type)
504 break;
506 if (i > 0 && i < vec_safe_length (v))
508 vec<constructor_elt, va_gc> &vref = *v;
509 constructor_elt elt = vref[i];
510 for (; i > 0; --i)
511 vref[i] = vref[i-1];
512 vref[0] = elt;
515 return v;
518 /* Build compile-time evalable representations of member-initializer list
519 for a constexpr constructor. */
521 static tree
522 build_constexpr_constructor_member_initializers (tree type, tree body)
524 vec<constructor_elt, va_gc> *vec = NULL;
525 bool ok = true;
526 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR
527 || TREE_CODE (body) == EH_SPEC_BLOCK)
528 body = TREE_OPERAND (body, 0);
529 if (TREE_CODE (body) == STATEMENT_LIST)
530 body = STATEMENT_LIST_HEAD (body)->stmt;
531 body = BIND_EXPR_BODY (body);
532 if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
534 body = TREE_OPERAND (body, 0);
535 if (TREE_CODE (body) == EXPR_STMT)
536 body = TREE_OPERAND (body, 0);
537 if (TREE_CODE (body) == INIT_EXPR
538 && (same_type_ignoring_top_level_qualifiers_p
539 (TREE_TYPE (TREE_OPERAND (body, 0)),
540 current_class_type)))
542 /* Trivial copy. */
543 return TREE_OPERAND (body, 1);
545 ok = build_data_member_initialization (body, &vec);
547 else if (TREE_CODE (body) == STATEMENT_LIST)
549 tree_stmt_iterator i;
550 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
552 ok = build_data_member_initialization (tsi_stmt (i), &vec);
553 if (!ok)
554 break;
557 else if (TREE_CODE (body) == TRY_BLOCK)
559 error ("body of %<constexpr%> constructor cannot be "
560 "a function-try-block");
561 return error_mark_node;
563 else if (EXPR_P (body))
564 ok = build_data_member_initialization (body, &vec);
565 else
566 gcc_assert (errorcount > 0);
567 if (ok)
569 if (vec_safe_length (vec) > 0)
571 /* In a delegating constructor, return the target. */
572 constructor_elt *ce = &(*vec)[0];
573 if (ce->index == current_class_ptr)
575 body = ce->value;
576 vec_free (vec);
577 return body;
580 vec = sort_constexpr_mem_initializers (type, vec);
581 return build_constructor (type, vec);
583 else
584 return error_mark_node;
587 /* Subroutine of register_constexpr_fundef. BODY is the body of a function
588 declared to be constexpr, or a sub-statement thereof. Returns the
589 return value if suitable, error_mark_node for a statement not allowed in
590 a constexpr function, or NULL_TREE if no return value was found. */
592 static tree
593 constexpr_fn_retval (tree body)
595 switch (TREE_CODE (body))
597 case STATEMENT_LIST:
599 tree_stmt_iterator i;
600 tree expr = NULL_TREE;
601 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
603 tree s = constexpr_fn_retval (tsi_stmt (i));
604 if (s == error_mark_node)
605 return error_mark_node;
606 else if (s == NULL_TREE)
607 /* Keep iterating. */;
608 else if (expr)
609 /* Multiple return statements. */
610 return error_mark_node;
611 else
612 expr = s;
614 return expr;
617 case RETURN_EXPR:
618 return break_out_target_exprs (TREE_OPERAND (body, 0));
620 case DECL_EXPR:
622 tree decl = DECL_EXPR_DECL (body);
623 if (TREE_CODE (decl) == USING_DECL
624 /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */
625 || DECL_ARTIFICIAL (decl))
626 return NULL_TREE;
627 return error_mark_node;
630 case CLEANUP_POINT_EXPR:
631 return constexpr_fn_retval (TREE_OPERAND (body, 0));
633 case BIND_EXPR:
634 if (!check_constexpr_bind_expr_vars (body))
635 return error_mark_node;
636 return constexpr_fn_retval (BIND_EXPR_BODY (body));
638 case USING_STMT:
639 return NULL_TREE;
641 default:
642 return error_mark_node;
646 /* Subroutine of register_constexpr_fundef. BODY is the DECL_SAVED_TREE of
647 FUN; do the necessary transformations to turn it into a single expression
648 that we can store in the hash table. */
650 static tree
651 massage_constexpr_body (tree fun, tree body)
653 if (DECL_CONSTRUCTOR_P (fun))
654 body = build_constexpr_constructor_member_initializers
655 (DECL_CONTEXT (fun), body);
656 else if (cxx_dialect < cxx14)
658 if (TREE_CODE (body) == EH_SPEC_BLOCK)
659 body = EH_SPEC_STMTS (body);
660 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
661 body = TREE_OPERAND (body, 0);
662 body = constexpr_fn_retval (body);
664 return body;
667 /* FUN is a constexpr constructor with massaged body BODY. Return true
668 if some bases/fields are uninitialized, and complain if COMPLAIN. */
670 static bool
671 cx_check_missing_mem_inits (tree fun, tree body, bool complain)
673 bool bad;
674 tree field;
675 unsigned i, nelts;
676 tree ctype;
678 if (TREE_CODE (body) != CONSTRUCTOR)
679 return false;
681 nelts = CONSTRUCTOR_NELTS (body);
682 ctype = DECL_CONTEXT (fun);
683 field = TYPE_FIELDS (ctype);
685 if (TREE_CODE (ctype) == UNION_TYPE)
687 if (nelts == 0 && next_initializable_field (field))
689 if (complain)
690 error ("%<constexpr%> constructor for union %qT must "
691 "initialize exactly one non-static data member", ctype);
692 return true;
694 return false;
697 bad = false;
698 for (i = 0; i <= nelts; ++i)
700 tree index;
701 if (i == nelts)
702 index = NULL_TREE;
703 else
705 index = CONSTRUCTOR_ELT (body, i)->index;
706 /* Skip base and vtable inits. */
707 if (TREE_CODE (index) != FIELD_DECL
708 || DECL_ARTIFICIAL (index))
709 continue;
711 for (; field != index; field = DECL_CHAIN (field))
713 tree ftype;
714 if (TREE_CODE (field) != FIELD_DECL
715 || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
716 || DECL_ARTIFICIAL (field))
717 continue;
718 ftype = strip_array_types (TREE_TYPE (field));
719 if (type_has_constexpr_default_constructor (ftype))
721 /* It's OK to skip a member with a trivial constexpr ctor.
722 A constexpr ctor that isn't trivial should have been
723 added in by now. */
724 gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype)
725 || errorcount != 0);
726 continue;
728 if (!complain)
729 return true;
730 error ("member %qD must be initialized by mem-initializer "
731 "in %<constexpr%> constructor", field);
732 inform (DECL_SOURCE_LOCATION (field), "declared here");
733 bad = true;
735 if (field == NULL_TREE)
736 break;
737 field = DECL_CHAIN (field);
740 return bad;
743 /* We are processing the definition of the constexpr function FUN.
744 Check that its BODY fulfills the propriate requirements and
745 enter it in the constexpr function definition table.
746 For constructor BODY is actually the TREE_LIST of the
747 member-initializer list. */
749 tree
750 register_constexpr_fundef (tree fun, tree body)
752 constexpr_fundef entry;
753 constexpr_fundef **slot;
755 if (!is_valid_constexpr_fn (fun, !DECL_GENERATED_P (fun)))
756 return NULL;
758 body = massage_constexpr_body (fun, body);
759 if (body == NULL_TREE || body == error_mark_node)
761 if (!DECL_CONSTRUCTOR_P (fun))
762 error ("body of constexpr function %qD not a return-statement", fun);
763 return NULL;
766 if (!potential_rvalue_constant_expression (body))
768 if (!DECL_GENERATED_P (fun))
769 require_potential_rvalue_constant_expression (body);
770 return NULL;
773 if (DECL_CONSTRUCTOR_P (fun)
774 && cx_check_missing_mem_inits (fun, body, !DECL_GENERATED_P (fun)))
775 return NULL;
777 /* Create the constexpr function table if necessary. */
778 if (constexpr_fundef_table == NULL)
779 constexpr_fundef_table
780 = hash_table<constexpr_fundef_hasher>::create_ggc (101);
782 entry.decl = fun;
783 entry.body = body;
784 slot = constexpr_fundef_table->find_slot (&entry, INSERT);
786 gcc_assert (*slot == NULL);
787 *slot = ggc_alloc<constexpr_fundef> ();
788 **slot = entry;
790 return fun;
793 /* FUN is a non-constexpr function called in a context that requires a
794 constant expression. If it comes from a constexpr template, explain why
795 the instantiation isn't constexpr. */
797 void
798 explain_invalid_constexpr_fn (tree fun)
800 static hash_set<tree> *diagnosed;
801 tree body;
802 location_t save_loc;
803 /* Only diagnose defaulted functions or instantiations. */
804 if (!DECL_DEFAULTED_FN (fun)
805 && !is_instantiation_of_constexpr (fun))
806 return;
807 if (diagnosed == NULL)
808 diagnosed = new hash_set<tree>;
809 if (diagnosed->add (fun))
810 /* Already explained. */
811 return;
813 save_loc = input_location;
814 input_location = DECL_SOURCE_LOCATION (fun);
815 inform (0, "%q+D is not usable as a constexpr function because:", fun);
816 /* First check the declaration. */
817 if (is_valid_constexpr_fn (fun, true))
819 /* Then if it's OK, the body. */
820 if (!DECL_DECLARED_CONSTEXPR_P (fun))
821 explain_implicit_non_constexpr (fun);
822 else
824 body = massage_constexpr_body (fun, DECL_SAVED_TREE (fun));
825 require_potential_rvalue_constant_expression (body);
826 if (DECL_CONSTRUCTOR_P (fun))
827 cx_check_missing_mem_inits (fun, body, true);
830 input_location = save_loc;
833 /* Objects of this type represent calls to constexpr functions
834 along with the bindings of parameters to their arguments, for
835 the purpose of compile time evaluation. */
837 struct GTY((for_user)) constexpr_call {
838 /* Description of the constexpr function definition. */
839 constexpr_fundef *fundef;
840 /* Parameter bindings environment. A TREE_LIST where each TREE_PURPOSE
841 is a parameter _DECL and the TREE_VALUE is the value of the parameter.
842 Note: This arrangement is made to accommodate the use of
843 iterative_hash_template_arg (see pt.c). If you change this
844 representation, also change the hash calculation in
845 cxx_eval_call_expression. */
846 tree bindings;
847 /* Result of the call.
848 NULL means the call is being evaluated.
849 error_mark_node means that the evaluation was erroneous;
850 otherwise, the actuall value of the call. */
851 tree result;
852 /* The hash of this call; we remember it here to avoid having to
853 recalculate it when expanding the hash table. */
854 hashval_t hash;
857 struct constexpr_call_hasher : ggc_hasher<constexpr_call *>
859 static hashval_t hash (constexpr_call *);
860 static bool equal (constexpr_call *, constexpr_call *);
863 /* The constexpr expansion context. CALL is the current function
864 expansion, CTOR is the current aggregate initializer, OBJECT is the
865 object being initialized by CTOR, either a VAR_DECL or a _REF. VALUES
866 is a map of values of variables initialized within the expression. */
868 struct constexpr_ctx {
869 constexpr_call *call;
870 hash_map<tree,tree> *values;
871 tree ctor;
872 tree object;
873 bool quiet;
874 bool strict;
877 /* A table of all constexpr calls that have been evaluated by the
878 compiler in this translation unit. */
880 static GTY (()) hash_table<constexpr_call_hasher> *constexpr_call_table;
882 static tree cxx_eval_constant_expression (const constexpr_ctx *, tree,
883 bool, bool *, bool *, tree * = NULL);
885 /* Compute a hash value for a constexpr call representation. */
887 inline hashval_t
888 constexpr_call_hasher::hash (constexpr_call *info)
890 return info->hash;
893 /* Return true if the objects pointed to by P and Q represent calls
894 to the same constexpr function with the same arguments.
895 Otherwise, return false. */
897 bool
898 constexpr_call_hasher::equal (constexpr_call *lhs, constexpr_call *rhs)
900 tree lhs_bindings;
901 tree rhs_bindings;
902 if (lhs == rhs)
903 return 1;
904 if (!constexpr_fundef_hasher::equal (lhs->fundef, rhs->fundef))
905 return 0;
906 lhs_bindings = lhs->bindings;
907 rhs_bindings = rhs->bindings;
908 while (lhs_bindings != NULL && rhs_bindings != NULL)
910 tree lhs_arg = TREE_VALUE (lhs_bindings);
911 tree rhs_arg = TREE_VALUE (rhs_bindings);
912 gcc_assert (TREE_TYPE (lhs_arg) == TREE_TYPE (rhs_arg));
913 if (!cp_tree_equal (lhs_arg, rhs_arg))
914 return 0;
915 lhs_bindings = TREE_CHAIN (lhs_bindings);
916 rhs_bindings = TREE_CHAIN (rhs_bindings);
918 return lhs_bindings == rhs_bindings;
921 /* Initialize the constexpr call table, if needed. */
923 static void
924 maybe_initialize_constexpr_call_table (void)
926 if (constexpr_call_table == NULL)
927 constexpr_call_table = hash_table<constexpr_call_hasher>::create_ggc (101);
930 /* We have an expression tree T that represents a call, either CALL_EXPR
931 or AGGR_INIT_EXPR. If the call is lexically to a named function,
932 retrun the _DECL for that function. */
934 static tree
935 get_function_named_in_call (tree t)
937 tree fun = NULL;
938 switch (TREE_CODE (t))
940 case CALL_EXPR:
941 fun = CALL_EXPR_FN (t);
942 break;
944 case AGGR_INIT_EXPR:
945 fun = AGGR_INIT_EXPR_FN (t);
946 break;
948 default:
949 gcc_unreachable();
950 break;
952 if (fun && TREE_CODE (fun) == ADDR_EXPR
953 && TREE_CODE (TREE_OPERAND (fun, 0)) == FUNCTION_DECL)
954 fun = TREE_OPERAND (fun, 0);
955 return fun;
958 /* We have an expression tree T that represents a call, either CALL_EXPR
959 or AGGR_INIT_EXPR. Return the Nth argument. */
961 static inline tree
962 get_nth_callarg (tree t, int n)
964 switch (TREE_CODE (t))
966 case CALL_EXPR:
967 return CALL_EXPR_ARG (t, n);
969 case AGGR_INIT_EXPR:
970 return AGGR_INIT_EXPR_ARG (t, n);
972 default:
973 gcc_unreachable ();
974 return NULL;
978 /* Look up the binding of the function parameter T in a constexpr
979 function call context CALL. */
981 static tree
982 lookup_parameter_binding (const constexpr_call *call, tree t)
984 tree b = purpose_member (t, call->bindings);
985 return TREE_VALUE (b);
988 /* Attempt to evaluate T which represents a call to a builtin function.
989 We assume here that all builtin functions evaluate to scalar types
990 represented by _CST nodes. */
992 static tree
993 cxx_eval_builtin_function_call (const constexpr_ctx *ctx, tree t,
994 bool addr,
995 bool *non_constant_p, bool *overflow_p)
997 const int nargs = call_expr_nargs (t);
998 tree *args = (tree *) alloca (nargs * sizeof (tree));
999 tree new_call;
1000 int i;
1001 for (i = 0; i < nargs; ++i)
1003 args[i] = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, i),
1004 addr,
1005 non_constant_p, overflow_p);
1006 if (ctx->quiet && *non_constant_p)
1007 return t;
1009 if (*non_constant_p)
1010 return t;
1011 new_call = fold_build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t),
1012 CALL_EXPR_FN (t), nargs, args);
1013 VERIFY_CONSTANT (new_call);
1014 return new_call;
1017 /* TEMP is the constant value of a temporary object of type TYPE. Adjust
1018 the type of the value to match. */
1020 static tree
1021 adjust_temp_type (tree type, tree temp)
1023 if (TREE_TYPE (temp) == type)
1024 return temp;
1025 /* Avoid wrapping an aggregate value in a NOP_EXPR. */
1026 if (TREE_CODE (temp) == CONSTRUCTOR)
1027 return build_constructor (type, CONSTRUCTOR_ELTS (temp));
1028 gcc_assert (scalarish_type_p (type));
1029 return cp_fold_convert (type, temp);
1032 /* True if we want to use the new handling of constexpr calls based on
1033 DECL_SAVED_TREE. */
1034 #define use_new_call true
1036 /* Subroutine of cxx_eval_call_expression.
1037 We are processing a call expression (either CALL_EXPR or
1038 AGGR_INIT_EXPR) in the context of CTX. Evaluate
1039 all arguments and bind their values to correspondings
1040 parameters, making up the NEW_CALL context. */
1042 static void
1043 cxx_bind_parameters_in_call (const constexpr_ctx *ctx, tree t,
1044 constexpr_call *new_call,
1045 bool *non_constant_p, bool *overflow_p)
1047 const int nargs = call_expr_nargs (t);
1048 tree fun = new_call->fundef->decl;
1049 tree parms = DECL_ARGUMENTS (fun);
1050 int i;
1051 tree *p = &new_call->bindings;
1052 for (i = 0; i < nargs; ++i)
1054 tree x, arg;
1055 tree type = parms ? TREE_TYPE (parms) : void_type_node;
1056 x = get_nth_callarg (t, i);
1057 /* For member function, the first argument is a pointer to the implied
1058 object. For a constructor, it might still be a dummy object, in
1059 which case we get the real argument from ctx. */
1060 if (i == 0 && DECL_CONSTRUCTOR_P (fun)
1061 && is_dummy_object (x))
1063 x = ctx->object;
1064 x = cp_build_addr_expr (x, tf_warning_or_error);
1066 bool addr = false;
1067 if (parms && DECL_BY_REFERENCE (parms) && !use_new_call)
1069 /* cp_genericize made this a reference for argument passing, but
1070 we don't want to treat it like one for C++11 constexpr
1071 evaluation. C++14 constexpr evaluation uses the genericized
1072 DECL_SAVED_TREE. */
1073 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
1074 gcc_assert (TREE_CODE (TREE_TYPE (x)) == REFERENCE_TYPE);
1075 type = TREE_TYPE (type);
1076 x = convert_from_reference (x);
1077 addr = true;
1079 arg = cxx_eval_constant_expression (ctx, x, addr,
1080 non_constant_p, overflow_p);
1081 /* Don't VERIFY_CONSTANT here. */
1082 if (*non_constant_p && ctx->quiet)
1083 return;
1084 /* Just discard ellipsis args after checking their constantitude. */
1085 if (!parms)
1086 continue;
1087 if (*non_constant_p)
1088 /* Don't try to adjust the type of non-constant args. */
1089 goto next;
1091 /* Make sure the binding has the same type as the parm. */
1092 if (TREE_CODE (type) != REFERENCE_TYPE)
1093 arg = adjust_temp_type (type, arg);
1094 *p = build_tree_list (parms, arg);
1095 p = &TREE_CHAIN (*p);
1096 next:
1097 parms = TREE_CHAIN (parms);
1101 /* Variables and functions to manage constexpr call expansion context.
1102 These do not need to be marked for PCH or GC. */
1104 /* FIXME remember and print actual constant arguments. */
1105 static vec<tree> call_stack = vNULL;
1106 static int call_stack_tick;
1107 static int last_cx_error_tick;
1109 static bool
1110 push_cx_call_context (tree call)
1112 ++call_stack_tick;
1113 if (!EXPR_HAS_LOCATION (call))
1114 SET_EXPR_LOCATION (call, input_location);
1115 call_stack.safe_push (call);
1116 if (call_stack.length () > (unsigned) max_constexpr_depth)
1117 return false;
1118 return true;
1121 static void
1122 pop_cx_call_context (void)
1124 ++call_stack_tick;
1125 call_stack.pop ();
1128 vec<tree>
1129 cx_error_context (void)
1131 vec<tree> r = vNULL;
1132 if (call_stack_tick != last_cx_error_tick
1133 && !call_stack.is_empty ())
1134 r = call_stack;
1135 last_cx_error_tick = call_stack_tick;
1136 return r;
1139 /* Subroutine of cxx_eval_constant_expression.
1140 Evaluate the call expression tree T in the context of OLD_CALL expression
1141 evaluation. */
1143 static tree
1144 cxx_eval_call_expression (const constexpr_ctx *ctx, tree t,
1145 bool addr,
1146 bool *non_constant_p, bool *overflow_p)
1148 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
1149 tree fun = get_function_named_in_call (t);
1150 tree result;
1151 constexpr_call new_call = { NULL, NULL, NULL, 0 };
1152 constexpr_call **slot;
1153 constexpr_call *entry;
1154 bool depth_ok;
1156 if (fun == NULL_TREE)
1157 switch (CALL_EXPR_IFN (t))
1159 case IFN_UBSAN_NULL:
1160 case IFN_UBSAN_BOUNDS:
1161 return void_node;
1162 default:
1163 if (!ctx->quiet)
1164 error_at (loc, "call to internal function");
1165 *non_constant_p = true;
1166 return t;
1169 if (TREE_CODE (fun) != FUNCTION_DECL)
1171 /* Might be a constexpr function pointer. */
1172 fun = cxx_eval_constant_expression (ctx, fun,
1173 /*addr*/false, non_constant_p,
1174 overflow_p);
1175 STRIP_NOPS (fun);
1176 if (TREE_CODE (fun) == ADDR_EXPR)
1177 fun = TREE_OPERAND (fun, 0);
1179 if (TREE_CODE (fun) != FUNCTION_DECL)
1181 if (!ctx->quiet && !*non_constant_p)
1182 error_at (loc, "expression %qE does not designate a constexpr "
1183 "function", fun);
1184 *non_constant_p = true;
1185 return t;
1187 if (DECL_CLONED_FUNCTION_P (fun))
1188 fun = DECL_CLONED_FUNCTION (fun);
1190 if (is_ubsan_builtin_p (fun))
1191 return void_node;
1193 if (is_builtin_fn (fun))
1194 return cxx_eval_builtin_function_call (ctx, t,
1195 addr, non_constant_p, overflow_p);
1196 if (!DECL_DECLARED_CONSTEXPR_P (fun))
1198 if (!ctx->quiet)
1200 error_at (loc, "call to non-constexpr function %qD", fun);
1201 explain_invalid_constexpr_fn (fun);
1203 *non_constant_p = true;
1204 return t;
1207 /* Shortcut trivial constructor/op=. */
1208 if (trivial_fn_p (fun))
1210 if (call_expr_nargs (t) == 2)
1212 tree arg = convert_from_reference (get_nth_callarg (t, 1));
1213 return cxx_eval_constant_expression (ctx, arg,
1214 addr, non_constant_p,
1215 overflow_p);
1217 else if (TREE_CODE (t) == AGGR_INIT_EXPR
1218 && AGGR_INIT_ZERO_FIRST (t))
1219 return build_zero_init (DECL_CONTEXT (fun), NULL_TREE, false);
1222 /* If in direct recursive call, optimize definition search. */
1223 if (ctx && ctx->call && ctx->call->fundef->decl == fun)
1224 new_call.fundef = ctx->call->fundef;
1225 else
1227 new_call.fundef = retrieve_constexpr_fundef (fun);
1228 if (new_call.fundef == NULL || new_call.fundef->body == NULL)
1230 if (!ctx->quiet)
1232 if (DECL_INITIAL (fun))
1234 /* The definition of fun was somehow unsuitable. */
1235 error_at (loc, "%qD called in a constant expression", fun);
1236 explain_invalid_constexpr_fn (fun);
1238 else
1239 error_at (loc, "%qD used before its definition", fun);
1241 *non_constant_p = true;
1242 return t;
1246 constexpr_ctx new_ctx = *ctx;
1247 if (DECL_CONSTRUCTOR_P (fun) && !ctx->object
1248 && TREE_CODE (t) == AGGR_INIT_EXPR)
1250 /* We want to have an initialization target for an AGGR_INIT_EXPR.
1251 If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
1252 new_ctx.object = AGGR_INIT_EXPR_SLOT (t);
1253 tree ctor = new_ctx.ctor = build_constructor (DECL_CONTEXT (fun), NULL);
1254 CONSTRUCTOR_NO_IMPLICIT_ZERO (ctor) = true;
1255 ctx->values->put (new_ctx.object, ctor);
1256 ctx = &new_ctx;
1259 cxx_bind_parameters_in_call (ctx, t, &new_call,
1260 non_constant_p, overflow_p);
1261 if (*non_constant_p)
1262 return t;
1264 depth_ok = push_cx_call_context (t);
1266 new_call.hash
1267 = iterative_hash_template_arg (new_call.bindings,
1268 constexpr_fundef_hasher::hash (new_call.fundef));
1270 /* If we have seen this call before, we are done. */
1271 maybe_initialize_constexpr_call_table ();
1272 slot = constexpr_call_table->find_slot (&new_call, INSERT);
1273 entry = *slot;
1274 if (entry == NULL)
1276 /* We need to keep a pointer to the entry, not just the slot, as the
1277 slot can move in the call to cxx_eval_builtin_function_call. */
1278 *slot = entry = ggc_alloc<constexpr_call> ();
1279 *entry = new_call;
1281 /* Calls which are in progress have their result set to NULL
1282 so that we can detect circular dependencies. */
1283 else if (entry->result == NULL)
1285 if (!ctx->quiet)
1286 error ("call has circular dependency");
1287 *non_constant_p = true;
1288 entry->result = result = error_mark_node;
1291 if (!depth_ok)
1293 if (!ctx->quiet)
1294 error ("constexpr evaluation depth exceeds maximum of %d (use "
1295 "-fconstexpr-depth= to increase the maximum)",
1296 max_constexpr_depth);
1297 *non_constant_p = true;
1298 entry->result = result = error_mark_node;
1300 else
1302 result = entry->result;
1303 if (!result || result == error_mark_node)
1305 if (!use_new_call)
1307 new_ctx.call = &new_call;
1308 result = (cxx_eval_constant_expression
1309 (&new_ctx, new_call.fundef->body,
1310 addr,
1311 non_constant_p, overflow_p));
1313 else
1315 if (DECL_SAVED_TREE (fun) == NULL_TREE
1316 && (DECL_CONSTRUCTOR_P (fun) || DECL_DESTRUCTOR_P (fun)))
1317 /* The maybe-in-charge 'tor had its DECL_SAVED_TREE
1318 cleared, try the first clone. */
1319 fun = DECL_CHAIN (fun);
1320 gcc_assert (DECL_SAVED_TREE (fun));
1321 tree parms, res;
1323 /* Unshare the whole function body. */
1324 tree body = copy_fn (fun, parms, res);
1326 /* Associate the bindings with the remapped parms. */
1327 tree bound = new_call.bindings;
1328 tree remapped = parms;
1329 while (bound)
1331 tree oparm = TREE_PURPOSE (bound);
1332 tree arg = TREE_VALUE (bound);
1333 gcc_assert (DECL_NAME (remapped) == DECL_NAME (oparm));
1334 ctx->values->put (remapped, arg);
1335 bound = TREE_CHAIN (bound);
1336 remapped = DECL_CHAIN (remapped);
1338 /* Add the RESULT_DECL to the values map, too. */
1339 tree slot = NULL_TREE;
1340 if (DECL_BY_REFERENCE (res))
1342 slot = AGGR_INIT_EXPR_SLOT (t);
1343 tree addr = build_address (slot);
1344 addr = build_nop (TREE_TYPE (res), addr);
1345 ctx->values->put (res, addr);
1346 ctx->values->put (slot, NULL_TREE);
1348 else
1349 ctx->values->put (res, NULL_TREE);
1351 tree jump_target = NULL_TREE;
1352 cxx_eval_constant_expression (ctx, body,
1353 addr, non_constant_p, overflow_p,
1354 &jump_target);
1356 if (DECL_CONSTRUCTOR_P (fun))
1357 /* This can be null for a subobject constructor call, in
1358 which case what we care about is the initialization
1359 side-effects rather than the value. We could get at the
1360 value by evaluating *this, but we don't bother; there's
1361 no need to put such a call in the hash table. */
1362 result = addr ? ctx->object : ctx->ctor;
1363 else
1365 result = *ctx->values->get (slot ? slot : res);
1366 if (result == NULL_TREE && !*non_constant_p)
1368 if (!ctx->quiet)
1369 error ("constexpr call flows off the end "
1370 "of the function");
1371 *non_constant_p = true;
1375 /* Remove the parms/result from the values map. Is it worth
1376 bothering to do this when the map itself is only live for
1377 one constexpr evaluation? If so, maybe also clear out
1378 other vars from call, maybe in BIND_EXPR handling? */
1379 ctx->values->remove (res);
1380 if (slot)
1381 ctx->values->remove (slot);
1382 for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
1383 ctx->values->remove (parm);
1387 if (result == error_mark_node)
1388 *non_constant_p = true;
1389 if (*non_constant_p)
1390 entry->result = result = error_mark_node;
1391 else if (result)
1393 /* If this was a call to initialize an object, set the type of
1394 the CONSTRUCTOR to the type of that object. */
1395 if (DECL_CONSTRUCTOR_P (fun) && !use_new_call)
1397 tree ob_arg = get_nth_callarg (t, 0);
1398 STRIP_NOPS (ob_arg);
1399 gcc_assert (TYPE_PTR_P (TREE_TYPE (ob_arg))
1400 && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (ob_arg))));
1401 result = adjust_temp_type (TREE_TYPE (TREE_TYPE (ob_arg)),
1402 result);
1404 entry->result = result;
1406 else
1407 result = void_node;
1410 pop_cx_call_context ();
1411 return unshare_expr (result);
1414 /* FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
1416 bool
1417 reduced_constant_expression_p (tree t)
1419 switch (TREE_CODE (t))
1421 case PTRMEM_CST:
1422 /* Even if we can't lower this yet, it's constant. */
1423 return true;
1425 case CONSTRUCTOR:
1426 /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
1427 tree elt; unsigned HOST_WIDE_INT idx;
1428 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), idx, elt)
1429 if (!reduced_constant_expression_p (elt))
1430 return false;
1431 return true;
1433 default:
1434 /* FIXME are we calling this too much? */
1435 return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE;
1439 /* Some expressions may have constant operands but are not constant
1440 themselves, such as 1/0. Call this function (or rather, the macro
1441 following it) to check for that condition.
1443 We only call this in places that require an arithmetic constant, not in
1444 places where we might have a non-constant expression that can be a
1445 component of a constant expression, such as the address of a constexpr
1446 variable that might be dereferenced later. */
1448 static bool
1449 verify_constant (tree t, bool allow_non_constant, bool *non_constant_p,
1450 bool *overflow_p)
1452 if (!*non_constant_p && !reduced_constant_expression_p (t))
1454 if (!allow_non_constant)
1455 error ("%q+E is not a constant expression", t);
1456 *non_constant_p = true;
1458 if (TREE_OVERFLOW_P (t))
1460 if (!allow_non_constant)
1462 permerror (input_location, "overflow in constant expression");
1463 /* If we're being permissive (and are in an enforcing
1464 context), ignore the overflow. */
1465 if (flag_permissive)
1466 return *non_constant_p;
1468 *overflow_p = true;
1470 return *non_constant_p;
1473 /* Check whether the shift operation with code CODE and type TYPE on LHS
1474 and RHS is undefined. If it is, give an error with an explanation,
1475 and return true; return false otherwise. */
1477 static bool
1478 cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx,
1479 enum tree_code code, tree type, tree lhs, tree rhs)
1481 if ((code != LSHIFT_EXPR && code != RSHIFT_EXPR)
1482 || TREE_CODE (lhs) != INTEGER_CST
1483 || TREE_CODE (rhs) != INTEGER_CST)
1484 return false;
1486 tree lhstype = TREE_TYPE (lhs);
1487 unsigned HOST_WIDE_INT uprec = TYPE_PRECISION (TREE_TYPE (lhs));
1489 /* [expr.shift] The behavior is undefined if the right operand
1490 is negative, or greater than or equal to the length in bits
1491 of the promoted left operand. */
1492 if (tree_int_cst_sgn (rhs) == -1)
1494 if (!ctx->quiet)
1495 error_at (loc, "right operand of shift expression %q+E is negative",
1496 build2_loc (loc, code, type, lhs, rhs));
1497 return true;
1499 if (compare_tree_int (rhs, uprec) >= 0)
1501 if (!ctx->quiet)
1502 error_at (loc, "right operand of shift expression %q+E is >= than "
1503 "the precision of the left operand",
1504 build2_loc (loc, code, type, lhs, rhs));
1505 return true;
1508 /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
1509 if E1 has a signed type and non-negative value, and E1x2^E2 is
1510 representable in the corresponding unsigned type of the result type,
1511 then that value, converted to the result type, is the resulting value;
1512 otherwise, the behavior is undefined. */
1513 if (code == LSHIFT_EXPR && !TYPE_UNSIGNED (lhstype)
1514 && (cxx_dialect >= cxx11))
1516 if (tree_int_cst_sgn (lhs) == -1)
1518 if (!ctx->quiet)
1519 error_at (loc, "left operand of shift expression %q+E is negative",
1520 build2_loc (loc, code, type, lhs, rhs));
1521 return true;
1523 /* For signed x << y the following:
1524 (unsigned) x >> ((prec (lhs) - 1) - y)
1525 if > 1, is undefined. The right-hand side of this formula
1526 is the highest bit of the LHS that can be set (starting from 0),
1527 so that the shift doesn't overflow. We then right-shift the LHS
1528 to see whether any other bit is set making the original shift
1529 undefined -- the result is not representable in the corresponding
1530 unsigned type. */
1531 tree t = build_int_cst (unsigned_type_node, uprec - 1);
1532 t = fold_build2 (MINUS_EXPR, unsigned_type_node, t, rhs);
1533 tree ulhs = fold_convert (unsigned_type_for (lhstype), lhs);
1534 t = fold_build2 (RSHIFT_EXPR, TREE_TYPE (ulhs), ulhs, t);
1535 if (tree_int_cst_lt (integer_one_node, t))
1537 if (!ctx->quiet)
1538 error_at (loc, "shift expression %q+E overflows",
1539 build2_loc (loc, code, type, lhs, rhs));
1540 return true;
1543 return false;
1546 /* Subroutine of cxx_eval_constant_expression.
1547 Attempt to reduce the unary expression tree T to a compile time value.
1548 If successful, return the value. Otherwise issue a diagnostic
1549 and return error_mark_node. */
1551 static tree
1552 cxx_eval_unary_expression (const constexpr_ctx *ctx, tree t,
1553 bool addr,
1554 bool *non_constant_p, bool *overflow_p)
1556 tree r;
1557 tree orig_arg = TREE_OPERAND (t, 0);
1558 tree arg = cxx_eval_constant_expression (ctx, orig_arg,
1559 addr, non_constant_p, overflow_p);
1560 VERIFY_CONSTANT (arg);
1561 location_t loc = EXPR_LOCATION (t);
1562 enum tree_code code = TREE_CODE (t);
1563 tree type = TREE_TYPE (t);
1564 r = fold_unary_loc (loc, code, type, arg);
1565 if (r == NULL_TREE)
1567 if (arg == orig_arg)
1568 r = t;
1569 else
1570 r = build1_loc (loc, code, type, arg);
1572 VERIFY_CONSTANT (r);
1573 return r;
1576 /* Subroutine of cxx_eval_constant_expression.
1577 Like cxx_eval_unary_expression, except for binary expressions. */
1579 static tree
1580 cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t,
1581 bool addr,
1582 bool *non_constant_p, bool *overflow_p)
1584 tree r;
1585 tree orig_lhs = TREE_OPERAND (t, 0);
1586 tree orig_rhs = TREE_OPERAND (t, 1);
1587 tree lhs, rhs;
1588 lhs = cxx_eval_constant_expression (ctx, orig_lhs,
1589 addr,
1590 non_constant_p, overflow_p);
1591 VERIFY_CONSTANT (lhs);
1592 rhs = cxx_eval_constant_expression (ctx, orig_rhs,
1593 addr,
1594 non_constant_p, overflow_p);
1595 VERIFY_CONSTANT (rhs);
1597 location_t loc = EXPR_LOCATION (t);
1598 enum tree_code code = TREE_CODE (t);
1599 tree type = TREE_TYPE (t);
1600 r = fold_binary_loc (loc, code, type, lhs, rhs);
1601 if (r == NULL_TREE)
1603 if (lhs == orig_lhs && rhs == orig_rhs)
1604 r = t;
1605 else
1606 r = build2_loc (loc, code, type, lhs, rhs);
1608 else if (cxx_eval_check_shift_p (loc, ctx, code, type, lhs, rhs))
1609 *non_constant_p = true;
1610 VERIFY_CONSTANT (r);
1611 return r;
1614 /* Subroutine of cxx_eval_constant_expression.
1615 Attempt to evaluate condition expressions. Dead branches are not
1616 looked into. */
1618 static tree
1619 cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t,
1620 bool addr,
1621 bool *non_constant_p, bool *overflow_p,
1622 tree *jump_target)
1624 tree val = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
1625 addr,
1626 non_constant_p, overflow_p);
1627 VERIFY_CONSTANT (val);
1628 /* Don't VERIFY_CONSTANT the other operands. */
1629 if (integer_zerop (val))
1630 return cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2),
1631 addr,
1632 non_constant_p, overflow_p,
1633 jump_target);
1634 return cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
1635 addr,
1636 non_constant_p, overflow_p,
1637 jump_target);
1640 /* Subroutine of cxx_eval_constant_expression.
1641 Attempt to reduce a reference to an array slot. */
1643 static tree
1644 cxx_eval_array_reference (const constexpr_ctx *ctx, tree t,
1645 bool addr,
1646 bool *non_constant_p, bool *overflow_p)
1648 tree oldary = TREE_OPERAND (t, 0);
1649 tree ary = cxx_eval_constant_expression (ctx, oldary,
1650 addr,
1651 non_constant_p, overflow_p);
1652 tree index, oldidx;
1653 HOST_WIDE_INT i;
1654 tree elem_type;
1655 unsigned len, elem_nchars = 1;
1656 if (*non_constant_p)
1657 return t;
1658 oldidx = TREE_OPERAND (t, 1);
1659 index = cxx_eval_constant_expression (ctx, oldidx,
1660 false,
1661 non_constant_p, overflow_p);
1662 VERIFY_CONSTANT (index);
1663 if (addr && ary == oldary && index == oldidx)
1664 return t;
1665 else if (addr)
1666 return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL);
1667 elem_type = TREE_TYPE (TREE_TYPE (ary));
1668 if (TREE_CODE (ary) == CONSTRUCTOR)
1669 len = CONSTRUCTOR_NELTS (ary);
1670 else if (TREE_CODE (ary) == STRING_CST)
1672 elem_nchars = (TYPE_PRECISION (elem_type)
1673 / TYPE_PRECISION (char_type_node));
1674 len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars;
1676 else
1678 /* We can't do anything with other tree codes, so use
1679 VERIFY_CONSTANT to complain and fail. */
1680 VERIFY_CONSTANT (ary);
1681 gcc_unreachable ();
1683 if (compare_tree_int (index, len) >= 0)
1685 if (tree_int_cst_lt (index, array_type_nelts_top (TREE_TYPE (ary))))
1687 /* If it's within the array bounds but doesn't have an explicit
1688 initializer, it's value-initialized. */
1689 tree val = build_value_init (elem_type, tf_warning_or_error);
1690 return cxx_eval_constant_expression (ctx, val,
1691 addr,
1692 non_constant_p, overflow_p);
1695 if (!ctx->quiet)
1696 error ("array subscript out of bound");
1697 *non_constant_p = true;
1698 return t;
1700 else if (tree_int_cst_lt (index, integer_zero_node))
1702 if (!ctx->quiet)
1703 error ("negative array subscript");
1704 *non_constant_p = true;
1705 return t;
1707 i = tree_to_shwi (index);
1708 if (TREE_CODE (ary) == CONSTRUCTOR)
1709 return (*CONSTRUCTOR_ELTS (ary))[i].value;
1710 else if (elem_nchars == 1)
1711 return build_int_cst (cv_unqualified (TREE_TYPE (TREE_TYPE (ary))),
1712 TREE_STRING_POINTER (ary)[i]);
1713 else
1715 tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (ary)));
1716 return native_interpret_expr (type, (const unsigned char *)
1717 TREE_STRING_POINTER (ary)
1718 + i * elem_nchars, elem_nchars);
1720 /* Don't VERIFY_CONSTANT here. */
1723 /* Subroutine of cxx_eval_constant_expression.
1724 Attempt to reduce a field access of a value of class type. */
1726 static tree
1727 cxx_eval_component_reference (const constexpr_ctx *ctx, tree t,
1728 bool addr,
1729 bool *non_constant_p, bool *overflow_p)
1731 unsigned HOST_WIDE_INT i;
1732 tree field;
1733 tree value;
1734 tree part = TREE_OPERAND (t, 1);
1735 tree orig_whole = TREE_OPERAND (t, 0);
1736 tree whole = cxx_eval_constant_expression (ctx, orig_whole,
1737 addr,
1738 non_constant_p, overflow_p);
1739 if (whole == orig_whole)
1740 return t;
1741 if (addr)
1742 return fold_build3 (COMPONENT_REF, TREE_TYPE (t),
1743 whole, part, NULL_TREE);
1744 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
1745 CONSTRUCTOR. */
1746 if (!*non_constant_p && TREE_CODE (whole) != CONSTRUCTOR)
1748 if (!ctx->quiet)
1749 error ("%qE is not a constant expression", orig_whole);
1750 *non_constant_p = true;
1752 if (DECL_MUTABLE_P (part))
1754 if (!ctx->quiet)
1755 error ("mutable %qD is not usable in a constant expression", part);
1756 *non_constant_p = true;
1758 if (*non_constant_p)
1759 return t;
1760 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
1762 if (field == part)
1764 if (value)
1765 return value;
1766 else
1767 /* We're in the middle of initializing it. */
1768 break;
1771 if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE
1772 && CONSTRUCTOR_NELTS (whole) > 0)
1774 /* DR 1188 says we don't have to deal with this. */
1775 if (!ctx->quiet)
1776 error ("accessing %qD member instead of initialized %qD member in "
1777 "constant expression", part, CONSTRUCTOR_ELT (whole, 0)->index);
1778 *non_constant_p = true;
1779 return t;
1782 if (CONSTRUCTOR_NO_IMPLICIT_ZERO (whole))
1784 /* 'whole' is part of the aggregate initializer we're currently
1785 building; if there's no initializer for this member yet, that's an
1786 error. */
1787 if (!ctx->quiet)
1788 error ("accessing uninitialized member %qD", part);
1789 *non_constant_p = true;
1790 return t;
1793 /* If there's no explicit init for this field, it's value-initialized. */
1794 value = build_value_init (TREE_TYPE (t), tf_warning_or_error);
1795 return cxx_eval_constant_expression (ctx, value,
1796 addr,
1797 non_constant_p, overflow_p);
1800 /* Subroutine of cxx_eval_constant_expression.
1801 Attempt to reduce a field access of a value of class type that is
1802 expressed as a BIT_FIELD_REF. */
1804 static tree
1805 cxx_eval_bit_field_ref (const constexpr_ctx *ctx, tree t,
1806 bool addr,
1807 bool *non_constant_p, bool *overflow_p)
1809 tree orig_whole = TREE_OPERAND (t, 0);
1810 tree retval, fldval, utype, mask;
1811 bool fld_seen = false;
1812 HOST_WIDE_INT istart, isize;
1813 tree whole = cxx_eval_constant_expression (ctx, orig_whole,
1814 addr,
1815 non_constant_p, overflow_p);
1816 tree start, field, value;
1817 unsigned HOST_WIDE_INT i;
1819 if (whole == orig_whole)
1820 return t;
1821 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
1822 CONSTRUCTOR. */
1823 if (!*non_constant_p
1824 && TREE_CODE (whole) != VECTOR_CST
1825 && TREE_CODE (whole) != CONSTRUCTOR)
1827 if (!ctx->quiet)
1828 error ("%qE is not a constant expression", orig_whole);
1829 *non_constant_p = true;
1831 if (*non_constant_p)
1832 return t;
1834 if (TREE_CODE (whole) == VECTOR_CST)
1835 return fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
1836 TREE_OPERAND (t, 1), TREE_OPERAND (t, 2));
1838 start = TREE_OPERAND (t, 2);
1839 istart = tree_to_shwi (start);
1840 isize = tree_to_shwi (TREE_OPERAND (t, 1));
1841 utype = TREE_TYPE (t);
1842 if (!TYPE_UNSIGNED (utype))
1843 utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1);
1844 retval = build_int_cst (utype, 0);
1845 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
1847 tree bitpos = bit_position (field);
1848 if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1))
1849 return value;
1850 if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE
1851 && TREE_CODE (value) == INTEGER_CST
1852 && tree_fits_shwi_p (bitpos)
1853 && tree_fits_shwi_p (DECL_SIZE (field)))
1855 HOST_WIDE_INT bit = tree_to_shwi (bitpos);
1856 HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field));
1857 HOST_WIDE_INT shift;
1858 if (bit >= istart && bit + sz <= istart + isize)
1860 fldval = fold_convert (utype, value);
1861 mask = build_int_cst_type (utype, -1);
1862 mask = fold_build2 (LSHIFT_EXPR, utype, mask,
1863 size_int (TYPE_PRECISION (utype) - sz));
1864 mask = fold_build2 (RSHIFT_EXPR, utype, mask,
1865 size_int (TYPE_PRECISION (utype) - sz));
1866 fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask);
1867 shift = bit - istart;
1868 if (BYTES_BIG_ENDIAN)
1869 shift = TYPE_PRECISION (utype) - shift - sz;
1870 fldval = fold_build2 (LSHIFT_EXPR, utype, fldval,
1871 size_int (shift));
1872 retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval);
1873 fld_seen = true;
1877 if (fld_seen)
1878 return fold_convert (TREE_TYPE (t), retval);
1879 gcc_unreachable ();
1880 return error_mark_node;
1883 /* Subroutine of cxx_eval_constant_expression.
1884 Evaluate a short-circuited logical expression T in the context
1885 of a given constexpr CALL. BAILOUT_VALUE is the value for
1886 early return. CONTINUE_VALUE is used here purely for
1887 sanity check purposes. */
1889 static tree
1890 cxx_eval_logical_expression (const constexpr_ctx *ctx, tree t,
1891 tree bailout_value, tree continue_value,
1892 bool addr,
1893 bool *non_constant_p, bool *overflow_p)
1895 tree r;
1896 tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
1897 addr,
1898 non_constant_p, overflow_p);
1899 VERIFY_CONSTANT (lhs);
1900 if (tree_int_cst_equal (lhs, bailout_value))
1901 return lhs;
1902 gcc_assert (tree_int_cst_equal (lhs, continue_value));
1903 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
1904 addr, non_constant_p,
1905 overflow_p);
1906 VERIFY_CONSTANT (r);
1907 return r;
1910 /* REF is a COMPONENT_REF designating a particular field. V is a vector of
1911 CONSTRUCTOR elements to initialize (part of) an object containing that
1912 field. Return a pointer to the constructor_elt corresponding to the
1913 initialization of the field. */
1915 static constructor_elt *
1916 base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref)
1918 tree aggr = TREE_OPERAND (ref, 0);
1919 tree field = TREE_OPERAND (ref, 1);
1920 HOST_WIDE_INT i;
1921 constructor_elt *ce;
1923 gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
1925 if (TREE_CODE (aggr) == COMPONENT_REF)
1927 constructor_elt *base_ce
1928 = base_field_constructor_elt (v, aggr);
1929 v = CONSTRUCTOR_ELTS (base_ce->value);
1932 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
1933 if (ce->index == field)
1934 return ce;
1936 gcc_unreachable ();
1937 return NULL;
1940 /* Some of the expressions fed to the constexpr mechanism are calls to
1941 constructors, which have type void. In that case, return the type being
1942 initialized by the constructor. */
1944 static tree
1945 initialized_type (tree t)
1947 if (TYPE_P (t))
1948 return t;
1949 tree type = cv_unqualified (TREE_TYPE (t));
1950 if (TREE_CODE (t) == CALL_EXPR || TREE_CODE (t) == AGGR_INIT_EXPR)
1952 /* A constructor call has void type, so we need to look deeper. */
1953 tree fn = get_function_named_in_call (t);
1954 if (fn && TREE_CODE (fn) == FUNCTION_DECL
1955 && DECL_CXX_CONSTRUCTOR_P (fn))
1956 type = DECL_CONTEXT (fn);
1958 return type;
1961 /* We're about to initialize element INDEX of an array or class from VALUE.
1962 Set up NEW_CTX appropriately by adjusting .object to refer to the
1963 subobject and creating a new CONSTRUCTOR if the element is itself
1964 a class or array. */
1966 static void
1967 init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx,
1968 tree index, tree &value)
1970 new_ctx = *ctx;
1972 if (index && TREE_CODE (index) != INTEGER_CST
1973 && TREE_CODE (index) != FIELD_DECL)
1974 /* This won't have an element in the new CONSTRUCTOR. */
1975 return;
1977 tree type = initialized_type (value);
1978 if (!AGGREGATE_TYPE_P (type) && !VECTOR_TYPE_P (type))
1979 /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
1980 return;
1982 /* The sub-aggregate initializer might contain a placeholder;
1983 update object to refer to the subobject and ctor to refer to
1984 the (newly created) sub-initializer. */
1985 if (ctx->object)
1986 new_ctx.object = build_ctor_subob_ref (index, type, ctx->object);
1987 tree elt = build_constructor (type, NULL);
1988 CONSTRUCTOR_NO_IMPLICIT_ZERO (elt) = true;
1989 new_ctx.ctor = elt;
1991 if (TREE_CODE (value) == TARGET_EXPR)
1992 /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
1993 value = TARGET_EXPR_INITIAL (value);
1996 /* We're about to process an initializer for a class or array TYPE. Make
1997 sure that CTX is set up appropriately. */
1999 static void
2000 verify_ctor_sanity (const constexpr_ctx *ctx, tree type)
2002 /* We don't bother building a ctor for an empty base subobject. */
2003 if (is_empty_class (type))
2004 return;
2006 /* We're in the middle of an initializer that might involve placeholders;
2007 our caller should have created a CONSTRUCTOR for us to put the
2008 initializer into. We will either return that constructor or T. */
2009 gcc_assert (ctx->ctor);
2010 gcc_assert (same_type_ignoring_top_level_qualifiers_p
2011 (type, TREE_TYPE (ctx->ctor)));
2012 gcc_assert (CONSTRUCTOR_NELTS (ctx->ctor) == 0);
2013 if (ctx->object)
2014 gcc_assert (same_type_ignoring_top_level_qualifiers_p
2015 (type, TREE_TYPE (ctx->object)));
2016 gcc_assert (!ctx->object || !DECL_P (ctx->object)
2017 || *(ctx->values->get (ctx->object)) == ctx->ctor);
2020 /* Subroutine of cxx_eval_constant_expression.
2021 The expression tree T denotes a C-style array or a C-style
2022 aggregate. Reduce it to a constant expression. */
2024 static tree
2025 cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t,
2026 bool addr,
2027 bool *non_constant_p, bool *overflow_p)
2029 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
2030 bool changed = false;
2031 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t));
2033 verify_ctor_sanity (ctx, TREE_TYPE (t));
2034 vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
2035 vec_alloc (*p, vec_safe_length (v));
2037 unsigned i; tree index, value;
2038 FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value)
2040 constexpr_ctx new_ctx;
2041 init_subob_ctx (ctx, new_ctx, index, value);
2042 if (new_ctx.ctor != ctx->ctor)
2043 /* If we built a new CONSTRUCTOR, attach it now so that other
2044 initializers can refer to it. */
2045 CONSTRUCTOR_APPEND_ELT (*p, index, new_ctx.ctor);
2046 tree elt = cxx_eval_constant_expression (&new_ctx, value,
2047 addr,
2048 non_constant_p, overflow_p);
2049 /* Don't VERIFY_CONSTANT here. */
2050 if (ctx->quiet && *non_constant_p)
2051 break;
2052 if (elt != value)
2053 changed = true;
2054 if (index && TREE_CODE (index) == COMPONENT_REF)
2056 /* This is an initialization of a vfield inside a base
2057 subaggregate that we already initialized; push this
2058 initialization into the previous initialization. */
2059 constructor_elt *inner = base_field_constructor_elt (*p, index);
2060 inner->value = elt;
2061 changed = true;
2063 else if (index
2064 && (TREE_CODE (index) == NOP_EXPR
2065 || TREE_CODE (index) == POINTER_PLUS_EXPR))
2067 /* This is an initializer for an empty base; now that we've
2068 checked that it's constant, we can ignore it. */
2069 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index))));
2070 changed = true;
2072 else if (new_ctx.ctor != ctx->ctor)
2074 /* We appended this element above; update the value. */
2075 gcc_assert ((*p)->last().index == index);
2076 (*p)->last().value = elt;
2078 else
2079 CONSTRUCTOR_APPEND_ELT (*p, index, elt);
2081 if (*non_constant_p || !changed)
2082 return t;
2083 t = ctx->ctor;
2084 /* We're done building this CONSTRUCTOR, so now we can interpret an
2085 element without an explicit initializer as value-initialized. */
2086 CONSTRUCTOR_NO_IMPLICIT_ZERO (t) = false;
2087 if (TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
2088 t = fold (t);
2089 return t;
2092 /* Subroutine of cxx_eval_constant_expression.
2093 The expression tree T is a VEC_INIT_EXPR which denotes the desired
2094 initialization of a non-static data member of array type. Reduce it to a
2095 CONSTRUCTOR.
2097 Note that apart from value-initialization (when VALUE_INIT is true),
2098 this is only intended to support value-initialization and the
2099 initializations done by defaulted constructors for classes with
2100 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
2101 will either be NULL_TREE for the default constructor, or a COMPONENT_REF
2102 for the copy/move constructor. */
2104 static tree
2105 cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init,
2106 bool value_init, bool addr,
2107 bool *non_constant_p, bool *overflow_p)
2109 tree elttype = TREE_TYPE (atype);
2110 unsigned HOST_WIDE_INT max = tree_to_uhwi (array_type_nelts_top (atype));
2111 verify_ctor_sanity (ctx, atype);
2112 vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
2113 vec_alloc (*p, max + 1);
2114 bool pre_init = false;
2115 unsigned HOST_WIDE_INT i;
2117 /* For the default constructor, build up a call to the default
2118 constructor of the element type. We only need to handle class types
2119 here, as for a constructor to be constexpr, all members must be
2120 initialized, which for a defaulted default constructor means they must
2121 be of a class type with a constexpr default constructor. */
2122 if (TREE_CODE (elttype) == ARRAY_TYPE)
2123 /* We only do this at the lowest level. */;
2124 else if (value_init)
2126 init = build_value_init (elttype, tf_warning_or_error);
2127 pre_init = true;
2129 else if (!init)
2131 vec<tree, va_gc> *argvec = make_tree_vector ();
2132 init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
2133 &argvec, elttype, LOOKUP_NORMAL,
2134 tf_warning_or_error);
2135 release_tree_vector (argvec);
2136 init = build_aggr_init_expr (TREE_TYPE (init), init);
2137 pre_init = true;
2140 for (i = 0; i < max; ++i)
2142 tree idx = build_int_cst (size_type_node, i);
2143 tree eltinit;
2144 constexpr_ctx new_ctx;
2145 init_subob_ctx (ctx, new_ctx, idx, pre_init ? init : elttype);
2146 if (new_ctx.ctor != ctx->ctor)
2147 CONSTRUCTOR_APPEND_ELT (*p, idx, new_ctx.ctor);
2148 if (TREE_CODE (elttype) == ARRAY_TYPE)
2150 /* A multidimensional array; recurse. */
2151 if (value_init || init == NULL_TREE)
2152 eltinit = NULL_TREE;
2153 else
2154 eltinit = cp_build_array_ref (input_location, init, idx,
2155 tf_warning_or_error);
2156 eltinit = cxx_eval_vec_init_1 (&new_ctx, elttype, eltinit, value_init,
2157 addr,
2158 non_constant_p, overflow_p);
2160 else if (pre_init)
2162 /* Initializing an element using value or default initialization
2163 we just pre-built above. */
2164 eltinit = (cxx_eval_constant_expression
2165 (&new_ctx, init,
2166 addr, non_constant_p, overflow_p));
2168 else
2170 /* Copying an element. */
2171 gcc_assert (same_type_ignoring_top_level_qualifiers_p
2172 (atype, TREE_TYPE (init)));
2173 eltinit = cp_build_array_ref (input_location, init, idx,
2174 tf_warning_or_error);
2175 if (!real_lvalue_p (init))
2176 eltinit = move (eltinit);
2177 eltinit = force_rvalue (eltinit, tf_warning_or_error);
2178 eltinit = (cxx_eval_constant_expression
2179 (&new_ctx, eltinit, addr,
2180 non_constant_p, overflow_p));
2182 if (*non_constant_p && !ctx->quiet)
2183 break;
2184 if (new_ctx.ctor != ctx->ctor)
2186 /* We appended this element above; update the value. */
2187 gcc_assert ((*p)->last().index == idx);
2188 (*p)->last().value = eltinit;
2190 else
2191 CONSTRUCTOR_APPEND_ELT (*p, idx, eltinit);
2194 if (!*non_constant_p)
2196 init = ctx->ctor;
2197 CONSTRUCTOR_NO_IMPLICIT_ZERO (init) = false;
2199 return init;
2202 static tree
2203 cxx_eval_vec_init (const constexpr_ctx *ctx, tree t,
2204 bool addr,
2205 bool *non_constant_p, bool *overflow_p)
2207 tree atype = TREE_TYPE (t);
2208 tree init = VEC_INIT_EXPR_INIT (t);
2209 tree r = cxx_eval_vec_init_1 (ctx, atype, init,
2210 VEC_INIT_EXPR_VALUE_INIT (t),
2211 addr, non_constant_p, overflow_p);
2212 if (*non_constant_p)
2213 return t;
2214 else
2215 return r;
2218 /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
2219 match. We want to be less strict for simple *& folding; if we have a
2220 non-const temporary that we access through a const pointer, that should
2221 work. We handle this here rather than change fold_indirect_ref_1
2222 because we're dealing with things like ADDR_EXPR of INTEGER_CST which
2223 don't really make sense outside of constant expression evaluation. Also
2224 we want to allow folding to COMPONENT_REF, which could cause trouble
2225 with TBAA in fold_indirect_ref_1.
2227 Try to keep this function synced with fold_indirect_ref_1. */
2229 static tree
2230 cxx_fold_indirect_ref (location_t loc, tree type, tree op0, bool *empty_base)
2232 tree sub, subtype;
2234 sub = op0;
2235 STRIP_NOPS (sub);
2236 subtype = TREE_TYPE (sub);
2237 if (!POINTER_TYPE_P (subtype))
2238 return NULL_TREE;
2240 if (TREE_CODE (sub) == ADDR_EXPR)
2242 tree op = TREE_OPERAND (sub, 0);
2243 tree optype = TREE_TYPE (op);
2245 /* *&CONST_DECL -> to the value of the const decl. */
2246 if (TREE_CODE (op) == CONST_DECL)
2247 return DECL_INITIAL (op);
2248 /* *&p => p; make sure to handle *&"str"[cst] here. */
2249 if (same_type_ignoring_top_level_qualifiers_p (optype, type))
2251 tree fop = fold_read_from_constant_string (op);
2252 if (fop)
2253 return fop;
2254 else
2255 return op;
2257 /* *(foo *)&fooarray => fooarray[0] */
2258 else if (TREE_CODE (optype) == ARRAY_TYPE
2259 && (same_type_ignoring_top_level_qualifiers_p
2260 (type, TREE_TYPE (optype))))
2262 tree type_domain = TYPE_DOMAIN (optype);
2263 tree min_val = size_zero_node;
2264 if (type_domain && TYPE_MIN_VALUE (type_domain))
2265 min_val = TYPE_MIN_VALUE (type_domain);
2266 return build4_loc (loc, ARRAY_REF, type, op, min_val,
2267 NULL_TREE, NULL_TREE);
2269 /* *(foo *)&complexfoo => __real__ complexfoo */
2270 else if (TREE_CODE (optype) == COMPLEX_TYPE
2271 && (same_type_ignoring_top_level_qualifiers_p
2272 (type, TREE_TYPE (optype))))
2273 return fold_build1_loc (loc, REALPART_EXPR, type, op);
2274 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
2275 else if (TREE_CODE (optype) == VECTOR_TYPE
2276 && (same_type_ignoring_top_level_qualifiers_p
2277 (type, TREE_TYPE (optype))))
2279 tree part_width = TYPE_SIZE (type);
2280 tree index = bitsize_int (0);
2281 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
2283 /* Also handle conversion to an empty base class, which
2284 is represented with a NOP_EXPR. */
2285 else if (is_empty_class (type)
2286 && CLASS_TYPE_P (optype)
2287 && DERIVED_FROM_P (type, optype))
2289 *empty_base = true;
2290 return op;
2292 /* *(foo *)&struct_with_foo_field => COMPONENT_REF */
2293 else if (RECORD_OR_UNION_TYPE_P (optype))
2295 tree field = TYPE_FIELDS (optype);
2296 for (; field; field = DECL_CHAIN (field))
2297 if (TREE_CODE (field) == FIELD_DECL
2298 && integer_zerop (byte_position (field))
2299 && (same_type_ignoring_top_level_qualifiers_p
2300 (TREE_TYPE (field), type)))
2302 return fold_build3 (COMPONENT_REF, type, op, field, NULL_TREE);
2303 break;
2307 else if (TREE_CODE (sub) == POINTER_PLUS_EXPR
2308 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
2310 tree op00 = TREE_OPERAND (sub, 0);
2311 tree op01 = TREE_OPERAND (sub, 1);
2313 STRIP_NOPS (op00);
2314 if (TREE_CODE (op00) == ADDR_EXPR)
2316 tree op00type;
2317 op00 = TREE_OPERAND (op00, 0);
2318 op00type = TREE_TYPE (op00);
2320 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
2321 if (TREE_CODE (op00type) == VECTOR_TYPE
2322 && (same_type_ignoring_top_level_qualifiers_p
2323 (type, TREE_TYPE (op00type))))
2325 HOST_WIDE_INT offset = tree_to_shwi (op01);
2326 tree part_width = TYPE_SIZE (type);
2327 unsigned HOST_WIDE_INT part_widthi = tree_to_shwi (part_width)/BITS_PER_UNIT;
2328 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
2329 tree index = bitsize_int (indexi);
2331 if (offset / part_widthi < TYPE_VECTOR_SUBPARTS (op00type))
2332 return fold_build3_loc (loc,
2333 BIT_FIELD_REF, type, op00,
2334 part_width, index);
2337 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
2338 else if (TREE_CODE (op00type) == COMPLEX_TYPE
2339 && (same_type_ignoring_top_level_qualifiers_p
2340 (type, TREE_TYPE (op00type))))
2342 tree size = TYPE_SIZE_UNIT (type);
2343 if (tree_int_cst_equal (size, op01))
2344 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
2346 /* ((foo *)&fooarray)[1] => fooarray[1] */
2347 else if (TREE_CODE (op00type) == ARRAY_TYPE
2348 && (same_type_ignoring_top_level_qualifiers_p
2349 (type, TREE_TYPE (op00type))))
2351 tree type_domain = TYPE_DOMAIN (op00type);
2352 tree min_val = size_zero_node;
2353 if (type_domain && TYPE_MIN_VALUE (type_domain))
2354 min_val = TYPE_MIN_VALUE (type_domain);
2355 op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
2356 TYPE_SIZE_UNIT (type));
2357 op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
2358 return build4_loc (loc, ARRAY_REF, type, op00, op01,
2359 NULL_TREE, NULL_TREE);
2361 /* Also handle conversion to an empty base class, which
2362 is represented with a NOP_EXPR. */
2363 else if (is_empty_class (type)
2364 && CLASS_TYPE_P (op00type)
2365 && DERIVED_FROM_P (type, op00type))
2367 *empty_base = true;
2368 return op00;
2370 /* ((foo *)&struct_with_foo_field)[1] => COMPONENT_REF */
2371 else if (RECORD_OR_UNION_TYPE_P (op00type))
2373 tree field = TYPE_FIELDS (op00type);
2374 for (; field; field = DECL_CHAIN (field))
2375 if (TREE_CODE (field) == FIELD_DECL
2376 && tree_int_cst_equal (byte_position (field), op01)
2377 && (same_type_ignoring_top_level_qualifiers_p
2378 (TREE_TYPE (field), type)))
2380 return fold_build3 (COMPONENT_REF, type, op00,
2381 field, NULL_TREE);
2382 break;
2387 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
2388 else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
2389 && (same_type_ignoring_top_level_qualifiers_p
2390 (type, TREE_TYPE (TREE_TYPE (subtype)))))
2392 tree type_domain;
2393 tree min_val = size_zero_node;
2394 tree newsub = cxx_fold_indirect_ref (loc, TREE_TYPE (subtype), sub, NULL);
2395 if (newsub)
2396 sub = newsub;
2397 else
2398 sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub);
2399 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
2400 if (type_domain && TYPE_MIN_VALUE (type_domain))
2401 min_val = TYPE_MIN_VALUE (type_domain);
2402 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
2403 NULL_TREE);
2406 return NULL_TREE;
2409 static tree
2410 cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t,
2411 bool addr,
2412 bool *non_constant_p, bool *overflow_p)
2414 tree orig_op0 = TREE_OPERAND (t, 0);
2415 tree op0 = cxx_eval_constant_expression (ctx, orig_op0,
2416 /*addr*/false, non_constant_p,
2417 overflow_p);
2418 bool empty_base = false;
2419 tree r;
2421 /* Don't VERIFY_CONSTANT here. */
2422 if (*non_constant_p)
2423 return t;
2425 r = cxx_fold_indirect_ref (EXPR_LOCATION (t), TREE_TYPE (t), op0,
2426 &empty_base);
2428 if (r)
2429 r = cxx_eval_constant_expression (ctx, r,
2430 addr, non_constant_p, overflow_p);
2431 else
2433 tree sub = op0;
2434 STRIP_NOPS (sub);
2435 if (TREE_CODE (sub) == ADDR_EXPR)
2437 /* We couldn't fold to a constant value. Make sure it's not
2438 something we should have been able to fold. */
2439 gcc_assert (!same_type_ignoring_top_level_qualifiers_p
2440 (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t)));
2441 /* DR 1188 says we don't have to deal with this. */
2442 if (!ctx->quiet)
2443 error ("accessing value of %qE through a %qT glvalue in a "
2444 "constant expression", build_fold_indirect_ref (sub),
2445 TREE_TYPE (t));
2446 *non_constant_p = true;
2447 return t;
2451 /* If we're pulling out the value of an empty base, make sure
2452 that the whole object is constant and then return an empty
2453 CONSTRUCTOR. */
2454 if (empty_base && !addr)
2456 VERIFY_CONSTANT (r);
2457 r = build_constructor (TREE_TYPE (t), NULL);
2458 TREE_CONSTANT (r) = true;
2461 if (r == NULL_TREE)
2463 if (addr && op0 != orig_op0)
2464 return build1 (INDIRECT_REF, TREE_TYPE (t), op0);
2465 if (!addr)
2466 VERIFY_CONSTANT (t);
2467 return t;
2469 return r;
2472 /* Complain about R, a VAR_DECL, not being usable in a constant expression.
2473 Shared between potential_constant_expression and
2474 cxx_eval_constant_expression. */
2476 static void
2477 non_const_var_error (tree r)
2479 tree type = TREE_TYPE (r);
2480 error ("the value of %qD is not usable in a constant "
2481 "expression", r);
2482 /* Avoid error cascade. */
2483 if (DECL_INITIAL (r) == error_mark_node)
2484 return;
2485 if (DECL_DECLARED_CONSTEXPR_P (r))
2486 inform (DECL_SOURCE_LOCATION (r),
2487 "%qD used in its own initializer", r);
2488 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
2490 if (!CP_TYPE_CONST_P (type))
2491 inform (DECL_SOURCE_LOCATION (r),
2492 "%q#D is not const", r);
2493 else if (CP_TYPE_VOLATILE_P (type))
2494 inform (DECL_SOURCE_LOCATION (r),
2495 "%q#D is volatile", r);
2496 else if (!DECL_INITIAL (r)
2497 || !TREE_CONSTANT (DECL_INITIAL (r)))
2498 inform (DECL_SOURCE_LOCATION (r),
2499 "%qD was not initialized with a constant "
2500 "expression", r);
2501 else
2502 gcc_unreachable ();
2504 else
2506 if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r))
2507 inform (DECL_SOURCE_LOCATION (r),
2508 "%qD was not declared %<constexpr%>", r);
2509 else
2510 inform (DECL_SOURCE_LOCATION (r),
2511 "%qD does not have integral or enumeration type",
2516 /* Subroutine of cxx_eval_constant_expression.
2517 Like cxx_eval_unary_expression, except for trinary expressions. */
2519 static tree
2520 cxx_eval_trinary_expression (const constexpr_ctx *ctx, tree t,
2521 bool addr,
2522 bool *non_constant_p, bool *overflow_p)
2524 int i;
2525 tree args[3];
2526 tree val;
2528 for (i = 0; i < 3; i++)
2530 args[i] = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, i),
2531 addr,
2532 non_constant_p, overflow_p);
2533 VERIFY_CONSTANT (args[i]);
2536 val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t),
2537 args[0], args[1], args[2]);
2538 if (val == NULL_TREE)
2539 return t;
2540 VERIFY_CONSTANT (val);
2541 return val;
2544 bool
2545 var_in_constexpr_fn (tree t)
2547 tree ctx = DECL_CONTEXT (t);
2548 return (cxx_dialect >= cxx14 && ctx && TREE_CODE (ctx) == FUNCTION_DECL
2549 && DECL_DECLARED_CONSTEXPR_P (ctx));
2552 /* Evaluate an INIT_EXPR or MODIFY_EXPR. */
2554 static tree
2555 cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
2556 bool addr,
2557 bool *non_constant_p, bool *overflow_p)
2559 constexpr_ctx new_ctx = *ctx;
2561 /* First we figure out where we're storing to. */
2562 tree target = TREE_OPERAND (t, 0);
2563 target = cxx_eval_constant_expression (ctx, target,
2564 true,
2565 non_constant_p, overflow_p);
2566 if (*non_constant_p)
2567 return t;
2569 /* And then find the underlying variable. */
2570 vec<tree,va_gc> *refs = make_tree_vector();
2571 tree object = NULL_TREE;
2572 for (tree probe = target; object == NULL_TREE; )
2574 switch (TREE_CODE (probe))
2576 case BIT_FIELD_REF:
2577 case COMPONENT_REF:
2578 case ARRAY_REF:
2579 vec_safe_push (refs, TREE_OPERAND (probe, 1));
2580 vec_safe_push (refs, TREE_TYPE (probe));
2581 probe = TREE_OPERAND (probe, 0);
2582 break;
2584 default:
2585 object = probe;
2589 /* And then find/build up our initializer for the path to the subobject
2590 we're initializing. */
2591 tree *valp;
2592 if (DECL_P (object))
2593 valp = ctx->values->get (object);
2594 else
2595 valp = NULL;
2596 if (!valp)
2598 /* A constant-expression cannot modify objects from outside the
2599 constant-expression. */
2600 if (!ctx->quiet)
2601 error ("modification of %qE is not a constant-expression", object);
2602 *non_constant_p = true;
2603 return t;
2605 tree type = TREE_TYPE (object);
2606 while (!refs->is_empty())
2608 if (*valp == NULL_TREE)
2610 *valp = build_constructor (type, NULL);
2611 CONSTRUCTOR_NO_IMPLICIT_ZERO (*valp) = true;
2614 constructor_elt ce;
2615 type = refs->pop();
2616 ce.index = refs->pop();
2617 ce.value = NULL_TREE;
2619 unsigned HOST_WIDE_INT idx = 0;
2620 constructor_elt *cep = NULL;
2621 for (idx = 0;
2622 vec_safe_iterate (CONSTRUCTOR_ELTS (*valp), idx, &cep);
2623 idx++)
2624 /* ??? slow */
2625 if (cp_tree_equal (ce.index, cep->index))
2626 break;
2627 if (!cep)
2628 cep = vec_safe_push (CONSTRUCTOR_ELTS (*valp), ce);
2629 valp = &cep->value;
2631 release_tree_vector (refs);
2633 if ((AGGREGATE_TYPE_P (TREE_TYPE (t)) || VECTOR_TYPE_P (TREE_TYPE (t))))
2635 /* Create a new CONSTRUCTOR in case evaluation of the initializer
2636 wants to modify it. */
2637 *valp = new_ctx.ctor = build_constructor (TREE_TYPE (t), NULL);
2638 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx.ctor) = true;
2639 new_ctx.object = target;
2642 tree init = cxx_eval_constant_expression (&new_ctx, TREE_OPERAND (t, 1),
2643 false,
2644 non_constant_p, overflow_p);
2645 if (target == object)
2646 /* The hash table might have moved since the get earlier. */
2647 ctx->values->put (object, init);
2648 else
2649 *valp = init;
2651 if (*non_constant_p)
2652 return t;
2653 else if (addr)
2654 return target;
2655 else
2656 return init;
2659 /* Evaluate a ++ or -- expression. */
2661 static tree
2662 cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t,
2663 bool addr,
2664 bool *non_constant_p, bool *overflow_p)
2666 enum tree_code code = TREE_CODE (t);
2667 tree type = TREE_TYPE (t);
2668 tree op = TREE_OPERAND (t, 0);
2669 tree offset = TREE_OPERAND (t, 1);
2670 gcc_assert (TREE_CONSTANT (offset));
2672 /* The operand as an lvalue. */
2673 op = cxx_eval_constant_expression (ctx, op, true,
2674 non_constant_p, overflow_p);
2676 /* The operand as an rvalue. */
2677 tree val = rvalue (op);
2678 val = cxx_eval_constant_expression (ctx, val, false,
2679 non_constant_p, overflow_p);
2680 VERIFY_CONSTANT (val);
2682 /* The modified value. */
2683 bool inc = (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR);
2684 tree mod;
2685 if (POINTER_TYPE_P (type))
2687 /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
2688 offset = convert_to_ptrofftype (offset);
2689 if (!inc)
2690 offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset);
2691 mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset);
2693 else
2694 mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset);
2695 VERIFY_CONSTANT (mod);
2697 /* Storing the modified value. */
2698 tree store = build2 (MODIFY_EXPR, type, op, mod);
2699 cxx_eval_constant_expression (ctx, store,
2700 true, non_constant_p, overflow_p);
2702 /* And the value of the expression. */
2703 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
2705 /* Prefix ops are lvalues. */
2706 if (addr)
2707 return op;
2708 else
2709 /* But we optimize when the caller wants an rvalue. */
2710 return mod;
2712 else
2713 /* Postfix ops are rvalues. */
2714 return val;
2717 /* Predicates for the meaning of *jump_target. */
2719 static bool
2720 returns (tree *jump_target)
2722 return *jump_target
2723 && TREE_CODE (*jump_target) == RETURN_EXPR;
2726 static bool
2727 breaks (tree *jump_target)
2729 return *jump_target
2730 && TREE_CODE (*jump_target) == LABEL_DECL
2731 && LABEL_DECL_BREAK (*jump_target);
2734 static bool
2735 continues (tree *jump_target)
2737 return *jump_target
2738 && TREE_CODE (*jump_target) == LABEL_DECL
2739 && LABEL_DECL_CONTINUE (*jump_target);
2742 static bool
2743 switches (tree *jump_target)
2745 return *jump_target
2746 && TREE_CODE (*jump_target) == INTEGER_CST;
2749 /* Subroutine of cxx_eval_statement_list. Determine whether the statement
2750 at I matches *jump_target. If we're looking for a case label and we see
2751 the default label, copy I into DEFAULT_LABEL. */
2753 static bool
2754 label_matches (tree *jump_target, tree_stmt_iterator i,
2755 tree_stmt_iterator& default_label)
2757 tree stmt = tsi_stmt (i);
2758 switch (TREE_CODE (*jump_target))
2760 case LABEL_DECL:
2761 if (TREE_CODE (stmt) == LABEL_EXPR
2762 && LABEL_EXPR_LABEL (stmt) == *jump_target)
2763 return true;
2764 break;
2766 case INTEGER_CST:
2767 if (TREE_CODE (stmt) == CASE_LABEL_EXPR)
2769 if (!CASE_LOW (stmt))
2770 default_label = i;
2771 else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt)))
2772 return true;
2774 break;
2776 default:
2777 gcc_unreachable ();
2779 return false;
2782 /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
2783 semantics, for switch, break, continue, and return. */
2785 static tree
2786 cxx_eval_statement_list (const constexpr_ctx *ctx, tree t,
2787 bool *non_constant_p, bool *overflow_p,
2788 tree *jump_target)
2790 tree_stmt_iterator i;
2791 tree_stmt_iterator default_label = tree_stmt_iterator();
2792 tree local_target;
2793 /* In a statement-expression we want to return the last value. */
2794 tree r = NULL_TREE;
2795 if (!jump_target)
2797 local_target = NULL_TREE;
2798 jump_target = &local_target;
2800 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
2802 reenter:
2803 tree stmt = tsi_stmt (i);
2804 if (*jump_target)
2806 if (TREE_CODE (stmt) == STATEMENT_LIST)
2807 /* The label we want might be inside. */;
2808 else if (label_matches (jump_target, i, default_label))
2809 /* Found it. */
2810 *jump_target = NULL_TREE;
2811 else
2812 continue;
2814 r = cxx_eval_constant_expression (ctx, stmt, false,
2815 non_constant_p, overflow_p,
2816 jump_target);
2817 if (*non_constant_p)
2818 break;
2819 if (returns (jump_target) || breaks (jump_target))
2820 break;
2822 if (switches (jump_target) && !tsi_end_p (default_label))
2824 i = default_label;
2825 *jump_target = NULL_TREE;
2826 goto reenter;
2828 return r;
2831 /* Evaluate a LOOP_EXPR for side-effects. Handles break and return
2832 semantics; continue semantics are covered by cxx_eval_statement_list. */
2834 static tree
2835 cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t,
2836 bool *non_constant_p, bool *overflow_p,
2837 tree *jump_target)
2839 tree body = TREE_OPERAND (t, 0);
2840 while (true)
2842 cxx_eval_statement_list (ctx, body,
2843 non_constant_p, overflow_p, jump_target);
2844 if (returns (jump_target) || breaks (jump_target))
2845 break;
2847 if (breaks (jump_target))
2848 *jump_target = NULL_TREE;
2849 return NULL_TREE;
2852 /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
2853 semantics. */
2855 static tree
2856 cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t,
2857 bool *non_constant_p, bool *overflow_p,
2858 tree *jump_target)
2860 tree cond = TREE_OPERAND (t, 0);
2861 cond = cxx_eval_constant_expression (ctx, cond, false,
2862 non_constant_p, overflow_p);
2863 VERIFY_CONSTANT (cond);
2864 *jump_target = cond;
2866 tree body = TREE_OPERAND (t, 1);
2867 cxx_eval_statement_list (ctx, body,
2868 non_constant_p, overflow_p, jump_target);
2869 if (breaks (jump_target) || switches (jump_target))
2870 *jump_target = NULL_TREE;
2871 return NULL_TREE;
2874 /* Attempt to reduce the expression T to a constant value.
2875 On failure, issue diagnostic and return error_mark_node. */
2876 /* FIXME unify with c_fully_fold */
2877 /* FIXME overflow_p is too global */
2879 static tree
2880 cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
2881 bool addr,
2882 bool *non_constant_p, bool *overflow_p,
2883 tree *jump_target)
2885 constexpr_ctx new_ctx;
2886 tree r = t;
2888 if (t == error_mark_node)
2890 *non_constant_p = true;
2891 return t;
2893 if (CONSTANT_CLASS_P (t))
2895 if (TREE_CODE (t) == PTRMEM_CST)
2896 t = cplus_expand_constant (t);
2897 else if (TREE_OVERFLOW (t) && (!flag_permissive || ctx->quiet))
2898 *overflow_p = true;
2899 return t;
2901 if (TREE_CODE (t) != NOP_EXPR
2902 && reduced_constant_expression_p (t))
2903 return fold (t);
2905 switch (TREE_CODE (t))
2907 case RESULT_DECL:
2908 if (addr)
2909 return t;
2910 /* We ask for an rvalue for the RESULT_DECL when indirecting
2911 through an invisible reference. */
2912 gcc_assert (DECL_BY_REFERENCE (t));
2913 return (*ctx->values->get (t));
2915 case VAR_DECL:
2916 if (addr)
2917 return t;
2918 /* else fall through. */
2919 case CONST_DECL:
2920 if (ctx->strict)
2921 r = decl_really_constant_value (t);
2922 else
2923 r = decl_constant_value (t);
2924 if (TREE_CODE (r) == TARGET_EXPR
2925 && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR)
2926 r = TARGET_EXPR_INITIAL (r);
2927 if (TREE_CODE (r) == VAR_DECL)
2928 if (tree *p = ctx->values->get (r))
2929 r = *p;
2930 if (DECL_P (r))
2932 if (!ctx->quiet)
2933 non_const_var_error (r);
2934 *non_constant_p = true;
2936 break;
2938 case FUNCTION_DECL:
2939 case TEMPLATE_DECL:
2940 case LABEL_DECL:
2941 case LABEL_EXPR:
2942 case CASE_LABEL_EXPR:
2943 return t;
2945 case PARM_DECL:
2946 if (!use_new_call && ctx
2947 && ctx->call && DECL_CONTEXT (t) == ctx->call->fundef->decl)
2948 r = lookup_parameter_binding (ctx->call, t);
2949 else if (addr && TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
2950 /* glvalue use. */;
2951 else if (tree *p = ctx->values->get (r))
2952 r = *p;
2953 else if (addr)
2954 /* Defer in case this is only used for its type. */;
2955 else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
2956 /* Defer, there's no lvalue->rvalue conversion. */;
2957 else if (is_empty_class (TREE_TYPE (t)))
2959 /* If the class is empty, we aren't actually loading anything. */
2960 r = build_constructor (TREE_TYPE (t), NULL);
2961 TREE_CONSTANT (r) = true;
2963 else
2965 if (!ctx->quiet)
2966 error ("%qE is not a constant expression", t);
2967 *non_constant_p = true;
2969 break;
2971 case CALL_EXPR:
2972 case AGGR_INIT_EXPR:
2973 r = cxx_eval_call_expression (ctx, t, addr,
2974 non_constant_p, overflow_p);
2975 break;
2977 case DECL_EXPR:
2979 r = DECL_EXPR_DECL (t);
2980 if (AGGREGATE_TYPE_P (TREE_TYPE (r))
2981 || VECTOR_TYPE_P (TREE_TYPE (r)))
2983 new_ctx = *ctx;
2984 new_ctx.object = r;
2985 new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL);
2986 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx.ctor) = true;
2987 new_ctx.values->put (r, new_ctx.ctor);
2988 ctx = &new_ctx;
2991 if (tree init = DECL_INITIAL (r))
2993 init = cxx_eval_constant_expression (ctx, init,
2994 false,
2995 non_constant_p, overflow_p);
2996 ctx->values->put (r, init);
2998 else if (ctx == &new_ctx)
2999 /* We gave it a CONSTRUCTOR above. */;
3000 else
3001 ctx->values->put (r, NULL_TREE);
3003 break;
3005 case TARGET_EXPR:
3006 if (!literal_type_p (TREE_TYPE (t)))
3008 if (!ctx->quiet)
3010 error ("temporary of non-literal type %qT in a "
3011 "constant expression", TREE_TYPE (t));
3012 explain_non_literal_class (TREE_TYPE (t));
3014 *non_constant_p = true;
3015 break;
3017 if ((AGGREGATE_TYPE_P (TREE_TYPE (t)) || VECTOR_TYPE_P (TREE_TYPE (t))))
3019 /* We're being expanded without an explicit target, so start
3020 initializing a new object; expansion with an explicit target
3021 strips the TARGET_EXPR before we get here. */
3022 new_ctx = *ctx;
3023 new_ctx.ctor = build_constructor (TREE_TYPE (t), NULL);
3024 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx.ctor) = true;
3025 new_ctx.object = TARGET_EXPR_SLOT (t);
3026 ctx->values->put (new_ctx.object, new_ctx.ctor);
3027 ctx = &new_ctx;
3029 /* Pass false for 'addr' because this indicates
3030 initialization of a temporary. */
3031 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
3032 false,
3033 non_constant_p, overflow_p);
3034 if (!*non_constant_p)
3035 /* Adjust the type of the result to the type of the temporary. */
3036 r = adjust_temp_type (TREE_TYPE (t), r);
3037 if (addr)
3039 tree slot = TARGET_EXPR_SLOT (t);
3040 ctx->values->put (slot, r);
3041 return slot;
3043 break;
3045 case INIT_EXPR:
3046 if (!use_new_call)
3048 /* In C++11 constexpr evaluation we are looking for the value,
3049 not the side-effect of the initialization. */
3050 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
3051 false,
3052 non_constant_p, overflow_p);
3053 break;
3055 /* else fall through */
3056 case MODIFY_EXPR:
3057 r = cxx_eval_store_expression (ctx, t, addr,
3058 non_constant_p, overflow_p);
3059 break;
3061 case SCOPE_REF:
3062 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
3063 addr,
3064 non_constant_p, overflow_p);
3065 break;
3067 case RETURN_EXPR:
3068 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
3069 addr,
3070 non_constant_p, overflow_p);
3071 *jump_target = t;
3072 break;
3074 case SAVE_EXPR:
3075 /* Avoid evaluating a SAVE_EXPR more than once. */
3076 if (tree *p = ctx->values->get (t))
3077 r = *p;
3078 else
3080 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), addr,
3081 non_constant_p, overflow_p);
3082 ctx->values->put (t, r);
3084 break;
3086 case NON_LVALUE_EXPR:
3087 case TRY_CATCH_EXPR:
3088 case CLEANUP_POINT_EXPR:
3089 case MUST_NOT_THROW_EXPR:
3090 case EXPR_STMT:
3091 case EH_SPEC_BLOCK:
3092 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
3093 addr,
3094 non_constant_p, overflow_p,
3095 jump_target);
3096 break;
3098 /* These differ from cxx_eval_unary_expression in that this doesn't
3099 check for a constant operand or result; an address can be
3100 constant without its operand being, and vice versa. */
3101 case INDIRECT_REF:
3102 r = cxx_eval_indirect_ref (ctx, t, addr,
3103 non_constant_p, overflow_p);
3104 break;
3106 case ADDR_EXPR:
3108 tree oldop = TREE_OPERAND (t, 0);
3109 tree op = cxx_eval_constant_expression (ctx, oldop,
3110 /*addr*/true,
3111 non_constant_p, overflow_p);
3112 /* Don't VERIFY_CONSTANT here. */
3113 if (*non_constant_p)
3114 return t;
3115 gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR);
3116 /* This function does more aggressive folding than fold itself. */
3117 r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
3118 if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
3119 return t;
3120 break;
3123 case REALPART_EXPR:
3124 case IMAGPART_EXPR:
3125 case CONJ_EXPR:
3126 case FIX_TRUNC_EXPR:
3127 case FLOAT_EXPR:
3128 case NEGATE_EXPR:
3129 case ABS_EXPR:
3130 case BIT_NOT_EXPR:
3131 case TRUTH_NOT_EXPR:
3132 case FIXED_CONVERT_EXPR:
3133 r = cxx_eval_unary_expression (ctx, t, addr,
3134 non_constant_p, overflow_p);
3135 break;
3137 case SIZEOF_EXPR:
3138 if (SIZEOF_EXPR_TYPE_P (t))
3139 r = cxx_sizeof_or_alignof_type (TREE_TYPE (TREE_OPERAND (t, 0)),
3140 SIZEOF_EXPR, false);
3141 else if (TYPE_P (TREE_OPERAND (t, 0)))
3142 r = cxx_sizeof_or_alignof_type (TREE_OPERAND (t, 0), SIZEOF_EXPR,
3143 false);
3144 else
3145 r = cxx_sizeof_or_alignof_expr (TREE_OPERAND (t, 0), SIZEOF_EXPR,
3146 false);
3147 if (r == error_mark_node)
3148 r = size_one_node;
3149 VERIFY_CONSTANT (r);
3150 break;
3152 case COMPOUND_EXPR:
3154 /* check_return_expr sometimes wraps a TARGET_EXPR in a
3155 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
3156 introduced by build_call_a. */
3157 tree op0 = TREE_OPERAND (t, 0);
3158 tree op1 = TREE_OPERAND (t, 1);
3159 STRIP_NOPS (op1);
3160 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
3161 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
3162 r = cxx_eval_constant_expression (ctx, op0,
3163 addr, non_constant_p, overflow_p,
3164 jump_target);
3165 else
3167 /* Check that the LHS is constant and then discard it. */
3168 cxx_eval_constant_expression (ctx, op0,
3169 false, non_constant_p, overflow_p,
3170 jump_target);
3171 op1 = TREE_OPERAND (t, 1);
3172 r = cxx_eval_constant_expression (ctx, op1,
3173 addr, non_constant_p, overflow_p,
3174 jump_target);
3177 break;
3179 case POINTER_PLUS_EXPR:
3180 case PLUS_EXPR:
3181 case MINUS_EXPR:
3182 case MULT_EXPR:
3183 case TRUNC_DIV_EXPR:
3184 case CEIL_DIV_EXPR:
3185 case FLOOR_DIV_EXPR:
3186 case ROUND_DIV_EXPR:
3187 case TRUNC_MOD_EXPR:
3188 case CEIL_MOD_EXPR:
3189 case ROUND_MOD_EXPR:
3190 case RDIV_EXPR:
3191 case EXACT_DIV_EXPR:
3192 case MIN_EXPR:
3193 case MAX_EXPR:
3194 case LSHIFT_EXPR:
3195 case RSHIFT_EXPR:
3196 case LROTATE_EXPR:
3197 case RROTATE_EXPR:
3198 case BIT_IOR_EXPR:
3199 case BIT_XOR_EXPR:
3200 case BIT_AND_EXPR:
3201 case TRUTH_XOR_EXPR:
3202 case LT_EXPR:
3203 case LE_EXPR:
3204 case GT_EXPR:
3205 case GE_EXPR:
3206 case EQ_EXPR:
3207 case NE_EXPR:
3208 case UNORDERED_EXPR:
3209 case ORDERED_EXPR:
3210 case UNLT_EXPR:
3211 case UNLE_EXPR:
3212 case UNGT_EXPR:
3213 case UNGE_EXPR:
3214 case UNEQ_EXPR:
3215 case LTGT_EXPR:
3216 case RANGE_EXPR:
3217 case COMPLEX_EXPR:
3218 r = cxx_eval_binary_expression (ctx, t, addr,
3219 non_constant_p, overflow_p);
3220 break;
3222 /* fold can introduce non-IF versions of these; still treat them as
3223 short-circuiting. */
3224 case TRUTH_AND_EXPR:
3225 case TRUTH_ANDIF_EXPR:
3226 r = cxx_eval_logical_expression (ctx, t, boolean_false_node,
3227 boolean_true_node,
3228 addr,
3229 non_constant_p, overflow_p);
3230 break;
3232 case TRUTH_OR_EXPR:
3233 case TRUTH_ORIF_EXPR:
3234 r = cxx_eval_logical_expression (ctx, t, boolean_true_node,
3235 boolean_false_node,
3236 addr,
3237 non_constant_p, overflow_p);
3238 break;
3240 case ARRAY_REF:
3241 r = cxx_eval_array_reference (ctx, t, addr,
3242 non_constant_p, overflow_p);
3243 break;
3245 case COMPONENT_REF:
3246 if (is_overloaded_fn (t))
3248 /* We can only get here in checking mode via
3249 build_non_dependent_expr, because any expression that
3250 calls or takes the address of the function will have
3251 pulled a FUNCTION_DECL out of the COMPONENT_REF. */
3252 gcc_checking_assert (ctx->quiet || errorcount);
3253 *non_constant_p = true;
3254 return t;
3256 r = cxx_eval_component_reference (ctx, t, addr,
3257 non_constant_p, overflow_p);
3258 break;
3260 case BIT_FIELD_REF:
3261 r = cxx_eval_bit_field_ref (ctx, t, addr,
3262 non_constant_p, overflow_p);
3263 break;
3265 case COND_EXPR:
3266 case VEC_COND_EXPR:
3267 r = cxx_eval_conditional_expression (ctx, t, addr,
3268 non_constant_p, overflow_p,
3269 jump_target);
3270 break;
3272 case CONSTRUCTOR:
3273 r = cxx_eval_bare_aggregate (ctx, t, addr,
3274 non_constant_p, overflow_p);
3275 break;
3277 case VEC_INIT_EXPR:
3278 /* We can get this in a defaulted constructor for a class with a
3279 non-static data member of array type. Either the initializer will
3280 be NULL, meaning default-initialization, or it will be an lvalue
3281 or xvalue of the same type, meaning direct-initialization from the
3282 corresponding member. */
3283 r = cxx_eval_vec_init (ctx, t, addr,
3284 non_constant_p, overflow_p);
3285 break;
3287 case FMA_EXPR:
3288 case VEC_PERM_EXPR:
3289 r = cxx_eval_trinary_expression (ctx, t, addr,
3290 non_constant_p, overflow_p);
3291 break;
3293 case CONVERT_EXPR:
3294 case VIEW_CONVERT_EXPR:
3295 case NOP_EXPR:
3297 tree oldop = TREE_OPERAND (t, 0);
3298 tree op = cxx_eval_constant_expression (ctx, oldop,
3299 addr,
3300 non_constant_p, overflow_p);
3301 if (*non_constant_p)
3302 return t;
3303 if (POINTER_TYPE_P (TREE_TYPE (t))
3304 && TREE_CODE (op) == INTEGER_CST
3305 && !integer_zerop (op))
3307 if (!ctx->quiet)
3308 error_at (EXPR_LOC_OR_LOC (t, input_location),
3309 "reinterpret_cast from integer to pointer");
3310 *non_constant_p = true;
3311 return t;
3313 if (op == oldop)
3314 /* We didn't fold at the top so we could check for ptr-int
3315 conversion. */
3316 return fold (t);
3317 r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), op);
3318 /* Conversion of an out-of-range value has implementation-defined
3319 behavior; the language considers it different from arithmetic
3320 overflow, which is undefined. */
3321 if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op))
3322 TREE_OVERFLOW (r) = false;
3324 break;
3326 case EMPTY_CLASS_EXPR:
3327 /* This is good enough for a function argument that might not get
3328 used, and they can't do anything with it, so just return it. */
3329 return t;
3331 case STATEMENT_LIST:
3332 new_ctx = *ctx;
3333 new_ctx.ctor = new_ctx.object = NULL_TREE;
3334 return cxx_eval_statement_list (&new_ctx, t,
3335 non_constant_p, overflow_p, jump_target);
3337 case BIND_EXPR:
3338 return cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t),
3339 addr,
3340 non_constant_p, overflow_p,
3341 jump_target);
3343 case PREINCREMENT_EXPR:
3344 case POSTINCREMENT_EXPR:
3345 case PREDECREMENT_EXPR:
3346 case POSTDECREMENT_EXPR:
3347 return cxx_eval_increment_expression (ctx, t,
3348 addr, non_constant_p, overflow_p);
3350 case LAMBDA_EXPR:
3351 case NEW_EXPR:
3352 case VEC_NEW_EXPR:
3353 case DELETE_EXPR:
3354 case VEC_DELETE_EXPR:
3355 case THROW_EXPR:
3356 case MODOP_EXPR:
3357 /* GCC internal stuff. */
3358 case VA_ARG_EXPR:
3359 case OBJ_TYPE_REF:
3360 case WITH_CLEANUP_EXPR:
3361 case NON_DEPENDENT_EXPR:
3362 case BASELINK:
3363 case OFFSET_REF:
3364 if (!ctx->quiet)
3365 error_at (EXPR_LOC_OR_LOC (t, input_location),
3366 "expression %qE is not a constant-expression", t);
3367 *non_constant_p = true;
3368 break;
3370 case PLACEHOLDER_EXPR:
3371 if (!ctx || !ctx->ctor || (addr && !ctx->object))
3373 /* A placeholder without a referent. We can get here when
3374 checking whether NSDMIs are noexcept, or in massage_init_elt;
3375 just say it's non-constant for now. */
3376 gcc_assert (ctx->quiet);
3377 *non_constant_p = true;
3378 break;
3380 else
3382 /* Use of the value or address of the current object. We could
3383 use ctx->object unconditionally, but using ctx->ctor when we
3384 can is a minor optimization. */
3385 tree ctor = addr ? ctx->object : ctx->ctor;
3386 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3387 (TREE_TYPE (t), TREE_TYPE (ctor)));
3388 return cxx_eval_constant_expression
3389 (ctx, ctor, addr,
3390 non_constant_p, overflow_p);
3392 break;
3394 case GOTO_EXPR:
3395 *jump_target = TREE_OPERAND (t, 0);
3396 gcc_assert (breaks (jump_target) || continues (jump_target));
3397 break;
3399 case LOOP_EXPR:
3400 cxx_eval_loop_expr (ctx, t,
3401 non_constant_p, overflow_p, jump_target);
3402 break;
3404 case SWITCH_EXPR:
3405 cxx_eval_switch_expr (ctx, t,
3406 non_constant_p, overflow_p, jump_target);
3407 break;
3409 default:
3410 internal_error ("unexpected expression %qE of kind %s", t,
3411 get_tree_code_name (TREE_CODE (t)));
3412 *non_constant_p = true;
3413 break;
3416 if (r == error_mark_node)
3417 *non_constant_p = true;
3419 if (*non_constant_p)
3420 return t;
3421 else
3422 return r;
3425 static tree
3426 cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant,
3427 bool strict = true, tree object = NULL_TREE)
3429 bool non_constant_p = false;
3430 bool overflow_p = false;
3431 constexpr_ctx ctx = { NULL, NULL, NULL, NULL, allow_non_constant, strict };
3432 hash_map<tree,tree> map;
3433 ctx.values = &map;
3434 tree type = initialized_type (t);
3435 tree r = t;
3436 if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
3438 /* In C++14 an NSDMI can participate in aggregate initialization,
3439 and can refer to the address of the object being initialized, so
3440 we need to pass in the relevant VAR_DECL if we want to do the
3441 evaluation in a single pass. The evaluation will dynamically
3442 update ctx.values for the VAR_DECL. We use the same strategy
3443 for C++11 constexpr constructors that refer to the object being
3444 initialized. */
3445 ctx.ctor = build_constructor (type, NULL);
3446 CONSTRUCTOR_NO_IMPLICIT_ZERO (ctx.ctor) = true;
3447 if (!object)
3449 if (TREE_CODE (t) == TARGET_EXPR)
3450 object = TARGET_EXPR_SLOT (t);
3451 else if (TREE_CODE (t) == AGGR_INIT_EXPR)
3452 object = AGGR_INIT_EXPR_SLOT (t);
3454 ctx.object = object;
3455 if (object)
3456 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3457 (type, TREE_TYPE (object)));
3458 if (object && DECL_P (object))
3459 map.put (object, ctx.ctor);
3460 if (TREE_CODE (r) == TARGET_EXPR)
3461 /* Avoid creating another CONSTRUCTOR when we expand the
3462 TARGET_EXPR. */
3463 r = TARGET_EXPR_INITIAL (r);
3466 r = cxx_eval_constant_expression (&ctx, r,
3467 false, &non_constant_p, &overflow_p);
3469 verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p);
3471 /* Mutable logic is a bit tricky: we want to allow initialization of
3472 constexpr variables with mutable members, but we can't copy those
3473 members to another constexpr variable. */
3474 if (TREE_CODE (r) == CONSTRUCTOR
3475 && CONSTRUCTOR_MUTABLE_POISON (r))
3477 if (!allow_non_constant)
3478 error ("%qE is not a constant expression because it refers to "
3479 "mutable subobjects of %qT", t, type);
3480 non_constant_p = true;
3483 /* Technically we should check this for all subexpressions, but that
3484 runs into problems with our internal representation of pointer
3485 subtraction and the 5.19 rules are still in flux. */
3486 if (CONVERT_EXPR_CODE_P (TREE_CODE (r))
3487 && ARITHMETIC_TYPE_P (TREE_TYPE (r))
3488 && TREE_CODE (TREE_OPERAND (r, 0)) == ADDR_EXPR)
3490 if (!allow_non_constant)
3491 error ("conversion from pointer type %qT "
3492 "to arithmetic type %qT in a constant-expression",
3493 TREE_TYPE (TREE_OPERAND (r, 0)), TREE_TYPE (r));
3494 non_constant_p = true;
3497 if (!non_constant_p && overflow_p)
3498 non_constant_p = true;
3500 if (non_constant_p && !allow_non_constant)
3501 return error_mark_node;
3502 else if (non_constant_p && TREE_CONSTANT (r))
3504 /* This isn't actually constant, so unset TREE_CONSTANT. */
3505 if (EXPR_P (r))
3506 r = copy_node (r);
3507 else if (TREE_CODE (r) == CONSTRUCTOR)
3508 r = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (r), r);
3509 else
3510 r = build_nop (TREE_TYPE (r), r);
3511 TREE_CONSTANT (r) = false;
3513 else if (non_constant_p || r == t)
3514 return t;
3516 if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
3518 if (TREE_CODE (t) == TARGET_EXPR
3519 && TARGET_EXPR_INITIAL (t) == r)
3520 return t;
3521 else
3523 r = get_target_expr (r);
3524 TREE_CONSTANT (r) = true;
3525 return r;
3528 else
3529 return r;
3532 /* Returns true if T is a valid subexpression of a constant expression,
3533 even if it isn't itself a constant expression. */
3535 bool
3536 is_sub_constant_expr (tree t)
3538 bool non_constant_p = false;
3539 bool overflow_p = false;
3540 constexpr_ctx ctx = { NULL, NULL, NULL, NULL, true, true };
3541 hash_map <tree, tree> map;
3542 ctx.values = &map;
3543 cxx_eval_constant_expression (&ctx, t, false, &non_constant_p,
3544 &overflow_p);
3545 return !non_constant_p && !overflow_p;
3548 /* If T represents a constant expression returns its reduced value.
3549 Otherwise return error_mark_node. If T is dependent, then
3550 return NULL. */
3552 tree
3553 cxx_constant_value (tree t, tree decl)
3555 return cxx_eval_outermost_constant_expr (t, false, true, decl);
3558 /* If T is a constant expression, returns its reduced value.
3559 Otherwise, if T does not have TREE_CONSTANT set, returns T.
3560 Otherwise, returns a version of T without TREE_CONSTANT. */
3562 tree
3563 maybe_constant_value (tree t, tree decl)
3565 tree r;
3567 if (instantiation_dependent_expression_p (t)
3568 || type_unknown_p (t)
3569 || BRACE_ENCLOSED_INITIALIZER_P (t)
3570 || !potential_constant_expression (t))
3572 if (TREE_OVERFLOW_P (t))
3574 t = build_nop (TREE_TYPE (t), t);
3575 TREE_CONSTANT (t) = false;
3577 return t;
3580 r = cxx_eval_outermost_constant_expr (t, true, true, decl);
3581 #ifdef ENABLE_CHECKING
3582 /* cp_tree_equal looks through NOPs, so allow them. */
3583 gcc_assert (r == t
3584 || CONVERT_EXPR_P (t)
3585 || TREE_CODE (t) == VIEW_CONVERT_EXPR
3586 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
3587 || !cp_tree_equal (r, t));
3588 #endif
3589 return r;
3592 /* Like maybe_constant_value but first fully instantiate the argument.
3594 Note: this is equivalent to instantiate_non_dependent_expr_sfinae
3595 (t, tf_none) followed by maybe_constant_value but is more efficient,
3596 because calls instantiation_dependent_expression_p and
3597 potential_constant_expression at most once. */
3599 tree
3600 fold_non_dependent_expr (tree t)
3602 if (t == NULL_TREE)
3603 return NULL_TREE;
3605 /* If we're in a template, but T isn't value dependent, simplify
3606 it. We're supposed to treat:
3608 template <typename T> void f(T[1 + 1]);
3609 template <typename T> void f(T[2]);
3611 as two declarations of the same function, for example. */
3612 if (processing_template_decl)
3614 if (!instantiation_dependent_expression_p (t)
3615 && potential_constant_expression (t))
3617 processing_template_decl_sentinel s;
3618 t = instantiate_non_dependent_expr_internal (t, tf_none);
3620 if (type_unknown_p (t)
3621 || BRACE_ENCLOSED_INITIALIZER_P (t))
3623 if (TREE_OVERFLOW_P (t))
3625 t = build_nop (TREE_TYPE (t), t);
3626 TREE_CONSTANT (t) = false;
3628 return t;
3631 tree r = cxx_eval_outermost_constant_expr (t, true, true, NULL_TREE);
3632 #ifdef ENABLE_CHECKING
3633 /* cp_tree_equal looks through NOPs, so allow them. */
3634 gcc_assert (r == t
3635 || CONVERT_EXPR_P (t)
3636 || TREE_CODE (t) == VIEW_CONVERT_EXPR
3637 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
3638 || !cp_tree_equal (r, t));
3639 #endif
3640 return r;
3642 else if (TREE_OVERFLOW_P (t))
3644 t = build_nop (TREE_TYPE (t), t);
3645 TREE_CONSTANT (t) = false;
3647 return t;
3650 return maybe_constant_value (t);
3653 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
3654 than wrapped in a TARGET_EXPR. */
3656 tree
3657 maybe_constant_init (tree t, tree decl)
3659 if (TREE_CODE (t) == EXPR_STMT)
3660 t = TREE_OPERAND (t, 0);
3661 if (TREE_CODE (t) == CONVERT_EXPR
3662 && VOID_TYPE_P (TREE_TYPE (t)))
3663 t = TREE_OPERAND (t, 0);
3664 if (TREE_CODE (t) == INIT_EXPR)
3665 t = TREE_OPERAND (t, 1);
3666 if (instantiation_dependent_expression_p (t)
3667 || type_unknown_p (t)
3668 || BRACE_ENCLOSED_INITIALIZER_P (t)
3669 || !potential_static_init_expression (t))
3670 /* Don't try to evaluate it. */;
3671 else
3672 t = cxx_eval_outermost_constant_expr (t, true, false, decl);
3673 if (TREE_CODE (t) == TARGET_EXPR)
3675 tree init = TARGET_EXPR_INITIAL (t);
3676 if (TREE_CODE (init) == CONSTRUCTOR)
3677 t = init;
3679 return t;
3682 #if 0
3683 /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
3684 /* Return true if the object referred to by REF has automatic or thread
3685 local storage. */
3687 enum { ck_ok, ck_bad, ck_unknown };
3688 static int
3689 check_automatic_or_tls (tree ref)
3691 machine_mode mode;
3692 HOST_WIDE_INT bitsize, bitpos;
3693 tree offset;
3694 int volatilep = 0, unsignedp = 0;
3695 tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset,
3696 &mode, &unsignedp, &volatilep, false);
3697 duration_kind dk;
3699 /* If there isn't a decl in the middle, we don't know the linkage here,
3700 and this isn't a constant expression anyway. */
3701 if (!DECL_P (decl))
3702 return ck_unknown;
3703 dk = decl_storage_duration (decl);
3704 return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok;
3706 #endif
3708 /* Return true if T denotes a potentially constant expression. Issue
3709 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
3710 an lvalue-rvalue conversion is implied.
3712 C++0x [expr.const] used to say
3714 6 An expression is a potential constant expression if it is
3715 a constant expression where all occurrences of function
3716 parameters are replaced by arbitrary constant expressions
3717 of the appropriate type.
3719 2 A conditional expression is a constant expression unless it
3720 involves one of the following as a potentially evaluated
3721 subexpression (3.2), but subexpressions of logical AND (5.14),
3722 logical OR (5.15), and conditional (5.16) operations that are
3723 not evaluated are not considered. */
3725 static bool
3726 potential_constant_expression_1 (tree t, bool want_rval, bool strict,
3727 tsubst_flags_t flags)
3729 #define RECUR(T,RV) potential_constant_expression_1 ((T), (RV), strict, flags)
3730 enum { any = false, rval = true };
3731 int i;
3732 tree tmp;
3734 if (t == error_mark_node)
3735 return false;
3736 if (t == NULL_TREE)
3737 return true;
3738 if (TREE_THIS_VOLATILE (t))
3740 if (flags & tf_error)
3741 error ("expression %qE has side-effects", t);
3742 return false;
3744 if (CONSTANT_CLASS_P (t))
3745 return true;
3747 switch (TREE_CODE (t))
3749 case FUNCTION_DECL:
3750 case BASELINK:
3751 case TEMPLATE_DECL:
3752 case OVERLOAD:
3753 case TEMPLATE_ID_EXPR:
3754 case LABEL_DECL:
3755 case LABEL_EXPR:
3756 case CASE_LABEL_EXPR:
3757 case CONST_DECL:
3758 case SIZEOF_EXPR:
3759 case ALIGNOF_EXPR:
3760 case OFFSETOF_EXPR:
3761 case NOEXCEPT_EXPR:
3762 case TEMPLATE_PARM_INDEX:
3763 case TRAIT_EXPR:
3764 case IDENTIFIER_NODE:
3765 case USERDEF_LITERAL:
3766 /* We can see a FIELD_DECL in a pointer-to-member expression. */
3767 case FIELD_DECL:
3768 case PARM_DECL:
3769 case USING_DECL:
3770 case USING_STMT:
3771 case PLACEHOLDER_EXPR:
3772 case BREAK_STMT:
3773 case CONTINUE_STMT:
3774 return true;
3776 case AGGR_INIT_EXPR:
3777 case CALL_EXPR:
3778 /* -- an invocation of a function other than a constexpr function
3779 or a constexpr constructor. */
3781 tree fun = get_function_named_in_call (t);
3782 const int nargs = call_expr_nargs (t);
3783 i = 0;
3785 if (fun == NULL_TREE)
3787 /* fold_call_expr can't do anything with IFN calls. */
3788 if (flags & tf_error)
3789 error_at (EXPR_LOC_OR_LOC (t, input_location),
3790 "call to internal function");
3791 return false;
3793 if (is_overloaded_fn (fun))
3795 if (TREE_CODE (fun) == FUNCTION_DECL)
3797 if (builtin_valid_in_constant_expr_p (fun))
3798 return true;
3799 if (!DECL_DECLARED_CONSTEXPR_P (fun)
3800 /* Allow any built-in function; if the expansion
3801 isn't constant, we'll deal with that then. */
3802 && !is_builtin_fn (fun))
3804 if (flags & tf_error)
3806 error_at (EXPR_LOC_OR_LOC (t, input_location),
3807 "call to non-constexpr function %qD", fun);
3808 explain_invalid_constexpr_fn (fun);
3810 return false;
3812 /* A call to a non-static member function takes the address
3813 of the object as the first argument. But in a constant
3814 expression the address will be folded away, so look
3815 through it now. */
3816 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
3817 && !DECL_CONSTRUCTOR_P (fun))
3819 tree x = get_nth_callarg (t, 0);
3820 if (is_this_parameter (x))
3821 return true;
3822 else if (!RECUR (x, rval))
3823 return false;
3824 i = 1;
3827 else
3829 if (!RECUR (fun, true))
3830 return false;
3831 fun = get_first_fn (fun);
3833 /* Skip initial arguments to base constructors. */
3834 if (DECL_BASE_CONSTRUCTOR_P (fun))
3835 i = num_artificial_parms_for (fun);
3836 fun = DECL_ORIGIN (fun);
3838 else
3840 if (RECUR (fun, rval))
3841 /* Might end up being a constant function pointer. */;
3842 else
3843 return false;
3845 for (; i < nargs; ++i)
3847 tree x = get_nth_callarg (t, i);
3848 if (!RECUR (x, rval))
3849 return false;
3851 return true;
3854 case NON_LVALUE_EXPR:
3855 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
3856 -- an lvalue of integral type that refers to a non-volatile
3857 const variable or static data member initialized with
3858 constant expressions, or
3860 -- an lvalue of literal type that refers to non-volatile
3861 object defined with constexpr, or that refers to a
3862 sub-object of such an object; */
3863 return RECUR (TREE_OPERAND (t, 0), rval);
3865 case VAR_DECL:
3866 if (want_rval
3867 && !decl_constant_var_p (t)
3868 && (strict
3869 || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t))
3870 || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t))
3871 && !var_in_constexpr_fn (t)
3872 && !dependent_type_p (TREE_TYPE (t)))
3874 if (flags & tf_error)
3875 non_const_var_error (t);
3876 return false;
3878 return true;
3880 case NOP_EXPR:
3881 case CONVERT_EXPR:
3882 case VIEW_CONVERT_EXPR:
3883 /* -- a reinterpret_cast. FIXME not implemented, and this rule
3884 may change to something more specific to type-punning (DR 1312). */
3886 tree from = TREE_OPERAND (t, 0);
3887 if (POINTER_TYPE_P (TREE_TYPE (t))
3888 && TREE_CODE (from) == INTEGER_CST
3889 && !integer_zerop (from))
3891 if (flags & tf_error)
3892 error_at (EXPR_LOC_OR_LOC (t, input_location),
3893 "reinterpret_cast from integer to pointer");
3894 return false;
3896 return (RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR));
3899 case ADDR_EXPR:
3900 /* -- a unary operator & that is applied to an lvalue that
3901 designates an object with thread or automatic storage
3902 duration; */
3903 t = TREE_OPERAND (t, 0);
3905 if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t))
3906 /* A pointer-to-member constant. */
3907 return true;
3909 #if 0
3910 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
3911 any checking here, as we might dereference the pointer later. If
3912 we remove this code, also remove check_automatic_or_tls. */
3913 i = check_automatic_or_tls (t);
3914 if (i == ck_ok)
3915 return true;
3916 if (i == ck_bad)
3918 if (flags & tf_error)
3919 error ("address-of an object %qE with thread local or "
3920 "automatic storage is not a constant expression", t);
3921 return false;
3923 #endif
3924 return RECUR (t, any);
3926 case COMPONENT_REF:
3927 case BIT_FIELD_REF:
3928 case ARROW_EXPR:
3929 case OFFSET_REF:
3930 /* -- a class member access unless its postfix-expression is
3931 of literal type or of pointer to literal type. */
3932 /* This test would be redundant, as it follows from the
3933 postfix-expression being a potential constant expression. */
3934 return RECUR (TREE_OPERAND (t, 0), want_rval);
3936 case EXPR_PACK_EXPANSION:
3937 return RECUR (PACK_EXPANSION_PATTERN (t), want_rval);
3939 case INDIRECT_REF:
3941 tree x = TREE_OPERAND (t, 0);
3942 STRIP_NOPS (x);
3943 if (is_this_parameter (x))
3945 if (DECL_CONTEXT (x)
3946 && !DECL_DECLARED_CONSTEXPR_P (DECL_CONTEXT (x)))
3948 if (flags & tf_error)
3949 error ("use of %<this%> in a constant expression");
3950 return false;
3952 return true;
3954 return RECUR (x, rval);
3957 case STATEMENT_LIST:
3959 tree_stmt_iterator i;
3960 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
3962 if (!RECUR (tsi_stmt (i), any))
3963 return false;
3965 return true;
3967 break;
3969 case MODIFY_EXPR:
3970 if (cxx_dialect < cxx14)
3971 goto fail;
3972 if (!RECUR (TREE_OPERAND (t, 0), any))
3973 return false;
3974 if (!RECUR (TREE_OPERAND (t, 1), rval))
3975 return false;
3976 return true;
3978 case MODOP_EXPR:
3979 if (cxx_dialect < cxx14)
3980 goto fail;
3981 if (!RECUR (TREE_OPERAND (t, 0), rval))
3982 return false;
3983 if (!RECUR (TREE_OPERAND (t, 2), rval))
3984 return false;
3985 return true;
3987 case IF_STMT:
3988 if (!RECUR (IF_COND (t), rval))
3989 return false;
3990 if (!RECUR (THEN_CLAUSE (t), any))
3991 return false;
3992 if (!RECUR (ELSE_CLAUSE (t), any))
3993 return false;
3994 return true;
3996 case DO_STMT:
3997 if (!RECUR (DO_COND (t), rval))
3998 return false;
3999 if (!RECUR (DO_BODY (t), any))
4000 return false;
4001 return true;
4003 case FOR_STMT:
4004 if (!RECUR (FOR_INIT_STMT (t), any))
4005 return false;
4006 if (!RECUR (FOR_COND (t), rval))
4007 return false;
4008 if (!RECUR (FOR_EXPR (t), any))
4009 return false;
4010 if (!RECUR (FOR_BODY (t), any))
4011 return false;
4012 return true;
4014 case WHILE_STMT:
4015 if (!RECUR (WHILE_COND (t), rval))
4016 return false;
4017 if (!RECUR (WHILE_BODY (t), any))
4018 return false;
4019 return true;
4021 case SWITCH_STMT:
4022 if (!RECUR (SWITCH_STMT_COND (t), rval))
4023 return false;
4024 if (!RECUR (SWITCH_STMT_BODY (t), any))
4025 return false;
4026 return true;
4028 case STMT_EXPR:
4029 return RECUR (STMT_EXPR_STMT (t), rval);
4031 case LAMBDA_EXPR:
4032 case DYNAMIC_CAST_EXPR:
4033 case PSEUDO_DTOR_EXPR:
4034 case NEW_EXPR:
4035 case VEC_NEW_EXPR:
4036 case DELETE_EXPR:
4037 case VEC_DELETE_EXPR:
4038 case THROW_EXPR:
4039 case OMP_ATOMIC:
4040 case OMP_ATOMIC_READ:
4041 case OMP_ATOMIC_CAPTURE_OLD:
4042 case OMP_ATOMIC_CAPTURE_NEW:
4043 /* GCC internal stuff. */
4044 case VA_ARG_EXPR:
4045 case OBJ_TYPE_REF:
4046 case TRANSACTION_EXPR:
4047 case ASM_EXPR:
4048 fail:
4049 if (flags & tf_error)
4050 error ("expression %qE is not a constant-expression", t);
4051 return false;
4053 case TYPEID_EXPR:
4054 /* -- a typeid expression whose operand is of polymorphic
4055 class type; */
4057 tree e = TREE_OPERAND (t, 0);
4058 if (!TYPE_P (e) && !type_dependent_expression_p (e)
4059 && TYPE_POLYMORPHIC_P (TREE_TYPE (e)))
4061 if (flags & tf_error)
4062 error ("typeid-expression is not a constant expression "
4063 "because %qE is of polymorphic type", e);
4064 return false;
4066 return true;
4069 case MINUS_EXPR:
4070 /* -- a subtraction where both operands are pointers. */
4071 if (TYPE_PTR_P (TREE_OPERAND (t, 0))
4072 && TYPE_PTR_P (TREE_OPERAND (t, 1)))
4074 if (flags & tf_error)
4075 error ("difference of two pointer expressions is not "
4076 "a constant expression");
4077 return false;
4079 want_rval = true;
4080 goto binary;
4082 case LT_EXPR:
4083 case LE_EXPR:
4084 case GT_EXPR:
4085 case GE_EXPR:
4086 case EQ_EXPR:
4087 case NE_EXPR:
4088 /* -- a relational or equality operator where at least
4089 one of the operands is a pointer. */
4090 if (TYPE_PTR_P (TREE_OPERAND (t, 0))
4091 || TYPE_PTR_P (TREE_OPERAND (t, 1)))
4093 if (flags & tf_error)
4094 error ("pointer comparison expression is not a "
4095 "constant expression");
4096 return false;
4098 want_rval = true;
4099 goto binary;
4101 case PREINCREMENT_EXPR:
4102 case POSTINCREMENT_EXPR:
4103 case PREDECREMENT_EXPR:
4104 case POSTDECREMENT_EXPR:
4105 if (cxx_dialect < cxx14)
4106 goto fail;
4107 goto unary;
4109 case BIT_NOT_EXPR:
4110 /* A destructor. */
4111 if (TYPE_P (TREE_OPERAND (t, 0)))
4112 return true;
4113 /* else fall through. */
4115 case REALPART_EXPR:
4116 case IMAGPART_EXPR:
4117 case CONJ_EXPR:
4118 case SAVE_EXPR:
4119 case FIX_TRUNC_EXPR:
4120 case FLOAT_EXPR:
4121 case NEGATE_EXPR:
4122 case ABS_EXPR:
4123 case TRUTH_NOT_EXPR:
4124 case FIXED_CONVERT_EXPR:
4125 case UNARY_PLUS_EXPR:
4126 unary:
4127 return RECUR (TREE_OPERAND (t, 0), rval);
4129 case CAST_EXPR:
4130 case CONST_CAST_EXPR:
4131 case STATIC_CAST_EXPR:
4132 case REINTERPRET_CAST_EXPR:
4133 case IMPLICIT_CONV_EXPR:
4134 if (cxx_dialect < cxx11
4135 && !dependent_type_p (TREE_TYPE (t))
4136 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t)))
4137 /* In C++98, a conversion to non-integral type can't be part of a
4138 constant expression. */
4140 if (flags & tf_error)
4141 error ("cast to non-integral type %qT in a constant expression",
4142 TREE_TYPE (t));
4143 return false;
4146 return (RECUR (TREE_OPERAND (t, 0),
4147 TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE));
4149 case BIND_EXPR:
4150 return RECUR (BIND_EXPR_BODY (t), want_rval);
4152 case WITH_CLEANUP_EXPR:
4153 case CLEANUP_POINT_EXPR:
4154 case MUST_NOT_THROW_EXPR:
4155 case TRY_CATCH_EXPR:
4156 case EH_SPEC_BLOCK:
4157 case EXPR_STMT:
4158 case PAREN_EXPR:
4159 case DECL_EXPR:
4160 case NON_DEPENDENT_EXPR:
4161 /* For convenience. */
4162 case RETURN_EXPR:
4163 return RECUR (TREE_OPERAND (t, 0), want_rval);
4165 case SCOPE_REF:
4166 return RECUR (TREE_OPERAND (t, 1), want_rval);
4168 case TARGET_EXPR:
4169 if (!literal_type_p (TREE_TYPE (t)))
4171 if (flags & tf_error)
4173 error ("temporary of non-literal type %qT in a "
4174 "constant expression", TREE_TYPE (t));
4175 explain_non_literal_class (TREE_TYPE (t));
4177 return false;
4179 case INIT_EXPR:
4180 return RECUR (TREE_OPERAND (t, 1), rval);
4182 case CONSTRUCTOR:
4184 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
4185 constructor_elt *ce;
4186 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
4187 if (!RECUR (ce->value, want_rval))
4188 return false;
4189 return true;
4192 case TREE_LIST:
4194 gcc_assert (TREE_PURPOSE (t) == NULL_TREE
4195 || DECL_P (TREE_PURPOSE (t)));
4196 if (!RECUR (TREE_VALUE (t), want_rval))
4197 return false;
4198 if (TREE_CHAIN (t) == NULL_TREE)
4199 return true;
4200 return RECUR (TREE_CHAIN (t), want_rval);
4203 case TRUNC_DIV_EXPR:
4204 case CEIL_DIV_EXPR:
4205 case FLOOR_DIV_EXPR:
4206 case ROUND_DIV_EXPR:
4207 case TRUNC_MOD_EXPR:
4208 case CEIL_MOD_EXPR:
4209 case ROUND_MOD_EXPR:
4211 tree denom = TREE_OPERAND (t, 1);
4212 if (!RECUR (denom, rval))
4213 return false;
4214 /* We can't call cxx_eval_outermost_constant_expr on an expression
4215 that hasn't been through instantiate_non_dependent_expr yet. */
4216 if (!processing_template_decl)
4217 denom = cxx_eval_outermost_constant_expr (denom, true);
4218 if (integer_zerop (denom))
4220 if (flags & tf_error)
4221 error ("division by zero is not a constant-expression");
4222 return false;
4224 else
4226 want_rval = true;
4227 return RECUR (TREE_OPERAND (t, 0), want_rval);
4231 case COMPOUND_EXPR:
4233 /* check_return_expr sometimes wraps a TARGET_EXPR in a
4234 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
4235 introduced by build_call_a. */
4236 tree op0 = TREE_OPERAND (t, 0);
4237 tree op1 = TREE_OPERAND (t, 1);
4238 STRIP_NOPS (op1);
4239 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
4240 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
4241 return RECUR (op0, want_rval);
4242 else
4243 goto binary;
4246 /* If the first operand is the non-short-circuit constant, look at
4247 the second operand; otherwise we only care about the first one for
4248 potentiality. */
4249 case TRUTH_AND_EXPR:
4250 case TRUTH_ANDIF_EXPR:
4251 tmp = boolean_true_node;
4252 goto truth;
4253 case TRUTH_OR_EXPR:
4254 case TRUTH_ORIF_EXPR:
4255 tmp = boolean_false_node;
4256 truth:
4258 tree op = TREE_OPERAND (t, 0);
4259 if (!RECUR (op, rval))
4260 return false;
4261 if (!processing_template_decl)
4262 op = cxx_eval_outermost_constant_expr (op, true);
4263 if (tree_int_cst_equal (op, tmp))
4264 return RECUR (TREE_OPERAND (t, 1), rval);
4265 else
4266 return true;
4269 case PLUS_EXPR:
4270 case MULT_EXPR:
4271 case POINTER_PLUS_EXPR:
4272 case RDIV_EXPR:
4273 case EXACT_DIV_EXPR:
4274 case MIN_EXPR:
4275 case MAX_EXPR:
4276 case LSHIFT_EXPR:
4277 case RSHIFT_EXPR:
4278 case LROTATE_EXPR:
4279 case RROTATE_EXPR:
4280 case BIT_IOR_EXPR:
4281 case BIT_XOR_EXPR:
4282 case BIT_AND_EXPR:
4283 case TRUTH_XOR_EXPR:
4284 case UNORDERED_EXPR:
4285 case ORDERED_EXPR:
4286 case UNLT_EXPR:
4287 case UNLE_EXPR:
4288 case UNGT_EXPR:
4289 case UNGE_EXPR:
4290 case UNEQ_EXPR:
4291 case LTGT_EXPR:
4292 case RANGE_EXPR:
4293 case COMPLEX_EXPR:
4294 want_rval = true;
4295 /* Fall through. */
4296 case ARRAY_REF:
4297 case ARRAY_RANGE_REF:
4298 case MEMBER_REF:
4299 case DOTSTAR_EXPR:
4300 binary:
4301 for (i = 0; i < 2; ++i)
4302 if (!RECUR (TREE_OPERAND (t, i), want_rval))
4303 return false;
4304 return true;
4306 case CILK_SYNC_STMT:
4307 case CILK_SPAWN_STMT:
4308 case ARRAY_NOTATION_REF:
4309 return false;
4311 case FMA_EXPR:
4312 case VEC_PERM_EXPR:
4313 for (i = 0; i < 3; ++i)
4314 if (!RECUR (TREE_OPERAND (t, i), true))
4315 return false;
4316 return true;
4318 case COND_EXPR:
4319 case VEC_COND_EXPR:
4320 /* If the condition is a known constant, we know which of the legs we
4321 care about; otherwise we only require that the condition and
4322 either of the legs be potentially constant. */
4323 tmp = TREE_OPERAND (t, 0);
4324 if (!RECUR (tmp, rval))
4325 return false;
4326 if (!processing_template_decl)
4327 tmp = cxx_eval_outermost_constant_expr (tmp, true);
4328 if (integer_zerop (tmp))
4329 return RECUR (TREE_OPERAND (t, 2), want_rval);
4330 else if (TREE_CODE (tmp) == INTEGER_CST)
4331 return RECUR (TREE_OPERAND (t, 1), want_rval);
4332 for (i = 1; i < 3; ++i)
4333 if (potential_constant_expression_1 (TREE_OPERAND (t, i),
4334 want_rval, strict, tf_none))
4335 return true;
4336 if (flags & tf_error)
4337 error ("expression %qE is not a constant-expression", t);
4338 return false;
4340 case VEC_INIT_EXPR:
4341 if (VEC_INIT_EXPR_IS_CONSTEXPR (t))
4342 return true;
4343 if (flags & tf_error)
4345 error ("non-constant array initialization");
4346 diagnose_non_constexpr_vec_init (t);
4348 return false;
4350 default:
4351 if (objc_is_property_ref (t))
4352 return false;
4354 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t)));
4355 gcc_unreachable();
4356 return false;
4358 #undef RECUR
4361 /* The main entry point to the above. */
4363 bool
4364 potential_constant_expression (tree t)
4366 return potential_constant_expression_1 (t, false, true, tf_none);
4369 bool
4370 potential_static_init_expression (tree t)
4372 return potential_constant_expression_1 (t, false, false, tf_none);
4375 /* As above, but require a constant rvalue. */
4377 bool
4378 potential_rvalue_constant_expression (tree t)
4380 return potential_constant_expression_1 (t, true, true, tf_none);
4383 /* Like above, but complain about non-constant expressions. */
4385 bool
4386 require_potential_constant_expression (tree t)
4388 return potential_constant_expression_1 (t, false, true, tf_warning_or_error);
4391 /* Cross product of the above. */
4393 bool
4394 require_potential_rvalue_constant_expression (tree t)
4396 return potential_constant_expression_1 (t, true, true, tf_warning_or_error);
4399 #include "gt-cp-constexpr.h"