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 : : #include "contracts.h"
45 : :
46 : : static bool verify_constant (tree, bool, bool *, bool *);
47 : : #define VERIFY_CONSTANT(X) \
48 : : do { \
49 : : if (verify_constant ((X), ctx->quiet, non_constant_p, overflow_p)) \
50 : : return t; \
51 : : } while (0)
52 : :
53 : : static HOST_WIDE_INT find_array_ctor_elt (tree ary, tree dindex,
54 : : bool insert = false);
55 : : static int array_index_cmp (tree key, tree index);
56 : :
57 : : /* Returns true iff FUN is an instantiation of a constexpr function
58 : : template or a defaulted constexpr function. */
59 : :
60 : : bool
61 : 72730342 : is_instantiation_of_constexpr (tree fun)
62 : : {
63 : 96953718 : return ((DECL_TEMPLOID_INSTANTIATION (fun)
64 : 48896825 : && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun)))
65 : 106034238 : || (DECL_DEFAULTED_FN (fun)
66 : 1601937 : && DECL_DECLARED_CONSTEXPR_P (fun)));
67 : : }
68 : :
69 : : /* Return true if T is a literal type. */
70 : :
71 : : bool
72 : 105167323 : literal_type_p (tree t)
73 : : {
74 : 102625330 : if (SCALAR_TYPE_P (t)
75 : 42586272 : || VECTOR_TYPE_P (t)
76 : 42574496 : || TYPE_REF_P (t)
77 : 140032844 : || (VOID_TYPE_P (t) && cxx_dialect >= cxx14))
78 : : return true;
79 : 32108757 : if (CLASS_TYPE_P (t))
80 : : {
81 : 30850171 : t = complete_type (t);
82 : 30850171 : gcc_assert (COMPLETE_TYPE_P (t) || errorcount);
83 : 30850171 : return CLASSTYPE_LITERAL_P (t);
84 : : }
85 : 1258586 : if (TREE_CODE (t) == ARRAY_TYPE)
86 : 1258408 : return literal_type_p (strip_array_types (t));
87 : : return false;
88 : : }
89 : :
90 : : /* If DECL is a variable declared `constexpr', require its type
91 : : be literal. Return error_mark_node if we give an error, the
92 : : DECL otherwise. */
93 : :
94 : : tree
95 : 280977680 : ensure_literal_type_for_constexpr_object (tree decl)
96 : : {
97 : 280977680 : tree type = TREE_TYPE (decl);
98 : 280977680 : if (VAR_P (decl)
99 : 99269473 : && (DECL_DECLARED_CONSTEXPR_P (decl)
100 : 77329965 : || var_in_constexpr_fn (decl))
101 : 310751798 : && !processing_template_decl)
102 : : {
103 : 19823638 : tree stype = strip_array_types (type);
104 : 19823638 : if (CLASS_TYPE_P (stype) && !COMPLETE_TYPE_P (complete_type (stype)))
105 : : /* Don't complain here, we'll complain about incompleteness
106 : : when we try to initialize the variable. */;
107 : 19823632 : else if (!literal_type_p (type))
108 : : {
109 : 10042 : if (DECL_DECLARED_CONSTEXPR_P (decl))
110 : : {
111 : 49 : auto_diagnostic_group d;
112 : 49 : error_at (DECL_SOURCE_LOCATION (decl),
113 : : "the type %qT of %<constexpr%> variable %qD "
114 : : "is not literal", type, decl);
115 : 49 : explain_non_literal_class (type);
116 : 49 : decl = error_mark_node;
117 : 49 : }
118 : 9993 : else if (cxx_dialect < cxx23)
119 : : {
120 : 2409 : if (!is_instantiation_of_constexpr (current_function_decl))
121 : : {
122 : 8 : auto_diagnostic_group d;
123 : 8 : error_at (DECL_SOURCE_LOCATION (decl),
124 : : "variable %qD of non-literal type %qT in "
125 : : "%<constexpr%> function only available with "
126 : : "%<-std=c++23%> or %<-std=gnu++23%>", decl, type);
127 : 8 : explain_non_literal_class (type);
128 : 8 : decl = error_mark_node;
129 : 8 : }
130 : 2409 : cp_function_chain->invalid_constexpr = true;
131 : : }
132 : : }
133 : 19813590 : else if (DECL_DECLARED_CONSTEXPR_P (decl)
134 : 19813590 : && variably_modified_type_p (type, NULL_TREE))
135 : : {
136 : 4 : error_at (DECL_SOURCE_LOCATION (decl),
137 : : "%<constexpr%> variable %qD has variably-modified "
138 : : "type %qT", decl, type);
139 : 4 : decl = error_mark_node;
140 : : }
141 : : }
142 : 280977680 : return decl;
143 : : }
144 : :
145 : : /* Issue a diagnostic with text GMSGID for constructs that are invalid in
146 : : constexpr functions. CONSTEXPR_FUNDEF_P is true if we're checking
147 : : a constexpr function body; if so, don't report hard errors and issue
148 : : a pedwarn pre-C++23, or a warning in C++23, if requested by
149 : : -Winvalid-constexpr. Otherwise, we're not in the context where we are
150 : : checking if a function can be marked 'constexpr', so give a hard error. */
151 : :
152 : : ATTRIBUTE_GCC_DIAG(3,4)
153 : : static bool
154 : 897 : constexpr_error (location_t location, bool constexpr_fundef_p,
155 : : const char *gmsgid, ...)
156 : : {
157 : 897 : diagnostics::diagnostic_info diagnostic;
158 : 897 : va_list ap;
159 : 897 : rich_location richloc (line_table, location);
160 : 897 : va_start (ap, gmsgid);
161 : 897 : bool ret;
162 : 897 : if (!constexpr_fundef_p)
163 : : {
164 : : /* Report an error that cannot be suppressed. */
165 : 678 : diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
166 : : diagnostics::kind::error);
167 : 678 : ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
168 : : }
169 : 219 : else if (warn_invalid_constexpr)
170 : : {
171 : 181 : diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
172 : 181 : (cxx_dialect < cxx23
173 : : ? diagnostics::kind::pedwarn
174 : : : diagnostics::kind::warning));
175 : 181 : diagnostic.m_option_id = OPT_Winvalid_constexpr;
176 : 181 : ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
177 : : }
178 : : else
179 : : ret = false;
180 : 897 : va_end (ap);
181 : 1794 : return ret;
182 : 897 : }
183 : :
184 : : struct constexpr_fundef_hasher : ggc_ptr_hash<constexpr_fundef>
185 : : {
186 : : static hashval_t hash (const constexpr_fundef *);
187 : : static bool equal (const constexpr_fundef *, const constexpr_fundef *);
188 : : };
189 : :
190 : : /* This table holds all constexpr function definitions seen in
191 : : the current translation unit. */
192 : :
193 : : static GTY (()) hash_table<constexpr_fundef_hasher> *constexpr_fundef_table;
194 : :
195 : : /* Utility function used for managing the constexpr function table.
196 : : Return true if the entries pointed to by P and Q are for the
197 : : same constexpr function. */
198 : :
199 : : inline bool
200 : 429930337 : constexpr_fundef_hasher::equal (const constexpr_fundef *lhs,
201 : : const constexpr_fundef *rhs)
202 : : {
203 : 413442078 : return lhs->decl == rhs->decl;
204 : : }
205 : :
206 : : /* Utility function used for managing the constexpr function table.
207 : : Return a hash value for the entry pointed to by Q. */
208 : :
209 : : inline hashval_t
210 : 415590196 : constexpr_fundef_hasher::hash (const constexpr_fundef *fundef)
211 : : {
212 : 415590196 : return DECL_UID (fundef->decl);
213 : : }
214 : :
215 : : /* Return a previously saved definition of function FUN. */
216 : :
217 : : constexpr_fundef *
218 : 43347829 : retrieve_constexpr_fundef (tree fun)
219 : : {
220 : 43347829 : if (constexpr_fundef_table == NULL)
221 : : return NULL;
222 : :
223 : 43344363 : constexpr_fundef fundef = { fun, NULL_TREE, NULL_TREE, NULL_TREE };
224 : 43344363 : return constexpr_fundef_table->find (&fundef);
225 : : }
226 : :
227 : : /* Check whether the parameter and return types of FUN are valid for a
228 : : constexpr function, and complain if COMPLAIN. */
229 : :
230 : : bool
231 : 18965024 : is_valid_constexpr_fn (tree fun, bool complain)
232 : : {
233 : 18965024 : bool ret = true;
234 : :
235 : 37930048 : if (DECL_INHERITED_CTOR (fun)
236 : 2098449 : && TREE_CODE (fun) == TEMPLATE_DECL)
237 : : {
238 : 0 : ret = false;
239 : 0 : if (complain)
240 : 0 : error ("inherited constructor %qD is not %<constexpr%>",
241 : 0 : DECL_INHERITED_CTOR (fun));
242 : : }
243 : : else
244 : : {
245 : 18965024 : for (tree parm = FUNCTION_FIRST_USER_PARM (fun);
246 : 37004411 : parm != NULL_TREE; parm = TREE_CHAIN (parm))
247 : 18039387 : if (!literal_type_p (TREE_TYPE (parm)))
248 : : {
249 : 9205 : ret = false;
250 : 9205 : if (complain)
251 : : {
252 : 22 : auto_diagnostic_group d;
253 : 22 : if (constexpr_error (input_location, /*constexpr_fundef_p*/true,
254 : : "invalid type for parameter %d of "
255 : : "%<constexpr%> function %q+#D",
256 : 22 : DECL_PARM_INDEX (parm), fun))
257 : 20 : explain_non_literal_class (TREE_TYPE (parm));
258 : 22 : }
259 : : }
260 : : }
261 : :
262 : 30455943 : if (LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun)) && cxx_dialect < cxx17)
263 : : {
264 : 3 : ret = false;
265 : 3 : if (complain)
266 : 3 : inform (DECL_SOURCE_LOCATION (fun),
267 : : "lambdas are implicitly %<constexpr%> only in C++17 and later");
268 : : }
269 : 37930042 : else if (DECL_DESTRUCTOR_P (fun) && cxx_dialect < cxx20)
270 : : {
271 : 96 : ret = false;
272 : 96 : if (complain)
273 : 0 : error_at (DECL_SOURCE_LOCATION (fun),
274 : : "%<constexpr%> destructors only available with "
275 : : "%<-std=c++20%> or %<-std=gnu++20%>");
276 : : }
277 : 18964925 : else if (!DECL_CONSTRUCTOR_P (fun) && !DECL_DESTRUCTOR_P (fun))
278 : : {
279 : 16628546 : tree rettype = TREE_TYPE (TREE_TYPE (fun));
280 : 16628546 : if (!literal_type_p (rettype))
281 : : {
282 : 51080 : ret = false;
283 : 51080 : if (complain)
284 : : {
285 : 12 : auto_diagnostic_group d;
286 : 12 : if (constexpr_error (input_location, /*constexpr_fundef_p*/true,
287 : : "invalid return type %qT of %<constexpr%> "
288 : : "function %q+D", rettype, fun))
289 : 12 : explain_non_literal_class (rettype);
290 : 12 : }
291 : : }
292 : :
293 : : /* C++14 DR 1684 removed this restriction. */
294 : 16628546 : if (cxx_dialect < cxx14
295 : 31516 : && DECL_IOBJ_MEMBER_FUNCTION_P (fun)
296 : 16629633 : && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun)))
297 : : {
298 : 1 : ret = false;
299 : 1 : if (complain)
300 : : {
301 : 1 : auto_diagnostic_group d;
302 : 1 : if (pedwarn (DECL_SOURCE_LOCATION (fun), OPT_Wpedantic,
303 : : "enclosing class of %<constexpr%> non-static"
304 : : " member function %q+#D is not a literal type",
305 : : fun))
306 : 1 : explain_non_literal_class (DECL_CONTEXT (fun));
307 : 1 : }
308 : : }
309 : : }
310 : 2336379 : else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun)) && cxx_dialect < cxx26)
311 : : {
312 : 50 : ret = false;
313 : 50 : if (complain)
314 : : {
315 : 10 : if (DECL_CONSTRUCTOR_P (fun))
316 : 8 : error ("%<constexpr%> constructor in %q#T that has "
317 : : "virtual base classes only available with "
318 : 8 : "%<-std=c++2c%> or %<-std=gnu++2c%>", DECL_CONTEXT (fun));
319 : : else
320 : 2 : error ("%<constexpr%> destructor in %q#T that has "
321 : : "virtual base classes only available with "
322 : 2 : "%<-std=c++2c%> or %<-std=gnu++2c%>", DECL_CONTEXT (fun));
323 : : }
324 : : }
325 : :
326 : 18965024 : return ret;
327 : : }
328 : :
329 : : /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF
330 : : for a member of an anonymous aggregate, INIT is the initializer for that
331 : : member, and VEC_OUTER is the vector of constructor elements for the class
332 : : whose constructor we are processing. Add the initializer to the vector
333 : : and return true to indicate success. */
334 : :
335 : : static bool
336 : 892 : build_anon_member_initialization (tree member, tree init,
337 : : vec<constructor_elt, va_gc> **vec_outer)
338 : : {
339 : : /* MEMBER presents the relevant fields from the inside out, but we need
340 : : to build up the initializer from the outside in so that we can reuse
341 : : previously built CONSTRUCTORs if this is, say, the second field in an
342 : : anonymous struct. So we use a vec as a stack. */
343 : 892 : auto_vec<tree, 2> fields;
344 : 1880 : do
345 : : {
346 : 1880 : fields.safe_push (TREE_OPERAND (member, 1));
347 : 1880 : member = TREE_OPERAND (member, 0);
348 : : }
349 : 1880 : while (ANON_AGGR_TYPE_P (TREE_TYPE (member))
350 : 3766 : && TREE_CODE (member) == COMPONENT_REF);
351 : :
352 : : /* VEC has the constructor elements vector for the context of FIELD.
353 : : If FIELD is an anonymous aggregate, we will push inside it. */
354 : : vec<constructor_elt, va_gc> **vec = vec_outer;
355 : : tree field;
356 : 1880 : while (field = fields.pop(),
357 : 1880 : ANON_AGGR_TYPE_P (TREE_TYPE (field)))
358 : : {
359 : 988 : tree ctor;
360 : : /* If there is already an outer constructor entry for the anonymous
361 : : aggregate FIELD, use it; otherwise, insert one. */
362 : 988 : if (vec_safe_is_empty (*vec)
363 : 198 : || (*vec)->last().index != field)
364 : : {
365 : 886 : ctor = build_constructor (TREE_TYPE (field), NULL);
366 : 886 : CONSTRUCTOR_APPEND_ELT (*vec, field, ctor);
367 : : }
368 : : else
369 : 102 : ctor = (*vec)->last().value;
370 : 988 : vec = &CONSTRUCTOR_ELTS (ctor);
371 : : }
372 : :
373 : : /* Now we're at the innermost field, the one that isn't an anonymous
374 : : aggregate. Add its initializer to the CONSTRUCTOR and we're done. */
375 : 892 : gcc_assert (fields.is_empty());
376 : 892 : CONSTRUCTOR_APPEND_ELT (*vec, field, init);
377 : :
378 : 892 : return true;
379 : 892 : }
380 : :
381 : : /* Subroutine of build_constexpr_constructor_member_initializers.
382 : : The expression tree T represents a data member initialization
383 : : in a (constexpr) constructor definition. Build a pairing of
384 : : the data member with its initializer, and prepend that pair
385 : : to the existing initialization pair INITS. */
386 : :
387 : : static bool
388 : 3124460 : build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec)
389 : : {
390 : 3394942 : tree member, init;
391 : 3394942 : if (TREE_CODE (t) == CLEANUP_POINT_EXPR)
392 : 1451421 : t = TREE_OPERAND (t, 0);
393 : 3394942 : if (TREE_CODE (t) == EXPR_STMT)
394 : 1450646 : t = TREE_OPERAND (t, 0);
395 : 3394942 : if (t == error_mark_node)
396 : : return false;
397 : 3394935 : if (TREE_CODE (t) == STATEMENT_LIST)
398 : : {
399 : 3632611 : for (tree stmt : tsi_range (t))
400 : 240972 : if (! build_data_member_initialization (stmt, vec))
401 : 7 : return false;
402 : : return true;
403 : : }
404 : 3127749 : if (TREE_CODE (t) == CLEANUP_STMT)
405 : : {
406 : : /* We can't see a CLEANUP_STMT in a constructor for a literal class,
407 : : but we can in a constexpr constructor for a non-literal class. Just
408 : : ignore it; either all the initialization will be constant, in which
409 : : case the cleanup can't run, or it can't be constexpr.
410 : : Still recurse into CLEANUP_BODY. */
411 : 252531 : return build_data_member_initialization (CLEANUP_BODY (t), vec);
412 : : }
413 : 2875218 : if (TREE_CODE (t) == CONVERT_EXPR)
414 : 1739287 : t = TREE_OPERAND (t, 0);
415 : 2875218 : if (TREE_CODE (t) == INIT_EXPR
416 : : /* vptr initialization shows up as a MODIFY_EXPR. In C++14 we only
417 : : use what this function builds for cx_check_missing_mem_inits, and
418 : : assignment in the ctor body doesn't count. */
419 : 1223107 : || (cxx_dialect < cxx14 && TREE_CODE (t) == MODIFY_EXPR))
420 : : {
421 : 1652593 : member = TREE_OPERAND (t, 0);
422 : 1652593 : init = break_out_target_exprs (TREE_OPERAND (t, 1));
423 : : }
424 : 1222625 : else if (TREE_CODE (t) == CALL_EXPR)
425 : : {
426 : 1039804 : tree fn = get_callee_fndecl (t);
427 : 2079608 : if (!fn || !DECL_CONSTRUCTOR_P (fn))
428 : : /* We're only interested in calls to subobject constructors. */
429 : : return true;
430 : 918341 : member = CALL_EXPR_ARG (t, 0);
431 : : /* We don't use build_cplus_new here because it complains about
432 : : abstract bases. Leaving the call unwrapped means that it has the
433 : : wrong type, but cxx_eval_constant_expression doesn't care. */
434 : 918341 : init = break_out_target_exprs (t);
435 : : }
436 : 182821 : else if (TREE_CODE (t) == BIND_EXPR)
437 : 17951 : return build_data_member_initialization (BIND_EXPR_BODY (t), vec);
438 : : else
439 : : /* Don't add anything else to the CONSTRUCTOR. */
440 : : return true;
441 : 2570934 : if (INDIRECT_REF_P (member))
442 : 30448 : member = TREE_OPERAND (member, 0);
443 : 2570934 : if (TREE_CODE (member) == NOP_EXPR)
444 : : {
445 : 219530 : tree op = member;
446 : 219530 : STRIP_NOPS (op);
447 : 219530 : if (TREE_CODE (op) == ADDR_EXPR)
448 : : {
449 : 299 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
450 : : (TREE_TYPE (TREE_TYPE (op)),
451 : : TREE_TYPE (TREE_TYPE (member))));
452 : : /* Initializing a cv-qualified member; we need to look through
453 : : the const_cast. */
454 : : member = op;
455 : : }
456 : 219231 : else if (op == current_class_ptr
457 : 438350 : && (same_type_ignoring_top_level_qualifiers_p
458 : 219119 : (TREE_TYPE (TREE_TYPE (member)),
459 : : current_class_type)))
460 : : /* Delegating constructor. */
461 : : member = op;
462 : : else
463 : : {
464 : : /* This is an initializer for an empty base; keep it for now so
465 : : we can check it in cxx_eval_bare_aggregate. */
466 : 199179 : gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member))));
467 : : }
468 : : }
469 : 2570934 : if (TREE_CODE (member) == ADDR_EXPR)
470 : 729558 : member = TREE_OPERAND (member, 0);
471 : 2570934 : if (TREE_CODE (member) == COMPONENT_REF)
472 : : {
473 : 2339960 : tree aggr = TREE_OPERAND (member, 0);
474 : 2339960 : if (TREE_CODE (aggr) == VAR_DECL)
475 : : /* Initializing a local variable, don't add anything. */
476 : : return true;
477 : 2339958 : if (TREE_CODE (aggr) != COMPONENT_REF)
478 : : /* Normal member initialization. */
479 : 2338799 : member = TREE_OPERAND (member, 1);
480 : 1159 : else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr)))
481 : : /* Initializing a member of an anonymous union. */
482 : 892 : return build_anon_member_initialization (member, init, vec);
483 : : else
484 : : /* We're initializing a vtable pointer in a base. Leave it as
485 : : COMPONENT_REF so we remember the path to get to the vfield. */
486 : 267 : gcc_assert (TREE_TYPE (member) == vtbl_ptr_type_node);
487 : : }
488 : :
489 : : /* Value-initialization can produce multiple initializers for the
490 : : same field; use the last one. */
491 : 3144082 : if (!vec_safe_is_empty (*vec) && (*vec)->last().index == member)
492 : 32 : (*vec)->last().value = init;
493 : : else
494 : 2570008 : CONSTRUCTOR_APPEND_ELT (*vec, member, init);
495 : : return true;
496 : : }
497 : :
498 : : /* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval.
499 : : In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a
500 : : BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */
501 : :
502 : : static bool
503 : 2363 : check_constexpr_bind_expr_vars (tree t)
504 : : {
505 : 2363 : gcc_assert (TREE_CODE (t) == BIND_EXPR);
506 : :
507 : 3185 : for (tree var = BIND_EXPR_VARS (t); var; var = DECL_CHAIN (var))
508 : 834 : if (TREE_CODE (var) == TYPE_DECL
509 : 815 : && DECL_IMPLICIT_TYPEDEF_P (var)
510 : 856 : && !LAMBDA_TYPE_P (TREE_TYPE (var)))
511 : : return false;
512 : : return true;
513 : : }
514 : :
515 : : /* Subroutine of check_constexpr_ctor_body. */
516 : :
517 : : static bool
518 : 4069 : check_constexpr_ctor_body_1 (tree last, tree list)
519 : : {
520 : 4069 : switch (TREE_CODE (list))
521 : : {
522 : 6 : case DECL_EXPR:
523 : 6 : if (TREE_CODE (DECL_EXPR_DECL (list)) == USING_DECL
524 : 6 : || TREE_CODE (DECL_EXPR_DECL (list)) == TYPE_DECL)
525 : : return true;
526 : : return false;
527 : :
528 : 3 : case CLEANUP_POINT_EXPR:
529 : 3 : return check_constexpr_ctor_body (last, TREE_OPERAND (list, 0),
530 : 3 : /*complain=*/false);
531 : :
532 : 2026 : case BIND_EXPR:
533 : 2026 : if (!check_constexpr_bind_expr_vars (list)
534 : 2026 : || !check_constexpr_ctor_body (last, BIND_EXPR_BODY (list),
535 : : /*complain=*/false))
536 : 11 : return false;
537 : : return true;
538 : :
539 : : case USING_STMT:
540 : : case STATIC_ASSERT:
541 : : case DEBUG_BEGIN_STMT:
542 : : return true;
543 : :
544 : : default:
545 : : return false;
546 : : }
547 : : }
548 : :
549 : : /* Make sure that there are no statements after LAST in the constructor
550 : : body represented by LIST. */
551 : :
552 : : bool
553 : 3494173 : check_constexpr_ctor_body (tree last, tree list, bool complain)
554 : : {
555 : : /* C++14 doesn't require a constexpr ctor to have an empty body. */
556 : 3494173 : if (cxx_dialect >= cxx14)
557 : : return true;
558 : :
559 : 13580 : bool ok = true;
560 : 13580 : if (TREE_CODE (list) == STATEMENT_LIST)
561 : : {
562 : 13567 : tree_stmt_iterator i = tsi_last (list);
563 : 17605 : for (; !tsi_end_p (i); tsi_prev (&i))
564 : : {
565 : 15482 : tree t = tsi_stmt (i);
566 : 15482 : if (t == last)
567 : : break;
568 : 4056 : if (!check_constexpr_ctor_body_1 (last, t))
569 : : {
570 : : ok = false;
571 : : break;
572 : : }
573 : : }
574 : : }
575 : 13 : else if (list != last
576 : 13 : && !check_constexpr_ctor_body_1 (last, list))
577 : : ok = false;
578 : 13567 : if (!ok)
579 : : {
580 : 22 : if (complain)
581 : 16 : error ("%<constexpr%> constructor does not have empty body");
582 : 22 : DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false;
583 : : }
584 : : return ok;
585 : : }
586 : :
587 : : /* V is a vector of constructor elements built up for the base and member
588 : : initializers of a constructor for TYPE. They need to be in increasing
589 : : offset order, which they might not be yet if TYPE has a primary base
590 : : which is not first in the base-clause or a vptr and at least one base
591 : : all of which are non-primary. */
592 : :
593 : : static vec<constructor_elt, va_gc> *
594 : 2077587 : sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v)
595 : : {
596 : 2077587 : tree pri = CLASSTYPE_PRIMARY_BINFO (type);
597 : 2077587 : tree field_type;
598 : 2077587 : unsigned i;
599 : 2077587 : constructor_elt *ce;
600 : :
601 : 2077587 : if (pri)
602 : 32350 : field_type = BINFO_TYPE (pri);
603 : 2045237 : else if (TYPE_CONTAINS_VPTR_P (type))
604 : 26621 : field_type = vtbl_ptr_type_node;
605 : : else
606 : : return v;
607 : :
608 : : /* Find the element for the primary base or vptr and move it to the
609 : : beginning of the vec. */
610 : 76482 : for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
611 : 49956 : if (TREE_TYPE (ce->index) == field_type)
612 : : break;
613 : :
614 : 71184 : if (i > 0 && i < vec_safe_length (v))
615 : : {
616 : 23 : vec<constructor_elt, va_gc> &vref = *v;
617 : 23 : constructor_elt elt = vref[i];
618 : 46 : for (; i > 0; --i)
619 : 23 : vref[i] = vref[i-1];
620 : 23 : vref[0] = elt;
621 : : }
622 : :
623 : : return v;
624 : : }
625 : :
626 : : /* Build compile-time evalable representations of member-initializer list
627 : : for a constexpr constructor. */
628 : :
629 : : static tree
630 : 2097642 : build_constexpr_constructor_member_initializers (tree type, tree body)
631 : : {
632 : 2097642 : vec<constructor_elt, va_gc> *vec = NULL;
633 : 2097642 : bool ok = true;
634 : 3139500 : while (true)
635 : 3139500 : switch (TREE_CODE (body))
636 : : {
637 : 1041812 : case MUST_NOT_THROW_EXPR:
638 : 1041812 : case EH_SPEC_BLOCK:
639 : 1041812 : body = TREE_OPERAND (body, 0);
640 : 1041812 : break;
641 : :
642 : 46 : case STATEMENT_LIST:
643 : 3139565 : for (tree stmt : tsi_range (body))
644 : : {
645 : 95 : body = stmt;
646 : 95 : if (TREE_CODE (body) == BIND_EXPR)
647 : : break;
648 : : }
649 : : break;
650 : :
651 : 2097642 : case BIND_EXPR:
652 : 2097642 : body = BIND_EXPR_BODY (body);
653 : 2097642 : goto found;
654 : :
655 : 0 : default:
656 : 0 : gcc_unreachable ();
657 : : }
658 : 2097642 : found:
659 : 2097642 : if (TREE_CODE (body) == TRY_BLOCK)
660 : : {
661 : 26 : body = TREE_OPERAND (body, 0);
662 : 26 : if (TREE_CODE (body) == BIND_EXPR)
663 : 26 : body = BIND_EXPR_BODY (body);
664 : : }
665 : 2097642 : if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
666 : : {
667 : 1313402 : body = TREE_OPERAND (body, 0);
668 : 1313402 : if (TREE_CODE (body) == EXPR_STMT)
669 : 1313402 : body = TREE_OPERAND (body, 0);
670 : 1313402 : if (TREE_CODE (body) == INIT_EXPR
671 : 1313402 : && (same_type_ignoring_top_level_qualifiers_p
672 : 0 : (TREE_TYPE (TREE_OPERAND (body, 0)),
673 : : current_class_type)))
674 : : {
675 : : /* Trivial copy. */
676 : 0 : return TREE_OPERAND (body, 1);
677 : : }
678 : 1313402 : ok = build_data_member_initialization (body, &vec);
679 : : }
680 : 784240 : else if (TREE_CODE (body) == STATEMENT_LIST)
681 : : {
682 : 2353552 : for (tree stmt : tsi_range (body))
683 : : {
684 : 1569699 : ok = build_data_member_initialization (stmt, &vec);
685 : 1569699 : if (!ok)
686 : : break;
687 : : }
688 : : }
689 : 387 : else if (EXPR_P (body))
690 : 387 : ok = build_data_member_initialization (body, &vec);
691 : : else
692 : 0 : gcc_assert (errorcount > 0);
693 : 2097642 : if (ok)
694 : : {
695 : 2097635 : if (vec_safe_length (vec) > 0)
696 : : {
697 : : /* In a delegating constructor, return the target. */
698 : 1996764 : constructor_elt *ce = &(*vec)[0];
699 : 1996764 : if (ce->index == current_class_ptr)
700 : : {
701 : 20048 : body = ce->value;
702 : 20048 : vec_free (vec);
703 : 20048 : return body;
704 : : }
705 : : }
706 : 2077587 : vec = sort_constexpr_mem_initializers (type, vec);
707 : 2077587 : return build_constructor (type, vec);
708 : : }
709 : : else
710 : 7 : return error_mark_node;
711 : : }
712 : :
713 : : /* We have an expression tree T that represents a call, either CALL_EXPR
714 : : or AGGR_INIT_EXPR. If the call is lexically to a named function,
715 : : return the _DECL for that function. */
716 : :
717 : : static tree
718 : 316090591 : get_function_named_in_call (tree t)
719 : : {
720 : 316090591 : tree callee = cp_get_callee (t);
721 : 316090591 : tree fun = cp_get_fndecl_from_callee (callee, /*fold*/false);
722 : 316090591 : return fun ? fun : callee;
723 : : }
724 : :
725 : : /* Subroutine of check_constexpr_fundef. BODY is the body of a function
726 : : declared to be constexpr, or a sub-statement thereof. Returns the
727 : : return value if suitable, error_mark_node for a statement not allowed in
728 : : a constexpr function, or NULL_TREE if no return value was found. */
729 : :
730 : : tree
731 : 68667 : constexpr_fn_retval (tree body)
732 : : {
733 : 75199 : switch (TREE_CODE (body))
734 : : {
735 : 18540 : case STATEMENT_LIST:
736 : 18540 : {
737 : 18540 : tree expr = NULL_TREE;
738 : 55552 : for (tree stmt : tsi_range (body))
739 : : {
740 : 37039 : tree s = constexpr_fn_retval (stmt);
741 : 37039 : if (s == error_mark_node)
742 : : return error_mark_node;
743 : 37012 : else if (s == NULL_TREE)
744 : : /* Keep iterating. */;
745 : 18503 : else if (expr)
746 : : /* Multiple return statements. */
747 : : return error_mark_node;
748 : : else
749 : : expr = s;
750 : : }
751 : : return expr;
752 : : }
753 : :
754 : 31576 : case RETURN_EXPR:
755 : 31576 : return break_out_target_exprs (TREE_OPERAND (body, 0));
756 : :
757 : 20 : case DECL_EXPR:
758 : 20 : {
759 : 20 : tree decl = DECL_EXPR_DECL (body);
760 : 20 : if (TREE_CODE (decl) == USING_DECL
761 : : /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */
762 : 20 : || DECL_ARTIFICIAL (decl))
763 : : return NULL_TREE;
764 : 6 : return error_mark_node;
765 : : }
766 : :
767 : 6201 : case CLEANUP_POINT_EXPR:
768 : 6201 : return constexpr_fn_retval (TREE_OPERAND (body, 0));
769 : :
770 : 337 : case BIND_EXPR:
771 : 337 : if (!check_constexpr_bind_expr_vars (body))
772 : 6 : return error_mark_node;
773 : 331 : return constexpr_fn_retval (BIND_EXPR_BODY (body));
774 : :
775 : : case USING_STMT:
776 : : case DEBUG_BEGIN_STMT:
777 : : return NULL_TREE;
778 : :
779 : 0 : case CALL_EXPR:
780 : 0 : {
781 : 0 : tree fun = get_function_named_in_call (body);
782 : 0 : if (fun != NULL_TREE
783 : 0 : && fndecl_built_in_p (fun, BUILT_IN_UNREACHABLE))
784 : : return NULL_TREE;
785 : : }
786 : : /* Fallthru. */
787 : :
788 : 30 : default:
789 : 30 : return error_mark_node;
790 : : }
791 : : }
792 : :
793 : : /* Subroutine of check_constexpr_fundef. BODY is the DECL_SAVED_TREE of
794 : : FUN; do the necessary transformations to turn it into a single expression
795 : : that we can store in the hash table. */
796 : :
797 : : static tree
798 : 18363263 : massage_constexpr_body (tree fun, tree body)
799 : : {
800 : 36726526 : if (DECL_CONSTRUCTOR_P (fun))
801 : 2097642 : body = build_constexpr_constructor_member_initializers
802 : 2097642 : (DECL_CONTEXT (fun), body);
803 : 16265621 : else if (cxx_dialect < cxx14)
804 : : {
805 : 31393 : if (TREE_CODE (body) == EH_SPEC_BLOCK)
806 : 1 : body = EH_SPEC_STMTS (body);
807 : 31393 : if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
808 : 20294 : body = TREE_OPERAND (body, 0);
809 : 31393 : body = constexpr_fn_retval (body);
810 : : }
811 : 18363263 : return body;
812 : : }
813 : :
814 : : /* CTYPE is a type constructed from BODY. Return true if some
815 : : bases/fields are uninitialized, and complain if COMPLAIN. */
816 : :
817 : : static bool
818 : 1752528 : cx_check_missing_mem_inits (tree ctype, tree body, bool complain)
819 : : {
820 : : /* We allow uninitialized bases/fields in C++20. */
821 : 1752528 : if (cxx_dialect >= cxx20)
822 : : return false;
823 : :
824 : 793962 : unsigned nelts = 0;
825 : :
826 : 793962 : if (body)
827 : : {
828 : 793958 : if (TREE_CODE (body) != CONSTRUCTOR)
829 : : return false;
830 : 793702 : nelts = CONSTRUCTOR_NELTS (body);
831 : : }
832 : 793706 : tree field = TYPE_FIELDS (ctype);
833 : :
834 : 793706 : if (TREE_CODE (ctype) == UNION_TYPE)
835 : : {
836 : 8186 : if (nelts == 0 && next_aggregate_field (field))
837 : : {
838 : 2 : if (complain)
839 : 2 : error ("%<constexpr%> constructor for union %qT must "
840 : : "initialize exactly one non-static data member", ctype);
841 : 2 : return true;
842 : : }
843 : 8184 : return false;
844 : : }
845 : :
846 : : /* Iterate over the CONSTRUCTOR, checking any missing fields don't
847 : : need an explicit initialization. */
848 : : bool bad = false;
849 : 1695816 : for (unsigned i = 0; i <= nelts; ++i)
850 : : {
851 : 1695816 : tree index = NULL_TREE;
852 : 1695816 : if (i < nelts)
853 : : {
854 : 910296 : index = CONSTRUCTOR_ELT (body, i)->index;
855 : : /* Skip vptr adjustment represented with a COMPONENT_REF. */
856 : 910296 : if (TREE_CODE (index) != FIELD_DECL)
857 : 67592 : continue;
858 : : }
859 : :
860 : 39493251 : for (; field != index; field = DECL_CHAIN (field))
861 : : {
862 : 37865096 : tree ftype;
863 : 37865096 : if (TREE_CODE (field) != FIELD_DECL)
864 : 75595883 : continue;
865 : 134219 : if (DECL_UNNAMED_BIT_FIELD (field))
866 : 16 : continue;
867 : : /* Artificial fields can be ignored unless they're bases. */
868 : 134203 : if (DECL_ARTIFICIAL (field) && !DECL_FIELD_IS_BASE (field))
869 : 3 : continue;
870 : 134200 : if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
871 : : {
872 : : /* Recurse to check the anonymous aggregate member. */
873 : 8 : bad |= cx_check_missing_mem_inits
874 : 4 : (TREE_TYPE (field), NULL_TREE, complain);
875 : 4 : if (bad && !complain)
876 : 69 : return true;
877 : 4 : continue;
878 : : }
879 : 134196 : ftype = TREE_TYPE (field);
880 : 134196 : if (!ftype || !TYPE_P (ftype) || !COMPLETE_TYPE_P (ftype))
881 : : /* A flexible array can't be intialized here, so don't complain
882 : : that it isn't. */
883 : 2 : continue;
884 : 134194 : if (is_empty_field (field))
885 : : /* An empty field doesn't need an initializer. */
886 : 134093 : continue;
887 : 101 : ftype = strip_array_types (ftype);
888 : 101 : if (type_has_constexpr_default_constructor (ftype))
889 : : {
890 : : /* It's OK to skip a member with a trivial constexpr ctor.
891 : : A constexpr ctor that isn't trivial should have been
892 : : added in by now. */
893 : 11 : gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype)
894 : : || errorcount != 0);
895 : 11 : continue;
896 : : }
897 : 90 : if (!complain)
898 : : return true;
899 : 21 : auto_diagnostic_group d;
900 : 21 : error ("member %qD must be initialized by mem-initializer "
901 : : "in %<constexpr%> constructor", field);
902 : 21 : inform (DECL_SOURCE_LOCATION (field), "declared here");
903 : 21 : bad = true;
904 : 21 : }
905 : 1628155 : if (field == NULL_TREE)
906 : : break;
907 : :
908 : 842704 : if (ANON_AGGR_TYPE_P (TREE_TYPE (index)))
909 : : {
910 : : /* Check the anonymous aggregate initializer is valid. */
911 : 448 : bad |= cx_check_missing_mem_inits
912 : 224 : (TREE_TYPE (index), CONSTRUCTOR_ELT (body, i)->value, complain);
913 : 224 : if (bad && !complain)
914 : : return true;
915 : : }
916 : 842704 : field = DECL_CHAIN (field);
917 : : }
918 : :
919 : : return bad;
920 : : }
921 : :
922 : : /* We are processing the definition of the constexpr function FUN.
923 : : Check that its body fulfills the apropriate requirements and
924 : : enter it in the constexpr function definition table. */
925 : :
926 : : void
927 : 142532307 : maybe_save_constexpr_fundef (tree fun)
928 : : {
929 : 142532307 : if (processing_template_decl
930 : 56637209 : || cp_function_chain->invalid_constexpr
931 : 199057579 : || (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun)))
932 : 124169311 : return;
933 : :
934 : : /* With -fimplicit-constexpr, try to make inlines constexpr. We'll
935 : : actually set DECL_DECLARED_CONSTEXPR_P below if the checks pass. */
936 : 42896478 : bool implicit = false;
937 : 42896478 : if (flag_implicit_constexpr)
938 : : {
939 : 13 : if (DECL_DELETING_DESTRUCTOR_P (fun)
940 : 13 : && decl_implicit_constexpr_p (DECL_CLONED_FUNCTION (fun)))
941 : : /* Don't inherit implicit constexpr from the non-deleting
942 : : destructor. */
943 : 0 : DECL_DECLARED_CONSTEXPR_P (fun) = false;
944 : :
945 : 13 : if (!DECL_DECLARED_CONSTEXPR_P (fun)
946 : 12 : && DECL_DECLARED_INLINE_P (fun)
947 : 21 : && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fun)))
948 : : implicit = true;
949 : : }
950 : :
951 : 42896478 : if (!DECL_DECLARED_CONSTEXPR_P (fun) && !implicit)
952 : : return;
953 : :
954 : 18418226 : bool complain = !DECL_GENERATED_P (fun) && !implicit;
955 : :
956 : 18418226 : if (!is_valid_constexpr_fn (fun, complain))
957 : : return;
958 : :
959 : 18363211 : tree massaged = massage_constexpr_body (fun, DECL_SAVED_TREE (fun));
960 : 18363211 : if (massaged == NULL_TREE || massaged == error_mark_node)
961 : : {
962 : 66 : if (!DECL_CONSTRUCTOR_P (fun) && complain)
963 : 22 : error ("body of %<constexpr%> function %qD not a return-statement",
964 : : fun);
965 : 33 : return;
966 : : }
967 : :
968 : 18363178 : bool potential = potential_rvalue_constant_expression (massaged);
969 : 18363178 : if (!potential && complain)
970 : 178 : require_potential_rvalue_constant_expression_fncheck (massaged);
971 : :
972 : 20460802 : if (DECL_CONSTRUCTOR_P (fun) && potential
973 : 20457327 : && !DECL_DEFAULTED_FN (fun))
974 : : {
975 : 1752289 : if (cx_check_missing_mem_inits (DECL_CONTEXT (fun),
976 : : massaged, complain))
977 : : potential = false;
978 : 1752201 : else if (cxx_dialect > cxx11)
979 : : {
980 : : /* What we got from massage_constexpr_body is pretty much just the
981 : : ctor-initializer, also check the body. */
982 : 1747503 : massaged = DECL_SAVED_TREE (fun);
983 : 1747503 : potential = potential_rvalue_constant_expression (massaged);
984 : 1747503 : if (!potential && complain)
985 : 16 : require_potential_rvalue_constant_expression_fncheck (massaged);
986 : : }
987 : : }
988 : :
989 : 18363178 : if (!potential && complain
990 : : /* If -Wno-invalid-constexpr was specified, we haven't complained
991 : : about non-constant expressions yet. Register the function and
992 : : complain in explain_invalid_constexpr_fn if the function is
993 : : called. */
994 : 213 : && warn_invalid_constexpr != 0)
995 : : return;
996 : :
997 : 18363004 : if (implicit)
998 : : {
999 : 8 : if (potential)
1000 : : {
1001 : 0 : DECL_DECLARED_CONSTEXPR_P (fun) = true;
1002 : 0 : DECL_LANG_SPECIFIC (fun)->u.fn.implicit_constexpr = true;
1003 : 0 : if (DECL_CONSTRUCTOR_P (fun))
1004 : 0 : TYPE_HAS_CONSTEXPR_CTOR (DECL_CONTEXT (fun)) = true;
1005 : : }
1006 : : else
1007 : : /* Don't bother keeping the pre-generic body of unsuitable functions
1008 : : not explicitly declared constexpr. */
1009 : : return;
1010 : : }
1011 : :
1012 : 18362996 : constexpr_fundef entry = {fun, NULL_TREE, NULL_TREE, NULL_TREE};
1013 : 18362996 : bool clear_ctx = false;
1014 : 18362996 : if (DECL_RESULT (fun) && DECL_CONTEXT (DECL_RESULT (fun)) == NULL_TREE)
1015 : : {
1016 : 18362996 : clear_ctx = true;
1017 : 18362996 : DECL_CONTEXT (DECL_RESULT (fun)) = fun;
1018 : : }
1019 : 18362996 : tree saved_fn = current_function_decl;
1020 : 18362996 : current_function_decl = fun;
1021 : 18362996 : entry.body = copy_fn (entry.decl, entry.parms, entry.result);
1022 : 18362996 : current_function_decl = saved_fn;
1023 : 18362996 : if (clear_ctx)
1024 : 18362996 : DECL_CONTEXT (DECL_RESULT (entry.decl)) = NULL_TREE;
1025 : 18362996 : if (!potential)
1026 : : /* For a template instantiation, we want to remember the pre-generic body
1027 : : for explain_invalid_constexpr_fn, but do tell cxx_eval_call_expression
1028 : : that it doesn't need to bother trying to expand the function. */
1029 : 81962 : entry.result = error_mark_node;
1030 : :
1031 : 18362996 : register_constexpr_fundef (entry);
1032 : : }
1033 : :
1034 : : /* BODY is a validated and massaged definition of a constexpr
1035 : : function. Register it in the hash table. */
1036 : :
1037 : : void
1038 : 18384801 : register_constexpr_fundef (const constexpr_fundef &value)
1039 : : {
1040 : : /* Create the constexpr function table if necessary. */
1041 : 18384801 : if (constexpr_fundef_table == NULL)
1042 : 25604 : constexpr_fundef_table
1043 : 25604 : = hash_table<constexpr_fundef_hasher>::create_ggc (101);
1044 : :
1045 : 18384801 : constexpr_fundef **slot = constexpr_fundef_table->find_slot
1046 : 18384801 : (const_cast<constexpr_fundef *> (&value), INSERT);
1047 : :
1048 : 18384801 : gcc_assert (*slot == NULL);
1049 : 18384801 : *slot = ggc_alloc<constexpr_fundef> ();
1050 : 18384801 : **slot = value;
1051 : 18384801 : }
1052 : :
1053 : : /* FUN is a non-constexpr (or, with -Wno-invalid-constexpr, a constexpr
1054 : : function called in a context that requires a constant expression).
1055 : : If it comes from a constexpr template, explain why the instantiation
1056 : : isn't constexpr. Otherwise, explain why the function cannot be used
1057 : : in a constexpr context. */
1058 : :
1059 : : void
1060 : 773 : explain_invalid_constexpr_fn (tree fun)
1061 : : {
1062 : 773 : static hash_set<tree> *diagnosed;
1063 : 773 : tree body;
1064 : :
1065 : : /* Don't try to explain a function we already complained about. */
1066 : 773 : if (function *f = DECL_STRUCT_FUNCTION (fun))
1067 : 580 : if (f->language->erroneous)
1068 : 773 : return;
1069 : :
1070 : : /* In C++23, a function marked 'constexpr' may not actually be a constant
1071 : : expression. We haven't diagnosed the problem yet: -Winvalid-constexpr
1072 : : wasn't enabled. The function was called, so diagnose why it cannot be
1073 : : used in a constant expression. */
1074 : 724 : if (warn_invalid_constexpr == 0 && DECL_DECLARED_CONSTEXPR_P (fun))
1075 : : /* Go on. */;
1076 : : /* Only diagnose defaulted functions, lambdas, or instantiations. */
1077 : 699 : else if (!DECL_DEFAULTED_FN (fun)
1078 : 919 : && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun))
1079 : 672 : && !(flag_implicit_constexpr
1080 : 6 : && !DECL_DECLARED_CONSTEXPR_P (fun)
1081 : 6 : && DECL_DECLARED_INLINE_P (fun))
1082 : 1368 : && !is_instantiation_of_constexpr (fun))
1083 : : {
1084 : 655 : inform (DECL_SOURCE_LOCATION (fun), "%qD declared here", fun);
1085 : 3 : if (flag_implicit_constexpr && !maybe_constexpr_fn (fun)
1086 : 658 : && decl_defined_p (fun))
1087 : 3 : inform (DECL_SOURCE_LOCATION (fun),
1088 : : "%<-fimplicit-constexpr%> only affects %<inline%> functions");
1089 : 655 : return;
1090 : : }
1091 : 69 : if (diagnosed == NULL)
1092 : 55 : diagnosed = new hash_set<tree>;
1093 : 69 : if (diagnosed->add (fun))
1094 : : /* Already explained. */
1095 : : return;
1096 : :
1097 : 68 : iloc_sentinel ils = input_location;
1098 : 68 : if (!lambda_static_thunk_p (fun))
1099 : : {
1100 : : /* Diagnostics should completely ignore the static thunk, so leave
1101 : : input_location set to our caller's location. */
1102 : 66 : input_location = DECL_SOURCE_LOCATION (fun);
1103 : 66 : inform (input_location,
1104 : : "%qD is not usable as a %<constexpr%> function because:", fun);
1105 : : }
1106 : : /* First check the declaration. */
1107 : 68 : if (is_valid_constexpr_fn (fun, true))
1108 : : {
1109 : : /* Then if it's OK, the body. */
1110 : 62 : if (!DECL_DECLARED_CONSTEXPR_P (fun)
1111 : 62 : && DECL_DEFAULTED_FN (fun))
1112 : 10 : explain_implicit_non_constexpr (fun);
1113 : : else
1114 : : {
1115 : 52 : if (constexpr_fundef *fd = retrieve_constexpr_fundef (fun))
1116 : 40 : body = fd->body;
1117 : : else
1118 : 12 : body = DECL_SAVED_TREE (fun);
1119 : 52 : tree massaged = massage_constexpr_body (fun, body);
1120 : 52 : require_potential_rvalue_constant_expression (massaged);
1121 : 104 : if (DECL_CONSTRUCTOR_P (fun))
1122 : : {
1123 : 11 : cx_check_missing_mem_inits (DECL_CONTEXT (fun), massaged, true);
1124 : 11 : if (cxx_dialect > cxx11)
1125 : : /* Also check the body, not just the ctor-initializer. */
1126 : 9 : require_potential_rvalue_constant_expression (body);
1127 : : }
1128 : : }
1129 : : }
1130 : 68 : }
1131 : :
1132 : : /* Objects of this type represent calls to constexpr functions
1133 : : along with the bindings of parameters to their arguments, for
1134 : : the purpose of compile time evaluation. */
1135 : :
1136 : : struct GTY((for_user)) constexpr_call {
1137 : : /* Description of the constexpr function definition. */
1138 : : constexpr_fundef *fundef = nullptr;
1139 : : /* Parameter bindings environment. A TREE_VEC of arguments. */
1140 : : tree bindings = NULL_TREE;
1141 : : /* Result of the call, indexed by the value of
1142 : : constexpr_ctx::manifestly_const_eval.
1143 : : unknown_type_node means the call is being evaluated.
1144 : : error_mark_node means that the evaluation was erroneous or otherwise
1145 : : uncacheable (e.g. because it depends on the caller).
1146 : : Otherwise, the actual value of the call. */
1147 : : tree results[3] = { NULL_TREE, NULL_TREE, NULL_TREE };
1148 : : /* The hash of this call; we remember it here to avoid having to
1149 : : recalculate it when expanding the hash table. */
1150 : : hashval_t hash = 0;
1151 : :
1152 : : /* The result slot corresponding to the given mce_value. */
1153 : 39047799 : tree& result (mce_value mce) { return results[1 + int(mce)]; }
1154 : : };
1155 : :
1156 : : struct constexpr_call_hasher : ggc_ptr_hash<constexpr_call>
1157 : : {
1158 : : static hashval_t hash (constexpr_call *);
1159 : : static bool equal (constexpr_call *, constexpr_call *);
1160 : : };
1161 : :
1162 : : enum constexpr_switch_state {
1163 : : /* Used when processing a switch for the first time by cxx_eval_switch_expr
1164 : : and default: label for that switch has not been seen yet. */
1165 : : css_default_not_seen,
1166 : : /* Used when processing a switch for the first time by cxx_eval_switch_expr
1167 : : and default: label for that switch has been seen already. */
1168 : : css_default_seen,
1169 : : /* Used when processing a switch for the second time by
1170 : : cxx_eval_switch_expr, where default: label should match. */
1171 : : css_default_processing
1172 : : };
1173 : :
1174 : : /* The constexpr expansion context part which needs one instance per
1175 : : cxx_eval_outermost_constant_expr invocation. VALUES is a map of values of
1176 : : variables initialized within the expression. */
1177 : :
1178 : : class constexpr_global_ctx {
1179 : : /* Values for any temporaries or local variables within the
1180 : : constant-expression. Objects outside their lifetime have
1181 : : value 'void_node'. */
1182 : : hash_map<tree,tree> values;
1183 : : public:
1184 : : /* Number of cxx_eval_constant_expression calls (except skipped ones,
1185 : : on simple constants or location wrappers) encountered during current
1186 : : cxx_eval_outermost_constant_expr call. */
1187 : : HOST_WIDE_INT constexpr_ops_count;
1188 : : /* Heap VAR_DECLs created during the evaluation of the outermost constant
1189 : : expression. */
1190 : : auto_vec<tree, 16> heap_vars;
1191 : : /* Vector of caught exceptions, including exceptions still not active at
1192 : : the start of a handler (those are immediately followed up by HANDLER_TYPE
1193 : : until __cxa_begin_catch finishes). */
1194 : : auto_vec<tree, 2> caught_exceptions;
1195 : : /* Cleanups that need to be evaluated at the end of CLEANUP_POINT_EXPR. */
1196 : : vec<tree> *cleanups;
1197 : : /* If non-null, only allow modification of existing values of the variables
1198 : : in this set. Set by modifiable_tracker, below. */
1199 : : hash_set<tree> *modifiable;
1200 : : /* Number of heap VAR_DECL deallocations. */
1201 : : unsigned heap_dealloc_count;
1202 : : /* Number of uncaught exceptions. */
1203 : : unsigned uncaught_exceptions;
1204 : :
1205 : : /* Constructor. */
1206 : 380948465 : constexpr_global_ctx ()
1207 : 761896930 : : constexpr_ops_count (0), cleanups (NULL), modifiable (nullptr),
1208 : 380948465 : heap_dealloc_count (0), uncaught_exceptions (0) {}
1209 : :
1210 : 206057809 : bool is_outside_lifetime (tree t)
1211 : : {
1212 : 206057809 : if (tree *p = values.get (t))
1213 : 50345752 : if (*p == void_node)
1214 : 178 : return true;
1215 : : return false;
1216 : : }
1217 : 244656687 : tree get_value (tree t)
1218 : : {
1219 : 244656687 : if (tree *p = values.get (t))
1220 : 79273714 : if (*p != void_node)
1221 : 78064182 : return *p;
1222 : : return NULL_TREE;
1223 : : }
1224 : 34139048 : tree *get_value_ptr (tree t, bool initializing)
1225 : : {
1226 : 34139048 : if (modifiable && !modifiable->contains (t))
1227 : : return nullptr;
1228 : 34139037 : if (tree *p = values.get (t))
1229 : : {
1230 : 34134827 : if (*p != void_node)
1231 : : return p;
1232 : 36 : else if (initializing)
1233 : : {
1234 : 6 : *p = NULL_TREE;
1235 : 6 : return p;
1236 : : }
1237 : : }
1238 : : return nullptr;
1239 : : }
1240 : 97225224 : void put_value (tree t, tree v)
1241 : : {
1242 : 97225224 : bool already_in_map = values.put (t, v);
1243 : 97225224 : if (!already_in_map && modifiable)
1244 : 32 : modifiable->add (t);
1245 : 97225224 : }
1246 : 71122599 : void destroy_value (tree t)
1247 : : {
1248 : 71122599 : if (TREE_CODE (t) == VAR_DECL
1249 : 71122599 : || TREE_CODE (t) == PARM_DECL
1250 : : || TREE_CODE (t) == RESULT_DECL)
1251 : 71116391 : values.put (t, void_node);
1252 : : else
1253 : 6208 : values.remove (t);
1254 : 71122599 : }
1255 : 3925698 : void clear_value (tree t)
1256 : : {
1257 : 7851396 : values.remove (t);
1258 : : }
1259 : : };
1260 : :
1261 : : /* Helper class for constexpr_global_ctx. In some cases we want to avoid
1262 : : side-effects from evaluation of a particular subexpression of a
1263 : : constant-expression. In such cases we use modifiable_tracker to prevent
1264 : : modification of variables created outside of that subexpression.
1265 : :
1266 : : ??? We could change the hash_set to a hash_map, allow and track external
1267 : : modifications, and roll them back in the destructor. It's not clear to me
1268 : : that this would be worthwhile. */
1269 : :
1270 : : class modifiable_tracker
1271 : : {
1272 : : hash_set<tree> set;
1273 : : constexpr_global_ctx *global;
1274 : : public:
1275 : 283590 : modifiable_tracker (constexpr_global_ctx *g): global(g)
1276 : : {
1277 : 283590 : global->modifiable = &set;
1278 : 283590 : }
1279 : 283590 : ~modifiable_tracker ()
1280 : : {
1281 : 283622 : for (tree t: set)
1282 : 32 : global->clear_value (t);
1283 : 283590 : global->modifiable = nullptr;
1284 : 283590 : }
1285 : : };
1286 : :
1287 : : /* The constexpr expansion context. CALL is the current function
1288 : : expansion, CTOR is the current aggregate initializer, OBJECT is the
1289 : : object being initialized by CTOR, either a VAR_DECL or a _REF. */
1290 : :
1291 : : struct constexpr_ctx {
1292 : : /* The part of the context that needs to be unique to the whole
1293 : : cxx_eval_outermost_constant_expr invocation. */
1294 : : constexpr_global_ctx *global;
1295 : : /* The innermost call we're evaluating. */
1296 : : constexpr_call *call;
1297 : : /* SAVE_EXPRs and TARGET_EXPR_SLOT vars of TARGET_EXPRs that we've seen
1298 : : within the current LOOP_EXPR. NULL if we aren't inside a loop. */
1299 : : vec<tree> *save_exprs;
1300 : : /* The CONSTRUCTOR we're currently building up for an aggregate
1301 : : initializer. */
1302 : : tree ctor;
1303 : : /* The object we're building the CONSTRUCTOR for. */
1304 : : tree object;
1305 : : /* If inside SWITCH_EXPR. */
1306 : : constexpr_switch_state *css_state;
1307 : : /* The aggregate initialization context inside which this one is nested. This
1308 : : is used by lookup_placeholder to resolve PLACEHOLDER_EXPRs. */
1309 : : const constexpr_ctx *parent;
1310 : :
1311 : : /* Whether we should error on a non-constant expression or fail quietly.
1312 : : This flag needs to be here, but some of the others could move to global
1313 : : if they get larger than a word. */
1314 : : bool quiet;
1315 : : /* Whether we are strictly conforming to constant expression rules or
1316 : : trying harder to get a constant value. */
1317 : : bool strict;
1318 : : /* Whether __builtin_is_constant_evaluated () should be true. */
1319 : : mce_value manifestly_const_eval;
1320 : : };
1321 : :
1322 : : /* Predicates for the meaning of *jump_target. */
1323 : :
1324 : : static bool
1325 : 33042219 : returns (tree *jump_target)
1326 : : {
1327 : 5928326 : return *jump_target && TREE_CODE (*jump_target) == RETURN_EXPR;
1328 : : }
1329 : :
1330 : : static bool
1331 : 33306890 : breaks (tree *jump_target)
1332 : : {
1333 : 33306890 : return (*jump_target
1334 : 33306890 : && ((TREE_CODE (*jump_target) == LABEL_DECL
1335 : 52 : && LABEL_DECL_BREAK (*jump_target))
1336 : 1107004 : || TREE_CODE (*jump_target) == BREAK_STMT
1337 : 1060726 : || TREE_CODE (*jump_target) == EXIT_EXPR));
1338 : : }
1339 : :
1340 : : static bool
1341 : 49547940 : continues (tree *jump_target)
1342 : : {
1343 : 49547940 : return (*jump_target
1344 : 49547940 : && ((TREE_CODE (*jump_target) == LABEL_DECL
1345 : 88 : && LABEL_DECL_CONTINUE (*jump_target))
1346 : 1085499 : || TREE_CODE (*jump_target) == CONTINUE_STMT));
1347 : : }
1348 : :
1349 : : static bool
1350 : 6724360 : switches (tree *jump_target)
1351 : : {
1352 : 51524 : return *jump_target && TREE_CODE (*jump_target) == INTEGER_CST;
1353 : : }
1354 : :
1355 : : static bool
1356 : 1052377456 : throws (tree *jump_target)
1357 : : {
1358 : : /* void_node is for use in potential_constant_expression_1, otherwise
1359 : : it should an artificial VAR_DECL created by constant evaluation
1360 : : of __cxa_allocate_exception (). */
1361 : 58346554 : return (*jump_target && (VAR_P (*jump_target) || *jump_target == void_node));
1362 : : }
1363 : :
1364 : : /* True if the constexpr relaxations afforded by P2280R4 for unknown
1365 : : references and objects are in effect. */
1366 : :
1367 : : static bool
1368 : 158853543 : p2280_active_p (const constexpr_ctx *ctx)
1369 : : {
1370 : 27860033 : if (ctx->manifestly_const_eval != mce_true)
1371 : : /* Disable these relaxations during speculative constexpr folding,
1372 : : as it can significantly increase compile time/memory use
1373 : : (PR119387). */
1374 : : return false;
1375 : :
1376 : : /* P2280R4 was accepted as a DR against C++11. */
1377 : 0 : return cxx_dialect >= cxx11;
1378 : : }
1379 : :
1380 : : /* Remove T from the global values map, checking for attempts to destroy
1381 : : a value that has already finished its lifetime. */
1382 : :
1383 : : static void
1384 : 71089956 : destroy_value_checked (const constexpr_ctx* ctx, tree t, bool *non_constant_p)
1385 : : {
1386 : 71089956 : if (t == error_mark_node || TREE_TYPE (t) == error_mark_node)
1387 : : return;
1388 : :
1389 : : /* Don't error again here if we've already reported a problem. */
1390 : 71089947 : if (!*non_constant_p
1391 : 50574814 : && DECL_P (t)
1392 : : /* Non-trivial destructors have their lifetimes ended explicitly
1393 : : with a clobber, so don't worry about it here. */
1394 : 50568785 : && (!TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (t))
1395 : : /* ...except parameters are remapped in cxx_eval_call_expression,
1396 : : and the destructor call during cleanup won't be able to tell that
1397 : : this value has already been destroyed, so complain now. This is
1398 : : not quite unobservable, but is extremely unlikely to crop up in
1399 : : practice; see g++.dg/cpp2a/constexpr-lifetime2.C. */
1400 : 198860 : || TREE_CODE (t) == PARM_DECL)
1401 : 121460170 : && ctx->global->is_outside_lifetime (t))
1402 : : {
1403 : 54 : if (!ctx->quiet)
1404 : : {
1405 : 18 : auto_diagnostic_group d;
1406 : 18 : error ("destroying %qE outside its lifetime", t);
1407 : 18 : inform (DECL_SOURCE_LOCATION (t), "declared here");
1408 : 18 : }
1409 : 54 : *non_constant_p = true;
1410 : : }
1411 : 71089947 : ctx->global->destroy_value (t);
1412 : : }
1413 : :
1414 : : /* This internal flag controls whether we should avoid doing anything during
1415 : : constexpr evaluation that would cause extra DECL_UID generation, such as
1416 : : template instantiation and function body copying. */
1417 : :
1418 : : static bool uid_sensitive_constexpr_evaluation_value;
1419 : :
1420 : : /* An internal counter that keeps track of the number of times
1421 : : uid_sensitive_constexpr_evaluation_p returned true. */
1422 : :
1423 : : static unsigned uid_sensitive_constexpr_evaluation_true_counter;
1424 : :
1425 : : /* The accessor for uid_sensitive_constexpr_evaluation_value which also
1426 : : increments the corresponding counter. */
1427 : :
1428 : : static bool
1429 : 4884669 : uid_sensitive_constexpr_evaluation_p ()
1430 : : {
1431 : 4880327 : if (uid_sensitive_constexpr_evaluation_value)
1432 : : {
1433 : 174844 : ++uid_sensitive_constexpr_evaluation_true_counter;
1434 : 174844 : return true;
1435 : : }
1436 : : else
1437 : : return false;
1438 : : }
1439 : :
1440 : : /* The default constructor for uid_sensitive_constexpr_evaluation_sentinel
1441 : : enables the internal flag for uid_sensitive_constexpr_evaluation_p
1442 : : during the lifetime of the sentinel object. Upon its destruction, the
1443 : : previous value of uid_sensitive_constexpr_evaluation_p is restored. */
1444 : :
1445 : 66191833 : uid_sensitive_constexpr_evaluation_sentinel
1446 : 66191833 : ::uid_sensitive_constexpr_evaluation_sentinel ()
1447 : 66191833 : : ovr (uid_sensitive_constexpr_evaluation_value, true)
1448 : : {
1449 : 66191833 : }
1450 : :
1451 : : /* The default constructor for uid_sensitive_constexpr_evaluation_checker
1452 : : records the current number of times that uid_sensitive_constexpr_evaluation_p
1453 : : has been called and returned true. */
1454 : :
1455 : 1860818249 : uid_sensitive_constexpr_evaluation_checker
1456 : 1860818249 : ::uid_sensitive_constexpr_evaluation_checker ()
1457 : 1860818249 : : saved_counter (uid_sensitive_constexpr_evaluation_true_counter)
1458 : : {
1459 : 1860818249 : }
1460 : :
1461 : : /* Returns true iff uid_sensitive_constexpr_evaluation_p is true, and
1462 : : some constexpr evaluation was restricted due to u_s_c_e_p being called
1463 : : and returning true during the lifetime of this checker object. */
1464 : :
1465 : : bool
1466 : 1312268121 : uid_sensitive_constexpr_evaluation_checker::evaluation_restricted_p () const
1467 : : {
1468 : 1312268121 : return (uid_sensitive_constexpr_evaluation_value
1469 : 1312268121 : && saved_counter != uid_sensitive_constexpr_evaluation_true_counter);
1470 : : }
1471 : :
1472 : :
1473 : : /* A table of all constexpr calls that have been evaluated by the
1474 : : compiler in this translation unit. */
1475 : :
1476 : : static GTY (()) hash_table<constexpr_call_hasher> *constexpr_call_table;
1477 : :
1478 : : /* Compute a hash value for a constexpr call representation. */
1479 : :
1480 : : inline hashval_t
1481 : 117544903 : constexpr_call_hasher::hash (constexpr_call *info)
1482 : : {
1483 : 117544903 : return info->hash;
1484 : : }
1485 : :
1486 : : /* Return true if the objects pointed to by P and Q represent calls
1487 : : to the same constexpr function with the same arguments.
1488 : : Otherwise, return false. */
1489 : :
1490 : : bool
1491 : 124038258 : constexpr_call_hasher::equal (constexpr_call *lhs, constexpr_call *rhs)
1492 : : {
1493 : 124038258 : if (lhs == rhs)
1494 : : return true;
1495 : 124038258 : if (lhs->hash != rhs->hash)
1496 : : return false;
1497 : 16488259 : if (!constexpr_fundef_hasher::equal (lhs->fundef, rhs->fundef))
1498 : : return false;
1499 : 16488259 : return cp_tree_equal (lhs->bindings, rhs->bindings);
1500 : : }
1501 : :
1502 : : /* Initialize the constexpr call table, if needed. */
1503 : :
1504 : : static void
1505 : 19744135 : maybe_initialize_constexpr_call_table (void)
1506 : : {
1507 : 19744135 : if (constexpr_call_table == NULL)
1508 : 17137 : constexpr_call_table = hash_table<constexpr_call_hasher>::create_ggc (101);
1509 : 19744135 : }
1510 : :
1511 : : /* During constexpr CALL_EXPR evaluation, to avoid issues with sharing when
1512 : : a function happens to get called recursively, we unshare the callee
1513 : : function's body and evaluate this unshared copy instead of evaluating the
1514 : : original body.
1515 : :
1516 : : FUNDEF_COPIES_TABLE is a per-function freelist of these unshared function
1517 : : copies. The underlying data structure of FUNDEF_COPIES_TABLE is a hash_map
1518 : : that's keyed off of the original FUNCTION_DECL and whose value is a
1519 : : TREE_LIST of this function's unused copies awaiting reuse.
1520 : :
1521 : : This is not GC-deletable to avoid GC affecting UID generation. */
1522 : :
1523 : : static GTY(()) decl_tree_map *fundef_copies_table;
1524 : :
1525 : : /* Reuse a copy or create a new unshared copy of the function FUN.
1526 : : Return this copy. We use a TREE_LIST whose PURPOSE is body, VALUE
1527 : : is parms, TYPE is result. */
1528 : :
1529 : : static tree
1530 : 29369887 : get_fundef_copy (constexpr_fundef *fundef)
1531 : : {
1532 : 29369887 : tree copy;
1533 : 29369887 : bool existed;
1534 : 29369887 : tree *slot = &(hash_map_safe_get_or_insert<hm_ggc>
1535 : 29369887 : (fundef_copies_table, fundef->decl, &existed, 127));
1536 : :
1537 : 29369887 : if (!existed)
1538 : : {
1539 : : /* There is no cached function available, or in use. We can use
1540 : : the function directly. That the slot is now created records
1541 : : that this function is now in use. */
1542 : 4491165 : copy = build_tree_list (fundef->body, fundef->parms);
1543 : 4491165 : TREE_TYPE (copy) = fundef->result;
1544 : : }
1545 : 24878722 : else if (*slot == NULL_TREE)
1546 : : {
1547 : 4342 : if (uid_sensitive_constexpr_evaluation_p ())
1548 : 0 : return NULL_TREE;
1549 : :
1550 : : /* We've already used the function itself, so make a copy. */
1551 : 4342 : copy = build_tree_list (NULL, NULL);
1552 : 4342 : tree saved_body = DECL_SAVED_TREE (fundef->decl);
1553 : 4342 : tree saved_parms = DECL_ARGUMENTS (fundef->decl);
1554 : 4342 : tree saved_result = DECL_RESULT (fundef->decl);
1555 : 4342 : tree saved_fn = current_function_decl;
1556 : 4342 : DECL_SAVED_TREE (fundef->decl) = fundef->body;
1557 : 4342 : DECL_ARGUMENTS (fundef->decl) = fundef->parms;
1558 : 4342 : DECL_RESULT (fundef->decl) = fundef->result;
1559 : 4342 : current_function_decl = fundef->decl;
1560 : 4342 : TREE_PURPOSE (copy) = copy_fn (fundef->decl, TREE_VALUE (copy),
1561 : 4342 : TREE_TYPE (copy));
1562 : 4342 : current_function_decl = saved_fn;
1563 : 4342 : DECL_RESULT (fundef->decl) = saved_result;
1564 : 4342 : DECL_ARGUMENTS (fundef->decl) = saved_parms;
1565 : 4342 : DECL_SAVED_TREE (fundef->decl) = saved_body;
1566 : : }
1567 : : else
1568 : : {
1569 : : /* We have a cached function available. */
1570 : 24874380 : copy = *slot;
1571 : 24874380 : *slot = TREE_CHAIN (copy);
1572 : : }
1573 : :
1574 : : return copy;
1575 : : }
1576 : :
1577 : : /* Save the copy COPY of function FUN for later reuse by
1578 : : get_fundef_copy(). By construction, there will always be an entry
1579 : : to find. */
1580 : :
1581 : : static void
1582 : 29369885 : save_fundef_copy (tree fun, tree copy)
1583 : : {
1584 : 29369885 : tree *slot = fundef_copies_table->get (fun);
1585 : 29369885 : TREE_CHAIN (copy) = *slot;
1586 : 29369885 : *slot = copy;
1587 : 29369885 : }
1588 : :
1589 : : /* Whether our evaluation wants a prvalue (e.g. CONSTRUCTOR or _CST),
1590 : : a glvalue (e.g. VAR_DECL or _REF), or nothing. */
1591 : :
1592 : : enum value_cat {
1593 : : vc_prvalue = 0,
1594 : : vc_glvalue = 1,
1595 : : vc_discard = 2
1596 : : };
1597 : :
1598 : : static tree cxx_eval_constant_expression (const constexpr_ctx *, tree,
1599 : : value_cat, bool *, bool *, tree *);
1600 : : static tree cxx_eval_bare_aggregate (const constexpr_ctx *, tree,
1601 : : value_cat, bool *, bool *, tree *);
1602 : : static tree cxx_fold_indirect_ref (const constexpr_ctx *, location_t, tree, tree,
1603 : : bool *, tree *);
1604 : : static tree find_heap_var_refs (tree *, int *, void *);
1605 : :
1606 : : /* For exception object EXC if it has class type and usable what () method
1607 : : which returns cv char * return the xmalloced string literal which it returns
1608 : : if possible, otherwise return NULL. */
1609 : :
1610 : : static char *
1611 : 87 : exception_what_str (const constexpr_ctx *ctx, tree exc)
1612 : : {
1613 : 87 : tree type = strip_array_types (TREE_TYPE (exc));
1614 : 87 : if (!CLASS_TYPE_P (type))
1615 : : return NULL;
1616 : 49 : tree std_exception = lookup_qualified_name (std_node, "exception",
1617 : : LOOK_want::NORMAL, false);
1618 : 49 : if (TREE_CODE (std_exception) != TYPE_DECL)
1619 : : return NULL;
1620 : 46 : if (!CLASS_TYPE_P (TREE_TYPE (std_exception)))
1621 : : return NULL;
1622 : 46 : base_kind b_kind;
1623 : 46 : tree binfo = lookup_base (type, TREE_TYPE (std_exception), ba_check, &b_kind,
1624 : : tf_none);
1625 : 46 : if (binfo == NULL_TREE || binfo == error_mark_node)
1626 : : return NULL;
1627 : 42 : if (type != TREE_TYPE (exc))
1628 : 4 : exc = build4 (ARRAY_REF, type, exc, size_zero_node, NULL, NULL);
1629 : 42 : tree call
1630 : 42 : = finish_class_member_access_expr (exc, get_identifier ("what"), false,
1631 : : tf_none);
1632 : 42 : if (call == error_mark_node)
1633 : : return NULL;
1634 : 42 : releasing_vec what_args;
1635 : 42 : call = finish_call_expr (call, &what_args, false, false, tf_none);
1636 : 42 : if (call == error_mark_node)
1637 : : return NULL;
1638 : 42 : if (TREE_CODE (TREE_TYPE (call)) != POINTER_TYPE
1639 : 42 : || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (call)))
1640 : 42 : || !COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (call)))
1641 : 42 : || !tree_int_cst_equal (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (call))),
1642 : 42 : TYPE_SIZE_UNIT (char_type_node))
1643 : 84 : || TYPE_PRECISION (TREE_TYPE (TREE_TYPE (call))) != BITS_PER_UNIT)
1644 : 0 : return NULL;
1645 : 42 : if (!potential_constant_expression (call))
1646 : : return NULL;
1647 : 42 : bool non_constant_p = false, overflow_p = false;
1648 : 42 : tree jmp_target = NULL;
1649 : 42 : tree ptr = cxx_eval_constant_expression (ctx, call, vc_prvalue,
1650 : : &non_constant_p, &overflow_p,
1651 : : &jmp_target);
1652 : 42 : if (throws (&jmp_target) || non_constant_p)
1653 : : return NULL;
1654 : 42 : if (reduced_constant_expression_p (ptr))
1655 : 40 : if (const char *msg = c_getstr (ptr))
1656 : 40 : return xstrdup (msg);
1657 : 2 : auto_vec <char, 32> v;
1658 : 26 : for (unsigned i = 0; i < INT_MAX; ++i)
1659 : : {
1660 : 26 : tree t = call;
1661 : 26 : if (i)
1662 : 24 : t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (ptr), ptr, size_int (i));
1663 : 26 : t = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (t)), t);
1664 : 26 : non_constant_p = false;
1665 : 26 : overflow_p = false;
1666 : 26 : jmp_target = NULL;
1667 : 26 : tree t2 = cxx_eval_constant_expression (ctx, t, vc_prvalue,
1668 : : &non_constant_p, &overflow_p,
1669 : : &jmp_target);
1670 : 26 : if (throws (&jmp_target)
1671 : 26 : || non_constant_p
1672 : 26 : || !tree_fits_shwi_p (t2))
1673 : 0 : return NULL;
1674 : 26 : char c = tree_to_shwi (t2);
1675 : 26 : v.safe_push (c);
1676 : 26 : if (c == '\0')
1677 : : break;
1678 : : }
1679 : 4 : return xstrdup (v.address ());
1680 : 44 : }
1681 : :
1682 : : /* Diagnose constant expression evaluation encountering call to
1683 : : std::terminate due to exception EXC. */
1684 : :
1685 : : static void
1686 : 11 : diagnose_std_terminate (location_t loc, const constexpr_ctx *ctx, tree exc)
1687 : : {
1688 : 11 : tree type = strip_array_types (TREE_TYPE (exc));
1689 : 11 : if (char *str = exception_what_str (ctx, exc))
1690 : : {
1691 : 2 : error_at (loc, "%qs called after throwing an exception of type %qT; "
1692 : : "%<what()%>: %qs", "std::terminate", type, str);
1693 : 2 : free (str);
1694 : : }
1695 : : else
1696 : : {
1697 : 9 : if (type != TREE_TYPE (exc))
1698 : 9 : exc = build4 (ARRAY_REF, type, exc, size_zero_node, NULL, NULL);
1699 : 9 : bool non_constant_p = false, overflow_p = false;
1700 : 9 : tree jmp_target = NULL;
1701 : 9 : tree val = cxx_eval_constant_expression (ctx, exc, vc_prvalue,
1702 : : &non_constant_p, &overflow_p,
1703 : : &jmp_target);
1704 : 9 : gcc_assert (!throws (&jmp_target) && !non_constant_p);
1705 : 9 : if (reduced_constant_expression_p (val))
1706 : 9 : error_at (loc, "%qs called after throwing an exception %qE",
1707 : : "std::terminate", val);
1708 : : else
1709 : 0 : error_at (loc, "%qs called after throwing an exception of type %qT",
1710 : : "std::terminate", type);
1711 : : }
1712 : 11 : }
1713 : :
1714 : : /* Diagnose constant expression evaluation encountering call to
1715 : : uncaught exception EXC. */
1716 : :
1717 : : static void
1718 : 76 : diagnose_uncaught_exception (location_t loc, const constexpr_ctx *ctx, tree exc)
1719 : : {
1720 : 76 : tree type = strip_array_types (TREE_TYPE (exc));
1721 : 76 : if (char *str = exception_what_str (ctx, exc))
1722 : : {
1723 : 40 : error_at (loc, "uncaught exception of type %qT; %<what()%>: %qs", type, str);
1724 : 40 : free (str);
1725 : : }
1726 : : else
1727 : : {
1728 : 36 : if (type != TREE_TYPE (exc))
1729 : 36 : exc = build4 (ARRAY_REF, type, exc, size_zero_node, NULL, NULL);
1730 : 36 : bool non_constant_p = false, overflow_p = false;
1731 : 36 : tree jmp_target = NULL;
1732 : 36 : tree val = cxx_eval_constant_expression (ctx, exc, vc_prvalue,
1733 : : &non_constant_p, &overflow_p,
1734 : : &jmp_target);
1735 : 36 : gcc_assert (!throws (&jmp_target) && !non_constant_p);
1736 : 36 : if (reduced_constant_expression_p (val))
1737 : 35 : error_at (loc, "uncaught exception %qE", val);
1738 : : else
1739 : 1 : error_at (loc, "uncaught exception of type %qT", type);
1740 : : }
1741 : 76 : }
1742 : :
1743 : : /* Kinds of __cxa_* functions (and a few other EH related ones) we handle as
1744 : : magic constexpr functions for C++26. */
1745 : :
1746 : : enum cxa_builtin {
1747 : : CXA_NONE = 0,
1748 : : CXA_ALLOCATE_EXCEPTION = 1,
1749 : : CXA_FREE_EXCEPTION = 2,
1750 : : CXA_THROW = 3,
1751 : : CXA_BEGIN_CATCH = 4,
1752 : : CXA_END_CATCH = 5,
1753 : : CXA_RETHROW = 6,
1754 : : CXA_GET_EXCEPTION_PTR = 7,
1755 : : CXA_BAD_CAST = 8,
1756 : : CXA_BAD_TYPEID = 9,
1757 : : CXA_THROW_BAD_ARRAY_NEW_LENGTH = 10,
1758 : : STD_UNCAUGHT_EXCEPTIONS = 11,
1759 : : STD_CURRENT_EXCEPTION = 12,
1760 : : STD_RETHROW_EXCEPTION = 13,
1761 : : BUILTIN_EH_PTR_ADJUST_REF = 14
1762 : : };
1763 : :
1764 : : /* Return cxa_builtin if FNDECL is a __cxa_* function handled as
1765 : : magic constexpr function for C++26. Return CXA_NONE otherwise. */
1766 : :
1767 : : static enum cxa_builtin
1768 : 30367412 : cxx_cxa_builtin_fn_p (tree fndecl)
1769 : : {
1770 : 30367412 : if (cxx_dialect < cxx26)
1771 : : return CXA_NONE;
1772 : 5068200 : if (DECL_LANGUAGE (fndecl) != lang_c)
1773 : : {
1774 : 4626544 : if (!decl_in_std_namespace_p (fndecl))
1775 : : return CXA_NONE;
1776 : 1885802 : if (id_equal (DECL_NAME (fndecl), "uncaught_exceptions"))
1777 : : return STD_UNCAUGHT_EXCEPTIONS;
1778 : 1885681 : if (id_equal (DECL_NAME (fndecl), "current_exception"))
1779 : : return STD_CURRENT_EXCEPTION;
1780 : 1867121 : if (id_equal (DECL_NAME (fndecl), "rethrow_exception"))
1781 : : return STD_RETHROW_EXCEPTION;
1782 : : return CXA_NONE;
1783 : : }
1784 : 441656 : if (!startswith (IDENTIFIER_POINTER (DECL_NAME (fndecl)), "__cxa_"))
1785 : : return CXA_NONE;
1786 : 40266 : if (id_equal (DECL_NAME (fndecl), "__cxa_allocate_exception"))
1787 : : return CXA_ALLOCATE_EXCEPTION;
1788 : 6628 : if (id_equal (DECL_NAME (fndecl), "__cxa_free_exception"))
1789 : : return CXA_FREE_EXCEPTION;
1790 : 6627 : if (id_equal (DECL_NAME (fndecl), "__cxa_throw"))
1791 : : return CXA_THROW;
1792 : 6174 : if (id_equal (DECL_NAME (fndecl), "__cxa_begin_catch"))
1793 : : return CXA_BEGIN_CATCH;
1794 : 1101 : if (id_equal (DECL_NAME (fndecl), "__cxa_end_catch"))
1795 : : return CXA_END_CATCH;
1796 : 928 : if (id_equal (DECL_NAME (fndecl), "__cxa_rethrow"))
1797 : : return CXA_RETHROW;
1798 : 872 : if (id_equal (DECL_NAME (fndecl), "__cxa_get_exception_ptr"))
1799 : : return CXA_GET_EXCEPTION_PTR;
1800 : 852 : if (id_equal (DECL_NAME (fndecl), "__cxa_bad_cast"))
1801 : : return CXA_BAD_CAST;
1802 : 568 : if (id_equal (DECL_NAME (fndecl), "__cxa_bad_typeid"))
1803 : : return CXA_BAD_TYPEID;
1804 : 560 : if (id_equal (DECL_NAME (fndecl), "__cxa_throw_bad_array_new_length"))
1805 : : return CXA_THROW_BAD_ARRAY_NEW_LENGTH;
1806 : : return CXA_NONE;
1807 : : }
1808 : :
1809 : : /* Helper function for cxx_eval_cxa_builtin_fn.
1810 : : Check if ARG is a valid first argument of __cxa_throw or
1811 : : __cxa_free_exception or __builtin_eh_ptr_adjust_ref. Return NULL_TREE if
1812 : : not, otherwise return the artificial __cxa_allocate_exception allocated
1813 : : VAR_DECL. FREE_EXC is true for __cxa_free_exception, false otherwise. */
1814 : :
1815 : : static tree
1816 : 408 : cxa_check_throw_arg (tree arg, bool free_exc)
1817 : : {
1818 : 408 : STRIP_NOPS (arg);
1819 : 408 : if (TREE_CODE (arg) != ADDR_EXPR)
1820 : : return NULL_TREE;
1821 : 408 : arg = TREE_OPERAND (arg, 0);
1822 : 408 : if (!VAR_P (arg)
1823 : 408 : || !DECL_ARTIFICIAL (arg)
1824 : 408 : || ((!free_exc || DECL_NAME (arg) != heap_uninit_identifier)
1825 : 408 : && DECL_NAME (arg) != heap_identifier)
1826 : 816 : || !DECL_LANG_SPECIFIC (arg))
1827 : 0 : return NULL_TREE;
1828 : : return arg;
1829 : : }
1830 : :
1831 : : /* Helper function for cxx_eval_cxa_builtin_fn.
1832 : : "Allocate" on the constexpr heap an exception object of TYPE
1833 : : with REFCOUNT. */
1834 : :
1835 : : static tree
1836 : 16251 : cxa_allocate_exception (location_t loc, const constexpr_ctx *ctx, tree type,
1837 : : tree refcount)
1838 : : {
1839 : 16251 : tree var = build_decl (loc, VAR_DECL, heap_uninit_identifier, type);
1840 : 16251 : DECL_ARTIFICIAL (var) = 1;
1841 : 16251 : retrofit_lang_decl (var);
1842 : 16251 : DECL_EXCEPTION_REFCOUNT (var) = refcount;
1843 : 16251 : ctx->global->heap_vars.safe_push (var);
1844 : 16251 : return var;
1845 : : }
1846 : :
1847 : : /* Evaluate various __cxa_* calls as magic constexpr builtins for
1848 : : C++26 constexpr exception support (P3068R5). */
1849 : :
1850 : : static tree
1851 : 27334 : cxx_eval_cxa_builtin_fn (const constexpr_ctx *ctx, tree call,
1852 : : enum cxa_builtin kind, tree fndecl,
1853 : : bool *non_constant_p, bool *overflow_p,
1854 : : tree *jump_target)
1855 : : {
1856 : 27334 : int nargs = call_expr_nargs (call);
1857 : 27334 : location_t loc = cp_expr_loc_or_input_loc (call);
1858 : 27334 : tree args[4], arg;
1859 : 27334 : if (nargs > 4)
1860 : : {
1861 : 0 : invalid_nargs:
1862 : 0 : if (!ctx->quiet)
1863 : 0 : error_at (loc, "call to %qD function with incorrect "
1864 : : "number of arguments", fndecl);
1865 : 0 : *non_constant_p = true;
1866 : 0 : return call;
1867 : : }
1868 : 27334 : if ((kind == CXA_BEGIN_CATCH || kind == CXA_GET_EXCEPTION_PTR)
1869 : 2640 : && nargs == 1
1870 : 2640 : && (arg = CALL_EXPR_ARG (call, 0))
1871 : 2640 : && TREE_CODE (arg) == CALL_EXPR
1872 : 2640 : && call_expr_nargs (arg) == 1
1873 : 29974 : && integer_zerop (CALL_EXPR_ARG (arg, 0)))
1874 : 2640 : if (tree fun = get_function_named_in_call (arg))
1875 : 2640 : if (fndecl_built_in_p (fun, BUILT_IN_EH_POINTER))
1876 : : {
1877 : 2640 : if (ctx->global->caught_exceptions.length () < 2)
1878 : : {
1879 : 2453 : no_caught_exceptions:
1880 : 2453 : if (!ctx->quiet)
1881 : 0 : error_at (loc, "%qD called with no caught exceptions pending",
1882 : : fndecl);
1883 : 2453 : *non_constant_p = true;
1884 : 2453 : return call;
1885 : : }
1886 : : /* Both __cxa_get_exception_ptr (__builtin_eh_pointer (0))
1887 : : and __cxa_begin_catch (__builtin_eh_pointer (0)) calls expect
1888 : : ctx->global->caught_exceptions vector to end with
1889 : : __cxa_allocate_exception created artificial VAR_DECL (the
1890 : : exception object) followed by handler type, pushed by TRY_BLOCK
1891 : : evaluation. The only difference between the functions is that
1892 : : __cxa_begin_catch pops the handler type from the vector and keeps
1893 : : the VAR_DECL last and decreases uncaught_exceptions. The
1894 : : VAR_DECL after __cxa_begin_catch serves as the current exception
1895 : : and is then popped in __cxa_end_catch evaluation. */
1896 : 187 : tree handler_type = ctx->global->caught_exceptions.last ();
1897 : 187 : if (handler_type && VAR_P (handler_type))
1898 : 0 : goto no_caught_exceptions;
1899 : 187 : unsigned idx = ctx->global->caught_exceptions.length () - 2;
1900 : 187 : arg = ctx->global->caught_exceptions[idx];
1901 : 187 : gcc_assert (VAR_P (arg));
1902 : 187 : if (kind == CXA_BEGIN_CATCH)
1903 : : {
1904 : 175 : ctx->global->caught_exceptions.pop ();
1905 : 175 : --ctx->global->uncaught_exceptions;
1906 : : }
1907 : 187 : if (handler_type == NULL_TREE)
1908 : : /* Used for catch (...). Just return void. */
1909 : 32 : return void_node;
1910 : 155 : else if (POINTER_TYPE_P (handler_type))
1911 : : {
1912 : : /* Used for catch of a pointer. */
1913 : 33 : if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE)
1914 : 33 : arg = build4 (ARRAY_REF, TREE_TYPE (TREE_TYPE (arg)), arg,
1915 : : size_zero_node, NULL_TREE, NULL_TREE);
1916 : 33 : arg = cp_convert (handler_type, arg,
1917 : 33 : ctx->quiet ? tf_none : tf_warning_or_error);
1918 : 33 : if (arg == error_mark_node)
1919 : : {
1920 : 0 : *non_constant_p = true;
1921 : 0 : return call;
1922 : : }
1923 : : }
1924 : : else
1925 : : {
1926 : : /* Used for catch of a non-pointer type. */
1927 : 122 : tree exc_type = strip_array_types (TREE_TYPE (arg));
1928 : 122 : tree exc_ptr_type = build_pointer_type (exc_type);
1929 : 122 : arg = build_fold_addr_expr_with_type (arg, exc_ptr_type);
1930 : 122 : if (CLASS_TYPE_P (handler_type))
1931 : : {
1932 : 81 : tree ptr_type = build_pointer_type (handler_type);
1933 : 81 : arg = cp_convert (ptr_type, arg,
1934 : 81 : ctx->quiet ? tf_none
1935 : : : tf_warning_or_error);
1936 : 81 : if (arg == error_mark_node)
1937 : : {
1938 : 0 : *non_constant_p = true;
1939 : 0 : return call;
1940 : : }
1941 : : }
1942 : : }
1943 : 155 : return cxx_eval_constant_expression (ctx, arg, vc_prvalue,
1944 : : non_constant_p, overflow_p,
1945 : 155 : jump_target);
1946 : : }
1947 : 41924 : for (int i = 0; i < nargs; ++i)
1948 : : {
1949 : 17230 : args[i] = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (call, i),
1950 : : vc_prvalue, non_constant_p,
1951 : : overflow_p, jump_target);
1952 : 17230 : if (*non_constant_p)
1953 : : return call;
1954 : 17230 : if (*jump_target)
1955 : : return NULL_TREE;
1956 : : }
1957 : 24694 : switch (kind)
1958 : : {
1959 : 16123 : case CXA_ALLOCATE_EXCEPTION:
1960 : 16123 : if (nargs != 1)
1961 : 0 : goto invalid_nargs;
1962 : 16123 : if (!tree_fits_uhwi_p (args[0]))
1963 : : {
1964 : 0 : if (!ctx->quiet)
1965 : 0 : error_at (loc, "cannot allocate exception: size not constant");
1966 : 0 : *non_constant_p = true;
1967 : 0 : return call;
1968 : : }
1969 : : else
1970 : : {
1971 : 32246 : tree type = build_array_type_nelts (char_type_node,
1972 : 16123 : tree_to_uhwi (args[0]));
1973 : 16123 : tree var = cxa_allocate_exception (loc, ctx, type, size_zero_node);
1974 : 16123 : ctx->global->put_value (var, NULL_TREE);
1975 : 16123 : return fold_convert (ptr_type_node, build_address (var));
1976 : : }
1977 : 1 : case CXA_FREE_EXCEPTION:
1978 : 1 : if (nargs != 1)
1979 : 0 : goto invalid_nargs;
1980 : 1 : arg = cxa_check_throw_arg (args[0], true);
1981 : 1 : if (arg == NULL_TREE)
1982 : : {
1983 : 0 : invalid_ptr:
1984 : 0 : if (!ctx->quiet)
1985 : 0 : error_at (loc, "first argument to %qD function not result of "
1986 : : "%<__cxa_allocate_exception%>", fndecl);
1987 : 0 : *non_constant_p = true;
1988 : 0 : return call;
1989 : : }
1990 : 1 : DECL_NAME (arg) = heap_deleted_identifier;
1991 : 1 : ctx->global->destroy_value (arg);
1992 : 1 : ctx->global->heap_dealloc_count++;
1993 : 1 : return void_node;
1994 : 314 : case CXA_THROW:
1995 : 314 : if (nargs != 3)
1996 : 0 : goto invalid_nargs;
1997 : 314 : arg = cxa_check_throw_arg (args[0], false);
1998 : 314 : if (arg == NULL_TREE)
1999 : 0 : goto invalid_ptr;
2000 : 314 : DECL_EXCEPTION_REFCOUNT (arg)
2001 : 314 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg),
2002 : : size_one_node);
2003 : 314 : ++ctx->global->uncaught_exceptions;
2004 : 314 : *jump_target = arg;
2005 : 314 : return void_node;
2006 : 0 : case CXA_BEGIN_CATCH:
2007 : 0 : case CXA_GET_EXCEPTION_PTR:
2008 : 0 : goto invalid_nargs;
2009 : 173 : case CXA_END_CATCH:
2010 : 173 : if (nargs != 0)
2011 : 0 : goto invalid_nargs;
2012 : 173 : if (ctx->global->caught_exceptions.is_empty ())
2013 : : {
2014 : 0 : no_active_exc:
2015 : 0 : if (!ctx->quiet)
2016 : 0 : error_at (loc, "%qD called with no caught exceptions active",
2017 : : fndecl);
2018 : 0 : *non_constant_p = true;
2019 : 0 : return call;
2020 : : }
2021 : : else
2022 : : {
2023 : 173 : arg = ctx->global->caught_exceptions.pop ();
2024 : 173 : if (arg == NULL_TREE || !VAR_P (arg))
2025 : 0 : goto no_active_exc;
2026 : 173 : free_except:
2027 : 216 : DECL_EXCEPTION_REFCOUNT (arg)
2028 : 216 : = size_binop (MINUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg),
2029 : : size_one_node);
2030 : 216 : if (integer_zerop (DECL_EXCEPTION_REFCOUNT (arg)))
2031 : : {
2032 : 110 : if (type_build_dtor_call (TREE_TYPE (arg)))
2033 : : {
2034 : 40 : tree cleanup
2035 : 40 : = cxx_maybe_build_cleanup (arg, (ctx->quiet ? tf_none
2036 : : : tf_warning_or_error));
2037 : 40 : if (cleanup == error_mark_node)
2038 : 0 : *non_constant_p = true;
2039 : 40 : tree jmp_target = NULL_TREE;
2040 : 40 : cxx_eval_constant_expression (ctx, cleanup, vc_discard,
2041 : : non_constant_p, overflow_p,
2042 : : &jmp_target);
2043 : 40 : if (throws (&jmp_target))
2044 : 0 : *jump_target = jmp_target;
2045 : : }
2046 : 110 : DECL_NAME (arg) = heap_deleted_identifier;
2047 : 110 : ctx->global->destroy_value (arg);
2048 : 110 : ctx->global->heap_dealloc_count++;
2049 : : }
2050 : : }
2051 : 216 : return void_node;
2052 : 50 : case CXA_RETHROW:
2053 : 50 : if (nargs != 0)
2054 : 0 : goto invalid_nargs;
2055 : 50 : unsigned idx;
2056 : 100 : FOR_EACH_VEC_ELT_REVERSE (ctx->global->caught_exceptions, idx, arg)
2057 : 36 : if (arg == NULL_TREE || !VAR_P (arg))
2058 : 0 : --idx;
2059 : : else
2060 : : break;
2061 : 50 : if (arg == NULL_TREE)
2062 : : {
2063 : 14 : if (!ctx->quiet)
2064 : 4 : error_at (loc, "%qD called with no caught exceptions active",
2065 : : fndecl);
2066 : 14 : *non_constant_p = true;
2067 : 14 : return call;
2068 : : }
2069 : 36 : DECL_EXCEPTION_REFCOUNT (arg)
2070 : 36 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg), size_one_node);
2071 : 36 : ++ctx->global->uncaught_exceptions;
2072 : 36 : *jump_target = arg;
2073 : 36 : return void_node;
2074 : 143 : case CXA_BAD_CAST:
2075 : 143 : case CXA_BAD_TYPEID:
2076 : 143 : case CXA_THROW_BAD_ARRAY_NEW_LENGTH:
2077 : 143 : if (nargs != 0)
2078 : 0 : goto invalid_nargs;
2079 : : else
2080 : : {
2081 : 143 : tree name;
2082 : 143 : switch (kind)
2083 : : {
2084 : 117 : case CXA_BAD_CAST:
2085 : 117 : name = get_identifier ("bad_cast");
2086 : 117 : break;
2087 : 5 : case CXA_BAD_TYPEID:
2088 : 5 : name = get_identifier ("bad_typeid");
2089 : 5 : break;
2090 : 21 : case CXA_THROW_BAD_ARRAY_NEW_LENGTH:
2091 : 21 : name = get_identifier ("bad_array_new_length");
2092 : 21 : break;
2093 : : default:
2094 : : gcc_unreachable ();
2095 : : }
2096 : 143 : tree decl = lookup_qualified_name (std_node, name);
2097 : 143 : if (TREE_CODE (decl) != TYPE_DECL
2098 : 128 : || !CLASS_TYPE_P (TREE_TYPE (decl))
2099 : 271 : || !type_build_ctor_call (TREE_TYPE (decl)))
2100 : : {
2101 : 15 : if (!ctx->quiet)
2102 : 4 : error_at (loc, "%qD called without %<std::%D%> being defined",
2103 : : fndecl, name);
2104 : 15 : *non_constant_p = true;
2105 : 15 : return call;
2106 : : }
2107 : 128 : tree type = TREE_TYPE (decl);
2108 : 128 : tree var = cxa_allocate_exception (loc, ctx, type, size_one_node);
2109 : 128 : tree ctor
2110 : 128 : = build_special_member_call (var, complete_ctor_identifier,
2111 : : NULL, type, LOOKUP_NORMAL,
2112 : 128 : ctx->quiet ? tf_none
2113 : : : tf_warning_or_error);
2114 : 128 : if (ctor == error_mark_node)
2115 : : {
2116 : 0 : *non_constant_p = true;
2117 : 0 : return call;
2118 : : }
2119 : 128 : if (TREE_CONSTANT (ctor))
2120 : 0 : ctx->global->put_value (var, ctor);
2121 : : else
2122 : : {
2123 : 128 : ctx->global->put_value (var, NULL_TREE);
2124 : 128 : cxx_eval_constant_expression (ctx, ctor, vc_discard,
2125 : : non_constant_p, overflow_p,
2126 : : jump_target);
2127 : 128 : if (*non_constant_p)
2128 : : return call;
2129 : 128 : if (throws (jump_target))
2130 : : return NULL_TREE;
2131 : : }
2132 : 128 : ++ctx->global->uncaught_exceptions;
2133 : 128 : *jump_target = var;
2134 : : }
2135 : 128 : return void_node;
2136 : 59 : case STD_UNCAUGHT_EXCEPTIONS:
2137 : 59 : if (nargs != 0)
2138 : 0 : goto invalid_nargs;
2139 : : /* Similarly to __builtin_is_constant_evaluated (), we don't
2140 : : want to give a definite answer during mce_unknown evaluation,
2141 : : because that might prevent evaluation later on when some
2142 : : exceptions might be uncaught. But unlike that, we don't
2143 : : want to constant fold it even during cp_fold, because at runtime
2144 : : std::uncaught_exceptions () might still be non-zero. */
2145 : 59 : if (ctx->manifestly_const_eval != mce_true)
2146 : : {
2147 : 38 : *non_constant_p = true;
2148 : 38 : return call;
2149 : : }
2150 : 21 : return build_int_cst (integer_type_node,
2151 : 21 : ctx->global->uncaught_exceptions);
2152 : 7738 : case STD_CURRENT_EXCEPTION:
2153 : 7738 : if (nargs != 0)
2154 : 0 : goto invalid_nargs;
2155 : : else
2156 : : {
2157 : 7738 : tree name = get_identifier ("exception_ptr");
2158 : 7738 : tree decl = lookup_qualified_name (std_node, name);
2159 : 7738 : tree fld;
2160 : 7738 : if (TREE_CODE (decl) != TYPE_DECL
2161 : 7738 : || !CLASS_TYPE_P (TREE_TYPE (decl))
2162 : 7738 : || !COMPLETE_TYPE_P (TREE_TYPE (decl))
2163 : 7738 : || !(fld = next_aggregate_field (TYPE_FIELDS (TREE_TYPE (decl))))
2164 : 7738 : || DECL_ARTIFICIAL (fld)
2165 : 7738 : || TREE_CODE (TREE_TYPE (fld)) != POINTER_TYPE
2166 : 7738 : || next_aggregate_field (DECL_CHAIN (fld))
2167 : 15476 : || !tree_int_cst_equal (TYPE_SIZE (TREE_TYPE (decl)),
2168 : 7738 : TYPE_SIZE (TREE_TYPE (fld))))
2169 : : {
2170 : 0 : if (!ctx->quiet)
2171 : 0 : error_at (loc, "%qD called without supportable %qs",
2172 : : fndecl, "std::exception_ptr");
2173 : 0 : *non_constant_p = true;
2174 : 0 : return call;
2175 : : }
2176 : 15476 : FOR_EACH_VEC_ELT_REVERSE (ctx->global->caught_exceptions, idx, arg)
2177 : 21 : if (arg == NULL_TREE || !VAR_P (arg))
2178 : 0 : --idx;
2179 : : else
2180 : : break;
2181 : : /* Similarly to __builtin_is_constant_evaluated (), we don't
2182 : : want to give a definite answer during mce_unknown evaluation,
2183 : : because that might prevent evaluation later on when some
2184 : : exceptions might be current. But unlike that, we don't
2185 : : want to constant fold it to null even during cp_fold, because
2186 : : at runtime std::current_exception () might still be non-null. */
2187 : 7738 : if (ctx->manifestly_const_eval != mce_true && arg == NULL_TREE)
2188 : : {
2189 : 7706 : *non_constant_p = true;
2190 : 7706 : return call;
2191 : : }
2192 : 32 : if (arg == NULL_TREE)
2193 : 11 : arg = build_zero_cst (TREE_TYPE (fld));
2194 : : else
2195 : : {
2196 : 21 : DECL_EXCEPTION_REFCOUNT (arg)
2197 : 21 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg),
2198 : : size_one_node);
2199 : 21 : arg = fold_convert (ptr_type_node, build_address (arg));
2200 : : }
2201 : 32 : return build_constructor_single (TREE_TYPE (decl), fld, arg);
2202 : : }
2203 : 22 : case STD_RETHROW_EXCEPTION:
2204 : 22 : if (nargs != 1)
2205 : 0 : goto invalid_nargs;
2206 : 22 : if (TYPE_REF_P (TREE_TYPE (args[0])))
2207 : : {
2208 : 22 : arg = args[0];
2209 : 22 : STRIP_NOPS (arg);
2210 : 22 : if (TREE_CODE (arg) == ADDR_EXPR)
2211 : : {
2212 : 22 : args[0]
2213 : 22 : = cxx_eval_constant_expression (ctx, TREE_OPERAND (arg, 0),
2214 : : vc_prvalue, non_constant_p,
2215 : : overflow_p, jump_target);
2216 : 22 : if (*non_constant_p)
2217 : : return call;
2218 : 22 : if (*jump_target)
2219 : : return NULL_TREE;
2220 : : }
2221 : : }
2222 : 22 : if (TREE_CODE (args[0]) != CONSTRUCTOR
2223 : 22 : || CONSTRUCTOR_NELTS (args[0]) != 1)
2224 : : {
2225 : 0 : invalid_std_rethrow:
2226 : 0 : if (!ctx->quiet)
2227 : 0 : error_at (loc, "%qD called with unexpected %qs argument",
2228 : : fndecl, "std::exception_ptr");
2229 : 0 : *non_constant_p = true;
2230 : 0 : return void_node;
2231 : : }
2232 : 22 : arg = cxa_check_throw_arg (CONSTRUCTOR_ELT (args[0], 0)->value, false);
2233 : 22 : if (arg == NULL_TREE)
2234 : 0 : goto invalid_std_rethrow;
2235 : 22 : DECL_EXCEPTION_REFCOUNT (arg)
2236 : 22 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg), size_one_node);
2237 : 22 : ++ctx->global->uncaught_exceptions;
2238 : 22 : *jump_target = arg;
2239 : 22 : return void_node;
2240 : 71 : case BUILTIN_EH_PTR_ADJUST_REF:
2241 : 71 : if (nargs != 2)
2242 : 0 : goto invalid_nargs;
2243 : 71 : arg = cxa_check_throw_arg (args[0], false);
2244 : 71 : if (arg == NULL_TREE)
2245 : 0 : goto invalid_ptr;
2246 : 71 : if (integer_onep (args[1]))
2247 : 28 : DECL_EXCEPTION_REFCOUNT (arg)
2248 : 56 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg),
2249 : : size_one_node);
2250 : 43 : else if (integer_minus_onep (args[1]))
2251 : 43 : goto free_except;
2252 : : else
2253 : : {
2254 : 0 : if (!ctx->quiet)
2255 : 0 : error_at (loc, "%qD called with second argument "
2256 : : "other than 1 or -1", fndecl);
2257 : 0 : *non_constant_p = true;
2258 : : }
2259 : 28 : return void_node;
2260 : 0 : default:
2261 : 0 : gcc_unreachable ();
2262 : : }
2263 : : }
2264 : :
2265 : : /* Attempt to evaluate T which represents a call to a builtin function.
2266 : : We assume here that all builtin functions evaluate to scalar types
2267 : : represented by _CST nodes. */
2268 : :
2269 : : static tree
2270 : 10986601 : cxx_eval_builtin_function_call (const constexpr_ctx *ctx, tree t, tree fun,
2271 : : value_cat lval,
2272 : : bool *non_constant_p, bool *overflow_p,
2273 : : tree *jump_target)
2274 : : {
2275 : 10986601 : const int nargs = call_expr_nargs (t);
2276 : 10986601 : tree *args = (tree *) alloca (nargs * sizeof (tree));
2277 : 10986601 : tree new_call;
2278 : 10986601 : int i;
2279 : :
2280 : : /* Don't fold __builtin_constant_p within a constexpr function. */
2281 : 10986601 : bool bi_const_p = DECL_IS_BUILTIN_CONSTANT_P (fun);
2282 : :
2283 : : /* If we aren't requiring a constant expression, defer __builtin_constant_p
2284 : : in a constexpr function until we have values for the parameters. */
2285 : 751583 : if (bi_const_p
2286 : 751583 : && ctx->manifestly_const_eval != mce_true
2287 : 737718 : && current_function_decl
2288 : 735544 : && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
2289 : : {
2290 : 437510 : *non_constant_p = true;
2291 : 437510 : return t;
2292 : : }
2293 : :
2294 : : /* For __builtin_is_constant_evaluated, defer it if not
2295 : : ctx->manifestly_const_eval (as sometimes we try to constant evaluate
2296 : : without manifestly_const_eval even expressions or parts thereof which
2297 : : will later be manifestly const_eval evaluated), otherwise fold it to
2298 : : true. */
2299 : 10549091 : if (fndecl_built_in_p (fun, CP_BUILT_IN_IS_CONSTANT_EVALUATED,
2300 : : BUILT_IN_FRONTEND))
2301 : : {
2302 : 1972231 : if (ctx->manifestly_const_eval == mce_unknown)
2303 : : {
2304 : 1955600 : *non_constant_p = true;
2305 : 1955600 : return t;
2306 : : }
2307 : 16631 : return constant_boolean_node (ctx->manifestly_const_eval == mce_true,
2308 : 16631 : boolean_type_node);
2309 : : }
2310 : :
2311 : 8576860 : if (fndecl_built_in_p (fun, CP_BUILT_IN_SOURCE_LOCATION, BUILT_IN_FRONTEND))
2312 : : {
2313 : 436 : temp_override<tree> ovr (current_function_decl);
2314 : 436 : if (ctx->call && ctx->call->fundef)
2315 : 137 : current_function_decl = ctx->call->fundef->decl;
2316 : 436 : return fold_builtin_source_location (t);
2317 : 436 : }
2318 : :
2319 : 8576424 : if (fndecl_built_in_p (fun, CP_BUILT_IN_EH_PTR_ADJUST_REF,
2320 : : BUILT_IN_FRONTEND))
2321 : 71 : return cxx_eval_cxa_builtin_fn (ctx, t, BUILTIN_EH_PTR_ADJUST_REF,
2322 : : fun, non_constant_p, overflow_p,
2323 : 71 : jump_target);
2324 : :
2325 : 8576353 : int strops = 0;
2326 : 8576353 : int strret = 0;
2327 : 8576353 : if (fndecl_built_in_p (fun, BUILT_IN_NORMAL))
2328 : 7940548 : switch (DECL_FUNCTION_CODE (fun))
2329 : : {
2330 : : case BUILT_IN_STRLEN:
2331 : : case BUILT_IN_STRNLEN:
2332 : 8576255 : strops = 1;
2333 : : break;
2334 : 111084 : case BUILT_IN_MEMCHR:
2335 : 111084 : case BUILT_IN_STRCHR:
2336 : 111084 : case BUILT_IN_STRRCHR:
2337 : 111084 : strops = 1;
2338 : 111084 : strret = 1;
2339 : 111084 : break;
2340 : 49757 : case BUILT_IN_MEMCMP:
2341 : 49757 : case BUILT_IN_STRCMP:
2342 : 49757 : strops = 2;
2343 : 49757 : break;
2344 : 30732 : case BUILT_IN_STRSTR:
2345 : 30732 : strops = 2;
2346 : 30732 : strret = 1;
2347 : 30732 : break;
2348 : 42 : case BUILT_IN_ASAN_POINTER_COMPARE:
2349 : 42 : case BUILT_IN_ASAN_POINTER_SUBTRACT:
2350 : : /* These builtins shall be ignored during constant expression
2351 : : evaluation. */
2352 : 42 : return void_node;
2353 : 56 : case BUILT_IN_UNREACHABLE:
2354 : 56 : case BUILT_IN_TRAP:
2355 : 56 : if (!*non_constant_p && !ctx->quiet)
2356 : : {
2357 : : /* Do not allow__builtin_unreachable in constexpr function.
2358 : : The __builtin_unreachable call with BUILTINS_LOCATION
2359 : : comes from cp_maybe_instrument_return. */
2360 : 13 : if (EXPR_LOCATION (t) == BUILTINS_LOCATION)
2361 : 0 : error ("%<constexpr%> call flows off the end of the function");
2362 : : else
2363 : 13 : error ("%q+E is not a constant expression", t);
2364 : : }
2365 : 56 : *non_constant_p = true;
2366 : 56 : return t;
2367 : : default:
2368 : : break;
2369 : : }
2370 : :
2371 : : /* Be permissive for arguments to built-ins; __builtin_constant_p should
2372 : : return constant false for a non-constant argument. */
2373 : 8576255 : constexpr_ctx new_ctx = *ctx;
2374 : 8576255 : new_ctx.quiet = true;
2375 : 23152133 : for (i = 0; i < nargs; ++i)
2376 : : {
2377 : 14575879 : tree arg = CALL_EXPR_ARG (t, i);
2378 : 14575879 : tree oarg = arg;
2379 : :
2380 : : /* To handle string built-ins we need to pass ADDR_EXPR<STRING_CST> since
2381 : : expand_builtin doesn't know how to look in the values table. */
2382 : 14575879 : bool strop = i < strops;
2383 : 14575879 : if (strop)
2384 : : {
2385 : 365920 : STRIP_NOPS (arg);
2386 : 365920 : if (TREE_CODE (arg) == ADDR_EXPR)
2387 : 7788 : arg = TREE_OPERAND (arg, 0);
2388 : : else
2389 : : strop = false;
2390 : : }
2391 : :
2392 : : /* If builtin_valid_in_constant_expr_p is true,
2393 : : potential_constant_expression_1 has not recursed into the arguments
2394 : : of the builtin, verify it here. */
2395 : 14575879 : if (!builtin_valid_in_constant_expr_p (fun)
2396 : 14575879 : || potential_constant_expression (arg))
2397 : : {
2398 : 14575748 : bool dummy1 = false, dummy2 = false;
2399 : 14575748 : tree jmp_target = NULL_TREE;
2400 : 14575748 : arg = cxx_eval_constant_expression (&new_ctx, arg, vc_prvalue,
2401 : : &dummy1, &dummy2, &jmp_target);
2402 : 14575747 : if (jmp_target)
2403 : : {
2404 : 0 : *jump_target = jmp_target;
2405 : 0 : return NULL_TREE;
2406 : : }
2407 : : }
2408 : :
2409 : 14575878 : if (bi_const_p)
2410 : : /* For __builtin_constant_p, fold all expressions with constant values
2411 : : even if they aren't C++ constant-expressions. */
2412 : 314072 : arg = cp_fold_rvalue (arg);
2413 : 14261806 : else if (strop)
2414 : : {
2415 : 7788 : if (TREE_CODE (arg) == CONSTRUCTOR)
2416 : 100 : arg = braced_lists_to_strings (TREE_TYPE (arg), arg);
2417 : 7788 : if (TREE_CODE (arg) == STRING_CST)
2418 : 2885 : arg = build_address (arg);
2419 : : else
2420 : : arg = oarg;
2421 : : }
2422 : :
2423 : 14575878 : args[i] = arg;
2424 : : }
2425 : :
2426 : 8576254 : bool save_ffbcp = force_folding_builtin_constant_p;
2427 : 8576254 : force_folding_builtin_constant_p |= ctx->manifestly_const_eval == mce_true;
2428 : 8576254 : tree save_cur_fn = current_function_decl;
2429 : : /* Return name of ctx->call->fundef->decl for __builtin_FUNCTION (). */
2430 : 8576254 : if (fndecl_built_in_p (fun, BUILT_IN_FUNCTION)
2431 : 38 : && ctx->call
2432 : 8576258 : && ctx->call->fundef)
2433 : 4 : current_function_decl = ctx->call->fundef->decl;
2434 : 8576254 : if (fndecl_built_in_p (fun,
2435 : : CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS,
2436 : : BUILT_IN_FRONTEND))
2437 : : {
2438 : 348 : location_t loc = EXPR_LOCATION (t);
2439 : 348 : if (nargs >= 1)
2440 : 345 : VERIFY_CONSTANT (args[0]);
2441 : 143 : new_call
2442 : 143 : = fold_builtin_is_pointer_inverconvertible_with_class (loc, nargs,
2443 : : args);
2444 : : }
2445 : 8575906 : else if (fndecl_built_in_p (fun,
2446 : : CP_BUILT_IN_IS_CORRESPONDING_MEMBER,
2447 : : BUILT_IN_FRONTEND))
2448 : : {
2449 : 459 : location_t loc = EXPR_LOCATION (t);
2450 : 459 : if (nargs >= 2)
2451 : : {
2452 : 453 : VERIFY_CONSTANT (args[0]);
2453 : 231 : VERIFY_CONSTANT (args[1]);
2454 : : }
2455 : 237 : new_call = fold_builtin_is_corresponding_member (loc, nargs, args);
2456 : : }
2457 : : else
2458 : 17150894 : new_call = fold_builtin_call_array (EXPR_LOCATION (t), TREE_TYPE (t),
2459 : 8575447 : CALL_EXPR_FN (t), nargs, args);
2460 : 8575827 : current_function_decl = save_cur_fn;
2461 : 8575827 : force_folding_builtin_constant_p = save_ffbcp;
2462 : 8575827 : if (new_call == NULL)
2463 : : {
2464 : 6295280 : if (!*non_constant_p && !ctx->quiet)
2465 : : {
2466 : 0 : new_call = build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t),
2467 : 0 : CALL_EXPR_FN (t), nargs, args);
2468 : 0 : error ("%q+E is not a constant expression", new_call);
2469 : : }
2470 : 6295280 : *non_constant_p = true;
2471 : 6295280 : return t;
2472 : : }
2473 : :
2474 : 2280547 : if (!potential_constant_expression (new_call))
2475 : : {
2476 : 577 : if (!*non_constant_p && !ctx->quiet)
2477 : 4 : error ("%q+E is not a constant expression", new_call);
2478 : 577 : *non_constant_p = true;
2479 : 577 : return t;
2480 : : }
2481 : :
2482 : 2279970 : if (strret)
2483 : : {
2484 : : /* memchr returns a pointer into the first argument, but we replaced the
2485 : : argument above with a STRING_CST; put it back it now. */
2486 : 117 : tree op = CALL_EXPR_ARG (t, strret-1);
2487 : 117 : STRIP_NOPS (new_call);
2488 : 117 : if (TREE_CODE (new_call) == POINTER_PLUS_EXPR)
2489 : 59 : TREE_OPERAND (new_call, 0) = op;
2490 : 58 : else if (TREE_CODE (new_call) == ADDR_EXPR)
2491 : 2279970 : new_call = op;
2492 : : }
2493 : :
2494 : 2279970 : return cxx_eval_constant_expression (&new_ctx, new_call, lval,
2495 : : non_constant_p, overflow_p,
2496 : 2279970 : jump_target);
2497 : : }
2498 : :
2499 : : /* TEMP is the constant value of a temporary object of type TYPE. Adjust
2500 : : the type of the value to match. */
2501 : :
2502 : : static tree
2503 : 48514272 : adjust_temp_type (tree type, tree temp)
2504 : : {
2505 : 48514272 : if (same_type_p (TREE_TYPE (temp), type))
2506 : : return temp;
2507 : : /* Avoid wrapping an aggregate value in a NOP_EXPR. */
2508 : 24120657 : if (TREE_CODE (temp) == CONSTRUCTOR)
2509 : : {
2510 : : /* build_constructor wouldn't retain various CONSTRUCTOR flags. */
2511 : 2742292 : tree t = copy_node (temp);
2512 : 2742292 : TREE_TYPE (t) = type;
2513 : 2742292 : return t;
2514 : : }
2515 : 21378365 : if (TREE_CODE (temp) == EMPTY_CLASS_EXPR)
2516 : 0 : return build0 (EMPTY_CLASS_EXPR, type);
2517 : 21378365 : gcc_assert (scalarish_type_p (type));
2518 : : /* Now we know we're dealing with a scalar, and a prvalue of non-class
2519 : : type is cv-unqualified. */
2520 : 21378365 : return cp_fold_convert (cv_unqualified (type), temp);
2521 : : }
2522 : :
2523 : : /* If T is a CONSTRUCTOR, return an unshared copy of T and any
2524 : : sub-CONSTRUCTORs. Otherwise return T.
2525 : :
2526 : : We use this whenever we initialize an object as a whole, whether it's a
2527 : : parameter, a local variable, or a subobject, so that subsequent
2528 : : modifications don't affect other places where it was used. */
2529 : :
2530 : : tree
2531 : 37041222 : unshare_constructor (tree t MEM_STAT_DECL)
2532 : : {
2533 : 37041222 : if (!t || TREE_CODE (t) != CONSTRUCTOR)
2534 : : return t;
2535 : 8117320 : auto_vec <tree*, 4> ptrs;
2536 : 8117320 : ptrs.safe_push (&t);
2537 : 8117320 : while (!ptrs.is_empty ())
2538 : : {
2539 : 9593544 : tree *p = ptrs.pop ();
2540 : 9593544 : tree n = copy_node (*p PASS_MEM_STAT);
2541 : 14525133 : CONSTRUCTOR_ELTS (n) = vec_safe_copy (CONSTRUCTOR_ELTS (*p) PASS_MEM_STAT);
2542 : 9593544 : *p = n;
2543 : 9593544 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (n);
2544 : 9593544 : constructor_elt *ce;
2545 : 37284986 : for (HOST_WIDE_INT i = 0; vec_safe_iterate (v, i, &ce); ++i)
2546 : 9980578 : if (ce->value && TREE_CODE (ce->value) == CONSTRUCTOR)
2547 : 1476224 : ptrs.safe_push (&ce->value);
2548 : : }
2549 : 8117320 : return t;
2550 : 8117320 : }
2551 : :
2552 : : /* If T is a CONSTRUCTOR, ggc_free T and any sub-CONSTRUCTORs. */
2553 : :
2554 : : static void
2555 : 744896 : free_constructor (tree t)
2556 : : {
2557 : 744896 : if (!t || TREE_CODE (t) != CONSTRUCTOR)
2558 : 0 : return;
2559 : 744896 : releasing_vec ctors;
2560 : 744896 : vec_safe_push (ctors, t);
2561 : 1533583 : while (!ctors->is_empty ())
2562 : : {
2563 : 788687 : tree c = ctors->pop ();
2564 : 788687 : if (vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (c))
2565 : : {
2566 : : constructor_elt *ce;
2567 : 253308 : for (HOST_WIDE_INT i = 0; vec_safe_iterate (elts, i, &ce); ++i)
2568 : 167854 : if (ce->value && TREE_CODE (ce->value) == CONSTRUCTOR)
2569 : 43791 : vec_safe_push (ctors, ce->value);
2570 : 85454 : ggc_free (elts);
2571 : : }
2572 : 788687 : ggc_free (c);
2573 : : }
2574 : 744896 : }
2575 : :
2576 : : /* Helper function of cxx_bind_parameters_in_call. Return non-NULL
2577 : : if *TP is address of a static variable (or part of it) currently being
2578 : : constructed or of a heap artificial variable. */
2579 : :
2580 : : static tree
2581 : 12854613 : addr_of_non_const_var (tree *tp, int *walk_subtrees, void *data)
2582 : : {
2583 : 12854613 : if (TREE_CODE (*tp) == ADDR_EXPR)
2584 : 1922831 : if (tree var = get_base_address (TREE_OPERAND (*tp, 0)))
2585 : 1922831 : if (VAR_P (var) && TREE_STATIC (var))
2586 : : {
2587 : 871203 : if (DECL_NAME (var) == heap_uninit_identifier
2588 : 871203 : || DECL_NAME (var) == heap_identifier
2589 : 871203 : || DECL_NAME (var) == heap_vec_uninit_identifier
2590 : 1742406 : || DECL_NAME (var) == heap_vec_identifier)
2591 : : return var;
2592 : :
2593 : 871203 : constexpr_global_ctx *global = (constexpr_global_ctx *) data;
2594 : 871203 : if (global->get_value (var))
2595 : : return var;
2596 : : }
2597 : 12728991 : if (TYPE_P (*tp))
2598 : 1179 : *walk_subtrees = false;
2599 : : return NULL_TREE;
2600 : : }
2601 : :
2602 : : /* Subroutine of cxx_eval_call_expression.
2603 : : We are processing a call expression (either CALL_EXPR or
2604 : : AGGR_INIT_EXPR) in the context of CTX. Evaluate
2605 : : all arguments and bind their values to correspondings
2606 : : parameters, making up the NEW_CALL context. */
2607 : :
2608 : : static tree
2609 : 76427680 : cxx_bind_parameters_in_call (const constexpr_ctx *ctx, tree t, tree fun,
2610 : : tree orig_fun, bool *non_constant_p,
2611 : : bool *overflow_p, bool *non_constant_args,
2612 : : tree *jump_target)
2613 : : {
2614 : 76427680 : int nargs = call_expr_nargs (t);
2615 : 76427680 : tree parms = DECL_ARGUMENTS (fun);
2616 : 76427680 : int i, j = 0;
2617 : 76427680 : if (DECL_HAS_IN_CHARGE_PARM_P (fun) && fun != orig_fun)
2618 : 1233 : ++nargs;
2619 : 76427680 : if (DECL_HAS_VTT_PARM_P (fun)
2620 : 1235 : && fun != orig_fun
2621 : 76428913 : && (DECL_COMPLETE_CONSTRUCTOR_P (orig_fun)
2622 : 900 : || DECL_COMPLETE_DESTRUCTOR_P (orig_fun)))
2623 : 344 : ++nargs;
2624 : : /* We don't record ellipsis args below. */
2625 : 76427680 : int nparms = list_length (parms);
2626 : 76427680 : int nbinds = nargs < nparms ? nargs : nparms;
2627 : 76427680 : tree binds = make_tree_vec (nbinds);
2628 : :
2629 : : /* The call is not a constant expression if it involves the cdtor for a type
2630 : : with virtual bases before C++26. */
2631 : 76427680 : if (cxx_dialect < cxx26
2632 : 76427680 : && (DECL_HAS_IN_CHARGE_PARM_P (fun) || DECL_HAS_VTT_PARM_P (fun)))
2633 : : {
2634 : 17 : if (!ctx->quiet)
2635 : : {
2636 : 3 : error_at (cp_expr_loc_or_input_loc (t),
2637 : : "call to non-%<constexpr%> function %qD", fun);
2638 : 3 : explain_invalid_constexpr_fn (fun);
2639 : : }
2640 : 17 : *non_constant_p = true;
2641 : 17 : return binds;
2642 : : }
2643 : :
2644 : 120049477 : for (i = 0; i < nargs; ++i)
2645 : : {
2646 : 77027854 : tree x, arg;
2647 : 77027854 : tree type = parms ? TREE_TYPE (parms) : void_type_node;
2648 : 77027854 : if (parms && DECL_BY_REFERENCE (parms))
2649 : 7001 : type = TREE_TYPE (type);
2650 : 77027854 : if (i == 1
2651 : 77027854 : && j == 0
2652 : 13081241 : && DECL_HAS_IN_CHARGE_PARM_P (fun)
2653 : 77028976 : && orig_fun != fun)
2654 : : {
2655 : 1122 : if (DECL_COMPLETE_CONSTRUCTOR_P (orig_fun)
2656 : 1122 : || DECL_COMPLETE_DESTRUCTOR_P (orig_fun))
2657 : 324 : x = boolean_true_node;
2658 : : else
2659 : 798 : x = boolean_false_node;
2660 : : j = -1;
2661 : : }
2662 : 77026732 : else if (i == 2
2663 : 77026732 : && j == -1
2664 : 1122 : && DECL_HAS_VTT_PARM_P (fun)
2665 : 1122 : && orig_fun != fun
2666 : 77027854 : && (DECL_COMPLETE_CONSTRUCTOR_P (orig_fun)
2667 : 809 : || DECL_COMPLETE_DESTRUCTOR_P (orig_fun)))
2668 : : {
2669 : 324 : x = build_zero_cst (type);
2670 : 324 : j = -2;
2671 : : }
2672 : : else
2673 : 77026408 : x = get_nth_callarg (t, i + j);
2674 : : /* For member function, the first argument is a pointer to the implied
2675 : : object. For a constructor, it might still be a dummy object, in
2676 : : which case we get the real argument from ctx. */
2677 : 122458474 : if (i == 0 && DECL_CONSTRUCTOR_P (fun)
2678 : 86267133 : && is_dummy_object (x))
2679 : : {
2680 : 4059468 : x = ctx->object;
2681 : 4059468 : x = build_address (x);
2682 : : }
2683 : 77027854 : if (TREE_ADDRESSABLE (type))
2684 : : {
2685 : : /* Undo convert_for_arg_passing work here. */
2686 : 10718 : x = convert_from_reference (x);
2687 : 10718 : arg = cxx_eval_constant_expression (ctx, x, vc_glvalue,
2688 : : non_constant_p, overflow_p,
2689 : : jump_target);
2690 : : }
2691 : : else
2692 : : /* Normally we would strip a TARGET_EXPR in an initialization context
2693 : : such as this, but here we do the elision differently: we keep the
2694 : : TARGET_EXPR, and use its CONSTRUCTOR as the value of the parm. */
2695 : 77017136 : arg = cxx_eval_constant_expression (ctx, x, vc_prvalue,
2696 : : non_constant_p, overflow_p,
2697 : : jump_target);
2698 : 77027854 : if (*jump_target)
2699 : : break;
2700 : : /* Check we aren't dereferencing a null pointer when calling a non-static
2701 : : member function, which is undefined behaviour. */
2702 : 61229232 : if (i == 0 && DECL_OBJECT_MEMBER_FUNCTION_P (fun)
2703 : 33225722 : && integer_zerop (arg)
2704 : : /* But ignore calls from within compiler-generated code, to handle
2705 : : cases like lambda function pointer conversion operator thunks
2706 : : which pass NULL as the 'this' pointer. */
2707 : 77073644 : && !(TREE_CODE (t) == CALL_EXPR && CALL_FROM_THUNK_P (t)))
2708 : : {
2709 : 290 : if (!ctx->quiet)
2710 : 6 : error_at (cp_expr_loc_or_input_loc (x),
2711 : : "dereferencing a null pointer");
2712 : 290 : *non_constant_p = true;
2713 : : }
2714 : : /* Don't VERIFY_CONSTANT here. */
2715 : 77027845 : if (*non_constant_p && ctx->quiet)
2716 : : break;
2717 : : /* Just discard ellipsis args after checking their constantitude. */
2718 : 43621814 : if (!parms)
2719 : 405 : continue;
2720 : :
2721 : 43621409 : if (!*non_constant_p)
2722 : : {
2723 : : /* Make sure the binding has the same type as the parm. But
2724 : : only for constant args. */
2725 : 43620629 : if (TREE_ADDRESSABLE (type))
2726 : : {
2727 : 7503 : if (!same_type_p (type, TREE_TYPE (arg)))
2728 : : {
2729 : 9 : arg = build_fold_addr_expr (arg);
2730 : 9 : arg = cp_fold_convert (build_reference_type (type), arg);
2731 : 9 : arg = convert_from_reference (arg);
2732 : : }
2733 : : }
2734 : 43613126 : else if (!TYPE_REF_P (type))
2735 : 33120807 : arg = adjust_temp_type (type, arg);
2736 : 43620629 : if (!TREE_CONSTANT (arg))
2737 : 30987132 : *non_constant_args = true;
2738 : 12633497 : else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
2739 : : /* The destructor needs to see any modifications the callee makes
2740 : : to the argument. */
2741 : 0 : *non_constant_args = true;
2742 : : /* If arg is or contains address of a heap artificial variable or
2743 : : of a static variable being constructed, avoid caching the
2744 : : function call, as those variables might be modified by the
2745 : : function, or might be modified by the callers in between
2746 : : the cached function and just read by the function. */
2747 : 12633497 : else if (!*non_constant_args
2748 : 12633497 : && cp_walk_tree (&arg, addr_of_non_const_var, ctx->global,
2749 : : NULL))
2750 : 125622 : *non_constant_args = true;
2751 : :
2752 : : /* For virtual calls, adjust the this argument, so that it is
2753 : : the object on which the method is called, rather than
2754 : : one of its bases. */
2755 : 43620629 : if (i == 0 && DECL_VIRTUAL_P (fun))
2756 : : {
2757 : 16540 : tree addr = arg;
2758 : 16540 : STRIP_NOPS (addr);
2759 : 16540 : if (TREE_CODE (addr) == ADDR_EXPR)
2760 : : {
2761 : 16520 : tree obj = TREE_OPERAND (addr, 0);
2762 : 16520 : while (TREE_CODE (obj) == COMPONENT_REF
2763 : 7662 : && DECL_FIELD_IS_BASE (TREE_OPERAND (obj, 1))
2764 : 24185 : && !same_type_ignoring_top_level_qualifiers_p
2765 : 7611 : (TREE_TYPE (obj), DECL_CONTEXT (fun)))
2766 : 54 : obj = TREE_OPERAND (obj, 0);
2767 : 16520 : if (obj != TREE_OPERAND (addr, 0))
2768 : 51 : arg = build_fold_addr_expr_with_type (obj,
2769 : : TREE_TYPE (arg));
2770 : : }
2771 : : }
2772 : 43620629 : TREE_VEC_ELT (binds, i) = arg;
2773 : : }
2774 : 43621409 : parms = TREE_CHAIN (parms);
2775 : : }
2776 : :
2777 : : return binds;
2778 : : }
2779 : :
2780 : : /* Variables and functions to manage constexpr call expansion context.
2781 : : These do not need to be marked for PCH or GC. */
2782 : :
2783 : : /* FIXME remember and print actual constant arguments. */
2784 : : static vec<tree> call_stack;
2785 : : static int call_stack_tick;
2786 : : static int last_cx_error_tick;
2787 : :
2788 : : static int
2789 : 42727049 : push_cx_call_context (tree call)
2790 : : {
2791 : 42727049 : ++call_stack_tick;
2792 : 42727049 : if (!EXPR_HAS_LOCATION (call))
2793 : 20293 : SET_EXPR_LOCATION (call, input_location);
2794 : 42727049 : call_stack.safe_push (call);
2795 : 42727049 : int len = call_stack.length ();
2796 : 42727049 : if (len > max_constexpr_depth)
2797 : 30 : return false;
2798 : : return len;
2799 : : }
2800 : :
2801 : : static void
2802 : 42727047 : pop_cx_call_context (void)
2803 : : {
2804 : 42727047 : ++call_stack_tick;
2805 : 42727047 : call_stack.pop ();
2806 : 42727047 : }
2807 : :
2808 : : vec<tree>
2809 : 222982 : cx_error_context (void)
2810 : : {
2811 : 222982 : vec<tree> r = vNULL;
2812 : 222982 : if (call_stack_tick != last_cx_error_tick
2813 : 222982 : && !call_stack.is_empty ())
2814 : : r = call_stack;
2815 : 222982 : last_cx_error_tick = call_stack_tick;
2816 : 222982 : return r;
2817 : : }
2818 : :
2819 : : /* E is an operand of a failed assertion, fold it either with or without
2820 : : constexpr context. */
2821 : :
2822 : : static tree
2823 : 592 : fold_operand (tree e, const constexpr_ctx *ctx)
2824 : : {
2825 : 592 : if (ctx)
2826 : : {
2827 : 70 : bool new_non_constant_p = false, new_overflow_p = false;
2828 : 70 : tree jmp_target = NULL_TREE;
2829 : 70 : e = cxx_eval_constant_expression (ctx, e, vc_prvalue,
2830 : : &new_non_constant_p,
2831 : : &new_overflow_p, &jmp_target);
2832 : : }
2833 : : else
2834 : 522 : e = fold_non_dependent_expr (e, tf_none, /*manifestly_const_eval=*/true);
2835 : 592 : return e;
2836 : : }
2837 : :
2838 : : /* If we have a condition in conjunctive normal form (CNF), find the first
2839 : : failing clause. In other words, given an expression like
2840 : :
2841 : : true && true && false && true && false
2842 : :
2843 : : return the first 'false'. EXPR is the expression. */
2844 : :
2845 : : static tree
2846 : 396 : find_failing_clause_r (const constexpr_ctx *ctx, tree expr)
2847 : : {
2848 : 508 : if (TREE_CODE (expr) == TRUTH_ANDIF_EXPR)
2849 : : {
2850 : : /* First check the left side... */
2851 : 224 : tree e = find_failing_clause_r (ctx, TREE_OPERAND (expr, 0));
2852 : 224 : if (e == NULL_TREE)
2853 : : /* ...if we didn't find a false clause, check the right side. */
2854 : 112 : e = find_failing_clause_r (ctx, TREE_OPERAND (expr, 1));
2855 : : return e;
2856 : : }
2857 : 284 : tree e = contextual_conv_bool (expr, tf_none);
2858 : 284 : e = fold_operand (e, ctx);
2859 : 284 : if (integer_zerop (e))
2860 : : /* This is the failing clause. */
2861 : 172 : return expr;
2862 : : return NULL_TREE;
2863 : : }
2864 : :
2865 : : /* Wrapper for find_failing_clause_r. */
2866 : :
2867 : : tree
2868 : 1627 : find_failing_clause (const constexpr_ctx *ctx, tree expr)
2869 : : {
2870 : 1627 : if (TREE_CODE (expr) == TRUTH_ANDIF_EXPR)
2871 : 172 : if (tree e = find_failing_clause_r (ctx, expr))
2872 : 1627 : expr = e;
2873 : 1627 : return expr;
2874 : : }
2875 : :
2876 : : /* Emit additional diagnostics for failing condition BAD.
2877 : : Used by finish_static_assert and IFN_ASSUME constexpr diagnostics.
2878 : : If SHOW_EXPR_P is true, print the condition (because it was
2879 : : instantiation-dependent). */
2880 : :
2881 : : void
2882 : 1627 : diagnose_failing_condition (tree bad, location_t cloc, bool show_expr_p,
2883 : : const constexpr_ctx *ctx /* = nullptr */)
2884 : : {
2885 : : /* Nobody wants to see the artificial (bool) cast. */
2886 : 1627 : bad = tree_strip_nop_conversions (bad);
2887 : 1627 : if (TREE_CODE (bad) == CLEANUP_POINT_EXPR)
2888 : 4 : bad = TREE_OPERAND (bad, 0);
2889 : :
2890 : 1627 : auto_diagnostic_nesting_level sentinel;
2891 : :
2892 : : /* Actually explain the failure if this is a concept check or a
2893 : : requires-expression. */
2894 : 1627 : if (concept_check_p (bad) || TREE_CODE (bad) == REQUIRES_EXPR)
2895 : 301 : diagnose_constraints (cloc, bad, NULL_TREE);
2896 : : /* Similarly if this is a standard trait. */
2897 : 1326 : else if (maybe_diagnose_standard_trait (cloc, bad))
2898 : : ;
2899 : 1045 : else if (COMPARISON_CLASS_P (bad)
2900 : 1045 : && ARITHMETIC_TYPE_P (TREE_TYPE (TREE_OPERAND (bad, 0))))
2901 : : {
2902 : 154 : tree op0 = fold_operand (TREE_OPERAND (bad, 0), ctx);
2903 : 154 : tree op1 = fold_operand (TREE_OPERAND (bad, 1), ctx);
2904 : 154 : tree cond = build2 (TREE_CODE (bad), boolean_type_node, op0, op1);
2905 : 154 : inform (cloc, "the comparison reduces to %qE", cond);
2906 : : }
2907 : 891 : else if (show_expr_p)
2908 : 654 : inform (cloc, "%qE evaluates to false", bad);
2909 : 1627 : }
2910 : :
2911 : : /* Process an assert/assume of ORIG_ARG. If it's not supposed to be evaluated,
2912 : : do it without changing the current evaluation state. If it evaluates to
2913 : : false, complain and return false; otherwise, return true. */
2914 : :
2915 : : static bool
2916 : 283680 : cxx_eval_assert (const constexpr_ctx *ctx, tree arg, const char *msg,
2917 : : location_t loc, bool evaluated,
2918 : : bool *non_constant_p, bool *overflow_p)
2919 : : {
2920 : 283680 : if (*non_constant_p)
2921 : : return true;
2922 : :
2923 : 283680 : tree eval, jmp_target = NULL_TREE;
2924 : 283680 : if (!evaluated)
2925 : : {
2926 : 283609 : if (!potential_rvalue_constant_expression (arg))
2927 : 19 : return true;
2928 : :
2929 : 283590 : constexpr_ctx new_ctx = *ctx;
2930 : 283590 : new_ctx.quiet = true;
2931 : 283590 : bool new_non_constant_p = false, new_overflow_p = false;
2932 : : /* Avoid modification of existing values. */
2933 : 283590 : modifiable_tracker ms (new_ctx.global);
2934 : 283590 : eval = cxx_eval_constant_expression (&new_ctx, arg, vc_prvalue,
2935 : : &new_non_constant_p,
2936 : : &new_overflow_p, &jmp_target);
2937 : 283590 : }
2938 : : else
2939 : 71 : eval = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
2940 : : non_constant_p,
2941 : : overflow_p, &jmp_target);
2942 : 283661 : if (jmp_target)
2943 : : return true;
2944 : :
2945 : 283661 : if (!*non_constant_p && integer_zerop (eval))
2946 : : {
2947 : 99 : if (!ctx->quiet)
2948 : : {
2949 : : /* See if we can find which clause was failing
2950 : : (for logical AND). */
2951 : 32 : tree bad = find_failing_clause (ctx, arg);
2952 : : /* If not, or its location is unusable, fall back to the
2953 : : previous location. */
2954 : 32 : location_t cloc = cp_expr_loc_or_loc (bad, loc);
2955 : :
2956 : : /* Report the error. */
2957 : 32 : auto_diagnostic_group d;
2958 : 32 : error_at (cloc, msg);
2959 : 32 : diagnose_failing_condition (bad, cloc, true, ctx);
2960 : 32 : return bad;
2961 : 32 : }
2962 : 67 : *non_constant_p = true;
2963 : 67 : return false;
2964 : : }
2965 : :
2966 : : return true;
2967 : : }
2968 : :
2969 : : /* Evaluate a call T to a GCC internal function when possible and return
2970 : : the evaluated result or, under the control of CTX, give an error, set
2971 : : NON_CONSTANT_P, and return the unevaluated call T otherwise. */
2972 : :
2973 : : static tree
2974 : 332622 : cxx_eval_internal_function (const constexpr_ctx *ctx, tree t,
2975 : : value_cat lval,
2976 : : bool *non_constant_p, bool *overflow_p,
2977 : : tree *jump_target)
2978 : : {
2979 : 332622 : enum tree_code opcode = ERROR_MARK;
2980 : :
2981 : 332622 : switch (CALL_EXPR_IFN (t))
2982 : : {
2983 : 165 : case IFN_UBSAN_NULL:
2984 : 165 : case IFN_UBSAN_BOUNDS:
2985 : 165 : case IFN_UBSAN_VPTR:
2986 : 165 : case IFN_FALLTHROUGH:
2987 : 165 : return void_node;
2988 : :
2989 : 283593 : case IFN_ASSUME:
2990 : 283593 : if (!cxx_eval_assert (ctx, CALL_EXPR_ARG (t, 0),
2991 : : G_("failed %<assume%> attribute assumption"),
2992 : 283593 : EXPR_LOCATION (t), /*eval*/false,
2993 : : non_constant_p, overflow_p))
2994 : : return t;
2995 : 283564 : return void_node;
2996 : :
2997 : : case IFN_ADD_OVERFLOW:
2998 : : opcode = PLUS_EXPR;
2999 : : break;
3000 : 339 : case IFN_SUB_OVERFLOW:
3001 : 339 : opcode = MINUS_EXPR;
3002 : 339 : break;
3003 : 47967 : case IFN_MUL_OVERFLOW:
3004 : 47967 : opcode = MULT_EXPR;
3005 : 47967 : break;
3006 : :
3007 : 74 : case IFN_LAUNDER:
3008 : 74 : return cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0),
3009 : : vc_prvalue, non_constant_p,
3010 : 74 : overflow_p, jump_target);
3011 : :
3012 : 0 : case IFN_DEFERRED_INIT:
3013 : 0 : return build_clobber (TREE_TYPE (t), CLOBBER_OBJECT_BEGIN);
3014 : :
3015 : 30 : case IFN_VEC_CONVERT:
3016 : 30 : {
3017 : 30 : tree arg = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0),
3018 : : vc_prvalue, non_constant_p,
3019 : : overflow_p, jump_target);
3020 : 30 : if (*jump_target)
3021 : : return NULL_TREE;
3022 : 30 : if (TREE_CODE (arg) == VECTOR_CST)
3023 : 19 : if (tree r = fold_const_call (CFN_VEC_CONVERT, TREE_TYPE (t), arg))
3024 : : return r;
3025 : : }
3026 : : /* FALLTHRU */
3027 : :
3028 : 16 : default:
3029 : 16 : if (!ctx->quiet)
3030 : 0 : error_at (cp_expr_loc_or_input_loc (t),
3031 : : "call to internal function %qE", t);
3032 : 16 : *non_constant_p = true;
3033 : 16 : return t;
3034 : : }
3035 : :
3036 : : /* Evaluate constant arguments using OPCODE and return a complex
3037 : : number containing the result and the overflow bit. */
3038 : 48760 : tree arg0 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), lval,
3039 : : non_constant_p, overflow_p,
3040 : : jump_target);
3041 : 48760 : tree arg1 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 1), lval,
3042 : : non_constant_p, overflow_p,
3043 : : jump_target);
3044 : 48760 : if (*jump_target)
3045 : : return NULL_TREE;
3046 : 48760 : if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
3047 : : {
3048 : 2 : location_t loc = cp_expr_loc_or_input_loc (t);
3049 : 2 : tree type = TREE_TYPE (TREE_TYPE (t));
3050 : 2 : tree result = fold_binary_loc (loc, opcode, type,
3051 : : fold_convert_loc (loc, type, arg0),
3052 : : fold_convert_loc (loc, type, arg1));
3053 : 2 : tree ovf
3054 : 2 : = build_int_cst (type, arith_overflowed_p (opcode, type, arg0, arg1));
3055 : : /* Reset TREE_OVERFLOW to avoid warnings for the overflow. */
3056 : 2 : if (TREE_OVERFLOW (result))
3057 : 0 : TREE_OVERFLOW (result) = 0;
3058 : :
3059 : 2 : return build_complex (TREE_TYPE (t), result, ovf);
3060 : : }
3061 : :
3062 : 48758 : *non_constant_p = true;
3063 : 48758 : return t;
3064 : : }
3065 : :
3066 : : /* Clean CONSTRUCTOR_NO_CLEARING from CTOR and its sub-aggregates. */
3067 : :
3068 : : static void
3069 : 4205705 : clear_no_implicit_zero (tree ctor)
3070 : : {
3071 : 4205705 : if (CONSTRUCTOR_NO_CLEARING (ctor))
3072 : : {
3073 : 1146066 : CONSTRUCTOR_NO_CLEARING (ctor) = false;
3074 : 5312545 : for (auto &e: CONSTRUCTOR_ELTS (ctor))
3075 : 1898369 : if (TREE_CODE (e.value) == CONSTRUCTOR)
3076 : 327839 : clear_no_implicit_zero (e.value);
3077 : : }
3078 : 4205705 : }
3079 : :
3080 : : /* Complain about a const object OBJ being modified in a constant expression.
3081 : : EXPR is the MODIFY_EXPR expression performing the modification. */
3082 : :
3083 : : static void
3084 : 63 : modifying_const_object_error (tree expr, tree obj)
3085 : : {
3086 : 63 : location_t loc = cp_expr_loc_or_input_loc (expr);
3087 : 63 : auto_diagnostic_group d;
3088 : 126 : error_at (loc, "modifying a const object %qE is not allowed in "
3089 : 63 : "a constant expression", TREE_OPERAND (expr, 0));
3090 : :
3091 : : /* Find the underlying object that was declared as const. */
3092 : 63 : location_t decl_loc = UNKNOWN_LOCATION;
3093 : 138 : for (tree probe = obj; decl_loc == UNKNOWN_LOCATION; )
3094 : 75 : switch (TREE_CODE (probe))
3095 : : {
3096 : 39 : case BIT_FIELD_REF:
3097 : 39 : case COMPONENT_REF:
3098 : 39 : {
3099 : 39 : tree elt = TREE_OPERAND (probe, 1);
3100 : 39 : if (CP_TYPE_CONST_P (TREE_TYPE (elt)))
3101 : 27 : decl_loc = DECL_SOURCE_LOCATION (elt);
3102 : 39 : probe = TREE_OPERAND (probe, 0);
3103 : : }
3104 : 39 : break;
3105 : :
3106 : 0 : case ARRAY_REF:
3107 : 0 : case REALPART_EXPR:
3108 : 0 : case IMAGPART_EXPR:
3109 : 0 : probe = TREE_OPERAND (probe, 0);
3110 : 0 : break;
3111 : :
3112 : 36 : default:
3113 : 36 : decl_loc = location_of (probe);
3114 : 36 : break;
3115 : : }
3116 : 63 : inform (decl_loc, "originally declared %<const%> here");
3117 : 63 : }
3118 : :
3119 : : /* Return true if FNDECL is a replaceable global allocation function that
3120 : : should be useable during constant expression evaluation. */
3121 : :
3122 : : static inline bool
3123 : 30651844 : cxx_replaceable_global_alloc_fn (tree fndecl)
3124 : : {
3125 : 30651844 : return (cxx_dialect >= cxx20
3126 : 9310374 : && IDENTIFIER_NEWDEL_OP_P (DECL_NAME (fndecl))
3127 : 354670 : && CP_DECL_CONTEXT (fndecl) == global_namespace
3128 : 31006369 : && (DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl)
3129 : 153207 : || DECL_IS_OPERATOR_DELETE_P (fndecl)));
3130 : : }
3131 : :
3132 : : /* Return true if FNDECL is a placement new function that should be
3133 : : useable during constant expression evaluation of std::construct_at. */
3134 : :
3135 : : static inline bool
3136 : 30420785 : cxx_placement_new_fn (tree fndecl)
3137 : : {
3138 : 30420785 : return (cxx_dialect >= cxx20 && std_placement_new_fn_p (fndecl));
3139 : : }
3140 : :
3141 : : /* Return true if FNDECL is std::construct_at. */
3142 : :
3143 : : static inline bool
3144 : 224860 : is_std_construct_at (tree fndecl)
3145 : : {
3146 : 224860 : if (!decl_in_std_namespace_p (fndecl))
3147 : : return false;
3148 : :
3149 : 215562 : tree name = DECL_NAME (fndecl);
3150 : 215562 : return name && id_equal (name, "construct_at");
3151 : : }
3152 : :
3153 : : /* Overload for the above taking constexpr_call*. */
3154 : :
3155 : : static inline bool
3156 : 201906 : is_std_construct_at (const constexpr_call *call)
3157 : : {
3158 : 201906 : return (call
3159 : 174068 : && call->fundef
3160 : 375974 : && is_std_construct_at (call->fundef->decl));
3161 : : }
3162 : :
3163 : : /* True if CTX is an instance of std::NAME class. */
3164 : :
3165 : : bool
3166 : 4207659 : is_std_class (tree ctx, const char *name)
3167 : : {
3168 : 4207659 : if (ctx == NULL_TREE || !CLASS_TYPE_P (ctx) || !TYPE_MAIN_DECL (ctx))
3169 : : return false;
3170 : :
3171 : 4206807 : tree decl = TYPE_MAIN_DECL (ctx);
3172 : 4206807 : tree dname = DECL_NAME (decl);
3173 : 4206807 : if (dname == NULL_TREE || !id_equal (dname, name))
3174 : : return false;
3175 : :
3176 : 199272 : return decl_in_std_namespace_p (decl);
3177 : : }
3178 : :
3179 : : /* True if CTX is an instance of std::allocator. */
3180 : :
3181 : : bool
3182 : 81725 : is_std_allocator (tree ctx)
3183 : : {
3184 : 81725 : return is_std_class (ctx, "allocator");
3185 : : }
3186 : :
3187 : : /* Return true if FNDECL is std::allocator<T>::{,de}allocate. */
3188 : :
3189 : : static inline bool
3190 : 93275 : is_std_allocator_allocate (tree fndecl)
3191 : : {
3192 : 93275 : tree name = DECL_NAME (fndecl);
3193 : 93275 : if (name == NULL_TREE
3194 : 93275 : || !(id_equal (name, "allocate") || id_equal (name, "deallocate")))
3195 : : return false;
3196 : :
3197 : 77597 : return is_std_allocator (DECL_CONTEXT (fndecl));
3198 : : }
3199 : :
3200 : : /* Overload for the above taking constexpr_call*. */
3201 : :
3202 : : static inline bool
3203 : 52396 : is_std_allocator_allocate (const constexpr_call *call)
3204 : : {
3205 : 52396 : return (call
3206 : 20750 : && call->fundef
3207 : 73146 : && is_std_allocator_allocate (call->fundef->decl));
3208 : : }
3209 : :
3210 : : /* Return true if FNDECL is std::source_location::current. */
3211 : :
3212 : : static inline bool
3213 : 8435 : is_std_source_location_current (tree fndecl)
3214 : : {
3215 : 8435 : if (!decl_in_std_namespace_p (fndecl))
3216 : : return false;
3217 : :
3218 : 1102 : tree name = DECL_NAME (fndecl);
3219 : 1102 : if (name == NULL_TREE || !id_equal (name, "current"))
3220 : : return false;
3221 : :
3222 : 159 : tree ctx = DECL_CONTEXT (fndecl);
3223 : 159 : if (ctx == NULL_TREE || !CLASS_TYPE_P (ctx) || !TYPE_MAIN_DECL (ctx))
3224 : : return false;
3225 : :
3226 : 159 : name = DECL_NAME (TYPE_MAIN_DECL (ctx));
3227 : 159 : return name && id_equal (name, "source_location");
3228 : : }
3229 : :
3230 : : /* Overload for the above taking constexpr_call*. */
3231 : :
3232 : : static inline bool
3233 : 19963 : is_std_source_location_current (const constexpr_call *call)
3234 : : {
3235 : 19963 : return (call
3236 : 8435 : && call->fundef
3237 : 28398 : && is_std_source_location_current (call->fundef->decl));
3238 : : }
3239 : :
3240 : : /* Return true if FNDECL is __dynamic_cast. */
3241 : :
3242 : : static inline bool
3243 : 30372428 : cxx_dynamic_cast_fn_p (tree fndecl)
3244 : : {
3245 : 30372428 : return (cxx_dialect >= cxx20
3246 : 9030935 : && id_equal (DECL_NAME (fndecl), "__dynamic_cast")
3247 : 5018 : && CP_DECL_CONTEXT (fndecl) == abi_node
3248 : : /* Only consider implementation-detail __dynamic_cast calls that
3249 : : correspond to a dynamic_cast, and ignore direct calls to
3250 : : abi::__dynamic_cast. */
3251 : 30377446 : && DECL_ARTIFICIAL (fndecl));
3252 : : }
3253 : :
3254 : : /* Often, we have an expression in the form of address + offset, e.g.
3255 : : "&_ZTV1A + 16". Extract the object from it, i.e. "_ZTV1A". */
3256 : :
3257 : : static tree
3258 : 4478 : extract_obj_from_addr_offset (tree expr)
3259 : : {
3260 : 4478 : if (TREE_CODE (expr) == POINTER_PLUS_EXPR)
3261 : 2167 : expr = TREE_OPERAND (expr, 0);
3262 : 4478 : STRIP_NOPS (expr);
3263 : 4478 : if (TREE_CODE (expr) == ADDR_EXPR)
3264 : 4471 : expr = TREE_OPERAND (expr, 0);
3265 : 4478 : return expr;
3266 : : }
3267 : :
3268 : : /* Given a PATH like
3269 : :
3270 : : g.D.2181.D.2154.D.2102.D.2093
3271 : :
3272 : : find a component with type TYPE. Return NULL_TREE if not found, and
3273 : : error_mark_node if the component is not accessible. If STOP is non-null,
3274 : : this function will return NULL_TREE if STOP is found before TYPE. */
3275 : :
3276 : : static tree
3277 : 2212 : get_component_with_type (tree path, tree type, tree stop)
3278 : : {
3279 : 6398 : while (true)
3280 : : {
3281 : 4305 : if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path), type))
3282 : : /* Found it. */
3283 : : return path;
3284 : 3142 : else if (stop
3285 : 3142 : && (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path),
3286 : : stop)))
3287 : : return NULL_TREE;
3288 : 3097 : else if (TREE_CODE (path) == COMPONENT_REF
3289 : 3097 : && DECL_FIELD_IS_BASE (TREE_OPERAND (path, 1)))
3290 : : {
3291 : : /* We need to check that the component we're accessing is in fact
3292 : : accessible. */
3293 : 3097 : if (TREE_PRIVATE (TREE_OPERAND (path, 1))
3294 : 3097 : || TREE_PROTECTED (TREE_OPERAND (path, 1)))
3295 : 1004 : return error_mark_node;
3296 : 2093 : path = TREE_OPERAND (path, 0);
3297 : : }
3298 : : else
3299 : : return NULL_TREE;
3300 : : }
3301 : : }
3302 : :
3303 : : /* Evaluate a call to __dynamic_cast (permitted by P1327R1).
3304 : :
3305 : : The declaration of __dynamic_cast is:
3306 : :
3307 : : void* __dynamic_cast (const void* __src_ptr,
3308 : : const __class_type_info* __src_type,
3309 : : const __class_type_info* __dst_type,
3310 : : ptrdiff_t __src2dst);
3311 : :
3312 : : where src2dst has the following possible values
3313 : :
3314 : : >-1: src_type is a unique public non-virtual base of dst_type
3315 : : dst_ptr + src2dst == src_ptr
3316 : : -1: unspecified relationship
3317 : : -2: src_type is not a public base of dst_type
3318 : : -3: src_type is a multiple public non-virtual base of dst_type */
3319 : :
3320 : : static tree
3321 : 2402 : cxx_eval_dynamic_cast_fn (const constexpr_ctx *ctx, tree call,
3322 : : bool *non_constant_p, bool *overflow_p,
3323 : : tree *jump_target)
3324 : : {
3325 : : /* T will be something like
3326 : : __dynamic_cast ((B*) b, &_ZTI1B, &_ZTI1D, 8)
3327 : : dismantle it. */
3328 : 2402 : gcc_assert (call_expr_nargs (call) == 4);
3329 : 2402 : tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
3330 : 2402 : tree obj = CALL_EXPR_ARG (call, 0);
3331 : 2402 : tree type = CALL_EXPR_ARG (call, 2);
3332 : 2402 : HOST_WIDE_INT hint = int_cst_value (CALL_EXPR_ARG (call, 3));
3333 : 2402 : location_t loc = cp_expr_loc_or_input_loc (call);
3334 : :
3335 : : /* Get the target type of the dynamic_cast. */
3336 : 2402 : gcc_assert (TREE_CODE (type) == ADDR_EXPR);
3337 : 2402 : type = TREE_OPERAND (type, 0);
3338 : 2402 : type = TREE_TYPE (DECL_NAME (type));
3339 : :
3340 : : /* TYPE can only be either T* or T&. We can't know which of these it
3341 : : is by looking at TYPE, but OBJ will be "(T*) x" in the first case,
3342 : : and something like "(T*)(T&)(T*) x" in the second case.
3343 : : This is true for the reference cases in C++ < 26 or when exceptions
3344 : : aren't enabled, in that case we should diagnose errors. For C++26
3345 : : with exceptions we should silently evaluate to null pointer and
3346 : : let the callers call __cxa_bad_cast () later to throw an exception. */
3347 : 2402 : bool fail_for_non_constant_p = false;
3348 : 11086 : while (CONVERT_EXPR_P (obj) || TREE_CODE (obj) == SAVE_EXPR)
3349 : : {
3350 : 8684 : if (cxx_dialect < cxx26 || !flag_exceptions)
3351 : 5196 : fail_for_non_constant_p |= TYPE_REF_P (TREE_TYPE (obj));
3352 : 8684 : obj = TREE_OPERAND (obj, 0);
3353 : : }
3354 : :
3355 : : /* Evaluate the object so that we know its dynamic type. */
3356 : 2402 : obj = cxx_eval_constant_expression (ctx, obj, vc_prvalue, non_constant_p,
3357 : : overflow_p, jump_target);
3358 : 2402 : if (*non_constant_p)
3359 : : return call;
3360 : 2311 : if (*jump_target)
3361 : : return NULL_TREE;
3362 : :
3363 : : /* For dynamic_cast from classes with virtual bases we can get something
3364 : : like (virt_base *)(&d + 16) as OBJ. Try to convert that into
3365 : : d.D.1234 using cxx_fold_indirect_ref. */
3366 : 2311 : if (cxx_dialect >= cxx26 && CONVERT_EXPR_P (obj))
3367 : : {
3368 : 609 : tree objo = obj;
3369 : 609 : STRIP_NOPS (objo);
3370 : 609 : if (TREE_CODE (objo) == POINTER_PLUS_EXPR)
3371 : : {
3372 : 576 : objo = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (TREE_TYPE (obj)),
3373 : : obj, NULL, jump_target);
3374 : 576 : if (objo)
3375 : 576 : obj = build_fold_addr_expr (objo);
3376 : : }
3377 : : }
3378 : :
3379 : : /* We expect OBJ to be in form of &d.D.2102 when HINT == 0,
3380 : : but when HINT is > 0, it can also be something like
3381 : : &d.D.2102 + 18446744073709551608, which includes the BINFO_OFFSET. */
3382 : 2311 : obj = extract_obj_from_addr_offset (obj);
3383 : 2311 : const tree objtype = TREE_TYPE (obj);
3384 : : /* If OBJ doesn't refer to a base field, we're done. */
3385 : 4595 : if (tree t = (TREE_CODE (obj) == COMPONENT_REF
3386 : 2311 : ? TREE_OPERAND (obj, 1) : obj))
3387 : 2311 : if (TREE_CODE (t) != FIELD_DECL || !DECL_FIELD_IS_BASE (t))
3388 : : {
3389 : 27 : if (fail_for_non_constant_p)
3390 : : {
3391 : 8 : if (!ctx->quiet)
3392 : : {
3393 : 2 : auto_diagnostic_group d;
3394 : 2 : error_at (loc, "reference %<dynamic_cast%> failed");
3395 : 2 : inform (loc, "dynamic type %qT of its operand does "
3396 : : "not have a base class of type %qT",
3397 : : objtype, type);
3398 : 2 : }
3399 : 8 : *non_constant_p = true;
3400 : : }
3401 : 27 : return integer_zero_node;
3402 : : }
3403 : :
3404 : : /* [class.cdtor] When a dynamic_cast is used in a constructor ...
3405 : : or in a destructor ... if the operand of the dynamic_cast refers
3406 : : to the object under construction or destruction, this object is
3407 : : considered to be a most derived object that has the type of the
3408 : : constructor or destructor's class. */
3409 : 2284 : tree vtable = build_vfield_ref (obj, objtype);
3410 : 2284 : vtable = cxx_eval_constant_expression (ctx, vtable, vc_prvalue,
3411 : : non_constant_p, overflow_p,
3412 : : jump_target);
3413 : 2284 : if (*non_constant_p)
3414 : : return call;
3415 : 2179 : if (*jump_target)
3416 : : return NULL_TREE;
3417 : : /* With -fsanitize=vptr, we initialize all vtable pointers to null,
3418 : : so it's possible that we got a null pointer now. */
3419 : 2179 : if (integer_zerop (vtable))
3420 : : {
3421 : 12 : if (!ctx->quiet)
3422 : 3 : error_at (loc, "virtual table pointer is used uninitialized");
3423 : 12 : *non_constant_p = true;
3424 : 12 : return integer_zero_node;
3425 : : }
3426 : : /* VTABLE will be &_ZTV1A + 16 or similar, get _ZTV1A. */
3427 : 2167 : vtable = extract_obj_from_addr_offset (vtable);
3428 : 2167 : const tree mdtype = DECL_CONTEXT (vtable);
3429 : :
3430 : : /* Given dynamic_cast<T>(v),
3431 : :
3432 : : [expr.dynamic.cast] If C is the class type to which T points or refers,
3433 : : the runtime check logically executes as follows:
3434 : :
3435 : : If, in the most derived object pointed (referred) to by v, v points
3436 : : (refers) to a public base class subobject of a C object, and if only
3437 : : one object of type C is derived from the subobject pointed (referred)
3438 : : to by v the result points (refers) to that C object.
3439 : :
3440 : : In this case, HINT >= 0 or -3. */
3441 : 2167 : if (hint >= 0 || hint == -3)
3442 : : {
3443 : : /* Look for a component with type TYPE. */
3444 : 657 : tree t = get_component_with_type (obj, type, mdtype);
3445 : : /* If not accessible, give an error. */
3446 : 657 : if (t == error_mark_node)
3447 : : {
3448 : 198 : if (fail_for_non_constant_p)
3449 : : {
3450 : 108 : if (!ctx->quiet)
3451 : : {
3452 : 12 : auto_diagnostic_group d;
3453 : 12 : error_at (loc, "reference %<dynamic_cast%> failed");
3454 : 12 : inform (loc, "static type %qT of its operand is a "
3455 : : "non-public base class of dynamic type %qT",
3456 : : objtype, type);
3457 : :
3458 : 12 : }
3459 : 108 : *non_constant_p = true;
3460 : : }
3461 : 198 : return integer_zero_node;
3462 : : }
3463 : 459 : else if (t)
3464 : : /* The result points to the TYPE object. */
3465 : 414 : return cp_build_addr_expr (t, complain);
3466 : : /* Else, TYPE was not found, because the HINT turned out to be wrong.
3467 : : Fall through to the normal processing. */
3468 : : }
3469 : :
3470 : : /* Otherwise, if v points (refers) to a public base class subobject of the
3471 : : most derived object, and the type of the most derived object has a base
3472 : : class, of type C, that is unambiguous and public, the result points
3473 : : (refers) to the C subobject of the most derived object.
3474 : :
3475 : : But it can also be an invalid case. */
3476 : :
3477 : : /* Get the most derived object. */
3478 : 1555 : obj = get_component_with_type (obj, mdtype, NULL_TREE);
3479 : 1555 : if (obj == error_mark_node)
3480 : : {
3481 : 806 : if (fail_for_non_constant_p)
3482 : : {
3483 : 350 : if (!ctx->quiet)
3484 : : {
3485 : 38 : auto_diagnostic_group d;
3486 : 38 : error_at (loc, "reference %<dynamic_cast%> failed");
3487 : 38 : inform (loc, "static type %qT of its operand is a non-public"
3488 : : " base class of dynamic type %qT", objtype, mdtype);
3489 : 38 : }
3490 : 350 : *non_constant_p = true;
3491 : : }
3492 : 806 : return integer_zero_node;
3493 : : }
3494 : : else
3495 : 749 : gcc_assert (obj);
3496 : :
3497 : : /* Check that the type of the most derived object has a base class
3498 : : of type TYPE that is unambiguous and public. */
3499 : 749 : base_kind b_kind;
3500 : 749 : tree binfo = lookup_base (mdtype, type, ba_check, &b_kind, tf_none);
3501 : 749 : if (!binfo || binfo == error_mark_node)
3502 : : {
3503 : 339 : if (fail_for_non_constant_p)
3504 : : {
3505 : 134 : if (!ctx->quiet)
3506 : : {
3507 : 16 : auto_diagnostic_group d;
3508 : 16 : error_at (loc, "reference %<dynamic_cast%> failed");
3509 : 16 : if (b_kind == bk_ambig)
3510 : 6 : inform (loc, "%qT is an ambiguous base class of dynamic "
3511 : : "type %qT of its operand", type, mdtype);
3512 : : else
3513 : 10 : inform (loc, "dynamic type %qT of its operand does not "
3514 : : "have an unambiguous public base class %qT",
3515 : : mdtype, type);
3516 : 16 : }
3517 : 134 : *non_constant_p = true;
3518 : : }
3519 : 339 : return integer_zero_node;
3520 : : }
3521 : : /* If so, return the TYPE subobject of the most derived object. */
3522 : 410 : obj = convert_to_base_statically (obj, binfo);
3523 : 410 : return cp_build_addr_expr (obj, complain);
3524 : : }
3525 : :
3526 : : /* Data structure used by replace_decl and replace_decl_r. */
3527 : :
3528 : : struct replace_decl_data
3529 : : {
3530 : : /* The _DECL we want to replace. */
3531 : : tree decl;
3532 : : /* The replacement for DECL. */
3533 : : tree replacement;
3534 : : /* Trees we've visited. */
3535 : : hash_set<tree> *pset;
3536 : : /* Whether we've performed any replacements. */
3537 : : bool changed;
3538 : : };
3539 : :
3540 : : /* Helper function for replace_decl, called through cp_walk_tree. */
3541 : :
3542 : : static tree
3543 : 5813345 : replace_decl_r (tree *tp, int *walk_subtrees, void *data)
3544 : : {
3545 : 5813345 : replace_decl_data *d = (replace_decl_data *) data;
3546 : :
3547 : : /* We could be replacing
3548 : : &<retval>.bar -> &foo.bar
3549 : : where foo is a static VAR_DECL, so we need to recompute TREE_CONSTANT
3550 : : on the ADDR_EXPR around it. */
3551 : 5813345 : if (TREE_CODE (*tp) == ADDR_EXPR)
3552 : : {
3553 : 610529 : d->pset->add (*tp);
3554 : 610529 : auto save_changed = d->changed;
3555 : 610529 : d->changed = false;
3556 : 610529 : cp_walk_tree (&TREE_OPERAND (*tp, 0), replace_decl_r, d, nullptr);
3557 : 610529 : if (d->changed)
3558 : : {
3559 : 326 : cxx_mark_addressable (*tp);
3560 : 326 : recompute_tree_invariant_for_addr_expr (*tp);
3561 : : }
3562 : : else
3563 : 610203 : d->changed = save_changed;
3564 : 610529 : *walk_subtrees = 0;
3565 : : }
3566 : 5202816 : else if (*tp == d->decl)
3567 : : {
3568 : 661 : *tp = unshare_expr (d->replacement);
3569 : 661 : d->changed = true;
3570 : 661 : *walk_subtrees = 0;
3571 : : }
3572 : 5202155 : else if (TYPE_P (*tp)
3573 : 5202155 : || d->pset->add (*tp))
3574 : 740905 : *walk_subtrees = 0;
3575 : :
3576 : 5813345 : return NULL_TREE;
3577 : : }
3578 : :
3579 : : /* Replace every occurrence of DECL with (an unshared copy of)
3580 : : REPLACEMENT within the expression *TP. Returns true iff a
3581 : : replacement was performed. */
3582 : :
3583 : : bool
3584 : 777561 : replace_decl (tree *tp, tree decl, tree replacement)
3585 : : {
3586 : 777561 : gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
3587 : : (TREE_TYPE (decl), TREE_TYPE (replacement)));
3588 : 777561 : hash_set<tree> pset;
3589 : 777561 : replace_decl_data data = { decl, replacement, &pset, false };
3590 : 777561 : cp_walk_tree (tp, replace_decl_r, &data, NULL);
3591 : 777561 : return data.changed;
3592 : 777561 : }
3593 : :
3594 : : /* Evaluate the call T to virtual function thunk THUNK_FNDECL. */
3595 : :
3596 : : static tree
3597 : 27 : cxx_eval_thunk_call (const constexpr_ctx *ctx, tree t, tree thunk_fndecl,
3598 : : value_cat lval,
3599 : : bool *non_constant_p, bool *overflow_p, tree *jump_target)
3600 : : {
3601 : 27 : tree function = THUNK_TARGET (thunk_fndecl);
3602 : :
3603 : 27 : if (THUNK_VIRTUAL_OFFSET (thunk_fndecl))
3604 : : {
3605 : 11 : if (!ctx->quiet)
3606 : : {
3607 : 3 : if (!DECL_DECLARED_CONSTEXPR_P (function))
3608 : : {
3609 : 0 : error ("call to non-%<constexpr%> function %qD", function);
3610 : 0 : explain_invalid_constexpr_fn (function);
3611 : : }
3612 : : else
3613 : : /* virtual_offset is only set for virtual bases, which make the
3614 : : class non-literal, so we don't need to handle it here. */
3615 : 3 : error ("calling constexpr member function %qD through virtual "
3616 : : "base subobject", function);
3617 : : }
3618 : 11 : *non_constant_p = true;
3619 : 11 : return t;
3620 : : }
3621 : :
3622 : 16 : tree new_call = copy_node (t);
3623 : 16 : CALL_EXPR_FN (new_call) = function;
3624 : 16 : TREE_TYPE (new_call) = TREE_TYPE (TREE_TYPE (function));
3625 : :
3626 : 16 : tree offset = size_int (THUNK_FIXED_OFFSET (thunk_fndecl));
3627 : :
3628 : 16 : if (DECL_THIS_THUNK_P (thunk_fndecl))
3629 : : {
3630 : : /* 'this'-adjusting thunk. */
3631 : 10 : tree this_arg = CALL_EXPR_ARG (t, 0);
3632 : 10 : this_arg = build2 (POINTER_PLUS_EXPR, TREE_TYPE (this_arg),
3633 : : this_arg, offset);
3634 : 10 : CALL_EXPR_ARG (new_call, 0) = this_arg;
3635 : : }
3636 : : else
3637 : : /* Return-adjusting thunk. */
3638 : 6 : new_call = build2 (POINTER_PLUS_EXPR, TREE_TYPE (new_call),
3639 : : new_call, offset);
3640 : :
3641 : 16 : return cxx_eval_constant_expression (ctx, new_call, lval,
3642 : : non_constant_p, overflow_p,
3643 : 16 : jump_target);
3644 : : }
3645 : :
3646 : : /* If OBJECT is of const class type, evaluate it to a CONSTRUCTOR and set
3647 : : its TREE_READONLY flag according to READONLY_P. Used for constexpr
3648 : : 'tors to detect modifying const objects in a constexpr context. */
3649 : :
3650 : : static void
3651 : 4228603 : cxx_set_object_constness (const constexpr_ctx *ctx, tree object,
3652 : : bool readonly_p, bool *non_constant_p,
3653 : : bool *overflow_p, tree *jump_target)
3654 : : {
3655 : 8457206 : if (CLASS_TYPE_P (TREE_TYPE (object))
3656 : 8457206 : && CP_TYPE_CONST_P (TREE_TYPE (object)))
3657 : : {
3658 : : /* Subobjects might not be stored in ctx->global->values but we
3659 : : can get its CONSTRUCTOR by evaluating *this. */
3660 : 538670 : tree e = cxx_eval_constant_expression (ctx, object, vc_prvalue,
3661 : : non_constant_p, overflow_p,
3662 : : jump_target);
3663 : 538670 : if (!*non_constant_p
3664 : 4756582 : && !throws (jump_target)
3665 : 1066649 : && TREE_CODE (e) == CONSTRUCTOR)
3666 : 527979 : TREE_READONLY (e) = readonly_p;
3667 : : }
3668 : 4228603 : }
3669 : :
3670 : : /* Subroutine of cxx_eval_constant_expression.
3671 : : Evaluate the call expression tree T in the context of OLD_CALL expression
3672 : : evaluation. */
3673 : :
3674 : : static tree
3675 : 87998367 : cxx_eval_call_expression (const constexpr_ctx *ctx, tree t,
3676 : : value_cat lval,
3677 : : bool *non_constant_p, bool *overflow_p,
3678 : : tree *jump_target)
3679 : : {
3680 : 87998367 : location_t loc = cp_expr_loc_or_input_loc (t);
3681 : 87998367 : tree fun = get_function_named_in_call (t);
3682 : :
3683 : 87998367 : if (fun == NULL_TREE)
3684 : 332622 : return cxx_eval_internal_function (ctx, t, lval,
3685 : : non_constant_p, overflow_p,
3686 : 332622 : jump_target);
3687 : :
3688 : 87665745 : if (TREE_CODE (fun) != FUNCTION_DECL)
3689 : : {
3690 : : /* Might be a constexpr function pointer. */
3691 : 121341 : fun = cxx_eval_constant_expression (ctx, fun, vc_prvalue,
3692 : : non_constant_p, overflow_p,
3693 : : jump_target);
3694 : 121341 : if (*jump_target)
3695 : : return NULL_TREE;
3696 : 121341 : STRIP_NOPS (fun);
3697 : 121341 : if (TREE_CODE (fun) == ADDR_EXPR)
3698 : 19927 : fun = TREE_OPERAND (fun, 0);
3699 : : /* For TARGET_VTABLE_USES_DESCRIPTORS targets, there is no
3700 : : indirection, the called expression is a pointer into the
3701 : : virtual table which should contain FDESC_EXPR. Extract the
3702 : : FUNCTION_DECL from there. */
3703 : : else if (TARGET_VTABLE_USES_DESCRIPTORS
3704 : : && TREE_CODE (fun) == POINTER_PLUS_EXPR
3705 : : && TREE_CODE (TREE_OPERAND (fun, 0)) == ADDR_EXPR
3706 : : && TREE_CODE (TREE_OPERAND (fun, 1)) == INTEGER_CST)
3707 : : {
3708 : : tree d = TREE_OPERAND (TREE_OPERAND (fun, 0), 0);
3709 : : if (VAR_P (d)
3710 : : && DECL_VTABLE_OR_VTT_P (d)
3711 : : && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE
3712 : : && TREE_TYPE (TREE_TYPE (d)) == vtable_entry_type
3713 : : && DECL_INITIAL (d)
3714 : : && TREE_CODE (DECL_INITIAL (d)) == CONSTRUCTOR)
3715 : : {
3716 : : tree i = int_const_binop (TRUNC_DIV_EXPR, TREE_OPERAND (fun, 1),
3717 : : TYPE_SIZE_UNIT (vtable_entry_type));
3718 : : HOST_WIDE_INT idx = find_array_ctor_elt (DECL_INITIAL (d), i);
3719 : : if (idx >= 0)
3720 : : {
3721 : : tree fdesc
3722 : : = (*CONSTRUCTOR_ELTS (DECL_INITIAL (d)))[idx].value;
3723 : : if (TREE_CODE (fdesc) == FDESC_EXPR
3724 : : && integer_zerop (TREE_OPERAND (fdesc, 1)))
3725 : : fun = TREE_OPERAND (fdesc, 0);
3726 : : }
3727 : : }
3728 : : }
3729 : : }
3730 : 87665745 : if (TREE_CODE (fun) != FUNCTION_DECL)
3731 : : {
3732 : 101414 : if (!ctx->quiet && !*non_constant_p)
3733 : 0 : error_at (loc, "expression %qE does not designate a %<constexpr%> "
3734 : : "function", fun);
3735 : 101414 : *non_constant_p = true;
3736 : 101414 : return t;
3737 : : }
3738 : 87564331 : tree orig_fun = fun;
3739 : 87564331 : if (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun))
3740 : 9467498 : fun = DECL_CLONED_FUNCTION (fun);
3741 : :
3742 : 87564331 : if (is_ubsan_builtin_p (fun))
3743 : 58 : return void_node;
3744 : :
3745 : 87564273 : if (fndecl_built_in_p (fun))
3746 : 10986601 : return cxx_eval_builtin_function_call (ctx, t, fun,
3747 : : lval, non_constant_p, overflow_p,
3748 : 10986600 : jump_target);
3749 : 76577672 : if (DECL_THUNK_P (fun))
3750 : 27 : return cxx_eval_thunk_call (ctx, t, fun, lval, non_constant_p, overflow_p,
3751 : 27 : jump_target);
3752 : 76577645 : bool non_constexpr_call = false;
3753 : 76577645 : if (!maybe_constexpr_fn (fun))
3754 : : {
3755 : 232381 : if (TREE_CODE (t) == CALL_EXPR
3756 : 218429 : && cxx_replaceable_global_alloc_fn (fun)
3757 : 313029 : && (CALL_FROM_NEW_OR_DELETE_P (t)
3758 : 28321 : || is_std_allocator_allocate (ctx->call)))
3759 : : {
3760 : 60530 : const bool new_op_p = IDENTIFIER_NEW_OP_P (DECL_NAME (fun));
3761 : 60530 : const int nargs = call_expr_nargs (t);
3762 : 60530 : tree arg0 = NULL_TREE;
3763 : 98685 : for (int i = 0; i < nargs; ++i)
3764 : : {
3765 : 61066 : tree arg = CALL_EXPR_ARG (t, i);
3766 : 61066 : arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
3767 : : non_constant_p, overflow_p,
3768 : : jump_target);
3769 : 61066 : if (*jump_target)
3770 : : return NULL_TREE;
3771 : : /* Deleting a non-constant pointer has a better error message
3772 : : below. */
3773 : 61058 : if (new_op_p || i != 0)
3774 : 56395 : VERIFY_CONSTANT (arg);
3775 : 33492 : if (i == 0)
3776 : : arg0 = arg;
3777 : : }
3778 : 37619 : gcc_assert (arg0);
3779 : 37619 : if (new_op_p)
3780 : : {
3781 : 32956 : if (!tree_fits_uhwi_p (arg0))
3782 : : {
3783 : : /* We should not get here; the VERIFY_CONSTANT above
3784 : : should have already caught it. */
3785 : 0 : gcc_checking_assert (false);
3786 : : if (!ctx->quiet)
3787 : : error_at (loc, "cannot allocate array: size not constant");
3788 : : *non_constant_p = true;
3789 : : return t;
3790 : : }
3791 : 65912 : tree type = build_array_type_nelts (char_type_node,
3792 : 32956 : tree_to_uhwi (arg0));
3793 : 32956 : tree var = build_decl (loc, VAR_DECL,
3794 : 32956 : (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
3795 : : & OVL_OP_FLAG_VEC)
3796 : : ? heap_vec_uninit_identifier
3797 : : : heap_uninit_identifier,
3798 : 32956 : type);
3799 : 32956 : DECL_ARTIFICIAL (var) = 1;
3800 : 32956 : ctx->global->heap_vars.safe_push (var);
3801 : 32956 : ctx->global->put_value (var, NULL_TREE);
3802 : 32956 : return fold_convert (ptr_type_node, build_address (var));
3803 : : }
3804 : : else
3805 : : {
3806 : 4663 : STRIP_NOPS (arg0);
3807 : 4663 : if (TREE_CODE (arg0) == ADDR_EXPR
3808 : 4663 : && VAR_P (TREE_OPERAND (arg0, 0)))
3809 : : {
3810 : 4663 : tree var = TREE_OPERAND (arg0, 0);
3811 : 4663 : if (DECL_NAME (var) == heap_uninit_identifier
3812 : 4663 : || DECL_NAME (var) == heap_identifier)
3813 : : {
3814 : 4497 : if (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
3815 : : & OVL_OP_FLAG_VEC)
3816 : : {
3817 : 9 : if (!ctx->quiet)
3818 : : {
3819 : 3 : auto_diagnostic_group d;
3820 : 3 : error_at (loc, "array deallocation of object "
3821 : : "allocated with non-array "
3822 : : "allocation");
3823 : 3 : inform (DECL_SOURCE_LOCATION (var),
3824 : : "allocation performed here");
3825 : 3 : }
3826 : 9 : *non_constant_p = true;
3827 : 9 : return t;
3828 : : }
3829 : 4488 : DECL_NAME (var) = heap_deleted_identifier;
3830 : 4488 : ctx->global->destroy_value (var);
3831 : 4488 : ctx->global->heap_dealloc_count++;
3832 : 4488 : return void_node;
3833 : : }
3834 : 166 : else if (DECL_NAME (var) == heap_vec_uninit_identifier
3835 : 166 : || DECL_NAME (var) == heap_vec_identifier)
3836 : : {
3837 : 142 : if ((IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
3838 : : & OVL_OP_FLAG_VEC) == 0)
3839 : : {
3840 : 9 : if (!ctx->quiet)
3841 : : {
3842 : 3 : auto_diagnostic_group d;
3843 : 3 : error_at (loc, "non-array deallocation of "
3844 : : "object allocated with array "
3845 : : "allocation");
3846 : 3 : inform (DECL_SOURCE_LOCATION (var),
3847 : : "allocation performed here");
3848 : 3 : }
3849 : 9 : *non_constant_p = true;
3850 : 9 : return t;
3851 : : }
3852 : 133 : DECL_NAME (var) = heap_deleted_identifier;
3853 : 133 : ctx->global->destroy_value (var);
3854 : 133 : ctx->global->heap_dealloc_count++;
3855 : 133 : return void_node;
3856 : : }
3857 : 24 : else if (DECL_NAME (var) == heap_deleted_identifier)
3858 : : {
3859 : 15 : if (!ctx->quiet)
3860 : 6 : error_at (loc, "deallocation of already deallocated "
3861 : : "storage");
3862 : 15 : *non_constant_p = true;
3863 : 15 : return t;
3864 : : }
3865 : : }
3866 : 9 : if (!ctx->quiet)
3867 : 3 : error_at (loc, "deallocation of storage that was "
3868 : : "not previously allocated");
3869 : 9 : *non_constant_p = true;
3870 : 9 : return t;
3871 : : }
3872 : : }
3873 : : /* Allow placement new in std::construct_at, just return the second
3874 : : argument. */
3875 : 171851 : if (TREE_CODE (t) == CALL_EXPR
3876 : 157899 : && cxx_placement_new_fn (fun)
3877 : 217101 : && is_std_construct_at (ctx->call))
3878 : : {
3879 : 28937 : const int nargs = call_expr_nargs (t);
3880 : 28937 : tree arg1 = NULL_TREE;
3881 : 86811 : for (int i = 0; i < nargs; ++i)
3882 : : {
3883 : 57874 : tree arg = CALL_EXPR_ARG (t, i);
3884 : 57874 : arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
3885 : : non_constant_p, overflow_p,
3886 : : jump_target);
3887 : 57874 : if (*jump_target)
3888 : : return NULL_TREE;
3889 : 57874 : if (i == 1)
3890 : : arg1 = arg;
3891 : : else
3892 : 57874 : VERIFY_CONSTANT (arg);
3893 : : }
3894 : 28937 : gcc_assert (arg1);
3895 : : return arg1;
3896 : : }
3897 : 142914 : else if (cxx_dynamic_cast_fn_p (fun))
3898 : 2402 : return cxx_eval_dynamic_cast_fn (ctx, t, non_constant_p, overflow_p,
3899 : 2402 : jump_target);
3900 : 140512 : else if (enum cxa_builtin kind = cxx_cxa_builtin_fn_p (fun))
3901 : 27263 : return cxx_eval_cxa_builtin_fn (ctx, t, kind, fun,
3902 : : non_constant_p, overflow_p,
3903 : 27263 : jump_target);
3904 : :
3905 : : /* Calls to non-constexpr functions can be diagnosed right away
3906 : : before C++26, though in C++26 evaluation of the arguments might
3907 : : throw and if caught it could be still constant expression.
3908 : : So for C++26 this is diagnosed only after
3909 : : cxx_bind_parameters_in_call. */
3910 : 113249 : if (cxx_dialect >= cxx26)
3911 : : non_constexpr_call = true;
3912 : : else
3913 : : {
3914 : 30833 : if (!ctx->quiet)
3915 : : {
3916 : 188 : if (!lambda_static_thunk_p (fun))
3917 : 188 : error_at (loc, "call to non-%<constexpr%> function %qD", fun);
3918 : 188 : explain_invalid_constexpr_fn (fun);
3919 : : }
3920 : 30833 : *non_constant_p = true;
3921 : 30833 : return t;
3922 : : }
3923 : : }
3924 : :
3925 : 76427680 : constexpr_ctx new_ctx = *ctx;
3926 : 76427680 : ctx = &new_ctx;
3927 : 85666976 : if (DECL_CONSTRUCTOR_P (fun) && !ctx->object
3928 : 77654200 : && TREE_CODE (t) == AGGR_INIT_EXPR)
3929 : : {
3930 : : /* We want to have an initialization target for an AGGR_INIT_EXPR.
3931 : : If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
3932 : 640 : new_ctx.object = AGGR_INIT_EXPR_SLOT (t);
3933 : 640 : tree ctor = new_ctx.ctor = build_constructor (DECL_CONTEXT (fun), NULL);
3934 : 640 : CONSTRUCTOR_NO_CLEARING (ctor) = true;
3935 : 640 : ctx->global->put_value (new_ctx.object, ctor);
3936 : : }
3937 : : /* An immediate invocation is manifestly constant evaluated including the
3938 : : arguments of the call, so use mce_true even for the argument
3939 : : evaluation. */
3940 : 152855360 : if (DECL_IMMEDIATE_FUNCTION_P (fun))
3941 : 1229357 : new_ctx.manifestly_const_eval = mce_true;
3942 : :
3943 : : /* We used to shortcut trivial constructor/op= here, but nowadays
3944 : : we can only get a trivial function here with -fno-elide-constructors. */
3945 : 76427680 : gcc_checking_assert (!trivial_fn_p (fun)
3946 : : || !flag_elide_constructors
3947 : : /* Or it's a call from maybe_thunk_body (111075). */
3948 : : || (TREE_CODE (t) == CALL_EXPR ? CALL_FROM_THUNK_P (t)
3949 : : : AGGR_INIT_FROM_THUNK_P (t))
3950 : : /* We don't elide constructors when processing
3951 : : a noexcept-expression. */
3952 : : || cp_noexcept_operand);
3953 : :
3954 : 76427680 : bool non_constant_args = false;
3955 : 76427680 : constexpr_call new_call;
3956 : 76427680 : new_call.bindings
3957 : 76427680 : = cxx_bind_parameters_in_call (ctx, t, fun, orig_fun, non_constant_p,
3958 : : overflow_p, &non_constant_args,
3959 : : jump_target);
3960 : :
3961 : : /* We build up the bindings list before we know whether we already have this
3962 : : call cached. If we don't end up saving these bindings, ggc_free them when
3963 : : this function exits. */
3964 : 76427680 : class free_bindings
3965 : : {
3966 : : tree *bindings;
3967 : : public:
3968 : 76427680 : free_bindings (tree &b): bindings (&b) { }
3969 : 76427678 : ~free_bindings () { if (bindings) ggc_free (*bindings); }
3970 : 3035638 : void preserve () { bindings = NULL; }
3971 : 76427680 : } fb (new_call.bindings);
3972 : :
3973 : 76427680 : if (*jump_target)
3974 : : return NULL_TREE;
3975 : 76427671 : if (*non_constant_p)
3976 : : return t;
3977 : 43020838 : if (non_constexpr_call)
3978 : : {
3979 : 7036 : if (!ctx->quiet)
3980 : : {
3981 : 207 : if (!lambda_static_thunk_p (fun))
3982 : 207 : error_at (loc, "call to non-%<constexpr%> function %qD", fun);
3983 : 207 : explain_invalid_constexpr_fn (fun);
3984 : : }
3985 : 7036 : *non_constant_p = true;
3986 : 7036 : return t;
3987 : : }
3988 : :
3989 : : /* We can't defer instantiating the function any longer. */
3990 : 43013802 : if (!DECL_INITIAL (fun)
3991 : 1684913 : && (DECL_TEMPLOID_INSTANTIATION (fun) || DECL_DEFAULTED_FN (fun))
3992 : 43013802 : && !uid_sensitive_constexpr_evaluation_p ())
3993 : : {
3994 : 1509927 : location_t save_loc = input_location;
3995 : 1509927 : input_location = loc;
3996 : 1509927 : ++function_depth;
3997 : 1509927 : if (ctx->manifestly_const_eval == mce_true)
3998 : 196071 : FNDECL_MANIFESTLY_CONST_EVALUATED (fun) = true;
3999 : 1509927 : if (DECL_TEMPLOID_INSTANTIATION (fun))
4000 : 1509919 : instantiate_decl (fun, /*defer_ok*/false, /*expl_inst*/false);
4001 : : else
4002 : 8 : synthesize_method (fun);
4003 : 1509927 : --function_depth;
4004 : 1509927 : input_location = save_loc;
4005 : : }
4006 : :
4007 : : /* If in direct recursive call, optimize definition search. */
4008 : 43013802 : if (ctx && ctx->call && ctx->call->fundef && ctx->call->fundef->decl == fun)
4009 : 27534 : new_call.fundef = ctx->call->fundef;
4010 : : else
4011 : : {
4012 : 42986268 : new_call.fundef = retrieve_constexpr_fundef (fun);
4013 : 42986268 : if (new_call.fundef == NULL || new_call.fundef->body == NULL
4014 : 42718692 : || new_call.fundef->result == error_mark_node
4015 : 42699518 : || fun == current_function_decl)
4016 : : {
4017 : 286753 : if (!ctx->quiet)
4018 : : {
4019 : : /* We need to check for current_function_decl here in case we're
4020 : : being called during cp_fold_function, because at that point
4021 : : DECL_INITIAL is set properly and we have a fundef but we
4022 : : haven't lowered invisirefs yet (c++/70344). */
4023 : 157 : if (DECL_INITIAL (fun) == error_mark_node
4024 : 157 : || fun == current_function_decl)
4025 : 15 : error_at (loc, "%qD called in a constant expression before its "
4026 : : "definition is complete", fun);
4027 : 142 : else if (DECL_INITIAL (fun))
4028 : : {
4029 : : /* The definition of fun was somehow unsuitable. But pretend
4030 : : that lambda static thunks don't exist. */
4031 : 105 : if (!lambda_static_thunk_p (fun))
4032 : 103 : error_at (loc, "%qD called in a constant expression", fun);
4033 : 105 : explain_invalid_constexpr_fn (fun);
4034 : : }
4035 : : else
4036 : 37 : error_at (loc, "%qD used before its definition", fun);
4037 : : }
4038 : 286753 : *non_constant_p = true;
4039 : 286753 : return t;
4040 : : }
4041 : : }
4042 : :
4043 : : /* Don't complain about problems evaluating an ill-formed function. */
4044 : 42727049 : if (function *f = DECL_STRUCT_FUNCTION (fun))
4045 : 42727049 : if (f->language->erroneous)
4046 : 549 : new_ctx.quiet = true;
4047 : :
4048 : 42727049 : int depth_ok = push_cx_call_context (t);
4049 : :
4050 : : /* Remember the object we are constructing or destructing. */
4051 : 42727049 : tree new_obj = NULL_TREE;
4052 : 85454098 : if (DECL_CONSTRUCTOR_P (fun) || DECL_DESTRUCTOR_P (fun))
4053 : : {
4054 : : /* In a cdtor, it should be the first `this' argument.
4055 : : At this point it has already been evaluated in the call
4056 : : to cxx_bind_parameters_in_call. */
4057 : 5066101 : new_obj = TREE_VEC_ELT (new_call.bindings, 0);
4058 : 5066101 : bool empty_base = false;
4059 : 5066101 : new_obj = cxx_fold_indirect_ref (ctx, loc, DECL_CONTEXT (fun), new_obj,
4060 : : &empty_base, jump_target);
4061 : 5066101 : if (*jump_target)
4062 : 0 : return NULL_TREE;
4063 : : /* If we're initializing an empty class, don't set constness, because
4064 : : cxx_fold_indirect_ref will return the wrong object to set constness
4065 : : of. */
4066 : 5066101 : if (empty_base)
4067 : : new_obj = NULL_TREE;
4068 : 2245177 : else if (ctx->call && ctx->call->fundef
4069 : 8745590 : && DECL_CONSTRUCTOR_P (ctx->call->fundef->decl))
4070 : : {
4071 : 1375222 : tree cur_obj = TREE_VEC_ELT (ctx->call->bindings, 0);
4072 : 1375222 : STRIP_NOPS (cur_obj);
4073 : 1375222 : if (TREE_CODE (cur_obj) == ADDR_EXPR)
4074 : 1373975 : cur_obj = TREE_OPERAND (cur_obj, 0);
4075 : 1375222 : if (new_obj == cur_obj)
4076 : : /* We're calling the target constructor of a delegating
4077 : : constructor, or accessing a base subobject through a
4078 : : NOP_EXPR as part of a call to a base constructor, so
4079 : : there is no new (sub)object. */
4080 : 837498 : new_obj = NULL_TREE;
4081 : : }
4082 : : }
4083 : :
4084 : 42727049 : tree result = NULL_TREE;
4085 : :
4086 : 42727049 : constexpr_call *entry = NULL;
4087 : 42727049 : if (depth_ok && !non_constant_args && ctx->strict)
4088 : : {
4089 : 19744135 : new_call.hash = constexpr_fundef_hasher::hash (new_call.fundef);
4090 : 19744135 : new_call.hash
4091 : 19744135 : = iterative_hash_template_arg (new_call.bindings, new_call.hash);
4092 : :
4093 : : /* If we have seen this call before, we are done. */
4094 : 19744135 : maybe_initialize_constexpr_call_table ();
4095 : 19744135 : bool insert = depth_ok < constexpr_cache_depth;
4096 : 19744135 : constexpr_call **slot
4097 : 20293920 : = constexpr_call_table->find_slot (&new_call,
4098 : : insert ? INSERT : NO_INSERT);
4099 : 19744135 : entry = slot ? *slot : NULL;
4100 : 19523897 : if (entry == NULL)
4101 : : {
4102 : : /* Only cache up to constexpr_cache_depth to limit memory use. */
4103 : 3255876 : if (insert)
4104 : : {
4105 : : /* We need to keep a pointer to the entry, not just the slot, as
4106 : : the slot can move during evaluation of the body. */
4107 : 3035638 : *slot = entry = ggc_alloc<constexpr_call> ();
4108 : 3035638 : *entry = new_call;
4109 : 3035638 : entry->result (ctx->manifestly_const_eval) = unknown_type_node;
4110 : 3035638 : fb.preserve ();
4111 : : }
4112 : : }
4113 : : /* Calls that are in progress have their result set to unknown_type_node,
4114 : : so that we can detect circular dependencies. Now that we only cache
4115 : : up to constexpr_cache_depth this won't catch circular dependencies that
4116 : : start deeper, but they'll hit the recursion or ops limit. */
4117 : 16488259 : else if (entry->result (ctx->manifestly_const_eval) == unknown_type_node)
4118 : : {
4119 : 6 : if (!ctx->quiet)
4120 : 0 : error ("call has circular dependency");
4121 : 6 : *non_constant_p = true;
4122 : 6 : entry->result (ctx->manifestly_const_eval) = result = error_mark_node;
4123 : : }
4124 : : else
4125 : 16488253 : result = entry->result (ctx->manifestly_const_eval);
4126 : : }
4127 : :
4128 : 42727049 : if (!depth_ok)
4129 : : {
4130 : 30 : if (!ctx->quiet)
4131 : 3 : error ("%<constexpr%> evaluation depth exceeds maximum of %d (use "
4132 : : "%<-fconstexpr-depth=%> to increase the maximum)",
4133 : : max_constexpr_depth);
4134 : 30 : *non_constant_p = true;
4135 : 30 : result = error_mark_node;
4136 : : }
4137 : : else
4138 : : {
4139 : 42727019 : bool cacheable = !!entry;
4140 : 42727019 : if (result && result != error_mark_node)
4141 : : /* OK */;
4142 : 29369887 : else if (!DECL_SAVED_TREE (fun))
4143 : : {
4144 : : /* When at_eof >= 3, cgraph has started throwing away
4145 : : DECL_SAVED_TREE, so fail quietly. FIXME we get here because of
4146 : : late code generation for VEC_INIT_EXPR, which needs to be
4147 : : completely reconsidered. */
4148 : 0 : gcc_assert (at_eof >= 3 && ctx->quiet);
4149 : 0 : *non_constant_p = true;
4150 : : }
4151 : 29369887 : else if (tree copy = get_fundef_copy (new_call.fundef))
4152 : : {
4153 : 29369887 : tree body, parms, res;
4154 : 29369887 : releasing_vec ctors;
4155 : :
4156 : : /* Reuse or create a new unshared copy of this function's body. */
4157 : 29369887 : body = TREE_PURPOSE (copy);
4158 : 29369887 : parms = TREE_VALUE (copy);
4159 : 29369887 : res = TREE_TYPE (copy);
4160 : :
4161 : : /* Associate the bindings with the remapped parms. */
4162 : 29369887 : tree bound = new_call.bindings;
4163 : 29369887 : tree remapped = parms;
4164 : 64796570 : for (int i = 0; i < TREE_VEC_LENGTH (bound); ++i)
4165 : : {
4166 : 35426683 : tree arg = TREE_VEC_ELT (bound, i);
4167 : 35426683 : if (entry)
4168 : : {
4169 : : /* Unshare args going into the hash table to separate them
4170 : : from the caller's context, for better GC and to avoid
4171 : : problems with verify_gimple. */
4172 : 1990235 : arg = unshare_expr_without_location (arg);
4173 : 1990235 : TREE_VEC_ELT (bound, i) = arg;
4174 : :
4175 : : /* And then unshare again so the callee doesn't change the
4176 : : argument values in the hash table. XXX Could we unshare
4177 : : lazily in cxx_eval_store_expression? */
4178 : 1990235 : arg = unshare_constructor (arg);
4179 : 1990235 : if (TREE_CODE (arg) == CONSTRUCTOR)
4180 : 744414 : vec_safe_push (ctors, arg);
4181 : : }
4182 : 35426683 : ctx->global->put_value (remapped, arg);
4183 : 35426683 : remapped = DECL_CHAIN (remapped);
4184 : : }
4185 : 29369887 : if (remapped)
4186 : : {
4187 : : /* We shouldn't have any parms without args, but fail gracefully
4188 : : in error recovery. */
4189 : 6 : gcc_checking_assert (seen_error ());
4190 : 6 : *non_constant_p = true;
4191 : : }
4192 : : /* Add the RESULT_DECL to the values map, too. */
4193 : 29369887 : gcc_assert (!DECL_BY_REFERENCE (res));
4194 : 29369887 : ctx->global->put_value (res, NULL_TREE);
4195 : :
4196 : : /* Remember the current call we're evaluating. */
4197 : 29369887 : constexpr_ctx call_ctx = *ctx;
4198 : 29369887 : call_ctx.call = &new_call;
4199 : 29369887 : unsigned save_heap_alloc_count = ctx->global->heap_vars.length ();
4200 : 29369887 : unsigned save_heap_dealloc_count = ctx->global->heap_dealloc_count;
4201 : :
4202 : : /* Make sure we fold std::is_constant_evaluated to true in an
4203 : : immediate function. */
4204 : 58739774 : if (DECL_IMMEDIATE_FUNCTION_P (fun))
4205 : 640465 : call_ctx.manifestly_const_eval = mce_true;
4206 : :
4207 : : /* If this is a constexpr destructor, the object's const and volatile
4208 : : semantics are no longer in effect; see [class.dtor]p5. */
4209 : 33598490 : if (new_obj && DECL_DESTRUCTOR_P (fun))
4210 : 208633 : cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/false,
4211 : : non_constant_p, overflow_p, jump_target);
4212 : :
4213 : : /* If this is a constructor, we are beginning the lifetime of the
4214 : : object we are initializing. */
4215 : 29369887 : if (new_obj
4216 : 8457206 : && DECL_CONSTRUCTOR_P (fun)
4217 : 4019970 : && TREE_CODE (new_obj) == COMPONENT_REF
4218 : 30385495 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (new_obj, 0))) == UNION_TYPE)
4219 : : {
4220 : 2989 : tree ctor = build_constructor (TREE_TYPE (new_obj), NULL);
4221 : 2989 : CONSTRUCTOR_NO_CLEARING (ctor) = true;
4222 : 2989 : tree activate = build2 (INIT_EXPR, TREE_TYPE (new_obj),
4223 : : new_obj, ctor);
4224 : 2989 : cxx_eval_constant_expression (ctx, activate,
4225 : : lval, non_constant_p, overflow_p,
4226 : : jump_target);
4227 : 2989 : ggc_free (activate);
4228 : 2989 : if (*jump_target)
4229 : 0 : return NULL_TREE;
4230 : : }
4231 : :
4232 : 29369887 : tree jmp_target = NULL_TREE;
4233 : 29369887 : cxx_eval_constant_expression (&call_ctx, body,
4234 : : vc_discard, non_constant_p, overflow_p,
4235 : : &jmp_target);
4236 : :
4237 : 29369885 : if (!*non_constant_p && throws (&jmp_target))
4238 : : {
4239 : 292 : result = NULL_TREE;
4240 : 292 : cacheable = false;
4241 : 292 : *jump_target = jmp_target;
4242 : : }
4243 : 58739186 : else if (DECL_CONSTRUCTOR_P (fun))
4244 : : /* This can be null for a subobject constructor call, in
4245 : : which case what we care about is the initialization
4246 : : side-effects rather than the value. We could get at the
4247 : : value by evaluating *this, but we don't bother; there's
4248 : : no need to put such a call in the hash table. */
4249 : 4851956 : result = lval ? ctx->object : ctx->ctor;
4250 : 24517637 : else if (VOID_TYPE_P (TREE_TYPE (res)))
4251 : 1521023 : result = void_node;
4252 : : else
4253 : : {
4254 : 22996614 : result = ctx->global->get_value (res);
4255 : 8004832 : if (result == NULL_TREE && !*non_constant_p
4256 : 22996804 : && !DECL_DESTRUCTOR_P (fun))
4257 : : {
4258 : 95 : if (!ctx->quiet)
4259 : 6 : error ("%<constexpr%> call flows off the end "
4260 : : "of the function");
4261 : 95 : *non_constant_p = true;
4262 : : }
4263 : : }
4264 : :
4265 : : /* At this point, the object's constructor will have run, so
4266 : : the object is no longer under construction, and its possible
4267 : : 'const' semantics now apply. Make a note of this fact by
4268 : : marking the CONSTRUCTOR TREE_READONLY. */
4269 : 33598488 : if (new_obj && DECL_CONSTRUCTOR_P (fun))
4270 : 4019970 : cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/true,
4271 : : non_constant_p, overflow_p, jump_target);
4272 : :
4273 : : /* Remove the parms/result from the values map. */
4274 : 29369885 : destroy_value_checked (ctx, res, non_constant_p);
4275 : 64796573 : for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
4276 : 35426688 : destroy_value_checked (ctx, parm, non_constant_p);
4277 : :
4278 : : /* Free any parameter CONSTRUCTORs we aren't returning directly. */
4279 : 30114299 : while (!ctors->is_empty ())
4280 : : {
4281 : 744414 : tree c = ctors->pop ();
4282 : 744414 : if (c != result)
4283 : 744414 : free_constructor (c);
4284 : : }
4285 : :
4286 : : /* Make the unshared function copy we used available for re-use. */
4287 : 29369885 : save_fundef_copy (fun, copy);
4288 : :
4289 : : /* If the call allocated some heap object that hasn't been
4290 : : deallocated during the call, or if it deallocated some heap
4291 : : object it has not allocated, the call isn't really stateless
4292 : : for the constexpr evaluation and should not be cached.
4293 : : It is fine if the call allocates something and deallocates it
4294 : : too. */
4295 : 29369885 : if (cacheable
4296 : 35536424 : && (save_heap_alloc_count != ctx->global->heap_vars.length ()
4297 : 6164966 : || (save_heap_dealloc_count
4298 : 6164966 : != ctx->global->heap_dealloc_count)))
4299 : : {
4300 : 1573 : tree heap_var;
4301 : 1573 : unsigned int i;
4302 : 1573 : if ((ctx->global->heap_vars.length ()
4303 : 1573 : - ctx->global->heap_dealloc_count)
4304 : 1573 : != save_heap_alloc_count - save_heap_dealloc_count)
4305 : : cacheable = false;
4306 : : else
4307 : 11921 : FOR_EACH_VEC_ELT_FROM (ctx->global->heap_vars, i, heap_var,
4308 : : save_heap_alloc_count)
4309 : 10570 : if (DECL_NAME (heap_var) != heap_deleted_identifier)
4310 : : {
4311 : : cacheable = false;
4312 : : break;
4313 : : }
4314 : : }
4315 : :
4316 : : /* Rewrite all occurrences of the function's RESULT_DECL with the
4317 : : current object under construction. */
4318 : 29369885 : if (!*non_constant_p
4319 : 19628638 : && ctx->object
4320 : 6160282 : && CLASS_TYPE_P (TREE_TYPE (res))
4321 : 30844342 : && !is_empty_class (TREE_TYPE (res)))
4322 : : {
4323 : 777389 : if (!same_type_ignoring_top_level_qualifiers_p
4324 : 777389 : (TREE_TYPE (res), TREE_TYPE (ctx->object)))
4325 : 0 : *non_constant_p = true;
4326 : 777389 : else if (replace_decl (&result, res, ctx->object))
4327 : : {
4328 : 238 : cacheable = false;
4329 : 238 : result = cxx_eval_constant_expression (ctx, result, lval,
4330 : : non_constant_p,
4331 : : overflow_p,
4332 : : jump_target);
4333 : 238 : if (*jump_target)
4334 : : {
4335 : 0 : cacheable = false;
4336 : 0 : result = NULL_TREE;
4337 : : }
4338 : : }
4339 : : }
4340 : :
4341 : : /* Only cache a permitted result of a constant expression. */
4342 : 29369647 : if (cacheable && !reduced_constant_expression_p (result))
4343 : : cacheable = false;
4344 : 29369885 : }
4345 : : else
4346 : : /* Couldn't get a function copy to evaluate. */
4347 : 0 : *non_constant_p = true;
4348 : :
4349 : 42727017 : if (result == error_mark_node)
4350 : 0 : *non_constant_p = true;
4351 : 42727017 : if (*non_constant_p || *overflow_p)
4352 : 9741403 : result = error_mark_node;
4353 : 32985614 : else if (!result)
4354 : 1225178 : result = void_node;
4355 : 42727017 : if (entry)
4356 : : {
4357 : 39047792 : entry->result (ctx->manifestly_const_eval)
4358 : 19523896 : = cacheable ? result : error_mark_node;
4359 : :
4360 : 19523896 : if (result != error_mark_node
4361 : 16464556 : && ctx->manifestly_const_eval == mce_unknown)
4362 : : {
4363 : : /* Evaluation succeeded and was independent of whether we're in a
4364 : : manifestly constant-evaluated context, so we can also reuse
4365 : : this result when evaluating this call with a fixed context. */
4366 : 3414808 : if (!entry->result (mce_true))
4367 : 631270 : entry->result (mce_true) = entry->result (mce_unknown);
4368 : 3414808 : if (!entry->result (mce_false))
4369 : 619199 : entry->result (mce_false) = entry->result (mce_unknown);
4370 : : }
4371 : : }
4372 : : }
4373 : :
4374 : : /* The result of a constexpr function must be completely initialized.
4375 : :
4376 : : However, in C++20, a constexpr constructor doesn't necessarily have
4377 : : to initialize all the fields, so we don't clear CONSTRUCTOR_NO_CLEARING
4378 : : in order to detect reading an unitialized object in constexpr instead
4379 : : of value-initializing it. (reduced_constant_expression_p is expected to
4380 : : take care of clearing the flag.) */
4381 : 42727047 : if (TREE_CODE (result) == CONSTRUCTOR
4382 : 42727047 : && (cxx_dialect < cxx20
4383 : 7921464 : || !DECL_CONSTRUCTOR_P (fun)))
4384 : 3877866 : clear_no_implicit_zero (result);
4385 : :
4386 : 42727047 : pop_cx_call_context ();
4387 : 42727047 : return result;
4388 : 76427678 : }
4389 : :
4390 : : /* Return true if T is a valid constant initializer. If a CONSTRUCTOR
4391 : : initializes all the members, the CONSTRUCTOR_NO_CLEARING flag will be
4392 : : cleared. If called recursively on a FIELD_DECL's CONSTRUCTOR, SZ
4393 : : is DECL_SIZE of the FIELD_DECL, otherwise NULL.
4394 : : FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
4395 : :
4396 : : bool
4397 : 637663185 : reduced_constant_expression_p (tree t, tree sz /* = NULL_TREE */)
4398 : : {
4399 : 637663185 : if (t == NULL_TREE)
4400 : : return false;
4401 : :
4402 : 634605397 : switch (TREE_CODE (t))
4403 : : {
4404 : : case PTRMEM_CST:
4405 : : /* Even if we can't lower this yet, it's constant. */
4406 : : return true;
4407 : :
4408 : : case OMP_DECLARE_MAPPER:
4409 : : return true;
4410 : :
4411 : 43627995 : case CONSTRUCTOR:
4412 : : /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
4413 : 43627995 : tree field;
4414 : 43627995 : if (!AGGREGATE_TYPE_P (TREE_TYPE (t)))
4415 : : /* A constant vector would be folded to VECTOR_CST.
4416 : : A CONSTRUCTOR of scalar type means uninitialized. */
4417 : : return false;
4418 : 43612690 : if (CONSTRUCTOR_NO_CLEARING (t))
4419 : : {
4420 : 1351267 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
4421 : : {
4422 : : /* There must be a valid constant initializer at every array
4423 : : index. */
4424 : 1520 : tree min = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (t)));
4425 : 1520 : tree max = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (t)));
4426 : 1520 : tree cursor = min;
4427 : 24669 : for (auto &e: CONSTRUCTOR_ELTS (t))
4428 : : {
4429 : 20219 : if (!reduced_constant_expression_p (e.value))
4430 : : return false;
4431 : 20169 : if (array_index_cmp (cursor, e.index) != 0)
4432 : : return false;
4433 : 20109 : if (TREE_CODE (e.index) == RANGE_EXPR)
4434 : 0 : cursor = TREE_OPERAND (e.index, 1);
4435 : 20109 : if (TREE_CODE (e.value) == RAW_DATA_CST)
4436 : 0 : cursor
4437 : 0 : = int_const_binop (PLUS_EXPR, cursor,
4438 : 0 : size_int (RAW_DATA_LENGTH (e.value)));
4439 : : else
4440 : 20109 : cursor = int_const_binop (PLUS_EXPR, cursor,
4441 : 20109 : size_one_node);
4442 : : }
4443 : 1410 : if (find_array_ctor_elt (t, max) == -1)
4444 : : return false;
4445 : 1324 : goto ok;
4446 : : }
4447 : 1349747 : else if (cxx_dialect >= cxx20
4448 : 1349747 : && TREE_CODE (TREE_TYPE (t)) == UNION_TYPE)
4449 : : {
4450 : 4271154 : if (CONSTRUCTOR_NELTS (t) == 0)
4451 : : /* An initialized union has a constructor element. */
4452 : : return false;
4453 : : /* And it only initializes one member. */
4454 : : field = NULL_TREE;
4455 : : }
4456 : : else
4457 : 1347954 : field = next_subobject_field (TYPE_FIELDS (TREE_TYPE (t)));
4458 : : }
4459 : : else
4460 : : field = NULL_TREE;
4461 : 282312758 : for (auto &e: CONSTRUCTOR_ELTS (t))
4462 : : {
4463 : : /* If VAL is null, we're in the middle of initializing this
4464 : : element. */
4465 : 181307570 : if (!reduced_constant_expression_p (e.value,
4466 : 181307570 : (e.index
4467 : 181307570 : && (TREE_CODE (e.index)
4468 : : == FIELD_DECL))
4469 : 56206844 : ? DECL_SIZE (e.index)
4470 : : : NULL_TREE))
4471 : : return false;
4472 : : /* We want to remove initializers for empty fields in a struct to
4473 : : avoid confusing output_constructor. */
4474 : 180752792 : if (is_empty_field (e.index)
4475 : 180752792 : && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE)
4476 : : return false;
4477 : : /* Check for non-empty fields between initialized fields when
4478 : : CONSTRUCTOR_NO_CLEARING. */
4479 : 180219877 : for (; field && e.index != field;
4480 : 106035 : field = next_subobject_field (DECL_CHAIN (field)))
4481 : 106278 : if (!is_really_empty_class (TREE_TYPE (field),
4482 : : /*ignore_vptr*/false))
4483 : : return false;
4484 : 180113599 : if (field)
4485 : 1491706 : field = next_subobject_field (DECL_CHAIN (field));
4486 : : }
4487 : : /* There could be a non-empty field at the end. */
4488 : 42495362 : for (; field; field = next_subobject_field (DECL_CHAIN (field)))
4489 : 82309 : if (!is_really_empty_class (TREE_TYPE (field), /*ignore_vptr*/false))
4490 : : {
4491 : : /* Ignore FIELD_DECLs with bit positions beyond DECL_SIZE of
4492 : : the parent FIELD_DECL (if any) for classes with virtual
4493 : : bases. */
4494 : 2353 : if (cxx_dialect >= cxx26
4495 : 1433 : && sz
4496 : 2732 : && tree_int_cst_le (sz, bit_position (field)))
4497 : : break;
4498 : 2101 : return false;
4499 : : }
4500 : 42413053 : ok:
4501 : 42414629 : if (CONSTRUCTOR_NO_CLEARING (t))
4502 : : /* All the fields are initialized. */
4503 : 1220419 : CONSTRUCTOR_NO_CLEARING (t) = false;
4504 : : return true;
4505 : :
4506 : 590943049 : default:
4507 : : /* FIXME are we calling this too much? */
4508 : 590943049 : return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE;
4509 : : }
4510 : : }
4511 : :
4512 : : /* *TP was not deemed constant by reduced_constant_expression_p. Explain
4513 : : why and suggest what could be done about it. */
4514 : :
4515 : : static tree
4516 : 952 : verify_constant_explain_r (tree *tp, int *walk_subtrees, void *)
4517 : : {
4518 : 952 : bool ref_p = false;
4519 : :
4520 : : /* No need to look into types or unevaluated operands. */
4521 : 952 : if (TYPE_P (*tp) || unevaluated_p (TREE_CODE (*tp)))
4522 : : {
4523 : 0 : *walk_subtrees = false;
4524 : 0 : return NULL_TREE;
4525 : : }
4526 : :
4527 : 952 : switch (TREE_CODE (*tp))
4528 : : {
4529 : 70 : CASE_CONVERT:
4530 : 70 : if (TREE_CODE (TREE_OPERAND (*tp, 0)) != ADDR_EXPR)
4531 : : break;
4532 : 51 : ref_p = TYPE_REF_P (TREE_TYPE (*tp));
4533 : 51 : *tp = TREE_OPERAND (*tp, 0);
4534 : 183 : gcc_fallthrough ();
4535 : 183 : case ADDR_EXPR:
4536 : 183 : {
4537 : 183 : tree op = TREE_OPERAND (*tp, 0);
4538 : 183 : if (VAR_P (op)
4539 : 114 : && DECL_DECLARED_CONSTEXPR_P (op)
4540 : 42 : && !TREE_STATIC (op)
4541 : : /* ??? We should also say something about temporaries. */
4542 : 222 : && !DECL_ARTIFICIAL (op))
4543 : : {
4544 : 36 : if (ref_p)
4545 : 15 : inform (location_of (*tp), "reference to %qD is not a constant "
4546 : : "expression", op);
4547 : : else
4548 : 21 : inform (location_of (*tp), "pointer to %qD is not a constant "
4549 : : "expression", op);
4550 : 36 : const location_t op_loc = DECL_SOURCE_LOCATION (op);
4551 : 36 : rich_location richloc (line_table, op_loc);
4552 : 36 : richloc.add_fixit_insert_before (op_loc, "static ");
4553 : 36 : inform (&richloc,
4554 : : "address of non-static constexpr variable %qD may differ on "
4555 : : "each invocation of the enclosing function; add %<static%> "
4556 : : "to give it a constant address", op);
4557 : 36 : }
4558 : : break;
4559 : : }
4560 : : default:
4561 : : break;
4562 : : }
4563 : :
4564 : : return NULL_TREE;
4565 : : }
4566 : :
4567 : : /* Some expressions may have constant operands but are not constant
4568 : : themselves, such as 1/0. Call this function to check for that
4569 : : condition.
4570 : :
4571 : : We only call this in places that require an arithmetic constant, not in
4572 : : places where we might have a non-constant expression that can be a
4573 : : component of a constant expression, such as the address of a constexpr
4574 : : variable that might be dereferenced later. */
4575 : :
4576 : : static bool
4577 : 393078697 : verify_constant (tree t, bool allow_non_constant, bool *non_constant_p,
4578 : : bool *overflow_p)
4579 : : {
4580 : 384659201 : if (!*non_constant_p && !reduced_constant_expression_p (t)
4581 : 395285463 : && t != void_node)
4582 : : {
4583 : 2206691 : if (!allow_non_constant)
4584 : : {
4585 : 256 : auto_diagnostic_group d;
4586 : 360 : error_at (cp_expr_loc_or_input_loc (t),
4587 : : "%q+E is not a constant expression", t);
4588 : 256 : cp_walk_tree_without_duplicates (&t, verify_constant_explain_r,
4589 : : nullptr);
4590 : 256 : }
4591 : 2206691 : *non_constant_p = true;
4592 : : }
4593 : 393078697 : if (TREE_OVERFLOW_P (t))
4594 : : {
4595 : 678 : if (!allow_non_constant)
4596 : : {
4597 : 155 : permerror (input_location, "overflow in constant expression");
4598 : : /* If we're being permissive (and are in an enforcing
4599 : : context), ignore the overflow. */
4600 : 155 : if (flag_permissive)
4601 : 75 : return *non_constant_p;
4602 : : }
4603 : 603 : *overflow_p = true;
4604 : : }
4605 : 393078622 : return *non_constant_p;
4606 : : }
4607 : :
4608 : : /* Check whether the shift operation with code CODE and type TYPE on LHS
4609 : : and RHS is undefined. If it is, give an error with an explanation,
4610 : : and return true; return false otherwise. */
4611 : :
4612 : : static bool
4613 : 33672601 : cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx,
4614 : : enum tree_code code, tree type, tree lhs, tree rhs)
4615 : : {
4616 : 33672601 : if ((code != LSHIFT_EXPR && code != RSHIFT_EXPR)
4617 : 3057449 : || TREE_CODE (lhs) != INTEGER_CST
4618 : 3057449 : || TREE_CODE (rhs) != INTEGER_CST)
4619 : : return false;
4620 : :
4621 : 3057449 : tree lhstype = TREE_TYPE (lhs);
4622 : 3057449 : unsigned HOST_WIDE_INT uprec = TYPE_PRECISION (TREE_TYPE (lhs));
4623 : :
4624 : : /* [expr.shift] The behavior is undefined if the right operand
4625 : : is negative, or greater than or equal to the length in bits
4626 : : of the promoted left operand. */
4627 : 3057449 : if (tree_int_cst_sgn (rhs) == -1)
4628 : : {
4629 : 126 : if (!ctx->quiet)
4630 : 35 : permerror (loc, "right operand of shift expression %q+E is negative",
4631 : : build2_loc (loc, code, type, lhs, rhs));
4632 : 126 : return (!flag_permissive || ctx->quiet);
4633 : : }
4634 : 3057323 : if (compare_tree_int (rhs, uprec) >= 0)
4635 : : {
4636 : 270 : if (!ctx->quiet)
4637 : 34 : permerror (loc, "right operand of shift expression %q+E is greater "
4638 : : "than or equal to the precision %wu of the left operand",
4639 : : build2_loc (loc, code, type, lhs, rhs), uprec);
4640 : 270 : return (!flag_permissive || ctx->quiet);
4641 : : }
4642 : :
4643 : : /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
4644 : : if E1 has a signed type and non-negative value, and E1x2^E2 is
4645 : : representable in the corresponding unsigned type of the result type,
4646 : : then that value, converted to the result type, is the resulting value;
4647 : : otherwise, the behavior is undefined.
4648 : : For C++20:
4649 : : The value of E1 << E2 is the unique value congruent to E1 x 2^E2 modulo
4650 : : 2^N, where N is the range exponent of the type of the result. */
4651 : 3057053 : if (code == LSHIFT_EXPR
4652 : 2930164 : && !TYPE_OVERFLOW_WRAPS (lhstype)
4653 : 1971768 : && cxx_dialect >= cxx11
4654 : 5012752 : && cxx_dialect < cxx20)
4655 : : {
4656 : 1442396 : if (tree_int_cst_sgn (lhs) == -1)
4657 : : {
4658 : 128 : if (!ctx->quiet)
4659 : 23 : permerror (loc,
4660 : : "left operand of shift expression %q+E is negative",
4661 : : build2_loc (loc, code, type, lhs, rhs));
4662 : 128 : return (!flag_permissive || ctx->quiet);
4663 : : }
4664 : : /* For signed x << y the following:
4665 : : (unsigned) x >> ((prec (lhs) - 1) - y)
4666 : : if > 1, is undefined. The right-hand side of this formula
4667 : : is the highest bit of the LHS that can be set (starting from 0),
4668 : : so that the shift doesn't overflow. We then right-shift the LHS
4669 : : to see whether any other bit is set making the original shift
4670 : : undefined -- the result is not representable in the corresponding
4671 : : unsigned type. */
4672 : 1442268 : tree t = build_int_cst (unsigned_type_node, uprec - 1);
4673 : 1442268 : t = fold_build2 (MINUS_EXPR, unsigned_type_node, t, rhs);
4674 : 1442268 : tree ulhs = fold_convert (unsigned_type_for (lhstype), lhs);
4675 : 1442268 : t = fold_build2 (RSHIFT_EXPR, TREE_TYPE (ulhs), ulhs, t);
4676 : 1442268 : if (tree_int_cst_lt (integer_one_node, t))
4677 : : {
4678 : 86 : if (!ctx->quiet)
4679 : 8 : permerror (loc, "shift expression %q+E overflows",
4680 : : build2_loc (loc, code, type, lhs, rhs));
4681 : 86 : return (!flag_permissive || ctx->quiet);
4682 : : }
4683 : : }
4684 : : return false;
4685 : : }
4686 : :
4687 : : /* Subroutine of cxx_eval_constant_expression.
4688 : : Attempt to reduce the unary expression tree T to a compile time value.
4689 : : If successful, return the value. Otherwise issue a diagnostic
4690 : : and return error_mark_node. */
4691 : :
4692 : : static tree
4693 : 20710305 : cxx_eval_unary_expression (const constexpr_ctx *ctx, tree t,
4694 : : bool /*lval*/,
4695 : : bool *non_constant_p, bool *overflow_p,
4696 : : tree *jump_target)
4697 : : {
4698 : 20710305 : tree r;
4699 : 20710305 : tree orig_arg = TREE_OPERAND (t, 0);
4700 : 20710305 : tree arg = cxx_eval_constant_expression (ctx, orig_arg, vc_prvalue,
4701 : : non_constant_p, overflow_p,
4702 : : jump_target);
4703 : 20710304 : if (*jump_target)
4704 : : return NULL_TREE;
4705 : 20710303 : VERIFY_CONSTANT (arg);
4706 : 17132107 : location_t loc = EXPR_LOCATION (t);
4707 : 17132107 : enum tree_code code = TREE_CODE (t);
4708 : 17132107 : tree type = TREE_TYPE (t);
4709 : 17132107 : r = fold_unary_loc (loc, code, type, arg);
4710 : 17132107 : if (r == NULL_TREE)
4711 : : {
4712 : 17 : if (arg == orig_arg)
4713 : : r = t;
4714 : : else
4715 : 13 : r = build1_loc (loc, code, type, arg);
4716 : : }
4717 : 17132107 : VERIFY_CONSTANT (r);
4718 : : return r;
4719 : : }
4720 : :
4721 : : /* Helper function for cxx_eval_binary_expression. Try to optimize
4722 : : original POINTER_PLUS_EXPR T, LHS p+ RHS, return NULL_TREE if the
4723 : : generic folding should be used. */
4724 : :
4725 : : static tree
4726 : 3213466 : cxx_fold_pointer_plus_expression (const constexpr_ctx *ctx, tree t,
4727 : : tree lhs, tree rhs, bool *non_constant_p,
4728 : : bool *overflow_p, tree *jump_target)
4729 : : {
4730 : 3213466 : STRIP_NOPS (lhs);
4731 : 3213466 : if (TREE_CODE (lhs) != ADDR_EXPR)
4732 : : return NULL_TREE;
4733 : :
4734 : 2868781 : lhs = TREE_OPERAND (lhs, 0);
4735 : :
4736 : : /* &A[i] p+ j => &A[i + j] */
4737 : 2868781 : if (TREE_CODE (lhs) == ARRAY_REF
4738 : 6858 : && TREE_CODE (TREE_OPERAND (lhs, 1)) == INTEGER_CST
4739 : 6858 : && TREE_CODE (rhs) == INTEGER_CST
4740 : 6858 : && TYPE_SIZE_UNIT (TREE_TYPE (lhs))
4741 : 2875639 : && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs))) == INTEGER_CST)
4742 : : {
4743 : 6858 : tree orig_type = TREE_TYPE (t);
4744 : 6858 : location_t loc = EXPR_LOCATION (t);
4745 : 6858 : tree type = TREE_TYPE (lhs);
4746 : :
4747 : 6858 : t = fold_convert_loc (loc, ssizetype, TREE_OPERAND (lhs, 1));
4748 : 6858 : tree nelts = array_type_nelts_top (TREE_TYPE (TREE_OPERAND (lhs, 0)));
4749 : 6858 : nelts = cxx_eval_constant_expression (ctx, nelts, vc_prvalue,
4750 : : non_constant_p, overflow_p,
4751 : : jump_target);
4752 : 6858 : if (*non_constant_p)
4753 : : return NULL_TREE;
4754 : 6846 : if (*jump_target)
4755 : : return NULL_TREE;
4756 : : /* Don't fold an out-of-bound access. */
4757 : 6846 : if (!tree_int_cst_le (t, nelts))
4758 : : return NULL_TREE;
4759 : 6846 : rhs = cp_fold_convert (ssizetype, rhs);
4760 : : /* Don't fold if rhs can't be divided exactly by TYPE_SIZE_UNIT.
4761 : : constexpr int A[1]; ... (char *)&A[0] + 1 */
4762 : 6846 : if (!integer_zerop (fold_build2_loc (loc, TRUNC_MOD_EXPR, sizetype,
4763 : 6846 : rhs, TYPE_SIZE_UNIT (type))))
4764 : : return NULL_TREE;
4765 : : /* Make sure to treat the second operand of POINTER_PLUS_EXPR
4766 : : as signed. */
4767 : 6814 : rhs = fold_build2_loc (loc, EXACT_DIV_EXPR, ssizetype, rhs,
4768 : 6814 : TYPE_SIZE_UNIT (type));
4769 : 6814 : t = size_binop_loc (loc, PLUS_EXPR, rhs, t);
4770 : 6814 : t = build4_loc (loc, ARRAY_REF, type, TREE_OPERAND (lhs, 0),
4771 : : t, NULL_TREE, NULL_TREE);
4772 : 6814 : t = cp_build_addr_expr (t, tf_warning_or_error);
4773 : 6814 : t = cp_fold_convert (orig_type, t);
4774 : 6814 : return cxx_eval_constant_expression (ctx, t, vc_prvalue,
4775 : : non_constant_p, overflow_p,
4776 : 6814 : jump_target);
4777 : : }
4778 : :
4779 : : return NULL_TREE;
4780 : : }
4781 : :
4782 : : /* Try to fold expressions like
4783 : : (struct S *) (&a[0].D.2378 + 12)
4784 : : into
4785 : : &MEM <struct T> [(void *)&a + 12B]
4786 : : This is something normally done by gimple_fold_stmt_to_constant_1
4787 : : on GIMPLE, but is undesirable on GENERIC if we are e.g. going to
4788 : : dereference the address because some details are lost.
4789 : : For pointer comparisons we want such folding though so that
4790 : : match.pd address_compare optimization works. */
4791 : :
4792 : : static tree
4793 : 2625138 : cxx_maybe_fold_addr_pointer_plus (tree t)
4794 : : {
4795 : 5250276 : while (CONVERT_EXPR_P (t)
4796 : 2883821 : && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0))))
4797 : 258683 : t = TREE_OPERAND (t, 0);
4798 : 2625138 : if (TREE_CODE (t) != POINTER_PLUS_EXPR)
4799 : : return NULL_TREE;
4800 : 2126447 : tree op0 = TREE_OPERAND (t, 0);
4801 : 2126447 : tree op1 = TREE_OPERAND (t, 1);
4802 : 2126447 : if (TREE_CODE (op1) != INTEGER_CST)
4803 : : return NULL_TREE;
4804 : 2126447 : while (CONVERT_EXPR_P (op0)
4805 : 4250748 : && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
4806 : 2124301 : op0 = TREE_OPERAND (op0, 0);
4807 : 2126447 : if (TREE_CODE (op0) != ADDR_EXPR)
4808 : : return NULL_TREE;
4809 : 2126447 : op1 = fold_convert (ptr_type_node, op1);
4810 : 2126447 : tree r = fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (op0)), op0, op1);
4811 : 2126447 : return build1_loc (EXPR_LOCATION (t), ADDR_EXPR, TREE_TYPE (op0), r);
4812 : : }
4813 : :
4814 : : /* Subroutine of cxx_eval_constant_expression.
4815 : : Like cxx_eval_unary_expression, except for binary expressions. */
4816 : :
4817 : : static tree
4818 : 50343663 : cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t,
4819 : : value_cat lval,
4820 : : bool *non_constant_p, bool *overflow_p,
4821 : : tree *jump_target)
4822 : : {
4823 : 50343663 : tree r = NULL_TREE;
4824 : 50343663 : tree orig_lhs = TREE_OPERAND (t, 0);
4825 : 50343663 : tree orig_rhs = TREE_OPERAND (t, 1);
4826 : 50343663 : tree lhs, rhs;
4827 : 50343663 : lhs = cxx_eval_constant_expression (ctx, orig_lhs, vc_prvalue,
4828 : : non_constant_p, overflow_p,
4829 : : jump_target);
4830 : : /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer
4831 : : subtraction. */
4832 : 50343662 : if (*non_constant_p)
4833 : : return t;
4834 : 37228195 : if (*jump_target)
4835 : : return NULL_TREE;
4836 : 37228149 : rhs = cxx_eval_constant_expression (ctx, orig_rhs, vc_prvalue,
4837 : : non_constant_p, overflow_p,
4838 : : jump_target);
4839 : 37228149 : if (*non_constant_p)
4840 : : return t;
4841 : 36296801 : if (*jump_target)
4842 : : return NULL_TREE;
4843 : :
4844 : 36296796 : location_t loc = EXPR_LOCATION (t);
4845 : 36296796 : enum tree_code code = TREE_CODE (t);
4846 : 36296796 : tree type = TREE_TYPE (t);
4847 : :
4848 : 36296796 : if (code == EQ_EXPR || code == NE_EXPR)
4849 : : {
4850 : 4618925 : bool is_code_eq = (code == EQ_EXPR);
4851 : :
4852 : 4618925 : if (TREE_CODE (lhs) == PTRMEM_CST
4853 : 181 : && TREE_CODE (rhs) == PTRMEM_CST)
4854 : : {
4855 : 54 : tree lmem = PTRMEM_CST_MEMBER (lhs);
4856 : 54 : tree rmem = PTRMEM_CST_MEMBER (rhs);
4857 : 54 : bool eq;
4858 : 54 : if (TREE_CODE (lmem) == TREE_CODE (rmem)
4859 : 54 : && TREE_CODE (lmem) == FIELD_DECL
4860 : 54 : && TREE_CODE (DECL_CONTEXT (lmem)) == UNION_TYPE
4861 : 81 : && same_type_p (DECL_CONTEXT (lmem),
4862 : : DECL_CONTEXT (rmem)))
4863 : : /* If both refer to (possibly different) members of the same union
4864 : : (12.3), they compare equal. */
4865 : : eq = true;
4866 : : else
4867 : 27 : eq = cp_tree_equal (lhs, rhs);
4868 : 54 : r = constant_boolean_node (eq == is_code_eq, type);
4869 : 54 : }
4870 : 4618871 : else if ((TREE_CODE (lhs) == PTRMEM_CST
4871 : 4618744 : || TREE_CODE (rhs) == PTRMEM_CST)
4872 : 4618871 : && (null_member_pointer_value_p (lhs)
4873 : 127 : || null_member_pointer_value_p (rhs)))
4874 : 127 : r = constant_boolean_node (!is_code_eq, type);
4875 : 4618744 : else if (TREE_CODE (lhs) == PTRMEM_CST)
4876 : 0 : lhs = cplus_expand_constant (lhs);
4877 : 4618744 : else if (TREE_CODE (rhs) == PTRMEM_CST)
4878 : 0 : rhs = cplus_expand_constant (rhs);
4879 : : }
4880 : 0 : if (r == NULL_TREE
4881 : 36296615 : && TREE_CODE_CLASS (code) == tcc_comparison
4882 : 12780017 : && POINTER_TYPE_P (TREE_TYPE (lhs)))
4883 : : {
4884 : 1312569 : if (tree lhso = cxx_maybe_fold_addr_pointer_plus (lhs))
4885 : 1031433 : lhs = fold_convert (TREE_TYPE (lhs), lhso);
4886 : 1312569 : if (tree rhso = cxx_maybe_fold_addr_pointer_plus (rhs))
4887 : 1095014 : rhs = fold_convert (TREE_TYPE (rhs), rhso);
4888 : : }
4889 : 3213621 : if (code == POINTER_PLUS_EXPR && !*non_constant_p
4890 : 39510417 : && integer_zerop (lhs) && !integer_zerop (rhs))
4891 : : {
4892 : 155 : if (!ctx->quiet)
4893 : 45 : error ("arithmetic involving a null pointer in %qE", lhs);
4894 : 155 : *non_constant_p = true;
4895 : 155 : return t;
4896 : : }
4897 : 36296641 : else if (code == POINTER_PLUS_EXPR)
4898 : : {
4899 : 3213466 : r = cxx_fold_pointer_plus_expression (ctx, t, lhs, rhs, non_constant_p,
4900 : : overflow_p, jump_target);
4901 : 3213466 : if (*jump_target)
4902 : : return NULL_TREE;
4903 : : }
4904 : 33083175 : else if (code == SPACESHIP_EXPR)
4905 : : {
4906 : 23685 : r = genericize_spaceship (loc, type, lhs, rhs);
4907 : 23685 : return cxx_eval_constant_expression (ctx, r, lval, non_constant_p,
4908 : 23685 : overflow_p, jump_target);
4909 : : }
4910 : :
4911 : 36272956 : if (r == NULL_TREE)
4912 : : {
4913 : 36265961 : if (ctx->manifestly_const_eval == mce_true
4914 : 21723193 : && (flag_constexpr_fp_except
4915 : 21723190 : || TREE_CODE (type) != REAL_TYPE))
4916 : : {
4917 : 21710169 : auto ofcc = make_temp_override (folding_cxx_constexpr, true);
4918 : 21710169 : r = fold_binary_initializer_loc (loc, code, type, lhs, rhs);
4919 : 21710169 : }
4920 : : else
4921 : 14555792 : r = fold_binary_loc (loc, code, type, lhs, rhs);
4922 : : }
4923 : :
4924 : 36265961 : if (r == NULL_TREE
4925 : 2600453 : && (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
4926 : 100 : && TREE_CODE (lhs) == INTEGER_CST
4927 : 98 : && TREE_CODE (rhs) == INTEGER_CST
4928 : 38873409 : && wi::neg_p (wi::to_wide (rhs)))
4929 : : {
4930 : : /* For diagnostics and -fpermissive emulate previous behavior of
4931 : : handling shifts by negative amount. */
4932 : 98 : tree nrhs = const_unop (NEGATE_EXPR, TREE_TYPE (rhs), rhs);
4933 : 98 : if (nrhs)
4934 : 119 : r = fold_binary_loc (loc,
4935 : : code == LSHIFT_EXPR ? RSHIFT_EXPR : LSHIFT_EXPR,
4936 : : type, lhs, nrhs);
4937 : : }
4938 : :
4939 : 36272956 : if (r == NULL_TREE)
4940 : : {
4941 : 2600355 : if (lhs == orig_lhs && rhs == orig_rhs)
4942 : : r = t;
4943 : : else
4944 : 796217 : r = build2_loc (loc, code, type, lhs, rhs);
4945 : : }
4946 : 33672601 : else if (cxx_eval_check_shift_p (loc, ctx, code, type, lhs, rhs))
4947 : 600 : *non_constant_p = true;
4948 : : /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
4949 : : a local array in a constexpr function. */
4950 : 36272956 : bool ptr = INDIRECT_TYPE_P (TREE_TYPE (lhs));
4951 : 31600935 : if (!ptr)
4952 : 31600935 : VERIFY_CONSTANT (r);
4953 : : return r;
4954 : : }
4955 : :
4956 : : /* Subroutine of cxx_eval_constant_expression.
4957 : : Attempt to evaluate condition expressions. */
4958 : :
4959 : : static tree
4960 : 9208599 : cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t,
4961 : : value_cat lval,
4962 : : bool *non_constant_p, bool *overflow_p,
4963 : : tree *jump_target)
4964 : : {
4965 : 9208599 : tree val = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
4966 : : vc_prvalue,
4967 : : non_constant_p, overflow_p,
4968 : : jump_target);
4969 : 9208599 : if (*jump_target)
4970 : : return NULL_TREE;
4971 : 9208590 : VERIFY_CONSTANT (val);
4972 : 7480617 : if (TREE_CODE (t) == IF_STMT && IF_STMT_CONSTEVAL_P (t))
4973 : : {
4974 : : /* Evaluate the condition as if it was
4975 : : if (__builtin_is_constant_evaluated ()), i.e. defer it if not
4976 : : ctx->manifestly_const_eval (as sometimes we try to constant evaluate
4977 : : without manifestly_const_eval even expressions or parts thereof which
4978 : : will later be manifestly const_eval evaluated), otherwise fold it to
4979 : : true. */
4980 : 1091124 : if (ctx->manifestly_const_eval == mce_unknown)
4981 : : {
4982 : 1081664 : *non_constant_p = true;
4983 : 1081664 : return t;
4984 : : }
4985 : 9460 : val = constant_boolean_node (ctx->manifestly_const_eval == mce_true,
4986 : : boolean_type_node);
4987 : : }
4988 : : /* Don't VERIFY_CONSTANT the other operands. */
4989 : 6398953 : const bool zero_p = integer_zerop (val);
4990 : 6398953 : if (zero_p)
4991 : 3657006 : val = TREE_OPERAND (t, 2);
4992 : : else
4993 : 2741947 : val = TREE_OPERAND (t, 1);
4994 : 6398953 : if (TREE_CODE (t) == IF_STMT && !val)
4995 : 1775825 : val = void_node;
4996 : :
4997 : : /* P2564: If we aren't in immediate function context (including a manifestly
4998 : : constant-evaluated expression), check any uses of immediate functions in
4999 : : the arm we're discarding. But don't do this inside a call; we already
5000 : : checked when parsing the function. */
5001 : 6398953 : if (ctx->manifestly_const_eval != mce_true
5002 : 1848321 : && !in_immediate_context ()
5003 : 1779422 : && !ctx->call
5004 : 6992145 : && cp_fold_immediate (&TREE_OPERAND (t, zero_p ? 1 : 2),
5005 : 593192 : ctx->manifestly_const_eval))
5006 : : {
5007 : 7 : *non_constant_p = true;
5008 : 7 : return t;
5009 : : }
5010 : :
5011 : : /* A TARGET_EXPR may be nested inside another TARGET_EXPR, but still
5012 : : serve as the initializer for the same object as the outer TARGET_EXPR,
5013 : : as in
5014 : : A a = true ? A{} : A{};
5015 : : so strip the inner TARGET_EXPR so we don't materialize a temporary. */
5016 : 6398946 : if (TREE_CODE (val) == TARGET_EXPR)
5017 : 764 : val = TARGET_EXPR_INITIAL (val);
5018 : 6398946 : return cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
5019 : 6398946 : overflow_p, jump_target);
5020 : : }
5021 : :
5022 : : /* Subroutine of cxx_eval_constant_expression.
5023 : : Attempt to evaluate vector condition expressions. Unlike
5024 : : cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal
5025 : : ternary arithmetics operation, where all 3 arguments have to be
5026 : : evaluated as constants and then folding computes the result from
5027 : : them. */
5028 : :
5029 : : static tree
5030 : 866 : cxx_eval_vector_conditional_expression (const constexpr_ctx *ctx, tree t,
5031 : : bool *non_constant_p, bool *overflow_p,
5032 : : tree *jump_target)
5033 : : {
5034 : 866 : tree arg1 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
5035 : : vc_prvalue,
5036 : : non_constant_p, overflow_p,
5037 : : jump_target);
5038 : 866 : if (*jump_target)
5039 : : return NULL_TREE;
5040 : 866 : VERIFY_CONSTANT (arg1);
5041 : 657 : tree arg2 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
5042 : : vc_prvalue,
5043 : : non_constant_p, overflow_p,
5044 : : jump_target);
5045 : 657 : if (*jump_target)
5046 : : return NULL_TREE;
5047 : 657 : VERIFY_CONSTANT (arg2);
5048 : 657 : tree arg3 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2),
5049 : : vc_prvalue,
5050 : : non_constant_p, overflow_p,
5051 : : jump_target);
5052 : 657 : if (*jump_target)
5053 : : return NULL_TREE;
5054 : 657 : VERIFY_CONSTANT (arg3);
5055 : 657 : location_t loc = EXPR_LOCATION (t);
5056 : 657 : tree type = TREE_TYPE (t);
5057 : 657 : tree r = fold_ternary_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
5058 : 657 : if (r == NULL_TREE)
5059 : : {
5060 : 0 : if (arg1 == TREE_OPERAND (t, 0)
5061 : 0 : && arg2 == TREE_OPERAND (t, 1)
5062 : 0 : && arg3 == TREE_OPERAND (t, 2))
5063 : : r = t;
5064 : : else
5065 : 0 : r = build3_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
5066 : : }
5067 : 657 : VERIFY_CONSTANT (r);
5068 : : return r;
5069 : : }
5070 : :
5071 : : /* Returns less than, equal to, or greater than zero if KEY is found to be
5072 : : less than, to match, or to be greater than the constructor_elt's INDEX. */
5073 : :
5074 : : static int
5075 : 96928 : array_index_cmp (tree key, tree index)
5076 : : {
5077 : 96928 : gcc_assert (TREE_CODE (key) == INTEGER_CST);
5078 : :
5079 : 96928 : switch (TREE_CODE (index))
5080 : : {
5081 : 94059 : case INTEGER_CST:
5082 : 94059 : return tree_int_cst_compare (key, index);
5083 : 2869 : case RANGE_EXPR:
5084 : 2869 : {
5085 : 2869 : tree lo = TREE_OPERAND (index, 0);
5086 : 2869 : tree hi = TREE_OPERAND (index, 1);
5087 : 2869 : if (tree_int_cst_lt (key, lo))
5088 : : return -1;
5089 : 2583 : else if (tree_int_cst_lt (hi, key))
5090 : : return 1;
5091 : : else
5092 : 2583 : return 0;
5093 : : }
5094 : 0 : default:
5095 : 0 : gcc_unreachable ();
5096 : : }
5097 : : }
5098 : :
5099 : : /* Extract a single INTEGER_CST from RAW_DATA_CST RAW_DATA at
5100 : : relative index OFF. */
5101 : :
5102 : : static tree
5103 : 29084 : raw_data_cst_elt (tree raw_data, unsigned int off)
5104 : : {
5105 : 29084 : return build_int_cst (TREE_TYPE (raw_data),
5106 : 29084 : TYPE_UNSIGNED (TREE_TYPE (raw_data))
5107 : 58168 : ? (HOST_WIDE_INT)
5108 : 29084 : RAW_DATA_UCHAR_ELT (raw_data, off)
5109 : : : (HOST_WIDE_INT)
5110 : 0 : RAW_DATA_SCHAR_ELT (raw_data, off));
5111 : : }
5112 : :
5113 : : /* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1
5114 : : if none. If INSERT is true, insert a matching element rather than fail. */
5115 : :
5116 : : static HOST_WIDE_INT
5117 : 1889256 : find_array_ctor_elt (tree ary, tree dindex, bool insert)
5118 : : {
5119 : 1889256 : if (tree_int_cst_sgn (dindex) < 0)
5120 : : return -1;
5121 : :
5122 : 1889256 : unsigned HOST_WIDE_INT i = tree_to_uhwi (dindex);
5123 : 1889256 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ary);
5124 : 1889256 : unsigned HOST_WIDE_INT len = vec_safe_length (elts);
5125 : :
5126 : 2814500 : unsigned HOST_WIDE_INT end = len;
5127 : 2814500 : unsigned HOST_WIDE_INT begin = 0;
5128 : :
5129 : : /* If the last element of the CONSTRUCTOR has its own index, we can assume
5130 : : that the same is true of the other elements and index directly. */
5131 : 1778490 : if (end > 0)
5132 : : {
5133 : 1751400 : tree cindex = (*elts)[end - 1].index;
5134 : 1751400 : if (cindex == NULL_TREE)
5135 : : {
5136 : : /* Verify that if the last index is missing, all indexes
5137 : : are missing and there is no RAW_DATA_CST. */
5138 : 19033 : if (flag_checking)
5139 : 201926 : for (unsigned int j = 0; j < len - 1; ++j)
5140 : 182893 : gcc_assert ((*elts)[j].index == NULL_TREE
5141 : : && TREE_CODE ((*elts)[j].value) != RAW_DATA_CST);
5142 : 19033 : if (i < end)
5143 : 19033 : return i;
5144 : : else
5145 : : {
5146 : 0 : begin = end;
5147 : 0 : if (i == end)
5148 : : /* If the element is to be added right at the end,
5149 : : make sure it is added with cleared index too. */
5150 : 1036010 : dindex = NULL_TREE;
5151 : 0 : else if (insert)
5152 : : /* Otherwise, in order not to break the assumption
5153 : : that CONSTRUCTOR either has all indexes or none,
5154 : : we need to add indexes to all elements. */
5155 : 0 : for (unsigned int j = 0; j < len; ++j)
5156 : 0 : (*elts)[j].index = build_int_cst (TREE_TYPE (dindex), j);
5157 : : }
5158 : : }
5159 : 1732367 : else if (TREE_CODE (cindex) == INTEGER_CST
5160 : 1732367 : && compare_tree_int (cindex, end - 1) == 0)
5161 : : {
5162 : 1706174 : tree value = (*elts)[end - 1].value;
5163 : 1706174 : if (i < end)
5164 : : {
5165 : 834303 : if (i == end - 1 && TREE_CODE (value) == RAW_DATA_CST)
5166 : : begin = end - 1;
5167 : : else
5168 : 834213 : return i;
5169 : : }
5170 : 871871 : else if (TREE_CODE (value) == RAW_DATA_CST
5171 : 904049 : && wi::to_offset (dindex) < (wi::to_offset (cindex)
5172 : 32178 : + RAW_DATA_LENGTH (value)))
5173 : 16089 : begin = end - 1;
5174 : : else
5175 : 855782 : begin = end;
5176 : : }
5177 : : }
5178 : :
5179 : : /* Otherwise, find a matching index by means of a binary search. */
5180 : 1078491 : while (begin != end)
5181 : : {
5182 : 76756 : unsigned HOST_WIDE_INT middle = (begin + end) / 2;
5183 : 76756 : constructor_elt &elt = (*elts)[middle];
5184 : 76756 : tree idx = elt.index;
5185 : :
5186 : 76756 : int cmp = array_index_cmp (dindex, idx);
5187 : 76756 : if (cmp > 0
5188 : 56230 : && TREE_CODE (elt.value) == RAW_DATA_CST
5189 : 159630 : && wi::to_offset (dindex) < (wi::to_offset (idx)
5190 : 82874 : + RAW_DATA_LENGTH (elt.value)))
5191 : 28950 : cmp = 0;
5192 : 76756 : if (cmp < 0)
5193 : : end = middle;
5194 : 61555 : else if (cmp > 0)
5195 : 27280 : begin = middle + 1;
5196 : : else
5197 : : {
5198 : 34275 : if (insert && TREE_CODE (elt.value) == RAW_DATA_CST)
5199 : : {
5200 : : /* We need to split the RAW_DATA_CST elt. */
5201 : 122 : constructor_elt e;
5202 : 122 : gcc_checking_assert (TREE_CODE (idx) != RANGE_EXPR);
5203 : 244 : unsigned int off = (wi::to_offset (dindex)
5204 : 244 : - wi::to_offset (idx)).to_uhwi ();
5205 : 122 : tree value = elt.value;
5206 : 122 : unsigned int len = RAW_DATA_LENGTH (value);
5207 : 122 : if (off > 1 && len >= off + 3)
5208 : 22 : value = copy_node (elt.value);
5209 : 122 : if (off)
5210 : : {
5211 : 33 : if (off > 1)
5212 : 33 : RAW_DATA_LENGTH (elt.value) = off;
5213 : : else
5214 : 0 : elt.value = raw_data_cst_elt (elt.value, 0);
5215 : 33 : e.index = size_binop (PLUS_EXPR, elt.index,
5216 : : build_int_cst (TREE_TYPE (elt.index),
5217 : : off));
5218 : 33 : e.value = NULL_TREE;
5219 : 33 : ++middle;
5220 : 33 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e);
5221 : : }
5222 : 122 : (*elts)[middle].value = raw_data_cst_elt (value, off);
5223 : 122 : if (len >= off + 2)
5224 : : {
5225 : 111 : e.index = (*elts)[middle].index;
5226 : 111 : e.index = size_binop (PLUS_EXPR, e.index,
5227 : : build_one_cst (TREE_TYPE (e.index)));
5228 : 111 : if (len >= off + 3)
5229 : : {
5230 : 111 : RAW_DATA_LENGTH (value) -= off + 1;
5231 : 111 : RAW_DATA_POINTER (value) += off + 1;
5232 : 111 : e.value = value;
5233 : : }
5234 : : else
5235 : 0 : e.value = raw_data_cst_elt (value, off + 1);
5236 : 111 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e);
5237 : : }
5238 : 122 : return middle;
5239 : : }
5240 : 1298 : if (insert && TREE_CODE (idx) == RANGE_EXPR)
5241 : : {
5242 : : /* We need to split the range. */
5243 : 347 : constructor_elt e;
5244 : 347 : tree lo = TREE_OPERAND (idx, 0);
5245 : 347 : tree hi = TREE_OPERAND (idx, 1);
5246 : 347 : tree value = elt.value;
5247 : 347 : dindex = fold_convert (sizetype, dindex);
5248 : 347 : if (tree_int_cst_lt (lo, dindex))
5249 : : {
5250 : : /* There are still some lower elts; shorten the range. */
5251 : 178 : tree new_hi = int_const_binop (MINUS_EXPR, dindex,
5252 : 89 : size_one_node);
5253 : 89 : if (tree_int_cst_equal (lo, new_hi))
5254 : : /* Only one element left, no longer a range. */
5255 : 37 : elt.index = lo;
5256 : : else
5257 : 52 : TREE_OPERAND (idx, 1) = new_hi;
5258 : : /* Append the element we want to insert. */
5259 : 89 : ++middle;
5260 : 89 : e.index = dindex;
5261 : 89 : e.value = unshare_constructor (value);
5262 : 89 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e);
5263 : : }
5264 : : else
5265 : : /* No lower elts, the range elt is now ours. */
5266 : 258 : elt.index = dindex;
5267 : :
5268 : 347 : if (tree_int_cst_lt (dindex, hi))
5269 : : {
5270 : : /* There are still some higher elts; append a range. */
5271 : 478 : tree new_lo = int_const_binop (PLUS_EXPR, dindex,
5272 : 239 : size_one_node);
5273 : 239 : if (tree_int_cst_equal (new_lo, hi))
5274 : 98 : e.index = hi;
5275 : : else
5276 : 141 : e.index = build2 (RANGE_EXPR, sizetype, new_lo, hi);
5277 : 239 : e.value = unshare_constructor (value);
5278 : 239 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e);
5279 : : }
5280 : : }
5281 : 34153 : return middle;
5282 : : }
5283 : : }
5284 : :
5285 : 1001735 : if (insert)
5286 : : {
5287 : 997913 : constructor_elt e = { dindex, NULL_TREE };
5288 : 997913 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), end, e);
5289 : 997913 : return end;
5290 : : }
5291 : :
5292 : : return -1;
5293 : : }
5294 : :
5295 : : /* Return a pointer to the constructor_elt of CTOR which matches INDEX. If no
5296 : : matching constructor_elt exists, then add one to CTOR.
5297 : :
5298 : : As an optimization, if POS_HINT is non-negative then it is used as a guess
5299 : : for the (integer) index of the matching constructor_elt within CTOR.
5300 : :
5301 : : If POS_HINT is -2, it means do not insert. */
5302 : :
5303 : : static constructor_elt *
5304 : 11103982 : get_or_insert_ctor_field (tree ctor, tree index, int pos_hint = -1)
5305 : : {
5306 : : /* Check the hint first. */
5307 : 922167 : if (pos_hint >= 0 && (unsigned)pos_hint < CONSTRUCTOR_NELTS (ctor)
5308 : 12026149 : && CONSTRUCTOR_ELT (ctor, pos_hint)->index == index)
5309 : : return CONSTRUCTOR_ELT (ctor, pos_hint);
5310 : :
5311 : 10181821 : bool insertp = (pos_hint != -2);
5312 : 10181821 : tree type = TREE_TYPE (ctor);
5313 : 10181821 : if (TREE_CODE (type) == VECTOR_TYPE && index == NULL_TREE)
5314 : : {
5315 : 128 : gcc_assert (insertp);
5316 : 128 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (ctor), index, NULL_TREE);
5317 : 128 : return &CONSTRUCTOR_ELTS (ctor)->last();
5318 : : }
5319 : 10181693 : else if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
5320 : : {
5321 : 1383694 : if (TREE_CODE (index) == RANGE_EXPR)
5322 : : {
5323 : : /* Support for RANGE_EXPR index lookups is currently limited to
5324 : : accessing an existing element via POS_HINT, or appending a new
5325 : : element to the end of CTOR. ??? Support for other access
5326 : : patterns may also be needed. */
5327 : 3 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
5328 : 3 : if (vec_safe_length (elts))
5329 : : {
5330 : 3 : tree lo = TREE_OPERAND (index, 0);
5331 : 3 : gcc_assert (array_index_cmp (elts->last().index, lo) < 0);
5332 : : }
5333 : 3 : gcc_assert (insertp);
5334 : 3 : CONSTRUCTOR_APPEND_ELT (elts, index, NULL_TREE);
5335 : 3 : return &elts->last();
5336 : : }
5337 : :
5338 : 1383691 : HOST_WIDE_INT i = find_array_ctor_elt (ctor, index, insertp);
5339 : 1383691 : if (i < 0)
5340 : : {
5341 : 620 : gcc_assert (!insertp);
5342 : : return nullptr;
5343 : : }
5344 : 1383071 : constructor_elt *cep = CONSTRUCTOR_ELT (ctor, i);
5345 : 1383071 : if (!insertp && cep->index && TREE_CODE (cep->index) == RANGE_EXPR)
5346 : : {
5347 : : /* Split a range even if we aren't inserting new entries. */
5348 : 0 : gcc_assert (!insertp);
5349 : 0 : i = find_array_ctor_elt (ctor, index, /*insert*/true);
5350 : 0 : gcc_assert (i >= 0);
5351 : 0 : cep = CONSTRUCTOR_ELT (ctor, i);
5352 : : }
5353 : 1383071 : gcc_assert (cep->index == NULL_TREE
5354 : : || TREE_CODE (cep->index) != RANGE_EXPR);
5355 : : return cep;
5356 : : }
5357 : : else
5358 : : {
5359 : 8797999 : gcc_assert (TREE_CODE (index) == FIELD_DECL
5360 : : && (same_type_ignoring_top_level_qualifiers_p
5361 : : (DECL_CONTEXT (index), TREE_TYPE (ctor))));
5362 : :
5363 : : /* We must keep the CONSTRUCTOR's ELTS in FIELD order.
5364 : : Usually we meet initializers in that order, but it is
5365 : : possible for base types to be placed not in program
5366 : : order. */
5367 : 8797999 : tree fields = TYPE_FIELDS (DECL_CONTEXT (index));
5368 : 8797999 : unsigned HOST_WIDE_INT idx = 0;
5369 : 8797999 : constructor_elt *cep = NULL;
5370 : :
5371 : : /* Check if we're changing the active member of a union. */
5372 : 191875 : if (TREE_CODE (type) == UNION_TYPE && CONSTRUCTOR_NELTS (ctor)
5373 : 8875105 : && CONSTRUCTOR_ELT (ctor, 0)->index != index)
5374 : 2608 : vec_safe_truncate (CONSTRUCTOR_ELTS (ctor), 0);
5375 : : /* If the bit offset of INDEX is larger than that of the last
5376 : : constructor_elt, then we can just immediately append a new
5377 : : constructor_elt to the end of CTOR. */
5378 : 8795391 : else if (CONSTRUCTOR_NELTS (ctor)
5379 : 10412611 : && tree_int_cst_compare (bit_position (index),
5380 : 10137156 : bit_position (CONSTRUCTOR_ELTS (ctor)
5381 : 5068578 : ->last().index)) > 0)
5382 : : {
5383 : 2042678 : idx = CONSTRUCTOR_NELTS (ctor);
5384 : 2042678 : goto insert;
5385 : : }
5386 : :
5387 : : /* Otherwise, we need to iterate over CTOR to find or insert INDEX
5388 : : appropriately. */
5389 : :
5390 : 7906234 : for (; vec_safe_iterate (CONSTRUCTOR_ELTS (ctor), idx, &cep);
5391 : 1150913 : idx++, fields = DECL_CHAIN (fields))
5392 : : {
5393 : 4176813 : if (index == cep->index)
5394 : 3005657 : goto found;
5395 : :
5396 : : /* The field we're initializing must be on the field
5397 : : list. Look to see if it is present before the
5398 : : field the current ELT initializes. */
5399 : 15063604 : for (; fields != cep->index; fields = DECL_CHAIN (fields))
5400 : 13912691 : if (index == fields)
5401 : 20243 : goto insert;
5402 : : }
5403 : : /* We fell off the end of the CONSTRUCTOR, so insert a new
5404 : : entry at the end. */
5405 : :
5406 : 5792342 : insert:
5407 : 5792342 : if (!insertp)
5408 : : return nullptr;
5409 : :
5410 : 5792339 : {
5411 : 5792339 : constructor_elt ce = { index, NULL_TREE };
5412 : :
5413 : 5792339 : vec_safe_insert (CONSTRUCTOR_ELTS (ctor), idx, ce);
5414 : 5792339 : cep = CONSTRUCTOR_ELT (ctor, idx);
5415 : : }
5416 : 8797996 : found:;
5417 : :
5418 : 8797996 : return cep;
5419 : : }
5420 : : }
5421 : :
5422 : : /* Under the control of CTX, issue a detailed diagnostic for
5423 : : an out-of-bounds subscript INDEX into the expression ARRAY. */
5424 : :
5425 : : static void
5426 : 558 : diag_array_subscript (location_t loc, const constexpr_ctx *ctx, tree array, tree index)
5427 : : {
5428 : 558 : if (!ctx->quiet)
5429 : : {
5430 : 118 : tree arraytype = TREE_TYPE (array);
5431 : :
5432 : : /* Convert the unsigned array subscript to a signed integer to avoid
5433 : : printing huge numbers for small negative values. */
5434 : 118 : tree sidx = fold_convert (ssizetype, index);
5435 : 118 : STRIP_ANY_LOCATION_WRAPPER (array);
5436 : 118 : if (DECL_P (array))
5437 : : {
5438 : 75 : auto_diagnostic_group d;
5439 : 75 : if (TYPE_DOMAIN (arraytype))
5440 : 69 : error_at (loc, "array subscript value %qE is outside the bounds "
5441 : : "of array %qD of type %qT", sidx, array, arraytype);
5442 : : else
5443 : 6 : error_at (loc, "nonzero array subscript %qE is used with array %qD of "
5444 : : "type %qT with unknown bounds", sidx, array, arraytype);
5445 : 75 : inform (DECL_SOURCE_LOCATION (array), "declared here");
5446 : 75 : }
5447 : 43 : else if (TYPE_DOMAIN (arraytype))
5448 : 40 : error_at (loc, "array subscript value %qE is outside the bounds "
5449 : : "of array type %qT", sidx, arraytype);
5450 : : else
5451 : 3 : error_at (loc, "nonzero array subscript %qE is used with array of type %qT "
5452 : : "with unknown bounds", sidx, arraytype);
5453 : : }
5454 : 558 : }
5455 : :
5456 : : /* Return the number of elements for TYPE (which is an ARRAY_TYPE or
5457 : : a VECTOR_TYPE). */
5458 : :
5459 : : static tree
5460 : 4079701 : get_array_or_vector_nelts (const constexpr_ctx *ctx, tree type,
5461 : : bool *non_constant_p, bool *overflow_p,
5462 : : tree *jump_target)
5463 : : {
5464 : 4079701 : tree nelts;
5465 : 4079701 : if (TREE_CODE (type) == ARRAY_TYPE)
5466 : : {
5467 : 4079701 : if (TYPE_DOMAIN (type))
5468 : 4079465 : nelts = array_type_nelts_top (type);
5469 : : else
5470 : 236 : nelts = size_zero_node;
5471 : : }
5472 : 0 : else if (VECTOR_TYPE_P (type))
5473 : 0 : nelts = size_int (TYPE_VECTOR_SUBPARTS (type));
5474 : : else
5475 : 0 : gcc_unreachable ();
5476 : :
5477 : : /* For VLAs, the number of elements won't be an integer constant. */
5478 : 4079701 : nelts = cxx_eval_constant_expression (ctx, nelts, vc_prvalue,
5479 : : non_constant_p, overflow_p,
5480 : : jump_target);
5481 : 4079701 : return nelts;
5482 : : }
5483 : :
5484 : : /* Extract element INDEX consisting of CHARS_PER_ELT chars from
5485 : : STRING_CST STRING. */
5486 : :
5487 : : static tree
5488 : 1057055 : extract_string_elt (tree string, unsigned chars_per_elt, unsigned index)
5489 : : {
5490 : 1057055 : tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (string)));
5491 : 1057055 : tree r;
5492 : :
5493 : 1057055 : if (chars_per_elt == 1)
5494 : 794390 : r = build_int_cst (type, TREE_STRING_POINTER (string)[index]);
5495 : : else
5496 : : {
5497 : 262665 : const unsigned char *ptr
5498 : 262665 : = ((const unsigned char *)TREE_STRING_POINTER (string)
5499 : 262665 : + index * chars_per_elt);
5500 : 262665 : r = native_interpret_expr (type, ptr, chars_per_elt);
5501 : : }
5502 : 1057055 : return r;
5503 : : }
5504 : :
5505 : : /* Subroutine of cxx_eval_array_reference. T is an ARRAY_REF; evaluate the
5506 : : subscript, diagnose any problems with it, and return the result. */
5507 : :
5508 : : static tree
5509 : 4080758 : eval_and_check_array_index (const constexpr_ctx *ctx,
5510 : : tree t, bool allow_one_past,
5511 : : bool *non_constant_p, bool *overflow_p,
5512 : : tree *jump_target)
5513 : : {
5514 : 4080758 : location_t loc = cp_expr_loc_or_input_loc (t);
5515 : 4080758 : tree ary = TREE_OPERAND (t, 0);
5516 : 4080758 : t = TREE_OPERAND (t, 1);
5517 : 4080758 : tree index = cxx_eval_constant_expression (ctx, t, vc_prvalue,
5518 : : non_constant_p, overflow_p,
5519 : : jump_target);
5520 : 4080758 : if (*jump_target)
5521 : : return NULL_TREE;
5522 : 4080758 : VERIFY_CONSTANT (index);
5523 : :
5524 : 4079184 : if (!tree_fits_shwi_p (index)
5525 : 4079184 : || tree_int_cst_sgn (index) < 0)
5526 : : {
5527 : 108 : diag_array_subscript (loc, ctx, ary, index);
5528 : 108 : *non_constant_p = true;
5529 : 108 : return t;
5530 : : }
5531 : :
5532 : 4079076 : tree nelts = get_array_or_vector_nelts (ctx, TREE_TYPE (ary), non_constant_p,
5533 : : overflow_p, jump_target);
5534 : 4079076 : if (*jump_target)
5535 : : return NULL_TREE;
5536 : 4079076 : VERIFY_CONSTANT (nelts);
5537 : 4079013 : if (allow_one_past
5538 : 4079013 : ? !tree_int_cst_le (index, nelts)
5539 : 2854223 : : !tree_int_cst_lt (index, nelts))
5540 : : {
5541 : 450 : diag_array_subscript (loc, ctx, ary, index);
5542 : 450 : *non_constant_p = true;
5543 : 450 : return t;
5544 : : }
5545 : :
5546 : : return index;
5547 : : }
5548 : :
5549 : : /* Subroutine of cxx_eval_constant_expression.
5550 : : Attempt to reduce a reference to an array slot. */
5551 : :
5552 : : static tree
5553 : 2976389 : cxx_eval_array_reference (const constexpr_ctx *ctx, tree t,
5554 : : value_cat lval,
5555 : : bool *non_constant_p, bool *overflow_p,
5556 : : tree *jump_target)
5557 : : {
5558 : 2976389 : tree oldary = TREE_OPERAND (t, 0);
5559 : 2976389 : tree ary = cxx_eval_constant_expression (ctx, oldary,
5560 : : lval,
5561 : : non_constant_p, overflow_p,
5562 : : jump_target);
5563 : 2976389 : if (*non_constant_p)
5564 : : return t;
5565 : 2797418 : if (*jump_target)
5566 : : return NULL_TREE;
5567 : 2797418 : if (!lval
5568 : 1571409 : && TREE_CODE (ary) == VIEW_CONVERT_EXPR
5569 : 13228 : && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary, 0)))
5570 : 2810646 : && (TYPE_MAIN_VARIANT (TREE_TYPE (t))
5571 : 13228 : == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary, 0))))))
5572 : 13228 : ary = TREE_OPERAND (ary, 0);
5573 : :
5574 : 2797418 : tree oldidx = TREE_OPERAND (t, 1);
5575 : 2797418 : tree index = eval_and_check_array_index (ctx, t, lval,
5576 : : non_constant_p, overflow_p,
5577 : : jump_target);
5578 : 2797418 : if (*non_constant_p)
5579 : : return t;
5580 : 2795289 : if (*jump_target)
5581 : : return NULL_TREE;
5582 : :
5583 : 2795289 : if (lval && ary == oldary && index == oldidx)
5584 : : return t;
5585 : 1696758 : else if (lval == vc_discard)
5586 : : return t;
5587 : 1696758 : else if (lval)
5588 : 126019 : return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL);
5589 : :
5590 : 1570739 : unsigned len = 0, elem_nchars = 1;
5591 : 1570739 : tree elem_type = TREE_TYPE (TREE_TYPE (ary));
5592 : 1570739 : if (TREE_CODE (ary) == CONSTRUCTOR)
5593 : 504155 : len = CONSTRUCTOR_NELTS (ary);
5594 : 1066584 : else if (TREE_CODE (ary) == STRING_CST)
5595 : : {
5596 : 1053364 : elem_nchars = (TYPE_PRECISION (elem_type)
5597 : 1053364 : / TYPE_PRECISION (char_type_node));
5598 : 1053364 : len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars;
5599 : : }
5600 : 13220 : else if (TREE_CODE (ary) == VECTOR_CST)
5601 : : /* We don't create variable-length VECTOR_CSTs. */
5602 : 13220 : len = VECTOR_CST_NELTS (ary).to_constant ();
5603 : : else
5604 : : {
5605 : : /* We can't do anything with other tree codes, so use
5606 : : VERIFY_CONSTANT to complain and fail. */
5607 : 0 : VERIFY_CONSTANT (ary);
5608 : 0 : gcc_unreachable ();
5609 : : }
5610 : :
5611 : 1570739 : bool found;
5612 : 1570739 : HOST_WIDE_INT i = 0;
5613 : 1570739 : if (TREE_CODE (ary) == CONSTRUCTOR)
5614 : : {
5615 : 504155 : HOST_WIDE_INT ix = find_array_ctor_elt (ary, index);
5616 : 504155 : found = (ix >= 0);
5617 : 504155 : if (found)
5618 : 501039 : i = ix;
5619 : : }
5620 : : else
5621 : : {
5622 : 1066584 : i = tree_to_shwi (index);
5623 : 1066584 : found = (i < len);
5624 : : }
5625 : :
5626 : 1570739 : if (found)
5627 : : {
5628 : 1567184 : tree r;
5629 : 1567184 : if (TREE_CODE (ary) == CONSTRUCTOR)
5630 : : {
5631 : 501039 : r = (*CONSTRUCTOR_ELTS (ary))[i].value;
5632 : 501039 : if (TREE_CODE (r) == RAW_DATA_CST)
5633 : : {
5634 : 28962 : tree ridx = (*CONSTRUCTOR_ELTS (ary))[i].index;
5635 : 28962 : gcc_checking_assert (ridx);
5636 : 28962 : unsigned int off
5637 : 28962 : = (wi::to_offset (index) - wi::to_offset (ridx)).to_uhwi ();
5638 : 28962 : r = raw_data_cst_elt (r, off);
5639 : : }
5640 : : }
5641 : 1066145 : else if (TREE_CODE (ary) == VECTOR_CST)
5642 : 13220 : r = VECTOR_CST_ELT (ary, i);
5643 : : else
5644 : 1052925 : r = extract_string_elt (ary, elem_nchars, i);
5645 : :
5646 : 1095107 : if (r)
5647 : : /* Don't VERIFY_CONSTANT here. */
5648 : 1567184 : return r;
5649 : :
5650 : : /* Otherwise the element doesn't have a value yet. */
5651 : : }
5652 : :
5653 : : /* Not found. */
5654 : :
5655 : 3555 : if (is_really_empty_class (elem_type, /*ignore_vptr*/false))
5656 : 49 : return build_constructor (elem_type, NULL);
5657 : :
5658 : 3506 : if (TREE_CODE (ary) == CONSTRUCTOR
5659 : 3506 : && CONSTRUCTOR_NO_CLEARING (ary))
5660 : : {
5661 : : /* 'ary' is part of the aggregate initializer we're currently
5662 : : building; if there's no initializer for this element yet,
5663 : : that's an error. */
5664 : 51 : if (!ctx->quiet)
5665 : 16 : error ("accessing uninitialized array element");
5666 : 51 : *non_constant_p = true;
5667 : 51 : return t;
5668 : : }
5669 : :
5670 : : /* If it's within the array bounds but doesn't have an explicit
5671 : : initializer, it's initialized from {}. But use build_value_init
5672 : : directly for non-aggregates to avoid creating a garbage CONSTRUCTOR. */
5673 : 3455 : tree val;
5674 : 3455 : constexpr_ctx new_ctx;
5675 : 3455 : if (CP_AGGREGATE_TYPE_P (elem_type))
5676 : : {
5677 : 480 : tree empty_ctor = build_constructor (init_list_type_node, NULL);
5678 : 480 : val = digest_init (elem_type, empty_ctor, tf_warning_or_error);
5679 : : }
5680 : : else
5681 : 2975 : val = build_value_init (elem_type, tf_warning_or_error);
5682 : :
5683 : : /* Create a new constructor only if we don't already have a suitable one. */
5684 : 3455 : const bool new_ctor = (!SCALAR_TYPE_P (elem_type)
5685 : 3967 : && (!ctx->ctor
5686 : 45 : || !same_type_ignoring_top_level_qualifiers_p
5687 : 45 : (elem_type, TREE_TYPE (ctx->ctor))));
5688 : 503 : if (new_ctor)
5689 : : {
5690 : 503 : new_ctx = *ctx;
5691 : : /* We clear the object here. We used to replace it with T, but that
5692 : : caused problems (101371, 108158); and anyway, T is the initializer,
5693 : : not the target object. */
5694 : 503 : new_ctx.object = NULL_TREE;
5695 : 503 : new_ctx.ctor = build_constructor (elem_type, NULL);
5696 : 503 : ctx = &new_ctx;
5697 : : }
5698 : 3455 : t = cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
5699 : : overflow_p, jump_target);
5700 : 3455 : if (new_ctor && t != ctx->ctor)
5701 : 482 : free_constructor (ctx->ctor);
5702 : : return t;
5703 : : }
5704 : :
5705 : : /* Subroutine of cxx_eval_constant_expression.
5706 : : Attempt to reduce a field access of a value of class type. */
5707 : :
5708 : : static tree
5709 : 34289739 : cxx_eval_component_reference (const constexpr_ctx *ctx, tree t,
5710 : : value_cat lval,
5711 : : bool *non_constant_p, bool *overflow_p,
5712 : : tree *jump_target)
5713 : : {
5714 : 34289739 : unsigned HOST_WIDE_INT i;
5715 : 34289739 : tree field;
5716 : 34289739 : tree value;
5717 : 34289739 : tree part = TREE_OPERAND (t, 1);
5718 : 34289739 : tree orig_whole = TREE_OPERAND (t, 0);
5719 : 34289739 : tree whole = cxx_eval_constant_expression (ctx, orig_whole,
5720 : : lval,
5721 : : non_constant_p, overflow_p,
5722 : : jump_target);
5723 : 34289739 : if (*non_constant_p)
5724 : : return t;
5725 : 19239156 : if (*jump_target)
5726 : : return NULL_TREE;
5727 : 19239156 : if (INDIRECT_REF_P (whole)
5728 : 19239156 : && integer_zerop (TREE_OPERAND (whole, 0)))
5729 : : {
5730 : 177 : if (!ctx->quiet)
5731 : 39 : error ("dereferencing a null pointer in %qE", orig_whole);
5732 : 177 : *non_constant_p = true;
5733 : 177 : return t;
5734 : : }
5735 : :
5736 : 19238979 : if (TREE_CODE (whole) == PTRMEM_CST)
5737 : 1310 : whole = cplus_expand_constant (whole);
5738 : 19238979 : if (whole == orig_whole)
5739 : : return t;
5740 : 12903701 : if (lval == vc_discard)
5741 : : return t;
5742 : 12903677 : if (lval)
5743 : 4916453 : return fold_build3 (COMPONENT_REF, TREE_TYPE (t),
5744 : : whole, part, NULL_TREE);
5745 : : /* Don't VERIFY_CONSTANT here; we only want to check that we got a
5746 : : CONSTRUCTOR. */
5747 : 7987224 : if (TREE_CODE (whole) != CONSTRUCTOR)
5748 : : {
5749 : 0 : if (!ctx->quiet)
5750 : 0 : error ("%qE is not a constant expression", orig_whole);
5751 : 0 : *non_constant_p = true;
5752 : 0 : return t;
5753 : : }
5754 : 7985755 : if ((cxx_dialect < cxx14 || CONSTRUCTOR_MUTABLE_POISON (whole))
5755 : 7988863 : && DECL_MUTABLE_P (part))
5756 : : {
5757 : 132 : if (!ctx->quiet)
5758 : 16 : error ("mutable %qD is not usable in a constant expression", part);
5759 : 132 : *non_constant_p = true;
5760 : 132 : return t;
5761 : : }
5762 : :
5763 : 7987092 : bool pmf = TYPE_PTRMEMFUNC_P (TREE_TYPE (whole));
5764 : 10872969 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
5765 : : {
5766 : : /* Use name match for PMF fields, as a variant will have a
5767 : : different FIELD_DECL with a different type. */
5768 : 10868268 : if (pmf ? DECL_NAME (field) == DECL_NAME (part)
5769 : : : field == part)
5770 : : {
5771 : 7982391 : if (value == void_node)
5772 : 3 : goto uninit;
5773 : 7982388 : if (value)
5774 : : {
5775 : 7982370 : STRIP_ANY_LOCATION_WRAPPER (value);
5776 : 7982370 : return value;
5777 : : }
5778 : : else
5779 : : /* We're in the middle of initializing it. */
5780 : : break;
5781 : : }
5782 : : }
5783 : 4719 : if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE)
5784 : : {
5785 : 89 : if (CONSTRUCTOR_NELTS (whole) > 0)
5786 : : {
5787 : : /* DR 1188 says we don't have to deal with this. */
5788 : 74 : if (!ctx->quiet)
5789 : : {
5790 : 19 : constructor_elt *cep = CONSTRUCTOR_ELT (whole, 0);
5791 : 19 : if (cep->value == NULL_TREE)
5792 : 9 : error ("accessing uninitialized member %qD", part);
5793 : : else
5794 : 10 : error ("accessing %qD member instead of active %qD member "
5795 : : "in constant expression", part, cep->index);
5796 : : }
5797 : 74 : *non_constant_p = true;
5798 : 74 : return t;
5799 : : }
5800 : 15 : else if (!CONSTRUCTOR_NO_CLEARING (whole))
5801 : : {
5802 : : /* Value-initialized union, check if looking at the first member. */
5803 : 12 : tree first = next_aggregate_field (TYPE_FIELDS (TREE_TYPE (whole)));
5804 : 12 : if (first != part)
5805 : : {
5806 : 9 : if (!ctx->quiet)
5807 : 3 : error ("accessing %qD member instead of initialized %qD "
5808 : : "member in constant expression", part, first);
5809 : 9 : *non_constant_p = true;
5810 : 9 : return t;
5811 : : }
5812 : : }
5813 : : }
5814 : :
5815 : : /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty
5816 : : classes never get represented; throw together a value now. */
5817 : 4636 : if (is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
5818 : 3551 : return build_constructor (TREE_TYPE (t), NULL);
5819 : :
5820 : 1085 : gcc_assert (DECL_CONTEXT (part) == TYPE_MAIN_VARIANT (TREE_TYPE (whole)));
5821 : :
5822 : 1085 : if (CONSTRUCTOR_NO_CLEARING (whole))
5823 : : {
5824 : 96 : uninit:
5825 : : /* 'whole' is part of the aggregate initializer we're currently
5826 : : building; if there's no initializer for this member yet, that's an
5827 : : error. */
5828 : 99 : if (!ctx->quiet)
5829 : 22 : error ("accessing uninitialized member %qD", part);
5830 : 99 : *non_constant_p = true;
5831 : 99 : return t;
5832 : : }
5833 : :
5834 : : /* If there's no explicit init for this field, it's value-initialized. */
5835 : :
5836 : 989 : if (AGGREGATE_TYPE_P (TREE_TYPE (t)))
5837 : : {
5838 : : /* As in cxx_eval_store_expression, insert an empty CONSTRUCTOR
5839 : : and copy the flags. */
5840 : 128 : constructor_elt *e = get_or_insert_ctor_field (whole, part);
5841 : 128 : e->value = value = build_constructor (TREE_TYPE (part), NULL);
5842 : 128 : CONSTRUCTOR_ZERO_PADDING_BITS (value)
5843 : 128 : = CONSTRUCTOR_ZERO_PADDING_BITS (whole);
5844 : 128 : return value;
5845 : : }
5846 : :
5847 : 861 : value = build_value_init (TREE_TYPE (t), tf_warning_or_error);
5848 : 861 : return cxx_eval_constant_expression (ctx, value,
5849 : : lval,
5850 : : non_constant_p, overflow_p,
5851 : 861 : jump_target);
5852 : : }
5853 : :
5854 : : /* Subroutine of cxx_eval_constant_expression.
5855 : : Attempt to reduce a field access of a value of class type that is
5856 : : expressed as a BIT_FIELD_REF. */
5857 : :
5858 : : static tree
5859 : 2885 : cxx_eval_bit_field_ref (const constexpr_ctx *ctx, tree t,
5860 : : value_cat lval,
5861 : : bool *non_constant_p, bool *overflow_p,
5862 : : tree *jump_target)
5863 : : {
5864 : 2885 : tree orig_whole = TREE_OPERAND (t, 0);
5865 : 2885 : tree retval, fldval, utype, mask;
5866 : 2885 : bool fld_seen = false;
5867 : 2885 : HOST_WIDE_INT istart, isize;
5868 : 2885 : tree whole = cxx_eval_constant_expression (ctx, orig_whole,
5869 : : lval,
5870 : : non_constant_p, overflow_p,
5871 : : jump_target);
5872 : 2885 : tree start, field, value;
5873 : 2885 : unsigned HOST_WIDE_INT i;
5874 : :
5875 : 2885 : if (*jump_target)
5876 : : return NULL_TREE;
5877 : 2885 : if (whole == orig_whole)
5878 : : return t;
5879 : : /* Don't VERIFY_CONSTANT here; we only want to check that we got a
5880 : : CONSTRUCTOR. */
5881 : 2602 : if (!*non_constant_p
5882 : 2602 : && TREE_CODE (whole) != VECTOR_CST
5883 : 2600 : && TREE_CODE (whole) != CONSTRUCTOR)
5884 : : {
5885 : 0 : if (!ctx->quiet)
5886 : 0 : error ("%qE is not a constant expression", orig_whole);
5887 : 0 : *non_constant_p = true;
5888 : : }
5889 : 2602 : if (*non_constant_p)
5890 : : return t;
5891 : :
5892 : 2602 : if (TREE_CODE (whole) == VECTOR_CST || !INTEGRAL_TYPE_P (TREE_TYPE (t)))
5893 : : {
5894 : 2 : if (tree r = fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
5895 : : TREE_OPERAND (t, 1), TREE_OPERAND (t, 2)))
5896 : : return r;
5897 : 0 : if (!ctx->quiet)
5898 : 0 : error ("%qE is not a constant expression", orig_whole);
5899 : 0 : *non_constant_p = true;
5900 : 0 : return t;
5901 : : }
5902 : :
5903 : 2600 : start = TREE_OPERAND (t, 2);
5904 : 2600 : istart = tree_to_shwi (start);
5905 : 2600 : isize = tree_to_shwi (TREE_OPERAND (t, 1));
5906 : 2600 : utype = TREE_TYPE (t);
5907 : 2600 : if (!TYPE_UNSIGNED (utype))
5908 : 0 : utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1);
5909 : 2600 : retval = build_int_cst (utype, 0);
5910 : 36400 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
5911 : : {
5912 : 33800 : tree bitpos = bit_position (field);
5913 : 33800 : STRIP_ANY_LOCATION_WRAPPER (value);
5914 : 33800 : if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1))
5915 : : return value;
5916 : 33800 : if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE
5917 : 33800 : && TREE_CODE (value) == INTEGER_CST
5918 : 33800 : && tree_fits_shwi_p (bitpos)
5919 : 67600 : && tree_fits_shwi_p (DECL_SIZE (field)))
5920 : : {
5921 : 33800 : HOST_WIDE_INT bit = tree_to_shwi (bitpos);
5922 : 33800 : HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field));
5923 : 33800 : HOST_WIDE_INT shift;
5924 : 33800 : if (bit >= istart && bit + sz <= istart + isize)
5925 : : {
5926 : 7800 : fldval = fold_convert (utype, value);
5927 : 7800 : mask = build_int_cst_type (utype, -1);
5928 : 7800 : mask = fold_build2 (LSHIFT_EXPR, utype, mask,
5929 : : size_int (TYPE_PRECISION (utype) - sz));
5930 : 7800 : mask = fold_build2 (RSHIFT_EXPR, utype, mask,
5931 : : size_int (TYPE_PRECISION (utype) - sz));
5932 : 7800 : fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask);
5933 : 7800 : shift = bit - istart;
5934 : 7800 : if (BYTES_BIG_ENDIAN)
5935 : : shift = TYPE_PRECISION (utype) - shift - sz;
5936 : 7800 : fldval = fold_build2 (LSHIFT_EXPR, utype, fldval,
5937 : : size_int (shift));
5938 : 7800 : retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval);
5939 : 7800 : fld_seen = true;
5940 : : }
5941 : : }
5942 : : }
5943 : 2600 : if (fld_seen)
5944 : 2600 : return fold_convert (TREE_TYPE (t), retval);
5945 : 0 : gcc_unreachable ();
5946 : : return error_mark_node;
5947 : : }
5948 : :
5949 : : /* Helper for cxx_eval_bit_cast.
5950 : : Check [bit.cast]/3 rules, bit_cast is constexpr only if the To and From
5951 : : types and types of all subobjects have is_union_v<T>, is_pointer_v<T>,
5952 : : is_member_pointer_v<T>, is_volatile_v<T> false and has no non-static
5953 : : data members of reference type. */
5954 : :
5955 : : static bool
5956 : 4850 : check_bit_cast_type (const constexpr_ctx *ctx, location_t loc, tree type,
5957 : : tree orig_type)
5958 : : {
5959 : 5343 : if (TREE_CODE (type) == UNION_TYPE)
5960 : : {
5961 : 63 : if (!ctx->quiet)
5962 : : {
5963 : 21 : if (type == orig_type)
5964 : 6 : error_at (loc, "%qs is not a constant expression because %qT is "
5965 : : "a union type", "__builtin_bit_cast", type);
5966 : : else
5967 : 15 : error_at (loc, "%qs is not a constant expression because %qT "
5968 : : "contains a union type", "__builtin_bit_cast",
5969 : : orig_type);
5970 : : }
5971 : 63 : return true;
5972 : : }
5973 : : if (TREE_CODE (type) == POINTER_TYPE)
5974 : : {
5975 : 60 : if (!ctx->quiet)
5976 : : {
5977 : 18 : if (type == orig_type)
5978 : 6 : error_at (loc, "%qs is not a constant expression because %qT is "
5979 : : "a pointer type", "__builtin_bit_cast", type);
5980 : : else
5981 : 12 : error_at (loc, "%qs is not a constant expression because %qT "
5982 : : "contains a pointer type", "__builtin_bit_cast",
5983 : : orig_type);
5984 : : }
5985 : 60 : return true;
5986 : : }
5987 : : if (TREE_CODE (type) == REFERENCE_TYPE)
5988 : : {
5989 : 0 : if (!ctx->quiet)
5990 : : {
5991 : 0 : if (type == orig_type)
5992 : 0 : error_at (loc, "%qs is not a constant expression because %qT is "
5993 : : "a reference type", "__builtin_bit_cast", type);
5994 : : else
5995 : 0 : error_at (loc, "%qs is not a constant expression because %qT "
5996 : : "contains a reference type", "__builtin_bit_cast",
5997 : : orig_type);
5998 : : }
5999 : 0 : return true;
6000 : : }
6001 : 1373 : if (TYPE_PTRMEM_P (type))
6002 : : {
6003 : 36 : if (!ctx->quiet)
6004 : : {
6005 : 12 : if (type == orig_type)
6006 : 12 : error_at (loc, "%qs is not a constant expression because %qT is "
6007 : : "a pointer to member type", "__builtin_bit_cast",
6008 : : type);
6009 : : else
6010 : 0 : error_at (loc, "%qs is not a constant expression because %qT "
6011 : : "contains a pointer to member type",
6012 : : "__builtin_bit_cast", orig_type);
6013 : : }
6014 : 36 : return true;
6015 : : }
6016 : 5184 : if (TYPE_VOLATILE (type))
6017 : : {
6018 : 0 : if (!ctx->quiet)
6019 : : {
6020 : 0 : if (type == orig_type)
6021 : 0 : error_at (loc, "%qs is not a constant expression because %qT is "
6022 : : "volatile", "__builtin_bit_cast", type);
6023 : : else
6024 : 0 : error_at (loc, "%qs is not a constant expression because %qT "
6025 : : "contains a volatile subobject",
6026 : : "__builtin_bit_cast", orig_type);
6027 : : }
6028 : 0 : return true;
6029 : : }
6030 : 5184 : if (TREE_CODE (type) == RECORD_TYPE)
6031 : 27210 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6032 : 25945 : if (TREE_CODE (field) == FIELD_DECL
6033 : 25945 : && check_bit_cast_type (ctx, loc, TREE_TYPE (field), orig_type))
6034 : : return true;
6035 : 5094 : if (TREE_CODE (type) == ARRAY_TYPE)
6036 : 493 : return check_bit_cast_type (ctx, loc, TREE_TYPE (type), orig_type);
6037 : : return false;
6038 : : }
6039 : :
6040 : : /* Helper function for cxx_eval_bit_cast. For unsigned char or
6041 : : std::byte members of CONSTRUCTOR (recursively) if they contain
6042 : : some indeterminate bits (as set in MASK), remove the ctor elts,
6043 : : mark the CONSTRUCTOR as CONSTRUCTOR_NO_CLEARING and clear the
6044 : : bits in MASK. */
6045 : :
6046 : : static void
6047 : 609 : clear_uchar_or_std_byte_in_mask (location_t loc, tree t, unsigned char *mask)
6048 : : {
6049 : 609 : if (TREE_CODE (t) != CONSTRUCTOR)
6050 : : return;
6051 : :
6052 : : unsigned i, j = 0;
6053 : : tree index, value;
6054 : 2102 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), i, index, value)
6055 : : {
6056 : 1493 : tree type = TREE_TYPE (value);
6057 : 1493 : if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
6058 : 2547 : && DECL_BIT_FIELD_TYPE (index) != NULL_TREE)
6059 : : {
6060 : 270 : if (is_byte_access_type_not_plain_char (DECL_BIT_FIELD_TYPE (index)))
6061 : : {
6062 : 135 : HOST_WIDE_INT fldsz = TYPE_PRECISION (TREE_TYPE (index));
6063 : 135 : gcc_assert (fldsz != 0);
6064 : 135 : HOST_WIDE_INT pos = int_byte_position (index);
6065 : 135 : HOST_WIDE_INT bpos
6066 : 135 : = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (index));
6067 : 135 : bpos %= BITS_PER_UNIT;
6068 : 135 : HOST_WIDE_INT end
6069 : 135 : = ROUND_UP (bpos + fldsz, BITS_PER_UNIT) / BITS_PER_UNIT;
6070 : 135 : gcc_assert (end == 1 || end == 2);
6071 : 135 : unsigned char *p = mask + pos;
6072 : 135 : unsigned char mask_save[2];
6073 : 135 : mask_save[0] = mask[pos];
6074 : 135 : mask_save[1] = end == 2 ? mask[pos + 1] : 0;
6075 : 135 : if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
6076 : : sorry_at (loc, "PDP11 bit-field handling unsupported"
6077 : : " in %qs", "__builtin_bit_cast");
6078 : 135 : else if (BYTES_BIG_ENDIAN)
6079 : : {
6080 : : /* Big endian. */
6081 : : if (bpos + fldsz <= BITS_PER_UNIT)
6082 : : *p &= ~(((1 << fldsz) - 1)
6083 : : << (BITS_PER_UNIT - bpos - fldsz));
6084 : : else
6085 : : {
6086 : : gcc_assert (bpos);
6087 : : *p &= ~(((1U << BITS_PER_UNIT) - 1) >> bpos);
6088 : : p++;
6089 : : fldsz -= BITS_PER_UNIT - bpos;
6090 : : gcc_assert (fldsz && fldsz < BITS_PER_UNIT);
6091 : : *p &= ((1U << BITS_PER_UNIT) - 1) >> fldsz;
6092 : : }
6093 : : }
6094 : : else
6095 : : {
6096 : : /* Little endian. */
6097 : 135 : if (bpos + fldsz <= BITS_PER_UNIT)
6098 : 135 : *p &= ~(((1 << fldsz) - 1) << bpos);
6099 : : else
6100 : : {
6101 : 0 : gcc_assert (bpos);
6102 : 0 : *p &= ~(((1 << BITS_PER_UNIT) - 1) << bpos);
6103 : 0 : p++;
6104 : 0 : fldsz -= BITS_PER_UNIT - bpos;
6105 : 0 : gcc_assert (fldsz && fldsz < BITS_PER_UNIT);
6106 : 0 : *p &= ~((1 << fldsz) - 1);
6107 : : }
6108 : : }
6109 : 135 : if (mask_save[0] != mask[pos]
6110 : 99 : || (end == 2 && mask_save[1] != mask[pos + 1]))
6111 : : {
6112 : 36 : CONSTRUCTOR_NO_CLEARING (t) = 1;
6113 : 36 : continue;
6114 : : }
6115 : : }
6116 : : }
6117 : 1223 : else if (is_byte_access_type_not_plain_char (type))
6118 : : {
6119 : 228 : HOST_WIDE_INT pos;
6120 : 228 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6121 : 132 : pos = tree_to_shwi (index);
6122 : : else
6123 : 96 : pos = int_byte_position (index);
6124 : 228 : if (mask[pos])
6125 : : {
6126 : 48 : CONSTRUCTOR_NO_CLEARING (t) = 1;
6127 : 48 : mask[pos] = 0;
6128 : 48 : continue;
6129 : : }
6130 : : }
6131 : 1409 : if (TREE_CODE (value) == CONSTRUCTOR)
6132 : : {
6133 : 264 : HOST_WIDE_INT pos;
6134 : 264 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6135 : 144 : pos = tree_to_shwi (index)
6136 : 72 : * tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))));
6137 : : else
6138 : 192 : pos = int_byte_position (index);
6139 : 264 : clear_uchar_or_std_byte_in_mask (loc, value, mask + pos);
6140 : : }
6141 : 1409 : if (i != j)
6142 : : {
6143 : 180 : CONSTRUCTOR_ELT (t, j)->index = index;
6144 : 180 : CONSTRUCTOR_ELT (t, j)->value = value;
6145 : : }
6146 : 1409 : ++j;
6147 : : }
6148 : 1218 : if (CONSTRUCTOR_NELTS (t) != j)
6149 : 84 : vec_safe_truncate (CONSTRUCTOR_ELTS (t), j);
6150 : : }
6151 : :
6152 : : /* Subroutine of cxx_eval_constant_expression.
6153 : : Attempt to evaluate a BIT_CAST_EXPR. */
6154 : :
6155 : : static tree
6156 : 1031 : cxx_eval_bit_cast (const constexpr_ctx *ctx, tree t, bool *non_constant_p,
6157 : : bool *overflow_p, tree *jump_target)
6158 : : {
6159 : 1031 : if (check_bit_cast_type (ctx, EXPR_LOCATION (t), TREE_TYPE (t),
6160 : 1031 : TREE_TYPE (t))
6161 : 3349 : || check_bit_cast_type (ctx, cp_expr_loc_or_loc (TREE_OPERAND (t, 0),
6162 : 950 : EXPR_LOCATION (t)),
6163 : 950 : TREE_TYPE (TREE_OPERAND (t, 0)),
6164 : 950 : TREE_TYPE (TREE_OPERAND (t, 0))))
6165 : : {
6166 : 159 : *non_constant_p = true;
6167 : 159 : return t;
6168 : : }
6169 : :
6170 : 872 : tree op = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), vc_prvalue,
6171 : : non_constant_p, overflow_p,
6172 : : jump_target);
6173 : 872 : if (*non_constant_p)
6174 : : return t;
6175 : 776 : if (*jump_target)
6176 : : return NULL_TREE;
6177 : :
6178 : 776 : location_t loc = EXPR_LOCATION (t);
6179 : 776 : if (BITS_PER_UNIT != 8 || CHAR_BIT != 8)
6180 : : {
6181 : : if (!ctx->quiet)
6182 : : sorry_at (loc, "%qs cannot be constant evaluated on the target",
6183 : : "__builtin_bit_cast");
6184 : : *non_constant_p = true;
6185 : : return t;
6186 : : }
6187 : :
6188 : 776 : if (!tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (t))))
6189 : : {
6190 : 0 : if (!ctx->quiet)
6191 : 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
6192 : : "type is too large", "__builtin_bit_cast");
6193 : 0 : *non_constant_p = true;
6194 : 0 : return t;
6195 : : }
6196 : :
6197 : 776 : HOST_WIDE_INT len = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (t)));
6198 : 776 : if (len < 0 || (int) len != len)
6199 : : {
6200 : 0 : if (!ctx->quiet)
6201 : 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
6202 : : "type is too large", "__builtin_bit_cast");
6203 : 0 : *non_constant_p = true;
6204 : 0 : return t;
6205 : : }
6206 : :
6207 : 776 : unsigned char buf[64];
6208 : 776 : unsigned char *ptr, *mask;
6209 : 776 : size_t alen = (size_t) len * 2;
6210 : 776 : if (alen <= sizeof (buf))
6211 : : ptr = buf;
6212 : : else
6213 : 3 : ptr = XNEWVEC (unsigned char, alen);
6214 : 776 : mask = ptr + (size_t) len;
6215 : : /* At the beginning consider everything indeterminate. */
6216 : 776 : memset (mask, ~0, (size_t) len);
6217 : :
6218 : 776 : if (native_encode_initializer (op, ptr, len, 0, mask) != len)
6219 : : {
6220 : 0 : if (!ctx->quiet)
6221 : 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
6222 : : "argument cannot be encoded", "__builtin_bit_cast");
6223 : 0 : *non_constant_p = true;
6224 : 0 : if (ptr != buf)
6225 : 0 : XDELETE (ptr);
6226 : 0 : return t;
6227 : : }
6228 : :
6229 : 776 : tree r = NULL_TREE;
6230 : 776 : if (can_native_interpret_type_p (TREE_TYPE (t)))
6231 : : {
6232 : 431 : r = native_interpret_expr (TREE_TYPE (t), ptr, len);
6233 : 431 : if (is_byte_access_type_not_plain_char (TREE_TYPE (t)))
6234 : : {
6235 : 46 : gcc_assert (len == 1);
6236 : 46 : if (mask[0])
6237 : : {
6238 : 24 : memset (mask, 0, len);
6239 : 24 : r = build_constructor (TREE_TYPE (r), NULL);
6240 : 24 : CONSTRUCTOR_NO_CLEARING (r) = 1;
6241 : : }
6242 : : }
6243 : : }
6244 : 345 : else if (TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE)
6245 : : {
6246 : 345 : r = native_interpret_aggregate (TREE_TYPE (t), ptr, 0, len);
6247 : 345 : if (r != NULL_TREE)
6248 : : {
6249 : 345 : clear_type_padding_in_mask (TREE_TYPE (t), mask);
6250 : 345 : clear_uchar_or_std_byte_in_mask (loc, r, mask);
6251 : 345 : if (CHECKING_P)
6252 : : {
6253 : 345 : tree e = cxx_eval_bare_aggregate (ctx, r, vc_prvalue,
6254 : : non_constant_p, overflow_p,
6255 : : jump_target);
6256 : 345 : gcc_checking_assert (e == r && !*jump_target);
6257 : : r = e;
6258 : : }
6259 : : }
6260 : : }
6261 : :
6262 : 776 : if (r != NULL_TREE)
6263 : : {
6264 : 6223 : for (int i = 0; i < len; i++)
6265 : 5537 : if (mask[i])
6266 : : {
6267 : 90 : if (!ctx->quiet)
6268 : 30 : error_at (loc, "%qs accessing uninitialized byte at offset %d",
6269 : : "__builtin_bit_cast", i);
6270 : 90 : *non_constant_p = true;
6271 : 90 : r = t;
6272 : 90 : break;
6273 : : }
6274 : 776 : if (ptr != buf)
6275 : 3 : XDELETE (ptr);
6276 : 776 : return r;
6277 : : }
6278 : :
6279 : 0 : if (!ctx->quiet)
6280 : 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
6281 : : "argument cannot be interpreted", "__builtin_bit_cast");
6282 : 0 : *non_constant_p = true;
6283 : 0 : if (ptr != buf)
6284 : 0 : XDELETE (ptr);
6285 : : return t;
6286 : : }
6287 : :
6288 : : /* Subroutine of cxx_eval_constant_expression.
6289 : : Evaluate a short-circuited logical expression T in the context
6290 : : of a given constexpr CALL. BAILOUT_VALUE is the value for
6291 : : early return. CONTINUE_VALUE is used here purely for
6292 : : sanity check purposes. */
6293 : :
6294 : : static tree
6295 : 7247760 : cxx_eval_logical_expression (const constexpr_ctx *ctx, tree t,
6296 : : tree bailout_value, tree continue_value,
6297 : : bool *non_constant_p, bool *overflow_p,
6298 : : tree *jump_target)
6299 : : {
6300 : 7247760 : tree r;
6301 : 7247760 : tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
6302 : : vc_prvalue, non_constant_p,
6303 : : overflow_p, jump_target);
6304 : 7247759 : if (*jump_target)
6305 : : return NULL_TREE;
6306 : 7247750 : VERIFY_CONSTANT (lhs);
6307 : 4419290 : if (tree_int_cst_equal (lhs, bailout_value))
6308 : : return lhs;
6309 : 3730097 : gcc_assert (tree_int_cst_equal (lhs, continue_value));
6310 : 3730097 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
6311 : : vc_prvalue, non_constant_p,
6312 : : overflow_p, jump_target);
6313 : 3730097 : if (*jump_target)
6314 : : return NULL_TREE;
6315 : 3730097 : VERIFY_CONSTANT (r);
6316 : : return r;
6317 : : }
6318 : :
6319 : : /* REF is a COMPONENT_REF designating a particular field. V is a vector of
6320 : : CONSTRUCTOR elements to initialize (part of) an object containing that
6321 : : field. Return a pointer to the constructor_elt corresponding to the
6322 : : initialization of the field. */
6323 : :
6324 : : static constructor_elt *
6325 : 0 : base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref)
6326 : : {
6327 : 0 : tree aggr = TREE_OPERAND (ref, 0);
6328 : 0 : tree field = TREE_OPERAND (ref, 1);
6329 : 0 : HOST_WIDE_INT i;
6330 : 0 : constructor_elt *ce;
6331 : :
6332 : 0 : gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
6333 : :
6334 : 0 : if (TREE_CODE (aggr) == COMPONENT_REF)
6335 : : {
6336 : 0 : constructor_elt *base_ce
6337 : 0 : = base_field_constructor_elt (v, aggr);
6338 : 0 : v = CONSTRUCTOR_ELTS (base_ce->value);
6339 : : }
6340 : :
6341 : 0 : for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
6342 : 0 : if (ce->index == field)
6343 : 0 : return ce;
6344 : :
6345 : 0 : gcc_unreachable ();
6346 : : return NULL;
6347 : : }
6348 : :
6349 : : /* Some of the expressions fed to the constexpr mechanism are calls to
6350 : : constructors, which have type void. In that case, return the type being
6351 : : initialized by the constructor. */
6352 : :
6353 : : static tree
6354 : 385652659 : initialized_type (tree t)
6355 : : {
6356 : 386463226 : if (TYPE_P (t))
6357 : : return t;
6358 : 386462891 : tree type = TREE_TYPE (t);
6359 : 386462891 : if (TREE_CODE (t) == CALL_EXPR)
6360 : : {
6361 : : /* A constructor call has void type, so we need to look deeper. */
6362 : 46278134 : tree fn = get_function_named_in_call (t);
6363 : 46278121 : if (fn && TREE_CODE (fn) == FUNCTION_DECL
6364 : 92509019 : && DECL_CXX_CONSTRUCTOR_P (fn))
6365 : 2371333 : type = DECL_CONTEXT (fn);
6366 : : }
6367 : 340184757 : else if (TREE_CODE (t) == COMPOUND_EXPR)
6368 : 810567 : return initialized_type (TREE_OPERAND (t, 1));
6369 : 339374190 : else if (TREE_CODE (t) == AGGR_INIT_EXPR)
6370 : 504854 : type = TREE_TYPE (AGGR_INIT_EXPR_SLOT (t));
6371 : 385652324 : return cv_unqualified (type);
6372 : : }
6373 : :
6374 : : /* We're about to initialize element INDEX of an array or class from VALUE.
6375 : : Set up NEW_CTX appropriately by adjusting .object to refer to the
6376 : : subobject and creating a new CONSTRUCTOR if the element is itself
6377 : : a class or array. */
6378 : :
6379 : : static void
6380 : 1604087 : init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx,
6381 : : tree index, tree &value)
6382 : : {
6383 : 1604087 : new_ctx = *ctx;
6384 : :
6385 : 1604087 : if (index && TREE_CODE (index) != INTEGER_CST
6386 : 1468521 : && TREE_CODE (index) != FIELD_DECL
6387 : 3 : && TREE_CODE (index) != RANGE_EXPR)
6388 : : /* This won't have an element in the new CONSTRUCTOR. */
6389 : : return;
6390 : :
6391 : 1604087 : tree type = initialized_type (value);
6392 : 1604087 : if (!AGGREGATE_TYPE_P (type) && !VECTOR_TYPE_P (type))
6393 : : /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
6394 : : return;
6395 : 687108 : if (VECTOR_TYPE_P (type)
6396 : 1103 : && VECTOR_TYPE_P (TREE_TYPE (ctx->ctor))
6397 : 687144 : && index == NULL_TREE)
6398 : : /* A vector inside of a vector CONSTRUCTOR, e.g. when a larger
6399 : : vector is constructed from smaller vectors, doesn't get its own
6400 : : CONSTRUCTOR either. */
6401 : : return;
6402 : :
6403 : : /* The sub-aggregate initializer might contain a placeholder;
6404 : : update object to refer to the subobject and ctor to refer to
6405 : : the (newly created) sub-initializer. */
6406 : 687072 : if (ctx->object)
6407 : : {
6408 : 525489 : if (index == NULL_TREE || TREE_CODE (index) == RANGE_EXPR)
6409 : : /* There's no well-defined subobject for this index. */
6410 : 3 : new_ctx.object = NULL_TREE;
6411 : : else
6412 : 525486 : new_ctx.object = build_ctor_subob_ref (index, type, ctx->object);
6413 : : }
6414 : :
6415 : 687072 : if (is_empty_class (type))
6416 : : /* Leave ctor null for an empty subobject, they aren't represented in the
6417 : : result of evaluation. */
6418 : 640716 : new_ctx.ctor = NULL_TREE;
6419 : : else
6420 : : {
6421 : 46356 : tree elt = build_constructor (type, NULL);
6422 : 46356 : CONSTRUCTOR_NO_CLEARING (elt) = true;
6423 : 46356 : new_ctx.ctor = elt;
6424 : : }
6425 : :
6426 : 687072 : if (TREE_CODE (value) == TARGET_EXPR)
6427 : : /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
6428 : 35003 : value = TARGET_EXPR_INITIAL (value);
6429 : : }
6430 : :
6431 : : /* We're about to process an initializer for a class or array TYPE. Make
6432 : : sure that CTX is set up appropriately. */
6433 : :
6434 : : static void
6435 : 1228093 : verify_ctor_sanity (const constexpr_ctx *ctx, tree type)
6436 : : {
6437 : : /* We don't bother building a ctor for an empty base subobject. */
6438 : 1228093 : if (is_empty_class (type))
6439 : : return;
6440 : :
6441 : : /* We're in the middle of an initializer that might involve placeholders;
6442 : : our caller should have created a CONSTRUCTOR for us to put the
6443 : : initializer into. We will either return that constructor or T. */
6444 : 589914 : gcc_assert (ctx->ctor);
6445 : 589914 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
6446 : : (type, TREE_TYPE (ctx->ctor)));
6447 : : /* We used to check that ctx->ctor was empty, but that isn't the case when
6448 : : the object is zero-initialized before calling the constructor. */
6449 : 589914 : if (ctx->object)
6450 : : {
6451 : 451629 : tree otype = TREE_TYPE (ctx->object);
6452 : 451629 : gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, otype)
6453 : : /* Handle flexible array members. */
6454 : : || (TREE_CODE (otype) == ARRAY_TYPE
6455 : : && TYPE_DOMAIN (otype) == NULL_TREE
6456 : : && TREE_CODE (type) == ARRAY_TYPE
6457 : : && (same_type_ignoring_top_level_qualifiers_p
6458 : : (TREE_TYPE (type), TREE_TYPE (otype)))));
6459 : : }
6460 : 589914 : gcc_assert (!ctx->object || !DECL_P (ctx->object)
6461 : : || ctx->global->get_value (ctx->object) == ctx->ctor);
6462 : : }
6463 : :
6464 : : /* Subroutine of cxx_eval_constant_expression.
6465 : : The expression tree T denotes a C-style array or a C-style
6466 : : aggregate. Reduce it to a constant expression. */
6467 : :
6468 : : static tree
6469 : 1227468 : cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t,
6470 : : value_cat lval,
6471 : : bool *non_constant_p, bool *overflow_p,
6472 : : tree *jump_target)
6473 : : {
6474 : 1227468 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
6475 : 1227468 : bool changed = false;
6476 : 1227468 : gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t));
6477 : 1227468 : tree type = TREE_TYPE (t);
6478 : :
6479 : 1227468 : constexpr_ctx new_ctx;
6480 : 1227468 : if (TYPE_PTRMEMFUNC_P (type) || VECTOR_TYPE_P (type))
6481 : : {
6482 : : /* We don't really need the ctx->ctor business for a PMF or
6483 : : vector, but it's simpler to use the same code. */
6484 : 24876 : new_ctx = *ctx;
6485 : 24876 : new_ctx.ctor = build_constructor (type, NULL);
6486 : 24876 : new_ctx.object = NULL_TREE;
6487 : 24876 : ctx = &new_ctx;
6488 : 1227468 : };
6489 : 1227468 : verify_ctor_sanity (ctx, type);
6490 : 1227468 : vec<constructor_elt, va_gc> **p = nullptr;
6491 : 1227468 : if (ctx->ctor)
6492 : : {
6493 : 1227460 : p = &CONSTRUCTOR_ELTS (ctx->ctor);
6494 : 2451877 : vec_alloc (*p, vec_safe_length (v));
6495 : 1227460 : if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (t))
6496 : 16057 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor) = 1;
6497 : : }
6498 : :
6499 : 1227468 : unsigned i;
6500 : 1227468 : tree index, value;
6501 : 1227468 : bool constant_p = true;
6502 : 1227468 : bool side_effects_p = false;
6503 : 2508942 : FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value)
6504 : : {
6505 : 1598021 : tree orig_value = value;
6506 : 1598021 : init_subob_ctx (ctx, new_ctx, index, value);
6507 : : /* Like in cxx_eval_store_expression, omit entries for empty fields. */
6508 : 1598021 : bool no_slot = new_ctx.ctor == NULL_TREE;
6509 : 1598021 : int pos_hint = -1;
6510 : 1598021 : if (new_ctx.ctor != ctx->ctor && !no_slot)
6511 : : {
6512 : : /* If we built a new CONSTRUCTOR, attach it now so that other
6513 : : initializers can refer to it. */
6514 : 40470 : constructor_elt *cep = get_or_insert_ctor_field (ctx->ctor, index);
6515 : 40470 : cep->value = new_ctx.ctor;
6516 : 40470 : pos_hint = cep - (*p)->begin();
6517 : 40470 : }
6518 : 1557551 : else if (TREE_CODE (type) == UNION_TYPE)
6519 : : /* Otherwise if we're constructing a non-aggregate union member, set
6520 : : the active union member now so that we can later detect and diagnose
6521 : : if its initializer attempts to activate another member. */
6522 : 969 : get_or_insert_ctor_field (ctx->ctor, index);
6523 : 1598021 : tree elt = cxx_eval_constant_expression (&new_ctx, value,
6524 : : lval,
6525 : : non_constant_p, overflow_p,
6526 : : jump_target);
6527 : 1598021 : if (*jump_target)
6528 : : return NULL_TREE;
6529 : : /* Don't VERIFY_CONSTANT here. */
6530 : 1598021 : if (ctx->quiet && *non_constant_p)
6531 : : break;
6532 : 1281474 : if (elt != orig_value)
6533 : 173371 : changed = true;
6534 : :
6535 : 1281474 : if (!TREE_CONSTANT (elt))
6536 : 435865 : constant_p = false;
6537 : 1281474 : if (TREE_SIDE_EFFECTS (elt))
6538 : 126 : side_effects_p = true;
6539 : 1281474 : if (index && TREE_CODE (index) == COMPONENT_REF)
6540 : : {
6541 : : /* This is an initialization of a vfield inside a base
6542 : : subaggregate that we already initialized; push this
6543 : : initialization into the previous initialization. */
6544 : 0 : constructor_elt *inner = base_field_constructor_elt (*p, index);
6545 : 0 : inner->value = elt;
6546 : 0 : changed = true;
6547 : 0 : }
6548 : 1281474 : else if (no_slot)
6549 : : /* This is an initializer for an empty field; now that we've
6550 : : checked that it's constant, we can ignore it. */
6551 : : changed = true;
6552 : 641069 : else if (index
6553 : 640941 : && (TREE_CODE (index) == NOP_EXPR
6554 : 640941 : || TREE_CODE (index) == POINTER_PLUS_EXPR))
6555 : : {
6556 : : /* Old representation of empty bases. FIXME remove. */
6557 : 0 : gcc_checking_assert (false);
6558 : : gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index))));
6559 : : changed = true;
6560 : : }
6561 : : else
6562 : : {
6563 : 641069 : if (TREE_CODE (type) == UNION_TYPE
6564 : 641069 : && (*p)->last().index != index)
6565 : : /* The initializer erroneously changed the active union member that
6566 : : we're initializing. */
6567 : 8 : gcc_assert (*non_constant_p);
6568 : : else
6569 : : {
6570 : : /* The initializer might have mutated the underlying CONSTRUCTOR,
6571 : : so recompute the location of the target constructer_elt. */
6572 : 641061 : constructor_elt *cep
6573 : 641061 : = get_or_insert_ctor_field (ctx->ctor, index, pos_hint);
6574 : 641061 : cep->value = elt;
6575 : : }
6576 : :
6577 : : /* Adding or replacing an element might change the ctor's flags. */
6578 : 641069 : TREE_CONSTANT (ctx->ctor) = constant_p;
6579 : 641069 : TREE_SIDE_EFFECTS (ctx->ctor) = side_effects_p;
6580 : : }
6581 : : }
6582 : 1227468 : if (*non_constant_p)
6583 : : return t;
6584 : 910885 : if (!changed)
6585 : : {
6586 : 157571 : if (VECTOR_TYPE_P (type))
6587 : 13948 : t = fold (t);
6588 : 157571 : return t;
6589 : : }
6590 : 753314 : t = ctx->ctor;
6591 : 753314 : if (!t)
6592 : 8 : t = build_constructor (type, NULL);
6593 : : /* We're done building this CONSTRUCTOR, so now we can interpret an
6594 : : element without an explicit initializer as value-initialized. */
6595 : 753314 : CONSTRUCTOR_NO_CLEARING (t) = false;
6596 : 753314 : TREE_CONSTANT (t) = constant_p;
6597 : 753314 : TREE_SIDE_EFFECTS (t) = side_effects_p;
6598 : 753314 : if (VECTOR_TYPE_P (type))
6599 : 1070 : t = fold (t);
6600 : : return t;
6601 : : }
6602 : :
6603 : : /* Subroutine of cxx_eval_constant_expression.
6604 : : The expression tree T is a VEC_INIT_EXPR which denotes the desired
6605 : : initialization of a non-static data member of array type. Reduce it to a
6606 : : CONSTRUCTOR.
6607 : :
6608 : : Note that apart from value-initialization (when VALUE_INIT is true),
6609 : : this is only intended to support value-initialization and the
6610 : : initializations done by defaulted constructors for classes with
6611 : : non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
6612 : : will either be NULL_TREE for the default constructor, or a COMPONENT_REF
6613 : : for the copy/move constructor. */
6614 : :
6615 : : static tree
6616 : 625 : cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init,
6617 : : bool value_init, value_cat lval,
6618 : : bool *non_constant_p, bool *overflow_p,
6619 : : tree *jump_target)
6620 : : {
6621 : 625 : tree elttype = TREE_TYPE (atype);
6622 : 625 : verify_ctor_sanity (ctx, atype);
6623 : 625 : vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
6624 : 625 : bool pre_init = false;
6625 : 625 : unsigned HOST_WIDE_INT i;
6626 : 625 : tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
6627 : :
6628 : 625 : if (init && TREE_CODE (init) == CONSTRUCTOR)
6629 : 0 : return cxx_eval_bare_aggregate (ctx, init, lval,
6630 : 0 : non_constant_p, overflow_p, jump_target);
6631 : :
6632 : : /* We already checked access when building the VEC_INIT_EXPR. */
6633 : 625 : deferring_access_check_sentinel acs (dk_deferred);
6634 : :
6635 : : /* For the default constructor, build up a call to the default
6636 : : constructor of the element type. We only need to handle class types
6637 : : here, as for a constructor to be constexpr, all members must be
6638 : : initialized, which for a defaulted default constructor means they must
6639 : : be of a class type with a constexpr default constructor. */
6640 : 625 : if (TREE_CODE (elttype) == ARRAY_TYPE)
6641 : : /* We only do this at the lowest level. */;
6642 : 581 : else if (value_init)
6643 : : {
6644 : 174 : init = build_value_init (elttype, complain);
6645 : 174 : pre_init = true;
6646 : : }
6647 : 407 : else if (!init)
6648 : : {
6649 : 322 : releasing_vec argvec;
6650 : 322 : init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
6651 : : &argvec, elttype, LOOKUP_NORMAL,
6652 : : complain);
6653 : 322 : init = build_aggr_init_expr (elttype, init);
6654 : 322 : pre_init = true;
6655 : 322 : }
6656 : :
6657 : 625 : bool zeroed_out = false;
6658 : 625 : if (!CONSTRUCTOR_NO_CLEARING (ctx->ctor))
6659 : : {
6660 : : /* We're initializing an array object that had been zero-initialized
6661 : : earlier. Truncate ctx->ctor, and propagate its zeroed state by
6662 : : clearing CONSTRUCTOR_NO_CLEARING on each of the aggregate element
6663 : : initializers we append to it. */
6664 : 156 : gcc_checking_assert (initializer_zerop (ctx->ctor));
6665 : 156 : zeroed_out = true;
6666 : 156 : vec_safe_truncate (*p, 0);
6667 : : }
6668 : :
6669 : 625 : tree nelts = get_array_or_vector_nelts (ctx, atype, non_constant_p,
6670 : : overflow_p, jump_target);
6671 : 625 : if (*jump_target)
6672 : : return NULL_TREE;
6673 : 625 : unsigned HOST_WIDE_INT max = tree_to_uhwi (nelts);
6674 : 6273 : for (i = 0; i < max; ++i)
6675 : : {
6676 : 6066 : tree idx = build_int_cst (size_type_node, i);
6677 : 6066 : tree eltinit;
6678 : 6066 : bool reuse = false;
6679 : 6066 : constexpr_ctx new_ctx;
6680 : 6401 : init_subob_ctx (ctx, new_ctx, idx, pre_init ? init : elttype);
6681 : 6066 : bool no_slot = new_ctx.ctor == NULL_TREE;
6682 : 6066 : if (new_ctx.ctor != ctx->ctor && !no_slot)
6683 : : {
6684 : 5886 : if (zeroed_out)
6685 : 5445 : CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = false;
6686 : 5886 : CONSTRUCTOR_APPEND_ELT (*p, idx, new_ctx.ctor);
6687 : : }
6688 : 6066 : if (TREE_CODE (elttype) == ARRAY_TYPE)
6689 : : {
6690 : : /* A multidimensional array; recurse. */
6691 : 176 : if (value_init || init == NULL_TREE)
6692 : : {
6693 : 168 : eltinit = NULL_TREE;
6694 : 168 : reuse = i == 0;
6695 : : }
6696 : : else
6697 : 8 : eltinit = cp_build_array_ref (input_location, init, idx, complain);
6698 : 176 : eltinit = cxx_eval_vec_init_1 (&new_ctx, elttype, eltinit,
6699 : : value_init, lval, non_constant_p,
6700 : : overflow_p, jump_target);
6701 : : }
6702 : 5890 : else if (pre_init)
6703 : : {
6704 : : /* Initializing an element using value or default initialization
6705 : : we just pre-built above. */
6706 : 5731 : if (init == void_node)
6707 : : /* Trivial default-init, don't do anything to the CONSTRUCTOR. */
6708 : 3 : return ctx->ctor;
6709 : 5728 : eltinit = init;
6710 : 5728 : if (CLASS_TYPE_P (elttype) && new_ctx.object)
6711 : : /* Clarify what object is being initialized (118285). */
6712 : 5591 : eltinit = build2 (INIT_EXPR, elttype, new_ctx.object, eltinit);
6713 : 5728 : eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval,
6714 : : non_constant_p, overflow_p,
6715 : : jump_target);
6716 : 5728 : reuse = i == 0;
6717 : : }
6718 : : else
6719 : : {
6720 : : /* Copying an element. */
6721 : 159 : eltinit = cp_build_array_ref (input_location, init, idx, complain);
6722 : 159 : if (!lvalue_p (init))
6723 : 132 : eltinit = move (eltinit);
6724 : 159 : eltinit = (perform_implicit_conversion_flags
6725 : 159 : (elttype, eltinit, complain,
6726 : : LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING));
6727 : 159 : if (CLASS_TYPE_P (elttype) && new_ctx.object)
6728 : : /* Clarify what object is being initialized (118285). */
6729 : 142 : eltinit = build2 (INIT_EXPR, elttype, new_ctx.object, eltinit);
6730 : 159 : eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval,
6731 : : non_constant_p, overflow_p,
6732 : : jump_target);
6733 : : }
6734 : 6063 : if (*jump_target)
6735 : : return NULL_TREE;
6736 : 6063 : if (*non_constant_p)
6737 : : break;
6738 : 5965 : if (no_slot)
6739 : : {
6740 : : /* This is an initializer for an empty subobject; now that we've
6741 : : checked that it's constant, we can ignore it. */
6742 : 18 : gcc_checking_assert (i == 0);
6743 : : break;
6744 : : }
6745 : 5947 : else if (new_ctx.ctor != ctx->ctor)
6746 : : {
6747 : : /* We appended this element above; update the value. */
6748 : 5816 : gcc_assert ((*p)->last().index == idx);
6749 : 5816 : (*p)->last().value = eltinit;
6750 : : }
6751 : : else
6752 : 131 : CONSTRUCTOR_APPEND_ELT (*p, idx, eltinit);
6753 : : /* Reuse the result of cxx_eval_constant_expression call
6754 : : from the first iteration to all others if it is a constant
6755 : : initializer that doesn't require relocations. */
6756 : 5947 : if (reuse
6757 : 5947 : && max > 1
6758 : 5947 : && (eltinit == NULL_TREE
6759 : 452 : || (initializer_constant_valid_p (eltinit, TREE_TYPE (eltinit))
6760 : 452 : == null_pointer_node)))
6761 : : {
6762 : 299 : if (new_ctx.ctor != ctx->ctor)
6763 : 174 : eltinit = new_ctx.ctor;
6764 : 299 : tree range = build2 (RANGE_EXPR, size_type_node,
6765 : : build_int_cst (size_type_node, 1),
6766 : 299 : build_int_cst (size_type_node, max - 1));
6767 : 299 : CONSTRUCTOR_APPEND_ELT (*p, range, unshare_constructor (eltinit));
6768 : 299 : break;
6769 : : }
6770 : 5648 : else if (i == 0)
6771 : 204 : vec_safe_reserve (*p, max);
6772 : : }
6773 : :
6774 : 622 : if (!*non_constant_p)
6775 : : {
6776 : 524 : init = ctx->ctor;
6777 : 524 : CONSTRUCTOR_NO_CLEARING (init) = false;
6778 : : }
6779 : 622 : return init;
6780 : : }
6781 : :
6782 : : static tree
6783 : 512 : cxx_eval_vec_init (const constexpr_ctx *ctx, tree t,
6784 : : value_cat lval,
6785 : : bool *non_constant_p, bool *overflow_p, tree *jump_target)
6786 : : {
6787 : 512 : tree atype = TREE_TYPE (t);
6788 : 512 : tree init = VEC_INIT_EXPR_INIT (t);
6789 : 512 : bool value_init = VEC_INIT_EXPR_VALUE_INIT (t);
6790 : 512 : if (!init || !BRACE_ENCLOSED_INITIALIZER_P (init))
6791 : : ;
6792 : 73 : else if (CONSTRUCTOR_NELTS (init) == 0
6793 : 73 : && !CP_AGGREGATE_TYPE_P (strip_array_types (atype)))
6794 : : {
6795 : : /* Handle {} as value-init. */
6796 : : init = NULL_TREE;
6797 : : value_init = true;
6798 : : }
6799 : : else
6800 : : {
6801 : : /* This is a more complicated case, like needing to loop over trailing
6802 : : elements; call build_vec_init and evaluate the result. */
6803 : 63 : tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
6804 : 63 : constexpr_ctx new_ctx = *ctx;
6805 : 63 : if (!ctx->object)
6806 : : {
6807 : : /* We want to have an initialization target for an VEC_INIT_EXPR.
6808 : : If we don't already have one in CTX, use the VEC_INIT_EXPR_SLOT. */
6809 : 51 : new_ctx.object = VEC_INIT_EXPR_SLOT (t);
6810 : 51 : tree ctor = new_ctx.ctor = build_constructor (atype, NULL);
6811 : 51 : CONSTRUCTOR_NO_CLEARING (ctor) = true;
6812 : 51 : ctx->global->put_value (new_ctx.object, ctor);
6813 : 51 : ctx = &new_ctx;
6814 : : }
6815 : 63 : init = expand_vec_init_expr (ctx->object, t, complain);
6816 : 63 : return cxx_eval_constant_expression (ctx, init, lval, non_constant_p,
6817 : : overflow_p, jump_target);
6818 : : }
6819 : 449 : tree r = cxx_eval_vec_init_1 (ctx, atype, init, value_init,
6820 : : lval, non_constant_p, overflow_p, jump_target);
6821 : 449 : if (*non_constant_p)
6822 : : return t;
6823 : : else
6824 : 370 : return r;
6825 : : }
6826 : :
6827 : : /* Like same_type_ignoring_top_level_qualifiers_p, but also handle the case
6828 : : where the desired type is an array of unknown bounds because the variable
6829 : : has had its bounds deduced since the wrapping expression was created. */
6830 : :
6831 : : static bool
6832 : 151794052 : same_type_ignoring_tlq_and_bounds_p (tree type1, tree type2)
6833 : : {
6834 : 151794052 : while (TREE_CODE (type1) == ARRAY_TYPE
6835 : 13295 : && TREE_CODE (type2) == ARRAY_TYPE
6836 : 151794056 : && (!TYPE_DOMAIN (type1) || !TYPE_DOMAIN (type2)))
6837 : : {
6838 : 2 : type1 = TREE_TYPE (type1);
6839 : 2 : type2 = TREE_TYPE (type2);
6840 : : }
6841 : 151794052 : return same_type_ignoring_top_level_qualifiers_p (type1, type2);
6842 : : }
6843 : :
6844 : : /* Try to determine the currently active union member for an expression
6845 : : with UNION_TYPE. If it can be determined, return the FIELD_DECL,
6846 : : otherwise return NULL_TREE. */
6847 : :
6848 : : static tree
6849 : 75 : cxx_union_active_member (const constexpr_ctx *ctx, tree t, tree *jump_target)
6850 : : {
6851 : 75 : constexpr_ctx new_ctx = *ctx;
6852 : 75 : new_ctx.quiet = true;
6853 : 75 : bool non_constant_p = false, overflow_p = false;
6854 : 75 : tree ctor = cxx_eval_constant_expression (&new_ctx, t, vc_prvalue,
6855 : : &non_constant_p,
6856 : : &overflow_p, jump_target);
6857 : 75 : if (*jump_target)
6858 : : return NULL_TREE;
6859 : 75 : if (TREE_CODE (ctor) == CONSTRUCTOR
6860 : 24 : && CONSTRUCTOR_NELTS (ctor) == 1
6861 : 12 : && CONSTRUCTOR_ELT (ctor, 0)->index
6862 : 87 : && TREE_CODE (CONSTRUCTOR_ELT (ctor, 0)->index) == FIELD_DECL)
6863 : : return CONSTRUCTOR_ELT (ctor, 0)->index;
6864 : : return NULL_TREE;
6865 : : }
6866 : :
6867 : : /* Helper function for cxx_fold_indirect_ref_1, called recursively. */
6868 : :
6869 : : static tree
6870 : 3729093 : cxx_fold_indirect_ref_1 (const constexpr_ctx *ctx, location_t loc, tree type,
6871 : : tree op, unsigned HOST_WIDE_INT off, bool *empty_base,
6872 : : tree *jump_target)
6873 : : {
6874 : 5968798 : tree optype = TREE_TYPE (op);
6875 : 5968798 : unsigned HOST_WIDE_INT const_nunits;
6876 : 5968798 : if (off == 0 && similar_type_p (optype, type))
6877 : : return op;
6878 : 3725435 : else if (cxx_dialect >= cxx26
6879 : 2109876 : && VAR_P (op)
6880 : 774542 : && DECL_VTABLE_OR_VTT_P (op)
6881 : 12329 : && same_type_ignoring_top_level_qualifiers_p (type,
6882 : : ptrdiff_type_node)
6883 : 3726417 : && POINTER_TYPE_P (strip_array_types (optype)))
6884 : : {
6885 : : /* We often read some virtual table elements using ptrdiff_t rather
6886 : : than pointer type. */
6887 : 982 : if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc,
6888 : : strip_array_types (optype),
6889 : : op, off, empty_base,
6890 : : jump_target))
6891 : 982 : return fold_convert (type, ret);
6892 : : }
6893 : 3724453 : else if (TREE_CODE (optype) == COMPLEX_TYPE
6894 : 3724453 : && similar_type_p (type, TREE_TYPE (optype)))
6895 : : {
6896 : : /* *(foo *)&complexfoo => __real__ complexfoo */
6897 : 0 : if (off == 0)
6898 : 0 : return build1_loc (loc, REALPART_EXPR, type, op);
6899 : : /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
6900 : 0 : else if (tree_to_uhwi (TYPE_SIZE_UNIT (type)) == off)
6901 : 0 : return build1_loc (loc, IMAGPART_EXPR, type, op);
6902 : : }
6903 : : /* ((foo*)&vectorfoo)[x] => BIT_FIELD_REF<vectorfoo,...> */
6904 : 3724453 : else if (VECTOR_TYPE_P (optype)
6905 : 0 : && similar_type_p (type, TREE_TYPE (optype))
6906 : 3724453 : && TYPE_VECTOR_SUBPARTS (optype).is_constant (&const_nunits))
6907 : : {
6908 : 0 : unsigned HOST_WIDE_INT part_width = tree_to_uhwi (TYPE_SIZE_UNIT (type));
6909 : 0 : unsigned HOST_WIDE_INT max_offset = part_width * const_nunits;
6910 : 0 : if (off < max_offset && off % part_width == 0)
6911 : : {
6912 : 0 : tree index = bitsize_int (off * BITS_PER_UNIT);
6913 : 0 : return build3_loc (loc, BIT_FIELD_REF, type, op,
6914 : 0 : TYPE_SIZE (type), index);
6915 : : }
6916 : : }
6917 : : /* ((foo *)&fooarray)[x] => fooarray[x] */
6918 : 3724453 : else if (TREE_CODE (optype) == ARRAY_TYPE
6919 : 2239705 : && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (optype)))
6920 : 5964158 : && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (optype))))
6921 : : {
6922 : 2239705 : tree type_domain = TYPE_DOMAIN (optype);
6923 : 2239705 : tree min_val = size_zero_node;
6924 : 2239705 : if (type_domain && TYPE_MIN_VALUE (type_domain))
6925 : 2239700 : min_val = TYPE_MIN_VALUE (type_domain);
6926 : 2239705 : unsigned HOST_WIDE_INT el_sz
6927 : 2239705 : = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (optype)));
6928 : 2239705 : unsigned HOST_WIDE_INT idx = off / el_sz;
6929 : 2239705 : unsigned HOST_WIDE_INT rem = off % el_sz;
6930 : 2239705 : if (tree_fits_uhwi_p (min_val))
6931 : : {
6932 : 2239705 : tree index = size_int (idx + tree_to_uhwi (min_val));
6933 : 2239705 : op = build4_loc (loc, ARRAY_REF, TREE_TYPE (optype), op, index,
6934 : : NULL_TREE, NULL_TREE);
6935 : 2239705 : return cxx_fold_indirect_ref_1 (ctx, loc, type, op, rem,
6936 : 2239705 : empty_base, jump_target);
6937 : : }
6938 : : }
6939 : : /* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */
6940 : 1484748 : else if (TREE_CODE (optype) == RECORD_TYPE
6941 : 1484748 : || TREE_CODE (optype) == UNION_TYPE)
6942 : : {
6943 : 1481231 : if (TREE_CODE (optype) == UNION_TYPE)
6944 : : /* For unions prefer the currently active member. */
6945 : 75 : if (tree field = cxx_union_active_member (ctx, op, jump_target))
6946 : : {
6947 : 12 : unsigned HOST_WIDE_INT el_sz
6948 : 12 : = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field)));
6949 : 12 : if (off < el_sz)
6950 : : {
6951 : 12 : tree cop = build3 (COMPONENT_REF, TREE_TYPE (field),
6952 : : op, field, NULL_TREE);
6953 : 12 : if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop,
6954 : : off, empty_base,
6955 : : jump_target))
6956 : : return ret;
6957 : : }
6958 : : }
6959 : :
6960 : : /* Handle conversion to "as base" type. */
6961 : 1481225 : if (CLASS_TYPE_P (optype)
6962 : 2962286 : && CLASSTYPE_AS_BASE (optype) == type)
6963 : : return op;
6964 : :
6965 : : /* Handle conversion to an empty base class, which is represented with a
6966 : : NOP_EXPR. Do this before spelunking into the non-empty subobjects,
6967 : : which is likely to be a waste of time (109678). */
6968 : 1480648 : if (is_empty_class (type)
6969 : 1440028 : && CLASS_TYPE_P (optype)
6970 : 2920670 : && lookup_base (optype, type, ba_any, NULL, tf_none, off))
6971 : : {
6972 : 844884 : if (empty_base)
6973 : 844884 : *empty_base = true;
6974 : 844884 : return op;
6975 : : }
6976 : :
6977 : 635764 : for (tree field = TYPE_FIELDS (optype);
6978 : 6200257 : field; field = DECL_CHAIN (field))
6979 : 6191089 : if (TREE_CODE (field) == FIELD_DECL
6980 : 641704 : && TREE_TYPE (field) != error_mark_node
6981 : 6832793 : && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (field))))
6982 : : {
6983 : 641704 : tree pos = byte_position (field);
6984 : 641704 : if (!tree_fits_uhwi_p (pos))
6985 : 0 : continue;
6986 : 641704 : unsigned HOST_WIDE_INT upos = tree_to_uhwi (pos);
6987 : 641704 : unsigned HOST_WIDE_INT el_sz;
6988 : 641704 : if (DECL_FIELD_IS_BASE (field)
6989 : 41402 : && CLASS_TYPE_P (optype)
6990 : 683106 : && CLASSTYPE_VBASECLASSES (optype))
6991 : 6189 : el_sz = tree_to_uhwi (DECL_SIZE_UNIT (field));
6992 : : else
6993 : 635515 : el_sz = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field)));
6994 : 641704 : if (upos <= off && off < upos + el_sz)
6995 : : {
6996 : 635674 : tree cop = build3 (COMPONENT_REF, TREE_TYPE (field),
6997 : : op, field, NULL_TREE);
6998 : 635674 : if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop,
6999 : : off - upos,
7000 : : empty_base,
7001 : : jump_target))
7002 : : return ret;
7003 : : }
7004 : : }
7005 : : }
7006 : :
7007 : : return NULL_TREE;
7008 : : }
7009 : :
7010 : : /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
7011 : : match. We want to be less strict for simple *& folding; if we have a
7012 : : non-const temporary that we access through a const pointer, that should
7013 : : work. We handle this here rather than change fold_indirect_ref_1
7014 : : because we're dealing with things like ADDR_EXPR of INTEGER_CST which
7015 : : don't really make sense outside of constant expression evaluation. Also
7016 : : we want to allow folding to COMPONENT_REF, which could cause trouble
7017 : : with TBAA in fold_indirect_ref_1. */
7018 : :
7019 : : static tree
7020 : 63119403 : cxx_fold_indirect_ref (const constexpr_ctx *ctx, location_t loc, tree type,
7021 : : tree op0, bool *empty_base, tree *jump_target)
7022 : : {
7023 : 63119403 : tree sub = op0;
7024 : 63119403 : tree subtype;
7025 : :
7026 : : /* STRIP_NOPS, but stop if REINTERPRET_CAST_P. */
7027 : 131477202 : while (CONVERT_EXPR_P (sub) || TREE_CODE (sub) == NON_LVALUE_EXPR
7028 : 176498693 : || TREE_CODE (sub) == VIEW_CONVERT_EXPR)
7029 : : {
7030 : 48639809 : if (TREE_CODE (sub) == NOP_EXPR
7031 : 48639809 : && REINTERPRET_CAST_P (sub))
7032 : : return NULL_TREE;
7033 : 48639809 : sub = TREE_OPERAND (sub, 0);
7034 : : }
7035 : :
7036 : 63119403 : subtype = TREE_TYPE (sub);
7037 : 63119403 : if (!INDIRECT_TYPE_P (subtype))
7038 : : return NULL_TREE;
7039 : :
7040 : : /* Canonicalizes the given OBJ/OFF pair by iteratively absorbing
7041 : : the innermost component into the offset until it would make the
7042 : : offset positive, so that cxx_fold_indirect_ref_1 can identify
7043 : : more folding opportunities. */
7044 : 66211776 : auto canonicalize_obj_off = [] (tree& obj, tree& off) {
7045 : 3092425 : if (cxx_dialect >= cxx26)
7046 : : {
7047 : : /* For C++26, we need to fold *(B *)(&x.D.1234 + 32) used
7048 : : to access virtual base members. */
7049 : 1718194 : tree nobj = obj;
7050 : 1718194 : while (TREE_CODE (nobj) == COMPONENT_REF
7051 : 1732938 : && DECL_FIELD_IS_BASE (TREE_OPERAND (nobj, 1)))
7052 : 14744 : nobj = TREE_OPERAND (nobj, 0);
7053 : 1718194 : if (nobj != obj
7054 : 14286 : && CLASS_TYPE_P (TREE_TYPE (nobj))
7055 : 1732480 : && CLASSTYPE_VBASECLASSES (TREE_TYPE (nobj)))
7056 : 2039 : while (obj != nobj)
7057 : : {
7058 : 1174 : tree field = TREE_OPERAND (obj, 1);
7059 : 1174 : tree pos = byte_position (field);
7060 : 1174 : off = int_const_binop (PLUS_EXPR, off, pos);
7061 : 1174 : obj = TREE_OPERAND (obj, 0);
7062 : : }
7063 : : }
7064 : 3730393 : while (TREE_CODE (obj) == COMPONENT_REF
7065 : : /* We need to preserve union member accesses so that we can
7066 : : later properly diagnose accessing the wrong member. */
7067 : 1456482 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (obj, 0))) == RECORD_TYPE
7068 : 5078935 : && (tree_int_cst_sign_bit (off) || integer_zerop (off)))
7069 : : {
7070 : 641606 : tree field = TREE_OPERAND (obj, 1);
7071 : 641606 : tree pos = byte_position (field);
7072 : 641606 : if (integer_zerop (off) && integer_nonzerop (pos))
7073 : : /* If the offset is already 0, keep going as long as the
7074 : : component is at position 0. */
7075 : : break;
7076 : 637968 : off = int_const_binop (PLUS_EXPR, off, pos);
7077 : 637968 : obj = TREE_OPERAND (obj, 0);
7078 : : }
7079 : 3092425 : };
7080 : :
7081 : 63119351 : if (TREE_CODE (sub) == ADDR_EXPR)
7082 : : {
7083 : 26961973 : tree op = TREE_OPERAND (sub, 0);
7084 : 26961973 : tree optype = TREE_TYPE (op);
7085 : :
7086 : : /* *&CONST_DECL -> to the value of the const decl. */
7087 : 26961973 : if (TREE_CODE (op) == CONST_DECL)
7088 : 0 : return DECL_INITIAL (op);
7089 : : /* *&p => p; make sure to handle *&"str"[cst] here. */
7090 : 26961973 : if (similar_type_p (optype, type))
7091 : : {
7092 : 25748230 : tree fop = fold_read_from_constant_string (op);
7093 : 25748230 : if (fop)
7094 : : return fop;
7095 : : else
7096 : 25738138 : return op;
7097 : : }
7098 : : else
7099 : : {
7100 : 1213743 : tree off = integer_zero_node;
7101 : 1213743 : canonicalize_obj_off (op, off);
7102 : 1213743 : return cxx_fold_indirect_ref_1 (ctx, loc, type, op,
7103 : : tree_to_uhwi (off), empty_base,
7104 : : jump_target);
7105 : : }
7106 : : }
7107 : 36157378 : else if (TREE_CODE (sub) == POINTER_PLUS_EXPR
7108 : 36157378 : && tree_fits_uhwi_p (TREE_OPERAND (sub, 1)))
7109 : : {
7110 : 2027302 : tree op00 = TREE_OPERAND (sub, 0);
7111 : 2027302 : tree off = TREE_OPERAND (sub, 1);
7112 : :
7113 : 2027302 : STRIP_NOPS (op00);
7114 : 2027302 : if (TREE_CODE (op00) == ADDR_EXPR)
7115 : : {
7116 : 1878682 : tree obj = TREE_OPERAND (op00, 0);
7117 : 1878682 : canonicalize_obj_off (obj, off);
7118 : 1878682 : return cxx_fold_indirect_ref_1 (ctx, loc, type, obj,
7119 : : tree_to_uhwi (off), empty_base,
7120 : : jump_target);
7121 : : }
7122 : : }
7123 : : /* *(foo *)fooarrptr => (*fooarrptr)[0] */
7124 : 34130076 : else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
7125 : 34130076 : && similar_type_p (type, TREE_TYPE (TREE_TYPE (subtype))))
7126 : : {
7127 : 0 : tree type_domain;
7128 : 0 : tree min_val = size_zero_node;
7129 : 0 : tree newsub
7130 : 0 : = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (subtype), sub, NULL,
7131 : : jump_target);
7132 : 0 : if (*jump_target)
7133 : : return NULL_TREE;
7134 : 0 : if (newsub)
7135 : : sub = newsub;
7136 : : else
7137 : 0 : sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub);
7138 : 0 : type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
7139 : 0 : if (type_domain && TYPE_MIN_VALUE (type_domain))
7140 : 0 : min_val = TYPE_MIN_VALUE (type_domain);
7141 : 0 : return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
7142 : 0 : NULL_TREE);
7143 : : }
7144 : :
7145 : : return NULL_TREE;
7146 : : }
7147 : :
7148 : : static tree
7149 : 35475675 : cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t,
7150 : : value_cat lval,
7151 : : bool *non_constant_p, bool *overflow_p,
7152 : : tree *jump_target)
7153 : : {
7154 : 35475675 : tree orig_op0 = TREE_OPERAND (t, 0);
7155 : 35475675 : bool empty_base = false;
7156 : :
7157 : : /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second
7158 : : operand is an integer-zero. Otherwise reject the MEM_REF for now. */
7159 : :
7160 : 35475675 : if (TREE_CODE (t) == MEM_REF
7161 : 35475675 : && (!TREE_OPERAND (t, 1) || !integer_zerop (TREE_OPERAND (t, 1))))
7162 : : {
7163 : 9 : gcc_assert (ctx->quiet);
7164 : 9 : *non_constant_p = true;
7165 : 9 : return t;
7166 : : }
7167 : :
7168 : : /* First try to simplify it directly. */
7169 : 35475666 : tree r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t),
7170 : : orig_op0, &empty_base, jump_target);
7171 : 35475666 : if (*jump_target)
7172 : : return NULL_TREE;
7173 : 35475666 : if (!r)
7174 : : {
7175 : : /* If that didn't work, evaluate the operand first. */
7176 : 34058749 : tree op0 = cxx_eval_constant_expression (ctx, orig_op0,
7177 : : vc_prvalue, non_constant_p,
7178 : : overflow_p, jump_target);
7179 : 34058749 : if (*jump_target)
7180 : : return NULL_TREE;
7181 : : /* Don't VERIFY_CONSTANT here. */
7182 : 34058741 : if (*non_constant_p)
7183 : : return t;
7184 : :
7185 : 22571985 : if (!lval && integer_zerop (op0))
7186 : : {
7187 : 68 : if (!ctx->quiet)
7188 : 12 : error ("dereferencing a null pointer");
7189 : 68 : *non_constant_p = true;
7190 : 68 : return t;
7191 : : }
7192 : :
7193 : 22571917 : r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t), op0,
7194 : : &empty_base, jump_target);
7195 : 22571917 : if (*jump_target)
7196 : : return NULL_TREE;
7197 : 22571917 : if (r == NULL_TREE)
7198 : : {
7199 : : /* We couldn't fold to a constant value. Make sure it's not
7200 : : something we should have been able to fold. */
7201 : 223565 : tree sub = op0;
7202 : 223565 : STRIP_NOPS (sub);
7203 : 223565 : if (TREE_CODE (sub) == ADDR_EXPR)
7204 : : {
7205 : 1724 : gcc_assert (!similar_type_p
7206 : : (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t)));
7207 : : /* DR 1188 says we don't have to deal with this. */
7208 : 1724 : if (!ctx->quiet)
7209 : : {
7210 : 8 : auto_diagnostic_group d;
7211 : 13 : error_at (cp_expr_loc_or_input_loc (t),
7212 : : "accessing value of %qT object through a %qT "
7213 : : "glvalue in a constant expression",
7214 : 8 : TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t));
7215 : 8 : tree ob = build_fold_indirect_ref (sub);
7216 : 8 : if (DECL_P (ob))
7217 : : {
7218 : 8 : if (DECL_ARTIFICIAL (ob))
7219 : 1 : inform (DECL_SOURCE_LOCATION (ob),
7220 : 1 : "%qT object created here", TREE_TYPE (ob));
7221 : : else
7222 : 7 : inform (DECL_SOURCE_LOCATION (ob),
7223 : : "%q#D declared here", ob);
7224 : : }
7225 : 8 : }
7226 : 1724 : *non_constant_p = true;
7227 : 1724 : return t;
7228 : : }
7229 : :
7230 : 221841 : if (lval == vc_glvalue && op0 != orig_op0)
7231 : 19539 : return build1 (INDIRECT_REF, TREE_TYPE (t), op0);
7232 : 202302 : if (!lval)
7233 : 144565 : VERIFY_CONSTANT (t);
7234 : 202302 : return t;
7235 : : }
7236 : : }
7237 : :
7238 : 23765269 : r = cxx_eval_constant_expression (ctx, r,
7239 : : lval, non_constant_p, overflow_p,
7240 : : jump_target);
7241 : 23765268 : if (*jump_target)
7242 : : return NULL_TREE;
7243 : 23765268 : if (*non_constant_p)
7244 : : return t;
7245 : :
7246 : : /* If we're pulling out the value of an empty base, just return an empty
7247 : : CONSTRUCTOR. */
7248 : 17813853 : if (empty_base && !lval)
7249 : : {
7250 : 19220 : r = build_constructor (TREE_TYPE (t), NULL);
7251 : 19220 : TREE_CONSTANT (r) = true;
7252 : : }
7253 : :
7254 : : return r;
7255 : : }
7256 : :
7257 : : /* Complain about R, a DECL that is accessed outside its lifetime. */
7258 : :
7259 : : static void
7260 : 34 : outside_lifetime_error (location_t loc, tree r)
7261 : : {
7262 : 34 : auto_diagnostic_group d;
7263 : 34 : if (DECL_NAME (r) == heap_deleted_identifier)
7264 : : {
7265 : : /* Provide a more accurate message for deleted variables. */
7266 : 6 : error_at (loc, "use of allocated storage after deallocation "
7267 : : "in a constant expression");
7268 : 6 : inform (DECL_SOURCE_LOCATION (r), "allocated here");
7269 : : }
7270 : : else
7271 : : {
7272 : 28 : error_at (loc, "accessing %qE outside its lifetime", r);
7273 : 28 : inform (DECL_SOURCE_LOCATION (r), "declared here");
7274 : : }
7275 : 34 : }
7276 : :
7277 : : /* Complain about R, a VAR_DECL, not being usable in a constant expression.
7278 : : FUNDEF_P is true if we're checking a constexpr function body.
7279 : : Shared between potential_constant_expression and
7280 : : cxx_eval_constant_expression. */
7281 : :
7282 : : static void
7283 : 328 : non_const_var_error (location_t loc, tree r, bool fundef_p)
7284 : : {
7285 : 328 : auto_diagnostic_group d;
7286 : 328 : tree type = TREE_TYPE (r);
7287 : 328 : if (DECL_NAME (r) == heap_uninit_identifier
7288 : 328 : || DECL_NAME (r) == heap_identifier
7289 : 325 : || DECL_NAME (r) == heap_vec_uninit_identifier
7290 : 653 : || DECL_NAME (r) == heap_vec_identifier)
7291 : : {
7292 : 3 : if (constexpr_error (loc, fundef_p, "the content of uninitialized "
7293 : : "storage is not usable in a constant expression"))
7294 : 3 : inform (DECL_SOURCE_LOCATION (r), "allocated here");
7295 : 3 : return;
7296 : : }
7297 : 325 : if (DECL_NAME (r) == heap_deleted_identifier)
7298 : : {
7299 : 0 : if (constexpr_error (loc, fundef_p, "use of allocated storage after "
7300 : : "deallocation in a constant expression"))
7301 : 0 : inform (DECL_SOURCE_LOCATION (r), "allocated here");
7302 : 0 : return;
7303 : : }
7304 : 325 : if (!constexpr_error (loc, fundef_p, "the value of %qD is not usable in "
7305 : : "a constant expression", r))
7306 : : return;
7307 : : /* Avoid error cascade. */
7308 : 324 : if (DECL_INITIAL (r) == error_mark_node)
7309 : : return;
7310 : 310 : if (DECL_DECLARED_CONSTEXPR_P (r))
7311 : 3 : inform (DECL_SOURCE_LOCATION (r),
7312 : : "%qD used in its own initializer", r);
7313 : 307 : else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
7314 : : {
7315 : 216 : if (!CP_TYPE_CONST_P (type))
7316 : 187 : inform (DECL_SOURCE_LOCATION (r),
7317 : : "%q#D is not const", r);
7318 : 29 : else if (CP_TYPE_VOLATILE_P (type))
7319 : 0 : inform (DECL_SOURCE_LOCATION (r),
7320 : : "%q#D is volatile", r);
7321 : 29 : else if (!DECL_INITIAL (r)
7322 : 9 : || !TREE_CONSTANT (DECL_INITIAL (r))
7323 : 37 : || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r))
7324 : 29 : inform (DECL_SOURCE_LOCATION (r),
7325 : : "%qD was not initialized with a constant "
7326 : : "expression", r);
7327 : : else
7328 : 0 : gcc_unreachable ();
7329 : : }
7330 : 91 : else if (TYPE_REF_P (type))
7331 : 9 : inform (DECL_SOURCE_LOCATION (r),
7332 : : "%qD was not initialized with a constant "
7333 : : "expression", r);
7334 : : else
7335 : : {
7336 : 82 : if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r))
7337 : 82 : inform (DECL_SOURCE_LOCATION (r),
7338 : : "%qD was not declared %<constexpr%>", r);
7339 : : else
7340 : 0 : inform (DECL_SOURCE_LOCATION (r),
7341 : : "%qD does not have integral or enumeration type",
7342 : : r);
7343 : : }
7344 : 328 : }
7345 : :
7346 : : /* Subroutine of cxx_eval_constant_expression.
7347 : : Like cxx_eval_unary_expression, except for trinary expressions. */
7348 : :
7349 : : static tree
7350 : 16 : cxx_eval_trinary_expression (const constexpr_ctx *ctx, tree t,
7351 : : value_cat lval,
7352 : : bool *non_constant_p, bool *overflow_p,
7353 : : tree *jump_target)
7354 : : {
7355 : 16 : int i;
7356 : 16 : tree args[3];
7357 : 16 : tree val;
7358 : :
7359 : 37 : for (i = 0; i < 3; i++)
7360 : : {
7361 : 30 : args[i] = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, i),
7362 : : lval,
7363 : : non_constant_p, overflow_p,
7364 : : jump_target);
7365 : 30 : if (*jump_target)
7366 : : return NULL_TREE;
7367 : 30 : VERIFY_CONSTANT (args[i]);
7368 : : }
7369 : :
7370 : 7 : val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t),
7371 : : args[0], args[1], args[2]);
7372 : 7 : if (val == NULL_TREE)
7373 : : return t;
7374 : 7 : VERIFY_CONSTANT (val);
7375 : : return val;
7376 : : }
7377 : :
7378 : : /* True if T was declared in a function declared to be constexpr, and
7379 : : therefore potentially constant in C++14. */
7380 : :
7381 : : bool
7382 : 79813793 : var_in_constexpr_fn (tree t)
7383 : : {
7384 : 79813793 : tree ctx = DECL_CONTEXT (t);
7385 : 79813793 : return (ctx && TREE_CODE (ctx) == FUNCTION_DECL
7386 : 151989097 : && DECL_DECLARED_CONSTEXPR_P (ctx));
7387 : : }
7388 : :
7389 : : /* True if a function might be constexpr: either a function that was
7390 : : declared constexpr, or a C++17 lambda op(). */
7391 : :
7392 : : bool
7393 : 438011936 : maybe_constexpr_fn (tree t)
7394 : : {
7395 : 438011936 : return (DECL_DECLARED_CONSTEXPR_P (t)
7396 : 195658924 : || (cxx_dialect >= cxx17 && LAMBDA_FUNCTION_P (t))
7397 : 628808173 : || (flag_implicit_constexpr
7398 : 66 : && DECL_DECLARED_INLINE_P (STRIP_TEMPLATE (t))));
7399 : : }
7400 : :
7401 : : /* True if T was declared in a function that might be constexpr: either a
7402 : : function that was declared constexpr, or a C++17 lambda op(). */
7403 : :
7404 : : bool
7405 : 52764585 : var_in_maybe_constexpr_fn (tree t)
7406 : : {
7407 : 105528590 : return (DECL_FUNCTION_SCOPE_P (t)
7408 : 98261660 : && maybe_constexpr_fn (DECL_CONTEXT (t)));
7409 : : }
7410 : :
7411 : : /* We're assigning INIT to TARGET. In do_build_copy_constructor and
7412 : : build_over_call we implement trivial copy of a class with tail padding using
7413 : : assignment of character arrays, which is valid in normal code, but not in
7414 : : constexpr evaluation. We don't need to worry about clobbering tail padding
7415 : : in constexpr evaluation, so strip the type punning. */
7416 : :
7417 : : static void
7418 : 32686351 : maybe_simplify_trivial_copy (tree &target, tree &init)
7419 : : {
7420 : 32686351 : if (TREE_CODE (target) == MEM_REF
7421 : 4429 : && TREE_CODE (init) == MEM_REF
7422 : 4343 : && TREE_TYPE (target) == TREE_TYPE (init)
7423 : 4343 : && TREE_CODE (TREE_TYPE (target)) == ARRAY_TYPE
7424 : 32690694 : && TREE_TYPE (TREE_TYPE (target)) == unsigned_char_type_node)
7425 : : {
7426 : 4343 : target = build_fold_indirect_ref (TREE_OPERAND (target, 0));
7427 : 4343 : init = build_fold_indirect_ref (TREE_OPERAND (init, 0));
7428 : : }
7429 : 32686351 : }
7430 : :
7431 : : /* Returns true if REF, which is a COMPONENT_REF, has any fields
7432 : : of constant type. This does not check for 'mutable', so the
7433 : : caller is expected to be mindful of that. */
7434 : :
7435 : : static bool
7436 : 280 : cref_has_const_field (tree ref)
7437 : : {
7438 : 349 : while (TREE_CODE (ref) == COMPONENT_REF)
7439 : : {
7440 : 340 : if (CP_TYPE_CONST_P (TREE_TYPE (TREE_OPERAND (ref, 1))))
7441 : : return true;
7442 : 69 : ref = TREE_OPERAND (ref, 0);
7443 : : }
7444 : : return false;
7445 : : }
7446 : :
7447 : : /* Return true if we are modifying something that is const during constant
7448 : : expression evaluation. CODE is the code of the statement, OBJ is the
7449 : : object in question, MUTABLE_P is true if one of the subobjects were
7450 : : declared mutable. */
7451 : :
7452 : : static bool
7453 : 29918941 : modifying_const_object_p (tree_code code, tree obj, bool mutable_p)
7454 : : {
7455 : : /* If this is initialization, there's no problem. */
7456 : 29918941 : if (code != MODIFY_EXPR)
7457 : : return false;
7458 : :
7459 : : /* [basic.type.qualifier] "A const object is an object of type
7460 : : const T or a non-mutable subobject of a const object." */
7461 : 8385459 : if (mutable_p)
7462 : : return false;
7463 : :
7464 : 8384723 : if (TREE_READONLY (obj))
7465 : : return true;
7466 : :
7467 : 8381000 : if (CP_TYPE_CONST_P (TREE_TYPE (obj)))
7468 : : {
7469 : : /* Although a COMPONENT_REF may have a const type, we should
7470 : : only consider it modifying a const object when any of the
7471 : : field components is const. This can happen when using
7472 : : constructs such as const_cast<const T &>(m), making something
7473 : : const even though it wasn't declared const. */
7474 : 3993 : if (TREE_CODE (obj) == COMPONENT_REF)
7475 : 280 : return cref_has_const_field (obj);
7476 : : else
7477 : : return true;
7478 : : }
7479 : :
7480 : : return false;
7481 : : }
7482 : :
7483 : : /* Evaluate an INIT_EXPR or MODIFY_EXPR. */
7484 : :
7485 : : static tree
7486 : 32686351 : cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
7487 : : value_cat lval,
7488 : : bool *non_constant_p, bool *overflow_p,
7489 : : tree *jump_target)
7490 : : {
7491 : 32686351 : constexpr_ctx new_ctx = *ctx;
7492 : :
7493 : 32686351 : tree init = TREE_OPERAND (t, 1);
7494 : :
7495 : : /* First we figure out where we're storing to. */
7496 : 32686351 : tree target = TREE_OPERAND (t, 0);
7497 : :
7498 : 32686351 : maybe_simplify_trivial_copy (target, init);
7499 : :
7500 : 32686351 : tree type = TREE_TYPE (target);
7501 : 32686351 : bool preeval = SCALAR_TYPE_P (type) || TREE_CODE (t) == MODIFY_EXPR;
7502 : 25391979 : if (preeval && !TREE_CLOBBER_P (init))
7503 : : {
7504 : : /* Ignore var = .DEFERRED_INIT (); for now, until PR121965 is fixed. */
7505 : 25340153 : if (flag_auto_var_init > AUTO_INIT_UNINITIALIZED
7506 : 10172951 : && TREE_CODE (init) == CALL_EXPR
7507 : 904026 : && CALL_EXPR_FN (init) == NULL_TREE
7508 : 25340157 : && CALL_EXPR_IFN (init) == IFN_DEFERRED_INIT)
7509 : 4 : return void_node;
7510 : :
7511 : : /* Evaluate the value to be stored without knowing what object it will be
7512 : : stored in, so that any side-effects happen first. */
7513 : 25340149 : if (!SCALAR_TYPE_P (type))
7514 : 55894 : new_ctx.ctor = new_ctx.object = NULL_TREE;
7515 : 25340149 : init = cxx_eval_constant_expression (&new_ctx, init, vc_prvalue,
7516 : : non_constant_p, overflow_p,
7517 : : jump_target);
7518 : 25340147 : if (*jump_target)
7519 : : return NULL_TREE;
7520 : 25340068 : if (*non_constant_p)
7521 : : return t;
7522 : : }
7523 : :
7524 : 26852442 : bool evaluated = false;
7525 : 26852442 : if (lval == vc_glvalue)
7526 : : {
7527 : : /* If we want to return a reference to the target, we need to evaluate it
7528 : : as a whole; otherwise, only evaluate the innermost piece to avoid
7529 : : building up unnecessary *_REFs. */
7530 : 0 : target = cxx_eval_constant_expression (ctx, target, lval,
7531 : : non_constant_p, overflow_p,
7532 : : jump_target);
7533 : 0 : evaluated = true;
7534 : 0 : if (*jump_target)
7535 : : return NULL_TREE;
7536 : 0 : if (*non_constant_p)
7537 : : return t;
7538 : : }
7539 : :
7540 : : /* Find the underlying variable. */
7541 : 26852442 : releasing_vec refs;
7542 : 26852442 : tree object = NULL_TREE;
7543 : : /* If we're modifying a const object, save it. */
7544 : 26852442 : tree const_object_being_modified = NULL_TREE;
7545 : 26852442 : bool mutable_p = false;
7546 : : /* If we see a union, we can't ignore clobbers. */
7547 : 26852442 : int seen_union = 0;
7548 : 90115121 : for (tree probe = target; object == NULL_TREE; )
7549 : : {
7550 : 63262971 : switch (TREE_CODE (probe))
7551 : : {
7552 : 9558325 : case BIT_FIELD_REF:
7553 : 9558325 : case COMPONENT_REF:
7554 : 9558325 : case ARRAY_REF:
7555 : 9558325 : {
7556 : 9558325 : tree ob = TREE_OPERAND (probe, 0);
7557 : 9558325 : tree elt = TREE_OPERAND (probe, 1);
7558 : 9558325 : if (TREE_CODE (elt) == FIELD_DECL && DECL_MUTABLE_P (elt))
7559 : : mutable_p = true;
7560 : 9558325 : if (TREE_CODE (probe) == ARRAY_REF)
7561 : : {
7562 : 1283340 : elt = eval_and_check_array_index (ctx, probe, false,
7563 : : non_constant_p, overflow_p,
7564 : : jump_target);
7565 : 1283340 : if (*jump_target)
7566 : 66 : return NULL_TREE;
7567 : 1283340 : if (*non_constant_p)
7568 : : return t;
7569 : : }
7570 : : /* We don't check modifying_const_object_p for ARRAY_REFs. Given
7571 : : "int a[10]", an ARRAY_REF "a[2]" can be "const int", even though
7572 : : the array isn't const. Instead, check "a" in the next iteration;
7573 : : that will detect modifying "const int a[10]". */
7574 : 8274985 : else if (evaluated
7575 : 3066791 : && modifying_const_object_p (TREE_CODE (t), probe,
7576 : : mutable_p)
7577 : 8275256 : && const_object_being_modified == NULL_TREE)
7578 : : const_object_being_modified = probe;
7579 : :
7580 : : /* Track named member accesses for unions to validate modifications
7581 : : that change active member. */
7582 : 9558259 : if (!evaluated && TREE_CODE (probe) == COMPONENT_REF)
7583 : 5208194 : vec_safe_push (refs, probe);
7584 : : else
7585 : 4350065 : vec_safe_push (refs, NULL_TREE);
7586 : :
7587 : 9558259 : vec_safe_push (refs, elt);
7588 : 9558259 : vec_safe_push (refs, TREE_TYPE (probe));
7589 : 9558259 : probe = ob;
7590 : 9558259 : if (TREE_CODE (TREE_TYPE (ob)) == UNION_TYPE)
7591 : 190037 : ++seen_union;
7592 : : }
7593 : 9558259 : break;
7594 : :
7595 : 15 : case REALPART_EXPR:
7596 : 15 : gcc_assert (refs->is_empty ());
7597 : 15 : vec_safe_push (refs, NULL_TREE);
7598 : 15 : vec_safe_push (refs, probe);
7599 : 15 : vec_safe_push (refs, TREE_TYPE (probe));
7600 : 15 : probe = TREE_OPERAND (probe, 0);
7601 : 15 : break;
7602 : :
7603 : 17 : case IMAGPART_EXPR:
7604 : 17 : gcc_assert (refs->is_empty ());
7605 : 17 : vec_safe_push (refs, NULL_TREE);
7606 : 17 : vec_safe_push (refs, probe);
7607 : 17 : vec_safe_push (refs, TREE_TYPE (probe));
7608 : 17 : probe = TREE_OPERAND (probe, 0);
7609 : 17 : break;
7610 : :
7611 : 53704614 : default:
7612 : 53704614 : if (evaluated)
7613 : : object = probe;
7614 : : else
7615 : : {
7616 : 26852464 : tree pvar = tree_strip_any_location_wrapper (probe);
7617 : 26852464 : if (VAR_P (pvar) && DECL_ANON_UNION_VAR_P (pvar))
7618 : : {
7619 : : /* Stores to DECL_ANON_UNION_VAR_P var are allowed to change
7620 : : active union member. */
7621 : 55 : probe = DECL_VALUE_EXPR (pvar);
7622 : 55 : break;
7623 : : }
7624 : 26852409 : probe = cxx_eval_constant_expression (ctx, probe, vc_glvalue,
7625 : : non_constant_p, overflow_p,
7626 : : jump_target);
7627 : 26852409 : evaluated = true;
7628 : 26852409 : if (*jump_target)
7629 : : return NULL_TREE;
7630 : 26852409 : if (*non_constant_p)
7631 : : return t;
7632 : : }
7633 : : break;
7634 : : }
7635 : : }
7636 : :
7637 : 26852150 : if (modifying_const_object_p (TREE_CODE (t), object, mutable_p)
7638 : 26852150 : && const_object_being_modified == NULL_TREE)
7639 : 26852150 : const_object_being_modified = object;
7640 : :
7641 : 26852150 : if (DECL_P (object)
7642 : 26852113 : && TREE_CLOBBER_P (init)
7643 : 26911248 : && DECL_NAME (object) == heap_deleted_identifier)
7644 : : /* Ignore clobbers of deleted allocations for now; we'll get a better error
7645 : : message later when operator delete is called. */
7646 : 15 : return void_node;
7647 : :
7648 : : /* And then find/build up our initializer for the path to the subobject
7649 : : we're initializing. */
7650 : 26852135 : tree *valp;
7651 : 26852135 : if (DECL_P (object))
7652 : 26852098 : valp = ctx->global->get_value_ptr (object, TREE_CODE (t) == INIT_EXPR);
7653 : : else
7654 : 37 : valp = NULL;
7655 : 26852135 : if (!valp)
7656 : : {
7657 : : /* A constant-expression cannot modify objects from outside the
7658 : : constant-expression. */
7659 : 4288 : if (!ctx->quiet)
7660 : : {
7661 : 25 : auto_diagnostic_group d;
7662 : 25 : if (DECL_P (object) && DECL_NAME (object) == heap_deleted_identifier)
7663 : : {
7664 : 0 : error ("modification of allocated storage after deallocation "
7665 : : "is not a constant expression");
7666 : 0 : inform (DECL_SOURCE_LOCATION (object), "allocated here");
7667 : : }
7668 : 25 : else if (DECL_P (object) && ctx->global->is_outside_lifetime (object))
7669 : : {
7670 : 10 : if (TREE_CLOBBER_P (init))
7671 : 3 : error ("destroying %qE outside its lifetime", object);
7672 : : else
7673 : 7 : error ("modification of %qE outside its lifetime "
7674 : : "is not a constant expression", object);
7675 : 10 : inform (DECL_SOURCE_LOCATION (object), "declared here");
7676 : : }
7677 : : else
7678 : : {
7679 : 15 : if (TREE_CLOBBER_P (init))
7680 : 6 : error ("destroying %qE from outside current evaluation "
7681 : : "is not a constant expression", object);
7682 : : else
7683 : 9 : error ("modification of %qE from outside current evaluation "
7684 : : "is not a constant expression", object);
7685 : : }
7686 : 25 : }
7687 : 4288 : *non_constant_p = true;
7688 : 4288 : return t;
7689 : : }
7690 : :
7691 : : /* Handle explicit end-of-lifetime. */
7692 : 26847847 : if (TREE_CLOBBER_P (init))
7693 : : {
7694 : 59035 : if (CLOBBER_KIND (init) >= CLOBBER_OBJECT_END
7695 : 59035 : && refs->is_empty ())
7696 : : {
7697 : 27920 : ctx->global->destroy_value (object);
7698 : 27920 : return void_node;
7699 : : }
7700 : :
7701 : 29005 : if (!seen_union && !*valp
7702 : 35130 : && CLOBBER_KIND (init) < CLOBBER_OBJECT_END)
7703 : 3985 : return void_node;
7704 : :
7705 : : /* Ending the lifetime of a const object is OK. */
7706 : : const_object_being_modified = NULL_TREE;
7707 : : }
7708 : :
7709 : 26815942 : type = TREE_TYPE (object);
7710 : 26815942 : bool no_zero_init = true;
7711 : 26815942 : bool zero_padding_bits = false;
7712 : :
7713 : 53631884 : auto_vec<tree *> ctors;
7714 : 53631884 : releasing_vec indexes;
7715 : 53631884 : auto_vec<int> index_pos_hints;
7716 : 26815942 : bool activated_union_member_p = false;
7717 : 26815942 : bool empty_base = false;
7718 : 36339527 : while (!refs->is_empty ())
7719 : : {
7720 : 9550008 : if (*valp == NULL_TREE)
7721 : : {
7722 : 1114534 : *valp = build_constructor (type, NULL);
7723 : 1114534 : CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
7724 : 1114534 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
7725 : : }
7726 : 8435474 : else if (STRIP_ANY_LOCATION_WRAPPER (*valp),
7727 : 8435474 : TREE_CODE (*valp) == STRING_CST)
7728 : : {
7729 : : /* An array was initialized with a string constant, and now
7730 : : we're writing into one of its elements. Explode the
7731 : : single initialization into a set of element
7732 : : initializations. */
7733 : 3969 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
7734 : :
7735 : 3969 : tree string = *valp;
7736 : 3969 : tree elt_type = TREE_TYPE (type);
7737 : 3969 : unsigned chars_per_elt = (TYPE_PRECISION (elt_type)
7738 : 3969 : / TYPE_PRECISION (char_type_node));
7739 : 3969 : unsigned num_elts = TREE_STRING_LENGTH (string) / chars_per_elt;
7740 : 3969 : tree ary_ctor = build_constructor (type, NULL);
7741 : :
7742 : 3969 : vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor), num_elts);
7743 : 8099 : for (unsigned ix = 0; ix != num_elts; ix++)
7744 : : {
7745 : 4130 : constructor_elt elt =
7746 : : {
7747 : 4130 : build_int_cst (size_type_node, ix),
7748 : 4130 : extract_string_elt (string, chars_per_elt, ix)
7749 : 4130 : };
7750 : 4130 : CONSTRUCTOR_ELTS (ary_ctor)->quick_push (elt);
7751 : : }
7752 : :
7753 : 3969 : *valp = ary_ctor;
7754 : : }
7755 : :
7756 : 9550008 : enum tree_code code = TREE_CODE (type);
7757 : 9550008 : tree reftype = refs->pop();
7758 : 9550008 : tree index = refs->pop();
7759 : 9550008 : bool is_access_expr = refs->pop() != NULL_TREE;
7760 : :
7761 : 9550008 : if (code == COMPLEX_TYPE)
7762 : : {
7763 : 32 : if (TREE_CODE (*valp) == COMPLEX_CST)
7764 : 30 : *valp = build2 (COMPLEX_EXPR, type, TREE_REALPART (*valp),
7765 : 30 : TREE_IMAGPART (*valp));
7766 : 2 : else if (TREE_CODE (*valp) == CONSTRUCTOR
7767 : 1 : && CONSTRUCTOR_NELTS (*valp) == 0
7768 : 3 : && CONSTRUCTOR_NO_CLEARING (*valp))
7769 : : {
7770 : 1 : tree r = build_constructor (reftype, NULL);
7771 : 1 : CONSTRUCTOR_NO_CLEARING (r) = 1;
7772 : 1 : *valp = build2 (COMPLEX_EXPR, type, r, r);
7773 : : }
7774 : 32 : gcc_assert (TREE_CODE (*valp) == COMPLEX_EXPR);
7775 : 32 : ctors.safe_push (valp);
7776 : 32 : vec_safe_push (indexes, index);
7777 : 32 : valp = &TREE_OPERAND (*valp, TREE_CODE (index) == IMAGPART_EXPR);
7778 : 32 : gcc_checking_assert (refs->is_empty ());
7779 : : type = reftype;
7780 : 25800 : break;
7781 : : }
7782 : :
7783 : : /* If the value of object is already zero-initialized, any new ctors for
7784 : : subobjects will also be zero-initialized. Similarly with zeroing of
7785 : : padding bits. */
7786 : 9549976 : no_zero_init = CONSTRUCTOR_NO_CLEARING (*valp);
7787 : 9549976 : zero_padding_bits = CONSTRUCTOR_ZERO_PADDING_BITS (*valp);
7788 : :
7789 : 9549976 : if (code == RECORD_TYPE && is_empty_field (index))
7790 : : /* Don't build a sub-CONSTRUCTOR for an empty base or field, as they
7791 : : have no data and might have an offset lower than previously declared
7792 : : fields, which confuses the middle-end. The code below will notice
7793 : : that we don't have a CONSTRUCTOR for our inner target and just
7794 : : return init. */
7795 : : {
7796 : : empty_base = true;
7797 : : break;
7798 : : }
7799 : :
7800 : : /* If a union is zero-initialized, its first non-static named data member
7801 : : is zero-initialized (and therefore active). */
7802 : 9524208 : if (code == UNION_TYPE
7803 : 9524208 : && !no_zero_init
7804 : 9524208 : && CONSTRUCTOR_NELTS (*valp) == 0)
7805 : 75 : if (tree first = next_aggregate_field (TYPE_FIELDS (type)))
7806 : 75 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (*valp), first, NULL_TREE);
7807 : :
7808 : : /* Check for implicit change of active member for a union. */
7809 : :
7810 : : /* LWG3436, CWG2675, c++/121068: The array object model is confused. For
7811 : : now allow initializing an array element to activate the array. */
7812 : 9557975 : auto only_array_refs = [](const releasing_vec &refs)
7813 : : {
7814 : 33771 : for (unsigned i = 1; i < refs->length(); i += 3)
7815 : 34 : if (TREE_CODE ((*refs)[i]) != INTEGER_CST)
7816 : : return false;
7817 : : return true;
7818 : : };
7819 : :
7820 : 9524208 : if (code == UNION_TYPE
7821 : 190003 : && (CONSTRUCTOR_NELTS (*valp) == 0
7822 : 77080 : || CONSTRUCTOR_ELT (*valp, 0)->index != index)
7823 : : /* An INIT_EXPR of the last member in an access chain is always OK,
7824 : : but still check implicit change of members earlier on; see
7825 : : cpp2a/constexpr-union6.C. */
7826 : 9639736 : && !(TREE_CODE (t) == INIT_EXPR && only_array_refs (refs)))
7827 : : {
7828 : 81791 : bool has_active_member = CONSTRUCTOR_NELTS (*valp) != 0;
7829 : 81791 : tree inner = strip_array_types (reftype);
7830 : :
7831 : 81791 : if (has_active_member && cxx_dialect < cxx20)
7832 : : {
7833 : 68 : if (!ctx->quiet)
7834 : 22 : error_at (cp_expr_loc_or_input_loc (t),
7835 : : "change of the active member of a union "
7836 : : "from %qD to %qD is not a constant expression "
7837 : : "before C++20",
7838 : 22 : CONSTRUCTOR_ELT (*valp, 0)->index,
7839 : : index);
7840 : 68 : *non_constant_p = true;
7841 : : }
7842 : 81723 : else if (!is_access_expr
7843 : 5335 : || (TREE_CLOBBER_P (init)
7844 : 0 : && CLOBBER_KIND (init) >= CLOBBER_OBJECT_END)
7845 : 87058 : || (TREE_CODE (t) == MODIFY_EXPR
7846 : 5323 : && CLASS_TYPE_P (inner)
7847 : 15 : && !type_has_non_deleted_trivial_default_ctor (inner)))
7848 : : {
7849 : : /* Diagnose changing active union member after initialization
7850 : : without a valid member access expression, as described in
7851 : : [class.union.general] p5. */
7852 : 76397 : if (!ctx->quiet)
7853 : : {
7854 : 33 : auto_diagnostic_group d;
7855 : 33 : if (has_active_member)
7856 : 24 : error_at (cp_expr_loc_or_input_loc (t),
7857 : : "accessing %qD member instead of initialized "
7858 : : "%qD member in constant expression",
7859 : 24 : index, CONSTRUCTOR_ELT (*valp, 0)->index);
7860 : : else
7861 : 9 : error_at (cp_expr_loc_or_input_loc (t),
7862 : : "accessing uninitialized member %qD",
7863 : : index);
7864 : 33 : if (is_access_expr)
7865 : 3 : inform (DECL_SOURCE_LOCATION (index),
7866 : : "%qD does not implicitly begin its lifetime "
7867 : : "because %qT does not have a non-deleted "
7868 : : "trivial default constructor, use "
7869 : : "%<std::construct_at%> instead",
7870 : : index, inner);
7871 : : else
7872 : 30 : inform (DECL_SOURCE_LOCATION (index),
7873 : : "initializing %qD requires a member access "
7874 : : "expression as the left operand of the assignment",
7875 : : index);
7876 : 33 : }
7877 : 76397 : *non_constant_p = true;
7878 : : }
7879 : 5326 : else if (has_active_member && CONSTRUCTOR_NO_CLEARING (*valp))
7880 : : {
7881 : : /* Diagnose changing the active union member while the union
7882 : : is in the process of being initialized. */
7883 : 9 : if (!ctx->quiet)
7884 : 3 : error_at (cp_expr_loc_or_input_loc (t),
7885 : : "change of the active member of a union "
7886 : : "from %qD to %qD during initialization",
7887 : 3 : CONSTRUCTOR_ELT (*valp, 0)->index,
7888 : : index);
7889 : 9 : *non_constant_p = true;
7890 : : }
7891 : : no_zero_init = true;
7892 : : }
7893 : :
7894 : 9524208 : ctors.safe_push (valp);
7895 : 9524208 : vec_safe_push (indexes, index);
7896 : :
7897 : : /* Avoid adding an _elt for a clobber when the whole CONSTRUCTOR is
7898 : : uninitialized. */
7899 : 9015098 : int pos = (!seen_union && TREE_CLOBBER_P (init)
7900 : 30799 : && CONSTRUCTOR_NO_CLEARING (*valp)
7901 : 9552890 : && CLOBBER_KIND (init) < CLOBBER_OBJECT_END) ? -2 : -1;
7902 : 9524208 : constructor_elt *cep
7903 : 9524208 : = get_or_insert_ctor_field (*valp, index, pos);
7904 : 9524208 : if (cep == nullptr)
7905 : 623 : return void_node;
7906 : 9523585 : index_pos_hints.safe_push (cep - CONSTRUCTOR_ELTS (*valp)->begin());
7907 : :
7908 : 9523585 : if (code == UNION_TYPE)
7909 : : {
7910 : 190003 : activated_union_member_p = true;
7911 : 190003 : --seen_union;
7912 : : }
7913 : :
7914 : 9523585 : valp = &cep->value;
7915 : 9523585 : type = reftype;
7916 : : }
7917 : :
7918 : : /* Change an "as-base" clobber to the real type;
7919 : : we don't need to worry about padding in constexpr. */
7920 : 26815319 : tree itype = initialized_type (init);
7921 : 26815319 : if (IS_FAKE_BASE_TYPE (itype))
7922 : 77 : itype = TYPE_CONTEXT (itype);
7923 : :
7924 : : /* For initialization of an empty base, the original target will be
7925 : : *(base*)this, evaluation of which resolves to the object
7926 : : argument, which has the derived type rather than the base type. */
7927 : 53604870 : if (!empty_base && !(same_type_ignoring_top_level_qualifiers_p
7928 : 26789551 : (itype, type)))
7929 : : {
7930 : 6378 : gcc_assert (is_empty_class (TREE_TYPE (target)));
7931 : : empty_base = true;
7932 : : }
7933 : :
7934 : : /* Detect modifying a constant object in constexpr evaluation.
7935 : : We have found a const object that is being modified. Figure out
7936 : : if we need to issue an error. Consider
7937 : :
7938 : : struct A {
7939 : : int n;
7940 : : constexpr A() : n(1) { n = 2; } // #1
7941 : : };
7942 : : struct B {
7943 : : const A a;
7944 : : constexpr B() { a.n = 3; } // #2
7945 : : };
7946 : : constexpr B b{};
7947 : :
7948 : : #1 is OK, since we're modifying an object under construction, but
7949 : : #2 is wrong, since "a" is const and has been fully constructed.
7950 : : To track it, we use the TREE_READONLY bit in the object's CONSTRUCTOR
7951 : : which means that the object is read-only. For the example above, the
7952 : : *ctors stack at the point of #2 will look like:
7953 : :
7954 : : ctors[0] = {.a={.n=2}} TREE_READONLY = 0
7955 : : ctors[1] = {.n=2} TREE_READONLY = 1
7956 : :
7957 : : and we're modifying "b.a", so we search the stack and see if the
7958 : : constructor for "b.a" has already run. */
7959 : 26815319 : if (const_object_being_modified)
7960 : : {
7961 : 6684 : bool fail = false;
7962 : 6684 : tree const_objtype
7963 : 6684 : = strip_array_types (TREE_TYPE (const_object_being_modified));
7964 : 6684 : if (!CLASS_TYPE_P (const_objtype))
7965 : : fail = true;
7966 : : else
7967 : : {
7968 : : /* [class.ctor]p5 "A constructor can be invoked for a const,
7969 : : volatile, or const volatile object. const and volatile
7970 : : semantics are not applied on an object under construction.
7971 : : They come into effect when the constructor for the most
7972 : : derived object ends." */
7973 : 19906 : for (tree *elt : ctors)
7974 : 6748 : if (same_type_ignoring_top_level_qualifiers_p
7975 : 6748 : (TREE_TYPE (const_object_being_modified), TREE_TYPE (*elt)))
7976 : : {
7977 : 6579 : fail = TREE_READONLY (*elt);
7978 : 6579 : break;
7979 : : }
7980 : : }
7981 : 6579 : if (fail)
7982 : : {
7983 : 198 : if (!ctx->quiet)
7984 : 63 : modifying_const_object_error (t, const_object_being_modified);
7985 : 198 : *non_constant_p = true;
7986 : 198 : return t;
7987 : : }
7988 : : }
7989 : :
7990 : 26815121 : if (!preeval)
7991 : : {
7992 : : /* We're handling an INIT_EXPR of class type, so the value of the
7993 : : initializer can depend on the object it's initializing. */
7994 : :
7995 : : /* Create a new CONSTRUCTOR in case evaluation of the initializer
7996 : : wants to modify it. */
7997 : 7286972 : if (*valp == NULL_TREE)
7998 : : {
7999 : 7134057 : *valp = build_constructor (type, NULL);
8000 : 7134057 : CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
8001 : 7134057 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
8002 : : }
8003 : 7286972 : new_ctx.ctor = empty_base ? NULL_TREE : *valp;
8004 : 7286972 : new_ctx.object = target;
8005 : : /* Avoid temporary materialization when initializing from a TARGET_EXPR.
8006 : : We don't need to mess with AGGR_EXPR_SLOT/VEC_INIT_EXPR_SLOT because
8007 : : expansion of those trees uses ctx instead. */
8008 : 7286972 : if (TREE_CODE (init) == TARGET_EXPR)
8009 : 1568113 : if (tree tinit = TARGET_EXPR_INITIAL (init))
8010 : 1568113 : init = tinit;
8011 : 7286972 : init = cxx_eval_constant_expression (&new_ctx, init, vc_prvalue,
8012 : : non_constant_p, overflow_p,
8013 : : jump_target);
8014 : 7286972 : if (*jump_target)
8015 : : return NULL_TREE;
8016 : : /* The hash table might have moved since the get earlier, and the
8017 : : initializer might have mutated the underlying CONSTRUCTORs, so we must
8018 : : recompute VALP. */
8019 : 7286950 : valp = ctx->global->get_value_ptr (object, TREE_CODE (t) == INIT_EXPR);
8020 : 8184096 : for (unsigned i = 0; i < vec_safe_length (indexes); i++)
8021 : : {
8022 : 897146 : ctors[i] = valp;
8023 : 897146 : constructor_elt *cep
8024 : 897146 : = get_or_insert_ctor_field (*valp, indexes[i], index_pos_hints[i]);
8025 : 897146 : valp = &cep->value;
8026 : : }
8027 : : }
8028 : :
8029 : 26815099 : if (*non_constant_p)
8030 : : return t;
8031 : :
8032 : : /* Don't share a CONSTRUCTOR that might be changed later. */
8033 : 25828410 : init = unshare_constructor (init);
8034 : :
8035 : 25828410 : gcc_checking_assert (!*valp
8036 : : || *valp == void_node
8037 : : || (same_type_ignoring_top_level_qualifiers_p
8038 : : (TREE_TYPE (*valp), type)));
8039 : 25828410 : if (empty_base)
8040 : : {
8041 : : /* Just evaluate the initializer and return, since there's no actual data
8042 : : to store, and we didn't build a CONSTRUCTOR. */
8043 : 29106 : if (!*valp)
8044 : : {
8045 : : /* But do make sure we have something in *valp. */
8046 : 0 : *valp = build_constructor (type, nullptr);
8047 : 0 : CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
8048 : 0 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
8049 : : }
8050 : : }
8051 : 25799304 : else if (TREE_CLOBBER_P (init))
8052 : : {
8053 : 26507 : if (AGGREGATE_TYPE_P (type))
8054 : : {
8055 : 13557 : if (*valp)
8056 : 13537 : CONSTRUCTOR_ELTS (*valp) = nullptr;
8057 : : else
8058 : 20 : *valp = build_constructor (type, nullptr);
8059 : 13557 : TREE_CONSTANT (*valp) = true;
8060 : 13557 : TREE_SIDE_EFFECTS (*valp) = false;
8061 : 13557 : CONSTRUCTOR_NO_CLEARING (*valp) = true;
8062 : 13557 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
8063 : : }
8064 : : else
8065 : 12950 : *valp = void_node;
8066 : : }
8067 : 25772797 : else if (*valp && TREE_CODE (*valp) == CONSTRUCTOR
8068 : 6397461 : && TREE_CODE (init) == CONSTRUCTOR)
8069 : : {
8070 : : /* An outer ctx->ctor might be pointing to *valp, so replace
8071 : : its contents. */
8072 : 2067126 : CONSTRUCTOR_ELTS (*valp) = CONSTRUCTOR_ELTS (init);
8073 : 2067126 : TREE_CONSTANT (*valp) = TREE_CONSTANT (init);
8074 : 2067126 : TREE_SIDE_EFFECTS (*valp) = TREE_SIDE_EFFECTS (init);
8075 : 6201378 : CONSTRUCTOR_NO_CLEARING (*valp)
8076 : 2067126 : = CONSTRUCTOR_NO_CLEARING (init);
8077 : 6201378 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp)
8078 : 2067126 : = CONSTRUCTOR_ZERO_PADDING_BITS (init);
8079 : : }
8080 : : else
8081 : 23705671 : *valp = init;
8082 : :
8083 : : /* After initialization, 'const' semantics apply to the value of the
8084 : : object. Make a note of this fact by marking the CONSTRUCTOR
8085 : : TREE_READONLY. */
8086 : 25828410 : if (TREE_CODE (t) == INIT_EXPR
8087 : 18868443 : && !empty_base
8088 : 18839337 : && TREE_CODE (*valp) == CONSTRUCTOR
8089 : 27845712 : && TYPE_READONLY (type))
8090 : : {
8091 : 20961 : tree target_type = TREE_TYPE (target);
8092 : 20961 : if (IS_FAKE_BASE_TYPE (target_type))
8093 : 0 : target_type = TYPE_CONTEXT (target_type);
8094 : 20961 : if (INDIRECT_REF_P (target)
8095 : 36051 : && (is_this_parameter
8096 : 15090 : (tree_strip_nop_conversions (TREE_OPERAND (target, 0)))))
8097 : : /* We've just initialized '*this' (perhaps via the target
8098 : : constructor of a delegating constructor). Leave it up to the
8099 : : caller that set 'this' to set TREE_READONLY appropriately. */
8100 : 26 : gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
8101 : : (target_type, type) || empty_base);
8102 : : else
8103 : 20935 : TREE_READONLY (*valp) = true;
8104 : : }
8105 : :
8106 : : /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing
8107 : : CONSTRUCTORs, if any. */
8108 : 25828410 : bool c = TREE_CONSTANT (init);
8109 : 25828410 : bool s = TREE_SIDE_EFFECTS (init);
8110 : 25828410 : if (!indexes->is_empty ())
8111 : : {
8112 : 5759480 : tree last = indexes->last ();
8113 : 5759480 : if (TREE_CODE (last) == REALPART_EXPR
8114 : 5759480 : || TREE_CODE (last) == IMAGPART_EXPR)
8115 : : {
8116 : : /* And canonicalize COMPLEX_EXPR into COMPLEX_CST if
8117 : : possible. */
8118 : 32 : tree *cexpr = ctors.last ();
8119 : 32 : if (tree c = const_binop (COMPLEX_EXPR, TREE_TYPE (*cexpr),
8120 : 32 : TREE_OPERAND (*cexpr, 0),
8121 : 32 : TREE_OPERAND (*cexpr, 1)))
8122 : 31 : *cexpr = c;
8123 : : else
8124 : : {
8125 : 2 : TREE_CONSTANT (*cexpr)
8126 : 1 : = (TREE_CONSTANT (TREE_OPERAND (*cexpr, 0))
8127 : 1 : & TREE_CONSTANT (TREE_OPERAND (*cexpr, 1)));
8128 : 2 : TREE_SIDE_EFFECTS (*cexpr)
8129 : 2 : = (TREE_SIDE_EFFECTS (TREE_OPERAND (*cexpr, 0))
8130 : 1 : | TREE_SIDE_EFFECTS (TREE_OPERAND (*cexpr, 1)));
8131 : : }
8132 : 32 : c = TREE_CONSTANT (*cexpr);
8133 : 32 : s = TREE_SIDE_EFFECTS (*cexpr);
8134 : : }
8135 : : }
8136 : 25828410 : if (!c || s || activated_union_member_p)
8137 : 12099427 : for (tree *elt : ctors)
8138 : : {
8139 : 1969324 : if (TREE_CODE (*elt) != CONSTRUCTOR)
8140 : 0 : continue;
8141 : 1969324 : if (!c)
8142 : 1557666 : TREE_CONSTANT (*elt) = false;
8143 : 1969324 : if (s)
8144 : 0 : TREE_SIDE_EFFECTS (*elt) = true;
8145 : : /* Clear CONSTRUCTOR_NO_CLEARING since we've activated a member of
8146 : : this union. */
8147 : 1969324 : if (TREE_CODE (TREE_TYPE (*elt)) == UNION_TYPE)
8148 : 113455 : CONSTRUCTOR_NO_CLEARING (*elt) = false;
8149 : : }
8150 : :
8151 : 25828410 : if (lval)
8152 : : return target;
8153 : : else
8154 : 431542 : return init;
8155 : 26852442 : }
8156 : :
8157 : : /* Evaluate a ++ or -- expression. */
8158 : :
8159 : : static tree
8160 : 2468967 : cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t,
8161 : : value_cat lval,
8162 : : bool *non_constant_p, bool *overflow_p,
8163 : : tree *jump_target)
8164 : : {
8165 : 2468967 : enum tree_code code = TREE_CODE (t);
8166 : 2468967 : tree type = TREE_TYPE (t);
8167 : 2468967 : tree op = TREE_OPERAND (t, 0);
8168 : 2468967 : tree offset = TREE_OPERAND (t, 1);
8169 : 2468967 : gcc_assert (TREE_CONSTANT (offset));
8170 : :
8171 : : /* OFFSET is constant, but perhaps not constant enough. We need to
8172 : : e.g. bash FLOAT_EXPRs to REAL_CSTs. */
8173 : 2468967 : offset = fold_simple (offset);
8174 : :
8175 : : /* The operand as an lvalue. */
8176 : 2468967 : op = cxx_eval_constant_expression (ctx, op, vc_glvalue,
8177 : : non_constant_p, overflow_p,
8178 : : jump_target);
8179 : 2468967 : if (*jump_target)
8180 : : return NULL_TREE;
8181 : :
8182 : : /* The operand as an rvalue. */
8183 : 2468967 : tree val
8184 : 2468967 : = cxx_eval_constant_expression (ctx, op, vc_prvalue,
8185 : : non_constant_p, overflow_p,
8186 : : jump_target);
8187 : 2468967 : if (*jump_target)
8188 : : return NULL_TREE;
8189 : : /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
8190 : : a local array in a constexpr function. */
8191 : 2468967 : bool ptr = INDIRECT_TYPE_P (TREE_TYPE (val));
8192 : 1188621 : if (!ptr)
8193 : 1188621 : VERIFY_CONSTANT (val);
8194 : :
8195 : : /* The modified value. */
8196 : 2432381 : bool inc = (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR);
8197 : 2432381 : tree mod;
8198 : 2432381 : if (INDIRECT_TYPE_P (type))
8199 : : {
8200 : : /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
8201 : 1280346 : offset = convert_to_ptrofftype (offset);
8202 : 1280346 : if (!inc)
8203 : 96305 : offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset);
8204 : 1280346 : mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset);
8205 : : }
8206 : 1152035 : else if (c_promoting_integer_type_p (type)
8207 : 9990 : && !TYPE_UNSIGNED (type)
8208 : 1152104 : && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
8209 : : {
8210 : 69 : offset = fold_convert (integer_type_node, offset);
8211 : 69 : mod = fold_convert (integer_type_node, val);
8212 : 125 : tree t = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, integer_type_node,
8213 : : mod, offset);
8214 : 69 : mod = fold_convert (type, t);
8215 : 69 : if (TREE_OVERFLOW_P (mod) && !TREE_OVERFLOW_P (t))
8216 : 9 : TREE_OVERFLOW (mod) = false;
8217 : : }
8218 : : else
8219 : 1521699 : mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset);
8220 : 2432381 : if (!ptr)
8221 : 1152035 : VERIFY_CONSTANT (mod);
8222 : :
8223 : : /* Storing the modified value. */
8224 : 3953968 : tree store = build2_loc (cp_expr_loc_or_loc (t, input_location),
8225 : : MODIFY_EXPR, type, op, mod);
8226 : 2432381 : mod = cxx_eval_constant_expression (ctx, store, lval,
8227 : : non_constant_p, overflow_p,
8228 : : jump_target);
8229 : 2432381 : ggc_free (store);
8230 : 2432381 : if (*jump_target)
8231 : : return NULL_TREE;
8232 : 2432381 : if (*non_constant_p)
8233 : : return t;
8234 : :
8235 : : /* And the value of the expression. */
8236 : 2393064 : if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
8237 : : /* Prefix ops are lvalues, but the caller might want an rvalue;
8238 : : lval has already been taken into account in the store above. */
8239 : : return mod;
8240 : : else
8241 : : /* Postfix ops are rvalues. */
8242 : 526566 : return val;
8243 : : }
8244 : :
8245 : : /* Subroutine of cxx_eval_statement_list. Determine whether the statement
8246 : : STMT matches *jump_target. If we're looking for a case label and we see
8247 : : the default label, note it in ctx->css_state. */
8248 : :
8249 : : static bool
8250 : 397091 : label_matches (const constexpr_ctx *ctx, tree *jump_target, tree stmt)
8251 : : {
8252 : 397091 : switch (TREE_CODE (*jump_target))
8253 : : {
8254 : 45 : case LABEL_DECL:
8255 : 45 : if (TREE_CODE (stmt) == LABEL_EXPR
8256 : 45 : && LABEL_EXPR_LABEL (stmt) == *jump_target)
8257 : : return true;
8258 : : break;
8259 : :
8260 : 393641 : case INTEGER_CST:
8261 : 393641 : if (TREE_CODE (stmt) == CASE_LABEL_EXPR)
8262 : : {
8263 : 393625 : gcc_assert (ctx->css_state != NULL);
8264 : 393625 : if (!CASE_LOW (stmt))
8265 : : {
8266 : : /* default: should appear just once in a SWITCH_EXPR
8267 : : body (excluding nested SWITCH_EXPR). */
8268 : 56642 : gcc_assert (*ctx->css_state != css_default_seen);
8269 : : /* When evaluating SWITCH_EXPR body for the second time,
8270 : : return true for the default: label. */
8271 : 56642 : if (*ctx->css_state == css_default_processing)
8272 : : return true;
8273 : 28333 : *ctx->css_state = css_default_seen;
8274 : : }
8275 : 336983 : else if (CASE_HIGH (stmt))
8276 : : {
8277 : 20 : if (tree_int_cst_le (CASE_LOW (stmt), *jump_target)
8278 : 31 : && tree_int_cst_le (*jump_target, CASE_HIGH (stmt)))
8279 : : return true;
8280 : : }
8281 : 336963 : else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt)))
8282 : : return true;
8283 : : }
8284 : : break;
8285 : :
8286 : : case BREAK_STMT:
8287 : : case CONTINUE_STMT:
8288 : : /* These two are handled directly in cxx_eval_loop_expr by testing
8289 : : breaks (jump_target) or continues (jump_target). */
8290 : : break;
8291 : :
8292 : : case VAR_DECL:
8293 : : /* Uncaught exception. This is handled by TRY_BLOCK evaluation
8294 : : and other places by testing throws (jump_target). */
8295 : : break;
8296 : :
8297 : 0 : default:
8298 : 0 : gcc_unreachable ();
8299 : : }
8300 : : return false;
8301 : : }
8302 : :
8303 : : /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
8304 : : semantics, for switch, break, continue, and return. */
8305 : :
8306 : : static tree
8307 : 17223779 : cxx_eval_statement_list (const constexpr_ctx *ctx, tree t,
8308 : : bool *non_constant_p, bool *overflow_p,
8309 : : tree *jump_target)
8310 : : {
8311 : : /* In a statement-expression we want to return the last value.
8312 : : For empty statement expression return void_node. */
8313 : 17223779 : tree r = void_node;
8314 : 39957521 : for (tree_stmt_iterator i = tsi_start (t); !tsi_end_p (i); ++i)
8315 : : {
8316 : 32484226 : tree stmt = *i;
8317 : :
8318 : : /* We've found a continue, so skip everything until we reach
8319 : : the label its jumping to. */
8320 : 32484226 : if (continues (jump_target))
8321 : : {
8322 : 3450 : if (label_matches (ctx, jump_target, stmt))
8323 : : /* Found it. */
8324 : 9 : *jump_target = NULL_TREE;
8325 : : else
8326 : 3441 : continue;
8327 : : }
8328 : 32480785 : if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT)
8329 : 6548376 : continue;
8330 : :
8331 : 25932409 : value_cat lval = vc_discard;
8332 : : /* The result of a statement-expression is not wrapped in EXPR_STMT. */
8333 : 37490111 : if (tsi_one_before_end_p (i)
8334 : 11558872 : && !VOID_TYPE_P (TREE_TYPE (stmt)))
8335 : : lval = vc_prvalue;
8336 : :
8337 : 25932409 : r = cxx_eval_constant_expression (ctx, stmt, lval,
8338 : : non_constant_p, overflow_p,
8339 : : jump_target);
8340 : 25932407 : if (*non_constant_p)
8341 : : break;
8342 : 19985314 : if (returns (jump_target)
8343 : 16205354 : || breaks (jump_target)
8344 : 22733742 : || throws (jump_target))
8345 : : break;
8346 : : }
8347 : 17223777 : return r;
8348 : : }
8349 : :
8350 : : /* Evaluate a LOOP_EXPR for side-effects. Handles break and return
8351 : : semantics; continue semantics are covered by cxx_eval_statement_list. */
8352 : :
8353 : : static tree
8354 : 978338 : cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t,
8355 : : bool *non_constant_p, bool *overflow_p,
8356 : : tree *jump_target)
8357 : : {
8358 : 978338 : tree body, cond = NULL_TREE, expr = NULL_TREE;
8359 : 978338 : tree cond_prep = NULL_TREE, cond_cleanup = NULL_TREE;
8360 : 978338 : unsigned cond_cleanup_depth = 0;
8361 : 978338 : int count = 0;
8362 : 978338 : switch (TREE_CODE (t))
8363 : : {
8364 : 85 : case LOOP_EXPR:
8365 : 85 : body = LOOP_EXPR_BODY (t);
8366 : 85 : break;
8367 : 770283 : case DO_STMT:
8368 : 770283 : body = DO_BODY (t);
8369 : 770283 : cond = DO_COND (t);
8370 : 770283 : break;
8371 : 78151 : case WHILE_STMT:
8372 : 78151 : body = WHILE_BODY (t);
8373 : 78151 : cond = WHILE_COND (t);
8374 : 78151 : cond_prep = WHILE_COND_PREP (t);
8375 : 78151 : cond_cleanup = WHILE_COND_CLEANUP (t);
8376 : 78151 : count = -1;
8377 : 78151 : break;
8378 : 129819 : case FOR_STMT:
8379 : 129819 : if (FOR_INIT_STMT (t))
8380 : 0 : cxx_eval_constant_expression (ctx, FOR_INIT_STMT (t), vc_discard,
8381 : : non_constant_p, overflow_p, jump_target);
8382 : 129819 : if (*non_constant_p)
8383 : : return NULL_TREE;
8384 : 129819 : body = FOR_BODY (t);
8385 : 129819 : cond = FOR_COND (t);
8386 : 129819 : expr = FOR_EXPR (t);
8387 : 129819 : cond_prep = FOR_COND_PREP (t);
8388 : 129819 : cond_cleanup = FOR_COND_CLEANUP (t);
8389 : 129819 : count = -1;
8390 : 129819 : break;
8391 : 0 : default:
8392 : 0 : gcc_unreachable ();
8393 : : }
8394 : 978338 : if (cond_prep)
8395 : 12 : gcc_assert (TREE_CODE (cond_prep) == BIND_EXPR);
8396 : 9399158 : auto cleanup_cond = [&] {
8397 : : /* Clean up the condition variable after each iteration. */
8398 : 8420820 : if (cond_cleanup_depth && !*non_constant_p)
8399 : : {
8400 : 105 : auto_vec<tree, 4> cleanups (cond_cleanup_depth);
8401 : 105 : tree s = BIND_EXPR_BODY (cond_prep);
8402 : 105 : unsigned i;
8403 : 210 : for (i = cond_cleanup_depth; i; --i)
8404 : : {
8405 : 105 : tree_stmt_iterator iter = tsi_last (s);
8406 : 105 : s = tsi_stmt (iter);
8407 : 105 : cleanups.quick_push (CLEANUP_EXPR (s));
8408 : 105 : s = CLEANUP_BODY (s);
8409 : : }
8410 : 105 : tree c;
8411 : 420 : FOR_EACH_VEC_ELT_REVERSE (cleanups, i, c)
8412 : 105 : cxx_eval_constant_expression (ctx, c, vc_discard, non_constant_p,
8413 : : overflow_p, jump_target);
8414 : 105 : }
8415 : 8420820 : if (cond_prep)
8416 : 111 : for (tree decl = BIND_EXPR_VARS (cond_prep);
8417 : 222 : decl; decl = DECL_CHAIN (decl))
8418 : 111 : destroy_value_checked (ctx, decl, non_constant_p);
8419 : 978338 : };
8420 : 7651072 : do
8421 : : {
8422 : 7651072 : if (count != -1)
8423 : : {
8424 : 7443102 : if (body)
8425 : 7443102 : cxx_eval_constant_expression (ctx, body, vc_discard,
8426 : : non_constant_p, overflow_p,
8427 : : jump_target);
8428 : 7443102 : if (breaks (jump_target))
8429 : : {
8430 : 620 : *jump_target = NULL_TREE;
8431 : 620 : break;
8432 : : }
8433 : :
8434 : 7442482 : if (TREE_CODE (t) != LOOP_EXPR && continues (jump_target))
8435 : 694 : *jump_target = NULL_TREE;
8436 : :
8437 : 7442482 : if (expr)
8438 : 1286768 : cxx_eval_constant_expression (ctx, expr, vc_discard,
8439 : : non_constant_p, overflow_p,
8440 : : jump_target);
8441 : 7442482 : cleanup_cond ();
8442 : : }
8443 : :
8444 : 7650452 : if (cond_prep)
8445 : : {
8446 : 111 : for (tree decl = BIND_EXPR_VARS (cond_prep);
8447 : 222 : decl; decl = DECL_CHAIN (decl))
8448 : 111 : ctx->global->clear_value (decl);
8449 : 111 : if (cond_cleanup)
8450 : : {
8451 : : /* If COND_CLEANUP is non-NULL, we need to evaluate DEPTH
8452 : : nested STATEMENT_LISTs from inside of BIND_EXPR_BODY,
8453 : : but defer the evaluation of CLEANUP_EXPRs of CLEANUP_STMT
8454 : : at the end of those STATEMENT_LISTs. */
8455 : 111 : cond_cleanup_depth = 0;
8456 : 111 : tree s = BIND_EXPR_BODY (cond_prep);
8457 : 111 : for (unsigned depth = tree_to_uhwi (cond_cleanup);
8458 : 222 : depth; --depth)
8459 : : {
8460 : 111 : for (tree_stmt_iterator i = tsi_start (s);
8461 : 321 : !tsi_end_p (i); ++i)
8462 : : {
8463 : 321 : tree stmt = *i;
8464 : 321 : if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT)
8465 : 0 : continue;
8466 : 321 : if (tsi_one_before_end_p (i))
8467 : : {
8468 : : /* The last statement in the STATEMENT_LIST
8469 : : has to be a CLEANUP_STMT (verified in
8470 : : finish_loop_cond_prep). We want to
8471 : : evaluate just its CLEANUP_BODY part but not
8472 : : CLEANUP_EXPR part just yet. */
8473 : 105 : gcc_assert (TREE_CODE (stmt) == CLEANUP_STMT);
8474 : : /* If the CLEANUP_STMT is not actually to be
8475 : : evaluated, don't increment cond_cleanup_depth
8476 : : so that we don't evaluate the CLEANUP_EXPR
8477 : : for it later either. */
8478 : 105 : if (*jump_target)
8479 : : {
8480 : : depth = 1;
8481 : : break;
8482 : : }
8483 : 105 : ++cond_cleanup_depth;
8484 : : /* If not in the innermost one, next iteration
8485 : : will handle CLEANUP_BODY similarly. */
8486 : 105 : if (depth > 1)
8487 : : {
8488 : 0 : s = CLEANUP_BODY (stmt);
8489 : 0 : break;
8490 : : }
8491 : : /* The innermost one can be evaluated normally. */
8492 : 105 : cxx_eval_constant_expression (ctx,
8493 : 105 : CLEANUP_BODY (stmt),
8494 : : vc_discard,
8495 : : non_constant_p,
8496 : : overflow_p,
8497 : : jump_target);
8498 : 105 : break;
8499 : : }
8500 : : /* And so should be evaluated statements which aren't
8501 : : last in the STATEMENT_LIST. */
8502 : 216 : cxx_eval_constant_expression (ctx, stmt, vc_discard,
8503 : : non_constant_p, overflow_p,
8504 : : jump_target);
8505 : 216 : if (*non_constant_p
8506 : 210 : || returns (jump_target)
8507 : 210 : || breaks (jump_target)
8508 : 210 : || continues (jump_target)
8509 : 531 : || throws (jump_target))
8510 : : {
8511 : : depth = 1;
8512 : : break;
8513 : : }
8514 : : }
8515 : : }
8516 : : }
8517 : : else
8518 : 0 : cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (cond_prep),
8519 : : vc_discard, non_constant_p,
8520 : : overflow_p, jump_target);
8521 : : }
8522 : :
8523 : 7650452 : if (cond)
8524 : : {
8525 : 7649399 : tree res
8526 : 7649399 : = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
8527 : : non_constant_p, overflow_p,
8528 : : jump_target);
8529 : 7649399 : if (res)
8530 : : {
8531 : 7613156 : if (verify_constant (res, ctx->quiet, non_constant_p,
8532 : : overflow_p))
8533 : : break;
8534 : 7391519 : if (integer_zerop (res))
8535 : : break;
8536 : : }
8537 : : else
8538 : 36243 : gcc_assert (*jump_target);
8539 : : }
8540 : :
8541 : 6709270 : if (++count >= constexpr_loop_limit)
8542 : : {
8543 : 28 : if (!ctx->quiet)
8544 : 9 : error_at (cp_expr_loc_or_input_loc (t),
8545 : : "%<constexpr%> loop iteration count exceeds limit of %d "
8546 : : "(use %<-fconstexpr-loop-limit=%> to increase the limit)",
8547 : : constexpr_loop_limit);
8548 : 28 : *non_constant_p = true;
8549 : 28 : break;
8550 : : }
8551 : : }
8552 : 20091382 : while (!returns (jump_target)
8553 : 6672898 : && !breaks (jump_target)
8554 : 6672898 : && !continues (jump_target)
8555 : 6672991 : && (!switches (jump_target) || count == 0)
8556 : 6672868 : && !throws (jump_target)
8557 : 13418618 : && !*non_constant_p);
8558 : :
8559 : 978338 : cleanup_cond ();
8560 : :
8561 : 978338 : return NULL_TREE;
8562 : : }
8563 : :
8564 : : /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
8565 : : semantics. */
8566 : :
8567 : : static tree
8568 : 37275 : cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t,
8569 : : bool *non_constant_p, bool *overflow_p,
8570 : : tree *jump_target)
8571 : : {
8572 : 37275 : tree cond
8573 : 37275 : = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_COND (t) : SWITCH_COND (t);
8574 : 37275 : cond = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
8575 : : non_constant_p, overflow_p,
8576 : : jump_target);
8577 : 37275 : if (*jump_target)
8578 : : return NULL_TREE;
8579 : 37275 : VERIFY_CONSTANT (cond);
8580 : 36957 : if (TREE_CODE (cond) != INTEGER_CST)
8581 : : {
8582 : : /* If the condition doesn't reduce to an INTEGER_CST it isn't a usable
8583 : : switch condition even if it's constant enough for other things
8584 : : (c++/113545). */
8585 : 0 : gcc_checking_assert (ctx->quiet);
8586 : 0 : *non_constant_p = true;
8587 : 0 : return t;
8588 : : }
8589 : :
8590 : 36957 : *jump_target = cond;
8591 : :
8592 : 36957 : tree body
8593 : 36957 : = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_BODY (t) : SWITCH_BODY (t);
8594 : 36957 : constexpr_ctx new_ctx = *ctx;
8595 : 36957 : constexpr_switch_state css = css_default_not_seen;
8596 : 36957 : new_ctx.css_state = &css;
8597 : 36957 : cxx_eval_constant_expression (&new_ctx, body, vc_discard,
8598 : : non_constant_p, overflow_p, jump_target);
8599 : 65310 : if (switches (jump_target) && css == css_default_seen)
8600 : : {
8601 : : /* If the SWITCH_EXPR body has default: label, process it once again,
8602 : : this time instructing label_matches to return true for default:
8603 : : label on switches (jump_target). */
8604 : 28309 : css = css_default_processing;
8605 : 28309 : cxx_eval_constant_expression (&new_ctx, body, vc_discard,
8606 : : non_constant_p, overflow_p, jump_target);
8607 : : }
8608 : 36957 : if (breaks (jump_target) || switches (jump_target))
8609 : 22496 : *jump_target = NULL_TREE;
8610 : : return NULL_TREE;
8611 : : }
8612 : :
8613 : : /* Find the object of TYPE under initialization in CTX. */
8614 : :
8615 : : static tree
8616 : 16631 : lookup_placeholder (const constexpr_ctx *ctx, value_cat lval, tree type)
8617 : : {
8618 : 16631 : if (!ctx)
8619 : : return NULL_TREE;
8620 : :
8621 : : /* Prefer the outermost matching object, but don't cross
8622 : : CONSTRUCTOR_PLACEHOLDER_BOUNDARY constructors. */
8623 : 16299 : if (ctx->ctor && !CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor))
8624 : 510 : if (tree outer_ob = lookup_placeholder (ctx->parent, lval, type))
8625 : : return outer_ob;
8626 : :
8627 : : /* We could use ctx->object unconditionally, but using ctx->ctor when we
8628 : : can is a minor optimization. */
8629 : 16121 : if (!lval && ctx->ctor && same_type_p (TREE_TYPE (ctx->ctor), type))
8630 : 300 : return ctx->ctor;
8631 : :
8632 : 15821 : if (!ctx->object)
8633 : : return NULL_TREE;
8634 : :
8635 : : /* Since an object cannot have a field of its own type, we can search outward
8636 : : from ctx->object to find the unique containing object of TYPE. */
8637 : : tree ob = ctx->object;
8638 : 15812 : while (ob)
8639 : : {
8640 : 15812 : if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob), type))
8641 : : break;
8642 : 223 : if (handled_component_p (ob))
8643 : 221 : ob = TREE_OPERAND (ob, 0);
8644 : : else
8645 : : ob = NULL_TREE;
8646 : : }
8647 : :
8648 : : return ob;
8649 : : }
8650 : :
8651 : : /* Complain about an attempt to evaluate inline assembly. If FUNDEF_P is
8652 : : true, we're checking a constexpr function body. */
8653 : :
8654 : : static void
8655 : 31 : inline_asm_in_constexpr_error (location_t loc, bool fundef_p)
8656 : : {
8657 : 31 : auto_diagnostic_group d;
8658 : 31 : if (constexpr_error (loc, fundef_p, "inline assembly is not a "
8659 : : "constant expression"))
8660 : 31 : inform (loc, "only unevaluated inline assembly is allowed in a "
8661 : : "%<constexpr%> function in C++20");
8662 : 31 : }
8663 : :
8664 : : /* We're getting the constant value of DECL in a manifestly constant-evaluated
8665 : : context; maybe complain about that. */
8666 : :
8667 : : static void
8668 : 59686262 : maybe_warn_about_constant_value (location_t loc, tree decl)
8669 : : {
8670 : 59686262 : static bool explained = false;
8671 : 59686262 : if (cxx_dialect >= cxx17
8672 : 59531443 : && warn_interference_size
8673 : 59531443 : && !OPTION_SET_P (param_destruct_interfere_size)
8674 : 59531443 : && DECL_CONTEXT (decl) == std_node
8675 : 7834448 : && DECL_NAME (decl)
8676 : 7834442 : && id_equal (DECL_NAME (decl), "hardware_destructive_interference_size")
8677 : 12 : && (LOCATION_FILE (input_location) != main_input_filename
8678 : 6 : || module_exporting_p ())
8679 : 59686265 : && warning_at (loc, OPT_Winterference_size, "use of %qD", decl)
8680 : 59686271 : && !explained)
8681 : : {
8682 : 6 : explained = true;
8683 : 6 : inform (loc, "its value can vary between compiler versions or "
8684 : : "with different %<-mtune%> or %<-mcpu%> flags");
8685 : 6 : inform (loc, "if this use is part of a public ABI, change it to "
8686 : : "instead use a constant variable you define");
8687 : 6 : inform (loc, "the default value for the current CPU tuning "
8688 : : "is %d bytes", param_destruct_interfere_size);
8689 : 6 : inform (loc, "you can stabilize this value with %<--param "
8690 : : "hardware_destructive_interference_size=%d%>, or disable "
8691 : : "this warning with %<-Wno-interference-size%>",
8692 : : param_destruct_interfere_size);
8693 : : }
8694 : 59686262 : }
8695 : :
8696 : : /* For element type ELT_TYPE, return the appropriate type of the heap object
8697 : : containing such element(s). COOKIE_SIZE is NULL or the size of cookie
8698 : : in bytes. If COOKIE_SIZE is NULL, return array type
8699 : : ELT_TYPE[FULL_SIZE / sizeof(ELT_TYPE)], otherwise return
8700 : : struct { size_t[COOKIE_SIZE/sizeof(size_t)]; ELT_TYPE[N]; }
8701 : : where N is computed such that the size of the struct fits into FULL_SIZE.
8702 : : If ARG_SIZE is non-NULL, it is the first argument to the new operator.
8703 : : It should be passed if ELT_TYPE is zero sized type in which case FULL_SIZE
8704 : : will be also 0 and so it is not possible to determine the actual array
8705 : : size. CTX, NON_CONSTANT_P and OVERFLOW_P are used during constant
8706 : : expression evaluation of subexpressions of ARG_SIZE. */
8707 : :
8708 : : static tree
8709 : 9954 : build_new_constexpr_heap_type (const constexpr_ctx *ctx, tree elt_type,
8710 : : tree cookie_size, tree full_size, tree arg_size,
8711 : : bool *non_constant_p, bool *overflow_p,
8712 : : tree *jump_target)
8713 : : {
8714 : 9954 : gcc_assert (cookie_size == NULL_TREE || tree_fits_uhwi_p (cookie_size));
8715 : 9954 : gcc_assert (tree_fits_uhwi_p (full_size));
8716 : 9954 : unsigned HOST_WIDE_INT csz = cookie_size ? tree_to_uhwi (cookie_size) : 0;
8717 : 9954 : if (arg_size)
8718 : : {
8719 : 9 : STRIP_NOPS (arg_size);
8720 : 9 : if (cookie_size)
8721 : : {
8722 : 0 : if (TREE_CODE (arg_size) != PLUS_EXPR)
8723 : : arg_size = NULL_TREE;
8724 : 0 : else if (TREE_CODE (TREE_OPERAND (arg_size, 0)) == INTEGER_CST
8725 : 0 : && tree_int_cst_equal (cookie_size,
8726 : 0 : TREE_OPERAND (arg_size, 0)))
8727 : : {
8728 : 0 : arg_size = TREE_OPERAND (arg_size, 1);
8729 : 0 : STRIP_NOPS (arg_size);
8730 : : }
8731 : 0 : else if (TREE_CODE (TREE_OPERAND (arg_size, 1)) == INTEGER_CST
8732 : 0 : && tree_int_cst_equal (cookie_size,
8733 : 0 : TREE_OPERAND (arg_size, 1)))
8734 : : {
8735 : 0 : arg_size = TREE_OPERAND (arg_size, 0);
8736 : 0 : STRIP_NOPS (arg_size);
8737 : : }
8738 : : else
8739 : : arg_size = NULL_TREE;
8740 : : }
8741 : 9 : if (arg_size && TREE_CODE (arg_size) == MULT_EXPR)
8742 : : {
8743 : 9 : tree op0 = TREE_OPERAND (arg_size, 0);
8744 : 9 : tree op1 = TREE_OPERAND (arg_size, 1);
8745 : 9 : if (integer_zerop (op0))
8746 : 9 : arg_size
8747 : 9 : = cxx_eval_constant_expression (ctx, op1, vc_prvalue,
8748 : : non_constant_p, overflow_p,
8749 : : jump_target);
8750 : 0 : else if (integer_zerop (op1))
8751 : 0 : arg_size
8752 : 0 : = cxx_eval_constant_expression (ctx, op0, vc_prvalue,
8753 : : non_constant_p, overflow_p,
8754 : : jump_target);
8755 : : else
8756 : : arg_size = NULL_TREE;
8757 : 9 : if (*jump_target)
8758 : : return NULL_TREE;
8759 : : }
8760 : : else
8761 : : arg_size = NULL_TREE;
8762 : : }
8763 : :
8764 : 9 : unsigned HOST_WIDE_INT fsz = tree_to_uhwi (arg_size ? arg_size : full_size);
8765 : 9954 : if (!arg_size)
8766 : : {
8767 : 9945 : unsigned HOST_WIDE_INT esz = int_size_in_bytes (elt_type);
8768 : 9945 : gcc_assert (fsz >= csz);
8769 : 9945 : fsz -= csz;
8770 : 9945 : if (esz)
8771 : 9945 : fsz /= esz;
8772 : : }
8773 : 9954 : tree itype2 = build_index_type (size_int (fsz - 1));
8774 : 9954 : if (!cookie_size)
8775 : 9945 : return build_cplus_array_type (elt_type, itype2);
8776 : 9 : return build_new_constexpr_heap_type (elt_type, cookie_size, itype2);
8777 : : }
8778 : :
8779 : : /* Handle the case when a cleanup of some expression throws. JMP_TARGET
8780 : : indicates whether the cleanup threw or not, *JUMP_TARGET indicates whether
8781 : : the expression which needed the cleanup threw. If both threw, diagnose
8782 : : it and return NULL, otherwise return R. If only the cleanup threw, set
8783 : : *JUMP_TARGET to the exception object from the cleanup. */
8784 : :
8785 : : static tree
8786 : 77822 : merge_jump_target (location_t loc, const constexpr_ctx *ctx, tree r,
8787 : : bool *non_constant_p, tree *jump_target, tree jmp_target)
8788 : : {
8789 : 77823 : if (!throws (&jmp_target))
8790 : : return r;
8791 : 7 : if (throws (jump_target))
8792 : : {
8793 : : /* [except.throw]/9 - If the exception handling mechanism
8794 : : handling an uncaught exception directly invokes a function
8795 : : that exits via an exception, the function std::terminate is
8796 : : invoked. */
8797 : 6 : if (!ctx->quiet)
8798 : : {
8799 : 2 : auto_diagnostic_group d;
8800 : 2 : diagnose_std_terminate (loc, ctx, *jump_target);
8801 : 2 : inform (loc, "destructor exited with an exception");
8802 : 2 : }
8803 : 6 : *non_constant_p = true;
8804 : 6 : *jump_target = NULL_TREE;
8805 : 6 : return NULL_TREE;
8806 : : }
8807 : 1 : *jump_target = jmp_target;
8808 : 1 : return r;
8809 : : }
8810 : :
8811 : : /* Attempt to reduce the expression T to a constant value.
8812 : : On failure, issue diagnostic and return error_mark_node. */
8813 : : /* FIXME unify with c_fully_fold */
8814 : : /* FIXME overflow_p is too global */
8815 : :
8816 : : static tree
8817 : 1225692545 : cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
8818 : : value_cat lval,
8819 : : bool *non_constant_p, bool *overflow_p,
8820 : : tree *jump_target)
8821 : : {
8822 : 1225692545 : if (*jump_target)
8823 : : {
8824 : : /* If we are jumping, ignore all statements/expressions except those
8825 : : that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */
8826 : 1440006 : switch (TREE_CODE (t))
8827 : : {
8828 : : case BIND_EXPR:
8829 : : case STATEMENT_LIST:
8830 : : case LOOP_EXPR:
8831 : : case COND_EXPR:
8832 : : case IF_STMT:
8833 : : case DO_STMT:
8834 : : case WHILE_STMT:
8835 : : case FOR_STMT:
8836 : : break;
8837 : 393641 : case LABEL_EXPR:
8838 : 393641 : case CASE_LABEL_EXPR:
8839 : 393641 : if (label_matches (ctx, jump_target, t))
8840 : : /* Found it. */
8841 : 36913 : *jump_target = NULL_TREE;
8842 : 393641 : return NULL_TREE;
8843 : : default:
8844 : : return NULL_TREE;
8845 : : }
8846 : : }
8847 : 1224477021 : if (error_operand_p (t))
8848 : : {
8849 : 133 : *non_constant_p = true;
8850 : 133 : return t;
8851 : : }
8852 : :
8853 : : /* Change the input location to the currently processed expression for
8854 : : better error messages when a subexpression has no location. */
8855 : 1224476888 : location_t loc = cp_expr_loc_or_input_loc (t);
8856 : 2448948363 : iloc_sentinel sentinel (loc);
8857 : :
8858 : 1224476888 : STRIP_ANY_LOCATION_WRAPPER (t);
8859 : :
8860 : 1224476888 : if (CONSTANT_CLASS_P (t))
8861 : : {
8862 : 259542177 : if (TREE_OVERFLOW (t))
8863 : : {
8864 : 62 : if (!ctx->quiet)
8865 : 15 : permerror (input_location, "overflow in constant expression");
8866 : 62 : if (!flag_permissive || ctx->quiet)
8867 : 59 : *overflow_p = true;
8868 : : }
8869 : :
8870 : 259542177 : if (TREE_CODE (t) == INTEGER_CST
8871 : 250319532 : && TYPE_PTR_P (TREE_TYPE (t))
8872 : : /* INTEGER_CST with pointer-to-method type is only used
8873 : : for a virtual method in a pointer to member function.
8874 : : Don't reject those. */
8875 : 848185 : && TREE_CODE (TREE_TYPE (TREE_TYPE (t))) != METHOD_TYPE
8876 : 260390051 : && !integer_zerop (t))
8877 : : {
8878 : 6 : if (!ctx->quiet)
8879 : 0 : error ("value %qE of type %qT is not a constant expression",
8880 : 0 : t, TREE_TYPE (t));
8881 : 6 : *non_constant_p = true;
8882 : : }
8883 : :
8884 : 259542177 : return t;
8885 : : }
8886 : :
8887 : : /* Avoid excessively long constexpr evaluations. */
8888 : 964934711 : if (++ctx->global->constexpr_ops_count >= constexpr_ops_limit)
8889 : : {
8890 : 9 : if (!ctx->quiet)
8891 : 3 : error_at (loc,
8892 : : "%<constexpr%> evaluation operation count exceeds limit of "
8893 : : "%wd (use %<-fconstexpr-ops-limit=%> to increase the limit)",
8894 : : constexpr_ops_limit);
8895 : 9 : ctx->global->constexpr_ops_count = INTTYPE_MINIMUM (HOST_WIDE_INT);
8896 : 9 : *non_constant_p = true;
8897 : 9 : return t;
8898 : : }
8899 : :
8900 : 964934702 : constexpr_ctx new_ctx;
8901 : 964934702 : tree r = t;
8902 : :
8903 : 964934702 : tree_code tcode = TREE_CODE (t);
8904 : 964934702 : switch (tcode)
8905 : : {
8906 : 15389019 : case RESULT_DECL:
8907 : 15389019 : if (lval)
8908 : : return t;
8909 : : /* We ask for an rvalue for the RESULT_DECL when indirecting
8910 : : through an invisible reference, or in named return value
8911 : : optimization. */
8912 : 38975 : if (tree v = ctx->global->get_value (t))
8913 : : return v;
8914 : : else
8915 : : {
8916 : 3 : if (!ctx->quiet)
8917 : 0 : error ("%qE is not a constant expression", t);
8918 : 3 : *non_constant_p = true;
8919 : : }
8920 : 3 : break;
8921 : :
8922 : 137014473 : case VAR_DECL:
8923 : 137014473 : if (DECL_HAS_VALUE_EXPR_P (t))
8924 : : {
8925 : 1308333 : if (is_normal_capture_proxy (t)
8926 : 1308333 : && current_function_decl == DECL_CONTEXT (t))
8927 : : {
8928 : : /* Function parms aren't constexpr within the function
8929 : : definition, so don't try to look at the closure. But if the
8930 : : captured variable is constant, try to evaluate it directly. */
8931 : 393047 : r = DECL_CAPTURED_VARIABLE (t);
8932 : : }
8933 : : else
8934 : 915286 : r = DECL_VALUE_EXPR (t);
8935 : :
8936 : 1308333 : tree type = TREE_TYPE (t);
8937 : 1308333 : if (TYPE_REF_P (type) != TYPE_REF_P (TREE_TYPE (r)))
8938 : : {
8939 : : /* Adjust r to match the reference-ness of t. */
8940 : 245586 : if (TYPE_REF_P (type))
8941 : 245535 : r = build_address (r);
8942 : : else
8943 : 51 : r = convert_from_reference (r);
8944 : : }
8945 : 1308333 : return cxx_eval_constant_expression (ctx, r, lval, non_constant_p,
8946 : 1308333 : overflow_p, jump_target);
8947 : : }
8948 : : /* fall through */
8949 : 143464757 : case CONST_DECL:
8950 : : /* We used to not check lval for CONST_DECL, but darwin.cc uses
8951 : : CONST_DECL for aggregate constants. */
8952 : 143464757 : if (lval)
8953 : : return t;
8954 : 112924030 : else if (t == ctx->object)
8955 : 535314 : return ctx->ctor;
8956 : 112388716 : if (VAR_P (t))
8957 : : {
8958 : 104630099 : if (tree v = ctx->global->get_value (t))
8959 : : {
8960 : : r = v;
8961 : : break;
8962 : : }
8963 : 90150815 : if (ctx->global->is_outside_lifetime (t))
8964 : : {
8965 : 96 : if (!ctx->quiet)
8966 : 28 : outside_lifetime_error (loc, t);
8967 : 96 : *non_constant_p = true;
8968 : 96 : break;
8969 : : }
8970 : : }
8971 : 97909336 : if (ctx->manifestly_const_eval == mce_true)
8972 : 59686262 : maybe_warn_about_constant_value (loc, t);
8973 : 97909336 : if (COMPLETE_TYPE_P (TREE_TYPE (t))
8974 : 97909336 : && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
8975 : : {
8976 : : /* If the class is empty, we aren't actually loading anything. */
8977 : 84229 : r = build_constructor (TREE_TYPE (t), NULL);
8978 : 84229 : TREE_CONSTANT (r) = true;
8979 : : }
8980 : 97825107 : else if (ctx->strict)
8981 : 97549261 : r = decl_really_constant_value (t, /*unshare_p=*/false);
8982 : : else
8983 : 275846 : r = decl_constant_value (t, /*unshare_p=*/false);
8984 : 97906639 : if (TREE_CODE (r) == TARGET_EXPR
8985 : 97906639 : && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR)
8986 : 0 : r = TARGET_EXPR_INITIAL (r);
8987 : 97906639 : if (DECL_P (r)
8988 : : /* P2280 allows references to unknown. */
8989 : 98060040 : && !(p2280_active_p (ctx) && VAR_P (t) && TYPE_REF_P (TREE_TYPE (t))))
8990 : : {
8991 : 27857857 : if (!ctx->quiet)
8992 : 168 : non_const_var_error (loc, r, /*fundef_p*/false);
8993 : 27857857 : *non_constant_p = true;
8994 : : }
8995 : : break;
8996 : :
8997 : : case DEBUG_BEGIN_STMT:
8998 : : /* ??? It might be nice to retain this information somehow, so
8999 : : as to be able to step into a constexpr function call. */
9000 : : /* Fall through. */
9001 : :
9002 : : case FUNCTION_DECL:
9003 : : case TEMPLATE_DECL:
9004 : : case LABEL_DECL:
9005 : : case LABEL_EXPR:
9006 : : case CASE_LABEL_EXPR:
9007 : : case PREDICT_EXPR:
9008 : : case OMP_DECLARE_MAPPER:
9009 : : return t;
9010 : :
9011 : 111263786 : case PARM_DECL:
9012 : 111263786 : if (lval && !TYPE_REF_P (TREE_TYPE (t)))
9013 : : {
9014 : : /* glvalue use. */
9015 : 5749662 : if (TREE_ADDRESSABLE (TREE_TYPE (t)))
9016 : 116304 : if (tree v = ctx->global->get_value (t))
9017 : 770698527 : r = v;
9018 : : }
9019 : 105514124 : else if (tree v = ctx->global->get_value (t))
9020 : : {
9021 : 39977375 : r = v;
9022 : 39977375 : if (TREE_ADDRESSABLE (TREE_TYPE (t)))
9023 : 4719 : r = cxx_eval_constant_expression (ctx, r, vc_prvalue,
9024 : : non_constant_p, overflow_p,
9025 : : jump_target);
9026 : 39977375 : if (*jump_target)
9027 : : return NULL_TREE;
9028 : : }
9029 : 65536749 : else if (lval)
9030 : : /* Defer in case this is only used for its type. */;
9031 : 65536749 : else if (ctx->global->is_outside_lifetime (t))
9032 : : {
9033 : 18 : if (!ctx->quiet)
9034 : 6 : outside_lifetime_error (loc, t);
9035 : 18 : *non_constant_p = true;
9036 : 18 : break;
9037 : : }
9038 : 65536731 : else if (COMPLETE_TYPE_P (TREE_TYPE (t))
9039 : 65536731 : && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
9040 : : {
9041 : : /* If the class is empty, we aren't actually loading anything. */
9042 : 4183 : r = build_constructor (TREE_TYPE (t), NULL);
9043 : 4183 : TREE_CONSTANT (r) = true;
9044 : : }
9045 : 65532548 : else if (p2280_active_p (ctx) && TYPE_REF_P (TREE_TYPE (t)))
9046 : : /* P2280 allows references to unknown... */;
9047 : 65460962 : else if (p2280_active_p (ctx) && is_this_parameter (t))
9048 : : /* ...as well as the this pointer. */;
9049 : : else
9050 : : {
9051 : 65304038 : if (!ctx->quiet)
9052 : 194 : error ("%qE is not a constant expression", t);
9053 : 65304038 : *non_constant_p = true;
9054 : : }
9055 : : break;
9056 : :
9057 : 87998367 : case CALL_EXPR:
9058 : 87998367 : case AGGR_INIT_EXPR:
9059 : 87998367 : r = cxx_eval_call_expression (ctx, t, lval,
9060 : : non_constant_p, overflow_p, jump_target);
9061 : 87998367 : break;
9062 : :
9063 : 4652118 : case DECL_EXPR:
9064 : 4652118 : {
9065 : 4652118 : r = DECL_EXPR_DECL (t);
9066 : 4652118 : if (TREE_CODE (r) == USING_DECL)
9067 : : {
9068 : 2405 : r = void_node;
9069 : 2405 : break;
9070 : : }
9071 : :
9072 : 4649713 : if (VAR_P (r)
9073 : 4649713 : && (TREE_STATIC (r)
9074 : 4544458 : || (CP_DECL_THREAD_LOCAL_P (r) && !DECL_REALLY_EXTERN (r)))
9075 : : /* Allow __FUNCTION__ etc. */
9076 : 105255 : && !DECL_ARTIFICIAL (r)
9077 : 4649732 : && !decl_constant_var_p (r))
9078 : : {
9079 : 7 : if (!ctx->quiet)
9080 : : {
9081 : 2 : if (CP_DECL_THREAD_LOCAL_P (r))
9082 : 1 : error_at (loc, "control passes through definition of %qD "
9083 : : "with thread storage duration", r);
9084 : : else
9085 : 1 : error_at (loc, "control passes through definition of %qD "
9086 : : "with static storage duration", r);
9087 : : }
9088 : 7 : *non_constant_p = true;
9089 : 7 : break;
9090 : : }
9091 : :
9092 : : /* make_rtl_for_nonlocal_decl could have deferred emission of
9093 : : a local static var, but if it appears in a statement expression
9094 : : which is constant expression evaluated to e.g. just the address
9095 : : of the variable, its DECL_EXPR will never be seen during
9096 : : gimple lowering's record_vars_into as the statement expression
9097 : : will not be in the IL at all. */
9098 : 4649706 : if (VAR_P (r)
9099 : 4649706 : && TREE_STATIC (r)
9100 : 105248 : && !DECL_REALLY_EXTERN (r)
9101 : 105248 : && DECL_FUNCTION_SCOPE_P (r)
9102 : 105248 : && !var_in_maybe_constexpr_fn (r)
9103 : 4649709 : && decl_constant_var_p (r))
9104 : : {
9105 : 3 : varpool_node *node = varpool_node::get (r);
9106 : 3 : if (node == NULL || !node->definition)
9107 : 3 : rest_of_decl_compilation (r, 0, at_eof);
9108 : : }
9109 : :
9110 : 9184755 : if (AGGREGATE_TYPE_P (TREE_TYPE (r))
9111 : 8879197 : || VECTOR_TYPE_P (TREE_TYPE (r)))
9112 : : {
9113 : 420421 : new_ctx = *ctx;
9114 : 420421 : new_ctx.object = r;
9115 : 420421 : new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL);
9116 : 420421 : CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
9117 : 420421 : ctx->global->put_value (r, new_ctx.ctor);
9118 : 420421 : ctx = &new_ctx;
9119 : : }
9120 : :
9121 : 4649706 : if (tree init = DECL_INITIAL (r))
9122 : : {
9123 : 2391412 : init = cxx_eval_constant_expression (ctx, init, vc_prvalue,
9124 : : non_constant_p, overflow_p,
9125 : : jump_target);
9126 : 2391412 : if (*jump_target)
9127 : : return NULL_TREE;
9128 : : /* Don't share a CONSTRUCTOR that might be changed. */
9129 : 2391412 : init = unshare_constructor (init);
9130 : : /* Remember that a constant object's constructor has already
9131 : : run. */
9132 : 4782824 : if (CLASS_TYPE_P (TREE_TYPE (r))
9133 : 2496087 : && CP_TYPE_CONST_P (TREE_TYPE (r)))
9134 : 3430 : TREE_READONLY (init) = true;
9135 : 2391412 : ctx->global->put_value (r, init);
9136 : : }
9137 : 2258294 : else if (ctx == &new_ctx)
9138 : : /* We gave it a CONSTRUCTOR above. */;
9139 : : else
9140 : 1951900 : ctx->global->put_value (r, NULL_TREE);
9141 : : }
9142 : : break;
9143 : :
9144 : 9441770 : case TARGET_EXPR:
9145 : 9441770 : {
9146 : 9441770 : tree type = TREE_TYPE (t);
9147 : :
9148 : 9441770 : if (!literal_type_p (type))
9149 : : {
9150 : 2429 : if (!ctx->quiet)
9151 : : {
9152 : 0 : auto_diagnostic_group d;
9153 : 0 : error ("temporary of non-literal type %qT in a "
9154 : : "constant expression", type);
9155 : 0 : explain_non_literal_class (type);
9156 : 0 : }
9157 : 2429 : *non_constant_p = true;
9158 : 3326992 : break;
9159 : : }
9160 : 9439341 : gcc_checking_assert (!TARGET_EXPR_DIRECT_INIT_P (t));
9161 : : /* Avoid evaluating a TARGET_EXPR more than once. */
9162 : 9439341 : tree slot = TARGET_EXPR_SLOT (t);
9163 : 9439341 : if (tree v = ctx->global->get_value (slot))
9164 : : {
9165 : 756 : if (lval)
9166 : 5075515 : return slot;
9167 : : r = v;
9168 : : break;
9169 : : }
9170 : 9438585 : if ((AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type)))
9171 : : {
9172 : : /* We're being expanded without an explicit target, so start
9173 : : initializing a new object; expansion with an explicit target
9174 : : strips the TARGET_EXPR before we get here. */
9175 : 7466204 : new_ctx = *ctx;
9176 : : /* Link CTX to NEW_CTX so that lookup_placeholder can resolve
9177 : : any PLACEHOLDER_EXPR within the initializer that refers to the
9178 : : former object under construction. */
9179 : 7466204 : new_ctx.parent = ctx;
9180 : 7466204 : new_ctx.ctor = build_constructor (type, NULL);
9181 : 7466204 : CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
9182 : 7466204 : new_ctx.object = slot;
9183 : 7466204 : ctx->global->put_value (new_ctx.object, new_ctx.ctor);
9184 : 7466204 : ctx = &new_ctx;
9185 : : }
9186 : :
9187 : : /* If the initializer is complex, evaluate it to initialize slot. */
9188 : 9438585 : bool is_complex = target_expr_needs_replace (t);
9189 : 9438585 : if (is_complex)
9190 : : /* In case no initialization actually happens, clear out any
9191 : : void_node from a previous evaluation. */
9192 : 45 : ctx->global->put_value (slot, NULL_TREE);
9193 : :
9194 : : /* Pass vc_prvalue because this indicates
9195 : : initialization of a temporary. */
9196 : 9438585 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), vc_prvalue,
9197 : : non_constant_p, overflow_p,
9198 : : jump_target);
9199 : 9438585 : if (*non_constant_p)
9200 : : break;
9201 : 6114565 : if (*jump_target)
9202 : : return NULL_TREE;
9203 : 6114561 : if (!is_complex)
9204 : : {
9205 : 6114522 : r = unshare_constructor (r);
9206 : : /* Adjust the type of the result to the type of the temporary. */
9207 : 6114522 : r = adjust_temp_type (type, r);
9208 : 6114522 : ctx->global->put_value (slot, r);
9209 : : }
9210 : 6114561 : if (TARGET_EXPR_CLEANUP (t)
9211 : 6114561 : && (!CLEANUP_EH_ONLY (t) || cxx_dialect >= cxx26))
9212 : : {
9213 : 258516 : ctx->global->cleanups->safe_push (TARGET_EXPR_CLEANUP (t));
9214 : : /* Mark CLEANUP_EH_ONLY cleanups by pushing NULL_TREE after
9215 : : them. */
9216 : 258516 : if (CLEANUP_EH_ONLY (t))
9217 : 351 : ctx->global->cleanups->safe_push (NULL_TREE);
9218 : : }
9219 : 6114561 : if (ctx->save_exprs)
9220 : 2361510 : ctx->save_exprs->safe_push (slot);
9221 : 6114561 : if (lval)
9222 : : return slot;
9223 : 1039263 : if (is_complex)
9224 : 0 : r = ctx->global->get_value (slot);
9225 : : }
9226 : 1039263 : break;
9227 : :
9228 : 32686351 : case INIT_EXPR:
9229 : 32686351 : case MODIFY_EXPR:
9230 : 32686351 : gcc_assert (jump_target == NULL || *jump_target == NULL_TREE);
9231 : 32686351 : r = cxx_eval_store_expression (ctx, t, lval,
9232 : : non_constant_p, overflow_p, jump_target);
9233 : 32686351 : break;
9234 : :
9235 : 0 : case SCOPE_REF:
9236 : 0 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
9237 : : lval,
9238 : : non_constant_p, overflow_p,
9239 : : jump_target);
9240 : 0 : break;
9241 : :
9242 : 20344819 : case RETURN_EXPR:
9243 : 20344819 : if (TREE_OPERAND (t, 0) != NULL_TREE)
9244 : 20315509 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
9245 : : lval,
9246 : : non_constant_p, overflow_p,
9247 : : jump_target);
9248 : 20344817 : if (!throws (jump_target))
9249 : 20344759 : *jump_target = t;
9250 : : break;
9251 : 23723 : case BREAK_STMT:
9252 : 23723 : case CONTINUE_STMT:
9253 : 23723 : *jump_target = t;
9254 : 23723 : break;
9255 : :
9256 : 635291 : case SAVE_EXPR:
9257 : : /* Avoid evaluating a SAVE_EXPR more than once. */
9258 : 635291 : if (tree v = ctx->global->get_value (t))
9259 : : r = v;
9260 : : else
9261 : : {
9262 : 628801 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
9263 : : vc_prvalue, non_constant_p,
9264 : : overflow_p, jump_target);
9265 : 628801 : if (*non_constant_p || *jump_target)
9266 : : break;
9267 : 8321 : ctx->global->put_value (t, r);
9268 : 8321 : if (ctx->save_exprs)
9269 : 6208 : ctx->save_exprs->safe_push (t);
9270 : : }
9271 : : break;
9272 : :
9273 : 19905853 : case NON_LVALUE_EXPR:
9274 : 19905853 : case EXPR_STMT:
9275 : 19905853 : case EH_SPEC_BLOCK:
9276 : 19905853 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
9277 : : lval,
9278 : : non_constant_p, overflow_p,
9279 : : jump_target);
9280 : 19905853 : break;
9281 : :
9282 : 446 : case TRY_BLOCK:
9283 : 446 : r = cxx_eval_constant_expression (ctx, TRY_STMTS (t), lval,
9284 : : non_constant_p, overflow_p,
9285 : : jump_target);
9286 : 446 : if (!*non_constant_p && throws (jump_target))
9287 : 195 : if (tree h = TRY_HANDLERS (t))
9288 : : {
9289 : 195 : tree type = strip_array_types (TREE_TYPE (*jump_target));
9290 : 195 : if (TREE_CODE (h) == STATEMENT_LIST)
9291 : : {
9292 : 334 : for (tree stmt : tsi_range (h))
9293 : 317 : if (TREE_CODE (stmt) == HANDLER
9294 : 317 : && handler_match_for_exception_type (stmt, type))
9295 : : {
9296 : : h = stmt;
9297 : : break;
9298 : : }
9299 : 95 : if (TREE_CODE (h) == STATEMENT_LIST)
9300 : : h = NULL_TREE;
9301 : : }
9302 : 100 : else if (TREE_CODE (h) != HANDLER
9303 : 100 : || !handler_match_for_exception_type (h, type))
9304 : : h = NULL_TREE;
9305 : : if (h)
9306 : : {
9307 : 178 : gcc_assert (VAR_P (*jump_target));
9308 : 178 : ctx->global->caught_exceptions.safe_push (*jump_target);
9309 : 178 : ctx->global->caught_exceptions.safe_push (HANDLER_TYPE (h));
9310 : 178 : *jump_target = NULL_TREE;
9311 : 178 : r = cxx_eval_constant_expression (ctx, HANDLER_BODY (h),
9312 : : vc_discard, non_constant_p,
9313 : : overflow_p, jump_target);
9314 : : }
9315 : : }
9316 : : break;
9317 : :
9318 : 27182194 : case CLEANUP_POINT_EXPR:
9319 : 27182194 : {
9320 : 27182194 : auto_vec<tree, 2> cleanups;
9321 : 27182194 : vec<tree> *prev_cleanups = ctx->global->cleanups;
9322 : 27182194 : ctx->global->cleanups = &cleanups;
9323 : :
9324 : 27182194 : auto_vec<tree, 10> save_exprs;
9325 : 27182194 : constexpr_ctx new_ctx = *ctx;
9326 : 27182194 : new_ctx.save_exprs = &save_exprs;
9327 : :
9328 : 27182194 : r = cxx_eval_constant_expression (&new_ctx, TREE_OPERAND (t, 0),
9329 : : lval,
9330 : : non_constant_p, overflow_p,
9331 : : jump_target);
9332 : :
9333 : 27182193 : ctx->global->cleanups = prev_cleanups;
9334 : 27182193 : unsigned int i;
9335 : 27182193 : tree cleanup, jmp_target = NULL_TREE;
9336 : 27182193 : bool eh = throws (jump_target);
9337 : : /* Evaluate the cleanups. */
9338 : 54602025 : FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup)
9339 : 237639 : if (cleanup == NULL_TREE)
9340 : : {
9341 : : /* NULL_TREE cleanup is a marker that before it is
9342 : : CLEANUP_EH_ONLY cleanup. Skip the cleanup before it
9343 : : if the body didn't throw. */
9344 : 328 : if (!eh)
9345 : 327 : --i;
9346 : : }
9347 : : else
9348 : 237311 : cxx_eval_constant_expression (&new_ctx, cleanup, vc_discard,
9349 : : non_constant_p, overflow_p,
9350 : : &jmp_target);
9351 : :
9352 : : /* Forget SAVE_EXPRs and TARGET_EXPRs created by this
9353 : : full-expression. */
9354 : 83914297 : for (tree save_expr : save_exprs)
9355 : 2367718 : destroy_value_checked (ctx, save_expr, non_constant_p);
9356 : 27182193 : if (throws (&jmp_target))
9357 : 0 : *jump_target = jmp_target;
9358 : 27182193 : }
9359 : 27182193 : break;
9360 : :
9361 : 19068664 : case MUST_NOT_THROW_EXPR:
9362 : 19068664 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
9363 : : lval,
9364 : : non_constant_p, overflow_p,
9365 : : jump_target);
9366 : 19068663 : if (throws (jump_target))
9367 : : {
9368 : : /* [except.handle]/7 - If the search for a handler exits the
9369 : : function body of a function with a non-throwing exception
9370 : : specification, the function std::terminate is invoked. */
9371 : 27 : if (!ctx->quiet)
9372 : : {
9373 : 9 : auto_diagnostic_group d;
9374 : 9 : diagnose_std_terminate (loc, ctx, *jump_target);
9375 : 9 : if (MUST_NOT_THROW_NOEXCEPT_P (t)
9376 : 7 : && ctx->call
9377 : 16 : && ctx->call->fundef)
9378 : 7 : inform (loc, "uncaught exception exited from %<noexcept%> "
9379 : : "function %qD",
9380 : : ctx->call->fundef->decl);
9381 : 2 : else if (MUST_NOT_THROW_THROW_P (t))
9382 : 1 : inform (loc, "destructor exited with an exception after "
9383 : : "initializing the exception object");
9384 : 1 : else if (MUST_NOT_THROW_CATCH_P (t))
9385 : 1 : inform (loc, "constructor exited with another exception while "
9386 : : "entering handler");
9387 : 9 : }
9388 : 27 : *non_constant_p = true;
9389 : 27 : *jump_target = NULL_TREE;
9390 : 27 : r = NULL_TREE;
9391 : : }
9392 : : break;
9393 : :
9394 : 0 : case TRY_CATCH_EXPR:
9395 : 0 : if (TREE_OPERAND (t, 0) == NULL_TREE)
9396 : : {
9397 : 0 : r = void_node;
9398 : 0 : break;
9399 : : }
9400 : 0 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
9401 : : non_constant_p, overflow_p,
9402 : : jump_target);
9403 : 0 : if (!*non_constant_p && throws (jump_target))
9404 : : {
9405 : 0 : tree jmp_target = NULL_TREE;
9406 : 0 : cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), vc_discard,
9407 : : non_constant_p, overflow_p,
9408 : : &jmp_target);
9409 : 0 : r = merge_jump_target (loc, ctx, r, non_constant_p, jump_target,
9410 : : jmp_target);
9411 : : }
9412 : : break;
9413 : :
9414 : 471 : case TRY_FINALLY_EXPR:
9415 : 471 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
9416 : : non_constant_p, overflow_p,
9417 : : jump_target);
9418 : 471 : if (!*non_constant_p)
9419 : : {
9420 : 456 : tree jmp_target = NULL_TREE;
9421 : : /* Also evaluate the cleanup. */
9422 : 456 : if (TREE_CODE (TREE_OPERAND (t, 1)) == EH_ELSE_EXPR
9423 : 456 : && throws (jump_target))
9424 : 0 : cxx_eval_constant_expression (ctx,
9425 : 0 : TREE_OPERAND (TREE_OPERAND (t, 1),
9426 : : 1), vc_discard,
9427 : : non_constant_p, overflow_p,
9428 : : &jmp_target);
9429 : : else
9430 : 456 : cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), vc_discard,
9431 : : non_constant_p, overflow_p,
9432 : : &jmp_target);
9433 : 456 : r = merge_jump_target (loc, ctx, r, non_constant_p, jump_target,
9434 : : jmp_target);
9435 : : }
9436 : : break;
9437 : :
9438 : 18 : case EH_ELSE_EXPR:
9439 : : /* Evaluate any cleanup that applies to non-EH exits. */
9440 : 18 : cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), vc_discard,
9441 : : non_constant_p, overflow_p,
9442 : : jump_target);
9443 : :
9444 : : /* The EH path is handled in TRY_FINALLY_EXPR handling above. */
9445 : 18 : break;
9446 : :
9447 : 703640 : case CLEANUP_STMT:
9448 : 703640 : r = cxx_eval_constant_expression (ctx, CLEANUP_BODY (t), lval,
9449 : : non_constant_p, overflow_p,
9450 : : jump_target);
9451 : 703639 : if ((!CLEANUP_EH_ONLY (t) || throws (jump_target)) && !*non_constant_p)
9452 : : {
9453 : 77366 : iloc_sentinel ils (loc);
9454 : 77366 : tree jmp_target = NULL_TREE;
9455 : : /* Also evaluate the cleanup. */
9456 : 77366 : cxx_eval_constant_expression (ctx, CLEANUP_EXPR (t), vc_discard,
9457 : : non_constant_p, overflow_p,
9458 : : &jmp_target);
9459 : 77366 : r = merge_jump_target (loc, ctx, r, non_constant_p, jump_target,
9460 : : jmp_target);
9461 : 77366 : }
9462 : : break;
9463 : :
9464 : : /* These differ from cxx_eval_unary_expression in that this doesn't
9465 : : check for a constant operand or result; an address can be
9466 : : constant without its operand being, and vice versa. */
9467 : 35475675 : case MEM_REF:
9468 : 35475675 : case INDIRECT_REF:
9469 : 35475675 : r = cxx_eval_indirect_ref (ctx, t, lval,
9470 : : non_constant_p, overflow_p,
9471 : : jump_target);
9472 : 35475675 : break;
9473 : :
9474 : 40598869 : case ADDR_EXPR:
9475 : 40598869 : {
9476 : 40598869 : tree oldop = TREE_OPERAND (t, 0);
9477 : 40598869 : tree op = cxx_eval_constant_expression (ctx, oldop, vc_glvalue,
9478 : : non_constant_p, overflow_p,
9479 : : jump_target);
9480 : 40598869 : if (*jump_target)
9481 : : return NULL_TREE;
9482 : : /* Don't VERIFY_CONSTANT here. */
9483 : 40598866 : if (*non_constant_p)
9484 : : return t;
9485 : 32839875 : gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR);
9486 : : /* This function does more aggressive folding than fold itself. */
9487 : 32839875 : r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
9488 : 32839875 : if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
9489 : : {
9490 : 24081539 : ggc_free (r);
9491 : 24081539 : return t;
9492 : : }
9493 : : break;
9494 : : }
9495 : :
9496 : 497943 : case REALPART_EXPR:
9497 : 497943 : case IMAGPART_EXPR:
9498 : 497943 : if (lval)
9499 : : {
9500 : 614 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
9501 : : non_constant_p, overflow_p,
9502 : : jump_target);
9503 : 614 : if (*jump_target)
9504 : : return NULL_TREE;
9505 : 614 : if (r == error_mark_node)
9506 : : ;
9507 : 614 : else if (r == TREE_OPERAND (t, 0) || lval == vc_discard)
9508 : : r = t;
9509 : : else
9510 : 568 : r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), r);
9511 : : break;
9512 : : }
9513 : : /* FALLTHRU */
9514 : 20710305 : case CONJ_EXPR:
9515 : 20710305 : case FIX_TRUNC_EXPR:
9516 : 20710305 : case FLOAT_EXPR:
9517 : 20710305 : case NEGATE_EXPR:
9518 : 20710305 : case ABS_EXPR:
9519 : 20710305 : case ABSU_EXPR:
9520 : 20710305 : case BIT_NOT_EXPR:
9521 : 20710305 : case TRUTH_NOT_EXPR:
9522 : 20710305 : case FIXED_CONVERT_EXPR:
9523 : 20710305 : case VEC_DUPLICATE_EXPR:
9524 : 20710305 : r = cxx_eval_unary_expression (ctx, t, lval,
9525 : : non_constant_p, overflow_p,
9526 : : jump_target);
9527 : 20710305 : break;
9528 : :
9529 : 7165388 : case SIZEOF_EXPR:
9530 : 7165388 : r = fold_sizeof_expr (t);
9531 : : /* In a template, fold_sizeof_expr may merely create a new SIZEOF_EXPR,
9532 : : which could lead to an infinite recursion. */
9533 : 7165388 : if (TREE_CODE (r) != SIZEOF_EXPR)
9534 : 7165388 : r = cxx_eval_constant_expression (ctx, r, lval,
9535 : : non_constant_p, overflow_p,
9536 : : jump_target);
9537 : : else
9538 : : {
9539 : 0 : *non_constant_p = true;
9540 : 0 : gcc_assert (ctx->quiet);
9541 : : }
9542 : :
9543 : : break;
9544 : :
9545 : 3711456 : case COMPOUND_EXPR:
9546 : 3711456 : {
9547 : : /* check_return_expr sometimes wraps a TARGET_EXPR in a
9548 : : COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
9549 : : introduced by build_call_a. */
9550 : 3711456 : tree op0 = TREE_OPERAND (t, 0);
9551 : 3711456 : tree op1 = TREE_OPERAND (t, 1);
9552 : 3711456 : STRIP_NOPS (op1);
9553 : 918494 : if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
9554 : 3884854 : || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
9555 : 3073620 : r = cxx_eval_constant_expression (ctx, op0,
9556 : : lval, non_constant_p, overflow_p,
9557 : : jump_target);
9558 : : else
9559 : : {
9560 : : /* Check that the LHS is constant and then discard it. */
9561 : 637836 : cxx_eval_constant_expression (ctx, op0, vc_discard,
9562 : : non_constant_p, overflow_p,
9563 : : jump_target);
9564 : 637836 : if (*jump_target)
9565 : : return NULL_TREE;
9566 : 637579 : if (*non_constant_p)
9567 : : return t;
9568 : 552007 : op1 = TREE_OPERAND (t, 1);
9569 : 552007 : r = cxx_eval_constant_expression (ctx, op1,
9570 : : lval, non_constant_p, overflow_p,
9571 : : jump_target);
9572 : : }
9573 : : }
9574 : : break;
9575 : :
9576 : 50343663 : case POINTER_PLUS_EXPR:
9577 : 50343663 : case POINTER_DIFF_EXPR:
9578 : 50343663 : case PLUS_EXPR:
9579 : 50343663 : case MINUS_EXPR:
9580 : 50343663 : case MULT_EXPR:
9581 : 50343663 : case TRUNC_DIV_EXPR:
9582 : 50343663 : case CEIL_DIV_EXPR:
9583 : 50343663 : case FLOOR_DIV_EXPR:
9584 : 50343663 : case ROUND_DIV_EXPR:
9585 : 50343663 : case TRUNC_MOD_EXPR:
9586 : 50343663 : case CEIL_MOD_EXPR:
9587 : 50343663 : case ROUND_MOD_EXPR:
9588 : 50343663 : case RDIV_EXPR:
9589 : 50343663 : case EXACT_DIV_EXPR:
9590 : 50343663 : case MIN_EXPR:
9591 : 50343663 : case MAX_EXPR:
9592 : 50343663 : case LSHIFT_EXPR:
9593 : 50343663 : case RSHIFT_EXPR:
9594 : 50343663 : case LROTATE_EXPR:
9595 : 50343663 : case RROTATE_EXPR:
9596 : 50343663 : case BIT_IOR_EXPR:
9597 : 50343663 : case BIT_XOR_EXPR:
9598 : 50343663 : case BIT_AND_EXPR:
9599 : 50343663 : case TRUTH_XOR_EXPR:
9600 : 50343663 : case LT_EXPR:
9601 : 50343663 : case LE_EXPR:
9602 : 50343663 : case GT_EXPR:
9603 : 50343663 : case GE_EXPR:
9604 : 50343663 : case EQ_EXPR:
9605 : 50343663 : case NE_EXPR:
9606 : 50343663 : case SPACESHIP_EXPR:
9607 : 50343663 : case UNORDERED_EXPR:
9608 : 50343663 : case ORDERED_EXPR:
9609 : 50343663 : case UNLT_EXPR:
9610 : 50343663 : case UNLE_EXPR:
9611 : 50343663 : case UNGT_EXPR:
9612 : 50343663 : case UNGE_EXPR:
9613 : 50343663 : case UNEQ_EXPR:
9614 : 50343663 : case LTGT_EXPR:
9615 : 50343663 : case RANGE_EXPR:
9616 : 50343663 : case COMPLEX_EXPR:
9617 : 50343663 : r = cxx_eval_binary_expression (ctx, t, lval,
9618 : : non_constant_p, overflow_p,
9619 : : jump_target);
9620 : 50343663 : break;
9621 : :
9622 : : /* fold can introduce non-IF versions of these; still treat them as
9623 : : short-circuiting. */
9624 : 5937458 : case TRUTH_AND_EXPR:
9625 : 5937458 : case TRUTH_ANDIF_EXPR:
9626 : 5937458 : r = cxx_eval_logical_expression (ctx, t, boolean_false_node,
9627 : : boolean_true_node,
9628 : : non_constant_p, overflow_p,
9629 : : jump_target);
9630 : 5937458 : break;
9631 : :
9632 : 1310302 : case TRUTH_OR_EXPR:
9633 : 1310302 : case TRUTH_ORIF_EXPR:
9634 : 1310302 : r = cxx_eval_logical_expression (ctx, t, boolean_true_node,
9635 : : boolean_false_node,
9636 : : non_constant_p, overflow_p,
9637 : : jump_target);
9638 : 1310302 : break;
9639 : :
9640 : 2976389 : case ARRAY_REF:
9641 : 2976389 : r = cxx_eval_array_reference (ctx, t, lval,
9642 : : non_constant_p, overflow_p,
9643 : : jump_target);
9644 : 2976389 : break;
9645 : :
9646 : 34289745 : case COMPONENT_REF:
9647 : 34289745 : if (is_overloaded_fn (t))
9648 : : {
9649 : : /* We can only get here in checking mode via
9650 : : build_non_dependent_expr, because any expression that
9651 : : calls or takes the address of the function will have
9652 : : pulled a FUNCTION_DECL out of the COMPONENT_REF. */
9653 : 6 : gcc_checking_assert (ctx->quiet || errorcount);
9654 : 6 : *non_constant_p = true;
9655 : 6 : return t;
9656 : : }
9657 : 34289739 : r = cxx_eval_component_reference (ctx, t, lval,
9658 : : non_constant_p, overflow_p,
9659 : : jump_target);
9660 : 34289739 : break;
9661 : :
9662 : 2885 : case BIT_FIELD_REF:
9663 : 2885 : r = cxx_eval_bit_field_ref (ctx, t, lval,
9664 : : non_constant_p, overflow_p,
9665 : : jump_target);
9666 : 2885 : break;
9667 : :
9668 : 9361402 : case COND_EXPR:
9669 : 9361402 : case IF_STMT:
9670 : 9361402 : if (*jump_target)
9671 : : {
9672 : 152803 : tree orig_jump = *jump_target;
9673 : 152803 : tree arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 1))
9674 : 305606 : ? TREE_OPERAND (t, 1) : void_node);
9675 : : /* When jumping to a label, the label might be either in the
9676 : : then or else blocks, so process then block first in skipping
9677 : : mode first, and if we are still in the skipping mode at its end,
9678 : : process the else block too. */
9679 : 152803 : r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p,
9680 : : overflow_p, jump_target);
9681 : : /* It's possible that we found the label in the then block. But
9682 : : it could have been followed by another jumping statement, e.g.
9683 : : say we're looking for case 1:
9684 : : if (cond)
9685 : : {
9686 : : // skipped statements
9687 : : case 1:; // clears up *jump_target
9688 : : return 1; // and sets it to a RETURN_EXPR
9689 : : }
9690 : : else { ... }
9691 : : in which case we need not go looking to the else block.
9692 : : (goto is not allowed in a constexpr function.) */
9693 : 152803 : if (*jump_target == orig_jump)
9694 : : {
9695 : 152851 : arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 2))
9696 : 152851 : ? TREE_OPERAND (t, 2) : void_node);
9697 : 152769 : r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p,
9698 : : overflow_p, jump_target);
9699 : : }
9700 : : break;
9701 : : }
9702 : 9208599 : r = cxx_eval_conditional_expression (ctx, t, lval,
9703 : : non_constant_p, overflow_p,
9704 : : jump_target);
9705 : 9208599 : break;
9706 : 866 : case VEC_COND_EXPR:
9707 : 866 : r = cxx_eval_vector_conditional_expression (ctx, t, non_constant_p,
9708 : : overflow_p, jump_target);
9709 : 866 : break;
9710 : :
9711 : 12822730 : case CONSTRUCTOR:
9712 : 12822730 : if (TREE_CONSTANT (t) && reduced_constant_expression_p (t))
9713 : : {
9714 : : /* Don't re-process a constant CONSTRUCTOR. */
9715 : 11595607 : verify_constructor_flags (t);
9716 : 11595607 : if (TREE_CONSTANT (t))
9717 : : return t;
9718 : : }
9719 : 1227123 : if (TREE_CLOBBER_P (t))
9720 : : {
9721 : : /* Assignment from a clobber is handled in cxx_eval_store_expression;
9722 : : a clobber by itself isn't a constant-expression. */
9723 : 0 : gcc_assert (ctx->quiet);
9724 : 0 : *non_constant_p = true;
9725 : 0 : break;
9726 : : }
9727 : 1227123 : r = cxx_eval_bare_aggregate (ctx, t, lval,
9728 : : non_constant_p, overflow_p, jump_target);
9729 : 1227123 : break;
9730 : :
9731 : 512 : case VEC_INIT_EXPR:
9732 : : /* We can get this in a defaulted constructor for a class with a
9733 : : non-static data member of array type. Either the initializer will
9734 : : be NULL, meaning default-initialization, or it will be an lvalue
9735 : : or xvalue of the same type, meaning direct-initialization from the
9736 : : corresponding member. */
9737 : 512 : r = cxx_eval_vec_init (ctx, t, lval,
9738 : : non_constant_p, overflow_p, jump_target);
9739 : 512 : break;
9740 : :
9741 : 16 : case VEC_PERM_EXPR:
9742 : 16 : r = cxx_eval_trinary_expression (ctx, t, lval,
9743 : : non_constant_p, overflow_p,
9744 : : jump_target);
9745 : 16 : break;
9746 : :
9747 : 4 : case PAREN_EXPR:
9748 : 4 : gcc_assert (!REF_PARENTHESIZED_P (t));
9749 : : /* A PAREN_EXPR resulting from __builtin_assoc_barrier has no effect in
9750 : : constant expressions since it's unaffected by -fassociative-math. */
9751 : 4 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
9752 : : non_constant_p, overflow_p,
9753 : : jump_target);
9754 : 4 : break;
9755 : :
9756 : 179101138 : case NOP_EXPR:
9757 : 179101138 : if (REINTERPRET_CAST_P (t))
9758 : : {
9759 : 73 : if (!ctx->quiet)
9760 : 6 : error_at (loc,
9761 : : "%<reinterpret_cast%> is not a constant expression");
9762 : 73 : *non_constant_p = true;
9763 : 73 : return t;
9764 : : }
9765 : : /* FALLTHROUGH. */
9766 : 212062204 : case CONVERT_EXPR:
9767 : 212062204 : case VIEW_CONVERT_EXPR:
9768 : 212062204 : case UNARY_PLUS_EXPR:
9769 : 212062204 : {
9770 : 212062204 : tree oldop = TREE_OPERAND (t, 0);
9771 : :
9772 : 212062204 : tree op = cxx_eval_constant_expression (ctx, oldop,
9773 : 212062204 : VOID_TYPE_P (TREE_TYPE (t))
9774 : : ? vc_discard
9775 : : : tcode == VIEW_CONVERT_EXPR
9776 : 200264353 : ? lval : vc_prvalue,
9777 : : non_constant_p, overflow_p,
9778 : : jump_target);
9779 : 212059506 : if (*jump_target)
9780 : : return NULL_TREE;
9781 : 212059171 : if (*non_constant_p)
9782 : : return t;
9783 : 160444587 : tree type = TREE_TYPE (t);
9784 : :
9785 : 160444587 : if (VOID_TYPE_P (type))
9786 : 11209009 : return void_node;
9787 : :
9788 : 149235578 : if (TREE_CODE (t) == CONVERT_EXPR
9789 : 12984834 : && ARITHMETIC_TYPE_P (type)
9790 : 2383766 : && INDIRECT_TYPE_P (TREE_TYPE (op))
9791 : 149258688 : && ctx->strict)
9792 : : {
9793 : 23020 : if (!ctx->quiet)
9794 : 54 : error_at (loc,
9795 : : "conversion from pointer type %qT to arithmetic type "
9796 : 54 : "%qT in a constant expression", TREE_TYPE (op), type);
9797 : 23020 : *non_constant_p = true;
9798 : 23020 : return t;
9799 : : }
9800 : :
9801 : : /* [expr.const]: a conversion from type cv void* to a pointer-to-object
9802 : : type cannot be part of a core constant expression as a resolution to
9803 : : DR 1312. */
9804 : 43149369 : if (TYPE_PTROB_P (type)
9805 : 41838413 : && TYPE_PTR_P (TREE_TYPE (op))
9806 : 31202272 : && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (op)))
9807 : : /* Inside a call to std::construct_at,
9808 : : std::allocator<T>::{,de}allocate, or
9809 : : std::source_location::current, we permit casting from void*
9810 : : because that is compiler-generated code. */
9811 : 156656 : && !is_std_construct_at (ctx->call)
9812 : 24075 : && !is_std_allocator_allocate (ctx->call)
9813 : 149232521 : && !is_std_source_location_current (ctx->call))
9814 : : {
9815 : : /* Likewise, don't error when casting from void* when OP is
9816 : : &heap uninit and similar. */
9817 : 19804 : tree sop = tree_strip_nop_conversions (op);
9818 : 19804 : tree decl = NULL_TREE;
9819 : 19804 : if (TREE_CODE (sop) == ADDR_EXPR)
9820 : 14753 : decl = TREE_OPERAND (sop, 0);
9821 : 14753 : if (decl
9822 : 14753 : && VAR_P (decl)
9823 : 14569 : && DECL_ARTIFICIAL (decl)
9824 : 29283 : && (DECL_NAME (decl) == heap_identifier
9825 : 6872 : || DECL_NAME (decl) == heap_uninit_identifier
9826 : 2188 : || DECL_NAME (decl) == heap_vec_identifier
9827 : 1174 : || DECL_NAME (decl) == heap_vec_uninit_identifier))
9828 : : /* OK */;
9829 : : /* P2738 (C++26): a conversion from a prvalue P of type "pointer to
9830 : : cv void" to a pointer-to-object type T unless P is a null
9831 : : pointer value or points to an object whose type is similar to
9832 : : T. */
9833 : 5290 : else if (cxx_dialect > cxx23)
9834 : : {
9835 : 5174 : if (integer_zerop (sop))
9836 : 31 : return build_int_cst (type, 0);
9837 : 5143 : r = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (type), sop,
9838 : : NULL, jump_target);
9839 : 5143 : if (*jump_target)
9840 : : return NULL_TREE;
9841 : 5143 : if (r)
9842 : : {
9843 : 5108 : r = build1 (ADDR_EXPR, type, r);
9844 : 5108 : break;
9845 : : }
9846 : 35 : if (!ctx->quiet)
9847 : : {
9848 : 3 : gcc_assert (TREE_CODE (sop) == ADDR_EXPR);
9849 : 3 : auto_diagnostic_group d;
9850 : 3 : error_at (loc, "cast from %qT is not allowed in a "
9851 : : "constant expression because "
9852 : : "pointed-to type %qT is not similar to %qT",
9853 : 3 : TREE_TYPE (op), TREE_TYPE (TREE_TYPE (sop)),
9854 : 3 : TREE_TYPE (type));
9855 : 3 : tree obj = build_fold_indirect_ref (sop);
9856 : 3 : if (TREE_CODE (obj) == COMPONENT_REF)
9857 : 2 : obj = TREE_OPERAND (obj, 1);
9858 : 3 : if (DECL_P (obj))
9859 : 3 : inform (DECL_SOURCE_LOCATION (obj),
9860 : : "pointed-to object declared here");
9861 : 3 : }
9862 : 35 : *non_constant_p = true;
9863 : 35 : return t;
9864 : : }
9865 : : else
9866 : : {
9867 : 116 : if (!ctx->quiet)
9868 : 26 : error_at (loc, "cast from %qT is not allowed in a "
9869 : : "constant expression before C++26",
9870 : 26 : TREE_TYPE (op));
9871 : 116 : *non_constant_p = true;
9872 : 116 : return t;
9873 : : }
9874 : : }
9875 : :
9876 : 149207268 : if (TREE_CODE (op) == PTRMEM_CST && !TYPE_PTRMEM_P (type))
9877 : : {
9878 : 1688 : op = cplus_expand_constant (op);
9879 : 1688 : if (TREE_CODE (op) == PTRMEM_CST)
9880 : : {
9881 : 21 : if (!ctx->quiet)
9882 : 3 : error_at (loc, "%qE is not a constant expression when the "
9883 : : "class %qT is still incomplete", op,
9884 : 3 : PTRMEM_CST_CLASS (op));
9885 : 21 : *non_constant_p = true;
9886 : 21 : return t;
9887 : : }
9888 : : }
9889 : :
9890 : 149207247 : if (TREE_CODE (op) == PTRMEM_CST && tcode == NOP_EXPR)
9891 : : {
9892 : 803 : if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (op))
9893 : 803 : && !can_convert_qual (type, op))
9894 : 6 : op = cplus_expand_constant (op);
9895 : 803 : return cp_fold_convert (type, op);
9896 : : }
9897 : :
9898 : 149206444 : if (INDIRECT_TYPE_P (type) && TREE_CODE (op) == INTEGER_CST)
9899 : : {
9900 : 342614 : if (integer_zerop (op))
9901 : : {
9902 : 283820 : if (TYPE_REF_P (type))
9903 : : {
9904 : 32 : if (!ctx->quiet)
9905 : 4 : error_at (loc, "dereferencing a null pointer");
9906 : 32 : *non_constant_p = true;
9907 : 32 : return t;
9908 : : }
9909 : : }
9910 : 58794 : else if (TYPE_PTR_P (type)
9911 : 58794 : && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
9912 : : /* INTEGER_CST with pointer-to-method type is only used
9913 : : for a virtual method in a pointer to member function.
9914 : : Don't reject those. */
9915 : : ;
9916 : : else
9917 : : {
9918 : : /* This detects for example:
9919 : : reinterpret_cast<void*>(sizeof 0)
9920 : : */
9921 : 58791 : if (!ctx->quiet)
9922 : 21 : error_at (loc, "%<reinterpret_cast<%T>(%E)%> is not "
9923 : : "a constant expression",
9924 : : type, op);
9925 : 58791 : *non_constant_p = true;
9926 : 58791 : return t;
9927 : : }
9928 : : }
9929 : :
9930 : 149147621 : if (INDIRECT_TYPE_P (type)
9931 : 66681900 : && TREE_CODE (op) == NOP_EXPR
9932 : 33182206 : && TREE_TYPE (op) == ptr_type_node
9933 : 78146 : && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR
9934 : 76712 : && VAR_P (TREE_OPERAND (TREE_OPERAND (op, 0), 0))
9935 : 149192021 : && (DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0),
9936 : 44400 : 0)) == heap_uninit_identifier
9937 : 35604 : || DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0),
9938 : 35604 : 0)) == heap_vec_uninit_identifier))
9939 : : {
9940 : 9954 : tree var = TREE_OPERAND (TREE_OPERAND (op, 0), 0);
9941 : 9954 : tree var_size = TYPE_SIZE_UNIT (TREE_TYPE (var));
9942 : 9954 : tree elt_type = TREE_TYPE (type);
9943 : 9954 : tree cookie_size = NULL_TREE;
9944 : 9954 : tree arg_size = NULL_TREE;
9945 : 9954 : if (TREE_CODE (elt_type) == RECORD_TYPE
9946 : 9954 : && TYPE_IDENTIFIER (elt_type) == heap_identifier)
9947 : : {
9948 : 9 : tree fld1 = TYPE_FIELDS (elt_type);
9949 : 9 : tree fld2 = DECL_CHAIN (fld1);
9950 : 9 : elt_type = TREE_TYPE (TREE_TYPE (fld2));
9951 : 9 : cookie_size = TYPE_SIZE_UNIT (TREE_TYPE (fld1));
9952 : : }
9953 : 9954 : DECL_NAME (var)
9954 : 9954 : = (DECL_NAME (var) == heap_uninit_identifier
9955 : 9954 : ? heap_identifier : heap_vec_identifier);
9956 : : /* For zero sized elt_type, try to recover how many outer_nelts
9957 : : it should have. */
9958 : 9954 : if ((cookie_size ? tree_int_cst_equal (var_size, cookie_size)
9959 : 9945 : : integer_zerop (var_size))
9960 : 40 : && !int_size_in_bytes (elt_type)
9961 : 9 : && TREE_CODE (oldop) == CALL_EXPR
9962 : 9972 : && call_expr_nargs (oldop) >= 1)
9963 : 9 : if (tree fun = get_function_named_in_call (oldop))
9964 : 9 : if (cxx_replaceable_global_alloc_fn (fun)
9965 : 9 : && IDENTIFIER_NEW_OP_P (DECL_NAME (fun)))
9966 : 9 : arg_size = CALL_EXPR_ARG (oldop, 0);
9967 : 9954 : tree new_type
9968 : 9954 : = build_new_constexpr_heap_type (ctx, elt_type, cookie_size,
9969 : : var_size, arg_size,
9970 : : non_constant_p, overflow_p,
9971 : : jump_target);
9972 : 9954 : if (*jump_target)
9973 : : return NULL_TREE;
9974 : 9954 : TREE_TYPE (var) = new_type;
9975 : 9954 : TREE_TYPE (TREE_OPERAND (op, 0))
9976 : 19908 : = build_pointer_type (TREE_TYPE (var));
9977 : : }
9978 : :
9979 : 149147621 : if (op == oldop && tcode != UNARY_PLUS_EXPR)
9980 : : /* We didn't fold at the top so we could check for ptr-int
9981 : : conversion. */
9982 : 12308789 : return fold (t);
9983 : :
9984 : 136838832 : tree sop;
9985 : :
9986 : : /* Handle an array's bounds having been deduced after we built
9987 : : the wrapping expression. */
9988 : 136838832 : if (same_type_ignoring_tlq_and_bounds_p (type, TREE_TYPE (op)))
9989 : : r = op;
9990 : 39441317 : else if (sop = tree_strip_nop_conversions (op),
9991 : 54396537 : sop != op && (same_type_ignoring_tlq_and_bounds_p
9992 : 14955220 : (type, TREE_TYPE (sop))))
9993 : : r = sop;
9994 : 32285514 : else if (tcode == UNARY_PLUS_EXPR)
9995 : 0 : r = fold_convert (TREE_TYPE (t), op);
9996 : : else
9997 : 32285514 : r = fold_build1 (tcode, type, op);
9998 : :
9999 : : /* Conversion of an out-of-range value has implementation-defined
10000 : : behavior; the language considers it different from arithmetic
10001 : : overflow, which is undefined. */
10002 : 136838832 : if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op))
10003 : 11955 : TREE_OVERFLOW (r) = false;
10004 : : }
10005 : : break;
10006 : :
10007 : 8240 : case EXCESS_PRECISION_EXPR:
10008 : 8240 : {
10009 : 8240 : tree oldop = TREE_OPERAND (t, 0);
10010 : :
10011 : 8240 : tree op = cxx_eval_constant_expression (ctx, oldop,
10012 : : lval,
10013 : : non_constant_p, overflow_p,
10014 : : jump_target);
10015 : 8240 : if (*jump_target)
10016 : : return NULL_TREE;
10017 : 8240 : if (*non_constant_p)
10018 : : return t;
10019 : 8236 : r = fold_convert (TREE_TYPE (t), op);
10020 : 8236 : break;
10021 : : }
10022 : :
10023 : 38950 : case EMPTY_CLASS_EXPR:
10024 : : /* Handle EMPTY_CLASS_EXPR produced by build_call_a by lowering
10025 : : it to an appropriate CONSTRUCTOR. */
10026 : 38950 : return build_constructor (TREE_TYPE (t), NULL);
10027 : :
10028 : 17223779 : case STATEMENT_LIST:
10029 : 17223779 : new_ctx = *ctx;
10030 : 17223779 : new_ctx.ctor = new_ctx.object = NULL_TREE;
10031 : 17223779 : return cxx_eval_statement_list (&new_ctx, t,
10032 : 17223777 : non_constant_p, overflow_p, jump_target);
10033 : :
10034 : 9093363 : case BIND_EXPR:
10035 : : /* Pre-emptively clear the vars declared by this BIND_EXPR from the value
10036 : : map, so that when checking whether they're already destroyed later we
10037 : : don't get confused by remnants of previous calls. */
10038 : 13018918 : for (tree decl = BIND_EXPR_VARS (t); decl; decl = DECL_CHAIN (decl))
10039 : 3925555 : ctx->global->clear_value (decl);
10040 : 9093363 : r = cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t),
10041 : : lval,
10042 : : non_constant_p, overflow_p,
10043 : : jump_target);
10044 : 13018916 : for (tree decl = BIND_EXPR_VARS (t); decl; decl = DECL_CHAIN (decl))
10045 : 3925554 : destroy_value_checked (ctx, decl, non_constant_p);
10046 : : break;
10047 : :
10048 : 2468967 : case PREINCREMENT_EXPR:
10049 : 2468967 : case POSTINCREMENT_EXPR:
10050 : 2468967 : case PREDECREMENT_EXPR:
10051 : 2468967 : case POSTDECREMENT_EXPR:
10052 : 2468967 : return cxx_eval_increment_expression (ctx, t,
10053 : : lval, non_constant_p, overflow_p,
10054 : 2468967 : jump_target);
10055 : :
10056 : 696 : case THROW_EXPR:
10057 : 696 : if (cxx_dialect >= cxx26)
10058 : 368 : return cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
10059 : : non_constant_p, overflow_p,
10060 : 368 : jump_target);
10061 : : /* FALLTHROUGH */
10062 : 336 : case LAMBDA_EXPR:
10063 : 336 : case NEW_EXPR:
10064 : 336 : case VEC_NEW_EXPR:
10065 : 336 : case DELETE_EXPR:
10066 : 336 : case VEC_DELETE_EXPR:
10067 : 336 : case MODOP_EXPR:
10068 : : /* GCC internal stuff. */
10069 : 336 : case VA_ARG_EXPR:
10070 : 336 : case BASELINK:
10071 : 336 : case OFFSET_REF:
10072 : 336 : if (!ctx->quiet)
10073 : 86 : error_at (loc, "expression %qE is not a constant expression", t);
10074 : 336 : *non_constant_p = true;
10075 : 336 : break;
10076 : :
10077 : 117231 : case OBJ_TYPE_REF:
10078 : : /* Virtual function lookup. We don't need to do anything fancy. */
10079 : 117231 : return cxx_eval_constant_expression (ctx, OBJ_TYPE_REF_EXPR (t),
10080 : : lval, non_constant_p, overflow_p,
10081 : 117231 : jump_target);
10082 : :
10083 : 16121 : case PLACEHOLDER_EXPR:
10084 : : /* Use of the value or address of the current object. */
10085 : 16121 : if (tree ctor = lookup_placeholder (ctx, lval, TREE_TYPE (t)))
10086 : : {
10087 : 15889 : if (TREE_CODE (ctor) == CONSTRUCTOR)
10088 : : return ctor;
10089 : : else
10090 : 15589 : return cxx_eval_constant_expression (ctx, ctor, lval,
10091 : : non_constant_p, overflow_p,
10092 : 15589 : jump_target);
10093 : : }
10094 : : /* A placeholder without a referent. We can get here when
10095 : : checking whether NSDMIs are noexcept, or in massage_init_elt;
10096 : : just say it's non-constant for now. */
10097 : 232 : gcc_assert (ctx->quiet);
10098 : 232 : *non_constant_p = true;
10099 : 232 : break;
10100 : :
10101 : 239 : case EXIT_EXPR:
10102 : 239 : {
10103 : 239 : tree cond = TREE_OPERAND (t, 0);
10104 : 239 : cond = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
10105 : : non_constant_p, overflow_p,
10106 : : jump_target);
10107 : 239 : if (*jump_target)
10108 : : return NULL_TREE;
10109 : 239 : VERIFY_CONSTANT (cond);
10110 : 239 : if (integer_nonzerop (cond))
10111 : 67 : *jump_target = t;
10112 : : }
10113 : : break;
10114 : :
10115 : 21 : case GOTO_EXPR:
10116 : 21 : if (breaks (&TREE_OPERAND (t, 0))
10117 : 21 : || continues (&TREE_OPERAND (t, 0)))
10118 : 9 : *jump_target = TREE_OPERAND (t, 0);
10119 : : else
10120 : : {
10121 : 12 : if (!ctx->quiet)
10122 : 1 : error_at (loc, "%<goto%> is not a constant expression");
10123 : 12 : *non_constant_p = true;
10124 : : }
10125 : : break;
10126 : :
10127 : 978338 : case LOOP_EXPR:
10128 : 978338 : case DO_STMT:
10129 : 978338 : case WHILE_STMT:
10130 : 978338 : case FOR_STMT:
10131 : 978338 : cxx_eval_loop_expr (ctx, t,
10132 : : non_constant_p, overflow_p, jump_target);
10133 : 978338 : break;
10134 : :
10135 : 37275 : case SWITCH_EXPR:
10136 : 37275 : case SWITCH_STMT:
10137 : 37275 : cxx_eval_switch_expr (ctx, t,
10138 : : non_constant_p, overflow_p, jump_target);
10139 : 37275 : break;
10140 : :
10141 : 118 : case REQUIRES_EXPR:
10142 : : /* It's possible to get a requires-expression in a constant
10143 : : expression. For example:
10144 : :
10145 : : template<typename T> concept bool C() {
10146 : : return requires (T t) { t; };
10147 : : }
10148 : :
10149 : : template<typename T> requires !C<T>() void f(T);
10150 : :
10151 : : Normalization leaves f with the associated constraint
10152 : : '!requires (T t) { ... }' which is not transformed into
10153 : : a constraint. */
10154 : 118 : if (!processing_template_decl)
10155 : 118 : return evaluate_requires_expr (t);
10156 : : else
10157 : 0 : *non_constant_p = true;
10158 : 0 : return t;
10159 : :
10160 : 8482 : case ANNOTATE_EXPR:
10161 : 8482 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
10162 : : lval,
10163 : : non_constant_p, overflow_p,
10164 : : jump_target);
10165 : 8482 : break;
10166 : :
10167 : 19817 : case USING_STMT:
10168 : 19817 : r = void_node;
10169 : 19817 : break;
10170 : :
10171 : 103 : case ASSERTION_STMT:
10172 : 103 : case PRECONDITION_STMT:
10173 : 103 : case POSTCONDITION_STMT:
10174 : 103 : {
10175 : 103 : if (contract_ignored_p (t))
10176 : : break;
10177 : :
10178 : 87 : if (!cxx_eval_assert (ctx, CONTRACT_CONDITION (t),
10179 : : G_("contract predicate is false in "
10180 : : "constant expression"),
10181 : 87 : EXPR_LOCATION (t), contract_evaluated_p (t),
10182 : : non_constant_p, overflow_p))
10183 : 38 : *non_constant_p = true;
10184 : 87 : r = void_node;
10185 : : }
10186 : 87 : break;
10187 : :
10188 : 1302807 : case TEMPLATE_ID_EXPR:
10189 : 1302807 : {
10190 : : /* We can evaluate template-id that refers to a concept only if
10191 : : the template arguments are non-dependent. */
10192 : 1302807 : gcc_assert (concept_check_p (t));
10193 : :
10194 : 1302807 : if (!value_dependent_expression_p (t)
10195 : 1302807 : && !uid_sensitive_constexpr_evaluation_p ())
10196 : 1302761 : r = evaluate_concept_check (t);
10197 : : else
10198 : 46 : *non_constant_p = true;
10199 : :
10200 : : break;
10201 : : }
10202 : :
10203 : 48 : case ASM_EXPR:
10204 : 48 : if (!ctx->quiet)
10205 : 28 : inline_asm_in_constexpr_error (loc, /*constexpr_fundef_p*/false);
10206 : 48 : *non_constant_p = true;
10207 : 48 : return t;
10208 : :
10209 : 1031 : case BIT_CAST_EXPR:
10210 : 1031 : if (lval)
10211 : : {
10212 : 0 : if (!ctx->quiet)
10213 : 0 : error_at (EXPR_LOCATION (t),
10214 : : "address of a call to %qs is not a constant expression",
10215 : : "__builtin_bit_cast");
10216 : 0 : *non_constant_p = true;
10217 : 0 : return t;
10218 : : }
10219 : 1031 : r = cxx_eval_bit_cast (ctx, t, non_constant_p, overflow_p, jump_target);
10220 : 1031 : break;
10221 : :
10222 : 0 : case OMP_PARALLEL:
10223 : 0 : case OMP_TASK:
10224 : 0 : case OMP_FOR:
10225 : 0 : case OMP_SIMD:
10226 : 0 : case OMP_DISTRIBUTE:
10227 : 0 : case OMP_TASKLOOP:
10228 : 0 : case OMP_LOOP:
10229 : 0 : case OMP_TEAMS:
10230 : 0 : case OMP_TARGET_DATA:
10231 : 0 : case OMP_TARGET:
10232 : 0 : case OMP_SECTIONS:
10233 : 0 : case OMP_ORDERED:
10234 : 0 : case OMP_CRITICAL:
10235 : 0 : case OMP_SINGLE:
10236 : 0 : case OMP_SCAN:
10237 : 0 : case OMP_SCOPE:
10238 : 0 : case OMP_SECTION:
10239 : 0 : case OMP_STRUCTURED_BLOCK:
10240 : 0 : case OMP_MASTER:
10241 : 0 : case OMP_MASKED:
10242 : 0 : case OMP_TASKGROUP:
10243 : 0 : case OMP_TARGET_UPDATE:
10244 : 0 : case OMP_TARGET_ENTER_DATA:
10245 : 0 : case OMP_TARGET_EXIT_DATA:
10246 : 0 : case OMP_ATOMIC:
10247 : 0 : case OMP_ATOMIC_READ:
10248 : 0 : case OMP_ATOMIC_CAPTURE_OLD:
10249 : 0 : case OMP_ATOMIC_CAPTURE_NEW:
10250 : 0 : case OMP_DEPOBJ:
10251 : 0 : case OACC_PARALLEL:
10252 : 0 : case OACC_KERNELS:
10253 : 0 : case OACC_SERIAL:
10254 : 0 : case OACC_DATA:
10255 : 0 : case OACC_HOST_DATA:
10256 : 0 : case OACC_LOOP:
10257 : 0 : case OACC_CACHE:
10258 : 0 : case OACC_DECLARE:
10259 : 0 : case OACC_ENTER_DATA:
10260 : 0 : case OACC_EXIT_DATA:
10261 : 0 : case OACC_UPDATE:
10262 : 0 : if (!ctx->quiet)
10263 : 0 : error_at (EXPR_LOCATION (t),
10264 : : "statement is not a constant expression");
10265 : 0 : *non_constant_p = true;
10266 : 0 : break;
10267 : :
10268 : 0 : default:
10269 : 0 : if (STATEMENT_CODE_P (TREE_CODE (t)))
10270 : : {
10271 : : /* This function doesn't know how to deal with pre-genericize
10272 : : statements; this can only happen with statement-expressions,
10273 : : so for now just fail. */
10274 : 0 : if (!ctx->quiet)
10275 : 0 : error_at (EXPR_LOCATION (t),
10276 : : "statement is not a constant expression");
10277 : : }
10278 : 0 : else if (flag_checking)
10279 : 0 : internal_error ("unexpected expression %qE of kind %s", t,
10280 : : get_tree_code_name (TREE_CODE (t)));
10281 : 0 : *non_constant_p = true;
10282 : 0 : break;
10283 : : }
10284 : :
10285 : 770698527 : if (r == error_mark_node)
10286 : 9742212 : *non_constant_p = true;
10287 : :
10288 : 39674945 : if (r == void_node && lval != vc_discard && !*jump_target
10289 : 770709158 : && !VOID_TYPE_P (TREE_TYPE (t)))
10290 : : {
10291 : : /* For diagnostic quality we should have handled this sooner, where we
10292 : : can be more specific about the out-of-lifetime object. But here we
10293 : : can still be correct. */
10294 : 1 : gcc_checking_assert (false);
10295 : : if (!ctx->quiet)
10296 : : error_at (EXPR_LOCATION (t),
10297 : : "%qE accesses an object outside its lifetime", t);
10298 : : *non_constant_p = true;
10299 : : }
10300 : :
10301 : 770698526 : if (*non_constant_p)
10302 : 239534951 : return t;
10303 : : else
10304 : : return r;
10305 : : }
10306 : :
10307 : : /* P0859: A function is needed for constant evaluation if it is a constexpr
10308 : : function that is named by an expression ([basic.def.odr]) that is
10309 : : potentially constant evaluated.
10310 : :
10311 : : So we need to instantiate any constexpr functions mentioned by the
10312 : : expression even if the definition isn't needed for evaluating the
10313 : : expression. */
10314 : :
10315 : : static tree
10316 : 403509709 : instantiate_cx_fn_r (tree *tp, int *walk_subtrees, void */*data*/)
10317 : : {
10318 : 403509709 : if (TREE_CODE (*tp) == FUNCTION_DECL
10319 : 10761135 : && DECL_DECLARED_CONSTEXPR_P (*tp)
10320 : 10657846 : && !DECL_INITIAL (*tp)
10321 : 1892858 : && !trivial_fn_p (*tp)
10322 : 1892855 : && (DECL_TEMPLOID_INSTANTIATION (*tp) || DECL_DEFAULTED_FN (*tp))
10323 : 403509709 : && !uid_sensitive_constexpr_evaluation_p ())
10324 : : {
10325 : 1892795 : ++function_depth;
10326 : 1892795 : if (DECL_TEMPLOID_INSTANTIATION (*tp))
10327 : 1892790 : instantiate_decl (*tp, /*defer_ok*/false, /*expl_inst*/false);
10328 : : else
10329 : 5 : synthesize_method (*tp);
10330 : 1892795 : --function_depth;
10331 : : }
10332 : 401616914 : else if (TREE_CODE (*tp) == CALL_EXPR
10333 : 390979078 : || TREE_CODE (*tp) == AGGR_INIT_EXPR)
10334 : : {
10335 : 10812557 : if (EXPR_HAS_LOCATION (*tp))
10336 : 10812418 : input_location = EXPR_LOCATION (*tp);
10337 : : }
10338 : :
10339 : 403509709 : if (!EXPR_P (*tp))
10340 : 237216517 : *walk_subtrees = 0;
10341 : :
10342 : 403509709 : return NULL_TREE;
10343 : : }
10344 : :
10345 : : static void
10346 : 203698649 : instantiate_constexpr_fns (tree t)
10347 : : {
10348 : 203698649 : location_t loc = input_location;
10349 : 203698649 : cp_walk_tree_without_duplicates (&t, instantiate_cx_fn_r, NULL);
10350 : 203698649 : input_location = loc;
10351 : 203698649 : }
10352 : :
10353 : : /* Look for heap variables in the expression *TP. */
10354 : :
10355 : : static tree
10356 : 153911 : find_heap_var_refs (tree *tp, int *walk_subtrees, void */*data*/)
10357 : : {
10358 : 153911 : if (VAR_P (*tp)
10359 : 153911 : && (DECL_NAME (*tp) == heap_uninit_identifier
10360 : 5210 : || DECL_NAME (*tp) == heap_identifier
10361 : 1878 : || DECL_NAME (*tp) == heap_vec_uninit_identifier
10362 : 1427 : || DECL_NAME (*tp) == heap_vec_identifier
10363 : 529 : || DECL_NAME (*tp) == heap_deleted_identifier))
10364 : : return *tp;
10365 : :
10366 : 110559 : if (TYPE_P (*tp))
10367 : 0 : *walk_subtrees = 0;
10368 : : return NULL_TREE;
10369 : : }
10370 : :
10371 : : /* Find immediate function decls in *TP if any. */
10372 : :
10373 : : static tree
10374 : 202798310 : find_immediate_fndecl (tree *tp, int */*walk_subtrees*/, void */*data*/)
10375 : : {
10376 : 204245702 : if (TREE_CODE (*tp) == FUNCTION_DECL && DECL_IMMEDIATE_FUNCTION_P (*tp))
10377 : : return *tp;
10378 : 202795409 : if (TREE_CODE (*tp) == PTRMEM_CST
10379 : 7944 : && TREE_CODE (PTRMEM_CST_MEMBER (*tp)) == FUNCTION_DECL
10380 : 202802794 : && DECL_IMMEDIATE_FUNCTION_P (PTRMEM_CST_MEMBER (*tp)))
10381 : : return PTRMEM_CST_MEMBER (*tp);
10382 : : return NULL_TREE;
10383 : : }
10384 : :
10385 : : /* T has TREE_CONSTANT set but has been deemed not a valid C++ constant
10386 : : expression. Return a version of T that has TREE_CONSTANT cleared. */
10387 : :
10388 : : static tree
10389 : 137359 : mark_non_constant (tree t)
10390 : : {
10391 : 137359 : gcc_checking_assert (TREE_CONSTANT (t));
10392 : :
10393 : : /* This isn't actually constant, so unset TREE_CONSTANT.
10394 : : Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires
10395 : : it to be set if it is invariant address, even when it is not
10396 : : a valid C++ constant expression. Wrap it with a NOP_EXPR
10397 : : instead. */
10398 : 137359 : if (EXPR_P (t) && TREE_CODE (t) != ADDR_EXPR)
10399 : 136539 : t = copy_node (t);
10400 : 820 : else if (TREE_CODE (t) == CONSTRUCTOR)
10401 : 236 : t = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (t), t);
10402 : : else
10403 : 584 : t = build_nop (TREE_TYPE (t), t);
10404 : 137359 : TREE_CONSTANT (t) = false;
10405 : 137359 : return t;
10406 : : }
10407 : :
10408 : : /* ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
10409 : : STRICT has the same sense as for constant_value_1: true if we only allow
10410 : : conforming C++ constant expressions, or false if we want a constant value
10411 : : even if it doesn't conform.
10412 : : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
10413 : : per P0595 even when ALLOW_NON_CONSTANT is true.
10414 : : CONSTEXPR_DTOR is true when evaluating the dtor of a constexpr variable.
10415 : : OBJECT must be non-NULL in that case. */
10416 : :
10417 : : static tree
10418 : 380948465 : cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant,
10419 : : bool strict = true,
10420 : : mce_value manifestly_const_eval = mce_unknown,
10421 : : bool constexpr_dtor = false,
10422 : : tree object = NULL_TREE)
10423 : : {
10424 : 380948465 : auto_timevar time (TV_CONSTEXPR);
10425 : :
10426 : 380948465 : bool non_constant_p = false;
10427 : 380948465 : bool overflow_p = false;
10428 : :
10429 : 380948465 : if (BRACE_ENCLOSED_INITIALIZER_P (t))
10430 : : {
10431 : 0 : gcc_checking_assert (allow_non_constant);
10432 : : return t;
10433 : : }
10434 : :
10435 : 380948465 : constexpr_global_ctx global_ctx;
10436 : 380948465 : constexpr_ctx ctx = { &global_ctx, NULL, NULL, NULL, NULL, NULL, NULL,
10437 : : allow_non_constant, strict,
10438 : 380948465 : !allow_non_constant ? mce_true : manifestly_const_eval };
10439 : :
10440 : : /* Turn off -frounding-math for manifestly constant evaluation. */
10441 : 380948465 : warning_sentinel rm (flag_rounding_math,
10442 : 380948465 : ctx.manifestly_const_eval == mce_true);
10443 : 380948465 : tree type = (object
10444 : 380948465 : ? cv_unqualified (TREE_TYPE (object))
10445 : 357233253 : : initialized_type (t));
10446 : 380948465 : tree r = t;
10447 : 380948465 : bool is_consteval = false;
10448 : 380948465 : if (VOID_TYPE_P (type))
10449 : : {
10450 : 3653751 : if (!constexpr_dtor)
10451 : : {
10452 : 3653751 : if (cxx_dialect < cxx20)
10453 : : return t;
10454 : : /* We could have a COMPOUND_EXPR here coming from
10455 : : keep_unused_object_arg. */
10456 : 3320357 : tree x = extract_call_expr (t);
10457 : 3320357 : if (x == NULL_TREE || x == error_mark_node)
10458 : : return t;
10459 : : /* Calls to immediate functions returning void need to be
10460 : : evaluated. */
10461 : 3320346 : tree fndecl = cp_get_callee_fndecl_nofold (x);
10462 : 6640692 : if (fndecl == NULL_TREE || !DECL_IMMEDIATE_FUNCTION_P (fndecl))
10463 : : return t;
10464 : : else
10465 : : is_consteval = true;
10466 : : }
10467 : : }
10468 : 377294714 : else if (cxx_dialect >= cxx20
10469 : 166264684 : && (TREE_CODE (t) == CALL_EXPR
10470 : 140905558 : || TREE_CODE (t) == AGGR_INIT_EXPR
10471 : 139404667 : || TREE_CODE (t) == TARGET_EXPR))
10472 : : {
10473 : 28590347 : tree x = t;
10474 : 28590347 : if (TREE_CODE (x) == TARGET_EXPR)
10475 : 1730330 : x = TARGET_EXPR_INITIAL (x);
10476 : 28590347 : tree fndecl = cp_get_callee_fndecl_nofold (x);
10477 : 56328221 : if (fndecl && DECL_IMMEDIATE_FUNCTION_P (fndecl))
10478 : : is_consteval = true;
10479 : : /* Don't try to evaluate a std::vector constructor taking an integer, it
10480 : : will fail in the 'if (heap_var)' block below after doing all the work
10481 : : (c++/113835). This will need adjustment if P3554 is accepted. Note
10482 : : that evaluation of e.g. the vector default constructor can succeed, so
10483 : : we don't shortcut all vector constructors. */
10484 : 55475748 : if (fndecl && DECL_CONSTRUCTOR_P (fndecl) && allow_non_constant
10485 : 4020728 : && is_std_class (type, "vector") && call_expr_nargs (x) > 1
10486 : 28595338 : && TREE_CODE (TREE_TYPE (get_nth_callarg (x, 1))) == INTEGER_TYPE)
10487 : : return t;
10488 : : }
10489 : 377294428 : if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
10490 : : {
10491 : : /* In C++14 an NSDMI can participate in aggregate initialization,
10492 : : and can refer to the address of the object being initialized, so
10493 : : we need to pass in the relevant VAR_DECL if we want to do the
10494 : : evaluation in a single pass. The evaluation will dynamically
10495 : : update ctx.values for the VAR_DECL. We use the same strategy
10496 : : for C++11 constexpr constructors that refer to the object being
10497 : : initialized. */
10498 : 24009450 : if (constexpr_dtor)
10499 : : {
10500 : 147 : gcc_assert (object && VAR_P (object));
10501 : 147 : gcc_assert (DECL_DECLARED_CONSTEXPR_P (object));
10502 : 147 : gcc_assert (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object));
10503 : 147 : if (error_operand_p (DECL_INITIAL (object)))
10504 : : return t;
10505 : 136 : ctx.ctor = unshare_expr (DECL_INITIAL (object));
10506 : 136 : TREE_READONLY (ctx.ctor) = false;
10507 : : /* Temporarily force decl_really_constant_value to return false
10508 : : for it, we want to use ctx.ctor for the current value instead. */
10509 : 136 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = false;
10510 : : }
10511 : : else
10512 : : {
10513 : 24009303 : ctx.ctor = build_constructor (type, NULL);
10514 : 24009303 : CONSTRUCTOR_NO_CLEARING (ctx.ctor) = true;
10515 : : }
10516 : 24009439 : if (!object)
10517 : : {
10518 : 13322833 : if (TREE_CODE (t) == CALL_EXPR)
10519 : : {
10520 : : /* If T is calling a constructor to initialize an object, reframe
10521 : : it as an AGGR_INIT_EXPR to avoid trying to modify an object
10522 : : from outside the constant evaluation, which will fail even if
10523 : : the value is actually constant (is_constant_evaluated3.C). */
10524 : 5175141 : tree fn = cp_get_callee_fndecl_nofold (t);
10525 : 10350274 : if (fn && DECL_CONSTRUCTOR_P (fn))
10526 : : {
10527 : 2371130 : object = CALL_EXPR_ARG (t, 0);
10528 : 2371130 : object = build_fold_indirect_ref (object);
10529 : 2371130 : r = build_aggr_init_expr (type, r);
10530 : : }
10531 : : }
10532 : 8147692 : else if (TREE_CODE (t) == TARGET_EXPR)
10533 : 3110716 : object = TARGET_EXPR_SLOT (t);
10534 : 5036976 : else if (TREE_CODE (t) == AGGR_INIT_EXPR)
10535 : 440995 : object = AGGR_INIT_EXPR_SLOT (t);
10536 : : }
10537 : 24009439 : ctx.object = object;
10538 : 24009439 : if (object)
10539 : 16609447 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
10540 : : (type, TREE_TYPE (object)));
10541 : 16609447 : if (object && DECL_P (object))
10542 : 14025931 : global_ctx.put_value (object, ctx.ctor);
10543 : 24009439 : if (TREE_CODE (r) == TARGET_EXPR)
10544 : : /* Avoid creating another CONSTRUCTOR when we expand the
10545 : : TARGET_EXPR. */
10546 : 3170947 : r = TARGET_EXPR_INITIAL (r);
10547 : : }
10548 : :
10549 : 377294417 : auto_vec<tree, 16> cleanups;
10550 : 377294417 : global_ctx.cleanups = &cleanups;
10551 : :
10552 : 377294417 : if (manifestly_const_eval == mce_true)
10553 : 203698649 : instantiate_constexpr_fns (r);
10554 : 377294417 : tree jmp_target = NULL_TREE;
10555 : 377294417 : r = cxx_eval_constant_expression (&ctx, r, vc_prvalue,
10556 : : &non_constant_p, &overflow_p,
10557 : : &jmp_target);
10558 : 377292001 : if (throws (&jmp_target) && !non_constant_p)
10559 : : {
10560 : 282 : if (!ctx.quiet)
10561 : 76 : diagnose_uncaught_exception (input_location, &ctx, jmp_target);
10562 : 282 : non_constant_p = true;
10563 : 282 : jmp_target = NULL_TREE;
10564 : 282 : r = t;
10565 : : }
10566 : 377291437 : else if (!non_constant_p && jmp_target)
10567 : : {
10568 : 24 : non_constant_p = true;
10569 : 24 : if (!ctx.quiet)
10570 : : {
10571 : 3 : if (breaks (&jmp_target))
10572 : 3 : error ("%<break%> outside of a loop or %<switch%>");
10573 : 0 : else if (continues (&jmp_target))
10574 : 0 : error ("%<continue%> outside of a loop");
10575 : 0 : else if (returns (&jmp_target))
10576 : 0 : error ("%<return%> in a statement expression");
10577 : : else
10578 : 0 : gcc_unreachable ();
10579 : : }
10580 : 24 : r = t;
10581 : : }
10582 : :
10583 : : /* If we got a non-simple TARGET_EXPR, the initializer was a sequence
10584 : : of statements, and the result ought to be stored in ctx.ctor. */
10585 : 377291719 : if (r == void_node && !constexpr_dtor && ctx.ctor)
10586 : 0 : r = ctx.ctor;
10587 : :
10588 : 377291719 : unsigned int i;
10589 : 377291719 : tree cleanup;
10590 : 377291719 : jmp_target = NULL_TREE;
10591 : : /* Evaluate the cleanups. */
10592 : 754604316 : FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup)
10593 : 20878 : if (cleanup == NULL_TREE)
10594 : : /* NULL_TREE cleanup is a marker that before it is
10595 : : CLEANUP_EH_ONLY cleanup. Skip the cleanup before it. */
10596 : 23 : --i;
10597 : : else
10598 : 20855 : cxx_eval_constant_expression (&ctx, cleanup, vc_discard,
10599 : : &non_constant_p, &overflow_p,
10600 : : &jmp_target);
10601 : 377291719 : if (throws (&jmp_target) && !non_constant_p)
10602 : : {
10603 : 0 : if (!ctx.quiet)
10604 : 0 : diagnose_uncaught_exception (input_location, &ctx, jmp_target);
10605 : 0 : non_constant_p = true;
10606 : 0 : r = t;
10607 : : }
10608 : :
10609 : : /* Mutable logic is a bit tricky: we want to allow initialization of
10610 : : constexpr variables with mutable members, but we can't copy those
10611 : : members to another constexpr variable. */
10612 : 377291719 : if (!non_constant_p
10613 : 285110601 : && TREE_CODE (r) == CONSTRUCTOR
10614 : 387245854 : && CONSTRUCTOR_MUTABLE_POISON (r))
10615 : : {
10616 : 93 : if (!allow_non_constant)
10617 : 6 : error ("%qE is not a constant expression because it refers to "
10618 : : "mutable subobjects of %qT", t, type);
10619 : 93 : non_constant_p = true;
10620 : : }
10621 : :
10622 : 285110508 : if (!non_constant_p && cxx_dialect >= cxx20
10623 : 662402227 : && !global_ctx.heap_vars.is_empty ())
10624 : : {
10625 : 43888 : tree heap_var = cp_walk_tree_without_duplicates (&r, find_heap_var_refs,
10626 : : NULL);
10627 : 43888 : unsigned int i;
10628 : 43888 : if (heap_var)
10629 : : {
10630 : 43352 : if (!allow_non_constant && !non_constant_p)
10631 : : {
10632 : 11 : if (DECL_LANG_SPECIFIC (heap_var))
10633 : 2 : error ("%qE is not a constant expression because it refers to "
10634 : : "exception object allocated with "
10635 : : "%<__cxa_allocate_exception%>", t);
10636 : : else
10637 : 9 : error ("%qE is not a constant expression because it refers to "
10638 : : "a result of %<operator new%>", t);
10639 : 11 : inform (DECL_SOURCE_LOCATION (heap_var), "allocated here");
10640 : : }
10641 : 43352 : r = t;
10642 : 43352 : non_constant_p = true;
10643 : : }
10644 : 91960 : FOR_EACH_VEC_ELT (global_ctx.heap_vars, i, heap_var)
10645 : : {
10646 : 48072 : if (DECL_NAME (heap_var) != heap_deleted_identifier)
10647 : : {
10648 : 43411 : if (!allow_non_constant && !non_constant_p)
10649 : : {
10650 : 16 : error ("%qE is not a constant expression because allocated "
10651 : : "storage has not been deallocated", t);
10652 : 16 : inform (DECL_SOURCE_LOCATION (heap_var), "allocated here");
10653 : : }
10654 : 43411 : r = t;
10655 : 43411 : non_constant_p = true;
10656 : : }
10657 : : }
10658 : : }
10659 : :
10660 : : /* Check that immediate invocation does not return an expression referencing
10661 : : any immediate function decls. */
10662 : 377291719 : if (!non_constant_p && cxx_dialect >= cxx20)
10663 : 244288596 : if (tree immediate_fndecl
10664 : 122144298 : = cp_walk_tree_without_duplicates (&r, find_immediate_fndecl,
10665 : : NULL))
10666 : : {
10667 : 3006 : if (!allow_non_constant && !non_constant_p)
10668 : : {
10669 : 72 : if (is_consteval)
10670 : 36 : error_at (cp_expr_loc_or_input_loc (t),
10671 : : "immediate evaluation returns address of immediate "
10672 : : "function %qD", immediate_fndecl);
10673 : : else
10674 : 54 : error_at (cp_expr_loc_or_input_loc (t),
10675 : : "constant evaluation returns address of immediate "
10676 : : "function %qD", immediate_fndecl);
10677 : : }
10678 : 3006 : r = t;
10679 : 3006 : non_constant_p = true;
10680 : : }
10681 : :
10682 : 377291719 : if (!non_constant_p && !constexpr_dtor)
10683 : 285063955 : verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p);
10684 : :
10685 : : /* After verify_constant because reduced_constant_expression_p can unset
10686 : : CONSTRUCTOR_NO_CLEARING. */
10687 : 377291719 : if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_NO_CLEARING (r))
10688 : : {
10689 : 78514 : if (!allow_non_constant)
10690 : 40 : error ("%qE is not a constant expression because it refers to "
10691 : : "an incompletely initialized variable", t);
10692 : 78514 : TREE_CONSTANT (r) = false;
10693 : 78514 : non_constant_p = true;
10694 : : }
10695 : :
10696 : 377291719 : if (non_constant_p)
10697 : : /* If we saw something bad, go back to our argument. The wrapping below is
10698 : : only for the cases of TREE_CONSTANT argument or overflow. */
10699 : 94288498 : r = t;
10700 : :
10701 : 377291719 : if (!non_constant_p && overflow_p)
10702 : 215 : non_constant_p = true;
10703 : :
10704 : : /* Unshare the result. */
10705 : 377291719 : bool should_unshare = true;
10706 : 377291719 : if (r == t || (TREE_CODE (t) == TARGET_EXPR
10707 : 990914 : && TARGET_EXPR_INITIAL (t) == r))
10708 : : should_unshare = false;
10709 : :
10710 : 377291719 : if (non_constant_p && !allow_non_constant)
10711 : 2640 : return error_mark_node;
10712 : 377289079 : else if (non_constant_p && TREE_CONSTANT (r))
10713 : 133034 : r = mark_non_constant (r);
10714 : 377156045 : else if (non_constant_p)
10715 : : return t;
10716 : :
10717 : 283136040 : if (constexpr_dtor)
10718 : : {
10719 : 118 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = true;
10720 : 118 : return r;
10721 : : }
10722 : :
10723 : : /* Check we are not trying to return the wrong type. */
10724 : 283135922 : if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (r)))
10725 : : {
10726 : : /* If so, this is not a constant expression. */
10727 : 134 : if (!allow_non_constant)
10728 : 0 : error ("%qE is not a constant expression because it initializes "
10729 : 0 : "a %qT rather than %qT", t, TREE_TYPE (t), type);
10730 : 134 : return t;
10731 : : }
10732 : :
10733 : 283135788 : if (should_unshare)
10734 : 146800935 : r = unshare_expr (r);
10735 : :
10736 : 283135788 : if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
10737 : : {
10738 : 9278943 : r = adjust_temp_type (type, r);
10739 : 9278943 : if (TREE_CODE (t) == TARGET_EXPR
10740 : 9278943 : && TARGET_EXPR_INITIAL (t) == r)
10741 : : return t;
10742 : 8511394 : else if (TREE_CODE (t) == CONSTRUCTOR || TREE_CODE (t) == CALL_EXPR
10743 : 1135365 : || TREE_CODE (t) == AGGR_INIT_EXPR)
10744 : : /* Don't add a TARGET_EXPR if our argument didn't have one. */;
10745 : 243423 : else if (TREE_CODE (t) == TARGET_EXPR && TARGET_EXPR_CLEANUP (t))
10746 : 1277 : r = get_target_expr (r);
10747 : : else
10748 : : {
10749 : 242146 : r = get_target_expr (r, tf_warning_or_error | tf_no_cleanup);
10750 : 242146 : TREE_CONSTANT (r) = true;
10751 : : }
10752 : : }
10753 : :
10754 : 282368239 : if (TREE_CODE (t) == TARGET_EXPR
10755 : 223365 : && TREE_CODE (r) == TARGET_EXPR)
10756 : : {
10757 : : /* Preserve this flag for potential_constant_expression, and the others
10758 : : for good measure. */
10759 : 223271 : TARGET_EXPR_ELIDING_P (r) = TARGET_EXPR_ELIDING_P (t);
10760 : 223271 : TARGET_EXPR_IMPLICIT_P (r) = TARGET_EXPR_IMPLICIT_P (t);
10761 : 223271 : TARGET_EXPR_LIST_INIT_P (r) = TARGET_EXPR_LIST_INIT_P (t);
10762 : 223271 : TARGET_EXPR_DIRECT_INIT_P (r) = TARGET_EXPR_DIRECT_INIT_P (t);
10763 : : }
10764 : :
10765 : : /* Remember the original location if that wouldn't need a wrapper. */
10766 : 282368239 : if (location_t loc = EXPR_LOCATION (t))
10767 : 115941225 : protected_set_expr_location (r, loc);
10768 : :
10769 : 282368239 : return r;
10770 : 380945767 : }
10771 : :
10772 : : /* If T represents a constant expression returns its reduced value.
10773 : : Otherwise return error_mark_node. */
10774 : :
10775 : : tree
10776 : 124351004 : cxx_constant_value (tree t, tree decl /* = NULL_TREE */,
10777 : : tsubst_flags_t complain /* = tf_error */)
10778 : : {
10779 : 124351004 : bool sfinae = !(complain & tf_error);
10780 : 124351004 : tree r = cxx_eval_outermost_constant_expr (t, sfinae, true, mce_true, false, decl);
10781 : 124351004 : if (sfinae && !TREE_CONSTANT (r))
10782 : 884 : r = error_mark_node;
10783 : 124351004 : return r;
10784 : : }
10785 : :
10786 : : /* Like cxx_constant_value, but used for evaluation of constexpr destructors
10787 : : of constexpr variables. The actual initializer of DECL is not modified. */
10788 : :
10789 : : void
10790 : 147 : cxx_constant_dtor (tree t, tree decl)
10791 : : {
10792 : 147 : cxx_eval_outermost_constant_expr (t, false, true, mce_true, true, decl);
10793 : 147 : }
10794 : :
10795 : : /* Helper routine for fold_simple function. Either return simplified
10796 : : expression T, otherwise NULL_TREE.
10797 : : In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold
10798 : : even if we are within template-declaration. So be careful on call, as in
10799 : : such case types can be undefined. */
10800 : :
10801 : : static tree
10802 : 108804423 : fold_simple_1 (tree t)
10803 : : {
10804 : 108804423 : tree op1;
10805 : 108804423 : enum tree_code code = TREE_CODE (t);
10806 : :
10807 : 108804423 : switch (code)
10808 : : {
10809 : : case INTEGER_CST:
10810 : : case REAL_CST:
10811 : : case VECTOR_CST:
10812 : : case FIXED_CST:
10813 : : case COMPLEX_CST:
10814 : : return t;
10815 : :
10816 : 967400 : case SIZEOF_EXPR:
10817 : 967400 : return fold_sizeof_expr (t);
10818 : :
10819 : 30751446 : case ABS_EXPR:
10820 : 30751446 : case ABSU_EXPR:
10821 : 30751446 : case CONJ_EXPR:
10822 : 30751446 : case REALPART_EXPR:
10823 : 30751446 : case IMAGPART_EXPR:
10824 : 30751446 : case NEGATE_EXPR:
10825 : 30751446 : case BIT_NOT_EXPR:
10826 : 30751446 : case TRUTH_NOT_EXPR:
10827 : 30751446 : case VIEW_CONVERT_EXPR:
10828 : 30751446 : CASE_CONVERT:
10829 : 30751446 : case FLOAT_EXPR:
10830 : 30751446 : case FIX_TRUNC_EXPR:
10831 : 30751446 : case FIXED_CONVERT_EXPR:
10832 : 30751446 : case ADDR_SPACE_CONVERT_EXPR:
10833 : :
10834 : 30751446 : op1 = TREE_OPERAND (t, 0);
10835 : :
10836 : 30751446 : t = const_unop (code, TREE_TYPE (t), op1);
10837 : 30751446 : if (!t)
10838 : : return NULL_TREE;
10839 : :
10840 : 2064888 : if (CONVERT_EXPR_CODE_P (code)
10841 : 2064888 : && TREE_OVERFLOW_P (t) && !TREE_OVERFLOW_P (op1))
10842 : 0 : TREE_OVERFLOW (t) = false;
10843 : : return t;
10844 : :
10845 : : default:
10846 : : return NULL_TREE;
10847 : : }
10848 : : }
10849 : :
10850 : : /* If T is a simple constant expression, returns its simplified value.
10851 : : Otherwise returns T. In contrast to maybe_constant_value we
10852 : : simplify only few operations on constant-expressions, and we don't
10853 : : try to simplify constexpressions. */
10854 : :
10855 : : tree
10856 : 109325639 : fold_simple (tree t)
10857 : : {
10858 : 109325639 : if (processing_template_decl)
10859 : : return t;
10860 : :
10861 : 108804423 : tree r = fold_simple_1 (t);
10862 : 108804423 : if (r)
10863 : : return r;
10864 : :
10865 : : return t;
10866 : : }
10867 : :
10868 : : /* Try folding the expression T to a simple constant.
10869 : : Returns that constant, otherwise returns T. */
10870 : :
10871 : : tree
10872 : 1004728 : fold_to_constant (tree t)
10873 : : {
10874 : 1004728 : if (processing_template_decl)
10875 : : return t;
10876 : :
10877 : 907748 : tree r = fold (t);
10878 : 907748 : if (CONSTANT_CLASS_P (r) && !TREE_OVERFLOW (r))
10879 : : return r;
10880 : : else
10881 : : return t;
10882 : : }
10883 : :
10884 : : /* If T is a constant expression, returns its reduced value.
10885 : : Otherwise, if T does not have TREE_CONSTANT set, returns T.
10886 : : Otherwise, returns a version of T without TREE_CONSTANT.
10887 : : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated
10888 : : as per P0595. */
10889 : :
10890 : : static GTY((deletable)) hash_map<tree, tree> *cv_cache;
10891 : :
10892 : : tree
10893 : 554021531 : maybe_constant_value (tree t, tree decl /* = NULL_TREE */,
10894 : : mce_value manifestly_const_eval /* = mce_unknown */)
10895 : : {
10896 : 554021531 : tree orig_t = t;
10897 : 554021531 : tree r;
10898 : :
10899 : 554021531 : if (EXPR_P (t) && manifestly_const_eval == mce_unknown)
10900 : : {
10901 : : /* Look up each operand in the cv_cache first to see if we've already
10902 : : reduced it, and reuse that result to avoid quadratic behavior if
10903 : : we're called when building up a large expression. */
10904 : 199072768 : int n = cp_tree_operand_length (t);
10905 : 199072768 : tree *ops = XALLOCAVEC (tree, n);
10906 : 199072768 : bool rebuild = false;
10907 : 566127564 : for (int i = 0; i < n; ++i)
10908 : : {
10909 : 367054796 : ops[i] = TREE_OPERAND (t, i);
10910 : 1100342559 : if (tree *cached = hash_map_safe_get (cv_cache, ops[i]))
10911 : 26779001 : if (*cached != ops[i])
10912 : : {
10913 : 7801798 : ops[i] = *cached;
10914 : 7801798 : rebuild = true;
10915 : : }
10916 : : }
10917 : 199072768 : if (rebuild)
10918 : : {
10919 : 6764558 : t = copy_node (t);
10920 : 22032118 : for (int i = 0; i < n; ++i)
10921 : 15267560 : TREE_OPERAND (t, i) = ops[i];
10922 : : }
10923 : : }
10924 : :
10925 : 554021531 : if (!is_nondependent_constant_expression (t))
10926 : : {
10927 : 0 : if (TREE_OVERFLOW_P (t)
10928 : 118609949 : || (!processing_template_decl && TREE_CONSTANT (t)))
10929 : 4325 : t = mark_non_constant (t);
10930 : 118609949 : return t;
10931 : : }
10932 : 435411582 : else if (CONSTANT_CLASS_P (t))
10933 : : /* No caching or evaluation needed. */
10934 : : return t;
10935 : :
10936 : : /* Don't constant evaluate an unevaluated non-manifestly-constant operand,
10937 : : but at least try folding it to a simple constant. */
10938 : 224235128 : if (cp_unevaluated_operand && manifestly_const_eval != mce_true)
10939 : 451185 : return fold_to_constant (t);
10940 : :
10941 : 218117241 : if (manifestly_const_eval != mce_unknown)
10942 : : /* TODO: Extend the cache to be mce_value aware. And if we have a
10943 : : previously cached mce_unknown result that's TREE_CONSTANT, it means
10944 : : the reduced value is independent of mce_value and so we should
10945 : : be able to reuse it in the mce_true/false case. */
10946 : 104106969 : return cxx_eval_outermost_constant_expr (t, true, true,
10947 : 104104271 : manifestly_const_eval, false, decl);
10948 : :
10949 : 119676974 : if (cv_cache == NULL)
10950 : 139239 : cv_cache = hash_map<tree, tree>::create_ggc (101);
10951 : 119676974 : if (tree *cached = cv_cache->get (t))
10952 : : {
10953 : 9302252 : r = *cached;
10954 : 9302252 : if (r != t)
10955 : : {
10956 : : /* Clear processing_template_decl for sake of break_out_target_exprs;
10957 : : entries in the cv_cache are non-templated. */
10958 : 2753280 : processing_template_decl_sentinel ptds;
10959 : :
10960 : 2753280 : r = break_out_target_exprs (r, /*clear_loc*/true);
10961 : 2753280 : protected_set_expr_location (r, EXPR_LOCATION (t));
10962 : 2753280 : }
10963 : 9302252 : return r;
10964 : : }
10965 : :
10966 : 110374722 : uid_sensitive_constexpr_evaluation_checker c;
10967 : 110374722 : r = cxx_eval_outermost_constant_expr (t, true, true,
10968 : : manifestly_const_eval, false, decl);
10969 : 110374722 : gcc_checking_assert (r == t
10970 : : || CONVERT_EXPR_P (t)
10971 : : || TREE_CODE (t) == VIEW_CONVERT_EXPR
10972 : : || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
10973 : : || !cp_tree_equal (r, t));
10974 : 110374722 : if (!c.evaluation_restricted_p ())
10975 : 110199930 : cv_cache->put (orig_t, r);
10976 : : return r;
10977 : : }
10978 : :
10979 : : /* Dispose of the whole CV_CACHE. */
10980 : :
10981 : : static void
10982 : 26360377 : clear_cv_cache (void)
10983 : : {
10984 : 26360377 : if (cv_cache != NULL)
10985 : 26136091 : cv_cache->empty ();
10986 : 26360377 : }
10987 : :
10988 : : /* Dispose of the whole CV_CACHE and FOLD_CACHE. */
10989 : :
10990 : : void
10991 : 26360377 : clear_cv_and_fold_caches ()
10992 : : {
10993 : 26360377 : clear_cv_cache ();
10994 : 26360377 : clear_fold_cache ();
10995 : 26360377 : }
10996 : :
10997 : : /* Internal function handling expressions in templates for
10998 : : fold_non_dependent_expr and fold_non_dependent_init.
10999 : :
11000 : : If we're in a template, but T isn't value dependent, simplify
11001 : : it. We're supposed to treat:
11002 : :
11003 : : template <typename T> void f(T[1 + 1]);
11004 : : template <typename T> void f(T[2]);
11005 : :
11006 : : as two declarations of the same function, for example. */
11007 : :
11008 : : static tree
11009 : 25628074 : fold_non_dependent_expr_template (tree t, tsubst_flags_t complain,
11010 : : bool manifestly_const_eval,
11011 : : tree object)
11012 : : {
11013 : 25628074 : gcc_assert (processing_template_decl);
11014 : :
11015 : 25628074 : if (is_nondependent_constant_expression (t))
11016 : : {
11017 : 14016084 : processing_template_decl_sentinel s;
11018 : 14016084 : t = instantiate_non_dependent_expr_internal (t, complain);
11019 : :
11020 : 14016084 : if (type_unknown_p (t) || BRACE_ENCLOSED_INITIALIZER_P (t))
11021 : : {
11022 : 0 : if (TREE_OVERFLOW_P (t))
11023 : : {
11024 : 0 : t = build_nop (TREE_TYPE (t), t);
11025 : 0 : TREE_CONSTANT (t) = false;
11026 : : }
11027 : 0 : return t;
11028 : : }
11029 : 14016084 : else if (CONSTANT_CLASS_P (t))
11030 : : /* No evaluation needed. */
11031 : : return t;
11032 : :
11033 : : /* Don't constant evaluate an unevaluated non-manifestly-constant operand,
11034 : : but at least try folding it to a simple constant. */
11035 : 3638655 : if (cp_unevaluated_operand && !manifestly_const_eval)
11036 : 89 : return fold_to_constant (t);
11037 : :
11038 : 3638566 : tree r = cxx_eval_outermost_constant_expr (t, true, true,
11039 : : mce_value (manifestly_const_eval),
11040 : : false, object);
11041 : : /* cp_tree_equal looks through NOPs, so allow them. */
11042 : 3638566 : gcc_checking_assert (r == t
11043 : : || CONVERT_EXPR_P (t)
11044 : : || TREE_CODE (t) == VIEW_CONVERT_EXPR
11045 : : || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
11046 : : || !cp_tree_equal (r, t));
11047 : 3638566 : return r;
11048 : 14016084 : }
11049 : 11611990 : else if (TREE_OVERFLOW_P (t))
11050 : : {
11051 : 0 : t = build_nop (TREE_TYPE (t), t);
11052 : 0 : TREE_CONSTANT (t) = false;
11053 : : }
11054 : :
11055 : : return t;
11056 : : }
11057 : :
11058 : : /* Like maybe_constant_value but first fully instantiate the argument.
11059 : :
11060 : : Note: this is equivalent to instantiate_non_dependent_expr (t, complain)
11061 : : followed by maybe_constant_value but is more efficient,
11062 : : because it calls instantiation_dependent_expression_p and
11063 : : potential_constant_expression at most once.
11064 : : The manifestly_const_eval argument is passed to maybe_constant_value.
11065 : :
11066 : : Callers should generally pass their active complain, or if they are in a
11067 : : non-template, diagnosing context, they can use the default of
11068 : : tf_warning_or_error. Callers that might be within a template context, don't
11069 : : have a complain parameter, and aren't going to remember the result for long
11070 : : (e.g. null_ptr_cst_p), can pass tf_none and deal with error_mark_node
11071 : : appropriately. */
11072 : :
11073 : : tree
11074 : 124314849 : fold_non_dependent_expr (tree t,
11075 : : tsubst_flags_t complain /* = tf_warning_or_error */,
11076 : : bool manifestly_const_eval /* = false */,
11077 : : tree object /* = NULL_TREE */)
11078 : : {
11079 : 124314849 : if (t == NULL_TREE)
11080 : : return NULL_TREE;
11081 : :
11082 : 123790526 : if (processing_template_decl)
11083 : 24632097 : return fold_non_dependent_expr_template (t, complain,
11084 : 24632097 : manifestly_const_eval, object);
11085 : :
11086 : 99158429 : return maybe_constant_value (t, object, mce_value (manifestly_const_eval));
11087 : : }
11088 : :
11089 : : /* Like fold_non_dependent_expr, but if EXPR couldn't be folded to a constant,
11090 : : return the original expression. */
11091 : :
11092 : : tree
11093 : 2029331 : maybe_fold_non_dependent_expr (tree expr,
11094 : : tsubst_flags_t complain/*=tf_warning_or_error*/)
11095 : : {
11096 : 2029331 : tree t = fold_non_dependent_expr (expr, complain);
11097 : 2029331 : if (t && TREE_CONSTANT (t))
11098 : 1011267 : return t;
11099 : :
11100 : : return expr;
11101 : : }
11102 : :
11103 : : /* Like maybe_constant_init but first fully instantiate the argument. */
11104 : :
11105 : : tree
11106 : 27847127 : fold_non_dependent_init (tree t,
11107 : : tsubst_flags_t complain /*=tf_warning_or_error*/,
11108 : : bool manifestly_const_eval /*=false*/,
11109 : : tree object /* = NULL_TREE */)
11110 : : {
11111 : 27847127 : if (t == NULL_TREE)
11112 : : return NULL_TREE;
11113 : :
11114 : 27847127 : if (processing_template_decl)
11115 : : {
11116 : 995977 : t = fold_non_dependent_expr_template (t, complain,
11117 : : manifestly_const_eval, object);
11118 : : /* maybe_constant_init does this stripping, so do it here too. */
11119 : 995977 : if (TREE_CODE (t) == TARGET_EXPR)
11120 : : {
11121 : 61 : tree init = TARGET_EXPR_INITIAL (t);
11122 : 61 : if (TREE_CODE (init) == CONSTRUCTOR)
11123 : 995977 : t = init;
11124 : : }
11125 : 995977 : return t;
11126 : : }
11127 : :
11128 : 26851150 : return maybe_constant_init (t, object, manifestly_const_eval);
11129 : : }
11130 : :
11131 : : /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
11132 : : than wrapped in a TARGET_EXPR.
11133 : : ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
11134 : : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
11135 : : per P0595 even when ALLOW_NON_CONSTANT is true. */
11136 : :
11137 : : static tree
11138 : 64048074 : maybe_constant_init_1 (tree t, tree decl, bool allow_non_constant,
11139 : : mce_value manifestly_const_eval)
11140 : : {
11141 : 64048074 : if (!t)
11142 : : return t;
11143 : 64048074 : if (TREE_CODE (t) == EXPR_STMT)
11144 : 23855 : t = TREE_OPERAND (t, 0);
11145 : 64048074 : if (TREE_CODE (t) == CONVERT_EXPR
11146 : 64048074 : && VOID_TYPE_P (TREE_TYPE (t)))
11147 : 34488 : t = TREE_OPERAND (t, 0);
11148 : : /* If the types don't match, the INIT_EXPR is initializing a subobject of
11149 : : DECL and losing that information would cause mischief later. */
11150 : 64048074 : if (TREE_CODE (t) == INIT_EXPR
11151 : 64048074 : && (!decl
11152 : 10644 : || same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (decl),
11153 : 10644 : TREE_TYPE (t))))
11154 : 24814 : t = TREE_OPERAND (t, 1);
11155 : 64048074 : if (TREE_CODE (t) == TARGET_EXPR)
11156 : 434482 : t = TARGET_EXPR_INITIAL (t);
11157 : 64048074 : if (!is_nondependent_static_init_expression (t))
11158 : : /* Don't try to evaluate it. */;
11159 : 55404816 : else if (CONSTANT_CLASS_P (t) && TREE_CODE (t) != PTRMEM_CST)
11160 : : /* No evaluation needed. PTRMEM_CST needs the immediate fn check. */;
11161 : : else
11162 : : {
11163 : : /* [basic.start.static] allows constant-initialization of variables with
11164 : : static or thread storage duration even if it isn't required, but we
11165 : : shouldn't bend the rules the same way for automatic variables.
11166 : :
11167 : : But still enforce the requirements of constexpr/constinit.
11168 : : [dcl.constinit] "If a variable declared with the constinit specifier
11169 : : has dynamic initialization, the program is ill-formed, even if the
11170 : : implementation would perform that initialization as a static
11171 : : initialization." */
11172 : 11455017 : bool is_static = (decl && DECL_P (decl)
11173 : 37438918 : && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)));
11174 : 1697024 : bool strict = (!is_static
11175 : : || (decl && DECL_P (decl)
11176 : 1697024 : && (DECL_DECLARED_CONSTEXPR_P (decl)
11177 : 901371 : || DECL_DECLARED_CONSTINIT_P (decl))));
11178 : : if (is_static)
11179 : : manifestly_const_eval = mce_true;
11180 : :
11181 : 26409390 : if (cp_unevaluated_operand && manifestly_const_eval != mce_true)
11182 : 35621 : return fold_to_constant (t);
11183 : :
11184 : 26373769 : t = cxx_eval_outermost_constant_expr (t, allow_non_constant, strict,
11185 : : manifestly_const_eval,
11186 : : false, decl);
11187 : : }
11188 : 64012453 : if (TREE_CODE (t) == TARGET_EXPR)
11189 : : {
11190 : 19690 : tree init = TARGET_EXPR_INITIAL (t);
11191 : 19690 : if (TREE_CODE (init) == CONSTRUCTOR)
11192 : 64048074 : t = init;
11193 : : }
11194 : : return t;
11195 : : }
11196 : :
11197 : : /* Wrapper for maybe_constant_init_1 which permits non constants. */
11198 : :
11199 : : tree
11200 : 48655223 : maybe_constant_init (tree t, tree decl, bool manifestly_const_eval)
11201 : : {
11202 : 48655223 : return maybe_constant_init_1 (t, decl, true, mce_value (manifestly_const_eval));
11203 : : }
11204 : :
11205 : : tree
11206 : 15391381 : maybe_constant_init (tree t, tree decl, mce_value manifestly_const_eval)
11207 : : {
11208 : 15391381 : return maybe_constant_init_1 (t, decl, true, manifestly_const_eval);
11209 : : }
11210 : :
11211 : : /* Wrapper for maybe_constant_init_1 which does not permit non constants. */
11212 : :
11213 : : tree
11214 : 1470 : cxx_constant_init (tree t, tree decl)
11215 : : {
11216 : 1470 : return maybe_constant_init_1 (t, decl, false, mce_true);
11217 : : }
11218 : :
11219 : : /* Return true if CALL_EXPR T might throw during constant evaluation. */
11220 : :
11221 : : static bool
11222 : 135096942 : callee_might_throw (tree t)
11223 : : {
11224 : 135096942 : if (cxx_dialect < cxx26 || !flag_exceptions)
11225 : : return false;
11226 : 37913797 : tree callee = cp_get_callee (t);
11227 : 37913797 : if (callee == NULL_TREE)
11228 : : return false;
11229 : 37861355 : tree callee_fn = cp_get_fndecl_from_callee (callee, false);
11230 : 37861355 : return (!flag_enforce_eh_specs
11231 : 37861355 : || type_dependent_expression_p (callee)
11232 : 33900975 : || !POINTER_TYPE_P (TREE_TYPE (callee))
11233 : 68984136 : || (!type_noexcept_p (TREE_TYPE (TREE_TYPE (callee)))
11234 : 12169354 : && (callee_fn == NULL_TREE || !TREE_NOTHROW (callee_fn))));
11235 : : }
11236 : :
11237 : : #if 0
11238 : : /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
11239 : : /* Return true if the object referred to by REF has automatic or thread
11240 : : local storage. */
11241 : :
11242 : : enum { ck_ok, ck_bad, ck_unknown };
11243 : : static int
11244 : : check_automatic_or_tls (tree ref)
11245 : : {
11246 : : machine_mode mode;
11247 : : poly_int64 bitsize, bitpos;
11248 : : tree offset;
11249 : : int volatilep = 0, unsignedp = 0;
11250 : : tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset,
11251 : : &mode, &unsignedp, &volatilep, false);
11252 : : duration_kind dk;
11253 : :
11254 : : /* If there isn't a decl in the middle, we don't know the linkage here,
11255 : : and this isn't a constant expression anyway. */
11256 : : if (!DECL_P (decl))
11257 : : return ck_unknown;
11258 : : dk = decl_storage_duration (decl);
11259 : : return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok;
11260 : : }
11261 : : #endif
11262 : :
11263 : : /* Data structure for passing data from potential_constant_expression_1
11264 : : to check_for_return_continue via cp_walk_tree. */
11265 : : struct check_for_return_continue_data {
11266 : : hash_set<tree> *pset;
11267 : : tree continue_stmt;
11268 : : tree break_stmt;
11269 : : bool could_throw;
11270 : : };
11271 : :
11272 : : /* Helper function for potential_constant_expression_1 SWITCH_STMT handling,
11273 : : called through cp_walk_tree. Return the first RETURN_EXPR found, or note
11274 : : the first CONTINUE_STMT and/or BREAK_STMT if RETURN_EXPR is not found.
11275 : : For C++26 also note presence of possibly throwing calls. */
11276 : : static tree
11277 : 22968201 : check_for_return_continue (tree *tp, int *walk_subtrees, void *data)
11278 : : {
11279 : 22968201 : tree t = *tp, s, b;
11280 : 22968201 : check_for_return_continue_data *d = (check_for_return_continue_data *) data;
11281 : 22968201 : switch (TREE_CODE (t))
11282 : : {
11283 : : case RETURN_EXPR:
11284 : : return t;
11285 : :
11286 : 30 : case CONTINUE_STMT:
11287 : 30 : if (d->continue_stmt == NULL_TREE)
11288 : 30 : d->continue_stmt = t;
11289 : : break;
11290 : :
11291 : 5229 : case BREAK_STMT:
11292 : 5229 : if (d->break_stmt == NULL_TREE)
11293 : 3102 : d->break_stmt = t;
11294 : : break;
11295 : :
11296 : : #define RECUR(x) \
11297 : : if (tree r = cp_walk_tree (&x, check_for_return_continue, data, \
11298 : : d->pset)) \
11299 : : return r
11300 : :
11301 : : /* For loops, walk subtrees manually, so that continue stmts found
11302 : : inside of the bodies of the loops are ignored. */
11303 : 13276 : case DO_STMT:
11304 : 13276 : *walk_subtrees = 0;
11305 : 13276 : RECUR (DO_COND (t));
11306 : 13276 : s = d->continue_stmt;
11307 : 13276 : b = d->break_stmt;
11308 : 13276 : RECUR (DO_BODY (t));
11309 : 13276 : d->continue_stmt = s;
11310 : 13276 : d->break_stmt = b;
11311 : 13276 : break;
11312 : :
11313 : 101 : case WHILE_STMT:
11314 : 101 : *walk_subtrees = 0;
11315 : 101 : RECUR (WHILE_COND_PREP (t));
11316 : 101 : RECUR (WHILE_COND (t));
11317 : 101 : s = d->continue_stmt;
11318 : 101 : b = d->break_stmt;
11319 : 101 : RECUR (WHILE_BODY (t));
11320 : 95 : d->continue_stmt = s;
11321 : 95 : d->break_stmt = b;
11322 : 95 : break;
11323 : :
11324 : 140 : case FOR_STMT:
11325 : 140 : *walk_subtrees = 0;
11326 : 140 : RECUR (FOR_INIT_STMT (t));
11327 : 140 : RECUR (FOR_COND_PREP (t));
11328 : 140 : RECUR (FOR_COND (t));
11329 : 140 : RECUR (FOR_EXPR (t));
11330 : 140 : s = d->continue_stmt;
11331 : 140 : b = d->break_stmt;
11332 : 140 : RECUR (FOR_BODY (t));
11333 : 94 : d->continue_stmt = s;
11334 : 94 : d->break_stmt = b;
11335 : 94 : break;
11336 : :
11337 : 0 : case RANGE_FOR_STMT:
11338 : 0 : *walk_subtrees = 0;
11339 : 0 : RECUR (RANGE_FOR_EXPR (t));
11340 : 0 : s = d->continue_stmt;
11341 : 0 : b = d->break_stmt;
11342 : 0 : RECUR (RANGE_FOR_BODY (t));
11343 : 0 : d->continue_stmt = s;
11344 : 0 : d->break_stmt = b;
11345 : 0 : break;
11346 : :
11347 : 207 : case SWITCH_STMT:
11348 : 207 : *walk_subtrees = 0;
11349 : 207 : RECUR (SWITCH_STMT_COND (t));
11350 : 207 : b = d->break_stmt;
11351 : 207 : RECUR (SWITCH_STMT_BODY (t));
11352 : 187 : d->break_stmt = b;
11353 : 187 : break;
11354 : : #undef RECUR
11355 : :
11356 : : case STATEMENT_LIST:
11357 : : case CONSTRUCTOR:
11358 : : break;
11359 : :
11360 : 1148902 : case AGGR_INIT_EXPR:
11361 : 1148902 : case CALL_EXPR:
11362 : : /* In C++26 a function could throw. */
11363 : 1148902 : if (callee_might_throw (t))
11364 : 100130 : d->could_throw = true;
11365 : : break;
11366 : :
11367 : 21075196 : default:
11368 : 21075196 : if (!EXPR_P (t))
11369 : 6613043 : *walk_subtrees = 0;
11370 : : break;
11371 : : }
11372 : :
11373 : : return NULL_TREE;
11374 : : }
11375 : :
11376 : : /* Return true if T denotes a potentially constant expression. Issue
11377 : : diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
11378 : : an lvalue-rvalue conversion is implied. If NOW is true, we want to
11379 : : consider the expression in the current context, independent of constexpr
11380 : : substitution. If FUNDEF_P is true, we're checking a constexpr function body
11381 : : and hard errors should not be reported by constexpr_error.
11382 : :
11383 : : C++0x [expr.const] used to say
11384 : :
11385 : : 6 An expression is a potential constant expression if it is
11386 : : a constant expression where all occurrences of function
11387 : : parameters are replaced by arbitrary constant expressions
11388 : : of the appropriate type.
11389 : :
11390 : : 2 A conditional expression is a constant expression unless it
11391 : : involves one of the following as a potentially evaluated
11392 : : subexpression (3.2), but subexpressions of logical AND (5.14),
11393 : : logical OR (5.15), and conditional (5.16) operations that are
11394 : : not evaluated are not considered. */
11395 : :
11396 : : static bool
11397 : 3144545197 : potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
11398 : : bool fundef_p, tsubst_flags_t flags,
11399 : : tree *jump_target)
11400 : : {
11401 : : #define RECUR(T,RV) \
11402 : : potential_constant_expression_1 ((T), (RV), strict, now, fundef_p, flags, \
11403 : : jump_target)
11404 : :
11405 : 3144545197 : enum { any = false, rval = true };
11406 : 3144545197 : int i;
11407 : 3144545197 : tree tmp;
11408 : :
11409 : 3144545197 : if (t == error_mark_node)
11410 : : return false;
11411 : 3144534174 : if (t == NULL_TREE)
11412 : : return true;
11413 : 3138911971 : location_t loc = cp_expr_loc_or_input_loc (t);
11414 : :
11415 : 3138911971 : if (*jump_target)
11416 : : /* If we are jumping, ignore everything. This is simpler than the
11417 : : cxx_eval_constant_expression handling because we only need to be
11418 : : conservatively correct, and we don't necessarily have a constant value
11419 : : available, so we don't bother with switch tracking. */
11420 : : return true;
11421 : :
11422 : 822124 : if (TREE_THIS_VOLATILE (t) && want_rval
11423 : 283280 : && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (t))
11424 : 3133261337 : && !NULLPTR_TYPE_P (TREE_TYPE (t)))
11425 : : {
11426 : 77715 : if (TREE_CLOBBER_P (t))
11427 : : {
11428 : : /* We should have caught any clobbers in INIT/MODIFY_EXPR. */
11429 : 0 : gcc_checking_assert (false);
11430 : : return true;
11431 : : }
11432 : :
11433 : 77715 : if (flags & tf_error)
11434 : 21 : constexpr_error (loc, fundef_p, "lvalue-to-rvalue conversion of "
11435 : : "a volatile lvalue %qE with type %qT", t,
11436 : 21 : TREE_TYPE (t));
11437 : 77715 : return false;
11438 : : }
11439 : 3133105881 : if (CONSTANT_CLASS_P (t))
11440 : : return true;
11441 : 2374047669 : if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED)
11442 : 2374047669 : && TREE_TYPE (t) == error_mark_node)
11443 : : return false;
11444 : :
11445 : 2374047644 : switch (TREE_CODE (t))
11446 : : {
11447 : : case FUNCTION_DECL:
11448 : : case BASELINK:
11449 : : case TEMPLATE_DECL:
11450 : : case OVERLOAD:
11451 : : case TEMPLATE_ID_EXPR:
11452 : : case LABEL_DECL:
11453 : : case CASE_LABEL_EXPR:
11454 : : case PREDICT_EXPR:
11455 : : case CONST_DECL:
11456 : : case SIZEOF_EXPR:
11457 : : case ALIGNOF_EXPR:
11458 : : case OFFSETOF_EXPR:
11459 : : case NOEXCEPT_EXPR:
11460 : : case TEMPLATE_PARM_INDEX:
11461 : : case TRAIT_EXPR:
11462 : : case IDENTIFIER_NODE:
11463 : : case USERDEF_LITERAL:
11464 : : /* We can see a FIELD_DECL in a pointer-to-member expression. */
11465 : : case FIELD_DECL:
11466 : : case RESULT_DECL:
11467 : : case USING_DECL:
11468 : : case USING_STMT:
11469 : : case PLACEHOLDER_EXPR:
11470 : : case REQUIRES_EXPR:
11471 : : case STATIC_ASSERT:
11472 : : case DEBUG_BEGIN_STMT:
11473 : : return true;
11474 : :
11475 : 15307645 : case RETURN_EXPR:
11476 : 15307645 : if (!RECUR (TREE_OPERAND (t, 0), any))
11477 : : return false;
11478 : : /* FALLTHROUGH */
11479 : :
11480 : 15172999 : case BREAK_STMT:
11481 : 15172999 : case CONTINUE_STMT:
11482 : 15172999 : *jump_target = t;
11483 : 15172999 : return true;
11484 : :
11485 : 239833349 : case PARM_DECL:
11486 : 239833349 : if (now && want_rval)
11487 : : {
11488 : 71919381 : tree type = TREE_TYPE (t);
11489 : 71919381 : if (dependent_type_p (type)
11490 : 47678365 : || !COMPLETE_TYPE_P (processing_template_decl
11491 : : ? type : complete_type (type))
11492 : 119597744 : || is_really_empty_class (type, /*ignore_vptr*/false))
11493 : : /* An empty class has no data to read. */
11494 : 24241352 : return true;
11495 : 47678029 : if (flags & tf_error)
11496 : 16 : constexpr_error (input_location, fundef_p,
11497 : : "%qE is not a constant expression", t);
11498 : 47678029 : return false;
11499 : : }
11500 : : return true;
11501 : :
11502 : 181811441 : case AGGR_INIT_EXPR:
11503 : 181811441 : case CALL_EXPR:
11504 : : /* -- an invocation of a function other than a constexpr function
11505 : : or a constexpr constructor. */
11506 : 181811441 : {
11507 : 181811441 : tree fun = get_function_named_in_call (t);
11508 : 181811441 : const int nargs = call_expr_nargs (t);
11509 : 181811441 : i = 0;
11510 : :
11511 : 181811441 : if (fun == NULL_TREE)
11512 : : {
11513 : : /* Reset to allow the function to continue past the end
11514 : : of the block below. Otherwise return early. */
11515 : 100075 : bool bail = true;
11516 : :
11517 : 100075 : if (TREE_CODE (t) == CALL_EXPR
11518 : 100075 : && CALL_EXPR_FN (t) == NULL_TREE)
11519 : 100075 : switch (CALL_EXPR_IFN (t))
11520 : : {
11521 : : /* These should be ignored, they are optimized away from
11522 : : constexpr functions. */
11523 : : case IFN_UBSAN_NULL:
11524 : : case IFN_UBSAN_BOUNDS:
11525 : : case IFN_UBSAN_VPTR:
11526 : : case IFN_FALLTHROUGH:
11527 : : case IFN_ASSUME:
11528 : : return true;
11529 : :
11530 : : case IFN_ADD_OVERFLOW:
11531 : : case IFN_SUB_OVERFLOW:
11532 : : case IFN_MUL_OVERFLOW:
11533 : : case IFN_LAUNDER:
11534 : : case IFN_VEC_CONVERT:
11535 : : bail = false;
11536 : : break;
11537 : :
11538 : : default:
11539 : : break;
11540 : : }
11541 : :
11542 : : if (bail)
11543 : : {
11544 : : /* fold_call_expr can't do anything with IFN calls. */
11545 : 44 : if (flags & tf_error)
11546 : 0 : constexpr_error (loc, fundef_p,
11547 : : "call to internal function %qE", t);
11548 : 44 : return false;
11549 : : }
11550 : : }
11551 : :
11552 : 181711366 : if (fun && is_overloaded_fn (fun))
11553 : : {
11554 : 165493946 : if (!RECUR (fun, true))
11555 : : return false;
11556 : 165083306 : fun = get_fns (fun);
11557 : :
11558 : 165083306 : if (TREE_CODE (fun) == FUNCTION_DECL)
11559 : : {
11560 : 144566291 : if (builtin_valid_in_constant_expr_p (fun))
11561 : : return true;
11562 : 143772494 : if (!maybe_constexpr_fn (fun)
11563 : : /* Allow any built-in function; if the expansion
11564 : : isn't constant, we'll deal with that then. */
11565 : 42540882 : && !fndecl_built_in_p (fun)
11566 : : /* In C++20, replaceable global allocation functions
11567 : : are constant expressions. */
11568 : 30433406 : && (!cxx_replaceable_global_alloc_fn (fun)
11569 : 177787 : || TREE_CODE (t) != CALL_EXPR
11570 : 177787 : || (!CALL_FROM_NEW_OR_DELETE_P (t)
11571 : 72525 : && (current_function_decl == NULL_TREE
11572 : 72525 : || !is_std_allocator_allocate
11573 : 72525 : (current_function_decl))))
11574 : : /* Allow placement new in std::construct_at. */
11575 : 30262886 : && (!cxx_placement_new_fn (fun)
11576 : 50798 : || TREE_CODE (t) != CALL_EXPR
11577 : 50798 : || current_function_decl == NULL_TREE
11578 : 50792 : || !is_std_construct_at (current_function_decl))
11579 : 30229514 : && !cxx_dynamic_cast_fn_p (fun)
11580 : 173999394 : && !cxx_cxa_builtin_fn_p (fun))
11581 : : {
11582 : : /* In C++26 evaluation of the function arguments might
11583 : : throw and in that case it is irrelevant whether
11584 : : fun is constexpr or not. */
11585 : 30194187 : if (cxx_dialect >= cxx26)
11586 : 10819458 : for (; i < nargs; ++i)
11587 : : {
11588 : 6533128 : tree x = get_nth_callarg (t, i);
11589 : 6533128 : bool rv = processing_template_decl ? any : rval;
11590 : 6533128 : bool sub_now = false;
11591 : 6533128 : if (!potential_constant_expression_1 (x, rv, strict,
11592 : : sub_now,
11593 : : fundef_p,
11594 : : flags,
11595 : : jump_target))
11596 : : return false;
11597 : 5987802 : if (throws (jump_target))
11598 : : return true;
11599 : : }
11600 : 29554709 : if ((flags & tf_error)
11601 : 29554709 : && constexpr_error (loc, fundef_p,
11602 : : "call to non-%<constexpr%> "
11603 : : "function %qD", fun))
11604 : 260 : explain_invalid_constexpr_fn (fun);
11605 : 29554709 : return false;
11606 : : }
11607 : : }
11608 : :
11609 : 134095322 : fun = OVL_FIRST (fun);
11610 : : /* Skip initial arguments to base constructors. */
11611 : 134095322 : if (DECL_BASE_CONSTRUCTOR_P (fun))
11612 : 2389040 : i = num_artificial_parms_for (fun);
11613 : : }
11614 : 16217420 : else if (fun)
11615 : : {
11616 : 16217420 : if (TREE_TYPE (fun)
11617 : 16217420 : && FUNCTION_POINTER_TYPE_P (TREE_TYPE (fun)))
11618 : : want_rval = rval;
11619 : : else
11620 : : want_rval = any;
11621 : 16217420 : if (RECUR (fun, want_rval))
11622 : : /* Might end up being a constant function pointer. But it
11623 : : could also be a function object with constexpr op(), so
11624 : : we pass 'any' so that the underlying VAR_DECL is deemed
11625 : : as potentially-constant even though it wasn't declared
11626 : : constexpr. */;
11627 : : else
11628 : : return false;
11629 : : }
11630 : 296951799 : for (; i < nargs; ++i)
11631 : : {
11632 : 162927206 : tree x = get_nth_callarg (t, i);
11633 : : /* In a template, reference arguments haven't been converted to
11634 : : REFERENCE_TYPE and we might not even know if the parameter
11635 : : is a reference, so accept lvalue constants too. */
11636 : 162927206 : bool rv = processing_template_decl ? any : rval;
11637 : : /* Don't require an immediately constant value, as constexpr
11638 : : substitution might not use the value of the argument. */
11639 : 162927206 : bool sub_now = false;
11640 : 162927206 : if (!potential_constant_expression_1 (x, rv, strict,
11641 : : sub_now, fundef_p, flags,
11642 : : jump_target))
11643 : : return false;
11644 : 1937216297 : if (throws (jump_target))
11645 : : return true;
11646 : : }
11647 : : /* In C++26 a function could throw. */
11648 : 134024593 : if (*jump_target == NULL_TREE && callee_might_throw (t))
11649 : 12100656 : *jump_target = void_node;
11650 : : return true;
11651 : : }
11652 : :
11653 : 131617174 : case NON_LVALUE_EXPR:
11654 : : /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
11655 : : -- an lvalue of integral type that refers to a non-volatile
11656 : : const variable or static data member initialized with
11657 : : constant expressions, or
11658 : :
11659 : : -- an lvalue of literal type that refers to non-volatile
11660 : : object defined with constexpr, or that refers to a
11661 : : sub-object of such an object; */
11662 : 131617174 : return RECUR (TREE_OPERAND (t, 0), rval);
11663 : :
11664 : 34415 : case EXCESS_PRECISION_EXPR:
11665 : 34415 : return RECUR (TREE_OPERAND (t, 0), rval);
11666 : :
11667 : 238143557 : case VAR_DECL:
11668 : 238143557 : if (DECL_HAS_VALUE_EXPR_P (t))
11669 : : {
11670 : 4019636 : if (now && is_normal_capture_proxy (t))
11671 : : {
11672 : : /* -- in a lambda-expression, a reference to this or to a
11673 : : variable with automatic storage duration defined outside that
11674 : : lambda-expression, where the reference would be an
11675 : : odr-use. */
11676 : :
11677 : 440739 : if (want_rval)
11678 : : /* Since we're doing an lvalue-rvalue conversion, this might
11679 : : not be an odr-use, so evaluate the variable directly. */
11680 : 440062 : return RECUR (DECL_CAPTURED_VARIABLE (t), rval);
11681 : :
11682 : 677 : if (flags & tf_error)
11683 : : {
11684 : 3 : tree cap = DECL_CAPTURED_VARIABLE (t);
11685 : 3 : auto_diagnostic_group d;
11686 : 3 : if (constexpr_error (input_location, fundef_p,
11687 : : "lambda capture of %qE is not a "
11688 : : "constant expression", cap)
11689 : 3 : && decl_constant_var_p (cap))
11690 : 3 : inform (input_location, "because it is used as a glvalue");
11691 : 3 : }
11692 : 677 : return false;
11693 : : }
11694 : 3578897 : tree ve = DECL_VALUE_EXPR (t);
11695 : : /* Treat __PRETTY_FUNCTION__ inside a template function as
11696 : : potentially-constant. */
11697 : 3578897 : if (DECL_PRETTY_FUNCTION_P (t) && ve == error_mark_node)
11698 : : return true;
11699 : 3578892 : if (DECL_DECOMPOSITION_P (t) && TREE_CODE (ve) == TREE_VEC)
11700 : 2736 : return RECUR (TREE_VEC_ELT (ve, 0), rval);
11701 : 3576156 : return RECUR (ve, rval);
11702 : : }
11703 : 234123921 : if (want_rval
11704 : 160989666 : && (now || !var_in_maybe_constexpr_fn (t))
11705 : 151809284 : && !type_dependent_expression_p (t)
11706 : 120532376 : && !decl_maybe_constant_var_p (t)
11707 : 50658861 : && !is_local_temp (t)
11708 : 50213871 : && (strict
11709 : 1308994 : || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t))
11710 : 413458 : || (DECL_INITIAL (t)
11711 : 406441 : && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t)))
11712 : 50206085 : && COMPLETE_TYPE_P (TREE_TYPE (t))
11713 : 284315235 : && !is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
11714 : : {
11715 : 50186490 : if (flags & tf_error)
11716 : 160 : non_const_var_error (loc, t, fundef_p);
11717 : 50186490 : return false;
11718 : : }
11719 : : return true;
11720 : :
11721 : 253391170 : case NOP_EXPR:
11722 : 253391170 : if (REINTERPRET_CAST_P (t))
11723 : : {
11724 : 131186 : if (flags & tf_error)
11725 : 47 : constexpr_error (loc, fundef_p, "%<reinterpret_cast%> is not a "
11726 : : "constant expression");
11727 : 131186 : return false;
11728 : : }
11729 : : /* FALLTHRU */
11730 : 583168373 : case CONVERT_EXPR:
11731 : 583168373 : case VIEW_CONVERT_EXPR:
11732 : : /* -- a reinterpret_cast. FIXME not implemented, and this rule
11733 : : may change to something more specific to type-punning (DR 1312). */
11734 : 583168373 : {
11735 : 583168373 : tree from = TREE_OPERAND (t, 0);
11736 : 583168373 : if (location_wrapper_p (t))
11737 : : {
11738 : 289670136 : iloc_sentinel ils = loc;
11739 : 289670136 : return (RECUR (from, want_rval));
11740 : 289670136 : }
11741 : 293498237 : if (INDIRECT_TYPE_P (TREE_TYPE (t)))
11742 : : {
11743 : 151129992 : STRIP_ANY_LOCATION_WRAPPER (from);
11744 : 151129992 : if (TREE_CODE (from) == INTEGER_CST
11745 : 151129992 : && !integer_zerop (from))
11746 : : {
11747 : 1766 : if (flags & tf_error)
11748 : 22 : constexpr_error (loc, fundef_p,
11749 : : "%<reinterpret_cast%> from integer to "
11750 : : "pointer");
11751 : 1766 : return false;
11752 : : }
11753 : : }
11754 : 293496471 : return (RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR));
11755 : : }
11756 : :
11757 : 11726 : case ADDRESSOF_EXPR:
11758 : : /* This is like ADDR_EXPR, except it won't form pointer-to-member. */
11759 : 11726 : t = TREE_OPERAND (t, 0);
11760 : 11726 : goto handle_addr_expr;
11761 : :
11762 : 57437008 : case ADDR_EXPR:
11763 : : /* -- a unary operator & that is applied to an lvalue that
11764 : : designates an object with thread or automatic storage
11765 : : duration; */
11766 : 57437008 : t = TREE_OPERAND (t, 0);
11767 : :
11768 : 57437008 : if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t))
11769 : : /* A pointer-to-member constant. */
11770 : : return true;
11771 : :
11772 : 57448496 : handle_addr_expr:
11773 : : #if 0
11774 : : /* FIXME adjust when issue 1197 is fully resolved. For now don't do
11775 : : any checking here, as we might dereference the pointer later. If
11776 : : we remove this code, also remove check_automatic_or_tls. */
11777 : : i = check_automatic_or_tls (t);
11778 : : if (i == ck_ok)
11779 : : return true;
11780 : : if (i == ck_bad)
11781 : : {
11782 : : if (flags & tf_error)
11783 : : error ("address-of an object %qE with thread local or "
11784 : : "automatic storage is not a constant expression", t);
11785 : : return false;
11786 : : }
11787 : : #endif
11788 : 57448496 : return RECUR (t, any);
11789 : :
11790 : 73654109 : case COMPONENT_REF:
11791 : 73654109 : case ARROW_EXPR:
11792 : 73654109 : case OFFSET_REF:
11793 : : /* -- a class member access unless its postfix-expression is
11794 : : of literal type or of pointer to literal type. */
11795 : : /* This test would be redundant, as it follows from the
11796 : : postfix-expression being a potential constant expression. */
11797 : 73654109 : if (type_unknown_p (t))
11798 : : return true;
11799 : 67203591 : if (is_overloaded_fn (t))
11800 : : /* In a template, a COMPONENT_REF of a function expresses ob.fn(),
11801 : : which uses ob as an lvalue. */
11802 : 68772082 : want_rval = false;
11803 : 68772082 : gcc_fallthrough ();
11804 : :
11805 : 68772082 : case REALPART_EXPR:
11806 : 68772082 : case IMAGPART_EXPR:
11807 : 68772082 : case BIT_FIELD_REF:
11808 : 68772082 : return RECUR (TREE_OPERAND (t, 0), want_rval);
11809 : :
11810 : 220027 : case EXPR_PACK_EXPANSION:
11811 : 220027 : return RECUR (PACK_EXPANSION_PATTERN (t), want_rval);
11812 : :
11813 : : case PACK_INDEX_EXPR:
11814 : : return true;
11815 : :
11816 : 71365855 : case INDIRECT_REF:
11817 : 71365855 : return RECUR (TREE_OPERAND (t, 0), rval);
11818 : :
11819 : 14313707 : case STATEMENT_LIST:
11820 : 52114550 : for (tree stmt : tsi_range (t))
11821 : 38211658 : if (!RECUR (stmt, any))
11822 : 250534546 : return false;
11823 : : return true;
11824 : :
11825 : 2172377 : case MODIFY_EXPR:
11826 : 2172377 : if (cxx_dialect < cxx14)
11827 : 1957 : goto fail;
11828 : 2170420 : if (!RECUR (TREE_OPERAND (t, 0), any))
11829 : : return false;
11830 : : /* Just ignore clobbers. */
11831 : 1744456 : if (TREE_CLOBBER_P (TREE_OPERAND (t, 1)))
11832 : : return true;
11833 : 1605583 : if (!RECUR (TREE_OPERAND (t, 1), rval))
11834 : : return false;
11835 : : return true;
11836 : :
11837 : 321188 : case MODOP_EXPR:
11838 : 321188 : if (cxx_dialect < cxx14)
11839 : 96 : goto fail;
11840 : 321092 : if (!RECUR (TREE_OPERAND (t, 0), rval))
11841 : : return false;
11842 : 309724 : if (!RECUR (TREE_OPERAND (t, 2), rval))
11843 : : return false;
11844 : : return true;
11845 : :
11846 : 291182 : case DO_STMT:
11847 : 291182 : if (!RECUR (DO_COND (t), rval))
11848 : : return false;
11849 : 291182 : if (!RECUR (DO_BODY (t), any))
11850 : : return false;
11851 : 291141 : if (breaks (jump_target) || continues (jump_target))
11852 : 2 : *jump_target = NULL_TREE;
11853 : : return true;
11854 : :
11855 : 224476 : case FOR_STMT:
11856 : 224476 : if (!RECUR (FOR_INIT_STMT (t), any))
11857 : : return false;
11858 : 224476 : if (!RECUR (FOR_COND_PREP (t), any))
11859 : : return false;
11860 : 224476 : tmp = FOR_COND (t);
11861 : 224476 : if (!RECUR (tmp, rval))
11862 : : return false;
11863 : 224406 : if (tmp)
11864 : : {
11865 : 180062 : if (!processing_template_decl)
11866 : 175271 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
11867 : : /* If we couldn't evaluate the condition, it might not ever be
11868 : : true. */
11869 : 180062 : if (!integer_onep (tmp))
11870 : : {
11871 : : /* Before returning true, check if the for body can contain
11872 : : a return. */
11873 : 180062 : hash_set<tree> pset;
11874 : 180062 : check_for_return_continue_data data = { &pset, NULL_TREE,
11875 : 180062 : NULL_TREE, false };
11876 : 180062 : if (tree ret_expr
11877 : 180062 : = cp_walk_tree (&FOR_BODY (t), check_for_return_continue,
11878 : : &data, &pset))
11879 : 104035 : *jump_target = ret_expr;
11880 : 180062 : if (data.could_throw)
11881 : 14795 : *jump_target = void_node;
11882 : 180062 : return true;
11883 : 180062 : }
11884 : : }
11885 : 44344 : if (!RECUR (FOR_EXPR (t), any))
11886 : : return false;
11887 : 44344 : if (!RECUR (FOR_BODY (t), any))
11888 : : return false;
11889 : 44344 : if (!RECUR (FOR_COND_CLEANUP (t), any))
11890 : : return false;
11891 : 44344 : if (breaks (jump_target) || continues (jump_target))
11892 : 12 : *jump_target = NULL_TREE;
11893 : : return true;
11894 : :
11895 : 425 : case RANGE_FOR_STMT:
11896 : 425 : if (!RECUR (RANGE_FOR_INIT_STMT (t), any))
11897 : : return false;
11898 : 425 : if (!RECUR (RANGE_FOR_EXPR (t), any))
11899 : : return false;
11900 : 425 : if (!RECUR (RANGE_FOR_BODY (t), any))
11901 : : return false;
11902 : 421 : if (breaks (jump_target) || continues (jump_target))
11903 : 0 : *jump_target = NULL_TREE;
11904 : : return true;
11905 : :
11906 : 93259 : case WHILE_STMT:
11907 : 93259 : if (!RECUR (WHILE_COND_PREP (t), any))
11908 : : return false;
11909 : 93259 : tmp = WHILE_COND (t);
11910 : 93259 : if (!RECUR (tmp, rval))
11911 : : return false;
11912 : 93198 : if (!processing_template_decl)
11913 : 93182 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
11914 : : /* If we couldn't evaluate the condition, it might not ever be true. */
11915 : 93198 : if (!integer_onep (tmp))
11916 : : {
11917 : : /* Before returning true, check if the while body can contain
11918 : : a return. */
11919 : 90349 : hash_set<tree> pset;
11920 : 90349 : check_for_return_continue_data data = { &pset, NULL_TREE,
11921 : 90349 : NULL_TREE, false };
11922 : 90349 : if (tree ret_expr
11923 : 90349 : = cp_walk_tree (&WHILE_BODY (t), check_for_return_continue,
11924 : : &data, &pset))
11925 : 235 : *jump_target = ret_expr;
11926 : 90349 : if (data.could_throw)
11927 : 6583 : *jump_target = void_node;
11928 : 90349 : return true;
11929 : 90349 : }
11930 : 2849 : if (!RECUR (WHILE_BODY (t), any))
11931 : : return false;
11932 : 2848 : if (!RECUR (WHILE_COND_CLEANUP (t), any))
11933 : : return false;
11934 : 2848 : if (breaks (jump_target) || continues (jump_target))
11935 : 16 : *jump_target = NULL_TREE;
11936 : : return true;
11937 : :
11938 : 21386 : case SWITCH_STMT:
11939 : 21386 : if (!RECUR (SWITCH_STMT_COND (t), rval))
11940 : : return false;
11941 : : /* FIXME we don't check SWITCH_STMT_BODY currently, because even
11942 : : unreachable labels would be checked and it is enough if there is
11943 : : a single switch cond value for which it is a valid constant
11944 : : expression. We need to check if there are any RETURN_EXPRs
11945 : : or CONTINUE_STMTs inside of the body though, as in that case
11946 : : we need to set *jump_target. */
11947 : : else
11948 : : {
11949 : 21380 : hash_set<tree> pset;
11950 : 21380 : check_for_return_continue_data data = { &pset, NULL_TREE,
11951 : 21380 : NULL_TREE, false };
11952 : 21380 : if (tree ret_expr
11953 : 21380 : = cp_walk_tree (&SWITCH_STMT_BODY (t), check_for_return_continue,
11954 : : &data, &pset))
11955 : : /* The switch might return. */
11956 : 20863 : *jump_target = ret_expr;
11957 : 517 : else if (data.continue_stmt)
11958 : : /* The switch can't return, but might continue. */
11959 : 3 : *jump_target = data.continue_stmt;
11960 : 21380 : if (data.could_throw)
11961 : 34 : *jump_target = void_node;
11962 : 21380 : }
11963 : 21380 : return true;
11964 : :
11965 : 44 : case STMT_EXPR:
11966 : 44 : return RECUR (STMT_EXPR_STMT (t), rval);
11967 : :
11968 : 273186 : case LAMBDA_EXPR:
11969 : 273186 : if (cxx_dialect >= cxx17)
11970 : : /* In C++17 lambdas can be constexpr, don't give up yet. */
11971 : : return true;
11972 : 413 : else if (flags & tf_error)
11973 : 0 : constexpr_error (loc, fundef_p, "lambda-expression is not a "
11974 : : "constant expression before C++17");
11975 : : return false;
11976 : :
11977 : 107461 : case NEW_EXPR:
11978 : 107461 : case VEC_NEW_EXPR:
11979 : 107461 : case DELETE_EXPR:
11980 : 107461 : case VEC_DELETE_EXPR:
11981 : 107461 : if (cxx_dialect >= cxx20)
11982 : : /* In C++20, new-expressions are potentially constant. */
11983 : : return true;
11984 : 79303 : else if (flags & tf_error)
11985 : 0 : constexpr_error (loc, fundef_p, "new-expression is not a "
11986 : : "constant expression before C++20");
11987 : : return false;
11988 : :
11989 : 66050 : case DYNAMIC_CAST_EXPR:
11990 : 66050 : case PSEUDO_DTOR_EXPR:
11991 : 66050 : case OMP_PARALLEL:
11992 : 66050 : case OMP_TASK:
11993 : 66050 : case OMP_FOR:
11994 : 66050 : case OMP_SIMD:
11995 : 66050 : case OMP_DISTRIBUTE:
11996 : 66050 : case OMP_TASKLOOP:
11997 : 66050 : case OMP_LOOP:
11998 : 66050 : case OMP_TEAMS:
11999 : 66050 : case OMP_TARGET_DATA:
12000 : 66050 : case OMP_TARGET:
12001 : 66050 : case OMP_SECTIONS:
12002 : 66050 : case OMP_ORDERED:
12003 : 66050 : case OMP_CRITICAL:
12004 : 66050 : case OMP_SINGLE:
12005 : 66050 : case OMP_SCAN:
12006 : 66050 : case OMP_SCOPE:
12007 : 66050 : case OMP_SECTION:
12008 : 66050 : case OMP_MASTER:
12009 : 66050 : case OMP_MASKED:
12010 : 66050 : case OMP_TASKGROUP:
12011 : 66050 : case OMP_TARGET_UPDATE:
12012 : 66050 : case OMP_TARGET_ENTER_DATA:
12013 : 66050 : case OMP_TARGET_EXIT_DATA:
12014 : 66050 : case OMP_ATOMIC:
12015 : 66050 : case OMP_ATOMIC_READ:
12016 : 66050 : case OMP_ATOMIC_CAPTURE_OLD:
12017 : 66050 : case OMP_ATOMIC_CAPTURE_NEW:
12018 : 66050 : case OMP_DEPOBJ:
12019 : 66050 : case OACC_PARALLEL:
12020 : 66050 : case OACC_KERNELS:
12021 : 66050 : case OACC_SERIAL:
12022 : 66050 : case OACC_DATA:
12023 : 66050 : case OACC_HOST_DATA:
12024 : 66050 : case OACC_LOOP:
12025 : 66050 : case OACC_CACHE:
12026 : 66050 : case OACC_DECLARE:
12027 : 66050 : case OACC_ENTER_DATA:
12028 : 66050 : case OACC_EXIT_DATA:
12029 : 66050 : case OACC_UPDATE:
12030 : 66050 : case OMP_ARRAY_SECTION:
12031 : : /* GCC internal stuff. */
12032 : 66050 : case VA_ARG_EXPR:
12033 : 66050 : case TRANSACTION_EXPR:
12034 : 66050 : case AT_ENCODE_EXPR:
12035 : 66050 : fail:
12036 : 66050 : if (flags & tf_error)
12037 : 25 : constexpr_error (loc, fundef_p, "expression %qE is not a constant "
12038 : : "expression", t);
12039 : : return false;
12040 : :
12041 : : case OMP_DECLARE_MAPPER:
12042 : : /* This can be used to initialize VAR_DECLs: it's treated as a magic
12043 : : constant. */
12044 : : return true;
12045 : :
12046 : 464 : case THROW_EXPR:
12047 : 464 : if (cxx_dialect < cxx26)
12048 : 285 : goto fail;
12049 : 179 : return RECUR (TREE_OPERAND (t, 0), rval);
12050 : :
12051 : 485 : case ASM_EXPR:
12052 : 485 : if (flags & tf_error)
12053 : 3 : inline_asm_in_constexpr_error (loc, fundef_p);
12054 : : return false;
12055 : :
12056 : 367550 : case OBJ_TYPE_REF:
12057 : 367550 : if (cxx_dialect >= cxx20)
12058 : : /* In C++20 virtual calls can be constexpr, don't give up yet. */
12059 : : return true;
12060 : 225125 : else if (flags & tf_error)
12061 : 0 : constexpr_error (loc, fundef_p, "virtual functions cannot be "
12062 : : "%<constexpr%> before C++20");
12063 : : return false;
12064 : :
12065 : 6638 : case TYPEID_EXPR:
12066 : : /* In C++20, a typeid expression whose operand is of polymorphic
12067 : : class type can be constexpr. */
12068 : 6638 : {
12069 : 6638 : tree e = TREE_OPERAND (t, 0);
12070 : 6638 : if (cxx_dialect < cxx20
12071 : 513 : && strict
12072 : 513 : && !TYPE_P (e)
12073 : 273 : && !type_dependent_expression_p (e)
12074 : 6644 : && TYPE_POLYMORPHIC_P (TREE_TYPE (e)))
12075 : : {
12076 : 4 : if (flags & tf_error)
12077 : 1 : constexpr_error (loc, fundef_p, "%<typeid%> is not a "
12078 : : "constant expression because %qE is "
12079 : : "of polymorphic type", e);
12080 : 4 : return false;
12081 : : }
12082 : : return true;
12083 : : }
12084 : :
12085 : 25465260 : case POINTER_DIFF_EXPR:
12086 : 25465260 : case MINUS_EXPR:
12087 : 25465260 : want_rval = true;
12088 : 25465260 : goto binary;
12089 : :
12090 : 61254503 : case LT_EXPR:
12091 : 61254503 : case LE_EXPR:
12092 : 61254503 : case GT_EXPR:
12093 : 61254503 : case GE_EXPR:
12094 : 61254503 : case EQ_EXPR:
12095 : 61254503 : case NE_EXPR:
12096 : 61254503 : case SPACESHIP_EXPR:
12097 : 61254503 : want_rval = true;
12098 : 61254503 : goto binary;
12099 : :
12100 : 1341346 : case PREINCREMENT_EXPR:
12101 : 1341346 : case POSTINCREMENT_EXPR:
12102 : 1341346 : case PREDECREMENT_EXPR:
12103 : 1341346 : case POSTDECREMENT_EXPR:
12104 : 1341346 : if (cxx_dialect < cxx14)
12105 : 8064 : goto fail;
12106 : 1333282 : goto unary;
12107 : :
12108 : 1035720 : case BIT_NOT_EXPR:
12109 : : /* A destructor. */
12110 : 1035720 : if (TYPE_P (TREE_OPERAND (t, 0)))
12111 : : return true;
12112 : : /* fall through. */
12113 : :
12114 : 43550617 : case CONJ_EXPR:
12115 : 43550617 : case SAVE_EXPR:
12116 : 43550617 : case FIX_TRUNC_EXPR:
12117 : 43550617 : case FLOAT_EXPR:
12118 : 43550617 : case NEGATE_EXPR:
12119 : 43550617 : case ABS_EXPR:
12120 : 43550617 : case ABSU_EXPR:
12121 : 43550617 : case TRUTH_NOT_EXPR:
12122 : 43550617 : case FIXED_CONVERT_EXPR:
12123 : 43550617 : case UNARY_PLUS_EXPR:
12124 : 43550617 : case UNARY_LEFT_FOLD_EXPR:
12125 : 43550617 : case UNARY_RIGHT_FOLD_EXPR:
12126 : 43550617 : case VEC_DUPLICATE_EXPR:
12127 : 1035720 : unary:
12128 : 43550617 : return RECUR (TREE_OPERAND (t, 0), rval);
12129 : :
12130 : 27942295 : case CAST_EXPR:
12131 : 27942295 : case CONST_CAST_EXPR:
12132 : 27942295 : case STATIC_CAST_EXPR:
12133 : 27942295 : case REINTERPRET_CAST_EXPR:
12134 : 27942295 : case IMPLICIT_CONV_EXPR:
12135 : 27942295 : if (!cast_valid_in_integral_constant_expression_p (TREE_TYPE (t)))
12136 : : /* In C++98, a conversion to non-integral type can't be part of a
12137 : : constant expression. */
12138 : : {
12139 : 283 : if (flags & tf_error)
12140 : 0 : constexpr_error (loc, fundef_p,
12141 : : "cast to non-integral type %qT in a constant "
12142 : 0 : "expression", TREE_TYPE (t));
12143 : 283 : return false;
12144 : : }
12145 : : /* This might be a conversion from a class to a (potentially) literal
12146 : : type. Let's consider it potentially constant since the conversion
12147 : : might be a constexpr user-defined conversion. */
12148 : 27942012 : else if (cxx_dialect >= cxx11
12149 : 27923174 : && (dependent_type_p (TREE_TYPE (t))
12150 : 7919459 : || !COMPLETE_TYPE_P (TREE_TYPE (t))
12151 : 7914511 : || literal_type_p (TREE_TYPE (t)))
12152 : 27898045 : && TREE_OPERAND (t, 0)
12153 : 55619081 : && (TREE_CODE (t) != CAST_EXPR
12154 : 22224118 : || !TREE_CHAIN (TREE_OPERAND (t, 0))))
12155 : : {
12156 : 27646843 : tree from = TREE_OPERAND (t, 0);
12157 : 27646843 : if (TREE_CODE (t) == CAST_EXPR)
12158 : 22193892 : from = TREE_VALUE (from);
12159 : 27646843 : tree type = TREE_TYPE (from);
12160 : : /* If this is a dependent type, it could end up being a class
12161 : : with conversions. */
12162 : 27646843 : if (type == NULL_TREE || WILDCARD_TYPE_P (type))
12163 : : return true;
12164 : : /* Or a non-dependent class which has conversions. */
12165 : 351471 : else if (CLASS_TYPE_P (type)
12166 : 351471 : && (TYPE_HAS_CONVERSION (type) || dependent_scope_p (type)))
12167 : 98061 : return true;
12168 : : }
12169 : :
12170 : 23564889 : return (RECUR (TREE_OPERAND (t, 0),
12171 : 23564889 : !TYPE_REF_P (TREE_TYPE (t))));
12172 : :
12173 : 8299977 : case BIND_EXPR:
12174 : 8299977 : return RECUR (BIND_EXPR_BODY (t), want_rval);
12175 : :
12176 : 36157079 : case CLEANUP_POINT_EXPR:
12177 : 36157079 : case MUST_NOT_THROW_EXPR:
12178 : 36157079 : case TRY_CATCH_EXPR:
12179 : : /* Even for C++26 handle TRY_BLOCK conservatively, if we detect the
12180 : : body could throw, even with catch (...) among handlers we'd need
12181 : : to analyze them in detail if they couldn't rethrow it. More
12182 : : importantly though, throws (jump_target) is just conservative,
12183 : : and there could be e.g.
12184 : : try
12185 : : {
12186 : : possibly_throwing_fn (args);
12187 : : break;
12188 : : }
12189 : : catch (...)
12190 : : {
12191 : : }
12192 : : or continue or return instead of break. So, clearing *jump_target
12193 : : because we see catch (...) handler might mean we missed break
12194 : : etc. */
12195 : 36157079 : case TRY_BLOCK:
12196 : 36157079 : case EH_SPEC_BLOCK:
12197 : 36157079 : case EXPR_STMT:
12198 : 36157079 : case PAREN_EXPR:
12199 : : /* For convenience. */
12200 : 36157079 : case LOOP_EXPR:
12201 : 36157079 : case EXIT_EXPR:
12202 : 36157079 : return RECUR (TREE_OPERAND (t, 0), want_rval);
12203 : :
12204 : 5781574 : case DECL_EXPR:
12205 : 5781574 : tmp = DECL_EXPR_DECL (t);
12206 : 4638299 : if (VAR_P (tmp) && !DECL_ARTIFICIAL (tmp)
12207 : 8708516 : && (processing_template_decl
12208 : 2552514 : ? !decl_maybe_constant_var_p (tmp)
12209 : 2178086 : : !decl_constant_var_p (tmp)))
12210 : : {
12211 : 2233498 : if (CP_DECL_THREAD_LOCAL_P (tmp) && !DECL_REALLY_EXTERN (tmp))
12212 : : {
12213 : 5 : if (flags & tf_error)
12214 : 2 : constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p,
12215 : : "%qD defined %<thread_local%> in "
12216 : : "%<constexpr%> context", tmp);
12217 : 5 : return false;
12218 : : }
12219 : 2233493 : else if (TREE_STATIC (tmp))
12220 : : {
12221 : 81 : if (flags & tf_error)
12222 : 15 : constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p,
12223 : : "%qD defined %<static%> in %<constexpr%> "
12224 : : "context", tmp);
12225 : 81 : return false;
12226 : : }
12227 : 2233412 : else if (!check_for_uninitialized_const_var
12228 : 2233412 : (tmp, /*constexpr_context_p=*/true, flags))
12229 : : return false;
12230 : : }
12231 : 5777719 : if (VAR_P (tmp))
12232 : 4634444 : return RECUR (DECL_INITIAL (tmp), want_rval);
12233 : : return true;
12234 : :
12235 : 25917 : case TRY_FINALLY_EXPR:
12236 : 25917 : return (RECUR (TREE_OPERAND (t, 0), want_rval)
12237 : 25917 : && RECUR (TREE_OPERAND (t, 1), any));
12238 : :
12239 : 20229036 : case SCOPE_REF:
12240 : 20229036 : return RECUR (TREE_OPERAND (t, 1), want_rval);
12241 : :
12242 : 22537061 : case TARGET_EXPR:
12243 : 22537061 : if (!TARGET_EXPR_DIRECT_INIT_P (t)
12244 : 22477696 : && !TARGET_EXPR_ELIDING_P (t)
12245 : 39997282 : && !literal_type_p (TREE_TYPE (t)))
12246 : : {
12247 : 1334003 : if (flags & tf_error)
12248 : : {
12249 : 21 : auto_diagnostic_group d;
12250 : 21 : if (constexpr_error (loc, fundef_p,
12251 : : "temporary of non-literal type %qT in a "
12252 : 21 : "constant expression", TREE_TYPE (t)))
12253 : 21 : explain_non_literal_class (TREE_TYPE (t));
12254 : 21 : }
12255 : 1334003 : return false;
12256 : : }
12257 : : /* FALLTHRU */
12258 : 38315542 : case INIT_EXPR:
12259 : 38315542 : if (TREE_CLOBBER_P (TREE_OPERAND (t, 1)))
12260 : : return true;
12261 : 38299115 : return RECUR (TREE_OPERAND (t, 1), rval);
12262 : :
12263 : 27759994 : case CONSTRUCTOR:
12264 : 27759994 : {
12265 : 27759994 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
12266 : 27759994 : constructor_elt *ce;
12267 : 117544111 : for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
12268 : 90634652 : if (!RECUR (ce->value, want_rval))
12269 : : return false;
12270 : : return true;
12271 : : }
12272 : :
12273 : 20559566 : case TREE_LIST:
12274 : 20559566 : {
12275 : 20559566 : gcc_assert (TREE_PURPOSE (t) == NULL_TREE
12276 : : || DECL_P (TREE_PURPOSE (t)));
12277 : 20559566 : if (!RECUR (TREE_VALUE (t), want_rval))
12278 : : return false;
12279 : 18351658 : if (TREE_CHAIN (t) == NULL_TREE)
12280 : : return true;
12281 : 53273 : return RECUR (TREE_CHAIN (t), want_rval);
12282 : : }
12283 : :
12284 : 9963068 : case TRUNC_DIV_EXPR:
12285 : 9963068 : case CEIL_DIV_EXPR:
12286 : 9963068 : case FLOOR_DIV_EXPR:
12287 : 9963068 : case ROUND_DIV_EXPR:
12288 : 9963068 : case TRUNC_MOD_EXPR:
12289 : 9963068 : case CEIL_MOD_EXPR:
12290 : 9963068 : case ROUND_MOD_EXPR:
12291 : 9963068 : {
12292 : 9963068 : tree denom = TREE_OPERAND (t, 1);
12293 : 9963068 : if (!RECUR (denom, rval))
12294 : : return false;
12295 : : /* We can't call cxx_eval_outermost_constant_expr on an expression
12296 : : that hasn't been through instantiate_non_dependent_expr yet. */
12297 : 9478088 : if (!processing_template_decl)
12298 : 3991419 : denom = cxx_eval_outermost_constant_expr (denom, true);
12299 : 9478088 : if (integer_zerop (denom))
12300 : : {
12301 : 335 : if (flags & tf_error)
12302 : 35 : constexpr_error (input_location, fundef_p,
12303 : : "division by zero is not a constant expression");
12304 : 335 : return false;
12305 : : }
12306 : : else
12307 : : {
12308 : 9477753 : want_rval = true;
12309 : 9477753 : return RECUR (TREE_OPERAND (t, 0), want_rval);
12310 : : }
12311 : : }
12312 : :
12313 : 4495252 : case COMPOUND_EXPR:
12314 : 4495252 : {
12315 : : /* check_return_expr sometimes wraps a TARGET_EXPR in a
12316 : : COMPOUND_EXPR; don't get confused. */
12317 : 4495252 : tree op0 = TREE_OPERAND (t, 0);
12318 : 4495252 : tree op1 = TREE_OPERAND (t, 1);
12319 : 4495252 : STRIP_NOPS (op1);
12320 : 4495252 : if (TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
12321 : 542065 : return RECUR (op0, want_rval);
12322 : : else
12323 : 3953187 : goto binary;
12324 : : }
12325 : :
12326 : : /* If the first operand is the non-short-circuit constant, look at
12327 : : the second operand; otherwise we only care about the first one for
12328 : : potentiality. */
12329 : 15501576 : case TRUTH_AND_EXPR:
12330 : 15501576 : case TRUTH_ANDIF_EXPR:
12331 : 15501576 : tmp = boolean_true_node;
12332 : 15501576 : goto truth;
12333 : 6218738 : case TRUTH_OR_EXPR:
12334 : 6218738 : case TRUTH_ORIF_EXPR:
12335 : 6218738 : tmp = boolean_false_node;
12336 : 21720314 : truth:
12337 : 21720314 : {
12338 : 21720314 : tree op0 = TREE_OPERAND (t, 0);
12339 : 21720314 : tree op1 = TREE_OPERAND (t, 1);
12340 : 21720314 : if (!RECUR (op0, rval))
12341 : : return false;
12342 : 14034611 : if (!(flags & tf_error) && RECUR (op1, rval))
12343 : : /* When quiet, try to avoid expensive trial evaluation by first
12344 : : checking potentiality of the second operand. */
12345 : : return true;
12346 : 1292218 : if (!processing_template_decl)
12347 : 922131 : op0 = cxx_eval_outermost_constant_expr (op0, true);
12348 : 1292218 : if (tree_int_cst_equal (op0, tmp))
12349 : 5349 : return (flags & tf_error) ? RECUR (op1, rval) : false;
12350 : : else
12351 : : return true;
12352 : : }
12353 : :
12354 : : case PLUS_EXPR:
12355 : : case MULT_EXPR:
12356 : : case POINTER_PLUS_EXPR:
12357 : : case RDIV_EXPR:
12358 : : case EXACT_DIV_EXPR:
12359 : : case MIN_EXPR:
12360 : : case MAX_EXPR:
12361 : : case LSHIFT_EXPR:
12362 : : case RSHIFT_EXPR:
12363 : : case LROTATE_EXPR:
12364 : : case RROTATE_EXPR:
12365 : : case BIT_IOR_EXPR:
12366 : : case BIT_XOR_EXPR:
12367 : : case BIT_AND_EXPR:
12368 : : case TRUTH_XOR_EXPR:
12369 : : case UNORDERED_EXPR:
12370 : : case ORDERED_EXPR:
12371 : : case UNLT_EXPR:
12372 : : case UNLE_EXPR:
12373 : : case UNGT_EXPR:
12374 : : case UNGE_EXPR:
12375 : : case UNEQ_EXPR:
12376 : : case LTGT_EXPR:
12377 : : case RANGE_EXPR:
12378 : : case COMPLEX_EXPR:
12379 : 191079826 : want_rval = true;
12380 : : /* Fall through. */
12381 : 191079826 : case ARRAY_REF:
12382 : 191079826 : case ARRAY_RANGE_REF:
12383 : 191079826 : case MEMBER_REF:
12384 : 191079826 : case DOTSTAR_EXPR:
12385 : 191079826 : case MEM_REF:
12386 : 191079826 : case BINARY_LEFT_FOLD_EXPR:
12387 : 191079826 : case BINARY_RIGHT_FOLD_EXPR:
12388 : 191079826 : binary:
12389 : 401733850 : for (i = 0; i < 2; ++i)
12390 : 302501382 : if (!RECUR (TREE_OPERAND (t, i), want_rval))
12391 : : return false;
12392 : : return true;
12393 : :
12394 : : case VEC_PERM_EXPR:
12395 : 91287 : for (i = 0; i < 3; ++i)
12396 : 91253 : if (!RECUR (TREE_OPERAND (t, i), true))
12397 : : return false;
12398 : : return true;
12399 : :
12400 : 4620748 : case COND_EXPR:
12401 : 4620748 : if (COND_EXPR_IS_VEC_DELETE (t) && cxx_dialect < cxx20)
12402 : : {
12403 : 8 : if (flags & tf_error)
12404 : 2 : constexpr_error (loc, fundef_p, "%<delete[]%> is not a "
12405 : : "constant expression");
12406 : 8 : return false;
12407 : : }
12408 : : /* Fall through. */
12409 : 9337293 : case IF_STMT:
12410 : 9337293 : case VEC_COND_EXPR:
12411 : : /* If the condition is a known constant, we know which of the legs we
12412 : : care about; otherwise we only require that the condition and
12413 : : either of the legs be potentially constant. */
12414 : 9337293 : tmp = TREE_OPERAND (t, 0);
12415 : 9337293 : if (!RECUR (tmp, rval))
12416 : : return false;
12417 : 8052540 : if (!processing_template_decl)
12418 : 6921285 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
12419 : : /* potential_constant_expression* isn't told if it is called for
12420 : : manifestly_const_eval or not, so for consteval if always
12421 : : process both branches as if the condition is not a known
12422 : : constant. */
12423 : 8052540 : if (TREE_CODE (t) != IF_STMT || !IF_STMT_CONSTEVAL_P (t))
12424 : : {
12425 : 8035519 : if (integer_zerop (tmp))
12426 : 2186976 : return RECUR (TREE_OPERAND (t, 2), want_rval);
12427 : 5848543 : else if (TREE_CODE (tmp) == INTEGER_CST)
12428 : 2023221 : return RECUR (TREE_OPERAND (t, 1), want_rval);
12429 : : }
12430 : 3842343 : tmp = *jump_target;
12431 : 4478756 : for (i = 1; i < 3; ++i)
12432 : : {
12433 : 4374284 : tree this_jump_target = tmp;
12434 : 4374284 : if (potential_constant_expression_1 (TREE_OPERAND (t, i),
12435 : : want_rval, strict, now, fundef_p,
12436 : : tf_none, &this_jump_target))
12437 : : {
12438 : 3943622 : if (returns (&this_jump_target) || throws (&this_jump_target))
12439 : 1128302 : *jump_target = this_jump_target;
12440 : 2609569 : else if (!returns (jump_target) && !throws (jump_target))
12441 : : {
12442 : 2609569 : if (breaks (&this_jump_target)
12443 : 2609569 : || continues (&this_jump_target))
12444 : 42 : *jump_target = this_jump_target;
12445 : 2609569 : if (i == 1)
12446 : : {
12447 : : /* If the then branch is potentially constant, but
12448 : : does not return, check if the else branch
12449 : : couldn't return, break or continue. */
12450 : 2186760 : hash_set<tree> pset;
12451 : 2186760 : check_for_return_continue_data data = { &pset, NULL_TREE,
12452 : : NULL_TREE,
12453 : 2186760 : false };
12454 : 4373520 : if (tree ret_expr
12455 : 2186760 : = cp_walk_tree (&TREE_OPERAND (t, 2),
12456 : : check_for_return_continue, &data,
12457 : : &pset))
12458 : 18303 : *jump_target = ret_expr;
12459 : 2168457 : else if (*jump_target == NULL_TREE)
12460 : : {
12461 : 2168418 : if (data.continue_stmt)
12462 : 0 : *jump_target = data.continue_stmt;
12463 : 2168418 : else if (data.break_stmt)
12464 : 5 : *jump_target = data.break_stmt;
12465 : : }
12466 : 2186760 : if (data.could_throw)
12467 : 39910 : *jump_target = void_node;
12468 : 2186760 : }
12469 : : }
12470 : 3737871 : return true;
12471 : : }
12472 : : }
12473 : 104472 : if (flags & tf_error)
12474 : : {
12475 : 3 : if (TREE_CODE (t) == IF_STMT)
12476 : 3 : constexpr_error (loc, fundef_p, "neither branch of %<if%> is a "
12477 : : "constant expression");
12478 : : else
12479 : 0 : constexpr_error (loc, fundef_p, "expression %qE is not a "
12480 : : "constant expression", t);
12481 : : }
12482 : : return false;
12483 : :
12484 : 988 : case VEC_INIT_EXPR:
12485 : 988 : if (VEC_INIT_EXPR_IS_CONSTEXPR (t))
12486 : : return true;
12487 : 411 : if (flags & tf_error)
12488 : : {
12489 : 3 : if (constexpr_error (loc, fundef_p, "non-constant array "
12490 : : "initialization"))
12491 : 3 : diagnose_non_constexpr_vec_init (t);
12492 : : }
12493 : : return false;
12494 : :
12495 : : case TYPE_DECL:
12496 : : case TAG_DEFN:
12497 : : /* We can see these in statement-expressions. */
12498 : : return true;
12499 : :
12500 : 491294 : case CLEANUP_STMT:
12501 : 491294 : if (!RECUR (CLEANUP_BODY (t), any))
12502 : : return false;
12503 : 491014 : if (!CLEANUP_EH_ONLY (t) && !RECUR (CLEANUP_EXPR (t), any))
12504 : : return false;
12505 : : return true;
12506 : :
12507 : : case EMPTY_CLASS_EXPR:
12508 : : return true;
12509 : :
12510 : 22 : case GOTO_EXPR:
12511 : 22 : {
12512 : 22 : tree *target = &TREE_OPERAND (t, 0);
12513 : : /* Gotos representing break, continue and cdtor return are OK. */
12514 : 22 : if (breaks (target) || continues (target) || returns (target))
12515 : : {
12516 : 9 : *jump_target = *target;
12517 : 9 : return true;
12518 : : }
12519 : 13 : if (DECL_ARTIFICIAL (*target))
12520 : : /* The user didn't write this goto, this isn't the problem. */
12521 : : return true;
12522 : 11 : if (flags & tf_error)
12523 : 2 : constexpr_error (loc, fundef_p, "%<goto%> is not a constant "
12524 : : "expression");
12525 : : return false;
12526 : : }
12527 : :
12528 : 48 : case ASSERTION_STMT:
12529 : 48 : case PRECONDITION_STMT:
12530 : 48 : case POSTCONDITION_STMT:
12531 : 48 : if (!checked_contract_p (get_contract_semantic (t)))
12532 : : return true;
12533 : 33 : return RECUR (CONTRACT_CONDITION (t), rval);
12534 : :
12535 : 224 : case LABEL_EXPR:
12536 : 224 : t = LABEL_EXPR_LABEL (t);
12537 : 224 : if (DECL_ARTIFICIAL (t) || cxx_dialect >= cxx23)
12538 : : return true;
12539 : 29 : else if (flags & tf_error)
12540 : 6 : constexpr_error (loc, fundef_p, "label definition in %<constexpr%> "
12541 : : "function only available with %<-std=c++23%> or "
12542 : : "%<-std=gnu++23%>");
12543 : : return false;
12544 : :
12545 : 2744 : case ANNOTATE_EXPR:
12546 : 2744 : return RECUR (TREE_OPERAND (t, 0), rval);
12547 : :
12548 : 22132 : case BIT_CAST_EXPR:
12549 : 22132 : return RECUR (TREE_OPERAND (t, 0), rval);
12550 : :
12551 : : /* Coroutine await, yield and return expressions are not. */
12552 : 682 : case CO_AWAIT_EXPR:
12553 : 682 : case CO_YIELD_EXPR:
12554 : 682 : case CO_RETURN_EXPR:
12555 : 682 : case TEMPLATE_FOR_STMT:
12556 : 682 : if (flags & tf_error)
12557 : 3 : constexpr_error (cp_expr_loc_or_loc (t, input_location), fundef_p,
12558 : : "%qE is not a constant expression", t);
12559 : : return false;
12560 : :
12561 : : /* Assume a TU-local entity is not constant, we'll error later when
12562 : : instantiating. */
12563 : : case TU_LOCAL_ENTITY:
12564 : : return false;
12565 : :
12566 : 30 : case NONTYPE_ARGUMENT_PACK:
12567 : 30 : {
12568 : 30 : tree args = ARGUMENT_PACK_ARGS (t);
12569 : 30 : int len = TREE_VEC_LENGTH (args);
12570 : 90 : for (int i = 0; i < len; ++i)
12571 : 60 : if (!RECUR (TREE_VEC_ELT (args, i), any))
12572 : : return false;
12573 : : return true;
12574 : : }
12575 : :
12576 : 0 : default:
12577 : 0 : if (objc_non_constant_expr_p (t))
12578 : : return false;
12579 : :
12580 : 0 : sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t)));
12581 : 0 : gcc_unreachable ();
12582 : : return false;
12583 : : }
12584 : : #undef RECUR
12585 : : }
12586 : :
12587 : : bool
12588 : 1159932809 : potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
12589 : : bool fundef_p, tsubst_flags_t flags)
12590 : : {
12591 : 1159932809 : if (flags & tf_error)
12592 : : {
12593 : : /* Check potentiality quietly first, as that could be performed more
12594 : : efficiently in some cases (currently only for TRUTH_*_EXPR). If
12595 : : that fails, replay the check noisily to give errors. */
12596 : 5854548 : flags &= ~tf_error;
12597 : 5854548 : if (potential_constant_expression_1 (t, want_rval, strict, now, fundef_p,
12598 : : flags))
12599 : : return true;
12600 : 888 : flags |= tf_error;
12601 : : }
12602 : :
12603 : 1154079149 : tree target = NULL_TREE;
12604 : 1154079149 : return potential_constant_expression_1 (t, want_rval, strict, now, fundef_p,
12605 : 1154079149 : flags, &target);
12606 : : }
12607 : :
12608 : : /* The main entry point to the above. */
12609 : :
12610 : : bool
12611 : 358823191 : potential_constant_expression (tree t)
12612 : : {
12613 : 358823191 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
12614 : : /*now*/false, /*fundef_p*/false,
12615 : 358823191 : tf_none);
12616 : : }
12617 : :
12618 : : /* As above, but require a constant rvalue. */
12619 : :
12620 : : bool
12621 : 21100329 : potential_rvalue_constant_expression (tree t)
12622 : : {
12623 : 21100329 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
12624 : : /*now*/false, /*fundef_p*/false,
12625 : 21100329 : tf_none);
12626 : : }
12627 : :
12628 : : /* Like above, but complain about non-constant expressions. */
12629 : :
12630 : : bool
12631 : 73 : require_potential_constant_expression (tree t)
12632 : : {
12633 : 73 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
12634 : : /*now*/false, /*fundef_p*/false,
12635 : 73 : tf_warning_or_error);
12636 : : }
12637 : :
12638 : : /* Cross product of the above. */
12639 : :
12640 : : bool
12641 : 76923 : require_potential_rvalue_constant_expression (tree t)
12642 : : {
12643 : 76923 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
12644 : : /*now*/false, /*fundef_p*/false,
12645 : 76923 : tf_warning_or_error);
12646 : : }
12647 : :
12648 : : /* Like require_potential_rvalue_constant_expression, but fundef_p is true. */
12649 : :
12650 : : bool
12651 : 206 : require_potential_rvalue_constant_expression_fncheck (tree t)
12652 : : {
12653 : 206 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
12654 : : /*now*/false, /*fundef_p*/true,
12655 : 206 : tf_warning_or_error);
12656 : : }
12657 : :
12658 : : /* Like above, but don't consider PARM_DECL a potential_constant_expression. */
12659 : :
12660 : : bool
12661 : 520 : require_rvalue_constant_expression (tree t)
12662 : : {
12663 : 520 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
12664 : : /*now*/true, /*fundef_p*/false,
12665 : 520 : tf_warning_or_error);
12666 : : }
12667 : :
12668 : : /* Like potential_constant_expression, but don't consider possible constexpr
12669 : : substitution of the current function. That is, PARM_DECL qualifies under
12670 : : potential_constant_expression, but not here.
12671 : :
12672 : : This is basically what you can check when any actual constant values might
12673 : : be value-dependent. */
12674 : :
12675 : : bool
12676 : 597522539 : is_constant_expression (tree t)
12677 : : {
12678 : 597522539 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
12679 : : /*now*/true, /*fundef_p*/false,
12680 : 597522539 : tf_none);
12681 : : }
12682 : :
12683 : : /* As above, but expect an rvalue. */
12684 : :
12685 : : bool
12686 : 106729580 : is_rvalue_constant_expression (tree t)
12687 : : {
12688 : 106729580 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
12689 : : /*now*/true, /*fundef_p*/false,
12690 : 106729580 : tf_none);
12691 : : }
12692 : :
12693 : : /* Like above, but complain about non-constant expressions. */
12694 : :
12695 : : bool
12696 : 5776826 : require_constant_expression (tree t)
12697 : : {
12698 : 5776826 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
12699 : : /*now*/true, /*fundef_p*/false,
12700 : 5776826 : tf_warning_or_error);
12701 : : }
12702 : :
12703 : : /* Like is_constant_expression, but allow const variables that are not allowed
12704 : : under constexpr rules. */
12705 : :
12706 : : bool
12707 : 64048074 : is_static_init_expression (tree t)
12708 : : {
12709 : 64048074 : return potential_constant_expression_1 (t, /*want_rval*/false,
12710 : : /*strict*/false, /*now*/true,
12711 : 64048074 : /*fundef_p*/false, tf_none);
12712 : : }
12713 : :
12714 : : /* Returns true if T is a potential constant expression that is not
12715 : : instantiation-dependent, and therefore a candidate for constant folding even
12716 : : in a template. */
12717 : :
12718 : : bool
12719 : 579823918 : is_nondependent_constant_expression (tree t)
12720 : : {
12721 : 579823918 : return (!type_unknown_p (t)
12722 : 579823869 : && is_constant_expression (t)
12723 : 1056242310 : && !instantiation_dependent_expression_p (t));
12724 : : }
12725 : :
12726 : : /* Returns true if T is a potential static initializer expression that is not
12727 : : instantiation-dependent. */
12728 : :
12729 : : bool
12730 : 64048074 : is_nondependent_static_init_expression (tree t)
12731 : : {
12732 : 64048074 : return (!type_unknown_p (t)
12733 : 64048074 : && is_static_init_expression (t)
12734 : 119492704 : && !instantiation_dependent_expression_p (t));
12735 : : }
12736 : :
12737 : : /* True iff FN is an implicitly constexpr function. */
12738 : :
12739 : : bool
12740 : 160655 : decl_implicit_constexpr_p (tree fn)
12741 : : {
12742 : 160655 : if (!(flag_implicit_constexpr
12743 : 0 : && TREE_CODE (fn) == FUNCTION_DECL
12744 : 0 : && DECL_DECLARED_CONSTEXPR_P (fn)))
12745 : : return false;
12746 : :
12747 : 0 : if (DECL_CLONED_FUNCTION_P (fn))
12748 : 0 : fn = DECL_CLONED_FUNCTION (fn);
12749 : :
12750 : 0 : return (DECL_LANG_SPECIFIC (fn)
12751 : 0 : && DECL_LANG_SPECIFIC (fn)->u.fn.implicit_constexpr);
12752 : : }
12753 : :
12754 : : /* Finalize constexpr processing after parsing. */
12755 : :
12756 : : void
12757 : 94835 : fini_constexpr (void)
12758 : : {
12759 : : /* The contexpr call and fundef copies tables are no longer needed. */
12760 : 94835 : constexpr_call_table = NULL;
12761 : 94835 : fundef_copies_table = NULL;
12762 : 94835 : }
12763 : :
12764 : : #include "gt-cp-constexpr.h"
|