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