Daily bump.
[official-gcc.git] / gcc / cp / constexpr.c
blob48bc8f19442d9839d85739155f4278a0932f3c86
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_builtin_call_array (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;
2586 gcc_assert (DECL_P (object));
2590 /* And then find/build up our initializer for the path to the subobject
2591 we're initializing. */
2592 tree *valp = ctx->values->get (object);
2593 if (!valp)
2595 /* A constant-expression cannot modify objects from outside the
2596 constant-expression. */
2597 if (!ctx->quiet)
2598 error ("modification of %qD is not a constant-expression", object);
2599 *non_constant_p = true;
2600 return t;
2602 tree type = TREE_TYPE (object);
2603 while (!refs->is_empty())
2605 if (*valp == NULL_TREE)
2607 *valp = build_constructor (type, NULL);
2608 CONSTRUCTOR_NO_IMPLICIT_ZERO (*valp) = true;
2611 constructor_elt ce;
2612 type = refs->pop();
2613 ce.index = refs->pop();
2614 ce.value = NULL_TREE;
2616 unsigned HOST_WIDE_INT idx = 0;
2617 constructor_elt *cep = NULL;
2618 for (idx = 0;
2619 vec_safe_iterate (CONSTRUCTOR_ELTS (*valp), idx, &cep);
2620 idx++)
2621 /* ??? slow */
2622 if (cp_tree_equal (ce.index, cep->index))
2623 break;
2624 if (!cep)
2625 cep = vec_safe_push (CONSTRUCTOR_ELTS (*valp), ce);
2626 valp = &cep->value;
2628 release_tree_vector (refs);
2630 if ((AGGREGATE_TYPE_P (TREE_TYPE (t)) || VECTOR_TYPE_P (TREE_TYPE (t))))
2632 /* Create a new CONSTRUCTOR in case evaluation of the initializer
2633 wants to modify it. */
2634 *valp = new_ctx.ctor = build_constructor (TREE_TYPE (t), NULL);
2635 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx.ctor) = true;
2636 new_ctx.object = target;
2639 tree init = cxx_eval_constant_expression (&new_ctx, TREE_OPERAND (t, 1),
2640 false,
2641 non_constant_p, overflow_p);
2642 if (target == object)
2643 /* The hash table might have moved since the get earlier. */
2644 ctx->values->put (object, init);
2645 else
2646 *valp = init;
2648 if (*non_constant_p)
2649 return t;
2650 else if (addr)
2651 return target;
2652 else
2653 return init;
2656 /* Evaluate a ++ or -- expression. */
2658 static tree
2659 cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t,
2660 bool addr,
2661 bool *non_constant_p, bool *overflow_p)
2663 enum tree_code code = TREE_CODE (t);
2664 tree type = TREE_TYPE (t);
2665 tree op = TREE_OPERAND (t, 0);
2666 tree offset = TREE_OPERAND (t, 1);
2667 gcc_assert (TREE_CONSTANT (offset));
2669 /* The operand as an lvalue. */
2670 op = cxx_eval_constant_expression (ctx, op, true,
2671 non_constant_p, overflow_p);
2673 /* The operand as an rvalue. */
2674 tree val = rvalue (op);
2675 val = cxx_eval_constant_expression (ctx, val, false,
2676 non_constant_p, overflow_p);
2677 VERIFY_CONSTANT (val);
2679 /* The modified value. */
2680 bool inc = (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR);
2681 tree mod;
2682 if (POINTER_TYPE_P (type))
2684 /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
2685 offset = convert_to_ptrofftype (offset);
2686 if (!inc)
2687 offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset);
2688 mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset);
2690 else
2691 mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset);
2692 VERIFY_CONSTANT (mod);
2694 /* Storing the modified value. */
2695 tree store = build2 (MODIFY_EXPR, type, op, mod);
2696 cxx_eval_constant_expression (ctx, store,
2697 true, non_constant_p, overflow_p);
2699 /* And the value of the expression. */
2700 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
2702 /* Prefix ops are lvalues. */
2703 if (addr)
2704 return op;
2705 else
2706 /* But we optimize when the caller wants an rvalue. */
2707 return mod;
2709 else
2710 /* Postfix ops are rvalues. */
2711 return val;
2714 /* Predicates for the meaning of *jump_target. */
2716 static bool
2717 returns (tree *jump_target)
2719 return *jump_target
2720 && TREE_CODE (*jump_target) == RETURN_EXPR;
2723 static bool
2724 breaks (tree *jump_target)
2726 return *jump_target
2727 && TREE_CODE (*jump_target) == LABEL_DECL
2728 && LABEL_DECL_BREAK (*jump_target);
2731 static bool
2732 continues (tree *jump_target)
2734 return *jump_target
2735 && TREE_CODE (*jump_target) == LABEL_DECL
2736 && LABEL_DECL_CONTINUE (*jump_target);
2739 static bool
2740 switches (tree *jump_target)
2742 return *jump_target
2743 && TREE_CODE (*jump_target) == INTEGER_CST;
2746 /* Subroutine of cxx_eval_statement_list. Determine whether the statement
2747 at I matches *jump_target. If we're looking for a case label and we see
2748 the default label, copy I into DEFAULT_LABEL. */
2750 static bool
2751 label_matches (tree *jump_target, tree_stmt_iterator i,
2752 tree_stmt_iterator& default_label)
2754 tree stmt = tsi_stmt (i);
2755 switch (TREE_CODE (*jump_target))
2757 case LABEL_DECL:
2758 if (TREE_CODE (stmt) == LABEL_EXPR
2759 && LABEL_EXPR_LABEL (stmt) == *jump_target)
2760 return true;
2761 break;
2763 case INTEGER_CST:
2764 if (TREE_CODE (stmt) == CASE_LABEL_EXPR)
2766 if (!CASE_LOW (stmt))
2767 default_label = i;
2768 else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt)))
2769 return true;
2771 break;
2773 default:
2774 gcc_unreachable ();
2776 return false;
2779 /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
2780 semantics, for switch, break, continue, and return. */
2782 static tree
2783 cxx_eval_statement_list (const constexpr_ctx *ctx, tree t,
2784 bool *non_constant_p, bool *overflow_p,
2785 tree *jump_target)
2787 tree_stmt_iterator i;
2788 tree_stmt_iterator default_label = tree_stmt_iterator();
2789 tree local_target;
2790 /* In a statement-expression we want to return the last value. */
2791 tree r = NULL_TREE;
2792 if (!jump_target)
2794 local_target = NULL_TREE;
2795 jump_target = &local_target;
2797 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
2799 reenter:
2800 tree stmt = tsi_stmt (i);
2801 if (*jump_target)
2803 if (TREE_CODE (stmt) == STATEMENT_LIST)
2804 /* The label we want might be inside. */;
2805 else if (label_matches (jump_target, i, default_label))
2806 /* Found it. */
2807 *jump_target = NULL_TREE;
2808 else
2809 continue;
2811 r = cxx_eval_constant_expression (ctx, stmt, false,
2812 non_constant_p, overflow_p,
2813 jump_target);
2814 if (*non_constant_p)
2815 break;
2816 if (returns (jump_target) || breaks (jump_target))
2817 break;
2819 if (switches (jump_target) && !tsi_end_p (default_label))
2821 i = default_label;
2822 *jump_target = NULL_TREE;
2823 goto reenter;
2825 return r;
2828 /* Evaluate a LOOP_EXPR for side-effects. Handles break and return
2829 semantics; continue semantics are covered by cxx_eval_statement_list. */
2831 static tree
2832 cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t,
2833 bool *non_constant_p, bool *overflow_p,
2834 tree *jump_target)
2836 tree body = TREE_OPERAND (t, 0);
2837 while (true)
2839 cxx_eval_statement_list (ctx, body,
2840 non_constant_p, overflow_p, jump_target);
2841 if (returns (jump_target) || breaks (jump_target))
2842 break;
2844 if (breaks (jump_target))
2845 *jump_target = NULL_TREE;
2846 return NULL_TREE;
2849 /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
2850 semantics. */
2852 static tree
2853 cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t,
2854 bool *non_constant_p, bool *overflow_p,
2855 tree *jump_target)
2857 tree cond = TREE_OPERAND (t, 0);
2858 cond = cxx_eval_constant_expression (ctx, cond, false,
2859 non_constant_p, overflow_p);
2860 VERIFY_CONSTANT (cond);
2861 *jump_target = cond;
2863 tree body = TREE_OPERAND (t, 1);
2864 cxx_eval_statement_list (ctx, body,
2865 non_constant_p, overflow_p, jump_target);
2866 if (breaks (jump_target) || switches (jump_target))
2867 *jump_target = NULL_TREE;
2868 return NULL_TREE;
2871 /* Attempt to reduce the expression T to a constant value.
2872 On failure, issue diagnostic and return error_mark_node. */
2873 /* FIXME unify with c_fully_fold */
2874 /* FIXME overflow_p is too global */
2876 static tree
2877 cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
2878 bool addr,
2879 bool *non_constant_p, bool *overflow_p,
2880 tree *jump_target)
2882 constexpr_ctx new_ctx;
2883 tree r = t;
2885 if (t == error_mark_node)
2887 *non_constant_p = true;
2888 return t;
2890 if (CONSTANT_CLASS_P (t))
2892 if (TREE_CODE (t) == PTRMEM_CST)
2893 t = cplus_expand_constant (t);
2894 else if (TREE_OVERFLOW (t) && (!flag_permissive || ctx->quiet))
2895 *overflow_p = true;
2896 return t;
2898 if (TREE_CODE (t) != NOP_EXPR
2899 && reduced_constant_expression_p (t))
2900 return fold (t);
2902 switch (TREE_CODE (t))
2904 case RESULT_DECL:
2905 if (addr)
2906 return t;
2907 /* We ask for an rvalue for the RESULT_DECL when indirecting
2908 through an invisible reference. */
2909 gcc_assert (DECL_BY_REFERENCE (t));
2910 return (*ctx->values->get (t));
2912 case VAR_DECL:
2913 if (addr)
2914 return t;
2915 /* else fall through. */
2916 case CONST_DECL:
2917 if (ctx->strict)
2918 r = decl_really_constant_value (t);
2919 else
2920 r = decl_constant_value (t);
2921 if (TREE_CODE (r) == TARGET_EXPR
2922 && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR)
2923 r = TARGET_EXPR_INITIAL (r);
2924 if (TREE_CODE (r) == VAR_DECL)
2925 if (tree *p = ctx->values->get (r))
2926 r = *p;
2927 if (DECL_P (r))
2929 if (!ctx->quiet)
2930 non_const_var_error (r);
2931 *non_constant_p = true;
2933 break;
2935 case FUNCTION_DECL:
2936 case TEMPLATE_DECL:
2937 case LABEL_DECL:
2938 case LABEL_EXPR:
2939 case CASE_LABEL_EXPR:
2940 return t;
2942 case PARM_DECL:
2943 if (!use_new_call && ctx
2944 && ctx->call && DECL_CONTEXT (t) == ctx->call->fundef->decl)
2945 r = lookup_parameter_binding (ctx->call, t);
2946 else if (addr && TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
2947 /* glvalue use. */;
2948 else if (tree *p = ctx->values->get (r))
2949 r = *p;
2950 else if (addr)
2951 /* Defer in case this is only used for its type. */;
2952 else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
2953 /* Defer, there's no lvalue->rvalue conversion. */;
2954 else if (is_empty_class (TREE_TYPE (t)))
2956 /* If the class is empty, we aren't actually loading anything. */
2957 r = build_constructor (TREE_TYPE (t), NULL);
2958 TREE_CONSTANT (r) = true;
2960 else
2962 if (!ctx->quiet)
2963 error ("%qE is not a constant expression", t);
2964 *non_constant_p = true;
2966 break;
2968 case CALL_EXPR:
2969 case AGGR_INIT_EXPR:
2970 r = cxx_eval_call_expression (ctx, t, addr,
2971 non_constant_p, overflow_p);
2972 break;
2974 case DECL_EXPR:
2976 r = DECL_EXPR_DECL (t);
2977 if (AGGREGATE_TYPE_P (TREE_TYPE (r))
2978 || VECTOR_TYPE_P (TREE_TYPE (r)))
2980 new_ctx = *ctx;
2981 new_ctx.object = r;
2982 new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL);
2983 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx.ctor) = true;
2984 new_ctx.values->put (r, new_ctx.ctor);
2985 ctx = &new_ctx;
2988 if (tree init = DECL_INITIAL (r))
2990 init = cxx_eval_constant_expression (ctx, init,
2991 false,
2992 non_constant_p, overflow_p);
2993 ctx->values->put (r, init);
2995 else if (ctx == &new_ctx)
2996 /* We gave it a CONSTRUCTOR above. */;
2997 else
2998 ctx->values->put (r, NULL_TREE);
3000 break;
3002 case TARGET_EXPR:
3003 if (!literal_type_p (TREE_TYPE (t)))
3005 if (!ctx->quiet)
3007 error ("temporary of non-literal type %qT in a "
3008 "constant expression", TREE_TYPE (t));
3009 explain_non_literal_class (TREE_TYPE (t));
3011 *non_constant_p = true;
3012 break;
3014 if ((AGGREGATE_TYPE_P (TREE_TYPE (t)) || VECTOR_TYPE_P (TREE_TYPE (t))))
3016 /* We're being expanded without an explicit target, so start
3017 initializing a new object; expansion with an explicit target
3018 strips the TARGET_EXPR before we get here. */
3019 new_ctx = *ctx;
3020 new_ctx.ctor = build_constructor (TREE_TYPE (t), NULL);
3021 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx.ctor) = true;
3022 new_ctx.object = TARGET_EXPR_SLOT (t);
3023 ctx->values->put (new_ctx.object, new_ctx.ctor);
3024 ctx = &new_ctx;
3026 /* Pass false for 'addr' because this indicates
3027 initialization of a temporary. */
3028 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
3029 false,
3030 non_constant_p, overflow_p);
3031 if (!*non_constant_p)
3032 /* Adjust the type of the result to the type of the temporary. */
3033 r = adjust_temp_type (TREE_TYPE (t), r);
3034 if (addr)
3036 tree slot = TARGET_EXPR_SLOT (t);
3037 ctx->values->put (slot, r);
3038 return slot;
3040 break;
3042 case INIT_EXPR:
3043 if (!use_new_call)
3045 /* In C++11 constexpr evaluation we are looking for the value,
3046 not the side-effect of the initialization. */
3047 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
3048 false,
3049 non_constant_p, overflow_p);
3050 break;
3052 /* else fall through */
3053 case MODIFY_EXPR:
3054 r = cxx_eval_store_expression (ctx, t, addr,
3055 non_constant_p, overflow_p);
3056 break;
3058 case SCOPE_REF:
3059 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
3060 addr,
3061 non_constant_p, overflow_p);
3062 break;
3064 case RETURN_EXPR:
3065 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
3066 addr,
3067 non_constant_p, overflow_p);
3068 *jump_target = t;
3069 break;
3071 case SAVE_EXPR:
3072 /* Avoid evaluating a SAVE_EXPR more than once. */
3073 if (tree *p = ctx->values->get (t))
3074 r = *p;
3075 else
3077 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), addr,
3078 non_constant_p, overflow_p);
3079 ctx->values->put (t, r);
3081 break;
3083 case NON_LVALUE_EXPR:
3084 case TRY_CATCH_EXPR:
3085 case CLEANUP_POINT_EXPR:
3086 case MUST_NOT_THROW_EXPR:
3087 case EXPR_STMT:
3088 case EH_SPEC_BLOCK:
3089 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
3090 addr,
3091 non_constant_p, overflow_p,
3092 jump_target);
3093 break;
3095 /* These differ from cxx_eval_unary_expression in that this doesn't
3096 check for a constant operand or result; an address can be
3097 constant without its operand being, and vice versa. */
3098 case INDIRECT_REF:
3099 r = cxx_eval_indirect_ref (ctx, t, addr,
3100 non_constant_p, overflow_p);
3101 break;
3103 case ADDR_EXPR:
3105 tree oldop = TREE_OPERAND (t, 0);
3106 tree op = cxx_eval_constant_expression (ctx, oldop,
3107 /*addr*/true,
3108 non_constant_p, overflow_p);
3109 /* Don't VERIFY_CONSTANT here. */
3110 if (*non_constant_p)
3111 return t;
3112 gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR);
3113 /* This function does more aggressive folding than fold itself. */
3114 r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
3115 if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
3116 return t;
3117 break;
3120 case REALPART_EXPR:
3121 case IMAGPART_EXPR:
3122 case CONJ_EXPR:
3123 case FIX_TRUNC_EXPR:
3124 case FLOAT_EXPR:
3125 case NEGATE_EXPR:
3126 case ABS_EXPR:
3127 case BIT_NOT_EXPR:
3128 case TRUTH_NOT_EXPR:
3129 case FIXED_CONVERT_EXPR:
3130 r = cxx_eval_unary_expression (ctx, t, addr,
3131 non_constant_p, overflow_p);
3132 break;
3134 case SIZEOF_EXPR:
3135 if (SIZEOF_EXPR_TYPE_P (t))
3136 r = cxx_sizeof_or_alignof_type (TREE_TYPE (TREE_OPERAND (t, 0)),
3137 SIZEOF_EXPR, false);
3138 else if (TYPE_P (TREE_OPERAND (t, 0)))
3139 r = cxx_sizeof_or_alignof_type (TREE_OPERAND (t, 0), SIZEOF_EXPR,
3140 false);
3141 else
3142 r = cxx_sizeof_or_alignof_expr (TREE_OPERAND (t, 0), SIZEOF_EXPR,
3143 false);
3144 if (r == error_mark_node)
3145 r = size_one_node;
3146 VERIFY_CONSTANT (r);
3147 break;
3149 case COMPOUND_EXPR:
3151 /* check_return_expr sometimes wraps a TARGET_EXPR in a
3152 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
3153 introduced by build_call_a. */
3154 tree op0 = TREE_OPERAND (t, 0);
3155 tree op1 = TREE_OPERAND (t, 1);
3156 STRIP_NOPS (op1);
3157 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
3158 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
3159 r = cxx_eval_constant_expression (ctx, op0,
3160 addr, non_constant_p, overflow_p,
3161 jump_target);
3162 else
3164 /* Check that the LHS is constant and then discard it. */
3165 cxx_eval_constant_expression (ctx, op0,
3166 false, non_constant_p, overflow_p,
3167 jump_target);
3168 op1 = TREE_OPERAND (t, 1);
3169 r = cxx_eval_constant_expression (ctx, op1,
3170 addr, non_constant_p, overflow_p,
3171 jump_target);
3174 break;
3176 case POINTER_PLUS_EXPR:
3177 case PLUS_EXPR:
3178 case MINUS_EXPR:
3179 case MULT_EXPR:
3180 case TRUNC_DIV_EXPR:
3181 case CEIL_DIV_EXPR:
3182 case FLOOR_DIV_EXPR:
3183 case ROUND_DIV_EXPR:
3184 case TRUNC_MOD_EXPR:
3185 case CEIL_MOD_EXPR:
3186 case ROUND_MOD_EXPR:
3187 case RDIV_EXPR:
3188 case EXACT_DIV_EXPR:
3189 case MIN_EXPR:
3190 case MAX_EXPR:
3191 case LSHIFT_EXPR:
3192 case RSHIFT_EXPR:
3193 case LROTATE_EXPR:
3194 case RROTATE_EXPR:
3195 case BIT_IOR_EXPR:
3196 case BIT_XOR_EXPR:
3197 case BIT_AND_EXPR:
3198 case TRUTH_XOR_EXPR:
3199 case LT_EXPR:
3200 case LE_EXPR:
3201 case GT_EXPR:
3202 case GE_EXPR:
3203 case EQ_EXPR:
3204 case NE_EXPR:
3205 case UNORDERED_EXPR:
3206 case ORDERED_EXPR:
3207 case UNLT_EXPR:
3208 case UNLE_EXPR:
3209 case UNGT_EXPR:
3210 case UNGE_EXPR:
3211 case UNEQ_EXPR:
3212 case LTGT_EXPR:
3213 case RANGE_EXPR:
3214 case COMPLEX_EXPR:
3215 r = cxx_eval_binary_expression (ctx, t, addr,
3216 non_constant_p, overflow_p);
3217 break;
3219 /* fold can introduce non-IF versions of these; still treat them as
3220 short-circuiting. */
3221 case TRUTH_AND_EXPR:
3222 case TRUTH_ANDIF_EXPR:
3223 r = cxx_eval_logical_expression (ctx, t, boolean_false_node,
3224 boolean_true_node,
3225 addr,
3226 non_constant_p, overflow_p);
3227 break;
3229 case TRUTH_OR_EXPR:
3230 case TRUTH_ORIF_EXPR:
3231 r = cxx_eval_logical_expression (ctx, t, boolean_true_node,
3232 boolean_false_node,
3233 addr,
3234 non_constant_p, overflow_p);
3235 break;
3237 case ARRAY_REF:
3238 r = cxx_eval_array_reference (ctx, t, addr,
3239 non_constant_p, overflow_p);
3240 break;
3242 case COMPONENT_REF:
3243 if (is_overloaded_fn (t))
3245 /* We can only get here in checking mode via
3246 build_non_dependent_expr, because any expression that
3247 calls or takes the address of the function will have
3248 pulled a FUNCTION_DECL out of the COMPONENT_REF. */
3249 gcc_checking_assert (ctx->quiet || errorcount);
3250 *non_constant_p = true;
3251 return t;
3253 r = cxx_eval_component_reference (ctx, t, addr,
3254 non_constant_p, overflow_p);
3255 break;
3257 case BIT_FIELD_REF:
3258 r = cxx_eval_bit_field_ref (ctx, t, addr,
3259 non_constant_p, overflow_p);
3260 break;
3262 case COND_EXPR:
3263 case VEC_COND_EXPR:
3264 r = cxx_eval_conditional_expression (ctx, t, addr,
3265 non_constant_p, overflow_p,
3266 jump_target);
3267 break;
3269 case CONSTRUCTOR:
3270 r = cxx_eval_bare_aggregate (ctx, t, addr,
3271 non_constant_p, overflow_p);
3272 break;
3274 case VEC_INIT_EXPR:
3275 /* We can get this in a defaulted constructor for a class with a
3276 non-static data member of array type. Either the initializer will
3277 be NULL, meaning default-initialization, or it will be an lvalue
3278 or xvalue of the same type, meaning direct-initialization from the
3279 corresponding member. */
3280 r = cxx_eval_vec_init (ctx, t, addr,
3281 non_constant_p, overflow_p);
3282 break;
3284 case FMA_EXPR:
3285 case VEC_PERM_EXPR:
3286 r = cxx_eval_trinary_expression (ctx, t, addr,
3287 non_constant_p, overflow_p);
3288 break;
3290 case CONVERT_EXPR:
3291 case VIEW_CONVERT_EXPR:
3292 case NOP_EXPR:
3294 tree oldop = TREE_OPERAND (t, 0);
3295 tree op = cxx_eval_constant_expression (ctx, oldop,
3296 addr,
3297 non_constant_p, overflow_p);
3298 if (*non_constant_p)
3299 return t;
3300 if (POINTER_TYPE_P (TREE_TYPE (t))
3301 && TREE_CODE (op) == INTEGER_CST
3302 && !integer_zerop (op))
3304 if (!ctx->quiet)
3305 error_at (EXPR_LOC_OR_LOC (t, input_location),
3306 "reinterpret_cast from integer to pointer");
3307 *non_constant_p = true;
3308 return t;
3310 if (op == oldop)
3311 /* We didn't fold at the top so we could check for ptr-int
3312 conversion. */
3313 return fold (t);
3314 r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), op);
3315 /* Conversion of an out-of-range value has implementation-defined
3316 behavior; the language considers it different from arithmetic
3317 overflow, which is undefined. */
3318 if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op))
3319 TREE_OVERFLOW (r) = false;
3321 break;
3323 case EMPTY_CLASS_EXPR:
3324 /* This is good enough for a function argument that might not get
3325 used, and they can't do anything with it, so just return it. */
3326 return t;
3328 case STATEMENT_LIST:
3329 new_ctx = *ctx;
3330 new_ctx.ctor = new_ctx.object = NULL_TREE;
3331 return cxx_eval_statement_list (&new_ctx, t,
3332 non_constant_p, overflow_p, jump_target);
3334 case BIND_EXPR:
3335 return cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t),
3336 addr,
3337 non_constant_p, overflow_p,
3338 jump_target);
3340 case PREINCREMENT_EXPR:
3341 case POSTINCREMENT_EXPR:
3342 case PREDECREMENT_EXPR:
3343 case POSTDECREMENT_EXPR:
3344 return cxx_eval_increment_expression (ctx, t,
3345 addr, non_constant_p, overflow_p);
3347 case LAMBDA_EXPR:
3348 case NEW_EXPR:
3349 case VEC_NEW_EXPR:
3350 case DELETE_EXPR:
3351 case VEC_DELETE_EXPR:
3352 case THROW_EXPR:
3353 case MODOP_EXPR:
3354 /* GCC internal stuff. */
3355 case VA_ARG_EXPR:
3356 case OBJ_TYPE_REF:
3357 case WITH_CLEANUP_EXPR:
3358 case NON_DEPENDENT_EXPR:
3359 case BASELINK:
3360 case OFFSET_REF:
3361 if (!ctx->quiet)
3362 error_at (EXPR_LOC_OR_LOC (t, input_location),
3363 "expression %qE is not a constant-expression", t);
3364 *non_constant_p = true;
3365 break;
3367 case PLACEHOLDER_EXPR:
3368 if (!ctx || !ctx->ctor || (addr && !ctx->object))
3370 /* A placeholder without a referent. We can get here when
3371 checking whether NSDMIs are noexcept, or in massage_init_elt;
3372 just say it's non-constant for now. */
3373 gcc_assert (ctx->quiet);
3374 *non_constant_p = true;
3375 break;
3377 else
3379 /* Use of the value or address of the current object. We could
3380 use ctx->object unconditionally, but using ctx->ctor when we
3381 can is a minor optimization. */
3382 tree ctor = addr ? ctx->object : ctx->ctor;
3383 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3384 (TREE_TYPE (t), TREE_TYPE (ctor)));
3385 return cxx_eval_constant_expression
3386 (ctx, ctor, addr,
3387 non_constant_p, overflow_p);
3389 break;
3391 case GOTO_EXPR:
3392 *jump_target = TREE_OPERAND (t, 0);
3393 gcc_assert (breaks (jump_target) || continues (jump_target));
3394 break;
3396 case LOOP_EXPR:
3397 cxx_eval_loop_expr (ctx, t,
3398 non_constant_p, overflow_p, jump_target);
3399 break;
3401 case SWITCH_EXPR:
3402 cxx_eval_switch_expr (ctx, t,
3403 non_constant_p, overflow_p, jump_target);
3404 break;
3406 default:
3407 internal_error ("unexpected expression %qE of kind %s", t,
3408 get_tree_code_name (TREE_CODE (t)));
3409 *non_constant_p = true;
3410 break;
3413 if (r == error_mark_node)
3414 *non_constant_p = true;
3416 if (*non_constant_p)
3417 return t;
3418 else
3419 return r;
3422 static tree
3423 cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant,
3424 bool strict = true, tree object = NULL_TREE)
3426 bool non_constant_p = false;
3427 bool overflow_p = false;
3428 constexpr_ctx ctx = { NULL, NULL, NULL, NULL, allow_non_constant, strict };
3429 hash_map<tree,tree> map;
3430 ctx.values = &map;
3431 tree type = initialized_type (t);
3432 tree r = t;
3433 if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
3435 /* In C++14 an NSDMI can participate in aggregate initialization,
3436 and can refer to the address of the object being initialized, so
3437 we need to pass in the relevant VAR_DECL if we want to do the
3438 evaluation in a single pass. The evaluation will dynamically
3439 update ctx.values for the VAR_DECL. We use the same strategy
3440 for C++11 constexpr constructors that refer to the object being
3441 initialized. */
3442 ctx.ctor = build_constructor (type, NULL);
3443 CONSTRUCTOR_NO_IMPLICIT_ZERO (ctx.ctor) = true;
3444 if (!object)
3446 if (TREE_CODE (t) == TARGET_EXPR)
3447 object = TARGET_EXPR_SLOT (t);
3448 else if (TREE_CODE (t) == AGGR_INIT_EXPR)
3449 object = AGGR_INIT_EXPR_SLOT (t);
3451 ctx.object = object;
3452 if (object)
3453 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3454 (type, TREE_TYPE (object)));
3455 if (object && DECL_P (object))
3456 map.put (object, ctx.ctor);
3457 if (TREE_CODE (r) == TARGET_EXPR)
3458 /* Avoid creating another CONSTRUCTOR when we expand the
3459 TARGET_EXPR. */
3460 r = TARGET_EXPR_INITIAL (r);
3463 r = cxx_eval_constant_expression (&ctx, r,
3464 false, &non_constant_p, &overflow_p);
3466 verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p);
3468 /* Mutable logic is a bit tricky: we want to allow initialization of
3469 constexpr variables with mutable members, but we can't copy those
3470 members to another constexpr variable. */
3471 if (TREE_CODE (r) == CONSTRUCTOR
3472 && CONSTRUCTOR_MUTABLE_POISON (r))
3474 if (!allow_non_constant)
3475 error ("%qE is not a constant expression because it refers to "
3476 "mutable subobjects of %qT", t, type);
3477 non_constant_p = true;
3480 /* Technically we should check this for all subexpressions, but that
3481 runs into problems with our internal representation of pointer
3482 subtraction and the 5.19 rules are still in flux. */
3483 if (CONVERT_EXPR_CODE_P (TREE_CODE (r))
3484 && ARITHMETIC_TYPE_P (TREE_TYPE (r))
3485 && TREE_CODE (TREE_OPERAND (r, 0)) == ADDR_EXPR)
3487 if (!allow_non_constant)
3488 error ("conversion from pointer type %qT "
3489 "to arithmetic type %qT in a constant-expression",
3490 TREE_TYPE (TREE_OPERAND (r, 0)), TREE_TYPE (r));
3491 non_constant_p = true;
3494 if (!non_constant_p && overflow_p)
3495 non_constant_p = true;
3497 if (non_constant_p && !allow_non_constant)
3498 return error_mark_node;
3499 else if (non_constant_p && TREE_CONSTANT (r))
3501 /* This isn't actually constant, so unset TREE_CONSTANT. */
3502 if (EXPR_P (r))
3503 r = copy_node (r);
3504 else if (TREE_CODE (r) == CONSTRUCTOR)
3505 r = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (r), r);
3506 else
3507 r = build_nop (TREE_TYPE (r), r);
3508 TREE_CONSTANT (r) = false;
3510 else if (non_constant_p || r == t)
3511 return t;
3513 if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
3515 if (TREE_CODE (t) == TARGET_EXPR
3516 && TARGET_EXPR_INITIAL (t) == r)
3517 return t;
3518 else
3520 r = get_target_expr (r);
3521 TREE_CONSTANT (r) = true;
3522 return r;
3525 else
3526 return r;
3529 /* Returns true if T is a valid subexpression of a constant expression,
3530 even if it isn't itself a constant expression. */
3532 bool
3533 is_sub_constant_expr (tree t)
3535 bool non_constant_p = false;
3536 bool overflow_p = false;
3537 constexpr_ctx ctx = { NULL, NULL, NULL, NULL, true, true };
3538 hash_map <tree, tree> map;
3539 ctx.values = &map;
3540 cxx_eval_constant_expression (&ctx, t, false, &non_constant_p,
3541 &overflow_p);
3542 return !non_constant_p && !overflow_p;
3545 /* If T represents a constant expression returns its reduced value.
3546 Otherwise return error_mark_node. If T is dependent, then
3547 return NULL. */
3549 tree
3550 cxx_constant_value (tree t, tree decl)
3552 return cxx_eval_outermost_constant_expr (t, false, true, decl);
3555 /* If T is a constant expression, returns its reduced value.
3556 Otherwise, if T does not have TREE_CONSTANT set, returns T.
3557 Otherwise, returns a version of T without TREE_CONSTANT. */
3559 tree
3560 maybe_constant_value (tree t, tree decl)
3562 tree r;
3564 if (instantiation_dependent_expression_p (t)
3565 || type_unknown_p (t)
3566 || BRACE_ENCLOSED_INITIALIZER_P (t)
3567 || !potential_constant_expression (t))
3569 if (TREE_OVERFLOW_P (t))
3571 t = build_nop (TREE_TYPE (t), t);
3572 TREE_CONSTANT (t) = false;
3574 return t;
3577 r = cxx_eval_outermost_constant_expr (t, true, true, decl);
3578 #ifdef ENABLE_CHECKING
3579 /* cp_tree_equal looks through NOPs, so allow them. */
3580 gcc_assert (r == t
3581 || CONVERT_EXPR_P (t)
3582 || TREE_CODE (t) == VIEW_CONVERT_EXPR
3583 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
3584 || !cp_tree_equal (r, t));
3585 #endif
3586 return r;
3589 /* Like maybe_constant_value but first fully instantiate the argument.
3591 Note: this is equivalent to instantiate_non_dependent_expr_sfinae
3592 (t, tf_none) followed by maybe_constant_value but is more efficient,
3593 because calls instantiation_dependent_expression_p and
3594 potential_constant_expression at most once. */
3596 tree
3597 fold_non_dependent_expr (tree t)
3599 if (t == NULL_TREE)
3600 return NULL_TREE;
3602 /* If we're in a template, but T isn't value dependent, simplify
3603 it. We're supposed to treat:
3605 template <typename T> void f(T[1 + 1]);
3606 template <typename T> void f(T[2]);
3608 as two declarations of the same function, for example. */
3609 if (processing_template_decl)
3611 if (!instantiation_dependent_expression_p (t)
3612 && potential_constant_expression (t))
3614 processing_template_decl_sentinel s;
3615 t = instantiate_non_dependent_expr_internal (t, tf_none);
3617 if (type_unknown_p (t)
3618 || BRACE_ENCLOSED_INITIALIZER_P (t))
3620 if (TREE_OVERFLOW_P (t))
3622 t = build_nop (TREE_TYPE (t), t);
3623 TREE_CONSTANT (t) = false;
3625 return t;
3628 tree r = cxx_eval_outermost_constant_expr (t, true, true, NULL_TREE);
3629 #ifdef ENABLE_CHECKING
3630 /* cp_tree_equal looks through NOPs, so allow them. */
3631 gcc_assert (r == t
3632 || CONVERT_EXPR_P (t)
3633 || TREE_CODE (t) == VIEW_CONVERT_EXPR
3634 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
3635 || !cp_tree_equal (r, t));
3636 #endif
3637 return r;
3639 else if (TREE_OVERFLOW_P (t))
3641 t = build_nop (TREE_TYPE (t), t);
3642 TREE_CONSTANT (t) = false;
3644 return t;
3647 return maybe_constant_value (t);
3650 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
3651 than wrapped in a TARGET_EXPR. */
3653 tree
3654 maybe_constant_init (tree t, tree decl)
3656 if (TREE_CODE (t) == EXPR_STMT)
3657 t = TREE_OPERAND (t, 0);
3658 if (TREE_CODE (t) == CONVERT_EXPR
3659 && VOID_TYPE_P (TREE_TYPE (t)))
3660 t = TREE_OPERAND (t, 0);
3661 if (TREE_CODE (t) == INIT_EXPR)
3662 t = TREE_OPERAND (t, 1);
3663 if (instantiation_dependent_expression_p (t)
3664 || type_unknown_p (t)
3665 || BRACE_ENCLOSED_INITIALIZER_P (t)
3666 || !potential_static_init_expression (t))
3667 /* Don't try to evaluate it. */;
3668 else
3669 t = cxx_eval_outermost_constant_expr (t, true, false, decl);
3670 if (TREE_CODE (t) == TARGET_EXPR)
3672 tree init = TARGET_EXPR_INITIAL (t);
3673 if (TREE_CODE (init) == CONSTRUCTOR)
3674 t = init;
3676 return t;
3679 #if 0
3680 /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
3681 /* Return true if the object referred to by REF has automatic or thread
3682 local storage. */
3684 enum { ck_ok, ck_bad, ck_unknown };
3685 static int
3686 check_automatic_or_tls (tree ref)
3688 machine_mode mode;
3689 HOST_WIDE_INT bitsize, bitpos;
3690 tree offset;
3691 int volatilep = 0, unsignedp = 0;
3692 tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset,
3693 &mode, &unsignedp, &volatilep, false);
3694 duration_kind dk;
3696 /* If there isn't a decl in the middle, we don't know the linkage here,
3697 and this isn't a constant expression anyway. */
3698 if (!DECL_P (decl))
3699 return ck_unknown;
3700 dk = decl_storage_duration (decl);
3701 return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok;
3703 #endif
3705 /* Return true if T denotes a potentially constant expression. Issue
3706 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
3707 an lvalue-rvalue conversion is implied.
3709 C++0x [expr.const] used to say
3711 6 An expression is a potential constant expression if it is
3712 a constant expression where all occurrences of function
3713 parameters are replaced by arbitrary constant expressions
3714 of the appropriate type.
3716 2 A conditional expression is a constant expression unless it
3717 involves one of the following as a potentially evaluated
3718 subexpression (3.2), but subexpressions of logical AND (5.14),
3719 logical OR (5.15), and conditional (5.16) operations that are
3720 not evaluated are not considered. */
3722 static bool
3723 potential_constant_expression_1 (tree t, bool want_rval, bool strict,
3724 tsubst_flags_t flags)
3726 #define RECUR(T,RV) potential_constant_expression_1 ((T), (RV), strict, flags)
3727 enum { any = false, rval = true };
3728 int i;
3729 tree tmp;
3731 if (t == error_mark_node)
3732 return false;
3733 if (t == NULL_TREE)
3734 return true;
3735 if (TREE_THIS_VOLATILE (t))
3737 if (flags & tf_error)
3738 error ("expression %qE has side-effects", t);
3739 return false;
3741 if (CONSTANT_CLASS_P (t))
3742 return true;
3744 switch (TREE_CODE (t))
3746 case FUNCTION_DECL:
3747 case BASELINK:
3748 case TEMPLATE_DECL:
3749 case OVERLOAD:
3750 case TEMPLATE_ID_EXPR:
3751 case LABEL_DECL:
3752 case LABEL_EXPR:
3753 case CASE_LABEL_EXPR:
3754 case CONST_DECL:
3755 case SIZEOF_EXPR:
3756 case ALIGNOF_EXPR:
3757 case OFFSETOF_EXPR:
3758 case NOEXCEPT_EXPR:
3759 case TEMPLATE_PARM_INDEX:
3760 case TRAIT_EXPR:
3761 case IDENTIFIER_NODE:
3762 case USERDEF_LITERAL:
3763 /* We can see a FIELD_DECL in a pointer-to-member expression. */
3764 case FIELD_DECL:
3765 case PARM_DECL:
3766 case USING_DECL:
3767 case USING_STMT:
3768 case PLACEHOLDER_EXPR:
3769 case BREAK_STMT:
3770 case CONTINUE_STMT:
3771 return true;
3773 case AGGR_INIT_EXPR:
3774 case CALL_EXPR:
3775 /* -- an invocation of a function other than a constexpr function
3776 or a constexpr constructor. */
3778 tree fun = get_function_named_in_call (t);
3779 const int nargs = call_expr_nargs (t);
3780 i = 0;
3782 if (fun == NULL_TREE)
3784 /* fold_call_expr can't do anything with IFN calls. */
3785 if (flags & tf_error)
3786 error_at (EXPR_LOC_OR_LOC (t, input_location),
3787 "call to internal function");
3788 return false;
3790 if (is_overloaded_fn (fun))
3792 if (TREE_CODE (fun) == FUNCTION_DECL)
3794 if (builtin_valid_in_constant_expr_p (fun))
3795 return true;
3796 if (!DECL_DECLARED_CONSTEXPR_P (fun)
3797 /* Allow any built-in function; if the expansion
3798 isn't constant, we'll deal with that then. */
3799 && !is_builtin_fn (fun))
3801 if (flags & tf_error)
3803 error_at (EXPR_LOC_OR_LOC (t, input_location),
3804 "call to non-constexpr function %qD", fun);
3805 explain_invalid_constexpr_fn (fun);
3807 return false;
3809 /* A call to a non-static member function takes the address
3810 of the object as the first argument. But in a constant
3811 expression the address will be folded away, so look
3812 through it now. */
3813 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
3814 && !DECL_CONSTRUCTOR_P (fun))
3816 tree x = get_nth_callarg (t, 0);
3817 if (is_this_parameter (x))
3818 return true;
3819 else if (!RECUR (x, rval))
3820 return false;
3821 i = 1;
3824 else
3826 if (!RECUR (fun, true))
3827 return false;
3828 fun = get_first_fn (fun);
3830 /* Skip initial arguments to base constructors. */
3831 if (DECL_BASE_CONSTRUCTOR_P (fun))
3832 i = num_artificial_parms_for (fun);
3833 fun = DECL_ORIGIN (fun);
3835 else
3837 if (RECUR (fun, rval))
3838 /* Might end up being a constant function pointer. */;
3839 else
3840 return false;
3842 for (; i < nargs; ++i)
3844 tree x = get_nth_callarg (t, i);
3845 if (!RECUR (x, rval))
3846 return false;
3848 return true;
3851 case NON_LVALUE_EXPR:
3852 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
3853 -- an lvalue of integral type that refers to a non-volatile
3854 const variable or static data member initialized with
3855 constant expressions, or
3857 -- an lvalue of literal type that refers to non-volatile
3858 object defined with constexpr, or that refers to a
3859 sub-object of such an object; */
3860 return RECUR (TREE_OPERAND (t, 0), rval);
3862 case VAR_DECL:
3863 if (want_rval
3864 && !decl_constant_var_p (t)
3865 && (strict
3866 || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t))
3867 || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t))
3868 && !var_in_constexpr_fn (t)
3869 && !dependent_type_p (TREE_TYPE (t)))
3871 if (flags & tf_error)
3872 non_const_var_error (t);
3873 return false;
3875 return true;
3877 case NOP_EXPR:
3878 case CONVERT_EXPR:
3879 case VIEW_CONVERT_EXPR:
3880 /* -- a reinterpret_cast. FIXME not implemented, and this rule
3881 may change to something more specific to type-punning (DR 1312). */
3883 tree from = TREE_OPERAND (t, 0);
3884 if (POINTER_TYPE_P (TREE_TYPE (t))
3885 && TREE_CODE (from) == INTEGER_CST
3886 && !integer_zerop (from))
3888 if (flags & tf_error)
3889 error_at (EXPR_LOC_OR_LOC (t, input_location),
3890 "reinterpret_cast from integer to pointer");
3891 return false;
3893 return (RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR));
3896 case ADDR_EXPR:
3897 /* -- a unary operator & that is applied to an lvalue that
3898 designates an object with thread or automatic storage
3899 duration; */
3900 t = TREE_OPERAND (t, 0);
3902 if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t))
3903 /* A pointer-to-member constant. */
3904 return true;
3906 #if 0
3907 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
3908 any checking here, as we might dereference the pointer later. If
3909 we remove this code, also remove check_automatic_or_tls. */
3910 i = check_automatic_or_tls (t);
3911 if (i == ck_ok)
3912 return true;
3913 if (i == ck_bad)
3915 if (flags & tf_error)
3916 error ("address-of an object %qE with thread local or "
3917 "automatic storage is not a constant expression", t);
3918 return false;
3920 #endif
3921 return RECUR (t, any);
3923 case COMPONENT_REF:
3924 case BIT_FIELD_REF:
3925 case ARROW_EXPR:
3926 case OFFSET_REF:
3927 /* -- a class member access unless its postfix-expression is
3928 of literal type or of pointer to literal type. */
3929 /* This test would be redundant, as it follows from the
3930 postfix-expression being a potential constant expression. */
3931 return RECUR (TREE_OPERAND (t, 0), want_rval);
3933 case EXPR_PACK_EXPANSION:
3934 return RECUR (PACK_EXPANSION_PATTERN (t), want_rval);
3936 case INDIRECT_REF:
3938 tree x = TREE_OPERAND (t, 0);
3939 STRIP_NOPS (x);
3940 if (is_this_parameter (x))
3942 if (DECL_CONTEXT (x)
3943 && !DECL_DECLARED_CONSTEXPR_P (DECL_CONTEXT (x)))
3945 if (flags & tf_error)
3946 error ("use of %<this%> in a constant expression");
3947 return false;
3949 return true;
3951 return RECUR (x, rval);
3954 case STATEMENT_LIST:
3956 tree_stmt_iterator i;
3957 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
3959 if (!RECUR (tsi_stmt (i), any))
3960 return false;
3962 return true;
3964 break;
3966 case MODIFY_EXPR:
3967 if (cxx_dialect < cxx14)
3968 goto fail;
3969 if (!RECUR (TREE_OPERAND (t, 0), any))
3970 return false;
3971 if (!RECUR (TREE_OPERAND (t, 1), rval))
3972 return false;
3973 return true;
3975 case MODOP_EXPR:
3976 if (cxx_dialect < cxx14)
3977 goto fail;
3978 if (!RECUR (TREE_OPERAND (t, 0), rval))
3979 return false;
3980 if (!RECUR (TREE_OPERAND (t, 2), rval))
3981 return false;
3982 return true;
3984 case IF_STMT:
3985 if (!RECUR (IF_COND (t), rval))
3986 return false;
3987 if (!RECUR (THEN_CLAUSE (t), any))
3988 return false;
3989 if (!RECUR (ELSE_CLAUSE (t), any))
3990 return false;
3991 return true;
3993 case DO_STMT:
3994 if (!RECUR (DO_COND (t), rval))
3995 return false;
3996 if (!RECUR (DO_BODY (t), any))
3997 return false;
3998 return true;
4000 case FOR_STMT:
4001 if (!RECUR (FOR_INIT_STMT (t), any))
4002 return false;
4003 if (!RECUR (FOR_COND (t), rval))
4004 return false;
4005 if (!RECUR (FOR_EXPR (t), any))
4006 return false;
4007 if (!RECUR (FOR_BODY (t), any))
4008 return false;
4009 return true;
4011 case WHILE_STMT:
4012 if (!RECUR (WHILE_COND (t), rval))
4013 return false;
4014 if (!RECUR (WHILE_BODY (t), any))
4015 return false;
4016 return true;
4018 case SWITCH_STMT:
4019 if (!RECUR (SWITCH_STMT_COND (t), rval))
4020 return false;
4021 if (!RECUR (SWITCH_STMT_BODY (t), any))
4022 return false;
4023 return true;
4025 case STMT_EXPR:
4026 return RECUR (STMT_EXPR_STMT (t), rval);
4028 case LAMBDA_EXPR:
4029 case DYNAMIC_CAST_EXPR:
4030 case PSEUDO_DTOR_EXPR:
4031 case NEW_EXPR:
4032 case VEC_NEW_EXPR:
4033 case DELETE_EXPR:
4034 case VEC_DELETE_EXPR:
4035 case THROW_EXPR:
4036 case OMP_ATOMIC:
4037 case OMP_ATOMIC_READ:
4038 case OMP_ATOMIC_CAPTURE_OLD:
4039 case OMP_ATOMIC_CAPTURE_NEW:
4040 /* GCC internal stuff. */
4041 case VA_ARG_EXPR:
4042 case OBJ_TYPE_REF:
4043 case TRANSACTION_EXPR:
4044 case ASM_EXPR:
4045 fail:
4046 if (flags & tf_error)
4047 error ("expression %qE is not a constant-expression", t);
4048 return false;
4050 case TYPEID_EXPR:
4051 /* -- a typeid expression whose operand is of polymorphic
4052 class type; */
4054 tree e = TREE_OPERAND (t, 0);
4055 if (!TYPE_P (e) && !type_dependent_expression_p (e)
4056 && TYPE_POLYMORPHIC_P (TREE_TYPE (e)))
4058 if (flags & tf_error)
4059 error ("typeid-expression is not a constant expression "
4060 "because %qE is of polymorphic type", e);
4061 return false;
4063 return true;
4066 case MINUS_EXPR:
4067 /* -- a subtraction where both operands are pointers. */
4068 if (TYPE_PTR_P (TREE_OPERAND (t, 0))
4069 && TYPE_PTR_P (TREE_OPERAND (t, 1)))
4071 if (flags & tf_error)
4072 error ("difference of two pointer expressions is not "
4073 "a constant expression");
4074 return false;
4076 want_rval = true;
4077 goto binary;
4079 case LT_EXPR:
4080 case LE_EXPR:
4081 case GT_EXPR:
4082 case GE_EXPR:
4083 case EQ_EXPR:
4084 case NE_EXPR:
4085 /* -- a relational or equality operator where at least
4086 one of the operands is a pointer. */
4087 if (TYPE_PTR_P (TREE_OPERAND (t, 0))
4088 || TYPE_PTR_P (TREE_OPERAND (t, 1)))
4090 if (flags & tf_error)
4091 error ("pointer comparison expression is not a "
4092 "constant expression");
4093 return false;
4095 want_rval = true;
4096 goto binary;
4098 case PREINCREMENT_EXPR:
4099 case POSTINCREMENT_EXPR:
4100 case PREDECREMENT_EXPR:
4101 case POSTDECREMENT_EXPR:
4102 if (cxx_dialect < cxx14)
4103 goto fail;
4104 goto unary;
4106 case BIT_NOT_EXPR:
4107 /* A destructor. */
4108 if (TYPE_P (TREE_OPERAND (t, 0)))
4109 return true;
4110 /* else fall through. */
4112 case REALPART_EXPR:
4113 case IMAGPART_EXPR:
4114 case CONJ_EXPR:
4115 case SAVE_EXPR:
4116 case FIX_TRUNC_EXPR:
4117 case FLOAT_EXPR:
4118 case NEGATE_EXPR:
4119 case ABS_EXPR:
4120 case TRUTH_NOT_EXPR:
4121 case FIXED_CONVERT_EXPR:
4122 case UNARY_PLUS_EXPR:
4123 unary:
4124 return RECUR (TREE_OPERAND (t, 0), rval);
4126 case CAST_EXPR:
4127 case CONST_CAST_EXPR:
4128 case STATIC_CAST_EXPR:
4129 case REINTERPRET_CAST_EXPR:
4130 case IMPLICIT_CONV_EXPR:
4131 if (cxx_dialect < cxx11
4132 && !dependent_type_p (TREE_TYPE (t))
4133 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t)))
4134 /* In C++98, a conversion to non-integral type can't be part of a
4135 constant expression. */
4137 if (flags & tf_error)
4138 error ("cast to non-integral type %qT in a constant expression",
4139 TREE_TYPE (t));
4140 return false;
4143 return (RECUR (TREE_OPERAND (t, 0),
4144 TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE));
4146 case BIND_EXPR:
4147 return RECUR (BIND_EXPR_BODY (t), want_rval);
4149 case WITH_CLEANUP_EXPR:
4150 case CLEANUP_POINT_EXPR:
4151 case MUST_NOT_THROW_EXPR:
4152 case TRY_CATCH_EXPR:
4153 case EH_SPEC_BLOCK:
4154 case EXPR_STMT:
4155 case PAREN_EXPR:
4156 case DECL_EXPR:
4157 case NON_DEPENDENT_EXPR:
4158 /* For convenience. */
4159 case RETURN_EXPR:
4160 return RECUR (TREE_OPERAND (t, 0), want_rval);
4162 case SCOPE_REF:
4163 return RECUR (TREE_OPERAND (t, 1), want_rval);
4165 case TARGET_EXPR:
4166 if (!literal_type_p (TREE_TYPE (t)))
4168 if (flags & tf_error)
4170 error ("temporary of non-literal type %qT in a "
4171 "constant expression", TREE_TYPE (t));
4172 explain_non_literal_class (TREE_TYPE (t));
4174 return false;
4176 case INIT_EXPR:
4177 return RECUR (TREE_OPERAND (t, 1), rval);
4179 case CONSTRUCTOR:
4181 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
4182 constructor_elt *ce;
4183 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
4184 if (!RECUR (ce->value, want_rval))
4185 return false;
4186 return true;
4189 case TREE_LIST:
4191 gcc_assert (TREE_PURPOSE (t) == NULL_TREE
4192 || DECL_P (TREE_PURPOSE (t)));
4193 if (!RECUR (TREE_VALUE (t), want_rval))
4194 return false;
4195 if (TREE_CHAIN (t) == NULL_TREE)
4196 return true;
4197 return RECUR (TREE_CHAIN (t), want_rval);
4200 case TRUNC_DIV_EXPR:
4201 case CEIL_DIV_EXPR:
4202 case FLOOR_DIV_EXPR:
4203 case ROUND_DIV_EXPR:
4204 case TRUNC_MOD_EXPR:
4205 case CEIL_MOD_EXPR:
4206 case ROUND_MOD_EXPR:
4208 tree denom = TREE_OPERAND (t, 1);
4209 if (!RECUR (denom, rval))
4210 return false;
4211 /* We can't call cxx_eval_outermost_constant_expr on an expression
4212 that hasn't been through instantiate_non_dependent_expr yet. */
4213 if (!processing_template_decl)
4214 denom = cxx_eval_outermost_constant_expr (denom, true);
4215 if (integer_zerop (denom))
4217 if (flags & tf_error)
4218 error ("division by zero is not a constant-expression");
4219 return false;
4221 else
4223 want_rval = true;
4224 return RECUR (TREE_OPERAND (t, 0), want_rval);
4228 case COMPOUND_EXPR:
4230 /* check_return_expr sometimes wraps a TARGET_EXPR in a
4231 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
4232 introduced by build_call_a. */
4233 tree op0 = TREE_OPERAND (t, 0);
4234 tree op1 = TREE_OPERAND (t, 1);
4235 STRIP_NOPS (op1);
4236 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
4237 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
4238 return RECUR (op0, want_rval);
4239 else
4240 goto binary;
4243 /* If the first operand is the non-short-circuit constant, look at
4244 the second operand; otherwise we only care about the first one for
4245 potentiality. */
4246 case TRUTH_AND_EXPR:
4247 case TRUTH_ANDIF_EXPR:
4248 tmp = boolean_true_node;
4249 goto truth;
4250 case TRUTH_OR_EXPR:
4251 case TRUTH_ORIF_EXPR:
4252 tmp = boolean_false_node;
4253 truth:
4255 tree op = TREE_OPERAND (t, 0);
4256 if (!RECUR (op, rval))
4257 return false;
4258 if (!processing_template_decl)
4259 op = cxx_eval_outermost_constant_expr (op, true);
4260 if (tree_int_cst_equal (op, tmp))
4261 return RECUR (TREE_OPERAND (t, 1), rval);
4262 else
4263 return true;
4266 case PLUS_EXPR:
4267 case MULT_EXPR:
4268 case POINTER_PLUS_EXPR:
4269 case RDIV_EXPR:
4270 case EXACT_DIV_EXPR:
4271 case MIN_EXPR:
4272 case MAX_EXPR:
4273 case LSHIFT_EXPR:
4274 case RSHIFT_EXPR:
4275 case LROTATE_EXPR:
4276 case RROTATE_EXPR:
4277 case BIT_IOR_EXPR:
4278 case BIT_XOR_EXPR:
4279 case BIT_AND_EXPR:
4280 case TRUTH_XOR_EXPR:
4281 case UNORDERED_EXPR:
4282 case ORDERED_EXPR:
4283 case UNLT_EXPR:
4284 case UNLE_EXPR:
4285 case UNGT_EXPR:
4286 case UNGE_EXPR:
4287 case UNEQ_EXPR:
4288 case LTGT_EXPR:
4289 case RANGE_EXPR:
4290 case COMPLEX_EXPR:
4291 want_rval = true;
4292 /* Fall through. */
4293 case ARRAY_REF:
4294 case ARRAY_RANGE_REF:
4295 case MEMBER_REF:
4296 case DOTSTAR_EXPR:
4297 binary:
4298 for (i = 0; i < 2; ++i)
4299 if (!RECUR (TREE_OPERAND (t, i), want_rval))
4300 return false;
4301 return true;
4303 case CILK_SYNC_STMT:
4304 case CILK_SPAWN_STMT:
4305 case ARRAY_NOTATION_REF:
4306 return false;
4308 case FMA_EXPR:
4309 case VEC_PERM_EXPR:
4310 for (i = 0; i < 3; ++i)
4311 if (!RECUR (TREE_OPERAND (t, i), true))
4312 return false;
4313 return true;
4315 case COND_EXPR:
4316 case VEC_COND_EXPR:
4317 /* If the condition is a known constant, we know which of the legs we
4318 care about; otherwise we only require that the condition and
4319 either of the legs be potentially constant. */
4320 tmp = TREE_OPERAND (t, 0);
4321 if (!RECUR (tmp, rval))
4322 return false;
4323 if (!processing_template_decl)
4324 tmp = cxx_eval_outermost_constant_expr (tmp, true);
4325 if (integer_zerop (tmp))
4326 return RECUR (TREE_OPERAND (t, 2), want_rval);
4327 else if (TREE_CODE (tmp) == INTEGER_CST)
4328 return RECUR (TREE_OPERAND (t, 1), want_rval);
4329 for (i = 1; i < 3; ++i)
4330 if (potential_constant_expression_1 (TREE_OPERAND (t, i),
4331 want_rval, strict, tf_none))
4332 return true;
4333 if (flags & tf_error)
4334 error ("expression %qE is not a constant-expression", t);
4335 return false;
4337 case VEC_INIT_EXPR:
4338 if (VEC_INIT_EXPR_IS_CONSTEXPR (t))
4339 return true;
4340 if (flags & tf_error)
4342 error ("non-constant array initialization");
4343 diagnose_non_constexpr_vec_init (t);
4345 return false;
4347 default:
4348 if (objc_is_property_ref (t))
4349 return false;
4351 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t)));
4352 gcc_unreachable();
4353 return false;
4355 #undef RECUR
4358 /* The main entry point to the above. */
4360 bool
4361 potential_constant_expression (tree t)
4363 return potential_constant_expression_1 (t, false, true, tf_none);
4366 bool
4367 potential_static_init_expression (tree t)
4369 return potential_constant_expression_1 (t, false, false, tf_none);
4372 /* As above, but require a constant rvalue. */
4374 bool
4375 potential_rvalue_constant_expression (tree t)
4377 return potential_constant_expression_1 (t, true, true, tf_none);
4380 /* Like above, but complain about non-constant expressions. */
4382 bool
4383 require_potential_constant_expression (tree t)
4385 return potential_constant_expression_1 (t, false, true, tf_warning_or_error);
4388 /* Cross product of the above. */
4390 bool
4391 require_potential_rvalue_constant_expression (tree t)
4393 return potential_constant_expression_1 (t, true, true, tf_warning_or_error);
4396 #include "gt-cp-constexpr.h"