]> gcc.gnu.org Git - gcc.git/blame - gcc/cp/constexpr.cc
testsuite: Add missing <vector> header to test
[gcc.git] / gcc / cp / constexpr.cc
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
7adcbafe 5 Copyright (C) 1998-2022 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"
a8db7887 38#include "cgraph.h"
00f34291 39#include "opts.h"
87c2080b
JM
40#include "stringpool.h"
41#include "attribs.h"
2d76680f
PC
42
43static bool verify_constant (tree, bool, bool *, bool *);
44#define VERIFY_CONSTANT(X) \
45do { \
2b3ab879 46 if (verify_constant ((X), ctx->quiet, non_constant_p, overflow_p)) \
2d76680f
PC
47 return t; \
48 } while (0)
49
582d2481
JJ
50static HOST_WIDE_INT find_array_ctor_elt (tree ary, tree dindex,
51 bool insert = false);
baf05d54 52static int array_index_cmp (tree key, tree index);
582d2481 53
2d76680f
PC
54/* Returns true iff FUN is an instantiation of a constexpr function
55 template or a defaulted constexpr function. */
56
57bool
58is_instantiation_of_constexpr (tree fun)
59{
60 return ((DECL_TEMPLOID_INSTANTIATION (fun)
61 && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun)))
62 || (DECL_DEFAULTED_FN (fun)
63 && DECL_DECLARED_CONSTEXPR_P (fun)));
64}
65
66/* Return true if T is a literal type. */
67
68bool
69literal_type_p (tree t)
70{
71 if (SCALAR_TYPE_P (t)
b55b02ea 72 || VECTOR_TYPE_P (t)
9f613f06 73 || TYPE_REF_P (t)
e42c407c 74 || (VOID_TYPE_P (t) && cxx_dialect >= cxx14))
2d76680f
PC
75 return true;
76 if (CLASS_TYPE_P (t))
77 {
78 t = complete_type (t);
79 gcc_assert (COMPLETE_TYPE_P (t) || errorcount);
80 return CLASSTYPE_LITERAL_P (t);
81 }
82 if (TREE_CODE (t) == ARRAY_TYPE)
83 return literal_type_p (strip_array_types (t));
84 return false;
85}
86
87/* If DECL is a variable declared `constexpr', require its type
9638f320
PC
88 be literal. Return error_mark_node if we give an error, the
89 DECL otherwise. */
2d76680f
PC
90
91tree
92ensure_literal_type_for_constexpr_object (tree decl)
93{
94 tree type = TREE_TYPE (decl);
95 if (VAR_P (decl)
96 && (DECL_DECLARED_CONSTEXPR_P (decl)
97 || var_in_constexpr_fn (decl))
98 && !processing_template_decl)
99 {
100 tree stype = strip_array_types (type);
101 if (CLASS_TYPE_P (stype) && !COMPLETE_TYPE_P (complete_type (stype)))
102 /* Don't complain here, we'll complain about incompleteness
103 when we try to initialize the variable. */;
104 else if (!literal_type_p (type))
105 {
106 if (DECL_DECLARED_CONSTEXPR_P (decl))
f1eac182 107 {
097f82ec 108 auto_diagnostic_group d;
f2935576
PC
109 error_at (DECL_SOURCE_LOCATION (decl),
110 "the type %qT of %<constexpr%> variable %qD "
111 "is not literal", type, decl);
f1eac182 112 explain_non_literal_class (type);
9638f320 113 decl = error_mark_node;
f1eac182 114 }
8892d532 115 else if (cxx_dialect < cxx23)
60813a46 116 {
5d4e573b 117 if (!is_instantiation_of_constexpr (current_function_decl))
f1eac182 118 {
097f82ec 119 auto_diagnostic_group d;
f2935576
PC
120 error_at (DECL_SOURCE_LOCATION (decl),
121 "variable %qD of non-literal type %qT in "
8892d532
JJ
122 "%<constexpr%> function only available with "
123 "%<-std=c++2b%> or %<-std=gnu++2b%>", decl, type);
f1eac182 124 explain_non_literal_class (type);
9638f320 125 decl = error_mark_node;
f1eac182 126 }
60813a46
JM
127 cp_function_chain->invalid_constexpr = true;
128 }
2d76680f 129 }
8e9589bd
JM
130 else if (DECL_DECLARED_CONSTEXPR_P (decl)
131 && variably_modified_type_p (type, NULL_TREE))
132 {
f2935576
PC
133 error_at (DECL_SOURCE_LOCATION (decl),
134 "%<constexpr%> variable %qD has variably-modified "
135 "type %qT", decl, type);
8e9589bd
JM
136 decl = error_mark_node;
137 }
2d76680f
PC
138 }
139 return decl;
140}
141
ca752f39 142struct constexpr_fundef_hasher : ggc_ptr_hash<constexpr_fundef>
2d76680f 143{
bfc139e2
NS
144 static hashval_t hash (const constexpr_fundef *);
145 static bool equal (const constexpr_fundef *, const constexpr_fundef *);
2d76680f
PC
146};
147
148/* This table holds all constexpr function definitions seen in
149 the current translation unit. */
150
151static GTY (()) hash_table<constexpr_fundef_hasher> *constexpr_fundef_table;
152
153/* Utility function used for managing the constexpr function table.
154 Return true if the entries pointed to by P and Q are for the
155 same constexpr function. */
156
157inline bool
bfc139e2
NS
158constexpr_fundef_hasher::equal (const constexpr_fundef *lhs,
159 const constexpr_fundef *rhs)
2d76680f
PC
160{
161 return lhs->decl == rhs->decl;
162}
163
164/* Utility function used for managing the constexpr function table.
165 Return a hash value for the entry pointed to by Q. */
166
167inline hashval_t
bfc139e2 168constexpr_fundef_hasher::hash (const constexpr_fundef *fundef)
2d76680f
PC
169{
170 return DECL_UID (fundef->decl);
171}
172
173/* Return a previously saved definition of function FUN. */
174
bfc139e2 175constexpr_fundef *
2d76680f
PC
176retrieve_constexpr_fundef (tree fun)
177{
2d76680f
PC
178 if (constexpr_fundef_table == NULL)
179 return NULL;
180
bfc139e2 181 constexpr_fundef fundef = { fun, NULL_TREE, NULL_TREE, NULL_TREE };
2d76680f
PC
182 return constexpr_fundef_table->find (&fundef);
183}
184
185/* Check whether the parameter and return types of FUN are valid for a
186 constexpr function, and complain if COMPLAIN. */
187
98e5a19a 188bool
2d76680f
PC
189is_valid_constexpr_fn (tree fun, bool complain)
190{
191 bool ret = true;
192
31f7f784 193 if (DECL_INHERITED_CTOR (fun)
2d76680f
PC
194 && TREE_CODE (fun) == TEMPLATE_DECL)
195 {
196 ret = false;
197 if (complain)
84fa214d 198 error ("inherited constructor %qD is not %<constexpr%>",
31f7f784 199 DECL_INHERITED_CTOR (fun));
2d76680f
PC
200 }
201 else
202 {
203 for (tree parm = FUNCTION_FIRST_USER_PARM (fun);
204 parm != NULL_TREE; parm = TREE_CHAIN (parm))
205 if (!literal_type_p (TREE_TYPE (parm)))
206 {
207 ret = false;
208 if (complain)
209 {
097f82ec 210 auto_diagnostic_group d;
84fa214d 211 error ("invalid type for parameter %d of %<constexpr%> "
2d76680f
PC
212 "function %q+#D", DECL_PARM_INDEX (parm), fun);
213 explain_non_literal_class (TREE_TYPE (parm));
214 }
215 }
216 }
217
e1bea341
JM
218 if (LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun)) && cxx_dialect < cxx17)
219 {
220 ret = false;
221 if (complain)
222 inform (DECL_SOURCE_LOCATION (fun),
84fa214d 223 "lambdas are implicitly %<constexpr%> only in C++17 and later");
e1bea341 224 }
87c2080b
JM
225 else if (DECL_DESTRUCTOR_P (fun))
226 {
227 if (cxx_dialect < cxx20)
228 {
229 ret = false;
230 if (complain)
231 error_at (DECL_SOURCE_LOCATION (fun),
232 "%<constexpr%> destructors only available"
233 " with %<-std=c++20%> or %<-std=gnu++20%>");
234 }
235 }
e1bea341 236 else if (!DECL_CONSTRUCTOR_P (fun))
2d76680f
PC
237 {
238 tree rettype = TREE_TYPE (TREE_TYPE (fun));
239 if (!literal_type_p (rettype))
240 {
241 ret = false;
242 if (complain)
243 {
097f82ec 244 auto_diagnostic_group d;
84fa214d 245 error ("invalid return type %qT of %<constexpr%> function %q+D",
2d76680f
PC
246 rettype, fun);
247 explain_non_literal_class (rettype);
248 }
249 }
250
54069e59
JM
251 /* C++14 DR 1684 removed this restriction. */
252 if (cxx_dialect < cxx14
253 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
2d76680f
PC
254 && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun)))
255 {
256 ret = false;
097f82ec
DM
257 if (complain)
258 {
259 auto_diagnostic_group d;
260 if (pedwarn (DECL_SOURCE_LOCATION (fun), OPT_Wpedantic,
261 "enclosing class of %<constexpr%> non-static"
262 " member function %q+#D is not a literal type",
263 fun))
264 explain_non_literal_class (DECL_CONTEXT (fun));
265 }
2d76680f
PC
266 }
267 }
268 else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun)))
269 {
270 ret = false;
271 if (complain)
272 error ("%q#T has virtual base classes", DECL_CONTEXT (fun));
273 }
274
275 return ret;
276}
277
278/* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF
279 for a member of an anonymous aggregate, INIT is the initializer for that
280 member, and VEC_OUTER is the vector of constructor elements for the class
281 whose constructor we are processing. Add the initializer to the vector
282 and return true to indicate success. */
283
284static bool
285build_anon_member_initialization (tree member, tree init,
286 vec<constructor_elt, va_gc> **vec_outer)
287{
288 /* MEMBER presents the relevant fields from the inside out, but we need
289 to build up the initializer from the outside in so that we can reuse
290 previously built CONSTRUCTORs if this is, say, the second field in an
291 anonymous struct. So we use a vec as a stack. */
292 auto_vec<tree, 2> fields;
293 do
294 {
295 fields.safe_push (TREE_OPERAND (member, 1));
296 member = TREE_OPERAND (member, 0);
297 }
298 while (ANON_AGGR_TYPE_P (TREE_TYPE (member))
299 && TREE_CODE (member) == COMPONENT_REF);
300
301 /* VEC has the constructor elements vector for the context of FIELD.
302 If FIELD is an anonymous aggregate, we will push inside it. */
303 vec<constructor_elt, va_gc> **vec = vec_outer;
304 tree field;
305 while (field = fields.pop(),
306 ANON_AGGR_TYPE_P (TREE_TYPE (field)))
307 {
308 tree ctor;
309 /* If there is already an outer constructor entry for the anonymous
310 aggregate FIELD, use it; otherwise, insert one. */
311 if (vec_safe_is_empty (*vec)
312 || (*vec)->last().index != field)
313 {
314 ctor = build_constructor (TREE_TYPE (field), NULL);
315 CONSTRUCTOR_APPEND_ELT (*vec, field, ctor);
316 }
317 else
318 ctor = (*vec)->last().value;
319 vec = &CONSTRUCTOR_ELTS (ctor);
320 }
321
322 /* Now we're at the innermost field, the one that isn't an anonymous
323 aggregate. Add its initializer to the CONSTRUCTOR and we're done. */
324 gcc_assert (fields.is_empty());
325 CONSTRUCTOR_APPEND_ELT (*vec, field, init);
326
327 return true;
328}
329
330/* Subroutine of build_constexpr_constructor_member_initializers.
331 The expression tree T represents a data member initialization
332 in a (constexpr) constructor definition. Build a pairing of
333 the data member with its initializer, and prepend that pair
334 to the existing initialization pair INITS. */
335
336static bool
337build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec)
338{
339 tree member, init;
340 if (TREE_CODE (t) == CLEANUP_POINT_EXPR)
341 t = TREE_OPERAND (t, 0);
342 if (TREE_CODE (t) == EXPR_STMT)
343 t = TREE_OPERAND (t, 0);
344 if (t == error_mark_node)
345 return false;
346 if (TREE_CODE (t) == STATEMENT_LIST)
347 {
0f54cc9c
JM
348 for (tree stmt : tsi_range (t))
349 if (! build_data_member_initialization (stmt, vec))
350 return false;
2d76680f
PC
351 return true;
352 }
353 if (TREE_CODE (t) == CLEANUP_STMT)
354 {
355 /* We can't see a CLEANUP_STMT in a constructor for a literal class,
356 but we can in a constexpr constructor for a non-literal class. Just
357 ignore it; either all the initialization will be constant, in which
358 case the cleanup can't run, or it can't be constexpr.
359 Still recurse into CLEANUP_BODY. */
360 return build_data_member_initialization (CLEANUP_BODY (t), vec);
361 }
362 if (TREE_CODE (t) == CONVERT_EXPR)
363 t = TREE_OPERAND (t, 0);
364 if (TREE_CODE (t) == INIT_EXPR
60813a46
JM
365 /* vptr initialization shows up as a MODIFY_EXPR. In C++14 we only
366 use what this function builds for cx_check_missing_mem_inits, and
367 assignment in the ctor body doesn't count. */
368 || (cxx_dialect < cxx14 && TREE_CODE (t) == MODIFY_EXPR))
2d76680f
PC
369 {
370 member = TREE_OPERAND (t, 0);
371 init = break_out_target_exprs (TREE_OPERAND (t, 1));
372 }
373 else if (TREE_CODE (t) == CALL_EXPR)
374 {
60813a46
JM
375 tree fn = get_callee_fndecl (t);
376 if (!fn || !DECL_CONSTRUCTOR_P (fn))
377 /* We're only interested in calls to subobject constructors. */
378 return true;
2d76680f
PC
379 member = CALL_EXPR_ARG (t, 0);
380 /* We don't use build_cplus_new here because it complains about
381 abstract bases. Leaving the call unwrapped means that it has the
382 wrong type, but cxx_eval_constant_expression doesn't care. */
383 init = break_out_target_exprs (t);
384 }
385 else if (TREE_CODE (t) == BIND_EXPR)
386 return build_data_member_initialization (BIND_EXPR_BODY (t), vec);
2d76680f 387 else
60813a46
JM
388 /* Don't add anything else to the CONSTRUCTOR. */
389 return true;
2d76680f
PC
390 if (INDIRECT_REF_P (member))
391 member = TREE_OPERAND (member, 0);
392 if (TREE_CODE (member) == NOP_EXPR)
393 {
394 tree op = member;
395 STRIP_NOPS (op);
396 if (TREE_CODE (op) == ADDR_EXPR)
397 {
398 gcc_assert (same_type_ignoring_top_level_qualifiers_p
399 (TREE_TYPE (TREE_TYPE (op)),
400 TREE_TYPE (TREE_TYPE (member))));
401 /* Initializing a cv-qualified member; we need to look through
402 the const_cast. */
403 member = op;
404 }
405 else if (op == current_class_ptr
406 && (same_type_ignoring_top_level_qualifiers_p
407 (TREE_TYPE (TREE_TYPE (member)),
408 current_class_type)))
409 /* Delegating constructor. */
410 member = op;
411 else
412 {
413 /* This is an initializer for an empty base; keep it for now so
414 we can check it in cxx_eval_bare_aggregate. */
415 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member))));
416 }
417 }
418 if (TREE_CODE (member) == ADDR_EXPR)
419 member = TREE_OPERAND (member, 0);
420 if (TREE_CODE (member) == COMPONENT_REF)
421 {
422 tree aggr = TREE_OPERAND (member, 0);
8cb7aaa1
JM
423 if (TREE_CODE (aggr) == VAR_DECL)
424 /* Initializing a local variable, don't add anything. */
425 return true;
2d76680f
PC
426 if (TREE_CODE (aggr) != COMPONENT_REF)
427 /* Normal member initialization. */
428 member = TREE_OPERAND (member, 1);
429 else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr)))
430 /* Initializing a member of an anonymous union. */
431 return build_anon_member_initialization (member, init, vec);
432 else
433 /* We're initializing a vtable pointer in a base. Leave it as
434 COMPONENT_REF so we remember the path to get to the vfield. */
435 gcc_assert (TREE_TYPE (member) == vtbl_ptr_type_node);
436 }
437
49b5925f
JM
438 /* Value-initialization can produce multiple initializers for the
439 same field; use the last one. */
440 if (!vec_safe_is_empty (*vec) && (*vec)->last().index == member)
441 (*vec)->last().value = init;
442 else
443 CONSTRUCTOR_APPEND_ELT (*vec, member, init);
2d76680f
PC
444 return true;
445}
446
447/* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval.
448 In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a
449 BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */
450
451static bool
452check_constexpr_bind_expr_vars (tree t)
453{
454 gcc_assert (TREE_CODE (t) == BIND_EXPR);
455
2d76680f
PC
456 for (tree var = BIND_EXPR_VARS (t); var; var = DECL_CHAIN (var))
457 if (TREE_CODE (var) == TYPE_DECL
4414e22e
PC
458 && DECL_IMPLICIT_TYPEDEF_P (var)
459 && !LAMBDA_TYPE_P (TREE_TYPE (var)))
2d76680f
PC
460 return false;
461 return true;
462}
463
464/* Subroutine of check_constexpr_ctor_body. */
465
466static bool
467check_constexpr_ctor_body_1 (tree last, tree list)
468{
469 switch (TREE_CODE (list))
470 {
471 case DECL_EXPR:
a0008434
MP
472 if (TREE_CODE (DECL_EXPR_DECL (list)) == USING_DECL
473 || TREE_CODE (DECL_EXPR_DECL (list)) == TYPE_DECL)
2d76680f 474 return true;
2d76680f
PC
475 return false;
476
477 case CLEANUP_POINT_EXPR:
478 return check_constexpr_ctor_body (last, TREE_OPERAND (list, 0),
479 /*complain=*/false);
480
481 case BIND_EXPR:
482 if (!check_constexpr_bind_expr_vars (list)
483 || !check_constexpr_ctor_body (last, BIND_EXPR_BODY (list),
484 /*complain=*/false))
485 return false;
486 return true;
487
488 case USING_STMT:
489 case STATIC_ASSERT:
96a95ac1 490 case DEBUG_BEGIN_STMT:
2d76680f
PC
491 return true;
492
493 default:
494 return false;
495 }
496}
497
498/* Make sure that there are no statements after LAST in the constructor
499 body represented by LIST. */
500
501bool
502check_constexpr_ctor_body (tree last, tree list, bool complain)
503{
60813a46
JM
504 /* C++14 doesn't require a constexpr ctor to have an empty body. */
505 if (cxx_dialect >= cxx14)
506 return true;
507
2d76680f
PC
508 bool ok = true;
509 if (TREE_CODE (list) == STATEMENT_LIST)
510 {
511 tree_stmt_iterator i = tsi_last (list);
512 for (; !tsi_end_p (i); tsi_prev (&i))
513 {
514 tree t = tsi_stmt (i);
515 if (t == last)
516 break;
517 if (!check_constexpr_ctor_body_1 (last, t))
518 {
519 ok = false;
520 break;
521 }
522 }
523 }
524 else if (list != last
525 && !check_constexpr_ctor_body_1 (last, list))
526 ok = false;
527 if (!ok)
528 {
529 if (complain)
84fa214d 530 error ("%<constexpr%> constructor does not have empty body");
2d76680f
PC
531 DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false;
532 }
533 return ok;
534}
535
536/* V is a vector of constructor elements built up for the base and member
537 initializers of a constructor for TYPE. They need to be in increasing
538 offset order, which they might not be yet if TYPE has a primary base
539 which is not first in the base-clause or a vptr and at least one base
540 all of which are non-primary. */
541
542static vec<constructor_elt, va_gc> *
543sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v)
544{
545 tree pri = CLASSTYPE_PRIMARY_BINFO (type);
546 tree field_type;
547 unsigned i;
548 constructor_elt *ce;
549
550 if (pri)
551 field_type = BINFO_TYPE (pri);
552 else if (TYPE_CONTAINS_VPTR_P (type))
553 field_type = vtbl_ptr_type_node;
554 else
555 return v;
556
557 /* Find the element for the primary base or vptr and move it to the
558 beginning of the vec. */
559 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
560 if (TREE_TYPE (ce->index) == field_type)
561 break;
562
563 if (i > 0 && i < vec_safe_length (v))
564 {
565 vec<constructor_elt, va_gc> &vref = *v;
566 constructor_elt elt = vref[i];
567 for (; i > 0; --i)
568 vref[i] = vref[i-1];
569 vref[0] = elt;
570 }
571
572 return v;
573}
574
575/* Build compile-time evalable representations of member-initializer list
576 for a constexpr constructor. */
577
578static tree
579build_constexpr_constructor_member_initializers (tree type, tree body)
580{
581 vec<constructor_elt, va_gc> *vec = NULL;
582 bool ok = true;
fa837fb6
JM
583 while (true)
584 switch (TREE_CODE (body))
585 {
586 case MUST_NOT_THROW_EXPR:
587 case EH_SPEC_BLOCK:
588 body = TREE_OPERAND (body, 0);
589 break;
590
591 case STATEMENT_LIST:
0f54cc9c 592 for (tree stmt : tsi_range (body))
fa837fb6 593 {
0f54cc9c 594 body = stmt;
fa837fb6
JM
595 if (TREE_CODE (body) == BIND_EXPR)
596 break;
597 }
598 break;
599
600 case BIND_EXPR:
601 body = BIND_EXPR_BODY (body);
602 goto found;
603
604 default:
605 gcc_unreachable ();
58cc255c 606 }
fa837fb6 607 found:
1259cb6d
JJ
608 if (TREE_CODE (body) == TRY_BLOCK)
609 {
610 body = TREE_OPERAND (body, 0);
611 if (TREE_CODE (body) == BIND_EXPR)
612 body = BIND_EXPR_BODY (body);
613 }
2d76680f
PC
614 if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
615 {
616 body = TREE_OPERAND (body, 0);
617 if (TREE_CODE (body) == EXPR_STMT)
618 body = TREE_OPERAND (body, 0);
619 if (TREE_CODE (body) == INIT_EXPR
620 && (same_type_ignoring_top_level_qualifiers_p
621 (TREE_TYPE (TREE_OPERAND (body, 0)),
622 current_class_type)))
623 {
624 /* Trivial copy. */
625 return TREE_OPERAND (body, 1);
626 }
627 ok = build_data_member_initialization (body, &vec);
628 }
629 else if (TREE_CODE (body) == STATEMENT_LIST)
630 {
0f54cc9c 631 for (tree stmt : tsi_range (body))
2d76680f 632 {
0f54cc9c 633 ok = build_data_member_initialization (stmt, &vec);
2d76680f
PC
634 if (!ok)
635 break;
636 }
637 }
2d76680f
PC
638 else if (EXPR_P (body))
639 ok = build_data_member_initialization (body, &vec);
640 else
641 gcc_assert (errorcount > 0);
642 if (ok)
643 {
644 if (vec_safe_length (vec) > 0)
645 {
646 /* In a delegating constructor, return the target. */
647 constructor_elt *ce = &(*vec)[0];
648 if (ce->index == current_class_ptr)
649 {
650 body = ce->value;
651 vec_free (vec);
652 return body;
653 }
654 }
655 vec = sort_constexpr_mem_initializers (type, vec);
656 return build_constructor (type, vec);
657 }
658 else
659 return error_mark_node;
660}
661
1b6fa695
ML
662/* We have an expression tree T that represents a call, either CALL_EXPR
663 or AGGR_INIT_EXPR. If the call is lexically to a named function,
664 retrun the _DECL for that function. */
665
666static tree
667get_function_named_in_call (tree t)
668{
669 tree fun = cp_get_callee (t);
670 if (fun && TREE_CODE (fun) == ADDR_EXPR
671 && TREE_CODE (TREE_OPERAND (fun, 0)) == FUNCTION_DECL)
672 fun = TREE_OPERAND (fun, 0);
673 return fun;
674}
675
bfc139e2 676/* Subroutine of check_constexpr_fundef. BODY is the body of a function
2d76680f
PC
677 declared to be constexpr, or a sub-statement thereof. Returns the
678 return value if suitable, error_mark_node for a statement not allowed in
679 a constexpr function, or NULL_TREE if no return value was found. */
680
5afef8b1 681tree
2d76680f
PC
682constexpr_fn_retval (tree body)
683{
684 switch (TREE_CODE (body))
685 {
686 case STATEMENT_LIST:
687 {
2d76680f 688 tree expr = NULL_TREE;
0f54cc9c 689 for (tree stmt : tsi_range (body))
2d76680f 690 {
0f54cc9c 691 tree s = constexpr_fn_retval (stmt);
2d76680f
PC
692 if (s == error_mark_node)
693 return error_mark_node;
694 else if (s == NULL_TREE)
695 /* Keep iterating. */;
696 else if (expr)
697 /* Multiple return statements. */
698 return error_mark_node;
699 else
700 expr = s;
701 }
702 return expr;
703 }
704
705 case RETURN_EXPR:
706 return break_out_target_exprs (TREE_OPERAND (body, 0));
707
708 case DECL_EXPR:
0162cb3b
PC
709 {
710 tree decl = DECL_EXPR_DECL (body);
711 if (TREE_CODE (decl) == USING_DECL
712 /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */
713 || DECL_ARTIFICIAL (decl))
714 return NULL_TREE;
715 return error_mark_node;
716 }
2d76680f
PC
717
718 case CLEANUP_POINT_EXPR:
719 return constexpr_fn_retval (TREE_OPERAND (body, 0));
720
721 case BIND_EXPR:
722 if (!check_constexpr_bind_expr_vars (body))
723 return error_mark_node;
724 return constexpr_fn_retval (BIND_EXPR_BODY (body));
725
726 case USING_STMT:
96a95ac1 727 case DEBUG_BEGIN_STMT:
2d76680f
PC
728 return NULL_TREE;
729
1b6fa695
ML
730 case CALL_EXPR:
731 {
732 tree fun = get_function_named_in_call (body);
733 if (fun != NULL_TREE
3d78e008 734 && fndecl_built_in_p (fun, BUILT_IN_UNREACHABLE))
1b6fa695
ML
735 return NULL_TREE;
736 }
737 /* Fallthru. */
738
2d76680f
PC
739 default:
740 return error_mark_node;
741 }
742}
743
bfc139e2 744/* Subroutine of check_constexpr_fundef. BODY is the DECL_SAVED_TREE of
2d76680f
PC
745 FUN; do the necessary transformations to turn it into a single expression
746 that we can store in the hash table. */
747
748static tree
749massage_constexpr_body (tree fun, tree body)
750{
751 if (DECL_CONSTRUCTOR_P (fun))
752 body = build_constexpr_constructor_member_initializers
753 (DECL_CONTEXT (fun), body);
60813a46 754 else if (cxx_dialect < cxx14)
2d76680f
PC
755 {
756 if (TREE_CODE (body) == EH_SPEC_BLOCK)
757 body = EH_SPEC_STMTS (body);
758 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
759 body = TREE_OPERAND (body, 0);
760 body = constexpr_fn_retval (body);
761 }
762 return body;
763}
764
3e4b91f2
NS
765/* CTYPE is a type constructed from BODY. Return true if some
766 bases/fields are uninitialized, and complain if COMPLAIN. */
2d76680f
PC
767
768static bool
3e4b91f2 769cx_check_missing_mem_inits (tree ctype, tree body, bool complain)
2d76680f 770{
7906797e 771 /* We allow uninitialized bases/fields in C++20. */
b04445d4 772 if (cxx_dialect >= cxx20)
7906797e
MP
773 return false;
774
3e4b91f2
NS
775 unsigned nelts = 0;
776
777 if (body)
778 {
779 if (TREE_CODE (body) != CONSTRUCTOR)
780 return false;
781 nelts = CONSTRUCTOR_NELTS (body);
782 }
783 tree field = TYPE_FIELDS (ctype);
2d76680f
PC
784
785 if (TREE_CODE (ctype) == UNION_TYPE)
786 {
787 if (nelts == 0 && next_initializable_field (field))
788 {
789 if (complain)
790 error ("%<constexpr%> constructor for union %qT must "
791 "initialize exactly one non-static data member", ctype);
792 return true;
793 }
794 return false;
795 }
796
3e4b91f2
NS
797 /* Iterate over the CONSTRUCTOR, checking any missing fields don't
798 need an explicit initialization. */
799 bool bad = false;
800 for (unsigned i = 0; i <= nelts; ++i)
2d76680f 801 {
3e4b91f2
NS
802 tree index = NULL_TREE;
803 if (i < nelts)
2d76680f
PC
804 {
805 index = CONSTRUCTOR_ELT (body, i)->index;
806 /* Skip base and vtable inits. */
807 if (TREE_CODE (index) != FIELD_DECL
808 || DECL_ARTIFICIAL (index))
809 continue;
810 }
3e4b91f2 811
2d76680f
PC
812 for (; field != index; field = DECL_CHAIN (field))
813 {
814 tree ftype;
3e4b91f2
NS
815 if (TREE_CODE (field) != FIELD_DECL)
816 continue;
7c30b12a 817 if (DECL_UNNAMED_BIT_FIELD (field))
2d76680f 818 continue;
3e4b91f2
NS
819 if (DECL_ARTIFICIAL (field))
820 continue;
821 if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
822 {
7906797e 823 /* Recurse to check the anonymous aggregate member. */
3e4b91f2
NS
824 bad |= cx_check_missing_mem_inits
825 (TREE_TYPE (field), NULL_TREE, complain);
826 if (bad && !complain)
827 return true;
828 continue;
829 }
a1c9c9ff
JM
830 ftype = TREE_TYPE (field);
831 if (!ftype || !TYPE_P (ftype) || !COMPLETE_TYPE_P (ftype))
832 /* A flexible array can't be intialized here, so don't complain
833 that it isn't. */
834 continue;
a4dfd0f0 835 if (is_empty_field (field))
6876b269
JM
836 /* An empty field doesn't need an initializer. */
837 continue;
a1c9c9ff 838 ftype = strip_array_types (ftype);
2d76680f
PC
839 if (type_has_constexpr_default_constructor (ftype))
840 {
841 /* It's OK to skip a member with a trivial constexpr ctor.
842 A constexpr ctor that isn't trivial should have been
843 added in by now. */
844 gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype)
845 || errorcount != 0);
846 continue;
847 }
848 if (!complain)
849 return true;
097f82ec 850 auto_diagnostic_group d;
b8cd3996
JM
851 error ("member %qD must be initialized by mem-initializer "
852 "in %<constexpr%> constructor", field);
853 inform (DECL_SOURCE_LOCATION (field), "declared here");
2d76680f
PC
854 bad = true;
855 }
856 if (field == NULL_TREE)
857 break;
3e4b91f2
NS
858
859 if (ANON_AGGR_TYPE_P (TREE_TYPE (index)))
860 {
861 /* Check the anonymous aggregate initializer is valid. */
862 bad |= cx_check_missing_mem_inits
863 (TREE_TYPE (index), CONSTRUCTOR_ELT (body, i)->value, complain);
864 if (bad && !complain)
865 return true;
866 }
2d76680f
PC
867 field = DECL_CHAIN (field);
868 }
869
870 return bad;
871}
872
873/* We are processing the definition of the constexpr function FUN.
bfc139e2
NS
874 Check that its body fulfills the apropriate requirements and
875 enter it in the constexpr function definition table. */
2d76680f 876
bfc139e2
NS
877void
878maybe_save_constexpr_fundef (tree fun)
2d76680f 879{
bfc139e2 880 if (processing_template_decl
bfc139e2 881 || cp_function_chain->invalid_constexpr
81f97181 882 || (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun)))
bfc139e2 883 return;
2d76680f 884
87c2080b
JM
885 /* With -fimplicit-constexpr, try to make inlines constexpr. We'll
886 actually set DECL_DECLARED_CONSTEXPR_P below if the checks pass. */
887 bool implicit = false;
888 if (flag_implicit_constexpr)
889 {
890 if (DECL_DELETING_DESTRUCTOR_P (fun)
891 && decl_implicit_constexpr_p (DECL_CLONED_FUNCTION (fun)))
892 /* Don't inherit implicit constexpr from the non-deleting
893 destructor. */
894 DECL_DECLARED_CONSTEXPR_P (fun) = false;
895
896 if (!DECL_DECLARED_CONSTEXPR_P (fun)
897 && DECL_DECLARED_INLINE_P (fun)
898 && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fun)))
899 implicit = true;
900 }
901
902 if (!DECL_DECLARED_CONSTEXPR_P (fun) && !implicit)
903 return;
904
905 bool complain = !DECL_GENERATED_P (fun) && !implicit;
37326651
JM
906
907 if (!is_valid_constexpr_fn (fun, complain))
bfc139e2 908 return;
2d76680f 909
bfc139e2 910 tree massaged = massage_constexpr_body (fun, DECL_SAVED_TREE (fun));
ca30abcd 911 if (massaged == NULL_TREE || massaged == error_mark_node)
2d76680f 912 {
87c2080b 913 if (!DECL_CONSTRUCTOR_P (fun) && complain)
84fa214d
ML
914 error ("body of %<constexpr%> function %qD not a return-statement",
915 fun);
bfc139e2 916 return;
2d76680f
PC
917 }
918
4f05d85a 919 bool potential = potential_rvalue_constant_expression (massaged);
37326651 920 if (!potential && complain)
4f05d85a 921 require_potential_rvalue_constant_expression (massaged);
2d76680f 922
37326651
JM
923 if (DECL_CONSTRUCTOR_P (fun) && potential)
924 {
925 if (cx_check_missing_mem_inits (DECL_CONTEXT (fun),
926 massaged, complain))
927 potential = false;
928 else if (cxx_dialect > cxx11)
929 {
930 /* What we got from massage_constexpr_body is pretty much just the
931 ctor-initializer, also check the body. */
932 massaged = DECL_SAVED_TREE (fun);
933 potential = potential_rvalue_constant_expression (massaged);
934 if (!potential && complain)
935 require_potential_rvalue_constant_expression (massaged);
936 }
937 }
4f05d85a 938
37326651 939 if (!potential && complain)
bfc139e2 940 return;
2d76680f 941
87c2080b
JM
942 if (implicit)
943 {
944 if (potential)
945 {
946 DECL_DECLARED_CONSTEXPR_P (fun) = true;
947 DECL_LANG_SPECIFIC (fun)->u.fn.implicit_constexpr = true;
948 if (DECL_CONSTRUCTOR_P (fun))
949 TYPE_HAS_CONSTEXPR_CTOR (DECL_CONTEXT (fun)) = true;
950 }
951 else
952 /* Don't bother keeping the pre-generic body of unsuitable functions
953 not explicitly declared constexpr. */
954 return;
955 }
956
bfc139e2 957 constexpr_fundef entry = {fun, NULL_TREE, NULL_TREE, NULL_TREE};
43574e4f 958 bool clear_ctx = false;
43574e4f
JJ
959 if (DECL_RESULT (fun) && DECL_CONTEXT (DECL_RESULT (fun)) == NULL_TREE)
960 {
961 clear_ctx = true;
962 DECL_CONTEXT (DECL_RESULT (fun)) = fun;
963 }
bfc139e2
NS
964 tree saved_fn = current_function_decl;
965 current_function_decl = fun;
966 entry.body = copy_fn (entry.decl, entry.parms, entry.result);
43574e4f 967 current_function_decl = saved_fn;
43574e4f 968 if (clear_ctx)
bfc139e2 969 DECL_CONTEXT (DECL_RESULT (entry.decl)) = NULL_TREE;
4f05d85a
JM
970 if (!potential)
971 /* For a template instantiation, we want to remember the pre-generic body
972 for explain_invalid_constexpr_fn, but do tell cxx_eval_call_expression
973 that it doesn't need to bother trying to expand the function. */
974 entry.result = error_mark_node;
975
bfc139e2
NS
976 register_constexpr_fundef (entry);
977}
978
979/* BODY is a validated and massaged definition of a constexpr
980 function. Register it in the hash table. */
981
982void
983register_constexpr_fundef (const constexpr_fundef &value)
984{
985 /* Create the constexpr function table if necessary. */
986 if (constexpr_fundef_table == NULL)
987 constexpr_fundef_table
988 = hash_table<constexpr_fundef_hasher>::create_ggc (101);
989
990 constexpr_fundef **slot = constexpr_fundef_table->find_slot
991 (const_cast<constexpr_fundef *> (&value), INSERT);
992
2d76680f
PC
993 gcc_assert (*slot == NULL);
994 *slot = ggc_alloc<constexpr_fundef> ();
bfc139e2 995 **slot = value;
2d76680f
PC
996}
997
998/* FUN is a non-constexpr function called in a context that requires a
999 constant expression. If it comes from a constexpr template, explain why
1000 the instantiation isn't constexpr. */
1001
1002void
1003explain_invalid_constexpr_fn (tree fun)
1004{
1005 static hash_set<tree> *diagnosed;
1006 tree body;
98e5a19a 1007 /* Only diagnose defaulted functions, lambdas, or instantiations. */
2d76680f 1008 if (!DECL_DEFAULTED_FN (fun)
98e5a19a 1009 && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun))
2d76680f 1010 && !is_instantiation_of_constexpr (fun))
f22f817c
JM
1011 {
1012 inform (DECL_SOURCE_LOCATION (fun), "%qD declared here", fun);
1013 return;
1014 }
2d76680f
PC
1015 if (diagnosed == NULL)
1016 diagnosed = new hash_set<tree>;
1017 if (diagnosed->add (fun))
1018 /* Already explained. */
1019 return;
1020
17dc903e 1021 iloc_sentinel ils = input_location;
98e5a19a
JM
1022 if (!lambda_static_thunk_p (fun))
1023 {
1024 /* Diagnostics should completely ignore the static thunk, so leave
1025 input_location set to our caller's location. */
1026 input_location = DECL_SOURCE_LOCATION (fun);
1027 inform (input_location,
84fa214d 1028 "%qD is not usable as a %<constexpr%> function because:", fun);
98e5a19a 1029 }
2d76680f
PC
1030 /* First check the declaration. */
1031 if (is_valid_constexpr_fn (fun, true))
1032 {
1033 /* Then if it's OK, the body. */
98e5a19a 1034 if (!DECL_DECLARED_CONSTEXPR_P (fun)
4f05d85a 1035 && DECL_DEFAULTED_FN (fun))
2d76680f
PC
1036 explain_implicit_non_constexpr (fun);
1037 else
1038 {
4f05d85a
JM
1039 if (constexpr_fundef *fd = retrieve_constexpr_fundef (fun))
1040 body = fd->body;
1041 else
1042 body = DECL_SAVED_TREE (fun);
1043 body = massage_constexpr_body (fun, body);
2d76680f
PC
1044 require_potential_rvalue_constant_expression (body);
1045 if (DECL_CONSTRUCTOR_P (fun))
3e4b91f2 1046 cx_check_missing_mem_inits (DECL_CONTEXT (fun), body, true);
2d76680f
PC
1047 }
1048 }
2d76680f
PC
1049}
1050
1051/* Objects of this type represent calls to constexpr functions
1052 along with the bindings of parameters to their arguments, for
1053 the purpose of compile time evaluation. */
1054
1055struct GTY((for_user)) constexpr_call {
1056 /* Description of the constexpr function definition. */
1057 constexpr_fundef *fundef;
3c961dc7 1058 /* Parameter bindings environment. A TREE_VEC of arguments. */
2d76680f
PC
1059 tree bindings;
1060 /* Result of the call.
1061 NULL means the call is being evaluated.
1062 error_mark_node means that the evaluation was erroneous;
1063 otherwise, the actuall value of the call. */
1064 tree result;
1065 /* The hash of this call; we remember it here to avoid having to
1066 recalculate it when expanding the hash table. */
1067 hashval_t hash;
cce3ae91 1068 /* Whether __builtin_is_constant_evaluated() should evaluate to true. */
13de99bc 1069 bool manifestly_const_eval;
2d76680f
PC
1070};
1071
ca752f39 1072struct constexpr_call_hasher : ggc_ptr_hash<constexpr_call>
2d76680f
PC
1073{
1074 static hashval_t hash (constexpr_call *);
1075 static bool equal (constexpr_call *, constexpr_call *);
3e605b20
JM
1076};
1077
4b390698
JJ
1078enum constexpr_switch_state {
1079 /* Used when processing a switch for the first time by cxx_eval_switch_expr
1080 and default: label for that switch has not been seen yet. */
1081 css_default_not_seen,
1082 /* Used when processing a switch for the first time by cxx_eval_switch_expr
1083 and default: label for that switch has been seen already. */
1084 css_default_seen,
1085 /* Used when processing a switch for the second time by
1086 cxx_eval_switch_expr, where default: label should match. */
1087 css_default_processing
1088};
1089
8e007055
JJ
1090/* The constexpr expansion context part which needs one instance per
1091 cxx_eval_outermost_constant_expr invocation. VALUES is a map of values of
1092 variables initialized within the expression. */
1093
1094struct constexpr_global_ctx {
1095 /* Values for any temporaries or local variables within the
1096 constant-expression. */
1097 hash_map<tree,tree> values;
1098 /* Number of cxx_eval_constant_expression calls (except skipped ones,
1099 on simple constants or location wrappers) encountered during current
1100 cxx_eval_outermost_constant_expr call. */
1101 HOST_WIDE_INT constexpr_ops_count;
1102 /* Heap VAR_DECLs created during the evaluation of the outermost constant
1103 expression. */
1104 auto_vec<tree, 16> heap_vars;
ee1de08d
JJ
1105 /* Cleanups that need to be evaluated at the end of CLEANUP_POINT_EXPR. */
1106 vec<tree> *cleanups;
f74f6092
JJ
1107 /* Number of heap VAR_DECL deallocations. */
1108 unsigned heap_dealloc_count;
8e007055 1109 /* Constructor. */
f74f6092
JJ
1110 constexpr_global_ctx ()
1111 : constexpr_ops_count (0), cleanups (NULL), heap_dealloc_count (0) {}
8e007055
JJ
1112};
1113
3e605b20
JM
1114/* The constexpr expansion context. CALL is the current function
1115 expansion, CTOR is the current aggregate initializer, OBJECT is the
8e007055 1116 object being initialized by CTOR, either a VAR_DECL or a _REF. */
3e605b20
JM
1117
1118struct constexpr_ctx {
8e007055
JJ
1119 /* The part of the context that needs to be unique to the whole
1120 cxx_eval_outermost_constant_expr invocation. */
1121 constexpr_global_ctx *global;
13f649f6 1122 /* The innermost call we're evaluating. */
3e605b20 1123 constexpr_call *call;
ee1de08d
JJ
1124 /* SAVE_EXPRs and TARGET_EXPR_SLOT vars of TARGET_EXPRs that we've seen
1125 within the current LOOP_EXPR. NULL if we aren't inside a loop. */
0ee28590 1126 vec<tree> *save_exprs;
13f649f6
JM
1127 /* The CONSTRUCTOR we're currently building up for an aggregate
1128 initializer. */
3e605b20 1129 tree ctor;
13f649f6 1130 /* The object we're building the CONSTRUCTOR for. */
3e605b20 1131 tree object;
4b390698
JJ
1132 /* If inside SWITCH_EXPR. */
1133 constexpr_switch_state *css_state;
49a86fce
PP
1134 /* The aggregate initialization context inside which this one is nested. This
1135 is used by lookup_placeholder to resolve PLACEHOLDER_EXPRs. */
1136 const constexpr_ctx *parent;
a15ffa22 1137
aaa26bf4
JM
1138 /* Whether we should error on a non-constant expression or fail quietly.
1139 This flag needs to be here, but some of the others could move to global
1140 if they get larger than a word. */
2b3ab879 1141 bool quiet;
13f649f6
JM
1142 /* Whether we are strictly conforming to constant expression rules or
1143 trying harder to get a constant value. */
69eb4fde 1144 bool strict;
e4082611 1145 /* Whether __builtin_is_constant_evaluated () should be true. */
13de99bc 1146 bool manifestly_const_eval;
3e605b20 1147};
2d76680f 1148
f65a3299
PP
1149/* This internal flag controls whether we should avoid doing anything during
1150 constexpr evaluation that would cause extra DECL_UID generation, such as
1151 template instantiation and function body copying. */
1152
1153static bool uid_sensitive_constexpr_evaluation_value;
1154
1155/* An internal counter that keeps track of the number of times
1156 uid_sensitive_constexpr_evaluation_p returned true. */
1157
1158static unsigned uid_sensitive_constexpr_evaluation_true_counter;
1159
1160/* The accessor for uid_sensitive_constexpr_evaluation_value which also
1161 increments the corresponding counter. */
1162
1163static bool
1164uid_sensitive_constexpr_evaluation_p ()
1165{
1166 if (uid_sensitive_constexpr_evaluation_value)
1167 {
1168 ++uid_sensitive_constexpr_evaluation_true_counter;
1169 return true;
1170 }
1171 else
1172 return false;
1173}
1174
1175/* The default constructor for uid_sensitive_constexpr_evaluation_sentinel
1176 enables the internal flag for uid_sensitive_constexpr_evaluation_p
1177 during the lifetime of the sentinel object. Upon its destruction, the
1178 previous value of uid_sensitive_constexpr_evaluation_p is restored. */
1179
1180uid_sensitive_constexpr_evaluation_sentinel
1181::uid_sensitive_constexpr_evaluation_sentinel ()
1182 : ovr (uid_sensitive_constexpr_evaluation_value, true)
1183{
1184}
1185
1186/* The default constructor for uid_sensitive_constexpr_evaluation_checker
1187 records the current number of times that uid_sensitive_constexpr_evaluation_p
1188 has been called and returned true. */
1189
1190uid_sensitive_constexpr_evaluation_checker
1191::uid_sensitive_constexpr_evaluation_checker ()
1192 : saved_counter (uid_sensitive_constexpr_evaluation_true_counter)
1193{
1194}
1195
1196/* Returns true iff uid_sensitive_constexpr_evaluation_p is true, and
1197 some constexpr evaluation was restricted due to u_s_c_e_p being called
1198 and returning true during the lifetime of this checker object. */
1199
1200bool
1201uid_sensitive_constexpr_evaluation_checker::evaluation_restricted_p () const
1202{
1203 return (uid_sensitive_constexpr_evaluation_value
1204 && saved_counter != uid_sensitive_constexpr_evaluation_true_counter);
1205}
1206
1207
2d76680f
PC
1208/* A table of all constexpr calls that have been evaluated by the
1209 compiler in this translation unit. */
1210
97f3003f 1211static GTY (()) hash_table<constexpr_call_hasher> *constexpr_call_table;
2d76680f 1212
3e605b20 1213static tree cxx_eval_constant_expression (const constexpr_ctx *, tree,
5a804683 1214 bool, bool *, bool *, tree * = NULL);
2d76680f
PC
1215
1216/* Compute a hash value for a constexpr call representation. */
1217
1218inline hashval_t
1219constexpr_call_hasher::hash (constexpr_call *info)
1220{
1221 return info->hash;
1222}
1223
1224/* Return true if the objects pointed to by P and Q represent calls
1225 to the same constexpr function with the same arguments.
1226 Otherwise, return false. */
1227
1228bool
1229constexpr_call_hasher::equal (constexpr_call *lhs, constexpr_call *rhs)
1230{
2d76680f 1231 if (lhs == rhs)
46cf7fa1
JJ
1232 return true;
1233 if (lhs->hash != rhs->hash)
1234 return false;
13de99bc 1235 if (lhs->manifestly_const_eval != rhs->manifestly_const_eval)
cce3ae91 1236 return false;
2d76680f 1237 if (!constexpr_fundef_hasher::equal (lhs->fundef, rhs->fundef))
46cf7fa1 1238 return false;
3c961dc7 1239 return cp_tree_equal (lhs->bindings, rhs->bindings);
2d76680f
PC
1240}
1241
1242/* Initialize the constexpr call table, if needed. */
1243
1244static void
1245maybe_initialize_constexpr_call_table (void)
1246{
1247 if (constexpr_call_table == NULL)
1248 constexpr_call_table = hash_table<constexpr_call_hasher>::create_ggc (101);
1249}
1250
c0daf32d
PP
1251/* During constexpr CALL_EXPR evaluation, to avoid issues with sharing when
1252 a function happens to get called recursively, we unshare the callee
1253 function's body and evaluate this unshared copy instead of evaluating the
1254 original body.
1255
1256 FUNDEF_COPIES_TABLE is a per-function freelist of these unshared function
1257 copies. The underlying data structure of FUNDEF_COPIES_TABLE is a hash_map
97f3003f
JM
1258 that's keyed off of the original FUNCTION_DECL and whose value is a
1259 TREE_LIST of this function's unused copies awaiting reuse.
c0daf32d 1260
97f3003f 1261 This is not GC-deletable to avoid GC affecting UID generation. */
c0daf32d 1262
ba6730bd 1263static GTY(()) decl_tree_map *fundef_copies_table;
c0daf32d 1264
c0daf32d 1265/* Reuse a copy or create a new unshared copy of the function FUN.
3c98ff9b
NS
1266 Return this copy. We use a TREE_LIST whose PURPOSE is body, VALUE
1267 is parms, TYPE is result. */
c0daf32d 1268
97f3003f 1269static tree
f65a3299 1270get_fundef_copy (constexpr_fundef *fundef)
c0daf32d 1271{
97f3003f 1272 tree copy;
3c98ff9b 1273 bool existed;
c89844e5
JM
1274 tree *slot = &(hash_map_safe_get_or_insert<hm_ggc>
1275 (fundef_copies_table, fundef->decl, &existed, 127));
3c98ff9b
NS
1276
1277 if (!existed)
c0daf32d 1278 {
3c98ff9b
NS
1279 /* There is no cached function available, or in use. We can use
1280 the function directly. That the slot is now created records
1281 that this function is now in use. */
43574e4f
JJ
1282 copy = build_tree_list (fundef->body, fundef->parms);
1283 TREE_TYPE (copy) = fundef->result;
3c98ff9b
NS
1284 }
1285 else if (*slot == NULL_TREE)
1286 {
f65a3299 1287 if (uid_sensitive_constexpr_evaluation_p ())
aaa26bf4
JM
1288 return NULL_TREE;
1289
3c98ff9b 1290 /* We've already used the function itself, so make a copy. */
97f3003f 1291 copy = build_tree_list (NULL, NULL);
43574e4f
JJ
1292 tree saved_body = DECL_SAVED_TREE (fundef->decl);
1293 tree saved_parms = DECL_ARGUMENTS (fundef->decl);
1294 tree saved_result = DECL_RESULT (fundef->decl);
1295 tree saved_fn = current_function_decl;
1296 DECL_SAVED_TREE (fundef->decl) = fundef->body;
1297 DECL_ARGUMENTS (fundef->decl) = fundef->parms;
1298 DECL_RESULT (fundef->decl) = fundef->result;
1299 current_function_decl = fundef->decl;
1300 TREE_PURPOSE (copy) = copy_fn (fundef->decl, TREE_VALUE (copy),
1301 TREE_TYPE (copy));
1302 current_function_decl = saved_fn;
1303 DECL_RESULT (fundef->decl) = saved_result;
1304 DECL_ARGUMENTS (fundef->decl) = saved_parms;
1305 DECL_SAVED_TREE (fundef->decl) = saved_body;
c0daf32d
PP
1306 }
1307 else
1308 {
3c98ff9b 1309 /* We have a cached function available. */
c0daf32d 1310 copy = *slot;
97f3003f 1311 *slot = TREE_CHAIN (copy);
c0daf32d
PP
1312 }
1313
1314 return copy;
1315}
1316
3c98ff9b
NS
1317/* Save the copy COPY of function FUN for later reuse by
1318 get_fundef_copy(). By construction, there will always be an entry
1319 to find. */
c0daf32d
PP
1320
1321static void
97f3003f 1322save_fundef_copy (tree fun, tree copy)
c0daf32d 1323{
3c98ff9b 1324 tree *slot = fundef_copies_table->get (fun);
97f3003f 1325 TREE_CHAIN (copy) = *slot;
c0daf32d
PP
1326 *slot = copy;
1327}
1328
2d76680f
PC
1329/* We have an expression tree T that represents a call, either CALL_EXPR
1330 or AGGR_INIT_EXPR. Return the Nth argument. */
1331
1332static inline tree
1333get_nth_callarg (tree t, int n)
1334{
1335 switch (TREE_CODE (t))
1336 {
1337 case CALL_EXPR:
1338 return CALL_EXPR_ARG (t, n);
1339
1340 case AGGR_INIT_EXPR:
1341 return AGGR_INIT_EXPR_ARG (t, n);
1342
1343 default:
1344 gcc_unreachable ();
1345 return NULL;
1346 }
1347}
1348
2d76680f
PC
1349/* Attempt to evaluate T which represents a call to a builtin function.
1350 We assume here that all builtin functions evaluate to scalar types
1351 represented by _CST nodes. */
1352
1353static tree
5756d0f9 1354cxx_eval_builtin_function_call (const constexpr_ctx *ctx, tree t, tree fun,
92a596e8 1355 bool lval,
2d76680f
PC
1356 bool *non_constant_p, bool *overflow_p)
1357{
1358 const int nargs = call_expr_nargs (t);
1359 tree *args = (tree *) alloca (nargs * sizeof (tree));
1360 tree new_call;
1361 int i;
5756d0f9
JM
1362
1363 /* Don't fold __builtin_constant_p within a constexpr function. */
e4082611 1364 bool bi_const_p = DECL_IS_BUILTIN_CONSTANT_P (fun);
b925d25d 1365
fe736ffd
JM
1366 /* If we aren't requiring a constant expression, defer __builtin_constant_p
1367 in a constexpr function until we have values for the parameters. */
b925d25d 1368 if (bi_const_p
4cd3e7df 1369 && !ctx->manifestly_const_eval
5756d0f9
JM
1370 && current_function_decl
1371 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
2d76680f 1372 {
5756d0f9
JM
1373 *non_constant_p = true;
1374 return t;
2d76680f 1375 }
5756d0f9 1376
e4082611 1377 /* For __builtin_is_constant_evaluated, defer it if not
117c6426
JJ
1378 ctx->manifestly_const_eval (as sometimes we try to constant evaluate
1379 without manifestly_const_eval even expressions or parts thereof which
1380 will later be manifestly const_eval evaluated), otherwise fold it to
1381 true. */
3d78e008 1382 if (fndecl_built_in_p (fun, CP_BUILT_IN_IS_CONSTANT_EVALUATED,
13de99bc 1383 BUILT_IN_FRONTEND))
e4082611 1384 {
13de99bc 1385 if (!ctx->manifestly_const_eval)
e4082611
JJ
1386 {
1387 *non_constant_p = true;
1388 return t;
1389 }
1390 return boolean_true_node;
1391 }
1392
ff603745 1393 if (fndecl_built_in_p (fun, CP_BUILT_IN_SOURCE_LOCATION, BUILT_IN_FRONTEND))
ba3d8dff
JJ
1394 {
1395 temp_override<tree> ovr (current_function_decl);
1396 if (ctx->call && ctx->call->fundef)
1397 current_function_decl = ctx->call->fundef->decl;
1398 return fold_builtin_source_location (EXPR_LOCATION (t));
1399 }
ff603745 1400
69dc042f
JM
1401 int strops = 0;
1402 int strret = 0;
1403 if (fndecl_built_in_p (fun, BUILT_IN_NORMAL))
1404 switch (DECL_FUNCTION_CODE (fun))
1405 {
1406 case BUILT_IN_STRLEN:
1407 case BUILT_IN_STRNLEN:
1408 strops = 1;
1409 break;
1410 case BUILT_IN_MEMCHR:
1411 case BUILT_IN_STRCHR:
1412 case BUILT_IN_STRRCHR:
1413 strops = 1;
1414 strret = 1;
1415 break;
1416 case BUILT_IN_MEMCMP:
1417 case BUILT_IN_STRCMP:
1418 strops = 2;
1419 break;
1420 case BUILT_IN_STRSTR:
1421 strops = 2;
1422 strret = 1;
bc13106e
JJ
1423 break;
1424 case BUILT_IN_ASAN_POINTER_COMPARE:
1425 case BUILT_IN_ASAN_POINTER_SUBTRACT:
1426 /* These builtins shall be ignored during constant expression
1427 evaluation. */
1428 return void_node;
69dc042f
JM
1429 default:
1430 break;
1431 }
1432
5756d0f9
JM
1433 /* Be permissive for arguments to built-ins; __builtin_constant_p should
1434 return constant false for a non-constant argument. */
1435 constexpr_ctx new_ctx = *ctx;
1436 new_ctx.quiet = true;
5756d0f9 1437 for (i = 0; i < nargs; ++i)
b925d25d 1438 {
69dc042f 1439 tree arg = CALL_EXPR_ARG (t, i);
6f346913 1440 tree oarg = arg;
69dc042f
JM
1441
1442 /* To handle string built-ins we need to pass ADDR_EXPR<STRING_CST> since
1443 expand_builtin doesn't know how to look in the values table. */
1444 bool strop = i < strops;
1445 if (strop)
1446 {
1447 STRIP_NOPS (arg);
1448 if (TREE_CODE (arg) == ADDR_EXPR)
1449 arg = TREE_OPERAND (arg, 0);
1450 else
1451 strop = false;
1452 }
1453
2ff7172a
JJ
1454 /* If builtin_valid_in_constant_expr_p is true,
1455 potential_constant_expression_1 has not recursed into the arguments
1456 of the builtin, verify it here. */
1457 if (!builtin_valid_in_constant_expr_p (fun)
69dc042f 1458 || potential_constant_expression (arg))
4cd3e7df
JJ
1459 {
1460 bool dummy1 = false, dummy2 = false;
69dc042f
JM
1461 arg = cxx_eval_constant_expression (&new_ctx, arg, false,
1462 &dummy1, &dummy2);
4cd3e7df
JJ
1463 }
1464
b925d25d 1465 if (bi_const_p)
4cd3e7df 1466 /* For __builtin_constant_p, fold all expressions with constant values
b925d25d 1467 even if they aren't C++ constant-expressions. */
69dc042f
JM
1468 arg = cp_fold_rvalue (arg);
1469 else if (strop)
1470 {
1471 if (TREE_CODE (arg) == CONSTRUCTOR)
1472 arg = braced_lists_to_strings (TREE_TYPE (arg), arg);
1473 if (TREE_CODE (arg) == STRING_CST)
1474 arg = build_address (arg);
6f346913
JM
1475 else
1476 arg = oarg;
69dc042f
JM
1477 }
1478
1479 args[i] = arg;
b925d25d 1480 }
5756d0f9
JM
1481
1482 bool save_ffbcp = force_folding_builtin_constant_p;
b09a67ea 1483 force_folding_builtin_constant_p |= ctx->manifestly_const_eval;
43574e4f
JJ
1484 tree save_cur_fn = current_function_decl;
1485 /* Return name of ctx->call->fundef->decl for __builtin_FUNCTION (). */
1486 if (fndecl_built_in_p (fun, BUILT_IN_FUNCTION)
1487 && ctx->call
1488 && ctx->call->fundef)
1489 current_function_decl = ctx->call->fundef->decl;
6cd005a2
JJ
1490 if (fndecl_built_in_p (fun,
1491 CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS,
1492 BUILT_IN_FRONTEND))
1493 {
1494 location_t loc = EXPR_LOCATION (t);
1495 if (nargs >= 1)
1496 VERIFY_CONSTANT (args[0]);
1497 new_call
1498 = fold_builtin_is_pointer_inverconvertible_with_class (loc, nargs,
1499 args);
1500 }
32c3a753
JJ
1501 else if (fndecl_built_in_p (fun,
1502 CP_BUILT_IN_IS_CORRESPONDING_MEMBER,
1503 BUILT_IN_FRONTEND))
1504 {
1505 location_t loc = EXPR_LOCATION (t);
1506 if (nargs >= 2)
1507 {
1508 VERIFY_CONSTANT (args[0]);
1509 VERIFY_CONSTANT (args[1]);
1510 }
1511 new_call = fold_builtin_is_corresponding_member (loc, nargs, args);
1512 }
6cd005a2
JJ
1513 else
1514 new_call = fold_builtin_call_array (EXPR_LOCATION (t), TREE_TYPE (t),
1515 CALL_EXPR_FN (t), nargs, args);
43574e4f 1516 current_function_decl = save_cur_fn;
5756d0f9 1517 force_folding_builtin_constant_p = save_ffbcp;
109d2197
JJ
1518 if (new_call == NULL)
1519 {
1520 if (!*non_constant_p && !ctx->quiet)
1521 {
1b6fa695
ML
1522 /* Do not allow__builtin_unreachable in constexpr function.
1523 The __builtin_unreachable call with BUILTINS_LOCATION
1524 comes from cp_maybe_instrument_return. */
3d78e008 1525 if (fndecl_built_in_p (fun, BUILT_IN_UNREACHABLE)
1b6fa695 1526 && EXPR_LOCATION (t) == BUILTINS_LOCATION)
84fa214d 1527 error ("%<constexpr%> call flows off the end of the function");
1b6fa695
ML
1528 else
1529 {
1530 new_call = build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t),
1531 CALL_EXPR_FN (t), nargs, args);
1532 error ("%q+E is not a constant expression", new_call);
1533 }
109d2197
JJ
1534 }
1535 *non_constant_p = true;
1536 return t;
1537 }
1538
43574e4f 1539 if (!potential_constant_expression (new_call))
109d2197
JJ
1540 {
1541 if (!*non_constant_p && !ctx->quiet)
1542 error ("%q+E is not a constant expression", new_call);
1543 *non_constant_p = true;
1544 return t;
1545 }
1546
69dc042f
JM
1547 if (strret)
1548 {
1549 /* memchr returns a pointer into the first argument, but we replaced the
1550 argument above with a STRING_CST; put it back it now. */
1551 tree op = CALL_EXPR_ARG (t, strret-1);
1552 STRIP_NOPS (new_call);
1553 if (TREE_CODE (new_call) == POINTER_PLUS_EXPR)
1554 TREE_OPERAND (new_call, 0) = op;
1555 else if (TREE_CODE (new_call) == ADDR_EXPR)
1556 new_call = op;
1557 }
1558
109d2197
JJ
1559 return cxx_eval_constant_expression (&new_ctx, new_call, lval,
1560 non_constant_p, overflow_p);
2d76680f
PC
1561}
1562
1563/* TEMP is the constant value of a temporary object of type TYPE. Adjust
1564 the type of the value to match. */
1565
1566static tree
1567adjust_temp_type (tree type, tree temp)
1568{
47be9509 1569 if (same_type_p (TREE_TYPE (temp), type))
2d76680f
PC
1570 return temp;
1571 /* Avoid wrapping an aggregate value in a NOP_EXPR. */
1572 if (TREE_CODE (temp) == CONSTRUCTOR)
b27f74e7
MP
1573 {
1574 /* build_constructor wouldn't retain various CONSTRUCTOR flags. */
1575 tree t = copy_node (temp);
1576 TREE_TYPE (t) = type;
1577 return t;
1578 }
ff8ba86f
JJ
1579 if (TREE_CODE (temp) == EMPTY_CLASS_EXPR)
1580 return build0 (EMPTY_CLASS_EXPR, type);
2d76680f 1581 gcc_assert (scalarish_type_p (type));
72b091f7
MP
1582 /* Now we know we're dealing with a scalar, and a prvalue of non-class
1583 type is cv-unqualified. */
1584 return cp_fold_convert (cv_unqualified (type), temp);
2d76680f
PC
1585}
1586
9b9eb42a
JM
1587/* If T is a CONSTRUCTOR, return an unshared copy of T and any
1588 sub-CONSTRUCTORs. Otherwise return T.
0146e25f 1589
9b9eb42a
JM
1590 We use this whenever we initialize an object as a whole, whether it's a
1591 parameter, a local variable, or a subobject, so that subsequent
1592 modifications don't affect other places where it was used. */
0146e25f 1593
1f6857ba 1594tree
3da7d774 1595unshare_constructor (tree t MEM_STAT_DECL)
0146e25f 1596{
9b9eb42a
JM
1597 if (!t || TREE_CODE (t) != CONSTRUCTOR)
1598 return t;
1599 auto_vec <tree*, 4> ptrs;
1600 ptrs.safe_push (&t);
1601 while (!ptrs.is_empty ())
1602 {
1603 tree *p = ptrs.pop ();
3da7d774
JM
1604 tree n = copy_node (*p PASS_MEM_STAT);
1605 CONSTRUCTOR_ELTS (n) = vec_safe_copy (CONSTRUCTOR_ELTS (*p) PASS_MEM_STAT);
9b9eb42a
JM
1606 *p = n;
1607 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (n);
1608 constructor_elt *ce;
1609 for (HOST_WIDE_INT i = 0; vec_safe_iterate (v, i, &ce); ++i)
6b7d53a2 1610 if (ce->value && TREE_CODE (ce->value) == CONSTRUCTOR)
9b9eb42a
JM
1611 ptrs.safe_push (&ce->value);
1612 }
0146e25f
PP
1613 return t;
1614}
1615
620adbec
JM
1616/* If T is a CONSTRUCTOR, ggc_free T and any sub-CONSTRUCTORs. */
1617
1618static void
1619free_constructor (tree t)
1620{
1621 if (!t || TREE_CODE (t) != CONSTRUCTOR)
1622 return;
1623 releasing_vec ctors;
1624 vec_safe_push (ctors, t);
1625 while (!ctors->is_empty ())
1626 {
1627 tree c = ctors->pop ();
1628 if (vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (c))
1629 {
1630 constructor_elt *ce;
1631 for (HOST_WIDE_INT i = 0; vec_safe_iterate (elts, i, &ce); ++i)
1632 if (TREE_CODE (ce->value) == CONSTRUCTOR)
1633 vec_safe_push (ctors, ce->value);
1634 ggc_free (elts);
1635 }
1636 ggc_free (c);
1637 }
1638}
1639
559d2f1e
JJ
1640/* Helper function of cxx_bind_parameters_in_call. Return non-NULL
1641 if *TP is address of a static variable (or part of it) currently being
1642 constructed or of a heap artificial variable. */
1643
1644static tree
1645addr_of_non_const_var (tree *tp, int *walk_subtrees, void *data)
1646{
1647 if (TREE_CODE (*tp) == ADDR_EXPR)
1648 if (tree var = get_base_address (TREE_OPERAND (*tp, 0)))
1649 if (VAR_P (var) && TREE_STATIC (var))
1650 {
1651 if (DECL_NAME (var) == heap_uninit_identifier
1652 || DECL_NAME (var) == heap_identifier
1653 || DECL_NAME (var) == heap_vec_uninit_identifier
1654 || DECL_NAME (var) == heap_vec_identifier)
1655 return var;
1656
1657 constexpr_global_ctx *global = (constexpr_global_ctx *) data;
1658 if (global->values.get (var))
1659 return var;
1660 }
1661 if (TYPE_P (*tp))
1662 *walk_subtrees = false;
1663 return NULL_TREE;
1664}
1665
2d76680f
PC
1666/* Subroutine of cxx_eval_call_expression.
1667 We are processing a call expression (either CALL_EXPR or
3e605b20 1668 AGGR_INIT_EXPR) in the context of CTX. Evaluate
2d76680f
PC
1669 all arguments and bind their values to correspondings
1670 parameters, making up the NEW_CALL context. */
1671
1595fe44
JM
1672static tree
1673cxx_bind_parameters_in_call (const constexpr_ctx *ctx, tree t, tree fun,
12d9ce19
JM
1674 bool *non_constant_p, bool *overflow_p,
1675 bool *non_constant_args)
2d76680f
PC
1676{
1677 const int nargs = call_expr_nargs (t);
1595fe44 1678 tree parms = DECL_ARGUMENTS (fun);
2d76680f 1679 int i;
3c961dc7
JM
1680 /* We don't record ellipsis args below. */
1681 int nparms = list_length (parms);
1682 int nbinds = nargs < nparms ? nargs : nparms;
1595fe44 1683 tree binds = make_tree_vec (nbinds);
2d76680f
PC
1684 for (i = 0; i < nargs; ++i)
1685 {
1686 tree x, arg;
1687 tree type = parms ? TREE_TYPE (parms) : void_type_node;
1595fe44
JM
1688 if (parms && DECL_BY_REFERENCE (parms))
1689 type = TREE_TYPE (type);
2d76680f 1690 x = get_nth_callarg (t, i);
3e605b20
JM
1691 /* For member function, the first argument is a pointer to the implied
1692 object. For a constructor, it might still be a dummy object, in
60813a46 1693 which case we get the real argument from ctx. */
3e605b20
JM
1694 if (i == 0 && DECL_CONSTRUCTOR_P (fun)
1695 && is_dummy_object (x))
1696 {
1697 x = ctx->object;
84dd815f 1698 x = build_address (x);
3e605b20 1699 }
43574e4f
JJ
1700 if (TREE_ADDRESSABLE (type))
1701 /* Undo convert_for_arg_passing work here. */
1702 x = convert_from_reference (x);
6c59b8a9
JM
1703 /* Normally we would strip a TARGET_EXPR in an initialization context
1704 such as this, but here we do the elision differently: we keep the
1705 TARGET_EXPR, and use its CONSTRUCTOR as the value of the parm. */
0e038601 1706 arg = cxx_eval_constant_expression (ctx, x, /*lval=*/false,
5a804683 1707 non_constant_p, overflow_p);
2d76680f 1708 /* Don't VERIFY_CONSTANT here. */
2b3ab879 1709 if (*non_constant_p && ctx->quiet)
1595fe44 1710 break;
2d76680f
PC
1711 /* Just discard ellipsis args after checking their constantitude. */
1712 if (!parms)
1713 continue;
4727d4c6
NS
1714
1715 if (!*non_constant_p)
1716 {
1717 /* Make sure the binding has the same type as the parm. But
1718 only for constant args. */
9f613f06 1719 if (!TYPE_REF_P (type))
4727d4c6
NS
1720 arg = adjust_temp_type (type, arg);
1721 if (!TREE_CONSTANT (arg))
1722 *non_constant_args = true;
5afd90c5
JJ
1723 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
1724 /* The destructor needs to see any modifications the callee makes
1725 to the argument. */
1726 *non_constant_args = true;
559d2f1e
JJ
1727 /* If arg is or contains address of a heap artificial variable or
1728 of a static variable being constructed, avoid caching the
1729 function call, as those variables might be modified by the
1730 function, or might be modified by the callers in between
1731 the cached function and just read by the function. */
1732 else if (!*non_constant_args
1733 && cp_walk_tree (&arg, addr_of_non_const_var, ctx->global,
1734 NULL))
1735 *non_constant_args = true;
5afd90c5 1736
5558a0da
JJ
1737 /* For virtual calls, adjust the this argument, so that it is
1738 the object on which the method is called, rather than
1739 one of its bases. */
1740 if (i == 0 && DECL_VIRTUAL_P (fun))
1741 {
1742 tree addr = arg;
1743 STRIP_NOPS (addr);
1744 if (TREE_CODE (addr) == ADDR_EXPR)
1745 {
1746 tree obj = TREE_OPERAND (addr, 0);
1747 while (TREE_CODE (obj) == COMPONENT_REF
1748 && DECL_FIELD_IS_BASE (TREE_OPERAND (obj, 1))
1749 && !same_type_ignoring_top_level_qualifiers_p
1750 (TREE_TYPE (obj), DECL_CONTEXT (fun)))
1751 obj = TREE_OPERAND (obj, 0);
1752 if (obj != TREE_OPERAND (addr, 0))
1753 arg = build_fold_addr_expr_with_type (obj,
1754 TREE_TYPE (arg));
1755 }
1756 }
3c961dc7 1757 TREE_VEC_ELT (binds, i) = arg;
4727d4c6 1758 }
2d76680f
PC
1759 parms = TREE_CHAIN (parms);
1760 }
1595fe44
JM
1761
1762 return binds;
2d76680f
PC
1763}
1764
1765/* Variables and functions to manage constexpr call expansion context.
1766 These do not need to be marked for PCH or GC. */
1767
1768/* FIXME remember and print actual constant arguments. */
7de76362 1769static vec<tree> call_stack;
2d76680f
PC
1770static int call_stack_tick;
1771static int last_cx_error_tick;
1772
7ffc7de5 1773static int
2d76680f
PC
1774push_cx_call_context (tree call)
1775{
1776 ++call_stack_tick;
1777 if (!EXPR_HAS_LOCATION (call))
1778 SET_EXPR_LOCATION (call, input_location);
1779 call_stack.safe_push (call);
7ffc7de5
JM
1780 int len = call_stack.length ();
1781 if (len > max_constexpr_depth)
2d76680f 1782 return false;
7ffc7de5 1783 return len;
2d76680f
PC
1784}
1785
1786static void
1787pop_cx_call_context (void)
1788{
1789 ++call_stack_tick;
1790 call_stack.pop ();
1791}
1792
1793vec<tree>
1794cx_error_context (void)
1795{
1796 vec<tree> r = vNULL;
1797 if (call_stack_tick != last_cx_error_tick
1798 && !call_stack.is_empty ())
1799 r = call_stack;
1800 last_cx_error_tick = call_stack_tick;
1801 return r;
1802}
1803
44a845ca
MS
1804/* Evaluate a call T to a GCC internal function when possible and return
1805 the evaluated result or, under the control of CTX, give an error, set
1806 NON_CONSTANT_P, and return the unevaluated call T otherwise. */
1807
1808static tree
1809cxx_eval_internal_function (const constexpr_ctx *ctx, tree t,
1810 bool lval,
1811 bool *non_constant_p, bool *overflow_p)
1812{
1813 enum tree_code opcode = ERROR_MARK;
1814
1815 switch (CALL_EXPR_IFN (t))
1816 {
1817 case IFN_UBSAN_NULL:
1818 case IFN_UBSAN_BOUNDS:
1819 case IFN_UBSAN_VPTR:
81fea426 1820 case IFN_FALLTHROUGH:
44a845ca
MS
1821 return void_node;
1822
1823 case IFN_ADD_OVERFLOW:
1824 opcode = PLUS_EXPR;
1825 break;
1826 case IFN_SUB_OVERFLOW:
1827 opcode = MINUS_EXPR;
1828 break;
1829 case IFN_MUL_OVERFLOW:
1830 opcode = MULT_EXPR;
1831 break;
1832
e16f1cc7
JJ
1833 case IFN_LAUNDER:
1834 return cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0),
1835 false, non_constant_p, overflow_p);
1836
d8fcab68
JJ
1837 case IFN_VEC_CONVERT:
1838 {
1839 tree arg = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0),
1840 false, non_constant_p,
1841 overflow_p);
1842 if (TREE_CODE (arg) == VECTOR_CST)
84993d94
JJ
1843 if (tree r = fold_const_call (CFN_VEC_CONVERT, TREE_TYPE (t), arg))
1844 return r;
d8fcab68 1845 }
84993d94 1846 /* FALLTHRU */
d8fcab68 1847
44a845ca
MS
1848 default:
1849 if (!ctx->quiet)
f9d0ca40 1850 error_at (cp_expr_loc_or_input_loc (t),
44a845ca
MS
1851 "call to internal function %qE", t);
1852 *non_constant_p = true;
1853 return t;
1854 }
1855
1856 /* Evaluate constant arguments using OPCODE and return a complex
1857 number containing the result and the overflow bit. */
1858 tree arg0 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), lval,
1859 non_constant_p, overflow_p);
1860 tree arg1 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 1), lval,
1861 non_constant_p, overflow_p);
1862
1863 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1864 {
f9d0ca40 1865 location_t loc = cp_expr_loc_or_input_loc (t);
44a845ca
MS
1866 tree type = TREE_TYPE (TREE_TYPE (t));
1867 tree result = fold_binary_loc (loc, opcode, type,
1868 fold_convert_loc (loc, type, arg0),
1869 fold_convert_loc (loc, type, arg1));
1870 tree ovf
1871 = build_int_cst (type, arith_overflowed_p (opcode, type, arg0, arg1));
1872 /* Reset TREE_OVERFLOW to avoid warnings for the overflow. */
1873 if (TREE_OVERFLOW (result))
1874 TREE_OVERFLOW (result) = 0;
1875
1876 return build_complex (TREE_TYPE (t), result, ovf);
1877 }
1878
1879 *non_constant_p = true;
1880 return t;
1881}
1882
e8c48716 1883/* Clean CONSTRUCTOR_NO_CLEARING from CTOR and its sub-aggregates. */
ecc57615
JM
1884
1885static void
1886clear_no_implicit_zero (tree ctor)
1887{
e8c48716 1888 if (CONSTRUCTOR_NO_CLEARING (ctor))
ecc57615 1889 {
e8c48716 1890 CONSTRUCTOR_NO_CLEARING (ctor) = false;
fae00a0a
JM
1891 for (auto &e: CONSTRUCTOR_ELTS (ctor))
1892 if (TREE_CODE (e.value) == CONSTRUCTOR)
1893 clear_no_implicit_zero (e.value);
ecc57615
JM
1894 }
1895}
1896
04e1749c
MP
1897/* Complain about a const object OBJ being modified in a constant expression.
1898 EXPR is the MODIFY_EXPR expression performing the modification. */
1899
1900static void
1901modifying_const_object_error (tree expr, tree obj)
1902{
1903 location_t loc = cp_expr_loc_or_input_loc (expr);
1904 auto_diagnostic_group d;
1905 error_at (loc, "modifying a const object %qE is not allowed in "
1906 "a constant expression", TREE_OPERAND (expr, 0));
1907 inform (location_of (obj), "originally declared %<const%> here");
1908}
1909
8e007055
JJ
1910/* Return true if FNDECL is a replaceable global allocation function that
1911 should be useable during constant expression evaluation. */
1912
1913static inline bool
1914cxx_replaceable_global_alloc_fn (tree fndecl)
1915{
b04445d4 1916 return (cxx_dialect >= cxx20
8e007055 1917 && IDENTIFIER_NEWDEL_OP_P (DECL_NAME (fndecl))
cf650568
JJ
1918 && CP_DECL_CONTEXT (fndecl) == global_namespace
1919 && (DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl)
1920 || DECL_IS_OPERATOR_DELETE_P (fndecl)));
1921}
1922
1923/* Return true if FNDECL is a placement new function that should be
1924 useable during constant expression evaluation of std::construct_at. */
1925
1926static inline bool
1927cxx_placement_new_fn (tree fndecl)
1928{
b04445d4 1929 if (cxx_dialect >= cxx20
cf650568
JJ
1930 && IDENTIFIER_NEW_OP_P (DECL_NAME (fndecl))
1931 && CP_DECL_CONTEXT (fndecl) == global_namespace
1932 && !DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl)
1933 && TREE_CODE (TREE_TYPE (fndecl)) == FUNCTION_TYPE)
1934 {
1935 tree first_arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
1936 if (TREE_VALUE (first_arg) == ptr_type_node
1937 && TREE_CHAIN (first_arg) == void_list_node)
1938 return true;
1939 }
1940 return false;
1941}
1942
1943/* Return true if FNDECL is std::construct_at. */
1944
1945static inline bool
1946is_std_construct_at (tree fndecl)
1947{
1948 if (!decl_in_std_namespace_p (fndecl))
1949 return false;
1950
1951 tree name = DECL_NAME (fndecl);
1952 return name && id_equal (name, "construct_at");
8e007055
JJ
1953}
1954
2ffc2645
MP
1955/* Overload for the above taking constexpr_call*. */
1956
1957static inline bool
1958is_std_construct_at (const constexpr_call *call)
1959{
1960 return (call
1961 && call->fundef
1962 && is_std_construct_at (call->fundef->decl));
1963}
1964
8412b939
JJ
1965/* Return true if FNDECL is std::allocator<T>::{,de}allocate. */
1966
1967static inline bool
1968is_std_allocator_allocate (tree fndecl)
1969{
1970 tree name = DECL_NAME (fndecl);
1971 if (name == NULL_TREE
1972 || !(id_equal (name, "allocate") || id_equal (name, "deallocate")))
1973 return false;
1974
1975 tree ctx = DECL_CONTEXT (fndecl);
1976 if (ctx == NULL_TREE || !CLASS_TYPE_P (ctx) || !TYPE_MAIN_DECL (ctx))
1977 return false;
1978
1979 tree decl = TYPE_MAIN_DECL (ctx);
1980 name = DECL_NAME (decl);
1981 if (name == NULL_TREE || !id_equal (name, "allocator"))
1982 return false;
1983
1984 return decl_in_std_namespace_p (decl);
1985}
1986
2ffc2645
MP
1987/* Overload for the above taking constexpr_call*. */
1988
1989static inline bool
1990is_std_allocator_allocate (const constexpr_call *call)
1991{
1992 return (call
1993 && call->fundef
1994 && is_std_allocator_allocate (call->fundef->decl));
1995}
1996
22edf943
MP
1997/* Return true if FNDECL is __dynamic_cast. */
1998
1999static inline bool
2000cxx_dynamic_cast_fn_p (tree fndecl)
2001{
b04445d4 2002 return (cxx_dialect >= cxx20
22edf943
MP
2003 && id_equal (DECL_NAME (fndecl), "__dynamic_cast")
2004 && CP_DECL_CONTEXT (fndecl) == global_namespace);
2005}
2006
2007/* Often, we have an expression in the form of address + offset, e.g.
2008 "&_ZTV1A + 16". Extract the object from it, i.e. "_ZTV1A". */
2009
2010static tree
2011extract_obj_from_addr_offset (tree expr)
2012{
2013 if (TREE_CODE (expr) == POINTER_PLUS_EXPR)
2014 expr = TREE_OPERAND (expr, 0);
2015 STRIP_NOPS (expr);
2016 if (TREE_CODE (expr) == ADDR_EXPR)
2017 expr = TREE_OPERAND (expr, 0);
2018 return expr;
2019}
2020
2021/* Given a PATH like
2022
2023 g.D.2181.D.2154.D.2102.D.2093
2024
2025 find a component with type TYPE. Return NULL_TREE if not found, and
2026 error_mark_node if the component is not accessible. If STOP is non-null,
2027 this function will return NULL_TREE if STOP is found before TYPE. */
2028
2029static tree
2030get_component_with_type (tree path, tree type, tree stop)
2031{
2032 while (true)
2033 {
2034 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path), type))
2035 /* Found it. */
2036 return path;
2037 else if (stop
2038 && (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path),
2039 stop)))
2040 return NULL_TREE;
2041 else if (TREE_CODE (path) == COMPONENT_REF
2042 && DECL_FIELD_IS_BASE (TREE_OPERAND (path, 1)))
2043 {
2044 /* We need to check that the component we're accessing is in fact
2045 accessible. */
2046 if (TREE_PRIVATE (TREE_OPERAND (path, 1))
2047 || TREE_PROTECTED (TREE_OPERAND (path, 1)))
2048 return error_mark_node;
2049 path = TREE_OPERAND (path, 0);
2050 }
2051 else
2052 return NULL_TREE;
2053 }
2054}
2055
2056/* Evaluate a call to __dynamic_cast (permitted by P1327R1).
2057
2058 The declaration of __dynamic_cast is:
2059
2060 void* __dynamic_cast (const void* __src_ptr,
2061 const __class_type_info* __src_type,
2062 const __class_type_info* __dst_type,
2063 ptrdiff_t __src2dst);
2064
2065 where src2dst has the following possible values
2066
2067 >-1: src_type is a unique public non-virtual base of dst_type
2068 dst_ptr + src2dst == src_ptr
2069 -1: unspecified relationship
2070 -2: src_type is not a public base of dst_type
2071 -3: src_type is a multiple public non-virtual base of dst_type
2072
2073 Since literal types can't have virtual bases, we only expect hint >=0,
2074 -2, or -3. */
2075
2076static tree
2077cxx_eval_dynamic_cast_fn (const constexpr_ctx *ctx, tree call,
2078 bool *non_constant_p, bool *overflow_p)
2079{
2080 /* T will be something like
2081 __dynamic_cast ((B*) b, &_ZTI1B, &_ZTI1D, 8)
2082 dismantle it. */
2083 gcc_assert (call_expr_nargs (call) == 4);
2084 tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
2085 tree obj = CALL_EXPR_ARG (call, 0);
2086 tree type = CALL_EXPR_ARG (call, 2);
2087 HOST_WIDE_INT hint = int_cst_value (CALL_EXPR_ARG (call, 3));
2088 location_t loc = cp_expr_loc_or_input_loc (call);
2089
2090 /* Get the target type of the dynamic_cast. */
2091 gcc_assert (TREE_CODE (type) == ADDR_EXPR);
2092 type = TREE_OPERAND (type, 0);
2093 type = TREE_TYPE (DECL_NAME (type));
2094
2095 /* TYPE can only be either T* or T&. We can't know which of these it
2096 is by looking at TYPE, but OBJ will be "(T*) x" in the first case,
2097 and something like "(T*)(T&)(T*) x" in the second case. */
2098 bool reference_p = false;
2099 while (CONVERT_EXPR_P (obj) || TREE_CODE (obj) == SAVE_EXPR)
2100 {
2101 reference_p |= TYPE_REF_P (TREE_TYPE (obj));
2102 obj = TREE_OPERAND (obj, 0);
2103 }
2104
2105 /* Evaluate the object so that we know its dynamic type. */
2106 obj = cxx_eval_constant_expression (ctx, obj, /*lval*/false, non_constant_p,
2107 overflow_p);
2108 if (*non_constant_p)
2109 return call;
2110
2111 /* We expect OBJ to be in form of &d.D.2102 when HINT == 0,
2112 but when HINT is > 0, it can also be something like
2113 &d.D.2102 + 18446744073709551608, which includes the BINFO_OFFSET. */
2114 obj = extract_obj_from_addr_offset (obj);
2115 const tree objtype = TREE_TYPE (obj);
2116 /* If OBJ doesn't refer to a base field, we're done. */
2117 if (tree t = (TREE_CODE (obj) == COMPONENT_REF
2118 ? TREE_OPERAND (obj, 1) : obj))
2119 if (TREE_CODE (t) != FIELD_DECL || !DECL_FIELD_IS_BASE (t))
de0684bf
MP
2120 {
2121 if (reference_p)
2122 {
2123 if (!ctx->quiet)
2124 {
2125 error_at (loc, "reference %<dynamic_cast%> failed");
2126 inform (loc, "dynamic type %qT of its operand does "
2127 "not have a base class of type %qT",
2128 objtype, type);
2129 }
2130 *non_constant_p = true;
2131 }
2132 return integer_zero_node;
2133 }
22edf943
MP
2134
2135 /* [class.cdtor] When a dynamic_cast is used in a constructor ...
2136 or in a destructor ... if the operand of the dynamic_cast refers
2137 to the object under construction or destruction, this object is
2138 considered to be a most derived object that has the type of the
2139 constructor or destructor's class. */
0221c656 2140 tree vtable = build_vfield_ref (obj, objtype);
22edf943
MP
2141 vtable = cxx_eval_constant_expression (ctx, vtable, /*lval*/false,
2142 non_constant_p, overflow_p);
2143 if (*non_constant_p)
2144 return call;
0221c656
MP
2145 /* With -fsanitize=vptr, we initialize all vtable pointers to null,
2146 so it's possible that we got a null pointer now. */
2147 if (integer_zerop (vtable))
2148 {
2149 if (!ctx->quiet)
2150 error_at (loc, "virtual table pointer is used uninitialized");
2151 *non_constant_p = true;
2152 return integer_zero_node;
2153 }
22edf943
MP
2154 /* VTABLE will be &_ZTV1A + 16 or similar, get _ZTV1A. */
2155 vtable = extract_obj_from_addr_offset (vtable);
2156 const tree mdtype = DECL_CONTEXT (vtable);
2157
2158 /* Given dynamic_cast<T>(v),
2159
2160 [expr.dynamic.cast] If C is the class type to which T points or refers,
2161 the runtime check logically executes as follows:
2162
2163 If, in the most derived object pointed (referred) to by v, v points
2164 (refers) to a public base class subobject of a C object, and if only
2165 one object of type C is derived from the subobject pointed (referred)
2166 to by v the result points (refers) to that C object.
2167
2168 In this case, HINT >= 0 or -3. */
2169 if (hint >= 0 || hint == -3)
2170 {
2171 /* Look for a component with type TYPE. */
2172 tree t = get_component_with_type (obj, type, mdtype);
2173 /* If not accessible, give an error. */
2174 if (t == error_mark_node)
2175 {
2176 if (reference_p)
2177 {
2178 if (!ctx->quiet)
2179 {
2180 error_at (loc, "reference %<dynamic_cast%> failed");
2181 inform (loc, "static type %qT of its operand is a "
2182 "non-public base class of dynamic type %qT",
2183 objtype, type);
2184
2185 }
2186 *non_constant_p = true;
2187 }
2188 return integer_zero_node;
2189 }
2190 else if (t)
2191 /* The result points to the TYPE object. */
2192 return cp_build_addr_expr (t, complain);
2193 /* Else, TYPE was not found, because the HINT turned out to be wrong.
2194 Fall through to the normal processing. */
2195 }
2196
2197 /* Otherwise, if v points (refers) to a public base class subobject of the
2198 most derived object, and the type of the most derived object has a base
2199 class, of type C, that is unambiguous and public, the result points
2200 (refers) to the C subobject of the most derived object.
2201
2202 But it can also be an invalid case. */
2203
2204 /* Get the most derived object. */
2205 obj = get_component_with_type (obj, mdtype, NULL_TREE);
2206 if (obj == error_mark_node)
2207 {
2208 if (reference_p)
2209 {
2210 if (!ctx->quiet)
2211 {
2212 error_at (loc, "reference %<dynamic_cast%> failed");
2213 inform (loc, "static type %qT of its operand is a non-public"
2214 " base class of dynamic type %qT", objtype, mdtype);
2215 }
2216 *non_constant_p = true;
2217 }
2218 return integer_zero_node;
2219 }
2220 else
2221 gcc_assert (obj);
2222
2223 /* Check that the type of the most derived object has a base class
2224 of type TYPE that is unambiguous and public. */
2225 base_kind b_kind;
2226 tree binfo = lookup_base (mdtype, type, ba_check, &b_kind, tf_none);
2227 if (!binfo || binfo == error_mark_node)
2228 {
2229 if (reference_p)
2230 {
2231 if (!ctx->quiet)
2232 {
2233 error_at (loc, "reference %<dynamic_cast%> failed");
2234 if (b_kind == bk_ambig)
2235 inform (loc, "%qT is an ambiguous base class of dynamic "
2236 "type %qT of its operand", type, mdtype);
2237 else
2238 inform (loc, "dynamic type %qT of its operand does not "
2239 "have an unambiguous public base class %qT",
2240 mdtype, type);
2241 }
2242 *non_constant_p = true;
2243 }
2244 return integer_zero_node;
2245 }
2246 /* If so, return the TYPE subobject of the most derived object. */
2247 obj = convert_to_base_statically (obj, binfo);
2248 return cp_build_addr_expr (obj, complain);
2249}
2250
4f6bc28f 2251/* Data structure used by replace_decl and replace_decl_r. */
b2562229 2252
4f6bc28f 2253struct replace_decl_data
b2562229 2254{
4f6bc28f 2255 /* The _DECL we want to replace. */
b2562229
PP
2256 tree decl;
2257 /* The replacement for DECL. */
2258 tree replacement;
4f6bc28f
JM
2259 /* Trees we've visited. */
2260 hash_set<tree> *pset;
b2562229
PP
2261 /* Whether we've performed any replacements. */
2262 bool changed;
2263};
2264
4f6bc28f 2265/* Helper function for replace_decl, called through cp_walk_tree. */
b2562229
PP
2266
2267static tree
4f6bc28f 2268replace_decl_r (tree *tp, int *walk_subtrees, void *data)
b2562229 2269{
4f6bc28f 2270 replace_decl_data *d = (replace_decl_data *) data;
b2562229
PP
2271
2272 if (*tp == d->decl)
2273 {
2274 *tp = unshare_expr (d->replacement);
2275 d->changed = true;
2276 *walk_subtrees = 0;
2277 }
4f6bc28f
JM
2278 else if (TYPE_P (*tp)
2279 || d->pset->add (*tp))
b2562229
PP
2280 *walk_subtrees = 0;
2281
2282 return NULL_TREE;
2283}
2284
4f6bc28f
JM
2285/* Replace every occurrence of DECL with (an unshared copy of)
2286 REPLACEMENT within the expression *TP. Returns true iff a
b2562229
PP
2287 replacement was performed. */
2288
4f6bc28f
JM
2289bool
2290replace_decl (tree *tp, tree decl, tree replacement)
b2562229 2291{
4f6bc28f
JM
2292 gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
2293 (TREE_TYPE (decl), TREE_TYPE (replacement)));
2294 hash_set<tree> pset;
2295 replace_decl_data data = { decl, replacement, &pset, false };
2296 cp_walk_tree (tp, replace_decl_r, &data, NULL);
b2562229
PP
2297 return data.changed;
2298}
2299
e6321c45
JM
2300/* Evaluate the call T to virtual function thunk THUNK_FNDECL. */
2301
2302static tree
2303cxx_eval_thunk_call (const constexpr_ctx *ctx, tree t, tree thunk_fndecl,
2304 bool lval,
2305 bool *non_constant_p, bool *overflow_p)
2306{
2307 tree function = THUNK_TARGET (thunk_fndecl);
2308
e6321c45
JM
2309 if (THUNK_VIRTUAL_OFFSET (thunk_fndecl))
2310 {
e6321c45
JM
2311 if (!ctx->quiet)
2312 {
267318a2
JM
2313 if (!DECL_DECLARED_CONSTEXPR_P (function))
2314 {
2315 error ("call to non-%<constexpr%> function %qD", function);
2316 explain_invalid_constexpr_fn (function);
2317 }
2318 else
2319 /* virtual_offset is only set for virtual bases, which make the
2320 class non-literal, so we don't need to handle it here. */
2321 error ("calling constexpr member function %qD through virtual "
2322 "base subobject", function);
e6321c45
JM
2323 }
2324 *non_constant_p = true;
2325 return t;
2326 }
2327
2328 tree new_call = copy_node (t);
2329 CALL_EXPR_FN (new_call) = function;
2330 TREE_TYPE (new_call) = TREE_TYPE (TREE_TYPE (function));
2331
2332 tree offset = size_int (THUNK_FIXED_OFFSET (thunk_fndecl));
2333
2334 if (DECL_THIS_THUNK_P (thunk_fndecl))
2335 {
2336 /* 'this'-adjusting thunk. */
2337 tree this_arg = CALL_EXPR_ARG (t, 0);
2338 this_arg = build2 (POINTER_PLUS_EXPR, TREE_TYPE (this_arg),
2339 this_arg, offset);
2340 CALL_EXPR_ARG (new_call, 0) = this_arg;
2341 }
2342 else
2343 /* Return-adjusting thunk. */
2344 new_call = build2 (POINTER_PLUS_EXPR, TREE_TYPE (new_call),
2345 new_call, offset);
2346
2347 return cxx_eval_constant_expression (ctx, new_call, lval,
2348 non_constant_p, overflow_p);
2349}
2350
caf17f3a
MP
2351/* If OBJECT is of const class type, evaluate it to a CONSTRUCTOR and set
2352 its TREE_READONLY flag according to READONLY_P. Used for constexpr
2353 'tors to detect modifying const objects in a constexpr context. */
2354
2355static void
2356cxx_set_object_constness (const constexpr_ctx *ctx, tree object,
2357 bool readonly_p, bool *non_constant_p,
2358 bool *overflow_p)
2359{
2360 if (CLASS_TYPE_P (TREE_TYPE (object))
2361 && CP_TYPE_CONST_P (TREE_TYPE (object)))
2362 {
2363 /* Subobjects might not be stored in ctx->global->values but we
2364 can get its CONSTRUCTOR by evaluating *this. */
2365 tree e = cxx_eval_constant_expression (ctx, object, /*lval*/false,
2366 non_constant_p, overflow_p);
2367 if (TREE_CODE (e) == CONSTRUCTOR && !*non_constant_p)
2368 TREE_READONLY (e) = readonly_p;
2369 }
2370}
2371
2d76680f
PC
2372/* Subroutine of cxx_eval_constant_expression.
2373 Evaluate the call expression tree T in the context of OLD_CALL expression
2374 evaluation. */
2375
2376static tree
3e605b20 2377cxx_eval_call_expression (const constexpr_ctx *ctx, tree t,
92a596e8 2378 bool lval,
2d76680f
PC
2379 bool *non_constant_p, bool *overflow_p)
2380{
861d4af8
AS
2381 /* Handle concept checks separately. */
2382 if (concept_check_p (t))
662ef5b5 2383 return evaluate_concept_check (t);
861d4af8 2384
f9d0ca40 2385 location_t loc = cp_expr_loc_or_input_loc (t);
2d76680f 2386 tree fun = get_function_named_in_call (t);
cce3ae91 2387 constexpr_call new_call
13de99bc 2388 = { NULL, NULL, NULL, 0, ctx->manifestly_const_eval };
7ffc7de5 2389 int depth_ok;
2d76680f 2390
0b274c17 2391 if (fun == NULL_TREE)
44a845ca
MS
2392 return cxx_eval_internal_function (ctx, t, lval,
2393 non_constant_p, overflow_p);
0b274c17 2394
2d76680f
PC
2395 if (TREE_CODE (fun) != FUNCTION_DECL)
2396 {
2397 /* Might be a constexpr function pointer. */
2b3ab879 2398 fun = cxx_eval_constant_expression (ctx, fun,
92a596e8 2399 /*lval*/false, non_constant_p,
5a804683 2400 overflow_p);
2d76680f
PC
2401 STRIP_NOPS (fun);
2402 if (TREE_CODE (fun) == ADDR_EXPR)
2403 fun = TREE_OPERAND (fun, 0);
582d2481
JJ
2404 /* For TARGET_VTABLE_USES_DESCRIPTORS targets, there is no
2405 indirection, the called expression is a pointer into the
2406 virtual table which should contain FDESC_EXPR. Extract the
2407 FUNCTION_DECL from there. */
2408 else if (TARGET_VTABLE_USES_DESCRIPTORS
2409 && TREE_CODE (fun) == POINTER_PLUS_EXPR
2410 && TREE_CODE (TREE_OPERAND (fun, 0)) == ADDR_EXPR
2411 && TREE_CODE (TREE_OPERAND (fun, 1)) == INTEGER_CST)
2412 {
2413 tree d = TREE_OPERAND (TREE_OPERAND (fun, 0), 0);
2414 if (VAR_P (d)
2415 && DECL_VTABLE_OR_VTT_P (d)
2416 && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE
2417 && TREE_TYPE (TREE_TYPE (d)) == vtable_entry_type
2418 && DECL_INITIAL (d)
2419 && TREE_CODE (DECL_INITIAL (d)) == CONSTRUCTOR)
2420 {
2421 tree i = int_const_binop (TRUNC_DIV_EXPR, TREE_OPERAND (fun, 1),
2422 TYPE_SIZE_UNIT (vtable_entry_type));
2423 HOST_WIDE_INT idx = find_array_ctor_elt (DECL_INITIAL (d), i);
2424 if (idx >= 0)
2425 {
2426 tree fdesc
2427 = (*CONSTRUCTOR_ELTS (DECL_INITIAL (d)))[idx].value;
2428 if (TREE_CODE (fdesc) == FDESC_EXPR
2429 && integer_zerop (TREE_OPERAND (fdesc, 1)))
2430 fun = TREE_OPERAND (fdesc, 0);
2431 }
2432 }
2433 }
2d76680f
PC
2434 }
2435 if (TREE_CODE (fun) != FUNCTION_DECL)
2436 {
2b3ab879 2437 if (!ctx->quiet && !*non_constant_p)
84fa214d 2438 error_at (loc, "expression %qE does not designate a %<constexpr%> "
2d76680f
PC
2439 "function", fun);
2440 *non_constant_p = true;
2441 return t;
2442 }
81f97181 2443 if (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun))
2d76680f 2444 fun = DECL_CLONED_FUNCTION (fun);
0b274c17
MP
2445
2446 if (is_ubsan_builtin_p (fun))
2447 return void_node;
2448
3d78e008 2449 if (fndecl_built_in_p (fun))
5756d0f9 2450 return cxx_eval_builtin_function_call (ctx, t, fun,
92a596e8 2451 lval, non_constant_p, overflow_p);
e6321c45
JM
2452 if (DECL_THUNK_P (fun))
2453 return cxx_eval_thunk_call (ctx, t, fun, lval, non_constant_p, overflow_p);
87c2080b 2454 if (!maybe_constexpr_fn (fun))
2d76680f 2455 {
8412b939
JJ
2456 if (TREE_CODE (t) == CALL_EXPR
2457 && cxx_replaceable_global_alloc_fn (fun)
2458 && (CALL_FROM_NEW_OR_DELETE_P (t)
2ffc2645 2459 || is_std_allocator_allocate (ctx->call)))
8e007055
JJ
2460 {
2461 const int nargs = call_expr_nargs (t);
2462 tree arg0 = NULL_TREE;
2463 for (int i = 0; i < nargs; ++i)
2464 {
2465 tree arg = CALL_EXPR_ARG (t, i);
2466 arg = cxx_eval_constant_expression (ctx, arg, false,
2467 non_constant_p, overflow_p);
2468 VERIFY_CONSTANT (arg);
2469 if (i == 0)
2470 arg0 = arg;
2471 }
2472 gcc_assert (arg0);
2473 if (IDENTIFIER_NEW_OP_P (DECL_NAME (fun)))
2474 {
2475 tree type = build_array_type_nelts (char_type_node,
2476 tree_to_uhwi (arg0));
815baade
JJ
2477 tree var = build_decl (loc, VAR_DECL,
2478 (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
2479 & OVL_OP_FLAG_VEC)
2480 ? heap_vec_uninit_identifier
2481 : heap_uninit_identifier,
8e007055
JJ
2482 type);
2483 DECL_ARTIFICIAL (var) = 1;
2484 TREE_STATIC (var) = 1;
a8db7887
JJ
2485 // Temporarily register the artificial var in varpool,
2486 // so that comparisons of its address against NULL are folded
2487 // through nonzero_address even with
2488 // -fno-delete-null-pointer-checks or that comparison of
2489 // addresses of different heap artificial vars is folded too.
2490 // See PR98988 and PR99031.
2491 varpool_node::finalize_decl (var);
8e007055
JJ
2492 ctx->global->heap_vars.safe_push (var);
2493 ctx->global->values.put (var, NULL_TREE);
2494 return fold_convert (ptr_type_node, build_address (var));
2495 }
2496 else
2497 {
2498 STRIP_NOPS (arg0);
2499 if (TREE_CODE (arg0) == ADDR_EXPR
2500 && VAR_P (TREE_OPERAND (arg0, 0)))
2501 {
2502 tree var = TREE_OPERAND (arg0, 0);
2503 if (DECL_NAME (var) == heap_uninit_identifier
2504 || DECL_NAME (var) == heap_identifier)
2505 {
815baade
JJ
2506 if (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
2507 & OVL_OP_FLAG_VEC)
2508 {
2509 if (!ctx->quiet)
2510 {
2511 error_at (loc, "array deallocation of object "
2512 "allocated with non-array "
2513 "allocation");
2514 inform (DECL_SOURCE_LOCATION (var),
2515 "allocation performed here");
2516 }
2517 *non_constant_p = true;
2518 return t;
2519 }
2520 DECL_NAME (var) = heap_deleted_identifier;
2521 ctx->global->values.remove (var);
2522 ctx->global->heap_dealloc_count++;
2523 return void_node;
2524 }
2525 else if (DECL_NAME (var) == heap_vec_uninit_identifier
2526 || DECL_NAME (var) == heap_vec_identifier)
2527 {
2528 if ((IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
2529 & OVL_OP_FLAG_VEC) == 0)
2530 {
2531 if (!ctx->quiet)
2532 {
2533 error_at (loc, "non-array deallocation of "
2534 "object allocated with array "
2535 "allocation");
2536 inform (DECL_SOURCE_LOCATION (var),
2537 "allocation performed here");
2538 }
2539 *non_constant_p = true;
2540 return t;
2541 }
8e007055
JJ
2542 DECL_NAME (var) = heap_deleted_identifier;
2543 ctx->global->values.remove (var);
f74f6092 2544 ctx->global->heap_dealloc_count++;
8e007055
JJ
2545 return void_node;
2546 }
2547 else if (DECL_NAME (var) == heap_deleted_identifier)
2548 {
2549 if (!ctx->quiet)
2550 error_at (loc, "deallocation of already deallocated "
2551 "storage");
2552 *non_constant_p = true;
2553 return t;
2554 }
2555 }
2556 if (!ctx->quiet)
2557 error_at (loc, "deallocation of storage that was "
2558 "not previously allocated");
2559 *non_constant_p = true;
2560 return t;
2561 }
2562 }
cf650568
JJ
2563 /* Allow placement new in std::construct_at, just return the second
2564 argument. */
8412b939
JJ
2565 if (TREE_CODE (t) == CALL_EXPR
2566 && cxx_placement_new_fn (fun)
2ffc2645 2567 && is_std_construct_at (ctx->call))
cf650568
JJ
2568 {
2569 const int nargs = call_expr_nargs (t);
2570 tree arg1 = NULL_TREE;
2571 for (int i = 0; i < nargs; ++i)
2572 {
2573 tree arg = CALL_EXPR_ARG (t, i);
2574 arg = cxx_eval_constant_expression (ctx, arg, false,
2575 non_constant_p, overflow_p);
cf650568
JJ
2576 if (i == 1)
2577 arg1 = arg;
2805fcb3
JJ
2578 else
2579 VERIFY_CONSTANT (arg);
cf650568
JJ
2580 }
2581 gcc_assert (arg1);
2582 return arg1;
2583 }
22edf943
MP
2584 else if (cxx_dynamic_cast_fn_p (fun))
2585 return cxx_eval_dynamic_cast_fn (ctx, t, non_constant_p, overflow_p);
2586
2b3ab879 2587 if (!ctx->quiet)
2d76680f 2588 {
11399477 2589 if (!lambda_static_thunk_p (fun))
84fa214d 2590 error_at (loc, "call to non-%<constexpr%> function %qD", fun);
2d76680f
PC
2591 explain_invalid_constexpr_fn (fun);
2592 }
2593 *non_constant_p = true;
2594 return t;
2595 }
2596
86461cad
JM
2597 constexpr_ctx new_ctx = *ctx;
2598 if (DECL_CONSTRUCTOR_P (fun) && !ctx->object
2599 && TREE_CODE (t) == AGGR_INIT_EXPR)
2600 {
2601 /* We want to have an initialization target for an AGGR_INIT_EXPR.
2602 If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
2603 new_ctx.object = AGGR_INIT_EXPR_SLOT (t);
2604 tree ctor = new_ctx.ctor = build_constructor (DECL_CONTEXT (fun), NULL);
e8c48716 2605 CONSTRUCTOR_NO_CLEARING (ctor) = true;
8e007055 2606 ctx->global->values.put (new_ctx.object, ctor);
86461cad
JM
2607 ctx = &new_ctx;
2608 }
2609
2d76680f
PC
2610 /* Shortcut trivial constructor/op=. */
2611 if (trivial_fn_p (fun))
2612 {
86461cad 2613 tree init = NULL_TREE;
2d76680f 2614 if (call_expr_nargs (t) == 2)
86461cad 2615 init = convert_from_reference (get_nth_callarg (t, 1));
2d76680f
PC
2616 else if (TREE_CODE (t) == AGGR_INIT_EXPR
2617 && AGGR_INIT_ZERO_FIRST (t))
86461cad
JM
2618 init = build_zero_init (DECL_CONTEXT (fun), NULL_TREE, false);
2619 if (init)
2620 {
2621 tree op = get_nth_callarg (t, 0);
2622 if (is_dummy_object (op))
2623 op = ctx->object;
2624 else
2625 op = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (op)), op);
2626 tree set = build2 (MODIFY_EXPR, TREE_TYPE (op), op, init);
c8816908
JM
2627 new_ctx.call = &new_call;
2628 return cxx_eval_constant_expression (&new_ctx, set, lval,
86461cad
JM
2629 non_constant_p, overflow_p);
2630 }
2d76680f
PC
2631 }
2632
1595fe44
JM
2633 bool non_constant_args = false;
2634 new_call.bindings
2635 = cxx_bind_parameters_in_call (ctx, t, fun, non_constant_p,
2636 overflow_p, &non_constant_args);
2637
2638 /* We build up the bindings list before we know whether we already have this
2639 call cached. If we don't end up saving these bindings, ggc_free them when
2640 this function exits. */
2641 class free_bindings
2642 {
2643 tree *bindings;
2644 public:
2645 free_bindings (tree &b): bindings (&b) { }
2646 ~free_bindings () { if (bindings) ggc_free (*bindings); }
2647 void preserve () { bindings = NULL; }
2648 } fb (new_call.bindings);
2649
2650 if (*non_constant_p)
2651 return t;
2652
81371eff
JM
2653 /* We can't defer instantiating the function any longer. */
2654 if (!DECL_INITIAL (fun)
f65a3299
PP
2655 && DECL_TEMPLOID_INSTANTIATION (fun)
2656 && !uid_sensitive_constexpr_evaluation_p ())
81371eff 2657 {
f065303f
JM
2658 location_t save_loc = input_location;
2659 input_location = loc;
81371eff 2660 ++function_depth;
9aeadd8c
JM
2661 if (ctx->manifestly_const_eval)
2662 FNDECL_MANIFESTLY_CONST_EVALUATED (fun) = true;
81371eff
JM
2663 instantiate_decl (fun, /*defer_ok*/false, /*expl_inst*/false);
2664 --function_depth;
f065303f 2665 input_location = save_loc;
81371eff
JM
2666 }
2667
2d76680f 2668 /* If in direct recursive call, optimize definition search. */
c8816908 2669 if (ctx && ctx->call && ctx->call->fundef && ctx->call->fundef->decl == fun)
3e605b20 2670 new_call.fundef = ctx->call->fundef;
2d76680f
PC
2671 else
2672 {
2673 new_call.fundef = retrieve_constexpr_fundef (fun);
4ddcdbf9 2674 if (new_call.fundef == NULL || new_call.fundef->body == NULL
4f05d85a 2675 || new_call.fundef->result == error_mark_node
4ddcdbf9 2676 || fun == current_function_decl)
2d76680f 2677 {
2b3ab879 2678 if (!ctx->quiet)
2d76680f 2679 {
4ddcdbf9
JM
2680 /* We need to check for current_function_decl here in case we're
2681 being called during cp_fold_function, because at that point
2682 DECL_INITIAL is set properly and we have a fundef but we
2683 haven't lowered invisirefs yet (c++/70344). */
2684 if (DECL_INITIAL (fun) == error_mark_node
2685 || fun == current_function_decl)
ddd6d421
JM
2686 error_at (loc, "%qD called in a constant expression before its "
2687 "definition is complete", fun);
2688 else if (DECL_INITIAL (fun))
2d76680f 2689 {
98e5a19a
JM
2690 /* The definition of fun was somehow unsuitable. But pretend
2691 that lambda static thunks don't exist. */
2692 if (!lambda_static_thunk_p (fun))
2693 error_at (loc, "%qD called in a constant expression", fun);
2d76680f
PC
2694 explain_invalid_constexpr_fn (fun);
2695 }
2696 else
2697 error_at (loc, "%qD used before its definition", fun);
2698 }
2699 *non_constant_p = true;
2700 return t;
2701 }
2702 }
60813a46 2703
2d76680f
PC
2704 depth_ok = push_cx_call_context (t);
2705
caf17f3a 2706 /* Remember the object we are constructing or destructing. */
04e1749c 2707 tree new_obj = NULL_TREE;
caf17f3a 2708 if (DECL_CONSTRUCTOR_P (fun) || DECL_DESTRUCTOR_P (fun))
04e1749c 2709 {
caf17f3a 2710 /* In a cdtor, it should be the first `this' argument.
04e1749c
MP
2711 At this point it has already been evaluated in the call
2712 to cxx_bind_parameters_in_call. */
2713 new_obj = TREE_VEC_ELT (new_call.bindings, 0);
2714 STRIP_NOPS (new_obj);
2715 if (TREE_CODE (new_obj) == ADDR_EXPR)
2716 new_obj = TREE_OPERAND (new_obj, 0);
64da1b76
PP
2717
2718 if (ctx->call && ctx->call->fundef
2719 && DECL_CONSTRUCTOR_P (ctx->call->fundef->decl))
2720 {
2721 tree cur_obj = TREE_VEC_ELT (ctx->call->bindings, 0);
2722 STRIP_NOPS (cur_obj);
2723 if (TREE_CODE (cur_obj) == ADDR_EXPR)
2724 cur_obj = TREE_OPERAND (cur_obj, 0);
2725 if (new_obj == cur_obj)
2726 /* We're calling the target constructor of a delegating
2727 constructor, or accessing a base subobject through a
2728 NOP_EXPR as part of a call to a base constructor, so
2729 there is no new (sub)object. */
2730 new_obj = NULL_TREE;
2731 }
04e1749c
MP
2732 }
2733
12d9ce19 2734 tree result = NULL_TREE;
2d76680f 2735
12d9ce19 2736 constexpr_call *entry = NULL;
4a58d2fe 2737 if (depth_ok && !non_constant_args && ctx->strict)
2d76680f 2738 {
cce3ae91
JJ
2739 new_call.hash = constexpr_fundef_hasher::hash (new_call.fundef);
2740 new_call.hash
2741 = iterative_hash_template_arg (new_call.bindings, new_call.hash);
2742 new_call.hash
13de99bc 2743 = iterative_hash_object (ctx->manifestly_const_eval, new_call.hash);
12d9ce19
JM
2744
2745 /* If we have seen this call before, we are done. */
2746 maybe_initialize_constexpr_call_table ();
2747 constexpr_call **slot
2748 = constexpr_call_table->find_slot (&new_call, INSERT);
2749 entry = *slot;
2750 if (entry == NULL)
2751 {
7ffc7de5
JM
2752 /* Only cache up to constexpr_cache_depth to limit memory use. */
2753 if (depth_ok < constexpr_cache_depth)
2754 {
2755 /* We need to keep a pointer to the entry, not just the slot, as
2756 the slot can move during evaluation of the body. */
2757 *slot = entry = ggc_alloc<constexpr_call> ();
2758 *entry = new_call;
2759 fb.preserve ();
2760 }
12d9ce19 2761 }
7ffc7de5
JM
2762 /* Calls that are in progress have their result set to NULL, so that we
2763 can detect circular dependencies. Now that we only cache up to
2764 constexpr_cache_depth this won't catch circular dependencies that
2765 start deeper, but they'll hit the recursion or ops limit. */
12d9ce19
JM
2766 else if (entry->result == NULL)
2767 {
2768 if (!ctx->quiet)
2769 error ("call has circular dependency");
2770 *non_constant_p = true;
2771 entry->result = result = error_mark_node;
2772 }
2773 else
2774 result = entry->result;
2d76680f
PC
2775 }
2776
2777 if (!depth_ok)
2778 {
2b3ab879 2779 if (!ctx->quiet)
84fa214d 2780 error ("%<constexpr%> evaluation depth exceeds maximum of %d (use "
7e7a6ed7 2781 "%<-fconstexpr-depth=%> to increase the maximum)",
2d76680f
PC
2782 max_constexpr_depth);
2783 *non_constant_p = true;
12d9ce19 2784 result = error_mark_node;
2d76680f
PC
2785 }
2786 else
2787 {
f74f6092 2788 bool cacheable = true;
de54de93
JM
2789 if (result && result != error_mark_node)
2790 /* OK */;
2791 else if (!DECL_SAVED_TREE (fun))
2792 {
2793 /* When at_eof >= 2, cgraph has started throwing away
2794 DECL_SAVED_TREE, so fail quietly. FIXME we get here because of
2795 late code generation for VEC_INIT_EXPR, which needs to be
2796 completely reconsidered. */
2797 gcc_assert (at_eof >= 2 && ctx->quiet);
2798 *non_constant_p = true;
2799 }
f65a3299 2800 else if (tree copy = get_fundef_copy (new_call.fundef))
3e605b20 2801 {
c0daf32d 2802 tree body, parms, res;
620adbec 2803 releasing_vec ctors;
0567dcd2 2804
c0daf32d 2805 /* Reuse or create a new unshared copy of this function's body. */
97f3003f
JM
2806 body = TREE_PURPOSE (copy);
2807 parms = TREE_VALUE (copy);
2808 res = TREE_TYPE (copy);
0567dcd2
MP
2809
2810 /* Associate the bindings with the remapped parms. */
2811 tree bound = new_call.bindings;
2812 tree remapped = parms;
3c961dc7 2813 for (int i = 0; i < TREE_VEC_LENGTH (bound); ++i)
60813a46 2814 {
3c961dc7 2815 tree arg = TREE_VEC_ELT (bound, i);
4953b790
JM
2816 if (entry)
2817 {
2818 /* Unshare args going into the hash table to separate them
2819 from the caller's context, for better GC and to avoid
2820 problems with verify_gimple. */
db38a029 2821 arg = unshare_expr_without_location (arg);
4953b790 2822 TREE_VEC_ELT (bound, i) = arg;
5afd90c5
JJ
2823
2824 /* And then unshare again so the callee doesn't change the
2825 argument values in the hash table. XXX Could we unshare
2826 lazily in cxx_eval_store_expression? */
2827 arg = unshare_constructor (arg);
2828 if (TREE_CODE (arg) == CONSTRUCTOR)
2829 vec_safe_push (ctors, arg);
4953b790 2830 }
8e007055 2831 ctx->global->values.put (remapped, arg);
0567dcd2 2832 remapped = DECL_CHAIN (remapped);
60813a46 2833 }
0567dcd2 2834 /* Add the RESULT_DECL to the values map, too. */
cd3ca6cb
JM
2835 gcc_assert (!DECL_BY_REFERENCE (res));
2836 ctx->global->values.put (res, NULL_TREE);
60813a46 2837
ee1de08d
JJ
2838 /* Track the callee's evaluated SAVE_EXPRs and TARGET_EXPRs so that
2839 we can forget their values after the call. */
c0daf32d 2840 constexpr_ctx ctx_with_save_exprs = *ctx;
0ee28590 2841 auto_vec<tree, 10> save_exprs;
c0daf32d 2842 ctx_with_save_exprs.save_exprs = &save_exprs;
1e163090 2843 ctx_with_save_exprs.call = &new_call;
f74f6092
JJ
2844 unsigned save_heap_alloc_count = ctx->global->heap_vars.length ();
2845 unsigned save_heap_dealloc_count = ctx->global->heap_dealloc_count;
c0daf32d 2846
caf17f3a
MP
2847 /* If this is a constexpr destructor, the object's const and volatile
2848 semantics are no longer in effect; see [class.dtor]p5. */
2849 if (new_obj && DECL_DESTRUCTOR_P (fun))
2850 cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/false,
2851 non_constant_p, overflow_p);
2852
0567dcd2 2853 tree jump_target = NULL_TREE;
c0daf32d 2854 cxx_eval_constant_expression (&ctx_with_save_exprs, body,
0567dcd2
MP
2855 lval, non_constant_p, overflow_p,
2856 &jump_target);
60813a46 2857
0567dcd2 2858 if (DECL_CONSTRUCTOR_P (fun))
de07cff9
PP
2859 {
2860 /* This can be null for a subobject constructor call, in
2861 which case what we care about is the initialization
2862 side-effects rather than the value. We could get at the
2863 value by evaluating *this, but we don't bother; there's
2864 no need to put such a call in the hash table. */
2865 result = lval ? ctx->object : ctx->ctor;
2866
2867 /* If we've just evaluated a subobject constructor call for an
2868 empty union member, it might not have produced a side effect
2869 that actually activated the union member. So produce such a
2870 side effect now to ensure the union appears initialized. */
2871 if (!result && new_obj
2872 && TREE_CODE (new_obj) == COMPONENT_REF
2873 && TREE_CODE (TREE_TYPE
2874 (TREE_OPERAND (new_obj, 0))) == UNION_TYPE
2875 && is_really_empty_class (TREE_TYPE (new_obj),
2876 /*ignore_vptr*/false))
2877 {
2878 tree activate = build2 (MODIFY_EXPR, TREE_TYPE (new_obj),
2879 new_obj,
2880 build_constructor (TREE_TYPE (new_obj),
2881 NULL));
2882 cxx_eval_constant_expression (ctx, activate, lval,
2883 non_constant_p, overflow_p);
2884 ggc_free (activate);
2885 }
2886 }
0567dcd2
MP
2887 else if (VOID_TYPE_P (TREE_TYPE (res)))
2888 result = void_node;
2889 else
2890 {
cd3ca6cb 2891 result = *ctx->global->values.get (res);
0567dcd2 2892 if (result == NULL_TREE && !*non_constant_p)
60813a46 2893 {
0567dcd2 2894 if (!ctx->quiet)
84fa214d 2895 error ("%<constexpr%> call flows off the end "
0567dcd2
MP
2896 "of the function");
2897 *non_constant_p = true;
60813a46 2898 }
60813a46 2899 }
0567dcd2 2900
04e1749c
MP
2901 /* At this point, the object's constructor will have run, so
2902 the object is no longer under construction, and its possible
2903 'const' semantics now apply. Make a note of this fact by
2904 marking the CONSTRUCTOR TREE_READONLY. */
caf17f3a
MP
2905 if (new_obj && DECL_CONSTRUCTOR_P (fun))
2906 cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/true,
2907 non_constant_p, overflow_p);
04e1749c 2908
ee1de08d
JJ
2909 /* Forget the saved values of the callee's SAVE_EXPRs and
2910 TARGET_EXPRs. */
3f207ab3 2911 for (tree save_expr : save_exprs)
8e007055 2912 ctx->global->values.remove (save_expr);
c0daf32d 2913
0567dcd2
MP
2914 /* Remove the parms/result from the values map. Is it worth
2915 bothering to do this when the map itself is only live for
2916 one constexpr evaluation? If so, maybe also clear out
2917 other vars from call, maybe in BIND_EXPR handling? */
8e007055 2918 ctx->global->values.remove (res);
0567dcd2 2919 for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
8e007055 2920 ctx->global->values.remove (parm);
c0daf32d 2921
620adbec
JM
2922 /* Free any parameter CONSTRUCTORs we aren't returning directly. */
2923 while (!ctors->is_empty ())
2924 {
2925 tree c = ctors->pop ();
2926 if (c != result)
2927 free_constructor (c);
2928 }
2929
c0daf32d
PP
2930 /* Make the unshared function copy we used available for re-use. */
2931 save_fundef_copy (fun, copy);
f74f6092
JJ
2932
2933 /* If the call allocated some heap object that hasn't been
2934 deallocated during the call, or if it deallocated some heap
2935 object it has not allocated, the call isn't really stateless
2936 for the constexpr evaluation and should not be cached.
2937 It is fine if the call allocates something and deallocates it
2938 too. */
2939 if (entry
2940 && (save_heap_alloc_count != ctx->global->heap_vars.length ()
2941 || (save_heap_dealloc_count
2942 != ctx->global->heap_dealloc_count)))
2943 {
2944 tree heap_var;
2945 unsigned int i;
2946 if ((ctx->global->heap_vars.length ()
2947 - ctx->global->heap_dealloc_count)
2948 != save_heap_alloc_count - save_heap_dealloc_count)
2949 cacheable = false;
2950 else
2951 FOR_EACH_VEC_ELT_FROM (ctx->global->heap_vars, i, heap_var,
2952 save_heap_alloc_count)
2953 if (DECL_NAME (heap_var) != heap_deleted_identifier)
2954 {
2955 cacheable = false;
2956 break;
2957 }
2958 }
b2562229
PP
2959
2960 /* Rewrite all occurrences of the function's RESULT_DECL with the
2961 current object under construction. */
2962 if (!*non_constant_p && ctx->object
bb1f0b50 2963 && CLASS_TYPE_P (TREE_TYPE (res))
b2562229 2964 && !is_empty_class (TREE_TYPE (res)))
4f6bc28f 2965 if (replace_decl (&result, res, ctx->object))
b2562229 2966 cacheable = false;
3e605b20 2967 }
aaa26bf4
JM
2968 else
2969 /* Couldn't get a function copy to evaluate. */
2970 *non_constant_p = true;
60813a46 2971
2d76680f
PC
2972 if (result == error_mark_node)
2973 *non_constant_p = true;
52228180 2974 if (*non_constant_p || *overflow_p)
12d9ce19 2975 result = error_mark_node;
0567dcd2 2976 else if (!result)
60813a46 2977 result = void_node;
12d9ce19 2978 if (entry)
f74f6092 2979 entry->result = cacheable ? result : error_mark_node;
2d76680f
PC
2980 }
2981
7906797e
MP
2982 /* The result of a constexpr function must be completely initialized.
2983
2984 However, in C++20, a constexpr constructor doesn't necessarily have
2985 to initialize all the fields, so we don't clear CONSTRUCTOR_NO_CLEARING
2986 in order to detect reading an unitialized object in constexpr instead
2987 of value-initializing it. (reduced_constant_expression_p is expected to
2988 take care of clearing the flag.) */
2989 if (TREE_CODE (result) == CONSTRUCTOR
b04445d4 2990 && (cxx_dialect < cxx20
7906797e 2991 || !DECL_CONSTRUCTOR_P (fun)))
ecc57615 2992 clear_no_implicit_zero (result);
f64e0c02 2993
2d76680f 2994 pop_cx_call_context ();
9b9eb42a 2995 return result;
2d76680f
PC
2996}
2997
7906797e
MP
2998/* Return true if T is a valid constant initializer. If a CONSTRUCTOR
2999 initializes all the members, the CONSTRUCTOR_NO_CLEARING flag will be
3000 cleared.
3001 FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
2d76680f
PC
3002
3003bool
3004reduced_constant_expression_p (tree t)
3005{
4930c53e
MP
3006 if (t == NULL_TREE)
3007 return false;
3008
2d76680f
PC
3009 switch (TREE_CODE (t))
3010 {
3011 case PTRMEM_CST:
3012 /* Even if we can't lower this yet, it's constant. */
3013 return true;
3014
3015 case CONSTRUCTOR:
3016 /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
fae00a0a 3017 tree field;
e8c48716 3018 if (CONSTRUCTOR_NO_CLEARING (t))
6f11ddd8
JM
3019 {
3020 if (TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
3021 /* An initialized vector would have a VECTOR_CST. */
3022 return false;
e948436e 3023 else if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
baf05d54
PP
3024 {
3025 /* There must be a valid constant initializer at every array
3026 index. */
3027 tree min = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (t)));
3028 tree max = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (t)));
3029 tree cursor = min;
fae00a0a 3030 for (auto &e: CONSTRUCTOR_ELTS (t))
baf05d54 3031 {
fae00a0a 3032 if (!reduced_constant_expression_p (e.value))
baf05d54 3033 return false;
fae00a0a 3034 if (array_index_cmp (cursor, e.index) != 0)
baf05d54 3035 return false;
fae00a0a
JM
3036 if (TREE_CODE (e.index) == RANGE_EXPR)
3037 cursor = TREE_OPERAND (e.index, 1);
baf05d54
PP
3038 cursor = int_const_binop (PLUS_EXPR, cursor, size_one_node);
3039 }
3040 if (find_array_ctor_elt (t, max) == -1)
3041 return false;
3042 goto ok;
3043 }
b04445d4 3044 else if (cxx_dialect >= cxx20
b599bf9d
PP
3045 && TREE_CODE (TREE_TYPE (t)) == UNION_TYPE)
3046 {
3047 if (CONSTRUCTOR_NELTS (t) == 0)
3048 /* An initialized union has a constructor element. */
3049 return false;
3050 /* And it only initializes one member. */
3051 field = NULL_TREE;
3052 }
6f11ddd8
JM
3053 else
3054 field = next_initializable_field (TYPE_FIELDS (TREE_TYPE (t)));
3055 }
7368cfa4
JM
3056 else
3057 field = NULL_TREE;
fae00a0a 3058 for (auto &e: CONSTRUCTOR_ELTS (t))
125db6a1 3059 {
4930c53e
MP
3060 /* If VAL is null, we're in the middle of initializing this
3061 element. */
fae00a0a 3062 if (!reduced_constant_expression_p (e.value))
125db6a1 3063 return false;
d6ff2207 3064 /* Empty class field may or may not have an initializer. */
fae00a0a 3065 for (; field && e.index != field;
d6ff2207
MP
3066 field = next_initializable_field (DECL_CHAIN (field)))
3067 if (!is_really_empty_class (TREE_TYPE (field),
3068 /*ignore_vptr*/false))
3069 return false;
7368cfa4 3070 if (field)
d6ff2207 3071 field = next_initializable_field (DECL_CHAIN (field));
125db6a1 3072 }
7906797e
MP
3073 /* There could be a non-empty field at the end. */
3074 for (; field; field = next_initializable_field (DECL_CHAIN (field)))
3075 if (!is_really_empty_class (TREE_TYPE (field), /*ignore_vptr*/false))
3076 return false;
baf05d54 3077ok:
7906797e 3078 if (CONSTRUCTOR_NO_CLEARING (t))
7368cfa4 3079 /* All the fields are initialized. */
e8c48716 3080 CONSTRUCTOR_NO_CLEARING (t) = false;
2d76680f
PC
3081 return true;
3082
3083 default:
3084 /* FIXME are we calling this too much? */
3085 return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE;
3086 }
3087}
3088
3089/* Some expressions may have constant operands but are not constant
7d75ea04
JJ
3090 themselves, such as 1/0. Call this function to check for that
3091 condition.
2d76680f
PC
3092
3093 We only call this in places that require an arithmetic constant, not in
3094 places where we might have a non-constant expression that can be a
3095 component of a constant expression, such as the address of a constexpr
3096 variable that might be dereferenced later. */
3097
3098static bool
3099verify_constant (tree t, bool allow_non_constant, bool *non_constant_p,
3100 bool *overflow_p)
3101{
596334fa
JM
3102 if (!*non_constant_p && !reduced_constant_expression_p (t)
3103 && t != void_node)
2d76680f
PC
3104 {
3105 if (!allow_non_constant)
3106 error ("%q+E is not a constant expression", t);
3107 *non_constant_p = true;
3108 }
3109 if (TREE_OVERFLOW_P (t))
3110 {
3111 if (!allow_non_constant)
3112 {
3113 permerror (input_location, "overflow in constant expression");
3114 /* If we're being permissive (and are in an enforcing
3115 context), ignore the overflow. */
3116 if (flag_permissive)
3117 return *non_constant_p;
3118 }
3119 *overflow_p = true;
3120 }
3121 return *non_constant_p;
3122}
3123
253a921b
MP
3124/* Check whether the shift operation with code CODE and type TYPE on LHS
3125 and RHS is undefined. If it is, give an error with an explanation,
3126 and return true; return false otherwise. */
3127
3128static bool
3129cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx,
3130 enum tree_code code, tree type, tree lhs, tree rhs)
3131{
3132 if ((code != LSHIFT_EXPR && code != RSHIFT_EXPR)
3133 || TREE_CODE (lhs) != INTEGER_CST
3134 || TREE_CODE (rhs) != INTEGER_CST)
3135 return false;
3136
3137 tree lhstype = TREE_TYPE (lhs);
3138 unsigned HOST_WIDE_INT uprec = TYPE_PRECISION (TREE_TYPE (lhs));
3139
3140 /* [expr.shift] The behavior is undefined if the right operand
3141 is negative, or greater than or equal to the length in bits
3142 of the promoted left operand. */
3143 if (tree_int_cst_sgn (rhs) == -1)
3144 {
3145 if (!ctx->quiet)
5342156c
MP
3146 permerror (loc, "right operand of shift expression %q+E is negative",
3147 build2_loc (loc, code, type, lhs, rhs));
3148 return (!flag_permissive || ctx->quiet);
253a921b
MP
3149 }
3150 if (compare_tree_int (rhs, uprec) >= 0)
3151 {
3152 if (!ctx->quiet)
a9c697b8
MS
3153 permerror (loc, "right operand of shift expression %q+E is greater "
3154 "than or equal to the precision %wu of the left operand",
3155 build2_loc (loc, code, type, lhs, rhs), uprec);
5342156c 3156 return (!flag_permissive || ctx->quiet);
253a921b
MP
3157 }
3158
3159 /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
3160 if E1 has a signed type and non-negative value, and E1x2^E2 is
3161 representable in the corresponding unsigned type of the result type,
3162 then that value, converted to the result type, is the resulting value;
8ee09943 3163 otherwise, the behavior is undefined.
b04445d4 3164 For C++20:
8ee09943
JJ
3165 The value of E1 << E2 is the unique value congruent to E1 x 2^E2 modulo
3166 2^N, where N is the range exponent of the type of the result. */
3167 if (code == LSHIFT_EXPR
d7651113 3168 && !TYPE_OVERFLOW_WRAPS (lhstype)
8ee09943 3169 && cxx_dialect >= cxx11
b04445d4 3170 && cxx_dialect < cxx20)
253a921b
MP
3171 {
3172 if (tree_int_cst_sgn (lhs) == -1)
3173 {
3174 if (!ctx->quiet)
5342156c
MP
3175 permerror (loc,
3176 "left operand of shift expression %q+E is negative",
3177 build2_loc (loc, code, type, lhs, rhs));
3178 return (!flag_permissive || ctx->quiet);
253a921b
MP
3179 }
3180 /* For signed x << y the following:
3181 (unsigned) x >> ((prec (lhs) - 1) - y)
3182 if > 1, is undefined. The right-hand side of this formula
3183 is the highest bit of the LHS that can be set (starting from 0),
3184 so that the shift doesn't overflow. We then right-shift the LHS
3185 to see whether any other bit is set making the original shift
3186 undefined -- the result is not representable in the corresponding
3187 unsigned type. */
3188 tree t = build_int_cst (unsigned_type_node, uprec - 1);
3189 t = fold_build2 (MINUS_EXPR, unsigned_type_node, t, rhs);
3190 tree ulhs = fold_convert (unsigned_type_for (lhstype), lhs);
3191 t = fold_build2 (RSHIFT_EXPR, TREE_TYPE (ulhs), ulhs, t);
3192 if (tree_int_cst_lt (integer_one_node, t))
3193 {
3194 if (!ctx->quiet)
5342156c
MP
3195 permerror (loc, "shift expression %q+E overflows",
3196 build2_loc (loc, code, type, lhs, rhs));
3197 return (!flag_permissive || ctx->quiet);
253a921b
MP
3198 }
3199 }
3200 return false;
3201}
3202
2d76680f
PC
3203/* Subroutine of cxx_eval_constant_expression.
3204 Attempt to reduce the unary expression tree T to a compile time value.
3205 If successful, return the value. Otherwise issue a diagnostic
3206 and return error_mark_node. */
3207
3208static tree
3e605b20 3209cxx_eval_unary_expression (const constexpr_ctx *ctx, tree t,
12d9ce19 3210 bool /*lval*/,
2d76680f
PC
3211 bool *non_constant_p, bool *overflow_p)
3212{
3213 tree r;
3214 tree orig_arg = TREE_OPERAND (t, 0);
12d9ce19
JM
3215 tree arg = cxx_eval_constant_expression (ctx, orig_arg, /*lval*/false,
3216 non_constant_p, overflow_p);
2d76680f 3217 VERIFY_CONSTANT (arg);
f899317e
JM
3218 location_t loc = EXPR_LOCATION (t);
3219 enum tree_code code = TREE_CODE (t);
3220 tree type = TREE_TYPE (t);
3221 r = fold_unary_loc (loc, code, type, arg);
3222 if (r == NULL_TREE)
3223 {
3224 if (arg == orig_arg)
3225 r = t;
3226 else
3227 r = build1_loc (loc, code, type, arg);
3228 }
2d76680f
PC
3229 VERIFY_CONSTANT (r);
3230 return r;
3231}
3232
ea8661cd
JJ
3233/* Helper function for cxx_eval_binary_expression. Try to optimize
3234 original POINTER_PLUS_EXPR T, LHS p+ RHS, return NULL_TREE if the
3235 generic folding should be used. */
3236
3237static tree
3238cxx_fold_pointer_plus_expression (const constexpr_ctx *ctx, tree t,
3239 tree lhs, tree rhs, bool *non_constant_p,
3240 bool *overflow_p)
3241{
3242 STRIP_NOPS (lhs);
3243 if (TREE_CODE (lhs) != ADDR_EXPR)
3244 return NULL_TREE;
3245
3246 lhs = TREE_OPERAND (lhs, 0);
3247
3248 /* &A[i] p+ j => &A[i + j] */
3249 if (TREE_CODE (lhs) == ARRAY_REF
3250 && TREE_CODE (TREE_OPERAND (lhs, 1)) == INTEGER_CST
3251 && TREE_CODE (rhs) == INTEGER_CST
3252 && TYPE_SIZE_UNIT (TREE_TYPE (lhs))
3253 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs))) == INTEGER_CST)
3254 {
3255 tree orig_type = TREE_TYPE (t);
3256 location_t loc = EXPR_LOCATION (t);
3257 tree type = TREE_TYPE (lhs);
3258
3259 t = fold_convert_loc (loc, ssizetype, TREE_OPERAND (lhs, 1));
3260 tree nelts = array_type_nelts_top (TREE_TYPE (TREE_OPERAND (lhs, 0)));
3261 nelts = cxx_eval_constant_expression (ctx, nelts, false, non_constant_p,
3262 overflow_p);
3263 if (*non_constant_p)
3264 return NULL_TREE;
3265 /* Don't fold an out-of-bound access. */
3266 if (!tree_int_cst_le (t, nelts))
3267 return NULL_TREE;
3268 rhs = cp_fold_convert (ssizetype, rhs);
3269 /* Don't fold if rhs can't be divided exactly by TYPE_SIZE_UNIT.
3270 constexpr int A[1]; ... (char *)&A[0] + 1 */
3271 if (!integer_zerop (fold_build2_loc (loc, TRUNC_MOD_EXPR, sizetype,
3272 rhs, TYPE_SIZE_UNIT (type))))
3273 return NULL_TREE;
3274 /* Make sure to treat the second operand of POINTER_PLUS_EXPR
3275 as signed. */
3276 rhs = fold_build2_loc (loc, EXACT_DIV_EXPR, ssizetype, rhs,
3277 TYPE_SIZE_UNIT (type));
3278 t = size_binop_loc (loc, PLUS_EXPR, rhs, t);
3279 t = build4_loc (loc, ARRAY_REF, type, TREE_OPERAND (lhs, 0),
3280 t, NULL_TREE, NULL_TREE);
3281 t = cp_build_addr_expr (t, tf_warning_or_error);
3282 t = cp_fold_convert (orig_type, t);
3283 return cxx_eval_constant_expression (ctx, t, /*lval*/false,
3284 non_constant_p, overflow_p);
3285 }
3286
3287 return NULL_TREE;
3288}
3289
51d464b6
JJ
3290/* Try to fold expressions like
3291 (struct S *) (&a[0].D.2378 + 12)
3292 into
3293 &MEM <struct T> [(void *)&a + 12B]
3294 This is something normally done by gimple_fold_stmt_to_constant_1
3295 on GIMPLE, but is undesirable on GENERIC if we are e.g. going to
3296 dereference the address because some details are lost.
3297 For pointer comparisons we want such folding though so that
3298 match.pd address_compare optimization works. */
3299
3300static tree
3301cxx_maybe_fold_addr_pointer_plus (tree t)
3302{
3303 while (CONVERT_EXPR_P (t)
3304 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0))))
3305 t = TREE_OPERAND (t, 0);
3306 if (TREE_CODE (t) != POINTER_PLUS_EXPR)
3307 return NULL_TREE;
3308 tree op0 = TREE_OPERAND (t, 0);
3309 tree op1 = TREE_OPERAND (t, 1);
3310 if (TREE_CODE (op1) != INTEGER_CST)
3311 return NULL_TREE;
3312 while (CONVERT_EXPR_P (op0)
3313 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
3314 op0 = TREE_OPERAND (op0, 0);
3315 if (TREE_CODE (op0) != ADDR_EXPR)
3316 return NULL_TREE;
3317 op1 = fold_convert (ptr_type_node, op1);
3318 tree r = fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (op0)), op0, op1);
3319 return build1_loc (EXPR_LOCATION (t), ADDR_EXPR, TREE_TYPE (op0), r);
3320}
3321
2d76680f
PC
3322/* Subroutine of cxx_eval_constant_expression.
3323 Like cxx_eval_unary_expression, except for binary expressions. */
3324
3325static tree
3e605b20 3326cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t,
dfffecb8 3327 bool lval,
2d76680f
PC
3328 bool *non_constant_p, bool *overflow_p)
3329{
618d6c1c 3330 tree r = NULL_TREE;
2d76680f
PC
3331 tree orig_lhs = TREE_OPERAND (t, 0);
3332 tree orig_rhs = TREE_OPERAND (t, 1);
3333 tree lhs, rhs;
12d9ce19 3334 lhs = cxx_eval_constant_expression (ctx, orig_lhs, /*lval*/false,
5a804683 3335 non_constant_p, overflow_p);
c8a66fc9
JM
3336 /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer
3337 subtraction. */
3338 if (*non_constant_p)
3339 return t;
12d9ce19 3340 rhs = cxx_eval_constant_expression (ctx, orig_rhs, /*lval*/false,
5a804683 3341 non_constant_p, overflow_p);
c8a66fc9
JM
3342 if (*non_constant_p)
3343 return t;
f899317e
JM
3344
3345 location_t loc = EXPR_LOCATION (t);
3346 enum tree_code code = TREE_CODE (t);
3347 tree type = TREE_TYPE (t);
618d6c1c
PP
3348
3349 if (code == EQ_EXPR || code == NE_EXPR)
3350 {
3351 bool is_code_eq = (code == EQ_EXPR);
3352
3353 if (TREE_CODE (lhs) == PTRMEM_CST
3354 && TREE_CODE (rhs) == PTRMEM_CST)
9b0607de
JM
3355 {
3356 tree lmem = PTRMEM_CST_MEMBER (lhs);
3357 tree rmem = PTRMEM_CST_MEMBER (rhs);
3358 bool eq;
3359 if (TREE_CODE (lmem) == TREE_CODE (rmem)
3360 && TREE_CODE (lmem) == FIELD_DECL
3361 && TREE_CODE (DECL_CONTEXT (lmem)) == UNION_TYPE
3362 && same_type_p (DECL_CONTEXT (lmem),
3363 DECL_CONTEXT (rmem)))
3364 /* If both refer to (possibly different) members of the same union
3365 (12.3), they compare equal. */
3366 eq = true;
3367 else
3368 eq = cp_tree_equal (lhs, rhs);
3369 r = constant_boolean_node (eq == is_code_eq, type);
3370 }
618d6c1c
PP
3371 else if ((TREE_CODE (lhs) == PTRMEM_CST
3372 || TREE_CODE (rhs) == PTRMEM_CST)
3373 && (null_member_pointer_value_p (lhs)
3374 || null_member_pointer_value_p (rhs)))
3375 r = constant_boolean_node (!is_code_eq, type);
dd5dda56
JM
3376 else if (TREE_CODE (lhs) == PTRMEM_CST)
3377 lhs = cplus_expand_constant (lhs);
3378 else if (TREE_CODE (rhs) == PTRMEM_CST)
3379 rhs = cplus_expand_constant (rhs);
618d6c1c 3380 }
51d464b6
JJ
3381 if (r == NULL_TREE
3382 && TREE_CODE_CLASS (code) == tcc_comparison
3383 && POINTER_TYPE_P (TREE_TYPE (lhs)))
3384 {
3385 if (tree lhso = cxx_maybe_fold_addr_pointer_plus (lhs))
3386 lhs = fold_convert (TREE_TYPE (lhs), lhso);
3387 if (tree rhso = cxx_maybe_fold_addr_pointer_plus (rhs))
3388 rhs = fold_convert (TREE_TYPE (rhs), rhso);
3389 }
8bada5cd
MS
3390 if (code == POINTER_PLUS_EXPR && !*non_constant_p
3391 && integer_zerop (lhs) && !integer_zerop (rhs))
3392 {
3393 if (!ctx->quiet)
3394 error ("arithmetic involving a null pointer in %qE", lhs);
bf533db8 3395 *non_constant_p = true;
8bada5cd
MS
3396 return t;
3397 }
ea8661cd
JJ
3398 else if (code == POINTER_PLUS_EXPR)
3399 r = cxx_fold_pointer_plus_expression (ctx, t, lhs, rhs, non_constant_p,
3400 overflow_p);
b7689b96
JM
3401 else if (code == SPACESHIP_EXPR)
3402 {
4ed1dc12 3403 r = genericize_spaceship (loc, type, lhs, rhs);
5c64df80
JJ
3404 return cxx_eval_constant_expression (ctx, r, lval, non_constant_p,
3405 overflow_p);
b7689b96 3406 }
618d6c1c
PP
3407
3408 if (r == NULL_TREE)
53caa472
JM
3409 {
3410 if (ctx->manifestly_const_eval
3411 && (flag_constexpr_fp_except
3412 || TREE_CODE (type) != REAL_TYPE))
f9e900ce
JJ
3413 {
3414 auto ofcc = make_temp_override (folding_cxx_constexpr, true);
3415 r = fold_binary_initializer_loc (loc, code, type, lhs, rhs);
3416 }
53caa472
JM
3417 else
3418 r = fold_binary_loc (loc, code, type, lhs, rhs);
3419 }
618d6c1c 3420
4866b2f5
JJ
3421 if (r == NULL_TREE
3422 && (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
3423 && TREE_CODE (lhs) == INTEGER_CST
3424 && TREE_CODE (rhs) == INTEGER_CST
3425 && wi::neg_p (wi::to_wide (rhs)))
3426 {
3427 /* For diagnostics and -fpermissive emulate previous behavior of
3428 handling shifts by negative amount. */
3429 tree nrhs = const_unop (NEGATE_EXPR, TREE_TYPE (rhs), rhs);
3430 if (nrhs)
3431 r = fold_binary_loc (loc,
3432 code == LSHIFT_EXPR ? RSHIFT_EXPR : LSHIFT_EXPR,
3433 type, lhs, nrhs);
3434 }
3435
f899317e
JM
3436 if (r == NULL_TREE)
3437 {
3438 if (lhs == orig_lhs && rhs == orig_rhs)
3439 r = t;
3440 else
3441 r = build2_loc (loc, code, type, lhs, rhs);
3442 }
253a921b
MP
3443 else if (cxx_eval_check_shift_p (loc, ctx, code, type, lhs, rhs))
3444 *non_constant_p = true;
c8a66fc9
JM
3445 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
3446 a local array in a constexpr function. */
71a93b08 3447 bool ptr = INDIRECT_TYPE_P (TREE_TYPE (lhs));
caee690e 3448 if (!ptr)
134efa82 3449 VERIFY_CONSTANT (r);
2d76680f
PC
3450 return r;
3451}
3452
3453/* Subroutine of cxx_eval_constant_expression.
3454 Attempt to evaluate condition expressions. Dead branches are not
3455 looked into. */
3456
3457static tree
3e605b20 3458cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t,
92a596e8 3459 bool lval,
56632b27
JM
3460 bool *non_constant_p, bool *overflow_p,
3461 tree *jump_target)
2d76680f 3462{
3e605b20 3463 tree val = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
12d9ce19 3464 /*lval*/false,
5a804683 3465 non_constant_p, overflow_p);
2d76680f 3466 VERIFY_CONSTANT (val);
117c6426
JJ
3467 if (TREE_CODE (t) == IF_STMT && IF_STMT_CONSTEVAL_P (t))
3468 {
3469 /* Evaluate the condition as if it was
3470 if (__builtin_is_constant_evaluated ()), i.e. defer it if not
3471 ctx->manifestly_const_eval (as sometimes we try to constant evaluate
3472 without manifestly_const_eval even expressions or parts thereof which
3473 will later be manifestly const_eval evaluated), otherwise fold it to
3474 true. */
3475 if (ctx->manifestly_const_eval)
3476 val = boolean_true_node;
3477 else
3478 {
3479 *non_constant_p = true;
3480 return t;
3481 }
3482 }
2d76680f
PC
3483 /* Don't VERIFY_CONSTANT the other operands. */
3484 if (integer_zerop (val))
43574e4f
JJ
3485 val = TREE_OPERAND (t, 2);
3486 else
3487 val = TREE_OPERAND (t, 1);
3488 if (TREE_CODE (t) == IF_STMT && !val)
3489 val = void_node;
3490 return cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
3491 overflow_p, jump_target);
2d76680f
PC
3492}
3493
f370e36d
JJ
3494/* Subroutine of cxx_eval_constant_expression.
3495 Attempt to evaluate vector condition expressions. Unlike
3496 cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal
3497 ternary arithmetics operation, where all 3 arguments have to be
3498 evaluated as constants and then folding computes the result from
3499 them. */
3500
3501static tree
3502cxx_eval_vector_conditional_expression (const constexpr_ctx *ctx, tree t,
3503 bool *non_constant_p, bool *overflow_p)
3504{
3505 tree arg1 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
3506 /*lval*/false,
3507 non_constant_p, overflow_p);
3508 VERIFY_CONSTANT (arg1);
3509 tree arg2 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
3510 /*lval*/false,
3511 non_constant_p, overflow_p);
3512 VERIFY_CONSTANT (arg2);
3513 tree arg3 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2),
3514 /*lval*/false,
3515 non_constant_p, overflow_p);
3516 VERIFY_CONSTANT (arg3);
3517 location_t loc = EXPR_LOCATION (t);
3518 tree type = TREE_TYPE (t);
3519 tree r = fold_ternary_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
3520 if (r == NULL_TREE)
3521 {
3522 if (arg1 == TREE_OPERAND (t, 0)
3523 && arg2 == TREE_OPERAND (t, 1)
3524 && arg3 == TREE_OPERAND (t, 2))
3525 r = t;
3526 else
3527 r = build3_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
3528 }
3529 VERIFY_CONSTANT (r);
3530 return r;
3531}
3532
ceaaf873
JM
3533/* Returns less than, equal to, or greater than zero if KEY is found to be
3534 less than, to match, or to be greater than the constructor_elt's INDEX. */
3535
3536static int
3537array_index_cmp (tree key, tree index)
3538{
3539 gcc_assert (TREE_CODE (key) == INTEGER_CST);
3540
3541 switch (TREE_CODE (index))
3542 {
3543 case INTEGER_CST:
3544 return tree_int_cst_compare (key, index);
3545 case RANGE_EXPR:
3546 {
3547 tree lo = TREE_OPERAND (index, 0);
3548 tree hi = TREE_OPERAND (index, 1);
3549 if (tree_int_cst_lt (key, lo))
3550 return -1;
3551 else if (tree_int_cst_lt (hi, key))
3552 return 1;
3553 else
3554 return 0;
3555 }
3556 default:
3557 gcc_unreachable ();
3558 }
3559}
3560
3561/* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1
3562 if none. If INSERT is true, insert a matching element rather than fail. */
3563
3564static HOST_WIDE_INT
582d2481 3565find_array_ctor_elt (tree ary, tree dindex, bool insert)
ceaaf873
JM
3566{
3567 if (tree_int_cst_sgn (dindex) < 0)
3568 return -1;
3569
3570 unsigned HOST_WIDE_INT i = tree_to_uhwi (dindex);
3571 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ary);
3572 unsigned HOST_WIDE_INT len = vec_safe_length (elts);
3573
3574 unsigned HOST_WIDE_INT end = len;
3575 unsigned HOST_WIDE_INT begin = 0;
3576
3577 /* If the last element of the CONSTRUCTOR has its own index, we can assume
3578 that the same is true of the other elements and index directly. */
3579 if (end > 0)
3580 {
fe217ba0 3581 tree cindex = (*elts)[end - 1].index;
c7c09af8
JJ
3582 if (cindex == NULL_TREE)
3583 {
3584 /* Verify that if the last index is missing, all indexes
3585 are missing. */
3586 if (flag_checking)
3587 for (unsigned int j = 0; j < len - 1; ++j)
3588 gcc_assert ((*elts)[j].index == NULL_TREE);
3589 if (i < end)
3590 return i;
3591 else
3592 {
3593 begin = end;
3594 if (i == end)
3595 /* If the element is to be added right at the end,
3596 make sure it is added with cleared index too. */
3597 dindex = NULL_TREE;
3598 else if (insert)
3599 /* Otherwise, in order not to break the assumption
3600 that CONSTRUCTOR either has all indexes or none,
3601 we need to add indexes to all elements. */
3602 for (unsigned int j = 0; j < len; ++j)
3603 (*elts)[j].index = build_int_cst (TREE_TYPE (dindex), j);
3604 }
3605 }
3606 else if (TREE_CODE (cindex) == INTEGER_CST
3607 && compare_tree_int (cindex, end - 1) == 0)
ceaaf873
JM
3608 {
3609 if (i < end)
3610 return i;
3611 else
3612 begin = end;
3613 }
3614 }
3615
3616 /* Otherwise, find a matching index by means of a binary search. */
3617 while (begin != end)
3618 {
3619 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
a7ccb9e7
JM
3620 constructor_elt &elt = (*elts)[middle];
3621 tree idx = elt.index;
ceaaf873 3622
a7ccb9e7 3623 int cmp = array_index_cmp (dindex, idx);
ceaaf873
JM
3624 if (cmp < 0)
3625 end = middle;
3626 else if (cmp > 0)
3627 begin = middle + 1;
3628 else
a7ccb9e7
JM
3629 {
3630 if (insert && TREE_CODE (idx) == RANGE_EXPR)
3631 {
3632 /* We need to split the range. */
3633 constructor_elt e;
3634 tree lo = TREE_OPERAND (idx, 0);
3635 tree hi = TREE_OPERAND (idx, 1);
fe217ba0
JJ
3636 tree value = elt.value;
3637 dindex = fold_convert (sizetype, dindex);
a7ccb9e7
JM
3638 if (tree_int_cst_lt (lo, dindex))
3639 {
3640 /* There are still some lower elts; shorten the range. */
3641 tree new_hi = int_const_binop (MINUS_EXPR, dindex,
3642 size_one_node);
3643 if (tree_int_cst_equal (lo, new_hi))
3644 /* Only one element left, no longer a range. */
3645 elt.index = lo;
3646 else
3647 TREE_OPERAND (idx, 1) = new_hi;
3648 /* Append the element we want to insert. */
3649 ++middle;
3650 e.index = dindex;
fe217ba0 3651 e.value = unshare_constructor (value);
a7ccb9e7
JM
3652 vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e);
3653 }
3654 else
3655 /* No lower elts, the range elt is now ours. */
3656 elt.index = dindex;
3657
3658 if (tree_int_cst_lt (dindex, hi))
3659 {
3660 /* There are still some higher elts; append a range. */
3661 tree new_lo = int_const_binop (PLUS_EXPR, dindex,
3662 size_one_node);
3663 if (tree_int_cst_equal (new_lo, hi))
3664 e.index = hi;
3665 else
3666 e.index = build2 (RANGE_EXPR, sizetype, new_lo, hi);
fe217ba0
JJ
3667 e.value = unshare_constructor (value);
3668 vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e);
a7ccb9e7
JM
3669 }
3670 }
3671 return middle;
3672 }
ceaaf873
JM
3673 }
3674
3675 if (insert)
3676 {
3677 constructor_elt e = { dindex, NULL_TREE };
3678 vec_safe_insert (CONSTRUCTOR_ELTS (ary), end, e);
3679 return end;
3680 }
3681
3682 return -1;
3683}
3684
37244b21
PP
3685/* Return a pointer to the constructor_elt of CTOR which matches INDEX. If no
3686 matching constructor_elt exists, then add one to CTOR.
3687
3688 As an optimization, if POS_HINT is non-negative then it is used as a guess
3689 for the (integer) index of the matching constructor_elt within CTOR. */
3690
3691static constructor_elt *
077dd9b3 3692get_or_insert_ctor_field (tree ctor, tree index, int pos_hint = -1)
37244b21
PP
3693{
3694 /* Check the hint first. */
3695 if (pos_hint >= 0 && (unsigned)pos_hint < CONSTRUCTOR_NELTS (ctor)
3696 && CONSTRUCTOR_ELT (ctor, pos_hint)->index == index)
3697 return CONSTRUCTOR_ELT (ctor, pos_hint);
3698
3699 tree type = TREE_TYPE (ctor);
3700 if (TREE_CODE (type) == VECTOR_TYPE && index == NULL_TREE)
3701 {
3702 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (ctor), index, NULL_TREE);
3703 return &CONSTRUCTOR_ELTS (ctor)->last();
3704 }
3705 else if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
3706 {
e069285c
PP
3707 if (TREE_CODE (index) == RANGE_EXPR)
3708 {
3709 /* Support for RANGE_EXPR index lookups is currently limited to
3710 accessing an existing element via POS_HINT, or appending a new
3711 element to the end of CTOR. ??? Support for other access
3712 patterns may also be needed. */
3713 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
3714 if (vec_safe_length (elts))
3715 {
3716 tree lo = TREE_OPERAND (index, 0);
3717 gcc_assert (array_index_cmp (elts->last().index, lo) < 0);
3718 }
3719 CONSTRUCTOR_APPEND_ELT (elts, index, NULL_TREE);
3720 return &elts->last();
3721 }
3722
37244b21
PP
3723 HOST_WIDE_INT i = find_array_ctor_elt (ctor, index, /*insert*/true);
3724 gcc_assert (i >= 0);
3725 constructor_elt *cep = CONSTRUCTOR_ELT (ctor, i);
3726 gcc_assert (cep->index == NULL_TREE
3727 || TREE_CODE (cep->index) != RANGE_EXPR);
3728 return cep;
3729 }
3730 else
3731 {
94ff4c9d
JM
3732 gcc_assert (TREE_CODE (index) == FIELD_DECL
3733 && (same_type_ignoring_top_level_qualifiers_p
3734 (DECL_CONTEXT (index), TREE_TYPE (ctor))));
37244b21
PP
3735
3736 /* We must keep the CONSTRUCTOR's ELTS in FIELD order.
3737 Usually we meet initializers in that order, but it is
3738 possible for base types to be placed not in program
3739 order. */
3740 tree fields = TYPE_FIELDS (DECL_CONTEXT (index));
3741 unsigned HOST_WIDE_INT idx = 0;
3742 constructor_elt *cep = NULL;
3743
3744 /* Check if we're changing the active member of a union. */
3745 if (TREE_CODE (type) == UNION_TYPE && CONSTRUCTOR_NELTS (ctor)
3746 && CONSTRUCTOR_ELT (ctor, 0)->index != index)
3747 vec_safe_truncate (CONSTRUCTOR_ELTS (ctor), 0);
3748 /* If the bit offset of INDEX is larger than that of the last
3749 constructor_elt, then we can just immediately append a new
3750 constructor_elt to the end of CTOR. */
3751 else if (CONSTRUCTOR_NELTS (ctor)
3752 && tree_int_cst_compare (bit_position (index),
3753 bit_position (CONSTRUCTOR_ELTS (ctor)
3754 ->last().index)) > 0)
3755 {
3756 idx = CONSTRUCTOR_NELTS (ctor);
3757 goto insert;
3758 }
3759
3760 /* Otherwise, we need to iterate over CTOR to find or insert INDEX
3761 appropriately. */
3762
3763 for (; vec_safe_iterate (CONSTRUCTOR_ELTS (ctor), idx, &cep);
3764 idx++, fields = DECL_CHAIN (fields))
3765 {
3766 if (index == cep->index)
3767 goto found;
3768
3769 /* The field we're initializing must be on the field
3770 list. Look to see if it is present before the
3771 field the current ELT initializes. */
3772 for (; fields != cep->index; fields = DECL_CHAIN (fields))
3773 if (index == fields)
3774 goto insert;
3775 }
3776 /* We fell off the end of the CONSTRUCTOR, so insert a new
3777 entry at the end. */
3778
3779 insert:
3780 {
3781 constructor_elt ce = { index, NULL_TREE };
3782
3783 vec_safe_insert (CONSTRUCTOR_ELTS (ctor), idx, ce);
3784 cep = CONSTRUCTOR_ELT (ctor, idx);
3785 }
3786 found:;
3787
3788 return cep;
3789 }
3790}
3791
abdc16c8
MS
3792/* Under the control of CTX, issue a detailed diagnostic for
3793 an out-of-bounds subscript INDEX into the expression ARRAY. */
3794
3795static void
043666e0 3796diag_array_subscript (location_t loc, const constexpr_ctx *ctx, tree array, tree index)
abdc16c8
MS
3797{
3798 if (!ctx->quiet)
3799 {
3800 tree arraytype = TREE_TYPE (array);
3801
3802 /* Convert the unsigned array subscript to a signed integer to avoid
3803 printing huge numbers for small negative values. */
3804 tree sidx = fold_convert (ssizetype, index);
043666e0 3805 STRIP_ANY_LOCATION_WRAPPER (array);
abdc16c8
MS
3806 if (DECL_P (array))
3807 {
08b3748c 3808 if (TYPE_DOMAIN (arraytype))
043666e0
JM
3809 error_at (loc, "array subscript value %qE is outside the bounds "
3810 "of array %qD of type %qT", sidx, array, arraytype);
08b3748c 3811 else
043666e0
JM
3812 error_at (loc, "nonzero array subscript %qE is used with array %qD of "
3813 "type %qT with unknown bounds", sidx, array, arraytype);
abdc16c8
MS
3814 inform (DECL_SOURCE_LOCATION (array), "declared here");
3815 }
08b3748c 3816 else if (TYPE_DOMAIN (arraytype))
043666e0
JM
3817 error_at (loc, "array subscript value %qE is outside the bounds "
3818 "of array type %qT", sidx, arraytype);
08b3748c 3819 else
043666e0
JM
3820 error_at (loc, "nonzero array subscript %qE is used with array of type %qT "
3821 "with unknown bounds", sidx, arraytype);
abdc16c8
MS
3822 }
3823}
ceaaf873 3824
74f8705e
MP
3825/* Return the number of elements for TYPE (which is an ARRAY_TYPE or
3826 a VECTOR_TYPE). */
3827
3828static tree
3829get_array_or_vector_nelts (const constexpr_ctx *ctx, tree type,
3830 bool *non_constant_p, bool *overflow_p)
3831{
3832 tree nelts;
3833 if (TREE_CODE (type) == ARRAY_TYPE)
3834 {
3835 if (TYPE_DOMAIN (type))
3836 nelts = array_type_nelts_top (type);
3837 else
3838 nelts = size_zero_node;
3839 }
3840 else if (VECTOR_TYPE_P (type))
3841 nelts = size_int (TYPE_VECTOR_SUBPARTS (type));
3842 else
3843 gcc_unreachable ();
3844
3845 /* For VLAs, the number of elements won't be an integer constant. */
3846 nelts = cxx_eval_constant_expression (ctx, nelts, false,
3847 non_constant_p, overflow_p);
3848 return nelts;
3849}
3850
d6b46fca
NS
3851/* Extract element INDEX consisting of CHARS_PER_ELT chars from
3852 STRING_CST STRING. */
3853
3854static tree
3855extract_string_elt (tree string, unsigned chars_per_elt, unsigned index)
3856{
3857 tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (string)));
3858 tree r;
3859
3860 if (chars_per_elt == 1)
3861 r = build_int_cst (type, TREE_STRING_POINTER (string)[index]);
3862 else
3863 {
3864 const unsigned char *ptr
3865 = ((const unsigned char *)TREE_STRING_POINTER (string)
3866 + index * chars_per_elt);
3867 r = native_interpret_expr (type, ptr, chars_per_elt);
3868 }
3869 return r;
3870}
3871
043666e0
JM
3872/* Subroutine of cxx_eval_array_reference. T is an ARRAY_REF; evaluate the
3873 subscript, diagnose any problems with it, and return the result. */
3874
3875static tree
3876eval_and_check_array_index (const constexpr_ctx *ctx,
3877 tree t, bool allow_one_past,
3878 bool *non_constant_p, bool *overflow_p)
3879{
f9d0ca40 3880 location_t loc = cp_expr_loc_or_input_loc (t);
043666e0
JM
3881 tree ary = TREE_OPERAND (t, 0);
3882 t = TREE_OPERAND (t, 1);
3883 tree index = cxx_eval_constant_expression (ctx, t, false,
3884 non_constant_p, overflow_p);
3885 VERIFY_CONSTANT (index);
3886
3887 if (!tree_fits_shwi_p (index)
3888 || tree_int_cst_sgn (index) < 0)
3889 {
3890 diag_array_subscript (loc, ctx, ary, index);
3891 *non_constant_p = true;
3892 return t;
3893 }
3894
3895 tree nelts = get_array_or_vector_nelts (ctx, TREE_TYPE (ary), non_constant_p,
3896 overflow_p);
3897 VERIFY_CONSTANT (nelts);
3898 if (allow_one_past
3899 ? !tree_int_cst_le (index, nelts)
3900 : !tree_int_cst_lt (index, nelts))
3901 {
3902 diag_array_subscript (loc, ctx, ary, index);
3903 *non_constant_p = true;
3904 return t;
3905 }
3906
3907 return index;
3908}
3909
2d76680f
PC
3910/* Subroutine of cxx_eval_constant_expression.
3911 Attempt to reduce a reference to an array slot. */
3912
3913static tree
3e605b20 3914cxx_eval_array_reference (const constexpr_ctx *ctx, tree t,
92a596e8 3915 bool lval,
2d76680f
PC
3916 bool *non_constant_p, bool *overflow_p)
3917{
3918 tree oldary = TREE_OPERAND (t, 0);
3e605b20 3919 tree ary = cxx_eval_constant_expression (ctx, oldary,
92a596e8 3920 lval,
5a804683 3921 non_constant_p, overflow_p);
2d76680f
PC
3922 if (*non_constant_p)
3923 return t;
bc2687dd
JJ
3924 if (!lval
3925 && TREE_CODE (ary) == VIEW_CONVERT_EXPR
043666e0
JM
3926 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary, 0)))
3927 && TREE_TYPE (t) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary, 0))))
3928 ary = TREE_OPERAND (ary, 0);
3b9997bb 3929
043666e0
JM
3930 tree oldidx = TREE_OPERAND (t, 1);
3931 tree index = eval_and_check_array_index (ctx, t, lval,
3932 non_constant_p, overflow_p);
3933 if (*non_constant_p)
3934 return t;
5453bfed 3935
bc2a38df
JJ
3936 if (lval && ary == oldary && index == oldidx)
3937 return t;
3938 else if (lval)
3939 return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL);
3940
043666e0
JM
3941 unsigned len = 0, elem_nchars = 1;
3942 tree elem_type = TREE_TYPE (TREE_TYPE (ary));
3943 if (TREE_CODE (ary) == CONSTRUCTOR)
3944 len = CONSTRUCTOR_NELTS (ary);
3945 else if (TREE_CODE (ary) == STRING_CST)
3946 {
3947 elem_nchars = (TYPE_PRECISION (elem_type)
3948 / TYPE_PRECISION (char_type_node));
3949 len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars;
3950 }
3951 else if (TREE_CODE (ary) == VECTOR_CST)
3952 /* We don't create variable-length VECTOR_CSTs. */
3953 len = VECTOR_CST_NELTS (ary).to_constant ();
3954 else
3955 {
3956 /* We can't do anything with other tree codes, so use
3957 VERIFY_CONSTANT to complain and fail. */
3958 VERIFY_CONSTANT (ary);
3959 gcc_unreachable ();
3960 }
3961
ceaaf873 3962 bool found;
043666e0 3963 HOST_WIDE_INT i = 0;
ceaaf873
JM
3964 if (TREE_CODE (ary) == CONSTRUCTOR)
3965 {
3966 HOST_WIDE_INT ix = find_array_ctor_elt (ary, index);
3967 found = (ix >= 0);
3968 if (found)
3969 i = ix;
3b9997bb 3970 }
ceaaf873 3971 else
043666e0
JM
3972 {
3973 i = tree_to_shwi (index);
3974 found = (i < len);
3975 }
3b9997bb 3976
fd2bfee5
JM
3977 if (found)
3978 {
3979 tree r;
3980 if (TREE_CODE (ary) == CONSTRUCTOR)
3981 r = (*CONSTRUCTOR_ELTS (ary))[i].value;
3982 else if (TREE_CODE (ary) == VECTOR_CST)
3983 r = VECTOR_CST_ELT (ary, i);
fd2bfee5 3984 else
d6b46fca
NS
3985 r = extract_string_elt (ary, elem_nchars, i);
3986
fd2bfee5
JM
3987 if (r)
3988 /* Don't VERIFY_CONSTANT here. */
3989 return r;
2d76680f 3990
fd2bfee5 3991 /* Otherwise the element doesn't have a value yet. */
2d76680f 3992 }
3b9997bb 3993
fd2bfee5
JM
3994 /* Not found. */
3995
3996 if (TREE_CODE (ary) == CONSTRUCTOR
e8c48716 3997 && CONSTRUCTOR_NO_CLEARING (ary))
2d76680f 3998 {
fd2bfee5
JM
3999 /* 'ary' is part of the aggregate initializer we're currently
4000 building; if there's no initializer for this element yet,
4001 that's an error. */
4002 if (!ctx->quiet)
4003 error ("accessing uninitialized array element");
4004 *non_constant_p = true;
4005 return t;
2d76680f 4006 }
fd2bfee5
JM
4007
4008 /* If it's within the array bounds but doesn't have an explicit
0712ea63
JM
4009 initializer, it's initialized from {}. But use build_value_init
4010 directly for non-aggregates to avoid creating a garbage CONSTRUCTOR. */
4011 tree val;
0df73bee 4012 constexpr_ctx new_ctx;
a688c284
PP
4013 if (is_really_empty_class (elem_type, /*ignore_vptr*/false))
4014 return build_constructor (elem_type, NULL);
4015 else if (CP_AGGREGATE_TYPE_P (elem_type))
0712ea63
JM
4016 {
4017 tree empty_ctor = build_constructor (init_list_type_node, NULL);
4018 val = digest_init (elem_type, empty_ctor, tf_warning_or_error);
a42f8120
MP
4019 }
4020 else
4021 val = build_value_init (elem_type, tf_warning_or_error);
4022
4023 if (!SCALAR_TYPE_P (elem_type))
4024 {
0df73bee 4025 new_ctx = *ctx;
a42f8120
MP
4026 if (ctx->object)
4027 /* If there was no object, don't add one: it could confuse us
4028 into thinking we're modifying a const object. */
4029 new_ctx.object = t;
0df73bee
MP
4030 new_ctx.ctor = build_constructor (elem_type, NULL);
4031 ctx = &new_ctx;
0712ea63 4032 }
0df73bee
MP
4033 t = cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
4034 overflow_p);
a42f8120 4035 if (!SCALAR_TYPE_P (elem_type) && t != ctx->ctor)
0df73bee
MP
4036 free_constructor (ctx->ctor);
4037 return t;
2d76680f
PC
4038}
4039
4040/* Subroutine of cxx_eval_constant_expression.
4041 Attempt to reduce a field access of a value of class type. */
4042
4043static tree
3e605b20 4044cxx_eval_component_reference (const constexpr_ctx *ctx, tree t,
92a596e8 4045 bool lval,
2d76680f
PC
4046 bool *non_constant_p, bool *overflow_p)
4047{
4048 unsigned HOST_WIDE_INT i;
4049 tree field;
4050 tree value;
4051 tree part = TREE_OPERAND (t, 1);
4052 tree orig_whole = TREE_OPERAND (t, 0);
3e605b20 4053 tree whole = cxx_eval_constant_expression (ctx, orig_whole,
92a596e8 4054 lval,
5a804683 4055 non_constant_p, overflow_p);
a7f8415c 4056 if (INDIRECT_REF_P (whole)
bf533db8
JJ
4057 && integer_zerop (TREE_OPERAND (whole, 0)))
4058 {
4059 if (!ctx->quiet)
4060 error ("dereferencing a null pointer in %qE", orig_whole);
4061 *non_constant_p = true;
4062 return t;
4063 }
8bada5cd 4064
dcdbc004
JM
4065 if (TREE_CODE (whole) == PTRMEM_CST)
4066 whole = cplus_expand_constant (whole);
2d76680f
PC
4067 if (whole == orig_whole)
4068 return t;
92a596e8 4069 if (lval)
2d76680f
PC
4070 return fold_build3 (COMPONENT_REF, TREE_TYPE (t),
4071 whole, part, NULL_TREE);
4072 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
4073 CONSTRUCTOR. */
4074 if (!*non_constant_p && TREE_CODE (whole) != CONSTRUCTOR)
4075 {
2b3ab879 4076 if (!ctx->quiet)
2d76680f
PC
4077 error ("%qE is not a constant expression", orig_whole);
4078 *non_constant_p = true;
4079 }
4080 if (DECL_MUTABLE_P (part))
4081 {
2b3ab879 4082 if (!ctx->quiet)
2d76680f
PC
4083 error ("mutable %qD is not usable in a constant expression", part);
4084 *non_constant_p = true;
4085 }
4086 if (*non_constant_p)
4087 return t;
2db613e5 4088 bool pmf = TYPE_PTRMEMFUNC_P (TREE_TYPE (whole));
2d76680f
PC
4089 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
4090 {
2db613e5
JM
4091 /* Use name match for PMF fields, as a variant will have a
4092 different FIELD_DECL with a different type. */
4093 if (pmf ? DECL_NAME (field) == DECL_NAME (part)
4094 : field == part)
60813a46
JM
4095 {
4096 if (value)
5b884e94
JJ
4097 {
4098 STRIP_ANY_LOCATION_WRAPPER (value);
4099 return value;
4100 }
60813a46
JM
4101 else
4102 /* We're in the middle of initializing it. */
4103 break;
4104 }
2d76680f
PC
4105 }
4106 if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE
4107 && CONSTRUCTOR_NELTS (whole) > 0)
4108 {
4109 /* DR 1188 says we don't have to deal with this. */
2b3ab879 4110 if (!ctx->quiet)
b599bf9d
PP
4111 {
4112 constructor_elt *cep = CONSTRUCTOR_ELT (whole, 0);
4113 if (cep->value == NULL_TREE)
4114 error ("accessing uninitialized member %qD", part);
4115 else
4116 error ("accessing %qD member instead of initialized %qD member in "
4117 "constant expression", part, cep->index);
4118 }
2d76680f
PC
4119 *non_constant_p = true;
4120 return t;
4121 }
4122
188e53bd
JM
4123 /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty
4124 classes never get represented; throw together a value now. */
dbcd32f8 4125 if (is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
188e53bd
JM
4126 return build_constructor (TREE_TYPE (t), NULL);
4127
2db613e5
JM
4128 gcc_assert (DECL_CONTEXT (part) == TYPE_MAIN_VARIANT (TREE_TYPE (whole)));
4129
e8c48716 4130 if (CONSTRUCTOR_NO_CLEARING (whole))
3e605b20
JM
4131 {
4132 /* 'whole' is part of the aggregate initializer we're currently
4133 building; if there's no initializer for this member yet, that's an
188e53bd 4134 error. */
2b3ab879 4135 if (!ctx->quiet)
3e605b20
JM
4136 error ("accessing uninitialized member %qD", part);
4137 *non_constant_p = true;
4138 return t;
4139 }
4140
2d76680f
PC
4141 /* If there's no explicit init for this field, it's value-initialized. */
4142 value = build_value_init (TREE_TYPE (t), tf_warning_or_error);
3e605b20 4143 return cxx_eval_constant_expression (ctx, value,
92a596e8 4144 lval,
5a804683 4145 non_constant_p, overflow_p);
2d76680f
PC
4146}
4147
4148/* Subroutine of cxx_eval_constant_expression.
4149 Attempt to reduce a field access of a value of class type that is
4150 expressed as a BIT_FIELD_REF. */
4151
4152static tree
3e605b20 4153cxx_eval_bit_field_ref (const constexpr_ctx *ctx, tree t,
92a596e8 4154 bool lval,
2d76680f
PC
4155 bool *non_constant_p, bool *overflow_p)
4156{
4157 tree orig_whole = TREE_OPERAND (t, 0);
4158 tree retval, fldval, utype, mask;
4159 bool fld_seen = false;
4160 HOST_WIDE_INT istart, isize;
3e605b20 4161 tree whole = cxx_eval_constant_expression (ctx, orig_whole,
92a596e8 4162 lval,
5a804683 4163 non_constant_p, overflow_p);
2d76680f
PC
4164 tree start, field, value;
4165 unsigned HOST_WIDE_INT i;
4166
4167 if (whole == orig_whole)
4168 return t;
4169 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
4170 CONSTRUCTOR. */
4171 if (!*non_constant_p
4172 && TREE_CODE (whole) != VECTOR_CST
4173 && TREE_CODE (whole) != CONSTRUCTOR)
4174 {
2b3ab879 4175 if (!ctx->quiet)
2d76680f
PC
4176 error ("%qE is not a constant expression", orig_whole);
4177 *non_constant_p = true;
4178 }
4179 if (*non_constant_p)
4180 return t;
4181
4182 if (TREE_CODE (whole) == VECTOR_CST)
4183 return fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
4184 TREE_OPERAND (t, 1), TREE_OPERAND (t, 2));
4185
4186 start = TREE_OPERAND (t, 2);
4187 istart = tree_to_shwi (start);
4188 isize = tree_to_shwi (TREE_OPERAND (t, 1));
4189 utype = TREE_TYPE (t);
4190 if (!TYPE_UNSIGNED (utype))
4191 utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1);
4192 retval = build_int_cst (utype, 0);
4193 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
4194 {
4195 tree bitpos = bit_position (field);
5b884e94 4196 STRIP_ANY_LOCATION_WRAPPER (value);
2d76680f
PC
4197 if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1))
4198 return value;
4199 if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE
4200 && TREE_CODE (value) == INTEGER_CST
4201 && tree_fits_shwi_p (bitpos)
4202 && tree_fits_shwi_p (DECL_SIZE (field)))
4203 {
4204 HOST_WIDE_INT bit = tree_to_shwi (bitpos);
4205 HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field));
4206 HOST_WIDE_INT shift;
4207 if (bit >= istart && bit + sz <= istart + isize)
4208 {
4209 fldval = fold_convert (utype, value);
4210 mask = build_int_cst_type (utype, -1);
4211 mask = fold_build2 (LSHIFT_EXPR, utype, mask,
4212 size_int (TYPE_PRECISION (utype) - sz));
4213 mask = fold_build2 (RSHIFT_EXPR, utype, mask,
4214 size_int (TYPE_PRECISION (utype) - sz));
4215 fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask);
4216 shift = bit - istart;
4217 if (BYTES_BIG_ENDIAN)
4218 shift = TYPE_PRECISION (utype) - shift - sz;
4219 fldval = fold_build2 (LSHIFT_EXPR, utype, fldval,
4220 size_int (shift));
4221 retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval);
4222 fld_seen = true;
4223 }
4224 }
4225 }
4226 if (fld_seen)
4227 return fold_convert (TREE_TYPE (t), retval);
4228 gcc_unreachable ();
4229 return error_mark_node;
4230}
4231
896048cf
JJ
4232/* Helper for cxx_eval_bit_cast.
4233 Check [bit.cast]/3 rules, bit_cast is constexpr only if the To and From
4234 types and types of all subobjects have is_union_v<T>, is_pointer_v<T>,
4235 is_member_pointer_v<T>, is_volatile_v<T> false and has no non-static
4236 data members of reference type. */
4237
4238static bool
4239check_bit_cast_type (const constexpr_ctx *ctx, location_t loc, tree type,
4240 tree orig_type)
4241{
4242 if (TREE_CODE (type) == UNION_TYPE)
4243 {
4244 if (!ctx->quiet)
4245 {
4246 if (type == orig_type)
4247 error_at (loc, "%qs is not a constant expression because %qT is "
4248 "a union type", "__builtin_bit_cast", type);
4249 else
4250 error_at (loc, "%qs is not a constant expression because %qT "
4251 "contains a union type", "__builtin_bit_cast",
4252 orig_type);
4253 }
4254 return true;
4255 }
4256 if (TREE_CODE (type) == POINTER_TYPE)
4257 {
4258 if (!ctx->quiet)
4259 {
4260 if (type == orig_type)
4261 error_at (loc, "%qs is not a constant expression because %qT is "
4262 "a pointer type", "__builtin_bit_cast", type);
4263 else
4264 error_at (loc, "%qs is not a constant expression because %qT "
4265 "contains a pointer type", "__builtin_bit_cast",
4266 orig_type);
4267 }
4268 return true;
4269 }
4270 if (TREE_CODE (type) == REFERENCE_TYPE)
4271 {
4272 if (!ctx->quiet)
4273 {
4274 if (type == orig_type)
4275 error_at (loc, "%qs is not a constant expression because %qT is "
4276 "a reference type", "__builtin_bit_cast", type);
4277 else
4278 error_at (loc, "%qs is not a constant expression because %qT "
4279 "contains a reference type", "__builtin_bit_cast",
4280 orig_type);
4281 }
4282 return true;
4283 }
4284 if (TYPE_PTRMEM_P (type))
4285 {
4286 if (!ctx->quiet)
4287 {
4288 if (type == orig_type)
4289 error_at (loc, "%qs is not a constant expression because %qT is "
4290 "a pointer to member type", "__builtin_bit_cast",
4291 type);
4292 else
4293 error_at (loc, "%qs is not a constant expression because %qT "
4294 "contains a pointer to member type",
4295 "__builtin_bit_cast", orig_type);
4296 }
4297 return true;
4298 }
4299 if (TYPE_VOLATILE (type))
4300 {
4301 if (!ctx->quiet)
4302 {
4303 if (type == orig_type)
4304 error_at (loc, "%qs is not a constant expression because %qT is "
4305 "volatile", "__builtin_bit_cast", type);
4306 else
4307 error_at (loc, "%qs is not a constant expression because %qT "
4308 "contains a volatile subobject",
4309 "__builtin_bit_cast", orig_type);
4310 }
4311 return true;
4312 }
4313 if (TREE_CODE (type) == RECORD_TYPE)
4314 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4315 if (TREE_CODE (field) == FIELD_DECL
4316 && check_bit_cast_type (ctx, loc, TREE_TYPE (field), orig_type))
4317 return true;
4318 return false;
4319}
4320
c57c910c
JJ
4321/* Helper function for cxx_eval_bit_cast. For unsigned char or
4322 std::byte members of CONSTRUCTOR (recursively) if they contain
4323 some indeterminate bits (as set in MASK), remove the ctor elts,
4324 mark the CONSTRUCTOR as CONSTRUCTOR_NO_CLEARING and clear the
4325 bits in MASK. */
4326
4327static void
4328clear_uchar_or_std_byte_in_mask (location_t loc, tree t, unsigned char *mask)
4329{
4330 if (TREE_CODE (t) != CONSTRUCTOR)
4331 return;
4332
4333 unsigned i, j = 0;
4334 tree index, value;
4335 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), i, index, value)
4336 {
4337 tree type = TREE_TYPE (value);
4338 if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
4339 && DECL_BIT_FIELD_TYPE (index) != NULL_TREE)
4340 {
4341 if (is_byte_access_type_not_plain_char (DECL_BIT_FIELD_TYPE (index)))
4342 {
4343 HOST_WIDE_INT fldsz = TYPE_PRECISION (TREE_TYPE (index));
4344 gcc_assert (fldsz != 0);
4345 HOST_WIDE_INT pos = int_byte_position (index);
4346 HOST_WIDE_INT bpos
4347 = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (index));
4348 bpos %= BITS_PER_UNIT;
4349 HOST_WIDE_INT end
4350 = ROUND_UP (bpos + fldsz, BITS_PER_UNIT) / BITS_PER_UNIT;
4351 gcc_assert (end == 1 || end == 2);
4352 unsigned char *p = mask + pos;
4353 unsigned char mask_save[2];
4354 mask_save[0] = mask[pos];
4355 mask_save[1] = end == 2 ? mask[pos + 1] : 0;
4356 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
4357 sorry_at (loc, "PDP11 bit-field handling unsupported"
4358 " in %qs", "__builtin_bit_cast");
4359 else if (BYTES_BIG_ENDIAN)
4360 {
4361 /* Big endian. */
4362 if (bpos + fldsz <= BITS_PER_UNIT)
4363 *p &= ~(((1 << fldsz) - 1)
4364 << (BITS_PER_UNIT - bpos - fldsz));
4365 else
4366 {
4367 gcc_assert (bpos);
4368 *p &= ~(((1U << BITS_PER_UNIT) - 1) >> bpos);
4369 p++;
4370 fldsz -= BITS_PER_UNIT - bpos;
4371 gcc_assert (fldsz && fldsz < BITS_PER_UNIT);
4372 *p &= ((1U << BITS_PER_UNIT) - 1) >> fldsz;
4373 }
4374 }
4375 else
4376 {
4377 /* Little endian. */
4378 if (bpos + fldsz <= BITS_PER_UNIT)
4379 *p &= ~(((1 << fldsz) - 1) << bpos);
4380 else
4381 {
4382 gcc_assert (bpos);
4383 *p &= ~(((1 << BITS_PER_UNIT) - 1) << bpos);
4384 p++;
4385 fldsz -= BITS_PER_UNIT - bpos;
4386 gcc_assert (fldsz && fldsz < BITS_PER_UNIT);
4387 *p &= ~((1 << fldsz) - 1);
4388 }
4389 }
4390 if (mask_save[0] != mask[pos]
4391 || (end == 2 && mask_save[1] != mask[pos + 1]))
4392 {
4393 CONSTRUCTOR_NO_CLEARING (t) = 1;
4394 continue;
4395 }
4396 }
4397 }
4398 else if (is_byte_access_type_not_plain_char (type))
4399 {
4400 HOST_WIDE_INT pos;
4401 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
4402 pos = tree_to_shwi (index);
4403 else
4404 pos = int_byte_position (index);
4405 if (mask[pos])
4406 {
4407 CONSTRUCTOR_NO_CLEARING (t) = 1;
4408 mask[pos] = 0;
4409 continue;
4410 }
4411 }
4412 if (TREE_CODE (value) == CONSTRUCTOR)
4413 {
4414 HOST_WIDE_INT pos;
4415 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
4416 pos = tree_to_shwi (index)
4417 * tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))));
4418 else
4419 pos = int_byte_position (index);
4420 clear_uchar_or_std_byte_in_mask (loc, value, mask + pos);
4421 }
4422 if (i != j)
4423 {
4424 CONSTRUCTOR_ELT (t, j)->index = index;
4425 CONSTRUCTOR_ELT (t, j)->value = value;
4426 }
4427 ++j;
4428 }
4429 if (CONSTRUCTOR_NELTS (t) != j)
4430 vec_safe_truncate (CONSTRUCTOR_ELTS (t), j);
4431}
4432
896048cf
JJ
4433/* Subroutine of cxx_eval_constant_expression.
4434 Attempt to evaluate a BIT_CAST_EXPR. */
4435
4436static tree
4437cxx_eval_bit_cast (const constexpr_ctx *ctx, tree t, bool *non_constant_p,
4438 bool *overflow_p)
4439{
4440 if (check_bit_cast_type (ctx, EXPR_LOCATION (t), TREE_TYPE (t),
4441 TREE_TYPE (t))
4442 || check_bit_cast_type (ctx, cp_expr_loc_or_loc (TREE_OPERAND (t, 0),
4443 EXPR_LOCATION (t)),
4444 TREE_TYPE (TREE_OPERAND (t, 0)),
4445 TREE_TYPE (TREE_OPERAND (t, 0))))
4446 {
4447 *non_constant_p = true;
4448 return t;
4449 }
4450
4451 tree op = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), false,
4452 non_constant_p, overflow_p);
4453 if (*non_constant_p)
4454 return t;
4455
4456 location_t loc = EXPR_LOCATION (t);
4457 if (BITS_PER_UNIT != 8 || CHAR_BIT != 8)
4458 {
4459 if (!ctx->quiet)
4460 sorry_at (loc, "%qs cannot be constant evaluated on the target",
4461 "__builtin_bit_cast");
4462 *non_constant_p = true;
4463 return t;
4464 }
4465
4466 if (!tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (t))))
4467 {
4468 if (!ctx->quiet)
4469 sorry_at (loc, "%qs cannot be constant evaluated because the "
4470 "type is too large", "__builtin_bit_cast");
4471 *non_constant_p = true;
4472 return t;
4473 }
4474
4475 HOST_WIDE_INT len = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (t)));
4476 if (len < 0 || (int) len != len)
4477 {
4478 if (!ctx->quiet)
4479 sorry_at (loc, "%qs cannot be constant evaluated because the "
4480 "type is too large", "__builtin_bit_cast");
4481 *non_constant_p = true;
4482 return t;
4483 }
4484
4485 unsigned char buf[64];
4486 unsigned char *ptr, *mask;
4487 size_t alen = (size_t) len * 2;
4488 if (alen <= sizeof (buf))
4489 ptr = buf;
4490 else
4491 ptr = XNEWVEC (unsigned char, alen);
4492 mask = ptr + (size_t) len;
4493 /* At the beginning consider everything indeterminate. */
4494 memset (mask, ~0, (size_t) len);
4495
4496 if (native_encode_initializer (op, ptr, len, 0, mask) != len)
4497 {
4498 if (!ctx->quiet)
4499 sorry_at (loc, "%qs cannot be constant evaluated because the "
4500 "argument cannot be encoded", "__builtin_bit_cast");
4501 *non_constant_p = true;
4502 if (ptr != buf)
4503 XDELETE (ptr);
4504 return t;
4505 }
4506
4507 tree r = NULL_TREE;
4508 if (can_native_interpret_type_p (TREE_TYPE (t)))
c57c910c
JJ
4509 {
4510 r = native_interpret_expr (TREE_TYPE (t), ptr, len);
4511 if (is_byte_access_type_not_plain_char (TREE_TYPE (t)))
4512 {
4513 gcc_assert (len == 1);
4514 if (mask[0])
4515 {
4516 memset (mask, 0, len);
4517 r = build_constructor (TREE_TYPE (r), NULL);
4518 CONSTRUCTOR_NO_CLEARING (r) = 1;
4519 }
4520 }
4521 }
896048cf
JJ
4522 else if (TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE)
4523 {
4524 r = native_interpret_aggregate (TREE_TYPE (t), ptr, 0, len);
4525 if (r != NULL_TREE)
c57c910c
JJ
4526 {
4527 clear_type_padding_in_mask (TREE_TYPE (t), mask);
4528 clear_uchar_or_std_byte_in_mask (loc, r, mask);
4529 }
896048cf
JJ
4530 }
4531
4532 if (r != NULL_TREE)
4533 {
4534 for (int i = 0; i < len; i++)
4535 if (mask[i])
4536 {
4537 if (!ctx->quiet)
4538 error_at (loc, "%qs accessing uninitialized byte at offset %d",
4539 "__builtin_bit_cast", i);
4540 *non_constant_p = true;
4541 r = t;
4542 break;
4543 }
4544 if (ptr != buf)
4545 XDELETE (ptr);
4546 return r;
4547 }
4548
4549 if (!ctx->quiet)
4550 sorry_at (loc, "%qs cannot be constant evaluated because the "
4551 "argument cannot be interpreted", "__builtin_bit_cast");
4552 *non_constant_p = true;
4553 if (ptr != buf)
4554 XDELETE (ptr);
4555 return t;
4556}
4557
2d76680f
PC
4558/* Subroutine of cxx_eval_constant_expression.
4559 Evaluate a short-circuited logical expression T in the context
4560 of a given constexpr CALL. BAILOUT_VALUE is the value for
4561 early return. CONTINUE_VALUE is used here purely for
4562 sanity check purposes. */
4563
4564static tree
3e605b20 4565cxx_eval_logical_expression (const constexpr_ctx *ctx, tree t,
2d76680f 4566 tree bailout_value, tree continue_value,
92a596e8 4567 bool lval,
2d76680f
PC
4568 bool *non_constant_p, bool *overflow_p)
4569{
4570 tree r;
3e605b20 4571 tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
92a596e8 4572 lval,
5a804683 4573 non_constant_p, overflow_p);
2d76680f
PC
4574 VERIFY_CONSTANT (lhs);
4575 if (tree_int_cst_equal (lhs, bailout_value))
4576 return lhs;
4577 gcc_assert (tree_int_cst_equal (lhs, continue_value));
3e605b20 4578 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
92a596e8 4579 lval, non_constant_p,
5a804683 4580 overflow_p);
2d76680f
PC
4581 VERIFY_CONSTANT (r);
4582 return r;
4583}
4584
4585/* REF is a COMPONENT_REF designating a particular field. V is a vector of
4586 CONSTRUCTOR elements to initialize (part of) an object containing that
4587 field. Return a pointer to the constructor_elt corresponding to the
4588 initialization of the field. */
4589
4590static constructor_elt *
4591base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref)
4592{
4593 tree aggr = TREE_OPERAND (ref, 0);
4594 tree field = TREE_OPERAND (ref, 1);
4595 HOST_WIDE_INT i;
4596 constructor_elt *ce;
4597
4598 gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
4599
4600 if (TREE_CODE (aggr) == COMPONENT_REF)
4601 {
4602 constructor_elt *base_ce
4603 = base_field_constructor_elt (v, aggr);
4604 v = CONSTRUCTOR_ELTS (base_ce->value);
4605 }
4606
4607 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
4608 if (ce->index == field)
4609 return ce;
4610
4611 gcc_unreachable ();
4612 return NULL;
4613}
4614
3e605b20
JM
4615/* Some of the expressions fed to the constexpr mechanism are calls to
4616 constructors, which have type void. In that case, return the type being
4617 initialized by the constructor. */
4618
4619static tree
4620initialized_type (tree t)
4621{
4622 if (TYPE_P (t))
4623 return t;
5dab8b11 4624 tree type = TREE_TYPE (t);
4d0c18c6 4625 if (TREE_CODE (t) == CALL_EXPR)
3e605b20
JM
4626 {
4627 /* A constructor call has void type, so we need to look deeper. */
4628 tree fn = get_function_named_in_call (t);
4629 if (fn && TREE_CODE (fn) == FUNCTION_DECL
4630 && DECL_CXX_CONSTRUCTOR_P (fn))
4631 type = DECL_CONTEXT (fn);
4632 }
4d0c18c6
JM
4633 else if (TREE_CODE (t) == COMPOUND_EXPR)
4634 return initialized_type (TREE_OPERAND (t, 1));
5dab8b11
JM
4635 else if (TREE_CODE (t) == AGGR_INIT_EXPR)
4636 type = TREE_TYPE (AGGR_INIT_EXPR_SLOT (t));
4637 return cv_unqualified (type);
3e605b20
JM
4638}
4639
4640/* We're about to initialize element INDEX of an array or class from VALUE.
4641 Set up NEW_CTX appropriately by adjusting .object to refer to the
4642 subobject and creating a new CONSTRUCTOR if the element is itself
4643 a class or array. */
4644
4645static void
4646init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx,
4647 tree index, tree &value)
4648{
4649 new_ctx = *ctx;
4650
4651 if (index && TREE_CODE (index) != INTEGER_CST
3d423c6f
PP
4652 && TREE_CODE (index) != FIELD_DECL
4653 && TREE_CODE (index) != RANGE_EXPR)
3e605b20
JM
4654 /* This won't have an element in the new CONSTRUCTOR. */
4655 return;
4656
4657 tree type = initialized_type (value);
4658 if (!AGGREGATE_TYPE_P (type) && !VECTOR_TYPE_P (type))
4659 /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
4660 return;
abea1c9a
JJ
4661 if (VECTOR_TYPE_P (type)
4662 && VECTOR_TYPE_P (TREE_TYPE (ctx->ctor))
4663 && index == NULL_TREE)
4664 /* A vector inside of a vector CONSTRUCTOR, e.g. when a larger
4665 vector is constructed from smaller vectors, doesn't get its own
4666 CONSTRUCTOR either. */
4667 return;
3e605b20
JM
4668
4669 /* The sub-aggregate initializer might contain a placeholder;
4670 update object to refer to the subobject and ctor to refer to
4671 the (newly created) sub-initializer. */
4672 if (ctx->object)
3d423c6f
PP
4673 {
4674 if (index == NULL_TREE || TREE_CODE (index) == RANGE_EXPR)
4675 /* There's no well-defined subobject for this index. */
4676 new_ctx.object = NULL_TREE;
4677 else
4678 new_ctx.object = build_ctor_subob_ref (index, type, ctx->object);
4679 }
3e605b20 4680 tree elt = build_constructor (type, NULL);
e8c48716 4681 CONSTRUCTOR_NO_CLEARING (elt) = true;
3e605b20
JM
4682 new_ctx.ctor = elt;
4683
4684 if (TREE_CODE (value) == TARGET_EXPR)
4685 /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
4686 value = TARGET_EXPR_INITIAL (value);
4687}
4688
4689/* We're about to process an initializer for a class or array TYPE. Make
4690 sure that CTX is set up appropriately. */
4691
4692static void
4693verify_ctor_sanity (const constexpr_ctx *ctx, tree type)
4694{
4695 /* We don't bother building a ctor for an empty base subobject. */
4696 if (is_empty_class (type))
4697 return;
4698
4699 /* We're in the middle of an initializer that might involve placeholders;
4700 our caller should have created a CONSTRUCTOR for us to put the
4701 initializer into. We will either return that constructor or T. */
4702 gcc_assert (ctx->ctor);
4703 gcc_assert (same_type_ignoring_top_level_qualifiers_p
4704 (type, TREE_TYPE (ctx->ctor)));
fe69277d
JM
4705 /* We used to check that ctx->ctor was empty, but that isn't the case when
4706 the object is zero-initialized before calling the constructor. */
3e605b20 4707 if (ctx->object)
176e79b5
JM
4708 {
4709 tree otype = TREE_TYPE (ctx->object);
4710 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, otype)
4711 /* Handle flexible array members. */
4712 || (TREE_CODE (otype) == ARRAY_TYPE
4713 && TYPE_DOMAIN (otype) == NULL_TREE
4714 && TREE_CODE (type) == ARRAY_TYPE
4715 && (same_type_ignoring_top_level_qualifiers_p
4716 (TREE_TYPE (type), TREE_TYPE (otype)))));
4717 }
3e605b20 4718 gcc_assert (!ctx->object || !DECL_P (ctx->object)
8e007055 4719 || *(ctx->global->values.get (ctx->object)) == ctx->ctor);
3e605b20
JM
4720}
4721
2d76680f
PC
4722/* Subroutine of cxx_eval_constant_expression.
4723 The expression tree T denotes a C-style array or a C-style
4724 aggregate. Reduce it to a constant expression. */
4725
4726static tree
3e605b20 4727cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t,
92a596e8 4728 bool lval,
2d76680f
PC
4729 bool *non_constant_p, bool *overflow_p)
4730{
4731 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
2d76680f
PC
4732 bool changed = false;
4733 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t));
8a29084d 4734 tree type = TREE_TYPE (t);
3e605b20 4735
8a29084d 4736 constexpr_ctx new_ctx;
d4619dc1 4737 if (TYPE_PTRMEMFUNC_P (type) || VECTOR_TYPE_P (type))
8a29084d 4738 {
d4619dc1
NS
4739 /* We don't really need the ctx->ctor business for a PMF or
4740 vector, but it's simpler to use the same code. */
8a29084d
JM
4741 new_ctx = *ctx;
4742 new_ctx.ctor = build_constructor (type, NULL);
4743 new_ctx.object = NULL_TREE;
4744 ctx = &new_ctx;
4745 };
4746 verify_ctor_sanity (ctx, type);
3e605b20
JM
4747 vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
4748 vec_alloc (*p, vec_safe_length (v));
4749
49a86fce
PP
4750 if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (t))
4751 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor) = 1;
4752
2d63bc39
JM
4753 unsigned i;
4754 tree index, value;
4755 bool constant_p = true;
4756 bool side_effects_p = false;
3e605b20 4757 FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value)
2d76680f 4758 {
bcb5f3c9 4759 tree orig_value = value;
2168bfb8
JM
4760 /* Like in cxx_eval_store_expression, omit entries for empty fields. */
4761 bool no_slot = TREE_CODE (type) == RECORD_TYPE && is_empty_field (index);
4762 if (no_slot)
4763 new_ctx = *ctx;
4764 else
4765 init_subob_ctx (ctx, new_ctx, index, value);
37244b21 4766 int pos_hint = -1;
3e605b20 4767 if (new_ctx.ctor != ctx->ctor)
37244b21
PP
4768 {
4769 /* If we built a new CONSTRUCTOR, attach it now so that other
4770 initializers can refer to it. */
077dd9b3
PP
4771 constructor_elt *cep = get_or_insert_ctor_field (ctx->ctor, index);
4772 cep->value = new_ctx.ctor;
4773 pos_hint = cep - (*p)->begin();
37244b21 4774 }
b599bf9d 4775 else if (TREE_CODE (type) == UNION_TYPE)
37244b21
PP
4776 /* Otherwise if we're constructing a non-aggregate union member, set
4777 the active union member now so that we can later detect and diagnose
4778 if its initializer attempts to activate another member. */
077dd9b3 4779 get_or_insert_ctor_field (ctx->ctor, index);
3e605b20 4780 tree elt = cxx_eval_constant_expression (&new_ctx, value,
92a596e8 4781 lval,
5a804683 4782 non_constant_p, overflow_p);
2d76680f 4783 /* Don't VERIFY_CONSTANT here. */
2b3ab879 4784 if (ctx->quiet && *non_constant_p)
3e605b20 4785 break;
bcb5f3c9 4786 if (elt != orig_value)
2d76680f 4787 changed = true;
2d63bc39
JM
4788
4789 if (!TREE_CONSTANT (elt))
4790 constant_p = false;
4791 if (TREE_SIDE_EFFECTS (elt))
4792 side_effects_p = true;
3e605b20 4793 if (index && TREE_CODE (index) == COMPONENT_REF)
2d76680f
PC
4794 {
4795 /* This is an initialization of a vfield inside a base
4796 subaggregate that we already initialized; push this
4797 initialization into the previous initialization. */
3e605b20 4798 constructor_elt *inner = base_field_constructor_elt (*p, index);
2d76680f 4799 inner->value = elt;
3e605b20 4800 changed = true;
2d76680f 4801 }
3e605b20
JM
4802 else if (index
4803 && (TREE_CODE (index) == NOP_EXPR
4804 || TREE_CODE (index) == POINTER_PLUS_EXPR))
2d76680f
PC
4805 {
4806 /* This is an initializer for an empty base; now that we've
4807 checked that it's constant, we can ignore it. */
3e605b20
JM
4808 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index))));
4809 changed = true;
4810 }
2168bfb8
JM
4811 else if (no_slot)
4812 changed = true;
2d76680f 4813 else
ac9ec198 4814 {
b599bf9d
PP
4815 if (TREE_CODE (type) == UNION_TYPE
4816 && (*p)->last().index != index)
37244b21
PP
4817 /* The initializer erroneously changed the active union member that
4818 we're initializing. */
b599bf9d 4819 gcc_assert (*non_constant_p);
37244b21 4820 else
1bdbef09 4821 {
37244b21
PP
4822 /* The initializer might have mutated the underlying CONSTRUCTOR,
4823 so recompute the location of the target constructer_elt. */
4824 constructor_elt *cep
4825 = get_or_insert_ctor_field (ctx->ctor, index, pos_hint);
4826 cep->value = elt;
1bdbef09 4827 }
37244b21 4828
1bdbef09 4829 /* Adding or replacing an element might change the ctor's flags. */
ac9ec198
MP
4830 TREE_CONSTANT (ctx->ctor) = constant_p;
4831 TREE_SIDE_EFFECTS (ctx->ctor) = side_effects_p;
4832 }
2d76680f
PC
4833 }
4834 if (*non_constant_p || !changed)
3e605b20
JM
4835 return t;
4836 t = ctx->ctor;
4837 /* We're done building this CONSTRUCTOR, so now we can interpret an
4838 element without an explicit initializer as value-initialized. */
e8c48716 4839 CONSTRUCTOR_NO_CLEARING (t) = false;
2d63bc39
JM
4840 TREE_CONSTANT (t) = constant_p;
4841 TREE_SIDE_EFFECTS (t) = side_effects_p;
8a29084d 4842 if (VECTOR_TYPE_P (type))
2d76680f
PC
4843 t = fold (t);
4844 return t;
4845}
4846
4847/* Subroutine of cxx_eval_constant_expression.
4848 The expression tree T is a VEC_INIT_EXPR which denotes the desired
4849 initialization of a non-static data member of array type. Reduce it to a
4850 CONSTRUCTOR.
4851
4852 Note that apart from value-initialization (when VALUE_INIT is true),
4853 this is only intended to support value-initialization and the
4854 initializations done by defaulted constructors for classes with
4855 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
4856 will either be NULL_TREE for the default constructor, or a COMPONENT_REF
4857 for the copy/move constructor. */
4858
4859static tree
3e605b20 4860cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init,
92a596e8 4861 bool value_init, bool lval,
2d76680f
PC
4862 bool *non_constant_p, bool *overflow_p)
4863{
4864 tree elttype = TREE_TYPE (atype);
3e605b20
JM
4865 verify_ctor_sanity (ctx, atype);
4866 vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
2d76680f 4867 bool pre_init = false;
4784470a 4868 unsigned HOST_WIDE_INT i;
dc5ca6c8 4869 tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
2d76680f 4870
a1c9c9ff
JM
4871 if (init && TREE_CODE (init) == CONSTRUCTOR)
4872 return cxx_eval_bare_aggregate (ctx, init, lval,
4873 non_constant_p, overflow_p);
4874
2d76680f
PC
4875 /* For the default constructor, build up a call to the default
4876 constructor of the element type. We only need to handle class types
4877 here, as for a constructor to be constexpr, all members must be
4878 initialized, which for a defaulted default constructor means they must
4879 be of a class type with a constexpr default constructor. */
4880 if (TREE_CODE (elttype) == ARRAY_TYPE)
4881 /* We only do this at the lowest level. */;
4882 else if (value_init)
4883 {
dc5ca6c8 4884 init = build_value_init (elttype, complain);
2d76680f
PC
4885 pre_init = true;
4886 }
4887 else if (!init)
4888 {
cd9cf97b 4889 releasing_vec argvec;
2d76680f
PC
4890 init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
4891 &argvec, elttype, LOOKUP_NORMAL,
dc5ca6c8 4892 complain);
5dab8b11 4893 init = build_aggr_init_expr (elttype, init);
2d76680f
PC
4894 pre_init = true;
4895 }
4896
d21252de
PP
4897 bool zeroed_out = false;
4898 if (!CONSTRUCTOR_NO_CLEARING (ctx->ctor))
4899 {
4900 /* We're initializing an array object that had been zero-initialized
4901 earlier. Truncate ctx->ctor, and propagate its zeroed state by
4902 clearing CONSTRUCTOR_NO_CLEARING on each of the aggregate element
4903 initializers we append to it. */
4904 gcc_checking_assert (initializer_zerop (ctx->ctor));
4905 zeroed_out = true;
4906 vec_safe_truncate (*p, 0);
4907 }
4908
74f8705e
MP
4909 tree nelts = get_array_or_vector_nelts (ctx, atype, non_constant_p,
4910 overflow_p);
4911 unsigned HOST_WIDE_INT max = tree_to_uhwi (nelts);
4784470a 4912 for (i = 0; i < max; ++i)
2d76680f
PC
4913 {
4914 tree idx = build_int_cst (size_type_node, i);
4915 tree eltinit;
928af3bf 4916 bool reuse = false;
3e605b20
JM
4917 constexpr_ctx new_ctx;
4918 init_subob_ctx (ctx, new_ctx, idx, pre_init ? init : elttype);
4919 if (new_ctx.ctor != ctx->ctor)
d21252de
PP
4920 {
4921 if (zeroed_out)
4922 CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = false;
4923 CONSTRUCTOR_APPEND_ELT (*p, idx, new_ctx.ctor);
4924 }
2d76680f
PC
4925 if (TREE_CODE (elttype) == ARRAY_TYPE)
4926 {
4927 /* A multidimensional array; recurse. */
4928 if (value_init || init == NULL_TREE)
928af3bf
JJ
4929 {
4930 eltinit = NULL_TREE;
4931 reuse = i == 0;
4932 }
2d76680f 4933 else
dc5ca6c8 4934 eltinit = cp_build_array_ref (input_location, init, idx, complain);
3e605b20 4935 eltinit = cxx_eval_vec_init_1 (&new_ctx, elttype, eltinit, value_init,
92a596e8 4936 lval,
2d76680f
PC
4937 non_constant_p, overflow_p);
4938 }
4939 else if (pre_init)
4940 {
4941 /* Initializing an element using value or default initialization
4942 we just pre-built above. */
3ee378fb
JM
4943 if (init == void_node)
4944 /* Trivial default-init, don't do anything to the CONSTRUCTOR. */
4945 return ctx->ctor;
928af3bf
JJ
4946 eltinit = cxx_eval_constant_expression (&new_ctx, init, lval,
4947 non_constant_p, overflow_p);
4948 reuse = i == 0;
2d76680f
PC
4949 }
4950 else
4951 {
4952 /* Copying an element. */
4953 gcc_assert (same_type_ignoring_top_level_qualifiers_p
4954 (atype, TREE_TYPE (init)));
dc5ca6c8 4955 eltinit = cp_build_array_ref (input_location, init, idx, complain);
72b3e203 4956 if (!lvalue_p (init))
2d76680f 4957 eltinit = move (eltinit);
dc5ca6c8 4958 eltinit = force_rvalue (eltinit, complain);
c2236b9b
JJ
4959 eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval,
4960 non_constant_p, overflow_p);
2d76680f 4961 }
6b7d53a2 4962 if (*non_constant_p)
3e605b20
JM
4963 break;
4964 if (new_ctx.ctor != ctx->ctor)
4965 {
4966 /* We appended this element above; update the value. */
4967 gcc_assert ((*p)->last().index == idx);
4968 (*p)->last().value = eltinit;
4969 }
4970 else
4971 CONSTRUCTOR_APPEND_ELT (*p, idx, eltinit);
928af3bf 4972 /* Reuse the result of cxx_eval_constant_expression call
c2236b9b
JJ
4973 from the first iteration to all others if it is a constant
4974 initializer that doesn't require relocations. */
928af3bf
JJ
4975 if (reuse
4976 && max > 1
c2236b9b
JJ
4977 && (eltinit == NULL_TREE
4978 || (initializer_constant_valid_p (eltinit, TREE_TYPE (eltinit))
4979 == null_pointer_node)))
928af3bf
JJ
4980 {
4981 if (new_ctx.ctor != ctx->ctor)
4982 eltinit = new_ctx.ctor;
1e027956
RB
4983 tree range = build2 (RANGE_EXPR, size_type_node,
4984 build_int_cst (size_type_node, 1),
4985 build_int_cst (size_type_node, max - 1));
4986 CONSTRUCTOR_APPEND_ELT (*p, range, unshare_constructor (eltinit));
928af3bf
JJ
4987 break;
4988 }
1e027956
RB
4989 else if (i == 0)
4990 vec_safe_reserve (*p, max);
2d76680f
PC
4991 }
4992
4993 if (!*non_constant_p)
4994 {
3e605b20 4995 init = ctx->ctor;
e8c48716 4996 CONSTRUCTOR_NO_CLEARING (init) = false;
2d76680f 4997 }
2d76680f
PC
4998 return init;
4999}
5000
5001static tree
3e605b20 5002cxx_eval_vec_init (const constexpr_ctx *ctx, tree t,
92a596e8 5003 bool lval,
2d76680f
PC
5004 bool *non_constant_p, bool *overflow_p)
5005{
5006 tree atype = TREE_TYPE (t);
5007 tree init = VEC_INIT_EXPR_INIT (t);
e948436e
JM
5008 bool value_init = VEC_INIT_EXPR_VALUE_INIT (t);
5009 if (!init || !BRACE_ENCLOSED_INITIALIZER_P (init))
5010 ;
5011 else if (CONSTRUCTOR_NELTS (init) == 0)
5012 {
5013 /* Handle {} as value-init. */
5014 init = NULL_TREE;
5015 value_init = true;
5016 }
5017 else
5018 {
5019 /* This is a more complicated case, like needing to loop over trailing
5020 elements; call build_vec_init and evaluate the result. */
5021 tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
5022 constexpr_ctx new_ctx = *ctx;
5023 if (!ctx->object)
5024 {
5025 /* We want to have an initialization target for an VEC_INIT_EXPR.
5026 If we don't already have one in CTX, use the VEC_INIT_EXPR_SLOT. */
5027 new_ctx.object = VEC_INIT_EXPR_SLOT (t);
5028 tree ctor = new_ctx.ctor = build_constructor (atype, NULL);
5029 CONSTRUCTOR_NO_CLEARING (ctor) = true;
5030 ctx->global->values.put (new_ctx.object, ctor);
5031 ctx = &new_ctx;
5032 }
5033 init = expand_vec_init_expr (ctx->object, t, complain);
5034 return cxx_eval_constant_expression (ctx, init, lval, non_constant_p,
5035 overflow_p);
5036 }
5037 tree r = cxx_eval_vec_init_1 (ctx, atype, init, value_init,
92a596e8 5038 lval, non_constant_p, overflow_p);
2d76680f
PC
5039 if (*non_constant_p)
5040 return t;
5041 else
5042 return r;
5043}
5044
e4511ca2
JM
5045/* Like same_type_ignoring_top_level_qualifiers_p, but also handle the case
5046 where the desired type is an array of unknown bounds because the variable
5047 has had its bounds deduced since the wrapping expression was created. */
5048
5049static bool
5050same_type_ignoring_tlq_and_bounds_p (tree type1, tree type2)
5051{
5052 while (TREE_CODE (type1) == ARRAY_TYPE
5053 && TREE_CODE (type2) == ARRAY_TYPE
5054 && (!TYPE_DOMAIN (type1) || !TYPE_DOMAIN (type2)))
5055 {
5056 type1 = TREE_TYPE (type1);
5057 type2 = TREE_TYPE (type2);
5058 }
5059 return same_type_ignoring_top_level_qualifiers_p (type1, type2);
5060}
5061
43e84ce7
JJ
5062/* Try to determine the currently active union member for an expression
5063 with UNION_TYPE. If it can be determined, return the FIELD_DECL,
5064 otherwise return NULL_TREE. */
5065
5066static tree
5067cxx_union_active_member (const constexpr_ctx *ctx, tree t)
5068{
5069 constexpr_ctx new_ctx = *ctx;
5070 new_ctx.quiet = true;
5071 bool non_constant_p = false, overflow_p = false;
5072 tree ctor = cxx_eval_constant_expression (&new_ctx, t, false,
5073 &non_constant_p,
5074 &overflow_p);
5075 if (TREE_CODE (ctor) == CONSTRUCTOR
5076 && CONSTRUCTOR_NELTS (ctor) == 1
5077 && CONSTRUCTOR_ELT (ctor, 0)->index
5078 && TREE_CODE (CONSTRUCTOR_ELT (ctor, 0)->index) == FIELD_DECL)
5079 return CONSTRUCTOR_ELT (ctor, 0)->index;
5080 return NULL_TREE;
5081}
5082
0fe2ae29
JJ
5083/* Helper function for cxx_fold_indirect_ref_1, called recursively. */
5084
5085static tree
43e84ce7
JJ
5086cxx_fold_indirect_ref_1 (const constexpr_ctx *ctx, location_t loc, tree type,
5087 tree op, unsigned HOST_WIDE_INT off, bool *empty_base)
0fe2ae29
JJ
5088{
5089 tree optype = TREE_TYPE (op);
5090 unsigned HOST_WIDE_INT const_nunits;
bc99c54d
JM
5091 if (off == 0 && similar_type_p (optype, type))
5092 return op;
5093 else if (TREE_CODE (optype) == COMPLEX_TYPE
5094 && similar_type_p (type, TREE_TYPE (optype)))
0fe2ae29 5095 {
0fe2ae29 5096 /* *(foo *)&complexfoo => __real__ complexfoo */
bc99c54d 5097 if (off == 0)
0fe2ae29 5098 return build1_loc (loc, REALPART_EXPR, type, op);
bc99c54d
JM
5099 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
5100 else if (tree_to_uhwi (TYPE_SIZE_UNIT (type)) == off)
5101 return build1_loc (loc, IMAGPART_EXPR, type, op);
0fe2ae29
JJ
5102 }
5103 /* ((foo*)&vectorfoo)[x] => BIT_FIELD_REF<vectorfoo,...> */
5104 else if (VECTOR_TYPE_P (optype)
5105 && similar_type_p (type, TREE_TYPE (optype))
5106 && TYPE_VECTOR_SUBPARTS (optype).is_constant (&const_nunits))
5107 {
5108 unsigned HOST_WIDE_INT part_width = tree_to_uhwi (TYPE_SIZE_UNIT (type));
5109 unsigned HOST_WIDE_INT max_offset = part_width * const_nunits;
5110 if (off < max_offset && off % part_width == 0)
5111 {
5112 tree index = bitsize_int (off * BITS_PER_UNIT);
5113 return build3_loc (loc, BIT_FIELD_REF, type, op,
5114 TYPE_SIZE (type), index);
5115 }
5116 }
5117 /* ((foo *)&fooarray)[x] => fooarray[x] */
5118 else if (TREE_CODE (optype) == ARRAY_TYPE
5119 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (optype)))
5120 && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (optype))))
5121 {
5122 tree type_domain = TYPE_DOMAIN (optype);
5123 tree min_val = size_zero_node;
5124 if (type_domain && TYPE_MIN_VALUE (type_domain))
5125 min_val = TYPE_MIN_VALUE (type_domain);
5126 unsigned HOST_WIDE_INT el_sz
5127 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (optype)));
5128 unsigned HOST_WIDE_INT idx = off / el_sz;
5129 unsigned HOST_WIDE_INT rem = off % el_sz;
5130 if (tree_fits_uhwi_p (min_val))
5131 {
5132 tree index = size_int (idx + tree_to_uhwi (min_val));
5133 op = build4_loc (loc, ARRAY_REF, TREE_TYPE (optype), op, index,
5134 NULL_TREE, NULL_TREE);
43e84ce7 5135 return cxx_fold_indirect_ref_1 (ctx, loc, type, op, rem,
0fe2ae29
JJ
5136 empty_base);
5137 }
5138 }
5139 /* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */
43e84ce7
JJ
5140 else if (TREE_CODE (optype) == RECORD_TYPE
5141 || TREE_CODE (optype) == UNION_TYPE)
0fe2ae29 5142 {
43e84ce7
JJ
5143 if (TREE_CODE (optype) == UNION_TYPE)
5144 /* For unions prefer the currently active member. */
5145 if (tree field = cxx_union_active_member (ctx, op))
5146 {
5147 unsigned HOST_WIDE_INT el_sz
5148 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field)));
5149 if (off < el_sz)
5150 {
5151 tree cop = build3 (COMPONENT_REF, TREE_TYPE (field),
5152 op, field, NULL_TREE);
5153 if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop,
5154 off, empty_base))
5155 return ret;
5156 }
5157 }
0fe2ae29
JJ
5158 for (tree field = TYPE_FIELDS (optype);
5159 field; field = DECL_CHAIN (field))
5160 if (TREE_CODE (field) == FIELD_DECL
5161 && TREE_TYPE (field) != error_mark_node
5162 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (field))))
5163 {
5164 tree pos = byte_position (field);
5165 if (!tree_fits_uhwi_p (pos))
5166 continue;
5167 unsigned HOST_WIDE_INT upos = tree_to_uhwi (pos);
43e84ce7 5168 unsigned HOST_WIDE_INT el_sz
0fe2ae29
JJ
5169 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field)));
5170 if (upos <= off && off < upos + el_sz)
5171 {
5172 tree cop = build3 (COMPONENT_REF, TREE_TYPE (field),
5173 op, field, NULL_TREE);
43e84ce7 5174 if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop,
0fe2ae29
JJ
5175 off - upos,
5176 empty_base))
5177 return ret;
5178 }
5179 }
bc99c54d
JM
5180 /* Also handle conversion to an empty base class, which
5181 is represented with a NOP_EXPR. */
5182 if (is_empty_class (type)
5183 && CLASS_TYPE_P (optype)
5184 && DERIVED_FROM_P (type, optype))
5185 {
5186 *empty_base = true;
5187 return op;
5188 }
0fe2ae29
JJ
5189 }
5190
5191 return NULL_TREE;
5192}
5193
2d76680f
PC
5194/* A less strict version of fold_indirect_ref_1, which requires cv-quals to
5195 match. We want to be less strict for simple *& folding; if we have a
5196 non-const temporary that we access through a const pointer, that should
5197 work. We handle this here rather than change fold_indirect_ref_1
5198 because we're dealing with things like ADDR_EXPR of INTEGER_CST which
5199 don't really make sense outside of constant expression evaluation. Also
5200 we want to allow folding to COMPONENT_REF, which could cause trouble
0fe2ae29 5201 with TBAA in fold_indirect_ref_1. */
2d76680f
PC
5202
5203static tree
43e84ce7
JJ
5204cxx_fold_indirect_ref (const constexpr_ctx *ctx, location_t loc, tree type,
5205 tree op0, bool *empty_base)
2d76680f 5206{
ebe4bf41
MP
5207 tree sub = op0;
5208 tree subtype;
5209 poly_uint64 const_op01;
2d76680f 5210
80de0002
JM
5211 /* STRIP_NOPS, but stop if REINTERPRET_CAST_P. */
5212 while (CONVERT_EXPR_P (sub) || TREE_CODE (sub) == NON_LVALUE_EXPR
5213 || TREE_CODE (sub) == VIEW_CONVERT_EXPR)
5214 {
5215 if (TREE_CODE (sub) == NOP_EXPR
5216 && REINTERPRET_CAST_P (sub))
5217 return NULL_TREE;
5218 sub = TREE_OPERAND (sub, 0);
5219 }
5220
2d76680f 5221 subtype = TREE_TYPE (sub);
71a93b08 5222 if (!INDIRECT_TYPE_P (subtype))
2d76680f
PC
5223 return NULL_TREE;
5224
ab36b554
PP
5225 /* Canonicalizes the given OBJ/OFF pair by iteratively absorbing
5226 the innermost component into the offset until it would make the
5227 offset positive, so that cxx_fold_indirect_ref_1 can identify
5228 more folding opportunities. */
5229 auto canonicalize_obj_off = [] (tree& obj, tree& off) {
5230 while (TREE_CODE (obj) == COMPONENT_REF
5231 && (tree_int_cst_sign_bit (off) || integer_zerop (off)))
5232 {
5233 tree field = TREE_OPERAND (obj, 1);
5234 tree pos = byte_position (field);
5235 if (integer_zerop (off) && integer_nonzerop (pos))
5236 /* If the offset is already 0, keep going as long as the
5237 component is at position 0. */
5238 break;
5239 off = int_const_binop (PLUS_EXPR, off, pos);
5240 obj = TREE_OPERAND (obj, 0);
5241 }
5242 };
5243
2d76680f
PC
5244 if (TREE_CODE (sub) == ADDR_EXPR)
5245 {
5246 tree op = TREE_OPERAND (sub, 0);
5247 tree optype = TREE_TYPE (op);
5248
5249 /* *&CONST_DECL -> to the value of the const decl. */
5250 if (TREE_CODE (op) == CONST_DECL)
5251 return DECL_INITIAL (op);
5252 /* *&p => p; make sure to handle *&"str"[cst] here. */
1a120ec1 5253 if (similar_type_p (optype, type))
2d76680f
PC
5254 {
5255 tree fop = fold_read_from_constant_string (op);
5256 if (fop)
5257 return fop;
5258 else
5259 return op;
5260 }
0fe2ae29 5261 else
ab36b554
PP
5262 {
5263 tree off = integer_zero_node;
5264 canonicalize_obj_off (op, off);
5265 gcc_assert (integer_zerop (off));
5266 return cxx_fold_indirect_ref_1 (ctx, loc, type, op, 0, empty_base);
5267 }
2d76680f
PC
5268 }
5269 else if (TREE_CODE (sub) == POINTER_PLUS_EXPR
0fe2ae29 5270 && tree_fits_uhwi_p (TREE_OPERAND (sub, 1)))
2d76680f
PC
5271 {
5272 tree op00 = TREE_OPERAND (sub, 0);
0120cd93 5273 tree off = TREE_OPERAND (sub, 1);
2d76680f
PC
5274
5275 STRIP_NOPS (op00);
5276 if (TREE_CODE (op00) == ADDR_EXPR)
0120cd93
PP
5277 {
5278 tree obj = TREE_OPERAND (op00, 0);
ab36b554 5279 canonicalize_obj_off (obj, off);
0120cd93
PP
5280 return cxx_fold_indirect_ref_1 (ctx, loc, type, obj,
5281 tree_to_uhwi (off), empty_base);
5282 }
2d76680f
PC
5283 }
5284 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
5285 else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
1a120ec1 5286 && similar_type_p (type, TREE_TYPE (TREE_TYPE (subtype))))
2d76680f
PC
5287 {
5288 tree type_domain;
5289 tree min_val = size_zero_node;
ebe4bf41 5290 tree newsub
43e84ce7 5291 = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (subtype), sub, NULL);
2d76680f
PC
5292 if (newsub)
5293 sub = newsub;
5294 else
5295 sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub);
5296 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
5297 if (type_domain && TYPE_MIN_VALUE (type_domain))
5298 min_val = TYPE_MIN_VALUE (type_domain);
5299 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
5300 NULL_TREE);
5301 }
5302
5303 return NULL_TREE;
5304}
5305
5306static tree
3e605b20 5307cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t,
92a596e8 5308 bool lval,
2d76680f
PC
5309 bool *non_constant_p, bool *overflow_p)
5310{
5311 tree orig_op0 = TREE_OPERAND (t, 0);
2d76680f 5312 bool empty_base = false;
2d76680f 5313
cda0a029
JM
5314 /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second
5315 operand is an integer-zero. Otherwise reject the MEM_REF for now. */
5316
5317 if (TREE_CODE (t) == MEM_REF
5318 && (!TREE_OPERAND (t, 1) || !integer_zerop (TREE_OPERAND (t, 1))))
5319 {
5320 gcc_assert (ctx->quiet);
5321 *non_constant_p = true;
5322 return t;
5323 }
5324
255a48d6 5325 /* First try to simplify it directly. */
43e84ce7
JJ
5326 tree r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t),
5327 orig_op0, &empty_base);
255a48d6 5328 if (!r)
2d76680f 5329 {
255a48d6
JM
5330 /* If that didn't work, evaluate the operand first. */
5331 tree op0 = cxx_eval_constant_expression (ctx, orig_op0,
5332 /*lval*/false, non_constant_p,
5333 overflow_p);
5334 /* Don't VERIFY_CONSTANT here. */
5335 if (*non_constant_p)
5336 return t;
5337
8bada5cd
MS
5338 if (!lval && integer_zerop (op0))
5339 {
5340 if (!ctx->quiet)
5341 error ("dereferencing a null pointer");
5342 *non_constant_p = true;
5343 return t;
5344 }
5345
43e84ce7 5346 r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t), op0,
255a48d6
JM
5347 &empty_base);
5348 if (r == NULL_TREE)
2d76680f
PC
5349 {
5350 /* We couldn't fold to a constant value. Make sure it's not
5351 something we should have been able to fold. */
255a48d6
JM
5352 tree sub = op0;
5353 STRIP_NOPS (sub);
5354 if (TREE_CODE (sub) == ADDR_EXPR)
5355 {
1a120ec1 5356 gcc_assert (!similar_type_p
255a48d6
JM
5357 (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t)));
5358 /* DR 1188 says we don't have to deal with this. */
5359 if (!ctx->quiet)
1a120ec1
JM
5360 error_at (cp_expr_loc_or_input_loc (t),
5361 "accessing value of %qE through a %qT glvalue in a "
5362 "constant expression", build_fold_indirect_ref (sub),
5363 TREE_TYPE (t));
255a48d6
JM
5364 *non_constant_p = true;
5365 return t;
5366 }
5367
5368 if (lval && op0 != orig_op0)
5369 return build1 (INDIRECT_REF, TREE_TYPE (t), op0);
5370 if (!lval)
5371 VERIFY_CONSTANT (t);
2d76680f
PC
5372 return t;
5373 }
5374 }
5375
255a48d6
JM
5376 r = cxx_eval_constant_expression (ctx, r,
5377 lval, non_constant_p, overflow_p);
5378 if (*non_constant_p)
5379 return t;
5380
125db6a1 5381 /* If we're pulling out the value of an empty base, just return an empty
2d76680f 5382 CONSTRUCTOR. */
92a596e8 5383 if (empty_base && !lval)
2d76680f 5384 {
2d76680f
PC
5385 r = build_constructor (TREE_TYPE (t), NULL);
5386 TREE_CONSTANT (r) = true;
5387 }
5388
2d76680f
PC
5389 return r;
5390}
5391
5392/* Complain about R, a VAR_DECL, not being usable in a constant expression.
5393 Shared between potential_constant_expression and
5394 cxx_eval_constant_expression. */
5395
5396static void
6821245b 5397non_const_var_error (location_t loc, tree r)
2d76680f 5398{
8e007055 5399 auto_diagnostic_group d;
2d76680f 5400 tree type = TREE_TYPE (r);
8e007055 5401 if (DECL_NAME (r) == heap_uninit_identifier
815baade
JJ
5402 || DECL_NAME (r) == heap_identifier
5403 || DECL_NAME (r) == heap_vec_uninit_identifier
5404 || DECL_NAME (r) == heap_vec_identifier)
8e007055 5405 {
6821245b
JM
5406 error_at (loc, "the content of uninitialized storage is not usable "
5407 "in a constant expression");
8e007055
JJ
5408 inform (DECL_SOURCE_LOCATION (r), "allocated here");
5409 return;
5410 }
5411 if (DECL_NAME (r) == heap_deleted_identifier)
5412 {
6821245b
JM
5413 error_at (loc, "use of allocated storage after deallocation in a "
5414 "constant expression");
8e007055
JJ
5415 inform (DECL_SOURCE_LOCATION (r), "allocated here");
5416 return;
5417 }
6821245b
JM
5418 error_at (loc, "the value of %qD is not usable in a constant "
5419 "expression", r);
2d76680f
PC
5420 /* Avoid error cascade. */
5421 if (DECL_INITIAL (r) == error_mark_node)
5422 return;
5423 if (DECL_DECLARED_CONSTEXPR_P (r))
5424 inform (DECL_SOURCE_LOCATION (r),
5425 "%qD used in its own initializer", r);
5426 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5427 {
5428 if (!CP_TYPE_CONST_P (type))
5429 inform (DECL_SOURCE_LOCATION (r),
5430 "%q#D is not const", r);
5431 else if (CP_TYPE_VOLATILE_P (type))
5432 inform (DECL_SOURCE_LOCATION (r),
5433 "%q#D is volatile", r);
5434 else if (!DECL_INITIAL (r)
a3e2b438
PP
5435 || !TREE_CONSTANT (DECL_INITIAL (r))
5436 || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r))
2d76680f
PC
5437 inform (DECL_SOURCE_LOCATION (r),
5438 "%qD was not initialized with a constant "
5439 "expression", r);
5440 else
5441 gcc_unreachable ();
5442 }
9f613f06 5443 else if (TYPE_REF_P (type))
fd338b13
JM
5444 inform (DECL_SOURCE_LOCATION (r),
5445 "%qD was not initialized with a constant "
5446 "expression", r);
2d76680f
PC
5447 else
5448 {
5449 if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r))
5450 inform (DECL_SOURCE_LOCATION (r),
5451 "%qD was not declared %<constexpr%>", r);
5452 else
5453 inform (DECL_SOURCE_LOCATION (r),
5454 "%qD does not have integral or enumeration type",
5455 r);
5456 }
5457}
5458
5459/* Subroutine of cxx_eval_constant_expression.
5460 Like cxx_eval_unary_expression, except for trinary expressions. */
5461
5462static tree
3e605b20 5463cxx_eval_trinary_expression (const constexpr_ctx *ctx, tree t,
92a596e8 5464 bool lval,
2d76680f
PC
5465 bool *non_constant_p, bool *overflow_p)
5466{
5467 int i;
5468 tree args[3];
5469 tree val;
5470
5471 for (i = 0; i < 3; i++)
5472 {
3e605b20 5473 args[i] = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, i),
92a596e8 5474 lval,
5a804683 5475 non_constant_p, overflow_p);
2d76680f
PC
5476 VERIFY_CONSTANT (args[i]);
5477 }
5478
5479 val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t),
5480 args[0], args[1], args[2]);
5481 if (val == NULL_TREE)
5482 return t;
5483 VERIFY_CONSTANT (val);
5484 return val;
5485}
5486
98e5a19a
JM
5487/* True if T was declared in a function declared to be constexpr, and
5488 therefore potentially constant in C++14. */
5489
2d76680f
PC
5490bool
5491var_in_constexpr_fn (tree t)
5492{
5493 tree ctx = DECL_CONTEXT (t);
98e5a19a 5494 return (ctx && TREE_CODE (ctx) == FUNCTION_DECL
2d76680f
PC
5495 && DECL_DECLARED_CONSTEXPR_P (ctx));
5496}
5497
26dbe85a
MP
5498/* True if a function might be constexpr: either a function that was
5499 declared constexpr, or a C++17 lambda op(). */
5500
5501bool
5502maybe_constexpr_fn (tree t)
5503{
5504 return (DECL_DECLARED_CONSTEXPR_P (t)
87c2080b
JM
5505 || (cxx_dialect >= cxx17 && LAMBDA_FUNCTION_P (t))
5506 || (flag_implicit_constexpr
5507 && DECL_DECLARED_INLINE_P (STRIP_TEMPLATE (t))));
26dbe85a
MP
5508}
5509
98e5a19a
JM
5510/* True if T was declared in a function that might be constexpr: either a
5511 function that was declared constexpr, or a C++17 lambda op(). */
5512
5513bool
5514var_in_maybe_constexpr_fn (tree t)
5515{
87c2080b
JM
5516 return (DECL_FUNCTION_SCOPE_P (t)
5517 && maybe_constexpr_fn (DECL_CONTEXT (t)));
98e5a19a
JM
5518}
5519
68d01920
JM
5520/* We're assigning INIT to TARGET. In do_build_copy_constructor and
5521 build_over_call we implement trivial copy of a class with tail padding using
5522 assignment of character arrays, which is valid in normal code, but not in
5523 constexpr evaluation. We don't need to worry about clobbering tail padding
5524 in constexpr evaluation, so strip the type punning. */
5525
5526static void
5527maybe_simplify_trivial_copy (tree &target, tree &init)
5528{
5529 if (TREE_CODE (target) == MEM_REF
5530 && TREE_CODE (init) == MEM_REF
5531 && TREE_TYPE (target) == TREE_TYPE (init)
5532 && TREE_CODE (TREE_TYPE (target)) == ARRAY_TYPE
5533 && TREE_TYPE (TREE_TYPE (target)) == unsigned_char_type_node)
5534 {
5535 target = build_fold_indirect_ref (TREE_OPERAND (target, 0));
5536 init = build_fold_indirect_ref (TREE_OPERAND (init, 0));
5537 }
5538}
5539
7eb5be6a
MP
5540/* Returns true if REF, which is a COMPONENT_REF, has any fields
5541 of constant type. This does not check for 'mutable', so the
5542 caller is expected to be mindful of that. */
5543
5544static bool
5545cref_has_const_field (tree ref)
5546{
5547 while (TREE_CODE (ref) == COMPONENT_REF)
5548 {
5549 if (CP_TYPE_CONST_P (TREE_TYPE (TREE_OPERAND (ref, 1))))
5550 return true;
5551 ref = TREE_OPERAND (ref, 0);
5552 }
5553 return false;
5554}
5555
04e1749c
MP
5556/* Return true if we are modifying something that is const during constant
5557 expression evaluation. CODE is the code of the statement, OBJ is the
5558 object in question, MUTABLE_P is true if one of the subobjects were
5559 declared mutable. */
5560
5561static bool
5562modifying_const_object_p (tree_code code, tree obj, bool mutable_p)
5563{
5564 /* If this is initialization, there's no problem. */
5565 if (code != MODIFY_EXPR)
5566 return false;
5567
5568 /* [basic.type.qualifier] "A const object is an object of type
5569 const T or a non-mutable subobject of a const object." */
5570 if (mutable_p)
5571 return false;
5572
7eb5be6a
MP
5573 if (TREE_READONLY (obj))
5574 return true;
5575
5576 if (CP_TYPE_CONST_P (TREE_TYPE (obj)))
5577 {
5578 /* Although a COMPONENT_REF may have a const type, we should
5579 only consider it modifying a const object when any of the
5580 field components is const. This can happen when using
5581 constructs such as const_cast<const T &>(m), making something
5582 const even though it wasn't declared const. */
5583 if (TREE_CODE (obj) == COMPONENT_REF)
5584 return cref_has_const_field (obj);
5585 else
5586 return true;
5587 }
5588
5589 return false;
04e1749c
MP
5590}
5591
60813a46
JM
5592/* Evaluate an INIT_EXPR or MODIFY_EXPR. */
5593
5594static tree
5595cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
92a596e8 5596 bool lval,
60813a46
JM
5597 bool *non_constant_p, bool *overflow_p)
5598{
5599 constexpr_ctx new_ctx = *ctx;
5600
58cc255c
JM
5601 tree init = TREE_OPERAND (t, 1);
5602 if (TREE_CLOBBER_P (init))
5603 /* Just ignore clobbers. */
5604 return void_node;
5605
60813a46
JM
5606 /* First we figure out where we're storing to. */
5607 tree target = TREE_OPERAND (t, 0);
68d01920
JM
5608
5609 maybe_simplify_trivial_copy (target, init);
5610
3f8e2835 5611 tree type = TREE_TYPE (target);
e8b3c1bc
JM
5612 bool preeval = SCALAR_TYPE_P (type) || TREE_CODE (t) == MODIFY_EXPR;
5613 if (preeval)
5614 {
5615 /* Evaluate the value to be stored without knowing what object it will be
5616 stored in, so that any side-effects happen first. */
5617 if (!SCALAR_TYPE_P (type))
5618 new_ctx.ctor = new_ctx.object = NULL_TREE;
5619 init = cxx_eval_constant_expression (&new_ctx, init, false,
5620 non_constant_p, overflow_p);
5621 if (*non_constant_p)
5622 return t;
5623 }
60813a46 5624
d0aa42d2
JM
5625 bool evaluated = false;
5626 if (lval)
3f8e2835 5627 {
d0aa42d2
JM
5628 /* If we want to return a reference to the target, we need to evaluate it
5629 as a whole; otherwise, only evaluate the innermost piece to avoid
5630 building up unnecessary *_REFs. */
5631 target = cxx_eval_constant_expression (ctx, target, true,
5632 non_constant_p, overflow_p);
5633 evaluated = true;
5634 if (*non_constant_p)
5635 return t;
3f8e2835
JM
5636 }
5637
043666e0 5638 /* Find the underlying variable. */
cd9cf97b 5639 releasing_vec refs;
60813a46 5640 tree object = NULL_TREE;
04e1749c
MP
5641 /* If we're modifying a const object, save it. */
5642 tree const_object_being_modified = NULL_TREE;
5643 bool mutable_p = false;
60813a46
JM
5644 for (tree probe = target; object == NULL_TREE; )
5645 {
5646 switch (TREE_CODE (probe))
5647 {
5648 case BIT_FIELD_REF:
5649 case COMPONENT_REF:
5650 case ARRAY_REF:
043666e0
JM
5651 {
5652 tree ob = TREE_OPERAND (probe, 0);
5653 tree elt = TREE_OPERAND (probe, 1);
7d349dd8 5654 if (TREE_CODE (elt) == FIELD_DECL && DECL_MUTABLE_P (elt))
04e1749c 5655 mutable_p = true;
043666e0
JM
5656 if (TREE_CODE (probe) == ARRAY_REF)
5657 {
5658 elt = eval_and_check_array_index (ctx, probe, false,
5659 non_constant_p, overflow_p);
5660 if (*non_constant_p)
5661 return t;
5662 }
0f184800
MP
5663 /* We don't check modifying_const_object_p for ARRAY_REFs. Given
5664 "int a[10]", an ARRAY_REF "a[2]" can be "const int", even though
5665 the array isn't const. Instead, check "a" in the next iteration;
5666 that will detect modifying "const int a[10]". */
5667 else if (evaluated
5668 && modifying_const_object_p (TREE_CODE (t), probe,
5669 mutable_p)
5670 && const_object_being_modified == NULL_TREE)
5671 const_object_being_modified = probe;
043666e0
JM
5672 vec_safe_push (refs, elt);
5673 vec_safe_push (refs, TREE_TYPE (probe));
5674 probe = ob;
5675 }
60813a46
JM
5676 break;
5677
5678 default:
d0aa42d2
JM
5679 if (evaluated)
5680 object = probe;
5681 else
5682 {
5683 probe = cxx_eval_constant_expression (ctx, probe, true,
5684 non_constant_p, overflow_p);
5685 evaluated = true;
5686 if (*non_constant_p)
5687 return t;
5688 }
5689 break;
60813a46
JM
5690 }
5691 }
5692
04e1749c
MP
5693 if (modifying_const_object_p (TREE_CODE (t), object, mutable_p)
5694 && const_object_being_modified == NULL_TREE)
5695 const_object_being_modified = object;
5696
60813a46
JM
5697 /* And then find/build up our initializer for the path to the subobject
5698 we're initializing. */
a1408b7e 5699 tree *valp;
077dd9b3 5700 if (DECL_P (object))
8e007055 5701 valp = ctx->global->values.get (object);
a1408b7e
JM
5702 else
5703 valp = NULL;
60813a46
JM
5704 if (!valp)
5705 {
5706 /* A constant-expression cannot modify objects from outside the
5707 constant-expression. */
2b3ab879 5708 if (!ctx->quiet)
64d6d399 5709 error ("modification of %qE is not a constant expression", object);
60813a46
JM
5710 *non_constant_p = true;
5711 return t;
5712 }
3f8e2835 5713 type = TREE_TYPE (object);
c75ce530 5714 bool no_zero_init = true;
2d63bc39 5715
37244b21
PP
5716 releasing_vec ctors, indexes;
5717 auto_vec<int> index_pos_hints;
5718 bool activated_union_member_p = false;
276a52d5 5719 while (!refs->is_empty ())
60813a46
JM
5720 {
5721 if (*valp == NULL_TREE)
5722 {
5723 *valp = build_constructor (type, NULL);
e8c48716 5724 CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
60813a46 5725 }
d6b46fca
NS
5726 else if (TREE_CODE (*valp) == STRING_CST)
5727 {
5728 /* An array was initialized with a string constant, and now
5729 we're writing into one of its elements. Explode the
5730 single initialization into a set of element
5731 initializations. */
5732 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
5733
5734 tree string = *valp;
5735 tree elt_type = TREE_TYPE (type);
5736 unsigned chars_per_elt = (TYPE_PRECISION (elt_type)
5737 / TYPE_PRECISION (char_type_node));
5738 unsigned num_elts = TREE_STRING_LENGTH (string) / chars_per_elt;
5739 tree ary_ctor = build_constructor (type, NULL);
5740
5741 vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor), num_elts);
5742 for (unsigned ix = 0; ix != num_elts; ix++)
5743 {
5744 constructor_elt elt =
5745 {
5746 build_int_cst (size_type_node, ix),
5747 extract_string_elt (string, chars_per_elt, ix)
5748 };
5749 CONSTRUCTOR_ELTS (ary_ctor)->quick_push (elt);
5750 }
94ff4c9d 5751
d6b46fca
NS
5752 *valp = ary_ctor;
5753 }
5754
c75ce530
JM
5755 /* If the value of object is already zero-initialized, any new ctors for
5756 subobjects will also be zero-initialized. */
e8c48716 5757 no_zero_init = CONSTRUCTOR_NO_CLEARING (*valp);
60813a46 5758
ceaaf873 5759 enum tree_code code = TREE_CODE (type);
60813a46 5760 type = refs->pop();
ceaaf873 5761 tree index = refs->pop();
60813a46 5762
a8dd2b3e 5763 if (code == RECORD_TYPE && is_empty_field (index))
a4dfd0f0
JM
5764 /* Don't build a sub-CONSTRUCTOR for an empty base or field, as they
5765 have no data and might have an offset lower than previously declared
5766 fields, which confuses the middle-end. The code below will notice
5767 that we don't have a CONSTRUCTOR for our inner target and just
5768 return init. */
5769 break;
94ff4c9d 5770
37244b21
PP
5771 if (code == UNION_TYPE && CONSTRUCTOR_NELTS (*valp)
5772 && CONSTRUCTOR_ELT (*valp, 0)->index != index)
ceaaf873 5773 {
b04445d4 5774 if (cxx_dialect < cxx20)
ed4ec9ce 5775 {
37244b21
PP
5776 if (!ctx->quiet)
5777 error_at (cp_expr_loc_or_input_loc (t),
5778 "change of the active member of a union "
5779 "from %qD to %qD",
5780 CONSTRUCTOR_ELT (*valp, 0)->index,
5781 index);
5782 *non_constant_p = true;
ed4ec9ce 5783 }
37244b21
PP
5784 else if (TREE_CODE (t) == MODIFY_EXPR
5785 && CONSTRUCTOR_NO_CLEARING (*valp))
ceaaf873 5786 {
37244b21
PP
5787 /* Diagnose changing the active union member while the union
5788 is in the process of being initialized. */
5789 if (!ctx->quiet)
5790 error_at (cp_expr_loc_or_input_loc (t),
5791 "change of the active member of a union "
5792 "from %qD to %qD during initialization",
5793 CONSTRUCTOR_ELT (*valp, 0)->index,
5794 index);
5795 *non_constant_p = true;
ceaaf873 5796 }
37244b21
PP
5797 no_zero_init = true;
5798 }
88504f34 5799
37244b21
PP
5800 vec_safe_push (ctors, *valp);
5801 vec_safe_push (indexes, index);
88504f34 5802
37244b21 5803 constructor_elt *cep
077dd9b3 5804 = get_or_insert_ctor_field (*valp, index);
37244b21
PP
5805 index_pos_hints.safe_push (cep - CONSTRUCTOR_ELTS (*valp)->begin());
5806
5807 if (code == UNION_TYPE)
5808 activated_union_member_p = true;
b599bf9d 5809
60813a46
JM
5810 valp = &cep->value;
5811 }
60813a46 5812
04e1749c
MP
5813 /* Detect modifying a constant object in constexpr evaluation.
5814 We have found a const object that is being modified. Figure out
5815 if we need to issue an error. Consider
5816
5817 struct A {
5818 int n;
5819 constexpr A() : n(1) { n = 2; } // #1
5820 };
5821 struct B {
5822 const A a;
5823 constexpr B() { a.n = 3; } // #2
5824 };
5825 constexpr B b{};
5826
5827 #1 is OK, since we're modifying an object under construction, but
5828 #2 is wrong, since "a" is const and has been fully constructed.
5829 To track it, we use the TREE_READONLY bit in the object's CONSTRUCTOR
5830 which means that the object is read-only. For the example above, the
5831 *ctors stack at the point of #2 will look like:
5832
5833 ctors[0] = {.a={.n=2}} TREE_READONLY = 0
5834 ctors[1] = {.n=2} TREE_READONLY = 1
5835
5836 and we're modifying "b.a", so we search the stack and see if the
5837 constructor for "b.a" has already run. */
5838 if (const_object_being_modified)
5839 {
5840 bool fail = false;
276a52d5
JJ
5841 tree const_objtype
5842 = strip_array_types (TREE_TYPE (const_object_being_modified));
5843 if (!CLASS_TYPE_P (const_objtype))
04e1749c
MP
5844 fail = true;
5845 else
5846 {
5847 /* [class.ctor]p5 "A constructor can be invoked for a const,
5848 volatile, or const volatile object. const and volatile
5849 semantics are not applied on an object under construction.
5850 They come into effect when the constructor for the most
5851 derived object ends." */
3f207ab3 5852 for (tree elt : *ctors)
04e1749c
MP
5853 if (same_type_ignoring_top_level_qualifiers_p
5854 (TREE_TYPE (const_object_being_modified), TREE_TYPE (elt)))
5855 {
5856 fail = TREE_READONLY (elt);
5857 break;
5858 }
5859 }
5860 if (fail)
5861 {
5862 if (!ctx->quiet)
5863 modifying_const_object_error (t, const_object_being_modified);
5864 *non_constant_p = true;
5865 return t;
5866 }
5867 }
5868
e8b3c1bc 5869 if (!preeval)
60813a46 5870 {
6c59b8a9
JM
5871 /* We're handling an INIT_EXPR of class type, so the value of the
5872 initializer can depend on the object it's initializing. */
5873
60813a46
JM
5874 /* Create a new CONSTRUCTOR in case evaluation of the initializer
5875 wants to modify it. */
acb2970c 5876 if (*valp == NULL_TREE)
73a84269 5877 {
664beaf2 5878 *valp = build_constructor (type, NULL);
e8c48716 5879 CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
73a84269 5880 }
664beaf2 5881 new_ctx.ctor = *valp;
60813a46 5882 new_ctx.object = target;
10d2f801
JM
5883 /* Avoid temporary materialization when initializing from a TARGET_EXPR.
5884 We don't need to mess with AGGR_EXPR_SLOT/VEC_INIT_EXPR_SLOT because
5885 expansion of those trees uses ctx instead. */
5886 if (TREE_CODE (init) == TARGET_EXPR)
5887 if (tree tinit = TARGET_EXPR_INITIAL (init))
5888 init = tinit;
e8b3c1bc
JM
5889 init = cxx_eval_constant_expression (&new_ctx, init, false,
5890 non_constant_p, overflow_p);
37244b21
PP
5891 /* The hash table might have moved since the get earlier, and the
5892 initializer might have mutated the underlying CONSTRUCTORs, so we must
5893 recompute VALP. */
5894 valp = ctx->global->values.get (object);
5895 for (unsigned i = 0; i < vec_safe_length (indexes); i++)
5896 {
5897 constructor_elt *cep
5898 = get_or_insert_ctor_field (*valp, indexes[i], index_pos_hints[i]);
5899 valp = &cep->value;
5900 }
60813a46
JM
5901 }
5902
7574c916 5903 /* Don't share a CONSTRUCTOR that might be changed later. */
0146e25f 5904 init = unshare_constructor (init);
d96e8407 5905
e8b3c1bc
JM
5906 if (*valp && TREE_CODE (*valp) == CONSTRUCTOR
5907 && TREE_CODE (init) == CONSTRUCTOR)
acb2970c 5908 {
d96e8407
JM
5909 /* An outer ctx->ctor might be pointing to *valp, so replace
5910 its contents. */
d0aa42d2
JM
5911 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init),
5912 TREE_TYPE (*valp)))
5913 {
5914 /* For initialization of an empty base, the original target will be
5915 *(base*)this, evaluation of which resolves to the object
5916 argument, which has the derived type rather than the base type. In
5917 this situation, just evaluate the initializer and return, since
5918 there's no actual data to store. */
21fd62e5
PP
5919 gcc_assert (is_empty_class (TREE_TYPE (init)));
5920 return lval ? target : init;
d0aa42d2 5921 }
d96e8407
JM
5922 CONSTRUCTOR_ELTS (*valp) = CONSTRUCTOR_ELTS (init);
5923 TREE_CONSTANT (*valp) = TREE_CONSTANT (init);
5924 TREE_SIDE_EFFECTS (*valp) = TREE_SIDE_EFFECTS (init);
e8c48716
JM
5925 CONSTRUCTOR_NO_CLEARING (*valp)
5926 = CONSTRUCTOR_NO_CLEARING (init);
acb2970c 5927 }
35e8b38a
JJ
5928 else if (TREE_CODE (init) == CONSTRUCTOR
5929 && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init),
5930 type))
5931 {
5932 /* See above on initialization of empty bases. */
5933 gcc_assert (is_empty_class (TREE_TYPE (init)) && !lval);
5934 return init;
5935 }
60813a46 5936 else
d96e8407 5937 *valp = init;
2d63bc39 5938
7eb5be6a
MP
5939 /* After initialization, 'const' semantics apply to the value of the
5940 object. Make a note of this fact by marking the CONSTRUCTOR
5941 TREE_READONLY. */
5942 if (TREE_CODE (t) == INIT_EXPR
5943 && TREE_CODE (*valp) == CONSTRUCTOR
5944 && TYPE_READONLY (type))
64da1b76
PP
5945 {
5946 if (INDIRECT_REF_P (target)
5947 && (is_this_parameter
5948 (tree_strip_nop_conversions (TREE_OPERAND (target, 0)))))
5949 /* We've just initialized '*this' (perhaps via the target
5950 constructor of a delegating constructor). Leave it up to the
5951 caller that set 'this' to set TREE_READONLY appropriately. */
5952 gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
5953 (TREE_TYPE (target), type));
5954 else
5955 TREE_READONLY (*valp) = true;
5956 }
7eb5be6a 5957
d96e8407
JM
5958 /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing
5959 CONSTRUCTORs, if any. */
d96e8407
JM
5960 bool c = TREE_CONSTANT (init);
5961 bool s = TREE_SIDE_EFFECTS (init);
37244b21 5962 if (!c || s || activated_union_member_p)
3f207ab3 5963 for (tree elt : *ctors)
d96e8407
JM
5964 {
5965 if (!c)
5966 TREE_CONSTANT (elt) = false;
5967 if (s)
5968 TREE_SIDE_EFFECTS (elt) = true;
b599bf9d
PP
5969 /* Clear CONSTRUCTOR_NO_CLEARING since we've activated a member of
5970 this union. */
5971 if (TREE_CODE (TREE_TYPE (elt)) == UNION_TYPE)
5972 CONSTRUCTOR_NO_CLEARING (elt) = false;
d96e8407 5973 }
60813a46
JM
5974
5975 if (*non_constant_p)
5976 return t;
92a596e8 5977 else if (lval)
60813a46
JM
5978 return target;
5979 else
3bdf0a9b 5980 return init;
60813a46
JM
5981}
5982
5983/* Evaluate a ++ or -- expression. */
5984
5985static tree
5986cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t,
92a596e8 5987 bool lval,
60813a46
JM
5988 bool *non_constant_p, bool *overflow_p)
5989{
5990 enum tree_code code = TREE_CODE (t);
5991 tree type = TREE_TYPE (t);
5992 tree op = TREE_OPERAND (t, 0);
5993 tree offset = TREE_OPERAND (t, 1);
5994 gcc_assert (TREE_CONSTANT (offset));
5995
d85569f6
MP
5996 /* OFFSET is constant, but perhaps not constant enough. We need to
5997 e.g. bash FLOAT_EXPRs to REAL_CSTs. */
5998 offset = fold_simple (offset);
5999
60813a46 6000 /* The operand as an lvalue. */
2b3ab879 6001 op = cxx_eval_constant_expression (ctx, op, true,
5a804683 6002 non_constant_p, overflow_p);
60813a46
JM
6003
6004 /* The operand as an rvalue. */
84dd815f
JM
6005 tree val
6006 = cxx_eval_constant_expression (ctx, op, false,
6007 non_constant_p, overflow_p);
caee690e
JM
6008 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
6009 a local array in a constexpr function. */
71a93b08 6010 bool ptr = INDIRECT_TYPE_P (TREE_TYPE (val));
caee690e
JM
6011 if (!ptr)
6012 VERIFY_CONSTANT (val);
60813a46
JM
6013
6014 /* The modified value. */
6015 bool inc = (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR);
ac2f8d26 6016 tree mod;
71a93b08 6017 if (INDIRECT_TYPE_P (type))
ac2f8d26
JM
6018 {
6019 /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
6020 offset = convert_to_ptrofftype (offset);
6021 if (!inc)
6022 offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset);
6023 mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset);
6024 }
6025 else
6026 mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset);
caee690e
JM
6027 if (!ptr)
6028 VERIFY_CONSTANT (mod);
60813a46
JM
6029
6030 /* Storing the modified value. */
04e1749c
MP
6031 tree store = build2_loc (cp_expr_loc_or_loc (t, input_location),
6032 MODIFY_EXPR, type, op, mod);
d1bba463
PP
6033 mod = cxx_eval_constant_expression (ctx, store, lval,
6034 non_constant_p, overflow_p);
ecdcd560 6035 ggc_free (store);
d1bba463
PP
6036 if (*non_constant_p)
6037 return t;
60813a46
JM
6038
6039 /* And the value of the expression. */
6040 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
d1bba463
PP
6041 /* Prefix ops are lvalues, but the caller might want an rvalue;
6042 lval has already been taken into account in the store above. */
6043 return mod;
60813a46
JM
6044 else
6045 /* Postfix ops are rvalues. */
6046 return val;
6047}
6048
56632b27
JM
6049/* Predicates for the meaning of *jump_target. */
6050
6051static bool
6052returns (tree *jump_target)
6053{
6054 return *jump_target
d49318d9
PC
6055 && (TREE_CODE (*jump_target) == RETURN_EXPR
6056 || (TREE_CODE (*jump_target) == LABEL_DECL
6057 && LABEL_DECL_CDTOR (*jump_target)));
56632b27
JM
6058}
6059
6060static bool
6061breaks (tree *jump_target)
6062{
6063 return *jump_target
06ec22b7
JM
6064 && ((TREE_CODE (*jump_target) == LABEL_DECL
6065 && LABEL_DECL_BREAK (*jump_target))
3075affd 6066 || TREE_CODE (*jump_target) == BREAK_STMT
06ec22b7 6067 || TREE_CODE (*jump_target) == EXIT_EXPR);
56632b27
JM
6068}
6069
6070static bool
6071continues (tree *jump_target)
6072{
6073 return *jump_target
3075affd
JM
6074 && ((TREE_CODE (*jump_target) == LABEL_DECL
6075 && LABEL_DECL_CONTINUE (*jump_target))
6076 || TREE_CODE (*jump_target) == CONTINUE_STMT);
6077
56632b27
JM
6078}
6079
6080static bool
6081switches (tree *jump_target)
6082{
6083 return *jump_target
6084 && TREE_CODE (*jump_target) == INTEGER_CST;
6085}
6086
6087/* Subroutine of cxx_eval_statement_list. Determine whether the statement
4b390698
JJ
6088 STMT matches *jump_target. If we're looking for a case label and we see
6089 the default label, note it in ctx->css_state. */
56632b27
JM
6090
6091static bool
4b390698 6092label_matches (const constexpr_ctx *ctx, tree *jump_target, tree stmt)
56632b27 6093{
56632b27
JM
6094 switch (TREE_CODE (*jump_target))
6095 {
6096 case LABEL_DECL:
6097 if (TREE_CODE (stmt) == LABEL_EXPR
6098 && LABEL_EXPR_LABEL (stmt) == *jump_target)
6099 return true;
6100 break;
6101
6102 case INTEGER_CST:
6103 if (TREE_CODE (stmt) == CASE_LABEL_EXPR)
6104 {
4b390698 6105 gcc_assert (ctx->css_state != NULL);
56632b27 6106 if (!CASE_LOW (stmt))
4b390698
JJ
6107 {
6108 /* default: should appear just once in a SWITCH_EXPR
6109 body (excluding nested SWITCH_EXPR). */
6110 gcc_assert (*ctx->css_state != css_default_seen);
6111 /* When evaluating SWITCH_EXPR body for the second time,
6112 return true for the default: label. */
6113 if (*ctx->css_state == css_default_processing)
6114 return true;
6115 *ctx->css_state = css_default_seen;
6116 }
385ed708
JJ
6117 else if (CASE_HIGH (stmt))
6118 {
6119 if (tree_int_cst_le (CASE_LOW (stmt), *jump_target)
6120 && tree_int_cst_le (*jump_target, CASE_HIGH (stmt)))
6121 return true;
6122 }
56632b27
JM
6123 else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt)))
6124 return true;
6125 }
6126 break;
6127
43574e4f
JJ
6128 case BREAK_STMT:
6129 case CONTINUE_STMT:
6130 /* These two are handled directly in cxx_eval_loop_expr by testing
6131 breaks (jump_target) or continues (jump_target). */
6132 break;
6133
56632b27
JM
6134 default:
6135 gcc_unreachable ();
6136 }
6137 return false;
6138}
6139
6140/* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
6141 semantics, for switch, break, continue, and return. */
6142
6143static tree
6144cxx_eval_statement_list (const constexpr_ctx *ctx, tree t,
56632b27
JM
6145 bool *non_constant_p, bool *overflow_p,
6146 tree *jump_target)
6147{
58611fb6 6148 tree local_target;
345edb37
JJ
6149 /* In a statement-expression we want to return the last value.
6150 For empty statement expression return void_node. */
6151 tree r = void_node;
58611fb6
JM
6152 if (!jump_target)
6153 {
6154 local_target = NULL_TREE;
6155 jump_target = &local_target;
6156 }
0f54cc9c 6157 for (tree stmt : tsi_range (t))
56632b27 6158 {
0250ba58
MP
6159 /* We've found a continue, so skip everything until we reach
6160 the label its jumping to. */
6161 if (continues (jump_target))
6162 {
6163 if (label_matches (ctx, jump_target, stmt))
6164 /* Found it. */
6165 *jump_target = NULL_TREE;
6166 else
6167 continue;
6168 }
6ef72c36
JJ
6169 if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT)
6170 continue;
58611fb6
JM
6171 r = cxx_eval_constant_expression (ctx, stmt, false,
6172 non_constant_p, overflow_p,
6173 jump_target);
56632b27
JM
6174 if (*non_constant_p)
6175 break;
6176 if (returns (jump_target) || breaks (jump_target))
6177 break;
6178 }
ce36ba09
JM
6179 if (*jump_target && jump_target == &local_target)
6180 {
6181 /* We aren't communicating the jump to our caller, so give up. We don't
6182 need to support evaluation of jumps out of statement-exprs. */
6183 if (!ctx->quiet)
f9d0ca40 6184 error_at (cp_expr_loc_or_input_loc (r),
ce36ba09
JM
6185 "statement is not a constant expression");
6186 *non_constant_p = true;
6187 }
58611fb6 6188 return r;
56632b27
JM
6189}
6190
6191/* Evaluate a LOOP_EXPR for side-effects. Handles break and return
6192 semantics; continue semantics are covered by cxx_eval_statement_list. */
6193
6194static tree
6195cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t,
56632b27
JM
6196 bool *non_constant_p, bool *overflow_p,
6197 tree *jump_target)
6198{
39dce2b7 6199 constexpr_ctx new_ctx = *ctx;
8e007055
JJ
6200 tree local_target;
6201 if (!jump_target)
6202 {
6203 local_target = NULL_TREE;
6204 jump_target = &local_target;
6205 }
39dce2b7 6206
43574e4f 6207 tree body, cond = NULL_TREE, expr = NULL_TREE;
5ec2cd9f 6208 int count = 0;
43574e4f
JJ
6209 switch (TREE_CODE (t))
6210 {
6211 case LOOP_EXPR:
6212 body = LOOP_EXPR_BODY (t);
6213 break;
6214 case DO_STMT:
6215 body = DO_BODY (t);
6216 cond = DO_COND (t);
6217 break;
6218 case WHILE_STMT:
6219 body = WHILE_BODY (t);
6220 cond = WHILE_COND (t);
6221 count = -1;
6222 break;
6223 case FOR_STMT:
6224 if (FOR_INIT_STMT (t))
6225 cxx_eval_constant_expression (ctx, FOR_INIT_STMT (t), /*lval*/false,
6226 non_constant_p, overflow_p, jump_target);
6227 if (*non_constant_p)
6228 return NULL_TREE;
6229 body = FOR_BODY (t);
6230 cond = FOR_COND (t);
6231 expr = FOR_EXPR (t);
6232 count = -1;
6233 break;
6234 default:
6235 gcc_unreachable ();
6236 }
0ee28590 6237 auto_vec<tree, 10> save_exprs;
43574e4f 6238 new_ctx.save_exprs = &save_exprs;
d259b234 6239 do
56632b27 6240 {
43574e4f
JJ
6241 if (count != -1)
6242 {
6243 if (body)
6244 cxx_eval_constant_expression (&new_ctx, body, /*lval*/false,
6245 non_constant_p, overflow_p,
6246 jump_target);
6247 if (breaks (jump_target))
6248 {
6249 *jump_target = NULL_TREE;
6250 break;
6251 }
39dce2b7 6252
43574e4f
JJ
6253 if (TREE_CODE (t) != LOOP_EXPR && continues (jump_target))
6254 *jump_target = NULL_TREE;
6255
6256 if (expr)
6257 cxx_eval_constant_expression (&new_ctx, expr, /*lval*/false,
6258 non_constant_p, overflow_p,
6259 jump_target);
6260 }
6261
6262 if (cond)
6263 {
6264 tree res
6265 = cxx_eval_constant_expression (&new_ctx, cond, /*lval*/false,
6266 non_constant_p, overflow_p,
6267 jump_target);
6268 if (res)
6269 {
6270 if (verify_constant (res, ctx->quiet, non_constant_p,
6271 overflow_p))
6272 break;
6273 if (integer_zerop (res))
6274 break;
6275 }
6276 else
6277 gcc_assert (*jump_target);
6278 }
39dce2b7 6279
ee1de08d 6280 /* Forget saved values of SAVE_EXPRs and TARGET_EXPRs. */
3f207ab3 6281 for (tree save_expr : save_exprs)
8e007055 6282 ctx->global->values.remove (save_expr);
0ee28590 6283 save_exprs.truncate (0);
43574e4f 6284
5ec2cd9f
JM
6285 if (++count >= constexpr_loop_limit)
6286 {
6287 if (!ctx->quiet)
f9d0ca40 6288 error_at (cp_expr_loc_or_input_loc (t),
84fa214d 6289 "%<constexpr%> loop iteration count exceeds limit of %d "
a3f9f006 6290 "(use %<-fconstexpr-loop-limit=%> to increase the limit)",
5ec2cd9f
JM
6291 constexpr_loop_limit);
6292 *non_constant_p = true;
6293 break;
6294 }
56632b27 6295 }
4b390698
JJ
6296 while (!returns (jump_target)
6297 && !breaks (jump_target)
43574e4f
JJ
6298 && !continues (jump_target)
6299 && (!switches (jump_target) || count == 0)
4b390698 6300 && !*non_constant_p);
d259b234 6301
ee1de08d 6302 /* Forget saved values of SAVE_EXPRs and TARGET_EXPRs. */
3f207ab3 6303 for (tree save_expr : save_exprs)
8e007055 6304 ctx->global->values.remove (save_expr);
39dce2b7 6305
56632b27
JM
6306 return NULL_TREE;
6307}
6308
6309/* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
6310 semantics. */
6311
6312static tree
6313cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t,
56632b27
JM
6314 bool *non_constant_p, bool *overflow_p,
6315 tree *jump_target)
6316{
43574e4f
JJ
6317 tree cond
6318 = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_COND (t) : SWITCH_COND (t);
2b3ab879 6319 cond = cxx_eval_constant_expression (ctx, cond, false,
5a804683 6320 non_constant_p, overflow_p);
56632b27
JM
6321 VERIFY_CONSTANT (cond);
6322 *jump_target = cond;
6323
43574e4f
JJ
6324 tree body
6325 = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_BODY (t) : SWITCH_BODY (t);
4b390698
JJ
6326 constexpr_ctx new_ctx = *ctx;
6327 constexpr_switch_state css = css_default_not_seen;
6328 new_ctx.css_state = &css;
6329 cxx_eval_constant_expression (&new_ctx, body, false,
6330 non_constant_p, overflow_p, jump_target);
6331 if (switches (jump_target) && css == css_default_seen)
6332 {
6333 /* If the SWITCH_EXPR body has default: label, process it once again,
6334 this time instructing label_matches to return true for default:
6335 label on switches (jump_target). */
6336 css = css_default_processing;
6337 cxx_eval_constant_expression (&new_ctx, body, false,
6338 non_constant_p, overflow_p, jump_target);
6339 }
56632b27
JM
6340 if (breaks (jump_target) || switches (jump_target))
6341 *jump_target = NULL_TREE;
6342 return NULL_TREE;
6343}
6344
89262ec6
JM
6345/* Find the object of TYPE under initialization in CTX. */
6346
6347static tree
6348lookup_placeholder (const constexpr_ctx *ctx, bool lval, tree type)
6349{
fbd603c4 6350 if (!ctx)
89262ec6
JM
6351 return NULL_TREE;
6352
49a86fce
PP
6353 /* Prefer the outermost matching object, but don't cross
6354 CONSTRUCTOR_PLACEHOLDER_BOUNDARY constructors. */
6355 if (ctx->ctor && !CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor))
6356 if (tree outer_ob = lookup_placeholder (ctx->parent, lval, type))
6357 return outer_ob;
6358
89262ec6
JM
6359 /* We could use ctx->object unconditionally, but using ctx->ctor when we
6360 can is a minor optimization. */
fbd603c4 6361 if (!lval && ctx->ctor && same_type_p (TREE_TYPE (ctx->ctor), type))
89262ec6
JM
6362 return ctx->ctor;
6363
fbd603c4
JM
6364 if (!ctx->object)
6365 return NULL_TREE;
6366
89262ec6
JM
6367 /* Since an object cannot have a field of its own type, we can search outward
6368 from ctx->object to find the unique containing object of TYPE. */
6369 tree ob = ctx->object;
6370 while (ob)
6371 {
6372 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob), type))
6373 break;
6374 if (handled_component_p (ob))
6375 ob = TREE_OPERAND (ob, 0);
6376 else
6377 ob = NULL_TREE;
6378 }
6379
6380 return ob;
6381}
6382
7c814975
MP
6383/* Complain about an attempt to evaluate inline assembly. */
6384
6385static void
6386inline_asm_in_constexpr_error (location_t loc)
6387{
6388 auto_diagnostic_group d;
6389 error_at (loc, "inline assembly is not a constant expression");
6390 inform (loc, "only unevaluated inline assembly is allowed in a "
b04445d4 6391 "%<constexpr%> function in C++20");
7c814975
MP
6392}
6393
76b75018
JM
6394/* We're getting the constant value of DECL in a manifestly constant-evaluated
6395 context; maybe complain about that. */
6396
6397static void
6398maybe_warn_about_constant_value (location_t loc, tree decl)
6399{
6400 static bool explained = false;
6401 if (cxx_dialect >= cxx17
6402 && warn_interference_size
00f34291 6403 && !OPTION_SET_P (param_destruct_interfere_size)
76b75018
JM
6404 && DECL_CONTEXT (decl) == std_node
6405 && id_equal (DECL_NAME (decl), "hardware_destructive_interference_size")
6406 && (LOCATION_FILE (input_location) != main_input_filename
6407 || module_exporting_p ())
6408 && warning_at (loc, OPT_Winterference_size, "use of %qD", decl)
6409 && !explained)
6410 {
6411 explained = true;
6412 inform (loc, "its value can vary between compiler versions or "
6413 "with different %<-mtune%> or %<-mcpu%> flags");
6414 inform (loc, "if this use is part of a public ABI, change it to "
6415 "instead use a constant variable you define");
6416 inform (loc, "the default value for the current CPU tuning "
6417 "is %d bytes", param_destruct_interfere_size);
6418 inform (loc, "you can stabilize this value with %<--param "
6419 "hardware_destructive_interference_size=%d%>, or disable "
6420 "this warning with %<-Wno-interference-size%>",
6421 param_destruct_interfere_size);
6422 }
6423}
6424
2d76680f
PC
6425/* Attempt to reduce the expression T to a constant value.
6426 On failure, issue diagnostic and return error_mark_node. */
6427/* FIXME unify with c_fully_fold */
60813a46 6428/* FIXME overflow_p is too global */
2d76680f
PC
6429
6430static tree
3e605b20 6431cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
92a596e8 6432 bool lval,
56632b27 6433 bool *non_constant_p, bool *overflow_p,
3075affd 6434 tree *jump_target /* = NULL */)
2d76680f 6435{
4b390698
JJ
6436 if (jump_target && *jump_target)
6437 {
6438 /* If we are jumping, ignore all statements/expressions except those
6439 that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */
6440 switch (TREE_CODE (t))
6441 {
6442 case BIND_EXPR:
6443 case STATEMENT_LIST:
6444 case LOOP_EXPR:
6445 case COND_EXPR:
43574e4f
JJ
6446 case IF_STMT:
6447 case DO_STMT:
6448 case WHILE_STMT:
6449 case FOR_STMT:
4b390698
JJ
6450 break;
6451 case LABEL_EXPR:
6452 case CASE_LABEL_EXPR:
6453 if (label_matches (ctx, jump_target, t))
6454 /* Found it. */
6455 *jump_target = NULL_TREE;
6456 return NULL_TREE;
6457 default:
6458 return NULL_TREE;
6459 }
6460 }
3075affd 6461 if (error_operand_p (t))
2d76680f
PC
6462 {
6463 *non_constant_p = true;
6464 return t;
6465 }
7a649ef5 6466
6821245b
JM
6467 location_t loc = cp_expr_loc_or_input_loc (t);
6468
7a649ef5
JM
6469 STRIP_ANY_LOCATION_WRAPPER (t);
6470
2d76680f
PC
6471 if (CONSTANT_CLASS_P (t))
6472 {
61637db3
JJ
6473 if (TREE_OVERFLOW (t))
6474 {
6475 if (!ctx->quiet)
6476 permerror (input_location, "overflow in constant expression");
6477 if (!flag_permissive || ctx->quiet)
6478 *overflow_p = true;
6479 }
8bada5cd
MS
6480
6481 if (TREE_CODE (t) == INTEGER_CST
9f613f06 6482 && TYPE_PTR_P (TREE_TYPE (t))
f45610a4
JJ
6483 /* INTEGER_CST with pointer-to-method type is only used
6484 for a virtual method in a pointer to member function.
6485 Don't reject those. */
6486 && TREE_CODE (TREE_TYPE (TREE_TYPE (t))) != METHOD_TYPE
8bada5cd
MS
6487 && !integer_zerop (t))
6488 {
6489 if (!ctx->quiet)
6490 error ("value %qE of type %qT is not a constant expression",
6491 t, TREE_TYPE (t));
6492 *non_constant_p = true;
6493 }
6494
2d76680f
PC
6495 return t;
6496 }
2d76680f 6497
a15ffa22 6498 /* Avoid excessively long constexpr evaluations. */
8e007055 6499 if (++ctx->global->constexpr_ops_count >= constexpr_ops_limit)
a15ffa22
JJ
6500 {
6501 if (!ctx->quiet)
6821245b 6502 error_at (loc,
a15ffa22 6503 "%<constexpr%> evaluation operation count exceeds limit of "
a9c697b8 6504 "%wd (use %<-fconstexpr-ops-limit=%> to increase the limit)",
a15ffa22 6505 constexpr_ops_limit);
8e007055 6506 ctx->global->constexpr_ops_count = INTTYPE_MINIMUM (HOST_WIDE_INT);
a15ffa22
JJ
6507 *non_constant_p = true;
6508 return t;
6509 }
6510
7a649ef5
JM
6511 constexpr_ctx new_ctx;
6512 tree r = t;
6513
8bada5cd
MS
6514 tree_code tcode = TREE_CODE (t);
6515 switch (tcode)
2d76680f 6516 {
60813a46 6517 case RESULT_DECL:
92a596e8 6518 if (lval)
60813a46
JM
6519 return t;
6520 /* We ask for an rvalue for the RESULT_DECL when indirecting
bf66b9b4
AH
6521 through an invisible reference, or in named return value
6522 optimization. */
8e007055 6523 if (tree *p = ctx->global->values.get (t))
1efb1dc2
MP
6524 return *p;
6525 else
6526 {
6527 if (!ctx->quiet)
6528 error ("%qE is not a constant expression", t);
6529 *non_constant_p = true;
6530 }
6531 break;
60813a46 6532
2d76680f 6533 case VAR_DECL:
70f40fea 6534 if (DECL_HAS_VALUE_EXPR_P (t))
c59fa7ea
JM
6535 {
6536 if (is_normal_capture_proxy (t)
6537 && current_function_decl == DECL_CONTEXT (t))
6538 {
6539 /* Function parms aren't constexpr within the function
6540 definition, so don't try to look at the closure. But if the
6541 captured variable is constant, try to evaluate it directly. */
6542 r = DECL_CAPTURED_VARIABLE (t);
6543 tree type = TREE_TYPE (t);
6544 if (TYPE_REF_P (type) != TYPE_REF_P (TREE_TYPE (r)))
6545 {
6546 /* Adjust r to match the reference-ness of t. */
6547 if (TYPE_REF_P (type))
6548 r = build_address (r);
6549 else
6550 r = convert_from_reference (r);
6551 }
6552 }
6553 else
6554 r = DECL_VALUE_EXPR (t);
6555 return cxx_eval_constant_expression (ctx, r, lval, non_constant_p,
6556 overflow_p);
6557 }
191816a3 6558 /* fall through */
7c83622c 6559 case CONST_DECL:
e53b6e56 6560 /* We used to not check lval for CONST_DECL, but darwin.cc uses
7c83622c 6561 CONST_DECL for aggregate constants. */
92a596e8 6562 if (lval)
2d76680f 6563 return t;
ef529765
JM
6564 else if (t == ctx->object)
6565 return ctx->ctor;
6566 if (VAR_P (t))
6567 if (tree *p = ctx->global->values.get (t))
6568 if (*p != NULL_TREE)
6569 {
6570 r = *p;
6571 break;
6572 }
76b75018
JM
6573 if (ctx->manifestly_const_eval)
6574 maybe_warn_about_constant_value (loc, t);
f4fce183 6575 if (COMPLETE_TYPE_P (TREE_TYPE (t))
dbcd32f8 6576 && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
7dc2b4a2
JM
6577 {
6578 /* If the class is empty, we aren't actually loading anything. */
6579 r = build_constructor (TREE_TYPE (t), NULL);
6580 TREE_CONSTANT (r) = true;
6581 }
6582 else if (ctx->strict)
8c00059c 6583 r = decl_really_constant_value (t, /*unshare_p=*/false);
69eb4fde 6584 else
8c00059c 6585 r = decl_constant_value (t, /*unshare_p=*/false);
2d76680f
PC
6586 if (TREE_CODE (r) == TARGET_EXPR
6587 && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR)
6588 r = TARGET_EXPR_INITIAL (r);
2d76680f
PC
6589 if (DECL_P (r))
6590 {
2b3ab879 6591 if (!ctx->quiet)
6821245b 6592 non_const_var_error (loc, r);
2d76680f
PC
6593 *non_constant_p = true;
6594 }
6595 break;
6596
96a95ac1
AO
6597 case DEBUG_BEGIN_STMT:
6598 /* ??? It might be nice to retain this information somehow, so
6599 as to be able to step into a constexpr function call. */
6600 /* Fall through. */
6601
2d76680f
PC
6602 case FUNCTION_DECL:
6603 case TEMPLATE_DECL:
6604 case LABEL_DECL:
56632b27
JM
6605 case LABEL_EXPR:
6606 case CASE_LABEL_EXPR:
4b390698 6607 case PREDICT_EXPR:
2d76680f
PC
6608 return t;
6609
6610 case PARM_DECL:
9f613f06 6611 if (lval && !TYPE_REF_P (TREE_TYPE (t)))
60813a46 6612 /* glvalue use. */;
8e007055 6613 else if (tree *p = ctx->global->values.get (r))
60813a46 6614 r = *p;
92a596e8 6615 else if (lval)
2d76680f 6616 /* Defer in case this is only used for its type. */;
1c2f613f 6617 else if (COMPLETE_TYPE_P (TREE_TYPE (t))
dbcd32f8 6618 && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
052beba4
JM
6619 {
6620 /* If the class is empty, we aren't actually loading anything. */
6621 r = build_constructor (TREE_TYPE (t), NULL);
6622 TREE_CONSTANT (r) = true;
6623 }
2d76680f
PC
6624 else
6625 {
2b3ab879 6626 if (!ctx->quiet)
2d76680f
PC
6627 error ("%qE is not a constant expression", t);
6628 *non_constant_p = true;
6629 }
6630 break;
6631
6632 case CALL_EXPR:
6633 case AGGR_INIT_EXPR:
92a596e8 6634 r = cxx_eval_call_expression (ctx, t, lval,
2d76680f
PC
6635 non_constant_p, overflow_p);
6636 break;
6637
60813a46
JM
6638 case DECL_EXPR:
6639 {
6640 r = DECL_EXPR_DECL (t);
43574e4f
JJ
6641 if (TREE_CODE (r) == USING_DECL)
6642 {
6643 r = void_node;
6644 break;
6645 }
8892d532
JJ
6646
6647 if (VAR_P (r)
6648 && (TREE_STATIC (r) || CP_DECL_THREAD_LOCAL_P (r))
6649 /* Allow __FUNCTION__ etc. */
6650 && !DECL_ARTIFICIAL (r))
6651 {
8892d532
JJ
6652 if (!ctx->quiet)
6653 {
6654 if (CP_DECL_THREAD_LOCAL_P (r))
6655 error_at (loc, "control passes through declaration of %qD "
6656 "with thread storage duration", r);
6657 else
6658 error_at (loc, "control passes through declaration of %qD "
6659 "with static storage duration", r);
6660 }
6661 *non_constant_p = true;
6662 break;
6663 }
6664
60813a46
JM
6665 if (AGGREGATE_TYPE_P (TREE_TYPE (r))
6666 || VECTOR_TYPE_P (TREE_TYPE (r)))
6667 {
6668 new_ctx = *ctx;
6669 new_ctx.object = r;
6670 new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL);
e8c48716 6671 CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
8e007055 6672 ctx->global->values.put (r, new_ctx.ctor);
60813a46
JM
6673 ctx = &new_ctx;
6674 }
6675
6676 if (tree init = DECL_INITIAL (r))
6677 {
6678 init = cxx_eval_constant_expression (ctx, init,
2b3ab879 6679 false,
5a804683 6680 non_constant_p, overflow_p);
7f0e23e9 6681 /* Don't share a CONSTRUCTOR that might be changed. */
0146e25f 6682 init = unshare_constructor (init);
04e1749c
MP
6683 /* Remember that a constant object's constructor has already
6684 run. */
6685 if (CLASS_TYPE_P (TREE_TYPE (r))
6686 && CP_TYPE_CONST_P (TREE_TYPE (r)))
6687 TREE_READONLY (init) = true;
8e007055 6688 ctx->global->values.put (r, init);
60813a46
JM
6689 }
6690 else if (ctx == &new_ctx)
6691 /* We gave it a CONSTRUCTOR above. */;
6692 else
8e007055 6693 ctx->global->values.put (r, NULL_TREE);
60813a46
JM
6694 }
6695 break;
6696
2d76680f 6697 case TARGET_EXPR:
595f1b12
JM
6698 {
6699 tree type = TREE_TYPE (t);
6700
6701 if (!literal_type_p (type))
6702 {
6703 if (!ctx->quiet)
6704 {
6705 auto_diagnostic_group d;
6706 error ("temporary of non-literal type %qT in a "
6707 "constant expression", type);
6708 explain_non_literal_class (type);
6709 }
6710 *non_constant_p = true;
6711 break;
6712 }
6713 gcc_checking_assert (!TARGET_EXPR_DIRECT_INIT_P (t));
6714 /* Avoid evaluating a TARGET_EXPR more than once. */
6715 tree slot = TARGET_EXPR_SLOT (t);
6716 if (tree *p = ctx->global->values.get (slot))
6717 {
6718 if (lval)
6719 return slot;
6720 r = *p;
6721 break;
6722 }
595f1b12
JM
6723 if ((AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type)))
6724 {
6725 /* We're being expanded without an explicit target, so start
6726 initializing a new object; expansion with an explicit target
6727 strips the TARGET_EXPR before we get here. */
6728 new_ctx = *ctx;
49a86fce
PP
6729 /* Link CTX to NEW_CTX so that lookup_placeholder can resolve
6730 any PLACEHOLDER_EXPR within the initializer that refers to the
6731 former object under construction. */
6732 new_ctx.parent = ctx;
595f1b12
JM
6733 new_ctx.ctor = build_constructor (type, NULL);
6734 CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
6735 new_ctx.object = slot;
6736 ctx->global->values.put (new_ctx.object, new_ctx.ctor);
6737 ctx = &new_ctx;
6738 }
6739 /* Pass false for 'lval' because this indicates
6740 initialization of a temporary. */
6741 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
6742 false,
6743 non_constant_p, overflow_p);
6744 if (*non_constant_p)
ee1de08d 6745 break;
595f1b12
JM
6746 /* Adjust the type of the result to the type of the temporary. */
6747 r = adjust_temp_type (type, r);
6748 if (TARGET_EXPR_CLEANUP (t) && !CLEANUP_EH_ONLY (t))
6749 ctx->global->cleanups->safe_push (TARGET_EXPR_CLEANUP (t));
6750 r = unshare_constructor (r);
6751 ctx->global->values.put (slot, r);
6752 if (ctx->save_exprs)
6753 ctx->save_exprs->safe_push (slot);
6754 if (lval)
6755 return slot;
6756 }
2d76680f
PC
6757 break;
6758
60813a46 6759 case INIT_EXPR:
60813a46 6760 case MODIFY_EXPR:
4b390698 6761 gcc_assert (jump_target == NULL || *jump_target == NULL_TREE);
92a596e8 6762 r = cxx_eval_store_expression (ctx, t, lval,
60813a46
JM
6763 non_constant_p, overflow_p);
6764 break;
6765
2d76680f 6766 case SCOPE_REF:
3e605b20 6767 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
92a596e8 6768 lval,
5a804683 6769 non_constant_p, overflow_p);
2d76680f
PC
6770 break;
6771
6772 case RETURN_EXPR:
75e0295b
MP
6773 if (TREE_OPERAND (t, 0) != NULL_TREE)
6774 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
6775 lval,
6776 non_constant_p, overflow_p);
43574e4f
JJ
6777 /* FALLTHRU */
6778 case BREAK_STMT:
6779 case CONTINUE_STMT:
019e0ae8
MP
6780 if (jump_target)
6781 *jump_target = t;
6782 else
6783 {
6784 /* Can happen with ({ return true; }) && false; passed to
6785 maybe_constant_value. There is nothing to jump over in this
6786 case, and the bug will be diagnosed later. */
6787 gcc_assert (ctx->quiet);
6788 *non_constant_p = true;
6789 }
56632b27
JM
6790 break;
6791
cabaf50a
JM
6792 case SAVE_EXPR:
6793 /* Avoid evaluating a SAVE_EXPR more than once. */
8e007055 6794 if (tree *p = ctx->global->values.get (t))
cabaf50a
JM
6795 r = *p;
6796 else
6797 {
12d9ce19 6798 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), false,
cabaf50a 6799 non_constant_p, overflow_p);
0e0ffbfc
JJ
6800 if (*non_constant_p)
6801 break;
8e007055 6802 ctx->global->values.put (t, r);
39dce2b7 6803 if (ctx->save_exprs)
0ee28590 6804 ctx->save_exprs->safe_push (t);
cabaf50a
JM
6805 }
6806 break;
6807
2d76680f 6808 case TRY_CATCH_EXPR:
ee1de08d
JJ
6809 if (TREE_OPERAND (t, 0) == NULL_TREE)
6810 {
6811 r = void_node;
6812 break;
6813 }
6814 /* FALLTHRU */
6815 case NON_LVALUE_EXPR:
769430b2 6816 case TRY_BLOCK:
2d76680f 6817 case MUST_NOT_THROW_EXPR:
60813a46
JM
6818 case EXPR_STMT:
6819 case EH_SPEC_BLOCK:
3e605b20 6820 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
92a596e8 6821 lval,
56632b27
JM
6822 non_constant_p, overflow_p,
6823 jump_target);
2d76680f
PC
6824 break;
6825
ee1de08d
JJ
6826 case CLEANUP_POINT_EXPR:
6827 {
6828 auto_vec<tree, 2> cleanups;
6829 vec<tree> *prev_cleanups = ctx->global->cleanups;
6830 ctx->global->cleanups = &cleanups;
6831 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
6832 lval,
6833 non_constant_p, overflow_p,
6834 jump_target);
6835 ctx->global->cleanups = prev_cleanups;
6836 unsigned int i;
6837 tree cleanup;
6838 /* Evaluate the cleanups. */
6839 FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup)
6840 cxx_eval_constant_expression (ctx, cleanup, false,
fc531c2e 6841 non_constant_p, overflow_p);
ee1de08d
JJ
6842 }
6843 break;
6844
769430b2
JM
6845 case TRY_FINALLY_EXPR:
6846 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
6847 non_constant_p, overflow_p,
6848 jump_target);
6849 if (!*non_constant_p)
6850 /* Also evaluate the cleanup. */
6851 cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), true,
fc531c2e 6852 non_constant_p, overflow_p);
769430b2
JM
6853 break;
6854
43574e4f 6855 case CLEANUP_STMT:
fc531c2e
JJ
6856 r = cxx_eval_constant_expression (ctx, CLEANUP_BODY (t), lval,
6857 non_constant_p, overflow_p,
6858 jump_target);
6859 if (!CLEANUP_EH_ONLY (t) && !*non_constant_p)
6860 {
6861 iloc_sentinel ils (loc);
6862 /* Also evaluate the cleanup. */
6863 cxx_eval_constant_expression (ctx, CLEANUP_EXPR (t), true,
6864 non_constant_p, overflow_p);
6865 }
43574e4f
JJ
6866 break;
6867
2d76680f
PC
6868 /* These differ from cxx_eval_unary_expression in that this doesn't
6869 check for a constant operand or result; an address can be
6870 constant without its operand being, and vice versa. */
cda0a029 6871 case MEM_REF:
2d76680f 6872 case INDIRECT_REF:
92a596e8 6873 r = cxx_eval_indirect_ref (ctx, t, lval,
2d76680f
PC
6874 non_constant_p, overflow_p);
6875 break;
6876
6877 case ADDR_EXPR:
6878 {
6879 tree oldop = TREE_OPERAND (t, 0);
3e605b20 6880 tree op = cxx_eval_constant_expression (ctx, oldop,
92a596e8 6881 /*lval*/true,
5a804683 6882 non_constant_p, overflow_p);
2d76680f
PC
6883 /* Don't VERIFY_CONSTANT here. */
6884 if (*non_constant_p)
6885 return t;
f2acb8ad 6886 gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR);
2d76680f
PC
6887 /* This function does more aggressive folding than fold itself. */
6888 r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
6889 if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
7a649ef5
JM
6890 {
6891 ggc_free (r);
6892 return t;
6893 }
2d76680f
PC
6894 break;
6895 }
6896
6897 case REALPART_EXPR:
6898 case IMAGPART_EXPR:
87713c6a
JJ
6899 if (lval)
6900 {
6901 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
6902 non_constant_p, overflow_p);
6903 if (r == error_mark_node)
6904 ;
6905 else if (r == TREE_OPERAND (t, 0))
6906 r = t;
6907 else
6908 r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), r);
6909 break;
6910 }
6911 /* FALLTHRU */
2d76680f
PC
6912 case CONJ_EXPR:
6913 case FIX_TRUNC_EXPR:
6914 case FLOAT_EXPR:
6915 case NEGATE_EXPR:
6916 case ABS_EXPR:
e28fadbc 6917 case ABSU_EXPR:
2d76680f
PC
6918 case BIT_NOT_EXPR:
6919 case TRUTH_NOT_EXPR:
6920 case FIXED_CONVERT_EXPR:
92a596e8 6921 r = cxx_eval_unary_expression (ctx, t, lval,
2d76680f
PC
6922 non_constant_p, overflow_p);
6923 break;
6924
6925 case SIZEOF_EXPR:
99e764a2
MP
6926 r = fold_sizeof_expr (t);
6927 /* In a template, fold_sizeof_expr may merely create a new SIZEOF_EXPR,
6928 which could lead to an infinite recursion. */
6929 if (TREE_CODE (r) != SIZEOF_EXPR)
6930 r = cxx_eval_constant_expression (ctx, r, lval,
6931 non_constant_p, overflow_p,
6932 jump_target);
6933 else
6934 {
6935 *non_constant_p = true;
6936 gcc_assert (ctx->quiet);
6937 }
6938
2d76680f
PC
6939 break;
6940
6941 case COMPOUND_EXPR:
6942 {
6943 /* check_return_expr sometimes wraps a TARGET_EXPR in a
6944 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
6945 introduced by build_call_a. */
6946 tree op0 = TREE_OPERAND (t, 0);
6947 tree op1 = TREE_OPERAND (t, 1);
6948 STRIP_NOPS (op1);
6949 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
6950 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
2b3ab879 6951 r = cxx_eval_constant_expression (ctx, op0,
92a596e8 6952 lval, non_constant_p, overflow_p,
56632b27 6953 jump_target);
2d76680f
PC
6954 else
6955 {
6956 /* Check that the LHS is constant and then discard it. */
2b3ab879 6957 cxx_eval_constant_expression (ctx, op0,
12d9ce19 6958 true, non_constant_p, overflow_p,
56632b27 6959 jump_target);
06ec22b7
JM
6960 if (*non_constant_p)
6961 return t;
2d76680f 6962 op1 = TREE_OPERAND (t, 1);
2b3ab879 6963 r = cxx_eval_constant_expression (ctx, op1,
92a596e8 6964 lval, non_constant_p, overflow_p,
56632b27 6965 jump_target);
2d76680f
PC
6966 }
6967 }
6968 break;
6969
6970 case POINTER_PLUS_EXPR:
1af4ebf5 6971 case POINTER_DIFF_EXPR:
2d76680f
PC
6972 case PLUS_EXPR:
6973 case MINUS_EXPR:
6974 case MULT_EXPR:
6975 case TRUNC_DIV_EXPR:
6976 case CEIL_DIV_EXPR:
6977 case FLOOR_DIV_EXPR:
6978 case ROUND_DIV_EXPR:
6979 case TRUNC_MOD_EXPR:
6980 case CEIL_MOD_EXPR:
6981 case ROUND_MOD_EXPR:
6982 case RDIV_EXPR:
6983 case EXACT_DIV_EXPR:
6984 case MIN_EXPR:
6985 case MAX_EXPR:
6986 case LSHIFT_EXPR:
6987 case RSHIFT_EXPR:
81fa6d73
JJ
6988 case LROTATE_EXPR:
6989 case RROTATE_EXPR:
2d76680f
PC
6990 case BIT_IOR_EXPR:
6991 case BIT_XOR_EXPR:
6992 case BIT_AND_EXPR:
6993 case TRUTH_XOR_EXPR:
6994 case LT_EXPR:
6995 case LE_EXPR:
6996 case GT_EXPR:
6997 case GE_EXPR:
6998 case EQ_EXPR:
6999 case NE_EXPR:
b7689b96 7000 case SPACESHIP_EXPR:
2d76680f
PC
7001 case UNORDERED_EXPR:
7002 case ORDERED_EXPR:
7003 case UNLT_EXPR:
7004 case UNLE_EXPR:
7005 case UNGT_EXPR:
7006 case UNGE_EXPR:
7007 case UNEQ_EXPR:
7008 case LTGT_EXPR:
7009 case RANGE_EXPR:
7010 case COMPLEX_EXPR:
92a596e8 7011 r = cxx_eval_binary_expression (ctx, t, lval,
2d76680f
PC
7012 non_constant_p, overflow_p);
7013 break;
7014
7015 /* fold can introduce non-IF versions of these; still treat them as
7016 short-circuiting. */
7017 case TRUTH_AND_EXPR:
7018 case TRUTH_ANDIF_EXPR:
3e605b20 7019 r = cxx_eval_logical_expression (ctx, t, boolean_false_node,
2d76680f 7020 boolean_true_node,
92a596e8 7021 lval,
2d76680f
PC
7022 non_constant_p, overflow_p);
7023 break;
7024
7025 case TRUTH_OR_EXPR:
7026 case TRUTH_ORIF_EXPR:
3e605b20 7027 r = cxx_eval_logical_expression (ctx, t, boolean_true_node,
2d76680f 7028 boolean_false_node,
92a596e8 7029 lval,
2d76680f
PC
7030 non_constant_p, overflow_p);
7031 break;
7032
7033 case ARRAY_REF:
92a596e8 7034 r = cxx_eval_array_reference (ctx, t, lval,
2d76680f
PC
7035 non_constant_p, overflow_p);
7036 break;
7037
7038 case COMPONENT_REF:
7039 if (is_overloaded_fn (t))
7040 {
7041 /* We can only get here in checking mode via
7042 build_non_dependent_expr, because any expression that
7043 calls or takes the address of the function will have
7044 pulled a FUNCTION_DECL out of the COMPONENT_REF. */
2b3ab879 7045 gcc_checking_assert (ctx->quiet || errorcount);
2d76680f
PC
7046 *non_constant_p = true;
7047 return t;
7048 }
92a596e8 7049 r = cxx_eval_component_reference (ctx, t, lval,
2d76680f
PC
7050 non_constant_p, overflow_p);
7051 break;
7052
7053 case BIT_FIELD_REF:
92a596e8 7054 r = cxx_eval_bit_field_ref (ctx, t, lval,
2d76680f
PC
7055 non_constant_p, overflow_p);
7056 break;
7057
7058 case COND_EXPR:
43574e4f 7059 case IF_STMT:
4b390698
JJ
7060 if (jump_target && *jump_target)
7061 {
e9fa2f6d 7062 tree orig_jump = *jump_target;
43574e4f
JJ
7063 tree arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 1))
7064 ? TREE_OPERAND (t, 1) : void_node);
4b390698
JJ
7065 /* When jumping to a label, the label might be either in the
7066 then or else blocks, so process then block first in skipping
7067 mode first, and if we are still in the skipping mode at its end,
7068 process the else block too. */
43574e4f
JJ
7069 r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p,
7070 overflow_p, jump_target);
e9fa2f6d
MP
7071 /* It's possible that we found the label in the then block. But
7072 it could have been followed by another jumping statement, e.g.
7073 say we're looking for case 1:
7074 if (cond)
7075 {
7076 // skipped statements
7077 case 1:; // clears up *jump_target
7078 return 1; // and sets it to a RETURN_EXPR
7079 }
7080 else { ... }
7081 in which case we need not go looking to the else block.
7082 (goto is not allowed in a constexpr function.) */
7083 if (*jump_target == orig_jump)
43574e4f
JJ
7084 {
7085 arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 2))
7086 ? TREE_OPERAND (t, 2) : void_node);
7087 r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p,
7088 overflow_p, jump_target);
7089 }
4b390698
JJ
7090 break;
7091 }
92a596e8 7092 r = cxx_eval_conditional_expression (ctx, t, lval,
56632b27
JM
7093 non_constant_p, overflow_p,
7094 jump_target);
2d76680f 7095 break;
f370e36d
JJ
7096 case VEC_COND_EXPR:
7097 r = cxx_eval_vector_conditional_expression (ctx, t, non_constant_p,
7098 overflow_p);
7099 break;
2d76680f
PC
7100
7101 case CONSTRUCTOR:
35d87f01 7102 if (TREE_CONSTANT (t) && reduced_constant_expression_p (t))
2d63bc39
JM
7103 {
7104 /* Don't re-process a constant CONSTRUCTOR, but do fold it to
7105 VECTOR_CST if applicable. */
ac9ec198 7106 verify_constructor_flags (t);
2d63bc39
JM
7107 if (TREE_CONSTANT (t))
7108 return fold (t);
7109 }
92a596e8 7110 r = cxx_eval_bare_aggregate (ctx, t, lval,
2d76680f
PC
7111 non_constant_p, overflow_p);
7112 break;
7113
7114 case VEC_INIT_EXPR:
7115 /* We can get this in a defaulted constructor for a class with a
7116 non-static data member of array type. Either the initializer will
7117 be NULL, meaning default-initialization, or it will be an lvalue
7118 or xvalue of the same type, meaning direct-initialization from the
7119 corresponding member. */
92a596e8 7120 r = cxx_eval_vec_init (ctx, t, lval,
2d76680f
PC
7121 non_constant_p, overflow_p);
7122 break;
7123
2d76680f 7124 case VEC_PERM_EXPR:
92a596e8 7125 r = cxx_eval_trinary_expression (ctx, t, lval,
2d76680f
PC
7126 non_constant_p, overflow_p);
7127 break;
efb7c510
MK
7128
7129 case PAREN_EXPR:
7130 gcc_assert (!REF_PARENTHESIZED_P (t));
7131 /* A PAREN_EXPR resulting from __builtin_assoc_barrier has no effect in
7132 constant expressions since it's unaffected by -fassociative-math. */
7133 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
7134 non_constant_p, overflow_p);
7135 break;
2d76680f 7136
7d75ea04
JJ
7137 case NOP_EXPR:
7138 if (REINTERPRET_CAST_P (t))
7139 {
7140 if (!ctx->quiet)
6821245b 7141 error_at (loc,
a9c697b8 7142 "%<reinterpret_cast%> is not a constant expression");
7d75ea04
JJ
7143 *non_constant_p = true;
7144 return t;
7145 }
7146 /* FALLTHROUGH. */
2d76680f
PC
7147 case CONVERT_EXPR:
7148 case VIEW_CONVERT_EXPR:
cda0a029 7149 case UNARY_PLUS_EXPR:
2d76680f
PC
7150 {
7151 tree oldop = TREE_OPERAND (t, 0);
cda0a029 7152
3e605b20 7153 tree op = cxx_eval_constant_expression (ctx, oldop,
92a596e8 7154 lval,
5a804683 7155 non_constant_p, overflow_p);
2d76680f
PC
7156 if (*non_constant_p)
7157 return t;
dcdbc004 7158 tree type = TREE_TYPE (t);
02a8575c
JM
7159
7160 if (VOID_TYPE_P (type))
7161 return void_node;
7162
eeb54a14
JJ
7163 if (TREE_CODE (t) == CONVERT_EXPR
7164 && ARITHMETIC_TYPE_P (type)
82bb6673
JJ
7165 && INDIRECT_TYPE_P (TREE_TYPE (op))
7166 && ctx->manifestly_const_eval)
eeb54a14
JJ
7167 {
7168 if (!ctx->quiet)
7169 error_at (loc,
7170 "conversion from pointer type %qT to arithmetic type "
7171 "%qT in a constant expression", TREE_TYPE (op), type);
7172 *non_constant_p = true;
7173 return t;
7174 }
7175
2ffc2645
MP
7176 /* [expr.const]: a conversion from type cv void* to a pointer-to-object
7177 type cannot be part of a core constant expression as a resolution to
7178 DR 1312. */
6252e35c 7179 if (TYPE_PTROB_P (type)
2ffc2645
MP
7180 && TYPE_PTR_P (TREE_TYPE (op))
7181 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (op)))
7182 /* Inside a call to std::construct_at or to
7183 std::allocator<T>::{,de}allocate, we permit casting from void*
7184 because that is compiler-generated code. */
7185 && !is_std_construct_at (ctx->call)
7186 && !is_std_allocator_allocate (ctx->call))
7187 {
7188 /* Likewise, don't error when casting from void* when OP is
7189 &heap uninit and similar. */
7190 tree sop = tree_strip_nop_conversions (op);
7191 if (TREE_CODE (sop) == ADDR_EXPR
7192 && VAR_P (TREE_OPERAND (sop, 0))
7193 && DECL_ARTIFICIAL (TREE_OPERAND (sop, 0)))
7194 /* OK */;
7195 else
7196 {
7197 if (!ctx->quiet)
7198 error_at (loc, "cast from %qT is not allowed",
7199 TREE_TYPE (op));
7200 *non_constant_p = true;
7201 return t;
7202 }
7203 }
7204
8e007055 7205 if (TREE_CODE (op) == PTRMEM_CST && !TYPE_PTRMEM_P (type))
dcdbc004 7206 op = cplus_expand_constant (op);
7d75ea04 7207
05bf54c3
MP
7208 if (TREE_CODE (op) == PTRMEM_CST && tcode == NOP_EXPR)
7209 {
7d75ea04
JJ
7210 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (op))
7211 && !can_convert_qual (type, op))
7212 op = cplus_expand_constant (op);
7213 return cp_fold_convert (type, op);
05bf54c3 7214 }
8bada5cd 7215
71a93b08 7216 if (INDIRECT_TYPE_P (type) && TREE_CODE (op) == INTEGER_CST)
2d76680f 7217 {
8bada5cd
MS
7218 if (integer_zerop (op))
7219 {
9f613f06 7220 if (TYPE_REF_P (type))
8bada5cd
MS
7221 {
7222 if (!ctx->quiet)
2ffc2645 7223 error_at (loc, "dereferencing a null pointer");
8bada5cd
MS
7224 *non_constant_p = true;
7225 return t;
7226 }
8bada5cd
MS
7227 }
7228 else
7229 {
7230 /* This detects for example:
7231 reinterpret_cast<void*>(sizeof 0)
7232 */
7233 if (!ctx->quiet)
6821245b 7234 error_at (loc, "%<reinterpret_cast<%T>(%E)%> is not "
64d6d399 7235 "a constant expression",
8bada5cd
MS
7236 type, op);
7237 *non_constant_p = true;
7238 return t;
7239 }
2d76680f 7240 }
7d75ea04 7241
8e007055
JJ
7242 if (INDIRECT_TYPE_P (type)
7243 && TREE_CODE (op) == NOP_EXPR
7244 && TREE_TYPE (op) == ptr_type_node
7245 && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR
7246 && VAR_P (TREE_OPERAND (TREE_OPERAND (op, 0), 0))
815baade
JJ
7247 && (DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0),
7248 0)) == heap_uninit_identifier
7249 || DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0),
7250 0)) == heap_vec_uninit_identifier))
8e007055
JJ
7251 {
7252 tree var = TREE_OPERAND (TREE_OPERAND (op, 0), 0);
7253 tree var_size = TYPE_SIZE_UNIT (TREE_TYPE (var));
7254 tree elt_type = TREE_TYPE (type);
7255 tree cookie_size = NULL_TREE;
7256 if (TREE_CODE (elt_type) == RECORD_TYPE
7257 && TYPE_NAME (elt_type) == heap_identifier)
7258 {
7259 tree fld1 = TYPE_FIELDS (elt_type);
7260 tree fld2 = DECL_CHAIN (fld1);
7261 elt_type = TREE_TYPE (TREE_TYPE (fld2));
7262 cookie_size = TYPE_SIZE_UNIT (TREE_TYPE (fld1));
7263 }
815baade
JJ
7264 DECL_NAME (var)
7265 = (DECL_NAME (var) == heap_uninit_identifier
7266 ? heap_identifier : heap_vec_identifier);
8e007055
JJ
7267 TREE_TYPE (var)
7268 = build_new_constexpr_heap_type (elt_type, cookie_size,
7269 var_size);
7270 TREE_TYPE (TREE_OPERAND (op, 0))
7271 = build_pointer_type (TREE_TYPE (var));
7272 }
7273
cda0a029 7274 if (op == oldop && tcode != UNARY_PLUS_EXPR)
2d76680f
PC
7275 /* We didn't fold at the top so we could check for ptr-int
7276 conversion. */
7277 return fold (t);
7d75ea04 7278
02a8575c
JM
7279 tree sop;
7280
e4511ca2
JM
7281 /* Handle an array's bounds having been deduced after we built
7282 the wrapping expression. */
7283 if (same_type_ignoring_tlq_and_bounds_p (type, TREE_TYPE (op)))
7284 r = op;
02a8575c
JM
7285 else if (sop = tree_strip_nop_conversions (op),
7286 sop != op && (same_type_ignoring_tlq_and_bounds_p
7287 (type, TREE_TYPE (sop))))
7288 r = sop;
e4511ca2 7289 else if (tcode == UNARY_PLUS_EXPR)
cda0a029
JM
7290 r = fold_convert (TREE_TYPE (t), op);
7291 else
7292 r = fold_build1 (tcode, type, op);
7d75ea04 7293
2d76680f
PC
7294 /* Conversion of an out-of-range value has implementation-defined
7295 behavior; the language considers it different from arithmetic
7296 overflow, which is undefined. */
7297 if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op))
7298 TREE_OVERFLOW (r) = false;
7299 }
7300 break;
7301
7302 case EMPTY_CLASS_EXPR:
46fdced6
PP
7303 /* Handle EMPTY_CLASS_EXPR produced by build_call_a by lowering
7304 it to an appropriate CONSTRUCTOR. */
7305 return build_constructor (TREE_TYPE (t), NULL);
2d76680f 7306
60813a46 7307 case STATEMENT_LIST:
56632b27
JM
7308 new_ctx = *ctx;
7309 new_ctx.ctor = new_ctx.object = NULL_TREE;
2b3ab879 7310 return cxx_eval_statement_list (&new_ctx, t,
56632b27 7311 non_constant_p, overflow_p, jump_target);
60813a46
JM
7312
7313 case BIND_EXPR:
7314 return cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t),
92a596e8 7315 lval,
56632b27
JM
7316 non_constant_p, overflow_p,
7317 jump_target);
60813a46 7318
2d76680f
PC
7319 case PREINCREMENT_EXPR:
7320 case POSTINCREMENT_EXPR:
7321 case PREDECREMENT_EXPR:
7322 case POSTDECREMENT_EXPR:
2b3ab879 7323 return cxx_eval_increment_expression (ctx, t,
92a596e8 7324 lval, non_constant_p, overflow_p);
60813a46
JM
7325
7326 case LAMBDA_EXPR:
2d76680f
PC
7327 case NEW_EXPR:
7328 case VEC_NEW_EXPR:
7329 case DELETE_EXPR:
7330 case VEC_DELETE_EXPR:
7331 case THROW_EXPR:
2d76680f
PC
7332 case MODOP_EXPR:
7333 /* GCC internal stuff. */
7334 case VA_ARG_EXPR:
2d76680f
PC
7335 case NON_DEPENDENT_EXPR:
7336 case BASELINK:
2d76680f 7337 case OFFSET_REF:
2b3ab879 7338 if (!ctx->quiet)
6821245b 7339 error_at (loc, "expression %qE is not a constant expression", t);
2d76680f
PC
7340 *non_constant_p = true;
7341 break;
7342
bf8d8309 7343 case OBJ_TYPE_REF:
7ece3bd8
JM
7344 /* Virtual function lookup. We don't need to do anything fancy. */
7345 return cxx_eval_constant_expression (ctx, OBJ_TYPE_REF_EXPR (t),
7346 lval, non_constant_p, overflow_p);
bf8d8309 7347
3e605b20 7348 case PLACEHOLDER_EXPR:
89262ec6
JM
7349 /* Use of the value or address of the current object. */
7350 if (tree ctor = lookup_placeholder (ctx, lval, TREE_TYPE (t)))
b599bf9d
PP
7351 {
7352 if (TREE_CODE (ctor) == CONSTRUCTOR)
7353 return ctor;
7354 else
7355 return cxx_eval_constant_expression (ctx, ctor, lval,
7356 non_constant_p, overflow_p);
7357 }
89262ec6
JM
7358 /* A placeholder without a referent. We can get here when
7359 checking whether NSDMIs are noexcept, or in massage_init_elt;
7360 just say it's non-constant for now. */
7361 gcc_assert (ctx->quiet);
7362 *non_constant_p = true;
3e605b20
JM
7363 break;
7364
06ec22b7
JM
7365 case EXIT_EXPR:
7366 {
7367 tree cond = TREE_OPERAND (t, 0);
7368 cond = cxx_eval_constant_expression (ctx, cond, /*lval*/false,
7369 non_constant_p, overflow_p);
7370 VERIFY_CONSTANT (cond);
7371 if (integer_nonzerop (cond))
7372 *jump_target = t;
7373 }
7374 break;
7375
60813a46 7376 case GOTO_EXPR:
8892d532
JJ
7377 if (breaks (&TREE_OPERAND (t, 0))
7378 || continues (&TREE_OPERAND (t, 0))
7379 /* Allow for jumping to a cdtor_label. */
7380 || returns (&TREE_OPERAND (t, 0)))
7381 *jump_target = TREE_OPERAND (t, 0);
7382 else
7383 {
7384 gcc_assert (cxx_dialect >= cxx23);
7385 if (!ctx->quiet)
7386 error_at (loc, "%<goto%> is not a constant expression");
7387 *non_constant_p = true;
7388 }
56632b27
JM
7389 break;
7390
60813a46 7391 case LOOP_EXPR:
43574e4f
JJ
7392 case DO_STMT:
7393 case WHILE_STMT:
7394 case FOR_STMT:
2b3ab879 7395 cxx_eval_loop_expr (ctx, t,
56632b27
JM
7396 non_constant_p, overflow_p, jump_target);
7397 break;
7398
60813a46 7399 case SWITCH_EXPR:
43574e4f 7400 case SWITCH_STMT:
2b3ab879 7401 cxx_eval_switch_expr (ctx, t,
56632b27 7402 non_constant_p, overflow_p, jump_target);
60813a46
JM
7403 break;
7404
971e17ff
AS
7405 case REQUIRES_EXPR:
7406 /* It's possible to get a requires-expression in a constant
7407 expression. For example:
7408
7409 template<typename T> concept bool C() {
7410 return requires (T t) { t; };
7411 }
7412
7413 template<typename T> requires !C<T>() void f(T);
7414
7415 Normalization leaves f with the associated constraint
7416 '!requires (T t) { ... }' which is not transformed into
7417 a constraint. */
7418 if (!processing_template_decl)
662ef5b5 7419 return evaluate_requires_expr (t);
971e17ff
AS
7420 else
7421 *non_constant_p = true;
7422 return t;
7423
98e09245 7424 case ANNOTATE_EXPR:
98e09245
JJ
7425 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
7426 lval,
7427 non_constant_p, overflow_p,
7428 jump_target);
7429 break;
7430
3664e317
JM
7431 case USING_STMT:
7432 r = void_node;
7433 break;
7434
cb57504a
JM
7435 case TEMPLATE_ID_EXPR:
7436 {
7437 /* We can evaluate template-id that refers to a concept only if
7438 the template arguments are non-dependent. */
861d4af8
AS
7439 tree id = unpack_concept_check (t);
7440 tree tmpl = TREE_OPERAND (id, 0);
7441 if (!concept_definition_p (tmpl))
cb57504a
JM
7442 internal_error ("unexpected template-id %qE", t);
7443
861d4af8
AS
7444 if (function_concept_p (tmpl))
7445 {
7446 if (!ctx->quiet)
7447 error_at (cp_expr_loc_or_input_loc (t),
7448 "function concept must be called");
7449 r = error_mark_node;
7450 break;
7451 }
7452
9329344a 7453 if (!value_dependent_expression_p (t)
6d1556ec 7454 && !uid_sensitive_constexpr_evaluation_p ())
662ef5b5 7455 r = evaluate_concept_check (t);
cb57504a
JM
7456 else
7457 *non_constant_p = true;
861d4af8
AS
7458
7459 break;
cb57504a
JM
7460 }
7461
529bc410
MP
7462 case ASM_EXPR:
7463 if (!ctx->quiet)
6821245b 7464 inline_asm_in_constexpr_error (loc);
529bc410
MP
7465 *non_constant_p = true;
7466 return t;
7467
896048cf 7468 case BIT_CAST_EXPR:
606f2af1
JJ
7469 if (lval)
7470 {
7471 if (!ctx->quiet)
7472 error_at (EXPR_LOCATION (t),
7473 "address of a call to %qs is not a constant expression",
7474 "__builtin_bit_cast");
7475 *non_constant_p = true;
7476 return t;
7477 }
896048cf
JJ
7478 r = cxx_eval_bit_cast (ctx, t, non_constant_p, overflow_p);
7479 break;
7480
2d76680f 7481 default:
c6e7c499
JM
7482 if (STATEMENT_CODE_P (TREE_CODE (t)))
7483 {
7484 /* This function doesn't know how to deal with pre-genericize
7485 statements; this can only happen with statement-expressions,
7486 so for now just fail. */
7487 if (!ctx->quiet)
7488 error_at (EXPR_LOCATION (t),
64d6d399 7489 "statement is not a constant expression");
c6e7c499
JM
7490 }
7491 else
7492 internal_error ("unexpected expression %qE of kind %s", t,
7493 get_tree_code_name (TREE_CODE (t)));
2d76680f
PC
7494 *non_constant_p = true;
7495 break;
7496 }
7497
7498 if (r == error_mark_node)
7499 *non_constant_p = true;
7500
7501 if (*non_constant_p)
7502 return t;
7503 else
7504 return r;
7505}
7506
e079dced
JM
7507/* P0859: A function is needed for constant evaluation if it is a constexpr
7508 function that is named by an expression ([basic.def.odr]) that is
7509 potentially constant evaluated.
7510
7511 So we need to instantiate any constexpr functions mentioned by the
7512 expression even if the definition isn't needed for evaluating the
7513 expression. */
7514
7515static tree
7516instantiate_cx_fn_r (tree *tp, int *walk_subtrees, void */*data*/)
7517{
7518 if (TREE_CODE (*tp) == FUNCTION_DECL
7519 && DECL_DECLARED_CONSTEXPR_P (*tp)
7520 && !DECL_INITIAL (*tp)
4fdda3ce 7521 && !trivial_fn_p (*tp)
f65a3299
PP
7522 && DECL_TEMPLOID_INSTANTIATION (*tp)
7523 && !uid_sensitive_constexpr_evaluation_p ())
e079dced
JM
7524 {
7525 ++function_depth;
7526 instantiate_decl (*tp, /*defer_ok*/false, /*expl_inst*/false);
7527 --function_depth;
7528 }
7529 else if (TREE_CODE (*tp) == CALL_EXPR
7530 || TREE_CODE (*tp) == AGGR_INIT_EXPR)
7531 {
7532 if (EXPR_HAS_LOCATION (*tp))
7533 input_location = EXPR_LOCATION (*tp);
7534 }
7535
7536 if (!EXPR_P (*tp))
7537 *walk_subtrees = 0;
7538
7539 return NULL_TREE;
7540}
8e007055 7541
e079dced
JM
7542static void
7543instantiate_constexpr_fns (tree t)
7544{
7545 location_t loc = input_location;
7546 cp_walk_tree_without_duplicates (&t, instantiate_cx_fn_r, NULL);
7547 input_location = loc;
7548}
7549
8e007055
JJ
7550/* Look for heap variables in the expression *TP. */
7551
7552static tree
7553find_heap_var_refs (tree *tp, int *walk_subtrees, void */*data*/)
7554{
7555 if (VAR_P (*tp)
7556 && (DECL_NAME (*tp) == heap_uninit_identifier
7557 || DECL_NAME (*tp) == heap_identifier
815baade
JJ
7558 || DECL_NAME (*tp) == heap_vec_uninit_identifier
7559 || DECL_NAME (*tp) == heap_vec_identifier
8e007055
JJ
7560 || DECL_NAME (*tp) == heap_deleted_identifier))
7561 return *tp;
7562
7563 if (TYPE_P (*tp))
7564 *walk_subtrees = 0;
7565 return NULL_TREE;
7566}
7567
f968ef9b
JJ
7568/* Find immediate function decls in *TP if any. */
7569
7570static tree
7571find_immediate_fndecl (tree *tp, int */*walk_subtrees*/, void */*data*/)
7572{
7573 if (TREE_CODE (*tp) == FUNCTION_DECL && DECL_IMMEDIATE_FUNCTION_P (*tp))
7574 return *tp;
4b2fda8b
JJ
7575 if (TREE_CODE (*tp) == PTRMEM_CST
7576 && TREE_CODE (PTRMEM_CST_MEMBER (*tp)) == FUNCTION_DECL
7577 && DECL_IMMEDIATE_FUNCTION_P (PTRMEM_CST_MEMBER (*tp)))
7578 return PTRMEM_CST_MEMBER (*tp);
f968ef9b
JJ
7579 return NULL_TREE;
7580}
7581
ab71d3fe
PP
7582/* T has TREE_CONSTANT set but has been deemed not a valid C++ constant
7583 expression. Return a version of T that has TREE_CONSTANT cleared. */
7584
7585static tree
7586mark_non_constant (tree t)
7587{
7588 gcc_checking_assert (TREE_CONSTANT (t));
7589
7590 /* This isn't actually constant, so unset TREE_CONSTANT.
7591 Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires
7592 it to be set if it is invariant address, even when it is not
7593 a valid C++ constant expression. Wrap it with a NOP_EXPR
7594 instead. */
7595 if (EXPR_P (t) && TREE_CODE (t) != ADDR_EXPR)
7596 t = copy_node (t);
7597 else if (TREE_CODE (t) == CONSTRUCTOR)
7598 t = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (t), t);
7599 else
7600 t = build_nop (TREE_TYPE (t), t);
7601 TREE_CONSTANT (t) = false;
7602 return t;
7603}
7604
e4082611 7605/* ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
d4accef3
JM
7606 STRICT has the same sense as for constant_value_1: true if we only allow
7607 conforming C++ constant expressions, or false if we want a constant value
7608 even if it doesn't conform.
13de99bc 7609 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
8e007055
JJ
7610 per P0595 even when ALLOW_NON_CONSTANT is true.
7611 CONSTEXPR_DTOR is true when evaluating the dtor of a constexpr variable.
7612 OBJECT must be non-NULL in that case. */
e4082611 7613
2d76680f 7614static tree
3e605b20 7615cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant,
e4082611 7616 bool strict = true,
13de99bc 7617 bool manifestly_const_eval = false,
8e007055 7618 bool constexpr_dtor = false,
f65a3299 7619 tree object = NULL_TREE)
2d76680f 7620{
8108ea30
JM
7621 auto_timevar time (TV_CONSTEXPR);
7622
2d76680f
PC
7623 bool non_constant_p = false;
7624 bool overflow_p = false;
39dce2b7 7625
d419e176
MP
7626 if (BRACE_ENCLOSED_INITIALIZER_P (t))
7627 {
7628 gcc_checking_assert (allow_non_constant);
7629 return t;
7630 }
7631
8e007055 7632 constexpr_global_ctx global_ctx;
49a86fce 7633 constexpr_ctx ctx = { &global_ctx, NULL, NULL, NULL, NULL, NULL, NULL,
8e007055 7634 allow_non_constant, strict,
f65a3299 7635 manifestly_const_eval || !allow_non_constant };
39dce2b7 7636
6641d6d3
JJ
7637 /* Turn off -frounding-math for manifestly constant evaluation. */
7638 warning_sentinel rm (flag_rounding_math, ctx.manifestly_const_eval);
3e605b20 7639 tree type = initialized_type (t);
3e605b20 7640 tree r = t;
f968ef9b 7641 bool is_consteval = false;
4d0c18c6 7642 if (VOID_TYPE_P (type))
8e007055
JJ
7643 {
7644 if (constexpr_dtor)
7645 /* Used for destructors of array elements. */
7646 type = TREE_TYPE (object);
7647 else
f968ef9b 7648 {
b04445d4 7649 if (cxx_dialect < cxx20)
f968ef9b
JJ
7650 return t;
7651 if (TREE_CODE (t) != CALL_EXPR && TREE_CODE (t) != AGGR_INIT_EXPR)
7652 return t;
7653 /* Calls to immediate functions returning void need to be
7654 evaluated. */
7655 tree fndecl = cp_get_callee_fndecl_nofold (t);
7656 if (fndecl == NULL_TREE || !DECL_IMMEDIATE_FUNCTION_P (fndecl))
7657 return t;
7658 else
7659 is_consteval = true;
7660 }
7661 }
b04445d4 7662 else if (cxx_dialect >= cxx20
f968ef9b
JJ
7663 && (TREE_CODE (t) == CALL_EXPR
7664 || TREE_CODE (t) == AGGR_INIT_EXPR
7665 || TREE_CODE (t) == TARGET_EXPR))
7666 {
861d4af8
AS
7667 /* For non-concept checks, determine if it is consteval. */
7668 if (!concept_check_p (t))
7669 {
7670 tree x = t;
7671 if (TREE_CODE (x) == TARGET_EXPR)
7672 x = TARGET_EXPR_INITIAL (x);
7673 tree fndecl = cp_get_callee_fndecl_nofold (x);
7674 if (fndecl && DECL_IMMEDIATE_FUNCTION_P (fndecl))
7675 is_consteval = true;
7676 }
8e007055 7677 }
3e605b20
JM
7678 if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
7679 {
7680 /* In C++14 an NSDMI can participate in aggregate initialization,
7681 and can refer to the address of the object being initialized, so
7682 we need to pass in the relevant VAR_DECL if we want to do the
7683 evaluation in a single pass. The evaluation will dynamically
7684 update ctx.values for the VAR_DECL. We use the same strategy
7685 for C++11 constexpr constructors that refer to the object being
7686 initialized. */
8e007055
JJ
7687 if (constexpr_dtor)
7688 {
7689 gcc_assert (object && VAR_P (object));
7690 gcc_assert (DECL_DECLARED_CONSTEXPR_P (object));
7691 gcc_assert (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object));
fce6467b
JJ
7692 if (error_operand_p (DECL_INITIAL (object)))
7693 return t;
8e007055
JJ
7694 ctx.ctor = unshare_expr (DECL_INITIAL (object));
7695 TREE_READONLY (ctx.ctor) = false;
7696 /* Temporarily force decl_really_constant_value to return false
7697 for it, we want to use ctx.ctor for the current value instead. */
7698 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = false;
7699 }
7700 else
7701 {
7702 ctx.ctor = build_constructor (type, NULL);
7703 CONSTRUCTOR_NO_CLEARING (ctx.ctor) = true;
7704 }
60813a46
JM
7705 if (!object)
7706 {
7707 if (TREE_CODE (t) == TARGET_EXPR)
7708 object = TARGET_EXPR_SLOT (t);
7709 else if (TREE_CODE (t) == AGGR_INIT_EXPR)
7710 object = AGGR_INIT_EXPR_SLOT (t);
7711 }
3e605b20
JM
7712 ctx.object = object;
7713 if (object)
7714 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7715 (type, TREE_TYPE (object)));
7716 if (object && DECL_P (object))
8e007055 7717 global_ctx.values.put (object, ctx.ctor);
3e605b20
JM
7718 if (TREE_CODE (r) == TARGET_EXPR)
7719 /* Avoid creating another CONSTRUCTOR when we expand the
7720 TARGET_EXPR. */
7721 r = TARGET_EXPR_INITIAL (r);
7722 }
7723
ee1de08d
JJ
7724 auto_vec<tree, 16> cleanups;
7725 global_ctx.cleanups = &cleanups;
7726
1595fe44
JM
7727 if (manifestly_const_eval)
7728 instantiate_constexpr_fns (r);
2b3ab879 7729 r = cxx_eval_constant_expression (&ctx, r,
5a804683 7730 false, &non_constant_p, &overflow_p);
2d76680f 7731
8e007055
JJ
7732 if (!constexpr_dtor)
7733 verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p);
7734 else
7735 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = true;
2d76680f 7736
ee1de08d
JJ
7737 unsigned int i;
7738 tree cleanup;
7739 /* Evaluate the cleanups. */
7740 FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup)
7741 cxx_eval_constant_expression (&ctx, cleanup, false,
7742 &non_constant_p, &overflow_p);
7743
023d89c7
JM
7744 /* Mutable logic is a bit tricky: we want to allow initialization of
7745 constexpr variables with mutable members, but we can't copy those
7746 members to another constexpr variable. */
8e007055 7747 if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_MUTABLE_POISON (r))
2d76680f 7748 {
2d76680f 7749 if (!allow_non_constant)
023d89c7
JM
7750 error ("%qE is not a constant expression because it refers to "
7751 "mutable subobjects of %qT", t, type);
2d76680f
PC
7752 non_constant_p = true;
7753 }
7754
8e007055 7755 if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_NO_CLEARING (r))
f64e0c02
JM
7756 {
7757 if (!allow_non_constant)
7758 error ("%qE is not a constant expression because it refers to "
7759 "an incompletely initialized variable", t);
7760 TREE_CONSTANT (r) = false;
7761 non_constant_p = true;
7762 }
7763
8e007055
JJ
7764 if (!global_ctx.heap_vars.is_empty ())
7765 {
7766 tree heap_var = cp_walk_tree_without_duplicates (&r, find_heap_var_refs,
7767 NULL);
7768 unsigned int i;
7769 if (heap_var)
7770 {
7771 if (!allow_non_constant && !non_constant_p)
7772 error_at (DECL_SOURCE_LOCATION (heap_var),
7773 "%qE is not a constant expression because it refers to "
7774 "a result of %<operator new%>", t);
7775 r = t;
7776 non_constant_p = true;
7777 }
7778 FOR_EACH_VEC_ELT (global_ctx.heap_vars, i, heap_var)
a8db7887
JJ
7779 {
7780 if (DECL_NAME (heap_var) != heap_deleted_identifier)
7781 {
7782 if (!allow_non_constant && !non_constant_p)
7783 error_at (DECL_SOURCE_LOCATION (heap_var),
7784 "%qE is not a constant expression because allocated "
7785 "storage has not been deallocated", t);
7786 r = t;
7787 non_constant_p = true;
7788 }
7789 varpool_node::get (heap_var)->remove ();
7790 }
8e007055
JJ
7791 }
7792
f968ef9b 7793 /* Check that immediate invocation does not return an expression referencing
7473b8a9
JJ
7794 any immediate function decls. */
7795 if (is_consteval || in_immediate_context ())
f968ef9b
JJ
7796 if (tree immediate_fndecl
7797 = cp_walk_tree_without_duplicates (&r, find_immediate_fndecl,
7798 NULL))
7799 {
7800 if (!allow_non_constant && !non_constant_p)
7801 error_at (cp_expr_loc_or_input_loc (t),
7802 "immediate evaluation returns address of immediate "
7803 "function %qD", immediate_fndecl);
7804 r = t;
7805 non_constant_p = true;
7806 }
7807
8895443a
JM
7808 if (non_constant_p)
7809 /* If we saw something bad, go back to our argument. The wrapping below is
7810 only for the cases of TREE_CONSTANT argument or overflow. */
7811 r = t;
2d76680f
PC
7812
7813 if (!non_constant_p && overflow_p)
7814 non_constant_p = true;
7815
e0804c9b 7816 /* Unshare the result. */
56cfb596 7817 bool should_unshare = true;
e0804c9b
JM
7818 if (r == t || (TREE_CODE (t) == TARGET_EXPR
7819 && TARGET_EXPR_INITIAL (t) == r))
56cfb596
PP
7820 should_unshare = false;
7821
2d76680f
PC
7822 if (non_constant_p && !allow_non_constant)
7823 return error_mark_node;
8e007055
JJ
7824 else if (constexpr_dtor)
7825 return r;
2d76680f 7826 else if (non_constant_p && TREE_CONSTANT (r))
ab71d3fe 7827 r = mark_non_constant (r);
5dab8b11 7828 else if (non_constant_p)
2d76680f
PC
7829 return t;
7830
56cfb596
PP
7831 if (should_unshare)
7832 r = unshare_expr (r);
7833
2d76680f
PC
7834 if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
7835 {
5dab8b11 7836 r = adjust_temp_type (type, r);
2d76680f
PC
7837 if (TREE_CODE (t) == TARGET_EXPR
7838 && TARGET_EXPR_INITIAL (t) == r)
7839 return t;
1a5145f1
JJ
7840 else if (TREE_CODE (t) == CONSTRUCTOR)
7841 ;
7842 else if (TREE_CODE (t) == TARGET_EXPR && TARGET_EXPR_CLEANUP (t))
7843 r = get_target_expr (r);
7844 else
2d76680f 7845 {
8e007055 7846 r = get_target_expr_sfinae (r, tf_warning_or_error | tf_no_cleanup);
2d76680f 7847 TREE_CONSTANT (r) = true;
2d76680f
PC
7848 }
7849 }
5dab8b11 7850
729f6881
JM
7851 /* Remember the original location if that wouldn't need a wrapper. */
7852 if (location_t loc = EXPR_LOCATION (t))
8d970817 7853 protected_set_expr_location (r, loc);
729f6881 7854
5dab8b11 7855 return r;
2d76680f
PC
7856}
7857
2d76680f
PC
7858/* If T represents a constant expression returns its reduced value.
7859 Otherwise return error_mark_node. If T is dependent, then
7860 return NULL. */
7861
7862tree
3e605b20 7863cxx_constant_value (tree t, tree decl)
2d76680f 7864{
8e007055
JJ
7865 return cxx_eval_outermost_constant_expr (t, false, true, true, false, decl);
7866}
7867
a8de8324
JM
7868/* As above, but respect SFINAE. */
7869
7870tree
7871cxx_constant_value_sfinae (tree t, tsubst_flags_t complain)
7872{
7873 bool sfinae = !(complain & tf_error);
7874 tree r = cxx_eval_outermost_constant_expr (t, sfinae, true, true);
7875 if (sfinae && !TREE_CONSTANT (r))
7876 r = error_mark_node;
7877 return r;
7878}
7879
8e007055
JJ
7880/* Like cxx_constant_value, but used for evaluation of constexpr destructors
7881 of constexpr variables. The actual initializer of DECL is not modified. */
7882
7883void
7884cxx_constant_dtor (tree t, tree decl)
7885{
7886 cxx_eval_outermost_constant_expr (t, false, true, true, true, decl);
2d76680f
PC
7887}
7888
cda0a029
JM
7889/* Helper routine for fold_simple function. Either return simplified
7890 expression T, otherwise NULL_TREE.
7891 In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold
7892 even if we are within template-declaration. So be careful on call, as in
7893 such case types can be undefined. */
7894
7895static tree
7896fold_simple_1 (tree t)
7897{
7898 tree op1;
7899 enum tree_code code = TREE_CODE (t);
7900
7901 switch (code)
7902 {
7903 case INTEGER_CST:
7904 case REAL_CST:
7905 case VECTOR_CST:
7906 case FIXED_CST:
7907 case COMPLEX_CST:
7908 return t;
7909
7910 case SIZEOF_EXPR:
7911 return fold_sizeof_expr (t);
7912
7913 case ABS_EXPR:
e28fadbc 7914 case ABSU_EXPR:
cda0a029
JM
7915 case CONJ_EXPR:
7916 case REALPART_EXPR:
7917 case IMAGPART_EXPR:
7918 case NEGATE_EXPR:
7919 case BIT_NOT_EXPR:
7920 case TRUTH_NOT_EXPR:
7921 case NOP_EXPR:
7922 case VIEW_CONVERT_EXPR:
7923 case CONVERT_EXPR:
7924 case FLOAT_EXPR:
7925 case FIX_TRUNC_EXPR:
7926 case FIXED_CONVERT_EXPR:
7927 case ADDR_SPACE_CONVERT_EXPR:
7928
7929 op1 = TREE_OPERAND (t, 0);
7930
7931 t = const_unop (code, TREE_TYPE (t), op1);
7932 if (!t)
7933 return NULL_TREE;
7934
7935 if (CONVERT_EXPR_CODE_P (code)
7936 && TREE_OVERFLOW_P (t) && !TREE_OVERFLOW_P (op1))
7937 TREE_OVERFLOW (t) = false;
7938 return t;
7939
7940 default:
7941 return NULL_TREE;
7942 }
7943}
7944
7945/* If T is a simple constant expression, returns its simplified value.
dba99244 7946 Otherwise returns T. In contrast to maybe_constant_value we
cda0a029
JM
7947 simplify only few operations on constant-expressions, and we don't
7948 try to simplify constexpressions. */
7949
7950tree
7951fold_simple (tree t)
7952{
cda0a029
JM
7953 if (processing_template_decl)
7954 return t;
7955
dba99244
MP
7956 tree r = fold_simple_1 (t);
7957 if (r)
7958 return r;
cda0a029 7959
dba99244 7960 return t;
cda0a029
JM
7961}
7962
2d76680f
PC
7963/* If T is a constant expression, returns its reduced value.
7964 Otherwise, if T does not have TREE_CONSTANT set, returns T.
13de99bc
JJ
7965 Otherwise, returns a version of T without TREE_CONSTANT.
7966 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated
f65a3299 7967 as per P0595. */
2d76680f 7968
8a87daca
JM
7969static GTY((deletable)) hash_map<tree, tree> *cv_cache;
7970
7971tree
f65a3299 7972maybe_constant_value (tree t, tree decl, bool manifestly_const_eval)
2d76680f
PC
7973{
7974 tree r;
7975
a0ab7ccd 7976 if (!is_nondependent_constant_expression (t))
2d76680f 7977 {
ab71d3fe
PP
7978 if (TREE_OVERFLOW_P (t)
7979 || (!processing_template_decl && TREE_CONSTANT (t)))
7980 t = mark_non_constant (t);
2d76680f
PC
7981 return t;
7982 }
8a87daca
JM
7983 else if (CONSTANT_CLASS_P (t))
7984 /* No caching or evaluation needed. */
7985 return t;
7986
13de99bc 7987 if (manifestly_const_eval)
f65a3299 7988 return cxx_eval_outermost_constant_expr (t, true, true, true, false, decl);
13de99bc 7989
8a87daca
JM
7990 if (cv_cache == NULL)
7991 cv_cache = hash_map<tree, tree>::create_ggc (101);
7992 if (tree *cached = cv_cache->get (t))
173c8def
JM
7993 {
7994 r = *cached;
7995 if (r != t)
7996 {
9f143008 7997 r = break_out_target_exprs (r, /*clear_loc*/true);
173c8def
JM
7998 protected_set_expr_location (r, EXPR_LOCATION (t));
7999 }
dfffecb8 8000 return r;
173c8def 8001 }
2d76680f 8002
4df7f8c7
JM
8003 /* Don't evaluate an unevaluated operand. */
8004 if (cp_unevaluated_operand)
8005 return t;
8006
f65a3299
PP
8007 uid_sensitive_constexpr_evaluation_checker c;
8008 r = cxx_eval_outermost_constant_expr (t, true, true, false, false, decl);
595278be
MM
8009 gcc_checking_assert (r == t
8010 || CONVERT_EXPR_P (t)
8011 || TREE_CODE (t) == VIEW_CONVERT_EXPR
8012 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
8013 || !cp_tree_equal (r, t));
f65a3299
PP
8014 if (!c.evaluation_restricted_p ())
8015 cv_cache->put (t, r);
2d76680f
PC
8016 return r;
8017}
8018
7a7ac32a
PP
8019/* Dispose of the whole CV_CACHE. */
8020
8021static void
8022clear_cv_cache (void)
8023{
8024 if (cv_cache != NULL)
8025 cv_cache->empty ();
8026}
8027
bebabf70 8028/* Dispose of the whole CV_CACHE and FOLD_CACHE. */
1e297006
MP
8029
8030void
bebabf70 8031clear_cv_and_fold_caches ()
1e297006 8032{
7a7ac32a 8033 clear_cv_cache ();
eba9e839 8034 clear_fold_cache ();
1e297006
MP
8035}
8036
a81c8e8c
MP
8037/* Internal function handling expressions in templates for
8038 fold_non_dependent_expr and fold_non_dependent_init.
8039
8040 If we're in a template, but T isn't value dependent, simplify
8041 it. We're supposed to treat:
8042
8043 template <typename T> void f(T[1 + 1]);
8044 template <typename T> void f(T[2]);
8045
8046 as two declarations of the same function, for example. */
8047
8048static tree
8049fold_non_dependent_expr_template (tree t, tsubst_flags_t complain,
f968ef9b
JJ
8050 bool manifestly_const_eval,
8051 tree object)
a81c8e8c
MP
8052{
8053 gcc_assert (processing_template_decl);
8054
8055 if (is_nondependent_constant_expression (t))
8056 {
8057 processing_template_decl_sentinel s;
8058 t = instantiate_non_dependent_expr_internal (t, complain);
8059
8060 if (type_unknown_p (t) || BRACE_ENCLOSED_INITIALIZER_P (t))
8061 {
8062 if (TREE_OVERFLOW_P (t))
8063 {
8064 t = build_nop (TREE_TYPE (t), t);
8065 TREE_CONSTANT (t) = false;
8066 }
8067 return t;
8068 }
8069
4df7f8c7
JM
8070 if (cp_unevaluated_operand && !manifestly_const_eval)
8071 return t;
8072
a81c8e8c
MP
8073 tree r = cxx_eval_outermost_constant_expr (t, true, true,
8074 manifestly_const_eval,
f968ef9b 8075 false, object);
a81c8e8c
MP
8076 /* cp_tree_equal looks through NOPs, so allow them. */
8077 gcc_checking_assert (r == t
8078 || CONVERT_EXPR_P (t)
8079 || TREE_CODE (t) == VIEW_CONVERT_EXPR
8080 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
8081 || !cp_tree_equal (r, t));
8082 return r;
8083 }
8084 else if (TREE_OVERFLOW_P (t))
8085 {
8086 t = build_nop (TREE_TYPE (t), t);
8087 TREE_CONSTANT (t) = false;
8088 }
8089
8090 return t;
8091}
8092
234bef96
PC
8093/* Like maybe_constant_value but first fully instantiate the argument.
8094
8095 Note: this is equivalent to instantiate_non_dependent_expr_sfinae
e56f6629
JM
8096 (t, complain) followed by maybe_constant_value but is more efficient,
8097 because it calls instantiation_dependent_expression_p and
8098 potential_constant_expression at most once.
4cd3e7df 8099 The manifestly_const_eval argument is passed to maybe_constant_value.
e56f6629
JM
8100
8101 Callers should generally pass their active complain, or if they are in a
8102 non-template, diagnosing context, they can use the default of
8103 tf_warning_or_error. Callers that might be within a template context, don't
8104 have a complain parameter, and aren't going to remember the result for long
8105 (e.g. null_ptr_cst_p), can pass tf_none and deal with error_mark_node
8106 appropriately. */
234bef96
PC
8107
8108tree
e56f6629 8109fold_non_dependent_expr (tree t,
4cd3e7df 8110 tsubst_flags_t complain /* = tf_warning_or_error */,
f968ef9b
JJ
8111 bool manifestly_const_eval /* = false */,
8112 tree object /* = NULL_TREE */)
234bef96
PC
8113{
8114 if (t == NULL_TREE)
8115 return NULL_TREE;
8116
a81c8e8c
MP
8117 if (processing_template_decl)
8118 return fold_non_dependent_expr_template (t, complain,
f968ef9b 8119 manifestly_const_eval, object);
234bef96 8120
f968ef9b 8121 return maybe_constant_value (t, object, manifestly_const_eval);
a81c8e8c 8122}
234bef96 8123
ae2ebf01
MP
8124/* Like fold_non_dependent_expr, but if EXPR couldn't be folded to a constant,
8125 return the original expression. */
8126
8127tree
8128maybe_fold_non_dependent_expr (tree expr,
8129 tsubst_flags_t complain/*=tf_warning_or_error*/)
8130{
8131 tree t = fold_non_dependent_expr (expr, complain);
8132 if (t && TREE_CONSTANT (t))
8133 return t;
8134
8135 return expr;
8136}
234bef96 8137
a81c8e8c 8138/* Like maybe_constant_init but first fully instantiate the argument. */
234bef96 8139
a81c8e8c
MP
8140tree
8141fold_non_dependent_init (tree t,
8142 tsubst_flags_t complain /*=tf_warning_or_error*/,
69bf1c7d
MP
8143 bool manifestly_const_eval /*=false*/,
8144 tree object /* = NULL_TREE */)
a81c8e8c
MP
8145{
8146 if (t == NULL_TREE)
8147 return NULL_TREE;
8148
8149 if (processing_template_decl)
8150 {
8151 t = fold_non_dependent_expr_template (t, complain,
69bf1c7d 8152 manifestly_const_eval, object);
a81c8e8c
MP
8153 /* maybe_constant_init does this stripping, so do it here too. */
8154 if (TREE_CODE (t) == TARGET_EXPR)
234bef96 8155 {
a81c8e8c
MP
8156 tree init = TARGET_EXPR_INITIAL (t);
8157 if (TREE_CODE (init) == CONSTRUCTOR)
8158 t = init;
234bef96
PC
8159 }
8160 return t;
8161 }
8162
69bf1c7d 8163 return maybe_constant_init (t, object, manifestly_const_eval);
234bef96
PC
8164}
8165
2d76680f 8166/* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
e4082611
JJ
8167 than wrapped in a TARGET_EXPR.
8168 ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
13de99bc 8169 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
e4082611 8170 per P0595 even when ALLOW_NON_CONSTANT is true. */
2d76680f 8171
118cd6ba 8172static tree
e4082611 8173maybe_constant_init_1 (tree t, tree decl, bool allow_non_constant,
13de99bc 8174 bool manifestly_const_eval)
2d76680f 8175{
cda0a029
JM
8176 if (!t)
8177 return t;
2d76680f
PC
8178 if (TREE_CODE (t) == EXPR_STMT)
8179 t = TREE_OPERAND (t, 0);
8180 if (TREE_CODE (t) == CONVERT_EXPR
8181 && VOID_TYPE_P (TREE_TYPE (t)))
8182 t = TREE_OPERAND (t, 0);
60813a46
JM
8183 if (TREE_CODE (t) == INIT_EXPR)
8184 t = TREE_OPERAND (t, 1);
f64e0c02
JM
8185 if (TREE_CODE (t) == TARGET_EXPR)
8186 t = TARGET_EXPR_INITIAL (t);
a0ab7ccd 8187 if (!is_nondependent_static_init_expression (t))
69eb4fde 8188 /* Don't try to evaluate it. */;
118cd6ba 8189 else if (CONSTANT_CLASS_P (t) && allow_non_constant)
8a87daca 8190 /* No evaluation needed. */;
69eb4fde 8191 else
e4082611 8192 t = cxx_eval_outermost_constant_expr (t, allow_non_constant,
38295573 8193 /*strict*/false,
8e007055 8194 manifestly_const_eval, false, decl);
2d76680f
PC
8195 if (TREE_CODE (t) == TARGET_EXPR)
8196 {
8197 tree init = TARGET_EXPR_INITIAL (t);
8198 if (TREE_CODE (init) == CONSTRUCTOR)
8199 t = init;
8200 }
8201 return t;
8202}
8203
118cd6ba
MP
8204/* Wrapper for maybe_constant_init_1 which permits non constants. */
8205
8206tree
13de99bc 8207maybe_constant_init (tree t, tree decl, bool manifestly_const_eval)
118cd6ba 8208{
13de99bc 8209 return maybe_constant_init_1 (t, decl, true, manifestly_const_eval);
118cd6ba
MP
8210}
8211
8212/* Wrapper for maybe_constant_init_1 which does not permit non constants. */
8213
8214tree
8215cxx_constant_init (tree t, tree decl)
8216{
e4082611 8217 return maybe_constant_init_1 (t, decl, false, true);
118cd6ba
MP
8218}
8219
2d76680f
PC
8220#if 0
8221/* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
8222/* Return true if the object referred to by REF has automatic or thread
8223 local storage. */
8224
8225enum { ck_ok, ck_bad, ck_unknown };
8226static int
8227check_automatic_or_tls (tree ref)
8228{
ef4bddc2 8229 machine_mode mode;
f37fac2b 8230 poly_int64 bitsize, bitpos;
2d76680f
PC
8231 tree offset;
8232 int volatilep = 0, unsignedp = 0;
8233 tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset,
8234 &mode, &unsignedp, &volatilep, false);
8235 duration_kind dk;
8236
8237 /* If there isn't a decl in the middle, we don't know the linkage here,
8238 and this isn't a constant expression anyway. */
8239 if (!DECL_P (decl))
8240 return ck_unknown;
8241 dk = decl_storage_duration (decl);
8242 return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok;
8243}
8244#endif
8245
c7a53bdb
JJ
8246/* Data structure for passing data from potential_constant_expression_1
8247 to check_for_return_continue via cp_walk_tree. */
8248struct check_for_return_continue_data {
8249 hash_set<tree> *pset;
8250 tree continue_stmt;
0fb7aa20 8251 tree break_stmt;
c7a53bdb
JJ
8252};
8253
8254/* Helper function for potential_constant_expression_1 SWITCH_STMT handling,
8255 called through cp_walk_tree. Return the first RETURN_EXPR found, or note
0fb7aa20 8256 the first CONTINUE_STMT and/or BREAK_STMT if RETURN_EXPR is not found. */
c7a53bdb
JJ
8257static tree
8258check_for_return_continue (tree *tp, int *walk_subtrees, void *data)
8259{
0fb7aa20 8260 tree t = *tp, s, b;
c7a53bdb
JJ
8261 check_for_return_continue_data *d = (check_for_return_continue_data *) data;
8262 switch (TREE_CODE (t))
8263 {
8264 case RETURN_EXPR:
8265 return t;
8266
8267 case CONTINUE_STMT:
8268 if (d->continue_stmt == NULL_TREE)
8269 d->continue_stmt = t;
8270 break;
8271
0fb7aa20
JJ
8272 case BREAK_STMT:
8273 if (d->break_stmt == NULL_TREE)
8274 d->break_stmt = t;
8275 break;
8276
c7a53bdb
JJ
8277#define RECUR(x) \
8278 if (tree r = cp_walk_tree (&x, check_for_return_continue, data, \
8279 d->pset)) \
8280 return r
8281
8282 /* For loops, walk subtrees manually, so that continue stmts found
8283 inside of the bodies of the loops are ignored. */
8284 case DO_STMT:
8285 *walk_subtrees = 0;
8286 RECUR (DO_COND (t));
8287 s = d->continue_stmt;
0fb7aa20 8288 b = d->break_stmt;
c7a53bdb
JJ
8289 RECUR (DO_BODY (t));
8290 d->continue_stmt = s;
0fb7aa20 8291 d->break_stmt = b;
c7a53bdb
JJ
8292 break;
8293
8294 case WHILE_STMT:
8295 *walk_subtrees = 0;
8296 RECUR (WHILE_COND (t));
8297 s = d->continue_stmt;
0fb7aa20 8298 b = d->break_stmt;
c7a53bdb
JJ
8299 RECUR (WHILE_BODY (t));
8300 d->continue_stmt = s;
0fb7aa20 8301 d->break_stmt = b;
c7a53bdb
JJ
8302 break;
8303
8304 case FOR_STMT:
8305 *walk_subtrees = 0;
8306 RECUR (FOR_INIT_STMT (t));
8307 RECUR (FOR_COND (t));
8308 RECUR (FOR_EXPR (t));
8309 s = d->continue_stmt;
0fb7aa20 8310 b = d->break_stmt;
c7a53bdb
JJ
8311 RECUR (FOR_BODY (t));
8312 d->continue_stmt = s;
0fb7aa20 8313 d->break_stmt = b;
c7a53bdb
JJ
8314 break;
8315
8316 case RANGE_FOR_STMT:
8317 *walk_subtrees = 0;
8318 RECUR (RANGE_FOR_EXPR (t));
8319 s = d->continue_stmt;
0fb7aa20 8320 b = d->break_stmt;
c7a53bdb
JJ
8321 RECUR (RANGE_FOR_BODY (t));
8322 d->continue_stmt = s;
0fb7aa20
JJ
8323 d->break_stmt = b;
8324 break;
8325
8326 case SWITCH_STMT:
8327 *walk_subtrees = 0;
8328 RECUR (SWITCH_STMT_COND (t));
8329 b = d->break_stmt;
8330 RECUR (SWITCH_STMT_BODY (t));
8331 d->break_stmt = b;
c7a53bdb
JJ
8332 break;
8333#undef RECUR
8334
8335 case STATEMENT_LIST:
8336 case CONSTRUCTOR:
8337 break;
8338
8339 default:
8340 if (!EXPR_P (t))
8341 *walk_subtrees = 0;
8342 break;
8343 }
8344
8345 return NULL_TREE;
8346}
8347
2d76680f
PC
8348/* Return true if T denotes a potentially constant expression. Issue
8349 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
a0ab7ccd
JM
8350 an lvalue-rvalue conversion is implied. If NOW is true, we want to
8351 consider the expression in the current context, independent of constexpr
8352 substitution.
2d76680f
PC
8353
8354 C++0x [expr.const] used to say
8355
8356 6 An expression is a potential constant expression if it is
8357 a constant expression where all occurrences of function
8358 parameters are replaced by arbitrary constant expressions
8359 of the appropriate type.
8360
8361 2 A conditional expression is a constant expression unless it
8362 involves one of the following as a potentially evaluated
8363 subexpression (3.2), but subexpressions of logical AND (5.14),
8364 logical OR (5.15), and conditional (5.16) operations that are
8365 not evaluated are not considered. */
8366
8367static bool
a0ab7ccd 8368potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
3075affd 8369 tsubst_flags_t flags, tree *jump_target)
2d76680f 8370{
a0ab7ccd 8371#define RECUR(T,RV) \
3075affd 8372 potential_constant_expression_1 ((T), (RV), strict, now, flags, jump_target)
a0ab7ccd 8373
2d76680f
PC
8374 enum { any = false, rval = true };
8375 int i;
8376 tree tmp;
8377
8378 if (t == error_mark_node)
8379 return false;
8380 if (t == NULL_TREE)
8381 return true;
f9d0ca40 8382 location_t loc = cp_expr_loc_or_input_loc (t);
3075affd
JM
8383
8384 if (*jump_target)
8385 /* If we are jumping, ignore everything. This is simpler than the
8386 cxx_eval_constant_expression handling because we only need to be
8387 conservatively correct, and we don't necessarily have a constant value
8388 available, so we don't bother with switch tracking. */
8389 return true;
8390
3c393a2c 8391 if (TREE_THIS_VOLATILE (t) && want_rval)
2d76680f
PC
8392 {
8393 if (flags & tf_error)
3c393a2c
MP
8394 error_at (loc, "lvalue-to-rvalue conversion of a volatile lvalue "
8395 "%qE with type %qT", t, TREE_TYPE (t));
2d76680f
PC
8396 return false;
8397 }
8398 if (CONSTANT_CLASS_P (t))
8399 return true;
7dc2b4a2
JM
8400 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED)
8401 && TREE_TYPE (t) == error_mark_node)
8402 return false;
2d76680f
PC
8403
8404 switch (TREE_CODE (t))
8405 {
8406 case FUNCTION_DECL:
8407 case BASELINK:
8408 case TEMPLATE_DECL:
8409 case OVERLOAD:
8410 case TEMPLATE_ID_EXPR:
8411 case LABEL_DECL:
60813a46 8412 case CASE_LABEL_EXPR:
81012684 8413 case PREDICT_EXPR:
2d76680f
PC
8414 case CONST_DECL:
8415 case SIZEOF_EXPR:
8416 case ALIGNOF_EXPR:
8417 case OFFSETOF_EXPR:
8418 case NOEXCEPT_EXPR:
8419 case TEMPLATE_PARM_INDEX:
8420 case TRAIT_EXPR:
8421 case IDENTIFIER_NODE:
8422 case USERDEF_LITERAL:
8423 /* We can see a FIELD_DECL in a pointer-to-member expression. */
8424 case FIELD_DECL:
cda0a029 8425 case RESULT_DECL:
2d76680f 8426 case USING_DECL:
60813a46 8427 case USING_STMT:
3e605b20 8428 case PLACEHOLDER_EXPR:
971e17ff 8429 case REQUIRES_EXPR:
98e5a19a 8430 case STATIC_ASSERT:
96a95ac1 8431 case DEBUG_BEGIN_STMT:
2d76680f
PC
8432 return true;
8433
3075affd
JM
8434 case RETURN_EXPR:
8435 if (!RECUR (TREE_OPERAND (t, 0), any))
8436 return false;
8437 /* FALLTHROUGH */
8438
8439 case BREAK_STMT:
8440 case CONTINUE_STMT:
8441 *jump_target = t;
8442 return true;
8443
a0ab7ccd 8444 case PARM_DECL:
8fda2c27 8445 if (now && want_rval)
a0ab7ccd 8446 {
8fda2c27 8447 tree type = TREE_TYPE (t);
9eb7d0d7 8448 if ((processing_template_decl && !COMPLETE_TYPE_P (type))
61466206 8449 || dependent_type_p (type)
8fda2c27
JM
8450 || is_really_empty_class (type, /*ignore_vptr*/false))
8451 /* An empty class has no data to read. */
8452 return true;
a0ab7ccd
JM
8453 if (flags & tf_error)
8454 error ("%qE is not a constant expression", t);
8455 return false;
8456 }
8457 return true;
8458
2d76680f
PC
8459 case AGGR_INIT_EXPR:
8460 case CALL_EXPR:
8461 /* -- an invocation of a function other than a constexpr function
8462 or a constexpr constructor. */
8463 {
8464 tree fun = get_function_named_in_call (t);
8465 const int nargs = call_expr_nargs (t);
8466 i = 0;
8467
60813a46
JM
8468 if (fun == NULL_TREE)
8469 {
44a845ca
MS
8470 /* Reset to allow the function to continue past the end
8471 of the block below. Otherwise return early. */
8472 bool bail = true;
8473
35228ac7
JJ
8474 if (TREE_CODE (t) == CALL_EXPR
8475 && CALL_EXPR_FN (t) == NULL_TREE)
8476 switch (CALL_EXPR_IFN (t))
8477 {
8478 /* These should be ignored, they are optimized away from
8479 constexpr functions. */
8480 case IFN_UBSAN_NULL:
8481 case IFN_UBSAN_BOUNDS:
8482 case IFN_UBSAN_VPTR:
81fea426 8483 case IFN_FALLTHROUGH:
35228ac7 8484 return true;
44a845ca
MS
8485
8486 case IFN_ADD_OVERFLOW:
8487 case IFN_SUB_OVERFLOW:
8488 case IFN_MUL_OVERFLOW:
e16f1cc7 8489 case IFN_LAUNDER:
d8fcab68 8490 case IFN_VEC_CONVERT:
44a845ca 8491 bail = false;
d8fcab68 8492 break;
44a845ca 8493
35228ac7
JJ
8494 default:
8495 break;
8496 }
44a845ca
MS
8497
8498 if (bail)
8499 {
8500 /* fold_call_expr can't do anything with IFN calls. */
8501 if (flags & tf_error)
e40b6fc7 8502 error_at (loc, "call to internal function %qE", t);
44a845ca
MS
8503 return false;
8504 }
60813a46 8505 }
44a845ca
MS
8506
8507 if (fun && is_overloaded_fn (fun))
2d76680f
PC
8508 {
8509 if (TREE_CODE (fun) == FUNCTION_DECL)
8510 {
8511 if (builtin_valid_in_constant_expr_p (fun))
8512 return true;
87c2080b 8513 if (!maybe_constexpr_fn (fun)
2d76680f
PC
8514 /* Allow any built-in function; if the expansion
8515 isn't constant, we'll deal with that then. */
8e007055 8516 && !fndecl_built_in_p (fun)
b04445d4 8517 /* In C++20, replaceable global allocation functions
8e007055 8518 are constant expressions. */
8412b939
JJ
8519 && (!cxx_replaceable_global_alloc_fn (fun)
8520 || TREE_CODE (t) != CALL_EXPR
8521 || (!CALL_FROM_NEW_OR_DELETE_P (t)
8522 && (current_function_decl == NULL_TREE
8523 || !is_std_allocator_allocate
8524 (current_function_decl))))
cf650568
JJ
8525 /* Allow placement new in std::construct_at. */
8526 && (!cxx_placement_new_fn (fun)
8412b939 8527 || TREE_CODE (t) != CALL_EXPR
cf650568 8528 || current_function_decl == NULL_TREE
22edf943
MP
8529 || !is_std_construct_at (current_function_decl))
8530 && !cxx_dynamic_cast_fn_p (fun))
2d76680f
PC
8531 {
8532 if (flags & tf_error)
8533 {
84fa214d 8534 error_at (loc, "call to non-%<constexpr%> function %qD",
e40b6fc7 8535 fun);
2d76680f
PC
8536 explain_invalid_constexpr_fn (fun);
8537 }
8538 return false;
8539 }
8540 /* A call to a non-static member function takes the address
8541 of the object as the first argument. But in a constant
8542 expression the address will be folded away, so look
8543 through it now. */
8544 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
8545 && !DECL_CONSTRUCTOR_P (fun))
8546 {
8547 tree x = get_nth_callarg (t, 0);
8548 if (is_this_parameter (x))
3e605b20 8549 return true;
a0ab7ccd
JM
8550 /* Don't require an immediately constant value, as
8551 constexpr substitution might not use the value. */
8552 bool sub_now = false;
8553 if (!potential_constant_expression_1 (x, rval, strict,
3075affd
JM
8554 sub_now, flags,
8555 jump_target))
2d76680f
PC
8556 return false;
8557 i = 1;
8558 }
8559 }
8560 else
8561 {
69eb4fde 8562 if (!RECUR (fun, true))
2d76680f
PC
8563 return false;
8564 fun = get_first_fn (fun);
8565 }
8566 /* Skip initial arguments to base constructors. */
8567 if (DECL_BASE_CONSTRUCTOR_P (fun))
8568 i = num_artificial_parms_for (fun);
8569 fun = DECL_ORIGIN (fun);
8570 }
44a845ca 8571 else if (fun)
2d76680f 8572 {
69eb4fde 8573 if (RECUR (fun, rval))
2d76680f
PC
8574 /* Might end up being a constant function pointer. */;
8575 else
8576 return false;
8577 }
8578 for (; i < nargs; ++i)
8579 {
8580 tree x = get_nth_callarg (t, i);
c3c29ba5
JM
8581 /* In a template, reference arguments haven't been converted to
8582 REFERENCE_TYPE and we might not even know if the parameter
8583 is a reference, so accept lvalue constants too. */
8584 bool rv = processing_template_decl ? any : rval;
a0ab7ccd
JM
8585 /* Don't require an immediately constant value, as constexpr
8586 substitution might not use the value of the argument. */
8587 bool sub_now = false;
8588 if (!potential_constant_expression_1 (x, rv, strict,
3075affd 8589 sub_now, flags, jump_target))
2d76680f
PC
8590 return false;
8591 }
8592 return true;
8593 }
8594
8595 case NON_LVALUE_EXPR:
8596 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
8597 -- an lvalue of integral type that refers to a non-volatile
8598 const variable or static data member initialized with
8599 constant expressions, or
8600
8601 -- an lvalue of literal type that refers to non-volatile
8602 object defined with constexpr, or that refers to a
8603 sub-object of such an object; */
69eb4fde 8604 return RECUR (TREE_OPERAND (t, 0), rval);
2d76680f
PC
8605
8606 case VAR_DECL:
70f40fea 8607 if (DECL_HAS_VALUE_EXPR_P (t))
c1051bf7 8608 {
68ad1bf7 8609 if (now && is_normal_capture_proxy (t))
c1051bf7
JM
8610 {
8611 /* -- in a lambda-expression, a reference to this or to a
8612 variable with automatic storage duration defined outside that
8613 lambda-expression, where the reference would be an
8614 odr-use. */
04b89192
JM
8615
8616 if (want_rval)
8617 /* Since we're doing an lvalue-rvalue conversion, this might
8618 not be an odr-use, so evaluate the variable directly. */
8619 return RECUR (DECL_CAPTURED_VARIABLE (t), rval);
8620
c1051bf7
JM
8621 if (flags & tf_error)
8622 {
8623 tree cap = DECL_CAPTURED_VARIABLE (t);
8624 error ("lambda capture of %qE is not a constant expression",
8625 cap);
04b89192 8626 if (decl_constant_var_p (cap))
c1051bf7
JM
8627 inform (input_location, "because it is used as a glvalue");
8628 }
8629 return false;
8630 }
c2856cee
MP
8631 /* Treat __PRETTY_FUNCTION__ inside a template function as
8632 potentially-constant. */
8633 else if (DECL_PRETTY_FUNCTION_P (t)
8634 && DECL_VALUE_EXPR (t) == error_mark_node)
8635 return true;
c1051bf7
JM
8636 return RECUR (DECL_VALUE_EXPR (t), rval);
8637 }
69eb4fde 8638 if (want_rval
98e5a19a 8639 && !var_in_maybe_constexpr_fn (t)
7dc2b4a2 8640 && !type_dependent_expression_p (t)
4b691b13 8641 && !decl_maybe_constant_var_p (t)
69eb4fde
JM
8642 && (strict
8643 || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t))
4b691b13
JM
8644 || (DECL_INITIAL (t)
8645 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t)))
7dc2b4a2 8646 && COMPLETE_TYPE_P (TREE_TYPE (t))
dbcd32f8 8647 && !is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
2d76680f
PC
8648 {
8649 if (flags & tf_error)
6821245b 8650 non_const_var_error (loc, t);
2d76680f
PC
8651 return false;
8652 }
8653 return true;
8654
8655 case NOP_EXPR:
ed3ea9f2
JJ
8656 if (REINTERPRET_CAST_P (t))
8657 {
8658 if (flags & tf_error)
a9c697b8 8659 error_at (loc, "%<reinterpret_cast%> is not a constant expression");
ed3ea9f2
JJ
8660 return false;
8661 }
8662 /* FALLTHRU */
2d76680f
PC
8663 case CONVERT_EXPR:
8664 case VIEW_CONVERT_EXPR:
8665 /* -- a reinterpret_cast. FIXME not implemented, and this rule
8666 may change to something more specific to type-punning (DR 1312). */
8667 {
8668 tree from = TREE_OPERAND (t, 0);
dfd7fdca
DM
8669 if (location_wrapper_p (t))
8670 return (RECUR (from, want_rval));
8671 if (INDIRECT_TYPE_P (TREE_TYPE (t)))
2d76680f 8672 {
dfd7fdca
DM
8673 STRIP_ANY_LOCATION_WRAPPER (from);
8674 if (TREE_CODE (from) == INTEGER_CST
8675 && !integer_zerop (from))
8676 {
8677 if (flags & tf_error)
a9c697b8
MS
8678 error_at (loc,
8679 "%<reinterpret_cast%> from integer to pointer");
dfd7fdca
DM
8680 return false;
8681 }
2d76680f 8682 }
69eb4fde 8683 return (RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR));
2d76680f
PC
8684 }
8685
be845b04
JJ
8686 case ADDRESSOF_EXPR:
8687 /* This is like ADDR_EXPR, except it won't form pointer-to-member. */
8688 t = TREE_OPERAND (t, 0);
8689 goto handle_addr_expr;
8690
2d76680f
PC
8691 case ADDR_EXPR:
8692 /* -- a unary operator & that is applied to an lvalue that
8693 designates an object with thread or automatic storage
8694 duration; */
8695 t = TREE_OPERAND (t, 0);
8696
8697 if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t))
8698 /* A pointer-to-member constant. */
8699 return true;
8700
be845b04 8701 handle_addr_expr:
2d76680f
PC
8702#if 0
8703 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
8704 any checking here, as we might dereference the pointer later. If
8705 we remove this code, also remove check_automatic_or_tls. */
8706 i = check_automatic_or_tls (t);
8707 if (i == ck_ok)
8708 return true;
8709 if (i == ck_bad)
8710 {
8711 if (flags & tf_error)
8712 error ("address-of an object %qE with thread local or "
8713 "automatic storage is not a constant expression", t);
8714 return false;
8715 }
8716#endif
69eb4fde 8717 return RECUR (t, any);
2d76680f
PC
8718
8719 case COMPONENT_REF:
2d76680f
PC
8720 case ARROW_EXPR:
8721 case OFFSET_REF:
8722 /* -- a class member access unless its postfix-expression is
8723 of literal type or of pointer to literal type. */
8724 /* This test would be redundant, as it follows from the
8725 postfix-expression being a potential constant expression. */
19dedccf
JM
8726 if (type_unknown_p (t))
8727 return true;
8fda2c27
JM
8728 if (is_overloaded_fn (t))
8729 /* In a template, a COMPONENT_REF of a function expresses ob.fn(),
8730 which uses ob as an lvalue. */
8731 want_rval = false;
8732 gcc_fallthrough ();
8733
8734 case REALPART_EXPR:
8735 case IMAGPART_EXPR:
8736 case BIT_FIELD_REF:
69eb4fde 8737 return RECUR (TREE_OPERAND (t, 0), want_rval);
2d76680f
PC
8738
8739 case EXPR_PACK_EXPANSION:
69eb4fde 8740 return RECUR (PACK_EXPANSION_PATTERN (t), want_rval);
2d76680f
PC
8741
8742 case INDIRECT_REF:
8743 {
8744 tree x = TREE_OPERAND (t, 0);
8745 STRIP_NOPS (x);
948d0d91 8746 if (is_this_parameter (x) && !is_capture_proxy (x))
2d76680f 8747 {
5d4e573b 8748 if (!var_in_maybe_constexpr_fn (x))
2d76680f
PC
8749 {
8750 if (flags & tf_error)
e40b6fc7 8751 error_at (loc, "use of %<this%> in a constant expression");
2d76680f
PC
8752 return false;
8753 }
2d76680f
PC
8754 return true;
8755 }
69eb4fde 8756 return RECUR (x, rval);
2d76680f
PC
8757 }
8758
60813a46 8759 case STATEMENT_LIST:
0f54cc9c
JM
8760 for (tree stmt : tsi_range (t))
8761 if (!RECUR (stmt, any))
8762 return false;
8763 return true;
60813a46
JM
8764
8765 case MODIFY_EXPR:
8766 if (cxx_dialect < cxx14)
8767 goto fail;
69eb4fde 8768 if (!RECUR (TREE_OPERAND (t, 0), any))
60813a46 8769 return false;
8e007055
JJ
8770 /* Just ignore clobbers. */
8771 if (TREE_CLOBBER_P (TREE_OPERAND (t, 1)))
8772 return true;
69eb4fde 8773 if (!RECUR (TREE_OPERAND (t, 1), rval))
60813a46
JM
8774 return false;
8775 return true;
8776
8777 case MODOP_EXPR:
8778 if (cxx_dialect < cxx14)
8779 goto fail;
69eb4fde 8780 if (!RECUR (TREE_OPERAND (t, 0), rval))
60813a46 8781 return false;
69eb4fde 8782 if (!RECUR (TREE_OPERAND (t, 2), rval))
60813a46
JM
8783 return false;
8784 return true;
8785
60813a46 8786 case DO_STMT:
69eb4fde 8787 if (!RECUR (DO_COND (t), rval))
60813a46 8788 return false;
69eb4fde 8789 if (!RECUR (DO_BODY (t), any))
60813a46 8790 return false;
3075affd
JM
8791 if (breaks (jump_target) || continues (jump_target))
8792 *jump_target = NULL_TREE;
60813a46
JM
8793 return true;
8794
8795 case FOR_STMT:
69eb4fde 8796 if (!RECUR (FOR_INIT_STMT (t), any))
60813a46 8797 return false;
8e9558f0
MP
8798 tmp = FOR_COND (t);
8799 if (!RECUR (tmp, rval))
60813a46 8800 return false;
8e9558f0
MP
8801 if (tmp)
8802 {
8803 if (!processing_template_decl)
8804 tmp = cxx_eval_outermost_constant_expr (tmp, true);
a385474c
MP
8805 /* If we couldn't evaluate the condition, it might not ever be
8806 true. */
8807 if (!integer_onep (tmp))
0fb7aa20
JJ
8808 {
8809 /* Before returning true, check if the for body can contain
8810 a return. */
8811 hash_set<tree> pset;
8812 check_for_return_continue_data data = { &pset, NULL_TREE,
8813 NULL_TREE };
8814 if (tree ret_expr
8815 = cp_walk_tree (&FOR_BODY (t), check_for_return_continue,
8816 &data, &pset))
8817 *jump_target = ret_expr;
8818 return true;
8819 }
8e9558f0 8820 }
69eb4fde 8821 if (!RECUR (FOR_EXPR (t), any))
60813a46 8822 return false;
69eb4fde 8823 if (!RECUR (FOR_BODY (t), any))
60813a46 8824 return false;
3075affd
JM
8825 if (breaks (jump_target) || continues (jump_target))
8826 *jump_target = NULL_TREE;
60813a46
JM
8827 return true;
8828
98e5a19a 8829 case RANGE_FOR_STMT:
8112667c
MP
8830 if (!RECUR (RANGE_FOR_INIT_STMT (t), any))
8831 return false;
98e5a19a
JM
8832 if (!RECUR (RANGE_FOR_EXPR (t), any))
8833 return false;
8834 if (!RECUR (RANGE_FOR_BODY (t), any))
8835 return false;
3075affd
JM
8836 if (breaks (jump_target) || continues (jump_target))
8837 *jump_target = NULL_TREE;
98e5a19a
JM
8838 return true;
8839
60813a46 8840 case WHILE_STMT:
8e9558f0
MP
8841 tmp = WHILE_COND (t);
8842 if (!RECUR (tmp, rval))
60813a46 8843 return false;
8e9558f0
MP
8844 if (!processing_template_decl)
8845 tmp = cxx_eval_outermost_constant_expr (tmp, true);
a385474c
MP
8846 /* If we couldn't evaluate the condition, it might not ever be true. */
8847 if (!integer_onep (tmp))
0fb7aa20
JJ
8848 {
8849 /* Before returning true, check if the while body can contain
8850 a return. */
8851 hash_set<tree> pset;
8852 check_for_return_continue_data data = { &pset, NULL_TREE,
8853 NULL_TREE };
8854 if (tree ret_expr
8855 = cp_walk_tree (&WHILE_BODY (t), check_for_return_continue,
8856 &data, &pset))
8857 *jump_target = ret_expr;
8858 return true;
8859 }
69eb4fde 8860 if (!RECUR (WHILE_BODY (t), any))
60813a46 8861 return false;
3075affd
JM
8862 if (breaks (jump_target) || continues (jump_target))
8863 *jump_target = NULL_TREE;
60813a46
JM
8864 return true;
8865
8866 case SWITCH_STMT:
69eb4fde 8867 if (!RECUR (SWITCH_STMT_COND (t), rval))
60813a46 8868 return false;
ce965730 8869 /* FIXME we don't check SWITCH_STMT_BODY currently, because even
c7a53bdb
JJ
8870 unreachable labels would be checked and it is enough if there is
8871 a single switch cond value for which it is a valid constant
8872 expression. We need to check if there are any RETURN_EXPRs
8873 or CONTINUE_STMTs inside of the body though, as in that case
8874 we need to set *jump_target. */
8875 else
8876 {
8877 hash_set<tree> pset;
0fb7aa20
JJ
8878 check_for_return_continue_data data = { &pset, NULL_TREE,
8879 NULL_TREE };
c7a53bdb
JJ
8880 if (tree ret_expr
8881 = cp_walk_tree (&SWITCH_STMT_BODY (t), check_for_return_continue,
8882 &data, &pset))
8883 /* The switch might return. */
8884 *jump_target = ret_expr;
8885 else if (data.continue_stmt)
8886 /* The switch can't return, but might continue. */
8887 *jump_target = data.continue_stmt;
8888 }
60813a46
JM
8889 return true;
8890
58611fb6 8891 case STMT_EXPR:
69eb4fde 8892 return RECUR (STMT_EXPR_STMT (t), rval);
58611fb6 8893
2d76680f 8894 case LAMBDA_EXPR:
899ac3b8
JM
8895 if (cxx_dialect >= cxx17)
8896 /* In C++17 lambdas can be constexpr, don't give up yet. */
8897 return true;
8898 else if (flags & tf_error)
8899 error_at (loc, "lambda-expression is not a constant expression "
8900 "before C++17");
8901 return false;
8902
2d76680f
PC
8903 case DYNAMIC_CAST_EXPR:
8904 case PSEUDO_DTOR_EXPR:
2d76680f
PC
8905 case NEW_EXPR:
8906 case VEC_NEW_EXPR:
8907 case DELETE_EXPR:
8908 case VEC_DELETE_EXPR:
8909 case THROW_EXPR:
e40b6fc7
JJ
8910 case OMP_PARALLEL:
8911 case OMP_TASK:
8912 case OMP_FOR:
897064e2 8913 case OMP_SIMD:
e40b6fc7
JJ
8914 case OMP_DISTRIBUTE:
8915 case OMP_TASKLOOP:
d81ab49d 8916 case OMP_LOOP:
e40b6fc7
JJ
8917 case OMP_TEAMS:
8918 case OMP_TARGET_DATA:
8919 case OMP_TARGET:
8920 case OMP_SECTIONS:
8921 case OMP_ORDERED:
8922 case OMP_CRITICAL:
8923 case OMP_SINGLE:
8924 case OMP_SECTION:
8925 case OMP_MASTER:
4b1e1434 8926 case OMP_MASKED:
e40b6fc7
JJ
8927 case OMP_TASKGROUP:
8928 case OMP_TARGET_UPDATE:
8929 case OMP_TARGET_ENTER_DATA:
8930 case OMP_TARGET_EXIT_DATA:
2d76680f
PC
8931 case OMP_ATOMIC:
8932 case OMP_ATOMIC_READ:
8933 case OMP_ATOMIC_CAPTURE_OLD:
8934 case OMP_ATOMIC_CAPTURE_NEW:
28567c40 8935 case OMP_DEPOBJ:
e40b6fc7
JJ
8936 case OACC_PARALLEL:
8937 case OACC_KERNELS:
62aee289 8938 case OACC_SERIAL:
e40b6fc7
JJ
8939 case OACC_DATA:
8940 case OACC_HOST_DATA:
8941 case OACC_LOOP:
8942 case OACC_CACHE:
8943 case OACC_DECLARE:
8944 case OACC_ENTER_DATA:
8945 case OACC_EXIT_DATA:
8946 case OACC_UPDATE:
2d76680f
PC
8947 /* GCC internal stuff. */
8948 case VA_ARG_EXPR:
2d76680f 8949 case TRANSACTION_EXPR:
81b6a6c5 8950 case AT_ENCODE_EXPR:
60813a46 8951 fail:
2d76680f 8952 if (flags & tf_error)
e40b6fc7 8953 error_at (loc, "expression %qE is not a constant expression", t);
2d76680f
PC
8954 return false;
8955
529bc410 8956 case ASM_EXPR:
7c814975
MP
8957 if (flags & tf_error)
8958 inline_asm_in_constexpr_error (loc);
8959 return false;
529bc410 8960
bf8d8309 8961 case OBJ_TYPE_REF:
b04445d4
JM
8962 if (cxx_dialect >= cxx20)
8963 /* In C++20 virtual calls can be constexpr, don't give up yet. */
bf8d8309
MP
8964 return true;
8965 else if (flags & tf_error)
a9c697b8 8966 error_at (loc,
b04445d4 8967 "virtual functions cannot be %<constexpr%> before C++20");
bf8d8309
MP
8968 return false;
8969
2d76680f 8970 case TYPEID_EXPR:
66acfb80
MP
8971 /* In C++20, a typeid expression whose operand is of polymorphic
8972 class type can be constexpr. */
2d76680f
PC
8973 {
8974 tree e = TREE_OPERAND (t, 0);
b04445d4 8975 if (cxx_dialect < cxx20
66acfb80
MP
8976 && strict
8977 && !TYPE_P (e)
8978 && !type_dependent_expression_p (e)
2d76680f
PC
8979 && TYPE_POLYMORPHIC_P (TREE_TYPE (e)))
8980 {
8981 if (flags & tf_error)
a9c697b8 8982 error_at (loc, "%<typeid%> is not a constant expression "
e40b6fc7 8983 "because %qE is of polymorphic type", e);
2d76680f
PC
8984 return false;
8985 }
8986 return true;
8987 }
8988
1af4ebf5 8989 case POINTER_DIFF_EXPR:
2d76680f 8990 case MINUS_EXPR:
2d76680f
PC
8991 want_rval = true;
8992 goto binary;
8993
8994 case LT_EXPR:
8995 case LE_EXPR:
8996 case GT_EXPR:
8997 case GE_EXPR:
8998 case EQ_EXPR:
8999 case NE_EXPR:
b7689b96 9000 case SPACESHIP_EXPR:
2d76680f
PC
9001 want_rval = true;
9002 goto binary;
9003
60813a46
JM
9004 case PREINCREMENT_EXPR:
9005 case POSTINCREMENT_EXPR:
9006 case PREDECREMENT_EXPR:
9007 case POSTDECREMENT_EXPR:
9008 if (cxx_dialect < cxx14)
9009 goto fail;
9010 goto unary;
9011
2d76680f
PC
9012 case BIT_NOT_EXPR:
9013 /* A destructor. */
9014 if (TYPE_P (TREE_OPERAND (t, 0)))
9015 return true;
191816a3 9016 /* fall through. */
2d76680f 9017
2d76680f
PC
9018 case CONJ_EXPR:
9019 case SAVE_EXPR:
9020 case FIX_TRUNC_EXPR:
9021 case FLOAT_EXPR:
9022 case NEGATE_EXPR:
9023 case ABS_EXPR:
e197e64e 9024 case ABSU_EXPR:
2d76680f
PC
9025 case TRUTH_NOT_EXPR:
9026 case FIXED_CONVERT_EXPR:
9027 case UNARY_PLUS_EXPR:
4856a1f0
PC
9028 case UNARY_LEFT_FOLD_EXPR:
9029 case UNARY_RIGHT_FOLD_EXPR:
60813a46 9030 unary:
69eb4fde 9031 return RECUR (TREE_OPERAND (t, 0), rval);
2d76680f
PC
9032
9033 case CAST_EXPR:
9034 case CONST_CAST_EXPR:
9035 case STATIC_CAST_EXPR:
9036 case REINTERPRET_CAST_EXPR:
9037 case IMPLICIT_CONV_EXPR:
9038 if (cxx_dialect < cxx11
9039 && !dependent_type_p (TREE_TYPE (t))
9040 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t)))
9041 /* In C++98, a conversion to non-integral type can't be part of a
9042 constant expression. */
9043 {
9044 if (flags & tf_error)
e40b6fc7
JJ
9045 error_at (loc,
9046 "cast to non-integral type %qT in a constant expression",
9047 TREE_TYPE (t));
2d76680f
PC
9048 return false;
9049 }
fe0604d3
MP
9050 /* This might be a conversion from a class to a (potentially) literal
9051 type. Let's consider it potentially constant since the conversion
9052 might be a constexpr user-defined conversion. */
9053 else if (cxx_dialect >= cxx11
9054 && (dependent_type_p (TREE_TYPE (t))
9055 || !COMPLETE_TYPE_P (TREE_TYPE (t))
9056 || literal_type_p (TREE_TYPE (t)))
9057 && TREE_OPERAND (t, 0))
9058 {
9059 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
9060 /* If this is a dependent type, it could end up being a class
9061 with conversions. */
9062 if (type == NULL_TREE || WILDCARD_TYPE_P (type))
9063 return true;
9064 /* Or a non-dependent class which has conversions. */
9065 else if (CLASS_TYPE_P (type)
9066 && (TYPE_HAS_CONVERSION (type) || dependent_scope_p (type)))
9067 return true;
9068 }
2d76680f 9069
69eb4fde 9070 return (RECUR (TREE_OPERAND (t, 0),
9f613f06 9071 !TYPE_REF_P (TREE_TYPE (t))));
2d76680f 9072
60813a46 9073 case BIND_EXPR:
69eb4fde 9074 return RECUR (BIND_EXPR_BODY (t), want_rval);
60813a46 9075
c19f317a
PP
9076 case NON_DEPENDENT_EXPR:
9077 /* Treat NON_DEPENDENT_EXPR as non-constant: it's not handled by
9078 constexpr evaluation or tsubst, so fold_non_dependent_expr can't
9079 do anything useful with it. And we shouldn't see it in a context
9080 where a constant expression is strictly required, hence the assert. */
9081 gcc_checking_assert (!(flags & tf_error));
9082 return false;
9083
60813a46
JM
9084 case CLEANUP_POINT_EXPR:
9085 case MUST_NOT_THROW_EXPR:
9086 case TRY_CATCH_EXPR:
769430b2 9087 case TRY_BLOCK:
60813a46
JM
9088 case EH_SPEC_BLOCK:
9089 case EXPR_STMT:
2d76680f 9090 case PAREN_EXPR:
2d76680f 9091 /* For convenience. */
f937929e
JM
9092 case LOOP_EXPR:
9093 case EXIT_EXPR:
69eb4fde 9094 return RECUR (TREE_OPERAND (t, 0), want_rval);
2d76680f 9095
98e5a19a
JM
9096 case DECL_EXPR:
9097 tmp = DECL_EXPR_DECL (t);
9098 if (VAR_P (tmp) && !DECL_ARTIFICIAL (tmp))
9099 {
8892d532 9100 if (CP_DECL_THREAD_LOCAL_P (tmp))
98e5a19a
JM
9101 {
9102 if (flags & tf_error)
9103 error_at (DECL_SOURCE_LOCATION (tmp), "%qD declared "
8892d532 9104 "%<thread_local%> in %<constexpr%> context", tmp);
98e5a19a
JM
9105 return false;
9106 }
8892d532 9107 else if (TREE_STATIC (tmp))
98e5a19a
JM
9108 {
9109 if (flags & tf_error)
9110 error_at (DECL_SOURCE_LOCATION (tmp), "%qD declared "
8892d532 9111 "%<static%> in %<constexpr%> context", tmp);
98e5a19a
JM
9112 return false;
9113 }
3885527d
PC
9114 else if (!check_for_uninitialized_const_var
9115 (tmp, /*constexpr_context_p=*/true, flags))
9116 return false;
98e5a19a
JM
9117 }
9118 return RECUR (tmp, want_rval);
9119
769430b2
JM
9120 case TRY_FINALLY_EXPR:
9121 return (RECUR (TREE_OPERAND (t, 0), want_rval)
9122 && RECUR (TREE_OPERAND (t, 1), any));
9123
2d76680f 9124 case SCOPE_REF:
69eb4fde 9125 return RECUR (TREE_OPERAND (t, 1), want_rval);
2d76680f
PC
9126
9127 case TARGET_EXPR:
d3a9902e
JM
9128 if (!TARGET_EXPR_DIRECT_INIT_P (t)
9129 && !literal_type_p (TREE_TYPE (t)))
2d76680f
PC
9130 {
9131 if (flags & tf_error)
9132 {
097f82ec 9133 auto_diagnostic_group d;
e40b6fc7
JJ
9134 error_at (loc, "temporary of non-literal type %qT in a "
9135 "constant expression", TREE_TYPE (t));
2d76680f
PC
9136 explain_non_literal_class (TREE_TYPE (t));
9137 }
9138 return false;
9139 }
191816a3 9140 /* FALLTHRU */
2d76680f 9141 case INIT_EXPR:
69eb4fde 9142 return RECUR (TREE_OPERAND (t, 1), rval);
2d76680f
PC
9143
9144 case CONSTRUCTOR:
9145 {
9146 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
9147 constructor_elt *ce;
9148 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
69eb4fde 9149 if (!RECUR (ce->value, want_rval))
2d76680f
PC
9150 return false;
9151 return true;
9152 }
9153
9154 case TREE_LIST:
9155 {
9156 gcc_assert (TREE_PURPOSE (t) == NULL_TREE
9157 || DECL_P (TREE_PURPOSE (t)));
69eb4fde 9158 if (!RECUR (TREE_VALUE (t), want_rval))
2d76680f
PC
9159 return false;
9160 if (TREE_CHAIN (t) == NULL_TREE)
9161 return true;
69eb4fde 9162 return RECUR (TREE_CHAIN (t), want_rval);
2d76680f
PC
9163 }
9164
9165 case TRUNC_DIV_EXPR:
9166 case CEIL_DIV_EXPR:
9167 case FLOOR_DIV_EXPR:
9168 case ROUND_DIV_EXPR:
9169 case TRUNC_MOD_EXPR:
9170 case CEIL_MOD_EXPR:
9171 case ROUND_MOD_EXPR:
9172 {
9173 tree denom = TREE_OPERAND (t, 1);
69eb4fde 9174 if (!RECUR (denom, rval))
2d76680f
PC
9175 return false;
9176 /* We can't call cxx_eval_outermost_constant_expr on an expression
234bef96 9177 that hasn't been through instantiate_non_dependent_expr yet. */
2d76680f
PC
9178 if (!processing_template_decl)
9179 denom = cxx_eval_outermost_constant_expr (denom, true);
9180 if (integer_zerop (denom))
9181 {
9182 if (flags & tf_error)
64d6d399 9183 error ("division by zero is not a constant expression");
2d76680f
PC
9184 return false;
9185 }
9186 else
9187 {
9188 want_rval = true;
69eb4fde 9189 return RECUR (TREE_OPERAND (t, 0), want_rval);
2d76680f
PC
9190 }
9191 }
9192
9193 case COMPOUND_EXPR:
9194 {
9195 /* check_return_expr sometimes wraps a TARGET_EXPR in a
46fdced6 9196 COMPOUND_EXPR; don't get confused. */
2d76680f
PC
9197 tree op0 = TREE_OPERAND (t, 0);
9198 tree op1 = TREE_OPERAND (t, 1);
9199 STRIP_NOPS (op1);
46fdced6 9200 if (TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
69eb4fde 9201 return RECUR (op0, want_rval);
2d76680f
PC
9202 else
9203 goto binary;
9204 }
9205
9206 /* If the first operand is the non-short-circuit constant, look at
9207 the second operand; otherwise we only care about the first one for
9208 potentiality. */
9209 case TRUTH_AND_EXPR:
9210 case TRUTH_ANDIF_EXPR:
9211 tmp = boolean_true_node;
9212 goto truth;
9213 case TRUTH_OR_EXPR:
9214 case TRUTH_ORIF_EXPR:
9215 tmp = boolean_false_node;
9216 truth:
9217 {
9927ecbb
PP
9218 tree op0 = TREE_OPERAND (t, 0);
9219 tree op1 = TREE_OPERAND (t, 1);
9220 if (!RECUR (op0, rval))
2d76680f 9221 return false;
9927ecbb
PP
9222 if (!(flags & tf_error) && RECUR (op1, rval))
9223 /* When quiet, try to avoid expensive trial evaluation by first
9224 checking potentiality of the second operand. */
9225 return true;
2d76680f 9226 if (!processing_template_decl)
9927ecbb
PP
9227 op0 = cxx_eval_outermost_constant_expr (op0, true);
9228 if (tree_int_cst_equal (op0, tmp))
9229 return (flags & tf_error) ? RECUR (op1, rval) : false;
2d76680f
PC
9230 else
9231 return true;
9232 }
9233
9234 case PLUS_EXPR:
9235 case MULT_EXPR:
9236 case POINTER_PLUS_EXPR:
9237 case RDIV_EXPR:
9238 case EXACT_DIV_EXPR:
9239 case MIN_EXPR:
9240 case MAX_EXPR:
9241 case LSHIFT_EXPR:
9242 case RSHIFT_EXPR:
81fa6d73
JJ
9243 case LROTATE_EXPR:
9244 case RROTATE_EXPR:
2d76680f
PC
9245 case BIT_IOR_EXPR:
9246 case BIT_XOR_EXPR:
9247 case BIT_AND_EXPR:
9248 case TRUTH_XOR_EXPR:
9249 case UNORDERED_EXPR:
9250 case ORDERED_EXPR:
9251 case UNLT_EXPR:
9252 case UNLE_EXPR:
9253 case UNGT_EXPR:
9254 case UNGE_EXPR:
9255 case UNEQ_EXPR:
9256 case LTGT_EXPR:
9257 case RANGE_EXPR:
9258 case COMPLEX_EXPR:
9259 want_rval = true;
9260 /* Fall through. */
9261 case ARRAY_REF:
9262 case ARRAY_RANGE_REF:
9263 case MEMBER_REF:
9264 case DOTSTAR_EXPR:
41b38772 9265 case MEM_REF:
4856a1f0
PC
9266 case BINARY_LEFT_FOLD_EXPR:
9267 case BINARY_RIGHT_FOLD_EXPR:
2d76680f
PC
9268 binary:
9269 for (i = 0; i < 2; ++i)
69eb4fde 9270 if (!RECUR (TREE_OPERAND (t, i), want_rval))
2d76680f
PC
9271 return false;
9272 return true;
9273
2d76680f
PC
9274 case VEC_PERM_EXPR:
9275 for (i = 0; i < 3; ++i)
69eb4fde 9276 if (!RECUR (TREE_OPERAND (t, i), true))
2d76680f
PC
9277 return false;
9278 return true;
9279
9280 case COND_EXPR:
b04445d4 9281 if (COND_EXPR_IS_VEC_DELETE (t) && cxx_dialect < cxx20)
d5bcd6d4
JM
9282 {
9283 if (flags & tf_error)
e40b6fc7 9284 error_at (loc, "%<delete[]%> is not a constant expression");
d5bcd6d4
JM
9285 return false;
9286 }
9287 /* Fall through. */
9288 case IF_STMT:
2d76680f
PC
9289 case VEC_COND_EXPR:
9290 /* If the condition is a known constant, we know which of the legs we
9291 care about; otherwise we only require that the condition and
9292 either of the legs be potentially constant. */
9293 tmp = TREE_OPERAND (t, 0);
69eb4fde 9294 if (!RECUR (tmp, rval))
2d76680f
PC
9295 return false;
9296 if (!processing_template_decl)
9297 tmp = cxx_eval_outermost_constant_expr (tmp, true);
117c6426
JJ
9298 /* potential_constant_expression* isn't told if it is called for
9299 manifestly_const_eval or not, so for consteval if always
9300 process both branches as if the condition is not a known
9301 constant. */
9302 if (TREE_CODE (t) != IF_STMT || !IF_STMT_CONSTEVAL_P (t))
9303 {
9304 if (integer_zerop (tmp))
9305 return RECUR (TREE_OPERAND (t, 2), want_rval);
9306 else if (TREE_CODE (tmp) == INTEGER_CST)
9307 return RECUR (TREE_OPERAND (t, 1), want_rval);
9308 }
0fb7aa20 9309 tmp = *jump_target;
2d76680f 9310 for (i = 1; i < 3; ++i)
0fb7aa20
JJ
9311 {
9312 tree this_jump_target = tmp;
9313 if (potential_constant_expression_1 (TREE_OPERAND (t, i),
9314 want_rval, strict, now,
9315 tf_none, &this_jump_target))
9316 {
9317 if (returns (&this_jump_target))
9318 *jump_target = this_jump_target;
9319 else if (!returns (jump_target))
9320 {
9321 if (breaks (&this_jump_target)
9322 || continues (&this_jump_target))
9323 *jump_target = this_jump_target;
9324 if (i == 1)
9325 {
9326 /* If the then branch is potentially constant, but
9327 does not return, check if the else branch
9328 couldn't return, break or continue. */
9329 hash_set<tree> pset;
9330 check_for_return_continue_data data = { &pset, NULL_TREE,
9331 NULL_TREE };
9332 if (tree ret_expr
9333 = cp_walk_tree (&TREE_OPERAND (t, 2),
9334 check_for_return_continue, &data,
9335 &pset))
9336 *jump_target = ret_expr;
9337 else if (*jump_target == NULL_TREE)
9338 {
9339 if (data.continue_stmt)
9340 *jump_target = data.continue_stmt;
9341 else if (data.break_stmt)
9342 *jump_target = data.break_stmt;
9343 }
9344 }
9345 }
9346 return true;
9347 }
9348 }
2d76680f 9349 if (flags & tf_error)
e40b6fc7 9350 error_at (loc, "expression %qE is not a constant expression", t);
2d76680f
PC
9351 return false;
9352
9353 case VEC_INIT_EXPR:
9354 if (VEC_INIT_EXPR_IS_CONSTEXPR (t))
9355 return true;
9356 if (flags & tf_error)
9357 {
e40b6fc7 9358 error_at (loc, "non-constant array initialization");
2d76680f
PC
9359 diagnose_non_constexpr_vec_init (t);
9360 }
9361 return false;
9362
133bc698
JM
9363 case TYPE_DECL:
9364 case TAG_DEFN:
9365 /* We can see these in statement-expressions. */
9366 return true;
9367
baf9ebc8 9368 case CLEANUP_STMT:
1006c9d4
JJ
9369 if (!RECUR (CLEANUP_BODY (t), any))
9370 return false;
9371 if (!CLEANUP_EH_ONLY (t) && !RECUR (CLEANUP_EXPR (t), any))
9372 return false;
9373 return true;
9374
cda0a029 9375 case EMPTY_CLASS_EXPR:
46fdced6 9376 return true;
cda0a029 9377
f937929e
JM
9378 case GOTO_EXPR:
9379 {
9380 tree *target = &TREE_OPERAND (t, 0);
02a981a8
JJ
9381 /* Gotos representing break, continue and cdtor return are OK. */
9382 if (breaks (target) || continues (target) || returns (target))
3075affd
JM
9383 {
9384 *jump_target = *target;
9385 return true;
9386 }
ab3af181 9387 if (flags & tf_error)
e40b6fc7 9388 error_at (loc, "%<goto%> is not a constant expression");
ab3af181 9389 return false;
f937929e
JM
9390 }
9391
6f20c42c
MP
9392 case LABEL_EXPR:
9393 t = LABEL_EXPR_LABEL (t);
8892d532 9394 if (DECL_ARTIFICIAL (t) || cxx_dialect >= cxx23)
6f20c42c
MP
9395 return true;
9396 else if (flags & tf_error)
8892d532
JJ
9397 error_at (loc, "label definition in %<constexpr%> function only "
9398 "available with %<-std=c++2b%> or %<-std=gnu++2b%>");
6f20c42c
MP
9399 return false;
9400
98e09245 9401 case ANNOTATE_EXPR:
98e09245
JJ
9402 return RECUR (TREE_OPERAND (t, 0), rval);
9403
896048cf
JJ
9404 case BIT_CAST_EXPR:
9405 return RECUR (TREE_OPERAND (t, 0), rval);
9406
49789fd0
IS
9407 /* Coroutine await, yield and return expressions are not. */
9408 case CO_AWAIT_EXPR:
9409 case CO_YIELD_EXPR:
9410 case CO_RETURN_EXPR:
9411 return false;
9412
d4c470c3
PP
9413 case NONTYPE_ARGUMENT_PACK:
9414 {
9415 tree args = ARGUMENT_PACK_ARGS (t);
9416 int len = TREE_VEC_LENGTH (args);
9417 for (int i = 0; i < len; ++i)
9418 if (!RECUR (TREE_VEC_ELT (args, i), any))
9419 return false;
9420 return true;
9421 }
9422
2d76680f 9423 default:
878cffbd 9424 if (objc_non_constant_expr_p (t))
2d76680f
PC
9425 return false;
9426
9427 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t)));
ab3af181 9428 gcc_unreachable ();
2d76680f
PC
9429 return false;
9430 }
69eb4fde 9431#undef RECUR
2d76680f
PC
9432}
9433
3075affd
JM
9434bool
9435potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
9436 tsubst_flags_t flags)
9437{
9927ecbb
PP
9438 if (flags & tf_error)
9439 {
9440 /* Check potentiality quietly first, as that could be performed more
9441 efficiently in some cases (currently only for TRUTH_*_EXPR). If
9442 that fails, replay the check noisily to give errors. */
9443 flags &= ~tf_error;
9444 if (potential_constant_expression_1 (t, want_rval, strict, now, flags))
9445 return true;
9446 flags |= tf_error;
9447 }
9448
3075affd
JM
9449 tree target = NULL_TREE;
9450 return potential_constant_expression_1 (t, want_rval, strict, now,
9451 flags, &target);
9452}
9453
2d76680f
PC
9454/* The main entry point to the above. */
9455
9456bool
9457potential_constant_expression (tree t)
9458{
a0ab7ccd 9459 return potential_constant_expression_1 (t, false, true, false, tf_none);
2d76680f
PC
9460}
9461
9462/* As above, but require a constant rvalue. */
9463
9464bool
9465potential_rvalue_constant_expression (tree t)
9466{
a0ab7ccd 9467 return potential_constant_expression_1 (t, true, true, false, tf_none);
2d76680f
PC
9468}
9469
9470/* Like above, but complain about non-constant expressions. */
9471
9472bool
9473require_potential_constant_expression (tree t)
9474{
d8cff23f
MP
9475 return potential_constant_expression_1 (t, false, true, false,
9476 tf_warning_or_error);
2d76680f
PC
9477}
9478
9479/* Cross product of the above. */
9480
9481bool
9482require_potential_rvalue_constant_expression (tree t)
9483{
d8cff23f
MP
9484 return potential_constant_expression_1 (t, true, true, false,
9485 tf_warning_or_error);
9486}
9487
9488/* Like above, but don't consider PARM_DECL a potential_constant_expression. */
9489
9490bool
9491require_rvalue_constant_expression (tree t)
9492{
9493 return potential_constant_expression_1 (t, true, true, true,
9494 tf_warning_or_error);
a0ab7ccd
JM
9495}
9496
9497/* Like potential_constant_expression, but don't consider possible constexpr
9498 substitution of the current function. That is, PARM_DECL qualifies under
9499 potential_constant_expression, but not here.
9500
9501 This is basically what you can check when any actual constant values might
9502 be value-dependent. */
9503
9504bool
9505is_constant_expression (tree t)
9506{
9507 return potential_constant_expression_1 (t, false, true, true, tf_none);
9508}
9509
beb019d3
JM
9510/* As above, but expect an rvalue. */
9511
9512bool
9513is_rvalue_constant_expression (tree t)
9514{
9515 return potential_constant_expression_1 (t, true, true, true, tf_none);
9516}
9517
a0ab7ccd
JM
9518/* Like above, but complain about non-constant expressions. */
9519
9520bool
9521require_constant_expression (tree t)
9522{
9523 return potential_constant_expression_1 (t, false, true, true,
9524 tf_warning_or_error);
9525}
9526
9527/* Like is_constant_expression, but allow const variables that are not allowed
9528 under constexpr rules. */
9529
9530bool
9531is_static_init_expression (tree t)
9532{
9533 return potential_constant_expression_1 (t, false, false, true, tf_none);
2d76680f
PC
9534}
9535
eb07f187
JM
9536/* Returns true if T is a potential constant expression that is not
9537 instantiation-dependent, and therefore a candidate for constant folding even
9538 in a template. */
9539
9540bool
a0ab7ccd 9541is_nondependent_constant_expression (tree t)
eb07f187
JM
9542{
9543 return (!type_unknown_p (t)
a0ab7ccd 9544 && is_constant_expression (t)
eb07f187
JM
9545 && !instantiation_dependent_expression_p (t));
9546}
9547
9548/* Returns true if T is a potential static initializer expression that is not
9549 instantiation-dependent. */
9550
9551bool
a0ab7ccd 9552is_nondependent_static_init_expression (tree t)
eb07f187
JM
9553{
9554 return (!type_unknown_p (t)
a0ab7ccd 9555 && is_static_init_expression (t)
eb07f187
JM
9556 && !instantiation_dependent_expression_p (t));
9557}
9558
87c2080b
JM
9559/* True iff FN is an implicitly constexpr function. */
9560
9561bool
9562decl_implicit_constexpr_p (tree fn)
9563{
9564 if (!(flag_implicit_constexpr
9565 && TREE_CODE (fn) == FUNCTION_DECL
9566 && DECL_DECLARED_CONSTEXPR_P (fn)))
9567 return false;
9568
9569 if (DECL_CLONED_FUNCTION_P (fn))
9570 fn = DECL_CLONED_FUNCTION (fn);
9571
9572 return (DECL_LANG_SPECIFIC (fn)
9573 && DECL_LANG_SPECIFIC (fn)->u.fn.implicit_constexpr);
9574}
9575
97f3003f
JM
9576/* Finalize constexpr processing after parsing. */
9577
9578void
9579fini_constexpr (void)
9580{
9581 /* The contexpr call and fundef copies tables are no longer needed. */
9582 constexpr_call_table = NULL;
9583 fundef_copies_table = NULL;
9584}
9585
2d76680f 9586#include "gt-cp-constexpr.h"
This page took 4.044201 seconds and 5 git commands to generate.