Line data Source code
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-2023 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 "timevar.h"
35 : #include "fold-const-call.h"
36 : #include "stor-layout.h"
37 : #include "cgraph.h"
38 : #include "opts.h"
39 : #include "stringpool.h"
40 : #include "attribs.h"
41 : #include "fold-const.h"
42 : #include "intl.h"
43 : #include "toplev.h"
44 :
45 : static bool verify_constant (tree, bool, bool *, bool *);
46 : #define VERIFY_CONSTANT(X) \
47 : do { \
48 : if (verify_constant ((X), ctx->quiet, non_constant_p, overflow_p)) \
49 : return t; \
50 : } while (0)
51 :
52 : static HOST_WIDE_INT find_array_ctor_elt (tree ary, tree dindex,
53 : bool insert = false);
54 : static int array_index_cmp (tree key, tree index);
55 :
56 : /* Returns true iff FUN is an instantiation of a constexpr function
57 : template or a defaulted constexpr function. */
58 :
59 : bool
60 122054 : is_instantiation_of_constexpr (tree fun)
61 : {
62 122239 : return ((DECL_TEMPLOID_INSTANTIATION (fun)
63 121869 : && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun)))
64 122250 : || (DECL_DEFAULTED_FN (fun)
65 0 : && DECL_DECLARED_CONSTEXPR_P (fun)));
66 : }
67 :
68 : /* Return true if T is a literal type. */
69 :
70 : bool
71 53755404 : literal_type_p (tree t)
72 : {
73 52068298 : if (SCALAR_TYPE_P (t)
74 16970735 : || VECTOR_TYPE_P (t)
75 16958917 : || TYPE_REF_P (t)
76 67926675 : || (VOID_TYPE_P (t) && cxx_dialect >= cxx14))
77 : return true;
78 12962125 : if (CLASS_TYPE_P (t))
79 : {
80 12126034 : t = complete_type (t);
81 12126034 : gcc_assert (COMPLETE_TYPE_P (t) || errorcount);
82 12126034 : return CLASSTYPE_LITERAL_P (t);
83 : }
84 836091 : if (TREE_CODE (t) == ARRAY_TYPE)
85 835930 : return literal_type_p (strip_array_types (t));
86 : return false;
87 : }
88 :
89 : /* If DECL is a variable declared `constexpr', require its type
90 : be literal. Return error_mark_node if we give an error, the
91 : DECL otherwise. */
92 :
93 : tree
94 175482204 : ensure_literal_type_for_constexpr_object (tree decl)
95 : {
96 175482204 : tree type = TREE_TYPE (decl);
97 175482204 : if (VAR_P (decl)
98 58938313 : && (DECL_DECLARED_CONSTEXPR_P (decl)
99 49073851 : || var_in_constexpr_fn (decl))
100 187629573 : && !processing_template_decl)
101 : {
102 7528344 : tree stype = strip_array_types (type);
103 7528344 : if (CLASS_TYPE_P (stype) && !COMPLETE_TYPE_P (complete_type (stype)))
104 : /* Don't complain here, we'll complain about incompleteness
105 : when we try to initialize the variable. */;
106 7528338 : else if (!literal_type_p (type))
107 : {
108 729 : if (DECL_DECLARED_CONSTEXPR_P (decl))
109 : {
110 36 : auto_diagnostic_group d;
111 36 : error_at (DECL_SOURCE_LOCATION (decl),
112 : "the type %qT of %<constexpr%> variable %qD "
113 : "is not literal", type, decl);
114 36 : explain_non_literal_class (type);
115 36 : decl = error_mark_node;
116 36 : }
117 693 : else if (cxx_dialect < cxx23)
118 : {
119 647 : if (!is_instantiation_of_constexpr (current_function_decl))
120 : {
121 8 : auto_diagnostic_group d;
122 8 : error_at (DECL_SOURCE_LOCATION (decl),
123 : "variable %qD of non-literal type %qT in "
124 : "%<constexpr%> function only available with "
125 : "%<-std=c++2b%> or %<-std=gnu++2b%>", decl, type);
126 8 : explain_non_literal_class (type);
127 8 : decl = error_mark_node;
128 8 : }
129 647 : cp_function_chain->invalid_constexpr = true;
130 : }
131 : }
132 7527609 : else if (DECL_DECLARED_CONSTEXPR_P (decl)
133 7527609 : && variably_modified_type_p (type, NULL_TREE))
134 : {
135 4 : error_at (DECL_SOURCE_LOCATION (decl),
136 : "%<constexpr%> variable %qD has variably-modified "
137 : "type %qT", decl, type);
138 4 : decl = error_mark_node;
139 : }
140 : }
141 175482204 : return decl;
142 : }
143 :
144 : /* Issue a diagnostic with text GMSGID for constructs that are invalid in
145 : constexpr functions. CONSTEXPR_FUNDEF_P is true if we're checking
146 : a constexpr function body; if so, don't report hard errors and issue
147 : a pedwarn pre-C++23, or a warning in C++23, if requested by
148 : -Winvalid-constexpr. Otherwise, we're not in the context where we are
149 : checking if a function can be marked 'constexpr', so give a hard error. */
150 :
151 : ATTRIBUTE_GCC_DIAG(3,4)
152 : static bool
153 609 : constexpr_error (location_t location, bool constexpr_fundef_p,
154 : const char *gmsgid, ...)
155 : {
156 609 : diagnostic_info diagnostic;
157 609 : va_list ap;
158 609 : rich_location richloc (line_table, location);
159 609 : va_start (ap, gmsgid);
160 609 : bool ret;
161 609 : if (!constexpr_fundef_p)
162 : {
163 : /* Report an error that cannot be suppressed. */
164 412 : diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_ERROR);
165 412 : ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
166 : }
167 197 : else if (warn_invalid_constexpr)
168 : {
169 192 : diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
170 192 : cxx_dialect < cxx23 ? DK_PEDWARN : DK_WARNING);
171 192 : diagnostic.option_index = OPT_Winvalid_constexpr;
172 192 : ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
173 : }
174 : else
175 : ret = false;
176 609 : va_end (ap);
177 1218 : return ret;
178 609 : }
179 :
180 : struct constexpr_fundef_hasher : ggc_ptr_hash<constexpr_fundef>
181 : {
182 : static hashval_t hash (const constexpr_fundef *);
183 : static bool equal (const constexpr_fundef *, const constexpr_fundef *);
184 : };
185 :
186 : /* This table holds all constexpr function definitions seen in
187 : the current translation unit. */
188 :
189 : static GTY (()) hash_table<constexpr_fundef_hasher> *constexpr_fundef_table;
190 :
191 : /* Utility function used for managing the constexpr function table.
192 : Return true if the entries pointed to by P and Q are for the
193 : same constexpr function. */
194 :
195 : inline bool
196 233999522 : constexpr_fundef_hasher::equal (const constexpr_fundef *lhs,
197 : const constexpr_fundef *rhs)
198 : {
199 222106859 : return lhs->decl == rhs->decl;
200 : }
201 :
202 : /* Utility function used for managing the constexpr function table.
203 : Return a hash value for the entry pointed to by Q. */
204 :
205 : inline hashval_t
206 230245971 : constexpr_fundef_hasher::hash (const constexpr_fundef *fundef)
207 : {
208 230245971 : return DECL_UID (fundef->decl);
209 : }
210 :
211 : /* Return a previously saved definition of function FUN. */
212 :
213 : constexpr_fundef *
214 18416193 : retrieve_constexpr_fundef (tree fun)
215 : {
216 18416193 : if (constexpr_fundef_table == NULL)
217 : return NULL;
218 :
219 18414371 : constexpr_fundef fundef = { fun, NULL_TREE, NULL_TREE, NULL_TREE };
220 18414371 : return constexpr_fundef_table->find (&fundef);
221 : }
222 :
223 : /* Check whether the parameter and return types of FUN are valid for a
224 : constexpr function, and complain if COMPLAIN. */
225 :
226 : bool
227 8383580 : is_valid_constexpr_fn (tree fun, bool complain)
228 : {
229 8383580 : bool ret = true;
230 :
231 16767160 : if (DECL_INHERITED_CTOR (fun)
232 937321 : && TREE_CODE (fun) == TEMPLATE_DECL)
233 : {
234 0 : ret = false;
235 0 : if (complain)
236 0 : error ("inherited constructor %qD is not %<constexpr%>",
237 0 : DECL_INHERITED_CTOR (fun));
238 : }
239 : else
240 : {
241 8383580 : for (tree parm = FUNCTION_FIRST_USER_PARM (fun);
242 16230477 : parm != NULL_TREE; parm = TREE_CHAIN (parm))
243 7846897 : if (!literal_type_p (TREE_TYPE (parm)))
244 : {
245 5124 : ret = false;
246 5124 : if (complain)
247 : {
248 20 : auto_diagnostic_group d;
249 20 : if (constexpr_error (input_location, /*constexpr_fundef_p*/true,
250 : "invalid type for parameter %d of "
251 : "%<constexpr%> function %q+#D",
252 20 : DECL_PARM_INDEX (parm), fun))
253 20 : explain_non_literal_class (TREE_TYPE (parm));
254 20 : }
255 : }
256 : }
257 :
258 12913212 : if (LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun)) && cxx_dialect < cxx17)
259 : {
260 1 : ret = false;
261 1 : if (complain)
262 1 : inform (DECL_SOURCE_LOCATION (fun),
263 : "lambdas are implicitly %<constexpr%> only in C++17 and later");
264 : }
265 16767158 : else if (DECL_DESTRUCTOR_P (fun))
266 : {
267 17559 : if (cxx_dialect < cxx20)
268 : {
269 72 : ret = false;
270 72 : if (complain)
271 0 : error_at (DECL_SOURCE_LOCATION (fun),
272 : "%<constexpr%> destructors only available"
273 : " with %<-std=c++20%> or %<-std=gnu++20%>");
274 : }
275 : }
276 8366020 : else if (!DECL_CONSTRUCTOR_P (fun))
277 : {
278 7428699 : tree rettype = TREE_TYPE (TREE_TYPE (fun));
279 7428699 : if (!literal_type_p (rettype))
280 : {
281 38995 : ret = false;
282 38995 : if (complain)
283 : {
284 11 : auto_diagnostic_group d;
285 11 : if (constexpr_error (input_location, /*constexpr_fundef_p*/true,
286 : "invalid return type %qT of %<constexpr%> "
287 : "function %q+D", rettype, fun))
288 11 : explain_non_literal_class (rettype);
289 11 : }
290 : }
291 :
292 : /* C++14 DR 1684 removed this restriction. */
293 7428699 : if (cxx_dialect < cxx14
294 28483 : && DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
295 7429350 : && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun)))
296 : {
297 0 : ret = false;
298 0 : if (complain)
299 : {
300 0 : auto_diagnostic_group d;
301 0 : if (pedwarn (DECL_SOURCE_LOCATION (fun), OPT_Wpedantic,
302 : "enclosing class of %<constexpr%> non-static"
303 : " member function %q+#D is not a literal type",
304 : fun))
305 0 : explain_non_literal_class (DECL_CONTEXT (fun));
306 0 : }
307 : }
308 : }
309 937321 : else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun)))
310 : {
311 103 : ret = false;
312 103 : if (complain)
313 4 : error ("%q#T has virtual base classes", DECL_CONTEXT (fun));
314 : }
315 :
316 8383580 : return ret;
317 : }
318 :
319 : /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF
320 : for a member of an anonymous aggregate, INIT is the initializer for that
321 : member, and VEC_OUTER is the vector of constructor elements for the class
322 : whose constructor we are processing. Add the initializer to the vector
323 : and return true to indicate success. */
324 :
325 : static bool
326 539 : build_anon_member_initialization (tree member, tree init,
327 : vec<constructor_elt, va_gc> **vec_outer)
328 : {
329 : /* MEMBER presents the relevant fields from the inside out, but we need
330 : to build up the initializer from the outside in so that we can reuse
331 : previously built CONSTRUCTORs if this is, say, the second field in an
332 : anonymous struct. So we use a vec as a stack. */
333 539 : auto_vec<tree, 2> fields;
334 1120 : do
335 : {
336 1120 : fields.safe_push (TREE_OPERAND (member, 1));
337 1120 : member = TREE_OPERAND (member, 0);
338 : }
339 1120 : while (ANON_AGGR_TYPE_P (TREE_TYPE (member))
340 2243 : && TREE_CODE (member) == COMPONENT_REF);
341 :
342 : /* VEC has the constructor elements vector for the context of FIELD.
343 : If FIELD is an anonymous aggregate, we will push inside it. */
344 : vec<constructor_elt, va_gc> **vec = vec_outer;
345 : tree field;
346 1120 : while (field = fields.pop(),
347 1120 : ANON_AGGR_TYPE_P (TREE_TYPE (field)))
348 : {
349 581 : tree ctor;
350 : /* If there is already an outer constructor entry for the anonymous
351 : aggregate FIELD, use it; otherwise, insert one. */
352 581 : if (vec_safe_is_empty (*vec)
353 80 : || (*vec)->last().index != field)
354 : {
355 539 : ctor = build_constructor (TREE_TYPE (field), NULL);
356 539 : CONSTRUCTOR_APPEND_ELT (*vec, field, ctor);
357 : }
358 : else
359 42 : ctor = (*vec)->last().value;
360 581 : vec = &CONSTRUCTOR_ELTS (ctor);
361 : }
362 :
363 : /* Now we're at the innermost field, the one that isn't an anonymous
364 : aggregate. Add its initializer to the CONSTRUCTOR and we're done. */
365 539 : gcc_assert (fields.is_empty());
366 539 : CONSTRUCTOR_APPEND_ELT (*vec, field, init);
367 :
368 539 : return true;
369 539 : }
370 :
371 : /* Subroutine of build_constexpr_constructor_member_initializers.
372 : The expression tree T represents a data member initialization
373 : in a (constexpr) constructor definition. Build a pairing of
374 : the data member with its initializer, and prepend that pair
375 : to the existing initialization pair INITS. */
376 :
377 : static bool
378 1216187 : build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec)
379 : {
380 1310524 : tree member, init;
381 1310524 : if (TREE_CODE (t) == CLEANUP_POINT_EXPR)
382 465684 : t = TREE_OPERAND (t, 0);
383 1310524 : if (TREE_CODE (t) == EXPR_STMT)
384 465729 : t = TREE_OPERAND (t, 0);
385 1310524 : if (t == error_mark_node)
386 : return false;
387 1310518 : if (TREE_CODE (t) == STATEMENT_LIST)
388 : {
389 1336459 : for (tree stmt : tsi_range (t))
390 26651 : if (! build_data_member_initialization (stmt, vec))
391 6 : return false;
392 : return true;
393 : }
394 1216352 : if (TREE_CODE (t) == CLEANUP_STMT)
395 : {
396 : /* We can't see a CLEANUP_STMT in a constructor for a literal class,
397 : but we can in a constexpr constructor for a non-literal class. Just
398 : ignore it; either all the initialization will be constant, in which
399 : case the cleanup can't run, or it can't be constexpr.
400 : Still recurse into CLEANUP_BODY. */
401 93345 : return build_data_member_initialization (CLEANUP_BODY (t), vec);
402 : }
403 1123007 : if (TREE_CODE (t) == CONVERT_EXPR)
404 751219 : t = TREE_OPERAND (t, 0);
405 1123007 : if (TREE_CODE (t) == INIT_EXPR
406 : /* vptr initialization shows up as a MODIFY_EXPR. In C++14 we only
407 : use what this function builds for cx_check_missing_mem_inits, and
408 : assignment in the ctor body doesn't count. */
409 386048 : || (cxx_dialect < cxx14 && TREE_CODE (t) == MODIFY_EXPR))
410 : {
411 737328 : member = TREE_OPERAND (t, 0);
412 737328 : init = break_out_target_exprs (TREE_OPERAND (t, 1));
413 : }
414 385679 : else if (TREE_CODE (t) == CALL_EXPR)
415 : {
416 365665 : tree fn = get_callee_fndecl (t);
417 731330 : if (!fn || !DECL_CONSTRUCTOR_P (fn))
418 : /* We're only interested in calls to subobject constructors. */
419 : return true;
420 354451 : member = CALL_EXPR_ARG (t, 0);
421 : /* We don't use build_cplus_new here because it complains about
422 : abstract bases. Leaving the call unwrapped means that it has the
423 : wrong type, but cxx_eval_constant_expression doesn't care. */
424 354451 : init = break_out_target_exprs (t);
425 : }
426 20014 : else if (TREE_CODE (t) == BIND_EXPR)
427 992 : return build_data_member_initialization (BIND_EXPR_BODY (t), vec);
428 : else
429 : /* Don't add anything else to the CONSTRUCTOR. */
430 : return true;
431 1091779 : if (INDIRECT_REF_P (member))
432 15377 : member = TREE_OPERAND (member, 0);
433 1091779 : if (TREE_CODE (member) == NOP_EXPR)
434 : {
435 92513 : tree op = member;
436 92513 : STRIP_NOPS (op);
437 92513 : if (TREE_CODE (op) == ADDR_EXPR)
438 : {
439 222 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
440 : (TREE_TYPE (TREE_TYPE (op)),
441 : TREE_TYPE (TREE_TYPE (member))));
442 : /* Initializing a cv-qualified member; we need to look through
443 : the const_cast. */
444 : member = op;
445 : }
446 92291 : else if (op == current_class_ptr
447 184500 : && (same_type_ignoring_top_level_qualifiers_p
448 92209 : (TREE_TYPE (TREE_TYPE (member)),
449 : current_class_type)))
450 : /* Delegating constructor. */
451 : member = op;
452 : else
453 : {
454 : /* This is an initializer for an empty base; keep it for now so
455 : we can check it in cxx_eval_bare_aggregate. */
456 89859 : gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member))));
457 : }
458 : }
459 1091779 : if (TREE_CODE (member) == ADDR_EXPR)
460 277537 : member = TREE_OPERAND (member, 0);
461 1091779 : if (TREE_CODE (member) == COMPONENT_REF)
462 : {
463 998878 : tree aggr = TREE_OPERAND (member, 0);
464 998878 : if (TREE_CODE (aggr) == VAR_DECL)
465 : /* Initializing a local variable, don't add anything. */
466 : return true;
467 998876 : if (TREE_CODE (aggr) != COMPONENT_REF)
468 : /* Normal member initialization. */
469 998113 : member = TREE_OPERAND (member, 1);
470 763 : else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr)))
471 : /* Initializing a member of an anonymous union. */
472 539 : return build_anon_member_initialization (member, init, vec);
473 : else
474 : /* We're initializing a vtable pointer in a base. Leave it as
475 : COMPONENT_REF so we remember the path to get to the vfield. */
476 224 : gcc_assert (TREE_TYPE (member) == vtbl_ptr_type_node);
477 : }
478 :
479 : /* Value-initialization can produce multiple initializers for the
480 : same field; use the last one. */
481 1289221 : if (!vec_safe_is_empty (*vec) && (*vec)->last().index == member)
482 25 : (*vec)->last().value = init;
483 : else
484 1091213 : CONSTRUCTOR_APPEND_ELT (*vec, member, init);
485 : return true;
486 : }
487 :
488 : /* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval.
489 : In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a
490 : BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */
491 :
492 : static bool
493 2011 : check_constexpr_bind_expr_vars (tree t)
494 : {
495 2011 : gcc_assert (TREE_CODE (t) == BIND_EXPR);
496 :
497 2901 : for (tree var = BIND_EXPR_VARS (t); var; var = DECL_CHAIN (var))
498 890 : if (TREE_CODE (var) == TYPE_DECL
499 886 : && DECL_IMPLICIT_TYPEDEF_P (var)
500 890 : && !LAMBDA_TYPE_P (TREE_TYPE (var)))
501 : return false;
502 : return true;
503 : }
504 :
505 : /* Subroutine of check_constexpr_ctor_body. */
506 :
507 : static bool
508 3200 : check_constexpr_ctor_body_1 (tree last, tree list)
509 : {
510 3200 : switch (TREE_CODE (list))
511 : {
512 0 : case DECL_EXPR:
513 0 : if (TREE_CODE (DECL_EXPR_DECL (list)) == USING_DECL
514 0 : || TREE_CODE (DECL_EXPR_DECL (list)) == TYPE_DECL)
515 : return true;
516 : return false;
517 :
518 0 : case CLEANUP_POINT_EXPR:
519 0 : return check_constexpr_ctor_body (last, TREE_OPERAND (list, 0),
520 0 : /*complain=*/false);
521 :
522 1590 : case BIND_EXPR:
523 1590 : if (!check_constexpr_bind_expr_vars (list)
524 1590 : || !check_constexpr_ctor_body (last, BIND_EXPR_BODY (list),
525 : /*complain=*/false))
526 0 : return false;
527 : return true;
528 :
529 : case USING_STMT:
530 : case STATIC_ASSERT:
531 : case DEBUG_BEGIN_STMT:
532 : return true;
533 :
534 : default:
535 : return false;
536 : }
537 : }
538 :
539 : /* Make sure that there are no statements after LAST in the constructor
540 : body represented by LIST. */
541 :
542 : bool
543 1742347 : check_constexpr_ctor_body (tree last, tree list, bool complain)
544 : {
545 : /* C++14 doesn't require a constexpr ctor to have an empty body. */
546 1742347 : if (cxx_dialect >= cxx14)
547 : return true;
548 :
549 10419 : bool ok = true;
550 10419 : if (TREE_CODE (list) == STATEMENT_LIST)
551 : {
552 10419 : tree_stmt_iterator i = tsi_last (list);
553 13619 : for (; !tsi_end_p (i); tsi_prev (&i))
554 : {
555 11969 : tree t = tsi_stmt (i);
556 11969 : if (t == last)
557 : break;
558 3200 : if (!check_constexpr_ctor_body_1 (last, t))
559 : {
560 : ok = false;
561 : break;
562 : }
563 : }
564 : }
565 0 : else if (list != last
566 0 : && !check_constexpr_ctor_body_1 (last, list))
567 : ok = false;
568 10419 : if (!ok)
569 : {
570 0 : if (complain)
571 0 : error ("%<constexpr%> constructor does not have empty body");
572 0 : DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false;
573 : }
574 : return ok;
575 : }
576 :
577 : /* V is a vector of constructor elements built up for the base and member
578 : initializers of a constructor for TYPE. They need to be in increasing
579 : offset order, which they might not be yet if TYPE has a primary base
580 : which is not first in the base-clause or a vptr and at least one base
581 : all of which are non-primary. */
582 :
583 : static vec<constructor_elt, va_gc> *
584 934335 : sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v)
585 : {
586 934335 : tree pri = CLASSTYPE_PRIMARY_BINFO (type);
587 934335 : tree field_type;
588 934335 : unsigned i;
589 934335 : constructor_elt *ce;
590 :
591 934335 : if (pri)
592 2391 : field_type = BINFO_TYPE (pri);
593 931944 : else if (TYPE_CONTAINS_VPTR_P (type))
594 10108 : field_type = vtbl_ptr_type_node;
595 : else
596 : return v;
597 :
598 : /* Find the element for the primary base or vptr and move it to the
599 : beginning of the vec. */
600 13261 : for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
601 3295 : if (TREE_TYPE (ce->index) == field_type)
602 : break;
603 :
604 13010 : if (i > 0 && i < vec_safe_length (v))
605 : {
606 16 : vec<constructor_elt, va_gc> &vref = *v;
607 16 : constructor_elt elt = vref[i];
608 32 : for (; i > 0; --i)
609 16 : vref[i] = vref[i-1];
610 16 : vref[0] = elt;
611 : }
612 :
613 : return v;
614 : }
615 :
616 : /* Build compile-time evalable representations of member-initializer list
617 : for a constexpr constructor. */
618 :
619 : static tree
620 936770 : build_constexpr_constructor_member_initializers (tree type, tree body)
621 : {
622 936770 : vec<constructor_elt, va_gc> *vec = NULL;
623 936770 : bool ok = true;
624 2151998 : while (true)
625 2151998 : switch (TREE_CODE (body))
626 : {
627 464475 : case MUST_NOT_THROW_EXPR:
628 464475 : case EH_SPEC_BLOCK:
629 464475 : body = TREE_OPERAND (body, 0);
630 464475 : break;
631 :
632 750753 : case STATEMENT_LIST:
633 2902773 : for (tree stmt : tsi_range (body))
634 : {
635 1501522 : body = stmt;
636 1501522 : if (TREE_CODE (body) == BIND_EXPR)
637 : break;
638 : }
639 : break;
640 :
641 936770 : case BIND_EXPR:
642 936770 : body = BIND_EXPR_BODY (body);
643 936770 : goto found;
644 :
645 0 : default:
646 0 : gcc_unreachable ();
647 : }
648 936770 : found:
649 936770 : if (TREE_CODE (body) == TRY_BLOCK)
650 : {
651 27 : body = TREE_OPERAND (body, 0);
652 27 : if (TREE_CODE (body) == BIND_EXPR)
653 27 : body = BIND_EXPR_BODY (body);
654 : }
655 936770 : if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
656 : {
657 650213 : body = TREE_OPERAND (body, 0);
658 650213 : if (TREE_CODE (body) == EXPR_STMT)
659 650213 : body = TREE_OPERAND (body, 0);
660 650213 : if (TREE_CODE (body) == INIT_EXPR
661 650213 : && (same_type_ignoring_top_level_qualifiers_p
662 0 : (TREE_TYPE (TREE_OPERAND (body, 0)),
663 : current_class_type)))
664 : {
665 : /* Trivial copy. */
666 0 : return TREE_OPERAND (body, 1);
667 : }
668 650213 : ok = build_data_member_initialization (body, &vec);
669 : }
670 286557 : else if (TREE_CODE (body) == STATEMENT_LIST)
671 : {
672 825640 : for (tree stmt : tsi_range (body))
673 : {
674 539203 : ok = build_data_member_initialization (stmt, &vec);
675 539203 : if (!ok)
676 : break;
677 : }
678 : }
679 120 : else if (EXPR_P (body))
680 120 : ok = build_data_member_initialization (body, &vec);
681 : else
682 0 : gcc_assert (errorcount > 0);
683 936770 : if (ok)
684 : {
685 936764 : if (vec_safe_length (vec) > 0)
686 : {
687 : /* In a delegating constructor, return the target. */
688 893742 : constructor_elt *ce = &(*vec)[0];
689 893742 : if (ce->index == current_class_ptr)
690 : {
691 2429 : body = ce->value;
692 2429 : vec_free (vec);
693 2429 : return body;
694 : }
695 : }
696 934335 : vec = sort_constexpr_mem_initializers (type, vec);
697 934335 : return build_constructor (type, vec);
698 : }
699 : else
700 6 : return error_mark_node;
701 : }
702 :
703 : /* We have an expression tree T that represents a call, either CALL_EXPR
704 : or AGGR_INIT_EXPR. If the call is lexically to a named function,
705 : retrun the _DECL for that function. */
706 :
707 : static tree
708 172948602 : get_function_named_in_call (tree t)
709 : {
710 172948602 : tree fun = cp_get_callee (t);
711 172920423 : if (fun && TREE_CODE (fun) == ADDR_EXPR
712 290895337 : && TREE_CODE (TREE_OPERAND (fun, 0)) == FUNCTION_DECL)
713 117946735 : fun = TREE_OPERAND (fun, 0);
714 172948602 : return fun;
715 : }
716 :
717 : /* Subroutine of check_constexpr_fundef. BODY is the body of a function
718 : declared to be constexpr, or a sub-statement thereof. Returns the
719 : return value if suitable, error_mark_node for a statement not allowed in
720 : a constexpr function, or NULL_TREE if no return value was found. */
721 :
722 : tree
723 69187 : constexpr_fn_retval (tree body)
724 : {
725 75505 : switch (TREE_CODE (body))
726 : {
727 20389 : case STATEMENT_LIST:
728 20389 : {
729 20389 : tree expr = NULL_TREE;
730 61115 : for (tree stmt : tsi_range (body))
731 : {
732 40746 : tree s = constexpr_fn_retval (stmt);
733 40746 : if (s == error_mark_node)
734 20 : return error_mark_node;
735 40726 : else if (s == NULL_TREE)
736 : /* Keep iterating. */;
737 20362 : else if (expr)
738 : /* Multiple return statements. */
739 0 : return error_mark_node;
740 : else
741 : expr = s;
742 : }
743 : return expr;
744 : }
745 :
746 28414 : case RETURN_EXPR:
747 28414 : return break_out_target_exprs (TREE_OPERAND (body, 0));
748 :
749 2 : case DECL_EXPR:
750 2 : {
751 2 : tree decl = DECL_EXPR_DECL (body);
752 2 : if (TREE_CODE (decl) == USING_DECL
753 : /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */
754 2 : || DECL_ARTIFICIAL (decl))
755 : return NULL_TREE;
756 2 : return error_mark_node;
757 : }
758 :
759 5897 : case CLEANUP_POINT_EXPR:
760 5897 : return constexpr_fn_retval (TREE_OPERAND (body, 0));
761 :
762 421 : case BIND_EXPR:
763 421 : if (!check_constexpr_bind_expr_vars (body))
764 0 : return error_mark_node;
765 421 : return constexpr_fn_retval (BIND_EXPR_BODY (body));
766 :
767 : case USING_STMT:
768 : case DEBUG_BEGIN_STMT:
769 : return NULL_TREE;
770 :
771 0 : case CALL_EXPR:
772 0 : {
773 0 : tree fun = get_function_named_in_call (body);
774 0 : if (fun != NULL_TREE
775 0 : && fndecl_built_in_p (fun, BUILT_IN_UNREACHABLE))
776 : return NULL_TREE;
777 : }
778 : /* Fallthru. */
779 :
780 18 : default:
781 18 : return error_mark_node;
782 : }
783 : }
784 :
785 : /* Subroutine of check_constexpr_fundef. BODY is the DECL_SAVED_TREE of
786 : FUN; do the necessary transformations to turn it into a single expression
787 : that we can store in the hash table. */
788 :
789 : static tree
790 8261888 : massage_constexpr_body (tree fun, tree body)
791 : {
792 16523776 : if (DECL_CONSTRUCTOR_P (fun))
793 936770 : body = build_constexpr_constructor_member_initializers
794 936770 : (DECL_CONTEXT (fun), body);
795 7325118 : else if (cxx_dialect < cxx14)
796 : {
797 28241 : if (TREE_CODE (body) == EH_SPEC_BLOCK)
798 0 : body = EH_SPEC_STMTS (body);
799 28241 : if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
800 17006 : body = TREE_OPERAND (body, 0);
801 28241 : body = constexpr_fn_retval (body);
802 : }
803 8261888 : return body;
804 : }
805 :
806 : /* CTYPE is a type constructed from BODY. Return true if some
807 : bases/fields are uninitialized, and complain if COMPLAIN. */
808 :
809 : static bool
810 816861 : cx_check_missing_mem_inits (tree ctype, tree body, bool complain)
811 : {
812 : /* We allow uninitialized bases/fields in C++20. */
813 816861 : if (cxx_dialect >= cxx20)
814 : return false;
815 :
816 732694 : unsigned nelts = 0;
817 :
818 732694 : if (body)
819 : {
820 732690 : if (TREE_CODE (body) != CONSTRUCTOR)
821 : return false;
822 732468 : nelts = CONSTRUCTOR_NELTS (body);
823 : }
824 732472 : tree field = TYPE_FIELDS (ctype);
825 :
826 732472 : if (TREE_CODE (ctype) == UNION_TYPE)
827 : {
828 7565 : if (nelts == 0 && next_aggregate_field (field))
829 : {
830 2 : if (complain)
831 2 : error ("%<constexpr%> constructor for union %qT must "
832 : "initialize exactly one non-static data member", ctype);
833 2 : return true;
834 : }
835 7563 : return false;
836 : }
837 :
838 : /* Iterate over the CONSTRUCTOR, checking any missing fields don't
839 : need an explicit initialization. */
840 : bool bad = false;
841 1569971 : for (unsigned i = 0; i <= nelts; ++i)
842 : {
843 1569971 : tree index = NULL_TREE;
844 1569971 : if (i < nelts)
845 : {
846 845064 : index = CONSTRUCTOR_ELT (body, i)->index;
847 : /* Skip base and vtable inits. */
848 1092543 : if (TREE_CODE (index) != FIELD_DECL
849 845064 : || DECL_ARTIFICIAL (index))
850 247479 : continue;
851 : }
852 :
853 36344875 : for (; field != index; field = DECL_CHAIN (field))
854 : {
855 35022432 : tree ftype;
856 35022432 : if (TREE_CODE (field) != FIELD_DECL)
857 69729744 : continue;
858 315053 : if (DECL_UNNAMED_BIT_FIELD (field))
859 12 : continue;
860 315041 : if (DECL_ARTIFICIAL (field))
861 314965 : continue;
862 76 : if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
863 : {
864 : /* Recurse to check the anonymous aggregate member. */
865 8 : bad |= cx_check_missing_mem_inits
866 4 : (TREE_TYPE (field), NULL_TREE, complain);
867 4 : if (bad && !complain)
868 49 : return true;
869 4 : continue;
870 : }
871 72 : ftype = TREE_TYPE (field);
872 72 : if (!ftype || !TYPE_P (ftype) || !COMPLETE_TYPE_P (ftype))
873 : /* A flexible array can't be intialized here, so don't complain
874 : that it isn't. */
875 2 : continue;
876 70 : if (is_empty_field (field))
877 : /* An empty field doesn't need an initializer. */
878 1 : continue;
879 69 : ftype = strip_array_types (ftype);
880 69 : if (type_has_constexpr_default_constructor (ftype))
881 : {
882 : /* It's OK to skip a member with a trivial constexpr ctor.
883 : A constexpr ctor that isn't trivial should have been
884 : added in by now. */
885 2 : gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype)
886 : || errorcount != 0);
887 2 : continue;
888 : }
889 67 : if (!complain)
890 : return true;
891 18 : auto_diagnostic_group d;
892 18 : error ("member %qD must be initialized by mem-initializer "
893 : "in %<constexpr%> constructor", field);
894 18 : inform (DECL_SOURCE_LOCATION (field), "declared here");
895 18 : bad = true;
896 18 : }
897 1322443 : if (field == NULL_TREE)
898 : break;
899 :
900 597585 : if (ANON_AGGR_TYPE_P (TREE_TYPE (index)))
901 : {
902 : /* Check the anonymous aggregate initializer is valid. */
903 442 : bad |= cx_check_missing_mem_inits
904 221 : (TREE_TYPE (index), CONSTRUCTOR_ELT (body, i)->value, complain);
905 221 : if (bad && !complain)
906 : return true;
907 : }
908 597585 : field = DECL_CHAIN (field);
909 : }
910 :
911 : return bad;
912 : }
913 :
914 : /* We are processing the definition of the constexpr function FUN.
915 : Check that its body fulfills the apropriate requirements and
916 : enter it in the constexpr function definition table. */
917 :
918 : void
919 90437191 : maybe_save_constexpr_fundef (tree fun)
920 : {
921 90437191 : if (processing_template_decl
922 34439043 : || cp_function_chain->invalid_constexpr
923 124774675 : || (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun)))
924 82175521 : return;
925 :
926 : /* With -fimplicit-constexpr, try to make inlines constexpr. We'll
927 : actually set DECL_DECLARED_CONSTEXPR_P below if the checks pass. */
928 25716656 : bool implicit = false;
929 25716656 : if (flag_implicit_constexpr)
930 : {
931 8 : if (DECL_DELETING_DESTRUCTOR_P (fun)
932 8 : && decl_implicit_constexpr_p (DECL_CLONED_FUNCTION (fun)))
933 : /* Don't inherit implicit constexpr from the non-deleting
934 : destructor. */
935 0 : DECL_DECLARED_CONSTEXPR_P (fun) = false;
936 :
937 8 : if (!DECL_DECLARED_CONSTEXPR_P (fun)
938 7 : && DECL_DECLARED_INLINE_P (fun)
939 14 : && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fun)))
940 : implicit = true;
941 : }
942 :
943 25716656 : if (!DECL_DECLARED_CONSTEXPR_P (fun) && !implicit)
944 : return;
945 :
946 8302838 : bool complain = !DECL_GENERATED_P (fun) && !implicit;
947 :
948 8302838 : if (!is_valid_constexpr_fn (fun, complain))
949 : return;
950 :
951 8261864 : tree massaged = massage_constexpr_body (fun, DECL_SAVED_TREE (fun));
952 8261864 : if (massaged == NULL_TREE || massaged == error_mark_node)
953 : {
954 16 : if (!DECL_CONSTRUCTOR_P (fun) && complain)
955 0 : error ("body of %<constexpr%> function %qD not a return-statement",
956 : fun);
957 8 : return;
958 : }
959 :
960 8261856 : bool potential = potential_rvalue_constant_expression (massaged);
961 8261856 : if (!potential && complain)
962 159 : require_potential_rvalue_constant_expression_fncheck (massaged);
963 :
964 9198614 : if (DECL_CONSTRUCTOR_P (fun) && potential
965 9197296 : && !DECL_DEFAULTED_FN (fun))
966 : {
967 816630 : if (cx_check_missing_mem_inits (DECL_CONTEXT (fun),
968 : massaged, complain))
969 : potential = false;
970 816565 : else if (cxx_dialect > cxx11)
971 : {
972 : /* What we got from massage_constexpr_body is pretty much just the
973 : ctor-initializer, also check the body. */
974 812013 : massaged = DECL_SAVED_TREE (fun);
975 812013 : potential = potential_rvalue_constant_expression (massaged);
976 812013 : if (!potential && complain)
977 11 : require_potential_rvalue_constant_expression_fncheck (massaged);
978 : }
979 : }
980 :
981 8261856 : if (!potential && complain
982 : /* If -Wno-invalid-constexpr was specified, we haven't complained
983 : about non-constant expressions yet. Register the function and
984 : complain in explain_invalid_constexpr_fn if the function is
985 : called. */
986 186 : && warn_invalid_constexpr != 0)
987 : return;
988 :
989 8261675 : if (implicit)
990 : {
991 6 : if (potential)
992 : {
993 1 : DECL_DECLARED_CONSTEXPR_P (fun) = true;
994 1 : DECL_LANG_SPECIFIC (fun)->u.fn.implicit_constexpr = true;
995 2 : if (DECL_CONSTRUCTOR_P (fun))
996 0 : TYPE_HAS_CONSTEXPR_CTOR (DECL_CONTEXT (fun)) = true;
997 : }
998 : else
999 : /* Don't bother keeping the pre-generic body of unsuitable functions
1000 : not explicitly declared constexpr. */
1001 : return;
1002 : }
1003 :
1004 8261670 : constexpr_fundef entry = {fun, NULL_TREE, NULL_TREE, NULL_TREE};
1005 8261670 : bool clear_ctx = false;
1006 8261670 : if (DECL_RESULT (fun) && DECL_CONTEXT (DECL_RESULT (fun)) == NULL_TREE)
1007 : {
1008 8261670 : clear_ctx = true;
1009 8261670 : DECL_CONTEXT (DECL_RESULT (fun)) = fun;
1010 : }
1011 8261670 : tree saved_fn = current_function_decl;
1012 8261670 : current_function_decl = fun;
1013 8261670 : entry.body = copy_fn (entry.decl, entry.parms, entry.result);
1014 8261670 : current_function_decl = saved_fn;
1015 8261670 : if (clear_ctx)
1016 8261670 : DECL_CONTEXT (DECL_RESULT (entry.decl)) = NULL_TREE;
1017 8261670 : if (!potential)
1018 : /* For a template instantiation, we want to remember the pre-generic body
1019 : for explain_invalid_constexpr_fn, but do tell cxx_eval_call_expression
1020 : that it doesn't need to bother trying to expand the function. */
1021 47732 : entry.result = error_mark_node;
1022 :
1023 8261670 : register_constexpr_fundef (entry);
1024 : }
1025 :
1026 : /* BODY is a validated and massaged definition of a constexpr
1027 : function. Register it in the hash table. */
1028 :
1029 : void
1030 8291426 : register_constexpr_fundef (const constexpr_fundef &value)
1031 : {
1032 : /* Create the constexpr function table if necessary. */
1033 8291426 : if (constexpr_fundef_table == NULL)
1034 19822 : constexpr_fundef_table
1035 19822 : = hash_table<constexpr_fundef_hasher>::create_ggc (101);
1036 :
1037 8291426 : constexpr_fundef **slot = constexpr_fundef_table->find_slot
1038 8291426 : (const_cast<constexpr_fundef *> (&value), INSERT);
1039 :
1040 8291426 : gcc_assert (*slot == NULL);
1041 8291426 : *slot = ggc_alloc<constexpr_fundef> ();
1042 8291426 : **slot = value;
1043 8291426 : }
1044 :
1045 : /* FUN is a non-constexpr (or, with -Wno-invalid-constexpr, a constexpr
1046 : function called in a context that requires a constant expression).
1047 : If it comes from a constexpr template, explain why the instantiation
1048 : isn't constexpr. Otherwise, explain why the function cannot be used
1049 : in a constexpr context. */
1050 :
1051 : void
1052 194 : explain_invalid_constexpr_fn (tree fun)
1053 : {
1054 194 : static hash_set<tree> *diagnosed;
1055 194 : tree body;
1056 : /* In C++23, a function marked 'constexpr' may not actually be a constant
1057 : expression. We haven't diagnosed the problem yet: -Winvalid-constexpr
1058 : wasn't enabled. The function was called, so diagnose why it cannot be
1059 : used in a constant expression. */
1060 194 : if (warn_invalid_constexpr == 0 && DECL_DECLARED_CONSTEXPR_P (fun))
1061 : /* Go on. */;
1062 : /* Only diagnose defaulted functions, lambdas, or instantiations. */
1063 189 : else if (!DECL_DEFAULTED_FN (fun)
1064 264 : && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun))
1065 367 : && !is_instantiation_of_constexpr (fun))
1066 : {
1067 164 : inform (DECL_SOURCE_LOCATION (fun), "%qD declared here", fun);
1068 164 : return;
1069 : }
1070 30 : if (diagnosed == NULL)
1071 25 : diagnosed = new hash_set<tree>;
1072 30 : if (diagnosed->add (fun))
1073 : /* Already explained. */
1074 : return;
1075 :
1076 29 : iloc_sentinel ils = input_location;
1077 29 : if (!lambda_static_thunk_p (fun))
1078 : {
1079 : /* Diagnostics should completely ignore the static thunk, so leave
1080 : input_location set to our caller's location. */
1081 29 : input_location = DECL_SOURCE_LOCATION (fun);
1082 29 : inform (input_location,
1083 : "%qD is not usable as a %<constexpr%> function because:", fun);
1084 : }
1085 : /* First check the declaration. */
1086 29 : if (is_valid_constexpr_fn (fun, true))
1087 : {
1088 : /* Then if it's OK, the body. */
1089 27 : if (!DECL_DECLARED_CONSTEXPR_P (fun)
1090 27 : && DECL_DEFAULTED_FN (fun))
1091 3 : explain_implicit_non_constexpr (fun);
1092 : else
1093 : {
1094 24 : if (constexpr_fundef *fd = retrieve_constexpr_fundef (fun))
1095 22 : body = fd->body;
1096 : else
1097 2 : body = DECL_SAVED_TREE (fun);
1098 24 : body = massage_constexpr_body (fun, body);
1099 24 : require_potential_rvalue_constant_expression (body);
1100 48 : if (DECL_CONSTRUCTOR_P (fun))
1101 6 : cx_check_missing_mem_inits (DECL_CONTEXT (fun), body, true);
1102 : }
1103 : }
1104 29 : }
1105 :
1106 : /* Objects of this type represent calls to constexpr functions
1107 : along with the bindings of parameters to their arguments, for
1108 : the purpose of compile time evaluation. */
1109 :
1110 : struct GTY((for_user)) constexpr_call {
1111 : /* Description of the constexpr function definition. */
1112 : constexpr_fundef *fundef;
1113 : /* Parameter bindings environment. A TREE_VEC of arguments. */
1114 : tree bindings;
1115 : /* Result of the call.
1116 : NULL means the call is being evaluated.
1117 : error_mark_node means that the evaluation was erroneous;
1118 : otherwise, the actuall value of the call. */
1119 : tree result;
1120 : /* The hash of this call; we remember it here to avoid having to
1121 : recalculate it when expanding the hash table. */
1122 : hashval_t hash;
1123 : /* The value of constexpr_ctx::manifestly_const_eval. */
1124 : enum mce_value manifestly_const_eval;
1125 : };
1126 :
1127 : struct constexpr_call_hasher : ggc_ptr_hash<constexpr_call>
1128 : {
1129 : static hashval_t hash (constexpr_call *);
1130 : static bool equal (constexpr_call *, constexpr_call *);
1131 : };
1132 :
1133 : enum constexpr_switch_state {
1134 : /* Used when processing a switch for the first time by cxx_eval_switch_expr
1135 : and default: label for that switch has not been seen yet. */
1136 : css_default_not_seen,
1137 : /* Used when processing a switch for the first time by cxx_eval_switch_expr
1138 : and default: label for that switch has been seen already. */
1139 : css_default_seen,
1140 : /* Used when processing a switch for the second time by
1141 : cxx_eval_switch_expr, where default: label should match. */
1142 : css_default_processing
1143 : };
1144 :
1145 : /* The constexpr expansion context part which needs one instance per
1146 : cxx_eval_outermost_constant_expr invocation. VALUES is a map of values of
1147 : variables initialized within the expression. */
1148 :
1149 249816046 : class constexpr_global_ctx {
1150 : /* Values for any temporaries or local variables within the
1151 : constant-expression. */
1152 : hash_map<tree,tree> values;
1153 : public:
1154 : /* Number of cxx_eval_constant_expression calls (except skipped ones,
1155 : on simple constants or location wrappers) encountered during current
1156 : cxx_eval_outermost_constant_expr call. */
1157 : HOST_WIDE_INT constexpr_ops_count;
1158 : /* Heap VAR_DECLs created during the evaluation of the outermost constant
1159 : expression. */
1160 : auto_vec<tree, 16> heap_vars;
1161 : /* Cleanups that need to be evaluated at the end of CLEANUP_POINT_EXPR. */
1162 : vec<tree> *cleanups;
1163 : /* If non-null, only allow modification of existing values of the variables
1164 : in this set. Set by modifiable_tracker, below. */
1165 : hash_set<tree> *modifiable;
1166 : /* Number of heap VAR_DECL deallocations. */
1167 : unsigned heap_dealloc_count;
1168 : /* Constructor. */
1169 249826830 : constexpr_global_ctx ()
1170 499653660 : : constexpr_ops_count (0), cleanups (NULL), modifiable (nullptr),
1171 249826830 : heap_dealloc_count (0) {}
1172 :
1173 131493873 : tree get_value (tree t)
1174 : {
1175 131493873 : if (tree *p = values.get (t))
1176 7902944 : return *p;
1177 : return NULL_TREE;
1178 : }
1179 10691191 : tree *get_value_ptr (tree t)
1180 : {
1181 10691191 : if (modifiable && !modifiable->contains (t))
1182 : return nullptr;
1183 10691163 : return values.get (t);
1184 : }
1185 22552622 : void put_value (tree t, tree v)
1186 : {
1187 22552622 : bool already_in_map = values.put (t, v);
1188 22552622 : if (!already_in_map && modifiable)
1189 28 : modifiable->add (t);
1190 22552622 : }
1191 30885808 : void remove_value (tree t) { values.remove (t); }
1192 : };
1193 :
1194 : /* Helper class for constexpr_global_ctx. In some cases we want to avoid
1195 : side-effects from evaluation of a particular subexpression of a
1196 : constant-expression. In such cases we use modifiable_tracker to prevent
1197 : modification of variables created outside of that subexpression.
1198 :
1199 : ??? We could change the hash_set to a hash_map, allow and track external
1200 : modifications, and roll them back in the destructor. It's not clear to me
1201 : that this would be worthwhile. */
1202 :
1203 : class modifiable_tracker
1204 : {
1205 : hash_set<tree> set;
1206 : constexpr_global_ctx *global;
1207 : public:
1208 94 : modifiable_tracker (constexpr_global_ctx *g): global(g)
1209 : {
1210 94 : global->modifiable = &set;
1211 94 : }
1212 94 : ~modifiable_tracker ()
1213 : {
1214 122 : for (tree t: set)
1215 28 : global->remove_value (t);
1216 94 : global->modifiable = nullptr;
1217 94 : }
1218 : };
1219 :
1220 : /* The constexpr expansion context. CALL is the current function
1221 : expansion, CTOR is the current aggregate initializer, OBJECT is the
1222 : object being initialized by CTOR, either a VAR_DECL or a _REF. */
1223 :
1224 : struct constexpr_ctx {
1225 : /* The part of the context that needs to be unique to the whole
1226 : cxx_eval_outermost_constant_expr invocation. */
1227 : constexpr_global_ctx *global;
1228 : /* The innermost call we're evaluating. */
1229 : constexpr_call *call;
1230 : /* SAVE_EXPRs and TARGET_EXPR_SLOT vars of TARGET_EXPRs that we've seen
1231 : within the current LOOP_EXPR. NULL if we aren't inside a loop. */
1232 : vec<tree> *save_exprs;
1233 : /* The CONSTRUCTOR we're currently building up for an aggregate
1234 : initializer. */
1235 : tree ctor;
1236 : /* The object we're building the CONSTRUCTOR for. */
1237 : tree object;
1238 : /* If inside SWITCH_EXPR. */
1239 : constexpr_switch_state *css_state;
1240 : /* The aggregate initialization context inside which this one is nested. This
1241 : is used by lookup_placeholder to resolve PLACEHOLDER_EXPRs. */
1242 : const constexpr_ctx *parent;
1243 :
1244 : /* Whether we should error on a non-constant expression or fail quietly.
1245 : This flag needs to be here, but some of the others could move to global
1246 : if they get larger than a word. */
1247 : bool quiet;
1248 : /* Whether we are strictly conforming to constant expression rules or
1249 : trying harder to get a constant value. */
1250 : bool strict;
1251 : /* Whether __builtin_is_constant_evaluated () should be true. */
1252 : mce_value manifestly_const_eval;
1253 : };
1254 :
1255 : /* This internal flag controls whether we should avoid doing anything during
1256 : constexpr evaluation that would cause extra DECL_UID generation, such as
1257 : template instantiation and function body copying. */
1258 :
1259 : static bool uid_sensitive_constexpr_evaluation_value;
1260 :
1261 : /* An internal counter that keeps track of the number of times
1262 : uid_sensitive_constexpr_evaluation_p returned true. */
1263 :
1264 : static unsigned uid_sensitive_constexpr_evaluation_true_counter;
1265 :
1266 : /* The accessor for uid_sensitive_constexpr_evaluation_value which also
1267 : increments the corresponding counter. */
1268 :
1269 : static bool
1270 1445772 : uid_sensitive_constexpr_evaluation_p ()
1271 : {
1272 1441573 : if (uid_sensitive_constexpr_evaluation_value)
1273 : {
1274 187637 : ++uid_sensitive_constexpr_evaluation_true_counter;
1275 187637 : return true;
1276 : }
1277 : else
1278 : return false;
1279 : }
1280 :
1281 : /* The default constructor for uid_sensitive_constexpr_evaluation_sentinel
1282 : enables the internal flag for uid_sensitive_constexpr_evaluation_p
1283 : during the lifetime of the sentinel object. Upon its destruction, the
1284 : previous value of uid_sensitive_constexpr_evaluation_p is restored. */
1285 :
1286 41428652 : uid_sensitive_constexpr_evaluation_sentinel
1287 41428652 : ::uid_sensitive_constexpr_evaluation_sentinel ()
1288 41428652 : : ovr (uid_sensitive_constexpr_evaluation_value, true)
1289 : {
1290 41428652 : }
1291 :
1292 : /* The default constructor for uid_sensitive_constexpr_evaluation_checker
1293 : records the current number of times that uid_sensitive_constexpr_evaluation_p
1294 : has been called and returned true. */
1295 :
1296 1138791942 : uid_sensitive_constexpr_evaluation_checker
1297 1138791942 : ::uid_sensitive_constexpr_evaluation_checker ()
1298 1138791942 : : saved_counter (uid_sensitive_constexpr_evaluation_true_counter)
1299 : {
1300 1072624210 : }
1301 :
1302 : /* Returns true iff uid_sensitive_constexpr_evaluation_p is true, and
1303 : some constexpr evaluation was restricted due to u_s_c_e_p being called
1304 : and returning true during the lifetime of this checker object. */
1305 :
1306 : bool
1307 807730586 : uid_sensitive_constexpr_evaluation_checker::evaluation_restricted_p () const
1308 : {
1309 807730586 : return (uid_sensitive_constexpr_evaluation_value
1310 760157928 : && saved_counter != uid_sensitive_constexpr_evaluation_true_counter);
1311 : }
1312 :
1313 :
1314 : /* A table of all constexpr calls that have been evaluated by the
1315 : compiler in this translation unit. */
1316 :
1317 : static GTY (()) hash_table<constexpr_call_hasher> *constexpr_call_table;
1318 :
1319 : /* Compute a hash value for a constexpr call representation. */
1320 :
1321 : inline hashval_t
1322 98468549 : constexpr_call_hasher::hash (constexpr_call *info)
1323 : {
1324 98468549 : return info->hash;
1325 : }
1326 :
1327 : /* Return true if the objects pointed to by P and Q represent calls
1328 : to the same constexpr function with the same arguments.
1329 : Otherwise, return false. */
1330 :
1331 : bool
1332 102409804 : constexpr_call_hasher::equal (constexpr_call *lhs, constexpr_call *rhs)
1333 : {
1334 102409804 : if (lhs == rhs)
1335 : return true;
1336 102409804 : if (lhs->hash != rhs->hash)
1337 : return false;
1338 11892663 : if (lhs->manifestly_const_eval != rhs->manifestly_const_eval)
1339 : return false;
1340 11892663 : if (!constexpr_fundef_hasher::equal (lhs->fundef, rhs->fundef))
1341 : return false;
1342 11892663 : return cp_tree_equal (lhs->bindings, rhs->bindings);
1343 : }
1344 :
1345 : /* Initialize the constexpr call table, if needed. */
1346 :
1347 : static void
1348 13983435 : maybe_initialize_constexpr_call_table (void)
1349 : {
1350 13983435 : if (constexpr_call_table == NULL)
1351 12729 : constexpr_call_table = hash_table<constexpr_call_hasher>::create_ggc (101);
1352 13983435 : }
1353 :
1354 : /* During constexpr CALL_EXPR evaluation, to avoid issues with sharing when
1355 : a function happens to get called recursively, we unshare the callee
1356 : function's body and evaluate this unshared copy instead of evaluating the
1357 : original body.
1358 :
1359 : FUNDEF_COPIES_TABLE is a per-function freelist of these unshared function
1360 : copies. The underlying data structure of FUNDEF_COPIES_TABLE is a hash_map
1361 : that's keyed off of the original FUNCTION_DECL and whose value is a
1362 : TREE_LIST of this function's unused copies awaiting reuse.
1363 :
1364 : This is not GC-deletable to avoid GC affecting UID generation. */
1365 :
1366 : static GTY(()) decl_tree_map *fundef_copies_table;
1367 :
1368 : /* Reuse a copy or create a new unshared copy of the function FUN.
1369 : Return this copy. We use a TREE_LIST whose PURPOSE is body, VALUE
1370 : is parms, TYPE is result. */
1371 :
1372 : static tree
1373 8603070 : get_fundef_copy (constexpr_fundef *fundef)
1374 : {
1375 8603070 : tree copy;
1376 8603070 : bool existed;
1377 8603070 : tree *slot = &(hash_map_safe_get_or_insert<hm_ggc>
1378 8603070 : (fundef_copies_table, fundef->decl, &existed, 127));
1379 :
1380 8603070 : if (!existed)
1381 : {
1382 : /* There is no cached function available, or in use. We can use
1383 : the function directly. That the slot is now created records
1384 : that this function is now in use. */
1385 1623172 : copy = build_tree_list (fundef->body, fundef->parms);
1386 1623172 : TREE_TYPE (copy) = fundef->result;
1387 : }
1388 6979898 : else if (*slot == NULL_TREE)
1389 : {
1390 4199 : if (uid_sensitive_constexpr_evaluation_p ())
1391 0 : return NULL_TREE;
1392 :
1393 : /* We've already used the function itself, so make a copy. */
1394 4199 : copy = build_tree_list (NULL, NULL);
1395 4199 : tree saved_body = DECL_SAVED_TREE (fundef->decl);
1396 4199 : tree saved_parms = DECL_ARGUMENTS (fundef->decl);
1397 4199 : tree saved_result = DECL_RESULT (fundef->decl);
1398 4199 : tree saved_fn = current_function_decl;
1399 4199 : DECL_SAVED_TREE (fundef->decl) = fundef->body;
1400 4199 : DECL_ARGUMENTS (fundef->decl) = fundef->parms;
1401 4199 : DECL_RESULT (fundef->decl) = fundef->result;
1402 4199 : current_function_decl = fundef->decl;
1403 4199 : TREE_PURPOSE (copy) = copy_fn (fundef->decl, TREE_VALUE (copy),
1404 4199 : TREE_TYPE (copy));
1405 4199 : current_function_decl = saved_fn;
1406 4199 : DECL_RESULT (fundef->decl) = saved_result;
1407 4199 : DECL_ARGUMENTS (fundef->decl) = saved_parms;
1408 4199 : DECL_SAVED_TREE (fundef->decl) = saved_body;
1409 : }
1410 : else
1411 : {
1412 : /* We have a cached function available. */
1413 6975699 : copy = *slot;
1414 6975699 : *slot = TREE_CHAIN (copy);
1415 : }
1416 :
1417 : return copy;
1418 : }
1419 :
1420 : /* Save the copy COPY of function FUN for later reuse by
1421 : get_fundef_copy(). By construction, there will always be an entry
1422 : to find. */
1423 :
1424 : static void
1425 8603070 : save_fundef_copy (tree fun, tree copy)
1426 : {
1427 8603070 : tree *slot = fundef_copies_table->get (fun);
1428 8603070 : TREE_CHAIN (copy) = *slot;
1429 8603070 : *slot = copy;
1430 8603070 : }
1431 :
1432 : /* Whether our evaluation wants a prvalue (e.g. CONSTRUCTOR or _CST),
1433 : a glvalue (e.g. VAR_DECL or _REF), or nothing. */
1434 :
1435 : enum value_cat {
1436 : vc_prvalue = 0,
1437 : vc_glvalue = 1,
1438 : vc_discard = 2
1439 : };
1440 :
1441 : static tree cxx_eval_constant_expression (const constexpr_ctx *, tree,
1442 : value_cat, bool *, bool *, tree * = NULL);
1443 : static tree cxx_eval_bare_aggregate (const constexpr_ctx *, tree,
1444 : value_cat, bool *, bool *);
1445 : static tree cxx_fold_indirect_ref (const constexpr_ctx *, location_t, tree, tree,
1446 : bool * = NULL);
1447 : static tree find_heap_var_refs (tree *, int *, void *);
1448 :
1449 : /* Attempt to evaluate T which represents a call to a builtin function.
1450 : We assume here that all builtin functions evaluate to scalar types
1451 : represented by _CST nodes. */
1452 :
1453 : static tree
1454 8139860 : cxx_eval_builtin_function_call (const constexpr_ctx *ctx, tree t, tree fun,
1455 : value_cat lval,
1456 : bool *non_constant_p, bool *overflow_p)
1457 : {
1458 8139860 : const int nargs = call_expr_nargs (t);
1459 8139860 : tree *args = (tree *) alloca (nargs * sizeof (tree));
1460 8139860 : tree new_call;
1461 8139860 : int i;
1462 :
1463 : /* Don't fold __builtin_constant_p within a constexpr function. */
1464 8139860 : bool bi_const_p = DECL_IS_BUILTIN_CONSTANT_P (fun);
1465 :
1466 : /* If we aren't requiring a constant expression, defer __builtin_constant_p
1467 : in a constexpr function until we have values for the parameters. */
1468 229601 : if (bi_const_p
1469 229601 : && ctx->manifestly_const_eval != mce_true
1470 226013 : && current_function_decl
1471 225926 : && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
1472 : {
1473 16465 : *non_constant_p = true;
1474 16465 : return t;
1475 : }
1476 :
1477 : /* For __builtin_is_constant_evaluated, defer it if not
1478 : ctx->manifestly_const_eval (as sometimes we try to constant evaluate
1479 : without manifestly_const_eval even expressions or parts thereof which
1480 : will later be manifestly const_eval evaluated), otherwise fold it to
1481 : true. */
1482 8123395 : if (fndecl_built_in_p (fun, CP_BUILT_IN_IS_CONSTANT_EVALUATED,
1483 : BUILT_IN_FRONTEND))
1484 : {
1485 2758476 : if (ctx->manifestly_const_eval == mce_unknown)
1486 : {
1487 2745661 : *non_constant_p = true;
1488 2745661 : return t;
1489 : }
1490 12815 : return constant_boolean_node (ctx->manifestly_const_eval == mce_true,
1491 12815 : boolean_type_node);
1492 : }
1493 :
1494 5364919 : if (fndecl_built_in_p (fun, CP_BUILT_IN_SOURCE_LOCATION, BUILT_IN_FRONTEND))
1495 : {
1496 99 : temp_override<tree> ovr (current_function_decl);
1497 99 : if (ctx->call && ctx->call->fundef)
1498 1 : current_function_decl = ctx->call->fundef->decl;
1499 99 : return fold_builtin_source_location (t);
1500 99 : }
1501 :
1502 5364820 : int strops = 0;
1503 5364820 : int strret = 0;
1504 5364820 : if (fndecl_built_in_p (fun, BUILT_IN_NORMAL))
1505 4771689 : switch (DECL_FUNCTION_CODE (fun))
1506 : {
1507 : case BUILT_IN_STRLEN:
1508 : case BUILT_IN_STRNLEN:
1509 5364751 : strops = 1;
1510 : break;
1511 73382 : case BUILT_IN_MEMCHR:
1512 73382 : case BUILT_IN_STRCHR:
1513 73382 : case BUILT_IN_STRRCHR:
1514 73382 : strops = 1;
1515 73382 : strret = 1;
1516 73382 : break;
1517 237644 : case BUILT_IN_MEMCMP:
1518 237644 : case BUILT_IN_STRCMP:
1519 237644 : strops = 2;
1520 237644 : break;
1521 21330 : case BUILT_IN_STRSTR:
1522 21330 : strops = 2;
1523 21330 : strret = 1;
1524 21330 : break;
1525 42 : case BUILT_IN_ASAN_POINTER_COMPARE:
1526 42 : case BUILT_IN_ASAN_POINTER_SUBTRACT:
1527 : /* These builtins shall be ignored during constant expression
1528 : evaluation. */
1529 42 : return void_node;
1530 27 : case BUILT_IN_UNREACHABLE:
1531 27 : case BUILT_IN_TRAP:
1532 27 : if (!*non_constant_p && !ctx->quiet)
1533 : {
1534 : /* Do not allow__builtin_unreachable in constexpr function.
1535 : The __builtin_unreachable call with BUILTINS_LOCATION
1536 : comes from cp_maybe_instrument_return. */
1537 12 : if (EXPR_LOCATION (t) == BUILTINS_LOCATION)
1538 0 : error ("%<constexpr%> call flows off the end of the function");
1539 : else
1540 12 : error ("%q+E is not a constant expression", t);
1541 : }
1542 27 : *non_constant_p = true;
1543 27 : return t;
1544 : default:
1545 : break;
1546 : }
1547 :
1548 : /* Be permissive for arguments to built-ins; __builtin_constant_p should
1549 : return constant false for a non-constant argument. */
1550 5364751 : constexpr_ctx new_ctx = *ctx;
1551 5364751 : new_ctx.quiet = true;
1552 14680599 : for (i = 0; i < nargs; ++i)
1553 : {
1554 9315848 : tree arg = CALL_EXPR_ARG (t, i);
1555 9315848 : tree oarg = arg;
1556 :
1557 : /* To handle string built-ins we need to pass ADDR_EXPR<STRING_CST> since
1558 : expand_builtin doesn't know how to look in the values table. */
1559 9315848 : bool strop = i < strops;
1560 9315848 : if (strop)
1561 : {
1562 665707 : STRIP_NOPS (arg);
1563 665707 : if (TREE_CODE (arg) == ADDR_EXPR)
1564 230329 : arg = TREE_OPERAND (arg, 0);
1565 : else
1566 : strop = false;
1567 : }
1568 :
1569 : /* If builtin_valid_in_constant_expr_p is true,
1570 : potential_constant_expression_1 has not recursed into the arguments
1571 : of the builtin, verify it here. */
1572 9315848 : if (!builtin_valid_in_constant_expr_p (fun)
1573 9315848 : || potential_constant_expression (arg))
1574 : {
1575 9315718 : bool dummy1 = false, dummy2 = false;
1576 9315718 : arg = cxx_eval_constant_expression (&new_ctx, arg, vc_prvalue,
1577 : &dummy1, &dummy2);
1578 : }
1579 :
1580 9315848 : if (bi_const_p)
1581 : /* For __builtin_constant_p, fold all expressions with constant values
1582 : even if they aren't C++ constant-expressions. */
1583 213136 : arg = cp_fold_rvalue (arg);
1584 9102712 : else if (strop)
1585 : {
1586 230329 : if (TREE_CODE (arg) == CONSTRUCTOR)
1587 165 : arg = braced_lists_to_strings (TREE_TYPE (arg), arg);
1588 230329 : if (TREE_CODE (arg) == STRING_CST)
1589 225533 : arg = build_address (arg);
1590 : else
1591 : arg = oarg;
1592 : }
1593 :
1594 9315848 : args[i] = arg;
1595 : }
1596 :
1597 5364751 : bool save_ffbcp = force_folding_builtin_constant_p;
1598 5364751 : force_folding_builtin_constant_p |= ctx->manifestly_const_eval == mce_true;
1599 5364751 : tree save_cur_fn = current_function_decl;
1600 : /* Return name of ctx->call->fundef->decl for __builtin_FUNCTION (). */
1601 5364751 : if (fndecl_built_in_p (fun, BUILT_IN_FUNCTION)
1602 41 : && ctx->call
1603 5364754 : && ctx->call->fundef)
1604 3 : current_function_decl = ctx->call->fundef->decl;
1605 5364751 : if (fndecl_built_in_p (fun,
1606 : CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS,
1607 : BUILT_IN_FRONTEND))
1608 : {
1609 139 : location_t loc = EXPR_LOCATION (t);
1610 139 : if (nargs >= 1)
1611 138 : VERIFY_CONSTANT (args[0]);
1612 69 : new_call
1613 69 : = fold_builtin_is_pointer_inverconvertible_with_class (loc, nargs,
1614 : args);
1615 : }
1616 5364612 : else if (fndecl_built_in_p (fun,
1617 : CP_BUILT_IN_IS_CORRESPONDING_MEMBER,
1618 : BUILT_IN_FRONTEND))
1619 : {
1620 206 : location_t loc = EXPR_LOCATION (t);
1621 206 : if (nargs >= 2)
1622 : {
1623 204 : VERIFY_CONSTANT (args[0]);
1624 124 : VERIFY_CONSTANT (args[1]);
1625 : }
1626 126 : new_call = fold_builtin_is_corresponding_member (loc, nargs, args);
1627 : }
1628 : else
1629 10728812 : new_call = fold_builtin_call_array (EXPR_LOCATION (t), TREE_TYPE (t),
1630 5364406 : CALL_EXPR_FN (t), nargs, args);
1631 5364601 : current_function_decl = save_cur_fn;
1632 5364601 : force_folding_builtin_constant_p = save_ffbcp;
1633 5364601 : if (new_call == NULL)
1634 : {
1635 4168752 : if (!*non_constant_p && !ctx->quiet)
1636 : {
1637 0 : new_call = build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t),
1638 0 : CALL_EXPR_FN (t), nargs, args);
1639 0 : error ("%q+E is not a constant expression", new_call);
1640 : }
1641 4168752 : *non_constant_p = true;
1642 4168752 : return t;
1643 : }
1644 :
1645 1195849 : if (!potential_constant_expression (new_call))
1646 : {
1647 783 : if (!*non_constant_p && !ctx->quiet)
1648 3 : error ("%q+E is not a constant expression", new_call);
1649 783 : *non_constant_p = true;
1650 783 : return t;
1651 : }
1652 :
1653 1195066 : if (strret)
1654 : {
1655 : /* memchr returns a pointer into the first argument, but we replaced the
1656 : argument above with a STRING_CST; put it back it now. */
1657 218 : tree op = CALL_EXPR_ARG (t, strret-1);
1658 218 : STRIP_NOPS (new_call);
1659 218 : if (TREE_CODE (new_call) == POINTER_PLUS_EXPR)
1660 106 : TREE_OPERAND (new_call, 0) = op;
1661 112 : else if (TREE_CODE (new_call) == ADDR_EXPR)
1662 1195066 : new_call = op;
1663 : }
1664 :
1665 1195066 : return cxx_eval_constant_expression (&new_ctx, new_call, lval,
1666 1195066 : non_constant_p, overflow_p);
1667 : }
1668 :
1669 : /* TEMP is the constant value of a temporary object of type TYPE. Adjust
1670 : the type of the value to match. */
1671 :
1672 : static tree
1673 12453643 : adjust_temp_type (tree type, tree temp)
1674 : {
1675 12453643 : if (same_type_p (TREE_TYPE (temp), type))
1676 : return temp;
1677 : /* Avoid wrapping an aggregate value in a NOP_EXPR. */
1678 3636455 : if (TREE_CODE (temp) == CONSTRUCTOR)
1679 : {
1680 : /* build_constructor wouldn't retain various CONSTRUCTOR flags. */
1681 407325 : tree t = copy_node (temp);
1682 407325 : TREE_TYPE (t) = type;
1683 407325 : return t;
1684 : }
1685 3229130 : if (TREE_CODE (temp) == EMPTY_CLASS_EXPR)
1686 0 : return build0 (EMPTY_CLASS_EXPR, type);
1687 3229130 : gcc_assert (scalarish_type_p (type));
1688 : /* Now we know we're dealing with a scalar, and a prvalue of non-class
1689 : type is cv-unqualified. */
1690 3229130 : return cp_fold_convert (cv_unqualified (type), temp);
1691 : }
1692 :
1693 : /* If T is a CONSTRUCTOR, return an unshared copy of T and any
1694 : sub-CONSTRUCTORs. Otherwise return T.
1695 :
1696 : We use this whenever we initialize an object as a whole, whether it's a
1697 : parameter, a local variable, or a subobject, so that subsequent
1698 : modifications don't affect other places where it was used. */
1699 :
1700 : tree
1701 13116346 : unshare_constructor (tree t MEM_STAT_DECL)
1702 : {
1703 13116346 : if (!t || TREE_CODE (t) != CONSTRUCTOR)
1704 : return t;
1705 3154368 : auto_vec <tree*, 4> ptrs;
1706 3154368 : ptrs.safe_push (&t);
1707 3154368 : while (!ptrs.is_empty ())
1708 : {
1709 3567502 : tree *p = ptrs.pop ();
1710 3567502 : tree n = copy_node (*p PASS_MEM_STAT);
1711 4632776 : CONSTRUCTOR_ELTS (n) = vec_safe_copy (CONSTRUCTOR_ELTS (*p) PASS_MEM_STAT);
1712 3567502 : *p = n;
1713 3567502 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (n);
1714 3567502 : constructor_elt *ce;
1715 11602399 : for (HOST_WIDE_INT i = 0; vec_safe_iterate (v, i, &ce); ++i)
1716 1313027 : if (ce->value && TREE_CODE (ce->value) == CONSTRUCTOR)
1717 413134 : ptrs.safe_push (&ce->value);
1718 : }
1719 3154368 : return t;
1720 13116346 : }
1721 :
1722 : /* If T is a CONSTRUCTOR, ggc_free T and any sub-CONSTRUCTORs. */
1723 :
1724 : static void
1725 453319 : free_constructor (tree t)
1726 : {
1727 453319 : if (!t || TREE_CODE (t) != CONSTRUCTOR)
1728 0 : return;
1729 453319 : releasing_vec ctors;
1730 453319 : vec_safe_push (ctors, t);
1731 908459 : while (!ctors->is_empty ())
1732 : {
1733 455140 : tree c = ctors->pop ();
1734 455140 : if (vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (c))
1735 : {
1736 : constructor_elt *ce;
1737 12424 : for (HOST_WIDE_INT i = 0; vec_safe_iterate (elts, i, &ce); ++i)
1738 6696 : if (TREE_CODE (ce->value) == CONSTRUCTOR)
1739 6696 : vec_safe_push (ctors, ce->value);
1740 5728 : ggc_free (elts);
1741 : }
1742 455140 : ggc_free (c);
1743 : }
1744 453319 : }
1745 :
1746 : /* Helper function of cxx_bind_parameters_in_call. Return non-NULL
1747 : if *TP is address of a static variable (or part of it) currently being
1748 : constructed or of a heap artificial variable. */
1749 :
1750 : static tree
1751 4720545 : addr_of_non_const_var (tree *tp, int *walk_subtrees, void *data)
1752 : {
1753 4720545 : if (TREE_CODE (*tp) == ADDR_EXPR)
1754 182239 : if (tree var = get_base_address (TREE_OPERAND (*tp, 0)))
1755 182239 : if (VAR_P (var) && TREE_STATIC (var))
1756 : {
1757 140410 : if (DECL_NAME (var) == heap_uninit_identifier
1758 140410 : || DECL_NAME (var) == heap_identifier
1759 102836 : || DECL_NAME (var) == heap_vec_uninit_identifier
1760 243246 : || DECL_NAME (var) == heap_vec_identifier)
1761 : return var;
1762 :
1763 102833 : constexpr_global_ctx *global = (constexpr_global_ctx *) data;
1764 205666 : if (global->get_value (var))
1765 : return var;
1766 : }
1767 4639402 : if (TYPE_P (*tp))
1768 653 : *walk_subtrees = false;
1769 : return NULL_TREE;
1770 : }
1771 :
1772 : /* Subroutine of cxx_eval_call_expression.
1773 : We are processing a call expression (either CALL_EXPR or
1774 : AGGR_INIT_EXPR) in the context of CTX. Evaluate
1775 : all arguments and bind their values to correspondings
1776 : parameters, making up the NEW_CALL context. */
1777 :
1778 : static tree
1779 28623062 : cxx_bind_parameters_in_call (const constexpr_ctx *ctx, tree t, tree fun,
1780 : bool *non_constant_p, bool *overflow_p,
1781 : bool *non_constant_args)
1782 : {
1783 28623062 : const int nargs = call_expr_nargs (t);
1784 28623062 : tree parms = DECL_ARGUMENTS (fun);
1785 28623062 : int i;
1786 : /* We don't record ellipsis args below. */
1787 28623062 : int nparms = list_length (parms);
1788 28623062 : int nbinds = nargs < nparms ? nargs : nparms;
1789 28623062 : tree binds = make_tree_vec (nbinds);
1790 38334435 : for (i = 0; i < nargs; ++i)
1791 : {
1792 20361357 : tree x, arg;
1793 20361357 : tree type = parms ? TREE_TYPE (parms) : void_type_node;
1794 20361357 : if (parms && DECL_BY_REFERENCE (parms))
1795 3649 : type = TREE_TYPE (type);
1796 20361357 : x = get_nth_callarg (t, i);
1797 : /* For member function, the first argument is a pointer to the implied
1798 : object. For a constructor, it might still be a dummy object, in
1799 : which case we get the real argument from ctx. */
1800 34613384 : if (i == 0 && DECL_CONSTRUCTOR_P (fun)
1801 21940535 : && is_dummy_object (x))
1802 : {
1803 483449 : x = ctx->object;
1804 483449 : x = build_address (x);
1805 : }
1806 20361357 : if (TREE_ADDRESSABLE (type))
1807 : /* Undo convert_for_arg_passing work here. */
1808 4337 : x = convert_from_reference (x);
1809 : /* Normally we would strip a TARGET_EXPR in an initialization context
1810 : such as this, but here we do the elision differently: we keep the
1811 : TARGET_EXPR, and use its CONSTRUCTOR as the value of the parm. */
1812 20361357 : arg = cxx_eval_constant_expression (ctx, x, vc_prvalue,
1813 : non_constant_p, overflow_p);
1814 : /* Don't VERIFY_CONSTANT here. */
1815 20361357 : if (*non_constant_p && ctx->quiet)
1816 : break;
1817 : /* Just discard ellipsis args after checking their constantitude. */
1818 9711373 : if (!parms)
1819 474 : continue;
1820 :
1821 9710899 : if (!*non_constant_p)
1822 : {
1823 : /* Make sure the binding has the same type as the parm. But
1824 : only for constant args. */
1825 9710842 : if (!TYPE_REF_P (type))
1826 7747930 : arg = adjust_temp_type (type, arg);
1827 9710842 : if (!TREE_CONSTANT (arg))
1828 4864093 : *non_constant_args = true;
1829 4846749 : else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
1830 : /* The destructor needs to see any modifications the callee makes
1831 : to the argument. */
1832 66 : *non_constant_args = true;
1833 : /* If arg is or contains address of a heap artificial variable or
1834 : of a static variable being constructed, avoid caching the
1835 : function call, as those variables might be modified by the
1836 : function, or might be modified by the callers in between
1837 : the cached function and just read by the function. */
1838 4846683 : else if (!*non_constant_args
1839 4846683 : && cp_walk_tree (&arg, addr_of_non_const_var, ctx->global,
1840 : NULL))
1841 81143 : *non_constant_args = true;
1842 :
1843 : /* For virtual calls, adjust the this argument, so that it is
1844 : the object on which the method is called, rather than
1845 : one of its bases. */
1846 9710842 : if (i == 0 && DECL_VIRTUAL_P (fun))
1847 : {
1848 1341 : tree addr = arg;
1849 1341 : STRIP_NOPS (addr);
1850 1341 : if (TREE_CODE (addr) == ADDR_EXPR)
1851 : {
1852 1339 : tree obj = TREE_OPERAND (addr, 0);
1853 1339 : while (TREE_CODE (obj) == COMPONENT_REF
1854 530 : && DECL_FIELD_IS_BASE (TREE_OPERAND (obj, 1))
1855 1859 : && !same_type_ignoring_top_level_qualifiers_p
1856 511 : (TREE_TYPE (obj), DECL_CONTEXT (fun)))
1857 9 : obj = TREE_OPERAND (obj, 0);
1858 1339 : if (obj != TREE_OPERAND (addr, 0))
1859 9 : arg = build_fold_addr_expr_with_type (obj,
1860 : TREE_TYPE (arg));
1861 : }
1862 : }
1863 9710842 : TREE_VEC_ELT (binds, i) = arg;
1864 : }
1865 9710899 : parms = TREE_CHAIN (parms);
1866 : }
1867 :
1868 28623062 : return binds;
1869 : }
1870 :
1871 : /* Variables and functions to manage constexpr call expansion context.
1872 : These do not need to be marked for PCH or GC. */
1873 :
1874 : /* FIXME remember and print actual constant arguments. */
1875 : static vec<tree> call_stack;
1876 : static int call_stack_tick;
1877 : static int last_cx_error_tick;
1878 :
1879 : static int
1880 17751966 : push_cx_call_context (tree call)
1881 : {
1882 17751966 : ++call_stack_tick;
1883 17751966 : if (!EXPR_HAS_LOCATION (call))
1884 278906 : SET_EXPR_LOCATION (call, input_location);
1885 17751966 : call_stack.safe_push (call);
1886 17751966 : int len = call_stack.length ();
1887 17751966 : if (len > max_constexpr_depth)
1888 21 : return false;
1889 : return len;
1890 : }
1891 :
1892 : static void
1893 17751966 : pop_cx_call_context (void)
1894 : {
1895 17751966 : ++call_stack_tick;
1896 17751966 : call_stack.pop ();
1897 17751966 : }
1898 :
1899 : vec<tree>
1900 177578 : cx_error_context (void)
1901 : {
1902 177578 : vec<tree> r = vNULL;
1903 177578 : if (call_stack_tick != last_cx_error_tick
1904 177578 : && !call_stack.is_empty ())
1905 : r = call_stack;
1906 177578 : last_cx_error_tick = call_stack_tick;
1907 177578 : return r;
1908 : }
1909 :
1910 : /* E is an operand of a failed assertion, fold it either with or without
1911 : constexpr context. */
1912 :
1913 : static tree
1914 479 : fold_operand (tree e, const constexpr_ctx *ctx)
1915 : {
1916 479 : if (ctx)
1917 : {
1918 81 : bool new_non_constant_p = false, new_overflow_p = false;
1919 81 : e = cxx_eval_constant_expression (ctx, e, vc_prvalue,
1920 : &new_non_constant_p,
1921 : &new_overflow_p);
1922 : }
1923 : else
1924 398 : e = fold_non_dependent_expr (e, tf_none, /*manifestly_const_eval=*/true);
1925 479 : return e;
1926 : }
1927 :
1928 : /* If we have a condition in conjunctive normal form (CNF), find the first
1929 : failing clause. In other words, given an expression like
1930 :
1931 : true && true && false && true && false
1932 :
1933 : return the first 'false'. EXPR is the expression. */
1934 :
1935 : static tree
1936 284 : find_failing_clause_r (const constexpr_ctx *ctx, tree expr)
1937 : {
1938 365 : if (TREE_CODE (expr) == TRUTH_ANDIF_EXPR)
1939 : {
1940 : /* First check the left side... */
1941 162 : tree e = find_failing_clause_r (ctx, TREE_OPERAND (expr, 0));
1942 162 : if (e == NULL_TREE)
1943 : /* ...if we didn't find a false clause, check the right side. */
1944 81 : e = find_failing_clause_r (ctx, TREE_OPERAND (expr, 1));
1945 81 : return e;
1946 : }
1947 203 : tree e = contextual_conv_bool (expr, tf_none);
1948 203 : e = fold_operand (e, ctx);
1949 203 : if (integer_zerop (e))
1950 : /* This is the failing clause. */
1951 122 : return expr;
1952 : return NULL_TREE;
1953 : }
1954 :
1955 : /* Wrapper for find_failing_clause_r. */
1956 :
1957 : tree
1958 673 : find_failing_clause (const constexpr_ctx *ctx, tree expr)
1959 : {
1960 673 : if (TREE_CODE (expr) == TRUTH_ANDIF_EXPR)
1961 122 : if (tree e = find_failing_clause_r (ctx, expr))
1962 673 : expr = e;
1963 673 : return expr;
1964 : }
1965 :
1966 : /* Emit additional diagnostics for failing condition BAD.
1967 : Used by finish_static_assert and IFN_ASSUME constexpr diagnostics.
1968 : If SHOW_EXPR_P is true, print the condition (because it was
1969 : instantiation-dependent). */
1970 :
1971 : void
1972 673 : diagnose_failing_condition (tree bad, location_t cloc, bool show_expr_p,
1973 : const constexpr_ctx *ctx /* = nullptr */)
1974 : {
1975 : /* Nobody wants to see the artificial (bool) cast. */
1976 673 : bad = tree_strip_nop_conversions (bad);
1977 673 : if (TREE_CODE (bad) == CLEANUP_POINT_EXPR)
1978 3 : bad = TREE_OPERAND (bad, 0);
1979 :
1980 : /* Actually explain the failure if this is a concept check or a
1981 : requires-expression. */
1982 673 : if (concept_check_p (bad) || TREE_CODE (bad) == REQUIRES_EXPR)
1983 68 : diagnose_constraints (cloc, bad, NULL_TREE);
1984 605 : else if (COMPARISON_CLASS_P (bad)
1985 605 : && ARITHMETIC_TYPE_P (TREE_TYPE (TREE_OPERAND (bad, 0))))
1986 : {
1987 138 : tree op0 = fold_operand (TREE_OPERAND (bad, 0), ctx);
1988 138 : tree op1 = fold_operand (TREE_OPERAND (bad, 1), ctx);
1989 138 : tree cond = build2 (TREE_CODE (bad), boolean_type_node, op0, op1);
1990 138 : inform (cloc, "the comparison reduces to %qE", cond);
1991 : }
1992 467 : else if (show_expr_p)
1993 397 : inform (cloc, "%qE evaluates to false", bad);
1994 673 : }
1995 :
1996 : /* Process an assert/assume of ORIG_ARG. If it's not supposed to be evaluated,
1997 : do it without changing the current evaluation state. If it evaluates to
1998 : false, complain and return false; otherwise, return true. */
1999 :
2000 : static bool
2001 221 : cxx_eval_assert (const constexpr_ctx *ctx, tree arg, const char *msg,
2002 : location_t loc, bool evaluated,
2003 : bool *non_constant_p, bool *overflow_p)
2004 : {
2005 221 : if (*non_constant_p)
2006 : return true;
2007 :
2008 221 : tree eval;
2009 221 : if (!evaluated)
2010 : {
2011 150 : if (!potential_rvalue_constant_expression (arg))
2012 56 : return true;
2013 :
2014 94 : constexpr_ctx new_ctx = *ctx;
2015 94 : new_ctx.quiet = true;
2016 94 : bool new_non_constant_p = false, new_overflow_p = false;
2017 : /* Avoid modification of existing values. */
2018 94 : modifiable_tracker ms (new_ctx.global);
2019 94 : eval = cxx_eval_constant_expression (&new_ctx, arg, vc_prvalue,
2020 : &new_non_constant_p,
2021 : &new_overflow_p);
2022 94 : }
2023 : else
2024 71 : eval = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
2025 : non_constant_p,
2026 : overflow_p);
2027 165 : if (!*non_constant_p && integer_zerop (eval))
2028 : {
2029 110 : if (!ctx->quiet)
2030 : {
2031 : /* See if we can find which clause was failing
2032 : (for logical AND). */
2033 36 : tree bad = find_failing_clause (ctx, arg);
2034 : /* If not, or its location is unusable, fall back to the
2035 : previous location. */
2036 36 : location_t cloc = cp_expr_loc_or_loc (bad, loc);
2037 :
2038 : /* Report the error. */
2039 36 : auto_diagnostic_group d;
2040 36 : error_at (cloc, msg);
2041 36 : diagnose_failing_condition (bad, cloc, true, ctx);
2042 36 : return bad;
2043 36 : }
2044 74 : *non_constant_p = true;
2045 74 : return false;
2046 : }
2047 :
2048 : return true;
2049 : }
2050 :
2051 : /* Evaluate a call T to a GCC internal function when possible and return
2052 : the evaluated result or, under the control of CTX, give an error, set
2053 : NON_CONSTANT_P, and return the unevaluated call T otherwise. */
2054 :
2055 : static tree
2056 9093 : cxx_eval_internal_function (const constexpr_ctx *ctx, tree t,
2057 : value_cat lval,
2058 : bool *non_constant_p, bool *overflow_p)
2059 : {
2060 9093 : enum tree_code opcode = ERROR_MARK;
2061 :
2062 9093 : switch (CALL_EXPR_IFN (t))
2063 : {
2064 6 : case IFN_UBSAN_NULL:
2065 6 : case IFN_UBSAN_BOUNDS:
2066 6 : case IFN_UBSAN_VPTR:
2067 6 : case IFN_FALLTHROUGH:
2068 6 : return void_node;
2069 :
2070 135 : case IFN_ASSUME:
2071 135 : if (!cxx_eval_assert (ctx, CALL_EXPR_ARG (t, 0),
2072 : G_("failed %<assume%> attribute assumption"),
2073 135 : EXPR_LOCATION (t), /*eval*/false,
2074 : non_constant_p, overflow_p))
2075 : return t;
2076 99 : return void_node;
2077 :
2078 : case IFN_ADD_OVERFLOW:
2079 : opcode = PLUS_EXPR;
2080 : break;
2081 320 : case IFN_SUB_OVERFLOW:
2082 320 : opcode = MINUS_EXPR;
2083 320 : break;
2084 8034 : case IFN_MUL_OVERFLOW:
2085 8034 : opcode = MULT_EXPR;
2086 8034 : break;
2087 :
2088 66 : case IFN_LAUNDER:
2089 66 : return cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0),
2090 : vc_prvalue, non_constant_p,
2091 66 : overflow_p);
2092 :
2093 38 : case IFN_VEC_CONVERT:
2094 38 : {
2095 38 : tree arg = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0),
2096 : vc_prvalue, non_constant_p,
2097 : overflow_p);
2098 38 : if (TREE_CODE (arg) == VECTOR_CST)
2099 24 : if (tree r = fold_const_call (CFN_VEC_CONVERT, TREE_TYPE (t), arg))
2100 : return r;
2101 : }
2102 : /* FALLTHRU */
2103 :
2104 21 : default:
2105 21 : if (!ctx->quiet)
2106 0 : error_at (cp_expr_loc_or_input_loc (t),
2107 : "call to internal function %qE", t);
2108 21 : *non_constant_p = true;
2109 21 : return t;
2110 : }
2111 :
2112 : /* Evaluate constant arguments using OPCODE and return a complex
2113 : number containing the result and the overflow bit. */
2114 8848 : tree arg0 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), lval,
2115 : non_constant_p, overflow_p);
2116 8848 : tree arg1 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 1), lval,
2117 : non_constant_p, overflow_p);
2118 :
2119 8848 : if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2120 : {
2121 3 : location_t loc = cp_expr_loc_or_input_loc (t);
2122 3 : tree type = TREE_TYPE (TREE_TYPE (t));
2123 3 : tree result = fold_binary_loc (loc, opcode, type,
2124 : fold_convert_loc (loc, type, arg0),
2125 : fold_convert_loc (loc, type, arg1));
2126 3 : tree ovf
2127 3 : = build_int_cst (type, arith_overflowed_p (opcode, type, arg0, arg1));
2128 : /* Reset TREE_OVERFLOW to avoid warnings for the overflow. */
2129 3 : if (TREE_OVERFLOW (result))
2130 0 : TREE_OVERFLOW (result) = 0;
2131 :
2132 3 : return build_complex (TREE_TYPE (t), result, ovf);
2133 : }
2134 :
2135 8845 : *non_constant_p = true;
2136 8845 : return t;
2137 : }
2138 :
2139 : /* Clean CONSTRUCTOR_NO_CLEARING from CTOR and its sub-aggregates. */
2140 :
2141 : static void
2142 1996211 : clear_no_implicit_zero (tree ctor)
2143 : {
2144 1996211 : if (CONSTRUCTOR_NO_CLEARING (ctor))
2145 : {
2146 447476 : CONSTRUCTOR_NO_CLEARING (ctor) = false;
2147 2108096 : for (auto &e: CONSTRUCTOR_ELTS (ctor))
2148 779988 : if (TREE_CODE (e.value) == CONSTRUCTOR)
2149 161366 : clear_no_implicit_zero (e.value);
2150 : }
2151 1996211 : }
2152 :
2153 : /* Complain about a const object OBJ being modified in a constant expression.
2154 : EXPR is the MODIFY_EXPR expression performing the modification. */
2155 :
2156 : static void
2157 63 : modifying_const_object_error (tree expr, tree obj)
2158 : {
2159 63 : location_t loc = cp_expr_loc_or_input_loc (expr);
2160 63 : auto_diagnostic_group d;
2161 63 : error_at (loc, "modifying a const object %qE is not allowed in "
2162 63 : "a constant expression", TREE_OPERAND (expr, 0));
2163 63 : inform (location_of (obj), "originally declared %<const%> here");
2164 63 : }
2165 :
2166 : /* Return true if FNDECL is a replaceable global allocation function that
2167 : should be useable during constant expression evaluation. */
2168 :
2169 : static inline bool
2170 19871563 : cxx_replaceable_global_alloc_fn (tree fndecl)
2171 : {
2172 19871563 : return (cxx_dialect >= cxx20
2173 619902 : && IDENTIFIER_NEWDEL_OP_P (DECL_NAME (fndecl))
2174 39443 : && CP_DECL_CONTEXT (fndecl) == global_namespace
2175 19910806 : && (DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl)
2176 17522 : || DECL_IS_OPERATOR_DELETE_P (fndecl)));
2177 : }
2178 :
2179 : /* Return true if FNDECL is a placement new function that should be
2180 : useable during constant expression evaluation of std::construct_at. */
2181 :
2182 : static inline bool
2183 19857209 : cxx_placement_new_fn (tree fndecl)
2184 : {
2185 19857209 : if (cxx_dialect >= cxx20
2186 605548 : && IDENTIFIER_NEW_OP_P (DECL_NAME (fndecl))
2187 25087 : && CP_DECL_CONTEXT (fndecl) == global_namespace
2188 24887 : && !DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl)
2189 19870346 : && TREE_CODE (TREE_TYPE (fndecl)) == FUNCTION_TYPE)
2190 : {
2191 13137 : tree first_arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
2192 13137 : if (TREE_VALUE (first_arg) == ptr_type_node
2193 13137 : && TREE_CHAIN (first_arg) == void_list_node)
2194 12607 : return true;
2195 : }
2196 : return false;
2197 : }
2198 :
2199 : /* Return true if FNDECL is std::construct_at. */
2200 :
2201 : static inline bool
2202 22770 : is_std_construct_at (tree fndecl)
2203 : {
2204 22770 : if (!decl_in_std_namespace_p (fndecl))
2205 : return false;
2206 :
2207 21479 : tree name = DECL_NAME (fndecl);
2208 21479 : return name && id_equal (name, "construct_at");
2209 : }
2210 :
2211 : /* Overload for the above taking constexpr_call*. */
2212 :
2213 : static inline bool
2214 15390 : is_std_construct_at (const constexpr_call *call)
2215 : {
2216 15390 : return (call
2217 14978 : && call->fundef
2218 30368 : && is_std_construct_at (call->fundef->decl));
2219 : }
2220 :
2221 : /* True if CTX is an instance of std::allocator. */
2222 :
2223 : bool
2224 23406 : is_std_allocator (tree ctx)
2225 : {
2226 23406 : if (ctx == NULL_TREE || !CLASS_TYPE_P (ctx) || !TYPE_MAIN_DECL (ctx))
2227 : return false;
2228 :
2229 23044 : tree decl = TYPE_MAIN_DECL (ctx);
2230 23044 : tree name = DECL_NAME (decl);
2231 23044 : if (name == NULL_TREE || !id_equal (name, "allocator"))
2232 : return false;
2233 :
2234 15264 : return decl_in_std_namespace_p (decl);
2235 : }
2236 :
2237 : /* Return true if FNDECL is std::allocator<T>::{,de}allocate. */
2238 :
2239 : static inline bool
2240 20612 : is_std_allocator_allocate (tree fndecl)
2241 : {
2242 20612 : tree name = DECL_NAME (fndecl);
2243 20612 : if (name == NULL_TREE
2244 20612 : || !(id_equal (name, "allocate") || id_equal (name, "deallocate")))
2245 : return false;
2246 :
2247 20118 : return is_std_allocator (DECL_CONTEXT (fndecl));
2248 : }
2249 :
2250 : /* Overload for the above taking constexpr_call*. */
2251 :
2252 : static inline bool
2253 5346 : is_std_allocator_allocate (const constexpr_call *call)
2254 : {
2255 5346 : return (call
2256 1088 : && call->fundef
2257 6434 : && is_std_allocator_allocate (call->fundef->decl));
2258 : }
2259 :
2260 : /* Return true if FNDECL is __dynamic_cast. */
2261 :
2262 : static inline bool
2263 19893565 : cxx_dynamic_cast_fn_p (tree fndecl)
2264 : {
2265 19893565 : return (cxx_dialect >= cxx20
2266 594086 : && id_equal (DECL_NAME (fndecl), "__dynamic_cast")
2267 19895082 : && CP_DECL_CONTEXT (fndecl) == abi_node);
2268 : }
2269 :
2270 : /* Often, we have an expression in the form of address + offset, e.g.
2271 : "&_ZTV1A + 16". Extract the object from it, i.e. "_ZTV1A". */
2272 :
2273 : static tree
2274 1157 : extract_obj_from_addr_offset (tree expr)
2275 : {
2276 1157 : if (TREE_CODE (expr) == POINTER_PLUS_EXPR)
2277 531 : expr = TREE_OPERAND (expr, 0);
2278 1157 : STRIP_NOPS (expr);
2279 1157 : if (TREE_CODE (expr) == ADDR_EXPR)
2280 1157 : expr = TREE_OPERAND (expr, 0);
2281 1157 : return expr;
2282 : }
2283 :
2284 : /* Given a PATH like
2285 :
2286 : g.D.2181.D.2154.D.2102.D.2093
2287 :
2288 : find a component with type TYPE. Return NULL_TREE if not found, and
2289 : error_mark_node if the component is not accessible. If STOP is non-null,
2290 : this function will return NULL_TREE if STOP is found before TYPE. */
2291 :
2292 : static tree
2293 546 : get_component_with_type (tree path, tree type, tree stop)
2294 : {
2295 1710 : while (true)
2296 : {
2297 1128 : if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path), type))
2298 : /* Found it. */
2299 252 : return path;
2300 876 : else if (stop
2301 876 : && (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path),
2302 : stop)))
2303 : return NULL_TREE;
2304 861 : else if (TREE_CODE (path) == COMPONENT_REF
2305 861 : && DECL_FIELD_IS_BASE (TREE_OPERAND (path, 1)))
2306 : {
2307 : /* We need to check that the component we're accessing is in fact
2308 : accessible. */
2309 861 : if (TREE_PRIVATE (TREE_OPERAND (path, 1))
2310 861 : || TREE_PROTECTED (TREE_OPERAND (path, 1)))
2311 279 : return error_mark_node;
2312 582 : path = TREE_OPERAND (path, 0);
2313 : }
2314 : else
2315 : return NULL_TREE;
2316 : }
2317 : }
2318 :
2319 : /* Evaluate a call to __dynamic_cast (permitted by P1327R1).
2320 :
2321 : The declaration of __dynamic_cast is:
2322 :
2323 : void* __dynamic_cast (const void* __src_ptr,
2324 : const __class_type_info* __src_type,
2325 : const __class_type_info* __dst_type,
2326 : ptrdiff_t __src2dst);
2327 :
2328 : where src2dst has the following possible values
2329 :
2330 : >-1: src_type is a unique public non-virtual base of dst_type
2331 : dst_ptr + src2dst == src_ptr
2332 : -1: unspecified relationship
2333 : -2: src_type is not a public base of dst_type
2334 : -3: src_type is a multiple public non-virtual base of dst_type
2335 :
2336 : Since literal types can't have virtual bases, we only expect hint >=0,
2337 : -2, or -3. */
2338 :
2339 : static tree
2340 681 : cxx_eval_dynamic_cast_fn (const constexpr_ctx *ctx, tree call,
2341 : bool *non_constant_p, bool *overflow_p)
2342 : {
2343 : /* T will be something like
2344 : __dynamic_cast ((B*) b, &_ZTI1B, &_ZTI1D, 8)
2345 : dismantle it. */
2346 681 : gcc_assert (call_expr_nargs (call) == 4);
2347 681 : tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
2348 681 : tree obj = CALL_EXPR_ARG (call, 0);
2349 681 : tree type = CALL_EXPR_ARG (call, 2);
2350 681 : HOST_WIDE_INT hint = int_cst_value (CALL_EXPR_ARG (call, 3));
2351 681 : location_t loc = cp_expr_loc_or_input_loc (call);
2352 :
2353 : /* Get the target type of the dynamic_cast. */
2354 681 : gcc_assert (TREE_CODE (type) == ADDR_EXPR);
2355 681 : type = TREE_OPERAND (type, 0);
2356 681 : type = TREE_TYPE (DECL_NAME (type));
2357 :
2358 : /* TYPE can only be either T* or T&. We can't know which of these it
2359 : is by looking at TYPE, but OBJ will be "(T*) x" in the first case,
2360 : and something like "(T*)(T&)(T*) x" in the second case. */
2361 681 : bool reference_p = false;
2362 3589 : while (CONVERT_EXPR_P (obj) || TREE_CODE (obj) == SAVE_EXPR)
2363 : {
2364 2908 : reference_p |= TYPE_REF_P (TREE_TYPE (obj));
2365 2908 : obj = TREE_OPERAND (obj, 0);
2366 : }
2367 :
2368 : /* Evaluate the object so that we know its dynamic type. */
2369 681 : obj = cxx_eval_constant_expression (ctx, obj, vc_prvalue, non_constant_p,
2370 : overflow_p);
2371 681 : if (*non_constant_p)
2372 : return call;
2373 :
2374 : /* We expect OBJ to be in form of &d.D.2102 when HINT == 0,
2375 : but when HINT is > 0, it can also be something like
2376 : &d.D.2102 + 18446744073709551608, which includes the BINFO_OFFSET. */
2377 626 : obj = extract_obj_from_addr_offset (obj);
2378 626 : const tree objtype = TREE_TYPE (obj);
2379 : /* If OBJ doesn't refer to a base field, we're done. */
2380 1246 : if (tree t = (TREE_CODE (obj) == COMPONENT_REF
2381 626 : ? TREE_OPERAND (obj, 1) : obj))
2382 626 : if (TREE_CODE (t) != FIELD_DECL || !DECL_FIELD_IS_BASE (t))
2383 : {
2384 6 : if (reference_p)
2385 : {
2386 4 : if (!ctx->quiet)
2387 : {
2388 1 : error_at (loc, "reference %<dynamic_cast%> failed");
2389 1 : inform (loc, "dynamic type %qT of its operand does "
2390 : "not have a base class of type %qT",
2391 : objtype, type);
2392 : }
2393 4 : *non_constant_p = true;
2394 : }
2395 6 : return integer_zero_node;
2396 : }
2397 :
2398 : /* [class.cdtor] When a dynamic_cast is used in a constructor ...
2399 : or in a destructor ... if the operand of the dynamic_cast refers
2400 : to the object under construction or destruction, this object is
2401 : considered to be a most derived object that has the type of the
2402 : constructor or destructor's class. */
2403 620 : tree vtable = build_vfield_ref (obj, objtype);
2404 620 : vtable = cxx_eval_constant_expression (ctx, vtable, vc_prvalue,
2405 : non_constant_p, overflow_p);
2406 620 : if (*non_constant_p)
2407 : return call;
2408 : /* With -fsanitize=vptr, we initialize all vtable pointers to null,
2409 : so it's possible that we got a null pointer now. */
2410 535 : if (integer_zerop (vtable))
2411 : {
2412 4 : if (!ctx->quiet)
2413 1 : error_at (loc, "virtual table pointer is used uninitialized");
2414 4 : *non_constant_p = true;
2415 4 : return integer_zero_node;
2416 : }
2417 : /* VTABLE will be &_ZTV1A + 16 or similar, get _ZTV1A. */
2418 531 : vtable = extract_obj_from_addr_offset (vtable);
2419 531 : const tree mdtype = DECL_CONTEXT (vtable);
2420 :
2421 : /* Given dynamic_cast<T>(v),
2422 :
2423 : [expr.dynamic.cast] If C is the class type to which T points or refers,
2424 : the runtime check logically executes as follows:
2425 :
2426 : If, in the most derived object pointed (referred) to by v, v points
2427 : (refers) to a public base class subobject of a C object, and if only
2428 : one object of type C is derived from the subobject pointed (referred)
2429 : to by v the result points (refers) to that C object.
2430 :
2431 : In this case, HINT >= 0 or -3. */
2432 531 : if (hint >= 0 || hint == -3)
2433 : {
2434 : /* Look for a component with type TYPE. */
2435 222 : tree t = get_component_with_type (obj, type, mdtype);
2436 : /* If not accessible, give an error. */
2437 222 : if (t == error_mark_node)
2438 : {
2439 66 : if (reference_p)
2440 : {
2441 54 : if (!ctx->quiet)
2442 : {
2443 6 : error_at (loc, "reference %<dynamic_cast%> failed");
2444 6 : inform (loc, "static type %qT of its operand is a "
2445 : "non-public base class of dynamic type %qT",
2446 : objtype, type);
2447 :
2448 : }
2449 54 : *non_constant_p = true;
2450 : }
2451 66 : return integer_zero_node;
2452 : }
2453 156 : else if (t)
2454 : /* The result points to the TYPE object. */
2455 141 : return cp_build_addr_expr (t, complain);
2456 : /* Else, TYPE was not found, because the HINT turned out to be wrong.
2457 : Fall through to the normal processing. */
2458 : }
2459 :
2460 : /* Otherwise, if v points (refers) to a public base class subobject of the
2461 : most derived object, and the type of the most derived object has a base
2462 : class, of type C, that is unambiguous and public, the result points
2463 : (refers) to the C subobject of the most derived object.
2464 :
2465 : But it can also be an invalid case. */
2466 :
2467 : /* Get the most derived object. */
2468 324 : obj = get_component_with_type (obj, mdtype, NULL_TREE);
2469 324 : if (obj == error_mark_node)
2470 : {
2471 213 : if (reference_p)
2472 : {
2473 175 : if (!ctx->quiet)
2474 : {
2475 19 : error_at (loc, "reference %<dynamic_cast%> failed");
2476 19 : inform (loc, "static type %qT of its operand is a non-public"
2477 : " base class of dynamic type %qT", objtype, mdtype);
2478 : }
2479 175 : *non_constant_p = true;
2480 : }
2481 213 : return integer_zero_node;
2482 : }
2483 : else
2484 111 : gcc_assert (obj);
2485 :
2486 : /* Check that the type of the most derived object has a base class
2487 : of type TYPE that is unambiguous and public. */
2488 111 : base_kind b_kind;
2489 111 : tree binfo = lookup_base (mdtype, type, ba_check, &b_kind, tf_none);
2490 111 : if (!binfo || binfo == error_mark_node)
2491 : {
2492 80 : if (reference_p)
2493 : {
2494 68 : if (!ctx->quiet)
2495 : {
2496 8 : error_at (loc, "reference %<dynamic_cast%> failed");
2497 8 : if (b_kind == bk_ambig)
2498 3 : inform (loc, "%qT is an ambiguous base class of dynamic "
2499 : "type %qT of its operand", type, mdtype);
2500 : else
2501 5 : inform (loc, "dynamic type %qT of its operand does not "
2502 : "have an unambiguous public base class %qT",
2503 : mdtype, type);
2504 : }
2505 68 : *non_constant_p = true;
2506 : }
2507 80 : return integer_zero_node;
2508 : }
2509 : /* If so, return the TYPE subobject of the most derived object. */
2510 31 : obj = convert_to_base_statically (obj, binfo);
2511 31 : return cp_build_addr_expr (obj, complain);
2512 : }
2513 :
2514 : /* Data structure used by replace_decl and replace_decl_r. */
2515 :
2516 : struct replace_decl_data
2517 : {
2518 : /* The _DECL we want to replace. */
2519 : tree decl;
2520 : /* The replacement for DECL. */
2521 : tree replacement;
2522 : /* Trees we've visited. */
2523 : hash_set<tree> *pset;
2524 : /* Whether we've performed any replacements. */
2525 : bool changed;
2526 : };
2527 :
2528 : /* Helper function for replace_decl, called through cp_walk_tree. */
2529 :
2530 : static tree
2531 777462 : replace_decl_r (tree *tp, int *walk_subtrees, void *data)
2532 : {
2533 777462 : replace_decl_data *d = (replace_decl_data *) data;
2534 :
2535 777462 : if (*tp == d->decl)
2536 : {
2537 428 : *tp = unshare_expr (d->replacement);
2538 428 : d->changed = true;
2539 428 : *walk_subtrees = 0;
2540 : }
2541 777034 : else if (TYPE_P (*tp)
2542 777034 : || d->pset->add (*tp))
2543 50607 : *walk_subtrees = 0;
2544 :
2545 777462 : return NULL_TREE;
2546 : }
2547 :
2548 : /* Replace every occurrence of DECL with (an unshared copy of)
2549 : REPLACEMENT within the expression *TP. Returns true iff a
2550 : replacement was performed. */
2551 :
2552 : bool
2553 137800 : replace_decl (tree *tp, tree decl, tree replacement)
2554 : {
2555 137800 : gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
2556 : (TREE_TYPE (decl), TREE_TYPE (replacement)));
2557 137800 : hash_set<tree> pset;
2558 137800 : replace_decl_data data = { decl, replacement, &pset, false };
2559 137800 : cp_walk_tree (tp, replace_decl_r, &data, NULL);
2560 137800 : return data.changed;
2561 137800 : }
2562 :
2563 : /* Evaluate the call T to virtual function thunk THUNK_FNDECL. */
2564 :
2565 : static tree
2566 9 : cxx_eval_thunk_call (const constexpr_ctx *ctx, tree t, tree thunk_fndecl,
2567 : value_cat lval,
2568 : bool *non_constant_p, bool *overflow_p)
2569 : {
2570 9 : tree function = THUNK_TARGET (thunk_fndecl);
2571 :
2572 9 : if (THUNK_VIRTUAL_OFFSET (thunk_fndecl))
2573 : {
2574 5 : if (!ctx->quiet)
2575 : {
2576 1 : if (!DECL_DECLARED_CONSTEXPR_P (function))
2577 : {
2578 0 : error ("call to non-%<constexpr%> function %qD", function);
2579 0 : explain_invalid_constexpr_fn (function);
2580 : }
2581 : else
2582 : /* virtual_offset is only set for virtual bases, which make the
2583 : class non-literal, so we don't need to handle it here. */
2584 1 : error ("calling constexpr member function %qD through virtual "
2585 : "base subobject", function);
2586 : }
2587 5 : *non_constant_p = true;
2588 5 : return t;
2589 : }
2590 :
2591 4 : tree new_call = copy_node (t);
2592 4 : CALL_EXPR_FN (new_call) = function;
2593 4 : TREE_TYPE (new_call) = TREE_TYPE (TREE_TYPE (function));
2594 :
2595 4 : tree offset = size_int (THUNK_FIXED_OFFSET (thunk_fndecl));
2596 :
2597 4 : if (DECL_THIS_THUNK_P (thunk_fndecl))
2598 : {
2599 : /* 'this'-adjusting thunk. */
2600 2 : tree this_arg = CALL_EXPR_ARG (t, 0);
2601 2 : this_arg = build2 (POINTER_PLUS_EXPR, TREE_TYPE (this_arg),
2602 : this_arg, offset);
2603 2 : CALL_EXPR_ARG (new_call, 0) = this_arg;
2604 : }
2605 : else
2606 : /* Return-adjusting thunk. */
2607 2 : new_call = build2 (POINTER_PLUS_EXPR, TREE_TYPE (new_call),
2608 : new_call, offset);
2609 :
2610 4 : return cxx_eval_constant_expression (ctx, new_call, lval,
2611 4 : non_constant_p, overflow_p);
2612 : }
2613 :
2614 : /* If OBJECT is of const class type, evaluate it to a CONSTRUCTOR and set
2615 : its TREE_READONLY flag according to READONLY_P. Used for constexpr
2616 : 'tors to detect modifying const objects in a constexpr context. */
2617 :
2618 : static void
2619 627067 : cxx_set_object_constness (const constexpr_ctx *ctx, tree object,
2620 : bool readonly_p, bool *non_constant_p,
2621 : bool *overflow_p)
2622 : {
2623 1254134 : if (CLASS_TYPE_P (TREE_TYPE (object))
2624 1254134 : && CP_TYPE_CONST_P (TREE_TYPE (object)))
2625 : {
2626 : /* Subobjects might not be stored in ctx->global->values but we
2627 : can get its CONSTRUCTOR by evaluating *this. */
2628 231472 : tree e = cxx_eval_constant_expression (ctx, object, vc_prvalue,
2629 : non_constant_p, overflow_p);
2630 231472 : if (TREE_CODE (e) == CONSTRUCTOR && !*non_constant_p)
2631 230714 : TREE_READONLY (e) = readonly_p;
2632 : }
2633 627067 : }
2634 :
2635 : /* Subroutine of cxx_eval_constant_expression.
2636 : Evaluate the call expression tree T in the context of OLD_CALL expression
2637 : evaluation. */
2638 :
2639 : static tree
2640 37084400 : cxx_eval_call_expression (const constexpr_ctx *ctx, tree t,
2641 : value_cat lval,
2642 : bool *non_constant_p, bool *overflow_p)
2643 : {
2644 : /* Handle concept checks separately. */
2645 37084400 : if (concept_check_p (t))
2646 41 : return evaluate_concept_check (t);
2647 :
2648 37084359 : location_t loc = cp_expr_loc_or_input_loc (t);
2649 37084359 : tree fun = get_function_named_in_call (t);
2650 37084359 : constexpr_call new_call
2651 37084359 : = { NULL, NULL, NULL, 0, ctx->manifestly_const_eval };
2652 37084359 : int depth_ok;
2653 :
2654 37084359 : if (fun == NULL_TREE)
2655 9093 : return cxx_eval_internal_function (ctx, t, lval,
2656 9093 : non_constant_p, overflow_p);
2657 :
2658 37075266 : if (TREE_CODE (fun) != FUNCTION_DECL)
2659 : {
2660 : /* Might be a constexpr function pointer. */
2661 235096 : fun = cxx_eval_constant_expression (ctx, fun, vc_prvalue,
2662 : non_constant_p, overflow_p);
2663 235096 : STRIP_NOPS (fun);
2664 235096 : if (TREE_CODE (fun) == ADDR_EXPR)
2665 3188 : fun = TREE_OPERAND (fun, 0);
2666 : /* For TARGET_VTABLE_USES_DESCRIPTORS targets, there is no
2667 : indirection, the called expression is a pointer into the
2668 : virtual table which should contain FDESC_EXPR. Extract the
2669 : FUNCTION_DECL from there. */
2670 : else if (TARGET_VTABLE_USES_DESCRIPTORS
2671 : && TREE_CODE (fun) == POINTER_PLUS_EXPR
2672 : && TREE_CODE (TREE_OPERAND (fun, 0)) == ADDR_EXPR
2673 : && TREE_CODE (TREE_OPERAND (fun, 1)) == INTEGER_CST)
2674 : {
2675 : tree d = TREE_OPERAND (TREE_OPERAND (fun, 0), 0);
2676 : if (VAR_P (d)
2677 : && DECL_VTABLE_OR_VTT_P (d)
2678 : && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE
2679 : && TREE_TYPE (TREE_TYPE (d)) == vtable_entry_type
2680 : && DECL_INITIAL (d)
2681 : && TREE_CODE (DECL_INITIAL (d)) == CONSTRUCTOR)
2682 : {
2683 : tree i = int_const_binop (TRUNC_DIV_EXPR, TREE_OPERAND (fun, 1),
2684 : TYPE_SIZE_UNIT (vtable_entry_type));
2685 : HOST_WIDE_INT idx = find_array_ctor_elt (DECL_INITIAL (d), i);
2686 : if (idx >= 0)
2687 : {
2688 : tree fdesc
2689 : = (*CONSTRUCTOR_ELTS (DECL_INITIAL (d)))[idx].value;
2690 : if (TREE_CODE (fdesc) == FDESC_EXPR
2691 : && integer_zerop (TREE_OPERAND (fdesc, 1)))
2692 : fun = TREE_OPERAND (fdesc, 0);
2693 : }
2694 : }
2695 : }
2696 : }
2697 37075266 : if (TREE_CODE (fun) != FUNCTION_DECL)
2698 : {
2699 231908 : if (!ctx->quiet && !*non_constant_p)
2700 0 : error_at (loc, "expression %qE does not designate a %<constexpr%> "
2701 : "function", fun);
2702 231908 : *non_constant_p = true;
2703 231908 : return t;
2704 : }
2705 36843358 : if (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun))
2706 1637566 : fun = DECL_CLONED_FUNCTION (fun);
2707 :
2708 36843358 : if (is_ubsan_builtin_p (fun))
2709 71 : return void_node;
2710 :
2711 36843287 : if (fndecl_built_in_p (fun))
2712 8139860 : return cxx_eval_builtin_function_call (ctx, t, fun,
2713 8139860 : lval, non_constant_p, overflow_p);
2714 28703427 : if (DECL_THUNK_P (fun))
2715 9 : return cxx_eval_thunk_call (ctx, t, fun, lval, non_constant_p, overflow_p);
2716 28703418 : if (!maybe_constexpr_fn (fun))
2717 : {
2718 80356 : if (TREE_CODE (t) == CALL_EXPR
2719 32453 : && cxx_replaceable_global_alloc_fn (fun)
2720 85213 : && (CALL_FROM_NEW_OR_DELETE_P (t)
2721 4368 : || is_std_allocator_allocate (ctx->call)))
2722 : {
2723 1011 : const int nargs = call_expr_nargs (t);
2724 1011 : tree arg0 = NULL_TREE;
2725 2093 : for (int i = 0; i < nargs; ++i)
2726 : {
2727 1103 : tree arg = CALL_EXPR_ARG (t, i);
2728 1103 : arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
2729 : non_constant_p, overflow_p);
2730 1103 : VERIFY_CONSTANT (arg);
2731 1082 : if (i == 0)
2732 990 : arg0 = arg;
2733 : }
2734 990 : gcc_assert (arg0);
2735 990 : if (IDENTIFIER_NEW_OP_P (DECL_NAME (fun)))
2736 : {
2737 1228 : tree type = build_array_type_nelts (char_type_node,
2738 614 : tree_to_uhwi (arg0));
2739 614 : tree var = build_decl (loc, VAR_DECL,
2740 614 : (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
2741 : & OVL_OP_FLAG_VEC)
2742 : ? heap_vec_uninit_identifier
2743 : : heap_uninit_identifier,
2744 614 : type);
2745 614 : DECL_ARTIFICIAL (var) = 1;
2746 614 : TREE_STATIC (var) = 1;
2747 : // Temporarily register the artificial var in varpool,
2748 : // so that comparisons of its address against NULL are folded
2749 : // through nonzero_address even with
2750 : // -fno-delete-null-pointer-checks or that comparison of
2751 : // addresses of different heap artificial vars is folded too.
2752 : // See PR98988 and PR99031.
2753 614 : varpool_node::finalize_decl (var);
2754 614 : ctx->global->heap_vars.safe_push (var);
2755 614 : ctx->global->put_value (var, NULL_TREE);
2756 614 : return fold_convert (ptr_type_node, build_address (var));
2757 : }
2758 : else
2759 : {
2760 376 : STRIP_NOPS (arg0);
2761 376 : if (TREE_CODE (arg0) == ADDR_EXPR
2762 376 : && VAR_P (TREE_OPERAND (arg0, 0)))
2763 : {
2764 376 : tree var = TREE_OPERAND (arg0, 0);
2765 376 : if (DECL_NAME (var) == heap_uninit_identifier
2766 376 : || DECL_NAME (var) == heap_identifier)
2767 : {
2768 336 : if (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
2769 : & OVL_OP_FLAG_VEC)
2770 : {
2771 3 : if (!ctx->quiet)
2772 : {
2773 1 : error_at (loc, "array deallocation of object "
2774 : "allocated with non-array "
2775 : "allocation");
2776 1 : inform (DECL_SOURCE_LOCATION (var),
2777 : "allocation performed here");
2778 : }
2779 3 : *non_constant_p = true;
2780 3 : return t;
2781 : }
2782 333 : DECL_NAME (var) = heap_deleted_identifier;
2783 333 : ctx->global->remove_value (var);
2784 333 : ctx->global->heap_dealloc_count++;
2785 333 : return void_node;
2786 : }
2787 40 : else if (DECL_NAME (var) == heap_vec_uninit_identifier
2788 40 : || DECL_NAME (var) == heap_vec_identifier)
2789 : {
2790 32 : if ((IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
2791 : & OVL_OP_FLAG_VEC) == 0)
2792 : {
2793 3 : if (!ctx->quiet)
2794 : {
2795 1 : error_at (loc, "non-array deallocation of "
2796 : "object allocated with array "
2797 : "allocation");
2798 1 : inform (DECL_SOURCE_LOCATION (var),
2799 : "allocation performed here");
2800 : }
2801 3 : *non_constant_p = true;
2802 3 : return t;
2803 : }
2804 29 : DECL_NAME (var) = heap_deleted_identifier;
2805 29 : ctx->global->remove_value (var);
2806 29 : ctx->global->heap_dealloc_count++;
2807 29 : return void_node;
2808 : }
2809 8 : else if (DECL_NAME (var) == heap_deleted_identifier)
2810 : {
2811 5 : if (!ctx->quiet)
2812 2 : error_at (loc, "deallocation of already deallocated "
2813 : "storage");
2814 5 : *non_constant_p = true;
2815 5 : return t;
2816 : }
2817 : }
2818 3 : if (!ctx->quiet)
2819 1 : error_at (loc, "deallocation of storage that was "
2820 : "not previously allocated");
2821 3 : *non_constant_p = true;
2822 3 : return t;
2823 : }
2824 : }
2825 : /* Allow placement new in std::construct_at, just return the second
2826 : argument. */
2827 79345 : if (TREE_CODE (t) == CALL_EXPR
2828 31442 : && cxx_placement_new_fn (fun)
2829 84154 : && is_std_construct_at (ctx->call))
2830 : {
2831 4806 : const int nargs = call_expr_nargs (t);
2832 4806 : tree arg1 = NULL_TREE;
2833 14418 : for (int i = 0; i < nargs; ++i)
2834 : {
2835 9612 : tree arg = CALL_EXPR_ARG (t, i);
2836 9612 : arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
2837 : non_constant_p, overflow_p);
2838 9612 : if (i == 1)
2839 : arg1 = arg;
2840 : else
2841 9612 : VERIFY_CONSTANT (arg);
2842 : }
2843 4806 : gcc_assert (arg1);
2844 : return arg1;
2845 : }
2846 74539 : else if (cxx_dynamic_cast_fn_p (fun))
2847 681 : return cxx_eval_dynamic_cast_fn (ctx, t, non_constant_p, overflow_p);
2848 :
2849 73858 : if (!ctx->quiet)
2850 : {
2851 14 : if (!lambda_static_thunk_p (fun))
2852 14 : error_at (loc, "call to non-%<constexpr%> function %qD", fun);
2853 14 : explain_invalid_constexpr_fn (fun);
2854 : }
2855 73858 : *non_constant_p = true;
2856 73858 : return t;
2857 : }
2858 :
2859 28623062 : constexpr_ctx new_ctx = *ctx;
2860 30202240 : if (DECL_CONSTRUCTOR_P (fun) && !ctx->object
2861 29583745 : && TREE_CODE (t) == AGGR_INIT_EXPR)
2862 : {
2863 : /* We want to have an initialization target for an AGGR_INIT_EXPR.
2864 : If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
2865 161 : new_ctx.object = AGGR_INIT_EXPR_SLOT (t);
2866 161 : tree ctor = new_ctx.ctor = build_constructor (DECL_CONTEXT (fun), NULL);
2867 161 : CONSTRUCTOR_NO_CLEARING (ctor) = true;
2868 161 : ctx->global->put_value (new_ctx.object, ctor);
2869 161 : ctx = &new_ctx;
2870 : }
2871 :
2872 : /* We used to shortcut trivial constructor/op= here, but nowadays
2873 : we can only get a trivial function here with -fno-elide-constructors. */
2874 28623062 : gcc_checking_assert (!trivial_fn_p (fun)
2875 : || !flag_elide_constructors
2876 : /* We don't elide constructors when processing
2877 : a noexcept-expression. */
2878 : || cp_noexcept_operand);
2879 :
2880 28623062 : bool non_constant_args = false;
2881 28623062 : new_call.bindings
2882 28623062 : = cxx_bind_parameters_in_call (ctx, t, fun, non_constant_p,
2883 : overflow_p, &non_constant_args);
2884 :
2885 : /* We build up the bindings list before we know whether we already have this
2886 : call cached. If we don't end up saving these bindings, ggc_free them when
2887 : this function exits. */
2888 28623062 : class free_bindings
2889 : {
2890 : tree *bindings;
2891 : public:
2892 28623062 : free_bindings (tree &b): bindings (&b) { }
2893 28623062 : ~free_bindings () { if (bindings) ggc_free (*bindings); }
2894 2079771 : void preserve () { bindings = NULL; }
2895 28623062 : } fb (new_call.bindings);
2896 :
2897 28623062 : if (*non_constant_p)
2898 : return t;
2899 :
2900 : /* We can't defer instantiating the function any longer. */
2901 17973022 : if (!DECL_INITIAL (fun)
2902 605749 : && (DECL_TEMPLOID_INSTANTIATION (fun) || DECL_DEFAULTED_FN (fun))
2903 17973022 : && !uid_sensitive_constexpr_evaluation_p ())
2904 : {
2905 417975 : location_t save_loc = input_location;
2906 417975 : input_location = loc;
2907 417975 : ++function_depth;
2908 417975 : if (ctx->manifestly_const_eval == mce_true)
2909 35024 : FNDECL_MANIFESTLY_CONST_EVALUATED (fun) = true;
2910 417975 : if (DECL_TEMPLOID_INSTANTIATION (fun))
2911 417964 : instantiate_decl (fun, /*defer_ok*/false, /*expl_inst*/false);
2912 : else
2913 11 : synthesize_method (fun);
2914 417975 : --function_depth;
2915 417975 : input_location = save_loc;
2916 : }
2917 :
2918 : /* If in direct recursive call, optimize definition search. */
2919 17973022 : if (ctx && ctx->call && ctx->call->fundef && ctx->call->fundef->decl == fun)
2920 4019 : new_call.fundef = ctx->call->fundef;
2921 : else
2922 : {
2923 17969003 : new_call.fundef = retrieve_constexpr_fundef (fun);
2924 17969003 : if (new_call.fundef == NULL || new_call.fundef->body == NULL
2925 17751995 : || new_call.fundef->result == error_mark_node
2926 17747950 : || fun == current_function_decl)
2927 : {
2928 221056 : if (!ctx->quiet)
2929 : {
2930 : /* We need to check for current_function_decl here in case we're
2931 : being called during cp_fold_function, because at that point
2932 : DECL_INITIAL is set properly and we have a fundef but we
2933 : haven't lowered invisirefs yet (c++/70344). */
2934 92 : if (DECL_INITIAL (fun) == error_mark_node
2935 92 : || fun == current_function_decl)
2936 9 : error_at (loc, "%qD called in a constant expression before its "
2937 : "definition is complete", fun);
2938 83 : else if (DECL_INITIAL (fun))
2939 : {
2940 : /* The definition of fun was somehow unsuitable. But pretend
2941 : that lambda static thunks don't exist. */
2942 56 : if (!lambda_static_thunk_p (fun))
2943 56 : error_at (loc, "%qD called in a constant expression", fun);
2944 56 : explain_invalid_constexpr_fn (fun);
2945 : }
2946 : else
2947 27 : error_at (loc, "%qD used before its definition", fun);
2948 : }
2949 221056 : *non_constant_p = true;
2950 221056 : return t;
2951 : }
2952 : }
2953 :
2954 17751966 : depth_ok = push_cx_call_context (t);
2955 :
2956 : /* Remember the object we are constructing or destructing. */
2957 17751966 : tree new_obj = NULL_TREE;
2958 35503932 : if (DECL_CONSTRUCTOR_P (fun) || DECL_DESTRUCTOR_P (fun))
2959 : {
2960 : /* In a cdtor, it should be the first `this' argument.
2961 : At this point it has already been evaluated in the call
2962 : to cxx_bind_parameters_in_call. */
2963 637358 : new_obj = TREE_VEC_ELT (new_call.bindings, 0);
2964 637358 : new_obj = cxx_fold_indirect_ref (ctx, loc, DECL_CONTEXT (fun), new_obj);
2965 :
2966 320577 : if (ctx->call && ctx->call->fundef
2967 1278512 : && DECL_CONSTRUCTOR_P (ctx->call->fundef->decl))
2968 : {
2969 202646 : tree cur_obj = TREE_VEC_ELT (ctx->call->bindings, 0);
2970 202646 : STRIP_NOPS (cur_obj);
2971 202646 : if (TREE_CODE (cur_obj) == ADDR_EXPR)
2972 202636 : cur_obj = TREE_OPERAND (cur_obj, 0);
2973 202646 : if (new_obj == cur_obj)
2974 : /* We're calling the target constructor of a delegating
2975 : constructor, or accessing a base subobject through a
2976 : NOP_EXPR as part of a call to a base constructor, so
2977 : there is no new (sub)object. */
2978 17124899 : new_obj = NULL_TREE;
2979 : }
2980 : }
2981 :
2982 17751966 : tree result = NULL_TREE;
2983 :
2984 17751966 : constexpr_call *entry = NULL;
2985 17751966 : if (depth_ok && !non_constant_args && ctx->strict)
2986 : {
2987 13983435 : new_call.hash = constexpr_fundef_hasher::hash (new_call.fundef);
2988 13983435 : new_call.hash
2989 13983435 : = iterative_hash_template_arg (new_call.bindings, new_call.hash);
2990 13983435 : new_call.hash
2991 13983435 : = iterative_hash_object (ctx->manifestly_const_eval, new_call.hash);
2992 :
2993 : /* If we have seen this call before, we are done. */
2994 13983435 : maybe_initialize_constexpr_call_table ();
2995 13983435 : bool insert = depth_ok < constexpr_cache_depth;
2996 13983435 : constexpr_call **slot
2997 14011274 : = constexpr_call_table->find_slot (&new_call,
2998 : insert ? INSERT : NO_INSERT);
2999 13983435 : entry = slot ? *slot : NULL;
3000 13972431 : if (entry == NULL)
3001 : {
3002 : /* Only cache up to constexpr_cache_depth to limit memory use. */
3003 2090775 : if (insert)
3004 : {
3005 : /* We need to keep a pointer to the entry, not just the slot, as
3006 : the slot can move during evaluation of the body. */
3007 2079771 : *slot = entry = ggc_alloc<constexpr_call> ();
3008 2079771 : *entry = new_call;
3009 2079771 : fb.preserve ();
3010 : }
3011 : }
3012 : /* Calls that are in progress have their result set to NULL, so that we
3013 : can detect circular dependencies. Now that we only cache up to
3014 : constexpr_cache_depth this won't catch circular dependencies that
3015 : start deeper, but they'll hit the recursion or ops limit. */
3016 11892660 : else if (entry->result == NULL)
3017 : {
3018 15 : if (!ctx->quiet)
3019 0 : error ("call has circular dependency");
3020 15 : *non_constant_p = true;
3021 15 : entry->result = result = error_mark_node;
3022 : }
3023 : else
3024 11892645 : result = entry->result;
3025 : }
3026 :
3027 17751966 : if (!depth_ok)
3028 : {
3029 21 : if (!ctx->quiet)
3030 3 : error ("%<constexpr%> evaluation depth exceeds maximum of %d (use "
3031 : "%<-fconstexpr-depth=%> to increase the maximum)",
3032 : max_constexpr_depth);
3033 21 : *non_constant_p = true;
3034 21 : result = error_mark_node;
3035 : }
3036 : else
3037 : {
3038 17751945 : bool cacheable = !!entry;
3039 17751945 : if (result && result != error_mark_node)
3040 : /* OK */;
3041 8603070 : else if (!DECL_SAVED_TREE (fun))
3042 : {
3043 : /* When at_eof >= 2, cgraph has started throwing away
3044 : DECL_SAVED_TREE, so fail quietly. FIXME we get here because of
3045 : late code generation for VEC_INIT_EXPR, which needs to be
3046 : completely reconsidered. */
3047 0 : gcc_assert (at_eof >= 2 && ctx->quiet);
3048 0 : *non_constant_p = true;
3049 : }
3050 8603070 : else if (tree copy = get_fundef_copy (new_call.fundef))
3051 : {
3052 8603070 : tree body, parms, res;
3053 8603070 : releasing_vec ctors;
3054 :
3055 : /* Reuse or create a new unshared copy of this function's body. */
3056 8603070 : body = TREE_PURPOSE (copy);
3057 8603070 : parms = TREE_VALUE (copy);
3058 8603070 : res = TREE_TYPE (copy);
3059 :
3060 : /* Associate the bindings with the remapped parms. */
3061 8603070 : tree bound = new_call.bindings;
3062 8603070 : tree remapped = parms;
3063 15108669 : for (int i = 0; i < TREE_VEC_LENGTH (bound); ++i)
3064 : {
3065 6505599 : tree arg = TREE_VEC_ELT (bound, i);
3066 6505599 : if (entry)
3067 : {
3068 : /* Unshare args going into the hash table to separate them
3069 : from the caller's context, for better GC and to avoid
3070 : problems with verify_gimple. */
3071 1270852 : arg = unshare_expr_without_location (arg);
3072 1270852 : TREE_VEC_ELT (bound, i) = arg;
3073 :
3074 : /* And then unshare again so the callee doesn't change the
3075 : argument values in the hash table. XXX Could we unshare
3076 : lazily in cxx_eval_store_expression? */
3077 1270852 : arg = unshare_constructor (arg);
3078 1270852 : if (TREE_CODE (arg) == CONSTRUCTOR)
3079 452589 : vec_safe_push (ctors, arg);
3080 : }
3081 6505599 : ctx->global->put_value (remapped, arg);
3082 6505599 : remapped = DECL_CHAIN (remapped);
3083 : }
3084 : /* Add the RESULT_DECL to the values map, too. */
3085 8603070 : gcc_assert (!DECL_BY_REFERENCE (res));
3086 8603070 : ctx->global->put_value (res, NULL_TREE);
3087 :
3088 : /* Track the callee's evaluated SAVE_EXPRs and TARGET_EXPRs so that
3089 : we can forget their values after the call. */
3090 8603070 : constexpr_ctx ctx_with_save_exprs = *ctx;
3091 8603070 : auto_vec<tree, 10> save_exprs;
3092 8603070 : ctx_with_save_exprs.save_exprs = &save_exprs;
3093 8603070 : ctx_with_save_exprs.call = &new_call;
3094 8603070 : unsigned save_heap_alloc_count = ctx->global->heap_vars.length ();
3095 8603070 : unsigned save_heap_dealloc_count = ctx->global->heap_dealloc_count;
3096 :
3097 : /* If this is a constexpr destructor, the object's const and volatile
3098 : semantics are no longer in effect; see [class.dtor]p5. */
3099 9230137 : if (new_obj && DECL_DESTRUCTOR_P (fun))
3100 6200 : cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/false,
3101 : non_constant_p, overflow_p);
3102 :
3103 8603070 : tree jump_target = NULL_TREE;
3104 8603070 : cxx_eval_constant_expression (&ctx_with_save_exprs, body,
3105 : vc_discard, non_constant_p, overflow_p,
3106 : &jump_target);
3107 :
3108 17206140 : if (DECL_CONSTRUCTOR_P (fun))
3109 : {
3110 : /* This can be null for a subobject constructor call, in
3111 : which case what we care about is the initialization
3112 : side-effects rather than the value. We could get at the
3113 : value by evaluating *this, but we don't bother; there's
3114 : no need to put such a call in the hash table. */
3115 631158 : result = lval ? ctx->object : ctx->ctor;
3116 :
3117 : /* If we've just evaluated a subobject constructor call for an
3118 : empty union member, it might not have produced a side effect
3119 : that actually activated the union member. So produce such a
3120 : side effect now to ensure the union appears initialized. */
3121 631158 : if (!result && new_obj
3122 173141 : && TREE_CODE (new_obj) == COMPONENT_REF
3123 170276 : && TREE_CODE (TREE_TYPE
3124 : (TREE_OPERAND (new_obj, 0))) == UNION_TYPE
3125 631631 : && is_really_empty_class (TREE_TYPE (new_obj),
3126 : /*ignore_vptr*/false))
3127 : {
3128 41 : tree activate = build2 (MODIFY_EXPR, TREE_TYPE (new_obj),
3129 : new_obj,
3130 41 : build_constructor (TREE_TYPE (new_obj),
3131 : NULL));
3132 41 : cxx_eval_constant_expression (ctx, activate, lval,
3133 : non_constant_p, overflow_p);
3134 41 : ggc_free (activate);
3135 : }
3136 : }
3137 7971912 : else if (VOID_TYPE_P (TREE_TYPE (res)))
3138 92906 : result = void_node;
3139 : else
3140 : {
3141 7879006 : result = ctx->global->get_value (res);
3142 3175150 : if (result == NULL_TREE && !*non_constant_p
3143 7879176 : && !DECL_DESTRUCTOR_P (fun))
3144 : {
3145 85 : if (!ctx->quiet)
3146 13 : error ("%<constexpr%> call flows off the end "
3147 : "of the function");
3148 85 : *non_constant_p = true;
3149 : }
3150 : }
3151 :
3152 : /* At this point, the object's constructor will have run, so
3153 : the object is no longer under construction, and its possible
3154 : 'const' semantics now apply. Make a note of this fact by
3155 : marking the CONSTRUCTOR TREE_READONLY. */
3156 9230137 : if (new_obj && DECL_CONSTRUCTOR_P (fun))
3157 620867 : cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/true,
3158 : non_constant_p, overflow_p);
3159 :
3160 : /* Forget the saved values of the callee's SAVE_EXPRs and
3161 : TARGET_EXPRs. */
3162 26070945 : for (tree save_expr : save_exprs)
3163 261735 : ctx->global->remove_value (save_expr);
3164 :
3165 : /* Remove the parms/result from the values map. Is it worth
3166 : bothering to do this when the map itself is only live for
3167 : one constexpr evaluation? If so, maybe also clear out
3168 : other vars from call, maybe in BIND_EXPR handling? */
3169 8603070 : ctx->global->remove_value (res);
3170 15114874 : for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
3171 6511804 : ctx->global->remove_value (parm);
3172 :
3173 : /* Free any parameter CONSTRUCTORs we aren't returning directly. */
3174 9055659 : while (!ctors->is_empty ())
3175 : {
3176 452589 : tree c = ctors->pop ();
3177 452589 : if (c != result)
3178 452589 : free_constructor (c);
3179 : }
3180 :
3181 : /* Make the unshared function copy we used available for re-use. */
3182 8603070 : save_fundef_copy (fun, copy);
3183 :
3184 : /* If the call allocated some heap object that hasn't been
3185 : deallocated during the call, or if it deallocated some heap
3186 : object it has not allocated, the call isn't really stateless
3187 : for the constexpr evaluation and should not be cached.
3188 : It is fine if the call allocates something and deallocates it
3189 : too. */
3190 8603070 : if (cacheable
3191 13426626 : && (save_heap_alloc_count != ctx->global->heap_vars.length ()
3192 4823394 : || (save_heap_dealloc_count
3193 4823394 : != ctx->global->heap_dealloc_count)))
3194 : {
3195 162 : tree heap_var;
3196 162 : unsigned int i;
3197 162 : if ((ctx->global->heap_vars.length ()
3198 162 : - ctx->global->heap_dealloc_count)
3199 162 : != save_heap_alloc_count - save_heap_dealloc_count)
3200 : cacheable = false;
3201 : else
3202 495 : FOR_EACH_VEC_ELT_FROM (ctx->global->heap_vars, i, heap_var,
3203 : save_heap_alloc_count)
3204 355 : if (DECL_NAME (heap_var) != heap_deleted_identifier)
3205 : {
3206 : cacheable = false;
3207 : break;
3208 : }
3209 : }
3210 :
3211 : /* Rewrite all occurrences of the function's RESULT_DECL with the
3212 : current object under construction. */
3213 5347678 : if (!*non_constant_p && ctx->object
3214 1193250 : && CLASS_TYPE_P (TREE_TYPE (res))
3215 9144009 : && !is_empty_class (TREE_TYPE (res)))
3216 137690 : if (replace_decl (&result, res, ctx->object))
3217 : cacheable = false;
3218 :
3219 : /* Only cache a permitted result of a constant expression. */
3220 8602881 : if (cacheable && !reduced_constant_expression_p (result))
3221 : cacheable = false;
3222 8603070 : }
3223 : else
3224 : /* Couldn't get a function copy to evaluate. */
3225 0 : *non_constant_p = true;
3226 :
3227 17751945 : if (result == error_mark_node)
3228 0 : *non_constant_p = true;
3229 17751945 : if (*non_constant_p || *overflow_p)
3230 3255548 : result = error_mark_node;
3231 14496397 : else if (!result)
3232 169343 : result = void_node;
3233 17751945 : if (entry)
3234 13972431 : entry->result = cacheable ? result : error_mark_node;
3235 : }
3236 :
3237 : /* The result of a constexpr function must be completely initialized.
3238 :
3239 : However, in C++20, a constexpr constructor doesn't necessarily have
3240 : to initialize all the fields, so we don't clear CONSTRUCTOR_NO_CLEARING
3241 : in order to detect reading an unitialized object in constexpr instead
3242 : of value-initializing it. (reduced_constant_expression_p is expected to
3243 : take care of clearing the flag.) */
3244 17751966 : if (TREE_CODE (result) == CONSTRUCTOR
3245 17751966 : && (cxx_dialect < cxx20
3246 580482 : || !DECL_CONSTRUCTOR_P (fun)))
3247 1834845 : clear_no_implicit_zero (result);
3248 :
3249 17751966 : pop_cx_call_context ();
3250 17751966 : return result;
3251 28623062 : }
3252 :
3253 : /* Return true if T is a valid constant initializer. If a CONSTRUCTOR
3254 : initializes all the members, the CONSTRUCTOR_NO_CLEARING flag will be
3255 : cleared.
3256 : FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
3257 :
3258 : bool
3259 297416004 : reduced_constant_expression_p (tree t)
3260 : {
3261 297416004 : if (t == NULL_TREE)
3262 : return false;
3263 :
3264 294658592 : switch (TREE_CODE (t))
3265 : {
3266 : case PTRMEM_CST:
3267 : /* Even if we can't lower this yet, it's constant. */
3268 : return true;
3269 :
3270 12630506 : case CONSTRUCTOR:
3271 : /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
3272 12630506 : tree field;
3273 12630506 : if (!AGGREGATE_TYPE_P (TREE_TYPE (t)))
3274 : /* A constant vector would be folded to VECTOR_CST.
3275 : A CONSTRUCTOR of scalar type means uninitialized. */
3276 : return false;
3277 12614100 : if (CONSTRUCTOR_NO_CLEARING (t))
3278 : {
3279 56808 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
3280 : {
3281 : /* There must be a valid constant initializer at every array
3282 : index. */
3283 169 : tree min = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (t)));
3284 169 : tree max = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (t)));
3285 169 : tree cursor = min;
3286 15842 : for (auto &e: CONSTRUCTOR_ELTS (t))
3287 : {
3288 15405 : if (!reduced_constant_expression_p (e.value))
3289 : return false;
3290 15355 : if (array_index_cmp (cursor, e.index) != 0)
3291 : return false;
3292 15335 : if (TREE_CODE (e.index) == RANGE_EXPR)
3293 0 : cursor = TREE_OPERAND (e.index, 1);
3294 15335 : cursor = int_const_binop (PLUS_EXPR, cursor, size_one_node);
3295 : }
3296 99 : if (find_array_ctor_elt (t, max) == -1)
3297 : return false;
3298 21 : goto ok;
3299 : }
3300 56639 : else if (cxx_dialect >= cxx20
3301 56639 : && TREE_CODE (TREE_TYPE (t)) == UNION_TYPE)
3302 : {
3303 3135763 : if (CONSTRUCTOR_NELTS (t) == 0)
3304 : /* An initialized union has a constructor element. */
3305 : return false;
3306 : /* And it only initializes one member. */
3307 : field = NULL_TREE;
3308 : }
3309 : else
3310 56634 : field = next_subobject_field (TYPE_FIELDS (TREE_TYPE (t)));
3311 : }
3312 : else
3313 : field = NULL_TREE;
3314 44013902 : for (auto &e: CONSTRUCTOR_ELTS (t))
3315 : {
3316 : /* If VAL is null, we're in the middle of initializing this
3317 : element. */
3318 18399223 : if (!reduced_constant_expression_p (e.value))
3319 : return false;
3320 : /* We want to remove initializers for empty fields in a struct to
3321 : avoid confusing output_constructor. */
3322 18365555 : if (is_empty_field (e.index)
3323 18365555 : && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE)
3324 : return false;
3325 : /* Check for non-empty fields between initialized fields when
3326 : CONSTRUCTOR_NO_CLEARING. */
3327 18039542 : for (; field && e.index != field;
3328 1456 : field = next_subobject_field (DECL_CHAIN (field)))
3329 1576 : if (!is_really_empty_class (TREE_TYPE (field),
3330 : /*ignore_vptr*/false))
3331 : return false;
3332 18037966 : if (field)
3333 70381 : field = next_subobject_field (DECL_CHAIN (field));
3334 : }
3335 : /* There could be a non-empty field at the end. */
3336 12253109 : for (; field; field = next_subobject_field (DECL_CHAIN (field)))
3337 975 : if (!is_really_empty_class (TREE_TYPE (field), /*ignore_vptr*/false))
3338 : return false;
3339 12252134 : ok:
3340 12252155 : if (CONSTRUCTOR_NO_CLEARING (t))
3341 : /* All the fields are initialized. */
3342 55160 : CONSTRUCTOR_NO_CLEARING (t) = false;
3343 : return true;
3344 :
3345 282023868 : default:
3346 : /* FIXME are we calling this too much? */
3347 282023868 : return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE;
3348 : }
3349 : }
3350 :
3351 : /* Some expressions may have constant operands but are not constant
3352 : themselves, such as 1/0. Call this function to check for that
3353 : condition.
3354 :
3355 : We only call this in places that require an arithmetic constant, not in
3356 : places where we might have a non-constant expression that can be a
3357 : component of a constant expression, such as the address of a constexpr
3358 : variable that might be dereferenced later. */
3359 :
3360 : static bool
3361 321204877 : verify_constant (tree t, bool allow_non_constant, bool *non_constant_p,
3362 : bool *overflow_p)
3363 : {
3364 243541288 : if (!*non_constant_p && !reduced_constant_expression_p (t)
3365 328179057 : && t != void_node)
3366 : {
3367 6974168 : if (!allow_non_constant)
3368 178 : error ("%q+E is not a constant expression", t);
3369 6974168 : *non_constant_p = true;
3370 : }
3371 321204877 : if (TREE_OVERFLOW_P (t))
3372 : {
3373 912 : if (!allow_non_constant)
3374 : {
3375 195 : permerror (input_location, "overflow in constant expression");
3376 : /* If we're being permissive (and are in an enforcing
3377 : context), ignore the overflow. */
3378 195 : if (flag_permissive)
3379 100 : return *non_constant_p;
3380 : }
3381 812 : *overflow_p = true;
3382 : }
3383 321204777 : return *non_constant_p;
3384 : }
3385 :
3386 : /* Check whether the shift operation with code CODE and type TYPE on LHS
3387 : and RHS is undefined. If it is, give an error with an explanation,
3388 : and return true; return false otherwise. */
3389 :
3390 : static bool
3391 20104710 : cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx,
3392 : enum tree_code code, tree type, tree lhs, tree rhs)
3393 : {
3394 20104710 : if ((code != LSHIFT_EXPR && code != RSHIFT_EXPR)
3395 2288777 : || TREE_CODE (lhs) != INTEGER_CST
3396 2288777 : || TREE_CODE (rhs) != INTEGER_CST)
3397 : return false;
3398 :
3399 2288777 : tree lhstype = TREE_TYPE (lhs);
3400 2288777 : unsigned HOST_WIDE_INT uprec = TYPE_PRECISION (TREE_TYPE (lhs));
3401 :
3402 : /* [expr.shift] The behavior is undefined if the right operand
3403 : is negative, or greater than or equal to the length in bits
3404 : of the promoted left operand. */
3405 2288777 : if (tree_int_cst_sgn (rhs) == -1)
3406 : {
3407 138 : if (!ctx->quiet)
3408 40 : permerror (loc, "right operand of shift expression %q+E is negative",
3409 : build2_loc (loc, code, type, lhs, rhs));
3410 138 : return (!flag_permissive || ctx->quiet);
3411 : }
3412 2288639 : if (compare_tree_int (rhs, uprec) >= 0)
3413 : {
3414 221 : if (!ctx->quiet)
3415 37 : permerror (loc, "right operand of shift expression %q+E is greater "
3416 : "than or equal to the precision %wu of the left operand",
3417 : build2_loc (loc, code, type, lhs, rhs), uprec);
3418 221 : return (!flag_permissive || ctx->quiet);
3419 : }
3420 :
3421 : /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
3422 : if E1 has a signed type and non-negative value, and E1x2^E2 is
3423 : representable in the corresponding unsigned type of the result type,
3424 : then that value, converted to the result type, is the resulting value;
3425 : otherwise, the behavior is undefined.
3426 : For C++20:
3427 : The value of E1 << E2 is the unique value congruent to E1 x 2^E2 modulo
3428 : 2^N, where N is the range exponent of the type of the result. */
3429 2288418 : if (code == LSHIFT_EXPR
3430 2199785 : && !TYPE_OVERFLOW_WRAPS (lhstype)
3431 1639418 : && cxx_dialect >= cxx11
3432 3915574 : && cxx_dialect < cxx20)
3433 : {
3434 1534822 : if (tree_int_cst_sgn (lhs) == -1)
3435 : {
3436 155 : if (!ctx->quiet)
3437 23 : permerror (loc,
3438 : "left operand of shift expression %q+E is negative",
3439 : build2_loc (loc, code, type, lhs, rhs));
3440 155 : return (!flag_permissive || ctx->quiet);
3441 : }
3442 : /* For signed x << y the following:
3443 : (unsigned) x >> ((prec (lhs) - 1) - y)
3444 : if > 1, is undefined. The right-hand side of this formula
3445 : is the highest bit of the LHS that can be set (starting from 0),
3446 : so that the shift doesn't overflow. We then right-shift the LHS
3447 : to see whether any other bit is set making the original shift
3448 : undefined -- the result is not representable in the corresponding
3449 : unsigned type. */
3450 1534667 : tree t = build_int_cst (unsigned_type_node, uprec - 1);
3451 1534667 : t = fold_build2 (MINUS_EXPR, unsigned_type_node, t, rhs);
3452 1534667 : tree ulhs = fold_convert (unsigned_type_for (lhstype), lhs);
3453 1534667 : t = fold_build2 (RSHIFT_EXPR, TREE_TYPE (ulhs), ulhs, t);
3454 1534667 : if (tree_int_cst_lt (integer_one_node, t))
3455 : {
3456 108 : if (!ctx->quiet)
3457 8 : permerror (loc, "shift expression %q+E overflows",
3458 : build2_loc (loc, code, type, lhs, rhs));
3459 108 : return (!flag_permissive || ctx->quiet);
3460 : }
3461 : }
3462 : return false;
3463 : }
3464 :
3465 : /* Subroutine of cxx_eval_constant_expression.
3466 : Attempt to reduce the unary expression tree T to a compile time value.
3467 : If successful, return the value. Otherwise issue a diagnostic
3468 : and return error_mark_node. */
3469 :
3470 : static tree
3471 15997102 : cxx_eval_unary_expression (const constexpr_ctx *ctx, tree t,
3472 : bool /*lval*/,
3473 : bool *non_constant_p, bool *overflow_p)
3474 : {
3475 15997102 : tree r;
3476 15997102 : tree orig_arg = TREE_OPERAND (t, 0);
3477 15997102 : tree arg = cxx_eval_constant_expression (ctx, orig_arg, vc_prvalue,
3478 : non_constant_p, overflow_p);
3479 15997102 : VERIFY_CONSTANT (arg);
3480 14275756 : location_t loc = EXPR_LOCATION (t);
3481 14275756 : enum tree_code code = TREE_CODE (t);
3482 14275756 : tree type = TREE_TYPE (t);
3483 14275756 : r = fold_unary_loc (loc, code, type, arg);
3484 14275756 : if (r == NULL_TREE)
3485 : {
3486 3 : if (arg == orig_arg)
3487 : r = t;
3488 : else
3489 3 : r = build1_loc (loc, code, type, arg);
3490 : }
3491 14275756 : VERIFY_CONSTANT (r);
3492 : return r;
3493 : }
3494 :
3495 : /* Helper function for cxx_eval_binary_expression. Try to optimize
3496 : original POINTER_PLUS_EXPR T, LHS p+ RHS, return NULL_TREE if the
3497 : generic folding should be used. */
3498 :
3499 : static tree
3500 714743 : cxx_fold_pointer_plus_expression (const constexpr_ctx *ctx, tree t,
3501 : tree lhs, tree rhs, bool *non_constant_p,
3502 : bool *overflow_p)
3503 : {
3504 714743 : STRIP_NOPS (lhs);
3505 714743 : if (TREE_CODE (lhs) != ADDR_EXPR)
3506 : return NULL_TREE;
3507 :
3508 701683 : lhs = TREE_OPERAND (lhs, 0);
3509 :
3510 : /* &A[i] p+ j => &A[i + j] */
3511 701683 : if (TREE_CODE (lhs) == ARRAY_REF
3512 2809 : && TREE_CODE (TREE_OPERAND (lhs, 1)) == INTEGER_CST
3513 2809 : && TREE_CODE (rhs) == INTEGER_CST
3514 2809 : && TYPE_SIZE_UNIT (TREE_TYPE (lhs))
3515 704492 : && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs))) == INTEGER_CST)
3516 : {
3517 2809 : tree orig_type = TREE_TYPE (t);
3518 2809 : location_t loc = EXPR_LOCATION (t);
3519 2809 : tree type = TREE_TYPE (lhs);
3520 :
3521 2809 : t = fold_convert_loc (loc, ssizetype, TREE_OPERAND (lhs, 1));
3522 2809 : tree nelts = array_type_nelts_top (TREE_TYPE (TREE_OPERAND (lhs, 0)));
3523 2809 : nelts = cxx_eval_constant_expression (ctx, nelts, vc_prvalue,
3524 : non_constant_p, overflow_p);
3525 2809 : if (*non_constant_p)
3526 : return NULL_TREE;
3527 : /* Don't fold an out-of-bound access. */
3528 2791 : if (!tree_int_cst_le (t, nelts))
3529 : return NULL_TREE;
3530 2791 : rhs = cp_fold_convert (ssizetype, rhs);
3531 : /* Don't fold if rhs can't be divided exactly by TYPE_SIZE_UNIT.
3532 : constexpr int A[1]; ... (char *)&A[0] + 1 */
3533 2791 : if (!integer_zerop (fold_build2_loc (loc, TRUNC_MOD_EXPR, sizetype,
3534 2791 : rhs, TYPE_SIZE_UNIT (type))))
3535 : return NULL_TREE;
3536 : /* Make sure to treat the second operand of POINTER_PLUS_EXPR
3537 : as signed. */
3538 2754 : rhs = fold_build2_loc (loc, EXACT_DIV_EXPR, ssizetype, rhs,
3539 2754 : TYPE_SIZE_UNIT (type));
3540 2754 : t = size_binop_loc (loc, PLUS_EXPR, rhs, t);
3541 2754 : t = build4_loc (loc, ARRAY_REF, type, TREE_OPERAND (lhs, 0),
3542 : t, NULL_TREE, NULL_TREE);
3543 2754 : t = cp_build_addr_expr (t, tf_warning_or_error);
3544 2754 : t = cp_fold_convert (orig_type, t);
3545 2754 : return cxx_eval_constant_expression (ctx, t, vc_prvalue,
3546 2754 : non_constant_p, overflow_p);
3547 : }
3548 :
3549 : return NULL_TREE;
3550 : }
3551 :
3552 : /* Try to fold expressions like
3553 : (struct S *) (&a[0].D.2378 + 12)
3554 : into
3555 : &MEM <struct T> [(void *)&a + 12B]
3556 : This is something normally done by gimple_fold_stmt_to_constant_1
3557 : on GIMPLE, but is undesirable on GENERIC if we are e.g. going to
3558 : dereference the address because some details are lost.
3559 : For pointer comparisons we want such folding though so that
3560 : match.pd address_compare optimization works. */
3561 :
3562 : static tree
3563 205366 : cxx_maybe_fold_addr_pointer_plus (tree t)
3564 : {
3565 205366 : while (CONVERT_EXPR_P (t)
3566 245897 : && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0))))
3567 40531 : t = TREE_OPERAND (t, 0);
3568 205366 : if (TREE_CODE (t) != POINTER_PLUS_EXPR)
3569 : return NULL_TREE;
3570 93611 : tree op0 = TREE_OPERAND (t, 0);
3571 93611 : tree op1 = TREE_OPERAND (t, 1);
3572 93611 : if (TREE_CODE (op1) != INTEGER_CST)
3573 : return NULL_TREE;
3574 186842 : while (CONVERT_EXPR_P (op0)
3575 186842 : && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
3576 93231 : op0 = TREE_OPERAND (op0, 0);
3577 93611 : if (TREE_CODE (op0) != ADDR_EXPR)
3578 : return NULL_TREE;
3579 93611 : op1 = fold_convert (ptr_type_node, op1);
3580 93611 : tree r = fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (op0)), op0, op1);
3581 93611 : return build1_loc (EXPR_LOCATION (t), ADDR_EXPR, TREE_TYPE (op0), r);
3582 : }
3583 :
3584 : /* Subroutine of cxx_eval_constant_expression.
3585 : Like cxx_eval_unary_expression, except for binary expressions. */
3586 :
3587 : static tree
3588 27942488 : cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t,
3589 : value_cat lval,
3590 : bool *non_constant_p, bool *overflow_p)
3591 : {
3592 27942488 : tree r = NULL_TREE;
3593 27942488 : tree orig_lhs = TREE_OPERAND (t, 0);
3594 27942488 : tree orig_rhs = TREE_OPERAND (t, 1);
3595 27942488 : tree lhs, rhs;
3596 27942488 : lhs = cxx_eval_constant_expression (ctx, orig_lhs, vc_prvalue,
3597 : non_constant_p, overflow_p);
3598 : /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer
3599 : subtraction. */
3600 27942488 : if (*non_constant_p)
3601 : return t;
3602 21473321 : rhs = cxx_eval_constant_expression (ctx, orig_rhs, vc_prvalue,
3603 : non_constant_p, overflow_p);
3604 21473321 : if (*non_constant_p)
3605 : return t;
3606 :
3607 20797373 : location_t loc = EXPR_LOCATION (t);
3608 20797373 : enum tree_code code = TREE_CODE (t);
3609 20797373 : tree type = TREE_TYPE (t);
3610 :
3611 20797373 : if (code == EQ_EXPR || code == NE_EXPR)
3612 : {
3613 1291608 : bool is_code_eq = (code == EQ_EXPR);
3614 :
3615 1291608 : if (TREE_CODE (lhs) == PTRMEM_CST
3616 149 : && TREE_CODE (rhs) == PTRMEM_CST)
3617 : {
3618 54 : tree lmem = PTRMEM_CST_MEMBER (lhs);
3619 54 : tree rmem = PTRMEM_CST_MEMBER (rhs);
3620 54 : bool eq;
3621 54 : if (TREE_CODE (lmem) == TREE_CODE (rmem)
3622 54 : && TREE_CODE (lmem) == FIELD_DECL
3623 54 : && TREE_CODE (DECL_CONTEXT (lmem)) == UNION_TYPE
3624 81 : && same_type_p (DECL_CONTEXT (lmem),
3625 : DECL_CONTEXT (rmem)))
3626 : /* If both refer to (possibly different) members of the same union
3627 : (12.3), they compare equal. */
3628 : eq = true;
3629 : else
3630 27 : eq = cp_tree_equal (lhs, rhs);
3631 54 : r = constant_boolean_node (eq == is_code_eq, type);
3632 54 : }
3633 1291554 : else if ((TREE_CODE (lhs) == PTRMEM_CST
3634 1291459 : || TREE_CODE (rhs) == PTRMEM_CST)
3635 1291554 : && (null_member_pointer_value_p (lhs)
3636 95 : || null_member_pointer_value_p (rhs)))
3637 95 : r = constant_boolean_node (!is_code_eq, type);
3638 1291459 : else if (TREE_CODE (lhs) == PTRMEM_CST)
3639 0 : lhs = cplus_expand_constant (lhs);
3640 1291459 : else if (TREE_CODE (rhs) == PTRMEM_CST)
3641 0 : rhs = cplus_expand_constant (rhs);
3642 : }
3643 20797373 : if (r == NULL_TREE
3644 20797224 : && TREE_CODE_CLASS (code) == tcc_comparison
3645 27304223 : && POINTER_TYPE_P (TREE_TYPE (lhs)))
3646 : {
3647 102683 : if (tree lhso = cxx_maybe_fold_addr_pointer_plus (lhs))
3648 45944 : lhs = fold_convert (TREE_TYPE (lhs), lhso);
3649 102683 : if (tree rhso = cxx_maybe_fold_addr_pointer_plus (rhs))
3650 47667 : rhs = fold_convert (TREE_TYPE (rhs), rhso);
3651 : }
3652 714897 : if (code == POINTER_PLUS_EXPR && !*non_constant_p
3653 21512270 : && integer_zerop (lhs) && !integer_zerop (rhs))
3654 : {
3655 154 : if (!ctx->quiet)
3656 45 : error ("arithmetic involving a null pointer in %qE", lhs);
3657 154 : *non_constant_p = true;
3658 154 : return t;
3659 : }
3660 20797219 : else if (code == POINTER_PLUS_EXPR)
3661 714743 : r = cxx_fold_pointer_plus_expression (ctx, t, lhs, rhs, non_constant_p,
3662 : overflow_p);
3663 20082476 : else if (code == SPACESHIP_EXPR)
3664 : {
3665 5213 : r = genericize_spaceship (loc, type, lhs, rhs);
3666 5213 : return cxx_eval_constant_expression (ctx, r, lval, non_constant_p,
3667 5213 : overflow_p);
3668 : }
3669 :
3670 20792006 : if (r == NULL_TREE)
3671 : {
3672 20789103 : if (ctx->manifestly_const_eval == mce_true
3673 8737432 : && (flag_constexpr_fp_except
3674 8737429 : || TREE_CODE (type) != REAL_TYPE))
3675 : {
3676 8732400 : auto ofcc = make_temp_override (folding_cxx_constexpr, true);
3677 8732400 : r = fold_binary_initializer_loc (loc, code, type, lhs, rhs);
3678 8732400 : }
3679 : else
3680 12056703 : r = fold_binary_loc (loc, code, type, lhs, rhs);
3681 : }
3682 :
3683 20789103 : if (r == NULL_TREE
3684 687439 : && (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
3685 156 : && TREE_CODE (lhs) == INTEGER_CST
3686 143 : && TREE_CODE (rhs) == INTEGER_CST
3687 21479445 : && wi::neg_p (wi::to_wide (rhs)))
3688 : {
3689 : /* For diagnostics and -fpermissive emulate previous behavior of
3690 : handling shifts by negative amount. */
3691 143 : tree nrhs = const_unop (NEGATE_EXPR, TREE_TYPE (rhs), rhs);
3692 143 : if (nrhs)
3693 201 : r = fold_binary_loc (loc,
3694 : code == LSHIFT_EXPR ? RSHIFT_EXPR : LSHIFT_EXPR,
3695 : type, lhs, nrhs);
3696 : }
3697 :
3698 20792006 : if (r == NULL_TREE)
3699 : {
3700 687296 : if (lhs == orig_lhs && rhs == orig_rhs)
3701 : r = t;
3702 : else
3703 79736 : r = build2_loc (loc, code, type, lhs, rhs);
3704 : }
3705 20104710 : else if (cxx_eval_check_shift_p (loc, ctx, code, type, lhs, rhs))
3706 610 : *non_constant_p = true;
3707 : /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
3708 : a local array in a constexpr function. */
3709 20792006 : bool ptr = INDIRECT_TYPE_P (TREE_TYPE (lhs));
3710 19965423 : if (!ptr)
3711 19965423 : VERIFY_CONSTANT (r);
3712 : return r;
3713 : }
3714 :
3715 : /* Subroutine of cxx_eval_constant_expression.
3716 : Attempt to evaluate condition expressions. Dead branches are not
3717 : looked into. */
3718 :
3719 : static tree
3720 1809938 : cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t,
3721 : value_cat lval,
3722 : bool *non_constant_p, bool *overflow_p,
3723 : tree *jump_target)
3724 : {
3725 1809938 : tree val = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
3726 : vc_prvalue,
3727 : non_constant_p, overflow_p);
3728 1809938 : VERIFY_CONSTANT (val);
3729 1574861 : if (TREE_CODE (t) == IF_STMT && IF_STMT_CONSTEVAL_P (t))
3730 : {
3731 : /* Evaluate the condition as if it was
3732 : if (__builtin_is_constant_evaluated ()), i.e. defer it if not
3733 : ctx->manifestly_const_eval (as sometimes we try to constant evaluate
3734 : without manifestly_const_eval even expressions or parts thereof which
3735 : will later be manifestly const_eval evaluated), otherwise fold it to
3736 : true. */
3737 20091 : if (ctx->manifestly_const_eval == mce_unknown)
3738 : {
3739 19810 : *non_constant_p = true;
3740 19810 : return t;
3741 : }
3742 281 : val = constant_boolean_node (ctx->manifestly_const_eval == mce_true,
3743 : boolean_type_node);
3744 : }
3745 : /* Don't VERIFY_CONSTANT the other operands. */
3746 1555051 : if (integer_zerop (val))
3747 771873 : val = TREE_OPERAND (t, 2);
3748 : else
3749 783178 : val = TREE_OPERAND (t, 1);
3750 1555051 : if (TREE_CODE (t) == IF_STMT && !val)
3751 179134 : val = void_node;
3752 : /* A TARGET_EXPR may be nested inside another TARGET_EXPR, but still
3753 : serve as the initializer for the same object as the outer TARGET_EXPR,
3754 : as in
3755 : A a = true ? A{} : A{};
3756 : so strip the inner TARGET_EXPR so we don't materialize a temporary. */
3757 1555051 : if (TREE_CODE (val) == TARGET_EXPR)
3758 205 : val = TARGET_EXPR_INITIAL (val);
3759 1555051 : return cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
3760 1555051 : overflow_p, jump_target);
3761 : }
3762 :
3763 : /* Subroutine of cxx_eval_constant_expression.
3764 : Attempt to evaluate vector condition expressions. Unlike
3765 : cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal
3766 : ternary arithmetics operation, where all 3 arguments have to be
3767 : evaluated as constants and then folding computes the result from
3768 : them. */
3769 :
3770 : static tree
3771 659 : cxx_eval_vector_conditional_expression (const constexpr_ctx *ctx, tree t,
3772 : bool *non_constant_p, bool *overflow_p)
3773 : {
3774 659 : tree arg1 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
3775 : vc_prvalue,
3776 : non_constant_p, overflow_p);
3777 659 : VERIFY_CONSTANT (arg1);
3778 650 : tree arg2 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
3779 : vc_prvalue,
3780 : non_constant_p, overflow_p);
3781 650 : VERIFY_CONSTANT (arg2);
3782 650 : tree arg3 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2),
3783 : vc_prvalue,
3784 : non_constant_p, overflow_p);
3785 650 : VERIFY_CONSTANT (arg3);
3786 650 : location_t loc = EXPR_LOCATION (t);
3787 650 : tree type = TREE_TYPE (t);
3788 650 : tree r = fold_ternary_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
3789 650 : if (r == NULL_TREE)
3790 : {
3791 0 : if (arg1 == TREE_OPERAND (t, 0)
3792 0 : && arg2 == TREE_OPERAND (t, 1)
3793 0 : && arg3 == TREE_OPERAND (t, 2))
3794 : r = t;
3795 : else
3796 0 : r = build3_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
3797 : }
3798 650 : VERIFY_CONSTANT (r);
3799 : return r;
3800 : }
3801 :
3802 : /* Returns less than, equal to, or greater than zero if KEY is found to be
3803 : less than, to match, or to be greater than the constructor_elt's INDEX. */
3804 :
3805 : static int
3806 19125 : array_index_cmp (tree key, tree index)
3807 : {
3808 19125 : gcc_assert (TREE_CODE (key) == INTEGER_CST);
3809 :
3810 19125 : switch (TREE_CODE (index))
3811 : {
3812 17299 : case INTEGER_CST:
3813 17299 : return tree_int_cst_compare (key, index);
3814 1826 : case RANGE_EXPR:
3815 1826 : {
3816 1826 : tree lo = TREE_OPERAND (index, 0);
3817 1826 : tree hi = TREE_OPERAND (index, 1);
3818 1826 : if (tree_int_cst_lt (key, lo))
3819 : return -1;
3820 1669 : else if (tree_int_cst_lt (hi, key))
3821 : return 1;
3822 : else
3823 1669 : return 0;
3824 : }
3825 0 : default:
3826 0 : gcc_unreachable ();
3827 : }
3828 : }
3829 :
3830 : /* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1
3831 : if none. If INSERT is true, insert a matching element rather than fail. */
3832 :
3833 : static HOST_WIDE_INT
3834 333335 : find_array_ctor_elt (tree ary, tree dindex, bool insert)
3835 : {
3836 333335 : if (tree_int_cst_sgn (dindex) < 0)
3837 : return -1;
3838 :
3839 333335 : unsigned HOST_WIDE_INT i = tree_to_uhwi (dindex);
3840 333335 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ary);
3841 333335 : unsigned HOST_WIDE_INT len = vec_safe_length (elts);
3842 :
3843 475983 : unsigned HOST_WIDE_INT end = len;
3844 330272 : unsigned HOST_WIDE_INT begin = 0;
3845 :
3846 : /* If the last element of the CONSTRUCTOR has its own index, we can assume
3847 : that the same is true of the other elements and index directly. */
3848 330272 : if (end > 0)
3849 : {
3850 311120 : tree cindex = (*elts)[end - 1].index;
3851 311120 : if (cindex == NULL_TREE)
3852 : {
3853 : /* Verify that if the last index is missing, all indexes
3854 : are missing. */
3855 0 : if (flag_checking)
3856 0 : for (unsigned int j = 0; j < len - 1; ++j)
3857 0 : gcc_assert ((*elts)[j].index == NULL_TREE);
3858 0 : if (i < end)
3859 0 : return i;
3860 : else
3861 : {
3862 0 : begin = end;
3863 0 : if (i == end)
3864 : /* If the element is to be added right at the end,
3865 : make sure it is added with cleared index too. */
3866 145711 : dindex = NULL_TREE;
3867 0 : else if (insert)
3868 : /* Otherwise, in order not to break the assumption
3869 : that CONSTRUCTOR either has all indexes or none,
3870 : we need to add indexes to all elements. */
3871 0 : for (unsigned int j = 0; j < len; ++j)
3872 0 : (*elts)[j].index = build_int_cst (TREE_TYPE (dindex), j);
3873 : }
3874 : }
3875 311120 : else if (TREE_CODE (cindex) == INTEGER_CST
3876 311120 : && compare_tree_int (cindex, end - 1) == 0)
3877 : {
3878 308728 : if (i < end)
3879 187624 : return i;
3880 : else
3881 145711 : begin = end;
3882 : }
3883 : }
3884 :
3885 : /* Otherwise, find a matching index by means of a binary search. */
3886 147513 : while (begin != end)
3887 : {
3888 3767 : unsigned HOST_WIDE_INT middle = (begin + end) / 2;
3889 3767 : constructor_elt &elt = (*elts)[middle];
3890 3767 : tree idx = elt.index;
3891 :
3892 3767 : int cmp = array_index_cmp (dindex, idx);
3893 3767 : if (cmp < 0)
3894 : end = middle;
3895 2778 : else if (cmp > 0)
3896 813 : begin = middle + 1;
3897 : else
3898 : {
3899 1965 : if (insert && TREE_CODE (idx) == RANGE_EXPR)
3900 : {
3901 : /* We need to split the range. */
3902 219 : constructor_elt e;
3903 219 : tree lo = TREE_OPERAND (idx, 0);
3904 219 : tree hi = TREE_OPERAND (idx, 1);
3905 219 : tree value = elt.value;
3906 219 : dindex = fold_convert (sizetype, dindex);
3907 219 : if (tree_int_cst_lt (lo, dindex))
3908 : {
3909 : /* There are still some lower elts; shorten the range. */
3910 134 : tree new_hi = int_const_binop (MINUS_EXPR, dindex,
3911 67 : size_one_node);
3912 67 : if (tree_int_cst_equal (lo, new_hi))
3913 : /* Only one element left, no longer a range. */
3914 23 : elt.index = lo;
3915 : else
3916 44 : TREE_OPERAND (idx, 1) = new_hi;
3917 : /* Append the element we want to insert. */
3918 67 : ++middle;
3919 67 : e.index = dindex;
3920 67 : e.value = unshare_constructor (value);
3921 67 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e);
3922 : }
3923 : else
3924 : /* No lower elts, the range elt is now ours. */
3925 152 : elt.index = dindex;
3926 :
3927 219 : if (tree_int_cst_lt (dindex, hi))
3928 : {
3929 : /* There are still some higher elts; append a range. */
3930 330 : tree new_lo = int_const_binop (PLUS_EXPR, dindex,
3931 165 : size_one_node);
3932 165 : if (tree_int_cst_equal (new_lo, hi))
3933 69 : e.index = hi;
3934 : else
3935 96 : e.index = build2 (RANGE_EXPR, sizetype, new_lo, hi);
3936 165 : e.value = unshare_constructor (value);
3937 165 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e);
3938 : }
3939 : }
3940 1965 : return middle;
3941 : }
3942 : }
3943 :
3944 143746 : if (insert)
3945 : {
3946 141176 : constructor_elt e = { dindex, NULL_TREE };
3947 141176 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), end, e);
3948 141176 : return end;
3949 : }
3950 :
3951 : return -1;
3952 : }
3953 :
3954 : /* Return a pointer to the constructor_elt of CTOR which matches INDEX. If no
3955 : matching constructor_elt exists, then add one to CTOR.
3956 :
3957 : As an optimization, if POS_HINT is non-negative then it is used as a guess
3958 : for the (integer) index of the matching constructor_elt within CTOR. */
3959 :
3960 : static constructor_elt *
3961 1897535 : get_or_insert_ctor_field (tree ctor, tree index, int pos_hint = -1)
3962 : {
3963 : /* Check the hint first. */
3964 208166 : if (pos_hint >= 0 && (unsigned)pos_hint < CONSTRUCTOR_NELTS (ctor)
3965 2105701 : && CONSTRUCTOR_ELT (ctor, pos_hint)->index == index)
3966 : return CONSTRUCTOR_ELT (ctor, pos_hint);
3967 :
3968 1689374 : tree type = TREE_TYPE (ctor);
3969 1689374 : if (TREE_CODE (type) == VECTOR_TYPE && index == NULL_TREE)
3970 : {
3971 130 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (ctor), index, NULL_TREE);
3972 130 : return &CONSTRUCTOR_ELTS (ctor)->last();
3973 : }
3974 1689244 : else if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
3975 : {
3976 213749 : if (TREE_CODE (index) == RANGE_EXPR)
3977 : {
3978 : /* Support for RANGE_EXPR index lookups is currently limited to
3979 : accessing an existing element via POS_HINT, or appending a new
3980 : element to the end of CTOR. ??? Support for other access
3981 : patterns may also be needed. */
3982 3 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
3983 3 : if (vec_safe_length (elts))
3984 : {
3985 3 : tree lo = TREE_OPERAND (index, 0);
3986 3 : gcc_assert (array_index_cmp (elts->last().index, lo) < 0);
3987 : }
3988 3 : CONSTRUCTOR_APPEND_ELT (elts, index, NULL_TREE);
3989 3 : return &elts->last();
3990 : }
3991 :
3992 213746 : HOST_WIDE_INT i = find_array_ctor_elt (ctor, index, /*insert*/true);
3993 213746 : gcc_assert (i >= 0);
3994 213746 : constructor_elt *cep = CONSTRUCTOR_ELT (ctor, i);
3995 213746 : gcc_assert (cep->index == NULL_TREE
3996 : || TREE_CODE (cep->index) != RANGE_EXPR);
3997 : return cep;
3998 : }
3999 : else
4000 : {
4001 1475495 : gcc_assert (TREE_CODE (index) == FIELD_DECL
4002 : && (same_type_ignoring_top_level_qualifiers_p
4003 : (DECL_CONTEXT (index), TREE_TYPE (ctor))));
4004 :
4005 : /* We must keep the CONSTRUCTOR's ELTS in FIELD order.
4006 : Usually we meet initializers in that order, but it is
4007 : possible for base types to be placed not in program
4008 : order. */
4009 1475495 : tree fields = TYPE_FIELDS (DECL_CONTEXT (index));
4010 1475495 : unsigned HOST_WIDE_INT idx = 0;
4011 1475495 : constructor_elt *cep = NULL;
4012 :
4013 : /* Check if we're changing the active member of a union. */
4014 32243 : if (TREE_CODE (type) == UNION_TYPE && CONSTRUCTOR_NELTS (ctor)
4015 1479477 : && CONSTRUCTOR_ELT (ctor, 0)->index != index)
4016 526 : vec_safe_truncate (CONSTRUCTOR_ELTS (ctor), 0);
4017 : /* If the bit offset of INDEX is larger than that of the last
4018 : constructor_elt, then we can just immediately append a new
4019 : constructor_elt to the end of CTOR. */
4020 1474969 : else if (CONSTRUCTOR_NELTS (ctor)
4021 1788613 : && tree_int_cst_compare (bit_position (index),
4022 1742174 : bit_position (CONSTRUCTOR_ELTS (ctor)
4023 871087 : ->last().index)) > 0)
4024 : {
4025 411329 : idx = CONSTRUCTOR_NELTS (ctor);
4026 411329 : goto insert;
4027 : }
4028 :
4029 : /* Otherwise, we need to iterate over CTOR to find or insert INDEX
4030 : appropriately. */
4031 :
4032 1242571 : for (; vec_safe_iterate (CONSTRUCTOR_ELTS (ctor), idx, &cep);
4033 178405 : idx++, fields = DECL_CHAIN (fields))
4034 : {
4035 638163 : if (index == cep->index)
4036 459273 : goto found;
4037 :
4038 : /* The field we're initializing must be on the field
4039 : list. Look to see if it is present before the
4040 : field the current ELT initializes. */
4041 2079014 : for (; fields != cep->index; fields = DECL_CHAIN (fields))
4042 1900609 : if (index == fields)
4043 485 : goto insert;
4044 : }
4045 : /* We fell off the end of the CONSTRUCTOR, so insert a new
4046 : entry at the end. */
4047 :
4048 1016222 : insert:
4049 1016222 : {
4050 1016222 : constructor_elt ce = { index, NULL_TREE };
4051 :
4052 1016222 : vec_safe_insert (CONSTRUCTOR_ELTS (ctor), idx, ce);
4053 1016222 : cep = CONSTRUCTOR_ELT (ctor, idx);
4054 : }
4055 1475495 : found:;
4056 :
4057 1475495 : return cep;
4058 : }
4059 : }
4060 :
4061 : /* Under the control of CTX, issue a detailed diagnostic for
4062 : an out-of-bounds subscript INDEX into the expression ARRAY. */
4063 :
4064 : static void
4065 483 : diag_array_subscript (location_t loc, const constexpr_ctx *ctx, tree array, tree index)
4066 : {
4067 483 : if (!ctx->quiet)
4068 : {
4069 100 : tree arraytype = TREE_TYPE (array);
4070 :
4071 : /* Convert the unsigned array subscript to a signed integer to avoid
4072 : printing huge numbers for small negative values. */
4073 100 : tree sidx = fold_convert (ssizetype, index);
4074 100 : STRIP_ANY_LOCATION_WRAPPER (array);
4075 100 : if (DECL_P (array))
4076 : {
4077 74 : if (TYPE_DOMAIN (arraytype))
4078 68 : error_at (loc, "array subscript value %qE is outside the bounds "
4079 : "of array %qD of type %qT", sidx, array, arraytype);
4080 : else
4081 6 : error_at (loc, "nonzero array subscript %qE is used with array %qD of "
4082 : "type %qT with unknown bounds", sidx, array, arraytype);
4083 74 : inform (DECL_SOURCE_LOCATION (array), "declared here");
4084 : }
4085 26 : else if (TYPE_DOMAIN (arraytype))
4086 23 : error_at (loc, "array subscript value %qE is outside the bounds "
4087 : "of array type %qT", sidx, arraytype);
4088 : else
4089 3 : error_at (loc, "nonzero array subscript %qE is used with array of type %qT "
4090 : "with unknown bounds", sidx, arraytype);
4091 : }
4092 483 : }
4093 :
4094 : /* Return the number of elements for TYPE (which is an ARRAY_TYPE or
4095 : a VECTOR_TYPE). */
4096 :
4097 : static tree
4098 526091 : get_array_or_vector_nelts (const constexpr_ctx *ctx, tree type,
4099 : bool *non_constant_p, bool *overflow_p)
4100 : {
4101 526091 : tree nelts;
4102 526091 : if (TREE_CODE (type) == ARRAY_TYPE)
4103 : {
4104 526091 : if (TYPE_DOMAIN (type))
4105 525874 : nelts = array_type_nelts_top (type);
4106 : else
4107 217 : nelts = size_zero_node;
4108 : }
4109 0 : else if (VECTOR_TYPE_P (type))
4110 0 : nelts = size_int (TYPE_VECTOR_SUBPARTS (type));
4111 : else
4112 0 : gcc_unreachable ();
4113 :
4114 : /* For VLAs, the number of elements won't be an integer constant. */
4115 526091 : nelts = cxx_eval_constant_expression (ctx, nelts, vc_prvalue,
4116 : non_constant_p, overflow_p);
4117 526091 : return nelts;
4118 : }
4119 :
4120 : /* Extract element INDEX consisting of CHARS_PER_ELT chars from
4121 : STRING_CST STRING. */
4122 :
4123 : static tree
4124 82145 : extract_string_elt (tree string, unsigned chars_per_elt, unsigned index)
4125 : {
4126 82145 : tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (string)));
4127 82145 : tree r;
4128 :
4129 82145 : if (chars_per_elt == 1)
4130 75830 : r = build_int_cst (type, TREE_STRING_POINTER (string)[index]);
4131 : else
4132 : {
4133 6315 : const unsigned char *ptr
4134 6315 : = ((const unsigned char *)TREE_STRING_POINTER (string)
4135 6315 : + index * chars_per_elt);
4136 6315 : r = native_interpret_expr (type, ptr, chars_per_elt);
4137 : }
4138 82145 : return r;
4139 : }
4140 :
4141 : /* Subroutine of cxx_eval_array_reference. T is an ARRAY_REF; evaluate the
4142 : subscript, diagnose any problems with it, and return the result. */
4143 :
4144 : static tree
4145 527564 : eval_and_check_array_index (const constexpr_ctx *ctx,
4146 : tree t, bool allow_one_past,
4147 : bool *non_constant_p, bool *overflow_p)
4148 : {
4149 527564 : location_t loc = cp_expr_loc_or_input_loc (t);
4150 527564 : tree ary = TREE_OPERAND (t, 0);
4151 527564 : t = TREE_OPERAND (t, 1);
4152 527564 : tree index = cxx_eval_constant_expression (ctx, t, vc_prvalue,
4153 : non_constant_p, overflow_p);
4154 527564 : VERIFY_CONSTANT (index);
4155 :
4156 525814 : if (!tree_fits_shwi_p (index)
4157 525814 : || tree_int_cst_sgn (index) < 0)
4158 : {
4159 120 : diag_array_subscript (loc, ctx, ary, index);
4160 120 : *non_constant_p = true;
4161 120 : return t;
4162 : }
4163 :
4164 525694 : tree nelts = get_array_or_vector_nelts (ctx, TREE_TYPE (ary), non_constant_p,
4165 : overflow_p);
4166 525694 : VERIFY_CONSTANT (nelts);
4167 525628 : if (allow_one_past
4168 867416 : ? !tree_int_cst_le (index, nelts)
4169 341788 : : !tree_int_cst_lt (index, nelts))
4170 : {
4171 363 : diag_array_subscript (loc, ctx, ary, index);
4172 363 : *non_constant_p = true;
4173 363 : return t;
4174 : }
4175 :
4176 : return index;
4177 : }
4178 :
4179 : /* Subroutine of cxx_eval_constant_expression.
4180 : Attempt to reduce a reference to an array slot. */
4181 :
4182 : static tree
4183 589221 : cxx_eval_array_reference (const constexpr_ctx *ctx, tree t,
4184 : value_cat lval,
4185 : bool *non_constant_p, bool *overflow_p)
4186 : {
4187 589221 : tree oldary = TREE_OPERAND (t, 0);
4188 589221 : tree ary = cxx_eval_constant_expression (ctx, oldary,
4189 : lval,
4190 : non_constant_p, overflow_p);
4191 589221 : if (*non_constant_p)
4192 : return t;
4193 400919 : if (!lval
4194 215791 : && TREE_CODE (ary) == VIEW_CONVERT_EXPR
4195 13075 : && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary, 0)))
4196 413994 : && TREE_TYPE (t) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary, 0))))
4197 13075 : ary = TREE_OPERAND (ary, 0);
4198 :
4199 400919 : tree oldidx = TREE_OPERAND (t, 1);
4200 400919 : tree index = eval_and_check_array_index (ctx, t, lval,
4201 : non_constant_p, overflow_p);
4202 400919 : if (*non_constant_p)
4203 : return t;
4204 :
4205 398674 : if (lval && ary == oldary && index == oldidx)
4206 : return t;
4207 263504 : else if (lval == vc_discard)
4208 : return t;
4209 263236 : else if (lval)
4210 48203 : return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL);
4211 :
4212 215033 : unsigned len = 0, elem_nchars = 1;
4213 215033 : tree elem_type = TREE_TYPE (TREE_TYPE (ary));
4214 215033 : if (TREE_CODE (ary) == CONSTRUCTOR)
4215 119490 : len = CONSTRUCTOR_NELTS (ary);
4216 95543 : else if (TREE_CODE (ary) == STRING_CST)
4217 : {
4218 82480 : elem_nchars = (TYPE_PRECISION (elem_type)
4219 82480 : / TYPE_PRECISION (char_type_node));
4220 82480 : len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars;
4221 : }
4222 13063 : else if (TREE_CODE (ary) == VECTOR_CST)
4223 : /* We don't create variable-length VECTOR_CSTs. */
4224 13063 : len = VECTOR_CST_NELTS (ary).to_constant ();
4225 : else
4226 : {
4227 : /* We can't do anything with other tree codes, so use
4228 : VERIFY_CONSTANT to complain and fail. */
4229 0 : VERIFY_CONSTANT (ary);
4230 0 : gcc_unreachable ();
4231 : }
4232 :
4233 215033 : bool found;
4234 215033 : HOST_WIDE_INT i = 0;
4235 215033 : if (TREE_CODE (ary) == CONSTRUCTOR)
4236 : {
4237 119490 : HOST_WIDE_INT ix = find_array_ctor_elt (ary, index);
4238 119490 : found = (ix >= 0);
4239 119490 : if (found)
4240 116998 : i = ix;
4241 : }
4242 : else
4243 : {
4244 95543 : i = tree_to_shwi (index);
4245 95543 : found = (i < len);
4246 : }
4247 :
4248 215033 : if (found)
4249 : {
4250 211885 : tree r;
4251 211885 : if (TREE_CODE (ary) == CONSTRUCTOR)
4252 116998 : r = (*CONSTRUCTOR_ELTS (ary))[i].value;
4253 94887 : else if (TREE_CODE (ary) == VECTOR_CST)
4254 13063 : r = VECTOR_CST_ELT (ary, i);
4255 : else
4256 81824 : r = extract_string_elt (ary, elem_nchars, i);
4257 :
4258 211885 : if (r)
4259 : /* Don't VERIFY_CONSTANT here. */
4260 : return r;
4261 :
4262 : /* Otherwise the element doesn't have a value yet. */
4263 : }
4264 :
4265 : /* Not found. */
4266 :
4267 3148 : if (TREE_CODE (ary) == CONSTRUCTOR
4268 3148 : && CONSTRUCTOR_NO_CLEARING (ary))
4269 : {
4270 : /* 'ary' is part of the aggregate initializer we're currently
4271 : building; if there's no initializer for this element yet,
4272 : that's an error. */
4273 52 : if (!ctx->quiet)
4274 16 : error ("accessing uninitialized array element");
4275 52 : *non_constant_p = true;
4276 52 : return t;
4277 : }
4278 :
4279 : /* If it's within the array bounds but doesn't have an explicit
4280 : initializer, it's initialized from {}. But use build_value_init
4281 : directly for non-aggregates to avoid creating a garbage CONSTRUCTOR. */
4282 3096 : tree val;
4283 3096 : constexpr_ctx new_ctx;
4284 3096 : if (is_really_empty_class (elem_type, /*ignore_vptr*/false))
4285 46 : return build_constructor (elem_type, NULL);
4286 3050 : else if (CP_AGGREGATE_TYPE_P (elem_type))
4287 : {
4288 754 : tree empty_ctor = build_constructor (init_list_type_node, NULL);
4289 754 : val = digest_init (elem_type, empty_ctor, tf_warning_or_error);
4290 : }
4291 : else
4292 2296 : val = build_value_init (elem_type, tf_warning_or_error);
4293 :
4294 3050 : if (!SCALAR_TYPE_P (elem_type))
4295 : {
4296 775 : new_ctx = *ctx;
4297 775 : new_ctx.ctor = build_constructor (elem_type, NULL);
4298 775 : ctx = &new_ctx;
4299 : }
4300 3050 : t = cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
4301 : overflow_p);
4302 3050 : if (!SCALAR_TYPE_P (elem_type) && t != ctx->ctor)
4303 730 : free_constructor (ctx->ctor);
4304 : return t;
4305 : }
4306 :
4307 : /* Subroutine of cxx_eval_constant_expression.
4308 : Attempt to reduce a field access of a value of class type. */
4309 :
4310 : static tree
4311 6200206 : cxx_eval_component_reference (const constexpr_ctx *ctx, tree t,
4312 : value_cat lval,
4313 : bool *non_constant_p, bool *overflow_p)
4314 : {
4315 6200206 : unsigned HOST_WIDE_INT i;
4316 6200206 : tree field;
4317 6200206 : tree value;
4318 6200206 : tree part = TREE_OPERAND (t, 1);
4319 6200206 : tree orig_whole = TREE_OPERAND (t, 0);
4320 6200206 : tree whole = cxx_eval_constant_expression (ctx, orig_whole,
4321 : lval,
4322 : non_constant_p, overflow_p);
4323 6200206 : if (*non_constant_p)
4324 : return t;
4325 3113468 : if (INDIRECT_REF_P (whole)
4326 3113468 : && integer_zerop (TREE_OPERAND (whole, 0)))
4327 : {
4328 183 : if (!ctx->quiet)
4329 39 : error ("dereferencing a null pointer in %qE", orig_whole);
4330 183 : *non_constant_p = true;
4331 183 : return t;
4332 : }
4333 :
4334 3113285 : if (TREE_CODE (whole) == PTRMEM_CST)
4335 1256 : whole = cplus_expand_constant (whole);
4336 3113285 : if (whole == orig_whole)
4337 : return t;
4338 1515548 : if (lval == vc_discard)
4339 : return t;
4340 1515524 : if (lval)
4341 498825 : return fold_build3 (COMPONENT_REF, TREE_TYPE (t),
4342 : whole, part, NULL_TREE);
4343 : /* Don't VERIFY_CONSTANT here; we only want to check that we got a
4344 : CONSTRUCTOR. */
4345 1016699 : if (TREE_CODE (whole) != CONSTRUCTOR)
4346 : {
4347 0 : if (!ctx->quiet)
4348 0 : error ("%qE is not a constant expression", orig_whole);
4349 0 : *non_constant_p = true;
4350 0 : return t;
4351 : }
4352 1016530 : if ((cxx_dialect < cxx14 || CONSTRUCTOR_MUTABLE_POISON (whole))
4353 1017111 : && DECL_MUTABLE_P (part))
4354 : {
4355 129 : if (!ctx->quiet)
4356 15 : error ("mutable %qD is not usable in a constant expression", part);
4357 129 : *non_constant_p = true;
4358 129 : return t;
4359 : }
4360 1016570 : bool pmf = TYPE_PTRMEMFUNC_P (TREE_TYPE (whole));
4361 1373845 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
4362 : {
4363 : /* Use name match for PMF fields, as a variant will have a
4364 : different FIELD_DECL with a different type. */
4365 1372893 : if (pmf ? DECL_NAME (field) == DECL_NAME (part)
4366 : : field == part)
4367 : {
4368 1015618 : if (value)
4369 : {
4370 1015600 : STRIP_ANY_LOCATION_WRAPPER (value);
4371 1015600 : return value;
4372 : }
4373 : else
4374 : /* We're in the middle of initializing it. */
4375 : break;
4376 : }
4377 : }
4378 970 : if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE
4379 970 : && CONSTRUCTOR_NELTS (whole) > 0)
4380 : {
4381 : /* DR 1188 says we don't have to deal with this. */
4382 68 : if (!ctx->quiet)
4383 : {
4384 13 : constructor_elt *cep = CONSTRUCTOR_ELT (whole, 0);
4385 13 : if (cep->value == NULL_TREE)
4386 9 : error ("accessing uninitialized member %qD", part);
4387 : else
4388 4 : error ("accessing %qD member instead of initialized %qD member in "
4389 : "constant expression", part, cep->index);
4390 : }
4391 68 : *non_constant_p = true;
4392 68 : return t;
4393 : }
4394 :
4395 : /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty
4396 : classes never get represented; throw together a value now. */
4397 902 : if (is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
4398 400 : return build_constructor (TREE_TYPE (t), NULL);
4399 :
4400 502 : gcc_assert (DECL_CONTEXT (part) == TYPE_MAIN_VARIANT (TREE_TYPE (whole)));
4401 :
4402 502 : if (CONSTRUCTOR_NO_CLEARING (whole))
4403 : {
4404 : /* 'whole' is part of the aggregate initializer we're currently
4405 : building; if there's no initializer for this member yet, that's an
4406 : error. */
4407 209 : if (!ctx->quiet)
4408 17 : error ("accessing uninitialized member %qD", part);
4409 209 : *non_constant_p = true;
4410 209 : return t;
4411 : }
4412 :
4413 : /* If there's no explicit init for this field, it's value-initialized. */
4414 293 : value = build_value_init (TREE_TYPE (t), tf_warning_or_error);
4415 293 : return cxx_eval_constant_expression (ctx, value,
4416 : lval,
4417 293 : non_constant_p, overflow_p);
4418 : }
4419 :
4420 : /* Subroutine of cxx_eval_constant_expression.
4421 : Attempt to reduce a field access of a value of class type that is
4422 : expressed as a BIT_FIELD_REF. */
4423 :
4424 : static tree
4425 63 : cxx_eval_bit_field_ref (const constexpr_ctx *ctx, tree t,
4426 : value_cat lval,
4427 : bool *non_constant_p, bool *overflow_p)
4428 : {
4429 63 : tree orig_whole = TREE_OPERAND (t, 0);
4430 63 : tree retval, fldval, utype, mask;
4431 63 : bool fld_seen = false;
4432 63 : HOST_WIDE_INT istart, isize;
4433 63 : tree whole = cxx_eval_constant_expression (ctx, orig_whole,
4434 : lval,
4435 : non_constant_p, overflow_p);
4436 63 : tree start, field, value;
4437 63 : unsigned HOST_WIDE_INT i;
4438 :
4439 63 : if (whole == orig_whole)
4440 : return t;
4441 : /* Don't VERIFY_CONSTANT here; we only want to check that we got a
4442 : CONSTRUCTOR. */
4443 3 : if (!*non_constant_p
4444 3 : && TREE_CODE (whole) != VECTOR_CST
4445 0 : && TREE_CODE (whole) != CONSTRUCTOR)
4446 : {
4447 0 : if (!ctx->quiet)
4448 0 : error ("%qE is not a constant expression", orig_whole);
4449 0 : *non_constant_p = true;
4450 : }
4451 3 : if (*non_constant_p)
4452 : return t;
4453 :
4454 3 : if (TREE_CODE (whole) == VECTOR_CST || !INTEGRAL_TYPE_P (TREE_TYPE (t)))
4455 : {
4456 3 : if (tree r = fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
4457 : TREE_OPERAND (t, 1), TREE_OPERAND (t, 2)))
4458 : return r;
4459 0 : if (!ctx->quiet)
4460 0 : error ("%qE is not a constant expression", orig_whole);
4461 0 : *non_constant_p = true;
4462 0 : return t;
4463 : }
4464 :
4465 0 : start = TREE_OPERAND (t, 2);
4466 0 : istart = tree_to_shwi (start);
4467 0 : isize = tree_to_shwi (TREE_OPERAND (t, 1));
4468 0 : utype = TREE_TYPE (t);
4469 0 : if (!TYPE_UNSIGNED (utype))
4470 0 : utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1);
4471 0 : retval = build_int_cst (utype, 0);
4472 0 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
4473 : {
4474 0 : tree bitpos = bit_position (field);
4475 0 : STRIP_ANY_LOCATION_WRAPPER (value);
4476 0 : if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1))
4477 0 : return value;
4478 0 : if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE
4479 0 : && TREE_CODE (value) == INTEGER_CST
4480 0 : && tree_fits_shwi_p (bitpos)
4481 0 : && tree_fits_shwi_p (DECL_SIZE (field)))
4482 : {
4483 0 : HOST_WIDE_INT bit = tree_to_shwi (bitpos);
4484 0 : HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field));
4485 0 : HOST_WIDE_INT shift;
4486 0 : if (bit >= istart && bit + sz <= istart + isize)
4487 : {
4488 0 : fldval = fold_convert (utype, value);
4489 0 : mask = build_int_cst_type (utype, -1);
4490 0 : mask = fold_build2 (LSHIFT_EXPR, utype, mask,
4491 : size_int (TYPE_PRECISION (utype) - sz));
4492 0 : mask = fold_build2 (RSHIFT_EXPR, utype, mask,
4493 : size_int (TYPE_PRECISION (utype) - sz));
4494 0 : fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask);
4495 0 : shift = bit - istart;
4496 0 : if (BYTES_BIG_ENDIAN)
4497 : shift = TYPE_PRECISION (utype) - shift - sz;
4498 0 : fldval = fold_build2 (LSHIFT_EXPR, utype, fldval,
4499 : size_int (shift));
4500 0 : retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval);
4501 0 : fld_seen = true;
4502 : }
4503 : }
4504 : }
4505 0 : if (fld_seen)
4506 0 : return fold_convert (TREE_TYPE (t), retval);
4507 0 : gcc_unreachable ();
4508 : return error_mark_node;
4509 : }
4510 :
4511 : /* Helper for cxx_eval_bit_cast.
4512 : Check [bit.cast]/3 rules, bit_cast is constexpr only if the To and From
4513 : types and types of all subobjects have is_union_v<T>, is_pointer_v<T>,
4514 : is_member_pointer_v<T>, is_volatile_v<T> false and has no non-static
4515 : data members of reference type. */
4516 :
4517 : static bool
4518 3903 : check_bit_cast_type (const constexpr_ctx *ctx, location_t loc, tree type,
4519 : tree orig_type)
4520 : {
4521 3903 : if (TREE_CODE (type) == UNION_TYPE)
4522 : {
4523 27 : if (!ctx->quiet)
4524 : {
4525 9 : if (type == orig_type)
4526 6 : error_at (loc, "%qs is not a constant expression because %qT is "
4527 : "a union type", "__builtin_bit_cast", type);
4528 : else
4529 3 : error_at (loc, "%qs is not a constant expression because %qT "
4530 : "contains a union type", "__builtin_bit_cast",
4531 : orig_type);
4532 : }
4533 27 : return true;
4534 : }
4535 3876 : if (TREE_CODE (type) == POINTER_TYPE)
4536 : {
4537 38 : if (!ctx->quiet)
4538 : {
4539 12 : if (type == orig_type)
4540 6 : error_at (loc, "%qs is not a constant expression because %qT is "
4541 : "a pointer type", "__builtin_bit_cast", type);
4542 : else
4543 6 : error_at (loc, "%qs is not a constant expression because %qT "
4544 : "contains a pointer type", "__builtin_bit_cast",
4545 : orig_type);
4546 : }
4547 38 : return true;
4548 : }
4549 3838 : if (TREE_CODE (type) == REFERENCE_TYPE)
4550 : {
4551 0 : if (!ctx->quiet)
4552 : {
4553 0 : if (type == orig_type)
4554 0 : error_at (loc, "%qs is not a constant expression because %qT is "
4555 : "a reference type", "__builtin_bit_cast", type);
4556 : else
4557 0 : error_at (loc, "%qs is not a constant expression because %qT "
4558 : "contains a reference type", "__builtin_bit_cast",
4559 : orig_type);
4560 : }
4561 0 : return true;
4562 : }
4563 3838 : if (TYPE_PTRMEM_P (type))
4564 : {
4565 36 : if (!ctx->quiet)
4566 : {
4567 12 : if (type == orig_type)
4568 12 : error_at (loc, "%qs is not a constant expression because %qT is "
4569 : "a pointer to member type", "__builtin_bit_cast",
4570 : type);
4571 : else
4572 0 : error_at (loc, "%qs is not a constant expression because %qT "
4573 : "contains a pointer to member type",
4574 : "__builtin_bit_cast", orig_type);
4575 : }
4576 36 : return true;
4577 : }
4578 3802 : if (TYPE_VOLATILE (type))
4579 : {
4580 0 : if (!ctx->quiet)
4581 : {
4582 0 : if (type == orig_type)
4583 0 : error_at (loc, "%qs is not a constant expression because %qT is "
4584 : "volatile", "__builtin_bit_cast", type);
4585 : else
4586 0 : error_at (loc, "%qs is not a constant expression because %qT "
4587 : "contains a volatile subobject",
4588 : "__builtin_bit_cast", orig_type);
4589 : }
4590 0 : return true;
4591 : }
4592 3802 : if (TREE_CODE (type) == RECORD_TYPE)
4593 20410 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4594 19450 : if (TREE_CODE (field) == FIELD_DECL
4595 19450 : && check_bit_cast_type (ctx, loc, TREE_TYPE (field), orig_type))
4596 : return true;
4597 : return false;
4598 : }
4599 :
4600 : /* Helper function for cxx_eval_bit_cast. For unsigned char or
4601 : std::byte members of CONSTRUCTOR (recursively) if they contain
4602 : some indeterminate bits (as set in MASK), remove the ctor elts,
4603 : mark the CONSTRUCTOR as CONSTRUCTOR_NO_CLEARING and clear the
4604 : bits in MASK. */
4605 :
4606 : static void
4607 548 : clear_uchar_or_std_byte_in_mask (location_t loc, tree t, unsigned char *mask)
4608 : {
4609 548 : if (TREE_CODE (t) != CONSTRUCTOR)
4610 : return;
4611 :
4612 : unsigned i, j = 0;
4613 : tree index, value;
4614 1954 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), i, index, value)
4615 : {
4616 1406 : tree type = TREE_TYPE (value);
4617 1406 : if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
4618 2381 : && DECL_BIT_FIELD_TYPE (index) != NULL_TREE)
4619 : {
4620 266 : if (is_byte_access_type_not_plain_char (DECL_BIT_FIELD_TYPE (index)))
4621 : {
4622 135 : HOST_WIDE_INT fldsz = TYPE_PRECISION (TREE_TYPE (index));
4623 135 : gcc_assert (fldsz != 0);
4624 135 : HOST_WIDE_INT pos = int_byte_position (index);
4625 135 : HOST_WIDE_INT bpos
4626 135 : = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (index));
4627 135 : bpos %= BITS_PER_UNIT;
4628 135 : HOST_WIDE_INT end
4629 135 : = ROUND_UP (bpos + fldsz, BITS_PER_UNIT) / BITS_PER_UNIT;
4630 135 : gcc_assert (end == 1 || end == 2);
4631 135 : unsigned char *p = mask + pos;
4632 135 : unsigned char mask_save[2];
4633 135 : mask_save[0] = mask[pos];
4634 135 : mask_save[1] = end == 2 ? mask[pos + 1] : 0;
4635 135 : if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
4636 : sorry_at (loc, "PDP11 bit-field handling unsupported"
4637 : " in %qs", "__builtin_bit_cast");
4638 135 : else if (BYTES_BIG_ENDIAN)
4639 : {
4640 : /* Big endian. */
4641 : if (bpos + fldsz <= BITS_PER_UNIT)
4642 : *p &= ~(((1 << fldsz) - 1)
4643 : << (BITS_PER_UNIT - bpos - fldsz));
4644 : else
4645 : {
4646 : gcc_assert (bpos);
4647 : *p &= ~(((1U << BITS_PER_UNIT) - 1) >> bpos);
4648 : p++;
4649 : fldsz -= BITS_PER_UNIT - bpos;
4650 : gcc_assert (fldsz && fldsz < BITS_PER_UNIT);
4651 : *p &= ((1U << BITS_PER_UNIT) - 1) >> fldsz;
4652 : }
4653 : }
4654 : else
4655 : {
4656 : /* Little endian. */
4657 135 : if (bpos + fldsz <= BITS_PER_UNIT)
4658 135 : *p &= ~(((1 << fldsz) - 1) << bpos);
4659 : else
4660 : {
4661 0 : gcc_assert (bpos);
4662 0 : *p &= ~(((1 << BITS_PER_UNIT) - 1) << bpos);
4663 0 : p++;
4664 0 : fldsz -= BITS_PER_UNIT - bpos;
4665 0 : gcc_assert (fldsz && fldsz < BITS_PER_UNIT);
4666 0 : *p &= ~((1 << fldsz) - 1);
4667 : }
4668 : }
4669 135 : if (mask_save[0] != mask[pos]
4670 99 : || (end == 2 && mask_save[1] != mask[pos + 1]))
4671 : {
4672 36 : CONSTRUCTOR_NO_CLEARING (t) = 1;
4673 36 : continue;
4674 : }
4675 : }
4676 : }
4677 1140 : else if (is_byte_access_type_not_plain_char (type))
4678 : {
4679 225 : HOST_WIDE_INT pos;
4680 225 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
4681 132 : pos = tree_to_shwi (index);
4682 : else
4683 93 : pos = int_byte_position (index);
4684 225 : if (mask[pos])
4685 : {
4686 48 : CONSTRUCTOR_NO_CLEARING (t) = 1;
4687 48 : mask[pos] = 0;
4688 48 : continue;
4689 : }
4690 : }
4691 1322 : if (TREE_CODE (value) == CONSTRUCTOR)
4692 : {
4693 260 : HOST_WIDE_INT pos;
4694 260 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
4695 144 : pos = tree_to_shwi (index)
4696 72 : * tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))));
4697 : else
4698 188 : pos = int_byte_position (index);
4699 260 : clear_uchar_or_std_byte_in_mask (loc, value, mask + pos);
4700 : }
4701 1322 : if (i != j)
4702 : {
4703 180 : CONSTRUCTOR_ELT (t, j)->index = index;
4704 180 : CONSTRUCTOR_ELT (t, j)->value = value;
4705 : }
4706 1322 : ++j;
4707 : }
4708 1096 : if (CONSTRUCTOR_NELTS (t) != j)
4709 84 : vec_safe_truncate (CONSTRUCTOR_ELTS (t), j);
4710 : }
4711 :
4712 : /* Subroutine of cxx_eval_constant_expression.
4713 : Attempt to evaluate a BIT_CAST_EXPR. */
4714 :
4715 : static tree
4716 823 : cxx_eval_bit_cast (const constexpr_ctx *ctx, tree t, bool *non_constant_p,
4717 : bool *overflow_p)
4718 : {
4719 823 : if (check_bit_cast_type (ctx, EXPR_LOCATION (t), TREE_TYPE (t),
4720 823 : TREE_TYPE (t))
4721 2659 : || check_bit_cast_type (ctx, cp_expr_loc_or_loc (TREE_OPERAND (t, 0),
4722 769 : EXPR_LOCATION (t)),
4723 769 : TREE_TYPE (TREE_OPERAND (t, 0)),
4724 769 : TREE_TYPE (TREE_OPERAND (t, 0))))
4725 : {
4726 101 : *non_constant_p = true;
4727 101 : return t;
4728 : }
4729 :
4730 722 : tree op = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), vc_prvalue,
4731 : non_constant_p, overflow_p);
4732 722 : if (*non_constant_p)
4733 : return t;
4734 :
4735 655 : location_t loc = EXPR_LOCATION (t);
4736 655 : if (BITS_PER_UNIT != 8 || CHAR_BIT != 8)
4737 : {
4738 : if (!ctx->quiet)
4739 : sorry_at (loc, "%qs cannot be constant evaluated on the target",
4740 : "__builtin_bit_cast");
4741 : *non_constant_p = true;
4742 : return t;
4743 : }
4744 :
4745 655 : if (!tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (t))))
4746 : {
4747 0 : if (!ctx->quiet)
4748 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
4749 : "type is too large", "__builtin_bit_cast");
4750 0 : *non_constant_p = true;
4751 0 : return t;
4752 : }
4753 :
4754 655 : HOST_WIDE_INT len = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (t)));
4755 655 : if (len < 0 || (int) len != len)
4756 : {
4757 0 : if (!ctx->quiet)
4758 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
4759 : "type is too large", "__builtin_bit_cast");
4760 0 : *non_constant_p = true;
4761 0 : return t;
4762 : }
4763 :
4764 655 : unsigned char buf[64];
4765 655 : unsigned char *ptr, *mask;
4766 655 : size_t alen = (size_t) len * 2;
4767 655 : if (alen <= sizeof (buf))
4768 : ptr = buf;
4769 : else
4770 3 : ptr = XNEWVEC (unsigned char, alen);
4771 655 : mask = ptr + (size_t) len;
4772 : /* At the beginning consider everything indeterminate. */
4773 655 : memset (mask, ~0, (size_t) len);
4774 :
4775 655 : if (native_encode_initializer (op, ptr, len, 0, mask) != len)
4776 : {
4777 0 : if (!ctx->quiet)
4778 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
4779 : "argument cannot be encoded", "__builtin_bit_cast");
4780 0 : *non_constant_p = true;
4781 0 : if (ptr != buf)
4782 0 : XDELETE (ptr);
4783 0 : return t;
4784 : }
4785 :
4786 655 : tree r = NULL_TREE;
4787 655 : if (can_native_interpret_type_p (TREE_TYPE (t)))
4788 : {
4789 367 : r = native_interpret_expr (TREE_TYPE (t), ptr, len);
4790 367 : if (is_byte_access_type_not_plain_char (TREE_TYPE (t)))
4791 : {
4792 48 : gcc_assert (len == 1);
4793 48 : if (mask[0])
4794 : {
4795 24 : memset (mask, 0, len);
4796 24 : r = build_constructor (TREE_TYPE (r), NULL);
4797 24 : CONSTRUCTOR_NO_CLEARING (r) = 1;
4798 : }
4799 : }
4800 : }
4801 288 : else if (TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE)
4802 : {
4803 288 : r = native_interpret_aggregate (TREE_TYPE (t), ptr, 0, len);
4804 288 : if (r != NULL_TREE)
4805 : {
4806 288 : clear_type_padding_in_mask (TREE_TYPE (t), mask);
4807 288 : clear_uchar_or_std_byte_in_mask (loc, r, mask);
4808 288 : if (CHECKING_P)
4809 : {
4810 288 : tree e = cxx_eval_bare_aggregate (ctx, r, vc_prvalue,
4811 : non_constant_p, overflow_p);
4812 288 : gcc_checking_assert (e == r);
4813 : r = e;
4814 : }
4815 : }
4816 : }
4817 :
4818 655 : if (r != NULL_TREE)
4819 : {
4820 5349 : for (int i = 0; i < len; i++)
4821 4766 : if (mask[i])
4822 : {
4823 72 : if (!ctx->quiet)
4824 24 : error_at (loc, "%qs accessing uninitialized byte at offset %d",
4825 : "__builtin_bit_cast", i);
4826 72 : *non_constant_p = true;
4827 72 : r = t;
4828 72 : break;
4829 : }
4830 655 : if (ptr != buf)
4831 3 : XDELETE (ptr);
4832 655 : return r;
4833 : }
4834 :
4835 0 : if (!ctx->quiet)
4836 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
4837 : "argument cannot be interpreted", "__builtin_bit_cast");
4838 0 : *non_constant_p = true;
4839 0 : if (ptr != buf)
4840 0 : XDELETE (ptr);
4841 : return t;
4842 : }
4843 :
4844 : /* Subroutine of cxx_eval_constant_expression.
4845 : Evaluate a short-circuited logical expression T in the context
4846 : of a given constexpr CALL. BAILOUT_VALUE is the value for
4847 : early return. CONTINUE_VALUE is used here purely for
4848 : sanity check purposes. */
4849 :
4850 : static tree
4851 6111753 : cxx_eval_logical_expression (const constexpr_ctx *ctx, tree t,
4852 : tree bailout_value, tree continue_value,
4853 : bool *non_constant_p, bool *overflow_p)
4854 : {
4855 6111753 : tree r;
4856 6111753 : tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
4857 : vc_prvalue, non_constant_p,
4858 : overflow_p);
4859 6111753 : VERIFY_CONSTANT (lhs);
4860 5156465 : if (tree_int_cst_equal (lhs, bailout_value))
4861 : return lhs;
4862 4948175 : gcc_assert (tree_int_cst_equal (lhs, continue_value));
4863 4948175 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
4864 : vc_prvalue, non_constant_p,
4865 : overflow_p);
4866 4948175 : VERIFY_CONSTANT (r);
4867 : return r;
4868 : }
4869 :
4870 : /* REF is a COMPONENT_REF designating a particular field. V is a vector of
4871 : CONSTRUCTOR elements to initialize (part of) an object containing that
4872 : field. Return a pointer to the constructor_elt corresponding to the
4873 : initialization of the field. */
4874 :
4875 : static constructor_elt *
4876 0 : base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref)
4877 : {
4878 0 : tree aggr = TREE_OPERAND (ref, 0);
4879 0 : tree field = TREE_OPERAND (ref, 1);
4880 0 : HOST_WIDE_INT i;
4881 0 : constructor_elt *ce;
4882 :
4883 0 : gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
4884 :
4885 0 : if (TREE_CODE (aggr) == COMPONENT_REF)
4886 : {
4887 0 : constructor_elt *base_ce
4888 0 : = base_field_constructor_elt (v, aggr);
4889 0 : v = CONSTRUCTOR_ELTS (base_ce->value);
4890 : }
4891 :
4892 0 : for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
4893 0 : if (ce->index == field)
4894 0 : return ce;
4895 :
4896 0 : gcc_unreachable ();
4897 : return NULL;
4898 : }
4899 :
4900 : /* Some of the expressions fed to the constexpr mechanism are calls to
4901 : constructors, which have type void. In that case, return the type being
4902 : initialized by the constructor. */
4903 :
4904 : static tree
4905 259500717 : initialized_type (tree t)
4906 : {
4907 259629340 : if (TYPE_P (t))
4908 : return t;
4909 259629040 : tree type = TREE_TYPE (t);
4910 259629040 : if (TREE_CODE (t) == CALL_EXPR)
4911 : {
4912 : /* A constructor call has void type, so we need to look deeper. */
4913 22298231 : tree fn = get_function_named_in_call (t);
4914 22297765 : if (fn && TREE_CODE (fn) == FUNCTION_DECL
4915 44367421 : && DECL_CXX_CONSTRUCTOR_P (fn))
4916 905948 : type = DECL_CONTEXT (fn);
4917 : }
4918 237330809 : else if (TREE_CODE (t) == COMPOUND_EXPR)
4919 128623 : return initialized_type (TREE_OPERAND (t, 1));
4920 237202186 : else if (TREE_CODE (t) == AGGR_INIT_EXPR)
4921 220577 : type = TREE_TYPE (AGGR_INIT_EXPR_SLOT (t));
4922 259500417 : return cv_unqualified (type);
4923 : }
4924 :
4925 : /* We're about to initialize element INDEX of an array or class from VALUE.
4926 : Set up NEW_CTX appropriately by adjusting .object to refer to the
4927 : subobject and creating a new CONSTRUCTOR if the element is itself
4928 : a class or array. */
4929 :
4930 : static void
4931 523068 : init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx,
4932 : tree index, tree &value)
4933 : {
4934 523068 : new_ctx = *ctx;
4935 :
4936 523068 : if (index && TREE_CODE (index) != INTEGER_CST
4937 420081 : && TREE_CODE (index) != FIELD_DECL
4938 3 : && TREE_CODE (index) != RANGE_EXPR)
4939 : /* This won't have an element in the new CONSTRUCTOR. */
4940 : return;
4941 :
4942 523068 : tree type = initialized_type (value);
4943 523068 : if (!AGGREGATE_TYPE_P (type) && !VECTOR_TYPE_P (type))
4944 : /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
4945 : return;
4946 358905 : if (VECTOR_TYPE_P (type)
4947 9100 : && VECTOR_TYPE_P (TREE_TYPE (ctx->ctor))
4948 358959 : && index == NULL_TREE)
4949 : /* A vector inside of a vector CONSTRUCTOR, e.g. when a larger
4950 : vector is constructed from smaller vectors, doesn't get its own
4951 : CONSTRUCTOR either. */
4952 : return;
4953 :
4954 : /* The sub-aggregate initializer might contain a placeholder;
4955 : update object to refer to the subobject and ctor to refer to
4956 : the (newly created) sub-initializer. */
4957 358851 : if (ctx->object)
4958 : {
4959 236148 : if (index == NULL_TREE || TREE_CODE (index) == RANGE_EXPR)
4960 : /* There's no well-defined subobject for this index. */
4961 3 : new_ctx.object = NULL_TREE;
4962 : else
4963 236145 : new_ctx.object = build_ctor_subob_ref (index, type, ctx->object);
4964 : }
4965 :
4966 358851 : if (is_empty_class (type))
4967 : /* Leave ctor null for an empty subobject, they aren't represented in the
4968 : result of evaluation. */
4969 328294 : new_ctx.ctor = NULL_TREE;
4970 : else
4971 : {
4972 30557 : tree elt = build_constructor (type, NULL);
4973 30557 : CONSTRUCTOR_NO_CLEARING (elt) = true;
4974 30557 : new_ctx.ctor = elt;
4975 : }
4976 :
4977 358851 : if (TREE_CODE (value) == TARGET_EXPR)
4978 : /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
4979 12161 : value = TARGET_EXPR_INITIAL (value);
4980 : }
4981 :
4982 : /* We're about to process an initializer for a class or array TYPE. Make
4983 : sure that CTX is set up appropriately. */
4984 :
4985 : static void
4986 409154 : verify_ctor_sanity (const constexpr_ctx *ctx, tree type)
4987 : {
4988 : /* We don't bother building a ctor for an empty base subobject. */
4989 409154 : if (is_empty_class (type))
4990 : return;
4991 :
4992 : /* We're in the middle of an initializer that might involve placeholders;
4993 : our caller should have created a CONSTRUCTOR for us to put the
4994 : initializer into. We will either return that constructor or T. */
4995 82353 : gcc_assert (ctx->ctor);
4996 82353 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
4997 : (type, TREE_TYPE (ctx->ctor)));
4998 : /* We used to check that ctx->ctor was empty, but that isn't the case when
4999 : the object is zero-initialized before calling the constructor. */
5000 82353 : if (ctx->object)
5001 : {
5002 47658 : tree otype = TREE_TYPE (ctx->object);
5003 47658 : gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, otype)
5004 : /* Handle flexible array members. */
5005 : || (TREE_CODE (otype) == ARRAY_TYPE
5006 : && TYPE_DOMAIN (otype) == NULL_TREE
5007 : && TREE_CODE (type) == ARRAY_TYPE
5008 : && (same_type_ignoring_top_level_qualifiers_p
5009 : (TREE_TYPE (type), TREE_TYPE (otype)))));
5010 : }
5011 106291 : gcc_assert (!ctx->object || !DECL_P (ctx->object)
5012 : || ctx->global->get_value (ctx->object) == ctx->ctor);
5013 : }
5014 :
5015 : /* Subroutine of cxx_eval_constant_expression.
5016 : The expression tree T denotes a C-style array or a C-style
5017 : aggregate. Reduce it to a constant expression. */
5018 :
5019 : static tree
5020 408757 : cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t,
5021 : value_cat lval,
5022 : bool *non_constant_p, bool *overflow_p)
5023 : {
5024 408757 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
5025 408757 : bool changed = false;
5026 408757 : gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t));
5027 408757 : tree type = TREE_TYPE (t);
5028 :
5029 408757 : constexpr_ctx new_ctx;
5030 408757 : if (TYPE_PTRMEMFUNC_P (type) || VECTOR_TYPE_P (type))
5031 : {
5032 : /* We don't really need the ctx->ctor business for a PMF or
5033 : vector, but it's simpler to use the same code. */
5034 23304 : new_ctx = *ctx;
5035 23304 : new_ctx.ctor = build_constructor (type, NULL);
5036 23304 : new_ctx.object = NULL_TREE;
5037 23304 : ctx = &new_ctx;
5038 408757 : };
5039 408757 : verify_ctor_sanity (ctx, type);
5040 408757 : vec<constructor_elt, va_gc> **p = nullptr;
5041 408757 : if (ctx->ctor)
5042 : {
5043 408748 : p = &CONSTRUCTOR_ELTS (ctx->ctor);
5044 817407 : vec_alloc (*p, vec_safe_length (v));
5045 408748 : if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (t))
5046 15835 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor) = 1;
5047 : }
5048 :
5049 408757 : unsigned i;
5050 408757 : tree index, value;
5051 408757 : bool constant_p = true;
5052 408757 : bool side_effects_p = false;
5053 905350 : FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value)
5054 : {
5055 517223 : tree orig_value = value;
5056 : /* Like in cxx_eval_store_expression, omit entries for empty fields. */
5057 517223 : bool no_slot = TREE_CODE (type) == RECORD_TYPE && is_empty_field (index);
5058 517223 : init_subob_ctx (ctx, new_ctx, index, value);
5059 517223 : int pos_hint = -1;
5060 517223 : if (new_ctx.ctor != ctx->ctor && !no_slot)
5061 : {
5062 : /* If we built a new CONSTRUCTOR, attach it now so that other
5063 : initializers can refer to it. */
5064 24902 : constructor_elt *cep = get_or_insert_ctor_field (ctx->ctor, index);
5065 24902 : cep->value = new_ctx.ctor;
5066 24902 : pos_hint = cep - (*p)->begin();
5067 24902 : }
5068 492321 : else if (TREE_CODE (type) == UNION_TYPE)
5069 : /* Otherwise if we're constructing a non-aggregate union member, set
5070 : the active union member now so that we can later detect and diagnose
5071 : if its initializer attempts to activate another member. */
5072 959 : get_or_insert_ctor_field (ctx->ctor, index);
5073 517223 : tree elt = cxx_eval_constant_expression (&new_ctx, value,
5074 : lval,
5075 : non_constant_p, overflow_p);
5076 : /* Don't VERIFY_CONSTANT here. */
5077 517223 : if (ctx->quiet && *non_constant_p)
5078 : break;
5079 496593 : if (elt != orig_value)
5080 72602 : changed = true;
5081 :
5082 496593 : if (!TREE_CONSTANT (elt))
5083 28655 : constant_p = false;
5084 496593 : if (TREE_SIDE_EFFECTS (elt))
5085 24 : side_effects_p = true;
5086 496593 : if (index && TREE_CODE (index) == COMPONENT_REF)
5087 : {
5088 : /* This is an initialization of a vfield inside a base
5089 : subaggregate that we already initialized; push this
5090 : initialization into the previous initialization. */
5091 0 : constructor_elt *inner = base_field_constructor_elt (*p, index);
5092 0 : inner->value = elt;
5093 0 : changed = true;
5094 0 : }
5095 496593 : else if (no_slot)
5096 : /* This is an initializer for an empty field; now that we've
5097 : checked that it's constant, we can ignore it. */
5098 : changed = true;
5099 168450 : else if (index
5100 168320 : && (TREE_CODE (index) == NOP_EXPR
5101 168320 : || TREE_CODE (index) == POINTER_PLUS_EXPR))
5102 : {
5103 : /* Old representation of empty bases. FIXME remove. */
5104 0 : gcc_checking_assert (false);
5105 : gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index))));
5106 : changed = true;
5107 : }
5108 : else
5109 : {
5110 168450 : if (TREE_CODE (type) == UNION_TYPE
5111 168450 : && (*p)->last().index != index)
5112 : /* The initializer erroneously changed the active union member that
5113 : we're initializing. */
5114 8 : gcc_assert (*non_constant_p);
5115 : else
5116 : {
5117 : /* The initializer might have mutated the underlying CONSTRUCTOR,
5118 : so recompute the location of the target constructer_elt. */
5119 168442 : constructor_elt *cep
5120 168442 : = get_or_insert_ctor_field (ctx->ctor, index, pos_hint);
5121 168442 : cep->value = elt;
5122 : }
5123 :
5124 : /* Adding or replacing an element might change the ctor's flags. */
5125 168450 : TREE_CONSTANT (ctx->ctor) = constant_p;
5126 168450 : TREE_SIDE_EFFECTS (ctx->ctor) = side_effects_p;
5127 : }
5128 : }
5129 408757 : if (*non_constant_p)
5130 : return t;
5131 388094 : if (!changed)
5132 : {
5133 21304 : if (VECTOR_TYPE_P (type))
5134 13151 : t = fold (t);
5135 21304 : return t;
5136 : }
5137 366790 : t = ctx->ctor;
5138 366790 : if (!t)
5139 9 : t = build_constructor (type, NULL);
5140 : /* We're done building this CONSTRUCTOR, so now we can interpret an
5141 : element without an explicit initializer as value-initialized. */
5142 366790 : CONSTRUCTOR_NO_CLEARING (t) = false;
5143 366790 : TREE_CONSTANT (t) = constant_p;
5144 366790 : TREE_SIDE_EFFECTS (t) = side_effects_p;
5145 366790 : if (VECTOR_TYPE_P (type))
5146 1106 : t = fold (t);
5147 : return t;
5148 : }
5149 :
5150 : /* Subroutine of cxx_eval_constant_expression.
5151 : The expression tree T is a VEC_INIT_EXPR which denotes the desired
5152 : initialization of a non-static data member of array type. Reduce it to a
5153 : CONSTRUCTOR.
5154 :
5155 : Note that apart from value-initialization (when VALUE_INIT is true),
5156 : this is only intended to support value-initialization and the
5157 : initializations done by defaulted constructors for classes with
5158 : non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
5159 : will either be NULL_TREE for the default constructor, or a COMPONENT_REF
5160 : for the copy/move constructor. */
5161 :
5162 : static tree
5163 397 : cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init,
5164 : bool value_init, value_cat lval,
5165 : bool *non_constant_p, bool *overflow_p)
5166 : {
5167 397 : tree elttype = TREE_TYPE (atype);
5168 397 : verify_ctor_sanity (ctx, atype);
5169 397 : vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
5170 397 : bool pre_init = false;
5171 397 : unsigned HOST_WIDE_INT i;
5172 397 : tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
5173 :
5174 397 : if (init && TREE_CODE (init) == CONSTRUCTOR)
5175 0 : return cxx_eval_bare_aggregate (ctx, init, lval,
5176 0 : non_constant_p, overflow_p);
5177 :
5178 : /* For the default constructor, build up a call to the default
5179 : constructor of the element type. We only need to handle class types
5180 : here, as for a constructor to be constexpr, all members must be
5181 : initialized, which for a defaulted default constructor means they must
5182 : be of a class type with a constexpr default constructor. */
5183 397 : if (TREE_CODE (elttype) == ARRAY_TYPE)
5184 : /* We only do this at the lowest level. */;
5185 358 : else if (value_init)
5186 : {
5187 77 : init = build_value_init (elttype, complain);
5188 77 : pre_init = true;
5189 : }
5190 281 : else if (!init)
5191 : {
5192 238 : releasing_vec argvec;
5193 238 : init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
5194 : &argvec, elttype, LOOKUP_NORMAL,
5195 : complain);
5196 238 : init = build_aggr_init_expr (elttype, init);
5197 238 : pre_init = true;
5198 238 : }
5199 :
5200 397 : bool zeroed_out = false;
5201 397 : if (!CONSTRUCTOR_NO_CLEARING (ctx->ctor))
5202 : {
5203 : /* We're initializing an array object that had been zero-initialized
5204 : earlier. Truncate ctx->ctor, and propagate its zeroed state by
5205 : clearing CONSTRUCTOR_NO_CLEARING on each of the aggregate element
5206 : initializers we append to it. */
5207 148 : gcc_checking_assert (initializer_zerop (ctx->ctor));
5208 148 : zeroed_out = true;
5209 148 : vec_safe_truncate (*p, 0);
5210 : }
5211 :
5212 397 : tree nelts = get_array_or_vector_nelts (ctx, atype, non_constant_p,
5213 : overflow_p);
5214 397 : unsigned HOST_WIDE_INT max = tree_to_uhwi (nelts);
5215 6045 : for (i = 0; i < max; ++i)
5216 : {
5217 5845 : tree idx = build_int_cst (size_type_node, i);
5218 5845 : tree eltinit;
5219 5845 : bool reuse = false;
5220 5845 : constexpr_ctx new_ctx;
5221 6145 : init_subob_ctx (ctx, new_ctx, idx, pre_init ? init : elttype);
5222 5845 : if (new_ctx.ctor != ctx->ctor)
5223 : {
5224 5769 : if (zeroed_out)
5225 5437 : CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = false;
5226 5769 : CONSTRUCTOR_APPEND_ELT (*p, idx, new_ctx.ctor);
5227 : }
5228 5845 : if (TREE_CODE (elttype) == ARRAY_TYPE)
5229 : {
5230 : /* A multidimensional array; recurse. */
5231 171 : if (value_init || init == NULL_TREE)
5232 : {
5233 163 : eltinit = NULL_TREE;
5234 163 : reuse = i == 0;
5235 : }
5236 : else
5237 8 : eltinit = cp_build_array_ref (input_location, init, idx, complain);
5238 171 : eltinit = cxx_eval_vec_init_1 (&new_ctx, elttype, eltinit, value_init,
5239 : lval,
5240 : non_constant_p, overflow_p);
5241 : }
5242 5674 : else if (pre_init)
5243 : {
5244 : /* Initializing an element using value or default initialization
5245 : we just pre-built above. */
5246 5545 : if (init == void_node)
5247 : /* Trivial default-init, don't do anything to the CONSTRUCTOR. */
5248 3 : return ctx->ctor;
5249 5542 : eltinit = cxx_eval_constant_expression (&new_ctx, init, lval,
5250 : non_constant_p, overflow_p);
5251 5542 : reuse = i == 0;
5252 : }
5253 : else
5254 : {
5255 : /* Copying an element. */
5256 129 : eltinit = cp_build_array_ref (input_location, init, idx, complain);
5257 129 : if (!lvalue_p (init))
5258 92 : eltinit = move (eltinit);
5259 129 : eltinit = (perform_implicit_conversion_flags
5260 129 : (elttype, eltinit, complain,
5261 : LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING));
5262 129 : eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval,
5263 : non_constant_p, overflow_p);
5264 : }
5265 5842 : if (*non_constant_p)
5266 : break;
5267 5785 : if (new_ctx.ctor != ctx->ctor)
5268 : {
5269 : /* We appended this element above; update the value. */
5270 5716 : gcc_assert ((*p)->last().index == idx);
5271 5716 : (*p)->last().value = eltinit;
5272 : }
5273 : else
5274 69 : CONSTRUCTOR_APPEND_ELT (*p, idx, eltinit);
5275 : /* Reuse the result of cxx_eval_constant_expression call
5276 : from the first iteration to all others if it is a constant
5277 : initializer that doesn't require relocations. */
5278 5785 : if (reuse
5279 5785 : && max > 1
5280 5785 : && (eltinit == NULL_TREE
5281 285 : || (initializer_constant_valid_p (eltinit, TREE_TYPE (eltinit))
5282 285 : == null_pointer_node)))
5283 : {
5284 137 : if (new_ctx.ctor != ctx->ctor)
5285 74 : eltinit = new_ctx.ctor;
5286 137 : tree range = build2 (RANGE_EXPR, size_type_node,
5287 137 : build_int_cst (size_type_node, 1),
5288 137 : build_int_cst (size_type_node, max - 1));
5289 137 : CONSTRUCTOR_APPEND_ELT (*p, range, unshare_constructor (eltinit));
5290 137 : break;
5291 : }
5292 5648 : else if (i == 0)
5293 197 : vec_safe_reserve (*p, max);
5294 : }
5295 :
5296 394 : if (!*non_constant_p)
5297 : {
5298 337 : init = ctx->ctor;
5299 337 : CONSTRUCTOR_NO_CLEARING (init) = false;
5300 : }
5301 394 : return init;
5302 : }
5303 :
5304 : static tree
5305 295 : cxx_eval_vec_init (const constexpr_ctx *ctx, tree t,
5306 : value_cat lval,
5307 : bool *non_constant_p, bool *overflow_p)
5308 : {
5309 295 : tree atype = TREE_TYPE (t);
5310 295 : tree init = VEC_INIT_EXPR_INIT (t);
5311 295 : bool value_init = VEC_INIT_EXPR_VALUE_INIT (t);
5312 295 : if (!init || !BRACE_ENCLOSED_INITIALIZER_P (init))
5313 : ;
5314 70 : else if (CONSTRUCTOR_NELTS (init) == 0
5315 70 : && !CP_AGGREGATE_TYPE_P (strip_array_types (atype)))
5316 : {
5317 : /* Handle {} as value-init. */
5318 : init = NULL_TREE;
5319 : value_init = true;
5320 : }
5321 : else
5322 : {
5323 : /* This is a more complicated case, like needing to loop over trailing
5324 : elements; call build_vec_init and evaluate the result. */
5325 69 : tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
5326 69 : constexpr_ctx new_ctx = *ctx;
5327 69 : if (!ctx->object)
5328 : {
5329 : /* We want to have an initialization target for an VEC_INIT_EXPR.
5330 : If we don't already have one in CTX, use the VEC_INIT_EXPR_SLOT. */
5331 42 : new_ctx.object = VEC_INIT_EXPR_SLOT (t);
5332 42 : tree ctor = new_ctx.ctor = build_constructor (atype, NULL);
5333 42 : CONSTRUCTOR_NO_CLEARING (ctor) = true;
5334 42 : ctx->global->put_value (new_ctx.object, ctor);
5335 42 : ctx = &new_ctx;
5336 : }
5337 69 : init = expand_vec_init_expr (ctx->object, t, complain);
5338 69 : return cxx_eval_constant_expression (ctx, init, lval, non_constant_p,
5339 : overflow_p);
5340 : }
5341 226 : tree r = cxx_eval_vec_init_1 (ctx, atype, init, value_init,
5342 : lval, non_constant_p, overflow_p);
5343 226 : if (*non_constant_p)
5344 : return t;
5345 : else
5346 188 : return r;
5347 : }
5348 :
5349 : /* Like same_type_ignoring_top_level_qualifiers_p, but also handle the case
5350 : where the desired type is an array of unknown bounds because the variable
5351 : has had its bounds deduced since the wrapping expression was created. */
5352 :
5353 : static bool
5354 52250570 : same_type_ignoring_tlq_and_bounds_p (tree type1, tree type2)
5355 : {
5356 52250570 : while (TREE_CODE (type1) == ARRAY_TYPE
5357 13419 : && TREE_CODE (type2) == ARRAY_TYPE
5358 52250576 : && (!TYPE_DOMAIN (type1) || !TYPE_DOMAIN (type2)))
5359 : {
5360 3 : type1 = TREE_TYPE (type1);
5361 3 : type2 = TREE_TYPE (type2);
5362 : }
5363 52250570 : return same_type_ignoring_top_level_qualifiers_p (type1, type2);
5364 : }
5365 :
5366 : /* Try to determine the currently active union member for an expression
5367 : with UNION_TYPE. If it can be determined, return the FIELD_DECL,
5368 : otherwise return NULL_TREE. */
5369 :
5370 : static tree
5371 489 : cxx_union_active_member (const constexpr_ctx *ctx, tree t)
5372 : {
5373 489 : constexpr_ctx new_ctx = *ctx;
5374 489 : new_ctx.quiet = true;
5375 489 : bool non_constant_p = false, overflow_p = false;
5376 489 : tree ctor = cxx_eval_constant_expression (&new_ctx, t, vc_prvalue,
5377 : &non_constant_p,
5378 : &overflow_p);
5379 489 : if (TREE_CODE (ctor) == CONSTRUCTOR
5380 372 : && CONSTRUCTOR_NELTS (ctor) == 1
5381 372 : && CONSTRUCTOR_ELT (ctor, 0)->index
5382 861 : && TREE_CODE (CONSTRUCTOR_ELT (ctor, 0)->index) == FIELD_DECL)
5383 : return CONSTRUCTOR_ELT (ctor, 0)->index;
5384 : return NULL_TREE;
5385 : }
5386 :
5387 : /* Helper function for cxx_fold_indirect_ref_1, called recursively. */
5388 :
5389 : static tree
5390 416504 : cxx_fold_indirect_ref_1 (const constexpr_ctx *ctx, location_t loc, tree type,
5391 : tree op, unsigned HOST_WIDE_INT off, bool *empty_base)
5392 : {
5393 673999 : tree optype = TREE_TYPE (op);
5394 673999 : unsigned HOST_WIDE_INT const_nunits;
5395 673999 : if (off == 0 && similar_type_p (optype, type))
5396 : return op;
5397 412629 : else if (TREE_CODE (optype) == COMPLEX_TYPE
5398 412629 : && similar_type_p (type, TREE_TYPE (optype)))
5399 : {
5400 : /* *(foo *)&complexfoo => __real__ complexfoo */
5401 0 : if (off == 0)
5402 0 : return build1_loc (loc, REALPART_EXPR, type, op);
5403 : /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
5404 0 : else if (tree_to_uhwi (TYPE_SIZE_UNIT (type)) == off)
5405 0 : return build1_loc (loc, IMAGPART_EXPR, type, op);
5406 : }
5407 : /* ((foo*)&vectorfoo)[x] => BIT_FIELD_REF<vectorfoo,...> */
5408 412629 : else if (VECTOR_TYPE_P (optype)
5409 0 : && similar_type_p (type, TREE_TYPE (optype))
5410 412629 : && TYPE_VECTOR_SUBPARTS (optype).is_constant (&const_nunits))
5411 : {
5412 0 : unsigned HOST_WIDE_INT part_width = tree_to_uhwi (TYPE_SIZE_UNIT (type));
5413 0 : unsigned HOST_WIDE_INT max_offset = part_width * const_nunits;
5414 0 : if (off < max_offset && off % part_width == 0)
5415 : {
5416 0 : tree index = bitsize_int (off * BITS_PER_UNIT);
5417 0 : return build3_loc (loc, BIT_FIELD_REF, type, op,
5418 0 : TYPE_SIZE (type), index);
5419 : }
5420 : }
5421 : /* ((foo *)&fooarray)[x] => fooarray[x] */
5422 412629 : else if (TREE_CODE (optype) == ARRAY_TYPE
5423 257495 : && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (optype)))
5424 670124 : && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (optype))))
5425 : {
5426 257495 : tree type_domain = TYPE_DOMAIN (optype);
5427 257495 : tree min_val = size_zero_node;
5428 257495 : if (type_domain && TYPE_MIN_VALUE (type_domain))
5429 257485 : min_val = TYPE_MIN_VALUE (type_domain);
5430 257495 : unsigned HOST_WIDE_INT el_sz
5431 257495 : = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (optype)));
5432 257495 : unsigned HOST_WIDE_INT idx = off / el_sz;
5433 257495 : unsigned HOST_WIDE_INT rem = off % el_sz;
5434 257495 : if (tree_fits_uhwi_p (min_val))
5435 : {
5436 257495 : tree index = size_int (idx + tree_to_uhwi (min_val));
5437 257495 : op = build4_loc (loc, ARRAY_REF, TREE_TYPE (optype), op, index,
5438 : NULL_TREE, NULL_TREE);
5439 257495 : return cxx_fold_indirect_ref_1 (ctx, loc, type, op, rem,
5440 257495 : empty_base);
5441 : }
5442 : }
5443 : /* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */
5444 155134 : else if (TREE_CODE (optype) == RECORD_TYPE
5445 155134 : || TREE_CODE (optype) == UNION_TYPE)
5446 : {
5447 124919 : if (TREE_CODE (optype) == UNION_TYPE)
5448 : /* For unions prefer the currently active member. */
5449 489 : if (tree field = cxx_union_active_member (ctx, op))
5450 : {
5451 372 : unsigned HOST_WIDE_INT el_sz
5452 372 : = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field)));
5453 372 : if (off < el_sz)
5454 : {
5455 372 : tree cop = build3 (COMPONENT_REF, TREE_TYPE (field),
5456 : op, field, NULL_TREE);
5457 372 : if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop,
5458 : off, empty_base))
5459 : return ret;
5460 : }
5461 : }
5462 :
5463 : /* Handle conversion to an empty base class, which is represented with a
5464 : NOP_EXPR. Do this before spelunking into the non-empty subobjects,
5465 : which is likely to be a waste of time (109678). */
5466 124565 : if (is_empty_class (type)
5467 59069 : && CLASS_TYPE_P (optype)
5468 183634 : && lookup_base (optype, type, ba_any, NULL, tf_none, off))
5469 : {
5470 37644 : if (empty_base)
5471 11607 : *empty_base = true;
5472 37644 : return op;
5473 : }
5474 :
5475 86921 : for (tree field = TYPE_FIELDS (optype);
5476 473009 : field; field = DECL_CHAIN (field))
5477 414749 : if (TREE_CODE (field) == FIELD_DECL
5478 115694 : && TREE_TYPE (field) != error_mark_node
5479 530443 : && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (field))))
5480 : {
5481 115694 : tree pos = byte_position (field);
5482 115694 : if (!tree_fits_uhwi_p (pos))
5483 0 : continue;
5484 115694 : unsigned HOST_WIDE_INT upos = tree_to_uhwi (pos);
5485 115694 : unsigned HOST_WIDE_INT el_sz
5486 115694 : = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field)));
5487 115694 : if (upos <= off && off < upos + el_sz)
5488 : {
5489 86816 : tree cop = build3 (COMPONENT_REF, TREE_TYPE (field),
5490 : op, field, NULL_TREE);
5491 86816 : if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop,
5492 : off - upos,
5493 : empty_base))
5494 28661 : return ret;
5495 : }
5496 : }
5497 : }
5498 :
5499 : return NULL_TREE;
5500 : }
5501 :
5502 : /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
5503 : match. We want to be less strict for simple *& folding; if we have a
5504 : non-const temporary that we access through a const pointer, that should
5505 : work. We handle this here rather than change fold_indirect_ref_1
5506 : because we're dealing with things like ADDR_EXPR of INTEGER_CST which
5507 : don't really make sense outside of constant expression evaluation. Also
5508 : we want to allow folding to COMPONENT_REF, which could cause trouble
5509 : with TBAA in fold_indirect_ref_1. */
5510 :
5511 : static tree
5512 13397089 : cxx_fold_indirect_ref (const constexpr_ctx *ctx, location_t loc, tree type,
5513 : tree op0, bool *empty_base /* = NULL*/)
5514 : {
5515 13397089 : tree sub = op0;
5516 13397089 : tree subtype;
5517 13397089 : poly_uint64 const_op01;
5518 :
5519 : /* STRIP_NOPS, but stop if REINTERPRET_CAST_P. */
5520 17705916 : while (CONVERT_EXPR_P (sub) || TREE_CODE (sub) == NON_LVALUE_EXPR
5521 39641812 : || TREE_CODE (sub) == VIEW_CONVERT_EXPR)
5522 : {
5523 11194688 : if (TREE_CODE (sub) == NOP_EXPR
5524 11194688 : && REINTERPRET_CAST_P (sub))
5525 : return NULL_TREE;
5526 11169349 : sub = TREE_OPERAND (sub, 0);
5527 : }
5528 :
5529 13371750 : subtype = TREE_TYPE (sub);
5530 13371750 : if (!INDIRECT_TYPE_P (subtype))
5531 : return NULL_TREE;
5532 :
5533 : /* Canonicalizes the given OBJ/OFF pair by iteratively absorbing
5534 : the innermost component into the offset until it would make the
5535 : offset positive, so that cxx_fold_indirect_ref_1 can identify
5536 : more folding opportunities. */
5537 11939163 : auto canonicalize_obj_off = [] (tree& obj, tree& off) {
5538 329316 : while (TREE_CODE (obj) == COMPONENT_REF
5539 354875 : && (tree_int_cst_sign_bit (off) || integer_zerop (off)))
5540 : {
5541 27340 : tree field = TREE_OPERAND (obj, 1);
5542 27340 : tree pos = byte_position (field);
5543 27340 : if (integer_zerop (off) && integer_nonzerop (pos))
5544 : /* If the offset is already 0, keep going as long as the
5545 : component is at position 0. */
5546 : break;
5547 25559 : off = int_const_binop (PLUS_EXPR, off, pos);
5548 25559 : obj = TREE_OPERAND (obj, 0);
5549 : }
5550 329316 : };
5551 :
5552 11609847 : if (TREE_CODE (sub) == ADDR_EXPR)
5553 : {
5554 4031132 : tree op = TREE_OPERAND (sub, 0);
5555 4031132 : tree optype = TREE_TYPE (op);
5556 :
5557 : /* *&CONST_DECL -> to the value of the const decl. */
5558 4031132 : if (TREE_CODE (op) == CONST_DECL)
5559 0 : return DECL_INITIAL (op);
5560 : /* *&p => p; make sure to handle *&"str"[cst] here. */
5561 4031132 : if (similar_type_p (optype, type))
5562 : {
5563 3914731 : tree fop = fold_read_from_constant_string (op);
5564 3914731 : if (fop)
5565 : return fop;
5566 : else
5567 3910918 : return op;
5568 : }
5569 : else
5570 : {
5571 116401 : tree off = integer_zero_node;
5572 116401 : canonicalize_obj_off (op, off);
5573 116401 : gcc_assert (integer_zerop (off));
5574 116401 : return cxx_fold_indirect_ref_1 (ctx, loc, type, op, 0, empty_base);
5575 : }
5576 : }
5577 7578715 : else if (TREE_CODE (sub) == POINTER_PLUS_EXPR
5578 7578715 : && tree_fits_uhwi_p (TREE_OPERAND (sub, 1)))
5579 : {
5580 287761 : tree op00 = TREE_OPERAND (sub, 0);
5581 287761 : tree off = TREE_OPERAND (sub, 1);
5582 :
5583 287761 : STRIP_NOPS (op00);
5584 287761 : if (TREE_CODE (op00) == ADDR_EXPR)
5585 : {
5586 212915 : tree obj = TREE_OPERAND (op00, 0);
5587 212915 : canonicalize_obj_off (obj, off);
5588 212915 : return cxx_fold_indirect_ref_1 (ctx, loc, type, obj,
5589 : tree_to_uhwi (off), empty_base);
5590 : }
5591 : }
5592 : /* *(foo *)fooarrptr => (*fooarrptr)[0] */
5593 7290954 : else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
5594 7290954 : && similar_type_p (type, TREE_TYPE (TREE_TYPE (subtype))))
5595 : {
5596 0 : tree type_domain;
5597 0 : tree min_val = size_zero_node;
5598 0 : tree newsub
5599 0 : = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (subtype), sub, NULL);
5600 0 : if (newsub)
5601 : sub = newsub;
5602 : else
5603 0 : sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub);
5604 0 : type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
5605 0 : if (type_domain && TYPE_MIN_VALUE (type_domain))
5606 0 : min_val = TYPE_MIN_VALUE (type_domain);
5607 0 : return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
5608 0 : NULL_TREE);
5609 : }
5610 :
5611 : return NULL_TREE;
5612 : }
5613 :
5614 : static tree
5615 8680081 : cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t,
5616 : value_cat lval,
5617 : bool *non_constant_p, bool *overflow_p)
5618 : {
5619 8680081 : tree orig_op0 = TREE_OPERAND (t, 0);
5620 8680081 : bool empty_base = false;
5621 :
5622 : /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second
5623 : operand is an integer-zero. Otherwise reject the MEM_REF for now. */
5624 :
5625 8680081 : if (TREE_CODE (t) == MEM_REF
5626 8680081 : && (!TREE_OPERAND (t, 1) || !integer_zerop (TREE_OPERAND (t, 1))))
5627 : {
5628 0 : gcc_assert (ctx->quiet);
5629 0 : *non_constant_p = true;
5630 0 : return t;
5631 : }
5632 :
5633 : /* First try to simplify it directly. */
5634 8680081 : tree r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t),
5635 : orig_op0, &empty_base);
5636 8680081 : if (!r)
5637 : {
5638 : /* If that didn't work, evaluate the operand first. */
5639 8287187 : tree op0 = cxx_eval_constant_expression (ctx, orig_op0,
5640 : vc_prvalue, non_constant_p,
5641 : overflow_p);
5642 : /* Don't VERIFY_CONSTANT here. */
5643 8287187 : if (*non_constant_p)
5644 : return t;
5645 :
5646 4079729 : if (!lval && integer_zerop (op0))
5647 : {
5648 82 : if (!ctx->quiet)
5649 12 : error ("dereferencing a null pointer");
5650 82 : *non_constant_p = true;
5651 82 : return t;
5652 : }
5653 :
5654 4079647 : r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t), op0,
5655 : &empty_base);
5656 4079647 : if (r == NULL_TREE)
5657 : {
5658 : /* We couldn't fold to a constant value. Make sure it's not
5659 : something we should have been able to fold. */
5660 896157 : tree sub = op0;
5661 896157 : STRIP_NOPS (sub);
5662 896157 : if (TREE_CODE (sub) == ADDR_EXPR)
5663 : {
5664 15072 : gcc_assert (!similar_type_p
5665 : (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t)));
5666 : /* DR 1188 says we don't have to deal with this. */
5667 15072 : if (!ctx->quiet)
5668 3 : error_at (cp_expr_loc_or_input_loc (t),
5669 : "accessing value of %qE through a %qT glvalue in a "
5670 : "constant expression", build_fold_indirect_ref (sub),
5671 3 : TREE_TYPE (t));
5672 15072 : *non_constant_p = true;
5673 15072 : return t;
5674 : }
5675 :
5676 881085 : if (lval == vc_glvalue && op0 != orig_op0)
5677 99 : return build1 (INDIRECT_REF, TREE_TYPE (t), op0);
5678 880986 : if (!lval)
5679 880902 : VERIFY_CONSTANT (t);
5680 880986 : return t;
5681 : }
5682 : }
5683 :
5684 3576384 : r = cxx_eval_constant_expression (ctx, r,
5685 : lval, non_constant_p, overflow_p);
5686 3576384 : if (*non_constant_p)
5687 : return t;
5688 :
5689 : /* If we're pulling out the value of an empty base, just return an empty
5690 : CONSTRUCTOR. */
5691 2819737 : if (empty_base && !lval)
5692 : {
5693 10090 : r = build_constructor (TREE_TYPE (t), NULL);
5694 10090 : TREE_CONSTANT (r) = true;
5695 : }
5696 :
5697 : return r;
5698 : }
5699 :
5700 : /* Complain about R, a VAR_DECL, not being usable in a constant expression.
5701 : FUNDEF_P is true if we're checking a constexpr function body.
5702 : Shared between potential_constant_expression and
5703 : cxx_eval_constant_expression. */
5704 :
5705 : static void
5706 207 : non_const_var_error (location_t loc, tree r, bool fundef_p)
5707 : {
5708 207 : auto_diagnostic_group d;
5709 207 : tree type = TREE_TYPE (r);
5710 207 : if (DECL_NAME (r) == heap_uninit_identifier
5711 207 : || DECL_NAME (r) == heap_identifier
5712 206 : || DECL_NAME (r) == heap_vec_uninit_identifier
5713 413 : || DECL_NAME (r) == heap_vec_identifier)
5714 : {
5715 1 : if (constexpr_error (loc, fundef_p, "the content of uninitialized "
5716 : "storage is not usable in a constant expression"))
5717 1 : inform (DECL_SOURCE_LOCATION (r), "allocated here");
5718 1 : return;
5719 : }
5720 206 : if (DECL_NAME (r) == heap_deleted_identifier)
5721 : {
5722 2 : if (constexpr_error (loc, fundef_p, "use of allocated storage after "
5723 : "deallocation in a constant expression"))
5724 2 : inform (DECL_SOURCE_LOCATION (r), "allocated here");
5725 2 : return;
5726 : }
5727 204 : if (!constexpr_error (loc, fundef_p, "the value of %qD is not usable in "
5728 : "a constant expression", r))
5729 : return;
5730 : /* Avoid error cascade. */
5731 204 : if (DECL_INITIAL (r) == error_mark_node)
5732 : return;
5733 192 : if (DECL_DECLARED_CONSTEXPR_P (r))
5734 3 : inform (DECL_SOURCE_LOCATION (r),
5735 : "%qD used in its own initializer", r);
5736 189 : else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5737 : {
5738 142 : if (!CP_TYPE_CONST_P (type))
5739 109 : inform (DECL_SOURCE_LOCATION (r),
5740 : "%q#D is not const", r);
5741 33 : else if (CP_TYPE_VOLATILE_P (type))
5742 0 : inform (DECL_SOURCE_LOCATION (r),
5743 : "%q#D is volatile", r);
5744 33 : else if (!DECL_INITIAL (r)
5745 11 : || !TREE_CONSTANT (DECL_INITIAL (r))
5746 43 : || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r))
5747 33 : inform (DECL_SOURCE_LOCATION (r),
5748 : "%qD was not initialized with a constant "
5749 : "expression", r);
5750 : else
5751 0 : gcc_unreachable ();
5752 : }
5753 47 : else if (TYPE_REF_P (type))
5754 12 : inform (DECL_SOURCE_LOCATION (r),
5755 : "%qD was not initialized with a constant "
5756 : "expression", r);
5757 : else
5758 : {
5759 35 : if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r))
5760 35 : inform (DECL_SOURCE_LOCATION (r),
5761 : "%qD was not declared %<constexpr%>", r);
5762 : else
5763 0 : inform (DECL_SOURCE_LOCATION (r),
5764 : "%qD does not have integral or enumeration type",
5765 : r);
5766 : }
5767 207 : }
5768 :
5769 : /* Subroutine of cxx_eval_constant_expression.
5770 : Like cxx_eval_unary_expression, except for trinary expressions. */
5771 :
5772 : static tree
5773 18 : cxx_eval_trinary_expression (const constexpr_ctx *ctx, tree t,
5774 : value_cat lval,
5775 : bool *non_constant_p, bool *overflow_p)
5776 : {
5777 18 : int i;
5778 18 : tree args[3];
5779 18 : tree val;
5780 :
5781 45 : for (i = 0; i < 3; i++)
5782 : {
5783 36 : args[i] = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, i),
5784 : lval,
5785 : non_constant_p, overflow_p);
5786 36 : VERIFY_CONSTANT (args[i]);
5787 : }
5788 :
5789 9 : val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t),
5790 : args[0], args[1], args[2]);
5791 9 : if (val == NULL_TREE)
5792 : return t;
5793 9 : VERIFY_CONSTANT (val);
5794 : return val;
5795 : }
5796 :
5797 : /* True if T was declared in a function declared to be constexpr, and
5798 : therefore potentially constant in C++14. */
5799 :
5800 : bool
5801 51340941 : var_in_constexpr_fn (tree t)
5802 : {
5803 51340941 : tree ctx = DECL_CONTEXT (t);
5804 51340941 : return (ctx && TREE_CODE (ctx) == FUNCTION_DECL
5805 97281613 : && DECL_DECLARED_CONSTEXPR_P (ctx));
5806 : }
5807 :
5808 : /* True if a function might be constexpr: either a function that was
5809 : declared constexpr, or a C++17 lambda op(). */
5810 :
5811 : bool
5812 292215937 : maybe_constexpr_fn (tree t)
5813 : {
5814 292215937 : return (DECL_DECLARED_CONSTEXPR_P (t)
5815 182571597 : || (cxx_dialect >= cxx17 && LAMBDA_FUNCTION_P (t))
5816 471823268 : || (flag_implicit_constexpr
5817 24 : && DECL_DECLARED_INLINE_P (STRIP_TEMPLATE (t))));
5818 : }
5819 :
5820 : /* True if T was declared in a function that might be constexpr: either a
5821 : function that was declared constexpr, or a C++17 lambda op(). */
5822 :
5823 : bool
5824 156101591 : var_in_maybe_constexpr_fn (tree t)
5825 : {
5826 311913921 : return (DECL_FUNCTION_SCOPE_P (t)
5827 270597130 : && maybe_constexpr_fn (DECL_CONTEXT (t)));
5828 : }
5829 :
5830 : /* We're assigning INIT to TARGET. In do_build_copy_constructor and
5831 : build_over_call we implement trivial copy of a class with tail padding using
5832 : assignment of character arrays, which is valid in normal code, but not in
5833 : constexpr evaluation. We don't need to worry about clobbering tail padding
5834 : in constexpr evaluation, so strip the type punning. */
5835 :
5836 : static void
5837 12267539 : maybe_simplify_trivial_copy (tree &target, tree &init)
5838 : {
5839 12267539 : if (TREE_CODE (target) == MEM_REF
5840 690 : && TREE_CODE (init) == MEM_REF
5841 690 : && TREE_TYPE (target) == TREE_TYPE (init)
5842 690 : && TREE_CODE (TREE_TYPE (target)) == ARRAY_TYPE
5843 12268229 : && TREE_TYPE (TREE_TYPE (target)) == unsigned_char_type_node)
5844 : {
5845 690 : target = build_fold_indirect_ref (TREE_OPERAND (target, 0));
5846 690 : init = build_fold_indirect_ref (TREE_OPERAND (init, 0));
5847 : }
5848 12267539 : }
5849 :
5850 : /* Returns true if REF, which is a COMPONENT_REF, has any fields
5851 : of constant type. This does not check for 'mutable', so the
5852 : caller is expected to be mindful of that. */
5853 :
5854 : static bool
5855 282 : cref_has_const_field (tree ref)
5856 : {
5857 351 : while (TREE_CODE (ref) == COMPONENT_REF)
5858 : {
5859 342 : if (CP_TYPE_CONST_P (TREE_TYPE (TREE_OPERAND (ref, 1))))
5860 : return true;
5861 69 : ref = TREE_OPERAND (ref, 0);
5862 : }
5863 : return false;
5864 : }
5865 :
5866 : /* Return true if we are modifying something that is const during constant
5867 : expression evaluation. CODE is the code of the statement, OBJ is the
5868 : object in question, MUTABLE_P is true if one of the subobjects were
5869 : declared mutable. */
5870 :
5871 : static bool
5872 9658134 : modifying_const_object_p (tree_code code, tree obj, bool mutable_p)
5873 : {
5874 : /* If this is initialization, there's no problem. */
5875 9658134 : if (code != MODIFY_EXPR)
5876 : return false;
5877 :
5878 : /* [basic.type.qualifier] "A const object is an object of type
5879 : const T or a non-mutable subobject of a const object." */
5880 3652180 : if (mutable_p)
5881 : return false;
5882 :
5883 3651912 : if (TREE_READONLY (obj))
5884 : return true;
5885 :
5886 3651023 : if (CP_TYPE_CONST_P (TREE_TYPE (obj)))
5887 : {
5888 : /* Although a COMPONENT_REF may have a const type, we should
5889 : only consider it modifying a const object when any of the
5890 : field components is const. This can happen when using
5891 : constructs such as const_cast<const T &>(m), making something
5892 : const even though it wasn't declared const. */
5893 2114 : if (TREE_CODE (obj) == COMPONENT_REF)
5894 282 : return cref_has_const_field (obj);
5895 : else
5896 : return true;
5897 : }
5898 :
5899 : return false;
5900 : }
5901 :
5902 : /* Evaluate an INIT_EXPR or MODIFY_EXPR. */
5903 :
5904 : static tree
5905 12823584 : cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
5906 : value_cat lval,
5907 : bool *non_constant_p, bool *overflow_p)
5908 : {
5909 12823584 : constexpr_ctx new_ctx = *ctx;
5910 :
5911 12823584 : tree init = TREE_OPERAND (t, 1);
5912 12823584 : if (TREE_CLOBBER_P (init))
5913 : /* Just ignore clobbers. */
5914 556045 : return void_node;
5915 :
5916 : /* First we figure out where we're storing to. */
5917 12267539 : tree target = TREE_OPERAND (t, 0);
5918 :
5919 12267539 : maybe_simplify_trivial_copy (target, init);
5920 :
5921 12267539 : tree type = TREE_TYPE (target);
5922 12267539 : bool preeval = SCALAR_TYPE_P (type) || TREE_CODE (t) == MODIFY_EXPR;
5923 10731891 : if (preeval)
5924 : {
5925 : /* Evaluate the value to be stored without knowing what object it will be
5926 : stored in, so that any side-effects happen first. */
5927 10731891 : if (!SCALAR_TYPE_P (type))
5928 4349 : new_ctx.ctor = new_ctx.object = NULL_TREE;
5929 10731891 : init = cxx_eval_constant_expression (&new_ctx, init, vc_prvalue,
5930 : non_constant_p, overflow_p);
5931 10731891 : if (*non_constant_p)
5932 : return t;
5933 : }
5934 :
5935 9157456 : bool evaluated = false;
5936 9157456 : if (lval == vc_glvalue)
5937 : {
5938 : /* If we want to return a reference to the target, we need to evaluate it
5939 : as a whole; otherwise, only evaluate the innermost piece to avoid
5940 : building up unnecessary *_REFs. */
5941 0 : target = cxx_eval_constant_expression (ctx, target, lval,
5942 : non_constant_p, overflow_p);
5943 0 : evaluated = true;
5944 0 : if (*non_constant_p)
5945 : return t;
5946 : }
5947 :
5948 : /* Find the underlying variable. */
5949 9157456 : releasing_vec refs;
5950 9157456 : tree object = NULL_TREE;
5951 : /* If we're modifying a const object, save it. */
5952 9157456 : tree const_object_being_modified = NULL_TREE;
5953 9157456 : bool mutable_p = false;
5954 28999101 : for (tree probe = target; object == NULL_TREE; )
5955 : {
5956 19841705 : switch (TREE_CODE (probe))
5957 : {
5958 1526879 : case BIT_FIELD_REF:
5959 1526879 : case COMPONENT_REF:
5960 1526879 : case ARRAY_REF:
5961 1526879 : {
5962 1526879 : tree ob = TREE_OPERAND (probe, 0);
5963 1526879 : tree elt = TREE_OPERAND (probe, 1);
5964 1526879 : if (TREE_CODE (elt) == FIELD_DECL && DECL_MUTABLE_P (elt))
5965 : mutable_p = true;
5966 1526879 : if (TREE_CODE (probe) == ARRAY_REF)
5967 : {
5968 126645 : elt = eval_and_check_array_index (ctx, probe, false,
5969 : non_constant_p, overflow_p);
5970 126645 : if (*non_constant_p)
5971 54 : return t;
5972 : }
5973 : /* We don't check modifying_const_object_p for ARRAY_REFs. Given
5974 : "int a[10]", an ARRAY_REF "a[2]" can be "const int", even though
5975 : the array isn't const. Instead, check "a" in the next iteration;
5976 : that will detect modifying "const int a[10]". */
5977 1400234 : else if (evaluated
5978 500738 : && modifying_const_object_p (TREE_CODE (t), probe,
5979 : mutable_p)
5980 1400507 : && const_object_being_modified == NULL_TREE)
5981 : const_object_being_modified = probe;
5982 1526825 : vec_safe_push (refs, elt);
5983 1526825 : vec_safe_push (refs, TREE_TYPE (probe));
5984 1526825 : probe = ob;
5985 : }
5986 1526825 : break;
5987 :
5988 4 : case REALPART_EXPR:
5989 4 : gcc_assert (probe == target);
5990 4 : vec_safe_push (refs, probe);
5991 4 : vec_safe_push (refs, TREE_TYPE (probe));
5992 4 : probe = TREE_OPERAND (probe, 0);
5993 4 : break;
5994 :
5995 6 : case IMAGPART_EXPR:
5996 6 : gcc_assert (probe == target);
5997 6 : vec_safe_push (refs, probe);
5998 6 : vec_safe_push (refs, TREE_TYPE (probe));
5999 6 : probe = TREE_OPERAND (probe, 0);
6000 6 : break;
6001 :
6002 18314816 : default:
6003 18314816 : if (evaluated)
6004 : object = probe;
6005 : else
6006 : {
6007 9157420 : probe = cxx_eval_constant_expression (ctx, probe, vc_glvalue,
6008 : non_constant_p, overflow_p);
6009 9157420 : evaluated = true;
6010 9157420 : if (*non_constant_p)
6011 : return t;
6012 : }
6013 : break;
6014 : }
6015 : }
6016 :
6017 9157396 : if (modifying_const_object_p (TREE_CODE (t), object, mutable_p)
6018 9157396 : && const_object_being_modified == NULL_TREE)
6019 : const_object_being_modified = object;
6020 :
6021 : /* And then find/build up our initializer for the path to the subobject
6022 : we're initializing. */
6023 9157396 : tree *valp;
6024 9157396 : if (DECL_P (object))
6025 9157387 : valp = ctx->global->get_value_ptr (object);
6026 : else
6027 9 : valp = NULL;
6028 9157396 : if (!valp)
6029 : {
6030 : /* A constant-expression cannot modify objects from outside the
6031 : constant-expression. */
6032 5735 : if (!ctx->quiet)
6033 6 : error ("modification of %qE is not a constant expression", object);
6034 5735 : *non_constant_p = true;
6035 5735 : return t;
6036 : }
6037 9151661 : type = TREE_TYPE (object);
6038 9151661 : bool no_zero_init = true;
6039 :
6040 18309117 : auto_vec<tree *> ctors;
6041 18303322 : releasing_vec indexes;
6042 18303322 : auto_vec<int> index_pos_hints;
6043 9151661 : bool activated_union_member_p = false;
6044 9151661 : bool empty_base = false;
6045 10669844 : while (!refs->is_empty ())
6046 : {
6047 1519035 : if (*valp == NULL_TREE)
6048 : {
6049 154723 : *valp = build_constructor (type, NULL);
6050 154723 : CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
6051 : }
6052 1364312 : else if (STRIP_ANY_LOCATION_WRAPPER (*valp),
6053 1364312 : TREE_CODE (*valp) == STRING_CST)
6054 : {
6055 : /* An array was initialized with a string constant, and now
6056 : we're writing into one of its elements. Explode the
6057 : single initialization into a set of element
6058 : initializations. */
6059 224 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
6060 :
6061 224 : tree string = *valp;
6062 224 : tree elt_type = TREE_TYPE (type);
6063 224 : unsigned chars_per_elt = (TYPE_PRECISION (elt_type)
6064 224 : / TYPE_PRECISION (char_type_node));
6065 224 : unsigned num_elts = TREE_STRING_LENGTH (string) / chars_per_elt;
6066 224 : tree ary_ctor = build_constructor (type, NULL);
6067 :
6068 224 : vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor), num_elts);
6069 545 : for (unsigned ix = 0; ix != num_elts; ix++)
6070 : {
6071 321 : constructor_elt elt =
6072 : {
6073 321 : build_int_cst (size_type_node, ix),
6074 321 : extract_string_elt (string, chars_per_elt, ix)
6075 321 : };
6076 321 : CONSTRUCTOR_ELTS (ary_ctor)->quick_push (elt);
6077 : }
6078 :
6079 224 : *valp = ary_ctor;
6080 : }
6081 :
6082 1519035 : enum tree_code code = TREE_CODE (type);
6083 1519035 : tree reftype = refs->pop();
6084 1519035 : tree index = refs->pop();
6085 :
6086 1519035 : if (code == COMPLEX_TYPE)
6087 : {
6088 10 : if (TREE_CODE (*valp) == COMPLEX_CST)
6089 8 : *valp = build2 (COMPLEX_EXPR, type, TREE_REALPART (*valp),
6090 8 : TREE_IMAGPART (*valp));
6091 2 : else if (TREE_CODE (*valp) == CONSTRUCTOR
6092 1 : && CONSTRUCTOR_NELTS (*valp) == 0
6093 3 : && CONSTRUCTOR_NO_CLEARING (*valp))
6094 : {
6095 1 : tree r = build_constructor (reftype, NULL);
6096 1 : CONSTRUCTOR_NO_CLEARING (r) = 1;
6097 1 : *valp = build2 (COMPLEX_EXPR, type, r, r);
6098 : }
6099 10 : gcc_assert (TREE_CODE (*valp) == COMPLEX_EXPR);
6100 10 : ctors.safe_push (valp);
6101 10 : vec_safe_push (indexes, index);
6102 10 : valp = &TREE_OPERAND (*valp, TREE_CODE (index) == IMAGPART_EXPR);
6103 10 : gcc_checking_assert (refs->is_empty ());
6104 : type = reftype;
6105 852 : break;
6106 : }
6107 :
6108 : /* If the value of object is already zero-initialized, any new ctors for
6109 : subobjects will also be zero-initialized. */
6110 1519025 : no_zero_init = CONSTRUCTOR_NO_CLEARING (*valp);
6111 :
6112 1519025 : if (code == RECORD_TYPE && is_empty_field (index))
6113 : /* Don't build a sub-CONSTRUCTOR for an empty base or field, as they
6114 : have no data and might have an offset lower than previously declared
6115 : fields, which confuses the middle-end. The code below will notice
6116 : that we don't have a CONSTRUCTOR for our inner target and just
6117 : return init. */
6118 : {
6119 : empty_base = true;
6120 : break;
6121 : }
6122 :
6123 1518183 : type = reftype;
6124 :
6125 30366 : if (code == UNION_TYPE && CONSTRUCTOR_NELTS (*valp)
6126 1522144 : && CONSTRUCTOR_ELT (*valp, 0)->index != index)
6127 : {
6128 523 : if (cxx_dialect < cxx20)
6129 : {
6130 56 : if (!ctx->quiet)
6131 18 : error_at (cp_expr_loc_or_input_loc (t),
6132 : "change of the active member of a union "
6133 : "from %qD to %qD",
6134 18 : CONSTRUCTOR_ELT (*valp, 0)->index,
6135 : index);
6136 56 : *non_constant_p = true;
6137 : }
6138 467 : else if (TREE_CODE (t) == MODIFY_EXPR
6139 467 : && CONSTRUCTOR_NO_CLEARING (*valp))
6140 : {
6141 : /* Diagnose changing the active union member while the union
6142 : is in the process of being initialized. */
6143 9 : if (!ctx->quiet)
6144 3 : error_at (cp_expr_loc_or_input_loc (t),
6145 : "change of the active member of a union "
6146 : "from %qD to %qD during initialization",
6147 3 : CONSTRUCTOR_ELT (*valp, 0)->index,
6148 : index);
6149 9 : *non_constant_p = true;
6150 : }
6151 : no_zero_init = true;
6152 : }
6153 :
6154 1518183 : ctors.safe_push (valp);
6155 1518183 : vec_safe_push (indexes, index);
6156 :
6157 1518183 : constructor_elt *cep
6158 1518183 : = get_or_insert_ctor_field (*valp, index);
6159 1518183 : index_pos_hints.safe_push (cep - CONSTRUCTOR_ELTS (*valp)->begin());
6160 :
6161 1518183 : if (code == UNION_TYPE)
6162 30366 : activated_union_member_p = true;
6163 :
6164 1518183 : valp = &cep->value;
6165 : }
6166 :
6167 : /* For initialization of an empty base, the original target will be
6168 : *(base*)this, evaluation of which resolves to the object
6169 : argument, which has the derived type rather than the base type. */
6170 18302480 : if (!empty_base && !(same_type_ignoring_top_level_qualifiers_p
6171 9150819 : (initialized_type (init), type)))
6172 : {
6173 536 : gcc_assert (is_empty_class (TREE_TYPE (target)));
6174 : empty_base = true;
6175 : }
6176 :
6177 : /* Detect modifying a constant object in constexpr evaluation.
6178 : We have found a const object that is being modified. Figure out
6179 : if we need to issue an error. Consider
6180 :
6181 : struct A {
6182 : int n;
6183 : constexpr A() : n(1) { n = 2; } // #1
6184 : };
6185 : struct B {
6186 : const A a;
6187 : constexpr B() { a.n = 3; } // #2
6188 : };
6189 : constexpr B b{};
6190 :
6191 : #1 is OK, since we're modifying an object under construction, but
6192 : #2 is wrong, since "a" is const and has been fully constructed.
6193 : To track it, we use the TREE_READONLY bit in the object's CONSTRUCTOR
6194 : which means that the object is read-only. For the example above, the
6195 : *ctors stack at the point of #2 will look like:
6196 :
6197 : ctors[0] = {.a={.n=2}} TREE_READONLY = 0
6198 : ctors[1] = {.n=2} TREE_READONLY = 1
6199 :
6200 : and we're modifying "b.a", so we search the stack and see if the
6201 : constructor for "b.a" has already run. */
6202 9151661 : if (const_object_being_modified)
6203 : {
6204 2721 : bool fail = false;
6205 2721 : tree const_objtype
6206 2721 : = strip_array_types (TREE_TYPE (const_object_being_modified));
6207 2721 : if (!CLASS_TYPE_P (const_objtype))
6208 : fail = true;
6209 : else
6210 : {
6211 : /* [class.ctor]p5 "A constructor can be invoked for a const,
6212 : volatile, or const volatile object. const and volatile
6213 : semantics are not applied on an object under construction.
6214 : They come into effect when the constructor for the most
6215 : derived object ends." */
6216 8022 : for (tree *elt : ctors)
6217 2790 : if (same_type_ignoring_top_level_qualifiers_p
6218 2790 : (TREE_TYPE (const_object_being_modified), TREE_TYPE (*elt)))
6219 : {
6220 2616 : fail = TREE_READONLY (*elt);
6221 2616 : break;
6222 : }
6223 : }
6224 2616 : if (fail)
6225 : {
6226 198 : if (!ctx->quiet)
6227 63 : modifying_const_object_error (t, const_object_being_modified);
6228 198 : *non_constant_p = true;
6229 198 : return t;
6230 : }
6231 : }
6232 :
6233 9151463 : if (!preeval)
6234 : {
6235 : /* We're handling an INIT_EXPR of class type, so the value of the
6236 : initializer can depend on the object it's initializing. */
6237 :
6238 : /* Create a new CONSTRUCTOR in case evaluation of the initializer
6239 : wants to modify it. */
6240 1533804 : if (*valp == NULL_TREE)
6241 : {
6242 1519529 : *valp = build_constructor (type, NULL);
6243 1519529 : CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
6244 : }
6245 1533804 : new_ctx.ctor = empty_base ? NULL_TREE : *valp;
6246 1533804 : new_ctx.object = target;
6247 : /* Avoid temporary materialization when initializing from a TARGET_EXPR.
6248 : We don't need to mess with AGGR_EXPR_SLOT/VEC_INIT_EXPR_SLOT because
6249 : expansion of those trees uses ctx instead. */
6250 1533804 : if (TREE_CODE (init) == TARGET_EXPR)
6251 590001 : if (tree tinit = TARGET_EXPR_INITIAL (init))
6252 590001 : init = tinit;
6253 1533804 : init = cxx_eval_constant_expression (&new_ctx, init, vc_prvalue,
6254 : non_constant_p, overflow_p);
6255 : /* The hash table might have moved since the get earlier, and the
6256 : initializer might have mutated the underlying CONSTRUCTORs, so we must
6257 : recompute VALP. */
6258 1533804 : valp = ctx->global->get_value_ptr (object);
6259 1718853 : for (unsigned i = 0; i < vec_safe_length (indexes); i++)
6260 : {
6261 185049 : ctors[i] = valp;
6262 185049 : constructor_elt *cep
6263 185049 : = get_or_insert_ctor_field (*valp, indexes[i], index_pos_hints[i]);
6264 185049 : valp = &cep->value;
6265 : }
6266 : }
6267 :
6268 9151463 : if (*non_constant_p)
6269 : return t;
6270 :
6271 : /* Don't share a CONSTRUCTOR that might be changed later. */
6272 9102527 : init = unshare_constructor (init);
6273 :
6274 9102527 : gcc_checking_assert (!*valp || (same_type_ignoring_top_level_qualifiers_p
6275 : (TREE_TYPE (*valp), type)));
6276 9102527 : if (empty_base)
6277 : {
6278 : /* Just evaluate the initializer and return, since there's no actual data
6279 : to store, and we didn't build a CONSTRUCTOR. */
6280 1265 : if (!*valp)
6281 : {
6282 : /* But do make sure we have something in *valp. */
6283 0 : *valp = build_constructor (type, nullptr);
6284 0 : CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
6285 : }
6286 : }
6287 9101262 : else if (*valp && TREE_CODE (*valp) == CONSTRUCTOR
6288 1487659 : && TREE_CODE (init) == CONSTRUCTOR)
6289 : {
6290 : /* An outer ctx->ctor might be pointing to *valp, so replace
6291 : its contents. */
6292 710494 : CONSTRUCTOR_ELTS (*valp) = CONSTRUCTOR_ELTS (init);
6293 710494 : TREE_CONSTANT (*valp) = TREE_CONSTANT (init);
6294 710494 : TREE_SIDE_EFFECTS (*valp) = TREE_SIDE_EFFECTS (init);
6295 2131482 : CONSTRUCTOR_NO_CLEARING (*valp)
6296 710494 : = CONSTRUCTOR_NO_CLEARING (init);
6297 : }
6298 : else
6299 8390768 : *valp = init;
6300 :
6301 : /* After initialization, 'const' semantics apply to the value of the
6302 : object. Make a note of this fact by marking the CONSTRUCTOR
6303 : TREE_READONLY. */
6304 9102527 : if (TREE_CODE (t) == INIT_EXPR
6305 5511856 : && TREE_CODE (*valp) == CONSTRUCTOR
6306 9810300 : && TYPE_READONLY (type))
6307 : {
6308 16977 : if (INDIRECT_REF_P (target)
6309 32029 : && (is_this_parameter
6310 15052 : (tree_strip_nop_conversions (TREE_OPERAND (target, 0)))))
6311 : /* We've just initialized '*this' (perhaps via the target
6312 : constructor of a delegating constructor). Leave it up to the
6313 : caller that set 'this' to set TREE_READONLY appropriately. */
6314 24 : gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
6315 : (TREE_TYPE (target), type) || empty_base);
6316 : else
6317 16953 : TREE_READONLY (*valp) = true;
6318 : }
6319 :
6320 : /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing
6321 : CONSTRUCTORs, if any. */
6322 9102527 : bool c = TREE_CONSTANT (init);
6323 9102527 : bool s = TREE_SIDE_EFFECTS (init);
6324 9102527 : if (!indexes->is_empty ())
6325 : {
6326 936076 : tree last = indexes->last ();
6327 936076 : if (TREE_CODE (last) == REALPART_EXPR
6328 936076 : || TREE_CODE (last) == IMAGPART_EXPR)
6329 : {
6330 : /* And canonicalize COMPLEX_EXPR into COMPLEX_CST if
6331 : possible. */
6332 10 : tree *cexpr = ctors.last ();
6333 10 : if (tree c = const_binop (COMPLEX_EXPR, TREE_TYPE (*cexpr),
6334 10 : TREE_OPERAND (*cexpr, 0),
6335 10 : TREE_OPERAND (*cexpr, 1)))
6336 9 : *cexpr = c;
6337 : else
6338 : {
6339 2 : TREE_CONSTANT (*cexpr)
6340 1 : = (TREE_CONSTANT (TREE_OPERAND (*cexpr, 0))
6341 1 : & TREE_CONSTANT (TREE_OPERAND (*cexpr, 1)));
6342 2 : TREE_SIDE_EFFECTS (*cexpr)
6343 2 : = (TREE_SIDE_EFFECTS (TREE_OPERAND (*cexpr, 0))
6344 1 : | TREE_SIDE_EFFECTS (TREE_OPERAND (*cexpr, 1)));
6345 : }
6346 10 : c = TREE_CONSTANT (*cexpr);
6347 10 : s = TREE_SIDE_EFFECTS (*cexpr);
6348 : }
6349 : }
6350 9102527 : if (!c || s || activated_union_member_p)
6351 1419208 : for (tree *elt : ctors)
6352 : {
6353 168778 : if (TREE_CODE (*elt) != CONSTRUCTOR)
6354 0 : continue;
6355 168778 : if (!c)
6356 30502 : TREE_CONSTANT (*elt) = false;
6357 168778 : if (s)
6358 0 : TREE_SIDE_EFFECTS (*elt) = true;
6359 : /* Clear CONSTRUCTOR_NO_CLEARING since we've activated a member of
6360 : this union. */
6361 168778 : if (TREE_CODE (TREE_TYPE (*elt)) == UNION_TYPE)
6362 30276 : CONSTRUCTOR_NO_CLEARING (*elt) = false;
6363 : }
6364 :
6365 9102527 : if (lval)
6366 : return target;
6367 : else
6368 2268860 : return init;
6369 12823584 : }
6370 :
6371 : /* Evaluate a ++ or -- expression. */
6372 :
6373 : static tree
6374 1203951 : cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t,
6375 : value_cat lval,
6376 : bool *non_constant_p, bool *overflow_p)
6377 : {
6378 1203951 : enum tree_code code = TREE_CODE (t);
6379 1203951 : tree type = TREE_TYPE (t);
6380 1203951 : tree op = TREE_OPERAND (t, 0);
6381 1203951 : tree offset = TREE_OPERAND (t, 1);
6382 1203951 : gcc_assert (TREE_CONSTANT (offset));
6383 :
6384 : /* OFFSET is constant, but perhaps not constant enough. We need to
6385 : e.g. bash FLOAT_EXPRs to REAL_CSTs. */
6386 1203951 : offset = fold_simple (offset);
6387 :
6388 : /* The operand as an lvalue. */
6389 1203951 : op = cxx_eval_constant_expression (ctx, op, vc_glvalue,
6390 : non_constant_p, overflow_p);
6391 :
6392 : /* The operand as an rvalue. */
6393 1203951 : tree val
6394 1203951 : = cxx_eval_constant_expression (ctx, op, vc_prvalue,
6395 : non_constant_p, overflow_p);
6396 : /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
6397 : a local array in a constexpr function. */
6398 1203951 : bool ptr = INDIRECT_TYPE_P (TREE_TYPE (val));
6399 844217 : if (!ptr)
6400 844217 : VERIFY_CONSTANT (val);
6401 :
6402 : /* The modified value. */
6403 854632 : bool inc = (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR);
6404 854632 : tree mod;
6405 854632 : if (INDIRECT_TYPE_P (type))
6406 : {
6407 : /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
6408 359734 : offset = convert_to_ptrofftype (offset);
6409 359734 : if (!inc)
6410 36424 : offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset);
6411 359734 : mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset);
6412 : }
6413 494898 : else if (c_promoting_integer_type_p (type)
6414 11 : && !TYPE_UNSIGNED (type)
6415 494905 : && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6416 : {
6417 7 : offset = fold_convert (integer_type_node, offset);
6418 7 : mod = fold_convert (integer_type_node, val);
6419 9 : tree t = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, integer_type_node,
6420 : mod, offset);
6421 7 : mod = fold_convert (type, t);
6422 7 : if (TREE_OVERFLOW_P (mod) && !TREE_OVERFLOW_P (t))
6423 3 : TREE_OVERFLOW (mod) = false;
6424 : }
6425 : else
6426 842468 : mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset);
6427 854632 : if (!ptr)
6428 494898 : VERIFY_CONSTANT (mod);
6429 :
6430 : /* Storing the modified value. */
6431 1530145 : tree store = build2_loc (cp_expr_loc_or_loc (t, input_location),
6432 : MODIFY_EXPR, type, op, mod);
6433 854632 : mod = cxx_eval_constant_expression (ctx, store, lval,
6434 : non_constant_p, overflow_p);
6435 854632 : ggc_free (store);
6436 854632 : if (*non_constant_p)
6437 : return t;
6438 :
6439 : /* And the value of the expression. */
6440 850476 : if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
6441 : /* Prefix ops are lvalues, but the caller might want an rvalue;
6442 : lval has already been taken into account in the store above. */
6443 : return mod;
6444 : else
6445 : /* Postfix ops are rvalues. */
6446 406952 : return val;
6447 : }
6448 :
6449 : /* Predicates for the meaning of *jump_target. */
6450 :
6451 : static bool
6452 13255316 : returns (tree *jump_target)
6453 : {
6454 13255316 : return *jump_target
6455 1595916 : && TREE_CODE (*jump_target) == RETURN_EXPR;
6456 : }
6457 :
6458 : static bool
6459 15399569 : breaks (tree *jump_target)
6460 : {
6461 15399569 : return *jump_target
6462 15399569 : && ((TREE_CODE (*jump_target) == LABEL_DECL
6463 11 : && LABEL_DECL_BREAK (*jump_target))
6464 68438 : || TREE_CODE (*jump_target) == BREAK_STMT
6465 66647 : || TREE_CODE (*jump_target) == EXIT_EXPR);
6466 : }
6467 :
6468 : static bool
6469 23293019 : continues (tree *jump_target)
6470 : {
6471 23293019 : return *jump_target
6472 23293019 : && ((TREE_CODE (*jump_target) == LABEL_DECL
6473 11 : && LABEL_DECL_CONTINUE (*jump_target))
6474 67397 : || TREE_CODE (*jump_target) == CONTINUE_STMT);
6475 :
6476 : }
6477 :
6478 : static bool
6479 5229605 : switches (tree *jump_target)
6480 : {
6481 5229605 : return *jump_target
6482 5229576 : && TREE_CODE (*jump_target) == INTEGER_CST;
6483 : }
6484 :
6485 : /* Subroutine of cxx_eval_statement_list. Determine whether the statement
6486 : STMT matches *jump_target. If we're looking for a case label and we see
6487 : the default label, note it in ctx->css_state. */
6488 :
6489 : static bool
6490 14852 : label_matches (const constexpr_ctx *ctx, tree *jump_target, tree stmt)
6491 : {
6492 14852 : switch (TREE_CODE (*jump_target))
6493 : {
6494 0 : case LABEL_DECL:
6495 0 : if (TREE_CODE (stmt) == LABEL_EXPR
6496 0 : && LABEL_EXPR_LABEL (stmt) == *jump_target)
6497 : return true;
6498 : break;
6499 :
6500 13754 : case INTEGER_CST:
6501 13754 : if (TREE_CODE (stmt) == CASE_LABEL_EXPR)
6502 : {
6503 13754 : gcc_assert (ctx->css_state != NULL);
6504 13754 : if (!CASE_LOW (stmt))
6505 : {
6506 : /* default: should appear just once in a SWITCH_EXPR
6507 : body (excluding nested SWITCH_EXPR). */
6508 1982 : gcc_assert (*ctx->css_state != css_default_seen);
6509 : /* When evaluating SWITCH_EXPR body for the second time,
6510 : return true for the default: label. */
6511 1982 : if (*ctx->css_state == css_default_processing)
6512 : return true;
6513 1015 : *ctx->css_state = css_default_seen;
6514 : }
6515 11772 : else if (CASE_HIGH (stmt))
6516 : {
6517 40 : if (tree_int_cst_le (CASE_LOW (stmt), *jump_target)
6518 62 : && tree_int_cst_le (*jump_target, CASE_HIGH (stmt)))
6519 : return true;
6520 : }
6521 11732 : else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt)))
6522 : return true;
6523 : }
6524 : break;
6525 :
6526 : case BREAK_STMT:
6527 : case CONTINUE_STMT:
6528 : /* These two are handled directly in cxx_eval_loop_expr by testing
6529 : breaks (jump_target) or continues (jump_target). */
6530 : break;
6531 :
6532 0 : default:
6533 0 : gcc_unreachable ();
6534 : }
6535 : return false;
6536 : }
6537 :
6538 : /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
6539 : semantics, for switch, break, continue, and return. */
6540 :
6541 : static tree
6542 7579568 : cxx_eval_statement_list (const constexpr_ctx *ctx, tree t,
6543 : bool *non_constant_p, bool *overflow_p,
6544 : tree *jump_target)
6545 : {
6546 7579568 : tree local_target;
6547 : /* In a statement-expression we want to return the last value.
6548 : For empty statement expression return void_node. */
6549 7579568 : tree r = void_node;
6550 7579568 : if (!jump_target)
6551 : {
6552 308 : local_target = NULL_TREE;
6553 308 : jump_target = &local_target;
6554 : }
6555 14335640 : for (tree_stmt_iterator i = tsi_start (t); !tsi_end_p (i); ++i)
6556 : {
6557 10816677 : tree stmt = *i;
6558 :
6559 : /* We've found a continue, so skip everything until we reach
6560 : the label its jumping to. */
6561 10816677 : if (continues (jump_target))
6562 : {
6563 1098 : if (label_matches (ctx, jump_target, stmt))
6564 : /* Found it. */
6565 0 : *jump_target = NULL_TREE;
6566 : else
6567 1098 : continue;
6568 : }
6569 10815579 : if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT)
6570 3834978 : continue;
6571 :
6572 6980601 : value_cat lval = vc_discard;
6573 : /* The result of a statement-expression is not wrapped in EXPR_STMT. */
6574 6980601 : if (tsi_one_before_end_p (i) && TREE_CODE (stmt) != EXPR_STMT)
6575 : lval = vc_prvalue;
6576 :
6577 6980601 : r = cxx_eval_constant_expression (ctx, stmt, lval,
6578 : non_constant_p, overflow_p,
6579 : jump_target);
6580 6980601 : if (*non_constant_p)
6581 : break;
6582 4151775 : if (returns (jump_target) || breaks (jump_target))
6583 : break;
6584 : }
6585 7579568 : if (*jump_target && jump_target == &local_target)
6586 : {
6587 : /* We aren't communicating the jump to our caller, so give up. We don't
6588 : need to support evaluation of jumps out of statement-exprs. */
6589 27 : if (!ctx->quiet)
6590 3 : error_at (cp_expr_loc_or_input_loc (r),
6591 : "statement is not a constant expression");
6592 27 : *non_constant_p = true;
6593 : }
6594 7579568 : return r;
6595 : }
6596 :
6597 : /* Evaluate a LOOP_EXPR for side-effects. Handles break and return
6598 : semantics; continue semantics are covered by cxx_eval_statement_list. */
6599 :
6600 : static tree
6601 123953 : cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t,
6602 : bool *non_constant_p, bool *overflow_p,
6603 : tree *jump_target)
6604 : {
6605 123953 : constexpr_ctx new_ctx = *ctx;
6606 123953 : tree local_target;
6607 123953 : if (!jump_target)
6608 : {
6609 14 : local_target = NULL_TREE;
6610 14 : jump_target = &local_target;
6611 : }
6612 :
6613 123953 : tree body, cond = NULL_TREE, expr = NULL_TREE;
6614 123953 : int count = 0;
6615 123953 : switch (TREE_CODE (t))
6616 : {
6617 14 : case LOOP_EXPR:
6618 14 : body = LOOP_EXPR_BODY (t);
6619 14 : break;
6620 106207 : case DO_STMT:
6621 106207 : body = DO_BODY (t);
6622 106207 : cond = DO_COND (t);
6623 106207 : break;
6624 4845 : case WHILE_STMT:
6625 4845 : body = WHILE_BODY (t);
6626 4845 : cond = WHILE_COND (t);
6627 4845 : count = -1;
6628 4845 : break;
6629 12887 : case FOR_STMT:
6630 12887 : if (FOR_INIT_STMT (t))
6631 0 : cxx_eval_constant_expression (ctx, FOR_INIT_STMT (t), vc_discard,
6632 : non_constant_p, overflow_p, jump_target);
6633 12887 : if (*non_constant_p)
6634 : return NULL_TREE;
6635 12887 : body = FOR_BODY (t);
6636 12887 : cond = FOR_COND (t);
6637 12887 : expr = FOR_EXPR (t);
6638 12887 : count = -1;
6639 12887 : break;
6640 0 : default:
6641 0 : gcc_unreachable ();
6642 : }
6643 123953 : auto_vec<tree, 10> save_exprs;
6644 123953 : new_ctx.save_exprs = &save_exprs;
6645 5350136 : do
6646 : {
6647 5350136 : if (count != -1)
6648 : {
6649 5332404 : if (body)
6650 5332404 : cxx_eval_constant_expression (&new_ctx, body, vc_discard,
6651 : non_constant_p, overflow_p,
6652 : jump_target);
6653 5332404 : if (breaks (jump_target))
6654 : {
6655 283 : *jump_target = NULL_TREE;
6656 283 : break;
6657 : }
6658 :
6659 5332121 : if (TREE_CODE (t) != LOOP_EXPR && continues (jump_target))
6660 912 : *jump_target = NULL_TREE;
6661 :
6662 5332121 : if (expr)
6663 195294 : cxx_eval_constant_expression (&new_ctx, expr, vc_prvalue,
6664 : non_constant_p, overflow_p,
6665 : jump_target);
6666 : }
6667 :
6668 5349853 : if (cond)
6669 : {
6670 5349347 : tree res
6671 5349347 : = cxx_eval_constant_expression (&new_ctx, cond, vc_prvalue,
6672 : non_constant_p, overflow_p,
6673 : jump_target);
6674 5349347 : if (res)
6675 : {
6676 5345277 : if (verify_constant (res, ctx->quiet, non_constant_p,
6677 : overflow_p))
6678 : break;
6679 5336907 : if (integer_zerop (res))
6680 : break;
6681 : }
6682 : else
6683 4070 : gcc_assert (*jump_target);
6684 : }
6685 :
6686 : /* Forget saved values of SAVE_EXPRs and TARGET_EXPRs. */
6687 15748116 : for (tree save_expr : save_exprs)
6688 57189 : ctx->global->remove_value (save_expr);
6689 5230309 : save_exprs.truncate (0);
6690 :
6691 5230309 : if (++count >= constexpr_loop_limit)
6692 : {
6693 28 : if (!ctx->quiet)
6694 9 : error_at (cp_expr_loc_or_input_loc (t),
6695 : "%<constexpr%> loop iteration count exceeds limit of %d "
6696 : "(use %<-fconstexpr-loop-limit=%> to increase the limit)",
6697 : constexpr_loop_limit);
6698 28 : *non_constant_p = true;
6699 28 : break;
6700 : }
6701 : }
6702 10456570 : while (!returns (jump_target)
6703 5226289 : && !breaks (jump_target)
6704 5226289 : && !continues (jump_target)
6705 186 : && (!switches (jump_target) || count == 0)
6706 10460608 : && !*non_constant_p);
6707 :
6708 : /* Forget saved values of SAVE_EXPRs and TARGET_EXPRs. */
6709 380575 : for (tree save_expr : save_exprs)
6710 8716 : ctx->global->remove_value (save_expr);
6711 :
6712 123953 : return NULL_TREE;
6713 : }
6714 :
6715 : /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
6716 : semantics. */
6717 :
6718 : static tree
6719 2057 : cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t,
6720 : bool *non_constant_p, bool *overflow_p,
6721 : tree *jump_target)
6722 : {
6723 2057 : tree cond
6724 2057 : = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_COND (t) : SWITCH_COND (t);
6725 2057 : cond = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
6726 : non_constant_p, overflow_p);
6727 2057 : VERIFY_CONSTANT (cond);
6728 1922 : *jump_target = cond;
6729 :
6730 1922 : tree body
6731 1922 : = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_BODY (t) : SWITCH_BODY (t);
6732 1922 : constexpr_ctx new_ctx = *ctx;
6733 1922 : constexpr_switch_state css = css_default_not_seen;
6734 1922 : new_ctx.css_state = &css;
6735 1922 : cxx_eval_constant_expression (&new_ctx, body, vc_discard,
6736 : non_constant_p, overflow_p, jump_target);
6737 2919 : if (switches (jump_target) && css == css_default_seen)
6738 : {
6739 : /* If the SWITCH_EXPR body has default: label, process it once again,
6740 : this time instructing label_matches to return true for default:
6741 : label on switches (jump_target). */
6742 967 : css = css_default_processing;
6743 967 : cxx_eval_constant_expression (&new_ctx, body, vc_discard,
6744 : non_constant_p, overflow_p, jump_target);
6745 : }
6746 1922 : if (breaks (jump_target) || switches (jump_target))
6747 558 : *jump_target = NULL_TREE;
6748 : return NULL_TREE;
6749 : }
6750 :
6751 : /* Find the object of TYPE under initialization in CTX. */
6752 :
6753 : static tree
6754 16519 : lookup_placeholder (const constexpr_ctx *ctx, value_cat lval, tree type)
6755 : {
6756 16519 : if (!ctx)
6757 : return NULL_TREE;
6758 :
6759 : /* Prefer the outermost matching object, but don't cross
6760 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY constructors. */
6761 16199 : if (ctx->ctor && !CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor))
6762 493 : if (tree outer_ob = lookup_placeholder (ctx->parent, lval, type))
6763 : return outer_ob;
6764 :
6765 : /* We could use ctx->object unconditionally, but using ctx->ctor when we
6766 : can is a minor optimization. */
6767 16027 : if (!lval && ctx->ctor && same_type_p (TREE_TYPE (ctx->ctor), type))
6768 292 : return ctx->ctor;
6769 :
6770 15735 : if (!ctx->object)
6771 : return NULL_TREE;
6772 :
6773 : /* Since an object cannot have a field of its own type, we can search outward
6774 : from ctx->object to find the unique containing object of TYPE. */
6775 : tree ob = ctx->object;
6776 15764 : while (ob)
6777 : {
6778 15764 : if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob), type))
6779 : break;
6780 222 : if (handled_component_p (ob))
6781 220 : ob = TREE_OPERAND (ob, 0);
6782 : else
6783 : ob = NULL_TREE;
6784 : }
6785 :
6786 : return ob;
6787 : }
6788 :
6789 : /* Complain about an attempt to evaluate inline assembly. If FUNDEF_P is
6790 : true, we're checking a constexpr function body. */
6791 :
6792 : static void
6793 11 : inline_asm_in_constexpr_error (location_t loc, bool fundef_p)
6794 : {
6795 11 : auto_diagnostic_group d;
6796 11 : if (constexpr_error (loc, fundef_p, "inline assembly is not a "
6797 : "constant expression"))
6798 11 : inform (loc, "only unevaluated inline assembly is allowed in a "
6799 : "%<constexpr%> function in C++20");
6800 11 : }
6801 :
6802 : /* We're getting the constant value of DECL in a manifestly constant-evaluated
6803 : context; maybe complain about that. */
6804 :
6805 : static void
6806 29301956 : maybe_warn_about_constant_value (location_t loc, tree decl)
6807 : {
6808 29301956 : static bool explained = false;
6809 29301956 : if (cxx_dialect >= cxx17
6810 29022773 : && warn_interference_size
6811 29022773 : && !OPTION_SET_P (param_destruct_interfere_size)
6812 29022773 : && DECL_CONTEXT (decl) == std_node
6813 720099 : && id_equal (DECL_NAME (decl), "hardware_destructive_interference_size")
6814 29301960 : && (LOCATION_FILE (input_location) != main_input_filename
6815 3 : || module_exporting_p ())
6816 5 : && warning_at (loc, OPT_Winterference_size, "use of %qD", decl)
6817 29301961 : && !explained)
6818 : {
6819 3 : explained = true;
6820 3 : inform (loc, "its value can vary between compiler versions or "
6821 : "with different %<-mtune%> or %<-mcpu%> flags");
6822 3 : inform (loc, "if this use is part of a public ABI, change it to "
6823 : "instead use a constant variable you define");
6824 3 : inform (loc, "the default value for the current CPU tuning "
6825 : "is %d bytes", param_destruct_interfere_size);
6826 3 : inform (loc, "you can stabilize this value with %<--param "
6827 : "hardware_destructive_interference_size=%d%>, or disable "
6828 : "this warning with %<-Wno-interference-size%>",
6829 : param_destruct_interfere_size);
6830 : }
6831 29301956 : }
6832 :
6833 : /* For element type ELT_TYPE, return the appropriate type of the heap object
6834 : containing such element(s). COOKIE_SIZE is NULL or the size of cookie
6835 : in bytes. If COOKIE_SIZE is NULL, return array type
6836 : ELT_TYPE[FULL_SIZE / sizeof(ELT_TYPE)], otherwise return
6837 : struct { size_t[COOKIE_SIZE/sizeof(size_t)]; ELT_TYPE[N]; }
6838 : where N is is computed such that the size of the struct fits into FULL_SIZE.
6839 : If ARG_SIZE is non-NULL, it is the first argument to the new operator.
6840 : It should be passed if ELT_TYPE is zero sized type in which case FULL_SIZE
6841 : will be also 0 and so it is not possible to determine the actual array
6842 : size. CTX, NON_CONSTANT_P and OVERFLOW_P are used during constant
6843 : expression evaluation of subexpressions of ARG_SIZE. */
6844 :
6845 : static tree
6846 612 : build_new_constexpr_heap_type (const constexpr_ctx *ctx, tree elt_type,
6847 : tree cookie_size, tree full_size, tree arg_size,
6848 : bool *non_constant_p, bool *overflow_p)
6849 : {
6850 612 : gcc_assert (cookie_size == NULL_TREE || tree_fits_uhwi_p (cookie_size));
6851 612 : gcc_assert (tree_fits_uhwi_p (full_size));
6852 612 : unsigned HOST_WIDE_INT csz = cookie_size ? tree_to_uhwi (cookie_size) : 0;
6853 612 : if (arg_size)
6854 : {
6855 3 : STRIP_NOPS (arg_size);
6856 3 : if (cookie_size)
6857 : {
6858 0 : if (TREE_CODE (arg_size) != PLUS_EXPR)
6859 : arg_size = NULL_TREE;
6860 0 : else if (TREE_CODE (TREE_OPERAND (arg_size, 0)) == INTEGER_CST
6861 0 : && tree_int_cst_equal (cookie_size,
6862 0 : TREE_OPERAND (arg_size, 0)))
6863 : {
6864 0 : arg_size = TREE_OPERAND (arg_size, 1);
6865 0 : STRIP_NOPS (arg_size);
6866 : }
6867 0 : else if (TREE_CODE (TREE_OPERAND (arg_size, 1)) == INTEGER_CST
6868 0 : && tree_int_cst_equal (cookie_size,
6869 0 : TREE_OPERAND (arg_size, 1)))
6870 : {
6871 0 : arg_size = TREE_OPERAND (arg_size, 0);
6872 0 : STRIP_NOPS (arg_size);
6873 : }
6874 : else
6875 : arg_size = NULL_TREE;
6876 : }
6877 3 : if (arg_size && TREE_CODE (arg_size) == MULT_EXPR)
6878 : {
6879 3 : tree op0 = TREE_OPERAND (arg_size, 0);
6880 3 : tree op1 = TREE_OPERAND (arg_size, 1);
6881 3 : if (integer_zerop (op0))
6882 3 : arg_size
6883 3 : = cxx_eval_constant_expression (ctx, op1, vc_prvalue,
6884 : non_constant_p, overflow_p);
6885 0 : else if (integer_zerop (op1))
6886 0 : arg_size
6887 0 : = cxx_eval_constant_expression (ctx, op0, vc_prvalue,
6888 : non_constant_p, overflow_p);
6889 : else
6890 : arg_size = NULL_TREE;
6891 : }
6892 : else
6893 : arg_size = NULL_TREE;
6894 : }
6895 :
6896 3 : unsigned HOST_WIDE_INT fsz = tree_to_uhwi (arg_size ? arg_size : full_size);
6897 612 : if (!arg_size)
6898 : {
6899 609 : unsigned HOST_WIDE_INT esz = int_size_in_bytes (elt_type);
6900 609 : gcc_assert (fsz >= csz);
6901 609 : fsz -= csz;
6902 609 : if (esz)
6903 609 : fsz /= esz;
6904 : }
6905 612 : tree itype2 = build_index_type (size_int (fsz - 1));
6906 612 : if (!cookie_size)
6907 611 : return build_cplus_array_type (elt_type, itype2);
6908 1 : return build_new_constexpr_heap_type (elt_type, cookie_size, itype2);
6909 : }
6910 :
6911 : /* Attempt to reduce the expression T to a constant value.
6912 : On failure, issue diagnostic and return error_mark_node. */
6913 : /* FIXME unify with c_fully_fold */
6914 : /* FIXME overflow_p is too global */
6915 :
6916 : static tree
6917 555401349 : cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
6918 : value_cat lval,
6919 : bool *non_constant_p, bool *overflow_p,
6920 : tree *jump_target /* = NULL */)
6921 : {
6922 555401349 : if (jump_target && *jump_target)
6923 : {
6924 : /* If we are jumping, ignore all statements/expressions except those
6925 : that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */
6926 40010 : switch (TREE_CODE (t))
6927 : {
6928 : case BIND_EXPR:
6929 : case STATEMENT_LIST:
6930 : case LOOP_EXPR:
6931 : case COND_EXPR:
6932 : case IF_STMT:
6933 : case DO_STMT:
6934 : case WHILE_STMT:
6935 : case FOR_STMT:
6936 : break;
6937 13754 : case LABEL_EXPR:
6938 13754 : case CASE_LABEL_EXPR:
6939 13754 : if (label_matches (ctx, jump_target, t))
6940 : /* Found it. */
6941 1892 : *jump_target = NULL_TREE;
6942 13754 : return NULL_TREE;
6943 : default:
6944 : return NULL_TREE;
6945 : }
6946 : }
6947 555366136 : if (error_operand_p (t))
6948 : {
6949 200 : *non_constant_p = true;
6950 200 : return t;
6951 : }
6952 :
6953 555365936 : location_t loc = cp_expr_loc_or_input_loc (t);
6954 :
6955 555365936 : STRIP_ANY_LOCATION_WRAPPER (t);
6956 :
6957 555365936 : if (CONSTANT_CLASS_P (t))
6958 : {
6959 153412318 : if (TREE_OVERFLOW (t))
6960 : {
6961 62 : if (!ctx->quiet)
6962 17 : permerror (input_location, "overflow in constant expression");
6963 62 : if (!flag_permissive || ctx->quiet)
6964 58 : *overflow_p = true;
6965 : }
6966 :
6967 153412318 : if (TREE_CODE (t) == INTEGER_CST
6968 148043720 : && TYPE_PTR_P (TREE_TYPE (t))
6969 : /* INTEGER_CST with pointer-to-method type is only used
6970 : for a virtual method in a pointer to member function.
6971 : Don't reject those. */
6972 238010 : && TREE_CODE (TREE_TYPE (TREE_TYPE (t))) != METHOD_TYPE
6973 153650122 : && !integer_zerop (t))
6974 : {
6975 7 : if (!ctx->quiet)
6976 0 : error ("value %qE of type %qT is not a constant expression",
6977 0 : t, TREE_TYPE (t));
6978 7 : *non_constant_p = true;
6979 : }
6980 :
6981 153412318 : return t;
6982 : }
6983 :
6984 : /* Avoid excessively long constexpr evaluations. */
6985 401953618 : if (++ctx->global->constexpr_ops_count >= constexpr_ops_limit)
6986 : {
6987 9 : if (!ctx->quiet)
6988 3 : error_at (loc,
6989 : "%<constexpr%> evaluation operation count exceeds limit of "
6990 : "%wd (use %<-fconstexpr-ops-limit=%> to increase the limit)",
6991 : constexpr_ops_limit);
6992 9 : ctx->global->constexpr_ops_count = INTTYPE_MINIMUM (HOST_WIDE_INT);
6993 9 : *non_constant_p = true;
6994 9 : return t;
6995 : }
6996 :
6997 401953609 : constexpr_ctx new_ctx;
6998 401953609 : tree r = t;
6999 :
7000 401953609 : tree_code tcode = TREE_CODE (t);
7001 401953609 : switch (tcode)
7002 : {
7003 4782897 : case RESULT_DECL:
7004 4782897 : if (lval)
7005 : return t;
7006 : /* We ask for an rvalue for the RESULT_DECL when indirecting
7007 : through an invisible reference, or in named return value
7008 : optimization. */
7009 780 : if (tree v = ctx->global->get_value (t))
7010 : return v;
7011 : else
7012 : {
7013 0 : if (!ctx->quiet)
7014 0 : error ("%qE is not a constant expression", t);
7015 0 : *non_constant_p = true;
7016 : }
7017 0 : break;
7018 :
7019 82438230 : case VAR_DECL:
7020 82438230 : if (DECL_HAS_VALUE_EXPR_P (t))
7021 : {
7022 157375 : if (is_normal_capture_proxy (t)
7023 157375 : && current_function_decl == DECL_CONTEXT (t))
7024 : {
7025 : /* Function parms aren't constexpr within the function
7026 : definition, so don't try to look at the closure. But if the
7027 : captured variable is constant, try to evaluate it directly. */
7028 28920 : r = DECL_CAPTURED_VARIABLE (t);
7029 28920 : tree type = TREE_TYPE (t);
7030 28920 : if (TYPE_REF_P (type) != TYPE_REF_P (TREE_TYPE (r)))
7031 : {
7032 : /* Adjust r to match the reference-ness of t. */
7033 16209 : if (TYPE_REF_P (type))
7034 16209 : r = build_address (r);
7035 : else
7036 0 : r = convert_from_reference (r);
7037 : }
7038 : }
7039 : else
7040 128455 : r = DECL_VALUE_EXPR (t);
7041 157375 : return cxx_eval_constant_expression (ctx, r, lval, non_constant_p,
7042 157375 : overflow_p);
7043 : }
7044 : /* fall through */
7045 88153410 : case CONST_DECL:
7046 : /* We used to not check lval for CONST_DECL, but darwin.cc uses
7047 : CONST_DECL for aggregate constants. */
7048 88153410 : if (lval)
7049 : return t;
7050 80608316 : else if (t == ctx->object)
7051 231056 : return ctx->ctor;
7052 80377260 : if (VAR_P (t))
7053 149009410 : if (tree v = ctx->global->get_value (t))
7054 : {
7055 : r = v;
7056 : break;
7057 : }
7058 78265661 : if (ctx->manifestly_const_eval == mce_true)
7059 29301956 : maybe_warn_about_constant_value (loc, t);
7060 78265661 : if (COMPLETE_TYPE_P (TREE_TYPE (t))
7061 78265661 : && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
7062 : {
7063 : /* If the class is empty, we aren't actually loading anything. */
7064 55033 : r = build_constructor (TREE_TYPE (t), NULL);
7065 55033 : TREE_CONSTANT (r) = true;
7066 : }
7067 78210628 : else if (ctx->strict)
7068 77978827 : r = decl_really_constant_value (t, /*unshare_p=*/false);
7069 : else
7070 231801 : r = decl_constant_value (t, /*unshare_p=*/false);
7071 78254877 : if (TREE_CODE (r) == TARGET_EXPR
7072 78254877 : && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR)
7073 0 : r = TARGET_EXPR_INITIAL (r);
7074 78254877 : if (DECL_P (r))
7075 : {
7076 33715262 : if (!ctx->quiet)
7077 90 : non_const_var_error (loc, r, /*fundef_p*/false);
7078 33715262 : *non_constant_p = true;
7079 : }
7080 : break;
7081 :
7082 : case DEBUG_BEGIN_STMT:
7083 : /* ??? It might be nice to retain this information somehow, so
7084 : as to be able to step into a constexpr function call. */
7085 : /* Fall through. */
7086 :
7087 : case FUNCTION_DECL:
7088 : case TEMPLATE_DECL:
7089 : case LABEL_DECL:
7090 : case LABEL_EXPR:
7091 : case CASE_LABEL_EXPR:
7092 : case PREDICT_EXPR:
7093 : return t;
7094 :
7095 48124632 : case PARM_DECL:
7096 48124632 : if (lval && !TYPE_REF_P (TREE_TYPE (t)))
7097 : /* glvalue use. */;
7098 92325968 : else if (tree v = ctx->global->get_value (t))
7099 : r = v;
7100 39592437 : else if (lval)
7101 : /* Defer in case this is only used for its type. */;
7102 39592437 : else if (COMPLETE_TYPE_P (TREE_TYPE (t))
7103 39592437 : && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
7104 : {
7105 : /* If the class is empty, we aren't actually loading anything. */
7106 833 : r = build_constructor (TREE_TYPE (t), NULL);
7107 833 : TREE_CONSTANT (r) = true;
7108 : }
7109 : else
7110 : {
7111 39591604 : if (!ctx->quiet)
7112 97 : error ("%qE is not a constant expression", t);
7113 39591604 : *non_constant_p = true;
7114 : }
7115 : break;
7116 :
7117 37084400 : case CALL_EXPR:
7118 37084400 : case AGGR_INIT_EXPR:
7119 37084400 : r = cxx_eval_call_expression (ctx, t, lval,
7120 : non_constant_p, overflow_p);
7121 37084400 : break;
7122 :
7123 356144 : case DECL_EXPR:
7124 356144 : {
7125 356144 : r = DECL_EXPR_DECL (t);
7126 356144 : if (TREE_CODE (r) == USING_DECL)
7127 : {
7128 28 : r = void_node;
7129 28 : break;
7130 : }
7131 :
7132 356116 : if (VAR_P (r)
7133 356116 : && (TREE_STATIC (r)
7134 353997 : || (CP_DECL_THREAD_LOCAL_P (r) && !DECL_REALLY_EXTERN (r)))
7135 : /* Allow __FUNCTION__ etc. */
7136 2119 : && !DECL_ARTIFICIAL (r)
7137 356137 : && !decl_constant_var_p (r))
7138 : {
7139 8 : if (!ctx->quiet)
7140 : {
7141 2 : if (CP_DECL_THREAD_LOCAL_P (r))
7142 1 : error_at (loc, "control passes through definition of %qD "
7143 : "with thread storage duration", r);
7144 : else
7145 1 : error_at (loc, "control passes through definition of %qD "
7146 : "with static storage duration", r);
7147 : }
7148 8 : *non_constant_p = true;
7149 8 : break;
7150 : }
7151 :
7152 : /* make_rtl_for_nonlocal_decl could have deferred emission of
7153 : a local static var, but if it appears in a statement expression
7154 : which is constant expression evaluated to e.g. just the address
7155 : of the variable, its DECL_EXPR will never be seen during
7156 : gimple lowering's record_vars_into as the statement expression
7157 : will not be in the IL at all. */
7158 356108 : if (VAR_P (r)
7159 356108 : && TREE_STATIC (r)
7160 2111 : && !DECL_REALLY_EXTERN (r)
7161 2111 : && DECL_FUNCTION_SCOPE_P (r)
7162 2111 : && !var_in_maybe_constexpr_fn (r)
7163 356111 : && decl_constant_var_p (r))
7164 : {
7165 3 : varpool_node *node = varpool_node::get (r);
7166 3 : if (node == NULL || !node->definition)
7167 3 : rest_of_decl_compilation (r, 0, at_eof);
7168 : }
7169 :
7170 708467 : if (AGGREGATE_TYPE_P (TREE_TYPE (r))
7171 684674 : || VECTOR_TYPE_P (TREE_TYPE (r)))
7172 : {
7173 27738 : new_ctx = *ctx;
7174 27738 : new_ctx.object = r;
7175 27738 : new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL);
7176 27738 : CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
7177 27738 : ctx->global->put_value (r, new_ctx.ctor);
7178 27738 : ctx = &new_ctx;
7179 : }
7180 :
7181 356108 : if (tree init = DECL_INITIAL (r))
7182 : {
7183 201168 : init = cxx_eval_constant_expression (ctx, init, vc_prvalue,
7184 : non_constant_p, overflow_p);
7185 : /* Don't share a CONSTRUCTOR that might be changed. */
7186 201168 : init = unshare_constructor (init);
7187 : /* Remember that a constant object's constructor has already
7188 : run. */
7189 402336 : if (CLASS_TYPE_P (TREE_TYPE (r))
7190 209762 : && CP_TYPE_CONST_P (TREE_TYPE (r)))
7191 292 : TREE_READONLY (init) = true;
7192 201168 : ctx->global->put_value (r, init);
7193 : }
7194 154940 : else if (ctx == &new_ctx)
7195 : /* We gave it a CONSTRUCTOR above. */;
7196 : else
7197 137431 : ctx->global->put_value (r, NULL_TREE);
7198 : }
7199 : break;
7200 :
7201 2713538 : case TARGET_EXPR:
7202 2713538 : {
7203 2713538 : tree type = TREE_TYPE (t);
7204 :
7205 2713538 : if (!literal_type_p (type))
7206 : {
7207 18 : if (!ctx->quiet)
7208 : {
7209 0 : auto_diagnostic_group d;
7210 0 : error ("temporary of non-literal type %qT in a "
7211 : "constant expression", type);
7212 0 : explain_non_literal_class (type);
7213 0 : }
7214 18 : *non_constant_p = true;
7215 439429 : break;
7216 : }
7217 2713520 : gcc_checking_assert (!TARGET_EXPR_DIRECT_INIT_P (t));
7218 : /* Avoid evaluating a TARGET_EXPR more than once. */
7219 2713520 : tree slot = TARGET_EXPR_SLOT (t);
7220 5427040 : if (tree v = ctx->global->get_value (slot))
7221 : {
7222 147 : if (lval)
7223 2094243 : return slot;
7224 : r = v;
7225 : break;
7226 : }
7227 2713373 : if ((AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type)))
7228 : {
7229 : /* We're being expanded without an explicit target, so start
7230 : initializing a new object; expansion with an explicit target
7231 : strips the TARGET_EXPR before we get here. */
7232 2109941 : new_ctx = *ctx;
7233 : /* Link CTX to NEW_CTX so that lookup_placeholder can resolve
7234 : any PLACEHOLDER_EXPR within the initializer that refers to the
7235 : former object under construction. */
7236 2109941 : new_ctx.parent = ctx;
7237 2109941 : new_ctx.ctor = build_constructor (type, NULL);
7238 2109941 : CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
7239 2109941 : new_ctx.object = slot;
7240 2109941 : ctx->global->put_value (new_ctx.object, new_ctx.ctor);
7241 2109941 : ctx = &new_ctx;
7242 : }
7243 : /* Pass vc_prvalue because this indicates
7244 : initialization of a temporary. */
7245 2713373 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), vc_prvalue,
7246 : non_constant_p, overflow_p);
7247 2713373 : if (*non_constant_p)
7248 : break;
7249 : /* If the initializer is complex, evaluate it to initialize slot. */
7250 2274060 : bool is_complex = target_expr_needs_replace (t);
7251 2274060 : if (!is_complex)
7252 : {
7253 2274060 : r = unshare_constructor (r);
7254 : /* Adjust the type of the result to the type of the temporary. */
7255 2274060 : r = adjust_temp_type (type, r);
7256 2274060 : ctx->global->put_value (slot, r);
7257 : }
7258 2274060 : if (TARGET_EXPR_CLEANUP (t) && !CLEANUP_EH_ONLY (t))
7259 8090 : ctx->global->cleanups->safe_push (TARGET_EXPR_CLEANUP (t));
7260 2274060 : if (ctx->save_exprs)
7261 323754 : ctx->save_exprs->safe_push (slot);
7262 2274060 : if (lval)
7263 : return slot;
7264 179866 : if (is_complex)
7265 0 : r = ctx->global->get_value (slot);
7266 : }
7267 179866 : break;
7268 :
7269 12823584 : case INIT_EXPR:
7270 12823584 : case MODIFY_EXPR:
7271 12823584 : gcc_assert (jump_target == NULL || *jump_target == NULL_TREE);
7272 12823584 : r = cxx_eval_store_expression (ctx, t, lval,
7273 : non_constant_p, overflow_p);
7274 12823584 : break;
7275 :
7276 0 : case SCOPE_REF:
7277 0 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
7278 : lval,
7279 : non_constant_p, overflow_p);
7280 0 : break;
7281 :
7282 7762214 : case RETURN_EXPR:
7283 7762214 : if (TREE_OPERAND (t, 0) != NULL_TREE)
7284 7759391 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
7285 : lval,
7286 : non_constant_p, overflow_p);
7287 : /* FALLTHRU */
7288 7763956 : case BREAK_STMT:
7289 7763956 : case CONTINUE_STMT:
7290 7763956 : if (jump_target)
7291 7763956 : *jump_target = t;
7292 : else
7293 : {
7294 : /* Can happen with ({ return true; }) && false; passed to
7295 : maybe_constant_value. There is nothing to jump over in this
7296 : case, and the bug will be diagnosed later. */
7297 0 : gcc_assert (ctx->quiet);
7298 0 : *non_constant_p = true;
7299 : }
7300 : break;
7301 :
7302 106497 : case SAVE_EXPR:
7303 : /* Avoid evaluating a SAVE_EXPR more than once. */
7304 212994 : if (tree v = ctx->global->get_value (t))
7305 : r = v;
7306 : else
7307 : {
7308 102159 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), vc_prvalue,
7309 : non_constant_p, overflow_p);
7310 102159 : if (*non_constant_p)
7311 : break;
7312 4170 : ctx->global->put_value (t, r);
7313 4170 : if (ctx->save_exprs)
7314 3886 : ctx->save_exprs->safe_push (t);
7315 : }
7316 : break;
7317 :
7318 0 : case TRY_CATCH_EXPR:
7319 0 : if (TREE_OPERAND (t, 0) == NULL_TREE)
7320 : {
7321 0 : r = void_node;
7322 0 : break;
7323 : }
7324 : /* FALLTHRU */
7325 13190275 : case NON_LVALUE_EXPR:
7326 13190275 : case TRY_BLOCK:
7327 13190275 : case MUST_NOT_THROW_EXPR:
7328 13190275 : case EXPR_STMT:
7329 13190275 : case EH_SPEC_BLOCK:
7330 13190275 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
7331 : lval,
7332 : non_constant_p, overflow_p,
7333 : jump_target);
7334 13190275 : break;
7335 :
7336 7557076 : case CLEANUP_POINT_EXPR:
7337 7557076 : {
7338 7557076 : auto_vec<tree, 2> cleanups;
7339 7557076 : vec<tree> *prev_cleanups = ctx->global->cleanups;
7340 7557076 : ctx->global->cleanups = &cleanups;
7341 7557076 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
7342 : lval,
7343 : non_constant_p, overflow_p,
7344 : jump_target);
7345 7557076 : ctx->global->cleanups = prev_cleanups;
7346 7557076 : unsigned int i;
7347 7557076 : tree cleanup;
7348 : /* Evaluate the cleanups. */
7349 22677529 : FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup)
7350 6301 : cxx_eval_constant_expression (ctx, cleanup, vc_discard,
7351 : non_constant_p, overflow_p);
7352 7557076 : }
7353 7557076 : break;
7354 :
7355 88 : case TRY_FINALLY_EXPR:
7356 88 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
7357 : non_constant_p, overflow_p,
7358 : jump_target);
7359 88 : if (!*non_constant_p)
7360 : /* Also evaluate the cleanup. */
7361 85 : cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), vc_discard,
7362 : non_constant_p, overflow_p);
7363 : break;
7364 :
7365 20255 : case CLEANUP_STMT:
7366 20255 : r = cxx_eval_constant_expression (ctx, CLEANUP_BODY (t), lval,
7367 : non_constant_p, overflow_p,
7368 : jump_target);
7369 20255 : if (!CLEANUP_EH_ONLY (t) && !*non_constant_p)
7370 : {
7371 4447 : iloc_sentinel ils (loc);
7372 : /* Also evaluate the cleanup. */
7373 4447 : cxx_eval_constant_expression (ctx, CLEANUP_EXPR (t), vc_discard,
7374 : non_constant_p, overflow_p);
7375 4447 : }
7376 : break;
7377 :
7378 : /* These differ from cxx_eval_unary_expression in that this doesn't
7379 : check for a constant operand or result; an address can be
7380 : constant without its operand being, and vice versa. */
7381 8680081 : case MEM_REF:
7382 8680081 : case INDIRECT_REF:
7383 8680081 : r = cxx_eval_indirect_ref (ctx, t, lval,
7384 : non_constant_p, overflow_p);
7385 8680081 : break;
7386 :
7387 9742755 : case ADDR_EXPR:
7388 9742755 : {
7389 9742755 : tree oldop = TREE_OPERAND (t, 0);
7390 9742755 : tree op = cxx_eval_constant_expression (ctx, oldop, vc_glvalue,
7391 : non_constant_p, overflow_p);
7392 : /* Don't VERIFY_CONSTANT here. */
7393 9742755 : if (*non_constant_p)
7394 : return t;
7395 8228910 : gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR);
7396 : /* This function does more aggressive folding than fold itself. */
7397 8228910 : r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
7398 8228910 : if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
7399 : {
7400 5666884 : ggc_free (r);
7401 5666884 : return t;
7402 : }
7403 : break;
7404 : }
7405 :
7406 200490 : case REALPART_EXPR:
7407 200490 : case IMAGPART_EXPR:
7408 200490 : if (lval)
7409 : {
7410 30 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
7411 : non_constant_p, overflow_p);
7412 30 : if (r == error_mark_node)
7413 : ;
7414 30 : else if (r == TREE_OPERAND (t, 0) || lval == vc_discard)
7415 : r = t;
7416 : else
7417 30 : r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), r);
7418 : break;
7419 : }
7420 : /* FALLTHRU */
7421 15997102 : case CONJ_EXPR:
7422 15997102 : case FIX_TRUNC_EXPR:
7423 15997102 : case FLOAT_EXPR:
7424 15997102 : case NEGATE_EXPR:
7425 15997102 : case ABS_EXPR:
7426 15997102 : case ABSU_EXPR:
7427 15997102 : case BIT_NOT_EXPR:
7428 15997102 : case TRUTH_NOT_EXPR:
7429 15997102 : case FIXED_CONVERT_EXPR:
7430 15997102 : r = cxx_eval_unary_expression (ctx, t, lval,
7431 : non_constant_p, overflow_p);
7432 15997102 : break;
7433 :
7434 6229506 : case SIZEOF_EXPR:
7435 6229506 : r = fold_sizeof_expr (t);
7436 : /* In a template, fold_sizeof_expr may merely create a new SIZEOF_EXPR,
7437 : which could lead to an infinite recursion. */
7438 6229506 : if (TREE_CODE (r) != SIZEOF_EXPR)
7439 6229506 : r = cxx_eval_constant_expression (ctx, r, lval,
7440 : non_constant_p, overflow_p,
7441 : jump_target);
7442 : else
7443 : {
7444 0 : *non_constant_p = true;
7445 0 : gcc_assert (ctx->quiet);
7446 : }
7447 :
7448 : break;
7449 :
7450 1661788 : case COMPOUND_EXPR:
7451 1661788 : {
7452 : /* check_return_expr sometimes wraps a TARGET_EXPR in a
7453 : COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
7454 : introduced by build_call_a. */
7455 1661788 : tree op0 = TREE_OPERAND (t, 0);
7456 1661788 : tree op1 = TREE_OPERAND (t, 1);
7457 1661788 : STRIP_NOPS (op1);
7458 105917 : if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
7459 1681727 : || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
7460 1544664 : r = cxx_eval_constant_expression (ctx, op0,
7461 : lval, non_constant_p, overflow_p,
7462 : jump_target);
7463 : else
7464 : {
7465 : /* Check that the LHS is constant and then discard it. */
7466 117124 : cxx_eval_constant_expression (ctx, op0, vc_discard,
7467 : non_constant_p, overflow_p,
7468 : jump_target);
7469 117124 : if (*non_constant_p)
7470 : return t;
7471 104935 : op1 = TREE_OPERAND (t, 1);
7472 104935 : r = cxx_eval_constant_expression (ctx, op1,
7473 : lval, non_constant_p, overflow_p,
7474 : jump_target);
7475 : }
7476 : }
7477 : break;
7478 :
7479 27942488 : case POINTER_PLUS_EXPR:
7480 27942488 : case POINTER_DIFF_EXPR:
7481 27942488 : case PLUS_EXPR:
7482 27942488 : case MINUS_EXPR:
7483 27942488 : case MULT_EXPR:
7484 27942488 : case TRUNC_DIV_EXPR:
7485 27942488 : case CEIL_DIV_EXPR:
7486 27942488 : case FLOOR_DIV_EXPR:
7487 27942488 : case ROUND_DIV_EXPR:
7488 27942488 : case TRUNC_MOD_EXPR:
7489 27942488 : case CEIL_MOD_EXPR:
7490 27942488 : case ROUND_MOD_EXPR:
7491 27942488 : case RDIV_EXPR:
7492 27942488 : case EXACT_DIV_EXPR:
7493 27942488 : case MIN_EXPR:
7494 27942488 : case MAX_EXPR:
7495 27942488 : case LSHIFT_EXPR:
7496 27942488 : case RSHIFT_EXPR:
7497 27942488 : case LROTATE_EXPR:
7498 27942488 : case RROTATE_EXPR:
7499 27942488 : case BIT_IOR_EXPR:
7500 27942488 : case BIT_XOR_EXPR:
7501 27942488 : case BIT_AND_EXPR:
7502 27942488 : case TRUTH_XOR_EXPR:
7503 27942488 : case LT_EXPR:
7504 27942488 : case LE_EXPR:
7505 27942488 : case GT_EXPR:
7506 27942488 : case GE_EXPR:
7507 27942488 : case EQ_EXPR:
7508 27942488 : case NE_EXPR:
7509 27942488 : case SPACESHIP_EXPR:
7510 27942488 : case UNORDERED_EXPR:
7511 27942488 : case ORDERED_EXPR:
7512 27942488 : case UNLT_EXPR:
7513 27942488 : case UNLE_EXPR:
7514 27942488 : case UNGT_EXPR:
7515 27942488 : case UNGE_EXPR:
7516 27942488 : case UNEQ_EXPR:
7517 27942488 : case LTGT_EXPR:
7518 27942488 : case RANGE_EXPR:
7519 27942488 : case COMPLEX_EXPR:
7520 27942488 : r = cxx_eval_binary_expression (ctx, t, lval,
7521 : non_constant_p, overflow_p);
7522 27942488 : break;
7523 :
7524 : /* fold can introduce non-IF versions of these; still treat them as
7525 : short-circuiting. */
7526 4649366 : case TRUTH_AND_EXPR:
7527 4649366 : case TRUTH_ANDIF_EXPR:
7528 4649366 : r = cxx_eval_logical_expression (ctx, t, boolean_false_node,
7529 : boolean_true_node,
7530 : non_constant_p, overflow_p);
7531 4649366 : break;
7532 :
7533 1462387 : case TRUTH_OR_EXPR:
7534 1462387 : case TRUTH_ORIF_EXPR:
7535 1462387 : r = cxx_eval_logical_expression (ctx, t, boolean_true_node,
7536 : boolean_false_node,
7537 : non_constant_p, overflow_p);
7538 1462387 : break;
7539 :
7540 589221 : case ARRAY_REF:
7541 589221 : r = cxx_eval_array_reference (ctx, t, lval,
7542 : non_constant_p, overflow_p);
7543 589221 : break;
7544 :
7545 6200209 : case COMPONENT_REF:
7546 6200209 : if (is_overloaded_fn (t))
7547 : {
7548 : /* We can only get here in checking mode via
7549 : build_non_dependent_expr, because any expression that
7550 : calls or takes the address of the function will have
7551 : pulled a FUNCTION_DECL out of the COMPONENT_REF. */
7552 3 : gcc_checking_assert (ctx->quiet || errorcount);
7553 3 : *non_constant_p = true;
7554 3 : return t;
7555 : }
7556 6200206 : r = cxx_eval_component_reference (ctx, t, lval,
7557 : non_constant_p, overflow_p);
7558 6200206 : break;
7559 :
7560 63 : case BIT_FIELD_REF:
7561 63 : r = cxx_eval_bit_field_ref (ctx, t, lval,
7562 : non_constant_p, overflow_p);
7563 63 : break;
7564 :
7565 1810812 : case COND_EXPR:
7566 1810812 : case IF_STMT:
7567 1810812 : if (jump_target && *jump_target)
7568 : {
7569 874 : tree orig_jump = *jump_target;
7570 874 : tree arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 1))
7571 1748 : ? TREE_OPERAND (t, 1) : void_node);
7572 : /* When jumping to a label, the label might be either in the
7573 : then or else blocks, so process then block first in skipping
7574 : mode first, and if we are still in the skipping mode at its end,
7575 : process the else block too. */
7576 874 : r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p,
7577 : overflow_p, jump_target);
7578 : /* It's possible that we found the label in the then block. But
7579 : it could have been followed by another jumping statement, e.g.
7580 : say we're looking for case 1:
7581 : if (cond)
7582 : {
7583 : // skipped statements
7584 : case 1:; // clears up *jump_target
7585 : return 1; // and sets it to a RETURN_EXPR
7586 : }
7587 : else { ... }
7588 : in which case we need not go looking to the else block.
7589 : (goto is not allowed in a constexpr function.) */
7590 874 : if (*jump_target == orig_jump)
7591 : {
7592 814 : arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 2))
7593 1628 : ? TREE_OPERAND (t, 2) : void_node);
7594 814 : r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p,
7595 : overflow_p, jump_target);
7596 : }
7597 : break;
7598 : }
7599 1809938 : r = cxx_eval_conditional_expression (ctx, t, lval,
7600 : non_constant_p, overflow_p,
7601 : jump_target);
7602 1809938 : break;
7603 659 : case VEC_COND_EXPR:
7604 659 : r = cxx_eval_vector_conditional_expression (ctx, t, non_constant_p,
7605 : overflow_p);
7606 659 : break;
7607 :
7608 4558696 : case CONSTRUCTOR:
7609 4558696 : if (TREE_CONSTANT (t) && reduced_constant_expression_p (t))
7610 : {
7611 : /* Don't re-process a constant CONSTRUCTOR. */
7612 4150227 : verify_constructor_flags (t);
7613 4150227 : if (TREE_CONSTANT (t))
7614 : return t;
7615 : }
7616 408469 : r = cxx_eval_bare_aggregate (ctx, t, lval,
7617 : non_constant_p, overflow_p);
7618 408469 : break;
7619 :
7620 295 : case VEC_INIT_EXPR:
7621 : /* We can get this in a defaulted constructor for a class with a
7622 : non-static data member of array type. Either the initializer will
7623 : be NULL, meaning default-initialization, or it will be an lvalue
7624 : or xvalue of the same type, meaning direct-initialization from the
7625 : corresponding member. */
7626 295 : r = cxx_eval_vec_init (ctx, t, lval,
7627 : non_constant_p, overflow_p);
7628 295 : break;
7629 :
7630 18 : case VEC_PERM_EXPR:
7631 18 : r = cxx_eval_trinary_expression (ctx, t, lval,
7632 : non_constant_p, overflow_p);
7633 18 : break;
7634 :
7635 3 : case PAREN_EXPR:
7636 3 : gcc_assert (!REF_PARENTHESIZED_P (t));
7637 : /* A PAREN_EXPR resulting from __builtin_assoc_barrier has no effect in
7638 : constant expressions since it's unaffected by -fassociative-math. */
7639 3 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
7640 : non_constant_p, overflow_p);
7641 3 : break;
7642 :
7643 59942990 : case NOP_EXPR:
7644 59942990 : if (REINTERPRET_CAST_P (t))
7645 : {
7646 25480 : if (!ctx->quiet)
7647 8 : error_at (loc,
7648 : "%<reinterpret_cast%> is not a constant expression");
7649 25480 : *non_constant_p = true;
7650 25480 : return t;
7651 : }
7652 : /* FALLTHROUGH. */
7653 72791943 : case CONVERT_EXPR:
7654 72791943 : case VIEW_CONVERT_EXPR:
7655 72791943 : case UNARY_PLUS_EXPR:
7656 72791943 : {
7657 72791943 : tree oldop = TREE_OPERAND (t, 0);
7658 :
7659 72791943 : tree op = cxx_eval_constant_expression (ctx, oldop,
7660 : lval,
7661 : non_constant_p, overflow_p);
7662 72781159 : if (*non_constant_p)
7663 : return t;
7664 58224681 : tree type = TREE_TYPE (t);
7665 :
7666 58224681 : if (VOID_TYPE_P (type))
7667 4109691 : return void_node;
7668 :
7669 54114990 : if (TREE_CODE (t) == CONVERT_EXPR
7670 4433520 : && ARITHMETIC_TYPE_P (type)
7671 1198205 : && INDIRECT_TYPE_P (TREE_TYPE (op))
7672 54137730 : && ctx->manifestly_const_eval == mce_true)
7673 : {
7674 169 : if (!ctx->quiet)
7675 33 : error_at (loc,
7676 : "conversion from pointer type %qT to arithmetic type "
7677 33 : "%qT in a constant expression", TREE_TYPE (op), type);
7678 169 : *non_constant_p = true;
7679 169 : return t;
7680 : }
7681 :
7682 : /* [expr.const]: a conversion from type cv void* to a pointer-to-object
7683 : type cannot be part of a core constant expression as a resolution to
7684 : DR 1312. */
7685 8172789 : if (TYPE_PTROB_P (type)
7686 7459596 : && TYPE_PTR_P (TREE_TYPE (op))
7687 4254722 : && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (op)))
7688 : /* Inside a call to std::construct_at or to
7689 : std::allocator<T>::{,de}allocate, we permit casting from void*
7690 : because that is compiler-generated code. */
7691 10581 : && !is_std_construct_at (ctx->call)
7692 54115799 : && !is_std_allocator_allocate (ctx->call))
7693 : {
7694 : /* Likewise, don't error when casting from void* when OP is
7695 : &heap uninit and similar. */
7696 716 : tree sop = tree_strip_nop_conversions (op);
7697 716 : if (TREE_CODE (sop) == ADDR_EXPR
7698 601 : && VAR_P (TREE_OPERAND (sop, 0))
7699 1314 : && DECL_ARTIFICIAL (TREE_OPERAND (sop, 0)))
7700 : /* OK */;
7701 : /* P2738 (C++26): a conversion from a prvalue P of type "pointer to
7702 : cv void" to a pointer-to-object type T unless P points to an
7703 : object whose type is similar to T. */
7704 152 : else if (cxx_dialect > cxx23
7705 155 : && (sop = cxx_fold_indirect_ref (ctx, loc,
7706 3 : TREE_TYPE (type), sop)))
7707 : {
7708 3 : r = build1 (ADDR_EXPR, type, sop);
7709 3 : break;
7710 : }
7711 : else
7712 : {
7713 149 : if (!ctx->quiet)
7714 18 : error_at (loc, "cast from %qT is not allowed",
7715 18 : TREE_TYPE (op));
7716 149 : *non_constant_p = true;
7717 149 : return t;
7718 : }
7719 : }
7720 :
7721 54114669 : if (TREE_CODE (op) == PTRMEM_CST && !TYPE_PTRMEM_P (type))
7722 : {
7723 914 : op = cplus_expand_constant (op);
7724 914 : if (TREE_CODE (op) == PTRMEM_CST)
7725 : {
7726 21 : if (!ctx->quiet)
7727 3 : error_at (loc, "%qE is not a constant expression when the "
7728 : "class %qT is still incomplete", op,
7729 3 : PTRMEM_CST_CLASS (op));
7730 21 : *non_constant_p = true;
7731 21 : return t;
7732 : }
7733 : }
7734 :
7735 54114648 : if (TREE_CODE (op) == PTRMEM_CST && tcode == NOP_EXPR)
7736 : {
7737 147 : if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (op))
7738 147 : && !can_convert_qual (type, op))
7739 7 : op = cplus_expand_constant (op);
7740 147 : return cp_fold_convert (type, op);
7741 : }
7742 :
7743 54114501 : if (INDIRECT_TYPE_P (type) && TREE_CODE (op) == INTEGER_CST)
7744 : {
7745 165110 : if (integer_zerop (op))
7746 : {
7747 123305 : if (TYPE_REF_P (type))
7748 : {
7749 35 : if (!ctx->quiet)
7750 4 : error_at (loc, "dereferencing a null pointer");
7751 35 : *non_constant_p = true;
7752 35 : return t;
7753 : }
7754 : }
7755 : else
7756 : {
7757 : /* This detects for example:
7758 : reinterpret_cast<void*>(sizeof 0)
7759 : */
7760 41805 : if (!ctx->quiet)
7761 9 : error_at (loc, "%<reinterpret_cast<%T>(%E)%> is not "
7762 : "a constant expression",
7763 : type, op);
7764 41805 : *non_constant_p = true;
7765 41805 : return t;
7766 : }
7767 : }
7768 :
7769 54072661 : if (INDIRECT_TYPE_P (type)
7770 12547547 : && TREE_CODE (op) == NOP_EXPR
7771 4902546 : && TREE_TYPE (op) == ptr_type_node
7772 10359 : && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR
7773 9384 : && VAR_P (TREE_OPERAND (TREE_OPERAND (op, 0), 0))
7774 54078638 : && (DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0),
7775 5977 : 0)) == heap_uninit_identifier
7776 5509 : || DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0),
7777 5509 : 0)) == heap_vec_uninit_identifier))
7778 : {
7779 612 : tree var = TREE_OPERAND (TREE_OPERAND (op, 0), 0);
7780 612 : tree var_size = TYPE_SIZE_UNIT (TREE_TYPE (var));
7781 612 : tree elt_type = TREE_TYPE (type);
7782 612 : tree cookie_size = NULL_TREE;
7783 612 : tree arg_size = NULL_TREE;
7784 612 : if (TREE_CODE (elt_type) == RECORD_TYPE
7785 612 : && TYPE_NAME (elt_type) == heap_identifier)
7786 : {
7787 1 : tree fld1 = TYPE_FIELDS (elt_type);
7788 1 : tree fld2 = DECL_CHAIN (fld1);
7789 1 : elt_type = TREE_TYPE (TREE_TYPE (fld2));
7790 1 : cookie_size = TYPE_SIZE_UNIT (TREE_TYPE (fld1));
7791 : }
7792 612 : DECL_NAME (var)
7793 612 : = (DECL_NAME (var) == heap_uninit_identifier
7794 612 : ? heap_identifier : heap_vec_identifier);
7795 : /* For zero sized elt_type, try to recover how many outer_nelts
7796 : it should have. */
7797 612 : if ((cookie_size ? tree_int_cst_equal (var_size, cookie_size)
7798 611 : : integer_zerop (var_size))
7799 9 : && !int_size_in_bytes (elt_type)
7800 3 : && TREE_CODE (oldop) == CALL_EXPR
7801 616 : && call_expr_nargs (oldop) >= 1)
7802 3 : if (tree fun = get_function_named_in_call (oldop))
7803 3 : if (cxx_replaceable_global_alloc_fn (fun)
7804 3 : && IDENTIFIER_NEW_OP_P (DECL_NAME (fun)))
7805 3 : arg_size = CALL_EXPR_ARG (oldop, 0);
7806 612 : TREE_TYPE (var)
7807 612 : = build_new_constexpr_heap_type (ctx, elt_type, cookie_size,
7808 : var_size, arg_size,
7809 : non_constant_p, overflow_p);
7810 612 : TREE_TYPE (TREE_OPERAND (op, 0))
7811 1224 : = build_pointer_type (TREE_TYPE (var));
7812 : }
7813 :
7814 54072661 : if (op == oldop && tcode != UNARY_PLUS_EXPR)
7815 : /* We didn't fold at the top so we could check for ptr-int
7816 : conversion. */
7817 4370894 : return fold (t);
7818 :
7819 49701767 : tree sop;
7820 :
7821 : /* Handle an array's bounds having been deduced after we built
7822 : the wrapping expression. */
7823 49701767 : if (same_type_ignoring_tlq_and_bounds_p (type, TREE_TYPE (op)))
7824 : r = op;
7825 10084688 : else if (sop = tree_strip_nop_conversions (op),
7826 12633491 : sop != op && (same_type_ignoring_tlq_and_bounds_p
7827 2548803 : (type, TREE_TYPE (sop))))
7828 : r = sop;
7829 8143179 : else if (tcode == UNARY_PLUS_EXPR)
7830 0 : r = fold_convert (TREE_TYPE (t), op);
7831 : else
7832 8143179 : r = fold_build1 (tcode, type, op);
7833 :
7834 : /* Conversion of an out-of-range value has implementation-defined
7835 : behavior; the language considers it different from arithmetic
7836 : overflow, which is undefined. */
7837 49701767 : if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op))
7838 8287 : TREE_OVERFLOW (r) = false;
7839 : }
7840 : break;
7841 :
7842 490 : case EXCESS_PRECISION_EXPR:
7843 490 : {
7844 490 : tree oldop = TREE_OPERAND (t, 0);
7845 :
7846 490 : tree op = cxx_eval_constant_expression (ctx, oldop,
7847 : lval,
7848 : non_constant_p, overflow_p);
7849 490 : if (*non_constant_p)
7850 : return t;
7851 490 : r = fold_convert (TREE_TYPE (t), op);
7852 490 : break;
7853 : }
7854 :
7855 13781 : case EMPTY_CLASS_EXPR:
7856 : /* Handle EMPTY_CLASS_EXPR produced by build_call_a by lowering
7857 : it to an appropriate CONSTRUCTOR. */
7858 13781 : return build_constructor (TREE_TYPE (t), NULL);
7859 :
7860 7579568 : case STATEMENT_LIST:
7861 7579568 : new_ctx = *ctx;
7862 7579568 : new_ctx.ctor = new_ctx.object = NULL_TREE;
7863 7579568 : return cxx_eval_statement_list (&new_ctx, t,
7864 7579568 : non_constant_p, overflow_p, jump_target);
7865 :
7866 1048096 : case BIND_EXPR:
7867 1048096 : return cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t),
7868 : lval,
7869 : non_constant_p, overflow_p,
7870 1048096 : jump_target);
7871 :
7872 1203951 : case PREINCREMENT_EXPR:
7873 1203951 : case POSTINCREMENT_EXPR:
7874 1203951 : case PREDECREMENT_EXPR:
7875 1203951 : case POSTDECREMENT_EXPR:
7876 1203951 : return cxx_eval_increment_expression (ctx, t,
7877 1203951 : lval, non_constant_p, overflow_p);
7878 :
7879 7039 : case LAMBDA_EXPR:
7880 7039 : case NEW_EXPR:
7881 7039 : case VEC_NEW_EXPR:
7882 7039 : case DELETE_EXPR:
7883 7039 : case VEC_DELETE_EXPR:
7884 7039 : case THROW_EXPR:
7885 7039 : case MODOP_EXPR:
7886 : /* GCC internal stuff. */
7887 7039 : case VA_ARG_EXPR:
7888 7039 : case NON_DEPENDENT_EXPR:
7889 7039 : case BASELINK:
7890 7039 : case OFFSET_REF:
7891 7039 : if (!ctx->quiet)
7892 49 : error_at (loc, "expression %qE is not a constant expression", t);
7893 7039 : *non_constant_p = true;
7894 7039 : break;
7895 :
7896 7851 : case OBJ_TYPE_REF:
7897 : /* Virtual function lookup. We don't need to do anything fancy. */
7898 7851 : return cxx_eval_constant_expression (ctx, OBJ_TYPE_REF_EXPR (t),
7899 7851 : lval, non_constant_p, overflow_p);
7900 :
7901 16026 : case PLACEHOLDER_EXPR:
7902 : /* Use of the value or address of the current object. */
7903 16026 : if (tree ctor = lookup_placeholder (ctx, lval, TREE_TYPE (t)))
7904 : {
7905 15834 : if (TREE_CODE (ctor) == CONSTRUCTOR)
7906 : return ctor;
7907 : else
7908 15542 : return cxx_eval_constant_expression (ctx, ctor, lval,
7909 15542 : non_constant_p, overflow_p);
7910 : }
7911 : /* A placeholder without a referent. We can get here when
7912 : checking whether NSDMIs are noexcept, or in massage_init_elt;
7913 : just say it's non-constant for now. */
7914 192 : gcc_assert (ctx->quiet);
7915 192 : *non_constant_p = true;
7916 192 : break;
7917 :
7918 46 : case EXIT_EXPR:
7919 46 : {
7920 46 : tree cond = TREE_OPERAND (t, 0);
7921 46 : cond = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
7922 : non_constant_p, overflow_p);
7923 46 : VERIFY_CONSTANT (cond);
7924 46 : if (integer_nonzerop (cond))
7925 8 : *jump_target = t;
7926 : }
7927 : break;
7928 :
7929 3 : case GOTO_EXPR:
7930 3 : if (breaks (&TREE_OPERAND (t, 0))
7931 3 : || continues (&TREE_OPERAND (t, 0)))
7932 0 : *jump_target = TREE_OPERAND (t, 0);
7933 : else
7934 : {
7935 3 : gcc_assert (cxx_dialect >= cxx23);
7936 3 : if (!ctx->quiet)
7937 1 : error_at (loc, "%<goto%> is not a constant expression");
7938 3 : *non_constant_p = true;
7939 : }
7940 : break;
7941 :
7942 123953 : case LOOP_EXPR:
7943 123953 : case DO_STMT:
7944 123953 : case WHILE_STMT:
7945 123953 : case FOR_STMT:
7946 123953 : cxx_eval_loop_expr (ctx, t,
7947 : non_constant_p, overflow_p, jump_target);
7948 123953 : break;
7949 :
7950 2057 : case SWITCH_EXPR:
7951 2057 : case SWITCH_STMT:
7952 2057 : cxx_eval_switch_expr (ctx, t,
7953 : non_constant_p, overflow_p, jump_target);
7954 2057 : break;
7955 :
7956 52 : case REQUIRES_EXPR:
7957 : /* It's possible to get a requires-expression in a constant
7958 : expression. For example:
7959 :
7960 : template<typename T> concept bool C() {
7961 : return requires (T t) { t; };
7962 : }
7963 :
7964 : template<typename T> requires !C<T>() void f(T);
7965 :
7966 : Normalization leaves f with the associated constraint
7967 : '!requires (T t) { ... }' which is not transformed into
7968 : a constraint. */
7969 52 : if (!processing_template_decl)
7970 52 : return evaluate_requires_expr (t);
7971 : else
7972 0 : *non_constant_p = true;
7973 0 : return t;
7974 :
7975 57 : case ANNOTATE_EXPR:
7976 57 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
7977 : lval,
7978 : non_constant_p, overflow_p,
7979 : jump_target);
7980 57 : break;
7981 :
7982 359 : case USING_STMT:
7983 359 : r = void_node;
7984 359 : break;
7985 :
7986 102 : case ASSERTION_STMT:
7987 102 : case PRECONDITION_STMT:
7988 102 : case POSTCONDITION_STMT:
7989 102 : {
7990 102 : contract_semantic semantic = get_contract_semantic (t);
7991 102 : if (semantic == CCS_IGNORE)
7992 : break;
7993 :
7994 86 : if (!cxx_eval_assert (ctx, CONTRACT_CONDITION (t),
7995 : G_("contract predicate is false in "
7996 : "constant expression"),
7997 86 : EXPR_LOCATION (t), checked_contract_p (semantic),
7998 : non_constant_p, overflow_p))
7999 38 : *non_constant_p = true;
8000 86 : r = void_node;
8001 : }
8002 86 : break;
8003 :
8004 59779 : case TEMPLATE_ID_EXPR:
8005 59779 : {
8006 : /* We can evaluate template-id that refers to a concept only if
8007 : the template arguments are non-dependent. */
8008 59779 : tree id = unpack_concept_check (t);
8009 59779 : tree tmpl = TREE_OPERAND (id, 0);
8010 59779 : if (!concept_definition_p (tmpl))
8011 0 : internal_error ("unexpected template-id %qE", t);
8012 :
8013 59779 : if (function_concept_p (tmpl))
8014 : {
8015 0 : if (!ctx->quiet)
8016 0 : error_at (cp_expr_loc_or_input_loc (t),
8017 : "function concept must be called");
8018 0 : r = error_mark_node;
8019 0 : break;
8020 : }
8021 :
8022 59779 : if (!value_dependent_expression_p (t)
8023 59779 : && !uid_sensitive_constexpr_evaluation_p ())
8024 59756 : r = evaluate_concept_check (t);
8025 : else
8026 23 : *non_constant_p = true;
8027 :
8028 : break;
8029 : }
8030 :
8031 18 : case ASM_EXPR:
8032 18 : if (!ctx->quiet)
8033 10 : inline_asm_in_constexpr_error (loc, /*constexpr_fundef_p*/false);
8034 18 : *non_constant_p = true;
8035 18 : return t;
8036 :
8037 823 : case BIT_CAST_EXPR:
8038 823 : if (lval)
8039 : {
8040 0 : if (!ctx->quiet)
8041 0 : error_at (EXPR_LOCATION (t),
8042 : "address of a call to %qs is not a constant expression",
8043 : "__builtin_bit_cast");
8044 0 : *non_constant_p = true;
8045 0 : return t;
8046 : }
8047 823 : r = cxx_eval_bit_cast (ctx, t, non_constant_p, overflow_p);
8048 823 : break;
8049 :
8050 27 : case OMP_PARALLEL:
8051 27 : case OMP_TASK:
8052 27 : case OMP_FOR:
8053 27 : case OMP_SIMD:
8054 27 : case OMP_DISTRIBUTE:
8055 27 : case OMP_TASKLOOP:
8056 27 : case OMP_LOOP:
8057 27 : case OMP_TEAMS:
8058 27 : case OMP_TARGET_DATA:
8059 27 : case OMP_TARGET:
8060 27 : case OMP_SECTIONS:
8061 27 : case OMP_ORDERED:
8062 27 : case OMP_CRITICAL:
8063 27 : case OMP_SINGLE:
8064 27 : case OMP_SCAN:
8065 27 : case OMP_SCOPE:
8066 27 : case OMP_SECTION:
8067 27 : case OMP_MASTER:
8068 27 : case OMP_MASKED:
8069 27 : case OMP_TASKGROUP:
8070 27 : case OMP_TARGET_UPDATE:
8071 27 : case OMP_TARGET_ENTER_DATA:
8072 27 : case OMP_TARGET_EXIT_DATA:
8073 27 : case OMP_ATOMIC:
8074 27 : case OMP_ATOMIC_READ:
8075 27 : case OMP_ATOMIC_CAPTURE_OLD:
8076 27 : case OMP_ATOMIC_CAPTURE_NEW:
8077 27 : case OMP_DEPOBJ:
8078 27 : case OACC_PARALLEL:
8079 27 : case OACC_KERNELS:
8080 27 : case OACC_SERIAL:
8081 27 : case OACC_DATA:
8082 27 : case OACC_HOST_DATA:
8083 27 : case OACC_LOOP:
8084 27 : case OACC_CACHE:
8085 27 : case OACC_DECLARE:
8086 27 : case OACC_ENTER_DATA:
8087 27 : case OACC_EXIT_DATA:
8088 27 : case OACC_UPDATE:
8089 27 : if (!ctx->quiet)
8090 9 : error_at (EXPR_LOCATION (t),
8091 : "statement is not a constant expression");
8092 27 : *non_constant_p = true;
8093 27 : break;
8094 :
8095 0 : default:
8096 0 : if (STATEMENT_CODE_P (TREE_CODE (t)))
8097 : {
8098 : /* This function doesn't know how to deal with pre-genericize
8099 : statements; this can only happen with statement-expressions,
8100 : so for now just fail. */
8101 0 : if (!ctx->quiet)
8102 0 : error_at (EXPR_LOCATION (t),
8103 : "statement is not a constant expression");
8104 : }
8105 : else
8106 0 : internal_error ("unexpected expression %qE of kind %s", t,
8107 : get_tree_code_name (TREE_CODE (t)));
8108 0 : *non_constant_p = true;
8109 0 : break;
8110 : }
8111 :
8112 336091706 : if (r == error_mark_node)
8113 3256046 : *non_constant_p = true;
8114 :
8115 336091706 : if (*non_constant_p)
8116 127405574 : return t;
8117 : else
8118 : return r;
8119 : }
8120 :
8121 : /* P0859: A function is needed for constant evaluation if it is a constexpr
8122 : function that is named by an expression ([basic.def.odr]) that is
8123 : potentially constant evaluated.
8124 :
8125 : So we need to instantiate any constexpr functions mentioned by the
8126 : expression even if the definition isn't needed for evaluating the
8127 : expression. */
8128 :
8129 : static tree
8130 219350051 : instantiate_cx_fn_r (tree *tp, int *walk_subtrees, void */*data*/)
8131 : {
8132 219350051 : if (TREE_CODE (*tp) == FUNCTION_DECL
8133 6454111 : && DECL_DECLARED_CONSTEXPR_P (*tp)
8134 6374657 : && !DECL_INITIAL (*tp)
8135 776259 : && !trivial_fn_p (*tp)
8136 776256 : && (DECL_TEMPLOID_INSTANTIATION (*tp) || DECL_DEFAULTED_FN (*tp))
8137 219350051 : && !uid_sensitive_constexpr_evaluation_p ())
8138 : {
8139 776205 : ++function_depth;
8140 776205 : if (DECL_TEMPLOID_INSTANTIATION (*tp))
8141 776201 : instantiate_decl (*tp, /*defer_ok*/false, /*expl_inst*/false);
8142 : else
8143 4 : synthesize_method (*tp);
8144 776205 : --function_depth;
8145 : }
8146 218573846 : else if (TREE_CODE (*tp) == CALL_EXPR
8147 211905760 : || TREE_CODE (*tp) == AGGR_INIT_EXPR)
8148 : {
8149 6681143 : if (EXPR_HAS_LOCATION (*tp))
8150 6668546 : input_location = EXPR_LOCATION (*tp);
8151 : }
8152 :
8153 219350051 : if (!EXPR_P (*tp))
8154 132545651 : *walk_subtrees = 0;
8155 :
8156 219350051 : return NULL_TREE;
8157 : }
8158 :
8159 : static void
8160 114257744 : instantiate_constexpr_fns (tree t)
8161 : {
8162 114257744 : location_t loc = input_location;
8163 114257744 : cp_walk_tree_without_duplicates (&t, instantiate_cx_fn_r, NULL);
8164 114257744 : input_location = loc;
8165 114257744 : }
8166 :
8167 : /* Look for heap variables in the expression *TP. */
8168 :
8169 : static tree
8170 900 : find_heap_var_refs (tree *tp, int *walk_subtrees, void */*data*/)
8171 : {
8172 900 : if (VAR_P (*tp)
8173 900 : && (DECL_NAME (*tp) == heap_uninit_identifier
8174 194 : || DECL_NAME (*tp) == heap_identifier
8175 96 : || DECL_NAME (*tp) == heap_vec_uninit_identifier
8176 96 : || DECL_NAME (*tp) == heap_vec_identifier
8177 3 : || DECL_NAME (*tp) == heap_deleted_identifier))
8178 : return *tp;
8179 :
8180 706 : if (TYPE_P (*tp))
8181 0 : *walk_subtrees = 0;
8182 : return NULL_TREE;
8183 : }
8184 :
8185 : /* Find immediate function decls in *TP if any. */
8186 :
8187 : static tree
8188 10561411 : find_immediate_fndecl (tree *tp, int */*walk_subtrees*/, void */*data*/)
8189 : {
8190 10729049 : if (TREE_CODE (*tp) == FUNCTION_DECL && DECL_IMMEDIATE_FUNCTION_P (*tp))
8191 : return *tp;
8192 10561352 : if (TREE_CODE (*tp) == PTRMEM_CST
8193 386 : && TREE_CODE (PTRMEM_CST_MEMBER (*tp)) == FUNCTION_DECL
8194 10561554 : && DECL_IMMEDIATE_FUNCTION_P (PTRMEM_CST_MEMBER (*tp)))
8195 : return PTRMEM_CST_MEMBER (*tp);
8196 : return NULL_TREE;
8197 : }
8198 :
8199 : /* T has TREE_CONSTANT set but has been deemed not a valid C++ constant
8200 : expression. Return a version of T that has TREE_CONSTANT cleared. */
8201 :
8202 : static tree
8203 82593 : mark_non_constant (tree t)
8204 : {
8205 82593 : gcc_checking_assert (TREE_CONSTANT (t));
8206 :
8207 : /* This isn't actually constant, so unset TREE_CONSTANT.
8208 : Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires
8209 : it to be set if it is invariant address, even when it is not
8210 : a valid C++ constant expression. Wrap it with a NOP_EXPR
8211 : instead. */
8212 82593 : if (EXPR_P (t) && TREE_CODE (t) != ADDR_EXPR)
8213 81914 : t = copy_node (t);
8214 679 : else if (TREE_CODE (t) == CONSTRUCTOR)
8215 235 : t = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (t), t);
8216 : else
8217 444 : t = build_nop (TREE_TYPE (t), t);
8218 82593 : TREE_CONSTANT (t) = false;
8219 82593 : return t;
8220 : }
8221 :
8222 : /* ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
8223 : STRICT has the same sense as for constant_value_1: true if we only allow
8224 : conforming C++ constant expressions, or false if we want a constant value
8225 : even if it doesn't conform.
8226 : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
8227 : per P0595 even when ALLOW_NON_CONSTANT is true.
8228 : CONSTEXPR_DTOR is true when evaluating the dtor of a constexpr variable.
8229 : OBJECT must be non-NULL in that case. */
8230 :
8231 : static tree
8232 249826830 : cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant,
8233 : bool strict = true,
8234 : mce_value manifestly_const_eval = mce_unknown,
8235 : bool constexpr_dtor = false,
8236 : tree object = NULL_TREE)
8237 : {
8238 249826830 : auto_timevar time (TV_CONSTEXPR);
8239 :
8240 249826830 : bool non_constant_p = false;
8241 249826830 : bool overflow_p = false;
8242 :
8243 249826830 : if (BRACE_ENCLOSED_INITIALIZER_P (t))
8244 : {
8245 0 : gcc_checking_assert (allow_non_constant);
8246 : return t;
8247 : }
8248 :
8249 249826830 : constexpr_global_ctx global_ctx;
8250 249826830 : constexpr_ctx ctx = { &global_ctx, NULL, NULL, NULL, NULL, NULL, NULL,
8251 : allow_non_constant, strict,
8252 249826830 : !allow_non_constant ? mce_true : manifestly_const_eval };
8253 :
8254 : /* Turn off -frounding-math for manifestly constant evaluation. */
8255 249826830 : warning_sentinel rm (flag_rounding_math,
8256 249826830 : ctx.manifestly_const_eval == mce_true);
8257 249826830 : tree type = initialized_type (t);
8258 249826830 : tree r = t;
8259 249826830 : bool is_consteval = false;
8260 249826830 : if (VOID_TYPE_P (type))
8261 : {
8262 349007 : if (constexpr_dtor)
8263 : /* Used for destructors of array elements. */
8264 47 : type = TREE_TYPE (object);
8265 : else
8266 : {
8267 348960 : if (cxx_dialect < cxx20)
8268 : return t;
8269 211813 : if (TREE_CODE (t) != CALL_EXPR && TREE_CODE (t) != AGGR_INIT_EXPR)
8270 : return t;
8271 : /* Calls to immediate functions returning void need to be
8272 : evaluated. */
8273 210976 : tree fndecl = cp_get_callee_fndecl_nofold (t);
8274 421507 : if (fndecl == NULL_TREE || !DECL_IMMEDIATE_FUNCTION_P (fndecl))
8275 : return t;
8276 : else
8277 : is_consteval = true;
8278 : }
8279 : }
8280 249477823 : else if (cxx_dialect >= cxx20
8281 13886122 : && (TREE_CODE (t) == CALL_EXPR
8282 12352760 : || TREE_CODE (t) == AGGR_INIT_EXPR
8283 12350622 : || TREE_CODE (t) == TARGET_EXPR))
8284 : {
8285 : /* For non-concept checks, determine if it is consteval. */
8286 1709300 : if (!concept_check_p (t))
8287 : {
8288 1709291 : tree x = t;
8289 1709291 : if (TREE_CODE (x) == TARGET_EXPR)
8290 173800 : x = TARGET_EXPR_INITIAL (x);
8291 1709291 : tree fndecl = cp_get_callee_fndecl_nofold (x);
8292 3292709 : if (fndecl && DECL_IMMEDIATE_FUNCTION_P (fndecl))
8293 : is_consteval = true;
8294 : }
8295 : }
8296 249477877 : if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
8297 : {
8298 : /* In C++14 an NSDMI can participate in aggregate initialization,
8299 : and can refer to the address of the object being initialized, so
8300 : we need to pass in the relevant VAR_DECL if we want to do the
8301 : evaluation in a single pass. The evaluation will dynamically
8302 : update ctx.values for the VAR_DECL. We use the same strategy
8303 : for C++11 constexpr constructors that refer to the object being
8304 : initialized. */
8305 11645561 : if (constexpr_dtor)
8306 : {
8307 47 : gcc_assert (object && VAR_P (object));
8308 47 : gcc_assert (DECL_DECLARED_CONSTEXPR_P (object));
8309 47 : gcc_assert (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object));
8310 47 : if (error_operand_p (DECL_INITIAL (object)))
8311 : return t;
8312 45 : ctx.ctor = unshare_expr (DECL_INITIAL (object));
8313 45 : TREE_READONLY (ctx.ctor) = false;
8314 : /* Temporarily force decl_really_constant_value to return false
8315 : for it, we want to use ctx.ctor for the current value instead. */
8316 45 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = false;
8317 : }
8318 : else
8319 : {
8320 11645514 : ctx.ctor = build_constructor (type, NULL);
8321 11645514 : CONSTRUCTOR_NO_CLEARING (ctx.ctor) = true;
8322 : }
8323 11645559 : if (!object)
8324 : {
8325 10764149 : if (TREE_CODE (t) == TARGET_EXPR)
8326 1649066 : object = TARGET_EXPR_SLOT (t);
8327 9115083 : else if (TREE_CODE (t) == AGGR_INIT_EXPR)
8328 211482 : object = AGGR_INIT_EXPR_SLOT (t);
8329 : }
8330 11645559 : ctx.object = object;
8331 11645559 : if (object)
8332 2741958 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
8333 : (type, TREE_TYPE (object)));
8334 2741958 : if (object && DECL_P (object))
8335 2688628 : global_ctx.put_value (object, ctx.ctor);
8336 11645559 : if (TREE_CODE (r) == TARGET_EXPR)
8337 : /* Avoid creating another CONSTRUCTOR when we expand the
8338 : TARGET_EXPR. */
8339 1671719 : r = TARGET_EXPR_INITIAL (r);
8340 : }
8341 :
8342 249477875 : auto_vec<tree, 16> cleanups;
8343 249477875 : global_ctx.cleanups = &cleanups;
8344 :
8345 249477875 : if (manifestly_const_eval == mce_true)
8346 114257744 : instantiate_constexpr_fns (r);
8347 249477875 : r = cxx_eval_constant_expression (&ctx, r, vc_prvalue,
8348 : &non_constant_p, &overflow_p);
8349 :
8350 249467091 : if (!constexpr_dtor)
8351 249467046 : verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p);
8352 : else
8353 45 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = true;
8354 :
8355 249467091 : unsigned int i;
8356 249467091 : tree cleanup;
8357 : /* Evaluate the cleanups. */
8358 498935971 : FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup)
8359 1789 : cxx_eval_constant_expression (&ctx, cleanup, vc_discard,
8360 : &non_constant_p, &overflow_p);
8361 :
8362 : /* Mutable logic is a bit tricky: we want to allow initialization of
8363 : constexpr variables with mutable members, but we can't copy those
8364 : members to another constexpr variable. */
8365 249467091 : if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_MUTABLE_POISON (r))
8366 : {
8367 100 : if (!allow_non_constant)
8368 6 : error ("%qE is not a constant expression because it refers to "
8369 : "mutable subobjects of %qT", t, type);
8370 100 : non_constant_p = true;
8371 : }
8372 :
8373 249467091 : if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_NO_CLEARING (r))
8374 : {
8375 582 : if (!allow_non_constant)
8376 11 : error ("%qE is not a constant expression because it refers to "
8377 : "an incompletely initialized variable", t);
8378 582 : TREE_CONSTANT (r) = false;
8379 582 : non_constant_p = true;
8380 : }
8381 :
8382 168983513 : if (!non_constant_p && cxx_dialect >= cxx20
8383 418450604 : && !global_ctx.heap_vars.is_empty ())
8384 : {
8385 348 : tree heap_var = cp_walk_tree_without_duplicates (&r, find_heap_var_refs,
8386 : NULL);
8387 348 : unsigned int i;
8388 348 : if (heap_var)
8389 : {
8390 194 : if (!allow_non_constant && !non_constant_p)
8391 2 : error_at (DECL_SOURCE_LOCATION (heap_var),
8392 : "%qE is not a constant expression because it refers to "
8393 : "a result of %<operator new%>", t);
8394 194 : r = t;
8395 194 : non_constant_p = true;
8396 : }
8397 911 : FOR_EACH_VEC_ELT (global_ctx.heap_vars, i, heap_var)
8398 : {
8399 563 : if (DECL_NAME (heap_var) != heap_deleted_identifier)
8400 : {
8401 212 : if (!allow_non_constant && !non_constant_p)
8402 3 : error_at (DECL_SOURCE_LOCATION (heap_var),
8403 : "%qE is not a constant expression because allocated "
8404 : "storage has not been deallocated", t);
8405 212 : r = t;
8406 212 : non_constant_p = true;
8407 : }
8408 563 : varpool_node::get (heap_var)->remove ();
8409 : }
8410 : }
8411 :
8412 : /* Check that immediate invocation does not return an expression referencing
8413 : any immediate function decls. */
8414 249467091 : if (!non_constant_p && cxx_dialect >= cxx20)
8415 19153234 : if (tree immediate_fndecl
8416 9576617 : = cp_walk_tree_without_duplicates (&r, find_immediate_fndecl,
8417 : NULL))
8418 : {
8419 87 : if (!allow_non_constant && !non_constant_p)
8420 : {
8421 23 : if (is_consteval)
8422 11 : error_at (cp_expr_loc_or_input_loc (t),
8423 : "immediate evaluation returns address of immediate "
8424 : "function %qD", immediate_fndecl);
8425 : else
8426 18 : error_at (cp_expr_loc_or_input_loc (t),
8427 : "constant evaluation returns address of immediate "
8428 : "function %qD", immediate_fndecl);
8429 : }
8430 87 : r = t;
8431 87 : non_constant_p = true;
8432 : }
8433 :
8434 249467091 : if (non_constant_p)
8435 : /* If we saw something bad, go back to our argument. The wrapping below is
8436 : only for the cases of TREE_CONSTANT argument or overflow. */
8437 80483880 : r = t;
8438 :
8439 249467091 : if (!non_constant_p && overflow_p)
8440 295 : non_constant_p = true;
8441 :
8442 : /* Unshare the result. */
8443 249467091 : bool should_unshare = true;
8444 249467091 : if (r == t || (TREE_CODE (t) == TARGET_EXPR
8445 646191 : && TARGET_EXPR_INITIAL (t) == r))
8446 : should_unshare = false;
8447 :
8448 249467091 : if (non_constant_p && !allow_non_constant)
8449 1405 : return error_mark_node;
8450 249465686 : else if (constexpr_dtor)
8451 : return r;
8452 249465647 : else if (non_constant_p && TREE_CONSTANT (r))
8453 78464 : r = mark_non_constant (r);
8454 249387183 : else if (non_constant_p)
8455 : return t;
8456 :
8457 169061341 : if (should_unshare)
8458 89556116 : r = unshare_expr (r);
8459 :
8460 169061341 : if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
8461 : {
8462 2431653 : r = adjust_temp_type (type, r);
8463 2431653 : if (TREE_CODE (t) == TARGET_EXPR
8464 2431653 : && TARGET_EXPR_INITIAL (t) == r)
8465 : return t;
8466 1871191 : else if (TREE_CODE (t) == CONSTRUCTOR || TREE_CODE (t) == CALL_EXPR)
8467 : /* Don't add a TARGET_EXPR if our argument didn't have one. */;
8468 325039 : else if (TREE_CODE (t) == TARGET_EXPR && TARGET_EXPR_CLEANUP (t))
8469 15 : r = get_target_expr (r);
8470 : else
8471 : {
8472 325024 : r = get_target_expr (r, tf_warning_or_error | tf_no_cleanup);
8473 325024 : TREE_CONSTANT (r) = true;
8474 : }
8475 : }
8476 :
8477 168500879 : if (TREE_CODE (t) == TARGET_EXPR
8478 85729 : && TREE_CODE (r) == TARGET_EXPR)
8479 : {
8480 : /* Preserve this flag for potential_constant_expression, and the others
8481 : for good measure. */
8482 85655 : TARGET_EXPR_ELIDING_P (r) = TARGET_EXPR_ELIDING_P (t);
8483 85655 : TARGET_EXPR_IMPLICIT_P (r) = TARGET_EXPR_IMPLICIT_P (t);
8484 85655 : TARGET_EXPR_LIST_INIT_P (r) = TARGET_EXPR_LIST_INIT_P (t);
8485 85655 : TARGET_EXPR_DIRECT_INIT_P (r) = TARGET_EXPR_DIRECT_INIT_P (t);
8486 : }
8487 :
8488 : /* Remember the original location if that wouldn't need a wrapper. */
8489 168500879 : if (location_t loc = EXPR_LOCATION (t))
8490 73658150 : protected_set_expr_location (r, loc);
8491 :
8492 168500879 : return r;
8493 499632092 : }
8494 :
8495 : /* If T represents a constant expression returns its reduced value.
8496 : Otherwise return error_mark_node. */
8497 :
8498 : tree
8499 75354109 : cxx_constant_value (tree t, tree decl /* = NULL_TREE */,
8500 : tsubst_flags_t complain /* = tf_error */)
8501 : {
8502 75354109 : bool sfinae = !(complain & tf_error);
8503 75354109 : tree r = cxx_eval_outermost_constant_expr (t, sfinae, true, mce_true, false, decl);
8504 75354109 : if (sfinae && !TREE_CONSTANT (r))
8505 26 : r = error_mark_node;
8506 75354109 : return r;
8507 : }
8508 :
8509 : /* Like cxx_constant_value, but used for evaluation of constexpr destructors
8510 : of constexpr variables. The actual initializer of DECL is not modified. */
8511 :
8512 : void
8513 47 : cxx_constant_dtor (tree t, tree decl)
8514 : {
8515 47 : cxx_eval_outermost_constant_expr (t, false, true, mce_true, true, decl);
8516 47 : }
8517 :
8518 : /* Helper routine for fold_simple function. Either return simplified
8519 : expression T, otherwise NULL_TREE.
8520 : In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold
8521 : even if we are within template-declaration. So be careful on call, as in
8522 : such case types can be undefined. */
8523 :
8524 : static tree
8525 57171496 : fold_simple_1 (tree t)
8526 : {
8527 57171496 : tree op1;
8528 57171496 : enum tree_code code = TREE_CODE (t);
8529 :
8530 57171496 : switch (code)
8531 : {
8532 : case INTEGER_CST:
8533 : case REAL_CST:
8534 : case VECTOR_CST:
8535 : case FIXED_CST:
8536 : case COMPLEX_CST:
8537 : return t;
8538 :
8539 502698 : case SIZEOF_EXPR:
8540 502698 : return fold_sizeof_expr (t);
8541 :
8542 17593904 : case ABS_EXPR:
8543 17593904 : case ABSU_EXPR:
8544 17593904 : case CONJ_EXPR:
8545 17593904 : case REALPART_EXPR:
8546 17593904 : case IMAGPART_EXPR:
8547 17593904 : case NEGATE_EXPR:
8548 17593904 : case BIT_NOT_EXPR:
8549 17593904 : case TRUTH_NOT_EXPR:
8550 17593904 : case VIEW_CONVERT_EXPR:
8551 17593904 : CASE_CONVERT:
8552 17593904 : case FLOAT_EXPR:
8553 17593904 : case FIX_TRUNC_EXPR:
8554 17593904 : case FIXED_CONVERT_EXPR:
8555 17593904 : case ADDR_SPACE_CONVERT_EXPR:
8556 :
8557 17593904 : op1 = TREE_OPERAND (t, 0);
8558 :
8559 17593904 : t = const_unop (code, TREE_TYPE (t), op1);
8560 17593904 : if (!t)
8561 : return NULL_TREE;
8562 :
8563 857515 : if (CONVERT_EXPR_CODE_P (code)
8564 857515 : && TREE_OVERFLOW_P (t) && !TREE_OVERFLOW_P (op1))
8565 0 : TREE_OVERFLOW (t) = false;
8566 : return t;
8567 :
8568 : default:
8569 : return NULL_TREE;
8570 : }
8571 : }
8572 :
8573 : /* If T is a simple constant expression, returns its simplified value.
8574 : Otherwise returns T. In contrast to maybe_constant_value we
8575 : simplify only few operations on constant-expressions, and we don't
8576 : try to simplify constexpressions. */
8577 :
8578 : tree
8579 57503160 : fold_simple (tree t)
8580 : {
8581 57503160 : if (processing_template_decl)
8582 : return t;
8583 :
8584 57171496 : tree r = fold_simple_1 (t);
8585 57171496 : if (r)
8586 : return r;
8587 :
8588 : return t;
8589 : }
8590 :
8591 : /* Try folding the expression T to a simple constant.
8592 : Returns that constant, otherwise returns T. */
8593 :
8594 : tree
8595 567350 : fold_to_constant (tree t)
8596 : {
8597 567350 : tree r = fold (t);
8598 567350 : if (CONSTANT_CLASS_P (r) && !TREE_OVERFLOW (r))
8599 : return r;
8600 : else
8601 212443 : return t;
8602 : }
8603 :
8604 : /* If T is a constant expression, returns its reduced value.
8605 : Otherwise, if T does not have TREE_CONSTANT set, returns T.
8606 : Otherwise, returns a version of T without TREE_CONSTANT.
8607 : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated
8608 : as per P0595. */
8609 :
8610 : static GTY((deletable)) hash_map<tree, tree> *cv_cache;
8611 :
8612 : tree
8613 289954662 : maybe_constant_value (tree t, tree decl /* = NULL_TREE */,
8614 : mce_value manifestly_const_eval /* = mce_unknown */)
8615 : {
8616 289954662 : tree r;
8617 :
8618 289954662 : if (!is_nondependent_constant_expression (t))
8619 : {
8620 0 : if (TREE_OVERFLOW_P (t)
8621 79038072 : || (!processing_template_decl && TREE_CONSTANT (t)))
8622 4129 : t = mark_non_constant (t);
8623 79038072 : return t;
8624 : }
8625 210916590 : else if (CONSTANT_CLASS_P (t))
8626 : /* No caching or evaluation needed. */
8627 : return t;
8628 :
8629 : /* Don't constant evaluate an unevaluated non-manifestly-constant operand,
8630 : but at least try folding it to a simple constant. */
8631 119520396 : if (cp_unevaluated_operand && manifestly_const_eval != mce_true)
8632 318904 : return fold_to_constant (t);
8633 :
8634 118881029 : if (manifestly_const_eval != mce_unknown)
8635 47142457 : return cxx_eval_outermost_constant_expr (t, true, true,
8636 47131673 : manifestly_const_eval, false, decl);
8637 :
8638 72059035 : if (cv_cache == NULL)
8639 121868 : cv_cache = hash_map<tree, tree>::create_ggc (101);
8640 72059035 : if (tree *cached = cv_cache->get (t))
8641 : {
8642 5891303 : r = *cached;
8643 5891303 : if (r != t)
8644 : {
8645 : /* Clear processing_template_decl for sake of break_out_target_exprs;
8646 : entries in the cv_cache are non-templated. */
8647 2279051 : processing_template_decl_sentinel ptds;
8648 :
8649 2279051 : r = break_out_target_exprs (r, /*clear_loc*/true);
8650 2279051 : protected_set_expr_location (r, EXPR_LOCATION (t));
8651 2279051 : }
8652 5891303 : return r;
8653 : }
8654 :
8655 66167732 : uid_sensitive_constexpr_evaluation_checker c;
8656 66167732 : r = cxx_eval_outermost_constant_expr (t, true, true,
8657 : manifestly_const_eval, false, decl);
8658 66167732 : gcc_checking_assert (r == t
8659 : || CONVERT_EXPR_P (t)
8660 : || TREE_CODE (t) == VIEW_CONVERT_EXPR
8661 : || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
8662 : || !cp_tree_equal (r, t));
8663 66167732 : if (!c.evaluation_restricted_p ())
8664 65980095 : cv_cache->put (t, r);
8665 : return r;
8666 : }
8667 :
8668 : /* Dispose of the whole CV_CACHE. */
8669 :
8670 : static void
8671 13098869 : clear_cv_cache (void)
8672 : {
8673 13098869 : if (cv_cache != NULL)
8674 12904510 : cv_cache->empty ();
8675 13098869 : }
8676 :
8677 : /* Dispose of the whole CV_CACHE and FOLD_CACHE. */
8678 :
8679 : void
8680 13098869 : clear_cv_and_fold_caches ()
8681 : {
8682 13098869 : clear_cv_cache ();
8683 13098869 : clear_fold_cache ();
8684 13098869 : }
8685 :
8686 : /* Internal function handling expressions in templates for
8687 : fold_non_dependent_expr and fold_non_dependent_init.
8688 :
8689 : If we're in a template, but T isn't value dependent, simplify
8690 : it. We're supposed to treat:
8691 :
8692 : template <typename T> void f(T[1 + 1]);
8693 : template <typename T> void f(T[2]);
8694 :
8695 : as two declarations of the same function, for example. */
8696 :
8697 : static tree
8698 157735473 : fold_non_dependent_expr_template (tree t, tsubst_flags_t complain,
8699 : bool manifestly_const_eval,
8700 : tree object)
8701 : {
8702 157735473 : gcc_assert (processing_template_decl);
8703 :
8704 157735473 : if (is_nondependent_constant_expression (t))
8705 : {
8706 58876642 : processing_template_decl_sentinel s;
8707 58876642 : t = instantiate_non_dependent_expr_internal (t, complain);
8708 :
8709 58876642 : if (type_unknown_p (t) || BRACE_ENCLOSED_INITIALIZER_P (t))
8710 : {
8711 11435 : if (TREE_OVERFLOW_P (t))
8712 : {
8713 0 : t = build_nop (TREE_TYPE (t), t);
8714 0 : TREE_CONSTANT (t) = false;
8715 : }
8716 11435 : return t;
8717 : }
8718 58865207 : else if (CONSTANT_CLASS_P (t))
8719 : /* No evaluation needed. */
8720 : return t;
8721 :
8722 : /* Don't constant evaluate an unevaluated non-manifestly-constant operand,
8723 : but at least try folding it to a simple constant. */
8724 50749313 : if (cp_unevaluated_operand && !manifestly_const_eval)
8725 246421 : return fold_to_constant (t);
8726 :
8727 50502892 : tree r = cxx_eval_outermost_constant_expr (t, true, true,
8728 : mce_value (manifestly_const_eval),
8729 : false, object);
8730 : /* cp_tree_equal looks through NOPs, so allow them. */
8731 50502892 : gcc_checking_assert (r == t
8732 : || CONVERT_EXPR_P (t)
8733 : || TREE_CODE (t) == VIEW_CONVERT_EXPR
8734 : || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
8735 : || !cp_tree_equal (r, t));
8736 50502892 : return r;
8737 58876642 : }
8738 98858831 : else if (TREE_OVERFLOW_P (t))
8739 : {
8740 0 : t = build_nop (TREE_TYPE (t), t);
8741 0 : TREE_CONSTANT (t) = false;
8742 : }
8743 :
8744 : return t;
8745 : }
8746 :
8747 : /* Like maybe_constant_value but first fully instantiate the argument.
8748 :
8749 : Note: this is equivalent to instantiate_non_dependent_expr (t, complain)
8750 : followed by maybe_constant_value but is more efficient,
8751 : because it calls instantiation_dependent_expression_p and
8752 : potential_constant_expression at most once.
8753 : The manifestly_const_eval argument is passed to maybe_constant_value.
8754 :
8755 : Callers should generally pass their active complain, or if they are in a
8756 : non-template, diagnosing context, they can use the default of
8757 : tf_warning_or_error. Callers that might be within a template context, don't
8758 : have a complain parameter, and aren't going to remember the result for long
8759 : (e.g. null_ptr_cst_p), can pass tf_none and deal with error_mark_node
8760 : appropriately. */
8761 :
8762 : tree
8763 186411496 : fold_non_dependent_expr (tree t,
8764 : tsubst_flags_t complain /* = tf_warning_or_error */,
8765 : bool manifestly_const_eval /* = false */,
8766 : tree object /* = NULL_TREE */)
8767 : {
8768 186411496 : if (t == NULL_TREE)
8769 : return NULL_TREE;
8770 :
8771 186206329 : if (processing_template_decl)
8772 157039501 : return fold_non_dependent_expr_template (t, complain,
8773 157039501 : manifestly_const_eval, object);
8774 :
8775 29166828 : return maybe_constant_value (t, object, mce_value (manifestly_const_eval));
8776 : }
8777 :
8778 : /* Like fold_non_dependent_expr, but if EXPR couldn't be folded to a constant,
8779 : return the original expression. */
8780 :
8781 : tree
8782 1121269 : maybe_fold_non_dependent_expr (tree expr,
8783 : tsubst_flags_t complain/*=tf_warning_or_error*/)
8784 : {
8785 1121269 : tree t = fold_non_dependent_expr (expr, complain);
8786 1121269 : if (t && TREE_CONSTANT (t))
8787 510194 : return t;
8788 :
8789 : return expr;
8790 : }
8791 :
8792 : /* Like maybe_constant_init but first fully instantiate the argument. */
8793 :
8794 : tree
8795 6563979 : fold_non_dependent_init (tree t,
8796 : tsubst_flags_t complain /*=tf_warning_or_error*/,
8797 : bool manifestly_const_eval /*=false*/,
8798 : tree object /* = NULL_TREE */)
8799 : {
8800 6563979 : if (t == NULL_TREE)
8801 : return NULL_TREE;
8802 :
8803 6563979 : if (processing_template_decl)
8804 : {
8805 695972 : t = fold_non_dependent_expr_template (t, complain,
8806 : manifestly_const_eval, object);
8807 : /* maybe_constant_init does this stripping, so do it here too. */
8808 695972 : if (TREE_CODE (t) == TARGET_EXPR)
8809 : {
8810 44 : tree init = TARGET_EXPR_INITIAL (t);
8811 44 : if (TREE_CODE (init) == CONSTRUCTOR)
8812 695972 : t = init;
8813 : }
8814 695972 : return t;
8815 : }
8816 :
8817 5868007 : return maybe_constant_init (t, object, manifestly_const_eval);
8818 : }
8819 :
8820 : /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
8821 : than wrapped in a TARGET_EXPR.
8822 : ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
8823 : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
8824 : per P0595 even when ALLOW_NON_CONSTANT is true. */
8825 :
8826 : static tree
8827 15192788 : maybe_constant_init_1 (tree t, tree decl, bool allow_non_constant,
8828 : bool manifestly_const_eval)
8829 : {
8830 15192788 : if (!t)
8831 : return t;
8832 15192788 : if (TREE_CODE (t) == EXPR_STMT)
8833 19913 : t = TREE_OPERAND (t, 0);
8834 15192788 : if (TREE_CODE (t) == CONVERT_EXPR
8835 15192788 : && VOID_TYPE_P (TREE_TYPE (t)))
8836 19906 : t = TREE_OPERAND (t, 0);
8837 15192788 : if (TREE_CODE (t) == INIT_EXPR)
8838 20620 : t = TREE_OPERAND (t, 1);
8839 15192788 : if (TREE_CODE (t) == TARGET_EXPR)
8840 246269 : t = TARGET_EXPR_INITIAL (t);
8841 15192788 : if (!is_nondependent_static_init_expression (t))
8842 : /* Don't try to evaluate it. */;
8843 14089502 : else if (CONSTANT_CLASS_P (t) && TREE_CODE (t) != PTRMEM_CST)
8844 : /* No evaluation needed. PTRMEM_CST needs the immediate fn check. */;
8845 : else
8846 : {
8847 : /* [basic.start.static] allows constant-initialization of variables with
8848 : static or thread storage duration even if it isn't required, but we
8849 : shouldn't bend the rules the same way for automatic variables. */
8850 1267653 : bool is_static = (decl && DECL_P (decl)
8851 7157356 : && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)));
8852 : if (is_static)
8853 : manifestly_const_eval = true;
8854 :
8855 5943033 : if (cp_unevaluated_operand && !manifestly_const_eval)
8856 2025 : return fold_to_constant (t);
8857 :
8858 5941008 : t = cxx_eval_outermost_constant_expr (t, allow_non_constant, !is_static,
8859 : mce_value (manifestly_const_eval),
8860 : false, decl);
8861 : }
8862 15190763 : if (TREE_CODE (t) == TARGET_EXPR)
8863 : {
8864 211240 : tree init = TARGET_EXPR_INITIAL (t);
8865 211240 : if (TREE_CODE (init) == CONSTRUCTOR)
8866 15192788 : t = init;
8867 : }
8868 : return t;
8869 : }
8870 :
8871 : /* Wrapper for maybe_constant_init_1 which permits non constants. */
8872 :
8873 : tree
8874 15191814 : maybe_constant_init (tree t, tree decl, bool manifestly_const_eval)
8875 : {
8876 15191814 : return maybe_constant_init_1 (t, decl, true, manifestly_const_eval);
8877 : }
8878 :
8879 : /* Wrapper for maybe_constant_init_1 which does not permit non constants. */
8880 :
8881 : tree
8882 974 : cxx_constant_init (tree t, tree decl)
8883 : {
8884 974 : return maybe_constant_init_1 (t, decl, false, true);
8885 : }
8886 :
8887 : #if 0
8888 : /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
8889 : /* Return true if the object referred to by REF has automatic or thread
8890 : local storage. */
8891 :
8892 : enum { ck_ok, ck_bad, ck_unknown };
8893 : static int
8894 : check_automatic_or_tls (tree ref)
8895 : {
8896 : machine_mode mode;
8897 : poly_int64 bitsize, bitpos;
8898 : tree offset;
8899 : int volatilep = 0, unsignedp = 0;
8900 : tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset,
8901 : &mode, &unsignedp, &volatilep, false);
8902 : duration_kind dk;
8903 :
8904 : /* If there isn't a decl in the middle, we don't know the linkage here,
8905 : and this isn't a constant expression anyway. */
8906 : if (!DECL_P (decl))
8907 : return ck_unknown;
8908 : dk = decl_storage_duration (decl);
8909 : return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok;
8910 : }
8911 : #endif
8912 :
8913 : /* Data structure for passing data from potential_constant_expression_1
8914 : to check_for_return_continue via cp_walk_tree. */
8915 : struct check_for_return_continue_data {
8916 : hash_set<tree> *pset;
8917 : tree continue_stmt;
8918 : tree break_stmt;
8919 : };
8920 :
8921 : /* Helper function for potential_constant_expression_1 SWITCH_STMT handling,
8922 : called through cp_walk_tree. Return the first RETURN_EXPR found, or note
8923 : the first CONTINUE_STMT and/or BREAK_STMT if RETURN_EXPR is not found. */
8924 : static tree
8925 11708490 : check_for_return_continue (tree *tp, int *walk_subtrees, void *data)
8926 : {
8927 11708490 : tree t = *tp, s, b;
8928 11708490 : check_for_return_continue_data *d = (check_for_return_continue_data *) data;
8929 11708490 : switch (TREE_CODE (t))
8930 : {
8931 : case RETURN_EXPR:
8932 : return t;
8933 :
8934 30 : case CONTINUE_STMT:
8935 30 : if (d->continue_stmt == NULL_TREE)
8936 30 : d->continue_stmt = t;
8937 : break;
8938 :
8939 74 : case BREAK_STMT:
8940 74 : if (d->break_stmt == NULL_TREE)
8941 50 : d->break_stmt = t;
8942 : break;
8943 :
8944 : #define RECUR(x) \
8945 : if (tree r = cp_walk_tree (&x, check_for_return_continue, data, \
8946 : d->pset)) \
8947 : return r
8948 :
8949 : /* For loops, walk subtrees manually, so that continue stmts found
8950 : inside of the bodies of the loops are ignored. */
8951 576 : case DO_STMT:
8952 576 : *walk_subtrees = 0;
8953 576 : RECUR (DO_COND (t));
8954 576 : s = d->continue_stmt;
8955 576 : b = d->break_stmt;
8956 576 : RECUR (DO_BODY (t));
8957 576 : d->continue_stmt = s;
8958 576 : d->break_stmt = b;
8959 576 : break;
8960 :
8961 81 : case WHILE_STMT:
8962 81 : *walk_subtrees = 0;
8963 81 : RECUR (WHILE_COND (t));
8964 81 : s = d->continue_stmt;
8965 81 : b = d->break_stmt;
8966 81 : RECUR (WHILE_BODY (t));
8967 79 : d->continue_stmt = s;
8968 79 : d->break_stmt = b;
8969 79 : break;
8970 :
8971 82 : case FOR_STMT:
8972 82 : *walk_subtrees = 0;
8973 82 : RECUR (FOR_INIT_STMT (t));
8974 82 : RECUR (FOR_COND (t));
8975 82 : RECUR (FOR_EXPR (t));
8976 82 : s = d->continue_stmt;
8977 82 : b = d->break_stmt;
8978 82 : RECUR (FOR_BODY (t));
8979 63 : d->continue_stmt = s;
8980 63 : d->break_stmt = b;
8981 63 : break;
8982 :
8983 0 : case RANGE_FOR_STMT:
8984 0 : *walk_subtrees = 0;
8985 0 : RECUR (RANGE_FOR_EXPR (t));
8986 0 : s = d->continue_stmt;
8987 0 : b = d->break_stmt;
8988 0 : RECUR (RANGE_FOR_BODY (t));
8989 0 : d->continue_stmt = s;
8990 0 : d->break_stmt = b;
8991 0 : break;
8992 :
8993 6 : case SWITCH_STMT:
8994 6 : *walk_subtrees = 0;
8995 6 : RECUR (SWITCH_STMT_COND (t));
8996 6 : b = d->break_stmt;
8997 6 : RECUR (SWITCH_STMT_BODY (t));
8998 0 : d->break_stmt = b;
8999 0 : break;
9000 : #undef RECUR
9001 :
9002 : case STATEMENT_LIST:
9003 : case CONSTRUCTOR:
9004 : break;
9005 :
9006 11359588 : default:
9007 11359588 : if (!EXPR_P (t))
9008 4063743 : *walk_subtrees = 0;
9009 : break;
9010 : }
9011 :
9012 : return NULL_TREE;
9013 : }
9014 :
9015 : /* Return true if T denotes a potentially constant expression. Issue
9016 : diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
9017 : an lvalue-rvalue conversion is implied. If NOW is true, we want to
9018 : consider the expression in the current context, independent of constexpr
9019 : substitution. If FUNDEF_P is true, we're checking a constexpr function body
9020 : and hard errors should not be reported by constexpr_error.
9021 :
9022 : C++0x [expr.const] used to say
9023 :
9024 : 6 An expression is a potential constant expression if it is
9025 : a constant expression where all occurrences of function
9026 : parameters are replaced by arbitrary constant expressions
9027 : of the appropriate type.
9028 :
9029 : 2 A conditional expression is a constant expression unless it
9030 : involves one of the following as a potentially evaluated
9031 : subexpression (3.2), but subexpressions of logical AND (5.14),
9032 : logical OR (5.15), and conditional (5.16) operations that are
9033 : not evaluated are not considered. */
9034 :
9035 : static bool
9036 2148329510 : potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
9037 : bool fundef_p, tsubst_flags_t flags,
9038 : tree *jump_target)
9039 : {
9040 : #define RECUR(T,RV) \
9041 : potential_constant_expression_1 ((T), (RV), strict, now, fundef_p, flags, \
9042 : jump_target)
9043 :
9044 2148329510 : enum { any = false, rval = true };
9045 2148329510 : int i;
9046 2148329510 : tree tmp;
9047 :
9048 2148329510 : if (t == error_mark_node)
9049 : return false;
9050 2148315916 : if (t == NULL_TREE)
9051 : return true;
9052 2145059981 : location_t loc = cp_expr_loc_or_input_loc (t);
9053 :
9054 2145059981 : if (*jump_target)
9055 : /* If we are jumping, ignore everything. This is simpler than the
9056 : cxx_eval_constant_expression handling because we only need to be
9057 : conservatively correct, and we don't necessarily have a constant value
9058 : available, so we don't bother with switch tracking. */
9059 : return true;
9060 :
9061 2143993462 : if (TREE_THIS_VOLATILE (t) && want_rval)
9062 : {
9063 2849801 : if (flags & tf_error)
9064 22 : constexpr_error (loc, fundef_p, "lvalue-to-rvalue conversion of "
9065 : "a volatile lvalue %qE with type %qT", t,
9066 22 : TREE_TYPE (t));
9067 2849801 : return false;
9068 : }
9069 2141143661 : if (CONSTANT_CLASS_P (t))
9070 : return true;
9071 1743264132 : if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED)
9072 1743264132 : && TREE_TYPE (t) == error_mark_node)
9073 : return false;
9074 :
9075 1743264082 : switch (TREE_CODE (t))
9076 : {
9077 : case FUNCTION_DECL:
9078 : case BASELINK:
9079 : case TEMPLATE_DECL:
9080 : case OVERLOAD:
9081 : case TEMPLATE_ID_EXPR:
9082 : case LABEL_DECL:
9083 : case CASE_LABEL_EXPR:
9084 : case PREDICT_EXPR:
9085 : case CONST_DECL:
9086 : case SIZEOF_EXPR:
9087 : case ALIGNOF_EXPR:
9088 : case OFFSETOF_EXPR:
9089 : case NOEXCEPT_EXPR:
9090 : case TEMPLATE_PARM_INDEX:
9091 : case TRAIT_EXPR:
9092 : case IDENTIFIER_NODE:
9093 : case USERDEF_LITERAL:
9094 : /* We can see a FIELD_DECL in a pointer-to-member expression. */
9095 : case FIELD_DECL:
9096 : case RESULT_DECL:
9097 : case USING_DECL:
9098 : case USING_STMT:
9099 : case PLACEHOLDER_EXPR:
9100 : case REQUIRES_EXPR:
9101 : case STATIC_ASSERT:
9102 : case DEBUG_BEGIN_STMT:
9103 : return true;
9104 :
9105 7049519 : case RETURN_EXPR:
9106 7049519 : if (!RECUR (TREE_OPERAND (t, 0), any))
9107 : return false;
9108 : /* FALLTHROUGH */
9109 :
9110 7009560 : case BREAK_STMT:
9111 7009560 : case CONTINUE_STMT:
9112 7009560 : *jump_target = t;
9113 7009560 : return true;
9114 :
9115 181144747 : case PARM_DECL:
9116 181144747 : if (now && want_rval)
9117 : {
9118 86864509 : tree type = TREE_TYPE (t);
9119 63441641 : if ((processing_template_decl && !COMPLETE_TYPE_P (type))
9120 76749411 : || dependent_type_p (type)
9121 117752853 : || is_really_empty_class (type, /*ignore_vptr*/false))
9122 : /* An empty class has no data to read. */
9123 55976538 : return true;
9124 30887971 : if (flags & tf_error)
9125 10 : constexpr_error (input_location, fundef_p,
9126 : "%qE is not a constant expression", t);
9127 30887971 : return false;
9128 : }
9129 : return true;
9130 :
9131 113566009 : case AGGR_INIT_EXPR:
9132 113566009 : case CALL_EXPR:
9133 : /* -- an invocation of a function other than a constexpr function
9134 : or a constexpr constructor. */
9135 113566009 : {
9136 113566009 : tree fun = get_function_named_in_call (t);
9137 113566009 : const int nargs = call_expr_nargs (t);
9138 113566009 : i = 0;
9139 :
9140 113566009 : if (fun == NULL_TREE)
9141 : {
9142 : /* Reset to allow the function to continue past the end
9143 : of the block below. Otherwise return early. */
9144 18620 : bool bail = true;
9145 :
9146 18620 : if (TREE_CODE (t) == CALL_EXPR
9147 18620 : && CALL_EXPR_FN (t) == NULL_TREE)
9148 18620 : switch (CALL_EXPR_IFN (t))
9149 : {
9150 : /* These should be ignored, they are optimized away from
9151 : constexpr functions. */
9152 : case IFN_UBSAN_NULL:
9153 : case IFN_UBSAN_BOUNDS:
9154 : case IFN_UBSAN_VPTR:
9155 : case IFN_FALLTHROUGH:
9156 : case IFN_ASSUME:
9157 : return true;
9158 :
9159 : case IFN_ADD_OVERFLOW:
9160 : case IFN_SUB_OVERFLOW:
9161 : case IFN_MUL_OVERFLOW:
9162 : case IFN_LAUNDER:
9163 : case IFN_VEC_CONVERT:
9164 : bail = false;
9165 : break;
9166 :
9167 : default:
9168 : break;
9169 : }
9170 :
9171 : if (bail)
9172 : {
9173 : /* fold_call_expr can't do anything with IFN calls. */
9174 8 : if (flags & tf_error)
9175 0 : constexpr_error (loc, fundef_p,
9176 : "call to internal function %qE", t);
9177 8 : return false;
9178 : }
9179 : }
9180 :
9181 113547389 : if (fun && is_overloaded_fn (fun))
9182 : {
9183 99071925 : if (!RECUR (fun, true))
9184 : return false;
9185 94578730 : fun = get_fns (fun);
9186 :
9187 94578730 : if (TREE_CODE (fun) == FUNCTION_DECL)
9188 : {
9189 74271675 : if (builtin_valid_in_constant_expr_p (fun))
9190 : return true;
9191 73996917 : if (!maybe_constexpr_fn (fun)
9192 : /* Allow any built-in function; if the expansion
9193 : isn't constant, we'll deal with that then. */
9194 28234515 : && !fndecl_built_in_p (fun)
9195 : /* In C++20, replaceable global allocation functions
9196 : are constant expressions. */
9197 19839107 : && (!cxx_replaceable_global_alloc_fn (fun)
9198 21246 : || TREE_CODE (t) != CALL_EXPR
9199 21246 : || (!CALL_FROM_NEW_OR_DELETE_P (t)
9200 19524 : && (current_function_decl == NULL_TREE
9201 19524 : || !is_std_allocator_allocate
9202 19524 : (current_function_decl))))
9203 : /* Allow placement new in std::construct_at. */
9204 19825767 : && (!cxx_placement_new_fn (fun)
9205 7798 : || TREE_CODE (t) != CALL_EXPR
9206 7798 : || current_function_decl == NULL_TREE
9207 7792 : || !is_std_construct_at (current_function_decl))
9208 93815943 : && !cxx_dynamic_cast_fn_p (fun))
9209 : {
9210 19818190 : if ((flags & tf_error)
9211 19818190 : && constexpr_error (loc, fundef_p,
9212 : "call to non-%<constexpr%> "
9213 : "function %qD", fun))
9214 121 : explain_invalid_constexpr_fn (fun);
9215 19818190 : return false;
9216 : }
9217 : }
9218 :
9219 74485782 : fun = OVL_FIRST (fun);
9220 : /* Skip initial arguments to base constructors. */
9221 74485782 : if (DECL_BASE_CONSTRUCTOR_P (fun))
9222 1214834 : i = num_artificial_parms_for (fun);
9223 : }
9224 14475464 : else if (fun)
9225 : {
9226 28950928 : if (RECUR (fun, FUNCTION_POINTER_TYPE_P (fun) ? rval : any))
9227 : /* Might end up being a constant function pointer. But it
9228 : could also be a function object with constexpr op(), so
9229 : we pass 'any' so that the underlying VAR_DECL is deemed
9230 : as potentially-constant even though it wasn't declared
9231 : constexpr. */;
9232 : else
9233 : return false;
9234 : }
9235 155037088 : for (; i < nargs; ++i)
9236 : {
9237 78319054 : tree x = get_nth_callarg (t, i);
9238 : /* In a template, reference arguments haven't been converted to
9239 : REFERENCE_TYPE and we might not even know if the parameter
9240 : is a reference, so accept lvalue constants too. */
9241 78319054 : bool rv = processing_template_decl ? any : rval;
9242 : /* Don't require an immediately constant value, as constexpr
9243 : substitution might not use the value of the argument. */
9244 78319054 : bool sub_now = false;
9245 78319054 : if (!potential_constant_expression_1 (x, rv, strict,
9246 : sub_now, fundef_p, flags,
9247 : jump_target))
9248 : return false;
9249 : }
9250 : return true;
9251 : }
9252 :
9253 92481769 : case NON_LVALUE_EXPR:
9254 : /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
9255 : -- an lvalue of integral type that refers to a non-volatile
9256 : const variable or static data member initialized with
9257 : constant expressions, or
9258 :
9259 : -- an lvalue of literal type that refers to non-volatile
9260 : object defined with constexpr, or that refers to a
9261 : sub-object of such an object; */
9262 92481769 : return RECUR (TREE_OPERAND (t, 0), rval);
9263 :
9264 2545 : case EXCESS_PRECISION_EXPR:
9265 2545 : return RECUR (TREE_OPERAND (t, 0), rval);
9266 :
9267 212328364 : case VAR_DECL:
9268 212328364 : if (DECL_HAS_VALUE_EXPR_P (t))
9269 : {
9270 1109239 : if (now && is_normal_capture_proxy (t))
9271 : {
9272 : /* -- in a lambda-expression, a reference to this or to a
9273 : variable with automatic storage duration defined outside that
9274 : lambda-expression, where the reference would be an
9275 : odr-use. */
9276 :
9277 232922 : if (want_rval)
9278 : /* Since we're doing an lvalue-rvalue conversion, this might
9279 : not be an odr-use, so evaluate the variable directly. */
9280 135696 : return RECUR (DECL_CAPTURED_VARIABLE (t), rval);
9281 :
9282 97226 : if (flags & tf_error)
9283 : {
9284 2 : tree cap = DECL_CAPTURED_VARIABLE (t);
9285 2 : if (constexpr_error (input_location, fundef_p,
9286 : "lambda capture of %qE is not a "
9287 : "constant expression", cap)
9288 2 : && decl_constant_var_p (cap))
9289 2 : inform (input_location, "because it is used as a glvalue");
9290 : }
9291 97226 : return false;
9292 : }
9293 : /* Treat __PRETTY_FUNCTION__ inside a template function as
9294 : potentially-constant. */
9295 1751390 : else if (DECL_PRETTY_FUNCTION_P (t)
9296 929280 : && DECL_VALUE_EXPR (t) == error_mark_node)
9297 : return true;
9298 876309 : return RECUR (DECL_VALUE_EXPR (t), rval);
9299 : }
9300 211219125 : if (want_rval
9301 119287192 : && !var_in_maybe_constexpr_fn (t)
9302 111911783 : && !type_dependent_expression_p (t)
9303 82444044 : && !decl_maybe_constant_var_p (t)
9304 36899379 : && (strict
9305 144484 : || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t))
9306 104477 : || (DECL_INITIAL (t)
9307 104214 : && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t)))
9308 36898731 : && COMPLETE_TYPE_P (TREE_TYPE (t))
9309 248104147 : && !is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
9310 : {
9311 36874513 : if (flags & tf_error)
9312 117 : non_const_var_error (loc, t, fundef_p);
9313 36874513 : return false;
9314 : }
9315 : return true;
9316 :
9317 125429497 : case NOP_EXPR:
9318 125429497 : if (REINTERPRET_CAST_P (t))
9319 : {
9320 41204 : if (flags & tf_error)
9321 42 : constexpr_error (loc, fundef_p, "%<reinterpret_cast%> is not a "
9322 : "constant expression");
9323 41204 : return false;
9324 : }
9325 : /* FALLTHRU */
9326 413860302 : case CONVERT_EXPR:
9327 413860302 : case VIEW_CONVERT_EXPR:
9328 : /* -- a reinterpret_cast. FIXME not implemented, and this rule
9329 : may change to something more specific to type-punning (DR 1312). */
9330 413860302 : {
9331 413860302 : tree from = TREE_OPERAND (t, 0);
9332 413860302 : if (location_wrapper_p (t))
9333 : {
9334 270998838 : iloc_sentinel ils = loc;
9335 270998838 : return (RECUR (from, want_rval));
9336 270998838 : }
9337 142861464 : if (INDIRECT_TYPE_P (TREE_TYPE (t)))
9338 : {
9339 68033244 : STRIP_ANY_LOCATION_WRAPPER (from);
9340 68033244 : if (TREE_CODE (from) == INTEGER_CST
9341 68033244 : && !integer_zerop (from))
9342 : {
9343 1363 : if (flags & tf_error)
9344 21 : constexpr_error (loc, fundef_p,
9345 : "%<reinterpret_cast%> from integer to "
9346 : "pointer");
9347 1363 : return false;
9348 : }
9349 : }
9350 142860101 : return (RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR));
9351 : }
9352 :
9353 8526 : case ADDRESSOF_EXPR:
9354 : /* This is like ADDR_EXPR, except it won't form pointer-to-member. */
9355 8526 : t = TREE_OPERAND (t, 0);
9356 8526 : goto handle_addr_expr;
9357 :
9358 15710909 : case ADDR_EXPR:
9359 : /* -- a unary operator & that is applied to an lvalue that
9360 : designates an object with thread or automatic storage
9361 : duration; */
9362 15710909 : t = TREE_OPERAND (t, 0);
9363 :
9364 15710909 : if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t))
9365 : /* A pointer-to-member constant. */
9366 : return true;
9367 :
9368 15719079 : handle_addr_expr:
9369 : #if 0
9370 : /* FIXME adjust when issue 1197 is fully resolved. For now don't do
9371 : any checking here, as we might dereference the pointer later. If
9372 : we remove this code, also remove check_automatic_or_tls. */
9373 : i = check_automatic_or_tls (t);
9374 : if (i == ck_ok)
9375 : return true;
9376 : if (i == ck_bad)
9377 : {
9378 : if (flags & tf_error)
9379 : error ("address-of an object %qE with thread local or "
9380 : "automatic storage is not a constant expression", t);
9381 : return false;
9382 : }
9383 : #endif
9384 15719079 : return RECUR (t, any);
9385 :
9386 70697374 : case COMPONENT_REF:
9387 70697374 : case ARROW_EXPR:
9388 70697374 : case OFFSET_REF:
9389 : /* -- a class member access unless its postfix-expression is
9390 : of literal type or of pointer to literal type. */
9391 : /* This test would be redundant, as it follows from the
9392 : postfix-expression being a potential constant expression. */
9393 70697374 : if (type_unknown_p (t))
9394 : return true;
9395 65712146 : if (is_overloaded_fn (t))
9396 : /* In a template, a COMPONENT_REF of a function expresses ob.fn(),
9397 : which uses ob as an lvalue. */
9398 66166085 : want_rval = false;
9399 66166085 : gcc_fallthrough ();
9400 :
9401 66166085 : case REALPART_EXPR:
9402 66166085 : case IMAGPART_EXPR:
9403 66166085 : case BIT_FIELD_REF:
9404 66166085 : return RECUR (TREE_OPERAND (t, 0), want_rval);
9405 :
9406 145487 : case EXPR_PACK_EXPANSION:
9407 145487 : return RECUR (PACK_EXPANSION_PATTERN (t), want_rval);
9408 :
9409 79403810 : case INDIRECT_REF:
9410 79403810 : {
9411 79403810 : tree x = TREE_OPERAND (t, 0);
9412 79403810 : STRIP_NOPS (x);
9413 79403810 : if (is_this_parameter (x) && !is_capture_proxy (x))
9414 : {
9415 36727030 : if (!var_in_maybe_constexpr_fn (x))
9416 : {
9417 33239560 : if (flags & tf_error)
9418 9 : constexpr_error (loc, fundef_p, "use of %<this%> in a "
9419 : "constant expression");
9420 33239560 : return false;
9421 : }
9422 : return true;
9423 : }
9424 42676780 : return RECUR (x, rval);
9425 : }
9426 :
9427 6613284 : case STATEMENT_LIST:
9428 21541115 : for (tree stmt : tsi_range (t))
9429 14977119 : if (!RECUR (stmt, any))
9430 228718237 : return false;
9431 : return true;
9432 :
9433 1444991 : case MODIFY_EXPR:
9434 1444991 : if (cxx_dialect < cxx14)
9435 1993 : goto fail;
9436 1442998 : if (!RECUR (TREE_OPERAND (t, 0), any))
9437 : return false;
9438 : /* Just ignore clobbers. */
9439 1117032 : if (TREE_CLOBBER_P (TREE_OPERAND (t, 1)))
9440 : return true;
9441 381407 : if (!RECUR (TREE_OPERAND (t, 1), rval))
9442 : return false;
9443 : return true;
9444 :
9445 727866 : case MODOP_EXPR:
9446 727866 : if (cxx_dialect < cxx14)
9447 2925 : goto fail;
9448 724941 : if (!RECUR (TREE_OPERAND (t, 0), rval))
9449 : return false;
9450 312954 : if (!RECUR (TREE_OPERAND (t, 2), rval))
9451 : return false;
9452 : return true;
9453 :
9454 115462 : case DO_STMT:
9455 115462 : if (!RECUR (DO_COND (t), rval))
9456 : return false;
9457 115462 : if (!RECUR (DO_BODY (t), any))
9458 : return false;
9459 115441 : if (breaks (jump_target) || continues (jump_target))
9460 3 : *jump_target = NULL_TREE;
9461 : return true;
9462 :
9463 101627 : case FOR_STMT:
9464 101627 : if (!RECUR (FOR_INIT_STMT (t), any))
9465 : return false;
9466 101627 : tmp = FOR_COND (t);
9467 101627 : if (!RECUR (tmp, rval))
9468 : return false;
9469 101583 : if (tmp)
9470 : {
9471 69961 : if (!processing_template_decl)
9472 69908 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
9473 : /* If we couldn't evaluate the condition, it might not ever be
9474 : true. */
9475 69961 : if (!integer_onep (tmp))
9476 : {
9477 : /* Before returning true, check if the for body can contain
9478 : a return. */
9479 69961 : hash_set<tree> pset;
9480 69961 : check_for_return_continue_data data = { &pset, NULL_TREE,
9481 69961 : NULL_TREE };
9482 69961 : if (tree ret_expr
9483 69961 : = cp_walk_tree (&FOR_BODY (t), check_for_return_continue,
9484 : &data, &pset))
9485 59498 : *jump_target = ret_expr;
9486 69961 : return true;
9487 69961 : }
9488 : }
9489 31622 : if (!RECUR (FOR_EXPR (t), any))
9490 : return false;
9491 31622 : if (!RECUR (FOR_BODY (t), any))
9492 : return false;
9493 31622 : if (breaks (jump_target) || continues (jump_target))
9494 3 : *jump_target = NULL_TREE;
9495 : return true;
9496 :
9497 20 : case RANGE_FOR_STMT:
9498 20 : if (!RECUR (RANGE_FOR_INIT_STMT (t), any))
9499 : return false;
9500 20 : if (!RECUR (RANGE_FOR_EXPR (t), any))
9501 : return false;
9502 20 : if (!RECUR (RANGE_FOR_BODY (t), any))
9503 : return false;
9504 14 : if (breaks (jump_target) || continues (jump_target))
9505 0 : *jump_target = NULL_TREE;
9506 : return true;
9507 :
9508 34973 : case WHILE_STMT:
9509 34973 : tmp = WHILE_COND (t);
9510 34973 : if (!RECUR (tmp, rval))
9511 : return false;
9512 34930 : if (!processing_template_decl)
9513 34914 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
9514 : /* If we couldn't evaluate the condition, it might not ever be true. */
9515 34930 : if (!integer_onep (tmp))
9516 : {
9517 : /* Before returning true, check if the while body can contain
9518 : a return. */
9519 34876 : hash_set<tree> pset;
9520 34876 : check_for_return_continue_data data = { &pset, NULL_TREE,
9521 34876 : NULL_TREE };
9522 34876 : if (tree ret_expr
9523 34876 : = cp_walk_tree (&WHILE_BODY (t), check_for_return_continue,
9524 : &data, &pset))
9525 49 : *jump_target = ret_expr;
9526 34876 : return true;
9527 34876 : }
9528 54 : if (!RECUR (WHILE_BODY (t), any))
9529 : return false;
9530 52 : if (breaks (jump_target) || continues (jump_target))
9531 11 : *jump_target = NULL_TREE;
9532 : return true;
9533 :
9534 627 : case SWITCH_STMT:
9535 627 : if (!RECUR (SWITCH_STMT_COND (t), rval))
9536 : return false;
9537 : /* FIXME we don't check SWITCH_STMT_BODY currently, because even
9538 : unreachable labels would be checked and it is enough if there is
9539 : a single switch cond value for which it is a valid constant
9540 : expression. We need to check if there are any RETURN_EXPRs
9541 : or CONTINUE_STMTs inside of the body though, as in that case
9542 : we need to set *jump_target. */
9543 : else
9544 : {
9545 621 : hash_set<tree> pset;
9546 621 : check_for_return_continue_data data = { &pset, NULL_TREE,
9547 621 : NULL_TREE };
9548 621 : if (tree ret_expr
9549 621 : = cp_walk_tree (&SWITCH_STMT_BODY (t), check_for_return_continue,
9550 : &data, &pset))
9551 : /* The switch might return. */
9552 582 : *jump_target = ret_expr;
9553 39 : else if (data.continue_stmt)
9554 : /* The switch can't return, but might continue. */
9555 3 : *jump_target = data.continue_stmt;
9556 621 : }
9557 621 : return true;
9558 :
9559 292 : case STMT_EXPR:
9560 292 : return RECUR (STMT_EXPR_STMT (t), rval);
9561 :
9562 85269 : case LAMBDA_EXPR:
9563 85269 : if (cxx_dialect >= cxx17)
9564 : /* In C++17 lambdas can be constexpr, don't give up yet. */
9565 : return true;
9566 401 : else if (flags & tf_error)
9567 0 : constexpr_error (loc, fundef_p, "lambda-expression is not a "
9568 : "constant expression before C++17");
9569 : return false;
9570 :
9571 521717 : case NEW_EXPR:
9572 521717 : case VEC_NEW_EXPR:
9573 521717 : case DELETE_EXPR:
9574 521717 : case VEC_DELETE_EXPR:
9575 521717 : if (cxx_dialect >= cxx20)
9576 : /* In C++20, new-expressions are potentially constant. */
9577 : return true;
9578 504190 : else if (flags & tf_error)
9579 0 : constexpr_error (loc, fundef_p, "new-expression is not a "
9580 : "constant expression before C++20");
9581 : return false;
9582 :
9583 933701 : case DYNAMIC_CAST_EXPR:
9584 933701 : case PSEUDO_DTOR_EXPR:
9585 933701 : case THROW_EXPR:
9586 933701 : case OMP_PARALLEL:
9587 933701 : case OMP_TASK:
9588 933701 : case OMP_FOR:
9589 933701 : case OMP_SIMD:
9590 933701 : case OMP_DISTRIBUTE:
9591 933701 : case OMP_TASKLOOP:
9592 933701 : case OMP_LOOP:
9593 933701 : case OMP_TEAMS:
9594 933701 : case OMP_TARGET_DATA:
9595 933701 : case OMP_TARGET:
9596 933701 : case OMP_SECTIONS:
9597 933701 : case OMP_ORDERED:
9598 933701 : case OMP_CRITICAL:
9599 933701 : case OMP_SINGLE:
9600 933701 : case OMP_SCAN:
9601 933701 : case OMP_SCOPE:
9602 933701 : case OMP_SECTION:
9603 933701 : case OMP_MASTER:
9604 933701 : case OMP_MASKED:
9605 933701 : case OMP_TASKGROUP:
9606 933701 : case OMP_TARGET_UPDATE:
9607 933701 : case OMP_TARGET_ENTER_DATA:
9608 933701 : case OMP_TARGET_EXIT_DATA:
9609 933701 : case OMP_ATOMIC:
9610 933701 : case OMP_ATOMIC_READ:
9611 933701 : case OMP_ATOMIC_CAPTURE_OLD:
9612 933701 : case OMP_ATOMIC_CAPTURE_NEW:
9613 933701 : case OMP_DEPOBJ:
9614 933701 : case OACC_PARALLEL:
9615 933701 : case OACC_KERNELS:
9616 933701 : case OACC_SERIAL:
9617 933701 : case OACC_DATA:
9618 933701 : case OACC_HOST_DATA:
9619 933701 : case OACC_LOOP:
9620 933701 : case OACC_CACHE:
9621 933701 : case OACC_DECLARE:
9622 933701 : case OACC_ENTER_DATA:
9623 933701 : case OACC_EXIT_DATA:
9624 933701 : case OACC_UPDATE:
9625 : /* GCC internal stuff. */
9626 933701 : case VA_ARG_EXPR:
9627 933701 : case TRANSACTION_EXPR:
9628 933701 : case AT_ENCODE_EXPR:
9629 933701 : fail:
9630 933701 : if (flags & tf_error)
9631 37 : constexpr_error (loc, fundef_p, "expression %qE is not a constant "
9632 : "expression", t);
9633 : return false;
9634 :
9635 661 : case ASM_EXPR:
9636 661 : if (flags & tf_error)
9637 1 : inline_asm_in_constexpr_error (loc, fundef_p);
9638 : return false;
9639 :
9640 192996 : case OBJ_TYPE_REF:
9641 192996 : if (cxx_dialect >= cxx20)
9642 : /* In C++20 virtual calls can be constexpr, don't give up yet. */
9643 : return true;
9644 181869 : else if (flags & tf_error)
9645 0 : constexpr_error (loc, fundef_p, "virtual functions cannot be "
9646 : "%<constexpr%> before C++20");
9647 : return false;
9648 :
9649 116176 : case TYPEID_EXPR:
9650 : /* In C++20, a typeid expression whose operand is of polymorphic
9651 : class type can be constexpr. */
9652 116176 : {
9653 116176 : tree e = TREE_OPERAND (t, 0);
9654 116176 : if (cxx_dialect < cxx20
9655 113578 : && strict
9656 113578 : && !TYPE_P (e)
9657 2956 : && !type_dependent_expression_p (e)
9658 116195 : && TYPE_POLYMORPHIC_P (TREE_TYPE (e)))
9659 : {
9660 7 : if (flags & tf_error)
9661 1 : constexpr_error (loc, fundef_p, "%<typeid%> is not a "
9662 : "constant expression because %qE is "
9663 : "of polymorphic type", e);
9664 7 : return false;
9665 : }
9666 : return true;
9667 : }
9668 :
9669 20350409 : case POINTER_DIFF_EXPR:
9670 20350409 : case MINUS_EXPR:
9671 20350409 : want_rval = true;
9672 20350409 : goto binary;
9673 :
9674 41371190 : case LT_EXPR:
9675 41371190 : case LE_EXPR:
9676 41371190 : case GT_EXPR:
9677 41371190 : case GE_EXPR:
9678 41371190 : case EQ_EXPR:
9679 41371190 : case NE_EXPR:
9680 41371190 : case SPACESHIP_EXPR:
9681 41371190 : want_rval = true;
9682 41371190 : goto binary;
9683 :
9684 3857393 : case PREINCREMENT_EXPR:
9685 3857393 : case POSTINCREMENT_EXPR:
9686 3857393 : case PREDECREMENT_EXPR:
9687 3857393 : case POSTDECREMENT_EXPR:
9688 3857393 : if (cxx_dialect < cxx14)
9689 16361 : goto fail;
9690 3841032 : goto unary;
9691 :
9692 1113857 : case BIT_NOT_EXPR:
9693 : /* A destructor. */
9694 1113857 : if (TYPE_P (TREE_OPERAND (t, 0)))
9695 : return true;
9696 : /* fall through. */
9697 :
9698 40641088 : case CONJ_EXPR:
9699 40641088 : case SAVE_EXPR:
9700 40641088 : case FIX_TRUNC_EXPR:
9701 40641088 : case FLOAT_EXPR:
9702 40641088 : case NEGATE_EXPR:
9703 40641088 : case ABS_EXPR:
9704 40641088 : case ABSU_EXPR:
9705 40641088 : case TRUTH_NOT_EXPR:
9706 40641088 : case FIXED_CONVERT_EXPR:
9707 40641088 : case UNARY_PLUS_EXPR:
9708 40641088 : case UNARY_LEFT_FOLD_EXPR:
9709 40641088 : case UNARY_RIGHT_FOLD_EXPR:
9710 1113851 : unary:
9711 40641088 : return RECUR (TREE_OPERAND (t, 0), rval);
9712 :
9713 29178500 : case CAST_EXPR:
9714 29178500 : case CONST_CAST_EXPR:
9715 29178500 : case STATIC_CAST_EXPR:
9716 29178500 : case REINTERPRET_CAST_EXPR:
9717 29178500 : case IMPLICIT_CONV_EXPR:
9718 29178500 : if (!cast_valid_in_integral_constant_expression_p (TREE_TYPE (t)))
9719 : /* In C++98, a conversion to non-integral type can't be part of a
9720 : constant expression. */
9721 : {
9722 215 : if (flags & tf_error)
9723 0 : constexpr_error (loc, fundef_p,
9724 : "cast to non-integral type %qT in a constant "
9725 0 : "expression", TREE_TYPE (t));
9726 215 : return false;
9727 : }
9728 : /* This might be a conversion from a class to a (potentially) literal
9729 : type. Let's consider it potentially constant since the conversion
9730 : might be a constexpr user-defined conversion. */
9731 29178285 : else if (cxx_dialect >= cxx11
9732 29165467 : && (dependent_type_p (TREE_TYPE (t))
9733 11519885 : || !COMPLETE_TYPE_P (TREE_TYPE (t))
9734 11481081 : || literal_type_p (TREE_TYPE (t)))
9735 58088005 : && TREE_OPERAND (t, 0))
9736 : {
9737 27381182 : tree type = TREE_TYPE (TREE_OPERAND (t, 0));
9738 : /* If this is a dependent type, it could end up being a class
9739 : with conversions. */
9740 27381182 : if (type == NULL_TREE || WILDCARD_TYPE_P (type))
9741 : return true;
9742 : /* Or a non-dependent class which has conversions. */
9743 235492 : else if (CLASS_TYPE_P (type)
9744 235492 : && (TYPE_HAS_CONVERSION (type) || dependent_scope_p (type)))
9745 146635 : return true;
9746 : }
9747 :
9748 4795049 : return (RECUR (TREE_OPERAND (t, 0),
9749 4795049 : !TYPE_REF_P (TREE_TYPE (t))));
9750 :
9751 2320732 : case BIND_EXPR:
9752 2320732 : return RECUR (BIND_EXPR_BODY (t), want_rval);
9753 :
9754 886938 : case NON_DEPENDENT_EXPR:
9755 : /* Treat NON_DEPENDENT_EXPR as non-constant: it's not handled by
9756 : constexpr evaluation or tsubst, so fold_non_dependent_expr can't
9757 : do anything useful with it. And we shouldn't see it in a context
9758 : where a constant expression is strictly required, hence the assert. */
9759 886938 : gcc_checking_assert (!(flags & tf_error));
9760 : return false;
9761 :
9762 15289708 : case CLEANUP_POINT_EXPR:
9763 15289708 : case MUST_NOT_THROW_EXPR:
9764 15289708 : case TRY_CATCH_EXPR:
9765 15289708 : case TRY_BLOCK:
9766 15289708 : case EH_SPEC_BLOCK:
9767 15289708 : case EXPR_STMT:
9768 15289708 : case PAREN_EXPR:
9769 : /* For convenience. */
9770 15289708 : case LOOP_EXPR:
9771 15289708 : case EXIT_EXPR:
9772 15289708 : return RECUR (TREE_OPERAND (t, 0), want_rval);
9773 :
9774 1082068 : case DECL_EXPR:
9775 1082068 : tmp = DECL_EXPR_DECL (t);
9776 1064445 : if (VAR_P (tmp) && !DECL_ARTIFICIAL (tmp)
9777 1861020 : && (processing_template_decl
9778 644956 : ? !decl_maybe_constant_var_p (tmp)
9779 510960 : : !decl_constant_var_p (tmp)))
9780 : {
9781 478568 : if (CP_DECL_THREAD_LOCAL_P (tmp) && !DECL_REALLY_EXTERN (tmp))
9782 : {
9783 5 : if (flags & tf_error)
9784 2 : constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p,
9785 : "%qD defined %<thread_local%> in "
9786 : "%<constexpr%> context", tmp);
9787 5 : return false;
9788 : }
9789 478563 : else if (TREE_STATIC (tmp))
9790 : {
9791 64 : if (flags & tf_error)
9792 9 : constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p,
9793 : "%qD defined %<static%> in %<constexpr%> "
9794 : "context", tmp);
9795 64 : return false;
9796 : }
9797 478499 : else if (!check_for_uninitialized_const_var
9798 478499 : (tmp, /*constexpr_context_p=*/true, flags))
9799 : return false;
9800 : }
9801 1078205 : if (VAR_P (tmp))
9802 1060582 : return RECUR (DECL_INITIAL (tmp), want_rval);
9803 : return true;
9804 :
9805 98 : case TRY_FINALLY_EXPR:
9806 98 : return (RECUR (TREE_OPERAND (t, 0), want_rval)
9807 98 : && RECUR (TREE_OPERAND (t, 1), any));
9808 :
9809 27611742 : case SCOPE_REF:
9810 27611742 : return RECUR (TREE_OPERAND (t, 1), want_rval);
9811 :
9812 7949539 : case TARGET_EXPR:
9813 7949539 : if (!TARGET_EXPR_DIRECT_INIT_P (t)
9814 7945764 : && !TARGET_EXPR_ELIDING_P (t)
9815 14798977 : && !literal_type_p (TREE_TYPE (t)))
9816 : {
9817 1089091 : if (flags & tf_error)
9818 : {
9819 18 : auto_diagnostic_group d;
9820 18 : if (constexpr_error (loc, fundef_p,
9821 : "temporary of non-literal type %qT in a "
9822 18 : "constant expression", TREE_TYPE (t)))
9823 18 : explain_non_literal_class (TREE_TYPE (t));
9824 18 : }
9825 1089091 : return false;
9826 : }
9827 : /* FALLTHRU */
9828 14541283 : case INIT_EXPR:
9829 14541283 : return RECUR (TREE_OPERAND (t, 1), rval);
9830 :
9831 10113300 : case CONSTRUCTOR:
9832 10113300 : {
9833 10113300 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
9834 10113300 : constructor_elt *ce;
9835 18290626 : for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
9836 8446042 : if (!RECUR (ce->value, want_rval))
9837 : return false;
9838 : return true;
9839 : }
9840 :
9841 400955 : case TREE_LIST:
9842 400955 : {
9843 400955 : gcc_assert (TREE_PURPOSE (t) == NULL_TREE
9844 : || DECL_P (TREE_PURPOSE (t)));
9845 400955 : if (!RECUR (TREE_VALUE (t), want_rval))
9846 : return false;
9847 319423 : if (TREE_CHAIN (t) == NULL_TREE)
9848 : return true;
9849 119145 : return RECUR (TREE_CHAIN (t), want_rval);
9850 : }
9851 :
9852 8056228 : case TRUNC_DIV_EXPR:
9853 8056228 : case CEIL_DIV_EXPR:
9854 8056228 : case FLOOR_DIV_EXPR:
9855 8056228 : case ROUND_DIV_EXPR:
9856 8056228 : case TRUNC_MOD_EXPR:
9857 8056228 : case CEIL_MOD_EXPR:
9858 8056228 : case ROUND_MOD_EXPR:
9859 8056228 : {
9860 8056228 : tree denom = TREE_OPERAND (t, 1);
9861 8056228 : if (!RECUR (denom, rval))
9862 : return false;
9863 : /* We can't call cxx_eval_outermost_constant_expr on an expression
9864 : that hasn't been through instantiate_non_dependent_expr yet. */
9865 7526701 : if (!processing_template_decl)
9866 2070692 : denom = cxx_eval_outermost_constant_expr (denom, true);
9867 7526701 : if (integer_zerop (denom))
9868 : {
9869 472 : if (flags & tf_error)
9870 50 : constexpr_error (input_location, fundef_p,
9871 : "division by zero is not a constant expression");
9872 472 : return false;
9873 : }
9874 : else
9875 : {
9876 7526229 : want_rval = true;
9877 7526229 : return RECUR (TREE_OPERAND (t, 0), want_rval);
9878 : }
9879 : }
9880 :
9881 2461585 : case COMPOUND_EXPR:
9882 2461585 : {
9883 : /* check_return_expr sometimes wraps a TARGET_EXPR in a
9884 : COMPOUND_EXPR; don't get confused. */
9885 2461585 : tree op0 = TREE_OPERAND (t, 0);
9886 2461585 : tree op1 = TREE_OPERAND (t, 1);
9887 2461585 : STRIP_NOPS (op1);
9888 2461585 : if (TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
9889 182165 : return RECUR (op0, want_rval);
9890 : else
9891 2279420 : goto binary;
9892 : }
9893 :
9894 : /* If the first operand is the non-short-circuit constant, look at
9895 : the second operand; otherwise we only care about the first one for
9896 : potentiality. */
9897 13967797 : case TRUTH_AND_EXPR:
9898 13967797 : case TRUTH_ANDIF_EXPR:
9899 13967797 : tmp = boolean_true_node;
9900 13967797 : goto truth;
9901 4771102 : case TRUTH_OR_EXPR:
9902 4771102 : case TRUTH_ORIF_EXPR:
9903 4771102 : tmp = boolean_false_node;
9904 18738899 : truth:
9905 18738899 : {
9906 18738899 : tree op0 = TREE_OPERAND (t, 0);
9907 18738899 : tree op1 = TREE_OPERAND (t, 1);
9908 18738899 : if (!RECUR (op0, rval))
9909 : return false;
9910 11819210 : if (!(flags & tf_error) && RECUR (op1, rval))
9911 : /* When quiet, try to avoid expensive trial evaluation by first
9912 : checking potentiality of the second operand. */
9913 : return true;
9914 627157 : if (!processing_template_decl)
9915 433469 : op0 = cxx_eval_outermost_constant_expr (op0, true);
9916 627157 : if (tree_int_cst_equal (op0, tmp))
9917 5685 : return (flags & tf_error) ? RECUR (op1, rval) : false;
9918 : else
9919 : return true;
9920 : }
9921 :
9922 : case PLUS_EXPR:
9923 : case MULT_EXPR:
9924 : case POINTER_PLUS_EXPR:
9925 : case RDIV_EXPR:
9926 : case EXACT_DIV_EXPR:
9927 : case MIN_EXPR:
9928 : case MAX_EXPR:
9929 : case LSHIFT_EXPR:
9930 : case RSHIFT_EXPR:
9931 : case LROTATE_EXPR:
9932 : case RROTATE_EXPR:
9933 : case BIT_IOR_EXPR:
9934 : case BIT_XOR_EXPR:
9935 : case BIT_AND_EXPR:
9936 : case TRUTH_XOR_EXPR:
9937 : case UNORDERED_EXPR:
9938 : case ORDERED_EXPR:
9939 : case UNLT_EXPR:
9940 : case UNLE_EXPR:
9941 : case UNGT_EXPR:
9942 : case UNGE_EXPR:
9943 : case UNEQ_EXPR:
9944 : case LTGT_EXPR:
9945 : case RANGE_EXPR:
9946 : case COMPLEX_EXPR:
9947 151914047 : want_rval = true;
9948 : /* Fall through. */
9949 151914047 : case ARRAY_REF:
9950 151914047 : case ARRAY_RANGE_REF:
9951 151914047 : case MEMBER_REF:
9952 151914047 : case DOTSTAR_EXPR:
9953 151914047 : case MEM_REF:
9954 151914047 : case BINARY_LEFT_FOLD_EXPR:
9955 151914047 : case BINARY_RIGHT_FOLD_EXPR:
9956 151914047 : binary:
9957 315318174 : for (i = 0; i < 2; ++i)
9958 238113355 : if (!RECUR (TREE_OPERAND (t, i), want_rval))
9959 : return false;
9960 : return true;
9961 :
9962 : case VEC_PERM_EXPR:
9963 93241 : for (i = 0; i < 3; ++i)
9964 93211 : if (!RECUR (TREE_OPERAND (t, i), true))
9965 : return false;
9966 : return true;
9967 :
9968 3566764 : case COND_EXPR:
9969 3566764 : if (COND_EXPR_IS_VEC_DELETE (t) && cxx_dialect < cxx20)
9970 : {
9971 8 : if (flags & tf_error)
9972 2 : constexpr_error (loc, fundef_p, "%<delete[]%> is not a "
9973 : "constant expression");
9974 8 : return false;
9975 : }
9976 : /* Fall through. */
9977 4436594 : case IF_STMT:
9978 4436594 : case VEC_COND_EXPR:
9979 : /* If the condition is a known constant, we know which of the legs we
9980 : care about; otherwise we only require that the condition and
9981 : either of the legs be potentially constant. */
9982 4436594 : tmp = TREE_OPERAND (t, 0);
9983 4436594 : if (!RECUR (tmp, rval))
9984 : return false;
9985 3420065 : if (!processing_template_decl)
9986 2109602 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
9987 : /* potential_constant_expression* isn't told if it is called for
9988 : manifestly_const_eval or not, so for consteval if always
9989 : process both branches as if the condition is not a known
9990 : constant. */
9991 3420065 : if (TREE_CODE (t) != IF_STMT || !IF_STMT_CONSTEVAL_P (t))
9992 : {
9993 3419714 : if (integer_zerop (tmp))
9994 609642 : return RECUR (TREE_OPERAND (t, 2), want_rval);
9995 2810072 : else if (TREE_CODE (tmp) == INTEGER_CST)
9996 651705 : return RECUR (TREE_OPERAND (t, 1), want_rval);
9997 : }
9998 2158718 : tmp = *jump_target;
9999 2398662 : for (i = 1; i < 3; ++i)
10000 : {
10001 2371690 : tree this_jump_target = tmp;
10002 2371690 : if (potential_constant_expression_1 (TREE_OPERAND (t, i),
10003 : want_rval, strict, now, fundef_p,
10004 : tf_none, &this_jump_target))
10005 : {
10006 2131746 : if (returns (&this_jump_target))
10007 360879 : *jump_target = this_jump_target;
10008 1770867 : else if (!returns (jump_target))
10009 : {
10010 1770867 : if (breaks (&this_jump_target)
10011 1770867 : || continues (&this_jump_target))
10012 23 : *jump_target = this_jump_target;
10013 1770867 : if (i == 1)
10014 : {
10015 : /* If the then branch is potentially constant, but
10016 : does not return, check if the else branch
10017 : couldn't return, break or continue. */
10018 1585025 : hash_set<tree> pset;
10019 1585025 : check_for_return_continue_data data = { &pset, NULL_TREE,
10020 1585025 : NULL_TREE };
10021 3170050 : if (tree ret_expr
10022 1585025 : = cp_walk_tree (&TREE_OPERAND (t, 2),
10023 : check_for_return_continue, &data,
10024 : &pset))
10025 4776 : *jump_target = ret_expr;
10026 1580249 : else if (*jump_target == NULL_TREE)
10027 : {
10028 1580229 : if (data.continue_stmt)
10029 0 : *jump_target = data.continue_stmt;
10030 1580229 : else if (data.break_stmt)
10031 5 : *jump_target = data.break_stmt;
10032 : }
10033 1585025 : }
10034 : }
10035 2131746 : return true;
10036 : }
10037 : }
10038 26972 : if (flags & tf_error)
10039 : {
10040 3 : if (TREE_CODE (t) == IF_STMT)
10041 3 : constexpr_error (loc, fundef_p, "neither branch of %<if%> is a "
10042 : "constant expression");
10043 : else
10044 0 : constexpr_error (loc, fundef_p, "expression %qE is not a "
10045 : "constant expression", t);
10046 : }
10047 : return false;
10048 :
10049 838 : case VEC_INIT_EXPR:
10050 838 : if (VEC_INIT_EXPR_IS_CONSTEXPR (t))
10051 : return true;
10052 381 : if (flags & tf_error)
10053 : {
10054 3 : if (constexpr_error (loc, fundef_p, "non-constant array "
10055 : "initialization"))
10056 3 : diagnose_non_constexpr_vec_init (t);
10057 : }
10058 : return false;
10059 :
10060 : case TYPE_DECL:
10061 : case TAG_DEFN:
10062 : /* We can see these in statement-expressions. */
10063 : return true;
10064 :
10065 96717 : case CLEANUP_STMT:
10066 96717 : if (!RECUR (CLEANUP_BODY (t), any))
10067 : return false;
10068 96671 : if (!CLEANUP_EH_ONLY (t) && !RECUR (CLEANUP_EXPR (t), any))
10069 : return false;
10070 : return true;
10071 :
10072 : case EMPTY_CLASS_EXPR:
10073 : return true;
10074 :
10075 8 : case GOTO_EXPR:
10076 8 : {
10077 8 : tree *target = &TREE_OPERAND (t, 0);
10078 : /* Gotos representing break, continue and cdtor return are OK. */
10079 8 : if (breaks (target) || continues (target) || returns (target))
10080 : {
10081 0 : *jump_target = *target;
10082 0 : return true;
10083 : }
10084 8 : if (flags & tf_error)
10085 2 : constexpr_error (loc, fundef_p, "%<goto%> is not a constant "
10086 : "expression");
10087 : return false;
10088 : }
10089 :
10090 46 : case ASSERTION_STMT:
10091 46 : case PRECONDITION_STMT:
10092 46 : case POSTCONDITION_STMT:
10093 46 : if (!checked_contract_p (get_contract_semantic (t)))
10094 : return true;
10095 33 : return RECUR (CONTRACT_CONDITION (t), rval);
10096 :
10097 51 : case LABEL_EXPR:
10098 51 : t = LABEL_EXPR_LABEL (t);
10099 51 : if (DECL_ARTIFICIAL (t) || cxx_dialect >= cxx23)
10100 : return true;
10101 50 : else if (flags & tf_error)
10102 6 : constexpr_error (loc, fundef_p, "label definition in %<constexpr%> "
10103 : "function only available with %<-std=c++2b%> or "
10104 : "%<-std=gnu++2b%>");
10105 : return false;
10106 :
10107 3 : case ANNOTATE_EXPR:
10108 3 : return RECUR (TREE_OPERAND (t, 0), rval);
10109 :
10110 6734 : case BIT_CAST_EXPR:
10111 6734 : return RECUR (TREE_OPERAND (t, 0), rval);
10112 :
10113 : /* Coroutine await, yield and return expressions are not. */
10114 : case CO_AWAIT_EXPR:
10115 : case CO_YIELD_EXPR:
10116 : case CO_RETURN_EXPR:
10117 : return false;
10118 :
10119 30 : case NONTYPE_ARGUMENT_PACK:
10120 30 : {
10121 30 : tree args = ARGUMENT_PACK_ARGS (t);
10122 30 : int len = TREE_VEC_LENGTH (args);
10123 90 : for (int i = 0; i < len; ++i)
10124 60 : if (!RECUR (TREE_VEC_ELT (args, i), any))
10125 : return false;
10126 : return true;
10127 : }
10128 :
10129 0 : default:
10130 0 : if (objc_non_constant_expr_p (t))
10131 : return false;
10132 :
10133 0 : sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t)));
10134 0 : gcc_unreachable ();
10135 : return false;
10136 : }
10137 : #undef RECUR
10138 : }
10139 :
10140 : bool
10141 911799123 : potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
10142 : bool fundef_p, tsubst_flags_t flags)
10143 : {
10144 41557234 : if (flags & tf_error)
10145 : {
10146 : /* Check potentiality quietly first, as that could be performed more
10147 : efficiently in some cases (currently only for TRUTH_*_EXPR). If
10148 : that fails, replay the check noisily to give errors. */
10149 20778617 : flags &= ~tf_error;
10150 20778617 : if (potential_constant_expression_1 (t, want_rval, strict, now, fundef_p,
10151 : flags))
10152 : return true;
10153 866 : flags |= tf_error;
10154 : }
10155 :
10156 891021372 : tree target = NULL_TREE;
10157 20779483 : return potential_constant_expression_1 (t, want_rval, strict, now, fundef_p,
10158 20779483 : flags, &target);
10159 : }
10160 :
10161 : /* The main entry point to the above. */
10162 :
10163 : bool
10164 285330291 : potential_constant_expression (tree t)
10165 : {
10166 285330291 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
10167 : /*now*/false, /*fundef_p*/false,
10168 285330291 : tf_none);
10169 : }
10170 :
10171 : /* As above, but require a constant rvalue. */
10172 :
10173 : bool
10174 9501312 : potential_rvalue_constant_expression (tree t)
10175 : {
10176 9501312 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
10177 : /*now*/false, /*fundef_p*/false,
10178 9501312 : tf_none);
10179 : }
10180 :
10181 : /* Like above, but complain about non-constant expressions. */
10182 :
10183 : bool
10184 19667085 : require_potential_constant_expression (tree t)
10185 : {
10186 19667085 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
10187 : /*now*/false, /*fundef_p*/false,
10188 19667085 : tf_warning_or_error);
10189 : }
10190 :
10191 : /* Cross product of the above. */
10192 :
10193 : bool
10194 22895 : require_potential_rvalue_constant_expression (tree t)
10195 : {
10196 22895 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
10197 : /*now*/false, /*fundef_p*/false,
10198 22895 : tf_warning_or_error);
10199 : }
10200 :
10201 : /* Like require_potential_rvalue_constant_expression, but fundef_p is true. */
10202 :
10203 : bool
10204 174 : require_potential_rvalue_constant_expression_fncheck (tree t)
10205 : {
10206 174 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
10207 : /*now*/false, /*fundef_p*/true,
10208 174 : tf_warning_or_error);
10209 : }
10210 :
10211 : /* Like above, but don't consider PARM_DECL a potential_constant_expression. */
10212 :
10213 : bool
10214 406 : require_rvalue_constant_expression (tree t)
10215 : {
10216 406 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
10217 : /*now*/true, /*fundef_p*/false,
10218 406 : tf_warning_or_error);
10219 : }
10220 :
10221 : /* Like potential_constant_expression, but don't consider possible constexpr
10222 : substitution of the current function. That is, PARM_DECL qualifies under
10223 : potential_constant_expression, but not here.
10224 :
10225 : This is basically what you can check when any actual constant values might
10226 : be value-dependent. */
10227 :
10228 : bool
10229 455162036 : is_constant_expression (tree t)
10230 : {
10231 455162036 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
10232 : /*now*/true, /*fundef_p*/false,
10233 455162036 : tf_none);
10234 : }
10235 :
10236 : /* As above, but expect an rvalue. */
10237 :
10238 : bool
10239 105055462 : is_rvalue_constant_expression (tree t)
10240 : {
10241 105055462 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
10242 : /*now*/true, /*fundef_p*/false,
10243 105055462 : tf_none);
10244 : }
10245 :
10246 : /* Like above, but complain about non-constant expressions. */
10247 :
10248 : bool
10249 1088057 : require_constant_expression (tree t)
10250 : {
10251 1088057 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
10252 : /*now*/true, /*fundef_p*/false,
10253 1088057 : tf_warning_or_error);
10254 : }
10255 :
10256 : /* Like is_constant_expression, but allow const variables that are not allowed
10257 : under constexpr rules. */
10258 :
10259 : bool
10260 15192788 : is_static_init_expression (tree t)
10261 : {
10262 15192788 : return potential_constant_expression_1 (t, /*want_rval*/false,
10263 : /*strict*/false, /*now*/true,
10264 15192788 : /*fundef_p*/false, tf_none);
10265 : }
10266 :
10267 : /* Returns true if T is a potential constant expression that is not
10268 : instantiation-dependent, and therefore a candidate for constant folding even
10269 : in a template. */
10270 :
10271 : bool
10272 447694685 : is_nondependent_constant_expression (tree t)
10273 : {
10274 447694685 : return (!type_unknown_p (t)
10275 447694193 : && is_constant_expression (t)
10276 789224459 : && !instantiation_dependent_expression_p (t));
10277 : }
10278 :
10279 : /* Returns true if T is a potential static initializer expression that is not
10280 : instantiation-dependent. */
10281 :
10282 : bool
10283 15192788 : is_nondependent_static_init_expression (tree t)
10284 : {
10285 15192788 : return (!type_unknown_p (t)
10286 15192788 : && is_static_init_expression (t)
10287 29282296 : && !instantiation_dependent_expression_p (t));
10288 : }
10289 :
10290 : /* True iff FN is an implicitly constexpr function. */
10291 :
10292 : bool
10293 48640 : decl_implicit_constexpr_p (tree fn)
10294 : {
10295 48640 : if (!(flag_implicit_constexpr
10296 0 : && TREE_CODE (fn) == FUNCTION_DECL
10297 0 : && DECL_DECLARED_CONSTEXPR_P (fn)))
10298 : return false;
10299 :
10300 0 : if (DECL_CLONED_FUNCTION_P (fn))
10301 0 : fn = DECL_CLONED_FUNCTION (fn);
10302 :
10303 0 : return (DECL_LANG_SPECIFIC (fn)
10304 0 : && DECL_LANG_SPECIFIC (fn)->u.fn.implicit_constexpr);
10305 : }
10306 :
10307 : /* Finalize constexpr processing after parsing. */
10308 :
10309 : void
10310 88316 : fini_constexpr (void)
10311 : {
10312 : /* The contexpr call and fundef copies tables are no longer needed. */
10313 88316 : constexpr_call_table = NULL;
10314 88316 : fundef_copies_table = NULL;
10315 88316 : }
10316 :
10317 : #include "gt-cp-constexpr.h"
|