]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/constexpr.c
PR c++/86836
[thirdparty/gcc.git] / gcc / cp / constexpr.c
CommitLineData
f9885b8a 1/* Perform -*- C++ -*- constant expression evaluation, including calls to
2 constexpr functions. These routines are used both during actual parsing
09b42213 3 and during the instantiation of template functions.
4
8e8f6434 5 Copyright (C) 1998-2018 Free Software Foundation, Inc.
09b42213 6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
13
14 GCC is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with GCC; see the file COPYING3. If not see
21<http://www.gnu.org/licenses/>. */
22
23#include "config.h"
24#include "system.h"
25#include "coretypes.h"
09b42213 26#include "cp-tree.h"
4cba6f60 27#include "varasm.h"
09b42213 28#include "c-family/c-objc.h"
29#include "tree-iterator.h"
30#include "gimplify.h"
31#include "builtins.h"
9c96033c 32#include "tree-inline.h"
6b71bdb4 33#include "ubsan.h"
732905bb 34#include "gimple-fold.h"
c300fe28 35#include "timevar.h"
09b42213 36
37static bool verify_constant (tree, bool, bool *, bool *);
38#define VERIFY_CONSTANT(X) \
39do { \
f83e6885 40 if (verify_constant ((X), ctx->quiet, non_constant_p, overflow_p)) \
09b42213 41 return t; \
42 } while (0)
43
44/* Returns true iff FUN is an instantiation of a constexpr function
45 template or a defaulted constexpr function. */
46
47bool
48is_instantiation_of_constexpr (tree fun)
49{
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)));
54}
55
56/* Return true if T is a literal type. */
57
58bool
59literal_type_p (tree t)
60{
61 if (SCALAR_TYPE_P (t)
76249021 62 || VECTOR_TYPE_P (t)
90ad495b 63 || TYPE_REF_P (t)
dd418b39 64 || (VOID_TYPE_P (t) && cxx_dialect >= cxx14))
09b42213 65 return true;
66 if (CLASS_TYPE_P (t))
67 {
68 t = complete_type (t);
69 gcc_assert (COMPLETE_TYPE_P (t) || errorcount);
70 return CLASSTYPE_LITERAL_P (t);
71 }
72 if (TREE_CODE (t) == ARRAY_TYPE)
73 return literal_type_p (strip_array_types (t));
74 return false;
75}
76
77/* If DECL is a variable declared `constexpr', require its type
e89257db 78 be literal. Return error_mark_node if we give an error, the
79 DECL otherwise. */
09b42213 80
81tree
82ensure_literal_type_for_constexpr_object (tree decl)
83{
84 tree type = TREE_TYPE (decl);
85 if (VAR_P (decl)
86 && (DECL_DECLARED_CONSTEXPR_P (decl)
87 || var_in_constexpr_fn (decl))
88 && !processing_template_decl)
89 {
90 tree stype = strip_array_types (type);
91 if (CLASS_TYPE_P (stype) && !COMPLETE_TYPE_P (complete_type (stype)))
92 /* Don't complain here, we'll complain about incompleteness
93 when we try to initialize the variable. */;
03dbe51b 94 else if (type_uses_auto (type))
95 /* We don't know the actual type yet. */;
09b42213 96 else if (!literal_type_p (type))
97 {
98 if (DECL_DECLARED_CONSTEXPR_P (decl))
e45408ff 99 {
5967b28b 100 error ("the type %qT of %<constexpr%> variable %qD "
101 "is not literal", type, decl);
e45408ff 102 explain_non_literal_class (type);
e89257db 103 decl = error_mark_node;
e45408ff 104 }
09b42213 105 else
9c96033c 106 {
b7e4a558 107 if (!is_instantiation_of_constexpr (current_function_decl))
e45408ff 108 {
109 error ("variable %qD of non-literal type %qT in %<constexpr%> "
110 "function", decl, type);
111 explain_non_literal_class (type);
e89257db 112 decl = error_mark_node;
e45408ff 113 }
9c96033c 114 cp_function_chain->invalid_constexpr = true;
115 }
09b42213 116 }
fdafeeb7 117 else if (DECL_DECLARED_CONSTEXPR_P (decl)
118 && variably_modified_type_p (type, NULL_TREE))
119 {
120 error ("%<constexpr%> variable %qD has variably-modified type %qT",
121 decl, type);
122 decl = error_mark_node;
123 }
09b42213 124 }
125 return decl;
126}
127
128/* Representation of entries in the constexpr function definition table. */
129
130struct GTY((for_user)) constexpr_fundef {
131 tree decl;
132 tree body;
133};
134
b594087e 135struct constexpr_fundef_hasher : ggc_ptr_hash<constexpr_fundef>
09b42213 136{
137 static hashval_t hash (constexpr_fundef *);
138 static bool equal (constexpr_fundef *, constexpr_fundef *);
139};
140
141/* This table holds all constexpr function definitions seen in
142 the current translation unit. */
143
144static GTY (()) hash_table<constexpr_fundef_hasher> *constexpr_fundef_table;
145
146/* Utility function used for managing the constexpr function table.
147 Return true if the entries pointed to by P and Q are for the
148 same constexpr function. */
149
150inline bool
151constexpr_fundef_hasher::equal (constexpr_fundef *lhs, constexpr_fundef *rhs)
152{
153 return lhs->decl == rhs->decl;
154}
155
156/* Utility function used for managing the constexpr function table.
157 Return a hash value for the entry pointed to by Q. */
158
159inline hashval_t
160constexpr_fundef_hasher::hash (constexpr_fundef *fundef)
161{
162 return DECL_UID (fundef->decl);
163}
164
165/* Return a previously saved definition of function FUN. */
166
167static constexpr_fundef *
168retrieve_constexpr_fundef (tree fun)
169{
170 constexpr_fundef fundef = { NULL, NULL };
171 if (constexpr_fundef_table == NULL)
172 return NULL;
173
174 fundef.decl = fun;
175 return constexpr_fundef_table->find (&fundef);
176}
177
178/* Check whether the parameter and return types of FUN are valid for a
179 constexpr function, and complain if COMPLAIN. */
180
33603066 181bool
09b42213 182is_valid_constexpr_fn (tree fun, bool complain)
183{
184 bool ret = true;
185
7896267d 186 if (DECL_INHERITED_CTOR (fun)
09b42213 187 && TREE_CODE (fun) == TEMPLATE_DECL)
188 {
189 ret = false;
190 if (complain)
5967b28b 191 error ("inherited constructor %qD is not %<constexpr%>",
7896267d 192 DECL_INHERITED_CTOR (fun));
09b42213 193 }
194 else
195 {
196 for (tree parm = FUNCTION_FIRST_USER_PARM (fun);
197 parm != NULL_TREE; parm = TREE_CHAIN (parm))
198 if (!literal_type_p (TREE_TYPE (parm)))
199 {
200 ret = false;
201 if (complain)
202 {
5967b28b 203 error ("invalid type for parameter %d of %<constexpr%> "
09b42213 204 "function %q+#D", DECL_PARM_INDEX (parm), fun);
205 explain_non_literal_class (TREE_TYPE (parm));
206 }
207 }
208 }
209
672871ce 210 if (LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun)) && cxx_dialect < cxx17)
211 {
212 ret = false;
213 if (complain)
214 inform (DECL_SOURCE_LOCATION (fun),
5967b28b 215 "lambdas are implicitly %<constexpr%> only in C++17 and later");
672871ce 216 }
217 else if (!DECL_CONSTRUCTOR_P (fun))
09b42213 218 {
219 tree rettype = TREE_TYPE (TREE_TYPE (fun));
220 if (!literal_type_p (rettype))
221 {
222 ret = false;
223 if (complain)
224 {
5967b28b 225 error ("invalid return type %qT of %<constexpr%> function %q+D",
09b42213 226 rettype, fun);
227 explain_non_literal_class (rettype);
228 }
229 }
230
d66f34cf 231 /* C++14 DR 1684 removed this restriction. */
232 if (cxx_dialect < cxx14
233 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
09b42213 234 && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun)))
235 {
236 ret = false;
d66f34cf 237 if (complain
238 && pedwarn (DECL_SOURCE_LOCATION (fun), OPT_Wpedantic,
5967b28b 239 "enclosing class of %<constexpr%> non-static member "
d66f34cf 240 "function %q+#D is not a literal type", fun))
241 explain_non_literal_class (DECL_CONTEXT (fun));
09b42213 242 }
243 }
244 else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun)))
245 {
246 ret = false;
247 if (complain)
248 error ("%q#T has virtual base classes", DECL_CONTEXT (fun));
249 }
250
251 return ret;
252}
253
254/* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF
255 for a member of an anonymous aggregate, INIT is the initializer for that
256 member, and VEC_OUTER is the vector of constructor elements for the class
257 whose constructor we are processing. Add the initializer to the vector
258 and return true to indicate success. */
259
260static bool
261build_anon_member_initialization (tree member, tree init,
262 vec<constructor_elt, va_gc> **vec_outer)
263{
264 /* MEMBER presents the relevant fields from the inside out, but we need
265 to build up the initializer from the outside in so that we can reuse
266 previously built CONSTRUCTORs if this is, say, the second field in an
267 anonymous struct. So we use a vec as a stack. */
268 auto_vec<tree, 2> fields;
269 do
270 {
271 fields.safe_push (TREE_OPERAND (member, 1));
272 member = TREE_OPERAND (member, 0);
273 }
274 while (ANON_AGGR_TYPE_P (TREE_TYPE (member))
275 && TREE_CODE (member) == COMPONENT_REF);
276
277 /* VEC has the constructor elements vector for the context of FIELD.
278 If FIELD is an anonymous aggregate, we will push inside it. */
279 vec<constructor_elt, va_gc> **vec = vec_outer;
280 tree field;
281 while (field = fields.pop(),
282 ANON_AGGR_TYPE_P (TREE_TYPE (field)))
283 {
284 tree ctor;
285 /* If there is already an outer constructor entry for the anonymous
286 aggregate FIELD, use it; otherwise, insert one. */
287 if (vec_safe_is_empty (*vec)
288 || (*vec)->last().index != field)
289 {
290 ctor = build_constructor (TREE_TYPE (field), NULL);
291 CONSTRUCTOR_APPEND_ELT (*vec, field, ctor);
292 }
293 else
294 ctor = (*vec)->last().value;
295 vec = &CONSTRUCTOR_ELTS (ctor);
296 }
297
298 /* Now we're at the innermost field, the one that isn't an anonymous
299 aggregate. Add its initializer to the CONSTRUCTOR and we're done. */
300 gcc_assert (fields.is_empty());
301 CONSTRUCTOR_APPEND_ELT (*vec, field, init);
302
303 return true;
304}
305
306/* Subroutine of build_constexpr_constructor_member_initializers.
307 The expression tree T represents a data member initialization
308 in a (constexpr) constructor definition. Build a pairing of
309 the data member with its initializer, and prepend that pair
310 to the existing initialization pair INITS. */
311
312static bool
313build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec)
314{
315 tree member, init;
316 if (TREE_CODE (t) == CLEANUP_POINT_EXPR)
317 t = TREE_OPERAND (t, 0);
318 if (TREE_CODE (t) == EXPR_STMT)
319 t = TREE_OPERAND (t, 0);
320 if (t == error_mark_node)
321 return false;
322 if (TREE_CODE (t) == STATEMENT_LIST)
323 {
324 tree_stmt_iterator i;
325 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
326 {
327 if (! build_data_member_initialization (tsi_stmt (i), vec))
328 return false;
329 }
330 return true;
331 }
332 if (TREE_CODE (t) == CLEANUP_STMT)
333 {
334 /* We can't see a CLEANUP_STMT in a constructor for a literal class,
335 but we can in a constexpr constructor for a non-literal class. Just
336 ignore it; either all the initialization will be constant, in which
337 case the cleanup can't run, or it can't be constexpr.
338 Still recurse into CLEANUP_BODY. */
339 return build_data_member_initialization (CLEANUP_BODY (t), vec);
340 }
341 if (TREE_CODE (t) == CONVERT_EXPR)
342 t = TREE_OPERAND (t, 0);
343 if (TREE_CODE (t) == INIT_EXPR
9c96033c 344 /* vptr initialization shows up as a MODIFY_EXPR. In C++14 we only
345 use what this function builds for cx_check_missing_mem_inits, and
346 assignment in the ctor body doesn't count. */
347 || (cxx_dialect < cxx14 && TREE_CODE (t) == MODIFY_EXPR))
09b42213 348 {
349 member = TREE_OPERAND (t, 0);
350 init = break_out_target_exprs (TREE_OPERAND (t, 1));
351 }
352 else if (TREE_CODE (t) == CALL_EXPR)
353 {
9c96033c 354 tree fn = get_callee_fndecl (t);
355 if (!fn || !DECL_CONSTRUCTOR_P (fn))
356 /* We're only interested in calls to subobject constructors. */
357 return true;
09b42213 358 member = CALL_EXPR_ARG (t, 0);
359 /* We don't use build_cplus_new here because it complains about
360 abstract bases. Leaving the call unwrapped means that it has the
361 wrong type, but cxx_eval_constant_expression doesn't care. */
362 init = break_out_target_exprs (t);
363 }
364 else if (TREE_CODE (t) == BIND_EXPR)
365 return build_data_member_initialization (BIND_EXPR_BODY (t), vec);
09b42213 366 else
9c96033c 367 /* Don't add anything else to the CONSTRUCTOR. */
368 return true;
09b42213 369 if (INDIRECT_REF_P (member))
370 member = TREE_OPERAND (member, 0);
371 if (TREE_CODE (member) == NOP_EXPR)
372 {
373 tree op = member;
374 STRIP_NOPS (op);
375 if (TREE_CODE (op) == ADDR_EXPR)
376 {
377 gcc_assert (same_type_ignoring_top_level_qualifiers_p
378 (TREE_TYPE (TREE_TYPE (op)),
379 TREE_TYPE (TREE_TYPE (member))));
380 /* Initializing a cv-qualified member; we need to look through
381 the const_cast. */
382 member = op;
383 }
384 else if (op == current_class_ptr
385 && (same_type_ignoring_top_level_qualifiers_p
386 (TREE_TYPE (TREE_TYPE (member)),
387 current_class_type)))
388 /* Delegating constructor. */
389 member = op;
390 else
391 {
392 /* This is an initializer for an empty base; keep it for now so
393 we can check it in cxx_eval_bare_aggregate. */
394 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member))));
395 }
396 }
397 if (TREE_CODE (member) == ADDR_EXPR)
398 member = TREE_OPERAND (member, 0);
399 if (TREE_CODE (member) == COMPONENT_REF)
400 {
401 tree aggr = TREE_OPERAND (member, 0);
4952bc85 402 if (TREE_CODE (aggr) == VAR_DECL)
403 /* Initializing a local variable, don't add anything. */
404 return true;
09b42213 405 if (TREE_CODE (aggr) != COMPONENT_REF)
406 /* Normal member initialization. */
407 member = TREE_OPERAND (member, 1);
408 else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr)))
409 /* Initializing a member of an anonymous union. */
410 return build_anon_member_initialization (member, init, vec);
411 else
412 /* We're initializing a vtable pointer in a base. Leave it as
413 COMPONENT_REF so we remember the path to get to the vfield. */
414 gcc_assert (TREE_TYPE (member) == vtbl_ptr_type_node);
415 }
416
c678e23e 417 /* Value-initialization can produce multiple initializers for the
418 same field; use the last one. */
419 if (!vec_safe_is_empty (*vec) && (*vec)->last().index == member)
420 (*vec)->last().value = init;
421 else
422 CONSTRUCTOR_APPEND_ELT (*vec, member, init);
09b42213 423 return true;
424}
425
426/* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval.
427 In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a
428 BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */
429
430static bool
431check_constexpr_bind_expr_vars (tree t)
432{
433 gcc_assert (TREE_CODE (t) == BIND_EXPR);
434
09b42213 435 for (tree var = BIND_EXPR_VARS (t); var; var = DECL_CHAIN (var))
436 if (TREE_CODE (var) == TYPE_DECL
04103c39 437 && DECL_IMPLICIT_TYPEDEF_P (var)
438 && !LAMBDA_TYPE_P (TREE_TYPE (var)))
09b42213 439 return false;
440 return true;
441}
442
443/* Subroutine of check_constexpr_ctor_body. */
444
445static bool
446check_constexpr_ctor_body_1 (tree last, tree list)
447{
448 switch (TREE_CODE (list))
449 {
450 case DECL_EXPR:
810702a2 451 if (TREE_CODE (DECL_EXPR_DECL (list)) == USING_DECL
452 || TREE_CODE (DECL_EXPR_DECL (list)) == TYPE_DECL)
09b42213 453 return true;
09b42213 454 return false;
455
456 case CLEANUP_POINT_EXPR:
457 return check_constexpr_ctor_body (last, TREE_OPERAND (list, 0),
458 /*complain=*/false);
459
460 case BIND_EXPR:
461 if (!check_constexpr_bind_expr_vars (list)
462 || !check_constexpr_ctor_body (last, BIND_EXPR_BODY (list),
463 /*complain=*/false))
464 return false;
465 return true;
466
467 case USING_STMT:
468 case STATIC_ASSERT:
90567983 469 case DEBUG_BEGIN_STMT:
09b42213 470 return true;
471
472 default:
473 return false;
474 }
475}
476
477/* Make sure that there are no statements after LAST in the constructor
478 body represented by LIST. */
479
480bool
481check_constexpr_ctor_body (tree last, tree list, bool complain)
482{
9c96033c 483 /* C++14 doesn't require a constexpr ctor to have an empty body. */
484 if (cxx_dialect >= cxx14)
485 return true;
486
09b42213 487 bool ok = true;
488 if (TREE_CODE (list) == STATEMENT_LIST)
489 {
490 tree_stmt_iterator i = tsi_last (list);
491 for (; !tsi_end_p (i); tsi_prev (&i))
492 {
493 tree t = tsi_stmt (i);
494 if (t == last)
495 break;
496 if (!check_constexpr_ctor_body_1 (last, t))
497 {
498 ok = false;
499 break;
500 }
501 }
502 }
503 else if (list != last
504 && !check_constexpr_ctor_body_1 (last, list))
505 ok = false;
506 if (!ok)
507 {
508 if (complain)
5967b28b 509 error ("%<constexpr%> constructor does not have empty body");
09b42213 510 DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false;
511 }
512 return ok;
513}
514
515/* V is a vector of constructor elements built up for the base and member
516 initializers of a constructor for TYPE. They need to be in increasing
517 offset order, which they might not be yet if TYPE has a primary base
518 which is not first in the base-clause or a vptr and at least one base
519 all of which are non-primary. */
520
521static vec<constructor_elt, va_gc> *
522sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v)
523{
524 tree pri = CLASSTYPE_PRIMARY_BINFO (type);
525 tree field_type;
526 unsigned i;
527 constructor_elt *ce;
528
529 if (pri)
530 field_type = BINFO_TYPE (pri);
531 else if (TYPE_CONTAINS_VPTR_P (type))
532 field_type = vtbl_ptr_type_node;
533 else
534 return v;
535
536 /* Find the element for the primary base or vptr and move it to the
537 beginning of the vec. */
538 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
539 if (TREE_TYPE (ce->index) == field_type)
540 break;
541
542 if (i > 0 && i < vec_safe_length (v))
543 {
544 vec<constructor_elt, va_gc> &vref = *v;
545 constructor_elt elt = vref[i];
546 for (; i > 0; --i)
547 vref[i] = vref[i-1];
548 vref[0] = elt;
549 }
550
551 return v;
552}
553
554/* Build compile-time evalable representations of member-initializer list
555 for a constexpr constructor. */
556
557static tree
558build_constexpr_constructor_member_initializers (tree type, tree body)
559{
560 vec<constructor_elt, va_gc> *vec = NULL;
561 bool ok = true;
816919e1 562 while (true)
563 switch (TREE_CODE (body))
564 {
565 case MUST_NOT_THROW_EXPR:
566 case EH_SPEC_BLOCK:
567 body = TREE_OPERAND (body, 0);
568 break;
569
570 case STATEMENT_LIST:
571 for (tree_stmt_iterator i = tsi_start (body);
572 !tsi_end_p (i); tsi_next (&i))
573 {
574 body = tsi_stmt (i);
575 if (TREE_CODE (body) == BIND_EXPR)
576 break;
577 }
578 break;
579
580 case BIND_EXPR:
581 body = BIND_EXPR_BODY (body);
582 goto found;
583
584 default:
585 gcc_unreachable ();
8a36d0ec 586 }
816919e1 587 found:
09b42213 588 if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
589 {
590 body = TREE_OPERAND (body, 0);
591 if (TREE_CODE (body) == EXPR_STMT)
592 body = TREE_OPERAND (body, 0);
593 if (TREE_CODE (body) == INIT_EXPR
594 && (same_type_ignoring_top_level_qualifiers_p
595 (TREE_TYPE (TREE_OPERAND (body, 0)),
596 current_class_type)))
597 {
598 /* Trivial copy. */
599 return TREE_OPERAND (body, 1);
600 }
601 ok = build_data_member_initialization (body, &vec);
602 }
603 else if (TREE_CODE (body) == STATEMENT_LIST)
604 {
605 tree_stmt_iterator i;
606 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
607 {
608 ok = build_data_member_initialization (tsi_stmt (i), &vec);
609 if (!ok)
610 break;
611 }
612 }
613 else if (TREE_CODE (body) == TRY_BLOCK)
614 {
615 error ("body of %<constexpr%> constructor cannot be "
616 "a function-try-block");
617 return error_mark_node;
618 }
619 else if (EXPR_P (body))
620 ok = build_data_member_initialization (body, &vec);
621 else
622 gcc_assert (errorcount > 0);
623 if (ok)
624 {
625 if (vec_safe_length (vec) > 0)
626 {
627 /* In a delegating constructor, return the target. */
628 constructor_elt *ce = &(*vec)[0];
629 if (ce->index == current_class_ptr)
630 {
631 body = ce->value;
632 vec_free (vec);
633 return body;
634 }
635 }
636 vec = sort_constexpr_mem_initializers (type, vec);
637 return build_constructor (type, vec);
638 }
639 else
640 return error_mark_node;
641}
642
2fb20ba2 643/* We have an expression tree T that represents a call, either CALL_EXPR
644 or AGGR_INIT_EXPR. If the call is lexically to a named function,
645 retrun the _DECL for that function. */
646
647static tree
648get_function_named_in_call (tree t)
649{
650 tree fun = cp_get_callee (t);
651 if (fun && TREE_CODE (fun) == ADDR_EXPR
652 && TREE_CODE (TREE_OPERAND (fun, 0)) == FUNCTION_DECL)
653 fun = TREE_OPERAND (fun, 0);
654 return fun;
655}
656
09b42213 657/* Subroutine of register_constexpr_fundef. BODY is the body of a function
658 declared to be constexpr, or a sub-statement thereof. Returns the
659 return value if suitable, error_mark_node for a statement not allowed in
660 a constexpr function, or NULL_TREE if no return value was found. */
661
c49bd35e 662tree
09b42213 663constexpr_fn_retval (tree body)
664{
665 switch (TREE_CODE (body))
666 {
667 case STATEMENT_LIST:
668 {
669 tree_stmt_iterator i;
670 tree expr = NULL_TREE;
671 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
672 {
673 tree s = constexpr_fn_retval (tsi_stmt (i));
674 if (s == error_mark_node)
675 return error_mark_node;
676 else if (s == NULL_TREE)
677 /* Keep iterating. */;
678 else if (expr)
679 /* Multiple return statements. */
680 return error_mark_node;
681 else
682 expr = s;
683 }
684 return expr;
685 }
686
687 case RETURN_EXPR:
688 return break_out_target_exprs (TREE_OPERAND (body, 0));
689
690 case DECL_EXPR:
394aed6a 691 {
692 tree decl = DECL_EXPR_DECL (body);
693 if (TREE_CODE (decl) == USING_DECL
694 /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */
695 || DECL_ARTIFICIAL (decl))
696 return NULL_TREE;
697 return error_mark_node;
698 }
09b42213 699
700 case CLEANUP_POINT_EXPR:
701 return constexpr_fn_retval (TREE_OPERAND (body, 0));
702
703 case BIND_EXPR:
704 if (!check_constexpr_bind_expr_vars (body))
705 return error_mark_node;
706 return constexpr_fn_retval (BIND_EXPR_BODY (body));
707
708 case USING_STMT:
90567983 709 case DEBUG_BEGIN_STMT:
09b42213 710 return NULL_TREE;
711
2fb20ba2 712 case CALL_EXPR:
713 {
714 tree fun = get_function_named_in_call (body);
715 if (fun != NULL_TREE
716 && DECL_FUNCTION_CODE (fun) == BUILT_IN_UNREACHABLE)
717 return NULL_TREE;
718 }
719 /* Fallthru. */
720
09b42213 721 default:
722 return error_mark_node;
723 }
724}
725
726/* Subroutine of register_constexpr_fundef. BODY is the DECL_SAVED_TREE of
727 FUN; do the necessary transformations to turn it into a single expression
728 that we can store in the hash table. */
729
730static tree
731massage_constexpr_body (tree fun, tree body)
732{
733 if (DECL_CONSTRUCTOR_P (fun))
734 body = build_constexpr_constructor_member_initializers
735 (DECL_CONTEXT (fun), body);
9c96033c 736 else if (cxx_dialect < cxx14)
09b42213 737 {
738 if (TREE_CODE (body) == EH_SPEC_BLOCK)
739 body = EH_SPEC_STMTS (body);
740 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
741 body = TREE_OPERAND (body, 0);
742 body = constexpr_fn_retval (body);
743 }
744 return body;
745}
746
2f4cab3c 747/* CTYPE is a type constructed from BODY. Return true if some
748 bases/fields are uninitialized, and complain if COMPLAIN. */
09b42213 749
750static bool
2f4cab3c 751cx_check_missing_mem_inits (tree ctype, tree body, bool complain)
09b42213 752{
2f4cab3c 753 unsigned nelts = 0;
754
755 if (body)
756 {
757 if (TREE_CODE (body) != CONSTRUCTOR)
758 return false;
759 nelts = CONSTRUCTOR_NELTS (body);
760 }
761 tree field = TYPE_FIELDS (ctype);
09b42213 762
763 if (TREE_CODE (ctype) == UNION_TYPE)
764 {
765 if (nelts == 0 && next_initializable_field (field))
766 {
767 if (complain)
768 error ("%<constexpr%> constructor for union %qT must "
769 "initialize exactly one non-static data member", ctype);
770 return true;
771 }
772 return false;
773 }
774
2f4cab3c 775 /* Iterate over the CONSTRUCTOR, checking any missing fields don't
776 need an explicit initialization. */
777 bool bad = false;
778 for (unsigned i = 0; i <= nelts; ++i)
09b42213 779 {
2f4cab3c 780 tree index = NULL_TREE;
781 if (i < nelts)
09b42213 782 {
783 index = CONSTRUCTOR_ELT (body, i)->index;
784 /* Skip base and vtable inits. */
785 if (TREE_CODE (index) != FIELD_DECL
786 || DECL_ARTIFICIAL (index))
787 continue;
788 }
2f4cab3c 789
09b42213 790 for (; field != index; field = DECL_CHAIN (field))
791 {
792 tree ftype;
2f4cab3c 793 if (TREE_CODE (field) != FIELD_DECL)
794 continue;
3cde3c29 795 if (DECL_UNNAMED_BIT_FIELD (field))
09b42213 796 continue;
2f4cab3c 797 if (DECL_ARTIFICIAL (field))
798 continue;
799 if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
800 {
801 /* Recurse to check the anonummous aggregate member. */
802 bad |= cx_check_missing_mem_inits
803 (TREE_TYPE (field), NULL_TREE, complain);
804 if (bad && !complain)
805 return true;
806 continue;
807 }
09b42213 808 ftype = strip_array_types (TREE_TYPE (field));
809 if (type_has_constexpr_default_constructor (ftype))
810 {
811 /* It's OK to skip a member with a trivial constexpr ctor.
812 A constexpr ctor that isn't trivial should have been
813 added in by now. */
814 gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype)
815 || errorcount != 0);
816 continue;
817 }
818 if (!complain)
819 return true;
859e2c29 820 error ("member %qD must be initialized by mem-initializer "
821 "in %<constexpr%> constructor", field);
822 inform (DECL_SOURCE_LOCATION (field), "declared here");
09b42213 823 bad = true;
824 }
825 if (field == NULL_TREE)
826 break;
2f4cab3c 827
828 if (ANON_AGGR_TYPE_P (TREE_TYPE (index)))
829 {
830 /* Check the anonymous aggregate initializer is valid. */
831 bad |= cx_check_missing_mem_inits
832 (TREE_TYPE (index), CONSTRUCTOR_ELT (body, i)->value, complain);
833 if (bad && !complain)
834 return true;
835 }
09b42213 836 field = DECL_CHAIN (field);
837 }
838
839 return bad;
840}
841
842/* We are processing the definition of the constexpr function FUN.
843 Check that its BODY fulfills the propriate requirements and
844 enter it in the constexpr function definition table.
845 For constructor BODY is actually the TREE_LIST of the
846 member-initializer list. */
847
848tree
849register_constexpr_fundef (tree fun, tree body)
850{
851 constexpr_fundef entry;
852 constexpr_fundef **slot;
853
854 if (!is_valid_constexpr_fn (fun, !DECL_GENERATED_P (fun)))
855 return NULL;
856
6a385150 857 tree massaged = massage_constexpr_body (fun, body);
858 if (massaged == NULL_TREE || massaged == error_mark_node)
09b42213 859 {
860 if (!DECL_CONSTRUCTOR_P (fun))
5967b28b 861 error ("body of %<constexpr%> function %qD not a return-statement",
862 fun);
09b42213 863 return NULL;
864 }
865
6a385150 866 if (!potential_rvalue_constant_expression (massaged))
09b42213 867 {
868 if (!DECL_GENERATED_P (fun))
6a385150 869 require_potential_rvalue_constant_expression (massaged);
09b42213 870 return NULL;
871 }
872
873 if (DECL_CONSTRUCTOR_P (fun)
2f4cab3c 874 && cx_check_missing_mem_inits (DECL_CONTEXT (fun),
875 massaged, !DECL_GENERATED_P (fun)))
09b42213 876 return NULL;
877
878 /* Create the constexpr function table if necessary. */
879 if (constexpr_fundef_table == NULL)
880 constexpr_fundef_table
881 = hash_table<constexpr_fundef_hasher>::create_ggc (101);
882
883 entry.decl = fun;
884 entry.body = body;
885 slot = constexpr_fundef_table->find_slot (&entry, INSERT);
886
887 gcc_assert (*slot == NULL);
888 *slot = ggc_alloc<constexpr_fundef> ();
889 **slot = entry;
890
891 return fun;
892}
893
894/* FUN is a non-constexpr function called in a context that requires a
895 constant expression. If it comes from a constexpr template, explain why
896 the instantiation isn't constexpr. */
897
898void
899explain_invalid_constexpr_fn (tree fun)
900{
901 static hash_set<tree> *diagnosed;
902 tree body;
903 location_t save_loc;
33603066 904 /* Only diagnose defaulted functions, lambdas, or instantiations. */
09b42213 905 if (!DECL_DEFAULTED_FN (fun)
33603066 906 && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun))
09b42213 907 && !is_instantiation_of_constexpr (fun))
908 return;
909 if (diagnosed == NULL)
910 diagnosed = new hash_set<tree>;
911 if (diagnosed->add (fun))
912 /* Already explained. */
913 return;
914
915 save_loc = input_location;
33603066 916 if (!lambda_static_thunk_p (fun))
917 {
918 /* Diagnostics should completely ignore the static thunk, so leave
919 input_location set to our caller's location. */
920 input_location = DECL_SOURCE_LOCATION (fun);
921 inform (input_location,
5967b28b 922 "%qD is not usable as a %<constexpr%> function because:", fun);
33603066 923 }
09b42213 924 /* First check the declaration. */
925 if (is_valid_constexpr_fn (fun, true))
926 {
927 /* Then if it's OK, the body. */
33603066 928 if (!DECL_DECLARED_CONSTEXPR_P (fun)
929 && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun)))
09b42213 930 explain_implicit_non_constexpr (fun);
931 else
932 {
933 body = massage_constexpr_body (fun, DECL_SAVED_TREE (fun));
934 require_potential_rvalue_constant_expression (body);
935 if (DECL_CONSTRUCTOR_P (fun))
2f4cab3c 936 cx_check_missing_mem_inits (DECL_CONTEXT (fun), body, true);
09b42213 937 }
938 }
939 input_location = save_loc;
940}
941
942/* Objects of this type represent calls to constexpr functions
943 along with the bindings of parameters to their arguments, for
944 the purpose of compile time evaluation. */
945
946struct GTY((for_user)) constexpr_call {
947 /* Description of the constexpr function definition. */
948 constexpr_fundef *fundef;
949 /* Parameter bindings environment. A TREE_LIST where each TREE_PURPOSE
950 is a parameter _DECL and the TREE_VALUE is the value of the parameter.
951 Note: This arrangement is made to accommodate the use of
952 iterative_hash_template_arg (see pt.c). If you change this
953 representation, also change the hash calculation in
954 cxx_eval_call_expression. */
955 tree bindings;
956 /* Result of the call.
957 NULL means the call is being evaluated.
958 error_mark_node means that the evaluation was erroneous;
959 otherwise, the actuall value of the call. */
960 tree result;
961 /* The hash of this call; we remember it here to avoid having to
962 recalculate it when expanding the hash table. */
963 hashval_t hash;
964};
965
b594087e 966struct constexpr_call_hasher : ggc_ptr_hash<constexpr_call>
09b42213 967{
968 static hashval_t hash (constexpr_call *);
969 static bool equal (constexpr_call *, constexpr_call *);
cf72f34d 970};
971
2a2770c6 972enum constexpr_switch_state {
973 /* Used when processing a switch for the first time by cxx_eval_switch_expr
974 and default: label for that switch has not been seen yet. */
975 css_default_not_seen,
976 /* Used when processing a switch for the first time by cxx_eval_switch_expr
977 and default: label for that switch has been seen already. */
978 css_default_seen,
979 /* Used when processing a switch for the second time by
980 cxx_eval_switch_expr, where default: label should match. */
981 css_default_processing
982};
983
cf72f34d 984/* The constexpr expansion context. CALL is the current function
985 expansion, CTOR is the current aggregate initializer, OBJECT is the
986 object being initialized by CTOR, either a VAR_DECL or a _REF. VALUES
987 is a map of values of variables initialized within the expression. */
988
989struct constexpr_ctx {
f9885b8a 990 /* The innermost call we're evaluating. */
cf72f34d 991 constexpr_call *call;
f9885b8a 992 /* Values for any temporaries or local variables within the
993 constant-expression. */
cf72f34d 994 hash_map<tree,tree> *values;
2631cb67 995 /* SAVE_EXPRs that we've seen within the current LOOP_EXPR. NULL if we
996 aren't inside a loop. */
997 hash_set<tree> *save_exprs;
f9885b8a 998 /* The CONSTRUCTOR we're currently building up for an aggregate
999 initializer. */
cf72f34d 1000 tree ctor;
f9885b8a 1001 /* The object we're building the CONSTRUCTOR for. */
cf72f34d 1002 tree object;
2a2770c6 1003 /* If inside SWITCH_EXPR. */
1004 constexpr_switch_state *css_state;
f9885b8a 1005 /* Whether we should error on a non-constant expression or fail quietly. */
f83e6885 1006 bool quiet;
f9885b8a 1007 /* Whether we are strictly conforming to constant expression rules or
1008 trying harder to get a constant value. */
2055d27a 1009 bool strict;
cf72f34d 1010};
09b42213 1011
1012/* A table of all constexpr calls that have been evaluated by the
1013 compiler in this translation unit. */
1014
a050099a 1015static GTY (()) hash_table<constexpr_call_hasher> *constexpr_call_table;
09b42213 1016
cf72f34d 1017static tree cxx_eval_constant_expression (const constexpr_ctx *, tree,
b2a43197 1018 bool, bool *, bool *, tree * = NULL);
09b42213 1019
1020/* Compute a hash value for a constexpr call representation. */
1021
1022inline hashval_t
1023constexpr_call_hasher::hash (constexpr_call *info)
1024{
1025 return info->hash;
1026}
1027
1028/* Return true if the objects pointed to by P and Q represent calls
1029 to the same constexpr function with the same arguments.
1030 Otherwise, return false. */
1031
1032bool
1033constexpr_call_hasher::equal (constexpr_call *lhs, constexpr_call *rhs)
1034{
1035 tree lhs_bindings;
1036 tree rhs_bindings;
1037 if (lhs == rhs)
c751e2cd 1038 return true;
1039 if (lhs->hash != rhs->hash)
1040 return false;
09b42213 1041 if (!constexpr_fundef_hasher::equal (lhs->fundef, rhs->fundef))
c751e2cd 1042 return false;
09b42213 1043 lhs_bindings = lhs->bindings;
1044 rhs_bindings = rhs->bindings;
1045 while (lhs_bindings != NULL && rhs_bindings != NULL)
1046 {
1047 tree lhs_arg = TREE_VALUE (lhs_bindings);
1048 tree rhs_arg = TREE_VALUE (rhs_bindings);
1049 gcc_assert (TREE_TYPE (lhs_arg) == TREE_TYPE (rhs_arg));
1050 if (!cp_tree_equal (lhs_arg, rhs_arg))
c751e2cd 1051 return false;
09b42213 1052 lhs_bindings = TREE_CHAIN (lhs_bindings);
1053 rhs_bindings = TREE_CHAIN (rhs_bindings);
1054 }
1055 return lhs_bindings == rhs_bindings;
1056}
1057
1058/* Initialize the constexpr call table, if needed. */
1059
1060static void
1061maybe_initialize_constexpr_call_table (void)
1062{
1063 if (constexpr_call_table == NULL)
1064 constexpr_call_table = hash_table<constexpr_call_hasher>::create_ggc (101);
1065}
1066
4f7ebe46 1067/* During constexpr CALL_EXPR evaluation, to avoid issues with sharing when
1068 a function happens to get called recursively, we unshare the callee
1069 function's body and evaluate this unshared copy instead of evaluating the
1070 original body.
1071
1072 FUNDEF_COPIES_TABLE is a per-function freelist of these unshared function
1073 copies. The underlying data structure of FUNDEF_COPIES_TABLE is a hash_map
a050099a 1074 that's keyed off of the original FUNCTION_DECL and whose value is a
1075 TREE_LIST of this function's unused copies awaiting reuse.
4f7ebe46 1076
a050099a 1077 This is not GC-deletable to avoid GC affecting UID generation. */
4f7ebe46 1078
a050099a 1079static GTY(()) hash_map<tree, tree> *fundef_copies_table;
4f7ebe46 1080
1081/* Initialize FUNDEF_COPIES_TABLE if it's not initialized. */
1082
1083static void
1084maybe_initialize_fundef_copies_table ()
1085{
a050099a 1086 if (fundef_copies_table == NULL)
1087 fundef_copies_table = hash_map<tree,tree>::create_ggc (101);
4f7ebe46 1088}
1089
1090/* Reuse a copy or create a new unshared copy of the function FUN.
886d5463 1091 Return this copy. We use a TREE_LIST whose PURPOSE is body, VALUE
1092 is parms, TYPE is result. */
4f7ebe46 1093
a050099a 1094static tree
4f7ebe46 1095get_fundef_copy (tree fun)
1096{
1097 maybe_initialize_fundef_copies_table ();
1098
a050099a 1099 tree copy;
886d5463 1100 bool existed;
1101 tree *slot = &fundef_copies_table->get_or_insert (fun, &existed);
1102
1103 if (!existed)
4f7ebe46 1104 {
886d5463 1105 /* There is no cached function available, or in use. We can use
1106 the function directly. That the slot is now created records
1107 that this function is now in use. */
1108 copy = build_tree_list (DECL_SAVED_TREE (fun), DECL_ARGUMENTS (fun));
1109 TREE_TYPE (copy) = DECL_RESULT (fun);
1110 }
1111 else if (*slot == NULL_TREE)
1112 {
1113 /* We've already used the function itself, so make a copy. */
a050099a 1114 copy = build_tree_list (NULL, NULL);
a050099a 1115 TREE_PURPOSE (copy) = copy_fn (fun, TREE_VALUE (copy), TREE_TYPE (copy));
4f7ebe46 1116 }
1117 else
1118 {
886d5463 1119 /* We have a cached function available. */
4f7ebe46 1120 copy = *slot;
a050099a 1121 *slot = TREE_CHAIN (copy);
4f7ebe46 1122 }
1123
1124 return copy;
1125}
1126
886d5463 1127/* Save the copy COPY of function FUN for later reuse by
1128 get_fundef_copy(). By construction, there will always be an entry
1129 to find. */
4f7ebe46 1130
1131static void
a050099a 1132save_fundef_copy (tree fun, tree copy)
4f7ebe46 1133{
886d5463 1134 tree *slot = fundef_copies_table->get (fun);
a050099a 1135 TREE_CHAIN (copy) = *slot;
4f7ebe46 1136 *slot = copy;
1137}
1138
09b42213 1139/* We have an expression tree T that represents a call, either CALL_EXPR
1140 or AGGR_INIT_EXPR. Return the Nth argument. */
1141
1142static inline tree
1143get_nth_callarg (tree t, int n)
1144{
1145 switch (TREE_CODE (t))
1146 {
1147 case CALL_EXPR:
1148 return CALL_EXPR_ARG (t, n);
1149
1150 case AGGR_INIT_EXPR:
1151 return AGGR_INIT_EXPR_ARG (t, n);
1152
1153 default:
1154 gcc_unreachable ();
1155 return NULL;
1156 }
1157}
1158
09b42213 1159/* Attempt to evaluate T which represents a call to a builtin function.
1160 We assume here that all builtin functions evaluate to scalar types
1161 represented by _CST nodes. */
1162
1163static tree
f1c6af10 1164cxx_eval_builtin_function_call (const constexpr_ctx *ctx, tree t, tree fun,
1227ba74 1165 bool lval,
09b42213 1166 bool *non_constant_p, bool *overflow_p)
1167{
1168 const int nargs = call_expr_nargs (t);
1169 tree *args = (tree *) alloca (nargs * sizeof (tree));
1170 tree new_call;
1171 int i;
f1c6af10 1172
1173 /* Don't fold __builtin_constant_p within a constexpr function. */
efe6a40a 1174 bool bi_const_p = (DECL_FUNCTION_CODE (fun) == BUILT_IN_CONSTANT_P);
1175
0f5cec54 1176 /* If we aren't requiring a constant expression, defer __builtin_constant_p
1177 in a constexpr function until we have values for the parameters. */
efe6a40a 1178 if (bi_const_p
0f5cec54 1179 && ctx->quiet
f1c6af10 1180 && current_function_decl
1181 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
09b42213 1182 {
f1c6af10 1183 *non_constant_p = true;
1184 return t;
09b42213 1185 }
f1c6af10 1186
1187 /* Be permissive for arguments to built-ins; __builtin_constant_p should
1188 return constant false for a non-constant argument. */
1189 constexpr_ctx new_ctx = *ctx;
1190 new_ctx.quiet = true;
1191 bool dummy1 = false, dummy2 = false;
1192 for (i = 0; i < nargs; ++i)
efe6a40a 1193 {
91b66c3c 1194 args[i] = CALL_EXPR_ARG (t, i);
1195 /* If builtin_valid_in_constant_expr_p is true,
1196 potential_constant_expression_1 has not recursed into the arguments
1197 of the builtin, verify it here. */
1198 if (!builtin_valid_in_constant_expr_p (fun)
1199 || potential_constant_expression (args[i]))
1200 args[i] = cxx_eval_constant_expression (&new_ctx, args[i], false,
1201 &dummy1, &dummy2);
efe6a40a 1202 if (bi_const_p)
1203 /* For __built_in_constant_p, fold all expressions with constant values
1204 even if they aren't C++ constant-expressions. */
1205 args[i] = cp_fully_fold (args[i]);
1206 }
f1c6af10 1207
1208 bool save_ffbcp = force_folding_builtin_constant_p;
1209 force_folding_builtin_constant_p = true;
051eb924 1210 new_call = fold_builtin_call_array (EXPR_LOCATION (t), TREE_TYPE (t),
1211 CALL_EXPR_FN (t), nargs, args);
f1c6af10 1212 force_folding_builtin_constant_p = save_ffbcp;
051eb924 1213 if (new_call == NULL)
1214 {
1215 if (!*non_constant_p && !ctx->quiet)
1216 {
2fb20ba2 1217 /* Do not allow__builtin_unreachable in constexpr function.
1218 The __builtin_unreachable call with BUILTINS_LOCATION
1219 comes from cp_maybe_instrument_return. */
1220 if (DECL_FUNCTION_CODE (fun) == BUILT_IN_UNREACHABLE
1221 && EXPR_LOCATION (t) == BUILTINS_LOCATION)
5967b28b 1222 error ("%<constexpr%> call flows off the end of the function");
2fb20ba2 1223 else
1224 {
1225 new_call = build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t),
1226 CALL_EXPR_FN (t), nargs, args);
1227 error ("%q+E is not a constant expression", new_call);
1228 }
051eb924 1229 }
1230 *non_constant_p = true;
1231 return t;
1232 }
1233
7e1f8be4 1234 if (!is_constant_expression (new_call))
051eb924 1235 {
1236 if (!*non_constant_p && !ctx->quiet)
1237 error ("%q+E is not a constant expression", new_call);
1238 *non_constant_p = true;
1239 return t;
1240 }
1241
1242 return cxx_eval_constant_expression (&new_ctx, new_call, lval,
1243 non_constant_p, overflow_p);
09b42213 1244}
1245
1246/* TEMP is the constant value of a temporary object of type TYPE. Adjust
1247 the type of the value to match. */
1248
1249static tree
1250adjust_temp_type (tree type, tree temp)
1251{
1252 if (TREE_TYPE (temp) == type)
1253 return temp;
1254 /* Avoid wrapping an aggregate value in a NOP_EXPR. */
1255 if (TREE_CODE (temp) == CONSTRUCTOR)
1256 return build_constructor (type, CONSTRUCTOR_ELTS (temp));
1257 gcc_assert (scalarish_type_p (type));
1258 return cp_fold_convert (type, temp);
1259}
1260
e283bb4f 1261/* Callback for walk_tree used by unshare_constructor. */
1262
1263static tree
1264find_constructor (tree *tp, int *walk_subtrees, void *)
1265{
1266 if (TYPE_P (*tp))
1267 *walk_subtrees = 0;
1268 if (TREE_CODE (*tp) == CONSTRUCTOR)
1269 return *tp;
1270 return NULL_TREE;
1271}
1272
1273/* If T is a CONSTRUCTOR or an expression that has a CONSTRUCTOR node as a
1274 subexpression, return an unshared copy of T. Otherwise return T. */
1275
1276static tree
1277unshare_constructor (tree t)
1278{
1279 tree ctor = walk_tree (&t, find_constructor, NULL, NULL);
1280 if (ctor != NULL_TREE)
1281 return unshare_expr (t);
1282 return t;
1283}
1284
09b42213 1285/* Subroutine of cxx_eval_call_expression.
1286 We are processing a call expression (either CALL_EXPR or
cf72f34d 1287 AGGR_INIT_EXPR) in the context of CTX. Evaluate
09b42213 1288 all arguments and bind their values to correspondings
1289 parameters, making up the NEW_CALL context. */
1290
1291static void
cf72f34d 1292cxx_bind_parameters_in_call (const constexpr_ctx *ctx, tree t,
09b42213 1293 constexpr_call *new_call,
c8f6aeb1 1294 bool *non_constant_p, bool *overflow_p,
1295 bool *non_constant_args)
09b42213 1296{
1297 const int nargs = call_expr_nargs (t);
1298 tree fun = new_call->fundef->decl;
1299 tree parms = DECL_ARGUMENTS (fun);
1300 int i;
9c96033c 1301 tree *p = &new_call->bindings;
09b42213 1302 for (i = 0; i < nargs; ++i)
1303 {
1304 tree x, arg;
1305 tree type = parms ? TREE_TYPE (parms) : void_type_node;
09b42213 1306 x = get_nth_callarg (t, i);
cf72f34d 1307 /* For member function, the first argument is a pointer to the implied
1308 object. For a constructor, it might still be a dummy object, in
9c96033c 1309 which case we get the real argument from ctx. */
cf72f34d 1310 if (i == 0 && DECL_CONSTRUCTOR_P (fun)
1311 && is_dummy_object (x))
1312 {
1313 x = ctx->object;
c017458d 1314 x = build_address (x);
cf72f34d 1315 }
2e1c71c2 1316 arg = cxx_eval_constant_expression (ctx, x, /*lval=*/false,
b2a43197 1317 non_constant_p, overflow_p);
09b42213 1318 /* Don't VERIFY_CONSTANT here. */
f83e6885 1319 if (*non_constant_p && ctx->quiet)
09b42213 1320 return;
1321 /* Just discard ellipsis args after checking their constantitude. */
1322 if (!parms)
1323 continue;
2ba934c7 1324
1325 if (!*non_constant_p)
1326 {
24d5b4f0 1327 /* Don't share a CONSTRUCTOR that might be changed. */
1328 arg = unshare_constructor (arg);
2ba934c7 1329 /* Make sure the binding has the same type as the parm. But
1330 only for constant args. */
90ad495b 1331 if (!TYPE_REF_P (type))
2ba934c7 1332 arg = adjust_temp_type (type, arg);
1333 if (!TREE_CONSTANT (arg))
1334 *non_constant_args = true;
1335 *p = build_tree_list (parms, arg);
1336 p = &TREE_CHAIN (*p);
1337 }
09b42213 1338 parms = TREE_CHAIN (parms);
1339 }
1340}
1341
1342/* Variables and functions to manage constexpr call expansion context.
1343 These do not need to be marked for PCH or GC. */
1344
1345/* FIXME remember and print actual constant arguments. */
16fb756f 1346static vec<tree> call_stack;
09b42213 1347static int call_stack_tick;
1348static int last_cx_error_tick;
1349
1350static bool
1351push_cx_call_context (tree call)
1352{
1353 ++call_stack_tick;
1354 if (!EXPR_HAS_LOCATION (call))
1355 SET_EXPR_LOCATION (call, input_location);
1356 call_stack.safe_push (call);
1357 if (call_stack.length () > (unsigned) max_constexpr_depth)
1358 return false;
1359 return true;
1360}
1361
1362static void
1363pop_cx_call_context (void)
1364{
1365 ++call_stack_tick;
1366 call_stack.pop ();
1367}
1368
1369vec<tree>
1370cx_error_context (void)
1371{
1372 vec<tree> r = vNULL;
1373 if (call_stack_tick != last_cx_error_tick
1374 && !call_stack.is_empty ())
1375 r = call_stack;
1376 last_cx_error_tick = call_stack_tick;
1377 return r;
1378}
1379
732905bb 1380/* Evaluate a call T to a GCC internal function when possible and return
1381 the evaluated result or, under the control of CTX, give an error, set
1382 NON_CONSTANT_P, and return the unevaluated call T otherwise. */
1383
1384static tree
1385cxx_eval_internal_function (const constexpr_ctx *ctx, tree t,
1386 bool lval,
1387 bool *non_constant_p, bool *overflow_p)
1388{
1389 enum tree_code opcode = ERROR_MARK;
1390
1391 switch (CALL_EXPR_IFN (t))
1392 {
1393 case IFN_UBSAN_NULL:
1394 case IFN_UBSAN_BOUNDS:
1395 case IFN_UBSAN_VPTR:
3c77f69c 1396 case IFN_FALLTHROUGH:
732905bb 1397 return void_node;
1398
1399 case IFN_ADD_OVERFLOW:
1400 opcode = PLUS_EXPR;
1401 break;
1402 case IFN_SUB_OVERFLOW:
1403 opcode = MINUS_EXPR;
1404 break;
1405 case IFN_MUL_OVERFLOW:
1406 opcode = MULT_EXPR;
1407 break;
1408
6e1b2ffb 1409 case IFN_LAUNDER:
1410 return cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0),
1411 false, non_constant_p, overflow_p);
1412
732905bb 1413 default:
1414 if (!ctx->quiet)
d3a3cfb8 1415 error_at (cp_expr_loc_or_loc (t, input_location),
732905bb 1416 "call to internal function %qE", t);
1417 *non_constant_p = true;
1418 return t;
1419 }
1420
1421 /* Evaluate constant arguments using OPCODE and return a complex
1422 number containing the result and the overflow bit. */
1423 tree arg0 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), lval,
1424 non_constant_p, overflow_p);
1425 tree arg1 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 1), lval,
1426 non_constant_p, overflow_p);
1427
1428 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1429 {
d3a3cfb8 1430 location_t loc = cp_expr_loc_or_loc (t, input_location);
732905bb 1431 tree type = TREE_TYPE (TREE_TYPE (t));
1432 tree result = fold_binary_loc (loc, opcode, type,
1433 fold_convert_loc (loc, type, arg0),
1434 fold_convert_loc (loc, type, arg1));
1435 tree ovf
1436 = build_int_cst (type, arith_overflowed_p (opcode, type, arg0, arg1));
1437 /* Reset TREE_OVERFLOW to avoid warnings for the overflow. */
1438 if (TREE_OVERFLOW (result))
1439 TREE_OVERFLOW (result) = 0;
1440
1441 return build_complex (TREE_TYPE (t), result, ovf);
1442 }
1443
1444 *non_constant_p = true;
1445 return t;
1446}
1447
165d0d5b 1448/* Clean CONSTRUCTOR_NO_CLEARING from CTOR and its sub-aggregates. */
79cdb4f5 1449
1450static void
1451clear_no_implicit_zero (tree ctor)
1452{
165d0d5b 1453 if (CONSTRUCTOR_NO_CLEARING (ctor))
79cdb4f5 1454 {
165d0d5b 1455 CONSTRUCTOR_NO_CLEARING (ctor) = false;
79cdb4f5 1456 tree elt; unsigned HOST_WIDE_INT idx;
1457 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), idx, elt)
1458 if (TREE_CODE (elt) == CONSTRUCTOR)
1459 clear_no_implicit_zero (elt);
1460 }
1461}
1462
09b42213 1463/* Subroutine of cxx_eval_constant_expression.
1464 Evaluate the call expression tree T in the context of OLD_CALL expression
1465 evaluation. */
1466
1467static tree
cf72f34d 1468cxx_eval_call_expression (const constexpr_ctx *ctx, tree t,
1227ba74 1469 bool lval,
09b42213 1470 bool *non_constant_p, bool *overflow_p)
1471{
d3a3cfb8 1472 location_t loc = cp_expr_loc_or_loc (t, input_location);
09b42213 1473 tree fun = get_function_named_in_call (t);
09b42213 1474 constexpr_call new_call = { NULL, NULL, NULL, 0 };
09b42213 1475 bool depth_ok;
1476
6b71bdb4 1477 if (fun == NULL_TREE)
732905bb 1478 return cxx_eval_internal_function (ctx, t, lval,
1479 non_constant_p, overflow_p);
6b71bdb4 1480
09b42213 1481 if (TREE_CODE (fun) != FUNCTION_DECL)
1482 {
1483 /* Might be a constexpr function pointer. */
f83e6885 1484 fun = cxx_eval_constant_expression (ctx, fun,
1227ba74 1485 /*lval*/false, non_constant_p,
b2a43197 1486 overflow_p);
09b42213 1487 STRIP_NOPS (fun);
1488 if (TREE_CODE (fun) == ADDR_EXPR)
1489 fun = TREE_OPERAND (fun, 0);
1490 }
1491 if (TREE_CODE (fun) != FUNCTION_DECL)
1492 {
f83e6885 1493 if (!ctx->quiet && !*non_constant_p)
5967b28b 1494 error_at (loc, "expression %qE does not designate a %<constexpr%> "
09b42213 1495 "function", fun);
1496 *non_constant_p = true;
1497 return t;
1498 }
1499 if (DECL_CLONED_FUNCTION_P (fun))
1500 fun = DECL_CLONED_FUNCTION (fun);
6b71bdb4 1501
1502 if (is_ubsan_builtin_p (fun))
1503 return void_node;
1504
09b42213 1505 if (is_builtin_fn (fun))
f1c6af10 1506 return cxx_eval_builtin_function_call (ctx, t, fun,
1227ba74 1507 lval, non_constant_p, overflow_p);
09b42213 1508 if (!DECL_DECLARED_CONSTEXPR_P (fun))
1509 {
f83e6885 1510 if (!ctx->quiet)
09b42213 1511 {
1dcd84cf 1512 if (!lambda_static_thunk_p (fun))
5967b28b 1513 error_at (loc, "call to non-%<constexpr%> function %qD", fun);
09b42213 1514 explain_invalid_constexpr_fn (fun);
1515 }
1516 *non_constant_p = true;
1517 return t;
1518 }
1519
9ebe2ea3 1520 constexpr_ctx new_ctx = *ctx;
1521 if (DECL_CONSTRUCTOR_P (fun) && !ctx->object
1522 && TREE_CODE (t) == AGGR_INIT_EXPR)
1523 {
1524 /* We want to have an initialization target for an AGGR_INIT_EXPR.
1525 If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
1526 new_ctx.object = AGGR_INIT_EXPR_SLOT (t);
1527 tree ctor = new_ctx.ctor = build_constructor (DECL_CONTEXT (fun), NULL);
165d0d5b 1528 CONSTRUCTOR_NO_CLEARING (ctor) = true;
9ebe2ea3 1529 ctx->values->put (new_ctx.object, ctor);
1530 ctx = &new_ctx;
1531 }
1532
09b42213 1533 /* Shortcut trivial constructor/op=. */
1534 if (trivial_fn_p (fun))
1535 {
9ebe2ea3 1536 tree init = NULL_TREE;
09b42213 1537 if (call_expr_nargs (t) == 2)
9ebe2ea3 1538 init = convert_from_reference (get_nth_callarg (t, 1));
09b42213 1539 else if (TREE_CODE (t) == AGGR_INIT_EXPR
1540 && AGGR_INIT_ZERO_FIRST (t))
9ebe2ea3 1541 init = build_zero_init (DECL_CONTEXT (fun), NULL_TREE, false);
1542 if (init)
1543 {
1544 tree op = get_nth_callarg (t, 0);
1545 if (is_dummy_object (op))
1546 op = ctx->object;
1547 else
1548 op = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (op)), op);
1549 tree set = build2 (MODIFY_EXPR, TREE_TYPE (op), op, init);
0ce12360 1550 new_ctx.call = &new_call;
1551 return cxx_eval_constant_expression (&new_ctx, set, lval,
9ebe2ea3 1552 non_constant_p, overflow_p);
1553 }
09b42213 1554 }
1555
8afcf831 1556 /* We can't defer instantiating the function any longer. */
1557 if (!DECL_INITIAL (fun)
1558 && DECL_TEMPLOID_INSTANTIATION (fun))
1559 {
31db86df 1560 location_t save_loc = input_location;
1561 input_location = loc;
8afcf831 1562 ++function_depth;
1563 instantiate_decl (fun, /*defer_ok*/false, /*expl_inst*/false);
1564 --function_depth;
31db86df 1565 input_location = save_loc;
8afcf831 1566 }
1567
09b42213 1568 /* If in direct recursive call, optimize definition search. */
0ce12360 1569 if (ctx && ctx->call && ctx->call->fundef && ctx->call->fundef->decl == fun)
cf72f34d 1570 new_call.fundef = ctx->call->fundef;
09b42213 1571 else
1572 {
1573 new_call.fundef = retrieve_constexpr_fundef (fun);
d2a9b1c1 1574 if (new_call.fundef == NULL || new_call.fundef->body == NULL
1575 || fun == current_function_decl)
09b42213 1576 {
f83e6885 1577 if (!ctx->quiet)
09b42213 1578 {
d2a9b1c1 1579 /* We need to check for current_function_decl here in case we're
1580 being called during cp_fold_function, because at that point
1581 DECL_INITIAL is set properly and we have a fundef but we
1582 haven't lowered invisirefs yet (c++/70344). */
1583 if (DECL_INITIAL (fun) == error_mark_node
1584 || fun == current_function_decl)
a0efa758 1585 error_at (loc, "%qD called in a constant expression before its "
1586 "definition is complete", fun);
1587 else if (DECL_INITIAL (fun))
09b42213 1588 {
33603066 1589 /* The definition of fun was somehow unsuitable. But pretend
1590 that lambda static thunks don't exist. */
1591 if (!lambda_static_thunk_p (fun))
1592 error_at (loc, "%qD called in a constant expression", fun);
09b42213 1593 explain_invalid_constexpr_fn (fun);
1594 }
1595 else
1596 error_at (loc, "%qD used before its definition", fun);
1597 }
1598 *non_constant_p = true;
1599 return t;
1600 }
1601 }
9c96033c 1602
c8f6aeb1 1603 bool non_constant_args = false;
cf72f34d 1604 cxx_bind_parameters_in_call (ctx, t, &new_call,
c8f6aeb1 1605 non_constant_p, overflow_p, &non_constant_args);
09b42213 1606 if (*non_constant_p)
1607 return t;
1608
1609 depth_ok = push_cx_call_context (t);
1610
c8f6aeb1 1611 tree result = NULL_TREE;
09b42213 1612
c8f6aeb1 1613 constexpr_call *entry = NULL;
167a09fb 1614 if (depth_ok && !non_constant_args && ctx->strict)
09b42213 1615 {
c8f6aeb1 1616 new_call.hash = iterative_hash_template_arg
1617 (new_call.bindings, constexpr_fundef_hasher::hash (new_call.fundef));
1618
1619 /* If we have seen this call before, we are done. */
1620 maybe_initialize_constexpr_call_table ();
1621 constexpr_call **slot
1622 = constexpr_call_table->find_slot (&new_call, INSERT);
1623 entry = *slot;
1624 if (entry == NULL)
1625 {
1626 /* We need to keep a pointer to the entry, not just the slot, as the
1627 slot can move in the call to cxx_eval_builtin_function_call. */
1628 *slot = entry = ggc_alloc<constexpr_call> ();
1629 *entry = new_call;
1630 }
2ba934c7 1631 /* Calls that are in progress have their result set to NULL,
c8f6aeb1 1632 so that we can detect circular dependencies. */
1633 else if (entry->result == NULL)
1634 {
1635 if (!ctx->quiet)
1636 error ("call has circular dependency");
1637 *non_constant_p = true;
1638 entry->result = result = error_mark_node;
1639 }
1640 else
1641 result = entry->result;
09b42213 1642 }
1643
1644 if (!depth_ok)
1645 {
f83e6885 1646 if (!ctx->quiet)
5967b28b 1647 error ("%<constexpr%> evaluation depth exceeds maximum of %d (use "
09b42213 1648 "-fconstexpr-depth= to increase the maximum)",
1649 max_constexpr_depth);
1650 *non_constant_p = true;
c8f6aeb1 1651 result = error_mark_node;
09b42213 1652 }
1653 else
1654 {
ab099088 1655 if (result && result != error_mark_node)
1656 /* OK */;
1657 else if (!DECL_SAVED_TREE (fun))
1658 {
1659 /* When at_eof >= 2, cgraph has started throwing away
1660 DECL_SAVED_TREE, so fail quietly. FIXME we get here because of
1661 late code generation for VEC_INIT_EXPR, which needs to be
1662 completely reconsidered. */
1663 gcc_assert (at_eof >= 2 && ctx->quiet);
1664 *non_constant_p = true;
1665 }
1666 else
cf72f34d 1667 {
4f7ebe46 1668 tree body, parms, res;
88a59139 1669
4f7ebe46 1670 /* Reuse or create a new unshared copy of this function's body. */
a050099a 1671 tree copy = get_fundef_copy (fun);
1672 body = TREE_PURPOSE (copy);
1673 parms = TREE_VALUE (copy);
1674 res = TREE_TYPE (copy);
88a59139 1675
1676 /* Associate the bindings with the remapped parms. */
1677 tree bound = new_call.bindings;
1678 tree remapped = parms;
1679 while (bound)
9c96033c 1680 {
88a59139 1681 tree oparm = TREE_PURPOSE (bound);
1682 tree arg = TREE_VALUE (bound);
1683 gcc_assert (DECL_NAME (remapped) == DECL_NAME (oparm));
5898f0d7 1684 /* Don't share a CONSTRUCTOR that might be changed. */
e283bb4f 1685 arg = unshare_constructor (arg);
88a59139 1686 ctx->values->put (remapped, arg);
1687 bound = TREE_CHAIN (bound);
1688 remapped = DECL_CHAIN (remapped);
9c96033c 1689 }
88a59139 1690 /* Add the RESULT_DECL to the values map, too. */
1691 tree slot = NULL_TREE;
1692 if (DECL_BY_REFERENCE (res))
9c96033c 1693 {
88a59139 1694 slot = AGGR_INIT_EXPR_SLOT (t);
1695 tree addr = build_address (slot);
1696 addr = build_nop (TREE_TYPE (res), addr);
1697 ctx->values->put (res, addr);
1698 ctx->values->put (slot, NULL_TREE);
1699 }
1700 else
1701 ctx->values->put (res, NULL_TREE);
9c96033c 1702
4f7ebe46 1703 /* Track the callee's evaluated SAVE_EXPRs so that we can forget
1704 their values after the call. */
1705 constexpr_ctx ctx_with_save_exprs = *ctx;
1706 hash_set<tree> save_exprs;
1707 ctx_with_save_exprs.save_exprs = &save_exprs;
43b7271d 1708 ctx_with_save_exprs.call = &new_call;
4f7ebe46 1709
88a59139 1710 tree jump_target = NULL_TREE;
4f7ebe46 1711 cxx_eval_constant_expression (&ctx_with_save_exprs, body,
88a59139 1712 lval, non_constant_p, overflow_p,
1713 &jump_target);
9c96033c 1714
88a59139 1715 if (DECL_CONSTRUCTOR_P (fun))
1716 /* This can be null for a subobject constructor call, in
1717 which case what we care about is the initialization
1718 side-effects rather than the value. We could get at the
1719 value by evaluating *this, but we don't bother; there's
1720 no need to put such a call in the hash table. */
1721 result = lval ? ctx->object : ctx->ctor;
1722 else if (VOID_TYPE_P (TREE_TYPE (res)))
1723 result = void_node;
1724 else
1725 {
1726 result = *ctx->values->get (slot ? slot : res);
1727 if (result == NULL_TREE && !*non_constant_p)
9c96033c 1728 {
88a59139 1729 if (!ctx->quiet)
5967b28b 1730 error ("%<constexpr%> call flows off the end "
88a59139 1731 "of the function");
1732 *non_constant_p = true;
9c96033c 1733 }
9c96033c 1734 }
88a59139 1735
4f7ebe46 1736 /* Forget the saved values of the callee's SAVE_EXPRs. */
1737 for (hash_set<tree>::iterator iter = save_exprs.begin();
1738 iter != save_exprs.end(); ++iter)
1739 ctx_with_save_exprs.values->remove (*iter);
1740
88a59139 1741 /* Remove the parms/result from the values map. Is it worth
1742 bothering to do this when the map itself is only live for
1743 one constexpr evaluation? If so, maybe also clear out
1744 other vars from call, maybe in BIND_EXPR handling? */
1745 ctx->values->remove (res);
1746 if (slot)
1747 ctx->values->remove (slot);
1748 for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
1749 ctx->values->remove (parm);
4f7ebe46 1750
1751 /* Make the unshared function copy we used available for re-use. */
1752 save_fundef_copy (fun, copy);
cf72f34d 1753 }
9c96033c 1754
09b42213 1755 if (result == error_mark_node)
1756 *non_constant_p = true;
c3f376bf 1757 if (*non_constant_p || *overflow_p)
c8f6aeb1 1758 result = error_mark_node;
88a59139 1759 else if (!result)
9c96033c 1760 result = void_node;
c8f6aeb1 1761 if (entry)
1762 entry->result = result;
09b42213 1763 }
1764
c6d97004 1765 /* The result of a constexpr function must be completely initialized. */
1766 if (TREE_CODE (result) == CONSTRUCTOR)
79cdb4f5 1767 clear_no_implicit_zero (result);
c6d97004 1768
09b42213 1769 pop_cx_call_context ();
e283bb4f 1770 return unshare_constructor (result);
09b42213 1771}
1772
1773/* FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
1774
1775bool
1776reduced_constant_expression_p (tree t)
1777{
a34934c2 1778 if (t == NULL_TREE)
1779 return false;
1780
09b42213 1781 switch (TREE_CODE (t))
1782 {
1783 case PTRMEM_CST:
1784 /* Even if we can't lower this yet, it's constant. */
1785 return true;
1786
1787 case CONSTRUCTOR:
1788 /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
56da7fe6 1789 tree idx, val, field; unsigned HOST_WIDE_INT i;
165d0d5b 1790 if (CONSTRUCTOR_NO_CLEARING (t))
c4210852 1791 {
1792 if (TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
1793 /* An initialized vector would have a VECTOR_CST. */
1794 return false;
1795 else
1796 field = next_initializable_field (TYPE_FIELDS (TREE_TYPE (t)));
1797 }
56da7fe6 1798 else
1799 field = NULL_TREE;
1800 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), i, idx, val)
ce4ffd9e 1801 {
a34934c2 1802 /* If VAL is null, we're in the middle of initializing this
1803 element. */
56da7fe6 1804 if (!reduced_constant_expression_p (val))
ce4ffd9e 1805 return false;
56da7fe6 1806 if (field)
1807 {
1808 if (idx != field)
1809 return false;
1810 field = next_initializable_field (DECL_CHAIN (field));
1811 }
ce4ffd9e 1812 }
56da7fe6 1813 if (field)
1814 return false;
165d0d5b 1815 else if (CONSTRUCTOR_NO_CLEARING (t))
56da7fe6 1816 /* All the fields are initialized. */
165d0d5b 1817 CONSTRUCTOR_NO_CLEARING (t) = false;
09b42213 1818 return true;
1819
1820 default:
1821 /* FIXME are we calling this too much? */
1822 return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE;
1823 }
1824}
1825
1826/* Some expressions may have constant operands but are not constant
941fafa5 1827 themselves, such as 1/0. Call this function to check for that
1828 condition.
09b42213 1829
1830 We only call this in places that require an arithmetic constant, not in
1831 places where we might have a non-constant expression that can be a
1832 component of a constant expression, such as the address of a constexpr
1833 variable that might be dereferenced later. */
1834
1835static bool
1836verify_constant (tree t, bool allow_non_constant, bool *non_constant_p,
1837 bool *overflow_p)
1838{
1839 if (!*non_constant_p && !reduced_constant_expression_p (t))
1840 {
1841 if (!allow_non_constant)
1842 error ("%q+E is not a constant expression", t);
1843 *non_constant_p = true;
1844 }
1845 if (TREE_OVERFLOW_P (t))
1846 {
1847 if (!allow_non_constant)
1848 {
1849 permerror (input_location, "overflow in constant expression");
1850 /* If we're being permissive (and are in an enforcing
1851 context), ignore the overflow. */
1852 if (flag_permissive)
1853 return *non_constant_p;
1854 }
1855 *overflow_p = true;
1856 }
1857 return *non_constant_p;
1858}
1859
2b035ffe 1860/* Check whether the shift operation with code CODE and type TYPE on LHS
1861 and RHS is undefined. If it is, give an error with an explanation,
1862 and return true; return false otherwise. */
1863
1864static bool
1865cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx,
1866 enum tree_code code, tree type, tree lhs, tree rhs)
1867{
1868 if ((code != LSHIFT_EXPR && code != RSHIFT_EXPR)
1869 || TREE_CODE (lhs) != INTEGER_CST
1870 || TREE_CODE (rhs) != INTEGER_CST)
1871 return false;
1872
1873 tree lhstype = TREE_TYPE (lhs);
1874 unsigned HOST_WIDE_INT uprec = TYPE_PRECISION (TREE_TYPE (lhs));
1875
1876 /* [expr.shift] The behavior is undefined if the right operand
1877 is negative, or greater than or equal to the length in bits
1878 of the promoted left operand. */
1879 if (tree_int_cst_sgn (rhs) == -1)
1880 {
1881 if (!ctx->quiet)
91d87e22 1882 permerror (loc, "right operand of shift expression %q+E is negative",
1883 build2_loc (loc, code, type, lhs, rhs));
1884 return (!flag_permissive || ctx->quiet);
2b035ffe 1885 }
1886 if (compare_tree_int (rhs, uprec) >= 0)
1887 {
1888 if (!ctx->quiet)
91d87e22 1889 permerror (loc, "right operand of shift expression %q+E is >= than "
1890 "the precision of the left operand",
1891 build2_loc (loc, code, type, lhs, rhs));
1892 return (!flag_permissive || ctx->quiet);
2b035ffe 1893 }
1894
1895 /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
1896 if E1 has a signed type and non-negative value, and E1x2^E2 is
1897 representable in the corresponding unsigned type of the result type,
1898 then that value, converted to the result type, is the resulting value;
1899 otherwise, the behavior is undefined. */
1900 if (code == LSHIFT_EXPR && !TYPE_UNSIGNED (lhstype)
1901 && (cxx_dialect >= cxx11))
1902 {
1903 if (tree_int_cst_sgn (lhs) == -1)
1904 {
1905 if (!ctx->quiet)
91d87e22 1906 permerror (loc,
1907 "left operand of shift expression %q+E is negative",
1908 build2_loc (loc, code, type, lhs, rhs));
1909 return (!flag_permissive || ctx->quiet);
2b035ffe 1910 }
1911 /* For signed x << y the following:
1912 (unsigned) x >> ((prec (lhs) - 1) - y)
1913 if > 1, is undefined. The right-hand side of this formula
1914 is the highest bit of the LHS that can be set (starting from 0),
1915 so that the shift doesn't overflow. We then right-shift the LHS
1916 to see whether any other bit is set making the original shift
1917 undefined -- the result is not representable in the corresponding
1918 unsigned type. */
1919 tree t = build_int_cst (unsigned_type_node, uprec - 1);
1920 t = fold_build2 (MINUS_EXPR, unsigned_type_node, t, rhs);
1921 tree ulhs = fold_convert (unsigned_type_for (lhstype), lhs);
1922 t = fold_build2 (RSHIFT_EXPR, TREE_TYPE (ulhs), ulhs, t);
1923 if (tree_int_cst_lt (integer_one_node, t))
1924 {
1925 if (!ctx->quiet)
91d87e22 1926 permerror (loc, "shift expression %q+E overflows",
1927 build2_loc (loc, code, type, lhs, rhs));
1928 return (!flag_permissive || ctx->quiet);
2b035ffe 1929 }
1930 }
1931 return false;
1932}
1933
09b42213 1934/* Subroutine of cxx_eval_constant_expression.
1935 Attempt to reduce the unary expression tree T to a compile time value.
1936 If successful, return the value. Otherwise issue a diagnostic
1937 and return error_mark_node. */
1938
1939static tree
cf72f34d 1940cxx_eval_unary_expression (const constexpr_ctx *ctx, tree t,
c8f6aeb1 1941 bool /*lval*/,
09b42213 1942 bool *non_constant_p, bool *overflow_p)
1943{
1944 tree r;
1945 tree orig_arg = TREE_OPERAND (t, 0);
c8f6aeb1 1946 tree arg = cxx_eval_constant_expression (ctx, orig_arg, /*lval*/false,
1947 non_constant_p, overflow_p);
09b42213 1948 VERIFY_CONSTANT (arg);
93add516 1949 location_t loc = EXPR_LOCATION (t);
1950 enum tree_code code = TREE_CODE (t);
1951 tree type = TREE_TYPE (t);
1952 r = fold_unary_loc (loc, code, type, arg);
1953 if (r == NULL_TREE)
1954 {
1955 if (arg == orig_arg)
1956 r = t;
1957 else
1958 r = build1_loc (loc, code, type, arg);
1959 }
09b42213 1960 VERIFY_CONSTANT (r);
1961 return r;
1962}
1963
46fad8d5 1964/* Helper function for cxx_eval_binary_expression. Try to optimize
1965 original POINTER_PLUS_EXPR T, LHS p+ RHS, return NULL_TREE if the
1966 generic folding should be used. */
1967
1968static tree
1969cxx_fold_pointer_plus_expression (const constexpr_ctx *ctx, tree t,
1970 tree lhs, tree rhs, bool *non_constant_p,
1971 bool *overflow_p)
1972{
1973 STRIP_NOPS (lhs);
1974 if (TREE_CODE (lhs) != ADDR_EXPR)
1975 return NULL_TREE;
1976
1977 lhs = TREE_OPERAND (lhs, 0);
1978
1979 /* &A[i] p+ j => &A[i + j] */
1980 if (TREE_CODE (lhs) == ARRAY_REF
1981 && TREE_CODE (TREE_OPERAND (lhs, 1)) == INTEGER_CST
1982 && TREE_CODE (rhs) == INTEGER_CST
1983 && TYPE_SIZE_UNIT (TREE_TYPE (lhs))
1984 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs))) == INTEGER_CST)
1985 {
1986 tree orig_type = TREE_TYPE (t);
1987 location_t loc = EXPR_LOCATION (t);
1988 tree type = TREE_TYPE (lhs);
1989
1990 t = fold_convert_loc (loc, ssizetype, TREE_OPERAND (lhs, 1));
1991 tree nelts = array_type_nelts_top (TREE_TYPE (TREE_OPERAND (lhs, 0)));
1992 nelts = cxx_eval_constant_expression (ctx, nelts, false, non_constant_p,
1993 overflow_p);
1994 if (*non_constant_p)
1995 return NULL_TREE;
1996 /* Don't fold an out-of-bound access. */
1997 if (!tree_int_cst_le (t, nelts))
1998 return NULL_TREE;
1999 rhs = cp_fold_convert (ssizetype, rhs);
2000 /* Don't fold if rhs can't be divided exactly by TYPE_SIZE_UNIT.
2001 constexpr int A[1]; ... (char *)&A[0] + 1 */
2002 if (!integer_zerop (fold_build2_loc (loc, TRUNC_MOD_EXPR, sizetype,
2003 rhs, TYPE_SIZE_UNIT (type))))
2004 return NULL_TREE;
2005 /* Make sure to treat the second operand of POINTER_PLUS_EXPR
2006 as signed. */
2007 rhs = fold_build2_loc (loc, EXACT_DIV_EXPR, ssizetype, rhs,
2008 TYPE_SIZE_UNIT (type));
2009 t = size_binop_loc (loc, PLUS_EXPR, rhs, t);
2010 t = build4_loc (loc, ARRAY_REF, type, TREE_OPERAND (lhs, 0),
2011 t, NULL_TREE, NULL_TREE);
2012 t = cp_build_addr_expr (t, tf_warning_or_error);
2013 t = cp_fold_convert (orig_type, t);
2014 return cxx_eval_constant_expression (ctx, t, /*lval*/false,
2015 non_constant_p, overflow_p);
2016 }
2017
2018 return NULL_TREE;
2019}
2020
09b42213 2021/* Subroutine of cxx_eval_constant_expression.
2022 Like cxx_eval_unary_expression, except for binary expressions. */
2023
2024static tree
cf72f34d 2025cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t,
c8f6aeb1 2026 bool /*lval*/,
09b42213 2027 bool *non_constant_p, bool *overflow_p)
2028{
f7ef5392 2029 tree r = NULL_TREE;
09b42213 2030 tree orig_lhs = TREE_OPERAND (t, 0);
2031 tree orig_rhs = TREE_OPERAND (t, 1);
2032 tree lhs, rhs;
c8f6aeb1 2033 lhs = cxx_eval_constant_expression (ctx, orig_lhs, /*lval*/false,
b2a43197 2034 non_constant_p, overflow_p);
6d6737d9 2035 /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer
2036 subtraction. */
2037 if (*non_constant_p)
2038 return t;
c8f6aeb1 2039 rhs = cxx_eval_constant_expression (ctx, orig_rhs, /*lval*/false,
b2a43197 2040 non_constant_p, overflow_p);
6d6737d9 2041 if (*non_constant_p)
2042 return t;
93add516 2043
2044 location_t loc = EXPR_LOCATION (t);
2045 enum tree_code code = TREE_CODE (t);
2046 tree type = TREE_TYPE (t);
f7ef5392 2047
2048 if (code == EQ_EXPR || code == NE_EXPR)
2049 {
2050 bool is_code_eq = (code == EQ_EXPR);
2051
2052 if (TREE_CODE (lhs) == PTRMEM_CST
2053 && TREE_CODE (rhs) == PTRMEM_CST)
e9f29fe5 2054 {
2055 tree lmem = PTRMEM_CST_MEMBER (lhs);
2056 tree rmem = PTRMEM_CST_MEMBER (rhs);
2057 bool eq;
2058 if (TREE_CODE (lmem) == TREE_CODE (rmem)
2059 && TREE_CODE (lmem) == FIELD_DECL
2060 && TREE_CODE (DECL_CONTEXT (lmem)) == UNION_TYPE
2061 && same_type_p (DECL_CONTEXT (lmem),
2062 DECL_CONTEXT (rmem)))
2063 /* If both refer to (possibly different) members of the same union
2064 (12.3), they compare equal. */
2065 eq = true;
2066 else
2067 eq = cp_tree_equal (lhs, rhs);
2068 r = constant_boolean_node (eq == is_code_eq, type);
2069 }
f7ef5392 2070 else if ((TREE_CODE (lhs) == PTRMEM_CST
2071 || TREE_CODE (rhs) == PTRMEM_CST)
2072 && (null_member_pointer_value_p (lhs)
2073 || null_member_pointer_value_p (rhs)))
2074 r = constant_boolean_node (!is_code_eq, type);
f31f4e9c 2075 else if (TREE_CODE (lhs) == PTRMEM_CST)
2076 lhs = cplus_expand_constant (lhs);
2077 else if (TREE_CODE (rhs) == PTRMEM_CST)
2078 rhs = cplus_expand_constant (rhs);
f7ef5392 2079 }
cb768824 2080 if (code == POINTER_PLUS_EXPR && !*non_constant_p
2081 && integer_zerop (lhs) && !integer_zerop (rhs))
2082 {
2083 if (!ctx->quiet)
2084 error ("arithmetic involving a null pointer in %qE", lhs);
2b1749c4 2085 *non_constant_p = true;
cb768824 2086 return t;
2087 }
46fad8d5 2088 else if (code == POINTER_PLUS_EXPR)
2089 r = cxx_fold_pointer_plus_expression (ctx, t, lhs, rhs, non_constant_p,
2090 overflow_p);
f7ef5392 2091
2092 if (r == NULL_TREE)
2093 r = fold_binary_loc (loc, code, type, lhs, rhs);
2094
93add516 2095 if (r == NULL_TREE)
2096 {
2097 if (lhs == orig_lhs && rhs == orig_rhs)
2098 r = t;
2099 else
2100 r = build2_loc (loc, code, type, lhs, rhs);
2101 }
2b035ffe 2102 else if (cxx_eval_check_shift_p (loc, ctx, code, type, lhs, rhs))
2103 *non_constant_p = true;
6d6737d9 2104 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
2105 a local array in a constexpr function. */
d03fa520 2106 bool ptr = INDIRECT_TYPE_P (TREE_TYPE (lhs));
35f2c28f 2107 if (!ptr)
aae7465c 2108 VERIFY_CONSTANT (r);
09b42213 2109 return r;
2110}
2111
2112/* Subroutine of cxx_eval_constant_expression.
2113 Attempt to evaluate condition expressions. Dead branches are not
2114 looked into. */
2115
2116static tree
cf72f34d 2117cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t,
1227ba74 2118 bool lval,
00f21715 2119 bool *non_constant_p, bool *overflow_p,
2120 tree *jump_target)
09b42213 2121{
cf72f34d 2122 tree val = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
c8f6aeb1 2123 /*lval*/false,
b2a43197 2124 non_constant_p, overflow_p);
09b42213 2125 VERIFY_CONSTANT (val);
2126 /* Don't VERIFY_CONSTANT the other operands. */
2127 if (integer_zerop (val))
cf72f34d 2128 return cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2),
1227ba74 2129 lval,
00f21715 2130 non_constant_p, overflow_p,
2131 jump_target);
cf72f34d 2132 return cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
1227ba74 2133 lval,
00f21715 2134 non_constant_p, overflow_p,
2135 jump_target);
09b42213 2136}
2137
31595caf 2138/* Subroutine of cxx_eval_constant_expression.
2139 Attempt to evaluate vector condition expressions. Unlike
2140 cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal
2141 ternary arithmetics operation, where all 3 arguments have to be
2142 evaluated as constants and then folding computes the result from
2143 them. */
2144
2145static tree
2146cxx_eval_vector_conditional_expression (const constexpr_ctx *ctx, tree t,
2147 bool *non_constant_p, bool *overflow_p)
2148{
2149 tree arg1 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
2150 /*lval*/false,
2151 non_constant_p, overflow_p);
2152 VERIFY_CONSTANT (arg1);
2153 tree arg2 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
2154 /*lval*/false,
2155 non_constant_p, overflow_p);
2156 VERIFY_CONSTANT (arg2);
2157 tree arg3 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2),
2158 /*lval*/false,
2159 non_constant_p, overflow_p);
2160 VERIFY_CONSTANT (arg3);
2161 location_t loc = EXPR_LOCATION (t);
2162 tree type = TREE_TYPE (t);
2163 tree r = fold_ternary_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
2164 if (r == NULL_TREE)
2165 {
2166 if (arg1 == TREE_OPERAND (t, 0)
2167 && arg2 == TREE_OPERAND (t, 1)
2168 && arg3 == TREE_OPERAND (t, 2))
2169 r = t;
2170 else
2171 r = build3_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
2172 }
2173 VERIFY_CONSTANT (r);
2174 return r;
2175}
2176
7963b19d 2177/* Returns less than, equal to, or greater than zero if KEY is found to be
2178 less than, to match, or to be greater than the constructor_elt's INDEX. */
2179
2180static int
2181array_index_cmp (tree key, tree index)
2182{
2183 gcc_assert (TREE_CODE (key) == INTEGER_CST);
2184
2185 switch (TREE_CODE (index))
2186 {
2187 case INTEGER_CST:
2188 return tree_int_cst_compare (key, index);
2189 case RANGE_EXPR:
2190 {
2191 tree lo = TREE_OPERAND (index, 0);
2192 tree hi = TREE_OPERAND (index, 1);
2193 if (tree_int_cst_lt (key, lo))
2194 return -1;
2195 else if (tree_int_cst_lt (hi, key))
2196 return 1;
2197 else
2198 return 0;
2199 }
2200 default:
2201 gcc_unreachable ();
2202 }
2203}
2204
2205/* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1
2206 if none. If INSERT is true, insert a matching element rather than fail. */
2207
2208static HOST_WIDE_INT
2209find_array_ctor_elt (tree ary, tree dindex, bool insert = false)
2210{
2211 if (tree_int_cst_sgn (dindex) < 0)
2212 return -1;
2213
2214 unsigned HOST_WIDE_INT i = tree_to_uhwi (dindex);
2215 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ary);
2216 unsigned HOST_WIDE_INT len = vec_safe_length (elts);
2217
2218 unsigned HOST_WIDE_INT end = len;
2219 unsigned HOST_WIDE_INT begin = 0;
2220
2221 /* If the last element of the CONSTRUCTOR has its own index, we can assume
2222 that the same is true of the other elements and index directly. */
2223 if (end > 0)
2224 {
e99466d6 2225 tree cindex = (*elts)[end - 1].index;
7963b19d 2226 if (TREE_CODE (cindex) == INTEGER_CST
e99466d6 2227 && compare_tree_int (cindex, end - 1) == 0)
7963b19d 2228 {
2229 if (i < end)
2230 return i;
2231 else
2232 begin = end;
2233 }
2234 }
2235
2236 /* Otherwise, find a matching index by means of a binary search. */
2237 while (begin != end)
2238 {
2239 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
f0b916c7 2240 constructor_elt &elt = (*elts)[middle];
2241 tree idx = elt.index;
7963b19d 2242
f0b916c7 2243 int cmp = array_index_cmp (dindex, idx);
7963b19d 2244 if (cmp < 0)
2245 end = middle;
2246 else if (cmp > 0)
2247 begin = middle + 1;
2248 else
f0b916c7 2249 {
2250 if (insert && TREE_CODE (idx) == RANGE_EXPR)
2251 {
2252 /* We need to split the range. */
2253 constructor_elt e;
2254 tree lo = TREE_OPERAND (idx, 0);
2255 tree hi = TREE_OPERAND (idx, 1);
e99466d6 2256 tree value = elt.value;
2257 dindex = fold_convert (sizetype, dindex);
f0b916c7 2258 if (tree_int_cst_lt (lo, dindex))
2259 {
2260 /* There are still some lower elts; shorten the range. */
2261 tree new_hi = int_const_binop (MINUS_EXPR, dindex,
2262 size_one_node);
2263 if (tree_int_cst_equal (lo, new_hi))
2264 /* Only one element left, no longer a range. */
2265 elt.index = lo;
2266 else
2267 TREE_OPERAND (idx, 1) = new_hi;
2268 /* Append the element we want to insert. */
2269 ++middle;
2270 e.index = dindex;
e99466d6 2271 e.value = unshare_constructor (value);
f0b916c7 2272 vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e);
2273 }
2274 else
2275 /* No lower elts, the range elt is now ours. */
2276 elt.index = dindex;
2277
2278 if (tree_int_cst_lt (dindex, hi))
2279 {
2280 /* There are still some higher elts; append a range. */
2281 tree new_lo = int_const_binop (PLUS_EXPR, dindex,
2282 size_one_node);
2283 if (tree_int_cst_equal (new_lo, hi))
2284 e.index = hi;
2285 else
2286 e.index = build2 (RANGE_EXPR, sizetype, new_lo, hi);
e99466d6 2287 e.value = unshare_constructor (value);
2288 vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e);
f0b916c7 2289 }
2290 }
2291 return middle;
2292 }
7963b19d 2293 }
2294
2295 if (insert)
2296 {
2297 constructor_elt e = { dindex, NULL_TREE };
2298 vec_safe_insert (CONSTRUCTOR_ELTS (ary), end, e);
2299 return end;
2300 }
2301
2302 return -1;
2303}
2304
40df9099 2305/* Under the control of CTX, issue a detailed diagnostic for
2306 an out-of-bounds subscript INDEX into the expression ARRAY. */
2307
2308static void
2309diag_array_subscript (const constexpr_ctx *ctx, tree array, tree index)
2310{
2311 if (!ctx->quiet)
2312 {
2313 tree arraytype = TREE_TYPE (array);
2314
2315 /* Convert the unsigned array subscript to a signed integer to avoid
2316 printing huge numbers for small negative values. */
2317 tree sidx = fold_convert (ssizetype, index);
2318 if (DECL_P (array))
2319 {
d7d265db 2320 if (TYPE_DOMAIN (arraytype))
2321 error ("array subscript value %qE is outside the bounds "
2322 "of array %qD of type %qT", sidx, array, arraytype);
2323 else
2324 error ("non-zero array subscript %qE is used with array %qD of "
2325 "type %qT with unknown bounds", sidx, array, arraytype);
40df9099 2326 inform (DECL_SOURCE_LOCATION (array), "declared here");
2327 }
d7d265db 2328 else if (TYPE_DOMAIN (arraytype))
40df9099 2329 error ("array subscript value %qE is outside the bounds "
2330 "of array type %qT", sidx, arraytype);
d7d265db 2331 else
2332 error ("non-zero array subscript %qE is used with array of type %qT "
2333 "with unknown bounds", sidx, arraytype);
40df9099 2334 }
2335}
7963b19d 2336
51f0c7e2 2337/* Return the number of elements for TYPE (which is an ARRAY_TYPE or
2338 a VECTOR_TYPE). */
2339
2340static tree
2341get_array_or_vector_nelts (const constexpr_ctx *ctx, tree type,
2342 bool *non_constant_p, bool *overflow_p)
2343{
2344 tree nelts;
2345 if (TREE_CODE (type) == ARRAY_TYPE)
2346 {
2347 if (TYPE_DOMAIN (type))
2348 nelts = array_type_nelts_top (type);
2349 else
2350 nelts = size_zero_node;
2351 }
2352 else if (VECTOR_TYPE_P (type))
2353 nelts = size_int (TYPE_VECTOR_SUBPARTS (type));
2354 else
2355 gcc_unreachable ();
2356
2357 /* For VLAs, the number of elements won't be an integer constant. */
2358 nelts = cxx_eval_constant_expression (ctx, nelts, false,
2359 non_constant_p, overflow_p);
2360 return nelts;
2361}
2362
92fe8840 2363/* Extract element INDEX consisting of CHARS_PER_ELT chars from
2364 STRING_CST STRING. */
2365
2366static tree
2367extract_string_elt (tree string, unsigned chars_per_elt, unsigned index)
2368{
2369 tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (string)));
2370 tree r;
2371
2372 if (chars_per_elt == 1)
2373 r = build_int_cst (type, TREE_STRING_POINTER (string)[index]);
2374 else
2375 {
2376 const unsigned char *ptr
2377 = ((const unsigned char *)TREE_STRING_POINTER (string)
2378 + index * chars_per_elt);
2379 r = native_interpret_expr (type, ptr, chars_per_elt);
2380 }
2381 return r;
2382}
2383
09b42213 2384/* Subroutine of cxx_eval_constant_expression.
2385 Attempt to reduce a reference to an array slot. */
2386
2387static tree
cf72f34d 2388cxx_eval_array_reference (const constexpr_ctx *ctx, tree t,
1227ba74 2389 bool lval,
09b42213 2390 bool *non_constant_p, bool *overflow_p)
2391{
2392 tree oldary = TREE_OPERAND (t, 0);
cf72f34d 2393 tree ary = cxx_eval_constant_expression (ctx, oldary,
1227ba74 2394 lval,
b2a43197 2395 non_constant_p, overflow_p);
09b42213 2396 tree index, oldidx;
cf3cefc9 2397 HOST_WIDE_INT i = 0;
2398 tree elem_type = NULL_TREE;
2399 unsigned len = 0, elem_nchars = 1;
09b42213 2400 if (*non_constant_p)
2401 return t;
2402 oldidx = TREE_OPERAND (t, 1);
cf72f34d 2403 index = cxx_eval_constant_expression (ctx, oldidx,
f83e6885 2404 false,
b2a43197 2405 non_constant_p, overflow_p);
09b42213 2406 VERIFY_CONSTANT (index);
cf3cefc9 2407 if (!lval)
09b42213 2408 {
cf3cefc9 2409 elem_type = TREE_TYPE (TREE_TYPE (ary));
2410 if (TREE_CODE (ary) == VIEW_CONVERT_EXPR
2411 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary, 0)))
2412 && TREE_TYPE (t) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary, 0))))
2413 ary = TREE_OPERAND (ary, 0);
2414 if (TREE_CODE (ary) == CONSTRUCTOR)
2415 len = CONSTRUCTOR_NELTS (ary);
2416 else if (TREE_CODE (ary) == STRING_CST)
2417 {
2418 elem_nchars = (TYPE_PRECISION (elem_type)
2419 / TYPE_PRECISION (char_type_node));
2420 len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars;
2421 }
2422 else if (TREE_CODE (ary) == VECTOR_CST)
f08ee65f 2423 /* We don't create variable-length VECTOR_CSTs. */
2424 len = VECTOR_CST_NELTS (ary).to_constant ();
cf3cefc9 2425 else
2426 {
2427 /* We can't do anything with other tree codes, so use
2428 VERIFY_CONSTANT to complain and fail. */
2429 VERIFY_CONSTANT (ary);
2430 gcc_unreachable ();
2431 }
aed8dc7f 2432
cf3cefc9 2433 if (!tree_fits_shwi_p (index)
2434 || (i = tree_to_shwi (index)) < 0)
2435 {
2436 diag_array_subscript (ctx, ary, index);
2437 *non_constant_p = true;
2438 return t;
2439 }
7963b19d 2440 }
2441
51f0c7e2 2442 tree nelts = get_array_or_vector_nelts (ctx, TREE_TYPE (ary), non_constant_p,
2443 overflow_p);
ce6a6978 2444 VERIFY_CONSTANT (nelts);
0acc6340 2445 if ((lval
2446 ? !tree_int_cst_le (index, nelts)
2447 : !tree_int_cst_lt (index, nelts))
2448 || tree_int_cst_sgn (index) < 0)
ce6a6978 2449 {
40df9099 2450 diag_array_subscript (ctx, ary, index);
ce6a6978 2451 *non_constant_p = true;
2452 return t;
2453 }
2454
cf3cefc9 2455 if (lval && ary == oldary && index == oldidx)
2456 return t;
2457 else if (lval)
2458 return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL);
2459
7963b19d 2460 bool found;
2461 if (TREE_CODE (ary) == CONSTRUCTOR)
2462 {
2463 HOST_WIDE_INT ix = find_array_ctor_elt (ary, index);
2464 found = (ix >= 0);
2465 if (found)
2466 i = ix;
aed8dc7f 2467 }
7963b19d 2468 else
2469 found = (i < len);
aed8dc7f 2470
14869f28 2471 if (found)
2472 {
2473 tree r;
2474 if (TREE_CODE (ary) == CONSTRUCTOR)
2475 r = (*CONSTRUCTOR_ELTS (ary))[i].value;
2476 else if (TREE_CODE (ary) == VECTOR_CST)
2477 r = VECTOR_CST_ELT (ary, i);
14869f28 2478 else
92fe8840 2479 r = extract_string_elt (ary, elem_nchars, i);
2480
14869f28 2481 if (r)
2482 /* Don't VERIFY_CONSTANT here. */
2483 return r;
09b42213 2484
14869f28 2485 /* Otherwise the element doesn't have a value yet. */
09b42213 2486 }
aed8dc7f 2487
14869f28 2488 /* Not found. */
2489
2490 if (TREE_CODE (ary) == CONSTRUCTOR
165d0d5b 2491 && CONSTRUCTOR_NO_CLEARING (ary))
09b42213 2492 {
14869f28 2493 /* 'ary' is part of the aggregate initializer we're currently
2494 building; if there's no initializer for this element yet,
2495 that's an error. */
2496 if (!ctx->quiet)
2497 error ("accessing uninitialized array element");
2498 *non_constant_p = true;
2499 return t;
09b42213 2500 }
14869f28 2501
2502 /* If it's within the array bounds but doesn't have an explicit
2503 initializer, it's value-initialized. */
2504 tree val = build_value_init (elem_type, tf_warning_or_error);
2505 return cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
2506 overflow_p);
09b42213 2507}
2508
2509/* Subroutine of cxx_eval_constant_expression.
2510 Attempt to reduce a field access of a value of class type. */
2511
2512static tree
cf72f34d 2513cxx_eval_component_reference (const constexpr_ctx *ctx, tree t,
1227ba74 2514 bool lval,
09b42213 2515 bool *non_constant_p, bool *overflow_p)
2516{
2517 unsigned HOST_WIDE_INT i;
2518 tree field;
2519 tree value;
2520 tree part = TREE_OPERAND (t, 1);
2521 tree orig_whole = TREE_OPERAND (t, 0);
cf72f34d 2522 tree whole = cxx_eval_constant_expression (ctx, orig_whole,
1227ba74 2523 lval,
b2a43197 2524 non_constant_p, overflow_p);
7ab20d7c 2525 if (INDIRECT_REF_P (whole)
2b1749c4 2526 && integer_zerop (TREE_OPERAND (whole, 0)))
2527 {
2528 if (!ctx->quiet)
2529 error ("dereferencing a null pointer in %qE", orig_whole);
2530 *non_constant_p = true;
2531 return t;
2532 }
cb768824 2533
f16ed232 2534 if (TREE_CODE (whole) == PTRMEM_CST)
2535 whole = cplus_expand_constant (whole);
09b42213 2536 if (whole == orig_whole)
2537 return t;
1227ba74 2538 if (lval)
09b42213 2539 return fold_build3 (COMPONENT_REF, TREE_TYPE (t),
2540 whole, part, NULL_TREE);
2541 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
2542 CONSTRUCTOR. */
2543 if (!*non_constant_p && TREE_CODE (whole) != CONSTRUCTOR)
2544 {
f83e6885 2545 if (!ctx->quiet)
09b42213 2546 error ("%qE is not a constant expression", orig_whole);
2547 *non_constant_p = true;
2548 }
2549 if (DECL_MUTABLE_P (part))
2550 {
f83e6885 2551 if (!ctx->quiet)
09b42213 2552 error ("mutable %qD is not usable in a constant expression", part);
2553 *non_constant_p = true;
2554 }
2555 if (*non_constant_p)
2556 return t;
a4360e57 2557 bool pmf = TYPE_PTRMEMFUNC_P (TREE_TYPE (whole));
09b42213 2558 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
2559 {
a4360e57 2560 /* Use name match for PMF fields, as a variant will have a
2561 different FIELD_DECL with a different type. */
2562 if (pmf ? DECL_NAME (field) == DECL_NAME (part)
2563 : field == part)
9c96033c 2564 {
2565 if (value)
2566 return value;
2567 else
2568 /* We're in the middle of initializing it. */
2569 break;
2570 }
09b42213 2571 }
2572 if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE
2573 && CONSTRUCTOR_NELTS (whole) > 0)
2574 {
2575 /* DR 1188 says we don't have to deal with this. */
f83e6885 2576 if (!ctx->quiet)
09b42213 2577 error ("accessing %qD member instead of initialized %qD member in "
2578 "constant expression", part, CONSTRUCTOR_ELT (whole, 0)->index);
2579 *non_constant_p = true;
2580 return t;
2581 }
2582
f894a056 2583 /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty
2584 classes never get represented; throw together a value now. */
2585 if (is_really_empty_class (TREE_TYPE (t)))
2586 return build_constructor (TREE_TYPE (t), NULL);
2587
a4360e57 2588 gcc_assert (DECL_CONTEXT (part) == TYPE_MAIN_VARIANT (TREE_TYPE (whole)));
2589
165d0d5b 2590 if (CONSTRUCTOR_NO_CLEARING (whole))
cf72f34d 2591 {
2592 /* 'whole' is part of the aggregate initializer we're currently
2593 building; if there's no initializer for this member yet, that's an
f894a056 2594 error. */
f83e6885 2595 if (!ctx->quiet)
cf72f34d 2596 error ("accessing uninitialized member %qD", part);
2597 *non_constant_p = true;
2598 return t;
2599 }
2600
09b42213 2601 /* If there's no explicit init for this field, it's value-initialized. */
2602 value = build_value_init (TREE_TYPE (t), tf_warning_or_error);
cf72f34d 2603 return cxx_eval_constant_expression (ctx, value,
1227ba74 2604 lval,
b2a43197 2605 non_constant_p, overflow_p);
09b42213 2606}
2607
2608/* Subroutine of cxx_eval_constant_expression.
2609 Attempt to reduce a field access of a value of class type that is
2610 expressed as a BIT_FIELD_REF. */
2611
2612static tree
cf72f34d 2613cxx_eval_bit_field_ref (const constexpr_ctx *ctx, tree t,
1227ba74 2614 bool lval,
09b42213 2615 bool *non_constant_p, bool *overflow_p)
2616{
2617 tree orig_whole = TREE_OPERAND (t, 0);
2618 tree retval, fldval, utype, mask;
2619 bool fld_seen = false;
2620 HOST_WIDE_INT istart, isize;
cf72f34d 2621 tree whole = cxx_eval_constant_expression (ctx, orig_whole,
1227ba74 2622 lval,
b2a43197 2623 non_constant_p, overflow_p);
09b42213 2624 tree start, field, value;
2625 unsigned HOST_WIDE_INT i;
2626
2627 if (whole == orig_whole)
2628 return t;
2629 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
2630 CONSTRUCTOR. */
2631 if (!*non_constant_p
2632 && TREE_CODE (whole) != VECTOR_CST
2633 && TREE_CODE (whole) != CONSTRUCTOR)
2634 {
f83e6885 2635 if (!ctx->quiet)
09b42213 2636 error ("%qE is not a constant expression", orig_whole);
2637 *non_constant_p = true;
2638 }
2639 if (*non_constant_p)
2640 return t;
2641
2642 if (TREE_CODE (whole) == VECTOR_CST)
2643 return fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
2644 TREE_OPERAND (t, 1), TREE_OPERAND (t, 2));
2645
2646 start = TREE_OPERAND (t, 2);
2647 istart = tree_to_shwi (start);
2648 isize = tree_to_shwi (TREE_OPERAND (t, 1));
2649 utype = TREE_TYPE (t);
2650 if (!TYPE_UNSIGNED (utype))
2651 utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1);
2652 retval = build_int_cst (utype, 0);
2653 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
2654 {
2655 tree bitpos = bit_position (field);
2656 if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1))
2657 return value;
2658 if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE
2659 && TREE_CODE (value) == INTEGER_CST
2660 && tree_fits_shwi_p (bitpos)
2661 && tree_fits_shwi_p (DECL_SIZE (field)))
2662 {
2663 HOST_WIDE_INT bit = tree_to_shwi (bitpos);
2664 HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field));
2665 HOST_WIDE_INT shift;
2666 if (bit >= istart && bit + sz <= istart + isize)
2667 {
2668 fldval = fold_convert (utype, value);
2669 mask = build_int_cst_type (utype, -1);
2670 mask = fold_build2 (LSHIFT_EXPR, utype, mask,
2671 size_int (TYPE_PRECISION (utype) - sz));
2672 mask = fold_build2 (RSHIFT_EXPR, utype, mask,
2673 size_int (TYPE_PRECISION (utype) - sz));
2674 fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask);
2675 shift = bit - istart;
2676 if (BYTES_BIG_ENDIAN)
2677 shift = TYPE_PRECISION (utype) - shift - sz;
2678 fldval = fold_build2 (LSHIFT_EXPR, utype, fldval,
2679 size_int (shift));
2680 retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval);
2681 fld_seen = true;
2682 }
2683 }
2684 }
2685 if (fld_seen)
2686 return fold_convert (TREE_TYPE (t), retval);
2687 gcc_unreachable ();
2688 return error_mark_node;
2689}
2690
2691/* Subroutine of cxx_eval_constant_expression.
2692 Evaluate a short-circuited logical expression T in the context
2693 of a given constexpr CALL. BAILOUT_VALUE is the value for
2694 early return. CONTINUE_VALUE is used here purely for
2695 sanity check purposes. */
2696
2697static tree
cf72f34d 2698cxx_eval_logical_expression (const constexpr_ctx *ctx, tree t,
09b42213 2699 tree bailout_value, tree continue_value,
1227ba74 2700 bool lval,
09b42213 2701 bool *non_constant_p, bool *overflow_p)
2702{
2703 tree r;
cf72f34d 2704 tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
1227ba74 2705 lval,
b2a43197 2706 non_constant_p, overflow_p);
09b42213 2707 VERIFY_CONSTANT (lhs);
2708 if (tree_int_cst_equal (lhs, bailout_value))
2709 return lhs;
2710 gcc_assert (tree_int_cst_equal (lhs, continue_value));
cf72f34d 2711 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
1227ba74 2712 lval, non_constant_p,
b2a43197 2713 overflow_p);
09b42213 2714 VERIFY_CONSTANT (r);
2715 return r;
2716}
2717
2718/* REF is a COMPONENT_REF designating a particular field. V is a vector of
2719 CONSTRUCTOR elements to initialize (part of) an object containing that
2720 field. Return a pointer to the constructor_elt corresponding to the
2721 initialization of the field. */
2722
2723static constructor_elt *
2724base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref)
2725{
2726 tree aggr = TREE_OPERAND (ref, 0);
2727 tree field = TREE_OPERAND (ref, 1);
2728 HOST_WIDE_INT i;
2729 constructor_elt *ce;
2730
2731 gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
2732
2733 if (TREE_CODE (aggr) == COMPONENT_REF)
2734 {
2735 constructor_elt *base_ce
2736 = base_field_constructor_elt (v, aggr);
2737 v = CONSTRUCTOR_ELTS (base_ce->value);
2738 }
2739
2740 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
2741 if (ce->index == field)
2742 return ce;
2743
2744 gcc_unreachable ();
2745 return NULL;
2746}
2747
cf72f34d 2748/* Some of the expressions fed to the constexpr mechanism are calls to
2749 constructors, which have type void. In that case, return the type being
2750 initialized by the constructor. */
2751
2752static tree
2753initialized_type (tree t)
2754{
2755 if (TYPE_P (t))
2756 return t;
2757 tree type = cv_unqualified (TREE_TYPE (t));
2758 if (TREE_CODE (t) == CALL_EXPR || TREE_CODE (t) == AGGR_INIT_EXPR)
2759 {
2760 /* A constructor call has void type, so we need to look deeper. */
2761 tree fn = get_function_named_in_call (t);
2762 if (fn && TREE_CODE (fn) == FUNCTION_DECL
2763 && DECL_CXX_CONSTRUCTOR_P (fn))
2764 type = DECL_CONTEXT (fn);
2765 }
2766 return type;
2767}
2768
2769/* We're about to initialize element INDEX of an array or class from VALUE.
2770 Set up NEW_CTX appropriately by adjusting .object to refer to the
2771 subobject and creating a new CONSTRUCTOR if the element is itself
2772 a class or array. */
2773
2774static void
2775init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx,
2776 tree index, tree &value)
2777{
2778 new_ctx = *ctx;
2779
2780 if (index && TREE_CODE (index) != INTEGER_CST
2781 && TREE_CODE (index) != FIELD_DECL)
2782 /* This won't have an element in the new CONSTRUCTOR. */
2783 return;
2784
2785 tree type = initialized_type (value);
2786 if (!AGGREGATE_TYPE_P (type) && !VECTOR_TYPE_P (type))
2787 /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
2788 return;
2789
2790 /* The sub-aggregate initializer might contain a placeholder;
2791 update object to refer to the subobject and ctor to refer to
2792 the (newly created) sub-initializer. */
2793 if (ctx->object)
2794 new_ctx.object = build_ctor_subob_ref (index, type, ctx->object);
2795 tree elt = build_constructor (type, NULL);
165d0d5b 2796 CONSTRUCTOR_NO_CLEARING (elt) = true;
cf72f34d 2797 new_ctx.ctor = elt;
2798
2799 if (TREE_CODE (value) == TARGET_EXPR)
2800 /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
2801 value = TARGET_EXPR_INITIAL (value);
2802}
2803
2804/* We're about to process an initializer for a class or array TYPE. Make
2805 sure that CTX is set up appropriately. */
2806
2807static void
2808verify_ctor_sanity (const constexpr_ctx *ctx, tree type)
2809{
2810 /* We don't bother building a ctor for an empty base subobject. */
2811 if (is_empty_class (type))
2812 return;
2813
2814 /* We're in the middle of an initializer that might involve placeholders;
2815 our caller should have created a CONSTRUCTOR for us to put the
2816 initializer into. We will either return that constructor or T. */
2817 gcc_assert (ctx->ctor);
2818 gcc_assert (same_type_ignoring_top_level_qualifiers_p
2819 (type, TREE_TYPE (ctx->ctor)));
bbf58224 2820 /* We used to check that ctx->ctor was empty, but that isn't the case when
2821 the object is zero-initialized before calling the constructor. */
cf72f34d 2822 if (ctx->object)
d9751a3c 2823 {
2824 tree otype = TREE_TYPE (ctx->object);
2825 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, otype)
2826 /* Handle flexible array members. */
2827 || (TREE_CODE (otype) == ARRAY_TYPE
2828 && TYPE_DOMAIN (otype) == NULL_TREE
2829 && TREE_CODE (type) == ARRAY_TYPE
2830 && (same_type_ignoring_top_level_qualifiers_p
2831 (TREE_TYPE (type), TREE_TYPE (otype)))));
2832 }
cf72f34d 2833 gcc_assert (!ctx->object || !DECL_P (ctx->object)
2834 || *(ctx->values->get (ctx->object)) == ctx->ctor);
2835}
2836
09b42213 2837/* Subroutine of cxx_eval_constant_expression.
2838 The expression tree T denotes a C-style array or a C-style
2839 aggregate. Reduce it to a constant expression. */
2840
2841static tree
cf72f34d 2842cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t,
1227ba74 2843 bool lval,
09b42213 2844 bool *non_constant_p, bool *overflow_p)
2845{
2846 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
09b42213 2847 bool changed = false;
2848 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t));
68464393 2849 tree type = TREE_TYPE (t);
cf72f34d 2850
68464393 2851 constexpr_ctx new_ctx;
d2dd85e3 2852 if (TYPE_PTRMEMFUNC_P (type) || VECTOR_TYPE_P (type))
68464393 2853 {
d2dd85e3 2854 /* We don't really need the ctx->ctor business for a PMF or
2855 vector, but it's simpler to use the same code. */
68464393 2856 new_ctx = *ctx;
2857 new_ctx.ctor = build_constructor (type, NULL);
2858 new_ctx.object = NULL_TREE;
2859 ctx = &new_ctx;
2860 };
2861 verify_ctor_sanity (ctx, type);
cf72f34d 2862 vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
2863 vec_alloc (*p, vec_safe_length (v));
2864
58b0f9ce 2865 unsigned i;
2866 tree index, value;
2867 bool constant_p = true;
2868 bool side_effects_p = false;
cf72f34d 2869 FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value)
09b42213 2870 {
5ae773e4 2871 tree orig_value = value;
cf72f34d 2872 init_subob_ctx (ctx, new_ctx, index, value);
2873 if (new_ctx.ctor != ctx->ctor)
2874 /* If we built a new CONSTRUCTOR, attach it now so that other
2875 initializers can refer to it. */
2876 CONSTRUCTOR_APPEND_ELT (*p, index, new_ctx.ctor);
2877 tree elt = cxx_eval_constant_expression (&new_ctx, value,
1227ba74 2878 lval,
b2a43197 2879 non_constant_p, overflow_p);
09b42213 2880 /* Don't VERIFY_CONSTANT here. */
f83e6885 2881 if (ctx->quiet && *non_constant_p)
cf72f34d 2882 break;
5ae773e4 2883 if (elt != orig_value)
09b42213 2884 changed = true;
58b0f9ce 2885
2886 if (!TREE_CONSTANT (elt))
2887 constant_p = false;
2888 if (TREE_SIDE_EFFECTS (elt))
2889 side_effects_p = true;
cf72f34d 2890 if (index && TREE_CODE (index) == COMPONENT_REF)
09b42213 2891 {
2892 /* This is an initialization of a vfield inside a base
2893 subaggregate that we already initialized; push this
2894 initialization into the previous initialization. */
cf72f34d 2895 constructor_elt *inner = base_field_constructor_elt (*p, index);
09b42213 2896 inner->value = elt;
cf72f34d 2897 changed = true;
09b42213 2898 }
cf72f34d 2899 else if (index
2900 && (TREE_CODE (index) == NOP_EXPR
2901 || TREE_CODE (index) == POINTER_PLUS_EXPR))
09b42213 2902 {
2903 /* This is an initializer for an empty base; now that we've
2904 checked that it's constant, we can ignore it. */
cf72f34d 2905 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index))));
2906 changed = true;
2907 }
09b42213 2908 else
79e65a6b 2909 {
0dd49b5a 2910 if (new_ctx.ctor != ctx->ctor)
2911 {
2912 /* We appended this element above; update the value. */
2913 gcc_assert ((*p)->last().index == index);
2914 (*p)->last().value = elt;
2915 }
2916 else
2917 CONSTRUCTOR_APPEND_ELT (*p, index, elt);
2918 /* Adding or replacing an element might change the ctor's flags. */
79e65a6b 2919 TREE_CONSTANT (ctx->ctor) = constant_p;
2920 TREE_SIDE_EFFECTS (ctx->ctor) = side_effects_p;
2921 }
09b42213 2922 }
2923 if (*non_constant_p || !changed)
cf72f34d 2924 return t;
2925 t = ctx->ctor;
2926 /* We're done building this CONSTRUCTOR, so now we can interpret an
2927 element without an explicit initializer as value-initialized. */
165d0d5b 2928 CONSTRUCTOR_NO_CLEARING (t) = false;
58b0f9ce 2929 TREE_CONSTANT (t) = constant_p;
2930 TREE_SIDE_EFFECTS (t) = side_effects_p;
68464393 2931 if (VECTOR_TYPE_P (type))
09b42213 2932 t = fold (t);
2933 return t;
2934}
2935
2936/* Subroutine of cxx_eval_constant_expression.
2937 The expression tree T is a VEC_INIT_EXPR which denotes the desired
2938 initialization of a non-static data member of array type. Reduce it to a
2939 CONSTRUCTOR.
2940
2941 Note that apart from value-initialization (when VALUE_INIT is true),
2942 this is only intended to support value-initialization and the
2943 initializations done by defaulted constructors for classes with
2944 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
2945 will either be NULL_TREE for the default constructor, or a COMPONENT_REF
2946 for the copy/move constructor. */
2947
2948static tree
cf72f34d 2949cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init,
1227ba74 2950 bool value_init, bool lval,
09b42213 2951 bool *non_constant_p, bool *overflow_p)
2952{
2953 tree elttype = TREE_TYPE (atype);
cf72f34d 2954 verify_ctor_sanity (ctx, atype);
2955 vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
09b42213 2956 bool pre_init = false;
3e520b97 2957 unsigned HOST_WIDE_INT i;
c4970310 2958 tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
09b42213 2959
2960 /* For the default constructor, build up a call to the default
2961 constructor of the element type. We only need to handle class types
2962 here, as for a constructor to be constexpr, all members must be
2963 initialized, which for a defaulted default constructor means they must
2964 be of a class type with a constexpr default constructor. */
2965 if (TREE_CODE (elttype) == ARRAY_TYPE)
2966 /* We only do this at the lowest level. */;
2967 else if (value_init)
2968 {
c4970310 2969 init = build_value_init (elttype, complain);
09b42213 2970 pre_init = true;
2971 }
2972 else if (!init)
2973 {
2974 vec<tree, va_gc> *argvec = make_tree_vector ();
2975 init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
2976 &argvec, elttype, LOOKUP_NORMAL,
c4970310 2977 complain);
09b42213 2978 release_tree_vector (argvec);
9c96033c 2979 init = build_aggr_init_expr (TREE_TYPE (init), init);
09b42213 2980 pre_init = true;
2981 }
2982
51f0c7e2 2983 tree nelts = get_array_or_vector_nelts (ctx, atype, non_constant_p,
2984 overflow_p);
2985 unsigned HOST_WIDE_INT max = tree_to_uhwi (nelts);
3e520b97 2986 for (i = 0; i < max; ++i)
09b42213 2987 {
2988 tree idx = build_int_cst (size_type_node, i);
2989 tree eltinit;
d9377ad5 2990 bool reuse = false;
cf72f34d 2991 constexpr_ctx new_ctx;
2992 init_subob_ctx (ctx, new_ctx, idx, pre_init ? init : elttype);
2993 if (new_ctx.ctor != ctx->ctor)
2994 CONSTRUCTOR_APPEND_ELT (*p, idx, new_ctx.ctor);
09b42213 2995 if (TREE_CODE (elttype) == ARRAY_TYPE)
2996 {
2997 /* A multidimensional array; recurse. */
2998 if (value_init || init == NULL_TREE)
d9377ad5 2999 {
3000 eltinit = NULL_TREE;
3001 reuse = i == 0;
3002 }
09b42213 3003 else
c4970310 3004 eltinit = cp_build_array_ref (input_location, init, idx, complain);
cf72f34d 3005 eltinit = cxx_eval_vec_init_1 (&new_ctx, elttype, eltinit, value_init,
1227ba74 3006 lval,
09b42213 3007 non_constant_p, overflow_p);
3008 }
3009 else if (pre_init)
3010 {
3011 /* Initializing an element using value or default initialization
3012 we just pre-built above. */
d9377ad5 3013 eltinit = cxx_eval_constant_expression (&new_ctx, init, lval,
3014 non_constant_p, overflow_p);
3015 reuse = i == 0;
09b42213 3016 }
3017 else
3018 {
3019 /* Copying an element. */
3020 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3021 (atype, TREE_TYPE (init)));
c4970310 3022 eltinit = cp_build_array_ref (input_location, init, idx, complain);
18bede74 3023 if (!lvalue_p (init))
09b42213 3024 eltinit = move (eltinit);
c4970310 3025 eltinit = force_rvalue (eltinit, complain);
eb7c25cc 3026 eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval,
3027 non_constant_p, overflow_p);
09b42213 3028 }
f83e6885 3029 if (*non_constant_p && !ctx->quiet)
cf72f34d 3030 break;
3031 if (new_ctx.ctor != ctx->ctor)
3032 {
3033 /* We appended this element above; update the value. */
3034 gcc_assert ((*p)->last().index == idx);
3035 (*p)->last().value = eltinit;
3036 }
3037 else
3038 CONSTRUCTOR_APPEND_ELT (*p, idx, eltinit);
d9377ad5 3039 /* Reuse the result of cxx_eval_constant_expression call
eb7c25cc 3040 from the first iteration to all others if it is a constant
3041 initializer that doesn't require relocations. */
d9377ad5 3042 if (reuse
3043 && max > 1
eb7c25cc 3044 && (eltinit == NULL_TREE
3045 || (initializer_constant_valid_p (eltinit, TREE_TYPE (eltinit))
3046 == null_pointer_node)))
d9377ad5 3047 {
3048 if (new_ctx.ctor != ctx->ctor)
3049 eltinit = new_ctx.ctor;
228a2cd8 3050 tree range = build2 (RANGE_EXPR, size_type_node,
3051 build_int_cst (size_type_node, 1),
3052 build_int_cst (size_type_node, max - 1));
3053 CONSTRUCTOR_APPEND_ELT (*p, range, unshare_constructor (eltinit));
d9377ad5 3054 break;
3055 }
228a2cd8 3056 else if (i == 0)
3057 vec_safe_reserve (*p, max);
09b42213 3058 }
3059
3060 if (!*non_constant_p)
3061 {
cf72f34d 3062 init = ctx->ctor;
165d0d5b 3063 CONSTRUCTOR_NO_CLEARING (init) = false;
09b42213 3064 }
09b42213 3065 return init;
3066}
3067
3068static tree
cf72f34d 3069cxx_eval_vec_init (const constexpr_ctx *ctx, tree t,
1227ba74 3070 bool lval,
09b42213 3071 bool *non_constant_p, bool *overflow_p)
3072{
3073 tree atype = TREE_TYPE (t);
3074 tree init = VEC_INIT_EXPR_INIT (t);
cf72f34d 3075 tree r = cxx_eval_vec_init_1 (ctx, atype, init,
09b42213 3076 VEC_INIT_EXPR_VALUE_INIT (t),
1227ba74 3077 lval, non_constant_p, overflow_p);
09b42213 3078 if (*non_constant_p)
3079 return t;
3080 else
3081 return r;
3082}
3083
6b53d686 3084/* Like same_type_ignoring_top_level_qualifiers_p, but also handle the case
3085 where the desired type is an array of unknown bounds because the variable
3086 has had its bounds deduced since the wrapping expression was created. */
3087
3088static bool
3089same_type_ignoring_tlq_and_bounds_p (tree type1, tree type2)
3090{
3091 while (TREE_CODE (type1) == ARRAY_TYPE
3092 && TREE_CODE (type2) == ARRAY_TYPE
3093 && (!TYPE_DOMAIN (type1) || !TYPE_DOMAIN (type2)))
3094 {
3095 type1 = TREE_TYPE (type1);
3096 type2 = TREE_TYPE (type2);
3097 }
3098 return same_type_ignoring_top_level_qualifiers_p (type1, type2);
3099}
3100
09b42213 3101/* A less strict version of fold_indirect_ref_1, which requires cv-quals to
3102 match. We want to be less strict for simple *& folding; if we have a
3103 non-const temporary that we access through a const pointer, that should
3104 work. We handle this here rather than change fold_indirect_ref_1
3105 because we're dealing with things like ADDR_EXPR of INTEGER_CST which
3106 don't really make sense outside of constant expression evaluation. Also
3107 we want to allow folding to COMPONENT_REF, which could cause trouble
3108 with TBAA in fold_indirect_ref_1.
3109
3110 Try to keep this function synced with fold_indirect_ref_1. */
3111
3112static tree
3113cxx_fold_indirect_ref (location_t loc, tree type, tree op0, bool *empty_base)
3114{
7d8e655d 3115 tree sub = op0;
3116 tree subtype;
3117 poly_uint64 const_op01;
09b42213 3118
09b42213 3119 STRIP_NOPS (sub);
3120 subtype = TREE_TYPE (sub);
d03fa520 3121 if (!INDIRECT_TYPE_P (subtype))
09b42213 3122 return NULL_TREE;
3123
3124 if (TREE_CODE (sub) == ADDR_EXPR)
3125 {
3126 tree op = TREE_OPERAND (sub, 0);
3127 tree optype = TREE_TYPE (op);
3128
3129 /* *&CONST_DECL -> to the value of the const decl. */
3130 if (TREE_CODE (op) == CONST_DECL)
3131 return DECL_INITIAL (op);
3132 /* *&p => p; make sure to handle *&"str"[cst] here. */
6b53d686 3133 if (same_type_ignoring_tlq_and_bounds_p (optype, type))
09b42213 3134 {
3135 tree fop = fold_read_from_constant_string (op);
3136 if (fop)
3137 return fop;
3138 else
3139 return op;
3140 }
3141 /* *(foo *)&fooarray => fooarray[0] */
3142 else if (TREE_CODE (optype) == ARRAY_TYPE
3143 && (same_type_ignoring_top_level_qualifiers_p
3144 (type, TREE_TYPE (optype))))
3145 {
3146 tree type_domain = TYPE_DOMAIN (optype);
3147 tree min_val = size_zero_node;
3148 if (type_domain && TYPE_MIN_VALUE (type_domain))
3149 min_val = TYPE_MIN_VALUE (type_domain);
3150 return build4_loc (loc, ARRAY_REF, type, op, min_val,
3151 NULL_TREE, NULL_TREE);
3152 }
3153 /* *(foo *)&complexfoo => __real__ complexfoo */
3154 else if (TREE_CODE (optype) == COMPLEX_TYPE
3155 && (same_type_ignoring_top_level_qualifiers_p
3156 (type, TREE_TYPE (optype))))
3157 return fold_build1_loc (loc, REALPART_EXPR, type, op);
3158 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
76249021 3159 else if (VECTOR_TYPE_P (optype)
09b42213 3160 && (same_type_ignoring_top_level_qualifiers_p
3161 (type, TREE_TYPE (optype))))
3162 {
3163 tree part_width = TYPE_SIZE (type);
3164 tree index = bitsize_int (0);
7d8e655d 3165 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width,
3166 index);
09b42213 3167 }
3168 /* Also handle conversion to an empty base class, which
3169 is represented with a NOP_EXPR. */
3170 else if (is_empty_class (type)
3171 && CLASS_TYPE_P (optype)
3172 && DERIVED_FROM_P (type, optype))
3173 {
3174 *empty_base = true;
3175 return op;
3176 }
3177 /* *(foo *)&struct_with_foo_field => COMPONENT_REF */
3178 else if (RECORD_OR_UNION_TYPE_P (optype))
3179 {
3180 tree field = TYPE_FIELDS (optype);
3181 for (; field; field = DECL_CHAIN (field))
3182 if (TREE_CODE (field) == FIELD_DECL
a48667ee 3183 && TREE_TYPE (field) != error_mark_node
09b42213 3184 && integer_zerop (byte_position (field))
3185 && (same_type_ignoring_top_level_qualifiers_p
3186 (TREE_TYPE (field), type)))
a48667ee 3187 return fold_build3 (COMPONENT_REF, type, op, field, NULL_TREE);
09b42213 3188 }
3189 }
3190 else if (TREE_CODE (sub) == POINTER_PLUS_EXPR
7d8e655d 3191 && poly_int_tree_p (TREE_OPERAND (sub, 1), &const_op01))
09b42213 3192 {
3193 tree op00 = TREE_OPERAND (sub, 0);
3194 tree op01 = TREE_OPERAND (sub, 1);
3195
3196 STRIP_NOPS (op00);
3197 if (TREE_CODE (op00) == ADDR_EXPR)
3198 {
3199 tree op00type;
3200 op00 = TREE_OPERAND (op00, 0);
3201 op00type = TREE_TYPE (op00);
3202
3203 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
76249021 3204 if (VECTOR_TYPE_P (op00type)
7d8e655d 3205 && same_type_ignoring_top_level_qualifiers_p
3206 (type, TREE_TYPE (op00type))
3207 /* POINTER_PLUS_EXPR second operand is sizetype, unsigned,
3208 but we want to treat offsets with MSB set as negative.
3209 For the code below negative offsets are invalid and
3210 TYPE_SIZE of the element is something unsigned, so
3211 check whether op01 fits into poly_int64, which implies
3212 it is from 0 to INTTYPE_MAXIMUM (HOST_WIDE_INT), and
3213 then just use poly_uint64 because we want to treat the
3214 value as unsigned. */
3215 && tree_fits_poly_int64_p (op01))
09b42213 3216 {
09b42213 3217 tree part_width = TYPE_SIZE (type);
7d8e655d 3218 poly_uint64 max_offset
3219 = (tree_to_uhwi (part_width) / BITS_PER_UNIT
3220 * TYPE_VECTOR_SUBPARTS (op00type));
3221 if (known_lt (const_op01, max_offset))
3222 {
3223 tree index = bitsize_int (const_op01 * BITS_PER_UNIT);
3224 return fold_build3_loc (loc,
3225 BIT_FIELD_REF, type, op00,
3226 part_width, index);
3227 }
09b42213 3228 }
3229 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
3230 else if (TREE_CODE (op00type) == COMPLEX_TYPE
3231 && (same_type_ignoring_top_level_qualifiers_p
3232 (type, TREE_TYPE (op00type))))
3233 {
7d8e655d 3234 if (known_eq (wi::to_poly_offset (TYPE_SIZE_UNIT (type)),
3235 const_op01))
09b42213 3236 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
3237 }
3238 /* ((foo *)&fooarray)[1] => fooarray[1] */
3239 else if (TREE_CODE (op00type) == ARRAY_TYPE
3240 && (same_type_ignoring_top_level_qualifiers_p
3241 (type, TREE_TYPE (op00type))))
3242 {
3243 tree type_domain = TYPE_DOMAIN (op00type);
3244 tree min_val = size_zero_node;
3245 if (type_domain && TYPE_MIN_VALUE (type_domain))
3246 min_val = TYPE_MIN_VALUE (type_domain);
53fa96c0 3247 offset_int off = wi::to_offset (op01);
3248 offset_int el_sz = wi::to_offset (TYPE_SIZE_UNIT (type));
3249 offset_int remainder;
3250 off = wi::divmod_trunc (off, el_sz, SIGNED, &remainder);
3251 if (remainder == 0 && TREE_CODE (min_val) == INTEGER_CST)
3252 {
3253 off = off + wi::to_offset (min_val);
3254 op01 = wide_int_to_tree (sizetype, off);
3255 return build4_loc (loc, ARRAY_REF, type, op00, op01,
3256 NULL_TREE, NULL_TREE);
3257 }
09b42213 3258 }
3259 /* Also handle conversion to an empty base class, which
3260 is represented with a NOP_EXPR. */
3261 else if (is_empty_class (type)
3262 && CLASS_TYPE_P (op00type)
3263 && DERIVED_FROM_P (type, op00type))
3264 {
3265 *empty_base = true;
3266 return op00;
3267 }
3268 /* ((foo *)&struct_with_foo_field)[1] => COMPONENT_REF */
3269 else if (RECORD_OR_UNION_TYPE_P (op00type))
3270 {
3271 tree field = TYPE_FIELDS (op00type);
3272 for (; field; field = DECL_CHAIN (field))
3273 if (TREE_CODE (field) == FIELD_DECL
a48667ee 3274 && TREE_TYPE (field) != error_mark_node
09b42213 3275 && tree_int_cst_equal (byte_position (field), op01)
3276 && (same_type_ignoring_top_level_qualifiers_p
3277 (TREE_TYPE (field), type)))
a48667ee 3278 return fold_build3 (COMPONENT_REF, type, op00,
3279 field, NULL_TREE);
09b42213 3280 }
3281 }
3282 }
3283 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
3284 else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
3285 && (same_type_ignoring_top_level_qualifiers_p
3286 (type, TREE_TYPE (TREE_TYPE (subtype)))))
3287 {
3288 tree type_domain;
3289 tree min_val = size_zero_node;
7d8e655d 3290 tree newsub
3291 = cxx_fold_indirect_ref (loc, TREE_TYPE (subtype), sub, NULL);
09b42213 3292 if (newsub)
3293 sub = newsub;
3294 else
3295 sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub);
3296 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
3297 if (type_domain && TYPE_MIN_VALUE (type_domain))
3298 min_val = TYPE_MIN_VALUE (type_domain);
3299 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
3300 NULL_TREE);
3301 }
3302
3303 return NULL_TREE;
3304}
3305
3306static tree
cf72f34d 3307cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t,
1227ba74 3308 bool lval,
09b42213 3309 bool *non_constant_p, bool *overflow_p)
3310{
3311 tree orig_op0 = TREE_OPERAND (t, 0);
09b42213 3312 bool empty_base = false;
09b42213 3313
d2c63826 3314 /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second
3315 operand is an integer-zero. Otherwise reject the MEM_REF for now. */
3316
3317 if (TREE_CODE (t) == MEM_REF
3318 && (!TREE_OPERAND (t, 1) || !integer_zerop (TREE_OPERAND (t, 1))))
3319 {
3320 gcc_assert (ctx->quiet);
3321 *non_constant_p = true;
3322 return t;
3323 }
3324
7e0f3388 3325 /* First try to simplify it directly. */
3326 tree r = cxx_fold_indirect_ref (EXPR_LOCATION (t), TREE_TYPE (t), orig_op0,
3327 &empty_base);
3328 if (!r)
09b42213 3329 {
7e0f3388 3330 /* If that didn't work, evaluate the operand first. */
3331 tree op0 = cxx_eval_constant_expression (ctx, orig_op0,
3332 /*lval*/false, non_constant_p,
3333 overflow_p);
3334 /* Don't VERIFY_CONSTANT here. */
3335 if (*non_constant_p)
3336 return t;
3337
cb768824 3338 if (!lval && integer_zerop (op0))
3339 {
3340 if (!ctx->quiet)
3341 error ("dereferencing a null pointer");
3342 *non_constant_p = true;
3343 return t;
3344 }
3345
7e0f3388 3346 r = cxx_fold_indirect_ref (EXPR_LOCATION (t), TREE_TYPE (t), op0,
3347 &empty_base);
3348 if (r == NULL_TREE)
09b42213 3349 {
3350 /* We couldn't fold to a constant value. Make sure it's not
3351 something we should have been able to fold. */
7e0f3388 3352 tree sub = op0;
3353 STRIP_NOPS (sub);
3354 if (TREE_CODE (sub) == ADDR_EXPR)
3355 {
3356 gcc_assert (!same_type_ignoring_top_level_qualifiers_p
3357 (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t)));
3358 /* DR 1188 says we don't have to deal with this. */
3359 if (!ctx->quiet)
3360 error ("accessing value of %qE through a %qT glvalue in a "
3361 "constant expression", build_fold_indirect_ref (sub),
3362 TREE_TYPE (t));
3363 *non_constant_p = true;
3364 return t;
3365 }
3366
3367 if (lval && op0 != orig_op0)
3368 return build1 (INDIRECT_REF, TREE_TYPE (t), op0);
3369 if (!lval)
3370 VERIFY_CONSTANT (t);
09b42213 3371 return t;
3372 }
3373 }
3374
7e0f3388 3375 r = cxx_eval_constant_expression (ctx, r,
3376 lval, non_constant_p, overflow_p);
3377 if (*non_constant_p)
3378 return t;
3379
ce4ffd9e 3380 /* If we're pulling out the value of an empty base, just return an empty
09b42213 3381 CONSTRUCTOR. */
1227ba74 3382 if (empty_base && !lval)
09b42213 3383 {
09b42213 3384 r = build_constructor (TREE_TYPE (t), NULL);
3385 TREE_CONSTANT (r) = true;
3386 }
3387
09b42213 3388 return r;
3389}
3390
3391/* Complain about R, a VAR_DECL, not being usable in a constant expression.
3392 Shared between potential_constant_expression and
3393 cxx_eval_constant_expression. */
3394
3395static void
3396non_const_var_error (tree r)
3397{
3398 tree type = TREE_TYPE (r);
3399 error ("the value of %qD is not usable in a constant "
3400 "expression", r);
3401 /* Avoid error cascade. */
3402 if (DECL_INITIAL (r) == error_mark_node)
3403 return;
3404 if (DECL_DECLARED_CONSTEXPR_P (r))
3405 inform (DECL_SOURCE_LOCATION (r),
3406 "%qD used in its own initializer", r);
3407 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3408 {
3409 if (!CP_TYPE_CONST_P (type))
3410 inform (DECL_SOURCE_LOCATION (r),
3411 "%q#D is not const", r);
3412 else if (CP_TYPE_VOLATILE_P (type))
3413 inform (DECL_SOURCE_LOCATION (r),
3414 "%q#D is volatile", r);
3415 else if (!DECL_INITIAL (r)
c8e3e744 3416 || !TREE_CONSTANT (DECL_INITIAL (r))
3417 || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r))
09b42213 3418 inform (DECL_SOURCE_LOCATION (r),
3419 "%qD was not initialized with a constant "
3420 "expression", r);
3421 else
3422 gcc_unreachable ();
3423 }
90ad495b 3424 else if (TYPE_REF_P (type))
ac6641ca 3425 inform (DECL_SOURCE_LOCATION (r),
3426 "%qD was not initialized with a constant "
3427 "expression", r);
09b42213 3428 else
3429 {
3430 if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r))
3431 inform (DECL_SOURCE_LOCATION (r),
3432 "%qD was not declared %<constexpr%>", r);
3433 else
3434 inform (DECL_SOURCE_LOCATION (r),
3435 "%qD does not have integral or enumeration type",
3436 r);
3437 }
3438}
3439
3440/* Subroutine of cxx_eval_constant_expression.
3441 Like cxx_eval_unary_expression, except for trinary expressions. */
3442
3443static tree
cf72f34d 3444cxx_eval_trinary_expression (const constexpr_ctx *ctx, tree t,
1227ba74 3445 bool lval,
09b42213 3446 bool *non_constant_p, bool *overflow_p)
3447{
3448 int i;
3449 tree args[3];
3450 tree val;
3451
3452 for (i = 0; i < 3; i++)
3453 {
cf72f34d 3454 args[i] = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, i),
1227ba74 3455 lval,
b2a43197 3456 non_constant_p, overflow_p);
09b42213 3457 VERIFY_CONSTANT (args[i]);
3458 }
3459
3460 val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t),
3461 args[0], args[1], args[2]);
3462 if (val == NULL_TREE)
3463 return t;
3464 VERIFY_CONSTANT (val);
3465 return val;
3466}
3467
33603066 3468/* True if T was declared in a function declared to be constexpr, and
3469 therefore potentially constant in C++14. */
3470
09b42213 3471bool
3472var_in_constexpr_fn (tree t)
3473{
3474 tree ctx = DECL_CONTEXT (t);
33603066 3475 return (ctx && TREE_CODE (ctx) == FUNCTION_DECL
09b42213 3476 && DECL_DECLARED_CONSTEXPR_P (ctx));
3477}
3478
33603066 3479/* True if T was declared in a function that might be constexpr: either a
3480 function that was declared constexpr, or a C++17 lambda op(). */
3481
3482bool
3483var_in_maybe_constexpr_fn (tree t)
3484{
40e2decb 3485 if (cxx_dialect >= cxx17
33603066 3486 && DECL_FUNCTION_SCOPE_P (t)
3487 && LAMBDA_FUNCTION_P (DECL_CONTEXT (t)))
3488 return true;
3489 return var_in_constexpr_fn (t);
3490}
3491
93312944 3492/* We're assigning INIT to TARGET. In do_build_copy_constructor and
3493 build_over_call we implement trivial copy of a class with tail padding using
3494 assignment of character arrays, which is valid in normal code, but not in
3495 constexpr evaluation. We don't need to worry about clobbering tail padding
3496 in constexpr evaluation, so strip the type punning. */
3497
3498static void
3499maybe_simplify_trivial_copy (tree &target, tree &init)
3500{
3501 if (TREE_CODE (target) == MEM_REF
3502 && TREE_CODE (init) == MEM_REF
3503 && TREE_TYPE (target) == TREE_TYPE (init)
3504 && TREE_CODE (TREE_TYPE (target)) == ARRAY_TYPE
3505 && TREE_TYPE (TREE_TYPE (target)) == unsigned_char_type_node)
3506 {
3507 target = build_fold_indirect_ref (TREE_OPERAND (target, 0));
3508 init = build_fold_indirect_ref (TREE_OPERAND (init, 0));
3509 }
3510}
3511
9c96033c 3512/* Evaluate an INIT_EXPR or MODIFY_EXPR. */
3513
3514static tree
3515cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
1227ba74 3516 bool lval,
9c96033c 3517 bool *non_constant_p, bool *overflow_p)
3518{
3519 constexpr_ctx new_ctx = *ctx;
3520
8a36d0ec 3521 tree init = TREE_OPERAND (t, 1);
3522 if (TREE_CLOBBER_P (init))
3523 /* Just ignore clobbers. */
3524 return void_node;
3525
9c96033c 3526 /* First we figure out where we're storing to. */
3527 tree target = TREE_OPERAND (t, 0);
93312944 3528
3529 maybe_simplify_trivial_copy (target, init);
3530
9db9fc38 3531 tree type = TREE_TYPE (target);
9c96033c 3532 target = cxx_eval_constant_expression (ctx, target,
f83e6885 3533 true,
b2a43197 3534 non_constant_p, overflow_p);
9c96033c 3535 if (*non_constant_p)
3536 return t;
3537
cf3cefc9 3538 /* cxx_eval_array_reference for lval = true allows references one past
3539 end of array, because it does not know if it is just taking address
3540 (which is valid), or actual dereference. Here we know it is
3541 a dereference, so diagnose it here. */
3542 for (tree probe = target; probe; )
3543 {
3544 switch (TREE_CODE (probe))
3545 {
3546 case ARRAY_REF:
3547 tree nelts, ary;
3548 ary = TREE_OPERAND (probe, 0);
51f0c7e2 3549 nelts = get_array_or_vector_nelts (ctx, TREE_TYPE (ary),
3550 non_constant_p, overflow_p);
cf3cefc9 3551 VERIFY_CONSTANT (nelts);
3552 gcc_assert (TREE_CODE (nelts) == INTEGER_CST
3553 && TREE_CODE (TREE_OPERAND (probe, 1)) == INTEGER_CST);
cb7bca5f 3554 if (wi::to_widest (TREE_OPERAND (probe, 1)) == wi::to_widest (nelts))
cf3cefc9 3555 {
3556 diag_array_subscript (ctx, ary, TREE_OPERAND (probe, 1));
3557 *non_constant_p = true;
3558 return t;
3559 }
3560 /* FALLTHRU */
3561
3562 case BIT_FIELD_REF:
3563 case COMPONENT_REF:
3564 probe = TREE_OPERAND (probe, 0);
3565 continue;
3566
3567 default:
3568 probe = NULL_TREE;
3569 continue;
3570 }
3571 }
3572
9db9fc38 3573 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (target), type))
3574 {
3575 /* For initialization of an empty base, the original target will be
3576 *(base*)this, which the above evaluation resolves to the object
3577 argument, which has the derived type rather than the base type. In
3578 this situation, just evaluate the initializer and return, since
3579 there's no actual data to store. */
3580 gcc_assert (is_empty_class (type));
3581 return cxx_eval_constant_expression (ctx, init, false,
3582 non_constant_p, overflow_p);
3583 }
3584
9c96033c 3585 /* And then find the underlying variable. */
3586 vec<tree,va_gc> *refs = make_tree_vector();
3587 tree object = NULL_TREE;
3588 for (tree probe = target; object == NULL_TREE; )
3589 {
3590 switch (TREE_CODE (probe))
3591 {
3592 case BIT_FIELD_REF:
3593 case COMPONENT_REF:
3594 case ARRAY_REF:
3595 vec_safe_push (refs, TREE_OPERAND (probe, 1));
3596 vec_safe_push (refs, TREE_TYPE (probe));
3597 probe = TREE_OPERAND (probe, 0);
3598 break;
3599
3600 default:
3601 object = probe;
9c96033c 3602 }
3603 }
3604
3605 /* And then find/build up our initializer for the path to the subobject
3606 we're initializing. */
6fe83e54 3607 tree *valp;
43b7271d 3608 if (object == ctx->object && VAR_P (object)
3609 && DECL_NAME (object) && ctx->call == NULL)
3610 /* The variable we're building up an aggregate initializer for is outside
3611 the constant-expression, so don't evaluate the store. We check
3612 DECL_NAME to handle TARGET_EXPR temporaries, which are fair game. */
3613 valp = NULL;
3614 else if (DECL_P (object))
6fe83e54 3615 valp = ctx->values->get (object);
3616 else
3617 valp = NULL;
9c96033c 3618 if (!valp)
3619 {
3620 /* A constant-expression cannot modify objects from outside the
3621 constant-expression. */
f83e6885 3622 if (!ctx->quiet)
ea034e2c 3623 error ("modification of %qE is not a constant expression", object);
9c96033c 3624 *non_constant_p = true;
3625 return t;
3626 }
9db9fc38 3627 type = TREE_TYPE (object);
a02b42fe 3628 bool no_zero_init = true;
58b0f9ce 3629
3630 vec<tree,va_gc> *ctors = make_tree_vector ();
9c96033c 3631 while (!refs->is_empty())
3632 {
3633 if (*valp == NULL_TREE)
3634 {
3635 *valp = build_constructor (type, NULL);
165d0d5b 3636 CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
9c96033c 3637 }
92fe8840 3638 else if (TREE_CODE (*valp) == STRING_CST)
3639 {
3640 /* An array was initialized with a string constant, and now
3641 we're writing into one of its elements. Explode the
3642 single initialization into a set of element
3643 initializations. */
3644 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
3645
3646 tree string = *valp;
3647 tree elt_type = TREE_TYPE (type);
3648 unsigned chars_per_elt = (TYPE_PRECISION (elt_type)
3649 / TYPE_PRECISION (char_type_node));
3650 unsigned num_elts = TREE_STRING_LENGTH (string) / chars_per_elt;
3651 tree ary_ctor = build_constructor (type, NULL);
3652
3653 vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor), num_elts);
3654 for (unsigned ix = 0; ix != num_elts; ix++)
3655 {
3656 constructor_elt elt =
3657 {
3658 build_int_cst (size_type_node, ix),
3659 extract_string_elt (string, chars_per_elt, ix)
3660 };
3661 CONSTRUCTOR_ELTS (ary_ctor)->quick_push (elt);
3662 }
3663
3664 *valp = ary_ctor;
3665 }
3666
a02b42fe 3667 /* If the value of object is already zero-initialized, any new ctors for
3668 subobjects will also be zero-initialized. */
165d0d5b 3669 no_zero_init = CONSTRUCTOR_NO_CLEARING (*valp);
9c96033c 3670
58b0f9ce 3671 vec_safe_push (ctors, *valp);
3672
7963b19d 3673 enum tree_code code = TREE_CODE (type);
9c96033c 3674 type = refs->pop();
7963b19d 3675 tree index = refs->pop();
9c96033c 3676
9c96033c 3677 constructor_elt *cep = NULL;
7963b19d 3678 if (code == ARRAY_TYPE)
3679 {
3680 HOST_WIDE_INT i
3681 = find_array_ctor_elt (*valp, index, /*insert*/true);
3682 gcc_assert (i >= 0);
3683 cep = CONSTRUCTOR_ELT (*valp, i);
3684 gcc_assert (TREE_CODE (cep->index) != RANGE_EXPR);
3685 }
3686 else
3687 {
3688 gcc_assert (TREE_CODE (index) == FIELD_DECL);
13ee2de7 3689
3690 /* We must keep the CONSTRUCTOR's ELTS in FIELD order.
3691 Usually we meet initializers in that order, but it is
3692 possible for base types to be placed not in program
3693 order. */
3694 tree fields = TYPE_FIELDS (DECL_CONTEXT (index));
3695 unsigned HOST_WIDE_INT idx;
3696
ef782365 3697 if (code == UNION_TYPE && CONSTRUCTOR_NELTS (*valp)
3698 && CONSTRUCTOR_ELT (*valp, 0)->index != index)
3699 /* Changing active member. */
3700 vec_safe_truncate (CONSTRUCTOR_ELTS (*valp), 0);
3701
13ee2de7 3702 for (idx = 0;
7963b19d 3703 vec_safe_iterate (CONSTRUCTOR_ELTS (*valp), idx, &cep);
13ee2de7 3704 idx++, fields = DECL_CHAIN (fields))
7963b19d 3705 {
13ee2de7 3706 if (index == cep->index)
3707 goto found;
3708
3709 /* The field we're initializing must be on the field
3710 list. Look to see if it is present before the
3711 field the current ELT initializes. */
3712 for (; fields != cep->index; fields = DECL_CHAIN (fields))
3713 if (index == fields)
3714 goto insert;
7963b19d 3715 }
13ee2de7 3716
3717 /* We fell off the end of the CONSTRUCTOR, so insert a new
3718 entry at the end. */
3719 insert:
3720 {
3721 constructor_elt ce = { index, NULL_TREE };
3722
3723 vec_safe_insert (CONSTRUCTOR_ELTS (*valp), idx, ce);
3724 cep = CONSTRUCTOR_ELT (*valp, idx);
3725 }
3726 found:;
7963b19d 3727 }
9c96033c 3728 valp = &cep->value;
3729 }
3730 release_tree_vector (refs);
3731
469dc5a6 3732 if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
9c96033c 3733 {
3734 /* Create a new CONSTRUCTOR in case evaluation of the initializer
3735 wants to modify it. */
469dc5a6 3736 if (*valp == NULL_TREE)
6cded085 3737 {
6993e708 3738 *valp = build_constructor (type, NULL);
165d0d5b 3739 CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
6cded085 3740 }
6993e708 3741 else if (TREE_CODE (*valp) == PTRMEM_CST)
3742 *valp = cplus_expand_constant (*valp);
3743 new_ctx.ctor = *valp;
9c96033c 3744 new_ctx.object = target;
3745 }
3746
8a36d0ec 3747 init = cxx_eval_constant_expression (&new_ctx, init, false,
3748 non_constant_p, overflow_p);
7b9f713d 3749 /* Don't share a CONSTRUCTOR that might be changed later. */
e283bb4f 3750 init = unshare_constructor (init);
9c96033c 3751 if (target == object)
5579a199 3752 /* The hash table might have moved since the get earlier. */
3753 valp = ctx->values->get (object);
3754
3755 if (TREE_CODE (init) == CONSTRUCTOR)
469dc5a6 3756 {
5579a199 3757 /* An outer ctx->ctor might be pointing to *valp, so replace
3758 its contents. */
3759 CONSTRUCTOR_ELTS (*valp) = CONSTRUCTOR_ELTS (init);
3760 TREE_CONSTANT (*valp) = TREE_CONSTANT (init);
3761 TREE_SIDE_EFFECTS (*valp) = TREE_SIDE_EFFECTS (init);
165d0d5b 3762 CONSTRUCTOR_NO_CLEARING (*valp)
3763 = CONSTRUCTOR_NO_CLEARING (init);
469dc5a6 3764 }
9c96033c 3765 else
5579a199 3766 *valp = init;
58b0f9ce 3767
5579a199 3768 /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing
3769 CONSTRUCTORs, if any. */
3770 tree elt;
3771 unsigned i;
3772 bool c = TREE_CONSTANT (init);
3773 bool s = TREE_SIDE_EFFECTS (init);
3774 if (!c || s)
3775 FOR_EACH_VEC_SAFE_ELT (ctors, i, elt)
3776 {
3777 if (!c)
3778 TREE_CONSTANT (elt) = false;
3779 if (s)
3780 TREE_SIDE_EFFECTS (elt) = true;
3781 }
58b0f9ce 3782 release_tree_vector (ctors);
9c96033c 3783
3784 if (*non_constant_p)
3785 return t;
1227ba74 3786 else if (lval)
9c96033c 3787 return target;
3788 else
dfac7dd2 3789 return init;
9c96033c 3790}
3791
3792/* Evaluate a ++ or -- expression. */
3793
3794static tree
3795cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t,
1227ba74 3796 bool lval,
9c96033c 3797 bool *non_constant_p, bool *overflow_p)
3798{
3799 enum tree_code code = TREE_CODE (t);
3800 tree type = TREE_TYPE (t);
3801 tree op = TREE_OPERAND (t, 0);
3802 tree offset = TREE_OPERAND (t, 1);
3803 gcc_assert (TREE_CONSTANT (offset));
3804
3805 /* The operand as an lvalue. */
f83e6885 3806 op = cxx_eval_constant_expression (ctx, op, true,
b2a43197 3807 non_constant_p, overflow_p);
9c96033c 3808
3809 /* The operand as an rvalue. */
c017458d 3810 tree val
3811 = cxx_eval_constant_expression (ctx, op, false,
3812 non_constant_p, overflow_p);
35f2c28f 3813 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
3814 a local array in a constexpr function. */
d03fa520 3815 bool ptr = INDIRECT_TYPE_P (TREE_TYPE (val));
35f2c28f 3816 if (!ptr)
3817 VERIFY_CONSTANT (val);
9c96033c 3818
3819 /* The modified value. */
3820 bool inc = (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR);
c8a8f7ad 3821 tree mod;
d03fa520 3822 if (INDIRECT_TYPE_P (type))
c8a8f7ad 3823 {
3824 /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
3825 offset = convert_to_ptrofftype (offset);
3826 if (!inc)
3827 offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset);
3828 mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset);
3829 }
3830 else
3831 mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset);
35f2c28f 3832 if (!ptr)
3833 VERIFY_CONSTANT (mod);
9c96033c 3834
3835 /* Storing the modified value. */
3836 tree store = build2 (MODIFY_EXPR, type, op, mod);
f83e6885 3837 cxx_eval_constant_expression (ctx, store,
b2a43197 3838 true, non_constant_p, overflow_p);
9c96033c 3839
3840 /* And the value of the expression. */
3841 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
3842 {
3843 /* Prefix ops are lvalues. */
1227ba74 3844 if (lval)
9c96033c 3845 return op;
3846 else
3847 /* But we optimize when the caller wants an rvalue. */
3848 return mod;
3849 }
3850 else
3851 /* Postfix ops are rvalues. */
3852 return val;
3853}
3854
00f21715 3855/* Predicates for the meaning of *jump_target. */
3856
3857static bool
3858returns (tree *jump_target)
3859{
3860 return *jump_target
d29e4e8c 3861 && (TREE_CODE (*jump_target) == RETURN_EXPR
3862 || (TREE_CODE (*jump_target) == LABEL_DECL
3863 && LABEL_DECL_CDTOR (*jump_target)));
00f21715 3864}
3865
3866static bool
3867breaks (tree *jump_target)
3868{
3869 return *jump_target
cf03ba19 3870 && ((TREE_CODE (*jump_target) == LABEL_DECL
3871 && LABEL_DECL_BREAK (*jump_target))
3872 || TREE_CODE (*jump_target) == EXIT_EXPR);
00f21715 3873}
3874
3875static bool
3876continues (tree *jump_target)
3877{
3878 return *jump_target
3879 && TREE_CODE (*jump_target) == LABEL_DECL
3880 && LABEL_DECL_CONTINUE (*jump_target);
3881}
3882
3883static bool
3884switches (tree *jump_target)
3885{
3886 return *jump_target
3887 && TREE_CODE (*jump_target) == INTEGER_CST;
3888}
3889
3890/* Subroutine of cxx_eval_statement_list. Determine whether the statement
2a2770c6 3891 STMT matches *jump_target. If we're looking for a case label and we see
3892 the default label, note it in ctx->css_state. */
00f21715 3893
3894static bool
2a2770c6 3895label_matches (const constexpr_ctx *ctx, tree *jump_target, tree stmt)
00f21715 3896{
00f21715 3897 switch (TREE_CODE (*jump_target))
3898 {
3899 case LABEL_DECL:
3900 if (TREE_CODE (stmt) == LABEL_EXPR
3901 && LABEL_EXPR_LABEL (stmt) == *jump_target)
3902 return true;
3903 break;
3904
3905 case INTEGER_CST:
3906 if (TREE_CODE (stmt) == CASE_LABEL_EXPR)
3907 {
2a2770c6 3908 gcc_assert (ctx->css_state != NULL);
00f21715 3909 if (!CASE_LOW (stmt))
2a2770c6 3910 {
3911 /* default: should appear just once in a SWITCH_EXPR
3912 body (excluding nested SWITCH_EXPR). */
3913 gcc_assert (*ctx->css_state != css_default_seen);
3914 /* When evaluating SWITCH_EXPR body for the second time,
3915 return true for the default: label. */
3916 if (*ctx->css_state == css_default_processing)
3917 return true;
3918 *ctx->css_state = css_default_seen;
3919 }
83c375ba 3920 else if (CASE_HIGH (stmt))
3921 {
3922 if (tree_int_cst_le (CASE_LOW (stmt), *jump_target)
3923 && tree_int_cst_le (*jump_target, CASE_HIGH (stmt)))
3924 return true;
3925 }
00f21715 3926 else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt)))
3927 return true;
3928 }
3929 break;
3930
3931 default:
3932 gcc_unreachable ();
3933 }
3934 return false;
3935}
3936
3937/* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
3938 semantics, for switch, break, continue, and return. */
3939
3940static tree
3941cxx_eval_statement_list (const constexpr_ctx *ctx, tree t,
00f21715 3942 bool *non_constant_p, bool *overflow_p,
3943 tree *jump_target)
3944{
3945 tree_stmt_iterator i;
da7981e0 3946 tree local_target;
efea7c64 3947 /* In a statement-expression we want to return the last value.
3948 For empty statement expression return void_node. */
3949 tree r = void_node;
da7981e0 3950 if (!jump_target)
3951 {
3952 local_target = NULL_TREE;
3953 jump_target = &local_target;
3954 }
00f21715 3955 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
3956 {
00f21715 3957 tree stmt = tsi_stmt (i);
ba0f90e4 3958 /* We've found a continue, so skip everything until we reach
3959 the label its jumping to. */
3960 if (continues (jump_target))
3961 {
3962 if (label_matches (ctx, jump_target, stmt))
3963 /* Found it. */
3964 *jump_target = NULL_TREE;
3965 else
3966 continue;
3967 }
442c4003 3968 if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT)
3969 continue;
da7981e0 3970 r = cxx_eval_constant_expression (ctx, stmt, false,
3971 non_constant_p, overflow_p,
3972 jump_target);
00f21715 3973 if (*non_constant_p)
3974 break;
3975 if (returns (jump_target) || breaks (jump_target))
3976 break;
3977 }
da7981e0 3978 return r;
00f21715 3979}
3980
3981/* Evaluate a LOOP_EXPR for side-effects. Handles break and return
3982 semantics; continue semantics are covered by cxx_eval_statement_list. */
3983
3984static tree
3985cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t,
00f21715 3986 bool *non_constant_p, bool *overflow_p,
3987 tree *jump_target)
3988{
2631cb67 3989 constexpr_ctx new_ctx = *ctx;
3990
00f21715 3991 tree body = TREE_OPERAND (t, 0);
21483ab7 3992 int count = 0;
5370eb8d 3993 do
00f21715 3994 {
2631cb67 3995 hash_set<tree> save_exprs;
3996 new_ctx.save_exprs = &save_exprs;
3997
cf03ba19 3998 cxx_eval_constant_expression (&new_ctx, body, /*lval*/false,
3999 non_constant_p, overflow_p, jump_target);
2631cb67 4000
4001 /* Forget saved values of SAVE_EXPRs. */
4002 for (hash_set<tree>::iterator iter = save_exprs.begin();
4003 iter != save_exprs.end(); ++iter)
4004 new_ctx.values->remove (*iter);
21483ab7 4005 if (++count >= constexpr_loop_limit)
4006 {
4007 if (!ctx->quiet)
d3a3cfb8 4008 error_at (cp_expr_loc_or_loc (t, input_location),
5967b28b 4009 "%<constexpr%> loop iteration count exceeds limit of %d "
21483ab7 4010 "(use -fconstexpr-loop-limit= to increase the limit)",
4011 constexpr_loop_limit);
4012 *non_constant_p = true;
4013 break;
4014 }
00f21715 4015 }
2a2770c6 4016 while (!returns (jump_target)
4017 && !breaks (jump_target)
4018 && !switches (jump_target)
4019 && !*non_constant_p);
5370eb8d 4020
00f21715 4021 if (breaks (jump_target))
4022 *jump_target = NULL_TREE;
2631cb67 4023
00f21715 4024 return NULL_TREE;
4025}
4026
4027/* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
4028 semantics. */
4029
4030static tree
4031cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t,
00f21715 4032 bool *non_constant_p, bool *overflow_p,
4033 tree *jump_target)
4034{
4035 tree cond = TREE_OPERAND (t, 0);
f83e6885 4036 cond = cxx_eval_constant_expression (ctx, cond, false,
b2a43197 4037 non_constant_p, overflow_p);
00f21715 4038 VERIFY_CONSTANT (cond);
4039 *jump_target = cond;
4040
4041 tree body = TREE_OPERAND (t, 1);
2a2770c6 4042 constexpr_ctx new_ctx = *ctx;
4043 constexpr_switch_state css = css_default_not_seen;
4044 new_ctx.css_state = &css;
4045 cxx_eval_constant_expression (&new_ctx, body, false,
4046 non_constant_p, overflow_p, jump_target);
4047 if (switches (jump_target) && css == css_default_seen)
4048 {
4049 /* If the SWITCH_EXPR body has default: label, process it once again,
4050 this time instructing label_matches to return true for default:
4051 label on switches (jump_target). */
4052 css = css_default_processing;
4053 cxx_eval_constant_expression (&new_ctx, body, false,
4054 non_constant_p, overflow_p, jump_target);
4055 }
00f21715 4056 if (breaks (jump_target) || switches (jump_target))
4057 *jump_target = NULL_TREE;
4058 return NULL_TREE;
4059}
4060
85de9604 4061/* Find the object of TYPE under initialization in CTX. */
4062
4063static tree
4064lookup_placeholder (const constexpr_ctx *ctx, bool lval, tree type)
4065{
def8e41f 4066 if (!ctx)
85de9604 4067 return NULL_TREE;
4068
4069 /* We could use ctx->object unconditionally, but using ctx->ctor when we
4070 can is a minor optimization. */
def8e41f 4071 if (!lval && ctx->ctor && same_type_p (TREE_TYPE (ctx->ctor), type))
85de9604 4072 return ctx->ctor;
4073
def8e41f 4074 if (!ctx->object)
4075 return NULL_TREE;
4076
85de9604 4077 /* Since an object cannot have a field of its own type, we can search outward
4078 from ctx->object to find the unique containing object of TYPE. */
4079 tree ob = ctx->object;
4080 while (ob)
4081 {
4082 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob), type))
4083 break;
4084 if (handled_component_p (ob))
4085 ob = TREE_OPERAND (ob, 0);
4086 else
4087 ob = NULL_TREE;
4088 }
4089
4090 return ob;
4091}
4092
09b42213 4093/* Attempt to reduce the expression T to a constant value.
4094 On failure, issue diagnostic and return error_mark_node. */
4095/* FIXME unify with c_fully_fold */
9c96033c 4096/* FIXME overflow_p is too global */
09b42213 4097
4098static tree
cf72f34d 4099cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
1227ba74 4100 bool lval,
00f21715 4101 bool *non_constant_p, bool *overflow_p,
4102 tree *jump_target)
09b42213 4103{
cf72f34d 4104 constexpr_ctx new_ctx;
09b42213 4105 tree r = t;
4106
2a2770c6 4107 if (jump_target && *jump_target)
4108 {
4109 /* If we are jumping, ignore all statements/expressions except those
4110 that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */
4111 switch (TREE_CODE (t))
4112 {
4113 case BIND_EXPR:
4114 case STATEMENT_LIST:
4115 case LOOP_EXPR:
4116 case COND_EXPR:
4117 break;
4118 case LABEL_EXPR:
4119 case CASE_LABEL_EXPR:
4120 if (label_matches (ctx, jump_target, t))
4121 /* Found it. */
4122 *jump_target = NULL_TREE;
4123 return NULL_TREE;
4124 default:
4125 return NULL_TREE;
4126 }
4127 }
09b42213 4128 if (t == error_mark_node)
4129 {
4130 *non_constant_p = true;
4131 return t;
4132 }
4133 if (CONSTANT_CLASS_P (t))
4134 {
c4fa85c9 4135 if (TREE_OVERFLOW (t))
4136 {
4137 if (!ctx->quiet)
4138 permerror (input_location, "overflow in constant expression");
4139 if (!flag_permissive || ctx->quiet)
4140 *overflow_p = true;
4141 }
cb768824 4142
4143 if (TREE_CODE (t) == INTEGER_CST
90ad495b 4144 && TYPE_PTR_P (TREE_TYPE (t))
cb768824 4145 && !integer_zerop (t))
4146 {
4147 if (!ctx->quiet)
4148 error ("value %qE of type %qT is not a constant expression",
4149 t, TREE_TYPE (t));
4150 *non_constant_p = true;
4151 }
4152
09b42213 4153 return t;
4154 }
09b42213 4155
cb768824 4156 tree_code tcode = TREE_CODE (t);
4157 switch (tcode)
09b42213 4158 {
9c96033c 4159 case RESULT_DECL:
1227ba74 4160 if (lval)
9c96033c 4161 return t;
4162 /* We ask for an rvalue for the RESULT_DECL when indirecting
92a9c89e 4163 through an invisible reference, or in named return value
4164 optimization. */
8ea8d065 4165 if (tree *p = ctx->values->get (t))
4166 return *p;
4167 else
4168 {
4169 if (!ctx->quiet)
4170 error ("%qE is not a constant expression", t);
4171 *non_constant_p = true;
4172 }
4173 break;
9c96033c 4174
09b42213 4175 case VAR_DECL:
9c8aeb66 4176 if (DECL_HAS_VALUE_EXPR_P (t))
33603066 4177 return cxx_eval_constant_expression (ctx, DECL_VALUE_EXPR (t),
4178 lval, non_constant_p, overflow_p);
e3533433 4179 /* fall through */
8b2b2f24 4180 case CONST_DECL:
4181 /* We used to not check lval for CONST_DECL, but darwin.c uses
4182 CONST_DECL for aggregate constants. */
1227ba74 4183 if (lval)
09b42213 4184 return t;
594cc00c 4185 if (COMPLETE_TYPE_P (TREE_TYPE (t))
4186 && is_really_empty_class (TREE_TYPE (t)))
c7282bf2 4187 {
4188 /* If the class is empty, we aren't actually loading anything. */
4189 r = build_constructor (TREE_TYPE (t), NULL);
4190 TREE_CONSTANT (r) = true;
4191 }
4192 else if (ctx->strict)
2055d27a 4193 r = decl_really_constant_value (t);
4194 else
4195 r = decl_constant_value (t);
09b42213 4196 if (TREE_CODE (r) == TARGET_EXPR
4197 && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR)
4198 r = TARGET_EXPR_INITIAL (r);
f4ae4202 4199 if (VAR_P (r))
cf72f34d 4200 if (tree *p = ctx->values->get (r))
a143e277 4201 if (*p != NULL_TREE)
4202 r = *p;
09b42213 4203 if (DECL_P (r))
4204 {
f83e6885 4205 if (!ctx->quiet)
09b42213 4206 non_const_var_error (r);
4207 *non_constant_p = true;
4208 }
4209 break;
4210
90567983 4211 case DEBUG_BEGIN_STMT:
4212 /* ??? It might be nice to retain this information somehow, so
4213 as to be able to step into a constexpr function call. */
4214 /* Fall through. */
4215
09b42213 4216 case FUNCTION_DECL:
4217 case TEMPLATE_DECL:
4218 case LABEL_DECL:
00f21715 4219 case LABEL_EXPR:
4220 case CASE_LABEL_EXPR:
2a2770c6 4221 case PREDICT_EXPR:
09b42213 4222 return t;
4223
4224 case PARM_DECL:
90ad495b 4225 if (lval && !TYPE_REF_P (TREE_TYPE (t)))
9c96033c 4226 /* glvalue use. */;
4227 else if (tree *p = ctx->values->get (r))
4228 r = *p;
1227ba74 4229 else if (lval)
09b42213 4230 /* Defer in case this is only used for its type. */;
90ad495b 4231 else if (TYPE_REF_P (TREE_TYPE (t)))
1eb418cc 4232 /* Defer, there's no lvalue->rvalue conversion. */;
53d2f3de 4233 else if (COMPLETE_TYPE_P (TREE_TYPE (t))
4234 && is_really_empty_class (TREE_TYPE (t)))
a6613de7 4235 {
4236 /* If the class is empty, we aren't actually loading anything. */
4237 r = build_constructor (TREE_TYPE (t), NULL);
4238 TREE_CONSTANT (r) = true;
4239 }
09b42213 4240 else
4241 {
f83e6885 4242 if (!ctx->quiet)
09b42213 4243 error ("%qE is not a constant expression", t);
4244 *non_constant_p = true;
4245 }
4246 break;
4247
4248 case CALL_EXPR:
4249 case AGGR_INIT_EXPR:
1227ba74 4250 r = cxx_eval_call_expression (ctx, t, lval,
09b42213 4251 non_constant_p, overflow_p);
4252 break;
4253
9c96033c 4254 case DECL_EXPR:
4255 {
4256 r = DECL_EXPR_DECL (t);
4257 if (AGGREGATE_TYPE_P (TREE_TYPE (r))
4258 || VECTOR_TYPE_P (TREE_TYPE (r)))
4259 {
4260 new_ctx = *ctx;
4261 new_ctx.object = r;
4262 new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL);
165d0d5b 4263 CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
9c96033c 4264 new_ctx.values->put (r, new_ctx.ctor);
4265 ctx = &new_ctx;
4266 }
4267
4268 if (tree init = DECL_INITIAL (r))
4269 {
4270 init = cxx_eval_constant_expression (ctx, init,
f83e6885 4271 false,
b2a43197 4272 non_constant_p, overflow_p);
5898f0d7 4273 /* Don't share a CONSTRUCTOR that might be changed. */
e283bb4f 4274 init = unshare_constructor (init);
9c96033c 4275 ctx->values->put (r, init);
4276 }
4277 else if (ctx == &new_ctx)
4278 /* We gave it a CONSTRUCTOR above. */;
4279 else
4280 ctx->values->put (r, NULL_TREE);
4281 }
4282 break;
4283
09b42213 4284 case TARGET_EXPR:
4285 if (!literal_type_p (TREE_TYPE (t)))
4286 {
f83e6885 4287 if (!ctx->quiet)
09b42213 4288 {
4289 error ("temporary of non-literal type %qT in a "
4290 "constant expression", TREE_TYPE (t));
4291 explain_non_literal_class (TREE_TYPE (t));
4292 }
4293 *non_constant_p = true;
4294 break;
4295 }
cf72f34d 4296 if ((AGGREGATE_TYPE_P (TREE_TYPE (t)) || VECTOR_TYPE_P (TREE_TYPE (t))))
4297 {
4298 /* We're being expanded without an explicit target, so start
4299 initializing a new object; expansion with an explicit target
4300 strips the TARGET_EXPR before we get here. */
4301 new_ctx = *ctx;
4302 new_ctx.ctor = build_constructor (TREE_TYPE (t), NULL);
165d0d5b 4303 CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
cf72f34d 4304 new_ctx.object = TARGET_EXPR_SLOT (t);
4305 ctx->values->put (new_ctx.object, new_ctx.ctor);
4306 ctx = &new_ctx;
4307 }
1227ba74 4308 /* Pass false for 'lval' because this indicates
09b42213 4309 initialization of a temporary. */
cf72f34d 4310 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
f83e6885 4311 false,
b2a43197 4312 non_constant_p, overflow_p);
09b42213 4313 if (!*non_constant_p)
4314 /* Adjust the type of the result to the type of the temporary. */
4315 r = adjust_temp_type (TREE_TYPE (t), r);
1227ba74 4316 if (lval)
1eb418cc 4317 {
4318 tree slot = TARGET_EXPR_SLOT (t);
e283bb4f 4319 r = unshare_constructor (r);
1eb418cc 4320 ctx->values->put (slot, r);
4321 return slot;
4322 }
09b42213 4323 break;
4324
9c96033c 4325 case INIT_EXPR:
9c96033c 4326 case MODIFY_EXPR:
2a2770c6 4327 gcc_assert (jump_target == NULL || *jump_target == NULL_TREE);
1227ba74 4328 r = cxx_eval_store_expression (ctx, t, lval,
9c96033c 4329 non_constant_p, overflow_p);
4330 break;
4331
09b42213 4332 case SCOPE_REF:
cf72f34d 4333 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
1227ba74 4334 lval,
b2a43197 4335 non_constant_p, overflow_p);
09b42213 4336 break;
4337
4338 case RETURN_EXPR:
10fb495c 4339 if (TREE_OPERAND (t, 0) != NULL_TREE)
4340 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
4341 lval,
4342 non_constant_p, overflow_p);
2c572f5f 4343 if (jump_target)
4344 *jump_target = t;
4345 else
4346 {
4347 /* Can happen with ({ return true; }) && false; passed to
4348 maybe_constant_value. There is nothing to jump over in this
4349 case, and the bug will be diagnosed later. */
4350 gcc_assert (ctx->quiet);
4351 *non_constant_p = true;
4352 }
00f21715 4353 break;
4354
e0e672a0 4355 case SAVE_EXPR:
4356 /* Avoid evaluating a SAVE_EXPR more than once. */
4357 if (tree *p = ctx->values->get (t))
4358 r = *p;
4359 else
4360 {
c8f6aeb1 4361 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), false,
e0e672a0 4362 non_constant_p, overflow_p);
4363 ctx->values->put (t, r);
2631cb67 4364 if (ctx->save_exprs)
4365 ctx->save_exprs->add (t);
e0e672a0 4366 }
4367 break;
4368
09b42213 4369 case NON_LVALUE_EXPR:
4370 case TRY_CATCH_EXPR:
7f67d68c 4371 case TRY_BLOCK:
09b42213 4372 case CLEANUP_POINT_EXPR:
4373 case MUST_NOT_THROW_EXPR:
9c96033c 4374 case EXPR_STMT:
4375 case EH_SPEC_BLOCK:
cf72f34d 4376 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
1227ba74 4377 lval,
00f21715 4378 non_constant_p, overflow_p,
4379 jump_target);
09b42213 4380 break;
4381
7f67d68c 4382 case TRY_FINALLY_EXPR:
4383 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
4384 non_constant_p, overflow_p,
4385 jump_target);
4386 if (!*non_constant_p)
4387 /* Also evaluate the cleanup. */
4388 cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), true,
4389 non_constant_p, overflow_p,
4390 jump_target);
4391 break;
4392
09b42213 4393 /* These differ from cxx_eval_unary_expression in that this doesn't
4394 check for a constant operand or result; an address can be
4395 constant without its operand being, and vice versa. */
d2c63826 4396 case MEM_REF:
09b42213 4397 case INDIRECT_REF:
1227ba74 4398 r = cxx_eval_indirect_ref (ctx, t, lval,
09b42213 4399 non_constant_p, overflow_p);
4400 break;
4401
4402 case ADDR_EXPR:
4403 {
4404 tree oldop = TREE_OPERAND (t, 0);
cf72f34d 4405 tree op = cxx_eval_constant_expression (ctx, oldop,
1227ba74 4406 /*lval*/true,
b2a43197 4407 non_constant_p, overflow_p);
09b42213 4408 /* Don't VERIFY_CONSTANT here. */
4409 if (*non_constant_p)
4410 return t;
1eb418cc 4411 gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR);
09b42213 4412 /* This function does more aggressive folding than fold itself. */
4413 r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
4414 if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
4415 return t;
4416 break;
4417 }
4418
4419 case REALPART_EXPR:
4420 case IMAGPART_EXPR:
6fe424c2 4421 if (lval)
4422 {
4423 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
4424 non_constant_p, overflow_p);
4425 if (r == error_mark_node)
4426 ;
4427 else if (r == TREE_OPERAND (t, 0))
4428 r = t;
4429 else
4430 r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), r);
4431 break;
4432 }
4433 /* FALLTHRU */
09b42213 4434 case CONJ_EXPR:
4435 case FIX_TRUNC_EXPR:
4436 case FLOAT_EXPR:
4437 case NEGATE_EXPR:
4438 case ABS_EXPR:
19b4138e 4439 case ABSU_EXPR:
09b42213 4440 case BIT_NOT_EXPR:
4441 case TRUTH_NOT_EXPR:
4442 case FIXED_CONVERT_EXPR:
1227ba74 4443 r = cxx_eval_unary_expression (ctx, t, lval,
09b42213 4444 non_constant_p, overflow_p);
4445 break;
4446
4447 case SIZEOF_EXPR:
d2c63826 4448 r = fold_sizeof_expr (t);
09b42213 4449 VERIFY_CONSTANT (r);
4450 break;
4451
4452 case COMPOUND_EXPR:
4453 {
4454 /* check_return_expr sometimes wraps a TARGET_EXPR in a
4455 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
4456 introduced by build_call_a. */
4457 tree op0 = TREE_OPERAND (t, 0);
4458 tree op1 = TREE_OPERAND (t, 1);
4459 STRIP_NOPS (op1);
4460 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
4461 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
f83e6885 4462 r = cxx_eval_constant_expression (ctx, op0,
1227ba74 4463 lval, non_constant_p, overflow_p,
00f21715 4464 jump_target);
09b42213 4465 else
4466 {
4467 /* Check that the LHS is constant and then discard it. */
f83e6885 4468 cxx_eval_constant_expression (ctx, op0,
c8f6aeb1 4469 true, non_constant_p, overflow_p,
00f21715 4470 jump_target);
cf03ba19 4471 if (*non_constant_p)
4472 return t;
09b42213 4473 op1 = TREE_OPERAND (t, 1);
f83e6885 4474 r = cxx_eval_constant_expression (ctx, op1,
1227ba74 4475 lval, non_constant_p, overflow_p,
00f21715 4476 jump_target);
09b42213 4477 }
4478 }
4479 break;
4480
4481 case POINTER_PLUS_EXPR:
57e83b58 4482 case POINTER_DIFF_EXPR:
09b42213 4483 case PLUS_EXPR:
4484 case MINUS_EXPR:
4485 case MULT_EXPR:
4486 case TRUNC_DIV_EXPR:
4487 case CEIL_DIV_EXPR:
4488 case FLOOR_DIV_EXPR:
4489 case ROUND_DIV_EXPR:
4490 case TRUNC_MOD_EXPR:
4491 case CEIL_MOD_EXPR:
4492 case ROUND_MOD_EXPR:
4493 case RDIV_EXPR:
4494 case EXACT_DIV_EXPR:
4495 case MIN_EXPR:
4496 case MAX_EXPR:
4497 case LSHIFT_EXPR:
4498 case RSHIFT_EXPR:
4499 case LROTATE_EXPR:
4500 case RROTATE_EXPR:
4501 case BIT_IOR_EXPR:
4502 case BIT_XOR_EXPR:
4503 case BIT_AND_EXPR:
4504 case TRUTH_XOR_EXPR:
4505 case LT_EXPR:
4506 case LE_EXPR:
4507 case GT_EXPR:
4508 case GE_EXPR:
4509 case EQ_EXPR:
4510 case NE_EXPR:
4511 case UNORDERED_EXPR:
4512 case ORDERED_EXPR:
4513 case UNLT_EXPR:
4514 case UNLE_EXPR:
4515 case UNGT_EXPR:
4516 case UNGE_EXPR:
4517 case UNEQ_EXPR:
4518 case LTGT_EXPR:
4519 case RANGE_EXPR:
4520 case COMPLEX_EXPR:
1227ba74 4521 r = cxx_eval_binary_expression (ctx, t, lval,
09b42213 4522 non_constant_p, overflow_p);
4523 break;
4524
4525 /* fold can introduce non-IF versions of these; still treat them as
4526 short-circuiting. */
4527 case TRUTH_AND_EXPR:
4528 case TRUTH_ANDIF_EXPR:
cf72f34d 4529 r = cxx_eval_logical_expression (ctx, t, boolean_false_node,
09b42213 4530 boolean_true_node,
1227ba74 4531 lval,
09b42213 4532 non_constant_p, overflow_p);
4533 break;
4534
4535 case TRUTH_OR_EXPR:
4536 case TRUTH_ORIF_EXPR:
cf72f34d 4537 r = cxx_eval_logical_expression (ctx, t, boolean_true_node,
09b42213 4538 boolean_false_node,
1227ba74 4539 lval,
09b42213 4540 non_constant_p, overflow_p);
4541 break;
4542
4543 case ARRAY_REF:
1227ba74 4544 r = cxx_eval_array_reference (ctx, t, lval,
09b42213 4545 non_constant_p, overflow_p);
4546 break;
4547
4548 case COMPONENT_REF:
4549 if (is_overloaded_fn (t))
4550 {
4551 /* We can only get here in checking mode via
4552 build_non_dependent_expr, because any expression that
4553 calls or takes the address of the function will have
4554 pulled a FUNCTION_DECL out of the COMPONENT_REF. */
f83e6885 4555 gcc_checking_assert (ctx->quiet || errorcount);
09b42213 4556 *non_constant_p = true;
4557 return t;
4558 }
1227ba74 4559 r = cxx_eval_component_reference (ctx, t, lval,
09b42213 4560 non_constant_p, overflow_p);
4561 break;
4562
4563 case BIT_FIELD_REF:
1227ba74 4564 r = cxx_eval_bit_field_ref (ctx, t, lval,
09b42213 4565 non_constant_p, overflow_p);
4566 break;
4567
4568 case COND_EXPR:
2a2770c6 4569 if (jump_target && *jump_target)
4570 {
4571 /* When jumping to a label, the label might be either in the
4572 then or else blocks, so process then block first in skipping
4573 mode first, and if we are still in the skipping mode at its end,
4574 process the else block too. */
4575 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
4576 lval, non_constant_p, overflow_p,
4577 jump_target);
4578 if (*jump_target)
4579 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2),
4580 lval, non_constant_p, overflow_p,
4581 jump_target);
4582 break;
4583 }
1227ba74 4584 r = cxx_eval_conditional_expression (ctx, t, lval,
00f21715 4585 non_constant_p, overflow_p,
4586 jump_target);
09b42213 4587 break;
31595caf 4588 case VEC_COND_EXPR:
4589 r = cxx_eval_vector_conditional_expression (ctx, t, non_constant_p,
4590 overflow_p);
4591 break;
09b42213 4592
4593 case CONSTRUCTOR:
1a3631bc 4594 if (TREE_CONSTANT (t))
58b0f9ce 4595 {
4596 /* Don't re-process a constant CONSTRUCTOR, but do fold it to
4597 VECTOR_CST if applicable. */
79e65a6b 4598 verify_constructor_flags (t);
58b0f9ce 4599 if (TREE_CONSTANT (t))
4600 return fold (t);
4601 }
1227ba74 4602 r = cxx_eval_bare_aggregate (ctx, t, lval,
09b42213 4603 non_constant_p, overflow_p);
4604 break;
4605
4606 case VEC_INIT_EXPR:
4607 /* We can get this in a defaulted constructor for a class with a
4608 non-static data member of array type. Either the initializer will
4609 be NULL, meaning default-initialization, or it will be an lvalue
4610 or xvalue of the same type, meaning direct-initialization from the
4611 corresponding member. */
1227ba74 4612 r = cxx_eval_vec_init (ctx, t, lval,
09b42213 4613 non_constant_p, overflow_p);
4614 break;
4615
09b42213 4616 case VEC_PERM_EXPR:
1227ba74 4617 r = cxx_eval_trinary_expression (ctx, t, lval,
09b42213 4618 non_constant_p, overflow_p);
4619 break;
4620
941fafa5 4621 case NOP_EXPR:
4622 if (REINTERPRET_CAST_P (t))
4623 {
4624 if (!ctx->quiet)
d3a3cfb8 4625 error_at (cp_expr_loc_or_loc (t, input_location),
941fafa5 4626 "a reinterpret_cast is not a constant expression");
4627 *non_constant_p = true;
4628 return t;
4629 }
4630 /* FALLTHROUGH. */
09b42213 4631 case CONVERT_EXPR:
4632 case VIEW_CONVERT_EXPR:
d2c63826 4633 case UNARY_PLUS_EXPR:
09b42213 4634 {
4635 tree oldop = TREE_OPERAND (t, 0);
d2c63826 4636
cf72f34d 4637 tree op = cxx_eval_constant_expression (ctx, oldop,
1227ba74 4638 lval,
b2a43197 4639 non_constant_p, overflow_p);
09b42213 4640 if (*non_constant_p)
4641 return t;
f16ed232 4642 tree type = TREE_TYPE (t);
4643 if (TREE_CODE (op) == PTRMEM_CST
4644 && !TYPE_PTRMEM_P (type))
4645 op = cplus_expand_constant (op);
941fafa5 4646
6a9b35ef 4647 if (TREE_CODE (op) == PTRMEM_CST && tcode == NOP_EXPR)
4648 {
941fafa5 4649 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (op))
4650 && !can_convert_qual (type, op))
4651 op = cplus_expand_constant (op);
4652 return cp_fold_convert (type, op);
6a9b35ef 4653 }
cb768824 4654
d03fa520 4655 if (INDIRECT_TYPE_P (type) && TREE_CODE (op) == INTEGER_CST)
09b42213 4656 {
cb768824 4657 if (integer_zerop (op))
4658 {
90ad495b 4659 if (TYPE_REF_P (type))
cb768824 4660 {
4661 if (!ctx->quiet)
d3a3cfb8 4662 error_at (cp_expr_loc_or_loc (t, input_location),
cb768824 4663 "dereferencing a null pointer");
4664 *non_constant_p = true;
4665 return t;
4666 }
90ad495b 4667 else if (TYPE_PTR_P (TREE_TYPE (op)))
cb768824 4668 {
4669 tree from = TREE_TYPE (op);
4670
4671 if (!can_convert (type, from, tf_none))
4672 {
4673 if (!ctx->quiet)
d3a3cfb8 4674 error_at (cp_expr_loc_or_loc (t, input_location),
cb768824 4675 "conversion of %qT null pointer to %qT "
4676 "is not a constant expression",
4677 from, type);
4678 *non_constant_p = true;
4679 return t;
4680 }
4681 }
4682 }
4683 else
4684 {
4685 /* This detects for example:
4686 reinterpret_cast<void*>(sizeof 0)
4687 */
4688 if (!ctx->quiet)
d3a3cfb8 4689 error_at (cp_expr_loc_or_loc (t, input_location),
cb768824 4690 "%<reinterpret_cast<%T>(%E)%> is not "
ea034e2c 4691 "a constant expression",
cb768824 4692 type, op);
4693 *non_constant_p = true;
4694 return t;
4695 }
09b42213 4696 }
941fafa5 4697
d2c63826 4698 if (op == oldop && tcode != UNARY_PLUS_EXPR)
09b42213 4699 /* We didn't fold at the top so we could check for ptr-int
4700 conversion. */
4701 return fold (t);
941fafa5 4702
6b53d686 4703 /* Handle an array's bounds having been deduced after we built
4704 the wrapping expression. */
4705 if (same_type_ignoring_tlq_and_bounds_p (type, TREE_TYPE (op)))
4706 r = op;
4707 else if (tcode == UNARY_PLUS_EXPR)
d2c63826 4708 r = fold_convert (TREE_TYPE (t), op);
4709 else
4710 r = fold_build1 (tcode, type, op);
941fafa5 4711
09b42213 4712 /* Conversion of an out-of-range value has implementation-defined
4713 behavior; the language considers it different from arithmetic
4714 overflow, which is undefined. */
4715 if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op))
4716 TREE_OVERFLOW (r) = false;
4717 }
4718 break;
4719
4720 case EMPTY_CLASS_EXPR:
4721 /* This is good enough for a function argument that might not get
4722 used, and they can't do anything with it, so just return it. */
4723 return t;
4724
9c96033c 4725 case STATEMENT_LIST:
00f21715 4726 new_ctx = *ctx;
4727 new_ctx.ctor = new_ctx.object = NULL_TREE;
f83e6885 4728 return cxx_eval_statement_list (&new_ctx, t,
00f21715 4729 non_constant_p, overflow_p, jump_target);
9c96033c 4730
4731 case BIND_EXPR:
4732 return cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t),
1227ba74 4733 lval,
00f21715 4734 non_constant_p, overflow_p,
4735 jump_target);
9c96033c 4736
09b42213 4737 case PREINCREMENT_EXPR:
4738 case POSTINCREMENT_EXPR:
4739 case PREDECREMENT_EXPR:
4740 case POSTDECREMENT_EXPR:
f83e6885 4741 return cxx_eval_increment_expression (ctx, t,
1227ba74 4742 lval, non_constant_p, overflow_p);
9c96033c 4743
4744 case LAMBDA_EXPR:
09b42213 4745 case NEW_EXPR:
4746 case VEC_NEW_EXPR:
4747 case DELETE_EXPR:
4748 case VEC_DELETE_EXPR:
4749 case THROW_EXPR:
09b42213 4750 case MODOP_EXPR:
4751 /* GCC internal stuff. */
4752 case VA_ARG_EXPR:
4753 case OBJ_TYPE_REF:
09b42213 4754 case NON_DEPENDENT_EXPR:
4755 case BASELINK:
09b42213 4756 case OFFSET_REF:
f83e6885 4757 if (!ctx->quiet)
d3a3cfb8 4758 error_at (cp_expr_loc_or_loc (t, input_location),
ea034e2c 4759 "expression %qE is not a constant expression", t);
09b42213 4760 *non_constant_p = true;
4761 break;
4762
cf72f34d 4763 case PLACEHOLDER_EXPR:
85de9604 4764 /* Use of the value or address of the current object. */
4765 if (tree ctor = lookup_placeholder (ctx, lval, TREE_TYPE (t)))
4766 return cxx_eval_constant_expression (ctx, ctor, lval,
4767 non_constant_p, overflow_p);
4768 /* A placeholder without a referent. We can get here when
4769 checking whether NSDMIs are noexcept, or in massage_init_elt;
4770 just say it's non-constant for now. */
4771 gcc_assert (ctx->quiet);
4772 *non_constant_p = true;
cf72f34d 4773 break;
4774
cf03ba19 4775 case EXIT_EXPR:
4776 {
4777 tree cond = TREE_OPERAND (t, 0);
4778 cond = cxx_eval_constant_expression (ctx, cond, /*lval*/false,
4779 non_constant_p, overflow_p);
4780 VERIFY_CONSTANT (cond);
4781 if (integer_nonzerop (cond))
4782 *jump_target = t;
4783 }
4784 break;
4785
9c96033c 4786 case GOTO_EXPR:
00f21715 4787 *jump_target = TREE_OPERAND (t, 0);
d29e4e8c 4788 gcc_assert (breaks (jump_target) || continues (jump_target)
4789 /* Allow for jumping to a cdtor_label. */
4790 || returns (jump_target));
00f21715 4791 break;
4792
9c96033c 4793 case LOOP_EXPR:
f83e6885 4794 cxx_eval_loop_expr (ctx, t,
00f21715 4795 non_constant_p, overflow_p, jump_target);
4796 break;
4797
9c96033c 4798 case SWITCH_EXPR:
f83e6885 4799 cxx_eval_switch_expr (ctx, t,
00f21715 4800 non_constant_p, overflow_p, jump_target);
9c96033c 4801 break;
4802
56c12fd4 4803 case REQUIRES_EXPR:
4804 /* It's possible to get a requires-expression in a constant
4805 expression. For example:
4806
4807 template<typename T> concept bool C() {
4808 return requires (T t) { t; };
4809 }
4810
4811 template<typename T> requires !C<T>() void f(T);
4812
4813 Normalization leaves f with the associated constraint
4814 '!requires (T t) { ... }' which is not transformed into
4815 a constraint. */
4816 if (!processing_template_decl)
4817 return evaluate_constraint_expression (t, NULL_TREE);
4818 else
4819 *non_constant_p = true;
4820 return t;
4821
24c6ee98 4822 case ANNOTATE_EXPR:
24c6ee98 4823 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
4824 lval,
4825 non_constant_p, overflow_p,
4826 jump_target);
4827 break;
4828
7285637a 4829 case USING_STMT:
4830 r = void_node;
4831 break;
4832
09b42213 4833 default:
b9a3af23 4834 if (STATEMENT_CODE_P (TREE_CODE (t)))
4835 {
4836 /* This function doesn't know how to deal with pre-genericize
4837 statements; this can only happen with statement-expressions,
4838 so for now just fail. */
4839 if (!ctx->quiet)
4840 error_at (EXPR_LOCATION (t),
ea034e2c 4841 "statement is not a constant expression");
b9a3af23 4842 }
4843 else
4844 internal_error ("unexpected expression %qE of kind %s", t,
4845 get_tree_code_name (TREE_CODE (t)));
09b42213 4846 *non_constant_p = true;
4847 break;
4848 }
4849
4850 if (r == error_mark_node)
4851 *non_constant_p = true;
4852
4853 if (*non_constant_p)
4854 return t;
4855 else
4856 return r;
4857}
4858
6e1bbaae 4859/* P0859: A function is needed for constant evaluation if it is a constexpr
4860 function that is named by an expression ([basic.def.odr]) that is
4861 potentially constant evaluated.
4862
4863 So we need to instantiate any constexpr functions mentioned by the
4864 expression even if the definition isn't needed for evaluating the
4865 expression. */
4866
4867static tree
4868instantiate_cx_fn_r (tree *tp, int *walk_subtrees, void */*data*/)
4869{
4870 if (TREE_CODE (*tp) == FUNCTION_DECL
4871 && DECL_DECLARED_CONSTEXPR_P (*tp)
4872 && !DECL_INITIAL (*tp)
eed138cf 4873 && !trivial_fn_p (*tp)
6e1bbaae 4874 && DECL_TEMPLOID_INSTANTIATION (*tp))
4875 {
4876 ++function_depth;
4877 instantiate_decl (*tp, /*defer_ok*/false, /*expl_inst*/false);
4878 --function_depth;
4879 }
4880 else if (TREE_CODE (*tp) == CALL_EXPR
4881 || TREE_CODE (*tp) == AGGR_INIT_EXPR)
4882 {
4883 if (EXPR_HAS_LOCATION (*tp))
4884 input_location = EXPR_LOCATION (*tp);
4885 }
4886
4887 if (!EXPR_P (*tp))
4888 *walk_subtrees = 0;
4889
4890 return NULL_TREE;
4891}
4892static void
4893instantiate_constexpr_fns (tree t)
4894{
4895 location_t loc = input_location;
4896 cp_walk_tree_without_duplicates (&t, instantiate_cx_fn_r, NULL);
4897 input_location = loc;
4898}
4899
09b42213 4900static tree
cf72f34d 4901cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant,
2055d27a 4902 bool strict = true, tree object = NULL_TREE)
09b42213 4903{
c300fe28 4904 auto_timevar time (TV_CONSTEXPR);
4905
09b42213 4906 bool non_constant_p = false;
4907 bool overflow_p = false;
cf72f34d 4908 hash_map<tree,tree> map;
2631cb67 4909
2a2770c6 4910 constexpr_ctx ctx = { NULL, &map, NULL, NULL, NULL, NULL,
2631cb67 4911 allow_non_constant, strict };
4912
cf72f34d 4913 tree type = initialized_type (t);
cf72f34d 4914 tree r = t;
4915 if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
4916 {
4917 /* In C++14 an NSDMI can participate in aggregate initialization,
4918 and can refer to the address of the object being initialized, so
4919 we need to pass in the relevant VAR_DECL if we want to do the
4920 evaluation in a single pass. The evaluation will dynamically
4921 update ctx.values for the VAR_DECL. We use the same strategy
4922 for C++11 constexpr constructors that refer to the object being
4923 initialized. */
4924 ctx.ctor = build_constructor (type, NULL);
165d0d5b 4925 CONSTRUCTOR_NO_CLEARING (ctx.ctor) = true;
9c96033c 4926 if (!object)
4927 {
4928 if (TREE_CODE (t) == TARGET_EXPR)
4929 object = TARGET_EXPR_SLOT (t);
4930 else if (TREE_CODE (t) == AGGR_INIT_EXPR)
4931 object = AGGR_INIT_EXPR_SLOT (t);
4932 }
cf72f34d 4933 ctx.object = object;
4934 if (object)
4935 gcc_assert (same_type_ignoring_top_level_qualifiers_p
4936 (type, TREE_TYPE (object)));
4937 if (object && DECL_P (object))
4938 map.put (object, ctx.ctor);
4939 if (TREE_CODE (r) == TARGET_EXPR)
4940 /* Avoid creating another CONSTRUCTOR when we expand the
4941 TARGET_EXPR. */
4942 r = TARGET_EXPR_INITIAL (r);
4943 }
4944
6e1bbaae 4945 instantiate_constexpr_fns (r);
f83e6885 4946 r = cxx_eval_constant_expression (&ctx, r,
b2a43197 4947 false, &non_constant_p, &overflow_p);
09b42213 4948
4949 verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p);
4950
bbcc9042 4951 /* Mutable logic is a bit tricky: we want to allow initialization of
4952 constexpr variables with mutable members, but we can't copy those
4953 members to another constexpr variable. */
4954 if (TREE_CODE (r) == CONSTRUCTOR
4955 && CONSTRUCTOR_MUTABLE_POISON (r))
09b42213 4956 {
09b42213 4957 if (!allow_non_constant)
bbcc9042 4958 error ("%qE is not a constant expression because it refers to "
4959 "mutable subobjects of %qT", t, type);
09b42213 4960 non_constant_p = true;
4961 }
4962
c6d97004 4963 if (TREE_CODE (r) == CONSTRUCTOR
165d0d5b 4964 && CONSTRUCTOR_NO_CLEARING (r))
c6d97004 4965 {
4966 if (!allow_non_constant)
4967 error ("%qE is not a constant expression because it refers to "
4968 "an incompletely initialized variable", t);
4969 TREE_CONSTANT (r) = false;
4970 non_constant_p = true;
4971 }
4972
09b42213 4973 /* Technically we should check this for all subexpressions, but that
4974 runs into problems with our internal representation of pointer
4975 subtraction and the 5.19 rules are still in flux. */
4976 if (CONVERT_EXPR_CODE_P (TREE_CODE (r))
4977 && ARITHMETIC_TYPE_P (TREE_TYPE (r))
4978 && TREE_CODE (TREE_OPERAND (r, 0)) == ADDR_EXPR)
4979 {
4980 if (!allow_non_constant)
4981 error ("conversion from pointer type %qT "
ea034e2c 4982 "to arithmetic type %qT in a constant expression",
09b42213 4983 TREE_TYPE (TREE_OPERAND (r, 0)), TREE_TYPE (r));
4984 non_constant_p = true;
4985 }
4986
4987 if (!non_constant_p && overflow_p)
4988 non_constant_p = true;
4989
c6c0523b 4990 /* Unshare the result unless it's a CONSTRUCTOR in which case it's already
4991 unshared. */
4992 bool should_unshare = true;
4993 if (r == t || TREE_CODE (r) == CONSTRUCTOR)
4994 should_unshare = false;
4995
09b42213 4996 if (non_constant_p && !allow_non_constant)
4997 return error_mark_node;
4998 else if (non_constant_p && TREE_CONSTANT (r))
4999 {
1d29f4ea 5000 /* This isn't actually constant, so unset TREE_CONSTANT.
5001 Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires
5002 it to be set if it is invariant address, even when it is not
5003 a valid C++ constant expression. Wrap it with a NOP_EXPR
5004 instead. */
5005 if (EXPR_P (r) && TREE_CODE (r) != ADDR_EXPR)
09b42213 5006 r = copy_node (r);
5007 else if (TREE_CODE (r) == CONSTRUCTOR)
5008 r = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (r), r);
5009 else
5010 r = build_nop (TREE_TYPE (r), r);
5011 TREE_CONSTANT (r) = false;
5012 }
5013 else if (non_constant_p || r == t)
5014 return t;
5015
c6c0523b 5016 if (should_unshare)
5017 r = unshare_expr (r);
5018
09b42213 5019 if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
5020 {
5021 if (TREE_CODE (t) == TARGET_EXPR
5022 && TARGET_EXPR_INITIAL (t) == r)
5023 return t;
5024 else
5025 {
5026 r = get_target_expr (r);
5027 TREE_CONSTANT (r) = true;
5028 return r;
5029 }
5030 }
5031 else
5032 return r;
5033}
5034
5035/* Returns true if T is a valid subexpression of a constant expression,
5036 even if it isn't itself a constant expression. */
5037
5038bool
5039is_sub_constant_expr (tree t)
5040{
5041 bool non_constant_p = false;
5042 bool overflow_p = false;
cf72f34d 5043 hash_map <tree, tree> map;
2631cb67 5044
2a2770c6 5045 constexpr_ctx ctx = { NULL, &map, NULL, NULL, NULL, NULL, true, true };
2631cb67 5046
6e1bbaae 5047 instantiate_constexpr_fns (t);
f83e6885 5048 cxx_eval_constant_expression (&ctx, t, false, &non_constant_p,
b2a43197 5049 &overflow_p);
09b42213 5050 return !non_constant_p && !overflow_p;
5051}
5052
5053/* If T represents a constant expression returns its reduced value.
5054 Otherwise return error_mark_node. If T is dependent, then
5055 return NULL. */
5056
5057tree
cf72f34d 5058cxx_constant_value (tree t, tree decl)
09b42213 5059{
2055d27a 5060 return cxx_eval_outermost_constant_expr (t, false, true, decl);
09b42213 5061}
5062
d2c63826 5063/* Helper routine for fold_simple function. Either return simplified
5064 expression T, otherwise NULL_TREE.
5065 In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold
5066 even if we are within template-declaration. So be careful on call, as in
5067 such case types can be undefined. */
5068
5069static tree
5070fold_simple_1 (tree t)
5071{
5072 tree op1;
5073 enum tree_code code = TREE_CODE (t);
5074
5075 switch (code)
5076 {
5077 case INTEGER_CST:
5078 case REAL_CST:
5079 case VECTOR_CST:
5080 case FIXED_CST:
5081 case COMPLEX_CST:
5082 return t;
5083
5084 case SIZEOF_EXPR:
5085 return fold_sizeof_expr (t);
5086
5087 case ABS_EXPR:
19b4138e 5088 case ABSU_EXPR:
d2c63826 5089 case CONJ_EXPR:
5090 case REALPART_EXPR:
5091 case IMAGPART_EXPR:
5092 case NEGATE_EXPR:
5093 case BIT_NOT_EXPR:
5094 case TRUTH_NOT_EXPR:
5095 case NOP_EXPR:
5096 case VIEW_CONVERT_EXPR:
5097 case CONVERT_EXPR:
5098 case FLOAT_EXPR:
5099 case FIX_TRUNC_EXPR:
5100 case FIXED_CONVERT_EXPR:
5101 case ADDR_SPACE_CONVERT_EXPR:
5102
5103 op1 = TREE_OPERAND (t, 0);
5104
5105 t = const_unop (code, TREE_TYPE (t), op1);
5106 if (!t)
5107 return NULL_TREE;
5108
5109 if (CONVERT_EXPR_CODE_P (code)
5110 && TREE_OVERFLOW_P (t) && !TREE_OVERFLOW_P (op1))
5111 TREE_OVERFLOW (t) = false;
5112 return t;
5113
5114 default:
5115 return NULL_TREE;
5116 }
5117}
5118
5119/* If T is a simple constant expression, returns its simplified value.
6fe5fea9 5120 Otherwise returns T. In contrast to maybe_constant_value we
d2c63826 5121 simplify only few operations on constant-expressions, and we don't
5122 try to simplify constexpressions. */
5123
5124tree
5125fold_simple (tree t)
5126{
d2c63826 5127 if (processing_template_decl)
5128 return t;
5129
6fe5fea9 5130 tree r = fold_simple_1 (t);
5131 if (r)
5132 return r;
d2c63826 5133
6fe5fea9 5134 return t;
d2c63826 5135}
5136
09b42213 5137/* If T is a constant expression, returns its reduced value.
5138 Otherwise, if T does not have TREE_CONSTANT set, returns T.
5139 Otherwise, returns a version of T without TREE_CONSTANT. */
5140
b601ad30 5141static GTY((deletable)) hash_map<tree, tree> *cv_cache;
5142
5143tree
5144maybe_constant_value (tree t, tree decl)
09b42213 5145{
5146 tree r;
5147
7e1f8be4 5148 if (!is_nondependent_constant_expression (t))
09b42213 5149 {
5150 if (TREE_OVERFLOW_P (t))
5151 {
5152 t = build_nop (TREE_TYPE (t), t);
5153 TREE_CONSTANT (t) = false;
5154 }
5155 return t;
5156 }
b601ad30 5157 else if (CONSTANT_CLASS_P (t))
5158 /* No caching or evaluation needed. */
5159 return t;
5160
5161 if (cv_cache == NULL)
5162 cv_cache = hash_map<tree, tree>::create_ggc (101);
5163 if (tree *cached = cv_cache->get (t))
5164 return *cached;
09b42213 5165
2055d27a 5166 r = cxx_eval_outermost_constant_expr (t, true, true, decl);
5e8689fb 5167 gcc_checking_assert (r == t
5168 || CONVERT_EXPR_P (t)
5169 || TREE_CODE (t) == VIEW_CONVERT_EXPR
5170 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
5171 || !cp_tree_equal (r, t));
b601ad30 5172 cv_cache->put (t, r);
09b42213 5173 return r;
5174}
5175
2a655a4c 5176/* Dispose of the whole CV_CACHE. */
5177
5178static void
5179clear_cv_cache (void)
5180{
5181 if (cv_cache != NULL)
5182 cv_cache->empty ();
5183}
5184
f553d9f8 5185/* Dispose of the whole CV_CACHE and FOLD_CACHE. */
a0c919f7 5186
5187void
f553d9f8 5188clear_cv_and_fold_caches (void)
a0c919f7 5189{
2a655a4c 5190 clear_cv_cache ();
f553d9f8 5191 clear_fold_cache ();
a0c919f7 5192}
5193
21131a05 5194/* Like maybe_constant_value but first fully instantiate the argument.
5195
5196 Note: this is equivalent to instantiate_non_dependent_expr_sfinae
006b503a 5197 (t, complain) followed by maybe_constant_value but is more efficient,
5198 because it calls instantiation_dependent_expression_p and
5199 potential_constant_expression at most once.
5200
5201 Callers should generally pass their active complain, or if they are in a
5202 non-template, diagnosing context, they can use the default of
5203 tf_warning_or_error. Callers that might be within a template context, don't
5204 have a complain parameter, and aren't going to remember the result for long
5205 (e.g. null_ptr_cst_p), can pass tf_none and deal with error_mark_node
5206 appropriately. */
21131a05 5207
5208tree
006b503a 5209fold_non_dependent_expr (tree t,
5210 tsubst_flags_t complain /* = tf_warning_or_error */)
21131a05 5211{
5212 if (t == NULL_TREE)
5213 return NULL_TREE;
5214
5215 /* If we're in a template, but T isn't value dependent, simplify
5216 it. We're supposed to treat:
5217
5218 template <typename T> void f(T[1 + 1]);
5219 template <typename T> void f(T[2]);
5220
5221 as two declarations of the same function, for example. */
5222 if (processing_template_decl)
5223 {
7e1f8be4 5224 if (is_nondependent_constant_expression (t))
21131a05 5225 {
c8a9f04c 5226 processing_template_decl_sentinel s;
006b503a 5227 t = instantiate_non_dependent_expr_internal (t, complain);
21131a05 5228
5229 if (type_unknown_p (t)
5230 || BRACE_ENCLOSED_INITIALIZER_P (t))
5231 {
5232 if (TREE_OVERFLOW_P (t))
5233 {
5234 t = build_nop (TREE_TYPE (t), t);
5235 TREE_CONSTANT (t) = false;
5236 }
5237 return t;
5238 }
5239
2055d27a 5240 tree r = cxx_eval_outermost_constant_expr (t, true, true, NULL_TREE);
21131a05 5241 /* cp_tree_equal looks through NOPs, so allow them. */
5e8689fb 5242 gcc_checking_assert (r == t
5243 || CONVERT_EXPR_P (t)
5244 || TREE_CODE (t) == VIEW_CONVERT_EXPR
5245 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
5246 || !cp_tree_equal (r, t));
21131a05 5247 return r;
5248 }
5249 else if (TREE_OVERFLOW_P (t))
5250 {
5251 t = build_nop (TREE_TYPE (t), t);
5252 TREE_CONSTANT (t) = false;
5253 }
5254 return t;
5255 }
5256
5257 return maybe_constant_value (t);
5258}
5259
09b42213 5260/* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
5261 than wrapped in a TARGET_EXPR. */
5262
0c703e14 5263static tree
5264maybe_constant_init_1 (tree t, tree decl, bool allow_non_constant)
09b42213 5265{
d2c63826 5266 if (!t)
5267 return t;
09b42213 5268 if (TREE_CODE (t) == EXPR_STMT)
5269 t = TREE_OPERAND (t, 0);
5270 if (TREE_CODE (t) == CONVERT_EXPR
5271 && VOID_TYPE_P (TREE_TYPE (t)))
5272 t = TREE_OPERAND (t, 0);
9c96033c 5273 if (TREE_CODE (t) == INIT_EXPR)
5274 t = TREE_OPERAND (t, 1);
c6d97004 5275 if (TREE_CODE (t) == TARGET_EXPR)
5276 t = TARGET_EXPR_INITIAL (t);
7e1f8be4 5277 if (!is_nondependent_static_init_expression (t))
2055d27a 5278 /* Don't try to evaluate it. */;
0c703e14 5279 else if (CONSTANT_CLASS_P (t) && allow_non_constant)
b601ad30 5280 /* No evaluation needed. */;
2055d27a 5281 else
0c703e14 5282 t = cxx_eval_outermost_constant_expr (t, allow_non_constant, false, decl);
09b42213 5283 if (TREE_CODE (t) == TARGET_EXPR)
5284 {
5285 tree init = TARGET_EXPR_INITIAL (t);
5286 if (TREE_CODE (init) == CONSTRUCTOR)
5287 t = init;
5288 }
5289 return t;
5290}
5291
0c703e14 5292/* Wrapper for maybe_constant_init_1 which permits non constants. */
5293
5294tree
5295maybe_constant_init (tree t, tree decl)
5296{
5297 return maybe_constant_init_1 (t, decl, true);
5298}
5299
5300/* Wrapper for maybe_constant_init_1 which does not permit non constants. */
5301
5302tree
5303cxx_constant_init (tree t, tree decl)
5304{
5305 return maybe_constant_init_1 (t, decl, false);
5306}
5307
09b42213 5308#if 0
5309/* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
5310/* Return true if the object referred to by REF has automatic or thread
5311 local storage. */
5312
5313enum { ck_ok, ck_bad, ck_unknown };
5314static int
5315check_automatic_or_tls (tree ref)
5316{
3754d046 5317 machine_mode mode;
81bc0f0f 5318 poly_int64 bitsize, bitpos;
09b42213 5319 tree offset;
5320 int volatilep = 0, unsignedp = 0;
5321 tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset,
5322 &mode, &unsignedp, &volatilep, false);
5323 duration_kind dk;
5324
5325 /* If there isn't a decl in the middle, we don't know the linkage here,
5326 and this isn't a constant expression anyway. */
5327 if (!DECL_P (decl))
5328 return ck_unknown;
5329 dk = decl_storage_duration (decl);
5330 return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok;
5331}
5332#endif
5333
5334/* Return true if T denotes a potentially constant expression. Issue
5335 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
7e1f8be4 5336 an lvalue-rvalue conversion is implied. If NOW is true, we want to
5337 consider the expression in the current context, independent of constexpr
5338 substitution.
09b42213 5339
5340 C++0x [expr.const] used to say
5341
5342 6 An expression is a potential constant expression if it is
5343 a constant expression where all occurrences of function
5344 parameters are replaced by arbitrary constant expressions
5345 of the appropriate type.
5346
5347 2 A conditional expression is a constant expression unless it
5348 involves one of the following as a potentially evaluated
5349 subexpression (3.2), but subexpressions of logical AND (5.14),
5350 logical OR (5.15), and conditional (5.16) operations that are
5351 not evaluated are not considered. */
5352
5353static bool
7e1f8be4 5354potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
2055d27a 5355 tsubst_flags_t flags)
09b42213 5356{
7e1f8be4 5357#define RECUR(T,RV) \
5358 potential_constant_expression_1 ((T), (RV), strict, now, flags)
5359
09b42213 5360 enum { any = false, rval = true };
5361 int i;
5362 tree tmp;
5363
5364 if (t == error_mark_node)
5365 return false;
5366 if (t == NULL_TREE)
5367 return true;
d3a3cfb8 5368 location_t loc = cp_expr_loc_or_loc (t, input_location);
eade0940 5369 if (TREE_THIS_VOLATILE (t) && !DECL_P (t))
09b42213 5370 {
5371 if (flags & tf_error)
d6a08f5c 5372 error_at (loc, "expression %qE has side-effects", t);
09b42213 5373 return false;
5374 }
5375 if (CONSTANT_CLASS_P (t))
5376 return true;
c7282bf2 5377 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED)
5378 && TREE_TYPE (t) == error_mark_node)
5379 return false;
09b42213 5380
5381 switch (TREE_CODE (t))
5382 {
5383 case FUNCTION_DECL:
5384 case BASELINK:
5385 case TEMPLATE_DECL:
5386 case OVERLOAD:
5387 case TEMPLATE_ID_EXPR:
5388 case LABEL_DECL:
5389 case LABEL_EXPR:
9c96033c 5390 case CASE_LABEL_EXPR:
09b42213 5391 case CONST_DECL:
5392 case SIZEOF_EXPR:
5393 case ALIGNOF_EXPR:
5394 case OFFSETOF_EXPR:
5395 case NOEXCEPT_EXPR:
5396 case TEMPLATE_PARM_INDEX:
5397 case TRAIT_EXPR:
5398 case IDENTIFIER_NODE:
5399 case USERDEF_LITERAL:
5400 /* We can see a FIELD_DECL in a pointer-to-member expression. */
5401 case FIELD_DECL:
d2c63826 5402 case RESULT_DECL:
09b42213 5403 case USING_DECL:
9c96033c 5404 case USING_STMT:
cf72f34d 5405 case PLACEHOLDER_EXPR:
9c96033c 5406 case BREAK_STMT:
5407 case CONTINUE_STMT:
56c12fd4 5408 case REQUIRES_EXPR:
33603066 5409 case STATIC_ASSERT:
90567983 5410 case DEBUG_BEGIN_STMT:
09b42213 5411 return true;
5412
7e1f8be4 5413 case PARM_DECL:
5414 if (now)
5415 {
5416 if (flags & tf_error)
5417 error ("%qE is not a constant expression", t);
5418 return false;
5419 }
5420 return true;
5421
09b42213 5422 case AGGR_INIT_EXPR:
5423 case CALL_EXPR:
5424 /* -- an invocation of a function other than a constexpr function
5425 or a constexpr constructor. */
5426 {
5427 tree fun = get_function_named_in_call (t);
5428 const int nargs = call_expr_nargs (t);
5429 i = 0;
5430
9c96033c 5431 if (fun == NULL_TREE)
5432 {
732905bb 5433 /* Reset to allow the function to continue past the end
5434 of the block below. Otherwise return early. */
5435 bool bail = true;
5436
32cf7025 5437 if (TREE_CODE (t) == CALL_EXPR
5438 && CALL_EXPR_FN (t) == NULL_TREE)
5439 switch (CALL_EXPR_IFN (t))
5440 {
5441 /* These should be ignored, they are optimized away from
5442 constexpr functions. */
5443 case IFN_UBSAN_NULL:
5444 case IFN_UBSAN_BOUNDS:
5445 case IFN_UBSAN_VPTR:
3c77f69c 5446 case IFN_FALLTHROUGH:
32cf7025 5447 return true;
732905bb 5448
5449 case IFN_ADD_OVERFLOW:
5450 case IFN_SUB_OVERFLOW:
5451 case IFN_MUL_OVERFLOW:
6e1b2ffb 5452 case IFN_LAUNDER:
732905bb 5453 bail = false;
5454
32cf7025 5455 default:
5456 break;
5457 }
732905bb 5458
5459 if (bail)
5460 {
5461 /* fold_call_expr can't do anything with IFN calls. */
5462 if (flags & tf_error)
d6a08f5c 5463 error_at (loc, "call to internal function %qE", t);
732905bb 5464 return false;
5465 }
9c96033c 5466 }
732905bb 5467
5468 if (fun && is_overloaded_fn (fun))
09b42213 5469 {
5470 if (TREE_CODE (fun) == FUNCTION_DECL)
5471 {
5472 if (builtin_valid_in_constant_expr_p (fun))
5473 return true;
5474 if (!DECL_DECLARED_CONSTEXPR_P (fun)
5475 /* Allow any built-in function; if the expansion
5476 isn't constant, we'll deal with that then. */
5477 && !is_builtin_fn (fun))
5478 {
5479 if (flags & tf_error)
5480 {
5967b28b 5481 error_at (loc, "call to non-%<constexpr%> function %qD",
d6a08f5c 5482 fun);
09b42213 5483 explain_invalid_constexpr_fn (fun);
5484 }
5485 return false;
5486 }
5487 /* A call to a non-static member function takes the address
5488 of the object as the first argument. But in a constant
5489 expression the address will be folded away, so look
5490 through it now. */
5491 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
5492 && !DECL_CONSTRUCTOR_P (fun))
5493 {
5494 tree x = get_nth_callarg (t, 0);
5495 if (is_this_parameter (x))
cf72f34d 5496 return true;
7e1f8be4 5497 /* Don't require an immediately constant value, as
5498 constexpr substitution might not use the value. */
5499 bool sub_now = false;
5500 if (!potential_constant_expression_1 (x, rval, strict,
5501 sub_now, flags))
09b42213 5502 return false;
5503 i = 1;
5504 }
5505 }
5506 else
5507 {
2055d27a 5508 if (!RECUR (fun, true))
09b42213 5509 return false;
5510 fun = get_first_fn (fun);
5511 }
5512 /* Skip initial arguments to base constructors. */
5513 if (DECL_BASE_CONSTRUCTOR_P (fun))
5514 i = num_artificial_parms_for (fun);
5515 fun = DECL_ORIGIN (fun);
5516 }
732905bb 5517 else if (fun)
09b42213 5518 {
2055d27a 5519 if (RECUR (fun, rval))
09b42213 5520 /* Might end up being a constant function pointer. */;
5521 else
5522 return false;
5523 }
5524 for (; i < nargs; ++i)
5525 {
5526 tree x = get_nth_callarg (t, i);
b2742b58 5527 /* In a template, reference arguments haven't been converted to
5528 REFERENCE_TYPE and we might not even know if the parameter
5529 is a reference, so accept lvalue constants too. */
5530 bool rv = processing_template_decl ? any : rval;
7e1f8be4 5531 /* Don't require an immediately constant value, as constexpr
5532 substitution might not use the value of the argument. */
5533 bool sub_now = false;
5534 if (!potential_constant_expression_1 (x, rv, strict,
5535 sub_now, flags))
09b42213 5536 return false;
5537 }
5538 return true;
5539 }
5540
5541 case NON_LVALUE_EXPR:
5542 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
5543 -- an lvalue of integral type that refers to a non-volatile
5544 const variable or static data member initialized with
5545 constant expressions, or
5546
5547 -- an lvalue of literal type that refers to non-volatile
5548 object defined with constexpr, or that refers to a
5549 sub-object of such an object; */
2055d27a 5550 return RECUR (TREE_OPERAND (t, 0), rval);
09b42213 5551
5552 case VAR_DECL:
9c8aeb66 5553 if (DECL_HAS_VALUE_EXPR_P (t))
297de7bc 5554 {
d0e2b7e7 5555 if (now && is_normal_capture_proxy (t))
297de7bc 5556 {
5557 /* -- in a lambda-expression, a reference to this or to a
5558 variable with automatic storage duration defined outside that
5559 lambda-expression, where the reference would be an
5560 odr-use. */
5561 if (flags & tf_error)
5562 {
5563 tree cap = DECL_CAPTURED_VARIABLE (t);
5564 error ("lambda capture of %qE is not a constant expression",
5565 cap);
5566 if (!want_rval && decl_constant_var_p (cap))
5567 inform (input_location, "because it is used as a glvalue");
5568 }
5569 return false;
5570 }
5571 return RECUR (DECL_VALUE_EXPR (t), rval);
5572 }
2055d27a 5573 if (want_rval
33603066 5574 && !var_in_maybe_constexpr_fn (t)
c7282bf2 5575 && !type_dependent_expression_p (t)
8d6486e1 5576 && !decl_maybe_constant_var_p (t)
2055d27a 5577 && (strict
5578 || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t))
8d6486e1 5579 || (DECL_INITIAL (t)
5580 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t)))
c7282bf2 5581 && COMPLETE_TYPE_P (TREE_TYPE (t))
5582 && !is_really_empty_class (TREE_TYPE (t)))
09b42213 5583 {
5584 if (flags & tf_error)
5585 non_const_var_error (t);
5586 return false;
5587 }
5588 return true;
5589
5590 case NOP_EXPR:
5591 case CONVERT_EXPR:
5592 case VIEW_CONVERT_EXPR:
5593 /* -- a reinterpret_cast. FIXME not implemented, and this rule
5594 may change to something more specific to type-punning (DR 1312). */
5595 {
5596 tree from = TREE_OPERAND (t, 0);
d03fa520 5597 if (INDIRECT_TYPE_P (TREE_TYPE (t))
09b42213 5598 && TREE_CODE (from) == INTEGER_CST
5599 && !integer_zerop (from))
5600 {
5601 if (flags & tf_error)
d6a08f5c 5602 error_at (loc, "reinterpret_cast from integer to pointer");
09b42213 5603 return false;
5604 }
2055d27a 5605 return (RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR));
09b42213 5606 }
5607
cd45162d 5608 case ADDRESSOF_EXPR:
5609 /* This is like ADDR_EXPR, except it won't form pointer-to-member. */
5610 t = TREE_OPERAND (t, 0);
5611 goto handle_addr_expr;
5612
09b42213 5613 case ADDR_EXPR:
5614 /* -- a unary operator & that is applied to an lvalue that
5615 designates an object with thread or automatic storage
5616 duration; */
5617 t = TREE_OPERAND (t, 0);
5618
5619 if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t))
5620 /* A pointer-to-member constant. */
5621 return true;
5622
cd45162d 5623 handle_addr_expr:
09b42213 5624#if 0
5625 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
5626 any checking here, as we might dereference the pointer later. If
5627 we remove this code, also remove check_automatic_or_tls. */
5628 i = check_automatic_or_tls (t);
5629 if (i == ck_ok)
5630 return true;
5631 if (i == ck_bad)
5632 {
5633 if (flags & tf_error)
5634 error ("address-of an object %qE with thread local or "
5635 "automatic storage is not a constant expression", t);
5636 return false;
5637 }
5638#endif
2055d27a 5639 return RECUR (t, any);
09b42213 5640
20b34a56 5641 case REALPART_EXPR:
5642 case IMAGPART_EXPR:
09b42213 5643 case COMPONENT_REF:
5644 case BIT_FIELD_REF:
5645 case ARROW_EXPR:
5646 case OFFSET_REF:
5647 /* -- a class member access unless its postfix-expression is
5648 of literal type or of pointer to literal type. */
5649 /* This test would be redundant, as it follows from the
5650 postfix-expression being a potential constant expression. */
eade0940 5651 if (type_unknown_p (t))
5652 return true;
2055d27a 5653 return RECUR (TREE_OPERAND (t, 0), want_rval);
09b42213 5654
5655 case EXPR_PACK_EXPANSION:
2055d27a 5656 return RECUR (PACK_EXPANSION_PATTERN (t), want_rval);
09b42213 5657
5658 case INDIRECT_REF:
5659 {
5660 tree x = TREE_OPERAND (t, 0);
5661 STRIP_NOPS (x);
997bbf70 5662 if (is_this_parameter (x) && !is_capture_proxy (x))
09b42213 5663 {
b7e4a558 5664 if (!var_in_maybe_constexpr_fn (x))
09b42213 5665 {
5666 if (flags & tf_error)
d6a08f5c 5667 error_at (loc, "use of %<this%> in a constant expression");
09b42213 5668 return false;
5669 }
09b42213 5670 return true;
5671 }
2055d27a 5672 return RECUR (x, rval);
09b42213 5673 }
5674
9c96033c 5675 case STATEMENT_LIST:
5676 {
5677 tree_stmt_iterator i;
5678 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
5679 {
2055d27a 5680 if (!RECUR (tsi_stmt (i), any))
9c96033c 5681 return false;
5682 }
5683 return true;
5684 }
5685 break;
5686
5687 case MODIFY_EXPR:
5688 if (cxx_dialect < cxx14)
5689 goto fail;
2055d27a 5690 if (!RECUR (TREE_OPERAND (t, 0), any))
9c96033c 5691 return false;
2055d27a 5692 if (!RECUR (TREE_OPERAND (t, 1), rval))
9c96033c 5693 return false;
5694 return true;
5695
5696 case MODOP_EXPR:
5697 if (cxx_dialect < cxx14)
5698 goto fail;
2055d27a 5699 if (!RECUR (TREE_OPERAND (t, 0), rval))
9c96033c 5700 return false;
2055d27a 5701 if (!RECUR (TREE_OPERAND (t, 2), rval))
9c96033c 5702 return false;
5703 return true;
5704
9c96033c 5705 case DO_STMT:
2055d27a 5706 if (!RECUR (DO_COND (t), rval))
9c96033c 5707 return false;
2055d27a 5708 if (!RECUR (DO_BODY (t), any))
9c96033c 5709 return false;
5710 return true;
5711
5712 case FOR_STMT:
2055d27a 5713 if (!RECUR (FOR_INIT_STMT (t), any))
9c96033c 5714 return false;
2055d27a 5715 if (!RECUR (FOR_COND (t), rval))
9c96033c 5716 return false;
2055d27a 5717 if (!RECUR (FOR_EXPR (t), any))
9c96033c 5718 return false;
2055d27a 5719 if (!RECUR (FOR_BODY (t), any))
9c96033c 5720 return false;
5721 return true;
5722
33603066 5723 case RANGE_FOR_STMT:
5724 if (!RECUR (RANGE_FOR_EXPR (t), any))
5725 return false;
5726 if (!RECUR (RANGE_FOR_BODY (t), any))
5727 return false;
5728 return true;
5729
9c96033c 5730 case WHILE_STMT:
2055d27a 5731 if (!RECUR (WHILE_COND (t), rval))
9c96033c 5732 return false;
2055d27a 5733 if (!RECUR (WHILE_BODY (t), any))
9c96033c 5734 return false;
5735 return true;
5736
5737 case SWITCH_STMT:
2055d27a 5738 if (!RECUR (SWITCH_STMT_COND (t), rval))
9c96033c 5739 return false;
748c426a 5740 /* FIXME we don't check SWITCH_STMT_BODY currently, because even
5741 unreachable labels would be checked. */
9c96033c 5742 return true;
5743
da7981e0 5744 case STMT_EXPR:
2055d27a 5745 return RECUR (STMT_EXPR_STMT (t), rval);
da7981e0 5746
09b42213 5747 case LAMBDA_EXPR:
27db1f50 5748 if (cxx_dialect >= cxx17)
5749 /* In C++17 lambdas can be constexpr, don't give up yet. */
5750 return true;
5751 else if (flags & tf_error)
5752 error_at (loc, "lambda-expression is not a constant expression "
5753 "before C++17");
5754 return false;
5755
09b42213 5756 case DYNAMIC_CAST_EXPR:
5757 case PSEUDO_DTOR_EXPR:
09b42213 5758 case NEW_EXPR:
5759 case VEC_NEW_EXPR:
5760 case DELETE_EXPR:
5761 case VEC_DELETE_EXPR:
5762 case THROW_EXPR:
d6a08f5c 5763 case OMP_PARALLEL:
5764 case OMP_TASK:
5765 case OMP_FOR:
83728168 5766 case OMP_SIMD:
d6a08f5c 5767 case OMP_DISTRIBUTE:
5768 case OMP_TASKLOOP:
5769 case OMP_TEAMS:
5770 case OMP_TARGET_DATA:
5771 case OMP_TARGET:
5772 case OMP_SECTIONS:
5773 case OMP_ORDERED:
5774 case OMP_CRITICAL:
5775 case OMP_SINGLE:
5776 case OMP_SECTION:
5777 case OMP_MASTER:
5778 case OMP_TASKGROUP:
5779 case OMP_TARGET_UPDATE:
5780 case OMP_TARGET_ENTER_DATA:
5781 case OMP_TARGET_EXIT_DATA:
09b42213 5782 case OMP_ATOMIC:
5783 case OMP_ATOMIC_READ:
5784 case OMP_ATOMIC_CAPTURE_OLD:
5785 case OMP_ATOMIC_CAPTURE_NEW:
d6a08f5c 5786 case OACC_PARALLEL:
5787 case OACC_KERNELS:
5788 case OACC_DATA:
5789 case OACC_HOST_DATA:
5790 case OACC_LOOP:
5791 case OACC_CACHE:
5792 case OACC_DECLARE:
5793 case OACC_ENTER_DATA:
5794 case OACC_EXIT_DATA:
5795 case OACC_UPDATE:
09b42213 5796 /* GCC internal stuff. */
5797 case VA_ARG_EXPR:
5798 case OBJ_TYPE_REF:
09b42213 5799 case TRANSACTION_EXPR:
9c96033c 5800 case ASM_EXPR:
9abecca2 5801 case AT_ENCODE_EXPR:
9c96033c 5802 fail:
09b42213 5803 if (flags & tf_error)
d6a08f5c 5804 error_at (loc, "expression %qE is not a constant expression", t);
09b42213 5805 return false;
5806
5807 case TYPEID_EXPR:
5808 /* -- a typeid expression whose operand is of polymorphic
5809 class type; */
5810 {
5811 tree e = TREE_OPERAND (t, 0);
5812 if (!TYPE_P (e) && !type_dependent_expression_p (e)
5813 && TYPE_POLYMORPHIC_P (TREE_TYPE (e)))
5814 {
5815 if (flags & tf_error)
d6a08f5c 5816 error_at (loc, "typeid-expression is not a constant expression "
5817 "because %qE is of polymorphic type", e);
09b42213 5818 return false;
5819 }
5820 return true;
5821 }
5822
57e83b58 5823 case POINTER_DIFF_EXPR:
09b42213 5824 case MINUS_EXPR:
09b42213 5825 want_rval = true;
5826 goto binary;
5827
5828 case LT_EXPR:
5829 case LE_EXPR:
5830 case GT_EXPR:
5831 case GE_EXPR:
5832 case EQ_EXPR:
5833 case NE_EXPR:
09b42213 5834 want_rval = true;
5835 goto binary;
5836
9c96033c 5837 case PREINCREMENT_EXPR:
5838 case POSTINCREMENT_EXPR:
5839 case PREDECREMENT_EXPR:
5840 case POSTDECREMENT_EXPR:
5841 if (cxx_dialect < cxx14)
5842 goto fail;
5843 goto unary;
5844
09b42213 5845 case BIT_NOT_EXPR:
5846 /* A destructor. */
5847 if (TYPE_P (TREE_OPERAND (t, 0)))
5848 return true;
e3533433 5849 /* fall through. */
09b42213 5850
09b42213 5851 case CONJ_EXPR:
5852 case SAVE_EXPR:
5853 case FIX_TRUNC_EXPR:
5854 case FLOAT_EXPR:
5855 case NEGATE_EXPR:
5856 case ABS_EXPR:
1c67942e 5857 case ABSU_EXPR:
09b42213 5858 case TRUTH_NOT_EXPR:
5859 case FIXED_CONVERT_EXPR:
5860 case UNARY_PLUS_EXPR:
6fdf70f5 5861 case UNARY_LEFT_FOLD_EXPR:
5862 case UNARY_RIGHT_FOLD_EXPR:
9c96033c 5863 unary:
2055d27a 5864 return RECUR (TREE_OPERAND (t, 0), rval);
09b42213 5865
5866 case CAST_EXPR:
5867 case CONST_CAST_EXPR:
5868 case STATIC_CAST_EXPR:
5869 case REINTERPRET_CAST_EXPR:
5870 case IMPLICIT_CONV_EXPR:
5871 if (cxx_dialect < cxx11
5872 && !dependent_type_p (TREE_TYPE (t))
5873 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t)))
5874 /* In C++98, a conversion to non-integral type can't be part of a
5875 constant expression. */
5876 {
5877 if (flags & tf_error)
d6a08f5c 5878 error_at (loc,
5879 "cast to non-integral type %qT in a constant expression",
5880 TREE_TYPE (t));
09b42213 5881 return false;
5882 }
e0ccd480 5883 /* This might be a conversion from a class to a (potentially) literal
5884 type. Let's consider it potentially constant since the conversion
5885 might be a constexpr user-defined conversion. */
5886 else if (cxx_dialect >= cxx11
5887 && (dependent_type_p (TREE_TYPE (t))
5888 || !COMPLETE_TYPE_P (TREE_TYPE (t))
5889 || literal_type_p (TREE_TYPE (t)))
5890 && TREE_OPERAND (t, 0))
5891 {
5892 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
5893 /* If this is a dependent type, it could end up being a class
5894 with conversions. */
5895 if (type == NULL_TREE || WILDCARD_TYPE_P (type))
5896 return true;
5897 /* Or a non-dependent class which has conversions. */
5898 else if (CLASS_TYPE_P (type)
5899 && (TYPE_HAS_CONVERSION (type) || dependent_scope_p (type)))
5900 return true;
5901 }
09b42213 5902
2055d27a 5903 return (RECUR (TREE_OPERAND (t, 0),
90ad495b 5904 !TYPE_REF_P (TREE_TYPE (t))));
09b42213 5905
9c96033c 5906 case BIND_EXPR:
2055d27a 5907 return RECUR (BIND_EXPR_BODY (t), want_rval);
9c96033c 5908
9c96033c 5909 case CLEANUP_POINT_EXPR:
5910 case MUST_NOT_THROW_EXPR:
5911 case TRY_CATCH_EXPR:
7f67d68c 5912 case TRY_BLOCK:
9c96033c 5913 case EH_SPEC_BLOCK:
5914 case EXPR_STMT:
09b42213 5915 case PAREN_EXPR:
5916 case NON_DEPENDENT_EXPR:
5917 /* For convenience. */
5918 case RETURN_EXPR:
d1980671 5919 case LOOP_EXPR:
5920 case EXIT_EXPR:
2055d27a 5921 return RECUR (TREE_OPERAND (t, 0), want_rval);
09b42213 5922
33603066 5923 case DECL_EXPR:
5924 tmp = DECL_EXPR_DECL (t);
5925 if (VAR_P (tmp) && !DECL_ARTIFICIAL (tmp))
5926 {
5927 if (TREE_STATIC (tmp))
5928 {
5929 if (flags & tf_error)
5930 error_at (DECL_SOURCE_LOCATION (tmp), "%qD declared "
8d6486e1 5931 "%<static%> in %<constexpr%> context", tmp);
33603066 5932 return false;
5933 }
5934 else if (CP_DECL_THREAD_LOCAL_P (tmp))
5935 {
5936 if (flags & tf_error)
5937 error_at (DECL_SOURCE_LOCATION (tmp), "%qD declared "
8d6486e1 5938 "%<thread_local%> in %<constexpr%> context", tmp);
33603066 5939 return false;
5940 }
282ea908 5941 else if (!check_for_uninitialized_const_var
5942 (tmp, /*constexpr_context_p=*/true, flags))
5943 return false;
33603066 5944 }
5945 return RECUR (tmp, want_rval);
5946
7f67d68c 5947 case TRY_FINALLY_EXPR:
5948 return (RECUR (TREE_OPERAND (t, 0), want_rval)
5949 && RECUR (TREE_OPERAND (t, 1), any));
5950
09b42213 5951 case SCOPE_REF:
2055d27a 5952 return RECUR (TREE_OPERAND (t, 1), want_rval);
09b42213 5953
5954 case TARGET_EXPR:
07d8961c 5955 if (!TARGET_EXPR_DIRECT_INIT_P (t)
5956 && !literal_type_p (TREE_TYPE (t)))
09b42213 5957 {
5958 if (flags & tf_error)
5959 {
d6a08f5c 5960 error_at (loc, "temporary of non-literal type %qT in a "
5961 "constant expression", TREE_TYPE (t));
09b42213 5962 explain_non_literal_class (TREE_TYPE (t));
5963 }
5964 return false;
5965 }
e3533433 5966 /* FALLTHRU */
09b42213 5967 case INIT_EXPR:
2055d27a 5968 return RECUR (TREE_OPERAND (t, 1), rval);
09b42213 5969
5970 case CONSTRUCTOR:
5971 {
5972 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
5973 constructor_elt *ce;
5974 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
2055d27a 5975 if (!RECUR (ce->value, want_rval))
09b42213 5976 return false;
5977 return true;
5978 }
5979
5980 case TREE_LIST:
5981 {
5982 gcc_assert (TREE_PURPOSE (t) == NULL_TREE
5983 || DECL_P (TREE_PURPOSE (t)));
2055d27a 5984 if (!RECUR (TREE_VALUE (t), want_rval))
09b42213 5985 return false;
5986 if (TREE_CHAIN (t) == NULL_TREE)
5987 return true;
2055d27a 5988 return RECUR (TREE_CHAIN (t), want_rval);
09b42213 5989 }
5990
5991 case TRUNC_DIV_EXPR:
5992 case CEIL_DIV_EXPR:
5993 case FLOOR_DIV_EXPR:
5994 case ROUND_DIV_EXPR:
5995 case TRUNC_MOD_EXPR:
5996 case CEIL_MOD_EXPR:
5997 case ROUND_MOD_EXPR:
5998 {
5999 tree denom = TREE_OPERAND (t, 1);
2055d27a 6000 if (!RECUR (denom, rval))
09b42213 6001 return false;
6002 /* We can't call cxx_eval_outermost_constant_expr on an expression
21131a05 6003 that hasn't been through instantiate_non_dependent_expr yet. */
09b42213 6004 if (!processing_template_decl)
6005 denom = cxx_eval_outermost_constant_expr (denom, true);
6006 if (integer_zerop (denom))
6007 {
6008 if (flags & tf_error)
ea034e2c 6009 error ("division by zero is not a constant expression");
09b42213 6010 return false;
6011 }
6012 else
6013 {
6014 want_rval = true;
2055d27a 6015 return RECUR (TREE_OPERAND (t, 0), want_rval);
09b42213 6016 }
6017 }
6018
6019 case COMPOUND_EXPR:
6020 {
6021 /* check_return_expr sometimes wraps a TARGET_EXPR in a
6022 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
6023 introduced by build_call_a. */
6024 tree op0 = TREE_OPERAND (t, 0);
6025 tree op1 = TREE_OPERAND (t, 1);
6026 STRIP_NOPS (op1);
6027 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
6028 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
2055d27a 6029 return RECUR (op0, want_rval);
09b42213 6030 else
6031 goto binary;
6032 }
6033
6034 /* If the first operand is the non-short-circuit constant, look at
6035 the second operand; otherwise we only care about the first one for
6036 potentiality. */
6037 case TRUTH_AND_EXPR:
6038 case TRUTH_ANDIF_EXPR:
6039 tmp = boolean_true_node;
6040 goto truth;
6041 case TRUTH_OR_EXPR:
6042 case TRUTH_ORIF_EXPR:
6043 tmp = boolean_false_node;
6044 truth:
6045 {
6046 tree op = TREE_OPERAND (t, 0);
2055d27a 6047 if (!RECUR (op, rval))
09b42213 6048 return false;
6049 if (!processing_template_decl)
6050 op = cxx_eval_outermost_constant_expr (op, true);
6051 if (tree_int_cst_equal (op, tmp))
2055d27a 6052 return RECUR (TREE_OPERAND (t, 1), rval);
09b42213 6053 else
6054 return true;
6055 }
6056
6057 case PLUS_EXPR:
6058 case MULT_EXPR:
6059 case POINTER_PLUS_EXPR:
6060 case RDIV_EXPR:
6061 case EXACT_DIV_EXPR:
6062 case MIN_EXPR:
6063 case MAX_EXPR:
6064 case LSHIFT_EXPR:
6065 case RSHIFT_EXPR:
6066 case LROTATE_EXPR:
6067 case RROTATE_EXPR:
6068 case BIT_IOR_EXPR:
6069 case BIT_XOR_EXPR:
6070 case BIT_AND_EXPR:
6071 case TRUTH_XOR_EXPR:
6072 case UNORDERED_EXPR:
6073 case ORDERED_EXPR:
6074 case UNLT_EXPR:
6075 case UNLE_EXPR:
6076 case UNGT_EXPR:
6077 case UNGE_EXPR:
6078 case UNEQ_EXPR:
6079 case LTGT_EXPR:
6080 case RANGE_EXPR:
6081 case COMPLEX_EXPR:
6082 want_rval = true;
6083 /* Fall through. */
6084 case ARRAY_REF:
6085 case ARRAY_RANGE_REF:
6086 case MEMBER_REF:
6087 case DOTSTAR_EXPR:
f0abe78f 6088 case MEM_REF:
6fdf70f5 6089 case BINARY_LEFT_FOLD_EXPR:
6090 case BINARY_RIGHT_FOLD_EXPR:
09b42213 6091 binary:
6092 for (i = 0; i < 2; ++i)
2055d27a 6093 if (!RECUR (TREE_OPERAND (t, i), want_rval))
09b42213 6094 return false;
6095 return true;
6096
09b42213 6097 case VEC_PERM_EXPR:
6098 for (i = 0; i < 3; ++i)
2055d27a 6099 if (!RECUR (TREE_OPERAND (t, i), true))
09b42213 6100 return false;
6101 return true;
6102
6103 case COND_EXPR:
b0fe8b95 6104 if (COND_EXPR_IS_VEC_DELETE (t))
6105 {
6106 if (flags & tf_error)
d6a08f5c 6107 error_at (loc, "%<delete[]%> is not a constant expression");
b0fe8b95 6108 return false;
6109 }
6110 /* Fall through. */
6111 case IF_STMT:
09b42213 6112 case VEC_COND_EXPR:
6113 /* If the condition is a known constant, we know which of the legs we
6114 care about; otherwise we only require that the condition and
6115 either of the legs be potentially constant. */
6116 tmp = TREE_OPERAND (t, 0);
2055d27a 6117 if (!RECUR (tmp, rval))
09b42213 6118 return false;
6119 if (!processing_template_decl)
6120 tmp = cxx_eval_outermost_constant_expr (tmp, true);
6121 if (integer_zerop (tmp))
2055d27a 6122 return RECUR (TREE_OPERAND (t, 2), want_rval);
09b42213 6123 else if (TREE_CODE (tmp) == INTEGER_CST)
2055d27a 6124 return RECUR (TREE_OPERAND (t, 1), want_rval);
09b42213 6125 for (i = 1; i < 3; ++i)
6126 if (potential_constant_expression_1 (TREE_OPERAND (t, i),
7e1f8be4 6127 want_rval, strict, now, tf_none))
09b42213 6128 return true;
6129 if (flags & tf_error)
d6a08f5c 6130 error_at (loc, "expression %qE is not a constant expression", t);
09b42213 6131 return false;
6132
6133 case VEC_INIT_EXPR:
6134 if (VEC_INIT_EXPR_IS_CONSTEXPR (t))
6135 return true;
6136 if (flags & tf_error)
6137 {
d6a08f5c 6138 error_at (loc, "non-constant array initialization");
09b42213 6139 diagnose_non_constexpr_vec_init (t);
6140 }
6141 return false;
6142
db1ae94f 6143 case TYPE_DECL:
6144 case TAG_DEFN:
6145 /* We can see these in statement-expressions. */
6146 return true;
6147
fd130325 6148 case CLEANUP_STMT:
d2c63826 6149 case EMPTY_CLASS_EXPR:
38ef3642 6150 case PREDICT_EXPR:
d2c63826 6151 return false;
6152
d1980671 6153 case GOTO_EXPR:
6154 {
6155 tree *target = &TREE_OPERAND (t, 0);
d40a1eac 6156 /* Gotos representing break and continue are OK. */
6157 if (breaks (target) || continues (target))
6158 return true;
6159 if (flags & tf_error)
d6a08f5c 6160 error_at (loc, "%<goto%> is not a constant expression");
d40a1eac 6161 return false;
d1980671 6162 }
6163
24c6ee98 6164 case ANNOTATE_EXPR:
24c6ee98 6165 return RECUR (TREE_OPERAND (t, 0), rval);
6166
09b42213 6167 default:
6168 if (objc_is_property_ref (t))
6169 return false;
6170
6171 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t)));
d40a1eac 6172 gcc_unreachable ();
09b42213 6173 return false;
6174 }
2055d27a 6175#undef RECUR
09b42213 6176}
6177
6178/* The main entry point to the above. */
6179
6180bool
6181potential_constant_expression (tree t)
6182{
7e1f8be4 6183 return potential_constant_expression_1 (t, false, true, false, tf_none);
09b42213 6184}
6185
6186/* As above, but require a constant rvalue. */
6187
6188bool
6189potential_rvalue_constant_expression (tree t)
6190{
7e1f8be4 6191 return potential_constant_expression_1 (t, true, true, false, tf_none);
09b42213 6192}
6193
6194/* Like above, but complain about non-constant expressions. */
6195
6196bool
6197require_potential_constant_expression (tree t)
6198{
786721dc 6199 return potential_constant_expression_1 (t, false, true, false,
6200 tf_warning_or_error);
09b42213 6201}
6202
6203/* Cross product of the above. */
6204
6205bool
6206require_potential_rvalue_constant_expression (tree t)
6207{
786721dc 6208 return potential_constant_expression_1 (t, true, true, false,
6209 tf_warning_or_error);
6210}
6211
6212/* Like above, but don't consider PARM_DECL a potential_constant_expression. */
6213
6214bool
6215require_rvalue_constant_expression (tree t)
6216{
6217 return potential_constant_expression_1 (t, true, true, true,
6218 tf_warning_or_error);
7e1f8be4 6219}
6220
6221/* Like potential_constant_expression, but don't consider possible constexpr
6222 substitution of the current function. That is, PARM_DECL qualifies under
6223 potential_constant_expression, but not here.
6224
6225 This is basically what you can check when any actual constant values might
6226 be value-dependent. */
6227
6228bool
6229is_constant_expression (tree t)
6230{
6231 return potential_constant_expression_1 (t, false, true, true, tf_none);
6232}
6233
6234/* Like above, but complain about non-constant expressions. */
6235
6236bool
6237require_constant_expression (tree t)
6238{
6239 return potential_constant_expression_1 (t, false, true, true,
6240 tf_warning_or_error);
6241}
6242
6243/* Like is_constant_expression, but allow const variables that are not allowed
6244 under constexpr rules. */
6245
6246bool
6247is_static_init_expression (tree t)
6248{
6249 return potential_constant_expression_1 (t, false, false, true, tf_none);
09b42213 6250}
6251
29cf24ec 6252/* Returns true if T is a potential constant expression that is not
6253 instantiation-dependent, and therefore a candidate for constant folding even
6254 in a template. */
6255
6256bool
7e1f8be4 6257is_nondependent_constant_expression (tree t)
29cf24ec 6258{
6259 return (!type_unknown_p (t)
6260 && !BRACE_ENCLOSED_INITIALIZER_P (t)
7e1f8be4 6261 && is_constant_expression (t)
29cf24ec 6262 && !instantiation_dependent_expression_p (t));
6263}
6264
6265/* Returns true if T is a potential static initializer expression that is not
6266 instantiation-dependent. */
6267
6268bool
7e1f8be4 6269is_nondependent_static_init_expression (tree t)
29cf24ec 6270{
6271 return (!type_unknown_p (t)
6272 && !BRACE_ENCLOSED_INITIALIZER_P (t)
7e1f8be4 6273 && is_static_init_expression (t)
29cf24ec 6274 && !instantiation_dependent_expression_p (t));
6275}
6276
a050099a 6277/* Finalize constexpr processing after parsing. */
6278
6279void
6280fini_constexpr (void)
6281{
6282 /* The contexpr call and fundef copies tables are no longer needed. */
6283 constexpr_call_table = NULL;
6284 fundef_copies_table = NULL;
6285}
6286
09b42213 6287#include "gt-cp-constexpr.h"