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 : 70453200 : is_instantiation_of_constexpr (tree fun)
61 : : {
62 : 93790633 : return ((DECL_TEMPLOID_INSTANTIATION (fun)
63 : 47497274 : && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun)))
64 : 102763726 : || (DECL_DEFAULTED_FN (fun)
65 : 1544292 : && DECL_DECLARED_CONSTEXPR_P (fun)));
66 : : }
67 : :
68 : : /* Return true if T is a literal type. */
69 : :
70 : : bool
71 : 102458315 : literal_type_p (tree t)
72 : : {
73 : 99928746 : if (SCALAR_TYPE_P (t)
74 : 41672685 : || VECTOR_TYPE_P (t)
75 : 41660909 : || TYPE_REF_P (t)
76 : 136625699 : || (VOID_TYPE_P (t) && cxx_dialect >= cxx14))
77 : : return true;
78 : 31462146 : if (CLASS_TYPE_P (t))
79 : : {
80 : 30223760 : t = complete_type (t);
81 : 30223760 : gcc_assert (COMPLETE_TYPE_P (t) || errorcount);
82 : 30223760 : return CLASSTYPE_LITERAL_P (t);
83 : : }
84 : 1238386 : if (TREE_CODE (t) == ARRAY_TYPE)
85 : 1238212 : 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 : 275460578 : ensure_literal_type_for_constexpr_object (tree decl)
95 : : {
96 : 275460578 : tree type = TREE_TYPE (decl);
97 : 275460578 : if (VAR_P (decl)
98 : 97232792 : && (DECL_DECLARED_CONSTEXPR_P (decl)
99 : 76420153 : || var_in_constexpr_fn (decl))
100 : 303814458 : && !processing_template_decl)
101 : : {
102 : 18798304 : tree stype = strip_array_types (type);
103 : 18798304 : 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 : 18798298 : else if (!literal_type_p (type))
107 : : {
108 : 12752 : 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 : 12710 : 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 : 18785546 : else if (DECL_DECLARED_CONSTEXPR_P (decl)
133 : 18785546 : && 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 : 275460578 : 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 : 386980128 : constexpr_fundef_hasher::equal (const constexpr_fundef *lhs,
197 : : const constexpr_fundef *rhs)
198 : : {
199 : 370938894 : 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 : 381706013 : constexpr_fundef_hasher::hash (const constexpr_fundef *fundef)
207 : : {
208 : 381706013 : return DECL_UID (fundef->decl);
209 : : }
210 : :
211 : : /* Return a previously saved definition of function FUN. */
212 : :
213 : : constexpr_fundef *
214 : 40449130 : retrieve_constexpr_fundef (tree fun)
215 : : {
216 : 40449130 : if (constexpr_fundef_table == NULL)
217 : : return NULL;
218 : :
219 : 40445819 : constexpr_fundef fundef = { fun, NULL_TREE, NULL_TREE, NULL_TREE };
220 : 40445819 : 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 : 18650383 : is_valid_constexpr_fn (tree fun, bool complain)
228 : : {
229 : 18650383 : bool ret = true;
230 : :
231 : 37300766 : if (DECL_INHERITED_CTOR (fun)
232 : 2062179 : && 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 : 18650383 : for (tree parm = FUNCTION_FIRST_USER_PARM (fun);
242 : 36420798 : parm != NULL_TREE; parm = TREE_CHAIN (parm))
243 : 17770415 : if (!literal_type_p (TREE_TYPE (parm)))
244 : : {
245 : 18525 : ret = false;
246 : 18525 : 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 : 29872180 : 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 : 37300762 : 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 : 18650285 : else if (!DECL_CONSTRUCTOR_P (fun) && !DECL_DESTRUCTOR_P (fun))
274 : : {
275 : 16356342 : tree rettype = TREE_TYPE (TREE_TYPE (fun));
276 : 16356342 : if (!literal_type_p (rettype))
277 : : {
278 : 56846 : ret = false;
279 : 56846 : 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 : 16356342 : if (cxx_dialect < cxx14
291 : 31390 : && DECL_IOBJ_MEMBER_FUNCTION_P (fun)
292 : 16357397 : && !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 : 2293943 : 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 : 18650383 : 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 : 881 : 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 : 881 : auto_vec<tree, 2> fields;
340 : 1858 : do
341 : : {
342 : 1858 : fields.safe_push (TREE_OPERAND (member, 1));
343 : 1858 : member = TREE_OPERAND (member, 0);
344 : : }
345 : 1858 : while (ANON_AGGR_TYPE_P (TREE_TYPE (member))
346 : 3722 : && 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 : 1858 : while (field = fields.pop(),
353 : 1858 : ANON_AGGR_TYPE_P (TREE_TYPE (field)))
354 : : {
355 : 977 : tree ctor;
356 : : /* If there is already an outer constructor entry for the anonymous
357 : : aggregate FIELD, use it; otherwise, insert one. */
358 : 977 : if (vec_safe_is_empty (*vec)
359 : 198 : || (*vec)->last().index != field)
360 : : {
361 : 875 : ctor = build_constructor (TREE_TYPE (field), NULL);
362 : 875 : CONSTRUCTOR_APPEND_ELT (*vec, field, ctor);
363 : : }
364 : : else
365 : 102 : ctor = (*vec)->last().value;
366 : 977 : 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 : 881 : gcc_assert (fields.is_empty());
372 : 881 : CONSTRUCTOR_APPEND_ELT (*vec, field, init);
373 : :
374 : 881 : return true;
375 : 881 : }
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 : 3048409 : build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec)
385 : : {
386 : 3311236 : tree member, init;
387 : 3311236 : if (TREE_CODE (t) == CLEANUP_POINT_EXPR)
388 : 1408246 : t = TREE_OPERAND (t, 0);
389 : 3311236 : if (TREE_CODE (t) == EXPR_STMT)
390 : 1407892 : t = TREE_OPERAND (t, 0);
391 : 3311236 : if (t == error_mark_node)
392 : : return false;
393 : 3311229 : if (TREE_CODE (t) == STATEMENT_LIST)
394 : : {
395 : 3541258 : for (tree stmt : tsi_range (t))
396 : 232628 : if (! build_data_member_initialization (stmt, vec))
397 : 7 : return false;
398 : : return true;
399 : : }
400 : 3051001 : 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 : 246890 : return build_data_member_initialization (CLEANUP_BODY (t), vec);
408 : : }
409 : 2804111 : if (TREE_CODE (t) == CONVERT_EXPR)
410 : 1703661 : t = TREE_OPERAND (t, 0);
411 : 2804111 : 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 : 1185644 : || (cxx_dialect < cxx14 && TREE_CODE (t) == MODIFY_EXPR))
416 : : {
417 : 1618949 : member = TREE_OPERAND (t, 0);
418 : 1618949 : init = break_out_target_exprs (TREE_OPERAND (t, 1));
419 : : }
420 : 1185162 : else if (TREE_CODE (t) == CALL_EXPR)
421 : : {
422 : 1013443 : tree fn = get_callee_fndecl (t);
423 : 2026886 : if (!fn || !DECL_CONSTRUCTOR_P (fn))
424 : : /* We're only interested in calls to subobject constructors. */
425 : : return true;
426 : 894844 : 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 : 894844 : init = break_out_target_exprs (t);
431 : : }
432 : 171719 : else if (TREE_CODE (t) == BIND_EXPR)
433 : 15937 : 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 : 2513793 : if (INDIRECT_REF_P (member))
438 : 30099 : member = TREE_OPERAND (member, 0);
439 : 2513793 : if (TREE_CODE (member) == NOP_EXPR)
440 : : {
441 : 215018 : tree op = member;
442 : 215018 : STRIP_NOPS (op);
443 : 215018 : if (TREE_CODE (op) == ADDR_EXPR)
444 : : {
445 : 299 : 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 : 214719 : else if (op == current_class_ptr
453 : 429326 : && (same_type_ignoring_top_level_qualifiers_p
454 : 214607 : (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 : 195773 : gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member))));
463 : : }
464 : : }
465 : 2513793 : if (TREE_CODE (member) == ADDR_EXPR)
466 : 710224 : member = TREE_OPERAND (member, 0);
467 : 2513793 : if (TREE_CODE (member) == COMPONENT_REF)
468 : : {
469 : 2287660 : tree aggr = TREE_OPERAND (member, 0);
470 : 2287660 : if (TREE_CODE (aggr) == VAR_DECL)
471 : : /* Initializing a local variable, don't add anything. */
472 : : return true;
473 : 2287658 : if (TREE_CODE (aggr) != COMPONENT_REF)
474 : : /* Normal member initialization. */
475 : 2286510 : member = TREE_OPERAND (member, 1);
476 : 1148 : else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr)))
477 : : /* Initializing a member of an anonymous union. */
478 : 881 : 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 : 3069713 : if (!vec_safe_is_empty (*vec) && (*vec)->last().index == member)
488 : 32 : (*vec)->last().value = init;
489 : : else
490 : 2512878 : 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 : 3435887 : 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 : 3435887 : if (cxx_dialect >= cxx14)
553 : : return true;
554 : :
555 : 13069 : bool ok = true;
556 : 13069 : if (TREE_CODE (list) == STATEMENT_LIST)
557 : : {
558 : 13056 : tree_stmt_iterator i = tsi_last (list);
559 : 17094 : for (; !tsi_end_p (i); tsi_prev (&i))
560 : : {
561 : 14975 : tree t = tsi_stmt (i);
562 : 14975 : 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 : 13056 : 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 : 2041931 : sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v)
591 : : {
592 : 2041931 : tree pri = CLASSTYPE_PRIMARY_BINFO (type);
593 : 2041931 : tree field_type;
594 : 2041931 : unsigned i;
595 : 2041931 : constructor_elt *ce;
596 : :
597 : 2041931 : if (pri)
598 : 31523 : field_type = BINFO_TYPE (pri);
599 : 2010408 : else if (TYPE_CONTAINS_VPTR_P (type))
600 : 26232 : 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 : 74890 : for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
607 : 48753 : if (TREE_TYPE (ce->index) == field_type)
608 : : break;
609 : :
610 : 69702 : 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 : 2060880 : build_constexpr_constructor_member_initializers (tree type, tree body)
627 : : {
628 : 2060880 : vec<constructor_elt, va_gc> *vec = NULL;
629 : 2060880 : bool ok = true;
630 : 4643067 : while (true)
631 : 4643067 : switch (TREE_CODE (body))
632 : : {
633 : 1023479 : case MUST_NOT_THROW_EXPR:
634 : 1023479 : case EH_SPEC_BLOCK:
635 : 1023479 : body = TREE_OPERAND (body, 0);
636 : 1023479 : break;
637 : :
638 : 1558708 : case STATEMENT_LIST:
639 : 6201840 : for (tree stmt : tsi_range (body))
640 : : {
641 : 3117465 : body = stmt;
642 : 3117465 : if (TREE_CODE (body) == BIND_EXPR)
643 : : break;
644 : : }
645 : : break;
646 : :
647 : 2060880 : case BIND_EXPR:
648 : 2060880 : body = BIND_EXPR_BODY (body);
649 : 2060880 : goto found;
650 : :
651 : 0 : default:
652 : 0 : gcc_unreachable ();
653 : : }
654 : 2060880 : found:
655 : 2060880 : 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 : 2060880 : if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
662 : : {
663 : 1293570 : body = TREE_OPERAND (body, 0);
664 : 1293570 : if (TREE_CODE (body) == EXPR_STMT)
665 : 1293570 : body = TREE_OPERAND (body, 0);
666 : 1293570 : if (TREE_CODE (body) == INIT_EXPR
667 : 1293570 : && (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 : 1293570 : ok = build_data_member_initialization (body, &vec);
675 : : }
676 : 767310 : else if (TREE_CODE (body) == STATEMENT_LIST)
677 : : {
678 : 2288887 : for (tree stmt : tsi_range (body))
679 : : {
680 : 1521894 : ok = build_data_member_initialization (stmt, &vec);
681 : 1521894 : if (!ok)
682 : : break;
683 : : }
684 : : }
685 : 317 : else if (EXPR_P (body))
686 : 317 : ok = build_data_member_initialization (body, &vec);
687 : : else
688 : 0 : gcc_assert (errorcount > 0);
689 : 2060880 : if (ok)
690 : : {
691 : 2060873 : if (vec_safe_length (vec) > 0)
692 : : {
693 : : /* In a delegating constructor, return the target. */
694 : 1956862 : constructor_elt *ce = &(*vec)[0];
695 : 1956862 : if (ce->index == current_class_ptr)
696 : : {
697 : 18942 : body = ce->value;
698 : 18942 : vec_free (vec);
699 : 18942 : return body;
700 : : }
701 : : }
702 : 2041931 : vec = sort_constexpr_mem_initializers (type, vec);
703 : 2041931 : 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 : 308407636 : get_function_named_in_call (tree t)
715 : : {
716 : 308407636 : tree callee = cp_get_callee (t);
717 : 308407636 : tree fun = cp_get_fndecl_from_callee (callee, /*fold*/false);
718 : 308407636 : 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 : 18054165 : massage_constexpr_body (tree fun, tree body)
795 : : {
796 : 36108330 : if (DECL_CONSTRUCTOR_P (fun))
797 : 2060880 : body = build_constexpr_constructor_member_initializers
798 : 2060880 : (DECL_CONTEXT (fun), body);
799 : 15993285 : 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 : 18054165 : 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 : 1722677 : cx_check_missing_mem_inits (tree ctype, tree body, bool complain)
815 : : {
816 : : /* We allow uninitialized bases/fields in C++20. */
817 : 1722677 : if (cxx_dialect >= cxx20)
818 : : return false;
819 : :
820 : 790824 : unsigned nelts = 0;
821 : :
822 : 790824 : if (body)
823 : : {
824 : 790820 : if (TREE_CODE (body) != CONSTRUCTOR)
825 : : return false;
826 : 790564 : nelts = CONSTRUCTOR_NELTS (body);
827 : : }
828 : 790568 : tree field = TYPE_FIELDS (ctype);
829 : :
830 : 790568 : if (TREE_CODE (ctype) == UNION_TYPE)
831 : : {
832 : 8186 : 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 : 8184 : 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 : 1689596 : for (unsigned i = 0; i <= nelts; ++i)
846 : : {
847 : 1689596 : tree index = NULL_TREE;
848 : 1689596 : if (i < nelts)
849 : : {
850 : 907214 : index = CONSTRUCTOR_ELT (body, i)->index;
851 : : /* Skip vptr adjustment represented with a COMPONENT_REF. */
852 : 907214 : if (TREE_CODE (index) != FIELD_DECL)
853 : 67518 : continue;
854 : : }
855 : :
856 : 39402270 : for (; field != index; field = DECL_CHAIN (field))
857 : : {
858 : 37780261 : tree ftype;
859 : 37780261 : if (TREE_CODE (field) != FIELD_DECL)
860 : 75426343 : continue;
861 : 134089 : if (DECL_UNNAMED_BIT_FIELD (field))
862 : 16 : continue;
863 : : /* Artificial fields can be ignored unless they're bases. */
864 : 134073 : if (DECL_ARTIFICIAL (field) && !DECL_FIELD_IS_BASE (field))
865 : 3 : continue;
866 : 134070 : 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 : 134066 : ftype = TREE_TYPE (field);
876 : 134066 : 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 : 134064 : if (is_empty_field (field))
881 : : /* An empty field doesn't need an initializer. */
882 : 133963 : 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 : 1622009 : if (field == NULL_TREE)
902 : : break;
903 : :
904 : 839696 : 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 : 839696 : 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 : 141497351 : maybe_save_constexpr_fundef (tree fun)
924 : : {
925 : 141497351 : if (processing_template_decl
926 : 56193653 : || cp_function_chain->invalid_constexpr
927 : 197579206 : || (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun)))
928 : 123443446 : 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 : 42538799 : bool implicit = false;
933 : 42538799 : 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 : 42538799 : if (!DECL_DECLARED_CONSTEXPR_P (fun) && !implicit)
948 : : return;
949 : :
950 : 18118055 : bool complain = !DECL_GENERATED_P (fun) && !implicit;
951 : :
952 : 18118055 : if (!is_valid_constexpr_fn (fun, complain))
953 : : return;
954 : :
955 : 18054118 : tree massaged = massage_constexpr_body (fun, DECL_SAVED_TREE (fun));
956 : 18054118 : 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 : 18054085 : bool potential = potential_rvalue_constant_expression (massaged);
965 : 18054085 : if (!potential && complain)
966 : 182 : require_potential_rvalue_constant_expression_fncheck (massaged);
967 : :
968 : 20114947 : if (DECL_CONSTRUCTOR_P (fun) && potential
969 : 20112209 : && !DECL_DEFAULTED_FN (fun))
970 : : {
971 : 1722438 : if (cx_check_missing_mem_inits (DECL_CONTEXT (fun),
972 : : massaged, complain))
973 : : potential = false;
974 : 1722350 : 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 : 1717665 : massaged = DECL_SAVED_TREE (fun);
979 : 1717665 : potential = potential_rvalue_constant_expression (massaged);
980 : 1717665 : if (!potential && complain)
981 : 16 : require_potential_rvalue_constant_expression_fncheck (massaged);
982 : : }
983 : : }
984 : :
985 : 18054085 : 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 : 18053913 : 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 : 18053905 : constexpr_fundef entry = {fun, NULL_TREE, NULL_TREE, NULL_TREE};
1009 : 18053905 : bool clear_ctx = false;
1010 : 18053905 : if (DECL_RESULT (fun) && DECL_CONTEXT (DECL_RESULT (fun)) == NULL_TREE)
1011 : : {
1012 : 18053905 : clear_ctx = true;
1013 : 18053905 : DECL_CONTEXT (DECL_RESULT (fun)) = fun;
1014 : : }
1015 : 18053905 : tree saved_fn = current_function_decl;
1016 : 18053905 : current_function_decl = fun;
1017 : 18053905 : entry.body = copy_fn (entry.decl, entry.parms, entry.result);
1018 : 18053905 : current_function_decl = saved_fn;
1019 : 18053905 : if (clear_ctx)
1020 : 18053905 : DECL_CONTEXT (DECL_RESULT (entry.decl)) = NULL_TREE;
1021 : 18053905 : 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 : 84436 : entry.result = error_mark_node;
1026 : :
1027 : 18053905 : 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 : 18075829 : register_constexpr_fundef (const constexpr_fundef &value)
1035 : : {
1036 : : /* Create the constexpr function table if necessary. */
1037 : 18075829 : if (constexpr_fundef_table == NULL)
1038 : 25417 : constexpr_fundef_table
1039 : 25417 : = hash_table<constexpr_fundef_hasher>::create_ggc (101);
1040 : :
1041 : 18075829 : constexpr_fundef **slot = constexpr_fundef_table->find_slot
1042 : 18075829 : (const_cast<constexpr_fundef *> (&value), INSERT);
1043 : :
1044 : 18075829 : gcc_assert (*slot == NULL);
1045 : 18075829 : *slot = ggc_alloc<constexpr_fundef> ();
1046 : 18075829 : **slot = value;
1047 : 18075829 : }
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 : 694 : explain_invalid_constexpr_fn (tree fun)
1057 : : {
1058 : 694 : static hash_set<tree> *diagnosed;
1059 : 694 : tree body;
1060 : :
1061 : : /* Don't try to explain a function we already complained about. */
1062 : 694 : if (function *f = DECL_STRUCT_FUNCTION (fun))
1063 : 504 : if (f->language->erroneous)
1064 : 694 : 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 : 645 : if (warn_invalid_constexpr == 0 && DECL_DECLARED_CONSTEXPR_P (fun))
1071 : : /* Go on. */;
1072 : : /* Only diagnose defaulted functions, lambdas, or instantiations. */
1073 : 623 : else if (!DECL_DEFAULTED_FN (fun)
1074 : 829 : && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun))
1075 : 599 : && !(flag_implicit_constexpr
1076 : 6 : && !DECL_DECLARED_CONSTEXPR_P (fun)
1077 : 6 : && DECL_DECLARED_INLINE_P (fun))
1078 : 1219 : && !is_instantiation_of_constexpr (fun))
1079 : : {
1080 : 582 : inform (DECL_SOURCE_LOCATION (fun), "%qD declared here", fun);
1081 : 3 : if (flag_implicit_constexpr && !maybe_constexpr_fn (fun)
1082 : 585 : && decl_defined_p (fun))
1083 : 3 : inform (DECL_SOURCE_LOCATION (fun),
1084 : : "%<-fimplicit-constexpr%> only affects %<inline%> functions");
1085 : 582 : 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 : 37996828 : 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 : 370318914 : constexpr_global_ctx ()
1203 : 740637828 : : constexpr_ops_count (0), cleanups (NULL), modifiable (nullptr),
1204 : 370318914 : heap_dealloc_count (0), uncaught_exceptions (0) {}
1205 : :
1206 : 194647525 : bool is_outside_lifetime (tree t)
1207 : : {
1208 : 194647525 : if (tree *p = values.get (t))
1209 : 44076172 : if (*p == void_node)
1210 : 178 : return true;
1211 : : return false;
1212 : : }
1213 : 228757333 : tree get_value (tree t)
1214 : : {
1215 : 228757333 : if (tree *p = values.get (t))
1216 : 68796635 : if (*p != void_node)
1217 : 67984399 : return *p;
1218 : : return NULL_TREE;
1219 : : }
1220 : 31009779 : tree *get_value_ptr (tree t, bool initializing)
1221 : : {
1222 : 31009779 : if (modifiable && !modifiable->contains (t))
1223 : : return nullptr;
1224 : 31009768 : if (tree *p = values.get (t))
1225 : : {
1226 : 30995546 : 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 : 89250868 : void put_value (tree t, tree v)
1237 : : {
1238 : 89250868 : bool already_in_map = values.put (t, v);
1239 : 89250868 : if (!already_in_map && modifiable)
1240 : 32 : modifiable->add (t);
1241 : 89250868 : }
1242 : 63617075 : void destroy_value (tree t)
1243 : : {
1244 : 63617075 : if (TREE_CODE (t) == VAR_DECL
1245 : 63617075 : || TREE_CODE (t) == PARM_DECL
1246 : : || TREE_CODE (t) == RESULT_DECL)
1247 : 63611395 : values.put (t, void_node);
1248 : : else
1249 : 5680 : values.remove (t);
1250 : 63617075 : }
1251 : 3339651 : void clear_value (tree t)
1252 : : {
1253 : 6679302 : 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 : 34942964 : returns (tree *jump_target)
1322 : : {
1323 : 5068566 : return *jump_target && TREE_CODE (*jump_target) == RETURN_EXPR;
1324 : : }
1325 : :
1326 : : static bool
1327 : 35705779 : breaks (tree *jump_target)
1328 : : {
1329 : 35705779 : return (*jump_target
1330 : 35705779 : && ((TREE_CODE (*jump_target) == LABEL_DECL
1331 : 24 : && LABEL_DECL_BREAK (*jump_target))
1332 : 1025111 : || TREE_CODE (*jump_target) == BREAK_STMT
1333 : 981073 : || TREE_CODE (*jump_target) == EXIT_EXPR));
1334 : : }
1335 : :
1336 : : static bool
1337 : 50914884 : continues (tree *jump_target)
1338 : : {
1339 : 50914884 : return (*jump_target
1340 : 50914884 : && ((TREE_CODE (*jump_target) == LABEL_DECL
1341 : 36 : && LABEL_DECL_CONTINUE (*jump_target))
1342 : 1004772 : || TREE_CODE (*jump_target) == CONTINUE_STMT));
1343 : : }
1344 : :
1345 : : static bool
1346 : 6546187 : switches (tree *jump_target)
1347 : : {
1348 : 48935 : return *jump_target && TREE_CODE (*jump_target) == INTEGER_CST;
1349 : : }
1350 : :
1351 : : static bool
1352 : 1025194213 : 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 : 57365784 : 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 : 155692002 : p2280_active_p (const constexpr_ctx *ctx)
1365 : : {
1366 : 27018006 : 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 : 63594100 : destroy_value_checked (const constexpr_ctx* ctx, tree t, bool *non_constant_p)
1381 : : {
1382 : 63594100 : 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 : 63594097 : if (!*non_constant_p
1387 : 44283074 : && DECL_P (t)
1388 : : /* Non-trivial destructors have their lifetimes ended explicitly
1389 : : with a clobber, so don't worry about it here. */
1390 : 44277573 : && (!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 : 183772 : || TREE_CODE (t) == PARM_DECL)
1397 : 107688116 : && 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 : 63594097 : 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 : 4512052 : uid_sensitive_constexpr_evaluation_p ()
1426 : : {
1427 : 4507711 : if (uid_sensitive_constexpr_evaluation_value)
1428 : : {
1429 : 198681 : ++uid_sensitive_constexpr_evaluation_true_counter;
1430 : 198681 : 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 : 65562298 : uid_sensitive_constexpr_evaluation_sentinel
1442 : 65562298 : ::uid_sensitive_constexpr_evaluation_sentinel ()
1443 : 65562298 : : ovr (uid_sensitive_constexpr_evaluation_value, true)
1444 : : {
1445 : 65562298 : }
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 : 1900631309 : uid_sensitive_constexpr_evaluation_checker
1452 : 1900631309 : ::uid_sensitive_constexpr_evaluation_checker ()
1453 : 1900631309 : : saved_counter (uid_sensitive_constexpr_evaluation_true_counter)
1454 : : {
1455 : 1900631309 : }
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 : 1332047030 : uid_sensitive_constexpr_evaluation_checker::evaluation_restricted_p () const
1463 : : {
1464 : 1332047030 : return (uid_sensitive_constexpr_evaluation_value
1465 : 1332047030 : && 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 : 111734696 : constexpr_call_hasher::hash (constexpr_call *info)
1478 : : {
1479 : 111734696 : 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 : 121286351 : constexpr_call_hasher::equal (constexpr_call *lhs, constexpr_call *rhs)
1488 : : {
1489 : 121286351 : if (lhs == rhs)
1490 : : return true;
1491 : 121286351 : if (lhs->hash != rhs->hash)
1492 : : return false;
1493 : 16041234 : if (!constexpr_fundef_hasher::equal (lhs->fundef, rhs->fundef))
1494 : : return false;
1495 : 16041234 : return cp_tree_equal (lhs->bindings, rhs->bindings);
1496 : : }
1497 : :
1498 : : /* Initialize the constexpr call table, if needed. */
1499 : :
1500 : : static void
1501 : 19152939 : maybe_initialize_constexpr_call_table (void)
1502 : : {
1503 : 19152939 : if (constexpr_call_table == NULL)
1504 : 16993 : constexpr_call_table = hash_table<constexpr_call_hasher>::create_ggc (101);
1505 : 19152939 : }
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 : 26811217 : get_fundef_copy (constexpr_fundef *fundef)
1527 : : {
1528 : 26811217 : tree copy;
1529 : 26811217 : bool existed;
1530 : 26811217 : tree *slot = &(hash_map_safe_get_or_insert<hm_ggc>
1531 : 26811217 : (fundef_copies_table, fundef->decl, &existed, 127));
1532 : :
1533 : 26811217 : 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 : 4345619 : copy = build_tree_list (fundef->body, fundef->parms);
1539 : 4345619 : TREE_TYPE (copy) = fundef->result;
1540 : : }
1541 : 22465598 : 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 : 22461257 : copy = *slot;
1567 : 22461257 : *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 : 26811217 : save_fundef_copy (tree fun, tree copy)
1579 : : {
1580 : 26811217 : tree *slot = fundef_copies_table->get (fun);
1581 : 26811217 : TREE_CHAIN (copy) = *slot;
1582 : 26811217 : *slot = copy;
1583 : 26811217 : }
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 : 30103379 : cxx_cxa_builtin_fn_p (tree fndecl)
1765 : : {
1766 : 30103379 : if (cxx_dialect < cxx26)
1767 : : return CXA_NONE;
1768 : 4885649 : if (DECL_LANGUAGE (fndecl) != lang_c)
1769 : : {
1770 : 4473223 : if (!decl_in_std_namespace_p (fndecl))
1771 : : return CXA_NONE;
1772 : 1812617 : if (id_equal (DECL_NAME (fndecl), "uncaught_exceptions"))
1773 : : return STD_UNCAUGHT_EXCEPTIONS;
1774 : 1812496 : if (id_equal (DECL_NAME (fndecl), "current_exception"))
1775 : : return STD_CURRENT_EXCEPTION;
1776 : 1794514 : if (id_equal (DECL_NAME (fndecl), "rethrow_exception"))
1777 : : return STD_RETHROW_EXCEPTION;
1778 : : return CXA_NONE;
1779 : : }
1780 : 412426 : if (!startswith (IDENTIFIER_POINTER (DECL_NAME (fndecl)), "__cxa_"))
1781 : : return CXA_NONE;
1782 : 39074 : if (id_equal (DECL_NAME (fndecl), "__cxa_allocate_exception"))
1783 : : return CXA_ALLOCATE_EXCEPTION;
1784 : 6491 : if (id_equal (DECL_NAME (fndecl), "__cxa_free_exception"))
1785 : : return CXA_FREE_EXCEPTION;
1786 : 6490 : if (id_equal (DECL_NAME (fndecl), "__cxa_throw"))
1787 : : return CXA_THROW;
1788 : 6060 : if (id_equal (DECL_NAME (fndecl), "__cxa_begin_catch"))
1789 : : return CXA_BEGIN_CATCH;
1790 : 1099 : if (id_equal (DECL_NAME (fndecl), "__cxa_end_catch"))
1791 : : return CXA_END_CATCH;
1792 : 926 : if (id_equal (DECL_NAME (fndecl), "__cxa_rethrow"))
1793 : : return CXA_RETHROW;
1794 : 870 : if (id_equal (DECL_NAME (fndecl), "__cxa_get_exception_ptr"))
1795 : : return CXA_GET_EXCEPTION_PTR;
1796 : 850 : if (id_equal (DECL_NAME (fndecl), "__cxa_bad_cast"))
1797 : : return CXA_BAD_CAST;
1798 : 566 : if (id_equal (DECL_NAME (fndecl), "__cxa_bad_typeid"))
1799 : : return CXA_BAD_TYPEID;
1800 : 558 : 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 : 15740 : cxa_allocate_exception (location_t loc, const constexpr_ctx *ctx, tree type,
1833 : : tree refcount)
1834 : : {
1835 : 15740 : tree var = build_decl (loc, VAR_DECL, heap_uninit_identifier, type);
1836 : 15740 : DECL_ARTIFICIAL (var) = 1;
1837 : 15740 : retrofit_lang_decl (var);
1838 : 15740 : DECL_EXCEPTION_REFCOUNT (var) = refcount;
1839 : 15740 : ctx->global->heap_vars.safe_push (var);
1840 : 15740 : 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 : 26509 : 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 : 26509 : int nargs = call_expr_nargs (call);
1853 : 26509 : location_t loc = cp_expr_loc_or_input_loc (call);
1854 : 26509 : tree args[4], arg;
1855 : 26509 : 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 : 26509 : if ((kind == CXA_BEGIN_CATCH || kind == CXA_GET_EXCEPTION_PTR)
1865 : 2584 : && nargs == 1
1866 : 2584 : && (arg = CALL_EXPR_ARG (call, 0))
1867 : 2584 : && TREE_CODE (arg) == CALL_EXPR
1868 : 2584 : && call_expr_nargs (arg) == 1
1869 : 29093 : && integer_zerop (CALL_EXPR_ARG (arg, 0)))
1870 : 2584 : if (tree fun = get_function_named_in_call (arg))
1871 : 2584 : if (fndecl_built_in_p (fun, BUILT_IN_EH_POINTER))
1872 : : {
1873 : 2584 : if (ctx->global->caught_exceptions.length () < 2)
1874 : : {
1875 : 2397 : no_caught_exceptions:
1876 : 2397 : if (!ctx->quiet)
1877 : 0 : error_at (loc, "%qD called with no caught exceptions pending",
1878 : : fndecl);
1879 : 2397 : *non_constant_p = true;
1880 : 2397 : 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 : 40608 : for (int i = 0; i < nargs; ++i)
1944 : : {
1945 : 16683 : args[i] = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (call, i),
1946 : : vc_prvalue, non_constant_p,
1947 : : overflow_p, jump_target);
1948 : 16683 : if (*non_constant_p)
1949 : : return call;
1950 : 16683 : if (*jump_target)
1951 : : return NULL_TREE;
1952 : : }
1953 : 23925 : switch (kind)
1954 : : {
1955 : 15612 : case CXA_ALLOCATE_EXCEPTION:
1956 : 15612 : if (nargs != 1)
1957 : 0 : goto invalid_nargs;
1958 : 15612 : 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 : 31224 : tree type = build_array_type_nelts (char_type_node,
1968 : 15612 : tree_to_uhwi (args[0]));
1969 : 15612 : tree var = cxa_allocate_exception (loc, ctx, type, size_zero_node);
1970 : 15612 : ctx->global->put_value (var, NULL_TREE);
1971 : 15612 : 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 : 7492 : case STD_CURRENT_EXCEPTION:
2149 : 7492 : if (nargs != 0)
2150 : 0 : goto invalid_nargs;
2151 : : else
2152 : : {
2153 : 7492 : tree name = get_identifier ("exception_ptr");
2154 : 7492 : tree decl = lookup_qualified_name (std_node, name);
2155 : 7492 : tree fld;
2156 : 7492 : if (TREE_CODE (decl) != TYPE_DECL
2157 : 7492 : || !CLASS_TYPE_P (TREE_TYPE (decl))
2158 : 7492 : || !COMPLETE_TYPE_P (TREE_TYPE (decl))
2159 : 7492 : || !(fld = next_aggregate_field (TYPE_FIELDS (TREE_TYPE (decl))))
2160 : 7492 : || DECL_ARTIFICIAL (fld)
2161 : 7492 : || TREE_CODE (TREE_TYPE (fld)) != POINTER_TYPE
2162 : 7492 : || next_aggregate_field (DECL_CHAIN (fld))
2163 : 14984 : || !tree_int_cst_equal (TYPE_SIZE (TREE_TYPE (decl)),
2164 : 7492 : 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 : 14984 : 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 : 7492 : if (ctx->manifestly_const_eval != mce_true && arg == NULL_TREE)
2184 : : {
2185 : 7460 : *non_constant_p = true;
2186 : 7460 : 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 : 10879510 : 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 : 10879510 : const int nargs = call_expr_nargs (t);
2272 : 10879510 : tree *args = (tree *) alloca (nargs * sizeof (tree));
2273 : 10879510 : tree new_call;
2274 : 10879510 : int i;
2275 : :
2276 : : /* Don't fold __builtin_constant_p within a constexpr function. */
2277 : 10879510 : 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 : 741298 : if (bi_const_p
2282 : 741298 : && ctx->manifestly_const_eval != mce_true
2283 : 727841 : && current_function_decl
2284 : 725691 : && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
2285 : : {
2286 : 428659 : *non_constant_p = true;
2287 : 428659 : 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 : 10450851 : if (fndecl_built_in_p (fun, CP_BUILT_IN_IS_CONSTANT_EVALUATED,
2296 : : BUILT_IN_FRONTEND))
2297 : : {
2298 : 1964805 : if (ctx->manifestly_const_eval == mce_unknown)
2299 : : {
2300 : 1948207 : *non_constant_p = true;
2301 : 1948207 : return t;
2302 : : }
2303 : 16598 : return constant_boolean_node (ctx->manifestly_const_eval == mce_true,
2304 : 16598 : boolean_type_node);
2305 : : }
2306 : :
2307 : 8486046 : 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 : 8485610 : 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 : 8485539 : int strops = 0;
2322 : 8485539 : int strret = 0;
2323 : 8485539 : if (fndecl_built_in_p (fun, BUILT_IN_NORMAL))
2324 : 7849794 : switch (DECL_FUNCTION_CODE (fun))
2325 : : {
2326 : : case BUILT_IN_STRLEN:
2327 : : case BUILT_IN_STRNLEN:
2328 : 8485475 : strops = 1;
2329 : : break;
2330 : 106174 : case BUILT_IN_MEMCHR:
2331 : 106174 : case BUILT_IN_STRCHR:
2332 : 106174 : case BUILT_IN_STRRCHR:
2333 : 106174 : strops = 1;
2334 : 106174 : strret = 1;
2335 : 106174 : break;
2336 : 49337 : case BUILT_IN_MEMCMP:
2337 : 49337 : case BUILT_IN_STRCMP:
2338 : 49337 : strops = 2;
2339 : 49337 : break;
2340 : 30570 : case BUILT_IN_STRSTR:
2341 : 30570 : strops = 2;
2342 : 30570 : strret = 1;
2343 : 30570 : 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 : 22 : case BUILT_IN_UNREACHABLE:
2350 : 22 : case BUILT_IN_TRAP:
2351 : 22 : 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 : 2 : if (EXPR_LOCATION (t) == BUILTINS_LOCATION)
2357 : 0 : error ("%<constexpr%> call flows off the end of the function");
2358 : : else
2359 : 2 : error ("%q+E is not a constant expression", t);
2360 : : }
2361 : 22 : *non_constant_p = true;
2362 : 22 : 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 : 8485475 : constexpr_ctx new_ctx = *ctx;
2370 : 8485475 : new_ctx.quiet = true;
2371 : 22906510 : for (i = 0; i < nargs; ++i)
2372 : : {
2373 : 14421035 : tree arg = CALL_EXPR_ARG (t, i);
2374 : 14421035 : 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 : 14421035 : bool strop = i < strops;
2379 : 14421035 : if (strop)
2380 : : {
2381 : 359144 : STRIP_NOPS (arg);
2382 : 359144 : if (TREE_CODE (arg) == ADDR_EXPR)
2383 : 7717 : 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 : 14421035 : if (!builtin_valid_in_constant_expr_p (fun)
2392 : 14421035 : || potential_constant_expression (arg))
2393 : : {
2394 : 14420904 : bool dummy1 = false, dummy2 = false;
2395 : 14420904 : tree jmp_target = NULL_TREE;
2396 : 14420904 : arg = cxx_eval_constant_expression (&new_ctx, arg, vc_prvalue,
2397 : : &dummy1, &dummy2, &jmp_target);
2398 : 14420904 : if (jmp_target)
2399 : : {
2400 : 0 : *jump_target = jmp_target;
2401 : 0 : return NULL_TREE;
2402 : : }
2403 : : }
2404 : :
2405 : 14421035 : 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 : 312639 : arg = cp_fold_rvalue (arg);
2409 : 14108396 : else if (strop)
2410 : : {
2411 : 7717 : if (TREE_CODE (arg) == CONSTRUCTOR)
2412 : 100 : arg = braced_lists_to_strings (TREE_TYPE (arg), arg);
2413 : 7717 : if (TREE_CODE (arg) == STRING_CST)
2414 : 2824 : arg = build_address (arg);
2415 : : else
2416 : : arg = oarg;
2417 : : }
2418 : :
2419 : 14421035 : args[i] = arg;
2420 : : }
2421 : :
2422 : 8485475 : bool save_ffbcp = force_folding_builtin_constant_p;
2423 : 8485475 : force_folding_builtin_constant_p |= ctx->manifestly_const_eval == mce_true;
2424 : 8485475 : tree save_cur_fn = current_function_decl;
2425 : : /* Return name of ctx->call->fundef->decl for __builtin_FUNCTION (). */
2426 : 8485475 : if (fndecl_built_in_p (fun, BUILT_IN_FUNCTION)
2427 : 38 : && ctx->call
2428 : 8485479 : && ctx->call->fundef)
2429 : 4 : current_function_decl = ctx->call->fundef->decl;
2430 : 8485475 : 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 : 8485127 : 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 : 16969336 : new_call = fold_builtin_call_array (EXPR_LOCATION (t), TREE_TYPE (t),
2455 : 8484668 : CALL_EXPR_FN (t), nargs, args);
2456 : 8485048 : current_function_decl = save_cur_fn;
2457 : 8485048 : force_folding_builtin_constant_p = save_ffbcp;
2458 : 8485048 : if (new_call == NULL)
2459 : : {
2460 : 6246926 : 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 : 6246926 : *non_constant_p = true;
2467 : 6246926 : return t;
2468 : : }
2469 : :
2470 : 2238122 : 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 : 2237545 : 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 : 2237545 : new_call = op;
2488 : : }
2489 : :
2490 : 2237545 : return cxx_eval_constant_expression (&new_ctx, new_call, lval,
2491 : : non_constant_p, overflow_p,
2492 : 2237545 : 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 : 45073658 : adjust_temp_type (tree type, tree temp)
2500 : : {
2501 : 45073658 : if (same_type_p (TREE_TYPE (temp), type))
2502 : : return temp;
2503 : : /* Avoid wrapping an aggregate value in a NOP_EXPR. */
2504 : 22088164 : if (TREE_CODE (temp) == CONSTRUCTOR)
2505 : : {
2506 : : /* build_constructor wouldn't retain various CONSTRUCTOR flags. */
2507 : 2677579 : tree t = copy_node (temp);
2508 : 2677579 : TREE_TYPE (t) = type;
2509 : 2677579 : return t;
2510 : : }
2511 : 19410585 : if (TREE_CODE (temp) == EMPTY_CLASS_EXPR)
2512 : 0 : return build0 (EMPTY_CLASS_EXPR, type);
2513 : 19410585 : 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 : 19410585 : 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 : 33824278 : unshare_constructor (tree t MEM_STAT_DECL)
2528 : : {
2529 : 33824278 : if (!t || TREE_CODE (t) != CONSTRUCTOR)
2530 : : return t;
2531 : 7715928 : auto_vec <tree*, 4> ptrs;
2532 : 7715928 : ptrs.safe_push (&t);
2533 : 7715928 : while (!ptrs.is_empty ())
2534 : : {
2535 : 8824429 : tree *p = ptrs.pop ();
2536 : 8824429 : tree n = copy_node (*p PASS_MEM_STAT);
2537 : 12935205 : CONSTRUCTOR_ELTS (n) = vec_safe_copy (CONSTRUCTOR_ELTS (*p) PASS_MEM_STAT);
2538 : 8824429 : *p = n;
2539 : 8824429 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (n);
2540 : 8824429 : constructor_elt *ce;
2541 : 34030135 : for (HOST_WIDE_INT i = 0; vec_safe_iterate (v, i, &ce); ++i)
2542 : 8665349 : if (ce->value && TREE_CODE (ce->value) == CONSTRUCTOR)
2543 : 1108501 : ptrs.safe_push (&ce->value);
2544 : : }
2545 : 7715928 : return t;
2546 : 7715928 : }
2547 : :
2548 : : /* If T is a CONSTRUCTOR, ggc_free T and any sub-CONSTRUCTORs. */
2549 : :
2550 : : static void
2551 : 744493 : free_constructor (tree t)
2552 : : {
2553 : 744493 : if (!t || TREE_CODE (t) != CONSTRUCTOR)
2554 : 0 : return;
2555 : 744493 : releasing_vec ctors;
2556 : 744493 : vec_safe_push (ctors, t);
2557 : 1514473 : while (!ctors->is_empty ())
2558 : : {
2559 : 769980 : tree c = ctors->pop ();
2560 : 769980 : if (vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (c))
2561 : : {
2562 : : constructor_elt *ce;
2563 : 199891 : for (HOST_WIDE_INT i = 0; vec_safe_iterate (elts, i, &ce); ++i)
2564 : 136228 : if (ce->value && TREE_CODE (ce->value) == CONSTRUCTOR)
2565 : 25487 : vec_safe_push (ctors, ce->value);
2566 : 63663 : ggc_free (elts);
2567 : : }
2568 : 769980 : ggc_free (c);
2569 : : }
2570 : 744493 : }
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 : 12206028 : addr_of_non_const_var (tree *tp, int *walk_subtrees, void *data)
2578 : : {
2579 : 12206028 : if (TREE_CODE (*tp) == ADDR_EXPR)
2580 : 1815457 : if (tree var = get_base_address (TREE_OPERAND (*tp, 0)))
2581 : 1815457 : if (VAR_P (var) && TREE_STATIC (var))
2582 : : {
2583 : 792013 : if (DECL_NAME (var) == heap_uninit_identifier
2584 : 792013 : || DECL_NAME (var) == heap_identifier
2585 : 792013 : || DECL_NAME (var) == heap_vec_uninit_identifier
2586 : 1584026 : || DECL_NAME (var) == heap_vec_identifier)
2587 : : return var;
2588 : :
2589 : 792013 : constexpr_global_ctx *global = (constexpr_global_ctx *) data;
2590 : 792013 : if (global->get_value (var))
2591 : : return var;
2592 : : }
2593 : 12082517 : 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 : 72644752 : 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 : 72644752 : int nargs = call_expr_nargs (t);
2611 : 72644752 : tree parms = DECL_ARGUMENTS (fun);
2612 : 72644752 : int i, j = 0;
2613 : 72644752 : if (DECL_HAS_IN_CHARGE_PARM_P (fun) && fun != orig_fun)
2614 : 1226 : ++nargs;
2615 : 72644752 : if (DECL_HAS_VTT_PARM_P (fun)
2616 : 1228 : && fun != orig_fun
2617 : 72645978 : && (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 : 72644752 : int nparms = list_length (parms);
2622 : 72644752 : int nbinds = nargs < nparms ? nargs : nparms;
2623 : 72644752 : 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 : 72644752 : if (cxx_dialect < cxx26
2628 : 72644752 : && (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 : 112103490 : for (i = 0; i < nargs; ++i)
2641 : : {
2642 : 71982044 : tree x, arg;
2643 : 71982044 : tree type = parms ? TREE_TYPE (parms) : void_type_node;
2644 : 71982044 : if (parms && DECL_BY_REFERENCE (parms))
2645 : 6944 : type = TREE_TYPE (type);
2646 : 71982044 : if (i == 1
2647 : 71982044 : && j == 0
2648 : 11775174 : && DECL_HAS_IN_CHARGE_PARM_P (fun)
2649 : 71983166 : && 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 : 71980922 : else if (i == 2
2659 : 71980922 : && j == -1
2660 : 1122 : && DECL_HAS_VTT_PARM_P (fun)
2661 : 1122 : && orig_fun != fun
2662 : 71982044 : && (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 : 71980598 : 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 : 115668412 : if (i == 0 && DECL_CONSTRUCTOR_P (fun)
2674 : 80651617 : && is_dummy_object (x))
2675 : : {
2676 : 3793826 : x = ctx->object;
2677 : 3793826 : x = build_address (x);
2678 : : }
2679 : 71982044 : if (TREE_ADDRESSABLE (type))
2680 : : {
2681 : : /* Undo convert_for_arg_passing work here. */
2682 : 10036 : x = convert_from_reference (x);
2683 : 10036 : 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 : 71972008 : 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 : 57834206 : if (i == 0 && DECL_OBJECT_MEMBER_FUNCTION_P (fun)
2697 : 30898194 : && 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 : 72024997 : && !(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 : 71982044 : if (*non_constant_p && ctx->quiet)
2710 : : break;
2711 : 39458761 : if (*jump_target)
2712 : : break;
2713 : : /* Just discard ellipsis args after checking their constantitude. */
2714 : 39458755 : if (!parms)
2715 : 400 : continue;
2716 : :
2717 : 39458355 : if (!*non_constant_p)
2718 : : {
2719 : : /* Make sure the binding has the same type as the parm. But
2720 : : only for constant args. */
2721 : 39457608 : if (TREE_ADDRESSABLE (type))
2722 : : {
2723 : 7446 : 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 : 39450162 : else if (!TYPE_REF_P (type))
2731 : 29959166 : arg = adjust_temp_type (type, arg);
2732 : 39457608 : if (!TREE_CONSTANT (arg))
2733 : 27742930 : *non_constant_args = true;
2734 : 11714678 : 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 : 11714678 : else if (!*non_constant_args
2744 : 11714678 : && cp_walk_tree (&arg, addr_of_non_const_var, ctx->global,
2745 : : NULL))
2746 : 123511 : *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 : 39457608 : if (i == 0 && DECL_VIRTUAL_P (fun))
2752 : : {
2753 : 15658 : tree addr = arg;
2754 : 15658 : STRIP_NOPS (addr);
2755 : 15658 : if (TREE_CODE (addr) == ADDR_EXPR)
2756 : : {
2757 : 15638 : tree obj = TREE_OPERAND (addr, 0);
2758 : 15638 : while (TREE_CODE (obj) == COMPONENT_REF
2759 : 7219 : && DECL_FIELD_IS_BASE (TREE_OPERAND (obj, 1))
2760 : 22864 : && !same_type_ignoring_top_level_qualifiers_p
2761 : 7206 : (TREE_TYPE (obj), DECL_CONTEXT (fun)))
2762 : 20 : obj = TREE_OPERAND (obj, 0);
2763 : 15638 : if (obj != TREE_OPERAND (addr, 0))
2764 : 17 : arg = build_fold_addr_expr_with_type (obj,
2765 : : TREE_TYPE (arg));
2766 : : }
2767 : : }
2768 : 39457608 : TREE_VEC_ELT (binds, i) = arg;
2769 : : }
2770 : 39458355 : 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 : 39798166 : push_cx_call_context (tree call)
2786 : : {
2787 : 39798166 : ++call_stack_tick;
2788 : 39798166 : if (!EXPR_HAS_LOCATION (call))
2789 : 17857 : SET_EXPR_LOCATION (call, input_location);
2790 : 39798166 : call_stack.safe_push (call);
2791 : 39798166 : int len = call_stack.length ();
2792 : 39798166 : if (len > max_constexpr_depth)
2793 : 30 : return false;
2794 : : return len;
2795 : : }
2796 : :
2797 : : static void
2798 : 39798166 : pop_cx_call_context (void)
2799 : : {
2800 : 39798166 : ++call_stack_tick;
2801 : 39798166 : call_stack.pop ();
2802 : 39798166 : }
2803 : :
2804 : : vec<tree>
2805 : 212853 : cx_error_context (void)
2806 : : {
2807 : 212853 : vec<tree> r = vNULL;
2808 : 212853 : if (call_stack_tick != last_cx_error_tick
2809 : 212853 : && !call_stack.is_empty ())
2810 : : r = call_stack;
2811 : 212853 : last_cx_error_tick = call_stack_tick;
2812 : 212853 : 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 : 1355 : find_failing_clause (const constexpr_ctx *ctx, tree expr)
2865 : : {
2866 : 1355 : if (TREE_CODE (expr) == TRUTH_ANDIF_EXPR)
2867 : 168 : if (tree e = find_failing_clause_r (ctx, expr))
2868 : 1355 : expr = e;
2869 : 1355 : 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 : 1355 : 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 : 1355 : bad = tree_strip_nop_conversions (bad);
2883 : 1355 : 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 : 1355 : if (concept_check_p (bad) || TREE_CODE (bad) == REQUIRES_EXPR)
2889 : 243 : diagnose_constraints (cloc, bad, NULL_TREE);
2890 : 1112 : else if (COMPARISON_CLASS_P (bad)
2891 : 1112 : && 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 : 964 : else if (show_expr_p)
2899 : 740 : inform (cloc, "%qE evaluates to false", bad);
2900 : 1355 : }
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 : 271973 : 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 : 271973 : enum tree_code opcode = ERROR_MARK;
2971 : :
2972 : 271973 : 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 : 46978 : case IFN_MUL_OVERFLOW:
2995 : 46978 : opcode = MULT_EXPR;
2996 : 46978 : 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 : 47775 : tree arg0 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), lval,
3027 : : non_constant_p, overflow_p,
3028 : : jump_target);
3029 : 47775 : tree arg1 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 1), lval,
3030 : : non_constant_p, overflow_p,
3031 : : jump_target);
3032 : 47775 : if (*jump_target)
3033 : : return NULL_TREE;
3034 : 47775 : 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 : 47773 : *non_constant_p = true;
3051 : 47773 : return t;
3052 : : }
3053 : :
3054 : : /* Clean CONSTRUCTOR_NO_CLEARING from CTOR and its sub-aggregates. */
3055 : :
3056 : : static void
3057 : 4060651 : clear_no_implicit_zero (tree ctor)
3058 : : {
3059 : 4060651 : if (CONSTRUCTOR_NO_CLEARING (ctor))
3060 : : {
3061 : 989392 : CONSTRUCTOR_NO_CLEARING (ctor) = false;
3062 : 4618651 : for (auto &e: CONSTRUCTOR_ELTS (ctor))
3063 : 1672023 : if (TREE_CODE (e.value) == CONSTRUCTOR)
3064 : 268895 : clear_no_implicit_zero (e.value);
3065 : : }
3066 : 4060651 : }
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 : 30350205 : cxx_replaceable_global_alloc_fn (tree fndecl)
3112 : : {
3113 : 30350205 : return (cxx_dialect >= cxx20
3114 : 9051115 : && IDENTIFIER_NEWDEL_OP_P (DECL_NAME (fndecl))
3115 : 314928 : && CP_DECL_CONTEXT (fndecl) == global_namespace
3116 : 30664986 : && (DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl)
3117 : 124256 : || 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 : 30134024 : cxx_placement_new_fn (tree fndecl)
3125 : : {
3126 : 30134024 : 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 : 96170 : is_std_construct_at (tree fndecl)
3133 : : {
3134 : 96170 : if (!decl_in_std_namespace_p (fndecl))
3135 : : return false;
3136 : :
3137 : 90381 : tree name = DECL_NAME (fndecl);
3138 : 90381 : return name && id_equal (name, "construct_at");
3139 : : }
3140 : :
3141 : : /* Overload for the above taking constexpr_call*. */
3142 : :
3143 : : static inline bool
3144 : 68591 : is_std_construct_at (const constexpr_call *call)
3145 : : {
3146 : 68591 : return (call
3147 : 45864 : && call->fundef
3148 : 114455 : && is_std_construct_at (call->fundef->decl));
3149 : : }
3150 : :
3151 : : /* True if CTX is an instance of std::NAME class. */
3152 : :
3153 : : bool
3154 : 4039723 : is_std_class (tree ctx, const char *name)
3155 : : {
3156 : 4039723 : if (ctx == NULL_TREE || !CLASS_TYPE_P (ctx) || !TYPE_MAIN_DECL (ctx))
3157 : : return false;
3158 : :
3159 : 4038871 : tree decl = TYPE_MAIN_DECL (ctx);
3160 : 4038871 : tree dname = DECL_NAME (decl);
3161 : 4038871 : if (dname == NULL_TREE || !id_equal (dname, name))
3162 : : return false;
3163 : :
3164 : 187179 : return decl_in_std_namespace_p (decl);
3165 : : }
3166 : :
3167 : : /* True if CTX is an instance of std::allocator. */
3168 : :
3169 : : bool
3170 : 70859 : is_std_allocator (tree ctx)
3171 : : {
3172 : 70859 : 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 : 78469 : is_std_allocator_allocate (tree fndecl)
3179 : : {
3180 : 78469 : tree name = DECL_NAME (fndecl);
3181 : 78469 : if (name == NULL_TREE
3182 : 78469 : || !(id_equal (name, "allocate") || id_equal (name, "deallocate")))
3183 : : return false;
3184 : :
3185 : 66731 : return is_std_allocator (DECL_CONTEXT (fndecl));
3186 : : }
3187 : :
3188 : : /* Overload for the above taking constexpr_call*. */
3189 : :
3190 : : static inline bool
3191 : 33753 : is_std_allocator_allocate (const constexpr_call *call)
3192 : : {
3193 : 33753 : return (call
3194 : 7464 : && call->fundef
3195 : 41217 : && 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 : 4685 : is_std_source_location_current (tree fndecl)
3202 : : {
3203 : 4685 : if (!decl_in_std_namespace_p (fndecl))
3204 : : return false;
3205 : :
3206 : 852 : tree name = DECL_NAME (fndecl);
3207 : 852 : 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 : 11268 : is_std_source_location_current (const constexpr_call *call)
3222 : : {
3223 : 11268 : return (call
3224 : 4685 : && call->fundef
3225 : 15953 : && is_std_source_location_current (call->fundef->decl));
3226 : : }
3227 : :
3228 : : /* Return true if FNDECL is __dynamic_cast. */
3229 : :
3230 : : static inline bool
3231 : 30108302 : cxx_dynamic_cast_fn_p (tree fndecl)
3232 : : {
3233 : 30108302 : return (cxx_dialect >= cxx20
3234 : 8809203 : && id_equal (DECL_NAME (fndecl), "__dynamic_cast")
3235 : 30113225 : && 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 : 4639849 : replace_decl_r (tree *tp, int *walk_subtrees, void *data)
3528 : : {
3529 : 4639849 : 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 : 4639849 : if (TREE_CODE (*tp) == ADDR_EXPR)
3536 : : {
3537 : 444342 : d->pset->add (*tp);
3538 : 444342 : auto save_changed = d->changed;
3539 : 444342 : d->changed = false;
3540 : 444342 : cp_walk_tree (&TREE_OPERAND (*tp, 0), replace_decl_r, d, nullptr);
3541 : 444342 : if (d->changed)
3542 : : {
3543 : 326 : cxx_mark_addressable (*tp);
3544 : 326 : recompute_tree_invariant_for_addr_expr (*tp);
3545 : : }
3546 : : else
3547 : 444016 : d->changed = save_changed;
3548 : 444342 : *walk_subtrees = 0;
3549 : : }
3550 : 4195507 : 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 : 4194844 : else if (TYPE_P (*tp)
3557 : 4194844 : || d->pset->add (*tp))
3558 : 716514 : *walk_subtrees = 0;
3559 : :
3560 : 4639849 : 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 : 633817 : replace_decl (tree *tp, tree decl, tree replacement)
3569 : : {
3570 : 633817 : gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
3571 : : (TREE_TYPE (decl), TREE_TYPE (replacement)));
3572 : 633817 : hash_set<tree> pset;
3573 : 633817 : replace_decl_data data = { decl, replacement, &pset, false };
3574 : 633817 : cp_walk_tree (tp, replace_decl_r, &data, NULL);
3575 : 633817 : return data.changed;
3576 : 633817 : }
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 : 3823943 : 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 : 7647886 : if (CLASS_TYPE_P (TREE_TYPE (object))
3640 : 7647886 : && 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 : 526327 : tree e = cxx_eval_constant_expression (ctx, object, vc_prvalue,
3645 : : non_constant_p, overflow_p,
3646 : : jump_target);
3647 : 526327 : if (!*non_constant_p
3648 : 4339907 : && !throws (jump_target)
3649 : 1042291 : && TREE_CODE (e) == CONSTRUCTOR)
3650 : 515964 : TREE_READONLY (e) = readonly_p;
3651 : : }
3652 : 3823943 : }
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 : 84010216 : 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 : 84010216 : location_t loc = cp_expr_loc_or_input_loc (t);
3665 : 84010216 : tree fun = get_function_named_in_call (t);
3666 : :
3667 : 84010216 : if (fun == NULL_TREE)
3668 : 271973 : return cxx_eval_internal_function (ctx, t, lval,
3669 : : non_constant_p, overflow_p,
3670 : 271973 : jump_target);
3671 : :
3672 : 83738243 : if (TREE_CODE (fun) != FUNCTION_DECL)
3673 : : {
3674 : : /* Might be a constexpr function pointer. */
3675 : 118154 : fun = cxx_eval_constant_expression (ctx, fun, vc_prvalue,
3676 : : non_constant_p, overflow_p,
3677 : : jump_target);
3678 : 118154 : if (*jump_target)
3679 : : return NULL_TREE;
3680 : 118154 : STRIP_NOPS (fun);
3681 : 118154 : if (TREE_CODE (fun) == ADDR_EXPR)
3682 : 18987 : 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 : 83738243 : if (TREE_CODE (fun) != FUNCTION_DECL)
3715 : : {
3716 : 99167 : if (!ctx->quiet && !*non_constant_p)
3717 : 0 : error_at (loc, "expression %qE does not designate a %<constexpr%> "
3718 : : "function", fun);
3719 : 99167 : *non_constant_p = true;
3720 : 99167 : return t;
3721 : : }
3722 : 83639076 : tree orig_fun = fun;
3723 : 83639076 : if (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun))
3724 : 8877552 : fun = DECL_CLONED_FUNCTION (fun);
3725 : :
3726 : 83639076 : if (is_ubsan_builtin_p (fun))
3727 : 58 : return void_node;
3728 : :
3729 : 83639018 : if (fndecl_built_in_p (fun))
3730 : 10879510 : return cxx_eval_builtin_function_call (ctx, t, fun,
3731 : : lval, non_constant_p, overflow_p,
3732 : 10879510 : jump_target);
3733 : 72759508 : 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 : 72759481 : bool non_constexpr_call = false;
3737 : 72759481 : if (!maybe_constexpr_fn (fun))
3738 : : {
3739 : 192875 : if (TREE_CODE (t) == CALL_EXPR
3740 : 180247 : && cxx_replaceable_global_alloc_fn (fun)
3741 : 262807 : && (CALL_FROM_NEW_OR_DELETE_P (t)
3742 : 21552 : || is_std_allocator_allocate (ctx->call)))
3743 : : {
3744 : 50226 : const bool new_op_p = IDENTIFIER_NEW_OP_P (DECL_NAME (fun));
3745 : 50226 : const int nargs = call_expr_nargs (t);
3746 : 50226 : tree arg0 = NULL_TREE;
3747 : 79503 : for (int i = 0; i < nargs; ++i)
3748 : : {
3749 : 50608 : tree arg = CALL_EXPR_ARG (t, i);
3750 : 50608 : arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
3751 : : non_constant_p, overflow_p,
3752 : : jump_target);
3753 : 50608 : if (*jump_target)
3754 : : return NULL_TREE;
3755 : : /* Deleting a non-constant pointer has a better error message
3756 : : below. */
3757 : 50600 : if (new_op_p || i != 0)
3758 : 49205 : VERIFY_CONSTANT (arg);
3759 : 27882 : if (i == 0)
3760 : : arg0 = arg;
3761 : : }
3762 : 28895 : gcc_assert (arg0);
3763 : 28895 : if (new_op_p)
3764 : : {
3765 : 27500 : 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 : 55000 : tree type = build_array_type_nelts (char_type_node,
3776 : 27500 : tree_to_uhwi (arg0));
3777 : 27500 : tree var = build_decl (loc, VAR_DECL,
3778 : 27500 : (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
3779 : : & OVL_OP_FLAG_VEC)
3780 : : ? heap_vec_uninit_identifier
3781 : : : heap_uninit_identifier,
3782 : 27500 : type);
3783 : 27500 : DECL_ARTIFICIAL (var) = 1;
3784 : 27500 : ctx->global->heap_vars.safe_push (var);
3785 : 27500 : ctx->global->put_value (var, NULL_TREE);
3786 : 27500 : return fold_convert (ptr_type_node, build_address (var));
3787 : : }
3788 : : else
3789 : : {
3790 : 1395 : STRIP_NOPS (arg0);
3791 : 1395 : if (TREE_CODE (arg0) == ADDR_EXPR
3792 : 1395 : && VAR_P (TREE_OPERAND (arg0, 0)))
3793 : : {
3794 : 1395 : tree var = TREE_OPERAND (arg0, 0);
3795 : 1395 : if (DECL_NAME (var) == heap_uninit_identifier
3796 : 1395 : || DECL_NAME (var) == heap_identifier)
3797 : : {
3798 : 1232 : 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 : 1223 : DECL_NAME (var) = heap_deleted_identifier;
3814 : 1223 : ctx->global->destroy_value (var);
3815 : 1223 : ctx->global->heap_dealloc_count++;
3816 : 1223 : 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 : 142649 : if (TREE_CODE (t) == CALL_EXPR
3860 : 130021 : && cxx_placement_new_fn (fun)
3861 : 164113 : && 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 : 137333 : 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 : 134990 : else if (enum cxa_builtin kind = cxx_cxa_builtin_fn_p (fun))
3885 : 26438 : return cxx_eval_cxa_builtin_fn (ctx, t, kind, fun,
3886 : : non_constant_p, overflow_p,
3887 : 26438 : 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 : 108552 : 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 : 72644752 : constexpr_ctx new_ctx = *ctx;
3910 : 72644752 : ctx = &new_ctx;
3911 : 81314342 : if (DECL_CONSTRUCTOR_P (fun) && !ctx->object
3912 : 73928266 : && 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 : 145289504 : if (DECL_IMMEDIATE_FUNCTION_P (fun))
3925 : 964561 : 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 : 72644752 : 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 : 72644752 : bool non_constant_args = false;
3939 : 72644752 : constexpr_call new_call;
3940 : 72644752 : new_call.bindings
3941 : 72644752 : = 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 : 72644752 : class free_bindings
3949 : : {
3950 : : tree *bindings;
3951 : : public:
3952 : 72644752 : free_bindings (tree &b): bindings (&b) { }
3953 : 72644752 : ~free_bindings () { if (bindings) ggc_free (*bindings); }
3954 : 2957177 : void preserve () { bindings = NULL; }
3955 : 72644752 : } fb (new_call.bindings);
3956 : :
3957 : 72644752 : if (*jump_target)
3958 : : return NULL_TREE;
3959 : 72644746 : if (*non_constant_p)
3960 : : return t;
3961 : 40120694 : if (non_constexpr_call)
3962 : : {
3963 : 5691 : if (!ctx->quiet)
3964 : : {
3965 : 151 : if (!lambda_static_thunk_p (fun))
3966 : 151 : error_at (loc, "call to non-%<constexpr%> function %qD", fun);
3967 : 151 : explain_invalid_constexpr_fn (fun);
3968 : : }
3969 : 5691 : *non_constant_p = true;
3970 : 5691 : return t;
3971 : : }
3972 : :
3973 : : /* We can't defer instantiating the function any longer. */
3974 : 40115003 : if (!DECL_INITIAL (fun)
3975 : 1656416 : && (DECL_TEMPLOID_INSTANTIATION (fun) || DECL_DEFAULTED_FN (fun))
3976 : 40115003 : && !uid_sensitive_constexpr_evaluation_p ())
3977 : : {
3978 : 1457593 : location_t save_loc = input_location;
3979 : 1457593 : input_location = loc;
3980 : 1457593 : ++function_depth;
3981 : 1457593 : if (ctx->manifestly_const_eval == mce_true)
3982 : 166698 : FNDECL_MANIFESTLY_CONST_EVALUATED (fun) = true;
3983 : 1457593 : if (DECL_TEMPLOID_INSTANTIATION (fun))
3984 : 1457585 : instantiate_decl (fun, /*defer_ok*/false, /*expl_inst*/false);
3985 : : else
3986 : 8 : synthesize_method (fun);
3987 : 1457593 : --function_depth;
3988 : 1457593 : input_location = save_loc;
3989 : : }
3990 : :
3991 : : /* If in direct recursive call, optimize definition search. */
3992 : 40115003 : if (ctx && ctx->call && ctx->call->fundef && ctx->call->fundef->decl == fun)
3993 : 27533 : new_call.fundef = ctx->call->fundef;
3994 : : else
3995 : : {
3996 : 40087470 : new_call.fundef = retrieve_constexpr_fundef (fun);
3997 : 40087470 : if (new_call.fundef == NULL || new_call.fundef->body == NULL
3998 : 39792667 : || new_call.fundef->result == error_mark_node
3999 : 39770636 : || fun == current_function_decl)
4000 : : {
4001 : 316837 : 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 : 316837 : *non_constant_p = true;
4023 : 316837 : return t;
4024 : : }
4025 : : }
4026 : :
4027 : : /* Don't complain about problems evaluating an ill-formed function. */
4028 : 39798166 : if (function *f = DECL_STRUCT_FUNCTION (fun))
4029 : 39798166 : if (f->language->erroneous)
4030 : 509 : new_ctx.quiet = true;
4031 : :
4032 : 39798166 : int depth_ok = push_cx_call_context (t);
4033 : :
4034 : : /* Remember the object we are constructing or destructing. */
4035 : 39798166 : tree new_obj = NULL_TREE;
4036 : 79596332 : 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 : 4618599 : new_obj = TREE_VEC_ELT (new_call.bindings, 0);
4042 : 4618599 : bool empty_base = false;
4043 : 4618599 : new_obj = cxx_fold_indirect_ref (ctx, loc, DECL_CONTEXT (fun), new_obj,
4044 : : &empty_base, jump_target);
4045 : 4618599 : 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 : 4618599 : if (empty_base)
4051 : : new_obj = NULL_TREE;
4052 : 1909766 : else if (ctx->call && ctx->call->fundef
4053 : 7664140 : && DECL_CONSTRUCTOR_P (ctx->call->fundef->decl))
4054 : : {
4055 : 1196331 : tree cur_obj = TREE_VEC_ELT (ctx->call->bindings, 0);
4056 : 1196331 : STRIP_NOPS (cur_obj);
4057 : 1196331 : if (TREE_CODE (cur_obj) == ADDR_EXPR)
4058 : 1195084 : cur_obj = TREE_OPERAND (cur_obj, 0);
4059 : 1196331 : 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 : 794656 : new_obj = NULL_TREE;
4065 : : }
4066 : : }
4067 : :
4068 : 39798166 : tree result = NULL_TREE;
4069 : :
4070 : 39798166 : constexpr_call *entry = NULL;
4071 : 39798166 : if (depth_ok && !non_constant_args && ctx->strict)
4072 : : {
4073 : 19152939 : new_call.hash = constexpr_fundef_hasher::hash (new_call.fundef);
4074 : 19152939 : new_call.hash
4075 : 19152939 : = iterative_hash_template_arg (new_call.bindings, new_call.hash);
4076 : :
4077 : : /* If we have seen this call before, we are done. */
4078 : 19152939 : maybe_initialize_constexpr_call_table ();
4079 : 19152939 : bool insert = depth_ok < constexpr_cache_depth;
4080 : 19152939 : constexpr_call **slot
4081 : 19466743 : = constexpr_call_table->find_slot (&new_call,
4082 : : insert ? INSERT : NO_INSERT);
4083 : 19152939 : entry = slot ? *slot : NULL;
4084 : 18998411 : if (entry == NULL)
4085 : : {
4086 : : /* Only cache up to constexpr_cache_depth to limit memory use. */
4087 : 3111705 : 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 : 2957177 : *slot = entry = ggc_alloc<constexpr_call> ();
4092 : 2957177 : *entry = new_call;
4093 : 2957177 : entry->result (ctx->manifestly_const_eval) = unknown_type_node;
4094 : 2957177 : 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 : 16041234 : 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 : 16041228 : result = entry->result (ctx->manifestly_const_eval);
4110 : : }
4111 : :
4112 : 39798166 : 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 : 39798136 : bool cacheable = !!entry;
4124 : 39798136 : if (result && result != error_mark_node)
4125 : : /* OK */;
4126 : 26811217 : 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 : 26811217 : else if (tree copy = get_fundef_copy (new_call.fundef))
4136 : : {
4137 : 26811217 : tree body, parms, res;
4138 : 26811217 : releasing_vec ctors;
4139 : :
4140 : : /* Reuse or create a new unshared copy of this function's body. */
4141 : 26811217 : body = TREE_PURPOSE (copy);
4142 : 26811217 : parms = TREE_VALUE (copy);
4143 : 26811217 : res = TREE_TYPE (copy);
4144 : :
4145 : : /* Associate the bindings with the remapped parms. */
4146 : 26811217 : tree bound = new_call.bindings;
4147 : 26811217 : tree remapped = parms;
4148 : 58377844 : for (int i = 0; i < TREE_VEC_LENGTH (bound); ++i)
4149 : : {
4150 : 31566627 : tree arg = TREE_VEC_ELT (bound, i);
4151 : 31566627 : 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 : 1967505 : arg = unshare_expr_without_location (arg);
4157 : 1967505 : 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 : 1967505 : arg = unshare_constructor (arg);
4163 : 1967505 : if (TREE_CODE (arg) == CONSTRUCTOR)
4164 : 744017 : vec_safe_push (ctors, arg);
4165 : : }
4166 : 31566627 : ctx->global->put_value (remapped, arg);
4167 : 31566627 : remapped = DECL_CHAIN (remapped);
4168 : : }
4169 : 26811217 : 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 : 26811217 : gcc_assert (!DECL_BY_REFERENCE (res));
4178 : 26811217 : ctx->global->put_value (res, NULL_TREE);
4179 : :
4180 : : /* Remember the current call we're evaluating. */
4181 : 26811217 : constexpr_ctx call_ctx = *ctx;
4182 : 26811217 : call_ctx.call = &new_call;
4183 : 26811217 : unsigned save_heap_alloc_count = ctx->global->heap_vars.length ();
4184 : 26811217 : 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 : 53622434 : if (DECL_IMMEDIATE_FUNCTION_P (fun))
4189 : 386734 : 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 : 30635160 : if (new_obj && DECL_DESTRUCTOR_P (fun))
4194 : 191384 : 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 : 26811217 : if (new_obj
4200 : 7647886 : && DECL_CONSTRUCTOR_P (fun)
4201 : 3632559 : && TREE_CODE (new_obj) == COMPONENT_REF
4202 : 27668910 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (new_obj, 0))) == UNION_TYPE)
4203 : : {
4204 : 2932 : tree ctor = build_constructor (TREE_TYPE (new_obj), NULL);
4205 : 2932 : CONSTRUCTOR_NO_CLEARING (ctor) = true;
4206 : 2932 : tree activate = build2 (INIT_EXPR, TREE_TYPE (new_obj),
4207 : : new_obj, ctor);
4208 : 2932 : cxx_eval_constant_expression (ctx, activate,
4209 : : lval, non_constant_p, overflow_p,
4210 : : jump_target);
4211 : 2932 : ggc_free (activate);
4212 : 2932 : if (*jump_target)
4213 : 0 : return NULL_TREE;
4214 : : }
4215 : :
4216 : 26811217 : tree jmp_target = NULL_TREE;
4217 : 26811217 : cxx_eval_constant_expression (&call_ctx, body,
4218 : : vc_discard, non_constant_p, overflow_p,
4219 : : &jmp_target);
4220 : :
4221 : 26811217 : 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 : 53621856 : 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 : 4423883 : result = lval ? ctx->object : ctx->ctor;
4234 : 22387045 : else if (VOID_TYPE_P (TREE_TYPE (res)))
4235 : 1285380 : result = void_node;
4236 : : else
4237 : : {
4238 : 21101665 : result = ctx->global->get_value (res);
4239 : 7574310 : if (result == NULL_TREE && !*non_constant_p
4240 : 21101859 : && !DECL_DESTRUCTOR_P (fun))
4241 : : {
4242 : 97 : if (!ctx->quiet)
4243 : 6 : error ("%<constexpr%> call flows off the end "
4244 : : "of the function");
4245 : 97 : *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 : 30635160 : if (new_obj && DECL_CONSTRUCTOR_P (fun))
4254 : 3632559 : 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 : 26811217 : destroy_value_checked (ctx, res, non_constant_p);
4259 : 58377850 : for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
4260 : 31566633 : destroy_value_checked (ctx, parm, non_constant_p);
4261 : :
4262 : : /* Free any parameter CONSTRUCTORs we aren't returning directly. */
4263 : 27555234 : while (!ctors->is_empty ())
4264 : : {
4265 : 744017 : tree c = ctors->pop ();
4266 : 744017 : if (c != result)
4267 : 744017 : free_constructor (c);
4268 : : }
4269 : :
4270 : : /* Make the unshared function copy we used available for re-use. */
4271 : 26811217 : 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 : 26811217 : if (cacheable
4280 : 32822487 : && (save_heap_alloc_count != ctx->global->heap_vars.length ()
4281 : 6010537 : || (save_heap_dealloc_count
4282 : 6010537 : != ctx->global->heap_dealloc_count)))
4283 : : {
4284 : 733 : tree heap_var;
4285 : 733 : unsigned int i;
4286 : 733 : if ((ctx->global->heap_vars.length ()
4287 : 733 : - ctx->global->heap_dealloc_count)
4288 : 733 : != save_heap_alloc_count - save_heap_dealloc_count)
4289 : : cacheable = false;
4290 : : else
4291 : 1948 : FOR_EACH_VEC_ELT_FROM (ctx->global->heap_vars, i, heap_var,
4292 : : save_heap_alloc_count)
4293 : 1437 : 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 : 26811217 : if (!*non_constant_p
4303 : 17608914 : && ctx->object
4304 : 5324645 : && CLASS_TYPE_P (TREE_TYPE (res))
4305 : 28134377 : && !is_empty_class (TREE_TYPE (res)))
4306 : : {
4307 : 633641 : if (!same_type_ignoring_top_level_qualifiers_p
4308 : 633641 : (TREE_TYPE (res), TREE_TYPE (ctx->object)))
4309 : 0 : *non_constant_p = true;
4310 : 633641 : 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 : 26810979 : if (cacheable && !reduced_constant_expression_p (result))
4327 : : cacheable = false;
4328 : 26811217 : }
4329 : : else
4330 : : /* Couldn't get a function copy to evaluate. */
4331 : 0 : *non_constant_p = true;
4332 : :
4333 : 39798136 : if (result == error_mark_node)
4334 : 0 : *non_constant_p = true;
4335 : 39798136 : if (*non_constant_p || *overflow_p)
4336 : 9202459 : result = error_mark_node;
4337 : 30595677 : else if (!result)
4338 : 1275939 : result = void_node;
4339 : 39798136 : if (entry)
4340 : : {
4341 : 37996822 : entry->result (ctx->manifestly_const_eval)
4342 : 18998411 : = cacheable ? result : error_mark_node;
4343 : :
4344 : 18998411 : if (result != error_mark_node
4345 : 16010553 : && 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 : 3410613 : if (!entry->result (mce_true))
4351 : 631536 : entry->result (mce_true) = entry->result (mce_unknown);
4352 : 3410613 : if (!entry->result (mce_false))
4353 : 620007 : 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 : 39798166 : if (TREE_CODE (result) == CONSTRUCTOR
4366 : 39798166 : && (cxx_dialect < cxx20
4367 : 7337690 : || !DECL_CONSTRUCTOR_P (fun)))
4368 : 3791756 : clear_no_implicit_zero (result);
4369 : :
4370 : 39798166 : pop_cx_call_context ();
4371 : 39798166 : return result;
4372 : 72644752 : }
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 : 611925868 : reduced_constant_expression_p (tree t, tree sz /* = NULL_TREE */)
4382 : : {
4383 : 611925868 : if (t == NULL_TREE)
4384 : : return false;
4385 : :
4386 : 608939392 : 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 : 43625103 : case CONSTRUCTOR:
4396 : : /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
4397 : 43625103 : tree field;
4398 : 43625103 : 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 : 43609809 : if (CONSTRUCTOR_NO_CLEARING (t))
4403 : : {
4404 : 1268875 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
4405 : : {
4406 : : /* There must be a valid constant initializer at every array
4407 : : index. */
4408 : 1033 : tree min = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (t)));
4409 : 1033 : tree max = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (t)));
4410 : 1033 : tree cursor = min;
4411 : 21480 : for (auto &e: CONSTRUCTOR_ELTS (t))
4412 : : {
4413 : 18491 : if (!reduced_constant_expression_p (e.value))
4414 : : return false;
4415 : 18441 : if (array_index_cmp (cursor, e.index) != 0)
4416 : : return false;
4417 : 18381 : if (TREE_CODE (e.index) == RANGE_EXPR)
4418 : 0 : cursor = TREE_OPERAND (e.index, 1);
4419 : 18381 : 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 : 18381 : cursor = int_const_binop (PLUS_EXPR, cursor,
4425 : 18381 : size_one_node);
4426 : : }
4427 : 923 : if (find_array_ctor_elt (t, max) == -1)
4428 : : return false;
4429 : 837 : goto ok;
4430 : : }
4431 : 1267842 : else if (cxx_dialect >= cxx20
4432 : 1267842 : && TREE_CODE (TREE_TYPE (t)) == UNION_TYPE)
4433 : : {
4434 : 4221346 : 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 : 1266049 : field = next_subobject_field (TYPE_FIELDS (TREE_TYPE (t)));
4442 : : }
4443 : : else
4444 : : field = NULL_TREE;
4445 : 271280295 : for (auto &e: CONSTRUCTOR_ELTS (t))
4446 : : {
4447 : : /* If VAL is null, we're in the middle of initializing this
4448 : : element. */
4449 : 171447272 : if (!reduced_constant_expression_p (e.value,
4450 : 171447272 : (e.index
4451 : 171447272 : && (TREE_CODE (e.index)
4452 : : == FIELD_DECL))
4453 : 54902184 : ? 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 : 170937580 : if (is_empty_field (e.index)
4459 : 170937580 : && 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 : 170328185 : for (; field && e.index != field;
4464 : 95758 : field = next_subobject_field (DECL_CHAIN (field)))
4465 : 96507 : if (!is_really_empty_class (TREE_TYPE (field),
4466 : : /*ignore_vptr*/false))
4467 : : return false;
4468 : 170231678 : if (field)
4469 : 1442407 : field = next_subobject_field (DECL_CHAIN (field));
4470 : : }
4471 : : /* There could be a non-empty field at the end. */
4472 : 42468792 : for (; field; field = next_subobject_field (DECL_CHAIN (field)))
4473 : 79648 : 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 : 2245 : if (cxx_dialect >= cxx26
4479 : 1360 : && sz
4480 : 2622 : && tree_int_cst_le (sz, bit_position (field)))
4481 : : break;
4482 : 1993 : return false;
4483 : : }
4484 : 42389144 : ok:
4485 : 42390233 : if (CONSTRUCTOR_NO_CLEARING (t))
4486 : : /* All the fields are initialized. */
4487 : 1179451 : CONSTRUCTOR_NO_CLEARING (t) = false;
4488 : : return true;
4489 : :
4490 : 565280145 : default:
4491 : : /* FIXME are we calling this too much? */
4492 : 565280145 : 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 : 378909703 : verify_constant (tree t, bool allow_non_constant, bool *non_constant_p,
4562 : : bool *overflow_p)
4563 : : {
4564 : 370855335 : if (!*non_constant_p && !reduced_constant_expression_p (t)
4565 : 381043735 : && t != void_node)
4566 : : {
4567 : 2134005 : 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 : 2134005 : *non_constant_p = true;
4576 : : }
4577 : 378909703 : 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 : 378909628 : 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 : 32026523 : 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 : 32026523 : if ((code != LSHIFT_EXPR && code != RSHIFT_EXPR)
4601 : 3023743 : || TREE_CODE (lhs) != INTEGER_CST
4602 : 3023743 : || TREE_CODE (rhs) != INTEGER_CST)
4603 : : return false;
4604 : :
4605 : 3023743 : tree lhstype = TREE_TYPE (lhs);
4606 : 3023743 : 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 : 3023743 : 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 : 3023617 : 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 : 3023347 : if (code == LSHIFT_EXPR
4636 : 2901855 : && !TYPE_OVERFLOW_WRAPS (lhstype)
4637 : 1962128 : && cxx_dialect >= cxx11
4638 : 4969157 : && cxx_dialect < cxx20)
4639 : : {
4640 : 1443363 : 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 : 1443235 : tree t = build_int_cst (unsigned_type_node, uprec - 1);
4657 : 1443235 : t = fold_build2 (MINUS_EXPR, unsigned_type_node, t, rhs);
4658 : 1443235 : tree ulhs = fold_convert (unsigned_type_for (lhstype), lhs);
4659 : 1443235 : t = fold_build2 (RSHIFT_EXPR, TREE_TYPE (ulhs), ulhs, t);
4660 : 1443235 : 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 : 20225632 : 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 : 20225632 : tree r;
4683 : 20225632 : tree orig_arg = TREE_OPERAND (t, 0);
4684 : 20225632 : tree arg = cxx_eval_constant_expression (ctx, orig_arg, vc_prvalue,
4685 : : non_constant_p, overflow_p,
4686 : : jump_target);
4687 : 20225632 : if (*jump_target)
4688 : : return NULL_TREE;
4689 : 20225631 : VERIFY_CONSTANT (arg);
4690 : 16761906 : location_t loc = EXPR_LOCATION (t);
4691 : 16761906 : enum tree_code code = TREE_CODE (t);
4692 : 16761906 : tree type = TREE_TYPE (t);
4693 : 16761906 : r = fold_unary_loc (loc, code, type, arg);
4694 : 16761906 : 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 : 16761906 : 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 : 2907482 : 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 : 2907482 : STRIP_NOPS (lhs);
4715 : 2907482 : if (TREE_CODE (lhs) != ADDR_EXPR)
4716 : : return NULL_TREE;
4717 : :
4718 : 2594622 : lhs = TREE_OPERAND (lhs, 0);
4719 : :
4720 : : /* &A[i] p+ j => &A[i + j] */
4721 : 2594622 : 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 : 2601324 : && 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 : 2138762 : cxx_maybe_fold_addr_pointer_plus (tree t)
4778 : : {
4779 : 4277524 : while (CONVERT_EXPR_P (t)
4780 : 2328462 : && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0))))
4781 : 189700 : t = TREE_OPERAND (t, 0);
4782 : 2138762 : if (TREE_CODE (t) != POINTER_PLUS_EXPR)
4783 : : return NULL_TREE;
4784 : 1827385 : tree op0 = TREE_OPERAND (t, 0);
4785 : 1827385 : tree op1 = TREE_OPERAND (t, 1);
4786 : 1827385 : if (TREE_CODE (op1) != INTEGER_CST)
4787 : : return NULL_TREE;
4788 : 1827385 : while (CONVERT_EXPR_P (op0)
4789 : 3652627 : && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
4790 : 1825242 : op0 = TREE_OPERAND (op0, 0);
4791 : 1827385 : if (TREE_CODE (op0) != ADDR_EXPR)
4792 : : return NULL_TREE;
4793 : 1827385 : op1 = fold_convert (ptr_type_node, op1);
4794 : 1827385 : tree r = fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (op0)), op0, op1);
4795 : 1827385 : 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 : 48008216 : 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 : 48008216 : tree r = NULL_TREE;
4808 : 48008216 : tree orig_lhs = TREE_OPERAND (t, 0);
4809 : 48008216 : tree orig_rhs = TREE_OPERAND (t, 1);
4810 : 48008216 : tree lhs, rhs;
4811 : 48008216 : 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 : 48008216 : if (*non_constant_p)
4817 : : return t;
4818 : 35330898 : if (*jump_target)
4819 : : return NULL_TREE;
4820 : 35330852 : rhs = cxx_eval_constant_expression (ctx, orig_rhs, vc_prvalue,
4821 : : non_constant_p, overflow_p,
4822 : : jump_target);
4823 : 35330852 : if (*non_constant_p)
4824 : : return t;
4825 : 34413333 : if (*jump_target)
4826 : : return NULL_TREE;
4827 : :
4828 : 34413328 : location_t loc = EXPR_LOCATION (t);
4829 : 34413328 : enum tree_code code = TREE_CODE (t);
4830 : 34413328 : tree type = TREE_TYPE (t);
4831 : :
4832 : 34413328 : if (code == EQ_EXPR || code == NE_EXPR)
4833 : : {
4834 : 4283057 : bool is_code_eq = (code == EQ_EXPR);
4835 : :
4836 : 4283057 : 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 : 4283003 : else if ((TREE_CODE (lhs) == PTRMEM_CST
4855 : 4282876 : || TREE_CODE (rhs) == PTRMEM_CST)
4856 : 4283003 : && (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 : 4282876 : else if (TREE_CODE (lhs) == PTRMEM_CST)
4860 : 0 : lhs = cplus_expand_constant (lhs);
4861 : 4282876 : else if (TREE_CODE (rhs) == PTRMEM_CST)
4862 : 0 : rhs = cplus_expand_constant (rhs);
4863 : : }
4864 : 0 : if (r == NULL_TREE
4865 : 34413147 : && TREE_CODE_CLASS (code) == tcc_comparison
4866 : 11764433 : && POINTER_TYPE_P (TREE_TYPE (lhs)))
4867 : : {
4868 : 1069381 : if (tree lhso = cxx_maybe_fold_addr_pointer_plus (lhs))
4869 : 884805 : lhs = fold_convert (TREE_TYPE (lhs), lhso);
4870 : 1069381 : if (tree rhso = cxx_maybe_fold_addr_pointer_plus (rhs))
4871 : 942580 : rhs = fold_convert (TREE_TYPE (rhs), rhso);
4872 : : }
4873 : 2907637 : if (code == POINTER_PLUS_EXPR && !*non_constant_p
4874 : 37320965 : && 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 : 34413173 : else if (code == POINTER_PLUS_EXPR)
4882 : : {
4883 : 2907482 : r = cxx_fold_pointer_plus_expression (ctx, t, lhs, rhs, non_constant_p,
4884 : : overflow_p, jump_target);
4885 : 2907482 : if (*jump_target)
4886 : : return NULL_TREE;
4887 : : }
4888 : 31505691 : else if (code == SPACESHIP_EXPR)
4889 : : {
4890 : 23244 : r = genericize_spaceship (loc, type, lhs, rhs);
4891 : 23244 : return cxx_eval_constant_expression (ctx, r, lval, non_constant_p,
4892 : 23244 : overflow_p, jump_target);
4893 : : }
4894 : :
4895 : 34389929 : if (r == NULL_TREE)
4896 : : {
4897 : 34383090 : if (ctx->manifestly_const_eval == mce_true
4898 : 20043111 : && (flag_constexpr_fp_except
4899 : 20043108 : || TREE_CODE (type) != REAL_TYPE))
4900 : : {
4901 : 20030342 : auto ofcc = make_temp_override (folding_cxx_constexpr, true);
4902 : 20030342 : r = fold_binary_initializer_loc (loc, code, type, lhs, rhs);
4903 : 20030342 : }
4904 : : else
4905 : 14352748 : r = fold_binary_loc (loc, code, type, lhs, rhs);
4906 : : }
4907 : :
4908 : 34383090 : if (r == NULL_TREE
4909 : 2363504 : && (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
4910 : 100 : && TREE_CODE (lhs) == INTEGER_CST
4911 : 98 : && TREE_CODE (rhs) == INTEGER_CST
4912 : 36753433 : && 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 : 34389929 : if (r == NULL_TREE)
4924 : : {
4925 : 2363406 : if (lhs == orig_lhs && rhs == orig_rhs)
4926 : : r = t;
4927 : : else
4928 : 665249 : r = build2_loc (loc, code, type, lhs, rhs);
4929 : : }
4930 : 32026523 : 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 : 34389929 : bool ptr = INDIRECT_TYPE_P (TREE_TYPE (lhs));
4935 : 30321051 : if (!ptr)
4936 : 30321051 : 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 : 8057684 : 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 : 8057684 : tree val = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
4950 : : vc_prvalue,
4951 : : non_constant_p, overflow_p,
4952 : : jump_target);
4953 : 8057684 : if (*jump_target)
4954 : : return NULL_TREE;
4955 : 8057675 : VERIFY_CONSTANT (val);
4956 : 6406972 : 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 : 1033348 : if (ctx->manifestly_const_eval == mce_unknown)
4965 : : {
4966 : 1024298 : *non_constant_p = true;
4967 : 1024298 : return t;
4968 : : }
4969 : 9050 : val = constant_boolean_node (ctx->manifestly_const_eval == mce_true,
4970 : : boolean_type_node);
4971 : : }
4972 : : /* Don't VERIFY_CONSTANT the other operands. */
4973 : 5382674 : const bool zero_p = integer_zerop (val);
4974 : 5382674 : if (zero_p)
4975 : 2894304 : val = TREE_OPERAND (t, 2);
4976 : : else
4977 : 2488370 : val = TREE_OPERAND (t, 1);
4978 : 5382674 : if (TREE_CODE (t) == IF_STMT && !val)
4979 : 1344389 : 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 : 5382674 : if (ctx->manifestly_const_eval != mce_true
4986 : 1699228 : && !in_immediate_context ()
4987 : 1645751 : && !ctx->call
4988 : 6009587 : && cp_fold_immediate (&TREE_OPERAND (t, zero_p ? 1 : 2),
4989 : 626913 : 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 : 5382667 : if (TREE_CODE (val) == TARGET_EXPR)
5001 : 677 : val = TARGET_EXPR_INITIAL (val);
5002 : 5382667 : return cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
5003 : 5382667 : 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 : 80608 : array_index_cmp (tree key, tree index)
5060 : : {
5061 : 80608 : gcc_assert (TREE_CODE (key) == INTEGER_CST);
5062 : :
5063 : 80608 : switch (TREE_CODE (index))
5064 : : {
5065 : 77751 : case INTEGER_CST:
5066 : 77751 : return tree_int_cst_compare (key, index);
5067 : 2857 : case RANGE_EXPR:
5068 : 2857 : {
5069 : 2857 : tree lo = TREE_OPERAND (index, 0);
5070 : 2857 : tree hi = TREE_OPERAND (index, 1);
5071 : 2857 : if (tree_int_cst_lt (key, lo))
5072 : : return -1;
5073 : 2571 : else if (tree_int_cst_lt (hi, key))
5074 : : return 1;
5075 : : else
5076 : 2571 : 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 : 1627018 : find_array_ctor_elt (tree ary, tree dindex, bool insert)
5102 : : {
5103 : 1627018 : if (tree_int_cst_sgn (dindex) < 0)
5104 : : return -1;
5105 : :
5106 : 1627018 : unsigned HOST_WIDE_INT i = tree_to_uhwi (dindex);
5107 : 1627018 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ary);
5108 : 1627018 : unsigned HOST_WIDE_INT len = vec_safe_length (elts);
5109 : :
5110 : 2469248 : unsigned HOST_WIDE_INT end = len;
5111 : 2469248 : 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 : 1531630 : if (end > 0)
5116 : : {
5117 : 1509360 : tree cindex = (*elts)[end - 1].index;
5118 : 1509360 : 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 : 18151 : if (flag_checking)
5123 : 198474 : for (unsigned int j = 0; j < len - 1; ++j)
5124 : 180323 : gcc_assert ((*elts)[j].index == NULL_TREE
5125 : : && TREE_CODE ((*elts)[j].value) != RAW_DATA_CST);
5126 : 18151 : if (i < end)
5127 : 18151 : 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 : 937618 : 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 : 1491209 : else if (TREE_CODE (cindex) == INTEGER_CST
5144 : 1491209 : && compare_tree_int (cindex, end - 1) == 0)
5145 : : {
5146 : 1470776 : tree value = (*elts)[end - 1].value;
5147 : 1470776 : if (i < end)
5148 : : {
5149 : 671339 : if (i == end - 1 && TREE_CODE (value) == RAW_DATA_CST)
5150 : : begin = end - 1;
5151 : : else
5152 : 671249 : return i;
5153 : : }
5154 : 799437 : else if (TREE_CODE (value) == RAW_DATA_CST
5155 : 831615 : && wi::to_offset (dindex) < (wi::to_offset (cindex)
5156 : 32178 : + RAW_DATA_LENGTH (value)))
5157 : 16089 : begin = end - 1;
5158 : : else
5159 : 783348 : begin = end;
5160 : : }
5161 : : }
5162 : :
5163 : : /* Otherwise, find a matching index by means of a binary search. */
5164 : 965793 : while (begin != end)
5165 : : {
5166 : 62164 : unsigned HOST_WIDE_INT middle = (begin + end) / 2;
5167 : 62164 : constructor_elt &elt = (*elts)[middle];
5168 : 62164 : tree idx = elt.index;
5169 : :
5170 : 62164 : int cmp = array_index_cmp (dindex, idx);
5171 : 62164 : if (cmp > 0
5172 : 48656 : && TREE_CODE (elt.value) == RAW_DATA_CST
5173 : 145038 : && wi::to_offset (dindex) < (wi::to_offset (idx)
5174 : 82874 : + RAW_DATA_LENGTH (elt.value)))
5175 : 28950 : cmp = 0;
5176 : 62164 : if (cmp < 0)
5177 : : end = middle;
5178 : 53695 : else if (cmp > 0)
5179 : 19706 : begin = middle + 1;
5180 : : else
5181 : : {
5182 : 33989 : 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 : 1052 : 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 : 33867 : return middle;
5266 : : }
5267 : : }
5268 : :
5269 : 903629 : if (insert)
5270 : : {
5271 : 901088 : constructor_elt e = { dindex, NULL_TREE };
5272 : 901088 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), end, e);
5273 : 901088 : 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 : 9905107 : get_or_insert_ctor_field (tree ctor, tree index, int pos_hint = -1)
5287 : : {
5288 : : /* Check the hint first. */
5289 : 853244 : if (pos_hint >= 0 && (unsigned)pos_hint < CONSTRUCTOR_NELTS (ctor)
5290 : 10758351 : && CONSTRUCTOR_ELT (ctor, pos_hint)->index == index)
5291 : : return CONSTRUCTOR_ELT (ctor, pos_hint);
5292 : :
5293 : 9051868 : tree type = TREE_TYPE (ctor);
5294 : 9051868 : 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 : 9051740 : else if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
5300 : : {
5301 : 1239759 : 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 : 1239756 : HOST_WIDE_INT i = find_array_ctor_elt (ctor, index, /*insert*/true);
5318 : 1239756 : gcc_assert (i >= 0);
5319 : 1239756 : constructor_elt *cep = CONSTRUCTOR_ELT (ctor, i);
5320 : 1239756 : gcc_assert (cep->index == NULL_TREE
5321 : : || TREE_CODE (cep->index) != RANGE_EXPR);
5322 : : return cep;
5323 : : }
5324 : : else
5325 : : {
5326 : 7811981 : 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 : 7811981 : tree fields = TYPE_FIELDS (DECL_CONTEXT (index));
5335 : 7811981 : unsigned HOST_WIDE_INT idx = 0;
5336 : 7811981 : constructor_elt *cep = NULL;
5337 : :
5338 : : /* Check if we're changing the active member of a union. */
5339 : 151515 : if (TREE_CODE (type) == UNION_TYPE && CONSTRUCTOR_NELTS (ctor)
5340 : 7853453 : && CONSTRUCTOR_ELT (ctor, 0)->index != index)
5341 : 1576 : 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 : 7810405 : else if (CONSTRUCTOR_NELTS (ctor)
5346 : 9175196 : && tree_int_cst_compare (bit_position (index),
5347 : 8922024 : bit_position (CONSTRUCTOR_ELTS (ctor)
5348 : 4461012 : ->last().index)) > 0)
5349 : : {
5350 : 1901026 : idx = CONSTRUCTOR_NELTS (ctor);
5351 : 1901026 : goto insert;
5352 : : }
5353 : :
5354 : : /* Otherwise, we need to iterate over CTOR to find or insert INDEX
5355 : : appropriately. */
5356 : :
5357 : 6958329 : for (; vec_safe_iterate (CONSTRUCTOR_ELTS (ctor), idx, &cep);
5358 : 1047374 : idx++, fields = DECL_CHAIN (fields))
5359 : : {
5360 : 3607360 : if (index == cep->index)
5361 : 2543342 : 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 : 14082223 : for (; fields != cep->index; fields = DECL_CHAIN (fields))
5367 : 13034849 : if (index == fields)
5368 : 16644 : goto insert;
5369 : : }
5370 : : /* We fell off the end of the CONSTRUCTOR, so insert a new
5371 : : entry at the end. */
5372 : :
5373 : 5268639 : insert:
5374 : 5268639 : {
5375 : 5268639 : constructor_elt ce = { index, NULL_TREE };
5376 : :
5377 : 5268639 : vec_safe_insert (CONSTRUCTOR_ELTS (ctor), idx, ce);
5378 : 5268639 : cep = CONSTRUCTOR_ELT (ctor, idx);
5379 : : }
5380 : 7811981 : found:;
5381 : :
5382 : 7811981 : 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 : 549 : diag_array_subscript (location_t loc, const constexpr_ctx *ctx, tree array, tree index)
5391 : : {
5392 : 549 : 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 : 549 : }
5419 : :
5420 : : /* Return the number of elements for TYPE (which is an ARRAY_TYPE or
5421 : : a VECTOR_TYPE). */
5422 : :
5423 : : static tree
5424 : 3656305 : 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 : 3656305 : tree nelts;
5429 : 3656305 : if (TREE_CODE (type) == ARRAY_TYPE)
5430 : : {
5431 : 3656305 : if (TYPE_DOMAIN (type))
5432 : 3656069 : 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 : 3656305 : nelts = cxx_eval_constant_expression (ctx, nelts, vc_prvalue,
5443 : : non_constant_p, overflow_p,
5444 : : jump_target);
5445 : 3656305 : return nelts;
5446 : : }
5447 : :
5448 : : /* Extract element INDEX consisting of CHARS_PER_ELT chars from
5449 : : STRING_CST STRING. */
5450 : :
5451 : : static tree
5452 : 993045 : extract_string_elt (tree string, unsigned chars_per_elt, unsigned index)
5453 : : {
5454 : 993045 : tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (string)));
5455 : 993045 : tree r;
5456 : :
5457 : 993045 : if (chars_per_elt == 1)
5458 : 735705 : r = build_int_cst (type, TREE_STRING_POINTER (string)[index]);
5459 : : else
5460 : : {
5461 : 257340 : const unsigned char *ptr
5462 : 257340 : = ((const unsigned char *)TREE_STRING_POINTER (string)
5463 : 257340 : + index * chars_per_elt);
5464 : 257340 : r = native_interpret_expr (type, ptr, chars_per_elt);
5465 : : }
5466 : 993045 : 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 : 3657283 : 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 : 3657283 : location_t loc = cp_expr_loc_or_input_loc (t);
5479 : 3657283 : tree ary = TREE_OPERAND (t, 0);
5480 : 3657283 : t = TREE_OPERAND (t, 1);
5481 : 3657283 : tree index = cxx_eval_constant_expression (ctx, t, vc_prvalue,
5482 : : non_constant_p, overflow_p,
5483 : : jump_target);
5484 : 3657283 : if (*jump_target)
5485 : : return NULL_TREE;
5486 : 3657283 : VERIFY_CONSTANT (index);
5487 : :
5488 : 3655799 : if (!tree_fits_shwi_p (index)
5489 : 3655799 : || 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 : 3655691 : tree nelts = get_array_or_vector_nelts (ctx, TREE_TYPE (ary), non_constant_p,
5497 : : overflow_p, jump_target);
5498 : 3655691 : if (*jump_target)
5499 : : return NULL_TREE;
5500 : 3655691 : VERIFY_CONSTANT (nelts);
5501 : 3655628 : if (allow_one_past
5502 : 3655628 : ? !tree_int_cst_le (index, nelts)
5503 : 2545065 : : !tree_int_cst_lt (index, nelts))
5504 : : {
5505 : 441 : diag_array_subscript (loc, ctx, ary, index);
5506 : 441 : *non_constant_p = true;
5507 : 441 : 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 : 2679141 : 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 : 2679141 : tree oldary = TREE_OPERAND (t, 0);
5523 : 2679141 : tree ary = cxx_eval_constant_expression (ctx, oldary,
5524 : : lval,
5525 : : non_constant_p, overflow_p,
5526 : : jump_target);
5527 : 2679141 : if (*non_constant_p)
5528 : : return t;
5529 : 2501331 : if (*jump_target)
5530 : : return NULL_TREE;
5531 : 2501331 : if (!lval
5532 : 1389624 : && TREE_CODE (ary) == VIEW_CONVERT_EXPR
5533 : 13228 : && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary, 0)))
5534 : 2514559 : && (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 : 2501331 : tree oldidx = TREE_OPERAND (t, 1);
5539 : 2501331 : tree index = eval_and_check_array_index (ctx, t, lval,
5540 : : non_constant_p, overflow_p,
5541 : : jump_target);
5542 : 2501331 : if (*non_constant_p)
5543 : : return t;
5544 : 2499293 : if (*jump_target)
5545 : : return NULL_TREE;
5546 : :
5547 : 2499293 : if (lval && ary == oldary && index == oldidx)
5548 : : return t;
5549 : 1496497 : else if (lval == vc_discard)
5550 : : return t;
5551 : 1496497 : else if (lval)
5552 : 107528 : return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL);
5553 : :
5554 : 1388969 : unsigned len = 0, elem_nchars = 1;
5555 : 1388969 : tree elem_type = TREE_TYPE (TREE_TYPE (ary));
5556 : 1388969 : if (TREE_CODE (ary) == CONSTRUCTOR)
5557 : 386339 : len = CONSTRUCTOR_NELTS (ary);
5558 : 1002630 : else if (TREE_CODE (ary) == STRING_CST)
5559 : : {
5560 : 989410 : elem_nchars = (TYPE_PRECISION (elem_type)
5561 : 989410 : / TYPE_PRECISION (char_type_node));
5562 : 989410 : 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 : 1388969 : bool found;
5576 : 1388969 : HOST_WIDE_INT i = 0;
5577 : 1388969 : if (TREE_CODE (ary) == CONSTRUCTOR)
5578 : : {
5579 : 386339 : HOST_WIDE_INT ix = find_array_ctor_elt (ary, index);
5580 : 386339 : found = (ix >= 0);
5581 : 386339 : if (found)
5582 : 383884 : i = ix;
5583 : : }
5584 : : else
5585 : : {
5586 : 1002630 : i = tree_to_shwi (index);
5587 : 1002630 : found = (i < len);
5588 : : }
5589 : :
5590 : 1388969 : if (found)
5591 : : {
5592 : 1386075 : tree r;
5593 : 1386075 : if (TREE_CODE (ary) == CONSTRUCTOR)
5594 : : {
5595 : 383884 : r = (*CONSTRUCTOR_ELTS (ary))[i].value;
5596 : 383884 : 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 : 1002191 : else if (TREE_CODE (ary) == VECTOR_CST)
5606 : 13220 : r = VECTOR_CST_ELT (ary, i);
5607 : : else
5608 : 988971 : r = extract_string_elt (ary, elem_nchars, i);
5609 : :
5610 : 1031153 : if (r)
5611 : : /* Don't VERIFY_CONSTANT here. */
5612 : 1386075 : return r;
5613 : :
5614 : : /* Otherwise the element doesn't have a value yet. */
5615 : : }
5616 : :
5617 : : /* Not found. */
5618 : :
5619 : 2894 : if (is_really_empty_class (elem_type, /*ignore_vptr*/false))
5620 : 49 : return build_constructor (elem_type, NULL);
5621 : :
5622 : 2845 : if (TREE_CODE (ary) == CONSTRUCTOR
5623 : 2845 : && 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 : 2794 : tree val;
5638 : 2794 : constexpr_ctx new_ctx;
5639 : 2794 : if (CP_AGGREGATE_TYPE_P (elem_type))
5640 : : {
5641 : 480 : tree empty_ctor = build_constructor (init_list_type_node, NULL);
5642 : 480 : val = digest_init (elem_type, empty_ctor, tf_warning_or_error);
5643 : : }
5644 : : else
5645 : 2314 : 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 : 2794 : const bool new_ctor = (!SCALAR_TYPE_P (elem_type)
5649 : 3300 : && (!ctx->ctor
5650 : 45 : || !same_type_ignoring_top_level_qualifiers_p
5651 : 45 : (elem_type, TREE_TYPE (ctx->ctor))));
5652 : 497 : if (new_ctor)
5653 : : {
5654 : 497 : 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 : 497 : new_ctx.object = NULL_TREE;
5659 : 497 : new_ctx.ctor = build_constructor (elem_type, NULL);
5660 : 497 : ctx = &new_ctx;
5661 : : }
5662 : 2794 : t = cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
5663 : : overflow_p, jump_target);
5664 : 2794 : if (new_ctor && t != ctx->ctor)
5665 : 476 : 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 : 29295830 : 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 : 29295830 : unsigned HOST_WIDE_INT i;
5679 : 29295830 : tree field;
5680 : 29295830 : tree value;
5681 : 29295830 : tree part = TREE_OPERAND (t, 1);
5682 : 29295830 : tree orig_whole = TREE_OPERAND (t, 0);
5683 : 29295830 : tree whole = cxx_eval_constant_expression (ctx, orig_whole,
5684 : : lval,
5685 : : non_constant_p, overflow_p,
5686 : : jump_target);
5687 : 29295830 : if (*non_constant_p)
5688 : : return t;
5689 : 14998046 : if (*jump_target)
5690 : : return NULL_TREE;
5691 : 14998046 : if (INDIRECT_REF_P (whole)
5692 : 14998046 : && 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 : 14997869 : if (TREE_CODE (whole) == PTRMEM_CST)
5701 : 1289 : whole = cplus_expand_constant (whole);
5702 : 14997869 : if (whole == orig_whole)
5703 : : return t;
5704 : 9342919 : if (lval == vc_discard)
5705 : : return t;
5706 : 9342895 : if (lval)
5707 : 4084630 : 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 : 5258265 : 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 : 5256840 : if ((cxx_dialect < cxx14 || CONSTRUCTOR_MUTABLE_POISON (whole))
5719 : 5259860 : && 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 : 5258133 : bool pmf = TYPE_PTRMEMFUNC_P (TREE_TYPE (whole));
5728 : 7452223 : 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 : 7449833 : if (pmf ? DECL_NAME (field) == DECL_NAME (part)
5733 : : : field == part)
5734 : : {
5735 : 5255743 : if (value)
5736 : : {
5737 : 5255725 : STRIP_ANY_LOCATION_WRAPPER (value);
5738 : 5255725 : return value;
5739 : : }
5740 : : else
5741 : : /* We're in the middle of initializing it. */
5742 : : break;
5743 : : }
5744 : : }
5745 : 2408 : 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 : 2328 : if (is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
5780 : 1229 : return build_constructor (TREE_TYPE (t), NULL);
5781 : :
5782 : 1099 : gcc_assert (DECL_CONTEXT (part) == TYPE_MAIN_VARIANT (TREE_TYPE (whole)));
5783 : :
5784 : 1099 : 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 : 96 : if (!ctx->quiet)
5790 : 21 : error ("accessing uninitialized member %qD", part);
5791 : 96 : *non_constant_p = true;
5792 : 96 : return t;
5793 : : }
5794 : :
5795 : : /* If there's no explicit init for this field, it's value-initialized. */
5796 : :
5797 : 1003 : if (AGGREGATE_TYPE_P (TREE_TYPE (t)))
5798 : : {
5799 : : /* As in cxx_eval_store_expression, insert an empty CONSTRUCTOR
5800 : : and copy the flags. */
5801 : 124 : constructor_elt *e = get_or_insert_ctor_field (whole, part);
5802 : 124 : e->value = value = build_constructor (TREE_TYPE (part), NULL);
5803 : 124 : CONSTRUCTOR_ZERO_PADDING_BITS (value)
5804 : 124 : = CONSTRUCTOR_ZERO_PADDING_BITS (whole);
5805 : 124 : return value;
5806 : : }
5807 : :
5808 : 879 : value = build_value_init (TREE_TYPE (t), tf_warning_or_error);
5809 : 879 : return cxx_eval_constant_expression (ctx, value,
5810 : : lval,
5811 : : non_constant_p, overflow_p,
5812 : 879 : jump_target);
5813 : : }
5814 : :
5815 : : /* Subroutine of cxx_eval_constant_expression.
5816 : : Attempt to reduce a field access of a value of class type that is
5817 : : expressed as a BIT_FIELD_REF. */
5818 : :
5819 : : static tree
5820 : 2482 : cxx_eval_bit_field_ref (const constexpr_ctx *ctx, tree t,
5821 : : value_cat lval,
5822 : : bool *non_constant_p, bool *overflow_p,
5823 : : tree *jump_target)
5824 : : {
5825 : 2482 : tree orig_whole = TREE_OPERAND (t, 0);
5826 : 2482 : tree retval, fldval, utype, mask;
5827 : 2482 : bool fld_seen = false;
5828 : 2482 : HOST_WIDE_INT istart, isize;
5829 : 2482 : tree whole = cxx_eval_constant_expression (ctx, orig_whole,
5830 : : lval,
5831 : : non_constant_p, overflow_p,
5832 : : jump_target);
5833 : 2482 : tree start, field, value;
5834 : 2482 : unsigned HOST_WIDE_INT i;
5835 : :
5836 : 2482 : if (*jump_target)
5837 : : return NULL_TREE;
5838 : 2482 : if (whole == orig_whole)
5839 : : return t;
5840 : : /* Don't VERIFY_CONSTANT here; we only want to check that we got a
5841 : : CONSTRUCTOR. */
5842 : 2211 : if (!*non_constant_p
5843 : 2211 : && TREE_CODE (whole) != VECTOR_CST
5844 : 2209 : && TREE_CODE (whole) != CONSTRUCTOR)
5845 : : {
5846 : 0 : if (!ctx->quiet)
5847 : 0 : error ("%qE is not a constant expression", orig_whole);
5848 : 0 : *non_constant_p = true;
5849 : : }
5850 : 2211 : if (*non_constant_p)
5851 : : return t;
5852 : :
5853 : 2211 : if (TREE_CODE (whole) == VECTOR_CST || !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5854 : : {
5855 : 2 : if (tree r = fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
5856 : : TREE_OPERAND (t, 1), TREE_OPERAND (t, 2)))
5857 : : return r;
5858 : 0 : if (!ctx->quiet)
5859 : 0 : error ("%qE is not a constant expression", orig_whole);
5860 : 0 : *non_constant_p = true;
5861 : 0 : return t;
5862 : : }
5863 : :
5864 : 2209 : start = TREE_OPERAND (t, 2);
5865 : 2209 : istart = tree_to_shwi (start);
5866 : 2209 : isize = tree_to_shwi (TREE_OPERAND (t, 1));
5867 : 2209 : utype = TREE_TYPE (t);
5868 : 2209 : if (!TYPE_UNSIGNED (utype))
5869 : 0 : utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1);
5870 : 2209 : retval = build_int_cst (utype, 0);
5871 : 30926 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
5872 : : {
5873 : 28717 : tree bitpos = bit_position (field);
5874 : 28717 : STRIP_ANY_LOCATION_WRAPPER (value);
5875 : 28717 : if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1))
5876 : : return value;
5877 : 28717 : if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE
5878 : 28717 : && TREE_CODE (value) == INTEGER_CST
5879 : 28717 : && tree_fits_shwi_p (bitpos)
5880 : 57434 : && tree_fits_shwi_p (DECL_SIZE (field)))
5881 : : {
5882 : 28717 : HOST_WIDE_INT bit = tree_to_shwi (bitpos);
5883 : 28717 : HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field));
5884 : 28717 : HOST_WIDE_INT shift;
5885 : 28717 : if (bit >= istart && bit + sz <= istart + isize)
5886 : : {
5887 : 6627 : fldval = fold_convert (utype, value);
5888 : 6627 : mask = build_int_cst_type (utype, -1);
5889 : 6627 : mask = fold_build2 (LSHIFT_EXPR, utype, mask,
5890 : : size_int (TYPE_PRECISION (utype) - sz));
5891 : 6627 : mask = fold_build2 (RSHIFT_EXPR, utype, mask,
5892 : : size_int (TYPE_PRECISION (utype) - sz));
5893 : 6627 : fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask);
5894 : 6627 : shift = bit - istart;
5895 : 6627 : if (BYTES_BIG_ENDIAN)
5896 : : shift = TYPE_PRECISION (utype) - shift - sz;
5897 : 6627 : fldval = fold_build2 (LSHIFT_EXPR, utype, fldval,
5898 : : size_int (shift));
5899 : 6627 : retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval);
5900 : 6627 : fld_seen = true;
5901 : : }
5902 : : }
5903 : : }
5904 : 2209 : if (fld_seen)
5905 : 2209 : return fold_convert (TREE_TYPE (t), retval);
5906 : 0 : gcc_unreachable ();
5907 : : return error_mark_node;
5908 : : }
5909 : :
5910 : : /* Helper for cxx_eval_bit_cast.
5911 : : Check [bit.cast]/3 rules, bit_cast is constexpr only if the To and From
5912 : : types and types of all subobjects have is_union_v<T>, is_pointer_v<T>,
5913 : : is_member_pointer_v<T>, is_volatile_v<T> false and has no non-static
5914 : : data members of reference type. */
5915 : :
5916 : : static bool
5917 : 4820 : check_bit_cast_type (const constexpr_ctx *ctx, location_t loc, tree type,
5918 : : tree orig_type)
5919 : : {
5920 : 5313 : if (TREE_CODE (type) == UNION_TYPE)
5921 : : {
5922 : 63 : if (!ctx->quiet)
5923 : : {
5924 : 21 : if (type == orig_type)
5925 : 6 : error_at (loc, "%qs is not a constant expression because %qT is "
5926 : : "a union type", "__builtin_bit_cast", type);
5927 : : else
5928 : 15 : error_at (loc, "%qs is not a constant expression because %qT "
5929 : : "contains a union type", "__builtin_bit_cast",
5930 : : orig_type);
5931 : : }
5932 : 63 : return true;
5933 : : }
5934 : : if (TREE_CODE (type) == POINTER_TYPE)
5935 : : {
5936 : 60 : if (!ctx->quiet)
5937 : : {
5938 : 18 : if (type == orig_type)
5939 : 6 : error_at (loc, "%qs is not a constant expression because %qT is "
5940 : : "a pointer type", "__builtin_bit_cast", type);
5941 : : else
5942 : 12 : error_at (loc, "%qs is not a constant expression because %qT "
5943 : : "contains a pointer type", "__builtin_bit_cast",
5944 : : orig_type);
5945 : : }
5946 : 60 : return true;
5947 : : }
5948 : : if (TREE_CODE (type) == REFERENCE_TYPE)
5949 : : {
5950 : 0 : if (!ctx->quiet)
5951 : : {
5952 : 0 : if (type == orig_type)
5953 : 0 : error_at (loc, "%qs is not a constant expression because %qT is "
5954 : : "a reference type", "__builtin_bit_cast", type);
5955 : : else
5956 : 0 : error_at (loc, "%qs is not a constant expression because %qT "
5957 : : "contains a reference type", "__builtin_bit_cast",
5958 : : orig_type);
5959 : : }
5960 : 0 : return true;
5961 : : }
5962 : 1361 : if (TYPE_PTRMEM_P (type))
5963 : : {
5964 : 36 : if (!ctx->quiet)
5965 : : {
5966 : 12 : if (type == orig_type)
5967 : 12 : error_at (loc, "%qs is not a constant expression because %qT is "
5968 : : "a pointer to member type", "__builtin_bit_cast",
5969 : : type);
5970 : : else
5971 : 0 : error_at (loc, "%qs is not a constant expression because %qT "
5972 : : "contains a pointer to member type",
5973 : : "__builtin_bit_cast", orig_type);
5974 : : }
5975 : 36 : return true;
5976 : : }
5977 : 5154 : if (TYPE_VOLATILE (type))
5978 : : {
5979 : 0 : if (!ctx->quiet)
5980 : : {
5981 : 0 : if (type == orig_type)
5982 : 0 : error_at (loc, "%qs is not a constant expression because %qT is "
5983 : : "volatile", "__builtin_bit_cast", type);
5984 : : else
5985 : 0 : error_at (loc, "%qs is not a constant expression because %qT "
5986 : : "contains a volatile subobject",
5987 : : "__builtin_bit_cast", orig_type);
5988 : : }
5989 : 0 : return true;
5990 : : }
5991 : 5154 : if (TREE_CODE (type) == RECORD_TYPE)
5992 : 27024 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5993 : 25771 : if (TREE_CODE (field) == FIELD_DECL
5994 : 25771 : && check_bit_cast_type (ctx, loc, TREE_TYPE (field), orig_type))
5995 : : return true;
5996 : 5064 : if (TREE_CODE (type) == ARRAY_TYPE)
5997 : 493 : return check_bit_cast_type (ctx, loc, TREE_TYPE (type), orig_type);
5998 : : return false;
5999 : : }
6000 : :
6001 : : /* Helper function for cxx_eval_bit_cast. For unsigned char or
6002 : : std::byte members of CONSTRUCTOR (recursively) if they contain
6003 : : some indeterminate bits (as set in MASK), remove the ctor elts,
6004 : : mark the CONSTRUCTOR as CONSTRUCTOR_NO_CLEARING and clear the
6005 : : bits in MASK. */
6006 : :
6007 : : static void
6008 : 603 : clear_uchar_or_std_byte_in_mask (location_t loc, tree t, unsigned char *mask)
6009 : : {
6010 : 603 : if (TREE_CODE (t) != CONSTRUCTOR)
6011 : : return;
6012 : :
6013 : : unsigned i, j = 0;
6014 : : tree index, value;
6015 : 2090 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), i, index, value)
6016 : : {
6017 : 1487 : tree type = TREE_TYPE (value);
6018 : 1487 : if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
6019 : 2535 : && DECL_BIT_FIELD_TYPE (index) != NULL_TREE)
6020 : : {
6021 : 270 : if (is_byte_access_type_not_plain_char (DECL_BIT_FIELD_TYPE (index)))
6022 : : {
6023 : 135 : HOST_WIDE_INT fldsz = TYPE_PRECISION (TREE_TYPE (index));
6024 : 135 : gcc_assert (fldsz != 0);
6025 : 135 : HOST_WIDE_INT pos = int_byte_position (index);
6026 : 135 : HOST_WIDE_INT bpos
6027 : 135 : = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (index));
6028 : 135 : bpos %= BITS_PER_UNIT;
6029 : 135 : HOST_WIDE_INT end
6030 : 135 : = ROUND_UP (bpos + fldsz, BITS_PER_UNIT) / BITS_PER_UNIT;
6031 : 135 : gcc_assert (end == 1 || end == 2);
6032 : 135 : unsigned char *p = mask + pos;
6033 : 135 : unsigned char mask_save[2];
6034 : 135 : mask_save[0] = mask[pos];
6035 : 135 : mask_save[1] = end == 2 ? mask[pos + 1] : 0;
6036 : 135 : if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
6037 : : sorry_at (loc, "PDP11 bit-field handling unsupported"
6038 : : " in %qs", "__builtin_bit_cast");
6039 : 135 : else if (BYTES_BIG_ENDIAN)
6040 : : {
6041 : : /* Big endian. */
6042 : : if (bpos + fldsz <= BITS_PER_UNIT)
6043 : : *p &= ~(((1 << fldsz) - 1)
6044 : : << (BITS_PER_UNIT - bpos - fldsz));
6045 : : else
6046 : : {
6047 : : gcc_assert (bpos);
6048 : : *p &= ~(((1U << BITS_PER_UNIT) - 1) >> bpos);
6049 : : p++;
6050 : : fldsz -= BITS_PER_UNIT - bpos;
6051 : : gcc_assert (fldsz && fldsz < BITS_PER_UNIT);
6052 : : *p &= ((1U << BITS_PER_UNIT) - 1) >> fldsz;
6053 : : }
6054 : : }
6055 : : else
6056 : : {
6057 : : /* Little endian. */
6058 : 135 : if (bpos + fldsz <= BITS_PER_UNIT)
6059 : 135 : *p &= ~(((1 << fldsz) - 1) << bpos);
6060 : : else
6061 : : {
6062 : 0 : gcc_assert (bpos);
6063 : 0 : *p &= ~(((1 << BITS_PER_UNIT) - 1) << bpos);
6064 : 0 : p++;
6065 : 0 : fldsz -= BITS_PER_UNIT - bpos;
6066 : 0 : gcc_assert (fldsz && fldsz < BITS_PER_UNIT);
6067 : 0 : *p &= ~((1 << fldsz) - 1);
6068 : : }
6069 : : }
6070 : 135 : if (mask_save[0] != mask[pos]
6071 : 99 : || (end == 2 && mask_save[1] != mask[pos + 1]))
6072 : : {
6073 : 36 : CONSTRUCTOR_NO_CLEARING (t) = 1;
6074 : 36 : continue;
6075 : : }
6076 : : }
6077 : : }
6078 : 1217 : else if (is_byte_access_type_not_plain_char (type))
6079 : : {
6080 : 228 : HOST_WIDE_INT pos;
6081 : 228 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6082 : 132 : pos = tree_to_shwi (index);
6083 : : else
6084 : 96 : pos = int_byte_position (index);
6085 : 228 : if (mask[pos])
6086 : : {
6087 : 48 : CONSTRUCTOR_NO_CLEARING (t) = 1;
6088 : 48 : mask[pos] = 0;
6089 : 48 : continue;
6090 : : }
6091 : : }
6092 : 1403 : if (TREE_CODE (value) == CONSTRUCTOR)
6093 : : {
6094 : 264 : HOST_WIDE_INT pos;
6095 : 264 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6096 : 144 : pos = tree_to_shwi (index)
6097 : 72 : * tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))));
6098 : : else
6099 : 192 : pos = int_byte_position (index);
6100 : 264 : clear_uchar_or_std_byte_in_mask (loc, value, mask + pos);
6101 : : }
6102 : 1403 : if (i != j)
6103 : : {
6104 : 180 : CONSTRUCTOR_ELT (t, j)->index = index;
6105 : 180 : CONSTRUCTOR_ELT (t, j)->value = value;
6106 : : }
6107 : 1403 : ++j;
6108 : : }
6109 : 1206 : if (CONSTRUCTOR_NELTS (t) != j)
6110 : 84 : vec_safe_truncate (CONSTRUCTOR_ELTS (t), j);
6111 : : }
6112 : :
6113 : : /* Subroutine of cxx_eval_constant_expression.
6114 : : Attempt to evaluate a BIT_CAST_EXPR. */
6115 : :
6116 : : static tree
6117 : 1025 : cxx_eval_bit_cast (const constexpr_ctx *ctx, tree t, bool *non_constant_p,
6118 : : bool *overflow_p, tree *jump_target)
6119 : : {
6120 : 1025 : if (check_bit_cast_type (ctx, EXPR_LOCATION (t), TREE_TYPE (t),
6121 : 1025 : TREE_TYPE (t))
6122 : 3325 : || check_bit_cast_type (ctx, cp_expr_loc_or_loc (TREE_OPERAND (t, 0),
6123 : 944 : EXPR_LOCATION (t)),
6124 : 944 : TREE_TYPE (TREE_OPERAND (t, 0)),
6125 : 944 : TREE_TYPE (TREE_OPERAND (t, 0))))
6126 : : {
6127 : 159 : *non_constant_p = true;
6128 : 159 : return t;
6129 : : }
6130 : :
6131 : 866 : tree op = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), vc_prvalue,
6132 : : non_constant_p, overflow_p,
6133 : : jump_target);
6134 : 866 : if (*non_constant_p)
6135 : : return t;
6136 : 770 : if (*jump_target)
6137 : : return NULL_TREE;
6138 : :
6139 : 770 : location_t loc = EXPR_LOCATION (t);
6140 : 770 : if (BITS_PER_UNIT != 8 || CHAR_BIT != 8)
6141 : : {
6142 : : if (!ctx->quiet)
6143 : : sorry_at (loc, "%qs cannot be constant evaluated on the target",
6144 : : "__builtin_bit_cast");
6145 : : *non_constant_p = true;
6146 : : return t;
6147 : : }
6148 : :
6149 : 770 : if (!tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (t))))
6150 : : {
6151 : 0 : if (!ctx->quiet)
6152 : 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
6153 : : "type is too large", "__builtin_bit_cast");
6154 : 0 : *non_constant_p = true;
6155 : 0 : return t;
6156 : : }
6157 : :
6158 : 770 : HOST_WIDE_INT len = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (t)));
6159 : 770 : if (len < 0 || (int) len != len)
6160 : : {
6161 : 0 : if (!ctx->quiet)
6162 : 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
6163 : : "type is too large", "__builtin_bit_cast");
6164 : 0 : *non_constant_p = true;
6165 : 0 : return t;
6166 : : }
6167 : :
6168 : 770 : unsigned char buf[64];
6169 : 770 : unsigned char *ptr, *mask;
6170 : 770 : size_t alen = (size_t) len * 2;
6171 : 770 : if (alen <= sizeof (buf))
6172 : : ptr = buf;
6173 : : else
6174 : 3 : ptr = XNEWVEC (unsigned char, alen);
6175 : 770 : mask = ptr + (size_t) len;
6176 : : /* At the beginning consider everything indeterminate. */
6177 : 770 : memset (mask, ~0, (size_t) len);
6178 : :
6179 : 770 : if (native_encode_initializer (op, ptr, len, 0, mask) != len)
6180 : : {
6181 : 0 : if (!ctx->quiet)
6182 : 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
6183 : : "argument cannot be encoded", "__builtin_bit_cast");
6184 : 0 : *non_constant_p = true;
6185 : 0 : if (ptr != buf)
6186 : 0 : XDELETE (ptr);
6187 : 0 : return t;
6188 : : }
6189 : :
6190 : 770 : tree r = NULL_TREE;
6191 : 770 : if (can_native_interpret_type_p (TREE_TYPE (t)))
6192 : : {
6193 : 431 : r = native_interpret_expr (TREE_TYPE (t), ptr, len);
6194 : 431 : if (is_byte_access_type_not_plain_char (TREE_TYPE (t)))
6195 : : {
6196 : 46 : gcc_assert (len == 1);
6197 : 46 : if (mask[0])
6198 : : {
6199 : 24 : memset (mask, 0, len);
6200 : 24 : r = build_constructor (TREE_TYPE (r), NULL);
6201 : 24 : CONSTRUCTOR_NO_CLEARING (r) = 1;
6202 : : }
6203 : : }
6204 : : }
6205 : 339 : else if (TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE)
6206 : : {
6207 : 339 : r = native_interpret_aggregate (TREE_TYPE (t), ptr, 0, len);
6208 : 339 : if (r != NULL_TREE)
6209 : : {
6210 : 339 : clear_type_padding_in_mask (TREE_TYPE (t), mask);
6211 : 339 : clear_uchar_or_std_byte_in_mask (loc, r, mask);
6212 : 339 : if (CHECKING_P)
6213 : : {
6214 : 339 : tree e = cxx_eval_bare_aggregate (ctx, r, vc_prvalue,
6215 : : non_constant_p, overflow_p,
6216 : : jump_target);
6217 : 339 : gcc_checking_assert (e == r && !*jump_target);
6218 : : r = e;
6219 : : }
6220 : : }
6221 : : }
6222 : :
6223 : 770 : if (r != NULL_TREE)
6224 : : {
6225 : 6169 : for (int i = 0; i < len; i++)
6226 : 5489 : if (mask[i])
6227 : : {
6228 : 90 : if (!ctx->quiet)
6229 : 30 : error_at (loc, "%qs accessing uninitialized byte at offset %d",
6230 : : "__builtin_bit_cast", i);
6231 : 90 : *non_constant_p = true;
6232 : 90 : r = t;
6233 : 90 : break;
6234 : : }
6235 : 770 : if (ptr != buf)
6236 : 3 : XDELETE (ptr);
6237 : 770 : return r;
6238 : : }
6239 : :
6240 : 0 : if (!ctx->quiet)
6241 : 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
6242 : : "argument cannot be interpreted", "__builtin_bit_cast");
6243 : 0 : *non_constant_p = true;
6244 : 0 : if (ptr != buf)
6245 : 0 : XDELETE (ptr);
6246 : : return t;
6247 : : }
6248 : :
6249 : : /* Subroutine of cxx_eval_constant_expression.
6250 : : Evaluate a short-circuited logical expression T in the context
6251 : : of a given constexpr CALL. BAILOUT_VALUE is the value for
6252 : : early return. CONTINUE_VALUE is used here purely for
6253 : : sanity check purposes. */
6254 : :
6255 : : static tree
6256 : 6735840 : cxx_eval_logical_expression (const constexpr_ctx *ctx, tree t,
6257 : : tree bailout_value, tree continue_value,
6258 : : bool *non_constant_p, bool *overflow_p,
6259 : : tree *jump_target)
6260 : : {
6261 : 6735840 : tree r;
6262 : 6735840 : tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
6263 : : vc_prvalue, non_constant_p,
6264 : : overflow_p, jump_target);
6265 : 6735840 : if (*jump_target)
6266 : : return NULL_TREE;
6267 : 6735831 : VERIFY_CONSTANT (lhs);
6268 : 4065733 : if (tree_int_cst_equal (lhs, bailout_value))
6269 : : return lhs;
6270 : 3437891 : gcc_assert (tree_int_cst_equal (lhs, continue_value));
6271 : 3437891 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
6272 : : vc_prvalue, non_constant_p,
6273 : : overflow_p, jump_target);
6274 : 3437891 : if (*jump_target)
6275 : : return NULL_TREE;
6276 : 3437891 : VERIFY_CONSTANT (r);
6277 : : return r;
6278 : : }
6279 : :
6280 : : /* REF is a COMPONENT_REF designating a particular field. V is a vector of
6281 : : CONSTRUCTOR elements to initialize (part of) an object containing that
6282 : : field. Return a pointer to the constructor_elt corresponding to the
6283 : : initialization of the field. */
6284 : :
6285 : : static constructor_elt *
6286 : 0 : base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref)
6287 : : {
6288 : 0 : tree aggr = TREE_OPERAND (ref, 0);
6289 : 0 : tree field = TREE_OPERAND (ref, 1);
6290 : 0 : HOST_WIDE_INT i;
6291 : 0 : constructor_elt *ce;
6292 : :
6293 : 0 : gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
6294 : :
6295 : 0 : if (TREE_CODE (aggr) == COMPONENT_REF)
6296 : : {
6297 : 0 : constructor_elt *base_ce
6298 : 0 : = base_field_constructor_elt (v, aggr);
6299 : 0 : v = CONSTRUCTOR_ELTS (base_ce->value);
6300 : : }
6301 : :
6302 : 0 : for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
6303 : 0 : if (ce->index == field)
6304 : 0 : return ce;
6305 : :
6306 : 0 : gcc_unreachable ();
6307 : : return NULL;
6308 : : }
6309 : :
6310 : : /* Some of the expressions fed to the constexpr mechanism are calls to
6311 : : constructors, which have type void. In that case, return the type being
6312 : : initialized by the constructor. */
6313 : :
6314 : : static tree
6315 : 373676916 : initialized_type (tree t)
6316 : : {
6317 : 374368152 : if (TYPE_P (t))
6318 : : return t;
6319 : 374367817 : tree type = TREE_TYPE (t);
6320 : 374367817 : if (TREE_CODE (t) == CALL_EXPR)
6321 : : {
6322 : : /* A constructor call has void type, so we need to look deeper. */
6323 : 45405811 : tree fn = get_function_named_in_call (t);
6324 : 45405798 : if (fn && TREE_CODE (fn) == FUNCTION_DECL
6325 : 90765424 : && DECL_CXX_CONSTRUCTOR_P (fn))
6326 : 2303659 : type = DECL_CONTEXT (fn);
6327 : : }
6328 : 328962006 : else if (TREE_CODE (t) == COMPOUND_EXPR)
6329 : 691236 : return initialized_type (TREE_OPERAND (t, 1));
6330 : 328270770 : else if (TREE_CODE (t) == AGGR_INIT_EXPR)
6331 : 497733 : type = TREE_TYPE (AGGR_INIT_EXPR_SLOT (t));
6332 : 373676581 : return cv_unqualified (type);
6333 : : }
6334 : :
6335 : : /* We're about to initialize element INDEX of an array or class from VALUE.
6336 : : Set up NEW_CTX appropriately by adjusting .object to refer to the
6337 : : subobject and creating a new CONSTRUCTOR if the element is itself
6338 : : a class or array. */
6339 : :
6340 : : static void
6341 : 1644091 : init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx,
6342 : : tree index, tree &value)
6343 : : {
6344 : 1644091 : new_ctx = *ctx;
6345 : :
6346 : 1644091 : if (index && TREE_CODE (index) != INTEGER_CST
6347 : 1520154 : && TREE_CODE (index) != FIELD_DECL
6348 : 3 : && TREE_CODE (index) != RANGE_EXPR)
6349 : : /* This won't have an element in the new CONSTRUCTOR. */
6350 : : return;
6351 : :
6352 : 1644091 : tree type = initialized_type (value);
6353 : 1644091 : if (!AGGREGATE_TYPE_P (type) && !VECTOR_TYPE_P (type))
6354 : : /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
6355 : : return;
6356 : 748148 : if (VECTOR_TYPE_P (type)
6357 : 1103 : && VECTOR_TYPE_P (TREE_TYPE (ctx->ctor))
6358 : 748184 : && index == NULL_TREE)
6359 : : /* A vector inside of a vector CONSTRUCTOR, e.g. when a larger
6360 : : vector is constructed from smaller vectors, doesn't get its own
6361 : : CONSTRUCTOR either. */
6362 : : return;
6363 : :
6364 : : /* The sub-aggregate initializer might contain a placeholder;
6365 : : update object to refer to the subobject and ctor to refer to
6366 : : the (newly created) sub-initializer. */
6367 : 748112 : if (ctx->object)
6368 : : {
6369 : 581016 : if (index == NULL_TREE || TREE_CODE (index) == RANGE_EXPR)
6370 : : /* There's no well-defined subobject for this index. */
6371 : 3 : new_ctx.object = NULL_TREE;
6372 : : else
6373 : 581013 : new_ctx.object = build_ctor_subob_ref (index, type, ctx->object);
6374 : : }
6375 : :
6376 : 748112 : if (is_empty_class (type))
6377 : : /* Leave ctor null for an empty subobject, they aren't represented in the
6378 : : result of evaluation. */
6379 : 706541 : new_ctx.ctor = NULL_TREE;
6380 : : else
6381 : : {
6382 : 41571 : tree elt = build_constructor (type, NULL);
6383 : 41571 : CONSTRUCTOR_NO_CLEARING (elt) = true;
6384 : 41571 : new_ctx.ctor = elt;
6385 : : }
6386 : :
6387 : 748112 : if (TREE_CODE (value) == TARGET_EXPR)
6388 : : /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
6389 : 31004 : value = TARGET_EXPR_INITIAL (value);
6390 : : }
6391 : :
6392 : : /* We're about to process an initializer for a class or array TYPE. Make
6393 : : sure that CTX is set up appropriately. */
6394 : :
6395 : : static void
6396 : 1259410 : verify_ctor_sanity (const constexpr_ctx *ctx, tree type)
6397 : : {
6398 : : /* We don't bother building a ctor for an empty base subobject. */
6399 : 1259410 : if (is_empty_class (type))
6400 : : return;
6401 : :
6402 : : /* We're in the middle of an initializer that might involve placeholders;
6403 : : our caller should have created a CONSTRUCTOR for us to put the
6404 : : initializer into. We will either return that constructor or T. */
6405 : 555036 : gcc_assert (ctx->ctor);
6406 : 555036 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
6407 : : (type, TREE_TYPE (ctx->ctor)));
6408 : : /* We used to check that ctx->ctor was empty, but that isn't the case when
6409 : : the object is zero-initialized before calling the constructor. */
6410 : 555036 : if (ctx->object)
6411 : : {
6412 : 419447 : tree otype = TREE_TYPE (ctx->object);
6413 : 419447 : gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, otype)
6414 : : /* Handle flexible array members. */
6415 : : || (TREE_CODE (otype) == ARRAY_TYPE
6416 : : && TYPE_DOMAIN (otype) == NULL_TREE
6417 : : && TREE_CODE (type) == ARRAY_TYPE
6418 : : && (same_type_ignoring_top_level_qualifiers_p
6419 : : (TREE_TYPE (type), TREE_TYPE (otype)))));
6420 : : }
6421 : 555036 : gcc_assert (!ctx->object || !DECL_P (ctx->object)
6422 : : || ctx->global->get_value (ctx->object) == ctx->ctor);
6423 : : }
6424 : :
6425 : : /* Subroutine of cxx_eval_constant_expression.
6426 : : The expression tree T denotes a C-style array or a C-style
6427 : : aggregate. Reduce it to a constant expression. */
6428 : :
6429 : : static tree
6430 : 1258796 : cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t,
6431 : : value_cat lval,
6432 : : bool *non_constant_p, bool *overflow_p,
6433 : : tree *jump_target)
6434 : : {
6435 : 1258796 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
6436 : 1258796 : bool changed = false;
6437 : 1258796 : gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t));
6438 : 1258796 : tree type = TREE_TYPE (t);
6439 : :
6440 : 1258796 : constexpr_ctx new_ctx;
6441 : 1258796 : if (TYPE_PTRMEMFUNC_P (type) || VECTOR_TYPE_P (type))
6442 : : {
6443 : : /* We don't really need the ctx->ctor business for a PMF or
6444 : : vector, but it's simpler to use the same code. */
6445 : 24865 : new_ctx = *ctx;
6446 : 24865 : new_ctx.ctor = build_constructor (type, NULL);
6447 : 24865 : new_ctx.object = NULL_TREE;
6448 : 24865 : ctx = &new_ctx;
6449 : 1258796 : };
6450 : 1258796 : verify_ctor_sanity (ctx, type);
6451 : 1258796 : vec<constructor_elt, va_gc> **p = nullptr;
6452 : 1258796 : if (ctx->ctor)
6453 : : {
6454 : 1258788 : p = &CONSTRUCTOR_ELTS (ctx->ctor);
6455 : 2514586 : vec_alloc (*p, vec_safe_length (v));
6456 : 1258788 : if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (t))
6457 : 16057 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor) = 1;
6458 : : }
6459 : :
6460 : 1258796 : unsigned i;
6461 : 1258796 : tree index, value;
6462 : 1258796 : bool constant_p = true;
6463 : 1258796 : bool side_effects_p = false;
6464 : 2594997 : FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value)
6465 : : {
6466 : 1638036 : tree orig_value = value;
6467 : 1638036 : init_subob_ctx (ctx, new_ctx, index, value);
6468 : : /* Like in cxx_eval_store_expression, omit entries for empty fields. */
6469 : 1638036 : bool no_slot = new_ctx.ctor == NULL_TREE;
6470 : 1638036 : int pos_hint = -1;
6471 : 1638036 : if (new_ctx.ctor != ctx->ctor && !no_slot)
6472 : : {
6473 : : /* If we built a new CONSTRUCTOR, attach it now so that other
6474 : : initializers can refer to it. */
6475 : 35696 : constructor_elt *cep = get_or_insert_ctor_field (ctx->ctor, index);
6476 : 35696 : cep->value = new_ctx.ctor;
6477 : 35696 : pos_hint = cep - (*p)->begin();
6478 : 35696 : }
6479 : 1602340 : else if (TREE_CODE (type) == UNION_TYPE)
6480 : : /* Otherwise if we're constructing a non-aggregate union member, set
6481 : : the active union member now so that we can later detect and diagnose
6482 : : if its initializer attempts to activate another member. */
6483 : 969 : get_or_insert_ctor_field (ctx->ctor, index);
6484 : 1638036 : tree elt = cxx_eval_constant_expression (&new_ctx, value,
6485 : : lval,
6486 : : non_constant_p, overflow_p,
6487 : : jump_target);
6488 : 1638036 : if (*jump_target)
6489 : : return NULL_TREE;
6490 : : /* Don't VERIFY_CONSTANT here. */
6491 : 1638036 : if (ctx->quiet && *non_constant_p)
6492 : : break;
6493 : 1336201 : if (elt != orig_value)
6494 : 139045 : changed = true;
6495 : :
6496 : 1336201 : if (!TREE_CONSTANT (elt))
6497 : 448841 : constant_p = false;
6498 : 1336201 : if (TREE_SIDE_EFFECTS (elt))
6499 : 114 : side_effects_p = true;
6500 : 1336201 : if (index && TREE_CODE (index) == COMPONENT_REF)
6501 : : {
6502 : : /* This is an initialization of a vfield inside a base
6503 : : subaggregate that we already initialized; push this
6504 : : initialization into the previous initialization. */
6505 : 0 : constructor_elt *inner = base_field_constructor_elt (*p, index);
6506 : 0 : inner->value = elt;
6507 : 0 : changed = true;
6508 : 0 : }
6509 : 1336201 : else if (no_slot)
6510 : : /* This is an initializer for an empty field; now that we've
6511 : : checked that it's constant, we can ignore it. */
6512 : : changed = true;
6513 : 629955 : else if (index
6514 : 629827 : && (TREE_CODE (index) == NOP_EXPR
6515 : 629827 : || TREE_CODE (index) == POINTER_PLUS_EXPR))
6516 : : {
6517 : : /* Old representation of empty bases. FIXME remove. */
6518 : 0 : gcc_checking_assert (false);
6519 : : gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index))));
6520 : : changed = true;
6521 : : }
6522 : : else
6523 : : {
6524 : 629955 : if (TREE_CODE (type) == UNION_TYPE
6525 : 629955 : && (*p)->last().index != index)
6526 : : /* The initializer erroneously changed the active union member that
6527 : : we're initializing. */
6528 : 8 : gcc_assert (*non_constant_p);
6529 : : else
6530 : : {
6531 : : /* The initializer might have mutated the underlying CONSTRUCTOR,
6532 : : so recompute the location of the target constructer_elt. */
6533 : 629947 : constructor_elt *cep
6534 : 629947 : = get_or_insert_ctor_field (ctx->ctor, index, pos_hint);
6535 : 629947 : cep->value = elt;
6536 : : }
6537 : :
6538 : : /* Adding or replacing an element might change the ctor's flags. */
6539 : 629955 : TREE_CONSTANT (ctx->ctor) = constant_p;
6540 : 629955 : TREE_SIDE_EFFECTS (ctx->ctor) = side_effects_p;
6541 : : }
6542 : : }
6543 : 1258796 : if (*non_constant_p)
6544 : : return t;
6545 : 956925 : if (!changed)
6546 : : {
6547 : 160225 : if (VECTOR_TYPE_P (type))
6548 : 13937 : t = fold (t);
6549 : 160225 : return t;
6550 : : }
6551 : 796700 : t = ctx->ctor;
6552 : 796700 : if (!t)
6553 : 8 : t = build_constructor (type, NULL);
6554 : : /* We're done building this CONSTRUCTOR, so now we can interpret an
6555 : : element without an explicit initializer as value-initialized. */
6556 : 796700 : CONSTRUCTOR_NO_CLEARING (t) = false;
6557 : 796700 : TREE_CONSTANT (t) = constant_p;
6558 : 796700 : TREE_SIDE_EFFECTS (t) = side_effects_p;
6559 : 796700 : if (VECTOR_TYPE_P (type))
6560 : 1070 : t = fold (t);
6561 : : return t;
6562 : : }
6563 : :
6564 : : /* Subroutine of cxx_eval_constant_expression.
6565 : : The expression tree T is a VEC_INIT_EXPR which denotes the desired
6566 : : initialization of a non-static data member of array type. Reduce it to a
6567 : : CONSTRUCTOR.
6568 : :
6569 : : Note that apart from value-initialization (when VALUE_INIT is true),
6570 : : this is only intended to support value-initialization and the
6571 : : initializations done by defaulted constructors for classes with
6572 : : non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
6573 : : will either be NULL_TREE for the default constructor, or a COMPONENT_REF
6574 : : for the copy/move constructor. */
6575 : :
6576 : : static tree
6577 : 614 : cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init,
6578 : : bool value_init, value_cat lval,
6579 : : bool *non_constant_p, bool *overflow_p,
6580 : : tree *jump_target)
6581 : : {
6582 : 614 : tree elttype = TREE_TYPE (atype);
6583 : 614 : verify_ctor_sanity (ctx, atype);
6584 : 614 : vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
6585 : 614 : bool pre_init = false;
6586 : 614 : unsigned HOST_WIDE_INT i;
6587 : 614 : tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
6588 : :
6589 : 614 : if (init && TREE_CODE (init) == CONSTRUCTOR)
6590 : 0 : return cxx_eval_bare_aggregate (ctx, init, lval,
6591 : 0 : non_constant_p, overflow_p, jump_target);
6592 : :
6593 : : /* For the default constructor, build up a call to the default
6594 : : constructor of the element type. We only need to handle class types
6595 : : here, as for a constructor to be constexpr, all members must be
6596 : : initialized, which for a defaulted default constructor means they must
6597 : : be of a class type with a constexpr default constructor. */
6598 : 614 : if (TREE_CODE (elttype) == ARRAY_TYPE)
6599 : : /* We only do this at the lowest level. */;
6600 : 570 : else if (value_init)
6601 : : {
6602 : 173 : init = build_value_init (elttype, complain);
6603 : 173 : pre_init = true;
6604 : : }
6605 : 397 : else if (!init)
6606 : : {
6607 : 312 : releasing_vec argvec;
6608 : 312 : init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
6609 : : &argvec, elttype, LOOKUP_NORMAL,
6610 : : complain);
6611 : 312 : init = build_aggr_init_expr (elttype, init);
6612 : 312 : pre_init = true;
6613 : 312 : }
6614 : :
6615 : 614 : bool zeroed_out = false;
6616 : 614 : if (!CONSTRUCTOR_NO_CLEARING (ctx->ctor))
6617 : : {
6618 : : /* We're initializing an array object that had been zero-initialized
6619 : : earlier. Truncate ctx->ctor, and propagate its zeroed state by
6620 : : clearing CONSTRUCTOR_NO_CLEARING on each of the aggregate element
6621 : : initializers we append to it. */
6622 : 156 : gcc_checking_assert (initializer_zerop (ctx->ctor));
6623 : 156 : zeroed_out = true;
6624 : 156 : vec_safe_truncate (*p, 0);
6625 : : }
6626 : :
6627 : 614 : tree nelts = get_array_or_vector_nelts (ctx, atype, non_constant_p,
6628 : : overflow_p, jump_target);
6629 : 614 : if (*jump_target)
6630 : : return NULL_TREE;
6631 : 614 : unsigned HOST_WIDE_INT max = tree_to_uhwi (nelts);
6632 : 6261 : for (i = 0; i < max; ++i)
6633 : : {
6634 : 6055 : tree idx = build_int_cst (size_type_node, i);
6635 : 6055 : tree eltinit;
6636 : 6055 : bool reuse = false;
6637 : 6055 : constexpr_ctx new_ctx;
6638 : 6390 : init_subob_ctx (ctx, new_ctx, idx, pre_init ? init : elttype);
6639 : 6055 : bool no_slot = new_ctx.ctor == NULL_TREE;
6640 : 6055 : if (new_ctx.ctor != ctx->ctor && !no_slot)
6641 : : {
6642 : 5875 : if (zeroed_out)
6643 : 5445 : CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = false;
6644 : 5875 : CONSTRUCTOR_APPEND_ELT (*p, idx, new_ctx.ctor);
6645 : : }
6646 : 6055 : if (TREE_CODE (elttype) == ARRAY_TYPE)
6647 : : {
6648 : : /* A multidimensional array; recurse. */
6649 : 176 : if (value_init || init == NULL_TREE)
6650 : : {
6651 : 168 : eltinit = NULL_TREE;
6652 : 168 : reuse = i == 0;
6653 : : }
6654 : : else
6655 : 8 : eltinit = cp_build_array_ref (input_location, init, idx, complain);
6656 : 176 : eltinit = cxx_eval_vec_init_1 (&new_ctx, elttype, eltinit,
6657 : : value_init, lval, non_constant_p,
6658 : : overflow_p, jump_target);
6659 : : }
6660 : 5879 : else if (pre_init)
6661 : : {
6662 : : /* Initializing an element using value or default initialization
6663 : : we just pre-built above. */
6664 : 5720 : if (init == void_node)
6665 : : /* Trivial default-init, don't do anything to the CONSTRUCTOR. */
6666 : 3 : return ctx->ctor;
6667 : 5717 : eltinit = init;
6668 : 5717 : if (CLASS_TYPE_P (elttype) && new_ctx.object)
6669 : : /* Clarify what object is being initialized (118285). */
6670 : 5580 : eltinit = build2 (INIT_EXPR, elttype, new_ctx.object, eltinit);
6671 : 5717 : eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval,
6672 : : non_constant_p, overflow_p,
6673 : : jump_target);
6674 : 5717 : reuse = i == 0;
6675 : : }
6676 : : else
6677 : : {
6678 : : /* Copying an element. */
6679 : 159 : eltinit = cp_build_array_ref (input_location, init, idx, complain);
6680 : 159 : if (!lvalue_p (init))
6681 : 132 : eltinit = move (eltinit);
6682 : 159 : eltinit = (perform_implicit_conversion_flags
6683 : 159 : (elttype, eltinit, complain,
6684 : : LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING));
6685 : 159 : if (CLASS_TYPE_P (elttype) && new_ctx.object)
6686 : : /* Clarify what object is being initialized (118285). */
6687 : 142 : eltinit = build2 (INIT_EXPR, elttype, new_ctx.object, eltinit);
6688 : 159 : eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval,
6689 : : non_constant_p, overflow_p,
6690 : : jump_target);
6691 : : }
6692 : 6052 : if (*jump_target)
6693 : : return NULL_TREE;
6694 : 6052 : if (*non_constant_p)
6695 : : break;
6696 : 5954 : if (no_slot)
6697 : : {
6698 : : /* This is an initializer for an empty subobject; now that we've
6699 : : checked that it's constant, we can ignore it. */
6700 : 18 : gcc_checking_assert (i == 0);
6701 : : break;
6702 : : }
6703 : 5936 : else if (new_ctx.ctor != ctx->ctor)
6704 : : {
6705 : : /* We appended this element above; update the value. */
6706 : 5805 : gcc_assert ((*p)->last().index == idx);
6707 : 5805 : (*p)->last().value = eltinit;
6708 : : }
6709 : : else
6710 : 131 : CONSTRUCTOR_APPEND_ELT (*p, idx, eltinit);
6711 : : /* Reuse the result of cxx_eval_constant_expression call
6712 : : from the first iteration to all others if it is a constant
6713 : : initializer that doesn't require relocations. */
6714 : 5936 : if (reuse
6715 : 5936 : && max > 1
6716 : 5936 : && (eltinit == NULL_TREE
6717 : 442 : || (initializer_constant_valid_p (eltinit, TREE_TYPE (eltinit))
6718 : 442 : == null_pointer_node)))
6719 : : {
6720 : 289 : if (new_ctx.ctor != ctx->ctor)
6721 : 164 : eltinit = new_ctx.ctor;
6722 : 289 : tree range = build2 (RANGE_EXPR, size_type_node,
6723 : : build_int_cst (size_type_node, 1),
6724 : 289 : build_int_cst (size_type_node, max - 1));
6725 : 289 : CONSTRUCTOR_APPEND_ELT (*p, range, unshare_constructor (eltinit));
6726 : 289 : break;
6727 : : }
6728 : 5647 : else if (i == 0)
6729 : 203 : vec_safe_reserve (*p, max);
6730 : : }
6731 : :
6732 : 611 : if (!*non_constant_p)
6733 : : {
6734 : 513 : init = ctx->ctor;
6735 : 513 : CONSTRUCTOR_NO_CLEARING (init) = false;
6736 : : }
6737 : 611 : return init;
6738 : : }
6739 : :
6740 : : static tree
6741 : 501 : cxx_eval_vec_init (const constexpr_ctx *ctx, tree t,
6742 : : value_cat lval,
6743 : : bool *non_constant_p, bool *overflow_p, tree *jump_target)
6744 : : {
6745 : 501 : tree atype = TREE_TYPE (t);
6746 : 501 : tree init = VEC_INIT_EXPR_INIT (t);
6747 : 501 : bool value_init = VEC_INIT_EXPR_VALUE_INIT (t);
6748 : 501 : if (!init || !BRACE_ENCLOSED_INITIALIZER_P (init))
6749 : : ;
6750 : 72 : else if (CONSTRUCTOR_NELTS (init) == 0
6751 : 72 : && !CP_AGGREGATE_TYPE_P (strip_array_types (atype)))
6752 : : {
6753 : : /* Handle {} as value-init. */
6754 : : init = NULL_TREE;
6755 : : value_init = true;
6756 : : }
6757 : : else
6758 : : {
6759 : : /* This is a more complicated case, like needing to loop over trailing
6760 : : elements; call build_vec_init and evaluate the result. */
6761 : 63 : tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
6762 : 63 : constexpr_ctx new_ctx = *ctx;
6763 : 63 : if (!ctx->object)
6764 : : {
6765 : : /* We want to have an initialization target for an VEC_INIT_EXPR.
6766 : : If we don't already have one in CTX, use the VEC_INIT_EXPR_SLOT. */
6767 : 51 : new_ctx.object = VEC_INIT_EXPR_SLOT (t);
6768 : 51 : tree ctor = new_ctx.ctor = build_constructor (atype, NULL);
6769 : 51 : CONSTRUCTOR_NO_CLEARING (ctor) = true;
6770 : 51 : ctx->global->put_value (new_ctx.object, ctor);
6771 : 51 : ctx = &new_ctx;
6772 : : }
6773 : 63 : init = expand_vec_init_expr (ctx->object, t, complain);
6774 : 63 : return cxx_eval_constant_expression (ctx, init, lval, non_constant_p,
6775 : : overflow_p, jump_target);
6776 : : }
6777 : 438 : tree r = cxx_eval_vec_init_1 (ctx, atype, init, value_init,
6778 : : lval, non_constant_p, overflow_p, jump_target);
6779 : 438 : if (*non_constant_p)
6780 : : return t;
6781 : : else
6782 : 359 : return r;
6783 : : }
6784 : :
6785 : : /* Like same_type_ignoring_top_level_qualifiers_p, but also handle the case
6786 : : where the desired type is an array of unknown bounds because the variable
6787 : : has had its bounds deduced since the wrapping expression was created. */
6788 : :
6789 : : static bool
6790 : 138833235 : same_type_ignoring_tlq_and_bounds_p (tree type1, tree type2)
6791 : : {
6792 : 138833235 : while (TREE_CODE (type1) == ARRAY_TYPE
6793 : 13295 : && TREE_CODE (type2) == ARRAY_TYPE
6794 : 138833239 : && (!TYPE_DOMAIN (type1) || !TYPE_DOMAIN (type2)))
6795 : : {
6796 : 2 : type1 = TREE_TYPE (type1);
6797 : 2 : type2 = TREE_TYPE (type2);
6798 : : }
6799 : 138833235 : return same_type_ignoring_top_level_qualifiers_p (type1, type2);
6800 : : }
6801 : :
6802 : : /* Try to determine the currently active union member for an expression
6803 : : with UNION_TYPE. If it can be determined, return the FIELD_DECL,
6804 : : otherwise return NULL_TREE. */
6805 : :
6806 : : static tree
6807 : 75 : cxx_union_active_member (const constexpr_ctx *ctx, tree t, tree *jump_target)
6808 : : {
6809 : 75 : constexpr_ctx new_ctx = *ctx;
6810 : 75 : new_ctx.quiet = true;
6811 : 75 : bool non_constant_p = false, overflow_p = false;
6812 : 75 : tree ctor = cxx_eval_constant_expression (&new_ctx, t, vc_prvalue,
6813 : : &non_constant_p,
6814 : : &overflow_p, jump_target);
6815 : 75 : if (*jump_target)
6816 : : return NULL_TREE;
6817 : 75 : if (TREE_CODE (ctor) == CONSTRUCTOR
6818 : 24 : && CONSTRUCTOR_NELTS (ctor) == 1
6819 : 12 : && CONSTRUCTOR_ELT (ctor, 0)->index
6820 : 87 : && TREE_CODE (CONSTRUCTOR_ELT (ctor, 0)->index) == FIELD_DECL)
6821 : : return CONSTRUCTOR_ELT (ctor, 0)->index;
6822 : : return NULL_TREE;
6823 : : }
6824 : :
6825 : : /* Helper function for cxx_fold_indirect_ref_1, called recursively. */
6826 : :
6827 : : static tree
6828 : 3380512 : cxx_fold_indirect_ref_1 (const constexpr_ctx *ctx, location_t loc, tree type,
6829 : : tree op, unsigned HOST_WIDE_INT off, bool *empty_base,
6830 : : tree *jump_target)
6831 : : {
6832 : 5366507 : tree optype = TREE_TYPE (op);
6833 : 5366507 : unsigned HOST_WIDE_INT const_nunits;
6834 : 5366507 : if (off == 0 && similar_type_p (optype, type))
6835 : : return op;
6836 : 3376750 : else if (cxx_dialect >= cxx26
6837 : 1897860 : && VAR_P (op)
6838 : 651819 : && DECL_VTABLE_OR_VTT_P (op)
6839 : 11823 : && same_type_ignoring_top_level_qualifiers_p (type,
6840 : : ptrdiff_type_node)
6841 : 3377732 : && POINTER_TYPE_P (strip_array_types (optype)))
6842 : : {
6843 : : /* We often read some virtual table elements using ptrdiff_t rather
6844 : : than pointer type. */
6845 : 982 : if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc,
6846 : : strip_array_types (optype),
6847 : : op, off, empty_base,
6848 : : jump_target))
6849 : 982 : return fold_convert (type, ret);
6850 : : }
6851 : 3375768 : else if (TREE_CODE (optype) == COMPLEX_TYPE
6852 : 3375768 : && similar_type_p (type, TREE_TYPE (optype)))
6853 : : {
6854 : : /* *(foo *)&complexfoo => __real__ complexfoo */
6855 : 0 : if (off == 0)
6856 : 0 : return build1_loc (loc, REALPART_EXPR, type, op);
6857 : : /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
6858 : 0 : else if (tree_to_uhwi (TYPE_SIZE_UNIT (type)) == off)
6859 : 0 : return build1_loc (loc, IMAGPART_EXPR, type, op);
6860 : : }
6861 : : /* ((foo*)&vectorfoo)[x] => BIT_FIELD_REF<vectorfoo,...> */
6862 : 3375768 : else if (VECTOR_TYPE_P (optype)
6863 : 0 : && similar_type_p (type, TREE_TYPE (optype))
6864 : 3375768 : && TYPE_VECTOR_SUBPARTS (optype).is_constant (&const_nunits))
6865 : : {
6866 : 0 : unsigned HOST_WIDE_INT part_width = tree_to_uhwi (TYPE_SIZE_UNIT (type));
6867 : 0 : unsigned HOST_WIDE_INT max_offset = part_width * const_nunits;
6868 : 0 : if (off < max_offset && off % part_width == 0)
6869 : : {
6870 : 0 : tree index = bitsize_int (off * BITS_PER_UNIT);
6871 : 0 : return build3_loc (loc, BIT_FIELD_REF, type, op,
6872 : 0 : TYPE_SIZE (type), index);
6873 : : }
6874 : : }
6875 : : /* ((foo *)&fooarray)[x] => fooarray[x] */
6876 : 3375768 : else if (TREE_CODE (optype) == ARRAY_TYPE
6877 : 1985995 : && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (optype)))
6878 : 5361763 : && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (optype))))
6879 : : {
6880 : 1985995 : tree type_domain = TYPE_DOMAIN (optype);
6881 : 1985995 : tree min_val = size_zero_node;
6882 : 1985995 : if (type_domain && TYPE_MIN_VALUE (type_domain))
6883 : 1985990 : min_val = TYPE_MIN_VALUE (type_domain);
6884 : 1985995 : unsigned HOST_WIDE_INT el_sz
6885 : 1985995 : = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (optype)));
6886 : 1985995 : unsigned HOST_WIDE_INT idx = off / el_sz;
6887 : 1985995 : unsigned HOST_WIDE_INT rem = off % el_sz;
6888 : 1985995 : if (tree_fits_uhwi_p (min_val))
6889 : : {
6890 : 1985995 : tree index = size_int (idx + tree_to_uhwi (min_val));
6891 : 1985995 : op = build4_loc (loc, ARRAY_REF, TREE_TYPE (optype), op, index,
6892 : : NULL_TREE, NULL_TREE);
6893 : 1985995 : return cxx_fold_indirect_ref_1 (ctx, loc, type, op, rem,
6894 : 1985995 : empty_base, jump_target);
6895 : : }
6896 : : }
6897 : : /* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */
6898 : 1389773 : else if (TREE_CODE (optype) == RECORD_TYPE
6899 : 1389773 : || TREE_CODE (optype) == UNION_TYPE)
6900 : : {
6901 : 1386414 : if (TREE_CODE (optype) == UNION_TYPE)
6902 : : /* For unions prefer the currently active member. */
6903 : 75 : if (tree field = cxx_union_active_member (ctx, op, jump_target))
6904 : : {
6905 : 12 : unsigned HOST_WIDE_INT el_sz
6906 : 12 : = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field)));
6907 : 12 : if (off < el_sz)
6908 : : {
6909 : 12 : tree cop = build3 (COMPONENT_REF, TREE_TYPE (field),
6910 : : op, field, NULL_TREE);
6911 : 12 : if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop,
6912 : : off, empty_base,
6913 : : jump_target))
6914 : : return ret;
6915 : : }
6916 : : }
6917 : :
6918 : : /* Handle conversion to "as base" type. */
6919 : 1386408 : if (CLASS_TYPE_P (optype)
6920 : 2772728 : && CLASSTYPE_AS_BASE (optype) == type)
6921 : : return op;
6922 : :
6923 : : /* Handle conversion to an empty base class, which is represented with a
6924 : : NOP_EXPR. Do this before spelunking into the non-empty subobjects,
6925 : : which is likely to be a waste of time (109678). */
6926 : 1385838 : if (is_empty_class (type)
6927 : 1359076 : && CLASS_TYPE_P (optype)
6928 : 2744908 : && lookup_base (optype, type, ba_any, NULL, tf_none, off))
6929 : : {
6930 : 796182 : if (empty_base)
6931 : 796182 : *empty_base = true;
6932 : 796182 : return op;
6933 : : }
6934 : :
6935 : 589656 : for (tree field = TYPE_FIELDS (optype);
6936 : 5403420 : field; field = DECL_CHAIN (field))
6937 : 5401031 : if (TREE_CODE (field) == FIELD_DECL
6938 : 595524 : && TREE_TYPE (field) != error_mark_node
6939 : 5996555 : && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (field))))
6940 : : {
6941 : 595524 : tree pos = byte_position (field);
6942 : 595524 : if (!tree_fits_uhwi_p (pos))
6943 : 0 : continue;
6944 : 595524 : unsigned HOST_WIDE_INT upos = tree_to_uhwi (pos);
6945 : 595524 : unsigned HOST_WIDE_INT el_sz;
6946 : 595524 : if (DECL_FIELD_IS_BASE (field)
6947 : 30814 : && CLASS_TYPE_P (optype)
6948 : 626338 : && CLASSTYPE_VBASECLASSES (optype))
6949 : 6189 : el_sz = tree_to_uhwi (DECL_SIZE_UNIT (field));
6950 : : else
6951 : 589335 : el_sz = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field)));
6952 : 595524 : if (upos <= off && off < upos + el_sz)
6953 : : {
6954 : 589566 : tree cop = build3 (COMPONENT_REF, TREE_TYPE (field),
6955 : : op, field, NULL_TREE);
6956 : 589566 : if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop,
6957 : : off - upos,
6958 : : empty_base,
6959 : : jump_target))
6960 : : return ret;
6961 : : }
6962 : : }
6963 : : }
6964 : :
6965 : : return NULL_TREE;
6966 : : }
6967 : :
6968 : : /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
6969 : : match. We want to be less strict for simple *& folding; if we have a
6970 : : non-const temporary that we access through a const pointer, that should
6971 : : work. We handle this here rather than change fold_indirect_ref_1
6972 : : because we're dealing with things like ADDR_EXPR of INTEGER_CST which
6973 : : don't really make sense outside of constant expression evaluation. Also
6974 : : we want to allow folding to COMPONENT_REF, which could cause trouble
6975 : : with TBAA in fold_indirect_ref_1. */
6976 : :
6977 : : static tree
6978 : 55952465 : cxx_fold_indirect_ref (const constexpr_ctx *ctx, location_t loc, tree type,
6979 : : tree op0, bool *empty_base, tree *jump_target)
6980 : : {
6981 : 55952465 : tree sub = op0;
6982 : 55952465 : tree subtype;
6983 : :
6984 : : /* STRIP_NOPS, but stop if REINTERPRET_CAST_P. */
6985 : 116875318 : while (CONVERT_EXPR_P (sub) || TREE_CODE (sub) == NON_LVALUE_EXPR
6986 : 156977125 : || TREE_CODE (sub) == VIEW_CONVERT_EXPR)
6987 : : {
6988 : 43465605 : if (TREE_CODE (sub) == NOP_EXPR
6989 : 43465605 : && REINTERPRET_CAST_P (sub))
6990 : : return NULL_TREE;
6991 : 43465605 : sub = TREE_OPERAND (sub, 0);
6992 : : }
6993 : :
6994 : 55952465 : subtype = TREE_TYPE (sub);
6995 : 55952465 : if (!INDIRECT_TYPE_P (subtype))
6996 : : return NULL_TREE;
6997 : :
6998 : : /* Canonicalizes the given OBJ/OFF pair by iteratively absorbing
6999 : : the innermost component into the offset until it would make the
7000 : : offset positive, so that cxx_fold_indirect_ref_1 can identify
7001 : : more folding opportunities. */
7002 : 58742365 : auto canonicalize_obj_off = [] (tree& obj, tree& off) {
7003 : 2789952 : if (cxx_dialect >= cxx26)
7004 : : {
7005 : : /* For C++26, we need to fold *(B *)(&x.D.1234 + 32) used
7006 : : to access virtual base members. */
7007 : 1538323 : tree nobj = obj;
7008 : 1538323 : while (TREE_CODE (nobj) == COMPONENT_REF
7009 : 1551244 : && DECL_FIELD_IS_BASE (TREE_OPERAND (nobj, 1)))
7010 : 12921 : nobj = TREE_OPERAND (nobj, 0);
7011 : 1538323 : if (nobj != obj
7012 : 12465 : && CLASS_TYPE_P (TREE_TYPE (nobj))
7013 : 1550788 : && CLASSTYPE_VBASECLASSES (TREE_TYPE (nobj)))
7014 : 2039 : while (obj != nobj)
7015 : : {
7016 : 1174 : tree field = TREE_OPERAND (obj, 1);
7017 : 1174 : tree pos = byte_position (field);
7018 : 1174 : off = int_const_binop (PLUS_EXPR, off, pos);
7019 : 1174 : obj = TREE_OPERAND (obj, 0);
7020 : : }
7021 : : }
7022 : 3384420 : while (TREE_CODE (obj) == COMPONENT_REF
7023 : : /* We need to preserve union member accesses so that we can
7024 : : later properly diagnose accessing the wrong member. */
7025 : 1368510 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (obj, 0))) == RECORD_TYPE
7026 : 4668724 : && (tree_int_cst_sign_bit (off) || integer_zerop (off)))
7027 : : {
7028 : 596446 : tree field = TREE_OPERAND (obj, 1);
7029 : 596446 : tree pos = byte_position (field);
7030 : 596446 : if (integer_zerop (off) && integer_nonzerop (pos))
7031 : : /* If the offset is already 0, keep going as long as the
7032 : : component is at position 0. */
7033 : : break;
7034 : 594468 : off = int_const_binop (PLUS_EXPR, off, pos);
7035 : 594468 : obj = TREE_OPERAND (obj, 0);
7036 : : }
7037 : 2789952 : };
7038 : :
7039 : 55952413 : if (TREE_CODE (sub) == ADDR_EXPR)
7040 : : {
7041 : 23478822 : tree op = TREE_OPERAND (sub, 0);
7042 : 23478822 : tree optype = TREE_TYPE (op);
7043 : :
7044 : : /* *&CONST_DECL -> to the value of the const decl. */
7045 : 23478822 : if (TREE_CODE (op) == CONST_DECL)
7046 : 0 : return DECL_INITIAL (op);
7047 : : /* *&p => p; make sure to handle *&"str"[cst] here. */
7048 : 23478822 : if (similar_type_p (optype, type))
7049 : : {
7050 : 22373153 : tree fop = fold_read_from_constant_string (op);
7051 : 22373153 : if (fop)
7052 : : return fop;
7053 : : else
7054 : 22363061 : return op;
7055 : : }
7056 : : else
7057 : : {
7058 : 1105669 : tree off = integer_zero_node;
7059 : 1105669 : canonicalize_obj_off (op, off);
7060 : 1105669 : return cxx_fold_indirect_ref_1 (ctx, loc, type, op,
7061 : : tree_to_uhwi (off), empty_base,
7062 : : jump_target);
7063 : : }
7064 : : }
7065 : 32473591 : else if (TREE_CODE (sub) == POINTER_PLUS_EXPR
7066 : 32473591 : && tree_fits_uhwi_p (TREE_OPERAND (sub, 1)))
7067 : : {
7068 : 1829362 : tree op00 = TREE_OPERAND (sub, 0);
7069 : 1829362 : tree off = TREE_OPERAND (sub, 1);
7070 : :
7071 : 1829362 : STRIP_NOPS (op00);
7072 : 1829362 : if (TREE_CODE (op00) == ADDR_EXPR)
7073 : : {
7074 : 1684283 : tree obj = TREE_OPERAND (op00, 0);
7075 : 1684283 : canonicalize_obj_off (obj, off);
7076 : 1684283 : return cxx_fold_indirect_ref_1 (ctx, loc, type, obj,
7077 : : tree_to_uhwi (off), empty_base,
7078 : : jump_target);
7079 : : }
7080 : : }
7081 : : /* *(foo *)fooarrptr => (*fooarrptr)[0] */
7082 : 30644229 : else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
7083 : 30644229 : && similar_type_p (type, TREE_TYPE (TREE_TYPE (subtype))))
7084 : : {
7085 : 0 : tree type_domain;
7086 : 0 : tree min_val = size_zero_node;
7087 : 0 : tree newsub
7088 : 0 : = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (subtype), sub, NULL,
7089 : : jump_target);
7090 : 0 : if (*jump_target)
7091 : : return NULL_TREE;
7092 : 0 : if (newsub)
7093 : : sub = newsub;
7094 : : else
7095 : 0 : sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub);
7096 : 0 : type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
7097 : 0 : if (type_domain && TYPE_MIN_VALUE (type_domain))
7098 : 0 : min_val = TYPE_MIN_VALUE (type_domain);
7099 : 0 : return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
7100 : 0 : NULL_TREE);
7101 : : }
7102 : :
7103 : : return NULL_TREE;
7104 : : }
7105 : :
7106 : : static tree
7107 : 31898760 : cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t,
7108 : : value_cat lval,
7109 : : bool *non_constant_p, bool *overflow_p,
7110 : : tree *jump_target)
7111 : : {
7112 : 31898760 : tree orig_op0 = TREE_OPERAND (t, 0);
7113 : 31898760 : bool empty_base = false;
7114 : :
7115 : : /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second
7116 : : operand is an integer-zero. Otherwise reject the MEM_REF for now. */
7117 : :
7118 : 31898760 : if (TREE_CODE (t) == MEM_REF
7119 : 31898760 : && (!TREE_OPERAND (t, 1) || !integer_zerop (TREE_OPERAND (t, 1))))
7120 : : {
7121 : 9 : gcc_assert (ctx->quiet);
7122 : 9 : *non_constant_p = true;
7123 : 9 : return t;
7124 : : }
7125 : :
7126 : : /* First try to simplify it directly. */
7127 : 31898751 : tree r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t),
7128 : : orig_op0, &empty_base, jump_target);
7129 : 31898751 : if (*jump_target)
7130 : : return NULL_TREE;
7131 : 31898751 : if (!r)
7132 : : {
7133 : : /* If that didn't work, evaluate the operand first. */
7134 : 30585901 : tree op0 = cxx_eval_constant_expression (ctx, orig_op0,
7135 : : vc_prvalue, non_constant_p,
7136 : : overflow_p, jump_target);
7137 : 30585901 : if (*jump_target)
7138 : : return NULL_TREE;
7139 : : /* Don't VERIFY_CONSTANT here. */
7140 : 30585895 : if (*non_constant_p)
7141 : : return t;
7142 : :
7143 : 19431810 : if (!lval && integer_zerop (op0))
7144 : : {
7145 : 68 : if (!ctx->quiet)
7146 : 12 : error ("dereferencing a null pointer");
7147 : 68 : *non_constant_p = true;
7148 : 68 : return t;
7149 : : }
7150 : :
7151 : 19431742 : r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t), op0,
7152 : : &empty_base, jump_target);
7153 : 19431742 : if (*jump_target)
7154 : : return NULL_TREE;
7155 : 19431742 : if (r == NULL_TREE)
7156 : : {
7157 : : /* We couldn't fold to a constant value. Make sure it's not
7158 : : something we should have been able to fold. */
7159 : 206873 : tree sub = op0;
7160 : 206873 : STRIP_NOPS (sub);
7161 : 206873 : if (TREE_CODE (sub) == ADDR_EXPR)
7162 : : {
7163 : 1639 : gcc_assert (!similar_type_p
7164 : : (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t)));
7165 : : /* DR 1188 says we don't have to deal with this. */
7166 : 1639 : if (!ctx->quiet)
7167 : 5 : error_at (cp_expr_loc_or_input_loc (t),
7168 : : "accessing value of %qE through a %qT glvalue in a "
7169 : : "constant expression", build_fold_indirect_ref (sub),
7170 : 4 : TREE_TYPE (t));
7171 : 1639 : *non_constant_p = true;
7172 : 1639 : return t;
7173 : : }
7174 : :
7175 : 205234 : if (lval == vc_glvalue && op0 != orig_op0)
7176 : 18307 : return build1 (INDIRECT_REF, TREE_TYPE (t), op0);
7177 : 186927 : if (!lval)
7178 : 131249 : VERIFY_CONSTANT (t);
7179 : 186927 : return t;
7180 : : }
7181 : : }
7182 : :
7183 : 20537719 : r = cxx_eval_constant_expression (ctx, r,
7184 : : lval, non_constant_p, overflow_p,
7185 : : jump_target);
7186 : 20537719 : if (*jump_target)
7187 : : return NULL_TREE;
7188 : 20537719 : if (*non_constant_p)
7189 : : return t;
7190 : :
7191 : : /* If we're pulling out the value of an empty base, just return an empty
7192 : : CONSTRUCTOR. */
7193 : 14953511 : if (empty_base && !lval)
7194 : : {
7195 : 13212 : r = build_constructor (TREE_TYPE (t), NULL);
7196 : 13212 : TREE_CONSTANT (r) = true;
7197 : : }
7198 : :
7199 : : return r;
7200 : : }
7201 : :
7202 : : /* Complain about R, a DECL that is accessed outside its lifetime. */
7203 : :
7204 : : static void
7205 : 34 : outside_lifetime_error (location_t loc, tree r)
7206 : : {
7207 : 34 : auto_diagnostic_group d;
7208 : 34 : if (DECL_NAME (r) == heap_deleted_identifier)
7209 : : {
7210 : : /* Provide a more accurate message for deleted variables. */
7211 : 6 : error_at (loc, "use of allocated storage after deallocation "
7212 : : "in a constant expression");
7213 : 6 : inform (DECL_SOURCE_LOCATION (r), "allocated here");
7214 : : }
7215 : : else
7216 : : {
7217 : 28 : error_at (loc, "accessing %qE outside its lifetime", r);
7218 : 28 : inform (DECL_SOURCE_LOCATION (r), "declared here");
7219 : : }
7220 : 34 : }
7221 : :
7222 : : /* Complain about R, a VAR_DECL, not being usable in a constant expression.
7223 : : FUNDEF_P is true if we're checking a constexpr function body.
7224 : : Shared between potential_constant_expression and
7225 : : cxx_eval_constant_expression. */
7226 : :
7227 : : static void
7228 : 312 : non_const_var_error (location_t loc, tree r, bool fundef_p)
7229 : : {
7230 : 312 : auto_diagnostic_group d;
7231 : 312 : tree type = TREE_TYPE (r);
7232 : 312 : if (DECL_NAME (r) == heap_uninit_identifier
7233 : 312 : || DECL_NAME (r) == heap_identifier
7234 : 309 : || DECL_NAME (r) == heap_vec_uninit_identifier
7235 : 621 : || DECL_NAME (r) == heap_vec_identifier)
7236 : : {
7237 : 3 : if (constexpr_error (loc, fundef_p, "the content of uninitialized "
7238 : : "storage is not usable in a constant expression"))
7239 : 3 : inform (DECL_SOURCE_LOCATION (r), "allocated here");
7240 : 3 : return;
7241 : : }
7242 : 309 : if (DECL_NAME (r) == heap_deleted_identifier)
7243 : : {
7244 : 0 : if (constexpr_error (loc, fundef_p, "use of allocated storage after "
7245 : : "deallocation in a constant expression"))
7246 : 0 : inform (DECL_SOURCE_LOCATION (r), "allocated here");
7247 : 0 : return;
7248 : : }
7249 : 309 : if (!constexpr_error (loc, fundef_p, "the value of %qD is not usable in "
7250 : : "a constant expression", r))
7251 : : return;
7252 : : /* Avoid error cascade. */
7253 : 308 : if (DECL_INITIAL (r) == error_mark_node)
7254 : : return;
7255 : 294 : if (DECL_DECLARED_CONSTEXPR_P (r))
7256 : 3 : inform (DECL_SOURCE_LOCATION (r),
7257 : : "%qD used in its own initializer", r);
7258 : 291 : else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
7259 : : {
7260 : 216 : if (!CP_TYPE_CONST_P (type))
7261 : 187 : inform (DECL_SOURCE_LOCATION (r),
7262 : : "%q#D is not const", r);
7263 : 29 : else if (CP_TYPE_VOLATILE_P (type))
7264 : 0 : inform (DECL_SOURCE_LOCATION (r),
7265 : : "%q#D is volatile", r);
7266 : 29 : else if (!DECL_INITIAL (r)
7267 : 9 : || !TREE_CONSTANT (DECL_INITIAL (r))
7268 : 37 : || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r))
7269 : 29 : inform (DECL_SOURCE_LOCATION (r),
7270 : : "%qD was not initialized with a constant "
7271 : : "expression", r);
7272 : : else
7273 : 0 : gcc_unreachable ();
7274 : : }
7275 : 75 : else if (TYPE_REF_P (type))
7276 : 9 : inform (DECL_SOURCE_LOCATION (r),
7277 : : "%qD was not initialized with a constant "
7278 : : "expression", r);
7279 : : else
7280 : : {
7281 : 66 : if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r))
7282 : 66 : inform (DECL_SOURCE_LOCATION (r),
7283 : : "%qD was not declared %<constexpr%>", r);
7284 : : else
7285 : 0 : inform (DECL_SOURCE_LOCATION (r),
7286 : : "%qD does not have integral or enumeration type",
7287 : : r);
7288 : : }
7289 : 312 : }
7290 : :
7291 : : /* Subroutine of cxx_eval_constant_expression.
7292 : : Like cxx_eval_unary_expression, except for trinary expressions. */
7293 : :
7294 : : static tree
7295 : 16 : cxx_eval_trinary_expression (const constexpr_ctx *ctx, tree t,
7296 : : value_cat lval,
7297 : : bool *non_constant_p, bool *overflow_p,
7298 : : tree *jump_target)
7299 : : {
7300 : 16 : int i;
7301 : 16 : tree args[3];
7302 : 16 : tree val;
7303 : :
7304 : 37 : for (i = 0; i < 3; i++)
7305 : : {
7306 : 30 : args[i] = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, i),
7307 : : lval,
7308 : : non_constant_p, overflow_p,
7309 : : jump_target);
7310 : 30 : if (*jump_target)
7311 : : return NULL_TREE;
7312 : 30 : VERIFY_CONSTANT (args[i]);
7313 : : }
7314 : :
7315 : 7 : val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t),
7316 : : args[0], args[1], args[2]);
7317 : 7 : if (val == NULL_TREE)
7318 : : return t;
7319 : 7 : VERIFY_CONSTANT (val);
7320 : : return val;
7321 : : }
7322 : :
7323 : : /* True if T was declared in a function declared to be constexpr, and
7324 : : therefore potentially constant in C++14. */
7325 : :
7326 : : bool
7327 : 78885538 : var_in_constexpr_fn (tree t)
7328 : : {
7329 : 78885538 : tree ctx = DECL_CONTEXT (t);
7330 : 78885538 : return (ctx && TREE_CODE (ctx) == FUNCTION_DECL
7331 : 150248607 : && DECL_DECLARED_CONSTEXPR_P (ctx));
7332 : : }
7333 : :
7334 : : /* True if a function might be constexpr: either a function that was
7335 : : declared constexpr, or a C++17 lambda op(). */
7336 : :
7337 : : bool
7338 : 429624877 : maybe_constexpr_fn (tree t)
7339 : : {
7340 : 429624877 : return (DECL_DECLARED_CONSTEXPR_P (t)
7341 : 194171570 : || (cxx_dialect >= cxx17 && LAMBDA_FUNCTION_P (t))
7342 : 619108328 : || (flag_implicit_constexpr
7343 : 66 : && DECL_DECLARED_INLINE_P (STRIP_TEMPLATE (t))));
7344 : : }
7345 : :
7346 : : /* True if T was declared in a function that might be constexpr: either a
7347 : : function that was declared constexpr, or a C++17 lambda op(). */
7348 : :
7349 : : bool
7350 : 51674167 : var_in_maybe_constexpr_fn (tree t)
7351 : : {
7352 : 103347772 : return (DECL_FUNCTION_SCOPE_P (t)
7353 : 96455079 : && maybe_constexpr_fn (DECL_CONTEXT (t)));
7354 : : }
7355 : :
7356 : : /* We're assigning INIT to TARGET. In do_build_copy_constructor and
7357 : : build_over_call we implement trivial copy of a class with tail padding using
7358 : : assignment of character arrays, which is valid in normal code, but not in
7359 : : constexpr evaluation. We don't need to worry about clobbering tail padding
7360 : : in constexpr evaluation, so strip the type punning. */
7361 : :
7362 : : static void
7363 : 29987474 : maybe_simplify_trivial_copy (tree &target, tree &init)
7364 : : {
7365 : 29987474 : if (TREE_CODE (target) == MEM_REF
7366 : 2364 : && TREE_CODE (init) == MEM_REF
7367 : 2326 : && TREE_TYPE (target) == TREE_TYPE (init)
7368 : 2326 : && TREE_CODE (TREE_TYPE (target)) == ARRAY_TYPE
7369 : 29989800 : && TREE_TYPE (TREE_TYPE (target)) == unsigned_char_type_node)
7370 : : {
7371 : 2326 : target = build_fold_indirect_ref (TREE_OPERAND (target, 0));
7372 : 2326 : init = build_fold_indirect_ref (TREE_OPERAND (init, 0));
7373 : : }
7374 : 29987474 : }
7375 : :
7376 : : /* Returns true if REF, which is a COMPONENT_REF, has any fields
7377 : : of constant type. This does not check for 'mutable', so the
7378 : : caller is expected to be mindful of that. */
7379 : :
7380 : : static bool
7381 : 277 : cref_has_const_field (tree ref)
7382 : : {
7383 : 346 : while (TREE_CODE (ref) == COMPONENT_REF)
7384 : : {
7385 : 337 : if (CP_TYPE_CONST_P (TREE_TYPE (TREE_OPERAND (ref, 1))))
7386 : : return true;
7387 : 69 : ref = TREE_OPERAND (ref, 0);
7388 : : }
7389 : : return false;
7390 : : }
7391 : :
7392 : : /* Return true if we are modifying something that is const during constant
7393 : : expression evaluation. CODE is the code of the statement, OBJ is the
7394 : : object in question, MUTABLE_P is true if one of the subobjects were
7395 : : declared mutable. */
7396 : :
7397 : : static bool
7398 : 27136194 : modifying_const_object_p (tree_code code, tree obj, bool mutable_p)
7399 : : {
7400 : : /* If this is initialization, there's no problem. */
7401 : 27136194 : if (code != MODIFY_EXPR)
7402 : : return false;
7403 : :
7404 : : /* [basic.type.qualifier] "A const object is an object of type
7405 : : const T or a non-mutable subobject of a const object." */
7406 : 7851714 : if (mutable_p)
7407 : : return false;
7408 : :
7409 : 7850978 : if (TREE_READONLY (obj))
7410 : : return true;
7411 : :
7412 : 7849132 : if (CP_TYPE_CONST_P (TREE_TYPE (obj)))
7413 : : {
7414 : : /* Although a COMPONENT_REF may have a const type, we should
7415 : : only consider it modifying a const object when any of the
7416 : : field components is const. This can happen when using
7417 : : constructs such as const_cast<const T &>(m), making something
7418 : : const even though it wasn't declared const. */
7419 : 3914 : if (TREE_CODE (obj) == COMPONENT_REF)
7420 : 277 : return cref_has_const_field (obj);
7421 : : else
7422 : : return true;
7423 : : }
7424 : :
7425 : : return false;
7426 : : }
7427 : :
7428 : : /* Evaluate an INIT_EXPR or MODIFY_EXPR. */
7429 : :
7430 : : static tree
7431 : 32892546 : cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
7432 : : value_cat lval,
7433 : : bool *non_constant_p, bool *overflow_p,
7434 : : tree *jump_target)
7435 : : {
7436 : 32892546 : constexpr_ctx new_ctx = *ctx;
7437 : :
7438 : 32892546 : tree init = TREE_OPERAND (t, 1);
7439 : :
7440 : 3093960 : if (TREE_CLOBBER_P (init)
7441 : 35832951 : && CLOBBER_KIND (init) < CLOBBER_OBJECT_END)
7442 : : /* Only handle clobbers ending the lifetime of objects.
7443 : : ??? We should probably set CONSTRUCTOR_NO_CLEARING. */
7444 : 2905072 : return void_node;
7445 : :
7446 : : /* First we figure out where we're storing to. */
7447 : 29987474 : tree target = TREE_OPERAND (t, 0);
7448 : :
7449 : 29987474 : maybe_simplify_trivial_copy (target, init);
7450 : :
7451 : 29987474 : tree type = TREE_TYPE (target);
7452 : 29987474 : bool preeval = SCALAR_TYPE_P (type) || TREE_CODE (t) == MODIFY_EXPR;
7453 : 23427746 : if (preeval && !TREE_CLOBBER_P (init))
7454 : : {
7455 : : /* Evaluate the value to be stored without knowing what object it will be
7456 : : stored in, so that any side-effects happen first. */
7457 : 23392413 : if (!SCALAR_TYPE_P (type))
7458 : 48809 : new_ctx.ctor = new_ctx.object = NULL_TREE;
7459 : 23392413 : init = cxx_eval_constant_expression (&new_ctx, init, vc_prvalue,
7460 : : non_constant_p, overflow_p,
7461 : : jump_target);
7462 : 23392413 : if (*jump_target)
7463 : : return NULL_TREE;
7464 : 23392334 : if (*non_constant_p)
7465 : : return t;
7466 : : }
7467 : :
7468 : 24463349 : bool evaluated = false;
7469 : 24463349 : if (lval == vc_glvalue)
7470 : : {
7471 : : /* If we want to return a reference to the target, we need to evaluate it
7472 : : as a whole; otherwise, only evaluate the innermost piece to avoid
7473 : : building up unnecessary *_REFs. */
7474 : 0 : target = cxx_eval_constant_expression (ctx, target, lval,
7475 : : non_constant_p, overflow_p,
7476 : : jump_target);
7477 : 0 : evaluated = true;
7478 : 0 : if (*jump_target)
7479 : : return NULL_TREE;
7480 : 0 : if (*non_constant_p)
7481 : : return t;
7482 : : }
7483 : :
7484 : : /* Find the underlying variable. */
7485 : 24463349 : releasing_vec refs;
7486 : 24463349 : tree object = NULL_TREE;
7487 : : /* If we're modifying a const object, save it. */
7488 : 24463349 : tree const_object_being_modified = NULL_TREE;
7489 : 24463349 : bool mutable_p = false;
7490 : 81837776 : for (tree probe = target; object == NULL_TREE; )
7491 : : {
7492 : 57374688 : switch (TREE_CODE (probe))
7493 : : {
7494 : 8448243 : case BIT_FIELD_REF:
7495 : 8448243 : case COMPONENT_REF:
7496 : 8448243 : case ARRAY_REF:
7497 : 8448243 : {
7498 : 8448243 : tree ob = TREE_OPERAND (probe, 0);
7499 : 8448243 : tree elt = TREE_OPERAND (probe, 1);
7500 : 8448243 : if (TREE_CODE (elt) == FIELD_DECL && DECL_MUTABLE_P (elt))
7501 : : mutable_p = true;
7502 : 8448243 : if (TREE_CODE (probe) == ARRAY_REF)
7503 : : {
7504 : 1155952 : elt = eval_and_check_array_index (ctx, probe, false,
7505 : : non_constant_p, overflow_p,
7506 : : jump_target);
7507 : 1155952 : if (*jump_target)
7508 : 58 : return NULL_TREE;
7509 : 1155952 : if (*non_constant_p)
7510 : : return t;
7511 : : }
7512 : : /* We don't check modifying_const_object_p for ARRAY_REFs. Given
7513 : : "int a[10]", an ARRAY_REF "a[2]" can be "const int", even though
7514 : : the array isn't const. Instead, check "a" in the next iteration;
7515 : : that will detect modifying "const int a[10]". */
7516 : 7292291 : else if (evaluated
7517 : 2673106 : && modifying_const_object_p (TREE_CODE (t), probe,
7518 : : mutable_p)
7519 : 7292559 : && const_object_being_modified == NULL_TREE)
7520 : : const_object_being_modified = probe;
7521 : :
7522 : : /* Track named member accesses for unions to validate modifications
7523 : : that change active member. */
7524 : 8448185 : if (!evaluated && TREE_CODE (probe) == COMPONENT_REF)
7525 : 4619185 : vec_safe_push (refs, probe);
7526 : : else
7527 : 3829000 : vec_safe_push (refs, NULL_TREE);
7528 : :
7529 : 8448185 : vec_safe_push (refs, elt);
7530 : 8448185 : vec_safe_push (refs, TREE_TYPE (probe));
7531 : 8448185 : probe = ob;
7532 : : }
7533 : 8448185 : break;
7534 : :
7535 : 15 : case REALPART_EXPR:
7536 : 15 : gcc_assert (refs->is_empty ());
7537 : 15 : vec_safe_push (refs, NULL_TREE);
7538 : 15 : vec_safe_push (refs, probe);
7539 : 15 : vec_safe_push (refs, TREE_TYPE (probe));
7540 : 15 : probe = TREE_OPERAND (probe, 0);
7541 : 15 : break;
7542 : :
7543 : 17 : case IMAGPART_EXPR:
7544 : 17 : gcc_assert (refs->is_empty ());
7545 : 17 : vec_safe_push (refs, NULL_TREE);
7546 : 17 : vec_safe_push (refs, probe);
7547 : 17 : vec_safe_push (refs, TREE_TYPE (probe));
7548 : 17 : probe = TREE_OPERAND (probe, 0);
7549 : 17 : break;
7550 : :
7551 : 48926413 : default:
7552 : 48926413 : if (evaluated)
7553 : : object = probe;
7554 : : else
7555 : : {
7556 : 24463325 : tree pvar = tree_strip_any_location_wrapper (probe);
7557 : 24463325 : if (VAR_P (pvar) && DECL_ANON_UNION_VAR_P (pvar))
7558 : : {
7559 : : /* Stores to DECL_ANON_UNION_VAR_P var are allowed to change
7560 : : active union member. */
7561 : 9 : probe = DECL_VALUE_EXPR (pvar);
7562 : 9 : break;
7563 : : }
7564 : 24463316 : probe = cxx_eval_constant_expression (ctx, probe, vc_glvalue,
7565 : : non_constant_p, overflow_p,
7566 : : jump_target);
7567 : 24463316 : evaluated = true;
7568 : 24463316 : if (*jump_target)
7569 : : return NULL_TREE;
7570 : 24463316 : if (*non_constant_p)
7571 : : return t;
7572 : : }
7573 : : break;
7574 : : }
7575 : : }
7576 : :
7577 : 24463088 : if (modifying_const_object_p (TREE_CODE (t), object, mutable_p)
7578 : 24463088 : && const_object_being_modified == NULL_TREE)
7579 : 24463088 : const_object_being_modified = object;
7580 : :
7581 : 24463088 : if (DECL_P (object)
7582 : 24463052 : && TREE_CLOBBER_P (init)
7583 : 24498421 : && DECL_NAME (object) == heap_deleted_identifier)
7584 : : /* Ignore clobbers of deleted allocations for now; we'll get a better error
7585 : : message later when operator delete is called. */
7586 : 15 : return void_node;
7587 : :
7588 : : /* And then find/build up our initializer for the path to the subobject
7589 : : we're initializing. */
7590 : 24463073 : tree *valp;
7591 : 24463073 : if (DECL_P (object))
7592 : 24463037 : valp = ctx->global->get_value_ptr (object, TREE_CODE (t) == INIT_EXPR);
7593 : : else
7594 : 36 : valp = NULL;
7595 : 24463073 : if (!valp)
7596 : : {
7597 : : /* A constant-expression cannot modify objects from outside the
7598 : : constant-expression. */
7599 : 14299 : if (!ctx->quiet)
7600 : : {
7601 : 25 : auto_diagnostic_group d;
7602 : 25 : if (DECL_P (object) && DECL_NAME (object) == heap_deleted_identifier)
7603 : : {
7604 : 0 : error ("modification of allocated storage after deallocation "
7605 : : "is not a constant expression");
7606 : 0 : inform (DECL_SOURCE_LOCATION (object), "allocated here");
7607 : : }
7608 : 25 : else if (DECL_P (object) && ctx->global->is_outside_lifetime (object))
7609 : : {
7610 : 10 : if (TREE_CLOBBER_P (init))
7611 : 3 : error ("destroying %qE outside its lifetime", object);
7612 : : else
7613 : 7 : error ("modification of %qE outside its lifetime "
7614 : : "is not a constant expression", object);
7615 : 10 : inform (DECL_SOURCE_LOCATION (object), "declared here");
7616 : : }
7617 : : else
7618 : : {
7619 : 15 : if (TREE_CLOBBER_P (init))
7620 : 6 : error ("destroying %qE from outside current evaluation "
7621 : : "is not a constant expression", object);
7622 : : else
7623 : 9 : error ("modification of %qE from outside current evaluation "
7624 : : "is not a constant expression", object);
7625 : : }
7626 : 25 : }
7627 : 14299 : *non_constant_p = true;
7628 : 14299 : return t;
7629 : : }
7630 : :
7631 : : /* Handle explicit end-of-lifetime. */
7632 : 24448774 : if (TREE_CLOBBER_P (init))
7633 : : {
7634 : 35288 : if (refs->is_empty ())
7635 : 21514 : ctx->global->destroy_value (object);
7636 : 35288 : return void_node;
7637 : : }
7638 : :
7639 : 24413486 : type = TREE_TYPE (object);
7640 : 24413486 : bool no_zero_init = true;
7641 : 24413486 : bool zero_padding_bits = false;
7642 : :
7643 : 48826972 : auto_vec<tree *> ctors;
7644 : 48826972 : releasing_vec indexes;
7645 : 48826972 : auto_vec<int> index_pos_hints;
7646 : 24413486 : bool activated_union_member_p = false;
7647 : 24413486 : bool empty_base = false;
7648 : 32820399 : while (!refs->is_empty ())
7649 : : {
7650 : 8426577 : if (*valp == NULL_TREE)
7651 : : {
7652 : 957421 : *valp = build_constructor (type, NULL);
7653 : 957421 : CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
7654 : 957421 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
7655 : : }
7656 : 7469156 : else if (STRIP_ANY_LOCATION_WRAPPER (*valp),
7657 : 7469156 : TREE_CODE (*valp) == STRING_CST)
7658 : : {
7659 : : /* An array was initialized with a string constant, and now
7660 : : we're writing into one of its elements. Explode the
7661 : : single initialization into a set of element
7662 : : initializations. */
7663 : 3913 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
7664 : :
7665 : 3913 : tree string = *valp;
7666 : 3913 : tree elt_type = TREE_TYPE (type);
7667 : 3913 : unsigned chars_per_elt = (TYPE_PRECISION (elt_type)
7668 : 3913 : / TYPE_PRECISION (char_type_node));
7669 : 3913 : unsigned num_elts = TREE_STRING_LENGTH (string) / chars_per_elt;
7670 : 3913 : tree ary_ctor = build_constructor (type, NULL);
7671 : :
7672 : 3913 : vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor), num_elts);
7673 : 7987 : for (unsigned ix = 0; ix != num_elts; ix++)
7674 : : {
7675 : 4074 : constructor_elt elt =
7676 : : {
7677 : 4074 : build_int_cst (size_type_node, ix),
7678 : 4074 : extract_string_elt (string, chars_per_elt, ix)
7679 : 4074 : };
7680 : 4074 : CONSTRUCTOR_ELTS (ary_ctor)->quick_push (elt);
7681 : : }
7682 : :
7683 : 3913 : *valp = ary_ctor;
7684 : : }
7685 : :
7686 : 8426577 : enum tree_code code = TREE_CODE (type);
7687 : 8426577 : tree reftype = refs->pop();
7688 : 8426577 : tree index = refs->pop();
7689 : 8426577 : bool is_access_expr = refs->pop() != NULL_TREE;
7690 : :
7691 : 8426577 : if (code == COMPLEX_TYPE)
7692 : : {
7693 : 32 : if (TREE_CODE (*valp) == COMPLEX_CST)
7694 : 30 : *valp = build2 (COMPLEX_EXPR, type, TREE_REALPART (*valp),
7695 : 30 : TREE_IMAGPART (*valp));
7696 : 2 : else if (TREE_CODE (*valp) == CONSTRUCTOR
7697 : 1 : && CONSTRUCTOR_NELTS (*valp) == 0
7698 : 3 : && CONSTRUCTOR_NO_CLEARING (*valp))
7699 : : {
7700 : 1 : tree r = build_constructor (reftype, NULL);
7701 : 1 : CONSTRUCTOR_NO_CLEARING (r) = 1;
7702 : 1 : *valp = build2 (COMPLEX_EXPR, type, r, r);
7703 : : }
7704 : 32 : gcc_assert (TREE_CODE (*valp) == COMPLEX_EXPR);
7705 : 32 : ctors.safe_push (valp);
7706 : 32 : vec_safe_push (indexes, index);
7707 : 32 : valp = &TREE_OPERAND (*valp, TREE_CODE (index) == IMAGPART_EXPR);
7708 : 32 : gcc_checking_assert (refs->is_empty ());
7709 : : type = reftype;
7710 : 19632 : break;
7711 : : }
7712 : :
7713 : : /* If the value of object is already zero-initialized, any new ctors for
7714 : : subobjects will also be zero-initialized. Similarly with zeroing of
7715 : : padding bits. */
7716 : 8426545 : no_zero_init = CONSTRUCTOR_NO_CLEARING (*valp);
7717 : 8426545 : zero_padding_bits = CONSTRUCTOR_ZERO_PADDING_BITS (*valp);
7718 : :
7719 : 8426545 : if (code == RECORD_TYPE && is_empty_field (index))
7720 : : /* Don't build a sub-CONSTRUCTOR for an empty base or field, as they
7721 : : have no data and might have an offset lower than previously declared
7722 : : fields, which confuses the middle-end. The code below will notice
7723 : : that we don't have a CONSTRUCTOR for our inner target and just
7724 : : return init. */
7725 : : {
7726 : : empty_base = true;
7727 : : break;
7728 : : }
7729 : :
7730 : : /* If a union is zero-initialized, its first non-static named data member
7731 : : is zero-initialized (and therefore active). */
7732 : 8406913 : if (code == UNION_TYPE
7733 : 8406913 : && !no_zero_init
7734 : 8406913 : && CONSTRUCTOR_NELTS (*valp) == 0)
7735 : 75 : if (tree first = next_aggregate_field (TYPE_FIELDS (type)))
7736 : 75 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (*valp), first, NULL_TREE);
7737 : :
7738 : : /* Check for implicit change of active member for a union. */
7739 : 8406913 : if (code == UNION_TYPE
7740 : 149643 : && (CONSTRUCTOR_NELTS (*valp) == 0
7741 : 41446 : || CONSTRUCTOR_ELT (*valp, 0)->index != index)
7742 : : /* An INIT_EXPR of the last member in an access chain is always OK,
7743 : : but still check implicit change of members earlier on; see
7744 : : cpp2a/constexpr-union6.C. */
7745 : 8516683 : && !(TREE_CODE (t) == INIT_EXPR && refs->is_empty ()))
7746 : : {
7747 : 75895 : bool has_active_member = CONSTRUCTOR_NELTS (*valp) != 0;
7748 : 75895 : tree inner = strip_array_types (reftype);
7749 : :
7750 : 75895 : if (has_active_member && cxx_dialect < cxx20)
7751 : : {
7752 : 68 : if (!ctx->quiet)
7753 : 22 : error_at (cp_expr_loc_or_input_loc (t),
7754 : : "change of the active member of a union "
7755 : : "from %qD to %qD is not a constant expression "
7756 : : "before C++20",
7757 : 22 : CONSTRUCTOR_ELT (*valp, 0)->index,
7758 : : index);
7759 : 68 : *non_constant_p = true;
7760 : : }
7761 : 75827 : else if (!is_access_expr
7762 : 75827 : || (TREE_CODE (t) == MODIFY_EXPR
7763 : 1475 : && CLASS_TYPE_P (inner)
7764 : 15 : && !type_has_non_deleted_trivial_default_ctor (inner)))
7765 : : {
7766 : : /* Diagnose changing active union member after initialization
7767 : : without a valid member access expression, as described in
7768 : : [class.union.general] p5. */
7769 : 74349 : if (!ctx->quiet)
7770 : : {
7771 : 36 : auto_diagnostic_group d;
7772 : 36 : if (has_active_member)
7773 : 24 : error_at (cp_expr_loc_or_input_loc (t),
7774 : : "accessing %qD member instead of initialized "
7775 : : "%qD member in constant expression",
7776 : 24 : index, CONSTRUCTOR_ELT (*valp, 0)->index);
7777 : : else
7778 : 12 : error_at (cp_expr_loc_or_input_loc (t),
7779 : : "accessing uninitialized member %qD",
7780 : : index);
7781 : 36 : if (is_access_expr)
7782 : 3 : inform (DECL_SOURCE_LOCATION (index),
7783 : : "%qD does not implicitly begin its lifetime "
7784 : : "because %qT does not have a non-deleted "
7785 : : "trivial default constructor, use "
7786 : : "%<std::construct_at%> instead",
7787 : : index, inner);
7788 : : else
7789 : 33 : inform (DECL_SOURCE_LOCATION (index),
7790 : : "initializing %qD requires a member access "
7791 : : "expression as the left operand of the assignment",
7792 : : index);
7793 : 36 : }
7794 : 74349 : *non_constant_p = true;
7795 : : }
7796 : 1478 : else if (has_active_member && CONSTRUCTOR_NO_CLEARING (*valp))
7797 : : {
7798 : : /* Diagnose changing the active union member while the union
7799 : : is in the process of being initialized. */
7800 : 9 : if (!ctx->quiet)
7801 : 3 : error_at (cp_expr_loc_or_input_loc (t),
7802 : : "change of the active member of a union "
7803 : : "from %qD to %qD during initialization",
7804 : 3 : CONSTRUCTOR_ELT (*valp, 0)->index,
7805 : : index);
7806 : 9 : *non_constant_p = true;
7807 : : }
7808 : : no_zero_init = true;
7809 : : }
7810 : :
7811 : 8406913 : ctors.safe_push (valp);
7812 : 8406913 : vec_safe_push (indexes, index);
7813 : :
7814 : 8406913 : constructor_elt *cep
7815 : 8406913 : = get_or_insert_ctor_field (*valp, index);
7816 : 8406913 : index_pos_hints.safe_push (cep - CONSTRUCTOR_ELTS (*valp)->begin());
7817 : :
7818 : 8406913 : if (code == UNION_TYPE)
7819 : 149643 : activated_union_member_p = true;
7820 : :
7821 : 8406913 : valp = &cep->value;
7822 : 8406913 : type = reftype;
7823 : : }
7824 : :
7825 : : /* For initialization of an empty base, the original target will be
7826 : : *(base*)this, evaluation of which resolves to the object
7827 : : argument, which has the derived type rather than the base type. */
7828 : 48807340 : if (!empty_base && !(same_type_ignoring_top_level_qualifiers_p
7829 : 24393854 : (initialized_type (init), type)))
7830 : : {
7831 : 2786 : gcc_assert (is_empty_class (TREE_TYPE (target)));
7832 : : empty_base = true;
7833 : : }
7834 : :
7835 : : /* Detect modifying a constant object in constexpr evaluation.
7836 : : We have found a const object that is being modified. Figure out
7837 : : if we need to issue an error. Consider
7838 : :
7839 : : struct A {
7840 : : int n;
7841 : : constexpr A() : n(1) { n = 2; } // #1
7842 : : };
7843 : : struct B {
7844 : : const A a;
7845 : : constexpr B() { a.n = 3; } // #2
7846 : : };
7847 : : constexpr B b{};
7848 : :
7849 : : #1 is OK, since we're modifying an object under construction, but
7850 : : #2 is wrong, since "a" is const and has been fully constructed.
7851 : : To track it, we use the TREE_READONLY bit in the object's CONSTRUCTOR
7852 : : which means that the object is read-only. For the example above, the
7853 : : *ctors stack at the point of #2 will look like:
7854 : :
7855 : : ctors[0] = {.a={.n=2}} TREE_READONLY = 0
7856 : : ctors[1] = {.n=2} TREE_READONLY = 1
7857 : :
7858 : : and we're modifying "b.a", so we search the stack and see if the
7859 : : constructor for "b.a" has already run. */
7860 : 24413486 : if (const_object_being_modified)
7861 : : {
7862 : 5008 : bool fail = false;
7863 : 5008 : tree const_objtype
7864 : 5008 : = strip_array_types (TREE_TYPE (const_object_being_modified));
7865 : 5008 : if (!CLASS_TYPE_P (const_objtype))
7866 : : fail = true;
7867 : : else
7868 : : {
7869 : : /* [class.ctor]p5 "A constructor can be invoked for a const,
7870 : : volatile, or const volatile object. const and volatile
7871 : : semantics are not applied on an object under construction.
7872 : : They come into effect when the constructor for the most
7873 : : derived object ends." */
7874 : 14878 : for (tree *elt : ctors)
7875 : 5072 : if (same_type_ignoring_top_level_qualifiers_p
7876 : 5072 : (TREE_TYPE (const_object_being_modified), TREE_TYPE (*elt)))
7877 : : {
7878 : 4903 : fail = TREE_READONLY (*elt);
7879 : 4903 : break;
7880 : : }
7881 : : }
7882 : 4903 : if (fail)
7883 : : {
7884 : 198 : if (!ctx->quiet)
7885 : 63 : modifying_const_object_error (t, const_object_being_modified);
7886 : 198 : *non_constant_p = true;
7887 : 198 : return t;
7888 : : }
7889 : : }
7890 : :
7891 : 24413288 : if (!preeval)
7892 : : {
7893 : : /* We're handling an INIT_EXPR of class type, so the value of the
7894 : : initializer can depend on the object it's initializing. */
7895 : :
7896 : : /* Create a new CONSTRUCTOR in case evaluation of the initializer
7897 : : wants to modify it. */
7898 : 6546761 : if (*valp == NULL_TREE)
7899 : : {
7900 : 6429704 : *valp = build_constructor (type, NULL);
7901 : 6429704 : CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
7902 : 6429704 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
7903 : : }
7904 : 6546761 : new_ctx.ctor = empty_base ? NULL_TREE : *valp;
7905 : 6546761 : new_ctx.object = target;
7906 : : /* Avoid temporary materialization when initializing from a TARGET_EXPR.
7907 : : We don't need to mess with AGGR_EXPR_SLOT/VEC_INIT_EXPR_SLOT because
7908 : : expansion of those trees uses ctx instead. */
7909 : 6546761 : if (TREE_CODE (init) == TARGET_EXPR)
7910 : 1526810 : if (tree tinit = TARGET_EXPR_INITIAL (init))
7911 : 1526810 : init = tinit;
7912 : 6546761 : init = cxx_eval_constant_expression (&new_ctx, init, vc_prvalue,
7913 : : non_constant_p, overflow_p,
7914 : : jump_target);
7915 : 6546761 : if (*jump_target)
7916 : : return NULL_TREE;
7917 : : /* The hash table might have moved since the get earlier, and the
7918 : : initializer might have mutated the underlying CONSTRUCTORs, so we must
7919 : : recompute VALP. */
7920 : 6546742 : valp = ctx->global->get_value_ptr (object, TREE_CODE (t) == INIT_EXPR);
7921 : 7378200 : for (unsigned i = 0; i < vec_safe_length (indexes); i++)
7922 : : {
7923 : 831458 : ctors[i] = valp;
7924 : 831458 : constructor_elt *cep
7925 : 831458 : = get_or_insert_ctor_field (*valp, indexes[i], index_pos_hints[i]);
7926 : 831458 : valp = &cep->value;
7927 : : }
7928 : : }
7929 : :
7930 : 24413269 : if (*non_constant_p)
7931 : : return t;
7932 : :
7933 : : /* Don't share a CONSTRUCTOR that might be changed later. */
7934 : 23503717 : init = unshare_constructor (init);
7935 : :
7936 : 23503717 : gcc_checking_assert (!*valp || (same_type_ignoring_top_level_qualifiers_p
7937 : : (TREE_TYPE (*valp), type)));
7938 : 23503717 : if (empty_base)
7939 : : {
7940 : : /* Just evaluate the initializer and return, since there's no actual data
7941 : : to store, and we didn't build a CONSTRUCTOR. */
7942 : 21069 : if (!*valp)
7943 : : {
7944 : : /* But do make sure we have something in *valp. */
7945 : 0 : *valp = build_constructor (type, nullptr);
7946 : 0 : CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
7947 : 0 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
7948 : : }
7949 : : }
7950 : 23482648 : else if (*valp && TREE_CODE (*valp) == CONSTRUCTOR
7951 : 5736280 : && TREE_CODE (init) == CONSTRUCTOR)
7952 : : {
7953 : : /* An outer ctx->ctor might be pointing to *valp, so replace
7954 : : its contents. */
7955 : 1928254 : CONSTRUCTOR_ELTS (*valp) = CONSTRUCTOR_ELTS (init);
7956 : 1928254 : TREE_CONSTANT (*valp) = TREE_CONSTANT (init);
7957 : 1928254 : TREE_SIDE_EFFECTS (*valp) = TREE_SIDE_EFFECTS (init);
7958 : 5784762 : CONSTRUCTOR_NO_CLEARING (*valp)
7959 : 1928254 : = CONSTRUCTOR_NO_CLEARING (init);
7960 : 5784762 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp)
7961 : 1928254 : = CONSTRUCTOR_ZERO_PADDING_BITS (init);
7962 : : }
7963 : : else
7964 : 21554394 : *valp = init;
7965 : :
7966 : : /* After initialization, 'const' semantics apply to the value of the
7967 : : object. Make a note of this fact by marking the CONSTRUCTOR
7968 : : TREE_READONLY. */
7969 : 23503717 : if (TREE_CODE (t) == INIT_EXPR
7970 : 16989892 : && !empty_base
7971 : 16968823 : && TREE_CODE (*valp) == CONSTRUCTOR
7972 : 25386244 : && TYPE_READONLY (type))
7973 : : {
7974 : 17587 : if (INDIRECT_REF_P (target)
7975 : 32671 : && (is_this_parameter
7976 : 15084 : (tree_strip_nop_conversions (TREE_OPERAND (target, 0)))))
7977 : : /* We've just initialized '*this' (perhaps via the target
7978 : : constructor of a delegating constructor). Leave it up to the
7979 : : caller that set 'this' to set TREE_READONLY appropriately. */
7980 : 26 : gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
7981 : : (TREE_TYPE (target), type) || empty_base);
7982 : : else
7983 : 17561 : TREE_READONLY (*valp) = true;
7984 : : }
7985 : :
7986 : : /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing
7987 : : CONSTRUCTORs, if any. */
7988 : 23503717 : bool c = TREE_CONSTANT (init);
7989 : 23503717 : bool s = TREE_SIDE_EFFECTS (init);
7990 : 23503717 : if (!indexes->is_empty ())
7991 : : {
7992 : 5152327 : tree last = indexes->last ();
7993 : 5152327 : if (TREE_CODE (last) == REALPART_EXPR
7994 : 5152327 : || TREE_CODE (last) == IMAGPART_EXPR)
7995 : : {
7996 : : /* And canonicalize COMPLEX_EXPR into COMPLEX_CST if
7997 : : possible. */
7998 : 32 : tree *cexpr = ctors.last ();
7999 : 32 : if (tree c = const_binop (COMPLEX_EXPR, TREE_TYPE (*cexpr),
8000 : 32 : TREE_OPERAND (*cexpr, 0),
8001 : 32 : TREE_OPERAND (*cexpr, 1)))
8002 : 31 : *cexpr = c;
8003 : : else
8004 : : {
8005 : 2 : TREE_CONSTANT (*cexpr)
8006 : 1 : = (TREE_CONSTANT (TREE_OPERAND (*cexpr, 0))
8007 : 1 : & TREE_CONSTANT (TREE_OPERAND (*cexpr, 1)));
8008 : 2 : TREE_SIDE_EFFECTS (*cexpr)
8009 : 2 : = (TREE_SIDE_EFFECTS (TREE_OPERAND (*cexpr, 0))
8010 : 1 : | TREE_SIDE_EFFECTS (TREE_OPERAND (*cexpr, 1)));
8011 : : }
8012 : 32 : c = TREE_CONSTANT (*cexpr);
8013 : 32 : s = TREE_SIDE_EFFECTS (*cexpr);
8014 : : }
8015 : : }
8016 : 23503717 : if (!c || s || activated_union_member_p)
8017 : 9693285 : for (tree *elt : ctors)
8018 : : {
8019 : 1367726 : if (TREE_CODE (*elt) != CONSTRUCTOR)
8020 : 0 : continue;
8021 : 1367726 : if (!c)
8022 : 1072155 : TREE_CONSTANT (*elt) = false;
8023 : 1367726 : if (s)
8024 : 0 : TREE_SIDE_EFFECTS (*elt) = true;
8025 : : /* Clear CONSTRUCTOR_NO_CLEARING since we've activated a member of
8026 : : this union. */
8027 : 1367726 : if (TREE_CODE (TREE_TYPE (*elt)) == UNION_TYPE)
8028 : 75146 : CONSTRUCTOR_NO_CLEARING (*elt) = false;
8029 : : }
8030 : :
8031 : 23503717 : if (lval)
8032 : 20558037 : return target;
8033 : : else
8034 : : return init;
8035 : 24463349 : }
8036 : :
8037 : : /* Evaluate a ++ or -- expression. */
8038 : :
8039 : : static tree
8040 : 2259463 : cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t,
8041 : : value_cat lval,
8042 : : bool *non_constant_p, bool *overflow_p,
8043 : : tree *jump_target)
8044 : : {
8045 : 2259463 : enum tree_code code = TREE_CODE (t);
8046 : 2259463 : tree type = TREE_TYPE (t);
8047 : 2259463 : tree op = TREE_OPERAND (t, 0);
8048 : 2259463 : tree offset = TREE_OPERAND (t, 1);
8049 : 2259463 : gcc_assert (TREE_CONSTANT (offset));
8050 : :
8051 : : /* OFFSET is constant, but perhaps not constant enough. We need to
8052 : : e.g. bash FLOAT_EXPRs to REAL_CSTs. */
8053 : 2259463 : offset = fold_simple (offset);
8054 : :
8055 : : /* The operand as an lvalue. */
8056 : 2259463 : op = cxx_eval_constant_expression (ctx, op, vc_glvalue,
8057 : : non_constant_p, overflow_p,
8058 : : jump_target);
8059 : 2259463 : if (*jump_target)
8060 : : return NULL_TREE;
8061 : :
8062 : : /* The operand as an rvalue. */
8063 : 2259463 : tree val
8064 : 2259463 : = cxx_eval_constant_expression (ctx, op, vc_prvalue,
8065 : : non_constant_p, overflow_p,
8066 : : jump_target);
8067 : 2259463 : if (*jump_target)
8068 : : return NULL_TREE;
8069 : : /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
8070 : : a local array in a constexpr function. */
8071 : 2259463 : bool ptr = INDIRECT_TYPE_P (TREE_TYPE (val));
8072 : 1057066 : if (!ptr)
8073 : 1057066 : VERIFY_CONSTANT (val);
8074 : :
8075 : : /* The modified value. */
8076 : 2222521 : bool inc = (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR);
8077 : 2222521 : tree mod;
8078 : 2222521 : if (INDIRECT_TYPE_P (type))
8079 : : {
8080 : : /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
8081 : 1202397 : offset = convert_to_ptrofftype (offset);
8082 : 1202397 : if (!inc)
8083 : 95481 : offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset);
8084 : 1202397 : mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset);
8085 : : }
8086 : 1020124 : else if (c_promoting_integer_type_p (type)
8087 : 9990 : && !TYPE_UNSIGNED (type)
8088 : 1020193 : && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
8089 : : {
8090 : 69 : offset = fold_convert (integer_type_node, offset);
8091 : 69 : mod = fold_convert (integer_type_node, val);
8092 : 125 : tree t = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, integer_type_node,
8093 : : mod, offset);
8094 : 69 : mod = fold_convert (type, t);
8095 : 69 : if (TREE_OVERFLOW_P (mod) && !TREE_OVERFLOW_P (t))
8096 : 9 : TREE_OVERFLOW (mod) = false;
8097 : : }
8098 : : else
8099 : 1381563 : mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset);
8100 : 2222521 : if (!ptr)
8101 : 1020124 : VERIFY_CONSTANT (mod);
8102 : :
8103 : : /* Storing the modified value. */
8104 : 3556726 : tree store = build2_loc (cp_expr_loc_or_loc (t, input_location),
8105 : : MODIFY_EXPR, type, op, mod);
8106 : 2222521 : mod = cxx_eval_constant_expression (ctx, store, lval,
8107 : : non_constant_p, overflow_p,
8108 : : jump_target);
8109 : 2222521 : ggc_free (store);
8110 : 2222521 : if (*jump_target)
8111 : : return NULL_TREE;
8112 : 2222521 : if (*non_constant_p)
8113 : : return t;
8114 : :
8115 : : /* And the value of the expression. */
8116 : 2184192 : if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
8117 : : /* Prefix ops are lvalues, but the caller might want an rvalue;
8118 : : lval has already been taken into account in the store above. */
8119 : : return mod;
8120 : : else
8121 : : /* Postfix ops are rvalues. */
8122 : 482994 : return val;
8123 : : }
8124 : :
8125 : : /* Subroutine of cxx_eval_statement_list. Determine whether the statement
8126 : : STMT matches *jump_target. If we're looking for a case label and we see
8127 : : the default label, note it in ctx->css_state. */
8128 : :
8129 : : static bool
8130 : 365540 : label_matches (const constexpr_ctx *ctx, tree *jump_target, tree stmt)
8131 : : {
8132 : 365540 : switch (TREE_CODE (*jump_target))
8133 : : {
8134 : 15 : case LABEL_DECL:
8135 : 15 : if (TREE_CODE (stmt) == LABEL_EXPR
8136 : 15 : && LABEL_EXPR_LABEL (stmt) == *jump_target)
8137 : : return true;
8138 : : break;
8139 : :
8140 : 362120 : case INTEGER_CST:
8141 : 362120 : if (TREE_CODE (stmt) == CASE_LABEL_EXPR)
8142 : : {
8143 : 362120 : gcc_assert (ctx->css_state != NULL);
8144 : 362120 : if (!CASE_LOW (stmt))
8145 : : {
8146 : : /* default: should appear just once in a SWITCH_EXPR
8147 : : body (excluding nested SWITCH_EXPR). */
8148 : 55282 : gcc_assert (*ctx->css_state != css_default_seen);
8149 : : /* When evaluating SWITCH_EXPR body for the second time,
8150 : : return true for the default: label. */
8151 : 55282 : if (*ctx->css_state == css_default_processing)
8152 : : return true;
8153 : 27653 : *ctx->css_state = css_default_seen;
8154 : : }
8155 : 306838 : else if (CASE_HIGH (stmt))
8156 : : {
8157 : 20 : if (tree_int_cst_le (CASE_LOW (stmt), *jump_target)
8158 : 31 : && tree_int_cst_le (*jump_target, CASE_HIGH (stmt)))
8159 : : return true;
8160 : : }
8161 : 306818 : else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt)))
8162 : : return true;
8163 : : }
8164 : : break;
8165 : :
8166 : : case BREAK_STMT:
8167 : : case CONTINUE_STMT:
8168 : : /* These two are handled directly in cxx_eval_loop_expr by testing
8169 : : breaks (jump_target) or continues (jump_target). */
8170 : : break;
8171 : :
8172 : : case VAR_DECL:
8173 : : /* Uncaught exception. This is handled by TRY_BLOCK evaluation
8174 : : and other places by testing throws (jump_target). */
8175 : : break;
8176 : :
8177 : 0 : default:
8178 : 0 : gcc_unreachable ();
8179 : : }
8180 : : return false;
8181 : : }
8182 : :
8183 : : /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
8184 : : semantics, for switch, break, continue, and return. */
8185 : :
8186 : : static tree
8187 : 18600268 : cxx_eval_statement_list (const constexpr_ctx *ctx, tree t,
8188 : : bool *non_constant_p, bool *overflow_p,
8189 : : tree *jump_target)
8190 : : {
8191 : : /* In a statement-expression we want to return the last value.
8192 : : For empty statement expression return void_node. */
8193 : 18600268 : tree r = void_node;
8194 : 43750400 : for (tree_stmt_iterator i = tsi_start (t); !tsi_end_p (i); ++i)
8195 : : {
8196 : 34446514 : tree stmt = *i;
8197 : :
8198 : : /* We've found a continue, so skip everything until we reach
8199 : : the label its jumping to. */
8200 : 34446514 : if (continues (jump_target))
8201 : : {
8202 : 3420 : if (label_matches (ctx, jump_target, stmt))
8203 : : /* Found it. */
8204 : 3 : *jump_target = NULL_TREE;
8205 : : else
8206 : 3417 : continue;
8207 : : }
8208 : 34443097 : if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT)
8209 : 5967597 : continue;
8210 : :
8211 : 28475500 : value_cat lval = vc_discard;
8212 : : /* The result of a statement-expression is not wrapped in EXPR_STMT. */
8213 : 28475500 : if (tsi_one_before_end_p (i) && TREE_CODE (stmt) != EXPR_STMT)
8214 : : lval = vc_prvalue;
8215 : :
8216 : 28475500 : r = cxx_eval_constant_expression (ctx, stmt, lval,
8217 : : non_constant_p, overflow_p,
8218 : : jump_target);
8219 : 28475500 : if (*non_constant_p)
8220 : : break;
8221 : 22226470 : if (returns (jump_target)
8222 : 19201457 : || breaks (jump_target)
8223 : 25150132 : || throws (jump_target))
8224 : : break;
8225 : : }
8226 : 18600268 : return r;
8227 : : }
8228 : :
8229 : : /* Evaluate a LOOP_EXPR for side-effects. Handles break and return
8230 : : semantics; continue semantics are covered by cxx_eval_statement_list. */
8231 : :
8232 : : static tree
8233 : 774664 : cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t,
8234 : : bool *non_constant_p, bool *overflow_p,
8235 : : tree *jump_target)
8236 : : {
8237 : 774664 : tree body, cond = NULL_TREE, expr = NULL_TREE;
8238 : 774664 : tree cond_prep = NULL_TREE, cond_cleanup = NULL_TREE;
8239 : 774664 : unsigned cond_cleanup_depth = 0;
8240 : 774664 : int count = 0;
8241 : 774664 : switch (TREE_CODE (t))
8242 : : {
8243 : 82 : case LOOP_EXPR:
8244 : 82 : body = LOOP_EXPR_BODY (t);
8245 : 82 : break;
8246 : 605826 : case DO_STMT:
8247 : 605826 : body = DO_BODY (t);
8248 : 605826 : cond = DO_COND (t);
8249 : 605826 : break;
8250 : 74042 : case WHILE_STMT:
8251 : 74042 : body = WHILE_BODY (t);
8252 : 74042 : cond = WHILE_COND (t);
8253 : 74042 : cond_prep = WHILE_COND_PREP (t);
8254 : 74042 : cond_cleanup = WHILE_COND_CLEANUP (t);
8255 : 74042 : count = -1;
8256 : 74042 : break;
8257 : 94714 : case FOR_STMT:
8258 : 94714 : if (FOR_INIT_STMT (t))
8259 : 0 : cxx_eval_constant_expression (ctx, FOR_INIT_STMT (t), vc_discard,
8260 : : non_constant_p, overflow_p, jump_target);
8261 : 94714 : if (*non_constant_p)
8262 : : return NULL_TREE;
8263 : 94714 : body = FOR_BODY (t);
8264 : 94714 : cond = FOR_COND (t);
8265 : 94714 : expr = FOR_EXPR (t);
8266 : 94714 : cond_prep = FOR_COND_PREP (t);
8267 : 94714 : cond_cleanup = FOR_COND_CLEANUP (t);
8268 : 94714 : count = -1;
8269 : 94714 : break;
8270 : 0 : default:
8271 : 0 : gcc_unreachable ();
8272 : : }
8273 : 774664 : if (cond_prep)
8274 : 12 : gcc_assert (TREE_CODE (cond_prep) == BIND_EXPR);
8275 : 8651740 : auto cleanup_cond = [&] {
8276 : : /* Clean up the condition variable after each iteration. */
8277 : 7877076 : if (cond_cleanup_depth && !*non_constant_p)
8278 : : {
8279 : 105 : auto_vec<tree, 4> cleanups (cond_cleanup_depth);
8280 : 105 : tree s = BIND_EXPR_BODY (cond_prep);
8281 : 105 : unsigned i;
8282 : 210 : for (i = cond_cleanup_depth; i; --i)
8283 : : {
8284 : 105 : tree_stmt_iterator iter = tsi_last (s);
8285 : 105 : s = tsi_stmt (iter);
8286 : 105 : cleanups.quick_push (CLEANUP_EXPR (s));
8287 : 105 : s = CLEANUP_BODY (s);
8288 : : }
8289 : 105 : tree c;
8290 : 420 : FOR_EACH_VEC_ELT_REVERSE (cleanups, i, c)
8291 : 105 : cxx_eval_constant_expression (ctx, c, vc_discard, non_constant_p,
8292 : : overflow_p, jump_target);
8293 : 105 : }
8294 : 7877076 : if (cond_prep)
8295 : 111 : for (tree decl = BIND_EXPR_VARS (cond_prep);
8296 : 222 : decl; decl = DECL_CHAIN (decl))
8297 : 111 : destroy_value_checked (ctx, decl, non_constant_p);
8298 : 774664 : };
8299 : 7271782 : do
8300 : : {
8301 : 7271782 : if (count != -1)
8302 : : {
8303 : 7103026 : if (body)
8304 : 7103026 : cxx_eval_constant_expression (ctx, body, vc_discard,
8305 : : non_constant_p, overflow_p,
8306 : : jump_target);
8307 : 7103026 : if (breaks (jump_target))
8308 : : {
8309 : 614 : *jump_target = NULL_TREE;
8310 : 614 : break;
8311 : : }
8312 : :
8313 : 7102412 : if (TREE_CODE (t) != LOOP_EXPR && continues (jump_target))
8314 : 694 : *jump_target = NULL_TREE;
8315 : :
8316 : 7102412 : if (expr)
8317 : 1131093 : cxx_eval_constant_expression (ctx, expr, vc_prvalue,
8318 : : non_constant_p, overflow_p,
8319 : : jump_target);
8320 : 7102412 : cleanup_cond ();
8321 : : }
8322 : :
8323 : 7271168 : if (cond_prep)
8324 : : {
8325 : 111 : for (tree decl = BIND_EXPR_VARS (cond_prep);
8326 : 222 : decl; decl = DECL_CHAIN (decl))
8327 : 111 : ctx->global->clear_value (decl);
8328 : 111 : if (cond_cleanup)
8329 : : {
8330 : : /* If COND_CLEANUP is non-NULL, we need to evaluate DEPTH
8331 : : nested STATEMENT_LISTs from inside of BIND_EXPR_BODY,
8332 : : but defer the evaluation of CLEANUP_EXPRs of CLEANUP_STMT
8333 : : at the end of those STATEMENT_LISTs. */
8334 : 111 : cond_cleanup_depth = 0;
8335 : 111 : tree s = BIND_EXPR_BODY (cond_prep);
8336 : 111 : for (unsigned depth = tree_to_uhwi (cond_cleanup);
8337 : 222 : depth; --depth)
8338 : : {
8339 : 111 : for (tree_stmt_iterator i = tsi_start (s);
8340 : 321 : !tsi_end_p (i); ++i)
8341 : : {
8342 : 321 : tree stmt = *i;
8343 : 321 : if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT)
8344 : 0 : continue;
8345 : 321 : if (tsi_one_before_end_p (i))
8346 : : {
8347 : : /* The last statement in the STATEMENT_LIST
8348 : : has to be a CLEANUP_STMT (verified in
8349 : : finish_loop_cond_prep). We want to
8350 : : evaluate just its CLEANUP_BODY part but not
8351 : : CLEANUP_EXPR part just yet. */
8352 : 105 : gcc_assert (TREE_CODE (stmt) == CLEANUP_STMT);
8353 : : /* If the CLEANUP_STMT is not actually to be
8354 : : evaluated, don't increment cond_cleanup_depth
8355 : : so that we don't evaluate the CLEANUP_EXPR
8356 : : for it later either. */
8357 : 105 : if (*jump_target)
8358 : : {
8359 : : depth = 1;
8360 : : break;
8361 : : }
8362 : 105 : ++cond_cleanup_depth;
8363 : : /* If not in the innermost one, next iteration
8364 : : will handle CLEANUP_BODY similarly. */
8365 : 105 : if (depth > 1)
8366 : : {
8367 : 0 : s = CLEANUP_BODY (stmt);
8368 : 0 : break;
8369 : : }
8370 : : /* The innermost one can be evaluated normally. */
8371 : 105 : cxx_eval_constant_expression (ctx,
8372 : 105 : CLEANUP_BODY (stmt),
8373 : : vc_discard,
8374 : : non_constant_p,
8375 : : overflow_p,
8376 : : jump_target);
8377 : 105 : break;
8378 : : }
8379 : : /* And so should be evaluated statements which aren't
8380 : : last in the STATEMENT_LIST. */
8381 : 216 : cxx_eval_constant_expression (ctx, stmt, vc_discard,
8382 : : non_constant_p, overflow_p,
8383 : : jump_target);
8384 : 216 : if (*non_constant_p
8385 : 210 : || returns (jump_target)
8386 : 210 : || breaks (jump_target)
8387 : 210 : || continues (jump_target)
8388 : 531 : || throws (jump_target))
8389 : : {
8390 : : depth = 1;
8391 : : break;
8392 : : }
8393 : : }
8394 : : }
8395 : : }
8396 : : else
8397 : 0 : cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (cond_prep),
8398 : : vc_discard, non_constant_p,
8399 : : overflow_p, jump_target);
8400 : : }
8401 : :
8402 : 7271168 : if (cond)
8403 : : {
8404 : 7270124 : tree res
8405 : 7270124 : = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
8406 : : non_constant_p, overflow_p,
8407 : : jump_target);
8408 : 7270124 : if (res)
8409 : : {
8410 : 7237269 : if (verify_constant (res, ctx->quiet, non_constant_p,
8411 : : overflow_p))
8412 : : break;
8413 : 7029454 : if (integer_zerop (res))
8414 : : break;
8415 : : }
8416 : : else
8417 : 32855 : gcc_assert (*jump_target);
8418 : : }
8419 : :
8420 : 6530266 : if (++count >= constexpr_loop_limit)
8421 : : {
8422 : 28 : if (!ctx->quiet)
8423 : 9 : error_at (cp_expr_loc_or_input_loc (t),
8424 : : "%<constexpr%> loop iteration count exceeds limit of %d "
8425 : : "(use %<-fconstexpr-loop-limit=%> to increase the limit)",
8426 : : constexpr_loop_limit);
8427 : 28 : *non_constant_p = true;
8428 : 28 : break;
8429 : : }
8430 : : }
8431 : 19557758 : while (!returns (jump_target)
8432 : 6497282 : && !breaks (jump_target)
8433 : 6497282 : && !continues (jump_target)
8434 : 6497375 : && (!switches (jump_target) || count == 0)
8435 : 6497252 : && !throws (jump_target)
8436 : 13060610 : && !*non_constant_p);
8437 : :
8438 : 774664 : cleanup_cond ();
8439 : :
8440 : 774664 : return NULL_TREE;
8441 : : }
8442 : :
8443 : : /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
8444 : : semantics. */
8445 : :
8446 : : static tree
8447 : 35444 : cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t,
8448 : : bool *non_constant_p, bool *overflow_p,
8449 : : tree *jump_target)
8450 : : {
8451 : 35444 : tree cond
8452 : 35444 : = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_COND (t) : SWITCH_COND (t);
8453 : 35444 : cond = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
8454 : : non_constant_p, overflow_p,
8455 : : jump_target);
8456 : 35444 : if (*jump_target)
8457 : : return NULL_TREE;
8458 : 35444 : VERIFY_CONSTANT (cond);
8459 : 35126 : if (TREE_CODE (cond) != INTEGER_CST)
8460 : : {
8461 : : /* If the condition doesn't reduce to an INTEGER_CST it isn't a usable
8462 : : switch condition even if it's constant enough for other things
8463 : : (c++/113545). */
8464 : 0 : gcc_checking_assert (ctx->quiet);
8465 : 0 : *non_constant_p = true;
8466 : 0 : return t;
8467 : : }
8468 : :
8469 : 35126 : *jump_target = cond;
8470 : :
8471 : 35126 : tree body
8472 : 35126 : = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_BODY (t) : SWITCH_BODY (t);
8473 : 35126 : constexpr_ctx new_ctx = *ctx;
8474 : 35126 : constexpr_switch_state css = css_default_not_seen;
8475 : 35126 : new_ctx.css_state = &css;
8476 : 35126 : cxx_eval_constant_expression (&new_ctx, body, vc_discard,
8477 : : non_constant_p, overflow_p, jump_target);
8478 : 62799 : if (switches (jump_target) && css == css_default_seen)
8479 : : {
8480 : : /* If the SWITCH_EXPR body has default: label, process it once again,
8481 : : this time instructing label_matches to return true for default:
8482 : : label on switches (jump_target). */
8483 : 27629 : css = css_default_processing;
8484 : 27629 : cxx_eval_constant_expression (&new_ctx, body, vc_discard,
8485 : : non_constant_p, overflow_p, jump_target);
8486 : : }
8487 : 35126 : if (breaks (jump_target) || switches (jump_target))
8488 : 21391 : *jump_target = NULL_TREE;
8489 : : return NULL_TREE;
8490 : : }
8491 : :
8492 : : /* Find the object of TYPE under initialization in CTX. */
8493 : :
8494 : : static tree
8495 : 16631 : lookup_placeholder (const constexpr_ctx *ctx, value_cat lval, tree type)
8496 : : {
8497 : 16631 : if (!ctx)
8498 : : return NULL_TREE;
8499 : :
8500 : : /* Prefer the outermost matching object, but don't cross
8501 : : CONSTRUCTOR_PLACEHOLDER_BOUNDARY constructors. */
8502 : 16299 : if (ctx->ctor && !CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor))
8503 : 510 : if (tree outer_ob = lookup_placeholder (ctx->parent, lval, type))
8504 : : return outer_ob;
8505 : :
8506 : : /* We could use ctx->object unconditionally, but using ctx->ctor when we
8507 : : can is a minor optimization. */
8508 : 16121 : if (!lval && ctx->ctor && same_type_p (TREE_TYPE (ctx->ctor), type))
8509 : 300 : return ctx->ctor;
8510 : :
8511 : 15821 : if (!ctx->object)
8512 : : return NULL_TREE;
8513 : :
8514 : : /* Since an object cannot have a field of its own type, we can search outward
8515 : : from ctx->object to find the unique containing object of TYPE. */
8516 : : tree ob = ctx->object;
8517 : 15812 : while (ob)
8518 : : {
8519 : 15812 : if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob), type))
8520 : : break;
8521 : 223 : if (handled_component_p (ob))
8522 : 221 : ob = TREE_OPERAND (ob, 0);
8523 : : else
8524 : : ob = NULL_TREE;
8525 : : }
8526 : :
8527 : : return ob;
8528 : : }
8529 : :
8530 : : /* Complain about an attempt to evaluate inline assembly. If FUNDEF_P is
8531 : : true, we're checking a constexpr function body. */
8532 : :
8533 : : static void
8534 : 31 : inline_asm_in_constexpr_error (location_t loc, bool fundef_p)
8535 : : {
8536 : 31 : auto_diagnostic_group d;
8537 : 31 : if (constexpr_error (loc, fundef_p, "inline assembly is not a "
8538 : : "constant expression"))
8539 : 31 : inform (loc, "only unevaluated inline assembly is allowed in a "
8540 : : "%<constexpr%> function in C++20");
8541 : 31 : }
8542 : :
8543 : : /* We're getting the constant value of DECL in a manifestly constant-evaluated
8544 : : context; maybe complain about that. */
8545 : :
8546 : : static void
8547 : 56425333 : maybe_warn_about_constant_value (location_t loc, tree decl)
8548 : : {
8549 : 56425333 : static bool explained = false;
8550 : 56425333 : if (cxx_dialect >= cxx17
8551 : 56273750 : && warn_interference_size
8552 : 56273750 : && !OPTION_SET_P (param_destruct_interfere_size)
8553 : 56273750 : && DECL_CONTEXT (decl) == std_node
8554 : 6837211 : && DECL_NAME (decl)
8555 : 6837205 : && id_equal (DECL_NAME (decl), "hardware_destructive_interference_size")
8556 : 12 : && (LOCATION_FILE (input_location) != main_input_filename
8557 : 6 : || module_exporting_p ())
8558 : 56425336 : && warning_at (loc, OPT_Winterference_size, "use of %qD", decl)
8559 : 56425342 : && !explained)
8560 : : {
8561 : 6 : explained = true;
8562 : 6 : inform (loc, "its value can vary between compiler versions or "
8563 : : "with different %<-mtune%> or %<-mcpu%> flags");
8564 : 6 : inform (loc, "if this use is part of a public ABI, change it to "
8565 : : "instead use a constant variable you define");
8566 : 6 : inform (loc, "the default value for the current CPU tuning "
8567 : : "is %d bytes", param_destruct_interfere_size);
8568 : 6 : inform (loc, "you can stabilize this value with %<--param "
8569 : : "hardware_destructive_interference_size=%d%>, or disable "
8570 : : "this warning with %<-Wno-interference-size%>",
8571 : : param_destruct_interfere_size);
8572 : : }
8573 : 56425333 : }
8574 : :
8575 : : /* For element type ELT_TYPE, return the appropriate type of the heap object
8576 : : containing such element(s). COOKIE_SIZE is NULL or the size of cookie
8577 : : in bytes. If COOKIE_SIZE is NULL, return array type
8578 : : ELT_TYPE[FULL_SIZE / sizeof(ELT_TYPE)], otherwise return
8579 : : struct { size_t[COOKIE_SIZE/sizeof(size_t)]; ELT_TYPE[N]; }
8580 : : where N is computed such that the size of the struct fits into FULL_SIZE.
8581 : : If ARG_SIZE is non-NULL, it is the first argument to the new operator.
8582 : : It should be passed if ELT_TYPE is zero sized type in which case FULL_SIZE
8583 : : will be also 0 and so it is not possible to determine the actual array
8584 : : size. CTX, NON_CONSTANT_P and OVERFLOW_P are used during constant
8585 : : expression evaluation of subexpressions of ARG_SIZE. */
8586 : :
8587 : : static tree
8588 : 5510 : build_new_constexpr_heap_type (const constexpr_ctx *ctx, tree elt_type,
8589 : : tree cookie_size, tree full_size, tree arg_size,
8590 : : bool *non_constant_p, bool *overflow_p,
8591 : : tree *jump_target)
8592 : : {
8593 : 5510 : gcc_assert (cookie_size == NULL_TREE || tree_fits_uhwi_p (cookie_size));
8594 : 5510 : gcc_assert (tree_fits_uhwi_p (full_size));
8595 : 5510 : unsigned HOST_WIDE_INT csz = cookie_size ? tree_to_uhwi (cookie_size) : 0;
8596 : 5510 : if (arg_size)
8597 : : {
8598 : 9 : STRIP_NOPS (arg_size);
8599 : 9 : if (cookie_size)
8600 : : {
8601 : 0 : if (TREE_CODE (arg_size) != PLUS_EXPR)
8602 : : arg_size = NULL_TREE;
8603 : 0 : else if (TREE_CODE (TREE_OPERAND (arg_size, 0)) == INTEGER_CST
8604 : 0 : && tree_int_cst_equal (cookie_size,
8605 : 0 : TREE_OPERAND (arg_size, 0)))
8606 : : {
8607 : 0 : arg_size = TREE_OPERAND (arg_size, 1);
8608 : 0 : STRIP_NOPS (arg_size);
8609 : : }
8610 : 0 : else if (TREE_CODE (TREE_OPERAND (arg_size, 1)) == INTEGER_CST
8611 : 0 : && tree_int_cst_equal (cookie_size,
8612 : 0 : TREE_OPERAND (arg_size, 1)))
8613 : : {
8614 : 0 : arg_size = TREE_OPERAND (arg_size, 0);
8615 : 0 : STRIP_NOPS (arg_size);
8616 : : }
8617 : : else
8618 : : arg_size = NULL_TREE;
8619 : : }
8620 : 9 : if (arg_size && TREE_CODE (arg_size) == MULT_EXPR)
8621 : : {
8622 : 9 : tree op0 = TREE_OPERAND (arg_size, 0);
8623 : 9 : tree op1 = TREE_OPERAND (arg_size, 1);
8624 : 9 : if (integer_zerop (op0))
8625 : 9 : arg_size
8626 : 9 : = cxx_eval_constant_expression (ctx, op1, vc_prvalue,
8627 : : non_constant_p, overflow_p,
8628 : : jump_target);
8629 : 0 : else if (integer_zerop (op1))
8630 : 0 : arg_size
8631 : 0 : = cxx_eval_constant_expression (ctx, op0, vc_prvalue,
8632 : : non_constant_p, overflow_p,
8633 : : jump_target);
8634 : : else
8635 : : arg_size = NULL_TREE;
8636 : 9 : if (*jump_target)
8637 : : return NULL_TREE;
8638 : : }
8639 : : else
8640 : : arg_size = NULL_TREE;
8641 : : }
8642 : :
8643 : 9 : unsigned HOST_WIDE_INT fsz = tree_to_uhwi (arg_size ? arg_size : full_size);
8644 : 5510 : if (!arg_size)
8645 : : {
8646 : 5501 : unsigned HOST_WIDE_INT esz = int_size_in_bytes (elt_type);
8647 : 5501 : gcc_assert (fsz >= csz);
8648 : 5501 : fsz -= csz;
8649 : 5501 : if (esz)
8650 : 5501 : fsz /= esz;
8651 : : }
8652 : 5510 : tree itype2 = build_index_type (size_int (fsz - 1));
8653 : 5510 : if (!cookie_size)
8654 : 5505 : return build_cplus_array_type (elt_type, itype2);
8655 : 5 : return build_new_constexpr_heap_type (elt_type, cookie_size, itype2);
8656 : : }
8657 : :
8658 : : /* Handle the case when a cleanup of some expression throws. JMP_TARGET
8659 : : indicates whether the cleanup threw or not, *JUMP_TARGET indicates whether
8660 : : the expression which needed the cleanup threw. If both threw, diagnose
8661 : : it and return NULL, otherwise return R. If only the cleanup threw, set
8662 : : *JUMP_TARGET to the exception object from the cleanup. */
8663 : :
8664 : : static tree
8665 : 57496 : merge_jump_target (location_t loc, const constexpr_ctx *ctx, tree r,
8666 : : bool *non_constant_p, tree *jump_target, tree jmp_target)
8667 : : {
8668 : 57497 : if (!throws (&jmp_target))
8669 : : return r;
8670 : 7 : if (throws (jump_target))
8671 : : {
8672 : : /* [except.throw]/9 - If the exception handling mechanism
8673 : : handling an uncaught exception directly invokes a function
8674 : : that exits via an exception, the function std::terminate is
8675 : : invoked. */
8676 : 6 : if (!ctx->quiet)
8677 : : {
8678 : 2 : auto_diagnostic_group d;
8679 : 2 : diagnose_std_terminate (loc, ctx, *jump_target);
8680 : 2 : inform (loc, "destructor exited with an exception");
8681 : 2 : }
8682 : 6 : *non_constant_p = true;
8683 : 6 : *jump_target = NULL_TREE;
8684 : 6 : return NULL_TREE;
8685 : : }
8686 : 1 : *jump_target = jmp_target;
8687 : 1 : return r;
8688 : : }
8689 : :
8690 : : /* Attempt to reduce the expression T to a constant value.
8691 : : On failure, issue diagnostic and return error_mark_node. */
8692 : : /* FIXME unify with c_fully_fold */
8693 : : /* FIXME overflow_p is too global */
8694 : :
8695 : : static tree
8696 : 1159783830 : cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
8697 : : value_cat lval,
8698 : : bool *non_constant_p, bool *overflow_p,
8699 : : tree *jump_target)
8700 : : {
8701 : 1159783830 : if (*jump_target)
8702 : : {
8703 : : /* If we are jumping, ignore all statements/expressions except those
8704 : : that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */
8705 : 1351248 : switch (TREE_CODE (t))
8706 : : {
8707 : : case BIND_EXPR:
8708 : : case STATEMENT_LIST:
8709 : : case LOOP_EXPR:
8710 : : case COND_EXPR:
8711 : : case IF_STMT:
8712 : : case DO_STMT:
8713 : : case WHILE_STMT:
8714 : : case FOR_STMT:
8715 : : break;
8716 : 362120 : case LABEL_EXPR:
8717 : 362120 : case CASE_LABEL_EXPR:
8718 : 362120 : if (label_matches (ctx, jump_target, t))
8719 : : /* Found it. */
8720 : 35082 : *jump_target = NULL_TREE;
8721 : 362120 : return NULL_TREE;
8722 : : default:
8723 : : return NULL_TREE;
8724 : : }
8725 : : }
8726 : 1158652702 : if (error_operand_p (t))
8727 : : {
8728 : 133 : *non_constant_p = true;
8729 : 133 : return t;
8730 : : }
8731 : :
8732 : : /* Change the input location to the currently processed expression for
8733 : : better error messages when a subexpression has no location. */
8734 : 1158652569 : location_t loc = cp_expr_loc_or_input_loc (t);
8735 : 2317299744 : iloc_sentinel sentinel (loc);
8736 : :
8737 : 1158652569 : STRIP_ANY_LOCATION_WRAPPER (t);
8738 : :
8739 : 1158652569 : if (CONSTANT_CLASS_P (t))
8740 : : {
8741 : 250666126 : if (TREE_OVERFLOW (t))
8742 : : {
8743 : 62 : if (!ctx->quiet)
8744 : 15 : permerror (input_location, "overflow in constant expression");
8745 : 62 : if (!flag_permissive || ctx->quiet)
8746 : 59 : *overflow_p = true;
8747 : : }
8748 : :
8749 : 250666126 : if (TREE_CODE (t) == INTEGER_CST
8750 : 242155097 : && TYPE_PTR_P (TREE_TYPE (t))
8751 : : /* INTEGER_CST with pointer-to-method type is only used
8752 : : for a virtual method in a pointer to member function.
8753 : : Don't reject those. */
8754 : 739086 : && TREE_CODE (TREE_TYPE (TREE_TYPE (t))) != METHOD_TYPE
8755 : 251404910 : && !integer_zerop (t))
8756 : : {
8757 : 6 : if (!ctx->quiet)
8758 : 0 : error ("value %qE of type %qT is not a constant expression",
8759 : 0 : t, TREE_TYPE (t));
8760 : 6 : *non_constant_p = true;
8761 : : }
8762 : :
8763 : 250666126 : return t;
8764 : : }
8765 : :
8766 : : /* Avoid excessively long constexpr evaluations. */
8767 : 907986443 : if (++ctx->global->constexpr_ops_count >= constexpr_ops_limit)
8768 : : {
8769 : 9 : if (!ctx->quiet)
8770 : 3 : error_at (loc,
8771 : : "%<constexpr%> evaluation operation count exceeds limit of "
8772 : : "%wd (use %<-fconstexpr-ops-limit=%> to increase the limit)",
8773 : : constexpr_ops_limit);
8774 : 9 : ctx->global->constexpr_ops_count = INTTYPE_MINIMUM (HOST_WIDE_INT);
8775 : 9 : *non_constant_p = true;
8776 : 9 : return t;
8777 : : }
8778 : :
8779 : 907986434 : constexpr_ctx new_ctx;
8780 : 907986434 : tree r = t;
8781 : :
8782 : 907986434 : tree_code tcode = TREE_CODE (t);
8783 : 907986434 : switch (tcode)
8784 : : {
8785 : 13899899 : case RESULT_DECL:
8786 : 13899899 : if (lval)
8787 : : return t;
8788 : : /* We ask for an rvalue for the RESULT_DECL when indirecting
8789 : : through an invisible reference, or in named return value
8790 : : optimization. */
8791 : 35507 : if (tree v = ctx->global->get_value (t))
8792 : : return v;
8793 : : else
8794 : : {
8795 : 3 : if (!ctx->quiet)
8796 : 0 : error ("%qE is not a constant expression", t);
8797 : 3 : *non_constant_p = true;
8798 : : }
8799 : 3 : break;
8800 : :
8801 : 127923512 : case VAR_DECL:
8802 : 127923512 : if (DECL_HAS_VALUE_EXPR_P (t))
8803 : : {
8804 : 1212183 : if (is_normal_capture_proxy (t)
8805 : 1212183 : && current_function_decl == DECL_CONTEXT (t))
8806 : : {
8807 : : /* Function parms aren't constexpr within the function
8808 : : definition, so don't try to look at the closure. But if the
8809 : : captured variable is constant, try to evaluate it directly. */
8810 : 379208 : r = DECL_CAPTURED_VARIABLE (t);
8811 : : }
8812 : : else
8813 : 832975 : r = DECL_VALUE_EXPR (t);
8814 : :
8815 : 1212183 : tree type = TREE_TYPE (t);
8816 : 1212183 : if (TYPE_REF_P (type) != TYPE_REF_P (TREE_TYPE (r)))
8817 : : {
8818 : : /* Adjust r to match the reference-ness of t. */
8819 : 239481 : if (TYPE_REF_P (type))
8820 : 239478 : r = build_address (r);
8821 : : else
8822 : 3 : r = convert_from_reference (r);
8823 : : }
8824 : 1212183 : return cxx_eval_constant_expression (ctx, r, lval, non_constant_p,
8825 : 1212183 : overflow_p, jump_target);
8826 : : }
8827 : : /* fall through */
8828 : 134331946 : case CONST_DECL:
8829 : : /* We used to not check lval for CONST_DECL, but darwin.cc uses
8830 : : CONST_DECL for aggregate constants. */
8831 : 134331946 : if (lval)
8832 : : return t;
8833 : 106309145 : else if (t == ctx->object)
8834 : 523175 : return ctx->ctor;
8835 : 105785970 : if (VAR_P (t))
8836 : : {
8837 : 98165353 : if (tree v = ctx->global->get_value (t))
8838 : : {
8839 : : r = v;
8840 : : break;
8841 : : }
8842 : 86181160 : if (ctx->global->is_outside_lifetime (t))
8843 : : {
8844 : 96 : if (!ctx->quiet)
8845 : 28 : outside_lifetime_error (loc, t);
8846 : 96 : *non_constant_p = true;
8847 : 96 : break;
8848 : : }
8849 : : }
8850 : 93801681 : if (ctx->manifestly_const_eval == mce_true)
8851 : 56425333 : maybe_warn_about_constant_value (loc, t);
8852 : 93801681 : if (COMPLETE_TYPE_P (TREE_TYPE (t))
8853 : 93801681 : && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
8854 : : {
8855 : : /* If the class is empty, we aren't actually loading anything. */
8856 : 79334 : r = build_constructor (TREE_TYPE (t), NULL);
8857 : 79334 : TREE_CONSTANT (r) = true;
8858 : : }
8859 : 93722347 : else if (ctx->strict)
8860 : 93448222 : r = decl_really_constant_value (t, /*unshare_p=*/false);
8861 : : else
8862 : 274125 : r = decl_constant_value (t, /*unshare_p=*/false);
8863 : 93798984 : if (TREE_CODE (r) == TARGET_EXPR
8864 : 93798984 : && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR)
8865 : 0 : r = TARGET_EXPR_INITIAL (r);
8866 : 93798984 : if (DECL_P (r)
8867 : : /* P2280 allows references to unknown. */
8868 : 93943031 : && !(p2280_active_p (ctx) && VAR_P (t) && TYPE_REF_P (TREE_TYPE (t))))
8869 : : {
8870 : 27015968 : if (!ctx->quiet)
8871 : 152 : non_const_var_error (loc, r, /*fundef_p*/false);
8872 : 27015968 : *non_constant_p = true;
8873 : : }
8874 : : break;
8875 : :
8876 : : case DEBUG_BEGIN_STMT:
8877 : : /* ??? It might be nice to retain this information somehow, so
8878 : : as to be able to step into a constexpr function call. */
8879 : : /* Fall through. */
8880 : :
8881 : : case FUNCTION_DECL:
8882 : : case TEMPLATE_DECL:
8883 : : case LABEL_DECL:
8884 : : case LABEL_EXPR:
8885 : : case CASE_LABEL_EXPR:
8886 : : case PREDICT_EXPR:
8887 : : case OMP_DECLARE_MAPPER:
8888 : : return t;
8889 : :
8890 : 103958169 : case PARM_DECL:
8891 : 103958169 : if (lval && !TYPE_REF_P (TREE_TYPE (t)))
8892 : : {
8893 : : /* glvalue use. */
8894 : 5260540 : if (TREE_ADDRESSABLE (TREE_TYPE (t)))
8895 : 113539 : if (tree v = ctx->global->get_value (t))
8896 : 721961058 : r = v;
8897 : : }
8898 : 98697629 : else if (tree v = ctx->global->get_value (t))
8899 : : {
8900 : 34325305 : r = v;
8901 : 34325305 : if (TREE_ADDRESSABLE (TREE_TYPE (t)))
8902 : 4717 : r = cxx_eval_constant_expression (ctx, r, vc_prvalue,
8903 : : non_constant_p, overflow_p,
8904 : : jump_target);
8905 : 34325305 : if (*jump_target)
8906 : : return NULL_TREE;
8907 : : }
8908 : 64372324 : else if (lval)
8909 : : /* Defer in case this is only used for its type. */;
8910 : 64372324 : else if (ctx->global->is_outside_lifetime (t))
8911 : : {
8912 : 18 : if (!ctx->quiet)
8913 : 6 : outside_lifetime_error (loc, t);
8914 : 18 : *non_constant_p = true;
8915 : 18 : break;
8916 : : }
8917 : 64372306 : else if (COMPLETE_TYPE_P (TREE_TYPE (t))
8918 : 64372306 : && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
8919 : : {
8920 : : /* If the class is empty, we aren't actually loading anything. */
8921 : 5417 : r = build_constructor (TREE_TYPE (t), NULL);
8922 : 5417 : TREE_CONSTANT (r) = true;
8923 : : }
8924 : 64366889 : else if (p2280_active_p (ctx) && TYPE_REF_P (TREE_TYPE (t)))
8925 : : /* P2280 allows references to unknown... */;
8926 : 64307107 : else if (p2280_active_p (ctx) && is_this_parameter (t))
8927 : : /* ...as well as the this pointer. */;
8928 : : else
8929 : : {
8930 : 64153818 : if (!ctx->quiet)
8931 : 194 : error ("%qE is not a constant expression", t);
8932 : 64153818 : *non_constant_p = true;
8933 : : }
8934 : : break;
8935 : :
8936 : 84010216 : case CALL_EXPR:
8937 : 84010216 : case AGGR_INIT_EXPR:
8938 : 84010216 : r = cxx_eval_call_expression (ctx, t, lval,
8939 : : non_constant_p, overflow_p, jump_target);
8940 : 84010216 : break;
8941 : :
8942 : 3981652 : case DECL_EXPR:
8943 : 3981652 : {
8944 : 3981652 : r = DECL_EXPR_DECL (t);
8945 : 3981652 : if (TREE_CODE (r) == USING_DECL)
8946 : : {
8947 : 2346 : r = void_node;
8948 : 2346 : break;
8949 : : }
8950 : :
8951 : 3979306 : if (VAR_P (r)
8952 : 3979306 : && (TREE_STATIC (r)
8953 : 3961403 : || (CP_DECL_THREAD_LOCAL_P (r) && !DECL_REALLY_EXTERN (r)))
8954 : : /* Allow __FUNCTION__ etc. */
8955 : 17903 : && !DECL_ARTIFICIAL (r)
8956 : 3979325 : && !decl_constant_var_p (r))
8957 : : {
8958 : 7 : if (!ctx->quiet)
8959 : : {
8960 : 2 : if (CP_DECL_THREAD_LOCAL_P (r))
8961 : 1 : error_at (loc, "control passes through definition of %qD "
8962 : : "with thread storage duration", r);
8963 : : else
8964 : 1 : error_at (loc, "control passes through definition of %qD "
8965 : : "with static storage duration", r);
8966 : : }
8967 : 7 : *non_constant_p = true;
8968 : 7 : break;
8969 : : }
8970 : :
8971 : : /* make_rtl_for_nonlocal_decl could have deferred emission of
8972 : : a local static var, but if it appears in a statement expression
8973 : : which is constant expression evaluated to e.g. just the address
8974 : : of the variable, its DECL_EXPR will never be seen during
8975 : : gimple lowering's record_vars_into as the statement expression
8976 : : will not be in the IL at all. */
8977 : 3979299 : if (VAR_P (r)
8978 : 3979299 : && TREE_STATIC (r)
8979 : 17896 : && !DECL_REALLY_EXTERN (r)
8980 : 17896 : && DECL_FUNCTION_SCOPE_P (r)
8981 : 17896 : && !var_in_maybe_constexpr_fn (r)
8982 : 3979302 : && decl_constant_var_p (r))
8983 : : {
8984 : 3 : varpool_node *node = varpool_node::get (r);
8985 : 3 : if (node == NULL || !node->definition)
8986 : 3 : rest_of_decl_compilation (r, 0, at_eof);
8987 : : }
8988 : :
8989 : 7932237 : if (AGGREGATE_TYPE_P (TREE_TYPE (r))
8990 : 7679238 : || VECTOR_TYPE_P (TREE_TYPE (r)))
8991 : : {
8992 : 279566 : new_ctx = *ctx;
8993 : 279566 : new_ctx.object = r;
8994 : 279566 : new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL);
8995 : 279566 : CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
8996 : 279566 : ctx->global->put_value (r, new_ctx.ctor);
8997 : 279566 : ctx = &new_ctx;
8998 : : }
8999 : :
9000 : 3979299 : if (tree init = DECL_INITIAL (r))
9001 : : {
9002 : 1961547 : init = cxx_eval_constant_expression (ctx, init, vc_prvalue,
9003 : : non_constant_p, overflow_p,
9004 : : jump_target);
9005 : 1961547 : if (*jump_target)
9006 : : return NULL_TREE;
9007 : : /* Don't share a CONSTRUCTOR that might be changed. */
9008 : 1961547 : init = unshare_constructor (init);
9009 : : /* Remember that a constant object's constructor has already
9010 : : run. */
9011 : 3923094 : if (CLASS_TYPE_P (TREE_TYPE (r))
9012 : 2054347 : && CP_TYPE_CONST_P (TREE_TYPE (r)))
9013 : 1821 : TREE_READONLY (init) = true;
9014 : 1961547 : ctx->global->put_value (r, init);
9015 : : }
9016 : 2017752 : else if (ctx == &new_ctx)
9017 : : /* We gave it a CONSTRUCTOR above. */;
9018 : : else
9019 : 1839469 : ctx->global->put_value (r, NULL_TREE);
9020 : : }
9021 : : break;
9022 : :
9023 : 8861509 : case TARGET_EXPR:
9024 : 8861509 : {
9025 : 8861509 : tree type = TREE_TYPE (t);
9026 : :
9027 : 8861509 : if (!literal_type_p (type))
9028 : : {
9029 : 13326 : if (!ctx->quiet)
9030 : : {
9031 : 0 : auto_diagnostic_group d;
9032 : 0 : error ("temporary of non-literal type %qT in a "
9033 : : "constant expression", type);
9034 : 0 : explain_non_literal_class (type);
9035 : 0 : }
9036 : 13326 : *non_constant_p = true;
9037 : 3165685 : break;
9038 : : }
9039 : 8848183 : gcc_checking_assert (!TARGET_EXPR_DIRECT_INIT_P (t));
9040 : : /* Avoid evaluating a TARGET_EXPR more than once. */
9041 : 8848183 : tree slot = TARGET_EXPR_SLOT (t);
9042 : 8848183 : if (tree v = ctx->global->get_value (slot))
9043 : : {
9044 : 448 : if (lval)
9045 : 4850036 : return slot;
9046 : : r = v;
9047 : : break;
9048 : : }
9049 : 8847735 : if ((AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type)))
9050 : : {
9051 : : /* We're being expanded without an explicit target, so start
9052 : : initializing a new object; expansion with an explicit target
9053 : : strips the TARGET_EXPR before we get here. */
9054 : 7122641 : new_ctx = *ctx;
9055 : : /* Link CTX to NEW_CTX so that lookup_placeholder can resolve
9056 : : any PLACEHOLDER_EXPR within the initializer that refers to the
9057 : : former object under construction. */
9058 : 7122641 : new_ctx.parent = ctx;
9059 : 7122641 : new_ctx.ctor = build_constructor (type, NULL);
9060 : 7122641 : CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
9061 : 7122641 : new_ctx.object = slot;
9062 : 7122641 : ctx->global->put_value (new_ctx.object, new_ctx.ctor);
9063 : 7122641 : ctx = &new_ctx;
9064 : : }
9065 : :
9066 : : /* If the initializer is complex, evaluate it to initialize slot. */
9067 : 8847735 : bool is_complex = target_expr_needs_replace (t);
9068 : 8847735 : if (is_complex)
9069 : : /* In case no initialization actually happens, clear out any
9070 : : void_node from a previous evaluation. */
9071 : 42 : ctx->global->put_value (slot, NULL_TREE);
9072 : :
9073 : : /* Pass vc_prvalue because this indicates
9074 : : initialization of a temporary. */
9075 : 8847735 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), vc_prvalue,
9076 : : non_constant_p, overflow_p,
9077 : : jump_target);
9078 : 8847735 : if (*non_constant_p)
9079 : : break;
9080 : 5695800 : if (*jump_target)
9081 : : return NULL_TREE;
9082 : 5695799 : if (!is_complex)
9083 : : {
9084 : 5695760 : r = unshare_constructor (r);
9085 : : /* Adjust the type of the result to the type of the temporary. */
9086 : 5695760 : r = adjust_temp_type (type, r);
9087 : 5695760 : ctx->global->put_value (slot, r);
9088 : : }
9089 : 5695799 : if (TARGET_EXPR_CLEANUP (t)
9090 : 5695799 : && (!CLEANUP_EH_ONLY (t) || cxx_dialect >= cxx26))
9091 : : {
9092 : 199878 : ctx->global->cleanups->safe_push (TARGET_EXPR_CLEANUP (t));
9093 : : /* Mark CLEANUP_EH_ONLY cleanups by pushing NULL_TREE after
9094 : : them. */
9095 : 199878 : if (CLEANUP_EH_ONLY (t))
9096 : 335 : ctx->global->cleanups->safe_push (NULL_TREE);
9097 : : }
9098 : 5695799 : if (ctx->save_exprs)
9099 : 1870951 : ctx->save_exprs->safe_push (slot);
9100 : 5695799 : if (lval)
9101 : : return slot;
9102 : 845788 : if (is_complex)
9103 : 0 : r = ctx->global->get_value (slot);
9104 : : }
9105 : 845788 : break;
9106 : :
9107 : 32892546 : case INIT_EXPR:
9108 : 32892546 : case MODIFY_EXPR:
9109 : 32892546 : gcc_assert (jump_target == NULL || *jump_target == NULL_TREE);
9110 : 32892546 : r = cxx_eval_store_expression (ctx, t, lval,
9111 : : non_constant_p, overflow_p, jump_target);
9112 : 32892546 : break;
9113 : :
9114 : 0 : case SCOPE_REF:
9115 : 0 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
9116 : : lval,
9117 : : non_constant_p, overflow_p,
9118 : : jump_target);
9119 : 0 : break;
9120 : :
9121 : 18580225 : case RETURN_EXPR:
9122 : 18580225 : if (TREE_OPERAND (t, 0) != NULL_TREE)
9123 : 18561372 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
9124 : : lval,
9125 : : non_constant_p, overflow_p,
9126 : : jump_target);
9127 : 18580225 : if (!throws (jump_target))
9128 : 18580170 : *jump_target = t;
9129 : : break;
9130 : 22615 : case BREAK_STMT:
9131 : 22615 : case CONTINUE_STMT:
9132 : 22615 : *jump_target = t;
9133 : 22615 : break;
9134 : :
9135 : 619623 : case SAVE_EXPR:
9136 : : /* Avoid evaluating a SAVE_EXPR more than once. */
9137 : 619623 : if (tree v = ctx->global->get_value (t))
9138 : : r = v;
9139 : : else
9140 : : {
9141 : 613671 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
9142 : : vc_prvalue, non_constant_p,
9143 : : overflow_p, jump_target);
9144 : 613671 : if (*non_constant_p || *jump_target)
9145 : : break;
9146 : 7772 : ctx->global->put_value (t, r);
9147 : 7772 : if (ctx->save_exprs)
9148 : 5680 : ctx->save_exprs->safe_push (t);
9149 : : }
9150 : : break;
9151 : :
9152 : 21121156 : case NON_LVALUE_EXPR:
9153 : 21121156 : case EXPR_STMT:
9154 : 21121156 : case EH_SPEC_BLOCK:
9155 : 21121156 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
9156 : : lval,
9157 : : non_constant_p, overflow_p,
9158 : : jump_target);
9159 : 21121156 : break;
9160 : :
9161 : 310 : case TRY_BLOCK:
9162 : 310 : r = cxx_eval_constant_expression (ctx, TRY_STMTS (t), lval,
9163 : : non_constant_p, overflow_p,
9164 : : jump_target);
9165 : 310 : if (!*non_constant_p && throws (jump_target))
9166 : 195 : if (tree h = TRY_HANDLERS (t))
9167 : : {
9168 : 195 : tree type = strip_array_types (TREE_TYPE (*jump_target));
9169 : 195 : if (TREE_CODE (h) == STATEMENT_LIST)
9170 : : {
9171 : 334 : for (tree stmt : tsi_range (h))
9172 : 317 : if (TREE_CODE (stmt) == HANDLER
9173 : 317 : && handler_match_for_exception_type (stmt, type))
9174 : : {
9175 : : h = stmt;
9176 : : break;
9177 : : }
9178 : 95 : if (TREE_CODE (h) == STATEMENT_LIST)
9179 : : h = NULL_TREE;
9180 : : }
9181 : 100 : else if (TREE_CODE (h) != HANDLER
9182 : 100 : || !handler_match_for_exception_type (h, type))
9183 : : h = NULL_TREE;
9184 : : if (h)
9185 : : {
9186 : 178 : gcc_assert (VAR_P (*jump_target));
9187 : 178 : ctx->global->caught_exceptions.safe_push (*jump_target);
9188 : 178 : ctx->global->caught_exceptions.safe_push (HANDLER_TYPE (h));
9189 : 178 : *jump_target = NULL_TREE;
9190 : 178 : r = cxx_eval_constant_expression (ctx, HANDLER_BODY (h),
9191 : : vc_discard, non_constant_p,
9192 : : overflow_p, jump_target);
9193 : : }
9194 : : }
9195 : : break;
9196 : :
9197 : 27116634 : case CLEANUP_POINT_EXPR:
9198 : 27116634 : {
9199 : 27116634 : auto_vec<tree, 2> cleanups;
9200 : 27116634 : vec<tree> *prev_cleanups = ctx->global->cleanups;
9201 : 27116634 : ctx->global->cleanups = &cleanups;
9202 : :
9203 : 27116634 : auto_vec<tree, 10> save_exprs;
9204 : 27116634 : constexpr_ctx new_ctx = *ctx;
9205 : 27116634 : new_ctx.save_exprs = &save_exprs;
9206 : :
9207 : 27116634 : r = cxx_eval_constant_expression (&new_ctx, TREE_OPERAND (t, 0),
9208 : : lval,
9209 : : non_constant_p, overflow_p,
9210 : : jump_target);
9211 : :
9212 : 27116634 : ctx->global->cleanups = prev_cleanups;
9213 : 27116634 : unsigned int i;
9214 : 27116634 : tree cleanup, jmp_target = NULL_TREE;
9215 : 27116634 : bool eh = throws (jump_target);
9216 : : /* Evaluate the cleanups. */
9217 : 54413828 : FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup)
9218 : 180560 : if (cleanup == NULL_TREE)
9219 : : {
9220 : : /* NULL_TREE cleanup is a marker that before it is
9221 : : CLEANUP_EH_ONLY cleanup. Skip the cleanup before it
9222 : : if the body didn't throw. */
9223 : 315 : if (!eh)
9224 : 314 : --i;
9225 : : }
9226 : : else
9227 : 180245 : cxx_eval_constant_expression (&new_ctx, cleanup, vc_discard,
9228 : : non_constant_p, overflow_p,
9229 : : &jmp_target);
9230 : :
9231 : : /* Forget SAVE_EXPRs and TARGET_EXPRs created by this
9232 : : full-expression. */
9233 : 83226533 : for (tree save_expr : save_exprs)
9234 : 1876631 : destroy_value_checked (ctx, save_expr, non_constant_p);
9235 : 27116634 : if (throws (&jmp_target))
9236 : 0 : *jump_target = jmp_target;
9237 : 27116634 : }
9238 : 27116634 : break;
9239 : :
9240 : 17541587 : case MUST_NOT_THROW_EXPR:
9241 : 17541587 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
9242 : : lval,
9243 : : non_constant_p, overflow_p,
9244 : : jump_target);
9245 : 17541587 : if (throws (jump_target))
9246 : : {
9247 : : /* [except.handle]/7 - If the search for a handler exits the
9248 : : function body of a function with a non-throwing exception
9249 : : specification, the function std::terminate is invoked. */
9250 : 27 : if (!ctx->quiet)
9251 : : {
9252 : 9 : auto_diagnostic_group d;
9253 : 9 : diagnose_std_terminate (loc, ctx, *jump_target);
9254 : 9 : if (MUST_NOT_THROW_NOEXCEPT_P (t)
9255 : 7 : && ctx->call
9256 : 16 : && ctx->call->fundef)
9257 : 7 : inform (loc, "uncaught exception exited from %<noexcept%> "
9258 : : "function %qD",
9259 : : ctx->call->fundef->decl);
9260 : 2 : else if (MUST_NOT_THROW_THROW_P (t))
9261 : 1 : inform (loc, "destructor exited with an exception after "
9262 : : "initializing the exception object");
9263 : 1 : else if (MUST_NOT_THROW_CATCH_P (t))
9264 : 1 : inform (loc, "constructor exited with another exception while "
9265 : : "entering handler");
9266 : 9 : }
9267 : 27 : *non_constant_p = true;
9268 : 27 : *jump_target = NULL_TREE;
9269 : 27 : r = NULL_TREE;
9270 : : }
9271 : : break;
9272 : :
9273 : 0 : case TRY_CATCH_EXPR:
9274 : 0 : if (TREE_OPERAND (t, 0) == NULL_TREE)
9275 : : {
9276 : 0 : r = void_node;
9277 : 0 : break;
9278 : : }
9279 : 0 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
9280 : : non_constant_p, overflow_p,
9281 : : jump_target);
9282 : 0 : if (!*non_constant_p && throws (jump_target))
9283 : : {
9284 : 0 : tree jmp_target = NULL_TREE;
9285 : 0 : cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), vc_discard,
9286 : : non_constant_p, overflow_p,
9287 : : &jmp_target);
9288 : 0 : r = merge_jump_target (loc, ctx, r, non_constant_p, jump_target,
9289 : : jmp_target);
9290 : : }
9291 : : break;
9292 : :
9293 : 381 : case TRY_FINALLY_EXPR:
9294 : 381 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
9295 : : non_constant_p, overflow_p,
9296 : : jump_target);
9297 : 381 : if (!*non_constant_p)
9298 : : {
9299 : 366 : tree jmp_target = NULL_TREE;
9300 : : /* Also evaluate the cleanup. */
9301 : 366 : if (TREE_CODE (TREE_OPERAND (t, 1)) == EH_ELSE_EXPR
9302 : 366 : && throws (jump_target))
9303 : 0 : cxx_eval_constant_expression (ctx,
9304 : 0 : TREE_OPERAND (TREE_OPERAND (t, 1),
9305 : : 1), vc_discard,
9306 : : non_constant_p, overflow_p,
9307 : : &jmp_target);
9308 : : else
9309 : 366 : cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), vc_discard,
9310 : : non_constant_p, overflow_p,
9311 : : &jmp_target);
9312 : 366 : r = merge_jump_target (loc, ctx, r, non_constant_p, jump_target,
9313 : : jmp_target);
9314 : : }
9315 : : break;
9316 : :
9317 : 18 : case EH_ELSE_EXPR:
9318 : : /* Evaluate any cleanup that applies to non-EH exits. */
9319 : 18 : cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), vc_discard,
9320 : : non_constant_p, overflow_p,
9321 : : jump_target);
9322 : :
9323 : : /* The EH path is handled in TRY_FINALLY_EXPR handling above. */
9324 : 18 : break;
9325 : :
9326 : 655753 : case CLEANUP_STMT:
9327 : 655753 : r = cxx_eval_constant_expression (ctx, CLEANUP_BODY (t), lval,
9328 : : non_constant_p, overflow_p,
9329 : : jump_target);
9330 : 655753 : if ((!CLEANUP_EH_ONLY (t) || throws (jump_target)) && !*non_constant_p)
9331 : : {
9332 : 57130 : iloc_sentinel ils (loc);
9333 : 57130 : tree jmp_target = NULL_TREE;
9334 : : /* Also evaluate the cleanup. */
9335 : 57130 : cxx_eval_constant_expression (ctx, CLEANUP_EXPR (t), vc_discard,
9336 : : non_constant_p, overflow_p,
9337 : : &jmp_target);
9338 : 57130 : r = merge_jump_target (loc, ctx, r, non_constant_p, jump_target,
9339 : : jmp_target);
9340 : 57130 : }
9341 : : break;
9342 : :
9343 : : /* These differ from cxx_eval_unary_expression in that this doesn't
9344 : : check for a constant operand or result; an address can be
9345 : : constant without its operand being, and vice versa. */
9346 : 31898760 : case MEM_REF:
9347 : 31898760 : case INDIRECT_REF:
9348 : 31898760 : r = cxx_eval_indirect_ref (ctx, t, lval,
9349 : : non_constant_p, overflow_p,
9350 : : jump_target);
9351 : 31898760 : break;
9352 : :
9353 : 38252542 : case ADDR_EXPR:
9354 : 38252542 : {
9355 : 38252542 : tree oldop = TREE_OPERAND (t, 0);
9356 : 38252542 : tree op = cxx_eval_constant_expression (ctx, oldop, vc_glvalue,
9357 : : non_constant_p, overflow_p,
9358 : : jump_target);
9359 : 38252542 : if (*jump_target)
9360 : : return NULL_TREE;
9361 : : /* Don't VERIFY_CONSTANT here. */
9362 : 38252542 : if (*non_constant_p)
9363 : : return t;
9364 : 30751211 : gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR);
9365 : : /* This function does more aggressive folding than fold itself. */
9366 : 30751211 : r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
9367 : 30751211 : if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
9368 : : {
9369 : 22638104 : ggc_free (r);
9370 : 22638104 : return t;
9371 : : }
9372 : : break;
9373 : : }
9374 : :
9375 : 486315 : case REALPART_EXPR:
9376 : 486315 : case IMAGPART_EXPR:
9377 : 486315 : if (lval)
9378 : : {
9379 : 584 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
9380 : : non_constant_p, overflow_p,
9381 : : jump_target);
9382 : 584 : if (*jump_target)
9383 : : return NULL_TREE;
9384 : 584 : if (r == error_mark_node)
9385 : : ;
9386 : 584 : else if (r == TREE_OPERAND (t, 0) || lval == vc_discard)
9387 : : r = t;
9388 : : else
9389 : 562 : r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), r);
9390 : : break;
9391 : : }
9392 : : /* FALLTHRU */
9393 : 20225632 : case CONJ_EXPR:
9394 : 20225632 : case FIX_TRUNC_EXPR:
9395 : 20225632 : case FLOAT_EXPR:
9396 : 20225632 : case NEGATE_EXPR:
9397 : 20225632 : case ABS_EXPR:
9398 : 20225632 : case ABSU_EXPR:
9399 : 20225632 : case BIT_NOT_EXPR:
9400 : 20225632 : case TRUTH_NOT_EXPR:
9401 : 20225632 : case FIXED_CONVERT_EXPR:
9402 : 20225632 : case VEC_DUPLICATE_EXPR:
9403 : 20225632 : r = cxx_eval_unary_expression (ctx, t, lval,
9404 : : non_constant_p, overflow_p,
9405 : : jump_target);
9406 : 20225632 : break;
9407 : :
9408 : 7002878 : case SIZEOF_EXPR:
9409 : 7002878 : r = fold_sizeof_expr (t);
9410 : : /* In a template, fold_sizeof_expr may merely create a new SIZEOF_EXPR,
9411 : : which could lead to an infinite recursion. */
9412 : 7002878 : if (TREE_CODE (r) != SIZEOF_EXPR)
9413 : 7002878 : r = cxx_eval_constant_expression (ctx, r, lval,
9414 : : non_constant_p, overflow_p,
9415 : : jump_target);
9416 : : else
9417 : : {
9418 : 0 : *non_constant_p = true;
9419 : 0 : gcc_assert (ctx->quiet);
9420 : : }
9421 : :
9422 : : break;
9423 : :
9424 : 3308571 : case COMPOUND_EXPR:
9425 : 3308571 : {
9426 : : /* check_return_expr sometimes wraps a TARGET_EXPR in a
9427 : : COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
9428 : : introduced by build_call_a. */
9429 : 3308571 : tree op0 = TREE_OPERAND (t, 0);
9430 : 3308571 : tree op1 = TREE_OPERAND (t, 1);
9431 : 3308571 : STRIP_NOPS (op1);
9432 : 696130 : if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
9433 : 3379617 : || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
9434 : 2944796 : r = cxx_eval_constant_expression (ctx, op0,
9435 : : lval, non_constant_p, overflow_p,
9436 : : jump_target);
9437 : : else
9438 : : {
9439 : : /* Check that the LHS is constant and then discard it. */
9440 : 363775 : cxx_eval_constant_expression (ctx, op0, vc_discard,
9441 : : non_constant_p, overflow_p,
9442 : : jump_target);
9443 : 363775 : if (*jump_target)
9444 : : return NULL_TREE;
9445 : 363530 : if (*non_constant_p)
9446 : : return t;
9447 : 270114 : op1 = TREE_OPERAND (t, 1);
9448 : 270114 : r = cxx_eval_constant_expression (ctx, op1,
9449 : : lval, non_constant_p, overflow_p,
9450 : : jump_target);
9451 : : }
9452 : : }
9453 : : break;
9454 : :
9455 : 48008216 : case POINTER_PLUS_EXPR:
9456 : 48008216 : case POINTER_DIFF_EXPR:
9457 : 48008216 : case PLUS_EXPR:
9458 : 48008216 : case MINUS_EXPR:
9459 : 48008216 : case MULT_EXPR:
9460 : 48008216 : case TRUNC_DIV_EXPR:
9461 : 48008216 : case CEIL_DIV_EXPR:
9462 : 48008216 : case FLOOR_DIV_EXPR:
9463 : 48008216 : case ROUND_DIV_EXPR:
9464 : 48008216 : case TRUNC_MOD_EXPR:
9465 : 48008216 : case CEIL_MOD_EXPR:
9466 : 48008216 : case ROUND_MOD_EXPR:
9467 : 48008216 : case RDIV_EXPR:
9468 : 48008216 : case EXACT_DIV_EXPR:
9469 : 48008216 : case MIN_EXPR:
9470 : 48008216 : case MAX_EXPR:
9471 : 48008216 : case LSHIFT_EXPR:
9472 : 48008216 : case RSHIFT_EXPR:
9473 : 48008216 : case LROTATE_EXPR:
9474 : 48008216 : case RROTATE_EXPR:
9475 : 48008216 : case BIT_IOR_EXPR:
9476 : 48008216 : case BIT_XOR_EXPR:
9477 : 48008216 : case BIT_AND_EXPR:
9478 : 48008216 : case TRUTH_XOR_EXPR:
9479 : 48008216 : case LT_EXPR:
9480 : 48008216 : case LE_EXPR:
9481 : 48008216 : case GT_EXPR:
9482 : 48008216 : case GE_EXPR:
9483 : 48008216 : case EQ_EXPR:
9484 : 48008216 : case NE_EXPR:
9485 : 48008216 : case SPACESHIP_EXPR:
9486 : 48008216 : case UNORDERED_EXPR:
9487 : 48008216 : case ORDERED_EXPR:
9488 : 48008216 : case UNLT_EXPR:
9489 : 48008216 : case UNLE_EXPR:
9490 : 48008216 : case UNGT_EXPR:
9491 : 48008216 : case UNGE_EXPR:
9492 : 48008216 : case UNEQ_EXPR:
9493 : 48008216 : case LTGT_EXPR:
9494 : 48008216 : case RANGE_EXPR:
9495 : 48008216 : case COMPLEX_EXPR:
9496 : 48008216 : r = cxx_eval_binary_expression (ctx, t, lval,
9497 : : non_constant_p, overflow_p,
9498 : : jump_target);
9499 : 48008216 : break;
9500 : :
9501 : : /* fold can introduce non-IF versions of these; still treat them as
9502 : : short-circuiting. */
9503 : 5547652 : case TRUTH_AND_EXPR:
9504 : 5547652 : case TRUTH_ANDIF_EXPR:
9505 : 5547652 : r = cxx_eval_logical_expression (ctx, t, boolean_false_node,
9506 : : boolean_true_node,
9507 : : non_constant_p, overflow_p,
9508 : : jump_target);
9509 : 5547652 : break;
9510 : :
9511 : 1188188 : case TRUTH_OR_EXPR:
9512 : 1188188 : case TRUTH_ORIF_EXPR:
9513 : 1188188 : r = cxx_eval_logical_expression (ctx, t, boolean_true_node,
9514 : : boolean_false_node,
9515 : : non_constant_p, overflow_p,
9516 : : jump_target);
9517 : 1188188 : break;
9518 : :
9519 : 2679141 : case ARRAY_REF:
9520 : 2679141 : r = cxx_eval_array_reference (ctx, t, lval,
9521 : : non_constant_p, overflow_p,
9522 : : jump_target);
9523 : 2679141 : break;
9524 : :
9525 : 29295836 : case COMPONENT_REF:
9526 : 29295836 : if (is_overloaded_fn (t))
9527 : : {
9528 : : /* We can only get here in checking mode via
9529 : : build_non_dependent_expr, because any expression that
9530 : : calls or takes the address of the function will have
9531 : : pulled a FUNCTION_DECL out of the COMPONENT_REF. */
9532 : 6 : gcc_checking_assert (ctx->quiet || errorcount);
9533 : 6 : *non_constant_p = true;
9534 : 6 : return t;
9535 : : }
9536 : 29295830 : r = cxx_eval_component_reference (ctx, t, lval,
9537 : : non_constant_p, overflow_p,
9538 : : jump_target);
9539 : 29295830 : break;
9540 : :
9541 : 2482 : case BIT_FIELD_REF:
9542 : 2482 : r = cxx_eval_bit_field_ref (ctx, t, lval,
9543 : : non_constant_p, overflow_p,
9544 : : jump_target);
9545 : 2482 : break;
9546 : :
9547 : 8209828 : case COND_EXPR:
9548 : 8209828 : case IF_STMT:
9549 : 8209828 : if (*jump_target)
9550 : : {
9551 : 152144 : tree orig_jump = *jump_target;
9552 : 152144 : tree arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 1))
9553 : 304288 : ? TREE_OPERAND (t, 1) : void_node);
9554 : : /* When jumping to a label, the label might be either in the
9555 : : then or else blocks, so process then block first in skipping
9556 : : mode first, and if we are still in the skipping mode at its end,
9557 : : process the else block too. */
9558 : 152144 : r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p,
9559 : : overflow_p, jump_target);
9560 : : /* It's possible that we found the label in the then block. But
9561 : : it could have been followed by another jumping statement, e.g.
9562 : : say we're looking for case 1:
9563 : : if (cond)
9564 : : {
9565 : : // skipped statements
9566 : : case 1:; // clears up *jump_target
9567 : : return 1; // and sets it to a RETURN_EXPR
9568 : : }
9569 : : else { ... }
9570 : : in which case we need not go looking to the else block.
9571 : : (goto is not allowed in a constexpr function.) */
9572 : 152144 : if (*jump_target == orig_jump)
9573 : : {
9574 : 152183 : arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 2))
9575 : 152183 : ? TREE_OPERAND (t, 2) : void_node);
9576 : 152114 : r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p,
9577 : : overflow_p, jump_target);
9578 : : }
9579 : : break;
9580 : : }
9581 : 8057684 : r = cxx_eval_conditional_expression (ctx, t, lval,
9582 : : non_constant_p, overflow_p,
9583 : : jump_target);
9584 : 8057684 : break;
9585 : 866 : case VEC_COND_EXPR:
9586 : 866 : r = cxx_eval_vector_conditional_expression (ctx, t, non_constant_p,
9587 : : overflow_p, jump_target);
9588 : 866 : break;
9589 : :
9590 : 13092169 : case CONSTRUCTOR:
9591 : 13092169 : if (TREE_CONSTANT (t) && reduced_constant_expression_p (t))
9592 : : {
9593 : : /* Don't re-process a constant CONSTRUCTOR. */
9594 : 11833712 : verify_constructor_flags (t);
9595 : 11833712 : if (TREE_CONSTANT (t))
9596 : : return t;
9597 : : }
9598 : 1258457 : r = cxx_eval_bare_aggregate (ctx, t, lval,
9599 : : non_constant_p, overflow_p, jump_target);
9600 : 1258457 : break;
9601 : :
9602 : 501 : case VEC_INIT_EXPR:
9603 : : /* We can get this in a defaulted constructor for a class with a
9604 : : non-static data member of array type. Either the initializer will
9605 : : be NULL, meaning default-initialization, or it will be an lvalue
9606 : : or xvalue of the same type, meaning direct-initialization from the
9607 : : corresponding member. */
9608 : 501 : r = cxx_eval_vec_init (ctx, t, lval,
9609 : : non_constant_p, overflow_p, jump_target);
9610 : 501 : break;
9611 : :
9612 : 16 : case VEC_PERM_EXPR:
9613 : 16 : r = cxx_eval_trinary_expression (ctx, t, lval,
9614 : : non_constant_p, overflow_p,
9615 : : jump_target);
9616 : 16 : break;
9617 : :
9618 : 4 : case PAREN_EXPR:
9619 : 4 : gcc_assert (!REF_PARENTHESIZED_P (t));
9620 : : /* A PAREN_EXPR resulting from __builtin_assoc_barrier has no effect in
9621 : : constant expressions since it's unaffected by -fassociative-math. */
9622 : 4 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
9623 : : non_constant_p, overflow_p,
9624 : : jump_target);
9625 : 4 : break;
9626 : :
9627 : 166318339 : case NOP_EXPR:
9628 : 166318339 : if (REINTERPRET_CAST_P (t))
9629 : : {
9630 : 67 : if (!ctx->quiet)
9631 : 0 : error_at (loc,
9632 : : "%<reinterpret_cast%> is not a constant expression");
9633 : 67 : *non_constant_p = true;
9634 : 67 : return t;
9635 : : }
9636 : : /* FALLTHROUGH. */
9637 : 196924326 : case CONVERT_EXPR:
9638 : 196924326 : case VIEW_CONVERT_EXPR:
9639 : 196924326 : case UNARY_PLUS_EXPR:
9640 : 196924326 : {
9641 : 196924326 : tree oldop = TREE_OPERAND (t, 0);
9642 : :
9643 : 196924326 : tree op = cxx_eval_constant_expression (ctx, oldop,
9644 : 196924326 : VOID_TYPE_P (TREE_TYPE (t))
9645 : : ? vc_discard
9646 : : : tcode == VIEW_CONVERT_EXPR
9647 : 186035959 : ? lval : vc_prvalue,
9648 : : non_constant_p, overflow_p,
9649 : : jump_target);
9650 : 196921629 : if (*jump_target)
9651 : : return NULL_TREE;
9652 : 196921297 : if (*non_constant_p)
9653 : : return t;
9654 : 147218673 : tree type = TREE_TYPE (t);
9655 : :
9656 : 147218673 : if (VOID_TYPE_P (type))
9657 : 10312959 : return void_node;
9658 : :
9659 : 136905714 : if (TREE_CODE (t) == CONVERT_EXPR
9660 : 11826914 : && ARITHMETIC_TYPE_P (type)
9661 : 2311451 : && INDIRECT_TYPE_P (TREE_TYPE (op))
9662 : 136928824 : && ctx->strict)
9663 : : {
9664 : 23020 : if (!ctx->quiet)
9665 : 54 : error_at (loc,
9666 : : "conversion from pointer type %qT to arithmetic type "
9667 : 54 : "%qT in a constant expression", TREE_TYPE (op), type);
9668 : 23020 : *non_constant_p = true;
9669 : 23020 : return t;
9670 : : }
9671 : :
9672 : : /* [expr.const]: a conversion from type cv void* to a pointer-to-object
9673 : : type cannot be part of a core constant expression as a resolution to
9674 : : DR 1312. */
9675 : 37692445 : if (TYPE_PTROB_P (type)
9676 : 36474147 : && TYPE_PTR_P (TREE_TYPE (op))
9677 : 26945470 : && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (op)))
9678 : : /* Inside a call to std::construct_at,
9679 : : std::allocator<T>::{,de}allocate, or
9680 : : std::source_location::current, we permit casting from void*
9681 : : because that is compiler-generated code. */
9682 : 47127 : && !is_std_construct_at (ctx->call)
9683 : 12201 : && !is_std_allocator_allocate (ctx->call)
9684 : 136893962 : && !is_std_source_location_current (ctx->call))
9685 : : {
9686 : : /* Likewise, don't error when casting from void* when OP is
9687 : : &heap uninit and similar. */
9688 : 11109 : tree sop = tree_strip_nop_conversions (op);
9689 : 11109 : tree decl = NULL_TREE;
9690 : 11109 : if (TREE_CODE (sop) == ADDR_EXPR)
9691 : 8311 : decl = TREE_OPERAND (sop, 0);
9692 : 8311 : if (decl
9693 : 8311 : && VAR_P (decl)
9694 : 8219 : && DECL_ARTIFICIAL (decl)
9695 : 16493 : && (DECL_NAME (decl) == heap_identifier
9696 : 4839 : || DECL_NAME (decl) == heap_uninit_identifier
9697 : 791 : || DECL_NAME (decl) == heap_vec_identifier
9698 : 545 : || DECL_NAME (decl) == heap_vec_uninit_identifier))
9699 : : /* OK */;
9700 : : /* P2738 (C++26): a conversion from a prvalue P of type "pointer to
9701 : : cv void" to a pointer-to-object type T unless P is a null
9702 : : pointer value or points to an object whose type is similar to
9703 : : T. */
9704 : 2943 : else if (cxx_dialect > cxx23)
9705 : : {
9706 : 2827 : if (integer_zerop (sop))
9707 : 30 : return build_int_cst (type, 0);
9708 : 2797 : r = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (type), sop,
9709 : : NULL, jump_target);
9710 : 2797 : if (*jump_target)
9711 : : return NULL_TREE;
9712 : 2797 : if (r)
9713 : : {
9714 : 2768 : r = build1 (ADDR_EXPR, type, r);
9715 : 2768 : break;
9716 : : }
9717 : 29 : if (!ctx->quiet)
9718 : : {
9719 : 3 : gcc_assert (TREE_CODE (sop) == ADDR_EXPR);
9720 : 3 : auto_diagnostic_group d;
9721 : 3 : error_at (loc, "cast from %qT is not allowed in a "
9722 : : "constant expression because "
9723 : : "pointed-to type %qT is not similar to %qT",
9724 : 3 : TREE_TYPE (op), TREE_TYPE (TREE_TYPE (sop)),
9725 : 3 : TREE_TYPE (type));
9726 : 3 : tree obj = build_fold_indirect_ref (sop);
9727 : 3 : if (TREE_CODE (obj) == COMPONENT_REF)
9728 : 2 : obj = TREE_OPERAND (obj, 1);
9729 : 3 : if (DECL_P (obj))
9730 : 3 : inform (DECL_SOURCE_LOCATION (obj),
9731 : : "pointed-to object declared here");
9732 : 3 : }
9733 : 29 : *non_constant_p = true;
9734 : 29 : return t;
9735 : : }
9736 : : else
9737 : : {
9738 : 116 : if (!ctx->quiet)
9739 : 26 : error_at (loc, "cast from %qT is not allowed in a "
9740 : : "constant expression before C++26",
9741 : 26 : TREE_TYPE (op));
9742 : 116 : *non_constant_p = true;
9743 : 116 : return t;
9744 : : }
9745 : : }
9746 : :
9747 : 136879751 : if (TREE_CODE (op) == PTRMEM_CST && !TYPE_PTRMEM_P (type))
9748 : : {
9749 : 1685 : op = cplus_expand_constant (op);
9750 : 1685 : if (TREE_CODE (op) == PTRMEM_CST)
9751 : : {
9752 : 21 : if (!ctx->quiet)
9753 : 3 : error_at (loc, "%qE is not a constant expression when the "
9754 : : "class %qT is still incomplete", op,
9755 : 3 : PTRMEM_CST_CLASS (op));
9756 : 21 : *non_constant_p = true;
9757 : 21 : return t;
9758 : : }
9759 : : }
9760 : :
9761 : 136879730 : if (TREE_CODE (op) == PTRMEM_CST && tcode == NOP_EXPR)
9762 : : {
9763 : 795 : if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (op))
9764 : 795 : && !can_convert_qual (type, op))
9765 : 6 : op = cplus_expand_constant (op);
9766 : 795 : return cp_fold_convert (type, op);
9767 : : }
9768 : :
9769 : 136878935 : if (INDIRECT_TYPE_P (type) && TREE_CODE (op) == INTEGER_CST)
9770 : : {
9771 : 319037 : if (integer_zerop (op))
9772 : : {
9773 : 260627 : if (TYPE_REF_P (type))
9774 : : {
9775 : 32 : if (!ctx->quiet)
9776 : 4 : error_at (loc, "dereferencing a null pointer");
9777 : 32 : *non_constant_p = true;
9778 : 32 : return t;
9779 : : }
9780 : : }
9781 : 58410 : else if (TYPE_PTR_P (type)
9782 : 58410 : && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
9783 : : /* INTEGER_CST with pointer-to-method type is only used
9784 : : for a virtual method in a pointer to member function.
9785 : : Don't reject those. */
9786 : : ;
9787 : : else
9788 : : {
9789 : : /* This detects for example:
9790 : : reinterpret_cast<void*>(sizeof 0)
9791 : : */
9792 : 58407 : if (!ctx->quiet)
9793 : 21 : error_at (loc, "%<reinterpret_cast<%T>(%E)%> is not "
9794 : : "a constant expression",
9795 : : type, op);
9796 : 58407 : *non_constant_p = true;
9797 : 58407 : return t;
9798 : : }
9799 : : }
9800 : :
9801 : 136820496 : if (INDIRECT_TYPE_P (type)
9802 : 58985363 : && TREE_CODE (op) == NOP_EXPR
9803 : 29303815 : && TREE_TYPE (op) == ptr_type_node
9804 : 46401 : && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR
9805 : 44939 : && VAR_P (TREE_OPERAND (TREE_OPERAND (op, 0), 0))
9806 : 136843124 : && (DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0),
9807 : 22628 : 0)) == heap_uninit_identifier
9808 : 17647 : || DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0),
9809 : 17647 : 0)) == heap_vec_uninit_identifier))
9810 : : {
9811 : 5510 : tree var = TREE_OPERAND (TREE_OPERAND (op, 0), 0);
9812 : 5510 : tree var_size = TYPE_SIZE_UNIT (TREE_TYPE (var));
9813 : 5510 : tree elt_type = TREE_TYPE (type);
9814 : 5510 : tree cookie_size = NULL_TREE;
9815 : 5510 : tree arg_size = NULL_TREE;
9816 : 5510 : if (TREE_CODE (elt_type) == RECORD_TYPE
9817 : 5510 : && TYPE_IDENTIFIER (elt_type) == heap_identifier)
9818 : : {
9819 : 5 : tree fld1 = TYPE_FIELDS (elt_type);
9820 : 5 : tree fld2 = DECL_CHAIN (fld1);
9821 : 5 : elt_type = TREE_TYPE (TREE_TYPE (fld2));
9822 : 5 : cookie_size = TYPE_SIZE_UNIT (TREE_TYPE (fld1));
9823 : : }
9824 : 5510 : DECL_NAME (var)
9825 : 5510 : = (DECL_NAME (var) == heap_uninit_identifier
9826 : 5510 : ? heap_identifier : heap_vec_identifier);
9827 : : /* For zero sized elt_type, try to recover how many outer_nelts
9828 : : it should have. */
9829 : 5510 : if ((cookie_size ? tree_int_cst_equal (var_size, cookie_size)
9830 : 5505 : : integer_zerop (var_size))
9831 : 28 : && !int_size_in_bytes (elt_type)
9832 : 9 : && TREE_CODE (oldop) == CALL_EXPR
9833 : 5524 : && call_expr_nargs (oldop) >= 1)
9834 : 9 : if (tree fun = get_function_named_in_call (oldop))
9835 : 9 : if (cxx_replaceable_global_alloc_fn (fun)
9836 : 9 : && IDENTIFIER_NEW_OP_P (DECL_NAME (fun)))
9837 : 9 : arg_size = CALL_EXPR_ARG (oldop, 0);
9838 : 5510 : tree new_type
9839 : 5510 : = build_new_constexpr_heap_type (ctx, elt_type, cookie_size,
9840 : : var_size, arg_size,
9841 : : non_constant_p, overflow_p,
9842 : : jump_target);
9843 : 5510 : if (*jump_target)
9844 : : return NULL_TREE;
9845 : 5510 : TREE_TYPE (var) = new_type;
9846 : 5510 : TREE_TYPE (TREE_OPERAND (op, 0))
9847 : 11020 : = build_pointer_type (TREE_TYPE (var));
9848 : : }
9849 : :
9850 : 136820496 : if (op == oldop && tcode != UNARY_PLUS_EXPR)
9851 : : /* We didn't fold at the top so we could check for ptr-int
9852 : : conversion. */
9853 : 11590317 : return fold (t);
9854 : :
9855 : 125230179 : tree sop;
9856 : :
9857 : : /* Handle an array's bounds having been deduced after we built
9858 : : the wrapping expression. */
9859 : 125230179 : if (same_type_ignoring_tlq_and_bounds_p (type, TREE_TYPE (op)))
9860 : : r = op;
9861 : 35827587 : else if (sop = tree_strip_nop_conversions (op),
9862 : 49430643 : sop != op && (same_type_ignoring_tlq_and_bounds_p
9863 : 13603056 : (type, TREE_TYPE (sop))))
9864 : : r = sop;
9865 : 29140187 : else if (tcode == UNARY_PLUS_EXPR)
9866 : 0 : r = fold_convert (TREE_TYPE (t), op);
9867 : : else
9868 : 29140187 : r = fold_build1 (tcode, type, op);
9869 : :
9870 : : /* Conversion of an out-of-range value has implementation-defined
9871 : : behavior; the language considers it different from arithmetic
9872 : : overflow, which is undefined. */
9873 : 125230179 : if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op))
9874 : 11461 : TREE_OVERFLOW (r) = false;
9875 : : }
9876 : : break;
9877 : :
9878 : 8024 : case EXCESS_PRECISION_EXPR:
9879 : 8024 : {
9880 : 8024 : tree oldop = TREE_OPERAND (t, 0);
9881 : :
9882 : 8024 : tree op = cxx_eval_constant_expression (ctx, oldop,
9883 : : lval,
9884 : : non_constant_p, overflow_p,
9885 : : jump_target);
9886 : 8024 : if (*jump_target)
9887 : : return NULL_TREE;
9888 : 8024 : if (*non_constant_p)
9889 : : return t;
9890 : 8020 : r = fold_convert (TREE_TYPE (t), op);
9891 : 8020 : break;
9892 : : }
9893 : :
9894 : 36699 : case EMPTY_CLASS_EXPR:
9895 : : /* Handle EMPTY_CLASS_EXPR produced by build_call_a by lowering
9896 : : it to an appropriate CONSTRUCTOR. */
9897 : 36699 : return build_constructor (TREE_TYPE (t), NULL);
9898 : :
9899 : 18600268 : case STATEMENT_LIST:
9900 : 18600268 : new_ctx = *ctx;
9901 : 18600268 : new_ctx.ctor = new_ctx.object = NULL_TREE;
9902 : 18600268 : return cxx_eval_statement_list (&new_ctx, t,
9903 : 18600268 : non_constant_p, overflow_p, jump_target);
9904 : :
9905 : 7915005 : case BIND_EXPR:
9906 : : /* Pre-emptively clear the vars declared by this BIND_EXPR from the value
9907 : : map, so that when checking whether they're already destroyed later we
9908 : : don't get confused by remnants of previous calls. */
9909 : 11254513 : for (tree decl = BIND_EXPR_VARS (t); decl; decl = DECL_CHAIN (decl))
9910 : 3339508 : ctx->global->clear_value (decl);
9911 : 7915005 : r = cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t),
9912 : : lval,
9913 : : non_constant_p, overflow_p,
9914 : : jump_target);
9915 : 11254513 : for (tree decl = BIND_EXPR_VARS (t); decl; decl = DECL_CHAIN (decl))
9916 : 3339508 : destroy_value_checked (ctx, decl, non_constant_p);
9917 : : break;
9918 : :
9919 : 2259463 : case PREINCREMENT_EXPR:
9920 : 2259463 : case POSTINCREMENT_EXPR:
9921 : 2259463 : case PREDECREMENT_EXPR:
9922 : 2259463 : case POSTDECREMENT_EXPR:
9923 : 2259463 : return cxx_eval_increment_expression (ctx, t,
9924 : : lval, non_constant_p, overflow_p,
9925 : 2259463 : jump_target);
9926 : :
9927 : 684 : case THROW_EXPR:
9928 : 684 : if (cxx_dialect >= cxx26)
9929 : 356 : return cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
9930 : : non_constant_p, overflow_p,
9931 : 356 : jump_target);
9932 : : /* FALLTHROUGH */
9933 : 336 : case LAMBDA_EXPR:
9934 : 336 : case NEW_EXPR:
9935 : 336 : case VEC_NEW_EXPR:
9936 : 336 : case DELETE_EXPR:
9937 : 336 : case VEC_DELETE_EXPR:
9938 : 336 : case MODOP_EXPR:
9939 : : /* GCC internal stuff. */
9940 : 336 : case VA_ARG_EXPR:
9941 : 336 : case BASELINK:
9942 : 336 : case OFFSET_REF:
9943 : 336 : if (!ctx->quiet)
9944 : 86 : error_at (loc, "expression %qE is not a constant expression", t);
9945 : 336 : *non_constant_p = true;
9946 : 336 : break;
9947 : :
9948 : 114129 : case OBJ_TYPE_REF:
9949 : : /* Virtual function lookup. We don't need to do anything fancy. */
9950 : 114129 : return cxx_eval_constant_expression (ctx, OBJ_TYPE_REF_EXPR (t),
9951 : : lval, non_constant_p, overflow_p,
9952 : 114129 : jump_target);
9953 : :
9954 : 16121 : case PLACEHOLDER_EXPR:
9955 : : /* Use of the value or address of the current object. */
9956 : 16121 : if (tree ctor = lookup_placeholder (ctx, lval, TREE_TYPE (t)))
9957 : : {
9958 : 15889 : if (TREE_CODE (ctor) == CONSTRUCTOR)
9959 : : return ctor;
9960 : : else
9961 : 15589 : return cxx_eval_constant_expression (ctx, ctor, lval,
9962 : : non_constant_p, overflow_p,
9963 : 15589 : jump_target);
9964 : : }
9965 : : /* A placeholder without a referent. We can get here when
9966 : : checking whether NSDMIs are noexcept, or in massage_init_elt;
9967 : : just say it's non-constant for now. */
9968 : 232 : gcc_assert (ctx->quiet);
9969 : 232 : *non_constant_p = true;
9970 : 232 : break;
9971 : :
9972 : 227 : case EXIT_EXPR:
9973 : 227 : {
9974 : 227 : tree cond = TREE_OPERAND (t, 0);
9975 : 227 : cond = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
9976 : : non_constant_p, overflow_p,
9977 : : jump_target);
9978 : 227 : if (*jump_target)
9979 : : return NULL_TREE;
9980 : 227 : VERIFY_CONSTANT (cond);
9981 : 227 : if (integer_nonzerop (cond))
9982 : 64 : *jump_target = t;
9983 : : }
9984 : : break;
9985 : :
9986 : 9 : case GOTO_EXPR:
9987 : 9 : if (breaks (&TREE_OPERAND (t, 0))
9988 : 9 : || continues (&TREE_OPERAND (t, 0)))
9989 : 3 : *jump_target = TREE_OPERAND (t, 0);
9990 : : else
9991 : : {
9992 : 6 : if (!ctx->quiet)
9993 : 1 : error_at (loc, "%<goto%> is not a constant expression");
9994 : 6 : *non_constant_p = true;
9995 : : }
9996 : : break;
9997 : :
9998 : 774664 : case LOOP_EXPR:
9999 : 774664 : case DO_STMT:
10000 : 774664 : case WHILE_STMT:
10001 : 774664 : case FOR_STMT:
10002 : 774664 : cxx_eval_loop_expr (ctx, t,
10003 : : non_constant_p, overflow_p, jump_target);
10004 : 774664 : break;
10005 : :
10006 : 35444 : case SWITCH_EXPR:
10007 : 35444 : case SWITCH_STMT:
10008 : 35444 : cxx_eval_switch_expr (ctx, t,
10009 : : non_constant_p, overflow_p, jump_target);
10010 : 35444 : break;
10011 : :
10012 : 100 : case REQUIRES_EXPR:
10013 : : /* It's possible to get a requires-expression in a constant
10014 : : expression. For example:
10015 : :
10016 : : template<typename T> concept bool C() {
10017 : : return requires (T t) { t; };
10018 : : }
10019 : :
10020 : : template<typename T> requires !C<T>() void f(T);
10021 : :
10022 : : Normalization leaves f with the associated constraint
10023 : : '!requires (T t) { ... }' which is not transformed into
10024 : : a constraint. */
10025 : 100 : if (!processing_template_decl)
10026 : 100 : return evaluate_requires_expr (t);
10027 : : else
10028 : 0 : *non_constant_p = true;
10029 : 0 : return t;
10030 : :
10031 : 7301 : case ANNOTATE_EXPR:
10032 : 7301 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
10033 : : lval,
10034 : : non_constant_p, overflow_p,
10035 : : jump_target);
10036 : 7301 : break;
10037 : :
10038 : 19026 : case USING_STMT:
10039 : 19026 : r = void_node;
10040 : 19026 : break;
10041 : :
10042 : 103 : case ASSERTION_STMT:
10043 : 103 : case PRECONDITION_STMT:
10044 : 103 : case POSTCONDITION_STMT:
10045 : 103 : {
10046 : 103 : contract_semantic semantic = get_contract_semantic (t);
10047 : 103 : if (semantic == CCS_IGNORE)
10048 : : break;
10049 : :
10050 : 87 : if (!cxx_eval_assert (ctx, CONTRACT_CONDITION (t),
10051 : : G_("contract predicate is false in "
10052 : : "constant expression"),
10053 : 87 : EXPR_LOCATION (t), checked_contract_p (semantic),
10054 : : non_constant_p, overflow_p))
10055 : 38 : *non_constant_p = true;
10056 : 87 : r = void_node;
10057 : : }
10058 : 87 : break;
10059 : :
10060 : 1024033 : case TEMPLATE_ID_EXPR:
10061 : 1024033 : {
10062 : : /* We can evaluate template-id that refers to a concept only if
10063 : : the template arguments are non-dependent. */
10064 : 1024033 : gcc_assert (concept_check_p (t));
10065 : :
10066 : 1024033 : if (!value_dependent_expression_p (t)
10067 : 1024033 : && !uid_sensitive_constexpr_evaluation_p ())
10068 : 1023987 : r = evaluate_concept_check (t);
10069 : : else
10070 : 46 : *non_constant_p = true;
10071 : :
10072 : : break;
10073 : : }
10074 : :
10075 : 48 : case ASM_EXPR:
10076 : 48 : if (!ctx->quiet)
10077 : 28 : inline_asm_in_constexpr_error (loc, /*constexpr_fundef_p*/false);
10078 : 48 : *non_constant_p = true;
10079 : 48 : return t;
10080 : :
10081 : 1025 : case BIT_CAST_EXPR:
10082 : 1025 : if (lval)
10083 : : {
10084 : 0 : if (!ctx->quiet)
10085 : 0 : error_at (EXPR_LOCATION (t),
10086 : : "address of a call to %qs is not a constant expression",
10087 : : "__builtin_bit_cast");
10088 : 0 : *non_constant_p = true;
10089 : 0 : return t;
10090 : : }
10091 : 1025 : r = cxx_eval_bit_cast (ctx, t, non_constant_p, overflow_p, jump_target);
10092 : 1025 : break;
10093 : :
10094 : 0 : case OMP_PARALLEL:
10095 : 0 : case OMP_TASK:
10096 : 0 : case OMP_FOR:
10097 : 0 : case OMP_SIMD:
10098 : 0 : case OMP_DISTRIBUTE:
10099 : 0 : case OMP_TASKLOOP:
10100 : 0 : case OMP_LOOP:
10101 : 0 : case OMP_TEAMS:
10102 : 0 : case OMP_TARGET_DATA:
10103 : 0 : case OMP_TARGET:
10104 : 0 : case OMP_SECTIONS:
10105 : 0 : case OMP_ORDERED:
10106 : 0 : case OMP_CRITICAL:
10107 : 0 : case OMP_SINGLE:
10108 : 0 : case OMP_SCAN:
10109 : 0 : case OMP_SCOPE:
10110 : 0 : case OMP_SECTION:
10111 : 0 : case OMP_STRUCTURED_BLOCK:
10112 : 0 : case OMP_MASTER:
10113 : 0 : case OMP_MASKED:
10114 : 0 : case OMP_TASKGROUP:
10115 : 0 : case OMP_TARGET_UPDATE:
10116 : 0 : case OMP_TARGET_ENTER_DATA:
10117 : 0 : case OMP_TARGET_EXIT_DATA:
10118 : 0 : case OMP_ATOMIC:
10119 : 0 : case OMP_ATOMIC_READ:
10120 : 0 : case OMP_ATOMIC_CAPTURE_OLD:
10121 : 0 : case OMP_ATOMIC_CAPTURE_NEW:
10122 : 0 : case OMP_DEPOBJ:
10123 : 0 : case OACC_PARALLEL:
10124 : 0 : case OACC_KERNELS:
10125 : 0 : case OACC_SERIAL:
10126 : 0 : case OACC_DATA:
10127 : 0 : case OACC_HOST_DATA:
10128 : 0 : case OACC_LOOP:
10129 : 0 : case OACC_CACHE:
10130 : 0 : case OACC_DECLARE:
10131 : 0 : case OACC_ENTER_DATA:
10132 : 0 : case OACC_EXIT_DATA:
10133 : 0 : case OACC_UPDATE:
10134 : 0 : if (!ctx->quiet)
10135 : 0 : error_at (EXPR_LOCATION (t),
10136 : : "statement is not a constant expression");
10137 : 0 : *non_constant_p = true;
10138 : 0 : break;
10139 : :
10140 : 0 : default:
10141 : 0 : if (STATEMENT_CODE_P (TREE_CODE (t)))
10142 : : {
10143 : : /* This function doesn't know how to deal with pre-genericize
10144 : : statements; this can only happen with statement-expressions,
10145 : : so for now just fail. */
10146 : 0 : if (!ctx->quiet)
10147 : 0 : error_at (EXPR_LOCATION (t),
10148 : : "statement is not a constant expression");
10149 : : }
10150 : 0 : else if (flag_checking)
10151 : 0 : internal_error ("unexpected expression %qE of kind %s", t,
10152 : : get_tree_code_name (TREE_CODE (t)));
10153 : 0 : *non_constant_p = true;
10154 : 0 : break;
10155 : : }
10156 : :
10157 : 721961058 : if (r == error_mark_node)
10158 : 9203147 : *non_constant_p = true;
10159 : :
10160 : 721961058 : if (*non_constant_p)
10161 : 231786210 : return t;
10162 : : else
10163 : : return r;
10164 : : }
10165 : :
10166 : : /* P0859: A function is needed for constant evaluation if it is a constexpr
10167 : : function that is named by an expression ([basic.def.odr]) that is
10168 : : potentially constant evaluated.
10169 : :
10170 : : So we need to instantiate any constexpr functions mentioned by the
10171 : : expression even if the definition isn't needed for evaluating the
10172 : : expression. */
10173 : :
10174 : : static tree
10175 : 389385207 : instantiate_cx_fn_r (tree *tp, int *walk_subtrees, void */*data*/)
10176 : : {
10177 : 389385207 : if (TREE_CODE (*tp) == FUNCTION_DECL
10178 : 10637275 : && DECL_DECLARED_CONSTEXPR_P (*tp)
10179 : 10534475 : && !DECL_INITIAL (*tp)
10180 : 1827513 : && !trivial_fn_p (*tp)
10181 : 1827510 : && (DECL_TEMPLOID_INSTANTIATION (*tp) || DECL_DEFAULTED_FN (*tp))
10182 : 389385207 : && !uid_sensitive_constexpr_evaluation_p ())
10183 : : {
10184 : 1827450 : ++function_depth;
10185 : 1827450 : if (DECL_TEMPLOID_INSTANTIATION (*tp))
10186 : 1827445 : instantiate_decl (*tp, /*defer_ok*/false, /*expl_inst*/false);
10187 : : else
10188 : 5 : synthesize_method (*tp);
10189 : 1827450 : --function_depth;
10190 : : }
10191 : 387557757 : else if (TREE_CODE (*tp) == CALL_EXPR
10192 : 377041251 : || TREE_CODE (*tp) == AGGR_INIT_EXPR)
10193 : : {
10194 : 10687370 : if (EXPR_HAS_LOCATION (*tp))
10195 : 10687231 : input_location = EXPR_LOCATION (*tp);
10196 : : }
10197 : :
10198 : 389385207 : if (!EXPR_P (*tp))
10199 : 229698341 : *walk_subtrees = 0;
10200 : :
10201 : 389385207 : return NULL_TREE;
10202 : : }
10203 : :
10204 : : static void
10205 : 196627296 : instantiate_constexpr_fns (tree t)
10206 : : {
10207 : 196627296 : location_t loc = input_location;
10208 : 196627296 : cp_walk_tree_without_duplicates (&t, instantiate_cx_fn_r, NULL);
10209 : 196627296 : input_location = loc;
10210 : 196627296 : }
10211 : :
10212 : : /* Look for heap variables in the expression *TP. */
10213 : :
10214 : : static tree
10215 : 138493 : find_heap_var_refs (tree *tp, int *walk_subtrees, void */*data*/)
10216 : : {
10217 : 138493 : if (VAR_P (*tp)
10218 : 138493 : && (DECL_NAME (*tp) == heap_uninit_identifier
10219 : 3181 : || DECL_NAME (*tp) == heap_identifier
10220 : 353 : || DECL_NAME (*tp) == heap_vec_uninit_identifier
10221 : 309 : || DECL_NAME (*tp) == heap_vec_identifier
10222 : 21 : || DECL_NAME (*tp) == heap_deleted_identifier))
10223 : : return *tp;
10224 : :
10225 : 97777 : if (TYPE_P (*tp))
10226 : 0 : *walk_subtrees = 0;
10227 : : return NULL_TREE;
10228 : : }
10229 : :
10230 : : /* Find immediate function decls in *TP if any. */
10231 : :
10232 : : static tree
10233 : 191132655 : find_immediate_fndecl (tree *tp, int */*walk_subtrees*/, void */*data*/)
10234 : : {
10235 : 192532581 : if (TREE_CODE (*tp) == FUNCTION_DECL && DECL_IMMEDIATE_FUNCTION_P (*tp))
10236 : : return *tp;
10237 : 191131031 : if (TREE_CODE (*tp) == PTRMEM_CST
10238 : 7766 : && TREE_CODE (PTRMEM_CST_MEMBER (*tp)) == FUNCTION_DECL
10239 : 191138244 : && DECL_IMMEDIATE_FUNCTION_P (PTRMEM_CST_MEMBER (*tp)))
10240 : : return PTRMEM_CST_MEMBER (*tp);
10241 : : return NULL_TREE;
10242 : : }
10243 : :
10244 : : /* T has TREE_CONSTANT set but has been deemed not a valid C++ constant
10245 : : expression. Return a version of T that has TREE_CONSTANT cleared. */
10246 : :
10247 : : static tree
10248 : 133143 : mark_non_constant (tree t)
10249 : : {
10250 : 133143 : gcc_checking_assert (TREE_CONSTANT (t));
10251 : :
10252 : : /* This isn't actually constant, so unset TREE_CONSTANT.
10253 : : Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires
10254 : : it to be set if it is invariant address, even when it is not
10255 : : a valid C++ constant expression. Wrap it with a NOP_EXPR
10256 : : instead. */
10257 : 133143 : if (EXPR_P (t) && TREE_CODE (t) != ADDR_EXPR)
10258 : 132317 : t = copy_node (t);
10259 : 826 : else if (TREE_CODE (t) == CONSTRUCTOR)
10260 : 236 : t = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (t), t);
10261 : : else
10262 : 590 : t = build_nop (TREE_TYPE (t), t);
10263 : 133143 : TREE_CONSTANT (t) = false;
10264 : 133143 : return t;
10265 : : }
10266 : :
10267 : : /* ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
10268 : : STRICT has the same sense as for constant_value_1: true if we only allow
10269 : : conforming C++ constant expressions, or false if we want a constant value
10270 : : even if it doesn't conform.
10271 : : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
10272 : : per P0595 even when ALLOW_NON_CONSTANT is true.
10273 : : CONSTEXPR_DTOR is true when evaluating the dtor of a constexpr variable.
10274 : : OBJECT must be non-NULL in that case. */
10275 : :
10276 : : static tree
10277 : 370318914 : cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant,
10278 : : bool strict = true,
10279 : : mce_value manifestly_const_eval = mce_unknown,
10280 : : bool constexpr_dtor = false,
10281 : : tree object = NULL_TREE)
10282 : : {
10283 : 370318914 : auto_timevar time (TV_CONSTEXPR);
10284 : :
10285 : 370318914 : bool non_constant_p = false;
10286 : 370318914 : bool overflow_p = false;
10287 : :
10288 : 370318914 : if (BRACE_ENCLOSED_INITIALIZER_P (t))
10289 : : {
10290 : 0 : gcc_checking_assert (allow_non_constant);
10291 : : return t;
10292 : : }
10293 : :
10294 : 370318914 : constexpr_global_ctx global_ctx;
10295 : 370318914 : constexpr_ctx ctx = { &global_ctx, NULL, NULL, NULL, NULL, NULL, NULL,
10296 : : allow_non_constant, strict,
10297 : 370318914 : !allow_non_constant ? mce_true : manifestly_const_eval };
10298 : :
10299 : : /* Turn off -frounding-math for manifestly constant evaluation. */
10300 : 370318914 : warning_sentinel rm (flag_rounding_math,
10301 : 370318914 : ctx.manifestly_const_eval == mce_true);
10302 : 370318914 : tree type = (object
10303 : 370318914 : ? cv_unqualified (TREE_TYPE (object))
10304 : 347638971 : : initialized_type (t));
10305 : 370318914 : tree r = t;
10306 : 370318914 : bool is_consteval = false;
10307 : 370318914 : if (VOID_TYPE_P (type))
10308 : : {
10309 : 3558131 : if (!constexpr_dtor)
10310 : : {
10311 : 3558131 : if (cxx_dialect < cxx20)
10312 : : return t;
10313 : 3225268 : if (TREE_CODE (t) != CALL_EXPR && TREE_CODE (t) != AGGR_INIT_EXPR)
10314 : : return t;
10315 : : /* Calls to immediate functions returning void need to be
10316 : : evaluated. */
10317 : 3225257 : tree fndecl = cp_get_callee_fndecl_nofold (t);
10318 : 6450514 : if (fndecl == NULL_TREE || !DECL_IMMEDIATE_FUNCTION_P (fndecl))
10319 : : return t;
10320 : : else
10321 : : is_consteval = true;
10322 : : }
10323 : : }
10324 : 366760783 : else if (cxx_dialect >= cxx20
10325 : 157268829 : && (TREE_CODE (t) == CALL_EXPR
10326 : 132753103 : || TREE_CODE (t) == AGGR_INIT_EXPR
10327 : 131306904 : || TREE_CODE (t) == TARGET_EXPR))
10328 : : {
10329 : 27692836 : tree x = t;
10330 : 27692836 : if (TREE_CODE (x) == TARGET_EXPR)
10331 : 1730911 : x = TARGET_EXPR_INITIAL (x);
10332 : 27692836 : tree fndecl = cp_get_callee_fndecl_nofold (x);
10333 : 54489254 : if (fndecl && DECL_IMMEDIATE_FUNCTION_P (fndecl))
10334 : : is_consteval = true;
10335 : : /* Don't try to evaluate a std::vector constructor taking an integer, it
10336 : : will fail in the 'if (heap_var)' block below after doing all the work
10337 : : (c++/113835). This will need adjustment if P3554 is accepted. Note
10338 : : that evaluation of e.g. the vector default constructor can succeed, so
10339 : : we don't shortcut all vector constructors. */
10340 : 53592836 : if (fndecl && DECL_CONSTRUCTOR_P (fndecl) && allow_non_constant
10341 : 3864122 : && is_std_class (type, "vector") && call_expr_nargs (x) > 1
10342 : 27697222 : && TREE_CODE (TREE_TYPE (get_nth_callarg (x, 1))) == INTEGER_TYPE)
10343 : : return t;
10344 : : }
10345 : 366760442 : if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
10346 : : {
10347 : : /* In C++14 an NSDMI can participate in aggregate initialization,
10348 : : and can refer to the address of the object being initialized, so
10349 : : we need to pass in the relevant VAR_DECL if we want to do the
10350 : : evaluation in a single pass. The evaluation will dynamically
10351 : : update ctx.values for the VAR_DECL. We use the same strategy
10352 : : for C++11 constexpr constructors that refer to the object being
10353 : : initialized. */
10354 : 23791941 : if (constexpr_dtor)
10355 : : {
10356 : 147 : gcc_assert (object && VAR_P (object));
10357 : 147 : gcc_assert (DECL_DECLARED_CONSTEXPR_P (object));
10358 : 147 : gcc_assert (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object));
10359 : 147 : if (error_operand_p (DECL_INITIAL (object)))
10360 : : return t;
10361 : 136 : ctx.ctor = unshare_expr (DECL_INITIAL (object));
10362 : 136 : TREE_READONLY (ctx.ctor) = false;
10363 : : /* Temporarily force decl_really_constant_value to return false
10364 : : for it, we want to use ctx.ctor for the current value instead. */
10365 : 136 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = false;
10366 : : }
10367 : : else
10368 : : {
10369 : 23791794 : ctx.ctor = build_constructor (type, NULL);
10370 : 23791794 : CONSTRUCTOR_NO_CLEARING (ctx.ctor) = true;
10371 : : }
10372 : 23791930 : if (!object)
10373 : : {
10374 : 13282388 : if (TREE_CODE (t) == CALL_EXPR)
10375 : : {
10376 : : /* If T is calling a constructor to initialize an object, reframe
10377 : : it as an AGGR_INIT_EXPR to avoid trying to modify an object
10378 : : from outside the constant evaluation, which will fail even if
10379 : : the value is actually constant (is_constant_evaluated3.C). */
10380 : 5132947 : tree fn = cp_get_callee_fndecl_nofold (t);
10381 : 10265886 : if (fn && DECL_CONSTRUCTOR_P (fn))
10382 : : {
10383 : 2303455 : object = CALL_EXPR_ARG (t, 0);
10384 : 2303455 : object = build_fold_indirect_ref (object);
10385 : 2303455 : r = build_aggr_init_expr (type, r);
10386 : : }
10387 : : }
10388 : 8149441 : else if (TREE_CODE (t) == TARGET_EXPR)
10389 : 3185084 : object = TARGET_EXPR_SLOT (t);
10390 : 4964357 : else if (TREE_CODE (t) == AGGR_INIT_EXPR)
10391 : 438490 : object = AGGR_INIT_EXPR_SLOT (t);
10392 : : }
10393 : 23791930 : ctx.object = object;
10394 : 23791930 : if (object)
10395 : 16436571 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
10396 : : (type, TREE_TYPE (object)));
10397 : 16436571 : if (object && DECL_P (object))
10398 : 13922353 : global_ctx.put_value (object, ctx.ctor);
10399 : 23791930 : if (TREE_CODE (r) == TARGET_EXPR)
10400 : : /* Avoid creating another CONSTRUCTOR when we expand the
10401 : : TARGET_EXPR. */
10402 : 3243499 : r = TARGET_EXPR_INITIAL (r);
10403 : : }
10404 : :
10405 : 366760431 : auto_vec<tree, 16> cleanups;
10406 : 366760431 : global_ctx.cleanups = &cleanups;
10407 : :
10408 : 366760431 : if (manifestly_const_eval == mce_true)
10409 : 196627296 : instantiate_constexpr_fns (r);
10410 : 366760431 : tree jmp_target = NULL_TREE;
10411 : 366760431 : r = cxx_eval_constant_expression (&ctx, r, vc_prvalue,
10412 : : &non_constant_p, &overflow_p,
10413 : : &jmp_target);
10414 : 366758004 : if (throws (&jmp_target) && !non_constant_p)
10415 : : {
10416 : 270 : if (!ctx.quiet)
10417 : 75 : diagnose_uncaught_exception (input_location, &ctx, jmp_target);
10418 : 270 : non_constant_p = true;
10419 : 270 : jmp_target = NULL_TREE;
10420 : 270 : r = t;
10421 : : }
10422 : 366757464 : else if (!non_constant_p && jmp_target)
10423 : : {
10424 : 24 : non_constant_p = true;
10425 : 24 : if (!ctx.quiet)
10426 : : {
10427 : 3 : if (breaks (&jmp_target))
10428 : 3 : error ("%<break%> outside of a loop or %<switch%>");
10429 : 0 : else if (continues (&jmp_target))
10430 : 0 : error ("%<continue%> outside of a loop");
10431 : 0 : else if (returns (&jmp_target))
10432 : 0 : error ("%<return%> in a statement expression");
10433 : : else
10434 : 0 : gcc_unreachable ();
10435 : : }
10436 : 24 : r = t;
10437 : : }
10438 : :
10439 : : /* If we got a non-simple TARGET_EXPR, the initializer was a sequence
10440 : : of statements, and the result ought to be stored in ctx.ctor. */
10441 : 366757734 : if (r == void_node && !constexpr_dtor && ctx.ctor)
10442 : 0 : r = ctx.ctor;
10443 : :
10444 : 366757734 : unsigned int i;
10445 : 366757734 : tree cleanup;
10446 : 366757734 : jmp_target = NULL_TREE;
10447 : : /* Evaluate the cleanups. */
10448 : 733534787 : FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup)
10449 : 19319 : if (cleanup == NULL_TREE)
10450 : : /* NULL_TREE cleanup is a marker that before it is
10451 : : CLEANUP_EH_ONLY cleanup. Skip the cleanup before it. */
10452 : 20 : --i;
10453 : : else
10454 : 19299 : cxx_eval_constant_expression (&ctx, cleanup, vc_discard,
10455 : : &non_constant_p, &overflow_p,
10456 : : &jmp_target);
10457 : 366757734 : if (throws (&jmp_target) && !non_constant_p)
10458 : : {
10459 : 0 : if (!ctx.quiet)
10460 : 0 : diagnose_uncaught_exception (input_location, &ctx, jmp_target);
10461 : 0 : non_constant_p = true;
10462 : 0 : r = t;
10463 : : }
10464 : :
10465 : : /* Mutable logic is a bit tricky: we want to allow initialization of
10466 : : constexpr variables with mutable members, but we can't copy those
10467 : : members to another constexpr variable. */
10468 : 366757734 : if (!non_constant_p
10469 : 276559674 : && TREE_CODE (r) == CONSTRUCTOR
10470 : 376831969 : && CONSTRUCTOR_MUTABLE_POISON (r))
10471 : : {
10472 : 93 : if (!allow_non_constant)
10473 : 6 : error ("%qE is not a constant expression because it refers to "
10474 : : "mutable subobjects of %qT", t, type);
10475 : 93 : non_constant_p = true;
10476 : : }
10477 : :
10478 : 276559581 : if (!non_constant_p && cxx_dialect >= cxx20
10479 : 643317315 : && !global_ctx.heap_vars.is_empty ())
10480 : : {
10481 : 41248 : tree heap_var = cp_walk_tree_without_duplicates (&r, find_heap_var_refs,
10482 : : NULL);
10483 : 41248 : unsigned int i;
10484 : 41248 : if (heap_var)
10485 : : {
10486 : 40716 : if (!allow_non_constant && !non_constant_p)
10487 : : {
10488 : 11 : if (DECL_LANG_SPECIFIC (heap_var))
10489 : 2 : error ("%qE is not a constant expression because it refers to "
10490 : : "exception object allocated with "
10491 : : "%<__cxa_allocate_exception%>", t);
10492 : : else
10493 : 9 : error ("%qE is not a constant expression because it refers to "
10494 : : "a result of %<operator new%>", t);
10495 : 11 : inform (DECL_SOURCE_LOCATION (heap_var), "allocated here");
10496 : : }
10497 : 40716 : r = t;
10498 : 40716 : non_constant_p = true;
10499 : : }
10500 : 83416 : FOR_EACH_VEC_ELT (global_ctx.heap_vars, i, heap_var)
10501 : : {
10502 : 42168 : if (DECL_NAME (heap_var) != heap_deleted_identifier)
10503 : : {
10504 : 40775 : if (!allow_non_constant && !non_constant_p)
10505 : : {
10506 : 16 : error ("%qE is not a constant expression because allocated "
10507 : : "storage has not been deallocated", t);
10508 : 16 : inform (DECL_SOURCE_LOCATION (heap_var), "allocated here");
10509 : : }
10510 : 40775 : r = t;
10511 : 40775 : non_constant_p = true;
10512 : : }
10513 : : }
10514 : : }
10515 : :
10516 : : /* Check that immediate invocation does not return an expression referencing
10517 : : any immediate function decls. */
10518 : 366757734 : if (!non_constant_p && cxx_dialect >= cxx20)
10519 : 229626110 : if (tree immediate_fndecl
10520 : 114813055 : = cp_walk_tree_without_duplicates (&r, find_immediate_fndecl,
10521 : : NULL))
10522 : : {
10523 : 1729 : if (!allow_non_constant && !non_constant_p)
10524 : : {
10525 : 72 : if (is_consteval)
10526 : 36 : error_at (cp_expr_loc_or_input_loc (t),
10527 : : "immediate evaluation returns address of immediate "
10528 : : "function %qD", immediate_fndecl);
10529 : : else
10530 : 54 : error_at (cp_expr_loc_or_input_loc (t),
10531 : : "constant evaluation returns address of immediate "
10532 : : "function %qD", immediate_fndecl);
10533 : : }
10534 : 1729 : r = t;
10535 : 1729 : non_constant_p = true;
10536 : : }
10537 : :
10538 : 366757734 : if (!non_constant_p && !constexpr_dtor)
10539 : 276516941 : verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p);
10540 : :
10541 : : /* After verify_constant because reduced_constant_expression_p can unset
10542 : : CONSTRUCTOR_NO_CLEARING. */
10543 : 366757734 : if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_NO_CLEARING (r))
10544 : : {
10545 : 62391 : if (!allow_non_constant)
10546 : 40 : error ("%qE is not a constant expression because it refers to "
10547 : : "an incompletely initialized variable", t);
10548 : 62391 : TREE_CONSTANT (r) = false;
10549 : 62391 : non_constant_p = true;
10550 : : }
10551 : :
10552 : 366757734 : if (non_constant_p)
10553 : : /* If we saw something bad, go back to our argument. The wrapping below is
10554 : : only for the cases of TREE_CONSTANT argument or overflow. */
10555 : 92242175 : r = t;
10556 : :
10557 : 366757734 : if (!non_constant_p && overflow_p)
10558 : 215 : non_constant_p = true;
10559 : :
10560 : : /* Unshare the result. */
10561 : 366757734 : bool should_unshare = true;
10562 : 366757734 : if (r == t || (TREE_CODE (t) == TARGET_EXPR
10563 : 1133066 : && TARGET_EXPR_INITIAL (t) == r))
10564 : : should_unshare = false;
10565 : :
10566 : 366757734 : if (non_constant_p && !allow_non_constant)
10567 : 2473 : return error_mark_node;
10568 : 366755261 : else if (non_constant_p && TREE_CONSTANT (r))
10569 : 128822 : r = mark_non_constant (r);
10570 : 366626439 : else if (non_constant_p)
10571 : : return t;
10572 : :
10573 : 274644166 : if (constexpr_dtor)
10574 : : {
10575 : 118 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = true;
10576 : 118 : return r;
10577 : : }
10578 : :
10579 : : /* Check we are not trying to return the wrong type. */
10580 : 274644048 : if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (r)))
10581 : : {
10582 : : /* If so, this is not a constant expression. */
10583 : 131 : if (!allow_non_constant)
10584 : 0 : error ("%qE is not a constant expression because it initializes "
10585 : 0 : "a %qT rather than %qT", t, TREE_TYPE (t), type);
10586 : 131 : return t;
10587 : : }
10588 : :
10589 : 274643917 : if (should_unshare)
10590 : 140983371 : r = unshare_expr (r);
10591 : :
10592 : 274643917 : if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
10593 : : {
10594 : 9418732 : r = adjust_temp_type (type, r);
10595 : 9418732 : if (TREE_CODE (t) == TARGET_EXPR
10596 : 9418732 : && TARGET_EXPR_INITIAL (t) == r)
10597 : : return t;
10598 : 8522447 : else if (TREE_CODE (t) == CONSTRUCTOR || TREE_CODE (t) == CALL_EXPR
10599 : 1128099 : || TREE_CODE (t) == AGGR_INIT_EXPR)
10600 : : /* Don't add a TARGET_EXPR if our argument didn't have one. */;
10601 : 257310 : else if (TREE_CODE (t) == TARGET_EXPR && TARGET_EXPR_CLEANUP (t))
10602 : 1233 : r = get_target_expr (r);
10603 : : else
10604 : : {
10605 : 256077 : r = get_target_expr (r, tf_warning_or_error | tf_no_cleanup);
10606 : 256077 : TREE_CONSTANT (r) = true;
10607 : : }
10608 : : }
10609 : :
10610 : 273747632 : if (TREE_CODE (t) == TARGET_EXPR
10611 : 236781 : && TREE_CODE (r) == TARGET_EXPR)
10612 : : {
10613 : : /* Preserve this flag for potential_constant_expression, and the others
10614 : : for good measure. */
10615 : 236720 : TARGET_EXPR_ELIDING_P (r) = TARGET_EXPR_ELIDING_P (t);
10616 : 236720 : TARGET_EXPR_IMPLICIT_P (r) = TARGET_EXPR_IMPLICIT_P (t);
10617 : 236720 : TARGET_EXPR_LIST_INIT_P (r) = TARGET_EXPR_LIST_INIT_P (t);
10618 : 236720 : TARGET_EXPR_DIRECT_INIT_P (r) = TARGET_EXPR_DIRECT_INIT_P (t);
10619 : : }
10620 : :
10621 : : /* Remember the original location if that wouldn't need a wrapper. */
10622 : 273747632 : if (location_t loc = EXPR_LOCATION (t))
10623 : 112034703 : protected_set_expr_location (r, loc);
10624 : :
10625 : 273747632 : return r;
10626 : 370316217 : }
10627 : :
10628 : : /* If T represents a constant expression returns its reduced value.
10629 : : Otherwise return error_mark_node. */
10630 : :
10631 : : tree
10632 : 121519268 : cxx_constant_value (tree t, tree decl /* = NULL_TREE */,
10633 : : tsubst_flags_t complain /* = tf_error */)
10634 : : {
10635 : 121519268 : bool sfinae = !(complain & tf_error);
10636 : 121519268 : tree r = cxx_eval_outermost_constant_expr (t, sfinae, true, mce_true, false, decl);
10637 : 121519268 : if (sfinae && !TREE_CONSTANT (r))
10638 : 1026 : r = error_mark_node;
10639 : 121519268 : return r;
10640 : : }
10641 : :
10642 : : /* Like cxx_constant_value, but used for evaluation of constexpr destructors
10643 : : of constexpr variables. The actual initializer of DECL is not modified. */
10644 : :
10645 : : void
10646 : 147 : cxx_constant_dtor (tree t, tree decl)
10647 : : {
10648 : 147 : cxx_eval_outermost_constant_expr (t, false, true, mce_true, true, decl);
10649 : 147 : }
10650 : :
10651 : : /* Helper routine for fold_simple function. Either return simplified
10652 : : expression T, otherwise NULL_TREE.
10653 : : In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold
10654 : : even if we are within template-declaration. So be careful on call, as in
10655 : : such case types can be undefined. */
10656 : :
10657 : : static tree
10658 : 106338818 : fold_simple_1 (tree t)
10659 : : {
10660 : 106338818 : tree op1;
10661 : 106338818 : enum tree_code code = TREE_CODE (t);
10662 : :
10663 : 106338818 : switch (code)
10664 : : {
10665 : : case INTEGER_CST:
10666 : : case REAL_CST:
10667 : : case VECTOR_CST:
10668 : : case FIXED_CST:
10669 : : case COMPLEX_CST:
10670 : : return t;
10671 : :
10672 : 927502 : case SIZEOF_EXPR:
10673 : 927502 : return fold_sizeof_expr (t);
10674 : :
10675 : 30192845 : case ABS_EXPR:
10676 : 30192845 : case ABSU_EXPR:
10677 : 30192845 : case CONJ_EXPR:
10678 : 30192845 : case REALPART_EXPR:
10679 : 30192845 : case IMAGPART_EXPR:
10680 : 30192845 : case NEGATE_EXPR:
10681 : 30192845 : case BIT_NOT_EXPR:
10682 : 30192845 : case TRUTH_NOT_EXPR:
10683 : 30192845 : case VIEW_CONVERT_EXPR:
10684 : 30192845 : CASE_CONVERT:
10685 : 30192845 : case FLOAT_EXPR:
10686 : 30192845 : case FIX_TRUNC_EXPR:
10687 : 30192845 : case FIXED_CONVERT_EXPR:
10688 : 30192845 : case ADDR_SPACE_CONVERT_EXPR:
10689 : :
10690 : 30192845 : op1 = TREE_OPERAND (t, 0);
10691 : :
10692 : 30192845 : t = const_unop (code, TREE_TYPE (t), op1);
10693 : 30192845 : if (!t)
10694 : : return NULL_TREE;
10695 : :
10696 : 1980396 : if (CONVERT_EXPR_CODE_P (code)
10697 : 1980396 : && TREE_OVERFLOW_P (t) && !TREE_OVERFLOW_P (op1))
10698 : 0 : TREE_OVERFLOW (t) = false;
10699 : : return t;
10700 : :
10701 : : default:
10702 : : return NULL_TREE;
10703 : : }
10704 : : }
10705 : :
10706 : : /* If T is a simple constant expression, returns its simplified value.
10707 : : Otherwise returns T. In contrast to maybe_constant_value we
10708 : : simplify only few operations on constant-expressions, and we don't
10709 : : try to simplify constexpressions. */
10710 : :
10711 : : tree
10712 : 106859185 : fold_simple (tree t)
10713 : : {
10714 : 106859185 : if (processing_template_decl)
10715 : : return t;
10716 : :
10717 : 106338818 : tree r = fold_simple_1 (t);
10718 : 106338818 : if (r)
10719 : : return r;
10720 : :
10721 : : return t;
10722 : : }
10723 : :
10724 : : /* Try folding the expression T to a simple constant.
10725 : : Returns that constant, otherwise returns T. */
10726 : :
10727 : : tree
10728 : 973398 : fold_to_constant (tree t)
10729 : : {
10730 : 973398 : if (processing_template_decl)
10731 : : return t;
10732 : :
10733 : 876823 : tree r = fold (t);
10734 : 876823 : if (CONSTANT_CLASS_P (r) && !TREE_OVERFLOW (r))
10735 : : return r;
10736 : : else
10737 : : return t;
10738 : : }
10739 : :
10740 : : /* If T is a constant expression, returns its reduced value.
10741 : : Otherwise, if T does not have TREE_CONSTANT set, returns T.
10742 : : Otherwise, returns a version of T without TREE_CONSTANT.
10743 : : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated
10744 : : as per P0595. */
10745 : :
10746 : : static GTY((deletable)) hash_map<tree, tree> *cv_cache;
10747 : :
10748 : : tree
10749 : 539433984 : maybe_constant_value (tree t, tree decl /* = NULL_TREE */,
10750 : : mce_value manifestly_const_eval /* = mce_unknown */)
10751 : : {
10752 : 539433984 : tree orig_t = t;
10753 : 539433984 : tree r;
10754 : :
10755 : 539433984 : if (EXPR_P (t) && manifestly_const_eval == mce_unknown)
10756 : : {
10757 : : /* Look up each operand in the cv_cache first to see if we've already
10758 : : reduced it, and reuse that result to avoid quadratic behavior if
10759 : : we're called when building up a large expression. */
10760 : 196636466 : int n = cp_tree_operand_length (t);
10761 : 196636466 : tree *ops = XALLOCAVEC (tree, n);
10762 : 196636466 : bool rebuild = false;
10763 : 559659006 : for (int i = 0; i < n; ++i)
10764 : : {
10765 : 363022540 : ops[i] = TREE_OPERAND (t, i);
10766 : 1087931729 : if (tree *cached = hash_map_safe_get (cv_cache, ops[i]))
10767 : 26442304 : if (*cached != ops[i])
10768 : : {
10769 : 7687892 : ops[i] = *cached;
10770 : 7687892 : rebuild = true;
10771 : : }
10772 : : }
10773 : 196636466 : if (rebuild)
10774 : : {
10775 : 6676302 : t = copy_node (t);
10776 : 21751270 : for (int i = 0; i < n; ++i)
10777 : 15074968 : TREE_OPERAND (t, i) = ops[i];
10778 : : }
10779 : : }
10780 : :
10781 : 539433984 : if (!is_nondependent_constant_expression (t))
10782 : : {
10783 : 0 : if (TREE_OVERFLOW_P (t)
10784 : 117930288 : || (!processing_template_decl && TREE_CONSTANT (t)))
10785 : 4321 : t = mark_non_constant (t);
10786 : 117930288 : return t;
10787 : : }
10788 : 421503696 : else if (CONSTANT_CLASS_P (t))
10789 : : /* No caching or evaluation needed. */
10790 : : return t;
10791 : :
10792 : : /* Don't constant evaluate an unevaluated non-manifestly-constant operand,
10793 : : but at least try folding it to a simple constant. */
10794 : 217392954 : if (cp_unevaluated_operand && manifestly_const_eval != mce_true)
10795 : 449001 : return fold_to_constant (t);
10796 : :
10797 : 212176214 : if (manifestly_const_eval != mce_unknown)
10798 : : /* TODO: Extend the cache to be mce_value aware. And if we have a
10799 : : previously cached mce_unknown result that's TREE_CONSTANT, it means
10800 : : the reduced value is independent of mce_value and so we should
10801 : : be able to reuse it in the mce_true/false case. */
10802 : 99301339 : return cxx_eval_outermost_constant_expr (t, true, true,
10803 : 99298642 : manifestly_const_eval, false, decl);
10804 : :
10805 : 117642614 : if (cv_cache == NULL)
10806 : 137886 : cv_cache = hash_map<tree, tree>::create_ggc (101);
10807 : 117642614 : if (tree *cached = cv_cache->get (t))
10808 : : {
10809 : 9119551 : r = *cached;
10810 : 9119551 : if (r != t)
10811 : : {
10812 : : /* Clear processing_template_decl for sake of break_out_target_exprs;
10813 : : entries in the cv_cache are non-templated. */
10814 : 2708713 : processing_template_decl_sentinel ptds;
10815 : :
10816 : 2708713 : r = break_out_target_exprs (r, /*clear_loc*/true);
10817 : 2708713 : protected_set_expr_location (r, EXPR_LOCATION (t));
10818 : 2708713 : }
10819 : 9119551 : return r;
10820 : : }
10821 : :
10822 : 108523063 : uid_sensitive_constexpr_evaluation_checker c;
10823 : 108523063 : r = cxx_eval_outermost_constant_expr (t, true, true,
10824 : : manifestly_const_eval, false, decl);
10825 : 108523063 : gcc_checking_assert (r == t
10826 : : || CONVERT_EXPR_P (t)
10827 : : || TREE_CODE (t) == VIEW_CONVERT_EXPR
10828 : : || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
10829 : : || !cp_tree_equal (r, t));
10830 : 108523063 : if (!c.evaluation_restricted_p ())
10831 : 108324426 : cv_cache->put (orig_t, r);
10832 : : return r;
10833 : : }
10834 : :
10835 : : /* Dispose of the whole CV_CACHE. */
10836 : :
10837 : : static void
10838 : 25243159 : clear_cv_cache (void)
10839 : : {
10840 : 25243159 : if (cv_cache != NULL)
10841 : 25028253 : cv_cache->empty ();
10842 : 25243159 : }
10843 : :
10844 : : /* Dispose of the whole CV_CACHE and FOLD_CACHE. */
10845 : :
10846 : : void
10847 : 25243159 : clear_cv_and_fold_caches ()
10848 : : {
10849 : 25243159 : clear_cv_cache ();
10850 : 25243159 : clear_fold_cache ();
10851 : 25243159 : }
10852 : :
10853 : : /* Internal function handling expressions in templates for
10854 : : fold_non_dependent_expr and fold_non_dependent_init.
10855 : :
10856 : : If we're in a template, but T isn't value dependent, simplify
10857 : : it. We're supposed to treat:
10858 : :
10859 : : template <typename T> void f(T[1 + 1]);
10860 : : template <typename T> void f(T[2]);
10861 : :
10862 : : as two declarations of the same function, for example. */
10863 : :
10864 : : static tree
10865 : 25244939 : fold_non_dependent_expr_template (tree t, tsubst_flags_t complain,
10866 : : bool manifestly_const_eval,
10867 : : tree object)
10868 : : {
10869 : 25244939 : gcc_assert (processing_template_decl);
10870 : :
10871 : 25244939 : if (is_nondependent_constant_expression (t))
10872 : : {
10873 : 13736362 : processing_template_decl_sentinel s;
10874 : 13736362 : t = instantiate_non_dependent_expr_internal (t, complain);
10875 : :
10876 : 13736362 : if (type_unknown_p (t) || BRACE_ENCLOSED_INITIALIZER_P (t))
10877 : : {
10878 : 0 : if (TREE_OVERFLOW_P (t))
10879 : : {
10880 : 0 : t = build_nop (TREE_TYPE (t), t);
10881 : 0 : TREE_CONSTANT (t) = false;
10882 : : }
10883 : 0 : return t;
10884 : : }
10885 : 13736362 : else if (CONSTANT_CLASS_P (t))
10886 : : /* No evaluation needed. */
10887 : : return t;
10888 : :
10889 : : /* Don't constant evaluate an unevaluated non-manifestly-constant operand,
10890 : : but at least try folding it to a simple constant. */
10891 : 3533583 : if (cp_unevaluated_operand && !manifestly_const_eval)
10892 : 89 : return fold_to_constant (t);
10893 : :
10894 : 3533494 : tree r = cxx_eval_outermost_constant_expr (t, true, true,
10895 : : mce_value (manifestly_const_eval),
10896 : : false, object);
10897 : : /* cp_tree_equal looks through NOPs, so allow them. */
10898 : 3533494 : gcc_checking_assert (r == t
10899 : : || CONVERT_EXPR_P (t)
10900 : : || TREE_CODE (t) == VIEW_CONVERT_EXPR
10901 : : || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
10902 : : || !cp_tree_equal (r, t));
10903 : 3533494 : return r;
10904 : 13736362 : }
10905 : 11508577 : else if (TREE_OVERFLOW_P (t))
10906 : : {
10907 : 0 : t = build_nop (TREE_TYPE (t), t);
10908 : 0 : TREE_CONSTANT (t) = false;
10909 : : }
10910 : :
10911 : : return t;
10912 : : }
10913 : :
10914 : : /* Like maybe_constant_value but first fully instantiate the argument.
10915 : :
10916 : : Note: this is equivalent to instantiate_non_dependent_expr (t, complain)
10917 : : followed by maybe_constant_value but is more efficient,
10918 : : because it calls instantiation_dependent_expression_p and
10919 : : potential_constant_expression at most once.
10920 : : The manifestly_const_eval argument is passed to maybe_constant_value.
10921 : :
10922 : : Callers should generally pass their active complain, or if they are in a
10923 : : non-template, diagnosing context, they can use the default of
10924 : : tf_warning_or_error. Callers that might be within a template context, don't
10925 : : have a complain parameter, and aren't going to remember the result for long
10926 : : (e.g. null_ptr_cst_p), can pass tf_none and deal with error_mark_node
10927 : : appropriately. */
10928 : :
10929 : : tree
10930 : 118465488 : fold_non_dependent_expr (tree t,
10931 : : tsubst_flags_t complain /* = tf_warning_or_error */,
10932 : : bool manifestly_const_eval /* = false */,
10933 : : tree object /* = NULL_TREE */)
10934 : : {
10935 : 118465488 : if (t == NULL_TREE)
10936 : : return NULL_TREE;
10937 : :
10938 : 117970639 : if (processing_template_decl)
10939 : 24255434 : return fold_non_dependent_expr_template (t, complain,
10940 : 24255434 : manifestly_const_eval, object);
10941 : :
10942 : 93715205 : return maybe_constant_value (t, object, mce_value (manifestly_const_eval));
10943 : : }
10944 : :
10945 : : /* Like fold_non_dependent_expr, but if EXPR couldn't be folded to a constant,
10946 : : return the original expression. */
10947 : :
10948 : : tree
10949 : 2024601 : maybe_fold_non_dependent_expr (tree expr,
10950 : : tsubst_flags_t complain/*=tf_warning_or_error*/)
10951 : : {
10952 : 2024601 : tree t = fold_non_dependent_expr (expr, complain);
10953 : 2024601 : if (t && TREE_CONSTANT (t))
10954 : 1007710 : return t;
10955 : :
10956 : : return expr;
10957 : : }
10958 : :
10959 : : /* Like maybe_constant_init but first fully instantiate the argument. */
10960 : :
10961 : : tree
10962 : 26573141 : fold_non_dependent_init (tree t,
10963 : : tsubst_flags_t complain /*=tf_warning_or_error*/,
10964 : : bool manifestly_const_eval /*=false*/,
10965 : : tree object /* = NULL_TREE */)
10966 : : {
10967 : 26573141 : if (t == NULL_TREE)
10968 : : return NULL_TREE;
10969 : :
10970 : 26573141 : if (processing_template_decl)
10971 : : {
10972 : 989505 : t = fold_non_dependent_expr_template (t, complain,
10973 : : manifestly_const_eval, object);
10974 : : /* maybe_constant_init does this stripping, so do it here too. */
10975 : 989505 : if (TREE_CODE (t) == TARGET_EXPR)
10976 : : {
10977 : 61 : tree init = TARGET_EXPR_INITIAL (t);
10978 : 61 : if (TREE_CODE (init) == CONSTRUCTOR)
10979 : 989505 : t = init;
10980 : : }
10981 : 989505 : return t;
10982 : : }
10983 : :
10984 : 25583636 : return maybe_constant_init (t, object, manifestly_const_eval);
10985 : : }
10986 : :
10987 : : /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
10988 : : than wrapped in a TARGET_EXPR.
10989 : : ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
10990 : : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
10991 : : per P0595 even when ALLOW_NON_CONSTANT is true. */
10992 : :
10993 : : static tree
10994 : 61480651 : maybe_constant_init_1 (tree t, tree decl, bool allow_non_constant,
10995 : : mce_value manifestly_const_eval)
10996 : : {
10997 : 61480651 : if (!t)
10998 : : return t;
10999 : 61480651 : if (TREE_CODE (t) == EXPR_STMT)
11000 : 23470 : t = TREE_OPERAND (t, 0);
11001 : 61480651 : if (TREE_CODE (t) == CONVERT_EXPR
11002 : 61480651 : && VOID_TYPE_P (TREE_TYPE (t)))
11003 : 33875 : t = TREE_OPERAND (t, 0);
11004 : : /* If the types don't match, the INIT_EXPR is initializing a subobject of
11005 : : DECL and losing that information would cause mischief later. */
11006 : 61480651 : if (TREE_CODE (t) == INIT_EXPR
11007 : 61480651 : && (!decl
11008 : 10416 : || same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (decl),
11009 : 10416 : TREE_TYPE (t))))
11010 : 24423 : t = TREE_OPERAND (t, 1);
11011 : 61480651 : if (TREE_CODE (t) == TARGET_EXPR)
11012 : 434340 : t = TARGET_EXPR_INITIAL (t);
11013 : 61480651 : if (!is_nondependent_static_init_expression (t))
11014 : : /* Don't try to evaluate it. */;
11015 : 52885420 : else if (CONSTANT_CLASS_P (t) && TREE_CODE (t) != PTRMEM_CST)
11016 : : /* No evaluation needed. PTRMEM_CST needs the immediate fn check. */;
11017 : : else
11018 : : {
11019 : : /* [basic.start.static] allows constant-initialization of variables with
11020 : : static or thread storage duration even if it isn't required, but we
11021 : : shouldn't bend the rules the same way for automatic variables.
11022 : :
11023 : : But still enforce the requirements of constexpr/constinit.
11024 : : [dcl.constinit] "If a variable declared with the constinit specifier
11025 : : has dynamic initialization, the program is ill-formed, even if the
11026 : : implementation would perform that initialization as a static
11027 : : initialization." */
11028 : 11238118 : bool is_static = (decl && DECL_P (decl)
11029 : 36438880 : && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)));
11030 : 1672808 : bool strict = (!is_static
11031 : : || (decl && DECL_P (decl)
11032 : 1672808 : && (DECL_DECLARED_CONSTEXPR_P (decl)
11033 : 893828 : || DECL_DECLARED_CONSTINIT_P (decl))));
11034 : : if (is_static)
11035 : : manifestly_const_eval = mce_true;
11036 : :
11037 : 25612198 : if (cp_unevaluated_operand && manifestly_const_eval != mce_true)
11038 : 35453 : return fold_to_constant (t);
11039 : :
11040 : 25576745 : t = cxx_eval_outermost_constant_expr (t, allow_non_constant, strict,
11041 : : manifestly_const_eval,
11042 : : false, decl);
11043 : : }
11044 : 61445198 : if (TREE_CODE (t) == TARGET_EXPR)
11045 : : {
11046 : 20128 : tree init = TARGET_EXPR_INITIAL (t);
11047 : 20128 : if (TREE_CODE (init) == CONSTRUCTOR)
11048 : 61480651 : t = init;
11049 : : }
11050 : : return t;
11051 : : }
11052 : :
11053 : : /* Wrapper for maybe_constant_init_1 which permits non constants. */
11054 : :
11055 : : tree
11056 : 46268120 : maybe_constant_init (tree t, tree decl, bool manifestly_const_eval)
11057 : : {
11058 : 46268120 : return maybe_constant_init_1 (t, decl, true, mce_value (manifestly_const_eval));
11059 : : }
11060 : :
11061 : : tree
11062 : 15211106 : maybe_constant_init (tree t, tree decl, mce_value manifestly_const_eval)
11063 : : {
11064 : 15211106 : return maybe_constant_init_1 (t, decl, true, manifestly_const_eval);
11065 : : }
11066 : :
11067 : : /* Wrapper for maybe_constant_init_1 which does not permit non constants. */
11068 : :
11069 : : tree
11070 : 1425 : cxx_constant_init (tree t, tree decl)
11071 : : {
11072 : 1425 : return maybe_constant_init_1 (t, decl, false, mce_true);
11073 : : }
11074 : :
11075 : : /* Return true if CALL_EXPR T might throw during constant evaluation. */
11076 : :
11077 : : static bool
11078 : 132626636 : callee_might_throw (tree t)
11079 : : {
11080 : 132626636 : if (cxx_dialect < cxx26 || !flag_exceptions)
11081 : : return false;
11082 : 36050000 : tree callee = cp_get_callee (t);
11083 : 36050000 : if (callee == NULL_TREE)
11084 : : return false;
11085 : 35999051 : tree callee_fn = cp_get_fndecl_from_callee (callee, false);
11086 : 35999051 : return (!flag_enforce_eh_specs
11087 : 35999051 : || type_dependent_expression_p (callee)
11088 : 32252829 : || !POINTER_TYPE_P (TREE_TYPE (callee))
11089 : 65596332 : || (!type_noexcept_p (TREE_TYPE (TREE_TYPE (callee)))
11090 : 11727301 : && (callee_fn == NULL_TREE || !TREE_NOTHROW (callee_fn))));
11091 : : }
11092 : :
11093 : : #if 0
11094 : : /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
11095 : : /* Return true if the object referred to by REF has automatic or thread
11096 : : local storage. */
11097 : :
11098 : : enum { ck_ok, ck_bad, ck_unknown };
11099 : : static int
11100 : : check_automatic_or_tls (tree ref)
11101 : : {
11102 : : machine_mode mode;
11103 : : poly_int64 bitsize, bitpos;
11104 : : tree offset;
11105 : : int volatilep = 0, unsignedp = 0;
11106 : : tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset,
11107 : : &mode, &unsignedp, &volatilep, false);
11108 : : duration_kind dk;
11109 : :
11110 : : /* If there isn't a decl in the middle, we don't know the linkage here,
11111 : : and this isn't a constant expression anyway. */
11112 : : if (!DECL_P (decl))
11113 : : return ck_unknown;
11114 : : dk = decl_storage_duration (decl);
11115 : : return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok;
11116 : : }
11117 : : #endif
11118 : :
11119 : : /* Data structure for passing data from potential_constant_expression_1
11120 : : to check_for_return_continue via cp_walk_tree. */
11121 : : struct check_for_return_continue_data {
11122 : : hash_set<tree> *pset;
11123 : : tree continue_stmt;
11124 : : tree break_stmt;
11125 : : bool could_throw;
11126 : : };
11127 : :
11128 : : /* Helper function for potential_constant_expression_1 SWITCH_STMT handling,
11129 : : called through cp_walk_tree. Return the first RETURN_EXPR found, or note
11130 : : the first CONTINUE_STMT and/or BREAK_STMT if RETURN_EXPR is not found.
11131 : : For C++26 also note presence of possibly throwing calls. */
11132 : : static tree
11133 : 22175824 : check_for_return_continue (tree *tp, int *walk_subtrees, void *data)
11134 : : {
11135 : 22175824 : tree t = *tp, s, b;
11136 : 22175824 : check_for_return_continue_data *d = (check_for_return_continue_data *) data;
11137 : 22175824 : switch (TREE_CODE (t))
11138 : : {
11139 : : case RETURN_EXPR:
11140 : : return t;
11141 : :
11142 : 30 : case CONTINUE_STMT:
11143 : 30 : if (d->continue_stmt == NULL_TREE)
11144 : 30 : d->continue_stmt = t;
11145 : : break;
11146 : :
11147 : 5340 : case BREAK_STMT:
11148 : 5340 : if (d->break_stmt == NULL_TREE)
11149 : 3215 : d->break_stmt = t;
11150 : : break;
11151 : :
11152 : : #define RECUR(x) \
11153 : : if (tree r = cp_walk_tree (&x, check_for_return_continue, data, \
11154 : : d->pset)) \
11155 : : return r
11156 : :
11157 : : /* For loops, walk subtrees manually, so that continue stmts found
11158 : : inside of the bodies of the loops are ignored. */
11159 : 12960 : case DO_STMT:
11160 : 12960 : *walk_subtrees = 0;
11161 : 12960 : RECUR (DO_COND (t));
11162 : 12960 : s = d->continue_stmt;
11163 : 12960 : b = d->break_stmt;
11164 : 12960 : RECUR (DO_BODY (t));
11165 : 12960 : d->continue_stmt = s;
11166 : 12960 : d->break_stmt = b;
11167 : 12960 : break;
11168 : :
11169 : 100 : case WHILE_STMT:
11170 : 100 : *walk_subtrees = 0;
11171 : 100 : RECUR (WHILE_COND_PREP (t));
11172 : 100 : RECUR (WHILE_COND (t));
11173 : 100 : s = d->continue_stmt;
11174 : 100 : b = d->break_stmt;
11175 : 100 : RECUR (WHILE_BODY (t));
11176 : 93 : d->continue_stmt = s;
11177 : 93 : d->break_stmt = b;
11178 : 93 : break;
11179 : :
11180 : 119 : case FOR_STMT:
11181 : 119 : *walk_subtrees = 0;
11182 : 119 : RECUR (FOR_INIT_STMT (t));
11183 : 119 : RECUR (FOR_COND_PREP (t));
11184 : 119 : RECUR (FOR_COND (t));
11185 : 119 : RECUR (FOR_EXPR (t));
11186 : 119 : s = d->continue_stmt;
11187 : 119 : b = d->break_stmt;
11188 : 119 : RECUR (FOR_BODY (t));
11189 : 81 : d->continue_stmt = s;
11190 : 81 : d->break_stmt = b;
11191 : 81 : break;
11192 : :
11193 : 0 : case RANGE_FOR_STMT:
11194 : 0 : *walk_subtrees = 0;
11195 : 0 : RECUR (RANGE_FOR_EXPR (t));
11196 : 0 : s = d->continue_stmt;
11197 : 0 : b = d->break_stmt;
11198 : 0 : RECUR (RANGE_FOR_BODY (t));
11199 : 0 : d->continue_stmt = s;
11200 : 0 : d->break_stmt = b;
11201 : 0 : break;
11202 : :
11203 : 207 : case SWITCH_STMT:
11204 : 207 : *walk_subtrees = 0;
11205 : 207 : RECUR (SWITCH_STMT_COND (t));
11206 : 207 : b = d->break_stmt;
11207 : 207 : RECUR (SWITCH_STMT_BODY (t));
11208 : 187 : d->break_stmt = b;
11209 : 187 : break;
11210 : : #undef RECUR
11211 : :
11212 : : case STATEMENT_LIST:
11213 : : case CONSTRUCTOR:
11214 : : break;
11215 : :
11216 : 1105577 : case AGGR_INIT_EXPR:
11217 : 1105577 : case CALL_EXPR:
11218 : : /* In C++26 a function could throw. */
11219 : 1105577 : if (callee_might_throw (t))
11220 : 95558 : d->could_throw = true;
11221 : : break;
11222 : :
11223 : 20343252 : default:
11224 : 20343252 : if (!EXPR_P (t))
11225 : 6372658 : *walk_subtrees = 0;
11226 : : break;
11227 : : }
11228 : :
11229 : : return NULL_TREE;
11230 : : }
11231 : :
11232 : : /* Return true if T denotes a potentially constant expression. Issue
11233 : : diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
11234 : : an lvalue-rvalue conversion is implied. If NOW is true, we want to
11235 : : consider the expression in the current context, independent of constexpr
11236 : : substitution. If FUNDEF_P is true, we're checking a constexpr function body
11237 : : and hard errors should not be reported by constexpr_error.
11238 : :
11239 : : C++0x [expr.const] used to say
11240 : :
11241 : : 6 An expression is a potential constant expression if it is
11242 : : a constant expression where all occurrences of function
11243 : : parameters are replaced by arbitrary constant expressions
11244 : : of the appropriate type.
11245 : :
11246 : : 2 A conditional expression is a constant expression unless it
11247 : : involves one of the following as a potentially evaluated
11248 : : subexpression (3.2), but subexpressions of logical AND (5.14),
11249 : : logical OR (5.15), and conditional (5.16) operations that are
11250 : : not evaluated are not considered. */
11251 : :
11252 : : static bool
11253 : 3083644379 : potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
11254 : : bool fundef_p, tsubst_flags_t flags,
11255 : : tree *jump_target)
11256 : : {
11257 : : #define RECUR(T,RV) \
11258 : : potential_constant_expression_1 ((T), (RV), strict, now, fundef_p, flags, \
11259 : : jump_target)
11260 : :
11261 : 3083644379 : enum { any = false, rval = true };
11262 : 3083644379 : int i;
11263 : 3083644379 : tree tmp;
11264 : :
11265 : 3083644379 : if (t == error_mark_node)
11266 : : return false;
11267 : 3083633752 : if (t == NULL_TREE)
11268 : : return true;
11269 : 3078206334 : location_t loc = cp_expr_loc_or_input_loc (t);
11270 : :
11271 : 3078206334 : if (*jump_target)
11272 : : /* If we are jumping, ignore everything. This is simpler than the
11273 : : cxx_eval_constant_expression handling because we only need to be
11274 : : conservatively correct, and we don't necessarily have a constant value
11275 : : available, so we don't bother with switch tracking. */
11276 : : return true;
11277 : :
11278 : 814818 : if (TREE_THIS_VOLATILE (t) && want_rval
11279 : 279620 : && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (t))
11280 : 3072750197 : && !NULLPTR_TYPE_P (TREE_TYPE (t)))
11281 : : {
11282 : 77748 : if (flags & tf_error)
11283 : 21 : constexpr_error (loc, fundef_p, "lvalue-to-rvalue conversion of "
11284 : : "a volatile lvalue %qE with type %qT", t,
11285 : 21 : TREE_TYPE (t));
11286 : 77748 : return false;
11287 : : }
11288 : 3072594675 : if (CONSTANT_CLASS_P (t))
11289 : : return true;
11290 : 2337378239 : if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED)
11291 : 2337378239 : && TREE_TYPE (t) == error_mark_node)
11292 : : return false;
11293 : :
11294 : 2337378214 : switch (TREE_CODE (t))
11295 : : {
11296 : : case FUNCTION_DECL:
11297 : : case BASELINK:
11298 : : case TEMPLATE_DECL:
11299 : : case OVERLOAD:
11300 : : case TEMPLATE_ID_EXPR:
11301 : : case LABEL_DECL:
11302 : : case CASE_LABEL_EXPR:
11303 : : case PREDICT_EXPR:
11304 : : case CONST_DECL:
11305 : : case SIZEOF_EXPR:
11306 : : case ALIGNOF_EXPR:
11307 : : case OFFSETOF_EXPR:
11308 : : case NOEXCEPT_EXPR:
11309 : : case TEMPLATE_PARM_INDEX:
11310 : : case TRAIT_EXPR:
11311 : : case IDENTIFIER_NODE:
11312 : : case USERDEF_LITERAL:
11313 : : /* We can see a FIELD_DECL in a pointer-to-member expression. */
11314 : : case FIELD_DECL:
11315 : : case RESULT_DECL:
11316 : : case USING_DECL:
11317 : : case USING_STMT:
11318 : : case PLACEHOLDER_EXPR:
11319 : : case REQUIRES_EXPR:
11320 : : case STATIC_ASSERT:
11321 : : case DEBUG_BEGIN_STMT:
11322 : : return true;
11323 : :
11324 : 15023566 : case RETURN_EXPR:
11325 : 15023566 : if (!RECUR (TREE_OPERAND (t, 0), any))
11326 : : return false;
11327 : : /* FALLTHROUGH */
11328 : :
11329 : 14891476 : case BREAK_STMT:
11330 : 14891476 : case CONTINUE_STMT:
11331 : 14891476 : *jump_target = t;
11332 : 14891476 : return true;
11333 : :
11334 : 237427749 : case PARM_DECL:
11335 : 237427749 : if (now && want_rval)
11336 : : {
11337 : 71217677 : tree type = TREE_TYPE (t);
11338 : 71217677 : if (dependent_type_p (type)
11339 : 47213815 : || !COMPLETE_TYPE_P (processing_template_decl
11340 : : ? type : complete_type (type))
11341 : 118431490 : || is_really_empty_class (type, /*ignore_vptr*/false))
11342 : : /* An empty class has no data to read. */
11343 : 24004192 : return true;
11344 : 47213485 : if (flags & tf_error)
11345 : 15 : constexpr_error (input_location, fundef_p,
11346 : : "%qE is not a constant expression", t);
11347 : 47213485 : return false;
11348 : : }
11349 : : return true;
11350 : :
11351 : 178989016 : case AGGR_INIT_EXPR:
11352 : 178989016 : case CALL_EXPR:
11353 : : /* -- an invocation of a function other than a constexpr function
11354 : : or a constexpr constructor. */
11355 : 178989016 : {
11356 : 178989016 : tree fun = get_function_named_in_call (t);
11357 : 178989016 : const int nargs = call_expr_nargs (t);
11358 : 178989016 : i = 0;
11359 : :
11360 : 178989016 : if (fun == NULL_TREE)
11361 : : {
11362 : : /* Reset to allow the function to continue past the end
11363 : : of the block below. Otherwise return early. */
11364 : 98046 : bool bail = true;
11365 : :
11366 : 98046 : if (TREE_CODE (t) == CALL_EXPR
11367 : 98046 : && CALL_EXPR_FN (t) == NULL_TREE)
11368 : 98046 : switch (CALL_EXPR_IFN (t))
11369 : : {
11370 : : /* These should be ignored, they are optimized away from
11371 : : constexpr functions. */
11372 : : case IFN_UBSAN_NULL:
11373 : : case IFN_UBSAN_BOUNDS:
11374 : : case IFN_UBSAN_VPTR:
11375 : : case IFN_FALLTHROUGH:
11376 : : case IFN_ASSUME:
11377 : : return true;
11378 : :
11379 : : case IFN_ADD_OVERFLOW:
11380 : : case IFN_SUB_OVERFLOW:
11381 : : case IFN_MUL_OVERFLOW:
11382 : : case IFN_LAUNDER:
11383 : : case IFN_VEC_CONVERT:
11384 : : bail = false;
11385 : : break;
11386 : :
11387 : : default:
11388 : : break;
11389 : : }
11390 : :
11391 : : if (bail)
11392 : : {
11393 : : /* fold_call_expr can't do anything with IFN calls. */
11394 : 44 : if (flags & tf_error)
11395 : 0 : constexpr_error (loc, fundef_p,
11396 : : "call to internal function %qE", t);
11397 : 44 : return false;
11398 : : }
11399 : : }
11400 : :
11401 : 178890970 : if (fun && is_overloaded_fn (fun))
11402 : : {
11403 : 162866209 : if (!RECUR (fun, true))
11404 : : return false;
11405 : 162457288 : fun = get_fns (fun);
11406 : :
11407 : 162457288 : if (TREE_CODE (fun) == FUNCTION_DECL)
11408 : : {
11409 : 141975310 : if (builtin_valid_in_constant_expr_p (fun))
11410 : : return true;
11411 : 141206400 : if (!maybe_constexpr_fn (fun)
11412 : : /* Allow any built-in function; if the expansion
11413 : : isn't constant, we'll deal with that then. */
11414 : 42167678 : && !fndecl_built_in_p (fun)
11415 : : /* In C++20, replaceable global allocation functions
11416 : : are constant expressions. */
11417 : 30169949 : && (!cxx_replaceable_global_alloc_fn (fun)
11418 : 173023 : || TREE_CODE (t) != CALL_EXPR
11419 : 173023 : || (!CALL_FROM_NEW_OR_DELETE_P (t)
11420 : 71005 : && (current_function_decl == NULL_TREE
11421 : 71005 : || !is_std_allocator_allocate
11422 : 71005 : (current_function_decl))))
11423 : : /* Allow placement new in std::construct_at. */
11424 : 30004003 : && (!cxx_placement_new_fn (fun)
11425 : 50312 : || TREE_CODE (t) != CALL_EXPR
11426 : 50312 : || current_function_decl == NULL_TREE
11427 : 50306 : || !is_std_construct_at (current_function_decl))
11428 : 29970969 : && !cxx_dynamic_cast_fn_p (fun)
11429 : 171174789 : && !cxx_cxa_builtin_fn_p (fun))
11430 : : {
11431 : : /* In C++26 evaluation of the function arguments might
11432 : : throw and in that case it is irrelevant whether
11433 : : fun is constexpr or not. */
11434 : 29936662 : if (cxx_dialect >= cxx26)
11435 : 10408243 : for (; i < nargs; ++i)
11436 : : {
11437 : 6296078 : tree x = get_nth_callarg (t, i);
11438 : 6296078 : bool rv = processing_template_decl ? any : rval;
11439 : 6296078 : bool sub_now = false;
11440 : 6296078 : if (!potential_constant_expression_1 (x, rv, strict,
11441 : : sub_now,
11442 : : fundef_p,
11443 : : flags,
11444 : : jump_target))
11445 : : return false;
11446 : 5749894 : if (throws (jump_target))
11447 : : return true;
11448 : : }
11449 : 29299489 : if ((flags & tf_error)
11450 : 29299489 : && constexpr_error (loc, fundef_p,
11451 : : "call to non-%<constexpr%> "
11452 : : "function %qD", fun))
11453 : 244 : explain_invalid_constexpr_fn (fun);
11454 : 29299489 : return false;
11455 : : }
11456 : : }
11457 : :
11458 : 131751716 : fun = OVL_FIRST (fun);
11459 : : /* Skip initial arguments to base constructors. */
11460 : 131751716 : if (DECL_BASE_CONSTRUCTOR_P (fun))
11461 : 2349392 : i = num_artificial_parms_for (fun);
11462 : : }
11463 : 16024761 : else if (fun)
11464 : : {
11465 : 16024761 : if (TREE_TYPE (fun)
11466 : 16024761 : && FUNCTION_POINTER_TYPE_P (TREE_TYPE (fun)))
11467 : : want_rval = rval;
11468 : : else
11469 : : want_rval = any;
11470 : 16024761 : if (RECUR (fun, want_rval))
11471 : : /* Might end up being a constant function pointer. But it
11472 : : could also be a function object with constexpr op(), so
11473 : : we pass 'any' so that the underlying VAR_DECL is deemed
11474 : : as potentially-constant even though it wasn't declared
11475 : : constexpr. */;
11476 : : else
11477 : : return false;
11478 : : }
11479 : 291398425 : for (; i < nargs; ++i)
11480 : : {
11481 : 159809025 : tree x = get_nth_callarg (t, i);
11482 : : /* In a template, reference arguments haven't been converted to
11483 : : REFERENCE_TYPE and we might not even know if the parameter
11484 : : is a reference, so accept lvalue constants too. */
11485 : 159809025 : bool rv = processing_template_decl ? any : rval;
11486 : : /* Don't require an immediately constant value, as constexpr
11487 : : substitution might not use the value of the argument. */
11488 : 159809025 : bool sub_now = false;
11489 : 159809025 : if (!potential_constant_expression_1 (x, rv, strict,
11490 : : sub_now, fundef_p, flags,
11491 : : jump_target))
11492 : : return false;
11493 : 1893724196 : if (throws (jump_target))
11494 : : return true;
11495 : : }
11496 : : /* In C++26 a function could throw. */
11497 : 131589400 : if (*jump_target == NULL_TREE && callee_might_throw (t))
11498 : 11595221 : *jump_target = void_node;
11499 : : return true;
11500 : : }
11501 : :
11502 : 127999908 : case NON_LVALUE_EXPR:
11503 : : /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
11504 : : -- an lvalue of integral type that refers to a non-volatile
11505 : : const variable or static data member initialized with
11506 : : constant expressions, or
11507 : :
11508 : : -- an lvalue of literal type that refers to non-volatile
11509 : : object defined with constexpr, or that refers to a
11510 : : sub-object of such an object; */
11511 : 127999908 : return RECUR (TREE_OPERAND (t, 0), rval);
11512 : :
11513 : 33519 : case EXCESS_PRECISION_EXPR:
11514 : 33519 : return RECUR (TREE_OPERAND (t, 0), rval);
11515 : :
11516 : 231947009 : case VAR_DECL:
11517 : 231947009 : if (DECL_HAS_VALUE_EXPR_P (t))
11518 : : {
11519 : 3841896 : if (now && is_normal_capture_proxy (t))
11520 : : {
11521 : : /* -- in a lambda-expression, a reference to this or to a
11522 : : variable with automatic storage duration defined outside that
11523 : : lambda-expression, where the reference would be an
11524 : : odr-use. */
11525 : :
11526 : 426400 : if (want_rval)
11527 : : /* Since we're doing an lvalue-rvalue conversion, this might
11528 : : not be an odr-use, so evaluate the variable directly. */
11529 : 425789 : return RECUR (DECL_CAPTURED_VARIABLE (t), rval);
11530 : :
11531 : 611 : if (flags & tf_error)
11532 : : {
11533 : 3 : tree cap = DECL_CAPTURED_VARIABLE (t);
11534 : 3 : auto_diagnostic_group d;
11535 : 3 : if (constexpr_error (input_location, fundef_p,
11536 : : "lambda capture of %qE is not a "
11537 : : "constant expression", cap)
11538 : 3 : && decl_constant_var_p (cap))
11539 : 3 : inform (input_location, "because it is used as a glvalue");
11540 : 3 : }
11541 : 611 : return false;
11542 : : }
11543 : : /* Treat __PRETTY_FUNCTION__ inside a template function as
11544 : : potentially-constant. */
11545 : 6829833 : else if (DECL_PRETTY_FUNCTION_P (t)
11546 : 3531769 : && DECL_VALUE_EXPR (t) == error_mark_node)
11547 : : return true;
11548 : 3415491 : return RECUR (DECL_VALUE_EXPR (t), rval);
11549 : : }
11550 : 228105113 : if (want_rval
11551 : 156212173 : && (now || !var_in_maybe_constexpr_fn (t))
11552 : 147345823 : && !type_dependent_expression_p (t)
11553 : 116344970 : && !decl_maybe_constant_var_p (t)
11554 : 50147206 : && (strict
11555 : 1315136 : || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t))
11556 : 410120 : || (DECL_INITIAL (t)
11557 : 403305 : && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t)))
11558 : 50139622 : && COMPLETE_TYPE_P (TREE_TYPE (t))
11559 : 278229978 : && !is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
11560 : : {
11561 : 50120168 : if (flags & tf_error)
11562 : 160 : non_const_var_error (loc, t, fundef_p);
11563 : 50120168 : return false;
11564 : : }
11565 : : return true;
11566 : :
11567 : 247716348 : case NOP_EXPR:
11568 : 247716348 : if (REINTERPRET_CAST_P (t))
11569 : : {
11570 : 130167 : if (flags & tf_error)
11571 : 47 : constexpr_error (loc, fundef_p, "%<reinterpret_cast%> is not a "
11572 : : "constant expression");
11573 : 130167 : return false;
11574 : : }
11575 : : /* FALLTHRU */
11576 : 572148803 : case CONVERT_EXPR:
11577 : 572148803 : case VIEW_CONVERT_EXPR:
11578 : : /* -- a reinterpret_cast. FIXME not implemented, and this rule
11579 : : may change to something more specific to type-punning (DR 1312). */
11580 : 572148803 : {
11581 : 572148803 : tree from = TREE_OPERAND (t, 0);
11582 : 572148803 : if (location_wrapper_p (t))
11583 : : {
11584 : 285394038 : iloc_sentinel ils = loc;
11585 : 285394038 : return (RECUR (from, want_rval));
11586 : 285394038 : }
11587 : 286754765 : if (INDIRECT_TYPE_P (TREE_TYPE (t)))
11588 : : {
11589 : 148545835 : STRIP_ANY_LOCATION_WRAPPER (from);
11590 : 148545835 : if (TREE_CODE (from) == INTEGER_CST
11591 : 148545835 : && !integer_zerop (from))
11592 : : {
11593 : 1758 : if (flags & tf_error)
11594 : 22 : constexpr_error (loc, fundef_p,
11595 : : "%<reinterpret_cast%> from integer to "
11596 : : "pointer");
11597 : 1758 : return false;
11598 : : }
11599 : : }
11600 : 286753007 : return (RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR));
11601 : : }
11602 : :
11603 : 11669 : case ADDRESSOF_EXPR:
11604 : : /* This is like ADDR_EXPR, except it won't form pointer-to-member. */
11605 : 11669 : t = TREE_OPERAND (t, 0);
11606 : 11669 : goto handle_addr_expr;
11607 : :
11608 : 55812359 : case ADDR_EXPR:
11609 : : /* -- a unary operator & that is applied to an lvalue that
11610 : : designates an object with thread or automatic storage
11611 : : duration; */
11612 : 55812359 : t = TREE_OPERAND (t, 0);
11613 : :
11614 : 55812359 : if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t))
11615 : : /* A pointer-to-member constant. */
11616 : : return true;
11617 : :
11618 : 55823790 : handle_addr_expr:
11619 : : #if 0
11620 : : /* FIXME adjust when issue 1197 is fully resolved. For now don't do
11621 : : any checking here, as we might dereference the pointer later. If
11622 : : we remove this code, also remove check_automatic_or_tls. */
11623 : : i = check_automatic_or_tls (t);
11624 : : if (i == ck_ok)
11625 : : return true;
11626 : : if (i == ck_bad)
11627 : : {
11628 : : if (flags & tf_error)
11629 : : error ("address-of an object %qE with thread local or "
11630 : : "automatic storage is not a constant expression", t);
11631 : : return false;
11632 : : }
11633 : : #endif
11634 : 55823790 : return RECUR (t, any);
11635 : :
11636 : 72211075 : case COMPONENT_REF:
11637 : 72211075 : case ARROW_EXPR:
11638 : 72211075 : case OFFSET_REF:
11639 : : /* -- a class member access unless its postfix-expression is
11640 : : of literal type or of pointer to literal type. */
11641 : : /* This test would be redundant, as it follows from the
11642 : : postfix-expression being a potential constant expression. */
11643 : 72211075 : if (type_unknown_p (t))
11644 : : return true;
11645 : 65836047 : if (is_overloaded_fn (t))
11646 : : /* In a template, a COMPONENT_REF of a function expresses ob.fn(),
11647 : : which uses ob as an lvalue. */
11648 : 67369670 : want_rval = false;
11649 : 67369670 : gcc_fallthrough ();
11650 : :
11651 : 67369670 : case REALPART_EXPR:
11652 : 67369670 : case IMAGPART_EXPR:
11653 : 67369670 : case BIT_FIELD_REF:
11654 : 67369670 : return RECUR (TREE_OPERAND (t, 0), want_rval);
11655 : :
11656 : 204236 : case EXPR_PACK_EXPANSION:
11657 : 204236 : return RECUR (PACK_EXPANSION_PATTERN (t), want_rval);
11658 : :
11659 : : case PACK_INDEX_EXPR:
11660 : : return true;
11661 : :
11662 : 70744602 : case INDIRECT_REF:
11663 : 70744602 : return RECUR (TREE_OPERAND (t, 0), rval);
11664 : :
11665 : 15524067 : case STATEMENT_LIST:
11666 : 55480818 : for (tree stmt : tsi_range (t))
11667 : 40359509 : if (!RECUR (stmt, any))
11668 : 249120196 : return false;
11669 : : return true;
11670 : :
11671 : 3621183 : case MODIFY_EXPR:
11672 : 3621183 : if (cxx_dialect < cxx14)
11673 : 1921 : goto fail;
11674 : 3619262 : if (!RECUR (TREE_OPERAND (t, 0), any))
11675 : : return false;
11676 : : /* Just ignore clobbers. */
11677 : 3195920 : if (TREE_CLOBBER_P (TREE_OPERAND (t, 1)))
11678 : : return true;
11679 : 1568820 : if (!RECUR (TREE_OPERAND (t, 1), rval))
11680 : : return false;
11681 : : return true;
11682 : :
11683 : 287630 : case MODOP_EXPR:
11684 : 287630 : if (cxx_dialect < cxx14)
11685 : 96 : goto fail;
11686 : 287534 : if (!RECUR (TREE_OPERAND (t, 0), rval))
11687 : : return false;
11688 : 276270 : if (!RECUR (TREE_OPERAND (t, 2), rval))
11689 : : return false;
11690 : : return true;
11691 : :
11692 : 280331 : case DO_STMT:
11693 : 280331 : if (!RECUR (DO_COND (t), rval))
11694 : : return false;
11695 : 280331 : if (!RECUR (DO_BODY (t), any))
11696 : : return false;
11697 : 280262 : if (breaks (jump_target) || continues (jump_target))
11698 : 2 : *jump_target = NULL_TREE;
11699 : : return true;
11700 : :
11701 : 216118 : case FOR_STMT:
11702 : 216118 : if (!RECUR (FOR_INIT_STMT (t), any))
11703 : : return false;
11704 : 216118 : if (!RECUR (FOR_COND_PREP (t), any))
11705 : : return false;
11706 : 216118 : tmp = FOR_COND (t);
11707 : 216118 : if (!RECUR (tmp, rval))
11708 : : return false;
11709 : 215718 : if (tmp)
11710 : : {
11711 : 171576 : if (!processing_template_decl)
11712 : 167324 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
11713 : : /* If we couldn't evaluate the condition, it might not ever be
11714 : : true. */
11715 : 171576 : if (!integer_onep (tmp))
11716 : : {
11717 : : /* Before returning true, check if the for body can contain
11718 : : a return. */
11719 : 171576 : hash_set<tree> pset;
11720 : 171576 : check_for_return_continue_data data = { &pset, NULL_TREE,
11721 : 171576 : NULL_TREE, false };
11722 : 171576 : if (tree ret_expr
11723 : 171576 : = cp_walk_tree (&FOR_BODY (t), check_for_return_continue,
11724 : : &data, &pset))
11725 : 100037 : *jump_target = ret_expr;
11726 : 171576 : if (data.could_throw)
11727 : 16352 : *jump_target = void_node;
11728 : 171576 : return true;
11729 : 171576 : }
11730 : : }
11731 : 44142 : if (!RECUR (FOR_EXPR (t), any))
11732 : : return false;
11733 : 44142 : if (!RECUR (FOR_BODY (t), any))
11734 : : return false;
11735 : 44142 : if (!RECUR (FOR_COND_CLEANUP (t), any))
11736 : : return false;
11737 : 44142 : if (breaks (jump_target) || continues (jump_target))
11738 : 3 : *jump_target = NULL_TREE;
11739 : : return true;
11740 : :
11741 : 21 : case RANGE_FOR_STMT:
11742 : 21 : if (!RECUR (RANGE_FOR_INIT_STMT (t), any))
11743 : : return false;
11744 : 21 : if (!RECUR (RANGE_FOR_EXPR (t), any))
11745 : : return false;
11746 : 21 : if (!RECUR (RANGE_FOR_BODY (t), any))
11747 : : return false;
11748 : 17 : if (breaks (jump_target) || continues (jump_target))
11749 : 0 : *jump_target = NULL_TREE;
11750 : : return true;
11751 : :
11752 : 91315 : case WHILE_STMT:
11753 : 91315 : if (!RECUR (WHILE_COND_PREP (t), any))
11754 : : return false;
11755 : 91315 : tmp = WHILE_COND (t);
11756 : 91315 : if (!RECUR (tmp, rval))
11757 : : return false;
11758 : 91194 : if (!processing_template_decl)
11759 : 91178 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
11760 : : /* If we couldn't evaluate the condition, it might not ever be true. */
11761 : 91194 : if (!integer_onep (tmp))
11762 : : {
11763 : : /* Before returning true, check if the while body can contain
11764 : : a return. */
11765 : 88413 : hash_set<tree> pset;
11766 : 88413 : check_for_return_continue_data data = { &pset, NULL_TREE,
11767 : 88413 : NULL_TREE, false };
11768 : 88413 : if (tree ret_expr
11769 : 88413 : = cp_walk_tree (&WHILE_BODY (t), check_for_return_continue,
11770 : : &data, &pset))
11771 : 212 : *jump_target = ret_expr;
11772 : 88413 : if (data.could_throw)
11773 : 5971 : *jump_target = void_node;
11774 : 88413 : return true;
11775 : 88413 : }
11776 : 2781 : if (!RECUR (WHILE_BODY (t), any))
11777 : : return false;
11778 : 2779 : if (!RECUR (WHILE_COND_CLEANUP (t), any))
11779 : : return false;
11780 : 2779 : if (breaks (jump_target) || continues (jump_target))
11781 : 14 : *jump_target = NULL_TREE;
11782 : : return true;
11783 : :
11784 : 20941 : case SWITCH_STMT:
11785 : 20941 : if (!RECUR (SWITCH_STMT_COND (t), rval))
11786 : : return false;
11787 : : /* FIXME we don't check SWITCH_STMT_BODY currently, because even
11788 : : unreachable labels would be checked and it is enough if there is
11789 : : a single switch cond value for which it is a valid constant
11790 : : expression. We need to check if there are any RETURN_EXPRs
11791 : : or CONTINUE_STMTs inside of the body though, as in that case
11792 : : we need to set *jump_target. */
11793 : : else
11794 : : {
11795 : 20935 : hash_set<tree> pset;
11796 : 20935 : check_for_return_continue_data data = { &pset, NULL_TREE,
11797 : 20935 : NULL_TREE, false };
11798 : 20935 : if (tree ret_expr
11799 : 20935 : = cp_walk_tree (&SWITCH_STMT_BODY (t), check_for_return_continue,
11800 : : &data, &pset))
11801 : : /* The switch might return. */
11802 : 20418 : *jump_target = ret_expr;
11803 : 517 : else if (data.continue_stmt)
11804 : : /* The switch can't return, but might continue. */
11805 : 3 : *jump_target = data.continue_stmt;
11806 : 20935 : if (data.could_throw)
11807 : 34 : *jump_target = void_node;
11808 : 20935 : }
11809 : 20935 : return true;
11810 : :
11811 : 44 : case STMT_EXPR:
11812 : 44 : return RECUR (STMT_EXPR_STMT (t), rval);
11813 : :
11814 : 258894 : case LAMBDA_EXPR:
11815 : 258894 : if (cxx_dialect >= cxx17)
11816 : : /* In C++17 lambdas can be constexpr, don't give up yet. */
11817 : : return true;
11818 : 212 : else if (flags & tf_error)
11819 : 0 : constexpr_error (loc, fundef_p, "lambda-expression is not a "
11820 : : "constant expression before C++17");
11821 : : return false;
11822 : :
11823 : 106903 : case NEW_EXPR:
11824 : 106903 : case VEC_NEW_EXPR:
11825 : 106903 : case DELETE_EXPR:
11826 : 106903 : case VEC_DELETE_EXPR:
11827 : 106903 : if (cxx_dialect >= cxx20)
11828 : : /* In C++20, new-expressions are potentially constant. */
11829 : : return true;
11830 : 79357 : else if (flags & tf_error)
11831 : 0 : constexpr_error (loc, fundef_p, "new-expression is not a "
11832 : : "constant expression before C++20");
11833 : : return false;
11834 : :
11835 : 65683 : case DYNAMIC_CAST_EXPR:
11836 : 65683 : case PSEUDO_DTOR_EXPR:
11837 : 65683 : case OMP_PARALLEL:
11838 : 65683 : case OMP_TASK:
11839 : 65683 : case OMP_FOR:
11840 : 65683 : case OMP_SIMD:
11841 : 65683 : case OMP_DISTRIBUTE:
11842 : 65683 : case OMP_TASKLOOP:
11843 : 65683 : case OMP_LOOP:
11844 : 65683 : case OMP_TEAMS:
11845 : 65683 : case OMP_TARGET_DATA:
11846 : 65683 : case OMP_TARGET:
11847 : 65683 : case OMP_SECTIONS:
11848 : 65683 : case OMP_ORDERED:
11849 : 65683 : case OMP_CRITICAL:
11850 : 65683 : case OMP_SINGLE:
11851 : 65683 : case OMP_SCAN:
11852 : 65683 : case OMP_SCOPE:
11853 : 65683 : case OMP_SECTION:
11854 : 65683 : case OMP_MASTER:
11855 : 65683 : case OMP_MASKED:
11856 : 65683 : case OMP_TASKGROUP:
11857 : 65683 : case OMP_TARGET_UPDATE:
11858 : 65683 : case OMP_TARGET_ENTER_DATA:
11859 : 65683 : case OMP_TARGET_EXIT_DATA:
11860 : 65683 : case OMP_ATOMIC:
11861 : 65683 : case OMP_ATOMIC_READ:
11862 : 65683 : case OMP_ATOMIC_CAPTURE_OLD:
11863 : 65683 : case OMP_ATOMIC_CAPTURE_NEW:
11864 : 65683 : case OMP_DEPOBJ:
11865 : 65683 : case OACC_PARALLEL:
11866 : 65683 : case OACC_KERNELS:
11867 : 65683 : case OACC_SERIAL:
11868 : 65683 : case OACC_DATA:
11869 : 65683 : case OACC_HOST_DATA:
11870 : 65683 : case OACC_LOOP:
11871 : 65683 : case OACC_CACHE:
11872 : 65683 : case OACC_DECLARE:
11873 : 65683 : case OACC_ENTER_DATA:
11874 : 65683 : case OACC_EXIT_DATA:
11875 : 65683 : case OACC_UPDATE:
11876 : 65683 : case OMP_ARRAY_SECTION:
11877 : : /* GCC internal stuff. */
11878 : 65683 : case VA_ARG_EXPR:
11879 : 65683 : case TRANSACTION_EXPR:
11880 : 65683 : case AT_ENCODE_EXPR:
11881 : 65683 : fail:
11882 : 65683 : if (flags & tf_error)
11883 : 21 : constexpr_error (loc, fundef_p, "expression %qE is not a constant "
11884 : : "expression", t);
11885 : : return false;
11886 : :
11887 : : case OMP_DECLARE_MAPPER:
11888 : : /* This can be used to initialize VAR_DECLs: it's treated as a magic
11889 : : constant. */
11890 : : return true;
11891 : :
11892 : 444 : case THROW_EXPR:
11893 : 444 : if (cxx_dialect < cxx26)
11894 : 267 : goto fail;
11895 : 177 : return RECUR (TREE_OPERAND (t, 0), rval);
11896 : :
11897 : 482 : case ASM_EXPR:
11898 : 482 : if (flags & tf_error)
11899 : 3 : inline_asm_in_constexpr_error (loc, fundef_p);
11900 : : return false;
11901 : :
11902 : 364720 : case OBJ_TYPE_REF:
11903 : 364720 : if (cxx_dialect >= cxx20)
11904 : : /* In C++20 virtual calls can be constexpr, don't give up yet. */
11905 : : return true;
11906 : 225463 : else if (flags & tf_error)
11907 : 0 : constexpr_error (loc, fundef_p, "virtual functions cannot be "
11908 : : "%<constexpr%> before C++20");
11909 : : return false;
11910 : :
11911 : 6491 : case TYPEID_EXPR:
11912 : : /* In C++20, a typeid expression whose operand is of polymorphic
11913 : : class type can be constexpr. */
11914 : 6491 : {
11915 : 6491 : tree e = TREE_OPERAND (t, 0);
11916 : 6491 : if (cxx_dialect < cxx20
11917 : 513 : && strict
11918 : 513 : && !TYPE_P (e)
11919 : 273 : && !type_dependent_expression_p (e)
11920 : 6497 : && TYPE_POLYMORPHIC_P (TREE_TYPE (e)))
11921 : : {
11922 : 4 : if (flags & tf_error)
11923 : 1 : constexpr_error (loc, fundef_p, "%<typeid%> is not a "
11924 : : "constant expression because %qE is "
11925 : : "of polymorphic type", e);
11926 : 4 : return false;
11927 : : }
11928 : : return true;
11929 : : }
11930 : :
11931 : 25276669 : case POINTER_DIFF_EXPR:
11932 : 25276669 : case MINUS_EXPR:
11933 : 25276669 : want_rval = true;
11934 : 25276669 : goto binary;
11935 : :
11936 : 60175381 : case LT_EXPR:
11937 : 60175381 : case LE_EXPR:
11938 : 60175381 : case GT_EXPR:
11939 : 60175381 : case GE_EXPR:
11940 : 60175381 : case EQ_EXPR:
11941 : 60175381 : case NE_EXPR:
11942 : 60175381 : case SPACESHIP_EXPR:
11943 : 60175381 : want_rval = true;
11944 : 60175381 : goto binary;
11945 : :
11946 : 1342992 : case PREINCREMENT_EXPR:
11947 : 1342992 : case POSTINCREMENT_EXPR:
11948 : 1342992 : case PREDECREMENT_EXPR:
11949 : 1342992 : case POSTDECREMENT_EXPR:
11950 : 1342992 : if (cxx_dialect < cxx14)
11951 : 8190 : goto fail;
11952 : 1334802 : goto unary;
11953 : :
11954 : 1024486 : case BIT_NOT_EXPR:
11955 : : /* A destructor. */
11956 : 1024486 : if (TYPE_P (TREE_OPERAND (t, 0)))
11957 : : return true;
11958 : : /* fall through. */
11959 : :
11960 : 42957126 : case CONJ_EXPR:
11961 : 42957126 : case SAVE_EXPR:
11962 : 42957126 : case FIX_TRUNC_EXPR:
11963 : 42957126 : case FLOAT_EXPR:
11964 : 42957126 : case NEGATE_EXPR:
11965 : 42957126 : case ABS_EXPR:
11966 : 42957126 : case ABSU_EXPR:
11967 : 42957126 : case TRUTH_NOT_EXPR:
11968 : 42957126 : case FIXED_CONVERT_EXPR:
11969 : 42957126 : case UNARY_PLUS_EXPR:
11970 : 42957126 : case UNARY_LEFT_FOLD_EXPR:
11971 : 42957126 : case UNARY_RIGHT_FOLD_EXPR:
11972 : 42957126 : case VEC_DUPLICATE_EXPR:
11973 : 1024486 : unary:
11974 : 42957126 : return RECUR (TREE_OPERAND (t, 0), rval);
11975 : :
11976 : 27486600 : case CAST_EXPR:
11977 : 27486600 : case CONST_CAST_EXPR:
11978 : 27486600 : case STATIC_CAST_EXPR:
11979 : 27486600 : case REINTERPRET_CAST_EXPR:
11980 : 27486600 : case IMPLICIT_CONV_EXPR:
11981 : 27486600 : if (!cast_valid_in_integral_constant_expression_p (TREE_TYPE (t)))
11982 : : /* In C++98, a conversion to non-integral type can't be part of a
11983 : : constant expression. */
11984 : : {
11985 : 287 : if (flags & tf_error)
11986 : 0 : constexpr_error (loc, fundef_p,
11987 : : "cast to non-integral type %qT in a constant "
11988 : 0 : "expression", TREE_TYPE (t));
11989 : 287 : return false;
11990 : : }
11991 : : /* This might be a conversion from a class to a (potentially) literal
11992 : : type. Let's consider it potentially constant since the conversion
11993 : : might be a constexpr user-defined conversion. */
11994 : 27486313 : else if (cxx_dialect >= cxx11
11995 : 27467419 : && (dependent_type_p (TREE_TYPE (t))
11996 : 7791355 : || !COMPLETE_TYPE_P (TREE_TYPE (t))
11997 : 7786676 : || literal_type_p (TREE_TYPE (t)))
11998 : 27449611 : && TREE_OPERAND (t, 0)
11999 : 54735466 : && (TREE_CODE (t) != CAST_EXPR
12000 : 21865909 : || !TREE_CHAIN (TREE_OPERAND (t, 0))))
12001 : : {
12002 : 27217666 : tree from = TREE_OPERAND (t, 0);
12003 : 27217666 : if (TREE_CODE (t) == CAST_EXPR)
12004 : 21834422 : from = TREE_VALUE (from);
12005 : 27217666 : tree type = TREE_TYPE (from);
12006 : : /* If this is a dependent type, it could end up being a class
12007 : : with conversions. */
12008 : 27217666 : if (type == NULL_TREE || WILDCARD_TYPE_P (type))
12009 : : return true;
12010 : : /* Or a non-dependent class which has conversions. */
12011 : 335979 : else if (CLASS_TYPE_P (type)
12012 : 335979 : && (TYPE_HAS_CONVERSION (type) || dependent_scope_p (type)))
12013 : 84764 : return true;
12014 : : }
12015 : :
12016 : 23083944 : return (RECUR (TREE_OPERAND (t, 0),
12017 : 23083944 : !TYPE_REF_P (TREE_TYPE (t))));
12018 : :
12019 : 7953519 : case BIND_EXPR:
12020 : 7953519 : return RECUR (BIND_EXPR_BODY (t), want_rval);
12021 : :
12022 : 38363186 : case CLEANUP_POINT_EXPR:
12023 : 38363186 : case MUST_NOT_THROW_EXPR:
12024 : 38363186 : case TRY_CATCH_EXPR:
12025 : : /* Even for C++26 handle TRY_BLOCK conservatively, if we detect the
12026 : : body could throw, even with catch (...) among handlers we'd need
12027 : : to analyze them in detail if they couldn't rethrow it. More
12028 : : importantly though, throws (jump_target) is just conservative,
12029 : : and there could be e.g.
12030 : : try
12031 : : {
12032 : : possibly_throwing_fn (args);
12033 : : break;
12034 : : }
12035 : : catch (...)
12036 : : {
12037 : : }
12038 : : or continue or return instead of break. So, clearing *jump_target
12039 : : because we see catch (...) handler might mean we missed break
12040 : : etc. */
12041 : 38363186 : case TRY_BLOCK:
12042 : 38363186 : case EH_SPEC_BLOCK:
12043 : 38363186 : case EXPR_STMT:
12044 : 38363186 : case PAREN_EXPR:
12045 : : /* For convenience. */
12046 : 38363186 : case LOOP_EXPR:
12047 : 38363186 : case EXIT_EXPR:
12048 : 38363186 : return RECUR (TREE_OPERAND (t, 0), want_rval);
12049 : :
12050 : 5666154 : case DECL_EXPR:
12051 : 5666154 : tmp = DECL_EXPR_DECL (t);
12052 : 4428047 : if (VAR_P (tmp) && !DECL_ARTIFICIAL (tmp)
12053 : 8452098 : && (processing_template_decl
12054 : 2453468 : ? !decl_maybe_constant_var_p (tmp)
12055 : 2120992 : : !decl_constant_var_p (tmp)))
12056 : : {
12057 : 2146382 : if (CP_DECL_THREAD_LOCAL_P (tmp) && !DECL_REALLY_EXTERN (tmp))
12058 : : {
12059 : 5 : if (flags & tf_error)
12060 : 2 : constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p,
12061 : : "%qD defined %<thread_local%> in "
12062 : : "%<constexpr%> context", tmp);
12063 : 5 : return false;
12064 : : }
12065 : 2146377 : else if (TREE_STATIC (tmp))
12066 : : {
12067 : 81 : if (flags & tf_error)
12068 : 15 : constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p,
12069 : : "%qD defined %<static%> in %<constexpr%> "
12070 : : "context", tmp);
12071 : 81 : return false;
12072 : : }
12073 : 2146296 : else if (!check_for_uninitialized_const_var
12074 : 2146296 : (tmp, /*constexpr_context_p=*/true, flags))
12075 : : return false;
12076 : : }
12077 : 5662301 : if (VAR_P (tmp))
12078 : 4424194 : return RECUR (DECL_INITIAL (tmp), want_rval);
12079 : : return true;
12080 : :
12081 : 25157 : case TRY_FINALLY_EXPR:
12082 : 25157 : return (RECUR (TREE_OPERAND (t, 0), want_rval)
12083 : 25157 : && RECUR (TREE_OPERAND (t, 1), any));
12084 : :
12085 : 19933339 : case SCOPE_REF:
12086 : 19933339 : return RECUR (TREE_OPERAND (t, 1), want_rval);
12087 : :
12088 : 22207943 : case TARGET_EXPR:
12089 : 22207943 : if (!TARGET_EXPR_DIRECT_INIT_P (t)
12090 : 22150445 : && !TARGET_EXPR_ELIDING_P (t)
12091 : 39418412 : && !literal_type_p (TREE_TYPE (t)))
12092 : : {
12093 : 1348861 : if (flags & tf_error)
12094 : : {
12095 : 21 : auto_diagnostic_group d;
12096 : 21 : if (constexpr_error (loc, fundef_p,
12097 : : "temporary of non-literal type %qT in a "
12098 : 21 : "constant expression", TREE_TYPE (t)))
12099 : 21 : explain_non_literal_class (TREE_TYPE (t));
12100 : 21 : }
12101 : 1348861 : return false;
12102 : : }
12103 : : /* FALLTHRU */
12104 : 37667149 : case INIT_EXPR:
12105 : 37667149 : return RECUR (TREE_OPERAND (t, 1), rval);
12106 : :
12107 : 27514425 : case CONSTRUCTOR:
12108 : 27514425 : {
12109 : 27514425 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
12110 : 27514425 : constructor_elt *ce;
12111 : 112357792 : for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
12112 : 85679982 : if (!RECUR (ce->value, want_rval))
12113 : : return false;
12114 : : return true;
12115 : : }
12116 : :
12117 : 20282114 : case TREE_LIST:
12118 : 20282114 : {
12119 : 20282114 : gcc_assert (TREE_PURPOSE (t) == NULL_TREE
12120 : : || DECL_P (TREE_PURPOSE (t)));
12121 : 20282114 : if (!RECUR (TREE_VALUE (t), want_rval))
12122 : : return false;
12123 : 18087500 : if (TREE_CHAIN (t) == NULL_TREE)
12124 : : return true;
12125 : 49333 : return RECUR (TREE_CHAIN (t), want_rval);
12126 : : }
12127 : :
12128 : 9873417 : case TRUNC_DIV_EXPR:
12129 : 9873417 : case CEIL_DIV_EXPR:
12130 : 9873417 : case FLOOR_DIV_EXPR:
12131 : 9873417 : case ROUND_DIV_EXPR:
12132 : 9873417 : case TRUNC_MOD_EXPR:
12133 : 9873417 : case CEIL_MOD_EXPR:
12134 : 9873417 : case ROUND_MOD_EXPR:
12135 : 9873417 : {
12136 : 9873417 : tree denom = TREE_OPERAND (t, 1);
12137 : 9873417 : if (!RECUR (denom, rval))
12138 : : return false;
12139 : : /* We can't call cxx_eval_outermost_constant_expr on an expression
12140 : : that hasn't been through instantiate_non_dependent_expr yet. */
12141 : 9400080 : if (!processing_template_decl)
12142 : 3958774 : denom = cxx_eval_outermost_constant_expr (denom, true);
12143 : 9400080 : if (integer_zerop (denom))
12144 : : {
12145 : 335 : if (flags & tf_error)
12146 : 35 : constexpr_error (input_location, fundef_p,
12147 : : "division by zero is not a constant expression");
12148 : 335 : return false;
12149 : : }
12150 : : else
12151 : : {
12152 : 9399745 : want_rval = true;
12153 : 9399745 : return RECUR (TREE_OPERAND (t, 0), want_rval);
12154 : : }
12155 : : }
12156 : :
12157 : 4482126 : case COMPOUND_EXPR:
12158 : 4482126 : {
12159 : : /* check_return_expr sometimes wraps a TARGET_EXPR in a
12160 : : COMPOUND_EXPR; don't get confused. */
12161 : 4482126 : tree op0 = TREE_OPERAND (t, 0);
12162 : 4482126 : tree op1 = TREE_OPERAND (t, 1);
12163 : 4482126 : STRIP_NOPS (op1);
12164 : 4482126 : if (TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
12165 : 533144 : return RECUR (op0, want_rval);
12166 : : else
12167 : 3948982 : goto binary;
12168 : : }
12169 : :
12170 : : /* If the first operand is the non-short-circuit constant, look at
12171 : : the second operand; otherwise we only care about the first one for
12172 : : potentiality. */
12173 : 15249526 : case TRUTH_AND_EXPR:
12174 : 15249526 : case TRUTH_ANDIF_EXPR:
12175 : 15249526 : tmp = boolean_true_node;
12176 : 15249526 : goto truth;
12177 : 6070343 : case TRUTH_OR_EXPR:
12178 : 6070343 : case TRUTH_ORIF_EXPR:
12179 : 6070343 : tmp = boolean_false_node;
12180 : 21319869 : truth:
12181 : 21319869 : {
12182 : 21319869 : tree op0 = TREE_OPERAND (t, 0);
12183 : 21319869 : tree op1 = TREE_OPERAND (t, 1);
12184 : 21319869 : if (!RECUR (op0, rval))
12185 : : return false;
12186 : 13675449 : if (!(flags & tf_error) && RECUR (op1, rval))
12187 : : /* When quiet, try to avoid expensive trial evaluation by first
12188 : : checking potentiality of the second operand. */
12189 : : return true;
12190 : 1282424 : if (!processing_template_decl)
12191 : 912734 : op0 = cxx_eval_outermost_constant_expr (op0, true);
12192 : 1282424 : if (tree_int_cst_equal (op0, tmp))
12193 : 5392 : return (flags & tf_error) ? RECUR (op1, rval) : false;
12194 : : else
12195 : : return true;
12196 : : }
12197 : :
12198 : : case PLUS_EXPR:
12199 : : case MULT_EXPR:
12200 : : case POINTER_PLUS_EXPR:
12201 : : case RDIV_EXPR:
12202 : : case EXACT_DIV_EXPR:
12203 : : case MIN_EXPR:
12204 : : case MAX_EXPR:
12205 : : case LSHIFT_EXPR:
12206 : : case RSHIFT_EXPR:
12207 : : case LROTATE_EXPR:
12208 : : case RROTATE_EXPR:
12209 : : case BIT_IOR_EXPR:
12210 : : case BIT_XOR_EXPR:
12211 : : case BIT_AND_EXPR:
12212 : : case TRUTH_XOR_EXPR:
12213 : : case UNORDERED_EXPR:
12214 : : case ORDERED_EXPR:
12215 : : case UNLT_EXPR:
12216 : : case UNLE_EXPR:
12217 : : case UNGT_EXPR:
12218 : : case UNGE_EXPR:
12219 : : case UNEQ_EXPR:
12220 : : case LTGT_EXPR:
12221 : : case RANGE_EXPR:
12222 : : case COMPLEX_EXPR:
12223 : 189103456 : want_rval = true;
12224 : : /* Fall through. */
12225 : 189103456 : case ARRAY_REF:
12226 : 189103456 : case ARRAY_RANGE_REF:
12227 : 189103456 : case MEMBER_REF:
12228 : 189103456 : case DOTSTAR_EXPR:
12229 : 189103456 : case MEM_REF:
12230 : 189103456 : case BINARY_LEFT_FOLD_EXPR:
12231 : 189103456 : case BINARY_RIGHT_FOLD_EXPR:
12232 : 189103456 : binary:
12233 : 396640477 : for (i = 0; i < 2; ++i)
12234 : 298916324 : if (!RECUR (TREE_OPERAND (t, i), want_rval))
12235 : : return false;
12236 : : return true;
12237 : :
12238 : : case VEC_PERM_EXPR:
12239 : 91287 : for (i = 0; i < 3; ++i)
12240 : 91253 : if (!RECUR (TREE_OPERAND (t, i), true))
12241 : : return false;
12242 : : return true;
12243 : :
12244 : 4670787 : case COND_EXPR:
12245 : 4670787 : if (COND_EXPR_IS_VEC_DELETE (t) && cxx_dialect < cxx20)
12246 : : {
12247 : 8 : if (flags & tf_error)
12248 : 2 : constexpr_error (loc, fundef_p, "%<delete[]%> is not a "
12249 : : "constant expression");
12250 : 8 : return false;
12251 : : }
12252 : : /* Fall through. */
12253 : 9105781 : case IF_STMT:
12254 : 9105781 : case VEC_COND_EXPR:
12255 : : /* If the condition is a known constant, we know which of the legs we
12256 : : care about; otherwise we only require that the condition and
12257 : : either of the legs be potentially constant. */
12258 : 9105781 : tmp = TREE_OPERAND (t, 0);
12259 : 9105781 : if (!RECUR (tmp, rval))
12260 : : return false;
12261 : 7834005 : if (!processing_template_decl)
12262 : 6734848 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
12263 : : /* potential_constant_expression* isn't told if it is called for
12264 : : manifestly_const_eval or not, so for consteval if always
12265 : : process both branches as if the condition is not a known
12266 : : constant. */
12267 : 7834005 : if (TREE_CODE (t) != IF_STMT || !IF_STMT_CONSTEVAL_P (t))
12268 : : {
12269 : 7817464 : if (integer_zerop (tmp))
12270 : 2068641 : return RECUR (TREE_OPERAND (t, 2), want_rval);
12271 : 5748823 : else if (TREE_CODE (tmp) == INTEGER_CST)
12272 : 2017459 : return RECUR (TREE_OPERAND (t, 1), want_rval);
12273 : : }
12274 : 3747905 : tmp = *jump_target;
12275 : 4371277 : for (i = 1; i < 3; ++i)
12276 : : {
12277 : 4267955 : tree this_jump_target = tmp;
12278 : 4267955 : if (potential_constant_expression_1 (TREE_OPERAND (t, i),
12279 : : want_rval, strict, now, fundef_p,
12280 : : tf_none, &this_jump_target))
12281 : : {
12282 : 3843383 : if (returns (&this_jump_target) || throws (&this_jump_target))
12283 : 1103129 : *jump_target = this_jump_target;
12284 : 2541454 : else if (!returns (jump_target) && !throws (jump_target))
12285 : : {
12286 : 2541454 : if (breaks (&this_jump_target)
12287 : 2541454 : || continues (&this_jump_target))
12288 : 29 : *jump_target = this_jump_target;
12289 : 2541454 : if (i == 1)
12290 : : {
12291 : : /* If the then branch is potentially constant, but
12292 : : does not return, check if the else branch
12293 : : couldn't return, break or continue. */
12294 : 2129205 : hash_set<tree> pset;
12295 : 2129205 : check_for_return_continue_data data = { &pset, NULL_TREE,
12296 : : NULL_TREE,
12297 : 2129205 : false };
12298 : 4258410 : if (tree ret_expr
12299 : 2129205 : = cp_walk_tree (&TREE_OPERAND (t, 2),
12300 : : check_for_return_continue, &data,
12301 : : &pset))
12302 : 17952 : *jump_target = ret_expr;
12303 : 2111253 : else if (*jump_target == NULL_TREE)
12304 : : {
12305 : 2111227 : if (data.continue_stmt)
12306 : 0 : *jump_target = data.continue_stmt;
12307 : 2111227 : else if (data.break_stmt)
12308 : 5 : *jump_target = data.break_stmt;
12309 : : }
12310 : 2129205 : if (data.could_throw)
12311 : 36338 : *jump_target = void_node;
12312 : 2129205 : }
12313 : : }
12314 : 3644583 : return true;
12315 : : }
12316 : : }
12317 : 103322 : if (flags & tf_error)
12318 : : {
12319 : 3 : if (TREE_CODE (t) == IF_STMT)
12320 : 3 : constexpr_error (loc, fundef_p, "neither branch of %<if%> is a "
12321 : : "constant expression");
12322 : : else
12323 : 0 : constexpr_error (loc, fundef_p, "expression %qE is not a "
12324 : : "constant expression", t);
12325 : : }
12326 : : return false;
12327 : :
12328 : 975 : case VEC_INIT_EXPR:
12329 : 975 : if (VEC_INIT_EXPR_IS_CONSTEXPR (t))
12330 : : return true;
12331 : 409 : if (flags & tf_error)
12332 : : {
12333 : 3 : if (constexpr_error (loc, fundef_p, "non-constant array "
12334 : : "initialization"))
12335 : 3 : diagnose_non_constexpr_vec_init (t);
12336 : : }
12337 : : return false;
12338 : :
12339 : : case TYPE_DECL:
12340 : : case TAG_DEFN:
12341 : : /* We can see these in statement-expressions. */
12342 : : return true;
12343 : :
12344 : 479129 : case CLEANUP_STMT:
12345 : 479129 : if (!RECUR (CLEANUP_BODY (t), any))
12346 : : return false;
12347 : 478239 : if (!CLEANUP_EH_ONLY (t) && !RECUR (CLEANUP_EXPR (t), any))
12348 : : return false;
12349 : : return true;
12350 : :
12351 : : case EMPTY_CLASS_EXPR:
12352 : : return true;
12353 : :
12354 : 12 : case GOTO_EXPR:
12355 : 12 : {
12356 : 12 : tree *target = &TREE_OPERAND (t, 0);
12357 : : /* Gotos representing break, continue and cdtor return are OK. */
12358 : 12 : if (breaks (target) || continues (target) || returns (target))
12359 : : {
12360 : 3 : *jump_target = *target;
12361 : 3 : return true;
12362 : : }
12363 : 9 : if (DECL_ARTIFICIAL (*target))
12364 : : /* The user didn't write this goto, this isn't the problem. */
12365 : : return true;
12366 : 9 : if (flags & tf_error)
12367 : 2 : constexpr_error (loc, fundef_p, "%<goto%> is not a constant "
12368 : : "expression");
12369 : : return false;
12370 : : }
12371 : :
12372 : 48 : case ASSERTION_STMT:
12373 : 48 : case PRECONDITION_STMT:
12374 : 48 : case POSTCONDITION_STMT:
12375 : 48 : if (!checked_contract_p (get_contract_semantic (t)))
12376 : : return true;
12377 : 33 : return RECUR (CONTRACT_CONDITION (t), rval);
12378 : :
12379 : 224 : case LABEL_EXPR:
12380 : 224 : t = LABEL_EXPR_LABEL (t);
12381 : 224 : if (DECL_ARTIFICIAL (t) || cxx_dialect >= cxx23)
12382 : : return true;
12383 : 29 : else if (flags & tf_error)
12384 : 6 : constexpr_error (loc, fundef_p, "label definition in %<constexpr%> "
12385 : : "function only available with %<-std=c++23%> or "
12386 : : "%<-std=gnu++23%>");
12387 : : return false;
12388 : :
12389 : 2686 : case ANNOTATE_EXPR:
12390 : 2686 : return RECUR (TREE_OPERAND (t, 0), rval);
12391 : :
12392 : 21735 : case BIT_CAST_EXPR:
12393 : 21735 : return RECUR (TREE_OPERAND (t, 0), rval);
12394 : :
12395 : : /* Coroutine await, yield and return expressions are not. */
12396 : 678 : case CO_AWAIT_EXPR:
12397 : 678 : case CO_YIELD_EXPR:
12398 : 678 : case CO_RETURN_EXPR:
12399 : 678 : if (flags & tf_error)
12400 : 3 : constexpr_error (cp_expr_loc_or_loc (t, input_location), fundef_p,
12401 : : "%qE is not a constant expression", t);
12402 : : return false;
12403 : :
12404 : : /* Assume a TU-local entity is not constant, we'll error later when
12405 : : instantiating. */
12406 : : case TU_LOCAL_ENTITY:
12407 : : return false;
12408 : :
12409 : 30 : case NONTYPE_ARGUMENT_PACK:
12410 : 30 : {
12411 : 30 : tree args = ARGUMENT_PACK_ARGS (t);
12412 : 30 : int len = TREE_VEC_LENGTH (args);
12413 : 90 : for (int i = 0; i < len; ++i)
12414 : 60 : if (!RECUR (TREE_VEC_ELT (args, i), any))
12415 : : return false;
12416 : : return true;
12417 : : }
12418 : :
12419 : 0 : default:
12420 : 0 : if (objc_non_constant_expr_p (t))
12421 : : return false;
12422 : :
12423 : 0 : sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t)));
12424 : 0 : gcc_unreachable ();
12425 : : return false;
12426 : : }
12427 : : #undef RECUR
12428 : : }
12429 : :
12430 : : bool
12431 : 1130704436 : potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
12432 : : bool fundef_p, tsubst_flags_t flags)
12433 : : {
12434 : 1130704436 : if (flags & tf_error)
12435 : : {
12436 : : /* Check potentiality quietly first, as that could be performed more
12437 : : efficiently in some cases (currently only for TRUTH_*_EXPR). If
12438 : : that fails, replay the check noisily to give errors. */
12439 : 5432184 : flags &= ~tf_error;
12440 : 5432184 : if (potential_constant_expression_1 (t, want_rval, strict, now, fundef_p,
12441 : : flags))
12442 : : return true;
12443 : 873 : flags |= tf_error;
12444 : : }
12445 : :
12446 : 1125273125 : tree target = NULL_TREE;
12447 : 1125273125 : return potential_constant_expression_1 (t, want_rval, strict, now, fundef_p,
12448 : 1125273125 : flags, &target);
12449 : : }
12450 : :
12451 : : /* The main entry point to the above. */
12452 : :
12453 : : bool
12454 : 352974342 : potential_constant_expression (tree t)
12455 : : {
12456 : 352974342 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
12457 : : /*now*/false, /*fundef_p*/false,
12458 : 352974342 : tf_none);
12459 : : }
12460 : :
12461 : : /* As above, but require a constant rvalue. */
12462 : :
12463 : : bool
12464 : 20694060 : potential_rvalue_constant_expression (tree t)
12465 : : {
12466 : 20694060 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
12467 : : /*now*/false, /*fundef_p*/false,
12468 : 20694060 : tf_none);
12469 : : }
12470 : :
12471 : : /* Like above, but complain about non-constant expressions. */
12472 : :
12473 : : bool
12474 : 73 : require_potential_constant_expression (tree t)
12475 : : {
12476 : 73 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
12477 : : /*now*/false, /*fundef_p*/false,
12478 : 73 : tf_warning_or_error);
12479 : : }
12480 : :
12481 : : /* Cross product of the above. */
12482 : :
12483 : : bool
12484 : 73785 : require_potential_rvalue_constant_expression (tree t)
12485 : : {
12486 : 73785 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
12487 : : /*now*/false, /*fundef_p*/false,
12488 : 73785 : tf_warning_or_error);
12489 : : }
12490 : :
12491 : : /* Like require_potential_rvalue_constant_expression, but fundef_p is true. */
12492 : :
12493 : : bool
12494 : 210 : require_potential_rvalue_constant_expression_fncheck (tree t)
12495 : : {
12496 : 210 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
12497 : : /*now*/false, /*fundef_p*/true,
12498 : 210 : tf_warning_or_error);
12499 : : }
12500 : :
12501 : : /* Like above, but don't consider PARM_DECL a potential_constant_expression. */
12502 : :
12503 : : bool
12504 : 445 : require_rvalue_constant_expression (tree t)
12505 : : {
12506 : 445 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
12507 : : /*now*/true, /*fundef_p*/false,
12508 : 445 : tf_warning_or_error);
12509 : : }
12510 : :
12511 : : /* Like potential_constant_expression, but don't consider possible constexpr
12512 : : substitution of the current function. That is, PARM_DECL qualifies under
12513 : : potential_constant_expression, but not here.
12514 : :
12515 : : This is basically what you can check when any actual constant values might
12516 : : be value-dependent. */
12517 : :
12518 : : bool
12519 : 581454774 : is_constant_expression (tree t)
12520 : : {
12521 : 581454774 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
12522 : : /*now*/true, /*fundef_p*/false,
12523 : 581454774 : tf_none);
12524 : : }
12525 : :
12526 : : /* As above, but expect an rvalue. */
12527 : :
12528 : : bool
12529 : 103236241 : is_rvalue_constant_expression (tree t)
12530 : : {
12531 : 103236241 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
12532 : : /*now*/true, /*fundef_p*/false,
12533 : 103236241 : tf_none);
12534 : : }
12535 : :
12536 : : /* Like above, but complain about non-constant expressions. */
12537 : :
12538 : : bool
12539 : 5357671 : require_constant_expression (tree t)
12540 : : {
12541 : 5357671 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
12542 : : /*now*/true, /*fundef_p*/false,
12543 : 5357671 : tf_warning_or_error);
12544 : : }
12545 : :
12546 : : /* Like is_constant_expression, but allow const variables that are not allowed
12547 : : under constexpr rules. */
12548 : :
12549 : : bool
12550 : 61480651 : is_static_init_expression (tree t)
12551 : : {
12552 : 61480651 : return potential_constant_expression_1 (t, /*want_rval*/false,
12553 : : /*strict*/false, /*now*/true,
12554 : 61480651 : /*fundef_p*/false, tf_none);
12555 : : }
12556 : :
12557 : : /* Returns true if T is a potential constant expression that is not
12558 : : instantiation-dependent, and therefore a candidate for constant folding even
12559 : : in a template. */
12560 : :
12561 : : bool
12562 : 564808121 : is_nondependent_constant_expression (tree t)
12563 : : {
12564 : 564808121 : return (!type_unknown_p (t)
12565 : 564808072 : && is_constant_expression (t)
12566 : 1026753700 : && !instantiation_dependent_expression_p (t));
12567 : : }
12568 : :
12569 : : /* Returns true if T is a potential static initializer expression that is not
12570 : : instantiation-dependent. */
12571 : :
12572 : : bool
12573 : 61480651 : is_nondependent_static_init_expression (tree t)
12574 : : {
12575 : 61480651 : return (!type_unknown_p (t)
12576 : 61480651 : && is_static_init_expression (t)
12577 : 114405629 : && !instantiation_dependent_expression_p (t));
12578 : : }
12579 : :
12580 : : /* True iff FN is an implicitly constexpr function. */
12581 : :
12582 : : bool
12583 : 138811 : decl_implicit_constexpr_p (tree fn)
12584 : : {
12585 : 138811 : if (!(flag_implicit_constexpr
12586 : 0 : && TREE_CODE (fn) == FUNCTION_DECL
12587 : 0 : && DECL_DECLARED_CONSTEXPR_P (fn)))
12588 : : return false;
12589 : :
12590 : 0 : if (DECL_CLONED_FUNCTION_P (fn))
12591 : 0 : fn = DECL_CLONED_FUNCTION (fn);
12592 : :
12593 : 0 : return (DECL_LANG_SPECIFIC (fn)
12594 : 0 : && DECL_LANG_SPECIFIC (fn)->u.fn.implicit_constexpr);
12595 : : }
12596 : :
12597 : : /* Finalize constexpr processing after parsing. */
12598 : :
12599 : : void
12600 : 94314 : fini_constexpr (void)
12601 : : {
12602 : : /* The contexpr call and fundef copies tables are no longer needed. */
12603 : 94314 : constexpr_call_table = NULL;
12604 : 94314 : fundef_copies_table = NULL;
12605 : 94314 : }
12606 : :
12607 : : #include "gt-cp-constexpr.h"
|