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