]> gcc.gnu.org Git - gcc.git/blame - gcc/cp/constexpr.c
PR middle-end/92952 - gfortran.dg/lto/pr87689 FAILs at -O2
[gcc.git] / gcc / cp / constexpr.c
CommitLineData
13f649f6
JM
1/* Perform -*- C++ -*- constant expression evaluation, including calls to
2 constexpr functions. These routines are used both during actual parsing
2d76680f
PC
3 and during the instantiation of template functions.
4
a5544970 5 Copyright (C) 1998-2019 Free Software Foundation, Inc.
2d76680f
PC
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"
2d76680f 26#include "cp-tree.h"
2adfab87 27#include "varasm.h"
2d76680f
PC
28#include "c-family/c-objc.h"
29#include "tree-iterator.h"
30#include "gimplify.h"
31#include "builtins.h"
60813a46 32#include "tree-inline.h"
0b274c17 33#include "ubsan.h"
44a845ca 34#include "gimple-fold.h"
8108ea30 35#include "timevar.h"
d8fcab68 36#include "fold-const-call.h"
8e007055 37#include "stor-layout.h"
2d76680f
PC
38
39static bool verify_constant (tree, bool, bool *, bool *);
40#define VERIFY_CONSTANT(X) \
41do { \
2b3ab879 42 if (verify_constant ((X), ctx->quiet, non_constant_p, overflow_p)) \
2d76680f
PC
43 return t; \
44 } while (0)
45
582d2481
JJ
46static HOST_WIDE_INT find_array_ctor_elt (tree ary, tree dindex,
47 bool insert = false);
48
2d76680f
PC
49/* Returns true iff FUN is an instantiation of a constexpr function
50 template or a defaulted constexpr function. */
51
52bool
53is_instantiation_of_constexpr (tree fun)
54{
55 return ((DECL_TEMPLOID_INSTANTIATION (fun)
56 && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun)))
57 || (DECL_DEFAULTED_FN (fun)
58 && DECL_DECLARED_CONSTEXPR_P (fun)));
59}
60
61/* Return true if T is a literal type. */
62
63bool
64literal_type_p (tree t)
65{
66 if (SCALAR_TYPE_P (t)
b55b02ea 67 || VECTOR_TYPE_P (t)
9f613f06 68 || TYPE_REF_P (t)
e42c407c 69 || (VOID_TYPE_P (t) && cxx_dialect >= cxx14))
2d76680f
PC
70 return true;
71 if (CLASS_TYPE_P (t))
72 {
73 t = complete_type (t);
74 gcc_assert (COMPLETE_TYPE_P (t) || errorcount);
75 return CLASSTYPE_LITERAL_P (t);
76 }
77 if (TREE_CODE (t) == ARRAY_TYPE)
78 return literal_type_p (strip_array_types (t));
79 return false;
80}
81
82/* If DECL is a variable declared `constexpr', require its type
9638f320
PC
83 be literal. Return error_mark_node if we give an error, the
84 DECL otherwise. */
2d76680f
PC
85
86tree
87ensure_literal_type_for_constexpr_object (tree decl)
88{
89 tree type = TREE_TYPE (decl);
90 if (VAR_P (decl)
91 && (DECL_DECLARED_CONSTEXPR_P (decl)
92 || var_in_constexpr_fn (decl))
93 && !processing_template_decl)
94 {
95 tree stype = strip_array_types (type);
96 if (CLASS_TYPE_P (stype) && !COMPLETE_TYPE_P (complete_type (stype)))
97 /* Don't complain here, we'll complain about incompleteness
98 when we try to initialize the variable. */;
683be2f7
JM
99 else if (type_uses_auto (type))
100 /* We don't know the actual type yet. */;
2d76680f
PC
101 else if (!literal_type_p (type))
102 {
103 if (DECL_DECLARED_CONSTEXPR_P (decl))
f1eac182 104 {
097f82ec 105 auto_diagnostic_group d;
f2935576
PC
106 error_at (DECL_SOURCE_LOCATION (decl),
107 "the type %qT of %<constexpr%> variable %qD "
108 "is not literal", type, decl);
f1eac182 109 explain_non_literal_class (type);
9638f320 110 decl = error_mark_node;
f1eac182 111 }
2d76680f 112 else
60813a46 113 {
5d4e573b 114 if (!is_instantiation_of_constexpr (current_function_decl))
f1eac182 115 {
097f82ec 116 auto_diagnostic_group d;
f2935576
PC
117 error_at (DECL_SOURCE_LOCATION (decl),
118 "variable %qD of non-literal type %qT in "
119 "%<constexpr%> function", decl, type);
f1eac182 120 explain_non_literal_class (type);
9638f320 121 decl = error_mark_node;
f1eac182 122 }
60813a46
JM
123 cp_function_chain->invalid_constexpr = true;
124 }
2d76680f 125 }
8e9589bd
JM
126 else if (DECL_DECLARED_CONSTEXPR_P (decl)
127 && variably_modified_type_p (type, NULL_TREE))
128 {
f2935576
PC
129 error_at (DECL_SOURCE_LOCATION (decl),
130 "%<constexpr%> variable %qD has variably-modified "
131 "type %qT", decl, type);
8e9589bd
JM
132 decl = error_mark_node;
133 }
2d76680f
PC
134 }
135 return decl;
136}
137
138/* Representation of entries in the constexpr function definition table. */
139
140struct GTY((for_user)) constexpr_fundef {
141 tree decl;
142 tree body;
43574e4f
JJ
143 tree parms;
144 tree result;
2d76680f
PC
145};
146
ca752f39 147struct constexpr_fundef_hasher : ggc_ptr_hash<constexpr_fundef>
2d76680f
PC
148{
149 static hashval_t hash (constexpr_fundef *);
150 static bool equal (constexpr_fundef *, constexpr_fundef *);
151};
152
153/* This table holds all constexpr function definitions seen in
154 the current translation unit. */
155
156static GTY (()) hash_table<constexpr_fundef_hasher> *constexpr_fundef_table;
157
158/* Utility function used for managing the constexpr function table.
159 Return true if the entries pointed to by P and Q are for the
160 same constexpr function. */
161
162inline bool
163constexpr_fundef_hasher::equal (constexpr_fundef *lhs, constexpr_fundef *rhs)
164{
165 return lhs->decl == rhs->decl;
166}
167
168/* Utility function used for managing the constexpr function table.
169 Return a hash value for the entry pointed to by Q. */
170
171inline hashval_t
172constexpr_fundef_hasher::hash (constexpr_fundef *fundef)
173{
174 return DECL_UID (fundef->decl);
175}
176
177/* Return a previously saved definition of function FUN. */
178
179static constexpr_fundef *
180retrieve_constexpr_fundef (tree fun)
181{
2d76680f
PC
182 if (constexpr_fundef_table == NULL)
183 return NULL;
184
43574e4f 185 constexpr_fundef fundef = { fun, NULL, NULL, NULL };
2d76680f
PC
186 return constexpr_fundef_table->find (&fundef);
187}
188
189/* Check whether the parameter and return types of FUN are valid for a
190 constexpr function, and complain if COMPLAIN. */
191
98e5a19a 192bool
2d76680f
PC
193is_valid_constexpr_fn (tree fun, bool complain)
194{
195 bool ret = true;
196
31f7f784 197 if (DECL_INHERITED_CTOR (fun)
2d76680f
PC
198 && TREE_CODE (fun) == TEMPLATE_DECL)
199 {
200 ret = false;
201 if (complain)
84fa214d 202 error ("inherited constructor %qD is not %<constexpr%>",
31f7f784 203 DECL_INHERITED_CTOR (fun));
2d76680f
PC
204 }
205 else
206 {
207 for (tree parm = FUNCTION_FIRST_USER_PARM (fun);
208 parm != NULL_TREE; parm = TREE_CHAIN (parm))
209 if (!literal_type_p (TREE_TYPE (parm)))
210 {
211 ret = false;
212 if (complain)
213 {
097f82ec 214 auto_diagnostic_group d;
84fa214d 215 error ("invalid type for parameter %d of %<constexpr%> "
2d76680f
PC
216 "function %q+#D", DECL_PARM_INDEX (parm), fun);
217 explain_non_literal_class (TREE_TYPE (parm));
218 }
219 }
220 }
221
e1bea341
JM
222 if (LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun)) && cxx_dialect < cxx17)
223 {
224 ret = false;
225 if (complain)
226 inform (DECL_SOURCE_LOCATION (fun),
84fa214d 227 "lambdas are implicitly %<constexpr%> only in C++17 and later");
e1bea341
JM
228 }
229 else if (!DECL_CONSTRUCTOR_P (fun))
2d76680f
PC
230 {
231 tree rettype = TREE_TYPE (TREE_TYPE (fun));
232 if (!literal_type_p (rettype))
233 {
234 ret = false;
235 if (complain)
236 {
097f82ec 237 auto_diagnostic_group d;
84fa214d 238 error ("invalid return type %qT of %<constexpr%> function %q+D",
2d76680f
PC
239 rettype, fun);
240 explain_non_literal_class (rettype);
241 }
242 }
243
54069e59
JM
244 /* C++14 DR 1684 removed this restriction. */
245 if (cxx_dialect < cxx14
246 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
2d76680f
PC
247 && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun)))
248 {
249 ret = false;
097f82ec
DM
250 if (complain)
251 {
252 auto_diagnostic_group d;
253 if (pedwarn (DECL_SOURCE_LOCATION (fun), OPT_Wpedantic,
254 "enclosing class of %<constexpr%> non-static"
255 " member function %q+#D is not a literal type",
256 fun))
257 explain_non_literal_class (DECL_CONTEXT (fun));
258 }
2d76680f
PC
259 }
260 }
261 else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun)))
262 {
263 ret = false;
264 if (complain)
265 error ("%q#T has virtual base classes", DECL_CONTEXT (fun));
266 }
267
268 return ret;
269}
270
271/* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF
272 for a member of an anonymous aggregate, INIT is the initializer for that
273 member, and VEC_OUTER is the vector of constructor elements for the class
274 whose constructor we are processing. Add the initializer to the vector
275 and return true to indicate success. */
276
277static bool
278build_anon_member_initialization (tree member, tree init,
279 vec<constructor_elt, va_gc> **vec_outer)
280{
281 /* MEMBER presents the relevant fields from the inside out, but we need
282 to build up the initializer from the outside in so that we can reuse
283 previously built CONSTRUCTORs if this is, say, the second field in an
284 anonymous struct. So we use a vec as a stack. */
285 auto_vec<tree, 2> fields;
286 do
287 {
288 fields.safe_push (TREE_OPERAND (member, 1));
289 member = TREE_OPERAND (member, 0);
290 }
291 while (ANON_AGGR_TYPE_P (TREE_TYPE (member))
292 && TREE_CODE (member) == COMPONENT_REF);
293
294 /* VEC has the constructor elements vector for the context of FIELD.
295 If FIELD is an anonymous aggregate, we will push inside it. */
296 vec<constructor_elt, va_gc> **vec = vec_outer;
297 tree field;
298 while (field = fields.pop(),
299 ANON_AGGR_TYPE_P (TREE_TYPE (field)))
300 {
301 tree ctor;
302 /* If there is already an outer constructor entry for the anonymous
303 aggregate FIELD, use it; otherwise, insert one. */
304 if (vec_safe_is_empty (*vec)
305 || (*vec)->last().index != field)
306 {
307 ctor = build_constructor (TREE_TYPE (field), NULL);
308 CONSTRUCTOR_APPEND_ELT (*vec, field, ctor);
309 }
310 else
311 ctor = (*vec)->last().value;
312 vec = &CONSTRUCTOR_ELTS (ctor);
313 }
314
315 /* Now we're at the innermost field, the one that isn't an anonymous
316 aggregate. Add its initializer to the CONSTRUCTOR and we're done. */
317 gcc_assert (fields.is_empty());
318 CONSTRUCTOR_APPEND_ELT (*vec, field, init);
319
320 return true;
321}
322
323/* Subroutine of build_constexpr_constructor_member_initializers.
324 The expression tree T represents a data member initialization
325 in a (constexpr) constructor definition. Build a pairing of
326 the data member with its initializer, and prepend that pair
327 to the existing initialization pair INITS. */
328
329static bool
330build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec)
331{
332 tree member, init;
333 if (TREE_CODE (t) == CLEANUP_POINT_EXPR)
334 t = TREE_OPERAND (t, 0);
335 if (TREE_CODE (t) == EXPR_STMT)
336 t = TREE_OPERAND (t, 0);
337 if (t == error_mark_node)
338 return false;
339 if (TREE_CODE (t) == STATEMENT_LIST)
340 {
341 tree_stmt_iterator i;
342 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
343 {
344 if (! build_data_member_initialization (tsi_stmt (i), vec))
345 return false;
346 }
347 return true;
348 }
349 if (TREE_CODE (t) == CLEANUP_STMT)
350 {
351 /* We can't see a CLEANUP_STMT in a constructor for a literal class,
352 but we can in a constexpr constructor for a non-literal class. Just
353 ignore it; either all the initialization will be constant, in which
354 case the cleanup can't run, or it can't be constexpr.
355 Still recurse into CLEANUP_BODY. */
356 return build_data_member_initialization (CLEANUP_BODY (t), vec);
357 }
358 if (TREE_CODE (t) == CONVERT_EXPR)
359 t = TREE_OPERAND (t, 0);
360 if (TREE_CODE (t) == INIT_EXPR
60813a46
JM
361 /* vptr initialization shows up as a MODIFY_EXPR. In C++14 we only
362 use what this function builds for cx_check_missing_mem_inits, and
363 assignment in the ctor body doesn't count. */
364 || (cxx_dialect < cxx14 && TREE_CODE (t) == MODIFY_EXPR))
2d76680f
PC
365 {
366 member = TREE_OPERAND (t, 0);
367 init = break_out_target_exprs (TREE_OPERAND (t, 1));
368 }
369 else if (TREE_CODE (t) == CALL_EXPR)
370 {
60813a46
JM
371 tree fn = get_callee_fndecl (t);
372 if (!fn || !DECL_CONSTRUCTOR_P (fn))
373 /* We're only interested in calls to subobject constructors. */
374 return true;
2d76680f
PC
375 member = CALL_EXPR_ARG (t, 0);
376 /* We don't use build_cplus_new here because it complains about
377 abstract bases. Leaving the call unwrapped means that it has the
378 wrong type, but cxx_eval_constant_expression doesn't care. */
379 init = break_out_target_exprs (t);
380 }
381 else if (TREE_CODE (t) == BIND_EXPR)
382 return build_data_member_initialization (BIND_EXPR_BODY (t), vec);
2d76680f 383 else
60813a46
JM
384 /* Don't add anything else to the CONSTRUCTOR. */
385 return true;
2d76680f
PC
386 if (INDIRECT_REF_P (member))
387 member = TREE_OPERAND (member, 0);
388 if (TREE_CODE (member) == NOP_EXPR)
389 {
390 tree op = member;
391 STRIP_NOPS (op);
392 if (TREE_CODE (op) == ADDR_EXPR)
393 {
394 gcc_assert (same_type_ignoring_top_level_qualifiers_p
395 (TREE_TYPE (TREE_TYPE (op)),
396 TREE_TYPE (TREE_TYPE (member))));
397 /* Initializing a cv-qualified member; we need to look through
398 the const_cast. */
399 member = op;
400 }
401 else if (op == current_class_ptr
402 && (same_type_ignoring_top_level_qualifiers_p
403 (TREE_TYPE (TREE_TYPE (member)),
404 current_class_type)))
405 /* Delegating constructor. */
406 member = op;
407 else
408 {
409 /* This is an initializer for an empty base; keep it for now so
410 we can check it in cxx_eval_bare_aggregate. */
411 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member))));
412 }
413 }
414 if (TREE_CODE (member) == ADDR_EXPR)
415 member = TREE_OPERAND (member, 0);
416 if (TREE_CODE (member) == COMPONENT_REF)
417 {
418 tree aggr = TREE_OPERAND (member, 0);
8cb7aaa1
JM
419 if (TREE_CODE (aggr) == VAR_DECL)
420 /* Initializing a local variable, don't add anything. */
421 return true;
2d76680f
PC
422 if (TREE_CODE (aggr) != COMPONENT_REF)
423 /* Normal member initialization. */
424 member = TREE_OPERAND (member, 1);
425 else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr)))
426 /* Initializing a member of an anonymous union. */
427 return build_anon_member_initialization (member, init, vec);
428 else
429 /* We're initializing a vtable pointer in a base. Leave it as
430 COMPONENT_REF so we remember the path to get to the vfield. */
431 gcc_assert (TREE_TYPE (member) == vtbl_ptr_type_node);
432 }
433
49b5925f
JM
434 /* Value-initialization can produce multiple initializers for the
435 same field; use the last one. */
436 if (!vec_safe_is_empty (*vec) && (*vec)->last().index == member)
437 (*vec)->last().value = init;
438 else
439 CONSTRUCTOR_APPEND_ELT (*vec, member, init);
2d76680f
PC
440 return true;
441}
442
443/* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval.
444 In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a
445 BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */
446
447static bool
448check_constexpr_bind_expr_vars (tree t)
449{
450 gcc_assert (TREE_CODE (t) == BIND_EXPR);
451
2d76680f
PC
452 for (tree var = BIND_EXPR_VARS (t); var; var = DECL_CHAIN (var))
453 if (TREE_CODE (var) == TYPE_DECL
4414e22e
PC
454 && DECL_IMPLICIT_TYPEDEF_P (var)
455 && !LAMBDA_TYPE_P (TREE_TYPE (var)))
2d76680f
PC
456 return false;
457 return true;
458}
459
460/* Subroutine of check_constexpr_ctor_body. */
461
462static bool
463check_constexpr_ctor_body_1 (tree last, tree list)
464{
465 switch (TREE_CODE (list))
466 {
467 case DECL_EXPR:
a0008434
MP
468 if (TREE_CODE (DECL_EXPR_DECL (list)) == USING_DECL
469 || TREE_CODE (DECL_EXPR_DECL (list)) == TYPE_DECL)
2d76680f 470 return true;
2d76680f
PC
471 return false;
472
473 case CLEANUP_POINT_EXPR:
474 return check_constexpr_ctor_body (last, TREE_OPERAND (list, 0),
475 /*complain=*/false);
476
477 case BIND_EXPR:
478 if (!check_constexpr_bind_expr_vars (list)
479 || !check_constexpr_ctor_body (last, BIND_EXPR_BODY (list),
480 /*complain=*/false))
481 return false;
482 return true;
483
484 case USING_STMT:
485 case STATIC_ASSERT:
96a95ac1 486 case DEBUG_BEGIN_STMT:
2d76680f
PC
487 return true;
488
489 default:
490 return false;
491 }
492}
493
494/* Make sure that there are no statements after LAST in the constructor
495 body represented by LIST. */
496
497bool
498check_constexpr_ctor_body (tree last, tree list, bool complain)
499{
60813a46
JM
500 /* C++14 doesn't require a constexpr ctor to have an empty body. */
501 if (cxx_dialect >= cxx14)
502 return true;
503
2d76680f
PC
504 bool ok = true;
505 if (TREE_CODE (list) == STATEMENT_LIST)
506 {
507 tree_stmt_iterator i = tsi_last (list);
508 for (; !tsi_end_p (i); tsi_prev (&i))
509 {
510 tree t = tsi_stmt (i);
511 if (t == last)
512 break;
513 if (!check_constexpr_ctor_body_1 (last, t))
514 {
515 ok = false;
516 break;
517 }
518 }
519 }
520 else if (list != last
521 && !check_constexpr_ctor_body_1 (last, list))
522 ok = false;
523 if (!ok)
524 {
525 if (complain)
84fa214d 526 error ("%<constexpr%> constructor does not have empty body");
2d76680f
PC
527 DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false;
528 }
529 return ok;
530}
531
532/* V is a vector of constructor elements built up for the base and member
533 initializers of a constructor for TYPE. They need to be in increasing
534 offset order, which they might not be yet if TYPE has a primary base
535 which is not first in the base-clause or a vptr and at least one base
536 all of which are non-primary. */
537
538static vec<constructor_elt, va_gc> *
539sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v)
540{
541 tree pri = CLASSTYPE_PRIMARY_BINFO (type);
542 tree field_type;
543 unsigned i;
544 constructor_elt *ce;
545
546 if (pri)
547 field_type = BINFO_TYPE (pri);
548 else if (TYPE_CONTAINS_VPTR_P (type))
549 field_type = vtbl_ptr_type_node;
550 else
551 return v;
552
553 /* Find the element for the primary base or vptr and move it to the
554 beginning of the vec. */
555 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
556 if (TREE_TYPE (ce->index) == field_type)
557 break;
558
559 if (i > 0 && i < vec_safe_length (v))
560 {
561 vec<constructor_elt, va_gc> &vref = *v;
562 constructor_elt elt = vref[i];
563 for (; i > 0; --i)
564 vref[i] = vref[i-1];
565 vref[0] = elt;
566 }
567
568 return v;
569}
570
571/* Build compile-time evalable representations of member-initializer list
572 for a constexpr constructor. */
573
574static tree
575build_constexpr_constructor_member_initializers (tree type, tree body)
576{
577 vec<constructor_elt, va_gc> *vec = NULL;
578 bool ok = true;
fa837fb6
JM
579 while (true)
580 switch (TREE_CODE (body))
581 {
582 case MUST_NOT_THROW_EXPR:
583 case EH_SPEC_BLOCK:
584 body = TREE_OPERAND (body, 0);
585 break;
586
587 case STATEMENT_LIST:
588 for (tree_stmt_iterator i = tsi_start (body);
589 !tsi_end_p (i); tsi_next (&i))
590 {
591 body = tsi_stmt (i);
592 if (TREE_CODE (body) == BIND_EXPR)
593 break;
594 }
595 break;
596
597 case BIND_EXPR:
598 body = BIND_EXPR_BODY (body);
599 goto found;
600
601 default:
602 gcc_unreachable ();
58cc255c 603 }
fa837fb6 604 found:
1259cb6d
JJ
605 if (TREE_CODE (body) == TRY_BLOCK)
606 {
607 body = TREE_OPERAND (body, 0);
608 if (TREE_CODE (body) == BIND_EXPR)
609 body = BIND_EXPR_BODY (body);
610 }
2d76680f
PC
611 if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
612 {
613 body = TREE_OPERAND (body, 0);
614 if (TREE_CODE (body) == EXPR_STMT)
615 body = TREE_OPERAND (body, 0);
616 if (TREE_CODE (body) == INIT_EXPR
617 && (same_type_ignoring_top_level_qualifiers_p
618 (TREE_TYPE (TREE_OPERAND (body, 0)),
619 current_class_type)))
620 {
621 /* Trivial copy. */
622 return TREE_OPERAND (body, 1);
623 }
624 ok = build_data_member_initialization (body, &vec);
625 }
626 else if (TREE_CODE (body) == STATEMENT_LIST)
627 {
628 tree_stmt_iterator i;
629 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
630 {
631 ok = build_data_member_initialization (tsi_stmt (i), &vec);
632 if (!ok)
633 break;
634 }
635 }
2d76680f
PC
636 else if (EXPR_P (body))
637 ok = build_data_member_initialization (body, &vec);
638 else
639 gcc_assert (errorcount > 0);
640 if (ok)
641 {
642 if (vec_safe_length (vec) > 0)
643 {
644 /* In a delegating constructor, return the target. */
645 constructor_elt *ce = &(*vec)[0];
646 if (ce->index == current_class_ptr)
647 {
648 body = ce->value;
649 vec_free (vec);
650 return body;
651 }
652 }
653 vec = sort_constexpr_mem_initializers (type, vec);
654 return build_constructor (type, vec);
655 }
656 else
657 return error_mark_node;
658}
659
1b6fa695
ML
660/* We have an expression tree T that represents a call, either CALL_EXPR
661 or AGGR_INIT_EXPR. If the call is lexically to a named function,
662 retrun the _DECL for that function. */
663
664static tree
665get_function_named_in_call (tree t)
666{
667 tree fun = cp_get_callee (t);
668 if (fun && TREE_CODE (fun) == ADDR_EXPR
669 && TREE_CODE (TREE_OPERAND (fun, 0)) == FUNCTION_DECL)
670 fun = TREE_OPERAND (fun, 0);
671 return fun;
672}
673
2d76680f
PC
674/* Subroutine of register_constexpr_fundef. BODY is the body of a function
675 declared to be constexpr, or a sub-statement thereof. Returns the
676 return value if suitable, error_mark_node for a statement not allowed in
677 a constexpr function, or NULL_TREE if no return value was found. */
678
5afef8b1 679tree
2d76680f
PC
680constexpr_fn_retval (tree body)
681{
682 switch (TREE_CODE (body))
683 {
684 case STATEMENT_LIST:
685 {
686 tree_stmt_iterator i;
687 tree expr = NULL_TREE;
688 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
689 {
690 tree s = constexpr_fn_retval (tsi_stmt (i));
691 if (s == error_mark_node)
692 return error_mark_node;
693 else if (s == NULL_TREE)
694 /* Keep iterating. */;
695 else if (expr)
696 /* Multiple return statements. */
697 return error_mark_node;
698 else
699 expr = s;
700 }
701 return expr;
702 }
703
704 case RETURN_EXPR:
705 return break_out_target_exprs (TREE_OPERAND (body, 0));
706
707 case DECL_EXPR:
0162cb3b
PC
708 {
709 tree decl = DECL_EXPR_DECL (body);
710 if (TREE_CODE (decl) == USING_DECL
711 /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */
712 || DECL_ARTIFICIAL (decl))
713 return NULL_TREE;
714 return error_mark_node;
715 }
2d76680f
PC
716
717 case CLEANUP_POINT_EXPR:
718 return constexpr_fn_retval (TREE_OPERAND (body, 0));
719
720 case BIND_EXPR:
721 if (!check_constexpr_bind_expr_vars (body))
722 return error_mark_node;
723 return constexpr_fn_retval (BIND_EXPR_BODY (body));
724
725 case USING_STMT:
96a95ac1 726 case DEBUG_BEGIN_STMT:
2d76680f
PC
727 return NULL_TREE;
728
1b6fa695
ML
729 case CALL_EXPR:
730 {
731 tree fun = get_function_named_in_call (body);
732 if (fun != NULL_TREE
3d78e008 733 && fndecl_built_in_p (fun, BUILT_IN_UNREACHABLE))
1b6fa695
ML
734 return NULL_TREE;
735 }
736 /* Fallthru. */
737
2d76680f
PC
738 default:
739 return error_mark_node;
740 }
741}
742
743/* Subroutine of register_constexpr_fundef. BODY is the DECL_SAVED_TREE of
744 FUN; do the necessary transformations to turn it into a single expression
745 that we can store in the hash table. */
746
747static tree
748massage_constexpr_body (tree fun, tree body)
749{
750 if (DECL_CONSTRUCTOR_P (fun))
751 body = build_constexpr_constructor_member_initializers
752 (DECL_CONTEXT (fun), body);
60813a46 753 else if (cxx_dialect < cxx14)
2d76680f
PC
754 {
755 if (TREE_CODE (body) == EH_SPEC_BLOCK)
756 body = EH_SPEC_STMTS (body);
757 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
758 body = TREE_OPERAND (body, 0);
759 body = constexpr_fn_retval (body);
760 }
761 return body;
762}
763
3e4b91f2
NS
764/* CTYPE is a type constructed from BODY. Return true if some
765 bases/fields are uninitialized, and complain if COMPLAIN. */
2d76680f
PC
766
767static bool
3e4b91f2 768cx_check_missing_mem_inits (tree ctype, tree body, bool complain)
2d76680f 769{
7906797e
MP
770 /* We allow uninitialized bases/fields in C++20. */
771 if (cxx_dialect >= cxx2a)
772 return false;
773
3e4b91f2
NS
774 unsigned nelts = 0;
775
776 if (body)
777 {
778 if (TREE_CODE (body) != CONSTRUCTOR)
779 return false;
780 nelts = CONSTRUCTOR_NELTS (body);
781 }
782 tree field = TYPE_FIELDS (ctype);
2d76680f
PC
783
784 if (TREE_CODE (ctype) == UNION_TYPE)
785 {
786 if (nelts == 0 && next_initializable_field (field))
787 {
788 if (complain)
789 error ("%<constexpr%> constructor for union %qT must "
790 "initialize exactly one non-static data member", ctype);
791 return true;
792 }
793 return false;
794 }
795
3e4b91f2
NS
796 /* Iterate over the CONSTRUCTOR, checking any missing fields don't
797 need an explicit initialization. */
798 bool bad = false;
799 for (unsigned i = 0; i <= nelts; ++i)
2d76680f 800 {
3e4b91f2
NS
801 tree index = NULL_TREE;
802 if (i < nelts)
2d76680f
PC
803 {
804 index = CONSTRUCTOR_ELT (body, i)->index;
805 /* Skip base and vtable inits. */
806 if (TREE_CODE (index) != FIELD_DECL
807 || DECL_ARTIFICIAL (index))
808 continue;
809 }
3e4b91f2 810
2d76680f
PC
811 for (; field != index; field = DECL_CHAIN (field))
812 {
813 tree ftype;
3e4b91f2
NS
814 if (TREE_CODE (field) != FIELD_DECL)
815 continue;
7c30b12a 816 if (DECL_UNNAMED_BIT_FIELD (field))
2d76680f 817 continue;
3e4b91f2
NS
818 if (DECL_ARTIFICIAL (field))
819 continue;
820 if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
821 {
7906797e 822 /* Recurse to check the anonymous aggregate member. */
3e4b91f2
NS
823 bad |= cx_check_missing_mem_inits
824 (TREE_TYPE (field), NULL_TREE, complain);
825 if (bad && !complain)
826 return true;
827 continue;
828 }
2d76680f
PC
829 ftype = strip_array_types (TREE_TYPE (field));
830 if (type_has_constexpr_default_constructor (ftype))
831 {
832 /* It's OK to skip a member with a trivial constexpr ctor.
833 A constexpr ctor that isn't trivial should have been
834 added in by now. */
835 gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype)
836 || errorcount != 0);
837 continue;
838 }
839 if (!complain)
840 return true;
097f82ec 841 auto_diagnostic_group d;
b8cd3996
JM
842 error ("member %qD must be initialized by mem-initializer "
843 "in %<constexpr%> constructor", field);
844 inform (DECL_SOURCE_LOCATION (field), "declared here");
2d76680f
PC
845 bad = true;
846 }
847 if (field == NULL_TREE)
848 break;
3e4b91f2
NS
849
850 if (ANON_AGGR_TYPE_P (TREE_TYPE (index)))
851 {
852 /* Check the anonymous aggregate initializer is valid. */
853 bad |= cx_check_missing_mem_inits
854 (TREE_TYPE (index), CONSTRUCTOR_ELT (body, i)->value, complain);
855 if (bad && !complain)
856 return true;
857 }
2d76680f
PC
858 field = DECL_CHAIN (field);
859 }
860
861 return bad;
862}
863
864/* We are processing the definition of the constexpr function FUN.
865 Check that its BODY fulfills the propriate requirements and
866 enter it in the constexpr function definition table.
867 For constructor BODY is actually the TREE_LIST of the
868 member-initializer list. */
869
870tree
871register_constexpr_fundef (tree fun, tree body)
872{
873 constexpr_fundef entry;
874 constexpr_fundef **slot;
875
876 if (!is_valid_constexpr_fn (fun, !DECL_GENERATED_P (fun)))
877 return NULL;
878
ca30abcd
JM
879 tree massaged = massage_constexpr_body (fun, body);
880 if (massaged == NULL_TREE || massaged == error_mark_node)
2d76680f
PC
881 {
882 if (!DECL_CONSTRUCTOR_P (fun))
84fa214d
ML
883 error ("body of %<constexpr%> function %qD not a return-statement",
884 fun);
2d76680f
PC
885 return NULL;
886 }
887
ca30abcd 888 if (!potential_rvalue_constant_expression (massaged))
2d76680f
PC
889 {
890 if (!DECL_GENERATED_P (fun))
ca30abcd 891 require_potential_rvalue_constant_expression (massaged);
2d76680f
PC
892 return NULL;
893 }
894
895 if (DECL_CONSTRUCTOR_P (fun)
3e4b91f2
NS
896 && cx_check_missing_mem_inits (DECL_CONTEXT (fun),
897 massaged, !DECL_GENERATED_P (fun)))
2d76680f
PC
898 return NULL;
899
900 /* Create the constexpr function table if necessary. */
901 if (constexpr_fundef_table == NULL)
902 constexpr_fundef_table
903 = hash_table<constexpr_fundef_hasher>::create_ggc (101);
904
905 entry.decl = fun;
43574e4f
JJ
906 tree saved_fn = current_function_decl;
907 bool clear_ctx = false;
908 current_function_decl = fun;
909 if (DECL_RESULT (fun) && DECL_CONTEXT (DECL_RESULT (fun)) == NULL_TREE)
910 {
911 clear_ctx = true;
912 DECL_CONTEXT (DECL_RESULT (fun)) = fun;
913 }
914 entry.body = copy_fn (fun, entry.parms, entry.result);
915 current_function_decl = saved_fn;
2d76680f 916 slot = constexpr_fundef_table->find_slot (&entry, INSERT);
43574e4f
JJ
917 if (clear_ctx)
918 DECL_CONTEXT (DECL_RESULT (fun)) = NULL_TREE;
2d76680f
PC
919
920 gcc_assert (*slot == NULL);
921 *slot = ggc_alloc<constexpr_fundef> ();
922 **slot = entry;
923
924 return fun;
925}
926
927/* FUN is a non-constexpr function called in a context that requires a
928 constant expression. If it comes from a constexpr template, explain why
929 the instantiation isn't constexpr. */
930
931void
932explain_invalid_constexpr_fn (tree fun)
933{
934 static hash_set<tree> *diagnosed;
935 tree body;
936 location_t save_loc;
98e5a19a 937 /* Only diagnose defaulted functions, lambdas, or instantiations. */
2d76680f 938 if (!DECL_DEFAULTED_FN (fun)
98e5a19a 939 && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun))
2d76680f 940 && !is_instantiation_of_constexpr (fun))
f22f817c
JM
941 {
942 inform (DECL_SOURCE_LOCATION (fun), "%qD declared here", fun);
943 return;
944 }
2d76680f
PC
945 if (diagnosed == NULL)
946 diagnosed = new hash_set<tree>;
947 if (diagnosed->add (fun))
948 /* Already explained. */
949 return;
950
951 save_loc = input_location;
98e5a19a
JM
952 if (!lambda_static_thunk_p (fun))
953 {
954 /* Diagnostics should completely ignore the static thunk, so leave
955 input_location set to our caller's location. */
956 input_location = DECL_SOURCE_LOCATION (fun);
957 inform (input_location,
84fa214d 958 "%qD is not usable as a %<constexpr%> function because:", fun);
98e5a19a 959 }
2d76680f
PC
960 /* First check the declaration. */
961 if (is_valid_constexpr_fn (fun, true))
962 {
963 /* Then if it's OK, the body. */
98e5a19a
JM
964 if (!DECL_DECLARED_CONSTEXPR_P (fun)
965 && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun)))
2d76680f
PC
966 explain_implicit_non_constexpr (fun);
967 else
968 {
969 body = massage_constexpr_body (fun, DECL_SAVED_TREE (fun));
970 require_potential_rvalue_constant_expression (body);
971 if (DECL_CONSTRUCTOR_P (fun))
3e4b91f2 972 cx_check_missing_mem_inits (DECL_CONTEXT (fun), body, true);
2d76680f
PC
973 }
974 }
975 input_location = save_loc;
976}
977
978/* Objects of this type represent calls to constexpr functions
979 along with the bindings of parameters to their arguments, for
980 the purpose of compile time evaluation. */
981
982struct GTY((for_user)) constexpr_call {
983 /* Description of the constexpr function definition. */
984 constexpr_fundef *fundef;
3c961dc7 985 /* Parameter bindings environment. A TREE_VEC of arguments. */
2d76680f
PC
986 tree bindings;
987 /* Result of the call.
988 NULL means the call is being evaluated.
989 error_mark_node means that the evaluation was erroneous;
990 otherwise, the actuall value of the call. */
991 tree result;
992 /* The hash of this call; we remember it here to avoid having to
993 recalculate it when expanding the hash table. */
994 hashval_t hash;
cce3ae91 995 /* Whether __builtin_is_constant_evaluated() should evaluate to true. */
13de99bc 996 bool manifestly_const_eval;
2d76680f
PC
997};
998
ca752f39 999struct constexpr_call_hasher : ggc_ptr_hash<constexpr_call>
2d76680f
PC
1000{
1001 static hashval_t hash (constexpr_call *);
1002 static bool equal (constexpr_call *, constexpr_call *);
3e605b20
JM
1003};
1004
4b390698
JJ
1005enum constexpr_switch_state {
1006 /* Used when processing a switch for the first time by cxx_eval_switch_expr
1007 and default: label for that switch has not been seen yet. */
1008 css_default_not_seen,
1009 /* Used when processing a switch for the first time by cxx_eval_switch_expr
1010 and default: label for that switch has been seen already. */
1011 css_default_seen,
1012 /* Used when processing a switch for the second time by
1013 cxx_eval_switch_expr, where default: label should match. */
1014 css_default_processing
1015};
1016
8e007055
JJ
1017/* The constexpr expansion context part which needs one instance per
1018 cxx_eval_outermost_constant_expr invocation. VALUES is a map of values of
1019 variables initialized within the expression. */
1020
1021struct constexpr_global_ctx {
1022 /* Values for any temporaries or local variables within the
1023 constant-expression. */
1024 hash_map<tree,tree> values;
1025 /* Number of cxx_eval_constant_expression calls (except skipped ones,
1026 on simple constants or location wrappers) encountered during current
1027 cxx_eval_outermost_constant_expr call. */
1028 HOST_WIDE_INT constexpr_ops_count;
1029 /* Heap VAR_DECLs created during the evaluation of the outermost constant
1030 expression. */
1031 auto_vec<tree, 16> heap_vars;
ee1de08d
JJ
1032 /* Cleanups that need to be evaluated at the end of CLEANUP_POINT_EXPR. */
1033 vec<tree> *cleanups;
8e007055 1034 /* Constructor. */
ee1de08d 1035 constexpr_global_ctx () : constexpr_ops_count (0), cleanups (NULL) {}
8e007055
JJ
1036};
1037
3e605b20
JM
1038/* The constexpr expansion context. CALL is the current function
1039 expansion, CTOR is the current aggregate initializer, OBJECT is the
8e007055 1040 object being initialized by CTOR, either a VAR_DECL or a _REF. */
3e605b20
JM
1041
1042struct constexpr_ctx {
8e007055
JJ
1043 /* The part of the context that needs to be unique to the whole
1044 cxx_eval_outermost_constant_expr invocation. */
1045 constexpr_global_ctx *global;
13f649f6 1046 /* The innermost call we're evaluating. */
3e605b20 1047 constexpr_call *call;
ee1de08d
JJ
1048 /* SAVE_EXPRs and TARGET_EXPR_SLOT vars of TARGET_EXPRs that we've seen
1049 within the current LOOP_EXPR. NULL if we aren't inside a loop. */
0ee28590 1050 vec<tree> *save_exprs;
13f649f6
JM
1051 /* The CONSTRUCTOR we're currently building up for an aggregate
1052 initializer. */
3e605b20 1053 tree ctor;
13f649f6 1054 /* The object we're building the CONSTRUCTOR for. */
3e605b20 1055 tree object;
4b390698
JJ
1056 /* If inside SWITCH_EXPR. */
1057 constexpr_switch_state *css_state;
a15ffa22 1058
13f649f6 1059 /* Whether we should error on a non-constant expression or fail quietly. */
2b3ab879 1060 bool quiet;
13f649f6
JM
1061 /* Whether we are strictly conforming to constant expression rules or
1062 trying harder to get a constant value. */
69eb4fde 1063 bool strict;
e4082611 1064 /* Whether __builtin_is_constant_evaluated () should be true. */
13de99bc 1065 bool manifestly_const_eval;
3e605b20 1066};
2d76680f
PC
1067
1068/* A table of all constexpr calls that have been evaluated by the
1069 compiler in this translation unit. */
1070
97f3003f 1071static GTY (()) hash_table<constexpr_call_hasher> *constexpr_call_table;
2d76680f 1072
3e605b20 1073static tree cxx_eval_constant_expression (const constexpr_ctx *, tree,
5a804683 1074 bool, bool *, bool *, tree * = NULL);
2d76680f
PC
1075
1076/* Compute a hash value for a constexpr call representation. */
1077
1078inline hashval_t
1079constexpr_call_hasher::hash (constexpr_call *info)
1080{
1081 return info->hash;
1082}
1083
1084/* Return true if the objects pointed to by P and Q represent calls
1085 to the same constexpr function with the same arguments.
1086 Otherwise, return false. */
1087
1088bool
1089constexpr_call_hasher::equal (constexpr_call *lhs, constexpr_call *rhs)
1090{
2d76680f 1091 if (lhs == rhs)
46cf7fa1
JJ
1092 return true;
1093 if (lhs->hash != rhs->hash)
1094 return false;
13de99bc 1095 if (lhs->manifestly_const_eval != rhs->manifestly_const_eval)
cce3ae91 1096 return false;
2d76680f 1097 if (!constexpr_fundef_hasher::equal (lhs->fundef, rhs->fundef))
46cf7fa1 1098 return false;
3c961dc7 1099 return cp_tree_equal (lhs->bindings, rhs->bindings);
2d76680f
PC
1100}
1101
1102/* Initialize the constexpr call table, if needed. */
1103
1104static void
1105maybe_initialize_constexpr_call_table (void)
1106{
1107 if (constexpr_call_table == NULL)
1108 constexpr_call_table = hash_table<constexpr_call_hasher>::create_ggc (101);
1109}
1110
c0daf32d
PP
1111/* During constexpr CALL_EXPR evaluation, to avoid issues with sharing when
1112 a function happens to get called recursively, we unshare the callee
1113 function's body and evaluate this unshared copy instead of evaluating the
1114 original body.
1115
1116 FUNDEF_COPIES_TABLE is a per-function freelist of these unshared function
1117 copies. The underlying data structure of FUNDEF_COPIES_TABLE is a hash_map
97f3003f
JM
1118 that's keyed off of the original FUNCTION_DECL and whose value is a
1119 TREE_LIST of this function's unused copies awaiting reuse.
c0daf32d 1120
97f3003f 1121 This is not GC-deletable to avoid GC affecting UID generation. */
c0daf32d 1122
97f3003f 1123static GTY(()) hash_map<tree, tree> *fundef_copies_table;
c0daf32d 1124
c0daf32d 1125/* Reuse a copy or create a new unshared copy of the function FUN.
3c98ff9b
NS
1126 Return this copy. We use a TREE_LIST whose PURPOSE is body, VALUE
1127 is parms, TYPE is result. */
c0daf32d 1128
97f3003f 1129static tree
43574e4f 1130get_fundef_copy (constexpr_fundef *fundef)
c0daf32d 1131{
97f3003f 1132 tree copy;
3c98ff9b 1133 bool existed;
c89844e5
JM
1134 tree *slot = &(hash_map_safe_get_or_insert<hm_ggc>
1135 (fundef_copies_table, fundef->decl, &existed, 127));
3c98ff9b
NS
1136
1137 if (!existed)
c0daf32d 1138 {
3c98ff9b
NS
1139 /* There is no cached function available, or in use. We can use
1140 the function directly. That the slot is now created records
1141 that this function is now in use. */
43574e4f
JJ
1142 copy = build_tree_list (fundef->body, fundef->parms);
1143 TREE_TYPE (copy) = fundef->result;
3c98ff9b
NS
1144 }
1145 else if (*slot == NULL_TREE)
1146 {
1147 /* We've already used the function itself, so make a copy. */
97f3003f 1148 copy = build_tree_list (NULL, NULL);
43574e4f
JJ
1149 tree saved_body = DECL_SAVED_TREE (fundef->decl);
1150 tree saved_parms = DECL_ARGUMENTS (fundef->decl);
1151 tree saved_result = DECL_RESULT (fundef->decl);
1152 tree saved_fn = current_function_decl;
1153 DECL_SAVED_TREE (fundef->decl) = fundef->body;
1154 DECL_ARGUMENTS (fundef->decl) = fundef->parms;
1155 DECL_RESULT (fundef->decl) = fundef->result;
1156 current_function_decl = fundef->decl;
1157 TREE_PURPOSE (copy) = copy_fn (fundef->decl, TREE_VALUE (copy),
1158 TREE_TYPE (copy));
1159 current_function_decl = saved_fn;
1160 DECL_RESULT (fundef->decl) = saved_result;
1161 DECL_ARGUMENTS (fundef->decl) = saved_parms;
1162 DECL_SAVED_TREE (fundef->decl) = saved_body;
c0daf32d
PP
1163 }
1164 else
1165 {
3c98ff9b 1166 /* We have a cached function available. */
c0daf32d 1167 copy = *slot;
97f3003f 1168 *slot = TREE_CHAIN (copy);
c0daf32d
PP
1169 }
1170
1171 return copy;
1172}
1173
3c98ff9b
NS
1174/* Save the copy COPY of function FUN for later reuse by
1175 get_fundef_copy(). By construction, there will always be an entry
1176 to find. */
c0daf32d
PP
1177
1178static void
97f3003f 1179save_fundef_copy (tree fun, tree copy)
c0daf32d 1180{
3c98ff9b 1181 tree *slot = fundef_copies_table->get (fun);
97f3003f 1182 TREE_CHAIN (copy) = *slot;
c0daf32d
PP
1183 *slot = copy;
1184}
1185
2d76680f
PC
1186/* We have an expression tree T that represents a call, either CALL_EXPR
1187 or AGGR_INIT_EXPR. Return the Nth argument. */
1188
1189static inline tree
1190get_nth_callarg (tree t, int n)
1191{
1192 switch (TREE_CODE (t))
1193 {
1194 case CALL_EXPR:
1195 return CALL_EXPR_ARG (t, n);
1196
1197 case AGGR_INIT_EXPR:
1198 return AGGR_INIT_EXPR_ARG (t, n);
1199
1200 default:
1201 gcc_unreachable ();
1202 return NULL;
1203 }
1204}
1205
2d76680f
PC
1206/* Attempt to evaluate T which represents a call to a builtin function.
1207 We assume here that all builtin functions evaluate to scalar types
1208 represented by _CST nodes. */
1209
1210static tree
5756d0f9 1211cxx_eval_builtin_function_call (const constexpr_ctx *ctx, tree t, tree fun,
92a596e8 1212 bool lval,
2d76680f
PC
1213 bool *non_constant_p, bool *overflow_p)
1214{
1215 const int nargs = call_expr_nargs (t);
1216 tree *args = (tree *) alloca (nargs * sizeof (tree));
1217 tree new_call;
1218 int i;
5756d0f9
JM
1219
1220 /* Don't fold __builtin_constant_p within a constexpr function. */
e4082611 1221 bool bi_const_p = DECL_IS_BUILTIN_CONSTANT_P (fun);
b925d25d 1222
fe736ffd
JM
1223 /* If we aren't requiring a constant expression, defer __builtin_constant_p
1224 in a constexpr function until we have values for the parameters. */
b925d25d 1225 if (bi_const_p
4cd3e7df 1226 && !ctx->manifestly_const_eval
5756d0f9
JM
1227 && current_function_decl
1228 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
2d76680f 1229 {
5756d0f9
JM
1230 *non_constant_p = true;
1231 return t;
2d76680f 1232 }
5756d0f9 1233
e4082611 1234 /* For __builtin_is_constant_evaluated, defer it if not
13de99bc 1235 ctx->manifestly_const_eval, otherwise fold it to true. */
3d78e008 1236 if (fndecl_built_in_p (fun, CP_BUILT_IN_IS_CONSTANT_EVALUATED,
13de99bc 1237 BUILT_IN_FRONTEND))
e4082611 1238 {
13de99bc 1239 if (!ctx->manifestly_const_eval)
e4082611
JJ
1240 {
1241 *non_constant_p = true;
1242 return t;
1243 }
1244 return boolean_true_node;
1245 }
1246
ff603745
JJ
1247 if (fndecl_built_in_p (fun, CP_BUILT_IN_SOURCE_LOCATION, BUILT_IN_FRONTEND))
1248 return fold_builtin_source_location (EXPR_LOCATION (t));
1249
5756d0f9
JM
1250 /* Be permissive for arguments to built-ins; __builtin_constant_p should
1251 return constant false for a non-constant argument. */
1252 constexpr_ctx new_ctx = *ctx;
1253 new_ctx.quiet = true;
5756d0f9 1254 for (i = 0; i < nargs; ++i)
b925d25d 1255 {
2ff7172a
JJ
1256 args[i] = CALL_EXPR_ARG (t, i);
1257 /* If builtin_valid_in_constant_expr_p is true,
1258 potential_constant_expression_1 has not recursed into the arguments
1259 of the builtin, verify it here. */
1260 if (!builtin_valid_in_constant_expr_p (fun)
1261 || potential_constant_expression (args[i]))
4cd3e7df
JJ
1262 {
1263 bool dummy1 = false, dummy2 = false;
1264 args[i] = cxx_eval_constant_expression (&new_ctx, args[i], false,
1265 &dummy1, &dummy2);
1266 }
1267
b925d25d 1268 if (bi_const_p)
4cd3e7df 1269 /* For __builtin_constant_p, fold all expressions with constant values
b925d25d 1270 even if they aren't C++ constant-expressions. */
4cd3e7df 1271 args[i] = cp_fold_rvalue (args[i]);
b925d25d 1272 }
5756d0f9
JM
1273
1274 bool save_ffbcp = force_folding_builtin_constant_p;
b09a67ea 1275 force_folding_builtin_constant_p |= ctx->manifestly_const_eval;
43574e4f
JJ
1276 tree save_cur_fn = current_function_decl;
1277 /* Return name of ctx->call->fundef->decl for __builtin_FUNCTION (). */
1278 if (fndecl_built_in_p (fun, BUILT_IN_FUNCTION)
1279 && ctx->call
1280 && ctx->call->fundef)
1281 current_function_decl = ctx->call->fundef->decl;
109d2197
JJ
1282 new_call = fold_builtin_call_array (EXPR_LOCATION (t), TREE_TYPE (t),
1283 CALL_EXPR_FN (t), nargs, args);
43574e4f 1284 current_function_decl = save_cur_fn;
5756d0f9 1285 force_folding_builtin_constant_p = save_ffbcp;
109d2197
JJ
1286 if (new_call == NULL)
1287 {
1288 if (!*non_constant_p && !ctx->quiet)
1289 {
1b6fa695
ML
1290 /* Do not allow__builtin_unreachable in constexpr function.
1291 The __builtin_unreachable call with BUILTINS_LOCATION
1292 comes from cp_maybe_instrument_return. */
3d78e008 1293 if (fndecl_built_in_p (fun, BUILT_IN_UNREACHABLE)
1b6fa695 1294 && EXPR_LOCATION (t) == BUILTINS_LOCATION)
84fa214d 1295 error ("%<constexpr%> call flows off the end of the function");
1b6fa695
ML
1296 else
1297 {
1298 new_call = build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t),
1299 CALL_EXPR_FN (t), nargs, args);
1300 error ("%q+E is not a constant expression", new_call);
1301 }
109d2197
JJ
1302 }
1303 *non_constant_p = true;
1304 return t;
1305 }
1306
43574e4f 1307 if (!potential_constant_expression (new_call))
109d2197
JJ
1308 {
1309 if (!*non_constant_p && !ctx->quiet)
1310 error ("%q+E is not a constant expression", new_call);
1311 *non_constant_p = true;
1312 return t;
1313 }
1314
1315 return cxx_eval_constant_expression (&new_ctx, new_call, lval,
1316 non_constant_p, overflow_p);
2d76680f
PC
1317}
1318
1319/* TEMP is the constant value of a temporary object of type TYPE. Adjust
1320 the type of the value to match. */
1321
1322static tree
1323adjust_temp_type (tree type, tree temp)
1324{
47be9509 1325 if (same_type_p (TREE_TYPE (temp), type))
2d76680f
PC
1326 return temp;
1327 /* Avoid wrapping an aggregate value in a NOP_EXPR. */
1328 if (TREE_CODE (temp) == CONSTRUCTOR)
b27f74e7
MP
1329 {
1330 /* build_constructor wouldn't retain various CONSTRUCTOR flags. */
1331 tree t = copy_node (temp);
1332 TREE_TYPE (t) = type;
1333 return t;
1334 }
ff8ba86f
JJ
1335 if (TREE_CODE (temp) == EMPTY_CLASS_EXPR)
1336 return build0 (EMPTY_CLASS_EXPR, type);
2d76680f 1337 gcc_assert (scalarish_type_p (type));
72b091f7
MP
1338 /* Now we know we're dealing with a scalar, and a prvalue of non-class
1339 type is cv-unqualified. */
1340 return cp_fold_convert (cv_unqualified (type), temp);
2d76680f
PC
1341}
1342
9b9eb42a
JM
1343/* If T is a CONSTRUCTOR, return an unshared copy of T and any
1344 sub-CONSTRUCTORs. Otherwise return T.
0146e25f 1345
9b9eb42a
JM
1346 We use this whenever we initialize an object as a whole, whether it's a
1347 parameter, a local variable, or a subobject, so that subsequent
1348 modifications don't affect other places where it was used. */
0146e25f 1349
1f6857ba 1350tree
3da7d774 1351unshare_constructor (tree t MEM_STAT_DECL)
0146e25f 1352{
9b9eb42a
JM
1353 if (!t || TREE_CODE (t) != CONSTRUCTOR)
1354 return t;
1355 auto_vec <tree*, 4> ptrs;
1356 ptrs.safe_push (&t);
1357 while (!ptrs.is_empty ())
1358 {
1359 tree *p = ptrs.pop ();
3da7d774
JM
1360 tree n = copy_node (*p PASS_MEM_STAT);
1361 CONSTRUCTOR_ELTS (n) = vec_safe_copy (CONSTRUCTOR_ELTS (*p) PASS_MEM_STAT);
9b9eb42a
JM
1362 *p = n;
1363 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (n);
1364 constructor_elt *ce;
1365 for (HOST_WIDE_INT i = 0; vec_safe_iterate (v, i, &ce); ++i)
1366 if (TREE_CODE (ce->value) == CONSTRUCTOR)
1367 ptrs.safe_push (&ce->value);
1368 }
0146e25f
PP
1369 return t;
1370}
1371
620adbec
JM
1372/* If T is a CONSTRUCTOR, ggc_free T and any sub-CONSTRUCTORs. */
1373
1374static void
1375free_constructor (tree t)
1376{
1377 if (!t || TREE_CODE (t) != CONSTRUCTOR)
1378 return;
1379 releasing_vec ctors;
1380 vec_safe_push (ctors, t);
1381 while (!ctors->is_empty ())
1382 {
1383 tree c = ctors->pop ();
1384 if (vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (c))
1385 {
1386 constructor_elt *ce;
1387 for (HOST_WIDE_INT i = 0; vec_safe_iterate (elts, i, &ce); ++i)
1388 if (TREE_CODE (ce->value) == CONSTRUCTOR)
1389 vec_safe_push (ctors, ce->value);
1390 ggc_free (elts);
1391 }
1392 ggc_free (c);
1393 }
1394}
1395
2d76680f
PC
1396/* Subroutine of cxx_eval_call_expression.
1397 We are processing a call expression (either CALL_EXPR or
3e605b20 1398 AGGR_INIT_EXPR) in the context of CTX. Evaluate
2d76680f
PC
1399 all arguments and bind their values to correspondings
1400 parameters, making up the NEW_CALL context. */
1401
1402static void
3e605b20 1403cxx_bind_parameters_in_call (const constexpr_ctx *ctx, tree t,
2d76680f 1404 constexpr_call *new_call,
12d9ce19
JM
1405 bool *non_constant_p, bool *overflow_p,
1406 bool *non_constant_args)
2d76680f
PC
1407{
1408 const int nargs = call_expr_nargs (t);
1409 tree fun = new_call->fundef->decl;
43574e4f 1410 tree parms = new_call->fundef->parms;
2d76680f 1411 int i;
3c961dc7
JM
1412 /* We don't record ellipsis args below. */
1413 int nparms = list_length (parms);
1414 int nbinds = nargs < nparms ? nargs : nparms;
1415 tree binds = new_call->bindings = make_tree_vec (nbinds);
2d76680f
PC
1416 for (i = 0; i < nargs; ++i)
1417 {
1418 tree x, arg;
1419 tree type = parms ? TREE_TYPE (parms) : void_type_node;
2d76680f 1420 x = get_nth_callarg (t, i);
3e605b20
JM
1421 /* For member function, the first argument is a pointer to the implied
1422 object. For a constructor, it might still be a dummy object, in
60813a46 1423 which case we get the real argument from ctx. */
3e605b20
JM
1424 if (i == 0 && DECL_CONSTRUCTOR_P (fun)
1425 && is_dummy_object (x))
1426 {
1427 x = ctx->object;
84dd815f 1428 x = build_address (x);
3e605b20 1429 }
43574e4f
JJ
1430 if (TREE_ADDRESSABLE (type))
1431 /* Undo convert_for_arg_passing work here. */
1432 x = convert_from_reference (x);
0e038601 1433 arg = cxx_eval_constant_expression (ctx, x, /*lval=*/false,
5a804683 1434 non_constant_p, overflow_p);
2d76680f 1435 /* Don't VERIFY_CONSTANT here. */
2b3ab879 1436 if (*non_constant_p && ctx->quiet)
2d76680f
PC
1437 return;
1438 /* Just discard ellipsis args after checking their constantitude. */
1439 if (!parms)
1440 continue;
4727d4c6
NS
1441
1442 if (!*non_constant_p)
1443 {
620adbec
JM
1444 /* Unsharing here isn't necessary for correctness, but it
1445 significantly improves memory performance for some reason. */
1dc23505 1446 arg = unshare_constructor (arg);
4727d4c6
NS
1447 /* Make sure the binding has the same type as the parm. But
1448 only for constant args. */
9f613f06 1449 if (!TYPE_REF_P (type))
4727d4c6
NS
1450 arg = adjust_temp_type (type, arg);
1451 if (!TREE_CONSTANT (arg))
1452 *non_constant_args = true;
5558a0da
JJ
1453 /* For virtual calls, adjust the this argument, so that it is
1454 the object on which the method is called, rather than
1455 one of its bases. */
1456 if (i == 0 && DECL_VIRTUAL_P (fun))
1457 {
1458 tree addr = arg;
1459 STRIP_NOPS (addr);
1460 if (TREE_CODE (addr) == ADDR_EXPR)
1461 {
1462 tree obj = TREE_OPERAND (addr, 0);
1463 while (TREE_CODE (obj) == COMPONENT_REF
1464 && DECL_FIELD_IS_BASE (TREE_OPERAND (obj, 1))
1465 && !same_type_ignoring_top_level_qualifiers_p
1466 (TREE_TYPE (obj), DECL_CONTEXT (fun)))
1467 obj = TREE_OPERAND (obj, 0);
1468 if (obj != TREE_OPERAND (addr, 0))
1469 arg = build_fold_addr_expr_with_type (obj,
1470 TREE_TYPE (arg));
1471 }
1472 }
3c961dc7 1473 TREE_VEC_ELT (binds, i) = arg;
4727d4c6 1474 }
2d76680f
PC
1475 parms = TREE_CHAIN (parms);
1476 }
1477}
1478
1479/* Variables and functions to manage constexpr call expansion context.
1480 These do not need to be marked for PCH or GC. */
1481
1482/* FIXME remember and print actual constant arguments. */
7de76362 1483static vec<tree> call_stack;
2d76680f
PC
1484static int call_stack_tick;
1485static int last_cx_error_tick;
1486
7ffc7de5 1487static int
2d76680f
PC
1488push_cx_call_context (tree call)
1489{
1490 ++call_stack_tick;
1491 if (!EXPR_HAS_LOCATION (call))
1492 SET_EXPR_LOCATION (call, input_location);
1493 call_stack.safe_push (call);
7ffc7de5
JM
1494 int len = call_stack.length ();
1495 if (len > max_constexpr_depth)
2d76680f 1496 return false;
7ffc7de5 1497 return len;
2d76680f
PC
1498}
1499
1500static void
1501pop_cx_call_context (void)
1502{
1503 ++call_stack_tick;
1504 call_stack.pop ();
1505}
1506
1507vec<tree>
1508cx_error_context (void)
1509{
1510 vec<tree> r = vNULL;
1511 if (call_stack_tick != last_cx_error_tick
1512 && !call_stack.is_empty ())
1513 r = call_stack;
1514 last_cx_error_tick = call_stack_tick;
1515 return r;
1516}
1517
44a845ca
MS
1518/* Evaluate a call T to a GCC internal function when possible and return
1519 the evaluated result or, under the control of CTX, give an error, set
1520 NON_CONSTANT_P, and return the unevaluated call T otherwise. */
1521
1522static tree
1523cxx_eval_internal_function (const constexpr_ctx *ctx, tree t,
1524 bool lval,
1525 bool *non_constant_p, bool *overflow_p)
1526{
1527 enum tree_code opcode = ERROR_MARK;
1528
1529 switch (CALL_EXPR_IFN (t))
1530 {
1531 case IFN_UBSAN_NULL:
1532 case IFN_UBSAN_BOUNDS:
1533 case IFN_UBSAN_VPTR:
81fea426 1534 case IFN_FALLTHROUGH:
44a845ca
MS
1535 return void_node;
1536
1537 case IFN_ADD_OVERFLOW:
1538 opcode = PLUS_EXPR;
1539 break;
1540 case IFN_SUB_OVERFLOW:
1541 opcode = MINUS_EXPR;
1542 break;
1543 case IFN_MUL_OVERFLOW:
1544 opcode = MULT_EXPR;
1545 break;
1546
e16f1cc7
JJ
1547 case IFN_LAUNDER:
1548 return cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0),
1549 false, non_constant_p, overflow_p);
1550
d8fcab68
JJ
1551 case IFN_VEC_CONVERT:
1552 {
1553 tree arg = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0),
1554 false, non_constant_p,
1555 overflow_p);
1556 if (TREE_CODE (arg) == VECTOR_CST)
1557 return fold_const_call (CFN_VEC_CONVERT, TREE_TYPE (t), arg);
1558 else
1559 {
1560 *non_constant_p = true;
1561 return t;
1562 }
1563 }
1564
44a845ca
MS
1565 default:
1566 if (!ctx->quiet)
f9d0ca40 1567 error_at (cp_expr_loc_or_input_loc (t),
44a845ca
MS
1568 "call to internal function %qE", t);
1569 *non_constant_p = true;
1570 return t;
1571 }
1572
1573 /* Evaluate constant arguments using OPCODE and return a complex
1574 number containing the result and the overflow bit. */
1575 tree arg0 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), lval,
1576 non_constant_p, overflow_p);
1577 tree arg1 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 1), lval,
1578 non_constant_p, overflow_p);
1579
1580 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1581 {
f9d0ca40 1582 location_t loc = cp_expr_loc_or_input_loc (t);
44a845ca
MS
1583 tree type = TREE_TYPE (TREE_TYPE (t));
1584 tree result = fold_binary_loc (loc, opcode, type,
1585 fold_convert_loc (loc, type, arg0),
1586 fold_convert_loc (loc, type, arg1));
1587 tree ovf
1588 = build_int_cst (type, arith_overflowed_p (opcode, type, arg0, arg1));
1589 /* Reset TREE_OVERFLOW to avoid warnings for the overflow. */
1590 if (TREE_OVERFLOW (result))
1591 TREE_OVERFLOW (result) = 0;
1592
1593 return build_complex (TREE_TYPE (t), result, ovf);
1594 }
1595
1596 *non_constant_p = true;
1597 return t;
1598}
1599
e8c48716 1600/* Clean CONSTRUCTOR_NO_CLEARING from CTOR and its sub-aggregates. */
ecc57615
JM
1601
1602static void
1603clear_no_implicit_zero (tree ctor)
1604{
e8c48716 1605 if (CONSTRUCTOR_NO_CLEARING (ctor))
ecc57615 1606 {
e8c48716 1607 CONSTRUCTOR_NO_CLEARING (ctor) = false;
ecc57615
JM
1608 tree elt; unsigned HOST_WIDE_INT idx;
1609 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), idx, elt)
1610 if (TREE_CODE (elt) == CONSTRUCTOR)
1611 clear_no_implicit_zero (elt);
1612 }
1613}
1614
04e1749c
MP
1615/* Complain about a const object OBJ being modified in a constant expression.
1616 EXPR is the MODIFY_EXPR expression performing the modification. */
1617
1618static void
1619modifying_const_object_error (tree expr, tree obj)
1620{
1621 location_t loc = cp_expr_loc_or_input_loc (expr);
1622 auto_diagnostic_group d;
1623 error_at (loc, "modifying a const object %qE is not allowed in "
1624 "a constant expression", TREE_OPERAND (expr, 0));
1625 inform (location_of (obj), "originally declared %<const%> here");
1626}
1627
8e007055
JJ
1628/* Return true if FNDECL is a replaceable global allocation function that
1629 should be useable during constant expression evaluation. */
1630
1631static inline bool
1632cxx_replaceable_global_alloc_fn (tree fndecl)
1633{
1634 return (cxx_dialect >= cxx2a
1635 && IDENTIFIER_NEWDEL_OP_P (DECL_NAME (fndecl))
cf650568
JJ
1636 && CP_DECL_CONTEXT (fndecl) == global_namespace
1637 && (DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl)
1638 || DECL_IS_OPERATOR_DELETE_P (fndecl)));
1639}
1640
1641/* Return true if FNDECL is a placement new function that should be
1642 useable during constant expression evaluation of std::construct_at. */
1643
1644static inline bool
1645cxx_placement_new_fn (tree fndecl)
1646{
1647 if (cxx_dialect >= cxx2a
1648 && IDENTIFIER_NEW_OP_P (DECL_NAME (fndecl))
1649 && CP_DECL_CONTEXT (fndecl) == global_namespace
1650 && !DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl)
1651 && TREE_CODE (TREE_TYPE (fndecl)) == FUNCTION_TYPE)
1652 {
1653 tree first_arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
1654 if (TREE_VALUE (first_arg) == ptr_type_node
1655 && TREE_CHAIN (first_arg) == void_list_node)
1656 return true;
1657 }
1658 return false;
1659}
1660
1661/* Return true if FNDECL is std::construct_at. */
1662
1663static inline bool
1664is_std_construct_at (tree fndecl)
1665{
1666 if (!decl_in_std_namespace_p (fndecl))
1667 return false;
1668
1669 tree name = DECL_NAME (fndecl);
1670 return name && id_equal (name, "construct_at");
8e007055
JJ
1671}
1672
8412b939
JJ
1673/* Return true if FNDECL is std::allocator<T>::{,de}allocate. */
1674
1675static inline bool
1676is_std_allocator_allocate (tree fndecl)
1677{
1678 tree name = DECL_NAME (fndecl);
1679 if (name == NULL_TREE
1680 || !(id_equal (name, "allocate") || id_equal (name, "deallocate")))
1681 return false;
1682
1683 tree ctx = DECL_CONTEXT (fndecl);
1684 if (ctx == NULL_TREE || !CLASS_TYPE_P (ctx) || !TYPE_MAIN_DECL (ctx))
1685 return false;
1686
1687 tree decl = TYPE_MAIN_DECL (ctx);
1688 name = DECL_NAME (decl);
1689 if (name == NULL_TREE || !id_equal (name, "allocator"))
1690 return false;
1691
1692 return decl_in_std_namespace_p (decl);
1693}
1694
2d76680f
PC
1695/* Subroutine of cxx_eval_constant_expression.
1696 Evaluate the call expression tree T in the context of OLD_CALL expression
1697 evaluation. */
1698
1699static tree
3e605b20 1700cxx_eval_call_expression (const constexpr_ctx *ctx, tree t,
92a596e8 1701 bool lval,
2d76680f
PC
1702 bool *non_constant_p, bool *overflow_p)
1703{
861d4af8
AS
1704 /* Handle concept checks separately. */
1705 if (concept_check_p (t))
1706 return evaluate_concept_check (t, tf_warning_or_error);
1707
f9d0ca40 1708 location_t loc = cp_expr_loc_or_input_loc (t);
2d76680f 1709 tree fun = get_function_named_in_call (t);
cce3ae91 1710 constexpr_call new_call
13de99bc 1711 = { NULL, NULL, NULL, 0, ctx->manifestly_const_eval };
7ffc7de5 1712 int depth_ok;
2d76680f 1713
0b274c17 1714 if (fun == NULL_TREE)
44a845ca
MS
1715 return cxx_eval_internal_function (ctx, t, lval,
1716 non_constant_p, overflow_p);
0b274c17 1717
2d76680f
PC
1718 if (TREE_CODE (fun) != FUNCTION_DECL)
1719 {
1720 /* Might be a constexpr function pointer. */
2b3ab879 1721 fun = cxx_eval_constant_expression (ctx, fun,
92a596e8 1722 /*lval*/false, non_constant_p,
5a804683 1723 overflow_p);
2d76680f
PC
1724 STRIP_NOPS (fun);
1725 if (TREE_CODE (fun) == ADDR_EXPR)
1726 fun = TREE_OPERAND (fun, 0);
582d2481
JJ
1727 /* For TARGET_VTABLE_USES_DESCRIPTORS targets, there is no
1728 indirection, the called expression is a pointer into the
1729 virtual table which should contain FDESC_EXPR. Extract the
1730 FUNCTION_DECL from there. */
1731 else if (TARGET_VTABLE_USES_DESCRIPTORS
1732 && TREE_CODE (fun) == POINTER_PLUS_EXPR
1733 && TREE_CODE (TREE_OPERAND (fun, 0)) == ADDR_EXPR
1734 && TREE_CODE (TREE_OPERAND (fun, 1)) == INTEGER_CST)
1735 {
1736 tree d = TREE_OPERAND (TREE_OPERAND (fun, 0), 0);
1737 if (VAR_P (d)
1738 && DECL_VTABLE_OR_VTT_P (d)
1739 && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE
1740 && TREE_TYPE (TREE_TYPE (d)) == vtable_entry_type
1741 && DECL_INITIAL (d)
1742 && TREE_CODE (DECL_INITIAL (d)) == CONSTRUCTOR)
1743 {
1744 tree i = int_const_binop (TRUNC_DIV_EXPR, TREE_OPERAND (fun, 1),
1745 TYPE_SIZE_UNIT (vtable_entry_type));
1746 HOST_WIDE_INT idx = find_array_ctor_elt (DECL_INITIAL (d), i);
1747 if (idx >= 0)
1748 {
1749 tree fdesc
1750 = (*CONSTRUCTOR_ELTS (DECL_INITIAL (d)))[idx].value;
1751 if (TREE_CODE (fdesc) == FDESC_EXPR
1752 && integer_zerop (TREE_OPERAND (fdesc, 1)))
1753 fun = TREE_OPERAND (fdesc, 0);
1754 }
1755 }
1756 }
2d76680f
PC
1757 }
1758 if (TREE_CODE (fun) != FUNCTION_DECL)
1759 {
2b3ab879 1760 if (!ctx->quiet && !*non_constant_p)
84fa214d 1761 error_at (loc, "expression %qE does not designate a %<constexpr%> "
2d76680f
PC
1762 "function", fun);
1763 *non_constant_p = true;
1764 return t;
1765 }
1766 if (DECL_CLONED_FUNCTION_P (fun))
1767 fun = DECL_CLONED_FUNCTION (fun);
0b274c17
MP
1768
1769 if (is_ubsan_builtin_p (fun))
1770 return void_node;
1771
3d78e008 1772 if (fndecl_built_in_p (fun))
5756d0f9 1773 return cxx_eval_builtin_function_call (ctx, t, fun,
92a596e8 1774 lval, non_constant_p, overflow_p);
2d76680f
PC
1775 if (!DECL_DECLARED_CONSTEXPR_P (fun))
1776 {
8412b939
JJ
1777 if (TREE_CODE (t) == CALL_EXPR
1778 && cxx_replaceable_global_alloc_fn (fun)
1779 && (CALL_FROM_NEW_OR_DELETE_P (t)
1780 || (ctx->call
1781 && ctx->call->fundef
1782 && is_std_allocator_allocate (ctx->call->fundef->decl))))
8e007055
JJ
1783 {
1784 const int nargs = call_expr_nargs (t);
1785 tree arg0 = NULL_TREE;
1786 for (int i = 0; i < nargs; ++i)
1787 {
1788 tree arg = CALL_EXPR_ARG (t, i);
1789 arg = cxx_eval_constant_expression (ctx, arg, false,
1790 non_constant_p, overflow_p);
1791 VERIFY_CONSTANT (arg);
1792 if (i == 0)
1793 arg0 = arg;
1794 }
1795 gcc_assert (arg0);
1796 if (IDENTIFIER_NEW_OP_P (DECL_NAME (fun)))
1797 {
1798 tree type = build_array_type_nelts (char_type_node,
1799 tree_to_uhwi (arg0));
1800 tree var = build_decl (loc, VAR_DECL, heap_uninit_identifier,
1801 type);
1802 DECL_ARTIFICIAL (var) = 1;
1803 TREE_STATIC (var) = 1;
1804 ctx->global->heap_vars.safe_push (var);
1805 ctx->global->values.put (var, NULL_TREE);
1806 return fold_convert (ptr_type_node, build_address (var));
1807 }
1808 else
1809 {
1810 STRIP_NOPS (arg0);
1811 if (TREE_CODE (arg0) == ADDR_EXPR
1812 && VAR_P (TREE_OPERAND (arg0, 0)))
1813 {
1814 tree var = TREE_OPERAND (arg0, 0);
1815 if (DECL_NAME (var) == heap_uninit_identifier
1816 || DECL_NAME (var) == heap_identifier)
1817 {
1818 DECL_NAME (var) = heap_deleted_identifier;
1819 ctx->global->values.remove (var);
1820 return void_node;
1821 }
1822 else if (DECL_NAME (var) == heap_deleted_identifier)
1823 {
1824 if (!ctx->quiet)
1825 error_at (loc, "deallocation of already deallocated "
1826 "storage");
1827 *non_constant_p = true;
1828 return t;
1829 }
1830 }
1831 if (!ctx->quiet)
1832 error_at (loc, "deallocation of storage that was "
1833 "not previously allocated");
1834 *non_constant_p = true;
1835 return t;
1836 }
1837 }
cf650568
JJ
1838 /* Allow placement new in std::construct_at, just return the second
1839 argument. */
8412b939
JJ
1840 if (TREE_CODE (t) == CALL_EXPR
1841 && cxx_placement_new_fn (fun)
cf650568
JJ
1842 && ctx->call
1843 && ctx->call->fundef
1844 && is_std_construct_at (ctx->call->fundef->decl))
1845 {
1846 const int nargs = call_expr_nargs (t);
1847 tree arg1 = NULL_TREE;
1848 for (int i = 0; i < nargs; ++i)
1849 {
1850 tree arg = CALL_EXPR_ARG (t, i);
1851 arg = cxx_eval_constant_expression (ctx, arg, false,
1852 non_constant_p, overflow_p);
1853 VERIFY_CONSTANT (arg);
1854 if (i == 1)
1855 arg1 = arg;
1856 }
1857 gcc_assert (arg1);
1858 return arg1;
1859 }
2b3ab879 1860 if (!ctx->quiet)
2d76680f 1861 {
11399477 1862 if (!lambda_static_thunk_p (fun))
84fa214d 1863 error_at (loc, "call to non-%<constexpr%> function %qD", fun);
2d76680f
PC
1864 explain_invalid_constexpr_fn (fun);
1865 }
1866 *non_constant_p = true;
1867 return t;
1868 }
1869
86461cad
JM
1870 constexpr_ctx new_ctx = *ctx;
1871 if (DECL_CONSTRUCTOR_P (fun) && !ctx->object
1872 && TREE_CODE (t) == AGGR_INIT_EXPR)
1873 {
1874 /* We want to have an initialization target for an AGGR_INIT_EXPR.
1875 If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
1876 new_ctx.object = AGGR_INIT_EXPR_SLOT (t);
1877 tree ctor = new_ctx.ctor = build_constructor (DECL_CONTEXT (fun), NULL);
e8c48716 1878 CONSTRUCTOR_NO_CLEARING (ctor) = true;
8e007055 1879 ctx->global->values.put (new_ctx.object, ctor);
86461cad
JM
1880 ctx = &new_ctx;
1881 }
1882
2d76680f
PC
1883 /* Shortcut trivial constructor/op=. */
1884 if (trivial_fn_p (fun))
1885 {
86461cad 1886 tree init = NULL_TREE;
2d76680f 1887 if (call_expr_nargs (t) == 2)
86461cad 1888 init = convert_from_reference (get_nth_callarg (t, 1));
2d76680f
PC
1889 else if (TREE_CODE (t) == AGGR_INIT_EXPR
1890 && AGGR_INIT_ZERO_FIRST (t))
86461cad
JM
1891 init = build_zero_init (DECL_CONTEXT (fun), NULL_TREE, false);
1892 if (init)
1893 {
1894 tree op = get_nth_callarg (t, 0);
1895 if (is_dummy_object (op))
1896 op = ctx->object;
1897 else
1898 op = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (op)), op);
1899 tree set = build2 (MODIFY_EXPR, TREE_TYPE (op), op, init);
c8816908
JM
1900 new_ctx.call = &new_call;
1901 return cxx_eval_constant_expression (&new_ctx, set, lval,
86461cad
JM
1902 non_constant_p, overflow_p);
1903 }
2d76680f
PC
1904 }
1905
81371eff
JM
1906 /* We can't defer instantiating the function any longer. */
1907 if (!DECL_INITIAL (fun)
1908 && DECL_TEMPLOID_INSTANTIATION (fun))
1909 {
f065303f
JM
1910 location_t save_loc = input_location;
1911 input_location = loc;
81371eff
JM
1912 ++function_depth;
1913 instantiate_decl (fun, /*defer_ok*/false, /*expl_inst*/false);
1914 --function_depth;
f065303f 1915 input_location = save_loc;
81371eff
JM
1916 }
1917
2d76680f 1918 /* If in direct recursive call, optimize definition search. */
c8816908 1919 if (ctx && ctx->call && ctx->call->fundef && ctx->call->fundef->decl == fun)
3e605b20 1920 new_call.fundef = ctx->call->fundef;
2d76680f
PC
1921 else
1922 {
1923 new_call.fundef = retrieve_constexpr_fundef (fun);
4ddcdbf9
JM
1924 if (new_call.fundef == NULL || new_call.fundef->body == NULL
1925 || fun == current_function_decl)
2d76680f 1926 {
2b3ab879 1927 if (!ctx->quiet)
2d76680f 1928 {
4ddcdbf9
JM
1929 /* We need to check for current_function_decl here in case we're
1930 being called during cp_fold_function, because at that point
1931 DECL_INITIAL is set properly and we have a fundef but we
1932 haven't lowered invisirefs yet (c++/70344). */
1933 if (DECL_INITIAL (fun) == error_mark_node
1934 || fun == current_function_decl)
ddd6d421
JM
1935 error_at (loc, "%qD called in a constant expression before its "
1936 "definition is complete", fun);
1937 else if (DECL_INITIAL (fun))
2d76680f 1938 {
98e5a19a
JM
1939 /* The definition of fun was somehow unsuitable. But pretend
1940 that lambda static thunks don't exist. */
1941 if (!lambda_static_thunk_p (fun))
1942 error_at (loc, "%qD called in a constant expression", fun);
2d76680f
PC
1943 explain_invalid_constexpr_fn (fun);
1944 }
1945 else
1946 error_at (loc, "%qD used before its definition", fun);
1947 }
1948 *non_constant_p = true;
1949 return t;
1950 }
1951 }
60813a46 1952
12d9ce19 1953 bool non_constant_args = false;
3e605b20 1954 cxx_bind_parameters_in_call (ctx, t, &new_call,
12d9ce19 1955 non_constant_p, overflow_p, &non_constant_args);
ecdcd560
JM
1956
1957 /* We build up the bindings list before we know whether we already have this
1958 call cached. If we don't end up saving these bindings, ggc_free them when
1959 this function exits. */
6c1dae73 1960 class free_bindings
ecdcd560 1961 {
6c1dae73 1962 public:
ecdcd560
JM
1963 tree &bindings;
1964 bool do_free;
1965 free_bindings (tree &b): bindings (b), do_free(true) { }
1966 void preserve () { do_free = false; }
1967 ~free_bindings () {
1968 if (do_free)
620adbec
JM
1969 {
1970 for (int i = 0; i < TREE_VEC_LENGTH (bindings); ++i)
1971 free_constructor (TREE_VEC_ELT (bindings, i));
1972 ggc_free (bindings);
1973 }
ecdcd560
JM
1974 }
1975 } fb (new_call.bindings);
1976
2d76680f
PC
1977 if (*non_constant_p)
1978 return t;
1979
1980 depth_ok = push_cx_call_context (t);
1981
04e1749c
MP
1982 /* Remember the object we are constructing. */
1983 tree new_obj = NULL_TREE;
1984 if (DECL_CONSTRUCTOR_P (fun))
1985 {
1986 /* In a constructor, it should be the first `this' argument.
1987 At this point it has already been evaluated in the call
1988 to cxx_bind_parameters_in_call. */
1989 new_obj = TREE_VEC_ELT (new_call.bindings, 0);
1990 STRIP_NOPS (new_obj);
1991 if (TREE_CODE (new_obj) == ADDR_EXPR)
1992 new_obj = TREE_OPERAND (new_obj, 0);
1993 }
1994
12d9ce19 1995 tree result = NULL_TREE;
2d76680f 1996
12d9ce19 1997 constexpr_call *entry = NULL;
4a58d2fe 1998 if (depth_ok && !non_constant_args && ctx->strict)
2d76680f 1999 {
cce3ae91
JJ
2000 new_call.hash = constexpr_fundef_hasher::hash (new_call.fundef);
2001 new_call.hash
2002 = iterative_hash_template_arg (new_call.bindings, new_call.hash);
2003 new_call.hash
13de99bc 2004 = iterative_hash_object (ctx->manifestly_const_eval, new_call.hash);
12d9ce19
JM
2005
2006 /* If we have seen this call before, we are done. */
2007 maybe_initialize_constexpr_call_table ();
2008 constexpr_call **slot
2009 = constexpr_call_table->find_slot (&new_call, INSERT);
2010 entry = *slot;
2011 if (entry == NULL)
2012 {
7ffc7de5
JM
2013 /* Only cache up to constexpr_cache_depth to limit memory use. */
2014 if (depth_ok < constexpr_cache_depth)
2015 {
2016 /* We need to keep a pointer to the entry, not just the slot, as
2017 the slot can move during evaluation of the body. */
2018 *slot = entry = ggc_alloc<constexpr_call> ();
2019 *entry = new_call;
2020 fb.preserve ();
2021 }
12d9ce19 2022 }
7ffc7de5
JM
2023 /* Calls that are in progress have their result set to NULL, so that we
2024 can detect circular dependencies. Now that we only cache up to
2025 constexpr_cache_depth this won't catch circular dependencies that
2026 start deeper, but they'll hit the recursion or ops limit. */
12d9ce19
JM
2027 else if (entry->result == NULL)
2028 {
2029 if (!ctx->quiet)
2030 error ("call has circular dependency");
2031 *non_constant_p = true;
2032 entry->result = result = error_mark_node;
2033 }
2034 else
2035 result = entry->result;
2d76680f
PC
2036 }
2037
2038 if (!depth_ok)
2039 {
2b3ab879 2040 if (!ctx->quiet)
84fa214d 2041 error ("%<constexpr%> evaluation depth exceeds maximum of %d (use "
7e7a6ed7 2042 "%<-fconstexpr-depth=%> to increase the maximum)",
2d76680f
PC
2043 max_constexpr_depth);
2044 *non_constant_p = true;
12d9ce19 2045 result = error_mark_node;
2d76680f
PC
2046 }
2047 else
2048 {
de54de93
JM
2049 if (result && result != error_mark_node)
2050 /* OK */;
2051 else if (!DECL_SAVED_TREE (fun))
2052 {
2053 /* When at_eof >= 2, cgraph has started throwing away
2054 DECL_SAVED_TREE, so fail quietly. FIXME we get here because of
2055 late code generation for VEC_INIT_EXPR, which needs to be
2056 completely reconsidered. */
2057 gcc_assert (at_eof >= 2 && ctx->quiet);
2058 *non_constant_p = true;
2059 }
2060 else
3e605b20 2061 {
c0daf32d 2062 tree body, parms, res;
620adbec 2063 releasing_vec ctors;
0567dcd2 2064
c0daf32d 2065 /* Reuse or create a new unshared copy of this function's body. */
43574e4f 2066 tree copy = get_fundef_copy (new_call.fundef);
97f3003f
JM
2067 body = TREE_PURPOSE (copy);
2068 parms = TREE_VALUE (copy);
2069 res = TREE_TYPE (copy);
0567dcd2
MP
2070
2071 /* Associate the bindings with the remapped parms. */
2072 tree bound = new_call.bindings;
2073 tree remapped = parms;
3c961dc7 2074 for (int i = 0; i < TREE_VEC_LENGTH (bound); ++i)
60813a46 2075 {
3c961dc7 2076 tree arg = TREE_VEC_ELT (bound, i);
7f0e23e9 2077 /* Don't share a CONSTRUCTOR that might be changed. */
0146e25f 2078 arg = unshare_constructor (arg);
620adbec
JM
2079 if (TREE_CODE (arg) == CONSTRUCTOR)
2080 vec_safe_push (ctors, arg);
8e007055 2081 ctx->global->values.put (remapped, arg);
0567dcd2 2082 remapped = DECL_CHAIN (remapped);
60813a46 2083 }
0567dcd2
MP
2084 /* Add the RESULT_DECL to the values map, too. */
2085 tree slot = NULL_TREE;
2086 if (DECL_BY_REFERENCE (res))
60813a46 2087 {
0567dcd2
MP
2088 slot = AGGR_INIT_EXPR_SLOT (t);
2089 tree addr = build_address (slot);
2090 addr = build_nop (TREE_TYPE (res), addr);
8e007055
JJ
2091 ctx->global->values.put (res, addr);
2092 ctx->global->values.put (slot, NULL_TREE);
0567dcd2
MP
2093 }
2094 else
8e007055 2095 ctx->global->values.put (res, NULL_TREE);
60813a46 2096
ee1de08d
JJ
2097 /* Track the callee's evaluated SAVE_EXPRs and TARGET_EXPRs so that
2098 we can forget their values after the call. */
c0daf32d 2099 constexpr_ctx ctx_with_save_exprs = *ctx;
0ee28590 2100 auto_vec<tree, 10> save_exprs;
c0daf32d 2101 ctx_with_save_exprs.save_exprs = &save_exprs;
1e163090 2102 ctx_with_save_exprs.call = &new_call;
c0daf32d 2103
0567dcd2 2104 tree jump_target = NULL_TREE;
c0daf32d 2105 cxx_eval_constant_expression (&ctx_with_save_exprs, body,
0567dcd2
MP
2106 lval, non_constant_p, overflow_p,
2107 &jump_target);
60813a46 2108
0567dcd2
MP
2109 if (DECL_CONSTRUCTOR_P (fun))
2110 /* This can be null for a subobject constructor call, in
2111 which case what we care about is the initialization
2112 side-effects rather than the value. We could get at the
2113 value by evaluating *this, but we don't bother; there's
2114 no need to put such a call in the hash table. */
2115 result = lval ? ctx->object : ctx->ctor;
2116 else if (VOID_TYPE_P (TREE_TYPE (res)))
2117 result = void_node;
2118 else
2119 {
8e007055 2120 result = *ctx->global->values.get (slot ? slot : res);
0567dcd2 2121 if (result == NULL_TREE && !*non_constant_p)
60813a46 2122 {
0567dcd2 2123 if (!ctx->quiet)
84fa214d 2124 error ("%<constexpr%> call flows off the end "
0567dcd2
MP
2125 "of the function");
2126 *non_constant_p = true;
60813a46 2127 }
60813a46 2128 }
0567dcd2 2129
04e1749c
MP
2130 /* At this point, the object's constructor will have run, so
2131 the object is no longer under construction, and its possible
2132 'const' semantics now apply. Make a note of this fact by
2133 marking the CONSTRUCTOR TREE_READONLY. */
2134 if (new_obj
2135 && CLASS_TYPE_P (TREE_TYPE (new_obj))
2136 && CP_TYPE_CONST_P (TREE_TYPE (new_obj)))
2137 {
8e007055
JJ
2138 /* Subobjects might not be stored in ctx->global->values but we
2139 can get its CONSTRUCTOR by evaluating *this. */
04e1749c
MP
2140 tree e = cxx_eval_constant_expression (ctx, new_obj,
2141 /*lval*/false,
2142 non_constant_p,
2143 overflow_p);
2144 TREE_READONLY (e) = true;
2145 }
2146
ee1de08d
JJ
2147 /* Forget the saved values of the callee's SAVE_EXPRs and
2148 TARGET_EXPRs. */
0ee28590
JJ
2149 unsigned int i;
2150 tree save_expr;
2151 FOR_EACH_VEC_ELT (save_exprs, i, save_expr)
8e007055 2152 ctx->global->values.remove (save_expr);
c0daf32d 2153
0567dcd2
MP
2154 /* Remove the parms/result from the values map. Is it worth
2155 bothering to do this when the map itself is only live for
2156 one constexpr evaluation? If so, maybe also clear out
2157 other vars from call, maybe in BIND_EXPR handling? */
8e007055 2158 ctx->global->values.remove (res);
0567dcd2 2159 if (slot)
8e007055 2160 ctx->global->values.remove (slot);
0567dcd2 2161 for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
8e007055 2162 ctx->global->values.remove (parm);
c0daf32d 2163
620adbec
JM
2164 /* Free any parameter CONSTRUCTORs we aren't returning directly. */
2165 while (!ctors->is_empty ())
2166 {
2167 tree c = ctors->pop ();
2168 if (c != result)
2169 free_constructor (c);
2170 }
2171
c0daf32d
PP
2172 /* Make the unshared function copy we used available for re-use. */
2173 save_fundef_copy (fun, copy);
3e605b20 2174 }
60813a46 2175
2d76680f
PC
2176 if (result == error_mark_node)
2177 *non_constant_p = true;
52228180 2178 if (*non_constant_p || *overflow_p)
12d9ce19 2179 result = error_mark_node;
0567dcd2 2180 else if (!result)
60813a46 2181 result = void_node;
12d9ce19
JM
2182 if (entry)
2183 entry->result = result;
2d76680f
PC
2184 }
2185
7906797e
MP
2186 /* The result of a constexpr function must be completely initialized.
2187
2188 However, in C++20, a constexpr constructor doesn't necessarily have
2189 to initialize all the fields, so we don't clear CONSTRUCTOR_NO_CLEARING
2190 in order to detect reading an unitialized object in constexpr instead
2191 of value-initializing it. (reduced_constant_expression_p is expected to
2192 take care of clearing the flag.) */
2193 if (TREE_CODE (result) == CONSTRUCTOR
2194 && (cxx_dialect < cxx2a
2195 || !DECL_CONSTRUCTOR_P (fun)))
ecc57615 2196 clear_no_implicit_zero (result);
f64e0c02 2197
2d76680f 2198 pop_cx_call_context ();
9b9eb42a 2199 return result;
2d76680f
PC
2200}
2201
7906797e
MP
2202/* Return true if T is a valid constant initializer. If a CONSTRUCTOR
2203 initializes all the members, the CONSTRUCTOR_NO_CLEARING flag will be
2204 cleared.
2205 FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
2d76680f
PC
2206
2207bool
2208reduced_constant_expression_p (tree t)
2209{
4930c53e
MP
2210 if (t == NULL_TREE)
2211 return false;
2212
2d76680f
PC
2213 switch (TREE_CODE (t))
2214 {
2215 case PTRMEM_CST:
2216 /* Even if we can't lower this yet, it's constant. */
2217 return true;
2218
2219 case CONSTRUCTOR:
2220 /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
7368cfa4 2221 tree idx, val, field; unsigned HOST_WIDE_INT i;
e8c48716 2222 if (CONSTRUCTOR_NO_CLEARING (t))
6f11ddd8
JM
2223 {
2224 if (TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
2225 /* An initialized vector would have a VECTOR_CST. */
2226 return false;
7906797e
MP
2227 else if (cxx_dialect >= cxx2a
2228 /* An ARRAY_TYPE doesn't have any TYPE_FIELDS. */
2229 && (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
2230 /* A union only initializes one member. */
2231 || TREE_CODE (TREE_TYPE (t)) == UNION_TYPE))
2232 field = NULL_TREE;
6f11ddd8
JM
2233 else
2234 field = next_initializable_field (TYPE_FIELDS (TREE_TYPE (t)));
2235 }
7368cfa4
JM
2236 else
2237 field = NULL_TREE;
2238 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), i, idx, val)
125db6a1 2239 {
4930c53e
MP
2240 /* If VAL is null, we're in the middle of initializing this
2241 element. */
7368cfa4 2242 if (!reduced_constant_expression_p (val))
125db6a1 2243 return false;
7368cfa4
JM
2244 if (field)
2245 {
7906797e
MP
2246 /* Empty class field may or may not have an initializer. */
2247 for (; idx != field;
2248 field = next_initializable_field (DECL_CHAIN (field)))
2249 if (!is_really_empty_class (TREE_TYPE (field),
2250 /*ignore_vptr*/false))
2251 return false;
7368cfa4
JM
2252 field = next_initializable_field (DECL_CHAIN (field));
2253 }
125db6a1 2254 }
7906797e
MP
2255 /* There could be a non-empty field at the end. */
2256 for (; field; field = next_initializable_field (DECL_CHAIN (field)))
2257 if (!is_really_empty_class (TREE_TYPE (field), /*ignore_vptr*/false))
2258 return false;
2259 if (CONSTRUCTOR_NO_CLEARING (t))
7368cfa4 2260 /* All the fields are initialized. */
e8c48716 2261 CONSTRUCTOR_NO_CLEARING (t) = false;
2d76680f
PC
2262 return true;
2263
2264 default:
2265 /* FIXME are we calling this too much? */
2266 return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE;
2267 }
2268}
2269
2270/* Some expressions may have constant operands but are not constant
7d75ea04
JJ
2271 themselves, such as 1/0. Call this function to check for that
2272 condition.
2d76680f
PC
2273
2274 We only call this in places that require an arithmetic constant, not in
2275 places where we might have a non-constant expression that can be a
2276 component of a constant expression, such as the address of a constexpr
2277 variable that might be dereferenced later. */
2278
2279static bool
2280verify_constant (tree t, bool allow_non_constant, bool *non_constant_p,
2281 bool *overflow_p)
2282{
2283 if (!*non_constant_p && !reduced_constant_expression_p (t))
2284 {
2285 if (!allow_non_constant)
2286 error ("%q+E is not a constant expression", t);
2287 *non_constant_p = true;
2288 }
2289 if (TREE_OVERFLOW_P (t))
2290 {
2291 if (!allow_non_constant)
2292 {
2293 permerror (input_location, "overflow in constant expression");
2294 /* If we're being permissive (and are in an enforcing
2295 context), ignore the overflow. */
2296 if (flag_permissive)
2297 return *non_constant_p;
2298 }
2299 *overflow_p = true;
2300 }
2301 return *non_constant_p;
2302}
2303
253a921b
MP
2304/* Check whether the shift operation with code CODE and type TYPE on LHS
2305 and RHS is undefined. If it is, give an error with an explanation,
2306 and return true; return false otherwise. */
2307
2308static bool
2309cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx,
2310 enum tree_code code, tree type, tree lhs, tree rhs)
2311{
2312 if ((code != LSHIFT_EXPR && code != RSHIFT_EXPR)
2313 || TREE_CODE (lhs) != INTEGER_CST
2314 || TREE_CODE (rhs) != INTEGER_CST)
2315 return false;
2316
2317 tree lhstype = TREE_TYPE (lhs);
2318 unsigned HOST_WIDE_INT uprec = TYPE_PRECISION (TREE_TYPE (lhs));
2319
2320 /* [expr.shift] The behavior is undefined if the right operand
2321 is negative, or greater than or equal to the length in bits
2322 of the promoted left operand. */
2323 if (tree_int_cst_sgn (rhs) == -1)
2324 {
2325 if (!ctx->quiet)
5342156c
MP
2326 permerror (loc, "right operand of shift expression %q+E is negative",
2327 build2_loc (loc, code, type, lhs, rhs));
2328 return (!flag_permissive || ctx->quiet);
253a921b
MP
2329 }
2330 if (compare_tree_int (rhs, uprec) >= 0)
2331 {
2332 if (!ctx->quiet)
a9c697b8
MS
2333 permerror (loc, "right operand of shift expression %q+E is greater "
2334 "than or equal to the precision %wu of the left operand",
2335 build2_loc (loc, code, type, lhs, rhs), uprec);
5342156c 2336 return (!flag_permissive || ctx->quiet);
253a921b
MP
2337 }
2338
2339 /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
2340 if E1 has a signed type and non-negative value, and E1x2^E2 is
2341 representable in the corresponding unsigned type of the result type,
2342 then that value, converted to the result type, is the resulting value;
8ee09943
JJ
2343 otherwise, the behavior is undefined.
2344 For C++2a:
2345 The value of E1 << E2 is the unique value congruent to E1 x 2^E2 modulo
2346 2^N, where N is the range exponent of the type of the result. */
2347 if (code == LSHIFT_EXPR
2348 && !TYPE_UNSIGNED (lhstype)
2349 && cxx_dialect >= cxx11
2350 && cxx_dialect < cxx2a)
253a921b
MP
2351 {
2352 if (tree_int_cst_sgn (lhs) == -1)
2353 {
2354 if (!ctx->quiet)
5342156c
MP
2355 permerror (loc,
2356 "left operand of shift expression %q+E is negative",
2357 build2_loc (loc, code, type, lhs, rhs));
2358 return (!flag_permissive || ctx->quiet);
253a921b
MP
2359 }
2360 /* For signed x << y the following:
2361 (unsigned) x >> ((prec (lhs) - 1) - y)
2362 if > 1, is undefined. The right-hand side of this formula
2363 is the highest bit of the LHS that can be set (starting from 0),
2364 so that the shift doesn't overflow. We then right-shift the LHS
2365 to see whether any other bit is set making the original shift
2366 undefined -- the result is not representable in the corresponding
2367 unsigned type. */
2368 tree t = build_int_cst (unsigned_type_node, uprec - 1);
2369 t = fold_build2 (MINUS_EXPR, unsigned_type_node, t, rhs);
2370 tree ulhs = fold_convert (unsigned_type_for (lhstype), lhs);
2371 t = fold_build2 (RSHIFT_EXPR, TREE_TYPE (ulhs), ulhs, t);
2372 if (tree_int_cst_lt (integer_one_node, t))
2373 {
2374 if (!ctx->quiet)
5342156c
MP
2375 permerror (loc, "shift expression %q+E overflows",
2376 build2_loc (loc, code, type, lhs, rhs));
2377 return (!flag_permissive || ctx->quiet);
253a921b
MP
2378 }
2379 }
2380 return false;
2381}
2382
2d76680f
PC
2383/* Subroutine of cxx_eval_constant_expression.
2384 Attempt to reduce the unary expression tree T to a compile time value.
2385 If successful, return the value. Otherwise issue a diagnostic
2386 and return error_mark_node. */
2387
2388static tree
3e605b20 2389cxx_eval_unary_expression (const constexpr_ctx *ctx, tree t,
12d9ce19 2390 bool /*lval*/,
2d76680f
PC
2391 bool *non_constant_p, bool *overflow_p)
2392{
2393 tree r;
2394 tree orig_arg = TREE_OPERAND (t, 0);
12d9ce19
JM
2395 tree arg = cxx_eval_constant_expression (ctx, orig_arg, /*lval*/false,
2396 non_constant_p, overflow_p);
2d76680f 2397 VERIFY_CONSTANT (arg);
f899317e
JM
2398 location_t loc = EXPR_LOCATION (t);
2399 enum tree_code code = TREE_CODE (t);
2400 tree type = TREE_TYPE (t);
2401 r = fold_unary_loc (loc, code, type, arg);
2402 if (r == NULL_TREE)
2403 {
2404 if (arg == orig_arg)
2405 r = t;
2406 else
2407 r = build1_loc (loc, code, type, arg);
2408 }
2d76680f
PC
2409 VERIFY_CONSTANT (r);
2410 return r;
2411}
2412
ea8661cd
JJ
2413/* Helper function for cxx_eval_binary_expression. Try to optimize
2414 original POINTER_PLUS_EXPR T, LHS p+ RHS, return NULL_TREE if the
2415 generic folding should be used. */
2416
2417static tree
2418cxx_fold_pointer_plus_expression (const constexpr_ctx *ctx, tree t,
2419 tree lhs, tree rhs, bool *non_constant_p,
2420 bool *overflow_p)
2421{
2422 STRIP_NOPS (lhs);
2423 if (TREE_CODE (lhs) != ADDR_EXPR)
2424 return NULL_TREE;
2425
2426 lhs = TREE_OPERAND (lhs, 0);
2427
2428 /* &A[i] p+ j => &A[i + j] */
2429 if (TREE_CODE (lhs) == ARRAY_REF
2430 && TREE_CODE (TREE_OPERAND (lhs, 1)) == INTEGER_CST
2431 && TREE_CODE (rhs) == INTEGER_CST
2432 && TYPE_SIZE_UNIT (TREE_TYPE (lhs))
2433 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs))) == INTEGER_CST)
2434 {
2435 tree orig_type = TREE_TYPE (t);
2436 location_t loc = EXPR_LOCATION (t);
2437 tree type = TREE_TYPE (lhs);
2438
2439 t = fold_convert_loc (loc, ssizetype, TREE_OPERAND (lhs, 1));
2440 tree nelts = array_type_nelts_top (TREE_TYPE (TREE_OPERAND (lhs, 0)));
2441 nelts = cxx_eval_constant_expression (ctx, nelts, false, non_constant_p,
2442 overflow_p);
2443 if (*non_constant_p)
2444 return NULL_TREE;
2445 /* Don't fold an out-of-bound access. */
2446 if (!tree_int_cst_le (t, nelts))
2447 return NULL_TREE;
2448 rhs = cp_fold_convert (ssizetype, rhs);
2449 /* Don't fold if rhs can't be divided exactly by TYPE_SIZE_UNIT.
2450 constexpr int A[1]; ... (char *)&A[0] + 1 */
2451 if (!integer_zerop (fold_build2_loc (loc, TRUNC_MOD_EXPR, sizetype,
2452 rhs, TYPE_SIZE_UNIT (type))))
2453 return NULL_TREE;
2454 /* Make sure to treat the second operand of POINTER_PLUS_EXPR
2455 as signed. */
2456 rhs = fold_build2_loc (loc, EXACT_DIV_EXPR, ssizetype, rhs,
2457 TYPE_SIZE_UNIT (type));
2458 t = size_binop_loc (loc, PLUS_EXPR, rhs, t);
2459 t = build4_loc (loc, ARRAY_REF, type, TREE_OPERAND (lhs, 0),
2460 t, NULL_TREE, NULL_TREE);
2461 t = cp_build_addr_expr (t, tf_warning_or_error);
2462 t = cp_fold_convert (orig_type, t);
2463 return cxx_eval_constant_expression (ctx, t, /*lval*/false,
2464 non_constant_p, overflow_p);
2465 }
2466
2467 return NULL_TREE;
2468}
2469
2d76680f
PC
2470/* Subroutine of cxx_eval_constant_expression.
2471 Like cxx_eval_unary_expression, except for binary expressions. */
2472
2473static tree
3e605b20 2474cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t,
12d9ce19 2475 bool /*lval*/,
2d76680f
PC
2476 bool *non_constant_p, bool *overflow_p)
2477{
618d6c1c 2478 tree r = NULL_TREE;
2d76680f
PC
2479 tree orig_lhs = TREE_OPERAND (t, 0);
2480 tree orig_rhs = TREE_OPERAND (t, 1);
2481 tree lhs, rhs;
12d9ce19 2482 lhs = cxx_eval_constant_expression (ctx, orig_lhs, /*lval*/false,
5a804683 2483 non_constant_p, overflow_p);
c8a66fc9
JM
2484 /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer
2485 subtraction. */
2486 if (*non_constant_p)
2487 return t;
12d9ce19 2488 rhs = cxx_eval_constant_expression (ctx, orig_rhs, /*lval*/false,
5a804683 2489 non_constant_p, overflow_p);
c8a66fc9
JM
2490 if (*non_constant_p)
2491 return t;
f899317e
JM
2492
2493 location_t loc = EXPR_LOCATION (t);
2494 enum tree_code code = TREE_CODE (t);
2495 tree type = TREE_TYPE (t);
618d6c1c
PP
2496
2497 if (code == EQ_EXPR || code == NE_EXPR)
2498 {
2499 bool is_code_eq = (code == EQ_EXPR);
2500
2501 if (TREE_CODE (lhs) == PTRMEM_CST
2502 && TREE_CODE (rhs) == PTRMEM_CST)
9b0607de
JM
2503 {
2504 tree lmem = PTRMEM_CST_MEMBER (lhs);
2505 tree rmem = PTRMEM_CST_MEMBER (rhs);
2506 bool eq;
2507 if (TREE_CODE (lmem) == TREE_CODE (rmem)
2508 && TREE_CODE (lmem) == FIELD_DECL
2509 && TREE_CODE (DECL_CONTEXT (lmem)) == UNION_TYPE
2510 && same_type_p (DECL_CONTEXT (lmem),
2511 DECL_CONTEXT (rmem)))
2512 /* If both refer to (possibly different) members of the same union
2513 (12.3), they compare equal. */
2514 eq = true;
2515 else
2516 eq = cp_tree_equal (lhs, rhs);
2517 r = constant_boolean_node (eq == is_code_eq, type);
2518 }
618d6c1c
PP
2519 else if ((TREE_CODE (lhs) == PTRMEM_CST
2520 || TREE_CODE (rhs) == PTRMEM_CST)
2521 && (null_member_pointer_value_p (lhs)
2522 || null_member_pointer_value_p (rhs)))
2523 r = constant_boolean_node (!is_code_eq, type);
dd5dda56
JM
2524 else if (TREE_CODE (lhs) == PTRMEM_CST)
2525 lhs = cplus_expand_constant (lhs);
2526 else if (TREE_CODE (rhs) == PTRMEM_CST)
2527 rhs = cplus_expand_constant (rhs);
618d6c1c 2528 }
8bada5cd
MS
2529 if (code == POINTER_PLUS_EXPR && !*non_constant_p
2530 && integer_zerop (lhs) && !integer_zerop (rhs))
2531 {
2532 if (!ctx->quiet)
2533 error ("arithmetic involving a null pointer in %qE", lhs);
bf533db8 2534 *non_constant_p = true;
8bada5cd
MS
2535 return t;
2536 }
ea8661cd
JJ
2537 else if (code == POINTER_PLUS_EXPR)
2538 r = cxx_fold_pointer_plus_expression (ctx, t, lhs, rhs, non_constant_p,
2539 overflow_p);
b7689b96
JM
2540 else if (code == SPACESHIP_EXPR)
2541 {
2542 r = genericize_spaceship (type, lhs, rhs);
2543 r = cxx_eval_constant_expression (ctx, r, false, non_constant_p,
2544 overflow_p);
2545 }
618d6c1c
PP
2546
2547 if (r == NULL_TREE)
2548 r = fold_binary_loc (loc, code, type, lhs, rhs);
2549
f899317e
JM
2550 if (r == NULL_TREE)
2551 {
2552 if (lhs == orig_lhs && rhs == orig_rhs)
2553 r = t;
2554 else
2555 r = build2_loc (loc, code, type, lhs, rhs);
2556 }
253a921b
MP
2557 else if (cxx_eval_check_shift_p (loc, ctx, code, type, lhs, rhs))
2558 *non_constant_p = true;
c8a66fc9
JM
2559 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
2560 a local array in a constexpr function. */
71a93b08 2561 bool ptr = INDIRECT_TYPE_P (TREE_TYPE (lhs));
caee690e 2562 if (!ptr)
134efa82 2563 VERIFY_CONSTANT (r);
2d76680f
PC
2564 return r;
2565}
2566
2567/* Subroutine of cxx_eval_constant_expression.
2568 Attempt to evaluate condition expressions. Dead branches are not
2569 looked into. */
2570
2571static tree
3e605b20 2572cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t,
92a596e8 2573 bool lval,
56632b27
JM
2574 bool *non_constant_p, bool *overflow_p,
2575 tree *jump_target)
2d76680f 2576{
3e605b20 2577 tree val = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
12d9ce19 2578 /*lval*/false,
5a804683 2579 non_constant_p, overflow_p);
2d76680f
PC
2580 VERIFY_CONSTANT (val);
2581 /* Don't VERIFY_CONSTANT the other operands. */
2582 if (integer_zerop (val))
43574e4f
JJ
2583 val = TREE_OPERAND (t, 2);
2584 else
2585 val = TREE_OPERAND (t, 1);
2586 if (TREE_CODE (t) == IF_STMT && !val)
2587 val = void_node;
2588 return cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
2589 overflow_p, jump_target);
2d76680f
PC
2590}
2591
f370e36d
JJ
2592/* Subroutine of cxx_eval_constant_expression.
2593 Attempt to evaluate vector condition expressions. Unlike
2594 cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal
2595 ternary arithmetics operation, where all 3 arguments have to be
2596 evaluated as constants and then folding computes the result from
2597 them. */
2598
2599static tree
2600cxx_eval_vector_conditional_expression (const constexpr_ctx *ctx, tree t,
2601 bool *non_constant_p, bool *overflow_p)
2602{
2603 tree arg1 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
2604 /*lval*/false,
2605 non_constant_p, overflow_p);
2606 VERIFY_CONSTANT (arg1);
2607 tree arg2 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
2608 /*lval*/false,
2609 non_constant_p, overflow_p);
2610 VERIFY_CONSTANT (arg2);
2611 tree arg3 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2),
2612 /*lval*/false,
2613 non_constant_p, overflow_p);
2614 VERIFY_CONSTANT (arg3);
2615 location_t loc = EXPR_LOCATION (t);
2616 tree type = TREE_TYPE (t);
2617 tree r = fold_ternary_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
2618 if (r == NULL_TREE)
2619 {
2620 if (arg1 == TREE_OPERAND (t, 0)
2621 && arg2 == TREE_OPERAND (t, 1)
2622 && arg3 == TREE_OPERAND (t, 2))
2623 r = t;
2624 else
2625 r = build3_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
2626 }
2627 VERIFY_CONSTANT (r);
2628 return r;
2629}
2630
ceaaf873
JM
2631/* Returns less than, equal to, or greater than zero if KEY is found to be
2632 less than, to match, or to be greater than the constructor_elt's INDEX. */
2633
2634static int
2635array_index_cmp (tree key, tree index)
2636{
2637 gcc_assert (TREE_CODE (key) == INTEGER_CST);
2638
2639 switch (TREE_CODE (index))
2640 {
2641 case INTEGER_CST:
2642 return tree_int_cst_compare (key, index);
2643 case RANGE_EXPR:
2644 {
2645 tree lo = TREE_OPERAND (index, 0);
2646 tree hi = TREE_OPERAND (index, 1);
2647 if (tree_int_cst_lt (key, lo))
2648 return -1;
2649 else if (tree_int_cst_lt (hi, key))
2650 return 1;
2651 else
2652 return 0;
2653 }
2654 default:
2655 gcc_unreachable ();
2656 }
2657}
2658
2659/* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1
2660 if none. If INSERT is true, insert a matching element rather than fail. */
2661
2662static HOST_WIDE_INT
582d2481 2663find_array_ctor_elt (tree ary, tree dindex, bool insert)
ceaaf873
JM
2664{
2665 if (tree_int_cst_sgn (dindex) < 0)
2666 return -1;
2667
2668 unsigned HOST_WIDE_INT i = tree_to_uhwi (dindex);
2669 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ary);
2670 unsigned HOST_WIDE_INT len = vec_safe_length (elts);
2671
2672 unsigned HOST_WIDE_INT end = len;
2673 unsigned HOST_WIDE_INT begin = 0;
2674
2675 /* If the last element of the CONSTRUCTOR has its own index, we can assume
2676 that the same is true of the other elements and index directly. */
2677 if (end > 0)
2678 {
fe217ba0 2679 tree cindex = (*elts)[end - 1].index;
ceaaf873 2680 if (TREE_CODE (cindex) == INTEGER_CST
fe217ba0 2681 && compare_tree_int (cindex, end - 1) == 0)
ceaaf873
JM
2682 {
2683 if (i < end)
2684 return i;
2685 else
2686 begin = end;
2687 }
2688 }
2689
2690 /* Otherwise, find a matching index by means of a binary search. */
2691 while (begin != end)
2692 {
2693 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
a7ccb9e7
JM
2694 constructor_elt &elt = (*elts)[middle];
2695 tree idx = elt.index;
ceaaf873 2696
a7ccb9e7 2697 int cmp = array_index_cmp (dindex, idx);
ceaaf873
JM
2698 if (cmp < 0)
2699 end = middle;
2700 else if (cmp > 0)
2701 begin = middle + 1;
2702 else
a7ccb9e7
JM
2703 {
2704 if (insert && TREE_CODE (idx) == RANGE_EXPR)
2705 {
2706 /* We need to split the range. */
2707 constructor_elt e;
2708 tree lo = TREE_OPERAND (idx, 0);
2709 tree hi = TREE_OPERAND (idx, 1);
fe217ba0
JJ
2710 tree value = elt.value;
2711 dindex = fold_convert (sizetype, dindex);
a7ccb9e7
JM
2712 if (tree_int_cst_lt (lo, dindex))
2713 {
2714 /* There are still some lower elts; shorten the range. */
2715 tree new_hi = int_const_binop (MINUS_EXPR, dindex,
2716 size_one_node);
2717 if (tree_int_cst_equal (lo, new_hi))
2718 /* Only one element left, no longer a range. */
2719 elt.index = lo;
2720 else
2721 TREE_OPERAND (idx, 1) = new_hi;
2722 /* Append the element we want to insert. */
2723 ++middle;
2724 e.index = dindex;
fe217ba0 2725 e.value = unshare_constructor (value);
a7ccb9e7
JM
2726 vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e);
2727 }
2728 else
2729 /* No lower elts, the range elt is now ours. */
2730 elt.index = dindex;
2731
2732 if (tree_int_cst_lt (dindex, hi))
2733 {
2734 /* There are still some higher elts; append a range. */
2735 tree new_lo = int_const_binop (PLUS_EXPR, dindex,
2736 size_one_node);
2737 if (tree_int_cst_equal (new_lo, hi))
2738 e.index = hi;
2739 else
2740 e.index = build2 (RANGE_EXPR, sizetype, new_lo, hi);
fe217ba0
JJ
2741 e.value = unshare_constructor (value);
2742 vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e);
a7ccb9e7
JM
2743 }
2744 }
2745 return middle;
2746 }
ceaaf873
JM
2747 }
2748
2749 if (insert)
2750 {
2751 constructor_elt e = { dindex, NULL_TREE };
2752 vec_safe_insert (CONSTRUCTOR_ELTS (ary), end, e);
2753 return end;
2754 }
2755
2756 return -1;
2757}
2758
abdc16c8
MS
2759/* Under the control of CTX, issue a detailed diagnostic for
2760 an out-of-bounds subscript INDEX into the expression ARRAY. */
2761
2762static void
043666e0 2763diag_array_subscript (location_t loc, const constexpr_ctx *ctx, tree array, tree index)
abdc16c8
MS
2764{
2765 if (!ctx->quiet)
2766 {
2767 tree arraytype = TREE_TYPE (array);
2768
2769 /* Convert the unsigned array subscript to a signed integer to avoid
2770 printing huge numbers for small negative values. */
2771 tree sidx = fold_convert (ssizetype, index);
043666e0 2772 STRIP_ANY_LOCATION_WRAPPER (array);
abdc16c8
MS
2773 if (DECL_P (array))
2774 {
08b3748c 2775 if (TYPE_DOMAIN (arraytype))
043666e0
JM
2776 error_at (loc, "array subscript value %qE is outside the bounds "
2777 "of array %qD of type %qT", sidx, array, arraytype);
08b3748c 2778 else
043666e0
JM
2779 error_at (loc, "nonzero array subscript %qE is used with array %qD of "
2780 "type %qT with unknown bounds", sidx, array, arraytype);
abdc16c8
MS
2781 inform (DECL_SOURCE_LOCATION (array), "declared here");
2782 }
08b3748c 2783 else if (TYPE_DOMAIN (arraytype))
043666e0
JM
2784 error_at (loc, "array subscript value %qE is outside the bounds "
2785 "of array type %qT", sidx, arraytype);
08b3748c 2786 else
043666e0
JM
2787 error_at (loc, "nonzero array subscript %qE is used with array of type %qT "
2788 "with unknown bounds", sidx, arraytype);
abdc16c8
MS
2789 }
2790}
ceaaf873 2791
74f8705e
MP
2792/* Return the number of elements for TYPE (which is an ARRAY_TYPE or
2793 a VECTOR_TYPE). */
2794
2795static tree
2796get_array_or_vector_nelts (const constexpr_ctx *ctx, tree type,
2797 bool *non_constant_p, bool *overflow_p)
2798{
2799 tree nelts;
2800 if (TREE_CODE (type) == ARRAY_TYPE)
2801 {
2802 if (TYPE_DOMAIN (type))
2803 nelts = array_type_nelts_top (type);
2804 else
2805 nelts = size_zero_node;
2806 }
2807 else if (VECTOR_TYPE_P (type))
2808 nelts = size_int (TYPE_VECTOR_SUBPARTS (type));
2809 else
2810 gcc_unreachable ();
2811
2812 /* For VLAs, the number of elements won't be an integer constant. */
2813 nelts = cxx_eval_constant_expression (ctx, nelts, false,
2814 non_constant_p, overflow_p);
2815 return nelts;
2816}
2817
d6b46fca
NS
2818/* Extract element INDEX consisting of CHARS_PER_ELT chars from
2819 STRING_CST STRING. */
2820
2821static tree
2822extract_string_elt (tree string, unsigned chars_per_elt, unsigned index)
2823{
2824 tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (string)));
2825 tree r;
2826
2827 if (chars_per_elt == 1)
2828 r = build_int_cst (type, TREE_STRING_POINTER (string)[index]);
2829 else
2830 {
2831 const unsigned char *ptr
2832 = ((const unsigned char *)TREE_STRING_POINTER (string)
2833 + index * chars_per_elt);
2834 r = native_interpret_expr (type, ptr, chars_per_elt);
2835 }
2836 return r;
2837}
2838
043666e0
JM
2839/* Subroutine of cxx_eval_array_reference. T is an ARRAY_REF; evaluate the
2840 subscript, diagnose any problems with it, and return the result. */
2841
2842static tree
2843eval_and_check_array_index (const constexpr_ctx *ctx,
2844 tree t, bool allow_one_past,
2845 bool *non_constant_p, bool *overflow_p)
2846{
f9d0ca40 2847 location_t loc = cp_expr_loc_or_input_loc (t);
043666e0
JM
2848 tree ary = TREE_OPERAND (t, 0);
2849 t = TREE_OPERAND (t, 1);
2850 tree index = cxx_eval_constant_expression (ctx, t, false,
2851 non_constant_p, overflow_p);
2852 VERIFY_CONSTANT (index);
2853
2854 if (!tree_fits_shwi_p (index)
2855 || tree_int_cst_sgn (index) < 0)
2856 {
2857 diag_array_subscript (loc, ctx, ary, index);
2858 *non_constant_p = true;
2859 return t;
2860 }
2861
2862 tree nelts = get_array_or_vector_nelts (ctx, TREE_TYPE (ary), non_constant_p,
2863 overflow_p);
2864 VERIFY_CONSTANT (nelts);
2865 if (allow_one_past
2866 ? !tree_int_cst_le (index, nelts)
2867 : !tree_int_cst_lt (index, nelts))
2868 {
2869 diag_array_subscript (loc, ctx, ary, index);
2870 *non_constant_p = true;
2871 return t;
2872 }
2873
2874 return index;
2875}
2876
2d76680f
PC
2877/* Subroutine of cxx_eval_constant_expression.
2878 Attempt to reduce a reference to an array slot. */
2879
2880static tree
3e605b20 2881cxx_eval_array_reference (const constexpr_ctx *ctx, tree t,
92a596e8 2882 bool lval,
2d76680f
PC
2883 bool *non_constant_p, bool *overflow_p)
2884{
2885 tree oldary = TREE_OPERAND (t, 0);
3e605b20 2886 tree ary = cxx_eval_constant_expression (ctx, oldary,
92a596e8 2887 lval,
5a804683 2888 non_constant_p, overflow_p);
2d76680f
PC
2889 if (*non_constant_p)
2890 return t;
bc2687dd
JJ
2891 if (!lval
2892 && TREE_CODE (ary) == VIEW_CONVERT_EXPR
043666e0
JM
2893 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary, 0)))
2894 && TREE_TYPE (t) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary, 0))))
2895 ary = TREE_OPERAND (ary, 0);
3b9997bb 2896
043666e0
JM
2897 tree oldidx = TREE_OPERAND (t, 1);
2898 tree index = eval_and_check_array_index (ctx, t, lval,
2899 non_constant_p, overflow_p);
2900 if (*non_constant_p)
2901 return t;
5453bfed 2902
bc2a38df
JJ
2903 if (lval && ary == oldary && index == oldidx)
2904 return t;
2905 else if (lval)
2906 return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL);
2907
043666e0
JM
2908 unsigned len = 0, elem_nchars = 1;
2909 tree elem_type = TREE_TYPE (TREE_TYPE (ary));
2910 if (TREE_CODE (ary) == CONSTRUCTOR)
2911 len = CONSTRUCTOR_NELTS (ary);
2912 else if (TREE_CODE (ary) == STRING_CST)
2913 {
2914 elem_nchars = (TYPE_PRECISION (elem_type)
2915 / TYPE_PRECISION (char_type_node));
2916 len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars;
2917 }
2918 else if (TREE_CODE (ary) == VECTOR_CST)
2919 /* We don't create variable-length VECTOR_CSTs. */
2920 len = VECTOR_CST_NELTS (ary).to_constant ();
2921 else
2922 {
2923 /* We can't do anything with other tree codes, so use
2924 VERIFY_CONSTANT to complain and fail. */
2925 VERIFY_CONSTANT (ary);
2926 gcc_unreachable ();
2927 }
2928
ceaaf873 2929 bool found;
043666e0 2930 HOST_WIDE_INT i = 0;
ceaaf873
JM
2931 if (TREE_CODE (ary) == CONSTRUCTOR)
2932 {
2933 HOST_WIDE_INT ix = find_array_ctor_elt (ary, index);
2934 found = (ix >= 0);
2935 if (found)
2936 i = ix;
3b9997bb 2937 }
ceaaf873 2938 else
043666e0
JM
2939 {
2940 i = tree_to_shwi (index);
2941 found = (i < len);
2942 }
3b9997bb 2943
fd2bfee5
JM
2944 if (found)
2945 {
2946 tree r;
2947 if (TREE_CODE (ary) == CONSTRUCTOR)
2948 r = (*CONSTRUCTOR_ELTS (ary))[i].value;
2949 else if (TREE_CODE (ary) == VECTOR_CST)
2950 r = VECTOR_CST_ELT (ary, i);
fd2bfee5 2951 else
d6b46fca
NS
2952 r = extract_string_elt (ary, elem_nchars, i);
2953
fd2bfee5
JM
2954 if (r)
2955 /* Don't VERIFY_CONSTANT here. */
2956 return r;
2d76680f 2957
fd2bfee5 2958 /* Otherwise the element doesn't have a value yet. */
2d76680f 2959 }
3b9997bb 2960
fd2bfee5
JM
2961 /* Not found. */
2962
2963 if (TREE_CODE (ary) == CONSTRUCTOR
e8c48716 2964 && CONSTRUCTOR_NO_CLEARING (ary))
2d76680f 2965 {
fd2bfee5
JM
2966 /* 'ary' is part of the aggregate initializer we're currently
2967 building; if there's no initializer for this element yet,
2968 that's an error. */
2969 if (!ctx->quiet)
2970 error ("accessing uninitialized array element");
2971 *non_constant_p = true;
2972 return t;
2d76680f 2973 }
fd2bfee5
JM
2974
2975 /* If it's within the array bounds but doesn't have an explicit
2976 initializer, it's value-initialized. */
2977 tree val = build_value_init (elem_type, tf_warning_or_error);
2978 return cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
2979 overflow_p);
2d76680f
PC
2980}
2981
2982/* Subroutine of cxx_eval_constant_expression.
2983 Attempt to reduce a field access of a value of class type. */
2984
2985static tree
3e605b20 2986cxx_eval_component_reference (const constexpr_ctx *ctx, tree t,
92a596e8 2987 bool lval,
2d76680f
PC
2988 bool *non_constant_p, bool *overflow_p)
2989{
2990 unsigned HOST_WIDE_INT i;
2991 tree field;
2992 tree value;
2993 tree part = TREE_OPERAND (t, 1);
2994 tree orig_whole = TREE_OPERAND (t, 0);
3e605b20 2995 tree whole = cxx_eval_constant_expression (ctx, orig_whole,
92a596e8 2996 lval,
5a804683 2997 non_constant_p, overflow_p);
a7f8415c 2998 if (INDIRECT_REF_P (whole)
bf533db8
JJ
2999 && integer_zerop (TREE_OPERAND (whole, 0)))
3000 {
3001 if (!ctx->quiet)
3002 error ("dereferencing a null pointer in %qE", orig_whole);
3003 *non_constant_p = true;
3004 return t;
3005 }
8bada5cd 3006
dcdbc004
JM
3007 if (TREE_CODE (whole) == PTRMEM_CST)
3008 whole = cplus_expand_constant (whole);
2d76680f
PC
3009 if (whole == orig_whole)
3010 return t;
92a596e8 3011 if (lval)
2d76680f
PC
3012 return fold_build3 (COMPONENT_REF, TREE_TYPE (t),
3013 whole, part, NULL_TREE);
3014 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
3015 CONSTRUCTOR. */
3016 if (!*non_constant_p && TREE_CODE (whole) != CONSTRUCTOR)
3017 {
2b3ab879 3018 if (!ctx->quiet)
2d76680f
PC
3019 error ("%qE is not a constant expression", orig_whole);
3020 *non_constant_p = true;
3021 }
3022 if (DECL_MUTABLE_P (part))
3023 {
2b3ab879 3024 if (!ctx->quiet)
2d76680f
PC
3025 error ("mutable %qD is not usable in a constant expression", part);
3026 *non_constant_p = true;
3027 }
3028 if (*non_constant_p)
3029 return t;
2db613e5 3030 bool pmf = TYPE_PTRMEMFUNC_P (TREE_TYPE (whole));
2d76680f
PC
3031 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
3032 {
2db613e5
JM
3033 /* Use name match for PMF fields, as a variant will have a
3034 different FIELD_DECL with a different type. */
3035 if (pmf ? DECL_NAME (field) == DECL_NAME (part)
3036 : field == part)
60813a46
JM
3037 {
3038 if (value)
5b884e94
JJ
3039 {
3040 STRIP_ANY_LOCATION_WRAPPER (value);
3041 return value;
3042 }
60813a46
JM
3043 else
3044 /* We're in the middle of initializing it. */
3045 break;
3046 }
2d76680f
PC
3047 }
3048 if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE
3049 && CONSTRUCTOR_NELTS (whole) > 0)
3050 {
3051 /* DR 1188 says we don't have to deal with this. */
2b3ab879 3052 if (!ctx->quiet)
2d76680f
PC
3053 error ("accessing %qD member instead of initialized %qD member in "
3054 "constant expression", part, CONSTRUCTOR_ELT (whole, 0)->index);
3055 *non_constant_p = true;
3056 return t;
3057 }
3058
188e53bd
JM
3059 /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty
3060 classes never get represented; throw together a value now. */
dbcd32f8 3061 if (is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
188e53bd
JM
3062 return build_constructor (TREE_TYPE (t), NULL);
3063
2db613e5
JM
3064 gcc_assert (DECL_CONTEXT (part) == TYPE_MAIN_VARIANT (TREE_TYPE (whole)));
3065
e8c48716 3066 if (CONSTRUCTOR_NO_CLEARING (whole))
3e605b20
JM
3067 {
3068 /* 'whole' is part of the aggregate initializer we're currently
3069 building; if there's no initializer for this member yet, that's an
188e53bd 3070 error. */
2b3ab879 3071 if (!ctx->quiet)
3e605b20
JM
3072 error ("accessing uninitialized member %qD", part);
3073 *non_constant_p = true;
3074 return t;
3075 }
3076
2d76680f
PC
3077 /* If there's no explicit init for this field, it's value-initialized. */
3078 value = build_value_init (TREE_TYPE (t), tf_warning_or_error);
3e605b20 3079 return cxx_eval_constant_expression (ctx, value,
92a596e8 3080 lval,
5a804683 3081 non_constant_p, overflow_p);
2d76680f
PC
3082}
3083
3084/* Subroutine of cxx_eval_constant_expression.
3085 Attempt to reduce a field access of a value of class type that is
3086 expressed as a BIT_FIELD_REF. */
3087
3088static tree
3e605b20 3089cxx_eval_bit_field_ref (const constexpr_ctx *ctx, tree t,
92a596e8 3090 bool lval,
2d76680f
PC
3091 bool *non_constant_p, bool *overflow_p)
3092{
3093 tree orig_whole = TREE_OPERAND (t, 0);
3094 tree retval, fldval, utype, mask;
3095 bool fld_seen = false;
3096 HOST_WIDE_INT istart, isize;
3e605b20 3097 tree whole = cxx_eval_constant_expression (ctx, orig_whole,
92a596e8 3098 lval,
5a804683 3099 non_constant_p, overflow_p);
2d76680f
PC
3100 tree start, field, value;
3101 unsigned HOST_WIDE_INT i;
3102
3103 if (whole == orig_whole)
3104 return t;
3105 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
3106 CONSTRUCTOR. */
3107 if (!*non_constant_p
3108 && TREE_CODE (whole) != VECTOR_CST
3109 && TREE_CODE (whole) != CONSTRUCTOR)
3110 {
2b3ab879 3111 if (!ctx->quiet)
2d76680f
PC
3112 error ("%qE is not a constant expression", orig_whole);
3113 *non_constant_p = true;
3114 }
3115 if (*non_constant_p)
3116 return t;
3117
3118 if (TREE_CODE (whole) == VECTOR_CST)
3119 return fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
3120 TREE_OPERAND (t, 1), TREE_OPERAND (t, 2));
3121
3122 start = TREE_OPERAND (t, 2);
3123 istart = tree_to_shwi (start);
3124 isize = tree_to_shwi (TREE_OPERAND (t, 1));
3125 utype = TREE_TYPE (t);
3126 if (!TYPE_UNSIGNED (utype))
3127 utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1);
3128 retval = build_int_cst (utype, 0);
3129 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
3130 {
3131 tree bitpos = bit_position (field);
5b884e94 3132 STRIP_ANY_LOCATION_WRAPPER (value);
2d76680f
PC
3133 if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1))
3134 return value;
3135 if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE
3136 && TREE_CODE (value) == INTEGER_CST
3137 && tree_fits_shwi_p (bitpos)
3138 && tree_fits_shwi_p (DECL_SIZE (field)))
3139 {
3140 HOST_WIDE_INT bit = tree_to_shwi (bitpos);
3141 HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field));
3142 HOST_WIDE_INT shift;
3143 if (bit >= istart && bit + sz <= istart + isize)
3144 {
3145 fldval = fold_convert (utype, value);
3146 mask = build_int_cst_type (utype, -1);
3147 mask = fold_build2 (LSHIFT_EXPR, utype, mask,
3148 size_int (TYPE_PRECISION (utype) - sz));
3149 mask = fold_build2 (RSHIFT_EXPR, utype, mask,
3150 size_int (TYPE_PRECISION (utype) - sz));
3151 fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask);
3152 shift = bit - istart;
3153 if (BYTES_BIG_ENDIAN)
3154 shift = TYPE_PRECISION (utype) - shift - sz;
3155 fldval = fold_build2 (LSHIFT_EXPR, utype, fldval,
3156 size_int (shift));
3157 retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval);
3158 fld_seen = true;
3159 }
3160 }
3161 }
3162 if (fld_seen)
3163 return fold_convert (TREE_TYPE (t), retval);
3164 gcc_unreachable ();
3165 return error_mark_node;
3166}
3167
3168/* Subroutine of cxx_eval_constant_expression.
3169 Evaluate a short-circuited logical expression T in the context
3170 of a given constexpr CALL. BAILOUT_VALUE is the value for
3171 early return. CONTINUE_VALUE is used here purely for
3172 sanity check purposes. */
3173
3174static tree
3e605b20 3175cxx_eval_logical_expression (const constexpr_ctx *ctx, tree t,
2d76680f 3176 tree bailout_value, tree continue_value,
92a596e8 3177 bool lval,
2d76680f
PC
3178 bool *non_constant_p, bool *overflow_p)
3179{
3180 tree r;
3e605b20 3181 tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
92a596e8 3182 lval,
5a804683 3183 non_constant_p, overflow_p);
2d76680f
PC
3184 VERIFY_CONSTANT (lhs);
3185 if (tree_int_cst_equal (lhs, bailout_value))
3186 return lhs;
3187 gcc_assert (tree_int_cst_equal (lhs, continue_value));
3e605b20 3188 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
92a596e8 3189 lval, non_constant_p,
5a804683 3190 overflow_p);
2d76680f
PC
3191 VERIFY_CONSTANT (r);
3192 return r;
3193}
3194
3195/* REF is a COMPONENT_REF designating a particular field. V is a vector of
3196 CONSTRUCTOR elements to initialize (part of) an object containing that
3197 field. Return a pointer to the constructor_elt corresponding to the
3198 initialization of the field. */
3199
3200static constructor_elt *
3201base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref)
3202{
3203 tree aggr = TREE_OPERAND (ref, 0);
3204 tree field = TREE_OPERAND (ref, 1);
3205 HOST_WIDE_INT i;
3206 constructor_elt *ce;
3207
3208 gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
3209
3210 if (TREE_CODE (aggr) == COMPONENT_REF)
3211 {
3212 constructor_elt *base_ce
3213 = base_field_constructor_elt (v, aggr);
3214 v = CONSTRUCTOR_ELTS (base_ce->value);
3215 }
3216
3217 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
3218 if (ce->index == field)
3219 return ce;
3220
3221 gcc_unreachable ();
3222 return NULL;
3223}
3224
3e605b20
JM
3225/* Some of the expressions fed to the constexpr mechanism are calls to
3226 constructors, which have type void. In that case, return the type being
3227 initialized by the constructor. */
3228
3229static tree
3230initialized_type (tree t)
3231{
3232 if (TYPE_P (t))
3233 return t;
5dab8b11 3234 tree type = TREE_TYPE (t);
4d0c18c6 3235 if (TREE_CODE (t) == CALL_EXPR)
3e605b20
JM
3236 {
3237 /* A constructor call has void type, so we need to look deeper. */
3238 tree fn = get_function_named_in_call (t);
3239 if (fn && TREE_CODE (fn) == FUNCTION_DECL
3240 && DECL_CXX_CONSTRUCTOR_P (fn))
3241 type = DECL_CONTEXT (fn);
3242 }
4d0c18c6
JM
3243 else if (TREE_CODE (t) == COMPOUND_EXPR)
3244 return initialized_type (TREE_OPERAND (t, 1));
5dab8b11
JM
3245 else if (TREE_CODE (t) == AGGR_INIT_EXPR)
3246 type = TREE_TYPE (AGGR_INIT_EXPR_SLOT (t));
3247 return cv_unqualified (type);
3e605b20
JM
3248}
3249
3250/* We're about to initialize element INDEX of an array or class from VALUE.
3251 Set up NEW_CTX appropriately by adjusting .object to refer to the
3252 subobject and creating a new CONSTRUCTOR if the element is itself
3253 a class or array. */
3254
3255static void
3256init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx,
3257 tree index, tree &value)
3258{
3259 new_ctx = *ctx;
3260
3261 if (index && TREE_CODE (index) != INTEGER_CST
3262 && TREE_CODE (index) != FIELD_DECL)
3263 /* This won't have an element in the new CONSTRUCTOR. */
3264 return;
3265
3266 tree type = initialized_type (value);
3267 if (!AGGREGATE_TYPE_P (type) && !VECTOR_TYPE_P (type))
3268 /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
3269 return;
3270
3271 /* The sub-aggregate initializer might contain a placeholder;
3272 update object to refer to the subobject and ctor to refer to
3273 the (newly created) sub-initializer. */
3274 if (ctx->object)
3275 new_ctx.object = build_ctor_subob_ref (index, type, ctx->object);
3276 tree elt = build_constructor (type, NULL);
e8c48716 3277 CONSTRUCTOR_NO_CLEARING (elt) = true;
3e605b20
JM
3278 new_ctx.ctor = elt;
3279
3280 if (TREE_CODE (value) == TARGET_EXPR)
3281 /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
3282 value = TARGET_EXPR_INITIAL (value);
3283}
3284
3285/* We're about to process an initializer for a class or array TYPE. Make
3286 sure that CTX is set up appropriately. */
3287
3288static void
3289verify_ctor_sanity (const constexpr_ctx *ctx, tree type)
3290{
3291 /* We don't bother building a ctor for an empty base subobject. */
3292 if (is_empty_class (type))
3293 return;
3294
3295 /* We're in the middle of an initializer that might involve placeholders;
3296 our caller should have created a CONSTRUCTOR for us to put the
3297 initializer into. We will either return that constructor or T. */
3298 gcc_assert (ctx->ctor);
3299 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3300 (type, TREE_TYPE (ctx->ctor)));
fe69277d
JM
3301 /* We used to check that ctx->ctor was empty, but that isn't the case when
3302 the object is zero-initialized before calling the constructor. */
3e605b20 3303 if (ctx->object)
176e79b5
JM
3304 {
3305 tree otype = TREE_TYPE (ctx->object);
3306 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, otype)
3307 /* Handle flexible array members. */
3308 || (TREE_CODE (otype) == ARRAY_TYPE
3309 && TYPE_DOMAIN (otype) == NULL_TREE
3310 && TREE_CODE (type) == ARRAY_TYPE
3311 && (same_type_ignoring_top_level_qualifiers_p
3312 (TREE_TYPE (type), TREE_TYPE (otype)))));
3313 }
3e605b20 3314 gcc_assert (!ctx->object || !DECL_P (ctx->object)
8e007055 3315 || *(ctx->global->values.get (ctx->object)) == ctx->ctor);
3e605b20
JM
3316}
3317
2d76680f
PC
3318/* Subroutine of cxx_eval_constant_expression.
3319 The expression tree T denotes a C-style array or a C-style
3320 aggregate. Reduce it to a constant expression. */
3321
3322static tree
3e605b20 3323cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t,
92a596e8 3324 bool lval,
2d76680f
PC
3325 bool *non_constant_p, bool *overflow_p)
3326{
3327 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
2d76680f
PC
3328 bool changed = false;
3329 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t));
8a29084d 3330 tree type = TREE_TYPE (t);
3e605b20 3331
8a29084d 3332 constexpr_ctx new_ctx;
d4619dc1 3333 if (TYPE_PTRMEMFUNC_P (type) || VECTOR_TYPE_P (type))
8a29084d 3334 {
d4619dc1
NS
3335 /* We don't really need the ctx->ctor business for a PMF or
3336 vector, but it's simpler to use the same code. */
8a29084d
JM
3337 new_ctx = *ctx;
3338 new_ctx.ctor = build_constructor (type, NULL);
3339 new_ctx.object = NULL_TREE;
3340 ctx = &new_ctx;
3341 };
3342 verify_ctor_sanity (ctx, type);
3e605b20
JM
3343 vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
3344 vec_alloc (*p, vec_safe_length (v));
3345
2d63bc39
JM
3346 unsigned i;
3347 tree index, value;
3348 bool constant_p = true;
3349 bool side_effects_p = false;
3e605b20 3350 FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value)
2d76680f 3351 {
bcb5f3c9 3352 tree orig_value = value;
3e605b20
JM
3353 init_subob_ctx (ctx, new_ctx, index, value);
3354 if (new_ctx.ctor != ctx->ctor)
3355 /* If we built a new CONSTRUCTOR, attach it now so that other
3356 initializers can refer to it. */
3357 CONSTRUCTOR_APPEND_ELT (*p, index, new_ctx.ctor);
3358 tree elt = cxx_eval_constant_expression (&new_ctx, value,
92a596e8 3359 lval,
5a804683 3360 non_constant_p, overflow_p);
2d76680f 3361 /* Don't VERIFY_CONSTANT here. */
2b3ab879 3362 if (ctx->quiet && *non_constant_p)
3e605b20 3363 break;
bcb5f3c9 3364 if (elt != orig_value)
2d76680f 3365 changed = true;
2d63bc39
JM
3366
3367 if (!TREE_CONSTANT (elt))
3368 constant_p = false;
3369 if (TREE_SIDE_EFFECTS (elt))
3370 side_effects_p = true;
3e605b20 3371 if (index && TREE_CODE (index) == COMPONENT_REF)
2d76680f
PC
3372 {
3373 /* This is an initialization of a vfield inside a base
3374 subaggregate that we already initialized; push this
3375 initialization into the previous initialization. */
3e605b20 3376 constructor_elt *inner = base_field_constructor_elt (*p, index);
2d76680f 3377 inner->value = elt;
3e605b20 3378 changed = true;
2d76680f 3379 }
3e605b20
JM
3380 else if (index
3381 && (TREE_CODE (index) == NOP_EXPR
3382 || TREE_CODE (index) == POINTER_PLUS_EXPR))
2d76680f
PC
3383 {
3384 /* This is an initializer for an empty base; now that we've
3385 checked that it's constant, we can ignore it. */
3e605b20
JM
3386 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index))));
3387 changed = true;
3388 }
2d76680f 3389 else
ac9ec198 3390 {
1bdbef09
MP
3391 if (new_ctx.ctor != ctx->ctor)
3392 {
3393 /* We appended this element above; update the value. */
3394 gcc_assert ((*p)->last().index == index);
3395 (*p)->last().value = elt;
3396 }
3397 else
3398 CONSTRUCTOR_APPEND_ELT (*p, index, elt);
3399 /* Adding or replacing an element might change the ctor's flags. */
ac9ec198
MP
3400 TREE_CONSTANT (ctx->ctor) = constant_p;
3401 TREE_SIDE_EFFECTS (ctx->ctor) = side_effects_p;
3402 }
2d76680f
PC
3403 }
3404 if (*non_constant_p || !changed)
3e605b20
JM
3405 return t;
3406 t = ctx->ctor;
3407 /* We're done building this CONSTRUCTOR, so now we can interpret an
3408 element without an explicit initializer as value-initialized. */
e8c48716 3409 CONSTRUCTOR_NO_CLEARING (t) = false;
2d63bc39
JM
3410 TREE_CONSTANT (t) = constant_p;
3411 TREE_SIDE_EFFECTS (t) = side_effects_p;
8a29084d 3412 if (VECTOR_TYPE_P (type))
2d76680f
PC
3413 t = fold (t);
3414 return t;
3415}
3416
3417/* Subroutine of cxx_eval_constant_expression.
3418 The expression tree T is a VEC_INIT_EXPR which denotes the desired
3419 initialization of a non-static data member of array type. Reduce it to a
3420 CONSTRUCTOR.
3421
3422 Note that apart from value-initialization (when VALUE_INIT is true),
3423 this is only intended to support value-initialization and the
3424 initializations done by defaulted constructors for classes with
3425 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
3426 will either be NULL_TREE for the default constructor, or a COMPONENT_REF
3427 for the copy/move constructor. */
3428
3429static tree
3e605b20 3430cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init,
92a596e8 3431 bool value_init, bool lval,
2d76680f
PC
3432 bool *non_constant_p, bool *overflow_p)
3433{
3434 tree elttype = TREE_TYPE (atype);
3e605b20
JM
3435 verify_ctor_sanity (ctx, atype);
3436 vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
2d76680f 3437 bool pre_init = false;
4784470a 3438 unsigned HOST_WIDE_INT i;
dc5ca6c8 3439 tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
2d76680f
PC
3440
3441 /* For the default constructor, build up a call to the default
3442 constructor of the element type. We only need to handle class types
3443 here, as for a constructor to be constexpr, all members must be
3444 initialized, which for a defaulted default constructor means they must
3445 be of a class type with a constexpr default constructor. */
3446 if (TREE_CODE (elttype) == ARRAY_TYPE)
3447 /* We only do this at the lowest level. */;
3448 else if (value_init)
3449 {
dc5ca6c8 3450 init = build_value_init (elttype, complain);
2d76680f
PC
3451 pre_init = true;
3452 }
3453 else if (!init)
3454 {
cd9cf97b 3455 releasing_vec argvec;
2d76680f
PC
3456 init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
3457 &argvec, elttype, LOOKUP_NORMAL,
dc5ca6c8 3458 complain);
5dab8b11 3459 init = build_aggr_init_expr (elttype, init);
2d76680f
PC
3460 pre_init = true;
3461 }
3462
74f8705e
MP
3463 tree nelts = get_array_or_vector_nelts (ctx, atype, non_constant_p,
3464 overflow_p);
3465 unsigned HOST_WIDE_INT max = tree_to_uhwi (nelts);
4784470a 3466 for (i = 0; i < max; ++i)
2d76680f
PC
3467 {
3468 tree idx = build_int_cst (size_type_node, i);
3469 tree eltinit;
928af3bf 3470 bool reuse = false;
3e605b20
JM
3471 constexpr_ctx new_ctx;
3472 init_subob_ctx (ctx, new_ctx, idx, pre_init ? init : elttype);
3473 if (new_ctx.ctor != ctx->ctor)
3474 CONSTRUCTOR_APPEND_ELT (*p, idx, new_ctx.ctor);
2d76680f
PC
3475 if (TREE_CODE (elttype) == ARRAY_TYPE)
3476 {
3477 /* A multidimensional array; recurse. */
3478 if (value_init || init == NULL_TREE)
928af3bf
JJ
3479 {
3480 eltinit = NULL_TREE;
3481 reuse = i == 0;
3482 }
2d76680f 3483 else
dc5ca6c8 3484 eltinit = cp_build_array_ref (input_location, init, idx, complain);
3e605b20 3485 eltinit = cxx_eval_vec_init_1 (&new_ctx, elttype, eltinit, value_init,
92a596e8 3486 lval,
2d76680f
PC
3487 non_constant_p, overflow_p);
3488 }
3489 else if (pre_init)
3490 {
3491 /* Initializing an element using value or default initialization
3492 we just pre-built above. */
3ee378fb
JM
3493 if (init == void_node)
3494 /* Trivial default-init, don't do anything to the CONSTRUCTOR. */
3495 return ctx->ctor;
928af3bf
JJ
3496 eltinit = cxx_eval_constant_expression (&new_ctx, init, lval,
3497 non_constant_p, overflow_p);
3498 reuse = i == 0;
2d76680f
PC
3499 }
3500 else
3501 {
3502 /* Copying an element. */
3503 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3504 (atype, TREE_TYPE (init)));
dc5ca6c8 3505 eltinit = cp_build_array_ref (input_location, init, idx, complain);
72b3e203 3506 if (!lvalue_p (init))
2d76680f 3507 eltinit = move (eltinit);
dc5ca6c8 3508 eltinit = force_rvalue (eltinit, complain);
c2236b9b
JJ
3509 eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval,
3510 non_constant_p, overflow_p);
2d76680f 3511 }
2b3ab879 3512 if (*non_constant_p && !ctx->quiet)
3e605b20
JM
3513 break;
3514 if (new_ctx.ctor != ctx->ctor)
3515 {
3516 /* We appended this element above; update the value. */
3517 gcc_assert ((*p)->last().index == idx);
3518 (*p)->last().value = eltinit;
3519 }
3520 else
3521 CONSTRUCTOR_APPEND_ELT (*p, idx, eltinit);
928af3bf 3522 /* Reuse the result of cxx_eval_constant_expression call
c2236b9b
JJ
3523 from the first iteration to all others if it is a constant
3524 initializer that doesn't require relocations. */
928af3bf
JJ
3525 if (reuse
3526 && max > 1
c2236b9b
JJ
3527 && (eltinit == NULL_TREE
3528 || (initializer_constant_valid_p (eltinit, TREE_TYPE (eltinit))
3529 == null_pointer_node)))
928af3bf
JJ
3530 {
3531 if (new_ctx.ctor != ctx->ctor)
3532 eltinit = new_ctx.ctor;
1e027956
RB
3533 tree range = build2 (RANGE_EXPR, size_type_node,
3534 build_int_cst (size_type_node, 1),
3535 build_int_cst (size_type_node, max - 1));
3536 CONSTRUCTOR_APPEND_ELT (*p, range, unshare_constructor (eltinit));
928af3bf
JJ
3537 break;
3538 }
1e027956
RB
3539 else if (i == 0)
3540 vec_safe_reserve (*p, max);
2d76680f
PC
3541 }
3542
3543 if (!*non_constant_p)
3544 {
3e605b20 3545 init = ctx->ctor;
e8c48716 3546 CONSTRUCTOR_NO_CLEARING (init) = false;
2d76680f 3547 }
2d76680f
PC
3548 return init;
3549}
3550
3551static tree
3e605b20 3552cxx_eval_vec_init (const constexpr_ctx *ctx, tree t,
92a596e8 3553 bool lval,
2d76680f
PC
3554 bool *non_constant_p, bool *overflow_p)
3555{
3556 tree atype = TREE_TYPE (t);
3557 tree init = VEC_INIT_EXPR_INIT (t);
3e605b20 3558 tree r = cxx_eval_vec_init_1 (ctx, atype, init,
2d76680f 3559 VEC_INIT_EXPR_VALUE_INIT (t),
92a596e8 3560 lval, non_constant_p, overflow_p);
2d76680f
PC
3561 if (*non_constant_p)
3562 return t;
3563 else
3564 return r;
3565}
3566
e4511ca2
JM
3567/* Like same_type_ignoring_top_level_qualifiers_p, but also handle the case
3568 where the desired type is an array of unknown bounds because the variable
3569 has had its bounds deduced since the wrapping expression was created. */
3570
3571static bool
3572same_type_ignoring_tlq_and_bounds_p (tree type1, tree type2)
3573{
3574 while (TREE_CODE (type1) == ARRAY_TYPE
3575 && TREE_CODE (type2) == ARRAY_TYPE
3576 && (!TYPE_DOMAIN (type1) || !TYPE_DOMAIN (type2)))
3577 {
3578 type1 = TREE_TYPE (type1);
3579 type2 = TREE_TYPE (type2);
3580 }
3581 return same_type_ignoring_top_level_qualifiers_p (type1, type2);
3582}
3583
0fe2ae29
JJ
3584/* Helper function for cxx_fold_indirect_ref_1, called recursively. */
3585
3586static tree
3587cxx_fold_indirect_ref_1 (location_t loc, tree type, tree op,
3588 unsigned HOST_WIDE_INT off, bool *empty_base)
3589{
3590 tree optype = TREE_TYPE (op);
3591 unsigned HOST_WIDE_INT const_nunits;
3592 if (off == 0)
3593 {
3594 if (similar_type_p (optype, type))
3595 return op;
3596 /* Also handle conversion to an empty base class, which
3597 is represented with a NOP_EXPR. */
3598 /* *(foo *)&complexfoo => __real__ complexfoo */
3599 else if (TREE_CODE (optype) == COMPLEX_TYPE
3600 && similar_type_p (type, TREE_TYPE (optype)))
3601 return build1_loc (loc, REALPART_EXPR, type, op);
3602 }
3603 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
3604 else if (TREE_CODE (optype) == COMPLEX_TYPE
3605 && similar_type_p (type, TREE_TYPE (optype))
3606 && tree_to_uhwi (TYPE_SIZE_UNIT (type)) == off)
3607 return build1_loc (loc, IMAGPART_EXPR, type, op);
3608 if (is_empty_class (type)
3609 && CLASS_TYPE_P (optype)
3610 && DERIVED_FROM_P (type, optype))
3611 {
3612 *empty_base = true;
3613 return op;
3614 }
3615 /* ((foo*)&vectorfoo)[x] => BIT_FIELD_REF<vectorfoo,...> */
3616 else if (VECTOR_TYPE_P (optype)
3617 && similar_type_p (type, TREE_TYPE (optype))
3618 && TYPE_VECTOR_SUBPARTS (optype).is_constant (&const_nunits))
3619 {
3620 unsigned HOST_WIDE_INT part_width = tree_to_uhwi (TYPE_SIZE_UNIT (type));
3621 unsigned HOST_WIDE_INT max_offset = part_width * const_nunits;
3622 if (off < max_offset && off % part_width == 0)
3623 {
3624 tree index = bitsize_int (off * BITS_PER_UNIT);
3625 return build3_loc (loc, BIT_FIELD_REF, type, op,
3626 TYPE_SIZE (type), index);
3627 }
3628 }
3629 /* ((foo *)&fooarray)[x] => fooarray[x] */
3630 else if (TREE_CODE (optype) == ARRAY_TYPE
3631 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (optype)))
3632 && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (optype))))
3633 {
3634 tree type_domain = TYPE_DOMAIN (optype);
3635 tree min_val = size_zero_node;
3636 if (type_domain && TYPE_MIN_VALUE (type_domain))
3637 min_val = TYPE_MIN_VALUE (type_domain);
3638 unsigned HOST_WIDE_INT el_sz
3639 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (optype)));
3640 unsigned HOST_WIDE_INT idx = off / el_sz;
3641 unsigned HOST_WIDE_INT rem = off % el_sz;
3642 if (tree_fits_uhwi_p (min_val))
3643 {
3644 tree index = size_int (idx + tree_to_uhwi (min_val));
3645 op = build4_loc (loc, ARRAY_REF, TREE_TYPE (optype), op, index,
3646 NULL_TREE, NULL_TREE);
3647 return cxx_fold_indirect_ref_1 (loc, type, op, rem,
3648 empty_base);
3649 }
3650 }
3651 /* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */
3652 else if (TREE_CODE (optype) == RECORD_TYPE)
3653 {
3654 for (tree field = TYPE_FIELDS (optype);
3655 field; field = DECL_CHAIN (field))
3656 if (TREE_CODE (field) == FIELD_DECL
3657 && TREE_TYPE (field) != error_mark_node
3658 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (field))))
3659 {
3660 tree pos = byte_position (field);
3661 if (!tree_fits_uhwi_p (pos))
3662 continue;
3663 unsigned HOST_WIDE_INT upos = tree_to_uhwi (pos);
3664 unsigned el_sz
3665 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field)));
3666 if (upos <= off && off < upos + el_sz)
3667 {
3668 tree cop = build3 (COMPONENT_REF, TREE_TYPE (field),
3669 op, field, NULL_TREE);
3670 if (tree ret = cxx_fold_indirect_ref_1 (loc, type, cop,
3671 off - upos,
3672 empty_base))
3673 return ret;
3674 }
3675 }
3676 }
3677
3678 return NULL_TREE;
3679}
3680
2d76680f
PC
3681/* A less strict version of fold_indirect_ref_1, which requires cv-quals to
3682 match. We want to be less strict for simple *& folding; if we have a
3683 non-const temporary that we access through a const pointer, that should
3684 work. We handle this here rather than change fold_indirect_ref_1
3685 because we're dealing with things like ADDR_EXPR of INTEGER_CST which
3686 don't really make sense outside of constant expression evaluation. Also
3687 we want to allow folding to COMPONENT_REF, which could cause trouble
0fe2ae29 3688 with TBAA in fold_indirect_ref_1. */
2d76680f
PC
3689
3690static tree
3691cxx_fold_indirect_ref (location_t loc, tree type, tree op0, bool *empty_base)
3692{
ebe4bf41
MP
3693 tree sub = op0;
3694 tree subtype;
3695 poly_uint64 const_op01;
2d76680f 3696
2d76680f
PC
3697 STRIP_NOPS (sub);
3698 subtype = TREE_TYPE (sub);
71a93b08 3699 if (!INDIRECT_TYPE_P (subtype))
2d76680f
PC
3700 return NULL_TREE;
3701
3702 if (TREE_CODE (sub) == ADDR_EXPR)
3703 {
3704 tree op = TREE_OPERAND (sub, 0);
3705 tree optype = TREE_TYPE (op);
3706
3707 /* *&CONST_DECL -> to the value of the const decl. */
3708 if (TREE_CODE (op) == CONST_DECL)
3709 return DECL_INITIAL (op);
3710 /* *&p => p; make sure to handle *&"str"[cst] here. */
1a120ec1 3711 if (similar_type_p (optype, type))
2d76680f
PC
3712 {
3713 tree fop = fold_read_from_constant_string (op);
3714 if (fop)
3715 return fop;
3716 else
3717 return op;
3718 }
0fe2ae29
JJ
3719 else
3720 return cxx_fold_indirect_ref_1 (loc, type, op, 0, empty_base);
2d76680f
PC
3721 }
3722 else if (TREE_CODE (sub) == POINTER_PLUS_EXPR
0fe2ae29 3723 && tree_fits_uhwi_p (TREE_OPERAND (sub, 1)))
2d76680f
PC
3724 {
3725 tree op00 = TREE_OPERAND (sub, 0);
3726 tree op01 = TREE_OPERAND (sub, 1);
3727
3728 STRIP_NOPS (op00);
3729 if (TREE_CODE (op00) == ADDR_EXPR)
0fe2ae29
JJ
3730 return cxx_fold_indirect_ref_1 (loc, type, TREE_OPERAND (op00, 0),
3731 tree_to_uhwi (op01), empty_base);
2d76680f
PC
3732 }
3733 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
3734 else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
1a120ec1 3735 && similar_type_p (type, TREE_TYPE (TREE_TYPE (subtype))))
2d76680f
PC
3736 {
3737 tree type_domain;
3738 tree min_val = size_zero_node;
ebe4bf41
MP
3739 tree newsub
3740 = cxx_fold_indirect_ref (loc, TREE_TYPE (subtype), sub, NULL);
2d76680f
PC
3741 if (newsub)
3742 sub = newsub;
3743 else
3744 sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub);
3745 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
3746 if (type_domain && TYPE_MIN_VALUE (type_domain))
3747 min_val = TYPE_MIN_VALUE (type_domain);
3748 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
3749 NULL_TREE);
3750 }
3751
3752 return NULL_TREE;
3753}
3754
3755static tree
3e605b20 3756cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t,
92a596e8 3757 bool lval,
2d76680f
PC
3758 bool *non_constant_p, bool *overflow_p)
3759{
3760 tree orig_op0 = TREE_OPERAND (t, 0);
2d76680f 3761 bool empty_base = false;
2d76680f 3762
cda0a029
JM
3763 /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second
3764 operand is an integer-zero. Otherwise reject the MEM_REF for now. */
3765
3766 if (TREE_CODE (t) == MEM_REF
3767 && (!TREE_OPERAND (t, 1) || !integer_zerop (TREE_OPERAND (t, 1))))
3768 {
3769 gcc_assert (ctx->quiet);
3770 *non_constant_p = true;
3771 return t;
3772 }
3773
255a48d6
JM
3774 /* First try to simplify it directly. */
3775 tree r = cxx_fold_indirect_ref (EXPR_LOCATION (t), TREE_TYPE (t), orig_op0,
3776 &empty_base);
3777 if (!r)
2d76680f 3778 {
255a48d6
JM
3779 /* If that didn't work, evaluate the operand first. */
3780 tree op0 = cxx_eval_constant_expression (ctx, orig_op0,
3781 /*lval*/false, non_constant_p,
3782 overflow_p);
3783 /* Don't VERIFY_CONSTANT here. */
3784 if (*non_constant_p)
3785 return t;
3786
8bada5cd
MS
3787 if (!lval && integer_zerop (op0))
3788 {
3789 if (!ctx->quiet)
3790 error ("dereferencing a null pointer");
3791 *non_constant_p = true;
3792 return t;
3793 }
3794
255a48d6
JM
3795 r = cxx_fold_indirect_ref (EXPR_LOCATION (t), TREE_TYPE (t), op0,
3796 &empty_base);
3797 if (r == NULL_TREE)
2d76680f
PC
3798 {
3799 /* We couldn't fold to a constant value. Make sure it's not
3800 something we should have been able to fold. */
255a48d6
JM
3801 tree sub = op0;
3802 STRIP_NOPS (sub);
3803 if (TREE_CODE (sub) == ADDR_EXPR)
3804 {
1a120ec1 3805 gcc_assert (!similar_type_p
255a48d6
JM
3806 (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t)));
3807 /* DR 1188 says we don't have to deal with this. */
3808 if (!ctx->quiet)
1a120ec1
JM
3809 error_at (cp_expr_loc_or_input_loc (t),
3810 "accessing value of %qE through a %qT glvalue in a "
3811 "constant expression", build_fold_indirect_ref (sub),
3812 TREE_TYPE (t));
255a48d6
JM
3813 *non_constant_p = true;
3814 return t;
3815 }
3816
3817 if (lval && op0 != orig_op0)
3818 return build1 (INDIRECT_REF, TREE_TYPE (t), op0);
3819 if (!lval)
3820 VERIFY_CONSTANT (t);
2d76680f
PC
3821 return t;
3822 }
3823 }
3824
255a48d6
JM
3825 r = cxx_eval_constant_expression (ctx, r,
3826 lval, non_constant_p, overflow_p);
3827 if (*non_constant_p)
3828 return t;
3829
125db6a1 3830 /* If we're pulling out the value of an empty base, just return an empty
2d76680f 3831 CONSTRUCTOR. */
92a596e8 3832 if (empty_base && !lval)
2d76680f 3833 {
2d76680f
PC
3834 r = build_constructor (TREE_TYPE (t), NULL);
3835 TREE_CONSTANT (r) = true;
3836 }
3837
2d76680f
PC
3838 return r;
3839}
3840
3841/* Complain about R, a VAR_DECL, not being usable in a constant expression.
3842 Shared between potential_constant_expression and
3843 cxx_eval_constant_expression. */
3844
3845static void
6821245b 3846non_const_var_error (location_t loc, tree r)
2d76680f 3847{
8e007055 3848 auto_diagnostic_group d;
2d76680f 3849 tree type = TREE_TYPE (r);
8e007055
JJ
3850 if (DECL_NAME (r) == heap_uninit_identifier
3851 || DECL_NAME (r) == heap_identifier)
3852 {
6821245b
JM
3853 error_at (loc, "the content of uninitialized storage is not usable "
3854 "in a constant expression");
8e007055
JJ
3855 inform (DECL_SOURCE_LOCATION (r), "allocated here");
3856 return;
3857 }
3858 if (DECL_NAME (r) == heap_deleted_identifier)
3859 {
6821245b
JM
3860 error_at (loc, "use of allocated storage after deallocation in a "
3861 "constant expression");
8e007055
JJ
3862 inform (DECL_SOURCE_LOCATION (r), "allocated here");
3863 return;
3864 }
6821245b
JM
3865 error_at (loc, "the value of %qD is not usable in a constant "
3866 "expression", r);
2d76680f
PC
3867 /* Avoid error cascade. */
3868 if (DECL_INITIAL (r) == error_mark_node)
3869 return;
3870 if (DECL_DECLARED_CONSTEXPR_P (r))
3871 inform (DECL_SOURCE_LOCATION (r),
3872 "%qD used in its own initializer", r);
3873 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3874 {
3875 if (!CP_TYPE_CONST_P (type))
3876 inform (DECL_SOURCE_LOCATION (r),
3877 "%q#D is not const", r);
3878 else if (CP_TYPE_VOLATILE_P (type))
3879 inform (DECL_SOURCE_LOCATION (r),
3880 "%q#D is volatile", r);
3881 else if (!DECL_INITIAL (r)
a3e2b438
PP
3882 || !TREE_CONSTANT (DECL_INITIAL (r))
3883 || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r))
2d76680f
PC
3884 inform (DECL_SOURCE_LOCATION (r),
3885 "%qD was not initialized with a constant "
3886 "expression", r);
3887 else
3888 gcc_unreachable ();
3889 }
9f613f06 3890 else if (TYPE_REF_P (type))
fd338b13
JM
3891 inform (DECL_SOURCE_LOCATION (r),
3892 "%qD was not initialized with a constant "
3893 "expression", r);
2d76680f
PC
3894 else
3895 {
3896 if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r))
3897 inform (DECL_SOURCE_LOCATION (r),
3898 "%qD was not declared %<constexpr%>", r);
3899 else
3900 inform (DECL_SOURCE_LOCATION (r),
3901 "%qD does not have integral or enumeration type",
3902 r);
3903 }
3904}
3905
3906/* Subroutine of cxx_eval_constant_expression.
3907 Like cxx_eval_unary_expression, except for trinary expressions. */
3908
3909static tree
3e605b20 3910cxx_eval_trinary_expression (const constexpr_ctx *ctx, tree t,
92a596e8 3911 bool lval,
2d76680f
PC
3912 bool *non_constant_p, bool *overflow_p)
3913{
3914 int i;
3915 tree args[3];
3916 tree val;
3917
3918 for (i = 0; i < 3; i++)
3919 {
3e605b20 3920 args[i] = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, i),
92a596e8 3921 lval,
5a804683 3922 non_constant_p, overflow_p);
2d76680f
PC
3923 VERIFY_CONSTANT (args[i]);
3924 }
3925
3926 val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t),
3927 args[0], args[1], args[2]);
3928 if (val == NULL_TREE)
3929 return t;
3930 VERIFY_CONSTANT (val);
3931 return val;
3932}
3933
98e5a19a
JM
3934/* True if T was declared in a function declared to be constexpr, and
3935 therefore potentially constant in C++14. */
3936
2d76680f
PC
3937bool
3938var_in_constexpr_fn (tree t)
3939{
3940 tree ctx = DECL_CONTEXT (t);
98e5a19a 3941 return (ctx && TREE_CODE (ctx) == FUNCTION_DECL
2d76680f
PC
3942 && DECL_DECLARED_CONSTEXPR_P (ctx));
3943}
3944
98e5a19a
JM
3945/* True if T was declared in a function that might be constexpr: either a
3946 function that was declared constexpr, or a C++17 lambda op(). */
3947
3948bool
3949var_in_maybe_constexpr_fn (tree t)
3950{
7b936140 3951 if (cxx_dialect >= cxx17
98e5a19a
JM
3952 && DECL_FUNCTION_SCOPE_P (t)
3953 && LAMBDA_FUNCTION_P (DECL_CONTEXT (t)))
3954 return true;
3955 return var_in_constexpr_fn (t);
3956}
3957
68d01920
JM
3958/* We're assigning INIT to TARGET. In do_build_copy_constructor and
3959 build_over_call we implement trivial copy of a class with tail padding using
3960 assignment of character arrays, which is valid in normal code, but not in
3961 constexpr evaluation. We don't need to worry about clobbering tail padding
3962 in constexpr evaluation, so strip the type punning. */
3963
3964static void
3965maybe_simplify_trivial_copy (tree &target, tree &init)
3966{
3967 if (TREE_CODE (target) == MEM_REF
3968 && TREE_CODE (init) == MEM_REF
3969 && TREE_TYPE (target) == TREE_TYPE (init)
3970 && TREE_CODE (TREE_TYPE (target)) == ARRAY_TYPE
3971 && TREE_TYPE (TREE_TYPE (target)) == unsigned_char_type_node)
3972 {
3973 target = build_fold_indirect_ref (TREE_OPERAND (target, 0));
3974 init = build_fold_indirect_ref (TREE_OPERAND (init, 0));
3975 }
3976}
3977
04e1749c
MP
3978/* Return true if we are modifying something that is const during constant
3979 expression evaluation. CODE is the code of the statement, OBJ is the
3980 object in question, MUTABLE_P is true if one of the subobjects were
3981 declared mutable. */
3982
3983static bool
3984modifying_const_object_p (tree_code code, tree obj, bool mutable_p)
3985{
3986 /* If this is initialization, there's no problem. */
3987 if (code != MODIFY_EXPR)
3988 return false;
3989
3990 /* [basic.type.qualifier] "A const object is an object of type
3991 const T or a non-mutable subobject of a const object." */
3992 if (mutable_p)
3993 return false;
3994
3995 return (TREE_READONLY (obj) || CP_TYPE_CONST_P (TREE_TYPE (obj)));
3996}
3997
60813a46
JM
3998/* Evaluate an INIT_EXPR or MODIFY_EXPR. */
3999
4000static tree
4001cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
92a596e8 4002 bool lval,
60813a46
JM
4003 bool *non_constant_p, bool *overflow_p)
4004{
4005 constexpr_ctx new_ctx = *ctx;
4006
58cc255c
JM
4007 tree init = TREE_OPERAND (t, 1);
4008 if (TREE_CLOBBER_P (init))
4009 /* Just ignore clobbers. */
4010 return void_node;
4011
60813a46
JM
4012 /* First we figure out where we're storing to. */
4013 tree target = TREE_OPERAND (t, 0);
68d01920
JM
4014
4015 maybe_simplify_trivial_copy (target, init);
4016
3f8e2835 4017 tree type = TREE_TYPE (target);
e8b3c1bc
JM
4018 bool preeval = SCALAR_TYPE_P (type) || TREE_CODE (t) == MODIFY_EXPR;
4019 if (preeval)
4020 {
4021 /* Evaluate the value to be stored without knowing what object it will be
4022 stored in, so that any side-effects happen first. */
4023 if (!SCALAR_TYPE_P (type))
4024 new_ctx.ctor = new_ctx.object = NULL_TREE;
4025 init = cxx_eval_constant_expression (&new_ctx, init, false,
4026 non_constant_p, overflow_p);
4027 if (*non_constant_p)
4028 return t;
4029 }
60813a46 4030
d0aa42d2
JM
4031 bool evaluated = false;
4032 if (lval)
3f8e2835 4033 {
d0aa42d2
JM
4034 /* If we want to return a reference to the target, we need to evaluate it
4035 as a whole; otherwise, only evaluate the innermost piece to avoid
4036 building up unnecessary *_REFs. */
4037 target = cxx_eval_constant_expression (ctx, target, true,
4038 non_constant_p, overflow_p);
4039 evaluated = true;
4040 if (*non_constant_p)
4041 return t;
3f8e2835
JM
4042 }
4043
043666e0 4044 /* Find the underlying variable. */
cd9cf97b 4045 releasing_vec refs;
60813a46 4046 tree object = NULL_TREE;
04e1749c
MP
4047 /* If we're modifying a const object, save it. */
4048 tree const_object_being_modified = NULL_TREE;
4049 bool mutable_p = false;
60813a46
JM
4050 for (tree probe = target; object == NULL_TREE; )
4051 {
4052 switch (TREE_CODE (probe))
4053 {
4054 case BIT_FIELD_REF:
4055 case COMPONENT_REF:
4056 case ARRAY_REF:
043666e0
JM
4057 {
4058 tree ob = TREE_OPERAND (probe, 0);
4059 tree elt = TREE_OPERAND (probe, 1);
7d349dd8 4060 if (TREE_CODE (elt) == FIELD_DECL && DECL_MUTABLE_P (elt))
04e1749c 4061 mutable_p = true;
043666e0
JM
4062 if (TREE_CODE (probe) == ARRAY_REF)
4063 {
4064 elt = eval_and_check_array_index (ctx, probe, false,
4065 non_constant_p, overflow_p);
4066 if (*non_constant_p)
4067 return t;
4068 }
0f184800
MP
4069 /* We don't check modifying_const_object_p for ARRAY_REFs. Given
4070 "int a[10]", an ARRAY_REF "a[2]" can be "const int", even though
4071 the array isn't const. Instead, check "a" in the next iteration;
4072 that will detect modifying "const int a[10]". */
4073 else if (evaluated
4074 && modifying_const_object_p (TREE_CODE (t), probe,
4075 mutable_p)
4076 && const_object_being_modified == NULL_TREE)
4077 const_object_being_modified = probe;
043666e0
JM
4078 vec_safe_push (refs, elt);
4079 vec_safe_push (refs, TREE_TYPE (probe));
4080 probe = ob;
4081 }
60813a46
JM
4082 break;
4083
4084 default:
d0aa42d2
JM
4085 if (evaluated)
4086 object = probe;
4087 else
4088 {
4089 probe = cxx_eval_constant_expression (ctx, probe, true,
4090 non_constant_p, overflow_p);
4091 evaluated = true;
4092 if (*non_constant_p)
4093 return t;
4094 }
4095 break;
60813a46
JM
4096 }
4097 }
4098
04e1749c
MP
4099 if (modifying_const_object_p (TREE_CODE (t), object, mutable_p)
4100 && const_object_being_modified == NULL_TREE)
4101 const_object_being_modified = object;
4102
60813a46
JM
4103 /* And then find/build up our initializer for the path to the subobject
4104 we're initializing. */
a1408b7e 4105 tree *valp;
1e163090
JM
4106 if (object == ctx->object && VAR_P (object)
4107 && DECL_NAME (object) && ctx->call == NULL)
4108 /* The variable we're building up an aggregate initializer for is outside
4109 the constant-expression, so don't evaluate the store. We check
4110 DECL_NAME to handle TARGET_EXPR temporaries, which are fair game. */
4111 valp = NULL;
4112 else if (DECL_P (object))
8e007055 4113 valp = ctx->global->values.get (object);
a1408b7e
JM
4114 else
4115 valp = NULL;
60813a46
JM
4116 if (!valp)
4117 {
4118 /* A constant-expression cannot modify objects from outside the
4119 constant-expression. */
2b3ab879 4120 if (!ctx->quiet)
64d6d399 4121 error ("modification of %qE is not a constant expression", object);
60813a46
JM
4122 *non_constant_p = true;
4123 return t;
4124 }
3f8e2835 4125 type = TREE_TYPE (object);
c75ce530 4126 bool no_zero_init = true;
2d63bc39 4127
cd9cf97b 4128 releasing_vec ctors;
276a52d5 4129 while (!refs->is_empty ())
60813a46
JM
4130 {
4131 if (*valp == NULL_TREE)
4132 {
4133 *valp = build_constructor (type, NULL);
e8c48716 4134 CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
60813a46 4135 }
d6b46fca
NS
4136 else if (TREE_CODE (*valp) == STRING_CST)
4137 {
4138 /* An array was initialized with a string constant, and now
4139 we're writing into one of its elements. Explode the
4140 single initialization into a set of element
4141 initializations. */
4142 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
4143
4144 tree string = *valp;
4145 tree elt_type = TREE_TYPE (type);
4146 unsigned chars_per_elt = (TYPE_PRECISION (elt_type)
4147 / TYPE_PRECISION (char_type_node));
4148 unsigned num_elts = TREE_STRING_LENGTH (string) / chars_per_elt;
4149 tree ary_ctor = build_constructor (type, NULL);
4150
4151 vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor), num_elts);
4152 for (unsigned ix = 0; ix != num_elts; ix++)
4153 {
4154 constructor_elt elt =
4155 {
4156 build_int_cst (size_type_node, ix),
4157 extract_string_elt (string, chars_per_elt, ix)
4158 };
4159 CONSTRUCTOR_ELTS (ary_ctor)->quick_push (elt);
4160 }
4161
4162 *valp = ary_ctor;
4163 }
4164
c75ce530
JM
4165 /* If the value of object is already zero-initialized, any new ctors for
4166 subobjects will also be zero-initialized. */
e8c48716 4167 no_zero_init = CONSTRUCTOR_NO_CLEARING (*valp);
60813a46 4168
2d63bc39
JM
4169 vec_safe_push (ctors, *valp);
4170
ceaaf873 4171 enum tree_code code = TREE_CODE (type);
60813a46 4172 type = refs->pop();
ceaaf873 4173 tree index = refs->pop();
60813a46 4174
60813a46 4175 constructor_elt *cep = NULL;
ceaaf873
JM
4176 if (code == ARRAY_TYPE)
4177 {
4178 HOST_WIDE_INT i
4179 = find_array_ctor_elt (*valp, index, /*insert*/true);
4180 gcc_assert (i >= 0);
4181 cep = CONSTRUCTOR_ELT (*valp, i);
4182 gcc_assert (TREE_CODE (cep->index) != RANGE_EXPR);
4183 }
4184 else
4185 {
4186 gcc_assert (TREE_CODE (index) == FIELD_DECL);
88504f34
NS
4187
4188 /* We must keep the CONSTRUCTOR's ELTS in FIELD order.
4189 Usually we meet initializers in that order, but it is
4190 possible for base types to be placed not in program
4191 order. */
4192 tree fields = TYPE_FIELDS (DECL_CONTEXT (index));
4193 unsigned HOST_WIDE_INT idx;
4194
5c97093b
JM
4195 if (code == UNION_TYPE && CONSTRUCTOR_NELTS (*valp)
4196 && CONSTRUCTOR_ELT (*valp, 0)->index != index)
ed4ec9ce
JJ
4197 {
4198 if (cxx_dialect < cxx2a)
4199 {
4200 if (!ctx->quiet)
f9d0ca40 4201 error_at (cp_expr_loc_or_input_loc (t),
ed4ec9ce
JJ
4202 "change of the active member of a union "
4203 "from %qD to %qD",
4204 CONSTRUCTOR_ELT (*valp, 0)->index,
4205 index);
4206 *non_constant_p = true;
4207 }
4208 /* Changing active member. */
4209 vec_safe_truncate (CONSTRUCTOR_ELTS (*valp), 0);
c7f7c313 4210 no_zero_init = true;
ed4ec9ce 4211 }
5c97093b 4212
88504f34 4213 for (idx = 0;
ceaaf873 4214 vec_safe_iterate (CONSTRUCTOR_ELTS (*valp), idx, &cep);
88504f34 4215 idx++, fields = DECL_CHAIN (fields))
ceaaf873 4216 {
88504f34
NS
4217 if (index == cep->index)
4218 goto found;
4219
4220 /* The field we're initializing must be on the field
4221 list. Look to see if it is present before the
4222 field the current ELT initializes. */
4223 for (; fields != cep->index; fields = DECL_CHAIN (fields))
4224 if (index == fields)
4225 goto insert;
ceaaf873 4226 }
88504f34
NS
4227
4228 /* We fell off the end of the CONSTRUCTOR, so insert a new
4229 entry at the end. */
4230 insert:
4231 {
4232 constructor_elt ce = { index, NULL_TREE };
4233
4234 vec_safe_insert (CONSTRUCTOR_ELTS (*valp), idx, ce);
4235 cep = CONSTRUCTOR_ELT (*valp, idx);
4236 }
4237 found:;
ceaaf873 4238 }
60813a46
JM
4239 valp = &cep->value;
4240 }
60813a46 4241
04e1749c
MP
4242 /* Detect modifying a constant object in constexpr evaluation.
4243 We have found a const object that is being modified. Figure out
4244 if we need to issue an error. Consider
4245
4246 struct A {
4247 int n;
4248 constexpr A() : n(1) { n = 2; } // #1
4249 };
4250 struct B {
4251 const A a;
4252 constexpr B() { a.n = 3; } // #2
4253 };
4254 constexpr B b{};
4255
4256 #1 is OK, since we're modifying an object under construction, but
4257 #2 is wrong, since "a" is const and has been fully constructed.
4258 To track it, we use the TREE_READONLY bit in the object's CONSTRUCTOR
4259 which means that the object is read-only. For the example above, the
4260 *ctors stack at the point of #2 will look like:
4261
4262 ctors[0] = {.a={.n=2}} TREE_READONLY = 0
4263 ctors[1] = {.n=2} TREE_READONLY = 1
4264
4265 and we're modifying "b.a", so we search the stack and see if the
4266 constructor for "b.a" has already run. */
4267 if (const_object_being_modified)
4268 {
4269 bool fail = false;
276a52d5
JJ
4270 tree const_objtype
4271 = strip_array_types (TREE_TYPE (const_object_being_modified));
4272 if (!CLASS_TYPE_P (const_objtype))
04e1749c
MP
4273 fail = true;
4274 else
4275 {
4276 /* [class.ctor]p5 "A constructor can be invoked for a const,
4277 volatile, or const volatile object. const and volatile
4278 semantics are not applied on an object under construction.
4279 They come into effect when the constructor for the most
4280 derived object ends." */
4281 tree elt;
4282 unsigned int i;
4283 FOR_EACH_VEC_ELT (*ctors, i, elt)
4284 if (same_type_ignoring_top_level_qualifiers_p
4285 (TREE_TYPE (const_object_being_modified), TREE_TYPE (elt)))
4286 {
4287 fail = TREE_READONLY (elt);
4288 break;
4289 }
4290 }
4291 if (fail)
4292 {
4293 if (!ctx->quiet)
4294 modifying_const_object_error (t, const_object_being_modified);
4295 *non_constant_p = true;
4296 return t;
4297 }
4298 }
4299
e8b3c1bc 4300 if (!preeval)
60813a46
JM
4301 {
4302 /* Create a new CONSTRUCTOR in case evaluation of the initializer
4303 wants to modify it. */
acb2970c 4304 if (*valp == NULL_TREE)
73a84269 4305 {
664beaf2 4306 *valp = build_constructor (type, NULL);
e8c48716 4307 CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
73a84269 4308 }
664beaf2 4309 new_ctx.ctor = *valp;
60813a46 4310 new_ctx.object = target;
e8b3c1bc
JM
4311 init = cxx_eval_constant_expression (&new_ctx, init, false,
4312 non_constant_p, overflow_p);
d0aa42d2 4313 if (ctors->is_empty())
e8b3c1bc 4314 /* The hash table might have moved since the get earlier. */
8e007055 4315 valp = ctx->global->values.get (object);
60813a46
JM
4316 }
4317
7574c916 4318 /* Don't share a CONSTRUCTOR that might be changed later. */
0146e25f 4319 init = unshare_constructor (init);
d96e8407 4320
e8b3c1bc
JM
4321 if (*valp && TREE_CODE (*valp) == CONSTRUCTOR
4322 && TREE_CODE (init) == CONSTRUCTOR)
acb2970c 4323 {
d96e8407
JM
4324 /* An outer ctx->ctor might be pointing to *valp, so replace
4325 its contents. */
d0aa42d2
JM
4326 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init),
4327 TREE_TYPE (*valp)))
4328 {
4329 /* For initialization of an empty base, the original target will be
4330 *(base*)this, evaluation of which resolves to the object
4331 argument, which has the derived type rather than the base type. In
4332 this situation, just evaluate the initializer and return, since
4333 there's no actual data to store. */
4334 gcc_assert (is_empty_class (TREE_TYPE (init)) && !lval);
4335 return init;
4336 }
d96e8407
JM
4337 CONSTRUCTOR_ELTS (*valp) = CONSTRUCTOR_ELTS (init);
4338 TREE_CONSTANT (*valp) = TREE_CONSTANT (init);
4339 TREE_SIDE_EFFECTS (*valp) = TREE_SIDE_EFFECTS (init);
e8c48716
JM
4340 CONSTRUCTOR_NO_CLEARING (*valp)
4341 = CONSTRUCTOR_NO_CLEARING (init);
acb2970c 4342 }
60813a46 4343 else
d96e8407 4344 *valp = init;
2d63bc39 4345
d96e8407
JM
4346 /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing
4347 CONSTRUCTORs, if any. */
4348 tree elt;
4349 unsigned i;
4350 bool c = TREE_CONSTANT (init);
4351 bool s = TREE_SIDE_EFFECTS (init);
4352 if (!c || s)
cd9cf97b 4353 FOR_EACH_VEC_ELT (*ctors, i, elt)
d96e8407
JM
4354 {
4355 if (!c)
4356 TREE_CONSTANT (elt) = false;
4357 if (s)
4358 TREE_SIDE_EFFECTS (elt) = true;
4359 }
60813a46
JM
4360
4361 if (*non_constant_p)
4362 return t;
92a596e8 4363 else if (lval)
60813a46
JM
4364 return target;
4365 else
3bdf0a9b 4366 return init;
60813a46
JM
4367}
4368
4369/* Evaluate a ++ or -- expression. */
4370
4371static tree
4372cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t,
92a596e8 4373 bool lval,
60813a46
JM
4374 bool *non_constant_p, bool *overflow_p)
4375{
4376 enum tree_code code = TREE_CODE (t);
4377 tree type = TREE_TYPE (t);
4378 tree op = TREE_OPERAND (t, 0);
4379 tree offset = TREE_OPERAND (t, 1);
4380 gcc_assert (TREE_CONSTANT (offset));
4381
d85569f6
MP
4382 /* OFFSET is constant, but perhaps not constant enough. We need to
4383 e.g. bash FLOAT_EXPRs to REAL_CSTs. */
4384 offset = fold_simple (offset);
4385
60813a46 4386 /* The operand as an lvalue. */
2b3ab879 4387 op = cxx_eval_constant_expression (ctx, op, true,
5a804683 4388 non_constant_p, overflow_p);
60813a46
JM
4389
4390 /* The operand as an rvalue. */
84dd815f
JM
4391 tree val
4392 = cxx_eval_constant_expression (ctx, op, false,
4393 non_constant_p, overflow_p);
caee690e
JM
4394 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
4395 a local array in a constexpr function. */
71a93b08 4396 bool ptr = INDIRECT_TYPE_P (TREE_TYPE (val));
caee690e
JM
4397 if (!ptr)
4398 VERIFY_CONSTANT (val);
60813a46
JM
4399
4400 /* The modified value. */
4401 bool inc = (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR);
ac2f8d26 4402 tree mod;
71a93b08 4403 if (INDIRECT_TYPE_P (type))
ac2f8d26
JM
4404 {
4405 /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
4406 offset = convert_to_ptrofftype (offset);
4407 if (!inc)
4408 offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset);
4409 mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset);
4410 }
4411 else
4412 mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset);
caee690e
JM
4413 if (!ptr)
4414 VERIFY_CONSTANT (mod);
60813a46
JM
4415
4416 /* Storing the modified value. */
04e1749c
MP
4417 tree store = build2_loc (cp_expr_loc_or_loc (t, input_location),
4418 MODIFY_EXPR, type, op, mod);
2b3ab879 4419 cxx_eval_constant_expression (ctx, store,
5a804683 4420 true, non_constant_p, overflow_p);
ecdcd560 4421 ggc_free (store);
60813a46
JM
4422
4423 /* And the value of the expression. */
4424 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
4425 {
4426 /* Prefix ops are lvalues. */
92a596e8 4427 if (lval)
60813a46
JM
4428 return op;
4429 else
4430 /* But we optimize when the caller wants an rvalue. */
4431 return mod;
4432 }
4433 else
4434 /* Postfix ops are rvalues. */
4435 return val;
4436}
4437
56632b27
JM
4438/* Predicates for the meaning of *jump_target. */
4439
4440static bool
4441returns (tree *jump_target)
4442{
4443 return *jump_target
d49318d9
PC
4444 && (TREE_CODE (*jump_target) == RETURN_EXPR
4445 || (TREE_CODE (*jump_target) == LABEL_DECL
4446 && LABEL_DECL_CDTOR (*jump_target)));
56632b27
JM
4447}
4448
4449static bool
4450breaks (tree *jump_target)
4451{
4452 return *jump_target
06ec22b7
JM
4453 && ((TREE_CODE (*jump_target) == LABEL_DECL
4454 && LABEL_DECL_BREAK (*jump_target))
3075affd 4455 || TREE_CODE (*jump_target) == BREAK_STMT
06ec22b7 4456 || TREE_CODE (*jump_target) == EXIT_EXPR);
56632b27
JM
4457}
4458
4459static bool
4460continues (tree *jump_target)
4461{
4462 return *jump_target
3075affd
JM
4463 && ((TREE_CODE (*jump_target) == LABEL_DECL
4464 && LABEL_DECL_CONTINUE (*jump_target))
4465 || TREE_CODE (*jump_target) == CONTINUE_STMT);
4466
56632b27
JM
4467}
4468
4469static bool
4470switches (tree *jump_target)
4471{
4472 return *jump_target
4473 && TREE_CODE (*jump_target) == INTEGER_CST;
4474}
4475
4476/* Subroutine of cxx_eval_statement_list. Determine whether the statement
4b390698
JJ
4477 STMT matches *jump_target. If we're looking for a case label and we see
4478 the default label, note it in ctx->css_state. */
56632b27
JM
4479
4480static bool
4b390698 4481label_matches (const constexpr_ctx *ctx, tree *jump_target, tree stmt)
56632b27 4482{
56632b27
JM
4483 switch (TREE_CODE (*jump_target))
4484 {
4485 case LABEL_DECL:
4486 if (TREE_CODE (stmt) == LABEL_EXPR
4487 && LABEL_EXPR_LABEL (stmt) == *jump_target)
4488 return true;
4489 break;
4490
4491 case INTEGER_CST:
4492 if (TREE_CODE (stmt) == CASE_LABEL_EXPR)
4493 {
4b390698 4494 gcc_assert (ctx->css_state != NULL);
56632b27 4495 if (!CASE_LOW (stmt))
4b390698
JJ
4496 {
4497 /* default: should appear just once in a SWITCH_EXPR
4498 body (excluding nested SWITCH_EXPR). */
4499 gcc_assert (*ctx->css_state != css_default_seen);
4500 /* When evaluating SWITCH_EXPR body for the second time,
4501 return true for the default: label. */
4502 if (*ctx->css_state == css_default_processing)
4503 return true;
4504 *ctx->css_state = css_default_seen;
4505 }
385ed708
JJ
4506 else if (CASE_HIGH (stmt))
4507 {
4508 if (tree_int_cst_le (CASE_LOW (stmt), *jump_target)
4509 && tree_int_cst_le (*jump_target, CASE_HIGH (stmt)))
4510 return true;
4511 }
56632b27
JM
4512 else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt)))
4513 return true;
4514 }
4515 break;
4516
43574e4f
JJ
4517 case BREAK_STMT:
4518 case CONTINUE_STMT:
4519 /* These two are handled directly in cxx_eval_loop_expr by testing
4520 breaks (jump_target) or continues (jump_target). */
4521 break;
4522
56632b27
JM
4523 default:
4524 gcc_unreachable ();
4525 }
4526 return false;
4527}
4528
4529/* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
4530 semantics, for switch, break, continue, and return. */
4531
4532static tree
4533cxx_eval_statement_list (const constexpr_ctx *ctx, tree t,
56632b27
JM
4534 bool *non_constant_p, bool *overflow_p,
4535 tree *jump_target)
4536{
4537 tree_stmt_iterator i;
58611fb6 4538 tree local_target;
345edb37
JJ
4539 /* In a statement-expression we want to return the last value.
4540 For empty statement expression return void_node. */
4541 tree r = void_node;
58611fb6
JM
4542 if (!jump_target)
4543 {
4544 local_target = NULL_TREE;
4545 jump_target = &local_target;
4546 }
56632b27
JM
4547 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
4548 {
56632b27 4549 tree stmt = tsi_stmt (i);
0250ba58
MP
4550 /* We've found a continue, so skip everything until we reach
4551 the label its jumping to. */
4552 if (continues (jump_target))
4553 {
4554 if (label_matches (ctx, jump_target, stmt))
4555 /* Found it. */
4556 *jump_target = NULL_TREE;
4557 else
4558 continue;
4559 }
6ef72c36
JJ
4560 if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT)
4561 continue;
58611fb6
JM
4562 r = cxx_eval_constant_expression (ctx, stmt, false,
4563 non_constant_p, overflow_p,
4564 jump_target);
56632b27
JM
4565 if (*non_constant_p)
4566 break;
4567 if (returns (jump_target) || breaks (jump_target))
4568 break;
4569 }
ce36ba09
JM
4570 if (*jump_target && jump_target == &local_target)
4571 {
4572 /* We aren't communicating the jump to our caller, so give up. We don't
4573 need to support evaluation of jumps out of statement-exprs. */
4574 if (!ctx->quiet)
f9d0ca40 4575 error_at (cp_expr_loc_or_input_loc (r),
ce36ba09
JM
4576 "statement is not a constant expression");
4577 *non_constant_p = true;
4578 }
58611fb6 4579 return r;
56632b27
JM
4580}
4581
4582/* Evaluate a LOOP_EXPR for side-effects. Handles break and return
4583 semantics; continue semantics are covered by cxx_eval_statement_list. */
4584
4585static tree
4586cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t,
56632b27
JM
4587 bool *non_constant_p, bool *overflow_p,
4588 tree *jump_target)
4589{
39dce2b7 4590 constexpr_ctx new_ctx = *ctx;
8e007055
JJ
4591 tree local_target;
4592 if (!jump_target)
4593 {
4594 local_target = NULL_TREE;
4595 jump_target = &local_target;
4596 }
39dce2b7 4597
43574e4f 4598 tree body, cond = NULL_TREE, expr = NULL_TREE;
5ec2cd9f 4599 int count = 0;
43574e4f
JJ
4600 switch (TREE_CODE (t))
4601 {
4602 case LOOP_EXPR:
4603 body = LOOP_EXPR_BODY (t);
4604 break;
4605 case DO_STMT:
4606 body = DO_BODY (t);
4607 cond = DO_COND (t);
4608 break;
4609 case WHILE_STMT:
4610 body = WHILE_BODY (t);
4611 cond = WHILE_COND (t);
4612 count = -1;
4613 break;
4614 case FOR_STMT:
4615 if (FOR_INIT_STMT (t))
4616 cxx_eval_constant_expression (ctx, FOR_INIT_STMT (t), /*lval*/false,
4617 non_constant_p, overflow_p, jump_target);
4618 if (*non_constant_p)
4619 return NULL_TREE;
4620 body = FOR_BODY (t);
4621 cond = FOR_COND (t);
4622 expr = FOR_EXPR (t);
4623 count = -1;
4624 break;
4625 default:
4626 gcc_unreachable ();
4627 }
0ee28590 4628 auto_vec<tree, 10> save_exprs;
43574e4f 4629 new_ctx.save_exprs = &save_exprs;
d259b234 4630 do
56632b27 4631 {
43574e4f
JJ
4632 if (count != -1)
4633 {
4634 if (body)
4635 cxx_eval_constant_expression (&new_ctx, body, /*lval*/false,
4636 non_constant_p, overflow_p,
4637 jump_target);
4638 if (breaks (jump_target))
4639 {
4640 *jump_target = NULL_TREE;
4641 break;
4642 }
39dce2b7 4643
43574e4f
JJ
4644 if (TREE_CODE (t) != LOOP_EXPR && continues (jump_target))
4645 *jump_target = NULL_TREE;
4646
4647 if (expr)
4648 cxx_eval_constant_expression (&new_ctx, expr, /*lval*/false,
4649 non_constant_p, overflow_p,
4650 jump_target);
4651 }
4652
4653 if (cond)
4654 {
4655 tree res
4656 = cxx_eval_constant_expression (&new_ctx, cond, /*lval*/false,
4657 non_constant_p, overflow_p,
4658 jump_target);
4659 if (res)
4660 {
4661 if (verify_constant (res, ctx->quiet, non_constant_p,
4662 overflow_p))
4663 break;
4664 if (integer_zerop (res))
4665 break;
4666 }
4667 else
4668 gcc_assert (*jump_target);
4669 }
39dce2b7 4670
ee1de08d 4671 /* Forget saved values of SAVE_EXPRs and TARGET_EXPRs. */
0ee28590
JJ
4672 unsigned int i;
4673 tree save_expr;
4674 FOR_EACH_VEC_ELT (save_exprs, i, save_expr)
8e007055 4675 ctx->global->values.remove (save_expr);
0ee28590 4676 save_exprs.truncate (0);
43574e4f 4677
5ec2cd9f
JM
4678 if (++count >= constexpr_loop_limit)
4679 {
4680 if (!ctx->quiet)
f9d0ca40 4681 error_at (cp_expr_loc_or_input_loc (t),
84fa214d 4682 "%<constexpr%> loop iteration count exceeds limit of %d "
a3f9f006 4683 "(use %<-fconstexpr-loop-limit=%> to increase the limit)",
5ec2cd9f
JM
4684 constexpr_loop_limit);
4685 *non_constant_p = true;
4686 break;
4687 }
56632b27 4688 }
4b390698
JJ
4689 while (!returns (jump_target)
4690 && !breaks (jump_target)
43574e4f
JJ
4691 && !continues (jump_target)
4692 && (!switches (jump_target) || count == 0)
4b390698 4693 && !*non_constant_p);
d259b234 4694
ee1de08d 4695 /* Forget saved values of SAVE_EXPRs and TARGET_EXPRs. */
0ee28590
JJ
4696 unsigned int i;
4697 tree save_expr;
4698 FOR_EACH_VEC_ELT (save_exprs, i, save_expr)
8e007055 4699 ctx->global->values.remove (save_expr);
39dce2b7 4700
56632b27
JM
4701 return NULL_TREE;
4702}
4703
4704/* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
4705 semantics. */
4706
4707static tree
4708cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t,
56632b27
JM
4709 bool *non_constant_p, bool *overflow_p,
4710 tree *jump_target)
4711{
43574e4f
JJ
4712 tree cond
4713 = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_COND (t) : SWITCH_COND (t);
2b3ab879 4714 cond = cxx_eval_constant_expression (ctx, cond, false,
5a804683 4715 non_constant_p, overflow_p);
56632b27
JM
4716 VERIFY_CONSTANT (cond);
4717 *jump_target = cond;
4718
43574e4f
JJ
4719 tree body
4720 = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_BODY (t) : SWITCH_BODY (t);
4b390698
JJ
4721 constexpr_ctx new_ctx = *ctx;
4722 constexpr_switch_state css = css_default_not_seen;
4723 new_ctx.css_state = &css;
4724 cxx_eval_constant_expression (&new_ctx, body, false,
4725 non_constant_p, overflow_p, jump_target);
4726 if (switches (jump_target) && css == css_default_seen)
4727 {
4728 /* If the SWITCH_EXPR body has default: label, process it once again,
4729 this time instructing label_matches to return true for default:
4730 label on switches (jump_target). */
4731 css = css_default_processing;
4732 cxx_eval_constant_expression (&new_ctx, body, false,
4733 non_constant_p, overflow_p, jump_target);
4734 }
56632b27
JM
4735 if (breaks (jump_target) || switches (jump_target))
4736 *jump_target = NULL_TREE;
4737 return NULL_TREE;
4738}
4739
89262ec6
JM
4740/* Find the object of TYPE under initialization in CTX. */
4741
4742static tree
4743lookup_placeholder (const constexpr_ctx *ctx, bool lval, tree type)
4744{
fbd603c4 4745 if (!ctx)
89262ec6
JM
4746 return NULL_TREE;
4747
4748 /* We could use ctx->object unconditionally, but using ctx->ctor when we
4749 can is a minor optimization. */
fbd603c4 4750 if (!lval && ctx->ctor && same_type_p (TREE_TYPE (ctx->ctor), type))
89262ec6
JM
4751 return ctx->ctor;
4752
fbd603c4
JM
4753 if (!ctx->object)
4754 return NULL_TREE;
4755
89262ec6
JM
4756 /* Since an object cannot have a field of its own type, we can search outward
4757 from ctx->object to find the unique containing object of TYPE. */
4758 tree ob = ctx->object;
4759 while (ob)
4760 {
4761 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob), type))
4762 break;
4763 if (handled_component_p (ob))
4764 ob = TREE_OPERAND (ob, 0);
4765 else
4766 ob = NULL_TREE;
4767 }
4768
4769 return ob;
4770}
4771
7c814975
MP
4772/* Complain about an attempt to evaluate inline assembly. */
4773
4774static void
4775inline_asm_in_constexpr_error (location_t loc)
4776{
4777 auto_diagnostic_group d;
4778 error_at (loc, "inline assembly is not a constant expression");
4779 inform (loc, "only unevaluated inline assembly is allowed in a "
4780 "%<constexpr%> function in C++2a");
4781}
4782
2d76680f
PC
4783/* Attempt to reduce the expression T to a constant value.
4784 On failure, issue diagnostic and return error_mark_node. */
4785/* FIXME unify with c_fully_fold */
60813a46 4786/* FIXME overflow_p is too global */
2d76680f
PC
4787
4788static tree
3e605b20 4789cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
92a596e8 4790 bool lval,
56632b27 4791 bool *non_constant_p, bool *overflow_p,
3075affd 4792 tree *jump_target /* = NULL */)
2d76680f 4793{
4b390698
JJ
4794 if (jump_target && *jump_target)
4795 {
4796 /* If we are jumping, ignore all statements/expressions except those
4797 that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */
4798 switch (TREE_CODE (t))
4799 {
4800 case BIND_EXPR:
4801 case STATEMENT_LIST:
4802 case LOOP_EXPR:
4803 case COND_EXPR:
43574e4f
JJ
4804 case IF_STMT:
4805 case DO_STMT:
4806 case WHILE_STMT:
4807 case FOR_STMT:
4b390698
JJ
4808 break;
4809 case LABEL_EXPR:
4810 case CASE_LABEL_EXPR:
4811 if (label_matches (ctx, jump_target, t))
4812 /* Found it. */
4813 *jump_target = NULL_TREE;
4814 return NULL_TREE;
4815 default:
4816 return NULL_TREE;
4817 }
4818 }
3075affd 4819 if (error_operand_p (t))
2d76680f
PC
4820 {
4821 *non_constant_p = true;
4822 return t;
4823 }
7a649ef5 4824
6821245b
JM
4825 location_t loc = cp_expr_loc_or_input_loc (t);
4826
7a649ef5
JM
4827 STRIP_ANY_LOCATION_WRAPPER (t);
4828
2d76680f
PC
4829 if (CONSTANT_CLASS_P (t))
4830 {
61637db3
JJ
4831 if (TREE_OVERFLOW (t))
4832 {
4833 if (!ctx->quiet)
4834 permerror (input_location, "overflow in constant expression");
4835 if (!flag_permissive || ctx->quiet)
4836 *overflow_p = true;
4837 }
8bada5cd
MS
4838
4839 if (TREE_CODE (t) == INTEGER_CST
9f613f06 4840 && TYPE_PTR_P (TREE_TYPE (t))
8bada5cd
MS
4841 && !integer_zerop (t))
4842 {
4843 if (!ctx->quiet)
4844 error ("value %qE of type %qT is not a constant expression",
4845 t, TREE_TYPE (t));
4846 *non_constant_p = true;
4847 }
4848
2d76680f
PC
4849 return t;
4850 }
2d76680f 4851
a15ffa22 4852 /* Avoid excessively long constexpr evaluations. */
8e007055 4853 if (++ctx->global->constexpr_ops_count >= constexpr_ops_limit)
a15ffa22
JJ
4854 {
4855 if (!ctx->quiet)
6821245b 4856 error_at (loc,
a15ffa22 4857 "%<constexpr%> evaluation operation count exceeds limit of "
a9c697b8 4858 "%wd (use %<-fconstexpr-ops-limit=%> to increase the limit)",
a15ffa22 4859 constexpr_ops_limit);
8e007055 4860 ctx->global->constexpr_ops_count = INTTYPE_MINIMUM (HOST_WIDE_INT);
a15ffa22
JJ
4861 *non_constant_p = true;
4862 return t;
4863 }
4864
7a649ef5
JM
4865 constexpr_ctx new_ctx;
4866 tree r = t;
4867
8bada5cd
MS
4868 tree_code tcode = TREE_CODE (t);
4869 switch (tcode)
2d76680f 4870 {
60813a46 4871 case RESULT_DECL:
92a596e8 4872 if (lval)
60813a46
JM
4873 return t;
4874 /* We ask for an rvalue for the RESULT_DECL when indirecting
bf66b9b4
AH
4875 through an invisible reference, or in named return value
4876 optimization. */
8e007055 4877 if (tree *p = ctx->global->values.get (t))
1efb1dc2
MP
4878 return *p;
4879 else
4880 {
4881 if (!ctx->quiet)
4882 error ("%qE is not a constant expression", t);
4883 *non_constant_p = true;
4884 }
4885 break;
60813a46 4886
2d76680f 4887 case VAR_DECL:
70f40fea 4888 if (DECL_HAS_VALUE_EXPR_P (t))
c59fa7ea
JM
4889 {
4890 if (is_normal_capture_proxy (t)
4891 && current_function_decl == DECL_CONTEXT (t))
4892 {
4893 /* Function parms aren't constexpr within the function
4894 definition, so don't try to look at the closure. But if the
4895 captured variable is constant, try to evaluate it directly. */
4896 r = DECL_CAPTURED_VARIABLE (t);
4897 tree type = TREE_TYPE (t);
4898 if (TYPE_REF_P (type) != TYPE_REF_P (TREE_TYPE (r)))
4899 {
4900 /* Adjust r to match the reference-ness of t. */
4901 if (TYPE_REF_P (type))
4902 r = build_address (r);
4903 else
4904 r = convert_from_reference (r);
4905 }
4906 }
4907 else
4908 r = DECL_VALUE_EXPR (t);
4909 return cxx_eval_constant_expression (ctx, r, lval, non_constant_p,
4910 overflow_p);
4911 }
191816a3 4912 /* fall through */
7c83622c
JM
4913 case CONST_DECL:
4914 /* We used to not check lval for CONST_DECL, but darwin.c uses
4915 CONST_DECL for aggregate constants. */
92a596e8 4916 if (lval)
2d76680f 4917 return t;
f4fce183 4918 if (COMPLETE_TYPE_P (TREE_TYPE (t))
dbcd32f8 4919 && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
7dc2b4a2
JM
4920 {
4921 /* If the class is empty, we aren't actually loading anything. */
4922 r = build_constructor (TREE_TYPE (t), NULL);
4923 TREE_CONSTANT (r) = true;
4924 }
4925 else if (ctx->strict)
69eb4fde
JM
4926 r = decl_really_constant_value (t);
4927 else
4928 r = decl_constant_value (t);
2d76680f
PC
4929 if (TREE_CODE (r) == TARGET_EXPR
4930 && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR)
4931 r = TARGET_EXPR_INITIAL (r);
56a6f1d3 4932 if (VAR_P (r))
8e007055 4933 if (tree *p = ctx->global->values.get (r))
bd8f5bb2
MP
4934 if (*p != NULL_TREE)
4935 r = *p;
2d76680f
PC
4936 if (DECL_P (r))
4937 {
2b3ab879 4938 if (!ctx->quiet)
6821245b 4939 non_const_var_error (loc, r);
2d76680f
PC
4940 *non_constant_p = true;
4941 }
4942 break;
4943
96a95ac1
AO
4944 case DEBUG_BEGIN_STMT:
4945 /* ??? It might be nice to retain this information somehow, so
4946 as to be able to step into a constexpr function call. */
4947 /* Fall through. */
4948
2d76680f
PC
4949 case FUNCTION_DECL:
4950 case TEMPLATE_DECL:
4951 case LABEL_DECL:
56632b27
JM
4952 case LABEL_EXPR:
4953 case CASE_LABEL_EXPR:
4b390698 4954 case PREDICT_EXPR:
2d76680f
PC
4955 return t;
4956
4957 case PARM_DECL:
9f613f06 4958 if (lval && !TYPE_REF_P (TREE_TYPE (t)))
60813a46 4959 /* glvalue use. */;
8e007055 4960 else if (tree *p = ctx->global->values.get (r))
60813a46 4961 r = *p;
92a596e8 4962 else if (lval)
2d76680f 4963 /* Defer in case this is only used for its type. */;
9f613f06 4964 else if (TYPE_REF_P (TREE_TYPE (t)))
f2acb8ad 4965 /* Defer, there's no lvalue->rvalue conversion. */;
1c2f613f 4966 else if (COMPLETE_TYPE_P (TREE_TYPE (t))
dbcd32f8 4967 && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
052beba4
JM
4968 {
4969 /* If the class is empty, we aren't actually loading anything. */
4970 r = build_constructor (TREE_TYPE (t), NULL);
4971 TREE_CONSTANT (r) = true;
4972 }
2d76680f
PC
4973 else
4974 {
2b3ab879 4975 if (!ctx->quiet)
2d76680f
PC
4976 error ("%qE is not a constant expression", t);
4977 *non_constant_p = true;
4978 }
4979 break;
4980
4981 case CALL_EXPR:
4982 case AGGR_INIT_EXPR:
92a596e8 4983 r = cxx_eval_call_expression (ctx, t, lval,
2d76680f
PC
4984 non_constant_p, overflow_p);
4985 break;
4986
60813a46
JM
4987 case DECL_EXPR:
4988 {
4989 r = DECL_EXPR_DECL (t);
43574e4f
JJ
4990 if (TREE_CODE (r) == USING_DECL)
4991 {
4992 r = void_node;
4993 break;
4994 }
60813a46
JM
4995 if (AGGREGATE_TYPE_P (TREE_TYPE (r))
4996 || VECTOR_TYPE_P (TREE_TYPE (r)))
4997 {
4998 new_ctx = *ctx;
4999 new_ctx.object = r;
5000 new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL);
e8c48716 5001 CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
8e007055 5002 ctx->global->values.put (r, new_ctx.ctor);
60813a46
JM
5003 ctx = &new_ctx;
5004 }
5005
5006 if (tree init = DECL_INITIAL (r))
5007 {
5008 init = cxx_eval_constant_expression (ctx, init,
2b3ab879 5009 false,
5a804683 5010 non_constant_p, overflow_p);
7f0e23e9 5011 /* Don't share a CONSTRUCTOR that might be changed. */
0146e25f 5012 init = unshare_constructor (init);
04e1749c
MP
5013 /* Remember that a constant object's constructor has already
5014 run. */
5015 if (CLASS_TYPE_P (TREE_TYPE (r))
5016 && CP_TYPE_CONST_P (TREE_TYPE (r)))
5017 TREE_READONLY (init) = true;
8e007055 5018 ctx->global->values.put (r, init);
60813a46
JM
5019 }
5020 else if (ctx == &new_ctx)
5021 /* We gave it a CONSTRUCTOR above. */;
5022 else
8e007055 5023 ctx->global->values.put (r, NULL_TREE);
60813a46
JM
5024 }
5025 break;
5026
2d76680f
PC
5027 case TARGET_EXPR:
5028 if (!literal_type_p (TREE_TYPE (t)))
5029 {
2b3ab879 5030 if (!ctx->quiet)
2d76680f 5031 {
097f82ec 5032 auto_diagnostic_group d;
2d76680f
PC
5033 error ("temporary of non-literal type %qT in a "
5034 "constant expression", TREE_TYPE (t));
5035 explain_non_literal_class (TREE_TYPE (t));
5036 }
5037 *non_constant_p = true;
5038 break;
5039 }
ee1de08d
JJ
5040 /* Avoid evaluating a TARGET_EXPR more than once. */
5041 if (tree *p = ctx->global->values.get (TARGET_EXPR_SLOT (t)))
5042 {
5043 if (lval)
5044 return TARGET_EXPR_SLOT (t);
5045 r = *p;
5046 break;
5047 }
3e605b20
JM
5048 if ((AGGREGATE_TYPE_P (TREE_TYPE (t)) || VECTOR_TYPE_P (TREE_TYPE (t))))
5049 {
5050 /* We're being expanded without an explicit target, so start
5051 initializing a new object; expansion with an explicit target
5052 strips the TARGET_EXPR before we get here. */
5053 new_ctx = *ctx;
5054 new_ctx.ctor = build_constructor (TREE_TYPE (t), NULL);
e8c48716 5055 CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
3e605b20 5056 new_ctx.object = TARGET_EXPR_SLOT (t);
8e007055 5057 ctx->global->values.put (new_ctx.object, new_ctx.ctor);
3e605b20
JM
5058 ctx = &new_ctx;
5059 }
92a596e8 5060 /* Pass false for 'lval' because this indicates
2d76680f 5061 initialization of a temporary. */
3e605b20 5062 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
2b3ab879 5063 false,
5a804683 5064 non_constant_p, overflow_p);
2d76680f
PC
5065 if (!*non_constant_p)
5066 /* Adjust the type of the result to the type of the temporary. */
5067 r = adjust_temp_type (TREE_TYPE (t), r);
ee1de08d
JJ
5068 if (TARGET_EXPR_CLEANUP (t) && !CLEANUP_EH_ONLY (t))
5069 ctx->global->cleanups->safe_push (TARGET_EXPR_CLEANUP (t));
5070 r = unshare_constructor (r);
5071 ctx->global->values.put (TARGET_EXPR_SLOT (t), r);
5072 if (ctx->save_exprs)
5073 ctx->save_exprs->safe_push (TARGET_EXPR_SLOT (t));
92a596e8 5074 if (lval)
ee1de08d 5075 return TARGET_EXPR_SLOT (t);
2d76680f
PC
5076 break;
5077
60813a46 5078 case INIT_EXPR:
60813a46 5079 case MODIFY_EXPR:
4b390698 5080 gcc_assert (jump_target == NULL || *jump_target == NULL_TREE);
92a596e8 5081 r = cxx_eval_store_expression (ctx, t, lval,
60813a46
JM
5082 non_constant_p, overflow_p);
5083 break;
5084
2d76680f 5085 case SCOPE_REF:
3e605b20 5086 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
92a596e8 5087 lval,
5a804683 5088 non_constant_p, overflow_p);
2d76680f
PC
5089 break;
5090
5091 case RETURN_EXPR:
75e0295b
MP
5092 if (TREE_OPERAND (t, 0) != NULL_TREE)
5093 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
5094 lval,
5095 non_constant_p, overflow_p);
43574e4f
JJ
5096 /* FALLTHRU */
5097 case BREAK_STMT:
5098 case CONTINUE_STMT:
019e0ae8
MP
5099 if (jump_target)
5100 *jump_target = t;
5101 else
5102 {
5103 /* Can happen with ({ return true; }) && false; passed to
5104 maybe_constant_value. There is nothing to jump over in this
5105 case, and the bug will be diagnosed later. */
5106 gcc_assert (ctx->quiet);
5107 *non_constant_p = true;
5108 }
56632b27
JM
5109 break;
5110
cabaf50a
JM
5111 case SAVE_EXPR:
5112 /* Avoid evaluating a SAVE_EXPR more than once. */
8e007055 5113 if (tree *p = ctx->global->values.get (t))
cabaf50a
JM
5114 r = *p;
5115 else
5116 {
12d9ce19 5117 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), false,
cabaf50a 5118 non_constant_p, overflow_p);
8e007055 5119 ctx->global->values.put (t, r);
39dce2b7 5120 if (ctx->save_exprs)
0ee28590 5121 ctx->save_exprs->safe_push (t);
cabaf50a
JM
5122 }
5123 break;
5124
2d76680f 5125 case TRY_CATCH_EXPR:
ee1de08d
JJ
5126 if (TREE_OPERAND (t, 0) == NULL_TREE)
5127 {
5128 r = void_node;
5129 break;
5130 }
5131 /* FALLTHRU */
5132 case NON_LVALUE_EXPR:
769430b2 5133 case TRY_BLOCK:
2d76680f 5134 case MUST_NOT_THROW_EXPR:
60813a46
JM
5135 case EXPR_STMT:
5136 case EH_SPEC_BLOCK:
3e605b20 5137 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
92a596e8 5138 lval,
56632b27
JM
5139 non_constant_p, overflow_p,
5140 jump_target);
2d76680f
PC
5141 break;
5142
ee1de08d
JJ
5143 case CLEANUP_POINT_EXPR:
5144 {
5145 auto_vec<tree, 2> cleanups;
5146 vec<tree> *prev_cleanups = ctx->global->cleanups;
5147 ctx->global->cleanups = &cleanups;
5148 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
5149 lval,
5150 non_constant_p, overflow_p,
5151 jump_target);
5152 ctx->global->cleanups = prev_cleanups;
5153 unsigned int i;
5154 tree cleanup;
5155 /* Evaluate the cleanups. */
5156 FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup)
5157 cxx_eval_constant_expression (ctx, cleanup, false,
5158 non_constant_p, overflow_p,
5159 jump_target);
5160 }
5161 break;
5162
769430b2
JM
5163 case TRY_FINALLY_EXPR:
5164 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
5165 non_constant_p, overflow_p,
5166 jump_target);
5167 if (!*non_constant_p)
5168 /* Also evaluate the cleanup. */
5169 cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), true,
5170 non_constant_p, overflow_p,
5171 jump_target);
5172 break;
5173
43574e4f 5174 case CLEANUP_STMT:
1006c9d4
JJ
5175 {
5176 tree initial_jump_target = jump_target ? *jump_target : NULL_TREE;
5177 r = cxx_eval_constant_expression (ctx, CLEANUP_BODY (t), lval,
5178 non_constant_p, overflow_p,
5179 jump_target);
5180 if (!CLEANUP_EH_ONLY (t) && !*non_constant_p)
acfdb960 5181 {
6821245b 5182 iloc_sentinel ils (loc);
acfdb960
JJ
5183 /* Also evaluate the cleanup. If we weren't skipping at the
5184 start of the CLEANUP_BODY, change jump_target temporarily
5185 to &initial_jump_target, so that even a return or break or
5186 continue in the body doesn't skip the cleanup. */
5187 cxx_eval_constant_expression (ctx, CLEANUP_EXPR (t), true,
5188 non_constant_p, overflow_p,
5189 jump_target ? &initial_jump_target
5190 : NULL);
acfdb960 5191 }
1006c9d4 5192 }
43574e4f
JJ
5193 break;
5194
2d76680f
PC
5195 /* These differ from cxx_eval_unary_expression in that this doesn't
5196 check for a constant operand or result; an address can be
5197 constant without its operand being, and vice versa. */
cda0a029 5198 case MEM_REF:
2d76680f 5199 case INDIRECT_REF:
92a596e8 5200 r = cxx_eval_indirect_ref (ctx, t, lval,
2d76680f
PC
5201 non_constant_p, overflow_p);
5202 break;
5203
5204 case ADDR_EXPR:
5205 {
5206 tree oldop = TREE_OPERAND (t, 0);
3e605b20 5207 tree op = cxx_eval_constant_expression (ctx, oldop,
92a596e8 5208 /*lval*/true,
5a804683 5209 non_constant_p, overflow_p);
2d76680f
PC
5210 /* Don't VERIFY_CONSTANT here. */
5211 if (*non_constant_p)
5212 return t;
f2acb8ad 5213 gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR);
2d76680f
PC
5214 /* This function does more aggressive folding than fold itself. */
5215 r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
5216 if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
7a649ef5
JM
5217 {
5218 ggc_free (r);
5219 return t;
5220 }
2d76680f
PC
5221 break;
5222 }
5223
5224 case REALPART_EXPR:
5225 case IMAGPART_EXPR:
87713c6a
JJ
5226 if (lval)
5227 {
5228 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
5229 non_constant_p, overflow_p);
5230 if (r == error_mark_node)
5231 ;
5232 else if (r == TREE_OPERAND (t, 0))
5233 r = t;
5234 else
5235 r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), r);
5236 break;
5237 }
5238 /* FALLTHRU */
2d76680f
PC
5239 case CONJ_EXPR:
5240 case FIX_TRUNC_EXPR:
5241 case FLOAT_EXPR:
5242 case NEGATE_EXPR:
5243 case ABS_EXPR:
e28fadbc 5244 case ABSU_EXPR:
2d76680f
PC
5245 case BIT_NOT_EXPR:
5246 case TRUTH_NOT_EXPR:
5247 case FIXED_CONVERT_EXPR:
92a596e8 5248 r = cxx_eval_unary_expression (ctx, t, lval,
2d76680f
PC
5249 non_constant_p, overflow_p);
5250 break;
5251
5252 case SIZEOF_EXPR:
99e764a2
MP
5253 r = fold_sizeof_expr (t);
5254 /* In a template, fold_sizeof_expr may merely create a new SIZEOF_EXPR,
5255 which could lead to an infinite recursion. */
5256 if (TREE_CODE (r) != SIZEOF_EXPR)
5257 r = cxx_eval_constant_expression (ctx, r, lval,
5258 non_constant_p, overflow_p,
5259 jump_target);
5260 else
5261 {
5262 *non_constant_p = true;
5263 gcc_assert (ctx->quiet);
5264 }
5265
2d76680f
PC
5266 break;
5267
5268 case COMPOUND_EXPR:
5269 {
5270 /* check_return_expr sometimes wraps a TARGET_EXPR in a
5271 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
5272 introduced by build_call_a. */
5273 tree op0 = TREE_OPERAND (t, 0);
5274 tree op1 = TREE_OPERAND (t, 1);
5275 STRIP_NOPS (op1);
5276 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
5277 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
2b3ab879 5278 r = cxx_eval_constant_expression (ctx, op0,
92a596e8 5279 lval, non_constant_p, overflow_p,
56632b27 5280 jump_target);
2d76680f
PC
5281 else
5282 {
5283 /* Check that the LHS is constant and then discard it. */
2b3ab879 5284 cxx_eval_constant_expression (ctx, op0,
12d9ce19 5285 true, non_constant_p, overflow_p,
56632b27 5286 jump_target);
06ec22b7
JM
5287 if (*non_constant_p)
5288 return t;
2d76680f 5289 op1 = TREE_OPERAND (t, 1);
2b3ab879 5290 r = cxx_eval_constant_expression (ctx, op1,
92a596e8 5291 lval, non_constant_p, overflow_p,
56632b27 5292 jump_target);
2d76680f
PC
5293 }
5294 }
5295 break;
5296
5297 case POINTER_PLUS_EXPR:
1af4ebf5 5298 case POINTER_DIFF_EXPR:
2d76680f
PC
5299 case PLUS_EXPR:
5300 case MINUS_EXPR:
5301 case MULT_EXPR:
5302 case TRUNC_DIV_EXPR:
5303 case CEIL_DIV_EXPR:
5304 case FLOOR_DIV_EXPR:
5305 case ROUND_DIV_EXPR:
5306 case TRUNC_MOD_EXPR:
5307 case CEIL_MOD_EXPR:
5308 case ROUND_MOD_EXPR:
5309 case RDIV_EXPR:
5310 case EXACT_DIV_EXPR:
5311 case MIN_EXPR:
5312 case MAX_EXPR:
5313 case LSHIFT_EXPR:
5314 case RSHIFT_EXPR:
2d76680f
PC
5315 case BIT_IOR_EXPR:
5316 case BIT_XOR_EXPR:
5317 case BIT_AND_EXPR:
5318 case TRUTH_XOR_EXPR:
5319 case LT_EXPR:
5320 case LE_EXPR:
5321 case GT_EXPR:
5322 case GE_EXPR:
5323 case EQ_EXPR:
5324 case NE_EXPR:
b7689b96 5325 case SPACESHIP_EXPR:
2d76680f
PC
5326 case UNORDERED_EXPR:
5327 case ORDERED_EXPR:
5328 case UNLT_EXPR:
5329 case UNLE_EXPR:
5330 case UNGT_EXPR:
5331 case UNGE_EXPR:
5332 case UNEQ_EXPR:
5333 case LTGT_EXPR:
5334 case RANGE_EXPR:
5335 case COMPLEX_EXPR:
92a596e8 5336 r = cxx_eval_binary_expression (ctx, t, lval,
2d76680f
PC
5337 non_constant_p, overflow_p);
5338 break;
5339
5340 /* fold can introduce non-IF versions of these; still treat them as
5341 short-circuiting. */
5342 case TRUTH_AND_EXPR:
5343 case TRUTH_ANDIF_EXPR:
3e605b20 5344 r = cxx_eval_logical_expression (ctx, t, boolean_false_node,
2d76680f 5345 boolean_true_node,
92a596e8 5346 lval,
2d76680f
PC
5347 non_constant_p, overflow_p);
5348 break;
5349
5350 case TRUTH_OR_EXPR:
5351 case TRUTH_ORIF_EXPR:
3e605b20 5352 r = cxx_eval_logical_expression (ctx, t, boolean_true_node,
2d76680f 5353 boolean_false_node,
92a596e8 5354 lval,
2d76680f
PC
5355 non_constant_p, overflow_p);
5356 break;
5357
5358 case ARRAY_REF:
92a596e8 5359 r = cxx_eval_array_reference (ctx, t, lval,
2d76680f
PC
5360 non_constant_p, overflow_p);
5361 break;
5362
5363 case COMPONENT_REF:
5364 if (is_overloaded_fn (t))
5365 {
5366 /* We can only get here in checking mode via
5367 build_non_dependent_expr, because any expression that
5368 calls or takes the address of the function will have
5369 pulled a FUNCTION_DECL out of the COMPONENT_REF. */
2b3ab879 5370 gcc_checking_assert (ctx->quiet || errorcount);
2d76680f
PC
5371 *non_constant_p = true;
5372 return t;
5373 }
92a596e8 5374 r = cxx_eval_component_reference (ctx, t, lval,
2d76680f
PC
5375 non_constant_p, overflow_p);
5376 break;
5377
5378 case BIT_FIELD_REF:
92a596e8 5379 r = cxx_eval_bit_field_ref (ctx, t, lval,
2d76680f
PC
5380 non_constant_p, overflow_p);
5381 break;
5382
5383 case COND_EXPR:
43574e4f 5384 case IF_STMT:
4b390698
JJ
5385 if (jump_target && *jump_target)
5386 {
e9fa2f6d 5387 tree orig_jump = *jump_target;
43574e4f
JJ
5388 tree arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 1))
5389 ? TREE_OPERAND (t, 1) : void_node);
4b390698
JJ
5390 /* When jumping to a label, the label might be either in the
5391 then or else blocks, so process then block first in skipping
5392 mode first, and if we are still in the skipping mode at its end,
5393 process the else block too. */
43574e4f
JJ
5394 r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p,
5395 overflow_p, jump_target);
e9fa2f6d
MP
5396 /* It's possible that we found the label in the then block. But
5397 it could have been followed by another jumping statement, e.g.
5398 say we're looking for case 1:
5399 if (cond)
5400 {
5401 // skipped statements
5402 case 1:; // clears up *jump_target
5403 return 1; // and sets it to a RETURN_EXPR
5404 }
5405 else { ... }
5406 in which case we need not go looking to the else block.
5407 (goto is not allowed in a constexpr function.) */
5408 if (*jump_target == orig_jump)
43574e4f
JJ
5409 {
5410 arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 2))
5411 ? TREE_OPERAND (t, 2) : void_node);
5412 r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p,
5413 overflow_p, jump_target);
5414 }
4b390698
JJ
5415 break;
5416 }
92a596e8 5417 r = cxx_eval_conditional_expression (ctx, t, lval,
56632b27
JM
5418 non_constant_p, overflow_p,
5419 jump_target);
2d76680f 5420 break;
f370e36d
JJ
5421 case VEC_COND_EXPR:
5422 r = cxx_eval_vector_conditional_expression (ctx, t, non_constant_p,
5423 overflow_p);
5424 break;
2d76680f
PC
5425
5426 case CONSTRUCTOR:
35d87f01 5427 if (TREE_CONSTANT (t) && reduced_constant_expression_p (t))
2d63bc39
JM
5428 {
5429 /* Don't re-process a constant CONSTRUCTOR, but do fold it to
5430 VECTOR_CST if applicable. */
ac9ec198 5431 verify_constructor_flags (t);
2d63bc39
JM
5432 if (TREE_CONSTANT (t))
5433 return fold (t);
5434 }
92a596e8 5435 r = cxx_eval_bare_aggregate (ctx, t, lval,
2d76680f
PC
5436 non_constant_p, overflow_p);
5437 break;
5438
5439 case VEC_INIT_EXPR:
5440 /* We can get this in a defaulted constructor for a class with a
5441 non-static data member of array type. Either the initializer will
5442 be NULL, meaning default-initialization, or it will be an lvalue
5443 or xvalue of the same type, meaning direct-initialization from the
5444 corresponding member. */
92a596e8 5445 r = cxx_eval_vec_init (ctx, t, lval,
2d76680f
PC
5446 non_constant_p, overflow_p);
5447 break;
5448
2d76680f 5449 case VEC_PERM_EXPR:
92a596e8 5450 r = cxx_eval_trinary_expression (ctx, t, lval,
2d76680f
PC
5451 non_constant_p, overflow_p);
5452 break;
5453
7d75ea04
JJ
5454 case NOP_EXPR:
5455 if (REINTERPRET_CAST_P (t))
5456 {
5457 if (!ctx->quiet)
6821245b 5458 error_at (loc,
a9c697b8 5459 "%<reinterpret_cast%> is not a constant expression");
7d75ea04
JJ
5460 *non_constant_p = true;
5461 return t;
5462 }
5463 /* FALLTHROUGH. */
2d76680f
PC
5464 case CONVERT_EXPR:
5465 case VIEW_CONVERT_EXPR:
cda0a029 5466 case UNARY_PLUS_EXPR:
2d76680f
PC
5467 {
5468 tree oldop = TREE_OPERAND (t, 0);
cda0a029 5469
3e605b20 5470 tree op = cxx_eval_constant_expression (ctx, oldop,
92a596e8 5471 lval,
5a804683 5472 non_constant_p, overflow_p);
2d76680f
PC
5473 if (*non_constant_p)
5474 return t;
dcdbc004 5475 tree type = TREE_TYPE (t);
02a8575c
JM
5476
5477 if (VOID_TYPE_P (type))
5478 return void_node;
5479
8e007055 5480 if (TREE_CODE (op) == PTRMEM_CST && !TYPE_PTRMEM_P (type))
dcdbc004 5481 op = cplus_expand_constant (op);
7d75ea04 5482
05bf54c3
MP
5483 if (TREE_CODE (op) == PTRMEM_CST && tcode == NOP_EXPR)
5484 {
7d75ea04
JJ
5485 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (op))
5486 && !can_convert_qual (type, op))
5487 op = cplus_expand_constant (op);
5488 return cp_fold_convert (type, op);
05bf54c3 5489 }
8bada5cd 5490
71a93b08 5491 if (INDIRECT_TYPE_P (type) && TREE_CODE (op) == INTEGER_CST)
2d76680f 5492 {
8bada5cd
MS
5493 if (integer_zerop (op))
5494 {
9f613f06 5495 if (TYPE_REF_P (type))
8bada5cd
MS
5496 {
5497 if (!ctx->quiet)
6821245b 5498 error_at (loc,
8bada5cd
MS
5499 "dereferencing a null pointer");
5500 *non_constant_p = true;
5501 return t;
5502 }
9f613f06 5503 else if (TYPE_PTR_P (TREE_TYPE (op)))
8bada5cd
MS
5504 {
5505 tree from = TREE_TYPE (op);
5506
5507 if (!can_convert (type, from, tf_none))
5508 {
5509 if (!ctx->quiet)
6821245b 5510 error_at (loc,
8bada5cd
MS
5511 "conversion of %qT null pointer to %qT "
5512 "is not a constant expression",
5513 from, type);
5514 *non_constant_p = true;
5515 return t;
5516 }
5517 }
5518 }
5519 else
5520 {
5521 /* This detects for example:
5522 reinterpret_cast<void*>(sizeof 0)
5523 */
5524 if (!ctx->quiet)
6821245b 5525 error_at (loc, "%<reinterpret_cast<%T>(%E)%> is not "
64d6d399 5526 "a constant expression",
8bada5cd
MS
5527 type, op);
5528 *non_constant_p = true;
5529 return t;
5530 }
2d76680f 5531 }
7d75ea04 5532
8e007055
JJ
5533 if (INDIRECT_TYPE_P (type)
5534 && TREE_CODE (op) == NOP_EXPR
5535 && TREE_TYPE (op) == ptr_type_node
5536 && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR
5537 && VAR_P (TREE_OPERAND (TREE_OPERAND (op, 0), 0))
5538 && DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0),
5539 0)) == heap_uninit_identifier)
5540 {
5541 tree var = TREE_OPERAND (TREE_OPERAND (op, 0), 0);
5542 tree var_size = TYPE_SIZE_UNIT (TREE_TYPE (var));
5543 tree elt_type = TREE_TYPE (type);
5544 tree cookie_size = NULL_TREE;
5545 if (TREE_CODE (elt_type) == RECORD_TYPE
5546 && TYPE_NAME (elt_type) == heap_identifier)
5547 {
5548 tree fld1 = TYPE_FIELDS (elt_type);
5549 tree fld2 = DECL_CHAIN (fld1);
5550 elt_type = TREE_TYPE (TREE_TYPE (fld2));
5551 cookie_size = TYPE_SIZE_UNIT (TREE_TYPE (fld1));
5552 }
5553 DECL_NAME (var) = heap_identifier;
5554 TREE_TYPE (var)
5555 = build_new_constexpr_heap_type (elt_type, cookie_size,
5556 var_size);
5557 TREE_TYPE (TREE_OPERAND (op, 0))
5558 = build_pointer_type (TREE_TYPE (var));
5559 }
5560
cda0a029 5561 if (op == oldop && tcode != UNARY_PLUS_EXPR)
2d76680f
PC
5562 /* We didn't fold at the top so we could check for ptr-int
5563 conversion. */
5564 return fold (t);
7d75ea04 5565
02a8575c
JM
5566 tree sop;
5567
e4511ca2
JM
5568 /* Handle an array's bounds having been deduced after we built
5569 the wrapping expression. */
5570 if (same_type_ignoring_tlq_and_bounds_p (type, TREE_TYPE (op)))
5571 r = op;
02a8575c
JM
5572 else if (sop = tree_strip_nop_conversions (op),
5573 sop != op && (same_type_ignoring_tlq_and_bounds_p
5574 (type, TREE_TYPE (sop))))
5575 r = sop;
e4511ca2 5576 else if (tcode == UNARY_PLUS_EXPR)
cda0a029
JM
5577 r = fold_convert (TREE_TYPE (t), op);
5578 else
5579 r = fold_build1 (tcode, type, op);
7d75ea04 5580
2d76680f
PC
5581 /* Conversion of an out-of-range value has implementation-defined
5582 behavior; the language considers it different from arithmetic
5583 overflow, which is undefined. */
5584 if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op))
5585 TREE_OVERFLOW (r) = false;
5586 }
5587 break;
5588
5589 case EMPTY_CLASS_EXPR:
5590 /* This is good enough for a function argument that might not get
5591 used, and they can't do anything with it, so just return it. */
5592 return t;
5593
60813a46 5594 case STATEMENT_LIST:
56632b27
JM
5595 new_ctx = *ctx;
5596 new_ctx.ctor = new_ctx.object = NULL_TREE;
2b3ab879 5597 return cxx_eval_statement_list (&new_ctx, t,
56632b27 5598 non_constant_p, overflow_p, jump_target);
60813a46
JM
5599
5600 case BIND_EXPR:
5601 return cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t),
92a596e8 5602 lval,
56632b27
JM
5603 non_constant_p, overflow_p,
5604 jump_target);
60813a46 5605
2d76680f
PC
5606 case PREINCREMENT_EXPR:
5607 case POSTINCREMENT_EXPR:
5608 case PREDECREMENT_EXPR:
5609 case POSTDECREMENT_EXPR:
2b3ab879 5610 return cxx_eval_increment_expression (ctx, t,
92a596e8 5611 lval, non_constant_p, overflow_p);
60813a46
JM
5612
5613 case LAMBDA_EXPR:
2d76680f
PC
5614 case NEW_EXPR:
5615 case VEC_NEW_EXPR:
5616 case DELETE_EXPR:
5617 case VEC_DELETE_EXPR:
5618 case THROW_EXPR:
2d76680f
PC
5619 case MODOP_EXPR:
5620 /* GCC internal stuff. */
5621 case VA_ARG_EXPR:
2d76680f
PC
5622 case NON_DEPENDENT_EXPR:
5623 case BASELINK:
2d76680f 5624 case OFFSET_REF:
2b3ab879 5625 if (!ctx->quiet)
6821245b 5626 error_at (loc, "expression %qE is not a constant expression", t);
2d76680f
PC
5627 *non_constant_p = true;
5628 break;
5629
bf8d8309
MP
5630 case OBJ_TYPE_REF:
5631 {
5632 /* Virtual function call. Let the constexpr machinery figure out
5633 the dynamic type. */
5634 int token = tree_to_shwi (OBJ_TYPE_REF_TOKEN (t));
5635 tree obj = OBJ_TYPE_REF_OBJECT (t);
5636 obj = cxx_eval_constant_expression (ctx, obj, lval, non_constant_p,
5637 overflow_p);
3a0bd58f 5638 STRIP_NOPS (obj);
bf8d8309 5639 /* We expect something in the form of &x.D.2103.D.2094; get x. */
6a3ebcc6
JJ
5640 if (TREE_CODE (obj) != ADDR_EXPR
5641 || !DECL_P (get_base_address (TREE_OPERAND (obj, 0))))
bf8d8309
MP
5642 {
5643 if (!ctx->quiet)
6821245b 5644 error_at (loc, "expression %qE is not a constant expression", t);
bf8d8309
MP
5645 *non_constant_p = true;
5646 return t;
5647 }
5648 obj = TREE_OPERAND (obj, 0);
69deaf14
JJ
5649 while (TREE_CODE (obj) == COMPONENT_REF
5650 && DECL_FIELD_IS_BASE (TREE_OPERAND (obj, 1)))
bf8d8309
MP
5651 obj = TREE_OPERAND (obj, 0);
5652 tree objtype = TREE_TYPE (obj);
5653 /* Find the function decl in the virtual functions list. TOKEN is
5654 the DECL_VINDEX that says which function we're looking for. */
5655 tree virtuals = BINFO_VIRTUALS (TYPE_BINFO (objtype));
582d2481
JJ
5656 if (TARGET_VTABLE_USES_DESCRIPTORS)
5657 token /= MAX (TARGET_VTABLE_USES_DESCRIPTORS, 1);
bf8d8309
MP
5658 r = TREE_VALUE (chain_index (token, virtuals));
5659 break;
5660 }
5661
3e605b20 5662 case PLACEHOLDER_EXPR:
89262ec6
JM
5663 /* Use of the value or address of the current object. */
5664 if (tree ctor = lookup_placeholder (ctx, lval, TREE_TYPE (t)))
5665 return cxx_eval_constant_expression (ctx, ctor, lval,
5666 non_constant_p, overflow_p);
5667 /* A placeholder without a referent. We can get here when
5668 checking whether NSDMIs are noexcept, or in massage_init_elt;
5669 just say it's non-constant for now. */
5670 gcc_assert (ctx->quiet);
5671 *non_constant_p = true;
3e605b20
JM
5672 break;
5673
06ec22b7
JM
5674 case EXIT_EXPR:
5675 {
5676 tree cond = TREE_OPERAND (t, 0);
5677 cond = cxx_eval_constant_expression (ctx, cond, /*lval*/false,
5678 non_constant_p, overflow_p);
5679 VERIFY_CONSTANT (cond);
5680 if (integer_nonzerop (cond))
5681 *jump_target = t;
5682 }
5683 break;
5684
60813a46 5685 case GOTO_EXPR:
56632b27 5686 *jump_target = TREE_OPERAND (t, 0);
d49318d9
PC
5687 gcc_assert (breaks (jump_target) || continues (jump_target)
5688 /* Allow for jumping to a cdtor_label. */
5689 || returns (jump_target));
56632b27
JM
5690 break;
5691
60813a46 5692 case LOOP_EXPR:
43574e4f
JJ
5693 case DO_STMT:
5694 case WHILE_STMT:
5695 case FOR_STMT:
2b3ab879 5696 cxx_eval_loop_expr (ctx, t,
56632b27
JM
5697 non_constant_p, overflow_p, jump_target);
5698 break;
5699
60813a46 5700 case SWITCH_EXPR:
43574e4f 5701 case SWITCH_STMT:
2b3ab879 5702 cxx_eval_switch_expr (ctx, t,
56632b27 5703 non_constant_p, overflow_p, jump_target);
60813a46
JM
5704 break;
5705
971e17ff
AS
5706 case REQUIRES_EXPR:
5707 /* It's possible to get a requires-expression in a constant
5708 expression. For example:
5709
5710 template<typename T> concept bool C() {
5711 return requires (T t) { t; };
5712 }
5713
5714 template<typename T> requires !C<T>() void f(T);
5715
5716 Normalization leaves f with the associated constraint
5717 '!requires (T t) { ... }' which is not transformed into
5718 a constraint. */
5719 if (!processing_template_decl)
cb57504a 5720 return satisfy_constraint_expression (t);
971e17ff
AS
5721 else
5722 *non_constant_p = true;
5723 return t;
5724
98e09245 5725 case ANNOTATE_EXPR:
98e09245
JJ
5726 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
5727 lval,
5728 non_constant_p, overflow_p,
5729 jump_target);
5730 break;
5731
3664e317
JM
5732 case USING_STMT:
5733 r = void_node;
5734 break;
5735
cb57504a
JM
5736 case TEMPLATE_ID_EXPR:
5737 {
5738 /* We can evaluate template-id that refers to a concept only if
5739 the template arguments are non-dependent. */
861d4af8
AS
5740 tree id = unpack_concept_check (t);
5741 tree tmpl = TREE_OPERAND (id, 0);
5742 if (!concept_definition_p (tmpl))
cb57504a
JM
5743 internal_error ("unexpected template-id %qE", t);
5744
861d4af8
AS
5745 if (function_concept_p (tmpl))
5746 {
5747 if (!ctx->quiet)
5748 error_at (cp_expr_loc_or_input_loc (t),
5749 "function concept must be called");
5750 r = error_mark_node;
5751 break;
5752 }
5753
cb57504a 5754 if (!processing_template_decl)
861d4af8 5755 r = evaluate_concept_check (t, tf_warning_or_error);
cb57504a
JM
5756 else
5757 *non_constant_p = true;
861d4af8
AS
5758
5759 break;
cb57504a
JM
5760 }
5761
529bc410
MP
5762 case ASM_EXPR:
5763 if (!ctx->quiet)
6821245b 5764 inline_asm_in_constexpr_error (loc);
529bc410
MP
5765 *non_constant_p = true;
5766 return t;
5767
2d76680f 5768 default:
c6e7c499
JM
5769 if (STATEMENT_CODE_P (TREE_CODE (t)))
5770 {
5771 /* This function doesn't know how to deal with pre-genericize
5772 statements; this can only happen with statement-expressions,
5773 so for now just fail. */
5774 if (!ctx->quiet)
5775 error_at (EXPR_LOCATION (t),
64d6d399 5776 "statement is not a constant expression");
c6e7c499
JM
5777 }
5778 else
5779 internal_error ("unexpected expression %qE of kind %s", t,
5780 get_tree_code_name (TREE_CODE (t)));
2d76680f
PC
5781 *non_constant_p = true;
5782 break;
5783 }
5784
5785 if (r == error_mark_node)
5786 *non_constant_p = true;
5787
5788 if (*non_constant_p)
5789 return t;
5790 else
5791 return r;
5792}
5793
e079dced
JM
5794/* P0859: A function is needed for constant evaluation if it is a constexpr
5795 function that is named by an expression ([basic.def.odr]) that is
5796 potentially constant evaluated.
5797
5798 So we need to instantiate any constexpr functions mentioned by the
5799 expression even if the definition isn't needed for evaluating the
5800 expression. */
5801
5802static tree
5803instantiate_cx_fn_r (tree *tp, int *walk_subtrees, void */*data*/)
5804{
5805 if (TREE_CODE (*tp) == FUNCTION_DECL
5806 && DECL_DECLARED_CONSTEXPR_P (*tp)
5807 && !DECL_INITIAL (*tp)
4fdda3ce 5808 && !trivial_fn_p (*tp)
e079dced
JM
5809 && DECL_TEMPLOID_INSTANTIATION (*tp))
5810 {
5811 ++function_depth;
5812 instantiate_decl (*tp, /*defer_ok*/false, /*expl_inst*/false);
5813 --function_depth;
5814 }
5815 else if (TREE_CODE (*tp) == CALL_EXPR
5816 || TREE_CODE (*tp) == AGGR_INIT_EXPR)
5817 {
5818 if (EXPR_HAS_LOCATION (*tp))
5819 input_location = EXPR_LOCATION (*tp);
5820 }
5821
5822 if (!EXPR_P (*tp))
5823 *walk_subtrees = 0;
5824
5825 return NULL_TREE;
5826}
8e007055 5827
e079dced
JM
5828static void
5829instantiate_constexpr_fns (tree t)
5830{
5831 location_t loc = input_location;
5832 cp_walk_tree_without_duplicates (&t, instantiate_cx_fn_r, NULL);
5833 input_location = loc;
5834}
5835
8e007055
JJ
5836/* Look for heap variables in the expression *TP. */
5837
5838static tree
5839find_heap_var_refs (tree *tp, int *walk_subtrees, void */*data*/)
5840{
5841 if (VAR_P (*tp)
5842 && (DECL_NAME (*tp) == heap_uninit_identifier
5843 || DECL_NAME (*tp) == heap_identifier
5844 || DECL_NAME (*tp) == heap_deleted_identifier))
5845 return *tp;
5846
5847 if (TYPE_P (*tp))
5848 *walk_subtrees = 0;
5849 return NULL_TREE;
5850}
5851
f968ef9b
JJ
5852/* Find immediate function decls in *TP if any. */
5853
5854static tree
5855find_immediate_fndecl (tree *tp, int */*walk_subtrees*/, void */*data*/)
5856{
5857 if (TREE_CODE (*tp) == FUNCTION_DECL && DECL_IMMEDIATE_FUNCTION_P (*tp))
5858 return *tp;
5859 return NULL_TREE;
5860}
5861
e4082611 5862/* ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
d4accef3
JM
5863 STRICT has the same sense as for constant_value_1: true if we only allow
5864 conforming C++ constant expressions, or false if we want a constant value
5865 even if it doesn't conform.
13de99bc 5866 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
8e007055
JJ
5867 per P0595 even when ALLOW_NON_CONSTANT is true.
5868 CONSTEXPR_DTOR is true when evaluating the dtor of a constexpr variable.
5869 OBJECT must be non-NULL in that case. */
e4082611 5870
2d76680f 5871static tree
3e605b20 5872cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant,
e4082611 5873 bool strict = true,
13de99bc 5874 bool manifestly_const_eval = false,
8e007055 5875 bool constexpr_dtor = false,
e4082611 5876 tree object = NULL_TREE)
2d76680f 5877{
8108ea30
JM
5878 auto_timevar time (TV_CONSTEXPR);
5879
2d76680f
PC
5880 bool non_constant_p = false;
5881 bool overflow_p = false;
39dce2b7 5882
8e007055
JJ
5883 constexpr_global_ctx global_ctx;
5884 constexpr_ctx ctx = { &global_ctx, NULL, NULL, NULL, NULL, NULL,
5885 allow_non_constant, strict,
13de99bc 5886 manifestly_const_eval || !allow_non_constant };
39dce2b7 5887
3e605b20 5888 tree type = initialized_type (t);
3e605b20 5889 tree r = t;
f968ef9b 5890 bool is_consteval = false;
4d0c18c6 5891 if (VOID_TYPE_P (type))
8e007055
JJ
5892 {
5893 if (constexpr_dtor)
5894 /* Used for destructors of array elements. */
5895 type = TREE_TYPE (object);
5896 else
f968ef9b
JJ
5897 {
5898 if (cxx_dialect < cxx2a)
5899 return t;
5900 if (TREE_CODE (t) != CALL_EXPR && TREE_CODE (t) != AGGR_INIT_EXPR)
5901 return t;
5902 /* Calls to immediate functions returning void need to be
5903 evaluated. */
5904 tree fndecl = cp_get_callee_fndecl_nofold (t);
5905 if (fndecl == NULL_TREE || !DECL_IMMEDIATE_FUNCTION_P (fndecl))
5906 return t;
5907 else
5908 is_consteval = true;
5909 }
5910 }
5911 else if (cxx_dialect >= cxx2a
5912 && (TREE_CODE (t) == CALL_EXPR
5913 || TREE_CODE (t) == AGGR_INIT_EXPR
5914 || TREE_CODE (t) == TARGET_EXPR))
5915 {
861d4af8
AS
5916 /* For non-concept checks, determine if it is consteval. */
5917 if (!concept_check_p (t))
5918 {
5919 tree x = t;
5920 if (TREE_CODE (x) == TARGET_EXPR)
5921 x = TARGET_EXPR_INITIAL (x);
5922 tree fndecl = cp_get_callee_fndecl_nofold (x);
5923 if (fndecl && DECL_IMMEDIATE_FUNCTION_P (fndecl))
5924 is_consteval = true;
5925 }
8e007055 5926 }
3e605b20
JM
5927 if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
5928 {
5929 /* In C++14 an NSDMI can participate in aggregate initialization,
5930 and can refer to the address of the object being initialized, so
5931 we need to pass in the relevant VAR_DECL if we want to do the
5932 evaluation in a single pass. The evaluation will dynamically
5933 update ctx.values for the VAR_DECL. We use the same strategy
5934 for C++11 constexpr constructors that refer to the object being
5935 initialized. */
8e007055
JJ
5936 if (constexpr_dtor)
5937 {
5938 gcc_assert (object && VAR_P (object));
5939 gcc_assert (DECL_DECLARED_CONSTEXPR_P (object));
5940 gcc_assert (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object));
fce6467b
JJ
5941 if (error_operand_p (DECL_INITIAL (object)))
5942 return t;
8e007055
JJ
5943 ctx.ctor = unshare_expr (DECL_INITIAL (object));
5944 TREE_READONLY (ctx.ctor) = false;
5945 /* Temporarily force decl_really_constant_value to return false
5946 for it, we want to use ctx.ctor for the current value instead. */
5947 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = false;
5948 }
5949 else
5950 {
5951 ctx.ctor = build_constructor (type, NULL);
5952 CONSTRUCTOR_NO_CLEARING (ctx.ctor) = true;
5953 }
60813a46
JM
5954 if (!object)
5955 {
5956 if (TREE_CODE (t) == TARGET_EXPR)
5957 object = TARGET_EXPR_SLOT (t);
5958 else if (TREE_CODE (t) == AGGR_INIT_EXPR)
5959 object = AGGR_INIT_EXPR_SLOT (t);
5960 }
3e605b20
JM
5961 ctx.object = object;
5962 if (object)
5963 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5964 (type, TREE_TYPE (object)));
5965 if (object && DECL_P (object))
8e007055 5966 global_ctx.values.put (object, ctx.ctor);
3e605b20
JM
5967 if (TREE_CODE (r) == TARGET_EXPR)
5968 /* Avoid creating another CONSTRUCTOR when we expand the
5969 TARGET_EXPR. */
5970 r = TARGET_EXPR_INITIAL (r);
5971 }
5972
ee1de08d
JJ
5973 auto_vec<tree, 16> cleanups;
5974 global_ctx.cleanups = &cleanups;
5975
e079dced 5976 instantiate_constexpr_fns (r);
2b3ab879 5977 r = cxx_eval_constant_expression (&ctx, r,
5a804683 5978 false, &non_constant_p, &overflow_p);
2d76680f 5979
8e007055
JJ
5980 if (!constexpr_dtor)
5981 verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p);
5982 else
5983 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = true;
2d76680f 5984
ee1de08d
JJ
5985 unsigned int i;
5986 tree cleanup;
5987 /* Evaluate the cleanups. */
5988 FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup)
5989 cxx_eval_constant_expression (&ctx, cleanup, false,
5990 &non_constant_p, &overflow_p);
5991
023d89c7
JM
5992 /* Mutable logic is a bit tricky: we want to allow initialization of
5993 constexpr variables with mutable members, but we can't copy those
5994 members to another constexpr variable. */
8e007055 5995 if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_MUTABLE_POISON (r))
2d76680f 5996 {
2d76680f 5997 if (!allow_non_constant)
023d89c7
JM
5998 error ("%qE is not a constant expression because it refers to "
5999 "mutable subobjects of %qT", t, type);
2d76680f
PC
6000 non_constant_p = true;
6001 }
6002
8e007055 6003 if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_NO_CLEARING (r))
f64e0c02
JM
6004 {
6005 if (!allow_non_constant)
6006 error ("%qE is not a constant expression because it refers to "
6007 "an incompletely initialized variable", t);
6008 TREE_CONSTANT (r) = false;
6009 non_constant_p = true;
6010 }
6011
8e007055
JJ
6012 if (!global_ctx.heap_vars.is_empty ())
6013 {
6014 tree heap_var = cp_walk_tree_without_duplicates (&r, find_heap_var_refs,
6015 NULL);
6016 unsigned int i;
6017 if (heap_var)
6018 {
6019 if (!allow_non_constant && !non_constant_p)
6020 error_at (DECL_SOURCE_LOCATION (heap_var),
6021 "%qE is not a constant expression because it refers to "
6022 "a result of %<operator new%>", t);
6023 r = t;
6024 non_constant_p = true;
6025 }
6026 FOR_EACH_VEC_ELT (global_ctx.heap_vars, i, heap_var)
6027 if (DECL_NAME (heap_var) != heap_deleted_identifier)
6028 {
6029 if (!allow_non_constant && !non_constant_p)
6030 error_at (DECL_SOURCE_LOCATION (heap_var),
6031 "%qE is not a constant expression because allocated "
6032 "storage has not been deallocated", t);
6033 r = t;
6034 non_constant_p = true;
6035 }
6036 }
6037
f968ef9b
JJ
6038 /* Check that immediate invocation does not return an expression referencing
6039 any immediate function decls. They need to be allowed while parsing
6040 immediate functions, but can't leak outside of them. */
6041 if (is_consteval
6042 && t != r
6043 && (current_function_decl == NULL_TREE
6044 || !DECL_IMMEDIATE_FUNCTION_P (current_function_decl)))
6045 if (tree immediate_fndecl
6046 = cp_walk_tree_without_duplicates (&r, find_immediate_fndecl,
6047 NULL))
6048 {
6049 if (!allow_non_constant && !non_constant_p)
6050 error_at (cp_expr_loc_or_input_loc (t),
6051 "immediate evaluation returns address of immediate "
6052 "function %qD", immediate_fndecl);
6053 r = t;
6054 non_constant_p = true;
6055 }
6056
2d76680f
PC
6057 /* Technically we should check this for all subexpressions, but that
6058 runs into problems with our internal representation of pointer
6059 subtraction and the 5.19 rules are still in flux. */
6060 if (CONVERT_EXPR_CODE_P (TREE_CODE (r))
6061 && ARITHMETIC_TYPE_P (TREE_TYPE (r))
6062 && TREE_CODE (TREE_OPERAND (r, 0)) == ADDR_EXPR)
6063 {
6064 if (!allow_non_constant)
6065 error ("conversion from pointer type %qT "
64d6d399 6066 "to arithmetic type %qT in a constant expression",
2d76680f
PC
6067 TREE_TYPE (TREE_OPERAND (r, 0)), TREE_TYPE (r));
6068 non_constant_p = true;
6069 }
6070
6071 if (!non_constant_p && overflow_p)
6072 non_constant_p = true;
6073
56cfb596
PP
6074 /* Unshare the result unless it's a CONSTRUCTOR in which case it's already
6075 unshared. */
6076 bool should_unshare = true;
6077 if (r == t || TREE_CODE (r) == CONSTRUCTOR)
6078 should_unshare = false;
6079
2d76680f
PC
6080 if (non_constant_p && !allow_non_constant)
6081 return error_mark_node;
8e007055
JJ
6082 else if (constexpr_dtor)
6083 return r;
2d76680f
PC
6084 else if (non_constant_p && TREE_CONSTANT (r))
6085 {
e4082611
JJ
6086 /* If __builtin_is_constant_evaluated () was evaluated to true
6087 and the result is not a valid constant expression, we need to
6088 punt. */
13de99bc 6089 if (manifestly_const_eval)
e4082611 6090 return cxx_eval_outermost_constant_expr (t, true, strict,
8e007055 6091 false, false, object);
eddd715c
JM
6092 /* This isn't actually constant, so unset TREE_CONSTANT.
6093 Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires
6094 it to be set if it is invariant address, even when it is not
6095 a valid C++ constant expression. Wrap it with a NOP_EXPR
6096 instead. */
6097 if (EXPR_P (r) && TREE_CODE (r) != ADDR_EXPR)
2d76680f
PC
6098 r = copy_node (r);
6099 else if (TREE_CODE (r) == CONSTRUCTOR)
6100 r = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (r), r);
6101 else
6102 r = build_nop (TREE_TYPE (r), r);
6103 TREE_CONSTANT (r) = false;
6104 }
5dab8b11 6105 else if (non_constant_p)
2d76680f
PC
6106 return t;
6107
56cfb596
PP
6108 if (should_unshare)
6109 r = unshare_expr (r);
6110
2d76680f
PC
6111 if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
6112 {
5dab8b11 6113 r = adjust_temp_type (type, r);
2d76680f
PC
6114 if (TREE_CODE (t) == TARGET_EXPR
6115 && TARGET_EXPR_INITIAL (t) == r)
6116 return t;
5dab8b11 6117 else if (TREE_CODE (t) != CONSTRUCTOR)
2d76680f 6118 {
8e007055 6119 r = get_target_expr_sfinae (r, tf_warning_or_error | tf_no_cleanup);
2d76680f 6120 TREE_CONSTANT (r) = true;
2d76680f
PC
6121 }
6122 }
5dab8b11
JM
6123
6124 return r;
2d76680f
PC
6125}
6126
2d76680f
PC
6127/* If T represents a constant expression returns its reduced value.
6128 Otherwise return error_mark_node. If T is dependent, then
6129 return NULL. */
6130
6131tree
3e605b20 6132cxx_constant_value (tree t, tree decl)
2d76680f 6133{
8e007055
JJ
6134 return cxx_eval_outermost_constant_expr (t, false, true, true, false, decl);
6135}
6136
6137/* Like cxx_constant_value, but used for evaluation of constexpr destructors
6138 of constexpr variables. The actual initializer of DECL is not modified. */
6139
6140void
6141cxx_constant_dtor (tree t, tree decl)
6142{
6143 cxx_eval_outermost_constant_expr (t, false, true, true, true, decl);
2d76680f
PC
6144}
6145
cda0a029
JM
6146/* Helper routine for fold_simple function. Either return simplified
6147 expression T, otherwise NULL_TREE.
6148 In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold
6149 even if we are within template-declaration. So be careful on call, as in
6150 such case types can be undefined. */
6151
6152static tree
6153fold_simple_1 (tree t)
6154{
6155 tree op1;
6156 enum tree_code code = TREE_CODE (t);
6157
6158 switch (code)
6159 {
6160 case INTEGER_CST:
6161 case REAL_CST:
6162 case VECTOR_CST:
6163 case FIXED_CST:
6164 case COMPLEX_CST:
6165 return t;
6166
6167 case SIZEOF_EXPR:
6168 return fold_sizeof_expr (t);
6169
6170 case ABS_EXPR:
e28fadbc 6171 case ABSU_EXPR:
cda0a029
JM
6172 case CONJ_EXPR:
6173 case REALPART_EXPR:
6174 case IMAGPART_EXPR:
6175 case NEGATE_EXPR:
6176 case BIT_NOT_EXPR:
6177 case TRUTH_NOT_EXPR:
6178 case NOP_EXPR:
6179 case VIEW_CONVERT_EXPR:
6180 case CONVERT_EXPR:
6181 case FLOAT_EXPR:
6182 case FIX_TRUNC_EXPR:
6183 case FIXED_CONVERT_EXPR:
6184 case ADDR_SPACE_CONVERT_EXPR:
6185
6186 op1 = TREE_OPERAND (t, 0);
6187
6188 t = const_unop (code, TREE_TYPE (t), op1);
6189 if (!t)
6190 return NULL_TREE;
6191
6192 if (CONVERT_EXPR_CODE_P (code)
6193 && TREE_OVERFLOW_P (t) && !TREE_OVERFLOW_P (op1))
6194 TREE_OVERFLOW (t) = false;
6195 return t;
6196
6197 default:
6198 return NULL_TREE;
6199 }
6200}
6201
6202/* If T is a simple constant expression, returns its simplified value.
dba99244 6203 Otherwise returns T. In contrast to maybe_constant_value we
cda0a029
JM
6204 simplify only few operations on constant-expressions, and we don't
6205 try to simplify constexpressions. */
6206
6207tree
6208fold_simple (tree t)
6209{
cda0a029
JM
6210 if (processing_template_decl)
6211 return t;
6212
dba99244
MP
6213 tree r = fold_simple_1 (t);
6214 if (r)
6215 return r;
cda0a029 6216
dba99244 6217 return t;
cda0a029
JM
6218}
6219
2d76680f
PC
6220/* If T is a constant expression, returns its reduced value.
6221 Otherwise, if T does not have TREE_CONSTANT set, returns T.
13de99bc
JJ
6222 Otherwise, returns a version of T without TREE_CONSTANT.
6223 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated
6224 as per P0595. */
2d76680f 6225
8a87daca
JM
6226static GTY((deletable)) hash_map<tree, tree> *cv_cache;
6227
6228tree
13de99bc 6229maybe_constant_value (tree t, tree decl, bool manifestly_const_eval)
2d76680f
PC
6230{
6231 tree r;
6232
a0ab7ccd 6233 if (!is_nondependent_constant_expression (t))
2d76680f
PC
6234 {
6235 if (TREE_OVERFLOW_P (t))
6236 {
6237 t = build_nop (TREE_TYPE (t), t);
6238 TREE_CONSTANT (t) = false;
6239 }
6240 return t;
6241 }
8a87daca
JM
6242 else if (CONSTANT_CLASS_P (t))
6243 /* No caching or evaluation needed. */
6244 return t;
6245
13de99bc 6246 if (manifestly_const_eval)
8e007055 6247 return cxx_eval_outermost_constant_expr (t, true, true, true, false, decl);
13de99bc 6248
8a87daca
JM
6249 if (cv_cache == NULL)
6250 cv_cache = hash_map<tree, tree>::create_ggc (101);
6251 if (tree *cached = cv_cache->get (t))
6252 return *cached;
2d76680f 6253
8e007055 6254 r = cxx_eval_outermost_constant_expr (t, true, true, false, false, decl);
595278be
MM
6255 gcc_checking_assert (r == t
6256 || CONVERT_EXPR_P (t)
6257 || TREE_CODE (t) == VIEW_CONVERT_EXPR
6258 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
6259 || !cp_tree_equal (r, t));
8a87daca 6260 cv_cache->put (t, r);
2d76680f
PC
6261 return r;
6262}
6263
7a7ac32a
PP
6264/* Dispose of the whole CV_CACHE. */
6265
6266static void
6267clear_cv_cache (void)
6268{
6269 if (cv_cache != NULL)
6270 cv_cache->empty ();
6271}
6272
cb57504a 6273/* Dispose of the whole CV_CACHE, FOLD_CACHE, and satisfaction caches. */
1e297006
MP
6274
6275void
cb57504a 6276clear_cv_and_fold_caches (bool sat /*= true*/)
1e297006 6277{
7a7ac32a 6278 clear_cv_cache ();
eba9e839 6279 clear_fold_cache ();
cb57504a
JM
6280 if (sat)
6281 clear_satisfaction_cache ();
1e297006
MP
6282}
6283
a81c8e8c
MP
6284/* Internal function handling expressions in templates for
6285 fold_non_dependent_expr and fold_non_dependent_init.
6286
6287 If we're in a template, but T isn't value dependent, simplify
6288 it. We're supposed to treat:
6289
6290 template <typename T> void f(T[1 + 1]);
6291 template <typename T> void f(T[2]);
6292
6293 as two declarations of the same function, for example. */
6294
6295static tree
6296fold_non_dependent_expr_template (tree t, tsubst_flags_t complain,
f968ef9b
JJ
6297 bool manifestly_const_eval,
6298 tree object)
a81c8e8c
MP
6299{
6300 gcc_assert (processing_template_decl);
6301
6302 if (is_nondependent_constant_expression (t))
6303 {
6304 processing_template_decl_sentinel s;
6305 t = instantiate_non_dependent_expr_internal (t, complain);
6306
6307 if (type_unknown_p (t) || BRACE_ENCLOSED_INITIALIZER_P (t))
6308 {
6309 if (TREE_OVERFLOW_P (t))
6310 {
6311 t = build_nop (TREE_TYPE (t), t);
6312 TREE_CONSTANT (t) = false;
6313 }
6314 return t;
6315 }
6316
6317 tree r = cxx_eval_outermost_constant_expr (t, true, true,
6318 manifestly_const_eval,
f968ef9b 6319 false, object);
a81c8e8c
MP
6320 /* cp_tree_equal looks through NOPs, so allow them. */
6321 gcc_checking_assert (r == t
6322 || CONVERT_EXPR_P (t)
6323 || TREE_CODE (t) == VIEW_CONVERT_EXPR
6324 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
6325 || !cp_tree_equal (r, t));
6326 return r;
6327 }
6328 else if (TREE_OVERFLOW_P (t))
6329 {
6330 t = build_nop (TREE_TYPE (t), t);
6331 TREE_CONSTANT (t) = false;
6332 }
6333
6334 return t;
6335}
6336
234bef96
PC
6337/* Like maybe_constant_value but first fully instantiate the argument.
6338
6339 Note: this is equivalent to instantiate_non_dependent_expr_sfinae
e56f6629
JM
6340 (t, complain) followed by maybe_constant_value but is more efficient,
6341 because it calls instantiation_dependent_expression_p and
6342 potential_constant_expression at most once.
4cd3e7df 6343 The manifestly_const_eval argument is passed to maybe_constant_value.
e56f6629
JM
6344
6345 Callers should generally pass their active complain, or if they are in a
6346 non-template, diagnosing context, they can use the default of
6347 tf_warning_or_error. Callers that might be within a template context, don't
6348 have a complain parameter, and aren't going to remember the result for long
6349 (e.g. null_ptr_cst_p), can pass tf_none and deal with error_mark_node
6350 appropriately. */
234bef96
PC
6351
6352tree
e56f6629 6353fold_non_dependent_expr (tree t,
4cd3e7df 6354 tsubst_flags_t complain /* = tf_warning_or_error */,
f968ef9b
JJ
6355 bool manifestly_const_eval /* = false */,
6356 tree object /* = NULL_TREE */)
234bef96
PC
6357{
6358 if (t == NULL_TREE)
6359 return NULL_TREE;
6360
a81c8e8c
MP
6361 if (processing_template_decl)
6362 return fold_non_dependent_expr_template (t, complain,
f968ef9b 6363 manifestly_const_eval, object);
234bef96 6364
f968ef9b 6365 return maybe_constant_value (t, object, manifestly_const_eval);
a81c8e8c 6366}
234bef96 6367
234bef96 6368
a81c8e8c 6369/* Like maybe_constant_init but first fully instantiate the argument. */
234bef96 6370
a81c8e8c
MP
6371tree
6372fold_non_dependent_init (tree t,
6373 tsubst_flags_t complain /*=tf_warning_or_error*/,
6374 bool manifestly_const_eval /*=false*/)
6375{
6376 if (t == NULL_TREE)
6377 return NULL_TREE;
6378
6379 if (processing_template_decl)
6380 {
6381 t = fold_non_dependent_expr_template (t, complain,
f968ef9b 6382 manifestly_const_eval, NULL_TREE);
a81c8e8c
MP
6383 /* maybe_constant_init does this stripping, so do it here too. */
6384 if (TREE_CODE (t) == TARGET_EXPR)
234bef96 6385 {
a81c8e8c
MP
6386 tree init = TARGET_EXPR_INITIAL (t);
6387 if (TREE_CODE (init) == CONSTRUCTOR)
6388 t = init;
234bef96
PC
6389 }
6390 return t;
6391 }
6392
a81c8e8c 6393 return maybe_constant_init (t, NULL_TREE, manifestly_const_eval);
234bef96
PC
6394}
6395
2d76680f 6396/* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
e4082611
JJ
6397 than wrapped in a TARGET_EXPR.
6398 ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
13de99bc 6399 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
e4082611 6400 per P0595 even when ALLOW_NON_CONSTANT is true. */
2d76680f 6401
118cd6ba 6402static tree
e4082611 6403maybe_constant_init_1 (tree t, tree decl, bool allow_non_constant,
13de99bc 6404 bool manifestly_const_eval)
2d76680f 6405{
cda0a029
JM
6406 if (!t)
6407 return t;
2d76680f
PC
6408 if (TREE_CODE (t) == EXPR_STMT)
6409 t = TREE_OPERAND (t, 0);
6410 if (TREE_CODE (t) == CONVERT_EXPR
6411 && VOID_TYPE_P (TREE_TYPE (t)))
6412 t = TREE_OPERAND (t, 0);
60813a46
JM
6413 if (TREE_CODE (t) == INIT_EXPR)
6414 t = TREE_OPERAND (t, 1);
f64e0c02
JM
6415 if (TREE_CODE (t) == TARGET_EXPR)
6416 t = TARGET_EXPR_INITIAL (t);
a0ab7ccd 6417 if (!is_nondependent_static_init_expression (t))
69eb4fde 6418 /* Don't try to evaluate it. */;
118cd6ba 6419 else if (CONSTANT_CLASS_P (t) && allow_non_constant)
8a87daca 6420 /* No evaluation needed. */;
69eb4fde 6421 else
e4082611 6422 t = cxx_eval_outermost_constant_expr (t, allow_non_constant,
38295573 6423 /*strict*/false,
8e007055 6424 manifestly_const_eval, false, decl);
2d76680f
PC
6425 if (TREE_CODE (t) == TARGET_EXPR)
6426 {
6427 tree init = TARGET_EXPR_INITIAL (t);
6428 if (TREE_CODE (init) == CONSTRUCTOR)
6429 t = init;
6430 }
6431 return t;
6432}
6433
118cd6ba
MP
6434/* Wrapper for maybe_constant_init_1 which permits non constants. */
6435
6436tree
13de99bc 6437maybe_constant_init (tree t, tree decl, bool manifestly_const_eval)
118cd6ba 6438{
13de99bc 6439 return maybe_constant_init_1 (t, decl, true, manifestly_const_eval);
118cd6ba
MP
6440}
6441
6442/* Wrapper for maybe_constant_init_1 which does not permit non constants. */
6443
6444tree
6445cxx_constant_init (tree t, tree decl)
6446{
e4082611 6447 return maybe_constant_init_1 (t, decl, false, true);
118cd6ba
MP
6448}
6449
2d76680f
PC
6450#if 0
6451/* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
6452/* Return true if the object referred to by REF has automatic or thread
6453 local storage. */
6454
6455enum { ck_ok, ck_bad, ck_unknown };
6456static int
6457check_automatic_or_tls (tree ref)
6458{
ef4bddc2 6459 machine_mode mode;
f37fac2b 6460 poly_int64 bitsize, bitpos;
2d76680f
PC
6461 tree offset;
6462 int volatilep = 0, unsignedp = 0;
6463 tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset,
6464 &mode, &unsignedp, &volatilep, false);
6465 duration_kind dk;
6466
6467 /* If there isn't a decl in the middle, we don't know the linkage here,
6468 and this isn't a constant expression anyway. */
6469 if (!DECL_P (decl))
6470 return ck_unknown;
6471 dk = decl_storage_duration (decl);
6472 return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok;
6473}
6474#endif
6475
c7a53bdb
JJ
6476/* Data structure for passing data from potential_constant_expression_1
6477 to check_for_return_continue via cp_walk_tree. */
6478struct check_for_return_continue_data {
6479 hash_set<tree> *pset;
6480 tree continue_stmt;
6481};
6482
6483/* Helper function for potential_constant_expression_1 SWITCH_STMT handling,
6484 called through cp_walk_tree. Return the first RETURN_EXPR found, or note
6485 the first CONTINUE_STMT if RETURN_EXPR is not found. */
6486static tree
6487check_for_return_continue (tree *tp, int *walk_subtrees, void *data)
6488{
6489 tree t = *tp, s;
6490 check_for_return_continue_data *d = (check_for_return_continue_data *) data;
6491 switch (TREE_CODE (t))
6492 {
6493 case RETURN_EXPR:
6494 return t;
6495
6496 case CONTINUE_STMT:
6497 if (d->continue_stmt == NULL_TREE)
6498 d->continue_stmt = t;
6499 break;
6500
6501#define RECUR(x) \
6502 if (tree r = cp_walk_tree (&x, check_for_return_continue, data, \
6503 d->pset)) \
6504 return r
6505
6506 /* For loops, walk subtrees manually, so that continue stmts found
6507 inside of the bodies of the loops are ignored. */
6508 case DO_STMT:
6509 *walk_subtrees = 0;
6510 RECUR (DO_COND (t));
6511 s = d->continue_stmt;
6512 RECUR (DO_BODY (t));
6513 d->continue_stmt = s;
6514 break;
6515
6516 case WHILE_STMT:
6517 *walk_subtrees = 0;
6518 RECUR (WHILE_COND (t));
6519 s = d->continue_stmt;
6520 RECUR (WHILE_BODY (t));
6521 d->continue_stmt = s;
6522 break;
6523
6524 case FOR_STMT:
6525 *walk_subtrees = 0;
6526 RECUR (FOR_INIT_STMT (t));
6527 RECUR (FOR_COND (t));
6528 RECUR (FOR_EXPR (t));
6529 s = d->continue_stmt;
6530 RECUR (FOR_BODY (t));
6531 d->continue_stmt = s;
6532 break;
6533
6534 case RANGE_FOR_STMT:
6535 *walk_subtrees = 0;
6536 RECUR (RANGE_FOR_EXPR (t));
6537 s = d->continue_stmt;
6538 RECUR (RANGE_FOR_BODY (t));
6539 d->continue_stmt = s;
6540 break;
6541#undef RECUR
6542
6543 case STATEMENT_LIST:
6544 case CONSTRUCTOR:
6545 break;
6546
6547 default:
6548 if (!EXPR_P (t))
6549 *walk_subtrees = 0;
6550 break;
6551 }
6552
6553 return NULL_TREE;
6554}
6555
2d76680f
PC
6556/* Return true if T denotes a potentially constant expression. Issue
6557 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
a0ab7ccd
JM
6558 an lvalue-rvalue conversion is implied. If NOW is true, we want to
6559 consider the expression in the current context, independent of constexpr
6560 substitution.
2d76680f
PC
6561
6562 C++0x [expr.const] used to say
6563
6564 6 An expression is a potential constant expression if it is
6565 a constant expression where all occurrences of function
6566 parameters are replaced by arbitrary constant expressions
6567 of the appropriate type.
6568
6569 2 A conditional expression is a constant expression unless it
6570 involves one of the following as a potentially evaluated
6571 subexpression (3.2), but subexpressions of logical AND (5.14),
6572 logical OR (5.15), and conditional (5.16) operations that are
6573 not evaluated are not considered. */
6574
6575static bool
a0ab7ccd 6576potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
3075affd 6577 tsubst_flags_t flags, tree *jump_target)
2d76680f 6578{
a0ab7ccd 6579#define RECUR(T,RV) \
3075affd 6580 potential_constant_expression_1 ((T), (RV), strict, now, flags, jump_target)
a0ab7ccd 6581
2d76680f
PC
6582 enum { any = false, rval = true };
6583 int i;
6584 tree tmp;
6585
6586 if (t == error_mark_node)
6587 return false;
6588 if (t == NULL_TREE)
6589 return true;
f9d0ca40 6590 location_t loc = cp_expr_loc_or_input_loc (t);
3075affd
JM
6591
6592 if (*jump_target)
6593 /* If we are jumping, ignore everything. This is simpler than the
6594 cxx_eval_constant_expression handling because we only need to be
6595 conservatively correct, and we don't necessarily have a constant value
6596 available, so we don't bother with switch tracking. */
6597 return true;
6598
3c393a2c 6599 if (TREE_THIS_VOLATILE (t) && want_rval)
2d76680f
PC
6600 {
6601 if (flags & tf_error)
3c393a2c
MP
6602 error_at (loc, "lvalue-to-rvalue conversion of a volatile lvalue "
6603 "%qE with type %qT", t, TREE_TYPE (t));
2d76680f
PC
6604 return false;
6605 }
6606 if (CONSTANT_CLASS_P (t))
6607 return true;
7dc2b4a2
JM
6608 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED)
6609 && TREE_TYPE (t) == error_mark_node)
6610 return false;
2d76680f
PC
6611
6612 switch (TREE_CODE (t))
6613 {
6614 case FUNCTION_DECL:
6615 case BASELINK:
6616 case TEMPLATE_DECL:
6617 case OVERLOAD:
6618 case TEMPLATE_ID_EXPR:
6619 case LABEL_DECL:
6620 case LABEL_EXPR:
60813a46 6621 case CASE_LABEL_EXPR:
81012684 6622 case PREDICT_EXPR:
2d76680f
PC
6623 case CONST_DECL:
6624 case SIZEOF_EXPR:
6625 case ALIGNOF_EXPR:
6626 case OFFSETOF_EXPR:
6627 case NOEXCEPT_EXPR:
6628 case TEMPLATE_PARM_INDEX:
6629 case TRAIT_EXPR:
6630 case IDENTIFIER_NODE:
6631 case USERDEF_LITERAL:
6632 /* We can see a FIELD_DECL in a pointer-to-member expression. */
6633 case FIELD_DECL:
cda0a029 6634 case RESULT_DECL:
2d76680f 6635 case USING_DECL:
60813a46 6636 case USING_STMT:
3e605b20 6637 case PLACEHOLDER_EXPR:
971e17ff 6638 case REQUIRES_EXPR:
98e5a19a 6639 case STATIC_ASSERT:
96a95ac1 6640 case DEBUG_BEGIN_STMT:
2d76680f
PC
6641 return true;
6642
3075affd
JM
6643 case RETURN_EXPR:
6644 if (!RECUR (TREE_OPERAND (t, 0), any))
6645 return false;
6646 /* FALLTHROUGH */
6647
6648 case BREAK_STMT:
6649 case CONTINUE_STMT:
6650 *jump_target = t;
6651 return true;
6652
a0ab7ccd
JM
6653 case PARM_DECL:
6654 if (now)
6655 {
6656 if (flags & tf_error)
6657 error ("%qE is not a constant expression", t);
6658 return false;
6659 }
6660 return true;
6661
2d76680f
PC
6662 case AGGR_INIT_EXPR:
6663 case CALL_EXPR:
6664 /* -- an invocation of a function other than a constexpr function
6665 or a constexpr constructor. */
6666 {
6667 tree fun = get_function_named_in_call (t);
6668 const int nargs = call_expr_nargs (t);
6669 i = 0;
6670
60813a46
JM
6671 if (fun == NULL_TREE)
6672 {
44a845ca
MS
6673 /* Reset to allow the function to continue past the end
6674 of the block below. Otherwise return early. */
6675 bool bail = true;
6676
35228ac7
JJ
6677 if (TREE_CODE (t) == CALL_EXPR
6678 && CALL_EXPR_FN (t) == NULL_TREE)
6679 switch (CALL_EXPR_IFN (t))
6680 {
6681 /* These should be ignored, they are optimized away from
6682 constexpr functions. */
6683 case IFN_UBSAN_NULL:
6684 case IFN_UBSAN_BOUNDS:
6685 case IFN_UBSAN_VPTR:
81fea426 6686 case IFN_FALLTHROUGH:
35228ac7 6687 return true;
44a845ca
MS
6688
6689 case IFN_ADD_OVERFLOW:
6690 case IFN_SUB_OVERFLOW:
6691 case IFN_MUL_OVERFLOW:
e16f1cc7 6692 case IFN_LAUNDER:
d8fcab68 6693 case IFN_VEC_CONVERT:
44a845ca 6694 bail = false;
d8fcab68 6695 break;
44a845ca 6696
35228ac7
JJ
6697 default:
6698 break;
6699 }
44a845ca
MS
6700
6701 if (bail)
6702 {
6703 /* fold_call_expr can't do anything with IFN calls. */
6704 if (flags & tf_error)
e40b6fc7 6705 error_at (loc, "call to internal function %qE", t);
44a845ca
MS
6706 return false;
6707 }
60813a46 6708 }
44a845ca
MS
6709
6710 if (fun && is_overloaded_fn (fun))
2d76680f
PC
6711 {
6712 if (TREE_CODE (fun) == FUNCTION_DECL)
6713 {
6714 if (builtin_valid_in_constant_expr_p (fun))
6715 return true;
6716 if (!DECL_DECLARED_CONSTEXPR_P (fun)
6717 /* Allow any built-in function; if the expansion
6718 isn't constant, we'll deal with that then. */
8e007055
JJ
6719 && !fndecl_built_in_p (fun)
6720 /* In C++2a, replaceable global allocation functions
6721 are constant expressions. */
8412b939
JJ
6722 && (!cxx_replaceable_global_alloc_fn (fun)
6723 || TREE_CODE (t) != CALL_EXPR
6724 || (!CALL_FROM_NEW_OR_DELETE_P (t)
6725 && (current_function_decl == NULL_TREE
6726 || !is_std_allocator_allocate
6727 (current_function_decl))))
cf650568
JJ
6728 /* Allow placement new in std::construct_at. */
6729 && (!cxx_placement_new_fn (fun)
8412b939 6730 || TREE_CODE (t) != CALL_EXPR
cf650568
JJ
6731 || current_function_decl == NULL_TREE
6732 || !is_std_construct_at (current_function_decl)))
2d76680f
PC
6733 {
6734 if (flags & tf_error)
6735 {
84fa214d 6736 error_at (loc, "call to non-%<constexpr%> function %qD",
e40b6fc7 6737 fun);
2d76680f
PC
6738 explain_invalid_constexpr_fn (fun);
6739 }
6740 return false;
6741 }
6742 /* A call to a non-static member function takes the address
6743 of the object as the first argument. But in a constant
6744 expression the address will be folded away, so look
6745 through it now. */
6746 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
6747 && !DECL_CONSTRUCTOR_P (fun))
6748 {
6749 tree x = get_nth_callarg (t, 0);
6750 if (is_this_parameter (x))
3e605b20 6751 return true;
a0ab7ccd
JM
6752 /* Don't require an immediately constant value, as
6753 constexpr substitution might not use the value. */
6754 bool sub_now = false;
6755 if (!potential_constant_expression_1 (x, rval, strict,
3075affd
JM
6756 sub_now, flags,
6757 jump_target))
2d76680f
PC
6758 return false;
6759 i = 1;
6760 }
6761 }
6762 else
6763 {
69eb4fde 6764 if (!RECUR (fun, true))
2d76680f
PC
6765 return false;
6766 fun = get_first_fn (fun);
6767 }
6768 /* Skip initial arguments to base constructors. */
6769 if (DECL_BASE_CONSTRUCTOR_P (fun))
6770 i = num_artificial_parms_for (fun);
6771 fun = DECL_ORIGIN (fun);
6772 }
44a845ca 6773 else if (fun)
2d76680f 6774 {
69eb4fde 6775 if (RECUR (fun, rval))
2d76680f
PC
6776 /* Might end up being a constant function pointer. */;
6777 else
6778 return false;
6779 }
6780 for (; i < nargs; ++i)
6781 {
6782 tree x = get_nth_callarg (t, i);
c3c29ba5
JM
6783 /* In a template, reference arguments haven't been converted to
6784 REFERENCE_TYPE and we might not even know if the parameter
6785 is a reference, so accept lvalue constants too. */
6786 bool rv = processing_template_decl ? any : rval;
a0ab7ccd
JM
6787 /* Don't require an immediately constant value, as constexpr
6788 substitution might not use the value of the argument. */
6789 bool sub_now = false;
6790 if (!potential_constant_expression_1 (x, rv, strict,
3075affd 6791 sub_now, flags, jump_target))
2d76680f
PC
6792 return false;
6793 }
6794 return true;
6795 }
6796
6797 case NON_LVALUE_EXPR:
6798 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
6799 -- an lvalue of integral type that refers to a non-volatile
6800 const variable or static data member initialized with
6801 constant expressions, or
6802
6803 -- an lvalue of literal type that refers to non-volatile
6804 object defined with constexpr, or that refers to a
6805 sub-object of such an object; */
69eb4fde 6806 return RECUR (TREE_OPERAND (t, 0), rval);
2d76680f
PC
6807
6808 case VAR_DECL:
70f40fea 6809 if (DECL_HAS_VALUE_EXPR_P (t))
c1051bf7 6810 {
68ad1bf7 6811 if (now && is_normal_capture_proxy (t))
c1051bf7
JM
6812 {
6813 /* -- in a lambda-expression, a reference to this or to a
6814 variable with automatic storage duration defined outside that
6815 lambda-expression, where the reference would be an
6816 odr-use. */
6817 if (flags & tf_error)
6818 {
6819 tree cap = DECL_CAPTURED_VARIABLE (t);
6820 error ("lambda capture of %qE is not a constant expression",
6821 cap);
6822 if (!want_rval && decl_constant_var_p (cap))
6823 inform (input_location, "because it is used as a glvalue");
6824 }
6825 return false;
6826 }
6827 return RECUR (DECL_VALUE_EXPR (t), rval);
6828 }
69eb4fde 6829 if (want_rval
98e5a19a 6830 && !var_in_maybe_constexpr_fn (t)
7dc2b4a2 6831 && !type_dependent_expression_p (t)
4b691b13 6832 && !decl_maybe_constant_var_p (t)
69eb4fde
JM
6833 && (strict
6834 || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t))
4b691b13
JM
6835 || (DECL_INITIAL (t)
6836 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t)))
7dc2b4a2 6837 && COMPLETE_TYPE_P (TREE_TYPE (t))
dbcd32f8 6838 && !is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
2d76680f
PC
6839 {
6840 if (flags & tf_error)
6821245b 6841 non_const_var_error (loc, t);
2d76680f
PC
6842 return false;
6843 }
6844 return true;
6845
6846 case NOP_EXPR:
ed3ea9f2
JJ
6847 if (REINTERPRET_CAST_P (t))
6848 {
6849 if (flags & tf_error)
a9c697b8 6850 error_at (loc, "%<reinterpret_cast%> is not a constant expression");
ed3ea9f2
JJ
6851 return false;
6852 }
6853 /* FALLTHRU */
2d76680f
PC
6854 case CONVERT_EXPR:
6855 case VIEW_CONVERT_EXPR:
6856 /* -- a reinterpret_cast. FIXME not implemented, and this rule
6857 may change to something more specific to type-punning (DR 1312). */
6858 {
6859 tree from = TREE_OPERAND (t, 0);
dfd7fdca
DM
6860 if (location_wrapper_p (t))
6861 return (RECUR (from, want_rval));
6862 if (INDIRECT_TYPE_P (TREE_TYPE (t)))
2d76680f 6863 {
dfd7fdca
DM
6864 STRIP_ANY_LOCATION_WRAPPER (from);
6865 if (TREE_CODE (from) == INTEGER_CST
6866 && !integer_zerop (from))
6867 {
6868 if (flags & tf_error)
a9c697b8
MS
6869 error_at (loc,
6870 "%<reinterpret_cast%> from integer to pointer");
dfd7fdca
DM
6871 return false;
6872 }
2d76680f 6873 }
69eb4fde 6874 return (RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR));
2d76680f
PC
6875 }
6876
be845b04
JJ
6877 case ADDRESSOF_EXPR:
6878 /* This is like ADDR_EXPR, except it won't form pointer-to-member. */
6879 t = TREE_OPERAND (t, 0);
6880 goto handle_addr_expr;
6881
2d76680f
PC
6882 case ADDR_EXPR:
6883 /* -- a unary operator & that is applied to an lvalue that
6884 designates an object with thread or automatic storage
6885 duration; */
6886 t = TREE_OPERAND (t, 0);
6887
6888 if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t))
6889 /* A pointer-to-member constant. */
6890 return true;
6891
be845b04 6892 handle_addr_expr:
2d76680f
PC
6893#if 0
6894 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
6895 any checking here, as we might dereference the pointer later. If
6896 we remove this code, also remove check_automatic_or_tls. */
6897 i = check_automatic_or_tls (t);
6898 if (i == ck_ok)
6899 return true;
6900 if (i == ck_bad)
6901 {
6902 if (flags & tf_error)
6903 error ("address-of an object %qE with thread local or "
6904 "automatic storage is not a constant expression", t);
6905 return false;
6906 }
6907#endif
69eb4fde 6908 return RECUR (t, any);
2d76680f 6909
dc8d2d00
JM
6910 case REALPART_EXPR:
6911 case IMAGPART_EXPR:
2d76680f
PC
6912 case COMPONENT_REF:
6913 case BIT_FIELD_REF:
6914 case ARROW_EXPR:
6915 case OFFSET_REF:
6916 /* -- a class member access unless its postfix-expression is
6917 of literal type or of pointer to literal type. */
6918 /* This test would be redundant, as it follows from the
6919 postfix-expression being a potential constant expression. */
19dedccf
JM
6920 if (type_unknown_p (t))
6921 return true;
69eb4fde 6922 return RECUR (TREE_OPERAND (t, 0), want_rval);
2d76680f
PC
6923
6924 case EXPR_PACK_EXPANSION:
69eb4fde 6925 return RECUR (PACK_EXPANSION_PATTERN (t), want_rval);
2d76680f
PC
6926
6927 case INDIRECT_REF:
6928 {
6929 tree x = TREE_OPERAND (t, 0);
6930 STRIP_NOPS (x);
948d0d91 6931 if (is_this_parameter (x) && !is_capture_proxy (x))
2d76680f 6932 {
5d4e573b 6933 if (!var_in_maybe_constexpr_fn (x))
2d76680f
PC
6934 {
6935 if (flags & tf_error)
e40b6fc7 6936 error_at (loc, "use of %<this%> in a constant expression");
2d76680f
PC
6937 return false;
6938 }
2d76680f
PC
6939 return true;
6940 }
69eb4fde 6941 return RECUR (x, rval);
2d76680f
PC
6942 }
6943
60813a46
JM
6944 case STATEMENT_LIST:
6945 {
6946 tree_stmt_iterator i;
6947 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
6948 {
69eb4fde 6949 if (!RECUR (tsi_stmt (i), any))
60813a46
JM
6950 return false;
6951 }
6952 return true;
6953 }
6954 break;
6955
6956 case MODIFY_EXPR:
6957 if (cxx_dialect < cxx14)
6958 goto fail;
69eb4fde 6959 if (!RECUR (TREE_OPERAND (t, 0), any))
60813a46 6960 return false;
8e007055
JJ
6961 /* Just ignore clobbers. */
6962 if (TREE_CLOBBER_P (TREE_OPERAND (t, 1)))
6963 return true;
69eb4fde 6964 if (!RECUR (TREE_OPERAND (t, 1), rval))
60813a46
JM
6965 return false;
6966 return true;
6967
6968 case MODOP_EXPR:
6969 if (cxx_dialect < cxx14)
6970 goto fail;
69eb4fde 6971 if (!RECUR (TREE_OPERAND (t, 0), rval))
60813a46 6972 return false;
69eb4fde 6973 if (!RECUR (TREE_OPERAND (t, 2), rval))
60813a46
JM
6974 return false;
6975 return true;
6976
60813a46 6977 case DO_STMT:
69eb4fde 6978 if (!RECUR (DO_COND (t), rval))
60813a46 6979 return false;
69eb4fde 6980 if (!RECUR (DO_BODY (t), any))
60813a46 6981 return false;
3075affd
JM
6982 if (breaks (jump_target) || continues (jump_target))
6983 *jump_target = NULL_TREE;
60813a46
JM
6984 return true;
6985
6986 case FOR_STMT:
69eb4fde 6987 if (!RECUR (FOR_INIT_STMT (t), any))
60813a46 6988 return false;
8e9558f0
MP
6989 tmp = FOR_COND (t);
6990 if (!RECUR (tmp, rval))
60813a46 6991 return false;
8e9558f0
MP
6992 if (tmp)
6993 {
6994 if (!processing_template_decl)
6995 tmp = cxx_eval_outermost_constant_expr (tmp, true);
a385474c
MP
6996 /* If we couldn't evaluate the condition, it might not ever be
6997 true. */
6998 if (!integer_onep (tmp))
8e9558f0
MP
6999 return true;
7000 }
69eb4fde 7001 if (!RECUR (FOR_EXPR (t), any))
60813a46 7002 return false;
69eb4fde 7003 if (!RECUR (FOR_BODY (t), any))
60813a46 7004 return false;
3075affd
JM
7005 if (breaks (jump_target) || continues (jump_target))
7006 *jump_target = NULL_TREE;
60813a46
JM
7007 return true;
7008
98e5a19a 7009 case RANGE_FOR_STMT:
8112667c
MP
7010 if (!RECUR (RANGE_FOR_INIT_STMT (t), any))
7011 return false;
98e5a19a
JM
7012 if (!RECUR (RANGE_FOR_EXPR (t), any))
7013 return false;
7014 if (!RECUR (RANGE_FOR_BODY (t), any))
7015 return false;
3075affd
JM
7016 if (breaks (jump_target) || continues (jump_target))
7017 *jump_target = NULL_TREE;
98e5a19a
JM
7018 return true;
7019
60813a46 7020 case WHILE_STMT:
8e9558f0
MP
7021 tmp = WHILE_COND (t);
7022 if (!RECUR (tmp, rval))
60813a46 7023 return false;
8e9558f0
MP
7024 if (!processing_template_decl)
7025 tmp = cxx_eval_outermost_constant_expr (tmp, true);
a385474c
MP
7026 /* If we couldn't evaluate the condition, it might not ever be true. */
7027 if (!integer_onep (tmp))
8e9558f0 7028 return true;
69eb4fde 7029 if (!RECUR (WHILE_BODY (t), any))
60813a46 7030 return false;
3075affd
JM
7031 if (breaks (jump_target) || continues (jump_target))
7032 *jump_target = NULL_TREE;
60813a46
JM
7033 return true;
7034
7035 case SWITCH_STMT:
69eb4fde 7036 if (!RECUR (SWITCH_STMT_COND (t), rval))
60813a46 7037 return false;
ce965730 7038 /* FIXME we don't check SWITCH_STMT_BODY currently, because even
c7a53bdb
JJ
7039 unreachable labels would be checked and it is enough if there is
7040 a single switch cond value for which it is a valid constant
7041 expression. We need to check if there are any RETURN_EXPRs
7042 or CONTINUE_STMTs inside of the body though, as in that case
7043 we need to set *jump_target. */
7044 else
7045 {
7046 hash_set<tree> pset;
7047 check_for_return_continue_data data = { &pset, NULL_TREE };
7048 if (tree ret_expr
7049 = cp_walk_tree (&SWITCH_STMT_BODY (t), check_for_return_continue,
7050 &data, &pset))
7051 /* The switch might return. */
7052 *jump_target = ret_expr;
7053 else if (data.continue_stmt)
7054 /* The switch can't return, but might continue. */
7055 *jump_target = data.continue_stmt;
7056 }
60813a46
JM
7057 return true;
7058
58611fb6 7059 case STMT_EXPR:
69eb4fde 7060 return RECUR (STMT_EXPR_STMT (t), rval);
58611fb6 7061
2d76680f 7062 case LAMBDA_EXPR:
899ac3b8
JM
7063 if (cxx_dialect >= cxx17)
7064 /* In C++17 lambdas can be constexpr, don't give up yet. */
7065 return true;
7066 else if (flags & tf_error)
7067 error_at (loc, "lambda-expression is not a constant expression "
7068 "before C++17");
7069 return false;
7070
2d76680f
PC
7071 case DYNAMIC_CAST_EXPR:
7072 case PSEUDO_DTOR_EXPR:
2d76680f
PC
7073 case NEW_EXPR:
7074 case VEC_NEW_EXPR:
7075 case DELETE_EXPR:
7076 case VEC_DELETE_EXPR:
7077 case THROW_EXPR:
e40b6fc7
JJ
7078 case OMP_PARALLEL:
7079 case OMP_TASK:
7080 case OMP_FOR:
897064e2 7081 case OMP_SIMD:
e40b6fc7
JJ
7082 case OMP_DISTRIBUTE:
7083 case OMP_TASKLOOP:
d81ab49d 7084 case OMP_LOOP:
e40b6fc7
JJ
7085 case OMP_TEAMS:
7086 case OMP_TARGET_DATA:
7087 case OMP_TARGET:
7088 case OMP_SECTIONS:
7089 case OMP_ORDERED:
7090 case OMP_CRITICAL:
7091 case OMP_SINGLE:
7092 case OMP_SECTION:
7093 case OMP_MASTER:
7094 case OMP_TASKGROUP:
7095 case OMP_TARGET_UPDATE:
7096 case OMP_TARGET_ENTER_DATA:
7097 case OMP_TARGET_EXIT_DATA:
2d76680f
PC
7098 case OMP_ATOMIC:
7099 case OMP_ATOMIC_READ:
7100 case OMP_ATOMIC_CAPTURE_OLD:
7101 case OMP_ATOMIC_CAPTURE_NEW:
28567c40 7102 case OMP_DEPOBJ:
e40b6fc7
JJ
7103 case OACC_PARALLEL:
7104 case OACC_KERNELS:
62aee289 7105 case OACC_SERIAL:
e40b6fc7
JJ
7106 case OACC_DATA:
7107 case OACC_HOST_DATA:
7108 case OACC_LOOP:
7109 case OACC_CACHE:
7110 case OACC_DECLARE:
7111 case OACC_ENTER_DATA:
7112 case OACC_EXIT_DATA:
7113 case OACC_UPDATE:
2d76680f
PC
7114 /* GCC internal stuff. */
7115 case VA_ARG_EXPR:
2d76680f 7116 case TRANSACTION_EXPR:
81b6a6c5 7117 case AT_ENCODE_EXPR:
60813a46 7118 fail:
2d76680f 7119 if (flags & tf_error)
e40b6fc7 7120 error_at (loc, "expression %qE is not a constant expression", t);
2d76680f
PC
7121 return false;
7122
529bc410 7123 case ASM_EXPR:
7c814975
MP
7124 if (flags & tf_error)
7125 inline_asm_in_constexpr_error (loc);
7126 return false;
529bc410 7127
bf8d8309
MP
7128 case OBJ_TYPE_REF:
7129 if (cxx_dialect >= cxx2a)
7130 /* In C++2a virtual calls can be constexpr, don't give up yet. */
7131 return true;
7132 else if (flags & tf_error)
a9c697b8
MS
7133 error_at (loc,
7134 "virtual functions cannot be %<constexpr%> before C++2a");
bf8d8309
MP
7135 return false;
7136
2d76680f 7137 case TYPEID_EXPR:
66acfb80
MP
7138 /* In C++20, a typeid expression whose operand is of polymorphic
7139 class type can be constexpr. */
2d76680f
PC
7140 {
7141 tree e = TREE_OPERAND (t, 0);
66acfb80
MP
7142 if (cxx_dialect < cxx2a
7143 && strict
7144 && !TYPE_P (e)
7145 && !type_dependent_expression_p (e)
2d76680f
PC
7146 && TYPE_POLYMORPHIC_P (TREE_TYPE (e)))
7147 {
7148 if (flags & tf_error)
a9c697b8 7149 error_at (loc, "%<typeid%> is not a constant expression "
e40b6fc7 7150 "because %qE is of polymorphic type", e);
2d76680f
PC
7151 return false;
7152 }
7153 return true;
7154 }
7155
1af4ebf5 7156 case POINTER_DIFF_EXPR:
2d76680f 7157 case MINUS_EXPR:
2d76680f
PC
7158 want_rval = true;
7159 goto binary;
7160
7161 case LT_EXPR:
7162 case LE_EXPR:
7163 case GT_EXPR:
7164 case GE_EXPR:
7165 case EQ_EXPR:
7166 case NE_EXPR:
b7689b96 7167 case SPACESHIP_EXPR:
2d76680f
PC
7168 want_rval = true;
7169 goto binary;
7170
60813a46
JM
7171 case PREINCREMENT_EXPR:
7172 case POSTINCREMENT_EXPR:
7173 case PREDECREMENT_EXPR:
7174 case POSTDECREMENT_EXPR:
7175 if (cxx_dialect < cxx14)
7176 goto fail;
7177 goto unary;
7178
2d76680f
PC
7179 case BIT_NOT_EXPR:
7180 /* A destructor. */
7181 if (TYPE_P (TREE_OPERAND (t, 0)))
7182 return true;
191816a3 7183 /* fall through. */
2d76680f 7184
2d76680f
PC
7185 case CONJ_EXPR:
7186 case SAVE_EXPR:
7187 case FIX_TRUNC_EXPR:
7188 case FLOAT_EXPR:
7189 case NEGATE_EXPR:
7190 case ABS_EXPR:
e197e64e 7191 case ABSU_EXPR:
2d76680f
PC
7192 case TRUTH_NOT_EXPR:
7193 case FIXED_CONVERT_EXPR:
7194 case UNARY_PLUS_EXPR:
4856a1f0
PC
7195 case UNARY_LEFT_FOLD_EXPR:
7196 case UNARY_RIGHT_FOLD_EXPR:
60813a46 7197 unary:
69eb4fde 7198 return RECUR (TREE_OPERAND (t, 0), rval);
2d76680f
PC
7199
7200 case CAST_EXPR:
7201 case CONST_CAST_EXPR:
7202 case STATIC_CAST_EXPR:
7203 case REINTERPRET_CAST_EXPR:
7204 case IMPLICIT_CONV_EXPR:
7205 if (cxx_dialect < cxx11
7206 && !dependent_type_p (TREE_TYPE (t))
7207 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t)))
7208 /* In C++98, a conversion to non-integral type can't be part of a
7209 constant expression. */
7210 {
7211 if (flags & tf_error)
e40b6fc7
JJ
7212 error_at (loc,
7213 "cast to non-integral type %qT in a constant expression",
7214 TREE_TYPE (t));
2d76680f
PC
7215 return false;
7216 }
fe0604d3
MP
7217 /* This might be a conversion from a class to a (potentially) literal
7218 type. Let's consider it potentially constant since the conversion
7219 might be a constexpr user-defined conversion. */
7220 else if (cxx_dialect >= cxx11
7221 && (dependent_type_p (TREE_TYPE (t))
7222 || !COMPLETE_TYPE_P (TREE_TYPE (t))
7223 || literal_type_p (TREE_TYPE (t)))
7224 && TREE_OPERAND (t, 0))
7225 {
7226 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
7227 /* If this is a dependent type, it could end up being a class
7228 with conversions. */
7229 if (type == NULL_TREE || WILDCARD_TYPE_P (type))
7230 return true;
7231 /* Or a non-dependent class which has conversions. */
7232 else if (CLASS_TYPE_P (type)
7233 && (TYPE_HAS_CONVERSION (type) || dependent_scope_p (type)))
7234 return true;
7235 }
2d76680f 7236
69eb4fde 7237 return (RECUR (TREE_OPERAND (t, 0),
9f613f06 7238 !TYPE_REF_P (TREE_TYPE (t))));
2d76680f 7239
60813a46 7240 case BIND_EXPR:
69eb4fde 7241 return RECUR (BIND_EXPR_BODY (t), want_rval);
60813a46 7242
60813a46
JM
7243 case CLEANUP_POINT_EXPR:
7244 case MUST_NOT_THROW_EXPR:
7245 case TRY_CATCH_EXPR:
769430b2 7246 case TRY_BLOCK:
60813a46
JM
7247 case EH_SPEC_BLOCK:
7248 case EXPR_STMT:
2d76680f
PC
7249 case PAREN_EXPR:
7250 case NON_DEPENDENT_EXPR:
7251 /* For convenience. */
f937929e
JM
7252 case LOOP_EXPR:
7253 case EXIT_EXPR:
69eb4fde 7254 return RECUR (TREE_OPERAND (t, 0), want_rval);
2d76680f 7255
98e5a19a
JM
7256 case DECL_EXPR:
7257 tmp = DECL_EXPR_DECL (t);
7258 if (VAR_P (tmp) && !DECL_ARTIFICIAL (tmp))
7259 {
7260 if (TREE_STATIC (tmp))
7261 {
7262 if (flags & tf_error)
7263 error_at (DECL_SOURCE_LOCATION (tmp), "%qD declared "
4b691b13 7264 "%<static%> in %<constexpr%> context", tmp);
98e5a19a
JM
7265 return false;
7266 }
7267 else if (CP_DECL_THREAD_LOCAL_P (tmp))
7268 {
7269 if (flags & tf_error)
7270 error_at (DECL_SOURCE_LOCATION (tmp), "%qD declared "
4b691b13 7271 "%<thread_local%> in %<constexpr%> context", tmp);
98e5a19a
JM
7272 return false;
7273 }
3885527d
PC
7274 else if (!check_for_uninitialized_const_var
7275 (tmp, /*constexpr_context_p=*/true, flags))
7276 return false;
98e5a19a
JM
7277 }
7278 return RECUR (tmp, want_rval);
7279
769430b2
JM
7280 case TRY_FINALLY_EXPR:
7281 return (RECUR (TREE_OPERAND (t, 0), want_rval)
7282 && RECUR (TREE_OPERAND (t, 1), any));
7283
2d76680f 7284 case SCOPE_REF:
69eb4fde 7285 return RECUR (TREE_OPERAND (t, 1), want_rval);
2d76680f
PC
7286
7287 case TARGET_EXPR:
d3a9902e
JM
7288 if (!TARGET_EXPR_DIRECT_INIT_P (t)
7289 && !literal_type_p (TREE_TYPE (t)))
2d76680f
PC
7290 {
7291 if (flags & tf_error)
7292 {
097f82ec 7293 auto_diagnostic_group d;
e40b6fc7
JJ
7294 error_at (loc, "temporary of non-literal type %qT in a "
7295 "constant expression", TREE_TYPE (t));
2d76680f
PC
7296 explain_non_literal_class (TREE_TYPE (t));
7297 }
7298 return false;
7299 }
191816a3 7300 /* FALLTHRU */
2d76680f 7301 case INIT_EXPR:
69eb4fde 7302 return RECUR (TREE_OPERAND (t, 1), rval);
2d76680f
PC
7303
7304 case CONSTRUCTOR:
7305 {
7306 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
7307 constructor_elt *ce;
7308 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
69eb4fde 7309 if (!RECUR (ce->value, want_rval))
2d76680f
PC
7310 return false;
7311 return true;
7312 }
7313
7314 case TREE_LIST:
7315 {
7316 gcc_assert (TREE_PURPOSE (t) == NULL_TREE
7317 || DECL_P (TREE_PURPOSE (t)));
69eb4fde 7318 if (!RECUR (TREE_VALUE (t), want_rval))
2d76680f
PC
7319 return false;
7320 if (TREE_CHAIN (t) == NULL_TREE)
7321 return true;
69eb4fde 7322 return RECUR (TREE_CHAIN (t), want_rval);
2d76680f
PC
7323 }
7324
7325 case TRUNC_DIV_EXPR:
7326 case CEIL_DIV_EXPR:
7327 case FLOOR_DIV_EXPR:
7328 case ROUND_DIV_EXPR:
7329 case TRUNC_MOD_EXPR:
7330 case CEIL_MOD_EXPR:
7331 case ROUND_MOD_EXPR:
7332 {
7333 tree denom = TREE_OPERAND (t, 1);
69eb4fde 7334 if (!RECUR (denom, rval))
2d76680f
PC
7335 return false;
7336 /* We can't call cxx_eval_outermost_constant_expr on an expression
234bef96 7337 that hasn't been through instantiate_non_dependent_expr yet. */
2d76680f
PC
7338 if (!processing_template_decl)
7339 denom = cxx_eval_outermost_constant_expr (denom, true);
7340 if (integer_zerop (denom))
7341 {
7342 if (flags & tf_error)
64d6d399 7343 error ("division by zero is not a constant expression");
2d76680f
PC
7344 return false;
7345 }
7346 else
7347 {
7348 want_rval = true;
69eb4fde 7349 return RECUR (TREE_OPERAND (t, 0), want_rval);
2d76680f
PC
7350 }
7351 }
7352
7353 case COMPOUND_EXPR:
7354 {
7355 /* check_return_expr sometimes wraps a TARGET_EXPR in a
7356 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
7357 introduced by build_call_a. */
7358 tree op0 = TREE_OPERAND (t, 0);
7359 tree op1 = TREE_OPERAND (t, 1);
7360 STRIP_NOPS (op1);
7361 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
7362 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
69eb4fde 7363 return RECUR (op0, want_rval);
2d76680f
PC
7364 else
7365 goto binary;
7366 }
7367
7368 /* If the first operand is the non-short-circuit constant, look at
7369 the second operand; otherwise we only care about the first one for
7370 potentiality. */
7371 case TRUTH_AND_EXPR:
7372 case TRUTH_ANDIF_EXPR:
7373 tmp = boolean_true_node;
7374 goto truth;
7375 case TRUTH_OR_EXPR:
7376 case TRUTH_ORIF_EXPR:
7377 tmp = boolean_false_node;
7378 truth:
7379 {
7380 tree op = TREE_OPERAND (t, 0);
69eb4fde 7381 if (!RECUR (op, rval))
2d76680f
PC
7382 return false;
7383 if (!processing_template_decl)
7384 op = cxx_eval_outermost_constant_expr (op, true);
7385 if (tree_int_cst_equal (op, tmp))
69eb4fde 7386 return RECUR (TREE_OPERAND (t, 1), rval);
2d76680f
PC
7387 else
7388 return true;
7389 }
7390
7391 case PLUS_EXPR:
7392 case MULT_EXPR:
7393 case POINTER_PLUS_EXPR:
7394 case RDIV_EXPR:
7395 case EXACT_DIV_EXPR:
7396 case MIN_EXPR:
7397 case MAX_EXPR:
7398 case LSHIFT_EXPR:
7399 case RSHIFT_EXPR:
2d76680f
PC
7400 case BIT_IOR_EXPR:
7401 case BIT_XOR_EXPR:
7402 case BIT_AND_EXPR:
7403 case TRUTH_XOR_EXPR:
7404 case UNORDERED_EXPR:
7405 case ORDERED_EXPR:
7406 case UNLT_EXPR:
7407 case UNLE_EXPR:
7408 case UNGT_EXPR:
7409 case UNGE_EXPR:
7410 case UNEQ_EXPR:
7411 case LTGT_EXPR:
7412 case RANGE_EXPR:
7413 case COMPLEX_EXPR:
7414 want_rval = true;
7415 /* Fall through. */
7416 case ARRAY_REF:
7417 case ARRAY_RANGE_REF:
7418 case MEMBER_REF:
7419 case DOTSTAR_EXPR:
41b38772 7420 case MEM_REF:
4856a1f0
PC
7421 case BINARY_LEFT_FOLD_EXPR:
7422 case BINARY_RIGHT_FOLD_EXPR:
2d76680f
PC
7423 binary:
7424 for (i = 0; i < 2; ++i)
69eb4fde 7425 if (!RECUR (TREE_OPERAND (t, i), want_rval))
2d76680f
PC
7426 return false;
7427 return true;
7428
2d76680f
PC
7429 case VEC_PERM_EXPR:
7430 for (i = 0; i < 3; ++i)
69eb4fde 7431 if (!RECUR (TREE_OPERAND (t, i), true))
2d76680f
PC
7432 return false;
7433 return true;
7434
7435 case COND_EXPR:
8e007055 7436 if (COND_EXPR_IS_VEC_DELETE (t) && cxx_dialect < cxx2a)
d5bcd6d4
JM
7437 {
7438 if (flags & tf_error)
e40b6fc7 7439 error_at (loc, "%<delete[]%> is not a constant expression");
d5bcd6d4
JM
7440 return false;
7441 }
7442 /* Fall through. */
7443 case IF_STMT:
2d76680f
PC
7444 case VEC_COND_EXPR:
7445 /* If the condition is a known constant, we know which of the legs we
7446 care about; otherwise we only require that the condition and
7447 either of the legs be potentially constant. */
7448 tmp = TREE_OPERAND (t, 0);
69eb4fde 7449 if (!RECUR (tmp, rval))
2d76680f
PC
7450 return false;
7451 if (!processing_template_decl)
7452 tmp = cxx_eval_outermost_constant_expr (tmp, true);
7453 if (integer_zerop (tmp))
69eb4fde 7454 return RECUR (TREE_OPERAND (t, 2), want_rval);
2d76680f 7455 else if (TREE_CODE (tmp) == INTEGER_CST)
69eb4fde 7456 return RECUR (TREE_OPERAND (t, 1), want_rval);
2d76680f
PC
7457 for (i = 1; i < 3; ++i)
7458 if (potential_constant_expression_1 (TREE_OPERAND (t, i),
3075affd
JM
7459 want_rval, strict, now,
7460 tf_none, jump_target))
2d76680f
PC
7461 return true;
7462 if (flags & tf_error)
e40b6fc7 7463 error_at (loc, "expression %qE is not a constant expression", t);
2d76680f
PC
7464 return false;
7465
7466 case VEC_INIT_EXPR:
7467 if (VEC_INIT_EXPR_IS_CONSTEXPR (t))
7468 return true;
7469 if (flags & tf_error)
7470 {
e40b6fc7 7471 error_at (loc, "non-constant array initialization");
2d76680f
PC
7472 diagnose_non_constexpr_vec_init (t);
7473 }
7474 return false;
7475
133bc698
JM
7476 case TYPE_DECL:
7477 case TAG_DEFN:
7478 /* We can see these in statement-expressions. */
7479 return true;
7480
baf9ebc8 7481 case CLEANUP_STMT:
1006c9d4
JJ
7482 if (!RECUR (CLEANUP_BODY (t), any))
7483 return false;
7484 if (!CLEANUP_EH_ONLY (t) && !RECUR (CLEANUP_EXPR (t), any))
7485 return false;
7486 return true;
7487
cda0a029
JM
7488 case EMPTY_CLASS_EXPR:
7489 return false;
7490
f937929e
JM
7491 case GOTO_EXPR:
7492 {
7493 tree *target = &TREE_OPERAND (t, 0);
ab3af181
JJ
7494 /* Gotos representing break and continue are OK. */
7495 if (breaks (target) || continues (target))
3075affd
JM
7496 {
7497 *jump_target = *target;
7498 return true;
7499 }
ab3af181 7500 if (flags & tf_error)
e40b6fc7 7501 error_at (loc, "%<goto%> is not a constant expression");
ab3af181 7502 return false;
f937929e
JM
7503 }
7504
98e09245 7505 case ANNOTATE_EXPR:
98e09245
JJ
7506 return RECUR (TREE_OPERAND (t, 0), rval);
7507
2d76680f
PC
7508 default:
7509 if (objc_is_property_ref (t))
7510 return false;
7511
7512 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t)));
ab3af181 7513 gcc_unreachable ();
2d76680f
PC
7514 return false;
7515 }
69eb4fde 7516#undef RECUR
2d76680f
PC
7517}
7518
3075affd
JM
7519bool
7520potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
7521 tsubst_flags_t flags)
7522{
7523 tree target = NULL_TREE;
7524 return potential_constant_expression_1 (t, want_rval, strict, now,
7525 flags, &target);
7526}
7527
2d76680f
PC
7528/* The main entry point to the above. */
7529
7530bool
7531potential_constant_expression (tree t)
7532{
a0ab7ccd 7533 return potential_constant_expression_1 (t, false, true, false, tf_none);
2d76680f
PC
7534}
7535
7536/* As above, but require a constant rvalue. */
7537
7538bool
7539potential_rvalue_constant_expression (tree t)
7540{
a0ab7ccd 7541 return potential_constant_expression_1 (t, true, true, false, tf_none);
2d76680f
PC
7542}
7543
7544/* Like above, but complain about non-constant expressions. */
7545
7546bool
7547require_potential_constant_expression (tree t)
7548{
d8cff23f
MP
7549 return potential_constant_expression_1 (t, false, true, false,
7550 tf_warning_or_error);
2d76680f
PC
7551}
7552
7553/* Cross product of the above. */
7554
7555bool
7556require_potential_rvalue_constant_expression (tree t)
7557{
d8cff23f
MP
7558 return potential_constant_expression_1 (t, true, true, false,
7559 tf_warning_or_error);
7560}
7561
7562/* Like above, but don't consider PARM_DECL a potential_constant_expression. */
7563
7564bool
7565require_rvalue_constant_expression (tree t)
7566{
7567 return potential_constant_expression_1 (t, true, true, true,
7568 tf_warning_or_error);
a0ab7ccd
JM
7569}
7570
7571/* Like potential_constant_expression, but don't consider possible constexpr
7572 substitution of the current function. That is, PARM_DECL qualifies under
7573 potential_constant_expression, but not here.
7574
7575 This is basically what you can check when any actual constant values might
7576 be value-dependent. */
7577
7578bool
7579is_constant_expression (tree t)
7580{
7581 return potential_constant_expression_1 (t, false, true, true, tf_none);
7582}
7583
7584/* Like above, but complain about non-constant expressions. */
7585
7586bool
7587require_constant_expression (tree t)
7588{
7589 return potential_constant_expression_1 (t, false, true, true,
7590 tf_warning_or_error);
7591}
7592
7593/* Like is_constant_expression, but allow const variables that are not allowed
7594 under constexpr rules. */
7595
7596bool
7597is_static_init_expression (tree t)
7598{
7599 return potential_constant_expression_1 (t, false, false, true, tf_none);
2d76680f
PC
7600}
7601
eb07f187
JM
7602/* Returns true if T is a potential constant expression that is not
7603 instantiation-dependent, and therefore a candidate for constant folding even
7604 in a template. */
7605
7606bool
a0ab7ccd 7607is_nondependent_constant_expression (tree t)
eb07f187
JM
7608{
7609 return (!type_unknown_p (t)
7610 && !BRACE_ENCLOSED_INITIALIZER_P (t)
a0ab7ccd 7611 && is_constant_expression (t)
eb07f187
JM
7612 && !instantiation_dependent_expression_p (t));
7613}
7614
7615/* Returns true if T is a potential static initializer expression that is not
7616 instantiation-dependent. */
7617
7618bool
a0ab7ccd 7619is_nondependent_static_init_expression (tree t)
eb07f187
JM
7620{
7621 return (!type_unknown_p (t)
7622 && !BRACE_ENCLOSED_INITIALIZER_P (t)
a0ab7ccd 7623 && is_static_init_expression (t)
eb07f187
JM
7624 && !instantiation_dependent_expression_p (t));
7625}
7626
97f3003f
JM
7627/* Finalize constexpr processing after parsing. */
7628
7629void
7630fini_constexpr (void)
7631{
7632 /* The contexpr call and fundef copies tables are no longer needed. */
7633 constexpr_call_table = NULL;
7634 fundef_copies_table = NULL;
7635}
7636
2d76680f 7637#include "gt-cp-constexpr.h"
This page took 3.202112 seconds and 5 git commands to generate.