Branch data 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-2025 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 : 69970776 : is_instantiation_of_constexpr (tree fun)
61 : : {
62 : 93130599 : return ((DECL_TEMPLOID_INSTANTIATION (fun)
63 : 47188771 : && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun)))
64 : 102059105 : || (DECL_DEFAULTED_FN (fun)
65 : 1535974 : && DECL_DECLARED_CONSTEXPR_P (fun)));
66 : : }
67 : :
68 : : /* Return true if T is a literal type. */
69 : :
70 : : bool
71 : 101892245 : literal_type_p (tree t)
72 : : {
73 : 99363184 : if (SCALAR_TYPE_P (t)
74 : 41310448 : || VECTOR_TYPE_P (t)
75 : 41298672 : || TYPE_REF_P (t)
76 : 135720878 : || (VOID_TYPE_P (t) && cxx_dialect >= cxx14))
77 : : return true;
78 : 31130101 : if (CLASS_TYPE_P (t))
79 : : {
80 : 29893613 : t = complete_type (t);
81 : 29893613 : gcc_assert (COMPLETE_TYPE_P (t) || errorcount);
82 : 29893613 : return CLASSTYPE_LITERAL_P (t);
83 : : }
84 : 1236488 : if (TREE_CODE (t) == ARRAY_TYPE)
85 : 1236314 : 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 : 274893075 : ensure_literal_type_for_constexpr_object (tree decl)
95 : : {
96 : 274893075 : tree type = TREE_TYPE (decl);
97 : 274893075 : if (VAR_P (decl)
98 : 96966878 : && (DECL_DECLARED_CONSTEXPR_P (decl)
99 : 76282857 : || var_in_constexpr_fn (decl))
100 : 302972922 : && !processing_template_decl)
101 : : {
102 : 18669628 : tree stype = strip_array_types (type);
103 : 18669628 : 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 : 18669622 : else if (!literal_type_p (type))
107 : : {
108 : 12462 : if (DECL_DECLARED_CONSTEXPR_P (decl))
109 : : {
110 : 42 : auto_diagnostic_group d;
111 : 42 : error_at (DECL_SOURCE_LOCATION (decl),
112 : : "the type %qT of %<constexpr%> variable %qD "
113 : : "is not literal", type, decl);
114 : 42 : explain_non_literal_class (type);
115 : 42 : decl = error_mark_node;
116 : 42 : }
117 : 12420 : else if (cxx_dialect < cxx23)
118 : : {
119 : 2535 : 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++23%> or %<-std=gnu++23%>", decl, type);
126 : 8 : explain_non_literal_class (type);
127 : 8 : decl = error_mark_node;
128 : 8 : }
129 : 2535 : cp_function_chain->invalid_constexpr = true;
130 : : }
131 : : }
132 : 18657160 : else if (DECL_DECLARED_CONSTEXPR_P (decl)
133 : 18657160 : && 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 : 274893075 : 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 : 867 : constexpr_error (location_t location, bool constexpr_fundef_p,
154 : : const char *gmsgid, ...)
155 : : {
156 : 867 : diagnostic_info diagnostic;
157 : 867 : va_list ap;
158 : 867 : rich_location richloc (line_table, location);
159 : 867 : va_start (ap, gmsgid);
160 : 867 : bool ret;
161 : 867 : if (!constexpr_fundef_p)
162 : : {
163 : : /* Report an error that cannot be suppressed. */
164 : 644 : diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_ERROR);
165 : 644 : ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
166 : : }
167 : 223 : else if (warn_invalid_constexpr)
168 : : {
169 : 179 : diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
170 : 179 : cxx_dialect < cxx23 ? DK_PEDWARN : DK_WARNING);
171 : 179 : diagnostic.option_id = OPT_Winvalid_constexpr;
172 : 179 : ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
173 : : }
174 : : else
175 : : ret = false;
176 : 867 : va_end (ap);
177 : 1734 : return ret;
178 : 867 : }
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 : 355154030 : constexpr_fundef_hasher::equal (const constexpr_fundef *lhs,
197 : : const constexpr_fundef *rhs)
198 : : {
199 : 339262978 : 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 : 349790755 : constexpr_fundef_hasher::hash (const constexpr_fundef *fundef)
207 : : {
208 : 349790755 : return DECL_UID (fundef->decl);
209 : : }
210 : :
211 : : /* Return a previously saved definition of function FUN. */
212 : :
213 : : constexpr_fundef *
214 : 39836755 : retrieve_constexpr_fundef (tree fun)
215 : : {
216 : 39836755 : if (constexpr_fundef_table == NULL)
217 : : return NULL;
218 : :
219 : 39833444 : constexpr_fundef fundef = { fun, NULL_TREE, NULL_TREE, NULL_TREE };
220 : 39833444 : 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 : 18599207 : is_valid_constexpr_fn (tree fun, bool complain)
228 : : {
229 : 18599207 : bool ret = true;
230 : :
231 : 37198414 : if (DECL_INHERITED_CTOR (fun)
232 : 2056189 : && 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 : 18599207 : for (tree parm = FUNCTION_FIRST_USER_PARM (fun);
242 : 36317174 : parm != NULL_TREE; parm = TREE_CHAIN (parm))
243 : 17717967 : if (!literal_type_p (TREE_TYPE (parm)))
244 : : {
245 : 17991 : ret = false;
246 : 17991 : if (complain)
247 : : {
248 : 22 : auto_diagnostic_group d;
249 : 22 : if (constexpr_error (input_location, /*constexpr_fundef_p*/true,
250 : : "invalid type for parameter %d of "
251 : : "%<constexpr%> function %q+#D",
252 : 22 : DECL_PARM_INDEX (parm), fun))
253 : 20 : explain_non_literal_class (TREE_TYPE (parm));
254 : 22 : }
255 : : }
256 : : }
257 : :
258 : 29787418 : if (LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun)) && cxx_dialect < cxx17)
259 : : {
260 : 2 : ret = false;
261 : 2 : if (complain)
262 : 2 : inform (DECL_SOURCE_LOCATION (fun),
263 : : "lambdas are implicitly %<constexpr%> only in C++17 and later");
264 : : }
265 : 37198410 : else if (DECL_DESTRUCTOR_P (fun) && cxx_dialect < cxx20)
266 : : {
267 : 96 : ret = false;
268 : 96 : if (complain)
269 : 0 : error_at (DECL_SOURCE_LOCATION (fun),
270 : : "%<constexpr%> destructors only available with "
271 : : "%<-std=c++20%> or %<-std=gnu++20%>");
272 : : }
273 : 18599109 : else if (!DECL_CONSTRUCTOR_P (fun) && !DECL_DESTRUCTOR_P (fun))
274 : : {
275 : 16312789 : tree rettype = TREE_TYPE (TREE_TYPE (fun));
276 : 16312789 : if (!literal_type_p (rettype))
277 : : {
278 : 56655 : ret = false;
279 : 56655 : if (complain)
280 : : {
281 : 12 : auto_diagnostic_group d;
282 : 12 : if (constexpr_error (input_location, /*constexpr_fundef_p*/true,
283 : : "invalid return type %qT of %<constexpr%> "
284 : : "function %q+D", rettype, fun))
285 : 12 : explain_non_literal_class (rettype);
286 : 12 : }
287 : : }
288 : :
289 : : /* C++14 DR 1684 removed this restriction. */
290 : 16312789 : if (cxx_dialect < cxx14
291 : 31390 : && DECL_IOBJ_MEMBER_FUNCTION_P (fun)
292 : 16313844 : && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun)))
293 : : {
294 : 1 : ret = false;
295 : 1 : if (complain)
296 : : {
297 : 1 : auto_diagnostic_group d;
298 : 1 : if (pedwarn (DECL_SOURCE_LOCATION (fun), OPT_Wpedantic,
299 : : "enclosing class of %<constexpr%> non-static"
300 : : " member function %q+#D is not a literal type",
301 : : fun))
302 : 1 : explain_non_literal_class (DECL_CONTEXT (fun));
303 : 1 : }
304 : : }
305 : : }
306 : 2286320 : else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun)) && cxx_dialect < cxx26)
307 : : {
308 : 50 : ret = false;
309 : 50 : if (complain)
310 : : {
311 : 10 : if (DECL_CONSTRUCTOR_P (fun))
312 : 8 : error ("%<constexpr%> constructor in %q#T that has "
313 : : "virtual base classes only available with "
314 : 8 : "%<-std=c++2c%> or %<-std=gnu++2c%>", DECL_CONTEXT (fun));
315 : : else
316 : 2 : error ("%<constexpr%> destructor in %q#T that has "
317 : : "virtual base classes only available with "
318 : 2 : "%<-std=c++2c%> or %<-std=gnu++2c%>", DECL_CONTEXT (fun));
319 : : }
320 : : }
321 : :
322 : 18599207 : return ret;
323 : : }
324 : :
325 : : /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF
326 : : for a member of an anonymous aggregate, INIT is the initializer for that
327 : : member, and VEC_OUTER is the vector of constructor elements for the class
328 : : whose constructor we are processing. Add the initializer to the vector
329 : : and return true to indicate success. */
330 : :
331 : : static bool
332 : 878 : build_anon_member_initialization (tree member, tree init,
333 : : vec<constructor_elt, va_gc> **vec_outer)
334 : : {
335 : : /* MEMBER presents the relevant fields from the inside out, but we need
336 : : to build up the initializer from the outside in so that we can reuse
337 : : previously built CONSTRUCTORs if this is, say, the second field in an
338 : : anonymous struct. So we use a vec as a stack. */
339 : 878 : auto_vec<tree, 2> fields;
340 : 1852 : do
341 : : {
342 : 1852 : fields.safe_push (TREE_OPERAND (member, 1));
343 : 1852 : member = TREE_OPERAND (member, 0);
344 : : }
345 : 1852 : while (ANON_AGGR_TYPE_P (TREE_TYPE (member))
346 : 3710 : && TREE_CODE (member) == COMPONENT_REF);
347 : :
348 : : /* VEC has the constructor elements vector for the context of FIELD.
349 : : If FIELD is an anonymous aggregate, we will push inside it. */
350 : : vec<constructor_elt, va_gc> **vec = vec_outer;
351 : : tree field;
352 : 1852 : while (field = fields.pop(),
353 : 1852 : ANON_AGGR_TYPE_P (TREE_TYPE (field)))
354 : : {
355 : 974 : tree ctor;
356 : : /* If there is already an outer constructor entry for the anonymous
357 : : aggregate FIELD, use it; otherwise, insert one. */
358 : 974 : if (vec_safe_is_empty (*vec)
359 : 198 : || (*vec)->last().index != field)
360 : : {
361 : 872 : ctor = build_constructor (TREE_TYPE (field), NULL);
362 : 872 : CONSTRUCTOR_APPEND_ELT (*vec, field, ctor);
363 : : }
364 : : else
365 : 102 : ctor = (*vec)->last().value;
366 : 974 : vec = &CONSTRUCTOR_ELTS (ctor);
367 : : }
368 : :
369 : : /* Now we're at the innermost field, the one that isn't an anonymous
370 : : aggregate. Add its initializer to the CONSTRUCTOR and we're done. */
371 : 878 : gcc_assert (fields.is_empty());
372 : 878 : CONSTRUCTOR_APPEND_ELT (*vec, field, init);
373 : :
374 : 878 : return true;
375 : 878 : }
376 : :
377 : : /* Subroutine of build_constexpr_constructor_member_initializers.
378 : : The expression tree T represents a data member initialization
379 : : in a (constexpr) constructor definition. Build a pairing of
380 : : the data member with its initializer, and prepend that pair
381 : : to the existing initialization pair INITS. */
382 : :
383 : : static bool
384 : 3037997 : build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec)
385 : : {
386 : 3299842 : tree member, init;
387 : 3299842 : if (TREE_CODE (t) == CLEANUP_POINT_EXPR)
388 : 1402494 : t = TREE_OPERAND (t, 0);
389 : 3299842 : if (TREE_CODE (t) == EXPR_STMT)
390 : 1402140 : t = TREE_OPERAND (t, 0);
391 : 3299842 : if (t == error_mark_node)
392 : : return false;
393 : 3299835 : if (TREE_CODE (t) == STATEMENT_LIST)
394 : : {
395 : 3528483 : for (tree stmt : tsi_range (t))
396 : 231237 : if (! build_data_member_initialization (stmt, vec))
397 : 7 : return false;
398 : : return true;
399 : : }
400 : 3040579 : if (TREE_CODE (t) == CLEANUP_STMT)
401 : : {
402 : : /* We can't see a CLEANUP_STMT in a constructor for a literal class,
403 : : but we can in a constexpr constructor for a non-literal class. Just
404 : : ignore it; either all the initialization will be constant, in which
405 : : case the cleanup can't run, or it can't be constexpr.
406 : : Still recurse into CLEANUP_BODY. */
407 : 245999 : return build_data_member_initialization (CLEANUP_BODY (t), vec);
408 : : }
409 : 2794580 : if (TREE_CODE (t) == CONVERT_EXPR)
410 : 1698564 : t = TREE_OPERAND (t, 0);
411 : 2794580 : if (TREE_CODE (t) == INIT_EXPR
412 : : /* vptr initialization shows up as a MODIFY_EXPR. In C++14 we only
413 : : use what this function builds for cx_check_missing_mem_inits, and
414 : : assignment in the ctor body doesn't count. */
415 : 1180606 : || (cxx_dialect < cxx14 && TREE_CODE (t) == MODIFY_EXPR))
416 : : {
417 : 1614456 : member = TREE_OPERAND (t, 0);
418 : 1614456 : init = break_out_target_exprs (TREE_OPERAND (t, 1));
419 : : }
420 : 1180124 : else if (TREE_CODE (t) == CALL_EXPR)
421 : : {
422 : 1009517 : tree fn = get_callee_fndecl (t);
423 : 2019034 : if (!fn || !DECL_CONSTRUCTOR_P (fn))
424 : : /* We're only interested in calls to subobject constructors. */
425 : : return true;
426 : 891788 : member = CALL_EXPR_ARG (t, 0);
427 : : /* We don't use build_cplus_new here because it complains about
428 : : abstract bases. Leaving the call unwrapped means that it has the
429 : : wrong type, but cxx_eval_constant_expression doesn't care. */
430 : 891788 : init = break_out_target_exprs (t);
431 : : }
432 : 170607 : else if (TREE_CODE (t) == BIND_EXPR)
433 : 15846 : return build_data_member_initialization (BIND_EXPR_BODY (t), vec);
434 : : else
435 : : /* Don't add anything else to the CONSTRUCTOR. */
436 : : return true;
437 : 2506244 : if (INDIRECT_REF_P (member))
438 : 30020 : member = TREE_OPERAND (member, 0);
439 : 2506244 : if (TREE_CODE (member) == NOP_EXPR)
440 : : {
441 : 214331 : tree op = member;
442 : 214331 : STRIP_NOPS (op);
443 : 214331 : if (TREE_CODE (op) == ADDR_EXPR)
444 : : {
445 : 290 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
446 : : (TREE_TYPE (TREE_TYPE (op)),
447 : : TREE_TYPE (TREE_TYPE (member))));
448 : : /* Initializing a cv-qualified member; we need to look through
449 : : the const_cast. */
450 : : member = op;
451 : : }
452 : 214041 : else if (op == current_class_ptr
453 : 427970 : && (same_type_ignoring_top_level_qualifiers_p
454 : 213929 : (TREE_TYPE (TREE_TYPE (member)),
455 : : current_class_type)))
456 : : /* Delegating constructor. */
457 : : member = op;
458 : : else
459 : : {
460 : : /* This is an initializer for an empty base; keep it for now so
461 : : we can check it in cxx_eval_bare_aggregate. */
462 : 195239 : gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member))));
463 : : }
464 : : }
465 : 2506244 : if (TREE_CODE (member) == ADDR_EXPR)
466 : 707767 : member = TREE_OPERAND (member, 0);
467 : 2506244 : if (TREE_CODE (member) == COMPONENT_REF)
468 : : {
469 : 2280855 : tree aggr = TREE_OPERAND (member, 0);
470 : 2280855 : if (TREE_CODE (aggr) == VAR_DECL)
471 : : /* Initializing a local variable, don't add anything. */
472 : : return true;
473 : 2280853 : if (TREE_CODE (aggr) != COMPONENT_REF)
474 : : /* Normal member initialization. */
475 : 2279708 : member = TREE_OPERAND (member, 1);
476 : 1145 : else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr)))
477 : : /* Initializing a member of an anonymous union. */
478 : 878 : return build_anon_member_initialization (member, init, vec);
479 : : else
480 : : /* We're initializing a vtable pointer in a base. Leave it as
481 : : COMPONENT_REF so we remember the path to get to the vfield. */
482 : 267 : gcc_assert (TREE_TYPE (member) == vtbl_ptr_type_node);
483 : : }
484 : :
485 : : /* Value-initialization can produce multiple initializers for the
486 : : same field; use the last one. */
487 : 3060214 : if (!vec_safe_is_empty (*vec) && (*vec)->last().index == member)
488 : 32 : (*vec)->last().value = init;
489 : : else
490 : 2505332 : CONSTRUCTOR_APPEND_ELT (*vec, member, init);
491 : : return true;
492 : : }
493 : :
494 : : /* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval.
495 : : In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a
496 : : BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */
497 : :
498 : : static bool
499 : 2354 : check_constexpr_bind_expr_vars (tree t)
500 : : {
501 : 2354 : gcc_assert (TREE_CODE (t) == BIND_EXPR);
502 : :
503 : 3155 : for (tree var = BIND_EXPR_VARS (t); var; var = DECL_CHAIN (var))
504 : 813 : if (TREE_CODE (var) == TYPE_DECL
505 : 797 : && DECL_IMPLICIT_TYPEDEF_P (var)
506 : 835 : && !LAMBDA_TYPE_P (TREE_TYPE (var)))
507 : : return false;
508 : : return true;
509 : : }
510 : :
511 : : /* Subroutine of check_constexpr_ctor_body. */
512 : :
513 : : static bool
514 : 4069 : check_constexpr_ctor_body_1 (tree last, tree list)
515 : : {
516 : 4069 : switch (TREE_CODE (list))
517 : : {
518 : 6 : case DECL_EXPR:
519 : 6 : if (TREE_CODE (DECL_EXPR_DECL (list)) == USING_DECL
520 : 6 : || TREE_CODE (DECL_EXPR_DECL (list)) == TYPE_DECL)
521 : : return true;
522 : : return false;
523 : :
524 : 3 : case CLEANUP_POINT_EXPR:
525 : 3 : return check_constexpr_ctor_body (last, TREE_OPERAND (list, 0),
526 : 3 : /*complain=*/false);
527 : :
528 : 2026 : case BIND_EXPR:
529 : 2026 : if (!check_constexpr_bind_expr_vars (list)
530 : 2026 : || !check_constexpr_ctor_body (last, BIND_EXPR_BODY (list),
531 : : /*complain=*/false))
532 : 11 : return false;
533 : : return true;
534 : :
535 : : case USING_STMT:
536 : : case STATIC_ASSERT:
537 : : case DEBUG_BEGIN_STMT:
538 : : return true;
539 : :
540 : : default:
541 : : return false;
542 : : }
543 : : }
544 : :
545 : : /* Make sure that there are no statements after LAST in the constructor
546 : : body represented by LIST. */
547 : :
548 : : bool
549 : 3412900 : check_constexpr_ctor_body (tree last, tree list, bool complain)
550 : : {
551 : : /* C++14 doesn't require a constexpr ctor to have an empty body. */
552 : 3412900 : if (cxx_dialect >= cxx14)
553 : : return true;
554 : :
555 : 13068 : bool ok = true;
556 : 13068 : if (TREE_CODE (list) == STATEMENT_LIST)
557 : : {
558 : 13055 : tree_stmt_iterator i = tsi_last (list);
559 : 17093 : for (; !tsi_end_p (i); tsi_prev (&i))
560 : : {
561 : 14974 : tree t = tsi_stmt (i);
562 : 14974 : if (t == last)
563 : : break;
564 : 4056 : if (!check_constexpr_ctor_body_1 (last, t))
565 : : {
566 : : ok = false;
567 : : break;
568 : : }
569 : : }
570 : : }
571 : 13 : else if (list != last
572 : 13 : && !check_constexpr_ctor_body_1 (last, list))
573 : : ok = false;
574 : 13055 : if (!ok)
575 : : {
576 : 22 : if (complain)
577 : 16 : error ("%<constexpr%> constructor does not have empty body");
578 : 22 : DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false;
579 : : }
580 : : return ok;
581 : : }
582 : :
583 : : /* V is a vector of constructor elements built up for the base and member
584 : : initializers of a constructor for TYPE. They need to be in increasing
585 : : offset order, which they might not be yet if TYPE has a primary base
586 : : which is not first in the base-clause or a vptr and at least one base
587 : : all of which are non-primary. */
588 : :
589 : : static vec<constructor_elt, va_gc> *
590 : 2036100 : sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v)
591 : : {
592 : 2036100 : tree pri = CLASSTYPE_PRIMARY_BINFO (type);
593 : 2036100 : tree field_type;
594 : 2036100 : unsigned i;
595 : 2036100 : constructor_elt *ce;
596 : :
597 : 2036100 : if (pri)
598 : 31283 : field_type = BINFO_TYPE (pri);
599 : 2004817 : else if (TYPE_CONTAINS_VPTR_P (type))
600 : 26143 : field_type = vtbl_ptr_type_node;
601 : : else
602 : : return v;
603 : :
604 : : /* Find the element for the primary base or vptr and move it to the
605 : : beginning of the vec. */
606 : 74456 : for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
607 : 48408 : if (TREE_TYPE (ce->index) == field_type)
608 : : break;
609 : :
610 : 69298 : if (i > 0 && i < vec_safe_length (v))
611 : : {
612 : 23 : vec<constructor_elt, va_gc> &vref = *v;
613 : 23 : constructor_elt elt = vref[i];
614 : 46 : for (; i > 0; --i)
615 : 23 : vref[i] = vref[i-1];
616 : 23 : vref[0] = elt;
617 : : }
618 : :
619 : : return v;
620 : : }
621 : :
622 : : /* Build compile-time evalable representations of member-initializer list
623 : : for a constexpr constructor. */
624 : :
625 : : static tree
626 : 2054905 : build_constexpr_constructor_member_initializers (tree type, tree body)
627 : : {
628 : 2054905 : vec<constructor_elt, va_gc> *vec = NULL;
629 : 2054905 : bool ok = true;
630 : 4629848 : while (true)
631 : 4629848 : switch (TREE_CODE (body))
632 : : {
633 : 1020432 : case MUST_NOT_THROW_EXPR:
634 : 1020432 : case EH_SPEC_BLOCK:
635 : 1020432 : body = TREE_OPERAND (body, 0);
636 : 1020432 : break;
637 : :
638 : 1554511 : case STATEMENT_LIST:
639 : 6184424 : for (tree stmt : tsi_range (body))
640 : : {
641 : 3109071 : body = stmt;
642 : 3109071 : if (TREE_CODE (body) == BIND_EXPR)
643 : : break;
644 : : }
645 : : break;
646 : :
647 : 2054905 : case BIND_EXPR:
648 : 2054905 : body = BIND_EXPR_BODY (body);
649 : 2054905 : goto found;
650 : :
651 : 0 : default:
652 : 0 : gcc_unreachable ();
653 : : }
654 : 2054905 : found:
655 : 2054905 : if (TREE_CODE (body) == TRY_BLOCK)
656 : : {
657 : 26 : body = TREE_OPERAND (body, 0);
658 : 26 : if (TREE_CODE (body) == BIND_EXPR)
659 : 26 : body = BIND_EXPR_BODY (body);
660 : : }
661 : 2054905 : if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
662 : : {
663 : 1290459 : body = TREE_OPERAND (body, 0);
664 : 1290459 : if (TREE_CODE (body) == EXPR_STMT)
665 : 1290459 : body = TREE_OPERAND (body, 0);
666 : 1290459 : if (TREE_CODE (body) == INIT_EXPR
667 : 1290459 : && (same_type_ignoring_top_level_qualifiers_p
668 : 0 : (TREE_TYPE (TREE_OPERAND (body, 0)),
669 : : current_class_type)))
670 : : {
671 : : /* Trivial copy. */
672 : 0 : return TREE_OPERAND (body, 1);
673 : : }
674 : 1290459 : ok = build_data_member_initialization (body, &vec);
675 : : }
676 : 764446 : else if (TREE_CODE (body) == STATEMENT_LIST)
677 : : {
678 : 2280171 : for (tree stmt : tsi_range (body))
679 : : {
680 : 1516013 : ok = build_data_member_initialization (stmt, &vec);
681 : 1516013 : if (!ok)
682 : : break;
683 : : }
684 : : }
685 : 288 : else if (EXPR_P (body))
686 : 288 : ok = build_data_member_initialization (body, &vec);
687 : : else
688 : 0 : gcc_assert (errorcount > 0);
689 : 2054905 : if (ok)
690 : : {
691 : 2054898 : if (vec_safe_length (vec) > 0)
692 : : {
693 : : /* In a delegating constructor, return the target. */
694 : 1951266 : constructor_elt *ce = &(*vec)[0];
695 : 1951266 : if (ce->index == current_class_ptr)
696 : : {
697 : 18798 : body = ce->value;
698 : 18798 : vec_free (vec);
699 : 18798 : return body;
700 : : }
701 : : }
702 : 2036100 : vec = sort_constexpr_mem_initializers (type, vec);
703 : 2036100 : return build_constructor (type, vec);
704 : : }
705 : : else
706 : 7 : return error_mark_node;
707 : : }
708 : :
709 : : /* We have an expression tree T that represents a call, either CALL_EXPR
710 : : or AGGR_INIT_EXPR. If the call is lexically to a named function,
711 : : return the _DECL for that function. */
712 : :
713 : : static tree
714 : 306734931 : get_function_named_in_call (tree t)
715 : : {
716 : 306734931 : tree callee = cp_get_callee (t);
717 : 306734931 : tree fun = cp_get_fndecl_from_callee (callee, /*fold*/false);
718 : 306734931 : return fun ? fun : callee;
719 : : }
720 : :
721 : : /* Subroutine of check_constexpr_fundef. BODY is the body of a function
722 : : declared to be constexpr, or a sub-statement thereof. Returns the
723 : : return value if suitable, error_mark_node for a statement not allowed in
724 : : a constexpr function, or NULL_TREE if no return value was found. */
725 : :
726 : : tree
727 : 68421 : constexpr_fn_retval (tree body)
728 : : {
729 : 74923 : switch (TREE_CODE (body))
730 : : {
731 : 18483 : case STATEMENT_LIST:
732 : 18483 : {
733 : 18483 : tree expr = NULL_TREE;
734 : 55381 : for (tree stmt : tsi_range (body))
735 : : {
736 : 36925 : tree s = constexpr_fn_retval (stmt);
737 : 36925 : if (s == error_mark_node)
738 : : return error_mark_node;
739 : 36898 : else if (s == NULL_TREE)
740 : : /* Keep iterating. */;
741 : 18446 : else if (expr)
742 : : /* Multiple return statements. */
743 : : return error_mark_node;
744 : : else
745 : : expr = s;
746 : : }
747 : : return expr;
748 : : }
749 : :
750 : 31444 : case RETURN_EXPR:
751 : 31444 : return break_out_target_exprs (TREE_OPERAND (body, 0));
752 : :
753 : 17 : case DECL_EXPR:
754 : 17 : {
755 : 17 : tree decl = DECL_EXPR_DECL (body);
756 : 17 : if (TREE_CODE (decl) == USING_DECL
757 : : /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */
758 : 17 : || DECL_ARTIFICIAL (decl))
759 : : return NULL_TREE;
760 : 6 : return error_mark_node;
761 : : }
762 : :
763 : 6180 : case CLEANUP_POINT_EXPR:
764 : 6180 : return constexpr_fn_retval (TREE_OPERAND (body, 0));
765 : :
766 : 328 : case BIND_EXPR:
767 : 328 : if (!check_constexpr_bind_expr_vars (body))
768 : 6 : return error_mark_node;
769 : 322 : return constexpr_fn_retval (BIND_EXPR_BODY (body));
770 : :
771 : : case USING_STMT:
772 : : case DEBUG_BEGIN_STMT:
773 : : return NULL_TREE;
774 : :
775 : 0 : case CALL_EXPR:
776 : 0 : {
777 : 0 : tree fun = get_function_named_in_call (body);
778 : 0 : if (fun != NULL_TREE
779 : 0 : && fndecl_built_in_p (fun, BUILT_IN_UNREACHABLE))
780 : : return NULL_TREE;
781 : : }
782 : : /* Fallthru. */
783 : :
784 : 30 : default:
785 : 30 : return error_mark_node;
786 : : }
787 : : }
788 : :
789 : : /* Subroutine of check_constexpr_fundef. BODY is the DECL_SAVED_TREE of
790 : : FUN; do the necessary transformations to turn it into a single expression
791 : : that we can store in the hash table. */
792 : :
793 : : static tree
794 : 18005531 : massage_constexpr_body (tree fun, tree body)
795 : : {
796 : 36011062 : if (DECL_CONSTRUCTOR_P (fun))
797 : 2054905 : body = build_constexpr_constructor_member_initializers
798 : 2054905 : (DECL_CONTEXT (fun), body);
799 : 15950626 : else if (cxx_dialect < cxx14)
800 : : {
801 : 31267 : if (TREE_CODE (body) == EH_SPEC_BLOCK)
802 : 1 : body = EH_SPEC_STMTS (body);
803 : 31267 : if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
804 : 20221 : body = TREE_OPERAND (body, 0);
805 : 31267 : body = constexpr_fn_retval (body);
806 : : }
807 : 18005531 : return body;
808 : : }
809 : :
810 : : /* CTYPE is a type constructed from BODY. Return true if some
811 : : bases/fields are uninitialized, and complain if COMPLAIN. */
812 : :
813 : : static bool
814 : 1717885 : cx_check_missing_mem_inits (tree ctype, tree body, bool complain)
815 : : {
816 : : /* We allow uninitialized bases/fields in C++20. */
817 : 1717885 : if (cxx_dialect >= cxx20)
818 : : return false;
819 : :
820 : 792088 : unsigned nelts = 0;
821 : :
822 : 792088 : if (body)
823 : : {
824 : 792084 : if (TREE_CODE (body) != CONSTRUCTOR)
825 : : return false;
826 : 791828 : nelts = CONSTRUCTOR_NELTS (body);
827 : : }
828 : 791832 : tree field = TYPE_FIELDS (ctype);
829 : :
830 : 791832 : if (TREE_CODE (ctype) == UNION_TYPE)
831 : : {
832 : 8196 : if (nelts == 0 && next_aggregate_field (field))
833 : : {
834 : 2 : if (complain)
835 : 2 : error ("%<constexpr%> constructor for union %qT must "
836 : : "initialize exactly one non-static data member", ctype);
837 : 2 : return true;
838 : : }
839 : 8194 : return false;
840 : : }
841 : :
842 : : /* Iterate over the CONSTRUCTOR, checking any missing fields don't
843 : : need an explicit initialization. */
844 : : bool bad = false;
845 : 1692362 : for (unsigned i = 0; i <= nelts; ++i)
846 : : {
847 : 1692362 : tree index = NULL_TREE;
848 : 1692362 : if (i < nelts)
849 : : {
850 : 908726 : index = CONSTRUCTOR_ELT (body, i)->index;
851 : : /* Skip vptr adjustment represented with a COMPONENT_REF. */
852 : 908726 : if (TREE_CODE (index) != FIELD_DECL)
853 : 67608 : continue;
854 : : }
855 : :
856 : 39467932 : for (; field != index; field = DECL_CHAIN (field))
857 : : {
858 : 37843247 : tree ftype;
859 : 37843247 : if (TREE_CODE (field) != FIELD_DECL)
860 : 75552121 : continue;
861 : 134283 : if (DECL_UNNAMED_BIT_FIELD (field))
862 : 16 : continue;
863 : : /* Artificial fields can be ignored unless they're bases. */
864 : 134267 : if (DECL_ARTIFICIAL (field) && !DECL_FIELD_IS_BASE (field))
865 : 3 : continue;
866 : 134264 : if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
867 : : {
868 : : /* Recurse to check the anonymous aggregate member. */
869 : 8 : bad |= cx_check_missing_mem_inits
870 : 4 : (TREE_TYPE (field), NULL_TREE, complain);
871 : 4 : if (bad && !complain)
872 : 69 : return true;
873 : 4 : continue;
874 : : }
875 : 134260 : ftype = TREE_TYPE (field);
876 : 134260 : if (!ftype || !TYPE_P (ftype) || !COMPLETE_TYPE_P (ftype))
877 : : /* A flexible array can't be intialized here, so don't complain
878 : : that it isn't. */
879 : 2 : continue;
880 : 134258 : if (is_empty_field (field))
881 : : /* An empty field doesn't need an initializer. */
882 : 134157 : continue;
883 : 101 : ftype = strip_array_types (ftype);
884 : 101 : if (type_has_constexpr_default_constructor (ftype))
885 : : {
886 : : /* It's OK to skip a member with a trivial constexpr ctor.
887 : : A constexpr ctor that isn't trivial should have been
888 : : added in by now. */
889 : 11 : gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype)
890 : : || errorcount != 0);
891 : 11 : continue;
892 : : }
893 : 90 : if (!complain)
894 : : return true;
895 : 21 : auto_diagnostic_group d;
896 : 21 : error ("member %qD must be initialized by mem-initializer "
897 : : "in %<constexpr%> constructor", field);
898 : 21 : inform (DECL_SOURCE_LOCATION (field), "declared here");
899 : 21 : bad = true;
900 : 21 : }
901 : 1624685 : if (field == NULL_TREE)
902 : : break;
903 : :
904 : 841118 : if (ANON_AGGR_TYPE_P (TREE_TYPE (index)))
905 : : {
906 : : /* Check the anonymous aggregate initializer is valid. */
907 : 448 : bad |= cx_check_missing_mem_inits
908 : 224 : (TREE_TYPE (index), CONSTRUCTOR_ELT (body, i)->value, complain);
909 : 224 : if (bad && !complain)
910 : : return true;
911 : : }
912 : 841118 : field = DECL_CHAIN (field);
913 : : }
914 : :
915 : : return bad;
916 : : }
917 : :
918 : : /* We are processing the definition of the constexpr function FUN.
919 : : Check that its body fulfills the apropriate requirements and
920 : : enter it in the constexpr function definition table. */
921 : :
922 : : void
923 : 141210223 : maybe_save_constexpr_fundef (tree fun)
924 : : {
925 : 141210223 : if (processing_template_decl
926 : 56139443 : || cp_function_chain->invalid_constexpr
927 : 197237718 : || (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun)))
928 : 123204952 : return;
929 : :
930 : : /* With -fimplicit-constexpr, try to make inlines constexpr. We'll
931 : : actually set DECL_DECLARED_CONSTEXPR_P below if the checks pass. */
932 : 42492159 : bool implicit = false;
933 : 42492159 : if (flag_implicit_constexpr)
934 : : {
935 : 13 : if (DECL_DELETING_DESTRUCTOR_P (fun)
936 : 13 : && decl_implicit_constexpr_p (DECL_CLONED_FUNCTION (fun)))
937 : : /* Don't inherit implicit constexpr from the non-deleting
938 : : destructor. */
939 : 0 : DECL_DECLARED_CONSTEXPR_P (fun) = false;
940 : :
941 : 13 : if (!DECL_DECLARED_CONSTEXPR_P (fun)
942 : 12 : && DECL_DECLARED_INLINE_P (fun)
943 : 21 : && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fun)))
944 : : implicit = true;
945 : : }
946 : :
947 : 42492159 : if (!DECL_DECLARED_CONSTEXPR_P (fun) && !implicit)
948 : : return;
949 : :
950 : 18069023 : bool complain = !DECL_GENERATED_P (fun) && !implicit;
951 : :
952 : 18069023 : if (!is_valid_constexpr_fn (fun, complain))
953 : : return;
954 : :
955 : 18005484 : tree massaged = massage_constexpr_body (fun, DECL_SAVED_TREE (fun));
956 : 18005484 : if (massaged == NULL_TREE || massaged == error_mark_node)
957 : : {
958 : 66 : if (!DECL_CONSTRUCTOR_P (fun) && complain)
959 : 22 : error ("body of %<constexpr%> function %qD not a return-statement",
960 : : fun);
961 : 33 : return;
962 : : }
963 : :
964 : 18005451 : bool potential = potential_rvalue_constant_expression (massaged);
965 : 18005451 : if (!potential && complain)
966 : 182 : require_potential_rvalue_constant_expression_fncheck (massaged);
967 : :
968 : 20060338 : if (DECL_CONSTRUCTOR_P (fun) && potential
969 : 20057584 : && !DECL_DEFAULTED_FN (fun))
970 : : {
971 : 1717646 : if (cx_check_missing_mem_inits (DECL_CONTEXT (fun),
972 : : massaged, complain))
973 : : potential = false;
974 : 1717558 : else if (cxx_dialect > cxx11)
975 : : {
976 : : /* What we got from massage_constexpr_body is pretty much just the
977 : : ctor-initializer, also check the body. */
978 : 1712874 : massaged = DECL_SAVED_TREE (fun);
979 : 1712874 : potential = potential_rvalue_constant_expression (massaged);
980 : 1712874 : if (!potential && complain)
981 : 16 : require_potential_rvalue_constant_expression_fncheck (massaged);
982 : : }
983 : : }
984 : :
985 : 18005451 : if (!potential && complain
986 : : /* If -Wno-invalid-constexpr was specified, we haven't complained
987 : : about non-constant expressions yet. Register the function and
988 : : complain in explain_invalid_constexpr_fn if the function is
989 : : called. */
990 : 217 : && warn_invalid_constexpr != 0)
991 : : return;
992 : :
993 : 18005279 : if (implicit)
994 : : {
995 : 8 : if (potential)
996 : : {
997 : 0 : DECL_DECLARED_CONSTEXPR_P (fun) = true;
998 : 0 : DECL_LANG_SPECIFIC (fun)->u.fn.implicit_constexpr = true;
999 : 0 : if (DECL_CONSTRUCTOR_P (fun))
1000 : 0 : TYPE_HAS_CONSTEXPR_CTOR (DECL_CONTEXT (fun)) = true;
1001 : : }
1002 : : else
1003 : : /* Don't bother keeping the pre-generic body of unsuitable functions
1004 : : not explicitly declared constexpr. */
1005 : : return;
1006 : : }
1007 : :
1008 : 18005271 : constexpr_fundef entry = {fun, NULL_TREE, NULL_TREE, NULL_TREE};
1009 : 18005271 : bool clear_ctx = false;
1010 : 18005271 : if (DECL_RESULT (fun) && DECL_CONTEXT (DECL_RESULT (fun)) == NULL_TREE)
1011 : : {
1012 : 18005271 : clear_ctx = true;
1013 : 18005271 : DECL_CONTEXT (DECL_RESULT (fun)) = fun;
1014 : : }
1015 : 18005271 : tree saved_fn = current_function_decl;
1016 : 18005271 : current_function_decl = fun;
1017 : 18005271 : entry.body = copy_fn (entry.decl, entry.parms, entry.result);
1018 : 18005271 : current_function_decl = saved_fn;
1019 : 18005271 : if (clear_ctx)
1020 : 18005271 : DECL_CONTEXT (DECL_RESULT (entry.decl)) = NULL_TREE;
1021 : 18005271 : if (!potential)
1022 : : /* For a template instantiation, we want to remember the pre-generic body
1023 : : for explain_invalid_constexpr_fn, but do tell cxx_eval_call_expression
1024 : : that it doesn't need to bother trying to expand the function. */
1025 : 84071 : entry.result = error_mark_node;
1026 : :
1027 : 18005271 : register_constexpr_fundef (entry);
1028 : : }
1029 : :
1030 : : /* BODY is a validated and massaged definition of a constexpr
1031 : : function. Register it in the hash table. */
1032 : :
1033 : : void
1034 : 18027195 : register_constexpr_fundef (const constexpr_fundef &value)
1035 : : {
1036 : : /* Create the constexpr function table if necessary. */
1037 : 18027195 : if (constexpr_fundef_table == NULL)
1038 : 25400 : constexpr_fundef_table
1039 : 25400 : = hash_table<constexpr_fundef_hasher>::create_ggc (101);
1040 : :
1041 : 18027195 : constexpr_fundef **slot = constexpr_fundef_table->find_slot
1042 : 18027195 : (const_cast<constexpr_fundef *> (&value), INSERT);
1043 : :
1044 : 18027195 : gcc_assert (*slot == NULL);
1045 : 18027195 : *slot = ggc_alloc<constexpr_fundef> ();
1046 : 18027195 : **slot = value;
1047 : 18027195 : }
1048 : :
1049 : : /* FUN is a non-constexpr (or, with -Wno-invalid-constexpr, a constexpr
1050 : : function called in a context that requires a constant expression).
1051 : : If it comes from a constexpr template, explain why the instantiation
1052 : : isn't constexpr. Otherwise, explain why the function cannot be used
1053 : : in a constexpr context. */
1054 : :
1055 : : void
1056 : 692 : explain_invalid_constexpr_fn (tree fun)
1057 : : {
1058 : 692 : static hash_set<tree> *diagnosed;
1059 : 692 : tree body;
1060 : :
1061 : : /* Don't try to explain a function we already complained about. */
1062 : 692 : if (function *f = DECL_STRUCT_FUNCTION (fun))
1063 : 502 : if (f->language->erroneous)
1064 : 692 : return;
1065 : :
1066 : : /* In C++23, a function marked 'constexpr' may not actually be a constant
1067 : : expression. We haven't diagnosed the problem yet: -Winvalid-constexpr
1068 : : wasn't enabled. The function was called, so diagnose why it cannot be
1069 : : used in a constant expression. */
1070 : 643 : if (warn_invalid_constexpr == 0 && DECL_DECLARED_CONSTEXPR_P (fun))
1071 : : /* Go on. */;
1072 : : /* Only diagnose defaulted functions, lambdas, or instantiations. */
1073 : 621 : else if (!DECL_DEFAULTED_FN (fun)
1074 : 827 : && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun))
1075 : 597 : && !(flag_implicit_constexpr
1076 : 6 : && !DECL_DECLARED_CONSTEXPR_P (fun)
1077 : 6 : && DECL_DECLARED_INLINE_P (fun))
1078 : 1215 : && !is_instantiation_of_constexpr (fun))
1079 : : {
1080 : 580 : inform (DECL_SOURCE_LOCATION (fun), "%qD declared here", fun);
1081 : 3 : if (flag_implicit_constexpr && !maybe_constexpr_fn (fun)
1082 : 583 : && decl_defined_p (fun))
1083 : 3 : inform (DECL_SOURCE_LOCATION (fun),
1084 : : "%<-fimplicit-constexpr%> only affects %<inline%> functions");
1085 : 580 : return;
1086 : : }
1087 : 63 : if (diagnosed == NULL)
1088 : 51 : diagnosed = new hash_set<tree>;
1089 : 63 : if (diagnosed->add (fun))
1090 : : /* Already explained. */
1091 : : return;
1092 : :
1093 : 62 : iloc_sentinel ils = input_location;
1094 : 62 : if (!lambda_static_thunk_p (fun))
1095 : : {
1096 : : /* Diagnostics should completely ignore the static thunk, so leave
1097 : : input_location set to our caller's location. */
1098 : 62 : input_location = DECL_SOURCE_LOCATION (fun);
1099 : 62 : inform (input_location,
1100 : : "%qD is not usable as a %<constexpr%> function because:", fun);
1101 : : }
1102 : : /* First check the declaration. */
1103 : 62 : if (is_valid_constexpr_fn (fun, true))
1104 : : {
1105 : : /* Then if it's OK, the body. */
1106 : 57 : if (!DECL_DECLARED_CONSTEXPR_P (fun)
1107 : 57 : && DECL_DEFAULTED_FN (fun))
1108 : 10 : explain_implicit_non_constexpr (fun);
1109 : : else
1110 : : {
1111 : 47 : if (constexpr_fundef *fd = retrieve_constexpr_fundef (fun))
1112 : 37 : body = fd->body;
1113 : : else
1114 : 10 : body = DECL_SAVED_TREE (fun);
1115 : 47 : tree massaged = massage_constexpr_body (fun, body);
1116 : 47 : require_potential_rvalue_constant_expression (massaged);
1117 : 94 : if (DECL_CONSTRUCTOR_P (fun))
1118 : : {
1119 : 11 : cx_check_missing_mem_inits (DECL_CONTEXT (fun), massaged, true);
1120 : 11 : if (cxx_dialect > cxx11)
1121 : : /* Also check the body, not just the ctor-initializer. */
1122 : 9 : require_potential_rvalue_constant_expression (body);
1123 : : }
1124 : : }
1125 : : }
1126 : 62 : }
1127 : :
1128 : : /* Objects of this type represent calls to constexpr functions
1129 : : along with the bindings of parameters to their arguments, for
1130 : : the purpose of compile time evaluation. */
1131 : :
1132 : : struct GTY((for_user)) constexpr_call {
1133 : : /* Description of the constexpr function definition. */
1134 : : constexpr_fundef *fundef = nullptr;
1135 : : /* Parameter bindings environment. A TREE_VEC of arguments. */
1136 : : tree bindings = NULL_TREE;
1137 : : /* Result of the call, indexed by the value of
1138 : : constexpr_ctx::manifestly_const_eval.
1139 : : unknown_type_node means the call is being evaluated.
1140 : : error_mark_node means that the evaluation was erroneous or otherwise
1141 : : uncacheable (e.g. because it depends on the caller).
1142 : : Otherwise, the actual value of the call. */
1143 : : tree results[3] = { NULL_TREE, NULL_TREE, NULL_TREE };
1144 : : /* The hash of this call; we remember it here to avoid having to
1145 : : recalculate it when expanding the hash table. */
1146 : : hashval_t hash = 0;
1147 : :
1148 : : /* The result slot corresponding to the given mce_value. */
1149 : 37675126 : tree& result (mce_value mce) { return results[1 + int(mce)]; }
1150 : : };
1151 : :
1152 : : struct constexpr_call_hasher : ggc_ptr_hash<constexpr_call>
1153 : : {
1154 : : static hashval_t hash (constexpr_call *);
1155 : : static bool equal (constexpr_call *, constexpr_call *);
1156 : : };
1157 : :
1158 : : enum constexpr_switch_state {
1159 : : /* Used when processing a switch for the first time by cxx_eval_switch_expr
1160 : : and default: label for that switch has not been seen yet. */
1161 : : css_default_not_seen,
1162 : : /* Used when processing a switch for the first time by cxx_eval_switch_expr
1163 : : and default: label for that switch has been seen already. */
1164 : : css_default_seen,
1165 : : /* Used when processing a switch for the second time by
1166 : : cxx_eval_switch_expr, where default: label should match. */
1167 : : css_default_processing
1168 : : };
1169 : :
1170 : : /* The constexpr expansion context part which needs one instance per
1171 : : cxx_eval_outermost_constant_expr invocation. VALUES is a map of values of
1172 : : variables initialized within the expression. */
1173 : :
1174 : : class constexpr_global_ctx {
1175 : : /* Values for any temporaries or local variables within the
1176 : : constant-expression. Objects outside their lifetime have
1177 : : value 'void_node'. */
1178 : : hash_map<tree,tree> values;
1179 : : public:
1180 : : /* Number of cxx_eval_constant_expression calls (except skipped ones,
1181 : : on simple constants or location wrappers) encountered during current
1182 : : cxx_eval_outermost_constant_expr call. */
1183 : : HOST_WIDE_INT constexpr_ops_count;
1184 : : /* Heap VAR_DECLs created during the evaluation of the outermost constant
1185 : : expression. */
1186 : : auto_vec<tree, 16> heap_vars;
1187 : : /* Vector of caught exceptions, including exceptions still not active at
1188 : : the start of a handler (those are immediately followed up by HANDLER_TYPE
1189 : : until __cxa_begin_catch finishes). */
1190 : : auto_vec<tree, 2> caught_exceptions;
1191 : : /* Cleanups that need to be evaluated at the end of CLEANUP_POINT_EXPR. */
1192 : : vec<tree> *cleanups;
1193 : : /* If non-null, only allow modification of existing values of the variables
1194 : : in this set. Set by modifiable_tracker, below. */
1195 : : hash_set<tree> *modifiable;
1196 : : /* Number of heap VAR_DECL deallocations. */
1197 : : unsigned heap_dealloc_count;
1198 : : /* Number of uncaught exceptions. */
1199 : : unsigned uncaught_exceptions;
1200 : :
1201 : : /* Constructor. */
1202 : 369594866 : constexpr_global_ctx ()
1203 : 739189732 : : constexpr_ops_count (0), cleanups (NULL), modifiable (nullptr),
1204 : 369594866 : heap_dealloc_count (0), uncaught_exceptions (0) {}
1205 : :
1206 : 193529686 : bool is_outside_lifetime (tree t)
1207 : : {
1208 : 193529686 : if (tree *p = values.get (t))
1209 : 42968563 : if (*p == void_node)
1210 : 178 : return true;
1211 : : return false;
1212 : : }
1213 : 227174133 : tree get_value (tree t)
1214 : : {
1215 : 227174133 : if (tree *p = values.get (t))
1216 : 67379162 : if (*p != void_node)
1217 : 66592876 : return *p;
1218 : : return NULL_TREE;
1219 : : }
1220 : 30287327 : tree *get_value_ptr (tree t, bool initializing)
1221 : : {
1222 : 30287327 : if (modifiable && !modifiable->contains (t))
1223 : : return nullptr;
1224 : 30287316 : if (tree *p = values.get (t))
1225 : : {
1226 : 30273158 : if (*p != void_node)
1227 : : return p;
1228 : 36 : else if (initializing)
1229 : : {
1230 : 6 : *p = NULL_TREE;
1231 : 6 : return p;
1232 : : }
1233 : : }
1234 : : return nullptr;
1235 : : }
1236 : 87708221 : void put_value (tree t, tree v)
1237 : : {
1238 : 87708221 : bool already_in_map = values.put (t, v);
1239 : 87708221 : if (!already_in_map && modifiable)
1240 : 32 : modifiable->add (t);
1241 : 87708221 : }
1242 : 62386811 : void destroy_value (tree t)
1243 : : {
1244 : 62386811 : if (TREE_CODE (t) == VAR_DECL
1245 : 62386811 : || TREE_CODE (t) == PARM_DECL
1246 : : || TREE_CODE (t) == RESULT_DECL)
1247 : 62381131 : values.put (t, void_node);
1248 : : else
1249 : 5680 : values.remove (t);
1250 : 62386811 : }
1251 : 3244508 : void clear_value (tree t)
1252 : : {
1253 : 6489016 : values.remove (t);
1254 : : }
1255 : : };
1256 : :
1257 : : /* Helper class for constexpr_global_ctx. In some cases we want to avoid
1258 : : side-effects from evaluation of a particular subexpression of a
1259 : : constant-expression. In such cases we use modifiable_tracker to prevent
1260 : : modification of variables created outside of that subexpression.
1261 : :
1262 : : ??? We could change the hash_set to a hash_map, allow and track external
1263 : : modifications, and roll them back in the destructor. It's not clear to me
1264 : : that this would be worthwhile. */
1265 : :
1266 : : class modifiable_tracker
1267 : : {
1268 : : hash_set<tree> set;
1269 : : constexpr_global_ctx *global;
1270 : : public:
1271 : 223926 : modifiable_tracker (constexpr_global_ctx *g): global(g)
1272 : : {
1273 : 223926 : global->modifiable = &set;
1274 : 223926 : }
1275 : 223926 : ~modifiable_tracker ()
1276 : : {
1277 : 223958 : for (tree t: set)
1278 : 32 : global->clear_value (t);
1279 : 223926 : global->modifiable = nullptr;
1280 : 223926 : }
1281 : : };
1282 : :
1283 : : /* The constexpr expansion context. CALL is the current function
1284 : : expansion, CTOR is the current aggregate initializer, OBJECT is the
1285 : : object being initialized by CTOR, either a VAR_DECL or a _REF. */
1286 : :
1287 : : struct constexpr_ctx {
1288 : : /* The part of the context that needs to be unique to the whole
1289 : : cxx_eval_outermost_constant_expr invocation. */
1290 : : constexpr_global_ctx *global;
1291 : : /* The innermost call we're evaluating. */
1292 : : constexpr_call *call;
1293 : : /* SAVE_EXPRs and TARGET_EXPR_SLOT vars of TARGET_EXPRs that we've seen
1294 : : within the current LOOP_EXPR. NULL if we aren't inside a loop. */
1295 : : vec<tree> *save_exprs;
1296 : : /* The CONSTRUCTOR we're currently building up for an aggregate
1297 : : initializer. */
1298 : : tree ctor;
1299 : : /* The object we're building the CONSTRUCTOR for. */
1300 : : tree object;
1301 : : /* If inside SWITCH_EXPR. */
1302 : : constexpr_switch_state *css_state;
1303 : : /* The aggregate initialization context inside which this one is nested. This
1304 : : is used by lookup_placeholder to resolve PLACEHOLDER_EXPRs. */
1305 : : const constexpr_ctx *parent;
1306 : :
1307 : : /* Whether we should error on a non-constant expression or fail quietly.
1308 : : This flag needs to be here, but some of the others could move to global
1309 : : if they get larger than a word. */
1310 : : bool quiet;
1311 : : /* Whether we are strictly conforming to constant expression rules or
1312 : : trying harder to get a constant value. */
1313 : : bool strict;
1314 : : /* Whether __builtin_is_constant_evaluated () should be true. */
1315 : : mce_value manifestly_const_eval;
1316 : : };
1317 : :
1318 : : /* Predicates for the meaning of *jump_target. */
1319 : :
1320 : : static bool
1321 : 34435947 : returns (tree *jump_target)
1322 : : {
1323 : 4972935 : return *jump_target && TREE_CODE (*jump_target) == RETURN_EXPR;
1324 : : }
1325 : :
1326 : : static bool
1327 : 35269712 : breaks (tree *jump_target)
1328 : : {
1329 : 35269712 : return (*jump_target
1330 : 35269712 : && ((TREE_CODE (*jump_target) == LABEL_DECL
1331 : 24 : && LABEL_DECL_BREAK (*jump_target))
1332 : 1024891 : || TREE_CODE (*jump_target) == BREAK_STMT
1333 : 980856 : || TREE_CODE (*jump_target) == EXIT_EXPR));
1334 : : }
1335 : :
1336 : : static bool
1337 : 50327794 : continues (tree *jump_target)
1338 : : {
1339 : 50327794 : return (*jump_target
1340 : 50327794 : && ((TREE_CODE (*jump_target) == LABEL_DECL
1341 : 36 : && LABEL_DECL_CONTINUE (*jump_target))
1342 : 1004555 : || TREE_CODE (*jump_target) == CONTINUE_STMT));
1343 : : }
1344 : :
1345 : : static bool
1346 : 6527072 : switches (tree *jump_target)
1347 : : {
1348 : 48911 : return *jump_target && TREE_CODE (*jump_target) == INTEGER_CST;
1349 : : }
1350 : :
1351 : : static bool
1352 : 1020774875 : throws (tree *jump_target)
1353 : : {
1354 : : /* void_node is for use in potential_constant_expression_1, otherwise
1355 : : it should an artificial VAR_DECL created by constant evaluation
1356 : : of __cxa_allocate_exception (). */
1357 : 56197677 : return (*jump_target && (VAR_P (*jump_target) || *jump_target == void_node));
1358 : : }
1359 : :
1360 : : /* True if the constexpr relaxations afforded by P2280R4 for unknown
1361 : : references and objects are in effect. */
1362 : :
1363 : : static bool
1364 : 156090745 : p2280_active_p (const constexpr_ctx *ctx)
1365 : : {
1366 : 27626709 : if (ctx->manifestly_const_eval != mce_true)
1367 : : /* Disable these relaxations during speculative constexpr folding,
1368 : : as it can significantly increase compile time/memory use
1369 : : (PR119387). */
1370 : : return false;
1371 : :
1372 : : /* P2280R4 was accepted as a DR against C++11. */
1373 : 0 : return cxx_dialect >= cxx11;
1374 : : }
1375 : :
1376 : : /* Remove T from the global values map, checking for attempts to destroy
1377 : : a value that has already finished its lifetime. */
1378 : :
1379 : : static void
1380 : 62366438 : destroy_value_checked (const constexpr_ctx* ctx, tree t, bool *non_constant_p)
1381 : : {
1382 : 62366438 : if (t == error_mark_node || TREE_TYPE (t) == error_mark_node)
1383 : : return;
1384 : :
1385 : : /* Don't error again here if we've already reported a problem. */
1386 : 62366435 : if (!*non_constant_p
1387 : 43168109 : && DECL_P (t)
1388 : : /* Non-trivial destructors have their lifetimes ended explicitly
1389 : : with a clobber, so don't worry about it here. */
1390 : 43162608 : && (!TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (t))
1391 : : /* ...except parameters are remapped in cxx_eval_call_expression,
1392 : : and the destructor call during cleanup won't be able to tell that
1393 : : this value has already been destroyed, so complain now. This is
1394 : : not quite unobservable, but is extremely unlikely to crop up in
1395 : : practice; see g++.dg/cpp2a/constexpr-lifetime2.C. */
1396 : 180157 : || TREE_CODE (t) == PARM_DECL)
1397 : 105349104 : && ctx->global->is_outside_lifetime (t))
1398 : : {
1399 : 54 : if (!ctx->quiet)
1400 : : {
1401 : 18 : auto_diagnostic_group d;
1402 : 18 : error ("destroying %qE outside its lifetime", t);
1403 : 18 : inform (DECL_SOURCE_LOCATION (t), "declared here");
1404 : 18 : }
1405 : 54 : *non_constant_p = true;
1406 : : }
1407 : 62366435 : ctx->global->destroy_value (t);
1408 : : }
1409 : :
1410 : : /* This internal flag controls whether we should avoid doing anything during
1411 : : constexpr evaluation that would cause extra DECL_UID generation, such as
1412 : : template instantiation and function body copying. */
1413 : :
1414 : : static bool uid_sensitive_constexpr_evaluation_value;
1415 : :
1416 : : /* An internal counter that keeps track of the number of times
1417 : : uid_sensitive_constexpr_evaluation_p returned true. */
1418 : :
1419 : : static unsigned uid_sensitive_constexpr_evaluation_true_counter;
1420 : :
1421 : : /* The accessor for uid_sensitive_constexpr_evaluation_value which also
1422 : : increments the corresponding counter. */
1423 : :
1424 : : static bool
1425 : 4487822 : uid_sensitive_constexpr_evaluation_p ()
1426 : : {
1427 : 4483481 : if (uid_sensitive_constexpr_evaluation_value)
1428 : : {
1429 : 197945 : ++uid_sensitive_constexpr_evaluation_true_counter;
1430 : 197945 : return true;
1431 : : }
1432 : : else
1433 : : return false;
1434 : : }
1435 : :
1436 : : /* The default constructor for uid_sensitive_constexpr_evaluation_sentinel
1437 : : enables the internal flag for uid_sensitive_constexpr_evaluation_p
1438 : : during the lifetime of the sentinel object. Upon its destruction, the
1439 : : previous value of uid_sensitive_constexpr_evaluation_p is restored. */
1440 : :
1441 : 65418569 : uid_sensitive_constexpr_evaluation_sentinel
1442 : 65418569 : ::uid_sensitive_constexpr_evaluation_sentinel ()
1443 : 65418569 : : ovr (uid_sensitive_constexpr_evaluation_value, true)
1444 : : {
1445 : 65418569 : }
1446 : :
1447 : : /* The default constructor for uid_sensitive_constexpr_evaluation_checker
1448 : : records the current number of times that uid_sensitive_constexpr_evaluation_p
1449 : : has been called and returned true. */
1450 : :
1451 : 1899884805 : uid_sensitive_constexpr_evaluation_checker
1452 : 1899884805 : ::uid_sensitive_constexpr_evaluation_checker ()
1453 : 1899884805 : : saved_counter (uid_sensitive_constexpr_evaluation_true_counter)
1454 : : {
1455 : 1899884805 : }
1456 : :
1457 : : /* Returns true iff uid_sensitive_constexpr_evaluation_p is true, and
1458 : : some constexpr evaluation was restricted due to u_s_c_e_p being called
1459 : : and returning true during the lifetime of this checker object. */
1460 : :
1461 : : bool
1462 : 1332010600 : uid_sensitive_constexpr_evaluation_checker::evaluation_restricted_p () const
1463 : : {
1464 : 1332010600 : return (uid_sensitive_constexpr_evaluation_value
1465 : 1332010600 : && saved_counter != uid_sensitive_constexpr_evaluation_true_counter);
1466 : : }
1467 : :
1468 : :
1469 : : /* A table of all constexpr calls that have been evaluated by the
1470 : : compiler in this translation unit. */
1471 : :
1472 : : static GTY (()) hash_table<constexpr_call_hasher> *constexpr_call_table;
1473 : :
1474 : : /* Compute a hash value for a constexpr call representation. */
1475 : :
1476 : : inline hashval_t
1477 : 98142131 : constexpr_call_hasher::hash (constexpr_call *info)
1478 : : {
1479 : 98142131 : return info->hash;
1480 : : }
1481 : :
1482 : : /* Return true if the objects pointed to by P and Q represent calls
1483 : : to the same constexpr function with the same arguments.
1484 : : Otherwise, return false. */
1485 : :
1486 : : bool
1487 : 108247630 : constexpr_call_hasher::equal (constexpr_call *lhs, constexpr_call *rhs)
1488 : : {
1489 : 108247630 : if (lhs == rhs)
1490 : : return true;
1491 : 108247630 : if (lhs->hash != rhs->hash)
1492 : : return false;
1493 : 15891052 : if (!constexpr_fundef_hasher::equal (lhs->fundef, rhs->fundef))
1494 : : return false;
1495 : 15891052 : return cp_tree_equal (lhs->bindings, rhs->bindings);
1496 : : }
1497 : :
1498 : : /* Initialize the constexpr call table, if needed. */
1499 : :
1500 : : static void
1501 : 18973007 : maybe_initialize_constexpr_call_table (void)
1502 : : {
1503 : 18973007 : if (constexpr_call_table == NULL)
1504 : 17007 : constexpr_call_table = hash_table<constexpr_call_hasher>::create_ggc (101);
1505 : 18973007 : }
1506 : :
1507 : : /* During constexpr CALL_EXPR evaluation, to avoid issues with sharing when
1508 : : a function happens to get called recursively, we unshare the callee
1509 : : function's body and evaluate this unshared copy instead of evaluating the
1510 : : original body.
1511 : :
1512 : : FUNDEF_COPIES_TABLE is a per-function freelist of these unshared function
1513 : : copies. The underlying data structure of FUNDEF_COPIES_TABLE is a hash_map
1514 : : that's keyed off of the original FUNCTION_DECL and whose value is a
1515 : : TREE_LIST of this function's unused copies awaiting reuse.
1516 : :
1517 : : This is not GC-deletable to avoid GC affecting UID generation. */
1518 : :
1519 : : static GTY(()) decl_tree_map *fundef_copies_table;
1520 : :
1521 : : /* Reuse a copy or create a new unshared copy of the function FUN.
1522 : : Return this copy. We use a TREE_LIST whose PURPOSE is body, VALUE
1523 : : is parms, TYPE is result. */
1524 : :
1525 : : static tree
1526 : 26340119 : get_fundef_copy (constexpr_fundef *fundef)
1527 : : {
1528 : 26340119 : tree copy;
1529 : 26340119 : bool existed;
1530 : 26340119 : tree *slot = &(hash_map_safe_get_or_insert<hm_ggc>
1531 : 26340119 : (fundef_copies_table, fundef->decl, &existed, 127));
1532 : :
1533 : 26340119 : if (!existed)
1534 : : {
1535 : : /* There is no cached function available, or in use. We can use
1536 : : the function directly. That the slot is now created records
1537 : : that this function is now in use. */
1538 : 4328479 : copy = build_tree_list (fundef->body, fundef->parms);
1539 : 4328479 : TREE_TYPE (copy) = fundef->result;
1540 : : }
1541 : 22011640 : else if (*slot == NULL_TREE)
1542 : : {
1543 : 4341 : if (uid_sensitive_constexpr_evaluation_p ())
1544 : 0 : return NULL_TREE;
1545 : :
1546 : : /* We've already used the function itself, so make a copy. */
1547 : 4341 : copy = build_tree_list (NULL, NULL);
1548 : 4341 : tree saved_body = DECL_SAVED_TREE (fundef->decl);
1549 : 4341 : tree saved_parms = DECL_ARGUMENTS (fundef->decl);
1550 : 4341 : tree saved_result = DECL_RESULT (fundef->decl);
1551 : 4341 : tree saved_fn = current_function_decl;
1552 : 4341 : DECL_SAVED_TREE (fundef->decl) = fundef->body;
1553 : 4341 : DECL_ARGUMENTS (fundef->decl) = fundef->parms;
1554 : 4341 : DECL_RESULT (fundef->decl) = fundef->result;
1555 : 4341 : current_function_decl = fundef->decl;
1556 : 4341 : TREE_PURPOSE (copy) = copy_fn (fundef->decl, TREE_VALUE (copy),
1557 : 4341 : TREE_TYPE (copy));
1558 : 4341 : current_function_decl = saved_fn;
1559 : 4341 : DECL_RESULT (fundef->decl) = saved_result;
1560 : 4341 : DECL_ARGUMENTS (fundef->decl) = saved_parms;
1561 : 4341 : DECL_SAVED_TREE (fundef->decl) = saved_body;
1562 : : }
1563 : : else
1564 : : {
1565 : : /* We have a cached function available. */
1566 : 22007299 : copy = *slot;
1567 : 22007299 : *slot = TREE_CHAIN (copy);
1568 : : }
1569 : :
1570 : : return copy;
1571 : : }
1572 : :
1573 : : /* Save the copy COPY of function FUN for later reuse by
1574 : : get_fundef_copy(). By construction, there will always be an entry
1575 : : to find. */
1576 : :
1577 : : static void
1578 : 26340119 : save_fundef_copy (tree fun, tree copy)
1579 : : {
1580 : 26340119 : tree *slot = fundef_copies_table->get (fun);
1581 : 26340119 : TREE_CHAIN (copy) = *slot;
1582 : 26340119 : *slot = copy;
1583 : 26340119 : }
1584 : :
1585 : : /* Whether our evaluation wants a prvalue (e.g. CONSTRUCTOR or _CST),
1586 : : a glvalue (e.g. VAR_DECL or _REF), or nothing. */
1587 : :
1588 : : enum value_cat {
1589 : : vc_prvalue = 0,
1590 : : vc_glvalue = 1,
1591 : : vc_discard = 2
1592 : : };
1593 : :
1594 : : static tree cxx_eval_constant_expression (const constexpr_ctx *, tree,
1595 : : value_cat, bool *, bool *, tree *);
1596 : : static tree cxx_eval_bare_aggregate (const constexpr_ctx *, tree,
1597 : : value_cat, bool *, bool *, tree *);
1598 : : static tree cxx_fold_indirect_ref (const constexpr_ctx *, location_t, tree, tree,
1599 : : bool *, tree *);
1600 : : static tree find_heap_var_refs (tree *, int *, void *);
1601 : :
1602 : : /* For exception object EXC if it has class type and usable what () method
1603 : : which returns cv char * return the xmalloced string literal which it returns
1604 : : if possible, otherwise return NULL. */
1605 : :
1606 : : static char *
1607 : 86 : exception_what_str (const constexpr_ctx *ctx, tree exc)
1608 : : {
1609 : 86 : tree type = strip_array_types (TREE_TYPE (exc));
1610 : 86 : if (!CLASS_TYPE_P (type))
1611 : : return NULL;
1612 : 49 : tree std_exception = lookup_qualified_name (std_node, "exception",
1613 : : LOOK_want::NORMAL, false);
1614 : 49 : if (TREE_CODE (std_exception) != TYPE_DECL)
1615 : : return NULL;
1616 : 46 : if (!CLASS_TYPE_P (TREE_TYPE (std_exception)))
1617 : : return NULL;
1618 : 46 : base_kind b_kind;
1619 : 46 : tree binfo = lookup_base (type, TREE_TYPE (std_exception), ba_check, &b_kind,
1620 : : tf_none);
1621 : 46 : if (binfo == NULL_TREE || binfo == error_mark_node)
1622 : : return NULL;
1623 : 42 : if (type != TREE_TYPE (exc))
1624 : 4 : exc = build4 (ARRAY_REF, type, exc, size_zero_node, NULL, NULL);
1625 : 42 : tree call
1626 : 42 : = finish_class_member_access_expr (exc, get_identifier ("what"), false,
1627 : : tf_none);
1628 : 42 : if (call == error_mark_node)
1629 : : return NULL;
1630 : 42 : releasing_vec what_args;
1631 : 42 : call = finish_call_expr (call, &what_args, false, false, tf_none);
1632 : 42 : if (call == error_mark_node)
1633 : : return NULL;
1634 : 42 : if (TREE_CODE (TREE_TYPE (call)) != POINTER_TYPE
1635 : 42 : || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (call)))
1636 : 42 : || !COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (call)))
1637 : 42 : || !tree_int_cst_equal (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (call))),
1638 : 42 : TYPE_SIZE_UNIT (char_type_node))
1639 : 84 : || TYPE_PRECISION (TREE_TYPE (TREE_TYPE (call))) != BITS_PER_UNIT)
1640 : 0 : return NULL;
1641 : 42 : if (!potential_constant_expression (call))
1642 : : return NULL;
1643 : 42 : bool non_constant_p = false, overflow_p = false;
1644 : 42 : tree jmp_target = NULL;
1645 : 42 : tree ptr = cxx_eval_constant_expression (ctx, call, vc_prvalue,
1646 : : &non_constant_p, &overflow_p,
1647 : : &jmp_target);
1648 : 42 : if (throws (&jmp_target) || non_constant_p)
1649 : : return NULL;
1650 : 42 : if (reduced_constant_expression_p (ptr))
1651 : 40 : if (const char *msg = c_getstr (ptr))
1652 : 40 : return xstrdup (msg);
1653 : 2 : auto_vec <char, 32> v;
1654 : 26 : for (unsigned i = 0; i < INT_MAX; ++i)
1655 : : {
1656 : 26 : tree t = call;
1657 : 26 : if (i)
1658 : 24 : t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (ptr), ptr, size_int (i));
1659 : 26 : t = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (t)), t);
1660 : 26 : non_constant_p = false;
1661 : 26 : overflow_p = false;
1662 : 26 : jmp_target = NULL;
1663 : 26 : tree t2 = cxx_eval_constant_expression (ctx, t, vc_prvalue,
1664 : : &non_constant_p, &overflow_p,
1665 : : &jmp_target);
1666 : 26 : if (throws (&jmp_target)
1667 : 26 : || non_constant_p
1668 : 26 : || !tree_fits_shwi_p (t2))
1669 : 0 : return NULL;
1670 : 26 : char c = tree_to_shwi (t2);
1671 : 26 : v.safe_push (c);
1672 : 26 : if (c == '\0')
1673 : : break;
1674 : : }
1675 : 4 : return xstrdup (v.address ());
1676 : 44 : }
1677 : :
1678 : : /* Diagnose constant expression evaluation encountering call to
1679 : : std::terminate due to exception EXC. */
1680 : :
1681 : : static void
1682 : 11 : diagnose_std_terminate (location_t loc, const constexpr_ctx *ctx, tree exc)
1683 : : {
1684 : 11 : tree type = strip_array_types (TREE_TYPE (exc));
1685 : 11 : if (char *str = exception_what_str (ctx, exc))
1686 : : {
1687 : 2 : error_at (loc, "%qs called after throwing an exception of type %qT; "
1688 : : "%<what()%>: %qs", "std::terminate", type, str);
1689 : 2 : free (str);
1690 : : }
1691 : : else
1692 : : {
1693 : 9 : if (type != TREE_TYPE (exc))
1694 : 9 : exc = build4 (ARRAY_REF, type, exc, size_zero_node, NULL, NULL);
1695 : 9 : bool non_constant_p = false, overflow_p = false;
1696 : 9 : tree jmp_target = NULL;
1697 : 9 : tree val = cxx_eval_constant_expression (ctx, exc, vc_prvalue,
1698 : : &non_constant_p, &overflow_p,
1699 : : &jmp_target);
1700 : 9 : gcc_assert (!throws (&jmp_target) && !non_constant_p);
1701 : 9 : if (reduced_constant_expression_p (val))
1702 : 9 : error_at (loc, "%qs called after throwing an exception %qE",
1703 : : "std::terminate", val);
1704 : : else
1705 : 0 : error_at (loc, "%qs called after throwing an exception of type %qT",
1706 : : "std::terminate", type);
1707 : : }
1708 : 11 : }
1709 : :
1710 : : /* Diagnose constant expression evaluation encountering call to
1711 : : uncaught exception EXC. */
1712 : :
1713 : : static void
1714 : 75 : diagnose_uncaught_exception (location_t loc, const constexpr_ctx *ctx, tree exc)
1715 : : {
1716 : 75 : tree type = strip_array_types (TREE_TYPE (exc));
1717 : 75 : if (char *str = exception_what_str (ctx, exc))
1718 : : {
1719 : 40 : error_at (loc, "uncaught exception of type %qT; %<what()%>: %qs", type, str);
1720 : 40 : free (str);
1721 : : }
1722 : : else
1723 : : {
1724 : 35 : if (type != TREE_TYPE (exc))
1725 : 35 : exc = build4 (ARRAY_REF, type, exc, size_zero_node, NULL, NULL);
1726 : 35 : bool non_constant_p = false, overflow_p = false;
1727 : 35 : tree jmp_target = NULL;
1728 : 35 : tree val = cxx_eval_constant_expression (ctx, exc, vc_prvalue,
1729 : : &non_constant_p, &overflow_p,
1730 : : &jmp_target);
1731 : 35 : gcc_assert (!throws (&jmp_target) && !non_constant_p);
1732 : 35 : if (reduced_constant_expression_p (val))
1733 : 34 : error_at (loc, "uncaught exception %qE", val);
1734 : : else
1735 : 1 : error_at (loc, "uncaught exception of type %qT", type);
1736 : : }
1737 : 75 : }
1738 : :
1739 : : /* Kinds of __cxa_* functions (and a few other EH related ones) we handle as
1740 : : magic constexpr functions for C++26. */
1741 : :
1742 : : enum cxa_builtin {
1743 : : CXA_NONE = 0,
1744 : : CXA_ALLOCATE_EXCEPTION = 1,
1745 : : CXA_FREE_EXCEPTION = 2,
1746 : : CXA_THROW = 3,
1747 : : CXA_BEGIN_CATCH = 4,
1748 : : CXA_END_CATCH = 5,
1749 : : CXA_RETHROW = 6,
1750 : : CXA_GET_EXCEPTION_PTR = 7,
1751 : : CXA_BAD_CAST = 8,
1752 : : CXA_BAD_TYPEID = 9,
1753 : : CXA_THROW_BAD_ARRAY_NEW_LENGTH = 10,
1754 : : STD_UNCAUGHT_EXCEPTIONS = 11,
1755 : : STD_CURRENT_EXCEPTION = 12,
1756 : : STD_RETHROW_EXCEPTION = 13,
1757 : : BUILTIN_EH_PTR_ADJUST_REF = 14
1758 : : };
1759 : :
1760 : : /* Return cxa_builtin if FNDECL is a __cxa_* function handled as
1761 : : magic constexpr function for C++26. Return CXA_NONE otherwise. */
1762 : :
1763 : : static enum cxa_builtin
1764 : 30064082 : cxx_cxa_builtin_fn_p (tree fndecl)
1765 : : {
1766 : 30064082 : if (cxx_dialect < cxx26)
1767 : : return CXA_NONE;
1768 : 4826356 : if (DECL_LANGUAGE (fndecl) != lang_c)
1769 : : {
1770 : 4420594 : if (!decl_in_std_namespace_p (fndecl))
1771 : : return CXA_NONE;
1772 : 1790878 : if (id_equal (DECL_NAME (fndecl), "uncaught_exceptions"))
1773 : : return STD_UNCAUGHT_EXCEPTIONS;
1774 : 1790757 : if (id_equal (DECL_NAME (fndecl), "current_exception"))
1775 : : return STD_CURRENT_EXCEPTION;
1776 : 1775349 : if (id_equal (DECL_NAME (fndecl), "rethrow_exception"))
1777 : : return STD_RETHROW_EXCEPTION;
1778 : : return CXA_NONE;
1779 : : }
1780 : 405762 : if (!startswith (IDENTIFIER_POINTER (DECL_NAME (fndecl)), "__cxa_"))
1781 : : return CXA_NONE;
1782 : 36265 : if (id_equal (DECL_NAME (fndecl), "__cxa_allocate_exception"))
1783 : : return CXA_ALLOCATE_EXCEPTION;
1784 : 6431 : if (id_equal (DECL_NAME (fndecl), "__cxa_free_exception"))
1785 : : return CXA_FREE_EXCEPTION;
1786 : 6430 : if (id_equal (DECL_NAME (fndecl), "__cxa_throw"))
1787 : : return CXA_THROW;
1788 : 5994 : if (id_equal (DECL_NAME (fndecl), "__cxa_begin_catch"))
1789 : : return CXA_BEGIN_CATCH;
1790 : 1095 : if (id_equal (DECL_NAME (fndecl), "__cxa_end_catch"))
1791 : : return CXA_END_CATCH;
1792 : 922 : if (id_equal (DECL_NAME (fndecl), "__cxa_rethrow"))
1793 : : return CXA_RETHROW;
1794 : 866 : if (id_equal (DECL_NAME (fndecl), "__cxa_get_exception_ptr"))
1795 : : return CXA_GET_EXCEPTION_PTR;
1796 : 846 : if (id_equal (DECL_NAME (fndecl), "__cxa_bad_cast"))
1797 : : return CXA_BAD_CAST;
1798 : 562 : if (id_equal (DECL_NAME (fndecl), "__cxa_bad_typeid"))
1799 : : return CXA_BAD_TYPEID;
1800 : 554 : if (id_equal (DECL_NAME (fndecl), "__cxa_throw_bad_array_new_length"))
1801 : : return CXA_THROW_BAD_ARRAY_NEW_LENGTH;
1802 : : return CXA_NONE;
1803 : : }
1804 : :
1805 : : /* Helper function for cxx_eval_cxa_builtin_fn.
1806 : : Check if ARG is a valid first argument of __cxa_throw or
1807 : : __cxa_free_exception or __builtin_eh_ptr_adjust_ref. Return NULL_TREE if
1808 : : not, otherwise return the artificial __cxa_allocate_exception allocated
1809 : : VAR_DECL. FREE_EXC is true for __cxa_free_exception, false otherwise. */
1810 : :
1811 : : static tree
1812 : 396 : cxa_check_throw_arg (tree arg, bool free_exc)
1813 : : {
1814 : 396 : STRIP_NOPS (arg);
1815 : 396 : if (TREE_CODE (arg) != ADDR_EXPR)
1816 : : return NULL_TREE;
1817 : 396 : arg = TREE_OPERAND (arg, 0);
1818 : 396 : if (!VAR_P (arg)
1819 : 396 : || !DECL_ARTIFICIAL (arg)
1820 : 396 : || ((!free_exc || DECL_NAME (arg) != heap_uninit_identifier)
1821 : 396 : && DECL_NAME (arg) != heap_identifier)
1822 : 792 : || !DECL_LANG_SPECIFIC (arg))
1823 : 0 : return NULL_TREE;
1824 : : return arg;
1825 : : }
1826 : :
1827 : : /* Helper function for cxx_eval_cxa_builtin_fn.
1828 : : "Allocate" on the constexpr heap an exception object of TYPE
1829 : : with REFCOUNT. */
1830 : :
1831 : : static tree
1832 : 14370 : cxa_allocate_exception (location_t loc, const constexpr_ctx *ctx, tree type,
1833 : : tree refcount)
1834 : : {
1835 : 14370 : tree var = build_decl (loc, VAR_DECL, heap_uninit_identifier, type);
1836 : 14370 : DECL_ARTIFICIAL (var) = 1;
1837 : 14370 : retrofit_lang_decl (var);
1838 : 14370 : DECL_EXCEPTION_REFCOUNT (var) = refcount;
1839 : 14370 : ctx->global->heap_vars.safe_push (var);
1840 : 14370 : return var;
1841 : : }
1842 : :
1843 : : /* Evaluate various __cxa_* calls as magic constexpr builtins for
1844 : : C++26 constexpr exception support (P3068R5). */
1845 : :
1846 : : static tree
1847 : 23836 : cxx_eval_cxa_builtin_fn (const constexpr_ctx *ctx, tree call,
1848 : : enum cxa_builtin kind, tree fndecl,
1849 : : bool *non_constant_p, bool *overflow_p,
1850 : : tree *jump_target)
1851 : : {
1852 : 23836 : int nargs = call_expr_nargs (call);
1853 : 23836 : location_t loc = cp_expr_loc_or_input_loc (call);
1854 : 23836 : tree args[4], arg;
1855 : 23836 : if (nargs > 4)
1856 : : {
1857 : 0 : invalid_nargs:
1858 : 0 : if (!ctx->quiet)
1859 : 0 : error_at (loc, "call to %qD function with incorrect"
1860 : : "number of arguments", fndecl);
1861 : 0 : *non_constant_p = true;
1862 : 0 : return call;
1863 : : }
1864 : 23836 : if ((kind == CXA_BEGIN_CATCH || kind == CXA_GET_EXCEPTION_PTR)
1865 : 2553 : && nargs == 1
1866 : 2553 : && (arg = CALL_EXPR_ARG (call, 0))
1867 : 2553 : && TREE_CODE (arg) == CALL_EXPR
1868 : 2553 : && call_expr_nargs (arg) == 1
1869 : 26389 : && integer_zerop (CALL_EXPR_ARG (arg, 0)))
1870 : 2553 : if (tree fun = get_function_named_in_call (arg))
1871 : 2553 : if (fndecl_built_in_p (fun, BUILT_IN_EH_POINTER))
1872 : : {
1873 : 2553 : if (ctx->global->caught_exceptions.length () < 2)
1874 : : {
1875 : 2366 : no_caught_exceptions:
1876 : 2366 : if (!ctx->quiet)
1877 : 0 : error_at (loc, "%qD called with no caught exceptions pending",
1878 : : fndecl);
1879 : 2366 : *non_constant_p = true;
1880 : 2366 : return call;
1881 : : }
1882 : : /* Both __cxa_get_exception_ptr (__builtin_eh_pointer (0))
1883 : : and __cxa_begin_catch (__builtin_eh_pointer (0)) calls expect
1884 : : ctx->global->caught_exceptions vector to end with
1885 : : __cxa_allocate_exception created artificial VAR_DECL (the
1886 : : exception object) followed by handler type, pushed by TRY_BLOCK
1887 : : evaluation. The only difference between the functions is that
1888 : : __cxa_begin_catch pops the handler type from the vector and keeps
1889 : : the VAR_DECL last and decreases uncaught_exceptions. The
1890 : : VAR_DECL after __cxa_begin_catch serves as the current exception
1891 : : and is then popped in __cxa_end_catch evaluation. */
1892 : 187 : tree handler_type = ctx->global->caught_exceptions.last ();
1893 : 187 : if (handler_type && VAR_P (handler_type))
1894 : 0 : goto no_caught_exceptions;
1895 : 187 : unsigned idx = ctx->global->caught_exceptions.length () - 2;
1896 : 187 : arg = ctx->global->caught_exceptions[idx];
1897 : 187 : gcc_assert (VAR_P (arg));
1898 : 187 : if (kind == CXA_BEGIN_CATCH)
1899 : : {
1900 : 175 : ctx->global->caught_exceptions.pop ();
1901 : 175 : --ctx->global->uncaught_exceptions;
1902 : : }
1903 : 187 : if (handler_type == NULL_TREE)
1904 : : /* Used for catch (...). Just return void. */
1905 : 32 : return void_node;
1906 : 155 : else if (POINTER_TYPE_P (handler_type))
1907 : : {
1908 : : /* Used for catch of a pointer. */
1909 : 33 : if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE)
1910 : 33 : arg = build4 (ARRAY_REF, TREE_TYPE (TREE_TYPE (arg)), arg,
1911 : : size_zero_node, NULL_TREE, NULL_TREE);
1912 : 33 : arg = cp_convert (handler_type, arg,
1913 : 33 : ctx->quiet ? tf_none : tf_warning_or_error);
1914 : 33 : if (arg == error_mark_node)
1915 : : {
1916 : 0 : *non_constant_p = true;
1917 : 0 : return call;
1918 : : }
1919 : : }
1920 : : else
1921 : : {
1922 : : /* Used for catch of a non-pointer type. */
1923 : 122 : tree exc_type = strip_array_types (TREE_TYPE (arg));
1924 : 122 : tree exc_ptr_type = build_pointer_type (exc_type);
1925 : 122 : arg = build_fold_addr_expr_with_type (arg, exc_ptr_type);
1926 : 122 : if (CLASS_TYPE_P (handler_type))
1927 : : {
1928 : 81 : tree ptr_type = build_pointer_type (handler_type);
1929 : 81 : arg = cp_convert (ptr_type, arg,
1930 : 81 : ctx->quiet ? tf_none
1931 : : : tf_warning_or_error);
1932 : 81 : if (arg == error_mark_node)
1933 : : {
1934 : 0 : *non_constant_p = true;
1935 : 0 : return call;
1936 : : }
1937 : : }
1938 : : }
1939 : 155 : return cxx_eval_constant_expression (ctx, arg, vc_prvalue,
1940 : : non_constant_p, overflow_p,
1941 : 155 : jump_target);
1942 : : }
1943 : 36596 : for (int i = 0; i < nargs; ++i)
1944 : : {
1945 : 15313 : args[i] = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (call, i),
1946 : : vc_prvalue, non_constant_p,
1947 : : overflow_p, jump_target);
1948 : 15313 : if (*non_constant_p)
1949 : : return call;
1950 : 15313 : if (*jump_target)
1951 : : return NULL_TREE;
1952 : : }
1953 : 21283 : switch (kind)
1954 : : {
1955 : 14242 : case CXA_ALLOCATE_EXCEPTION:
1956 : 14242 : if (nargs != 1)
1957 : 0 : goto invalid_nargs;
1958 : 14242 : if (!tree_fits_uhwi_p (args[0]))
1959 : : {
1960 : 0 : if (!ctx->quiet)
1961 : 0 : error_at (loc, "cannot allocate exception: size not constant");
1962 : 0 : *non_constant_p = true;
1963 : 0 : return call;
1964 : : }
1965 : : else
1966 : : {
1967 : 28484 : tree type = build_array_type_nelts (char_type_node,
1968 : 14242 : tree_to_uhwi (args[0]));
1969 : 14242 : tree var = cxa_allocate_exception (loc, ctx, type, size_zero_node);
1970 : 14242 : ctx->global->put_value (var, NULL_TREE);
1971 : 14242 : return fold_convert (ptr_type_node, build_address (var));
1972 : : }
1973 : 1 : case CXA_FREE_EXCEPTION:
1974 : 1 : if (nargs != 1)
1975 : 0 : goto invalid_nargs;
1976 : 1 : arg = cxa_check_throw_arg (args[0], true);
1977 : 1 : if (arg == NULL_TREE)
1978 : : {
1979 : 0 : invalid_ptr:
1980 : 0 : if (!ctx->quiet)
1981 : 0 : error_at (loc, "first argument to %qD function not result of "
1982 : : "%<__cxa_allocate_exception%>", fndecl);
1983 : 0 : *non_constant_p = true;
1984 : 0 : return call;
1985 : : }
1986 : 1 : DECL_NAME (arg) = heap_deleted_identifier;
1987 : 1 : ctx->global->destroy_value (arg);
1988 : 1 : ctx->global->heap_dealloc_count++;
1989 : 1 : return void_node;
1990 : 302 : case CXA_THROW:
1991 : 302 : if (nargs != 3)
1992 : 0 : goto invalid_nargs;
1993 : 302 : arg = cxa_check_throw_arg (args[0], false);
1994 : 302 : if (arg == NULL_TREE)
1995 : 0 : goto invalid_ptr;
1996 : 302 : DECL_EXCEPTION_REFCOUNT (arg)
1997 : 302 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg),
1998 : : size_one_node);
1999 : 302 : ++ctx->global->uncaught_exceptions;
2000 : 302 : *jump_target = arg;
2001 : 302 : return void_node;
2002 : 0 : case CXA_BEGIN_CATCH:
2003 : 0 : case CXA_GET_EXCEPTION_PTR:
2004 : 0 : goto invalid_nargs;
2005 : 173 : case CXA_END_CATCH:
2006 : 173 : if (nargs != 0)
2007 : 0 : goto invalid_nargs;
2008 : 173 : if (ctx->global->caught_exceptions.is_empty ())
2009 : : {
2010 : 0 : no_active_exc:
2011 : 0 : if (!ctx->quiet)
2012 : 0 : error_at (loc, "%qD called with no caught exceptions active",
2013 : : fndecl);
2014 : 0 : *non_constant_p = true;
2015 : 0 : return call;
2016 : : }
2017 : : else
2018 : : {
2019 : 173 : arg = ctx->global->caught_exceptions.pop ();
2020 : 173 : if (arg == NULL_TREE || !VAR_P (arg))
2021 : 0 : goto no_active_exc;
2022 : 173 : free_except:
2023 : 216 : DECL_EXCEPTION_REFCOUNT (arg)
2024 : 216 : = size_binop (MINUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg),
2025 : : size_one_node);
2026 : 216 : if (integer_zerop (DECL_EXCEPTION_REFCOUNT (arg)))
2027 : : {
2028 : 110 : if (type_build_dtor_call (TREE_TYPE (arg)))
2029 : : {
2030 : 40 : tree cleanup
2031 : 40 : = cxx_maybe_build_cleanup (arg, (ctx->quiet ? tf_none
2032 : : : tf_warning_or_error));
2033 : 40 : if (cleanup == error_mark_node)
2034 : 0 : *non_constant_p = true;
2035 : 40 : tree jmp_target = NULL_TREE;
2036 : 40 : cxx_eval_constant_expression (ctx, cleanup, vc_discard,
2037 : : non_constant_p, overflow_p,
2038 : : &jmp_target);
2039 : 40 : if (throws (&jmp_target))
2040 : 0 : *jump_target = jmp_target;
2041 : : }
2042 : 110 : DECL_NAME (arg) = heap_deleted_identifier;
2043 : 110 : ctx->global->destroy_value (arg);
2044 : 110 : ctx->global->heap_dealloc_count++;
2045 : : }
2046 : : }
2047 : 216 : return void_node;
2048 : 50 : case CXA_RETHROW:
2049 : 50 : if (nargs != 0)
2050 : 0 : goto invalid_nargs;
2051 : 50 : unsigned idx;
2052 : 100 : FOR_EACH_VEC_ELT_REVERSE (ctx->global->caught_exceptions, idx, arg)
2053 : 36 : if (arg == NULL_TREE || !VAR_P (arg))
2054 : 0 : --idx;
2055 : : else
2056 : : break;
2057 : 50 : if (arg == NULL_TREE)
2058 : : {
2059 : 14 : if (!ctx->quiet)
2060 : 4 : error_at (loc, "%qD called with no caught exceptions active",
2061 : : fndecl);
2062 : 14 : *non_constant_p = true;
2063 : 14 : return call;
2064 : : }
2065 : 36 : DECL_EXCEPTION_REFCOUNT (arg)
2066 : 36 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg), size_one_node);
2067 : 36 : ++ctx->global->uncaught_exceptions;
2068 : 36 : *jump_target = arg;
2069 : 36 : return void_node;
2070 : 143 : case CXA_BAD_CAST:
2071 : 143 : case CXA_BAD_TYPEID:
2072 : 143 : case CXA_THROW_BAD_ARRAY_NEW_LENGTH:
2073 : 143 : if (nargs != 0)
2074 : 0 : goto invalid_nargs;
2075 : : else
2076 : : {
2077 : 143 : tree name;
2078 : 143 : switch (kind)
2079 : : {
2080 : 117 : case CXA_BAD_CAST:
2081 : 117 : name = get_identifier ("bad_cast");
2082 : 117 : break;
2083 : 5 : case CXA_BAD_TYPEID:
2084 : 5 : name = get_identifier ("bad_typeid");
2085 : 5 : break;
2086 : 21 : case CXA_THROW_BAD_ARRAY_NEW_LENGTH:
2087 : 21 : name = get_identifier ("bad_array_new_length");
2088 : 21 : break;
2089 : : default:
2090 : : gcc_unreachable ();
2091 : : }
2092 : 143 : tree decl = lookup_qualified_name (std_node, name);
2093 : 143 : if (TREE_CODE (decl) != TYPE_DECL
2094 : 128 : || !CLASS_TYPE_P (TREE_TYPE (decl))
2095 : 271 : || !type_build_ctor_call (TREE_TYPE (decl)))
2096 : : {
2097 : 15 : if (!ctx->quiet)
2098 : 4 : error_at (loc, "%qD called without %<std::%D%> being defined",
2099 : : fndecl, name);
2100 : 15 : *non_constant_p = true;
2101 : 15 : return call;
2102 : : }
2103 : 128 : tree type = TREE_TYPE (decl);
2104 : 128 : tree var = cxa_allocate_exception (loc, ctx, type, size_one_node);
2105 : 128 : tree ctor
2106 : 128 : = build_special_member_call (var, complete_ctor_identifier,
2107 : : NULL, type, LOOKUP_NORMAL,
2108 : 128 : ctx->quiet ? tf_none
2109 : : : tf_warning_or_error);
2110 : 128 : if (ctor == error_mark_node)
2111 : : {
2112 : 0 : *non_constant_p = true;
2113 : 0 : return call;
2114 : : }
2115 : 128 : if (TREE_CONSTANT (ctor))
2116 : 0 : ctx->global->put_value (var, ctor);
2117 : : else
2118 : : {
2119 : 128 : ctx->global->put_value (var, NULL_TREE);
2120 : 128 : cxx_eval_constant_expression (ctx, ctor, vc_discard,
2121 : : non_constant_p, overflow_p,
2122 : : jump_target);
2123 : 128 : if (*non_constant_p)
2124 : : return call;
2125 : 128 : if (throws (jump_target))
2126 : : return NULL_TREE;
2127 : : }
2128 : 128 : ++ctx->global->uncaught_exceptions;
2129 : 128 : *jump_target = var;
2130 : : }
2131 : 128 : return void_node;
2132 : 59 : case STD_UNCAUGHT_EXCEPTIONS:
2133 : 59 : if (nargs != 0)
2134 : 0 : goto invalid_nargs;
2135 : : /* Similarly to __builtin_is_constant_evaluated (), we don't
2136 : : want to give a definite answer during mce_unknown evaluation,
2137 : : because that might prevent evaluation later on when some
2138 : : exceptions might be uncaught. But unlike that, we don't
2139 : : want to constant fold it even during cp_fold, because at runtime
2140 : : std::uncaught_exceptions () might still be non-zero. */
2141 : 59 : if (ctx->manifestly_const_eval != mce_true)
2142 : : {
2143 : 38 : *non_constant_p = true;
2144 : 38 : return call;
2145 : : }
2146 : 21 : return build_int_cst (integer_type_node,
2147 : 21 : ctx->global->uncaught_exceptions);
2148 : 6220 : case STD_CURRENT_EXCEPTION:
2149 : 6220 : if (nargs != 0)
2150 : 0 : goto invalid_nargs;
2151 : : else
2152 : : {
2153 : 6220 : tree name = get_identifier ("exception_ptr");
2154 : 6220 : tree decl = lookup_qualified_name (std_node, name);
2155 : 6220 : tree fld;
2156 : 6220 : if (TREE_CODE (decl) != TYPE_DECL
2157 : 6220 : || !CLASS_TYPE_P (TREE_TYPE (decl))
2158 : 6220 : || !COMPLETE_TYPE_P (TREE_TYPE (decl))
2159 : 6220 : || !(fld = next_aggregate_field (TYPE_FIELDS (TREE_TYPE (decl))))
2160 : 6220 : || DECL_ARTIFICIAL (fld)
2161 : 6220 : || TREE_CODE (TREE_TYPE (fld)) != POINTER_TYPE
2162 : 6220 : || next_aggregate_field (DECL_CHAIN (fld))
2163 : 12440 : || !tree_int_cst_equal (TYPE_SIZE (TREE_TYPE (decl)),
2164 : 6220 : TYPE_SIZE (TREE_TYPE (fld))))
2165 : : {
2166 : 0 : if (!ctx->quiet)
2167 : 0 : error_at (loc, "%qD called without supportable %qs",
2168 : : fndecl, "std::exception_ptr");
2169 : 0 : *non_constant_p = true;
2170 : 0 : return call;
2171 : : }
2172 : 12440 : FOR_EACH_VEC_ELT_REVERSE (ctx->global->caught_exceptions, idx, arg)
2173 : 21 : if (arg == NULL_TREE || !VAR_P (arg))
2174 : 0 : --idx;
2175 : : else
2176 : : break;
2177 : : /* Similarly to __builtin_is_constant_evaluated (), we don't
2178 : : want to give a definite answer during mce_unknown evaluation,
2179 : : because that might prevent evaluation later on when some
2180 : : exceptions might be current. But unlike that, we don't
2181 : : want to constant fold it to null even during cp_fold, because
2182 : : at runtime std::current_exception () might still be non-null. */
2183 : 6220 : if (ctx->manifestly_const_eval != mce_true && arg == NULL_TREE)
2184 : : {
2185 : 6188 : *non_constant_p = true;
2186 : 6188 : return call;
2187 : : }
2188 : 32 : if (arg == NULL_TREE)
2189 : 11 : arg = build_zero_cst (TREE_TYPE (fld));
2190 : : else
2191 : : {
2192 : 21 : DECL_EXCEPTION_REFCOUNT (arg)
2193 : 21 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg),
2194 : : size_one_node);
2195 : 21 : arg = fold_convert (ptr_type_node, build_address (arg));
2196 : : }
2197 : 32 : return build_constructor_single (TREE_TYPE (decl), fld, arg);
2198 : : }
2199 : 22 : case STD_RETHROW_EXCEPTION:
2200 : 22 : if (nargs != 1)
2201 : 0 : goto invalid_nargs;
2202 : 22 : if (TYPE_REF_P (TREE_TYPE (args[0])))
2203 : : {
2204 : 22 : arg = args[0];
2205 : 22 : STRIP_NOPS (arg);
2206 : 22 : if (TREE_CODE (arg) == ADDR_EXPR)
2207 : : {
2208 : 22 : args[0]
2209 : 22 : = cxx_eval_constant_expression (ctx, TREE_OPERAND (arg, 0),
2210 : : vc_prvalue, non_constant_p,
2211 : : overflow_p, jump_target);
2212 : 22 : if (*non_constant_p)
2213 : : return call;
2214 : 22 : if (*jump_target)
2215 : : return NULL_TREE;
2216 : : }
2217 : : }
2218 : 22 : if (TREE_CODE (args[0]) != CONSTRUCTOR
2219 : 22 : || CONSTRUCTOR_NELTS (args[0]) != 1)
2220 : : {
2221 : 0 : invalid_std_rethrow:
2222 : 0 : if (!ctx->quiet)
2223 : 0 : error_at (loc, "%qD called with unexpected %qs argument",
2224 : : fndecl, "std::exception_ptr");
2225 : 0 : *non_constant_p = true;
2226 : 0 : return void_node;
2227 : : }
2228 : 22 : arg = cxa_check_throw_arg (CONSTRUCTOR_ELT (args[0], 0)->value, false);
2229 : 22 : if (arg == NULL_TREE)
2230 : 0 : goto invalid_std_rethrow;
2231 : 22 : DECL_EXCEPTION_REFCOUNT (arg)
2232 : 22 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg), size_one_node);
2233 : 22 : ++ctx->global->uncaught_exceptions;
2234 : 22 : *jump_target = arg;
2235 : 22 : return void_node;
2236 : 71 : case BUILTIN_EH_PTR_ADJUST_REF:
2237 : 71 : if (nargs != 2)
2238 : 0 : goto invalid_nargs;
2239 : 71 : arg = cxa_check_throw_arg (args[0], false);
2240 : 71 : if (arg == NULL_TREE)
2241 : 0 : goto invalid_ptr;
2242 : 71 : if (integer_onep (args[1]))
2243 : 28 : DECL_EXCEPTION_REFCOUNT (arg)
2244 : 56 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg),
2245 : : size_one_node);
2246 : 43 : else if (integer_minus_onep (args[1]))
2247 : 43 : goto free_except;
2248 : : else
2249 : : {
2250 : 0 : if (!ctx->quiet)
2251 : 0 : error_at (loc, "%qD called with second argument "
2252 : : "other than 1 or -1", fndecl);
2253 : 0 : *non_constant_p = true;
2254 : : }
2255 : 28 : return void_node;
2256 : 0 : default:
2257 : 0 : gcc_unreachable ();
2258 : : }
2259 : : }
2260 : :
2261 : : /* Attempt to evaluate T which represents a call to a builtin function.
2262 : : We assume here that all builtin functions evaluate to scalar types
2263 : : represented by _CST nodes. */
2264 : :
2265 : : static tree
2266 : 10852750 : cxx_eval_builtin_function_call (const constexpr_ctx *ctx, tree t, tree fun,
2267 : : value_cat lval,
2268 : : bool *non_constant_p, bool *overflow_p,
2269 : : tree *jump_target)
2270 : : {
2271 : 10852750 : const int nargs = call_expr_nargs (t);
2272 : 10852750 : tree *args = (tree *) alloca (nargs * sizeof (tree));
2273 : 10852750 : tree new_call;
2274 : 10852750 : int i;
2275 : :
2276 : : /* Don't fold __builtin_constant_p within a constexpr function. */
2277 : 10852750 : bool bi_const_p = DECL_IS_BUILTIN_CONSTANT_P (fun);
2278 : :
2279 : : /* If we aren't requiring a constant expression, defer __builtin_constant_p
2280 : : in a constexpr function until we have values for the parameters. */
2281 : 738614 : if (bi_const_p
2282 : 738614 : && ctx->manifestly_const_eval != mce_true
2283 : 725463 : && current_function_decl
2284 : 723313 : && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
2285 : : {
2286 : 425997 : *non_constant_p = true;
2287 : 425997 : return t;
2288 : : }
2289 : :
2290 : : /* For __builtin_is_constant_evaluated, defer it if not
2291 : : ctx->manifestly_const_eval (as sometimes we try to constant evaluate
2292 : : without manifestly_const_eval even expressions or parts thereof which
2293 : : will later be manifestly const_eval evaluated), otherwise fold it to
2294 : : true. */
2295 : 10426753 : if (fndecl_built_in_p (fun, CP_BUILT_IN_IS_CONSTANT_EVALUATED,
2296 : : BUILT_IN_FRONTEND))
2297 : : {
2298 : 1966797 : if (ctx->manifestly_const_eval == mce_unknown)
2299 : : {
2300 : 1950169 : *non_constant_p = true;
2301 : 1950169 : return t;
2302 : : }
2303 : 16628 : return constant_boolean_node (ctx->manifestly_const_eval == mce_true,
2304 : 16628 : boolean_type_node);
2305 : : }
2306 : :
2307 : 8459956 : if (fndecl_built_in_p (fun, CP_BUILT_IN_SOURCE_LOCATION, BUILT_IN_FRONTEND))
2308 : : {
2309 : 436 : temp_override<tree> ovr (current_function_decl);
2310 : 436 : if (ctx->call && ctx->call->fundef)
2311 : 137 : current_function_decl = ctx->call->fundef->decl;
2312 : 436 : return fold_builtin_source_location (t);
2313 : 436 : }
2314 : :
2315 : 8459520 : if (fndecl_built_in_p (fun, CP_BUILT_IN_EH_PTR_ADJUST_REF,
2316 : : BUILT_IN_FRONTEND))
2317 : 71 : return cxx_eval_cxa_builtin_fn (ctx, t, BUILTIN_EH_PTR_ADJUST_REF,
2318 : : fun, non_constant_p, overflow_p,
2319 : 71 : jump_target);
2320 : :
2321 : 8459449 : int strops = 0;
2322 : 8459449 : int strret = 0;
2323 : 8459449 : if (fndecl_built_in_p (fun, BUILT_IN_NORMAL))
2324 : 7823715 : switch (DECL_FUNCTION_CODE (fun))
2325 : : {
2326 : : case BUILT_IN_STRLEN:
2327 : : case BUILT_IN_STRNLEN:
2328 : 8459388 : strops = 1;
2329 : : break;
2330 : 106031 : case BUILT_IN_MEMCHR:
2331 : 106031 : case BUILT_IN_STRCHR:
2332 : 106031 : case BUILT_IN_STRRCHR:
2333 : 106031 : strops = 1;
2334 : 106031 : strret = 1;
2335 : 106031 : break;
2336 : 49344 : case BUILT_IN_MEMCMP:
2337 : 49344 : case BUILT_IN_STRCMP:
2338 : 49344 : strops = 2;
2339 : 49344 : break;
2340 : 30522 : case BUILT_IN_STRSTR:
2341 : 30522 : strops = 2;
2342 : 30522 : strret = 1;
2343 : 30522 : break;
2344 : 42 : case BUILT_IN_ASAN_POINTER_COMPARE:
2345 : 42 : case BUILT_IN_ASAN_POINTER_SUBTRACT:
2346 : : /* These builtins shall be ignored during constant expression
2347 : : evaluation. */
2348 : 42 : return void_node;
2349 : 19 : case BUILT_IN_UNREACHABLE:
2350 : 19 : case BUILT_IN_TRAP:
2351 : 19 : if (!*non_constant_p && !ctx->quiet)
2352 : : {
2353 : : /* Do not allow__builtin_unreachable in constexpr function.
2354 : : The __builtin_unreachable call with BUILTINS_LOCATION
2355 : : comes from cp_maybe_instrument_return. */
2356 : 1 : if (EXPR_LOCATION (t) == BUILTINS_LOCATION)
2357 : 0 : error ("%<constexpr%> call flows off the end of the function");
2358 : : else
2359 : 1 : error ("%q+E is not a constant expression", t);
2360 : : }
2361 : 19 : *non_constant_p = true;
2362 : 19 : return t;
2363 : : default:
2364 : : break;
2365 : : }
2366 : :
2367 : : /* Be permissive for arguments to built-ins; __builtin_constant_p should
2368 : : return constant false for a non-constant argument. */
2369 : 8459388 : constexpr_ctx new_ctx = *ctx;
2370 : 8459388 : new_ctx.quiet = true;
2371 : 22845102 : for (i = 0; i < nargs; ++i)
2372 : : {
2373 : 14385714 : tree arg = CALL_EXPR_ARG (t, i);
2374 : 14385714 : tree oarg = arg;
2375 : :
2376 : : /* To handle string built-ins we need to pass ADDR_EXPR<STRING_CST> since
2377 : : expand_builtin doesn't know how to look in the values table. */
2378 : 14385714 : bool strop = i < strops;
2379 : 14385714 : if (strop)
2380 : : {
2381 : 358773 : STRIP_NOPS (arg);
2382 : 358773 : if (TREE_CODE (arg) == ADDR_EXPR)
2383 : 7729 : arg = TREE_OPERAND (arg, 0);
2384 : : else
2385 : : strop = false;
2386 : : }
2387 : :
2388 : : /* If builtin_valid_in_constant_expr_p is true,
2389 : : potential_constant_expression_1 has not recursed into the arguments
2390 : : of the builtin, verify it here. */
2391 : 14385714 : if (!builtin_valid_in_constant_expr_p (fun)
2392 : 14385714 : || potential_constant_expression (arg))
2393 : : {
2394 : 14385583 : bool dummy1 = false, dummy2 = false;
2395 : 14385583 : tree jmp_target = NULL_TREE;
2396 : 14385583 : arg = cxx_eval_constant_expression (&new_ctx, arg, vc_prvalue,
2397 : : &dummy1, &dummy2, &jmp_target);
2398 : 14385583 : if (jmp_target)
2399 : : {
2400 : 0 : *jump_target = jmp_target;
2401 : 0 : return NULL_TREE;
2402 : : }
2403 : : }
2404 : :
2405 : 14385714 : if (bi_const_p)
2406 : : /* For __builtin_constant_p, fold all expressions with constant values
2407 : : even if they aren't C++ constant-expressions. */
2408 : 312617 : arg = cp_fold_rvalue (arg);
2409 : 14073097 : else if (strop)
2410 : : {
2411 : 7729 : if (TREE_CODE (arg) == CONSTRUCTOR)
2412 : 100 : arg = braced_lists_to_strings (TREE_TYPE (arg), arg);
2413 : 7729 : if (TREE_CODE (arg) == STRING_CST)
2414 : 2830 : arg = build_address (arg);
2415 : : else
2416 : : arg = oarg;
2417 : : }
2418 : :
2419 : 14385714 : args[i] = arg;
2420 : : }
2421 : :
2422 : 8459388 : bool save_ffbcp = force_folding_builtin_constant_p;
2423 : 8459388 : force_folding_builtin_constant_p |= ctx->manifestly_const_eval == mce_true;
2424 : 8459388 : tree save_cur_fn = current_function_decl;
2425 : : /* Return name of ctx->call->fundef->decl for __builtin_FUNCTION (). */
2426 : 8459388 : if (fndecl_built_in_p (fun, BUILT_IN_FUNCTION)
2427 : 38 : && ctx->call
2428 : 8459392 : && ctx->call->fundef)
2429 : 4 : current_function_decl = ctx->call->fundef->decl;
2430 : 8459388 : if (fndecl_built_in_p (fun,
2431 : : CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS,
2432 : : BUILT_IN_FRONTEND))
2433 : : {
2434 : 348 : location_t loc = EXPR_LOCATION (t);
2435 : 348 : if (nargs >= 1)
2436 : 345 : VERIFY_CONSTANT (args[0]);
2437 : 143 : new_call
2438 : 143 : = fold_builtin_is_pointer_inverconvertible_with_class (loc, nargs,
2439 : : args);
2440 : : }
2441 : 8459040 : else if (fndecl_built_in_p (fun,
2442 : : CP_BUILT_IN_IS_CORRESPONDING_MEMBER,
2443 : : BUILT_IN_FRONTEND))
2444 : : {
2445 : 459 : location_t loc = EXPR_LOCATION (t);
2446 : 459 : if (nargs >= 2)
2447 : : {
2448 : 453 : VERIFY_CONSTANT (args[0]);
2449 : 231 : VERIFY_CONSTANT (args[1]);
2450 : : }
2451 : 237 : new_call = fold_builtin_is_corresponding_member (loc, nargs, args);
2452 : : }
2453 : : else
2454 : 16917162 : new_call = fold_builtin_call_array (EXPR_LOCATION (t), TREE_TYPE (t),
2455 : 8458581 : CALL_EXPR_FN (t), nargs, args);
2456 : 8458961 : current_function_decl = save_cur_fn;
2457 : 8458961 : force_folding_builtin_constant_p = save_ffbcp;
2458 : 8458961 : if (new_call == NULL)
2459 : : {
2460 : 6229200 : if (!*non_constant_p && !ctx->quiet)
2461 : : {
2462 : 0 : new_call = build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t),
2463 : 0 : CALL_EXPR_FN (t), nargs, args);
2464 : 0 : error ("%q+E is not a constant expression", new_call);
2465 : : }
2466 : 6229200 : *non_constant_p = true;
2467 : 6229200 : return t;
2468 : : }
2469 : :
2470 : 2229761 : if (!potential_constant_expression (new_call))
2471 : : {
2472 : 577 : if (!*non_constant_p && !ctx->quiet)
2473 : 4 : error ("%q+E is not a constant expression", new_call);
2474 : 577 : *non_constant_p = true;
2475 : 577 : return t;
2476 : : }
2477 : :
2478 : 2229184 : if (strret)
2479 : : {
2480 : : /* memchr returns a pointer into the first argument, but we replaced the
2481 : : argument above with a STRING_CST; put it back it now. */
2482 : 117 : tree op = CALL_EXPR_ARG (t, strret-1);
2483 : 117 : STRIP_NOPS (new_call);
2484 : 117 : if (TREE_CODE (new_call) == POINTER_PLUS_EXPR)
2485 : 59 : TREE_OPERAND (new_call, 0) = op;
2486 : 58 : else if (TREE_CODE (new_call) == ADDR_EXPR)
2487 : 2229184 : new_call = op;
2488 : : }
2489 : :
2490 : 2229184 : return cxx_eval_constant_expression (&new_ctx, new_call, lval,
2491 : : non_constant_p, overflow_p,
2492 : 2229184 : jump_target);
2493 : : }
2494 : :
2495 : : /* TEMP is the constant value of a temporary object of type TYPE. Adjust
2496 : : the type of the value to match. */
2497 : :
2498 : : static tree
2499 : 44277684 : adjust_temp_type (tree type, tree temp)
2500 : : {
2501 : 44277684 : if (same_type_p (TREE_TYPE (temp), type))
2502 : : return temp;
2503 : : /* Avoid wrapping an aggregate value in a NOP_EXPR. */
2504 : 21698487 : if (TREE_CODE (temp) == CONSTRUCTOR)
2505 : : {
2506 : : /* build_constructor wouldn't retain various CONSTRUCTOR flags. */
2507 : 2660690 : tree t = copy_node (temp);
2508 : 2660690 : TREE_TYPE (t) = type;
2509 : 2660690 : return t;
2510 : : }
2511 : 19037797 : if (TREE_CODE (temp) == EMPTY_CLASS_EXPR)
2512 : 0 : return build0 (EMPTY_CLASS_EXPR, type);
2513 : 19037797 : gcc_assert (scalarish_type_p (type));
2514 : : /* Now we know we're dealing with a scalar, and a prvalue of non-class
2515 : : type is cv-unqualified. */
2516 : 19037797 : return cp_fold_convert (cv_unqualified (type), temp);
2517 : : }
2518 : :
2519 : : /* If T is a CONSTRUCTOR, return an unshared copy of T and any
2520 : : sub-CONSTRUCTORs. Otherwise return T.
2521 : :
2522 : : We use this whenever we initialize an object as a whole, whether it's a
2523 : : parameter, a local variable, or a subobject, so that subsequent
2524 : : modifications don't affect other places where it was used. */
2525 : :
2526 : : tree
2527 : 33078171 : unshare_constructor (tree t MEM_STAT_DECL)
2528 : : {
2529 : 33078171 : if (!t || TREE_CODE (t) != CONSTRUCTOR)
2530 : : return t;
2531 : 7547168 : auto_vec <tree*, 4> ptrs;
2532 : 7547168 : ptrs.safe_push (&t);
2533 : 7547168 : while (!ptrs.is_empty ())
2534 : : {
2535 : 8568917 : tree *p = ptrs.pop ();
2536 : 8568917 : tree n = copy_node (*p PASS_MEM_STAT);
2537 : 12541459 : CONSTRUCTOR_ELTS (n) = vec_safe_copy (CONSTRUCTOR_ELTS (*p) PASS_MEM_STAT);
2538 : 8568917 : *p = n;
2539 : 8568917 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (n);
2540 : 8568917 : constructor_elt *ce;
2541 : 32989155 : for (HOST_WIDE_INT i = 0; vec_safe_iterate (v, i, &ce); ++i)
2542 : 8304153 : if (ce->value && TREE_CODE (ce->value) == CONSTRUCTOR)
2543 : 1021749 : ptrs.safe_push (&ce->value);
2544 : : }
2545 : 7547168 : return t;
2546 : 7547168 : }
2547 : :
2548 : : /* If T is a CONSTRUCTOR, ggc_free T and any sub-CONSTRUCTORs. */
2549 : :
2550 : : static void
2551 : 740298 : free_constructor (tree t)
2552 : : {
2553 : 740298 : if (!t || TREE_CODE (t) != CONSTRUCTOR)
2554 : 0 : return;
2555 : 740298 : releasing_vec ctors;
2556 : 740298 : vec_safe_push (ctors, t);
2557 : 1497348 : while (!ctors->is_empty ())
2558 : : {
2559 : 757050 : tree c = ctors->pop ();
2560 : 757050 : if (vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (c))
2561 : : {
2562 : : constructor_elt *ce;
2563 : 152994 : for (HOST_WIDE_INT i = 0; vec_safe_iterate (elts, i, &ce); ++i)
2564 : 100985 : if (ce->value && TREE_CODE (ce->value) == CONSTRUCTOR)
2565 : 16752 : vec_safe_push (ctors, ce->value);
2566 : 52009 : ggc_free (elts);
2567 : : }
2568 : 757050 : ggc_free (c);
2569 : : }
2570 : 740298 : }
2571 : :
2572 : : /* Helper function of cxx_bind_parameters_in_call. Return non-NULL
2573 : : if *TP is address of a static variable (or part of it) currently being
2574 : : constructed or of a heap artificial variable. */
2575 : :
2576 : : static tree
2577 : 11724210 : addr_of_non_const_var (tree *tp, int *walk_subtrees, void *data)
2578 : : {
2579 : 11724210 : if (TREE_CODE (*tp) == ADDR_EXPR)
2580 : 1746295 : if (tree var = get_base_address (TREE_OPERAND (*tp, 0)))
2581 : 1746295 : if (VAR_P (var) && TREE_STATIC (var))
2582 : : {
2583 : 773809 : if (DECL_NAME (var) == heap_uninit_identifier
2584 : 773809 : || DECL_NAME (var) == heap_identifier
2585 : 773809 : || DECL_NAME (var) == heap_vec_uninit_identifier
2586 : 1547618 : || DECL_NAME (var) == heap_vec_identifier)
2587 : : return var;
2588 : :
2589 : 773809 : constexpr_global_ctx *global = (constexpr_global_ctx *) data;
2590 : 773809 : if (global->get_value (var))
2591 : : return var;
2592 : : }
2593 : 11601216 : if (TYPE_P (*tp))
2594 : 1179 : *walk_subtrees = false;
2595 : : return NULL_TREE;
2596 : : }
2597 : :
2598 : : /* Subroutine of cxx_eval_call_expression.
2599 : : We are processing a call expression (either CALL_EXPR or
2600 : : AGGR_INIT_EXPR) in the context of CTX. Evaluate
2601 : : all arguments and bind their values to correspondings
2602 : : parameters, making up the NEW_CALL context. */
2603 : :
2604 : : static tree
2605 : 71909393 : cxx_bind_parameters_in_call (const constexpr_ctx *ctx, tree t, tree fun,
2606 : : tree orig_fun, bool *non_constant_p,
2607 : : bool *overflow_p, bool *non_constant_args,
2608 : : tree *jump_target)
2609 : : {
2610 : 71909393 : int nargs = call_expr_nargs (t);
2611 : 71909393 : tree parms = DECL_ARGUMENTS (fun);
2612 : 71909393 : int i, j = 0;
2613 : 71909393 : if (DECL_HAS_IN_CHARGE_PARM_P (fun) && fun != orig_fun)
2614 : 1226 : ++nargs;
2615 : 71909393 : if (DECL_HAS_VTT_PARM_P (fun)
2616 : 1228 : && fun != orig_fun
2617 : 71910619 : && (DECL_COMPLETE_CONSTRUCTOR_P (orig_fun)
2618 : 900 : || DECL_COMPLETE_DESTRUCTOR_P (orig_fun)))
2619 : 337 : ++nargs;
2620 : : /* We don't record ellipsis args below. */
2621 : 71909393 : int nparms = list_length (parms);
2622 : 71909393 : int nbinds = nargs < nparms ? nargs : nparms;
2623 : 71909393 : tree binds = make_tree_vec (nbinds);
2624 : :
2625 : : /* The call is not a constant expression if it involves the cdtor for a type
2626 : : with virtual bases before C++26. */
2627 : 71909393 : if (cxx_dialect < cxx26
2628 : 71909393 : && (DECL_HAS_IN_CHARGE_PARM_P (fun) || DECL_HAS_VTT_PARM_P (fun)))
2629 : : {
2630 : 17 : if (!ctx->quiet)
2631 : : {
2632 : 3 : error_at (cp_expr_loc_or_input_loc (t),
2633 : : "call to non-%<constexpr%> function %qD", fun);
2634 : 3 : explain_invalid_constexpr_fn (fun);
2635 : : }
2636 : 17 : *non_constant_p = true;
2637 : 17 : return binds;
2638 : : }
2639 : :
2640 : 110593362 : for (i = 0; i < nargs; ++i)
2641 : : {
2642 : 71084065 : tree x, arg;
2643 : 71084065 : tree type = parms ? TREE_TYPE (parms) : void_type_node;
2644 : 71084065 : if (parms && DECL_BY_REFERENCE (parms))
2645 : 6180 : type = TREE_TYPE (type);
2646 : 71084065 : if (i == 1
2647 : 71084065 : && j == 0
2648 : 11613288 : && DECL_HAS_IN_CHARGE_PARM_P (fun)
2649 : 71085187 : && orig_fun != fun)
2650 : : {
2651 : 1122 : if (DECL_COMPLETE_CONSTRUCTOR_P (orig_fun)
2652 : 1122 : || DECL_COMPLETE_DESTRUCTOR_P (orig_fun))
2653 : 324 : x = boolean_true_node;
2654 : : else
2655 : 798 : x = boolean_false_node;
2656 : : j = -1;
2657 : : }
2658 : 71082943 : else if (i == 2
2659 : 71082943 : && j == -1
2660 : 1122 : && DECL_HAS_VTT_PARM_P (fun)
2661 : 1122 : && orig_fun != fun
2662 : 71084065 : && (DECL_COMPLETE_CONSTRUCTOR_P (orig_fun)
2663 : 809 : || DECL_COMPLETE_DESTRUCTOR_P (orig_fun)))
2664 : : {
2665 : 324 : x = build_zero_cst (type);
2666 : 324 : j = -2;
2667 : : }
2668 : : else
2669 : 71082619 : x = get_nth_callarg (t, i + j);
2670 : : /* For member function, the first argument is a pointer to the implied
2671 : : object. For a constructor, it might still be a dummy object, in
2672 : : which case we get the real argument from ctx. */
2673 : 114271272 : if (i == 0 && DECL_CONSTRUCTOR_P (fun)
2674 : 79699717 : && is_dummy_object (x))
2675 : : {
2676 : 3765422 : x = ctx->object;
2677 : 3765422 : x = build_address (x);
2678 : : }
2679 : 71084065 : if (TREE_ADDRESSABLE (type))
2680 : : {
2681 : : /* Undo convert_for_arg_passing work here. */
2682 : 9086 : x = convert_from_reference (x);
2683 : 9086 : arg = cxx_eval_constant_expression (ctx, x, vc_glvalue,
2684 : : non_constant_p, overflow_p,
2685 : : jump_target);
2686 : : }
2687 : : else
2688 : : /* Normally we would strip a TARGET_EXPR in an initialization context
2689 : : such as this, but here we do the elision differently: we keep the
2690 : : TARGET_EXPR, and use its CONSTRUCTOR as the value of the parm. */
2691 : 71074979 : arg = cxx_eval_constant_expression (ctx, x, vc_prvalue,
2692 : : non_constant_p, overflow_p,
2693 : : jump_target);
2694 : : /* Check we aren't dereferencing a null pointer when calling a non-static
2695 : : member function, which is undefined behaviour. */
2696 : 57135636 : if (i == 0 && DECL_OBJECT_MEMBER_FUNCTION_P (fun)
2697 : 30469594 : && integer_zerop (arg)
2698 : : /* But ignore calls from within compiler-generated code, to handle
2699 : : cases like lambda function pointer conversion operator thunks
2700 : : which pass NULL as the 'this' pointer. */
2701 : 71126714 : && !(TREE_CODE (t) == CALL_EXPR && CALL_FROM_THUNK_P (t)))
2702 : : {
2703 : 286 : if (!ctx->quiet)
2704 : 6 : error_at (cp_expr_loc_or_input_loc (x),
2705 : : "dereferencing a null pointer");
2706 : 286 : *non_constant_p = true;
2707 : : }
2708 : : /* Don't VERIFY_CONSTANT here. */
2709 : 71084065 : if (*non_constant_p && ctx->quiet)
2710 : : break;
2711 : 38683992 : if (*jump_target)
2712 : : break;
2713 : : /* Just discard ellipsis args after checking their constantitude. */
2714 : 38683986 : if (!parms)
2715 : 400 : continue;
2716 : :
2717 : 38683586 : if (!*non_constant_p)
2718 : : {
2719 : : /* Make sure the binding has the same type as the parm. But
2720 : : only for constant args. */
2721 : 38682839 : if (TREE_ADDRESSABLE (type))
2722 : : {
2723 : 6582 : if (!same_type_p (type, TREE_TYPE (arg)))
2724 : : {
2725 : 9 : arg = build_fold_addr_expr (arg);
2726 : 9 : arg = cp_fold_convert (build_reference_type (type), arg);
2727 : 9 : arg = convert_from_reference (arg);
2728 : : }
2729 : : }
2730 : 38676257 : else if (!TYPE_REF_P (type))
2731 : 29343483 : arg = adjust_temp_type (type, arg);
2732 : 38682839 : if (!TREE_CONSTANT (arg))
2733 : 27246843 : *non_constant_args = true;
2734 : 11435996 : else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
2735 : : /* The destructor needs to see any modifications the callee makes
2736 : : to the argument. */
2737 : 0 : *non_constant_args = true;
2738 : : /* If arg is or contains address of a heap artificial variable or
2739 : : of a static variable being constructed, avoid caching the
2740 : : function call, as those variables might be modified by the
2741 : : function, or might be modified by the callers in between
2742 : : the cached function and just read by the function. */
2743 : 11435996 : else if (!*non_constant_args
2744 : 11435996 : && cp_walk_tree (&arg, addr_of_non_const_var, ctx->global,
2745 : : NULL))
2746 : 122994 : *non_constant_args = true;
2747 : :
2748 : : /* For virtual calls, adjust the this argument, so that it is
2749 : : the object on which the method is called, rather than
2750 : : one of its bases. */
2751 : 38682839 : if (i == 0 && DECL_VIRTUAL_P (fun))
2752 : : {
2753 : 15646 : tree addr = arg;
2754 : 15646 : STRIP_NOPS (addr);
2755 : 15646 : if (TREE_CODE (addr) == ADDR_EXPR)
2756 : : {
2757 : 15626 : tree obj = TREE_OPERAND (addr, 0);
2758 : 15626 : while (TREE_CODE (obj) == COMPONENT_REF
2759 : 7213 : && DECL_FIELD_IS_BASE (TREE_OPERAND (obj, 1))
2760 : 22846 : && !same_type_ignoring_top_level_qualifiers_p
2761 : 7200 : (TREE_TYPE (obj), DECL_CONTEXT (fun)))
2762 : 20 : obj = TREE_OPERAND (obj, 0);
2763 : 15626 : if (obj != TREE_OPERAND (addr, 0))
2764 : 17 : arg = build_fold_addr_expr_with_type (obj,
2765 : : TREE_TYPE (arg));
2766 : : }
2767 : : }
2768 : 38682839 : TREE_VEC_ELT (binds, i) = arg;
2769 : : }
2770 : 38683586 : parms = TREE_CHAIN (parms);
2771 : : }
2772 : :
2773 : : return binds;
2774 : : }
2775 : :
2776 : : /* Variables and functions to manage constexpr call expansion context.
2777 : : These do not need to be marked for PCH or GC. */
2778 : :
2779 : : /* FIXME remember and print actual constant arguments. */
2780 : : static vec<tree> call_stack;
2781 : : static int call_stack_tick;
2782 : : static int last_cx_error_tick;
2783 : :
2784 : : static int
2785 : 39187305 : push_cx_call_context (tree call)
2786 : : {
2787 : 39187305 : ++call_stack_tick;
2788 : 39187305 : if (!EXPR_HAS_LOCATION (call))
2789 : 17765 : SET_EXPR_LOCATION (call, input_location);
2790 : 39187305 : call_stack.safe_push (call);
2791 : 39187305 : int len = call_stack.length ();
2792 : 39187305 : if (len > max_constexpr_depth)
2793 : 30 : return false;
2794 : : return len;
2795 : : }
2796 : :
2797 : : static void
2798 : 39187305 : pop_cx_call_context (void)
2799 : : {
2800 : 39187305 : ++call_stack_tick;
2801 : 39187305 : call_stack.pop ();
2802 : 39187305 : }
2803 : :
2804 : : vec<tree>
2805 : 212298 : cx_error_context (void)
2806 : : {
2807 : 212298 : vec<tree> r = vNULL;
2808 : 212298 : if (call_stack_tick != last_cx_error_tick
2809 : 212298 : && !call_stack.is_empty ())
2810 : : r = call_stack;
2811 : 212298 : last_cx_error_tick = call_stack_tick;
2812 : 212298 : return r;
2813 : : }
2814 : :
2815 : : /* E is an operand of a failed assertion, fold it either with or without
2816 : : constexpr context. */
2817 : :
2818 : : static tree
2819 : 576 : fold_operand (tree e, const constexpr_ctx *ctx)
2820 : : {
2821 : 576 : if (ctx)
2822 : : {
2823 : 70 : bool new_non_constant_p = false, new_overflow_p = false;
2824 : 70 : tree jmp_target = NULL_TREE;
2825 : 70 : e = cxx_eval_constant_expression (ctx, e, vc_prvalue,
2826 : : &new_non_constant_p,
2827 : : &new_overflow_p, &jmp_target);
2828 : : }
2829 : : else
2830 : 506 : e = fold_non_dependent_expr (e, tf_none, /*manifestly_const_eval=*/true);
2831 : 576 : return e;
2832 : : }
2833 : :
2834 : : /* If we have a condition in conjunctive normal form (CNF), find the first
2835 : : failing clause. In other words, given an expression like
2836 : :
2837 : : true && true && false && true && false
2838 : :
2839 : : return the first 'false'. EXPR is the expression. */
2840 : :
2841 : : static tree
2842 : 388 : find_failing_clause_r (const constexpr_ctx *ctx, tree expr)
2843 : : {
2844 : 500 : if (TREE_CODE (expr) == TRUTH_ANDIF_EXPR)
2845 : : {
2846 : : /* First check the left side... */
2847 : 220 : tree e = find_failing_clause_r (ctx, TREE_OPERAND (expr, 0));
2848 : 220 : if (e == NULL_TREE)
2849 : : /* ...if we didn't find a false clause, check the right side. */
2850 : 112 : e = find_failing_clause_r (ctx, TREE_OPERAND (expr, 1));
2851 : : return e;
2852 : : }
2853 : 280 : tree e = contextual_conv_bool (expr, tf_none);
2854 : 280 : e = fold_operand (e, ctx);
2855 : 280 : if (integer_zerop (e))
2856 : : /* This is the failing clause. */
2857 : 168 : return expr;
2858 : : return NULL_TREE;
2859 : : }
2860 : :
2861 : : /* Wrapper for find_failing_clause_r. */
2862 : :
2863 : : tree
2864 : 1347 : find_failing_clause (const constexpr_ctx *ctx, tree expr)
2865 : : {
2866 : 1347 : if (TREE_CODE (expr) == TRUTH_ANDIF_EXPR)
2867 : 168 : if (tree e = find_failing_clause_r (ctx, expr))
2868 : 1347 : expr = e;
2869 : 1347 : return expr;
2870 : : }
2871 : :
2872 : : /* Emit additional diagnostics for failing condition BAD.
2873 : : Used by finish_static_assert and IFN_ASSUME constexpr diagnostics.
2874 : : If SHOW_EXPR_P is true, print the condition (because it was
2875 : : instantiation-dependent). */
2876 : :
2877 : : void
2878 : 1347 : diagnose_failing_condition (tree bad, location_t cloc, bool show_expr_p,
2879 : : const constexpr_ctx *ctx /* = nullptr */)
2880 : : {
2881 : : /* Nobody wants to see the artificial (bool) cast. */
2882 : 1347 : bad = tree_strip_nop_conversions (bad);
2883 : 1347 : if (TREE_CODE (bad) == CLEANUP_POINT_EXPR)
2884 : 4 : bad = TREE_OPERAND (bad, 0);
2885 : :
2886 : : /* Actually explain the failure if this is a concept check or a
2887 : : requires-expression. */
2888 : 1347 : if (concept_check_p (bad) || TREE_CODE (bad) == REQUIRES_EXPR)
2889 : 243 : diagnose_constraints (cloc, bad, NULL_TREE);
2890 : 1104 : else if (COMPARISON_CLASS_P (bad)
2891 : 1104 : && ARITHMETIC_TYPE_P (TREE_TYPE (TREE_OPERAND (bad, 0))))
2892 : : {
2893 : 148 : tree op0 = fold_operand (TREE_OPERAND (bad, 0), ctx);
2894 : 148 : tree op1 = fold_operand (TREE_OPERAND (bad, 1), ctx);
2895 : 148 : tree cond = build2 (TREE_CODE (bad), boolean_type_node, op0, op1);
2896 : 148 : inform (cloc, "the comparison reduces to %qE", cond);
2897 : : }
2898 : 956 : else if (show_expr_p)
2899 : 740 : inform (cloc, "%qE evaluates to false", bad);
2900 : 1347 : }
2901 : :
2902 : : /* Process an assert/assume of ORIG_ARG. If it's not supposed to be evaluated,
2903 : : do it without changing the current evaluation state. If it evaluates to
2904 : : false, complain and return false; otherwise, return true. */
2905 : :
2906 : : static bool
2907 : 224016 : cxx_eval_assert (const constexpr_ctx *ctx, tree arg, const char *msg,
2908 : : location_t loc, bool evaluated,
2909 : : bool *non_constant_p, bool *overflow_p)
2910 : : {
2911 : 224016 : if (*non_constant_p)
2912 : : return true;
2913 : :
2914 : 224016 : tree eval, jmp_target = NULL_TREE;
2915 : 224016 : if (!evaluated)
2916 : : {
2917 : 223945 : if (!potential_rvalue_constant_expression (arg))
2918 : 19 : return true;
2919 : :
2920 : 223926 : constexpr_ctx new_ctx = *ctx;
2921 : 223926 : new_ctx.quiet = true;
2922 : 223926 : bool new_non_constant_p = false, new_overflow_p = false;
2923 : : /* Avoid modification of existing values. */
2924 : 223926 : modifiable_tracker ms (new_ctx.global);
2925 : 223926 : eval = cxx_eval_constant_expression (&new_ctx, arg, vc_prvalue,
2926 : : &new_non_constant_p,
2927 : : &new_overflow_p, &jmp_target);
2928 : 223926 : }
2929 : : else
2930 : 71 : eval = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
2931 : : non_constant_p,
2932 : : overflow_p, &jmp_target);
2933 : 223997 : if (jmp_target)
2934 : : return true;
2935 : :
2936 : 223997 : if (!*non_constant_p && integer_zerop (eval))
2937 : : {
2938 : 99 : if (!ctx->quiet)
2939 : : {
2940 : : /* See if we can find which clause was failing
2941 : : (for logical AND). */
2942 : 32 : tree bad = find_failing_clause (ctx, arg);
2943 : : /* If not, or its location is unusable, fall back to the
2944 : : previous location. */
2945 : 32 : location_t cloc = cp_expr_loc_or_loc (bad, loc);
2946 : :
2947 : : /* Report the error. */
2948 : 32 : auto_diagnostic_group d;
2949 : 32 : error_at (cloc, msg);
2950 : 32 : diagnose_failing_condition (bad, cloc, true, ctx);
2951 : 32 : return bad;
2952 : 32 : }
2953 : 67 : *non_constant_p = true;
2954 : 67 : return false;
2955 : : }
2956 : :
2957 : : return true;
2958 : : }
2959 : :
2960 : : /* Evaluate a call T to a GCC internal function when possible and return
2961 : : the evaluated result or, under the control of CTX, give an error, set
2962 : : NON_CONSTANT_P, and return the unevaluated call T otherwise. */
2963 : :
2964 : : static tree
2965 : 271714 : cxx_eval_internal_function (const constexpr_ctx *ctx, tree t,
2966 : : value_cat lval,
2967 : : bool *non_constant_p, bool *overflow_p,
2968 : : tree *jump_target)
2969 : : {
2970 : 271714 : enum tree_code opcode = ERROR_MARK;
2971 : :
2972 : 271714 : switch (CALL_EXPR_IFN (t))
2973 : : {
2974 : 165 : case IFN_UBSAN_NULL:
2975 : 165 : case IFN_UBSAN_BOUNDS:
2976 : 165 : case IFN_UBSAN_VPTR:
2977 : 165 : case IFN_FALLTHROUGH:
2978 : 165 : return void_node;
2979 : :
2980 : 223929 : case IFN_ASSUME:
2981 : 223929 : if (!cxx_eval_assert (ctx, CALL_EXPR_ARG (t, 0),
2982 : : G_("failed %<assume%> attribute assumption"),
2983 : 223929 : EXPR_LOCATION (t), /*eval*/false,
2984 : : non_constant_p, overflow_p))
2985 : : return t;
2986 : 223900 : return void_node;
2987 : :
2988 : : case IFN_ADD_OVERFLOW:
2989 : : opcode = PLUS_EXPR;
2990 : : break;
2991 : 339 : case IFN_SUB_OVERFLOW:
2992 : 339 : opcode = MINUS_EXPR;
2993 : 339 : break;
2994 : 46719 : case IFN_MUL_OVERFLOW:
2995 : 46719 : opcode = MULT_EXPR;
2996 : 46719 : break;
2997 : :
2998 : 74 : case IFN_LAUNDER:
2999 : 74 : return cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0),
3000 : : vc_prvalue, non_constant_p,
3001 : 74 : overflow_p, jump_target);
3002 : :
3003 : 30 : case IFN_VEC_CONVERT:
3004 : 30 : {
3005 : 30 : tree arg = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0),
3006 : : vc_prvalue, non_constant_p,
3007 : : overflow_p, jump_target);
3008 : 30 : if (*jump_target)
3009 : : return NULL_TREE;
3010 : 30 : if (TREE_CODE (arg) == VECTOR_CST)
3011 : 19 : if (tree r = fold_const_call (CFN_VEC_CONVERT, TREE_TYPE (t), arg))
3012 : : return r;
3013 : : }
3014 : : /* FALLTHRU */
3015 : :
3016 : 16 : default:
3017 : 16 : if (!ctx->quiet)
3018 : 0 : error_at (cp_expr_loc_or_input_loc (t),
3019 : : "call to internal function %qE", t);
3020 : 16 : *non_constant_p = true;
3021 : 16 : return t;
3022 : : }
3023 : :
3024 : : /* Evaluate constant arguments using OPCODE and return a complex
3025 : : number containing the result and the overflow bit. */
3026 : 47516 : tree arg0 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), lval,
3027 : : non_constant_p, overflow_p,
3028 : : jump_target);
3029 : 47516 : tree arg1 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 1), lval,
3030 : : non_constant_p, overflow_p,
3031 : : jump_target);
3032 : 47516 : if (*jump_target)
3033 : : return NULL_TREE;
3034 : 47516 : if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
3035 : : {
3036 : 2 : location_t loc = cp_expr_loc_or_input_loc (t);
3037 : 2 : tree type = TREE_TYPE (TREE_TYPE (t));
3038 : 2 : tree result = fold_binary_loc (loc, opcode, type,
3039 : : fold_convert_loc (loc, type, arg0),
3040 : : fold_convert_loc (loc, type, arg1));
3041 : 2 : tree ovf
3042 : 2 : = build_int_cst (type, arith_overflowed_p (opcode, type, arg0, arg1));
3043 : : /* Reset TREE_OVERFLOW to avoid warnings for the overflow. */
3044 : 2 : if (TREE_OVERFLOW (result))
3045 : 0 : TREE_OVERFLOW (result) = 0;
3046 : :
3047 : 2 : return build_complex (TREE_TYPE (t), result, ovf);
3048 : : }
3049 : :
3050 : 47514 : *non_constant_p = true;
3051 : 47514 : return t;
3052 : : }
3053 : :
3054 : : /* Clean CONSTRUCTOR_NO_CLEARING from CTOR and its sub-aggregates. */
3055 : :
3056 : : static void
3057 : 3940103 : clear_no_implicit_zero (tree ctor)
3058 : : {
3059 : 3940103 : if (CONSTRUCTOR_NO_CLEARING (ctor))
3060 : : {
3061 : 981407 : CONSTRUCTOR_NO_CLEARING (ctor) = false;
3062 : 4580446 : for (auto &e: CONSTRUCTOR_ELTS (ctor))
3063 : 1657793 : if (TREE_CODE (e.value) == CONSTRUCTOR)
3064 : 266083 : clear_no_implicit_zero (e.value);
3065 : : }
3066 : 3940103 : }
3067 : :
3068 : : /* Complain about a const object OBJ being modified in a constant expression.
3069 : : EXPR is the MODIFY_EXPR expression performing the modification. */
3070 : :
3071 : : static void
3072 : 63 : modifying_const_object_error (tree expr, tree obj)
3073 : : {
3074 : 63 : location_t loc = cp_expr_loc_or_input_loc (expr);
3075 : 63 : auto_diagnostic_group d;
3076 : 126 : error_at (loc, "modifying a const object %qE is not allowed in "
3077 : 63 : "a constant expression", TREE_OPERAND (expr, 0));
3078 : :
3079 : : /* Find the underlying object that was declared as const. */
3080 : 63 : location_t decl_loc = UNKNOWN_LOCATION;
3081 : 138 : for (tree probe = obj; decl_loc == UNKNOWN_LOCATION; )
3082 : 75 : switch (TREE_CODE (probe))
3083 : : {
3084 : 39 : case BIT_FIELD_REF:
3085 : 39 : case COMPONENT_REF:
3086 : 39 : {
3087 : 39 : tree elt = TREE_OPERAND (probe, 1);
3088 : 39 : if (CP_TYPE_CONST_P (TREE_TYPE (elt)))
3089 : 27 : decl_loc = DECL_SOURCE_LOCATION (elt);
3090 : 39 : probe = TREE_OPERAND (probe, 0);
3091 : : }
3092 : 39 : break;
3093 : :
3094 : 0 : case ARRAY_REF:
3095 : 0 : case REALPART_EXPR:
3096 : 0 : case IMAGPART_EXPR:
3097 : 0 : probe = TREE_OPERAND (probe, 0);
3098 : 0 : break;
3099 : :
3100 : 36 : default:
3101 : 36 : decl_loc = location_of (probe);
3102 : 36 : break;
3103 : : }
3104 : 63 : inform (decl_loc, "originally declared %<const%> here");
3105 : 63 : }
3106 : :
3107 : : /* Return true if FNDECL is a replaceable global allocation function that
3108 : : should be useable during constant expression evaluation. */
3109 : :
3110 : : static inline bool
3111 : 30310846 : cxx_replaceable_global_alloc_fn (tree fndecl)
3112 : : {
3113 : 30310846 : return (cxx_dialect >= cxx20
3114 : 8989533 : && IDENTIFIER_NEWDEL_OP_P (DECL_NAME (fndecl))
3115 : 313378 : && CP_DECL_CONTEXT (fndecl) == global_namespace
3116 : 30624077 : && (DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl)
3117 : 123837 : || DECL_IS_OPERATOR_DELETE_P (fndecl)));
3118 : : }
3119 : :
3120 : : /* Return true if FNDECL is a placement new function that should be
3121 : : useable during constant expression evaluation of std::construct_at. */
3122 : :
3123 : : static inline bool
3124 : 30096044 : cxx_placement_new_fn (tree fndecl)
3125 : : {
3126 : 30096044 : return (cxx_dialect >= cxx20 && std_placement_new_fn_p (fndecl));
3127 : : }
3128 : :
3129 : : /* Return true if FNDECL is std::construct_at. */
3130 : :
3131 : : static inline bool
3132 : 95553 : is_std_construct_at (tree fndecl)
3133 : : {
3134 : 95553 : if (!decl_in_std_namespace_p (fndecl))
3135 : : return false;
3136 : :
3137 : 89812 : tree name = DECL_NAME (fndecl);
3138 : 89812 : return name && id_equal (name, "construct_at");
3139 : : }
3140 : :
3141 : : /* Overload for the above taking constexpr_call*. */
3142 : :
3143 : : static inline bool
3144 : 67914 : is_std_construct_at (const constexpr_call *call)
3145 : : {
3146 : 67914 : return (call
3147 : 45247 : && call->fundef
3148 : 113161 : && is_std_construct_at (call->fundef->decl));
3149 : : }
3150 : :
3151 : : /* True if CTX is an instance of std::NAME class. */
3152 : :
3153 : : bool
3154 : 4009905 : is_std_class (tree ctx, const char *name)
3155 : : {
3156 : 4009905 : if (ctx == NULL_TREE || !CLASS_TYPE_P (ctx) || !TYPE_MAIN_DECL (ctx))
3157 : : return false;
3158 : :
3159 : 4009053 : tree decl = TYPE_MAIN_DECL (ctx);
3160 : 4009053 : tree dname = DECL_NAME (decl);
3161 : 4009053 : if (dname == NULL_TREE || !id_equal (dname, name))
3162 : : return false;
3163 : :
3164 : 186907 : return decl_in_std_namespace_p (decl);
3165 : : }
3166 : :
3167 : : /* True if CTX is an instance of std::allocator. */
3168 : :
3169 : : bool
3170 : 70480 : is_std_allocator (tree ctx)
3171 : : {
3172 : 70480 : return is_std_class (ctx, "allocator");
3173 : : }
3174 : :
3175 : : /* Return true if FNDECL is std::allocator<T>::{,de}allocate. */
3176 : :
3177 : : static inline bool
3178 : 77957 : is_std_allocator_allocate (tree fndecl)
3179 : : {
3180 : 77957 : tree name = DECL_NAME (fndecl);
3181 : 77957 : if (name == NULL_TREE
3182 : 77957 : || !(id_equal (name, "allocate") || id_equal (name, "deallocate")))
3183 : : return false;
3184 : :
3185 : 66352 : return is_std_allocator (DECL_CONTEXT (fndecl));
3186 : : }
3187 : :
3188 : : /* Overload for the above taking constexpr_call*. */
3189 : :
3190 : : static inline bool
3191 : 33493 : is_std_allocator_allocate (const constexpr_call *call)
3192 : : {
3193 : 33493 : return (call
3194 : 7371 : && call->fundef
3195 : 40864 : && is_std_allocator_allocate (call->fundef->decl));
3196 : : }
3197 : :
3198 : : /* Return true if FNDECL is std::source_location::current. */
3199 : :
3200 : : static inline bool
3201 : 4616 : is_std_source_location_current (tree fndecl)
3202 : : {
3203 : 4616 : if (!decl_in_std_namespace_p (fndecl))
3204 : : return false;
3205 : :
3206 : 831 : tree name = DECL_NAME (fndecl);
3207 : 831 : if (name == NULL_TREE || !id_equal (name, "current"))
3208 : : return false;
3209 : :
3210 : 159 : tree ctx = DECL_CONTEXT (fndecl);
3211 : 159 : if (ctx == NULL_TREE || !CLASS_TYPE_P (ctx) || !TYPE_MAIN_DECL (ctx))
3212 : : return false;
3213 : :
3214 : 159 : name = DECL_NAME (TYPE_MAIN_DECL (ctx));
3215 : 159 : return name && id_equal (name, "source_location");
3216 : : }
3217 : :
3218 : : /* Overload for the above taking constexpr_call*. */
3219 : :
3220 : : static inline bool
3221 : 11139 : is_std_source_location_current (const constexpr_call *call)
3222 : : {
3223 : 11139 : return (call
3224 : 4616 : && call->fundef
3225 : 15755 : && is_std_source_location_current (call->fundef->decl));
3226 : : }
3227 : :
3228 : : /* Return true if FNDECL is __dynamic_cast. */
3229 : :
3230 : : static inline bool
3231 : 30069005 : cxx_dynamic_cast_fn_p (tree fndecl)
3232 : : {
3233 : 30069005 : return (cxx_dialect >= cxx20
3234 : 8747683 : && id_equal (DECL_NAME (fndecl), "__dynamic_cast")
3235 : 30073928 : && CP_DECL_CONTEXT (fndecl) == abi_node);
3236 : : }
3237 : :
3238 : : /* Often, we have an expression in the form of address + offset, e.g.
3239 : : "&_ZTV1A + 16". Extract the object from it, i.e. "_ZTV1A". */
3240 : :
3241 : : static tree
3242 : 4363 : extract_obj_from_addr_offset (tree expr)
3243 : : {
3244 : 4363 : if (TREE_CODE (expr) == POINTER_PLUS_EXPR)
3245 : 2111 : expr = TREE_OPERAND (expr, 0);
3246 : 4363 : STRIP_NOPS (expr);
3247 : 4363 : if (TREE_CODE (expr) == ADDR_EXPR)
3248 : 4356 : expr = TREE_OPERAND (expr, 0);
3249 : 4363 : return expr;
3250 : : }
3251 : :
3252 : : /* Given a PATH like
3253 : :
3254 : : g.D.2181.D.2154.D.2102.D.2093
3255 : :
3256 : : find a component with type TYPE. Return NULL_TREE if not found, and
3257 : : error_mark_node if the component is not accessible. If STOP is non-null,
3258 : : this function will return NULL_TREE if STOP is found before TYPE. */
3259 : :
3260 : : static tree
3261 : 2156 : get_component_with_type (tree path, tree type, tree stop)
3262 : : {
3263 : 6230 : while (true)
3264 : : {
3265 : 4193 : if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path), type))
3266 : : /* Found it. */
3267 : : return path;
3268 : 3086 : else if (stop
3269 : 3086 : && (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path),
3270 : : stop)))
3271 : : return NULL_TREE;
3272 : 3041 : else if (TREE_CODE (path) == COMPONENT_REF
3273 : 3041 : && DECL_FIELD_IS_BASE (TREE_OPERAND (path, 1)))
3274 : : {
3275 : : /* We need to check that the component we're accessing is in fact
3276 : : accessible. */
3277 : 3041 : if (TREE_PRIVATE (TREE_OPERAND (path, 1))
3278 : 3041 : || TREE_PROTECTED (TREE_OPERAND (path, 1)))
3279 : 1004 : return error_mark_node;
3280 : 2037 : path = TREE_OPERAND (path, 0);
3281 : : }
3282 : : else
3283 : : return NULL_TREE;
3284 : : }
3285 : : }
3286 : :
3287 : : /* Evaluate a call to __dynamic_cast (permitted by P1327R1).
3288 : :
3289 : : The declaration of __dynamic_cast is:
3290 : :
3291 : : void* __dynamic_cast (const void* __src_ptr,
3292 : : const __class_type_info* __src_type,
3293 : : const __class_type_info* __dst_type,
3294 : : ptrdiff_t __src2dst);
3295 : :
3296 : : where src2dst has the following possible values
3297 : :
3298 : : >-1: src_type is a unique public non-virtual base of dst_type
3299 : : dst_ptr + src2dst == src_ptr
3300 : : -1: unspecified relationship
3301 : : -2: src_type is not a public base of dst_type
3302 : : -3: src_type is a multiple public non-virtual base of dst_type */
3303 : :
3304 : : static tree
3305 : 2343 : cxx_eval_dynamic_cast_fn (const constexpr_ctx *ctx, tree call,
3306 : : bool *non_constant_p, bool *overflow_p,
3307 : : tree *jump_target)
3308 : : {
3309 : : /* T will be something like
3310 : : __dynamic_cast ((B*) b, &_ZTI1B, &_ZTI1D, 8)
3311 : : dismantle it. */
3312 : 2343 : gcc_assert (call_expr_nargs (call) == 4);
3313 : 2343 : tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
3314 : 2343 : tree obj = CALL_EXPR_ARG (call, 0);
3315 : 2343 : tree type = CALL_EXPR_ARG (call, 2);
3316 : 2343 : HOST_WIDE_INT hint = int_cst_value (CALL_EXPR_ARG (call, 3));
3317 : 2343 : location_t loc = cp_expr_loc_or_input_loc (call);
3318 : :
3319 : : /* Get the target type of the dynamic_cast. */
3320 : 2343 : gcc_assert (TREE_CODE (type) == ADDR_EXPR);
3321 : 2343 : type = TREE_OPERAND (type, 0);
3322 : 2343 : type = TREE_TYPE (DECL_NAME (type));
3323 : :
3324 : : /* TYPE can only be either T* or T&. We can't know which of these it
3325 : : is by looking at TYPE, but OBJ will be "(T*) x" in the first case,
3326 : : and something like "(T*)(T&)(T*) x" in the second case.
3327 : : This is true for the reference cases in C++ < 26 or when exceptions
3328 : : aren't enabled, in that case we should diagnose errors. For C++26
3329 : : with exceptions we should silently evaluate to null pointer and
3330 : : let the callers call __cxa_bad_cast () later to throw an exception. */
3331 : 2343 : bool fail_for_non_constant_p = false;
3332 : 10905 : while (CONVERT_EXPR_P (obj) || TREE_CODE (obj) == SAVE_EXPR)
3333 : : {
3334 : 8562 : if (cxx_dialect < cxx26 || !flag_exceptions)
3335 : 5130 : fail_for_non_constant_p |= TYPE_REF_P (TREE_TYPE (obj));
3336 : 8562 : obj = TREE_OPERAND (obj, 0);
3337 : : }
3338 : :
3339 : : /* Evaluate the object so that we know its dynamic type. */
3340 : 2343 : obj = cxx_eval_constant_expression (ctx, obj, vc_prvalue, non_constant_p,
3341 : : overflow_p, jump_target);
3342 : 2343 : if (*non_constant_p)
3343 : : return call;
3344 : 2252 : if (*jump_target)
3345 : : return NULL_TREE;
3346 : :
3347 : : /* For dynamic_cast from classes with virtual bases we can get something
3348 : : like (virt_base *)(&d + 16) as OBJ. Try to convert that into
3349 : : d.D.1234 using cxx_fold_indirect_ref. */
3350 : 2252 : if (cxx_dialect >= cxx26 && CONVERT_EXPR_P (obj))
3351 : : {
3352 : 586 : tree objo = obj;
3353 : 586 : STRIP_NOPS (objo);
3354 : 586 : if (TREE_CODE (objo) == POINTER_PLUS_EXPR)
3355 : : {
3356 : 576 : objo = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (TREE_TYPE (obj)),
3357 : : obj, NULL, jump_target);
3358 : 576 : if (objo)
3359 : 576 : obj = build_fold_addr_expr (objo);
3360 : : }
3361 : : }
3362 : :
3363 : : /* We expect OBJ to be in form of &d.D.2102 when HINT == 0,
3364 : : but when HINT is > 0, it can also be something like
3365 : : &d.D.2102 + 18446744073709551608, which includes the BINFO_OFFSET. */
3366 : 2252 : obj = extract_obj_from_addr_offset (obj);
3367 : 2252 : const tree objtype = TREE_TYPE (obj);
3368 : : /* If OBJ doesn't refer to a base field, we're done. */
3369 : 4480 : if (tree t = (TREE_CODE (obj) == COMPONENT_REF
3370 : 2252 : ? TREE_OPERAND (obj, 1) : obj))
3371 : 2252 : if (TREE_CODE (t) != FIELD_DECL || !DECL_FIELD_IS_BASE (t))
3372 : : {
3373 : 24 : if (fail_for_non_constant_p)
3374 : : {
3375 : 8 : if (!ctx->quiet)
3376 : : {
3377 : 2 : auto_diagnostic_group d;
3378 : 2 : error_at (loc, "reference %<dynamic_cast%> failed");
3379 : 2 : inform (loc, "dynamic type %qT of its operand does "
3380 : : "not have a base class of type %qT",
3381 : : objtype, type);
3382 : 2 : }
3383 : 8 : *non_constant_p = true;
3384 : : }
3385 : 24 : return integer_zero_node;
3386 : : }
3387 : :
3388 : : /* [class.cdtor] When a dynamic_cast is used in a constructor ...
3389 : : or in a destructor ... if the operand of the dynamic_cast refers
3390 : : to the object under construction or destruction, this object is
3391 : : considered to be a most derived object that has the type of the
3392 : : constructor or destructor's class. */
3393 : 2228 : tree vtable = build_vfield_ref (obj, objtype);
3394 : 2228 : vtable = cxx_eval_constant_expression (ctx, vtable, vc_prvalue,
3395 : : non_constant_p, overflow_p,
3396 : : jump_target);
3397 : 2228 : if (*non_constant_p)
3398 : : return call;
3399 : 2123 : if (*jump_target)
3400 : : return NULL_TREE;
3401 : : /* With -fsanitize=vptr, we initialize all vtable pointers to null,
3402 : : so it's possible that we got a null pointer now. */
3403 : 2123 : if (integer_zerop (vtable))
3404 : : {
3405 : 12 : if (!ctx->quiet)
3406 : 3 : error_at (loc, "virtual table pointer is used uninitialized");
3407 : 12 : *non_constant_p = true;
3408 : 12 : return integer_zero_node;
3409 : : }
3410 : : /* VTABLE will be &_ZTV1A + 16 or similar, get _ZTV1A. */
3411 : 2111 : vtable = extract_obj_from_addr_offset (vtable);
3412 : 2111 : const tree mdtype = DECL_CONTEXT (vtable);
3413 : :
3414 : : /* Given dynamic_cast<T>(v),
3415 : :
3416 : : [expr.dynamic.cast] If C is the class type to which T points or refers,
3417 : : the runtime check logically executes as follows:
3418 : :
3419 : : If, in the most derived object pointed (referred) to by v, v points
3420 : : (refers) to a public base class subobject of a C object, and if only
3421 : : one object of type C is derived from the subobject pointed (referred)
3422 : : to by v the result points (refers) to that C object.
3423 : :
3424 : : In this case, HINT >= 0 or -3. */
3425 : 2111 : if (hint >= 0 || hint == -3)
3426 : : {
3427 : : /* Look for a component with type TYPE. */
3428 : 601 : tree t = get_component_with_type (obj, type, mdtype);
3429 : : /* If not accessible, give an error. */
3430 : 601 : if (t == error_mark_node)
3431 : : {
3432 : 198 : if (fail_for_non_constant_p)
3433 : : {
3434 : 108 : if (!ctx->quiet)
3435 : : {
3436 : 12 : auto_diagnostic_group d;
3437 : 12 : error_at (loc, "reference %<dynamic_cast%> failed");
3438 : 12 : inform (loc, "static type %qT of its operand is a "
3439 : : "non-public base class of dynamic type %qT",
3440 : : objtype, type);
3441 : :
3442 : 12 : }
3443 : 108 : *non_constant_p = true;
3444 : : }
3445 : 198 : return integer_zero_node;
3446 : : }
3447 : 403 : else if (t)
3448 : : /* The result points to the TYPE object. */
3449 : 358 : return cp_build_addr_expr (t, complain);
3450 : : /* Else, TYPE was not found, because the HINT turned out to be wrong.
3451 : : Fall through to the normal processing. */
3452 : : }
3453 : :
3454 : : /* Otherwise, if v points (refers) to a public base class subobject of the
3455 : : most derived object, and the type of the most derived object has a base
3456 : : class, of type C, that is unambiguous and public, the result points
3457 : : (refers) to the C subobject of the most derived object.
3458 : :
3459 : : But it can also be an invalid case. */
3460 : :
3461 : : /* Get the most derived object. */
3462 : 1555 : obj = get_component_with_type (obj, mdtype, NULL_TREE);
3463 : 1555 : if (obj == error_mark_node)
3464 : : {
3465 : 806 : if (fail_for_non_constant_p)
3466 : : {
3467 : 350 : if (!ctx->quiet)
3468 : : {
3469 : 38 : auto_diagnostic_group d;
3470 : 38 : error_at (loc, "reference %<dynamic_cast%> failed");
3471 : 38 : inform (loc, "static type %qT of its operand is a non-public"
3472 : : " base class of dynamic type %qT", objtype, mdtype);
3473 : 38 : }
3474 : 350 : *non_constant_p = true;
3475 : : }
3476 : 806 : return integer_zero_node;
3477 : : }
3478 : : else
3479 : 749 : gcc_assert (obj);
3480 : :
3481 : : /* Check that the type of the most derived object has a base class
3482 : : of type TYPE that is unambiguous and public. */
3483 : 749 : base_kind b_kind;
3484 : 749 : tree binfo = lookup_base (mdtype, type, ba_check, &b_kind, tf_none);
3485 : 749 : if (!binfo || binfo == error_mark_node)
3486 : : {
3487 : 339 : if (fail_for_non_constant_p)
3488 : : {
3489 : 134 : if (!ctx->quiet)
3490 : : {
3491 : 16 : auto_diagnostic_group d;
3492 : 16 : error_at (loc, "reference %<dynamic_cast%> failed");
3493 : 16 : if (b_kind == bk_ambig)
3494 : 6 : inform (loc, "%qT is an ambiguous base class of dynamic "
3495 : : "type %qT of its operand", type, mdtype);
3496 : : else
3497 : 10 : inform (loc, "dynamic type %qT of its operand does not "
3498 : : "have an unambiguous public base class %qT",
3499 : : mdtype, type);
3500 : 16 : }
3501 : 134 : *non_constant_p = true;
3502 : : }
3503 : 339 : return integer_zero_node;
3504 : : }
3505 : : /* If so, return the TYPE subobject of the most derived object. */
3506 : 410 : obj = convert_to_base_statically (obj, binfo);
3507 : 410 : return cp_build_addr_expr (obj, complain);
3508 : : }
3509 : :
3510 : : /* Data structure used by replace_decl and replace_decl_r. */
3511 : :
3512 : : struct replace_decl_data
3513 : : {
3514 : : /* The _DECL we want to replace. */
3515 : : tree decl;
3516 : : /* The replacement for DECL. */
3517 : : tree replacement;
3518 : : /* Trees we've visited. */
3519 : : hash_set<tree> *pset;
3520 : : /* Whether we've performed any replacements. */
3521 : : bool changed;
3522 : : };
3523 : :
3524 : : /* Helper function for replace_decl, called through cp_walk_tree. */
3525 : :
3526 : : static tree
3527 : 4347218 : replace_decl_r (tree *tp, int *walk_subtrees, void *data)
3528 : : {
3529 : 4347218 : replace_decl_data *d = (replace_decl_data *) data;
3530 : :
3531 : : /* We could be replacing
3532 : : &<retval>.bar -> &foo.bar
3533 : : where foo is a static VAR_DECL, so we need to recompute TREE_CONSTANT
3534 : : on the ADDR_EXPR around it. */
3535 : 4347218 : if (TREE_CODE (*tp) == ADDR_EXPR)
3536 : : {
3537 : 408704 : d->pset->add (*tp);
3538 : 408704 : auto save_changed = d->changed;
3539 : 408704 : d->changed = false;
3540 : 408704 : cp_walk_tree (&TREE_OPERAND (*tp, 0), replace_decl_r, d, nullptr);
3541 : 408704 : if (d->changed)
3542 : : {
3543 : 326 : cxx_mark_addressable (*tp);
3544 : 326 : recompute_tree_invariant_for_addr_expr (*tp);
3545 : : }
3546 : : else
3547 : 408378 : d->changed = save_changed;
3548 : 408704 : *walk_subtrees = 0;
3549 : : }
3550 : 3938514 : else if (*tp == d->decl)
3551 : : {
3552 : 663 : *tp = unshare_expr (d->replacement);
3553 : 663 : d->changed = true;
3554 : 663 : *walk_subtrees = 0;
3555 : : }
3556 : 3937851 : else if (TYPE_P (*tp)
3557 : 3937851 : || d->pset->add (*tp))
3558 : 676334 : *walk_subtrees = 0;
3559 : :
3560 : 4347218 : return NULL_TREE;
3561 : : }
3562 : :
3563 : : /* Replace every occurrence of DECL with (an unshared copy of)
3564 : : REPLACEMENT within the expression *TP. Returns true iff a
3565 : : replacement was performed. */
3566 : :
3567 : : bool
3568 : 614633 : replace_decl (tree *tp, tree decl, tree replacement)
3569 : : {
3570 : 614633 : gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
3571 : : (TREE_TYPE (decl), TREE_TYPE (replacement)));
3572 : 614633 : hash_set<tree> pset;
3573 : 614633 : replace_decl_data data = { decl, replacement, &pset, false };
3574 : 614633 : cp_walk_tree (tp, replace_decl_r, &data, NULL);
3575 : 614633 : return data.changed;
3576 : 614633 : }
3577 : :
3578 : : /* Evaluate the call T to virtual function thunk THUNK_FNDECL. */
3579 : :
3580 : : static tree
3581 : 27 : cxx_eval_thunk_call (const constexpr_ctx *ctx, tree t, tree thunk_fndecl,
3582 : : value_cat lval,
3583 : : bool *non_constant_p, bool *overflow_p, tree *jump_target)
3584 : : {
3585 : 27 : tree function = THUNK_TARGET (thunk_fndecl);
3586 : :
3587 : 27 : if (THUNK_VIRTUAL_OFFSET (thunk_fndecl))
3588 : : {
3589 : 11 : if (!ctx->quiet)
3590 : : {
3591 : 3 : if (!DECL_DECLARED_CONSTEXPR_P (function))
3592 : : {
3593 : 0 : error ("call to non-%<constexpr%> function %qD", function);
3594 : 0 : explain_invalid_constexpr_fn (function);
3595 : : }
3596 : : else
3597 : : /* virtual_offset is only set for virtual bases, which make the
3598 : : class non-literal, so we don't need to handle it here. */
3599 : 3 : error ("calling constexpr member function %qD through virtual "
3600 : : "base subobject", function);
3601 : : }
3602 : 11 : *non_constant_p = true;
3603 : 11 : return t;
3604 : : }
3605 : :
3606 : 16 : tree new_call = copy_node (t);
3607 : 16 : CALL_EXPR_FN (new_call) = function;
3608 : 16 : TREE_TYPE (new_call) = TREE_TYPE (TREE_TYPE (function));
3609 : :
3610 : 16 : tree offset = size_int (THUNK_FIXED_OFFSET (thunk_fndecl));
3611 : :
3612 : 16 : if (DECL_THIS_THUNK_P (thunk_fndecl))
3613 : : {
3614 : : /* 'this'-adjusting thunk. */
3615 : 10 : tree this_arg = CALL_EXPR_ARG (t, 0);
3616 : 10 : this_arg = build2 (POINTER_PLUS_EXPR, TREE_TYPE (this_arg),
3617 : : this_arg, offset);
3618 : 10 : CALL_EXPR_ARG (new_call, 0) = this_arg;
3619 : : }
3620 : : else
3621 : : /* Return-adjusting thunk. */
3622 : 6 : new_call = build2 (POINTER_PLUS_EXPR, TREE_TYPE (new_call),
3623 : : new_call, offset);
3624 : :
3625 : 16 : return cxx_eval_constant_expression (ctx, new_call, lval,
3626 : : non_constant_p, overflow_p,
3627 : 16 : jump_target);
3628 : : }
3629 : :
3630 : : /* If OBJECT is of const class type, evaluate it to a CONSTRUCTOR and set
3631 : : its TREE_READONLY flag according to READONLY_P. Used for constexpr
3632 : : 'tors to detect modifying const objects in a constexpr context. */
3633 : :
3634 : : static void
3635 : 3789611 : cxx_set_object_constness (const constexpr_ctx *ctx, tree object,
3636 : : bool readonly_p, bool *non_constant_p,
3637 : : bool *overflow_p, tree *jump_target)
3638 : : {
3639 : 7579222 : if (CLASS_TYPE_P (TREE_TYPE (object))
3640 : 7579222 : && CP_TYPE_CONST_P (TREE_TYPE (object)))
3641 : : {
3642 : : /* Subobjects might not be stored in ctx->global->values but we
3643 : : can get its CONSTRUCTOR by evaluating *this. */
3644 : 524675 : tree e = cxx_eval_constant_expression (ctx, object, vc_prvalue,
3645 : : non_constant_p, overflow_p,
3646 : : jump_target);
3647 : 524675 : if (!*non_constant_p
3648 : 4304025 : && !throws (jump_target)
3649 : 1039089 : && TREE_CODE (e) == CONSTRUCTOR)
3650 : 514414 : TREE_READONLY (e) = readonly_p;
3651 : : }
3652 : 3789611 : }
3653 : :
3654 : : /* Subroutine of cxx_eval_constant_expression.
3655 : : Evaluate the call expression tree T in the context of OLD_CALL expression
3656 : : evaluation. */
3657 : :
3658 : : static tree
3659 : 83244256 : cxx_eval_call_expression (const constexpr_ctx *ctx, tree t,
3660 : : value_cat lval,
3661 : : bool *non_constant_p, bool *overflow_p,
3662 : : tree *jump_target)
3663 : : {
3664 : 83244256 : location_t loc = cp_expr_loc_or_input_loc (t);
3665 : 83244256 : tree fun = get_function_named_in_call (t);
3666 : :
3667 : 83244256 : if (fun == NULL_TREE)
3668 : 271714 : return cxx_eval_internal_function (ctx, t, lval,
3669 : : non_constant_p, overflow_p,
3670 : 271714 : jump_target);
3671 : :
3672 : 82972542 : if (TREE_CODE (fun) != FUNCTION_DECL)
3673 : : {
3674 : : /* Might be a constexpr function pointer. */
3675 : 117542 : fun = cxx_eval_constant_expression (ctx, fun, vc_prvalue,
3676 : : non_constant_p, overflow_p,
3677 : : jump_target);
3678 : 117542 : if (*jump_target)
3679 : : return NULL_TREE;
3680 : 117542 : STRIP_NOPS (fun);
3681 : 117542 : if (TREE_CODE (fun) == ADDR_EXPR)
3682 : 18975 : fun = TREE_OPERAND (fun, 0);
3683 : : /* For TARGET_VTABLE_USES_DESCRIPTORS targets, there is no
3684 : : indirection, the called expression is a pointer into the
3685 : : virtual table which should contain FDESC_EXPR. Extract the
3686 : : FUNCTION_DECL from there. */
3687 : : else if (TARGET_VTABLE_USES_DESCRIPTORS
3688 : : && TREE_CODE (fun) == POINTER_PLUS_EXPR
3689 : : && TREE_CODE (TREE_OPERAND (fun, 0)) == ADDR_EXPR
3690 : : && TREE_CODE (TREE_OPERAND (fun, 1)) == INTEGER_CST)
3691 : : {
3692 : : tree d = TREE_OPERAND (TREE_OPERAND (fun, 0), 0);
3693 : : if (VAR_P (d)
3694 : : && DECL_VTABLE_OR_VTT_P (d)
3695 : : && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE
3696 : : && TREE_TYPE (TREE_TYPE (d)) == vtable_entry_type
3697 : : && DECL_INITIAL (d)
3698 : : && TREE_CODE (DECL_INITIAL (d)) == CONSTRUCTOR)
3699 : : {
3700 : : tree i = int_const_binop (TRUNC_DIV_EXPR, TREE_OPERAND (fun, 1),
3701 : : TYPE_SIZE_UNIT (vtable_entry_type));
3702 : : HOST_WIDE_INT idx = find_array_ctor_elt (DECL_INITIAL (d), i);
3703 : : if (idx >= 0)
3704 : : {
3705 : : tree fdesc
3706 : : = (*CONSTRUCTOR_ELTS (DECL_INITIAL (d)))[idx].value;
3707 : : if (TREE_CODE (fdesc) == FDESC_EXPR
3708 : : && integer_zerop (TREE_OPERAND (fdesc, 1)))
3709 : : fun = TREE_OPERAND (fdesc, 0);
3710 : : }
3711 : : }
3712 : : }
3713 : : }
3714 : 82972542 : if (TREE_CODE (fun) != FUNCTION_DECL)
3715 : : {
3716 : 98567 : if (!ctx->quiet && !*non_constant_p)
3717 : 0 : error_at (loc, "expression %qE does not designate a %<constexpr%> "
3718 : : "function", fun);
3719 : 98567 : *non_constant_p = true;
3720 : 98567 : return t;
3721 : : }
3722 : 82873975 : tree orig_fun = fun;
3723 : 82873975 : if (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun))
3724 : 8819920 : fun = DECL_CLONED_FUNCTION (fun);
3725 : :
3726 : 82873975 : if (is_ubsan_builtin_p (fun))
3727 : 58 : return void_node;
3728 : :
3729 : 82873917 : if (fndecl_built_in_p (fun))
3730 : 10852750 : return cxx_eval_builtin_function_call (ctx, t, fun,
3731 : : lval, non_constant_p, overflow_p,
3732 : 10852750 : jump_target);
3733 : 72021167 : if (DECL_THUNK_P (fun))
3734 : 27 : return cxx_eval_thunk_call (ctx, t, fun, lval, non_constant_p, overflow_p,
3735 : 27 : jump_target);
3736 : 72021140 : bool non_constexpr_call = false;
3737 : 72021140 : if (!maybe_constexpr_fn (fun))
3738 : : {
3739 : 189036 : if (TREE_CODE (t) == CALL_EXPR
3740 : 177725 : && cxx_replaceable_global_alloc_fn (fun)
3741 : 258552 : && (CALL_FROM_NEW_OR_DELETE_P (t)
3742 : 21429 : || is_std_allocator_allocate (ctx->call)))
3743 : : {
3744 : 49917 : const bool new_op_p = IDENTIFIER_NEW_OP_P (DECL_NAME (fun));
3745 : 49917 : const int nargs = call_expr_nargs (t);
3746 : 49917 : tree arg0 = NULL_TREE;
3747 : 79007 : for (int i = 0; i < nargs; ++i)
3748 : : {
3749 : 50299 : tree arg = CALL_EXPR_ARG (t, i);
3750 : 50299 : arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
3751 : : non_constant_p, overflow_p,
3752 : : jump_target);
3753 : 50299 : if (*jump_target)
3754 : : return NULL_TREE;
3755 : : /* Deleting a non-constant pointer has a better error message
3756 : : below. */
3757 : 50291 : if (new_op_p || i != 0)
3758 : 48904 : VERIFY_CONSTANT (arg);
3759 : 27703 : if (i == 0)
3760 : : arg0 = arg;
3761 : : }
3762 : 28708 : gcc_assert (arg0);
3763 : 28708 : if (new_op_p)
3764 : : {
3765 : 27321 : if (!tree_fits_uhwi_p (arg0))
3766 : : {
3767 : : /* We should not get here; the VERIFY_CONSTANT above
3768 : : should have already caught it. */
3769 : 0 : gcc_checking_assert (false);
3770 : : if (!ctx->quiet)
3771 : : error_at (loc, "cannot allocate array: size not constant");
3772 : : *non_constant_p = true;
3773 : : return t;
3774 : : }
3775 : 54642 : tree type = build_array_type_nelts (char_type_node,
3776 : 27321 : tree_to_uhwi (arg0));
3777 : 27321 : tree var = build_decl (loc, VAR_DECL,
3778 : 27321 : (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
3779 : : & OVL_OP_FLAG_VEC)
3780 : : ? heap_vec_uninit_identifier
3781 : : : heap_uninit_identifier,
3782 : 27321 : type);
3783 : 27321 : DECL_ARTIFICIAL (var) = 1;
3784 : 27321 : ctx->global->heap_vars.safe_push (var);
3785 : 27321 : ctx->global->put_value (var, NULL_TREE);
3786 : 27321 : return fold_convert (ptr_type_node, build_address (var));
3787 : : }
3788 : : else
3789 : : {
3790 : 1387 : STRIP_NOPS (arg0);
3791 : 1387 : if (TREE_CODE (arg0) == ADDR_EXPR
3792 : 1387 : && VAR_P (TREE_OPERAND (arg0, 0)))
3793 : : {
3794 : 1387 : tree var = TREE_OPERAND (arg0, 0);
3795 : 1387 : if (DECL_NAME (var) == heap_uninit_identifier
3796 : 1387 : || DECL_NAME (var) == heap_identifier)
3797 : : {
3798 : 1224 : if (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
3799 : : & OVL_OP_FLAG_VEC)
3800 : : {
3801 : 9 : if (!ctx->quiet)
3802 : : {
3803 : 3 : auto_diagnostic_group d;
3804 : 3 : error_at (loc, "array deallocation of object "
3805 : : "allocated with non-array "
3806 : : "allocation");
3807 : 3 : inform (DECL_SOURCE_LOCATION (var),
3808 : : "allocation performed here");
3809 : 3 : }
3810 : 9 : *non_constant_p = true;
3811 : 9 : return t;
3812 : : }
3813 : 1215 : DECL_NAME (var) = heap_deleted_identifier;
3814 : 1215 : ctx->global->destroy_value (var);
3815 : 1215 : ctx->global->heap_dealloc_count++;
3816 : 1215 : return void_node;
3817 : : }
3818 : 163 : else if (DECL_NAME (var) == heap_vec_uninit_identifier
3819 : 163 : || DECL_NAME (var) == heap_vec_identifier)
3820 : : {
3821 : 139 : if ((IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
3822 : : & OVL_OP_FLAG_VEC) == 0)
3823 : : {
3824 : 9 : if (!ctx->quiet)
3825 : : {
3826 : 3 : auto_diagnostic_group d;
3827 : 3 : error_at (loc, "non-array deallocation of "
3828 : : "object allocated with array "
3829 : : "allocation");
3830 : 3 : inform (DECL_SOURCE_LOCATION (var),
3831 : : "allocation performed here");
3832 : 3 : }
3833 : 9 : *non_constant_p = true;
3834 : 9 : return t;
3835 : : }
3836 : 130 : DECL_NAME (var) = heap_deleted_identifier;
3837 : 130 : ctx->global->destroy_value (var);
3838 : 130 : ctx->global->heap_dealloc_count++;
3839 : 130 : return void_node;
3840 : : }
3841 : 24 : else if (DECL_NAME (var) == heap_deleted_identifier)
3842 : : {
3843 : 15 : if (!ctx->quiet)
3844 : 6 : error_at (loc, "deallocation of already deallocated "
3845 : : "storage");
3846 : 15 : *non_constant_p = true;
3847 : 15 : return t;
3848 : : }
3849 : : }
3850 : 9 : if (!ctx->quiet)
3851 : 3 : error_at (loc, "deallocation of storage that was "
3852 : : "not previously allocated");
3853 : 9 : *non_constant_p = true;
3854 : 9 : return t;
3855 : : }
3856 : : }
3857 : : /* Allow placement new in std::construct_at, just return the second
3858 : : argument. */
3859 : 139119 : if (TREE_CODE (t) == CALL_EXPR
3860 : 127808 : && cxx_placement_new_fn (fun)
3861 : 160583 : && is_std_construct_at (ctx->call))
3862 : : {
3863 : 5316 : const int nargs = call_expr_nargs (t);
3864 : 5316 : tree arg1 = NULL_TREE;
3865 : 15948 : for (int i = 0; i < nargs; ++i)
3866 : : {
3867 : 10632 : tree arg = CALL_EXPR_ARG (t, i);
3868 : 10632 : arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
3869 : : non_constant_p, overflow_p,
3870 : : jump_target);
3871 : 10632 : if (*jump_target)
3872 : : return NULL_TREE;
3873 : 10632 : if (i == 1)
3874 : : arg1 = arg;
3875 : : else
3876 : 10632 : VERIFY_CONSTANT (arg);
3877 : : }
3878 : 5316 : gcc_assert (arg1);
3879 : : return arg1;
3880 : : }
3881 : 133803 : else if (cxx_dynamic_cast_fn_p (fun))
3882 : 2343 : return cxx_eval_dynamic_cast_fn (ctx, t, non_constant_p, overflow_p,
3883 : 2343 : jump_target);
3884 : 131460 : else if (enum cxa_builtin kind = cxx_cxa_builtin_fn_p (fun))
3885 : 23765 : return cxx_eval_cxa_builtin_fn (ctx, t, kind, fun,
3886 : : non_constant_p, overflow_p,
3887 : 23765 : jump_target);
3888 : :
3889 : : /* Calls to non-constexpr functions can be diagnosed right away
3890 : : before C++26, though in C++26 evaluation of the arguments might
3891 : : throw and if caught it could be still constant expression.
3892 : : So for C++26 this is diagnosed only after
3893 : : cxx_bind_parameters_in_call. */
3894 : 107695 : if (cxx_dialect >= cxx26)
3895 : : non_constexpr_call = true;
3896 : : else
3897 : : {
3898 : 30406 : if (!ctx->quiet)
3899 : : {
3900 : 188 : if (!lambda_static_thunk_p (fun))
3901 : 188 : error_at (loc, "call to non-%<constexpr%> function %qD", fun);
3902 : 188 : explain_invalid_constexpr_fn (fun);
3903 : : }
3904 : 30406 : *non_constant_p = true;
3905 : 30406 : return t;
3906 : : }
3907 : : }
3908 : :
3909 : 71909393 : constexpr_ctx new_ctx = *ctx;
3910 : 71909393 : ctx = &new_ctx;
3911 : 80525062 : if (DECL_CONSTRUCTOR_P (fun) && !ctx->object
3912 : 73182881 : && TREE_CODE (t) == AGGR_INIT_EXPR)
3913 : : {
3914 : : /* We want to have an initialization target for an AGGR_INIT_EXPR.
3915 : : If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
3916 : 583 : new_ctx.object = AGGR_INIT_EXPR_SLOT (t);
3917 : 583 : tree ctor = new_ctx.ctor = build_constructor (DECL_CONTEXT (fun), NULL);
3918 : 583 : CONSTRUCTOR_NO_CLEARING (ctor) = true;
3919 : 583 : ctx->global->put_value (new_ctx.object, ctor);
3920 : : }
3921 : : /* An immediate invocation is manifestly constant evaluated including the
3922 : : arguments of the call, so use mce_true even for the argument
3923 : : evaluation. */
3924 : 143818786 : if (DECL_IMMEDIATE_FUNCTION_P (fun))
3925 : 957068 : new_ctx.manifestly_const_eval = mce_true;
3926 : :
3927 : : /* We used to shortcut trivial constructor/op= here, but nowadays
3928 : : we can only get a trivial function here with -fno-elide-constructors. */
3929 : 71909393 : gcc_checking_assert (!trivial_fn_p (fun)
3930 : : || !flag_elide_constructors
3931 : : /* Or it's a call from maybe_thunk_body (111075). */
3932 : : || (TREE_CODE (t) == CALL_EXPR ? CALL_FROM_THUNK_P (t)
3933 : : : AGGR_INIT_FROM_THUNK_P (t))
3934 : : /* We don't elide constructors when processing
3935 : : a noexcept-expression. */
3936 : : || cp_noexcept_operand);
3937 : :
3938 : 71909393 : bool non_constant_args = false;
3939 : 71909393 : constexpr_call new_call;
3940 : 71909393 : new_call.bindings
3941 : 71909393 : = cxx_bind_parameters_in_call (ctx, t, fun, orig_fun, non_constant_p,
3942 : : overflow_p, &non_constant_args,
3943 : : jump_target);
3944 : :
3945 : : /* We build up the bindings list before we know whether we already have this
3946 : : call cached. If we don't end up saving these bindings, ggc_free them when
3947 : : this function exits. */
3948 : 71909393 : class free_bindings
3949 : : {
3950 : : tree *bindings;
3951 : : public:
3952 : 71909393 : free_bindings (tree &b): bindings (&b) { }
3953 : 71909393 : ~free_bindings () { if (bindings) ggc_free (*bindings); }
3954 : 2946508 : void preserve () { bindings = NULL; }
3955 : 71909393 : } fb (new_call.bindings);
3956 : :
3957 : 71909393 : if (*jump_target)
3958 : : return NULL_TREE;
3959 : 71909387 : if (*non_constant_p)
3960 : : return t;
3961 : 39508545 : if (non_constexpr_call)
3962 : : {
3963 : 5631 : if (!ctx->quiet)
3964 : : {
3965 : 149 : if (!lambda_static_thunk_p (fun))
3966 : 149 : error_at (loc, "call to non-%<constexpr%> function %qD", fun);
3967 : 149 : explain_invalid_constexpr_fn (fun);
3968 : : }
3969 : 5631 : *non_constant_p = true;
3970 : 5631 : return t;
3971 : : }
3972 : :
3973 : : /* We can't defer instantiating the function any longer. */
3974 : 39502914 : if (!DECL_INITIAL (fun)
3975 : 1648031 : && (DECL_TEMPLOID_INSTANTIATION (fun) || DECL_DEFAULTED_FN (fun))
3976 : 39502914 : && !uid_sensitive_constexpr_evaluation_p ())
3977 : : {
3978 : 1449944 : location_t save_loc = input_location;
3979 : 1449944 : input_location = loc;
3980 : 1449944 : ++function_depth;
3981 : 1449944 : if (ctx->manifestly_const_eval == mce_true)
3982 : 165148 : FNDECL_MANIFESTLY_CONST_EVALUATED (fun) = true;
3983 : 1449944 : if (DECL_TEMPLOID_INSTANTIATION (fun))
3984 : 1449936 : instantiate_decl (fun, /*defer_ok*/false, /*expl_inst*/false);
3985 : : else
3986 : 8 : synthesize_method (fun);
3987 : 1449944 : --function_depth;
3988 : 1449944 : input_location = save_loc;
3989 : : }
3990 : :
3991 : : /* If in direct recursive call, optimize definition search. */
3992 : 39502914 : if (ctx && ctx->call && ctx->call->fundef && ctx->call->fundef->decl == fun)
3993 : 27533 : new_call.fundef = ctx->call->fundef;
3994 : : else
3995 : : {
3996 : 39475381 : new_call.fundef = retrieve_constexpr_fundef (fun);
3997 : 39475381 : if (new_call.fundef == NULL || new_call.fundef->body == NULL
3998 : 39181625 : || new_call.fundef->result == error_mark_node
3999 : 39159775 : || fun == current_function_decl)
4000 : : {
4001 : 315609 : if (!ctx->quiet)
4002 : : {
4003 : : /* We need to check for current_function_decl here in case we're
4004 : : being called during cp_fold_function, because at that point
4005 : : DECL_INITIAL is set properly and we have a fundef but we
4006 : : haven't lowered invisirefs yet (c++/70344). */
4007 : 150 : if (DECL_INITIAL (fun) == error_mark_node
4008 : 150 : || fun == current_function_decl)
4009 : 15 : error_at (loc, "%qD called in a constant expression before its "
4010 : : "definition is complete", fun);
4011 : 135 : else if (DECL_INITIAL (fun))
4012 : : {
4013 : : /* The definition of fun was somehow unsuitable. But pretend
4014 : : that lambda static thunks don't exist. */
4015 : 98 : if (!lambda_static_thunk_p (fun))
4016 : 98 : error_at (loc, "%qD called in a constant expression", fun);
4017 : 98 : explain_invalid_constexpr_fn (fun);
4018 : : }
4019 : : else
4020 : 37 : error_at (loc, "%qD used before its definition", fun);
4021 : : }
4022 : 315609 : *non_constant_p = true;
4023 : 315609 : return t;
4024 : : }
4025 : : }
4026 : :
4027 : : /* Don't complain about problems evaluating an ill-formed function. */
4028 : 39187305 : if (function *f = DECL_STRUCT_FUNCTION (fun))
4029 : 39187305 : if (f->language->erroneous)
4030 : 509 : new_ctx.quiet = true;
4031 : :
4032 : 39187305 : int depth_ok = push_cx_call_context (t);
4033 : :
4034 : : /* Remember the object we are constructing or destructing. */
4035 : 39187305 : tree new_obj = NULL_TREE;
4036 : 78374610 : if (DECL_CONSTRUCTOR_P (fun) || DECL_DESTRUCTOR_P (fun))
4037 : : {
4038 : : /* In a cdtor, it should be the first `this' argument.
4039 : : At this point it has already been evaluated in the call
4040 : : to cxx_bind_parameters_in_call. */
4041 : 4579270 : new_obj = TREE_VEC_ELT (new_call.bindings, 0);
4042 : 4579270 : bool empty_base = false;
4043 : 4579270 : new_obj = cxx_fold_indirect_ref (ctx, loc, DECL_CONTEXT (fun), new_obj,
4044 : : &empty_base, jump_target);
4045 : 4579270 : if (*jump_target)
4046 : 0 : return NULL_TREE;
4047 : : /* If we're initializing an empty class, don't set constness, because
4048 : : cxx_fold_indirect_ref will return the wrong object to set constness
4049 : : of. */
4050 : 4579270 : if (empty_base)
4051 : : new_obj = NULL_TREE;
4052 : 1889860 : else if (ctx->call && ctx->call->fundef
4053 : 7589931 : && DECL_CONSTRUCTOR_P (ctx->call->fundef->decl))
4054 : : {
4055 : 1187141 : tree cur_obj = TREE_VEC_ELT (ctx->call->bindings, 0);
4056 : 1187141 : STRIP_NOPS (cur_obj);
4057 : 1187141 : if (TREE_CODE (cur_obj) == ADDR_EXPR)
4058 : 1185894 : cur_obj = TREE_OPERAND (cur_obj, 0);
4059 : 1187141 : if (new_obj == cur_obj)
4060 : : /* We're calling the target constructor of a delegating
4061 : : constructor, or accessing a base subobject through a
4062 : : NOP_EXPR as part of a call to a base constructor, so
4063 : : there is no new (sub)object. */
4064 : 789659 : new_obj = NULL_TREE;
4065 : : }
4066 : : }
4067 : :
4068 : 39187305 : tree result = NULL_TREE;
4069 : :
4070 : 39187305 : constexpr_call *entry = NULL;
4071 : 39187305 : if (depth_ok && !non_constant_args && ctx->strict)
4072 : : {
4073 : 18973007 : new_call.hash = constexpr_fundef_hasher::hash (new_call.fundef);
4074 : 18973007 : new_call.hash
4075 : 18973007 : = iterative_hash_template_arg (new_call.bindings, new_call.hash);
4076 : :
4077 : : /* If we have seen this call before, we are done. */
4078 : 18973007 : maybe_initialize_constexpr_call_table ();
4079 : 18973007 : bool insert = depth_ok < constexpr_cache_depth;
4080 : 18973007 : constexpr_call **slot
4081 : 19240958 : = constexpr_call_table->find_slot (&new_call,
4082 : : insert ? INSERT : NO_INSERT);
4083 : 18973007 : entry = slot ? *slot : NULL;
4084 : 18837560 : if (entry == NULL)
4085 : : {
4086 : : /* Only cache up to constexpr_cache_depth to limit memory use. */
4087 : 3081955 : if (insert)
4088 : : {
4089 : : /* We need to keep a pointer to the entry, not just the slot, as
4090 : : the slot can move during evaluation of the body. */
4091 : 2946508 : *slot = entry = ggc_alloc<constexpr_call> ();
4092 : 2946508 : *entry = new_call;
4093 : 2946508 : entry->result (ctx->manifestly_const_eval) = unknown_type_node;
4094 : 2946508 : fb.preserve ();
4095 : : }
4096 : : }
4097 : : /* Calls that are in progress have their result set to unknown_type_node,
4098 : : so that we can detect circular dependencies. Now that we only cache
4099 : : up to constexpr_cache_depth this won't catch circular dependencies that
4100 : : start deeper, but they'll hit the recursion or ops limit. */
4101 : 15891052 : else if (entry->result (ctx->manifestly_const_eval) == unknown_type_node)
4102 : : {
4103 : 6 : if (!ctx->quiet)
4104 : 0 : error ("call has circular dependency");
4105 : 6 : *non_constant_p = true;
4106 : 6 : entry->result (ctx->manifestly_const_eval) = result = error_mark_node;
4107 : : }
4108 : : else
4109 : 15891046 : result = entry->result (ctx->manifestly_const_eval);
4110 : : }
4111 : :
4112 : 39187305 : if (!depth_ok)
4113 : : {
4114 : 30 : if (!ctx->quiet)
4115 : 3 : error ("%<constexpr%> evaluation depth exceeds maximum of %d (use "
4116 : : "%<-fconstexpr-depth=%> to increase the maximum)",
4117 : : max_constexpr_depth);
4118 : 30 : *non_constant_p = true;
4119 : 30 : result = error_mark_node;
4120 : : }
4121 : : else
4122 : : {
4123 : 39187275 : bool cacheable = !!entry;
4124 : 39187275 : if (result && result != error_mark_node)
4125 : : /* OK */;
4126 : 26340119 : else if (!DECL_SAVED_TREE (fun))
4127 : : {
4128 : : /* When at_eof >= 3, cgraph has started throwing away
4129 : : DECL_SAVED_TREE, so fail quietly. FIXME we get here because of
4130 : : late code generation for VEC_INIT_EXPR, which needs to be
4131 : : completely reconsidered. */
4132 : 0 : gcc_assert (at_eof >= 3 && ctx->quiet);
4133 : 0 : *non_constant_p = true;
4134 : : }
4135 : 26340119 : else if (tree copy = get_fundef_copy (new_call.fundef))
4136 : : {
4137 : 26340119 : tree body, parms, res;
4138 : 26340119 : releasing_vec ctors;
4139 : :
4140 : : /* Reuse or create a new unshared copy of this function's body. */
4141 : 26340119 : body = TREE_PURPOSE (copy);
4142 : 26340119 : parms = TREE_VALUE (copy);
4143 : 26340119 : res = TREE_TYPE (copy);
4144 : :
4145 : : /* Associate the bindings with the remapped parms. */
4146 : 26340119 : tree bound = new_call.bindings;
4147 : 26340119 : tree remapped = parms;
4148 : 57284023 : for (int i = 0; i < TREE_VEC_LENGTH (bound); ++i)
4149 : : {
4150 : 30943904 : tree arg = TREE_VEC_ELT (bound, i);
4151 : 30943904 : if (entry)
4152 : : {
4153 : : /* Unshare args going into the hash table to separate them
4154 : : from the caller's context, for better GC and to avoid
4155 : : problems with verify_gimple. */
4156 : 1954631 : arg = unshare_expr_without_location (arg);
4157 : 1954631 : TREE_VEC_ELT (bound, i) = arg;
4158 : :
4159 : : /* And then unshare again so the callee doesn't change the
4160 : : argument values in the hash table. XXX Could we unshare
4161 : : lazily in cxx_eval_store_expression? */
4162 : 1954631 : arg = unshare_constructor (arg);
4163 : 1954631 : if (TREE_CODE (arg) == CONSTRUCTOR)
4164 : 739897 : vec_safe_push (ctors, arg);
4165 : : }
4166 : 30943904 : ctx->global->put_value (remapped, arg);
4167 : 30943904 : remapped = DECL_CHAIN (remapped);
4168 : : }
4169 : 26340119 : if (remapped)
4170 : : {
4171 : : /* We shouldn't have any parms without args, but fail gracefully
4172 : : in error recovery. */
4173 : 6 : gcc_checking_assert (seen_error ());
4174 : 6 : *non_constant_p = true;
4175 : : }
4176 : : /* Add the RESULT_DECL to the values map, too. */
4177 : 26340119 : gcc_assert (!DECL_BY_REFERENCE (res));
4178 : 26340119 : ctx->global->put_value (res, NULL_TREE);
4179 : :
4180 : : /* Remember the current call we're evaluating. */
4181 : 26340119 : constexpr_ctx call_ctx = *ctx;
4182 : 26340119 : call_ctx.call = &new_call;
4183 : 26340119 : unsigned save_heap_alloc_count = ctx->global->heap_vars.length ();
4184 : 26340119 : unsigned save_heap_dealloc_count = ctx->global->heap_dealloc_count;
4185 : :
4186 : : /* Make sure we fold std::is_constant_evaluated to true in an
4187 : : immediate function. */
4188 : 52680238 : if (DECL_IMMEDIATE_FUNCTION_P (fun))
4189 : 382319 : call_ctx.manifestly_const_eval = mce_true;
4190 : :
4191 : : /* If this is a constexpr destructor, the object's const and volatile
4192 : : semantics are no longer in effect; see [class.dtor]p5. */
4193 : 30129730 : if (new_obj && DECL_DESTRUCTOR_P (fun))
4194 : 187746 : cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/false,
4195 : : non_constant_p, overflow_p, jump_target);
4196 : :
4197 : : /* If this is a constructor, we are beginning the lifetime of the
4198 : : object we are initializing. */
4199 : 26340119 : if (new_obj
4200 : 7579222 : && DECL_CONSTRUCTOR_P (fun)
4201 : 3601865 : && TREE_CODE (new_obj) == COMPONENT_REF
4202 : 27190710 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (new_obj, 0))) == UNION_TYPE)
4203 : : {
4204 : 2928 : tree activate = build2 (INIT_EXPR, TREE_TYPE (new_obj),
4205 : : new_obj,
4206 : 2928 : build_constructor (TREE_TYPE (new_obj),
4207 : : NULL));
4208 : 2928 : cxx_eval_constant_expression (ctx, activate,
4209 : : lval, non_constant_p, overflow_p,
4210 : : jump_target);
4211 : 2928 : ggc_free (activate);
4212 : 2928 : if (*jump_target)
4213 : 0 : return NULL_TREE;
4214 : : }
4215 : :
4216 : 26340119 : tree jmp_target = NULL_TREE;
4217 : 26340119 : cxx_eval_constant_expression (&call_ctx, body,
4218 : : vc_discard, non_constant_p, overflow_p,
4219 : : &jmp_target);
4220 : :
4221 : 26340119 : if (!*non_constant_p && throws (&jmp_target))
4222 : : {
4223 : 289 : result = NULL_TREE;
4224 : 289 : cacheable = false;
4225 : 289 : *jump_target = jmp_target;
4226 : : }
4227 : 52679660 : else if (DECL_CONSTRUCTOR_P (fun))
4228 : : /* This can be null for a subobject constructor call, in
4229 : : which case what we care about is the initialization
4230 : : side-effects rather than the value. We could get at the
4231 : : value by evaluating *this, but we don't bother; there's
4232 : : no need to put such a call in the hash table. */
4233 : 4388192 : result = lval ? ctx->object : ctx->ctor;
4234 : 21951638 : else if (VOID_TYPE_P (TREE_TYPE (res)))
4235 : 1268092 : result = void_node;
4236 : : else
4237 : : {
4238 : 20683546 : result = ctx->global->get_value (res);
4239 : 7536566 : if (result == NULL_TREE && !*non_constant_p
4240 : 20683736 : && !DECL_DESTRUCTOR_P (fun))
4241 : : {
4242 : 95 : if (!ctx->quiet)
4243 : 6 : error ("%<constexpr%> call flows off the end "
4244 : : "of the function");
4245 : 95 : *non_constant_p = true;
4246 : : }
4247 : : }
4248 : :
4249 : : /* At this point, the object's constructor will have run, so
4250 : : the object is no longer under construction, and its possible
4251 : : 'const' semantics now apply. Make a note of this fact by
4252 : : marking the CONSTRUCTOR TREE_READONLY. */
4253 : 30129730 : if (new_obj && DECL_CONSTRUCTOR_P (fun))
4254 : 3601865 : cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/true,
4255 : : non_constant_p, overflow_p, jump_target);
4256 : :
4257 : : /* Remove the parms/result from the values map. */
4258 : 26340119 : destroy_value_checked (ctx, res, non_constant_p);
4259 : 57284029 : for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
4260 : 30943910 : destroy_value_checked (ctx, parm, non_constant_p);
4261 : :
4262 : : /* Free any parameter CONSTRUCTORs we aren't returning directly. */
4263 : 27080016 : while (!ctors->is_empty ())
4264 : : {
4265 : 739897 : tree c = ctors->pop ();
4266 : 739897 : if (c != result)
4267 : 739897 : free_constructor (c);
4268 : : }
4269 : :
4270 : : /* Make the unshared function copy we used available for re-use. */
4271 : 26340119 : save_fundef_copy (fun, copy);
4272 : :
4273 : : /* If the call allocated some heap object that hasn't been
4274 : : deallocated during the call, or if it deallocated some heap
4275 : : object it has not allocated, the call isn't really stateless
4276 : : for the constexpr evaluation and should not be cached.
4277 : : It is fine if the call allocates something and deallocates it
4278 : : too. */
4279 : 26340119 : if (cacheable
4280 : 32330301 : && (save_heap_alloc_count != ctx->global->heap_vars.length ()
4281 : 5989457 : || (save_heap_dealloc_count
4282 : 5989457 : != ctx->global->heap_dealloc_count)))
4283 : : {
4284 : 725 : tree heap_var;
4285 : 725 : unsigned int i;
4286 : 725 : if ((ctx->global->heap_vars.length ()
4287 : 725 : - ctx->global->heap_dealloc_count)
4288 : 725 : != save_heap_alloc_count - save_heap_dealloc_count)
4289 : : cacheable = false;
4290 : : else
4291 : 1932 : FOR_EACH_VEC_ELT_FROM (ctx->global->heap_vars, i, heap_var,
4292 : : save_heap_alloc_count)
4293 : 1429 : if (DECL_NAME (heap_var) != heap_deleted_identifier)
4294 : : {
4295 : : cacheable = false;
4296 : : break;
4297 : : }
4298 : : }
4299 : :
4300 : : /* Rewrite all occurrences of the function's RESULT_DECL with the
4301 : : current object under construction. */
4302 : 26340119 : if (!*non_constant_p
4303 : 17185883 : && ctx->object
4304 : 5247578 : && CLASS_TYPE_P (TREE_TYPE (res))
4305 : 27643418 : && !is_empty_class (TREE_TYPE (res)))
4306 : : {
4307 : 614457 : if (!same_type_ignoring_top_level_qualifiers_p
4308 : 614457 : (TREE_TYPE (res), TREE_TYPE (ctx->object)))
4309 : 0 : *non_constant_p = true;
4310 : 614457 : else if (replace_decl (&result, res, ctx->object))
4311 : : {
4312 : 238 : cacheable = false;
4313 : 238 : result = cxx_eval_constant_expression (ctx, result, lval,
4314 : : non_constant_p,
4315 : : overflow_p,
4316 : : jump_target);
4317 : 238 : if (*jump_target)
4318 : : {
4319 : 0 : cacheable = false;
4320 : 0 : result = NULL_TREE;
4321 : : }
4322 : : }
4323 : : }
4324 : :
4325 : : /* Only cache a permitted result of a constant expression. */
4326 : 26339881 : if (cacheable && !reduced_constant_expression_p (result))
4327 : : cacheable = false;
4328 : 26340119 : }
4329 : : else
4330 : : /* Couldn't get a function copy to evaluate. */
4331 : 0 : *non_constant_p = true;
4332 : :
4333 : 39187275 : if (result == error_mark_node)
4334 : 0 : *non_constant_p = true;
4335 : 39187275 : if (*non_constant_p || *overflow_p)
4336 : 9154392 : result = error_mark_node;
4337 : 30032883 : else if (!result)
4338 : 1265989 : result = void_node;
4339 : 39187275 : if (entry)
4340 : : {
4341 : 37675120 : entry->result (ctx->manifestly_const_eval)
4342 : 18837560 : = cacheable ? result : error_mark_node;
4343 : :
4344 : 18837560 : if (result != error_mark_node
4345 : 15858805 : && ctx->manifestly_const_eval == mce_unknown)
4346 : : {
4347 : : /* Evaluation succeeded and was independent of whether we're in a
4348 : : manifestly constant-evaluated context, so we can also reuse
4349 : : this result when evaluating this call with a fixed context. */
4350 : 3412034 : if (!entry->result (mce_true))
4351 : 631286 : entry->result (mce_true) = entry->result (mce_unknown);
4352 : 3412034 : if (!entry->result (mce_false))
4353 : 619896 : entry->result (mce_false) = entry->result (mce_unknown);
4354 : : }
4355 : : }
4356 : : }
4357 : :
4358 : : /* The result of a constexpr function must be completely initialized.
4359 : :
4360 : : However, in C++20, a constexpr constructor doesn't necessarily have
4361 : : to initialize all the fields, so we don't clear CONSTRUCTOR_NO_CLEARING
4362 : : in order to detect reading an unitialized object in constexpr instead
4363 : : of value-initializing it. (reduced_constant_expression_p is expected to
4364 : : take care of clearing the flag.) */
4365 : 39187305 : if (TREE_CODE (result) == CONSTRUCTOR
4366 : 39187305 : && (cxx_dialect < cxx20
4367 : 7053266 : || !DECL_CONSTRUCTOR_P (fun)))
4368 : 3674020 : clear_no_implicit_zero (result);
4369 : :
4370 : 39187305 : pop_cx_call_context ();
4371 : 39187305 : return result;
4372 : 71909393 : }
4373 : :
4374 : : /* Return true if T is a valid constant initializer. If a CONSTRUCTOR
4375 : : initializes all the members, the CONSTRUCTOR_NO_CLEARING flag will be
4376 : : cleared. If called recursively on a FIELD_DECL's CONSTRUCTOR, SZ
4377 : : is DECL_SIZE of the FIELD_DECL, otherwise NULL.
4378 : : FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
4379 : :
4380 : : bool
4381 : 608571685 : reduced_constant_expression_p (tree t, tree sz /* = NULL_TREE */)
4382 : : {
4383 : 608571685 : if (t == NULL_TREE)
4384 : : return false;
4385 : :
4386 : 605594255 : switch (TREE_CODE (t))
4387 : : {
4388 : : case PTRMEM_CST:
4389 : : /* Even if we can't lower this yet, it's constant. */
4390 : : return true;
4391 : :
4392 : : case OMP_DECLARE_MAPPER:
4393 : : return true;
4394 : :
4395 : 43309332 : case CONSTRUCTOR:
4396 : : /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
4397 : 43309332 : tree field;
4398 : 43309332 : if (!AGGREGATE_TYPE_P (TREE_TYPE (t)))
4399 : : /* A constant vector would be folded to VECTOR_CST.
4400 : : A CONSTRUCTOR of scalar type means uninitialized. */
4401 : : return false;
4402 : 43294038 : if (CONSTRUCTOR_NO_CLEARING (t))
4403 : : {
4404 : 1253836 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
4405 : : {
4406 : : /* There must be a valid constant initializer at every array
4407 : : index. */
4408 : 1007 : tree min = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (t)));
4409 : 1007 : tree max = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (t)));
4410 : 1007 : tree cursor = min;
4411 : 21340 : for (auto &e: CONSTRUCTOR_ELTS (t))
4412 : : {
4413 : 18429 : if (!reduced_constant_expression_p (e.value))
4414 : : return false;
4415 : 18379 : if (array_index_cmp (cursor, e.index) != 0)
4416 : : return false;
4417 : 18319 : if (TREE_CODE (e.index) == RANGE_EXPR)
4418 : 0 : cursor = TREE_OPERAND (e.index, 1);
4419 : 18319 : if (TREE_CODE (e.value) == RAW_DATA_CST)
4420 : 0 : cursor
4421 : 0 : = int_const_binop (PLUS_EXPR, cursor,
4422 : 0 : size_int (RAW_DATA_LENGTH (e.value)));
4423 : : else
4424 : 18319 : cursor = int_const_binop (PLUS_EXPR, cursor,
4425 : 18319 : size_one_node);
4426 : : }
4427 : 897 : if (find_array_ctor_elt (t, max) == -1)
4428 : : return false;
4429 : 811 : goto ok;
4430 : : }
4431 : 1252829 : else if (cxx_dialect >= cxx20
4432 : 1252829 : && TREE_CODE (TREE_TYPE (t)) == UNION_TYPE)
4433 : : {
4434 : 4205083 : if (CONSTRUCTOR_NELTS (t) == 0)
4435 : : /* An initialized union has a constructor element. */
4436 : : return false;
4437 : : /* And it only initializes one member. */
4438 : : field = NULL_TREE;
4439 : : }
4440 : : else
4441 : 1252814 : field = next_subobject_field (TYPE_FIELDS (TREE_TYPE (t)));
4442 : : }
4443 : : else
4444 : : field = NULL_TREE;
4445 : 269600072 : for (auto &e: CONSTRUCTOR_ELTS (t))
4446 : : {
4447 : : /* If VAL is null, we're in the middle of initializing this
4448 : : element. */
4449 : 170427451 : if (!reduced_constant_expression_p (e.value,
4450 : 170427451 : (e.index
4451 : 170427451 : && (TREE_CODE (e.index)
4452 : : == FIELD_DECL))
4453 : 54570413 : ? DECL_SIZE (e.index)
4454 : : : NULL_TREE))
4455 : : return false;
4456 : : /* We want to remove initializers for empty fields in a struct to
4457 : : avoid confusing output_constructor. */
4458 : 169921153 : if (is_empty_field (e.index)
4459 : 169921153 : && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE)
4460 : : return false;
4461 : : /* Check for non-empty fields between initialized fields when
4462 : : CONSTRUCTOR_NO_CLEARING. */
4463 : 169311701 : for (; field && e.index != field;
4464 : 94844 : field = next_subobject_field (DECL_CHAIN (field)))
4465 : 95523 : if (!is_really_empty_class (TREE_TYPE (field),
4466 : : /*ignore_vptr*/false))
4467 : : return false;
4468 : 169216178 : if (field)
4469 : 1429300 : field = next_subobject_field (DECL_CHAIN (field));
4470 : : }
4471 : : /* There could be a non-empty field at the end. */
4472 : 42158552 : for (; field; field = next_subobject_field (DECL_CHAIN (field)))
4473 : 77936 : if (!is_really_empty_class (TREE_TYPE (field), /*ignore_vptr*/false))
4474 : : {
4475 : : /* Ignore FIELD_DECLs with bit positions beyond DECL_SIZE of
4476 : : the parent FIELD_DECL (if any) for classes with virtual
4477 : : bases. */
4478 : 1127 : if (cxx_dialect >= cxx26
4479 : 860 : && sz
4480 : 1501 : && tree_int_cst_le (sz, bit_position (field)))
4481 : : break;
4482 : 875 : return false;
4483 : : }
4484 : 42080616 : ok:
4485 : 42081679 : if (CONSTRUCTOR_NO_CLEARING (t))
4486 : : /* All the fields are initialized. */
4487 : 1168207 : CONSTRUCTOR_NO_CLEARING (t) = false;
4488 : : return true;
4489 : :
4490 : 562250794 : default:
4491 : : /* FIXME are we calling this too much? */
4492 : 562250794 : return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE;
4493 : : }
4494 : : }
4495 : :
4496 : : /* *TP was not deemed constant by reduced_constant_expression_p. Explain
4497 : : why and suggest what could be done about it. */
4498 : :
4499 : : static tree
4500 : 916 : verify_constant_explain_r (tree *tp, int *walk_subtrees, void *)
4501 : : {
4502 : 916 : bool ref_p = false;
4503 : :
4504 : : /* No need to look into types or unevaluated operands. */
4505 : 916 : if (TYPE_P (*tp) || unevaluated_p (TREE_CODE (*tp)))
4506 : : {
4507 : 0 : *walk_subtrees = false;
4508 : 0 : return NULL_TREE;
4509 : : }
4510 : :
4511 : 916 : switch (TREE_CODE (*tp))
4512 : : {
4513 : 52 : CASE_CONVERT:
4514 : 52 : if (TREE_CODE (TREE_OPERAND (*tp, 0)) != ADDR_EXPR)
4515 : : break;
4516 : 33 : ref_p = TYPE_REF_P (TREE_TYPE (*tp));
4517 : 33 : *tp = TREE_OPERAND (*tp, 0);
4518 : 165 : gcc_fallthrough ();
4519 : 165 : case ADDR_EXPR:
4520 : 165 : {
4521 : 165 : tree op = TREE_OPERAND (*tp, 0);
4522 : 165 : if (VAR_P (op)
4523 : 96 : && DECL_DECLARED_CONSTEXPR_P (op)
4524 : 33 : && !TREE_STATIC (op)
4525 : : /* ??? We should also say something about temporaries. */
4526 : 195 : && !DECL_ARTIFICIAL (op))
4527 : : {
4528 : 27 : if (ref_p)
4529 : 6 : inform (location_of (*tp), "reference to %qD is not a constant "
4530 : : "expression", op);
4531 : : else
4532 : 21 : inform (location_of (*tp), "pointer to %qD is not a constant "
4533 : : "expression", op);
4534 : 27 : const location_t op_loc = DECL_SOURCE_LOCATION (op);
4535 : 27 : rich_location richloc (line_table, op_loc);
4536 : 27 : richloc.add_fixit_insert_before (op_loc, "static ");
4537 : 27 : inform (&richloc,
4538 : : "address of non-static constexpr variable %qD may differ on "
4539 : : "each invocation of the enclosing function; add %<static%> "
4540 : : "to give it a constant address", op);
4541 : 27 : }
4542 : : break;
4543 : : }
4544 : : default:
4545 : : break;
4546 : : }
4547 : :
4548 : : return NULL_TREE;
4549 : : }
4550 : :
4551 : : /* Some expressions may have constant operands but are not constant
4552 : : themselves, such as 1/0. Call this function to check for that
4553 : : condition.
4554 : :
4555 : : We only call this in places that require an arithmetic constant, not in
4556 : : places where we might have a non-constant expression that can be a
4557 : : component of a constant expression, such as the address of a constexpr
4558 : : variable that might be dereferenced later. */
4559 : :
4560 : : static bool
4561 : 377055753 : verify_constant (tree t, bool allow_non_constant, bool *non_constant_p,
4562 : : bool *overflow_p)
4563 : : {
4564 : 368977278 : if (!*non_constant_p && !reduced_constant_expression_p (t)
4565 : 379180905 : && t != void_node)
4566 : : {
4567 : 2125125 : if (!allow_non_constant)
4568 : : {
4569 : 238 : auto_diagnostic_group d;
4570 : 342 : error_at (cp_expr_loc_or_input_loc (t),
4571 : : "%q+E is not a constant expression", t);
4572 : 238 : cp_walk_tree_without_duplicates (&t, verify_constant_explain_r,
4573 : : nullptr);
4574 : 238 : }
4575 : 2125125 : *non_constant_p = true;
4576 : : }
4577 : 377055753 : if (TREE_OVERFLOW_P (t))
4578 : : {
4579 : 678 : if (!allow_non_constant)
4580 : : {
4581 : 155 : permerror (input_location, "overflow in constant expression");
4582 : : /* If we're being permissive (and are in an enforcing
4583 : : context), ignore the overflow. */
4584 : 155 : if (flag_permissive)
4585 : 75 : return *non_constant_p;
4586 : : }
4587 : 603 : *overflow_p = true;
4588 : : }
4589 : 377055678 : return *non_constant_p;
4590 : : }
4591 : :
4592 : : /* Check whether the shift operation with code CODE and type TYPE on LHS
4593 : : and RHS is undefined. If it is, give an error with an explanation,
4594 : : and return true; return false otherwise. */
4595 : :
4596 : : static bool
4597 : 31855981 : cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx,
4598 : : enum tree_code code, tree type, tree lhs, tree rhs)
4599 : : {
4600 : 31855981 : if ((code != LSHIFT_EXPR && code != RSHIFT_EXPR)
4601 : 3020846 : || TREE_CODE (lhs) != INTEGER_CST
4602 : 3020846 : || TREE_CODE (rhs) != INTEGER_CST)
4603 : : return false;
4604 : :
4605 : 3020846 : tree lhstype = TREE_TYPE (lhs);
4606 : 3020846 : unsigned HOST_WIDE_INT uprec = TYPE_PRECISION (TREE_TYPE (lhs));
4607 : :
4608 : : /* [expr.shift] The behavior is undefined if the right operand
4609 : : is negative, or greater than or equal to the length in bits
4610 : : of the promoted left operand. */
4611 : 3020846 : if (tree_int_cst_sgn (rhs) == -1)
4612 : : {
4613 : 126 : if (!ctx->quiet)
4614 : 35 : permerror (loc, "right operand of shift expression %q+E is negative",
4615 : : build2_loc (loc, code, type, lhs, rhs));
4616 : 126 : return (!flag_permissive || ctx->quiet);
4617 : : }
4618 : 3020720 : if (compare_tree_int (rhs, uprec) >= 0)
4619 : : {
4620 : 270 : if (!ctx->quiet)
4621 : 34 : permerror (loc, "right operand of shift expression %q+E is greater "
4622 : : "than or equal to the precision %wu of the left operand",
4623 : : build2_loc (loc, code, type, lhs, rhs), uprec);
4624 : 270 : return (!flag_permissive || ctx->quiet);
4625 : : }
4626 : :
4627 : : /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
4628 : : if E1 has a signed type and non-negative value, and E1x2^E2 is
4629 : : representable in the corresponding unsigned type of the result type,
4630 : : then that value, converted to the result type, is the resulting value;
4631 : : otherwise, the behavior is undefined.
4632 : : For C++20:
4633 : : The value of E1 << E2 is the unique value congruent to E1 x 2^E2 modulo
4634 : : 2^N, where N is the range exponent of the type of the result. */
4635 : 3020450 : if (code == LSHIFT_EXPR
4636 : 2901538 : && !TYPE_OVERFLOW_WRAPS (lhstype)
4637 : 1963416 : && cxx_dialect >= cxx11
4638 : 4967170 : && cxx_dialect < cxx20)
4639 : : {
4640 : 1447115 : if (tree_int_cst_sgn (lhs) == -1)
4641 : : {
4642 : 128 : if (!ctx->quiet)
4643 : 23 : permerror (loc,
4644 : : "left operand of shift expression %q+E is negative",
4645 : : build2_loc (loc, code, type, lhs, rhs));
4646 : 128 : return (!flag_permissive || ctx->quiet);
4647 : : }
4648 : : /* For signed x << y the following:
4649 : : (unsigned) x >> ((prec (lhs) - 1) - y)
4650 : : if > 1, is undefined. The right-hand side of this formula
4651 : : is the highest bit of the LHS that can be set (starting from 0),
4652 : : so that the shift doesn't overflow. We then right-shift the LHS
4653 : : to see whether any other bit is set making the original shift
4654 : : undefined -- the result is not representable in the corresponding
4655 : : unsigned type. */
4656 : 1446987 : tree t = build_int_cst (unsigned_type_node, uprec - 1);
4657 : 1446987 : t = fold_build2 (MINUS_EXPR, unsigned_type_node, t, rhs);
4658 : 1446987 : tree ulhs = fold_convert (unsigned_type_for (lhstype), lhs);
4659 : 1446987 : t = fold_build2 (RSHIFT_EXPR, TREE_TYPE (ulhs), ulhs, t);
4660 : 1446987 : if (tree_int_cst_lt (integer_one_node, t))
4661 : : {
4662 : 86 : if (!ctx->quiet)
4663 : 8 : permerror (loc, "shift expression %q+E overflows",
4664 : : build2_loc (loc, code, type, lhs, rhs));
4665 : 86 : return (!flag_permissive || ctx->quiet);
4666 : : }
4667 : : }
4668 : : return false;
4669 : : }
4670 : :
4671 : : /* Subroutine of cxx_eval_constant_expression.
4672 : : Attempt to reduce the unary expression tree T to a compile time value.
4673 : : If successful, return the value. Otherwise issue a diagnostic
4674 : : and return error_mark_node. */
4675 : :
4676 : : static tree
4677 : 20060451 : cxx_eval_unary_expression (const constexpr_ctx *ctx, tree t,
4678 : : bool /*lval*/,
4679 : : bool *non_constant_p, bool *overflow_p,
4680 : : tree *jump_target)
4681 : : {
4682 : 20060451 : tree r;
4683 : 20060451 : tree orig_arg = TREE_OPERAND (t, 0);
4684 : 20060451 : tree arg = cxx_eval_constant_expression (ctx, orig_arg, vc_prvalue,
4685 : : non_constant_p, overflow_p,
4686 : : jump_target);
4687 : 20060451 : if (*jump_target)
4688 : : return NULL_TREE;
4689 : 20060450 : VERIFY_CONSTANT (arg);
4690 : 16609164 : location_t loc = EXPR_LOCATION (t);
4691 : 16609164 : enum tree_code code = TREE_CODE (t);
4692 : 16609164 : tree type = TREE_TYPE (t);
4693 : 16609164 : r = fold_unary_loc (loc, code, type, arg);
4694 : 16609164 : if (r == NULL_TREE)
4695 : : {
4696 : 17 : if (arg == orig_arg)
4697 : : r = t;
4698 : : else
4699 : 13 : r = build1_loc (loc, code, type, arg);
4700 : : }
4701 : 16609164 : VERIFY_CONSTANT (r);
4702 : : return r;
4703 : : }
4704 : :
4705 : : /* Helper function for cxx_eval_binary_expression. Try to optimize
4706 : : original POINTER_PLUS_EXPR T, LHS p+ RHS, return NULL_TREE if the
4707 : : generic folding should be used. */
4708 : :
4709 : : static tree
4710 : 2872786 : cxx_fold_pointer_plus_expression (const constexpr_ctx *ctx, tree t,
4711 : : tree lhs, tree rhs, bool *non_constant_p,
4712 : : bool *overflow_p, tree *jump_target)
4713 : : {
4714 : 2872786 : STRIP_NOPS (lhs);
4715 : 2872786 : if (TREE_CODE (lhs) != ADDR_EXPR)
4716 : : return NULL_TREE;
4717 : :
4718 : 2564497 : lhs = TREE_OPERAND (lhs, 0);
4719 : :
4720 : : /* &A[i] p+ j => &A[i + j] */
4721 : 2564497 : if (TREE_CODE (lhs) == ARRAY_REF
4722 : 6702 : && TREE_CODE (TREE_OPERAND (lhs, 1)) == INTEGER_CST
4723 : 6702 : && TREE_CODE (rhs) == INTEGER_CST
4724 : 6702 : && TYPE_SIZE_UNIT (TREE_TYPE (lhs))
4725 : 2571199 : && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs))) == INTEGER_CST)
4726 : : {
4727 : 6702 : tree orig_type = TREE_TYPE (t);
4728 : 6702 : location_t loc = EXPR_LOCATION (t);
4729 : 6702 : tree type = TREE_TYPE (lhs);
4730 : :
4731 : 6702 : t = fold_convert_loc (loc, ssizetype, TREE_OPERAND (lhs, 1));
4732 : 6702 : tree nelts = array_type_nelts_top (TREE_TYPE (TREE_OPERAND (lhs, 0)));
4733 : 6702 : nelts = cxx_eval_constant_expression (ctx, nelts, vc_prvalue,
4734 : : non_constant_p, overflow_p,
4735 : : jump_target);
4736 : 6702 : if (*non_constant_p)
4737 : : return NULL_TREE;
4738 : 6690 : if (*jump_target)
4739 : : return NULL_TREE;
4740 : : /* Don't fold an out-of-bound access. */
4741 : 6690 : if (!tree_int_cst_le (t, nelts))
4742 : : return NULL_TREE;
4743 : 6690 : rhs = cp_fold_convert (ssizetype, rhs);
4744 : : /* Don't fold if rhs can't be divided exactly by TYPE_SIZE_UNIT.
4745 : : constexpr int A[1]; ... (char *)&A[0] + 1 */
4746 : 6690 : if (!integer_zerop (fold_build2_loc (loc, TRUNC_MOD_EXPR, sizetype,
4747 : 6690 : rhs, TYPE_SIZE_UNIT (type))))
4748 : : return NULL_TREE;
4749 : : /* Make sure to treat the second operand of POINTER_PLUS_EXPR
4750 : : as signed. */
4751 : 6658 : rhs = fold_build2_loc (loc, EXACT_DIV_EXPR, ssizetype, rhs,
4752 : 6658 : TYPE_SIZE_UNIT (type));
4753 : 6658 : t = size_binop_loc (loc, PLUS_EXPR, rhs, t);
4754 : 6658 : t = build4_loc (loc, ARRAY_REF, type, TREE_OPERAND (lhs, 0),
4755 : : t, NULL_TREE, NULL_TREE);
4756 : 6658 : t = cp_build_addr_expr (t, tf_warning_or_error);
4757 : 6658 : t = cp_fold_convert (orig_type, t);
4758 : 6658 : return cxx_eval_constant_expression (ctx, t, vc_prvalue,
4759 : : non_constant_p, overflow_p,
4760 : 6658 : jump_target);
4761 : : }
4762 : :
4763 : : return NULL_TREE;
4764 : : }
4765 : :
4766 : : /* Try to fold expressions like
4767 : : (struct S *) (&a[0].D.2378 + 12)
4768 : : into
4769 : : &MEM <struct T> [(void *)&a + 12B]
4770 : : This is something normally done by gimple_fold_stmt_to_constant_1
4771 : : on GIMPLE, but is undesirable on GENERIC if we are e.g. going to
4772 : : dereference the address because some details are lost.
4773 : : For pointer comparisons we want such folding though so that
4774 : : match.pd address_compare optimization works. */
4775 : :
4776 : : static tree
4777 : 2096376 : cxx_maybe_fold_addr_pointer_plus (tree t)
4778 : : {
4779 : 4192752 : while (CONVERT_EXPR_P (t)
4780 : 2274580 : && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0))))
4781 : 178204 : t = TREE_OPERAND (t, 0);
4782 : 2096376 : if (TREE_CODE (t) != POINTER_PLUS_EXPR)
4783 : : return NULL_TREE;
4784 : 1797023 : tree op0 = TREE_OPERAND (t, 0);
4785 : 1797023 : tree op1 = TREE_OPERAND (t, 1);
4786 : 1797023 : if (TREE_CODE (op1) != INTEGER_CST)
4787 : : return NULL_TREE;
4788 : 1797023 : while (CONVERT_EXPR_P (op0)
4789 : 3591899 : && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
4790 : 1794876 : op0 = TREE_OPERAND (op0, 0);
4791 : 1797023 : if (TREE_CODE (op0) != ADDR_EXPR)
4792 : : return NULL_TREE;
4793 : 1797023 : op1 = fold_convert (ptr_type_node, op1);
4794 : 1797023 : tree r = fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (op0)), op0, op1);
4795 : 1797023 : return build1_loc (EXPR_LOCATION (t), ADDR_EXPR, TREE_TYPE (op0), r);
4796 : : }
4797 : :
4798 : : /* Subroutine of cxx_eval_constant_expression.
4799 : : Like cxx_eval_unary_expression, except for binary expressions. */
4800 : :
4801 : : static tree
4802 : 47772514 : cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t,
4803 : : value_cat lval,
4804 : : bool *non_constant_p, bool *overflow_p,
4805 : : tree *jump_target)
4806 : : {
4807 : 47772514 : tree r = NULL_TREE;
4808 : 47772514 : tree orig_lhs = TREE_OPERAND (t, 0);
4809 : 47772514 : tree orig_rhs = TREE_OPERAND (t, 1);
4810 : 47772514 : tree lhs, rhs;
4811 : 47772514 : lhs = cxx_eval_constant_expression (ctx, orig_lhs, vc_prvalue,
4812 : : non_constant_p, overflow_p,
4813 : : jump_target);
4814 : : /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer
4815 : : subtraction. */
4816 : 47772514 : if (*non_constant_p)
4817 : : return t;
4818 : 35130159 : if (*jump_target)
4819 : : return NULL_TREE;
4820 : 35130113 : rhs = cxx_eval_constant_expression (ctx, orig_rhs, vc_prvalue,
4821 : : non_constant_p, overflow_p,
4822 : : jump_target);
4823 : 35130113 : if (*non_constant_p)
4824 : : return t;
4825 : 34214211 : if (*jump_target)
4826 : : return NULL_TREE;
4827 : :
4828 : 34214206 : location_t loc = EXPR_LOCATION (t);
4829 : 34214206 : enum tree_code code = TREE_CODE (t);
4830 : 34214206 : tree type = TREE_TYPE (t);
4831 : :
4832 : 34214206 : if (code == EQ_EXPR || code == NE_EXPR)
4833 : : {
4834 : 4238472 : bool is_code_eq = (code == EQ_EXPR);
4835 : :
4836 : 4238472 : if (TREE_CODE (lhs) == PTRMEM_CST
4837 : 181 : && TREE_CODE (rhs) == PTRMEM_CST)
4838 : : {
4839 : 54 : tree lmem = PTRMEM_CST_MEMBER (lhs);
4840 : 54 : tree rmem = PTRMEM_CST_MEMBER (rhs);
4841 : 54 : bool eq;
4842 : 54 : if (TREE_CODE (lmem) == TREE_CODE (rmem)
4843 : 54 : && TREE_CODE (lmem) == FIELD_DECL
4844 : 54 : && TREE_CODE (DECL_CONTEXT (lmem)) == UNION_TYPE
4845 : 81 : && same_type_p (DECL_CONTEXT (lmem),
4846 : : DECL_CONTEXT (rmem)))
4847 : : /* If both refer to (possibly different) members of the same union
4848 : : (12.3), they compare equal. */
4849 : : eq = true;
4850 : : else
4851 : 27 : eq = cp_tree_equal (lhs, rhs);
4852 : 54 : r = constant_boolean_node (eq == is_code_eq, type);
4853 : 54 : }
4854 : 4238418 : else if ((TREE_CODE (lhs) == PTRMEM_CST
4855 : 4238291 : || TREE_CODE (rhs) == PTRMEM_CST)
4856 : 4238418 : && (null_member_pointer_value_p (lhs)
4857 : 127 : || null_member_pointer_value_p (rhs)))
4858 : 127 : r = constant_boolean_node (!is_code_eq, type);
4859 : 4238291 : else if (TREE_CODE (lhs) == PTRMEM_CST)
4860 : 0 : lhs = cplus_expand_constant (lhs);
4861 : 4238291 : else if (TREE_CODE (rhs) == PTRMEM_CST)
4862 : 0 : rhs = cplus_expand_constant (rhs);
4863 : : }
4864 : 0 : if (r == NULL_TREE
4865 : 34214025 : && TREE_CODE_CLASS (code) == tcc_comparison
4866 : 11656445 : && POINTER_TYPE_P (TREE_TYPE (lhs)))
4867 : : {
4868 : 1048188 : if (tree lhso = cxx_maybe_fold_addr_pointer_plus (lhs))
4869 : 865400 : lhs = fold_convert (TREE_TYPE (lhs), lhso);
4870 : 1048188 : if (tree rhso = cxx_maybe_fold_addr_pointer_plus (rhs))
4871 : 931623 : rhs = fold_convert (TREE_TYPE (rhs), rhso);
4872 : : }
4873 : 2872941 : if (code == POINTER_PLUS_EXPR && !*non_constant_p
4874 : 37087147 : && integer_zerop (lhs) && !integer_zerop (rhs))
4875 : : {
4876 : 155 : if (!ctx->quiet)
4877 : 45 : error ("arithmetic involving a null pointer in %qE", lhs);
4878 : 155 : *non_constant_p = true;
4879 : 155 : return t;
4880 : : }
4881 : 34214051 : else if (code == POINTER_PLUS_EXPR)
4882 : : {
4883 : 2872786 : r = cxx_fold_pointer_plus_expression (ctx, t, lhs, rhs, non_constant_p,
4884 : : overflow_p, jump_target);
4885 : 2872786 : if (*jump_target)
4886 : : return NULL_TREE;
4887 : : }
4888 : 31341265 : else if (code == SPACESHIP_EXPR)
4889 : : {
4890 : 23143 : r = genericize_spaceship (loc, type, lhs, rhs);
4891 : 23143 : return cxx_eval_constant_expression (ctx, r, lval, non_constant_p,
4892 : 23143 : overflow_p, jump_target);
4893 : : }
4894 : :
4895 : 34190908 : if (r == NULL_TREE)
4896 : : {
4897 : 34184069 : if (ctx->manifestly_const_eval == mce_true
4898 : 19940724 : && (flag_constexpr_fp_except
4899 : 19940721 : || TREE_CODE (type) != REAL_TYPE))
4900 : : {
4901 : 19928015 : auto ofcc = make_temp_override (folding_cxx_constexpr, true);
4902 : 19928015 : r = fold_binary_initializer_loc (loc, code, type, lhs, rhs);
4903 : 19928015 : }
4904 : : else
4905 : 14256054 : r = fold_binary_loc (loc, code, type, lhs, rhs);
4906 : : }
4907 : :
4908 : 34184069 : if (r == NULL_TREE
4909 : 2335025 : && (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
4910 : 100 : && TREE_CODE (lhs) == INTEGER_CST
4911 : 98 : && TREE_CODE (rhs) == INTEGER_CST
4912 : 36525933 : && wi::neg_p (wi::to_wide (rhs)))
4913 : : {
4914 : : /* For diagnostics and -fpermissive emulate previous behavior of
4915 : : handling shifts by negative amount. */
4916 : 98 : tree nrhs = const_unop (NEGATE_EXPR, TREE_TYPE (rhs), rhs);
4917 : 98 : if (nrhs)
4918 : 119 : r = fold_binary_loc (loc,
4919 : : code == LSHIFT_EXPR ? RSHIFT_EXPR : LSHIFT_EXPR,
4920 : : type, lhs, nrhs);
4921 : : }
4922 : :
4923 : 34190908 : if (r == NULL_TREE)
4924 : : {
4925 : 2334927 : if (lhs == orig_lhs && rhs == orig_rhs)
4926 : : r = t;
4927 : : else
4928 : 658441 : r = build2_loc (loc, code, type, lhs, rhs);
4929 : : }
4930 : 31855981 : else if (cxx_eval_check_shift_p (loc, ctx, code, type, lhs, rhs))
4931 : 600 : *non_constant_p = true;
4932 : : /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
4933 : : a local array in a constexpr function. */
4934 : 34190908 : bool ptr = INDIRECT_TYPE_P (TREE_TYPE (lhs));
4935 : 30178739 : if (!ptr)
4936 : 30178739 : VERIFY_CONSTANT (r);
4937 : : return r;
4938 : : }
4939 : :
4940 : : /* Subroutine of cxx_eval_constant_expression.
4941 : : Attempt to evaluate condition expressions. */
4942 : :
4943 : : static tree
4944 : 7904510 : cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t,
4945 : : value_cat lval,
4946 : : bool *non_constant_p, bool *overflow_p,
4947 : : tree *jump_target)
4948 : : {
4949 : 7904510 : tree val = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
4950 : : vc_prvalue,
4951 : : non_constant_p, overflow_p,
4952 : : jump_target);
4953 : 7904510 : if (*jump_target)
4954 : : return NULL_TREE;
4955 : 7904501 : VERIFY_CONSTANT (val);
4956 : 6264473 : if (TREE_CODE (t) == IF_STMT && IF_STMT_CONSTEVAL_P (t))
4957 : : {
4958 : : /* Evaluate the condition as if it was
4959 : : if (__builtin_is_constant_evaluated ()), i.e. defer it if not
4960 : : ctx->manifestly_const_eval (as sometimes we try to constant evaluate
4961 : : without manifestly_const_eval even expressions or parts thereof which
4962 : : will later be manifestly const_eval evaluated), otherwise fold it to
4963 : : true. */
4964 : 1021840 : if (ctx->manifestly_const_eval == mce_unknown)
4965 : : {
4966 : 1013309 : *non_constant_p = true;
4967 : 1013309 : return t;
4968 : : }
4969 : 8531 : val = constant_boolean_node (ctx->manifestly_const_eval == mce_true,
4970 : : boolean_type_node);
4971 : : }
4972 : : /* Don't VERIFY_CONSTANT the other operands. */
4973 : 5251164 : const bool zero_p = integer_zerop (val);
4974 : 5251164 : if (zero_p)
4975 : 2852229 : val = TREE_OPERAND (t, 2);
4976 : : else
4977 : 2398935 : val = TREE_OPERAND (t, 1);
4978 : 5251164 : if (TREE_CODE (t) == IF_STMT && !val)
4979 : 1308129 : val = void_node;
4980 : :
4981 : : /* P2564: If we aren't in immediate function context (including a manifestly
4982 : : constant-evaluated expression), check any uses of immediate functions in
4983 : : the arm we're discarding. But don't do this inside a call; we already
4984 : : checked when parsing the function. */
4985 : 5251164 : if (ctx->manifestly_const_eval != mce_true
4986 : 1627514 : && !in_immediate_context ()
4987 : 1574352 : && !ctx->call
4988 : 5877303 : && cp_fold_immediate (&TREE_OPERAND (t, zero_p ? 1 : 2),
4989 : 626139 : ctx->manifestly_const_eval))
4990 : : {
4991 : 7 : *non_constant_p = true;
4992 : 7 : return t;
4993 : : }
4994 : :
4995 : : /* A TARGET_EXPR may be nested inside another TARGET_EXPR, but still
4996 : : serve as the initializer for the same object as the outer TARGET_EXPR,
4997 : : as in
4998 : : A a = true ? A{} : A{};
4999 : : so strip the inner TARGET_EXPR so we don't materialize a temporary. */
5000 : 5251157 : if (TREE_CODE (val) == TARGET_EXPR)
5001 : 677 : val = TARGET_EXPR_INITIAL (val);
5002 : 5251157 : return cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
5003 : 5251157 : overflow_p, jump_target);
5004 : : }
5005 : :
5006 : : /* Subroutine of cxx_eval_constant_expression.
5007 : : Attempt to evaluate vector condition expressions. Unlike
5008 : : cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal
5009 : : ternary arithmetics operation, where all 3 arguments have to be
5010 : : evaluated as constants and then folding computes the result from
5011 : : them. */
5012 : :
5013 : : static tree
5014 : 866 : cxx_eval_vector_conditional_expression (const constexpr_ctx *ctx, tree t,
5015 : : bool *non_constant_p, bool *overflow_p,
5016 : : tree *jump_target)
5017 : : {
5018 : 866 : tree arg1 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
5019 : : vc_prvalue,
5020 : : non_constant_p, overflow_p,
5021 : : jump_target);
5022 : 866 : if (*jump_target)
5023 : : return NULL_TREE;
5024 : 866 : VERIFY_CONSTANT (arg1);
5025 : 657 : tree arg2 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
5026 : : vc_prvalue,
5027 : : non_constant_p, overflow_p,
5028 : : jump_target);
5029 : 657 : if (*jump_target)
5030 : : return NULL_TREE;
5031 : 657 : VERIFY_CONSTANT (arg2);
5032 : 657 : tree arg3 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2),
5033 : : vc_prvalue,
5034 : : non_constant_p, overflow_p,
5035 : : jump_target);
5036 : 657 : if (*jump_target)
5037 : : return NULL_TREE;
5038 : 657 : VERIFY_CONSTANT (arg3);
5039 : 657 : location_t loc = EXPR_LOCATION (t);
5040 : 657 : tree type = TREE_TYPE (t);
5041 : 657 : tree r = fold_ternary_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
5042 : 657 : if (r == NULL_TREE)
5043 : : {
5044 : 0 : if (arg1 == TREE_OPERAND (t, 0)
5045 : 0 : && arg2 == TREE_OPERAND (t, 1)
5046 : 0 : && arg3 == TREE_OPERAND (t, 2))
5047 : : r = t;
5048 : : else
5049 : 0 : r = build3_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
5050 : : }
5051 : 657 : VERIFY_CONSTANT (r);
5052 : : return r;
5053 : : }
5054 : :
5055 : : /* Returns less than, equal to, or greater than zero if KEY is found to be
5056 : : less than, to match, or to be greater than the constructor_elt's INDEX. */
5057 : :
5058 : : static int
5059 : 78220 : array_index_cmp (tree key, tree index)
5060 : : {
5061 : 78220 : gcc_assert (TREE_CODE (key) == INTEGER_CST);
5062 : :
5063 : 78220 : switch (TREE_CODE (index))
5064 : : {
5065 : 75213 : case INTEGER_CST:
5066 : 75213 : return tree_int_cst_compare (key, index);
5067 : 3007 : case RANGE_EXPR:
5068 : 3007 : {
5069 : 3007 : tree lo = TREE_OPERAND (index, 0);
5070 : 3007 : tree hi = TREE_OPERAND (index, 1);
5071 : 3007 : if (tree_int_cst_lt (key, lo))
5072 : : return -1;
5073 : 2727 : else if (tree_int_cst_lt (hi, key))
5074 : : return 1;
5075 : : else
5076 : 2727 : return 0;
5077 : : }
5078 : 0 : default:
5079 : 0 : gcc_unreachable ();
5080 : : }
5081 : : }
5082 : :
5083 : : /* Extract a single INTEGER_CST from RAW_DATA_CST RAW_DATA at
5084 : : relative index OFF. */
5085 : :
5086 : : static tree
5087 : 29084 : raw_data_cst_elt (tree raw_data, unsigned int off)
5088 : : {
5089 : 29084 : return build_int_cst (TREE_TYPE (raw_data),
5090 : 29084 : TYPE_UNSIGNED (TREE_TYPE (raw_data))
5091 : 58168 : ? (HOST_WIDE_INT)
5092 : 29084 : RAW_DATA_UCHAR_ELT (raw_data, off)
5093 : : : (HOST_WIDE_INT)
5094 : 0 : RAW_DATA_SCHAR_ELT (raw_data, off));
5095 : : }
5096 : :
5097 : : /* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1
5098 : : if none. If INSERT is true, insert a matching element rather than fail. */
5099 : :
5100 : : static HOST_WIDE_INT
5101 : 1603537 : find_array_ctor_elt (tree ary, tree dindex, bool insert)
5102 : : {
5103 : 1603537 : if (tree_int_cst_sgn (dindex) < 0)
5104 : : return -1;
5105 : :
5106 : 1603537 : unsigned HOST_WIDE_INT i = tree_to_uhwi (dindex);
5107 : 1603537 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ary);
5108 : 1603537 : unsigned HOST_WIDE_INT len = vec_safe_length (elts);
5109 : :
5110 : 2439691 : unsigned HOST_WIDE_INT end = len;
5111 : 2439691 : unsigned HOST_WIDE_INT begin = 0;
5112 : :
5113 : : /* If the last element of the CONSTRUCTOR has its own index, we can assume
5114 : : that the same is true of the other elements and index directly. */
5115 : 1509950 : if (end > 0)
5116 : : {
5117 : 1487902 : tree cindex = (*elts)[end - 1].index;
5118 : 1487902 : if (cindex == NULL_TREE)
5119 : : {
5120 : : /* Verify that if the last index is missing, all indexes
5121 : : are missing and there is no RAW_DATA_CST. */
5122 : 18139 : if (flag_checking)
5123 : 198426 : for (unsigned int j = 0; j < len - 1; ++j)
5124 : 180287 : gcc_assert ((*elts)[j].index == NULL_TREE
5125 : : && TREE_CODE ((*elts)[j].value) != RAW_DATA_CST);
5126 : 18139 : if (i < end)
5127 : 18139 : return i;
5128 : : else
5129 : : {
5130 : 0 : begin = end;
5131 : 0 : if (i == end)
5132 : : /* If the element is to be added right at the end,
5133 : : make sure it is added with cleared index too. */
5134 : 929741 : dindex = NULL_TREE;
5135 : 0 : else if (insert)
5136 : : /* Otherwise, in order not to break the assumption
5137 : : that CONSTRUCTOR either has all indexes or none,
5138 : : we need to add indexes to all elements. */
5139 : 0 : for (unsigned int j = 0; j < len; ++j)
5140 : 0 : (*elts)[j].index = build_int_cst (TREE_TYPE (dindex), j);
5141 : : }
5142 : : }
5143 : 1469763 : else if (TREE_CODE (cindex) == INTEGER_CST
5144 : 1469763 : && compare_tree_int (cindex, end - 1) == 0)
5145 : : {
5146 : 1449816 : tree value = (*elts)[end - 1].value;
5147 : 1449816 : if (i < end)
5148 : : {
5149 : 655747 : if (i == end - 1 && TREE_CODE (value) == RAW_DATA_CST)
5150 : : begin = end - 1;
5151 : : else
5152 : 655657 : return i;
5153 : : }
5154 : 794069 : else if (TREE_CODE (value) == RAW_DATA_CST
5155 : 826247 : && wi::to_offset (dindex) < (wi::to_offset (cindex)
5156 : 32178 : + RAW_DATA_LENGTH (value)))
5157 : 16089 : begin = end - 1;
5158 : : else
5159 : 777980 : begin = end;
5160 : : }
5161 : : }
5162 : :
5163 : : /* Otherwise, find a matching index by means of a binary search. */
5164 : 955644 : while (begin != end)
5165 : : {
5166 : 59838 : unsigned HOST_WIDE_INT middle = (begin + end) / 2;
5167 : 59838 : constructor_elt &elt = (*elts)[middle];
5168 : 59838 : tree idx = elt.index;
5169 : :
5170 : 59838 : int cmp = array_index_cmp (dindex, idx);
5171 : 59838 : if (cmp > 0
5172 : 47506 : && TREE_CODE (elt.value) == RAW_DATA_CST
5173 : 142712 : && wi::to_offset (dindex) < (wi::to_offset (idx)
5174 : 82874 : + RAW_DATA_LENGTH (elt.value)))
5175 : 28950 : cmp = 0;
5176 : 59838 : if (cmp < 0)
5177 : : end = middle;
5178 : 52491 : else if (cmp > 0)
5179 : 18556 : begin = middle + 1;
5180 : : else
5181 : : {
5182 : 33935 : if (insert && TREE_CODE (elt.value) == RAW_DATA_CST)
5183 : : {
5184 : : /* We need to split the RAW_DATA_CST elt. */
5185 : 122 : constructor_elt e;
5186 : 122 : gcc_checking_assert (TREE_CODE (idx) != RANGE_EXPR);
5187 : 244 : unsigned int off = (wi::to_offset (dindex)
5188 : 244 : - wi::to_offset (idx)).to_uhwi ();
5189 : 122 : tree value = elt.value;
5190 : 122 : unsigned int len = RAW_DATA_LENGTH (value);
5191 : 122 : if (off > 1 && len >= off + 3)
5192 : 22 : value = copy_node (elt.value);
5193 : 122 : if (off)
5194 : : {
5195 : 33 : if (off > 1)
5196 : 33 : RAW_DATA_LENGTH (elt.value) = off;
5197 : : else
5198 : 0 : elt.value = raw_data_cst_elt (elt.value, 0);
5199 : 33 : e.index = size_binop (PLUS_EXPR, elt.index,
5200 : : build_int_cst (TREE_TYPE (elt.index),
5201 : : off));
5202 : 33 : e.value = NULL_TREE;
5203 : 33 : ++middle;
5204 : 33 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e);
5205 : : }
5206 : 122 : (*elts)[middle].value = raw_data_cst_elt (value, off);
5207 : 122 : if (len >= off + 2)
5208 : : {
5209 : 111 : e.index = (*elts)[middle].index;
5210 : 111 : e.index = size_binop (PLUS_EXPR, e.index,
5211 : : build_one_cst (TREE_TYPE (e.index)));
5212 : 111 : if (len >= off + 3)
5213 : : {
5214 : 111 : RAW_DATA_LENGTH (value) -= off + 1;
5215 : 111 : RAW_DATA_POINTER (value) += off + 1;
5216 : 111 : e.value = value;
5217 : : }
5218 : : else
5219 : 0 : e.value = raw_data_cst_elt (value, off + 1);
5220 : 111 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e);
5221 : : }
5222 : 122 : return middle;
5223 : : }
5224 : 848 : if (insert && TREE_CODE (idx) == RANGE_EXPR)
5225 : : {
5226 : : /* We need to split the range. */
5227 : 347 : constructor_elt e;
5228 : 347 : tree lo = TREE_OPERAND (idx, 0);
5229 : 347 : tree hi = TREE_OPERAND (idx, 1);
5230 : 347 : tree value = elt.value;
5231 : 347 : dindex = fold_convert (sizetype, dindex);
5232 : 347 : if (tree_int_cst_lt (lo, dindex))
5233 : : {
5234 : : /* There are still some lower elts; shorten the range. */
5235 : 178 : tree new_hi = int_const_binop (MINUS_EXPR, dindex,
5236 : 89 : size_one_node);
5237 : 89 : if (tree_int_cst_equal (lo, new_hi))
5238 : : /* Only one element left, no longer a range. */
5239 : 37 : elt.index = lo;
5240 : : else
5241 : 52 : TREE_OPERAND (idx, 1) = new_hi;
5242 : : /* Append the element we want to insert. */
5243 : 89 : ++middle;
5244 : 89 : e.index = dindex;
5245 : 89 : e.value = unshare_constructor (value);
5246 : 89 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e);
5247 : : }
5248 : : else
5249 : : /* No lower elts, the range elt is now ours. */
5250 : 258 : elt.index = dindex;
5251 : :
5252 : 347 : if (tree_int_cst_lt (dindex, hi))
5253 : : {
5254 : : /* There are still some higher elts; append a range. */
5255 : 478 : tree new_lo = int_const_binop (PLUS_EXPR, dindex,
5256 : 239 : size_one_node);
5257 : 239 : if (tree_int_cst_equal (new_lo, hi))
5258 : 98 : e.index = hi;
5259 : : else
5260 : 141 : e.index = build2 (RANGE_EXPR, sizetype, new_lo, hi);
5261 : 239 : e.value = unshare_constructor (value);
5262 : 239 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e);
5263 : : }
5264 : : }
5265 : 33813 : return middle;
5266 : : }
5267 : : }
5268 : :
5269 : 895806 : if (insert)
5270 : : {
5271 : 893451 : constructor_elt e = { dindex, NULL_TREE };
5272 : 893451 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), end, e);
5273 : 893451 : return end;
5274 : : }
5275 : :
5276 : : return -1;
5277 : : }
5278 : :
5279 : : /* Return a pointer to the constructor_elt of CTOR which matches INDEX. If no
5280 : : matching constructor_elt exists, then add one to CTOR.
5281 : :
5282 : : As an optimization, if POS_HINT is non-negative then it is used as a guess
5283 : : for the (integer) index of the matching constructor_elt within CTOR. */
5284 : :
5285 : : static constructor_elt *
5286 : 9720030 : get_or_insert_ctor_field (tree ctor, tree index, int pos_hint = -1)
5287 : : {
5288 : : /* Check the hint first. */
5289 : 843660 : if (pos_hint >= 0 && (unsigned)pos_hint < CONSTRUCTOR_NELTS (ctor)
5290 : 10563690 : && CONSTRUCTOR_ELT (ctor, pos_hint)->index == index)
5291 : : return CONSTRUCTOR_ELT (ctor, pos_hint);
5292 : :
5293 : 8876375 : tree type = TREE_TYPE (ctor);
5294 : 8876375 : if (TREE_CODE (type) == VECTOR_TYPE && index == NULL_TREE)
5295 : : {
5296 : 128 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (ctor), index, NULL_TREE);
5297 : 128 : return &CONSTRUCTOR_ELTS (ctor)->last();
5298 : : }
5299 : 8876247 : else if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
5300 : : {
5301 : 1222945 : if (TREE_CODE (index) == RANGE_EXPR)
5302 : : {
5303 : : /* Support for RANGE_EXPR index lookups is currently limited to
5304 : : accessing an existing element via POS_HINT, or appending a new
5305 : : element to the end of CTOR. ??? Support for other access
5306 : : patterns may also be needed. */
5307 : 3 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
5308 : 3 : if (vec_safe_length (elts))
5309 : : {
5310 : 3 : tree lo = TREE_OPERAND (index, 0);
5311 : 3 : gcc_assert (array_index_cmp (elts->last().index, lo) < 0);
5312 : : }
5313 : 3 : CONSTRUCTOR_APPEND_ELT (elts, index, NULL_TREE);
5314 : 3 : return &elts->last();
5315 : : }
5316 : :
5317 : 1222942 : HOST_WIDE_INT i = find_array_ctor_elt (ctor, index, /*insert*/true);
5318 : 1222942 : gcc_assert (i >= 0);
5319 : 1222942 : constructor_elt *cep = CONSTRUCTOR_ELT (ctor, i);
5320 : 1222942 : gcc_assert (cep->index == NULL_TREE
5321 : : || TREE_CODE (cep->index) != RANGE_EXPR);
5322 : : return cep;
5323 : : }
5324 : : else
5325 : : {
5326 : 7653302 : gcc_assert (TREE_CODE (index) == FIELD_DECL
5327 : : && (same_type_ignoring_top_level_qualifiers_p
5328 : : (DECL_CONTEXT (index), TREE_TYPE (ctor))));
5329 : :
5330 : : /* We must keep the CONSTRUCTOR's ELTS in FIELD order.
5331 : : Usually we meet initializers in that order, but it is
5332 : : possible for base types to be placed not in program
5333 : : order. */
5334 : 7653302 : tree fields = TYPE_FIELDS (DECL_CONTEXT (index));
5335 : 7653302 : unsigned HOST_WIDE_INT idx = 0;
5336 : 7653302 : constructor_elt *cep = NULL;
5337 : :
5338 : : /* Check if we're changing the active member of a union. */
5339 : 150071 : if (TREE_CODE (type) == UNION_TYPE && CONSTRUCTOR_NELTS (ctor)
5340 : 7695832 : && CONSTRUCTOR_ELT (ctor, 0)->index != index)
5341 : 3363 : vec_safe_truncate (CONSTRUCTOR_ELTS (ctor), 0);
5342 : : /* If the bit offset of INDEX is larger than that of the last
5343 : : constructor_elt, then we can just immediately append a new
5344 : : constructor_elt to the end of CTOR. */
5345 : 7649939 : else if (CONSTRUCTOR_NELTS (ctor)
5346 : 8926388 : && tree_int_cst_compare (bit_position (index),
5347 : 8674992 : bit_position (CONSTRUCTOR_ELTS (ctor)
5348 : 4337496 : ->last().index)) > 0)
5349 : : {
5350 : 1878543 : idx = CONSTRUCTOR_NELTS (ctor);
5351 : 1878543 : goto insert;
5352 : : }
5353 : :
5354 : : /* Otherwise, we need to iterate over CTOR to find or insert INDEX
5355 : : appropriately. */
5356 : :
5357 : 6682177 : for (; vec_safe_iterate (CONSTRUCTOR_ELTS (ctor), idx, &cep);
5358 : 907418 : idx++, fields = DECL_CHAIN (fields))
5359 : : {
5360 : 3366371 : if (index == cep->index)
5361 : 2445058 : goto found;
5362 : :
5363 : : /* The field we're initializing must be on the field
5364 : : list. Look to see if it is present before the
5365 : : field the current ELT initializes. */
5366 : 13071765 : for (; fields != cep->index; fields = DECL_CHAIN (fields))
5367 : 12164347 : if (index == fields)
5368 : 13895 : goto insert;
5369 : : }
5370 : : /* We fell off the end of the CONSTRUCTOR, so insert a new
5371 : : entry at the end. */
5372 : :
5373 : 5208244 : insert:
5374 : 5208244 : {
5375 : 5208244 : constructor_elt ce = { index, NULL_TREE };
5376 : :
5377 : 5208244 : vec_safe_insert (CONSTRUCTOR_ELTS (ctor), idx, ce);
5378 : 5208244 : cep = CONSTRUCTOR_ELT (ctor, idx);
5379 : : }
5380 : 7653302 : found:;
5381 : :
5382 : 7653302 : return cep;
5383 : : }
5384 : : }
5385 : :
5386 : : /* Under the control of CTX, issue a detailed diagnostic for
5387 : : an out-of-bounds subscript INDEX into the expression ARRAY. */
5388 : :
5389 : : static void
5390 : 548 : diag_array_subscript (location_t loc, const constexpr_ctx *ctx, tree array, tree index)
5391 : : {
5392 : 548 : if (!ctx->quiet)
5393 : : {
5394 : 118 : tree arraytype = TREE_TYPE (array);
5395 : :
5396 : : /* Convert the unsigned array subscript to a signed integer to avoid
5397 : : printing huge numbers for small negative values. */
5398 : 118 : tree sidx = fold_convert (ssizetype, index);
5399 : 118 : STRIP_ANY_LOCATION_WRAPPER (array);
5400 : 118 : if (DECL_P (array))
5401 : : {
5402 : 75 : auto_diagnostic_group d;
5403 : 75 : if (TYPE_DOMAIN (arraytype))
5404 : 69 : error_at (loc, "array subscript value %qE is outside the bounds "
5405 : : "of array %qD of type %qT", sidx, array, arraytype);
5406 : : else
5407 : 6 : error_at (loc, "nonzero array subscript %qE is used with array %qD of "
5408 : : "type %qT with unknown bounds", sidx, array, arraytype);
5409 : 75 : inform (DECL_SOURCE_LOCATION (array), "declared here");
5410 : 75 : }
5411 : 43 : else if (TYPE_DOMAIN (arraytype))
5412 : 40 : error_at (loc, "array subscript value %qE is outside the bounds "
5413 : : "of array type %qT", sidx, arraytype);
5414 : : else
5415 : 3 : error_at (loc, "nonzero array subscript %qE is used with array of type %qT "
5416 : : "with unknown bounds", sidx, arraytype);
5417 : : }
5418 : 548 : }
5419 : :
5420 : : /* Return the number of elements for TYPE (which is an ARRAY_TYPE or
5421 : : a VECTOR_TYPE). */
5422 : :
5423 : : static tree
5424 : 3585364 : get_array_or_vector_nelts (const constexpr_ctx *ctx, tree type,
5425 : : bool *non_constant_p, bool *overflow_p,
5426 : : tree *jump_target)
5427 : : {
5428 : 3585364 : tree nelts;
5429 : 3585364 : if (TREE_CODE (type) == ARRAY_TYPE)
5430 : : {
5431 : 3585364 : if (TYPE_DOMAIN (type))
5432 : 3585128 : nelts = array_type_nelts_top (type);
5433 : : else
5434 : 236 : nelts = size_zero_node;
5435 : : }
5436 : 0 : else if (VECTOR_TYPE_P (type))
5437 : 0 : nelts = size_int (TYPE_VECTOR_SUBPARTS (type));
5438 : : else
5439 : 0 : gcc_unreachable ();
5440 : :
5441 : : /* For VLAs, the number of elements won't be an integer constant. */
5442 : 3585364 : nelts = cxx_eval_constant_expression (ctx, nelts, vc_prvalue,
5443 : : non_constant_p, overflow_p,
5444 : : jump_target);
5445 : 3585364 : return nelts;
5446 : : }
5447 : :
5448 : : /* Extract element INDEX consisting of CHARS_PER_ELT chars from
5449 : : STRING_CST STRING. */
5450 : :
5451 : : static tree
5452 : 974406 : extract_string_elt (tree string, unsigned chars_per_elt, unsigned index)
5453 : : {
5454 : 974406 : tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (string)));
5455 : 974406 : tree r;
5456 : :
5457 : 974406 : if (chars_per_elt == 1)
5458 : 719440 : r = build_int_cst (type, TREE_STRING_POINTER (string)[index]);
5459 : : else
5460 : : {
5461 : 254966 : const unsigned char *ptr
5462 : 254966 : = ((const unsigned char *)TREE_STRING_POINTER (string)
5463 : 254966 : + index * chars_per_elt);
5464 : 254966 : r = native_interpret_expr (type, ptr, chars_per_elt);
5465 : : }
5466 : 974406 : return r;
5467 : : }
5468 : :
5469 : : /* Subroutine of cxx_eval_array_reference. T is an ARRAY_REF; evaluate the
5470 : : subscript, diagnose any problems with it, and return the result. */
5471 : :
5472 : : static tree
5473 : 3586114 : eval_and_check_array_index (const constexpr_ctx *ctx,
5474 : : tree t, bool allow_one_past,
5475 : : bool *non_constant_p, bool *overflow_p,
5476 : : tree *jump_target)
5477 : : {
5478 : 3586114 : location_t loc = cp_expr_loc_or_input_loc (t);
5479 : 3586114 : tree ary = TREE_OPERAND (t, 0);
5480 : 3586114 : t = TREE_OPERAND (t, 1);
5481 : 3586114 : tree index = cxx_eval_constant_expression (ctx, t, vc_prvalue,
5482 : : non_constant_p, overflow_p,
5483 : : jump_target);
5484 : 3586114 : if (*jump_target)
5485 : : return NULL_TREE;
5486 : 3586114 : VERIFY_CONSTANT (index);
5487 : :
5488 : 3584866 : if (!tree_fits_shwi_p (index)
5489 : 3584866 : || tree_int_cst_sgn (index) < 0)
5490 : : {
5491 : 108 : diag_array_subscript (loc, ctx, ary, index);
5492 : 108 : *non_constant_p = true;
5493 : 108 : return t;
5494 : : }
5495 : :
5496 : 3584758 : tree nelts = get_array_or_vector_nelts (ctx, TREE_TYPE (ary), non_constant_p,
5497 : : overflow_p, jump_target);
5498 : 3584758 : if (*jump_target)
5499 : : return NULL_TREE;
5500 : 3584758 : VERIFY_CONSTANT (nelts);
5501 : 3584695 : if (allow_one_past
5502 : 3584695 : ? !tree_int_cst_le (index, nelts)
5503 : 2502472 : : !tree_int_cst_lt (index, nelts))
5504 : : {
5505 : 440 : diag_array_subscript (loc, ctx, ary, index);
5506 : 440 : *non_constant_p = true;
5507 : 440 : return t;
5508 : : }
5509 : :
5510 : : return index;
5511 : : }
5512 : :
5513 : : /* Subroutine of cxx_eval_constant_expression.
5514 : : Attempt to reduce a reference to an array slot. */
5515 : :
5516 : : static tree
5517 : 2625284 : cxx_eval_array_reference (const constexpr_ctx *ctx, tree t,
5518 : : value_cat lval,
5519 : : bool *non_constant_p, bool *overflow_p,
5520 : : tree *jump_target)
5521 : : {
5522 : 2625284 : tree oldary = TREE_OPERAND (t, 0);
5523 : 2625284 : tree ary = cxx_eval_constant_expression (ctx, oldary,
5524 : : lval,
5525 : : non_constant_p, overflow_p,
5526 : : jump_target);
5527 : 2625284 : if (*non_constant_p)
5528 : : return t;
5529 : 2447490 : if (*jump_target)
5530 : : return NULL_TREE;
5531 : 2447490 : if (!lval
5532 : 1364135 : && TREE_CODE (ary) == VIEW_CONVERT_EXPR
5533 : 13228 : && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary, 0)))
5534 : 2460718 : && (TYPE_MAIN_VARIANT (TREE_TYPE (t))
5535 : 13228 : == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary, 0))))))
5536 : 13228 : ary = TREE_OPERAND (ary, 0);
5537 : :
5538 : 2447490 : tree oldidx = TREE_OPERAND (t, 1);
5539 : 2447490 : tree index = eval_and_check_array_index (ctx, t, lval,
5540 : : non_constant_p, overflow_p,
5541 : : jump_target);
5542 : 2447490 : if (*non_constant_p)
5543 : : return t;
5544 : 2445689 : if (*jump_target)
5545 : : return NULL_TREE;
5546 : :
5547 : 2445689 : if (lval && ary == oldary && index == oldidx)
5548 : : return t;
5549 : 1458693 : else if (lval == vc_discard)
5550 : : return t;
5551 : 1458693 : else if (lval)
5552 : 94989 : return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL);
5553 : :
5554 : 1363704 : unsigned len = 0, elem_nchars = 1;
5555 : 1363704 : tree elem_type = TREE_TYPE (TREE_TYPE (ary));
5556 : 1363704 : if (TREE_CODE (ary) == CONSTRUCTOR)
5557 : 379698 : len = CONSTRUCTOR_NELTS (ary);
5558 : 984006 : else if (TREE_CODE (ary) == STRING_CST)
5559 : : {
5560 : 970786 : elem_nchars = (TYPE_PRECISION (elem_type)
5561 : 970786 : / TYPE_PRECISION (char_type_node));
5562 : 970786 : len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars;
5563 : : }
5564 : 13220 : else if (TREE_CODE (ary) == VECTOR_CST)
5565 : : /* We don't create variable-length VECTOR_CSTs. */
5566 : 13220 : len = VECTOR_CST_NELTS (ary).to_constant ();
5567 : : else
5568 : : {
5569 : : /* We can't do anything with other tree codes, so use
5570 : : VERIFY_CONSTANT to complain and fail. */
5571 : 0 : VERIFY_CONSTANT (ary);
5572 : 0 : gcc_unreachable ();
5573 : : }
5574 : :
5575 : 1363704 : bool found;
5576 : 1363704 : HOST_WIDE_INT i = 0;
5577 : 1363704 : if (TREE_CODE (ary) == CONSTRUCTOR)
5578 : : {
5579 : 379698 : HOST_WIDE_INT ix = find_array_ctor_elt (ary, index);
5580 : 379698 : found = (ix >= 0);
5581 : 379698 : if (found)
5582 : 377429 : i = ix;
5583 : : }
5584 : : else
5585 : : {
5586 : 984006 : i = tree_to_shwi (index);
5587 : 984006 : found = (i < len);
5588 : : }
5589 : :
5590 : 1363704 : if (found)
5591 : : {
5592 : 1360996 : tree r;
5593 : 1360996 : if (TREE_CODE (ary) == CONSTRUCTOR)
5594 : : {
5595 : 377429 : r = (*CONSTRUCTOR_ELTS (ary))[i].value;
5596 : 377429 : if (TREE_CODE (r) == RAW_DATA_CST)
5597 : : {
5598 : 28962 : tree ridx = (*CONSTRUCTOR_ELTS (ary))[i].index;
5599 : 28962 : gcc_checking_assert (ridx);
5600 : 28962 : unsigned int off
5601 : 28962 : = (wi::to_offset (index) - wi::to_offset (ridx)).to_uhwi ();
5602 : 28962 : r = raw_data_cst_elt (r, off);
5603 : : }
5604 : : }
5605 : 983567 : else if (TREE_CODE (ary) == VECTOR_CST)
5606 : 13220 : r = VECTOR_CST_ELT (ary, i);
5607 : : else
5608 : 970347 : r = extract_string_elt (ary, elem_nchars, i);
5609 : :
5610 : 1012529 : if (r)
5611 : : /* Don't VERIFY_CONSTANT here. */
5612 : 1360996 : return r;
5613 : :
5614 : : /* Otherwise the element doesn't have a value yet. */
5615 : : }
5616 : :
5617 : : /* Not found. */
5618 : :
5619 : 2708 : if (is_really_empty_class (elem_type, /*ignore_vptr*/false))
5620 : 49 : return build_constructor (elem_type, NULL);
5621 : :
5622 : 2659 : if (TREE_CODE (ary) == CONSTRUCTOR
5623 : 2659 : && CONSTRUCTOR_NO_CLEARING (ary))
5624 : : {
5625 : : /* 'ary' is part of the aggregate initializer we're currently
5626 : : building; if there's no initializer for this element yet,
5627 : : that's an error. */
5628 : 51 : if (!ctx->quiet)
5629 : 16 : error ("accessing uninitialized array element");
5630 : 51 : *non_constant_p = true;
5631 : 51 : return t;
5632 : : }
5633 : :
5634 : : /* If it's within the array bounds but doesn't have an explicit
5635 : : initializer, it's initialized from {}. But use build_value_init
5636 : : directly for non-aggregates to avoid creating a garbage CONSTRUCTOR. */
5637 : 2608 : tree val;
5638 : 2608 : constexpr_ctx new_ctx;
5639 : 2608 : if (CP_AGGREGATE_TYPE_P (elem_type))
5640 : : {
5641 : 405 : tree empty_ctor = build_constructor (init_list_type_node, NULL);
5642 : 405 : val = digest_init (elem_type, empty_ctor, tf_warning_or_error);
5643 : : }
5644 : : else
5645 : 2203 : val = build_value_init (elem_type, tf_warning_or_error);
5646 : :
5647 : : /* Create a new constructor only if we don't already have a suitable one. */
5648 : 2608 : const bool new_ctor = (!SCALAR_TYPE_P (elem_type)
5649 : 3039 : && (!ctx->ctor
5650 : 15 : || !same_type_ignoring_top_level_qualifiers_p
5651 : 15 : (elem_type, TREE_TYPE (ctx->ctor))));
5652 : 422 : if (new_ctor)
5653 : : {
5654 : 422 : new_ctx = *ctx;
5655 : : /* We clear the object here. We used to replace it with T, but that
5656 : : caused problems (101371, 108158); and anyway, T is the initializer,
5657 : : not the target object. */
5658 : 422 : new_ctx.object = NULL_TREE;
5659 : 422 : new_ctx.ctor = build_constructor (elem_type, NULL);
5660 : 422 : ctx = &new_ctx;
5661 : : }
5662 : 2608 : t = cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
5663 : : overflow_p, jump_target);
5664 : 2608 : if (new_ctor && t != ctx->ctor)
5665 : 401 : free_constructor (ctx->ctor);
5666 : : return t;
5667 : : }
5668 : :
5669 : : /* Subroutine of cxx_eval_constant_expression.
5670 : : Attempt to reduce a field access of a value of class type. */
5671 : :
5672 : : static tree
5673 : 28762880 : cxx_eval_component_reference (const constexpr_ctx *ctx, tree t,
5674 : : value_cat lval,
5675 : : bool *non_constant_p, bool *overflow_p,
5676 : : tree *jump_target)
5677 : : {
5678 : 28762880 : unsigned HOST_WIDE_INT i;
5679 : 28762880 : tree field;
5680 : 28762880 : tree value;
5681 : 28762880 : tree part = TREE_OPERAND (t, 1);
5682 : 28762880 : tree orig_whole = TREE_OPERAND (t, 0);
5683 : 28762880 : tree whole = cxx_eval_constant_expression (ctx, orig_whole,
5684 : : lval,
5685 : : non_constant_p, overflow_p,
5686 : : jump_target);
5687 : 28762880 : if (*non_constant_p)
5688 : : return t;
5689 : 14530121 : if (*jump_target)
5690 : : return NULL_TREE;
5691 : 14530121 : if (INDIRECT_REF_P (whole)
5692 : 14530121 : && integer_zerop (TREE_OPERAND (whole, 0)))
5693 : : {
5694 : 177 : if (!ctx->quiet)
5695 : 39 : error ("dereferencing a null pointer in %qE", orig_whole);
5696 : 177 : *non_constant_p = true;
5697 : 177 : return t;
5698 : : }
5699 : :
5700 : 14529944 : if (TREE_CODE (whole) == PTRMEM_CST)
5701 : 1289 : whole = cplus_expand_constant (whole);
5702 : 14529944 : if (whole == orig_whole)
5703 : : return t;
5704 : 9005320 : if (lval == vc_discard)
5705 : : return t;
5706 : 9005296 : if (lval)
5707 : 3951101 : return fold_build3 (COMPONENT_REF, TREE_TYPE (t),
5708 : : whole, part, NULL_TREE);
5709 : : /* Don't VERIFY_CONSTANT here; we only want to check that we got a
5710 : : CONSTRUCTOR. */
5711 : 5054195 : if (TREE_CODE (whole) != CONSTRUCTOR)
5712 : : {
5713 : 0 : if (!ctx->quiet)
5714 : 0 : error ("%qE is not a constant expression", orig_whole);
5715 : 0 : *non_constant_p = true;
5716 : 0 : return t;
5717 : : }
5718 : 5052778 : if ((cxx_dialect < cxx14 || CONSTRUCTOR_MUTABLE_POISON (whole))
5719 : 5055782 : && DECL_MUTABLE_P (part))
5720 : : {
5721 : 132 : if (!ctx->quiet)
5722 : 16 : error ("mutable %qD is not usable in a constant expression", part);
5723 : 132 : *non_constant_p = true;
5724 : 132 : return t;
5725 : : }
5726 : :
5727 : 5054063 : bool pmf = TYPE_PTRMEMFUNC_P (TREE_TYPE (whole));
5728 : 7194004 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
5729 : : {
5730 : : /* Use name match for PMF fields, as a variant will have a
5731 : : different FIELD_DECL with a different type. */
5732 : 7192104 : if (pmf ? DECL_NAME (field) == DECL_NAME (part)
5733 : : : field == part)
5734 : : {
5735 : 5052163 : if (value)
5736 : : {
5737 : 5052145 : STRIP_ANY_LOCATION_WRAPPER (value);
5738 : 5052145 : return value;
5739 : : }
5740 : : else
5741 : : /* We're in the middle of initializing it. */
5742 : : break;
5743 : : }
5744 : : }
5745 : 1918 : if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE)
5746 : : {
5747 : 86 : if (CONSTRUCTOR_NELTS (whole) > 0)
5748 : : {
5749 : : /* DR 1188 says we don't have to deal with this. */
5750 : 71 : if (!ctx->quiet)
5751 : : {
5752 : 18 : constructor_elt *cep = CONSTRUCTOR_ELT (whole, 0);
5753 : 18 : if (cep->value == NULL_TREE)
5754 : 9 : error ("accessing uninitialized member %qD", part);
5755 : : else
5756 : 9 : error ("accessing %qD member instead of initialized %qD member "
5757 : : "in constant expression", part, cep->index);
5758 : : }
5759 : 71 : *non_constant_p = true;
5760 : 71 : return t;
5761 : : }
5762 : 15 : else if (!CONSTRUCTOR_NO_CLEARING (whole))
5763 : : {
5764 : : /* Value-initialized union, check if looking at the first member. */
5765 : 12 : tree first = next_aggregate_field (TYPE_FIELDS (TREE_TYPE (whole)));
5766 : 12 : if (first != part)
5767 : : {
5768 : 9 : if (!ctx->quiet)
5769 : 3 : error ("accessing %qD member instead of initialized %qD "
5770 : : "member in constant expression", part, first);
5771 : 9 : *non_constant_p = true;
5772 : 9 : return t;
5773 : : }
5774 : : }
5775 : : }
5776 : :
5777 : : /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty
5778 : : classes never get represented; throw together a value now. */
5779 : 1838 : if (is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
5780 : 1229 : return build_constructor (TREE_TYPE (t), NULL);
5781 : :
5782 : 609 : gcc_assert (DECL_CONTEXT (part) == TYPE_MAIN_VARIANT (TREE_TYPE (whole)));
5783 : :
5784 : 609 : if (CONSTRUCTOR_NO_CLEARING (whole))
5785 : : {
5786 : : /* 'whole' is part of the aggregate initializer we're currently
5787 : : building; if there's no initializer for this member yet, that's an
5788 : : error. */
5789 : 90 : if (!ctx->quiet)
5790 : 21 : error ("accessing uninitialized member %qD", part);
5791 : 90 : *non_constant_p = true;
5792 : 90 : return t;
5793 : : }
5794 : :
5795 : : /* If there's no explicit init for this field, it's value-initialized. */
5796 : 519 : value = build_value_init (TREE_TYPE (t), tf_warning_or_error);
5797 : 519 : return cxx_eval_constant_expression (ctx, value,
5798 : : lval,
5799 : : non_constant_p, overflow_p,
5800 : 519 : jump_target);
5801 : : }
5802 : :
5803 : : /* Subroutine of cxx_eval_constant_expression.
5804 : : Attempt to reduce a field access of a value of class type that is
5805 : : expressed as a BIT_FIELD_REF. */
5806 : :
5807 : : static tree
5808 : 49 : cxx_eval_bit_field_ref (const constexpr_ctx *ctx, tree t,
5809 : : value_cat lval,
5810 : : bool *non_constant_p, bool *overflow_p,
5811 : : tree *jump_target)
5812 : : {
5813 : 49 : tree orig_whole = TREE_OPERAND (t, 0);
5814 : 49 : tree retval, fldval, utype, mask;
5815 : 49 : bool fld_seen = false;
5816 : 49 : HOST_WIDE_INT istart, isize;
5817 : 49 : tree whole = cxx_eval_constant_expression (ctx, orig_whole,
5818 : : lval,
5819 : : non_constant_p, overflow_p,
5820 : : jump_target);
5821 : 49 : tree start, field, value;
5822 : 49 : unsigned HOST_WIDE_INT i;
5823 : :
5824 : 49 : if (*jump_target)
5825 : : return NULL_TREE;
5826 : 49 : if (whole == orig_whole)
5827 : : return t;
5828 : : /* Don't VERIFY_CONSTANT here; we only want to check that we got a
5829 : : CONSTRUCTOR. */
5830 : 2 : if (!*non_constant_p
5831 : 2 : && TREE_CODE (whole) != VECTOR_CST
5832 : 0 : && TREE_CODE (whole) != CONSTRUCTOR)
5833 : : {
5834 : 0 : if (!ctx->quiet)
5835 : 0 : error ("%qE is not a constant expression", orig_whole);
5836 : 0 : *non_constant_p = true;
5837 : : }
5838 : 2 : if (*non_constant_p)
5839 : : return t;
5840 : :
5841 : 2 : if (TREE_CODE (whole) == VECTOR_CST || !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5842 : : {
5843 : 2 : if (tree r = fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
5844 : : TREE_OPERAND (t, 1), TREE_OPERAND (t, 2)))
5845 : : return r;
5846 : 0 : if (!ctx->quiet)
5847 : 0 : error ("%qE is not a constant expression", orig_whole);
5848 : 0 : *non_constant_p = true;
5849 : 0 : return t;
5850 : : }
5851 : :
5852 : 0 : start = TREE_OPERAND (t, 2);
5853 : 0 : istart = tree_to_shwi (start);
5854 : 0 : isize = tree_to_shwi (TREE_OPERAND (t, 1));
5855 : 0 : utype = TREE_TYPE (t);
5856 : 0 : if (!TYPE_UNSIGNED (utype))
5857 : 0 : utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1);
5858 : 0 : retval = build_int_cst (utype, 0);
5859 : 0 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
5860 : : {
5861 : 0 : tree bitpos = bit_position (field);
5862 : 0 : STRIP_ANY_LOCATION_WRAPPER (value);
5863 : 0 : if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1))
5864 : : return value;
5865 : 0 : if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE
5866 : 0 : && TREE_CODE (value) == INTEGER_CST
5867 : 0 : && tree_fits_shwi_p (bitpos)
5868 : 0 : && tree_fits_shwi_p (DECL_SIZE (field)))
5869 : : {
5870 : 0 : HOST_WIDE_INT bit = tree_to_shwi (bitpos);
5871 : 0 : HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field));
5872 : 0 : HOST_WIDE_INT shift;
5873 : 0 : if (bit >= istart && bit + sz <= istart + isize)
5874 : : {
5875 : 0 : fldval = fold_convert (utype, value);
5876 : 0 : mask = build_int_cst_type (utype, -1);
5877 : 0 : mask = fold_build2 (LSHIFT_EXPR, utype, mask,
5878 : : size_int (TYPE_PRECISION (utype) - sz));
5879 : 0 : mask = fold_build2 (RSHIFT_EXPR, utype, mask,
5880 : : size_int (TYPE_PRECISION (utype) - sz));
5881 : 0 : fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask);
5882 : 0 : shift = bit - istart;
5883 : 0 : if (BYTES_BIG_ENDIAN)
5884 : : shift = TYPE_PRECISION (utype) - shift - sz;
5885 : 0 : fldval = fold_build2 (LSHIFT_EXPR, utype, fldval,
5886 : : size_int (shift));
5887 : 0 : retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval);
5888 : 0 : fld_seen = true;
5889 : : }
5890 : : }
5891 : : }
5892 : 0 : if (fld_seen)
5893 : 0 : return fold_convert (TREE_TYPE (t), retval);
5894 : 0 : gcc_unreachable ();
5895 : : return error_mark_node;
5896 : : }
5897 : :
5898 : : /* Helper for cxx_eval_bit_cast.
5899 : : Check [bit.cast]/3 rules, bit_cast is constexpr only if the To and From
5900 : : types and types of all subobjects have is_union_v<T>, is_pointer_v<T>,
5901 : : is_member_pointer_v<T>, is_volatile_v<T> false and has no non-static
5902 : : data members of reference type. */
5903 : :
5904 : : static bool
5905 : 4820 : check_bit_cast_type (const constexpr_ctx *ctx, location_t loc, tree type,
5906 : : tree orig_type)
5907 : : {
5908 : 5313 : if (TREE_CODE (type) == UNION_TYPE)
5909 : : {
5910 : 63 : if (!ctx->quiet)
5911 : : {
5912 : 21 : if (type == orig_type)
5913 : 6 : error_at (loc, "%qs is not a constant expression because %qT is "
5914 : : "a union type", "__builtin_bit_cast", type);
5915 : : else
5916 : 15 : error_at (loc, "%qs is not a constant expression because %qT "
5917 : : "contains a union type", "__builtin_bit_cast",
5918 : : orig_type);
5919 : : }
5920 : 63 : return true;
5921 : : }
5922 : : if (TREE_CODE (type) == POINTER_TYPE)
5923 : : {
5924 : 60 : if (!ctx->quiet)
5925 : : {
5926 : 18 : if (type == orig_type)
5927 : 6 : error_at (loc, "%qs is not a constant expression because %qT is "
5928 : : "a pointer type", "__builtin_bit_cast", type);
5929 : : else
5930 : 12 : error_at (loc, "%qs is not a constant expression because %qT "
5931 : : "contains a pointer type", "__builtin_bit_cast",
5932 : : orig_type);
5933 : : }
5934 : 60 : return true;
5935 : : }
5936 : : if (TREE_CODE (type) == REFERENCE_TYPE)
5937 : : {
5938 : 0 : if (!ctx->quiet)
5939 : : {
5940 : 0 : if (type == orig_type)
5941 : 0 : error_at (loc, "%qs is not a constant expression because %qT is "
5942 : : "a reference type", "__builtin_bit_cast", type);
5943 : : else
5944 : 0 : error_at (loc, "%qs is not a constant expression because %qT "
5945 : : "contains a reference type", "__builtin_bit_cast",
5946 : : orig_type);
5947 : : }
5948 : 0 : return true;
5949 : : }
5950 : 1361 : if (TYPE_PTRMEM_P (type))
5951 : : {
5952 : 36 : if (!ctx->quiet)
5953 : : {
5954 : 12 : if (type == orig_type)
5955 : 12 : error_at (loc, "%qs is not a constant expression because %qT is "
5956 : : "a pointer to member type", "__builtin_bit_cast",
5957 : : type);
5958 : : else
5959 : 0 : error_at (loc, "%qs is not a constant expression because %qT "
5960 : : "contains a pointer to member type",
5961 : : "__builtin_bit_cast", orig_type);
5962 : : }
5963 : 36 : return true;
5964 : : }
5965 : 5154 : if (TYPE_VOLATILE (type))
5966 : : {
5967 : 0 : if (!ctx->quiet)
5968 : : {
5969 : 0 : if (type == orig_type)
5970 : 0 : error_at (loc, "%qs is not a constant expression because %qT is "
5971 : : "volatile", "__builtin_bit_cast", type);
5972 : : else
5973 : 0 : error_at (loc, "%qs is not a constant expression because %qT "
5974 : : "contains a volatile subobject",
5975 : : "__builtin_bit_cast", orig_type);
5976 : : }
5977 : 0 : return true;
5978 : : }
5979 : 5154 : if (TREE_CODE (type) == RECORD_TYPE)
5980 : 27024 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5981 : 25771 : if (TREE_CODE (field) == FIELD_DECL
5982 : 25771 : && check_bit_cast_type (ctx, loc, TREE_TYPE (field), orig_type))
5983 : : return true;
5984 : 5064 : if (TREE_CODE (type) == ARRAY_TYPE)
5985 : 493 : return check_bit_cast_type (ctx, loc, TREE_TYPE (type), orig_type);
5986 : : return false;
5987 : : }
5988 : :
5989 : : /* Helper function for cxx_eval_bit_cast. For unsigned char or
5990 : : std::byte members of CONSTRUCTOR (recursively) if they contain
5991 : : some indeterminate bits (as set in MASK), remove the ctor elts,
5992 : : mark the CONSTRUCTOR as CONSTRUCTOR_NO_CLEARING and clear the
5993 : : bits in MASK. */
5994 : :
5995 : : static void
5996 : 603 : clear_uchar_or_std_byte_in_mask (location_t loc, tree t, unsigned char *mask)
5997 : : {
5998 : 603 : if (TREE_CODE (t) != CONSTRUCTOR)
5999 : : return;
6000 : :
6001 : : unsigned i, j = 0;
6002 : : tree index, value;
6003 : 2090 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), i, index, value)
6004 : : {
6005 : 1487 : tree type = TREE_TYPE (value);
6006 : 1487 : if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
6007 : 2535 : && DECL_BIT_FIELD_TYPE (index) != NULL_TREE)
6008 : : {
6009 : 270 : if (is_byte_access_type_not_plain_char (DECL_BIT_FIELD_TYPE (index)))
6010 : : {
6011 : 135 : HOST_WIDE_INT fldsz = TYPE_PRECISION (TREE_TYPE (index));
6012 : 135 : gcc_assert (fldsz != 0);
6013 : 135 : HOST_WIDE_INT pos = int_byte_position (index);
6014 : 135 : HOST_WIDE_INT bpos
6015 : 135 : = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (index));
6016 : 135 : bpos %= BITS_PER_UNIT;
6017 : 135 : HOST_WIDE_INT end
6018 : 135 : = ROUND_UP (bpos + fldsz, BITS_PER_UNIT) / BITS_PER_UNIT;
6019 : 135 : gcc_assert (end == 1 || end == 2);
6020 : 135 : unsigned char *p = mask + pos;
6021 : 135 : unsigned char mask_save[2];
6022 : 135 : mask_save[0] = mask[pos];
6023 : 135 : mask_save[1] = end == 2 ? mask[pos + 1] : 0;
6024 : 135 : if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
6025 : : sorry_at (loc, "PDP11 bit-field handling unsupported"
6026 : : " in %qs", "__builtin_bit_cast");
6027 : 135 : else if (BYTES_BIG_ENDIAN)
6028 : : {
6029 : : /* Big endian. */
6030 : : if (bpos + fldsz <= BITS_PER_UNIT)
6031 : : *p &= ~(((1 << fldsz) - 1)
6032 : : << (BITS_PER_UNIT - bpos - fldsz));
6033 : : else
6034 : : {
6035 : : gcc_assert (bpos);
6036 : : *p &= ~(((1U << BITS_PER_UNIT) - 1) >> bpos);
6037 : : p++;
6038 : : fldsz -= BITS_PER_UNIT - bpos;
6039 : : gcc_assert (fldsz && fldsz < BITS_PER_UNIT);
6040 : : *p &= ((1U << BITS_PER_UNIT) - 1) >> fldsz;
6041 : : }
6042 : : }
6043 : : else
6044 : : {
6045 : : /* Little endian. */
6046 : 135 : if (bpos + fldsz <= BITS_PER_UNIT)
6047 : 135 : *p &= ~(((1 << fldsz) - 1) << bpos);
6048 : : else
6049 : : {
6050 : 0 : gcc_assert (bpos);
6051 : 0 : *p &= ~(((1 << BITS_PER_UNIT) - 1) << bpos);
6052 : 0 : p++;
6053 : 0 : fldsz -= BITS_PER_UNIT - bpos;
6054 : 0 : gcc_assert (fldsz && fldsz < BITS_PER_UNIT);
6055 : 0 : *p &= ~((1 << fldsz) - 1);
6056 : : }
6057 : : }
6058 : 135 : if (mask_save[0] != mask[pos]
6059 : 99 : || (end == 2 && mask_save[1] != mask[pos + 1]))
6060 : : {
6061 : 36 : CONSTRUCTOR_NO_CLEARING (t) = 1;
6062 : 36 : continue;
6063 : : }
6064 : : }
6065 : : }
6066 : 1217 : else if (is_byte_access_type_not_plain_char (type))
6067 : : {
6068 : 228 : HOST_WIDE_INT pos;
6069 : 228 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6070 : 132 : pos = tree_to_shwi (index);
6071 : : else
6072 : 96 : pos = int_byte_position (index);
6073 : 228 : if (mask[pos])
6074 : : {
6075 : 48 : CONSTRUCTOR_NO_CLEARING (t) = 1;
6076 : 48 : mask[pos] = 0;
6077 : 48 : continue;
6078 : : }
6079 : : }
6080 : 1403 : if (TREE_CODE (value) == CONSTRUCTOR)
6081 : : {
6082 : 264 : HOST_WIDE_INT pos;
6083 : 264 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6084 : 144 : pos = tree_to_shwi (index)
6085 : 72 : * tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))));
6086 : : else
6087 : 192 : pos = int_byte_position (index);
6088 : 264 : clear_uchar_or_std_byte_in_mask (loc, value, mask + pos);
6089 : : }
6090 : 1403 : if (i != j)
6091 : : {
6092 : 180 : CONSTRUCTOR_ELT (t, j)->index = index;
6093 : 180 : CONSTRUCTOR_ELT (t, j)->value = value;
6094 : : }
6095 : 1403 : ++j;
6096 : : }
6097 : 1206 : if (CONSTRUCTOR_NELTS (t) != j)
6098 : 84 : vec_safe_truncate (CONSTRUCTOR_ELTS (t), j);
6099 : : }
6100 : :
6101 : : /* Subroutine of cxx_eval_constant_expression.
6102 : : Attempt to evaluate a BIT_CAST_EXPR. */
6103 : :
6104 : : static tree
6105 : 1025 : cxx_eval_bit_cast (const constexpr_ctx *ctx, tree t, bool *non_constant_p,
6106 : : bool *overflow_p, tree *jump_target)
6107 : : {
6108 : 1025 : if (check_bit_cast_type (ctx, EXPR_LOCATION (t), TREE_TYPE (t),
6109 : 1025 : TREE_TYPE (t))
6110 : 3325 : || check_bit_cast_type (ctx, cp_expr_loc_or_loc (TREE_OPERAND (t, 0),
6111 : 944 : EXPR_LOCATION (t)),
6112 : 944 : TREE_TYPE (TREE_OPERAND (t, 0)),
6113 : 944 : TREE_TYPE (TREE_OPERAND (t, 0))))
6114 : : {
6115 : 159 : *non_constant_p = true;
6116 : 159 : return t;
6117 : : }
6118 : :
6119 : 866 : tree op = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), vc_prvalue,
6120 : : non_constant_p, overflow_p,
6121 : : jump_target);
6122 : 866 : if (*non_constant_p)
6123 : : return t;
6124 : 770 : if (*jump_target)
6125 : : return NULL_TREE;
6126 : :
6127 : 770 : location_t loc = EXPR_LOCATION (t);
6128 : 770 : if (BITS_PER_UNIT != 8 || CHAR_BIT != 8)
6129 : : {
6130 : : if (!ctx->quiet)
6131 : : sorry_at (loc, "%qs cannot be constant evaluated on the target",
6132 : : "__builtin_bit_cast");
6133 : : *non_constant_p = true;
6134 : : return t;
6135 : : }
6136 : :
6137 : 770 : if (!tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (t))))
6138 : : {
6139 : 0 : if (!ctx->quiet)
6140 : 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
6141 : : "type is too large", "__builtin_bit_cast");
6142 : 0 : *non_constant_p = true;
6143 : 0 : return t;
6144 : : }
6145 : :
6146 : 770 : HOST_WIDE_INT len = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (t)));
6147 : 770 : if (len < 0 || (int) len != len)
6148 : : {
6149 : 0 : if (!ctx->quiet)
6150 : 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
6151 : : "type is too large", "__builtin_bit_cast");
6152 : 0 : *non_constant_p = true;
6153 : 0 : return t;
6154 : : }
6155 : :
6156 : 770 : unsigned char buf[64];
6157 : 770 : unsigned char *ptr, *mask;
6158 : 770 : size_t alen = (size_t) len * 2;
6159 : 770 : if (alen <= sizeof (buf))
6160 : : ptr = buf;
6161 : : else
6162 : 3 : ptr = XNEWVEC (unsigned char, alen);
6163 : 770 : mask = ptr + (size_t) len;
6164 : : /* At the beginning consider everything indeterminate. */
6165 : 770 : memset (mask, ~0, (size_t) len);
6166 : :
6167 : 770 : if (native_encode_initializer (op, ptr, len, 0, mask) != len)
6168 : : {
6169 : 0 : if (!ctx->quiet)
6170 : 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
6171 : : "argument cannot be encoded", "__builtin_bit_cast");
6172 : 0 : *non_constant_p = true;
6173 : 0 : if (ptr != buf)
6174 : 0 : XDELETE (ptr);
6175 : 0 : return t;
6176 : : }
6177 : :
6178 : 770 : tree r = NULL_TREE;
6179 : 770 : if (can_native_interpret_type_p (TREE_TYPE (t)))
6180 : : {
6181 : 431 : r = native_interpret_expr (TREE_TYPE (t), ptr, len);
6182 : 431 : if (is_byte_access_type_not_plain_char (TREE_TYPE (t)))
6183 : : {
6184 : 46 : gcc_assert (len == 1);
6185 : 46 : if (mask[0])
6186 : : {
6187 : 24 : memset (mask, 0, len);
6188 : 24 : r = build_constructor (TREE_TYPE (r), NULL);
6189 : 24 : CONSTRUCTOR_NO_CLEARING (r) = 1;
6190 : : }
6191 : : }
6192 : : }
6193 : 339 : else if (TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE)
6194 : : {
6195 : 339 : r = native_interpret_aggregate (TREE_TYPE (t), ptr, 0, len);
6196 : 339 : if (r != NULL_TREE)
6197 : : {
6198 : 339 : clear_type_padding_in_mask (TREE_TYPE (t), mask);
6199 : 339 : clear_uchar_or_std_byte_in_mask (loc, r, mask);
6200 : 339 : if (CHECKING_P)
6201 : : {
6202 : 339 : tree e = cxx_eval_bare_aggregate (ctx, r, vc_prvalue,
6203 : : non_constant_p, overflow_p,
6204 : : jump_target);
6205 : 339 : gcc_checking_assert (e == r && !*jump_target);
6206 : : r = e;
6207 : : }
6208 : : }
6209 : : }
6210 : :
6211 : 770 : if (r != NULL_TREE)
6212 : : {
6213 : 6169 : for (int i = 0; i < len; i++)
6214 : 5489 : if (mask[i])
6215 : : {
6216 : 90 : if (!ctx->quiet)
6217 : 30 : error_at (loc, "%qs accessing uninitialized byte at offset %d",
6218 : : "__builtin_bit_cast", i);
6219 : 90 : *non_constant_p = true;
6220 : 90 : r = t;
6221 : 90 : break;
6222 : : }
6223 : 770 : if (ptr != buf)
6224 : 3 : XDELETE (ptr);
6225 : 770 : return r;
6226 : : }
6227 : :
6228 : 0 : if (!ctx->quiet)
6229 : 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
6230 : : "argument cannot be interpreted", "__builtin_bit_cast");
6231 : 0 : *non_constant_p = true;
6232 : 0 : if (ptr != buf)
6233 : 0 : XDELETE (ptr);
6234 : : return t;
6235 : : }
6236 : :
6237 : : /* Subroutine of cxx_eval_constant_expression.
6238 : : Evaluate a short-circuited logical expression T in the context
6239 : : of a given constexpr CALL. BAILOUT_VALUE is the value for
6240 : : early return. CONTINUE_VALUE is used here purely for
6241 : : sanity check purposes. */
6242 : :
6243 : : static tree
6244 : 6836366 : cxx_eval_logical_expression (const constexpr_ctx *ctx, tree t,
6245 : : tree bailout_value, tree continue_value,
6246 : : bool *non_constant_p, bool *overflow_p,
6247 : : tree *jump_target)
6248 : : {
6249 : 6836366 : tree r;
6250 : 6836366 : tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
6251 : : vc_prvalue, non_constant_p,
6252 : : overflow_p, jump_target);
6253 : 6836366 : if (*jump_target)
6254 : : return NULL_TREE;
6255 : 6836357 : VERIFY_CONSTANT (lhs);
6256 : 4116777 : if (tree_int_cst_equal (lhs, bailout_value))
6257 : : return lhs;
6258 : 3510057 : gcc_assert (tree_int_cst_equal (lhs, continue_value));
6259 : 3510057 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
6260 : : vc_prvalue, non_constant_p,
6261 : : overflow_p, jump_target);
6262 : 3510057 : if (*jump_target)
6263 : : return NULL_TREE;
6264 : 3510057 : VERIFY_CONSTANT (r);
6265 : : return r;
6266 : : }
6267 : :
6268 : : /* REF is a COMPONENT_REF designating a particular field. V is a vector of
6269 : : CONSTRUCTOR elements to initialize (part of) an object containing that
6270 : : field. Return a pointer to the constructor_elt corresponding to the
6271 : : initialization of the field. */
6272 : :
6273 : : static constructor_elt *
6274 : 0 : base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref)
6275 : : {
6276 : 0 : tree aggr = TREE_OPERAND (ref, 0);
6277 : 0 : tree field = TREE_OPERAND (ref, 1);
6278 : 0 : HOST_WIDE_INT i;
6279 : 0 : constructor_elt *ce;
6280 : :
6281 : 0 : gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
6282 : :
6283 : 0 : if (TREE_CODE (aggr) == COMPONENT_REF)
6284 : : {
6285 : 0 : constructor_elt *base_ce
6286 : 0 : = base_field_constructor_elt (v, aggr);
6287 : 0 : v = CONSTRUCTOR_ELTS (base_ce->value);
6288 : : }
6289 : :
6290 : 0 : for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
6291 : 0 : if (ce->index == field)
6292 : 0 : return ce;
6293 : :
6294 : 0 : gcc_unreachable ();
6295 : : return NULL;
6296 : : }
6297 : :
6298 : : /* Some of the expressions fed to the constexpr mechanism are calls to
6299 : : constructors, which have type void. In that case, return the type being
6300 : : initialized by the constructor. */
6301 : :
6302 : : static tree
6303 : 372572655 : initialized_type (tree t)
6304 : : {
6305 : 373256346 : if (TYPE_P (t))
6306 : : return t;
6307 : 373256011 : tree type = TREE_TYPE (t);
6308 : 373256011 : if (TREE_CODE (t) == CALL_EXPR)
6309 : : {
6310 : : /* A constructor call has void type, so we need to look deeper. */
6311 : 45162761 : tree fn = get_function_named_in_call (t);
6312 : 45162748 : if (fn && TREE_CODE (fn) == FUNCTION_DECL
6313 : 90279609 : && DECL_CXX_CONSTRUCTOR_P (fn))
6314 : 2295481 : type = DECL_CONTEXT (fn);
6315 : : }
6316 : 328093250 : else if (TREE_CODE (t) == COMPOUND_EXPR)
6317 : 683691 : return initialized_type (TREE_OPERAND (t, 1));
6318 : 327409559 : else if (TREE_CODE (t) == AGGR_INIT_EXPR)
6319 : 496143 : type = TREE_TYPE (AGGR_INIT_EXPR_SLOT (t));
6320 : 372572320 : return cv_unqualified (type);
6321 : : }
6322 : :
6323 : : /* We're about to initialize element INDEX of an array or class from VALUE.
6324 : : Set up NEW_CTX appropriately by adjusting .object to refer to the
6325 : : subobject and creating a new CONSTRUCTOR if the element is itself
6326 : : a class or array. */
6327 : :
6328 : : static void
6329 : 1638430 : init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx,
6330 : : tree index, tree &value)
6331 : : {
6332 : 1638430 : new_ctx = *ctx;
6333 : :
6334 : 1638430 : if (index && TREE_CODE (index) != INTEGER_CST
6335 : 1515183 : && TREE_CODE (index) != FIELD_DECL
6336 : 3 : && TREE_CODE (index) != RANGE_EXPR)
6337 : : /* This won't have an element in the new CONSTRUCTOR. */
6338 : : return;
6339 : :
6340 : 1638430 : tree type = initialized_type (value);
6341 : 1638430 : if (!AGGREGATE_TYPE_P (type) && !VECTOR_TYPE_P (type))
6342 : : /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
6343 : : return;
6344 : 746953 : if (VECTOR_TYPE_P (type)
6345 : 1103 : && VECTOR_TYPE_P (TREE_TYPE (ctx->ctor))
6346 : 746989 : && index == NULL_TREE)
6347 : : /* A vector inside of a vector CONSTRUCTOR, e.g. when a larger
6348 : : vector is constructed from smaller vectors, doesn't get its own
6349 : : CONSTRUCTOR either. */
6350 : : return;
6351 : :
6352 : : /* The sub-aggregate initializer might contain a placeholder;
6353 : : update object to refer to the subobject and ctor to refer to
6354 : : the (newly created) sub-initializer. */
6355 : 746917 : if (ctx->object)
6356 : : {
6357 : 579818 : if (index == NULL_TREE || TREE_CODE (index) == RANGE_EXPR)
6358 : : /* There's no well-defined subobject for this index. */
6359 : 3 : new_ctx.object = NULL_TREE;
6360 : : else
6361 : 579815 : new_ctx.object = build_ctor_subob_ref (index, type, ctx->object);
6362 : : }
6363 : :
6364 : 746917 : if (is_empty_class (type))
6365 : : /* Leave ctor null for an empty subobject, they aren't represented in the
6366 : : result of evaluation. */
6367 : 705684 : new_ctx.ctor = NULL_TREE;
6368 : : else
6369 : : {
6370 : 41233 : tree elt = build_constructor (type, NULL);
6371 : 41233 : CONSTRUCTOR_NO_CLEARING (elt) = true;
6372 : 41233 : new_ctx.ctor = elt;
6373 : : }
6374 : :
6375 : 746917 : if (TREE_CODE (value) == TARGET_EXPR)
6376 : : /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
6377 : 30840 : value = TARGET_EXPR_INITIAL (value);
6378 : : }
6379 : :
6380 : : /* We're about to process an initializer for a class or array TYPE. Make
6381 : : sure that CTX is set up appropriately. */
6382 : :
6383 : : static void
6384 : 1253202 : verify_ctor_sanity (const constexpr_ctx *ctx, tree type)
6385 : : {
6386 : : /* We don't bother building a ctor for an empty base subobject. */
6387 : 1253202 : if (is_empty_class (type))
6388 : : return;
6389 : :
6390 : : /* We're in the middle of an initializer that might involve placeholders;
6391 : : our caller should have created a CONSTRUCTOR for us to put the
6392 : : initializer into. We will either return that constructor or T. */
6393 : 549685 : gcc_assert (ctx->ctor);
6394 : 549685 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
6395 : : (type, TREE_TYPE (ctx->ctor)));
6396 : : /* We used to check that ctx->ctor was empty, but that isn't the case when
6397 : : the object is zero-initialized before calling the constructor. */
6398 : 549685 : if (ctx->object)
6399 : : {
6400 : 414720 : tree otype = TREE_TYPE (ctx->object);
6401 : 414720 : gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, otype)
6402 : : /* Handle flexible array members. */
6403 : : || (TREE_CODE (otype) == ARRAY_TYPE
6404 : : && TYPE_DOMAIN (otype) == NULL_TREE
6405 : : && TREE_CODE (type) == ARRAY_TYPE
6406 : : && (same_type_ignoring_top_level_qualifiers_p
6407 : : (TREE_TYPE (type), TREE_TYPE (otype)))));
6408 : : }
6409 : 549685 : gcc_assert (!ctx->object || !DECL_P (ctx->object)
6410 : : || ctx->global->get_value (ctx->object) == ctx->ctor);
6411 : : }
6412 : :
6413 : : /* Subroutine of cxx_eval_constant_expression.
6414 : : The expression tree T denotes a C-style array or a C-style
6415 : : aggregate. Reduce it to a constant expression. */
6416 : :
6417 : : static tree
6418 : 1252596 : cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t,
6419 : : value_cat lval,
6420 : : bool *non_constant_p, bool *overflow_p,
6421 : : tree *jump_target)
6422 : : {
6423 : 1252596 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
6424 : 1252596 : bool changed = false;
6425 : 1252596 : gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t));
6426 : 1252596 : tree type = TREE_TYPE (t);
6427 : :
6428 : 1252596 : constexpr_ctx new_ctx;
6429 : 1252596 : if (TYPE_PTRMEMFUNC_P (type) || VECTOR_TYPE_P (type))
6430 : : {
6431 : : /* We don't really need the ctx->ctor business for a PMF or
6432 : : vector, but it's simpler to use the same code. */
6433 : 24865 : new_ctx = *ctx;
6434 : 24865 : new_ctx.ctor = build_constructor (type, NULL);
6435 : 24865 : new_ctx.object = NULL_TREE;
6436 : 24865 : ctx = &new_ctx;
6437 : 1252596 : };
6438 : 1252596 : verify_ctor_sanity (ctx, type);
6439 : 1252596 : vec<constructor_elt, va_gc> **p = nullptr;
6440 : 1252596 : if (ctx->ctor)
6441 : : {
6442 : 1252588 : p = &CONSTRUCTOR_ELTS (ctx->ctor);
6443 : 2505073 : vec_alloc (*p, vec_safe_length (v));
6444 : 1252588 : if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (t))
6445 : 16057 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor) = 1;
6446 : : }
6447 : :
6448 : 1252596 : unsigned i;
6449 : 1252596 : tree index, value;
6450 : 1252596 : bool constant_p = true;
6451 : 1252596 : bool side_effects_p = false;
6452 : 2583818 : FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value)
6453 : : {
6454 : 1632383 : tree orig_value = value;
6455 : 1632383 : init_subob_ctx (ctx, new_ctx, index, value);
6456 : : /* Like in cxx_eval_store_expression, omit entries for empty fields. */
6457 : 1632383 : bool no_slot = new_ctx.ctor == NULL_TREE;
6458 : 1632383 : int pos_hint = -1;
6459 : 1632383 : if (new_ctx.ctor != ctx->ctor && !no_slot)
6460 : : {
6461 : : /* If we built a new CONSTRUCTOR, attach it now so that other
6462 : : initializers can refer to it. */
6463 : 35366 : constructor_elt *cep = get_or_insert_ctor_field (ctx->ctor, index);
6464 : 35366 : cep->value = new_ctx.ctor;
6465 : 35366 : pos_hint = cep - (*p)->begin();
6466 : 35366 : }
6467 : 1597017 : else if (TREE_CODE (type) == UNION_TYPE)
6468 : : /* Otherwise if we're constructing a non-aggregate union member, set
6469 : : the active union member now so that we can later detect and diagnose
6470 : : if its initializer attempts to activate another member. */
6471 : 969 : get_or_insert_ctor_field (ctx->ctor, index);
6472 : 1632383 : tree elt = cxx_eval_constant_expression (&new_ctx, value,
6473 : : lval,
6474 : : non_constant_p, overflow_p,
6475 : : jump_target);
6476 : 1632383 : if (*jump_target)
6477 : : return NULL_TREE;
6478 : : /* Don't VERIFY_CONSTANT here. */
6479 : 1632383 : if (ctx->quiet && *non_constant_p)
6480 : : break;
6481 : 1331222 : if (elt != orig_value)
6482 : 137850 : changed = true;
6483 : :
6484 : 1331222 : if (!TREE_CONSTANT (elt))
6485 : 445903 : constant_p = false;
6486 : 1331222 : if (TREE_SIDE_EFFECTS (elt))
6487 : 114 : side_effects_p = true;
6488 : 1331222 : if (index && TREE_CODE (index) == COMPONENT_REF)
6489 : : {
6490 : : /* This is an initialization of a vfield inside a base
6491 : : subaggregate that we already initialized; push this
6492 : : initialization into the previous initialization. */
6493 : 0 : constructor_elt *inner = base_field_constructor_elt (*p, index);
6494 : 0 : inner->value = elt;
6495 : 0 : changed = true;
6496 : 0 : }
6497 : 1331222 : else if (no_slot)
6498 : : /* This is an initializer for an empty field; now that we've
6499 : : checked that it's constant, we can ignore it. */
6500 : : changed = true;
6501 : 625833 : else if (index
6502 : 625705 : && (TREE_CODE (index) == NOP_EXPR
6503 : 625705 : || TREE_CODE (index) == POINTER_PLUS_EXPR))
6504 : : {
6505 : : /* Old representation of empty bases. FIXME remove. */
6506 : 0 : gcc_checking_assert (false);
6507 : : gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index))));
6508 : : changed = true;
6509 : : }
6510 : : else
6511 : : {
6512 : 625833 : if (TREE_CODE (type) == UNION_TYPE
6513 : 625833 : && (*p)->last().index != index)
6514 : : /* The initializer erroneously changed the active union member that
6515 : : we're initializing. */
6516 : 8 : gcc_assert (*non_constant_p);
6517 : : else
6518 : : {
6519 : : /* The initializer might have mutated the underlying CONSTRUCTOR,
6520 : : so recompute the location of the target constructer_elt. */
6521 : 625825 : constructor_elt *cep
6522 : 625825 : = get_or_insert_ctor_field (ctx->ctor, index, pos_hint);
6523 : 625825 : cep->value = elt;
6524 : : }
6525 : :
6526 : : /* Adding or replacing an element might change the ctor's flags. */
6527 : 625833 : TREE_CONSTANT (ctx->ctor) = constant_p;
6528 : 625833 : TREE_SIDE_EFFECTS (ctx->ctor) = side_effects_p;
6529 : : }
6530 : : }
6531 : 1252596 : if (*non_constant_p)
6532 : : return t;
6533 : 951399 : if (!changed)
6534 : : {
6535 : 156449 : if (VECTOR_TYPE_P (type))
6536 : 13937 : t = fold (t);
6537 : 156449 : return t;
6538 : : }
6539 : 794950 : t = ctx->ctor;
6540 : 794950 : if (!t)
6541 : 8 : t = build_constructor (type, NULL);
6542 : : /* We're done building this CONSTRUCTOR, so now we can interpret an
6543 : : element without an explicit initializer as value-initialized. */
6544 : 794950 : CONSTRUCTOR_NO_CLEARING (t) = false;
6545 : 794950 : TREE_CONSTANT (t) = constant_p;
6546 : 794950 : TREE_SIDE_EFFECTS (t) = side_effects_p;
6547 : 794950 : if (VECTOR_TYPE_P (type))
6548 : 1070 : t = fold (t);
6549 : : return t;
6550 : : }
6551 : :
6552 : : /* Subroutine of cxx_eval_constant_expression.
6553 : : The expression tree T is a VEC_INIT_EXPR which denotes the desired
6554 : : initialization of a non-static data member of array type. Reduce it to a
6555 : : CONSTRUCTOR.
6556 : :
6557 : : Note that apart from value-initialization (when VALUE_INIT is true),
6558 : : this is only intended to support value-initialization and the
6559 : : initializations done by defaulted constructors for classes with
6560 : : non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
6561 : : will either be NULL_TREE for the default constructor, or a COMPONENT_REF
6562 : : for the copy/move constructor. */
6563 : :
6564 : : static tree
6565 : 606 : cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init,
6566 : : bool value_init, value_cat lval,
6567 : : bool *non_constant_p, bool *overflow_p,
6568 : : tree *jump_target)
6569 : : {
6570 : 606 : tree elttype = TREE_TYPE (atype);
6571 : 606 : verify_ctor_sanity (ctx, atype);
6572 : 606 : vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
6573 : 606 : bool pre_init = false;
6574 : 606 : unsigned HOST_WIDE_INT i;
6575 : 606 : tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
6576 : :
6577 : 606 : if (init && TREE_CODE (init) == CONSTRUCTOR)
6578 : 0 : return cxx_eval_bare_aggregate (ctx, init, lval,
6579 : 0 : non_constant_p, overflow_p, jump_target);
6580 : :
6581 : : /* For the default constructor, build up a call to the default
6582 : : constructor of the element type. We only need to handle class types
6583 : : here, as for a constructor to be constexpr, all members must be
6584 : : initialized, which for a defaulted default constructor means they must
6585 : : be of a class type with a constexpr default constructor. */
6586 : 606 : if (TREE_CODE (elttype) == ARRAY_TYPE)
6587 : : /* We only do this at the lowest level. */;
6588 : 562 : else if (value_init)
6589 : : {
6590 : 155 : init = build_value_init (elttype, complain);
6591 : 155 : pre_init = true;
6592 : : }
6593 : 407 : else if (!init)
6594 : : {
6595 : 322 : releasing_vec argvec;
6596 : 322 : init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
6597 : : &argvec, elttype, LOOKUP_NORMAL,
6598 : : complain);
6599 : 322 : init = build_aggr_init_expr (elttype, init);
6600 : 322 : pre_init = true;
6601 : 322 : }
6602 : :
6603 : 606 : bool zeroed_out = false;
6604 : 606 : if (!CONSTRUCTOR_NO_CLEARING (ctx->ctor))
6605 : : {
6606 : : /* We're initializing an array object that had been zero-initialized
6607 : : earlier. Truncate ctx->ctor, and propagate its zeroed state by
6608 : : clearing CONSTRUCTOR_NO_CLEARING on each of the aggregate element
6609 : : initializers we append to it. */
6610 : 156 : gcc_checking_assert (initializer_zerop (ctx->ctor));
6611 : 156 : zeroed_out = true;
6612 : 156 : vec_safe_truncate (*p, 0);
6613 : : }
6614 : :
6615 : 606 : tree nelts = get_array_or_vector_nelts (ctx, atype, non_constant_p,
6616 : : overflow_p, jump_target);
6617 : 606 : if (*jump_target)
6618 : : return NULL_TREE;
6619 : 606 : unsigned HOST_WIDE_INT max = tree_to_uhwi (nelts);
6620 : 6253 : for (i = 0; i < max; ++i)
6621 : : {
6622 : 6047 : tree idx = build_int_cst (size_type_node, i);
6623 : 6047 : tree eltinit;
6624 : 6047 : bool reuse = false;
6625 : 6047 : constexpr_ctx new_ctx;
6626 : 6382 : init_subob_ctx (ctx, new_ctx, idx, pre_init ? init : elttype);
6627 : 6047 : bool no_slot = new_ctx.ctor == NULL_TREE;
6628 : 6047 : if (new_ctx.ctor != ctx->ctor && !no_slot)
6629 : : {
6630 : 5867 : if (zeroed_out)
6631 : 5445 : CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = false;
6632 : 5867 : CONSTRUCTOR_APPEND_ELT (*p, idx, new_ctx.ctor);
6633 : : }
6634 : 6047 : if (TREE_CODE (elttype) == ARRAY_TYPE)
6635 : : {
6636 : : /* A multidimensional array; recurse. */
6637 : 176 : if (value_init || init == NULL_TREE)
6638 : : {
6639 : 168 : eltinit = NULL_TREE;
6640 : 168 : reuse = i == 0;
6641 : : }
6642 : : else
6643 : 8 : eltinit = cp_build_array_ref (input_location, init, idx, complain);
6644 : 176 : eltinit = cxx_eval_vec_init_1 (&new_ctx, elttype, eltinit,
6645 : : value_init, lval, non_constant_p,
6646 : : overflow_p, jump_target);
6647 : : }
6648 : 5871 : else if (pre_init)
6649 : : {
6650 : : /* Initializing an element using value or default initialization
6651 : : we just pre-built above. */
6652 : 5712 : if (init == void_node)
6653 : : /* Trivial default-init, don't do anything to the CONSTRUCTOR. */
6654 : 3 : return ctx->ctor;
6655 : 5709 : eltinit = init;
6656 : 5709 : if (CLASS_TYPE_P (elttype) && new_ctx.object)
6657 : : /* Clarify what object is being initialized (118285). */
6658 : 5572 : eltinit = build2 (INIT_EXPR, elttype, new_ctx.object, eltinit);
6659 : 5709 : eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval,
6660 : : non_constant_p, overflow_p,
6661 : : jump_target);
6662 : 5709 : reuse = i == 0;
6663 : : }
6664 : : else
6665 : : {
6666 : : /* Copying an element. */
6667 : 159 : eltinit = cp_build_array_ref (input_location, init, idx, complain);
6668 : 159 : if (!lvalue_p (init))
6669 : 132 : eltinit = move (eltinit);
6670 : 159 : eltinit = (perform_implicit_conversion_flags
6671 : 159 : (elttype, eltinit, complain,
6672 : : LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING));
6673 : 159 : if (CLASS_TYPE_P (elttype) && new_ctx.object)
6674 : : /* Clarify what object is being initialized (118285). */
6675 : 142 : eltinit = build2 (INIT_EXPR, elttype, new_ctx.object, eltinit);
6676 : 159 : eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval,
6677 : : non_constant_p, overflow_p,
6678 : : jump_target);
6679 : : }
6680 : 6044 : if (*jump_target)
6681 : : return NULL_TREE;
6682 : 6044 : if (*non_constant_p)
6683 : : break;
6684 : 5946 : if (no_slot)
6685 : : {
6686 : : /* This is an initializer for an empty subobject; now that we've
6687 : : checked that it's constant, we can ignore it. */
6688 : 18 : gcc_checking_assert (i == 0);
6689 : : break;
6690 : : }
6691 : 5928 : else if (new_ctx.ctor != ctx->ctor)
6692 : : {
6693 : : /* We appended this element above; update the value. */
6694 : 5797 : gcc_assert ((*p)->last().index == idx);
6695 : 5797 : (*p)->last().value = eltinit;
6696 : : }
6697 : : else
6698 : 131 : CONSTRUCTOR_APPEND_ELT (*p, idx, eltinit);
6699 : : /* Reuse the result of cxx_eval_constant_expression call
6700 : : from the first iteration to all others if it is a constant
6701 : : initializer that doesn't require relocations. */
6702 : 5928 : if (reuse
6703 : 5928 : && max > 1
6704 : 5928 : && (eltinit == NULL_TREE
6705 : 434 : || (initializer_constant_valid_p (eltinit, TREE_TYPE (eltinit))
6706 : 434 : == null_pointer_node)))
6707 : : {
6708 : 281 : if (new_ctx.ctor != ctx->ctor)
6709 : 156 : eltinit = new_ctx.ctor;
6710 : 281 : tree range = build2 (RANGE_EXPR, size_type_node,
6711 : : build_int_cst (size_type_node, 1),
6712 : 281 : build_int_cst (size_type_node, max - 1));
6713 : 281 : CONSTRUCTOR_APPEND_ELT (*p, range, unshare_constructor (eltinit));
6714 : 281 : break;
6715 : : }
6716 : 5647 : else if (i == 0)
6717 : 203 : vec_safe_reserve (*p, max);
6718 : : }
6719 : :
6720 : 603 : if (!*non_constant_p)
6721 : : {
6722 : 505 : init = ctx->ctor;
6723 : 505 : CONSTRUCTOR_NO_CLEARING (init) = false;
6724 : : }
6725 : 603 : return init;
6726 : : }
6727 : :
6728 : : static tree
6729 : 493 : cxx_eval_vec_init (const constexpr_ctx *ctx, tree t,
6730 : : value_cat lval,
6731 : : bool *non_constant_p, bool *overflow_p, tree *jump_target)
6732 : : {
6733 : 493 : tree atype = TREE_TYPE (t);
6734 : 493 : tree init = VEC_INIT_EXPR_INIT (t);
6735 : 493 : bool value_init = VEC_INIT_EXPR_VALUE_INIT (t);
6736 : 493 : if (!init || !BRACE_ENCLOSED_INITIALIZER_P (init))
6737 : : ;
6738 : 72 : else if (CONSTRUCTOR_NELTS (init) == 0
6739 : 72 : && !CP_AGGREGATE_TYPE_P (strip_array_types (atype)))
6740 : : {
6741 : : /* Handle {} as value-init. */
6742 : : init = NULL_TREE;
6743 : : value_init = true;
6744 : : }
6745 : : else
6746 : : {
6747 : : /* This is a more complicated case, like needing to loop over trailing
6748 : : elements; call build_vec_init and evaluate the result. */
6749 : 63 : tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
6750 : 63 : constexpr_ctx new_ctx = *ctx;
6751 : 63 : if (!ctx->object)
6752 : : {
6753 : : /* We want to have an initialization target for an VEC_INIT_EXPR.
6754 : : If we don't already have one in CTX, use the VEC_INIT_EXPR_SLOT. */
6755 : 51 : new_ctx.object = VEC_INIT_EXPR_SLOT (t);
6756 : 51 : tree ctor = new_ctx.ctor = build_constructor (atype, NULL);
6757 : 51 : CONSTRUCTOR_NO_CLEARING (ctor) = true;
6758 : 51 : ctx->global->put_value (new_ctx.object, ctor);
6759 : 51 : ctx = &new_ctx;
6760 : : }
6761 : 63 : init = expand_vec_init_expr (ctx->object, t, complain);
6762 : 63 : return cxx_eval_constant_expression (ctx, init, lval, non_constant_p,
6763 : : overflow_p, jump_target);
6764 : : }
6765 : 430 : tree r = cxx_eval_vec_init_1 (ctx, atype, init, value_init,
6766 : : lval, non_constant_p, overflow_p, jump_target);
6767 : 430 : if (*non_constant_p)
6768 : : return t;
6769 : : else
6770 : 351 : return r;
6771 : : }
6772 : :
6773 : : /* Like same_type_ignoring_top_level_qualifiers_p, but also handle the case
6774 : : where the desired type is an array of unknown bounds because the variable
6775 : : has had its bounds deduced since the wrapping expression was created. */
6776 : :
6777 : : static bool
6778 : 136566519 : same_type_ignoring_tlq_and_bounds_p (tree type1, tree type2)
6779 : : {
6780 : 136566519 : while (TREE_CODE (type1) == ARRAY_TYPE
6781 : 13295 : && TREE_CODE (type2) == ARRAY_TYPE
6782 : 136566523 : && (!TYPE_DOMAIN (type1) || !TYPE_DOMAIN (type2)))
6783 : : {
6784 : 2 : type1 = TREE_TYPE (type1);
6785 : 2 : type2 = TREE_TYPE (type2);
6786 : : }
6787 : 136566519 : return same_type_ignoring_top_level_qualifiers_p (type1, type2);
6788 : : }
6789 : :
6790 : : /* Try to determine the currently active union member for an expression
6791 : : with UNION_TYPE. If it can be determined, return the FIELD_DECL,
6792 : : otherwise return NULL_TREE. */
6793 : :
6794 : : static tree
6795 : 69 : cxx_union_active_member (const constexpr_ctx *ctx, tree t, tree *jump_target)
6796 : : {
6797 : 69 : constexpr_ctx new_ctx = *ctx;
6798 : 69 : new_ctx.quiet = true;
6799 : 69 : bool non_constant_p = false, overflow_p = false;
6800 : 69 : tree ctor = cxx_eval_constant_expression (&new_ctx, t, vc_prvalue,
6801 : : &non_constant_p,
6802 : : &overflow_p, jump_target);
6803 : 69 : if (*jump_target)
6804 : : return NULL_TREE;
6805 : 69 : if (TREE_CODE (ctor) == CONSTRUCTOR
6806 : 24 : && CONSTRUCTOR_NELTS (ctor) == 1
6807 : 12 : && CONSTRUCTOR_ELT (ctor, 0)->index
6808 : 81 : && TREE_CODE (CONSTRUCTOR_ELT (ctor, 0)->index) == FIELD_DECL)
6809 : : return CONSTRUCTOR_ELT (ctor, 0)->index;
6810 : : return NULL_TREE;
6811 : : }
6812 : :
6813 : : /* Helper function for cxx_fold_indirect_ref_1, called recursively. */
6814 : :
6815 : : static tree
6816 : 3346002 : cxx_fold_indirect_ref_1 (const constexpr_ctx *ctx, location_t loc, tree type,
6817 : : tree op, unsigned HOST_WIDE_INT off, bool *empty_base,
6818 : : tree *jump_target)
6819 : : {
6820 : 5306384 : tree optype = TREE_TYPE (op);
6821 : 5306384 : unsigned HOST_WIDE_INT const_nunits;
6822 : 5306384 : if (off == 0 && similar_type_p (optype, type))
6823 : : return op;
6824 : 3342240 : else if (cxx_dialect >= cxx26
6825 : 1871407 : && VAR_P (op)
6826 : 644988 : && DECL_VTABLE_OR_VTT_P (op)
6827 : 11817 : && same_type_ignoring_top_level_qualifiers_p (type,
6828 : : ptrdiff_type_node)
6829 : 3343222 : && POINTER_TYPE_P (strip_array_types (optype)))
6830 : : {
6831 : : /* We often read some virtual table elements using ptrdiff_t rather
6832 : : than pointer type. */
6833 : 982 : if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc,
6834 : : strip_array_types (optype),
6835 : : op, off, empty_base,
6836 : : jump_target))
6837 : 982 : return fold_convert (type, ret);
6838 : : }
6839 : 3341258 : else if (TREE_CODE (optype) == COMPLEX_TYPE
6840 : 3341258 : && similar_type_p (type, TREE_TYPE (optype)))
6841 : : {
6842 : : /* *(foo *)&complexfoo => __real__ complexfoo */
6843 : 0 : if (off == 0)
6844 : 0 : return build1_loc (loc, REALPART_EXPR, type, op);
6845 : : /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
6846 : 0 : else if (tree_to_uhwi (TYPE_SIZE_UNIT (type)) == off)
6847 : 0 : return build1_loc (loc, IMAGPART_EXPR, type, op);
6848 : : }
6849 : : /* ((foo*)&vectorfoo)[x] => BIT_FIELD_REF<vectorfoo,...> */
6850 : 3341258 : else if (VECTOR_TYPE_P (optype)
6851 : 0 : && similar_type_p (type, TREE_TYPE (optype))
6852 : 3341258 : && TYPE_VECTOR_SUBPARTS (optype).is_constant (&const_nunits))
6853 : : {
6854 : 0 : unsigned HOST_WIDE_INT part_width = tree_to_uhwi (TYPE_SIZE_UNIT (type));
6855 : 0 : unsigned HOST_WIDE_INT max_offset = part_width * const_nunits;
6856 : 0 : if (off < max_offset && off % part_width == 0)
6857 : : {
6858 : 0 : tree index = bitsize_int (off * BITS_PER_UNIT);
6859 : 0 : return build3_loc (loc, BIT_FIELD_REF, type, op,
6860 : 0 : TYPE_SIZE (type), index);
6861 : : }
6862 : : }
6863 : : /* ((foo *)&fooarray)[x] => fooarray[x] */
6864 : 3341258 : else if (TREE_CODE (optype) == ARRAY_TYPE
6865 : 1960382 : && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (optype)))
6866 : 5301640 : && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (optype))))
6867 : : {
6868 : 1960382 : tree type_domain = TYPE_DOMAIN (optype);
6869 : 1960382 : tree min_val = size_zero_node;
6870 : 1960382 : if (type_domain && TYPE_MIN_VALUE (type_domain))
6871 : 1960377 : min_val = TYPE_MIN_VALUE (type_domain);
6872 : 1960382 : unsigned HOST_WIDE_INT el_sz
6873 : 1960382 : = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (optype)));
6874 : 1960382 : unsigned HOST_WIDE_INT idx = off / el_sz;
6875 : 1960382 : unsigned HOST_WIDE_INT rem = off % el_sz;
6876 : 1960382 : if (tree_fits_uhwi_p (min_val))
6877 : : {
6878 : 1960382 : tree index = size_int (idx + tree_to_uhwi (min_val));
6879 : 1960382 : op = build4_loc (loc, ARRAY_REF, TREE_TYPE (optype), op, index,
6880 : : NULL_TREE, NULL_TREE);
6881 : 1960382 : return cxx_fold_indirect_ref_1 (ctx, loc, type, op, rem,
6882 : 1960382 : empty_base, jump_target);
6883 : : }
6884 : : }
6885 : : /* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */
6886 : 1380876 : else if (TREE_CODE (optype) == RECORD_TYPE
6887 : 1380876 : || TREE_CODE (optype) == UNION_TYPE)
6888 : : {
6889 : 1377523 : if (TREE_CODE (optype) == UNION_TYPE)
6890 : : /* For unions prefer the currently active member. */
6891 : 69 : if (tree field = cxx_union_active_member (ctx, op, jump_target))
6892 : : {
6893 : 12 : unsigned HOST_WIDE_INT el_sz
6894 : 12 : = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field)));
6895 : 12 : if (off < el_sz)
6896 : : {
6897 : 12 : tree cop = build3 (COMPONENT_REF, TREE_TYPE (field),
6898 : : op, field, NULL_TREE);
6899 : 12 : if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop,
6900 : : off, empty_base,
6901 : : jump_target))
6902 : : return ret;
6903 : : }
6904 : : }
6905 : :
6906 : : /* Handle conversion to "as base" type. */
6907 : 1377517 : if (CLASS_TYPE_P (optype)
6908 : 2754946 : && CLASSTYPE_AS_BASE (optype) == type)
6909 : : return op;
6910 : :
6911 : : /* Handle conversion to an empty base class, which is represented with a
6912 : : NOP_EXPR. Do this before spelunking into the non-empty subobjects,
6913 : : which is likely to be a waste of time (109678). */
6914 : 1376947 : if (is_empty_class (type)
6915 : 1350560 : && CLASS_TYPE_P (optype)
6916 : 2727501 : && lookup_base (optype, type, ba_any, NULL, tf_none, off))
6917 : : {
6918 : 791292 : if (empty_base)
6919 : 791292 : *empty_base = true;
6920 : 791292 : return op;
6921 : : }
6922 : :
6923 : 585655 : for (tree field = TYPE_FIELDS (optype);
6924 : 5363261 : field; field = DECL_CHAIN (field))
6925 : 5360902 : if (TREE_CODE (field) == FIELD_DECL
6926 : 591517 : && TREE_TYPE (field) != error_mark_node
6927 : 5952419 : && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (field))))
6928 : : {
6929 : 591517 : tree pos = byte_position (field);
6930 : 591517 : if (!tree_fits_uhwi_p (pos))
6931 : 0 : continue;
6932 : 591517 : unsigned HOST_WIDE_INT upos = tree_to_uhwi (pos);
6933 : 591517 : unsigned HOST_WIDE_INT el_sz;
6934 : 591517 : if (DECL_FIELD_IS_BASE (field)
6935 : 30724 : && CLASS_TYPE_P (optype)
6936 : 622241 : && CLASSTYPE_VBASECLASSES (optype))
6937 : 6189 : el_sz = tree_to_uhwi (DECL_SIZE_UNIT (field));
6938 : : else
6939 : 585328 : el_sz = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field)));
6940 : 591517 : if (upos <= off && off < upos + el_sz)
6941 : : {
6942 : 585559 : tree cop = build3 (COMPONENT_REF, TREE_TYPE (field),
6943 : : op, field, NULL_TREE);
6944 : 585559 : if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop,
6945 : : off - upos,
6946 : : empty_base,
6947 : : jump_target))
6948 : : return ret;
6949 : : }
6950 : : }
6951 : : }
6952 : :
6953 : : return NULL_TREE;
6954 : : }
6955 : :
6956 : : /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
6957 : : match. We want to be less strict for simple *& folding; if we have a
6958 : : non-const temporary that we access through a const pointer, that should
6959 : : work. We handle this here rather than change fold_indirect_ref_1
6960 : : because we're dealing with things like ADDR_EXPR of INTEGER_CST which
6961 : : don't really make sense outside of constant expression evaluation. Also
6962 : : we want to allow folding to COMPONENT_REF, which could cause trouble
6963 : : with TBAA in fold_indirect_ref_1. */
6964 : :
6965 : : static tree
6966 : 55104914 : cxx_fold_indirect_ref (const constexpr_ctx *ctx, location_t loc, tree type,
6967 : : tree op0, bool *empty_base, tree *jump_target)
6968 : : {
6969 : 55104914 : tree sub = op0;
6970 : 55104914 : tree subtype;
6971 : :
6972 : : /* STRIP_NOPS, but stop if REINTERPRET_CAST_P. */
6973 : 115167360 : while (CONVERT_EXPR_P (sub) || TREE_CODE (sub) == NON_LVALUE_EXPR
6974 : 154775433 : || TREE_CODE (sub) == VIEW_CONVERT_EXPR)
6975 : : {
6976 : 42959152 : if (TREE_CODE (sub) == NOP_EXPR
6977 : 42959152 : && REINTERPRET_CAST_P (sub))
6978 : : return NULL_TREE;
6979 : 42959152 : sub = TREE_OPERAND (sub, 0);
6980 : : }
6981 : :
6982 : 55104914 : subtype = TREE_TYPE (sub);
6983 : 55104914 : if (!INDIRECT_TYPE_P (subtype))
6984 : : return NULL_TREE;
6985 : :
6986 : : /* Canonicalizes the given OBJ/OFF pair by iteratively absorbing
6987 : : the innermost component into the offset until it would make the
6988 : : offset positive, so that cxx_fold_indirect_ref_1 can identify
6989 : : more folding opportunities. */
6990 : 57864311 : auto canonicalize_obj_off = [] (tree& obj, tree& off) {
6991 : 2759449 : if (cxx_dialect >= cxx26)
6992 : : {
6993 : : /* For C++26, we need to fold *(B *)(&x.D.1234 + 32) used
6994 : : to access virtual base members. */
6995 : 1515677 : tree nobj = obj;
6996 : 1515677 : while (TREE_CODE (nobj) == COMPONENT_REF
6997 : 1528508 : && DECL_FIELD_IS_BASE (TREE_OPERAND (nobj, 1)))
6998 : 12831 : nobj = TREE_OPERAND (nobj, 0);
6999 : 1515677 : if (nobj != obj
7000 : 12375 : && CLASS_TYPE_P (TREE_TYPE (nobj))
7001 : 1528052 : && CLASSTYPE_VBASECLASSES (TREE_TYPE (nobj)))
7002 : 2039 : while (obj != nobj)
7003 : : {
7004 : 1174 : tree field = TREE_OPERAND (obj, 1);
7005 : 1174 : tree pos = byte_position (field);
7006 : 1174 : off = int_const_binop (PLUS_EXPR, off, pos);
7007 : 1174 : obj = TREE_OPERAND (obj, 0);
7008 : : }
7009 : : }
7010 : 3349856 : while (TREE_CODE (obj) == COMPONENT_REF
7011 : : /* We need to preserve union member accesses so that we can
7012 : : later properly diagnose accessing the wrong member. */
7013 : 1358732 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (obj, 0))) == RECORD_TYPE
7014 : 4625906 : && (tree_int_cst_sign_bit (off) || integer_zerop (off)))
7015 : : {
7016 : 592385 : tree field = TREE_OPERAND (obj, 1);
7017 : 592385 : tree pos = byte_position (field);
7018 : 592385 : if (integer_zerop (off) && integer_nonzerop (pos))
7019 : : /* If the offset is already 0, keep going as long as the
7020 : : component is at position 0. */
7021 : : break;
7022 : 590407 : off = int_const_binop (PLUS_EXPR, off, pos);
7023 : 590407 : obj = TREE_OPERAND (obj, 0);
7024 : : }
7025 : 2759449 : };
7026 : :
7027 : 55104862 : if (TREE_CODE (sub) == ADDR_EXPR)
7028 : : {
7029 : 23074829 : tree op = TREE_OPERAND (sub, 0);
7030 : 23074829 : tree optype = TREE_TYPE (op);
7031 : :
7032 : : /* *&CONST_DECL -> to the value of the const decl. */
7033 : 23074829 : if (TREE_CODE (op) == CONST_DECL)
7034 : 0 : return DECL_INITIAL (op);
7035 : : /* *&p => p; make sure to handle *&"str"[cst] here. */
7036 : 23074829 : if (similar_type_p (optype, type))
7037 : : {
7038 : 21977316 : tree fop = fold_read_from_constant_string (op);
7039 : 21977316 : if (fop)
7040 : : return fop;
7041 : : else
7042 : 21967224 : return op;
7043 : : }
7044 : : else
7045 : : {
7046 : 1097513 : tree off = integer_zero_node;
7047 : 1097513 : canonicalize_obj_off (op, off);
7048 : 1097513 : return cxx_fold_indirect_ref_1 (ctx, loc, type, op,
7049 : : tree_to_uhwi (off), empty_base,
7050 : : jump_target);
7051 : : }
7052 : : }
7053 : 32030033 : else if (TREE_CODE (sub) == POINTER_PLUS_EXPR
7054 : 32030033 : && tree_fits_uhwi_p (TREE_OPERAND (sub, 1)))
7055 : : {
7056 : 1806214 : tree op00 = TREE_OPERAND (sub, 0);
7057 : 1806214 : tree off = TREE_OPERAND (sub, 1);
7058 : :
7059 : 1806214 : STRIP_NOPS (op00);
7060 : 1806214 : if (TREE_CODE (op00) == ADDR_EXPR)
7061 : : {
7062 : 1661936 : tree obj = TREE_OPERAND (op00, 0);
7063 : 1661936 : canonicalize_obj_off (obj, off);
7064 : 1661936 : return cxx_fold_indirect_ref_1 (ctx, loc, type, obj,
7065 : : tree_to_uhwi (off), empty_base,
7066 : : jump_target);
7067 : : }
7068 : : }
7069 : : /* *(foo *)fooarrptr => (*fooarrptr)[0] */
7070 : 30223819 : else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
7071 : 30223819 : && similar_type_p (type, TREE_TYPE (TREE_TYPE (subtype))))
7072 : : {
7073 : 0 : tree type_domain;
7074 : 0 : tree min_val = size_zero_node;
7075 : 0 : tree newsub
7076 : 0 : = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (subtype), sub, NULL,
7077 : : jump_target);
7078 : 0 : if (*jump_target)
7079 : : return NULL_TREE;
7080 : 0 : if (newsub)
7081 : : sub = newsub;
7082 : : else
7083 : 0 : sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub);
7084 : 0 : type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
7085 : 0 : if (type_domain && TYPE_MIN_VALUE (type_domain))
7086 : 0 : min_val = TYPE_MIN_VALUE (type_domain);
7087 : 0 : return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
7088 : 0 : NULL_TREE);
7089 : : }
7090 : :
7091 : : return NULL_TREE;
7092 : : }
7093 : :
7094 : : static tree
7095 : 31475212 : cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t,
7096 : : value_cat lval,
7097 : : bool *non_constant_p, bool *overflow_p,
7098 : : tree *jump_target)
7099 : : {
7100 : 31475212 : tree orig_op0 = TREE_OPERAND (t, 0);
7101 : 31475212 : bool empty_base = false;
7102 : :
7103 : : /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second
7104 : : operand is an integer-zero. Otherwise reject the MEM_REF for now. */
7105 : :
7106 : 31475212 : if (TREE_CODE (t) == MEM_REF
7107 : 31475212 : && (!TREE_OPERAND (t, 1) || !integer_zerop (TREE_OPERAND (t, 1))))
7108 : : {
7109 : 9 : gcc_assert (ctx->quiet);
7110 : 9 : *non_constant_p = true;
7111 : 9 : return t;
7112 : : }
7113 : :
7114 : : /* First try to simplify it directly. */
7115 : 31475203 : tree r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t),
7116 : : orig_op0, &empty_base, jump_target);
7117 : 31475203 : if (*jump_target)
7118 : : return NULL_TREE;
7119 : 31475203 : if (!r)
7120 : : {
7121 : : /* If that didn't work, evaluate the operand first. */
7122 : 30166092 : tree op0 = cxx_eval_constant_expression (ctx, orig_op0,
7123 : : vc_prvalue, non_constant_p,
7124 : : overflow_p, jump_target);
7125 : 30166092 : if (*jump_target)
7126 : : return NULL_TREE;
7127 : : /* Don't VERIFY_CONSTANT here. */
7128 : 30166086 : if (*non_constant_p)
7129 : : return t;
7130 : :
7131 : 19047205 : if (!lval && integer_zerop (op0))
7132 : : {
7133 : 68 : if (!ctx->quiet)
7134 : 12 : error ("dereferencing a null pointer");
7135 : 68 : *non_constant_p = true;
7136 : 68 : return t;
7137 : : }
7138 : :
7139 : 19047137 : r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t), op0,
7140 : : &empty_base, jump_target);
7141 : 19047137 : if (*jump_target)
7142 : : return NULL_TREE;
7143 : 19047137 : if (r == NULL_TREE)
7144 : : {
7145 : : /* We couldn't fold to a constant value. Make sure it's not
7146 : : something we should have been able to fold. */
7147 : 205471 : tree sub = op0;
7148 : 205471 : STRIP_NOPS (sub);
7149 : 205471 : if (TREE_CODE (sub) == ADDR_EXPR)
7150 : : {
7151 : 1639 : gcc_assert (!similar_type_p
7152 : : (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t)));
7153 : : /* DR 1188 says we don't have to deal with this. */
7154 : 1639 : if (!ctx->quiet)
7155 : 5 : error_at (cp_expr_loc_or_input_loc (t),
7156 : : "accessing value of %qE through a %qT glvalue in a "
7157 : : "constant expression", build_fold_indirect_ref (sub),
7158 : 4 : TREE_TYPE (t));
7159 : 1639 : *non_constant_p = true;
7160 : 1639 : return t;
7161 : : }
7162 : :
7163 : 203832 : if (lval == vc_glvalue && op0 != orig_op0)
7164 : 18197 : return build1 (INDIRECT_REF, TREE_TYPE (t), op0);
7165 : 185635 : if (!lval)
7166 : 130456 : VERIFY_CONSTANT (t);
7167 : 185635 : return t;
7168 : : }
7169 : : }
7170 : :
7171 : 20150777 : r = cxx_eval_constant_expression (ctx, r,
7172 : : lval, non_constant_p, overflow_p,
7173 : : jump_target);
7174 : 20150777 : if (*jump_target)
7175 : : return NULL_TREE;
7176 : 20150777 : if (*non_constant_p)
7177 : : return t;
7178 : :
7179 : : /* If we're pulling out the value of an empty base, just return an empty
7180 : : CONSTRUCTOR. */
7181 : 14596596 : if (empty_base && !lval)
7182 : : {
7183 : 13284 : r = build_constructor (TREE_TYPE (t), NULL);
7184 : 13284 : TREE_CONSTANT (r) = true;
7185 : : }
7186 : :
7187 : : return r;
7188 : : }
7189 : :
7190 : : /* Complain about R, a DECL that is accessed outside its lifetime. */
7191 : :
7192 : : static void
7193 : 34 : outside_lifetime_error (location_t loc, tree r)
7194 : : {
7195 : 34 : auto_diagnostic_group d;
7196 : 34 : if (DECL_NAME (r) == heap_deleted_identifier)
7197 : : {
7198 : : /* Provide a more accurate message for deleted variables. */
7199 : 6 : error_at (loc, "use of allocated storage after deallocation "
7200 : : "in a constant expression");
7201 : 6 : inform (DECL_SOURCE_LOCATION (r), "allocated here");
7202 : : }
7203 : : else
7204 : : {
7205 : 28 : error_at (loc, "accessing %qE outside its lifetime", r);
7206 : 28 : inform (DECL_SOURCE_LOCATION (r), "declared here");
7207 : : }
7208 : 34 : }
7209 : :
7210 : : /* Complain about R, a VAR_DECL, not being usable in a constant expression.
7211 : : FUNDEF_P is true if we're checking a constexpr function body.
7212 : : Shared between potential_constant_expression and
7213 : : cxx_eval_constant_expression. */
7214 : :
7215 : : static void
7216 : 312 : non_const_var_error (location_t loc, tree r, bool fundef_p)
7217 : : {
7218 : 312 : auto_diagnostic_group d;
7219 : 312 : tree type = TREE_TYPE (r);
7220 : 312 : if (DECL_NAME (r) == heap_uninit_identifier
7221 : 312 : || DECL_NAME (r) == heap_identifier
7222 : 309 : || DECL_NAME (r) == heap_vec_uninit_identifier
7223 : 621 : || DECL_NAME (r) == heap_vec_identifier)
7224 : : {
7225 : 3 : if (constexpr_error (loc, fundef_p, "the content of uninitialized "
7226 : : "storage is not usable in a constant expression"))
7227 : 3 : inform (DECL_SOURCE_LOCATION (r), "allocated here");
7228 : 3 : return;
7229 : : }
7230 : 309 : if (DECL_NAME (r) == heap_deleted_identifier)
7231 : : {
7232 : 0 : if (constexpr_error (loc, fundef_p, "use of allocated storage after "
7233 : : "deallocation in a constant expression"))
7234 : 0 : inform (DECL_SOURCE_LOCATION (r), "allocated here");
7235 : 0 : return;
7236 : : }
7237 : 309 : if (!constexpr_error (loc, fundef_p, "the value of %qD is not usable in "
7238 : : "a constant expression", r))
7239 : : return;
7240 : : /* Avoid error cascade. */
7241 : 308 : if (DECL_INITIAL (r) == error_mark_node)
7242 : : return;
7243 : 294 : if (DECL_DECLARED_CONSTEXPR_P (r))
7244 : 3 : inform (DECL_SOURCE_LOCATION (r),
7245 : : "%qD used in its own initializer", r);
7246 : 291 : else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
7247 : : {
7248 : 216 : if (!CP_TYPE_CONST_P (type))
7249 : 187 : inform (DECL_SOURCE_LOCATION (r),
7250 : : "%q#D is not const", r);
7251 : 29 : else if (CP_TYPE_VOLATILE_P (type))
7252 : 0 : inform (DECL_SOURCE_LOCATION (r),
7253 : : "%q#D is volatile", r);
7254 : 29 : else if (!DECL_INITIAL (r)
7255 : 9 : || !TREE_CONSTANT (DECL_INITIAL (r))
7256 : 37 : || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r))
7257 : 29 : inform (DECL_SOURCE_LOCATION (r),
7258 : : "%qD was not initialized with a constant "
7259 : : "expression", r);
7260 : : else
7261 : 0 : gcc_unreachable ();
7262 : : }
7263 : 75 : else if (TYPE_REF_P (type))
7264 : 9 : inform (DECL_SOURCE_LOCATION (r),
7265 : : "%qD was not initialized with a constant "
7266 : : "expression", r);
7267 : : else
7268 : : {
7269 : 66 : if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r))
7270 : 66 : inform (DECL_SOURCE_LOCATION (r),
7271 : : "%qD was not declared %<constexpr%>", r);
7272 : : else
7273 : 0 : inform (DECL_SOURCE_LOCATION (r),
7274 : : "%qD does not have integral or enumeration type",
7275 : : r);
7276 : : }
7277 : 312 : }
7278 : :
7279 : : /* Subroutine of cxx_eval_constant_expression.
7280 : : Like cxx_eval_unary_expression, except for trinary expressions. */
7281 : :
7282 : : static tree
7283 : 16 : cxx_eval_trinary_expression (const constexpr_ctx *ctx, tree t,
7284 : : value_cat lval,
7285 : : bool *non_constant_p, bool *overflow_p,
7286 : : tree *jump_target)
7287 : : {
7288 : 16 : int i;
7289 : 16 : tree args[3];
7290 : 16 : tree val;
7291 : :
7292 : 37 : for (i = 0; i < 3; i++)
7293 : : {
7294 : 30 : args[i] = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, i),
7295 : : lval,
7296 : : non_constant_p, overflow_p,
7297 : : jump_target);
7298 : 30 : if (*jump_target)
7299 : : return NULL_TREE;
7300 : 30 : VERIFY_CONSTANT (args[i]);
7301 : : }
7302 : :
7303 : 7 : val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t),
7304 : : args[0], args[1], args[2]);
7305 : 7 : if (val == NULL_TREE)
7306 : : return t;
7307 : 7 : VERIFY_CONSTANT (val);
7308 : : return val;
7309 : : }
7310 : :
7311 : : /* True if T was declared in a function declared to be constexpr, and
7312 : : therefore potentially constant in C++14. */
7313 : :
7314 : : bool
7315 : 78752222 : var_in_constexpr_fn (tree t)
7316 : : {
7317 : 78752222 : tree ctx = DECL_CONTEXT (t);
7318 : 78752222 : return (ctx && TREE_CODE (ctx) == FUNCTION_DECL
7319 : 149980146 : && DECL_DECLARED_CONSTEXPR_P (ctx));
7320 : : }
7321 : :
7322 : : /* True if a function might be constexpr: either a function that was
7323 : : declared constexpr, or a C++17 lambda op(). */
7324 : :
7325 : : bool
7326 : 427965371 : maybe_constexpr_fn (tree t)
7327 : : {
7328 : 427965371 : return (DECL_DECLARED_CONSTEXPR_P (t)
7329 : 194074121 : || (cxx_dialect >= cxx17 && LAMBDA_FUNCTION_P (t))
7330 : 617371145 : || (flag_implicit_constexpr
7331 : 66 : && DECL_DECLARED_INLINE_P (STRIP_TEMPLATE (t))));
7332 : : }
7333 : :
7334 : : /* True if T was declared in a function that might be constexpr: either a
7335 : : function that was declared constexpr, or a C++17 lambda op(). */
7336 : :
7337 : : bool
7338 : 51687595 : var_in_maybe_constexpr_fn (tree t)
7339 : : {
7340 : 103374628 : return (DECL_FUNCTION_SCOPE_P (t)
7341 : 96407296 : && maybe_constexpr_fn (DECL_CONTEXT (t)));
7342 : : }
7343 : :
7344 : : /* We're assigning INIT to TARGET. In do_build_copy_constructor and
7345 : : build_over_call we implement trivial copy of a class with tail padding using
7346 : : assignment of character arrays, which is valid in normal code, but not in
7347 : : constexpr evaluation. We don't need to worry about clobbering tail padding
7348 : : in constexpr evaluation, so strip the type punning. */
7349 : :
7350 : : static void
7351 : 29416507 : maybe_simplify_trivial_copy (tree &target, tree &init)
7352 : : {
7353 : 29416507 : if (TREE_CODE (target) == MEM_REF
7354 : 2210 : && TREE_CODE (init) == MEM_REF
7355 : 2172 : && TREE_TYPE (target) == TREE_TYPE (init)
7356 : 2172 : && TREE_CODE (TREE_TYPE (target)) == ARRAY_TYPE
7357 : 29418679 : && TREE_TYPE (TREE_TYPE (target)) == unsigned_char_type_node)
7358 : : {
7359 : 2172 : target = build_fold_indirect_ref (TREE_OPERAND (target, 0));
7360 : 2172 : init = build_fold_indirect_ref (TREE_OPERAND (init, 0));
7361 : : }
7362 : 29416507 : }
7363 : :
7364 : : /* Returns true if REF, which is a COMPONENT_REF, has any fields
7365 : : of constant type. This does not check for 'mutable', so the
7366 : : caller is expected to be mindful of that. */
7367 : :
7368 : : static bool
7369 : 277 : cref_has_const_field (tree ref)
7370 : : {
7371 : 346 : while (TREE_CODE (ref) == COMPONENT_REF)
7372 : : {
7373 : 337 : if (CP_TYPE_CONST_P (TREE_TYPE (TREE_OPERAND (ref, 1))))
7374 : : return true;
7375 : 69 : ref = TREE_OPERAND (ref, 0);
7376 : : }
7377 : : return false;
7378 : : }
7379 : :
7380 : : /* Return true if we are modifying something that is const during constant
7381 : : expression evaluation. CODE is the code of the statement, OBJ is the
7382 : : object in question, MUTABLE_P is true if one of the subobjects were
7383 : : declared mutable. */
7384 : :
7385 : : static bool
7386 : 26516061 : modifying_const_object_p (tree_code code, tree obj, bool mutable_p)
7387 : : {
7388 : : /* If this is initialization, there's no problem. */
7389 : 26516061 : if (code != MODIFY_EXPR)
7390 : : return false;
7391 : :
7392 : : /* [basic.type.qualifier] "A const object is an object of type
7393 : : const T or a non-mutable subobject of a const object." */
7394 : 7691096 : if (mutable_p)
7395 : : return false;
7396 : :
7397 : 7690360 : if (TREE_READONLY (obj))
7398 : : return true;
7399 : :
7400 : 7688510 : if (CP_TYPE_CONST_P (TREE_TYPE (obj)))
7401 : : {
7402 : : /* Although a COMPONENT_REF may have a const type, we should
7403 : : only consider it modifying a const object when any of the
7404 : : field components is const. This can happen when using
7405 : : constructs such as const_cast<const T &>(m), making something
7406 : : const even though it wasn't declared const. */
7407 : 3841 : if (TREE_CODE (obj) == COMPONENT_REF)
7408 : 277 : return cref_has_const_field (obj);
7409 : : else
7410 : : return true;
7411 : : }
7412 : :
7413 : : return false;
7414 : : }
7415 : :
7416 : : /* Evaluate an INIT_EXPR or MODIFY_EXPR. */
7417 : :
7418 : : static tree
7419 : 32294991 : cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
7420 : : value_cat lval,
7421 : : bool *non_constant_p, bool *overflow_p,
7422 : : tree *jump_target)
7423 : : {
7424 : 32294991 : constexpr_ctx new_ctx = *ctx;
7425 : :
7426 : 32294991 : tree init = TREE_OPERAND (t, 1);
7427 : :
7428 : 3063209 : if (TREE_CLOBBER_P (init)
7429 : 35205182 : && CLOBBER_KIND (init) < CLOBBER_OBJECT_END)
7430 : : /* Only handle clobbers ending the lifetime of objects.
7431 : : ??? We should probably set CONSTRUCTOR_NO_CLEARING. */
7432 : 2878484 : return void_node;
7433 : :
7434 : : /* First we figure out where we're storing to. */
7435 : 29416507 : tree target = TREE_OPERAND (t, 0);
7436 : :
7437 : 29416507 : maybe_simplify_trivial_copy (target, init);
7438 : :
7439 : 29416507 : tree type = TREE_TYPE (target);
7440 : 29416507 : bool preeval = SCALAR_TYPE_P (type) || TREE_CODE (t) == MODIFY_EXPR;
7441 : 23026341 : if (preeval && !TREE_CLOBBER_P (init))
7442 : : {
7443 : : /* Evaluate the value to be stored without knowing what object it will be
7444 : : stored in, so that any side-effects happen first. */
7445 : 22994634 : if (!SCALAR_TYPE_P (type))
7446 : 48321 : new_ctx.ctor = new_ctx.object = NULL_TREE;
7447 : 22994634 : init = cxx_eval_constant_expression (&new_ctx, init, vc_prvalue,
7448 : : non_constant_p, overflow_p,
7449 : : jump_target);
7450 : 22994634 : if (*jump_target)
7451 : : return NULL_TREE;
7452 : 22994555 : if (*non_constant_p)
7453 : : return t;
7454 : : }
7455 : :
7456 : 23910404 : bool evaluated = false;
7457 : 23910404 : if (lval == vc_glvalue)
7458 : : {
7459 : : /* If we want to return a reference to the target, we need to evaluate it
7460 : : as a whole; otherwise, only evaluate the innermost piece to avoid
7461 : : building up unnecessary *_REFs. */
7462 : 0 : target = cxx_eval_constant_expression (ctx, target, lval,
7463 : : non_constant_p, overflow_p,
7464 : : jump_target);
7465 : 0 : evaluated = true;
7466 : 0 : if (*jump_target)
7467 : : return NULL_TREE;
7468 : 0 : if (*non_constant_p)
7469 : : return t;
7470 : : }
7471 : :
7472 : : /* Find the underlying variable. */
7473 : 23910404 : releasing_vec refs;
7474 : 23910404 : tree object = NULL_TREE;
7475 : : /* If we're modifying a const object, save it. */
7476 : 23910404 : tree const_object_being_modified = NULL_TREE;
7477 : 23910404 : bool mutable_p = false;
7478 : 80006782 : for (tree probe = target; object == NULL_TREE; )
7479 : : {
7480 : 56096639 : switch (TREE_CODE (probe))
7481 : : {
7482 : 8276084 : case BIT_FIELD_REF:
7483 : 8276084 : case COMPONENT_REF:
7484 : 8276084 : case ARRAY_REF:
7485 : 8276084 : {
7486 : 8276084 : tree ob = TREE_OPERAND (probe, 0);
7487 : 8276084 : tree elt = TREE_OPERAND (probe, 1);
7488 : 8276084 : if (TREE_CODE (elt) == FIELD_DECL && DECL_MUTABLE_P (elt))
7489 : : mutable_p = true;
7490 : 8276084 : if (TREE_CODE (probe) == ARRAY_REF)
7491 : : {
7492 : 1138624 : elt = eval_and_check_array_index (ctx, probe, false,
7493 : : non_constant_p, overflow_p,
7494 : : jump_target);
7495 : 1138624 : if (*jump_target)
7496 : 58 : return NULL_TREE;
7497 : 1138624 : if (*non_constant_p)
7498 : : return t;
7499 : : }
7500 : : /* We don't check modifying_const_object_p for ARRAY_REFs. Given
7501 : : "int a[10]", an ARRAY_REF "a[2]" can be "const int", even though
7502 : : the array isn't const. Instead, check "a" in the next iteration;
7503 : : that will detect modifying "const int a[10]". */
7504 : 7137460 : else if (evaluated
7505 : 2605918 : && modifying_const_object_p (TREE_CODE (t), probe,
7506 : : mutable_p)
7507 : 7137728 : && const_object_being_modified == NULL_TREE)
7508 : : const_object_being_modified = probe;
7509 : :
7510 : : /* Track named member accesses for unions to validate modifications
7511 : : that change active member. */
7512 : 8276026 : if (!evaluated && TREE_CODE (probe) == COMPONENT_REF)
7513 : 4531542 : vec_safe_push (refs, probe);
7514 : : else
7515 : 3744484 : vec_safe_push (refs, NULL_TREE);
7516 : :
7517 : 8276026 : vec_safe_push (refs, elt);
7518 : 8276026 : vec_safe_push (refs, TREE_TYPE (probe));
7519 : 8276026 : probe = ob;
7520 : : }
7521 : 8276026 : break;
7522 : :
7523 : 15 : case REALPART_EXPR:
7524 : 15 : gcc_assert (refs->is_empty ());
7525 : 15 : vec_safe_push (refs, NULL_TREE);
7526 : 15 : vec_safe_push (refs, probe);
7527 : 15 : vec_safe_push (refs, TREE_TYPE (probe));
7528 : 15 : probe = TREE_OPERAND (probe, 0);
7529 : 15 : break;
7530 : :
7531 : 17 : case IMAGPART_EXPR:
7532 : 17 : gcc_assert (refs->is_empty ());
7533 : 17 : vec_safe_push (refs, NULL_TREE);
7534 : 17 : vec_safe_push (refs, probe);
7535 : 17 : vec_safe_push (refs, TREE_TYPE (probe));
7536 : 17 : probe = TREE_OPERAND (probe, 0);
7537 : 17 : break;
7538 : :
7539 : 47820523 : default:
7540 : 47820523 : if (evaluated)
7541 : : object = probe;
7542 : : else
7543 : : {
7544 : 23910380 : tree pvar = tree_strip_any_location_wrapper (probe);
7545 : 23910380 : if (VAR_P (pvar) && DECL_ANON_UNION_VAR_P (pvar))
7546 : : {
7547 : : /* Stores to DECL_ANON_UNION_VAR_P var are allowed to change
7548 : : active union member. */
7549 : 9 : probe = DECL_VALUE_EXPR (pvar);
7550 : 9 : break;
7551 : : }
7552 : 23910371 : probe = cxx_eval_constant_expression (ctx, probe, vc_glvalue,
7553 : : non_constant_p, overflow_p,
7554 : : jump_target);
7555 : 23910371 : evaluated = true;
7556 : 23910371 : if (*jump_target)
7557 : : return NULL_TREE;
7558 : 23910371 : if (*non_constant_p)
7559 : : return t;
7560 : : }
7561 : : break;
7562 : : }
7563 : : }
7564 : :
7565 : 23910143 : if (modifying_const_object_p (TREE_CODE (t), object, mutable_p)
7566 : 23910143 : && const_object_being_modified == NULL_TREE)
7567 : 23910143 : const_object_being_modified = object;
7568 : :
7569 : 23910143 : if (DECL_P (object)
7570 : 23910102 : && TREE_CLOBBER_P (init)
7571 : 23941850 : && DECL_NAME (object) == heap_deleted_identifier)
7572 : : /* Ignore clobbers of deleted allocations for now; we'll get a better error
7573 : : message later when operator delete is called. */
7574 : 15 : return void_node;
7575 : :
7576 : : /* And then find/build up our initializer for the path to the subobject
7577 : : we're initializing. */
7578 : 23910128 : tree *valp;
7579 : 23910128 : if (DECL_P (object))
7580 : 23910087 : valp = ctx->global->get_value_ptr (object, TREE_CODE (t) == INIT_EXPR);
7581 : : else
7582 : 41 : valp = NULL;
7583 : 23910128 : if (!valp)
7584 : : {
7585 : : /* A constant-expression cannot modify objects from outside the
7586 : : constant-expression. */
7587 : 14240 : if (!ctx->quiet)
7588 : : {
7589 : 25 : auto_diagnostic_group d;
7590 : 25 : if (DECL_P (object) && DECL_NAME (object) == heap_deleted_identifier)
7591 : : {
7592 : 0 : error ("modification of allocated storage after deallocation "
7593 : : "is not a constant expression");
7594 : 0 : inform (DECL_SOURCE_LOCATION (object), "allocated here");
7595 : : }
7596 : 25 : else if (DECL_P (object) && ctx->global->is_outside_lifetime (object))
7597 : : {
7598 : 10 : if (TREE_CLOBBER_P (init))
7599 : 3 : error ("destroying %qE outside its lifetime", object);
7600 : : else
7601 : 7 : error ("modification of %qE outside its lifetime "
7602 : : "is not a constant expression", object);
7603 : 10 : inform (DECL_SOURCE_LOCATION (object), "declared here");
7604 : : }
7605 : : else
7606 : : {
7607 : 15 : if (TREE_CLOBBER_P (init))
7608 : 6 : error ("destroying %qE from outside current evaluation "
7609 : : "is not a constant expression", object);
7610 : : else
7611 : 9 : error ("modification of %qE from outside current evaluation "
7612 : : "is not a constant expression", object);
7613 : : }
7614 : 25 : }
7615 : 14240 : *non_constant_p = true;
7616 : 14240 : return t;
7617 : : }
7618 : :
7619 : : /* Handle explicit end-of-lifetime. */
7620 : 23895888 : if (TREE_CLOBBER_P (init))
7621 : : {
7622 : 31662 : if (refs->is_empty ())
7623 : 18920 : ctx->global->destroy_value (object);
7624 : 31662 : return void_node;
7625 : : }
7626 : :
7627 : 23864226 : type = TREE_TYPE (object);
7628 : 23864226 : bool no_zero_init = true;
7629 : 23864226 : bool zero_padding_bits = false;
7630 : :
7631 : 47728452 : auto_vec<tree *> ctors;
7632 : 47728452 : releasing_vec indexes;
7633 : 47728452 : auto_vec<int> index_pos_hints;
7634 : 23864226 : bool activated_union_member_p = false;
7635 : 23864226 : bool empty_base = false;
7636 : 32100048 : while (!refs->is_empty ())
7637 : : {
7638 : 8255570 : if (*valp == NULL_TREE)
7639 : : {
7640 : 948128 : *valp = build_constructor (type, NULL);
7641 : 948128 : CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
7642 : 948128 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
7643 : : }
7644 : 7307442 : else if (STRIP_ANY_LOCATION_WRAPPER (*valp),
7645 : 7307442 : TREE_CODE (*valp) == STRING_CST)
7646 : : {
7647 : : /* An array was initialized with a string constant, and now
7648 : : we're writing into one of its elements. Explode the
7649 : : single initialization into a set of element
7650 : : initializations. */
7651 : 3898 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
7652 : :
7653 : 3898 : tree string = *valp;
7654 : 3898 : tree elt_type = TREE_TYPE (type);
7655 : 3898 : unsigned chars_per_elt = (TYPE_PRECISION (elt_type)
7656 : 3898 : / TYPE_PRECISION (char_type_node));
7657 : 3898 : unsigned num_elts = TREE_STRING_LENGTH (string) / chars_per_elt;
7658 : 3898 : tree ary_ctor = build_constructor (type, NULL);
7659 : :
7660 : 3898 : vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor), num_elts);
7661 : 7957 : for (unsigned ix = 0; ix != num_elts; ix++)
7662 : : {
7663 : 4059 : constructor_elt elt =
7664 : : {
7665 : 4059 : build_int_cst (size_type_node, ix),
7666 : 4059 : extract_string_elt (string, chars_per_elt, ix)
7667 : 4059 : };
7668 : 4059 : CONSTRUCTOR_ELTS (ary_ctor)->quick_push (elt);
7669 : : }
7670 : :
7671 : 3898 : *valp = ary_ctor;
7672 : : }
7673 : :
7674 : 8255570 : enum tree_code code = TREE_CODE (type);
7675 : 8255570 : tree reftype = refs->pop();
7676 : 8255570 : tree index = refs->pop();
7677 : 8255570 : bool is_access_expr = refs->pop() != NULL_TREE;
7678 : :
7679 : 8255570 : if (code == COMPLEX_TYPE)
7680 : : {
7681 : 32 : if (TREE_CODE (*valp) == COMPLEX_CST)
7682 : 30 : *valp = build2 (COMPLEX_EXPR, type, TREE_REALPART (*valp),
7683 : 30 : TREE_IMAGPART (*valp));
7684 : 2 : else if (TREE_CODE (*valp) == CONSTRUCTOR
7685 : 1 : && CONSTRUCTOR_NELTS (*valp) == 0
7686 : 3 : && CONSTRUCTOR_NO_CLEARING (*valp))
7687 : : {
7688 : 1 : tree r = build_constructor (reftype, NULL);
7689 : 1 : CONSTRUCTOR_NO_CLEARING (r) = 1;
7690 : 1 : *valp = build2 (COMPLEX_EXPR, type, r, r);
7691 : : }
7692 : 32 : gcc_assert (TREE_CODE (*valp) == COMPLEX_EXPR);
7693 : 32 : ctors.safe_push (valp);
7694 : 32 : vec_safe_push (indexes, index);
7695 : 32 : valp = &TREE_OPERAND (*valp, TREE_CODE (index) == IMAGPART_EXPR);
7696 : 32 : gcc_checking_assert (refs->is_empty ());
7697 : : type = reftype;
7698 : 19716 : break;
7699 : : }
7700 : :
7701 : : /* If the value of object is already zero-initialized, any new ctors for
7702 : : subobjects will also be zero-initialized. Similarly with zeroing of
7703 : : padding bits. */
7704 : 8255538 : no_zero_init = CONSTRUCTOR_NO_CLEARING (*valp);
7705 : 8255538 : zero_padding_bits = CONSTRUCTOR_ZERO_PADDING_BITS (*valp);
7706 : :
7707 : 8255538 : if (code == RECORD_TYPE && is_empty_field (index))
7708 : : /* Don't build a sub-CONSTRUCTOR for an empty base or field, as they
7709 : : have no data and might have an offset lower than previously declared
7710 : : fields, which confuses the middle-end. The code below will notice
7711 : : that we don't have a CONSTRUCTOR for our inner target and just
7712 : : return init. */
7713 : : {
7714 : : empty_base = true;
7715 : : break;
7716 : : }
7717 : :
7718 : : /* If a union is zero-initialized, its first non-static named data member
7719 : : is zero-initialized (and therefore active). */
7720 : 8235822 : if (code == UNION_TYPE
7721 : 8235822 : && !no_zero_init
7722 : 8235822 : && CONSTRUCTOR_NELTS (*valp) == 0)
7723 : 1959 : if (tree first = next_aggregate_field (TYPE_FIELDS (type)))
7724 : 1959 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (*valp), first, NULL_TREE);
7725 : :
7726 : : /* Check for implicit change of active member for a union. */
7727 : 8235822 : if (code == UNION_TYPE
7728 : 148199 : && (CONSTRUCTOR_NELTS (*valp) == 0
7729 : 42504 : || CONSTRUCTOR_ELT (*valp, 0)->index != index)
7730 : : /* An INIT_EXPR of the last member in an access chain is always OK,
7731 : : but still check implicit change of members earlier on; see
7732 : : cpp2a/constexpr-union6.C. */
7733 : 8344877 : && !(TREE_CODE (t) == INIT_EXPR && refs->is_empty ()))
7734 : : {
7735 : 75290 : bool has_active_member = CONSTRUCTOR_NELTS (*valp) != 0;
7736 : 75290 : tree inner = strip_array_types (reftype);
7737 : :
7738 : 75290 : if (has_active_member && cxx_dialect < cxx20)
7739 : : {
7740 : 68 : if (!ctx->quiet)
7741 : 22 : error_at (cp_expr_loc_or_input_loc (t),
7742 : : "change of the active member of a union "
7743 : : "from %qD to %qD is not a constant expression "
7744 : : "before C++20",
7745 : 22 : CONSTRUCTOR_ELT (*valp, 0)->index,
7746 : : index);
7747 : 68 : *non_constant_p = true;
7748 : : }
7749 : 75222 : else if (!is_access_expr
7750 : 75222 : || (TREE_CODE (t) == MODIFY_EXPR
7751 : 1405 : && CLASS_TYPE_P (inner)
7752 : 15 : && !type_has_non_deleted_trivial_default_ctor (inner)))
7753 : : {
7754 : : /* Diagnose changing active union member after initialization
7755 : : without a valid member access expression, as described in
7756 : : [class.union.general] p5. */
7757 : 73814 : if (!ctx->quiet)
7758 : : {
7759 : 36 : auto_diagnostic_group d;
7760 : 36 : if (has_active_member)
7761 : 24 : error_at (cp_expr_loc_or_input_loc (t),
7762 : : "accessing %qD member instead of initialized "
7763 : : "%qD member in constant expression",
7764 : 24 : index, CONSTRUCTOR_ELT (*valp, 0)->index);
7765 : : else
7766 : 12 : error_at (cp_expr_loc_or_input_loc (t),
7767 : : "accessing uninitialized member %qD",
7768 : : index);
7769 : 36 : if (is_access_expr)
7770 : 3 : inform (DECL_SOURCE_LOCATION (index),
7771 : : "%qD does not implicitly begin its lifetime "
7772 : : "because %qT does not have a non-deleted "
7773 : : "trivial default constructor, use "
7774 : : "%<std::construct_at%> instead",
7775 : : index, inner);
7776 : : else
7777 : 33 : inform (DECL_SOURCE_LOCATION (index),
7778 : : "initializing %qD requires a member access "
7779 : : "expression as the left operand of the assignment",
7780 : : index);
7781 : 36 : }
7782 : 73814 : *non_constant_p = true;
7783 : : }
7784 : 1408 : else if (has_active_member && CONSTRUCTOR_NO_CLEARING (*valp))
7785 : : {
7786 : : /* Diagnose changing the active union member while the union
7787 : : is in the process of being initialized. */
7788 : 9 : if (!ctx->quiet)
7789 : 3 : error_at (cp_expr_loc_or_input_loc (t),
7790 : : "change of the active member of a union "
7791 : : "from %qD to %qD during initialization",
7792 : 3 : CONSTRUCTOR_ELT (*valp, 0)->index,
7793 : : index);
7794 : 9 : *non_constant_p = true;
7795 : : }
7796 : : no_zero_init = true;
7797 : : }
7798 : :
7799 : 8235822 : ctors.safe_push (valp);
7800 : 8235822 : vec_safe_push (indexes, index);
7801 : :
7802 : 8235822 : constructor_elt *cep
7803 : 8235822 : = get_or_insert_ctor_field (*valp, index);
7804 : 8235822 : index_pos_hints.safe_push (cep - CONSTRUCTOR_ELTS (*valp)->begin());
7805 : :
7806 : 8235822 : if (code == UNION_TYPE)
7807 : 148199 : activated_union_member_p = true;
7808 : :
7809 : 8235822 : valp = &cep->value;
7810 : 8235822 : type = reftype;
7811 : : }
7812 : :
7813 : : /* For initialization of an empty base, the original target will be
7814 : : *(base*)this, evaluation of which resolves to the object
7815 : : argument, which has the derived type rather than the base type. */
7816 : 47708736 : if (!empty_base && !(same_type_ignoring_top_level_qualifiers_p
7817 : 23844510 : (initialized_type (init), type)))
7818 : : {
7819 : 2786 : gcc_assert (is_empty_class (TREE_TYPE (target)));
7820 : : empty_base = true;
7821 : : }
7822 : :
7823 : : /* Detect modifying a constant object in constexpr evaluation.
7824 : : We have found a const object that is being modified. Figure out
7825 : : if we need to issue an error. Consider
7826 : :
7827 : : struct A {
7828 : : int n;
7829 : : constexpr A() : n(1) { n = 2; } // #1
7830 : : };
7831 : : struct B {
7832 : : const A a;
7833 : : constexpr B() { a.n = 3; } // #2
7834 : : };
7835 : : constexpr B b{};
7836 : :
7837 : : #1 is OK, since we're modifying an object under construction, but
7838 : : #2 is wrong, since "a" is const and has been fully constructed.
7839 : : To track it, we use the TREE_READONLY bit in the object's CONSTRUCTOR
7840 : : which means that the object is read-only. For the example above, the
7841 : : *ctors stack at the point of #2 will look like:
7842 : :
7843 : : ctors[0] = {.a={.n=2}} TREE_READONLY = 0
7844 : : ctors[1] = {.n=2} TREE_READONLY = 1
7845 : :
7846 : : and we're modifying "b.a", so we search the stack and see if the
7847 : : constructor for "b.a" has already run. */
7848 : 23864226 : if (const_object_being_modified)
7849 : : {
7850 : 4935 : bool fail = false;
7851 : 4935 : tree const_objtype
7852 : 4935 : = strip_array_types (TREE_TYPE (const_object_being_modified));
7853 : 4935 : if (!CLASS_TYPE_P (const_objtype))
7854 : : fail = true;
7855 : : else
7856 : : {
7857 : : /* [class.ctor]p5 "A constructor can be invoked for a const,
7858 : : volatile, or const volatile object. const and volatile
7859 : : semantics are not applied on an object under construction.
7860 : : They come into effect when the constructor for the most
7861 : : derived object ends." */
7862 : 14659 : for (tree *elt : ctors)
7863 : 4999 : if (same_type_ignoring_top_level_qualifiers_p
7864 : 4999 : (TREE_TYPE (const_object_being_modified), TREE_TYPE (*elt)))
7865 : : {
7866 : 4830 : fail = TREE_READONLY (*elt);
7867 : 4830 : break;
7868 : : }
7869 : : }
7870 : 4830 : if (fail)
7871 : : {
7872 : 198 : if (!ctx->quiet)
7873 : 63 : modifying_const_object_error (t, const_object_being_modified);
7874 : 198 : *non_constant_p = true;
7875 : 198 : return t;
7876 : : }
7877 : : }
7878 : :
7879 : 23864028 : if (!preeval)
7880 : : {
7881 : : /* We're handling an INIT_EXPR of class type, so the value of the
7882 : : initializer can depend on the object it's initializing. */
7883 : :
7884 : : /* Create a new CONSTRUCTOR in case evaluation of the initializer
7885 : : wants to modify it. */
7886 : 6377259 : if (*valp == NULL_TREE)
7887 : : {
7888 : 6261546 : *valp = build_constructor (type, NULL);
7889 : 6261546 : CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
7890 : 6261546 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
7891 : : }
7892 : 6377259 : new_ctx.ctor = empty_base ? NULL_TREE : *valp;
7893 : 6377259 : new_ctx.object = target;
7894 : : /* Avoid temporary materialization when initializing from a TARGET_EXPR.
7895 : : We don't need to mess with AGGR_EXPR_SLOT/VEC_INIT_EXPR_SLOT because
7896 : : expansion of those trees uses ctx instead. */
7897 : 6377259 : if (TREE_CODE (init) == TARGET_EXPR)
7898 : 1508800 : if (tree tinit = TARGET_EXPR_INITIAL (init))
7899 : 1508800 : init = tinit;
7900 : 6377259 : init = cxx_eval_constant_expression (&new_ctx, init, vc_prvalue,
7901 : : non_constant_p, overflow_p,
7902 : : jump_target);
7903 : 6377259 : if (*jump_target)
7904 : : return NULL_TREE;
7905 : : /* The hash table might have moved since the get earlier, and the
7906 : : initializer might have mutated the underlying CONSTRUCTORs, so we must
7907 : : recompute VALP. */
7908 : 6377240 : valp = ctx->global->get_value_ptr (object, TREE_CODE (t) == INIT_EXPR);
7909 : 7199288 : for (unsigned i = 0; i < vec_safe_length (indexes); i++)
7910 : : {
7911 : 822048 : ctors[i] = valp;
7912 : 822048 : constructor_elt *cep
7913 : 822048 : = get_or_insert_ctor_field (*valp, indexes[i], index_pos_hints[i]);
7914 : 822048 : valp = &cep->value;
7915 : : }
7916 : : }
7917 : :
7918 : 23864009 : if (*non_constant_p)
7919 : : return t;
7920 : :
7921 : : /* Don't share a CONSTRUCTOR that might be changed later. */
7922 : 22960575 : init = unshare_constructor (init);
7923 : :
7924 : 22960575 : gcc_checking_assert (!*valp || (same_type_ignoring_top_level_qualifiers_p
7925 : : (TREE_TYPE (*valp), type)));
7926 : 22960575 : if (empty_base)
7927 : : {
7928 : : /* Just evaluate the initializer and return, since there's no actual data
7929 : : to store, and we didn't build a CONSTRUCTOR. */
7930 : 21153 : if (!*valp)
7931 : : {
7932 : : /* But do make sure we have something in *valp. */
7933 : 0 : *valp = build_constructor (type, nullptr);
7934 : 0 : CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
7935 : 0 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
7936 : : }
7937 : : }
7938 : 22939422 : else if (*valp && TREE_CODE (*valp) == CONSTRUCTOR
7939 : 5572215 : && TREE_CODE (init) == CONSTRUCTOR)
7940 : : {
7941 : : /* An outer ctx->ctor might be pointing to *valp, so replace
7942 : : its contents. */
7943 : 1904044 : CONSTRUCTOR_ELTS (*valp) = CONSTRUCTOR_ELTS (init);
7944 : 1904044 : TREE_CONSTANT (*valp) = TREE_CONSTANT (init);
7945 : 1904044 : TREE_SIDE_EFFECTS (*valp) = TREE_SIDE_EFFECTS (init);
7946 : 5712132 : CONSTRUCTOR_NO_CLEARING (*valp)
7947 : 1904044 : = CONSTRUCTOR_NO_CLEARING (init);
7948 : 5712132 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp)
7949 : 1904044 : = CONSTRUCTOR_ZERO_PADDING_BITS (init);
7950 : : }
7951 : : else
7952 : 21035378 : *valp = init;
7953 : :
7954 : : /* After initialization, 'const' semantics apply to the value of the
7955 : : object. Make a note of this fact by marking the CONSTRUCTOR
7956 : : TREE_READONLY. */
7957 : 22960575 : if (TREE_CODE (t) == INIT_EXPR
7958 : 16545003 : && !empty_base
7959 : 16523850 : && TREE_CODE (*valp) == CONSTRUCTOR
7960 : 24818956 : && TYPE_READONLY (type))
7961 : : {
7962 : 17574 : if (INDIRECT_REF_P (target)
7963 : 32655 : && (is_this_parameter
7964 : 15081 : (tree_strip_nop_conversions (TREE_OPERAND (target, 0)))))
7965 : : /* We've just initialized '*this' (perhaps via the target
7966 : : constructor of a delegating constructor). Leave it up to the
7967 : : caller that set 'this' to set TREE_READONLY appropriately. */
7968 : 26 : gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
7969 : : (TREE_TYPE (target), type) || empty_base);
7970 : : else
7971 : 17548 : TREE_READONLY (*valp) = true;
7972 : : }
7973 : :
7974 : : /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing
7975 : : CONSTRUCTORs, if any. */
7976 : 22960575 : bool c = TREE_CONSTANT (init);
7977 : 22960575 : bool s = TREE_SIDE_EFFECTS (init);
7978 : 22960575 : if (!indexes->is_empty ())
7979 : : {
7980 : 5053347 : tree last = indexes->last ();
7981 : 5053347 : if (TREE_CODE (last) == REALPART_EXPR
7982 : 5053347 : || TREE_CODE (last) == IMAGPART_EXPR)
7983 : : {
7984 : : /* And canonicalize COMPLEX_EXPR into COMPLEX_CST if
7985 : : possible. */
7986 : 32 : tree *cexpr = ctors.last ();
7987 : 32 : if (tree c = const_binop (COMPLEX_EXPR, TREE_TYPE (*cexpr),
7988 : 32 : TREE_OPERAND (*cexpr, 0),
7989 : 32 : TREE_OPERAND (*cexpr, 1)))
7990 : 31 : *cexpr = c;
7991 : : else
7992 : : {
7993 : 2 : TREE_CONSTANT (*cexpr)
7994 : 1 : = (TREE_CONSTANT (TREE_OPERAND (*cexpr, 0))
7995 : 1 : & TREE_CONSTANT (TREE_OPERAND (*cexpr, 1)));
7996 : 2 : TREE_SIDE_EFFECTS (*cexpr)
7997 : 2 : = (TREE_SIDE_EFFECTS (TREE_OPERAND (*cexpr, 0))
7998 : 1 : | TREE_SIDE_EFFECTS (TREE_OPERAND (*cexpr, 1)));
7999 : : }
8000 : 32 : c = TREE_CONSTANT (*cexpr);
8001 : 32 : s = TREE_SIDE_EFFECTS (*cexpr);
8002 : : }
8003 : : }
8004 : 22960575 : if (!c || s || activated_union_member_p)
8005 : 9457122 : for (tree *elt : ctors)
8006 : : {
8007 : 1342916 : if (TREE_CODE (*elt) != CONSTRUCTOR)
8008 : 0 : continue;
8009 : 1342916 : if (!c)
8010 : 1050047 : TREE_CONSTANT (*elt) = false;
8011 : 1342916 : if (s)
8012 : 0 : TREE_SIDE_EFFECTS (*elt) = true;
8013 : : /* Clear CONSTRUCTOR_NO_CLEARING since we've activated a member of
8014 : : this union. */
8015 : 1342916 : if (TREE_CODE (TREE_TYPE (*elt)) == UNION_TYPE)
8016 : 74237 : CONSTRUCTOR_NO_CLEARING (*elt) = false;
8017 : : }
8018 : :
8019 : 22960575 : if (lval)
8020 : 20104396 : return target;
8021 : : else
8022 : : return init;
8023 : 23910404 : }
8024 : :
8025 : : /* Evaluate a ++ or -- expression. */
8026 : :
8027 : : static tree
8028 : 2219914 : cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t,
8029 : : value_cat lval,
8030 : : bool *non_constant_p, bool *overflow_p,
8031 : : tree *jump_target)
8032 : : {
8033 : 2219914 : enum tree_code code = TREE_CODE (t);
8034 : 2219914 : tree type = TREE_TYPE (t);
8035 : 2219914 : tree op = TREE_OPERAND (t, 0);
8036 : 2219914 : tree offset = TREE_OPERAND (t, 1);
8037 : 2219914 : gcc_assert (TREE_CONSTANT (offset));
8038 : :
8039 : : /* OFFSET is constant, but perhaps not constant enough. We need to
8040 : : e.g. bash FLOAT_EXPRs to REAL_CSTs. */
8041 : 2219914 : offset = fold_simple (offset);
8042 : :
8043 : : /* The operand as an lvalue. */
8044 : 2219914 : op = cxx_eval_constant_expression (ctx, op, vc_glvalue,
8045 : : non_constant_p, overflow_p,
8046 : : jump_target);
8047 : 2219914 : if (*jump_target)
8048 : : return NULL_TREE;
8049 : :
8050 : : /* The operand as an rvalue. */
8051 : 2219914 : tree val
8052 : 2219914 : = cxx_eval_constant_expression (ctx, op, vc_prvalue,
8053 : : non_constant_p, overflow_p,
8054 : : jump_target);
8055 : 2219914 : if (*jump_target)
8056 : : return NULL_TREE;
8057 : : /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
8058 : : a local array in a constexpr function. */
8059 : 2219914 : bool ptr = INDIRECT_TYPE_P (TREE_TYPE (val));
8060 : 1039031 : if (!ptr)
8061 : 1039031 : VERIFY_CONSTANT (val);
8062 : :
8063 : : /* The modified value. */
8064 : 2183290 : bool inc = (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR);
8065 : 2183290 : tree mod;
8066 : 2183290 : if (INDIRECT_TYPE_P (type))
8067 : : {
8068 : : /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
8069 : 1180883 : offset = convert_to_ptrofftype (offset);
8070 : 1180883 : if (!inc)
8071 : 85451 : offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset);
8072 : 1180883 : mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset);
8073 : : }
8074 : 1002407 : else if (c_promoting_integer_type_p (type)
8075 : 1894 : && !TYPE_UNSIGNED (type)
8076 : 1002476 : && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
8077 : : {
8078 : 69 : offset = fold_convert (integer_type_node, offset);
8079 : 69 : mod = fold_convert (integer_type_node, val);
8080 : 125 : tree t = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, integer_type_node,
8081 : : mod, offset);
8082 : 69 : mod = fold_convert (type, t);
8083 : 69 : if (TREE_OVERFLOW_P (mod) && !TREE_OVERFLOW_P (t))
8084 : 9 : TREE_OVERFLOW (mod) = false;
8085 : : }
8086 : : else
8087 : 1361101 : mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset);
8088 : 2183290 : if (!ptr)
8089 : 1002407 : VERIFY_CONSTANT (mod);
8090 : :
8091 : : /* Storing the modified value. */
8092 : 3481723 : tree store = build2_loc (cp_expr_loc_or_loc (t, input_location),
8093 : : MODIFY_EXPR, type, op, mod);
8094 : 2183290 : mod = cxx_eval_constant_expression (ctx, store, lval,
8095 : : non_constant_p, overflow_p,
8096 : : jump_target);
8097 : 2183290 : ggc_free (store);
8098 : 2183290 : if (*jump_target)
8099 : : return NULL_TREE;
8100 : 2183290 : if (*non_constant_p)
8101 : : return t;
8102 : :
8103 : : /* And the value of the expression. */
8104 : 2145381 : if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
8105 : : /* Prefix ops are lvalues, but the caller might want an rvalue;
8106 : : lval has already been taken into account in the store above. */
8107 : : return mod;
8108 : : else
8109 : : /* Postfix ops are rvalues. */
8110 : 480648 : return val;
8111 : : }
8112 : :
8113 : : /* Subroutine of cxx_eval_statement_list. Determine whether the statement
8114 : : STMT matches *jump_target. If we're looking for a case label and we see
8115 : : the default label, note it in ctx->css_state. */
8116 : :
8117 : : static bool
8118 : 365444 : label_matches (const constexpr_ctx *ctx, tree *jump_target, tree stmt)
8119 : : {
8120 : 365444 : switch (TREE_CODE (*jump_target))
8121 : : {
8122 : 15 : case LABEL_DECL:
8123 : 15 : if (TREE_CODE (stmt) == LABEL_EXPR
8124 : 15 : && LABEL_EXPR_LABEL (stmt) == *jump_target)
8125 : : return true;
8126 : : break;
8127 : :
8128 : 362024 : case INTEGER_CST:
8129 : 362024 : if (TREE_CODE (stmt) == CASE_LABEL_EXPR)
8130 : : {
8131 : 362024 : gcc_assert (ctx->css_state != NULL);
8132 : 362024 : if (!CASE_LOW (stmt))
8133 : : {
8134 : : /* default: should appear just once in a SWITCH_EXPR
8135 : : body (excluding nested SWITCH_EXPR). */
8136 : 55258 : gcc_assert (*ctx->css_state != css_default_seen);
8137 : : /* When evaluating SWITCH_EXPR body for the second time,
8138 : : return true for the default: label. */
8139 : 55258 : if (*ctx->css_state == css_default_processing)
8140 : : return true;
8141 : 27641 : *ctx->css_state = css_default_seen;
8142 : : }
8143 : 306766 : else if (CASE_HIGH (stmt))
8144 : : {
8145 : 20 : if (tree_int_cst_le (CASE_LOW (stmt), *jump_target)
8146 : 31 : && tree_int_cst_le (*jump_target, CASE_HIGH (stmt)))
8147 : : return true;
8148 : : }
8149 : 306746 : else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt)))
8150 : : return true;
8151 : : }
8152 : : break;
8153 : :
8154 : : case BREAK_STMT:
8155 : : case CONTINUE_STMT:
8156 : : /* These two are handled directly in cxx_eval_loop_expr by testing
8157 : : breaks (jump_target) or continues (jump_target). */
8158 : : break;
8159 : :
8160 : : case VAR_DECL:
8161 : : /* Uncaught exception. This is handled by TRY_BLOCK evaluation
8162 : : and other places by testing throws (jump_target). */
8163 : : break;
8164 : :
8165 : 0 : default:
8166 : 0 : gcc_unreachable ();
8167 : : }
8168 : : return false;
8169 : : }
8170 : :
8171 : : /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
8172 : : semantics, for switch, break, continue, and return. */
8173 : :
8174 : : static tree
8175 : 18405692 : cxx_eval_statement_list (const constexpr_ctx *ctx, tree t,
8176 : : bool *non_constant_p, bool *overflow_p,
8177 : : tree *jump_target)
8178 : : {
8179 : : /* In a statement-expression we want to return the last value.
8180 : : For empty statement expression return void_node. */
8181 : 18405692 : tree r = void_node;
8182 : 43171446 : for (tree_stmt_iterator i = tsi_start (t); !tsi_end_p (i); ++i)
8183 : : {
8184 : 33937383 : tree stmt = *i;
8185 : :
8186 : : /* We've found a continue, so skip everything until we reach
8187 : : the label its jumping to. */
8188 : 33937383 : if (continues (jump_target))
8189 : : {
8190 : 3420 : if (label_matches (ctx, jump_target, stmt))
8191 : : /* Found it. */
8192 : 3 : *jump_target = NULL_TREE;
8193 : : else
8194 : 3417 : continue;
8195 : : }
8196 : 33933966 : if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT)
8197 : 5941312 : continue;
8198 : :
8199 : 27992654 : value_cat lval = vc_discard;
8200 : : /* The result of a statement-expression is not wrapped in EXPR_STMT. */
8201 : 27992654 : if (tsi_one_before_end_p (i) && TREE_CODE (stmt) != EXPR_STMT)
8202 : : lval = vc_prvalue;
8203 : :
8204 : 27992654 : r = cxx_eval_constant_expression (ctx, stmt, lval,
8205 : : non_constant_p, overflow_p,
8206 : : jump_target);
8207 : 27992654 : if (*non_constant_p)
8208 : : break;
8209 : 21775291 : if (returns (jump_target)
8210 : 18843364 : || breaks (jump_target)
8211 : 24765754 : || throws (jump_target))
8212 : : break;
8213 : : }
8214 : 18405692 : return r;
8215 : : }
8216 : :
8217 : : /* Evaluate a LOOP_EXPR for side-effects. Handles break and return
8218 : : semantics; continue semantics are covered by cxx_eval_statement_list. */
8219 : :
8220 : : static tree
8221 : 748607 : cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t,
8222 : : bool *non_constant_p, bool *overflow_p,
8223 : : tree *jump_target)
8224 : : {
8225 : 748607 : tree body, cond = NULL_TREE, expr = NULL_TREE;
8226 : 748607 : tree cond_prep = NULL_TREE, cond_cleanup = NULL_TREE;
8227 : 748607 : unsigned cond_cleanup_depth = 0;
8228 : 748607 : int count = 0;
8229 : 748607 : switch (TREE_CODE (t))
8230 : : {
8231 : 82 : case LOOP_EXPR:
8232 : 82 : body = LOOP_EXPR_BODY (t);
8233 : 82 : break;
8234 : 584188 : case DO_STMT:
8235 : 584188 : body = DO_BODY (t);
8236 : 584188 : cond = DO_COND (t);
8237 : 584188 : break;
8238 : 71316 : case WHILE_STMT:
8239 : 71316 : body = WHILE_BODY (t);
8240 : 71316 : cond = WHILE_COND (t);
8241 : 71316 : cond_prep = WHILE_COND_PREP (t);
8242 : 71316 : cond_cleanup = WHILE_COND_CLEANUP (t);
8243 : 71316 : count = -1;
8244 : 71316 : break;
8245 : 93021 : case FOR_STMT:
8246 : 93021 : if (FOR_INIT_STMT (t))
8247 : 0 : cxx_eval_constant_expression (ctx, FOR_INIT_STMT (t), vc_discard,
8248 : : non_constant_p, overflow_p, jump_target);
8249 : 93021 : if (*non_constant_p)
8250 : : return NULL_TREE;
8251 : 93021 : body = FOR_BODY (t);
8252 : 93021 : cond = FOR_COND (t);
8253 : 93021 : expr = FOR_EXPR (t);
8254 : 93021 : cond_prep = FOR_COND_PREP (t);
8255 : 93021 : cond_cleanup = FOR_COND_CLEANUP (t);
8256 : 93021 : count = -1;
8257 : 93021 : break;
8258 : 0 : default:
8259 : 0 : gcc_unreachable ();
8260 : : }
8261 : 748607 : if (cond_prep)
8262 : 12 : gcc_assert (TREE_CODE (cond_prep) == BIND_EXPR);
8263 : 8558897 : auto cleanup_cond = [&] {
8264 : : /* Clean up the condition variable after each iteration. */
8265 : 7810290 : if (cond_cleanup_depth && !*non_constant_p)
8266 : : {
8267 : 105 : auto_vec<tree, 4> cleanups (cond_cleanup_depth);
8268 : 105 : tree s = BIND_EXPR_BODY (cond_prep);
8269 : 105 : unsigned i;
8270 : 210 : for (i = cond_cleanup_depth; i; --i)
8271 : : {
8272 : 105 : tree_stmt_iterator iter = tsi_last (s);
8273 : 105 : s = tsi_stmt (iter);
8274 : 105 : cleanups.quick_push (CLEANUP_EXPR (s));
8275 : 105 : s = CLEANUP_BODY (s);
8276 : : }
8277 : 105 : tree c;
8278 : 420 : FOR_EACH_VEC_ELT_REVERSE (cleanups, i, c)
8279 : 105 : cxx_eval_constant_expression (ctx, c, vc_discard, non_constant_p,
8280 : : overflow_p, jump_target);
8281 : 105 : }
8282 : 7810290 : if (cond_prep)
8283 : 111 : for (tree decl = BIND_EXPR_VARS (cond_prep);
8284 : 222 : decl; decl = DECL_CHAIN (decl))
8285 : 111 : destroy_value_checked (ctx, decl, non_constant_p);
8286 : 748607 : };
8287 : 7226634 : do
8288 : : {
8289 : 7226634 : if (count != -1)
8290 : : {
8291 : 7062297 : if (body)
8292 : 7062297 : cxx_eval_constant_expression (ctx, body, vc_discard,
8293 : : non_constant_p, overflow_p,
8294 : : jump_target);
8295 : 7062297 : if (breaks (jump_target))
8296 : : {
8297 : 614 : *jump_target = NULL_TREE;
8298 : 614 : break;
8299 : : }
8300 : :
8301 : 7061683 : if (TREE_CODE (t) != LOOP_EXPR && continues (jump_target))
8302 : 694 : *jump_target = NULL_TREE;
8303 : :
8304 : 7061683 : if (expr)
8305 : 1121207 : cxx_eval_constant_expression (ctx, expr, vc_prvalue,
8306 : : non_constant_p, overflow_p,
8307 : : jump_target);
8308 : 7061683 : cleanup_cond ();
8309 : : }
8310 : :
8311 : 7226020 : if (cond_prep)
8312 : : {
8313 : 111 : for (tree decl = BIND_EXPR_VARS (cond_prep);
8314 : 222 : decl; decl = DECL_CHAIN (decl))
8315 : 111 : ctx->global->clear_value (decl);
8316 : 111 : if (cond_cleanup)
8317 : : {
8318 : : /* If COND_CLEANUP is non-NULL, we need to evaluate DEPTH
8319 : : nested STATEMENT_LISTs from inside of BIND_EXPR_BODY,
8320 : : but defer the evaluation of CLEANUP_EXPRs of CLEANUP_STMT
8321 : : at the end of those STATEMENT_LISTs. */
8322 : 111 : cond_cleanup_depth = 0;
8323 : 111 : tree s = BIND_EXPR_BODY (cond_prep);
8324 : 111 : for (unsigned depth = tree_to_uhwi (cond_cleanup);
8325 : 222 : depth; --depth)
8326 : : {
8327 : 111 : for (tree_stmt_iterator i = tsi_start (s);
8328 : 321 : !tsi_end_p (i); ++i)
8329 : : {
8330 : 321 : tree stmt = *i;
8331 : 321 : if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT)
8332 : 0 : continue;
8333 : 321 : if (tsi_one_before_end_p (i))
8334 : : {
8335 : : /* The last statement in the STATEMENT_LIST
8336 : : has to be a CLEANUP_STMT (verified in
8337 : : finish_loop_cond_prep). We want to
8338 : : evaluate just its CLEANUP_BODY part but not
8339 : : CLEANUP_EXPR part just yet. */
8340 : 105 : gcc_assert (TREE_CODE (stmt) == CLEANUP_STMT);
8341 : : /* If the CLEANUP_STMT is not actually to be
8342 : : evaluated, don't increment cond_cleanup_depth
8343 : : so that we don't evaluate the CLEANUP_EXPR
8344 : : for it later either. */
8345 : 105 : if (*jump_target)
8346 : : {
8347 : : depth = 1;
8348 : : break;
8349 : : }
8350 : 105 : ++cond_cleanup_depth;
8351 : : /* If not in the innermost one, next iteration
8352 : : will handle CLEANUP_BODY similarly. */
8353 : 105 : if (depth > 1)
8354 : : {
8355 : 0 : s = CLEANUP_BODY (stmt);
8356 : 0 : break;
8357 : : }
8358 : : /* The innermost one can be evaluated normally. */
8359 : 105 : cxx_eval_constant_expression (ctx,
8360 : 105 : CLEANUP_BODY (stmt),
8361 : : vc_discard,
8362 : : non_constant_p,
8363 : : overflow_p,
8364 : : jump_target);
8365 : 105 : break;
8366 : : }
8367 : : /* And so should be evaluated statements which aren't
8368 : : last in the STATEMENT_LIST. */
8369 : 216 : cxx_eval_constant_expression (ctx, stmt, vc_discard,
8370 : : non_constant_p, overflow_p,
8371 : : jump_target);
8372 : 216 : if (*non_constant_p
8373 : 210 : || returns (jump_target)
8374 : 210 : || breaks (jump_target)
8375 : 210 : || continues (jump_target)
8376 : 531 : || throws (jump_target))
8377 : : {
8378 : : depth = 1;
8379 : : break;
8380 : : }
8381 : : }
8382 : : }
8383 : : }
8384 : : else
8385 : 0 : cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (cond_prep),
8386 : : vc_discard, non_constant_p,
8387 : : overflow_p, jump_target);
8388 : : }
8389 : :
8390 : 7226020 : if (cond)
8391 : : {
8392 : 7224988 : tree res
8393 : 7224988 : = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
8394 : : non_constant_p, overflow_p,
8395 : : jump_target);
8396 : 7224988 : if (res)
8397 : : {
8398 : 7192178 : if (verify_constant (res, ctx->quiet, non_constant_p,
8399 : : overflow_p))
8400 : : break;
8401 : 6985942 : if (integer_zerop (res))
8402 : : break;
8403 : : }
8404 : : else
8405 : 32810 : gcc_assert (*jump_target);
8406 : : }
8407 : :
8408 : 6511124 : if (++count >= constexpr_loop_limit)
8409 : : {
8410 : 28 : if (!ctx->quiet)
8411 : 9 : error_at (cp_expr_loc_or_input_loc (t),
8412 : : "%<constexpr%> loop iteration count exceeds limit of %d "
8413 : : "(use %<-fconstexpr-loop-limit=%> to increase the limit)",
8414 : : constexpr_loop_limit);
8415 : 28 : *non_constant_p = true;
8416 : 28 : break;
8417 : : }
8418 : : }
8419 : 19500383 : while (!returns (jump_target)
8420 : 6478191 : && !breaks (jump_target)
8421 : 6478191 : && !continues (jump_target)
8422 : 6478284 : && (!switches (jump_target) || count == 0)
8423 : 6478161 : && !throws (jump_target)
8424 : 13022326 : && !*non_constant_p);
8425 : :
8426 : 748607 : cleanup_cond ();
8427 : :
8428 : 748607 : return NULL_TREE;
8429 : : }
8430 : :
8431 : : /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
8432 : : semantics. */
8433 : :
8434 : : static tree
8435 : 35432 : cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t,
8436 : : bool *non_constant_p, bool *overflow_p,
8437 : : tree *jump_target)
8438 : : {
8439 : 35432 : tree cond
8440 : 35432 : = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_COND (t) : SWITCH_COND (t);
8441 : 35432 : cond = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
8442 : : non_constant_p, overflow_p,
8443 : : jump_target);
8444 : 35432 : if (*jump_target)
8445 : : return NULL_TREE;
8446 : 35432 : VERIFY_CONSTANT (cond);
8447 : 35114 : if (TREE_CODE (cond) != INTEGER_CST)
8448 : : {
8449 : : /* If the condition doesn't reduce to an INTEGER_CST it isn't a usable
8450 : : switch condition even if it's constant enough for other things
8451 : : (c++/113545). */
8452 : 0 : gcc_checking_assert (ctx->quiet);
8453 : 0 : *non_constant_p = true;
8454 : 0 : return t;
8455 : : }
8456 : :
8457 : 35114 : *jump_target = cond;
8458 : :
8459 : 35114 : tree body
8460 : 35114 : = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_BODY (t) : SWITCH_BODY (t);
8461 : 35114 : constexpr_ctx new_ctx = *ctx;
8462 : 35114 : constexpr_switch_state css = css_default_not_seen;
8463 : 35114 : new_ctx.css_state = &css;
8464 : 35114 : cxx_eval_constant_expression (&new_ctx, body, vc_discard,
8465 : : non_constant_p, overflow_p, jump_target);
8466 : 62775 : if (switches (jump_target) && css == css_default_seen)
8467 : : {
8468 : : /* If the SWITCH_EXPR body has default: label, process it once again,
8469 : : this time instructing label_matches to return true for default:
8470 : : label on switches (jump_target). */
8471 : 27617 : css = css_default_processing;
8472 : 27617 : cxx_eval_constant_expression (&new_ctx, body, vc_discard,
8473 : : non_constant_p, overflow_p, jump_target);
8474 : : }
8475 : 35114 : if (breaks (jump_target) || switches (jump_target))
8476 : 21391 : *jump_target = NULL_TREE;
8477 : : return NULL_TREE;
8478 : : }
8479 : :
8480 : : /* Find the object of TYPE under initialization in CTX. */
8481 : :
8482 : : static tree
8483 : 16631 : lookup_placeholder (const constexpr_ctx *ctx, value_cat lval, tree type)
8484 : : {
8485 : 16631 : if (!ctx)
8486 : : return NULL_TREE;
8487 : :
8488 : : /* Prefer the outermost matching object, but don't cross
8489 : : CONSTRUCTOR_PLACEHOLDER_BOUNDARY constructors. */
8490 : 16299 : if (ctx->ctor && !CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor))
8491 : 510 : if (tree outer_ob = lookup_placeholder (ctx->parent, lval, type))
8492 : : return outer_ob;
8493 : :
8494 : : /* We could use ctx->object unconditionally, but using ctx->ctor when we
8495 : : can is a minor optimization. */
8496 : 16121 : if (!lval && ctx->ctor && same_type_p (TREE_TYPE (ctx->ctor), type))
8497 : 300 : return ctx->ctor;
8498 : :
8499 : 15821 : if (!ctx->object)
8500 : : return NULL_TREE;
8501 : :
8502 : : /* Since an object cannot have a field of its own type, we can search outward
8503 : : from ctx->object to find the unique containing object of TYPE. */
8504 : : tree ob = ctx->object;
8505 : 15812 : while (ob)
8506 : : {
8507 : 15812 : if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob), type))
8508 : : break;
8509 : 223 : if (handled_component_p (ob))
8510 : 221 : ob = TREE_OPERAND (ob, 0);
8511 : : else
8512 : : ob = NULL_TREE;
8513 : : }
8514 : :
8515 : : return ob;
8516 : : }
8517 : :
8518 : : /* Complain about an attempt to evaluate inline assembly. If FUNDEF_P is
8519 : : true, we're checking a constexpr function body. */
8520 : :
8521 : : static void
8522 : 31 : inline_asm_in_constexpr_error (location_t loc, bool fundef_p)
8523 : : {
8524 : 31 : auto_diagnostic_group d;
8525 : 31 : if (constexpr_error (loc, fundef_p, "inline assembly is not a "
8526 : : "constant expression"))
8527 : 31 : inform (loc, "only unevaluated inline assembly is allowed in a "
8528 : : "%<constexpr%> function in C++20");
8529 : 31 : }
8530 : :
8531 : : /* We're getting the constant value of DECL in a manifestly constant-evaluated
8532 : : context; maybe complain about that. */
8533 : :
8534 : : static void
8535 : 55749766 : maybe_warn_about_constant_value (location_t loc, tree decl)
8536 : : {
8537 : 55749766 : static bool explained = false;
8538 : 55749766 : if (cxx_dialect >= cxx17
8539 : 55597409 : && warn_interference_size
8540 : 55597409 : && !OPTION_SET_P (param_destruct_interfere_size)
8541 : 55597409 : && DECL_CONTEXT (decl) == std_node
8542 : 6705888 : && DECL_NAME (decl)
8543 : 6705882 : && id_equal (DECL_NAME (decl), "hardware_destructive_interference_size")
8544 : 12 : && (LOCATION_FILE (input_location) != main_input_filename
8545 : 6 : || module_exporting_p ())
8546 : 55749769 : && warning_at (loc, OPT_Winterference_size, "use of %qD", decl)
8547 : 55749775 : && !explained)
8548 : : {
8549 : 6 : explained = true;
8550 : 6 : inform (loc, "its value can vary between compiler versions or "
8551 : : "with different %<-mtune%> or %<-mcpu%> flags");
8552 : 6 : inform (loc, "if this use is part of a public ABI, change it to "
8553 : : "instead use a constant variable you define");
8554 : 6 : inform (loc, "the default value for the current CPU tuning "
8555 : : "is %d bytes", param_destruct_interfere_size);
8556 : 6 : inform (loc, "you can stabilize this value with %<--param "
8557 : : "hardware_destructive_interference_size=%d%>, or disable "
8558 : : "this warning with %<-Wno-interference-size%>",
8559 : : param_destruct_interfere_size);
8560 : : }
8561 : 55749766 : }
8562 : :
8563 : : /* For element type ELT_TYPE, return the appropriate type of the heap object
8564 : : containing such element(s). COOKIE_SIZE is NULL or the size of cookie
8565 : : in bytes. If COOKIE_SIZE is NULL, return array type
8566 : : ELT_TYPE[FULL_SIZE / sizeof(ELT_TYPE)], otherwise return
8567 : : struct { size_t[COOKIE_SIZE/sizeof(size_t)]; ELT_TYPE[N]; }
8568 : : where N is computed such that the size of the struct fits into FULL_SIZE.
8569 : : If ARG_SIZE is non-NULL, it is the first argument to the new operator.
8570 : : It should be passed if ELT_TYPE is zero sized type in which case FULL_SIZE
8571 : : will be also 0 and so it is not possible to determine the actual array
8572 : : size. CTX, NON_CONSTANT_P and OVERFLOW_P are used during constant
8573 : : expression evaluation of subexpressions of ARG_SIZE. */
8574 : :
8575 : : static tree
8576 : 5472 : build_new_constexpr_heap_type (const constexpr_ctx *ctx, tree elt_type,
8577 : : tree cookie_size, tree full_size, tree arg_size,
8578 : : bool *non_constant_p, bool *overflow_p,
8579 : : tree *jump_target)
8580 : : {
8581 : 5472 : gcc_assert (cookie_size == NULL_TREE || tree_fits_uhwi_p (cookie_size));
8582 : 5472 : gcc_assert (tree_fits_uhwi_p (full_size));
8583 : 5472 : unsigned HOST_WIDE_INT csz = cookie_size ? tree_to_uhwi (cookie_size) : 0;
8584 : 5472 : if (arg_size)
8585 : : {
8586 : 9 : STRIP_NOPS (arg_size);
8587 : 9 : if (cookie_size)
8588 : : {
8589 : 0 : if (TREE_CODE (arg_size) != PLUS_EXPR)
8590 : : arg_size = NULL_TREE;
8591 : 0 : else if (TREE_CODE (TREE_OPERAND (arg_size, 0)) == INTEGER_CST
8592 : 0 : && tree_int_cst_equal (cookie_size,
8593 : 0 : TREE_OPERAND (arg_size, 0)))
8594 : : {
8595 : 0 : arg_size = TREE_OPERAND (arg_size, 1);
8596 : 0 : STRIP_NOPS (arg_size);
8597 : : }
8598 : 0 : else if (TREE_CODE (TREE_OPERAND (arg_size, 1)) == INTEGER_CST
8599 : 0 : && tree_int_cst_equal (cookie_size,
8600 : 0 : TREE_OPERAND (arg_size, 1)))
8601 : : {
8602 : 0 : arg_size = TREE_OPERAND (arg_size, 0);
8603 : 0 : STRIP_NOPS (arg_size);
8604 : : }
8605 : : else
8606 : : arg_size = NULL_TREE;
8607 : : }
8608 : 9 : if (arg_size && TREE_CODE (arg_size) == MULT_EXPR)
8609 : : {
8610 : 9 : tree op0 = TREE_OPERAND (arg_size, 0);
8611 : 9 : tree op1 = TREE_OPERAND (arg_size, 1);
8612 : 9 : if (integer_zerop (op0))
8613 : 9 : arg_size
8614 : 9 : = cxx_eval_constant_expression (ctx, op1, vc_prvalue,
8615 : : non_constant_p, overflow_p,
8616 : : jump_target);
8617 : 0 : else if (integer_zerop (op1))
8618 : 0 : arg_size
8619 : 0 : = cxx_eval_constant_expression (ctx, op0, vc_prvalue,
8620 : : non_constant_p, overflow_p,
8621 : : jump_target);
8622 : : else
8623 : : arg_size = NULL_TREE;
8624 : 9 : if (*jump_target)
8625 : : return NULL_TREE;
8626 : : }
8627 : : else
8628 : : arg_size = NULL_TREE;
8629 : : }
8630 : :
8631 : 9 : unsigned HOST_WIDE_INT fsz = tree_to_uhwi (arg_size ? arg_size : full_size);
8632 : 5472 : if (!arg_size)
8633 : : {
8634 : 5463 : unsigned HOST_WIDE_INT esz = int_size_in_bytes (elt_type);
8635 : 5463 : gcc_assert (fsz >= csz);
8636 : 5463 : fsz -= csz;
8637 : 5463 : if (esz)
8638 : 5463 : fsz /= esz;
8639 : : }
8640 : 5472 : tree itype2 = build_index_type (size_int (fsz - 1));
8641 : 5472 : if (!cookie_size)
8642 : 5467 : return build_cplus_array_type (elt_type, itype2);
8643 : 5 : return build_new_constexpr_heap_type (elt_type, cookie_size, itype2);
8644 : : }
8645 : :
8646 : : /* Handle the case when a cleanup of some expression throws. JMP_TARGET
8647 : : indicates whether the cleanup threw or not, *JUMP_TARGET indicates whether
8648 : : the expression which needed the cleanup threw. If both threw, diagnose
8649 : : it and return NULL, otherwise return R. If only the cleanup threw, set
8650 : : *JUMP_TARGET to the exception object from the cleanup. */
8651 : :
8652 : : static tree
8653 : 52308 : merge_jump_target (location_t loc, const constexpr_ctx *ctx, tree r,
8654 : : bool *non_constant_p, tree *jump_target, tree jmp_target)
8655 : : {
8656 : 52309 : if (!throws (&jmp_target))
8657 : : return r;
8658 : 7 : if (throws (jump_target))
8659 : : {
8660 : : /* [except.throw]/9 - If the exception handling mechanism
8661 : : handling an uncaught exception directly invokes a function
8662 : : that exits via an exception, the function std::terminate is
8663 : : invoked. */
8664 : 6 : if (!ctx->quiet)
8665 : : {
8666 : 2 : auto_diagnostic_group d;
8667 : 2 : diagnose_std_terminate (loc, ctx, *jump_target);
8668 : 2 : inform (loc, "destructor exited with an exception");
8669 : 2 : }
8670 : 6 : *non_constant_p = true;
8671 : 6 : *jump_target = NULL_TREE;
8672 : 6 : return NULL_TREE;
8673 : : }
8674 : 1 : *jump_target = jmp_target;
8675 : 1 : return r;
8676 : : }
8677 : :
8678 : : /* Attempt to reduce the expression T to a constant value.
8679 : : On failure, issue diagnostic and return error_mark_node. */
8680 : : /* FIXME unify with c_fully_fold */
8681 : : /* FIXME overflow_p is too global */
8682 : :
8683 : : static tree
8684 : 1148648676 : cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
8685 : : value_cat lval,
8686 : : bool *non_constant_p, bool *overflow_p,
8687 : : tree *jump_target)
8688 : : {
8689 : 1148648676 : if (*jump_target)
8690 : : {
8691 : : /* If we are jumping, ignore all statements/expressions except those
8692 : : that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */
8693 : 1350959 : switch (TREE_CODE (t))
8694 : : {
8695 : : case BIND_EXPR:
8696 : : case STATEMENT_LIST:
8697 : : case LOOP_EXPR:
8698 : : case COND_EXPR:
8699 : : case IF_STMT:
8700 : : case DO_STMT:
8701 : : case WHILE_STMT:
8702 : : case FOR_STMT:
8703 : : break;
8704 : 362024 : case LABEL_EXPR:
8705 : 362024 : case CASE_LABEL_EXPR:
8706 : 362024 : if (label_matches (ctx, jump_target, t))
8707 : : /* Found it. */
8708 : 35070 : *jump_target = NULL_TREE;
8709 : 362024 : return NULL_TREE;
8710 : : default:
8711 : : return NULL_TREE;
8712 : : }
8713 : : }
8714 : 1147517813 : if (error_operand_p (t))
8715 : : {
8716 : 133 : *non_constant_p = true;
8717 : 133 : return t;
8718 : : }
8719 : :
8720 : : /* Change the input location to the currently processed expression for
8721 : : better error messages when a subexpression has no location. */
8722 : 1147517680 : location_t loc = cp_expr_loc_or_input_loc (t);
8723 : 2295029966 : iloc_sentinel sentinel (loc);
8724 : :
8725 : 1147517680 : STRIP_ANY_LOCATION_WRAPPER (t);
8726 : :
8727 : 1147517680 : if (CONSTANT_CLASS_P (t))
8728 : : {
8729 : 249628930 : if (TREE_OVERFLOW (t))
8730 : : {
8731 : 62 : if (!ctx->quiet)
8732 : 15 : permerror (input_location, "overflow in constant expression");
8733 : 62 : if (!flag_permissive || ctx->quiet)
8734 : 59 : *overflow_p = true;
8735 : : }
8736 : :
8737 : 249628930 : if (TREE_CODE (t) == INTEGER_CST
8738 : 241218242 : && TYPE_PTR_P (TREE_TYPE (t))
8739 : : /* INTEGER_CST with pointer-to-method type is only used
8740 : : for a virtual method in a pointer to member function.
8741 : : Don't reject those. */
8742 : 733476 : && TREE_CODE (TREE_TYPE (TREE_TYPE (t))) != METHOD_TYPE
8743 : 250362104 : && !integer_zerop (t))
8744 : : {
8745 : 6 : if (!ctx->quiet)
8746 : 0 : error ("value %qE of type %qT is not a constant expression",
8747 : 0 : t, TREE_TYPE (t));
8748 : 6 : *non_constant_p = true;
8749 : : }
8750 : :
8751 : 249628930 : return t;
8752 : : }
8753 : :
8754 : : /* Avoid excessively long constexpr evaluations. */
8755 : 897888750 : if (++ctx->global->constexpr_ops_count >= constexpr_ops_limit)
8756 : : {
8757 : 9 : if (!ctx->quiet)
8758 : 3 : error_at (loc,
8759 : : "%<constexpr%> evaluation operation count exceeds limit of "
8760 : : "%wd (use %<-fconstexpr-ops-limit=%> to increase the limit)",
8761 : : constexpr_ops_limit);
8762 : 9 : ctx->global->constexpr_ops_count = INTTYPE_MINIMUM (HOST_WIDE_INT);
8763 : 9 : *non_constant_p = true;
8764 : 9 : return t;
8765 : : }
8766 : :
8767 : 897888741 : constexpr_ctx new_ctx;
8768 : 897888741 : tree r = t;
8769 : :
8770 : 897888741 : tree_code tcode = TREE_CODE (t);
8771 : 897888741 : switch (tcode)
8772 : : {
8773 : 13499586 : case RESULT_DECL:
8774 : 13499586 : if (lval)
8775 : : return t;
8776 : : /* We ask for an rvalue for the RESULT_DECL when indirecting
8777 : : through an invisible reference, or in named return value
8778 : : optimization. */
8779 : 31943 : if (tree v = ctx->global->get_value (t))
8780 : : return v;
8781 : : else
8782 : : {
8783 : 3 : if (!ctx->quiet)
8784 : 0 : error ("%qE is not a constant expression", t);
8785 : 3 : *non_constant_p = true;
8786 : : }
8787 : 3 : break;
8788 : :
8789 : 127305453 : case VAR_DECL:
8790 : 127305453 : if (DECL_HAS_VALUE_EXPR_P (t))
8791 : : {
8792 : 1177316 : if (is_normal_capture_proxy (t)
8793 : 1177316 : && current_function_decl == DECL_CONTEXT (t))
8794 : : {
8795 : : /* Function parms aren't constexpr within the function
8796 : : definition, so don't try to look at the closure. But if the
8797 : : captured variable is constant, try to evaluate it directly. */
8798 : 375057 : r = DECL_CAPTURED_VARIABLE (t);
8799 : : }
8800 : : else
8801 : 802259 : r = DECL_VALUE_EXPR (t);
8802 : :
8803 : 1177316 : tree type = TREE_TYPE (t);
8804 : 1177316 : if (TYPE_REF_P (type) != TYPE_REF_P (TREE_TYPE (r)))
8805 : : {
8806 : : /* Adjust r to match the reference-ness of t. */
8807 : 237157 : if (TYPE_REF_P (type))
8808 : 237154 : r = build_address (r);
8809 : : else
8810 : 3 : r = convert_from_reference (r);
8811 : : }
8812 : 1177316 : return cxx_eval_constant_expression (ctx, r, lval, non_constant_p,
8813 : 1177316 : overflow_p, jump_target);
8814 : : }
8815 : : /* fall through */
8816 : 133743853 : case CONST_DECL:
8817 : : /* We used to not check lval for CONST_DECL, but darwin.cc uses
8818 : : CONST_DECL for aggregate constants. */
8819 : 133743853 : if (lval)
8820 : : return t;
8821 : 106113147 : else if (t == ctx->object)
8822 : 521546 : return ctx->ctor;
8823 : 105591601 : if (VAR_P (t))
8824 : : {
8825 : 97975885 : if (tree v = ctx->global->get_value (t))
8826 : : {
8827 : : r = v;
8828 : : break;
8829 : : }
8830 : 86279853 : if (ctx->global->is_outside_lifetime (t))
8831 : : {
8832 : 96 : if (!ctx->quiet)
8833 : 28 : outside_lifetime_error (loc, t);
8834 : 96 : *non_constant_p = true;
8835 : 96 : break;
8836 : : }
8837 : : }
8838 : 93895473 : if (ctx->manifestly_const_eval == mce_true)
8839 : 55749766 : maybe_warn_about_constant_value (loc, t);
8840 : 93895473 : if (COMPLETE_TYPE_P (TREE_TYPE (t))
8841 : 93895473 : && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
8842 : : {
8843 : : /* If the class is empty, we aren't actually loading anything. */
8844 : 79514 : r = build_constructor (TREE_TYPE (t), NULL);
8845 : 79514 : TREE_CONSTANT (r) = true;
8846 : : }
8847 : 93815959 : else if (ctx->strict)
8848 : 93541697 : r = decl_really_constant_value (t, /*unshare_p=*/false);
8849 : : else
8850 : 274262 : r = decl_constant_value (t, /*unshare_p=*/false);
8851 : 93892776 : if (TREE_CODE (r) == TARGET_EXPR
8852 : 93892776 : && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR)
8853 : 0 : r = TARGET_EXPR_INITIAL (r);
8854 : 93892776 : if (DECL_P (r)
8855 : : /* P2280 allows references to unknown. */
8856 : 94036319 : && !(p2280_active_p (ctx) && VAR_P (t) && TYPE_REF_P (TREE_TYPE (t))))
8857 : : {
8858 : 27624673 : if (!ctx->quiet)
8859 : 152 : non_const_var_error (loc, r, /*fundef_p*/false);
8860 : 27624673 : *non_constant_p = true;
8861 : : }
8862 : : break;
8863 : :
8864 : : case DEBUG_BEGIN_STMT:
8865 : : /* ??? It might be nice to retain this information somehow, so
8866 : : as to be able to step into a constexpr function call. */
8867 : : /* Fall through. */
8868 : :
8869 : : case FUNCTION_DECL:
8870 : : case TEMPLATE_DECL:
8871 : : case LABEL_DECL:
8872 : : case LABEL_EXPR:
8873 : : case CASE_LABEL_EXPR:
8874 : : case PREDICT_EXPR:
8875 : : case OMP_DECLARE_MAPPER:
8876 : : return t;
8877 : :
8878 : 103108528 : case PARM_DECL:
8879 : 103108528 : if (lval && !TYPE_REF_P (TREE_TYPE (t)))
8880 : : {
8881 : : /* glvalue use. */
8882 : 5176480 : if (TREE_ADDRESSABLE (TREE_TYPE (t)))
8883 : 93158 : if (tree v = ctx->global->get_value (t))
8884 : 713961893 : r = v;
8885 : : }
8886 : 97932048 : else if (tree v = ctx->global->get_value (t))
8887 : : {
8888 : 33664906 : r = v;
8889 : 33664906 : if (TREE_ADDRESSABLE (TREE_TYPE (t)))
8890 : 4717 : r = cxx_eval_constant_expression (ctx, r, vc_prvalue,
8891 : : non_constant_p, overflow_p,
8892 : : jump_target);
8893 : 33664906 : if (*jump_target)
8894 : : return NULL_TREE;
8895 : : }
8896 : 64267142 : else if (lval)
8897 : : /* Defer in case this is only used for its type. */;
8898 : 64267142 : else if (ctx->global->is_outside_lifetime (t))
8899 : : {
8900 : 18 : if (!ctx->quiet)
8901 : 6 : outside_lifetime_error (loc, t);
8902 : 18 : *non_constant_p = true;
8903 : 18 : break;
8904 : : }
8905 : 64267124 : else if (COMPLETE_TYPE_P (TREE_TYPE (t))
8906 : 64267124 : && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
8907 : : {
8908 : : /* If the class is empty, we aren't actually loading anything. */
8909 : 5428 : r = build_constructor (TREE_TYPE (t), NULL);
8910 : 5428 : TREE_CONSTANT (r) = true;
8911 : : }
8912 : 64261696 : else if (p2280_active_p (ctx) && TYPE_REF_P (TREE_TYPE (t)))
8913 : : /* P2280 allows references to unknown... */;
8914 : 64202340 : else if (p2280_active_p (ctx) && is_this_parameter (t))
8915 : : /* ...as well as the this pointer. */;
8916 : : else
8917 : : {
8918 : 64050127 : if (!ctx->quiet)
8919 : 194 : error ("%qE is not a constant expression", t);
8920 : 64050127 : *non_constant_p = true;
8921 : : }
8922 : : break;
8923 : :
8924 : 83244256 : case CALL_EXPR:
8925 : 83244256 : case AGGR_INIT_EXPR:
8926 : 83244256 : r = cxx_eval_call_expression (ctx, t, lval,
8927 : : non_constant_p, overflow_p, jump_target);
8928 : 83244256 : break;
8929 : :
8930 : 3886498 : case DECL_EXPR:
8931 : 3886498 : {
8932 : 3886498 : r = DECL_EXPR_DECL (t);
8933 : 3886498 : if (TREE_CODE (r) == USING_DECL)
8934 : : {
8935 : 2331 : r = void_node;
8936 : 2331 : break;
8937 : : }
8938 : :
8939 : 3884167 : if (VAR_P (r)
8940 : 3884167 : && (TREE_STATIC (r)
8941 : 3866664 : || (CP_DECL_THREAD_LOCAL_P (r) && !DECL_REALLY_EXTERN (r)))
8942 : : /* Allow __FUNCTION__ etc. */
8943 : 17503 : && !DECL_ARTIFICIAL (r)
8944 : 3884186 : && !decl_constant_var_p (r))
8945 : : {
8946 : 7 : if (!ctx->quiet)
8947 : : {
8948 : 2 : if (CP_DECL_THREAD_LOCAL_P (r))
8949 : 1 : error_at (loc, "control passes through definition of %qD "
8950 : : "with thread storage duration", r);
8951 : : else
8952 : 1 : error_at (loc, "control passes through definition of %qD "
8953 : : "with static storage duration", r);
8954 : : }
8955 : 7 : *non_constant_p = true;
8956 : 7 : break;
8957 : : }
8958 : :
8959 : : /* make_rtl_for_nonlocal_decl could have deferred emission of
8960 : : a local static var, but if it appears in a statement expression
8961 : : which is constant expression evaluated to e.g. just the address
8962 : : of the variable, its DECL_EXPR will never be seen during
8963 : : gimple lowering's record_vars_into as the statement expression
8964 : : will not be in the IL at all. */
8965 : 3884160 : if (VAR_P (r)
8966 : 3884160 : && TREE_STATIC (r)
8967 : 17496 : && !DECL_REALLY_EXTERN (r)
8968 : 17496 : && DECL_FUNCTION_SCOPE_P (r)
8969 : 17496 : && !var_in_maybe_constexpr_fn (r)
8970 : 3884163 : && decl_constant_var_p (r))
8971 : : {
8972 : 3 : varpool_node *node = varpool_node::get (r);
8973 : 3 : if (node == NULL || !node->definition)
8974 : 3 : rest_of_decl_compilation (r, 0, at_eof);
8975 : : }
8976 : :
8977 : 7742429 : if (AGGREGATE_TYPE_P (TREE_TYPE (r))
8978 : 7502225 : || VECTOR_TYPE_P (TREE_TYPE (r)))
8979 : : {
8980 : 266301 : new_ctx = *ctx;
8981 : 266301 : new_ctx.object = r;
8982 : 266301 : new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL);
8983 : 266301 : CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
8984 : 266301 : ctx->global->put_value (r, new_ctx.ctor);
8985 : 266301 : ctx = &new_ctx;
8986 : : }
8987 : :
8988 : 3884160 : if (tree init = DECL_INITIAL (r))
8989 : : {
8990 : 1918467 : init = cxx_eval_constant_expression (ctx, init, vc_prvalue,
8991 : : non_constant_p, overflow_p,
8992 : : jump_target);
8993 : 1918467 : if (*jump_target)
8994 : : return NULL_TREE;
8995 : : /* Don't share a CONSTRUCTOR that might be changed. */
8996 : 1918467 : init = unshare_constructor (init);
8997 : : /* Remember that a constant object's constructor has already
8998 : : run. */
8999 : 3836934 : if (CLASS_TYPE_P (TREE_TYPE (r))
9000 : 2004649 : && CP_TYPE_CONST_P (TREE_TYPE (r)))
9001 : 1777 : TREE_READONLY (init) = true;
9002 : 1918467 : ctx->global->put_value (r, init);
9003 : : }
9004 : 1965693 : else if (ctx == &new_ctx)
9005 : : /* We gave it a CONSTRUCTOR above. */;
9006 : : else
9007 : 1793991 : ctx->global->put_value (r, NULL_TREE);
9008 : : }
9009 : : break;
9010 : :
9011 : 8698623 : case TARGET_EXPR:
9012 : 8698623 : {
9013 : 8698623 : tree type = TREE_TYPE (t);
9014 : :
9015 : 8698623 : if (!literal_type_p (type))
9016 : : {
9017 : 12521 : if (!ctx->quiet)
9018 : : {
9019 : 0 : auto_diagnostic_group d;
9020 : 0 : error ("temporary of non-literal type %qT in a "
9021 : : "constant expression", type);
9022 : 0 : explain_non_literal_class (type);
9023 : 0 : }
9024 : 12521 : *non_constant_p = true;
9025 : 3145846 : break;
9026 : : }
9027 : 8686102 : gcc_checking_assert (!TARGET_EXPR_DIRECT_INIT_P (t));
9028 : : /* Avoid evaluating a TARGET_EXPR more than once. */
9029 : 8686102 : tree slot = TARGET_EXPR_SLOT (t);
9030 : 8686102 : if (tree v = ctx->global->get_value (slot))
9031 : : {
9032 : 448 : if (lval)
9033 : 4729047 : return slot;
9034 : : r = v;
9035 : : break;
9036 : : }
9037 : 8685654 : if ((AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type)))
9038 : : {
9039 : : /* We're being expanded without an explicit target, so start
9040 : : initializing a new object; expansion with an explicit target
9041 : : strips the TARGET_EXPR before we get here. */
9042 : 6975545 : new_ctx = *ctx;
9043 : : /* Link CTX to NEW_CTX so that lookup_placeholder can resolve
9044 : : any PLACEHOLDER_EXPR within the initializer that refers to the
9045 : : former object under construction. */
9046 : 6975545 : new_ctx.parent = ctx;
9047 : 6975545 : new_ctx.ctor = build_constructor (type, NULL);
9048 : 6975545 : CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
9049 : 6975545 : new_ctx.object = slot;
9050 : 6975545 : ctx->global->put_value (new_ctx.object, new_ctx.ctor);
9051 : 6975545 : ctx = &new_ctx;
9052 : : }
9053 : :
9054 : : /* If the initializer is complex, evaluate it to initialize slot. */
9055 : 8685654 : bool is_complex = target_expr_needs_replace (t);
9056 : 8685654 : if (is_complex)
9057 : : /* In case no initialization actually happens, clear out any
9058 : : void_node from a previous evaluation. */
9059 : 42 : ctx->global->put_value (slot, NULL_TREE);
9060 : :
9061 : : /* Pass vc_prvalue because this indicates
9062 : : initialization of a temporary. */
9063 : 8685654 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), vc_prvalue,
9064 : : non_constant_p, overflow_p,
9065 : : jump_target);
9066 : 8685654 : if (*non_constant_p)
9067 : : break;
9068 : 5552753 : if (*jump_target)
9069 : : return NULL_TREE;
9070 : 5552752 : if (!is_complex)
9071 : : {
9072 : 5552713 : r = unshare_constructor (r);
9073 : : /* Adjust the type of the result to the type of the temporary. */
9074 : 5552713 : r = adjust_temp_type (type, r);
9075 : 5552713 : ctx->global->put_value (slot, r);
9076 : : }
9077 : 5552752 : if (TARGET_EXPR_CLEANUP (t)
9078 : 5552752 : && (!CLEANUP_EH_ONLY (t) || cxx_dialect >= cxx26))
9079 : : {
9080 : 198401 : ctx->global->cleanups->safe_push (TARGET_EXPR_CLEANUP (t));
9081 : : /* Mark CLEANUP_EH_ONLY cleanups by pushing NULL_TREE after
9082 : : them. */
9083 : 198401 : if (CLEANUP_EH_ONLY (t))
9084 : 335 : ctx->global->cleanups->safe_push (NULL_TREE);
9085 : : }
9086 : 5552752 : if (ctx->save_exprs)
9087 : 1832253 : ctx->save_exprs->safe_push (slot);
9088 : 5552752 : if (lval)
9089 : : return slot;
9090 : 823730 : if (is_complex)
9091 : 0 : r = ctx->global->get_value (slot);
9092 : : }
9093 : 823730 : break;
9094 : :
9095 : 32294991 : case INIT_EXPR:
9096 : 32294991 : case MODIFY_EXPR:
9097 : 32294991 : gcc_assert (jump_target == NULL || *jump_target == NULL_TREE);
9098 : 32294991 : r = cxx_eval_store_expression (ctx, t, lval,
9099 : : non_constant_p, overflow_p, jump_target);
9100 : 32294991 : break;
9101 : :
9102 : 0 : case SCOPE_REF:
9103 : 0 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
9104 : : lval,
9105 : : non_constant_p, overflow_p,
9106 : : jump_target);
9107 : 0 : break;
9108 : :
9109 : 18182922 : case RETURN_EXPR:
9110 : 18182922 : if (TREE_OPERAND (t, 0) != NULL_TREE)
9111 : 18165611 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
9112 : : lval,
9113 : : non_constant_p, overflow_p,
9114 : : jump_target);
9115 : 18182922 : if (!throws (jump_target))
9116 : 18182867 : *jump_target = t;
9117 : : break;
9118 : 22615 : case BREAK_STMT:
9119 : 22615 : case CONTINUE_STMT:
9120 : 22615 : *jump_target = t;
9121 : 22615 : break;
9122 : :
9123 : 615480 : case SAVE_EXPR:
9124 : : /* Avoid evaluating a SAVE_EXPR more than once. */
9125 : 615480 : if (tree v = ctx->global->get_value (t))
9126 : : r = v;
9127 : : else
9128 : : {
9129 : 609528 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
9130 : : vc_prvalue, non_constant_p,
9131 : : overflow_p, jump_target);
9132 : 609528 : if (*non_constant_p || *jump_target)
9133 : : break;
9134 : 7772 : ctx->global->put_value (t, r);
9135 : 7772 : if (ctx->save_exprs)
9136 : 5680 : ctx->save_exprs->safe_push (t);
9137 : : }
9138 : : break;
9139 : :
9140 : 20828763 : case NON_LVALUE_EXPR:
9141 : 20828763 : case EXPR_STMT:
9142 : 20828763 : case EH_SPEC_BLOCK:
9143 : 20828763 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
9144 : : lval,
9145 : : non_constant_p, overflow_p,
9146 : : jump_target);
9147 : 20828763 : break;
9148 : :
9149 : 304 : case TRY_BLOCK:
9150 : 304 : r = cxx_eval_constant_expression (ctx, TRY_STMTS (t), lval,
9151 : : non_constant_p, overflow_p,
9152 : : jump_target);
9153 : 304 : if (!*non_constant_p && throws (jump_target))
9154 : 195 : if (tree h = TRY_HANDLERS (t))
9155 : : {
9156 : 195 : tree type = strip_array_types (TREE_TYPE (*jump_target));
9157 : 195 : if (TREE_CODE (h) == STATEMENT_LIST)
9158 : : {
9159 : 334 : for (tree stmt : tsi_range (h))
9160 : 317 : if (TREE_CODE (stmt) == HANDLER
9161 : 317 : && handler_match_for_exception_type (stmt, type))
9162 : : {
9163 : : h = stmt;
9164 : : break;
9165 : : }
9166 : 95 : if (TREE_CODE (h) == STATEMENT_LIST)
9167 : : h = NULL_TREE;
9168 : : }
9169 : 100 : else if (TREE_CODE (h) != HANDLER
9170 : 100 : || !handler_match_for_exception_type (h, type))
9171 : : h = NULL_TREE;
9172 : : if (h)
9173 : : {
9174 : 178 : gcc_assert (VAR_P (*jump_target));
9175 : 178 : ctx->global->caught_exceptions.safe_push (*jump_target);
9176 : 178 : ctx->global->caught_exceptions.safe_push (HANDLER_TYPE (h));
9177 : 178 : *jump_target = NULL_TREE;
9178 : 178 : r = cxx_eval_constant_expression (ctx, HANDLER_BODY (h),
9179 : : vc_discard, non_constant_p,
9180 : : overflow_p, jump_target);
9181 : : }
9182 : : }
9183 : : break;
9184 : :
9185 : 26771622 : case CLEANUP_POINT_EXPR:
9186 : 26771622 : {
9187 : 26771622 : auto_vec<tree, 2> cleanups;
9188 : 26771622 : vec<tree> *prev_cleanups = ctx->global->cleanups;
9189 : 26771622 : ctx->global->cleanups = &cleanups;
9190 : :
9191 : 26771622 : auto_vec<tree, 10> save_exprs;
9192 : 26771622 : constexpr_ctx new_ctx = *ctx;
9193 : 26771622 : new_ctx.save_exprs = &save_exprs;
9194 : :
9195 : 26771622 : r = cxx_eval_constant_expression (&new_ctx, TREE_OPERAND (t, 0),
9196 : : lval,
9197 : : non_constant_p, overflow_p,
9198 : : jump_target);
9199 : :
9200 : 26771622 : ctx->global->cleanups = prev_cleanups;
9201 : 26771622 : unsigned int i;
9202 : 26771622 : tree cleanup, jmp_target = NULL_TREE;
9203 : 26771622 : bool eh = throws (jump_target);
9204 : : /* Evaluate the cleanups. */
9205 : 53722437 : FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup)
9206 : 179193 : if (cleanup == NULL_TREE)
9207 : : {
9208 : : /* NULL_TREE cleanup is a marker that before it is
9209 : : CLEANUP_EH_ONLY cleanup. Skip the cleanup before it
9210 : : if the body didn't throw. */
9211 : 315 : if (!eh)
9212 : 314 : --i;
9213 : : }
9214 : : else
9215 : 178878 : cxx_eval_constant_expression (&new_ctx, cleanup, vc_discard,
9216 : : non_constant_p, overflow_p,
9217 : : &jmp_target);
9218 : :
9219 : : /* Forget SAVE_EXPRs and TARGET_EXPRs created by this
9220 : : full-expression. */
9221 : 82152799 : for (tree save_expr : save_exprs)
9222 : 1837933 : destroy_value_checked (ctx, save_expr, non_constant_p);
9223 : 26771622 : if (throws (&jmp_target))
9224 : 0 : *jump_target = jmp_target;
9225 : 26771622 : }
9226 : 26771622 : break;
9227 : :
9228 : 17229937 : case MUST_NOT_THROW_EXPR:
9229 : 17229937 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
9230 : : lval,
9231 : : non_constant_p, overflow_p,
9232 : : jump_target);
9233 : 17229937 : if (throws (jump_target))
9234 : : {
9235 : : /* [except.handle]/7 - If the search for a handler exits the
9236 : : function body of a function with a non-throwing exception
9237 : : specification, the function std::terminate is invoked. */
9238 : 27 : if (!ctx->quiet)
9239 : : {
9240 : 9 : auto_diagnostic_group d;
9241 : 9 : diagnose_std_terminate (loc, ctx, *jump_target);
9242 : 9 : if (MUST_NOT_THROW_NOEXCEPT_P (t)
9243 : 7 : && ctx->call
9244 : 16 : && ctx->call->fundef)
9245 : 7 : inform (loc, "uncaught exception exited from %<noexcept%> "
9246 : : "function %qD",
9247 : : ctx->call->fundef->decl);
9248 : 2 : else if (MUST_NOT_THROW_THROW_P (t))
9249 : 1 : inform (loc, "destructor exited with an exception after "
9250 : : "initializing the exception object");
9251 : 1 : else if (MUST_NOT_THROW_CATCH_P (t))
9252 : 1 : inform (loc, "constructor exited with another exception while "
9253 : : "entering handler");
9254 : 9 : }
9255 : 27 : *non_constant_p = true;
9256 : 27 : *jump_target = NULL_TREE;
9257 : 27 : r = NULL_TREE;
9258 : : }
9259 : : break;
9260 : :
9261 : 0 : case TRY_CATCH_EXPR:
9262 : 0 : if (TREE_OPERAND (t, 0) == NULL_TREE)
9263 : : {
9264 : 0 : r = void_node;
9265 : 0 : break;
9266 : : }
9267 : 0 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
9268 : : non_constant_p, overflow_p,
9269 : : jump_target);
9270 : 0 : if (!*non_constant_p && throws (jump_target))
9271 : : {
9272 : 0 : tree jmp_target = NULL_TREE;
9273 : 0 : cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), vc_discard,
9274 : : non_constant_p, overflow_p,
9275 : : &jmp_target);
9276 : 0 : r = merge_jump_target (loc, ctx, r, non_constant_p, jump_target,
9277 : : jmp_target);
9278 : : }
9279 : : break;
9280 : :
9281 : 381 : case TRY_FINALLY_EXPR:
9282 : 381 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
9283 : : non_constant_p, overflow_p,
9284 : : jump_target);
9285 : 381 : if (!*non_constant_p)
9286 : : {
9287 : 366 : tree jmp_target = NULL_TREE;
9288 : : /* Also evaluate the cleanup. */
9289 : 366 : if (TREE_CODE (TREE_OPERAND (t, 1)) == EH_ELSE_EXPR
9290 : 366 : && throws (jump_target))
9291 : 0 : cxx_eval_constant_expression (ctx,
9292 : 0 : TREE_OPERAND (TREE_OPERAND (t, 1),
9293 : : 1), vc_discard,
9294 : : non_constant_p, overflow_p,
9295 : : &jmp_target);
9296 : : else
9297 : 366 : cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), vc_discard,
9298 : : non_constant_p, overflow_p,
9299 : : &jmp_target);
9300 : 366 : r = merge_jump_target (loc, ctx, r, non_constant_p, jump_target,
9301 : : jmp_target);
9302 : : }
9303 : : break;
9304 : :
9305 : 18 : case EH_ELSE_EXPR:
9306 : : /* Evaluate any cleanup that applies to non-EH exits. */
9307 : 18 : cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), vc_discard,
9308 : : non_constant_p, overflow_p,
9309 : : jump_target);
9310 : :
9311 : : /* The EH path is handled in TRY_FINALLY_EXPR handling above. */
9312 : 18 : break;
9313 : :
9314 : 646941 : case CLEANUP_STMT:
9315 : 646941 : r = cxx_eval_constant_expression (ctx, CLEANUP_BODY (t), lval,
9316 : : non_constant_p, overflow_p,
9317 : : jump_target);
9318 : 646941 : if ((!CLEANUP_EH_ONLY (t) || throws (jump_target)) && !*non_constant_p)
9319 : : {
9320 : 51942 : iloc_sentinel ils (loc);
9321 : 51942 : tree jmp_target = NULL_TREE;
9322 : : /* Also evaluate the cleanup. */
9323 : 51942 : cxx_eval_constant_expression (ctx, CLEANUP_EXPR (t), vc_discard,
9324 : : non_constant_p, overflow_p,
9325 : : &jmp_target);
9326 : 51942 : r = merge_jump_target (loc, ctx, r, non_constant_p, jump_target,
9327 : : jmp_target);
9328 : 51942 : }
9329 : : break;
9330 : :
9331 : : /* These differ from cxx_eval_unary_expression in that this doesn't
9332 : : check for a constant operand or result; an address can be
9333 : : constant without its operand being, and vice versa. */
9334 : 31475212 : case MEM_REF:
9335 : 31475212 : case INDIRECT_REF:
9336 : 31475212 : r = cxx_eval_indirect_ref (ctx, t, lval,
9337 : : non_constant_p, overflow_p,
9338 : : jump_target);
9339 : 31475212 : break;
9340 : :
9341 : 37751971 : case ADDR_EXPR:
9342 : 37751971 : {
9343 : 37751971 : tree oldop = TREE_OPERAND (t, 0);
9344 : 37751971 : tree op = cxx_eval_constant_expression (ctx, oldop, vc_glvalue,
9345 : : non_constant_p, overflow_p,
9346 : : jump_target);
9347 : 37751971 : if (*jump_target)
9348 : : return NULL_TREE;
9349 : : /* Don't VERIFY_CONSTANT here. */
9350 : 37751971 : if (*non_constant_p)
9351 : : return t;
9352 : 30283850 : gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR);
9353 : : /* This function does more aggressive folding than fold itself. */
9354 : 30283850 : r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
9355 : 30283850 : if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
9356 : : {
9357 : 22398986 : ggc_free (r);
9358 : 22398986 : return t;
9359 : : }
9360 : : break;
9361 : : }
9362 : :
9363 : 482846 : case REALPART_EXPR:
9364 : 482846 : case IMAGPART_EXPR:
9365 : 482846 : if (lval)
9366 : : {
9367 : 584 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
9368 : : non_constant_p, overflow_p,
9369 : : jump_target);
9370 : 584 : if (*jump_target)
9371 : : return NULL_TREE;
9372 : 584 : if (r == error_mark_node)
9373 : : ;
9374 : 584 : else if (r == TREE_OPERAND (t, 0) || lval == vc_discard)
9375 : : r = t;
9376 : : else
9377 : 562 : r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), r);
9378 : : break;
9379 : : }
9380 : : /* FALLTHRU */
9381 : 20060451 : case CONJ_EXPR:
9382 : 20060451 : case FIX_TRUNC_EXPR:
9383 : 20060451 : case FLOAT_EXPR:
9384 : 20060451 : case NEGATE_EXPR:
9385 : 20060451 : case ABS_EXPR:
9386 : 20060451 : case ABSU_EXPR:
9387 : 20060451 : case BIT_NOT_EXPR:
9388 : 20060451 : case TRUTH_NOT_EXPR:
9389 : 20060451 : case FIXED_CONVERT_EXPR:
9390 : 20060451 : case VEC_DUPLICATE_EXPR:
9391 : 20060451 : r = cxx_eval_unary_expression (ctx, t, lval,
9392 : : non_constant_p, overflow_p,
9393 : : jump_target);
9394 : 20060451 : break;
9395 : :
9396 : 6913335 : case SIZEOF_EXPR:
9397 : 6913335 : r = fold_sizeof_expr (t);
9398 : : /* In a template, fold_sizeof_expr may merely create a new SIZEOF_EXPR,
9399 : : which could lead to an infinite recursion. */
9400 : 6913335 : if (TREE_CODE (r) != SIZEOF_EXPR)
9401 : 6913335 : r = cxx_eval_constant_expression (ctx, r, lval,
9402 : : non_constant_p, overflow_p,
9403 : : jump_target);
9404 : : else
9405 : : {
9406 : 0 : *non_constant_p = true;
9407 : 0 : gcc_assert (ctx->quiet);
9408 : : }
9409 : :
9410 : : break;
9411 : :
9412 : 3190242 : case COMPOUND_EXPR:
9413 : 3190242 : {
9414 : : /* check_return_expr sometimes wraps a TARGET_EXPR in a
9415 : : COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
9416 : : introduced by build_call_a. */
9417 : 3190242 : tree op0 = TREE_OPERAND (t, 0);
9418 : 3190242 : tree op1 = TREE_OPERAND (t, 1);
9419 : 3190242 : STRIP_NOPS (op1);
9420 : 686224 : if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
9421 : 3258797 : || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
9422 : 2834629 : r = cxx_eval_constant_expression (ctx, op0,
9423 : : lval, non_constant_p, overflow_p,
9424 : : jump_target);
9425 : : else
9426 : : {
9427 : : /* Check that the LHS is constant and then discard it. */
9428 : 355613 : cxx_eval_constant_expression (ctx, op0, vc_discard,
9429 : : non_constant_p, overflow_p,
9430 : : jump_target);
9431 : 355613 : if (*jump_target)
9432 : : return NULL_TREE;
9433 : 355368 : if (*non_constant_p)
9434 : : return t;
9435 : 262607 : op1 = TREE_OPERAND (t, 1);
9436 : 262607 : r = cxx_eval_constant_expression (ctx, op1,
9437 : : lval, non_constant_p, overflow_p,
9438 : : jump_target);
9439 : : }
9440 : : }
9441 : : break;
9442 : :
9443 : 47772514 : case POINTER_PLUS_EXPR:
9444 : 47772514 : case POINTER_DIFF_EXPR:
9445 : 47772514 : case PLUS_EXPR:
9446 : 47772514 : case MINUS_EXPR:
9447 : 47772514 : case MULT_EXPR:
9448 : 47772514 : case TRUNC_DIV_EXPR:
9449 : 47772514 : case CEIL_DIV_EXPR:
9450 : 47772514 : case FLOOR_DIV_EXPR:
9451 : 47772514 : case ROUND_DIV_EXPR:
9452 : 47772514 : case TRUNC_MOD_EXPR:
9453 : 47772514 : case CEIL_MOD_EXPR:
9454 : 47772514 : case ROUND_MOD_EXPR:
9455 : 47772514 : case RDIV_EXPR:
9456 : 47772514 : case EXACT_DIV_EXPR:
9457 : 47772514 : case MIN_EXPR:
9458 : 47772514 : case MAX_EXPR:
9459 : 47772514 : case LSHIFT_EXPR:
9460 : 47772514 : case RSHIFT_EXPR:
9461 : 47772514 : case LROTATE_EXPR:
9462 : 47772514 : case RROTATE_EXPR:
9463 : 47772514 : case BIT_IOR_EXPR:
9464 : 47772514 : case BIT_XOR_EXPR:
9465 : 47772514 : case BIT_AND_EXPR:
9466 : 47772514 : case TRUTH_XOR_EXPR:
9467 : 47772514 : case LT_EXPR:
9468 : 47772514 : case LE_EXPR:
9469 : 47772514 : case GT_EXPR:
9470 : 47772514 : case GE_EXPR:
9471 : 47772514 : case EQ_EXPR:
9472 : 47772514 : case NE_EXPR:
9473 : 47772514 : case SPACESHIP_EXPR:
9474 : 47772514 : case UNORDERED_EXPR:
9475 : 47772514 : case ORDERED_EXPR:
9476 : 47772514 : case UNLT_EXPR:
9477 : 47772514 : case UNLE_EXPR:
9478 : 47772514 : case UNGT_EXPR:
9479 : 47772514 : case UNGE_EXPR:
9480 : 47772514 : case UNEQ_EXPR:
9481 : 47772514 : case LTGT_EXPR:
9482 : 47772514 : case RANGE_EXPR:
9483 : 47772514 : case COMPLEX_EXPR:
9484 : 47772514 : r = cxx_eval_binary_expression (ctx, t, lval,
9485 : : non_constant_p, overflow_p,
9486 : : jump_target);
9487 : 47772514 : break;
9488 : :
9489 : : /* fold can introduce non-IF versions of these; still treat them as
9490 : : short-circuiting. */
9491 : 5659342 : case TRUTH_AND_EXPR:
9492 : 5659342 : case TRUTH_ANDIF_EXPR:
9493 : 5659342 : r = cxx_eval_logical_expression (ctx, t, boolean_false_node,
9494 : : boolean_true_node,
9495 : : non_constant_p, overflow_p,
9496 : : jump_target);
9497 : 5659342 : break;
9498 : :
9499 : 1177024 : case TRUTH_OR_EXPR:
9500 : 1177024 : case TRUTH_ORIF_EXPR:
9501 : 1177024 : r = cxx_eval_logical_expression (ctx, t, boolean_true_node,
9502 : : boolean_false_node,
9503 : : non_constant_p, overflow_p,
9504 : : jump_target);
9505 : 1177024 : break;
9506 : :
9507 : 2625284 : case ARRAY_REF:
9508 : 2625284 : r = cxx_eval_array_reference (ctx, t, lval,
9509 : : non_constant_p, overflow_p,
9510 : : jump_target);
9511 : 2625284 : break;
9512 : :
9513 : 28762886 : case COMPONENT_REF:
9514 : 28762886 : if (is_overloaded_fn (t))
9515 : : {
9516 : : /* We can only get here in checking mode via
9517 : : build_non_dependent_expr, because any expression that
9518 : : calls or takes the address of the function will have
9519 : : pulled a FUNCTION_DECL out of the COMPONENT_REF. */
9520 : 6 : gcc_checking_assert (ctx->quiet || errorcount);
9521 : 6 : *non_constant_p = true;
9522 : 6 : return t;
9523 : : }
9524 : 28762880 : r = cxx_eval_component_reference (ctx, t, lval,
9525 : : non_constant_p, overflow_p,
9526 : : jump_target);
9527 : 28762880 : break;
9528 : :
9529 : 49 : case BIT_FIELD_REF:
9530 : 49 : r = cxx_eval_bit_field_ref (ctx, t, lval,
9531 : : non_constant_p, overflow_p,
9532 : : jump_target);
9533 : 49 : break;
9534 : :
9535 : 8056654 : case COND_EXPR:
9536 : 8056654 : case IF_STMT:
9537 : 8056654 : if (*jump_target)
9538 : : {
9539 : 152144 : tree orig_jump = *jump_target;
9540 : 152144 : tree arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 1))
9541 : 304288 : ? TREE_OPERAND (t, 1) : void_node);
9542 : : /* When jumping to a label, the label might be either in the
9543 : : then or else blocks, so process then block first in skipping
9544 : : mode first, and if we are still in the skipping mode at its end,
9545 : : process the else block too. */
9546 : 152144 : r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p,
9547 : : overflow_p, jump_target);
9548 : : /* It's possible that we found the label in the then block. But
9549 : : it could have been followed by another jumping statement, e.g.
9550 : : say we're looking for case 1:
9551 : : if (cond)
9552 : : {
9553 : : // skipped statements
9554 : : case 1:; // clears up *jump_target
9555 : : return 1; // and sets it to a RETURN_EXPR
9556 : : }
9557 : : else { ... }
9558 : : in which case we need not go looking to the else block.
9559 : : (goto is not allowed in a constexpr function.) */
9560 : 152144 : if (*jump_target == orig_jump)
9561 : : {
9562 : 152183 : arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 2))
9563 : 152183 : ? TREE_OPERAND (t, 2) : void_node);
9564 : 152114 : r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p,
9565 : : overflow_p, jump_target);
9566 : : }
9567 : : break;
9568 : : }
9569 : 7904510 : r = cxx_eval_conditional_expression (ctx, t, lval,
9570 : : non_constant_p, overflow_p,
9571 : : jump_target);
9572 : 7904510 : break;
9573 : 866 : case VEC_COND_EXPR:
9574 : 866 : r = cxx_eval_vector_conditional_expression (ctx, t, non_constant_p,
9575 : : overflow_p, jump_target);
9576 : 866 : break;
9577 : :
9578 : 12945622 : case CONSTRUCTOR:
9579 : 12945622 : if (TREE_CONSTANT (t) && reduced_constant_expression_p (t))
9580 : : {
9581 : : /* Don't re-process a constant CONSTRUCTOR. */
9582 : 11693365 : verify_constructor_flags (t);
9583 : 11693365 : if (TREE_CONSTANT (t))
9584 : : return t;
9585 : : }
9586 : 1252257 : r = cxx_eval_bare_aggregate (ctx, t, lval,
9587 : : non_constant_p, overflow_p, jump_target);
9588 : 1252257 : break;
9589 : :
9590 : 493 : case VEC_INIT_EXPR:
9591 : : /* We can get this in a defaulted constructor for a class with a
9592 : : non-static data member of array type. Either the initializer will
9593 : : be NULL, meaning default-initialization, or it will be an lvalue
9594 : : or xvalue of the same type, meaning direct-initialization from the
9595 : : corresponding member. */
9596 : 493 : r = cxx_eval_vec_init (ctx, t, lval,
9597 : : non_constant_p, overflow_p, jump_target);
9598 : 493 : break;
9599 : :
9600 : 16 : case VEC_PERM_EXPR:
9601 : 16 : r = cxx_eval_trinary_expression (ctx, t, lval,
9602 : : non_constant_p, overflow_p,
9603 : : jump_target);
9604 : 16 : break;
9605 : :
9606 : 4 : case PAREN_EXPR:
9607 : 4 : gcc_assert (!REF_PARENTHESIZED_P (t));
9608 : : /* A PAREN_EXPR resulting from __builtin_assoc_barrier has no effect in
9609 : : constant expressions since it's unaffected by -fassociative-math. */
9610 : 4 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
9611 : : non_constant_p, overflow_p,
9612 : : jump_target);
9613 : 4 : break;
9614 : :
9615 : 164299831 : case NOP_EXPR:
9616 : 164299831 : if (REINTERPRET_CAST_P (t))
9617 : : {
9618 : 67 : if (!ctx->quiet)
9619 : 0 : error_at (loc,
9620 : : "%<reinterpret_cast%> is not a constant expression");
9621 : 67 : *non_constant_p = true;
9622 : 67 : return t;
9623 : : }
9624 : : /* FALLTHROUGH. */
9625 : 194440671 : case CONVERT_EXPR:
9626 : 194440671 : case VIEW_CONVERT_EXPR:
9627 : 194440671 : case UNARY_PLUS_EXPR:
9628 : 194440671 : {
9629 : 194440671 : tree oldop = TREE_OPERAND (t, 0);
9630 : :
9631 : 194440671 : tree op = cxx_eval_constant_expression (ctx, oldop,
9632 : 194440671 : VOID_TYPE_P (TREE_TYPE (t))
9633 : : ? vc_discard
9634 : : : tcode == VIEW_CONVERT_EXPR
9635 : 183718301 : ? lval : vc_prvalue,
9636 : : non_constant_p, overflow_p,
9637 : : jump_target);
9638 : 194437974 : if (*jump_target)
9639 : : return NULL_TREE;
9640 : 194437642 : if (*non_constant_p)
9641 : : return t;
9642 : 144930556 : tree type = TREE_TYPE (t);
9643 : :
9644 : 144930556 : if (VOID_TYPE_P (type))
9645 : 10150752 : return void_node;
9646 : :
9647 : 134779804 : if (TREE_CODE (t) == CONVERT_EXPR
9648 : 11542680 : && ARITHMETIC_TYPE_P (type)
9649 : 2264819 : && INDIRECT_TYPE_P (TREE_TYPE (op))
9650 : 134802905 : && ctx->strict)
9651 : : {
9652 : 23011 : if (!ctx->quiet)
9653 : 54 : error_at (loc,
9654 : : "conversion from pointer type %qT to arithmetic type "
9655 : 54 : "%qT in a constant expression", TREE_TYPE (op), type);
9656 : 23011 : *non_constant_p = true;
9657 : 23011 : return t;
9658 : : }
9659 : :
9660 : : /* [expr.const]: a conversion from type cv void* to a pointer-to-object
9661 : : type cannot be part of a core constant expression as a resolution to
9662 : : DR 1312. */
9663 : 36962245 : if (TYPE_PTROB_P (type)
9664 : 35754960 : && TYPE_PTR_P (TREE_TYPE (op))
9665 : 26463643 : && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (op)))
9666 : : /* Inside a call to std::construct_at,
9667 : : std::allocator<T>::{,de}allocate, or
9668 : : std::source_location::current, we permit casting from void*
9669 : : because that is compiler-generated code. */
9670 : 46450 : && !is_std_construct_at (ctx->call)
9671 : 12064 : && !is_std_allocator_allocate (ctx->call)
9672 : 134767932 : && !is_std_source_location_current (ctx->call))
9673 : : {
9674 : : /* Likewise, don't error when casting from void* when OP is
9675 : : &heap uninit and similar. */
9676 : 10980 : tree sop = tree_strip_nop_conversions (op);
9677 : 10980 : tree decl = NULL_TREE;
9678 : 10980 : if (TREE_CODE (sop) == ADDR_EXPR)
9679 : 8206 : decl = TREE_OPERAND (sop, 0);
9680 : 8206 : if (decl
9681 : 8206 : && VAR_P (decl)
9682 : 8159 : && DECL_ARTIFICIAL (decl)
9683 : 16328 : && (DECL_NAME (decl) == heap_identifier
9684 : 4809 : || DECL_NAME (decl) == heap_uninit_identifier
9685 : 791 : || DECL_NAME (decl) == heap_vec_identifier
9686 : 545 : || DECL_NAME (decl) == heap_vec_uninit_identifier))
9687 : : /* OK */;
9688 : : /* P2738 (C++26): a conversion from a prvalue P of type "pointer to
9689 : : cv void" to a pointer-to-object type T unless P is a null
9690 : : pointer value or points to an object whose type is similar to
9691 : : T. */
9692 : 2874 : else if (cxx_dialect > cxx23)
9693 : : {
9694 : 2758 : if (integer_zerop (sop))
9695 : 30 : return build_int_cst (type, 0);
9696 : 2728 : r = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (type), sop,
9697 : : NULL, jump_target);
9698 : 2728 : if (*jump_target)
9699 : : return NULL_TREE;
9700 : 2728 : if (r)
9701 : : {
9702 : 2699 : r = build1 (ADDR_EXPR, type, r);
9703 : 2699 : break;
9704 : : }
9705 : 29 : if (!ctx->quiet)
9706 : : {
9707 : 3 : gcc_assert (TREE_CODE (sop) == ADDR_EXPR);
9708 : 3 : auto_diagnostic_group d;
9709 : 3 : error_at (loc, "cast from %qT is not allowed in a "
9710 : : "constant expression because "
9711 : : "pointed-to type %qT is not similar to %qT",
9712 : 3 : TREE_TYPE (op), TREE_TYPE (TREE_TYPE (sop)),
9713 : 3 : TREE_TYPE (type));
9714 : 3 : tree obj = build_fold_indirect_ref (sop);
9715 : 3 : if (TREE_CODE (obj) == COMPONENT_REF)
9716 : 2 : obj = TREE_OPERAND (obj, 1);
9717 : 3 : if (DECL_P (obj))
9718 : 3 : inform (DECL_SOURCE_LOCATION (obj),
9719 : : "pointed-to object declared here");
9720 : 3 : }
9721 : 29 : *non_constant_p = true;
9722 : 29 : return t;
9723 : : }
9724 : : else
9725 : : {
9726 : 116 : if (!ctx->quiet)
9727 : 26 : error_at (loc, "cast from %qT is not allowed in a "
9728 : : "constant expression before C++26",
9729 : 26 : TREE_TYPE (op));
9730 : 116 : *non_constant_p = true;
9731 : 116 : return t;
9732 : : }
9733 : : }
9734 : :
9735 : 134753919 : if (TREE_CODE (op) == PTRMEM_CST && !TYPE_PTRMEM_P (type))
9736 : : {
9737 : 1685 : op = cplus_expand_constant (op);
9738 : 1685 : if (TREE_CODE (op) == PTRMEM_CST)
9739 : : {
9740 : 21 : if (!ctx->quiet)
9741 : 3 : error_at (loc, "%qE is not a constant expression when the "
9742 : : "class %qT is still incomplete", op,
9743 : 3 : PTRMEM_CST_CLASS (op));
9744 : 21 : *non_constant_p = true;
9745 : 21 : return t;
9746 : : }
9747 : : }
9748 : :
9749 : 134753898 : if (TREE_CODE (op) == PTRMEM_CST && tcode == NOP_EXPR)
9750 : : {
9751 : 795 : if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (op))
9752 : 795 : && !can_convert_qual (type, op))
9753 : 6 : op = cplus_expand_constant (op);
9754 : 795 : return cp_fold_convert (type, op);
9755 : : }
9756 : :
9757 : 134753103 : if (INDIRECT_TYPE_P (type) && TREE_CODE (op) == INTEGER_CST)
9758 : : {
9759 : 317838 : if (integer_zerop (op))
9760 : : {
9761 : 259458 : if (TYPE_REF_P (type))
9762 : : {
9763 : 32 : if (!ctx->quiet)
9764 : 4 : error_at (loc, "dereferencing a null pointer");
9765 : 32 : *non_constant_p = true;
9766 : 32 : return t;
9767 : : }
9768 : : }
9769 : 58380 : else if (TYPE_PTR_P (type)
9770 : 58380 : && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
9771 : : /* INTEGER_CST with pointer-to-method type is only used
9772 : : for a virtual method in a pointer to member function.
9773 : : Don't reject those. */
9774 : : ;
9775 : : else
9776 : : {
9777 : : /* This detects for example:
9778 : : reinterpret_cast<void*>(sizeof 0)
9779 : : */
9780 : 58377 : if (!ctx->quiet)
9781 : 21 : error_at (loc, "%<reinterpret_cast<%T>(%E)%> is not "
9782 : : "a constant expression",
9783 : : type, op);
9784 : 58377 : *non_constant_p = true;
9785 : 58377 : return t;
9786 : : }
9787 : : }
9788 : :
9789 : 134694694 : if (INDIRECT_TYPE_P (type)
9790 : 57760413 : && TREE_CODE (op) == NOP_EXPR
9791 : 28767541 : && TREE_TYPE (op) == ptr_type_node
9792 : 45935 : && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR
9793 : 44413 : && VAR_P (TREE_OPERAND (TREE_OPERAND (op, 0), 0))
9794 : 134717170 : && (DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0),
9795 : 22476 : 0)) == heap_uninit_identifier
9796 : 17533 : || DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0),
9797 : 17533 : 0)) == heap_vec_uninit_identifier))
9798 : : {
9799 : 5472 : tree var = TREE_OPERAND (TREE_OPERAND (op, 0), 0);
9800 : 5472 : tree var_size = TYPE_SIZE_UNIT (TREE_TYPE (var));
9801 : 5472 : tree elt_type = TREE_TYPE (type);
9802 : 5472 : tree cookie_size = NULL_TREE;
9803 : 5472 : tree arg_size = NULL_TREE;
9804 : 5472 : if (TREE_CODE (elt_type) == RECORD_TYPE
9805 : 5472 : && TYPE_IDENTIFIER (elt_type) == heap_identifier)
9806 : : {
9807 : 5 : tree fld1 = TYPE_FIELDS (elt_type);
9808 : 5 : tree fld2 = DECL_CHAIN (fld1);
9809 : 5 : elt_type = TREE_TYPE (TREE_TYPE (fld2));
9810 : 5 : cookie_size = TYPE_SIZE_UNIT (TREE_TYPE (fld1));
9811 : : }
9812 : 5472 : DECL_NAME (var)
9813 : 5472 : = (DECL_NAME (var) == heap_uninit_identifier
9814 : 5472 : ? heap_identifier : heap_vec_identifier);
9815 : : /* For zero sized elt_type, try to recover how many outer_nelts
9816 : : it should have. */
9817 : 5472 : if ((cookie_size ? tree_int_cst_equal (var_size, cookie_size)
9818 : 5467 : : integer_zerop (var_size))
9819 : 28 : && !int_size_in_bytes (elt_type)
9820 : 9 : && TREE_CODE (oldop) == CALL_EXPR
9821 : 5486 : && call_expr_nargs (oldop) >= 1)
9822 : 9 : if (tree fun = get_function_named_in_call (oldop))
9823 : 9 : if (cxx_replaceable_global_alloc_fn (fun)
9824 : 9 : && IDENTIFIER_NEW_OP_P (DECL_NAME (fun)))
9825 : 9 : arg_size = CALL_EXPR_ARG (oldop, 0);
9826 : 5472 : tree new_type
9827 : 5472 : = build_new_constexpr_heap_type (ctx, elt_type, cookie_size,
9828 : : var_size, arg_size,
9829 : : non_constant_p, overflow_p,
9830 : : jump_target);
9831 : 5472 : if (*jump_target)
9832 : : return NULL_TREE;
9833 : 5472 : TREE_TYPE (var) = new_type;
9834 : 5472 : TREE_TYPE (TREE_OPERAND (op, 0))
9835 : 10944 : = build_pointer_type (TREE_TYPE (var));
9836 : : }
9837 : :
9838 : 134694694 : if (op == oldop && tcode != UNARY_PLUS_EXPR)
9839 : : /* We didn't fold at the top so we could check for ptr-int
9840 : : conversion. */
9841 : 11478194 : return fold (t);
9842 : :
9843 : 123216500 : tree sop;
9844 : :
9845 : : /* Handle an array's bounds having been deduced after we built
9846 : : the wrapping expression. */
9847 : 123216500 : if (same_type_ignoring_tlq_and_bounds_p (type, TREE_TYPE (op)))
9848 : : r = op;
9849 : 35015553 : else if (sop = tree_strip_nop_conversions (op),
9850 : 48365572 : sop != op && (same_type_ignoring_tlq_and_bounds_p
9851 : 13350019 : (type, TREE_TYPE (sop))))
9852 : : r = sop;
9853 : 28501094 : else if (tcode == UNARY_PLUS_EXPR)
9854 : 0 : r = fold_convert (TREE_TYPE (t), op);
9855 : : else
9856 : 28501094 : r = fold_build1 (tcode, type, op);
9857 : :
9858 : : /* Conversion of an out-of-range value has implementation-defined
9859 : : behavior; the language considers it different from arithmetic
9860 : : overflow, which is undefined. */
9861 : 123216500 : if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op))
9862 : 11477 : TREE_OVERFLOW (r) = false;
9863 : : }
9864 : : break;
9865 : :
9866 : 7964 : case EXCESS_PRECISION_EXPR:
9867 : 7964 : {
9868 : 7964 : tree oldop = TREE_OPERAND (t, 0);
9869 : :
9870 : 7964 : tree op = cxx_eval_constant_expression (ctx, oldop,
9871 : : lval,
9872 : : non_constant_p, overflow_p,
9873 : : jump_target);
9874 : 7964 : if (*jump_target)
9875 : : return NULL_TREE;
9876 : 7964 : if (*non_constant_p)
9877 : : return t;
9878 : 7960 : r = fold_convert (TREE_TYPE (t), op);
9879 : 7960 : break;
9880 : : }
9881 : :
9882 : 36833 : case EMPTY_CLASS_EXPR:
9883 : : /* Handle EMPTY_CLASS_EXPR produced by build_call_a by lowering
9884 : : it to an appropriate CONSTRUCTOR. */
9885 : 36833 : return build_constructor (TREE_TYPE (t), NULL);
9886 : :
9887 : 18405692 : case STATEMENT_LIST:
9888 : 18405692 : new_ctx = *ctx;
9889 : 18405692 : new_ctx.ctor = new_ctx.object = NULL_TREE;
9890 : 18405692 : return cxx_eval_statement_list (&new_ctx, t,
9891 : 18405692 : non_constant_p, overflow_p, jump_target);
9892 : :
9893 : 7778568 : case BIND_EXPR:
9894 : : /* Pre-emptively clear the vars declared by this BIND_EXPR from the value
9895 : : map, so that when checking whether they're already destroyed later we
9896 : : don't get confused by remnants of previous calls. */
9897 : 11022933 : for (tree decl = BIND_EXPR_VARS (t); decl; decl = DECL_CHAIN (decl))
9898 : 3244365 : ctx->global->clear_value (decl);
9899 : 7778568 : r = cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t),
9900 : : lval,
9901 : : non_constant_p, overflow_p,
9902 : : jump_target);
9903 : 11022933 : for (tree decl = BIND_EXPR_VARS (t); decl; decl = DECL_CHAIN (decl))
9904 : 3244365 : destroy_value_checked (ctx, decl, non_constant_p);
9905 : : break;
9906 : :
9907 : 2219914 : case PREINCREMENT_EXPR:
9908 : 2219914 : case POSTINCREMENT_EXPR:
9909 : 2219914 : case PREDECREMENT_EXPR:
9910 : 2219914 : case POSTDECREMENT_EXPR:
9911 : 2219914 : return cxx_eval_increment_expression (ctx, t,
9912 : : lval, non_constant_p, overflow_p,
9913 : 2219914 : jump_target);
9914 : :
9915 : 684 : case THROW_EXPR:
9916 : 684 : if (cxx_dialect >= cxx26)
9917 : 356 : return cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
9918 : : non_constant_p, overflow_p,
9919 : 356 : jump_target);
9920 : : /* FALLTHROUGH */
9921 : 336 : case LAMBDA_EXPR:
9922 : 336 : case NEW_EXPR:
9923 : 336 : case VEC_NEW_EXPR:
9924 : 336 : case DELETE_EXPR:
9925 : 336 : case VEC_DELETE_EXPR:
9926 : 336 : case MODOP_EXPR:
9927 : : /* GCC internal stuff. */
9928 : 336 : case VA_ARG_EXPR:
9929 : 336 : case BASELINK:
9930 : 336 : case OFFSET_REF:
9931 : 336 : if (!ctx->quiet)
9932 : 86 : error_at (loc, "expression %qE is not a constant expression", t);
9933 : 336 : *non_constant_p = true;
9934 : 336 : break;
9935 : :
9936 : 113517 : case OBJ_TYPE_REF:
9937 : : /* Virtual function lookup. We don't need to do anything fancy. */
9938 : 113517 : return cxx_eval_constant_expression (ctx, OBJ_TYPE_REF_EXPR (t),
9939 : : lval, non_constant_p, overflow_p,
9940 : 113517 : jump_target);
9941 : :
9942 : 16121 : case PLACEHOLDER_EXPR:
9943 : : /* Use of the value or address of the current object. */
9944 : 16121 : if (tree ctor = lookup_placeholder (ctx, lval, TREE_TYPE (t)))
9945 : : {
9946 : 15889 : if (TREE_CODE (ctor) == CONSTRUCTOR)
9947 : : return ctor;
9948 : : else
9949 : 15589 : return cxx_eval_constant_expression (ctx, ctor, lval,
9950 : : non_constant_p, overflow_p,
9951 : 15589 : jump_target);
9952 : : }
9953 : : /* A placeholder without a referent. We can get here when
9954 : : checking whether NSDMIs are noexcept, or in massage_init_elt;
9955 : : just say it's non-constant for now. */
9956 : 232 : gcc_assert (ctx->quiet);
9957 : 232 : *non_constant_p = true;
9958 : 232 : break;
9959 : :
9960 : 227 : case EXIT_EXPR:
9961 : 227 : {
9962 : 227 : tree cond = TREE_OPERAND (t, 0);
9963 : 227 : cond = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
9964 : : non_constant_p, overflow_p,
9965 : : jump_target);
9966 : 227 : if (*jump_target)
9967 : : return NULL_TREE;
9968 : 227 : VERIFY_CONSTANT (cond);
9969 : 227 : if (integer_nonzerop (cond))
9970 : 64 : *jump_target = t;
9971 : : }
9972 : : break;
9973 : :
9974 : 9 : case GOTO_EXPR:
9975 : 9 : if (breaks (&TREE_OPERAND (t, 0))
9976 : 9 : || continues (&TREE_OPERAND (t, 0)))
9977 : 3 : *jump_target = TREE_OPERAND (t, 0);
9978 : : else
9979 : : {
9980 : 6 : if (!ctx->quiet)
9981 : 1 : error_at (loc, "%<goto%> is not a constant expression");
9982 : 6 : *non_constant_p = true;
9983 : : }
9984 : : break;
9985 : :
9986 : 748607 : case LOOP_EXPR:
9987 : 748607 : case DO_STMT:
9988 : 748607 : case WHILE_STMT:
9989 : 748607 : case FOR_STMT:
9990 : 748607 : cxx_eval_loop_expr (ctx, t,
9991 : : non_constant_p, overflow_p, jump_target);
9992 : 748607 : break;
9993 : :
9994 : 35432 : case SWITCH_EXPR:
9995 : 35432 : case SWITCH_STMT:
9996 : 35432 : cxx_eval_switch_expr (ctx, t,
9997 : : non_constant_p, overflow_p, jump_target);
9998 : 35432 : break;
9999 : :
10000 : 100 : case REQUIRES_EXPR:
10001 : : /* It's possible to get a requires-expression in a constant
10002 : : expression. For example:
10003 : :
10004 : : template<typename T> concept bool C() {
10005 : : return requires (T t) { t; };
10006 : : }
10007 : :
10008 : : template<typename T> requires !C<T>() void f(T);
10009 : :
10010 : : Normalization leaves f with the associated constraint
10011 : : '!requires (T t) { ... }' which is not transformed into
10012 : : a constraint. */
10013 : 100 : if (!processing_template_decl)
10014 : 100 : return evaluate_requires_expr (t);
10015 : : else
10016 : 0 : *non_constant_p = true;
10017 : 0 : return t;
10018 : :
10019 : 7252 : case ANNOTATE_EXPR:
10020 : 7252 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
10021 : : lval,
10022 : : non_constant_p, overflow_p,
10023 : : jump_target);
10024 : 7252 : break;
10025 : :
10026 : 18858 : case USING_STMT:
10027 : 18858 : r = void_node;
10028 : 18858 : break;
10029 : :
10030 : 103 : case ASSERTION_STMT:
10031 : 103 : case PRECONDITION_STMT:
10032 : 103 : case POSTCONDITION_STMT:
10033 : 103 : {
10034 : 103 : contract_semantic semantic = get_contract_semantic (t);
10035 : 103 : if (semantic == CCS_IGNORE)
10036 : : break;
10037 : :
10038 : 87 : if (!cxx_eval_assert (ctx, CONTRACT_CONDITION (t),
10039 : : G_("contract predicate is false in "
10040 : : "constant expression"),
10041 : 87 : EXPR_LOCATION (t), checked_contract_p (semantic),
10042 : : non_constant_p, overflow_p))
10043 : 38 : *non_constant_p = true;
10044 : 87 : r = void_node;
10045 : : }
10046 : 87 : break;
10047 : :
10048 : 1013281 : case TEMPLATE_ID_EXPR:
10049 : 1013281 : {
10050 : : /* We can evaluate template-id that refers to a concept only if
10051 : : the template arguments are non-dependent. */
10052 : 1013281 : gcc_assert (concept_check_p (t));
10053 : :
10054 : 1013281 : if (!value_dependent_expression_p (t)
10055 : 1013281 : && !uid_sensitive_constexpr_evaluation_p ())
10056 : 1013235 : r = evaluate_concept_check (t);
10057 : : else
10058 : 46 : *non_constant_p = true;
10059 : :
10060 : : break;
10061 : : }
10062 : :
10063 : 48 : case ASM_EXPR:
10064 : 48 : if (!ctx->quiet)
10065 : 28 : inline_asm_in_constexpr_error (loc, /*constexpr_fundef_p*/false);
10066 : 48 : *non_constant_p = true;
10067 : 48 : return t;
10068 : :
10069 : 1025 : case BIT_CAST_EXPR:
10070 : 1025 : if (lval)
10071 : : {
10072 : 0 : if (!ctx->quiet)
10073 : 0 : error_at (EXPR_LOCATION (t),
10074 : : "address of a call to %qs is not a constant expression",
10075 : : "__builtin_bit_cast");
10076 : 0 : *non_constant_p = true;
10077 : 0 : return t;
10078 : : }
10079 : 1025 : r = cxx_eval_bit_cast (ctx, t, non_constant_p, overflow_p, jump_target);
10080 : 1025 : break;
10081 : :
10082 : 0 : case OMP_PARALLEL:
10083 : 0 : case OMP_TASK:
10084 : 0 : case OMP_FOR:
10085 : 0 : case OMP_SIMD:
10086 : 0 : case OMP_DISTRIBUTE:
10087 : 0 : case OMP_TASKLOOP:
10088 : 0 : case OMP_LOOP:
10089 : 0 : case OMP_TEAMS:
10090 : 0 : case OMP_TARGET_DATA:
10091 : 0 : case OMP_TARGET:
10092 : 0 : case OMP_SECTIONS:
10093 : 0 : case OMP_ORDERED:
10094 : 0 : case OMP_CRITICAL:
10095 : 0 : case OMP_SINGLE:
10096 : 0 : case OMP_SCAN:
10097 : 0 : case OMP_SCOPE:
10098 : 0 : case OMP_SECTION:
10099 : 0 : case OMP_STRUCTURED_BLOCK:
10100 : 0 : case OMP_MASTER:
10101 : 0 : case OMP_MASKED:
10102 : 0 : case OMP_TASKGROUP:
10103 : 0 : case OMP_TARGET_UPDATE:
10104 : 0 : case OMP_TARGET_ENTER_DATA:
10105 : 0 : case OMP_TARGET_EXIT_DATA:
10106 : 0 : case OMP_ATOMIC:
10107 : 0 : case OMP_ATOMIC_READ:
10108 : 0 : case OMP_ATOMIC_CAPTURE_OLD:
10109 : 0 : case OMP_ATOMIC_CAPTURE_NEW:
10110 : 0 : case OMP_DEPOBJ:
10111 : 0 : case OACC_PARALLEL:
10112 : 0 : case OACC_KERNELS:
10113 : 0 : case OACC_SERIAL:
10114 : 0 : case OACC_DATA:
10115 : 0 : case OACC_HOST_DATA:
10116 : 0 : case OACC_LOOP:
10117 : 0 : case OACC_CACHE:
10118 : 0 : case OACC_DECLARE:
10119 : 0 : case OACC_ENTER_DATA:
10120 : 0 : case OACC_EXIT_DATA:
10121 : 0 : case OACC_UPDATE:
10122 : 0 : if (!ctx->quiet)
10123 : 0 : error_at (EXPR_LOCATION (t),
10124 : : "statement is not a constant expression");
10125 : 0 : *non_constant_p = true;
10126 : 0 : break;
10127 : :
10128 : 0 : default:
10129 : 0 : if (STATEMENT_CODE_P (TREE_CODE (t)))
10130 : : {
10131 : : /* This function doesn't know how to deal with pre-genericize
10132 : : statements; this can only happen with statement-expressions,
10133 : : so for now just fail. */
10134 : 0 : if (!ctx->quiet)
10135 : 0 : error_at (EXPR_LOCATION (t),
10136 : : "statement is not a constant expression");
10137 : : }
10138 : 0 : else if (flag_checking)
10139 : 0 : internal_error ("unexpected expression %qE of kind %s", t,
10140 : : get_tree_code_name (TREE_CODE (t)));
10141 : 0 : *non_constant_p = true;
10142 : 0 : break;
10143 : : }
10144 : :
10145 : 713961893 : if (r == error_mark_node)
10146 : 9155080 : *non_constant_p = true;
10147 : :
10148 : 713961893 : if (*non_constant_p)
10149 : 231837995 : return t;
10150 : : else
10151 : : return r;
10152 : : }
10153 : :
10154 : : /* P0859: A function is needed for constant evaluation if it is a constexpr
10155 : : function that is named by an expression ([basic.def.odr]) that is
10156 : : potentially constant evaluated.
10157 : :
10158 : : So we need to instantiate any constexpr functions mentioned by the
10159 : : expression even if the definition isn't needed for evaluating the
10160 : : expression. */
10161 : :
10162 : : static tree
10163 : 385409037 : instantiate_cx_fn_r (tree *tp, int *walk_subtrees, void */*data*/)
10164 : : {
10165 : 385409037 : if (TREE_CODE (*tp) == FUNCTION_DECL
10166 : 10438454 : && DECL_DECLARED_CONSTEXPR_P (*tp)
10167 : 10335598 : && !DECL_INITIAL (*tp)
10168 : 1822420 : && !trivial_fn_p (*tp)
10169 : 1822417 : && (DECL_TEMPLOID_INSTANTIATION (*tp) || DECL_DEFAULTED_FN (*tp))
10170 : 385409037 : && !uid_sensitive_constexpr_evaluation_p ())
10171 : : {
10172 : 1822357 : ++function_depth;
10173 : 1822357 : if (DECL_TEMPLOID_INSTANTIATION (*tp))
10174 : 1822352 : instantiate_decl (*tp, /*defer_ok*/false, /*expl_inst*/false);
10175 : : else
10176 : 5 : synthesize_method (*tp);
10177 : 1822357 : --function_depth;
10178 : : }
10179 : 383586680 : else if (TREE_CODE (*tp) == CALL_EXPR
10180 : 373268099 : || TREE_CODE (*tp) == AGGR_INIT_EXPR)
10181 : : {
10182 : 10488466 : if (EXPR_HAS_LOCATION (*tp))
10183 : 10488327 : input_location = EXPR_LOCATION (*tp);
10184 : : }
10185 : :
10186 : 385409037 : if (!EXPR_P (*tp))
10187 : 228144505 : *walk_subtrees = 0;
10188 : :
10189 : 385409037 : return NULL_TREE;
10190 : : }
10191 : :
10192 : : static void
10193 : 195496095 : instantiate_constexpr_fns (tree t)
10194 : : {
10195 : 195496095 : location_t loc = input_location;
10196 : 195496095 : cp_walk_tree_without_duplicates (&t, instantiate_cx_fn_r, NULL);
10197 : 195496095 : input_location = loc;
10198 : 195496095 : }
10199 : :
10200 : : /* Look for heap variables in the expression *TP. */
10201 : :
10202 : : static tree
10203 : 133682 : find_heap_var_refs (tree *tp, int *walk_subtrees, void */*data*/)
10204 : : {
10205 : 133682 : if (VAR_P (*tp)
10206 : 133682 : && (DECL_NAME (*tp) == heap_uninit_identifier
10207 : 3151 : || DECL_NAME (*tp) == heap_identifier
10208 : 353 : || DECL_NAME (*tp) == heap_vec_uninit_identifier
10209 : 309 : || DECL_NAME (*tp) == heap_vec_identifier
10210 : 21 : || DECL_NAME (*tp) == heap_deleted_identifier))
10211 : : return *tp;
10212 : :
10213 : 94507 : if (TYPE_P (*tp))
10214 : 0 : *walk_subtrees = 0;
10215 : : return NULL_TREE;
10216 : : }
10217 : :
10218 : : /* Find immediate function decls in *TP if any. */
10219 : :
10220 : : static tree
10221 : 189025263 : find_immediate_fndecl (tree *tp, int */*walk_subtrees*/, void */*data*/)
10222 : : {
10223 : 190409925 : if (TREE_CODE (*tp) == FUNCTION_DECL && DECL_IMMEDIATE_FUNCTION_P (*tp))
10224 : : return *tp;
10225 : 189023639 : if (TREE_CODE (*tp) == PTRMEM_CST
10226 : 7721 : && TREE_CODE (PTRMEM_CST_MEMBER (*tp)) == FUNCTION_DECL
10227 : 189030807 : && DECL_IMMEDIATE_FUNCTION_P (PTRMEM_CST_MEMBER (*tp)))
10228 : : return PTRMEM_CST_MEMBER (*tp);
10229 : : return NULL_TREE;
10230 : : }
10231 : :
10232 : : /* T has TREE_CONSTANT set but has been deemed not a valid C++ constant
10233 : : expression. Return a version of T that has TREE_CONSTANT cleared. */
10234 : :
10235 : : static tree
10236 : 133088 : mark_non_constant (tree t)
10237 : : {
10238 : 133088 : gcc_checking_assert (TREE_CONSTANT (t));
10239 : :
10240 : : /* This isn't actually constant, so unset TREE_CONSTANT.
10241 : : Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires
10242 : : it to be set if it is invariant address, even when it is not
10243 : : a valid C++ constant expression. Wrap it with a NOP_EXPR
10244 : : instead. */
10245 : 133088 : if (EXPR_P (t) && TREE_CODE (t) != ADDR_EXPR)
10246 : 132262 : t = copy_node (t);
10247 : 826 : else if (TREE_CODE (t) == CONSTRUCTOR)
10248 : 236 : t = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (t), t);
10249 : : else
10250 : 590 : t = build_nop (TREE_TYPE (t), t);
10251 : 133088 : TREE_CONSTANT (t) = false;
10252 : 133088 : return t;
10253 : : }
10254 : :
10255 : : /* ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
10256 : : STRICT has the same sense as for constant_value_1: true if we only allow
10257 : : conforming C++ constant expressions, or false if we want a constant value
10258 : : even if it doesn't conform.
10259 : : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
10260 : : per P0595 even when ALLOW_NON_CONSTANT is true.
10261 : : CONSTEXPR_DTOR is true when evaluating the dtor of a constexpr variable.
10262 : : OBJECT must be non-NULL in that case. */
10263 : :
10264 : : static tree
10265 : 369594866 : cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant,
10266 : : bool strict = true,
10267 : : mce_value manifestly_const_eval = mce_unknown,
10268 : : bool constexpr_dtor = false,
10269 : : tree object = NULL_TREE)
10270 : : {
10271 : 369594866 : auto_timevar time (TV_CONSTEXPR);
10272 : :
10273 : 369594866 : bool non_constant_p = false;
10274 : 369594866 : bool overflow_p = false;
10275 : :
10276 : 369594866 : if (BRACE_ENCLOSED_INITIALIZER_P (t))
10277 : : {
10278 : 0 : gcc_checking_assert (allow_non_constant);
10279 : : return t;
10280 : : }
10281 : :
10282 : 369594866 : constexpr_global_ctx global_ctx;
10283 : 369594866 : constexpr_ctx ctx = { &global_ctx, NULL, NULL, NULL, NULL, NULL, NULL,
10284 : : allow_non_constant, strict,
10285 : 369594866 : !allow_non_constant ? mce_true : manifestly_const_eval };
10286 : :
10287 : : /* Turn off -frounding-math for manifestly constant evaluation. */
10288 : 369594866 : warning_sentinel rm (flag_rounding_math,
10289 : 369594866 : ctx.manifestly_const_eval == mce_true);
10290 : 369594866 : tree type = (object
10291 : 369594866 : ? cv_unqualified (TREE_TYPE (object))
10292 : 347089715 : : initialized_type (t));
10293 : 369594866 : tree r = t;
10294 : 369594866 : bool is_consteval = false;
10295 : 369594866 : if (VOID_TYPE_P (type))
10296 : : {
10297 : 3535563 : if (!constexpr_dtor)
10298 : : {
10299 : 3535563 : if (cxx_dialect < cxx20)
10300 : : return t;
10301 : 3201930 : if (TREE_CODE (t) != CALL_EXPR && TREE_CODE (t) != AGGR_INIT_EXPR)
10302 : : return t;
10303 : : /* Calls to immediate functions returning void need to be
10304 : : evaluated. */
10305 : 3201919 : tree fndecl = cp_get_callee_fndecl_nofold (t);
10306 : 6403838 : if (fndecl == NULL_TREE || !DECL_IMMEDIATE_FUNCTION_P (fndecl))
10307 : : return t;
10308 : : else
10309 : : is_consteval = true;
10310 : : }
10311 : : }
10312 : 366059303 : else if (cxx_dialect >= cxx20
10313 : 155582870 : && (TREE_CODE (t) == CALL_EXPR
10314 : 131340815 : || TREE_CODE (t) == AGGR_INIT_EXPR
10315 : 129908101 : || TREE_CODE (t) == TARGET_EXPR))
10316 : : {
10317 : 27392873 : tree x = t;
10318 : 27392873 : if (TREE_CODE (x) == TARGET_EXPR)
10319 : 1718104 : x = TARGET_EXPR_INITIAL (x);
10320 : 27392873 : tree fndecl = cp_get_callee_fndecl_nofold (x);
10321 : 53895234 : if (fndecl && DECL_IMMEDIATE_FUNCTION_P (fndecl))
10322 : : is_consteval = true;
10323 : : /* Don't try to evaluate a std::vector constructor taking an integer, it
10324 : : will fail in the 'if (heap_var)' block below after doing all the work
10325 : : (c++/113835). This will need adjustment if P3554 is accepted. Note
10326 : : that evaluation of e.g. the vector default constructor can succeed, so
10327 : : we don't shortcut all vector constructors. */
10328 : 53004722 : if (fndecl && DECL_CONSTRUCTOR_P (fndecl) && allow_non_constant
10329 : 3834531 : && is_std_class (type, "vector") && call_expr_nargs (x) > 1
10330 : 27397259 : && TREE_CODE (TREE_TYPE (get_nth_callarg (x, 1))) == INTEGER_TYPE)
10331 : : return t;
10332 : : }
10333 : 366058962 : if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
10334 : : {
10335 : : /* In C++14 an NSDMI can participate in aggregate initialization,
10336 : : and can refer to the address of the object being initialized, so
10337 : : we need to pass in the relevant VAR_DECL if we want to do the
10338 : : evaluation in a single pass. The evaluation will dynamically
10339 : : update ctx.values for the VAR_DECL. We use the same strategy
10340 : : for C++11 constexpr constructors that refer to the object being
10341 : : initialized. */
10342 : 23747976 : if (constexpr_dtor)
10343 : : {
10344 : 147 : gcc_assert (object && VAR_P (object));
10345 : 147 : gcc_assert (DECL_DECLARED_CONSTEXPR_P (object));
10346 : 147 : gcc_assert (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object));
10347 : 147 : if (error_operand_p (DECL_INITIAL (object)))
10348 : : return t;
10349 : 136 : ctx.ctor = unshare_expr (DECL_INITIAL (object));
10350 : 136 : TREE_READONLY (ctx.ctor) = false;
10351 : : /* Temporarily force decl_really_constant_value to return false
10352 : : for it, we want to use ctx.ctor for the current value instead. */
10353 : 136 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = false;
10354 : : }
10355 : : else
10356 : : {
10357 : 23747829 : ctx.ctor = build_constructor (type, NULL);
10358 : 23747829 : CONSTRUCTOR_NO_CLEARING (ctx.ctor) = true;
10359 : : }
10360 : 23747965 : if (!object)
10361 : : {
10362 : 13283076 : if (TREE_CODE (t) == CALL_EXPR)
10363 : : {
10364 : : /* If T is calling a constructor to initialize an object, reframe
10365 : : it as an AGGR_INIT_EXPR to avoid trying to modify an object
10366 : : from outside the constant evaluation, which will fail even if
10367 : : the value is actually constant (is_constant_evaluated3.C). */
10368 : 5112965 : tree fn = cp_get_callee_fndecl_nofold (t);
10369 : 10225922 : if (fn && DECL_CONSTRUCTOR_P (fn))
10370 : : {
10371 : 2295277 : object = CALL_EXPR_ARG (t, 0);
10372 : 2295277 : object = build_fold_indirect_ref (object);
10373 : 2295277 : r = build_aggr_init_expr (type, r);
10374 : : }
10375 : : }
10376 : 8170111 : else if (TREE_CODE (t) == TARGET_EXPR)
10377 : 3175378 : object = TARGET_EXPR_SLOT (t);
10378 : 4994733 : else if (TREE_CODE (t) == AGGR_INIT_EXPR)
10379 : 437041 : object = AGGR_INIT_EXPR_SLOT (t);
10380 : : }
10381 : 23747965 : ctx.object = object;
10382 : 23747965 : if (object)
10383 : 16372585 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
10384 : : (type, TREE_TYPE (object)));
10385 : 16372585 : if (object && DECL_P (object))
10386 : 13867042 : global_ctx.put_value (object, ctx.ctor);
10387 : 23747965 : if (TREE_CODE (r) == TARGET_EXPR)
10388 : : /* Avoid creating another CONSTRUCTOR when we expand the
10389 : : TARGET_EXPR. */
10390 : 3233517 : r = TARGET_EXPR_INITIAL (r);
10391 : : }
10392 : :
10393 : 366058951 : auto_vec<tree, 16> cleanups;
10394 : 366058951 : global_ctx.cleanups = &cleanups;
10395 : :
10396 : 366058951 : if (manifestly_const_eval == mce_true)
10397 : 195496095 : instantiate_constexpr_fns (r);
10398 : 366058951 : tree jmp_target = NULL_TREE;
10399 : 366058951 : r = cxx_eval_constant_expression (&ctx, r, vc_prvalue,
10400 : : &non_constant_p, &overflow_p,
10401 : : &jmp_target);
10402 : 366056524 : if (throws (&jmp_target) && !non_constant_p)
10403 : : {
10404 : 270 : if (!ctx.quiet)
10405 : 75 : diagnose_uncaught_exception (input_location, &ctx, jmp_target);
10406 : 270 : non_constant_p = true;
10407 : 270 : jmp_target = NULL_TREE;
10408 : 270 : r = t;
10409 : : }
10410 : 366055984 : else if (!non_constant_p && jmp_target)
10411 : : {
10412 : 24 : non_constant_p = true;
10413 : 24 : if (!ctx.quiet)
10414 : : {
10415 : 3 : if (breaks (&jmp_target))
10416 : 3 : error ("%<break%> outside of a loop or %<switch%>");
10417 : 0 : else if (continues (&jmp_target))
10418 : 0 : error ("%<continue%> outside of a loop");
10419 : 0 : else if (returns (&jmp_target))
10420 : 0 : error ("%<return%> in a statement expression");
10421 : : else
10422 : 0 : gcc_unreachable ();
10423 : : }
10424 : 24 : r = t;
10425 : : }
10426 : :
10427 : : /* If we got a non-simple TARGET_EXPR, the initializer was a sequence
10428 : : of statements, and the result ought to be stored in ctx.ctor. */
10429 : 366056254 : if (r == void_node && !constexpr_dtor && ctx.ctor)
10430 : 0 : r = ctx.ctor;
10431 : :
10432 : 366056254 : unsigned int i;
10433 : 366056254 : tree cleanup;
10434 : 366056254 : jmp_target = NULL_TREE;
10435 : : /* Evaluate the cleanups. */
10436 : 732131717 : FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup)
10437 : 19209 : if (cleanup == NULL_TREE)
10438 : : /* NULL_TREE cleanup is a marker that before it is
10439 : : CLEANUP_EH_ONLY cleanup. Skip the cleanup before it. */
10440 : 20 : --i;
10441 : : else
10442 : 19189 : cxx_eval_constant_expression (&ctx, cleanup, vc_discard,
10443 : : &non_constant_p, &overflow_p,
10444 : : &jmp_target);
10445 : 366056254 : if (throws (&jmp_target) && !non_constant_p)
10446 : : {
10447 : 0 : if (!ctx.quiet)
10448 : 0 : diagnose_uncaught_exception (input_location, &ctx, jmp_target);
10449 : 0 : non_constant_p = true;
10450 : 0 : r = t;
10451 : : }
10452 : :
10453 : : /* Mutable logic is a bit tricky: we want to allow initialization of
10454 : : constexpr variables with mutable members, but we can't copy those
10455 : : members to another constexpr variable. */
10456 : 366056254 : if (!non_constant_p
10457 : 275368951 : && TREE_CODE (r) == CONSTRUCTOR
10458 : 376089278 : && CONSTRUCTOR_MUTABLE_POISON (r))
10459 : : {
10460 : 93 : if (!allow_non_constant)
10461 : 6 : error ("%qE is not a constant expression because it refers to "
10462 : : "mutable subobjects of %qT", t, type);
10463 : 93 : non_constant_p = true;
10464 : : }
10465 : :
10466 : 275368858 : if (!non_constant_p && cxx_dialect >= cxx20
10467 : 641425112 : && !global_ctx.heap_vars.is_empty ())
10468 : : {
10469 : 39699 : tree heap_var = cp_walk_tree_without_duplicates (&r, find_heap_var_refs,
10470 : : NULL);
10471 : 39699 : unsigned int i;
10472 : 39699 : if (heap_var)
10473 : : {
10474 : 39175 : if (!allow_non_constant && !non_constant_p)
10475 : : {
10476 : 11 : if (DECL_LANG_SPECIFIC (heap_var))
10477 : 2 : error ("%qE is not a constant expression because it refers to "
10478 : : "exception object allocated with "
10479 : : "%<__cxa_allocate_exception%>", t);
10480 : : else
10481 : 9 : error ("%qE is not a constant expression because it refers to "
10482 : : "a result of %<operator new%>", t);
10483 : 11 : inform (DECL_SOURCE_LOCATION (heap_var), "allocated here");
10484 : : }
10485 : 39175 : r = t;
10486 : 39175 : non_constant_p = true;
10487 : : }
10488 : 80318 : FOR_EACH_VEC_ELT (global_ctx.heap_vars, i, heap_var)
10489 : : {
10490 : 40619 : if (DECL_NAME (heap_var) != heap_deleted_identifier)
10491 : : {
10492 : 39234 : if (!allow_non_constant && !non_constant_p)
10493 : : {
10494 : 16 : error ("%qE is not a constant expression because allocated "
10495 : : "storage has not been deallocated", t);
10496 : 16 : inform (DECL_SOURCE_LOCATION (heap_var), "allocated here");
10497 : : }
10498 : 39234 : r = t;
10499 : 39234 : non_constant_p = true;
10500 : : }
10501 : : }
10502 : : }
10503 : :
10504 : : /* Check that immediate invocation does not return an expression referencing
10505 : : any immediate function decls. */
10506 : 366056254 : if (!non_constant_p && cxx_dialect >= cxx20)
10507 : 226646752 : if (tree immediate_fndecl
10508 : 113323376 : = cp_walk_tree_without_duplicates (&r, find_immediate_fndecl,
10509 : : NULL))
10510 : : {
10511 : 1729 : if (!allow_non_constant && !non_constant_p)
10512 : : {
10513 : 72 : if (is_consteval)
10514 : 36 : error_at (cp_expr_loc_or_input_loc (t),
10515 : : "immediate evaluation returns address of immediate "
10516 : : "function %qD", immediate_fndecl);
10517 : : else
10518 : 54 : error_at (cp_expr_loc_or_input_loc (t),
10519 : : "constant evaluation returns address of immediate "
10520 : : "function %qD", immediate_fndecl);
10521 : : }
10522 : 1729 : r = t;
10523 : 1729 : non_constant_p = true;
10524 : : }
10525 : :
10526 : 366056254 : if (!non_constant_p && !constexpr_dtor)
10527 : 275327759 : verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p);
10528 : :
10529 : : /* After verify_constant because reduced_constant_expression_p can unset
10530 : : CONSTRUCTOR_NO_CLEARING. */
10531 : 366056254 : if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_NO_CLEARING (r))
10532 : : {
10533 : 61602 : if (!allow_non_constant)
10534 : 40 : error ("%qE is not a constant expression because it refers to "
10535 : : "an incompletely initialized variable", t);
10536 : 61602 : TREE_CONSTANT (r) = false;
10537 : 61602 : non_constant_p = true;
10538 : : }
10539 : :
10540 : 366056254 : if (non_constant_p)
10541 : : /* If we saw something bad, go back to our argument. The wrapping below is
10542 : : only for the cases of TREE_CONSTANT argument or overflow. */
10543 : 92721790 : r = t;
10544 : :
10545 : 366056254 : if (!non_constant_p && overflow_p)
10546 : 215 : non_constant_p = true;
10547 : :
10548 : : /* Unshare the result. */
10549 : 366056254 : bool should_unshare = true;
10550 : 366056254 : if (r == t || (TREE_CODE (t) == TARGET_EXPR
10551 : 1129883 : && TARGET_EXPR_INITIAL (t) == r))
10552 : : should_unshare = false;
10553 : :
10554 : 366056254 : if (non_constant_p && !allow_non_constant)
10555 : 2470 : return error_mark_node;
10556 : 366053784 : else if (non_constant_p && TREE_CONSTANT (r))
10557 : 128767 : r = mark_non_constant (r);
10558 : 365925017 : else if (non_constant_p)
10559 : : return t;
10560 : :
10561 : 273463016 : if (constexpr_dtor)
10562 : : {
10563 : 118 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = true;
10564 : 118 : return r;
10565 : : }
10566 : :
10567 : : /* Check we are not trying to return the wrong type. */
10568 : 273462898 : if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (r)))
10569 : : {
10570 : : /* If so, this is not a constant expression. */
10571 : 131 : if (!allow_non_constant)
10572 : 0 : error ("%qE is not a constant expression because it initializes "
10573 : 0 : "a %qT rather than %qT", t, TREE_TYPE (t), type);
10574 : 131 : return t;
10575 : : }
10576 : :
10577 : 273462767 : if (should_unshare)
10578 : 140036706 : r = unshare_expr (r);
10579 : :
10580 : 273462767 : if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
10581 : : {
10582 : 9381488 : r = adjust_temp_type (type, r);
10583 : 9381488 : if (TREE_CODE (t) == TARGET_EXPR
10584 : 9381488 : && TARGET_EXPR_INITIAL (t) == r)
10585 : : return t;
10586 : 8486307 : else if (TREE_CODE (t) == CONSTRUCTOR || TREE_CODE (t) == CALL_EXPR
10587 : 1121264 : || TREE_CODE (t) == AGGR_INIT_EXPR)
10588 : : /* Don't add a TARGET_EXPR if our argument didn't have one. */;
10589 : 255420 : else if (TREE_CODE (t) == TARGET_EXPR && TARGET_EXPR_CLEANUP (t))
10590 : 26 : r = get_target_expr (r);
10591 : : else
10592 : : {
10593 : 255394 : r = get_target_expr (r, tf_warning_or_error | tf_no_cleanup);
10594 : 255394 : TREE_CONSTANT (r) = true;
10595 : : }
10596 : : }
10597 : :
10598 : 272567586 : if (TREE_CODE (t) == TARGET_EXPR
10599 : 234702 : && TREE_CODE (r) == TARGET_EXPR)
10600 : : {
10601 : : /* Preserve this flag for potential_constant_expression, and the others
10602 : : for good measure. */
10603 : 234641 : TARGET_EXPR_ELIDING_P (r) = TARGET_EXPR_ELIDING_P (t);
10604 : 234641 : TARGET_EXPR_IMPLICIT_P (r) = TARGET_EXPR_IMPLICIT_P (t);
10605 : 234641 : TARGET_EXPR_LIST_INIT_P (r) = TARGET_EXPR_LIST_INIT_P (t);
10606 : 234641 : TARGET_EXPR_DIRECT_INIT_P (r) = TARGET_EXPR_DIRECT_INIT_P (t);
10607 : : }
10608 : :
10609 : : /* Remember the original location if that wouldn't need a wrapper. */
10610 : 272567586 : if (location_t loc = EXPR_LOCATION (t))
10611 : 111230268 : protected_set_expr_location (r, loc);
10612 : :
10613 : 272567586 : return r;
10614 : 369592169 : }
10615 : :
10616 : : /* If T represents a constant expression returns its reduced value.
10617 : : Otherwise return error_mark_node. */
10618 : :
10619 : : tree
10620 : 121310973 : cxx_constant_value (tree t, tree decl /* = NULL_TREE */,
10621 : : tsubst_flags_t complain /* = tf_error */)
10622 : : {
10623 : 121310973 : bool sfinae = !(complain & tf_error);
10624 : 121310973 : tree r = cxx_eval_outermost_constant_expr (t, sfinae, true, mce_true, false, decl);
10625 : 121310973 : if (sfinae && !TREE_CONSTANT (r))
10626 : 1026 : r = error_mark_node;
10627 : 121310973 : return r;
10628 : : }
10629 : :
10630 : : /* Like cxx_constant_value, but used for evaluation of constexpr destructors
10631 : : of constexpr variables. The actual initializer of DECL is not modified. */
10632 : :
10633 : : void
10634 : 147 : cxx_constant_dtor (tree t, tree decl)
10635 : : {
10636 : 147 : cxx_eval_outermost_constant_expr (t, false, true, mce_true, true, decl);
10637 : 147 : }
10638 : :
10639 : : /* Helper routine for fold_simple function. Either return simplified
10640 : : expression T, otherwise NULL_TREE.
10641 : : In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold
10642 : : even if we are within template-declaration. So be careful on call, as in
10643 : : such case types can be undefined. */
10644 : :
10645 : : static tree
10646 : 106129001 : fold_simple_1 (tree t)
10647 : : {
10648 : 106129001 : tree op1;
10649 : 106129001 : enum tree_code code = TREE_CODE (t);
10650 : :
10651 : 106129001 : switch (code)
10652 : : {
10653 : : case INTEGER_CST:
10654 : : case REAL_CST:
10655 : : case VECTOR_CST:
10656 : : case FIXED_CST:
10657 : : case COMPLEX_CST:
10658 : : return t;
10659 : :
10660 : 925379 : case SIZEOF_EXPR:
10661 : 925379 : return fold_sizeof_expr (t);
10662 : :
10663 : 30135964 : case ABS_EXPR:
10664 : 30135964 : case ABSU_EXPR:
10665 : 30135964 : case CONJ_EXPR:
10666 : 30135964 : case REALPART_EXPR:
10667 : 30135964 : case IMAGPART_EXPR:
10668 : 30135964 : case NEGATE_EXPR:
10669 : 30135964 : case BIT_NOT_EXPR:
10670 : 30135964 : case TRUTH_NOT_EXPR:
10671 : 30135964 : case VIEW_CONVERT_EXPR:
10672 : 30135964 : CASE_CONVERT:
10673 : 30135964 : case FLOAT_EXPR:
10674 : 30135964 : case FIX_TRUNC_EXPR:
10675 : 30135964 : case FIXED_CONVERT_EXPR:
10676 : 30135964 : case ADDR_SPACE_CONVERT_EXPR:
10677 : :
10678 : 30135964 : op1 = TREE_OPERAND (t, 0);
10679 : :
10680 : 30135964 : t = const_unop (code, TREE_TYPE (t), op1);
10681 : 30135964 : if (!t)
10682 : : return NULL_TREE;
10683 : :
10684 : 1957795 : if (CONVERT_EXPR_CODE_P (code)
10685 : 1957795 : && TREE_OVERFLOW_P (t) && !TREE_OVERFLOW_P (op1))
10686 : 0 : TREE_OVERFLOW (t) = false;
10687 : : return t;
10688 : :
10689 : : default:
10690 : : return NULL_TREE;
10691 : : }
10692 : : }
10693 : :
10694 : : /* If T is a simple constant expression, returns its simplified value.
10695 : : Otherwise returns T. In contrast to maybe_constant_value we
10696 : : simplify only few operations on constant-expressions, and we don't
10697 : : try to simplify constexpressions. */
10698 : :
10699 : : tree
10700 : 106657078 : fold_simple (tree t)
10701 : : {
10702 : 106657078 : if (processing_template_decl)
10703 : : return t;
10704 : :
10705 : 106129001 : tree r = fold_simple_1 (t);
10706 : 106129001 : if (r)
10707 : : return r;
10708 : :
10709 : : return t;
10710 : : }
10711 : :
10712 : : /* Try folding the expression T to a simple constant.
10713 : : Returns that constant, otherwise returns T. */
10714 : :
10715 : : tree
10716 : 971973 : fold_to_constant (tree t)
10717 : : {
10718 : 971973 : if (processing_template_decl)
10719 : : return t;
10720 : :
10721 : 875338 : tree r = fold (t);
10722 : 875338 : if (CONSTANT_CLASS_P (r) && !TREE_OVERFLOW (r))
10723 : : return r;
10724 : : else
10725 : : return t;
10726 : : }
10727 : :
10728 : : /* If T is a constant expression, returns its reduced value.
10729 : : Otherwise, if T does not have TREE_CONSTANT set, returns T.
10730 : : Otherwise, returns a version of T without TREE_CONSTANT.
10731 : : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated
10732 : : as per P0595. */
10733 : :
10734 : : static GTY((deletable)) hash_map<tree, tree> *cv_cache;
10735 : :
10736 : : tree
10737 : 538351859 : maybe_constant_value (tree t, tree decl /* = NULL_TREE */,
10738 : : mce_value manifestly_const_eval /* = mce_unknown */)
10739 : : {
10740 : 538351859 : tree orig_t = t;
10741 : 538351859 : tree r;
10742 : :
10743 : 538351859 : if (EXPR_P (t) && manifestly_const_eval == mce_unknown)
10744 : : {
10745 : : /* Look up each operand in the cv_cache first to see if we've already
10746 : : reduced it, and reuse that result to avoid quadratic behavior if
10747 : : we're called when building up a large expression. */
10748 : 197453856 : int n = cp_tree_operand_length (t);
10749 : 197453856 : tree *ops = XALLOCAVEC (tree, n);
10750 : 197453856 : bool rebuild = false;
10751 : 561380783 : for (int i = 0; i < n; ++i)
10752 : : {
10753 : 363926927 : ops[i] = TREE_OPERAND (t, i);
10754 : 1090817745 : if (tree *cached = hash_map_safe_get (cv_cache, ops[i]))
10755 : 26434331 : if (*cached != ops[i])
10756 : : {
10757 : 7684915 : ops[i] = *cached;
10758 : 7684915 : rebuild = true;
10759 : : }
10760 : : }
10761 : 197453856 : if (rebuild)
10762 : : {
10763 : 6674016 : t = copy_node (t);
10764 : 21743981 : for (int i = 0; i < n; ++i)
10765 : 15069965 : TREE_OPERAND (t, i) = ops[i];
10766 : : }
10767 : : }
10768 : :
10769 : 538351859 : if (!is_nondependent_constant_expression (t))
10770 : : {
10771 : 0 : if (TREE_OVERFLOW_P (t)
10772 : 118092417 : || (!processing_template_decl && TREE_CONSTANT (t)))
10773 : 4321 : t = mark_non_constant (t);
10774 : 118092417 : return t;
10775 : : }
10776 : 420259442 : else if (CONSTANT_CLASS_P (t))
10777 : : /* No caching or evaluation needed. */
10778 : : return t;
10779 : :
10780 : : /* Don't constant evaluate an unevaluated non-manifestly-constant operand,
10781 : : but at least try folding it to a simple constant. */
10782 : 217019734 : if (cp_unevaluated_operand && manifestly_const_eval != mce_true)
10783 : 449115 : return fold_to_constant (t);
10784 : :
10785 : 212135920 : if (manifestly_const_eval != mce_unknown)
10786 : : /* TODO: Extend the cache to be mce_value aware. And if we have a
10787 : : previously cached mce_unknown result that's TREE_CONSTANT, it means
10788 : : the reduced value is independent of mce_value and so we should
10789 : : be able to reuse it in the mce_true/false case. */
10790 : 98263280 : return cxx_eval_outermost_constant_expr (t, true, true,
10791 : 98260583 : manifestly_const_eval, false, decl);
10792 : :
10793 : 118307339 : if (cv_cache == NULL)
10794 : 137601 : cv_cache = hash_map<tree, tree>::create_ggc (101);
10795 : 118307339 : if (tree *cached = cv_cache->get (t))
10796 : : {
10797 : 9127502 : r = *cached;
10798 : 9127502 : if (r != t)
10799 : : {
10800 : : /* Clear processing_template_decl for sake of break_out_target_exprs;
10801 : : entries in the cv_cache are non-templated. */
10802 : 2708111 : processing_template_decl_sentinel ptds;
10803 : :
10804 : 2708111 : r = break_out_target_exprs (r, /*clear_loc*/true);
10805 : 2708111 : protected_set_expr_location (r, EXPR_LOCATION (t));
10806 : 2708111 : }
10807 : 9127502 : return r;
10808 : : }
10809 : :
10810 : 109179837 : uid_sensitive_constexpr_evaluation_checker c;
10811 : 109179837 : r = cxx_eval_outermost_constant_expr (t, true, true,
10812 : : manifestly_const_eval, false, decl);
10813 : 109179837 : gcc_checking_assert (r == t
10814 : : || CONVERT_EXPR_P (t)
10815 : : || TREE_CODE (t) == VIEW_CONVERT_EXPR
10816 : : || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
10817 : : || !cp_tree_equal (r, t));
10818 : 109179837 : if (!c.evaluation_restricted_p ())
10819 : 108981936 : cv_cache->put (orig_t, r);
10820 : : return r;
10821 : : }
10822 : :
10823 : : /* Dispose of the whole CV_CACHE. */
10824 : :
10825 : : static void
10826 : 25099401 : clear_cv_cache (void)
10827 : : {
10828 : 25099401 : if (cv_cache != NULL)
10829 : 24882745 : cv_cache->empty ();
10830 : 25099401 : }
10831 : :
10832 : : /* Dispose of the whole CV_CACHE and FOLD_CACHE. */
10833 : :
10834 : : void
10835 : 25099401 : clear_cv_and_fold_caches ()
10836 : : {
10837 : 25099401 : clear_cv_cache ();
10838 : 25099401 : clear_fold_cache ();
10839 : 25099401 : }
10840 : :
10841 : : /* Internal function handling expressions in templates for
10842 : : fold_non_dependent_expr and fold_non_dependent_init.
10843 : :
10844 : : If we're in a template, but T isn't value dependent, simplify
10845 : : it. We're supposed to treat:
10846 : :
10847 : : template <typename T> void f(T[1 + 1]);
10848 : : template <typename T> void f(T[2]);
10849 : :
10850 : : as two declarations of the same function, for example. */
10851 : :
10852 : : static tree
10853 : 25177668 : fold_non_dependent_expr_template (tree t, tsubst_flags_t complain,
10854 : : bool manifestly_const_eval,
10855 : : tree object)
10856 : : {
10857 : 25177668 : gcc_assert (processing_template_decl);
10858 : :
10859 : 25177668 : if (is_nondependent_constant_expression (t))
10860 : : {
10861 : 13696261 : processing_template_decl_sentinel s;
10862 : 13696261 : t = instantiate_non_dependent_expr_internal (t, complain);
10863 : :
10864 : 13696261 : if (type_unknown_p (t) || BRACE_ENCLOSED_INITIALIZER_P (t))
10865 : : {
10866 : 0 : if (TREE_OVERFLOW_P (t))
10867 : : {
10868 : 0 : t = build_nop (TREE_TYPE (t), t);
10869 : 0 : TREE_CONSTANT (t) = false;
10870 : : }
10871 : 0 : return t;
10872 : : }
10873 : 13696261 : else if (CONSTANT_CLASS_P (t))
10874 : : /* No evaluation needed. */
10875 : : return t;
10876 : :
10877 : : /* Don't constant evaluate an unevaluated non-manifestly-constant operand,
10878 : : but at least try folding it to a simple constant. */
10879 : 3547487 : if (cp_unevaluated_operand && !manifestly_const_eval)
10880 : 89 : return fold_to_constant (t);
10881 : :
10882 : 3547398 : tree r = cxx_eval_outermost_constant_expr (t, true, true,
10883 : : mce_value (manifestly_const_eval),
10884 : : false, object);
10885 : : /* cp_tree_equal looks through NOPs, so allow them. */
10886 : 3547398 : gcc_checking_assert (r == t
10887 : : || CONVERT_EXPR_P (t)
10888 : : || TREE_CODE (t) == VIEW_CONVERT_EXPR
10889 : : || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
10890 : : || !cp_tree_equal (r, t));
10891 : 3547398 : return r;
10892 : 13696261 : }
10893 : 11481407 : else if (TREE_OVERFLOW_P (t))
10894 : : {
10895 : 0 : t = build_nop (TREE_TYPE (t), t);
10896 : 0 : TREE_CONSTANT (t) = false;
10897 : : }
10898 : :
10899 : : return t;
10900 : : }
10901 : :
10902 : : /* Like maybe_constant_value but first fully instantiate the argument.
10903 : :
10904 : : Note: this is equivalent to instantiate_non_dependent_expr (t, complain)
10905 : : followed by maybe_constant_value but is more efficient,
10906 : : because it calls instantiation_dependent_expression_p and
10907 : : potential_constant_expression at most once.
10908 : : The manifestly_const_eval argument is passed to maybe_constant_value.
10909 : :
10910 : : Callers should generally pass their active complain, or if they are in a
10911 : : non-template, diagnosing context, they can use the default of
10912 : : tf_warning_or_error. Callers that might be within a template context, don't
10913 : : have a complain parameter, and aren't going to remember the result for long
10914 : : (e.g. null_ptr_cst_p), can pass tf_none and deal with error_mark_node
10915 : : appropriately. */
10916 : :
10917 : : tree
10918 : 117854364 : fold_non_dependent_expr (tree t,
10919 : : tsubst_flags_t complain /* = tf_warning_or_error */,
10920 : : bool manifestly_const_eval /* = false */,
10921 : : tree object /* = NULL_TREE */)
10922 : : {
10923 : 117854364 : if (t == NULL_TREE)
10924 : : return NULL_TREE;
10925 : :
10926 : 117361229 : if (processing_template_decl)
10927 : 24188622 : return fold_non_dependent_expr_template (t, complain,
10928 : 24188622 : manifestly_const_eval, object);
10929 : :
10930 : 93172607 : return maybe_constant_value (t, object, mce_value (manifestly_const_eval));
10931 : : }
10932 : :
10933 : : /* Like fold_non_dependent_expr, but if EXPR couldn't be folded to a constant,
10934 : : return the original expression. */
10935 : :
10936 : : tree
10937 : 2024614 : maybe_fold_non_dependent_expr (tree expr,
10938 : : tsubst_flags_t complain/*=tf_warning_or_error*/)
10939 : : {
10940 : 2024614 : tree t = fold_non_dependent_expr (expr, complain);
10941 : 2024614 : if (t && TREE_CONSTANT (t))
10942 : 1007961 : return t;
10943 : :
10944 : : return expr;
10945 : : }
10946 : :
10947 : : /* Like maybe_constant_init but first fully instantiate the argument. */
10948 : :
10949 : : tree
10950 : 26453191 : fold_non_dependent_init (tree t,
10951 : : tsubst_flags_t complain /*=tf_warning_or_error*/,
10952 : : bool manifestly_const_eval /*=false*/,
10953 : : tree object /* = NULL_TREE */)
10954 : : {
10955 : 26453191 : if (t == NULL_TREE)
10956 : : return NULL_TREE;
10957 : :
10958 : 26453191 : if (processing_template_decl)
10959 : : {
10960 : 989046 : t = fold_non_dependent_expr_template (t, complain,
10961 : : manifestly_const_eval, object);
10962 : : /* maybe_constant_init does this stripping, so do it here too. */
10963 : 989046 : if (TREE_CODE (t) == TARGET_EXPR)
10964 : : {
10965 : 67 : tree init = TARGET_EXPR_INITIAL (t);
10966 : 67 : if (TREE_CODE (init) == CONSTRUCTOR)
10967 : 989046 : t = init;
10968 : : }
10969 : 989046 : return t;
10970 : : }
10971 : :
10972 : 25464145 : return maybe_constant_init (t, object, manifestly_const_eval);
10973 : : }
10974 : :
10975 : : /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
10976 : : than wrapped in a TARGET_EXPR.
10977 : : ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
10978 : : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
10979 : : per P0595 even when ALLOW_NON_CONSTANT is true. */
10980 : :
10981 : : static tree
10982 : 61122073 : maybe_constant_init_1 (tree t, tree decl, bool allow_non_constant,
10983 : : mce_value manifestly_const_eval)
10984 : : {
10985 : 61122073 : if (!t)
10986 : : return t;
10987 : 61122073 : if (TREE_CODE (t) == EXPR_STMT)
10988 : 23126 : t = TREE_OPERAND (t, 0);
10989 : 61122073 : if (TREE_CODE (t) == CONVERT_EXPR
10990 : 61122073 : && VOID_TYPE_P (TREE_TYPE (t)))
10991 : 33467 : t = TREE_OPERAND (t, 0);
10992 : : /* If the types don't match, the INIT_EXPR is initializing a subobject of
10993 : : DECL and losing that information would cause mischief later. */
10994 : 61122073 : if (TREE_CODE (t) == INIT_EXPR
10995 : 61122073 : && (!decl
10996 : 10356 : || same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (decl),
10997 : 10356 : TREE_TYPE (t))))
10998 : 24070 : t = TREE_OPERAND (t, 1);
10999 : 61122073 : if (TREE_CODE (t) == TARGET_EXPR)
11000 : 432885 : t = TARGET_EXPR_INITIAL (t);
11001 : 61122073 : if (!is_nondependent_static_init_expression (t))
11002 : : /* Don't try to evaluate it. */;
11003 : 52560648 : else if (CONSTANT_CLASS_P (t) && TREE_CODE (t) != PTRMEM_CST)
11004 : : /* No evaluation needed. PTRMEM_CST needs the immediate fn check. */;
11005 : : else
11006 : : {
11007 : : /* [basic.start.static] allows constant-initialization of variables with
11008 : : static or thread storage duration even if it isn't required, but we
11009 : : shouldn't bend the rules the same way for automatic variables.
11010 : :
11011 : : But still enforce the requirements of constexpr/constinit.
11012 : : [dcl.constinit] "If a variable declared with the constinit specifier
11013 : : has dynamic initialization, the program is ill-formed, even if the
11014 : : implementation would perform that initialization as a static
11015 : : initialization." */
11016 : 11192469 : bool is_static = (decl && DECL_P (decl)
11017 : 36275102 : && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)));
11018 : 1668871 : bool strict = (!is_static
11019 : : || (decl && DECL_P (decl)
11020 : 1668871 : && (DECL_DECLARED_CONSTEXPR_P (decl)
11021 : 893572 : || DECL_DECLARED_CONSTINIT_P (decl))));
11022 : : if (is_static)
11023 : : manifestly_const_eval = mce_true;
11024 : :
11025 : 25492165 : if (cp_unevaluated_operand && manifestly_const_eval != mce_true)
11026 : 35490 : return fold_to_constant (t);
11027 : :
11028 : 25456675 : t = cxx_eval_outermost_constant_expr (t, allow_non_constant, strict,
11029 : : manifestly_const_eval,
11030 : : false, decl);
11031 : : }
11032 : 61086583 : if (TREE_CODE (t) == TARGET_EXPR)
11033 : : {
11034 : 19915 : tree init = TARGET_EXPR_INITIAL (t);
11035 : 19915 : if (TREE_CODE (init) == CONSTRUCTOR)
11036 : 61122073 : t = init;
11037 : : }
11038 : : return t;
11039 : : }
11040 : :
11041 : : /* Wrapper for maybe_constant_init_1 which permits non constants. */
11042 : :
11043 : : tree
11044 : 45987140 : maybe_constant_init (tree t, tree decl, bool manifestly_const_eval)
11045 : : {
11046 : 45987140 : return maybe_constant_init_1 (t, decl, true, mce_value (manifestly_const_eval));
11047 : : }
11048 : :
11049 : : tree
11050 : 15133508 : maybe_constant_init (tree t, tree decl, mce_value manifestly_const_eval)
11051 : : {
11052 : 15133508 : return maybe_constant_init_1 (t, decl, true, manifestly_const_eval);
11053 : : }
11054 : :
11055 : : /* Wrapper for maybe_constant_init_1 which does not permit non constants. */
11056 : :
11057 : : tree
11058 : 1425 : cxx_constant_init (tree t, tree decl)
11059 : : {
11060 : 1425 : return maybe_constant_init_1 (t, decl, false, mce_true);
11061 : : }
11062 : :
11063 : : /* Return true if CALL_EXPR T might throw during constant evaluation. */
11064 : :
11065 : : static bool
11066 : 131995040 : callee_might_throw (tree t)
11067 : : {
11068 : 131995040 : if (cxx_dialect < cxx26 || !flag_exceptions)
11069 : : return false;
11070 : 35391179 : tree callee = cp_get_callee (t);
11071 : 35391179 : if (callee == NULL_TREE)
11072 : : return false;
11073 : 35340748 : tree callee_fn = cp_get_fndecl_from_callee (callee, false);
11074 : 35340748 : return (!flag_enforce_eh_specs
11075 : 35340748 : || type_dependent_expression_p (callee)
11076 : 31681165 : || !POINTER_TYPE_P (TREE_TYPE (callee))
11077 : 64418861 : || (!type_noexcept_p (TREE_TYPE (TREE_TYPE (callee)))
11078 : 11497651 : && (callee_fn == NULL_TREE || !TREE_NOTHROW (callee_fn))));
11079 : : }
11080 : :
11081 : : #if 0
11082 : : /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
11083 : : /* Return true if the object referred to by REF has automatic or thread
11084 : : local storage. */
11085 : :
11086 : : enum { ck_ok, ck_bad, ck_unknown };
11087 : : static int
11088 : : check_automatic_or_tls (tree ref)
11089 : : {
11090 : : machine_mode mode;
11091 : : poly_int64 bitsize, bitpos;
11092 : : tree offset;
11093 : : int volatilep = 0, unsignedp = 0;
11094 : : tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset,
11095 : : &mode, &unsignedp, &volatilep, false);
11096 : : duration_kind dk;
11097 : :
11098 : : /* If there isn't a decl in the middle, we don't know the linkage here,
11099 : : and this isn't a constant expression anyway. */
11100 : : if (!DECL_P (decl))
11101 : : return ck_unknown;
11102 : : dk = decl_storage_duration (decl);
11103 : : return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok;
11104 : : }
11105 : : #endif
11106 : :
11107 : : /* Data structure for passing data from potential_constant_expression_1
11108 : : to check_for_return_continue via cp_walk_tree. */
11109 : : struct check_for_return_continue_data {
11110 : : hash_set<tree> *pset;
11111 : : tree continue_stmt;
11112 : : tree break_stmt;
11113 : : bool could_throw;
11114 : : };
11115 : :
11116 : : /* Helper function for potential_constant_expression_1 SWITCH_STMT handling,
11117 : : called through cp_walk_tree. Return the first RETURN_EXPR found, or note
11118 : : the first CONTINUE_STMT and/or BREAK_STMT if RETURN_EXPR is not found.
11119 : : For C++26 also note presence of possibly throwing calls. */
11120 : : static tree
11121 : 21726364 : check_for_return_continue (tree *tp, int *walk_subtrees, void *data)
11122 : : {
11123 : 21726364 : tree t = *tp, s, b;
11124 : 21726364 : check_for_return_continue_data *d = (check_for_return_continue_data *) data;
11125 : 21726364 : switch (TREE_CODE (t))
11126 : : {
11127 : : case RETURN_EXPR:
11128 : : return t;
11129 : :
11130 : 30 : case CONTINUE_STMT:
11131 : 30 : if (d->continue_stmt == NULL_TREE)
11132 : 30 : d->continue_stmt = t;
11133 : : break;
11134 : :
11135 : 5338 : case BREAK_STMT:
11136 : 5338 : if (d->break_stmt == NULL_TREE)
11137 : 3214 : d->break_stmt = t;
11138 : : break;
11139 : :
11140 : : #define RECUR(x) \
11141 : : if (tree r = cp_walk_tree (&x, check_for_return_continue, data, \
11142 : : d->pset)) \
11143 : : return r
11144 : :
11145 : : /* For loops, walk subtrees manually, so that continue stmts found
11146 : : inside of the bodies of the loops are ignored. */
11147 : 12869 : case DO_STMT:
11148 : 12869 : *walk_subtrees = 0;
11149 : 12869 : RECUR (DO_COND (t));
11150 : 12869 : s = d->continue_stmt;
11151 : 12869 : b = d->break_stmt;
11152 : 12869 : RECUR (DO_BODY (t));
11153 : 12869 : d->continue_stmt = s;
11154 : 12869 : d->break_stmt = b;
11155 : 12869 : break;
11156 : :
11157 : 100 : case WHILE_STMT:
11158 : 100 : *walk_subtrees = 0;
11159 : 100 : RECUR (WHILE_COND_PREP (t));
11160 : 100 : RECUR (WHILE_COND (t));
11161 : 100 : s = d->continue_stmt;
11162 : 100 : b = d->break_stmt;
11163 : 100 : RECUR (WHILE_BODY (t));
11164 : 93 : d->continue_stmt = s;
11165 : 93 : d->break_stmt = b;
11166 : 93 : break;
11167 : :
11168 : 101 : case FOR_STMT:
11169 : 101 : *walk_subtrees = 0;
11170 : 101 : RECUR (FOR_INIT_STMT (t));
11171 : 101 : RECUR (FOR_COND_PREP (t));
11172 : 101 : RECUR (FOR_COND (t));
11173 : 101 : RECUR (FOR_EXPR (t));
11174 : 101 : s = d->continue_stmt;
11175 : 101 : b = d->break_stmt;
11176 : 101 : RECUR (FOR_BODY (t));
11177 : 63 : d->continue_stmt = s;
11178 : 63 : d->break_stmt = b;
11179 : 63 : break;
11180 : :
11181 : 0 : case RANGE_FOR_STMT:
11182 : 0 : *walk_subtrees = 0;
11183 : 0 : RECUR (RANGE_FOR_EXPR (t));
11184 : 0 : s = d->continue_stmt;
11185 : 0 : b = d->break_stmt;
11186 : 0 : RECUR (RANGE_FOR_BODY (t));
11187 : 0 : d->continue_stmt = s;
11188 : 0 : d->break_stmt = b;
11189 : 0 : break;
11190 : :
11191 : 207 : case SWITCH_STMT:
11192 : 207 : *walk_subtrees = 0;
11193 : 207 : RECUR (SWITCH_STMT_COND (t));
11194 : 207 : b = d->break_stmt;
11195 : 207 : RECUR (SWITCH_STMT_BODY (t));
11196 : 187 : d->break_stmt = b;
11197 : 187 : break;
11198 : : #undef RECUR
11199 : :
11200 : : case STATEMENT_LIST:
11201 : : case CONSTRUCTOR:
11202 : : break;
11203 : :
11204 : 1054510 : case AGGR_INIT_EXPR:
11205 : 1054510 : case CALL_EXPR:
11206 : : /* In C++26 a function could throw. */
11207 : 1054510 : if (callee_might_throw (t))
11208 : 76294 : d->could_throw = true;
11209 : : break;
11210 : :
11211 : 19956766 : default:
11212 : 19956766 : if (!EXPR_P (t))
11213 : 6266460 : *walk_subtrees = 0;
11214 : : break;
11215 : : }
11216 : :
11217 : : return NULL_TREE;
11218 : : }
11219 : :
11220 : : /* Return true if T denotes a potentially constant expression. Issue
11221 : : diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
11222 : : an lvalue-rvalue conversion is implied. If NOW is true, we want to
11223 : : consider the expression in the current context, independent of constexpr
11224 : : substitution. If FUNDEF_P is true, we're checking a constexpr function body
11225 : : and hard errors should not be reported by constexpr_error.
11226 : :
11227 : : C++0x [expr.const] used to say
11228 : :
11229 : : 6 An expression is a potential constant expression if it is
11230 : : a constant expression where all occurrences of function
11231 : : parameters are replaced by arbitrary constant expressions
11232 : : of the appropriate type.
11233 : :
11234 : : 2 A conditional expression is a constant expression unless it
11235 : : involves one of the following as a potentially evaluated
11236 : : subexpression (3.2), but subexpressions of logical AND (5.14),
11237 : : logical OR (5.15), and conditional (5.16) operations that are
11238 : : not evaluated are not considered. */
11239 : :
11240 : : static bool
11241 : 3076728033 : potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
11242 : : bool fundef_p, tsubst_flags_t flags,
11243 : : tree *jump_target)
11244 : : {
11245 : : #define RECUR(T,RV) \
11246 : : potential_constant_expression_1 ((T), (RV), strict, now, fundef_p, flags, \
11247 : : jump_target)
11248 : :
11249 : 3076728033 : enum { any = false, rval = true };
11250 : 3076728033 : int i;
11251 : 3076728033 : tree tmp;
11252 : :
11253 : 3076728033 : if (t == error_mark_node)
11254 : : return false;
11255 : 3076717430 : if (t == NULL_TREE)
11256 : : return true;
11257 : 3071311523 : location_t loc = cp_expr_loc_or_input_loc (t);
11258 : :
11259 : 3071311523 : if (*jump_target)
11260 : : /* If we are jumping, ignore everything. This is simpler than the
11261 : : cxx_eval_constant_expression handling because we only need to be
11262 : : conservatively correct, and we don't necessarily have a constant value
11263 : : available, so we don't bother with switch tracking. */
11264 : : return true;
11265 : :
11266 : 812896 : if (TREE_THIS_VOLATILE (t) && want_rval
11267 : 278265 : && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (t))
11268 : 3065919598 : && !NULLPTR_TYPE_P (TREE_TYPE (t)))
11269 : : {
11270 : 77398 : if (flags & tf_error)
11271 : 21 : constexpr_error (loc, fundef_p, "lvalue-to-rvalue conversion of "
11272 : : "a volatile lvalue %qE with type %qT", t,
11273 : 21 : TREE_TYPE (t));
11274 : 77398 : return false;
11275 : : }
11276 : 3065764776 : if (CONSTANT_CLASS_P (t))
11277 : : return true;
11278 : 2333071038 : if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED)
11279 : 2333071038 : && TREE_TYPE (t) == error_mark_node)
11280 : : return false;
11281 : :
11282 : 2333071013 : switch (TREE_CODE (t))
11283 : : {
11284 : : case FUNCTION_DECL:
11285 : : case BASELINK:
11286 : : case TEMPLATE_DECL:
11287 : : case OVERLOAD:
11288 : : case TEMPLATE_ID_EXPR:
11289 : : case LABEL_DECL:
11290 : : case CASE_LABEL_EXPR:
11291 : : case PREDICT_EXPR:
11292 : : case CONST_DECL:
11293 : : case SIZEOF_EXPR:
11294 : : case ALIGNOF_EXPR:
11295 : : case OFFSETOF_EXPR:
11296 : : case NOEXCEPT_EXPR:
11297 : : case TEMPLATE_PARM_INDEX:
11298 : : case TRAIT_EXPR:
11299 : : case IDENTIFIER_NODE:
11300 : : case USERDEF_LITERAL:
11301 : : /* We can see a FIELD_DECL in a pointer-to-member expression. */
11302 : : case FIELD_DECL:
11303 : : case RESULT_DECL:
11304 : : case USING_DECL:
11305 : : case USING_STMT:
11306 : : case PLACEHOLDER_EXPR:
11307 : : case REQUIRES_EXPR:
11308 : : case STATIC_ASSERT:
11309 : : case DEBUG_BEGIN_STMT:
11310 : : return true;
11311 : :
11312 : 14980989 : case RETURN_EXPR:
11313 : 14980989 : if (!RECUR (TREE_OPERAND (t, 0), any))
11314 : : return false;
11315 : : /* FALLTHROUGH */
11316 : :
11317 : 14849490 : case BREAK_STMT:
11318 : 14849490 : case CONTINUE_STMT:
11319 : 14849490 : *jump_target = t;
11320 : 14849490 : return true;
11321 : :
11322 : 237272952 : case PARM_DECL:
11323 : 237272952 : if (now && want_rval)
11324 : : {
11325 : 71403362 : tree type = TREE_TYPE (t);
11326 : 71403362 : if (dependent_type_p (type)
11327 : 47438994 : || !COMPLETE_TYPE_P (processing_template_decl
11328 : : ? type : complete_type (type))
11329 : 118842354 : || is_really_empty_class (type, /*ignore_vptr*/false))
11330 : : /* An empty class has no data to read. */
11331 : 23964698 : return true;
11332 : 47438664 : if (flags & tf_error)
11333 : 15 : constexpr_error (input_location, fundef_p,
11334 : : "%qE is not a constant expression", t);
11335 : 47438664 : return false;
11336 : : }
11337 : : return true;
11338 : :
11339 : 178325352 : case AGGR_INIT_EXPR:
11340 : 178325352 : case CALL_EXPR:
11341 : : /* -- an invocation of a function other than a constexpr function
11342 : : or a constexpr constructor. */
11343 : 178325352 : {
11344 : 178325352 : tree fun = get_function_named_in_call (t);
11345 : 178325352 : const int nargs = call_expr_nargs (t);
11346 : 178325352 : i = 0;
11347 : :
11348 : 178325352 : if (fun == NULL_TREE)
11349 : : {
11350 : : /* Reset to allow the function to continue past the end
11351 : : of the block below. Otherwise return early. */
11352 : 97513 : bool bail = true;
11353 : :
11354 : 97513 : if (TREE_CODE (t) == CALL_EXPR
11355 : 97513 : && CALL_EXPR_FN (t) == NULL_TREE)
11356 : 97513 : switch (CALL_EXPR_IFN (t))
11357 : : {
11358 : : /* These should be ignored, they are optimized away from
11359 : : constexpr functions. */
11360 : : case IFN_UBSAN_NULL:
11361 : : case IFN_UBSAN_BOUNDS:
11362 : : case IFN_UBSAN_VPTR:
11363 : : case IFN_FALLTHROUGH:
11364 : : case IFN_ASSUME:
11365 : : return true;
11366 : :
11367 : : case IFN_ADD_OVERFLOW:
11368 : : case IFN_SUB_OVERFLOW:
11369 : : case IFN_MUL_OVERFLOW:
11370 : : case IFN_LAUNDER:
11371 : : case IFN_VEC_CONVERT:
11372 : : bail = false;
11373 : : break;
11374 : :
11375 : : default:
11376 : : break;
11377 : : }
11378 : :
11379 : : if (bail)
11380 : : {
11381 : : /* fold_call_expr can't do anything with IFN calls. */
11382 : 44 : if (flags & tf_error)
11383 : 0 : constexpr_error (loc, fundef_p,
11384 : : "call to internal function %qE", t);
11385 : 44 : return false;
11386 : : }
11387 : : }
11388 : :
11389 : 178227839 : if (fun && is_overloaded_fn (fun))
11390 : : {
11391 : 162242984 : if (!RECUR (fun, true))
11392 : : return false;
11393 : 161834075 : fun = get_fns (fun);
11394 : :
11395 : 161834075 : if (TREE_CODE (fun) == FUNCTION_DECL)
11396 : : {
11397 : 141391601 : if (builtin_valid_in_constant_expr_p (fun))
11398 : : return true;
11399 : 140625131 : if (!maybe_constexpr_fn (fun)
11400 : : /* Allow any built-in function; if the expansion
11401 : : isn't constant, we'll deal with that then. */
11402 : 42098423 : && !fndecl_built_in_p (fun)
11403 : : /* In C++20, replaceable global allocation functions
11404 : : are constant expressions. */
11405 : 30133112 : && (!cxx_replaceable_global_alloc_fn (fun)
11406 : 171889 : || TREE_CODE (t) != CALL_EXPR
11407 : 171889 : || (!CALL_FROM_NEW_OR_DELETE_P (t)
11408 : 70586 : && (current_function_decl == NULL_TREE
11409 : 70586 : || !is_std_allocator_allocate
11410 : 70586 : (current_function_decl))))
11411 : : /* Allow placement new in std::construct_at. */
11412 : 29968236 : && (!cxx_placement_new_fn (fun)
11413 : 50312 : || TREE_CODE (t) != CALL_EXPR
11414 : 50312 : || current_function_decl == NULL_TREE
11415 : 50306 : || !is_std_construct_at (current_function_decl))
11416 : 29935202 : && !cxx_dynamic_cast_fn_p (fun)
11417 : 170557753 : && !cxx_cxa_builtin_fn_p (fun))
11418 : : {
11419 : : /* In C++26 evaluation of the function arguments might
11420 : : throw and in that case it is irrelevant whether
11421 : : fun is constexpr or not. */
11422 : 29903608 : if (cxx_dialect >= cxx26)
11423 : 10291961 : for (; i < nargs; ++i)
11424 : : {
11425 : 6225949 : tree x = get_nth_callarg (t, i);
11426 : 6225949 : bool rv = processing_template_decl ? any : rval;
11427 : 6225949 : bool sub_now = false;
11428 : 6225949 : if (!potential_constant_expression_1 (x, rv, strict,
11429 : : sub_now,
11430 : : fundef_p,
11431 : : flags,
11432 : : jump_target))
11433 : : return false;
11434 : 5685627 : if (throws (jump_target))
11435 : : return true;
11436 : : }
11437 : 29273332 : if ((flags & tf_error)
11438 : 29273332 : && constexpr_error (loc, fundef_p,
11439 : : "call to non-%<constexpr%> "
11440 : : "function %qD", fun))
11441 : 244 : explain_invalid_constexpr_fn (fun);
11442 : 29273332 : return false;
11443 : : }
11444 : : }
11445 : :
11446 : 131163997 : fun = OVL_FIRST (fun);
11447 : : /* Skip initial arguments to base constructors. */
11448 : 131163997 : if (DECL_BASE_CONSTRUCTOR_P (fun))
11449 : 2344153 : i = num_artificial_parms_for (fun);
11450 : : }
11451 : 15984855 : else if (fun)
11452 : : {
11453 : 15984855 : if (TREE_TYPE (fun)
11454 : 15984855 : && FUNCTION_POINTER_TYPE_P (TREE_TYPE (fun)))
11455 : : want_rval = rval;
11456 : : else
11457 : : want_rval = any;
11458 : 15984855 : if (RECUR (fun, want_rval))
11459 : : /* Might end up being a constant function pointer. But it
11460 : : could also be a function object with constexpr op(), so
11461 : : we pass 'any' so that the underlying VAR_DECL is deemed
11462 : : as potentially-constant even though it wasn't declared
11463 : : constexpr. */;
11464 : : else
11465 : : return false;
11466 : : }
11467 : 290092892 : for (; i < nargs; ++i)
11468 : : {
11469 : 159084788 : tree x = get_nth_callarg (t, i);
11470 : : /* In a template, reference arguments haven't been converted to
11471 : : REFERENCE_TYPE and we might not even know if the parameter
11472 : : is a reference, so accept lvalue constants too. */
11473 : 159084788 : bool rv = processing_template_decl ? any : rval;
11474 : : /* Don't require an immediately constant value, as constexpr
11475 : : substitution might not use the value of the argument. */
11476 : 159084788 : bool sub_now = false;
11477 : 159084788 : if (!potential_constant_expression_1 (x, rv, strict,
11478 : : sub_now, fundef_p, flags,
11479 : : jump_target))
11480 : : return false;
11481 : 1887798170 : if (throws (jump_target))
11482 : : return true;
11483 : : }
11484 : : /* In C++26 a function could throw. */
11485 : 131008104 : if (*jump_target == NULL_TREE && callee_might_throw (t))
11486 : 11392659 : *jump_target = void_node;
11487 : : return true;
11488 : : }
11489 : :
11490 : 127722091 : case NON_LVALUE_EXPR:
11491 : : /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
11492 : : -- an lvalue of integral type that refers to a non-volatile
11493 : : const variable or static data member initialized with
11494 : : constant expressions, or
11495 : :
11496 : : -- an lvalue of literal type that refers to non-volatile
11497 : : object defined with constexpr, or that refers to a
11498 : : sub-object of such an object; */
11499 : 127722091 : return RECUR (TREE_OPERAND (t, 0), rval);
11500 : :
11501 : 33264 : case EXCESS_PRECISION_EXPR:
11502 : 33264 : return RECUR (TREE_OPERAND (t, 0), rval);
11503 : :
11504 : 231937143 : case VAR_DECL:
11505 : 231937143 : if (DECL_HAS_VALUE_EXPR_P (t))
11506 : : {
11507 : 3811329 : if (now && is_normal_capture_proxy (t))
11508 : : {
11509 : : /* -- in a lambda-expression, a reference to this or to a
11510 : : variable with automatic storage duration defined outside that
11511 : : lambda-expression, where the reference would be an
11512 : : odr-use. */
11513 : :
11514 : 421391 : if (want_rval)
11515 : : /* Since we're doing an lvalue-rvalue conversion, this might
11516 : : not be an odr-use, so evaluate the variable directly. */
11517 : 420764 : return RECUR (DECL_CAPTURED_VARIABLE (t), rval);
11518 : :
11519 : 627 : if (flags & tf_error)
11520 : : {
11521 : 3 : tree cap = DECL_CAPTURED_VARIABLE (t);
11522 : 3 : auto_diagnostic_group d;
11523 : 3 : if (constexpr_error (input_location, fundef_p,
11524 : : "lambda capture of %qE is not a "
11525 : : "constant expression", cap)
11526 : 3 : && decl_constant_var_p (cap))
11527 : 3 : inform (input_location, "because it is used as a glvalue");
11528 : 3 : }
11529 : 627 : return false;
11530 : : }
11531 : : /* Treat __PRETTY_FUNCTION__ inside a template function as
11532 : : potentially-constant. */
11533 : 6778713 : else if (DECL_PRETTY_FUNCTION_P (t)
11534 : 3504452 : && DECL_VALUE_EXPR (t) == error_mark_node)
11535 : : return true;
11536 : 3389933 : return RECUR (DECL_VALUE_EXPR (t), rval);
11537 : : }
11538 : 228125814 : if (want_rval
11539 : 155715551 : && (now || !var_in_maybe_constexpr_fn (t))
11540 : 146887475 : && !type_dependent_expression_p (t)
11541 : 115907705 : && !decl_maybe_constant_var_p (t)
11542 : 50079081 : && (strict
11543 : 1311479 : || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t))
11544 : 409907 : || (DECL_INITIAL (t)
11545 : 403167 : && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t)))
11546 : 50071572 : && COMPLETE_TYPE_P (TREE_TYPE (t))
11547 : 278182609 : && !is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
11548 : : {
11549 : 50052110 : if (flags & tf_error)
11550 : 160 : non_const_var_error (loc, t, fundef_p);
11551 : 50052110 : return false;
11552 : : }
11553 : : return true;
11554 : :
11555 : 247134255 : case NOP_EXPR:
11556 : 247134255 : if (REINTERPRET_CAST_P (t))
11557 : : {
11558 : 130163 : if (flags & tf_error)
11559 : 47 : constexpr_error (loc, fundef_p, "%<reinterpret_cast%> is not a "
11560 : : "constant expression");
11561 : 130163 : return false;
11562 : : }
11563 : : /* FALLTHRU */
11564 : 571560817 : case CONVERT_EXPR:
11565 : 571560817 : case VIEW_CONVERT_EXPR:
11566 : : /* -- a reinterpret_cast. FIXME not implemented, and this rule
11567 : : may change to something more specific to type-punning (DR 1312). */
11568 : 571560817 : {
11569 : 571560817 : tree from = TREE_OPERAND (t, 0);
11570 : 571560817 : if (location_wrapper_p (t))
11571 : : {
11572 : 285513036 : iloc_sentinel ils = loc;
11573 : 285513036 : return (RECUR (from, want_rval));
11574 : 285513036 : }
11575 : 286047781 : if (INDIRECT_TYPE_P (TREE_TYPE (t)))
11576 : : {
11577 : 148318444 : STRIP_ANY_LOCATION_WRAPPER (from);
11578 : 148318444 : if (TREE_CODE (from) == INTEGER_CST
11579 : 148318444 : && !integer_zerop (from))
11580 : : {
11581 : 1758 : if (flags & tf_error)
11582 : 22 : constexpr_error (loc, fundef_p,
11583 : : "%<reinterpret_cast%> from integer to "
11584 : : "pointer");
11585 : 1758 : return false;
11586 : : }
11587 : : }
11588 : 286046023 : return (RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR));
11589 : : }
11590 : :
11591 : 11685 : case ADDRESSOF_EXPR:
11592 : : /* This is like ADDR_EXPR, except it won't form pointer-to-member. */
11593 : 11685 : t = TREE_OPERAND (t, 0);
11594 : 11685 : goto handle_addr_expr;
11595 : :
11596 : 55382735 : case ADDR_EXPR:
11597 : : /* -- a unary operator & that is applied to an lvalue that
11598 : : designates an object with thread or automatic storage
11599 : : duration; */
11600 : 55382735 : t = TREE_OPERAND (t, 0);
11601 : :
11602 : 55382735 : if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t))
11603 : : /* A pointer-to-member constant. */
11604 : : return true;
11605 : :
11606 : 55394182 : handle_addr_expr:
11607 : : #if 0
11608 : : /* FIXME adjust when issue 1197 is fully resolved. For now don't do
11609 : : any checking here, as we might dereference the pointer later. If
11610 : : we remove this code, also remove check_automatic_or_tls. */
11611 : : i = check_automatic_or_tls (t);
11612 : : if (i == ck_ok)
11613 : : return true;
11614 : : if (i == ck_bad)
11615 : : {
11616 : : if (flags & tf_error)
11617 : : error ("address-of an object %qE with thread local or "
11618 : : "automatic storage is not a constant expression", t);
11619 : : return false;
11620 : : }
11621 : : #endif
11622 : 55394182 : return RECUR (t, any);
11623 : :
11624 : 72225889 : case COMPONENT_REF:
11625 : 72225889 : case ARROW_EXPR:
11626 : 72225889 : case OFFSET_REF:
11627 : : /* -- a class member access unless its postfix-expression is
11628 : : of literal type or of pointer to literal type. */
11629 : : /* This test would be redundant, as it follows from the
11630 : : postfix-expression being a potential constant expression. */
11631 : 72225889 : if (type_unknown_p (t))
11632 : : return true;
11633 : 65898546 : if (is_overloaded_fn (t))
11634 : : /* In a template, a COMPONENT_REF of a function expresses ob.fn(),
11635 : : which uses ob as an lvalue. */
11636 : 67421587 : want_rval = false;
11637 : 67421587 : gcc_fallthrough ();
11638 : :
11639 : 67421587 : case REALPART_EXPR:
11640 : 67421587 : case IMAGPART_EXPR:
11641 : 67421587 : case BIT_FIELD_REF:
11642 : 67421587 : return RECUR (TREE_OPERAND (t, 0), want_rval);
11643 : :
11644 : 201424 : case EXPR_PACK_EXPANSION:
11645 : 201424 : return RECUR (PACK_EXPANSION_PATTERN (t), want_rval);
11646 : :
11647 : : case PACK_INDEX_EXPR:
11648 : : return true;
11649 : :
11650 : 70874943 : case INDIRECT_REF:
11651 : 70874943 : return RECUR (TREE_OPERAND (t, 0), rval);
11652 : :
11653 : 15480688 : case STATEMENT_LIST:
11654 : 55223043 : for (tree stmt : tsi_range (t))
11655 : 40143826 : if (!RECUR (stmt, any))
11656 : 249193556 : return false;
11657 : : return true;
11658 : :
11659 : 3615209 : case MODIFY_EXPR:
11660 : 3615209 : if (cxx_dialect < cxx14)
11661 : 1921 : goto fail;
11662 : 3613288 : if (!RECUR (TREE_OPERAND (t, 0), any))
11663 : : return false;
11664 : : /* Just ignore clobbers. */
11665 : 3190075 : if (TREE_CLOBBER_P (TREE_OPERAND (t, 1)))
11666 : : return true;
11667 : 1567701 : if (!RECUR (TREE_OPERAND (t, 1), rval))
11668 : : return false;
11669 : : return true;
11670 : :
11671 : 286337 : case MODOP_EXPR:
11672 : 286337 : if (cxx_dialect < cxx14)
11673 : 96 : goto fail;
11674 : 286241 : if (!RECUR (TREE_OPERAND (t, 0), rval))
11675 : : return false;
11676 : 274997 : if (!RECUR (TREE_OPERAND (t, 2), rval))
11677 : : return false;
11678 : : return true;
11679 : :
11680 : 279349 : case DO_STMT:
11681 : 279349 : if (!RECUR (DO_COND (t), rval))
11682 : : return false;
11683 : 279349 : if (!RECUR (DO_BODY (t), any))
11684 : : return false;
11685 : 279280 : if (breaks (jump_target) || continues (jump_target))
11686 : 2 : *jump_target = NULL_TREE;
11687 : : return true;
11688 : :
11689 : 215597 : case FOR_STMT:
11690 : 215597 : if (!RECUR (FOR_INIT_STMT (t), any))
11691 : : return false;
11692 : 215597 : if (!RECUR (FOR_COND_PREP (t), any))
11693 : : return false;
11694 : 215597 : tmp = FOR_COND (t);
11695 : 215597 : if (!RECUR (tmp, rval))
11696 : : return false;
11697 : 215197 : if (tmp)
11698 : : {
11699 : 171012 : if (!processing_template_decl)
11700 : 166805 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
11701 : : /* If we couldn't evaluate the condition, it might not ever be
11702 : : true. */
11703 : 171012 : if (!integer_onep (tmp))
11704 : : {
11705 : : /* Before returning true, check if the for body can contain
11706 : : a return. */
11707 : 171012 : hash_set<tree> pset;
11708 : 171012 : check_for_return_continue_data data = { &pset, NULL_TREE,
11709 : 171012 : NULL_TREE, false };
11710 : 171012 : if (tree ret_expr
11711 : 171012 : = cp_walk_tree (&FOR_BODY (t), check_for_return_continue,
11712 : : &data, &pset))
11713 : 100003 : *jump_target = ret_expr;
11714 : 171012 : if (data.could_throw)
11715 : 16084 : *jump_target = void_node;
11716 : 171012 : return true;
11717 : 171012 : }
11718 : : }
11719 : 44185 : if (!RECUR (FOR_EXPR (t), any))
11720 : : return false;
11721 : 44185 : if (!RECUR (FOR_BODY (t), any))
11722 : : return false;
11723 : 44185 : if (!RECUR (FOR_COND_CLEANUP (t), any))
11724 : : return false;
11725 : 44185 : if (breaks (jump_target) || continues (jump_target))
11726 : 3 : *jump_target = NULL_TREE;
11727 : : return true;
11728 : :
11729 : 21 : case RANGE_FOR_STMT:
11730 : 21 : if (!RECUR (RANGE_FOR_INIT_STMT (t), any))
11731 : : return false;
11732 : 21 : if (!RECUR (RANGE_FOR_EXPR (t), any))
11733 : : return false;
11734 : 21 : if (!RECUR (RANGE_FOR_BODY (t), any))
11735 : : return false;
11736 : 17 : if (breaks (jump_target) || continues (jump_target))
11737 : 0 : *jump_target = NULL_TREE;
11738 : : return true;
11739 : :
11740 : 91127 : case WHILE_STMT:
11741 : 91127 : if (!RECUR (WHILE_COND_PREP (t), any))
11742 : : return false;
11743 : 91127 : tmp = WHILE_COND (t);
11744 : 91127 : if (!RECUR (tmp, rval))
11745 : : return false;
11746 : 91006 : if (!processing_template_decl)
11747 : 90990 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
11748 : : /* If we couldn't evaluate the condition, it might not ever be true. */
11749 : 91006 : if (!integer_onep (tmp))
11750 : : {
11751 : : /* Before returning true, check if the while body can contain
11752 : : a return. */
11753 : 88242 : hash_set<tree> pset;
11754 : 88242 : check_for_return_continue_data data = { &pset, NULL_TREE,
11755 : 88242 : NULL_TREE, false };
11756 : 88242 : if (tree ret_expr
11757 : 88242 : = cp_walk_tree (&WHILE_BODY (t), check_for_return_continue,
11758 : : &data, &pset))
11759 : 210 : *jump_target = ret_expr;
11760 : 88242 : if (data.could_throw)
11761 : 5893 : *jump_target = void_node;
11762 : 88242 : return true;
11763 : 88242 : }
11764 : 2764 : if (!RECUR (WHILE_BODY (t), any))
11765 : : return false;
11766 : 2762 : if (!RECUR (WHILE_COND_CLEANUP (t), any))
11767 : : return false;
11768 : 2762 : if (breaks (jump_target) || continues (jump_target))
11769 : 13 : *jump_target = NULL_TREE;
11770 : : return true;
11771 : :
11772 : 20821 : case SWITCH_STMT:
11773 : 20821 : if (!RECUR (SWITCH_STMT_COND (t), rval))
11774 : : return false;
11775 : : /* FIXME we don't check SWITCH_STMT_BODY currently, because even
11776 : : unreachable labels would be checked and it is enough if there is
11777 : : a single switch cond value for which it is a valid constant
11778 : : expression. We need to check if there are any RETURN_EXPRs
11779 : : or CONTINUE_STMTs inside of the body though, as in that case
11780 : : we need to set *jump_target. */
11781 : : else
11782 : : {
11783 : 20815 : hash_set<tree> pset;
11784 : 20815 : check_for_return_continue_data data = { &pset, NULL_TREE,
11785 : 20815 : NULL_TREE, false };
11786 : 20815 : if (tree ret_expr
11787 : 20815 : = cp_walk_tree (&SWITCH_STMT_BODY (t), check_for_return_continue,
11788 : : &data, &pset))
11789 : : /* The switch might return. */
11790 : 20298 : *jump_target = ret_expr;
11791 : 517 : else if (data.continue_stmt)
11792 : : /* The switch can't return, but might continue. */
11793 : 3 : *jump_target = data.continue_stmt;
11794 : 20815 : if (data.could_throw)
11795 : 34 : *jump_target = void_node;
11796 : 20815 : }
11797 : 20815 : return true;
11798 : :
11799 : 44 : case STMT_EXPR:
11800 : 44 : return RECUR (STMT_EXPR_STMT (t), rval);
11801 : :
11802 : 252551 : case LAMBDA_EXPR:
11803 : 252551 : if (cxx_dialect >= cxx17)
11804 : : /* In C++17 lambdas can be constexpr, don't give up yet. */
11805 : : return true;
11806 : 212 : else if (flags & tf_error)
11807 : 0 : constexpr_error (loc, fundef_p, "lambda-expression is not a "
11808 : : "constant expression before C++17");
11809 : : return false;
11810 : :
11811 : 106904 : case NEW_EXPR:
11812 : 106904 : case VEC_NEW_EXPR:
11813 : 106904 : case DELETE_EXPR:
11814 : 106904 : case VEC_DELETE_EXPR:
11815 : 106904 : if (cxx_dialect >= cxx20)
11816 : : /* In C++20, new-expressions are potentially constant. */
11817 : : return true;
11818 : 79523 : else if (flags & tf_error)
11819 : 0 : constexpr_error (loc, fundef_p, "new-expression is not a "
11820 : : "constant expression before C++20");
11821 : : return false;
11822 : :
11823 : 65727 : case DYNAMIC_CAST_EXPR:
11824 : 65727 : case PSEUDO_DTOR_EXPR:
11825 : 65727 : case OMP_PARALLEL:
11826 : 65727 : case OMP_TASK:
11827 : 65727 : case OMP_FOR:
11828 : 65727 : case OMP_SIMD:
11829 : 65727 : case OMP_DISTRIBUTE:
11830 : 65727 : case OMP_TASKLOOP:
11831 : 65727 : case OMP_LOOP:
11832 : 65727 : case OMP_TEAMS:
11833 : 65727 : case OMP_TARGET_DATA:
11834 : 65727 : case OMP_TARGET:
11835 : 65727 : case OMP_SECTIONS:
11836 : 65727 : case OMP_ORDERED:
11837 : 65727 : case OMP_CRITICAL:
11838 : 65727 : case OMP_SINGLE:
11839 : 65727 : case OMP_SCAN:
11840 : 65727 : case OMP_SCOPE:
11841 : 65727 : case OMP_SECTION:
11842 : 65727 : case OMP_MASTER:
11843 : 65727 : case OMP_MASKED:
11844 : 65727 : case OMP_TASKGROUP:
11845 : 65727 : case OMP_TARGET_UPDATE:
11846 : 65727 : case OMP_TARGET_ENTER_DATA:
11847 : 65727 : case OMP_TARGET_EXIT_DATA:
11848 : 65727 : case OMP_ATOMIC:
11849 : 65727 : case OMP_ATOMIC_READ:
11850 : 65727 : case OMP_ATOMIC_CAPTURE_OLD:
11851 : 65727 : case OMP_ATOMIC_CAPTURE_NEW:
11852 : 65727 : case OMP_DEPOBJ:
11853 : 65727 : case OACC_PARALLEL:
11854 : 65727 : case OACC_KERNELS:
11855 : 65727 : case OACC_SERIAL:
11856 : 65727 : case OACC_DATA:
11857 : 65727 : case OACC_HOST_DATA:
11858 : 65727 : case OACC_LOOP:
11859 : 65727 : case OACC_CACHE:
11860 : 65727 : case OACC_DECLARE:
11861 : 65727 : case OACC_ENTER_DATA:
11862 : 65727 : case OACC_EXIT_DATA:
11863 : 65727 : case OACC_UPDATE:
11864 : 65727 : case OMP_ARRAY_SECTION:
11865 : : /* GCC internal stuff. */
11866 : 65727 : case VA_ARG_EXPR:
11867 : 65727 : case TRANSACTION_EXPR:
11868 : 65727 : case AT_ENCODE_EXPR:
11869 : 65727 : fail:
11870 : 65727 : if (flags & tf_error)
11871 : 21 : constexpr_error (loc, fundef_p, "expression %qE is not a constant "
11872 : : "expression", t);
11873 : : return false;
11874 : :
11875 : : case OMP_DECLARE_MAPPER:
11876 : : /* This can be used to initialize VAR_DECLs: it's treated as a magic
11877 : : constant. */
11878 : : return true;
11879 : :
11880 : 450 : case THROW_EXPR:
11881 : 450 : if (cxx_dialect < cxx26)
11882 : 267 : goto fail;
11883 : 183 : return RECUR (TREE_OPERAND (t, 0), rval);
11884 : :
11885 : 482 : case ASM_EXPR:
11886 : 482 : if (flags & tf_error)
11887 : 3 : inline_asm_in_constexpr_error (loc, fundef_p);
11888 : : return false;
11889 : :
11890 : 364533 : case OBJ_TYPE_REF:
11891 : 364533 : if (cxx_dialect >= cxx20)
11892 : : /* In C++20 virtual calls can be constexpr, don't give up yet. */
11893 : : return true;
11894 : 226116 : else if (flags & tf_error)
11895 : 0 : constexpr_error (loc, fundef_p, "virtual functions cannot be "
11896 : : "%<constexpr%> before C++20");
11897 : : return false;
11898 : :
11899 : 6431 : case TYPEID_EXPR:
11900 : : /* In C++20, a typeid expression whose operand is of polymorphic
11901 : : class type can be constexpr. */
11902 : 6431 : {
11903 : 6431 : tree e = TREE_OPERAND (t, 0);
11904 : 6431 : if (cxx_dialect < cxx20
11905 : 513 : && strict
11906 : 513 : && !TYPE_P (e)
11907 : 273 : && !type_dependent_expression_p (e)
11908 : 6437 : && TYPE_POLYMORPHIC_P (TREE_TYPE (e)))
11909 : : {
11910 : 4 : if (flags & tf_error)
11911 : 1 : constexpr_error (loc, fundef_p, "%<typeid%> is not a "
11912 : : "constant expression because %qE is "
11913 : : "of polymorphic type", e);
11914 : 4 : return false;
11915 : : }
11916 : : return true;
11917 : : }
11918 : :
11919 : 25260148 : case POINTER_DIFF_EXPR:
11920 : 25260148 : case MINUS_EXPR:
11921 : 25260148 : want_rval = true;
11922 : 25260148 : goto binary;
11923 : :
11924 : 60108827 : case LT_EXPR:
11925 : 60108827 : case LE_EXPR:
11926 : 60108827 : case GT_EXPR:
11927 : 60108827 : case GE_EXPR:
11928 : 60108827 : case EQ_EXPR:
11929 : 60108827 : case NE_EXPR:
11930 : 60108827 : case SPACESHIP_EXPR:
11931 : 60108827 : want_rval = true;
11932 : 60108827 : goto binary;
11933 : :
11934 : 1320039 : case PREINCREMENT_EXPR:
11935 : 1320039 : case POSTINCREMENT_EXPR:
11936 : 1320039 : case PREDECREMENT_EXPR:
11937 : 1320039 : case POSTDECREMENT_EXPR:
11938 : 1320039 : if (cxx_dialect < cxx14)
11939 : 8258 : goto fail;
11940 : 1311781 : goto unary;
11941 : :
11942 : 1023246 : case BIT_NOT_EXPR:
11943 : : /* A destructor. */
11944 : 1023246 : if (TYPE_P (TREE_OPERAND (t, 0)))
11945 : : return true;
11946 : : /* fall through. */
11947 : :
11948 : 42770796 : case CONJ_EXPR:
11949 : 42770796 : case SAVE_EXPR:
11950 : 42770796 : case FIX_TRUNC_EXPR:
11951 : 42770796 : case FLOAT_EXPR:
11952 : 42770796 : case NEGATE_EXPR:
11953 : 42770796 : case ABS_EXPR:
11954 : 42770796 : case ABSU_EXPR:
11955 : 42770796 : case TRUTH_NOT_EXPR:
11956 : 42770796 : case FIXED_CONVERT_EXPR:
11957 : 42770796 : case UNARY_PLUS_EXPR:
11958 : 42770796 : case UNARY_LEFT_FOLD_EXPR:
11959 : 42770796 : case UNARY_RIGHT_FOLD_EXPR:
11960 : 42770796 : case VEC_DUPLICATE_EXPR:
11961 : 1023246 : unary:
11962 : 42770796 : return RECUR (TREE_OPERAND (t, 0), rval);
11963 : :
11964 : 27468390 : case CAST_EXPR:
11965 : 27468390 : case CONST_CAST_EXPR:
11966 : 27468390 : case STATIC_CAST_EXPR:
11967 : 27468390 : case REINTERPRET_CAST_EXPR:
11968 : 27468390 : case IMPLICIT_CONV_EXPR:
11969 : 27468390 : if (!cast_valid_in_integral_constant_expression_p (TREE_TYPE (t)))
11970 : : /* In C++98, a conversion to non-integral type can't be part of a
11971 : : constant expression. */
11972 : : {
11973 : 293 : if (flags & tf_error)
11974 : 0 : constexpr_error (loc, fundef_p,
11975 : : "cast to non-integral type %qT in a constant "
11976 : 0 : "expression", TREE_TYPE (t));
11977 : 293 : return false;
11978 : : }
11979 : : /* This might be a conversion from a class to a (potentially) literal
11980 : : type. Let's consider it potentially constant since the conversion
11981 : : might be a constexpr user-defined conversion. */
11982 : 27468097 : else if (cxx_dialect >= cxx11
11983 : 27448951 : && (dependent_type_p (TREE_TYPE (t))
11984 : 7779874 : || !COMPLETE_TYPE_P (TREE_TYPE (t))
11985 : 7775259 : || literal_type_p (TREE_TYPE (t)))
11986 : 27431126 : && TREE_OPERAND (t, 0)
11987 : 54698889 : && (TREE_CODE (t) != CAST_EXPR
11988 : 21853772 : || !TREE_CHAIN (TREE_OPERAND (t, 0))))
11989 : : {
11990 : 27199428 : tree from = TREE_OPERAND (t, 0);
11991 : 27199428 : if (TREE_CODE (t) == CAST_EXPR)
11992 : 21822408 : from = TREE_VALUE (from);
11993 : 27199428 : tree type = TREE_TYPE (from);
11994 : : /* If this is a dependent type, it could end up being a class
11995 : : with conversions. */
11996 : 27199428 : if (type == NULL_TREE || WILDCARD_TYPE_P (type))
11997 : : return true;
11998 : : /* Or a non-dependent class which has conversions. */
11999 : 335332 : else if (CLASS_TYPE_P (type)
12000 : 335332 : && (TYPE_HAS_CONVERSION (type) || dependent_scope_p (type)))
12001 : 84442 : return true;
12002 : : }
12003 : :
12004 : 23072299 : return (RECUR (TREE_OPERAND (t, 0),
12005 : 23072299 : !TYPE_REF_P (TREE_TYPE (t))));
12006 : :
12007 : 7906651 : case BIND_EXPR:
12008 : 7906651 : return RECUR (BIND_EXPR_BODY (t), want_rval);
12009 : :
12010 : 38273902 : case CLEANUP_POINT_EXPR:
12011 : 38273902 : case MUST_NOT_THROW_EXPR:
12012 : 38273902 : case TRY_CATCH_EXPR:
12013 : : /* Even for C++26 handle TRY_BLOCK conservatively, if we detect the
12014 : : body could throw, even with catch (...) among handlers we'd need
12015 : : to analyze them in detail if they couldn't rethrow it. More
12016 : : importantly though, throws (jump_target) is just conservative,
12017 : : and there could be e.g.
12018 : : try
12019 : : {
12020 : : possibly_throwing_fn (args);
12021 : : break;
12022 : : }
12023 : : catch (...)
12024 : : {
12025 : : }
12026 : : or continue or return instead of break. So, clearing *jump_target
12027 : : because we see catch (...) handler might mean we missed break
12028 : : etc. */
12029 : 38273902 : case TRY_BLOCK:
12030 : 38273902 : case EH_SPEC_BLOCK:
12031 : 38273902 : case EXPR_STMT:
12032 : 38273902 : case PAREN_EXPR:
12033 : : /* For convenience. */
12034 : 38273902 : case LOOP_EXPR:
12035 : 38273902 : case EXIT_EXPR:
12036 : 38273902 : return RECUR (TREE_OPERAND (t, 0), want_rval);
12037 : :
12038 : 5572414 : case DECL_EXPR:
12039 : 5572414 : tmp = DECL_EXPR_DECL (t);
12040 : 4402152 : if (VAR_P (tmp) && !DECL_ARTIFICIAL (tmp)
12041 : 8341211 : && (processing_template_decl
12042 : 2439847 : ? !decl_maybe_constant_var_p (tmp)
12043 : 2110897 : : !decl_constant_var_p (tmp)))
12044 : : {
12045 : 2133910 : if (CP_DECL_THREAD_LOCAL_P (tmp) && !DECL_REALLY_EXTERN (tmp))
12046 : : {
12047 : 5 : if (flags & tf_error)
12048 : 2 : constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p,
12049 : : "%qD defined %<thread_local%> in "
12050 : : "%<constexpr%> context", tmp);
12051 : 5 : return false;
12052 : : }
12053 : 2133905 : else if (TREE_STATIC (tmp))
12054 : : {
12055 : 81 : if (flags & tf_error)
12056 : 15 : constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p,
12057 : : "%qD defined %<static%> in %<constexpr%> "
12058 : : "context", tmp);
12059 : 81 : return false;
12060 : : }
12061 : 2133824 : else if (!check_for_uninitialized_const_var
12062 : 2133824 : (tmp, /*constexpr_context_p=*/true, flags))
12063 : : return false;
12064 : : }
12065 : 5568561 : if (VAR_P (tmp))
12066 : 4398299 : return RECUR (DECL_INITIAL (tmp), want_rval);
12067 : : return true;
12068 : :
12069 : 24902 : case TRY_FINALLY_EXPR:
12070 : 24902 : return (RECUR (TREE_OPERAND (t, 0), want_rval)
12071 : 24902 : && RECUR (TREE_OPERAND (t, 1), any));
12072 : :
12073 : 19920872 : case SCOPE_REF:
12074 : 19920872 : return RECUR (TREE_OPERAND (t, 1), want_rval);
12075 : :
12076 : 22018938 : case TARGET_EXPR:
12077 : 22018938 : if (!TARGET_EXPR_DIRECT_INIT_P (t)
12078 : 21961804 : && !TARGET_EXPR_ELIDING_P (t)
12079 : 39068940 : && !literal_type_p (TREE_TYPE (t)))
12080 : : {
12081 : 1341516 : if (flags & tf_error)
12082 : : {
12083 : 21 : auto_diagnostic_group d;
12084 : 21 : if (constexpr_error (loc, fundef_p,
12085 : : "temporary of non-literal type %qT in a "
12086 : 21 : "constant expression", TREE_TYPE (t)))
12087 : 21 : explain_non_literal_class (TREE_TYPE (t));
12088 : 21 : }
12089 : 1341516 : return false;
12090 : : }
12091 : : /* FALLTHRU */
12092 : 37441105 : case INIT_EXPR:
12093 : 37441105 : return RECUR (TREE_OPERAND (t, 1), rval);
12094 : :
12095 : 27307937 : case CONSTRUCTOR:
12096 : 27307937 : {
12097 : 27307937 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
12098 : 27307937 : constructor_elt *ce;
12099 : 111632575 : for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
12100 : 85160073 : if (!RECUR (ce->value, want_rval))
12101 : : return false;
12102 : : return true;
12103 : : }
12104 : :
12105 : 20273054 : case TREE_LIST:
12106 : 20273054 : {
12107 : 20273054 : gcc_assert (TREE_PURPOSE (t) == NULL_TREE
12108 : : || DECL_P (TREE_PURPOSE (t)));
12109 : 20273054 : if (!RECUR (TREE_VALUE (t), want_rval))
12110 : : return false;
12111 : 18079446 : if (TREE_CHAIN (t) == NULL_TREE)
12112 : : return true;
12113 : 49227 : return RECUR (TREE_CHAIN (t), want_rval);
12114 : : }
12115 : :
12116 : 9859192 : case TRUNC_DIV_EXPR:
12117 : 9859192 : case CEIL_DIV_EXPR:
12118 : 9859192 : case FLOOR_DIV_EXPR:
12119 : 9859192 : case ROUND_DIV_EXPR:
12120 : 9859192 : case TRUNC_MOD_EXPR:
12121 : 9859192 : case CEIL_MOD_EXPR:
12122 : 9859192 : case ROUND_MOD_EXPR:
12123 : 9859192 : {
12124 : 9859192 : tree denom = TREE_OPERAND (t, 1);
12125 : 9859192 : if (!RECUR (denom, rval))
12126 : : return false;
12127 : : /* We can't call cxx_eval_outermost_constant_expr on an expression
12128 : : that hasn't been through instantiate_non_dependent_expr yet. */
12129 : 9386075 : if (!processing_template_decl)
12130 : 3950142 : denom = cxx_eval_outermost_constant_expr (denom, true);
12131 : 9386075 : if (integer_zerop (denom))
12132 : : {
12133 : 335 : if (flags & tf_error)
12134 : 35 : constexpr_error (input_location, fundef_p,
12135 : : "division by zero is not a constant expression");
12136 : 335 : return false;
12137 : : }
12138 : : else
12139 : : {
12140 : 9385740 : want_rval = true;
12141 : 9385740 : return RECUR (TREE_OPERAND (t, 0), want_rval);
12142 : : }
12143 : : }
12144 : :
12145 : 4377856 : case COMPOUND_EXPR:
12146 : 4377856 : {
12147 : : /* check_return_expr sometimes wraps a TARGET_EXPR in a
12148 : : COMPOUND_EXPR; don't get confused. */
12149 : 4377856 : tree op0 = TREE_OPERAND (t, 0);
12150 : 4377856 : tree op1 = TREE_OPERAND (t, 1);
12151 : 4377856 : STRIP_NOPS (op1);
12152 : 4377856 : if (TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
12153 : 531180 : return RECUR (op0, want_rval);
12154 : : else
12155 : 3846676 : goto binary;
12156 : : }
12157 : :
12158 : : /* If the first operand is the non-short-circuit constant, look at
12159 : : the second operand; otherwise we only care about the first one for
12160 : : potentiality. */
12161 : 15351793 : case TRUTH_AND_EXPR:
12162 : 15351793 : case TRUTH_ANDIF_EXPR:
12163 : 15351793 : tmp = boolean_true_node;
12164 : 15351793 : goto truth;
12165 : 6051653 : case TRUTH_OR_EXPR:
12166 : 6051653 : case TRUTH_ORIF_EXPR:
12167 : 6051653 : tmp = boolean_false_node;
12168 : 21403446 : truth:
12169 : 21403446 : {
12170 : 21403446 : tree op0 = TREE_OPERAND (t, 0);
12171 : 21403446 : tree op1 = TREE_OPERAND (t, 1);
12172 : 21403446 : if (!RECUR (op0, rval))
12173 : : return false;
12174 : 13742221 : if (!(flags & tf_error) && RECUR (op1, rval))
12175 : : /* When quiet, try to avoid expensive trial evaluation by first
12176 : : checking potentiality of the second operand. */
12177 : : return true;
12178 : 1277491 : if (!processing_template_decl)
12179 : 910046 : op0 = cxx_eval_outermost_constant_expr (op0, true);
12180 : 1277491 : if (tree_int_cst_equal (op0, tmp))
12181 : 5384 : return (flags & tf_error) ? RECUR (op1, rval) : false;
12182 : : else
12183 : : return true;
12184 : : }
12185 : :
12186 : : case PLUS_EXPR:
12187 : : case MULT_EXPR:
12188 : : case POINTER_PLUS_EXPR:
12189 : : case RDIV_EXPR:
12190 : : case EXACT_DIV_EXPR:
12191 : : case MIN_EXPR:
12192 : : case MAX_EXPR:
12193 : : case LSHIFT_EXPR:
12194 : : case RSHIFT_EXPR:
12195 : : case LROTATE_EXPR:
12196 : : case RROTATE_EXPR:
12197 : : case BIT_IOR_EXPR:
12198 : : case BIT_XOR_EXPR:
12199 : : case BIT_AND_EXPR:
12200 : : case TRUTH_XOR_EXPR:
12201 : : case UNORDERED_EXPR:
12202 : : case ORDERED_EXPR:
12203 : : case UNLT_EXPR:
12204 : : case UNLE_EXPR:
12205 : : case UNGT_EXPR:
12206 : : case UNGE_EXPR:
12207 : : case UNEQ_EXPR:
12208 : : case LTGT_EXPR:
12209 : : case RANGE_EXPR:
12210 : : case COMPLEX_EXPR:
12211 : 188860056 : want_rval = true;
12212 : : /* Fall through. */
12213 : 188860056 : case ARRAY_REF:
12214 : 188860056 : case ARRAY_RANGE_REF:
12215 : 188860056 : case MEMBER_REF:
12216 : 188860056 : case DOTSTAR_EXPR:
12217 : 188860056 : case MEM_REF:
12218 : 188860056 : case BINARY_LEFT_FOLD_EXPR:
12219 : 188860056 : case BINARY_RIGHT_FOLD_EXPR:
12220 : 188860056 : binary:
12221 : 395977524 : for (i = 0; i < 2; ++i)
12222 : 298455788 : if (!RECUR (TREE_OPERAND (t, i), want_rval))
12223 : : return false;
12224 : : return true;
12225 : :
12226 : : case VEC_PERM_EXPR:
12227 : 91287 : for (i = 0; i < 3; ++i)
12228 : 91253 : if (!RECUR (TREE_OPERAND (t, i), true))
12229 : : return false;
12230 : : return true;
12231 : :
12232 : 4651537 : case COND_EXPR:
12233 : 4651537 : if (COND_EXPR_IS_VEC_DELETE (t) && cxx_dialect < cxx20)
12234 : : {
12235 : 8 : if (flags & tf_error)
12236 : 2 : constexpr_error (loc, fundef_p, "%<delete[]%> is not a "
12237 : : "constant expression");
12238 : 8 : return false;
12239 : : }
12240 : : /* Fall through. */
12241 : 9073275 : case IF_STMT:
12242 : 9073275 : case VEC_COND_EXPR:
12243 : : /* If the condition is a known constant, we know which of the legs we
12244 : : care about; otherwise we only require that the condition and
12245 : : either of the legs be potentially constant. */
12246 : 9073275 : tmp = TREE_OPERAND (t, 0);
12247 : 9073275 : if (!RECUR (tmp, rval))
12248 : : return false;
12249 : 7803313 : if (!processing_template_decl)
12250 : 6718573 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
12251 : : /* potential_constant_expression* isn't told if it is called for
12252 : : manifestly_const_eval or not, so for consteval if always
12253 : : process both branches as if the condition is not a known
12254 : : constant. */
12255 : 7803313 : if (TREE_CODE (t) != IF_STMT || !IF_STMT_CONSTEVAL_P (t))
12256 : : {
12257 : 7787000 : if (integer_zerop (tmp))
12258 : 2104676 : return RECUR (TREE_OPERAND (t, 2), want_rval);
12259 : 5682324 : else if (TREE_CODE (tmp) == INTEGER_CST)
12260 : 1970418 : return RECUR (TREE_OPERAND (t, 1), want_rval);
12261 : : }
12262 : 3728219 : tmp = *jump_target;
12263 : 4349620 : for (i = 1; i < 3; ++i)
12264 : : {
12265 : 4246474 : tree this_jump_target = tmp;
12266 : 4246474 : if (potential_constant_expression_1 (TREE_OPERAND (t, i),
12267 : : want_rval, strict, now, fundef_p,
12268 : : tf_none, &this_jump_target))
12269 : : {
12270 : 3824518 : if (returns (&this_jump_target) || throws (&this_jump_target))
12271 : 1100805 : *jump_target = this_jump_target;
12272 : 2524268 : else if (!returns (jump_target) && !throws (jump_target))
12273 : : {
12274 : 2524268 : if (breaks (&this_jump_target)
12275 : 2524268 : || continues (&this_jump_target))
12276 : 27 : *jump_target = this_jump_target;
12277 : 2524268 : if (i == 1)
12278 : : {
12279 : : /* If the then branch is potentially constant, but
12280 : : does not return, check if the else branch
12281 : : couldn't return, break or continue. */
12282 : 2113582 : hash_set<tree> pset;
12283 : 2113582 : check_for_return_continue_data data = { &pset, NULL_TREE,
12284 : : NULL_TREE,
12285 : 2113582 : false };
12286 : 4227164 : if (tree ret_expr
12287 : 2113582 : = cp_walk_tree (&TREE_OPERAND (t, 2),
12288 : : check_for_return_continue, &data,
12289 : : &pset))
12290 : 17833 : *jump_target = ret_expr;
12291 : 2095749 : else if (*jump_target == NULL_TREE)
12292 : : {
12293 : 2095725 : if (data.continue_stmt)
12294 : 0 : *jump_target = data.continue_stmt;
12295 : 2095725 : else if (data.break_stmt)
12296 : 5 : *jump_target = data.break_stmt;
12297 : : }
12298 : 2113582 : if (data.could_throw)
12299 : 30501 : *jump_target = void_node;
12300 : 2113582 : }
12301 : : }
12302 : 3625073 : return true;
12303 : : }
12304 : : }
12305 : 103146 : if (flags & tf_error)
12306 : : {
12307 : 3 : if (TREE_CODE (t) == IF_STMT)
12308 : 3 : constexpr_error (loc, fundef_p, "neither branch of %<if%> is a "
12309 : : "constant expression");
12310 : : else
12311 : 0 : constexpr_error (loc, fundef_p, "expression %qE is not a "
12312 : : "constant expression", t);
12313 : : }
12314 : : return false;
12315 : :
12316 : 970 : case VEC_INIT_EXPR:
12317 : 970 : if (VEC_INIT_EXPR_IS_CONSTEXPR (t))
12318 : : return true;
12319 : 409 : if (flags & tf_error)
12320 : : {
12321 : 3 : if (constexpr_error (loc, fundef_p, "non-constant array "
12322 : : "initialization"))
12323 : 3 : diagnose_non_constexpr_vec_init (t);
12324 : : }
12325 : : return false;
12326 : :
12327 : : case TYPE_DECL:
12328 : : case TAG_DEFN:
12329 : : /* We can see these in statement-expressions. */
12330 : : return true;
12331 : :
12332 : 476689 : case CLEANUP_STMT:
12333 : 476689 : if (!RECUR (CLEANUP_BODY (t), any))
12334 : : return false;
12335 : 475799 : if (!CLEANUP_EH_ONLY (t) && !RECUR (CLEANUP_EXPR (t), any))
12336 : : return false;
12337 : : return true;
12338 : :
12339 : : case EMPTY_CLASS_EXPR:
12340 : : return true;
12341 : :
12342 : 12 : case GOTO_EXPR:
12343 : 12 : {
12344 : 12 : tree *target = &TREE_OPERAND (t, 0);
12345 : : /* Gotos representing break, continue and cdtor return are OK. */
12346 : 12 : if (breaks (target) || continues (target) || returns (target))
12347 : : {
12348 : 3 : *jump_target = *target;
12349 : 3 : return true;
12350 : : }
12351 : 9 : if (DECL_ARTIFICIAL (*target))
12352 : : /* The user didn't write this goto, this isn't the problem. */
12353 : : return true;
12354 : 9 : if (flags & tf_error)
12355 : 2 : constexpr_error (loc, fundef_p, "%<goto%> is not a constant "
12356 : : "expression");
12357 : : return false;
12358 : : }
12359 : :
12360 : 48 : case ASSERTION_STMT:
12361 : 48 : case PRECONDITION_STMT:
12362 : 48 : case POSTCONDITION_STMT:
12363 : 48 : if (!checked_contract_p (get_contract_semantic (t)))
12364 : : return true;
12365 : 33 : return RECUR (CONTRACT_CONDITION (t), rval);
12366 : :
12367 : 224 : case LABEL_EXPR:
12368 : 224 : t = LABEL_EXPR_LABEL (t);
12369 : 224 : if (DECL_ARTIFICIAL (t) || cxx_dialect >= cxx23)
12370 : : return true;
12371 : 29 : else if (flags & tf_error)
12372 : 6 : constexpr_error (loc, fundef_p, "label definition in %<constexpr%> "
12373 : : "function only available with %<-std=c++23%> or "
12374 : : "%<-std=gnu++23%>");
12375 : : return false;
12376 : :
12377 : 2665 : case ANNOTATE_EXPR:
12378 : 2665 : return RECUR (TREE_OPERAND (t, 0), rval);
12379 : :
12380 : 21645 : case BIT_CAST_EXPR:
12381 : 21645 : return RECUR (TREE_OPERAND (t, 0), rval);
12382 : :
12383 : : /* Coroutine await, yield and return expressions are not. */
12384 : 678 : case CO_AWAIT_EXPR:
12385 : 678 : case CO_YIELD_EXPR:
12386 : 678 : case CO_RETURN_EXPR:
12387 : 678 : if (flags & tf_error)
12388 : 3 : constexpr_error (cp_expr_loc_or_loc (t, input_location), fundef_p,
12389 : : "%qE is not a constant expression", t);
12390 : : return false;
12391 : :
12392 : : /* Assume a TU-local entity is not constant, we'll error later when
12393 : : instantiating. */
12394 : : case TU_LOCAL_ENTITY:
12395 : : return false;
12396 : :
12397 : 30 : case NONTYPE_ARGUMENT_PACK:
12398 : 30 : {
12399 : 30 : tree args = ARGUMENT_PACK_ARGS (t);
12400 : 30 : int len = TREE_VEC_LENGTH (args);
12401 : 90 : for (int i = 0; i < len; ++i)
12402 : 60 : if (!RECUR (TREE_VEC_ELT (args, i), any))
12403 : : return false;
12404 : : return true;
12405 : : }
12406 : :
12407 : 0 : default:
12408 : 0 : if (objc_non_constant_expr_p (t))
12409 : : return false;
12410 : :
12411 : 0 : sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t)));
12412 : 0 : gcc_unreachable ();
12413 : : return false;
12414 : : }
12415 : : #undef RECUR
12416 : : }
12417 : :
12418 : : bool
12419 : 1128155067 : potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
12420 : : bool fundef_p, tsubst_flags_t flags)
12421 : : {
12422 : 1128155067 : if (flags & tf_error)
12423 : : {
12424 : : /* Check potentiality quietly first, as that could be performed more
12425 : : efficiently in some cases (currently only for TRUTH_*_EXPR). If
12426 : : that fails, replay the check noisily to give errors. */
12427 : 5385047 : flags &= ~tf_error;
12428 : 5385047 : if (potential_constant_expression_1 (t, want_rval, strict, now, fundef_p,
12429 : : flags))
12430 : : return true;
12431 : 873 : flags |= tf_error;
12432 : : }
12433 : :
12434 : 1122770893 : tree target = NULL_TREE;
12435 : 1122770893 : return potential_constant_expression_1 (t, want_rval, strict, now, fundef_p,
12436 : 1122770893 : flags, &target);
12437 : : }
12438 : :
12439 : : /* The main entry point to the above. */
12440 : :
12441 : : bool
12442 : 352466419 : potential_constant_expression (tree t)
12443 : : {
12444 : 352466419 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
12445 : : /*now*/false, /*fundef_p*/false,
12446 : 352466419 : tf_none);
12447 : : }
12448 : :
12449 : : /* As above, but require a constant rvalue. */
12450 : :
12451 : : bool
12452 : 20639383 : potential_rvalue_constant_expression (tree t)
12453 : : {
12454 : 20639383 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
12455 : : /*now*/false, /*fundef_p*/false,
12456 : 20639383 : tf_none);
12457 : : }
12458 : :
12459 : : /* Like above, but complain about non-constant expressions. */
12460 : :
12461 : : bool
12462 : 73 : require_potential_constant_expression (tree t)
12463 : : {
12464 : 73 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
12465 : : /*now*/false, /*fundef_p*/false,
12466 : 73 : tf_warning_or_error);
12467 : : }
12468 : :
12469 : : /* Cross product of the above. */
12470 : :
12471 : : bool
12472 : 73680 : require_potential_rvalue_constant_expression (tree t)
12473 : : {
12474 : 73680 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
12475 : : /*now*/false, /*fundef_p*/false,
12476 : 73680 : tf_warning_or_error);
12477 : : }
12478 : :
12479 : : /* Like require_potential_rvalue_constant_expression, but fundef_p is true. */
12480 : :
12481 : : bool
12482 : 210 : require_potential_rvalue_constant_expression_fncheck (tree t)
12483 : : {
12484 : 210 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
12485 : : /*now*/false, /*fundef_p*/true,
12486 : 210 : tf_warning_or_error);
12487 : : }
12488 : :
12489 : : /* Like above, but don't consider PARM_DECL a potential_constant_expression. */
12490 : :
12491 : : bool
12492 : 442 : require_rvalue_constant_expression (tree t)
12493 : : {
12494 : 442 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
12495 : : /*now*/true, /*fundef_p*/false,
12496 : 442 : tf_warning_or_error);
12497 : : }
12498 : :
12499 : : /* Like potential_constant_expression, but don't consider possible constexpr
12500 : : substitution of the current function. That is, PARM_DECL qualifies under
12501 : : potential_constant_expression, but not here.
12502 : :
12503 : : This is basically what you can check when any actual constant values might
12504 : : be value-dependent. */
12505 : :
12506 : : bool
12507 : 580166681 : is_constant_expression (tree t)
12508 : : {
12509 : 580166681 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
12510 : : /*now*/true, /*fundef_p*/false,
12511 : 580166681 : tf_none);
12512 : : }
12513 : :
12514 : : /* As above, but expect an rvalue. */
12515 : :
12516 : : bool
12517 : 102990417 : is_rvalue_constant_expression (tree t)
12518 : : {
12519 : 102990417 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
12520 : : /*now*/true, /*fundef_p*/false,
12521 : 102990417 : tf_none);
12522 : : }
12523 : :
12524 : : /* Like above, but complain about non-constant expressions. */
12525 : :
12526 : : bool
12527 : 5310642 : require_constant_expression (tree t)
12528 : : {
12529 : 5310642 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
12530 : : /*now*/true, /*fundef_p*/false,
12531 : 5310642 : tf_warning_or_error);
12532 : : }
12533 : :
12534 : : /* Like is_constant_expression, but allow const variables that are not allowed
12535 : : under constexpr rules. */
12536 : :
12537 : : bool
12538 : 61122073 : is_static_init_expression (tree t)
12539 : : {
12540 : 61122073 : return potential_constant_expression_1 (t, /*want_rval*/false,
12541 : : /*strict*/false, /*now*/true,
12542 : 61122073 : /*fundef_p*/false, tf_none);
12543 : : }
12544 : :
12545 : : /* Returns true if T is a potential constant expression that is not
12546 : : instantiation-dependent, and therefore a candidate for constant folding even
12547 : : in a template. */
12548 : :
12549 : : bool
12550 : 563657612 : is_nondependent_constant_expression (tree t)
12551 : : {
12552 : 563657612 : return (!type_unknown_p (t)
12553 : 563657563 : && is_constant_expression (t)
12554 : 1024276990 : && !instantiation_dependent_expression_p (t));
12555 : : }
12556 : :
12557 : : /* Returns true if T is a potential static initializer expression that is not
12558 : : instantiation-dependent. */
12559 : :
12560 : : bool
12561 : 61122073 : is_nondependent_static_init_expression (tree t)
12562 : : {
12563 : 61122073 : return (!type_unknown_p (t)
12564 : 61122073 : && is_static_init_expression (t)
12565 : 113722259 : && !instantiation_dependent_expression_p (t));
12566 : : }
12567 : :
12568 : : /* True iff FN is an implicitly constexpr function. */
12569 : :
12570 : : bool
12571 : 138126 : decl_implicit_constexpr_p (tree fn)
12572 : : {
12573 : 138126 : if (!(flag_implicit_constexpr
12574 : 0 : && TREE_CODE (fn) == FUNCTION_DECL
12575 : 0 : && DECL_DECLARED_CONSTEXPR_P (fn)))
12576 : : return false;
12577 : :
12578 : 0 : if (DECL_CLONED_FUNCTION_P (fn))
12579 : 0 : fn = DECL_CLONED_FUNCTION (fn);
12580 : :
12581 : 0 : return (DECL_LANG_SPECIFIC (fn)
12582 : 0 : && DECL_LANG_SPECIFIC (fn)->u.fn.implicit_constexpr);
12583 : : }
12584 : :
12585 : : /* Finalize constexpr processing after parsing. */
12586 : :
12587 : : void
12588 : 93654 : fini_constexpr (void)
12589 : : {
12590 : : /* The contexpr call and fundef copies tables are no longer needed. */
12591 : 93654 : constexpr_call_table = NULL;
12592 : 93654 : fundef_copies_table = NULL;
12593 : 93654 : }
12594 : :
12595 : : #include "gt-cp-constexpr.h"
|