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