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