Branch data Line data Source code
1 : : /* Perform -*- C++ -*- constant expression evaluation, including calls to
2 : : constexpr functions. These routines are used both during actual parsing
3 : : and during the instantiation of template functions.
4 : :
5 : : Copyright (C) 1998-2025 Free Software Foundation, Inc.
6 : :
7 : : This file is part of GCC.
8 : :
9 : : GCC is free software; you can redistribute it and/or modify it
10 : : under the terms of the GNU General Public License as published by
11 : : the Free Software Foundation; either version 3, or (at your option)
12 : : any later version.
13 : :
14 : : GCC is distributed in the hope that it will be useful, but
15 : : WITHOUT ANY WARRANTY; without even the implied warranty of
16 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 : : General Public License for more details.
18 : :
19 : : You should have received a copy of the GNU General Public License
20 : : along with GCC; see the file COPYING3. If not see
21 : : <http://www.gnu.org/licenses/>. */
22 : :
23 : : #include "config.h"
24 : : #include "system.h"
25 : : #include "coretypes.h"
26 : : #include "cp-tree.h"
27 : : #include "varasm.h"
28 : : #include "c-family/c-objc.h"
29 : : #include "tree-iterator.h"
30 : : #include "gimplify.h"
31 : : #include "builtins.h"
32 : : #include "tree-inline.h"
33 : : #include "ubsan.h"
34 : : #include "timevar.h"
35 : : #include "fold-const-call.h"
36 : : #include "stor-layout.h"
37 : : #include "cgraph.h"
38 : : #include "opts.h"
39 : : #include "stringpool.h"
40 : : #include "attribs.h"
41 : : #include "fold-const.h"
42 : : #include "intl.h"
43 : : #include "toplev.h"
44 : :
45 : : static bool verify_constant (tree, bool, bool *, bool *);
46 : : #define VERIFY_CONSTANT(X) \
47 : : do { \
48 : : if (verify_constant ((X), ctx->quiet, non_constant_p, overflow_p)) \
49 : : return t; \
50 : : } while (0)
51 : :
52 : : static HOST_WIDE_INT find_array_ctor_elt (tree ary, tree dindex,
53 : : bool insert = false);
54 : : static int array_index_cmp (tree key, tree index);
55 : :
56 : : /* Returns true iff FUN is an instantiation of a constexpr function
57 : : template or a defaulted constexpr function. */
58 : :
59 : : bool
60 : 68787400 : is_instantiation_of_constexpr (tree fun)
61 : : {
62 : 91430265 : return ((DECL_TEMPLOID_INSTANTIATION (fun)
63 : 46405425 : && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun)))
64 : 100432111 : || (DECL_DEFAULTED_FN (fun)
65 : 1507247 : && DECL_DECLARED_CONSTEXPR_P (fun)));
66 : : }
67 : :
68 : : /* Return true if T is a literal type. */
69 : :
70 : : bool
71 : 99748291 : literal_type_p (tree t)
72 : : {
73 : 97299107 : if (SCALAR_TYPE_P (t)
74 : 40565690 : || VECTOR_TYPE_P (t)
75 : 40553929 : || TYPE_REF_P (t)
76 : 133011489 : || (VOID_TYPE_P (t) && cxx_dialect >= cxx14))
77 : : return true;
78 : 30655898 : if (CLASS_TYPE_P (t))
79 : : {
80 : 29434196 : t = complete_type (t);
81 : 29434196 : gcc_assert (COMPLETE_TYPE_P (t) || errorcount);
82 : 29434196 : return CLASSTYPE_LITERAL_P (t);
83 : : }
84 : 1221702 : if (TREE_CODE (t) == ARRAY_TYPE)
85 : 1221528 : return literal_type_p (strip_array_types (t));
86 : : return false;
87 : : }
88 : :
89 : : /* If DECL is a variable declared `constexpr', require its type
90 : : be literal. Return error_mark_node if we give an error, the
91 : : DECL otherwise. */
92 : :
93 : : tree
94 : 270694455 : ensure_literal_type_for_constexpr_object (tree decl)
95 : : {
96 : 270694455 : tree type = TREE_TYPE (decl);
97 : 270694455 : if (VAR_P (decl)
98 : 95178779 : && (DECL_DECLARED_CONSTEXPR_P (decl)
99 : 75342835 : || var_in_constexpr_fn (decl))
100 : 297677540 : && !processing_template_decl)
101 : : {
102 : 17840122 : tree stype = strip_array_types (type);
103 : 17840122 : if (CLASS_TYPE_P (stype) && !COMPLETE_TYPE_P (complete_type (stype)))
104 : : /* Don't complain here, we'll complain about incompleteness
105 : : when we try to initialize the variable. */;
106 : 17840116 : else if (!literal_type_p (type))
107 : : {
108 : 12038 : if (DECL_DECLARED_CONSTEXPR_P (decl))
109 : : {
110 : 42 : auto_diagnostic_group d;
111 : 42 : error_at (DECL_SOURCE_LOCATION (decl),
112 : : "the type %qT of %<constexpr%> variable %qD "
113 : : "is not literal", type, decl);
114 : 42 : explain_non_literal_class (type);
115 : 42 : decl = error_mark_node;
116 : 42 : }
117 : 11996 : else if (cxx_dialect < cxx23)
118 : : {
119 : 2491 : if (!is_instantiation_of_constexpr (current_function_decl))
120 : : {
121 : 8 : auto_diagnostic_group d;
122 : 8 : error_at (DECL_SOURCE_LOCATION (decl),
123 : : "variable %qD of non-literal type %qT in "
124 : : "%<constexpr%> function only available with "
125 : : "%<-std=c++23%> or %<-std=gnu++23%>", decl, type);
126 : 8 : explain_non_literal_class (type);
127 : 8 : decl = error_mark_node;
128 : 8 : }
129 : 2491 : cp_function_chain->invalid_constexpr = true;
130 : : }
131 : : }
132 : 17828078 : else if (DECL_DECLARED_CONSTEXPR_P (decl)
133 : 17828078 : && variably_modified_type_p (type, NULL_TREE))
134 : : {
135 : 4 : error_at (DECL_SOURCE_LOCATION (decl),
136 : : "%<constexpr%> variable %qD has variably-modified "
137 : : "type %qT", decl, type);
138 : 4 : decl = error_mark_node;
139 : : }
140 : : }
141 : 270694455 : return decl;
142 : : }
143 : :
144 : : /* Issue a diagnostic with text GMSGID for constructs that are invalid in
145 : : constexpr functions. CONSTEXPR_FUNDEF_P is true if we're checking
146 : : a constexpr function body; if so, don't report hard errors and issue
147 : : a pedwarn pre-C++23, or a warning in C++23, if requested by
148 : : -Winvalid-constexpr. Otherwise, we're not in the context where we are
149 : : checking if a function can be marked 'constexpr', so give a hard error. */
150 : :
151 : : ATTRIBUTE_GCC_DIAG(3,4)
152 : : static bool
153 : 795 : constexpr_error (location_t location, bool constexpr_fundef_p,
154 : : const char *gmsgid, ...)
155 : : {
156 : 795 : diagnostic_info diagnostic;
157 : 795 : va_list ap;
158 : 795 : rich_location richloc (line_table, location);
159 : 795 : va_start (ap, gmsgid);
160 : 795 : bool ret;
161 : 795 : if (!constexpr_fundef_p)
162 : : {
163 : : /* Report an error that cannot be suppressed. */
164 : 555 : diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_ERROR);
165 : 555 : ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
166 : : }
167 : 240 : else if (warn_invalid_constexpr)
168 : : {
169 : 182 : diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
170 : 182 : cxx_dialect < cxx23 ? DK_PEDWARN : DK_WARNING);
171 : 182 : diagnostic.option_id = OPT_Winvalid_constexpr;
172 : 182 : ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
173 : : }
174 : : else
175 : : ret = false;
176 : 795 : va_end (ap);
177 : 1590 : return ret;
178 : 795 : }
179 : :
180 : : struct constexpr_fundef_hasher : ggc_ptr_hash<constexpr_fundef>
181 : : {
182 : : static hashval_t hash (const constexpr_fundef *);
183 : : static bool equal (const constexpr_fundef *, const constexpr_fundef *);
184 : : };
185 : :
186 : : /* This table holds all constexpr function definitions seen in
187 : : the current translation unit. */
188 : :
189 : : static GTY (()) hash_table<constexpr_fundef_hasher> *constexpr_fundef_table;
190 : :
191 : : /* Utility function used for managing the constexpr function table.
192 : : Return true if the entries pointed to by P and Q are for the
193 : : same constexpr function. */
194 : :
195 : : inline bool
196 : 394966717 : constexpr_fundef_hasher::equal (const constexpr_fundef *lhs,
197 : : const constexpr_fundef *rhs)
198 : : {
199 : 379269671 : return lhs->decl == rhs->decl;
200 : : }
201 : :
202 : : /* Utility function used for managing the constexpr function table.
203 : : Return a hash value for the entry pointed to by Q. */
204 : :
205 : : inline hashval_t
206 : 391082404 : constexpr_fundef_hasher::hash (const constexpr_fundef *fundef)
207 : : {
208 : 391082404 : return DECL_UID (fundef->decl);
209 : : }
210 : :
211 : : /* Return a previously saved definition of function FUN. */
212 : :
213 : : constexpr_fundef *
214 : 38907043 : retrieve_constexpr_fundef (tree fun)
215 : : {
216 : 38907043 : if (constexpr_fundef_table == NULL)
217 : : return NULL;
218 : :
219 : 38903740 : constexpr_fundef fundef = { fun, NULL_TREE, NULL_TREE, NULL_TREE };
220 : 38903740 : return constexpr_fundef_table->find (&fundef);
221 : : }
222 : :
223 : : /* Check whether the parameter and return types of FUN are valid for a
224 : : constexpr function, and complain if COMPLAIN. */
225 : :
226 : : bool
227 : 18041455 : is_valid_constexpr_fn (tree fun, bool complain)
228 : : {
229 : 18041455 : bool ret = true;
230 : :
231 : 36082910 : if (DECL_INHERITED_CTOR (fun)
232 : 2003011 : && TREE_CODE (fun) == TEMPLATE_DECL)
233 : : {
234 : 0 : ret = false;
235 : 0 : if (complain)
236 : 0 : error ("inherited constructor %qD is not %<constexpr%>",
237 : 0 : DECL_INHERITED_CTOR (fun));
238 : : }
239 : : else
240 : : {
241 : 18041455 : for (tree parm = FUNCTION_FIRST_USER_PARM (fun);
242 : 35413433 : parm != NULL_TREE; parm = TREE_CHAIN (parm))
243 : 17371978 : if (!literal_type_p (TREE_TYPE (parm)))
244 : : {
245 : 15887 : ret = false;
246 : 15887 : if (complain)
247 : : {
248 : 22 : auto_diagnostic_group d;
249 : 22 : if (constexpr_error (input_location, /*constexpr_fundef_p*/true,
250 : : "invalid type for parameter %d of "
251 : : "%<constexpr%> function %q+#D",
252 : 22 : DECL_PARM_INDEX (parm), fun))
253 : 20 : explain_non_literal_class (TREE_TYPE (parm));
254 : 22 : }
255 : : }
256 : : }
257 : :
258 : 28790527 : if (LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun)) && cxx_dialect < cxx17)
259 : : {
260 : 2 : ret = false;
261 : 2 : if (complain)
262 : 2 : inform (DECL_SOURCE_LOCATION (fun),
263 : : "lambdas are implicitly %<constexpr%> only in C++17 and later");
264 : : }
265 : 36082906 : else if (DECL_DESTRUCTOR_P (fun) && cxx_dialect < cxx20)
266 : : {
267 : 96 : ret = false;
268 : 96 : if (complain)
269 : 0 : error_at (DECL_SOURCE_LOCATION (fun),
270 : : "%<constexpr%> destructors only available with "
271 : : "%<-std=c++20%> or %<-std=gnu++20%>");
272 : : }
273 : 18041357 : else if (!DECL_CONSTRUCTOR_P (fun) && !DECL_DESTRUCTOR_P (fun))
274 : : {
275 : 15856587 : tree rettype = TREE_TYPE (TREE_TYPE (fun));
276 : 15856587 : if (!literal_type_p (rettype))
277 : : {
278 : 56296 : ret = false;
279 : 56296 : if (complain)
280 : : {
281 : 12 : auto_diagnostic_group d;
282 : 12 : if (constexpr_error (input_location, /*constexpr_fundef_p*/true,
283 : : "invalid return type %qT of %<constexpr%> "
284 : : "function %q+D", rettype, fun))
285 : 12 : explain_non_literal_class (rettype);
286 : 12 : }
287 : : }
288 : :
289 : : /* C++14 DR 1684 removed this restriction. */
290 : 15856587 : if (cxx_dialect < cxx14
291 : 31219 : && DECL_IOBJ_MEMBER_FUNCTION_P (fun)
292 : 15857636 : && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun)))
293 : : {
294 : 1 : ret = false;
295 : 1 : if (complain)
296 : : {
297 : 1 : auto_diagnostic_group d;
298 : 1 : if (pedwarn (DECL_SOURCE_LOCATION (fun), OPT_Wpedantic,
299 : : "enclosing class of %<constexpr%> non-static"
300 : : " member function %q+#D is not a literal type",
301 : : fun))
302 : 1 : explain_non_literal_class (DECL_CONTEXT (fun));
303 : 1 : }
304 : : }
305 : : }
306 : 2184770 : else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun)))
307 : : {
308 : 105 : ret = false;
309 : 105 : if (complain)
310 : : {
311 : 11 : if (DECL_CONSTRUCTOR_P (fun))
312 : 9 : error ("%<constexpr%> constructor in %q#T that has "
313 : 9 : "virtual base classes", DECL_CONTEXT (fun));
314 : : else
315 : 2 : error ("%<constexpr%> destructor in %q#T that has "
316 : 2 : "virtual base classes", DECL_CONTEXT (fun));
317 : : }
318 : : }
319 : :
320 : 18041455 : return ret;
321 : : }
322 : :
323 : : /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF
324 : : for a member of an anonymous aggregate, INIT is the initializer for that
325 : : member, and VEC_OUTER is the vector of constructor elements for the class
326 : : whose constructor we are processing. Add the initializer to the vector
327 : : and return true to indicate success. */
328 : :
329 : : static bool
330 : 862 : build_anon_member_initialization (tree member, tree init,
331 : : vec<constructor_elt, va_gc> **vec_outer)
332 : : {
333 : : /* MEMBER presents the relevant fields from the inside out, but we need
334 : : to build up the initializer from the outside in so that we can reuse
335 : : previously built CONSTRUCTORs if this is, say, the second field in an
336 : : anonymous struct. So we use a vec as a stack. */
337 : 862 : auto_vec<tree, 2> fields;
338 : 1820 : do
339 : : {
340 : 1820 : fields.safe_push (TREE_OPERAND (member, 1));
341 : 1820 : member = TREE_OPERAND (member, 0);
342 : : }
343 : 1820 : while (ANON_AGGR_TYPE_P (TREE_TYPE (member))
344 : 3646 : && TREE_CODE (member) == COMPONENT_REF);
345 : :
346 : : /* VEC has the constructor elements vector for the context of FIELD.
347 : : If FIELD is an anonymous aggregate, we will push inside it. */
348 : : vec<constructor_elt, va_gc> **vec = vec_outer;
349 : : tree field;
350 : 1820 : while (field = fields.pop(),
351 : 1820 : ANON_AGGR_TYPE_P (TREE_TYPE (field)))
352 : : {
353 : 958 : tree ctor;
354 : : /* If there is already an outer constructor entry for the anonymous
355 : : aggregate FIELD, use it; otherwise, insert one. */
356 : 958 : if (vec_safe_is_empty (*vec)
357 : 198 : || (*vec)->last().index != field)
358 : : {
359 : 856 : ctor = build_constructor (TREE_TYPE (field), NULL);
360 : 856 : CONSTRUCTOR_APPEND_ELT (*vec, field, ctor);
361 : : }
362 : : else
363 : 102 : ctor = (*vec)->last().value;
364 : 958 : vec = &CONSTRUCTOR_ELTS (ctor);
365 : : }
366 : :
367 : : /* Now we're at the innermost field, the one that isn't an anonymous
368 : : aggregate. Add its initializer to the CONSTRUCTOR and we're done. */
369 : 862 : gcc_assert (fields.is_empty());
370 : 862 : CONSTRUCTOR_APPEND_ELT (*vec, field, init);
371 : :
372 : 862 : return true;
373 : 862 : }
374 : :
375 : : /* Subroutine of build_constexpr_constructor_member_initializers.
376 : : The expression tree T represents a data member initialization
377 : : in a (constexpr) constructor definition. Build a pairing of
378 : : the data member with its initializer, and prepend that pair
379 : : to the existing initialization pair INITS. */
380 : :
381 : : static bool
382 : 2924408 : build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec)
383 : : {
384 : 3164651 : tree member, init;
385 : 3164651 : if (TREE_CODE (t) == CLEANUP_POINT_EXPR)
386 : 1338716 : t = TREE_OPERAND (t, 0);
387 : 3164651 : if (TREE_CODE (t) == EXPR_STMT)
388 : 1338366 : t = TREE_OPERAND (t, 0);
389 : 3164651 : if (t == error_mark_node)
390 : : return false;
391 : 3164644 : if (TREE_CODE (t) == STATEMENT_LIST)
392 : : {
393 : 3373469 : for (tree stmt : tsi_range (t))
394 : 211366 : if (! build_data_member_initialization (stmt, vec))
395 : 7 : return false;
396 : : return true;
397 : : }
398 : 2926942 : if (TREE_CODE (t) == CLEANUP_STMT)
399 : : {
400 : : /* We can't see a CLEANUP_STMT in a constructor for a literal class,
401 : : but we can in a constexpr constructor for a non-literal class. Just
402 : : ignore it; either all the initialization will be constant, in which
403 : : case the cleanup can't run, or it can't be constexpr.
404 : : Still recurse into CLEANUP_BODY. */
405 : 224227 : return build_data_member_initialization (CLEANUP_BODY (t), vec);
406 : : }
407 : 2702715 : if (TREE_CODE (t) == CONVERT_EXPR)
408 : 1644549 : t = TREE_OPERAND (t, 0);
409 : 2702715 : if (TREE_CODE (t) == INIT_EXPR
410 : : /* vptr initialization shows up as a MODIFY_EXPR. In C++14 we only
411 : : use what this function builds for cx_check_missing_mem_inits, and
412 : : assignment in the ctor body doesn't count. */
413 : 1118278 : || (cxx_dialect < cxx14 && TREE_CODE (t) == MODIFY_EXPR))
414 : : {
415 : 1584919 : member = TREE_OPERAND (t, 0);
416 : 1584919 : init = break_out_target_exprs (TREE_OPERAND (t, 1));
417 : : }
418 : 1117796 : else if (TREE_CODE (t) == CALL_EXPR)
419 : : {
420 : 979805 : tree fn = get_callee_fndecl (t);
421 : 1959610 : if (!fn || !DECL_CONSTRUCTOR_P (fn))
422 : : /* We're only interested in calls to subobject constructors. */
423 : : return true;
424 : 863854 : member = CALL_EXPR_ARG (t, 0);
425 : : /* We don't use build_cplus_new here because it complains about
426 : : abstract bases. Leaving the call unwrapped means that it has the
427 : : wrong type, but cxx_eval_constant_expression doesn't care. */
428 : 863854 : init = break_out_target_exprs (t);
429 : : }
430 : 137991 : else if (TREE_CODE (t) == BIND_EXPR)
431 : 16016 : return build_data_member_initialization (BIND_EXPR_BODY (t), vec);
432 : : else
433 : : /* Don't add anything else to the CONSTRUCTOR. */
434 : : return true;
435 : 2448773 : if (INDIRECT_REF_P (member))
436 : 29688 : member = TREE_OPERAND (member, 0);
437 : 2448773 : if (TREE_CODE (member) == NOP_EXPR)
438 : : {
439 : 211797 : tree op = member;
440 : 211797 : STRIP_NOPS (op);
441 : 211797 : if (TREE_CODE (op) == ADDR_EXPR)
442 : : {
443 : 290 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
444 : : (TREE_TYPE (TREE_TYPE (op)),
445 : : TREE_TYPE (TREE_TYPE (member))));
446 : : /* Initializing a cv-qualified member; we need to look through
447 : : the const_cast. */
448 : : member = op;
449 : : }
450 : 211507 : else if (op == current_class_ptr
451 : 422902 : && (same_type_ignoring_top_level_qualifiers_p
452 : 211395 : (TREE_TYPE (TREE_TYPE (member)),
453 : : current_class_type)))
454 : : /* Delegating constructor. */
455 : : member = op;
456 : : else
457 : : {
458 : : /* This is an initializer for an empty base; keep it for now so
459 : : we can check it in cxx_eval_bare_aggregate. */
460 : 192955 : gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member))));
461 : : }
462 : : }
463 : 2448773 : if (TREE_CODE (member) == ADDR_EXPR)
464 : 682035 : member = TREE_OPERAND (member, 0);
465 : 2448773 : if (TREE_CODE (member) == COMPONENT_REF)
466 : : {
467 : 2225667 : tree aggr = TREE_OPERAND (member, 0);
468 : 2225667 : if (TREE_CODE (aggr) == VAR_DECL)
469 : : /* Initializing a local variable, don't add anything. */
470 : : return true;
471 : 2225665 : if (TREE_CODE (aggr) != COMPONENT_REF)
472 : : /* Normal member initialization. */
473 : 2224536 : member = TREE_OPERAND (member, 1);
474 : 1129 : else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr)))
475 : : /* Initializing a member of an anonymous union. */
476 : 862 : return build_anon_member_initialization (member, init, vec);
477 : : else
478 : : /* We're initializing a vtable pointer in a base. Leave it as
479 : : COMPONENT_REF so we remember the path to get to the vfield. */
480 : 267 : gcc_assert (TREE_TYPE (member) == vtbl_ptr_type_node);
481 : : }
482 : :
483 : : /* Value-initialization can produce multiple initializers for the
484 : : same field; use the last one. */
485 : 2994965 : if (!vec_safe_is_empty (*vec) && (*vec)->last().index == member)
486 : 32 : (*vec)->last().value = init;
487 : : else
488 : 2447877 : CONSTRUCTOR_APPEND_ELT (*vec, member, init);
489 : : return true;
490 : : }
491 : :
492 : : /* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval.
493 : : In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a
494 : : BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */
495 : :
496 : : static bool
497 : 2354 : check_constexpr_bind_expr_vars (tree t)
498 : : {
499 : 2354 : gcc_assert (TREE_CODE (t) == BIND_EXPR);
500 : :
501 : 3155 : for (tree var = BIND_EXPR_VARS (t); var; var = DECL_CHAIN (var))
502 : 813 : if (TREE_CODE (var) == TYPE_DECL
503 : 797 : && DECL_IMPLICIT_TYPEDEF_P (var)
504 : 835 : && !LAMBDA_TYPE_P (TREE_TYPE (var)))
505 : : return false;
506 : : return true;
507 : : }
508 : :
509 : : /* Subroutine of check_constexpr_ctor_body. */
510 : :
511 : : static bool
512 : 4069 : check_constexpr_ctor_body_1 (tree last, tree list)
513 : : {
514 : 4069 : switch (TREE_CODE (list))
515 : : {
516 : 6 : case DECL_EXPR:
517 : 6 : if (TREE_CODE (DECL_EXPR_DECL (list)) == USING_DECL
518 : 6 : || TREE_CODE (DECL_EXPR_DECL (list)) == TYPE_DECL)
519 : : return true;
520 : : return false;
521 : :
522 : 3 : case CLEANUP_POINT_EXPR:
523 : 3 : return check_constexpr_ctor_body (last, TREE_OPERAND (list, 0),
524 : 3 : /*complain=*/false);
525 : :
526 : 2026 : case BIND_EXPR:
527 : 2026 : if (!check_constexpr_bind_expr_vars (list)
528 : 2026 : || !check_constexpr_ctor_body (last, BIND_EXPR_BODY (list),
529 : : /*complain=*/false))
530 : 11 : return false;
531 : : return true;
532 : :
533 : : case USING_STMT:
534 : : case STATIC_ASSERT:
535 : : case DEBUG_BEGIN_STMT:
536 : : return true;
537 : :
538 : : default:
539 : : return false;
540 : : }
541 : : }
542 : :
543 : : /* Make sure that there are no statements after LAST in the constructor
544 : : body represented by LIST. */
545 : :
546 : : bool
547 : 3333963 : check_constexpr_ctor_body (tree last, tree list, bool complain)
548 : : {
549 : : /* C++14 doesn't require a constexpr ctor to have an empty body. */
550 : 3333963 : if (cxx_dialect >= cxx14)
551 : : return true;
552 : :
553 : 13068 : bool ok = true;
554 : 13068 : if (TREE_CODE (list) == STATEMENT_LIST)
555 : : {
556 : 13055 : tree_stmt_iterator i = tsi_last (list);
557 : 17093 : for (; !tsi_end_p (i); tsi_prev (&i))
558 : : {
559 : 14974 : tree t = tsi_stmt (i);
560 : 14974 : if (t == last)
561 : : break;
562 : 4056 : if (!check_constexpr_ctor_body_1 (last, t))
563 : : {
564 : : ok = false;
565 : : break;
566 : : }
567 : : }
568 : : }
569 : 13 : else if (list != last
570 : 13 : && !check_constexpr_ctor_body_1 (last, list))
571 : : ok = false;
572 : 13055 : if (!ok)
573 : : {
574 : 22 : if (complain)
575 : 16 : error ("%<constexpr%> constructor does not have empty body");
576 : 22 : DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false;
577 : : }
578 : : return ok;
579 : : }
580 : :
581 : : /* V is a vector of constructor elements built up for the base and member
582 : : initializers of a constructor for TYPE. They need to be in increasing
583 : : offset order, which they might not be yet if TYPE has a primary base
584 : : which is not first in the base-clause or a vptr and at least one base
585 : : all of which are non-primary. */
586 : :
587 : : static vec<constructor_elt, va_gc> *
588 : 1983108 : sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v)
589 : : {
590 : 1983108 : tree pri = CLASSTYPE_PRIMARY_BINFO (type);
591 : 1983108 : tree field_type;
592 : 1983108 : unsigned i;
593 : 1983108 : constructor_elt *ce;
594 : :
595 : 1983108 : if (pri)
596 : 13887 : field_type = BINFO_TYPE (pri);
597 : 1969221 : else if (TYPE_CONTAINS_VPTR_P (type))
598 : 22048 : field_type = vtbl_ptr_type_node;
599 : : else
600 : : return v;
601 : :
602 : : /* Find the element for the primary base or vptr and move it to the
603 : : beginning of the vec. */
604 : 50804 : for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
605 : 28968 : if (TREE_TYPE (ce->index) == field_type)
606 : : break;
607 : :
608 : 45864 : if (i > 0 && i < vec_safe_length (v))
609 : : {
610 : 16 : vec<constructor_elt, va_gc> &vref = *v;
611 : 16 : constructor_elt elt = vref[i];
612 : 32 : for (; i > 0; --i)
613 : 16 : vref[i] = vref[i-1];
614 : 16 : vref[0] = elt;
615 : : }
616 : :
617 : : return v;
618 : : }
619 : :
620 : : /* Build compile-time evalable representations of member-initializer list
621 : : for a constexpr constructor. */
622 : :
623 : : static tree
624 : 2001663 : build_constexpr_constructor_member_initializers (tree type, tree body)
625 : : {
626 : 2001663 : vec<constructor_elt, va_gc> *vec = NULL;
627 : 2001663 : bool ok = true;
628 : 4507917 : while (true)
629 : 4507917 : switch (TREE_CODE (body))
630 : : {
631 : 988871 : case MUST_NOT_THROW_EXPR:
632 : 988871 : case EH_SPEC_BLOCK:
633 : 988871 : body = TREE_OPERAND (body, 0);
634 : 988871 : break;
635 : :
636 : 1517383 : case STATEMENT_LIST:
637 : 6025358 : for (tree stmt : tsi_range (body))
638 : : {
639 : 3034808 : body = stmt;
640 : 3034808 : if (TREE_CODE (body) == BIND_EXPR)
641 : : break;
642 : : }
643 : : break;
644 : :
645 : 2001663 : case BIND_EXPR:
646 : 2001663 : body = BIND_EXPR_BODY (body);
647 : 2001663 : goto found;
648 : :
649 : 0 : default:
650 : 0 : gcc_unreachable ();
651 : : }
652 : 2001663 : found:
653 : 2001663 : if (TREE_CODE (body) == TRY_BLOCK)
654 : : {
655 : 24 : body = TREE_OPERAND (body, 0);
656 : 24 : if (TREE_CODE (body) == BIND_EXPR)
657 : 24 : body = BIND_EXPR_BODY (body);
658 : : }
659 : 2001663 : if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
660 : : {
661 : 1270867 : body = TREE_OPERAND (body, 0);
662 : 1270867 : if (TREE_CODE (body) == EXPR_STMT)
663 : 1270867 : body = TREE_OPERAND (body, 0);
664 : 1270867 : if (TREE_CODE (body) == INIT_EXPR
665 : 1270867 : && (same_type_ignoring_top_level_qualifiers_p
666 : 0 : (TREE_TYPE (TREE_OPERAND (body, 0)),
667 : : current_class_type)))
668 : : {
669 : : /* Trivial copy. */
670 : 0 : return TREE_OPERAND (body, 1);
671 : : }
672 : 1270867 : ok = build_data_member_initialization (body, &vec);
673 : : }
674 : 730796 : else if (TREE_CODE (body) == STATEMENT_LIST)
675 : : {
676 : 2172439 : for (tree stmt : tsi_range (body))
677 : : {
678 : 1441909 : ok = build_data_member_initialization (stmt, &vec);
679 : 1441909 : if (!ok)
680 : : break;
681 : : }
682 : : }
683 : 266 : else if (EXPR_P (body))
684 : 266 : ok = build_data_member_initialization (body, &vec);
685 : : else
686 : 0 : gcc_assert (errorcount > 0);
687 : 2001663 : if (ok)
688 : : {
689 : 2001656 : if (vec_safe_length (vec) > 0)
690 : : {
691 : : /* In a delegating constructor, return the target. */
692 : 1901589 : constructor_elt *ce = &(*vec)[0];
693 : 1901589 : if (ce->index == current_class_ptr)
694 : : {
695 : 18548 : body = ce->value;
696 : 18548 : vec_free (vec);
697 : 18548 : return body;
698 : : }
699 : : }
700 : 1983108 : vec = sort_constexpr_mem_initializers (type, vec);
701 : 1983108 : return build_constructor (type, vec);
702 : : }
703 : : else
704 : 7 : return error_mark_node;
705 : : }
706 : :
707 : : /* We have an expression tree T that represents a call, either CALL_EXPR
708 : : or AGGR_INIT_EXPR. If the call is lexically to a named function,
709 : : return the _DECL for that function. */
710 : :
711 : : static tree
712 : 302968427 : get_function_named_in_call (tree t)
713 : : {
714 : 302968427 : tree callee = cp_get_callee (t);
715 : 302968427 : tree fun = cp_get_fndecl_from_callee (callee, /*fold*/false);
716 : 302968427 : return fun ? fun : callee;
717 : : }
718 : :
719 : : /* Subroutine of check_constexpr_fundef. BODY is the body of a function
720 : : declared to be constexpr, or a sub-statement thereof. Returns the
721 : : return value if suitable, error_mark_node for a statement not allowed in
722 : : a constexpr function, or NULL_TREE if no return value was found. */
723 : :
724 : : tree
725 : 68232 : constexpr_fn_retval (tree body)
726 : : {
727 : 74746 : switch (TREE_CODE (body))
728 : : {
729 : 18474 : case STATEMENT_LIST:
730 : 18474 : {
731 : 18474 : tree expr = NULL_TREE;
732 : 55354 : for (tree stmt : tsi_range (body))
733 : : {
734 : 36907 : tree s = constexpr_fn_retval (stmt);
735 : 36907 : if (s == error_mark_node)
736 : : return error_mark_node;
737 : 36880 : else if (s == NULL_TREE)
738 : : /* Keep iterating. */;
739 : 18437 : else if (expr)
740 : : /* Multiple return statements. */
741 : : return error_mark_node;
742 : : else
743 : : expr = s;
744 : : }
745 : : return expr;
746 : : }
747 : :
748 : 31273 : case RETURN_EXPR:
749 : 31273 : return break_out_target_exprs (TREE_OPERAND (body, 0));
750 : :
751 : 17 : case DECL_EXPR:
752 : 17 : {
753 : 17 : tree decl = DECL_EXPR_DECL (body);
754 : 17 : if (TREE_CODE (decl) == USING_DECL
755 : : /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */
756 : 17 : || DECL_ARTIFICIAL (decl))
757 : : return NULL_TREE;
758 : 6 : return error_mark_node;
759 : : }
760 : :
761 : 6192 : case CLEANUP_POINT_EXPR:
762 : 6192 : return constexpr_fn_retval (TREE_OPERAND (body, 0));
763 : :
764 : 328 : case BIND_EXPR:
765 : 328 : if (!check_constexpr_bind_expr_vars (body))
766 : 6 : return error_mark_node;
767 : 322 : return constexpr_fn_retval (BIND_EXPR_BODY (body));
768 : :
769 : : case USING_STMT:
770 : : case DEBUG_BEGIN_STMT:
771 : : return NULL_TREE;
772 : :
773 : 0 : case CALL_EXPR:
774 : 0 : {
775 : 0 : tree fun = get_function_named_in_call (body);
776 : 0 : if (fun != NULL_TREE
777 : 0 : && fndecl_built_in_p (fun, BUILT_IN_UNREACHABLE))
778 : : return NULL_TREE;
779 : : }
780 : : /* Fallthru. */
781 : :
782 : 30 : default:
783 : 30 : return error_mark_node;
784 : : }
785 : : }
786 : :
787 : : /* Subroutine of check_constexpr_fundef. BODY is the DECL_SAVED_TREE of
788 : : FUN; do the necessary transformations to turn it into a single expression
789 : : that we can store in the hash table. */
790 : :
791 : : static tree
792 : 17457511 : massage_constexpr_body (tree fun, tree body)
793 : : {
794 : 34915022 : if (DECL_CONSTRUCTOR_P (fun))
795 : 2001663 : body = build_constexpr_constructor_member_initializers
796 : 2001663 : (DECL_CONTEXT (fun), body);
797 : 15455848 : else if (cxx_dialect < cxx14)
798 : : {
799 : 31096 : if (TREE_CODE (body) == EH_SPEC_BLOCK)
800 : 1 : body = EH_SPEC_STMTS (body);
801 : 31096 : if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
802 : 20233 : body = TREE_OPERAND (body, 0);
803 : 31096 : body = constexpr_fn_retval (body);
804 : : }
805 : 17457511 : return body;
806 : : }
807 : :
808 : : /* CTYPE is a type constructed from BODY. Return true if some
809 : : bases/fields are uninitialized, and complain if COMPLAIN. */
810 : :
811 : : static bool
812 : 1678607 : cx_check_missing_mem_inits (tree ctype, tree body, bool complain)
813 : : {
814 : : /* We allow uninitialized bases/fields in C++20. */
815 : 1678607 : if (cxx_dialect >= cxx20)
816 : : return false;
817 : :
818 : 788170 : unsigned nelts = 0;
819 : :
820 : 788170 : if (body)
821 : : {
822 : 788166 : if (TREE_CODE (body) != CONSTRUCTOR)
823 : : return false;
824 : 787910 : nelts = CONSTRUCTOR_NELTS (body);
825 : : }
826 : 787914 : tree field = TYPE_FIELDS (ctype);
827 : :
828 : 787914 : if (TREE_CODE (ctype) == UNION_TYPE)
829 : : {
830 : 8164 : if (nelts == 0 && next_aggregate_field (field))
831 : : {
832 : 2 : if (complain)
833 : 2 : error ("%<constexpr%> constructor for union %qT must "
834 : : "initialize exactly one non-static data member", ctype);
835 : 2 : return true;
836 : : }
837 : 8162 : return false;
838 : : }
839 : :
840 : : /* Iterate over the CONSTRUCTOR, checking any missing fields don't
841 : : need an explicit initialization. */
842 : : bool bad = false;
843 : 1683782 : for (unsigned i = 0; i <= nelts; ++i)
844 : : {
845 : 1683782 : tree index = NULL_TREE;
846 : 1683782 : if (i < nelts)
847 : : {
848 : 904032 : index = CONSTRUCTOR_ELT (body, i)->index;
849 : : /* Skip vptr adjustment represented with a COMPONENT_REF. */
850 : 904032 : if (TREE_CODE (index) != FIELD_DECL)
851 : 67320 : continue;
852 : : }
853 : :
854 : 39264846 : for (; field != index; field = DECL_CHAIN (field))
855 : : {
856 : 37648453 : tree ftype;
857 : 37648453 : if (TREE_CODE (field) != FIELD_DECL)
858 : 75163133 : continue;
859 : 133683 : if (DECL_UNNAMED_BIT_FIELD (field))
860 : 16 : continue;
861 : : /* Artificial fields can be ignored unless they're bases. */
862 : 133667 : if (DECL_ARTIFICIAL (field) && !DECL_FIELD_IS_BASE (field))
863 : 3 : continue;
864 : 133664 : if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
865 : : {
866 : : /* Recurse to check the anonymous aggregate member. */
867 : 8 : bad |= cx_check_missing_mem_inits
868 : 4 : (TREE_TYPE (field), NULL_TREE, complain);
869 : 4 : if (bad && !complain)
870 : 69 : return true;
871 : 4 : continue;
872 : : }
873 : 133660 : ftype = TREE_TYPE (field);
874 : 133660 : if (!ftype || !TYPE_P (ftype) || !COMPLETE_TYPE_P (ftype))
875 : : /* A flexible array can't be intialized here, so don't complain
876 : : that it isn't. */
877 : 2 : continue;
878 : 133658 : if (is_empty_field (field))
879 : : /* An empty field doesn't need an initializer. */
880 : 133557 : continue;
881 : 101 : ftype = strip_array_types (ftype);
882 : 101 : if (type_has_constexpr_default_constructor (ftype))
883 : : {
884 : : /* It's OK to skip a member with a trivial constexpr ctor.
885 : : A constexpr ctor that isn't trivial should have been
886 : : added in by now. */
887 : 11 : gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype)
888 : : || errorcount != 0);
889 : 11 : continue;
890 : : }
891 : 90 : if (!complain)
892 : : return true;
893 : 21 : auto_diagnostic_group d;
894 : 21 : error ("member %qD must be initialized by mem-initializer "
895 : : "in %<constexpr%> constructor", field);
896 : 21 : inform (DECL_SOURCE_LOCATION (field), "declared here");
897 : 21 : bad = true;
898 : 21 : }
899 : 1616393 : if (field == NULL_TREE)
900 : : break;
901 : :
902 : 836712 : if (ANON_AGGR_TYPE_P (TREE_TYPE (index)))
903 : : {
904 : : /* Check the anonymous aggregate initializer is valid. */
905 : 448 : bad |= cx_check_missing_mem_inits
906 : 224 : (TREE_TYPE (index), CONSTRUCTOR_ELT (body, i)->value, complain);
907 : 224 : if (bad && !complain)
908 : : return true;
909 : : }
910 : 836712 : field = DECL_CHAIN (field);
911 : : }
912 : :
913 : : return bad;
914 : : }
915 : :
916 : : /* We are processing the definition of the constexpr function FUN.
917 : : Check that its body fulfills the apropriate requirements and
918 : : enter it in the constexpr function definition table. */
919 : :
920 : : void
921 : 139343216 : maybe_save_constexpr_fundef (tree fun)
922 : : {
923 : 139343216 : if (processing_template_decl
924 : 55305866 : || cp_function_chain->invalid_constexpr
925 : 194537709 : || (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun)))
926 : 121885968 : return;
927 : :
928 : : /* With -fimplicit-constexpr, try to make inlines constexpr. We'll
929 : : actually set DECL_DECLARED_CONSTEXPR_P below if the checks pass. */
930 : 41830949 : bool implicit = false;
931 : 41830949 : if (flag_implicit_constexpr)
932 : : {
933 : 13 : if (DECL_DELETING_DESTRUCTOR_P (fun)
934 : 13 : && decl_implicit_constexpr_p (DECL_CLONED_FUNCTION (fun)))
935 : : /* Don't inherit implicit constexpr from the non-deleting
936 : : destructor. */
937 : 0 : DECL_DECLARED_CONSTEXPR_P (fun) = false;
938 : :
939 : 13 : if (!DECL_DECLARED_CONSTEXPR_P (fun)
940 : 12 : && DECL_DECLARED_INLINE_P (fun)
941 : 21 : && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fun)))
942 : : implicit = true;
943 : : }
944 : :
945 : 41830949 : if (!DECL_DECLARED_CONSTEXPR_P (fun) && !implicit)
946 : : return;
947 : :
948 : 17519167 : bool complain = !DECL_GENERATED_P (fun) && !implicit;
949 : :
950 : 17519167 : if (!is_valid_constexpr_fn (fun, complain))
951 : : return;
952 : :
953 : 17457464 : tree massaged = massage_constexpr_body (fun, DECL_SAVED_TREE (fun));
954 : 17457464 : if (massaged == NULL_TREE || massaged == error_mark_node)
955 : : {
956 : 66 : if (!DECL_CONSTRUCTOR_P (fun) && complain)
957 : 22 : error ("body of %<constexpr%> function %qD not a return-statement",
958 : : fun);
959 : 33 : return;
960 : : }
961 : :
962 : 17457431 : bool potential = potential_rvalue_constant_expression (massaged);
963 : 17457431 : if (!potential && complain)
964 : 198 : require_potential_rvalue_constant_expression_fncheck (massaged);
965 : :
966 : 19459076 : if (DECL_CONSTRUCTOR_P (fun) && potential
967 : 19456483 : && !DECL_DEFAULTED_FN (fun))
968 : : {
969 : 1678368 : if (cx_check_missing_mem_inits (DECL_CONTEXT (fun),
970 : : massaged, complain))
971 : : potential = false;
972 : 1678280 : else if (cxx_dialect > cxx11)
973 : : {
974 : : /* What we got from massage_constexpr_body is pretty much just the
975 : : ctor-initializer, also check the body. */
976 : 1673596 : massaged = DECL_SAVED_TREE (fun);
977 : 1673596 : potential = potential_rvalue_constant_expression (massaged);
978 : 1673596 : if (!potential && complain)
979 : 16 : require_potential_rvalue_constant_expression_fncheck (massaged);
980 : : }
981 : : }
982 : :
983 : 17457431 : if (!potential && complain
984 : : /* If -Wno-invalid-constexpr was specified, we haven't complained
985 : : about non-constant expressions yet. Register the function and
986 : : complain in explain_invalid_constexpr_fn if the function is
987 : : called. */
988 : 233 : && warn_invalid_constexpr != 0)
989 : : return;
990 : :
991 : 17457256 : if (implicit)
992 : : {
993 : 8 : if (potential)
994 : : {
995 : 0 : DECL_DECLARED_CONSTEXPR_P (fun) = true;
996 : 0 : DECL_LANG_SPECIFIC (fun)->u.fn.implicit_constexpr = true;
997 : 0 : if (DECL_CONSTRUCTOR_P (fun))
998 : 0 : TYPE_HAS_CONSTEXPR_CTOR (DECL_CONTEXT (fun)) = true;
999 : : }
1000 : : else
1001 : : /* Don't bother keeping the pre-generic body of unsuitable functions
1002 : : not explicitly declared constexpr. */
1003 : : return;
1004 : : }
1005 : :
1006 : 17457248 : constexpr_fundef entry = {fun, NULL_TREE, NULL_TREE, NULL_TREE};
1007 : 17457248 : bool clear_ctx = false;
1008 : 17457248 : if (DECL_RESULT (fun) && DECL_CONTEXT (DECL_RESULT (fun)) == NULL_TREE)
1009 : : {
1010 : 17457248 : clear_ctx = true;
1011 : 17457248 : DECL_CONTEXT (DECL_RESULT (fun)) = fun;
1012 : : }
1013 : 17457248 : tree saved_fn = current_function_decl;
1014 : 17457248 : current_function_decl = fun;
1015 : 17457248 : entry.body = copy_fn (entry.decl, entry.parms, entry.result);
1016 : 17457248 : current_function_decl = saved_fn;
1017 : 17457248 : if (clear_ctx)
1018 : 17457248 : DECL_CONTEXT (DECL_RESULT (entry.decl)) = NULL_TREE;
1019 : 17457248 : if (!potential)
1020 : : /* For a template instantiation, we want to remember the pre-generic body
1021 : : for explain_invalid_constexpr_fn, but do tell cxx_eval_call_expression
1022 : : that it doesn't need to bother trying to expand the function. */
1023 : 83463 : entry.result = error_mark_node;
1024 : :
1025 : 17457248 : register_constexpr_fundef (entry);
1026 : : }
1027 : :
1028 : : /* BODY is a validated and massaged definition of a constexpr
1029 : : function. Register it in the hash table. */
1030 : :
1031 : : void
1032 : 17478318 : register_constexpr_fundef (const constexpr_fundef &value)
1033 : : {
1034 : : /* Create the constexpr function table if necessary. */
1035 : 17478318 : if (constexpr_fundef_table == NULL)
1036 : 25111 : constexpr_fundef_table
1037 : 25111 : = hash_table<constexpr_fundef_hasher>::create_ggc (101);
1038 : :
1039 : 17478318 : constexpr_fundef **slot = constexpr_fundef_table->find_slot
1040 : 17478318 : (const_cast<constexpr_fundef *> (&value), INSERT);
1041 : :
1042 : 17478318 : gcc_assert (*slot == NULL);
1043 : 17478318 : *slot = ggc_alloc<constexpr_fundef> ();
1044 : 17478318 : **slot = value;
1045 : 17478318 : }
1046 : :
1047 : : /* FUN is a non-constexpr (or, with -Wno-invalid-constexpr, a constexpr
1048 : : function called in a context that requires a constant expression).
1049 : : If it comes from a constexpr template, explain why the instantiation
1050 : : isn't constexpr. Otherwise, explain why the function cannot be used
1051 : : in a constexpr context. */
1052 : :
1053 : : void
1054 : 344 : explain_invalid_constexpr_fn (tree fun)
1055 : : {
1056 : 344 : static hash_set<tree> *diagnosed;
1057 : 344 : tree body;
1058 : :
1059 : : /* Don't try to explain a function we already complained about. */
1060 : 344 : if (function *f = DECL_STRUCT_FUNCTION (fun))
1061 : 233 : if (f->language->erroneous)
1062 : 344 : return;
1063 : :
1064 : : /* In C++23, a function marked 'constexpr' may not actually be a constant
1065 : : expression. We haven't diagnosed the problem yet: -Winvalid-constexpr
1066 : : wasn't enabled. The function was called, so diagnose why it cannot be
1067 : : used in a constant expression. */
1068 : 295 : if (warn_invalid_constexpr == 0 && DECL_DECLARED_CONSTEXPR_P (fun))
1069 : : /* Go on. */;
1070 : : /* Only diagnose defaulted functions, lambdas, or instantiations. */
1071 : 273 : else if (!DECL_DEFAULTED_FN (fun)
1072 : 403 : && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun))
1073 : 248 : && !(flag_implicit_constexpr
1074 : 6 : && !DECL_DECLARED_CONSTEXPR_P (fun)
1075 : 6 : && DECL_DECLARED_INLINE_P (fun))
1076 : 518 : && !is_instantiation_of_constexpr (fun))
1077 : : {
1078 : 231 : inform (DECL_SOURCE_LOCATION (fun), "%qD declared here", fun);
1079 : 3 : if (flag_implicit_constexpr && !maybe_constexpr_fn (fun)
1080 : 234 : && decl_defined_p (fun))
1081 : 3 : inform (DECL_SOURCE_LOCATION (fun),
1082 : : "%<-fimplicit-constexpr%> only affects %<inline%> functions");
1083 : 231 : return;
1084 : : }
1085 : 64 : if (diagnosed == NULL)
1086 : 52 : diagnosed = new hash_set<tree>;
1087 : 64 : if (diagnosed->add (fun))
1088 : : /* Already explained. */
1089 : : return;
1090 : :
1091 : 63 : iloc_sentinel ils = input_location;
1092 : 63 : if (!lambda_static_thunk_p (fun))
1093 : : {
1094 : : /* Diagnostics should completely ignore the static thunk, so leave
1095 : : input_location set to our caller's location. */
1096 : 63 : input_location = DECL_SOURCE_LOCATION (fun);
1097 : 63 : inform (input_location,
1098 : : "%qD is not usable as a %<constexpr%> function because:", fun);
1099 : : }
1100 : : /* First check the declaration. */
1101 : 63 : if (is_valid_constexpr_fn (fun, true))
1102 : : {
1103 : : /* Then if it's OK, the body. */
1104 : 57 : if (!DECL_DECLARED_CONSTEXPR_P (fun)
1105 : 57 : && DECL_DEFAULTED_FN (fun))
1106 : 10 : explain_implicit_non_constexpr (fun);
1107 : : else
1108 : : {
1109 : 47 : if (constexpr_fundef *fd = retrieve_constexpr_fundef (fun))
1110 : 37 : body = fd->body;
1111 : : else
1112 : 10 : body = DECL_SAVED_TREE (fun);
1113 : 47 : tree massaged = massage_constexpr_body (fun, body);
1114 : 47 : require_potential_rvalue_constant_expression (massaged);
1115 : 94 : if (DECL_CONSTRUCTOR_P (fun))
1116 : : {
1117 : 11 : cx_check_missing_mem_inits (DECL_CONTEXT (fun), massaged, true);
1118 : 11 : if (cxx_dialect > cxx11)
1119 : : /* Also check the body, not just the ctor-initializer. */
1120 : 9 : require_potential_rvalue_constant_expression (body);
1121 : : }
1122 : : }
1123 : : }
1124 : 63 : }
1125 : :
1126 : : /* Objects of this type represent calls to constexpr functions
1127 : : along with the bindings of parameters to their arguments, for
1128 : : the purpose of compile time evaluation. */
1129 : :
1130 : : struct GTY((for_user)) constexpr_call {
1131 : : /* Description of the constexpr function definition. */
1132 : : constexpr_fundef *fundef = nullptr;
1133 : : /* Parameter bindings environment. A TREE_VEC of arguments. */
1134 : : tree bindings = NULL_TREE;
1135 : : /* Result of the call, indexed by the value of
1136 : : constexpr_ctx::manifestly_const_eval.
1137 : : unknown_type_node means the call is being evaluated.
1138 : : error_mark_node means that the evaluation was erroneous or otherwise
1139 : : uncacheable (e.g. because it depends on the caller).
1140 : : Otherwise, the actual value of the call. */
1141 : : tree results[3] = { NULL_TREE, NULL_TREE, NULL_TREE };
1142 : : /* The hash of this call; we remember it here to avoid having to
1143 : : recalculate it when expanding the hash table. */
1144 : : hashval_t hash = 0;
1145 : :
1146 : : /* The result slot corresponding to the given mce_value. */
1147 : 37167364 : tree& result (mce_value mce) { return results[1 + int(mce)]; }
1148 : : };
1149 : :
1150 : : struct constexpr_call_hasher : ggc_ptr_hash<constexpr_call>
1151 : : {
1152 : : static hashval_t hash (constexpr_call *);
1153 : : static bool equal (constexpr_call *, constexpr_call *);
1154 : : };
1155 : :
1156 : : enum constexpr_switch_state {
1157 : : /* Used when processing a switch for the first time by cxx_eval_switch_expr
1158 : : and default: label for that switch has not been seen yet. */
1159 : : css_default_not_seen,
1160 : : /* Used when processing a switch for the first time by cxx_eval_switch_expr
1161 : : and default: label for that switch has been seen already. */
1162 : : css_default_seen,
1163 : : /* Used when processing a switch for the second time by
1164 : : cxx_eval_switch_expr, where default: label should match. */
1165 : : css_default_processing
1166 : : };
1167 : :
1168 : : /* The constexpr expansion context part which needs one instance per
1169 : : cxx_eval_outermost_constant_expr invocation. VALUES is a map of values of
1170 : : variables initialized within the expression. */
1171 : :
1172 : 361361338 : class constexpr_global_ctx {
1173 : : /* Values for any temporaries or local variables within the
1174 : : constant-expression. Objects outside their lifetime have
1175 : : value 'void_node'. */
1176 : : hash_map<tree,tree> values;
1177 : : public:
1178 : : /* Number of cxx_eval_constant_expression calls (except skipped ones,
1179 : : on simple constants or location wrappers) encountered during current
1180 : : cxx_eval_outermost_constant_expr call. */
1181 : : HOST_WIDE_INT constexpr_ops_count;
1182 : : /* Heap VAR_DECLs created during the evaluation of the outermost constant
1183 : : expression. */
1184 : : auto_vec<tree, 16> heap_vars;
1185 : : /* Cleanups that need to be evaluated at the end of CLEANUP_POINT_EXPR. */
1186 : : vec<tree> *cleanups;
1187 : : /* If non-null, only allow modification of existing values of the variables
1188 : : in this set. Set by modifiable_tracker, below. */
1189 : : hash_set<tree> *modifiable;
1190 : : /* Number of heap VAR_DECL deallocations. */
1191 : : unsigned heap_dealloc_count;
1192 : : /* Constructor. */
1193 : 361364035 : constexpr_global_ctx ()
1194 : 722728070 : : constexpr_ops_count (0), cleanups (NULL), modifiable (nullptr),
1195 : 361364035 : heap_dealloc_count (0) {}
1196 : :
1197 : 187607590 : bool is_outside_lifetime (tree t)
1198 : : {
1199 : 187607590 : if (tree *p = values.get (t))
1200 : 40798459 : if (*p == void_node)
1201 : 178 : return true;
1202 : : return false;
1203 : : }
1204 : 220680154 : tree get_value (tree t)
1205 : : {
1206 : 220680154 : if (tree *p = values.get (t))
1207 : 64745042 : if (*p != void_node)
1208 : 64042381 : return *p;
1209 : : return NULL_TREE;
1210 : : }
1211 : 29326482 : tree *get_value_ptr (tree t, bool initializing)
1212 : : {
1213 : 29326482 : if (modifiable && !modifiable->contains (t))
1214 : : return nullptr;
1215 : 29326471 : if (tree *p = values.get (t))
1216 : : {
1217 : 29311493 : if (*p != void_node)
1218 : : return p;
1219 : 36 : else if (initializing)
1220 : : {
1221 : 6 : *p = NULL_TREE;
1222 : 6 : return p;
1223 : : }
1224 : : }
1225 : : return nullptr;
1226 : : }
1227 : 85130226 : void put_value (tree t, tree v)
1228 : : {
1229 : 85130226 : bool already_in_map = values.put (t, v);
1230 : 85130226 : if (!already_in_map && modifiable)
1231 : 32 : modifiable->add (t);
1232 : 85130226 : }
1233 : 60241291 : void destroy_value (tree t)
1234 : : {
1235 : 60241291 : if (TREE_CODE (t) == VAR_DECL
1236 : 60241291 : || TREE_CODE (t) == PARM_DECL
1237 : : || TREE_CODE (t) == RESULT_DECL)
1238 : 60235738 : values.put (t, void_node);
1239 : : else
1240 : 5553 : values.remove (t);
1241 : 60241291 : }
1242 : 3078246 : void clear_value (tree t)
1243 : : {
1244 : 6156492 : values.remove (t);
1245 : : }
1246 : : };
1247 : :
1248 : : /* Helper class for constexpr_global_ctx. In some cases we want to avoid
1249 : : side-effects from evaluation of a particular subexpression of a
1250 : : constant-expression. In such cases we use modifiable_tracker to prevent
1251 : : modification of variables created outside of that subexpression.
1252 : :
1253 : : ??? We could change the hash_set to a hash_map, allow and track external
1254 : : modifications, and roll them back in the destructor. It's not clear to me
1255 : : that this would be worthwhile. */
1256 : :
1257 : : class modifiable_tracker
1258 : : {
1259 : : hash_set<tree> set;
1260 : : constexpr_global_ctx *global;
1261 : : public:
1262 : 223924 : modifiable_tracker (constexpr_global_ctx *g): global(g)
1263 : : {
1264 : 223924 : global->modifiable = &set;
1265 : 223924 : }
1266 : 223924 : ~modifiable_tracker ()
1267 : : {
1268 : 223956 : for (tree t: set)
1269 : 32 : global->clear_value (t);
1270 : 223924 : global->modifiable = nullptr;
1271 : 223924 : }
1272 : : };
1273 : :
1274 : : /* The constexpr expansion context. CALL is the current function
1275 : : expansion, CTOR is the current aggregate initializer, OBJECT is the
1276 : : object being initialized by CTOR, either a VAR_DECL or a _REF. */
1277 : :
1278 : : struct constexpr_ctx {
1279 : : /* The part of the context that needs to be unique to the whole
1280 : : cxx_eval_outermost_constant_expr invocation. */
1281 : : constexpr_global_ctx *global;
1282 : : /* The innermost call we're evaluating. */
1283 : : constexpr_call *call;
1284 : : /* SAVE_EXPRs and TARGET_EXPR_SLOT vars of TARGET_EXPRs that we've seen
1285 : : within the current LOOP_EXPR. NULL if we aren't inside a loop. */
1286 : : vec<tree> *save_exprs;
1287 : : /* The CONSTRUCTOR we're currently building up for an aggregate
1288 : : initializer. */
1289 : : tree ctor;
1290 : : /* The object we're building the CONSTRUCTOR for. */
1291 : : tree object;
1292 : : /* If inside SWITCH_EXPR. */
1293 : : constexpr_switch_state *css_state;
1294 : : /* The aggregate initialization context inside which this one is nested. This
1295 : : is used by lookup_placeholder to resolve PLACEHOLDER_EXPRs. */
1296 : : const constexpr_ctx *parent;
1297 : :
1298 : : /* Whether we should error on a non-constant expression or fail quietly.
1299 : : This flag needs to be here, but some of the others could move to global
1300 : : if they get larger than a word. */
1301 : : bool quiet;
1302 : : /* Whether we are strictly conforming to constant expression rules or
1303 : : trying harder to get a constant value. */
1304 : : bool strict;
1305 : : /* Whether __builtin_is_constant_evaluated () should be true. */
1306 : : mce_value manifestly_const_eval;
1307 : : };
1308 : :
1309 : : /* True if the constexpr relaxations afforded by P2280R4 for unknown
1310 : : references and objects are in effect. */
1311 : :
1312 : : static bool
1313 : 154198834 : p2280_active_p (const constexpr_ctx *ctx)
1314 : : {
1315 : 27421730 : if (ctx->manifestly_const_eval != mce_true)
1316 : : /* Disable these relaxations during speculative constexpr folding,
1317 : : as it can significantly increase compile time/memory use
1318 : : (PR119387). */
1319 : : return false;
1320 : :
1321 : : /* P2280R4 was accepted as a DR against C++11. */
1322 : 0 : return cxx_dialect >= cxx11;
1323 : : }
1324 : :
1325 : : /* Remove T from the global values map, checking for attempts to destroy
1326 : : a value that has already finished its lifetime. */
1327 : :
1328 : : static void
1329 : 60222253 : destroy_value_checked (const constexpr_ctx* ctx, tree t, bool *non_constant_p)
1330 : : {
1331 : 60222253 : if (t == error_mark_node || TREE_TYPE (t) == error_mark_node)
1332 : : return;
1333 : :
1334 : : /* Don't error again here if we've already reported a problem. */
1335 : 60222250 : if (!*non_constant_p
1336 : 40999328 : && DECL_P (t)
1337 : : /* Non-trivial destructors have their lifetimes ended explicitly
1338 : : with a clobber, so don't worry about it here. */
1339 : 40993918 : && (!TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (t))
1340 : : /* ...except parameters are remapped in cxx_eval_call_expression,
1341 : : and the destructor call during cleanup won't be able to tell that
1342 : : this value has already been destroyed, so complain now. This is
1343 : : not quite unobservable, but is extremely unlikely to crop up in
1344 : : practice; see g++.dg/cpp2a/constexpr-lifetime2.C. */
1345 : 176478 : || TREE_CODE (t) == PARM_DECL)
1346 : 101039903 : && ctx->global->is_outside_lifetime (t))
1347 : : {
1348 : 54 : if (!ctx->quiet)
1349 : : {
1350 : 18 : auto_diagnostic_group d;
1351 : 18 : error ("destroying %qE outside its lifetime", t);
1352 : 18 : inform (DECL_SOURCE_LOCATION (t), "declared here");
1353 : 18 : }
1354 : 54 : *non_constant_p = true;
1355 : : }
1356 : 60222250 : ctx->global->destroy_value (t);
1357 : : }
1358 : :
1359 : : /* This internal flag controls whether we should avoid doing anything during
1360 : : constexpr evaluation that would cause extra DECL_UID generation, such as
1361 : : template instantiation and function body copying. */
1362 : :
1363 : : static bool uid_sensitive_constexpr_evaluation_value;
1364 : :
1365 : : /* An internal counter that keeps track of the number of times
1366 : : uid_sensitive_constexpr_evaluation_p returned true. */
1367 : :
1368 : : static unsigned uid_sensitive_constexpr_evaluation_true_counter;
1369 : :
1370 : : /* The accessor for uid_sensitive_constexpr_evaluation_value which also
1371 : : increments the corresponding counter. */
1372 : :
1373 : : static bool
1374 : 4388432 : uid_sensitive_constexpr_evaluation_p ()
1375 : : {
1376 : 4384097 : if (uid_sensitive_constexpr_evaluation_value)
1377 : : {
1378 : 194629 : ++uid_sensitive_constexpr_evaluation_true_counter;
1379 : 194629 : return true;
1380 : : }
1381 : : else
1382 : : return false;
1383 : : }
1384 : :
1385 : : /* The default constructor for uid_sensitive_constexpr_evaluation_sentinel
1386 : : enables the internal flag for uid_sensitive_constexpr_evaluation_p
1387 : : during the lifetime of the sentinel object. Upon its destruction, the
1388 : : previous value of uid_sensitive_constexpr_evaluation_p is restored. */
1389 : :
1390 : 64365188 : uid_sensitive_constexpr_evaluation_sentinel
1391 : 64365188 : ::uid_sensitive_constexpr_evaluation_sentinel ()
1392 : 64365188 : : ovr (uid_sensitive_constexpr_evaluation_value, true)
1393 : : {
1394 : 64365188 : }
1395 : :
1396 : : /* The default constructor for uid_sensitive_constexpr_evaluation_checker
1397 : : records the current number of times that uid_sensitive_constexpr_evaluation_p
1398 : : has been called and returned true. */
1399 : :
1400 : 1877755137 : uid_sensitive_constexpr_evaluation_checker
1401 : 1877755137 : ::uid_sensitive_constexpr_evaluation_checker ()
1402 : 1877755137 : : saved_counter (uid_sensitive_constexpr_evaluation_true_counter)
1403 : : {
1404 : 1877755137 : }
1405 : :
1406 : : /* Returns true iff uid_sensitive_constexpr_evaluation_p is true, and
1407 : : some constexpr evaluation was restricted due to u_s_c_e_p being called
1408 : : and returning true during the lifetime of this checker object. */
1409 : :
1410 : : bool
1411 : 1315973089 : uid_sensitive_constexpr_evaluation_checker::evaluation_restricted_p () const
1412 : : {
1413 : 1315973089 : return (uid_sensitive_constexpr_evaluation_value
1414 : 1315973089 : && saved_counter != uid_sensitive_constexpr_evaluation_true_counter);
1415 : : }
1416 : :
1417 : :
1418 : : /* A table of all constexpr calls that have been evaluated by the
1419 : : compiler in this translation unit. */
1420 : :
1421 : : static GTY (()) hash_table<constexpr_call_hasher> *constexpr_call_table;
1422 : :
1423 : : /* Compute a hash value for a constexpr call representation. */
1424 : :
1425 : : inline hashval_t
1426 : 111348415 : constexpr_call_hasher::hash (constexpr_call *info)
1427 : : {
1428 : 111348415 : return info->hash;
1429 : : }
1430 : :
1431 : : /* Return true if the objects pointed to by P and Q represent calls
1432 : : to the same constexpr function with the same arguments.
1433 : : Otherwise, return false. */
1434 : :
1435 : : bool
1436 : 121387098 : constexpr_call_hasher::equal (constexpr_call *lhs, constexpr_call *rhs)
1437 : : {
1438 : 121387098 : if (lhs == rhs)
1439 : : return true;
1440 : 121387098 : if (lhs->hash != rhs->hash)
1441 : : return false;
1442 : 15697046 : if (!constexpr_fundef_hasher::equal (lhs->fundef, rhs->fundef))
1443 : : return false;
1444 : 15697046 : return cp_tree_equal (lhs->bindings, rhs->bindings);
1445 : : }
1446 : :
1447 : : /* Initialize the constexpr call table, if needed. */
1448 : :
1449 : : static void
1450 : 18709129 : maybe_initialize_constexpr_call_table (void)
1451 : : {
1452 : 18709129 : if (constexpr_call_table == NULL)
1453 : 16801 : constexpr_call_table = hash_table<constexpr_call_hasher>::create_ggc (101);
1454 : 18709129 : }
1455 : :
1456 : : /* During constexpr CALL_EXPR evaluation, to avoid issues with sharing when
1457 : : a function happens to get called recursively, we unshare the callee
1458 : : function's body and evaluate this unshared copy instead of evaluating the
1459 : : original body.
1460 : :
1461 : : FUNDEF_COPIES_TABLE is a per-function freelist of these unshared function
1462 : : copies. The underlying data structure of FUNDEF_COPIES_TABLE is a hash_map
1463 : : that's keyed off of the original FUNCTION_DECL and whose value is a
1464 : : TREE_LIST of this function's unused copies awaiting reuse.
1465 : :
1466 : : This is not GC-deletable to avoid GC affecting UID generation. */
1467 : :
1468 : : static GTY(()) decl_tree_map *fundef_copies_table;
1469 : :
1470 : : /* Reuse a copy or create a new unshared copy of the function FUN.
1471 : : Return this copy. We use a TREE_LIST whose PURPOSE is body, VALUE
1472 : : is parms, TYPE is result. */
1473 : :
1474 : : static tree
1475 : 25622123 : get_fundef_copy (constexpr_fundef *fundef)
1476 : : {
1477 : 25622123 : tree copy;
1478 : 25622123 : bool existed;
1479 : 25622123 : tree *slot = &(hash_map_safe_get_or_insert<hm_ggc>
1480 : 25622123 : (fundef_copies_table, fundef->decl, &existed, 127));
1481 : :
1482 : 25622123 : if (!existed)
1483 : : {
1484 : : /* There is no cached function available, or in use. We can use
1485 : : the function directly. That the slot is now created records
1486 : : that this function is now in use. */
1487 : 4221082 : copy = build_tree_list (fundef->body, fundef->parms);
1488 : 4221082 : TREE_TYPE (copy) = fundef->result;
1489 : : }
1490 : 21401041 : else if (*slot == NULL_TREE)
1491 : : {
1492 : 4335 : if (uid_sensitive_constexpr_evaluation_p ())
1493 : 0 : return NULL_TREE;
1494 : :
1495 : : /* We've already used the function itself, so make a copy. */
1496 : 4335 : copy = build_tree_list (NULL, NULL);
1497 : 4335 : tree saved_body = DECL_SAVED_TREE (fundef->decl);
1498 : 4335 : tree saved_parms = DECL_ARGUMENTS (fundef->decl);
1499 : 4335 : tree saved_result = DECL_RESULT (fundef->decl);
1500 : 4335 : tree saved_fn = current_function_decl;
1501 : 4335 : DECL_SAVED_TREE (fundef->decl) = fundef->body;
1502 : 4335 : DECL_ARGUMENTS (fundef->decl) = fundef->parms;
1503 : 4335 : DECL_RESULT (fundef->decl) = fundef->result;
1504 : 4335 : current_function_decl = fundef->decl;
1505 : 4335 : TREE_PURPOSE (copy) = copy_fn (fundef->decl, TREE_VALUE (copy),
1506 : 4335 : TREE_TYPE (copy));
1507 : 4335 : current_function_decl = saved_fn;
1508 : 4335 : DECL_RESULT (fundef->decl) = saved_result;
1509 : 4335 : DECL_ARGUMENTS (fundef->decl) = saved_parms;
1510 : 4335 : DECL_SAVED_TREE (fundef->decl) = saved_body;
1511 : : }
1512 : : else
1513 : : {
1514 : : /* We have a cached function available. */
1515 : 21396706 : copy = *slot;
1516 : 21396706 : *slot = TREE_CHAIN (copy);
1517 : : }
1518 : :
1519 : : return copy;
1520 : : }
1521 : :
1522 : : /* Save the copy COPY of function FUN for later reuse by
1523 : : get_fundef_copy(). By construction, there will always be an entry
1524 : : to find. */
1525 : :
1526 : : static void
1527 : 25622123 : save_fundef_copy (tree fun, tree copy)
1528 : : {
1529 : 25622123 : tree *slot = fundef_copies_table->get (fun);
1530 : 25622123 : TREE_CHAIN (copy) = *slot;
1531 : 25622123 : *slot = copy;
1532 : 25622123 : }
1533 : :
1534 : : /* Whether our evaluation wants a prvalue (e.g. CONSTRUCTOR or _CST),
1535 : : a glvalue (e.g. VAR_DECL or _REF), or nothing. */
1536 : :
1537 : : enum value_cat {
1538 : : vc_prvalue = 0,
1539 : : vc_glvalue = 1,
1540 : : vc_discard = 2
1541 : : };
1542 : :
1543 : : static tree cxx_eval_constant_expression (const constexpr_ctx *, tree,
1544 : : value_cat, bool *, bool *, tree * = NULL);
1545 : : static tree cxx_eval_bare_aggregate (const constexpr_ctx *, tree,
1546 : : value_cat, bool *, bool *);
1547 : : static tree cxx_fold_indirect_ref (const constexpr_ctx *, location_t, tree, tree,
1548 : : bool * = NULL);
1549 : : static tree find_heap_var_refs (tree *, int *, void *);
1550 : :
1551 : : /* Attempt to evaluate T which represents a call to a builtin function.
1552 : : We assume here that all builtin functions evaluate to scalar types
1553 : : represented by _CST nodes. */
1554 : :
1555 : : static tree
1556 : 10723834 : cxx_eval_builtin_function_call (const constexpr_ctx *ctx, tree t, tree fun,
1557 : : value_cat lval,
1558 : : bool *non_constant_p, bool *overflow_p)
1559 : : {
1560 : 10723834 : const int nargs = call_expr_nargs (t);
1561 : 10723834 : tree *args = (tree *) alloca (nargs * sizeof (tree));
1562 : 10723834 : tree new_call;
1563 : 10723834 : int i;
1564 : :
1565 : : /* Don't fold __builtin_constant_p within a constexpr function. */
1566 : 10723834 : bool bi_const_p = DECL_IS_BUILTIN_CONSTANT_P (fun);
1567 : :
1568 : : /* If we aren't requiring a constant expression, defer __builtin_constant_p
1569 : : in a constexpr function until we have values for the parameters. */
1570 : 728028 : if (bi_const_p
1571 : 728028 : && ctx->manifestly_const_eval != mce_true
1572 : 715189 : && current_function_decl
1573 : 713039 : && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
1574 : : {
1575 : 420025 : *non_constant_p = true;
1576 : 420025 : return t;
1577 : : }
1578 : :
1579 : : /* For __builtin_is_constant_evaluated, defer it if not
1580 : : ctx->manifestly_const_eval (as sometimes we try to constant evaluate
1581 : : without manifestly_const_eval even expressions or parts thereof which
1582 : : will later be manifestly const_eval evaluated), otherwise fold it to
1583 : : true. */
1584 : 10303809 : if (fndecl_built_in_p (fun, CP_BUILT_IN_IS_CONSTANT_EVALUATED,
1585 : : BUILT_IN_FRONTEND))
1586 : : {
1587 : 1957183 : if (ctx->manifestly_const_eval == mce_unknown)
1588 : : {
1589 : 1940688 : *non_constant_p = true;
1590 : 1940688 : return t;
1591 : : }
1592 : 16495 : return constant_boolean_node (ctx->manifestly_const_eval == mce_true,
1593 : 16495 : boolean_type_node);
1594 : : }
1595 : :
1596 : 8346626 : if (fndecl_built_in_p (fun, CP_BUILT_IN_SOURCE_LOCATION, BUILT_IN_FRONTEND))
1597 : : {
1598 : 431 : temp_override<tree> ovr (current_function_decl);
1599 : 431 : if (ctx->call && ctx->call->fundef)
1600 : 137 : current_function_decl = ctx->call->fundef->decl;
1601 : 431 : return fold_builtin_source_location (t);
1602 : 431 : }
1603 : :
1604 : 8346195 : int strops = 0;
1605 : 8346195 : int strret = 0;
1606 : 8346195 : if (fndecl_built_in_p (fun, BUILT_IN_NORMAL))
1607 : 7715183 : switch (DECL_FUNCTION_CODE (fun))
1608 : : {
1609 : : case BUILT_IN_STRLEN:
1610 : : case BUILT_IN_STRNLEN:
1611 : 8346134 : strops = 1;
1612 : : break;
1613 : 105122 : case BUILT_IN_MEMCHR:
1614 : 105122 : case BUILT_IN_STRCHR:
1615 : 105122 : case BUILT_IN_STRRCHR:
1616 : 105122 : strops = 1;
1617 : 105122 : strret = 1;
1618 : 105122 : break;
1619 : 48602 : case BUILT_IN_MEMCMP:
1620 : 48602 : case BUILT_IN_STRCMP:
1621 : 48602 : strops = 2;
1622 : 48602 : break;
1623 : 30282 : case BUILT_IN_STRSTR:
1624 : 30282 : strops = 2;
1625 : 30282 : strret = 1;
1626 : 30282 : break;
1627 : 42 : case BUILT_IN_ASAN_POINTER_COMPARE:
1628 : 42 : case BUILT_IN_ASAN_POINTER_SUBTRACT:
1629 : : /* These builtins shall be ignored during constant expression
1630 : : evaluation. */
1631 : 42 : return void_node;
1632 : 19 : case BUILT_IN_UNREACHABLE:
1633 : 19 : case BUILT_IN_TRAP:
1634 : 19 : if (!*non_constant_p && !ctx->quiet)
1635 : : {
1636 : : /* Do not allow__builtin_unreachable in constexpr function.
1637 : : The __builtin_unreachable call with BUILTINS_LOCATION
1638 : : comes from cp_maybe_instrument_return. */
1639 : 1 : if (EXPR_LOCATION (t) == BUILTINS_LOCATION)
1640 : 0 : error ("%<constexpr%> call flows off the end of the function");
1641 : : else
1642 : 1 : error ("%q+E is not a constant expression", t);
1643 : : }
1644 : 19 : *non_constant_p = true;
1645 : 19 : return t;
1646 : : default:
1647 : : break;
1648 : : }
1649 : :
1650 : : /* Be permissive for arguments to built-ins; __builtin_constant_p should
1651 : : return constant false for a non-constant argument. */
1652 : 8346134 : constexpr_ctx new_ctx = *ctx;
1653 : 8346134 : new_ctx.quiet = true;
1654 : 22559612 : for (i = 0; i < nargs; ++i)
1655 : : {
1656 : 14213478 : tree arg = CALL_EXPR_ARG (t, i);
1657 : 14213478 : tree oarg = arg;
1658 : :
1659 : : /* To handle string built-ins we need to pass ADDR_EXPR<STRING_CST> since
1660 : : expand_builtin doesn't know how to look in the values table. */
1661 : 14213478 : bool strop = i < strops;
1662 : 14213478 : if (strop)
1663 : : {
1664 : 354331 : STRIP_NOPS (arg);
1665 : 354331 : if (TREE_CODE (arg) == ADDR_EXPR)
1666 : 7663 : arg = TREE_OPERAND (arg, 0);
1667 : : else
1668 : : strop = false;
1669 : : }
1670 : :
1671 : : /* If builtin_valid_in_constant_expr_p is true,
1672 : : potential_constant_expression_1 has not recursed into the arguments
1673 : : of the builtin, verify it here. */
1674 : 14213478 : if (!builtin_valid_in_constant_expr_p (fun)
1675 : 14213478 : || potential_constant_expression (arg))
1676 : : {
1677 : 14213347 : bool dummy1 = false, dummy2 = false;
1678 : 14213347 : arg = cxx_eval_constant_expression (&new_ctx, arg, vc_prvalue,
1679 : : &dummy1, &dummy2);
1680 : : }
1681 : :
1682 : 14213478 : if (bi_const_p)
1683 : : /* For __builtin_constant_p, fold all expressions with constant values
1684 : : even if they aren't C++ constant-expressions. */
1685 : 308003 : arg = cp_fold_rvalue (arg);
1686 : 13905475 : else if (strop)
1687 : : {
1688 : 7663 : if (TREE_CODE (arg) == CONSTRUCTOR)
1689 : 100 : arg = braced_lists_to_strings (TREE_TYPE (arg), arg);
1690 : 7663 : if (TREE_CODE (arg) == STRING_CST)
1691 : 2801 : arg = build_address (arg);
1692 : : else
1693 : : arg = oarg;
1694 : : }
1695 : :
1696 : 14213478 : args[i] = arg;
1697 : : }
1698 : :
1699 : 8346134 : bool save_ffbcp = force_folding_builtin_constant_p;
1700 : 8346134 : force_folding_builtin_constant_p |= ctx->manifestly_const_eval == mce_true;
1701 : 8346134 : tree save_cur_fn = current_function_decl;
1702 : : /* Return name of ctx->call->fundef->decl for __builtin_FUNCTION (). */
1703 : 8346134 : if (fndecl_built_in_p (fun, BUILT_IN_FUNCTION)
1704 : 38 : && ctx->call
1705 : 8346138 : && ctx->call->fundef)
1706 : 4 : current_function_decl = ctx->call->fundef->decl;
1707 : 8346134 : if (fndecl_built_in_p (fun,
1708 : : CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS,
1709 : : BUILT_IN_FRONTEND))
1710 : : {
1711 : 348 : location_t loc = EXPR_LOCATION (t);
1712 : 348 : if (nargs >= 1)
1713 : 345 : VERIFY_CONSTANT (args[0]);
1714 : 143 : new_call
1715 : 143 : = fold_builtin_is_pointer_inverconvertible_with_class (loc, nargs,
1716 : : args);
1717 : : }
1718 : 8345786 : else if (fndecl_built_in_p (fun,
1719 : : CP_BUILT_IN_IS_CORRESPONDING_MEMBER,
1720 : : BUILT_IN_FRONTEND))
1721 : : {
1722 : 459 : location_t loc = EXPR_LOCATION (t);
1723 : 459 : if (nargs >= 2)
1724 : : {
1725 : 453 : VERIFY_CONSTANT (args[0]);
1726 : 231 : VERIFY_CONSTANT (args[1]);
1727 : : }
1728 : 237 : new_call = fold_builtin_is_corresponding_member (loc, nargs, args);
1729 : : }
1730 : : else
1731 : 16690654 : new_call = fold_builtin_call_array (EXPR_LOCATION (t), TREE_TYPE (t),
1732 : 8345327 : CALL_EXPR_FN (t), nargs, args);
1733 : 8345707 : current_function_decl = save_cur_fn;
1734 : 8345707 : force_folding_builtin_constant_p = save_ffbcp;
1735 : 8345707 : if (new_call == NULL)
1736 : : {
1737 : 6157311 : if (!*non_constant_p && !ctx->quiet)
1738 : : {
1739 : 0 : new_call = build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t),
1740 : 0 : CALL_EXPR_FN (t), nargs, args);
1741 : 0 : error ("%q+E is not a constant expression", new_call);
1742 : : }
1743 : 6157311 : *non_constant_p = true;
1744 : 6157311 : return t;
1745 : : }
1746 : :
1747 : 2188396 : if (!potential_constant_expression (new_call))
1748 : : {
1749 : 577 : if (!*non_constant_p && !ctx->quiet)
1750 : 4 : error ("%q+E is not a constant expression", new_call);
1751 : 577 : *non_constant_p = true;
1752 : 577 : return t;
1753 : : }
1754 : :
1755 : 2187819 : if (strret)
1756 : : {
1757 : : /* memchr returns a pointer into the first argument, but we replaced the
1758 : : argument above with a STRING_CST; put it back it now. */
1759 : 117 : tree op = CALL_EXPR_ARG (t, strret-1);
1760 : 117 : STRIP_NOPS (new_call);
1761 : 117 : if (TREE_CODE (new_call) == POINTER_PLUS_EXPR)
1762 : 59 : TREE_OPERAND (new_call, 0) = op;
1763 : 58 : else if (TREE_CODE (new_call) == ADDR_EXPR)
1764 : 2187819 : new_call = op;
1765 : : }
1766 : :
1767 : 2187819 : return cxx_eval_constant_expression (&new_ctx, new_call, lval,
1768 : 2187819 : non_constant_p, overflow_p);
1769 : : }
1770 : :
1771 : : /* TEMP is the constant value of a temporary object of type TYPE. Adjust
1772 : : the type of the value to match. */
1773 : :
1774 : : static tree
1775 : 42860055 : adjust_temp_type (tree type, tree temp)
1776 : : {
1777 : 42860055 : if (same_type_p (TREE_TYPE (temp), type))
1778 : : return temp;
1779 : : /* Avoid wrapping an aggregate value in a NOP_EXPR. */
1780 : 21128445 : if (TREE_CODE (temp) == CONSTRUCTOR)
1781 : : {
1782 : : /* build_constructor wouldn't retain various CONSTRUCTOR flags. */
1783 : 2591968 : tree t = copy_node (temp);
1784 : 2591968 : TREE_TYPE (t) = type;
1785 : 2591968 : return t;
1786 : : }
1787 : 18536477 : if (TREE_CODE (temp) == EMPTY_CLASS_EXPR)
1788 : 0 : return build0 (EMPTY_CLASS_EXPR, type);
1789 : 18536477 : gcc_assert (scalarish_type_p (type));
1790 : : /* Now we know we're dealing with a scalar, and a prvalue of non-class
1791 : : type is cv-unqualified. */
1792 : 18536477 : return cp_fold_convert (cv_unqualified (type), temp);
1793 : : }
1794 : :
1795 : : /* If T is a CONSTRUCTOR, return an unshared copy of T and any
1796 : : sub-CONSTRUCTORs. Otherwise return T.
1797 : :
1798 : : We use this whenever we initialize an object as a whole, whether it's a
1799 : : parameter, a local variable, or a subobject, so that subsequent
1800 : : modifications don't affect other places where it was used. */
1801 : :
1802 : : tree
1803 : 31844067 : unshare_constructor (tree t MEM_STAT_DECL)
1804 : : {
1805 : 31844067 : if (!t || TREE_CODE (t) != CONSTRUCTOR)
1806 : : return t;
1807 : 7324974 : auto_vec <tree*, 4> ptrs;
1808 : 7324974 : ptrs.safe_push (&t);
1809 : 7324974 : while (!ptrs.is_empty ())
1810 : : {
1811 : 8265577 : tree *p = ptrs.pop ();
1812 : 8265577 : tree n = copy_node (*p PASS_MEM_STAT);
1813 : 12002854 : CONSTRUCTOR_ELTS (n) = vec_safe_copy (CONSTRUCTOR_ELTS (*p) PASS_MEM_STAT);
1814 : 8265577 : *p = n;
1815 : 8265577 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (n);
1816 : 8265577 : constructor_elt *ce;
1817 : 31772669 : for (HOST_WIDE_INT i = 0; vec_safe_iterate (v, i, &ce); ++i)
1818 : 7916541 : if (ce->value && TREE_CODE (ce->value) == CONSTRUCTOR)
1819 : 940603 : ptrs.safe_push (&ce->value);
1820 : : }
1821 : 7324974 : return t;
1822 : 7324974 : }
1823 : :
1824 : : /* If T is a CONSTRUCTOR, ggc_free T and any sub-CONSTRUCTORs. */
1825 : :
1826 : : static void
1827 : 731887 : free_constructor (tree t)
1828 : : {
1829 : 731887 : if (!t || TREE_CODE (t) != CONSTRUCTOR)
1830 : 0 : return;
1831 : 731887 : releasing_vec ctors;
1832 : 731887 : vec_safe_push (ctors, t);
1833 : 1480330 : while (!ctors->is_empty ())
1834 : : {
1835 : 748443 : tree c = ctors->pop ();
1836 : 748443 : if (vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (c))
1837 : : {
1838 : : constructor_elt *ce;
1839 : 151087 : for (HOST_WIDE_INT i = 0; vec_safe_iterate (elts, i, &ce); ++i)
1840 : 99849 : if (ce->value && TREE_CODE (ce->value) == CONSTRUCTOR)
1841 : 16556 : vec_safe_push (ctors, ce->value);
1842 : 51238 : ggc_free (elts);
1843 : : }
1844 : 748443 : ggc_free (c);
1845 : : }
1846 : 731887 : }
1847 : :
1848 : : /* Helper function of cxx_bind_parameters_in_call. Return non-NULL
1849 : : if *TP is address of a static variable (or part of it) currently being
1850 : : constructed or of a heap artificial variable. */
1851 : :
1852 : : static tree
1853 : 11245022 : addr_of_non_const_var (tree *tp, int *walk_subtrees, void *data)
1854 : : {
1855 : 11245022 : if (TREE_CODE (*tp) == ADDR_EXPR)
1856 : 1654022 : if (tree var = get_base_address (TREE_OPERAND (*tp, 0)))
1857 : 1654022 : if (VAR_P (var) && TREE_STATIC (var))
1858 : : {
1859 : 712931 : if (DECL_NAME (var) == heap_uninit_identifier
1860 : 712931 : || DECL_NAME (var) == heap_identifier
1861 : 712931 : || DECL_NAME (var) == heap_vec_uninit_identifier
1862 : 1425862 : || DECL_NAME (var) == heap_vec_identifier)
1863 : : return var;
1864 : :
1865 : 712931 : constexpr_global_ctx *global = (constexpr_global_ctx *) data;
1866 : 712931 : if (global->get_value (var))
1867 : : return var;
1868 : : }
1869 : 11124101 : if (TYPE_P (*tp))
1870 : 1187 : *walk_subtrees = false;
1871 : : return NULL_TREE;
1872 : : }
1873 : :
1874 : : /* Subroutine of cxx_eval_call_expression.
1875 : : We are processing a call expression (either CALL_EXPR or
1876 : : AGGR_INIT_EXPR) in the context of CTX. Evaluate
1877 : : all arguments and bind their values to correspondings
1878 : : parameters, making up the NEW_CALL context. */
1879 : :
1880 : : static tree
1881 : 70489897 : cxx_bind_parameters_in_call (const constexpr_ctx *ctx, tree t, tree fun,
1882 : : bool *non_constant_p, bool *overflow_p,
1883 : : bool *non_constant_args)
1884 : : {
1885 : 70489897 : const int nargs = call_expr_nargs (t);
1886 : 70489897 : tree parms = DECL_ARGUMENTS (fun);
1887 : 70489897 : int i;
1888 : : /* We don't record ellipsis args below. */
1889 : 70489897 : int nparms = list_length (parms);
1890 : 70489897 : int nbinds = nargs < nparms ? nargs : nparms;
1891 : 70489897 : tree binds = make_tree_vec (nbinds);
1892 : :
1893 : : /* The call is not a constant expression if it involves the cdtor for a type
1894 : : with virtual bases. */
1895 : 70489897 : if (DECL_HAS_IN_CHARGE_PARM_P (fun) || DECL_HAS_VTT_PARM_P (fun))
1896 : : {
1897 : 24 : if (!ctx->quiet)
1898 : : {
1899 : 3 : error_at (cp_expr_loc_or_input_loc (t),
1900 : : "call to non-%<constexpr%> function %qD", fun);
1901 : 3 : explain_invalid_constexpr_fn (fun);
1902 : : }
1903 : 24 : *non_constant_p = true;
1904 : 24 : return binds;
1905 : : }
1906 : :
1907 : 107812016 : for (i = 0; i < nargs; ++i)
1908 : : {
1909 : 69224571 : tree x, arg;
1910 : 69224571 : tree type = parms ? TREE_TYPE (parms) : void_type_node;
1911 : 69224571 : if (parms && DECL_BY_REFERENCE (parms))
1912 : 6170 : type = TREE_TYPE (type);
1913 : 69224571 : x = get_nth_callarg (t, i);
1914 : : /* For member function, the first argument is a pointer to the implied
1915 : : object. For a constructor, it might still be a dummy object, in
1916 : : which case we get the real argument from ctx. */
1917 : 111783262 : if (i == 0 && DECL_CONSTRUCTOR_P (fun)
1918 : 77517394 : && is_dummy_object (x))
1919 : : {
1920 : 3635238 : x = ctx->object;
1921 : 3635238 : x = build_address (x);
1922 : : }
1923 : 69224571 : if (TREE_ADDRESSABLE (type))
1924 : : {
1925 : : /* Undo convert_for_arg_passing work here. */
1926 : 9812 : x = convert_from_reference (x);
1927 : 9812 : arg = cxx_eval_constant_expression (ctx, x, vc_glvalue,
1928 : : non_constant_p, overflow_p);
1929 : : }
1930 : : else
1931 : : /* Normally we would strip a TARGET_EXPR in an initialization context
1932 : : such as this, but here we do the elision differently: we keep the
1933 : : TARGET_EXPR, and use its CONSTRUCTOR as the value of the parm. */
1934 : 69214759 : arg = cxx_eval_constant_expression (ctx, x, vc_prvalue,
1935 : : non_constant_p, overflow_p);
1936 : : /* Check we aren't dereferencing a null pointer when calling a non-static
1937 : : member function, which is undefined behaviour. */
1938 : 55891631 : if (i == 0 && DECL_OBJECT_MEMBER_FUNCTION_P (fun)
1939 : 29807484 : && integer_zerop (arg)
1940 : : /* But ignore calls from within compiler-generated code, to handle
1941 : : cases like lambda function pointer conversion operator thunks
1942 : : which pass NULL as the 'this' pointer. */
1943 : 69266552 : && !(TREE_CODE (t) == CALL_EXPR && CALL_FROM_THUNK_P (t)))
1944 : : {
1945 : 286 : if (!ctx->quiet)
1946 : 6 : error_at (cp_expr_loc_or_input_loc (x),
1947 : : "dereferencing a null pointer");
1948 : 286 : *non_constant_p = true;
1949 : : }
1950 : : /* Don't VERIFY_CONSTANT here. */
1951 : 69224571 : if (*non_constant_p && ctx->quiet)
1952 : : break;
1953 : : /* Just discard ellipsis args after checking their constantitude. */
1954 : 37322143 : if (!parms)
1955 : 394 : continue;
1956 : :
1957 : 37321749 : if (!*non_constant_p)
1958 : : {
1959 : : /* Make sure the binding has the same type as the parm. But
1960 : : only for constant args. */
1961 : 37321509 : if (TREE_ADDRESSABLE (type))
1962 : : {
1963 : 6494 : if (!same_type_p (type, TREE_TYPE (arg)))
1964 : : {
1965 : 9 : arg = build_fold_addr_expr (arg);
1966 : 9 : arg = cp_fold_convert (build_reference_type (type), arg);
1967 : 9 : arg = convert_from_reference (arg);
1968 : : }
1969 : : }
1970 : 37315015 : else if (!TYPE_REF_P (type))
1971 : 28335930 : arg = adjust_temp_type (type, arg);
1972 : 37321509 : if (!TREE_CONSTANT (arg))
1973 : 26409592 : *non_constant_args = true;
1974 : 10911917 : else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
1975 : : /* The destructor needs to see any modifications the callee makes
1976 : : to the argument. */
1977 : 0 : *non_constant_args = true;
1978 : : /* If arg is or contains address of a heap artificial variable or
1979 : : of a static variable being constructed, avoid caching the
1980 : : function call, as those variables might be modified by the
1981 : : function, or might be modified by the callers in between
1982 : : the cached function and just read by the function. */
1983 : 10911917 : else if (!*non_constant_args
1984 : 10911917 : && cp_walk_tree (&arg, addr_of_non_const_var, ctx->global,
1985 : : NULL))
1986 : 120921 : *non_constant_args = true;
1987 : :
1988 : : /* For virtual calls, adjust the this argument, so that it is
1989 : : the object on which the method is called, rather than
1990 : : one of its bases. */
1991 : 37321509 : if (i == 0 && DECL_VIRTUAL_P (fun))
1992 : : {
1993 : 16198 : tree addr = arg;
1994 : 16198 : STRIP_NOPS (addr);
1995 : 16198 : if (TREE_CODE (addr) == ADDR_EXPR)
1996 : : {
1997 : 16189 : tree obj = TREE_OPERAND (addr, 0);
1998 : 16189 : while (TREE_CODE (obj) == COMPONENT_REF
1999 : 6749 : && DECL_FIELD_IS_BASE (TREE_OPERAND (obj, 1))
2000 : 22942 : && !same_type_ignoring_top_level_qualifiers_p
2001 : 6736 : (TREE_TYPE (obj), DECL_CONTEXT (fun)))
2002 : 17 : obj = TREE_OPERAND (obj, 0);
2003 : 16189 : if (obj != TREE_OPERAND (addr, 0))
2004 : 14 : arg = build_fold_addr_expr_with_type (obj,
2005 : : TREE_TYPE (arg));
2006 : : }
2007 : : }
2008 : 37321509 : TREE_VEC_ELT (binds, i) = arg;
2009 : : }
2010 : 37321749 : parms = TREE_CHAIN (parms);
2011 : : }
2012 : :
2013 : : return binds;
2014 : : }
2015 : :
2016 : : /* Variables and functions to manage constexpr call expansion context.
2017 : : These do not need to be marked for PCH or GC. */
2018 : :
2019 : : /* FIXME remember and print actual constant arguments. */
2020 : : static vec<tree> call_stack;
2021 : : static int call_stack_tick;
2022 : : static int last_cx_error_tick;
2023 : :
2024 : : static int
2025 : 38281134 : push_cx_call_context (tree call)
2026 : : {
2027 : 38281134 : ++call_stack_tick;
2028 : 38281134 : if (!EXPR_HAS_LOCATION (call))
2029 : 17021 : SET_EXPR_LOCATION (call, input_location);
2030 : 38281134 : call_stack.safe_push (call);
2031 : 38281134 : int len = call_stack.length ();
2032 : 38281134 : if (len > max_constexpr_depth)
2033 : 30 : return false;
2034 : : return len;
2035 : : }
2036 : :
2037 : : static void
2038 : 38281134 : pop_cx_call_context (void)
2039 : : {
2040 : 38281134 : ++call_stack_tick;
2041 : 38281134 : call_stack.pop ();
2042 : 38281134 : }
2043 : :
2044 : : vec<tree>
2045 : 210143 : cx_error_context (void)
2046 : : {
2047 : 210143 : vec<tree> r = vNULL;
2048 : 210143 : if (call_stack_tick != last_cx_error_tick
2049 : 210143 : && !call_stack.is_empty ())
2050 : : r = call_stack;
2051 : 210143 : last_cx_error_tick = call_stack_tick;
2052 : 210143 : return r;
2053 : : }
2054 : :
2055 : : /* E is an operand of a failed assertion, fold it either with or without
2056 : : constexpr context. */
2057 : :
2058 : : static tree
2059 : 576 : fold_operand (tree e, const constexpr_ctx *ctx)
2060 : : {
2061 : 576 : if (ctx)
2062 : : {
2063 : 70 : bool new_non_constant_p = false, new_overflow_p = false;
2064 : 70 : e = cxx_eval_constant_expression (ctx, e, vc_prvalue,
2065 : : &new_non_constant_p,
2066 : : &new_overflow_p);
2067 : : }
2068 : : else
2069 : 506 : e = fold_non_dependent_expr (e, tf_none, /*manifestly_const_eval=*/true);
2070 : 576 : return e;
2071 : : }
2072 : :
2073 : : /* If we have a condition in conjunctive normal form (CNF), find the first
2074 : : failing clause. In other words, given an expression like
2075 : :
2076 : : true && true && false && true && false
2077 : :
2078 : : return the first 'false'. EXPR is the expression. */
2079 : :
2080 : : static tree
2081 : 388 : find_failing_clause_r (const constexpr_ctx *ctx, tree expr)
2082 : : {
2083 : 500 : if (TREE_CODE (expr) == TRUTH_ANDIF_EXPR)
2084 : : {
2085 : : /* First check the left side... */
2086 : 220 : tree e = find_failing_clause_r (ctx, TREE_OPERAND (expr, 0));
2087 : 220 : if (e == NULL_TREE)
2088 : : /* ...if we didn't find a false clause, check the right side. */
2089 : 112 : e = find_failing_clause_r (ctx, TREE_OPERAND (expr, 1));
2090 : : return e;
2091 : : }
2092 : 280 : tree e = contextual_conv_bool (expr, tf_none);
2093 : 280 : e = fold_operand (e, ctx);
2094 : 280 : if (integer_zerop (e))
2095 : : /* This is the failing clause. */
2096 : 168 : return expr;
2097 : : return NULL_TREE;
2098 : : }
2099 : :
2100 : : /* Wrapper for find_failing_clause_r. */
2101 : :
2102 : : tree
2103 : 1310 : find_failing_clause (const constexpr_ctx *ctx, tree expr)
2104 : : {
2105 : 1310 : if (TREE_CODE (expr) == TRUTH_ANDIF_EXPR)
2106 : 168 : if (tree e = find_failing_clause_r (ctx, expr))
2107 : 1310 : expr = e;
2108 : 1310 : return expr;
2109 : : }
2110 : :
2111 : : /* Emit additional diagnostics for failing condition BAD.
2112 : : Used by finish_static_assert and IFN_ASSUME constexpr diagnostics.
2113 : : If SHOW_EXPR_P is true, print the condition (because it was
2114 : : instantiation-dependent). */
2115 : :
2116 : : void
2117 : 1310 : diagnose_failing_condition (tree bad, location_t cloc, bool show_expr_p,
2118 : : const constexpr_ctx *ctx /* = nullptr */)
2119 : : {
2120 : : /* Nobody wants to see the artificial (bool) cast. */
2121 : 1310 : bad = tree_strip_nop_conversions (bad);
2122 : 1310 : if (TREE_CODE (bad) == CLEANUP_POINT_EXPR)
2123 : 4 : bad = TREE_OPERAND (bad, 0);
2124 : :
2125 : : /* Actually explain the failure if this is a concept check or a
2126 : : requires-expression. */
2127 : 1310 : if (concept_check_p (bad) || TREE_CODE (bad) == REQUIRES_EXPR)
2128 : 243 : diagnose_constraints (cloc, bad, NULL_TREE);
2129 : 1067 : else if (COMPARISON_CLASS_P (bad)
2130 : 1067 : && ARITHMETIC_TYPE_P (TREE_TYPE (TREE_OPERAND (bad, 0))))
2131 : : {
2132 : 148 : tree op0 = fold_operand (TREE_OPERAND (bad, 0), ctx);
2133 : 148 : tree op1 = fold_operand (TREE_OPERAND (bad, 1), ctx);
2134 : 148 : tree cond = build2 (TREE_CODE (bad), boolean_type_node, op0, op1);
2135 : 148 : inform (cloc, "the comparison reduces to %qE", cond);
2136 : : }
2137 : 919 : else if (show_expr_p)
2138 : 731 : inform (cloc, "%qE evaluates to false", bad);
2139 : 1310 : }
2140 : :
2141 : : /* Process an assert/assume of ORIG_ARG. If it's not supposed to be evaluated,
2142 : : do it without changing the current evaluation state. If it evaluates to
2143 : : false, complain and return false; otherwise, return true. */
2144 : :
2145 : : static bool
2146 : 224014 : cxx_eval_assert (const constexpr_ctx *ctx, tree arg, const char *msg,
2147 : : location_t loc, bool evaluated,
2148 : : bool *non_constant_p, bool *overflow_p)
2149 : : {
2150 : 224014 : if (*non_constant_p)
2151 : : return true;
2152 : :
2153 : 224014 : tree eval;
2154 : 224014 : if (!evaluated)
2155 : : {
2156 : 223943 : if (!potential_rvalue_constant_expression (arg))
2157 : 19 : return true;
2158 : :
2159 : 223924 : constexpr_ctx new_ctx = *ctx;
2160 : 223924 : new_ctx.quiet = true;
2161 : 223924 : bool new_non_constant_p = false, new_overflow_p = false;
2162 : : /* Avoid modification of existing values. */
2163 : 223924 : modifiable_tracker ms (new_ctx.global);
2164 : 223924 : eval = cxx_eval_constant_expression (&new_ctx, arg, vc_prvalue,
2165 : : &new_non_constant_p,
2166 : : &new_overflow_p);
2167 : 223924 : }
2168 : : else
2169 : 71 : eval = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
2170 : : non_constant_p,
2171 : : overflow_p);
2172 : 223995 : if (!*non_constant_p && integer_zerop (eval))
2173 : : {
2174 : 99 : if (!ctx->quiet)
2175 : : {
2176 : : /* See if we can find which clause was failing
2177 : : (for logical AND). */
2178 : 32 : tree bad = find_failing_clause (ctx, arg);
2179 : : /* If not, or its location is unusable, fall back to the
2180 : : previous location. */
2181 : 32 : location_t cloc = cp_expr_loc_or_loc (bad, loc);
2182 : :
2183 : : /* Report the error. */
2184 : 32 : auto_diagnostic_group d;
2185 : 32 : error_at (cloc, msg);
2186 : 32 : diagnose_failing_condition (bad, cloc, true, ctx);
2187 : 32 : return bad;
2188 : 32 : }
2189 : 67 : *non_constant_p = true;
2190 : 67 : return false;
2191 : : }
2192 : :
2193 : : return true;
2194 : : }
2195 : :
2196 : : /* Evaluate a call T to a GCC internal function when possible and return
2197 : : the evaluated result or, under the control of CTX, give an error, set
2198 : : NON_CONSTANT_P, and return the unevaluated call T otherwise. */
2199 : :
2200 : : static tree
2201 : 270943 : cxx_eval_internal_function (const constexpr_ctx *ctx, tree t,
2202 : : value_cat lval,
2203 : : bool *non_constant_p, bool *overflow_p)
2204 : : {
2205 : 270943 : enum tree_code opcode = ERROR_MARK;
2206 : :
2207 : 270943 : switch (CALL_EXPR_IFN (t))
2208 : : {
2209 : 149 : case IFN_UBSAN_NULL:
2210 : 149 : case IFN_UBSAN_BOUNDS:
2211 : 149 : case IFN_UBSAN_VPTR:
2212 : 149 : case IFN_FALLTHROUGH:
2213 : 149 : return void_node;
2214 : :
2215 : 223927 : case IFN_ASSUME:
2216 : 223927 : if (!cxx_eval_assert (ctx, CALL_EXPR_ARG (t, 0),
2217 : : G_("failed %<assume%> attribute assumption"),
2218 : 223927 : EXPR_LOCATION (t), /*eval*/false,
2219 : : non_constant_p, overflow_p))
2220 : : return t;
2221 : 223898 : return void_node;
2222 : :
2223 : : case IFN_ADD_OVERFLOW:
2224 : : opcode = PLUS_EXPR;
2225 : : break;
2226 : 339 : case IFN_SUB_OVERFLOW:
2227 : 339 : opcode = MINUS_EXPR;
2228 : 339 : break;
2229 : 45967 : case IFN_MUL_OVERFLOW:
2230 : 45967 : opcode = MULT_EXPR;
2231 : 45967 : break;
2232 : :
2233 : 73 : case IFN_LAUNDER:
2234 : 73 : return cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0),
2235 : : vc_prvalue, non_constant_p,
2236 : 73 : overflow_p);
2237 : :
2238 : 30 : case IFN_VEC_CONVERT:
2239 : 30 : {
2240 : 30 : tree arg = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0),
2241 : : vc_prvalue, non_constant_p,
2242 : : overflow_p);
2243 : 30 : if (TREE_CODE (arg) == VECTOR_CST)
2244 : 19 : if (tree r = fold_const_call (CFN_VEC_CONVERT, TREE_TYPE (t), arg))
2245 : : return r;
2246 : : }
2247 : : /* FALLTHRU */
2248 : :
2249 : 16 : default:
2250 : 16 : if (!ctx->quiet)
2251 : 0 : error_at (cp_expr_loc_or_input_loc (t),
2252 : : "call to internal function %qE", t);
2253 : 16 : *non_constant_p = true;
2254 : 16 : return t;
2255 : : }
2256 : :
2257 : : /* Evaluate constant arguments using OPCODE and return a complex
2258 : : number containing the result and the overflow bit. */
2259 : 46764 : tree arg0 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), lval,
2260 : : non_constant_p, overflow_p);
2261 : 46764 : tree arg1 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 1), lval,
2262 : : non_constant_p, overflow_p);
2263 : :
2264 : 46764 : if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2265 : : {
2266 : 2 : location_t loc = cp_expr_loc_or_input_loc (t);
2267 : 2 : tree type = TREE_TYPE (TREE_TYPE (t));
2268 : 2 : tree result = fold_binary_loc (loc, opcode, type,
2269 : : fold_convert_loc (loc, type, arg0),
2270 : : fold_convert_loc (loc, type, arg1));
2271 : 2 : tree ovf
2272 : 2 : = build_int_cst (type, arith_overflowed_p (opcode, type, arg0, arg1));
2273 : : /* Reset TREE_OVERFLOW to avoid warnings for the overflow. */
2274 : 2 : if (TREE_OVERFLOW (result))
2275 : 0 : TREE_OVERFLOW (result) = 0;
2276 : :
2277 : 2 : return build_complex (TREE_TYPE (t), result, ovf);
2278 : : }
2279 : :
2280 : 46762 : *non_constant_p = true;
2281 : 46762 : return t;
2282 : : }
2283 : :
2284 : : /* Clean CONSTRUCTOR_NO_CLEARING from CTOR and its sub-aggregates. */
2285 : :
2286 : : static void
2287 : 3854952 : clear_no_implicit_zero (tree ctor)
2288 : : {
2289 : 3854952 : if (CONSTRUCTOR_NO_CLEARING (ctor))
2290 : : {
2291 : 959310 : CONSTRUCTOR_NO_CLEARING (ctor) = false;
2292 : 4479025 : for (auto &e: CONSTRUCTOR_ELTS (ctor))
2293 : 1621775 : if (TREE_CODE (e.value) == CONSTRUCTOR)
2294 : 257373 : clear_no_implicit_zero (e.value);
2295 : : }
2296 : 3854952 : }
2297 : :
2298 : : /* Complain about a const object OBJ being modified in a constant expression.
2299 : : EXPR is the MODIFY_EXPR expression performing the modification. */
2300 : :
2301 : : static void
2302 : 63 : modifying_const_object_error (tree expr, tree obj)
2303 : : {
2304 : 63 : location_t loc = cp_expr_loc_or_input_loc (expr);
2305 : 63 : auto_diagnostic_group d;
2306 : 126 : error_at (loc, "modifying a const object %qE is not allowed in "
2307 : 63 : "a constant expression", TREE_OPERAND (expr, 0));
2308 : :
2309 : : /* Find the underlying object that was declared as const. */
2310 : 63 : location_t decl_loc = UNKNOWN_LOCATION;
2311 : 138 : for (tree probe = obj; decl_loc == UNKNOWN_LOCATION; )
2312 : 75 : switch (TREE_CODE (probe))
2313 : : {
2314 : 39 : case BIT_FIELD_REF:
2315 : 39 : case COMPONENT_REF:
2316 : 39 : {
2317 : 39 : tree elt = TREE_OPERAND (probe, 1);
2318 : 39 : if (CP_TYPE_CONST_P (TREE_TYPE (elt)))
2319 : 27 : decl_loc = DECL_SOURCE_LOCATION (elt);
2320 : 39 : probe = TREE_OPERAND (probe, 0);
2321 : : }
2322 : 39 : break;
2323 : :
2324 : 0 : case ARRAY_REF:
2325 : 0 : case REALPART_EXPR:
2326 : 0 : case IMAGPART_EXPR:
2327 : 0 : probe = TREE_OPERAND (probe, 0);
2328 : 0 : break;
2329 : :
2330 : 36 : default:
2331 : 36 : decl_loc = location_of (probe);
2332 : 36 : break;
2333 : : }
2334 : 63 : inform (decl_loc, "originally declared %<const%> here");
2335 : 63 : }
2336 : :
2337 : : /* Return true if FNDECL is a replaceable global allocation function that
2338 : : should be useable during constant expression evaluation. */
2339 : :
2340 : : static inline bool
2341 : 29855822 : cxx_replaceable_global_alloc_fn (tree fndecl)
2342 : : {
2343 : 29855822 : return (cxx_dialect >= cxx20
2344 : 8678788 : && IDENTIFIER_NEWDEL_OP_P (DECL_NAME (fndecl))
2345 : 287003 : && CP_DECL_CONTEXT (fndecl) == global_namespace
2346 : 30142678 : && (DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl)
2347 : 100603 : || DECL_IS_OPERATOR_DELETE_P (fndecl)));
2348 : : }
2349 : :
2350 : : /* Return true if FNDECL is a placement new function that should be
2351 : : useable during constant expression evaluation of std::construct_at. */
2352 : :
2353 : : static inline bool
2354 : 29666102 : cxx_placement_new_fn (tree fndecl)
2355 : : {
2356 : 29666102 : return (cxx_dialect >= cxx20 && std_placement_new_fn_p (fndecl));
2357 : : }
2358 : :
2359 : : /* Return true if FNDECL is std::construct_at. */
2360 : :
2361 : : static inline bool
2362 : 94112 : is_std_construct_at (tree fndecl)
2363 : : {
2364 : 94112 : if (!decl_in_std_namespace_p (fndecl))
2365 : : return false;
2366 : :
2367 : 88939 : tree name = DECL_NAME (fndecl);
2368 : 88939 : return name && id_equal (name, "construct_at");
2369 : : }
2370 : :
2371 : : /* Overload for the above taking constexpr_call*. */
2372 : :
2373 : : static inline bool
2374 : 60989 : is_std_construct_at (const constexpr_call *call)
2375 : : {
2376 : 60989 : return (call
2377 : 44424 : && call->fundef
2378 : 105413 : && is_std_construct_at (call->fundef->decl));
2379 : : }
2380 : :
2381 : : /* True if CTX is an instance of std::NAME class. */
2382 : :
2383 : : bool
2384 : 3823746 : is_std_class (tree ctx, const char *name)
2385 : : {
2386 : 3823746 : if (ctx == NULL_TREE || !CLASS_TYPE_P (ctx) || !TYPE_MAIN_DECL (ctx))
2387 : : return false;
2388 : :
2389 : 3822894 : tree decl = TYPE_MAIN_DECL (ctx);
2390 : 3822894 : tree dname = DECL_NAME (decl);
2391 : 3822894 : if (dname == NULL_TREE || !id_equal (dname, name))
2392 : : return false;
2393 : :
2394 : 184518 : return decl_in_std_namespace_p (decl);
2395 : : }
2396 : :
2397 : : /* True if CTX is an instance of std::allocator. */
2398 : :
2399 : : bool
2400 : 69452 : is_std_allocator (tree ctx)
2401 : : {
2402 : 69452 : return is_std_class (ctx, "allocator");
2403 : : }
2404 : :
2405 : : /* Return true if FNDECL is std::allocator<T>::{,de}allocate. */
2406 : :
2407 : : static inline bool
2408 : 76205 : is_std_allocator_allocate (tree fndecl)
2409 : : {
2410 : 76205 : tree name = DECL_NAME (fndecl);
2411 : 76205 : if (name == NULL_TREE
2412 : 76205 : || !(id_equal (name, "allocate") || id_equal (name, "deallocate")))
2413 : : return false;
2414 : :
2415 : 65333 : return is_std_allocator (DECL_CONTEXT (fndecl));
2416 : : }
2417 : :
2418 : : /* Overload for the above taking constexpr_call*. */
2419 : :
2420 : : static inline bool
2421 : 26710 : is_std_allocator_allocate (const constexpr_call *call)
2422 : : {
2423 : 26710 : return (call
2424 : 6802 : && call->fundef
2425 : 33512 : && is_std_allocator_allocate (call->fundef->decl));
2426 : : }
2427 : :
2428 : : /* Return true if FNDECL is std::source_location::current. */
2429 : :
2430 : : static inline bool
2431 : 4047 : is_std_source_location_current (tree fndecl)
2432 : : {
2433 : 4047 : if (!decl_in_std_namespace_p (fndecl))
2434 : : return false;
2435 : :
2436 : 804 : tree name = DECL_NAME (fndecl);
2437 : 804 : if (name == NULL_TREE || !id_equal (name, "current"))
2438 : : return false;
2439 : :
2440 : 159 : tree ctx = DECL_CONTEXT (fndecl);
2441 : 159 : if (ctx == NULL_TREE || !CLASS_TYPE_P (ctx) || !TYPE_MAIN_DECL (ctx))
2442 : : return false;
2443 : :
2444 : 159 : name = DECL_NAME (TYPE_MAIN_DECL (ctx));
2445 : 159 : return name && id_equal (name, "source_location");
2446 : : }
2447 : :
2448 : : /* Overload for the above taking constexpr_call*. */
2449 : :
2450 : : static inline bool
2451 : 4666 : is_std_source_location_current (const constexpr_call *call)
2452 : : {
2453 : 4666 : return (call
2454 : 4047 : && call->fundef
2455 : 8713 : && is_std_source_location_current (call->fundef->decl));
2456 : : }
2457 : :
2458 : : /* Return true if FNDECL is __dynamic_cast. */
2459 : :
2460 : : static inline bool
2461 : 29628215 : cxx_dynamic_cast_fn_p (tree fndecl)
2462 : : {
2463 : 29628215 : return (cxx_dialect >= cxx20
2464 : 8451172 : && id_equal (DECL_NAME (fndecl), "__dynamic_cast")
2465 : 29631939 : && CP_DECL_CONTEXT (fndecl) == abi_node);
2466 : : }
2467 : :
2468 : : /* Often, we have an expression in the form of address + offset, e.g.
2469 : : "&_ZTV1A + 16". Extract the object from it, i.e. "_ZTV1A". */
2470 : :
2471 : : static tree
2472 : 3181 : extract_obj_from_addr_offset (tree expr)
2473 : : {
2474 : 3181 : if (TREE_CODE (expr) == POINTER_PLUS_EXPR)
2475 : 1516 : expr = TREE_OPERAND (expr, 0);
2476 : 3181 : STRIP_NOPS (expr);
2477 : 3181 : if (TREE_CODE (expr) == ADDR_EXPR)
2478 : 3174 : expr = TREE_OPERAND (expr, 0);
2479 : 3181 : return expr;
2480 : : }
2481 : :
2482 : : /* Given a PATH like
2483 : :
2484 : : g.D.2181.D.2154.D.2102.D.2093
2485 : :
2486 : : find a component with type TYPE. Return NULL_TREE if not found, and
2487 : : error_mark_node if the component is not accessible. If STOP is non-null,
2488 : : this function will return NULL_TREE if STOP is found before TYPE. */
2489 : :
2490 : : static tree
2491 : 1561 : get_component_with_type (tree path, tree type, tree stop)
2492 : : {
2493 : 4779 : while (true)
2494 : : {
2495 : 3170 : if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path), type))
2496 : : /* Found it. */
2497 : : return path;
2498 : 2491 : else if (stop
2499 : 2491 : && (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path),
2500 : : stop)))
2501 : : return NULL_TREE;
2502 : 2446 : else if (TREE_CODE (path) == COMPONENT_REF
2503 : 2446 : && DECL_FIELD_IS_BASE (TREE_OPERAND (path, 1)))
2504 : : {
2505 : : /* We need to check that the component we're accessing is in fact
2506 : : accessible. */
2507 : 2446 : if (TREE_PRIVATE (TREE_OPERAND (path, 1))
2508 : 2446 : || TREE_PROTECTED (TREE_OPERAND (path, 1)))
2509 : 837 : return error_mark_node;
2510 : 1609 : path = TREE_OPERAND (path, 0);
2511 : : }
2512 : : else
2513 : : return NULL_TREE;
2514 : : }
2515 : : }
2516 : :
2517 : : /* Evaluate a call to __dynamic_cast (permitted by P1327R1).
2518 : :
2519 : : The declaration of __dynamic_cast is:
2520 : :
2521 : : void* __dynamic_cast (const void* __src_ptr,
2522 : : const __class_type_info* __src_type,
2523 : : const __class_type_info* __dst_type,
2524 : : ptrdiff_t __src2dst);
2525 : :
2526 : : where src2dst has the following possible values
2527 : :
2528 : : >-1: src_type is a unique public non-virtual base of dst_type
2529 : : dst_ptr + src2dst == src_ptr
2530 : : -1: unspecified relationship
2531 : : -2: src_type is not a public base of dst_type
2532 : : -3: src_type is a multiple public non-virtual base of dst_type
2533 : :
2534 : : Since literal types can't have virtual bases, we only expect hint >=0,
2535 : : -2, or -3. */
2536 : :
2537 : : static tree
2538 : 1756 : cxx_eval_dynamic_cast_fn (const constexpr_ctx *ctx, tree call,
2539 : : bool *non_constant_p, bool *overflow_p)
2540 : : {
2541 : : /* T will be something like
2542 : : __dynamic_cast ((B*) b, &_ZTI1B, &_ZTI1D, 8)
2543 : : dismantle it. */
2544 : 1756 : gcc_assert (call_expr_nargs (call) == 4);
2545 : 1756 : tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
2546 : 1756 : tree obj = CALL_EXPR_ARG (call, 0);
2547 : 1756 : tree type = CALL_EXPR_ARG (call, 2);
2548 : 1756 : HOST_WIDE_INT hint = int_cst_value (CALL_EXPR_ARG (call, 3));
2549 : 1756 : location_t loc = cp_expr_loc_or_input_loc (call);
2550 : :
2551 : : /* Get the target type of the dynamic_cast. */
2552 : 1756 : gcc_assert (TREE_CODE (type) == ADDR_EXPR);
2553 : 1756 : type = TREE_OPERAND (type, 0);
2554 : 1756 : type = TREE_TYPE (DECL_NAME (type));
2555 : :
2556 : : /* TYPE can only be either T* or T&. We can't know which of these it
2557 : : is by looking at TYPE, but OBJ will be "(T*) x" in the first case,
2558 : : and something like "(T*)(T&)(T*) x" in the second case. */
2559 : 1756 : bool reference_p = false;
2560 : 9686 : while (CONVERT_EXPR_P (obj) || TREE_CODE (obj) == SAVE_EXPR)
2561 : : {
2562 : 7930 : reference_p |= TYPE_REF_P (TREE_TYPE (obj));
2563 : 7930 : obj = TREE_OPERAND (obj, 0);
2564 : : }
2565 : :
2566 : : /* Evaluate the object so that we know its dynamic type. */
2567 : 1756 : obj = cxx_eval_constant_expression (ctx, obj, vc_prvalue, non_constant_p,
2568 : : overflow_p);
2569 : 1756 : if (*non_constant_p)
2570 : : return call;
2571 : :
2572 : : /* We expect OBJ to be in form of &d.D.2102 when HINT == 0,
2573 : : but when HINT is > 0, it can also be something like
2574 : : &d.D.2102 + 18446744073709551608, which includes the BINFO_OFFSET. */
2575 : 1665 : obj = extract_obj_from_addr_offset (obj);
2576 : 1665 : const tree objtype = TREE_TYPE (obj);
2577 : : /* If OBJ doesn't refer to a base field, we're done. */
2578 : 3307 : if (tree t = (TREE_CODE (obj) == COMPONENT_REF
2579 : 1665 : ? TREE_OPERAND (obj, 1) : obj))
2580 : 1665 : if (TREE_CODE (t) != FIELD_DECL || !DECL_FIELD_IS_BASE (t))
2581 : : {
2582 : 23 : if (reference_p)
2583 : : {
2584 : 17 : if (!ctx->quiet)
2585 : : {
2586 : 3 : auto_diagnostic_group d;
2587 : 3 : error_at (loc, "reference %<dynamic_cast%> failed");
2588 : 3 : inform (loc, "dynamic type %qT of its operand does "
2589 : : "not have a base class of type %qT",
2590 : : objtype, type);
2591 : 3 : }
2592 : 17 : *non_constant_p = true;
2593 : : }
2594 : 23 : return integer_zero_node;
2595 : : }
2596 : :
2597 : : /* [class.cdtor] When a dynamic_cast is used in a constructor ...
2598 : : or in a destructor ... if the operand of the dynamic_cast refers
2599 : : to the object under construction or destruction, this object is
2600 : : considered to be a most derived object that has the type of the
2601 : : constructor or destructor's class. */
2602 : 1642 : tree vtable = build_vfield_ref (obj, objtype);
2603 : 1642 : vtable = cxx_eval_constant_expression (ctx, vtable, vc_prvalue,
2604 : : non_constant_p, overflow_p);
2605 : 1642 : if (*non_constant_p)
2606 : : return call;
2607 : : /* With -fsanitize=vptr, we initialize all vtable pointers to null,
2608 : : so it's possible that we got a null pointer now. */
2609 : 1528 : if (integer_zerop (vtable))
2610 : : {
2611 : 12 : if (!ctx->quiet)
2612 : 3 : error_at (loc, "virtual table pointer is used uninitialized");
2613 : 12 : *non_constant_p = true;
2614 : 12 : return integer_zero_node;
2615 : : }
2616 : : /* VTABLE will be &_ZTV1A + 16 or similar, get _ZTV1A. */
2617 : 1516 : vtable = extract_obj_from_addr_offset (vtable);
2618 : 1516 : const tree mdtype = DECL_CONTEXT (vtable);
2619 : :
2620 : : /* Given dynamic_cast<T>(v),
2621 : :
2622 : : [expr.dynamic.cast] If C is the class type to which T points or refers,
2623 : : the runtime check logically executes as follows:
2624 : :
2625 : : If, in the most derived object pointed (referred) to by v, v points
2626 : : (refers) to a public base class subobject of a C object, and if only
2627 : : one object of type C is derived from the subobject pointed (referred)
2628 : : to by v the result points (refers) to that C object.
2629 : :
2630 : : In this case, HINT >= 0 or -3. */
2631 : 1516 : if (hint >= 0 || hint == -3)
2632 : : {
2633 : : /* Look for a component with type TYPE. */
2634 : 601 : tree t = get_component_with_type (obj, type, mdtype);
2635 : : /* If not accessible, give an error. */
2636 : 601 : if (t == error_mark_node)
2637 : : {
2638 : 198 : if (reference_p)
2639 : : {
2640 : 162 : if (!ctx->quiet)
2641 : : {
2642 : 18 : auto_diagnostic_group d;
2643 : 18 : error_at (loc, "reference %<dynamic_cast%> failed");
2644 : 18 : inform (loc, "static type %qT of its operand is a "
2645 : : "non-public base class of dynamic type %qT",
2646 : : objtype, type);
2647 : :
2648 : 18 : }
2649 : 162 : *non_constant_p = true;
2650 : : }
2651 : 198 : return integer_zero_node;
2652 : : }
2653 : 403 : else if (t)
2654 : : /* The result points to the TYPE object. */
2655 : 358 : return cp_build_addr_expr (t, complain);
2656 : : /* Else, TYPE was not found, because the HINT turned out to be wrong.
2657 : : Fall through to the normal processing. */
2658 : : }
2659 : :
2660 : : /* Otherwise, if v points (refers) to a public base class subobject of the
2661 : : most derived object, and the type of the most derived object has a base
2662 : : class, of type C, that is unambiguous and public, the result points
2663 : : (refers) to the C subobject of the most derived object.
2664 : :
2665 : : But it can also be an invalid case. */
2666 : :
2667 : : /* Get the most derived object. */
2668 : 960 : obj = get_component_with_type (obj, mdtype, NULL_TREE);
2669 : 960 : if (obj == error_mark_node)
2670 : : {
2671 : 639 : if (reference_p)
2672 : : {
2673 : 525 : if (!ctx->quiet)
2674 : : {
2675 : 57 : auto_diagnostic_group d;
2676 : 57 : error_at (loc, "reference %<dynamic_cast%> failed");
2677 : 57 : inform (loc, "static type %qT of its operand is a non-public"
2678 : : " base class of dynamic type %qT", objtype, mdtype);
2679 : 57 : }
2680 : 525 : *non_constant_p = true;
2681 : : }
2682 : 639 : return integer_zero_node;
2683 : : }
2684 : : else
2685 : 321 : gcc_assert (obj);
2686 : :
2687 : : /* Check that the type of the most derived object has a base class
2688 : : of type TYPE that is unambiguous and public. */
2689 : 321 : base_kind b_kind;
2690 : 321 : tree binfo = lookup_base (mdtype, type, ba_check, &b_kind, tf_none);
2691 : 321 : if (!binfo || binfo == error_mark_node)
2692 : : {
2693 : 237 : if (reference_p)
2694 : : {
2695 : 201 : if (!ctx->quiet)
2696 : : {
2697 : 24 : auto_diagnostic_group d;
2698 : 24 : error_at (loc, "reference %<dynamic_cast%> failed");
2699 : 24 : if (b_kind == bk_ambig)
2700 : 9 : inform (loc, "%qT is an ambiguous base class of dynamic "
2701 : : "type %qT of its operand", type, mdtype);
2702 : : else
2703 : 15 : inform (loc, "dynamic type %qT of its operand does not "
2704 : : "have an unambiguous public base class %qT",
2705 : : mdtype, type);
2706 : 24 : }
2707 : 201 : *non_constant_p = true;
2708 : : }
2709 : 237 : return integer_zero_node;
2710 : : }
2711 : : /* If so, return the TYPE subobject of the most derived object. */
2712 : 84 : obj = convert_to_base_statically (obj, binfo);
2713 : 84 : return cp_build_addr_expr (obj, complain);
2714 : : }
2715 : :
2716 : : /* Data structure used by replace_decl and replace_decl_r. */
2717 : :
2718 : : struct replace_decl_data
2719 : : {
2720 : : /* The _DECL we want to replace. */
2721 : : tree decl;
2722 : : /* The replacement for DECL. */
2723 : : tree replacement;
2724 : : /* Trees we've visited. */
2725 : : hash_set<tree> *pset;
2726 : : /* Whether we've performed any replacements. */
2727 : : bool changed;
2728 : : };
2729 : :
2730 : : /* Helper function for replace_decl, called through cp_walk_tree. */
2731 : :
2732 : : static tree
2733 : 4139681 : replace_decl_r (tree *tp, int *walk_subtrees, void *data)
2734 : : {
2735 : 4139681 : replace_decl_data *d = (replace_decl_data *) data;
2736 : :
2737 : : /* We could be replacing
2738 : : &<retval>.bar -> &foo.bar
2739 : : where foo is a static VAR_DECL, so we need to recompute TREE_CONSTANT
2740 : : on the ADDR_EXPR around it. */
2741 : 4139681 : if (TREE_CODE (*tp) == ADDR_EXPR)
2742 : : {
2743 : 395511 : d->pset->add (*tp);
2744 : 395511 : auto save_changed = d->changed;
2745 : 395511 : d->changed = false;
2746 : 395511 : cp_walk_tree (&TREE_OPERAND (*tp, 0), replace_decl_r, d, nullptr);
2747 : 395511 : if (d->changed)
2748 : : {
2749 : 326 : cxx_mark_addressable (*tp);
2750 : 326 : recompute_tree_invariant_for_addr_expr (*tp);
2751 : : }
2752 : : else
2753 : 395185 : d->changed = save_changed;
2754 : 395511 : *walk_subtrees = 0;
2755 : : }
2756 : 3744170 : else if (*tp == d->decl)
2757 : : {
2758 : 663 : *tp = unshare_expr (d->replacement);
2759 : 663 : d->changed = true;
2760 : 663 : *walk_subtrees = 0;
2761 : : }
2762 : 3743507 : else if (TYPE_P (*tp)
2763 : 3743507 : || d->pset->add (*tp))
2764 : 657850 : *walk_subtrees = 0;
2765 : :
2766 : 4139681 : return NULL_TREE;
2767 : : }
2768 : :
2769 : : /* Replace every occurrence of DECL with (an unshared copy of)
2770 : : REPLACEMENT within the expression *TP. Returns true iff a
2771 : : replacement was performed. */
2772 : :
2773 : : bool
2774 : 572911 : replace_decl (tree *tp, tree decl, tree replacement)
2775 : : {
2776 : 572911 : gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
2777 : : (TREE_TYPE (decl), TREE_TYPE (replacement)));
2778 : 572911 : hash_set<tree> pset;
2779 : 572911 : replace_decl_data data = { decl, replacement, &pset, false };
2780 : 572911 : cp_walk_tree (tp, replace_decl_r, &data, NULL);
2781 : 572911 : return data.changed;
2782 : 572911 : }
2783 : :
2784 : : /* Evaluate the call T to virtual function thunk THUNK_FNDECL. */
2785 : :
2786 : : static tree
2787 : 27 : cxx_eval_thunk_call (const constexpr_ctx *ctx, tree t, tree thunk_fndecl,
2788 : : value_cat lval,
2789 : : bool *non_constant_p, bool *overflow_p)
2790 : : {
2791 : 27 : tree function = THUNK_TARGET (thunk_fndecl);
2792 : :
2793 : 27 : if (THUNK_VIRTUAL_OFFSET (thunk_fndecl))
2794 : : {
2795 : 11 : if (!ctx->quiet)
2796 : : {
2797 : 3 : if (!DECL_DECLARED_CONSTEXPR_P (function))
2798 : : {
2799 : 0 : error ("call to non-%<constexpr%> function %qD", function);
2800 : 0 : explain_invalid_constexpr_fn (function);
2801 : : }
2802 : : else
2803 : : /* virtual_offset is only set for virtual bases, which make the
2804 : : class non-literal, so we don't need to handle it here. */
2805 : 3 : error ("calling constexpr member function %qD through virtual "
2806 : : "base subobject", function);
2807 : : }
2808 : 11 : *non_constant_p = true;
2809 : 11 : return t;
2810 : : }
2811 : :
2812 : 16 : tree new_call = copy_node (t);
2813 : 16 : CALL_EXPR_FN (new_call) = function;
2814 : 16 : TREE_TYPE (new_call) = TREE_TYPE (TREE_TYPE (function));
2815 : :
2816 : 16 : tree offset = size_int (THUNK_FIXED_OFFSET (thunk_fndecl));
2817 : :
2818 : 16 : if (DECL_THIS_THUNK_P (thunk_fndecl))
2819 : : {
2820 : : /* 'this'-adjusting thunk. */
2821 : 10 : tree this_arg = CALL_EXPR_ARG (t, 0);
2822 : 10 : this_arg = build2 (POINTER_PLUS_EXPR, TREE_TYPE (this_arg),
2823 : : this_arg, offset);
2824 : 10 : CALL_EXPR_ARG (new_call, 0) = this_arg;
2825 : : }
2826 : : else
2827 : : /* Return-adjusting thunk. */
2828 : 6 : new_call = build2 (POINTER_PLUS_EXPR, TREE_TYPE (new_call),
2829 : : new_call, offset);
2830 : :
2831 : 16 : return cxx_eval_constant_expression (ctx, new_call, lval,
2832 : 16 : non_constant_p, overflow_p);
2833 : : }
2834 : :
2835 : : /* If OBJECT is of const class type, evaluate it to a CONSTRUCTOR and set
2836 : : its TREE_READONLY flag according to READONLY_P. Used for constexpr
2837 : : 'tors to detect modifying const objects in a constexpr context. */
2838 : :
2839 : : static void
2840 : 3593362 : cxx_set_object_constness (const constexpr_ctx *ctx, tree object,
2841 : : bool readonly_p, bool *non_constant_p,
2842 : : bool *overflow_p)
2843 : : {
2844 : 7186724 : if (CLASS_TYPE_P (TREE_TYPE (object))
2845 : 7186724 : && CP_TYPE_CONST_P (TREE_TYPE (object)))
2846 : : {
2847 : : /* Subobjects might not be stored in ctx->global->values but we
2848 : : can get its CONSTRUCTOR by evaluating *this. */
2849 : 505284 : tree e = cxx_eval_constant_expression (ctx, object, vc_prvalue,
2850 : : non_constant_p, overflow_p);
2851 : 505284 : if (TREE_CODE (e) == CONSTRUCTOR && !*non_constant_p)
2852 : 493741 : TREE_READONLY (e) = readonly_p;
2853 : : }
2854 : 3593362 : }
2855 : :
2856 : : /* Subroutine of cxx_eval_constant_expression.
2857 : : Evaluate the call expression tree T in the context of OLD_CALL expression
2858 : : evaluation. */
2859 : :
2860 : : static tree
2861 : 81677959 : cxx_eval_call_expression (const constexpr_ctx *ctx, tree t,
2862 : : value_cat lval,
2863 : : bool *non_constant_p, bool *overflow_p)
2864 : : {
2865 : 81677959 : location_t loc = cp_expr_loc_or_input_loc (t);
2866 : 81677959 : tree fun = get_function_named_in_call (t);
2867 : :
2868 : 81677959 : if (fun == NULL_TREE)
2869 : 270943 : return cxx_eval_internal_function (ctx, t, lval,
2870 : 270943 : non_constant_p, overflow_p);
2871 : :
2872 : 81407016 : if (TREE_CODE (fun) != FUNCTION_DECL)
2873 : : {
2874 : : /* Might be a constexpr function pointer. */
2875 : 116453 : fun = cxx_eval_constant_expression (ctx, fun, vc_prvalue,
2876 : : non_constant_p, overflow_p);
2877 : 116453 : STRIP_NOPS (fun);
2878 : 116453 : if (TREE_CODE (fun) == ADDR_EXPR)
2879 : 19660 : fun = TREE_OPERAND (fun, 0);
2880 : : /* For TARGET_VTABLE_USES_DESCRIPTORS targets, there is no
2881 : : indirection, the called expression is a pointer into the
2882 : : virtual table which should contain FDESC_EXPR. Extract the
2883 : : FUNCTION_DECL from there. */
2884 : : else if (TARGET_VTABLE_USES_DESCRIPTORS
2885 : : && TREE_CODE (fun) == POINTER_PLUS_EXPR
2886 : : && TREE_CODE (TREE_OPERAND (fun, 0)) == ADDR_EXPR
2887 : : && TREE_CODE (TREE_OPERAND (fun, 1)) == INTEGER_CST)
2888 : : {
2889 : : tree d = TREE_OPERAND (TREE_OPERAND (fun, 0), 0);
2890 : : if (VAR_P (d)
2891 : : && DECL_VTABLE_OR_VTT_P (d)
2892 : : && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE
2893 : : && TREE_TYPE (TREE_TYPE (d)) == vtable_entry_type
2894 : : && DECL_INITIAL (d)
2895 : : && TREE_CODE (DECL_INITIAL (d)) == CONSTRUCTOR)
2896 : : {
2897 : : tree i = int_const_binop (TRUNC_DIV_EXPR, TREE_OPERAND (fun, 1),
2898 : : TYPE_SIZE_UNIT (vtable_entry_type));
2899 : : HOST_WIDE_INT idx = find_array_ctor_elt (DECL_INITIAL (d), i);
2900 : : if (idx >= 0)
2901 : : {
2902 : : tree fdesc
2903 : : = (*CONSTRUCTOR_ELTS (DECL_INITIAL (d)))[idx].value;
2904 : : if (TREE_CODE (fdesc) == FDESC_EXPR
2905 : : && integer_zerop (TREE_OPERAND (fdesc, 1)))
2906 : : fun = TREE_OPERAND (fdesc, 0);
2907 : : }
2908 : : }
2909 : : }
2910 : : }
2911 : 81407016 : if (TREE_CODE (fun) != FUNCTION_DECL)
2912 : : {
2913 : 96793 : if (!ctx->quiet && !*non_constant_p)
2914 : 0 : error_at (loc, "expression %qE does not designate a %<constexpr%> "
2915 : : "function", fun);
2916 : 96793 : *non_constant_p = true;
2917 : 96793 : return t;
2918 : : }
2919 : 81310223 : if (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun))
2920 : 8491140 : fun = DECL_CLONED_FUNCTION (fun);
2921 : :
2922 : 81310223 : if (is_ubsan_builtin_p (fun))
2923 : 58 : return void_node;
2924 : :
2925 : 81310165 : if (fndecl_built_in_p (fun))
2926 : 10723834 : return cxx_eval_builtin_function_call (ctx, t, fun,
2927 : 10723834 : lval, non_constant_p, overflow_p);
2928 : 70586331 : if (DECL_THUNK_P (fun))
2929 : 27 : return cxx_eval_thunk_call (ctx, t, fun, lval, non_constant_p, overflow_p);
2930 : 70586304 : if (!maybe_constexpr_fn (fun))
2931 : : {
2932 : 96407 : if (TREE_CODE (t) == CALL_EXPR
2933 : 96341 : && cxx_replaceable_global_alloc_fn (fun)
2934 : 161438 : && (CALL_FROM_NEW_OR_DELETE_P (t)
2935 : 21119 : || is_std_allocator_allocate (ctx->call)))
2936 : : {
2937 : 45742 : const bool new_op_p = IDENTIFIER_NEW_OP_P (DECL_NAME (fun));
2938 : 45742 : const int nargs = call_expr_nargs (t);
2939 : 45742 : tree arg0 = NULL_TREE;
2940 : 71010 : for (int i = 0; i < nargs; ++i)
2941 : : {
2942 : 46095 : tree arg = CALL_EXPR_ARG (t, i);
2943 : 46095 : arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
2944 : : non_constant_p, overflow_p);
2945 : : /* Deleting a non-constant pointer has a better error message
2946 : : below. */
2947 : 46095 : if (new_op_p || i != 0)
2948 : 44784 : VERIFY_CONSTANT (arg);
2949 : 23957 : if (i == 0)
2950 : : arg0 = arg;
2951 : : }
2952 : 24915 : gcc_assert (arg0);
2953 : 24915 : if (new_op_p)
2954 : : {
2955 : 23604 : if (!tree_fits_uhwi_p (arg0))
2956 : : {
2957 : : /* We should not get here; the VERIFY_CONSTANT above
2958 : : should have already caught it. */
2959 : 0 : gcc_checking_assert (false);
2960 : : if (!ctx->quiet)
2961 : : error_at (loc, "cannot allocate array: size not constant");
2962 : : *non_constant_p = true;
2963 : : return t;
2964 : : }
2965 : 47208 : tree type = build_array_type_nelts (char_type_node,
2966 : 23604 : tree_to_uhwi (arg0));
2967 : 23604 : tree var = build_decl (loc, VAR_DECL,
2968 : 23604 : (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
2969 : : & OVL_OP_FLAG_VEC)
2970 : : ? heap_vec_uninit_identifier
2971 : : : heap_uninit_identifier,
2972 : 23604 : type);
2973 : 23604 : DECL_ARTIFICIAL (var) = 1;
2974 : 23604 : ctx->global->heap_vars.safe_push (var);
2975 : 23604 : ctx->global->put_value (var, NULL_TREE);
2976 : 23604 : return fold_convert (ptr_type_node, build_address (var));
2977 : : }
2978 : : else
2979 : : {
2980 : 1311 : STRIP_NOPS (arg0);
2981 : 1311 : if (TREE_CODE (arg0) == ADDR_EXPR
2982 : 1311 : && VAR_P (TREE_OPERAND (arg0, 0)))
2983 : : {
2984 : 1311 : tree var = TREE_OPERAND (arg0, 0);
2985 : 1311 : if (DECL_NAME (var) == heap_uninit_identifier
2986 : 1311 : || DECL_NAME (var) == heap_identifier)
2987 : : {
2988 : 1198 : if (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
2989 : : & OVL_OP_FLAG_VEC)
2990 : : {
2991 : 9 : if (!ctx->quiet)
2992 : : {
2993 : 3 : auto_diagnostic_group d;
2994 : 3 : error_at (loc, "array deallocation of object "
2995 : : "allocated with non-array "
2996 : : "allocation");
2997 : 3 : inform (DECL_SOURCE_LOCATION (var),
2998 : : "allocation performed here");
2999 : 3 : }
3000 : 9 : *non_constant_p = true;
3001 : 9 : return t;
3002 : : }
3003 : 1189 : DECL_NAME (var) = heap_deleted_identifier;
3004 : 1189 : ctx->global->destroy_value (var);
3005 : 1189 : ctx->global->heap_dealloc_count++;
3006 : 1189 : return void_node;
3007 : : }
3008 : 113 : else if (DECL_NAME (var) == heap_vec_uninit_identifier
3009 : 113 : || DECL_NAME (var) == heap_vec_identifier)
3010 : : {
3011 : 89 : if ((IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
3012 : : & OVL_OP_FLAG_VEC) == 0)
3013 : : {
3014 : 9 : if (!ctx->quiet)
3015 : : {
3016 : 3 : auto_diagnostic_group d;
3017 : 3 : error_at (loc, "non-array deallocation of "
3018 : : "object allocated with array "
3019 : : "allocation");
3020 : 3 : inform (DECL_SOURCE_LOCATION (var),
3021 : : "allocation performed here");
3022 : 3 : }
3023 : 9 : *non_constant_p = true;
3024 : 9 : return t;
3025 : : }
3026 : 80 : DECL_NAME (var) = heap_deleted_identifier;
3027 : 80 : ctx->global->destroy_value (var);
3028 : 80 : ctx->global->heap_dealloc_count++;
3029 : 80 : return void_node;
3030 : : }
3031 : 24 : else if (DECL_NAME (var) == heap_deleted_identifier)
3032 : : {
3033 : 15 : if (!ctx->quiet)
3034 : 6 : error_at (loc, "deallocation of already deallocated "
3035 : : "storage");
3036 : 15 : *non_constant_p = true;
3037 : 15 : return t;
3038 : : }
3039 : : }
3040 : 9 : if (!ctx->quiet)
3041 : 3 : error_at (loc, "deallocation of storage that was "
3042 : : "not previously allocated");
3043 : 9 : *non_constant_p = true;
3044 : 9 : return t;
3045 : : }
3046 : : }
3047 : : /* Allow placement new in std::construct_at, just return the second
3048 : : argument. */
3049 : 50665 : if (TREE_CODE (t) == CALL_EXPR
3050 : 50599 : && cxx_placement_new_fn (fun)
3051 : 71929 : && is_std_construct_at (ctx->call))
3052 : : {
3053 : 5315 : const int nargs = call_expr_nargs (t);
3054 : 5315 : tree arg1 = NULL_TREE;
3055 : 15945 : for (int i = 0; i < nargs; ++i)
3056 : : {
3057 : 10630 : tree arg = CALL_EXPR_ARG (t, i);
3058 : 10630 : arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
3059 : : non_constant_p, overflow_p);
3060 : 10630 : if (i == 1)
3061 : : arg1 = arg;
3062 : : else
3063 : 10630 : VERIFY_CONSTANT (arg);
3064 : : }
3065 : 5315 : gcc_assert (arg1);
3066 : : return arg1;
3067 : : }
3068 : 45350 : else if (cxx_dynamic_cast_fn_p (fun))
3069 : 1756 : return cxx_eval_dynamic_cast_fn (ctx, t, non_constant_p, overflow_p);
3070 : :
3071 : 43594 : if (!ctx->quiet)
3072 : : {
3073 : 59 : if (!lambda_static_thunk_p (fun))
3074 : 59 : error_at (loc, "call to non-%<constexpr%> function %qD", fun);
3075 : 59 : explain_invalid_constexpr_fn (fun);
3076 : : }
3077 : 43594 : *non_constant_p = true;
3078 : 43594 : return t;
3079 : : }
3080 : :
3081 : 70489897 : constexpr_ctx new_ctx = *ctx;
3082 : 70489897 : ctx = &new_ctx;
3083 : 78782744 : if (DECL_CONSTRUCTOR_P (fun) && !ctx->object
3084 : 71662133 : && TREE_CODE (t) == AGGR_INIT_EXPR)
3085 : : {
3086 : : /* We want to have an initialization target for an AGGR_INIT_EXPR.
3087 : : If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
3088 : 582 : new_ctx.object = AGGR_INIT_EXPR_SLOT (t);
3089 : 582 : tree ctor = new_ctx.ctor = build_constructor (DECL_CONTEXT (fun), NULL);
3090 : 582 : CONSTRUCTOR_NO_CLEARING (ctor) = true;
3091 : 582 : ctx->global->put_value (new_ctx.object, ctor);
3092 : : }
3093 : : /* An immediate invocation is manifestly constant evaluated including the
3094 : : arguments of the call, so use mce_true even for the argument
3095 : : evaluation. */
3096 : 140979794 : if (DECL_IMMEDIATE_FUNCTION_P (fun))
3097 : 925285 : new_ctx.manifestly_const_eval = mce_true;
3098 : :
3099 : : /* We used to shortcut trivial constructor/op= here, but nowadays
3100 : : we can only get a trivial function here with -fno-elide-constructors. */
3101 : 70489897 : gcc_checking_assert (!trivial_fn_p (fun)
3102 : : || !flag_elide_constructors
3103 : : /* Or it's a call from maybe_thunk_body (111075). */
3104 : : || (TREE_CODE (t) == CALL_EXPR ? CALL_FROM_THUNK_P (t)
3105 : : : AGGR_INIT_FROM_THUNK_P (t))
3106 : : /* We don't elide constructors when processing
3107 : : a noexcept-expression. */
3108 : : || cp_noexcept_operand);
3109 : :
3110 : 70489897 : bool non_constant_args = false;
3111 : 70489897 : constexpr_call new_call;
3112 : 70489897 : new_call.bindings
3113 : 70489897 : = cxx_bind_parameters_in_call (ctx, t, fun, non_constant_p,
3114 : : overflow_p, &non_constant_args);
3115 : :
3116 : : /* We build up the bindings list before we know whether we already have this
3117 : : call cached. If we don't end up saving these bindings, ggc_free them when
3118 : : this function exits. */
3119 : 70489897 : class free_bindings
3120 : : {
3121 : : tree *bindings;
3122 : : public:
3123 : 70489897 : free_bindings (tree &b): bindings (&b) { }
3124 : 70489897 : ~free_bindings () { if (bindings) ggc_free (*bindings); }
3125 : 2886633 : void preserve () { bindings = NULL; }
3126 : 70489897 : } fb (new_call.bindings);
3127 : :
3128 : 70489897 : if (*non_constant_p)
3129 : : return t;
3130 : :
3131 : : /* We can't defer instantiating the function any longer. */
3132 : 38587200 : if (!DECL_INITIAL (fun)
3133 : 1612295 : && (DECL_TEMPLOID_INSTANTIATION (fun) || DECL_DEFAULTED_FN (fun))
3134 : 38587200 : && !uid_sensitive_constexpr_evaluation_p ())
3135 : : {
3136 : 1417524 : location_t save_loc = input_location;
3137 : 1417524 : input_location = loc;
3138 : 1417524 : ++function_depth;
3139 : 1417524 : if (ctx->manifestly_const_eval == mce_true)
3140 : 161990 : FNDECL_MANIFESTLY_CONST_EVALUATED (fun) = true;
3141 : 1417524 : if (DECL_TEMPLOID_INSTANTIATION (fun))
3142 : 1417519 : instantiate_decl (fun, /*defer_ok*/false, /*expl_inst*/false);
3143 : : else
3144 : 5 : synthesize_method (fun);
3145 : 1417524 : --function_depth;
3146 : 1417524 : input_location = save_loc;
3147 : : }
3148 : :
3149 : : /* If in direct recursive call, optimize definition search. */
3150 : 38587200 : if (ctx && ctx->call && ctx->call->fundef && ctx->call->fundef->decl == fun)
3151 : 27527 : new_call.fundef = ctx->call->fundef;
3152 : : else
3153 : : {
3154 : 38559673 : new_call.fundef = retrieve_constexpr_fundef (fun);
3155 : 38559673 : if (new_call.fundef == NULL || new_call.fundef->body == NULL
3156 : 38272613 : || new_call.fundef->result == error_mark_node
3157 : 38253610 : || fun == current_function_decl)
3158 : : {
3159 : 306066 : if (!ctx->quiet)
3160 : : {
3161 : : /* We need to check for current_function_decl here in case we're
3162 : : being called during cp_fold_function, because at that point
3163 : : DECL_INITIAL is set properly and we have a fundef but we
3164 : : haven't lowered invisirefs yet (c++/70344). */
3165 : 150 : if (DECL_INITIAL (fun) == error_mark_node
3166 : 150 : || fun == current_function_decl)
3167 : 15 : error_at (loc, "%qD called in a constant expression before its "
3168 : : "definition is complete", fun);
3169 : 135 : else if (DECL_INITIAL (fun))
3170 : : {
3171 : : /* The definition of fun was somehow unsuitable. But pretend
3172 : : that lambda static thunks don't exist. */
3173 : 98 : if (!lambda_static_thunk_p (fun))
3174 : 98 : error_at (loc, "%qD called in a constant expression", fun);
3175 : 98 : explain_invalid_constexpr_fn (fun);
3176 : : }
3177 : : else
3178 : 37 : error_at (loc, "%qD used before its definition", fun);
3179 : : }
3180 : 306066 : *non_constant_p = true;
3181 : 306066 : return t;
3182 : : }
3183 : : }
3184 : :
3185 : : /* Don't complain about problems evaluating an ill-formed function. */
3186 : 38281134 : if (function *f = DECL_STRUCT_FUNCTION (fun))
3187 : 38281134 : if (f->language->erroneous)
3188 : 497 : new_ctx.quiet = true;
3189 : :
3190 : 38281134 : int depth_ok = push_cx_call_context (t);
3191 : :
3192 : : /* Remember the object we are constructing or destructing. */
3193 : 38281134 : tree new_obj = NULL_TREE;
3194 : 76562268 : if (DECL_CONSTRUCTOR_P (fun) || DECL_DESTRUCTOR_P (fun))
3195 : : {
3196 : : /* In a cdtor, it should be the first `this' argument.
3197 : : At this point it has already been evaluated in the call
3198 : : to cxx_bind_parameters_in_call. */
3199 : 4359937 : new_obj = TREE_VEC_ELT (new_call.bindings, 0);
3200 : 4359937 : bool empty_base = false;
3201 : 4359937 : new_obj = cxx_fold_indirect_ref (ctx, loc, DECL_CONTEXT (fun), new_obj,
3202 : : &empty_base);
3203 : : /* If we're initializing an empty class, don't set constness, because
3204 : : cxx_fold_indirect_ref will return the wrong object to set constness
3205 : : of. */
3206 : 4359937 : if (empty_base)
3207 : : new_obj = NULL_TREE;
3208 : 1775953 : else if (ctx->call && ctx->call->fundef
3209 : 7165651 : && DECL_CONSTRUCTOR_P (ctx->call->fundef->decl))
3210 : : {
3211 : 1090073 : tree cur_obj = TREE_VEC_ELT (ctx->call->bindings, 0);
3212 : 1090073 : STRIP_NOPS (cur_obj);
3213 : 1090073 : if (TREE_CODE (cur_obj) == ADDR_EXPR)
3214 : 1088826 : cur_obj = TREE_OPERAND (cur_obj, 0);
3215 : 1090073 : if (new_obj == cur_obj)
3216 : : /* We're calling the target constructor of a delegating
3217 : : constructor, or accessing a base subobject through a
3218 : : NOP_EXPR as part of a call to a base constructor, so
3219 : : there is no new (sub)object. */
3220 : 766575 : new_obj = NULL_TREE;
3221 : : }
3222 : : }
3223 : :
3224 : 38281134 : tree result = NULL_TREE;
3225 : :
3226 : 38281134 : constexpr_call *entry = NULL;
3227 : 38281134 : if (depth_ok && !non_constant_args && ctx->strict)
3228 : : {
3229 : 18709129 : new_call.hash = constexpr_fundef_hasher::hash (new_call.fundef);
3230 : 18709129 : new_call.hash
3231 : 18709129 : = iterative_hash_template_arg (new_call.bindings, new_call.hash);
3232 : :
3233 : : /* If we have seen this call before, we are done. */
3234 : 18709129 : maybe_initialize_constexpr_call_table ();
3235 : 18709129 : bool insert = depth_ok < constexpr_cache_depth;
3236 : 18709129 : constexpr_call **slot
3237 : 18936815 : = constexpr_call_table->find_slot (&new_call,
3238 : : insert ? INSERT : NO_INSERT);
3239 : 18709129 : entry = slot ? *slot : NULL;
3240 : 18583679 : if (entry == NULL)
3241 : : {
3242 : : /* Only cache up to constexpr_cache_depth to limit memory use. */
3243 : 3012083 : if (insert)
3244 : : {
3245 : : /* We need to keep a pointer to the entry, not just the slot, as
3246 : : the slot can move during evaluation of the body. */
3247 : 2886633 : *slot = entry = ggc_alloc<constexpr_call> ();
3248 : 2886633 : *entry = new_call;
3249 : 2886633 : entry->result (ctx->manifestly_const_eval) = unknown_type_node;
3250 : 2886633 : fb.preserve ();
3251 : : }
3252 : : }
3253 : : /* Calls that are in progress have their result set to unknown_type_node,
3254 : : so that we can detect circular dependencies. Now that we only cache
3255 : : up to constexpr_cache_depth this won't catch circular dependencies that
3256 : : start deeper, but they'll hit the recursion or ops limit. */
3257 : 15697046 : else if (entry->result (ctx->manifestly_const_eval) == unknown_type_node)
3258 : : {
3259 : 6 : if (!ctx->quiet)
3260 : 0 : error ("call has circular dependency");
3261 : 6 : *non_constant_p = true;
3262 : 6 : entry->result (ctx->manifestly_const_eval) = result = error_mark_node;
3263 : : }
3264 : : else
3265 : 15697040 : result = entry->result (ctx->manifestly_const_eval);
3266 : : }
3267 : :
3268 : 38281134 : if (!depth_ok)
3269 : : {
3270 : 30 : if (!ctx->quiet)
3271 : 3 : error ("%<constexpr%> evaluation depth exceeds maximum of %d (use "
3272 : : "%<-fconstexpr-depth=%> to increase the maximum)",
3273 : : max_constexpr_depth);
3274 : 30 : *non_constant_p = true;
3275 : 30 : result = error_mark_node;
3276 : : }
3277 : : else
3278 : : {
3279 : 38281104 : bool cacheable = !!entry;
3280 : 38281104 : if (result && result != error_mark_node)
3281 : : /* OK */;
3282 : 25622123 : else if (!DECL_SAVED_TREE (fun))
3283 : : {
3284 : : /* When at_eof >= 3, cgraph has started throwing away
3285 : : DECL_SAVED_TREE, so fail quietly. FIXME we get here because of
3286 : : late code generation for VEC_INIT_EXPR, which needs to be
3287 : : completely reconsidered. */
3288 : 0 : gcc_assert (at_eof >= 3 && ctx->quiet);
3289 : 0 : *non_constant_p = true;
3290 : : }
3291 : 25622123 : else if (tree copy = get_fundef_copy (new_call.fundef))
3292 : : {
3293 : 25622123 : tree body, parms, res;
3294 : 25622123 : releasing_vec ctors;
3295 : :
3296 : : /* Reuse or create a new unshared copy of this function's body. */
3297 : 25622123 : body = TREE_PURPOSE (copy);
3298 : 25622123 : parms = TREE_VALUE (copy);
3299 : 25622123 : res = TREE_TYPE (copy);
3300 : :
3301 : : /* Associate the bindings with the remapped parms. */
3302 : 25622123 : tree bound = new_call.bindings;
3303 : 25622123 : tree remapped = parms;
3304 : 55405186 : for (int i = 0; i < TREE_VEC_LENGTH (bound); ++i)
3305 : : {
3306 : 29783063 : tree arg = TREE_VEC_ELT (bound, i);
3307 : 29783063 : if (entry)
3308 : : {
3309 : : /* Unshare args going into the hash table to separate them
3310 : : from the caller's context, for better GC and to avoid
3311 : : problems with verify_gimple. */
3312 : 1923131 : arg = unshare_expr_without_location (arg);
3313 : 1923131 : TREE_VEC_ELT (bound, i) = arg;
3314 : :
3315 : : /* And then unshare again so the callee doesn't change the
3316 : : argument values in the hash table. XXX Could we unshare
3317 : : lazily in cxx_eval_store_expression? */
3318 : 1923131 : arg = unshare_constructor (arg);
3319 : 1923131 : if (TREE_CODE (arg) == CONSTRUCTOR)
3320 : 731486 : vec_safe_push (ctors, arg);
3321 : : }
3322 : 29783063 : ctx->global->put_value (remapped, arg);
3323 : 29783063 : remapped = DECL_CHAIN (remapped);
3324 : : }
3325 : 25622123 : if (remapped)
3326 : : {
3327 : : /* We shouldn't have any parms without args, but fail gracefully
3328 : : in error recovery. */
3329 : 6 : gcc_checking_assert (seen_error ());
3330 : 6 : *non_constant_p = true;
3331 : : }
3332 : : /* Add the RESULT_DECL to the values map, too. */
3333 : 25622123 : gcc_assert (!DECL_BY_REFERENCE (res));
3334 : 25622123 : ctx->global->put_value (res, NULL_TREE);
3335 : :
3336 : : /* Remember the current call we're evaluating. */
3337 : 25622123 : constexpr_ctx call_ctx = *ctx;
3338 : 25622123 : call_ctx.call = &new_call;
3339 : 25622123 : unsigned save_heap_alloc_count = ctx->global->heap_vars.length ();
3340 : 25622123 : unsigned save_heap_dealloc_count = ctx->global->heap_dealloc_count;
3341 : :
3342 : : /* Make sure we fold std::is_constant_evaluated to true in an
3343 : : immediate function. */
3344 : 51244246 : if (DECL_IMMEDIATE_FUNCTION_P (fun))
3345 : 365781 : call_ctx.manifestly_const_eval = mce_true;
3346 : :
3347 : : /* If this is a constexpr destructor, the object's const and volatile
3348 : : semantics are no longer in effect; see [class.dtor]p5. */
3349 : 29215485 : if (new_obj && DECL_DESTRUCTOR_P (fun))
3350 : 183325 : cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/false,
3351 : : non_constant_p, overflow_p);
3352 : :
3353 : : /* If this is a constructor, we are beginning the lifetime of the
3354 : : object we are initializing. */
3355 : 25622123 : if (new_obj
3356 : 7186724 : && DECL_CONSTRUCTOR_P (fun)
3357 : 3410037 : && TREE_CODE (new_obj) == COMPONENT_REF
3358 : 26380341 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (new_obj, 0))) == UNION_TYPE)
3359 : : {
3360 : 2895 : tree activate = build2 (INIT_EXPR, TREE_TYPE (new_obj),
3361 : : new_obj,
3362 : 2895 : build_constructor (TREE_TYPE (new_obj),
3363 : : NULL));
3364 : 2895 : cxx_eval_constant_expression (ctx, activate,
3365 : : lval, non_constant_p, overflow_p);
3366 : 2895 : ggc_free (activate);
3367 : : }
3368 : :
3369 : 25622123 : tree jump_target = NULL_TREE;
3370 : 25622123 : cxx_eval_constant_expression (&call_ctx, body,
3371 : : vc_discard, non_constant_p, overflow_p,
3372 : : &jump_target);
3373 : :
3374 : 51244246 : if (DECL_CONSTRUCTOR_P (fun))
3375 : : /* This can be null for a subobject constructor call, in
3376 : : which case what we care about is the initialization
3377 : : side-effects rather than the value. We could get at the
3378 : : value by evaluating *this, but we don't bother; there's
3379 : : no need to put such a call in the hash table. */
3380 : 4173298 : result = lval ? ctx->object : ctx->ctor;
3381 : 21448825 : else if (VOID_TYPE_P (TREE_TYPE (res)))
3382 : 1247476 : result = void_node;
3383 : : else
3384 : : {
3385 : 20201349 : result = ctx->global->get_value (res);
3386 : 7456484 : if (result == NULL_TREE && !*non_constant_p
3387 : 20201527 : && !DECL_DESTRUCTOR_P (fun))
3388 : : {
3389 : 89 : if (!ctx->quiet)
3390 : 6 : error ("%<constexpr%> call flows off the end "
3391 : : "of the function");
3392 : 89 : *non_constant_p = true;
3393 : : }
3394 : : }
3395 : :
3396 : : /* At this point, the object's constructor will have run, so
3397 : : the object is no longer under construction, and its possible
3398 : : 'const' semantics now apply. Make a note of this fact by
3399 : : marking the CONSTRUCTOR TREE_READONLY. */
3400 : 29215485 : if (new_obj && DECL_CONSTRUCTOR_P (fun))
3401 : 3410037 : cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/true,
3402 : : non_constant_p, overflow_p);
3403 : :
3404 : : /* Remove the parms/result from the values map. */
3405 : 25622123 : destroy_value_checked (ctx, res, non_constant_p);
3406 : 55405192 : for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
3407 : 29783069 : destroy_value_checked (ctx, parm, non_constant_p);
3408 : :
3409 : : /* Free any parameter CONSTRUCTORs we aren't returning directly. */
3410 : 26353609 : while (!ctors->is_empty ())
3411 : : {
3412 : 731486 : tree c = ctors->pop ();
3413 : 731486 : if (c != result)
3414 : 731486 : free_constructor (c);
3415 : : }
3416 : :
3417 : : /* Make the unshared function copy we used available for re-use. */
3418 : 25622123 : save_fundef_copy (fun, copy);
3419 : :
3420 : : /* If the call allocated some heap object that hasn't been
3421 : : deallocated during the call, or if it deallocated some heap
3422 : : object it has not allocated, the call isn't really stateless
3423 : : for the constexpr evaluation and should not be cached.
3424 : : It is fine if the call allocates something and deallocates it
3425 : : too. */
3426 : 25622123 : if (cacheable
3427 : 31546821 : && (save_heap_alloc_count != ctx->global->heap_vars.length ()
3428 : 5924126 : || (save_heap_dealloc_count
3429 : 5924126 : != ctx->global->heap_dealloc_count)))
3430 : : {
3431 : 572 : tree heap_var;
3432 : 572 : unsigned int i;
3433 : 572 : if ((ctx->global->heap_vars.length ()
3434 : 572 : - ctx->global->heap_dealloc_count)
3435 : 572 : != save_heap_alloc_count - save_heap_dealloc_count)
3436 : : cacheable = false;
3437 : : else
3438 : 1719 : FOR_EACH_VEC_ELT_FROM (ctx->global->heap_vars, i, heap_var,
3439 : : save_heap_alloc_count)
3440 : 1310 : if (DECL_NAME (heap_var) != heap_deleted_identifier)
3441 : : {
3442 : : cacheable = false;
3443 : : break;
3444 : : }
3445 : : }
3446 : :
3447 : : /* Rewrite all occurrences of the function's RESULT_DECL with the
3448 : : current object under construction. */
3449 : 25622123 : if (!*non_constant_p
3450 : 16418921 : && ctx->object
3451 : 4897470 : && CLASS_TYPE_P (TREE_TYPE (res))
3452 : 26875493 : && !is_empty_class (TREE_TYPE (res)))
3453 : : {
3454 : 572735 : if (!same_type_ignoring_top_level_qualifiers_p
3455 : 572735 : (TREE_TYPE (res), TREE_TYPE (ctx->object)))
3456 : 0 : *non_constant_p = true;
3457 : 572735 : else if (replace_decl (&result, res, ctx->object))
3458 : : {
3459 : 238 : cacheable = false;
3460 : 238 : result = cxx_eval_constant_expression (ctx, result, lval,
3461 : : non_constant_p,
3462 : : overflow_p);
3463 : : }
3464 : : }
3465 : :
3466 : : /* Only cache a permitted result of a constant expression. */
3467 : 25622123 : if (cacheable && !reduced_constant_expression_p (result))
3468 : : cacheable = false;
3469 : 25622123 : }
3470 : : else
3471 : : /* Couldn't get a function copy to evaluate. */
3472 : 0 : *non_constant_p = true;
3473 : :
3474 : 38281104 : if (result == error_mark_node)
3475 : 0 : *non_constant_p = true;
3476 : 38281104 : if (*non_constant_p || *overflow_p)
3477 : 9203358 : result = error_mark_node;
3478 : 29077746 : else if (!result)
3479 : 1162909 : result = void_node;
3480 : 38281104 : if (entry)
3481 : : {
3482 : 37167358 : entry->result (ctx->manifestly_const_eval)
3483 : 18583679 : = cacheable ? result : error_mark_node;
3484 : :
3485 : 18583679 : if (result != error_mark_node
3486 : 15609848 : && ctx->manifestly_const_eval == mce_unknown)
3487 : : {
3488 : : /* Evaluation succeeded and was independent of whether we're in a
3489 : : manifestly constant-evaluated context, so we can also reuse
3490 : : this result when evaluating this call with a fixed context. */
3491 : 3381302 : if (!entry->result (mce_true))
3492 : 616022 : entry->result (mce_true) = entry->result (mce_unknown);
3493 : 3381302 : if (!entry->result (mce_false))
3494 : 605011 : entry->result (mce_false) = entry->result (mce_unknown);
3495 : : }
3496 : : }
3497 : : }
3498 : :
3499 : : /* The result of a constexpr function must be completely initialized.
3500 : :
3501 : : However, in C++20, a constexpr constructor doesn't necessarily have
3502 : : to initialize all the fields, so we don't clear CONSTRUCTOR_NO_CLEARING
3503 : : in order to detect reading an unitialized object in constexpr instead
3504 : : of value-initializing it. (reduced_constant_expression_p is expected to
3505 : : take care of clearing the flag.) */
3506 : 38281134 : if (TREE_CODE (result) == CONSTRUCTOR
3507 : 38281134 : && (cxx_dialect < cxx20
3508 : 6637412 : || !DECL_CONSTRUCTOR_P (fun)))
3509 : 3597579 : clear_no_implicit_zero (result);
3510 : :
3511 : 38281134 : pop_cx_call_context ();
3512 : 38281134 : return result;
3513 : 70489897 : }
3514 : :
3515 : : /* Return true if T is a valid constant initializer. If a CONSTRUCTOR
3516 : : initializes all the members, the CONSTRUCTOR_NO_CLEARING flag will be
3517 : : cleared.
3518 : : FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
3519 : :
3520 : : bool
3521 : 596164464 : reduced_constant_expression_p (tree t)
3522 : : {
3523 : 596164464 : if (t == NULL_TREE)
3524 : : return false;
3525 : :
3526 : 593191779 : switch (TREE_CODE (t))
3527 : : {
3528 : : case PTRMEM_CST:
3529 : : /* Even if we can't lower this yet, it's constant. */
3530 : : return true;
3531 : :
3532 : : case OMP_DECLARE_MAPPER:
3533 : : return true;
3534 : :
3535 : 42383136 : case CONSTRUCTOR:
3536 : : /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
3537 : 42383136 : tree field;
3538 : 42383136 : if (!AGGREGATE_TYPE_P (TREE_TYPE (t)))
3539 : : /* A constant vector would be folded to VECTOR_CST.
3540 : : A CONSTRUCTOR of scalar type means uninitialized. */
3541 : : return false;
3542 : 42367980 : if (CONSTRUCTOR_NO_CLEARING (t))
3543 : : {
3544 : 1123233 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
3545 : : {
3546 : : /* There must be a valid constant initializer at every array
3547 : : index. */
3548 : 989 : tree min = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (t)));
3549 : 989 : tree max = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (t)));
3550 : 989 : tree cursor = min;
3551 : 21260 : for (auto &e: CONSTRUCTOR_ELTS (t))
3552 : : {
3553 : 18403 : if (!reduced_constant_expression_p (e.value))
3554 : : return false;
3555 : 18353 : if (array_index_cmp (cursor, e.index) != 0)
3556 : : return false;
3557 : 18293 : if (TREE_CODE (e.index) == RANGE_EXPR)
3558 : 0 : cursor = TREE_OPERAND (e.index, 1);
3559 : 18293 : if (TREE_CODE (e.value) == RAW_DATA_CST)
3560 : 0 : cursor
3561 : 0 : = int_const_binop (PLUS_EXPR, cursor,
3562 : 0 : size_int (RAW_DATA_LENGTH (e.value)));
3563 : : else
3564 : 18293 : cursor = int_const_binop (PLUS_EXPR, cursor,
3565 : 18293 : size_one_node);
3566 : : }
3567 : 879 : if (find_array_ctor_elt (t, max) == -1)
3568 : : return false;
3569 : 793 : goto ok;
3570 : : }
3571 : 1122244 : else if (cxx_dialect >= cxx20
3572 : 1122244 : && TREE_CODE (TREE_TYPE (t)) == UNION_TYPE)
3573 : : {
3574 : 4190094 : if (CONSTRUCTOR_NELTS (t) == 0)
3575 : : /* An initialized union has a constructor element. */
3576 : : return false;
3577 : : /* And it only initializes one member. */
3578 : : field = NULL_TREE;
3579 : : }
3580 : : else
3581 : 1122229 : field = next_subobject_field (TYPE_FIELDS (TREE_TYPE (t)));
3582 : : }
3583 : : else
3584 : : field = NULL_TREE;
3585 : 264051193 : for (auto &e: CONSTRUCTOR_ELTS (t))
3586 : : {
3587 : : /* If VAL is null, we're in the middle of initializing this
3588 : : element. */
3589 : 167186696 : if (!reduced_constant_expression_p (e.value))
3590 : : return false;
3591 : : /* We want to remove initializers for empty fields in a struct to
3592 : : avoid confusing output_constructor. */
3593 : 166680195 : if (is_empty_field (e.index)
3594 : 166680195 : && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE)
3595 : : return false;
3596 : : /* Check for non-empty fields between initialized fields when
3597 : : CONSTRUCTOR_NO_CLEARING. */
3598 : 166078896 : for (; field && e.index != field;
3599 : 92907 : field = next_subobject_field (DECL_CHAIN (field)))
3600 : 93433 : if (!is_really_empty_class (TREE_TYPE (field),
3601 : : /*ignore_vptr*/false))
3602 : : return false;
3603 : 165985463 : if (field)
3604 : 1231822 : field = next_subobject_field (DECL_CHAIN (field));
3605 : : }
3606 : : /* There could be a non-empty field at the end. */
3607 : 41241081 : for (; field; field = next_subobject_field (DECL_CHAIN (field)))
3608 : 76147 : if (!is_really_empty_class (TREE_TYPE (field), /*ignore_vptr*/false))
3609 : : return false;
3610 : 41164934 : ok:
3611 : 41165727 : if (CONSTRUCTOR_NO_CLEARING (t))
3612 : : /* All the fields are initialized. */
3613 : 1033197 : CONSTRUCTOR_NO_CLEARING (t) = false;
3614 : : return true;
3615 : :
3616 : 550774736 : default:
3617 : : /* FIXME are we calling this too much? */
3618 : 550774736 : return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE;
3619 : : }
3620 : : }
3621 : :
3622 : : /* *TP was not deemed constant by reduced_constant_expression_p. Explain
3623 : : why and suggest what could be done about it. */
3624 : :
3625 : : static tree
3626 : 916 : verify_constant_explain_r (tree *tp, int *walk_subtrees, void *)
3627 : : {
3628 : 916 : bool ref_p = false;
3629 : :
3630 : : /* No need to look into types or unevaluated operands. */
3631 : 916 : if (TYPE_P (*tp) || unevaluated_p (TREE_CODE (*tp)))
3632 : : {
3633 : 0 : *walk_subtrees = false;
3634 : 0 : return NULL_TREE;
3635 : : }
3636 : :
3637 : 916 : switch (TREE_CODE (*tp))
3638 : : {
3639 : 52 : CASE_CONVERT:
3640 : 52 : if (TREE_CODE (TREE_OPERAND (*tp, 0)) != ADDR_EXPR)
3641 : : break;
3642 : 33 : ref_p = TYPE_REF_P (TREE_TYPE (*tp));
3643 : 33 : *tp = TREE_OPERAND (*tp, 0);
3644 : 165 : gcc_fallthrough ();
3645 : 165 : case ADDR_EXPR:
3646 : 165 : {
3647 : 165 : tree op = TREE_OPERAND (*tp, 0);
3648 : 165 : if (VAR_P (op)
3649 : 96 : && DECL_DECLARED_CONSTEXPR_P (op)
3650 : 33 : && !TREE_STATIC (op)
3651 : : /* ??? We should also say something about temporaries. */
3652 : 195 : && !DECL_ARTIFICIAL (op))
3653 : : {
3654 : 27 : if (ref_p)
3655 : 6 : inform (location_of (*tp), "reference to %qD is not a constant "
3656 : : "expression", op);
3657 : : else
3658 : 21 : inform (location_of (*tp), "pointer to %qD is not a constant "
3659 : : "expression", op);
3660 : 27 : const location_t op_loc = DECL_SOURCE_LOCATION (op);
3661 : 27 : rich_location richloc (line_table, op_loc);
3662 : 27 : richloc.add_fixit_insert_before (op_loc, "static ");
3663 : 27 : inform (&richloc,
3664 : : "address of non-static constexpr variable %qD may differ on "
3665 : : "each invocation of the enclosing function; add %<static%> "
3666 : : "to give it a constant address", op);
3667 : 27 : }
3668 : : break;
3669 : : }
3670 : : default:
3671 : : break;
3672 : : }
3673 : :
3674 : : return NULL_TREE;
3675 : : }
3676 : :
3677 : : /* Some expressions may have constant operands but are not constant
3678 : : themselves, such as 1/0. Call this function to check for that
3679 : : condition.
3680 : :
3681 : : We only call this in places that require an arithmetic constant, not in
3682 : : places where we might have a non-constant expression that can be a
3683 : : component of a constant expression, such as the address of a constexpr
3684 : : variable that might be dereferenced later. */
3685 : :
3686 : : static bool
3687 : 369746077 : verify_constant (tree t, bool allow_non_constant, bool *non_constant_p,
3688 : : bool *overflow_p)
3689 : : {
3690 : 361703640 : if (!*non_constant_p && !reduced_constant_expression_p (t)
3691 : 371850964 : && t != void_node)
3692 : : {
3693 : 2104860 : if (!allow_non_constant)
3694 : : {
3695 : 238 : auto_diagnostic_group d;
3696 : 342 : error_at (cp_expr_loc_or_input_loc (t),
3697 : : "%q+E is not a constant expression", t);
3698 : 238 : cp_walk_tree_without_duplicates (&t, verify_constant_explain_r,
3699 : : nullptr);
3700 : 238 : }
3701 : 2104860 : *non_constant_p = true;
3702 : : }
3703 : 369746077 : if (TREE_OVERFLOW_P (t))
3704 : : {
3705 : 674 : if (!allow_non_constant)
3706 : : {
3707 : 155 : permerror (input_location, "overflow in constant expression");
3708 : : /* If we're being permissive (and are in an enforcing
3709 : : context), ignore the overflow. */
3710 : 155 : if (flag_permissive)
3711 : 75 : return *non_constant_p;
3712 : : }
3713 : 599 : *overflow_p = true;
3714 : : }
3715 : 369746002 : return *non_constant_p;
3716 : : }
3717 : :
3718 : : /* Check whether the shift operation with code CODE and type TYPE on LHS
3719 : : and RHS is undefined. If it is, give an error with an explanation,
3720 : : and return true; return false otherwise. */
3721 : :
3722 : : static bool
3723 : 30588044 : cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx,
3724 : : enum tree_code code, tree type, tree lhs, tree rhs)
3725 : : {
3726 : 30588044 : if ((code != LSHIFT_EXPR && code != RSHIFT_EXPR)
3727 : 2931007 : || TREE_CODE (lhs) != INTEGER_CST
3728 : 2931007 : || TREE_CODE (rhs) != INTEGER_CST)
3729 : : return false;
3730 : :
3731 : 2931007 : tree lhstype = TREE_TYPE (lhs);
3732 : 2931007 : unsigned HOST_WIDE_INT uprec = TYPE_PRECISION (TREE_TYPE (lhs));
3733 : :
3734 : : /* [expr.shift] The behavior is undefined if the right operand
3735 : : is negative, or greater than or equal to the length in bits
3736 : : of the promoted left operand. */
3737 : 2931007 : if (tree_int_cst_sgn (rhs) == -1)
3738 : : {
3739 : 126 : if (!ctx->quiet)
3740 : 35 : permerror (loc, "right operand of shift expression %q+E is negative",
3741 : : build2_loc (loc, code, type, lhs, rhs));
3742 : 126 : return (!flag_permissive || ctx->quiet);
3743 : : }
3744 : 2930881 : if (compare_tree_int (rhs, uprec) >= 0)
3745 : : {
3746 : 270 : if (!ctx->quiet)
3747 : 34 : permerror (loc, "right operand of shift expression %q+E is greater "
3748 : : "than or equal to the precision %wu of the left operand",
3749 : : build2_loc (loc, code, type, lhs, rhs), uprec);
3750 : 270 : return (!flag_permissive || ctx->quiet);
3751 : : }
3752 : :
3753 : : /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
3754 : : if E1 has a signed type and non-negative value, and E1x2^E2 is
3755 : : representable in the corresponding unsigned type of the result type,
3756 : : then that value, converted to the result type, is the resulting value;
3757 : : otherwise, the behavior is undefined.
3758 : : For C++20:
3759 : : The value of E1 << E2 is the unique value congruent to E1 x 2^E2 modulo
3760 : : 2^N, where N is the range exponent of the type of the result. */
3761 : 2930611 : if (code == LSHIFT_EXPR
3762 : 2815216 : && !TYPE_OVERFLOW_WRAPS (lhstype)
3763 : 1920135 : && cxx_dialect >= cxx11
3764 : 4834808 : && cxx_dialect < cxx20)
3765 : : {
3766 : 1435008 : if (tree_int_cst_sgn (lhs) == -1)
3767 : : {
3768 : 128 : if (!ctx->quiet)
3769 : 23 : permerror (loc,
3770 : : "left operand of shift expression %q+E is negative",
3771 : : build2_loc (loc, code, type, lhs, rhs));
3772 : 128 : return (!flag_permissive || ctx->quiet);
3773 : : }
3774 : : /* For signed x << y the following:
3775 : : (unsigned) x >> ((prec (lhs) - 1) - y)
3776 : : if > 1, is undefined. The right-hand side of this formula
3777 : : is the highest bit of the LHS that can be set (starting from 0),
3778 : : so that the shift doesn't overflow. We then right-shift the LHS
3779 : : to see whether any other bit is set making the original shift
3780 : : undefined -- the result is not representable in the corresponding
3781 : : unsigned type. */
3782 : 1434880 : tree t = build_int_cst (unsigned_type_node, uprec - 1);
3783 : 1434880 : t = fold_build2 (MINUS_EXPR, unsigned_type_node, t, rhs);
3784 : 1434880 : tree ulhs = fold_convert (unsigned_type_for (lhstype), lhs);
3785 : 1434880 : t = fold_build2 (RSHIFT_EXPR, TREE_TYPE (ulhs), ulhs, t);
3786 : 1434880 : if (tree_int_cst_lt (integer_one_node, t))
3787 : : {
3788 : 86 : if (!ctx->quiet)
3789 : 8 : permerror (loc, "shift expression %q+E overflows",
3790 : : build2_loc (loc, code, type, lhs, rhs));
3791 : 86 : return (!flag_permissive || ctx->quiet);
3792 : : }
3793 : : }
3794 : : return false;
3795 : : }
3796 : :
3797 : : /* Subroutine of cxx_eval_constant_expression.
3798 : : Attempt to reduce the unary expression tree T to a compile time value.
3799 : : If successful, return the value. Otherwise issue a diagnostic
3800 : : and return error_mark_node. */
3801 : :
3802 : : static tree
3803 : 20912369 : cxx_eval_unary_expression (const constexpr_ctx *ctx, tree t,
3804 : : bool /*lval*/,
3805 : : bool *non_constant_p, bool *overflow_p)
3806 : : {
3807 : 20912369 : tree r;
3808 : 20912369 : tree orig_arg = TREE_OPERAND (t, 0);
3809 : 20912369 : tree arg = cxx_eval_constant_expression (ctx, orig_arg, vc_prvalue,
3810 : : non_constant_p, overflow_p);
3811 : 20912369 : VERIFY_CONSTANT (arg);
3812 : 17487440 : location_t loc = EXPR_LOCATION (t);
3813 : 17487440 : enum tree_code code = TREE_CODE (t);
3814 : 17487440 : tree type = TREE_TYPE (t);
3815 : 17487440 : r = fold_unary_loc (loc, code, type, arg);
3816 : 17487440 : if (r == NULL_TREE)
3817 : : {
3818 : 17 : if (arg == orig_arg)
3819 : : r = t;
3820 : : else
3821 : 13 : r = build1_loc (loc, code, type, arg);
3822 : : }
3823 : 17487440 : VERIFY_CONSTANT (r);
3824 : : return r;
3825 : : }
3826 : :
3827 : : /* Helper function for cxx_eval_binary_expression. Try to optimize
3828 : : original POINTER_PLUS_EXPR T, LHS p+ RHS, return NULL_TREE if the
3829 : : generic folding should be used. */
3830 : :
3831 : : static tree
3832 : 2783513 : cxx_fold_pointer_plus_expression (const constexpr_ctx *ctx, tree t,
3833 : : tree lhs, tree rhs, bool *non_constant_p,
3834 : : bool *overflow_p)
3835 : : {
3836 : 2783513 : STRIP_NOPS (lhs);
3837 : 2783513 : if (TREE_CODE (lhs) != ADDR_EXPR)
3838 : : return NULL_TREE;
3839 : :
3840 : 2484392 : lhs = TREE_OPERAND (lhs, 0);
3841 : :
3842 : : /* &A[i] p+ j => &A[i + j] */
3843 : 2484392 : if (TREE_CODE (lhs) == ARRAY_REF
3844 : 6679 : && TREE_CODE (TREE_OPERAND (lhs, 1)) == INTEGER_CST
3845 : 6679 : && TREE_CODE (rhs) == INTEGER_CST
3846 : 6679 : && TYPE_SIZE_UNIT (TREE_TYPE (lhs))
3847 : 2491071 : && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs))) == INTEGER_CST)
3848 : : {
3849 : 6679 : tree orig_type = TREE_TYPE (t);
3850 : 6679 : location_t loc = EXPR_LOCATION (t);
3851 : 6679 : tree type = TREE_TYPE (lhs);
3852 : :
3853 : 6679 : t = fold_convert_loc (loc, ssizetype, TREE_OPERAND (lhs, 1));
3854 : 6679 : tree nelts = array_type_nelts_top (TREE_TYPE (TREE_OPERAND (lhs, 0)));
3855 : 6679 : nelts = cxx_eval_constant_expression (ctx, nelts, vc_prvalue,
3856 : : non_constant_p, overflow_p);
3857 : 6679 : if (*non_constant_p)
3858 : : return NULL_TREE;
3859 : : /* Don't fold an out-of-bound access. */
3860 : 6667 : if (!tree_int_cst_le (t, nelts))
3861 : : return NULL_TREE;
3862 : 6667 : rhs = cp_fold_convert (ssizetype, rhs);
3863 : : /* Don't fold if rhs can't be divided exactly by TYPE_SIZE_UNIT.
3864 : : constexpr int A[1]; ... (char *)&A[0] + 1 */
3865 : 6667 : if (!integer_zerop (fold_build2_loc (loc, TRUNC_MOD_EXPR, sizetype,
3866 : 6667 : rhs, TYPE_SIZE_UNIT (type))))
3867 : : return NULL_TREE;
3868 : : /* Make sure to treat the second operand of POINTER_PLUS_EXPR
3869 : : as signed. */
3870 : 6635 : rhs = fold_build2_loc (loc, EXACT_DIV_EXPR, ssizetype, rhs,
3871 : 6635 : TYPE_SIZE_UNIT (type));
3872 : 6635 : t = size_binop_loc (loc, PLUS_EXPR, rhs, t);
3873 : 6635 : t = build4_loc (loc, ARRAY_REF, type, TREE_OPERAND (lhs, 0),
3874 : : t, NULL_TREE, NULL_TREE);
3875 : 6635 : t = cp_build_addr_expr (t, tf_warning_or_error);
3876 : 6635 : t = cp_fold_convert (orig_type, t);
3877 : 6635 : return cxx_eval_constant_expression (ctx, t, vc_prvalue,
3878 : 6635 : non_constant_p, overflow_p);
3879 : : }
3880 : :
3881 : : return NULL_TREE;
3882 : : }
3883 : :
3884 : : /* Try to fold expressions like
3885 : : (struct S *) (&a[0].D.2378 + 12)
3886 : : into
3887 : : &MEM <struct T> [(void *)&a + 12B]
3888 : : This is something normally done by gimple_fold_stmt_to_constant_1
3889 : : on GIMPLE, but is undesirable on GENERIC if we are e.g. going to
3890 : : dereference the address because some details are lost.
3891 : : For pointer comparisons we want such folding though so that
3892 : : match.pd address_compare optimization works. */
3893 : :
3894 : : static tree
3895 : 2054754 : cxx_maybe_fold_addr_pointer_plus (tree t)
3896 : : {
3897 : 4109508 : while (CONVERT_EXPR_P (t)
3898 : 2224809 : && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0))))
3899 : 170055 : t = TREE_OPERAND (t, 0);
3900 : 2054754 : if (TREE_CODE (t) != POINTER_PLUS_EXPR)
3901 : : return NULL_TREE;
3902 : 1771482 : tree op0 = TREE_OPERAND (t, 0);
3903 : 1771482 : tree op1 = TREE_OPERAND (t, 1);
3904 : 1771482 : if (TREE_CODE (op1) != INTEGER_CST)
3905 : : return NULL_TREE;
3906 : 1771482 : while (CONVERT_EXPR_P (op0)
3907 : 3542210 : && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
3908 : 1770728 : op0 = TREE_OPERAND (op0, 0);
3909 : 1771482 : if (TREE_CODE (op0) != ADDR_EXPR)
3910 : : return NULL_TREE;
3911 : 1771482 : op1 = fold_convert (ptr_type_node, op1);
3912 : 1771482 : tree r = fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (op0)), op0, op1);
3913 : 1771482 : return build1_loc (EXPR_LOCATION (t), ADDR_EXPR, TREE_TYPE (op0), r);
3914 : : }
3915 : :
3916 : : /* Subroutine of cxx_eval_constant_expression.
3917 : : Like cxx_eval_unary_expression, except for binary expressions. */
3918 : :
3919 : : static tree
3920 : 46370971 : cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t,
3921 : : value_cat lval,
3922 : : bool *non_constant_p, bool *overflow_p)
3923 : : {
3924 : 46370971 : tree r = NULL_TREE;
3925 : 46370971 : tree orig_lhs = TREE_OPERAND (t, 0);
3926 : 46370971 : tree orig_rhs = TREE_OPERAND (t, 1);
3927 : 46370971 : tree lhs, rhs;
3928 : 46370971 : lhs = cxx_eval_constant_expression (ctx, orig_lhs, vc_prvalue,
3929 : : non_constant_p, overflow_p);
3930 : : /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer
3931 : : subtraction. */
3932 : 46370971 : if (*non_constant_p)
3933 : : return t;
3934 : 33779003 : rhs = cxx_eval_constant_expression (ctx, orig_rhs, vc_prvalue,
3935 : : non_constant_p, overflow_p);
3936 : 33779003 : if (*non_constant_p)
3937 : : return t;
3938 : :
3939 : 32870117 : location_t loc = EXPR_LOCATION (t);
3940 : 32870117 : enum tree_code code = TREE_CODE (t);
3941 : 32870117 : tree type = TREE_TYPE (t);
3942 : :
3943 : 32870117 : if (code == EQ_EXPR || code == NE_EXPR)
3944 : : {
3945 : 4092372 : bool is_code_eq = (code == EQ_EXPR);
3946 : :
3947 : 4092372 : if (TREE_CODE (lhs) == PTRMEM_CST
3948 : 181 : && TREE_CODE (rhs) == PTRMEM_CST)
3949 : : {
3950 : 54 : tree lmem = PTRMEM_CST_MEMBER (lhs);
3951 : 54 : tree rmem = PTRMEM_CST_MEMBER (rhs);
3952 : 54 : bool eq;
3953 : 54 : if (TREE_CODE (lmem) == TREE_CODE (rmem)
3954 : 54 : && TREE_CODE (lmem) == FIELD_DECL
3955 : 54 : && TREE_CODE (DECL_CONTEXT (lmem)) == UNION_TYPE
3956 : 81 : && same_type_p (DECL_CONTEXT (lmem),
3957 : : DECL_CONTEXT (rmem)))
3958 : : /* If both refer to (possibly different) members of the same union
3959 : : (12.3), they compare equal. */
3960 : : eq = true;
3961 : : else
3962 : 27 : eq = cp_tree_equal (lhs, rhs);
3963 : 54 : r = constant_boolean_node (eq == is_code_eq, type);
3964 : 54 : }
3965 : 4092318 : else if ((TREE_CODE (lhs) == PTRMEM_CST
3966 : 4092191 : || TREE_CODE (rhs) == PTRMEM_CST)
3967 : 4092318 : && (null_member_pointer_value_p (lhs)
3968 : 127 : || null_member_pointer_value_p (rhs)))
3969 : 127 : r = constant_boolean_node (!is_code_eq, type);
3970 : 4092191 : else if (TREE_CODE (lhs) == PTRMEM_CST)
3971 : 0 : lhs = cplus_expand_constant (lhs);
3972 : 4092191 : else if (TREE_CODE (rhs) == PTRMEM_CST)
3973 : 0 : rhs = cplus_expand_constant (rhs);
3974 : : }
3975 : 0 : if (r == NULL_TREE
3976 : 32869936 : && TREE_CODE_CLASS (code) == tcc_comparison
3977 : 11156564 : && POINTER_TYPE_P (TREE_TYPE (lhs)))
3978 : : {
3979 : 1027377 : if (tree lhso = cxx_maybe_fold_addr_pointer_plus (lhs))
3980 : 850956 : lhs = fold_convert (TREE_TYPE (lhs), lhso);
3981 : 1027377 : if (tree rhso = cxx_maybe_fold_addr_pointer_plus (rhs))
3982 : 920526 : rhs = fold_convert (TREE_TYPE (rhs), rhso);
3983 : : }
3984 : 2783668 : if (code == POINTER_PLUS_EXPR && !*non_constant_p
3985 : 35653785 : && integer_zerop (lhs) && !integer_zerop (rhs))
3986 : : {
3987 : 155 : if (!ctx->quiet)
3988 : 45 : error ("arithmetic involving a null pointer in %qE", lhs);
3989 : 155 : *non_constant_p = true;
3990 : 155 : return t;
3991 : : }
3992 : 32869962 : else if (code == POINTER_PLUS_EXPR)
3993 : 2783513 : r = cxx_fold_pointer_plus_expression (ctx, t, lhs, rhs, non_constant_p,
3994 : : overflow_p);
3995 : 30086449 : else if (code == SPACESHIP_EXPR)
3996 : : {
3997 : 18029 : r = genericize_spaceship (loc, type, lhs, rhs);
3998 : 18029 : return cxx_eval_constant_expression (ctx, r, lval, non_constant_p,
3999 : 18029 : overflow_p);
4000 : : }
4001 : :
4002 : 32851933 : if (r == NULL_TREE)
4003 : : {
4004 : 32845117 : if (ctx->manifestly_const_eval == mce_true
4005 : 19241811 : && (flag_constexpr_fp_except
4006 : 19241808 : || TREE_CODE (type) != REAL_TYPE))
4007 : : {
4008 : 19229278 : auto ofcc = make_temp_override (folding_cxx_constexpr, true);
4009 : 19229278 : r = fold_binary_initializer_loc (loc, code, type, lhs, rhs);
4010 : 19229278 : }
4011 : : else
4012 : 13615839 : r = fold_binary_loc (loc, code, type, lhs, rhs);
4013 : : }
4014 : :
4015 : 32845117 : if (r == NULL_TREE
4016 : 2263987 : && (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
4017 : 100 : && TREE_CODE (lhs) == INTEGER_CST
4018 : 98 : && TREE_CODE (rhs) == INTEGER_CST
4019 : 35115920 : && wi::neg_p (wi::to_wide (rhs)))
4020 : : {
4021 : : /* For diagnostics and -fpermissive emulate previous behavior of
4022 : : handling shifts by negative amount. */
4023 : 98 : tree nrhs = const_unop (NEGATE_EXPR, TREE_TYPE (rhs), rhs);
4024 : 98 : if (nrhs)
4025 : 119 : r = fold_binary_loc (loc,
4026 : : code == LSHIFT_EXPR ? RSHIFT_EXPR : LSHIFT_EXPR,
4027 : : type, lhs, nrhs);
4028 : : }
4029 : :
4030 : 32851933 : if (r == NULL_TREE)
4031 : : {
4032 : 2263889 : if (lhs == orig_lhs && rhs == orig_rhs)
4033 : : r = t;
4034 : : else
4035 : 635234 : r = build2_loc (loc, code, type, lhs, rhs);
4036 : : }
4037 : 30588044 : else if (cxx_eval_check_shift_p (loc, ctx, code, type, lhs, rhs))
4038 : 600 : *non_constant_p = true;
4039 : : /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
4040 : : a local array in a constexpr function. */
4041 : 32851933 : bool ptr = INDIRECT_TYPE_P (TREE_TYPE (lhs));
4042 : 28952005 : if (!ptr)
4043 : 28952005 : VERIFY_CONSTANT (r);
4044 : : return r;
4045 : : }
4046 : :
4047 : : /* Subroutine of cxx_eval_constant_expression.
4048 : : Attempt to evaluate condition expressions. */
4049 : :
4050 : : static tree
4051 : 7608264 : cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t,
4052 : : value_cat lval,
4053 : : bool *non_constant_p, bool *overflow_p,
4054 : : tree *jump_target)
4055 : : {
4056 : 7608264 : tree val = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
4057 : : vc_prvalue,
4058 : : non_constant_p, overflow_p);
4059 : 7608264 : VERIFY_CONSTANT (val);
4060 : 5997496 : if (TREE_CODE (t) == IF_STMT && IF_STMT_CONSTEVAL_P (t))
4061 : : {
4062 : : /* Evaluate the condition as if it was
4063 : : if (__builtin_is_constant_evaluated ()), i.e. defer it if not
4064 : : ctx->manifestly_const_eval (as sometimes we try to constant evaluate
4065 : : without manifestly_const_eval even expressions or parts thereof which
4066 : : will later be manifestly const_eval evaluated), otherwise fold it to
4067 : : true. */
4068 : 1025274 : if (ctx->manifestly_const_eval == mce_unknown)
4069 : : {
4070 : 1018396 : *non_constant_p = true;
4071 : 1018396 : return t;
4072 : : }
4073 : 6878 : val = constant_boolean_node (ctx->manifestly_const_eval == mce_true,
4074 : : boolean_type_node);
4075 : : }
4076 : : /* Don't VERIFY_CONSTANT the other operands. */
4077 : 4979100 : const bool zero_p = integer_zerop (val);
4078 : 4979100 : if (zero_p)
4079 : 2728993 : val = TREE_OPERAND (t, 2);
4080 : : else
4081 : 2250107 : val = TREE_OPERAND (t, 1);
4082 : 4979100 : if (TREE_CODE (t) == IF_STMT && !val)
4083 : 1263028 : val = void_node;
4084 : :
4085 : : /* P2564: If we aren't in immediate function context (including a manifestly
4086 : : constant-evaluated expression), check any uses of immediate functions in
4087 : : the arm we're discarding. But don't do this inside a call; we already
4088 : : checked when parsing the function. */
4089 : 4979100 : if (ctx->manifestly_const_eval != mce_true
4090 : 1525999 : && !in_immediate_context ()
4091 : 1474061 : && !ctx->call
4092 : 5575914 : && cp_fold_immediate (&TREE_OPERAND (t, zero_p ? 1 : 2),
4093 : 596814 : ctx->manifestly_const_eval))
4094 : : {
4095 : 7 : *non_constant_p = true;
4096 : 7 : return t;
4097 : : }
4098 : :
4099 : : /* A TARGET_EXPR may be nested inside another TARGET_EXPR, but still
4100 : : serve as the initializer for the same object as the outer TARGET_EXPR,
4101 : : as in
4102 : : A a = true ? A{} : A{};
4103 : : so strip the inner TARGET_EXPR so we don't materialize a temporary. */
4104 : 4979093 : if (TREE_CODE (val) == TARGET_EXPR)
4105 : 669 : val = TARGET_EXPR_INITIAL (val);
4106 : 4979093 : return cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
4107 : 4979093 : overflow_p, jump_target);
4108 : : }
4109 : :
4110 : : /* Subroutine of cxx_eval_constant_expression.
4111 : : Attempt to evaluate vector condition expressions. Unlike
4112 : : cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal
4113 : : ternary arithmetics operation, where all 3 arguments have to be
4114 : : evaluated as constants and then folding computes the result from
4115 : : them. */
4116 : :
4117 : : static tree
4118 : 866 : cxx_eval_vector_conditional_expression (const constexpr_ctx *ctx, tree t,
4119 : : bool *non_constant_p, bool *overflow_p)
4120 : : {
4121 : 866 : tree arg1 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
4122 : : vc_prvalue,
4123 : : non_constant_p, overflow_p);
4124 : 866 : VERIFY_CONSTANT (arg1);
4125 : 657 : tree arg2 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
4126 : : vc_prvalue,
4127 : : non_constant_p, overflow_p);
4128 : 657 : VERIFY_CONSTANT (arg2);
4129 : 657 : tree arg3 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2),
4130 : : vc_prvalue,
4131 : : non_constant_p, overflow_p);
4132 : 657 : VERIFY_CONSTANT (arg3);
4133 : 657 : location_t loc = EXPR_LOCATION (t);
4134 : 657 : tree type = TREE_TYPE (t);
4135 : 657 : tree r = fold_ternary_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
4136 : 657 : if (r == NULL_TREE)
4137 : : {
4138 : 0 : if (arg1 == TREE_OPERAND (t, 0)
4139 : 0 : && arg2 == TREE_OPERAND (t, 1)
4140 : 0 : && arg3 == TREE_OPERAND (t, 2))
4141 : : r = t;
4142 : : else
4143 : 0 : r = build3_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
4144 : : }
4145 : 657 : VERIFY_CONSTANT (r);
4146 : : return r;
4147 : : }
4148 : :
4149 : : /* Returns less than, equal to, or greater than zero if KEY is found to be
4150 : : less than, to match, or to be greater than the constructor_elt's INDEX. */
4151 : :
4152 : : static int
4153 : 68549 : array_index_cmp (tree key, tree index)
4154 : : {
4155 : 68549 : gcc_assert (TREE_CODE (key) == INTEGER_CST);
4156 : :
4157 : 68549 : switch (TREE_CODE (index))
4158 : : {
4159 : 65560 : case INTEGER_CST:
4160 : 65560 : return tree_int_cst_compare (key, index);
4161 : 2989 : case RANGE_EXPR:
4162 : 2989 : {
4163 : 2989 : tree lo = TREE_OPERAND (index, 0);
4164 : 2989 : tree hi = TREE_OPERAND (index, 1);
4165 : 2989 : if (tree_int_cst_lt (key, lo))
4166 : : return -1;
4167 : 2720 : else if (tree_int_cst_lt (hi, key))
4168 : : return 1;
4169 : : else
4170 : 2720 : return 0;
4171 : : }
4172 : 0 : default:
4173 : 0 : gcc_unreachable ();
4174 : : }
4175 : : }
4176 : :
4177 : : /* Extract a single INTEGER_CST from RAW_DATA_CST RAW_DATA at
4178 : : relative index OFF. */
4179 : :
4180 : : static tree
4181 : 29084 : raw_data_cst_elt (tree raw_data, unsigned int off)
4182 : : {
4183 : 29084 : return build_int_cst (TREE_TYPE (raw_data),
4184 : 29084 : TYPE_UNSIGNED (TREE_TYPE (raw_data))
4185 : 58168 : ? (HOST_WIDE_INT)
4186 : 29084 : RAW_DATA_UCHAR_ELT (raw_data, off)
4187 : : : (HOST_WIDE_INT)
4188 : 0 : RAW_DATA_SCHAR_ELT (raw_data, off));
4189 : : }
4190 : :
4191 : : /* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1
4192 : : if none. If INSERT is true, insert a matching element rather than fail. */
4193 : :
4194 : : static HOST_WIDE_INT
4195 : 1544399 : find_array_ctor_elt (tree ary, tree dindex, bool insert)
4196 : : {
4197 : 1544399 : if (tree_int_cst_sgn (dindex) < 0)
4198 : : return -1;
4199 : :
4200 : 1544399 : unsigned HOST_WIDE_INT i = tree_to_uhwi (dindex);
4201 : 1544399 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ary);
4202 : 1544399 : unsigned HOST_WIDE_INT len = vec_safe_length (elts);
4203 : :
4204 : 2363768 : unsigned HOST_WIDE_INT end = len;
4205 : 2363768 : unsigned HOST_WIDE_INT begin = 0;
4206 : :
4207 : : /* If the last element of the CONSTRUCTOR has its own index, we can assume
4208 : : that the same is true of the other elements and index directly. */
4209 : 1456685 : if (end > 0)
4210 : : {
4211 : 1435506 : tree cindex = (*elts)[end - 1].index;
4212 : 1435506 : if (cindex == NULL_TREE)
4213 : : {
4214 : : /* Verify that if the last index is missing, all indexes
4215 : : are missing and there is no RAW_DATA_CST. */
4216 : 0 : if (flag_checking)
4217 : 0 : for (unsigned int j = 0; j < len - 1; ++j)
4218 : 0 : gcc_assert ((*elts)[j].index == NULL_TREE
4219 : : && TREE_CODE ((*elts)[j].value) != RAW_DATA_CST);
4220 : 0 : if (i < end)
4221 : 0 : return i;
4222 : : else
4223 : : {
4224 : 0 : begin = end;
4225 : 0 : if (i == end)
4226 : : /* If the element is to be added right at the end,
4227 : : make sure it is added with cleared index too. */
4228 : 907083 : dindex = NULL_TREE;
4229 : 0 : else if (insert)
4230 : : /* Otherwise, in order not to break the assumption
4231 : : that CONSTRUCTOR either has all indexes or none,
4232 : : we need to add indexes to all elements. */
4233 : 0 : for (unsigned int j = 0; j < len; ++j)
4234 : 0 : (*elts)[j].index = build_int_cst (TREE_TYPE (dindex), j);
4235 : : }
4236 : : }
4237 : 1435506 : else if (TREE_CODE (cindex) == INTEGER_CST
4238 : 1435506 : && compare_tree_int (cindex, end - 1) == 0)
4239 : : {
4240 : 1417629 : tree value = (*elts)[end - 1].value;
4241 : 1417629 : if (i < end)
4242 : : {
4243 : 637406 : if (i == end - 1 && TREE_CODE (value) == RAW_DATA_CST)
4244 : : begin = end - 1;
4245 : : else
4246 : 637316 : return i;
4247 : : }
4248 : 780223 : else if (TREE_CODE (value) == RAW_DATA_CST
4249 : 812401 : && wi::to_offset (dindex) < (wi::to_offset (cindex)
4250 : 32178 : + RAW_DATA_LENGTH (value)))
4251 : 16089 : begin = end - 1;
4252 : : else
4253 : 764134 : begin = end;
4254 : : }
4255 : : }
4256 : :
4257 : : /* Otherwise, find a matching index by means of a binary search. */
4258 : 924991 : while (begin != end)
4259 : : {
4260 : 50193 : unsigned HOST_WIDE_INT middle = (begin + end) / 2;
4261 : 50193 : constructor_elt &elt = (*elts)[middle];
4262 : 50193 : tree idx = elt.index;
4263 : :
4264 : 50193 : int cmp = array_index_cmp (dindex, idx);
4265 : 50193 : if (cmp > 0
4266 : 43438 : && TREE_CODE (elt.value) == RAW_DATA_CST
4267 : 133067 : && wi::to_offset (dindex) < (wi::to_offset (idx)
4268 : 82874 : + RAW_DATA_LENGTH (elt.value)))
4269 : 28950 : cmp = 0;
4270 : 50193 : if (cmp < 0)
4271 : : end = middle;
4272 : 46773 : else if (cmp > 0)
4273 : 14488 : begin = middle + 1;
4274 : : else
4275 : : {
4276 : 32285 : if (insert && TREE_CODE (elt.value) == RAW_DATA_CST)
4277 : : {
4278 : : /* We need to split the RAW_DATA_CST elt. */
4279 : 122 : constructor_elt e;
4280 : 122 : gcc_checking_assert (TREE_CODE (idx) != RANGE_EXPR);
4281 : 244 : unsigned int off = (wi::to_offset (dindex)
4282 : 244 : - wi::to_offset (idx)).to_uhwi ();
4283 : 122 : tree value = elt.value;
4284 : 122 : unsigned int len = RAW_DATA_LENGTH (value);
4285 : 122 : if (off > 1 && len >= off + 3)
4286 : 22 : value = copy_node (elt.value);
4287 : 122 : if (off)
4288 : : {
4289 : 33 : if (off > 1)
4290 : 33 : RAW_DATA_LENGTH (elt.value) = off;
4291 : : else
4292 : 0 : elt.value = raw_data_cst_elt (elt.value, 0);
4293 : 33 : e.index = size_binop (PLUS_EXPR, elt.index,
4294 : : build_int_cst (TREE_TYPE (elt.index),
4295 : : off));
4296 : 33 : e.value = NULL_TREE;
4297 : 33 : ++middle;
4298 : 33 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e);
4299 : : }
4300 : 122 : (*elts)[middle].value = raw_data_cst_elt (value, off);
4301 : 122 : if (len >= off + 2)
4302 : : {
4303 : 111 : e.index = (*elts)[middle].index;
4304 : 111 : e.index = size_binop (PLUS_EXPR, e.index,
4305 : : build_one_cst (TREE_TYPE (e.index)));
4306 : 111 : if (len >= off + 3)
4307 : : {
4308 : 111 : RAW_DATA_LENGTH (value) -= off + 1;
4309 : 111 : RAW_DATA_POINTER (value) += off + 1;
4310 : 111 : e.value = value;
4311 : : }
4312 : : else
4313 : 0 : e.value = raw_data_cst_elt (value, off + 1);
4314 : 111 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e);
4315 : : }
4316 : 122 : return middle;
4317 : : }
4318 : 430 : if (insert && TREE_CODE (idx) == RANGE_EXPR)
4319 : : {
4320 : : /* We need to split the range. */
4321 : 342 : constructor_elt e;
4322 : 342 : tree lo = TREE_OPERAND (idx, 0);
4323 : 342 : tree hi = TREE_OPERAND (idx, 1);
4324 : 342 : tree value = elt.value;
4325 : 342 : dindex = fold_convert (sizetype, dindex);
4326 : 342 : if (tree_int_cst_lt (lo, dindex))
4327 : : {
4328 : : /* There are still some lower elts; shorten the range. */
4329 : 170 : tree new_hi = int_const_binop (MINUS_EXPR, dindex,
4330 : 85 : size_one_node);
4331 : 85 : if (tree_int_cst_equal (lo, new_hi))
4332 : : /* Only one element left, no longer a range. */
4333 : 33 : elt.index = lo;
4334 : : else
4335 : 52 : TREE_OPERAND (idx, 1) = new_hi;
4336 : : /* Append the element we want to insert. */
4337 : 85 : ++middle;
4338 : 85 : e.index = dindex;
4339 : 85 : e.value = unshare_constructor (value);
4340 : 85 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e);
4341 : : }
4342 : : else
4343 : : /* No lower elts, the range elt is now ours. */
4344 : 257 : elt.index = dindex;
4345 : :
4346 : 342 : if (tree_int_cst_lt (dindex, hi))
4347 : : {
4348 : : /* There are still some higher elts; append a range. */
4349 : 476 : tree new_lo = int_const_binop (PLUS_EXPR, dindex,
4350 : 238 : size_one_node);
4351 : 238 : if (tree_int_cst_equal (new_lo, hi))
4352 : 97 : e.index = hi;
4353 : : else
4354 : 141 : e.index = build2 (RANGE_EXPR, sizetype, new_lo, hi);
4355 : 238 : e.value = unshare_constructor (value);
4356 : 238 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e);
4357 : : }
4358 : : }
4359 : 32163 : return middle;
4360 : : }
4361 : : }
4362 : :
4363 : 874798 : if (insert)
4364 : : {
4365 : 872467 : constructor_elt e = { dindex, NULL_TREE };
4366 : 872467 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), end, e);
4367 : 872467 : return end;
4368 : : }
4369 : :
4370 : : return -1;
4371 : : }
4372 : :
4373 : : /* Return a pointer to the constructor_elt of CTOR which matches INDEX. If no
4374 : : matching constructor_elt exists, then add one to CTOR.
4375 : :
4376 : : As an optimization, if POS_HINT is non-negative then it is used as a guess
4377 : : for the (integer) index of the matching constructor_elt within CTOR. */
4378 : :
4379 : : static constructor_elt *
4380 : 9188955 : get_or_insert_ctor_field (tree ctor, tree index, int pos_hint = -1)
4381 : : {
4382 : : /* Check the hint first. */
4383 : 820621 : if (pos_hint >= 0 && (unsigned)pos_hint < CONSTRUCTOR_NELTS (ctor)
4384 : 10009576 : && CONSTRUCTOR_ELT (ctor, pos_hint)->index == index)
4385 : : return CONSTRUCTOR_ELT (ctor, pos_hint);
4386 : :
4387 : 8368339 : tree type = TREE_TYPE (ctor);
4388 : 8368339 : if (TREE_CODE (type) == VECTOR_TYPE && index == NULL_TREE)
4389 : : {
4390 : 128 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (ctor), index, NULL_TREE);
4391 : 128 : return &CONSTRUCTOR_ELTS (ctor)->last();
4392 : : }
4393 : 8368211 : else if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
4394 : : {
4395 : 1191422 : if (TREE_CODE (index) == RANGE_EXPR)
4396 : : {
4397 : : /* Support for RANGE_EXPR index lookups is currently limited to
4398 : : accessing an existing element via POS_HINT, or appending a new
4399 : : element to the end of CTOR. ??? Support for other access
4400 : : patterns may also be needed. */
4401 : 3 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
4402 : 3 : if (vec_safe_length (elts))
4403 : : {
4404 : 3 : tree lo = TREE_OPERAND (index, 0);
4405 : 3 : gcc_assert (array_index_cmp (elts->last().index, lo) < 0);
4406 : : }
4407 : 3 : CONSTRUCTOR_APPEND_ELT (elts, index, NULL_TREE);
4408 : 3 : return &elts->last();
4409 : : }
4410 : :
4411 : 1191419 : HOST_WIDE_INT i = find_array_ctor_elt (ctor, index, /*insert*/true);
4412 : 1191419 : gcc_assert (i >= 0);
4413 : 1191419 : constructor_elt *cep = CONSTRUCTOR_ELT (ctor, i);
4414 : 1191419 : gcc_assert (cep->index == NULL_TREE
4415 : : || TREE_CODE (cep->index) != RANGE_EXPR);
4416 : : return cep;
4417 : : }
4418 : : else
4419 : : {
4420 : 7176789 : gcc_assert (TREE_CODE (index) == FIELD_DECL
4421 : : && (same_type_ignoring_top_level_qualifiers_p
4422 : : (DECL_CONTEXT (index), TREE_TYPE (ctor))));
4423 : :
4424 : : /* We must keep the CONSTRUCTOR's ELTS in FIELD order.
4425 : : Usually we meet initializers in that order, but it is
4426 : : possible for base types to be placed not in program
4427 : : order. */
4428 : 7176789 : tree fields = TYPE_FIELDS (DECL_CONTEXT (index));
4429 : 7176789 : unsigned HOST_WIDE_INT idx = 0;
4430 : 7176789 : constructor_elt *cep = NULL;
4431 : :
4432 : : /* Check if we're changing the active member of a union. */
4433 : 148368 : if (TREE_CODE (type) == UNION_TYPE && CONSTRUCTOR_NELTS (ctor)
4434 : 7219255 : && CONSTRUCTOR_ELT (ctor, 0)->index != index)
4435 : 3355 : vec_safe_truncate (CONSTRUCTOR_ELTS (ctor), 0);
4436 : : /* If the bit offset of INDEX is larger than that of the last
4437 : : constructor_elt, then we can just immediately append a new
4438 : : constructor_elt to the end of CTOR. */
4439 : 7173434 : else if (CONSTRUCTOR_NELTS (ctor)
4440 : 8400227 : && tree_int_cst_compare (bit_position (index),
4441 : 8153584 : bit_position (CONSTRUCTOR_ELTS (ctor)
4442 : 4076792 : ->last().index)) > 0)
4443 : : {
4444 : 1764603 : idx = CONSTRUCTOR_NELTS (ctor);
4445 : 1764603 : goto insert;
4446 : : }
4447 : :
4448 : : /* Otherwise, we need to iterate over CTOR to find or insert INDEX
4449 : : appropriately. */
4450 : :
4451 : 6292025 : for (; vec_safe_iterate (CONSTRUCTOR_ELTS (ctor), idx, &cep);
4452 : 879839 : idx++, fields = DECL_CHAIN (fields))
4453 : : {
4454 : 3192028 : if (index == cep->index)
4455 : 2302563 : goto found;
4456 : :
4457 : : /* The field we're initializing must be on the field
4458 : : list. Look to see if it is present before the
4459 : : field the current ELT initializes. */
4460 : 12369038 : for (; fields != cep->index; fields = DECL_CHAIN (fields))
4461 : 11489199 : if (index == fields)
4462 : 9626 : goto insert;
4463 : : }
4464 : : /* We fell off the end of the CONSTRUCTOR, so insert a new
4465 : : entry at the end. */
4466 : :
4467 : 4874226 : insert:
4468 : 4874226 : {
4469 : 4874226 : constructor_elt ce = { index, NULL_TREE };
4470 : :
4471 : 4874226 : vec_safe_insert (CONSTRUCTOR_ELTS (ctor), idx, ce);
4472 : 4874226 : cep = CONSTRUCTOR_ELT (ctor, idx);
4473 : : }
4474 : 7176789 : found:;
4475 : :
4476 : 7176789 : return cep;
4477 : : }
4478 : : }
4479 : :
4480 : : /* Under the control of CTX, issue a detailed diagnostic for
4481 : : an out-of-bounds subscript INDEX into the expression ARRAY. */
4482 : :
4483 : : static void
4484 : 541 : diag_array_subscript (location_t loc, const constexpr_ctx *ctx, tree array, tree index)
4485 : : {
4486 : 541 : if (!ctx->quiet)
4487 : : {
4488 : 115 : tree arraytype = TREE_TYPE (array);
4489 : :
4490 : : /* Convert the unsigned array subscript to a signed integer to avoid
4491 : : printing huge numbers for small negative values. */
4492 : 115 : tree sidx = fold_convert (ssizetype, index);
4493 : 115 : STRIP_ANY_LOCATION_WRAPPER (array);
4494 : 115 : if (DECL_P (array))
4495 : : {
4496 : 75 : auto_diagnostic_group d;
4497 : 75 : if (TYPE_DOMAIN (arraytype))
4498 : 69 : error_at (loc, "array subscript value %qE is outside the bounds "
4499 : : "of array %qD of type %qT", sidx, array, arraytype);
4500 : : else
4501 : 6 : error_at (loc, "nonzero array subscript %qE is used with array %qD of "
4502 : : "type %qT with unknown bounds", sidx, array, arraytype);
4503 : 75 : inform (DECL_SOURCE_LOCATION (array), "declared here");
4504 : 75 : }
4505 : 40 : else if (TYPE_DOMAIN (arraytype))
4506 : 37 : error_at (loc, "array subscript value %qE is outside the bounds "
4507 : : "of array type %qT", sidx, arraytype);
4508 : : else
4509 : 3 : error_at (loc, "nonzero array subscript %qE is used with array of type %qT "
4510 : : "with unknown bounds", sidx, arraytype);
4511 : : }
4512 : 541 : }
4513 : :
4514 : : /* Return the number of elements for TYPE (which is an ARRAY_TYPE or
4515 : : a VECTOR_TYPE). */
4516 : :
4517 : : static tree
4518 : 3460249 : get_array_or_vector_nelts (const constexpr_ctx *ctx, tree type,
4519 : : bool *non_constant_p, bool *overflow_p)
4520 : : {
4521 : 3460249 : tree nelts;
4522 : 3460249 : if (TREE_CODE (type) == ARRAY_TYPE)
4523 : : {
4524 : 3460249 : if (TYPE_DOMAIN (type))
4525 : 3460013 : nelts = array_type_nelts_top (type);
4526 : : else
4527 : 236 : nelts = size_zero_node;
4528 : : }
4529 : 0 : else if (VECTOR_TYPE_P (type))
4530 : 0 : nelts = size_int (TYPE_VECTOR_SUBPARTS (type));
4531 : : else
4532 : 0 : gcc_unreachable ();
4533 : :
4534 : : /* For VLAs, the number of elements won't be an integer constant. */
4535 : 3460249 : nelts = cxx_eval_constant_expression (ctx, nelts, vc_prvalue,
4536 : : non_constant_p, overflow_p);
4537 : 3460249 : return nelts;
4538 : : }
4539 : :
4540 : : /* Extract element INDEX consisting of CHARS_PER_ELT chars from
4541 : : STRING_CST STRING. */
4542 : :
4543 : : static tree
4544 : 949028 : extract_string_elt (tree string, unsigned chars_per_elt, unsigned index)
4545 : : {
4546 : 949028 : tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (string)));
4547 : 949028 : tree r;
4548 : :
4549 : 949028 : if (chars_per_elt == 1)
4550 : 690004 : r = build_int_cst (type, TREE_STRING_POINTER (string)[index]);
4551 : : else
4552 : : {
4553 : 259024 : const unsigned char *ptr
4554 : 259024 : = ((const unsigned char *)TREE_STRING_POINTER (string)
4555 : 259024 : + index * chars_per_elt);
4556 : 259024 : r = native_interpret_expr (type, ptr, chars_per_elt);
4557 : : }
4558 : 949028 : return r;
4559 : : }
4560 : :
4561 : : /* Subroutine of cxx_eval_array_reference. T is an ARRAY_REF; evaluate the
4562 : : subscript, diagnose any problems with it, and return the result. */
4563 : :
4564 : : static tree
4565 : 3461001 : eval_and_check_array_index (const constexpr_ctx *ctx,
4566 : : tree t, bool allow_one_past,
4567 : : bool *non_constant_p, bool *overflow_p)
4568 : : {
4569 : 3461001 : location_t loc = cp_expr_loc_or_input_loc (t);
4570 : 3461001 : tree ary = TREE_OPERAND (t, 0);
4571 : 3461001 : t = TREE_OPERAND (t, 1);
4572 : 3461001 : tree index = cxx_eval_constant_expression (ctx, t, vc_prvalue,
4573 : : non_constant_p, overflow_p);
4574 : 3461001 : VERIFY_CONSTANT (index);
4575 : :
4576 : 3459769 : if (!tree_fits_shwi_p (index)
4577 : 3459769 : || tree_int_cst_sgn (index) < 0)
4578 : : {
4579 : 108 : diag_array_subscript (loc, ctx, ary, index);
4580 : 108 : *non_constant_p = true;
4581 : 108 : return t;
4582 : : }
4583 : :
4584 : 3459661 : tree nelts = get_array_or_vector_nelts (ctx, TREE_TYPE (ary), non_constant_p,
4585 : : overflow_p);
4586 : 3459661 : VERIFY_CONSTANT (nelts);
4587 : 3459598 : if (allow_one_past
4588 : 3459598 : ? !tree_int_cst_le (index, nelts)
4589 : 2420188 : : !tree_int_cst_lt (index, nelts))
4590 : : {
4591 : 433 : diag_array_subscript (loc, ctx, ary, index);
4592 : 433 : *non_constant_p = true;
4593 : 433 : return t;
4594 : : }
4595 : :
4596 : : return index;
4597 : : }
4598 : :
4599 : : /* Subroutine of cxx_eval_constant_expression.
4600 : : Attempt to reduce a reference to an array slot. */
4601 : :
4602 : : static tree
4603 : 2527521 : cxx_eval_array_reference (const constexpr_ctx *ctx, tree t,
4604 : : value_cat lval,
4605 : : bool *non_constant_p, bool *overflow_p)
4606 : : {
4607 : 2527521 : tree oldary = TREE_OPERAND (t, 0);
4608 : 2527521 : tree ary = cxx_eval_constant_expression (ctx, oldary,
4609 : : lval,
4610 : : non_constant_p, overflow_p);
4611 : 2527521 : if (*non_constant_p)
4612 : : return t;
4613 : 2351580 : if (!lval
4614 : 1311054 : && TREE_CODE (ary) == VIEW_CONVERT_EXPR
4615 : 13228 : && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary, 0)))
4616 : 2364808 : && (TYPE_MAIN_VARIANT (TREE_TYPE (t))
4617 : 13228 : == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary, 0))))))
4618 : 13228 : ary = TREE_OPERAND (ary, 0);
4619 : :
4620 : 2351580 : tree oldidx = TREE_OPERAND (t, 1);
4621 : 2351580 : tree index = eval_and_check_array_index (ctx, t, lval,
4622 : : non_constant_p, overflow_p);
4623 : 2351580 : if (*non_constant_p)
4624 : : return t;
4625 : :
4626 : 2349798 : if (lval && ary == oldary && index == oldidx)
4627 : : return t;
4628 : 1395875 : else if (lval == vc_discard)
4629 : : return t;
4630 : 1395875 : else if (lval)
4631 : 85249 : return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL);
4632 : :
4633 : 1310626 : unsigned len = 0, elem_nchars = 1;
4634 : 1310626 : tree elem_type = TREE_TYPE (TREE_TYPE (ary));
4635 : 1310626 : if (TREE_CODE (ary) == CONSTRUCTOR)
4636 : 352101 : len = CONSTRUCTOR_NELTS (ary);
4637 : 958525 : else if (TREE_CODE (ary) == STRING_CST)
4638 : : {
4639 : 945305 : elem_nchars = (TYPE_PRECISION (elem_type)
4640 : 945305 : / TYPE_PRECISION (char_type_node));
4641 : 945305 : len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars;
4642 : : }
4643 : 13220 : else if (TREE_CODE (ary) == VECTOR_CST)
4644 : : /* We don't create variable-length VECTOR_CSTs. */
4645 : 13220 : len = VECTOR_CST_NELTS (ary).to_constant ();
4646 : : else
4647 : : {
4648 : : /* We can't do anything with other tree codes, so use
4649 : : VERIFY_CONSTANT to complain and fail. */
4650 : 0 : VERIFY_CONSTANT (ary);
4651 : 0 : gcc_unreachable ();
4652 : : }
4653 : :
4654 : 1310626 : bool found;
4655 : 1310626 : HOST_WIDE_INT i = 0;
4656 : 1310626 : if (TREE_CODE (ary) == CONSTRUCTOR)
4657 : : {
4658 : 352101 : HOST_WIDE_INT ix = find_array_ctor_elt (ary, index);
4659 : 352101 : found = (ix >= 0);
4660 : 352101 : if (found)
4661 : 349856 : i = ix;
4662 : : }
4663 : : else
4664 : : {
4665 : 958525 : i = tree_to_shwi (index);
4666 : 958525 : found = (i < len);
4667 : : }
4668 : :
4669 : 1310626 : if (found)
4670 : : {
4671 : 1307942 : tree r;
4672 : 1307942 : if (TREE_CODE (ary) == CONSTRUCTOR)
4673 : : {
4674 : 349856 : r = (*CONSTRUCTOR_ELTS (ary))[i].value;
4675 : 349856 : if (TREE_CODE (r) == RAW_DATA_CST)
4676 : : {
4677 : 28962 : tree ridx = (*CONSTRUCTOR_ELTS (ary))[i].index;
4678 : 28962 : gcc_checking_assert (ridx);
4679 : 28962 : unsigned int off
4680 : 28962 : = (wi::to_offset (index) - wi::to_offset (ridx)).to_uhwi ();
4681 : 28962 : r = raw_data_cst_elt (r, off);
4682 : : }
4683 : : }
4684 : 958086 : else if (TREE_CODE (ary) == VECTOR_CST)
4685 : 13220 : r = VECTOR_CST_ELT (ary, i);
4686 : : else
4687 : 944866 : r = extract_string_elt (ary, elem_nchars, i);
4688 : :
4689 : 987048 : if (r)
4690 : : /* Don't VERIFY_CONSTANT here. */
4691 : 1307942 : return r;
4692 : :
4693 : : /* Otherwise the element doesn't have a value yet. */
4694 : : }
4695 : :
4696 : : /* Not found. */
4697 : :
4698 : 2684 : if (is_really_empty_class (elem_type, /*ignore_vptr*/false))
4699 : 49 : return build_constructor (elem_type, NULL);
4700 : :
4701 : 2635 : if (TREE_CODE (ary) == CONSTRUCTOR
4702 : 2635 : && CONSTRUCTOR_NO_CLEARING (ary))
4703 : : {
4704 : : /* 'ary' is part of the aggregate initializer we're currently
4705 : : building; if there's no initializer for this element yet,
4706 : : that's an error. */
4707 : 51 : if (!ctx->quiet)
4708 : 16 : error ("accessing uninitialized array element");
4709 : 51 : *non_constant_p = true;
4710 : 51 : return t;
4711 : : }
4712 : :
4713 : : /* If it's within the array bounds but doesn't have an explicit
4714 : : initializer, it's initialized from {}. But use build_value_init
4715 : : directly for non-aggregates to avoid creating a garbage CONSTRUCTOR. */
4716 : 2584 : tree val;
4717 : 2584 : constexpr_ctx new_ctx;
4718 : 2584 : if (CP_AGGREGATE_TYPE_P (elem_type))
4719 : : {
4720 : 405 : tree empty_ctor = build_constructor (init_list_type_node, NULL);
4721 : 405 : val = digest_init (elem_type, empty_ctor, tf_warning_or_error);
4722 : : }
4723 : : else
4724 : 2179 : val = build_value_init (elem_type, tf_warning_or_error);
4725 : :
4726 : : /* Create a new constructor only if we don't already have a suitable one. */
4727 : 2584 : const bool new_ctor = (!SCALAR_TYPE_P (elem_type)
4728 : 3015 : && (!ctx->ctor
4729 : 15 : || !same_type_ignoring_top_level_qualifiers_p
4730 : 15 : (elem_type, TREE_TYPE (ctx->ctor))));
4731 : 422 : if (new_ctor)
4732 : : {
4733 : 422 : new_ctx = *ctx;
4734 : : /* We clear the object here. We used to replace it with T, but that
4735 : : caused problems (101371, 108158); and anyway, T is the initializer,
4736 : : not the target object. */
4737 : 422 : new_ctx.object = NULL_TREE;
4738 : 422 : new_ctx.ctor = build_constructor (elem_type, NULL);
4739 : 422 : ctx = &new_ctx;
4740 : : }
4741 : 2584 : t = cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
4742 : : overflow_p);
4743 : 2584 : if (new_ctor && t != ctx->ctor)
4744 : 401 : free_constructor (ctx->ctor);
4745 : : return t;
4746 : : }
4747 : :
4748 : : /* Subroutine of cxx_eval_constant_expression.
4749 : : Attempt to reduce a field access of a value of class type. */
4750 : :
4751 : : static tree
4752 : 27806341 : cxx_eval_component_reference (const constexpr_ctx *ctx, tree t,
4753 : : value_cat lval,
4754 : : bool *non_constant_p, bool *overflow_p)
4755 : : {
4756 : 27806341 : unsigned HOST_WIDE_INT i;
4757 : 27806341 : tree field;
4758 : 27806341 : tree value;
4759 : 27806341 : tree part = TREE_OPERAND (t, 1);
4760 : 27806341 : tree orig_whole = TREE_OPERAND (t, 0);
4761 : 27806341 : tree whole = cxx_eval_constant_expression (ctx, orig_whole,
4762 : : lval,
4763 : : non_constant_p, overflow_p);
4764 : 27806341 : if (*non_constant_p)
4765 : : return t;
4766 : 13722296 : if (INDIRECT_REF_P (whole)
4767 : 13722296 : && integer_zerop (TREE_OPERAND (whole, 0)))
4768 : : {
4769 : 177 : if (!ctx->quiet)
4770 : 39 : error ("dereferencing a null pointer in %qE", orig_whole);
4771 : 177 : *non_constant_p = true;
4772 : 177 : return t;
4773 : : }
4774 : :
4775 : 13722119 : if (TREE_CODE (whole) == PTRMEM_CST)
4776 : 1291 : whole = cplus_expand_constant (whole);
4777 : 13722119 : if (whole == orig_whole)
4778 : : return t;
4779 : 8525602 : if (lval == vc_discard)
4780 : : return t;
4781 : 8525578 : if (lval)
4782 : 3722886 : return fold_build3 (COMPONENT_REF, TREE_TYPE (t),
4783 : : whole, part, NULL_TREE);
4784 : : /* Don't VERIFY_CONSTANT here; we only want to check that we got a
4785 : : CONSTRUCTOR. */
4786 : 4802692 : if (TREE_CODE (whole) != CONSTRUCTOR)
4787 : : {
4788 : 0 : if (!ctx->quiet)
4789 : 0 : error ("%qE is not a constant expression", orig_whole);
4790 : 0 : *non_constant_p = true;
4791 : 0 : return t;
4792 : : }
4793 : 4801403 : if ((cxx_dialect < cxx14 || CONSTRUCTOR_MUTABLE_POISON (whole))
4794 : 4804151 : && DECL_MUTABLE_P (part))
4795 : : {
4796 : 132 : if (!ctx->quiet)
4797 : 16 : error ("mutable %qD is not usable in a constant expression", part);
4798 : 132 : *non_constant_p = true;
4799 : 132 : return t;
4800 : : }
4801 : :
4802 : 4802560 : bool pmf = TYPE_PTRMEMFUNC_P (TREE_TYPE (whole));
4803 : 6808036 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
4804 : : {
4805 : : /* Use name match for PMF fields, as a variant will have a
4806 : : different FIELD_DECL with a different type. */
4807 : 6804814 : if (pmf ? DECL_NAME (field) == DECL_NAME (part)
4808 : : : field == part)
4809 : : {
4810 : 4799338 : if (value)
4811 : : {
4812 : 4799320 : STRIP_ANY_LOCATION_WRAPPER (value);
4813 : 4799320 : return value;
4814 : : }
4815 : : else
4816 : : /* We're in the middle of initializing it. */
4817 : : break;
4818 : : }
4819 : : }
4820 : 3240 : if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE)
4821 : : {
4822 : 86 : if (CONSTRUCTOR_NELTS (whole) > 0)
4823 : : {
4824 : : /* DR 1188 says we don't have to deal with this. */
4825 : 71 : if (!ctx->quiet)
4826 : : {
4827 : 18 : constructor_elt *cep = CONSTRUCTOR_ELT (whole, 0);
4828 : 18 : if (cep->value == NULL_TREE)
4829 : 9 : error ("accessing uninitialized member %qD", part);
4830 : : else
4831 : 9 : error ("accessing %qD member instead of initialized %qD member "
4832 : : "in constant expression", part, cep->index);
4833 : : }
4834 : 71 : *non_constant_p = true;
4835 : 71 : return t;
4836 : : }
4837 : 15 : else if (!CONSTRUCTOR_NO_CLEARING (whole))
4838 : : {
4839 : : /* Value-initialized union, check if looking at the first member. */
4840 : 12 : tree first = next_aggregate_field (TYPE_FIELDS (TREE_TYPE (whole)));
4841 : 12 : if (first != part)
4842 : : {
4843 : 9 : if (!ctx->quiet)
4844 : 3 : error ("accessing %qD member instead of initialized %qD "
4845 : : "member in constant expression", part, first);
4846 : 9 : *non_constant_p = true;
4847 : 9 : return t;
4848 : : }
4849 : : }
4850 : : }
4851 : :
4852 : : /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty
4853 : : classes never get represented; throw together a value now. */
4854 : 3160 : if (is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
4855 : 1549 : return build_constructor (TREE_TYPE (t), NULL);
4856 : :
4857 : 1611 : gcc_assert (DECL_CONTEXT (part) == TYPE_MAIN_VARIANT (TREE_TYPE (whole)));
4858 : :
4859 : 1611 : if (CONSTRUCTOR_NO_CLEARING (whole))
4860 : : {
4861 : : /* 'whole' is part of the aggregate initializer we're currently
4862 : : building; if there's no initializer for this member yet, that's an
4863 : : error. */
4864 : 1390 : if (!ctx->quiet)
4865 : 21 : error ("accessing uninitialized member %qD", part);
4866 : 1390 : *non_constant_p = true;
4867 : 1390 : return t;
4868 : : }
4869 : :
4870 : : /* If there's no explicit init for this field, it's value-initialized. */
4871 : 221 : value = build_value_init (TREE_TYPE (t), tf_warning_or_error);
4872 : 221 : return cxx_eval_constant_expression (ctx, value,
4873 : : lval,
4874 : 221 : non_constant_p, overflow_p);
4875 : : }
4876 : :
4877 : : /* Subroutine of cxx_eval_constant_expression.
4878 : : Attempt to reduce a field access of a value of class type that is
4879 : : expressed as a BIT_FIELD_REF. */
4880 : :
4881 : : static tree
4882 : 49 : cxx_eval_bit_field_ref (const constexpr_ctx *ctx, tree t,
4883 : : value_cat lval,
4884 : : bool *non_constant_p, bool *overflow_p)
4885 : : {
4886 : 49 : tree orig_whole = TREE_OPERAND (t, 0);
4887 : 49 : tree retval, fldval, utype, mask;
4888 : 49 : bool fld_seen = false;
4889 : 49 : HOST_WIDE_INT istart, isize;
4890 : 49 : tree whole = cxx_eval_constant_expression (ctx, orig_whole,
4891 : : lval,
4892 : : non_constant_p, overflow_p);
4893 : 49 : tree start, field, value;
4894 : 49 : unsigned HOST_WIDE_INT i;
4895 : :
4896 : 49 : if (whole == orig_whole)
4897 : : return t;
4898 : : /* Don't VERIFY_CONSTANT here; we only want to check that we got a
4899 : : CONSTRUCTOR. */
4900 : 2 : if (!*non_constant_p
4901 : 2 : && TREE_CODE (whole) != VECTOR_CST
4902 : 0 : && TREE_CODE (whole) != CONSTRUCTOR)
4903 : : {
4904 : 0 : if (!ctx->quiet)
4905 : 0 : error ("%qE is not a constant expression", orig_whole);
4906 : 0 : *non_constant_p = true;
4907 : : }
4908 : 2 : if (*non_constant_p)
4909 : : return t;
4910 : :
4911 : 2 : if (TREE_CODE (whole) == VECTOR_CST || !INTEGRAL_TYPE_P (TREE_TYPE (t)))
4912 : : {
4913 : 2 : if (tree r = fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
4914 : : TREE_OPERAND (t, 1), TREE_OPERAND (t, 2)))
4915 : : return r;
4916 : 0 : if (!ctx->quiet)
4917 : 0 : error ("%qE is not a constant expression", orig_whole);
4918 : 0 : *non_constant_p = true;
4919 : 0 : return t;
4920 : : }
4921 : :
4922 : 0 : start = TREE_OPERAND (t, 2);
4923 : 0 : istart = tree_to_shwi (start);
4924 : 0 : isize = tree_to_shwi (TREE_OPERAND (t, 1));
4925 : 0 : utype = TREE_TYPE (t);
4926 : 0 : if (!TYPE_UNSIGNED (utype))
4927 : 0 : utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1);
4928 : 0 : retval = build_int_cst (utype, 0);
4929 : 0 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
4930 : : {
4931 : 0 : tree bitpos = bit_position (field);
4932 : 0 : STRIP_ANY_LOCATION_WRAPPER (value);
4933 : 0 : if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1))
4934 : : return value;
4935 : 0 : if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE
4936 : 0 : && TREE_CODE (value) == INTEGER_CST
4937 : 0 : && tree_fits_shwi_p (bitpos)
4938 : 0 : && tree_fits_shwi_p (DECL_SIZE (field)))
4939 : : {
4940 : 0 : HOST_WIDE_INT bit = tree_to_shwi (bitpos);
4941 : 0 : HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field));
4942 : 0 : HOST_WIDE_INT shift;
4943 : 0 : if (bit >= istart && bit + sz <= istart + isize)
4944 : : {
4945 : 0 : fldval = fold_convert (utype, value);
4946 : 0 : mask = build_int_cst_type (utype, -1);
4947 : 0 : mask = fold_build2 (LSHIFT_EXPR, utype, mask,
4948 : : size_int (TYPE_PRECISION (utype) - sz));
4949 : 0 : mask = fold_build2 (RSHIFT_EXPR, utype, mask,
4950 : : size_int (TYPE_PRECISION (utype) - sz));
4951 : 0 : fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask);
4952 : 0 : shift = bit - istart;
4953 : 0 : if (BYTES_BIG_ENDIAN)
4954 : : shift = TYPE_PRECISION (utype) - shift - sz;
4955 : 0 : fldval = fold_build2 (LSHIFT_EXPR, utype, fldval,
4956 : : size_int (shift));
4957 : 0 : retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval);
4958 : 0 : fld_seen = true;
4959 : : }
4960 : : }
4961 : : }
4962 : 0 : if (fld_seen)
4963 : 0 : return fold_convert (TREE_TYPE (t), retval);
4964 : 0 : gcc_unreachable ();
4965 : : return error_mark_node;
4966 : : }
4967 : :
4968 : : /* Helper for cxx_eval_bit_cast.
4969 : : Check [bit.cast]/3 rules, bit_cast is constexpr only if the To and From
4970 : : types and types of all subobjects have is_union_v<T>, is_pointer_v<T>,
4971 : : is_member_pointer_v<T>, is_volatile_v<T> false and has no non-static
4972 : : data members of reference type. */
4973 : :
4974 : : static bool
4975 : 4780 : check_bit_cast_type (const constexpr_ctx *ctx, location_t loc, tree type,
4976 : : tree orig_type)
4977 : : {
4978 : 5264 : if (TREE_CODE (type) == UNION_TYPE)
4979 : : {
4980 : 63 : if (!ctx->quiet)
4981 : : {
4982 : 21 : if (type == orig_type)
4983 : 6 : error_at (loc, "%qs is not a constant expression because %qT is "
4984 : : "a union type", "__builtin_bit_cast", type);
4985 : : else
4986 : 15 : error_at (loc, "%qs is not a constant expression because %qT "
4987 : : "contains a union type", "__builtin_bit_cast",
4988 : : orig_type);
4989 : : }
4990 : 63 : return true;
4991 : : }
4992 : : if (TREE_CODE (type) == POINTER_TYPE)
4993 : : {
4994 : 58 : if (!ctx->quiet)
4995 : : {
4996 : 18 : if (type == orig_type)
4997 : 6 : error_at (loc, "%qs is not a constant expression because %qT is "
4998 : : "a pointer type", "__builtin_bit_cast", type);
4999 : : else
5000 : 12 : error_at (loc, "%qs is not a constant expression because %qT "
5001 : : "contains a pointer type", "__builtin_bit_cast",
5002 : : orig_type);
5003 : : }
5004 : 58 : return true;
5005 : : }
5006 : : if (TREE_CODE (type) == REFERENCE_TYPE)
5007 : : {
5008 : 0 : if (!ctx->quiet)
5009 : : {
5010 : 0 : if (type == orig_type)
5011 : 0 : error_at (loc, "%qs is not a constant expression because %qT is "
5012 : : "a reference type", "__builtin_bit_cast", type);
5013 : : else
5014 : 0 : error_at (loc, "%qs is not a constant expression because %qT "
5015 : : "contains a reference type", "__builtin_bit_cast",
5016 : : orig_type);
5017 : : }
5018 : 0 : return true;
5019 : : }
5020 : 1343 : if (TYPE_PTRMEM_P (type))
5021 : : {
5022 : 36 : if (!ctx->quiet)
5023 : : {
5024 : 12 : if (type == orig_type)
5025 : 12 : error_at (loc, "%qs is not a constant expression because %qT is "
5026 : : "a pointer to member type", "__builtin_bit_cast",
5027 : : type);
5028 : : else
5029 : 0 : error_at (loc, "%qs is not a constant expression because %qT "
5030 : : "contains a pointer to member type",
5031 : : "__builtin_bit_cast", orig_type);
5032 : : }
5033 : 36 : return true;
5034 : : }
5035 : 5107 : if (TYPE_VOLATILE (type))
5036 : : {
5037 : 0 : if (!ctx->quiet)
5038 : : {
5039 : 0 : if (type == orig_type)
5040 : 0 : error_at (loc, "%qs is not a constant expression because %qT is "
5041 : : "volatile", "__builtin_bit_cast", type);
5042 : : else
5043 : 0 : error_at (loc, "%qs is not a constant expression because %qT "
5044 : : "contains a volatile subobject",
5045 : : "__builtin_bit_cast", orig_type);
5046 : : }
5047 : 0 : return true;
5048 : : }
5049 : 5107 : if (TREE_CODE (type) == RECORD_TYPE)
5050 : 26412 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5051 : 25177 : if (TREE_CODE (field) == FIELD_DECL
5052 : 25177 : && check_bit_cast_type (ctx, loc, TREE_TYPE (field), orig_type))
5053 : : return true;
5054 : 5017 : if (TREE_CODE (type) == ARRAY_TYPE)
5055 : 484 : return check_bit_cast_type (ctx, loc, TREE_TYPE (type), orig_type);
5056 : : return false;
5057 : : }
5058 : :
5059 : : /* Helper function for cxx_eval_bit_cast. For unsigned char or
5060 : : std::byte members of CONSTRUCTOR (recursively) if they contain
5061 : : some indeterminate bits (as set in MASK), remove the ctor elts,
5062 : : mark the CONSTRUCTOR as CONSTRUCTOR_NO_CLEARING and clear the
5063 : : bits in MASK. */
5064 : :
5065 : : static void
5066 : 603 : clear_uchar_or_std_byte_in_mask (location_t loc, tree t, unsigned char *mask)
5067 : : {
5068 : 603 : if (TREE_CODE (t) != CONSTRUCTOR)
5069 : : return;
5070 : :
5071 : : unsigned i, j = 0;
5072 : : tree index, value;
5073 : 2090 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), i, index, value)
5074 : : {
5075 : 1487 : tree type = TREE_TYPE (value);
5076 : 1487 : if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
5077 : 2535 : && DECL_BIT_FIELD_TYPE (index) != NULL_TREE)
5078 : : {
5079 : 270 : if (is_byte_access_type_not_plain_char (DECL_BIT_FIELD_TYPE (index)))
5080 : : {
5081 : 135 : HOST_WIDE_INT fldsz = TYPE_PRECISION (TREE_TYPE (index));
5082 : 135 : gcc_assert (fldsz != 0);
5083 : 135 : HOST_WIDE_INT pos = int_byte_position (index);
5084 : 135 : HOST_WIDE_INT bpos
5085 : 135 : = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (index));
5086 : 135 : bpos %= BITS_PER_UNIT;
5087 : 135 : HOST_WIDE_INT end
5088 : 135 : = ROUND_UP (bpos + fldsz, BITS_PER_UNIT) / BITS_PER_UNIT;
5089 : 135 : gcc_assert (end == 1 || end == 2);
5090 : 135 : unsigned char *p = mask + pos;
5091 : 135 : unsigned char mask_save[2];
5092 : 135 : mask_save[0] = mask[pos];
5093 : 135 : mask_save[1] = end == 2 ? mask[pos + 1] : 0;
5094 : 135 : if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
5095 : : sorry_at (loc, "PDP11 bit-field handling unsupported"
5096 : : " in %qs", "__builtin_bit_cast");
5097 : 135 : else if (BYTES_BIG_ENDIAN)
5098 : : {
5099 : : /* Big endian. */
5100 : : if (bpos + fldsz <= BITS_PER_UNIT)
5101 : : *p &= ~(((1 << fldsz) - 1)
5102 : : << (BITS_PER_UNIT - bpos - fldsz));
5103 : : else
5104 : : {
5105 : : gcc_assert (bpos);
5106 : : *p &= ~(((1U << BITS_PER_UNIT) - 1) >> bpos);
5107 : : p++;
5108 : : fldsz -= BITS_PER_UNIT - bpos;
5109 : : gcc_assert (fldsz && fldsz < BITS_PER_UNIT);
5110 : : *p &= ((1U << BITS_PER_UNIT) - 1) >> fldsz;
5111 : : }
5112 : : }
5113 : : else
5114 : : {
5115 : : /* Little endian. */
5116 : 135 : if (bpos + fldsz <= BITS_PER_UNIT)
5117 : 135 : *p &= ~(((1 << fldsz) - 1) << bpos);
5118 : : else
5119 : : {
5120 : 0 : gcc_assert (bpos);
5121 : 0 : *p &= ~(((1 << BITS_PER_UNIT) - 1) << bpos);
5122 : 0 : p++;
5123 : 0 : fldsz -= BITS_PER_UNIT - bpos;
5124 : 0 : gcc_assert (fldsz && fldsz < BITS_PER_UNIT);
5125 : 0 : *p &= ~((1 << fldsz) - 1);
5126 : : }
5127 : : }
5128 : 135 : if (mask_save[0] != mask[pos]
5129 : 99 : || (end == 2 && mask_save[1] != mask[pos + 1]))
5130 : : {
5131 : 36 : CONSTRUCTOR_NO_CLEARING (t) = 1;
5132 : 36 : continue;
5133 : : }
5134 : : }
5135 : : }
5136 : 1217 : else if (is_byte_access_type_not_plain_char (type))
5137 : : {
5138 : 228 : HOST_WIDE_INT pos;
5139 : 228 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5140 : 132 : pos = tree_to_shwi (index);
5141 : : else
5142 : 96 : pos = int_byte_position (index);
5143 : 228 : if (mask[pos])
5144 : : {
5145 : 48 : CONSTRUCTOR_NO_CLEARING (t) = 1;
5146 : 48 : mask[pos] = 0;
5147 : 48 : continue;
5148 : : }
5149 : : }
5150 : 1403 : if (TREE_CODE (value) == CONSTRUCTOR)
5151 : : {
5152 : 264 : HOST_WIDE_INT pos;
5153 : 264 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5154 : 144 : pos = tree_to_shwi (index)
5155 : 72 : * tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))));
5156 : : else
5157 : 192 : pos = int_byte_position (index);
5158 : 264 : clear_uchar_or_std_byte_in_mask (loc, value, mask + pos);
5159 : : }
5160 : 1403 : if (i != j)
5161 : : {
5162 : 180 : CONSTRUCTOR_ELT (t, j)->index = index;
5163 : 180 : CONSTRUCTOR_ELT (t, j)->value = value;
5164 : : }
5165 : 1403 : ++j;
5166 : : }
5167 : 1206 : if (CONSTRUCTOR_NELTS (t) != j)
5168 : 84 : vec_safe_truncate (CONSTRUCTOR_ELTS (t), j);
5169 : : }
5170 : :
5171 : : /* Subroutine of cxx_eval_constant_expression.
5172 : : Attempt to evaluate a BIT_CAST_EXPR. */
5173 : :
5174 : : static tree
5175 : 1014 : cxx_eval_bit_cast (const constexpr_ctx *ctx, tree t, bool *non_constant_p,
5176 : : bool *overflow_p)
5177 : : {
5178 : 1014 : if (check_bit_cast_type (ctx, EXPR_LOCATION (t), TREE_TYPE (t),
5179 : 1014 : TREE_TYPE (t))
5180 : 3290 : || check_bit_cast_type (ctx, cp_expr_loc_or_loc (TREE_OPERAND (t, 0),
5181 : 933 : EXPR_LOCATION (t)),
5182 : 933 : TREE_TYPE (TREE_OPERAND (t, 0)),
5183 : 933 : TREE_TYPE (TREE_OPERAND (t, 0))))
5184 : : {
5185 : 157 : *non_constant_p = true;
5186 : 157 : return t;
5187 : : }
5188 : :
5189 : 857 : tree op = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), vc_prvalue,
5190 : : non_constant_p, overflow_p);
5191 : 857 : if (*non_constant_p)
5192 : : return t;
5193 : :
5194 : 770 : location_t loc = EXPR_LOCATION (t);
5195 : 770 : if (BITS_PER_UNIT != 8 || CHAR_BIT != 8)
5196 : : {
5197 : : if (!ctx->quiet)
5198 : : sorry_at (loc, "%qs cannot be constant evaluated on the target",
5199 : : "__builtin_bit_cast");
5200 : : *non_constant_p = true;
5201 : : return t;
5202 : : }
5203 : :
5204 : 770 : if (!tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (t))))
5205 : : {
5206 : 0 : if (!ctx->quiet)
5207 : 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
5208 : : "type is too large", "__builtin_bit_cast");
5209 : 0 : *non_constant_p = true;
5210 : 0 : return t;
5211 : : }
5212 : :
5213 : 770 : HOST_WIDE_INT len = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (t)));
5214 : 770 : if (len < 0 || (int) len != len)
5215 : : {
5216 : 0 : if (!ctx->quiet)
5217 : 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
5218 : : "type is too large", "__builtin_bit_cast");
5219 : 0 : *non_constant_p = true;
5220 : 0 : return t;
5221 : : }
5222 : :
5223 : 770 : unsigned char buf[64];
5224 : 770 : unsigned char *ptr, *mask;
5225 : 770 : size_t alen = (size_t) len * 2;
5226 : 770 : if (alen <= sizeof (buf))
5227 : : ptr = buf;
5228 : : else
5229 : 3 : ptr = XNEWVEC (unsigned char, alen);
5230 : 770 : mask = ptr + (size_t) len;
5231 : : /* At the beginning consider everything indeterminate. */
5232 : 770 : memset (mask, ~0, (size_t) len);
5233 : :
5234 : 770 : if (native_encode_initializer (op, ptr, len, 0, mask) != len)
5235 : : {
5236 : 0 : if (!ctx->quiet)
5237 : 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
5238 : : "argument cannot be encoded", "__builtin_bit_cast");
5239 : 0 : *non_constant_p = true;
5240 : 0 : if (ptr != buf)
5241 : 0 : XDELETE (ptr);
5242 : 0 : return t;
5243 : : }
5244 : :
5245 : 770 : tree r = NULL_TREE;
5246 : 770 : if (can_native_interpret_type_p (TREE_TYPE (t)))
5247 : : {
5248 : 431 : r = native_interpret_expr (TREE_TYPE (t), ptr, len);
5249 : 431 : if (is_byte_access_type_not_plain_char (TREE_TYPE (t)))
5250 : : {
5251 : 46 : gcc_assert (len == 1);
5252 : 46 : if (mask[0])
5253 : : {
5254 : 24 : memset (mask, 0, len);
5255 : 24 : r = build_constructor (TREE_TYPE (r), NULL);
5256 : 24 : CONSTRUCTOR_NO_CLEARING (r) = 1;
5257 : : }
5258 : : }
5259 : : }
5260 : 339 : else if (TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE)
5261 : : {
5262 : 339 : r = native_interpret_aggregate (TREE_TYPE (t), ptr, 0, len);
5263 : 339 : if (r != NULL_TREE)
5264 : : {
5265 : 339 : clear_type_padding_in_mask (TREE_TYPE (t), mask);
5266 : 339 : clear_uchar_or_std_byte_in_mask (loc, r, mask);
5267 : 339 : if (CHECKING_P)
5268 : : {
5269 : 339 : tree e = cxx_eval_bare_aggregate (ctx, r, vc_prvalue,
5270 : : non_constant_p, overflow_p);
5271 : 339 : gcc_checking_assert (e == r);
5272 : : r = e;
5273 : : }
5274 : : }
5275 : : }
5276 : :
5277 : 770 : if (r != NULL_TREE)
5278 : : {
5279 : 6169 : for (int i = 0; i < len; i++)
5280 : 5489 : if (mask[i])
5281 : : {
5282 : 90 : if (!ctx->quiet)
5283 : 30 : error_at (loc, "%qs accessing uninitialized byte at offset %d",
5284 : : "__builtin_bit_cast", i);
5285 : 90 : *non_constant_p = true;
5286 : 90 : r = t;
5287 : 90 : break;
5288 : : }
5289 : 770 : if (ptr != buf)
5290 : 3 : XDELETE (ptr);
5291 : 770 : return r;
5292 : : }
5293 : :
5294 : 0 : if (!ctx->quiet)
5295 : 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
5296 : : "argument cannot be interpreted", "__builtin_bit_cast");
5297 : 0 : *non_constant_p = true;
5298 : 0 : if (ptr != buf)
5299 : 0 : XDELETE (ptr);
5300 : : return t;
5301 : : }
5302 : :
5303 : : /* Subroutine of cxx_eval_constant_expression.
5304 : : Evaluate a short-circuited logical expression T in the context
5305 : : of a given constexpr CALL. BAILOUT_VALUE is the value for
5306 : : early return. CONTINUE_VALUE is used here purely for
5307 : : sanity check purposes. */
5308 : :
5309 : : static tree
5310 : 6745634 : cxx_eval_logical_expression (const constexpr_ctx *ctx, tree t,
5311 : : tree bailout_value, tree continue_value,
5312 : : bool *non_constant_p, bool *overflow_p)
5313 : : {
5314 : 6745634 : tree r;
5315 : 6745634 : tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
5316 : : vc_prvalue, non_constant_p,
5317 : : overflow_p);
5318 : 6745634 : VERIFY_CONSTANT (lhs);
5319 : 3997812 : if (tree_int_cst_equal (lhs, bailout_value))
5320 : : return lhs;
5321 : 3425786 : gcc_assert (tree_int_cst_equal (lhs, continue_value));
5322 : 3425786 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
5323 : : vc_prvalue, non_constant_p,
5324 : : overflow_p);
5325 : 3425786 : VERIFY_CONSTANT (r);
5326 : : return r;
5327 : : }
5328 : :
5329 : : /* REF is a COMPONENT_REF designating a particular field. V is a vector of
5330 : : CONSTRUCTOR elements to initialize (part of) an object containing that
5331 : : field. Return a pointer to the constructor_elt corresponding to the
5332 : : initialization of the field. */
5333 : :
5334 : : static constructor_elt *
5335 : 0 : base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref)
5336 : : {
5337 : 0 : tree aggr = TREE_OPERAND (ref, 0);
5338 : 0 : tree field = TREE_OPERAND (ref, 1);
5339 : 0 : HOST_WIDE_INT i;
5340 : 0 : constructor_elt *ce;
5341 : :
5342 : 0 : gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
5343 : :
5344 : 0 : if (TREE_CODE (aggr) == COMPONENT_REF)
5345 : : {
5346 : 0 : constructor_elt *base_ce
5347 : 0 : = base_field_constructor_elt (v, aggr);
5348 : 0 : v = CONSTRUCTOR_ELTS (base_ce->value);
5349 : : }
5350 : :
5351 : 0 : for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
5352 : 0 : if (ce->index == field)
5353 : 0 : return ce;
5354 : :
5355 : 0 : gcc_unreachable ();
5356 : : return NULL;
5357 : : }
5358 : :
5359 : : /* Some of the expressions fed to the constexpr mechanism are calls to
5360 : : constructors, which have type void. In that case, return the type being
5361 : : initialized by the constructor. */
5362 : :
5363 : : static tree
5364 : 364134877 : initialized_type (tree t)
5365 : : {
5366 : 364847257 : if (TYPE_P (t))
5367 : : return t;
5368 : 364846925 : tree type = TREE_TYPE (t);
5369 : 364846925 : if (TREE_CODE (t) == CALL_EXPR)
5370 : : {
5371 : : /* A constructor call has void type, so we need to look deeper. */
5372 : 44338094 : tree fn = get_function_named_in_call (t);
5373 : 44338081 : if (fn && TREE_CODE (fn) == FUNCTION_DECL
5374 : 88631137 : && DECL_CXX_CONSTRUCTOR_P (fn))
5375 : 2226716 : type = DECL_CONTEXT (fn);
5376 : : }
5377 : 320508831 : else if (TREE_CODE (t) == COMPOUND_EXPR)
5378 : 712380 : return initialized_type (TREE_OPERAND (t, 1));
5379 : 319796451 : else if (TREE_CODE (t) == AGGR_INIT_EXPR)
5380 : 485036 : type = TREE_TYPE (AGGR_INIT_EXPR_SLOT (t));
5381 : 364134545 : return cv_unqualified (type);
5382 : : }
5383 : :
5384 : : /* We're about to initialize element INDEX of an array or class from VALUE.
5385 : : Set up NEW_CTX appropriately by adjusting .object to refer to the
5386 : : subobject and creating a new CONSTRUCTOR if the element is itself
5387 : : a class or array. */
5388 : :
5389 : : static void
5390 : 1614634 : init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx,
5391 : : tree index, tree &value)
5392 : : {
5393 : 1614634 : new_ctx = *ctx;
5394 : :
5395 : 1614634 : if (index && TREE_CODE (index) != INTEGER_CST
5396 : 1494080 : && TREE_CODE (index) != FIELD_DECL
5397 : 3 : && TREE_CODE (index) != RANGE_EXPR)
5398 : : /* This won't have an element in the new CONSTRUCTOR. */
5399 : : return;
5400 : :
5401 : 1614634 : tree type = initialized_type (value);
5402 : 1614634 : if (!AGGREGATE_TYPE_P (type) && !VECTOR_TYPE_P (type))
5403 : : /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
5404 : : return;
5405 : 736320 : if (VECTOR_TYPE_P (type)
5406 : 1097 : && VECTOR_TYPE_P (TREE_TYPE (ctx->ctor))
5407 : 736356 : && index == NULL_TREE)
5408 : : /* A vector inside of a vector CONSTRUCTOR, e.g. when a larger
5409 : : vector is constructed from smaller vectors, doesn't get its own
5410 : : CONSTRUCTOR either. */
5411 : : return;
5412 : :
5413 : : /* The sub-aggregate initializer might contain a placeholder;
5414 : : update object to refer to the subobject and ctor to refer to
5415 : : the (newly created) sub-initializer. */
5416 : 736284 : if (ctx->object)
5417 : : {
5418 : 571125 : if (index == NULL_TREE || TREE_CODE (index) == RANGE_EXPR)
5419 : : /* There's no well-defined subobject for this index. */
5420 : 3 : new_ctx.object = NULL_TREE;
5421 : : else
5422 : 571122 : new_ctx.object = build_ctor_subob_ref (index, type, ctx->object);
5423 : : }
5424 : :
5425 : 736284 : if (is_empty_class (type))
5426 : : /* Leave ctor null for an empty subobject, they aren't represented in the
5427 : : result of evaluation. */
5428 : 695576 : new_ctx.ctor = NULL_TREE;
5429 : : else
5430 : : {
5431 : 40708 : tree elt = build_constructor (type, NULL);
5432 : 40708 : CONSTRUCTOR_NO_CLEARING (elt) = true;
5433 : 40708 : new_ctx.ctor = elt;
5434 : : }
5435 : :
5436 : 736284 : if (TREE_CODE (value) == TARGET_EXPR)
5437 : : /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
5438 : 30337 : value = TARGET_EXPR_INITIAL (value);
5439 : : }
5440 : :
5441 : : /* We're about to process an initializer for a class or array TYPE. Make
5442 : : sure that CTX is set up appropriately. */
5443 : :
5444 : : static void
5445 : 1234262 : verify_ctor_sanity (const constexpr_ctx *ctx, tree type)
5446 : : {
5447 : : /* We don't bother building a ctor for an empty base subobject. */
5448 : 1234262 : if (is_empty_class (type))
5449 : : return;
5450 : :
5451 : : /* We're in the middle of an initializer that might involve placeholders;
5452 : : our caller should have created a CONSTRUCTOR for us to put the
5453 : : initializer into. We will either return that constructor or T. */
5454 : 540835 : gcc_assert (ctx->ctor);
5455 : 540835 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
5456 : : (type, TREE_TYPE (ctx->ctor)));
5457 : : /* We used to check that ctx->ctor was empty, but that isn't the case when
5458 : : the object is zero-initialized before calling the constructor. */
5459 : 540835 : if (ctx->object)
5460 : : {
5461 : 407230 : tree otype = TREE_TYPE (ctx->object);
5462 : 407230 : gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, otype)
5463 : : /* Handle flexible array members. */
5464 : : || (TREE_CODE (otype) == ARRAY_TYPE
5465 : : && TYPE_DOMAIN (otype) == NULL_TREE
5466 : : && TREE_CODE (type) == ARRAY_TYPE
5467 : : && (same_type_ignoring_top_level_qualifiers_p
5468 : : (TREE_TYPE (type), TREE_TYPE (otype)))));
5469 : : }
5470 : 540835 : gcc_assert (!ctx->object || !DECL_P (ctx->object)
5471 : : || ctx->global->get_value (ctx->object) == ctx->ctor);
5472 : : }
5473 : :
5474 : : /* Subroutine of cxx_eval_constant_expression.
5475 : : The expression tree T denotes a C-style array or a C-style
5476 : : aggregate. Reduce it to a constant expression. */
5477 : :
5478 : : static tree
5479 : 1233674 : cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t,
5480 : : value_cat lval,
5481 : : bool *non_constant_p, bool *overflow_p)
5482 : : {
5483 : 1233674 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
5484 : 1233674 : bool changed = false;
5485 : 1233674 : gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t));
5486 : 1233674 : tree type = TREE_TYPE (t);
5487 : :
5488 : 1233674 : constexpr_ctx new_ctx;
5489 : 1233674 : if (TYPE_PTRMEMFUNC_P (type) || VECTOR_TYPE_P (type))
5490 : : {
5491 : : /* We don't really need the ctx->ctor business for a PMF or
5492 : : vector, but it's simpler to use the same code. */
5493 : 24780 : new_ctx = *ctx;
5494 : 24780 : new_ctx.ctor = build_constructor (type, NULL);
5495 : 24780 : new_ctx.object = NULL_TREE;
5496 : 24780 : ctx = &new_ctx;
5497 : 1233674 : };
5498 : 1233674 : verify_ctor_sanity (ctx, type);
5499 : 1233674 : vec<constructor_elt, va_gc> **p = nullptr;
5500 : 1233674 : if (ctx->ctor)
5501 : : {
5502 : 1233666 : p = &CONSTRUCTOR_ELTS (ctx->ctor);
5503 : 2467229 : vec_alloc (*p, vec_safe_length (v));
5504 : 1233666 : if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (t))
5505 : 16049 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor) = 1;
5506 : : }
5507 : :
5508 : 1233674 : unsigned i;
5509 : 1233674 : tree index, value;
5510 : 1233674 : bool constant_p = true;
5511 : 1233674 : bool side_effects_p = false;
5512 : 2545198 : FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value)
5513 : : {
5514 : 1608605 : tree orig_value = value;
5515 : 1608605 : init_subob_ctx (ctx, new_ctx, index, value);
5516 : : /* Like in cxx_eval_store_expression, omit entries for empty fields. */
5517 : 1608605 : bool no_slot = new_ctx.ctor == NULL_TREE;
5518 : 1608605 : int pos_hint = -1;
5519 : 1608605 : if (new_ctx.ctor != ctx->ctor && !no_slot)
5520 : : {
5521 : : /* If we built a new CONSTRUCTOR, attach it now so that other
5522 : : initializers can refer to it. */
5523 : 34846 : constructor_elt *cep = get_or_insert_ctor_field (ctx->ctor, index);
5524 : 34846 : cep->value = new_ctx.ctor;
5525 : 34846 : pos_hint = cep - (*p)->begin();
5526 : 34846 : }
5527 : 1573759 : else if (TREE_CODE (type) == UNION_TYPE)
5528 : : /* Otherwise if we're constructing a non-aggregate union member, set
5529 : : the active union member now so that we can later detect and diagnose
5530 : : if its initializer attempts to activate another member. */
5531 : 963 : get_or_insert_ctor_field (ctx->ctor, index);
5532 : 1608605 : tree elt = cxx_eval_constant_expression (&new_ctx, value,
5533 : : lval,
5534 : : non_constant_p, overflow_p);
5535 : : /* Don't VERIFY_CONSTANT here. */
5536 : 1608605 : if (ctx->quiet && *non_constant_p)
5537 : : break;
5538 : 1311524 : if (elt != orig_value)
5539 : 133945 : changed = true;
5540 : :
5541 : 1311524 : if (!TREE_CONSTANT (elt))
5542 : 439128 : constant_p = false;
5543 : 1311524 : if (TREE_SIDE_EFFECTS (elt))
5544 : 111 : side_effects_p = true;
5545 : 1311524 : if (index && TREE_CODE (index) == COMPONENT_REF)
5546 : : {
5547 : : /* This is an initialization of a vfield inside a base
5548 : : subaggregate that we already initialized; push this
5549 : : initialization into the previous initialization. */
5550 : 0 : constructor_elt *inner = base_field_constructor_elt (*p, index);
5551 : 0 : inner->value = elt;
5552 : 0 : changed = true;
5553 : 0 : }
5554 : 1311524 : else if (no_slot)
5555 : : /* This is an initializer for an empty field; now that we've
5556 : : checked that it's constant, we can ignore it. */
5557 : : changed = true;
5558 : 616241 : else if (index
5559 : 616113 : && (TREE_CODE (index) == NOP_EXPR
5560 : 616113 : || TREE_CODE (index) == POINTER_PLUS_EXPR))
5561 : : {
5562 : : /* Old representation of empty bases. FIXME remove. */
5563 : 0 : gcc_checking_assert (false);
5564 : : gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index))));
5565 : : changed = true;
5566 : : }
5567 : : else
5568 : : {
5569 : 616241 : if (TREE_CODE (type) == UNION_TYPE
5570 : 616241 : && (*p)->last().index != index)
5571 : : /* The initializer erroneously changed the active union member that
5572 : : we're initializing. */
5573 : 8 : gcc_assert (*non_constant_p);
5574 : : else
5575 : : {
5576 : : /* The initializer might have mutated the underlying CONSTRUCTOR,
5577 : : so recompute the location of the target constructer_elt. */
5578 : 616233 : constructor_elt *cep
5579 : 616233 : = get_or_insert_ctor_field (ctx->ctor, index, pos_hint);
5580 : 616233 : cep->value = elt;
5581 : : }
5582 : :
5583 : : /* Adding or replacing an element might change the ctor's flags. */
5584 : 616241 : TREE_CONSTANT (ctx->ctor) = constant_p;
5585 : 616241 : TREE_SIDE_EFFECTS (ctx->ctor) = side_effects_p;
5586 : : }
5587 : : }
5588 : 1233674 : if (*non_constant_p)
5589 : : return t;
5590 : 936560 : if (!changed)
5591 : : {
5592 : 154829 : if (VECTOR_TYPE_P (type))
5593 : 13905 : t = fold (t);
5594 : 154829 : return t;
5595 : : }
5596 : 781731 : t = ctx->ctor;
5597 : 781731 : if (!t)
5598 : 8 : t = build_constructor (type, NULL);
5599 : : /* We're done building this CONSTRUCTOR, so now we can interpret an
5600 : : element without an explicit initializer as value-initialized. */
5601 : 781731 : CONSTRUCTOR_NO_CLEARING (t) = false;
5602 : 781731 : TREE_CONSTANT (t) = constant_p;
5603 : 781731 : TREE_SIDE_EFFECTS (t) = side_effects_p;
5604 : 781731 : if (VECTOR_TYPE_P (type))
5605 : 1070 : t = fold (t);
5606 : : return t;
5607 : : }
5608 : :
5609 : : /* Subroutine of cxx_eval_constant_expression.
5610 : : The expression tree T is a VEC_INIT_EXPR which denotes the desired
5611 : : initialization of a non-static data member of array type. Reduce it to a
5612 : : CONSTRUCTOR.
5613 : :
5614 : : Note that apart from value-initialization (when VALUE_INIT is true),
5615 : : this is only intended to support value-initialization and the
5616 : : initializations done by defaulted constructors for classes with
5617 : : non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
5618 : : will either be NULL_TREE for the default constructor, or a COMPONENT_REF
5619 : : for the copy/move constructor. */
5620 : :
5621 : : static tree
5622 : 588 : cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init,
5623 : : bool value_init, value_cat lval,
5624 : : bool *non_constant_p, bool *overflow_p)
5625 : : {
5626 : 588 : tree elttype = TREE_TYPE (atype);
5627 : 588 : verify_ctor_sanity (ctx, atype);
5628 : 588 : vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
5629 : 588 : bool pre_init = false;
5630 : 588 : unsigned HOST_WIDE_INT i;
5631 : 588 : tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
5632 : :
5633 : 588 : if (init && TREE_CODE (init) == CONSTRUCTOR)
5634 : 0 : return cxx_eval_bare_aggregate (ctx, init, lval,
5635 : 0 : non_constant_p, overflow_p);
5636 : :
5637 : : /* For the default constructor, build up a call to the default
5638 : : constructor of the element type. We only need to handle class types
5639 : : here, as for a constructor to be constexpr, all members must be
5640 : : initialized, which for a defaulted default constructor means they must
5641 : : be of a class type with a constexpr default constructor. */
5642 : 588 : if (TREE_CODE (elttype) == ARRAY_TYPE)
5643 : : /* We only do this at the lowest level. */;
5644 : 545 : else if (value_init)
5645 : : {
5646 : 142 : init = build_value_init (elttype, complain);
5647 : 142 : pre_init = true;
5648 : : }
5649 : 403 : else if (!init)
5650 : : {
5651 : 320 : releasing_vec argvec;
5652 : 320 : init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
5653 : : &argvec, elttype, LOOKUP_NORMAL,
5654 : : complain);
5655 : 320 : init = build_aggr_init_expr (elttype, init);
5656 : 320 : pre_init = true;
5657 : 320 : }
5658 : :
5659 : 588 : bool zeroed_out = false;
5660 : 588 : if (!CONSTRUCTOR_NO_CLEARING (ctx->ctor))
5661 : : {
5662 : : /* We're initializing an array object that had been zero-initialized
5663 : : earlier. Truncate ctx->ctor, and propagate its zeroed state by
5664 : : clearing CONSTRUCTOR_NO_CLEARING on each of the aggregate element
5665 : : initializers we append to it. */
5666 : 156 : gcc_checking_assert (initializer_zerop (ctx->ctor));
5667 : 156 : zeroed_out = true;
5668 : 156 : vec_safe_truncate (*p, 0);
5669 : : }
5670 : :
5671 : 588 : tree nelts = get_array_or_vector_nelts (ctx, atype, non_constant_p,
5672 : : overflow_p);
5673 : 588 : unsigned HOST_WIDE_INT max = tree_to_uhwi (nelts);
5674 : 6235 : for (i = 0; i < max; ++i)
5675 : : {
5676 : 6029 : tree idx = build_int_cst (size_type_node, i);
5677 : 6029 : tree eltinit;
5678 : 6029 : bool reuse = false;
5679 : 6029 : constexpr_ctx new_ctx;
5680 : 6361 : init_subob_ctx (ctx, new_ctx, idx, pre_init ? init : elttype);
5681 : 6029 : bool no_slot = new_ctx.ctor == NULL_TREE;
5682 : 6029 : if (new_ctx.ctor != ctx->ctor && !no_slot)
5683 : : {
5684 : 5862 : if (zeroed_out)
5685 : 5445 : CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = false;
5686 : 5862 : CONSTRUCTOR_APPEND_ELT (*p, idx, new_ctx.ctor);
5687 : : }
5688 : 6029 : if (TREE_CODE (elttype) == ARRAY_TYPE)
5689 : : {
5690 : : /* A multidimensional array; recurse. */
5691 : 175 : if (value_init || init == NULL_TREE)
5692 : : {
5693 : 167 : eltinit = NULL_TREE;
5694 : 167 : reuse = i == 0;
5695 : : }
5696 : : else
5697 : 8 : eltinit = cp_build_array_ref (input_location, init, idx, complain);
5698 : 175 : eltinit = cxx_eval_vec_init_1 (&new_ctx, elttype, eltinit, value_init,
5699 : : lval,
5700 : : non_constant_p, overflow_p);
5701 : : }
5702 : 5854 : else if (pre_init)
5703 : : {
5704 : : /* Initializing an element using value or default initialization
5705 : : we just pre-built above. */
5706 : 5697 : if (init == void_node)
5707 : : /* Trivial default-init, don't do anything to the CONSTRUCTOR. */
5708 : 3 : return ctx->ctor;
5709 : 5694 : eltinit = init;
5710 : 5694 : if (CLASS_TYPE_P (elttype) && new_ctx.object)
5711 : : /* Clarify what object is being initialized (118285). */
5712 : 5570 : eltinit = build2 (INIT_EXPR, elttype, new_ctx.object, eltinit);
5713 : 5694 : eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval,
5714 : : non_constant_p, overflow_p);
5715 : 5694 : reuse = i == 0;
5716 : : }
5717 : : else
5718 : : {
5719 : : /* Copying an element. */
5720 : 157 : eltinit = cp_build_array_ref (input_location, init, idx, complain);
5721 : 157 : if (!lvalue_p (init))
5722 : 130 : eltinit = move (eltinit);
5723 : 157 : eltinit = (perform_implicit_conversion_flags
5724 : 157 : (elttype, eltinit, complain,
5725 : : LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING));
5726 : 157 : if (CLASS_TYPE_P (elttype) && new_ctx.object)
5727 : : /* Clarify what object is being initialized (118285). */
5728 : 140 : eltinit = build2 (INIT_EXPR, elttype, new_ctx.object, eltinit);
5729 : 157 : eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval,
5730 : : non_constant_p, overflow_p);
5731 : : }
5732 : 6026 : if (*non_constant_p)
5733 : : break;
5734 : 5930 : if (no_slot)
5735 : : {
5736 : : /* This is an initializer for an empty subobject; now that we've
5737 : : checked that it's constant, we can ignore it. */
5738 : 18 : gcc_checking_assert (i == 0);
5739 : : break;
5740 : : }
5741 : 5912 : else if (new_ctx.ctor != ctx->ctor)
5742 : : {
5743 : : /* We appended this element above; update the value. */
5744 : 5794 : gcc_assert ((*p)->last().index == idx);
5745 : 5794 : (*p)->last().value = eltinit;
5746 : : }
5747 : : else
5748 : 118 : CONSTRUCTOR_APPEND_ELT (*p, idx, eltinit);
5749 : : /* Reuse the result of cxx_eval_constant_expression call
5750 : : from the first iteration to all others if it is a constant
5751 : : initializer that doesn't require relocations. */
5752 : 5912 : if (reuse
5753 : 5912 : && max > 1
5754 : 5912 : && (eltinit == NULL_TREE
5755 : 418 : || (initializer_constant_valid_p (eltinit, TREE_TYPE (eltinit))
5756 : 418 : == null_pointer_node)))
5757 : : {
5758 : 265 : if (new_ctx.ctor != ctx->ctor)
5759 : 153 : eltinit = new_ctx.ctor;
5760 : 265 : tree range = build2 (RANGE_EXPR, size_type_node,
5761 : : build_int_cst (size_type_node, 1),
5762 : 265 : build_int_cst (size_type_node, max - 1));
5763 : 265 : CONSTRUCTOR_APPEND_ELT (*p, range, unshare_constructor (eltinit));
5764 : 265 : break;
5765 : : }
5766 : 5647 : else if (i == 0)
5767 : 203 : vec_safe_reserve (*p, max);
5768 : : }
5769 : :
5770 : 585 : if (!*non_constant_p)
5771 : : {
5772 : 489 : init = ctx->ctor;
5773 : 489 : CONSTRUCTOR_NO_CLEARING (init) = false;
5774 : : }
5775 : 585 : return init;
5776 : : }
5777 : :
5778 : : static tree
5779 : 476 : cxx_eval_vec_init (const constexpr_ctx *ctx, tree t,
5780 : : value_cat lval,
5781 : : bool *non_constant_p, bool *overflow_p)
5782 : : {
5783 : 476 : tree atype = TREE_TYPE (t);
5784 : 476 : tree init = VEC_INIT_EXPR_INIT (t);
5785 : 476 : bool value_init = VEC_INIT_EXPR_VALUE_INIT (t);
5786 : 476 : if (!init || !BRACE_ENCLOSED_INITIALIZER_P (init))
5787 : : ;
5788 : 72 : else if (CONSTRUCTOR_NELTS (init) == 0
5789 : 72 : && !CP_AGGREGATE_TYPE_P (strip_array_types (atype)))
5790 : : {
5791 : : /* Handle {} as value-init. */
5792 : : init = NULL_TREE;
5793 : : value_init = true;
5794 : : }
5795 : : else
5796 : : {
5797 : : /* This is a more complicated case, like needing to loop over trailing
5798 : : elements; call build_vec_init and evaluate the result. */
5799 : 63 : tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
5800 : 63 : constexpr_ctx new_ctx = *ctx;
5801 : 63 : if (!ctx->object)
5802 : : {
5803 : : /* We want to have an initialization target for an VEC_INIT_EXPR.
5804 : : If we don't already have one in CTX, use the VEC_INIT_EXPR_SLOT. */
5805 : 51 : new_ctx.object = VEC_INIT_EXPR_SLOT (t);
5806 : 51 : tree ctor = new_ctx.ctor = build_constructor (atype, NULL);
5807 : 51 : CONSTRUCTOR_NO_CLEARING (ctor) = true;
5808 : 51 : ctx->global->put_value (new_ctx.object, ctor);
5809 : 51 : ctx = &new_ctx;
5810 : : }
5811 : 63 : init = expand_vec_init_expr (ctx->object, t, complain);
5812 : 63 : return cxx_eval_constant_expression (ctx, init, lval, non_constant_p,
5813 : : overflow_p);
5814 : : }
5815 : 413 : tree r = cxx_eval_vec_init_1 (ctx, atype, init, value_init,
5816 : : lval, non_constant_p, overflow_p);
5817 : 413 : if (*non_constant_p)
5818 : : return t;
5819 : : else
5820 : 336 : return r;
5821 : : }
5822 : :
5823 : : /* Like same_type_ignoring_top_level_qualifiers_p, but also handle the case
5824 : : where the desired type is an array of unknown bounds because the variable
5825 : : has had its bounds deduced since the wrapping expression was created. */
5826 : :
5827 : : static bool
5828 : 129961579 : same_type_ignoring_tlq_and_bounds_p (tree type1, tree type2)
5829 : : {
5830 : 129961579 : while (TREE_CODE (type1) == ARRAY_TYPE
5831 : 13295 : && TREE_CODE (type2) == ARRAY_TYPE
5832 : 129961583 : && (!TYPE_DOMAIN (type1) || !TYPE_DOMAIN (type2)))
5833 : : {
5834 : 2 : type1 = TREE_TYPE (type1);
5835 : 2 : type2 = TREE_TYPE (type2);
5836 : : }
5837 : 129961579 : return same_type_ignoring_top_level_qualifiers_p (type1, type2);
5838 : : }
5839 : :
5840 : : /* Try to determine the currently active union member for an expression
5841 : : with UNION_TYPE. If it can be determined, return the FIELD_DECL,
5842 : : otherwise return NULL_TREE. */
5843 : :
5844 : : static tree
5845 : 69 : cxx_union_active_member (const constexpr_ctx *ctx, tree t)
5846 : : {
5847 : 69 : constexpr_ctx new_ctx = *ctx;
5848 : 69 : new_ctx.quiet = true;
5849 : 69 : bool non_constant_p = false, overflow_p = false;
5850 : 69 : tree ctor = cxx_eval_constant_expression (&new_ctx, t, vc_prvalue,
5851 : : &non_constant_p,
5852 : : &overflow_p);
5853 : 69 : if (TREE_CODE (ctor) == CONSTRUCTOR
5854 : 24 : && CONSTRUCTOR_NELTS (ctor) == 1
5855 : 12 : && CONSTRUCTOR_ELT (ctor, 0)->index
5856 : 81 : && TREE_CODE (CONSTRUCTOR_ELT (ctor, 0)->index) == FIELD_DECL)
5857 : : return CONSTRUCTOR_ELT (ctor, 0)->index;
5858 : : return NULL_TREE;
5859 : : }
5860 : :
5861 : : /* Helper function for cxx_fold_indirect_ref_1, called recursively. */
5862 : :
5863 : : static tree
5864 : 3224490 : cxx_fold_indirect_ref_1 (const constexpr_ctx *ctx, location_t loc, tree type,
5865 : : tree op, unsigned HOST_WIDE_INT off, bool *empty_base)
5866 : : {
5867 : 5132992 : tree optype = TREE_TYPE (op);
5868 : 5132992 : unsigned HOST_WIDE_INT const_nunits;
5869 : 5132992 : if (off == 0 && similar_type_p (optype, type))
5870 : : return op;
5871 : 3222727 : else if (TREE_CODE (optype) == COMPLEX_TYPE
5872 : 3222727 : && similar_type_p (type, TREE_TYPE (optype)))
5873 : : {
5874 : : /* *(foo *)&complexfoo => __real__ complexfoo */
5875 : 0 : if (off == 0)
5876 : 0 : return build1_loc (loc, REALPART_EXPR, type, op);
5877 : : /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
5878 : 0 : else if (tree_to_uhwi (TYPE_SIZE_UNIT (type)) == off)
5879 : 0 : return build1_loc (loc, IMAGPART_EXPR, type, op);
5880 : : }
5881 : : /* ((foo*)&vectorfoo)[x] => BIT_FIELD_REF<vectorfoo,...> */
5882 : 3222727 : else if (VECTOR_TYPE_P (optype)
5883 : 0 : && similar_type_p (type, TREE_TYPE (optype))
5884 : 3222727 : && TYPE_VECTOR_SUBPARTS (optype).is_constant (&const_nunits))
5885 : : {
5886 : 0 : unsigned HOST_WIDE_INT part_width = tree_to_uhwi (TYPE_SIZE_UNIT (type));
5887 : 0 : unsigned HOST_WIDE_INT max_offset = part_width * const_nunits;
5888 : 0 : if (off < max_offset && off % part_width == 0)
5889 : : {
5890 : 0 : tree index = bitsize_int (off * BITS_PER_UNIT);
5891 : 0 : return build3_loc (loc, BIT_FIELD_REF, type, op,
5892 : 0 : TYPE_SIZE (type), index);
5893 : : }
5894 : : }
5895 : : /* ((foo *)&fooarray)[x] => fooarray[x] */
5896 : 3222727 : else if (TREE_CODE (optype) == ARRAY_TYPE
5897 : 1908502 : && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (optype)))
5898 : 5131229 : && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (optype))))
5899 : : {
5900 : 1908502 : tree type_domain = TYPE_DOMAIN (optype);
5901 : 1908502 : tree min_val = size_zero_node;
5902 : 1908502 : if (type_domain && TYPE_MIN_VALUE (type_domain))
5903 : 1908497 : min_val = TYPE_MIN_VALUE (type_domain);
5904 : 1908502 : unsigned HOST_WIDE_INT el_sz
5905 : 1908502 : = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (optype)));
5906 : 1908502 : unsigned HOST_WIDE_INT idx = off / el_sz;
5907 : 1908502 : unsigned HOST_WIDE_INT rem = off % el_sz;
5908 : 1908502 : if (tree_fits_uhwi_p (min_val))
5909 : : {
5910 : 1908502 : tree index = size_int (idx + tree_to_uhwi (min_val));
5911 : 1908502 : op = build4_loc (loc, ARRAY_REF, TREE_TYPE (optype), op, index,
5912 : : NULL_TREE, NULL_TREE);
5913 : 1908502 : return cxx_fold_indirect_ref_1 (ctx, loc, type, op, rem,
5914 : 1908502 : empty_base);
5915 : : }
5916 : : }
5917 : : /* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */
5918 : 1314225 : else if (TREE_CODE (optype) == RECORD_TYPE
5919 : 1314225 : || TREE_CODE (optype) == UNION_TYPE)
5920 : : {
5921 : 1311171 : if (TREE_CODE (optype) == UNION_TYPE)
5922 : : /* For unions prefer the currently active member. */
5923 : 69 : if (tree field = cxx_union_active_member (ctx, op))
5924 : : {
5925 : 12 : unsigned HOST_WIDE_INT el_sz
5926 : 12 : = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field)));
5927 : 12 : if (off < el_sz)
5928 : : {
5929 : 12 : tree cop = build3 (COMPONENT_REF, TREE_TYPE (field),
5930 : : op, field, NULL_TREE);
5931 : 12 : if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop,
5932 : : off, empty_base))
5933 : : return ret;
5934 : : }
5935 : : }
5936 : :
5937 : : /* Handle conversion to "as base" type. */
5938 : 1311165 : if (CLASS_TYPE_P (optype)
5939 : 2622254 : && CLASSTYPE_AS_BASE (optype) == type)
5940 : : return op;
5941 : :
5942 : : /* Handle conversion to an empty base class, which is represented with a
5943 : : NOP_EXPR. Do this before spelunking into the non-empty subobjects,
5944 : : which is likely to be a waste of time (109678). */
5945 : 1310624 : if (is_empty_class (type)
5946 : 1290223 : && CLASS_TYPE_P (optype)
5947 : 2600847 : && lookup_base (optype, type, ba_any, NULL, tf_none, off))
5948 : : {
5949 : 765183 : if (empty_base)
5950 : 765183 : *empty_base = true;
5951 : 765183 : return op;
5952 : : }
5953 : :
5954 : 545441 : for (tree field = TYPE_FIELDS (optype);
5955 : 4845445 : field; field = DECL_CHAIN (field))
5956 : 4843257 : if (TREE_CODE (field) == FIELD_DECL
5957 : 545791 : && TREE_TYPE (field) != error_mark_node
5958 : 5389048 : && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (field))))
5959 : : {
5960 : 545791 : tree pos = byte_position (field);
5961 : 545791 : if (!tree_fits_uhwi_p (pos))
5962 : 0 : continue;
5963 : 545791 : unsigned HOST_WIDE_INT upos = tree_to_uhwi (pos);
5964 : 545791 : unsigned HOST_WIDE_INT el_sz
5965 : 545791 : = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field)));
5966 : 545791 : if (upos <= off && off < upos + el_sz)
5967 : : {
5968 : 545345 : tree cop = build3 (COMPONENT_REF, TREE_TYPE (field),
5969 : : op, field, NULL_TREE);
5970 : 545345 : if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop,
5971 : : off - upos,
5972 : : empty_base))
5973 : : return ret;
5974 : : }
5975 : : }
5976 : : }
5977 : :
5978 : : return NULL_TREE;
5979 : : }
5980 : :
5981 : : /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
5982 : : match. We want to be less strict for simple *& folding; if we have a
5983 : : non-const temporary that we access through a const pointer, that should
5984 : : work. We handle this here rather than change fold_indirect_ref_1
5985 : : because we're dealing with things like ADDR_EXPR of INTEGER_CST which
5986 : : don't really make sense outside of constant expression evaluation. Also
5987 : : we want to allow folding to COMPONENT_REF, which could cause trouble
5988 : : with TBAA in fold_indirect_ref_1. */
5989 : :
5990 : : static tree
5991 : 53261245 : cxx_fold_indirect_ref (const constexpr_ctx *ctx, location_t loc, tree type,
5992 : : tree op0, bool *empty_base /* = NULL*/)
5993 : : {
5994 : 53261245 : tree sub = op0;
5995 : 53261245 : tree subtype;
5996 : :
5997 : : /* STRIP_NOPS, but stop if REINTERPRET_CAST_P. */
5998 : 111442625 : while (CONVERT_EXPR_P (sub) || TREE_CODE (sub) == NON_LVALUE_EXPR
5999 : 149989826 : || TREE_CODE (sub) == VIEW_CONVERT_EXPR)
6000 : : {
6001 : 41883954 : if (TREE_CODE (sub) == NOP_EXPR
6002 : 41883954 : && REINTERPRET_CAST_P (sub))
6003 : : return NULL_TREE;
6004 : 41883954 : sub = TREE_OPERAND (sub, 0);
6005 : : }
6006 : :
6007 : 53261245 : subtype = TREE_TYPE (sub);
6008 : 53261245 : if (!INDIRECT_TYPE_P (subtype))
6009 : : return NULL_TREE;
6010 : :
6011 : : /* Canonicalizes the given OBJ/OFF pair by iteratively absorbing
6012 : : the innermost component into the offset until it would make the
6013 : : offset positive, so that cxx_fold_indirect_ref_1 can identify
6014 : : more folding opportunities. */
6015 : 55940326 : auto canonicalize_obj_off = [] (tree& obj, tree& off) {
6016 : 2679133 : while (TREE_CODE (obj) == COMPONENT_REF
6017 : : /* We need to preserve union member accesses so that we can
6018 : : later properly diagnose accessing the wrong member. */
6019 : 1299556 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (obj, 0))) == RECORD_TYPE
6020 : 4444127 : && (tree_int_cst_sign_bit (off) || integer_zerop (off)))
6021 : : {
6022 : 548526 : tree field = TREE_OPERAND (obj, 1);
6023 : 548526 : tree pos = byte_position (field);
6024 : 548526 : if (integer_zerop (off) && integer_nonzerop (pos))
6025 : : /* If the offset is already 0, keep going as long as the
6026 : : component is at position 0. */
6027 : : break;
6028 : 546607 : off = int_const_binop (PLUS_EXPR, off, pos);
6029 : 546607 : obj = TREE_OPERAND (obj, 0);
6030 : : }
6031 : 2679133 : };
6032 : :
6033 : 53261193 : if (TREE_CODE (sub) == ADDR_EXPR)
6034 : : {
6035 : 22171539 : tree op = TREE_OPERAND (sub, 0);
6036 : 22171539 : tree optype = TREE_TYPE (op);
6037 : :
6038 : : /* *&CONST_DECL -> to the value of the const decl. */
6039 : 22171539 : if (TREE_CODE (op) == CONST_DECL)
6040 : 0 : return DECL_INITIAL (op);
6041 : : /* *&p => p; make sure to handle *&"str"[cst] here. */
6042 : 22171539 : if (similar_type_p (optype, type))
6043 : : {
6044 : 21116258 : tree fop = fold_read_from_constant_string (op);
6045 : 21116258 : if (fop)
6046 : : return fop;
6047 : : else
6048 : 21106178 : return op;
6049 : : }
6050 : : else
6051 : : {
6052 : 1055281 : tree off = integer_zero_node;
6053 : 1055281 : canonicalize_obj_off (op, off);
6054 : 1055281 : gcc_assert (integer_zerop (off));
6055 : 1055281 : return cxx_fold_indirect_ref_1 (ctx, loc, type, op, 0, empty_base);
6056 : : }
6057 : : }
6058 : 31089654 : else if (TREE_CODE (sub) == POINTER_PLUS_EXPR
6059 : 31089654 : && tree_fits_uhwi_p (TREE_OPERAND (sub, 1)))
6060 : : {
6061 : 1764725 : tree op00 = TREE_OPERAND (sub, 0);
6062 : 1764725 : tree off = TREE_OPERAND (sub, 1);
6063 : :
6064 : 1764725 : STRIP_NOPS (op00);
6065 : 1764725 : if (TREE_CODE (op00) == ADDR_EXPR)
6066 : : {
6067 : 1623852 : tree obj = TREE_OPERAND (op00, 0);
6068 : 1623852 : canonicalize_obj_off (obj, off);
6069 : 1623852 : return cxx_fold_indirect_ref_1 (ctx, loc, type, obj,
6070 : : tree_to_uhwi (off), empty_base);
6071 : : }
6072 : : }
6073 : : /* *(foo *)fooarrptr => (*fooarrptr)[0] */
6074 : 29324929 : else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
6075 : 29324929 : && similar_type_p (type, TREE_TYPE (TREE_TYPE (subtype))))
6076 : : {
6077 : 0 : tree type_domain;
6078 : 0 : tree min_val = size_zero_node;
6079 : 0 : tree newsub
6080 : 0 : = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (subtype), sub, NULL);
6081 : 0 : if (newsub)
6082 : : sub = newsub;
6083 : : else
6084 : 0 : sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub);
6085 : 0 : type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
6086 : 0 : if (type_domain && TYPE_MIN_VALUE (type_domain))
6087 : 0 : min_val = TYPE_MIN_VALUE (type_domain);
6088 : 0 : return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
6089 : 0 : NULL_TREE);
6090 : : }
6091 : :
6092 : : return NULL_TREE;
6093 : : }
6094 : :
6095 : : static tree
6096 : 30548959 : cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t,
6097 : : value_cat lval,
6098 : : bool *non_constant_p, bool *overflow_p)
6099 : : {
6100 : 30548959 : tree orig_op0 = TREE_OPERAND (t, 0);
6101 : 30548959 : bool empty_base = false;
6102 : :
6103 : : /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second
6104 : : operand is an integer-zero. Otherwise reject the MEM_REF for now. */
6105 : :
6106 : 30548959 : if (TREE_CODE (t) == MEM_REF
6107 : 30548959 : && (!TREE_OPERAND (t, 1) || !integer_zerop (TREE_OPERAND (t, 1))))
6108 : : {
6109 : 9 : gcc_assert (ctx->quiet);
6110 : 9 : *non_constant_p = true;
6111 : 9 : return t;
6112 : : }
6113 : :
6114 : : /* First try to simplify it directly. */
6115 : 30548950 : tree r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t),
6116 : : orig_op0, &empty_base);
6117 : 30548950 : if (!r)
6118 : : {
6119 : : /* If that didn't work, evaluate the operand first. */
6120 : 29267487 : tree op0 = cxx_eval_constant_expression (ctx, orig_op0,
6121 : : vc_prvalue, non_constant_p,
6122 : : overflow_p);
6123 : : /* Don't VERIFY_CONSTANT here. */
6124 : 29267487 : if (*non_constant_p)
6125 : : return t;
6126 : :
6127 : 18349724 : if (!lval && integer_zerop (op0))
6128 : : {
6129 : 68 : if (!ctx->quiet)
6130 : 12 : error ("dereferencing a null pointer");
6131 : 68 : *non_constant_p = true;
6132 : 68 : return t;
6133 : : }
6134 : :
6135 : 18349656 : r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t), op0,
6136 : : &empty_base);
6137 : 18349656 : if (r == NULL_TREE)
6138 : : {
6139 : : /* We couldn't fold to a constant value. Make sure it's not
6140 : : something we should have been able to fold. */
6141 : 201497 : tree sub = op0;
6142 : 201497 : STRIP_NOPS (sub);
6143 : 201497 : if (TREE_CODE (sub) == ADDR_EXPR)
6144 : : {
6145 : 1497 : gcc_assert (!similar_type_p
6146 : : (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t)));
6147 : : /* DR 1188 says we don't have to deal with this. */
6148 : 1497 : if (!ctx->quiet)
6149 : 5 : error_at (cp_expr_loc_or_input_loc (t),
6150 : : "accessing value of %qE through a %qT glvalue in a "
6151 : : "constant expression", build_fold_indirect_ref (sub),
6152 : 4 : TREE_TYPE (t));
6153 : 1497 : *non_constant_p = true;
6154 : 1497 : return t;
6155 : : }
6156 : :
6157 : 200000 : if (lval == vc_glvalue && op0 != orig_op0)
6158 : 17928 : return build1 (INDIRECT_REF, TREE_TYPE (t), op0);
6159 : 182072 : if (!lval)
6160 : 128239 : VERIFY_CONSTANT (t);
6161 : 182072 : return t;
6162 : : }
6163 : : }
6164 : :
6165 : 19429622 : r = cxx_eval_constant_expression (ctx, r,
6166 : : lval, non_constant_p, overflow_p);
6167 : 19429622 : if (*non_constant_p)
6168 : : return t;
6169 : :
6170 : : /* If we're pulling out the value of an empty base, just return an empty
6171 : : CONSTRUCTOR. */
6172 : 13799835 : if (empty_base && !lval)
6173 : : {
6174 : 13076 : r = build_constructor (TREE_TYPE (t), NULL);
6175 : 13076 : TREE_CONSTANT (r) = true;
6176 : : }
6177 : :
6178 : : return r;
6179 : : }
6180 : :
6181 : : /* Complain about R, a DECL that is accessed outside its lifetime. */
6182 : :
6183 : : static void
6184 : 34 : outside_lifetime_error (location_t loc, tree r)
6185 : : {
6186 : 34 : auto_diagnostic_group d;
6187 : 34 : if (DECL_NAME (r) == heap_deleted_identifier)
6188 : : {
6189 : : /* Provide a more accurate message for deleted variables. */
6190 : 6 : error_at (loc, "use of allocated storage after deallocation "
6191 : : "in a constant expression");
6192 : 6 : inform (DECL_SOURCE_LOCATION (r), "allocated here");
6193 : : }
6194 : : else
6195 : : {
6196 : 28 : error_at (loc, "accessing %qE outside its lifetime", r);
6197 : 28 : inform (DECL_SOURCE_LOCATION (r), "declared here");
6198 : : }
6199 : 34 : }
6200 : :
6201 : : /* Complain about R, a VAR_DECL, not being usable in a constant expression.
6202 : : FUNDEF_P is true if we're checking a constexpr function body.
6203 : : Shared between potential_constant_expression and
6204 : : cxx_eval_constant_expression. */
6205 : :
6206 : : static void
6207 : 294 : non_const_var_error (location_t loc, tree r, bool fundef_p)
6208 : : {
6209 : 294 : auto_diagnostic_group d;
6210 : 294 : tree type = TREE_TYPE (r);
6211 : 294 : if (DECL_NAME (r) == heap_uninit_identifier
6212 : 294 : || DECL_NAME (r) == heap_identifier
6213 : 291 : || DECL_NAME (r) == heap_vec_uninit_identifier
6214 : 585 : || DECL_NAME (r) == heap_vec_identifier)
6215 : : {
6216 : 3 : if (constexpr_error (loc, fundef_p, "the content of uninitialized "
6217 : : "storage is not usable in a constant expression"))
6218 : 3 : inform (DECL_SOURCE_LOCATION (r), "allocated here");
6219 : 3 : return;
6220 : : }
6221 : 291 : if (DECL_NAME (r) == heap_deleted_identifier)
6222 : : {
6223 : 0 : if (constexpr_error (loc, fundef_p, "use of allocated storage after "
6224 : : "deallocation in a constant expression"))
6225 : 0 : inform (DECL_SOURCE_LOCATION (r), "allocated here");
6226 : 0 : return;
6227 : : }
6228 : 291 : if (!constexpr_error (loc, fundef_p, "the value of %qD is not usable in "
6229 : : "a constant expression", r))
6230 : : return;
6231 : : /* Avoid error cascade. */
6232 : 290 : if (DECL_INITIAL (r) == error_mark_node)
6233 : : return;
6234 : 276 : if (DECL_DECLARED_CONSTEXPR_P (r))
6235 : 3 : inform (DECL_SOURCE_LOCATION (r),
6236 : : "%qD used in its own initializer", r);
6237 : 273 : else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6238 : : {
6239 : 216 : if (!CP_TYPE_CONST_P (type))
6240 : 187 : inform (DECL_SOURCE_LOCATION (r),
6241 : : "%q#D is not const", r);
6242 : 29 : else if (CP_TYPE_VOLATILE_P (type))
6243 : 0 : inform (DECL_SOURCE_LOCATION (r),
6244 : : "%q#D is volatile", r);
6245 : 29 : else if (!DECL_INITIAL (r)
6246 : 9 : || !TREE_CONSTANT (DECL_INITIAL (r))
6247 : 37 : || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r))
6248 : 29 : inform (DECL_SOURCE_LOCATION (r),
6249 : : "%qD was not initialized with a constant "
6250 : : "expression", r);
6251 : : else
6252 : 0 : gcc_unreachable ();
6253 : : }
6254 : 57 : else if (TYPE_REF_P (type))
6255 : 9 : inform (DECL_SOURCE_LOCATION (r),
6256 : : "%qD was not initialized with a constant "
6257 : : "expression", r);
6258 : : else
6259 : : {
6260 : 48 : if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r))
6261 : 48 : inform (DECL_SOURCE_LOCATION (r),
6262 : : "%qD was not declared %<constexpr%>", r);
6263 : : else
6264 : 0 : inform (DECL_SOURCE_LOCATION (r),
6265 : : "%qD does not have integral or enumeration type",
6266 : : r);
6267 : : }
6268 : 294 : }
6269 : :
6270 : : /* Subroutine of cxx_eval_constant_expression.
6271 : : Like cxx_eval_unary_expression, except for trinary expressions. */
6272 : :
6273 : : static tree
6274 : 16 : cxx_eval_trinary_expression (const constexpr_ctx *ctx, tree t,
6275 : : value_cat lval,
6276 : : bool *non_constant_p, bool *overflow_p)
6277 : : {
6278 : 16 : int i;
6279 : 16 : tree args[3];
6280 : 16 : tree val;
6281 : :
6282 : 37 : for (i = 0; i < 3; i++)
6283 : : {
6284 : 30 : args[i] = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, i),
6285 : : lval,
6286 : : non_constant_p, overflow_p);
6287 : 30 : VERIFY_CONSTANT (args[i]);
6288 : : }
6289 : :
6290 : 7 : val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t),
6291 : : args[0], args[1], args[2]);
6292 : 7 : if (val == NULL_TREE)
6293 : : return t;
6294 : 7 : VERIFY_CONSTANT (val);
6295 : : return val;
6296 : : }
6297 : :
6298 : : /* True if T was declared in a function declared to be constexpr, and
6299 : : therefore potentially constant in C++14. */
6300 : :
6301 : : bool
6302 : 77799058 : var_in_constexpr_fn (tree t)
6303 : : {
6304 : 77799058 : tree ctx = DECL_CONTEXT (t);
6305 : 77799058 : return (ctx && TREE_CODE (ctx) == FUNCTION_DECL
6306 : 148180731 : && DECL_DECLARED_CONSTEXPR_P (ctx));
6307 : : }
6308 : :
6309 : : /* True if a function might be constexpr: either a function that was
6310 : : declared constexpr, or a C++17 lambda op(). */
6311 : :
6312 : : bool
6313 : 422664361 : maybe_constexpr_fn (tree t)
6314 : : {
6315 : 422664361 : return (DECL_DECLARED_CONSTEXPR_P (t)
6316 : 192081509 : || (cxx_dialect >= cxx17 && LAMBDA_FUNCTION_P (t))
6317 : 610067694 : || (flag_implicit_constexpr
6318 : 66 : && DECL_DECLARED_INLINE_P (STRIP_TEMPLATE (t))));
6319 : : }
6320 : :
6321 : : /* True if T was declared in a function that might be constexpr: either a
6322 : : function that was declared constexpr, or a C++17 lambda op(). */
6323 : :
6324 : : bool
6325 : 51527531 : var_in_maybe_constexpr_fn (tree t)
6326 : : {
6327 : 103054500 : return (DECL_FUNCTION_SCOPE_P (t)
6328 : 96204635 : && maybe_constexpr_fn (DECL_CONTEXT (t)));
6329 : : }
6330 : :
6331 : : /* We're assigning INIT to TARGET. In do_build_copy_constructor and
6332 : : build_over_call we implement trivial copy of a class with tail padding using
6333 : : assignment of character arrays, which is valid in normal code, but not in
6334 : : constexpr evaluation. We don't need to worry about clobbering tail padding
6335 : : in constexpr evaluation, so strip the type punning. */
6336 : :
6337 : : static void
6338 : 28588211 : maybe_simplify_trivial_copy (tree &target, tree &init)
6339 : : {
6340 : 28588211 : if (TREE_CODE (target) == MEM_REF
6341 : 797 : && TREE_CODE (init) == MEM_REF
6342 : 797 : && TREE_TYPE (target) == TREE_TYPE (init)
6343 : 797 : && TREE_CODE (TREE_TYPE (target)) == ARRAY_TYPE
6344 : 28589008 : && TREE_TYPE (TREE_TYPE (target)) == unsigned_char_type_node)
6345 : : {
6346 : 797 : target = build_fold_indirect_ref (TREE_OPERAND (target, 0));
6347 : 797 : init = build_fold_indirect_ref (TREE_OPERAND (init, 0));
6348 : : }
6349 : 28588211 : }
6350 : :
6351 : : /* Returns true if REF, which is a COMPONENT_REF, has any fields
6352 : : of constant type. This does not check for 'mutable', so the
6353 : : caller is expected to be mindful of that. */
6354 : :
6355 : : static bool
6356 : 277 : cref_has_const_field (tree ref)
6357 : : {
6358 : 346 : while (TREE_CODE (ref) == COMPONENT_REF)
6359 : : {
6360 : 337 : if (CP_TYPE_CONST_P (TREE_TYPE (TREE_OPERAND (ref, 1))))
6361 : : return true;
6362 : 69 : ref = TREE_OPERAND (ref, 0);
6363 : : }
6364 : : return false;
6365 : : }
6366 : :
6367 : : /* Return true if we are modifying something that is const during constant
6368 : : expression evaluation. CODE is the code of the statement, OBJ is the
6369 : : object in question, MUTABLE_P is true if one of the subobjects were
6370 : : declared mutable. */
6371 : :
6372 : : static bool
6373 : 25548798 : modifying_const_object_p (tree_code code, tree obj, bool mutable_p)
6374 : : {
6375 : : /* If this is initialization, there's no problem. */
6376 : 25548798 : if (code != MODIFY_EXPR)
6377 : : return false;
6378 : :
6379 : : /* [basic.type.qualifier] "A const object is an object of type
6380 : : const T or a non-mutable subobject of a const object." */
6381 : 7500126 : if (mutable_p)
6382 : : return false;
6383 : :
6384 : 7499390 : if (TREE_READONLY (obj))
6385 : : return true;
6386 : :
6387 : 7497592 : if (CP_TYPE_CONST_P (TREE_TYPE (obj)))
6388 : : {
6389 : : /* Although a COMPONENT_REF may have a const type, we should
6390 : : only consider it modifying a const object when any of the
6391 : : field components is const. This can happen when using
6392 : : constructs such as const_cast<const T &>(m), making something
6393 : : const even though it wasn't declared const. */
6394 : 3123 : if (TREE_CODE (obj) == COMPONENT_REF)
6395 : 277 : return cref_has_const_field (obj);
6396 : : else
6397 : : return true;
6398 : : }
6399 : :
6400 : : return false;
6401 : : }
6402 : :
6403 : : /* Evaluate an INIT_EXPR or MODIFY_EXPR. */
6404 : :
6405 : : static tree
6406 : 31311059 : cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
6407 : : value_cat lval,
6408 : : bool *non_constant_p, bool *overflow_p)
6409 : : {
6410 : 31311059 : constexpr_ctx new_ctx = *ctx;
6411 : :
6412 : 31311059 : tree init = TREE_OPERAND (t, 1);
6413 : :
6414 : 2905467 : if (TREE_CLOBBER_P (init)
6415 : 34064320 : && CLOBBER_KIND (init) < CLOBBER_OBJECT_END)
6416 : : /* Only handle clobbers ending the lifetime of objects.
6417 : : ??? We should probably set CONSTRUCTOR_NO_CLEARING. */
6418 : 2722848 : return void_node;
6419 : :
6420 : : /* First we figure out where we're storing to. */
6421 : 28588211 : tree target = TREE_OPERAND (t, 0);
6422 : :
6423 : 28588211 : maybe_simplify_trivial_copy (target, init);
6424 : :
6425 : 28588211 : tree type = TREE_TYPE (target);
6426 : 28588211 : bool preeval = SCALAR_TYPE_P (type) || TREE_CODE (t) == MODIFY_EXPR;
6427 : 22372341 : if (preeval && !TREE_CLOBBER_P (init))
6428 : : {
6429 : : /* Evaluate the value to be stored without knowing what object it will be
6430 : : stored in, so that any side-effects happen first. */
6431 : 22341928 : if (!SCALAR_TYPE_P (type))
6432 : 46300 : new_ctx.ctor = new_ctx.object = NULL_TREE;
6433 : 22341928 : init = cxx_eval_constant_expression (&new_ctx, init, vc_prvalue,
6434 : : non_constant_p, overflow_p);
6435 : 22341928 : if (*non_constant_p)
6436 : : return t;
6437 : : }
6438 : :
6439 : 23124611 : bool evaluated = false;
6440 : 23124611 : if (lval == vc_glvalue)
6441 : : {
6442 : : /* If we want to return a reference to the target, we need to evaluate it
6443 : : as a whole; otherwise, only evaluate the innermost piece to avoid
6444 : : building up unnecessary *_REFs. */
6445 : 0 : target = cxx_eval_constant_expression (ctx, target, lval,
6446 : : non_constant_p, overflow_p);
6447 : 0 : evaluated = true;
6448 : 0 : if (*non_constant_p)
6449 : : return t;
6450 : : }
6451 : :
6452 : : /* Find the underlying variable. */
6453 : 23124611 : releasing_vec refs;
6454 : 23124611 : tree object = NULL_TREE;
6455 : : /* If we're modifying a const object, save it. */
6456 : 23124611 : tree const_object_being_modified = NULL_TREE;
6457 : 23124611 : bool mutable_p = false;
6458 : 77149861 : for (tree probe = target; object == NULL_TREE; )
6459 : : {
6460 : 54025496 : switch (TREE_CODE (probe))
6461 : : {
6462 : 7776512 : case BIT_FIELD_REF:
6463 : 7776512 : case COMPONENT_REF:
6464 : 7776512 : case ARRAY_REF:
6465 : 7776512 : {
6466 : 7776512 : tree ob = TREE_OPERAND (probe, 0);
6467 : 7776512 : tree elt = TREE_OPERAND (probe, 1);
6468 : 7776512 : if (TREE_CODE (elt) == FIELD_DECL && DECL_MUTABLE_P (elt))
6469 : : mutable_p = true;
6470 : 7776512 : if (TREE_CODE (probe) == ARRAY_REF)
6471 : : {
6472 : 1109421 : elt = eval_and_check_array_index (ctx, probe, false,
6473 : : non_constant_p, overflow_p);
6474 : 1109421 : if (*non_constant_p)
6475 : 54 : return t;
6476 : : }
6477 : : /* We don't check modifying_const_object_p for ARRAY_REFs. Given
6478 : : "int a[10]", an ARRAY_REF "a[2]" can be "const int", even though
6479 : : the array isn't const. Instead, check "a" in the next iteration;
6480 : : that will detect modifying "const int a[10]". */
6481 : 6667091 : else if (evaluated
6482 : 2424433 : && modifying_const_object_p (TREE_CODE (t), probe,
6483 : : mutable_p)
6484 : 6667359 : && const_object_being_modified == NULL_TREE)
6485 : : const_object_being_modified = probe;
6486 : :
6487 : : /* Track named member accesses for unions to validate modifications
6488 : : that change active member. */
6489 : 7776458 : if (!evaluated && TREE_CODE (probe) == COMPONENT_REF)
6490 : 4242658 : vec_safe_push (refs, probe);
6491 : : else
6492 : 3533800 : vec_safe_push (refs, NULL_TREE);
6493 : :
6494 : 7776458 : vec_safe_push (refs, elt);
6495 : 7776458 : vec_safe_push (refs, TREE_TYPE (probe));
6496 : 7776458 : probe = ob;
6497 : : }
6498 : 7776458 : break;
6499 : :
6500 : 15 : case REALPART_EXPR:
6501 : 15 : gcc_assert (refs->is_empty ());
6502 : 15 : vec_safe_push (refs, NULL_TREE);
6503 : 15 : vec_safe_push (refs, probe);
6504 : 15 : vec_safe_push (refs, TREE_TYPE (probe));
6505 : 15 : probe = TREE_OPERAND (probe, 0);
6506 : 15 : break;
6507 : :
6508 : 17 : case IMAGPART_EXPR:
6509 : 17 : gcc_assert (refs->is_empty ());
6510 : 17 : vec_safe_push (refs, NULL_TREE);
6511 : 17 : vec_safe_push (refs, probe);
6512 : 17 : vec_safe_push (refs, TREE_TYPE (probe));
6513 : 17 : probe = TREE_OPERAND (probe, 0);
6514 : 17 : break;
6515 : :
6516 : 46248952 : default:
6517 : 46248952 : if (evaluated)
6518 : : object = probe;
6519 : : else
6520 : : {
6521 : 23124587 : tree pvar = tree_strip_any_location_wrapper (probe);
6522 : 23124587 : if (VAR_P (pvar) && DECL_ANON_UNION_VAR_P (pvar))
6523 : : {
6524 : : /* Stores to DECL_ANON_UNION_VAR_P var are allowed to change
6525 : : active union member. */
6526 : 9 : probe = DECL_VALUE_EXPR (pvar);
6527 : 9 : break;
6528 : : }
6529 : 23124578 : probe = cxx_eval_constant_expression (ctx, probe, vc_glvalue,
6530 : : non_constant_p, overflow_p);
6531 : 23124578 : evaluated = true;
6532 : 23124578 : if (*non_constant_p)
6533 : : return t;
6534 : : }
6535 : : break;
6536 : : }
6537 : : }
6538 : :
6539 : 23124365 : if (modifying_const_object_p (TREE_CODE (t), object, mutable_p)
6540 : 23124365 : && const_object_being_modified == NULL_TREE)
6541 : 23124365 : const_object_being_modified = object;
6542 : :
6543 : 23124365 : if (DECL_P (object)
6544 : 23124324 : && TREE_CLOBBER_P (init)
6545 : 23154778 : && DECL_NAME (object) == heap_deleted_identifier)
6546 : : /* Ignore clobbers of deleted allocations for now; we'll get a better error
6547 : : message later when operator delete is called. */
6548 : 15 : return void_node;
6549 : :
6550 : : /* And then find/build up our initializer for the path to the subobject
6551 : : we're initializing. */
6552 : 23124350 : tree *valp;
6553 : 23124350 : if (DECL_P (object))
6554 : 23124309 : valp = ctx->global->get_value_ptr (object, TREE_CODE (t) == INIT_EXPR);
6555 : : else
6556 : 41 : valp = NULL;
6557 : 23124350 : if (!valp)
6558 : : {
6559 : : /* A constant-expression cannot modify objects from outside the
6560 : : constant-expression. */
6561 : 15060 : if (!ctx->quiet)
6562 : : {
6563 : 22 : auto_diagnostic_group d;
6564 : 22 : if (DECL_P (object) && DECL_NAME (object) == heap_deleted_identifier)
6565 : : {
6566 : 0 : error ("modification of allocated storage after deallocation "
6567 : : "is not a constant expression");
6568 : 0 : inform (DECL_SOURCE_LOCATION (object), "allocated here");
6569 : : }
6570 : 22 : else if (DECL_P (object) && ctx->global->is_outside_lifetime (object))
6571 : : {
6572 : 10 : if (TREE_CLOBBER_P (init))
6573 : 3 : error ("destroying %qE outside its lifetime", object);
6574 : : else
6575 : 7 : error ("modification of %qE outside its lifetime "
6576 : : "is not a constant expression", object);
6577 : 10 : inform (DECL_SOURCE_LOCATION (object), "declared here");
6578 : : }
6579 : : else
6580 : : {
6581 : 12 : if (TREE_CLOBBER_P (init))
6582 : 6 : error ("destroying %qE from outside current evaluation "
6583 : : "is not a constant expression", object);
6584 : : else
6585 : 6 : error ("modification of %qE from outside current evaluation "
6586 : : "is not a constant expression", object);
6587 : : }
6588 : 22 : }
6589 : 15060 : *non_constant_p = true;
6590 : 15060 : return t;
6591 : : }
6592 : :
6593 : : /* Handle explicit end-of-lifetime. */
6594 : 23109290 : if (TREE_CLOBBER_P (init))
6595 : : {
6596 : 30368 : if (refs->is_empty ())
6597 : 17772 : ctx->global->destroy_value (object);
6598 : 30368 : return void_node;
6599 : : }
6600 : :
6601 : 23078922 : type = TREE_TYPE (object);
6602 : 23078922 : bool no_zero_init = true;
6603 : 23078922 : bool zero_padding_bits = false;
6604 : :
6605 : 46157844 : auto_vec<tree *> ctors;
6606 : 46157844 : releasing_vec indexes;
6607 : 46157844 : auto_vec<int> index_pos_hints;
6608 : 23078922 : bool activated_union_member_p = false;
6609 : 23078922 : bool empty_base = false;
6610 : 30816978 : while (!refs->is_empty ())
6611 : : {
6612 : 7756149 : if (*valp == NULL_TREE)
6613 : : {
6614 : 852420 : *valp = build_constructor (type, NULL);
6615 : 852420 : CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
6616 : 852420 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
6617 : : }
6618 : 6903729 : else if (STRIP_ANY_LOCATION_WRAPPER (*valp),
6619 : 6903729 : TREE_CODE (*valp) == STRING_CST)
6620 : : {
6621 : : /* An array was initialized with a string constant, and now
6622 : : we're writing into one of its elements. Explode the
6623 : : single initialization into a set of element
6624 : : initializations. */
6625 : 4007 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
6626 : :
6627 : 4007 : tree string = *valp;
6628 : 4007 : tree elt_type = TREE_TYPE (type);
6629 : 4007 : unsigned chars_per_elt = (TYPE_PRECISION (elt_type)
6630 : 4007 : / TYPE_PRECISION (char_type_node));
6631 : 4007 : unsigned num_elts = TREE_STRING_LENGTH (string) / chars_per_elt;
6632 : 4007 : tree ary_ctor = build_constructor (type, NULL);
6633 : :
6634 : 4007 : vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor), num_elts);
6635 : 8169 : for (unsigned ix = 0; ix != num_elts; ix++)
6636 : : {
6637 : 4162 : constructor_elt elt =
6638 : : {
6639 : 4162 : build_int_cst (size_type_node, ix),
6640 : 4162 : extract_string_elt (string, chars_per_elt, ix)
6641 : 4162 : };
6642 : 4162 : CONSTRUCTOR_ELTS (ary_ctor)->quick_push (elt);
6643 : : }
6644 : :
6645 : 4007 : *valp = ary_ctor;
6646 : : }
6647 : :
6648 : 7756149 : enum tree_code code = TREE_CODE (type);
6649 : 7756149 : tree reftype = refs->pop();
6650 : 7756149 : tree index = refs->pop();
6651 : 7756149 : bool is_access_expr = refs->pop() != NULL_TREE;
6652 : :
6653 : 7756149 : if (code == COMPLEX_TYPE)
6654 : : {
6655 : 32 : if (TREE_CODE (*valp) == COMPLEX_CST)
6656 : 30 : *valp = build2 (COMPLEX_EXPR, type, TREE_REALPART (*valp),
6657 : 30 : TREE_IMAGPART (*valp));
6658 : 2 : else if (TREE_CODE (*valp) == CONSTRUCTOR
6659 : 1 : && CONSTRUCTOR_NELTS (*valp) == 0
6660 : 3 : && CONSTRUCTOR_NO_CLEARING (*valp))
6661 : : {
6662 : 1 : tree r = build_constructor (reftype, NULL);
6663 : 1 : CONSTRUCTOR_NO_CLEARING (r) = 1;
6664 : 1 : *valp = build2 (COMPLEX_EXPR, type, r, r);
6665 : : }
6666 : 32 : gcc_assert (TREE_CODE (*valp) == COMPLEX_EXPR);
6667 : 32 : ctors.safe_push (valp);
6668 : 32 : vec_safe_push (indexes, index);
6669 : 32 : valp = &TREE_OPERAND (*valp, TREE_CODE (index) == IMAGPART_EXPR);
6670 : 32 : gcc_checking_assert (refs->is_empty ());
6671 : : type = reftype;
6672 : 18061 : break;
6673 : : }
6674 : :
6675 : : /* If the value of object is already zero-initialized, any new ctors for
6676 : : subobjects will also be zero-initialized. Similarly with zeroing of
6677 : : padding bits. */
6678 : 7756117 : no_zero_init = CONSTRUCTOR_NO_CLEARING (*valp);
6679 : 7756117 : zero_padding_bits = CONSTRUCTOR_ZERO_PADDING_BITS (*valp);
6680 : :
6681 : 7756117 : if (code == RECORD_TYPE && is_empty_field (index))
6682 : : /* Don't build a sub-CONSTRUCTOR for an empty base or field, as they
6683 : : have no data and might have an offset lower than previously declared
6684 : : fields, which confuses the middle-end. The code below will notice
6685 : : that we don't have a CONSTRUCTOR for our inner target and just
6686 : : return init. */
6687 : : {
6688 : : empty_base = true;
6689 : : break;
6690 : : }
6691 : :
6692 : : /* If a union is zero-initialized, its first non-static named data member
6693 : : is zero-initialized (and therefore active). */
6694 : 7738056 : if (code == UNION_TYPE
6695 : 7738056 : && !no_zero_init
6696 : 7738056 : && CONSTRUCTOR_NELTS (*valp) == 0)
6697 : 1950 : if (tree first = next_aggregate_field (TYPE_FIELDS (type)))
6698 : 1950 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (*valp), first, NULL_TREE);
6699 : :
6700 : : /* Check for implicit change of active member for a union. */
6701 : 7738056 : if (code == UNION_TYPE
6702 : 146508 : && (CONSTRUCTOR_NELTS (*valp) == 0
6703 : 42440 : || CONSTRUCTOR_ELT (*valp, 0)->index != index)
6704 : : /* An INIT_EXPR of the last member in an access chain is always OK,
6705 : : but still check implicit change of members earlier on; see
6706 : : cpp2a/constexpr-union6.C. */
6707 : 7845476 : && !(TREE_CODE (t) == INIT_EXPR && refs->is_empty ()))
6708 : : {
6709 : 73785 : bool has_active_member = CONSTRUCTOR_NELTS (*valp) != 0;
6710 : 73785 : tree inner = strip_array_types (reftype);
6711 : :
6712 : 73785 : if (has_active_member && cxx_dialect < cxx20)
6713 : : {
6714 : 68 : if (!ctx->quiet)
6715 : 22 : error_at (cp_expr_loc_or_input_loc (t),
6716 : : "change of the active member of a union "
6717 : : "from %qD to %qD is not a constant expression "
6718 : : "before C++20",
6719 : 22 : CONSTRUCTOR_ELT (*valp, 0)->index,
6720 : : index);
6721 : 68 : *non_constant_p = true;
6722 : : }
6723 : 73717 : else if (!is_access_expr
6724 : 73717 : || (TREE_CODE (t) == MODIFY_EXPR
6725 : 1404 : && CLASS_TYPE_P (inner)
6726 : 15 : && !type_has_non_deleted_trivial_default_ctor (inner)))
6727 : : {
6728 : : /* Diagnose changing active union member after initialization
6729 : : without a valid member access expression, as described in
6730 : : [class.union.general] p5. */
6731 : 72310 : if (!ctx->quiet)
6732 : : {
6733 : 36 : auto_diagnostic_group d;
6734 : 36 : if (has_active_member)
6735 : 24 : error_at (cp_expr_loc_or_input_loc (t),
6736 : : "accessing %qD member instead of initialized "
6737 : : "%qD member in constant expression",
6738 : 24 : index, CONSTRUCTOR_ELT (*valp, 0)->index);
6739 : : else
6740 : 12 : error_at (cp_expr_loc_or_input_loc (t),
6741 : : "accessing uninitialized member %qD",
6742 : : index);
6743 : 36 : if (is_access_expr)
6744 : 3 : inform (DECL_SOURCE_LOCATION (index),
6745 : : "%qD does not implicitly begin its lifetime "
6746 : : "because %qT does not have a non-deleted "
6747 : : "trivial default constructor, use "
6748 : : "%<std::construct_at%> instead",
6749 : : index, inner);
6750 : : else
6751 : 33 : inform (DECL_SOURCE_LOCATION (index),
6752 : : "initializing %qD requires a member access "
6753 : : "expression as the left operand of the assignment",
6754 : : index);
6755 : 36 : }
6756 : 72310 : *non_constant_p = true;
6757 : : }
6758 : 1407 : else if (has_active_member && CONSTRUCTOR_NO_CLEARING (*valp))
6759 : : {
6760 : : /* Diagnose changing the active union member while the union
6761 : : is in the process of being initialized. */
6762 : 9 : if (!ctx->quiet)
6763 : 3 : error_at (cp_expr_loc_or_input_loc (t),
6764 : : "change of the active member of a union "
6765 : : "from %qD to %qD during initialization",
6766 : 3 : CONSTRUCTOR_ELT (*valp, 0)->index,
6767 : : index);
6768 : 9 : *non_constant_p = true;
6769 : : }
6770 : : no_zero_init = true;
6771 : : }
6772 : :
6773 : 7738056 : ctors.safe_push (valp);
6774 : 7738056 : vec_safe_push (indexes, index);
6775 : :
6776 : 7738056 : constructor_elt *cep
6777 : 7738056 : = get_or_insert_ctor_field (*valp, index);
6778 : 7738056 : index_pos_hints.safe_push (cep - CONSTRUCTOR_ELTS (*valp)->begin());
6779 : :
6780 : 7738056 : if (code == UNION_TYPE)
6781 : 146508 : activated_union_member_p = true;
6782 : :
6783 : 7738056 : valp = &cep->value;
6784 : 7738056 : type = reftype;
6785 : : }
6786 : :
6787 : : /* For initialization of an empty base, the original target will be
6788 : : *(base*)this, evaluation of which resolves to the object
6789 : : argument, which has the derived type rather than the base type. */
6790 : 46139783 : if (!empty_base && !(same_type_ignoring_top_level_qualifiers_p
6791 : 23060861 : (initialized_type (init), type)))
6792 : : {
6793 : 2431 : gcc_assert (is_empty_class (TREE_TYPE (target)));
6794 : : empty_base = true;
6795 : : }
6796 : :
6797 : : /* Detect modifying a constant object in constexpr evaluation.
6798 : : We have found a const object that is being modified. Figure out
6799 : : if we need to issue an error. Consider
6800 : :
6801 : : struct A {
6802 : : int n;
6803 : : constexpr A() : n(1) { n = 2; } // #1
6804 : : };
6805 : : struct B {
6806 : : const A a;
6807 : : constexpr B() { a.n = 3; } // #2
6808 : : };
6809 : : constexpr B b{};
6810 : :
6811 : : #1 is OK, since we're modifying an object under construction, but
6812 : : #2 is wrong, since "a" is const and has been fully constructed.
6813 : : To track it, we use the TREE_READONLY bit in the object's CONSTRUCTOR
6814 : : which means that the object is read-only. For the example above, the
6815 : : *ctors stack at the point of #2 will look like:
6816 : :
6817 : : ctors[0] = {.a={.n=2}} TREE_READONLY = 0
6818 : : ctors[1] = {.n=2} TREE_READONLY = 1
6819 : :
6820 : : and we're modifying "b.a", so we search the stack and see if the
6821 : : constructor for "b.a" has already run. */
6822 : 23078922 : if (const_object_being_modified)
6823 : : {
6824 : 4207 : bool fail = false;
6825 : 4207 : tree const_objtype
6826 : 4207 : = strip_array_types (TREE_TYPE (const_object_being_modified));
6827 : 4207 : if (!CLASS_TYPE_P (const_objtype))
6828 : : fail = true;
6829 : : else
6830 : : {
6831 : : /* [class.ctor]p5 "A constructor can be invoked for a const,
6832 : : volatile, or const volatile object. const and volatile
6833 : : semantics are not applied on an object under construction.
6834 : : They come into effect when the constructor for the most
6835 : : derived object ends." */
6836 : 12475 : for (tree *elt : ctors)
6837 : 4271 : if (same_type_ignoring_top_level_qualifiers_p
6838 : 4271 : (TREE_TYPE (const_object_being_modified), TREE_TYPE (*elt)))
6839 : : {
6840 : 4102 : fail = TREE_READONLY (*elt);
6841 : 4102 : break;
6842 : : }
6843 : : }
6844 : 4102 : if (fail)
6845 : : {
6846 : 198 : if (!ctx->quiet)
6847 : 63 : modifying_const_object_error (t, const_object_being_modified);
6848 : 198 : *non_constant_p = true;
6849 : 198 : return t;
6850 : : }
6851 : : }
6852 : :
6853 : 23078724 : if (!preeval)
6854 : : {
6855 : : /* We're handling an INIT_EXPR of class type, so the value of the
6856 : : initializer can depend on the object it's initializing. */
6857 : :
6858 : : /* Create a new CONSTRUCTOR in case evaluation of the initializer
6859 : : wants to modify it. */
6860 : 6202173 : if (*valp == NULL_TREE)
6861 : : {
6862 : 6070226 : *valp = build_constructor (type, NULL);
6863 : 6070226 : CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
6864 : 6070226 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
6865 : : }
6866 : 6202173 : new_ctx.ctor = empty_base ? NULL_TREE : *valp;
6867 : 6202173 : new_ctx.object = target;
6868 : : /* Avoid temporary materialization when initializing from a TARGET_EXPR.
6869 : : We don't need to mess with AGGR_EXPR_SLOT/VEC_INIT_EXPR_SLOT because
6870 : : expansion of those trees uses ctx instead. */
6871 : 6202173 : if (TREE_CODE (init) == TARGET_EXPR)
6872 : 1560167 : if (tree tinit = TARGET_EXPR_INITIAL (init))
6873 : 1560167 : init = tinit;
6874 : 6202173 : init = cxx_eval_constant_expression (&new_ctx, init, vc_prvalue,
6875 : : non_constant_p, overflow_p);
6876 : : /* The hash table might have moved since the get earlier, and the
6877 : : initializer might have mutated the underlying CONSTRUCTORs, so we must
6878 : : recompute VALP. */
6879 : 6202173 : valp = ctx->global->get_value_ptr (object, TREE_CODE (t) == INIT_EXPR);
6880 : 7001030 : for (unsigned i = 0; i < vec_safe_length (indexes); i++)
6881 : : {
6882 : 798857 : ctors[i] = valp;
6883 : 798857 : constructor_elt *cep
6884 : 798857 : = get_or_insert_ctor_field (*valp, indexes[i], index_pos_hints[i]);
6885 : 798857 : valp = &cep->value;
6886 : : }
6887 : : }
6888 : :
6889 : 23078724 : if (*non_constant_p)
6890 : : return t;
6891 : :
6892 : : /* Don't share a CONSTRUCTOR that might be changed later. */
6893 : 22042766 : init = unshare_constructor (init);
6894 : :
6895 : 22042766 : gcc_checking_assert (!*valp || (same_type_ignoring_top_level_qualifiers_p
6896 : : (TREE_TYPE (*valp), type)));
6897 : 22042766 : if (empty_base)
6898 : : {
6899 : : /* Just evaluate the initializer and return, since there's no actual data
6900 : : to store, and we didn't build a CONSTRUCTOR. */
6901 : 19441 : if (!*valp)
6902 : : {
6903 : : /* But do make sure we have something in *valp. */
6904 : 0 : *valp = build_constructor (type, nullptr);
6905 : 0 : CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
6906 : 0 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
6907 : : }
6908 : : }
6909 : 22023325 : else if (*valp && TREE_CODE (*valp) == CONSTRUCTOR
6910 : 5263122 : && TREE_CODE (init) == CONSTRUCTOR)
6911 : : {
6912 : : /* An outer ctx->ctor might be pointing to *valp, so replace
6913 : : its contents. */
6914 : 1838594 : CONSTRUCTOR_ELTS (*valp) = CONSTRUCTOR_ELTS (init);
6915 : 1838594 : TREE_CONSTANT (*valp) = TREE_CONSTANT (init);
6916 : 1838594 : TREE_SIDE_EFFECTS (*valp) = TREE_SIDE_EFFECTS (init);
6917 : 5515782 : CONSTRUCTOR_NO_CLEARING (*valp)
6918 : 1838594 : = CONSTRUCTOR_NO_CLEARING (init);
6919 : 5515782 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp)
6920 : 1838594 : = CONSTRUCTOR_ZERO_PADDING_BITS (init);
6921 : : }
6922 : : else
6923 : 20184731 : *valp = init;
6924 : :
6925 : : /* After initialization, 'const' semantics apply to the value of the
6926 : : object. Make a note of this fact by marking the CONSTRUCTOR
6927 : : TREE_READONLY. */
6928 : 22042766 : if (TREE_CODE (t) == INIT_EXPR
6929 : 15769108 : && !empty_base
6930 : 15749667 : && TREE_CODE (*valp) == CONSTRUCTOR
6931 : 23837407 : && TYPE_READONLY (type))
6932 : : {
6933 : 17555 : if (INDIRECT_REF_P (target)
6934 : 32636 : && (is_this_parameter
6935 : 15081 : (tree_strip_nop_conversions (TREE_OPERAND (target, 0)))))
6936 : : /* We've just initialized '*this' (perhaps via the target
6937 : : constructor of a delegating constructor). Leave it up to the
6938 : : caller that set 'this' to set TREE_READONLY appropriately. */
6939 : 26 : gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
6940 : : (TREE_TYPE (target), type) || empty_base);
6941 : : else
6942 : 17529 : TREE_READONLY (*valp) = true;
6943 : : }
6944 : :
6945 : : /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing
6946 : : CONSTRUCTORs, if any. */
6947 : 22042766 : bool c = TREE_CONSTANT (init);
6948 : 22042766 : bool s = TREE_SIDE_EFFECTS (init);
6949 : 22042766 : if (!indexes->is_empty ())
6950 : : {
6951 : 4735415 : tree last = indexes->last ();
6952 : 4735415 : if (TREE_CODE (last) == REALPART_EXPR
6953 : 4735415 : || TREE_CODE (last) == IMAGPART_EXPR)
6954 : : {
6955 : : /* And canonicalize COMPLEX_EXPR into COMPLEX_CST if
6956 : : possible. */
6957 : 32 : tree *cexpr = ctors.last ();
6958 : 32 : if (tree c = const_binop (COMPLEX_EXPR, TREE_TYPE (*cexpr),
6959 : 32 : TREE_OPERAND (*cexpr, 0),
6960 : 32 : TREE_OPERAND (*cexpr, 1)))
6961 : 31 : *cexpr = c;
6962 : : else
6963 : : {
6964 : 2 : TREE_CONSTANT (*cexpr)
6965 : 1 : = (TREE_CONSTANT (TREE_OPERAND (*cexpr, 0))
6966 : 1 : & TREE_CONSTANT (TREE_OPERAND (*cexpr, 1)));
6967 : 2 : TREE_SIDE_EFFECTS (*cexpr)
6968 : 2 : = (TREE_SIDE_EFFECTS (TREE_OPERAND (*cexpr, 0))
6969 : 1 : | TREE_SIDE_EFFECTS (TREE_OPERAND (*cexpr, 1)));
6970 : : }
6971 : 32 : c = TREE_CONSTANT (*cexpr);
6972 : 32 : s = TREE_SIDE_EFFECTS (*cexpr);
6973 : : }
6974 : : }
6975 : 22042766 : if (!c || s || activated_union_member_p)
6976 : 9016902 : for (tree *elt : ctors)
6977 : : {
6978 : 1294828 : if (TREE_CODE (*elt) != CONSTRUCTOR)
6979 : 0 : continue;
6980 : 1294828 : if (!c)
6981 : 1002738 : TREE_CONSTANT (*elt) = false;
6982 : 1294828 : if (s)
6983 : 0 : TREE_SIDE_EFFECTS (*elt) = true;
6984 : : /* Clear CONSTRUCTOR_NO_CLEARING since we've activated a member of
6985 : : this union. */
6986 : 1294828 : if (TREE_CODE (TREE_TYPE (*elt)) == UNION_TYPE)
6987 : 74050 : CONSTRUCTOR_NO_CLEARING (*elt) = false;
6988 : : }
6989 : :
6990 : 22042766 : if (lval)
6991 : 19338090 : return target;
6992 : : else
6993 : : return init;
6994 : 23124611 : }
6995 : :
6996 : : /* Evaluate a ++ or -- expression. */
6997 : :
6998 : : static tree
6999 : 2171602 : cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t,
7000 : : value_cat lval,
7001 : : bool *non_constant_p, bool *overflow_p)
7002 : : {
7003 : 2171602 : enum tree_code code = TREE_CODE (t);
7004 : 2171602 : tree type = TREE_TYPE (t);
7005 : 2171602 : tree op = TREE_OPERAND (t, 0);
7006 : 2171602 : tree offset = TREE_OPERAND (t, 1);
7007 : 2171602 : gcc_assert (TREE_CONSTANT (offset));
7008 : :
7009 : : /* OFFSET is constant, but perhaps not constant enough. We need to
7010 : : e.g. bash FLOAT_EXPRs to REAL_CSTs. */
7011 : 2171602 : offset = fold_simple (offset);
7012 : :
7013 : : /* The operand as an lvalue. */
7014 : 2171602 : op = cxx_eval_constant_expression (ctx, op, vc_glvalue,
7015 : : non_constant_p, overflow_p);
7016 : :
7017 : : /* The operand as an rvalue. */
7018 : 2171602 : tree val
7019 : 2171602 : = cxx_eval_constant_expression (ctx, op, vc_prvalue,
7020 : : non_constant_p, overflow_p);
7021 : : /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
7022 : : a local array in a constexpr function. */
7023 : 2171602 : bool ptr = INDIRECT_TYPE_P (TREE_TYPE (val));
7024 : 1008646 : if (!ptr)
7025 : 1008646 : VERIFY_CONSTANT (val);
7026 : :
7027 : : /* The modified value. */
7028 : 2136436 : bool inc = (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR);
7029 : 2136436 : tree mod;
7030 : 2136436 : if (INDIRECT_TYPE_P (type))
7031 : : {
7032 : : /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
7033 : 1162956 : offset = convert_to_ptrofftype (offset);
7034 : 1162956 : if (!inc)
7035 : 81208 : offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset);
7036 : 1162956 : mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset);
7037 : : }
7038 : 973480 : else if (c_promoting_integer_type_p (type)
7039 : 1719 : && !TYPE_UNSIGNED (type)
7040 : 973549 : && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
7041 : : {
7042 : 69 : offset = fold_convert (integer_type_node, offset);
7043 : 69 : mod = fold_convert (integer_type_node, val);
7044 : 125 : tree t = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, integer_type_node,
7045 : : mod, offset);
7046 : 69 : mod = fold_convert (type, t);
7047 : 69 : if (TREE_OVERFLOW_P (mod) && !TREE_OVERFLOW_P (t))
7048 : 9 : TREE_OVERFLOW (mod) = false;
7049 : : }
7050 : : else
7051 : 1328675 : mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset);
7052 : 2136436 : if (!ptr)
7053 : 973480 : VERIFY_CONSTANT (mod);
7054 : :
7055 : : /* Storing the modified value. */
7056 : 3402882 : tree store = build2_loc (cp_expr_loc_or_loc (t, input_location),
7057 : : MODIFY_EXPR, type, op, mod);
7058 : 2136436 : mod = cxx_eval_constant_expression (ctx, store, lval,
7059 : : non_constant_p, overflow_p);
7060 : 2136436 : ggc_free (store);
7061 : 2136436 : if (*non_constant_p)
7062 : : return t;
7063 : :
7064 : : /* And the value of the expression. */
7065 : 2099879 : if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
7066 : : /* Prefix ops are lvalues, but the caller might want an rvalue;
7067 : : lval has already been taken into account in the store above. */
7068 : : return mod;
7069 : : else
7070 : : /* Postfix ops are rvalues. */
7071 : 476386 : return val;
7072 : : }
7073 : :
7074 : : /* Predicates for the meaning of *jump_target. */
7075 : :
7076 : : static bool
7077 : 33522366 : returns (tree *jump_target)
7078 : : {
7079 : 33522366 : return *jump_target
7080 : 4556995 : && TREE_CODE (*jump_target) == RETURN_EXPR;
7081 : : }
7082 : :
7083 : : static bool
7084 : 34359683 : breaks (tree *jump_target)
7085 : : {
7086 : 34359683 : return *jump_target
7087 : 34359683 : && ((TREE_CODE (*jump_target) == LABEL_DECL
7088 : 23 : && LABEL_DECL_BREAK (*jump_target))
7089 : 931459 : || TREE_CODE (*jump_target) == BREAK_STMT
7090 : 889728 : || TREE_CODE (*jump_target) == EXIT_EXPR);
7091 : : }
7092 : :
7093 : : static bool
7094 : 49202701 : continues (tree *jump_target)
7095 : : {
7096 : 49202701 : return *jump_target
7097 : 49202701 : && ((TREE_CODE (*jump_target) == LABEL_DECL
7098 : 35 : && LABEL_DECL_CONTINUE (*jump_target))
7099 : 911510 : || TREE_CODE (*jump_target) == CONTINUE_STMT);
7100 : :
7101 : : }
7102 : :
7103 : : static bool
7104 : 6487252 : switches (tree *jump_target)
7105 : : {
7106 : 6487252 : return *jump_target
7107 : 6487251 : && TREE_CODE (*jump_target) == INTEGER_CST;
7108 : : }
7109 : :
7110 : : /* Subroutine of cxx_eval_statement_list. Determine whether the statement
7111 : : STMT matches *jump_target. If we're looking for a case label and we see
7112 : : the default label, note it in ctx->css_state. */
7113 : :
7114 : : static bool
7115 : 333781 : label_matches (const constexpr_ctx *ctx, tree *jump_target, tree stmt)
7116 : : {
7117 : 333781 : switch (TREE_CODE (*jump_target))
7118 : : {
7119 : 15 : case LABEL_DECL:
7120 : 15 : if (TREE_CODE (stmt) == LABEL_EXPR
7121 : 15 : && LABEL_EXPR_LABEL (stmt) == *jump_target)
7122 : : return true;
7123 : : break;
7124 : :
7125 : 331453 : case INTEGER_CST:
7126 : 331453 : if (TREE_CODE (stmt) == CASE_LABEL_EXPR)
7127 : : {
7128 : 331453 : gcc_assert (ctx->css_state != NULL);
7129 : 331453 : if (!CASE_LOW (stmt))
7130 : : {
7131 : : /* default: should appear just once in a SWITCH_EXPR
7132 : : body (excluding nested SWITCH_EXPR). */
7133 : 55008 : gcc_assert (*ctx->css_state != css_default_seen);
7134 : : /* When evaluating SWITCH_EXPR body for the second time,
7135 : : return true for the default: label. */
7136 : 55008 : if (*ctx->css_state == css_default_processing)
7137 : : return true;
7138 : 27516 : *ctx->css_state = css_default_seen;
7139 : : }
7140 : 276445 : else if (CASE_HIGH (stmt))
7141 : : {
7142 : 20 : if (tree_int_cst_le (CASE_LOW (stmt), *jump_target)
7143 : 31 : && tree_int_cst_le (*jump_target, CASE_HIGH (stmt)))
7144 : : return true;
7145 : : }
7146 : 276425 : else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt)))
7147 : : return true;
7148 : : }
7149 : : break;
7150 : :
7151 : : case BREAK_STMT:
7152 : : case CONTINUE_STMT:
7153 : : /* These two are handled directly in cxx_eval_loop_expr by testing
7154 : : breaks (jump_target) or continues (jump_target). */
7155 : : break;
7156 : :
7157 : 0 : default:
7158 : 0 : gcc_unreachable ();
7159 : : }
7160 : : return false;
7161 : : }
7162 : :
7163 : : /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
7164 : : semantics, for switch, break, continue, and return. */
7165 : :
7166 : : static tree
7167 : 17877494 : cxx_eval_statement_list (const constexpr_ctx *ctx, tree t,
7168 : : bool *non_constant_p, bool *overflow_p,
7169 : : tree *jump_target)
7170 : : {
7171 : 17877494 : tree local_target;
7172 : : /* In a statement-expression we want to return the last value.
7173 : : For empty statement expression return void_node. */
7174 : 17877494 : tree r = void_node;
7175 : 17877494 : if (!jump_target)
7176 : : {
7177 : 446 : local_target = NULL_TREE;
7178 : 446 : jump_target = &local_target;
7179 : : }
7180 : 41429071 : for (tree_stmt_iterator i = tsi_start (t); !tsi_end_p (i); ++i)
7181 : : {
7182 : 32607131 : tree stmt = *i;
7183 : :
7184 : : /* We've found a continue, so skip everything until we reach
7185 : : the label its jumping to. */
7186 : 32607131 : if (continues (jump_target))
7187 : : {
7188 : 2328 : if (label_matches (ctx, jump_target, stmt))
7189 : : /* Found it. */
7190 : 3 : *jump_target = NULL_TREE;
7191 : : else
7192 : 2325 : continue;
7193 : : }
7194 : 32604806 : if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT)
7195 : 5840456 : continue;
7196 : :
7197 : 26764350 : value_cat lval = vc_discard;
7198 : : /* The result of a statement-expression is not wrapped in EXPR_STMT. */
7199 : 26764350 : if (tsi_one_before_end_p (i) && TREE_CODE (stmt) != EXPR_STMT)
7200 : : lval = vc_prvalue;
7201 : :
7202 : 26764350 : r = cxx_eval_constant_expression (ctx, stmt, lval,
7203 : : non_constant_p, overflow_p,
7204 : : jump_target);
7205 : 26764350 : if (*non_constant_p)
7206 : : break;
7207 : 21322930 : if (returns (jump_target) || breaks (jump_target))
7208 : : break;
7209 : : }
7210 : 17877494 : if (*jump_target && jump_target == &local_target)
7211 : : {
7212 : : /* We aren't communicating the jump to our caller, so give up. We don't
7213 : : need to support evaluation of jumps out of statement-exprs. */
7214 : 24 : if (!ctx->quiet)
7215 : 3 : error_at (cp_expr_loc_or_input_loc (r),
7216 : : "statement is not a constant expression");
7217 : 24 : *non_constant_p = true;
7218 : : }
7219 : 17877494 : return r;
7220 : : }
7221 : :
7222 : : /* Evaluate a LOOP_EXPR for side-effects. Handles break and return
7223 : : semantics; continue semantics are covered by cxx_eval_statement_list. */
7224 : :
7225 : : static tree
7226 : 730742 : cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t,
7227 : : bool *non_constant_p, bool *overflow_p,
7228 : : tree *jump_target)
7229 : : {
7230 : 730742 : tree local_target;
7231 : 730742 : if (!jump_target)
7232 : : {
7233 : 53 : local_target = NULL_TREE;
7234 : 53 : jump_target = &local_target;
7235 : : }
7236 : :
7237 : 730742 : tree body, cond = NULL_TREE, expr = NULL_TREE;
7238 : 730742 : tree cond_prep = NULL_TREE, cond_cleanup = NULL_TREE;
7239 : 730742 : unsigned cond_cleanup_depth = 0;
7240 : 730742 : int count = 0;
7241 : 730742 : switch (TREE_CODE (t))
7242 : : {
7243 : 53 : case LOOP_EXPR:
7244 : 53 : body = LOOP_EXPR_BODY (t);
7245 : 53 : break;
7246 : 581380 : case DO_STMT:
7247 : 581380 : body = DO_BODY (t);
7248 : 581380 : cond = DO_COND (t);
7249 : 581380 : break;
7250 : 63045 : case WHILE_STMT:
7251 : 63045 : body = WHILE_BODY (t);
7252 : 63045 : cond = WHILE_COND (t);
7253 : 63045 : cond_prep = WHILE_COND_PREP (t);
7254 : 63045 : cond_cleanup = WHILE_COND_CLEANUP (t);
7255 : 63045 : count = -1;
7256 : 63045 : break;
7257 : 86264 : case FOR_STMT:
7258 : 86264 : if (FOR_INIT_STMT (t))
7259 : 0 : cxx_eval_constant_expression (ctx, FOR_INIT_STMT (t), vc_discard,
7260 : : non_constant_p, overflow_p, jump_target);
7261 : 86264 : if (*non_constant_p)
7262 : : return NULL_TREE;
7263 : 86264 : body = FOR_BODY (t);
7264 : 86264 : cond = FOR_COND (t);
7265 : 86264 : expr = FOR_EXPR (t);
7266 : 86264 : cond_prep = FOR_COND_PREP (t);
7267 : 86264 : cond_cleanup = FOR_COND_CLEANUP (t);
7268 : 86264 : count = -1;
7269 : 86264 : break;
7270 : 0 : default:
7271 : 0 : gcc_unreachable ();
7272 : : }
7273 : 730742 : if (cond_prep)
7274 : 12 : gcc_assert (TREE_CODE (cond_prep) == BIND_EXPR);
7275 : 8482407 : auto cleanup_cond = [&] {
7276 : : /* Clean up the condition variable after each iteration. */
7277 : 7751665 : if (cond_cleanup_depth && !*non_constant_p)
7278 : : {
7279 : 105 : auto_vec<tree, 4> cleanups (cond_cleanup_depth);
7280 : 105 : tree s = BIND_EXPR_BODY (cond_prep);
7281 : 105 : unsigned i;
7282 : 210 : for (i = cond_cleanup_depth; i; --i)
7283 : : {
7284 : 105 : tree_stmt_iterator iter = tsi_last (s);
7285 : 105 : s = tsi_stmt (iter);
7286 : 105 : cleanups.quick_push (CLEANUP_EXPR (s));
7287 : 105 : s = CLEANUP_BODY (s);
7288 : : }
7289 : 105 : tree c;
7290 : 420 : FOR_EACH_VEC_ELT_REVERSE (cleanups, i, c)
7291 : 105 : cxx_eval_constant_expression (ctx, c, vc_discard, non_constant_p,
7292 : : overflow_p);
7293 : 105 : }
7294 : 7751665 : if (cond_prep)
7295 : 111 : for (tree decl = BIND_EXPR_VARS (cond_prep);
7296 : 222 : decl; decl = DECL_CHAIN (decl))
7297 : 111 : destroy_value_checked (ctx, decl, non_constant_p);
7298 : 730742 : };
7299 : 7170775 : do
7300 : : {
7301 : 7170775 : if (count != -1)
7302 : : {
7303 : 7021466 : if (body)
7304 : 7021466 : cxx_eval_constant_expression (ctx, body, vc_discard,
7305 : : non_constant_p, overflow_p,
7306 : : jump_target);
7307 : 7021466 : if (breaks (jump_target))
7308 : : {
7309 : 543 : *jump_target = NULL_TREE;
7310 : 543 : break;
7311 : : }
7312 : :
7313 : 7020923 : if (TREE_CODE (t) != LOOP_EXPR && continues (jump_target))
7314 : 652 : *jump_target = NULL_TREE;
7315 : :
7316 : 7020923 : if (expr)
7317 : 1091150 : cxx_eval_constant_expression (ctx, expr, vc_prvalue,
7318 : : non_constant_p, overflow_p,
7319 : : jump_target);
7320 : 7020923 : cleanup_cond ();
7321 : : }
7322 : :
7323 : 7170232 : if (cond_prep)
7324 : : {
7325 : 111 : for (tree decl = BIND_EXPR_VARS (cond_prep);
7326 : 222 : decl; decl = DECL_CHAIN (decl))
7327 : 111 : ctx->global->clear_value (decl);
7328 : 111 : if (cond_cleanup)
7329 : : {
7330 : : /* If COND_CLEANUP is non-NULL, we need to evaluate DEPTH
7331 : : nested STATEMENT_LISTs from inside of BIND_EXPR_BODY,
7332 : : but defer the evaluation of CLEANUP_EXPRs of CLEANUP_STMT
7333 : : at the end of those STATEMENT_LISTs. */
7334 : 111 : cond_cleanup_depth = 0;
7335 : 111 : tree s = BIND_EXPR_BODY (cond_prep);
7336 : 111 : for (unsigned depth = tree_to_uhwi (cond_cleanup);
7337 : 222 : depth; --depth)
7338 : : {
7339 : 321 : for (tree_stmt_iterator i = tsi_start (s);
7340 : 321 : !tsi_end_p (i); ++i)
7341 : : {
7342 : 321 : tree stmt = *i;
7343 : 321 : if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT)
7344 : 0 : continue;
7345 : 321 : if (tsi_one_before_end_p (i))
7346 : : {
7347 : : /* The last statement in the STATEMENT_LIST
7348 : : has to be a CLEANUP_STMT (verified in
7349 : : finish_loop_cond_prep). We want to
7350 : : evaluate just its CLEANUP_BODY part but not
7351 : : CLEANUP_EXPR part just yet. */
7352 : 105 : gcc_assert (TREE_CODE (stmt) == CLEANUP_STMT);
7353 : : /* If the CLEANUP_STMT is not actually to be
7354 : : evaluated, don't increment cond_cleanup_depth
7355 : : so that we don't evaluate the CLEANUP_EXPR
7356 : : for it later either. */
7357 : 105 : if (*jump_target)
7358 : : {
7359 : : depth = 1;
7360 : : break;
7361 : : }
7362 : 105 : ++cond_cleanup_depth;
7363 : : /* If not in the innermost one, next iteration
7364 : : will handle CLEANUP_BODY similarly. */
7365 : 105 : if (depth > 1)
7366 : : {
7367 : 0 : s = CLEANUP_BODY (stmt);
7368 : 0 : break;
7369 : : }
7370 : : /* The innermost one can be evaluated normally. */
7371 : 105 : cxx_eval_constant_expression (ctx,
7372 : 105 : CLEANUP_BODY (stmt),
7373 : : vc_discard,
7374 : : non_constant_p,
7375 : : overflow_p,
7376 : : jump_target);
7377 : 105 : break;
7378 : : }
7379 : : /* And so should be evaluated statements which aren't
7380 : : last in the STATEMENT_LIST. */
7381 : 216 : cxx_eval_constant_expression (ctx, stmt, vc_discard,
7382 : : non_constant_p, overflow_p,
7383 : : jump_target);
7384 : 216 : if (*non_constant_p
7385 : 111 : || returns (jump_target)
7386 : 210 : || breaks (jump_target)
7387 : 426 : || continues (jump_target))
7388 : : {
7389 : : depth = 1;
7390 : : break;
7391 : : }
7392 : : }
7393 : : }
7394 : : }
7395 : : else
7396 : 0 : cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (cond_prep),
7397 : : vc_discard, non_constant_p,
7398 : : overflow_p, jump_target);
7399 : : }
7400 : :
7401 : 7170232 : if (cond)
7402 : : {
7403 : 7169265 : tree res
7404 : 7169265 : = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
7405 : : non_constant_p, overflow_p,
7406 : : jump_target);
7407 : 7169265 : if (res)
7408 : : {
7409 : 7138248 : if (verify_constant (res, ctx->quiet, non_constant_p,
7410 : : overflow_p))
7411 : : break;
7412 : 6938818 : if (integer_zerop (res))
7413 : : break;
7414 : : }
7415 : : else
7416 : 31017 : gcc_assert (*jump_target);
7417 : : }
7418 : :
7419 : 6471321 : if (++count >= constexpr_loop_limit)
7420 : : {
7421 : 28 : if (!ctx->quiet)
7422 : 9 : error_at (cp_expr_loc_or_input_loc (t),
7423 : : "%<constexpr%> loop iteration count exceeds limit of %d "
7424 : : "(use %<-fconstexpr-loop-limit=%> to increase the limit)",
7425 : : constexpr_loop_limit);
7426 : 28 : *non_constant_p = true;
7427 : 28 : break;
7428 : : }
7429 : : }
7430 : 12911474 : while (!returns (jump_target)
7431 : 6440181 : && !breaks (jump_target)
7432 : 6440181 : && !continues (jump_target)
7433 : 93 : && (!switches (jump_target) || count == 0)
7434 : 12942704 : && !*non_constant_p);
7435 : :
7436 : 730742 : cleanup_cond ();
7437 : :
7438 : 730742 : return NULL_TREE;
7439 : : }
7440 : :
7441 : : /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
7442 : : semantics. */
7443 : :
7444 : : static tree
7445 : 33930 : cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t,
7446 : : bool *non_constant_p, bool *overflow_p,
7447 : : tree *jump_target)
7448 : : {
7449 : 33930 : tree cond
7450 : 33930 : = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_COND (t) : SWITCH_COND (t);
7451 : 33930 : cond = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
7452 : : non_constant_p, overflow_p);
7453 : 33930 : VERIFY_CONSTANT (cond);
7454 : 33650 : if (TREE_CODE (cond) != INTEGER_CST)
7455 : : {
7456 : : /* If the condition doesn't reduce to an INTEGER_CST it isn't a usable
7457 : : switch condition even if it's constant enough for other things
7458 : : (c++/113545). */
7459 : 0 : gcc_checking_assert (ctx->quiet);
7460 : 0 : *non_constant_p = true;
7461 : 0 : return t;
7462 : : }
7463 : :
7464 : 33650 : *jump_target = cond;
7465 : :
7466 : 33650 : tree body
7467 : 33650 : = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_BODY (t) : SWITCH_BODY (t);
7468 : 33650 : constexpr_ctx new_ctx = *ctx;
7469 : 33650 : constexpr_switch_state css = css_default_not_seen;
7470 : 33650 : new_ctx.css_state = &css;
7471 : 33650 : cxx_eval_constant_expression (&new_ctx, body, vc_discard,
7472 : : non_constant_p, overflow_p, jump_target);
7473 : 61186 : if (switches (jump_target) && css == css_default_seen)
7474 : : {
7475 : : /* If the SWITCH_EXPR body has default: label, process it once again,
7476 : : this time instructing label_matches to return true for default:
7477 : : label on switches (jump_target). */
7478 : 27492 : css = css_default_processing;
7479 : 27492 : cxx_eval_constant_expression (&new_ctx, body, vc_discard,
7480 : : non_constant_p, overflow_p, jump_target);
7481 : : }
7482 : 33650 : if (breaks (jump_target) || switches (jump_target))
7483 : 20273 : *jump_target = NULL_TREE;
7484 : : return NULL_TREE;
7485 : : }
7486 : :
7487 : : /* Find the object of TYPE under initialization in CTX. */
7488 : :
7489 : : static tree
7490 : 16623 : lookup_placeholder (const constexpr_ctx *ctx, value_cat lval, tree type)
7491 : : {
7492 : 16623 : if (!ctx)
7493 : : return NULL_TREE;
7494 : :
7495 : : /* Prefer the outermost matching object, but don't cross
7496 : : CONSTRUCTOR_PLACEHOLDER_BOUNDARY constructors. */
7497 : 16291 : if (ctx->ctor && !CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor))
7498 : 510 : if (tree outer_ob = lookup_placeholder (ctx->parent, lval, type))
7499 : : return outer_ob;
7500 : :
7501 : : /* We could use ctx->object unconditionally, but using ctx->ctor when we
7502 : : can is a minor optimization. */
7503 : 16113 : if (!lval && ctx->ctor && same_type_p (TREE_TYPE (ctx->ctor), type))
7504 : 300 : return ctx->ctor;
7505 : :
7506 : 15813 : if (!ctx->object)
7507 : : return NULL_TREE;
7508 : :
7509 : : /* Since an object cannot have a field of its own type, we can search outward
7510 : : from ctx->object to find the unique containing object of TYPE. */
7511 : : tree ob = ctx->object;
7512 : 15804 : while (ob)
7513 : : {
7514 : 15804 : if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob), type))
7515 : : break;
7516 : 223 : if (handled_component_p (ob))
7517 : 221 : ob = TREE_OPERAND (ob, 0);
7518 : : else
7519 : : ob = NULL_TREE;
7520 : : }
7521 : :
7522 : : return ob;
7523 : : }
7524 : :
7525 : : /* Complain about an attempt to evaluate inline assembly. If FUNDEF_P is
7526 : : true, we're checking a constexpr function body. */
7527 : :
7528 : : static void
7529 : 31 : inline_asm_in_constexpr_error (location_t loc, bool fundef_p)
7530 : : {
7531 : 31 : auto_diagnostic_group d;
7532 : 31 : if (constexpr_error (loc, fundef_p, "inline assembly is not a "
7533 : : "constant expression"))
7534 : 31 : inform (loc, "only unevaluated inline assembly is allowed in a "
7535 : : "%<constexpr%> function in C++20");
7536 : 31 : }
7537 : :
7538 : : /* We're getting the constant value of DECL in a manifestly constant-evaluated
7539 : : context; maybe complain about that. */
7540 : :
7541 : : static void
7542 : 53196456 : maybe_warn_about_constant_value (location_t loc, tree decl)
7543 : : {
7544 : 53196456 : static bool explained = false;
7545 : 53196456 : if (cxx_dialect >= cxx17
7546 : 53050471 : && warn_interference_size
7547 : 53050471 : && !OPTION_SET_P (param_destruct_interfere_size)
7548 : 53050471 : && DECL_CONTEXT (decl) == std_node
7549 : 6625749 : && DECL_NAME (decl)
7550 : 6625743 : && id_equal (DECL_NAME (decl), "hardware_destructive_interference_size")
7551 : 12 : && (LOCATION_FILE (input_location) != main_input_filename
7552 : 6 : || module_exporting_p ())
7553 : 53196459 : && warning_at (loc, OPT_Winterference_size, "use of %qD", decl)
7554 : 53196465 : && !explained)
7555 : : {
7556 : 6 : explained = true;
7557 : 6 : inform (loc, "its value can vary between compiler versions or "
7558 : : "with different %<-mtune%> or %<-mcpu%> flags");
7559 : 6 : inform (loc, "if this use is part of a public ABI, change it to "
7560 : : "instead use a constant variable you define");
7561 : 6 : inform (loc, "the default value for the current CPU tuning "
7562 : : "is %d bytes", param_destruct_interfere_size);
7563 : 6 : inform (loc, "you can stabilize this value with %<--param "
7564 : : "hardware_destructive_interference_size=%d%>, or disable "
7565 : : "this warning with %<-Wno-interference-size%>",
7566 : : param_destruct_interfere_size);
7567 : : }
7568 : 53196456 : }
7569 : :
7570 : : /* For element type ELT_TYPE, return the appropriate type of the heap object
7571 : : containing such element(s). COOKIE_SIZE is NULL or the size of cookie
7572 : : in bytes. If COOKIE_SIZE is NULL, return array type
7573 : : ELT_TYPE[FULL_SIZE / sizeof(ELT_TYPE)], otherwise return
7574 : : struct { size_t[COOKIE_SIZE/sizeof(size_t)]; ELT_TYPE[N]; }
7575 : : where N is computed such that the size of the struct fits into FULL_SIZE.
7576 : : If ARG_SIZE is non-NULL, it is the first argument to the new operator.
7577 : : It should be passed if ELT_TYPE is zero sized type in which case FULL_SIZE
7578 : : will be also 0 and so it is not possible to determine the actual array
7579 : : size. CTX, NON_CONSTANT_P and OVERFLOW_P are used during constant
7580 : : expression evaluation of subexpressions of ARG_SIZE. */
7581 : :
7582 : : static tree
7583 : 1888 : build_new_constexpr_heap_type (const constexpr_ctx *ctx, tree elt_type,
7584 : : tree cookie_size, tree full_size, tree arg_size,
7585 : : bool *non_constant_p, bool *overflow_p)
7586 : : {
7587 : 1888 : gcc_assert (cookie_size == NULL_TREE || tree_fits_uhwi_p (cookie_size));
7588 : 1888 : gcc_assert (tree_fits_uhwi_p (full_size));
7589 : 1888 : unsigned HOST_WIDE_INT csz = cookie_size ? tree_to_uhwi (cookie_size) : 0;
7590 : 1888 : if (arg_size)
7591 : : {
7592 : 9 : STRIP_NOPS (arg_size);
7593 : 9 : if (cookie_size)
7594 : : {
7595 : 0 : if (TREE_CODE (arg_size) != PLUS_EXPR)
7596 : : arg_size = NULL_TREE;
7597 : 0 : else if (TREE_CODE (TREE_OPERAND (arg_size, 0)) == INTEGER_CST
7598 : 0 : && tree_int_cst_equal (cookie_size,
7599 : 0 : TREE_OPERAND (arg_size, 0)))
7600 : : {
7601 : 0 : arg_size = TREE_OPERAND (arg_size, 1);
7602 : 0 : STRIP_NOPS (arg_size);
7603 : : }
7604 : 0 : else if (TREE_CODE (TREE_OPERAND (arg_size, 1)) == INTEGER_CST
7605 : 0 : && tree_int_cst_equal (cookie_size,
7606 : 0 : TREE_OPERAND (arg_size, 1)))
7607 : : {
7608 : 0 : arg_size = TREE_OPERAND (arg_size, 0);
7609 : 0 : STRIP_NOPS (arg_size);
7610 : : }
7611 : : else
7612 : : arg_size = NULL_TREE;
7613 : : }
7614 : 9 : if (arg_size && TREE_CODE (arg_size) == MULT_EXPR)
7615 : : {
7616 : 9 : tree op0 = TREE_OPERAND (arg_size, 0);
7617 : 9 : tree op1 = TREE_OPERAND (arg_size, 1);
7618 : 9 : if (integer_zerop (op0))
7619 : 9 : arg_size
7620 : 9 : = cxx_eval_constant_expression (ctx, op1, vc_prvalue,
7621 : : non_constant_p, overflow_p);
7622 : 0 : else if (integer_zerop (op1))
7623 : 0 : arg_size
7624 : 0 : = cxx_eval_constant_expression (ctx, op0, vc_prvalue,
7625 : : non_constant_p, overflow_p);
7626 : : else
7627 : : arg_size = NULL_TREE;
7628 : : }
7629 : : else
7630 : : arg_size = NULL_TREE;
7631 : : }
7632 : :
7633 : 9 : unsigned HOST_WIDE_INT fsz = tree_to_uhwi (arg_size ? arg_size : full_size);
7634 : 1888 : if (!arg_size)
7635 : : {
7636 : 1879 : unsigned HOST_WIDE_INT esz = int_size_in_bytes (elt_type);
7637 : 1879 : gcc_assert (fsz >= csz);
7638 : 1879 : fsz -= csz;
7639 : 1879 : if (esz)
7640 : 1879 : fsz /= esz;
7641 : : }
7642 : 1888 : tree itype2 = build_index_type (size_int (fsz - 1));
7643 : 1888 : if (!cookie_size)
7644 : 1885 : return build_cplus_array_type (elt_type, itype2);
7645 : 3 : return build_new_constexpr_heap_type (elt_type, cookie_size, itype2);
7646 : : }
7647 : :
7648 : : /* Attempt to reduce the expression T to a constant value.
7649 : : On failure, issue diagnostic and return error_mark_node. */
7650 : : /* FIXME unify with c_fully_fold */
7651 : : /* FIXME overflow_p is too global */
7652 : :
7653 : : static tree
7654 : 1117919627 : cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
7655 : : value_cat lval,
7656 : : bool *non_constant_p, bool *overflow_p,
7657 : : tree *jump_target /* = NULL */)
7658 : : {
7659 : 1117919627 : if (jump_target && *jump_target)
7660 : : {
7661 : : /* If we are jumping, ignore all statements/expressions except those
7662 : : that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */
7663 : 1252613 : switch (TREE_CODE (t))
7664 : : {
7665 : : case BIND_EXPR:
7666 : : case STATEMENT_LIST:
7667 : : case LOOP_EXPR:
7668 : : case COND_EXPR:
7669 : : case IF_STMT:
7670 : : case DO_STMT:
7671 : : case WHILE_STMT:
7672 : : case FOR_STMT:
7673 : : break;
7674 : 331453 : case LABEL_EXPR:
7675 : 331453 : case CASE_LABEL_EXPR:
7676 : 331453 : if (label_matches (ctx, jump_target, t))
7677 : : /* Found it. */
7678 : 33606 : *jump_target = NULL_TREE;
7679 : 331453 : return NULL_TREE;
7680 : : default:
7681 : : return NULL_TREE;
7682 : : }
7683 : : }
7684 : 1116881391 : if (error_operand_p (t))
7685 : : {
7686 : 133 : *non_constant_p = true;
7687 : 133 : return t;
7688 : : }
7689 : :
7690 : : /* Change the input location to the currently processed expression for
7691 : : better error messages when a subexpression has no location. */
7692 : 1116881258 : location_t loc = cp_expr_loc_or_input_loc (t);
7693 : 2233757122 : iloc_sentinel sentinel (loc);
7694 : :
7695 : 1116881258 : STRIP_ANY_LOCATION_WRAPPER (t);
7696 : :
7697 : 1116881258 : if (CONSTANT_CLASS_P (t))
7698 : : {
7699 : 244060816 : if (TREE_OVERFLOW (t))
7700 : : {
7701 : 62 : if (!ctx->quiet)
7702 : 15 : permerror (input_location, "overflow in constant expression");
7703 : 62 : if (!flag_permissive || ctx->quiet)
7704 : 59 : *overflow_p = true;
7705 : : }
7706 : :
7707 : 244060816 : if (TREE_CODE (t) == INTEGER_CST
7708 : 235896640 : && TYPE_PTR_P (TREE_TYPE (t))
7709 : : /* INTEGER_CST with pointer-to-method type is only used
7710 : : for a virtual method in a pointer to member function.
7711 : : Don't reject those. */
7712 : 697332 : && TREE_CODE (TREE_TYPE (TREE_TYPE (t))) != METHOD_TYPE
7713 : 244757846 : && !integer_zerop (t))
7714 : : {
7715 : 6 : if (!ctx->quiet)
7716 : 0 : error ("value %qE of type %qT is not a constant expression",
7717 : 0 : t, TREE_TYPE (t));
7718 : 6 : *non_constant_p = true;
7719 : : }
7720 : :
7721 : 244060816 : return t;
7722 : : }
7723 : :
7724 : : /* Avoid excessively long constexpr evaluations. */
7725 : 872820442 : if (++ctx->global->constexpr_ops_count >= constexpr_ops_limit)
7726 : : {
7727 : 9 : if (!ctx->quiet)
7728 : 3 : error_at (loc,
7729 : : "%<constexpr%> evaluation operation count exceeds limit of "
7730 : : "%wd (use %<-fconstexpr-ops-limit=%> to increase the limit)",
7731 : : constexpr_ops_limit);
7732 : 9 : ctx->global->constexpr_ops_count = INTTYPE_MINIMUM (HOST_WIDE_INT);
7733 : 9 : *non_constant_p = true;
7734 : 9 : return t;
7735 : : }
7736 : :
7737 : 872820433 : constexpr_ctx new_ctx;
7738 : 872820433 : tree r = t;
7739 : :
7740 : 872820433 : tree_code tcode = TREE_CODE (t);
7741 : 872820433 : switch (tcode)
7742 : : {
7743 : 13051905 : case RESULT_DECL:
7744 : 13051905 : if (lval)
7745 : : return t;
7746 : : /* We ask for an rvalue for the RESULT_DECL when indirecting
7747 : : through an invisible reference, or in named return value
7748 : : optimization. */
7749 : 29608 : if (tree v = ctx->global->get_value (t))
7750 : : return v;
7751 : : else
7752 : : {
7753 : 3 : if (!ctx->quiet)
7754 : 0 : error ("%qE is not a constant expression", t);
7755 : 3 : *non_constant_p = true;
7756 : : }
7757 : 3 : break;
7758 : :
7759 : 123014990 : case VAR_DECL:
7760 : 123014990 : if (DECL_HAS_VALUE_EXPR_P (t))
7761 : : {
7762 : 1115383 : if (is_normal_capture_proxy (t)
7763 : 1115383 : && current_function_decl == DECL_CONTEXT (t))
7764 : : {
7765 : : /* Function parms aren't constexpr within the function
7766 : : definition, so don't try to look at the closure. But if the
7767 : : captured variable is constant, try to evaluate it directly. */
7768 : 370322 : r = DECL_CAPTURED_VARIABLE (t);
7769 : : }
7770 : : else
7771 : 745061 : r = DECL_VALUE_EXPR (t);
7772 : :
7773 : 1115383 : tree type = TREE_TYPE (t);
7774 : 1115383 : if (TYPE_REF_P (type) != TYPE_REF_P (TREE_TYPE (r)))
7775 : : {
7776 : : /* Adjust r to match the reference-ness of t. */
7777 : 234907 : if (TYPE_REF_P (type))
7778 : 234904 : r = build_address (r);
7779 : : else
7780 : 3 : r = convert_from_reference (r);
7781 : : }
7782 : 1115383 : return cxx_eval_constant_expression (ctx, r, lval, non_constant_p,
7783 : 1115383 : overflow_p);
7784 : : }
7785 : : /* fall through */
7786 : 129194811 : case CONST_DECL:
7787 : : /* We used to not check lval for CONST_DECL, but darwin.cc uses
7788 : : CONST_DECL for aggregate constants. */
7789 : 129194811 : if (lval)
7790 : : return t;
7791 : 102349040 : else if (t == ctx->object)
7792 : 500866 : return ctx->ctor;
7793 : 101848174 : if (VAR_P (t))
7794 : : {
7795 : 94552970 : if (tree v = ctx->global->get_value (t))
7796 : : {
7797 : : r = v;
7798 : : break;
7799 : : }
7800 : 83364130 : if (ctx->global->is_outside_lifetime (t))
7801 : : {
7802 : 96 : if (!ctx->quiet)
7803 : 28 : outside_lifetime_error (loc, t);
7804 : 96 : *non_constant_p = true;
7805 : 96 : break;
7806 : : }
7807 : : }
7808 : 90659238 : if (ctx->manifestly_const_eval == mce_true)
7809 : 53196456 : maybe_warn_about_constant_value (loc, t);
7810 : 90659238 : if (COMPLETE_TYPE_P (TREE_TYPE (t))
7811 : 90659238 : && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
7812 : : {
7813 : : /* If the class is empty, we aren't actually loading anything. */
7814 : 78888 : r = build_constructor (TREE_TYPE (t), NULL);
7815 : 78888 : TREE_CONSTANT (r) = true;
7816 : : }
7817 : 90580350 : else if (ctx->strict)
7818 : 90312176 : r = decl_really_constant_value (t, /*unshare_p=*/false);
7819 : : else
7820 : 268174 : r = decl_constant_value (t, /*unshare_p=*/false);
7821 : 90656541 : if (TREE_CODE (r) == TARGET_EXPR
7822 : 90656541 : && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR)
7823 : 0 : r = TARGET_EXPR_INITIAL (r);
7824 : 90656541 : if (DECL_P (r)
7825 : : /* P2280 allows references to unknown. */
7826 : 90794963 : && !(p2280_active_p (ctx) && VAR_P (t) && TYPE_REF_P (TREE_TYPE (t))))
7827 : : {
7828 : 27419695 : if (!ctx->quiet)
7829 : 146 : non_const_var_error (loc, r, /*fundef_p*/false);
7830 : 27419695 : *non_constant_p = true;
7831 : : }
7832 : : break;
7833 : :
7834 : : case DEBUG_BEGIN_STMT:
7835 : : /* ??? It might be nice to retain this information somehow, so
7836 : : as to be able to step into a constexpr function call. */
7837 : : /* Fall through. */
7838 : :
7839 : : case FUNCTION_DECL:
7840 : : case TEMPLATE_DECL:
7841 : : case LABEL_DECL:
7842 : : case LABEL_EXPR:
7843 : : case CASE_LABEL_EXPR:
7844 : : case PREDICT_EXPR:
7845 : : case OMP_DECLARE_MAPPER:
7846 : : return t;
7847 : :
7848 : 100619422 : case PARM_DECL:
7849 : 100619422 : if (lval && !TYPE_REF_P (TREE_TYPE (t)))
7850 : : {
7851 : : /* glvalue use. */
7852 : 5078955 : if (TREE_ADDRESSABLE (TREE_TYPE (t)))
7853 : 82775 : if (tree v = ctx->global->get_value (t))
7854 : 692749036 : r = v;
7855 : : }
7856 : 95540467 : else if (tree v = ctx->global->get_value (t))
7857 : : {
7858 : 32114679 : r = v;
7859 : 32114679 : if (TREE_ADDRESSABLE (TREE_TYPE (t)))
7860 : 4735 : r = cxx_eval_constant_expression (ctx, r, vc_prvalue,
7861 : : non_constant_p, overflow_p);
7862 : : }
7863 : 63425788 : else if (lval)
7864 : : /* Defer in case this is only used for its type. */;
7865 : 63425788 : else if (ctx->global->is_outside_lifetime (t))
7866 : : {
7867 : 18 : if (!ctx->quiet)
7868 : 6 : outside_lifetime_error (loc, t);
7869 : 18 : *non_constant_p = true;
7870 : 18 : break;
7871 : : }
7872 : 63425770 : else if (COMPLETE_TYPE_P (TREE_TYPE (t))
7873 : 63425770 : && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
7874 : : {
7875 : : /* If the class is empty, we aren't actually loading anything. */
7876 : 8180 : r = build_constructor (TREE_TYPE (t), NULL);
7877 : 8180 : TREE_CONSTANT (r) = true;
7878 : : }
7879 : 63417590 : else if (p2280_active_p (ctx) && TYPE_REF_P (TREE_TYPE (t)))
7880 : : /* P2280 allows references to unknown... */;
7881 : 63359514 : else if (p2280_active_p (ctx) && is_this_parameter (t))
7882 : : /* ...as well as the this pointer. */;
7883 : : else
7884 : : {
7885 : 63209959 : if (!ctx->quiet)
7886 : 194 : error ("%qE is not a constant expression", t);
7887 : 63209959 : *non_constant_p = true;
7888 : : }
7889 : : break;
7890 : :
7891 : 81677959 : case CALL_EXPR:
7892 : 81677959 : case AGGR_INIT_EXPR:
7893 : 81677959 : r = cxx_eval_call_expression (ctx, t, lval,
7894 : : non_constant_p, overflow_p);
7895 : 81677959 : break;
7896 : :
7897 : 3710788 : case DECL_EXPR:
7898 : 3710788 : {
7899 : 3710788 : r = DECL_EXPR_DECL (t);
7900 : 3710788 : if (TREE_CODE (r) == USING_DECL)
7901 : : {
7902 : 2283 : r = void_node;
7903 : 2283 : break;
7904 : : }
7905 : :
7906 : 3708505 : if (VAR_P (r)
7907 : 3708505 : && (TREE_STATIC (r)
7908 : 3691173 : || (CP_DECL_THREAD_LOCAL_P (r) && !DECL_REALLY_EXTERN (r)))
7909 : : /* Allow __FUNCTION__ etc. */
7910 : 17332 : && !DECL_ARTIFICIAL (r)
7911 : 3708520 : && !decl_constant_var_p (r))
7912 : : {
7913 : 7 : if (!ctx->quiet)
7914 : : {
7915 : 2 : if (CP_DECL_THREAD_LOCAL_P (r))
7916 : 1 : error_at (loc, "control passes through definition of %qD "
7917 : : "with thread storage duration", r);
7918 : : else
7919 : 1 : error_at (loc, "control passes through definition of %qD "
7920 : : "with static storage duration", r);
7921 : : }
7922 : 7 : *non_constant_p = true;
7923 : 7 : break;
7924 : : }
7925 : :
7926 : : /* make_rtl_for_nonlocal_decl could have deferred emission of
7927 : : a local static var, but if it appears in a statement expression
7928 : : which is constant expression evaluated to e.g. just the address
7929 : : of the variable, its DECL_EXPR will never be seen during
7930 : : gimple lowering's record_vars_into as the statement expression
7931 : : will not be in the IL at all. */
7932 : 3708498 : if (VAR_P (r)
7933 : 3708498 : && TREE_STATIC (r)
7934 : 17325 : && !DECL_REALLY_EXTERN (r)
7935 : 17325 : && DECL_FUNCTION_SCOPE_P (r)
7936 : 17325 : && !var_in_maybe_constexpr_fn (r)
7937 : 3708501 : && decl_constant_var_p (r))
7938 : : {
7939 : 3 : varpool_node *node = varpool_node::get (r);
7940 : 3 : if (node == NULL || !node->definition)
7941 : 3 : rest_of_decl_compilation (r, 0, at_eof);
7942 : : }
7943 : :
7944 : 7391105 : if (AGGREGATE_TYPE_P (TREE_TYPE (r))
7945 : 7159370 : || VECTOR_TYPE_P (TREE_TYPE (r)))
7946 : : {
7947 : 257832 : new_ctx = *ctx;
7948 : 257832 : new_ctx.object = r;
7949 : 257832 : new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL);
7950 : 257832 : CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
7951 : 257832 : ctx->global->put_value (r, new_ctx.ctor);
7952 : 257832 : ctx = &new_ctx;
7953 : : }
7954 : :
7955 : 3708498 : if (tree init = DECL_INITIAL (r))
7956 : : {
7957 : 1817537 : init = cxx_eval_constant_expression (ctx, init, vc_prvalue,
7958 : : non_constant_p, overflow_p);
7959 : : /* Don't share a CONSTRUCTOR that might be changed. */
7960 : 1817537 : init = unshare_constructor (init);
7961 : : /* Remember that a constant object's constructor has already
7962 : : run. */
7963 : 3635074 : if (CLASS_TYPE_P (TREE_TYPE (r))
7964 : 1899929 : && CP_TYPE_CONST_P (TREE_TYPE (r)))
7965 : 1345 : TREE_READONLY (init) = true;
7966 : 1817537 : ctx->global->put_value (r, init);
7967 : : }
7968 : 1890961 : else if (ctx == &new_ctx)
7969 : : /* We gave it a CONSTRUCTOR above. */;
7970 : : else
7971 : 1724110 : ctx->global->put_value (r, NULL_TREE);
7972 : : }
7973 : : break;
7974 : :
7975 : 8597657 : case TARGET_EXPR:
7976 : 8597657 : {
7977 : 8597657 : tree type = TREE_TYPE (t);
7978 : :
7979 : 8597657 : if (!literal_type_p (type))
7980 : : {
7981 : 11084 : if (!ctx->quiet)
7982 : : {
7983 : 0 : auto_diagnostic_group d;
7984 : 0 : error ("temporary of non-literal type %qT in a "
7985 : : "constant expression", type);
7986 : 0 : explain_non_literal_class (type);
7987 : 0 : }
7988 : 11084 : *non_constant_p = true;
7989 : 3216697 : break;
7990 : : }
7991 : 8586573 : gcc_checking_assert (!TARGET_EXPR_DIRECT_INIT_P (t));
7992 : : /* Avoid evaluating a TARGET_EXPR more than once. */
7993 : 8586573 : tree slot = TARGET_EXPR_SLOT (t);
7994 : 8586573 : if (tree v = ctx->global->get_value (slot))
7995 : : {
7996 : 1748 : if (lval)
7997 : 4610846 : return slot;
7998 : : r = v;
7999 : : break;
8000 : : }
8001 : 8584825 : if ((AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type)))
8002 : : {
8003 : : /* We're being expanded without an explicit target, so start
8004 : : initializing a new object; expansion with an explicit target
8005 : : strips the TARGET_EXPR before we get here. */
8006 : 6911078 : new_ctx = *ctx;
8007 : : /* Link CTX to NEW_CTX so that lookup_placeholder can resolve
8008 : : any PLACEHOLDER_EXPR within the initializer that refers to the
8009 : : former object under construction. */
8010 : 6911078 : new_ctx.parent = ctx;
8011 : 6911078 : new_ctx.ctor = build_constructor (type, NULL);
8012 : 6911078 : CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
8013 : 6911078 : new_ctx.object = slot;
8014 : 6911078 : ctx->global->put_value (new_ctx.object, new_ctx.ctor);
8015 : 6911078 : ctx = &new_ctx;
8016 : : }
8017 : : /* Pass vc_prvalue because this indicates
8018 : : initialization of a temporary. */
8019 : 8584825 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), vc_prvalue,
8020 : : non_constant_p, overflow_p);
8021 : 8584825 : if (*non_constant_p)
8022 : : break;
8023 : : /* If the initializer is complex, evaluate it to initialize slot. */
8024 : 5379636 : bool is_complex = target_expr_needs_replace (t);
8025 : 5379636 : if (!is_complex)
8026 : : {
8027 : 5379606 : r = unshare_constructor (r);
8028 : : /* Adjust the type of the result to the type of the temporary. */
8029 : 5379606 : r = adjust_temp_type (type, r);
8030 : 5379606 : ctx->global->put_value (slot, r);
8031 : : }
8032 : 5379636 : if (TARGET_EXPR_CLEANUP (t) && !CLEANUP_EH_ONLY (t))
8033 : 189933 : ctx->global->cleanups->safe_push (TARGET_EXPR_CLEANUP (t));
8034 : 5379636 : if (ctx->save_exprs)
8035 : 1733294 : ctx->save_exprs->safe_push (slot);
8036 : 5379636 : if (lval)
8037 : : return slot;
8038 : 770114 : if (is_complex)
8039 : 0 : r = ctx->global->get_value (slot);
8040 : : }
8041 : 770114 : break;
8042 : :
8043 : 31311059 : case INIT_EXPR:
8044 : 31311059 : case MODIFY_EXPR:
8045 : 31311059 : gcc_assert (jump_target == NULL || *jump_target == NULL_TREE);
8046 : 31311059 : r = cxx_eval_store_expression (ctx, t, lval,
8047 : : non_constant_p, overflow_p);
8048 : 31311059 : break;
8049 : :
8050 : 0 : case SCOPE_REF:
8051 : 0 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
8052 : : lval,
8053 : : non_constant_p, overflow_p);
8054 : 0 : break;
8055 : :
8056 : 17741162 : case RETURN_EXPR:
8057 : 17741162 : if (TREE_OPERAND (t, 0) != NULL_TREE)
8058 : 17720570 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
8059 : : lval,
8060 : : non_constant_p, overflow_p);
8061 : : /* FALLTHRU */
8062 : 17762575 : case BREAK_STMT:
8063 : 17762575 : case CONTINUE_STMT:
8064 : 17762575 : if (jump_target)
8065 : 17762575 : *jump_target = t;
8066 : : else
8067 : : {
8068 : : /* Can happen with ({ return true; }) && false; passed to
8069 : : maybe_constant_value. There is nothing to jump over in this
8070 : : case, and the bug will be diagnosed later. */
8071 : 0 : gcc_assert (ctx->quiet);
8072 : 0 : *non_constant_p = true;
8073 : : }
8074 : : break;
8075 : :
8076 : 598686 : case SAVE_EXPR:
8077 : : /* Avoid evaluating a SAVE_EXPR more than once. */
8078 : 598686 : if (tree v = ctx->global->get_value (t))
8079 : : r = v;
8080 : : else
8081 : : {
8082 : 592826 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), vc_prvalue,
8083 : : non_constant_p, overflow_p);
8084 : 592826 : if (*non_constant_p)
8085 : : break;
8086 : 5963 : ctx->global->put_value (t, r);
8087 : 5963 : if (ctx->save_exprs)
8088 : 5553 : ctx->save_exprs->safe_push (t);
8089 : : }
8090 : : break;
8091 : :
8092 : 0 : case TRY_CATCH_EXPR:
8093 : 0 : if (TREE_OPERAND (t, 0) == NULL_TREE)
8094 : : {
8095 : 0 : r = void_node;
8096 : 0 : break;
8097 : : }
8098 : : /* FALLTHRU */
8099 : 36733819 : case NON_LVALUE_EXPR:
8100 : 36733819 : case TRY_BLOCK:
8101 : 36733819 : case MUST_NOT_THROW_EXPR:
8102 : 36733819 : case EXPR_STMT:
8103 : 36733819 : case EH_SPEC_BLOCK:
8104 : 36733819 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
8105 : : lval,
8106 : : non_constant_p, overflow_p,
8107 : : jump_target);
8108 : 36733819 : break;
8109 : :
8110 : 25930865 : case CLEANUP_POINT_EXPR:
8111 : 25930865 : {
8112 : 25930865 : auto_vec<tree, 2> cleanups;
8113 : 25930865 : vec<tree> *prev_cleanups = ctx->global->cleanups;
8114 : 25930865 : ctx->global->cleanups = &cleanups;
8115 : :
8116 : 25930865 : auto_vec<tree, 10> save_exprs;
8117 : 25930865 : constexpr_ctx new_ctx = *ctx;
8118 : 25930865 : new_ctx.save_exprs = &save_exprs;
8119 : :
8120 : 25930865 : r = cxx_eval_constant_expression (&new_ctx, TREE_OPERAND (t, 0),
8121 : : lval,
8122 : : non_constant_p, overflow_p,
8123 : : jump_target);
8124 : :
8125 : 25930865 : ctx->global->cleanups = prev_cleanups;
8126 : 25930865 : unsigned int i;
8127 : 25930865 : tree cleanup;
8128 : : /* Evaluate the cleanups. */
8129 : 52037609 : FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup)
8130 : 175879 : cxx_eval_constant_expression (&new_ctx, cleanup, vc_discard,
8131 : : non_constant_p, overflow_p);
8132 : :
8133 : : /* Forget SAVE_EXPRs and TARGET_EXPRs created by this
8134 : : full-expression. */
8135 : 79531442 : for (tree save_expr : save_exprs)
8136 : 1738847 : destroy_value_checked (ctx, save_expr, non_constant_p);
8137 : 25930865 : }
8138 : 25930865 : break;
8139 : :
8140 : 353 : case TRY_FINALLY_EXPR:
8141 : 353 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
8142 : : non_constant_p, overflow_p,
8143 : : jump_target);
8144 : 353 : if (!*non_constant_p)
8145 : : /* Also evaluate the cleanup. */
8146 : 338 : cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), vc_discard,
8147 : : non_constant_p, overflow_p);
8148 : : break;
8149 : :
8150 : 18 : case EH_ELSE_EXPR:
8151 : : /* Evaluate any cleanup that applies to non-EH exits. */
8152 : 18 : cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), vc_discard,
8153 : : non_constant_p, overflow_p);
8154 : :
8155 : : /* We do not have constexpr exceptions yet, so skip the EH path. */
8156 : 18 : break;
8157 : :
8158 : 613768 : case CLEANUP_STMT:
8159 : 613768 : r = cxx_eval_constant_expression (ctx, CLEANUP_BODY (t), lval,
8160 : : non_constant_p, overflow_p,
8161 : : jump_target);
8162 : 613768 : if (!CLEANUP_EH_ONLY (t) && !*non_constant_p)
8163 : : {
8164 : 49298 : iloc_sentinel ils (loc);
8165 : : /* Also evaluate the cleanup. */
8166 : 49298 : cxx_eval_constant_expression (ctx, CLEANUP_EXPR (t), vc_discard,
8167 : : non_constant_p, overflow_p);
8168 : 49298 : }
8169 : : break;
8170 : :
8171 : : /* These differ from cxx_eval_unary_expression in that this doesn't
8172 : : check for a constant operand or result; an address can be
8173 : : constant without its operand being, and vice versa. */
8174 : 30548959 : case MEM_REF:
8175 : 30548959 : case INDIRECT_REF:
8176 : 30548959 : r = cxx_eval_indirect_ref (ctx, t, lval,
8177 : : non_constant_p, overflow_p);
8178 : 30548959 : break;
8179 : :
8180 : 37009927 : case ADDR_EXPR:
8181 : 37009927 : {
8182 : 37009927 : tree oldop = TREE_OPERAND (t, 0);
8183 : 37009927 : tree op = cxx_eval_constant_expression (ctx, oldop, vc_glvalue,
8184 : : non_constant_p, overflow_p);
8185 : : /* Don't VERIFY_CONSTANT here. */
8186 : 37009927 : if (*non_constant_p)
8187 : : return t;
8188 : 29583387 : gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR);
8189 : : /* This function does more aggressive folding than fold itself. */
8190 : 29583387 : r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
8191 : 29583387 : if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
8192 : : {
8193 : 22019156 : ggc_free (r);
8194 : 22019156 : return t;
8195 : : }
8196 : : break;
8197 : : }
8198 : :
8199 : 473065 : case REALPART_EXPR:
8200 : 473065 : case IMAGPART_EXPR:
8201 : 473065 : if (lval)
8202 : : {
8203 : 576 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
8204 : : non_constant_p, overflow_p);
8205 : 576 : if (r == error_mark_node)
8206 : : ;
8207 : 576 : else if (r == TREE_OPERAND (t, 0) || lval == vc_discard)
8208 : : r = t;
8209 : : else
8210 : 554 : r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), r);
8211 : : break;
8212 : : }
8213 : : /* FALLTHRU */
8214 : 20912369 : case CONJ_EXPR:
8215 : 20912369 : case FIX_TRUNC_EXPR:
8216 : 20912369 : case FLOAT_EXPR:
8217 : 20912369 : case NEGATE_EXPR:
8218 : 20912369 : case ABS_EXPR:
8219 : 20912369 : case ABSU_EXPR:
8220 : 20912369 : case BIT_NOT_EXPR:
8221 : 20912369 : case TRUTH_NOT_EXPR:
8222 : 20912369 : case FIXED_CONVERT_EXPR:
8223 : 20912369 : case VEC_DUPLICATE_EXPR:
8224 : 20912369 : r = cxx_eval_unary_expression (ctx, t, lval,
8225 : : non_constant_p, overflow_p);
8226 : 20912369 : break;
8227 : :
8228 : 7046244 : case SIZEOF_EXPR:
8229 : 7046244 : r = fold_sizeof_expr (t);
8230 : : /* In a template, fold_sizeof_expr may merely create a new SIZEOF_EXPR,
8231 : : which could lead to an infinite recursion. */
8232 : 7046244 : if (TREE_CODE (r) != SIZEOF_EXPR)
8233 : 7046244 : r = cxx_eval_constant_expression (ctx, r, lval,
8234 : : non_constant_p, overflow_p,
8235 : : jump_target);
8236 : : else
8237 : : {
8238 : 0 : *non_constant_p = true;
8239 : 0 : gcc_assert (ctx->quiet);
8240 : : }
8241 : :
8242 : : break;
8243 : :
8244 : 3136145 : case COMPOUND_EXPR:
8245 : 3136145 : {
8246 : : /* check_return_expr sometimes wraps a TARGET_EXPR in a
8247 : : COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
8248 : : introduced by build_call_a. */
8249 : 3136145 : tree op0 = TREE_OPERAND (t, 0);
8250 : 3136145 : tree op1 = TREE_OPERAND (t, 1);
8251 : 3136145 : STRIP_NOPS (op1);
8252 : 701514 : if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
8253 : 3190387 : || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
8254 : 2816472 : r = cxx_eval_constant_expression (ctx, op0,
8255 : : lval, non_constant_p, overflow_p,
8256 : : jump_target);
8257 : : else
8258 : : {
8259 : : /* Check that the LHS is constant and then discard it. */
8260 : 319673 : cxx_eval_constant_expression (ctx, op0, vc_discard,
8261 : : non_constant_p, overflow_p,
8262 : : jump_target);
8263 : 319673 : if (*non_constant_p)
8264 : : return t;
8265 : 230626 : op1 = TREE_OPERAND (t, 1);
8266 : 230626 : r = cxx_eval_constant_expression (ctx, op1,
8267 : : lval, non_constant_p, overflow_p,
8268 : : jump_target);
8269 : : }
8270 : : }
8271 : : break;
8272 : :
8273 : 46370971 : case POINTER_PLUS_EXPR:
8274 : 46370971 : case POINTER_DIFF_EXPR:
8275 : 46370971 : case PLUS_EXPR:
8276 : 46370971 : case MINUS_EXPR:
8277 : 46370971 : case MULT_EXPR:
8278 : 46370971 : case TRUNC_DIV_EXPR:
8279 : 46370971 : case CEIL_DIV_EXPR:
8280 : 46370971 : case FLOOR_DIV_EXPR:
8281 : 46370971 : case ROUND_DIV_EXPR:
8282 : 46370971 : case TRUNC_MOD_EXPR:
8283 : 46370971 : case CEIL_MOD_EXPR:
8284 : 46370971 : case ROUND_MOD_EXPR:
8285 : 46370971 : case RDIV_EXPR:
8286 : 46370971 : case EXACT_DIV_EXPR:
8287 : 46370971 : case MIN_EXPR:
8288 : 46370971 : case MAX_EXPR:
8289 : 46370971 : case LSHIFT_EXPR:
8290 : 46370971 : case RSHIFT_EXPR:
8291 : 46370971 : case LROTATE_EXPR:
8292 : 46370971 : case RROTATE_EXPR:
8293 : 46370971 : case BIT_IOR_EXPR:
8294 : 46370971 : case BIT_XOR_EXPR:
8295 : 46370971 : case BIT_AND_EXPR:
8296 : 46370971 : case TRUTH_XOR_EXPR:
8297 : 46370971 : case LT_EXPR:
8298 : 46370971 : case LE_EXPR:
8299 : 46370971 : case GT_EXPR:
8300 : 46370971 : case GE_EXPR:
8301 : 46370971 : case EQ_EXPR:
8302 : 46370971 : case NE_EXPR:
8303 : 46370971 : case SPACESHIP_EXPR:
8304 : 46370971 : case UNORDERED_EXPR:
8305 : 46370971 : case ORDERED_EXPR:
8306 : 46370971 : case UNLT_EXPR:
8307 : 46370971 : case UNLE_EXPR:
8308 : 46370971 : case UNGT_EXPR:
8309 : 46370971 : case UNGE_EXPR:
8310 : 46370971 : case UNEQ_EXPR:
8311 : 46370971 : case LTGT_EXPR:
8312 : 46370971 : case RANGE_EXPR:
8313 : 46370971 : case COMPLEX_EXPR:
8314 : 46370971 : r = cxx_eval_binary_expression (ctx, t, lval,
8315 : : non_constant_p, overflow_p);
8316 : 46370971 : break;
8317 : :
8318 : : /* fold can introduce non-IF versions of these; still treat them as
8319 : : short-circuiting. */
8320 : 5571778 : case TRUTH_AND_EXPR:
8321 : 5571778 : case TRUTH_ANDIF_EXPR:
8322 : 5571778 : r = cxx_eval_logical_expression (ctx, t, boolean_false_node,
8323 : : boolean_true_node,
8324 : : non_constant_p, overflow_p);
8325 : 5571778 : break;
8326 : :
8327 : 1173856 : case TRUTH_OR_EXPR:
8328 : 1173856 : case TRUTH_ORIF_EXPR:
8329 : 1173856 : r = cxx_eval_logical_expression (ctx, t, boolean_true_node,
8330 : : boolean_false_node,
8331 : : non_constant_p, overflow_p);
8332 : 1173856 : break;
8333 : :
8334 : 2527521 : case ARRAY_REF:
8335 : 2527521 : r = cxx_eval_array_reference (ctx, t, lval,
8336 : : non_constant_p, overflow_p);
8337 : 2527521 : break;
8338 : :
8339 : 27806347 : case COMPONENT_REF:
8340 : 27806347 : if (is_overloaded_fn (t))
8341 : : {
8342 : : /* We can only get here in checking mode via
8343 : : build_non_dependent_expr, because any expression that
8344 : : calls or takes the address of the function will have
8345 : : pulled a FUNCTION_DECL out of the COMPONENT_REF. */
8346 : 6 : gcc_checking_assert (ctx->quiet || errorcount);
8347 : 6 : *non_constant_p = true;
8348 : 6 : return t;
8349 : : }
8350 : 27806341 : r = cxx_eval_component_reference (ctx, t, lval,
8351 : : non_constant_p, overflow_p);
8352 : 27806341 : break;
8353 : :
8354 : 49 : case BIT_FIELD_REF:
8355 : 49 : r = cxx_eval_bit_field_ref (ctx, t, lval,
8356 : : non_constant_p, overflow_p);
8357 : 49 : break;
8358 : :
8359 : 7759908 : case COND_EXPR:
8360 : 7759908 : case IF_STMT:
8361 : 7759908 : if (jump_target && *jump_target)
8362 : : {
8363 : 151644 : tree orig_jump = *jump_target;
8364 : 151644 : tree arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 1))
8365 : 303288 : ? TREE_OPERAND (t, 1) : void_node);
8366 : : /* When jumping to a label, the label might be either in the
8367 : : then or else blocks, so process then block first in skipping
8368 : : mode first, and if we are still in the skipping mode at its end,
8369 : : process the else block too. */
8370 : 151644 : r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p,
8371 : : overflow_p, jump_target);
8372 : : /* It's possible that we found the label in the then block. But
8373 : : it could have been followed by another jumping statement, e.g.
8374 : : say we're looking for case 1:
8375 : : if (cond)
8376 : : {
8377 : : // skipped statements
8378 : : case 1:; // clears up *jump_target
8379 : : return 1; // and sets it to a RETURN_EXPR
8380 : : }
8381 : : else { ... }
8382 : : in which case we need not go looking to the else block.
8383 : : (goto is not allowed in a constexpr function.) */
8384 : 151644 : if (*jump_target == orig_jump)
8385 : : {
8386 : 151683 : arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 2))
8387 : 151683 : ? TREE_OPERAND (t, 2) : void_node);
8388 : 151614 : r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p,
8389 : : overflow_p, jump_target);
8390 : : }
8391 : : break;
8392 : : }
8393 : 7608264 : r = cxx_eval_conditional_expression (ctx, t, lval,
8394 : : non_constant_p, overflow_p,
8395 : : jump_target);
8396 : 7608264 : break;
8397 : 866 : case VEC_COND_EXPR:
8398 : 866 : r = cxx_eval_vector_conditional_expression (ctx, t, non_constant_p,
8399 : : overflow_p);
8400 : 866 : break;
8401 : :
8402 : 12708550 : case CONSTRUCTOR:
8403 : 12708550 : if (TREE_CONSTANT (t) && reduced_constant_expression_p (t))
8404 : : {
8405 : : /* Don't re-process a constant CONSTRUCTOR. */
8406 : 11475215 : verify_constructor_flags (t);
8407 : 11475215 : if (TREE_CONSTANT (t))
8408 : : return t;
8409 : : }
8410 : 1233335 : r = cxx_eval_bare_aggregate (ctx, t, lval,
8411 : : non_constant_p, overflow_p);
8412 : 1233335 : break;
8413 : :
8414 : 476 : case VEC_INIT_EXPR:
8415 : : /* We can get this in a defaulted constructor for a class with a
8416 : : non-static data member of array type. Either the initializer will
8417 : : be NULL, meaning default-initialization, or it will be an lvalue
8418 : : or xvalue of the same type, meaning direct-initialization from the
8419 : : corresponding member. */
8420 : 476 : r = cxx_eval_vec_init (ctx, t, lval,
8421 : : non_constant_p, overflow_p);
8422 : 476 : break;
8423 : :
8424 : 16 : case VEC_PERM_EXPR:
8425 : 16 : r = cxx_eval_trinary_expression (ctx, t, lval,
8426 : : non_constant_p, overflow_p);
8427 : 16 : break;
8428 : :
8429 : 4 : case PAREN_EXPR:
8430 : 4 : gcc_assert (!REF_PARENTHESIZED_P (t));
8431 : : /* A PAREN_EXPR resulting from __builtin_assoc_barrier has no effect in
8432 : : constant expressions since it's unaffected by -fassociative-math. */
8433 : 4 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
8434 : : non_constant_p, overflow_p);
8435 : 4 : break;
8436 : :
8437 : 158405972 : case NOP_EXPR:
8438 : 158405972 : if (REINTERPRET_CAST_P (t))
8439 : : {
8440 : 67 : if (!ctx->quiet)
8441 : 0 : error_at (loc,
8442 : : "%<reinterpret_cast%> is not a constant expression");
8443 : 67 : *non_constant_p = true;
8444 : 67 : return t;
8445 : : }
8446 : : /* FALLTHROUGH. */
8447 : 187257652 : case CONVERT_EXPR:
8448 : 187257652 : case VIEW_CONVERT_EXPR:
8449 : 187257652 : case UNARY_PLUS_EXPR:
8450 : 187257652 : {
8451 : 187257652 : tree oldop = TREE_OPERAND (t, 0);
8452 : :
8453 : 187257652 : tree op = cxx_eval_constant_expression (ctx, oldop,
8454 : 187257652 : VOID_TYPE_P (TREE_TYPE (t))
8455 : : ? vc_discard
8456 : : : tcode == VIEW_CONVERT_EXPR
8457 : 176910697 : ? lval : vc_prvalue,
8458 : : non_constant_p, overflow_p);
8459 : 187254955 : if (*non_constant_p)
8460 : : return t;
8461 : 138313674 : tree type = TREE_TYPE (t);
8462 : :
8463 : 138313674 : if (VOID_TYPE_P (type))
8464 : 9739685 : return void_node;
8465 : :
8466 : 128573989 : if (TREE_CODE (t) == CONVERT_EXPR
8467 : 10737972 : && ARITHMETIC_TYPE_P (type)
8468 : 1950453 : && INDIRECT_TYPE_P (TREE_TYPE (op))
8469 : 128597055 : && ctx->strict)
8470 : : {
8471 : 22978 : if (!ctx->quiet)
8472 : 54 : error_at (loc,
8473 : : "conversion from pointer type %qT to arithmetic type "
8474 : 54 : "%qT in a constant expression", TREE_TYPE (op), type);
8475 : 22978 : *non_constant_p = true;
8476 : 22978 : return t;
8477 : : }
8478 : :
8479 : : /* [expr.const]: a conversion from type cv void* to a pointer-to-object
8480 : : type cannot be part of a core constant expression as a resolution to
8481 : : DR 1312. */
8482 : 35697221 : if (TYPE_PTROB_P (type)
8483 : 34525761 : && TYPE_PTR_P (TREE_TYPE (op))
8484 : 25729003 : && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (op)))
8485 : : /* Inside a call to std::construct_at,
8486 : : std::allocator<T>::{,de}allocate, or
8487 : : std::source_location::current, we permit casting from void*
8488 : : because that is compiler-generated code. */
8489 : 39725 : && !is_std_construct_at (ctx->call)
8490 : 5591 : && !is_std_allocator_allocate (ctx->call)
8491 : 128555677 : && !is_std_source_location_current (ctx->call))
8492 : : {
8493 : : /* Likewise, don't error when casting from void* when OP is
8494 : : &heap uninit and similar. */
8495 : 4507 : tree sop = tree_strip_nop_conversions (op);
8496 : 4507 : tree decl = NULL_TREE;
8497 : 4507 : if (TREE_CODE (sop) == ADDR_EXPR)
8498 : 1748 : decl = TREE_OPERAND (sop, 0);
8499 : 1748 : if (decl
8500 : 1748 : && VAR_P (decl)
8501 : 1712 : && DECL_ARTIFICIAL (decl)
8502 : 3423 : && (DECL_NAME (decl) == heap_identifier
8503 : 1103 : || DECL_NAME (decl) == heap_uninit_identifier
8504 : 437 : || DECL_NAME (decl) == heap_vec_identifier
8505 : 313 : || DECL_NAME (decl) == heap_vec_uninit_identifier))
8506 : : /* OK */;
8507 : : /* P2738 (C++26): a conversion from a prvalue P of type "pointer to
8508 : : cv void" to a pointer-to-object type T unless P is a null
8509 : : pointer value or points to an object whose type is similar to
8510 : : T. */
8511 : 2848 : else if (cxx_dialect > cxx23)
8512 : : {
8513 : 2732 : if (integer_zerop (sop))
8514 : 30 : return build_int_cst (type, 0);
8515 : 2702 : r = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (type), sop);
8516 : 2702 : if (r)
8517 : : {
8518 : 2688 : r = build1 (ADDR_EXPR, type, r);
8519 : 2688 : break;
8520 : : }
8521 : 14 : if (!ctx->quiet)
8522 : : {
8523 : 3 : gcc_assert (TREE_CODE (sop) == ADDR_EXPR);
8524 : 3 : auto_diagnostic_group d;
8525 : 3 : error_at (loc, "cast from %qT is not allowed in a "
8526 : : "constant expression because "
8527 : : "pointed-to type %qT is not similar to %qT",
8528 : 3 : TREE_TYPE (op), TREE_TYPE (TREE_TYPE (sop)),
8529 : 3 : TREE_TYPE (type));
8530 : 3 : tree obj = build_fold_indirect_ref (sop);
8531 : 3 : if (TREE_CODE (obj) == COMPONENT_REF)
8532 : 2 : obj = TREE_OPERAND (obj, 1);
8533 : 3 : if (DECL_P (obj))
8534 : 3 : inform (DECL_SOURCE_LOCATION (obj),
8535 : : "pointed-to object declared here");
8536 : 3 : }
8537 : 14 : *non_constant_p = true;
8538 : 14 : return t;
8539 : : }
8540 : : else
8541 : : {
8542 : 116 : if (!ctx->quiet)
8543 : 26 : error_at (loc, "cast from %qT is not allowed in a "
8544 : : "constant expression before C++26",
8545 : 26 : TREE_TYPE (op));
8546 : 116 : *non_constant_p = true;
8547 : 116 : return t;
8548 : : }
8549 : : }
8550 : :
8551 : 128548163 : if (TREE_CODE (op) == PTRMEM_CST && !TYPE_PTRMEM_P (type))
8552 : : {
8553 : 1578 : op = cplus_expand_constant (op);
8554 : 1578 : if (TREE_CODE (op) == PTRMEM_CST)
8555 : : {
8556 : 21 : if (!ctx->quiet)
8557 : 3 : error_at (loc, "%qE is not a constant expression when the "
8558 : : "class %qT is still incomplete", op,
8559 : 3 : PTRMEM_CST_CLASS (op));
8560 : 21 : *non_constant_p = true;
8561 : 21 : return t;
8562 : : }
8563 : : }
8564 : :
8565 : 128548142 : if (TREE_CODE (op) == PTRMEM_CST && tcode == NOP_EXPR)
8566 : : {
8567 : 725 : if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (op))
8568 : 725 : && !can_convert_qual (type, op))
8569 : 6 : op = cplus_expand_constant (op);
8570 : 725 : return cp_fold_convert (type, op);
8571 : : }
8572 : :
8573 : 128547417 : if (INDIRECT_TYPE_P (type) && TREE_CODE (op) == INTEGER_CST)
8574 : : {
8575 : 312783 : if (integer_zerop (op))
8576 : : {
8577 : 254859 : if (TYPE_REF_P (type))
8578 : : {
8579 : 32 : if (!ctx->quiet)
8580 : 4 : error_at (loc, "dereferencing a null pointer");
8581 : 32 : *non_constant_p = true;
8582 : 32 : return t;
8583 : : }
8584 : : }
8585 : 57924 : else if (TYPE_PTR_P (type)
8586 : 57924 : && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
8587 : : /* INTEGER_CST with pointer-to-method type is only used
8588 : : for a virtual method in a pointer to member function.
8589 : : Don't reject those. */
8590 : : ;
8591 : : else
8592 : : {
8593 : : /* This detects for example:
8594 : : reinterpret_cast<void*>(sizeof 0)
8595 : : */
8596 : 57921 : if (!ctx->quiet)
8597 : 21 : error_at (loc, "%<reinterpret_cast<%T>(%E)%> is not "
8598 : : "a constant expression",
8599 : : type, op);
8600 : 57921 : *non_constant_p = true;
8601 : 57921 : return t;
8602 : : }
8603 : : }
8604 : :
8605 : 128489464 : if (INDIRECT_TYPE_P (type)
8606 : 55565132 : && TREE_CODE (op) == NOP_EXPR
8607 : 27578528 : && TREE_TYPE (op) == ptr_type_node
8608 : 38512 : && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR
8609 : 37149 : && VAR_P (TREE_OPERAND (TREE_OPERAND (op, 0), 0))
8610 : 128504945 : && (DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0),
8611 : 15481 : 0)) == heap_uninit_identifier
8612 : 13890 : || DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0),
8613 : 13890 : 0)) == heap_vec_uninit_identifier))
8614 : : {
8615 : 1888 : tree var = TREE_OPERAND (TREE_OPERAND (op, 0), 0);
8616 : 1888 : tree var_size = TYPE_SIZE_UNIT (TREE_TYPE (var));
8617 : 1888 : tree elt_type = TREE_TYPE (type);
8618 : 1888 : tree cookie_size = NULL_TREE;
8619 : 1888 : tree arg_size = NULL_TREE;
8620 : 1888 : if (TREE_CODE (elt_type) == RECORD_TYPE
8621 : 1888 : && TYPE_NAME (elt_type) == heap_identifier)
8622 : : {
8623 : 3 : tree fld1 = TYPE_FIELDS (elt_type);
8624 : 3 : tree fld2 = DECL_CHAIN (fld1);
8625 : 3 : elt_type = TREE_TYPE (TREE_TYPE (fld2));
8626 : 3 : cookie_size = TYPE_SIZE_UNIT (TREE_TYPE (fld1));
8627 : : }
8628 : 1888 : DECL_NAME (var)
8629 : 1888 : = (DECL_NAME (var) == heap_uninit_identifier
8630 : 1888 : ? heap_identifier : heap_vec_identifier);
8631 : : /* For zero sized elt_type, try to recover how many outer_nelts
8632 : : it should have. */
8633 : 1888 : if ((cookie_size ? tree_int_cst_equal (var_size, cookie_size)
8634 : 1885 : : integer_zerop (var_size))
8635 : 15 : && !int_size_in_bytes (elt_type)
8636 : 9 : && TREE_CODE (oldop) == CALL_EXPR
8637 : 1900 : && call_expr_nargs (oldop) >= 1)
8638 : 9 : if (tree fun = get_function_named_in_call (oldop))
8639 : 9 : if (cxx_replaceable_global_alloc_fn (fun)
8640 : 9 : && IDENTIFIER_NEW_OP_P (DECL_NAME (fun)))
8641 : 9 : arg_size = CALL_EXPR_ARG (oldop, 0);
8642 : 1888 : TREE_TYPE (var)
8643 : 1888 : = build_new_constexpr_heap_type (ctx, elt_type, cookie_size,
8644 : : var_size, arg_size,
8645 : : non_constant_p, overflow_p);
8646 : 1888 : TREE_TYPE (TREE_OPERAND (op, 0))
8647 : 3776 : = build_pointer_type (TREE_TYPE (var));
8648 : : }
8649 : :
8650 : 128489464 : if (op == oldop && tcode != UNARY_PLUS_EXPR)
8651 : : /* We didn't fold at the top so we could check for ptr-int
8652 : : conversion. */
8653 : 11300198 : return fold (t);
8654 : :
8655 : 117189266 : tree sop;
8656 : :
8657 : : /* Handle an array's bounds having been deduced after we built
8658 : : the wrapping expression. */
8659 : 117189266 : if (same_type_ignoring_tlq_and_bounds_p (type, TREE_TYPE (op)))
8660 : : r = op;
8661 : 32888353 : else if (sop = tree_strip_nop_conversions (op),
8662 : 45660666 : sop != op && (same_type_ignoring_tlq_and_bounds_p
8663 : 12772313 : (type, TREE_TYPE (sop))))
8664 : : r = sop;
8665 : 26774823 : else if (tcode == UNARY_PLUS_EXPR)
8666 : 0 : r = fold_convert (TREE_TYPE (t), op);
8667 : : else
8668 : 26774823 : r = fold_build1 (tcode, type, op);
8669 : :
8670 : : /* Conversion of an out-of-range value has implementation-defined
8671 : : behavior; the language considers it different from arithmetic
8672 : : overflow, which is undefined. */
8673 : 117189266 : if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op))
8674 : 11332 : TREE_OVERFLOW (r) = false;
8675 : : }
8676 : : break;
8677 : :
8678 : 6532 : case EXCESS_PRECISION_EXPR:
8679 : 6532 : {
8680 : 6532 : tree oldop = TREE_OPERAND (t, 0);
8681 : :
8682 : 6532 : tree op = cxx_eval_constant_expression (ctx, oldop,
8683 : : lval,
8684 : : non_constant_p, overflow_p);
8685 : 6532 : if (*non_constant_p)
8686 : : return t;
8687 : 6528 : r = fold_convert (TREE_TYPE (t), op);
8688 : 6528 : break;
8689 : : }
8690 : :
8691 : 29119 : case EMPTY_CLASS_EXPR:
8692 : : /* Handle EMPTY_CLASS_EXPR produced by build_call_a by lowering
8693 : : it to an appropriate CONSTRUCTOR. */
8694 : 29119 : return build_constructor (TREE_TYPE (t), NULL);
8695 : :
8696 : 17877494 : case STATEMENT_LIST:
8697 : 17877494 : new_ctx = *ctx;
8698 : 17877494 : new_ctx.ctor = new_ctx.object = NULL_TREE;
8699 : 17877494 : return cxx_eval_statement_list (&new_ctx, t,
8700 : 17877494 : non_constant_p, overflow_p, jump_target);
8701 : :
8702 : 7418426 : case BIND_EXPR:
8703 : : /* Pre-emptively clear the vars declared by this BIND_EXPR from the value
8704 : : map, so that when checking whether they're already destroyed later we
8705 : : don't get confused by remnants of previous calls. */
8706 : 10496529 : for (tree decl = BIND_EXPR_VARS (t); decl; decl = DECL_CHAIN (decl))
8707 : 3078103 : ctx->global->clear_value (decl);
8708 : 7418426 : r = cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t),
8709 : : lval,
8710 : : non_constant_p, overflow_p,
8711 : : jump_target);
8712 : 10496529 : for (tree decl = BIND_EXPR_VARS (t); decl; decl = DECL_CHAIN (decl))
8713 : 3078103 : destroy_value_checked (ctx, decl, non_constant_p);
8714 : : break;
8715 : :
8716 : 2171602 : case PREINCREMENT_EXPR:
8717 : 2171602 : case POSTINCREMENT_EXPR:
8718 : 2171602 : case PREDECREMENT_EXPR:
8719 : 2171602 : case POSTDECREMENT_EXPR:
8720 : 2171602 : return cxx_eval_increment_expression (ctx, t,
8721 : 2171602 : lval, non_constant_p, overflow_p);
8722 : :
8723 : 413 : case LAMBDA_EXPR:
8724 : 413 : case NEW_EXPR:
8725 : 413 : case VEC_NEW_EXPR:
8726 : 413 : case DELETE_EXPR:
8727 : 413 : case VEC_DELETE_EXPR:
8728 : 413 : case THROW_EXPR:
8729 : 413 : case MODOP_EXPR:
8730 : : /* GCC internal stuff. */
8731 : 413 : case VA_ARG_EXPR:
8732 : 413 : case BASELINK:
8733 : 413 : case OFFSET_REF:
8734 : 413 : if (!ctx->quiet)
8735 : 102 : error_at (loc, "expression %qE is not a constant expression", t);
8736 : 413 : *non_constant_p = true;
8737 : 413 : break;
8738 : :
8739 : 112487 : case OBJ_TYPE_REF:
8740 : : /* Virtual function lookup. We don't need to do anything fancy. */
8741 : 112487 : return cxx_eval_constant_expression (ctx, OBJ_TYPE_REF_EXPR (t),
8742 : 112487 : lval, non_constant_p, overflow_p);
8743 : :
8744 : 16113 : case PLACEHOLDER_EXPR:
8745 : : /* Use of the value or address of the current object. */
8746 : 16113 : if (tree ctor = lookup_placeholder (ctx, lval, TREE_TYPE (t)))
8747 : : {
8748 : 15881 : if (TREE_CODE (ctor) == CONSTRUCTOR)
8749 : : return ctor;
8750 : : else
8751 : 15581 : return cxx_eval_constant_expression (ctx, ctor, lval,
8752 : 15581 : non_constant_p, overflow_p);
8753 : : }
8754 : : /* A placeholder without a referent. We can get here when
8755 : : checking whether NSDMIs are noexcept, or in massage_init_elt;
8756 : : just say it's non-constant for now. */
8757 : 232 : gcc_assert (ctx->quiet);
8758 : 232 : *non_constant_p = true;
8759 : 232 : break;
8760 : :
8761 : 165 : case EXIT_EXPR:
8762 : 165 : {
8763 : 165 : tree cond = TREE_OPERAND (t, 0);
8764 : 165 : cond = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
8765 : : non_constant_p, overflow_p);
8766 : 165 : VERIFY_CONSTANT (cond);
8767 : 165 : if (integer_nonzerop (cond))
8768 : 35 : *jump_target = t;
8769 : : }
8770 : : break;
8771 : :
8772 : 9 : case GOTO_EXPR:
8773 : 9 : if (breaks (&TREE_OPERAND (t, 0))
8774 : 9 : || continues (&TREE_OPERAND (t, 0)))
8775 : 3 : *jump_target = TREE_OPERAND (t, 0);
8776 : : else
8777 : : {
8778 : 6 : if (!ctx->quiet)
8779 : 1 : error_at (loc, "%<goto%> is not a constant expression");
8780 : 6 : *non_constant_p = true;
8781 : : }
8782 : : break;
8783 : :
8784 : 730742 : case LOOP_EXPR:
8785 : 730742 : case DO_STMT:
8786 : 730742 : case WHILE_STMT:
8787 : 730742 : case FOR_STMT:
8788 : 730742 : cxx_eval_loop_expr (ctx, t,
8789 : : non_constant_p, overflow_p, jump_target);
8790 : 730742 : break;
8791 : :
8792 : 33930 : case SWITCH_EXPR:
8793 : 33930 : case SWITCH_STMT:
8794 : 33930 : cxx_eval_switch_expr (ctx, t,
8795 : : non_constant_p, overflow_p, jump_target);
8796 : 33930 : break;
8797 : :
8798 : 100 : case REQUIRES_EXPR:
8799 : : /* It's possible to get a requires-expression in a constant
8800 : : expression. For example:
8801 : :
8802 : : template<typename T> concept bool C() {
8803 : : return requires (T t) { t; };
8804 : : }
8805 : :
8806 : : template<typename T> requires !C<T>() void f(T);
8807 : :
8808 : : Normalization leaves f with the associated constraint
8809 : : '!requires (T t) { ... }' which is not transformed into
8810 : : a constraint. */
8811 : 100 : if (!processing_template_decl)
8812 : 100 : return evaluate_requires_expr (t);
8813 : : else
8814 : 0 : *non_constant_p = true;
8815 : 0 : return t;
8816 : :
8817 : 7220 : case ANNOTATE_EXPR:
8818 : 7220 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
8819 : : lval,
8820 : : non_constant_p, overflow_p,
8821 : : jump_target);
8822 : 7220 : break;
8823 : :
8824 : 18637 : case USING_STMT:
8825 : 18637 : r = void_node;
8826 : 18637 : break;
8827 : :
8828 : 103 : case ASSERTION_STMT:
8829 : 103 : case PRECONDITION_STMT:
8830 : 103 : case POSTCONDITION_STMT:
8831 : 103 : {
8832 : 103 : contract_semantic semantic = get_contract_semantic (t);
8833 : 103 : if (semantic == CCS_IGNORE)
8834 : : break;
8835 : :
8836 : 87 : if (!cxx_eval_assert (ctx, CONTRACT_CONDITION (t),
8837 : : G_("contract predicate is false in "
8838 : : "constant expression"),
8839 : 87 : EXPR_LOCATION (t), checked_contract_p (semantic),
8840 : : non_constant_p, overflow_p))
8841 : 38 : *non_constant_p = true;
8842 : 87 : r = void_node;
8843 : : }
8844 : 87 : break;
8845 : :
8846 : 983462 : case TEMPLATE_ID_EXPR:
8847 : 983462 : {
8848 : : /* We can evaluate template-id that refers to a concept only if
8849 : : the template arguments are non-dependent. */
8850 : 983462 : gcc_assert (concept_check_p (t));
8851 : :
8852 : 983462 : if (!value_dependent_expression_p (t)
8853 : 983462 : && !uid_sensitive_constexpr_evaluation_p ())
8854 : 983416 : r = evaluate_concept_check (t);
8855 : : else
8856 : 46 : *non_constant_p = true;
8857 : :
8858 : : break;
8859 : : }
8860 : :
8861 : 48 : case ASM_EXPR:
8862 : 48 : if (!ctx->quiet)
8863 : 28 : inline_asm_in_constexpr_error (loc, /*constexpr_fundef_p*/false);
8864 : 48 : *non_constant_p = true;
8865 : 48 : return t;
8866 : :
8867 : 1014 : case BIT_CAST_EXPR:
8868 : 1014 : if (lval)
8869 : : {
8870 : 0 : if (!ctx->quiet)
8871 : 0 : error_at (EXPR_LOCATION (t),
8872 : : "address of a call to %qs is not a constant expression",
8873 : : "__builtin_bit_cast");
8874 : 0 : *non_constant_p = true;
8875 : 0 : return t;
8876 : : }
8877 : 1014 : r = cxx_eval_bit_cast (ctx, t, non_constant_p, overflow_p);
8878 : 1014 : break;
8879 : :
8880 : 0 : case OMP_PARALLEL:
8881 : 0 : case OMP_TASK:
8882 : 0 : case OMP_FOR:
8883 : 0 : case OMP_SIMD:
8884 : 0 : case OMP_DISTRIBUTE:
8885 : 0 : case OMP_TASKLOOP:
8886 : 0 : case OMP_LOOP:
8887 : 0 : case OMP_TEAMS:
8888 : 0 : case OMP_TARGET_DATA:
8889 : 0 : case OMP_TARGET:
8890 : 0 : case OMP_SECTIONS:
8891 : 0 : case OMP_ORDERED:
8892 : 0 : case OMP_CRITICAL:
8893 : 0 : case OMP_SINGLE:
8894 : 0 : case OMP_SCAN:
8895 : 0 : case OMP_SCOPE:
8896 : 0 : case OMP_SECTION:
8897 : 0 : case OMP_STRUCTURED_BLOCK:
8898 : 0 : case OMP_MASTER:
8899 : 0 : case OMP_MASKED:
8900 : 0 : case OMP_TASKGROUP:
8901 : 0 : case OMP_TARGET_UPDATE:
8902 : 0 : case OMP_TARGET_ENTER_DATA:
8903 : 0 : case OMP_TARGET_EXIT_DATA:
8904 : 0 : case OMP_ATOMIC:
8905 : 0 : case OMP_ATOMIC_READ:
8906 : 0 : case OMP_ATOMIC_CAPTURE_OLD:
8907 : 0 : case OMP_ATOMIC_CAPTURE_NEW:
8908 : 0 : case OMP_DEPOBJ:
8909 : 0 : case OACC_PARALLEL:
8910 : 0 : case OACC_KERNELS:
8911 : 0 : case OACC_SERIAL:
8912 : 0 : case OACC_DATA:
8913 : 0 : case OACC_HOST_DATA:
8914 : 0 : case OACC_LOOP:
8915 : 0 : case OACC_CACHE:
8916 : 0 : case OACC_DECLARE:
8917 : 0 : case OACC_ENTER_DATA:
8918 : 0 : case OACC_EXIT_DATA:
8919 : 0 : case OACC_UPDATE:
8920 : 0 : if (!ctx->quiet)
8921 : 0 : error_at (EXPR_LOCATION (t),
8922 : : "statement is not a constant expression");
8923 : 0 : *non_constant_p = true;
8924 : 0 : break;
8925 : :
8926 : 0 : default:
8927 : 0 : if (STATEMENT_CODE_P (TREE_CODE (t)))
8928 : : {
8929 : : /* This function doesn't know how to deal with pre-genericize
8930 : : statements; this can only happen with statement-expressions,
8931 : : so for now just fail. */
8932 : 0 : if (!ctx->quiet)
8933 : 0 : error_at (EXPR_LOCATION (t),
8934 : : "statement is not a constant expression");
8935 : : }
8936 : 0 : else if (flag_checking)
8937 : 0 : internal_error ("unexpected expression %qE of kind %s", t,
8938 : : get_tree_code_name (TREE_CODE (t)));
8939 : 0 : *non_constant_p = true;
8940 : 0 : break;
8941 : : }
8942 : :
8943 : 692749036 : if (r == error_mark_node)
8944 : 9204033 : *non_constant_p = true;
8945 : :
8946 : 692749036 : if (*non_constant_p)
8947 : 230257455 : return t;
8948 : : else
8949 : : return r;
8950 : : }
8951 : :
8952 : : /* P0859: A function is needed for constant evaluation if it is a constexpr
8953 : : function that is named by an expression ([basic.def.odr]) that is
8954 : : potentially constant evaluated.
8955 : :
8956 : : So we need to instantiate any constexpr functions mentioned by the
8957 : : expression even if the definition isn't needed for evaluating the
8958 : : expression. */
8959 : :
8960 : : static tree
8961 : 376388107 : instantiate_cx_fn_r (tree *tp, int *walk_subtrees, void */*data*/)
8962 : : {
8963 : 376388107 : if (TREE_CODE (*tp) == FUNCTION_DECL
8964 : 10310625 : && DECL_DECLARED_CONSTEXPR_P (*tp)
8965 : 10209166 : && !DECL_INITIAL (*tp)
8966 : 1788591 : && !trivial_fn_p (*tp)
8967 : 1788588 : && (DECL_TEMPLOID_INSTANTIATION (*tp) || DECL_DEFAULTED_FN (*tp))
8968 : 376388107 : && !uid_sensitive_constexpr_evaluation_p ())
8969 : : {
8970 : 1788528 : ++function_depth;
8971 : 1788528 : if (DECL_TEMPLOID_INSTANTIATION (*tp))
8972 : 1788523 : instantiate_decl (*tp, /*defer_ok*/false, /*expl_inst*/false);
8973 : : else
8974 : 5 : synthesize_method (*tp);
8975 : 1788528 : --function_depth;
8976 : : }
8977 : 374599579 : else if (TREE_CODE (*tp) == CALL_EXPR
8978 : 364407135 : || TREE_CODE (*tp) == AGGR_INIT_EXPR)
8979 : : {
8980 : 10358984 : if (EXPR_HAS_LOCATION (*tp))
8981 : 10358878 : input_location = EXPR_LOCATION (*tp);
8982 : : }
8983 : :
8984 : 376388107 : if (!EXPR_P (*tp))
8985 : 222375989 : *walk_subtrees = 0;
8986 : :
8987 : 376388107 : return NULL_TREE;
8988 : : }
8989 : :
8990 : : static void
8991 : 190447271 : instantiate_constexpr_fns (tree t)
8992 : : {
8993 : 190447271 : location_t loc = input_location;
8994 : 190447271 : cp_walk_tree_without_duplicates (&t, instantiate_cx_fn_r, NULL);
8995 : 190447271 : input_location = loc;
8996 : 190447271 : }
8997 : :
8998 : : /* Look for heap variables in the expression *TP. */
8999 : :
9000 : : static tree
9001 : 68196 : find_heap_var_refs (tree *tp, int *walk_subtrees, void */*data*/)
9002 : : {
9003 : 68196 : if (VAR_P (*tp)
9004 : 68196 : && (DECL_NAME (*tp) == heap_uninit_identifier
9005 : 495 : || DECL_NAME (*tp) == heap_identifier
9006 : 249 : || DECL_NAME (*tp) == heap_vec_uninit_identifier
9007 : 205 : || DECL_NAME (*tp) == heap_vec_identifier
9008 : 20 : || DECL_NAME (*tp) == heap_deleted_identifier))
9009 : : return *tp;
9010 : :
9011 : 46044 : if (TYPE_P (*tp))
9012 : 0 : *walk_subtrees = 0;
9013 : : return NULL_TREE;
9014 : : }
9015 : :
9016 : : /* Find immediate function decls in *TP if any. */
9017 : :
9018 : : static tree
9019 : 183097106 : find_immediate_fndecl (tree *tp, int */*walk_subtrees*/, void */*data*/)
9020 : : {
9021 : 184454085 : if (TREE_CODE (*tp) == FUNCTION_DECL && DECL_IMMEDIATE_FUNCTION_P (*tp))
9022 : : return *tp;
9023 : 183095732 : if (TREE_CODE (*tp) == PTRMEM_CST
9024 : 7589 : && TREE_CODE (PTRMEM_CST_MEMBER (*tp)) == FUNCTION_DECL
9025 : 183102768 : && DECL_IMMEDIATE_FUNCTION_P (PTRMEM_CST_MEMBER (*tp)))
9026 : : return PTRMEM_CST_MEMBER (*tp);
9027 : : return NULL_TREE;
9028 : : }
9029 : :
9030 : : /* T has TREE_CONSTANT set but has been deemed not a valid C++ constant
9031 : : expression. Return a version of T that has TREE_CONSTANT cleared. */
9032 : :
9033 : : static tree
9034 : 132168 : mark_non_constant (tree t)
9035 : : {
9036 : 132168 : gcc_checking_assert (TREE_CONSTANT (t));
9037 : :
9038 : : /* This isn't actually constant, so unset TREE_CONSTANT.
9039 : : Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires
9040 : : it to be set if it is invariant address, even when it is not
9041 : : a valid C++ constant expression. Wrap it with a NOP_EXPR
9042 : : instead. */
9043 : 132168 : if (EXPR_P (t) && TREE_CODE (t) != ADDR_EXPR)
9044 : 131350 : t = copy_node (t);
9045 : 818 : else if (TREE_CODE (t) == CONSTRUCTOR)
9046 : 236 : t = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (t), t);
9047 : : else
9048 : 582 : t = build_nop (TREE_TYPE (t), t);
9049 : 132168 : TREE_CONSTANT (t) = false;
9050 : 132168 : return t;
9051 : : }
9052 : :
9053 : : /* ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
9054 : : STRICT has the same sense as for constant_value_1: true if we only allow
9055 : : conforming C++ constant expressions, or false if we want a constant value
9056 : : even if it doesn't conform.
9057 : : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
9058 : : per P0595 even when ALLOW_NON_CONSTANT is true.
9059 : : CONSTEXPR_DTOR is true when evaluating the dtor of a constexpr variable.
9060 : : OBJECT must be non-NULL in that case. */
9061 : :
9062 : : static tree
9063 : 361364035 : cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant,
9064 : : bool strict = true,
9065 : : mce_value manifestly_const_eval = mce_unknown,
9066 : : bool constexpr_dtor = false,
9067 : : tree object = NULL_TREE)
9068 : : {
9069 : 361364035 : auto_timevar time (TV_CONSTEXPR);
9070 : :
9071 : 361364035 : bool non_constant_p = false;
9072 : 361364035 : bool overflow_p = false;
9073 : :
9074 : 361364035 : if (BRACE_ENCLOSED_INITIALIZER_P (t))
9075 : : {
9076 : 0 : gcc_checking_assert (allow_non_constant);
9077 : : return t;
9078 : : }
9079 : :
9080 : 361364035 : constexpr_global_ctx global_ctx;
9081 : 361364035 : constexpr_ctx ctx = { &global_ctx, NULL, NULL, NULL, NULL, NULL, NULL,
9082 : : allow_non_constant, strict,
9083 : 361364035 : !allow_non_constant ? mce_true : manifestly_const_eval };
9084 : :
9085 : : /* Turn off -frounding-math for manifestly constant evaluation. */
9086 : 361364035 : warning_sentinel rm (flag_rounding_math,
9087 : 361364035 : ctx.manifestly_const_eval == mce_true);
9088 : 361364035 : tree type = (object
9089 : 361364035 : ? cv_unqualified (TREE_TYPE (object))
9090 : 339459382 : : initialized_type (t));
9091 : 361364035 : tree r = t;
9092 : 361364035 : bool is_consteval = false;
9093 : 361364035 : if (VOID_TYPE_P (type))
9094 : : {
9095 : 3308984 : if (!constexpr_dtor)
9096 : : {
9097 : 3308984 : if (cxx_dialect < cxx20)
9098 : : return t;
9099 : 2977846 : if (TREE_CODE (t) != CALL_EXPR && TREE_CODE (t) != AGGR_INIT_EXPR)
9100 : : return t;
9101 : : /* Calls to immediate functions returning void need to be
9102 : : evaluated. */
9103 : 2977835 : tree fndecl = cp_get_callee_fndecl_nofold (t);
9104 : 5955670 : if (fndecl == NULL_TREE || !DECL_IMMEDIATE_FUNCTION_P (fndecl))
9105 : : return t;
9106 : : else
9107 : : is_consteval = true;
9108 : : }
9109 : : }
9110 : 358055051 : else if (cxx_dialect >= cxx20
9111 : 150996692 : && (TREE_CODE (t) == CALL_EXPR
9112 : 127299856 : || TREE_CODE (t) == AGGR_INIT_EXPR
9113 : 125939039 : || TREE_CODE (t) == TARGET_EXPR))
9114 : : {
9115 : 26686327 : tree x = t;
9116 : 26686327 : if (TREE_CODE (x) == TARGET_EXPR)
9117 : 1628674 : x = TARGET_EXPR_INITIAL (x);
9118 : 26686327 : tree fndecl = cp_get_callee_fndecl_nofold (x);
9119 : 52495098 : if (fndecl && DECL_IMMEDIATE_FUNCTION_P (fndecl))
9120 : : is_consteval = true;
9121 : : /* Don't try to evaluate a std::vector constructor taking an integer, it
9122 : : will fail in the 'if (heap_var)' block below after doing all the work
9123 : : (c++/113835). This will need adjustment if P3554 is accepted. Note
9124 : : that evaluation of e.g. the vector default constructor can succeed, so
9125 : : we don't shortcut all vector constructors. */
9126 : 51617542 : if (fndecl && DECL_CONSTRUCTOR_P (fndecl) && allow_non_constant
9127 : 3650628 : && is_std_class (type, "vector") && call_expr_nargs (x) > 1
9128 : 26690708 : && TREE_CODE (TREE_TYPE (get_nth_callarg (x, 1))) == INTEGER_TYPE)
9129 : : return t;
9130 : : }
9131 : 358054710 : if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
9132 : : {
9133 : : /* In C++14 an NSDMI can participate in aggregate initialization,
9134 : : and can refer to the address of the object being initialized, so
9135 : : we need to pass in the relevant VAR_DECL if we want to do the
9136 : : evaluation in a single pass. The evaluation will dynamically
9137 : : update ctx.values for the VAR_DECL. We use the same strategy
9138 : : for C++11 constexpr constructors that refer to the object being
9139 : : initialized. */
9140 : 23322411 : if (constexpr_dtor)
9141 : : {
9142 : 131 : gcc_assert (object && VAR_P (object));
9143 : 131 : gcc_assert (DECL_DECLARED_CONSTEXPR_P (object));
9144 : 131 : gcc_assert (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object));
9145 : 131 : if (error_operand_p (DECL_INITIAL (object)))
9146 : : return t;
9147 : 122 : ctx.ctor = unshare_expr (DECL_INITIAL (object));
9148 : 122 : TREE_READONLY (ctx.ctor) = false;
9149 : : /* Temporarily force decl_really_constant_value to return false
9150 : : for it, we want to use ctx.ctor for the current value instead. */
9151 : 122 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = false;
9152 : : }
9153 : : else
9154 : : {
9155 : 23322280 : ctx.ctor = build_constructor (type, NULL);
9156 : 23322280 : CONSTRUCTOR_NO_CLEARING (ctx.ctor) = true;
9157 : : }
9158 : 23322402 : if (!object)
9159 : : {
9160 : 13021408 : if (TREE_CODE (t) == CALL_EXPR)
9161 : : {
9162 : : /* If T is calling a constructor to initialize an object, reframe
9163 : : it as an AGGR_INIT_EXPR to avoid trying to modify an object
9164 : : from outside the constant evaluation, which will fail even if
9165 : : the value is actually constant (is_constant_evaluated3.C). */
9166 : 5036419 : tree fn = cp_get_callee_fndecl_nofold (t);
9167 : 10072830 : if (fn && DECL_CONSTRUCTOR_P (fn))
9168 : : {
9169 : 2226512 : object = CALL_EXPR_ARG (t, 0);
9170 : 2226512 : object = build_fold_indirect_ref (object);
9171 : 2226512 : r = build_aggr_init_expr (type, r);
9172 : : }
9173 : : }
9174 : 7984989 : else if (TREE_CODE (t) == TARGET_EXPR)
9175 : 3078390 : object = TARGET_EXPR_SLOT (t);
9176 : 4906599 : else if (TREE_CODE (t) == AGGR_INIT_EXPR)
9177 : 432116 : object = AGGR_INIT_EXPR_SLOT (t);
9178 : : }
9179 : 23322402 : ctx.object = object;
9180 : 23322402 : if (object)
9181 : 16038012 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
9182 : : (type, TREE_TYPE (object)));
9183 : 16038012 : if (object && DECL_P (object))
9184 : 13604677 : global_ctx.put_value (object, ctx.ctor);
9185 : 23322402 : if (TREE_CODE (r) == TARGET_EXPR)
9186 : : /* Avoid creating another CONSTRUCTOR when we expand the
9187 : : TARGET_EXPR. */
9188 : 3134986 : r = TARGET_EXPR_INITIAL (r);
9189 : : }
9190 : :
9191 : 358054701 : auto_vec<tree, 16> cleanups;
9192 : 358054701 : global_ctx.cleanups = &cleanups;
9193 : :
9194 : 358054701 : if (manifestly_const_eval == mce_true)
9195 : 190447271 : instantiate_constexpr_fns (r);
9196 : 358054701 : r = cxx_eval_constant_expression (&ctx, r, vc_prvalue,
9197 : : &non_constant_p, &overflow_p);
9198 : :
9199 : : /* If we got a non-simple TARGET_EXPR, the initializer was a sequence
9200 : : of statements, and the result ought to be stored in ctx.ctor. */
9201 : 358052004 : if (r == void_node && !constexpr_dtor && ctx.ctor)
9202 : 0 : r = ctx.ctor;
9203 : :
9204 : 358052004 : unsigned int i;
9205 : 358052004 : tree cleanup;
9206 : : /* Evaluate the cleanups. */
9207 : 716118062 : FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup)
9208 : 14054 : cxx_eval_constant_expression (&ctx, cleanup, vc_discard,
9209 : : &non_constant_p, &overflow_p);
9210 : :
9211 : : /* Mutable logic is a bit tricky: we want to allow initialization of
9212 : : constexpr variables with mutable members, but we can't copy those
9213 : : members to another constexpr variable. */
9214 : 358052004 : if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_MUTABLE_POISON (r))
9215 : : {
9216 : 93 : if (!allow_non_constant)
9217 : 6 : error ("%qE is not a constant expression because it refers to "
9218 : : "mutable subobjects of %qT", t, type);
9219 : 93 : non_constant_p = true;
9220 : : }
9221 : :
9222 : 268381016 : if (!non_constant_p && cxx_dialect >= cxx20
9223 : 626433020 : && !global_ctx.heap_vars.is_empty ())
9224 : : {
9225 : 22566 : tree heap_var = cp_walk_tree_without_duplicates (&r, find_heap_var_refs,
9226 : : NULL);
9227 : 22566 : unsigned int i;
9228 : 22566 : if (heap_var)
9229 : : {
9230 : 22152 : if (!allow_non_constant && !non_constant_p)
9231 : : {
9232 : 9 : error ("%qE is not a constant expression because it refers to "
9233 : : "a result of %<operator new%>", t);
9234 : 9 : inform (DECL_SOURCE_LOCATION (heap_var), "allocated here");
9235 : : }
9236 : 22152 : r = t;
9237 : 22152 : non_constant_p = true;
9238 : : }
9239 : 46011 : FOR_EACH_VEC_ELT (global_ctx.heap_vars, i, heap_var)
9240 : : {
9241 : 23445 : if (DECL_NAME (heap_var) != heap_deleted_identifier)
9242 : : {
9243 : 22209 : if (!allow_non_constant && !non_constant_p)
9244 : : {
9245 : 13 : error ("%qE is not a constant expression because allocated "
9246 : : "storage has not been deallocated", t);
9247 : 13 : inform (DECL_SOURCE_LOCATION (heap_var), "allocated here");
9248 : : }
9249 : 22209 : r = t;
9250 : 22209 : non_constant_p = true;
9251 : : }
9252 : : }
9253 : : }
9254 : :
9255 : : /* Check that immediate invocation does not return an expression referencing
9256 : : any immediate function decls. */
9257 : 358052004 : if (!non_constant_p && cxx_dialect >= cxx20)
9258 : 218825652 : if (tree immediate_fndecl
9259 : 109412826 : = cp_walk_tree_without_duplicates (&r, find_immediate_fndecl,
9260 : : NULL))
9261 : : {
9262 : 1479 : if (!allow_non_constant && !non_constant_p)
9263 : : {
9264 : 72 : if (is_consteval)
9265 : 36 : error_at (cp_expr_loc_or_input_loc (t),
9266 : : "immediate evaluation returns address of immediate "
9267 : : "function %qD", immediate_fndecl);
9268 : : else
9269 : 54 : error_at (cp_expr_loc_or_input_loc (t),
9270 : : "constant evaluation returns address of immediate "
9271 : : "function %qD", immediate_fndecl);
9272 : : }
9273 : 1479 : r = t;
9274 : 1479 : non_constant_p = true;
9275 : : }
9276 : :
9277 : 358052004 : if (!non_constant_p && !constexpr_dtor)
9278 : 268357207 : verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p);
9279 : :
9280 : : /* After verify_constant because reduced_constant_expression_p can unset
9281 : : CONSTRUCTOR_NO_CLEARING. */
9282 : 358052004 : if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_NO_CLEARING (r))
9283 : : {
9284 : 66603 : if (!allow_non_constant)
9285 : 40 : error ("%qE is not a constant expression because it refers to "
9286 : : "an incompletely initialized variable", t);
9287 : 66603 : TREE_CONSTANT (r) = false;
9288 : 66603 : non_constant_p = true;
9289 : : }
9290 : :
9291 : 358052004 : if (non_constant_p)
9292 : : /* If we saw something bad, go back to our argument. The wrapping below is
9293 : : only for the cases of TREE_CONSTANT argument or overflow. */
9294 : 91670128 : r = t;
9295 : :
9296 : 358052004 : if (!non_constant_p && overflow_p)
9297 : 213 : non_constant_p = true;
9298 : :
9299 : : /* Unshare the result. */
9300 : 358052004 : bool should_unshare = true;
9301 : 358052004 : if (r == t || (TREE_CODE (t) == TARGET_EXPR
9302 : 1078884 : && TARGET_EXPR_INITIAL (t) == r))
9303 : : should_unshare = false;
9304 : :
9305 : 358052004 : if (non_constant_p && !allow_non_constant)
9306 : 2120 : return error_mark_node;
9307 : 358049884 : else if (non_constant_p && TREE_CONSTANT (r))
9308 : 127847 : r = mark_non_constant (r);
9309 : 357922037 : else if (non_constant_p)
9310 : : return t;
9311 : :
9312 : 266509510 : if (constexpr_dtor)
9313 : : {
9314 : 104 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = true;
9315 : 104 : return r;
9316 : : }
9317 : :
9318 : : /* Check we are not trying to return the wrong type. */
9319 : 266509406 : if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (r)))
9320 : : {
9321 : : /* If so, this is not a constant expression. */
9322 : 95 : if (!allow_non_constant)
9323 : 0 : error ("%qE is not a constant expression because it initializes "
9324 : 0 : "a %qT rather than %qT", t, TREE_TYPE (t), type);
9325 : 95 : return t;
9326 : : }
9327 : :
9328 : 266509311 : if (should_unshare)
9329 : 135971239 : r = unshare_expr (r);
9330 : :
9331 : 266509311 : if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
9332 : : {
9333 : 9144519 : r = adjust_temp_type (type, r);
9334 : 9144519 : if (TREE_CODE (t) == TARGET_EXPR
9335 : 9144519 : && TARGET_EXPR_INITIAL (t) == r)
9336 : : return t;
9337 : 8261224 : else if (TREE_CODE (t) == CONSTRUCTOR || TREE_CODE (t) == CALL_EXPR
9338 : 1036501 : || TREE_CODE (t) == AGGR_INIT_EXPR)
9339 : : /* Don't add a TARGET_EXPR if our argument didn't have one. */;
9340 : 218368 : else if (TREE_CODE (t) == TARGET_EXPR && TARGET_EXPR_CLEANUP (t))
9341 : 25 : r = get_target_expr (r);
9342 : : else
9343 : : {
9344 : 218343 : r = get_target_expr (r, tf_warning_or_error | tf_no_cleanup);
9345 : 218343 : TREE_CONSTANT (r) = true;
9346 : : }
9347 : : }
9348 : :
9349 : 265626016 : if (TREE_CODE (t) == TARGET_EXPR
9350 : 195589 : && TREE_CODE (r) == TARGET_EXPR)
9351 : : {
9352 : : /* Preserve this flag for potential_constant_expression, and the others
9353 : : for good measure. */
9354 : 195528 : TARGET_EXPR_ELIDING_P (r) = TARGET_EXPR_ELIDING_P (t);
9355 : 195528 : TARGET_EXPR_IMPLICIT_P (r) = TARGET_EXPR_IMPLICIT_P (t);
9356 : 195528 : TARGET_EXPR_LIST_INIT_P (r) = TARGET_EXPR_LIST_INIT_P (t);
9357 : 195528 : TARGET_EXPR_DIRECT_INIT_P (r) = TARGET_EXPR_DIRECT_INIT_P (t);
9358 : : }
9359 : :
9360 : : /* Remember the original location if that wouldn't need a wrapper. */
9361 : 265626016 : if (location_t loc = EXPR_LOCATION (t))
9362 : 109091362 : protected_set_expr_location (r, loc);
9363 : :
9364 : 265626016 : return r;
9365 : 722722676 : }
9366 : :
9367 : : /* If T represents a constant expression returns its reduced value.
9368 : : Otherwise return error_mark_node. */
9369 : :
9370 : : tree
9371 : 118860844 : cxx_constant_value (tree t, tree decl /* = NULL_TREE */,
9372 : : tsubst_flags_t complain /* = tf_error */)
9373 : : {
9374 : 118860844 : bool sfinae = !(complain & tf_error);
9375 : 118860844 : tree r = cxx_eval_outermost_constant_expr (t, sfinae, true, mce_true, false, decl);
9376 : 118860844 : if (sfinae && !TREE_CONSTANT (r))
9377 : 632 : r = error_mark_node;
9378 : 118860844 : return r;
9379 : : }
9380 : :
9381 : : /* Like cxx_constant_value, but used for evaluation of constexpr destructors
9382 : : of constexpr variables. The actual initializer of DECL is not modified. */
9383 : :
9384 : : void
9385 : 131 : cxx_constant_dtor (tree t, tree decl)
9386 : : {
9387 : 131 : cxx_eval_outermost_constant_expr (t, false, true, mce_true, true, decl);
9388 : 131 : }
9389 : :
9390 : : /* Helper routine for fold_simple function. Either return simplified
9391 : : expression T, otherwise NULL_TREE.
9392 : : In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold
9393 : : even if we are within template-declaration. So be careful on call, as in
9394 : : such case types can be undefined. */
9395 : :
9396 : : static tree
9397 : 104471941 : fold_simple_1 (tree t)
9398 : : {
9399 : 104471941 : tree op1;
9400 : 104471941 : enum tree_code code = TREE_CODE (t);
9401 : :
9402 : 104471941 : switch (code)
9403 : : {
9404 : : case INTEGER_CST:
9405 : : case REAL_CST:
9406 : : case VECTOR_CST:
9407 : : case FIXED_CST:
9408 : : case COMPLEX_CST:
9409 : : return t;
9410 : :
9411 : 917957 : case SIZEOF_EXPR:
9412 : 917957 : return fold_sizeof_expr (t);
9413 : :
9414 : 29771782 : case ABS_EXPR:
9415 : 29771782 : case ABSU_EXPR:
9416 : 29771782 : case CONJ_EXPR:
9417 : 29771782 : case REALPART_EXPR:
9418 : 29771782 : case IMAGPART_EXPR:
9419 : 29771782 : case NEGATE_EXPR:
9420 : 29771782 : case BIT_NOT_EXPR:
9421 : 29771782 : case TRUTH_NOT_EXPR:
9422 : 29771782 : case VIEW_CONVERT_EXPR:
9423 : 29771782 : CASE_CONVERT:
9424 : 29771782 : case FLOAT_EXPR:
9425 : 29771782 : case FIX_TRUNC_EXPR:
9426 : 29771782 : case FIXED_CONVERT_EXPR:
9427 : 29771782 : case ADDR_SPACE_CONVERT_EXPR:
9428 : :
9429 : 29771782 : op1 = TREE_OPERAND (t, 0);
9430 : :
9431 : 29771782 : t = const_unop (code, TREE_TYPE (t), op1);
9432 : 29771782 : if (!t)
9433 : : return NULL_TREE;
9434 : :
9435 : 1929211 : if (CONVERT_EXPR_CODE_P (code)
9436 : 1929211 : && TREE_OVERFLOW_P (t) && !TREE_OVERFLOW_P (op1))
9437 : 0 : TREE_OVERFLOW (t) = false;
9438 : : return t;
9439 : :
9440 : : default:
9441 : : return NULL_TREE;
9442 : : }
9443 : : }
9444 : :
9445 : : /* If T is a simple constant expression, returns its simplified value.
9446 : : Otherwise returns T. In contrast to maybe_constant_value we
9447 : : simplify only few operations on constant-expressions, and we don't
9448 : : try to simplify constexpressions. */
9449 : :
9450 : : tree
9451 : 104987504 : fold_simple (tree t)
9452 : : {
9453 : 104987504 : if (processing_template_decl)
9454 : : return t;
9455 : :
9456 : 104471941 : tree r = fold_simple_1 (t);
9457 : 104471941 : if (r)
9458 : : return r;
9459 : :
9460 : : return t;
9461 : : }
9462 : :
9463 : : /* Try folding the expression T to a simple constant.
9464 : : Returns that constant, otherwise returns T. */
9465 : :
9466 : : tree
9467 : 962385 : fold_to_constant (tree t)
9468 : : {
9469 : 962385 : if (processing_template_decl)
9470 : : return t;
9471 : :
9472 : 866833 : tree r = fold (t);
9473 : 866833 : if (CONSTANT_CLASS_P (r) && !TREE_OVERFLOW (r))
9474 : : return r;
9475 : : else
9476 : : return t;
9477 : : }
9478 : :
9479 : : /* If T is a constant expression, returns its reduced value.
9480 : : Otherwise, if T does not have TREE_CONSTANT set, returns T.
9481 : : Otherwise, returns a version of T without TREE_CONSTANT.
9482 : : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated
9483 : : as per P0595. */
9484 : :
9485 : : static GTY((deletable)) hash_map<tree, tree> *cv_cache;
9486 : :
9487 : : tree
9488 : 527671168 : maybe_constant_value (tree t, tree decl /* = NULL_TREE */,
9489 : : mce_value manifestly_const_eval /* = mce_unknown */)
9490 : : {
9491 : 527671168 : tree orig_t = t;
9492 : 527671168 : tree r;
9493 : :
9494 : 527671168 : if (EXPR_P (t) && manifestly_const_eval == mce_unknown)
9495 : : {
9496 : : /* Look up each operand in the cv_cache first to see if we've already
9497 : : reduced it, and reuse that result to avoid quadratic behavior if
9498 : : we're called when building up a large expression. */
9499 : 194727588 : int n = cp_tree_operand_length (t);
9500 : 194727588 : tree *ops = XALLOCAVEC (tree, n);
9501 : 194727588 : bool rebuild = false;
9502 : 553696049 : for (int i = 0; i < n; ++i)
9503 : : {
9504 : 358968461 : ops[i] = TREE_OPERAND (t, i);
9505 : 1076128082 : if (tree *cached = hash_map_safe_get (cv_cache, ops[i]))
9506 : 26044593 : if (*cached != ops[i])
9507 : : {
9508 : 7477115 : ops[i] = *cached;
9509 : 7477115 : rebuild = true;
9510 : : }
9511 : : }
9512 : 194727588 : if (rebuild)
9513 : : {
9514 : 6511031 : t = copy_node (t);
9515 : 21239448 : for (int i = 0; i < n; ++i)
9516 : 14728417 : TREE_OPERAND (t, i) = ops[i];
9517 : : }
9518 : : }
9519 : :
9520 : 527671168 : if (!is_nondependent_constant_expression (t))
9521 : : {
9522 : 0 : if (TREE_OVERFLOW_P (t)
9523 : 116978669 : || (!processing_template_decl && TREE_CONSTANT (t)))
9524 : 4321 : t = mark_non_constant (t);
9525 : 116978669 : return t;
9526 : : }
9527 : 410692499 : else if (CONSTANT_CLASS_P (t))
9528 : : /* No caching or evaluation needed. */
9529 : : return t;
9530 : :
9531 : : /* Don't constant evaluate an unevaluated non-manifestly-constant operand,
9532 : : but at least try folding it to a simple constant. */
9533 : 211951149 : if (cp_unevaluated_operand && manifestly_const_eval != mce_true)
9534 : 444833 : return fold_to_constant (t);
9535 : :
9536 : 207107061 : if (manifestly_const_eval != mce_unknown)
9537 : : /* TODO: Extend the cache to be mce_value aware. And if we have a
9538 : : previously cached mce_unknown result that's TREE_CONSTANT, it means
9539 : : the reduced value is independent of mce_value and so we should
9540 : : be able to reuse it in the mce_true/false case. */
9541 : 95173618 : return cxx_eval_outermost_constant_expr (t, true, true,
9542 : 95170921 : manifestly_const_eval, false, decl);
9543 : :
9544 : 116332698 : if (cv_cache == NULL)
9545 : 136406 : cv_cache = hash_map<tree, tree>::create_ggc (101);
9546 : 116332698 : if (tree *cached = cv_cache->get (t))
9547 : : {
9548 : 9104847 : r = *cached;
9549 : 9104847 : if (r != t)
9550 : : {
9551 : : /* Clear processing_template_decl for sake of break_out_target_exprs;
9552 : : entries in the cv_cache are non-templated. */
9553 : 2644065 : processing_template_decl_sentinel ptds;
9554 : :
9555 : 2644065 : r = break_out_target_exprs (r, /*clear_loc*/true);
9556 : 2644065 : protected_set_expr_location (r, EXPR_LOCATION (t));
9557 : 2644065 : }
9558 : 9104847 : return r;
9559 : : }
9560 : :
9561 : 107227851 : uid_sensitive_constexpr_evaluation_checker c;
9562 : 107227851 : r = cxx_eval_outermost_constant_expr (t, true, true,
9563 : : manifestly_const_eval, false, decl);
9564 : 107227851 : gcc_checking_assert (r == t
9565 : : || CONVERT_EXPR_P (t)
9566 : : || TREE_CODE (t) == VIEW_CONVERT_EXPR
9567 : : || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
9568 : : || !cp_tree_equal (r, t));
9569 : 107227851 : if (!c.evaluation_restricted_p ())
9570 : 107033266 : cv_cache->put (orig_t, r);
9571 : : return r;
9572 : : }
9573 : :
9574 : : /* Dispose of the whole CV_CACHE. */
9575 : :
9576 : : static void
9577 : 24292271 : clear_cv_cache (void)
9578 : : {
9579 : 24292271 : if (cv_cache != NULL)
9580 : 23939162 : cv_cache->empty ();
9581 : 24292271 : }
9582 : :
9583 : : /* Dispose of the whole CV_CACHE and FOLD_CACHE. */
9584 : :
9585 : : void
9586 : 24292271 : clear_cv_and_fold_caches ()
9587 : : {
9588 : 24292271 : clear_cv_cache ();
9589 : 24292271 : clear_fold_cache ();
9590 : 24292271 : }
9591 : :
9592 : : /* Internal function handling expressions in templates for
9593 : : fold_non_dependent_expr and fold_non_dependent_init.
9594 : :
9595 : : If we're in a template, but T isn't value dependent, simplify
9596 : : it. We're supposed to treat:
9597 : :
9598 : : template <typename T> void f(T[1 + 1]);
9599 : : template <typename T> void f(T[2]);
9600 : :
9601 : : as two declarations of the same function, for example. */
9602 : :
9603 : : static tree
9604 : 24863941 : fold_non_dependent_expr_template (tree t, tsubst_flags_t complain,
9605 : : bool manifestly_const_eval,
9606 : : tree object)
9607 : : {
9608 : 24863941 : gcc_assert (processing_template_decl);
9609 : :
9610 : 24863941 : if (is_nondependent_constant_expression (t))
9611 : : {
9612 : 13565373 : processing_template_decl_sentinel s;
9613 : 13565373 : t = instantiate_non_dependent_expr_internal (t, complain);
9614 : :
9615 : 13565373 : if (type_unknown_p (t) || BRACE_ENCLOSED_INITIALIZER_P (t))
9616 : : {
9617 : 0 : if (TREE_OVERFLOW_P (t))
9618 : : {
9619 : 0 : t = build_nop (TREE_TYPE (t), t);
9620 : 0 : TREE_CONSTANT (t) = false;
9621 : : }
9622 : 0 : return t;
9623 : : }
9624 : 13565373 : else if (CONSTANT_CLASS_P (t))
9625 : : /* No evaluation needed. */
9626 : : return t;
9627 : :
9628 : : /* Don't constant evaluate an unevaluated non-manifestly-constant operand,
9629 : : but at least try folding it to a simple constant. */
9630 : 3494854 : if (cp_unevaluated_operand && !manifestly_const_eval)
9631 : 89 : return fold_to_constant (t);
9632 : :
9633 : 3494765 : tree r = cxx_eval_outermost_constant_expr (t, true, true,
9634 : : mce_value (manifestly_const_eval),
9635 : : false, object);
9636 : : /* cp_tree_equal looks through NOPs, so allow them. */
9637 : 3494765 : gcc_checking_assert (r == t
9638 : : || CONVERT_EXPR_P (t)
9639 : : || TREE_CODE (t) == VIEW_CONVERT_EXPR
9640 : : || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
9641 : : || !cp_tree_equal (r, t));
9642 : 3494765 : return r;
9643 : 13565373 : }
9644 : 11298568 : else if (TREE_OVERFLOW_P (t))
9645 : : {
9646 : 0 : t = build_nop (TREE_TYPE (t), t);
9647 : 0 : TREE_CONSTANT (t) = false;
9648 : : }
9649 : :
9650 : : return t;
9651 : : }
9652 : :
9653 : : /* Like maybe_constant_value but first fully instantiate the argument.
9654 : :
9655 : : Note: this is equivalent to instantiate_non_dependent_expr (t, complain)
9656 : : followed by maybe_constant_value but is more efficient,
9657 : : because it calls instantiation_dependent_expression_p and
9658 : : potential_constant_expression at most once.
9659 : : The manifestly_const_eval argument is passed to maybe_constant_value.
9660 : :
9661 : : Callers should generally pass their active complain, or if they are in a
9662 : : non-template, diagnosing context, they can use the default of
9663 : : tf_warning_or_error. Callers that might be within a template context, don't
9664 : : have a complain parameter, and aren't going to remember the result for long
9665 : : (e.g. null_ptr_cst_p), can pass tf_none and deal with error_mark_node
9666 : : appropriately. */
9667 : :
9668 : : tree
9669 : 115797983 : fold_non_dependent_expr (tree t,
9670 : : tsubst_flags_t complain /* = tf_warning_or_error */,
9671 : : bool manifestly_const_eval /* = false */,
9672 : : tree object /* = NULL_TREE */)
9673 : : {
9674 : 115797983 : if (t == NULL_TREE)
9675 : : return NULL_TREE;
9676 : :
9677 : 115309771 : if (processing_template_decl)
9678 : 23883510 : return fold_non_dependent_expr_template (t, complain,
9679 : 23883510 : manifestly_const_eval, object);
9680 : :
9681 : 91426261 : return maybe_constant_value (t, object, mce_value (manifestly_const_eval));
9682 : : }
9683 : :
9684 : : /* Like fold_non_dependent_expr, but if EXPR couldn't be folded to a constant,
9685 : : return the original expression. */
9686 : :
9687 : : tree
9688 : 2012211 : maybe_fold_non_dependent_expr (tree expr,
9689 : : tsubst_flags_t complain/*=tf_warning_or_error*/)
9690 : : {
9691 : 2012211 : tree t = fold_non_dependent_expr (expr, complain);
9692 : 2012211 : if (t && TREE_CONSTANT (t))
9693 : 1003715 : return t;
9694 : :
9695 : : return expr;
9696 : : }
9697 : :
9698 : : /* Like maybe_constant_init but first fully instantiate the argument. */
9699 : :
9700 : : tree
9701 : 25995882 : fold_non_dependent_init (tree t,
9702 : : tsubst_flags_t complain /*=tf_warning_or_error*/,
9703 : : bool manifestly_const_eval /*=false*/,
9704 : : tree object /* = NULL_TREE */)
9705 : : {
9706 : 25995882 : if (t == NULL_TREE)
9707 : : return NULL_TREE;
9708 : :
9709 : 25995882 : if (processing_template_decl)
9710 : : {
9711 : 980431 : t = fold_non_dependent_expr_template (t, complain,
9712 : : manifestly_const_eval, object);
9713 : : /* maybe_constant_init does this stripping, so do it here too. */
9714 : 980431 : if (TREE_CODE (t) == TARGET_EXPR)
9715 : : {
9716 : 61 : tree init = TARGET_EXPR_INITIAL (t);
9717 : 61 : if (TREE_CODE (init) == CONSTRUCTOR)
9718 : 980431 : t = init;
9719 : : }
9720 : 980431 : return t;
9721 : : }
9722 : :
9723 : 25015451 : return maybe_constant_init (t, object, manifestly_const_eval);
9724 : : }
9725 : :
9726 : : /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
9727 : : than wrapped in a TARGET_EXPR.
9728 : : ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
9729 : : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
9730 : : per P0595 even when ALLOW_NON_CONSTANT is true. */
9731 : :
9732 : : static tree
9733 : 59738249 : maybe_constant_init_1 (tree t, tree decl, bool allow_non_constant,
9734 : : mce_value manifestly_const_eval)
9735 : : {
9736 : 59738249 : if (!t)
9737 : : return t;
9738 : 59738249 : if (TREE_CODE (t) == EXPR_STMT)
9739 : 23121 : t = TREE_OPERAND (t, 0);
9740 : 59738249 : if (TREE_CODE (t) == CONVERT_EXPR
9741 : 59738249 : && VOID_TYPE_P (TREE_TYPE (t)))
9742 : 33288 : t = TREE_OPERAND (t, 0);
9743 : : /* If the types don't match, the INIT_EXPR is initializing a subobject of
9744 : : DECL and losing that information would cause mischief later. */
9745 : 59738249 : if (TREE_CODE (t) == INIT_EXPR
9746 : 59738249 : && (!decl
9747 : 10180 : || same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (decl),
9748 : 10180 : TREE_TYPE (t))))
9749 : 24039 : t = TREE_OPERAND (t, 1);
9750 : 59738249 : if (TREE_CODE (t) == TARGET_EXPR)
9751 : 426077 : t = TARGET_EXPR_INITIAL (t);
9752 : 59738249 : if (!is_nondependent_static_init_expression (t))
9753 : : /* Don't try to evaluate it. */;
9754 : 51150621 : else if (CONSTANT_CLASS_P (t) && TREE_CODE (t) != PTRMEM_CST)
9755 : : /* No evaluation needed. PTRMEM_CST needs the immediate fn check. */;
9756 : : else
9757 : : {
9758 : : /* [basic.start.static] allows constant-initialization of variables with
9759 : : static or thread storage duration even if it isn't required, but we
9760 : : shouldn't bend the rules the same way for automatic variables.
9761 : :
9762 : : But still enforce the requirements of constexpr/constinit.
9763 : : [dcl.constinit] "If a variable declared with the constinit specifier
9764 : : has dynamic initialization, the program is ill-formed, even if the
9765 : : implementation would perform that initialization as a static
9766 : : initialization." */
9767 : 11008449 : bool is_static = (decl && DECL_P (decl)
9768 : 35668606 : && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)));
9769 : 1632627 : bool strict = (!is_static
9770 : : || (decl && DECL_P (decl)
9771 : 1632627 : && (DECL_DECLARED_CONSTEXPR_P (decl)
9772 : 872836 : || DECL_DECLARED_CONSTINIT_P (decl))));
9773 : : if (is_static)
9774 : : manifestly_const_eval = mce_true;
9775 : :
9776 : 25062593 : if (cp_unevaluated_operand && manifestly_const_eval != mce_true)
9777 : 35123 : return fold_to_constant (t);
9778 : :
9779 : 25027470 : t = cxx_eval_outermost_constant_expr (t, allow_non_constant, strict,
9780 : : manifestly_const_eval,
9781 : : false, decl);
9782 : : }
9783 : 59703126 : if (TREE_CODE (t) == TARGET_EXPR)
9784 : : {
9785 : 21984 : tree init = TARGET_EXPR_INITIAL (t);
9786 : 21984 : if (TREE_CODE (init) == CONSTRUCTOR)
9787 : 59738249 : t = init;
9788 : : }
9789 : : return t;
9790 : : }
9791 : :
9792 : : /* Wrapper for maybe_constant_init_1 which permits non constants. */
9793 : :
9794 : : tree
9795 : 44796968 : maybe_constant_init (tree t, tree decl, bool manifestly_const_eval)
9796 : : {
9797 : 44796968 : return maybe_constant_init_1 (t, decl, true, mce_value (manifestly_const_eval));
9798 : : }
9799 : :
9800 : : tree
9801 : 14939905 : maybe_constant_init (tree t, tree decl, mce_value manifestly_const_eval)
9802 : : {
9803 : 14939905 : return maybe_constant_init_1 (t, decl, true, manifestly_const_eval);
9804 : : }
9805 : :
9806 : : /* Wrapper for maybe_constant_init_1 which does not permit non constants. */
9807 : :
9808 : : tree
9809 : 1376 : cxx_constant_init (tree t, tree decl)
9810 : : {
9811 : 1376 : return maybe_constant_init_1 (t, decl, false, mce_true);
9812 : : }
9813 : :
9814 : : #if 0
9815 : : /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
9816 : : /* Return true if the object referred to by REF has automatic or thread
9817 : : local storage. */
9818 : :
9819 : : enum { ck_ok, ck_bad, ck_unknown };
9820 : : static int
9821 : : check_automatic_or_tls (tree ref)
9822 : : {
9823 : : machine_mode mode;
9824 : : poly_int64 bitsize, bitpos;
9825 : : tree offset;
9826 : : int volatilep = 0, unsignedp = 0;
9827 : : tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset,
9828 : : &mode, &unsignedp, &volatilep, false);
9829 : : duration_kind dk;
9830 : :
9831 : : /* If there isn't a decl in the middle, we don't know the linkage here,
9832 : : and this isn't a constant expression anyway. */
9833 : : if (!DECL_P (decl))
9834 : : return ck_unknown;
9835 : : dk = decl_storage_duration (decl);
9836 : : return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok;
9837 : : }
9838 : : #endif
9839 : :
9840 : : /* Data structure for passing data from potential_constant_expression_1
9841 : : to check_for_return_continue via cp_walk_tree. */
9842 : : struct check_for_return_continue_data {
9843 : : hash_set<tree> *pset;
9844 : : tree continue_stmt;
9845 : : tree break_stmt;
9846 : : };
9847 : :
9848 : : /* Helper function for potential_constant_expression_1 SWITCH_STMT handling,
9849 : : called through cp_walk_tree. Return the first RETURN_EXPR found, or note
9850 : : the first CONTINUE_STMT and/or BREAK_STMT if RETURN_EXPR is not found. */
9851 : : static tree
9852 : 25743377 : check_for_return_continue (tree *tp, int *walk_subtrees, void *data)
9853 : : {
9854 : 25743377 : tree t = *tp, s, b;
9855 : 25743377 : check_for_return_continue_data *d = (check_for_return_continue_data *) data;
9856 : 25743377 : switch (TREE_CODE (t))
9857 : : {
9858 : : case RETURN_EXPR:
9859 : : return t;
9860 : :
9861 : 30 : case CONTINUE_STMT:
9862 : 30 : if (d->continue_stmt == NULL_TREE)
9863 : 30 : d->continue_stmt = t;
9864 : : break;
9865 : :
9866 : 6434 : case BREAK_STMT:
9867 : 6434 : if (d->break_stmt == NULL_TREE)
9868 : 5506 : d->break_stmt = t;
9869 : : break;
9870 : :
9871 : : #define RECUR(x) \
9872 : : if (tree r = cp_walk_tree (&x, check_for_return_continue, data, \
9873 : : d->pset)) \
9874 : : return r
9875 : :
9876 : : /* For loops, walk subtrees manually, so that continue stmts found
9877 : : inside of the bodies of the loops are ignored. */
9878 : 12979 : case DO_STMT:
9879 : 12979 : *walk_subtrees = 0;
9880 : 12979 : RECUR (DO_COND (t));
9881 : 12979 : s = d->continue_stmt;
9882 : 12979 : b = d->break_stmt;
9883 : 12979 : RECUR (DO_BODY (t));
9884 : 12979 : d->continue_stmt = s;
9885 : 12979 : d->break_stmt = b;
9886 : 12979 : break;
9887 : :
9888 : 105 : case WHILE_STMT:
9889 : 105 : *walk_subtrees = 0;
9890 : 105 : RECUR (WHILE_COND_PREP (t));
9891 : 105 : RECUR (WHILE_COND (t));
9892 : 105 : s = d->continue_stmt;
9893 : 105 : b = d->break_stmt;
9894 : 105 : RECUR (WHILE_BODY (t));
9895 : 98 : d->continue_stmt = s;
9896 : 98 : d->break_stmt = b;
9897 : 98 : break;
9898 : :
9899 : 102 : case FOR_STMT:
9900 : 102 : *walk_subtrees = 0;
9901 : 102 : RECUR (FOR_INIT_STMT (t));
9902 : 102 : RECUR (FOR_COND_PREP (t));
9903 : 102 : RECUR (FOR_COND (t));
9904 : 102 : RECUR (FOR_EXPR (t));
9905 : 102 : s = d->continue_stmt;
9906 : 102 : b = d->break_stmt;
9907 : 102 : RECUR (FOR_BODY (t));
9908 : 64 : d->continue_stmt = s;
9909 : 64 : d->break_stmt = b;
9910 : 64 : break;
9911 : :
9912 : 0 : case RANGE_FOR_STMT:
9913 : 0 : *walk_subtrees = 0;
9914 : 0 : RECUR (RANGE_FOR_EXPR (t));
9915 : 0 : s = d->continue_stmt;
9916 : 0 : b = d->break_stmt;
9917 : 0 : RECUR (RANGE_FOR_BODY (t));
9918 : 0 : d->continue_stmt = s;
9919 : 0 : d->break_stmt = b;
9920 : 0 : break;
9921 : :
9922 : 193 : case SWITCH_STMT:
9923 : 193 : *walk_subtrees = 0;
9924 : 193 : RECUR (SWITCH_STMT_COND (t));
9925 : 193 : b = d->break_stmt;
9926 : 193 : RECUR (SWITCH_STMT_BODY (t));
9927 : 173 : d->break_stmt = b;
9928 : 173 : break;
9929 : : #undef RECUR
9930 : :
9931 : : case STATEMENT_LIST:
9932 : : case CONSTRUCTOR:
9933 : : break;
9934 : :
9935 : 24901452 : default:
9936 : 24901452 : if (!EXPR_P (t))
9937 : 7268956 : *walk_subtrees = 0;
9938 : : break;
9939 : : }
9940 : :
9941 : : return NULL_TREE;
9942 : : }
9943 : :
9944 : : /* Return true if T denotes a potentially constant expression. Issue
9945 : : diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
9946 : : an lvalue-rvalue conversion is implied. If NOW is true, we want to
9947 : : consider the expression in the current context, independent of constexpr
9948 : : substitution. If FUNDEF_P is true, we're checking a constexpr function body
9949 : : and hard errors should not be reported by constexpr_error.
9950 : :
9951 : : C++0x [expr.const] used to say
9952 : :
9953 : : 6 An expression is a potential constant expression if it is
9954 : : a constant expression where all occurrences of function
9955 : : parameters are replaced by arbitrary constant expressions
9956 : : of the appropriate type.
9957 : :
9958 : : 2 A conditional expression is a constant expression unless it
9959 : : involves one of the following as a potentially evaluated
9960 : : subexpression (3.2), but subexpressions of logical AND (5.14),
9961 : : logical OR (5.15), and conditional (5.16) operations that are
9962 : : not evaluated are not considered. */
9963 : :
9964 : : static bool
9965 : 3016862276 : potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
9966 : : bool fundef_p, tsubst_flags_t flags,
9967 : : tree *jump_target)
9968 : : {
9969 : : #define RECUR(T,RV) \
9970 : : potential_constant_expression_1 ((T), (RV), strict, now, fundef_p, flags, \
9971 : : jump_target)
9972 : :
9973 : 3016862276 : enum { any = false, rval = true };
9974 : 3016862276 : int i;
9975 : 3016862276 : tree tmp;
9976 : :
9977 : 3016862276 : if (t == error_mark_node)
9978 : : return false;
9979 : 3016852341 : if (t == NULL_TREE)
9980 : : return true;
9981 : 3011398103 : location_t loc = cp_expr_loc_or_input_loc (t);
9982 : :
9983 : 3011398103 : if (*jump_target)
9984 : : /* If we are jumping, ignore everything. This is simpler than the
9985 : : cxx_eval_constant_expression handling because we only need to be
9986 : : conservatively correct, and we don't necessarily have a constant value
9987 : : available, so we don't bother with switch tracking. */
9988 : : return true;
9989 : :
9990 : 846004 : if (TREE_THIS_VOLATILE (t) && want_rval
9991 : 337111 : && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (t))
9992 : 3008406045 : && !NULLPTR_TYPE_P (TREE_TYPE (t)))
9993 : : {
9994 : 77545 : if (flags & tf_error)
9995 : 21 : constexpr_error (loc, fundef_p, "lvalue-to-rvalue conversion of "
9996 : : "a volatile lvalue %qE with type %qT", t,
9997 : 21 : TREE_TYPE (t));
9998 : 77545 : return false;
9999 : : }
10000 : 3008250929 : if (CONSTANT_CLASS_P (t))
10001 : : return true;
10002 : 2290731012 : if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED)
10003 : 2290731012 : && TREE_TYPE (t) == error_mark_node)
10004 : : return false;
10005 : :
10006 : 2290730987 : switch (TREE_CODE (t))
10007 : : {
10008 : : case FUNCTION_DECL:
10009 : : case BASELINK:
10010 : : case TEMPLATE_DECL:
10011 : : case OVERLOAD:
10012 : : case TEMPLATE_ID_EXPR:
10013 : : case LABEL_DECL:
10014 : : case CASE_LABEL_EXPR:
10015 : : case PREDICT_EXPR:
10016 : : case CONST_DECL:
10017 : : case SIZEOF_EXPR:
10018 : : case ALIGNOF_EXPR:
10019 : : case OFFSETOF_EXPR:
10020 : : case NOEXCEPT_EXPR:
10021 : : case TEMPLATE_PARM_INDEX:
10022 : : case TRAIT_EXPR:
10023 : : case IDENTIFIER_NODE:
10024 : : case USERDEF_LITERAL:
10025 : : /* We can see a FIELD_DECL in a pointer-to-member expression. */
10026 : : case FIELD_DECL:
10027 : : case RESULT_DECL:
10028 : : case USING_DECL:
10029 : : case USING_STMT:
10030 : : case PLACEHOLDER_EXPR:
10031 : : case REQUIRES_EXPR:
10032 : : case STATIC_ASSERT:
10033 : : case DEBUG_BEGIN_STMT:
10034 : : return true;
10035 : :
10036 : 14796673 : case RETURN_EXPR:
10037 : 14796673 : if (!RECUR (TREE_OPERAND (t, 0), any))
10038 : : return false;
10039 : : /* FALLTHROUGH */
10040 : :
10041 : 14667121 : case BREAK_STMT:
10042 : 14667121 : case CONTINUE_STMT:
10043 : 14667121 : *jump_target = t;
10044 : 14667121 : return true;
10045 : :
10046 : 231388806 : case PARM_DECL:
10047 : 231388806 : if (now && want_rval)
10048 : : {
10049 : 70552187 : tree type = TREE_TYPE (t);
10050 : 70552187 : if (dependent_type_p (type)
10051 : 46937329 : || !COMPLETE_TYPE_P (processing_template_decl
10052 : : ? type : complete_type (type))
10053 : 117489514 : || is_really_empty_class (type, /*ignore_vptr*/false))
10054 : : /* An empty class has no data to read. */
10055 : 23615172 : return true;
10056 : 46937015 : if (flags & tf_error)
10057 : 15 : constexpr_error (input_location, fundef_p,
10058 : : "%qE is not a constant expression", t);
10059 : 46937015 : return false;
10060 : : }
10061 : : return true;
10062 : :
10063 : 176952365 : case AGGR_INIT_EXPR:
10064 : 176952365 : case CALL_EXPR:
10065 : : /* -- an invocation of a function other than a constexpr function
10066 : : or a constexpr constructor. */
10067 : 176952365 : {
10068 : 176952365 : tree fun = get_function_named_in_call (t);
10069 : 176952365 : const int nargs = call_expr_nargs (t);
10070 : 176952365 : i = 0;
10071 : :
10072 : 176952365 : if (fun == NULL_TREE)
10073 : : {
10074 : : /* Reset to allow the function to continue past the end
10075 : : of the block below. Otherwise return early. */
10076 : 95946 : bool bail = true;
10077 : :
10078 : 95946 : if (TREE_CODE (t) == CALL_EXPR
10079 : 95946 : && CALL_EXPR_FN (t) == NULL_TREE)
10080 : 95946 : switch (CALL_EXPR_IFN (t))
10081 : : {
10082 : : /* These should be ignored, they are optimized away from
10083 : : constexpr functions. */
10084 : : case IFN_UBSAN_NULL:
10085 : : case IFN_UBSAN_BOUNDS:
10086 : : case IFN_UBSAN_VPTR:
10087 : : case IFN_FALLTHROUGH:
10088 : : case IFN_ASSUME:
10089 : : return true;
10090 : :
10091 : : case IFN_ADD_OVERFLOW:
10092 : : case IFN_SUB_OVERFLOW:
10093 : : case IFN_MUL_OVERFLOW:
10094 : : case IFN_LAUNDER:
10095 : : case IFN_VEC_CONVERT:
10096 : : bail = false;
10097 : : break;
10098 : :
10099 : : default:
10100 : : break;
10101 : : }
10102 : :
10103 : : if (bail)
10104 : : {
10105 : : /* fold_call_expr can't do anything with IFN calls. */
10106 : 44 : if (flags & tf_error)
10107 : 0 : constexpr_error (loc, fundef_p,
10108 : : "call to internal function %qE", t);
10109 : 44 : return false;
10110 : : }
10111 : : }
10112 : :
10113 : 176856419 : if (fun && is_overloaded_fn (fun))
10114 : : {
10115 : 160697170 : if (!RECUR (fun, true))
10116 : : return false;
10117 : 160292983 : fun = get_fns (fun);
10118 : :
10119 : 160292983 : if (TREE_CODE (fun) == FUNCTION_DECL)
10120 : : {
10121 : 139689124 : if (builtin_valid_in_constant_expr_p (fun))
10122 : : return true;
10123 : 138936891 : if (!maybe_constexpr_fn (fun)
10124 : : /* Allow any built-in function; if the expansion
10125 : : isn't constant, we'll deal with that then. */
10126 : 41609314 : && !fndecl_built_in_p (fun)
10127 : : /* In C++20, replaceable global allocation functions
10128 : : are constant expressions. */
10129 : 29759472 : && (!cxx_replaceable_global_alloc_fn (fun)
10130 : 150817 : || TREE_CODE (t) != CALL_EXPR
10131 : 150817 : || (!CALL_FROM_NEW_OR_DELETE_P (t)
10132 : 69403 : && (current_function_decl == NULL_TREE
10133 : 69403 : || !is_std_allocator_allocate
10134 : 69403 : (current_function_decl))))
10135 : : /* Allow placement new in std::construct_at. */
10136 : 29615503 : && (!cxx_placement_new_fn (fun)
10137 : 49694 : || TREE_CODE (t) != CALL_EXPR
10138 : 49694 : || current_function_decl == NULL_TREE
10139 : 49688 : || !is_std_construct_at (current_function_decl))
10140 : 168519756 : && !cxx_dynamic_cast_fn_p (fun))
10141 : : {
10142 : 29580897 : if ((flags & tf_error)
10143 : 29580897 : && constexpr_error (loc, fundef_p,
10144 : : "call to non-%<constexpr%> "
10145 : : "function %qD", fun))
10146 : 174 : explain_invalid_constexpr_fn (fun);
10147 : 29580897 : return false;
10148 : : }
10149 : : }
10150 : :
10151 : 129959853 : fun = OVL_FIRST (fun);
10152 : : /* Skip initial arguments to base constructors. */
10153 : 129959853 : if (DECL_BASE_CONSTRUCTOR_P (fun))
10154 : 2269756 : i = num_artificial_parms_for (fun);
10155 : : }
10156 : 16159249 : else if (fun)
10157 : : {
10158 : 16159249 : if (TREE_TYPE (fun)
10159 : 16159249 : && FUNCTION_POINTER_TYPE_P (TREE_TYPE (fun)))
10160 : : want_rval = rval;
10161 : : else
10162 : : want_rval = any;
10163 : 16159249 : if (RECUR (fun, want_rval))
10164 : : /* Might end up being a constant function pointer. But it
10165 : : could also be a function object with constexpr op(), so
10166 : : we pass 'any' so that the underlying VAR_DECL is deemed
10167 : : as potentially-constant even though it wasn't declared
10168 : : constexpr. */;
10169 : : else
10170 : : return false;
10171 : : }
10172 : 290280855 : for (; i < nargs; ++i)
10173 : : {
10174 : 158463366 : tree x = get_nth_callarg (t, i);
10175 : : /* In a template, reference arguments haven't been converted to
10176 : : REFERENCE_TYPE and we might not even know if the parameter
10177 : : is a reference, so accept lvalue constants too. */
10178 : 158463366 : bool rv = processing_template_decl ? any : rval;
10179 : : /* Don't require an immediately constant value, as constexpr
10180 : : substitution might not use the value of the argument. */
10181 : 158463366 : bool sub_now = false;
10182 : 158463366 : if (!potential_constant_expression_1 (x, rv, strict,
10183 : : sub_now, fundef_p, flags,
10184 : : jump_target))
10185 : : return false;
10186 : : }
10187 : : return true;
10188 : : }
10189 : :
10190 : 125719364 : case NON_LVALUE_EXPR:
10191 : : /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
10192 : : -- an lvalue of integral type that refers to a non-volatile
10193 : : const variable or static data member initialized with
10194 : : constant expressions, or
10195 : :
10196 : : -- an lvalue of literal type that refers to non-volatile
10197 : : object defined with constexpr, or that refers to a
10198 : : sub-object of such an object; */
10199 : 125719364 : return RECUR (TREE_OPERAND (t, 0), rval);
10200 : :
10201 : 31310 : case EXCESS_PRECISION_EXPR:
10202 : 31310 : return RECUR (TREE_OPERAND (t, 0), rval);
10203 : :
10204 : 227602537 : case VAR_DECL:
10205 : 227602537 : if (DECL_HAS_VALUE_EXPR_P (t))
10206 : : {
10207 : 3460105 : if (now && is_normal_capture_proxy (t))
10208 : : {
10209 : : /* -- in a lambda-expression, a reference to this or to a
10210 : : variable with automatic storage duration defined outside that
10211 : : lambda-expression, where the reference would be an
10212 : : odr-use. */
10213 : :
10214 : 401180 : if (want_rval)
10215 : : /* Since we're doing an lvalue-rvalue conversion, this might
10216 : : not be an odr-use, so evaluate the variable directly. */
10217 : 400553 : return RECUR (DECL_CAPTURED_VARIABLE (t), rval);
10218 : :
10219 : 627 : if (flags & tf_error)
10220 : : {
10221 : 3 : tree cap = DECL_CAPTURED_VARIABLE (t);
10222 : 3 : auto_diagnostic_group d;
10223 : 3 : if (constexpr_error (input_location, fundef_p,
10224 : : "lambda capture of %qE is not a "
10225 : : "constant expression", cap)
10226 : 3 : && decl_constant_var_p (cap))
10227 : 3 : inform (input_location, "because it is used as a glvalue");
10228 : 3 : }
10229 : 627 : return false;
10230 : : }
10231 : : /* Treat __PRETTY_FUNCTION__ inside a template function as
10232 : : potentially-constant. */
10233 : 6116687 : else if (DECL_PRETTY_FUNCTION_P (t)
10234 : 3165740 : && DECL_VALUE_EXPR (t) == error_mark_node)
10235 : : return true;
10236 : 3058920 : return RECUR (DECL_VALUE_EXPR (t), rval);
10237 : : }
10238 : 224142432 : if (want_rval
10239 : 152541215 : && (now || !var_in_maybe_constexpr_fn (t))
10240 : 143372645 : && !type_dependent_expression_p (t)
10241 : 112446951 : && !decl_maybe_constant_var_p (t)
10242 : 49446526 : && (strict
10243 : 1217007 : || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t))
10244 : 397139 : || (DECL_INITIAL (t)
10245 : 396631 : && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t)))
10246 : 49445339 : && COMPLETE_TYPE_P (TREE_TYPE (t))
10247 : 273573065 : && !is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
10248 : : {
10249 : 49426769 : if (flags & tf_error)
10250 : 148 : non_const_var_error (loc, t, fundef_p);
10251 : 49426769 : return false;
10252 : : }
10253 : : return true;
10254 : :
10255 : 239569467 : case NOP_EXPR:
10256 : 239569467 : if (REINTERPRET_CAST_P (t))
10257 : : {
10258 : 128716 : if (flags & tf_error)
10259 : 47 : constexpr_error (loc, fundef_p, "%<reinterpret_cast%> is not a "
10260 : : "constant expression");
10261 : 128716 : return false;
10262 : : }
10263 : : /* FALLTHRU */
10264 : 558246583 : case CONVERT_EXPR:
10265 : 558246583 : case VIEW_CONVERT_EXPR:
10266 : : /* -- a reinterpret_cast. FIXME not implemented, and this rule
10267 : : may change to something more specific to type-punning (DR 1312). */
10268 : 558246583 : {
10269 : 558246583 : tree from = TREE_OPERAND (t, 0);
10270 : 558246583 : if (location_wrapper_p (t))
10271 : : {
10272 : 281674046 : iloc_sentinel ils = loc;
10273 : 281674046 : return (RECUR (from, want_rval));
10274 : 281674046 : }
10275 : 276572537 : if (INDIRECT_TYPE_P (TREE_TYPE (t)))
10276 : : {
10277 : 142929930 : STRIP_ANY_LOCATION_WRAPPER (from);
10278 : 142929930 : if (TREE_CODE (from) == INTEGER_CST
10279 : 142929930 : && !integer_zerop (from))
10280 : : {
10281 : 1694 : if (flags & tf_error)
10282 : 22 : constexpr_error (loc, fundef_p,
10283 : : "%<reinterpret_cast%> from integer to "
10284 : : "pointer");
10285 : 1694 : return false;
10286 : : }
10287 : : }
10288 : 276570843 : return (RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR));
10289 : : }
10290 : :
10291 : 11538 : case ADDRESSOF_EXPR:
10292 : : /* This is like ADDR_EXPR, except it won't form pointer-to-member. */
10293 : 11538 : t = TREE_OPERAND (t, 0);
10294 : 11538 : goto handle_addr_expr;
10295 : :
10296 : 53207804 : case ADDR_EXPR:
10297 : : /* -- a unary operator & that is applied to an lvalue that
10298 : : designates an object with thread or automatic storage
10299 : : duration; */
10300 : 53207804 : t = TREE_OPERAND (t, 0);
10301 : :
10302 : 53207804 : if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t))
10303 : : /* A pointer-to-member constant. */
10304 : : return true;
10305 : :
10306 : 53219104 : handle_addr_expr:
10307 : : #if 0
10308 : : /* FIXME adjust when issue 1197 is fully resolved. For now don't do
10309 : : any checking here, as we might dereference the pointer later. If
10310 : : we remove this code, also remove check_automatic_or_tls. */
10311 : : i = check_automatic_or_tls (t);
10312 : : if (i == ck_ok)
10313 : : return true;
10314 : : if (i == ck_bad)
10315 : : {
10316 : : if (flags & tf_error)
10317 : : error ("address-of an object %qE with thread local or "
10318 : : "automatic storage is not a constant expression", t);
10319 : : return false;
10320 : : }
10321 : : #endif
10322 : 53219104 : return RECUR (t, any);
10323 : :
10324 : 69829836 : case COMPONENT_REF:
10325 : 69829836 : case ARROW_EXPR:
10326 : 69829836 : case OFFSET_REF:
10327 : : /* -- a class member access unless its postfix-expression is
10328 : : of literal type or of pointer to literal type. */
10329 : : /* This test would be redundant, as it follows from the
10330 : : postfix-expression being a potential constant expression. */
10331 : 69829836 : if (type_unknown_p (t))
10332 : : return true;
10333 : 63658388 : if (is_overloaded_fn (t))
10334 : : /* In a template, a COMPONENT_REF of a function expresses ob.fn(),
10335 : : which uses ob as an lvalue. */
10336 : 65150933 : want_rval = false;
10337 : 65150933 : gcc_fallthrough ();
10338 : :
10339 : 65150933 : case REALPART_EXPR:
10340 : 65150933 : case IMAGPART_EXPR:
10341 : 65150933 : case BIT_FIELD_REF:
10342 : 65150933 : return RECUR (TREE_OPERAND (t, 0), want_rval);
10343 : :
10344 : 205847 : case EXPR_PACK_EXPANSION:
10345 : 205847 : return RECUR (PACK_EXPANSION_PATTERN (t), want_rval);
10346 : :
10347 : : case PACK_INDEX_EXPR:
10348 : : return true;
10349 : :
10350 : 68771614 : case INDIRECT_REF:
10351 : 68771614 : return RECUR (TREE_OPERAND (t, 0), rval);
10352 : :
10353 : 15112983 : case STATEMENT_LIST:
10354 : 52833120 : for (tree stmt : tsi_range (t))
10355 : 38225851 : if (!RECUR (stmt, any))
10356 : 247010278 : return false;
10357 : : return true;
10358 : :
10359 : 3611920 : case MODIFY_EXPR:
10360 : 3611920 : if (cxx_dialect < cxx14)
10361 : 1921 : goto fail;
10362 : 3609999 : if (!RECUR (TREE_OPERAND (t, 0), any))
10363 : : return false;
10364 : : /* Just ignore clobbers. */
10365 : 3195468 : if (TREE_CLOBBER_P (TREE_OPERAND (t, 1)))
10366 : : return true;
10367 : 1575935 : if (!RECUR (TREE_OPERAND (t, 1), rval))
10368 : : return false;
10369 : : return true;
10370 : :
10371 : 293544 : case MODOP_EXPR:
10372 : 293544 : if (cxx_dialect < cxx14)
10373 : 96 : goto fail;
10374 : 293448 : if (!RECUR (TREE_OPERAND (t, 0), rval))
10375 : : return false;
10376 : 282318 : if (!RECUR (TREE_OPERAND (t, 2), rval))
10377 : : return false;
10378 : : return true;
10379 : :
10380 : 280961 : case DO_STMT:
10381 : 280961 : if (!RECUR (DO_COND (t), rval))
10382 : : return false;
10383 : 280961 : if (!RECUR (DO_BODY (t), any))
10384 : : return false;
10385 : 280838 : if (breaks (jump_target) || continues (jump_target))
10386 : 3 : *jump_target = NULL_TREE;
10387 : : return true;
10388 : :
10389 : 216053 : case FOR_STMT:
10390 : 216053 : if (!RECUR (FOR_INIT_STMT (t), any))
10391 : : return false;
10392 : 216053 : if (!RECUR (FOR_COND_PREP (t), any))
10393 : : return false;
10394 : 216053 : tmp = FOR_COND (t);
10395 : 216053 : if (!RECUR (tmp, rval))
10396 : : return false;
10397 : 215317 : if (tmp)
10398 : : {
10399 : 171698 : if (!processing_template_decl)
10400 : 167602 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
10401 : : /* If we couldn't evaluate the condition, it might not ever be
10402 : : true. */
10403 : 171698 : if (!integer_onep (tmp))
10404 : : {
10405 : : /* Before returning true, check if the for body can contain
10406 : : a return. */
10407 : 171698 : hash_set<tree> pset;
10408 : 171698 : check_for_return_continue_data data = { &pset, NULL_TREE,
10409 : 171698 : NULL_TREE };
10410 : 171698 : if (tree ret_expr
10411 : 171698 : = cp_walk_tree (&FOR_BODY (t), check_for_return_continue,
10412 : : &data, &pset))
10413 : 98467 : *jump_target = ret_expr;
10414 : 171698 : return true;
10415 : 171698 : }
10416 : : }
10417 : 43619 : if (!RECUR (FOR_EXPR (t), any))
10418 : : return false;
10419 : 43619 : if (!RECUR (FOR_BODY (t), any))
10420 : : return false;
10421 : 43619 : if (!RECUR (FOR_COND_CLEANUP (t), any))
10422 : : return false;
10423 : 43619 : if (breaks (jump_target) || continues (jump_target))
10424 : 3 : *jump_target = NULL_TREE;
10425 : : return true;
10426 : :
10427 : 21 : case RANGE_FOR_STMT:
10428 : 21 : if (!RECUR (RANGE_FOR_INIT_STMT (t), any))
10429 : : return false;
10430 : 21 : if (!RECUR (RANGE_FOR_EXPR (t), any))
10431 : : return false;
10432 : 21 : if (!RECUR (RANGE_FOR_BODY (t), any))
10433 : : return false;
10434 : 15 : if (breaks (jump_target) || continues (jump_target))
10435 : 0 : *jump_target = NULL_TREE;
10436 : : return true;
10437 : :
10438 : 100685 : case WHILE_STMT:
10439 : 100685 : if (!RECUR (WHILE_COND_PREP (t), any))
10440 : : return false;
10441 : 100685 : tmp = WHILE_COND (t);
10442 : 100685 : if (!RECUR (tmp, rval))
10443 : : return false;
10444 : 100555 : if (!processing_template_decl)
10445 : 100539 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
10446 : : /* If we couldn't evaluate the condition, it might not ever be true. */
10447 : 100555 : if (!integer_onep (tmp))
10448 : : {
10449 : : /* Before returning true, check if the while body can contain
10450 : : a return. */
10451 : 97856 : hash_set<tree> pset;
10452 : 97856 : check_for_return_continue_data data = { &pset, NULL_TREE,
10453 : 97856 : NULL_TREE };
10454 : 97856 : if (tree ret_expr
10455 : 97856 : = cp_walk_tree (&WHILE_BODY (t), check_for_return_continue,
10456 : : &data, &pset))
10457 : 197 : *jump_target = ret_expr;
10458 : 97856 : return true;
10459 : 97856 : }
10460 : 2699 : if (!RECUR (WHILE_BODY (t), any))
10461 : : return false;
10462 : 2695 : if (!RECUR (WHILE_COND_CLEANUP (t), any))
10463 : : return false;
10464 : 2695 : if (breaks (jump_target) || continues (jump_target))
10465 : 30 : *jump_target = NULL_TREE;
10466 : : return true;
10467 : :
10468 : 20164 : case SWITCH_STMT:
10469 : 20164 : if (!RECUR (SWITCH_STMT_COND (t), rval))
10470 : : return false;
10471 : : /* FIXME we don't check SWITCH_STMT_BODY currently, because even
10472 : : unreachable labels would be checked and it is enough if there is
10473 : : a single switch cond value for which it is a valid constant
10474 : : expression. We need to check if there are any RETURN_EXPRs
10475 : : or CONTINUE_STMTs inside of the body though, as in that case
10476 : : we need to set *jump_target. */
10477 : : else
10478 : : {
10479 : 20158 : hash_set<tree> pset;
10480 : 20158 : check_for_return_continue_data data = { &pset, NULL_TREE,
10481 : 20158 : NULL_TREE };
10482 : 20158 : if (tree ret_expr
10483 : 20158 : = cp_walk_tree (&SWITCH_STMT_BODY (t), check_for_return_continue,
10484 : : &data, &pset))
10485 : : /* The switch might return. */
10486 : 19942 : *jump_target = ret_expr;
10487 : 216 : else if (data.continue_stmt)
10488 : : /* The switch can't return, but might continue. */
10489 : 3 : *jump_target = data.continue_stmt;
10490 : 20158 : }
10491 : 20158 : return true;
10492 : :
10493 : 44 : case STMT_EXPR:
10494 : 44 : return RECUR (STMT_EXPR_STMT (t), rval);
10495 : :
10496 : 249249 : case LAMBDA_EXPR:
10497 : 249249 : if (cxx_dialect >= cxx17)
10498 : : /* In C++17 lambdas can be constexpr, don't give up yet. */
10499 : : return true;
10500 : 212 : else if (flags & tf_error)
10501 : 0 : constexpr_error (loc, fundef_p, "lambda-expression is not a "
10502 : : "constant expression before C++17");
10503 : : return false;
10504 : :
10505 : 105805 : case NEW_EXPR:
10506 : 105805 : case VEC_NEW_EXPR:
10507 : 105805 : case DELETE_EXPR:
10508 : 105805 : case VEC_DELETE_EXPR:
10509 : 105805 : if (cxx_dialect >= cxx20)
10510 : : /* In C++20, new-expressions are potentially constant. */
10511 : : return true;
10512 : 78909 : else if (flags & tf_error)
10513 : 0 : constexpr_error (loc, fundef_p, "new-expression is not a "
10514 : : "constant expression before C++20");
10515 : : return false;
10516 : :
10517 : 65300 : case DYNAMIC_CAST_EXPR:
10518 : 65300 : case PSEUDO_DTOR_EXPR:
10519 : 65300 : case THROW_EXPR:
10520 : 65300 : case OMP_PARALLEL:
10521 : 65300 : case OMP_TASK:
10522 : 65300 : case OMP_FOR:
10523 : 65300 : case OMP_SIMD:
10524 : 65300 : case OMP_DISTRIBUTE:
10525 : 65300 : case OMP_TASKLOOP:
10526 : 65300 : case OMP_LOOP:
10527 : 65300 : case OMP_TEAMS:
10528 : 65300 : case OMP_TARGET_DATA:
10529 : 65300 : case OMP_TARGET:
10530 : 65300 : case OMP_SECTIONS:
10531 : 65300 : case OMP_ORDERED:
10532 : 65300 : case OMP_CRITICAL:
10533 : 65300 : case OMP_SINGLE:
10534 : 65300 : case OMP_SCAN:
10535 : 65300 : case OMP_SCOPE:
10536 : 65300 : case OMP_SECTION:
10537 : 65300 : case OMP_MASTER:
10538 : 65300 : case OMP_MASKED:
10539 : 65300 : case OMP_TASKGROUP:
10540 : 65300 : case OMP_TARGET_UPDATE:
10541 : 65300 : case OMP_TARGET_ENTER_DATA:
10542 : 65300 : case OMP_TARGET_EXIT_DATA:
10543 : 65300 : case OMP_ATOMIC:
10544 : 65300 : case OMP_ATOMIC_READ:
10545 : 65300 : case OMP_ATOMIC_CAPTURE_OLD:
10546 : 65300 : case OMP_ATOMIC_CAPTURE_NEW:
10547 : 65300 : case OMP_DEPOBJ:
10548 : 65300 : case OACC_PARALLEL:
10549 : 65300 : case OACC_KERNELS:
10550 : 65300 : case OACC_SERIAL:
10551 : 65300 : case OACC_DATA:
10552 : 65300 : case OACC_HOST_DATA:
10553 : 65300 : case OACC_LOOP:
10554 : 65300 : case OACC_CACHE:
10555 : 65300 : case OACC_DECLARE:
10556 : 65300 : case OACC_ENTER_DATA:
10557 : 65300 : case OACC_EXIT_DATA:
10558 : 65300 : case OACC_UPDATE:
10559 : 65300 : case OMP_ARRAY_SECTION:
10560 : : /* GCC internal stuff. */
10561 : 65300 : case VA_ARG_EXPR:
10562 : 65300 : case TRANSACTION_EXPR:
10563 : 65300 : case AT_ENCODE_EXPR:
10564 : 65300 : fail:
10565 : 65300 : if (flags & tf_error)
10566 : 21 : constexpr_error (loc, fundef_p, "expression %qE is not a constant "
10567 : : "expression", t);
10568 : : return false;
10569 : :
10570 : : case OMP_DECLARE_MAPPER:
10571 : : /* This can be used to initialize VAR_DECLs: it's treated as a magic
10572 : : constant. */
10573 : : return true;
10574 : :
10575 : 475 : case ASM_EXPR:
10576 : 475 : if (flags & tf_error)
10577 : 3 : inline_asm_in_constexpr_error (loc, fundef_p);
10578 : : return false;
10579 : :
10580 : 362598 : case OBJ_TYPE_REF:
10581 : 362598 : if (cxx_dialect >= cxx20)
10582 : : /* In C++20 virtual calls can be constexpr, don't give up yet. */
10583 : : return true;
10584 : 223981 : else if (flags & tf_error)
10585 : 0 : constexpr_error (loc, fundef_p, "virtual functions cannot be "
10586 : : "%<constexpr%> before C++20");
10587 : : return false;
10588 : :
10589 : 3371 : case TYPEID_EXPR:
10590 : : /* In C++20, a typeid expression whose operand is of polymorphic
10591 : : class type can be constexpr. */
10592 : 3371 : {
10593 : 3371 : tree e = TREE_OPERAND (t, 0);
10594 : 3371 : if (cxx_dialect < cxx20
10595 : 511 : && strict
10596 : 511 : && !TYPE_P (e)
10597 : 271 : && !type_dependent_expression_p (e)
10598 : 3377 : && TYPE_POLYMORPHIC_P (TREE_TYPE (e)))
10599 : : {
10600 : 4 : if (flags & tf_error)
10601 : 1 : constexpr_error (loc, fundef_p, "%<typeid%> is not a "
10602 : : "constant expression because %qE is "
10603 : : "of polymorphic type", e);
10604 : 4 : return false;
10605 : : }
10606 : : return true;
10607 : : }
10608 : :
10609 : 24940252 : case POINTER_DIFF_EXPR:
10610 : 24940252 : case MINUS_EXPR:
10611 : 24940252 : want_rval = true;
10612 : 24940252 : goto binary;
10613 : :
10614 : 59646897 : case LT_EXPR:
10615 : 59646897 : case LE_EXPR:
10616 : 59646897 : case GT_EXPR:
10617 : 59646897 : case GE_EXPR:
10618 : 59646897 : case EQ_EXPR:
10619 : 59646897 : case NE_EXPR:
10620 : 59646897 : case SPACESHIP_EXPR:
10621 : 59646897 : want_rval = true;
10622 : 59646897 : goto binary;
10623 : :
10624 : 1308195 : case PREINCREMENT_EXPR:
10625 : 1308195 : case POSTINCREMENT_EXPR:
10626 : 1308195 : case PREDECREMENT_EXPR:
10627 : 1308195 : case POSTDECREMENT_EXPR:
10628 : 1308195 : if (cxx_dialect < cxx14)
10629 : 8122 : goto fail;
10630 : 1300073 : goto unary;
10631 : :
10632 : 1002828 : case BIT_NOT_EXPR:
10633 : : /* A destructor. */
10634 : 1002828 : if (TYPE_P (TREE_OPERAND (t, 0)))
10635 : : return true;
10636 : : /* fall through. */
10637 : :
10638 : 43530401 : case CONJ_EXPR:
10639 : 43530401 : case SAVE_EXPR:
10640 : 43530401 : case FIX_TRUNC_EXPR:
10641 : 43530401 : case FLOAT_EXPR:
10642 : 43530401 : case NEGATE_EXPR:
10643 : 43530401 : case ABS_EXPR:
10644 : 43530401 : case ABSU_EXPR:
10645 : 43530401 : case TRUTH_NOT_EXPR:
10646 : 43530401 : case FIXED_CONVERT_EXPR:
10647 : 43530401 : case UNARY_PLUS_EXPR:
10648 : 43530401 : case UNARY_LEFT_FOLD_EXPR:
10649 : 43530401 : case UNARY_RIGHT_FOLD_EXPR:
10650 : 43530401 : case VEC_DUPLICATE_EXPR:
10651 : 1002828 : unary:
10652 : 43530401 : return RECUR (TREE_OPERAND (t, 0), rval);
10653 : :
10654 : 27401542 : case CAST_EXPR:
10655 : 27401542 : case CONST_CAST_EXPR:
10656 : 27401542 : case STATIC_CAST_EXPR:
10657 : 27401542 : case REINTERPRET_CAST_EXPR:
10658 : 27401542 : case IMPLICIT_CONV_EXPR:
10659 : 27401542 : if (!cast_valid_in_integral_constant_expression_p (TREE_TYPE (t)))
10660 : : /* In C++98, a conversion to non-integral type can't be part of a
10661 : : constant expression. */
10662 : : {
10663 : 281 : if (flags & tf_error)
10664 : 0 : constexpr_error (loc, fundef_p,
10665 : : "cast to non-integral type %qT in a constant "
10666 : 0 : "expression", TREE_TYPE (t));
10667 : 281 : return false;
10668 : : }
10669 : : /* This might be a conversion from a class to a (potentially) literal
10670 : : type. Let's consider it potentially constant since the conversion
10671 : : might be a constexpr user-defined conversion. */
10672 : 27401261 : else if (cxx_dialect >= cxx11
10673 : 27382622 : && (dependent_type_p (TREE_TYPE (t))
10674 : 7775706 : || !COMPLETE_TYPE_P (TREE_TYPE (t))
10675 : 7771335 : || literal_type_p (TREE_TYPE (t)))
10676 : 27364930 : && TREE_OPERAND (t, 0)
10677 : 54569693 : && (TREE_CODE (t) != CAST_EXPR
10678 : 21877286 : || !TREE_CHAIN (TREE_OPERAND (t, 0))))
10679 : : {
10680 : 27137413 : tree from = TREE_OPERAND (t, 0);
10681 : 27137413 : if (TREE_CODE (t) == CAST_EXPR)
10682 : 21846267 : from = TREE_VALUE (from);
10683 : 27137413 : tree type = TREE_TYPE (from);
10684 : : /* If this is a dependent type, it could end up being a class
10685 : : with conversions. */
10686 : 27137413 : if (type == NULL_TREE || WILDCARD_TYPE_P (type))
10687 : : return true;
10688 : : /* Or a non-dependent class which has conversions. */
10689 : 332464 : else if (CLASS_TYPE_P (type)
10690 : 332464 : && (TYPE_HAS_CONVERSION (type) || dependent_scope_p (type)))
10691 : 83910 : return true;
10692 : : }
10693 : :
10694 : 23042191 : return (RECUR (TREE_OPERAND (t, 0),
10695 : 23042191 : !TYPE_REF_P (TREE_TYPE (t))));
10696 : :
10697 : 7402067 : case BIND_EXPR:
10698 : 7402067 : return RECUR (BIND_EXPR_BODY (t), want_rval);
10699 : :
10700 : 38614117 : case CLEANUP_POINT_EXPR:
10701 : 38614117 : case MUST_NOT_THROW_EXPR:
10702 : 38614117 : case TRY_CATCH_EXPR:
10703 : 38614117 : case TRY_BLOCK:
10704 : 38614117 : case EH_SPEC_BLOCK:
10705 : 38614117 : case EXPR_STMT:
10706 : 38614117 : case PAREN_EXPR:
10707 : : /* For convenience. */
10708 : 38614117 : case LOOP_EXPR:
10709 : 38614117 : case EXIT_EXPR:
10710 : 38614117 : return RECUR (TREE_OPERAND (t, 0), want_rval);
10711 : :
10712 : 4465871 : case DECL_EXPR:
10713 : 4465871 : tmp = DECL_EXPR_DECL (t);
10714 : 4405889 : if (VAR_P (tmp) && !DECL_ARTIFICIAL (tmp)
10715 : 7337235 : && (processing_template_decl
10716 : 2521037 : ? !decl_maybe_constant_var_p (tmp)
10717 : 2170710 : : !decl_constant_var_p (tmp)))
10718 : : {
10719 : 2219860 : if (CP_DECL_THREAD_LOCAL_P (tmp) && !DECL_REALLY_EXTERN (tmp))
10720 : : {
10721 : 5 : if (flags & tf_error)
10722 : 2 : constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p,
10723 : : "%qD defined %<thread_local%> in "
10724 : : "%<constexpr%> context", tmp);
10725 : 5 : return false;
10726 : : }
10727 : 2219855 : else if (TREE_STATIC (tmp))
10728 : : {
10729 : 81 : if (flags & tf_error)
10730 : 15 : constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p,
10731 : : "%qD defined %<static%> in %<constexpr%> "
10732 : : "context", tmp);
10733 : 81 : return false;
10734 : : }
10735 : 2219774 : else if (!check_for_uninitialized_const_var
10736 : 2219774 : (tmp, /*constexpr_context_p=*/true, flags))
10737 : : return false;
10738 : : }
10739 : 4462018 : if (VAR_P (tmp))
10740 : 4402036 : return RECUR (DECL_INITIAL (tmp), want_rval);
10741 : : return true;
10742 : :
10743 : 3003 : case TRY_FINALLY_EXPR:
10744 : 3003 : return (RECUR (TREE_OPERAND (t, 0), want_rval)
10745 : 3003 : && RECUR (TREE_OPERAND (t, 1), any));
10746 : :
10747 : 19698983 : case SCOPE_REF:
10748 : 19698983 : return RECUR (TREE_OPERAND (t, 1), want_rval);
10749 : :
10750 : 21222858 : case TARGET_EXPR:
10751 : 21222858 : if (!TARGET_EXPR_DIRECT_INIT_P (t)
10752 : 21170010 : && !TARGET_EXPR_ELIDING_P (t)
10753 : 38048961 : && !literal_type_p (TREE_TYPE (t)))
10754 : : {
10755 : 1276181 : if (flags & tf_error)
10756 : : {
10757 : 28 : auto_diagnostic_group d;
10758 : 28 : if (constexpr_error (loc, fundef_p,
10759 : : "temporary of non-literal type %qT in a "
10760 : 28 : "constant expression", TREE_TYPE (t)))
10761 : 20 : explain_non_literal_class (TREE_TYPE (t));
10762 : 28 : }
10763 : 1276181 : return false;
10764 : : }
10765 : : /* FALLTHRU */
10766 : 36574409 : case INIT_EXPR:
10767 : 36574409 : return RECUR (TREE_OPERAND (t, 1), rval);
10768 : :
10769 : 26724227 : case CONSTRUCTOR:
10770 : 26724227 : {
10771 : 26724227 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
10772 : 26724227 : constructor_elt *ce;
10773 : 108764002 : for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
10774 : 82865228 : if (!RECUR (ce->value, want_rval))
10775 : : return false;
10776 : : return true;
10777 : : }
10778 : :
10779 : 20288805 : case TREE_LIST:
10780 : 20288805 : {
10781 : 20288805 : gcc_assert (TREE_PURPOSE (t) == NULL_TREE
10782 : : || DECL_P (TREE_PURPOSE (t)));
10783 : 20288805 : if (!RECUR (TREE_VALUE (t), want_rval))
10784 : : return false;
10785 : 18090370 : if (TREE_CHAIN (t) == NULL_TREE)
10786 : : return true;
10787 : 48752 : return RECUR (TREE_CHAIN (t), want_rval);
10788 : : }
10789 : :
10790 : 9649486 : case TRUNC_DIV_EXPR:
10791 : 9649486 : case CEIL_DIV_EXPR:
10792 : 9649486 : case FLOOR_DIV_EXPR:
10793 : 9649486 : case ROUND_DIV_EXPR:
10794 : 9649486 : case TRUNC_MOD_EXPR:
10795 : 9649486 : case CEIL_MOD_EXPR:
10796 : 9649486 : case ROUND_MOD_EXPR:
10797 : 9649486 : {
10798 : 9649486 : tree denom = TREE_OPERAND (t, 1);
10799 : 9649486 : if (!RECUR (denom, rval))
10800 : : return false;
10801 : : /* We can't call cxx_eval_outermost_constant_expr on an expression
10802 : : that hasn't been through instantiate_non_dependent_expr yet. */
10803 : 9180371 : if (!processing_template_decl)
10804 : 3820797 : denom = cxx_eval_outermost_constant_expr (denom, true);
10805 : 9180371 : if (integer_zerop (denom))
10806 : : {
10807 : 335 : if (flags & tf_error)
10808 : 35 : constexpr_error (input_location, fundef_p,
10809 : : "division by zero is not a constant expression");
10810 : 335 : return false;
10811 : : }
10812 : : else
10813 : : {
10814 : 9180036 : want_rval = true;
10815 : 9180036 : return RECUR (TREE_OPERAND (t, 0), want_rval);
10816 : : }
10817 : : }
10818 : :
10819 : 4360988 : case COMPOUND_EXPR:
10820 : 4360988 : {
10821 : : /* check_return_expr sometimes wraps a TARGET_EXPR in a
10822 : : COMPOUND_EXPR; don't get confused. */
10823 : 4360988 : tree op0 = TREE_OPERAND (t, 0);
10824 : 4360988 : tree op1 = TREE_OPERAND (t, 1);
10825 : 4360988 : STRIP_NOPS (op1);
10826 : 4360988 : if (TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
10827 : 531234 : return RECUR (op0, want_rval);
10828 : : else
10829 : 3829754 : goto binary;
10830 : : }
10831 : :
10832 : : /* If the first operand is the non-short-circuit constant, look at
10833 : : the second operand; otherwise we only care about the first one for
10834 : : potentiality. */
10835 : 15242822 : case TRUTH_AND_EXPR:
10836 : 15242822 : case TRUTH_ANDIF_EXPR:
10837 : 15242822 : tmp = boolean_true_node;
10838 : 15242822 : goto truth;
10839 : 6019403 : case TRUTH_OR_EXPR:
10840 : 6019403 : case TRUTH_ORIF_EXPR:
10841 : 6019403 : tmp = boolean_false_node;
10842 : 21262225 : truth:
10843 : 21262225 : {
10844 : 21262225 : tree op0 = TREE_OPERAND (t, 0);
10845 : 21262225 : tree op1 = TREE_OPERAND (t, 1);
10846 : 21262225 : if (!RECUR (op0, rval))
10847 : : return false;
10848 : 13638825 : if (!(flags & tf_error) && RECUR (op1, rval))
10849 : : /* When quiet, try to avoid expensive trial evaluation by first
10850 : : checking potentiality of the second operand. */
10851 : : return true;
10852 : 1308946 : if (!processing_template_decl)
10853 : 901640 : op0 = cxx_eval_outermost_constant_expr (op0, true);
10854 : 1308946 : if (tree_int_cst_equal (op0, tmp))
10855 : 5380 : return (flags & tf_error) ? RECUR (op1, rval) : false;
10856 : : else
10857 : : return true;
10858 : : }
10859 : :
10860 : : case PLUS_EXPR:
10861 : : case MULT_EXPR:
10862 : : case POINTER_PLUS_EXPR:
10863 : : case RDIV_EXPR:
10864 : : case EXACT_DIV_EXPR:
10865 : : case MIN_EXPR:
10866 : : case MAX_EXPR:
10867 : : case LSHIFT_EXPR:
10868 : : case RSHIFT_EXPR:
10869 : : case LROTATE_EXPR:
10870 : : case RROTATE_EXPR:
10871 : : case BIT_IOR_EXPR:
10872 : : case BIT_XOR_EXPR:
10873 : : case BIT_AND_EXPR:
10874 : : case TRUTH_XOR_EXPR:
10875 : : case UNORDERED_EXPR:
10876 : : case ORDERED_EXPR:
10877 : : case UNLT_EXPR:
10878 : : case UNLE_EXPR:
10879 : : case UNGT_EXPR:
10880 : : case UNGE_EXPR:
10881 : : case UNEQ_EXPR:
10882 : : case LTGT_EXPR:
10883 : : case RANGE_EXPR:
10884 : : case COMPLEX_EXPR:
10885 : 187057056 : want_rval = true;
10886 : : /* Fall through. */
10887 : 187057056 : case ARRAY_REF:
10888 : 187057056 : case ARRAY_RANGE_REF:
10889 : 187057056 : case MEMBER_REF:
10890 : 187057056 : case DOTSTAR_EXPR:
10891 : 187057056 : case MEM_REF:
10892 : 187057056 : case BINARY_LEFT_FOLD_EXPR:
10893 : 187057056 : case BINARY_RIGHT_FOLD_EXPR:
10894 : 187057056 : binary:
10895 : 391753826 : for (i = 0; i < 2; ++i)
10896 : 295417712 : if (!RECUR (TREE_OPERAND (t, i), want_rval))
10897 : : return false;
10898 : : return true;
10899 : :
10900 : : case VEC_PERM_EXPR:
10901 : 90657 : for (i = 0; i < 3; ++i)
10902 : 90623 : if (!RECUR (TREE_OPERAND (t, i), true))
10903 : : return false;
10904 : : return true;
10905 : :
10906 : 4533914 : case COND_EXPR:
10907 : 4533914 : if (COND_EXPR_IS_VEC_DELETE (t) && cxx_dialect < cxx20)
10908 : : {
10909 : 8 : if (flags & tf_error)
10910 : 2 : constexpr_error (loc, fundef_p, "%<delete[]%> is not a "
10911 : : "constant expression");
10912 : 8 : return false;
10913 : : }
10914 : : /* Fall through. */
10915 : 8910599 : case IF_STMT:
10916 : 8910599 : case VEC_COND_EXPR:
10917 : : /* If the condition is a known constant, we know which of the legs we
10918 : : care about; otherwise we only require that the condition and
10919 : : either of the legs be potentially constant. */
10920 : 8910599 : tmp = TREE_OPERAND (t, 0);
10921 : 8910599 : if (!RECUR (tmp, rval))
10922 : : return false;
10923 : 7654493 : if (!processing_template_decl)
10924 : 6588778 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
10925 : : /* potential_constant_expression* isn't told if it is called for
10926 : : manifestly_const_eval or not, so for consteval if always
10927 : : process both branches as if the condition is not a known
10928 : : constant. */
10929 : 7654493 : if (TREE_CODE (t) != IF_STMT || !IF_STMT_CONSTEVAL_P (t))
10930 : : {
10931 : 7642889 : if (integer_zerop (tmp))
10932 : 1952462 : return RECUR (TREE_OPERAND (t, 2), want_rval);
10933 : 5690427 : else if (TREE_CODE (tmp) == INTEGER_CST)
10934 : 1860826 : return RECUR (TREE_OPERAND (t, 1), want_rval);
10935 : : }
10936 : 3841205 : tmp = *jump_target;
10937 : 4481611 : for (i = 1; i < 3; ++i)
10938 : : {
10939 : 4379979 : tree this_jump_target = tmp;
10940 : 4379979 : if (potential_constant_expression_1 (TREE_OPERAND (t, i),
10941 : : want_rval, strict, now, fundef_p,
10942 : : tf_none, &this_jump_target))
10943 : : {
10944 : 3739573 : if (returns (&this_jump_target))
10945 : 932283 : *jump_target = this_jump_target;
10946 : 2807290 : else if (!returns (jump_target))
10947 : : {
10948 : 2807290 : if (breaks (&this_jump_target)
10949 : 2807290 : || continues (&this_jump_target))
10950 : 58 : *jump_target = this_jump_target;
10951 : 2807290 : if (i == 1)
10952 : : {
10953 : : /* If the then branch is potentially constant, but
10954 : : does not return, check if the else branch
10955 : : couldn't return, break or continue. */
10956 : 2370488 : hash_set<tree> pset;
10957 : 2370488 : check_for_return_continue_data data = { &pset, NULL_TREE,
10958 : 2370488 : NULL_TREE };
10959 : 4740976 : if (tree ret_expr
10960 : 2370488 : = cp_walk_tree (&TREE_OPERAND (t, 2),
10961 : : check_for_return_continue, &data,
10962 : : &pset))
10963 : 17634 : *jump_target = ret_expr;
10964 : 2352854 : else if (*jump_target == NULL_TREE)
10965 : : {
10966 : 2352799 : if (data.continue_stmt)
10967 : 0 : *jump_target = data.continue_stmt;
10968 : 2352799 : else if (data.break_stmt)
10969 : 8 : *jump_target = data.break_stmt;
10970 : : }
10971 : 2370488 : }
10972 : : }
10973 : 3739573 : return true;
10974 : : }
10975 : : }
10976 : 101632 : if (flags & tf_error)
10977 : : {
10978 : 3 : if (TREE_CODE (t) == IF_STMT)
10979 : 3 : constexpr_error (loc, fundef_p, "neither branch of %<if%> is a "
10980 : : "constant expression");
10981 : : else
10982 : 0 : constexpr_error (loc, fundef_p, "expression %qE is not a "
10983 : : "constant expression", t);
10984 : : }
10985 : : return false;
10986 : :
10987 : 953 : case VEC_INIT_EXPR:
10988 : 953 : if (VEC_INIT_EXPR_IS_CONSTEXPR (t))
10989 : : return true;
10990 : 399 : if (flags & tf_error)
10991 : : {
10992 : 3 : if (constexpr_error (loc, fundef_p, "non-constant array "
10993 : : "initialization"))
10994 : 3 : diagnose_non_constexpr_vec_init (t);
10995 : : }
10996 : : return false;
10997 : :
10998 : : case TYPE_DECL:
10999 : : case TAG_DEFN:
11000 : : /* We can see these in statement-expressions. */
11001 : : return true;
11002 : :
11003 : 478322 : case CLEANUP_STMT:
11004 : 478322 : if (!RECUR (CLEANUP_BODY (t), any))
11005 : : return false;
11006 : 477063 : if (!CLEANUP_EH_ONLY (t) && !RECUR (CLEANUP_EXPR (t), any))
11007 : : return false;
11008 : : return true;
11009 : :
11010 : : case EMPTY_CLASS_EXPR:
11011 : : return true;
11012 : :
11013 : 11 : case GOTO_EXPR:
11014 : 11 : {
11015 : 11 : tree *target = &TREE_OPERAND (t, 0);
11016 : : /* Gotos representing break, continue and cdtor return are OK. */
11017 : 11 : if (breaks (target) || continues (target) || returns (target))
11018 : : {
11019 : 3 : *jump_target = *target;
11020 : 3 : return true;
11021 : : }
11022 : 8 : if (DECL_ARTIFICIAL (*target))
11023 : : /* The user didn't write this goto, this isn't the problem. */
11024 : : return true;
11025 : 8 : if (flags & tf_error)
11026 : 2 : constexpr_error (loc, fundef_p, "%<goto%> is not a constant "
11027 : : "expression");
11028 : : return false;
11029 : : }
11030 : :
11031 : 48 : case ASSERTION_STMT:
11032 : 48 : case PRECONDITION_STMT:
11033 : 48 : case POSTCONDITION_STMT:
11034 : 48 : if (!checked_contract_p (get_contract_semantic (t)))
11035 : : return true;
11036 : 33 : return RECUR (CONTRACT_CONDITION (t), rval);
11037 : :
11038 : 210 : case LABEL_EXPR:
11039 : 210 : t = LABEL_EXPR_LABEL (t);
11040 : 210 : if (DECL_ARTIFICIAL (t) || cxx_dialect >= cxx23)
11041 : : return true;
11042 : 29 : else if (flags & tf_error)
11043 : 6 : constexpr_error (loc, fundef_p, "label definition in %<constexpr%> "
11044 : : "function only available with %<-std=c++23%> or "
11045 : : "%<-std=gnu++23%>");
11046 : : return false;
11047 : :
11048 : 2625 : case ANNOTATE_EXPR:
11049 : 2625 : return RECUR (TREE_OPERAND (t, 0), rval);
11050 : :
11051 : 21381 : case BIT_CAST_EXPR:
11052 : 21381 : return RECUR (TREE_OPERAND (t, 0), rval);
11053 : :
11054 : : /* Coroutine await, yield and return expressions are not. */
11055 : 678 : case CO_AWAIT_EXPR:
11056 : 678 : case CO_YIELD_EXPR:
11057 : 678 : case CO_RETURN_EXPR:
11058 : 678 : if (flags & tf_error)
11059 : 3 : constexpr_error (cp_expr_loc_or_loc (t, input_location), fundef_p,
11060 : : "%qE is not a constant expression", t);
11061 : : return false;
11062 : :
11063 : : /* Assume a TU-local entity is not constant, we'll error later when
11064 : : instantiating. */
11065 : : case TU_LOCAL_ENTITY:
11066 : : return false;
11067 : :
11068 : 30 : case NONTYPE_ARGUMENT_PACK:
11069 : 30 : {
11070 : 30 : tree args = ARGUMENT_PACK_ARGS (t);
11071 : 30 : int len = TREE_VEC_LENGTH (args);
11072 : 90 : for (int i = 0; i < len; ++i)
11073 : 60 : if (!RECUR (TREE_VEC_ELT (args, i), any))
11074 : : return false;
11075 : : return true;
11076 : : }
11077 : :
11078 : 0 : default:
11079 : 0 : if (objc_non_constant_expr_p (t))
11080 : : return false;
11081 : :
11082 : 0 : sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t)));
11083 : 0 : gcc_unreachable ();
11084 : : return false;
11085 : : }
11086 : : #undef RECUR
11087 : : }
11088 : :
11089 : : bool
11090 : 1107526450 : potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
11091 : : bool fundef_p, tsubst_flags_t flags)
11092 : : {
11093 : 1107526450 : if (flags & tf_error)
11094 : : {
11095 : : /* Check potentiality quietly first, as that could be performed more
11096 : : efficiently in some cases (currently only for TRUTH_*_EXPR). If
11097 : : that fails, replay the check noisily to give errors. */
11098 : 5290077 : flags &= ~tf_error;
11099 : 5290077 : if (potential_constant_expression_1 (t, want_rval, strict, now, fundef_p,
11100 : : flags))
11101 : : return true;
11102 : 806 : flags |= tf_error;
11103 : : }
11104 : :
11105 : 1102237179 : tree target = NULL_TREE;
11106 : 1102237179 : return potential_constant_expression_1 (t, want_rval, strict, now, fundef_p,
11107 : 1102237179 : flags, &target);
11108 : : }
11109 : :
11110 : : /* The main entry point to the above. */
11111 : :
11112 : : bool
11113 : 347135775 : potential_constant_expression (tree t)
11114 : : {
11115 : 347135775 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
11116 : : /*now*/false, /*fundef_p*/false,
11117 : 347135775 : tf_none);
11118 : : }
11119 : :
11120 : : /* As above, but require a constant rvalue. */
11121 : :
11122 : : bool
11123 : 20033414 : potential_rvalue_constant_expression (tree t)
11124 : : {
11125 : 20033414 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
11126 : : /*now*/false, /*fundef_p*/false,
11127 : 20033414 : tf_none);
11128 : : }
11129 : :
11130 : : /* Like above, but complain about non-constant expressions. */
11131 : :
11132 : : bool
11133 : 73 : require_potential_constant_expression (tree t)
11134 : : {
11135 : 73 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
11136 : : /*now*/false, /*fundef_p*/false,
11137 : 73 : tf_warning_or_error);
11138 : : }
11139 : :
11140 : : /* Cross product of the above. */
11141 : :
11142 : : bool
11143 : 72829 : require_potential_rvalue_constant_expression (tree t)
11144 : : {
11145 : 72829 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
11146 : : /*now*/false, /*fundef_p*/false,
11147 : 72829 : tf_warning_or_error);
11148 : : }
11149 : :
11150 : : /* Like require_potential_rvalue_constant_expression, but fundef_p is true. */
11151 : :
11152 : : bool
11153 : 226 : require_potential_rvalue_constant_expression_fncheck (tree t)
11154 : : {
11155 : 226 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
11156 : : /*now*/false, /*fundef_p*/true,
11157 : 226 : tf_warning_or_error);
11158 : : }
11159 : :
11160 : : /* Like above, but don't consider PARM_DECL a potential_constant_expression. */
11161 : :
11162 : : bool
11163 : 419 : require_rvalue_constant_expression (tree t)
11164 : : {
11165 : 419 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
11166 : : /*now*/true, /*fundef_p*/false,
11167 : 419 : tf_warning_or_error);
11168 : : }
11169 : :
11170 : : /* Like potential_constant_expression, but don't consider possible constexpr
11171 : : substitution of the current function. That is, PARM_DECL qualifies under
11172 : : potential_constant_expression, but not here.
11173 : :
11174 : : This is basically what you can check when any actual constant values might
11175 : : be value-dependent. */
11176 : :
11177 : : bool
11178 : 568475806 : is_constant_expression (tree t)
11179 : : {
11180 : 568475806 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
11181 : : /*now*/true, /*fundef_p*/false,
11182 : 568475806 : tf_none);
11183 : : }
11184 : :
11185 : : /* As above, but expect an rvalue. */
11186 : :
11187 : : bool
11188 : 101563052 : is_rvalue_constant_expression (tree t)
11189 : : {
11190 : 101563052 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
11191 : : /*now*/true, /*fundef_p*/false,
11192 : 101563052 : tf_none);
11193 : : }
11194 : :
11195 : : /* Like above, but complain about non-constant expressions. */
11196 : :
11197 : : bool
11198 : 5216530 : require_constant_expression (tree t)
11199 : : {
11200 : 5216530 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
11201 : : /*now*/true, /*fundef_p*/false,
11202 : 5216530 : tf_warning_or_error);
11203 : : }
11204 : :
11205 : : /* Like is_constant_expression, but allow const variables that are not allowed
11206 : : under constexpr rules. */
11207 : :
11208 : : bool
11209 : 59738249 : is_static_init_expression (tree t)
11210 : : {
11211 : 59738249 : return potential_constant_expression_1 (t, /*want_rval*/false,
11212 : : /*strict*/false, /*now*/true,
11213 : 59738249 : /*fundef_p*/false, tf_none);
11214 : : }
11215 : :
11216 : : /* Returns true if T is a potential constant expression that is not
11217 : : instantiation-dependent, and therefore a candidate for constant folding even
11218 : : in a template. */
11219 : :
11220 : : bool
11221 : 552660677 : is_nondependent_constant_expression (tree t)
11222 : : {
11223 : 552660677 : return (!type_unknown_p (t)
11224 : 552660628 : && is_constant_expression (t)
11225 : 1003208439 : && !instantiation_dependent_expression_p (t));
11226 : : }
11227 : :
11228 : : /* Returns true if T is a potential static initializer expression that is not
11229 : : instantiation-dependent. */
11230 : :
11231 : : bool
11232 : 59738249 : is_nondependent_static_init_expression (tree t)
11233 : : {
11234 : 59738249 : return (!type_unknown_p (t)
11235 : 59738249 : && is_static_init_expression (t)
11236 : 110928104 : && !instantiation_dependent_expression_p (t));
11237 : : }
11238 : :
11239 : : /* True iff FN is an implicitly constexpr function. */
11240 : :
11241 : : bool
11242 : 137682 : decl_implicit_constexpr_p (tree fn)
11243 : : {
11244 : 137682 : if (!(flag_implicit_constexpr
11245 : 0 : && TREE_CODE (fn) == FUNCTION_DECL
11246 : 0 : && DECL_DECLARED_CONSTEXPR_P (fn)))
11247 : : return false;
11248 : :
11249 : 0 : if (DECL_CLONED_FUNCTION_P (fn))
11250 : 0 : fn = DECL_CLONED_FUNCTION (fn);
11251 : :
11252 : 0 : return (DECL_LANG_SPECIFIC (fn)
11253 : 0 : && DECL_LANG_SPECIFIC (fn)->u.fn.implicit_constexpr);
11254 : : }
11255 : :
11256 : : /* Finalize constexpr processing after parsing. */
11257 : :
11258 : : void
11259 : 93543 : fini_constexpr (void)
11260 : : {
11261 : : /* The contexpr call and fundef copies tables are no longer needed. */
11262 : 93543 : constexpr_call_table = NULL;
11263 : 93543 : fundef_copies_table = NULL;
11264 : 93543 : }
11265 : :
11266 : : #include "gt-cp-constexpr.h"
|