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