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