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