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