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 : 49882628 : is_instantiation_of_constexpr (tree fun)
61 : : {
62 : 66684928 : return ((DECL_TEMPLOID_INSTANTIATION (fun)
63 : 33175991 : && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun)))
64 : 73071156 : || (DECL_DEFAULTED_FN (fun)
65 : 1188347 : && DECL_DECLARED_CONSTEXPR_P (fun)));
66 : : }
67 : :
68 : : /* Return true if T is a literal type. */
69 : :
70 : : bool
71 : 94284123 : literal_type_p (tree t)
72 : : {
73 : 91950775 : if (SCALAR_TYPE_P (t)
74 : 38830656 : || VECTOR_TYPE_P (t)
75 : 38819032 : || TYPE_REF_P (t)
76 : 126340713 : || (VOID_TYPE_P (t) && cxx_dialect >= cxx14))
77 : : return true;
78 : 29650558 : if (CLASS_TYPE_P (t))
79 : : {
80 : 28470016 : t = complete_type (t);
81 : 28470016 : gcc_assert (COMPLETE_TYPE_P (t) || errorcount);
82 : 28470016 : return CLASSTYPE_LITERAL_P (t);
83 : : }
84 : 1180542 : if (TREE_CODE (t) == ARRAY_TYPE)
85 : 1180368 : 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 : 260247879 : ensure_literal_type_for_constexpr_object (tree decl)
95 : : {
96 : 260247879 : tree type = TREE_TYPE (decl);
97 : 260247879 : if (VAR_P (decl)
98 : 90717943 : && (DECL_DECLARED_CONSTEXPR_P (decl)
99 : 72658987 : || var_in_constexpr_fn (decl))
100 : 284634549 : && !processing_template_decl)
101 : : {
102 : 15995532 : tree stype = strip_array_types (type);
103 : 15995532 : 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 : 15995526 : else if (!literal_type_p (type))
107 : : {
108 : 7709 : 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 : 7667 : else if (cxx_dialect < cxx23)
118 : : {
119 : 2411 : 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 : 2411 : cp_function_chain->invalid_constexpr = true;
130 : : }
131 : : }
132 : 15987817 : else if (DECL_DECLARED_CONSTEXPR_P (decl)
133 : 15987817 : && 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 : 260247879 : 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 : 755 : constexpr_error (location_t location, bool constexpr_fundef_p,
154 : : const char *gmsgid, ...)
155 : : {
156 : 755 : diagnostic_info diagnostic;
157 : 755 : va_list ap;
158 : 755 : rich_location richloc (line_table, location);
159 : 755 : va_start (ap, gmsgid);
160 : 755 : bool ret;
161 : 755 : if (!constexpr_fundef_p)
162 : : {
163 : : /* Report an error that cannot be suppressed. */
164 : 549 : diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_ERROR);
165 : 549 : ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
166 : : }
167 : 206 : else if (warn_invalid_constexpr)
168 : : {
169 : 179 : diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
170 : 179 : cxx_dialect < cxx23 ? DK_PEDWARN : DK_WARNING);
171 : 179 : diagnostic.option_id = OPT_Winvalid_constexpr;
172 : 179 : ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
173 : : }
174 : : else
175 : : ret = false;
176 : 755 : va_end (ap);
177 : 1510 : return ret;
178 : 755 : }
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 : 488433862 : constexpr_fundef_hasher::equal (const constexpr_fundef *lhs,
197 : : const constexpr_fundef *rhs)
198 : : {
199 : 474195663 : 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 : 461916623 : constexpr_fundef_hasher::hash (const constexpr_fundef *fundef)
207 : : {
208 : 461916623 : return DECL_UID (fundef->decl);
209 : : }
210 : :
211 : : /* Return a previously saved definition of function FUN. */
212 : :
213 : : constexpr_fundef *
214 : 52919136 : retrieve_constexpr_fundef (tree fun)
215 : : {
216 : 52919136 : if (constexpr_fundef_table == NULL)
217 : : return NULL;
218 : :
219 : 52916132 : constexpr_fundef fundef = { fun, NULL_TREE, NULL_TREE, NULL_TREE };
220 : 52916132 : 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 : 16576917 : is_valid_constexpr_fn (tree fun, bool complain)
228 : : {
229 : 16576917 : bool ret = true;
230 : :
231 : 33153834 : if (DECL_INHERITED_CTOR (fun)
232 : 1820242 : && 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 : 16576917 : for (tree parm = FUNCTION_FIRST_USER_PARM (fun);
242 : 32630148 : parm != NULL_TREE; parm = TREE_CHAIN (parm))
243 : 16053231 : if (!literal_type_p (TREE_TYPE (parm)))
244 : : {
245 : 11021 : ret = false;
246 : 11021 : 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 : 26246883 : 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 : 33153830 : 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 : 16576819 : else if (!DECL_CONSTRUCTOR_P (fun) && !DECL_DESTRUCTOR_P (fun))
274 : : {
275 : 14604954 : tree rettype = TREE_TYPE (TREE_TYPE (fun));
276 : 14604954 : if (!literal_type_p (rettype))
277 : : {
278 : 51124 : ret = false;
279 : 51124 : 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 : 14604954 : if (cxx_dialect < cxx14
291 : 31514 : && DECL_IOBJ_MEMBER_FUNCTION_P (fun)
292 : 14606010 : && !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 : 1971865 : else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun)))
307 : : {
308 : 106 : ret = false;
309 : 106 : if (complain)
310 : 12 : error ("%q#T has virtual base classes", DECL_CONTEXT (fun));
311 : : }
312 : :
313 : 16576917 : return ret;
314 : : }
315 : :
316 : : /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF
317 : : for a member of an anonymous aggregate, INIT is the initializer for that
318 : : member, and VEC_OUTER is the vector of constructor elements for the class
319 : : whose constructor we are processing. Add the initializer to the vector
320 : : and return true to indicate success. */
321 : :
322 : : static bool
323 : 850 : build_anon_member_initialization (tree member, tree init,
324 : : vec<constructor_elt, va_gc> **vec_outer)
325 : : {
326 : : /* MEMBER presents the relevant fields from the inside out, but we need
327 : : to build up the initializer from the outside in so that we can reuse
328 : : previously built CONSTRUCTORs if this is, say, the second field in an
329 : : anonymous struct. So we use a vec as a stack. */
330 : 850 : auto_vec<tree, 2> fields;
331 : 1796 : do
332 : : {
333 : 1796 : fields.safe_push (TREE_OPERAND (member, 1));
334 : 1796 : member = TREE_OPERAND (member, 0);
335 : : }
336 : 1796 : while (ANON_AGGR_TYPE_P (TREE_TYPE (member))
337 : 3598 : && TREE_CODE (member) == COMPONENT_REF);
338 : :
339 : : /* VEC has the constructor elements vector for the context of FIELD.
340 : : If FIELD is an anonymous aggregate, we will push inside it. */
341 : : vec<constructor_elt, va_gc> **vec = vec_outer;
342 : : tree field;
343 : 1796 : while (field = fields.pop(),
344 : 1796 : ANON_AGGR_TYPE_P (TREE_TYPE (field)))
345 : : {
346 : 946 : tree ctor;
347 : : /* If there is already an outer constructor entry for the anonymous
348 : : aggregate FIELD, use it; otherwise, insert one. */
349 : 946 : if (vec_safe_is_empty (*vec)
350 : 198 : || (*vec)->last().index != field)
351 : : {
352 : 844 : ctor = build_constructor (TREE_TYPE (field), NULL);
353 : 844 : CONSTRUCTOR_APPEND_ELT (*vec, field, ctor);
354 : : }
355 : : else
356 : 102 : ctor = (*vec)->last().value;
357 : 946 : vec = &CONSTRUCTOR_ELTS (ctor);
358 : : }
359 : :
360 : : /* Now we're at the innermost field, the one that isn't an anonymous
361 : : aggregate. Add its initializer to the CONSTRUCTOR and we're done. */
362 : 850 : gcc_assert (fields.is_empty());
363 : 850 : CONSTRUCTOR_APPEND_ELT (*vec, field, init);
364 : :
365 : 850 : return true;
366 : 850 : }
367 : :
368 : : /* Subroutine of build_constexpr_constructor_member_initializers.
369 : : The expression tree T represents a data member initialization
370 : : in a (constexpr) constructor definition. Build a pairing of
371 : : the data member with its initializer, and prepend that pair
372 : : to the existing initialization pair INITS. */
373 : :
374 : : static bool
375 : 2597670 : build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec)
376 : : {
377 : 2818224 : tree member, init;
378 : 2818224 : if (TREE_CODE (t) == CLEANUP_POINT_EXPR)
379 : 1152370 : t = TREE_OPERAND (t, 0);
380 : 2818224 : if (TREE_CODE (t) == EXPR_STMT)
381 : 1152434 : t = TREE_OPERAND (t, 0);
382 : 2818224 : if (t == error_mark_node)
383 : : return false;
384 : 2818217 : if (TREE_CODE (t) == STATEMENT_LIST)
385 : : {
386 : 3000614 : for (tree stmt : tsi_range (t))
387 : 183153 : if (! build_data_member_initialization (stmt, vec))
388 : 7 : return false;
389 : : return true;
390 : : }
391 : 2598419 : if (TREE_CODE (t) == CLEANUP_STMT)
392 : : {
393 : : /* We can't see a CLEANUP_STMT in a constructor for a literal class,
394 : : but we can in a constexpr constructor for a non-literal class. Just
395 : : ignore it; either all the initialization will be constant, in which
396 : : case the cleanup can't run, or it can't be constexpr.
397 : : Still recurse into CLEANUP_BODY. */
398 : 209337 : return build_data_member_initialization (CLEANUP_BODY (t), vec);
399 : : }
400 : 2389082 : if (TREE_CODE (t) == CONVERT_EXPR)
401 : 1484496 : t = TREE_OPERAND (t, 0);
402 : 2389082 : if (TREE_CODE (t) == INIT_EXPR
403 : : /* vptr initialization shows up as a MODIFY_EXPR. In C++14 we only
404 : : use what this function builds for cx_check_missing_mem_inits, and
405 : : assignment in the ctor body doesn't count. */
406 : 952600 : || (cxx_dialect < cxx14 && TREE_CODE (t) == MODIFY_EXPR))
407 : : {
408 : 1436949 : member = TREE_OPERAND (t, 0);
409 : 1436949 : init = break_out_target_exprs (TREE_OPERAND (t, 1));
410 : : }
411 : 952133 : else if (TREE_CODE (t) == CALL_EXPR)
412 : : {
413 : 856440 : tree fn = get_callee_fndecl (t);
414 : 1712880 : if (!fn || !DECL_CONSTRUCTOR_P (fn))
415 : : /* We're only interested in calls to subobject constructors. */
416 : : return true;
417 : 754258 : member = CALL_EXPR_ARG (t, 0);
418 : : /* We don't use build_cplus_new here because it complains about
419 : : abstract bases. Leaving the call unwrapped means that it has the
420 : : wrong type, but cxx_eval_constant_expression doesn't care. */
421 : 754258 : init = break_out_target_exprs (t);
422 : : }
423 : 95693 : else if (TREE_CODE (t) == BIND_EXPR)
424 : 11217 : return build_data_member_initialization (BIND_EXPR_BODY (t), vec);
425 : : else
426 : : /* Don't add anything else to the CONSTRUCTOR. */
427 : : return true;
428 : 2191207 : if (INDIRECT_REF_P (member))
429 : 28395 : member = TREE_OPERAND (member, 0);
430 : 2191207 : if (TREE_CODE (member) == NOP_EXPR)
431 : : {
432 : 195429 : tree op = member;
433 : 195429 : STRIP_NOPS (op);
434 : 195429 : if (TREE_CODE (op) == ADDR_EXPR)
435 : : {
436 : 238 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
437 : : (TREE_TYPE (TREE_TYPE (op)),
438 : : TREE_TYPE (TREE_TYPE (member))));
439 : : /* Initializing a cv-qualified member; we need to look through
440 : : the const_cast. */
441 : : member = op;
442 : : }
443 : 195191 : else if (op == current_class_ptr
444 : 390270 : && (same_type_ignoring_top_level_qualifiers_p
445 : 195079 : (TREE_TYPE (TREE_TYPE (member)),
446 : : current_class_type)))
447 : : /* Delegating constructor. */
448 : : member = op;
449 : : else
450 : : {
451 : : /* This is an initializer for an empty base; keep it for now so
452 : : we can check it in cxx_eval_bare_aggregate. */
453 : 179030 : gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member))));
454 : : }
455 : : }
456 : 2191207 : if (TREE_CODE (member) == ADDR_EXPR)
457 : 587462 : member = TREE_OPERAND (member, 0);
458 : 2191207 : if (TREE_CODE (member) == COMPONENT_REF)
459 : : {
460 : 1986977 : tree aggr = TREE_OPERAND (member, 0);
461 : 1986977 : if (TREE_CODE (aggr) == VAR_DECL)
462 : : /* Initializing a local variable, don't add anything. */
463 : : return true;
464 : 1986975 : if (TREE_CODE (aggr) != COMPONENT_REF)
465 : : /* Normal member initialization. */
466 : 1985868 : member = TREE_OPERAND (member, 1);
467 : 1107 : else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr)))
468 : : /* Initializing a member of an anonymous union. */
469 : 850 : return build_anon_member_initialization (member, init, vec);
470 : : else
471 : : /* We're initializing a vtable pointer in a base. Leave it as
472 : : COMPONENT_REF so we remember the path to get to the vfield. */
473 : 257 : gcc_assert (TREE_TYPE (member) == vtbl_ptr_type_node);
474 : : }
475 : :
476 : : /* Value-initialization can produce multiple initializers for the
477 : : same field; use the last one. */
478 : 2653174 : if (!vec_safe_is_empty (*vec) && (*vec)->last().index == member)
479 : 32 : (*vec)->last().value = init;
480 : : else
481 : 2190323 : CONSTRUCTOR_APPEND_ELT (*vec, member, init);
482 : : return true;
483 : : }
484 : :
485 : : /* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval.
486 : : In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a
487 : : BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */
488 : :
489 : : static bool
490 : 2362 : check_constexpr_bind_expr_vars (tree t)
491 : : {
492 : 2362 : gcc_assert (TREE_CODE (t) == BIND_EXPR);
493 : :
494 : 3171 : for (tree var = BIND_EXPR_VARS (t); var; var = DECL_CHAIN (var))
495 : 821 : if (TREE_CODE (var) == TYPE_DECL
496 : 805 : && DECL_IMPLICIT_TYPEDEF_P (var)
497 : 843 : && !LAMBDA_TYPE_P (TREE_TYPE (var)))
498 : : return false;
499 : : return true;
500 : : }
501 : :
502 : : /* Subroutine of check_constexpr_ctor_body. */
503 : :
504 : : static bool
505 : 4069 : check_constexpr_ctor_body_1 (tree last, tree list)
506 : : {
507 : 4069 : switch (TREE_CODE (list))
508 : : {
509 : 6 : case DECL_EXPR:
510 : 6 : if (TREE_CODE (DECL_EXPR_DECL (list)) == USING_DECL
511 : 6 : || TREE_CODE (DECL_EXPR_DECL (list)) == TYPE_DECL)
512 : : return true;
513 : : return false;
514 : :
515 : 3 : case CLEANUP_POINT_EXPR:
516 : 3 : return check_constexpr_ctor_body (last, TREE_OPERAND (list, 0),
517 : 3 : /*complain=*/false);
518 : :
519 : 2026 : case BIND_EXPR:
520 : 2026 : if (!check_constexpr_bind_expr_vars (list)
521 : 2026 : || !check_constexpr_ctor_body (last, BIND_EXPR_BODY (list),
522 : : /*complain=*/false))
523 : 11 : return false;
524 : : return true;
525 : :
526 : : case USING_STMT:
527 : : case STATIC_ASSERT:
528 : : case DEBUG_BEGIN_STMT:
529 : : return true;
530 : :
531 : : default:
532 : : return false;
533 : : }
534 : : }
535 : :
536 : : /* Make sure that there are no statements after LAST in the constructor
537 : : body represented by LIST. */
538 : :
539 : : bool
540 : 3072650 : check_constexpr_ctor_body (tree last, tree list, bool complain)
541 : : {
542 : : /* C++14 doesn't require a constexpr ctor to have an empty body. */
543 : 3072650 : if (cxx_dialect >= cxx14)
544 : : return true;
545 : :
546 : 12868 : bool ok = true;
547 : 12868 : if (TREE_CODE (list) == STATEMENT_LIST)
548 : : {
549 : 12855 : tree_stmt_iterator i = tsi_last (list);
550 : 16893 : for (; !tsi_end_p (i); tsi_prev (&i))
551 : : {
552 : 14774 : tree t = tsi_stmt (i);
553 : 14774 : if (t == last)
554 : : break;
555 : 4056 : if (!check_constexpr_ctor_body_1 (last, t))
556 : : {
557 : : ok = false;
558 : : break;
559 : : }
560 : : }
561 : : }
562 : 13 : else if (list != last
563 : 13 : && !check_constexpr_ctor_body_1 (last, list))
564 : : ok = false;
565 : 12855 : if (!ok)
566 : : {
567 : 22 : if (complain)
568 : 16 : error ("%<constexpr%> constructor does not have empty body");
569 : 22 : DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false;
570 : : }
571 : : return ok;
572 : : }
573 : :
574 : : /* V is a vector of constructor elements built up for the base and member
575 : : initializers of a constructor for TYPE. They need to be in increasing
576 : : offset order, which they might not be yet if TYPE has a primary base
577 : : which is not first in the base-clause or a vptr and at least one base
578 : : all of which are non-primary. */
579 : :
580 : : static vec<constructor_elt, va_gc> *
581 : 1803137 : sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v)
582 : : {
583 : 1803137 : tree pri = CLASSTYPE_PRIMARY_BINFO (type);
584 : 1803137 : tree field_type;
585 : 1803137 : unsigned i;
586 : 1803137 : constructor_elt *ce;
587 : :
588 : 1803137 : if (pri)
589 : 7125 : field_type = BINFO_TYPE (pri);
590 : 1796012 : else if (TYPE_CONTAINS_VPTR_P (type))
591 : 20999 : field_type = vtbl_ptr_type_node;
592 : : else
593 : : return v;
594 : :
595 : : /* Find the element for the primary base or vptr and move it to the
596 : : beginning of the vec. */
597 : 41762 : for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
598 : 20970 : if (TREE_TYPE (ce->index) == field_type)
599 : : break;
600 : :
601 : 37226 : if (i > 0 && i < vec_safe_length (v))
602 : : {
603 : 16 : vec<constructor_elt, va_gc> &vref = *v;
604 : 16 : constructor_elt elt = vref[i];
605 : 32 : for (; i > 0; --i)
606 : 16 : vref[i] = vref[i-1];
607 : 16 : vref[0] = elt;
608 : : }
609 : :
610 : : return v;
611 : : }
612 : :
613 : : /* Build compile-time evalable representations of member-initializer list
614 : : for a constexpr constructor. */
615 : :
616 : : static tree
617 : 1819301 : build_constexpr_constructor_member_initializers (tree type, tree body)
618 : : {
619 : 1819301 : vec<constructor_elt, va_gc> *vec = NULL;
620 : 1819301 : bool ok = true;
621 : 4120550 : while (true)
622 : 4120550 : switch (TREE_CODE (body))
623 : : {
624 : 913689 : case MUST_NOT_THROW_EXPR:
625 : 913689 : case EH_SPEC_BLOCK:
626 : 913689 : body = TREE_OPERAND (body, 0);
627 : 913689 : break;
628 : :
629 : 1387560 : case STATEMENT_LIST:
630 : 5508156 : for (tree stmt : tsi_range (body))
631 : : {
632 : 2775150 : body = stmt;
633 : 2775150 : if (TREE_CODE (body) == BIND_EXPR)
634 : : break;
635 : : }
636 : : break;
637 : :
638 : 1819301 : case BIND_EXPR:
639 : 1819301 : body = BIND_EXPR_BODY (body);
640 : 1819301 : goto found;
641 : :
642 : 0 : default:
643 : 0 : gcc_unreachable ();
644 : : }
645 : 1819301 : found:
646 : 1819301 : if (TREE_CODE (body) == TRY_BLOCK)
647 : : {
648 : 24 : body = TREE_OPERAND (body, 0);
649 : 24 : if (TREE_CODE (body) == BIND_EXPR)
650 : 24 : body = BIND_EXPR_BODY (body);
651 : : }
652 : 1819301 : if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
653 : : {
654 : 1174940 : body = TREE_OPERAND (body, 0);
655 : 1174940 : if (TREE_CODE (body) == EXPR_STMT)
656 : 1174940 : body = TREE_OPERAND (body, 0);
657 : 1174940 : if (TREE_CODE (body) == INIT_EXPR
658 : 1174940 : && (same_type_ignoring_top_level_qualifiers_p
659 : 0 : (TREE_TYPE (TREE_OPERAND (body, 0)),
660 : : current_class_type)))
661 : : {
662 : : /* Trivial copy. */
663 : 0 : return TREE_OPERAND (body, 1);
664 : : }
665 : 1174940 : ok = build_data_member_initialization (body, &vec);
666 : : }
667 : 644361 : else if (TREE_CODE (body) == STATEMENT_LIST)
668 : : {
669 : 1883654 : for (tree stmt : tsi_range (body))
670 : : {
671 : 1239435 : ok = build_data_member_initialization (stmt, &vec);
672 : 1239435 : if (!ok)
673 : : break;
674 : : }
675 : : }
676 : 142 : else if (EXPR_P (body))
677 : 142 : ok = build_data_member_initialization (body, &vec);
678 : : else
679 : 0 : gcc_assert (errorcount > 0);
680 : 1819301 : if (ok)
681 : : {
682 : 1819294 : if (vec_safe_length (vec) > 0)
683 : : {
684 : : /* In a delegating constructor, return the target. */
685 : 1728260 : constructor_elt *ce = &(*vec)[0];
686 : 1728260 : if (ce->index == current_class_ptr)
687 : : {
688 : 16157 : body = ce->value;
689 : 16157 : vec_free (vec);
690 : 16157 : return body;
691 : : }
692 : : }
693 : 1803137 : vec = sort_constexpr_mem_initializers (type, vec);
694 : 1803137 : return build_constructor (type, vec);
695 : : }
696 : : else
697 : 7 : return error_mark_node;
698 : : }
699 : :
700 : : /* We have an expression tree T that represents a call, either CALL_EXPR
701 : : or AGGR_INIT_EXPR. If the call is lexically to a named function,
702 : : return the _DECL for that function. */
703 : :
704 : : static tree
705 : 279753531 : get_function_named_in_call (tree t)
706 : : {
707 : 279753531 : tree callee = cp_get_callee (t);
708 : 279753531 : tree fun = cp_get_fndecl_from_callee (callee, /*fold*/false);
709 : 279753531 : return fun ? fun : callee;
710 : : }
711 : :
712 : : /* Subroutine of check_constexpr_fundef. BODY is the body of a function
713 : : declared to be constexpr, or a sub-statement thereof. Returns the
714 : : return value if suitable, error_mark_node for a statement not allowed in
715 : : a constexpr function, or NULL_TREE if no return value was found. */
716 : :
717 : : tree
718 : 68478 : constexpr_fn_retval (tree body)
719 : : {
720 : 74831 : switch (TREE_CODE (body))
721 : : {
722 : 18467 : case STATEMENT_LIST:
723 : 18467 : {
724 : 18467 : tree expr = NULL_TREE;
725 : 55331 : for (tree stmt : tsi_range (body))
726 : : {
727 : 36892 : tree s = constexpr_fn_retval (stmt);
728 : 36892 : if (s == error_mark_node)
729 : : return error_mark_node;
730 : 36864 : else if (s == NULL_TREE)
731 : : /* Keep iterating. */;
732 : 18429 : else if (expr)
733 : : /* Multiple return statements. */
734 : : return error_mark_node;
735 : : else
736 : : expr = s;
737 : : }
738 : : return expr;
739 : : }
740 : :
741 : 31533 : case RETURN_EXPR:
742 : 31533 : return break_out_target_exprs (TREE_OPERAND (body, 0));
743 : :
744 : 17 : case DECL_EXPR:
745 : 17 : {
746 : 17 : tree decl = DECL_EXPR_DECL (body);
747 : 17 : if (TREE_CODE (decl) == USING_DECL
748 : : /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */
749 : 17 : || DECL_ARTIFICIAL (decl))
750 : : return NULL_TREE;
751 : 6 : return error_mark_node;
752 : : }
753 : :
754 : 6023 : case CLEANUP_POINT_EXPR:
755 : 6023 : return constexpr_fn_retval (TREE_OPERAND (body, 0));
756 : :
757 : 336 : case BIND_EXPR:
758 : 336 : if (!check_constexpr_bind_expr_vars (body))
759 : 6 : return error_mark_node;
760 : 330 : return constexpr_fn_retval (BIND_EXPR_BODY (body));
761 : :
762 : : case USING_STMT:
763 : : case DEBUG_BEGIN_STMT:
764 : : return NULL_TREE;
765 : :
766 : 1 : case CALL_EXPR:
767 : 1 : {
768 : 1 : tree fun = get_function_named_in_call (body);
769 : 1 : if (fun != NULL_TREE
770 : 1 : && fndecl_built_in_p (fun, BUILT_IN_UNREACHABLE))
771 : : return NULL_TREE;
772 : : }
773 : : /* Fallthru. */
774 : :
775 : 31 : default:
776 : 31 : return error_mark_node;
777 : : }
778 : : }
779 : :
780 : : /* Subroutine of check_constexpr_fundef. BODY is the DECL_SAVED_TREE of
781 : : FUN; do the necessary transformations to turn it into a single expression
782 : : that we can store in the hash table. */
783 : :
784 : : static tree
785 : 16089096 : massage_constexpr_body (tree fun, tree body)
786 : : {
787 : 32178192 : if (DECL_CONSTRUCTOR_P (fun))
788 : 1819301 : body = build_constexpr_constructor_member_initializers
789 : 1819301 : (DECL_CONTEXT (fun), body);
790 : 14269795 : else if (cxx_dialect < cxx14)
791 : : {
792 : 31357 : if (TREE_CODE (body) == EH_SPEC_BLOCK)
793 : 1 : body = EH_SPEC_STMTS (body);
794 : 31357 : if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
795 : 20758 : body = TREE_OPERAND (body, 0);
796 : 31357 : body = constexpr_fn_retval (body);
797 : : }
798 : 16089096 : return body;
799 : : }
800 : :
801 : : /* CTYPE is a type constructed from BODY. Return true if some
802 : : bases/fields are uninitialized, and complain if COMPLAIN. */
803 : :
804 : : static bool
805 : 1533474 : cx_check_missing_mem_inits (tree ctype, tree body, bool complain)
806 : : {
807 : : /* We allow uninitialized bases/fields in C++20. */
808 : 1533474 : if (cxx_dialect >= cxx20)
809 : : return false;
810 : :
811 : 787033 : unsigned nelts = 0;
812 : :
813 : 787033 : if (body)
814 : : {
815 : 787029 : if (TREE_CODE (body) != CONSTRUCTOR)
816 : : return false;
817 : 786778 : nelts = CONSTRUCTOR_NELTS (body);
818 : : }
819 : 786782 : tree field = TYPE_FIELDS (ctype);
820 : :
821 : 786782 : if (TREE_CODE (ctype) == UNION_TYPE)
822 : : {
823 : 8153 : if (nelts == 0 && next_aggregate_field (field))
824 : : {
825 : 2 : if (complain)
826 : 2 : error ("%<constexpr%> constructor for union %qT must "
827 : : "initialize exactly one non-static data member", ctype);
828 : 2 : return true;
829 : : }
830 : 8151 : return false;
831 : : }
832 : :
833 : : /* Iterate over the CONSTRUCTOR, checking any missing fields don't
834 : : need an explicit initialization. */
835 : : bool bad = false;
836 : 1681189 : for (unsigned i = 0; i <= nelts; ++i)
837 : : {
838 : 1681189 : tree index = NULL_TREE;
839 : 1681189 : if (i < nelts)
840 : : {
841 : 902560 : index = CONSTRUCTOR_ELT (body, i)->index;
842 : : /* Skip vptr adjustment represented with a COMPONENT_REF. */
843 : 902560 : if (TREE_CODE (index) != FIELD_DECL)
844 : 67222 : continue;
845 : : }
846 : :
847 : 39203508 : for (; field != index; field = DECL_CHAIN (field))
848 : : {
849 : 37589610 : tree ftype;
850 : 37589610 : if (TREE_CODE (field) != FIELD_DECL)
851 : 75045703 : continue;
852 : 133427 : if (DECL_UNNAMED_BIT_FIELD (field))
853 : 16 : continue;
854 : : /* Artificial fields can be ignored unless they're bases. */
855 : 133411 : if (DECL_ARTIFICIAL (field) && !DECL_FIELD_IS_BASE (field))
856 : 3 : continue;
857 : 133408 : if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
858 : : {
859 : : /* Recurse to check the anonymous aggregate member. */
860 : 8 : bad |= cx_check_missing_mem_inits
861 : 4 : (TREE_TYPE (field), NULL_TREE, complain);
862 : 4 : if (bad && !complain)
863 : 69 : return true;
864 : 4 : continue;
865 : : }
866 : 133404 : ftype = TREE_TYPE (field);
867 : 133404 : if (!ftype || !TYPE_P (ftype) || !COMPLETE_TYPE_P (ftype))
868 : : /* A flexible array can't be intialized here, so don't complain
869 : : that it isn't. */
870 : 2 : continue;
871 : 133402 : if (is_empty_field (field))
872 : : /* An empty field doesn't need an initializer. */
873 : 133301 : continue;
874 : 101 : ftype = strip_array_types (ftype);
875 : 101 : if (type_has_constexpr_default_constructor (ftype))
876 : : {
877 : : /* It's OK to skip a member with a trivial constexpr ctor.
878 : : A constexpr ctor that isn't trivial should have been
879 : : added in by now. */
880 : 11 : gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype)
881 : : || errorcount != 0);
882 : 11 : continue;
883 : : }
884 : 90 : if (!complain)
885 : : return true;
886 : 21 : auto_diagnostic_group d;
887 : 21 : error ("member %qD must be initialized by mem-initializer "
888 : : "in %<constexpr%> constructor", field);
889 : 21 : inform (DECL_SOURCE_LOCATION (field), "declared here");
890 : 21 : bad = true;
891 : 21 : }
892 : 1613898 : if (field == NULL_TREE)
893 : : break;
894 : :
895 : 835338 : if (ANON_AGGR_TYPE_P (TREE_TYPE (index)))
896 : : {
897 : : /* Check the anonymous aggregate initializer is valid. */
898 : 448 : bad |= cx_check_missing_mem_inits
899 : 224 : (TREE_TYPE (index), CONSTRUCTOR_ELT (body, i)->value, complain);
900 : 224 : if (bad && !complain)
901 : : return true;
902 : : }
903 : 835338 : field = DECL_CHAIN (field);
904 : : }
905 : :
906 : : return bad;
907 : : }
908 : :
909 : : /* We are processing the definition of the constexpr function FUN.
910 : : Check that its body fulfills the apropriate requirements and
911 : : enter it in the constexpr function definition table. */
912 : :
913 : : void
914 : 133382150 : maybe_save_constexpr_fundef (tree fun)
915 : : {
916 : 133382150 : if (processing_template_decl
917 : 52452702 : || cp_function_chain->invalid_constexpr
918 : 185723723 : || (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun)))
919 : 117293306 : return;
920 : :
921 : : /* With -fimplicit-constexpr, try to make inlines constexpr. We'll
922 : : actually set DECL_DECLARED_CONSTEXPR_P below if the checks pass. */
923 : 39571931 : bool implicit = false;
924 : 39571931 : if (flag_implicit_constexpr)
925 : : {
926 : 14 : if (DECL_DELETING_DESTRUCTOR_P (fun)
927 : 14 : && decl_implicit_constexpr_p (DECL_CLONED_FUNCTION (fun)))
928 : : /* Don't inherit implicit constexpr from the non-deleting
929 : : destructor. */
930 : 0 : DECL_DECLARED_CONSTEXPR_P (fun) = false;
931 : :
932 : 14 : if (!DECL_DECLARED_CONSTEXPR_P (fun)
933 : 13 : && DECL_DECLARED_INLINE_P (fun)
934 : 23 : && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fun)))
935 : : implicit = true;
936 : : }
937 : :
938 : 39571931 : if (!DECL_DECLARED_CONSTEXPR_P (fun) && !implicit)
939 : : return;
940 : :
941 : 16144092 : bool complain = !DECL_GENERATED_P (fun) && !implicit;
942 : :
943 : 16144092 : if (!is_valid_constexpr_fn (fun, complain))
944 : : return;
945 : :
946 : 16089055 : tree massaged = massage_constexpr_body (fun, DECL_SAVED_TREE (fun));
947 : 16089055 : if (massaged == NULL_TREE || massaged == error_mark_node)
948 : : {
949 : 66 : if (!DECL_CONSTRUCTOR_P (fun) && complain)
950 : 22 : error ("body of %<constexpr%> function %qD not a return-statement",
951 : : fun);
952 : 33 : return;
953 : : }
954 : :
955 : 16089022 : bool potential = potential_rvalue_constant_expression (massaged);
956 : 16089022 : if (!potential && complain)
957 : 163 : require_potential_rvalue_constant_expression_fncheck (massaged);
958 : :
959 : 17908308 : if (DECL_CONSTRUCTOR_P (fun) && potential
960 : 17905933 : && !DECL_DEFAULTED_FN (fun))
961 : : {
962 : 1533238 : if (cx_check_missing_mem_inits (DECL_CONTEXT (fun),
963 : : massaged, complain))
964 : : potential = false;
965 : 1533150 : else if (cxx_dialect > cxx11)
966 : : {
967 : : /* What we got from massage_constexpr_body is pretty much just the
968 : : ctor-initializer, also check the body. */
969 : 1528467 : massaged = DECL_SAVED_TREE (fun);
970 : 1528467 : potential = potential_rvalue_constant_expression (massaged);
971 : 1528467 : if (!potential && complain)
972 : 14 : require_potential_rvalue_constant_expression_fncheck (massaged);
973 : : }
974 : : }
975 : :
976 : 16089022 : if (!potential && complain
977 : : /* If -Wno-invalid-constexpr was specified, we haven't complained
978 : : about non-constant expressions yet. Register the function and
979 : : complain in explain_invalid_constexpr_fn if the function is
980 : : called. */
981 : 196 : && warn_invalid_constexpr != 0)
982 : : return;
983 : :
984 : 16088852 : if (implicit)
985 : : {
986 : 9 : if (potential)
987 : : {
988 : 1 : DECL_DECLARED_CONSTEXPR_P (fun) = true;
989 : 1 : DECL_LANG_SPECIFIC (fun)->u.fn.implicit_constexpr = true;
990 : 2 : if (DECL_CONSTRUCTOR_P (fun))
991 : 0 : TYPE_HAS_CONSTEXPR_CTOR (DECL_CONTEXT (fun)) = true;
992 : : }
993 : : else
994 : : /* Don't bother keeping the pre-generic body of unsuitable functions
995 : : not explicitly declared constexpr. */
996 : : return;
997 : : }
998 : :
999 : 16088844 : constexpr_fundef entry = {fun, NULL_TREE, NULL_TREE, NULL_TREE};
1000 : 16088844 : bool clear_ctx = false;
1001 : 16088844 : if (DECL_RESULT (fun) && DECL_CONTEXT (DECL_RESULT (fun)) == NULL_TREE)
1002 : : {
1003 : 16088844 : clear_ctx = true;
1004 : 16088844 : DECL_CONTEXT (DECL_RESULT (fun)) = fun;
1005 : : }
1006 : 16088844 : tree saved_fn = current_function_decl;
1007 : 16088844 : current_function_decl = fun;
1008 : 16088844 : entry.body = copy_fn (entry.decl, entry.parms, entry.result);
1009 : 16088844 : current_function_decl = saved_fn;
1010 : 16088844 : if (clear_ctx)
1011 : 16088844 : DECL_CONTEXT (DECL_RESULT (entry.decl)) = NULL_TREE;
1012 : 16088844 : if (!potential)
1013 : : /* For a template instantiation, we want to remember the pre-generic body
1014 : : for explain_invalid_constexpr_fn, but do tell cxx_eval_call_expression
1015 : : that it doesn't need to bother trying to expand the function. */
1016 : 72827 : entry.result = error_mark_node;
1017 : :
1018 : 16088844 : register_constexpr_fundef (entry);
1019 : : }
1020 : :
1021 : : /* BODY is a validated and massaged definition of a constexpr
1022 : : function. Register it in the hash table. */
1023 : :
1024 : : void
1025 : 16108300 : register_constexpr_fundef (const constexpr_fundef &value)
1026 : : {
1027 : : /* Create the constexpr function table if necessary. */
1028 : 16108300 : if (constexpr_fundef_table == NULL)
1029 : 24248 : constexpr_fundef_table
1030 : 24248 : = hash_table<constexpr_fundef_hasher>::create_ggc (101);
1031 : :
1032 : 16108300 : constexpr_fundef **slot = constexpr_fundef_table->find_slot
1033 : 16108300 : (const_cast<constexpr_fundef *> (&value), INSERT);
1034 : :
1035 : 16108300 : gcc_assert (*slot == NULL);
1036 : 16108300 : *slot = ggc_alloc<constexpr_fundef> ();
1037 : 16108300 : **slot = value;
1038 : 16108300 : }
1039 : :
1040 : : /* FUN is a non-constexpr (or, with -Wno-invalid-constexpr, a constexpr
1041 : : function called in a context that requires a constant expression).
1042 : : If it comes from a constexpr template, explain why the instantiation
1043 : : isn't constexpr. Otherwise, explain why the function cannot be used
1044 : : in a constexpr context. */
1045 : :
1046 : : void
1047 : 321 : explain_invalid_constexpr_fn (tree fun)
1048 : : {
1049 : 321 : static hash_set<tree> *diagnosed;
1050 : 321 : tree body;
1051 : : /* In C++23, a function marked 'constexpr' may not actually be a constant
1052 : : expression. We haven't diagnosed the problem yet: -Winvalid-constexpr
1053 : : wasn't enabled. The function was called, so diagnose why it cannot be
1054 : : used in a constant expression. */
1055 : 321 : if (warn_invalid_constexpr == 0 && DECL_DECLARED_CONSTEXPR_P (fun))
1056 : : /* Go on. */;
1057 : : /* Only diagnose defaulted functions, lambdas, or instantiations. */
1058 : 301 : else if (!DECL_DEFAULTED_FN (fun)
1059 : 439 : && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun))
1060 : 280 : && !(flag_implicit_constexpr
1061 : 6 : && !DECL_DECLARED_CONSTEXPR_P (fun)
1062 : 6 : && DECL_DECLARED_INLINE_P (fun))
1063 : 578 : && !is_instantiation_of_constexpr (fun))
1064 : : {
1065 : 262 : inform (DECL_SOURCE_LOCATION (fun), "%qD declared here", fun);
1066 : 3 : if (flag_implicit_constexpr && !maybe_constexpr_fn (fun)
1067 : 265 : && decl_defined_p (fun))
1068 : 3 : inform (DECL_SOURCE_LOCATION (fun),
1069 : : "%<-fimplicit-constexpr%> only affects %<inline%> functions");
1070 : 262 : return;
1071 : : }
1072 : 59 : if (diagnosed == NULL)
1073 : 52 : diagnosed = new hash_set<tree>;
1074 : 59 : if (diagnosed->add (fun))
1075 : : /* Already explained. */
1076 : : return;
1077 : :
1078 : 58 : iloc_sentinel ils = input_location;
1079 : 58 : if (!lambda_static_thunk_p (fun))
1080 : : {
1081 : : /* Diagnostics should completely ignore the static thunk, so leave
1082 : : input_location set to our caller's location. */
1083 : 58 : input_location = DECL_SOURCE_LOCATION (fun);
1084 : 58 : inform (input_location,
1085 : : "%qD is not usable as a %<constexpr%> function because:", fun);
1086 : : }
1087 : : /* First check the declaration. */
1088 : 58 : if (is_valid_constexpr_fn (fun, true))
1089 : : {
1090 : : /* Then if it's OK, the body. */
1091 : 51 : if (!DECL_DECLARED_CONSTEXPR_P (fun)
1092 : 51 : && DECL_DEFAULTED_FN (fun))
1093 : 10 : explain_implicit_non_constexpr (fun);
1094 : : else
1095 : : {
1096 : 41 : if (constexpr_fundef *fd = retrieve_constexpr_fundef (fun))
1097 : 34 : body = fd->body;
1098 : : else
1099 : 7 : body = DECL_SAVED_TREE (fun);
1100 : 41 : body = massage_constexpr_body (fun, body);
1101 : 41 : require_potential_rvalue_constant_expression (body);
1102 : 82 : if (DECL_CONSTRUCTOR_P (fun))
1103 : : {
1104 : 8 : cx_check_missing_mem_inits (DECL_CONTEXT (fun), body, true);
1105 : 8 : if (cxx_dialect > cxx11)
1106 : : {
1107 : : /* Also check the body, not just the ctor-initializer. */
1108 : 6 : body = DECL_SAVED_TREE (fun);
1109 : 6 : require_potential_rvalue_constant_expression (body);
1110 : : }
1111 : : }
1112 : : }
1113 : : }
1114 : 58 : }
1115 : :
1116 : : /* Objects of this type represent calls to constexpr functions
1117 : : along with the bindings of parameters to their arguments, for
1118 : : the purpose of compile time evaluation. */
1119 : :
1120 : : struct GTY((for_user)) constexpr_call {
1121 : : /* Description of the constexpr function definition. */
1122 : : constexpr_fundef *fundef;
1123 : : /* Parameter bindings environment. A TREE_VEC of arguments. */
1124 : : tree bindings;
1125 : : /* Result of the call.
1126 : : NULL means the call is being evaluated.
1127 : : error_mark_node means that the evaluation was erroneous or otherwise
1128 : : uncacheable (e.g. because it depends on the caller).
1129 : : Otherwise, the actual value of the call. */
1130 : : tree result;
1131 : : /* The hash of this call; we remember it here to avoid having to
1132 : : recalculate it when expanding the hash table. */
1133 : : hashval_t hash;
1134 : : /* The value of constexpr_ctx::manifestly_const_eval. */
1135 : : enum mce_value manifestly_const_eval;
1136 : : };
1137 : :
1138 : : struct constexpr_call_hasher : ggc_ptr_hash<constexpr_call>
1139 : : {
1140 : : static hashval_t hash (constexpr_call *);
1141 : : static bool equal (constexpr_call *, constexpr_call *);
1142 : : };
1143 : :
1144 : : enum constexpr_switch_state {
1145 : : /* Used when processing a switch for the first time by cxx_eval_switch_expr
1146 : : and default: label for that switch has not been seen yet. */
1147 : : css_default_not_seen,
1148 : : /* Used when processing a switch for the first time by cxx_eval_switch_expr
1149 : : and default: label for that switch has been seen already. */
1150 : : css_default_seen,
1151 : : /* Used when processing a switch for the second time by
1152 : : cxx_eval_switch_expr, where default: label should match. */
1153 : : css_default_processing
1154 : : };
1155 : :
1156 : : /* The constexpr expansion context part which needs one instance per
1157 : : cxx_eval_outermost_constant_expr invocation. VALUES is a map of values of
1158 : : variables initialized within the expression. */
1159 : :
1160 : 336227337 : class constexpr_global_ctx {
1161 : : /* Values for any temporaries or local variables within the
1162 : : constant-expression. Objects outside their lifetime have
1163 : : value 'void_node'. */
1164 : : hash_map<tree,tree> values;
1165 : : public:
1166 : : /* Number of cxx_eval_constant_expression calls (except skipped ones,
1167 : : on simple constants or location wrappers) encountered during current
1168 : : cxx_eval_outermost_constant_expr call. */
1169 : : HOST_WIDE_INT constexpr_ops_count;
1170 : : /* Heap VAR_DECLs created during the evaluation of the outermost constant
1171 : : expression. */
1172 : : auto_vec<tree, 16> heap_vars;
1173 : : /* Cleanups that need to be evaluated at the end of CLEANUP_POINT_EXPR. */
1174 : : vec<tree> *cleanups;
1175 : : /* If non-null, only allow modification of existing values of the variables
1176 : : in this set. Set by modifiable_tracker, below. */
1177 : : hash_set<tree> *modifiable;
1178 : : /* Number of heap VAR_DECL deallocations. */
1179 : : unsigned heap_dealloc_count;
1180 : : /* Constructor. */
1181 : 336230034 : constexpr_global_ctx ()
1182 : 672460068 : : constexpr_ops_count (0), cleanups (NULL), modifiable (nullptr),
1183 : 336230034 : heap_dealloc_count (0) {}
1184 : :
1185 : 189129246 : bool is_outside_lifetime (tree t)
1186 : : {
1187 : 189129246 : if (tree *p = values.get (t))
1188 : 44636436 : if (*p == void_node)
1189 : 178 : return true;
1190 : : return false;
1191 : : }
1192 : 238481988 : tree get_value (tree t)
1193 : : {
1194 : 238481988 : if (tree *p = values.get (t))
1195 : 83231095 : if (*p != void_node)
1196 : 82765603 : return *p;
1197 : : return NULL_TREE;
1198 : : }
1199 : 37700063 : tree *get_value_ptr (tree t, bool initializing)
1200 : : {
1201 : 37700063 : if (modifiable && !modifiable->contains (t))
1202 : : return nullptr;
1203 : 37700041 : if (tree *p = values.get (t))
1204 : : {
1205 : 37697071 : if (*p != void_node)
1206 : : return p;
1207 : 42 : else if (initializing)
1208 : : {
1209 : 12 : *p = NULL_TREE;
1210 : 12 : return p;
1211 : : }
1212 : : }
1213 : : return nullptr;
1214 : : }
1215 : 113640246 : void put_value (tree t, tree v)
1216 : : {
1217 : 113640246 : bool already_in_map = values.put (t, v);
1218 : 113640246 : if (!already_in_map && modifiable)
1219 : 40 : modifiable->add (t);
1220 : 113640246 : }
1221 : 89932641 : void destroy_value (tree t)
1222 : : {
1223 : 89932641 : if (TREE_CODE (t) == VAR_DECL
1224 : 89932641 : || TREE_CODE (t) == PARM_DECL
1225 : : || TREE_CODE (t) == RESULT_DECL)
1226 : 89927091 : values.put (t, void_node);
1227 : : else
1228 : 5550 : values.remove (t);
1229 : 89932641 : }
1230 : 4838660 : void clear_value (tree t)
1231 : : {
1232 : 9677320 : values.remove (t);
1233 : : }
1234 : : };
1235 : :
1236 : : /* Helper class for constexpr_global_ctx. In some cases we want to avoid
1237 : : side-effects from evaluation of a particular subexpression of a
1238 : : constant-expression. In such cases we use modifiable_tracker to prevent
1239 : : modification of variables created outside of that subexpression.
1240 : :
1241 : : ??? We could change the hash_set to a hash_map, allow and track external
1242 : : modifications, and roll them back in the destructor. It's not clear to me
1243 : : that this would be worthwhile. */
1244 : :
1245 : : class modifiable_tracker
1246 : : {
1247 : : hash_set<tree> set;
1248 : : constexpr_global_ctx *global;
1249 : : public:
1250 : 223119 : modifiable_tracker (constexpr_global_ctx *g): global(g)
1251 : : {
1252 : 223119 : global->modifiable = &set;
1253 : 223119 : }
1254 : 223119 : ~modifiable_tracker ()
1255 : : {
1256 : 223159 : for (tree t: set)
1257 : 40 : global->clear_value (t);
1258 : 223119 : global->modifiable = nullptr;
1259 : 223119 : }
1260 : : };
1261 : :
1262 : : /* The constexpr expansion context. CALL is the current function
1263 : : expansion, CTOR is the current aggregate initializer, OBJECT is the
1264 : : object being initialized by CTOR, either a VAR_DECL or a _REF. */
1265 : :
1266 : : struct constexpr_ctx {
1267 : : /* The part of the context that needs to be unique to the whole
1268 : : cxx_eval_outermost_constant_expr invocation. */
1269 : : constexpr_global_ctx *global;
1270 : : /* The innermost call we're evaluating. */
1271 : : constexpr_call *call;
1272 : : /* SAVE_EXPRs and TARGET_EXPR_SLOT vars of TARGET_EXPRs that we've seen
1273 : : within the current LOOP_EXPR. NULL if we aren't inside a loop. */
1274 : : vec<tree> *save_exprs;
1275 : : /* The CONSTRUCTOR we're currently building up for an aggregate
1276 : : initializer. */
1277 : : tree ctor;
1278 : : /* The object we're building the CONSTRUCTOR for. */
1279 : : tree object;
1280 : : /* If inside SWITCH_EXPR. */
1281 : : constexpr_switch_state *css_state;
1282 : : /* The aggregate initialization context inside which this one is nested. This
1283 : : is used by lookup_placeholder to resolve PLACEHOLDER_EXPRs. */
1284 : : const constexpr_ctx *parent;
1285 : :
1286 : : /* Whether we should error on a non-constant expression or fail quietly.
1287 : : This flag needs to be here, but some of the others could move to global
1288 : : if they get larger than a word. */
1289 : : bool quiet;
1290 : : /* Whether we are strictly conforming to constant expression rules or
1291 : : trying harder to get a constant value. */
1292 : : bool strict;
1293 : : /* Whether __builtin_is_constant_evaluated () should be true. */
1294 : : mce_value manifestly_const_eval;
1295 : : };
1296 : :
1297 : : /* Remove T from the global values map, checking for attempts to destroy
1298 : : a value that has already finished its lifetime. */
1299 : :
1300 : : static void
1301 : 89922162 : destroy_value_checked (const constexpr_ctx* ctx, tree t, bool *non_constant_p)
1302 : : {
1303 : 89922162 : if (t == error_mark_node || TREE_TYPE (t) == error_mark_node)
1304 : : return;
1305 : :
1306 : : /* Don't error again here if we've already reported a problem. */
1307 : 89922159 : if (!*non_constant_p
1308 : 44822038 : && DECL_P (t)
1309 : : /* Non-trivial destructors have their lifetimes ended explicitly
1310 : : with a clobber, so don't worry about it here. */
1311 : 44816630 : && (!TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (t))
1312 : : /* ...except parameters are remapped in cxx_eval_call_expression,
1313 : : and the destructor call during cleanup won't be able to tell that
1314 : : this value has already been destroyed, so complain now. This is
1315 : : not quite unobservable, but is extremely unlikely to crop up in
1316 : : practice; see g++.dg/cpp2a/constexpr-lifetime2.C. */
1317 : 169425 : || TREE_CODE (t) == PARM_DECL)
1318 : 134569579 : && ctx->global->is_outside_lifetime (t))
1319 : : {
1320 : 54 : if (!ctx->quiet)
1321 : : {
1322 : 18 : auto_diagnostic_group d;
1323 : 18 : error ("destroying %qE outside its lifetime", t);
1324 : 18 : inform (DECL_SOURCE_LOCATION (t), "declared here");
1325 : 18 : }
1326 : 54 : *non_constant_p = true;
1327 : : }
1328 : 89922159 : ctx->global->destroy_value (t);
1329 : : }
1330 : :
1331 : : /* This internal flag controls whether we should avoid doing anything during
1332 : : constexpr evaluation that would cause extra DECL_UID generation, such as
1333 : : template instantiation and function body copying. */
1334 : :
1335 : : static bool uid_sensitive_constexpr_evaluation_value;
1336 : :
1337 : : /* An internal counter that keeps track of the number of times
1338 : : uid_sensitive_constexpr_evaluation_p returned true. */
1339 : :
1340 : : static unsigned uid_sensitive_constexpr_evaluation_true_counter;
1341 : :
1342 : : /* The accessor for uid_sensitive_constexpr_evaluation_value which also
1343 : : increments the corresponding counter. */
1344 : :
1345 : : static bool
1346 : 6579723 : uid_sensitive_constexpr_evaluation_p ()
1347 : : {
1348 : 6575420 : if (uid_sensitive_constexpr_evaluation_value)
1349 : : {
1350 : 1715715 : ++uid_sensitive_constexpr_evaluation_true_counter;
1351 : 1715715 : return true;
1352 : : }
1353 : : else
1354 : : return false;
1355 : : }
1356 : :
1357 : : /* The default constructor for uid_sensitive_constexpr_evaluation_sentinel
1358 : : enables the internal flag for uid_sensitive_constexpr_evaluation_p
1359 : : during the lifetime of the sentinel object. Upon its destruction, the
1360 : : previous value of uid_sensitive_constexpr_evaluation_p is restored. */
1361 : :
1362 : 61658152 : uid_sensitive_constexpr_evaluation_sentinel
1363 : 61658152 : ::uid_sensitive_constexpr_evaluation_sentinel ()
1364 : 61658152 : : ovr (uid_sensitive_constexpr_evaluation_value, true)
1365 : : {
1366 : 61658152 : }
1367 : :
1368 : : /* The default constructor for uid_sensitive_constexpr_evaluation_checker
1369 : : records the current number of times that uid_sensitive_constexpr_evaluation_p
1370 : : has been called and returned true. */
1371 : :
1372 : 1781151686 : uid_sensitive_constexpr_evaluation_checker
1373 : 1781151686 : ::uid_sensitive_constexpr_evaluation_checker ()
1374 : 1781151686 : : saved_counter (uid_sensitive_constexpr_evaluation_true_counter)
1375 : : {
1376 : 1781151686 : }
1377 : :
1378 : : /* Returns true iff uid_sensitive_constexpr_evaluation_p is true, and
1379 : : some constexpr evaluation was restricted due to u_s_c_e_p being called
1380 : : and returning true during the lifetime of this checker object. */
1381 : :
1382 : : bool
1383 : 1253113808 : uid_sensitive_constexpr_evaluation_checker::evaluation_restricted_p () const
1384 : : {
1385 : 1253113808 : return (uid_sensitive_constexpr_evaluation_value
1386 : 1253113808 : && saved_counter != uid_sensitive_constexpr_evaluation_true_counter);
1387 : : }
1388 : :
1389 : :
1390 : : /* A table of all constexpr calls that have been evaluated by the
1391 : : compiler in this translation unit. */
1392 : :
1393 : : static GTY (()) hash_table<constexpr_call_hasher> *constexpr_call_table;
1394 : :
1395 : : /* Compute a hash value for a constexpr call representation. */
1396 : :
1397 : : inline hashval_t
1398 : 121872316 : constexpr_call_hasher::hash (constexpr_call *info)
1399 : : {
1400 : 121872316 : return info->hash;
1401 : : }
1402 : :
1403 : : /* Return true if the objects pointed to by P and Q represent calls
1404 : : to the same constexpr function with the same arguments.
1405 : : Otherwise, return false. */
1406 : :
1407 : : bool
1408 : 127739302 : constexpr_call_hasher::equal (constexpr_call *lhs, constexpr_call *rhs)
1409 : : {
1410 : 127739302 : if (lhs == rhs)
1411 : : return true;
1412 : 127739302 : if (lhs->hash != rhs->hash)
1413 : : return false;
1414 : 14238199 : if (lhs->manifestly_const_eval != rhs->manifestly_const_eval)
1415 : : return false;
1416 : 14238199 : if (!constexpr_fundef_hasher::equal (lhs->fundef, rhs->fundef))
1417 : : return false;
1418 : 14238199 : return cp_tree_equal (lhs->bindings, rhs->bindings);
1419 : : }
1420 : :
1421 : : /* Initialize the constexpr call table, if needed. */
1422 : :
1423 : : static void
1424 : 17533351 : maybe_initialize_constexpr_call_table (void)
1425 : : {
1426 : 17533351 : if (constexpr_call_table == NULL)
1427 : 16071 : constexpr_call_table = hash_table<constexpr_call_hasher>::create_ggc (101);
1428 : 17533351 : }
1429 : :
1430 : : /* During constexpr CALL_EXPR evaluation, to avoid issues with sharing when
1431 : : a function happens to get called recursively, we unshare the callee
1432 : : function's body and evaluate this unshared copy instead of evaluating the
1433 : : original body.
1434 : :
1435 : : FUNDEF_COPIES_TABLE is a per-function freelist of these unshared function
1436 : : copies. The underlying data structure of FUNDEF_COPIES_TABLE is a hash_map
1437 : : that's keyed off of the original FUNCTION_DECL and whose value is a
1438 : : TREE_LIST of this function's unused copies awaiting reuse.
1439 : :
1440 : : This is not GC-deletable to avoid GC affecting UID generation. */
1441 : :
1442 : : static GTY(()) decl_tree_map *fundef_copies_table;
1443 : :
1444 : : /* Reuse a copy or create a new unshared copy of the function FUN.
1445 : : Return this copy. We use a TREE_LIST whose PURPOSE is body, VALUE
1446 : : is parms, TYPE is result. */
1447 : :
1448 : : static tree
1449 : 38836813 : get_fundef_copy (constexpr_fundef *fundef)
1450 : : {
1451 : 38836813 : tree copy;
1452 : 38836813 : bool existed;
1453 : 38836813 : tree *slot = &(hash_map_safe_get_or_insert<hm_ggc>
1454 : 38836813 : (fundef_copies_table, fundef->decl, &existed, 127));
1455 : :
1456 : 38836813 : if (!existed)
1457 : : {
1458 : : /* There is no cached function available, or in use. We can use
1459 : : the function directly. That the slot is now created records
1460 : : that this function is now in use. */
1461 : 5519356 : copy = build_tree_list (fundef->body, fundef->parms);
1462 : 5519356 : TREE_TYPE (copy) = fundef->result;
1463 : : }
1464 : 33317457 : else if (*slot == NULL_TREE)
1465 : : {
1466 : 4303 : if (uid_sensitive_constexpr_evaluation_p ())
1467 : 0 : return NULL_TREE;
1468 : :
1469 : : /* We've already used the function itself, so make a copy. */
1470 : 4303 : copy = build_tree_list (NULL, NULL);
1471 : 4303 : tree saved_body = DECL_SAVED_TREE (fundef->decl);
1472 : 4303 : tree saved_parms = DECL_ARGUMENTS (fundef->decl);
1473 : 4303 : tree saved_result = DECL_RESULT (fundef->decl);
1474 : 4303 : tree saved_fn = current_function_decl;
1475 : 4303 : DECL_SAVED_TREE (fundef->decl) = fundef->body;
1476 : 4303 : DECL_ARGUMENTS (fundef->decl) = fundef->parms;
1477 : 4303 : DECL_RESULT (fundef->decl) = fundef->result;
1478 : 4303 : current_function_decl = fundef->decl;
1479 : 4303 : TREE_PURPOSE (copy) = copy_fn (fundef->decl, TREE_VALUE (copy),
1480 : 4303 : TREE_TYPE (copy));
1481 : 4303 : current_function_decl = saved_fn;
1482 : 4303 : DECL_RESULT (fundef->decl) = saved_result;
1483 : 4303 : DECL_ARGUMENTS (fundef->decl) = saved_parms;
1484 : 4303 : DECL_SAVED_TREE (fundef->decl) = saved_body;
1485 : : }
1486 : : else
1487 : : {
1488 : : /* We have a cached function available. */
1489 : 33313154 : copy = *slot;
1490 : 33313154 : *slot = TREE_CHAIN (copy);
1491 : : }
1492 : :
1493 : : return copy;
1494 : : }
1495 : :
1496 : : /* Save the copy COPY of function FUN for later reuse by
1497 : : get_fundef_copy(). By construction, there will always be an entry
1498 : : to find. */
1499 : :
1500 : : static void
1501 : 38836813 : save_fundef_copy (tree fun, tree copy)
1502 : : {
1503 : 38836813 : tree *slot = fundef_copies_table->get (fun);
1504 : 38836813 : TREE_CHAIN (copy) = *slot;
1505 : 38836813 : *slot = copy;
1506 : 38836813 : }
1507 : :
1508 : : /* Whether our evaluation wants a prvalue (e.g. CONSTRUCTOR or _CST),
1509 : : a glvalue (e.g. VAR_DECL or _REF), or nothing. */
1510 : :
1511 : : enum value_cat {
1512 : : vc_prvalue = 0,
1513 : : vc_glvalue = 1,
1514 : : vc_discard = 2
1515 : : };
1516 : :
1517 : : static tree cxx_eval_constant_expression (const constexpr_ctx *, tree,
1518 : : value_cat, bool *, bool *, tree * = NULL);
1519 : : static tree cxx_eval_bare_aggregate (const constexpr_ctx *, tree,
1520 : : value_cat, bool *, bool *);
1521 : : static tree cxx_fold_indirect_ref (const constexpr_ctx *, location_t, tree, tree,
1522 : : bool * = NULL);
1523 : : static tree find_heap_var_refs (tree *, int *, void *);
1524 : : static tree find_deleted_heap_var (tree *, int *, void *);
1525 : :
1526 : : /* Attempt to evaluate T which represents a call to a builtin function.
1527 : : We assume here that all builtin functions evaluate to scalar types
1528 : : represented by _CST nodes. */
1529 : :
1530 : : static tree
1531 : 9986664 : cxx_eval_builtin_function_call (const constexpr_ctx *ctx, tree t, tree fun,
1532 : : value_cat lval,
1533 : : bool *non_constant_p, bool *overflow_p)
1534 : : {
1535 : 9986664 : const int nargs = call_expr_nargs (t);
1536 : 9986664 : tree *args = (tree *) alloca (nargs * sizeof (tree));
1537 : 9986664 : tree new_call;
1538 : 9986664 : int i;
1539 : :
1540 : : /* Don't fold __builtin_constant_p within a constexpr function. */
1541 : 9986664 : bool bi_const_p = DECL_IS_BUILTIN_CONSTANT_P (fun);
1542 : :
1543 : : /* If we aren't requiring a constant expression, defer __builtin_constant_p
1544 : : in a constexpr function until we have values for the parameters. */
1545 : 650855 : if (bi_const_p
1546 : 650855 : && ctx->manifestly_const_eval != mce_true
1547 : 641240 : && current_function_decl
1548 : 639086 : && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
1549 : : {
1550 : 352141 : *non_constant_p = true;
1551 : 352141 : return t;
1552 : : }
1553 : :
1554 : : /* For __builtin_is_constant_evaluated, defer it if not
1555 : : ctx->manifestly_const_eval (as sometimes we try to constant evaluate
1556 : : without manifestly_const_eval even expressions or parts thereof which
1557 : : will later be manifestly const_eval evaluated), otherwise fold it to
1558 : : true. */
1559 : 9634523 : if (fndecl_built_in_p (fun, CP_BUILT_IN_IS_CONSTANT_EVALUATED,
1560 : : BUILT_IN_FRONTEND))
1561 : : {
1562 : 1879615 : if (ctx->manifestly_const_eval == mce_unknown)
1563 : : {
1564 : 1864195 : *non_constant_p = true;
1565 : 1864195 : return t;
1566 : : }
1567 : 15420 : return constant_boolean_node (ctx->manifestly_const_eval == mce_true,
1568 : 15420 : boolean_type_node);
1569 : : }
1570 : :
1571 : 7754908 : if (fndecl_built_in_p (fun, CP_BUILT_IN_SOURCE_LOCATION, BUILT_IN_FRONTEND))
1572 : : {
1573 : 403 : temp_override<tree> ovr (current_function_decl);
1574 : 403 : if (ctx->call && ctx->call->fundef)
1575 : 137 : current_function_decl = ctx->call->fundef->decl;
1576 : 403 : return fold_builtin_source_location (t);
1577 : 403 : }
1578 : :
1579 : 7754505 : int strops = 0;
1580 : 7754505 : int strret = 0;
1581 : 7754505 : if (fndecl_built_in_p (fun, BUILT_IN_NORMAL))
1582 : 7106852 : switch (DECL_FUNCTION_CODE (fun))
1583 : : {
1584 : : case BUILT_IN_STRLEN:
1585 : : case BUILT_IN_STRNLEN:
1586 : 7754436 : strops = 1;
1587 : : break;
1588 : 103043 : case BUILT_IN_MEMCHR:
1589 : 103043 : case BUILT_IN_STRCHR:
1590 : 103043 : case BUILT_IN_STRRCHR:
1591 : 103043 : strops = 1;
1592 : 103043 : strret = 1;
1593 : 103043 : break;
1594 : 24124 : case BUILT_IN_MEMCMP:
1595 : 24124 : case BUILT_IN_STRCMP:
1596 : 24124 : strops = 2;
1597 : 24124 : break;
1598 : 29745 : case BUILT_IN_STRSTR:
1599 : 29745 : strops = 2;
1600 : 29745 : strret = 1;
1601 : 29745 : break;
1602 : 42 : case BUILT_IN_ASAN_POINTER_COMPARE:
1603 : 42 : case BUILT_IN_ASAN_POINTER_SUBTRACT:
1604 : : /* These builtins shall be ignored during constant expression
1605 : : evaluation. */
1606 : 42 : return void_node;
1607 : 27 : case BUILT_IN_UNREACHABLE:
1608 : 27 : case BUILT_IN_TRAP:
1609 : 27 : if (!*non_constant_p && !ctx->quiet)
1610 : : {
1611 : : /* Do not allow__builtin_unreachable in constexpr function.
1612 : : The __builtin_unreachable call with BUILTINS_LOCATION
1613 : : comes from cp_maybe_instrument_return. */
1614 : 1 : if (EXPR_LOCATION (t) == BUILTINS_LOCATION)
1615 : 0 : error ("%<constexpr%> call flows off the end of the function");
1616 : : else
1617 : 1 : error ("%q+E is not a constant expression", t);
1618 : : }
1619 : 27 : *non_constant_p = true;
1620 : 27 : return t;
1621 : : default:
1622 : : break;
1623 : : }
1624 : :
1625 : : /* Be permissive for arguments to built-ins; __builtin_constant_p should
1626 : : return constant false for a non-constant argument. */
1627 : 7754436 : constexpr_ctx new_ctx = *ctx;
1628 : 7754436 : new_ctx.quiet = true;
1629 : 20723436 : for (i = 0; i < nargs; ++i)
1630 : : {
1631 : 12969000 : tree arg = CALL_EXPR_ARG (t, i);
1632 : 12969000 : tree oarg = arg;
1633 : :
1634 : : /* To handle string built-ins we need to pass ADDR_EXPR<STRING_CST> since
1635 : : expand_builtin doesn't know how to look in the values table. */
1636 : 12969000 : bool strop = i < strops;
1637 : 12969000 : if (strop)
1638 : : {
1639 : 286786 : STRIP_NOPS (arg);
1640 : 286786 : if (TREE_CODE (arg) == ADDR_EXPR)
1641 : 8022 : arg = TREE_OPERAND (arg, 0);
1642 : : else
1643 : : strop = false;
1644 : : }
1645 : :
1646 : : /* If builtin_valid_in_constant_expr_p is true,
1647 : : potential_constant_expression_1 has not recursed into the arguments
1648 : : of the builtin, verify it here. */
1649 : 12969000 : if (!builtin_valid_in_constant_expr_p (fun)
1650 : 12969000 : || potential_constant_expression (arg))
1651 : : {
1652 : 12968865 : bool dummy1 = false, dummy2 = false;
1653 : 12968865 : arg = cxx_eval_constant_expression (&new_ctx, arg, vc_prvalue,
1654 : : &dummy1, &dummy2);
1655 : : }
1656 : :
1657 : 12969000 : if (bi_const_p)
1658 : : /* For __builtin_constant_p, fold all expressions with constant values
1659 : : even if they aren't C++ constant-expressions. */
1660 : 298714 : arg = cp_fold_rvalue (arg);
1661 : 12670286 : else if (strop)
1662 : : {
1663 : 8022 : if (TREE_CODE (arg) == CONSTRUCTOR)
1664 : 113 : arg = braced_lists_to_strings (TREE_TYPE (arg), arg);
1665 : 8022 : if (TREE_CODE (arg) == STRING_CST)
1666 : 3064 : arg = build_address (arg);
1667 : : else
1668 : : arg = oarg;
1669 : : }
1670 : :
1671 : 12969000 : args[i] = arg;
1672 : : }
1673 : :
1674 : 7754436 : bool save_ffbcp = force_folding_builtin_constant_p;
1675 : 7754436 : force_folding_builtin_constant_p |= ctx->manifestly_const_eval == mce_true;
1676 : 7754436 : tree save_cur_fn = current_function_decl;
1677 : : /* Return name of ctx->call->fundef->decl for __builtin_FUNCTION (). */
1678 : 7754436 : if (fndecl_built_in_p (fun, BUILT_IN_FUNCTION)
1679 : 35 : && ctx->call
1680 : 7754440 : && ctx->call->fundef)
1681 : 4 : current_function_decl = ctx->call->fundef->decl;
1682 : 7754436 : if (fndecl_built_in_p (fun,
1683 : : CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS,
1684 : : BUILT_IN_FRONTEND))
1685 : : {
1686 : 400 : location_t loc = EXPR_LOCATION (t);
1687 : 400 : if (nargs >= 1)
1688 : 397 : VERIFY_CONSTANT (args[0]);
1689 : 195 : new_call
1690 : 195 : = fold_builtin_is_pointer_inverconvertible_with_class (loc, nargs,
1691 : : args);
1692 : : }
1693 : 7754036 : else if (fndecl_built_in_p (fun,
1694 : : CP_BUILT_IN_IS_CORRESPONDING_MEMBER,
1695 : : BUILT_IN_FRONTEND))
1696 : : {
1697 : 575 : location_t loc = EXPR_LOCATION (t);
1698 : 575 : if (nargs >= 2)
1699 : : {
1700 : 569 : VERIFY_CONSTANT (args[0]);
1701 : 347 : VERIFY_CONSTANT (args[1]);
1702 : : }
1703 : 353 : new_call = fold_builtin_is_corresponding_member (loc, nargs, args);
1704 : : }
1705 : : else
1706 : 15506922 : new_call = fold_builtin_call_array (EXPR_LOCATION (t), TREE_TYPE (t),
1707 : 7753461 : CALL_EXPR_FN (t), nargs, args);
1708 : 7754009 : current_function_decl = save_cur_fn;
1709 : 7754009 : force_folding_builtin_constant_p = save_ffbcp;
1710 : 7754009 : if (new_call == NULL)
1711 : : {
1712 : 5619209 : if (!*non_constant_p && !ctx->quiet)
1713 : : {
1714 : 0 : new_call = build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t),
1715 : 0 : CALL_EXPR_FN (t), nargs, args);
1716 : 0 : error ("%q+E is not a constant expression", new_call);
1717 : : }
1718 : 5619209 : *non_constant_p = true;
1719 : 5619209 : return t;
1720 : : }
1721 : :
1722 : 2134800 : if (!potential_constant_expression (new_call))
1723 : : {
1724 : 582 : if (!*non_constant_p && !ctx->quiet)
1725 : 4 : error ("%q+E is not a constant expression", new_call);
1726 : 582 : *non_constant_p = true;
1727 : 582 : return t;
1728 : : }
1729 : :
1730 : 2134218 : if (strret)
1731 : : {
1732 : : /* memchr returns a pointer into the first argument, but we replaced the
1733 : : argument above with a STRING_CST; put it back it now. */
1734 : 219 : tree op = CALL_EXPR_ARG (t, strret-1);
1735 : 219 : STRIP_NOPS (new_call);
1736 : 219 : if (TREE_CODE (new_call) == POINTER_PLUS_EXPR)
1737 : 107 : TREE_OPERAND (new_call, 0) = op;
1738 : 112 : else if (TREE_CODE (new_call) == ADDR_EXPR)
1739 : 2134218 : new_call = op;
1740 : : }
1741 : :
1742 : 2134218 : return cxx_eval_constant_expression (&new_ctx, new_call, lval,
1743 : 2134218 : non_constant_p, overflow_p);
1744 : : }
1745 : :
1746 : : /* TEMP is the constant value of a temporary object of type TYPE. Adjust
1747 : : the type of the value to match. */
1748 : :
1749 : : static tree
1750 : 51565217 : adjust_temp_type (tree type, tree temp)
1751 : : {
1752 : 51565217 : if (same_type_p (TREE_TYPE (temp), type))
1753 : : return temp;
1754 : : /* Avoid wrapping an aggregate value in a NOP_EXPR. */
1755 : 32066856 : if (TREE_CODE (temp) == CONSTRUCTOR)
1756 : : {
1757 : : /* build_constructor wouldn't retain various CONSTRUCTOR flags. */
1758 : 2407687 : tree t = copy_node (temp);
1759 : 2407687 : TREE_TYPE (t) = type;
1760 : 2407687 : return t;
1761 : : }
1762 : 29659169 : if (TREE_CODE (temp) == EMPTY_CLASS_EXPR)
1763 : 0 : return build0 (EMPTY_CLASS_EXPR, type);
1764 : 29659169 : gcc_assert (scalarish_type_p (type));
1765 : : /* Now we know we're dealing with a scalar, and a prvalue of non-class
1766 : : type is cv-unqualified. */
1767 : 29659169 : return cp_fold_convert (cv_unqualified (type), temp);
1768 : : }
1769 : :
1770 : : /* If T is a CONSTRUCTOR, return an unshared copy of T and any
1771 : : sub-CONSTRUCTORs. Otherwise return T.
1772 : :
1773 : : We use this whenever we initialize an object as a whole, whether it's a
1774 : : parameter, a local variable, or a subobject, so that subsequent
1775 : : modifications don't affect other places where it was used. */
1776 : :
1777 : : tree
1778 : 33803468 : unshare_constructor (tree t MEM_STAT_DECL)
1779 : : {
1780 : 33803468 : if (!t || TREE_CODE (t) != CONSTRUCTOR)
1781 : : return t;
1782 : 6692775 : auto_vec <tree*, 4> ptrs;
1783 : 6692775 : ptrs.safe_push (&t);
1784 : 6692775 : while (!ptrs.is_empty ())
1785 : : {
1786 : 7513865 : tree *p = ptrs.pop ();
1787 : 7513865 : tree n = copy_node (*p PASS_MEM_STAT);
1788 : 7513865 : CONSTRUCTOR_ELTS (n) = vec_safe_copy (CONSTRUCTOR_ELTS (*p) PASS_MEM_STAT);
1789 : 7513865 : *p = n;
1790 : 7513865 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (n);
1791 : 7513865 : constructor_elt *ce;
1792 : 29860058 : for (HOST_WIDE_INT i = 0; vec_safe_iterate (v, i, &ce); ++i)
1793 : 8139553 : if (ce->value && TREE_CODE (ce->value) == CONSTRUCTOR)
1794 : 821090 : ptrs.safe_push (&ce->value);
1795 : : }
1796 : 6692775 : return t;
1797 : 6692775 : }
1798 : :
1799 : : /* If T is a CONSTRUCTOR, ggc_free T and any sub-CONSTRUCTORs. */
1800 : :
1801 : : static void
1802 : 683209 : free_constructor (tree t)
1803 : : {
1804 : 683209 : if (!t || TREE_CODE (t) != CONSTRUCTOR)
1805 : 0 : return;
1806 : 683209 : releasing_vec ctors;
1807 : 683209 : vec_safe_push (ctors, t);
1808 : 1373773 : while (!ctors->is_empty ())
1809 : : {
1810 : 690564 : tree c = ctors->pop ();
1811 : 690564 : if (vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (c))
1812 : : {
1813 : : constructor_elt *ce;
1814 : 112749 : for (HOST_WIDE_INT i = 0; vec_safe_iterate (elts, i, &ce); ++i)
1815 : 75269 : if (ce->value && TREE_CODE (ce->value) == CONSTRUCTOR)
1816 : 7355 : vec_safe_push (ctors, ce->value);
1817 : 37480 : ggc_free (elts);
1818 : : }
1819 : 690564 : ggc_free (c);
1820 : : }
1821 : 683209 : }
1822 : :
1823 : : /* Helper function of cxx_bind_parameters_in_call. Return non-NULL
1824 : : if *TP is address of a static variable (or part of it) currently being
1825 : : constructed or of a heap artificial variable. */
1826 : :
1827 : : static tree
1828 : 8452513 : addr_of_non_const_var (tree *tp, int *walk_subtrees, void *data)
1829 : : {
1830 : 8452513 : if (TREE_CODE (*tp) == ADDR_EXPR)
1831 : 1082371 : if (tree var = get_base_address (TREE_OPERAND (*tp, 0)))
1832 : 1082371 : if (VAR_P (var) && TREE_STATIC (var))
1833 : : {
1834 : 785785 : if (DECL_NAME (var) == heap_uninit_identifier
1835 : 785785 : || DECL_NAME (var) == heap_identifier
1836 : 624695 : || DECL_NAME (var) == heap_vec_uninit_identifier
1837 : 1410480 : || DECL_NAME (var) == heap_vec_identifier)
1838 : : return var;
1839 : :
1840 : 624684 : constexpr_global_ctx *global = (constexpr_global_ctx *) data;
1841 : 624684 : if (global->get_value (var))
1842 : : return var;
1843 : : }
1844 : 8184415 : if (TYPE_P (*tp))
1845 : 1505 : *walk_subtrees = false;
1846 : : return NULL_TREE;
1847 : : }
1848 : :
1849 : : /* Subroutine of cxx_eval_call_expression.
1850 : : We are processing a call expression (either CALL_EXPR or
1851 : : AGGR_INIT_EXPR) in the context of CTX. Evaluate
1852 : : all arguments and bind their values to correspondings
1853 : : parameters, making up the NEW_CALL context. */
1854 : :
1855 : : static tree
1856 : 68875064 : cxx_bind_parameters_in_call (const constexpr_ctx *ctx, tree t, tree fun,
1857 : : bool *non_constant_p, bool *overflow_p,
1858 : : bool *non_constant_args)
1859 : : {
1860 : 68875064 : const int nargs = call_expr_nargs (t);
1861 : 68875064 : tree parms = DECL_ARGUMENTS (fun);
1862 : 68875064 : int i;
1863 : : /* We don't record ellipsis args below. */
1864 : 68875064 : int nparms = list_length (parms);
1865 : 68875064 : int nbinds = nargs < nparms ? nargs : nparms;
1866 : 68875064 : tree binds = make_tree_vec (nbinds);
1867 : :
1868 : : /* The call is not a constant expression if it involves the cdtor for a type
1869 : : with virtual bases. */
1870 : 68875064 : if (DECL_HAS_IN_CHARGE_PARM_P (fun) || DECL_HAS_VTT_PARM_P (fun))
1871 : : {
1872 : 24 : if (!ctx->quiet)
1873 : : {
1874 : 3 : error_at (cp_expr_loc_or_input_loc (t),
1875 : : "call to non-%<constexpr%> function %qD", fun);
1876 : 3 : explain_invalid_constexpr_fn (fun);
1877 : : }
1878 : 24 : *non_constant_p = true;
1879 : 24 : return binds;
1880 : : }
1881 : :
1882 : 123883554 : for (i = 0; i < nargs; ++i)
1883 : : {
1884 : 71246117 : tree x, arg;
1885 : 71246117 : tree type = parms ? TREE_TYPE (parms) : void_type_node;
1886 : 71246117 : if (parms && DECL_BY_REFERENCE (parms))
1887 : 7730 : type = TREE_TYPE (type);
1888 : 71246117 : x = get_nth_callarg (t, i);
1889 : : /* For member function, the first argument is a pointer to the implied
1890 : : object. For a constructor, it might still be a dummy object, in
1891 : : which case we get the real argument from ctx. */
1892 : 110051476 : if (i == 0 && DECL_CONSTRUCTOR_P (fun)
1893 : 80331773 : && is_dummy_object (x))
1894 : : {
1895 : 4146935 : x = ctx->object;
1896 : 4146935 : x = build_address (x);
1897 : : }
1898 : 71246117 : if (TREE_ADDRESSABLE (type))
1899 : : {
1900 : : /* Undo convert_for_arg_passing work here. */
1901 : 12263 : x = convert_from_reference (x);
1902 : 12263 : arg = cxx_eval_constant_expression (ctx, x, vc_glvalue,
1903 : : non_constant_p, overflow_p);
1904 : : }
1905 : : else
1906 : : /* Normally we would strip a TARGET_EXPR in an initialization context
1907 : : such as this, but here we do the elision differently: we keep the
1908 : : TARGET_EXPR, and use its CONSTRUCTOR as the value of the parm. */
1909 : 71233854 : arg = cxx_eval_constant_expression (ctx, x, vc_prvalue,
1910 : : non_constant_p, overflow_p);
1911 : : /* Check we aren't dereferencing a null pointer when calling a non-static
1912 : : member function, which is undefined behaviour. */
1913 : 55025738 : if (i == 0 && DECL_OBJECT_MEMBER_FUNCTION_P (fun)
1914 : 29854211 : && integer_zerop (arg)
1915 : : /* But ignore calls from within compiler-generated code, to handle
1916 : : cases like lambda function pointer conversion operator thunks
1917 : : which pass NULL as the 'this' pointer. */
1918 : 71268055 : && !(TREE_CODE (t) == CALL_EXPR && CALL_FROM_THUNK_P (t)))
1919 : : {
1920 : 226 : if (!ctx->quiet)
1921 : 6 : error_at (cp_expr_loc_or_input_loc (x),
1922 : : "dereferencing a null pointer");
1923 : 226 : *non_constant_p = true;
1924 : : }
1925 : : /* Don't VERIFY_CONSTANT here. */
1926 : 71246117 : if (*non_constant_p && ctx->quiet)
1927 : : break;
1928 : : /* Just discard ellipsis args after checking their constantitude. */
1929 : 55008514 : if (!parms)
1930 : 448 : continue;
1931 : :
1932 : 55008066 : if (!*non_constant_p)
1933 : : {
1934 : : /* Make sure the binding has the same type as the parm. But
1935 : : only for constant args. */
1936 : 55007829 : if (TREE_ADDRESSABLE (type))
1937 : : {
1938 : 7829 : if (!same_type_p (type, TREE_TYPE (arg)))
1939 : : {
1940 : 9 : arg = build_fold_addr_expr (arg);
1941 : 9 : arg = cp_fold_convert (build_reference_type (type), arg);
1942 : 9 : arg = convert_from_reference (arg);
1943 : : }
1944 : : }
1945 : 55000000 : else if (!TYPE_REF_P (type))
1946 : 37905878 : arg = adjust_temp_type (type, arg);
1947 : 55007829 : if (!TREE_CONSTANT (arg))
1948 : 45782242 : *non_constant_args = true;
1949 : 9225587 : else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
1950 : : /* The destructor needs to see any modifications the callee makes
1951 : : to the argument. */
1952 : 0 : *non_constant_args = true;
1953 : : /* If arg is or contains address of a heap artificial variable or
1954 : : of a static variable being constructed, avoid caching the
1955 : : function call, as those variables might be modified by the
1956 : : function, or might be modified by the callers in between
1957 : : the cached function and just read by the function. */
1958 : 9225587 : else if (!*non_constant_args
1959 : 9225587 : && cp_walk_tree (&arg, addr_of_non_const_var, ctx->global,
1960 : : NULL))
1961 : 268098 : *non_constant_args = true;
1962 : :
1963 : : /* For virtual calls, adjust the this argument, so that it is
1964 : : the object on which the method is called, rather than
1965 : : one of its bases. */
1966 : 55007829 : if (i == 0 && DECL_VIRTUAL_P (fun))
1967 : : {
1968 : 6537 : tree addr = arg;
1969 : 6537 : STRIP_NOPS (addr);
1970 : 6537 : if (TREE_CODE (addr) == ADDR_EXPR)
1971 : : {
1972 : 6528 : tree obj = TREE_OPERAND (addr, 0);
1973 : 6528 : while (TREE_CODE (obj) == COMPONENT_REF
1974 : 3164 : && DECL_FIELD_IS_BASE (TREE_OPERAND (obj, 1))
1975 : 9694 : && !same_type_ignoring_top_level_qualifiers_p
1976 : 3141 : (TREE_TYPE (obj), DECL_CONTEXT (fun)))
1977 : 25 : obj = TREE_OPERAND (obj, 0);
1978 : 6528 : if (obj != TREE_OPERAND (addr, 0))
1979 : 22 : arg = build_fold_addr_expr_with_type (obj,
1980 : : TREE_TYPE (arg));
1981 : : }
1982 : : }
1983 : 55007829 : TREE_VEC_ELT (binds, i) = arg;
1984 : : }
1985 : 55008066 : parms = TREE_CHAIN (parms);
1986 : : }
1987 : :
1988 : : return binds;
1989 : : }
1990 : :
1991 : : /* Variables and functions to manage constexpr call expansion context.
1992 : : These do not need to be marked for PCH or GC. */
1993 : :
1994 : : /* FIXME remember and print actual constant arguments. */
1995 : : static vec<tree> call_stack;
1996 : : static int call_stack_tick;
1997 : : static int last_cx_error_tick;
1998 : :
1999 : : static int
2000 : 50426694 : push_cx_call_context (tree call)
2001 : : {
2002 : 50426694 : ++call_stack_tick;
2003 : 50426694 : if (!EXPR_HAS_LOCATION (call))
2004 : 7415 : SET_EXPR_LOCATION (call, input_location);
2005 : 50426694 : call_stack.safe_push (call);
2006 : 50426694 : int len = call_stack.length ();
2007 : 50426694 : if (len > max_constexpr_depth)
2008 : 24 : return false;
2009 : : return len;
2010 : : }
2011 : :
2012 : : static void
2013 : 50426694 : pop_cx_call_context (void)
2014 : : {
2015 : 50426694 : ++call_stack_tick;
2016 : 50426694 : call_stack.pop ();
2017 : 50426694 : }
2018 : :
2019 : : vec<tree>
2020 : 209141 : cx_error_context (void)
2021 : : {
2022 : 209141 : vec<tree> r = vNULL;
2023 : 209141 : if (call_stack_tick != last_cx_error_tick
2024 : 209141 : && !call_stack.is_empty ())
2025 : : r = call_stack;
2026 : 209141 : last_cx_error_tick = call_stack_tick;
2027 : 209141 : return r;
2028 : : }
2029 : :
2030 : : /* E is an operand of a failed assertion, fold it either with or without
2031 : : constexpr context. */
2032 : :
2033 : : static tree
2034 : 562 : fold_operand (tree e, const constexpr_ctx *ctx)
2035 : : {
2036 : 562 : if (ctx)
2037 : : {
2038 : 70 : bool new_non_constant_p = false, new_overflow_p = false;
2039 : 70 : e = cxx_eval_constant_expression (ctx, e, vc_prvalue,
2040 : : &new_non_constant_p,
2041 : : &new_overflow_p);
2042 : : }
2043 : : else
2044 : 492 : e = fold_non_dependent_expr (e, tf_none, /*manifestly_const_eval=*/true);
2045 : 562 : return e;
2046 : : }
2047 : :
2048 : : /* If we have a condition in conjunctive normal form (CNF), find the first
2049 : : failing clause. In other words, given an expression like
2050 : :
2051 : : true && true && false && true && false
2052 : :
2053 : : return the first 'false'. EXPR is the expression. */
2054 : :
2055 : : static tree
2056 : 372 : find_failing_clause_r (const constexpr_ctx *ctx, tree expr)
2057 : : {
2058 : 478 : if (TREE_CODE (expr) == TRUTH_ANDIF_EXPR)
2059 : : {
2060 : : /* First check the left side... */
2061 : 212 : tree e = find_failing_clause_r (ctx, TREE_OPERAND (expr, 0));
2062 : 212 : if (e == NULL_TREE)
2063 : : /* ...if we didn't find a false clause, check the right side. */
2064 : 106 : e = find_failing_clause_r (ctx, TREE_OPERAND (expr, 1));
2065 : : return e;
2066 : : }
2067 : 266 : tree e = contextual_conv_bool (expr, tf_none);
2068 : 266 : e = fold_operand (e, ctx);
2069 : 266 : if (integer_zerop (e))
2070 : : /* This is the failing clause. */
2071 : 160 : return expr;
2072 : : return NULL_TREE;
2073 : : }
2074 : :
2075 : : /* Wrapper for find_failing_clause_r. */
2076 : :
2077 : : tree
2078 : 1244 : find_failing_clause (const constexpr_ctx *ctx, tree expr)
2079 : : {
2080 : 1244 : if (TREE_CODE (expr) == TRUTH_ANDIF_EXPR)
2081 : 160 : if (tree e = find_failing_clause_r (ctx, expr))
2082 : 1244 : expr = e;
2083 : 1244 : return expr;
2084 : : }
2085 : :
2086 : : /* Emit additional diagnostics for failing condition BAD.
2087 : : Used by finish_static_assert and IFN_ASSUME constexpr diagnostics.
2088 : : If SHOW_EXPR_P is true, print the condition (because it was
2089 : : instantiation-dependent). */
2090 : :
2091 : : void
2092 : 1244 : diagnose_failing_condition (tree bad, location_t cloc, bool show_expr_p,
2093 : : const constexpr_ctx *ctx /* = nullptr */)
2094 : : {
2095 : : /* Nobody wants to see the artificial (bool) cast. */
2096 : 1244 : bad = tree_strip_nop_conversions (bad);
2097 : 1244 : if (TREE_CODE (bad) == CLEANUP_POINT_EXPR)
2098 : 4 : bad = TREE_OPERAND (bad, 0);
2099 : :
2100 : : /* Actually explain the failure if this is a concept check or a
2101 : : requires-expression. */
2102 : 1244 : if (concept_check_p (bad) || TREE_CODE (bad) == REQUIRES_EXPR)
2103 : 224 : diagnose_constraints (cloc, bad, NULL_TREE);
2104 : 1020 : else if (COMPARISON_CLASS_P (bad)
2105 : 1020 : && ARITHMETIC_TYPE_P (TREE_TYPE (TREE_OPERAND (bad, 0))))
2106 : : {
2107 : 148 : tree op0 = fold_operand (TREE_OPERAND (bad, 0), ctx);
2108 : 148 : tree op1 = fold_operand (TREE_OPERAND (bad, 1), ctx);
2109 : 148 : tree cond = build2 (TREE_CODE (bad), boolean_type_node, op0, op1);
2110 : 148 : inform (cloc, "the comparison reduces to %qE", cond);
2111 : : }
2112 : 872 : else if (show_expr_p)
2113 : 687 : inform (cloc, "%qE evaluates to false", bad);
2114 : 1244 : }
2115 : :
2116 : : /* Process an assert/assume of ORIG_ARG. If it's not supposed to be evaluated,
2117 : : do it without changing the current evaluation state. If it evaluates to
2118 : : false, complain and return false; otherwise, return true. */
2119 : :
2120 : : static bool
2121 : 223232 : cxx_eval_assert (const constexpr_ctx *ctx, tree arg, const char *msg,
2122 : : location_t loc, bool evaluated,
2123 : : bool *non_constant_p, bool *overflow_p)
2124 : : {
2125 : 223232 : if (*non_constant_p)
2126 : : return true;
2127 : :
2128 : 223232 : tree eval;
2129 : 223232 : if (!evaluated)
2130 : : {
2131 : 223161 : if (!potential_rvalue_constant_expression (arg))
2132 : 42 : return true;
2133 : :
2134 : 223119 : constexpr_ctx new_ctx = *ctx;
2135 : 223119 : new_ctx.quiet = true;
2136 : 223119 : bool new_non_constant_p = false, new_overflow_p = false;
2137 : : /* Avoid modification of existing values. */
2138 : 223119 : modifiable_tracker ms (new_ctx.global);
2139 : 223119 : eval = cxx_eval_constant_expression (&new_ctx, arg, vc_prvalue,
2140 : : &new_non_constant_p,
2141 : : &new_overflow_p);
2142 : 223119 : }
2143 : : else
2144 : 71 : eval = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
2145 : : non_constant_p,
2146 : : overflow_p);
2147 : 223190 : if (!*non_constant_p && integer_zerop (eval))
2148 : : {
2149 : 99 : if (!ctx->quiet)
2150 : : {
2151 : : /* See if we can find which clause was failing
2152 : : (for logical AND). */
2153 : 32 : tree bad = find_failing_clause (ctx, arg);
2154 : : /* If not, or its location is unusable, fall back to the
2155 : : previous location. */
2156 : 32 : location_t cloc = cp_expr_loc_or_loc (bad, loc);
2157 : :
2158 : : /* Report the error. */
2159 : 32 : auto_diagnostic_group d;
2160 : 32 : error_at (cloc, msg);
2161 : 32 : diagnose_failing_condition (bad, cloc, true, ctx);
2162 : 32 : return bad;
2163 : 32 : }
2164 : 67 : *non_constant_p = true;
2165 : 67 : return false;
2166 : : }
2167 : :
2168 : : return true;
2169 : : }
2170 : :
2171 : : /* Evaluate a call T to a GCC internal function when possible and return
2172 : : the evaluated result or, under the control of CTX, give an error, set
2173 : : NON_CONSTANT_P, and return the unevaluated call T otherwise. */
2174 : :
2175 : : static tree
2176 : 265750 : cxx_eval_internal_function (const constexpr_ctx *ctx, tree t,
2177 : : value_cat lval,
2178 : : bool *non_constant_p, bool *overflow_p)
2179 : : {
2180 : 265750 : enum tree_code opcode = ERROR_MARK;
2181 : :
2182 : 265750 : switch (CALL_EXPR_IFN (t))
2183 : : {
2184 : 32 : case IFN_UBSAN_NULL:
2185 : 32 : case IFN_UBSAN_BOUNDS:
2186 : 32 : case IFN_UBSAN_VPTR:
2187 : 32 : case IFN_FALLTHROUGH:
2188 : 32 : return void_node;
2189 : :
2190 : 223142 : case IFN_ASSUME:
2191 : 223142 : if (!cxx_eval_assert (ctx, CALL_EXPR_ARG (t, 0),
2192 : : G_("failed %<assume%> attribute assumption"),
2193 : 223142 : EXPR_LOCATION (t), /*eval*/false,
2194 : : non_constant_p, overflow_p))
2195 : : return t;
2196 : 223113 : return void_node;
2197 : :
2198 : : case IFN_ADD_OVERFLOW:
2199 : : opcode = PLUS_EXPR;
2200 : : break;
2201 : 339 : case IFN_SUB_OVERFLOW:
2202 : 339 : opcode = MINUS_EXPR;
2203 : 339 : break;
2204 : 41676 : case IFN_MUL_OVERFLOW:
2205 : 41676 : opcode = MULT_EXPR;
2206 : 41676 : break;
2207 : :
2208 : 73 : case IFN_LAUNDER:
2209 : 73 : return cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0),
2210 : : vc_prvalue, non_constant_p,
2211 : 73 : overflow_p);
2212 : :
2213 : 30 : case IFN_VEC_CONVERT:
2214 : 30 : {
2215 : 30 : tree arg = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0),
2216 : : vc_prvalue, non_constant_p,
2217 : : overflow_p);
2218 : 30 : if (TREE_CODE (arg) == VECTOR_CST)
2219 : 19 : if (tree r = fold_const_call (CFN_VEC_CONVERT, TREE_TYPE (t), arg))
2220 : : return r;
2221 : : }
2222 : : /* FALLTHRU */
2223 : :
2224 : 16 : default:
2225 : 16 : if (!ctx->quiet)
2226 : 0 : error_at (cp_expr_loc_or_input_loc (t),
2227 : : "call to internal function %qE", t);
2228 : 16 : *non_constant_p = true;
2229 : 16 : return t;
2230 : : }
2231 : :
2232 : : /* Evaluate constant arguments using OPCODE and return a complex
2233 : : number containing the result and the overflow bit. */
2234 : 42473 : tree arg0 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), lval,
2235 : : non_constant_p, overflow_p);
2236 : 42473 : tree arg1 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 1), lval,
2237 : : non_constant_p, overflow_p);
2238 : :
2239 : 42473 : if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2240 : : {
2241 : 2 : location_t loc = cp_expr_loc_or_input_loc (t);
2242 : 2 : tree type = TREE_TYPE (TREE_TYPE (t));
2243 : 2 : tree result = fold_binary_loc (loc, opcode, type,
2244 : : fold_convert_loc (loc, type, arg0),
2245 : : fold_convert_loc (loc, type, arg1));
2246 : 2 : tree ovf
2247 : 2 : = build_int_cst (type, arith_overflowed_p (opcode, type, arg0, arg1));
2248 : : /* Reset TREE_OVERFLOW to avoid warnings for the overflow. */
2249 : 2 : if (TREE_OVERFLOW (result))
2250 : 0 : TREE_OVERFLOW (result) = 0;
2251 : :
2252 : 2 : return build_complex (TREE_TYPE (t), result, ovf);
2253 : : }
2254 : :
2255 : 42471 : *non_constant_p = true;
2256 : 42471 : return t;
2257 : : }
2258 : :
2259 : : /* Clean CONSTRUCTOR_NO_CLEARING from CTOR and its sub-aggregates. */
2260 : :
2261 : : static void
2262 : 3325371 : clear_no_implicit_zero (tree ctor)
2263 : : {
2264 : 3325371 : if (CONSTRUCTOR_NO_CLEARING (ctor))
2265 : : {
2266 : 764446 : CONSTRUCTOR_NO_CLEARING (ctor) = false;
2267 : 3494285 : for (auto &e: CONSTRUCTOR_ELTS (ctor))
2268 : 1242293 : if (TREE_CODE (e.value) == CONSTRUCTOR)
2269 : 256933 : clear_no_implicit_zero (e.value);
2270 : : }
2271 : 3325371 : }
2272 : :
2273 : : /* Complain about a const object OBJ being modified in a constant expression.
2274 : : EXPR is the MODIFY_EXPR expression performing the modification. */
2275 : :
2276 : : static void
2277 : 63 : modifying_const_object_error (tree expr, tree obj)
2278 : : {
2279 : 63 : location_t loc = cp_expr_loc_or_input_loc (expr);
2280 : 63 : auto_diagnostic_group d;
2281 : 126 : error_at (loc, "modifying a const object %qE is not allowed in "
2282 : 63 : "a constant expression", TREE_OPERAND (expr, 0));
2283 : :
2284 : : /* Find the underlying object that was declared as const. */
2285 : 63 : location_t decl_loc = UNKNOWN_LOCATION;
2286 : 138 : for (tree probe = obj; decl_loc == UNKNOWN_LOCATION; )
2287 : 75 : switch (TREE_CODE (probe))
2288 : : {
2289 : 39 : case BIT_FIELD_REF:
2290 : 39 : case COMPONENT_REF:
2291 : 39 : {
2292 : 39 : tree elt = TREE_OPERAND (probe, 1);
2293 : 39 : if (CP_TYPE_CONST_P (TREE_TYPE (elt)))
2294 : 27 : decl_loc = DECL_SOURCE_LOCATION (elt);
2295 : 39 : probe = TREE_OPERAND (probe, 0);
2296 : : }
2297 : 39 : break;
2298 : :
2299 : 0 : case ARRAY_REF:
2300 : 0 : case REALPART_EXPR:
2301 : 0 : case IMAGPART_EXPR:
2302 : 0 : probe = TREE_OPERAND (probe, 0);
2303 : 0 : break;
2304 : :
2305 : 36 : default:
2306 : 36 : decl_loc = location_of (probe);
2307 : 36 : break;
2308 : : }
2309 : 63 : inform (decl_loc, "originally declared %<const%> here");
2310 : 63 : }
2311 : :
2312 : : /* Return true if FNDECL is a replaceable global allocation function that
2313 : : should be useable during constant expression evaluation. */
2314 : :
2315 : : static inline bool
2316 : 28121410 : cxx_replaceable_global_alloc_fn (tree fndecl)
2317 : : {
2318 : 28121410 : return (cxx_dialect >= cxx20
2319 : 7143692 : && IDENTIFIER_NEWDEL_OP_P (DECL_NAME (fndecl))
2320 : 260820 : && CP_DECL_CONTEXT (fndecl) == global_namespace
2321 : 28382083 : && (DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl)
2322 : 102185 : || DECL_IS_OPERATOR_DELETE_P (fndecl)));
2323 : : }
2324 : :
2325 : : /* Return true if FNDECL is a placement new function that should be
2326 : : useable during constant expression evaluation of std::construct_at. */
2327 : :
2328 : : static inline bool
2329 : 27960670 : cxx_placement_new_fn (tree fndecl)
2330 : : {
2331 : 27960670 : return (cxx_dialect >= cxx20 && std_placement_new_fn_p (fndecl));
2332 : : }
2333 : :
2334 : : /* Return true if FNDECL is std::construct_at. */
2335 : :
2336 : : static inline bool
2337 : 98175 : is_std_construct_at (tree fndecl)
2338 : : {
2339 : 98175 : if (!decl_in_std_namespace_p (fndecl))
2340 : : return false;
2341 : :
2342 : 93193 : tree name = DECL_NAME (fndecl);
2343 : 93193 : return name && id_equal (name, "construct_at");
2344 : : }
2345 : :
2346 : : /* Overload for the above taking constexpr_call*. */
2347 : :
2348 : : static inline bool
2349 : 67630 : is_std_construct_at (const constexpr_call *call)
2350 : : {
2351 : 67630 : return (call
2352 : 52294 : && call->fundef
2353 : 119924 : && is_std_construct_at (call->fundef->decl));
2354 : : }
2355 : :
2356 : : /* True if CTX is an instance of std::NAME class. */
2357 : :
2358 : : bool
2359 : 163182 : is_std_class (tree ctx, const char *name)
2360 : : {
2361 : 163182 : if (ctx == NULL_TREE || !CLASS_TYPE_P (ctx) || !TYPE_MAIN_DECL (ctx))
2362 : : return false;
2363 : :
2364 : 162702 : tree decl = TYPE_MAIN_DECL (ctx);
2365 : 162702 : tree dname = DECL_NAME (decl);
2366 : 162702 : if (dname == NULL_TREE || !id_equal (dname, name))
2367 : : return false;
2368 : :
2369 : 162608 : return decl_in_std_namespace_p (decl);
2370 : : }
2371 : :
2372 : : /* True if CTX is an instance of std::allocator. */
2373 : :
2374 : : bool
2375 : 62932 : is_std_allocator (tree ctx)
2376 : : {
2377 : 62932 : return is_std_class (ctx, "allocator");
2378 : : }
2379 : :
2380 : : /* Return true if FNDECL is std::allocator<T>::{,de}allocate. */
2381 : :
2382 : : static inline bool
2383 : 69598 : is_std_allocator_allocate (tree fndecl)
2384 : : {
2385 : 69598 : tree name = DECL_NAME (fndecl);
2386 : 69598 : if (name == NULL_TREE
2387 : 69598 : || !(id_equal (name, "allocate") || id_equal (name, "deallocate")))
2388 : : return false;
2389 : :
2390 : 59220 : return is_std_allocator (DECL_CONTEXT (fndecl));
2391 : : }
2392 : :
2393 : : /* Overload for the above taking constexpr_call*. */
2394 : :
2395 : : static inline bool
2396 : 24969 : is_std_allocator_allocate (const constexpr_call *call)
2397 : : {
2398 : 24969 : return (call
2399 : 6890 : && call->fundef
2400 : 31859 : && is_std_allocator_allocate (call->fundef->decl));
2401 : : }
2402 : :
2403 : : /* Return true if FNDECL is std::source_location::current. */
2404 : :
2405 : : static inline bool
2406 : 4323 : is_std_source_location_current (tree fndecl)
2407 : : {
2408 : 4323 : if (!decl_in_std_namespace_p (fndecl))
2409 : : return false;
2410 : :
2411 : 892 : tree name = DECL_NAME (fndecl);
2412 : 892 : if (name == NULL_TREE || !id_equal (name, "current"))
2413 : : return false;
2414 : :
2415 : 177 : tree ctx = DECL_CONTEXT (fndecl);
2416 : 177 : if (ctx == NULL_TREE || !CLASS_TYPE_P (ctx) || !TYPE_MAIN_DECL (ctx))
2417 : : return false;
2418 : :
2419 : 177 : name = DECL_NAME (TYPE_MAIN_DECL (ctx));
2420 : 177 : return name && id_equal (name, "source_location");
2421 : : }
2422 : :
2423 : : /* Overload for the above taking constexpr_call*. */
2424 : :
2425 : : static inline bool
2426 : 4954 : is_std_source_location_current (const constexpr_call *call)
2427 : : {
2428 : 4954 : return (call
2429 : 4323 : && call->fundef
2430 : 9277 : && is_std_source_location_current (call->fundef->decl));
2431 : : }
2432 : :
2433 : : /* Return true if FNDECL is __dynamic_cast. */
2434 : :
2435 : : static inline bool
2436 : 27914699 : cxx_dynamic_cast_fn_p (tree fndecl)
2437 : : {
2438 : 27914699 : return (cxx_dialect >= cxx20
2439 : 6936973 : && id_equal (DECL_NAME (fndecl), "__dynamic_cast")
2440 : 27918415 : && CP_DECL_CONTEXT (fndecl) == abi_node);
2441 : : }
2442 : :
2443 : : /* Often, we have an expression in the form of address + offset, e.g.
2444 : : "&_ZTV1A + 16". Extract the object from it, i.e. "_ZTV1A". */
2445 : :
2446 : : static tree
2447 : 3251 : extract_obj_from_addr_offset (tree expr)
2448 : : {
2449 : 3251 : if (TREE_CODE (expr) == POINTER_PLUS_EXPR)
2450 : 1516 : expr = TREE_OPERAND (expr, 0);
2451 : 3251 : STRIP_NOPS (expr);
2452 : 3251 : if (TREE_CODE (expr) == ADDR_EXPR)
2453 : 3189 : expr = TREE_OPERAND (expr, 0);
2454 : 3251 : return expr;
2455 : : }
2456 : :
2457 : : /* Given a PATH like
2458 : :
2459 : : g.D.2181.D.2154.D.2102.D.2093
2460 : :
2461 : : find a component with type TYPE. Return NULL_TREE if not found, and
2462 : : error_mark_node if the component is not accessible. If STOP is non-null,
2463 : : this function will return NULL_TREE if STOP is found before TYPE. */
2464 : :
2465 : : static tree
2466 : 1561 : get_component_with_type (tree path, tree type, tree stop)
2467 : : {
2468 : 4779 : while (true)
2469 : : {
2470 : 3170 : if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path), type))
2471 : : /* Found it. */
2472 : : return path;
2473 : 2491 : else if (stop
2474 : 2491 : && (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path),
2475 : : stop)))
2476 : : return NULL_TREE;
2477 : 2446 : else if (TREE_CODE (path) == COMPONENT_REF
2478 : 2446 : && DECL_FIELD_IS_BASE (TREE_OPERAND (path, 1)))
2479 : : {
2480 : : /* We need to check that the component we're accessing is in fact
2481 : : accessible. */
2482 : 2446 : if (TREE_PRIVATE (TREE_OPERAND (path, 1))
2483 : 2446 : || TREE_PROTECTED (TREE_OPERAND (path, 1)))
2484 : 837 : return error_mark_node;
2485 : 1609 : path = TREE_OPERAND (path, 0);
2486 : : }
2487 : : else
2488 : : return NULL_TREE;
2489 : : }
2490 : : }
2491 : :
2492 : : /* Evaluate a call to __dynamic_cast (permitted by P1327R1).
2493 : :
2494 : : The declaration of __dynamic_cast is:
2495 : :
2496 : : void* __dynamic_cast (const void* __src_ptr,
2497 : : const __class_type_info* __src_type,
2498 : : const __class_type_info* __dst_type,
2499 : : ptrdiff_t __src2dst);
2500 : :
2501 : : where src2dst has the following possible values
2502 : :
2503 : : >-1: src_type is a unique public non-virtual base of dst_type
2504 : : dst_ptr + src2dst == src_ptr
2505 : : -1: unspecified relationship
2506 : : -2: src_type is not a public base of dst_type
2507 : : -3: src_type is a multiple public non-virtual base of dst_type
2508 : :
2509 : : Since literal types can't have virtual bases, we only expect hint >=0,
2510 : : -2, or -3. */
2511 : :
2512 : : static tree
2513 : 1756 : cxx_eval_dynamic_cast_fn (const constexpr_ctx *ctx, tree call,
2514 : : bool *non_constant_p, bool *overflow_p)
2515 : : {
2516 : : /* T will be something like
2517 : : __dynamic_cast ((B*) b, &_ZTI1B, &_ZTI1D, 8)
2518 : : dismantle it. */
2519 : 1756 : gcc_assert (call_expr_nargs (call) == 4);
2520 : 1756 : tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
2521 : 1756 : tree obj = CALL_EXPR_ARG (call, 0);
2522 : 1756 : tree type = CALL_EXPR_ARG (call, 2);
2523 : 1756 : HOST_WIDE_INT hint = int_cst_value (CALL_EXPR_ARG (call, 3));
2524 : 1756 : location_t loc = cp_expr_loc_or_input_loc (call);
2525 : :
2526 : : /* Get the target type of the dynamic_cast. */
2527 : 1756 : gcc_assert (TREE_CODE (type) == ADDR_EXPR);
2528 : 1756 : type = TREE_OPERAND (type, 0);
2529 : 1756 : type = TREE_TYPE (DECL_NAME (type));
2530 : :
2531 : : /* TYPE can only be either T* or T&. We can't know which of these it
2532 : : is by looking at TYPE, but OBJ will be "(T*) x" in the first case,
2533 : : and something like "(T*)(T&)(T*) x" in the second case. */
2534 : 1756 : bool reference_p = false;
2535 : 9686 : while (CONVERT_EXPR_P (obj) || TREE_CODE (obj) == SAVE_EXPR)
2536 : : {
2537 : 7930 : reference_p |= TYPE_REF_P (TREE_TYPE (obj));
2538 : 7930 : obj = TREE_OPERAND (obj, 0);
2539 : : }
2540 : :
2541 : : /* Evaluate the object so that we know its dynamic type. */
2542 : 1756 : obj = cxx_eval_constant_expression (ctx, obj, vc_prvalue, non_constant_p,
2543 : : overflow_p);
2544 : 1756 : if (*non_constant_p)
2545 : : return call;
2546 : :
2547 : : /* We expect OBJ to be in form of &d.D.2102 when HINT == 0,
2548 : : but when HINT is > 0, it can also be something like
2549 : : &d.D.2102 + 18446744073709551608, which includes the BINFO_OFFSET. */
2550 : 1735 : obj = extract_obj_from_addr_offset (obj);
2551 : 1735 : const tree objtype = TREE_TYPE (obj);
2552 : : /* If OBJ doesn't refer to a base field, we're done. */
2553 : 3392 : if (tree t = (TREE_CODE (obj) == COMPONENT_REF
2554 : 1735 : ? TREE_OPERAND (obj, 1) : obj))
2555 : 1735 : if (TREE_CODE (t) != FIELD_DECL || !DECL_FIELD_IS_BASE (t))
2556 : : {
2557 : 78 : if (reference_p)
2558 : : {
2559 : 72 : if (!ctx->quiet)
2560 : : {
2561 : 3 : auto_diagnostic_group d;
2562 : 3 : error_at (loc, "reference %<dynamic_cast%> failed");
2563 : 3 : inform (loc, "dynamic type %qT of its operand does "
2564 : : "not have a base class of type %qT",
2565 : : objtype, type);
2566 : 3 : }
2567 : 72 : *non_constant_p = true;
2568 : : }
2569 : 78 : return integer_zero_node;
2570 : : }
2571 : :
2572 : : /* [class.cdtor] When a dynamic_cast is used in a constructor ...
2573 : : or in a destructor ... if the operand of the dynamic_cast refers
2574 : : to the object under construction or destruction, this object is
2575 : : considered to be a most derived object that has the type of the
2576 : : constructor or destructor's class. */
2577 : 1657 : tree vtable = build_vfield_ref (obj, objtype);
2578 : 1657 : vtable = cxx_eval_constant_expression (ctx, vtable, vc_prvalue,
2579 : : non_constant_p, overflow_p);
2580 : 1657 : if (*non_constant_p)
2581 : : return call;
2582 : : /* With -fsanitize=vptr, we initialize all vtable pointers to null,
2583 : : so it's possible that we got a null pointer now. */
2584 : 1528 : if (integer_zerop (vtable))
2585 : : {
2586 : 12 : if (!ctx->quiet)
2587 : 3 : error_at (loc, "virtual table pointer is used uninitialized");
2588 : 12 : *non_constant_p = true;
2589 : 12 : return integer_zero_node;
2590 : : }
2591 : : /* VTABLE will be &_ZTV1A + 16 or similar, get _ZTV1A. */
2592 : 1516 : vtable = extract_obj_from_addr_offset (vtable);
2593 : 1516 : const tree mdtype = DECL_CONTEXT (vtable);
2594 : :
2595 : : /* Given dynamic_cast<T>(v),
2596 : :
2597 : : [expr.dynamic.cast] If C is the class type to which T points or refers,
2598 : : the runtime check logically executes as follows:
2599 : :
2600 : : If, in the most derived object pointed (referred) to by v, v points
2601 : : (refers) to a public base class subobject of a C object, and if only
2602 : : one object of type C is derived from the subobject pointed (referred)
2603 : : to by v the result points (refers) to that C object.
2604 : :
2605 : : In this case, HINT >= 0 or -3. */
2606 : 1516 : if (hint >= 0 || hint == -3)
2607 : : {
2608 : : /* Look for a component with type TYPE. */
2609 : 601 : tree t = get_component_with_type (obj, type, mdtype);
2610 : : /* If not accessible, give an error. */
2611 : 601 : if (t == error_mark_node)
2612 : : {
2613 : 198 : if (reference_p)
2614 : : {
2615 : 162 : if (!ctx->quiet)
2616 : : {
2617 : 18 : auto_diagnostic_group d;
2618 : 18 : error_at (loc, "reference %<dynamic_cast%> failed");
2619 : 18 : inform (loc, "static type %qT of its operand is a "
2620 : : "non-public base class of dynamic type %qT",
2621 : : objtype, type);
2622 : :
2623 : 18 : }
2624 : 162 : *non_constant_p = true;
2625 : : }
2626 : 198 : return integer_zero_node;
2627 : : }
2628 : 403 : else if (t)
2629 : : /* The result points to the TYPE object. */
2630 : 358 : return cp_build_addr_expr (t, complain);
2631 : : /* Else, TYPE was not found, because the HINT turned out to be wrong.
2632 : : Fall through to the normal processing. */
2633 : : }
2634 : :
2635 : : /* Otherwise, if v points (refers) to a public base class subobject of the
2636 : : most derived object, and the type of the most derived object has a base
2637 : : class, of type C, that is unambiguous and public, the result points
2638 : : (refers) to the C subobject of the most derived object.
2639 : :
2640 : : But it can also be an invalid case. */
2641 : :
2642 : : /* Get the most derived object. */
2643 : 960 : obj = get_component_with_type (obj, mdtype, NULL_TREE);
2644 : 960 : if (obj == error_mark_node)
2645 : : {
2646 : 639 : if (reference_p)
2647 : : {
2648 : 525 : if (!ctx->quiet)
2649 : : {
2650 : 57 : auto_diagnostic_group d;
2651 : 57 : error_at (loc, "reference %<dynamic_cast%> failed");
2652 : 57 : inform (loc, "static type %qT of its operand is a non-public"
2653 : : " base class of dynamic type %qT", objtype, mdtype);
2654 : 57 : }
2655 : 525 : *non_constant_p = true;
2656 : : }
2657 : 639 : return integer_zero_node;
2658 : : }
2659 : : else
2660 : 321 : gcc_assert (obj);
2661 : :
2662 : : /* Check that the type of the most derived object has a base class
2663 : : of type TYPE that is unambiguous and public. */
2664 : 321 : base_kind b_kind;
2665 : 321 : tree binfo = lookup_base (mdtype, type, ba_check, &b_kind, tf_none);
2666 : 321 : if (!binfo || binfo == error_mark_node)
2667 : : {
2668 : 237 : if (reference_p)
2669 : : {
2670 : 201 : if (!ctx->quiet)
2671 : : {
2672 : 24 : auto_diagnostic_group d;
2673 : 24 : error_at (loc, "reference %<dynamic_cast%> failed");
2674 : 24 : if (b_kind == bk_ambig)
2675 : 9 : inform (loc, "%qT is an ambiguous base class of dynamic "
2676 : : "type %qT of its operand", type, mdtype);
2677 : : else
2678 : 15 : inform (loc, "dynamic type %qT of its operand does not "
2679 : : "have an unambiguous public base class %qT",
2680 : : mdtype, type);
2681 : 24 : }
2682 : 201 : *non_constant_p = true;
2683 : : }
2684 : 237 : return integer_zero_node;
2685 : : }
2686 : : /* If so, return the TYPE subobject of the most derived object. */
2687 : 84 : obj = convert_to_base_statically (obj, binfo);
2688 : 84 : return cp_build_addr_expr (obj, complain);
2689 : : }
2690 : :
2691 : : /* Data structure used by replace_decl and replace_decl_r. */
2692 : :
2693 : : struct replace_decl_data
2694 : : {
2695 : : /* The _DECL we want to replace. */
2696 : : tree decl;
2697 : : /* The replacement for DECL. */
2698 : : tree replacement;
2699 : : /* Trees we've visited. */
2700 : : hash_set<tree> *pset;
2701 : : /* Whether we've performed any replacements. */
2702 : : bool changed;
2703 : : };
2704 : :
2705 : : /* Helper function for replace_decl, called through cp_walk_tree. */
2706 : :
2707 : : static tree
2708 : 2505062 : replace_decl_r (tree *tp, int *walk_subtrees, void *data)
2709 : : {
2710 : 2505062 : replace_decl_data *d = (replace_decl_data *) data;
2711 : :
2712 : : /* We could be replacing
2713 : : &<retval>.bar -> &foo.bar
2714 : : where foo is a static VAR_DECL, so we need to recompute TREE_CONSTANT
2715 : : on the ADDR_EXPR around it. */
2716 : 2505062 : if (TREE_CODE (*tp) == ADDR_EXPR)
2717 : : {
2718 : 175856 : d->pset->add (*tp);
2719 : 175856 : auto save_changed = d->changed;
2720 : 175856 : d->changed = false;
2721 : 175856 : cp_walk_tree (&TREE_OPERAND (*tp, 0), replace_decl_r, d, nullptr);
2722 : 175856 : if (d->changed)
2723 : : {
2724 : 274 : cxx_mark_addressable (*tp);
2725 : 274 : recompute_tree_invariant_for_addr_expr (*tp);
2726 : : }
2727 : : else
2728 : 175582 : d->changed = save_changed;
2729 : 175856 : *walk_subtrees = 0;
2730 : : }
2731 : 2329206 : else if (*tp == d->decl)
2732 : : {
2733 : 381 : *tp = unshare_expr (d->replacement);
2734 : 381 : d->changed = true;
2735 : 381 : *walk_subtrees = 0;
2736 : : }
2737 : 2328825 : else if (TYPE_P (*tp)
2738 : 2328825 : || d->pset->add (*tp))
2739 : 562825 : *walk_subtrees = 0;
2740 : :
2741 : 2505062 : return NULL_TREE;
2742 : : }
2743 : :
2744 : : /* Replace every occurrence of DECL with (an unshared copy of)
2745 : : REPLACEMENT within the expression *TP. Returns true iff a
2746 : : replacement was performed. */
2747 : :
2748 : : bool
2749 : 342635 : replace_decl (tree *tp, tree decl, tree replacement)
2750 : : {
2751 : 342635 : gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
2752 : : (TREE_TYPE (decl), TREE_TYPE (replacement)));
2753 : 342635 : hash_set<tree> pset;
2754 : 342635 : replace_decl_data data = { decl, replacement, &pset, false };
2755 : 342635 : cp_walk_tree (tp, replace_decl_r, &data, NULL);
2756 : 342635 : return data.changed;
2757 : 342635 : }
2758 : :
2759 : : /* Evaluate the call T to virtual function thunk THUNK_FNDECL. */
2760 : :
2761 : : static tree
2762 : 26 : cxx_eval_thunk_call (const constexpr_ctx *ctx, tree t, tree thunk_fndecl,
2763 : : value_cat lval,
2764 : : bool *non_constant_p, bool *overflow_p)
2765 : : {
2766 : 26 : tree function = THUNK_TARGET (thunk_fndecl);
2767 : :
2768 : 26 : if (THUNK_VIRTUAL_OFFSET (thunk_fndecl))
2769 : : {
2770 : 11 : if (!ctx->quiet)
2771 : : {
2772 : 3 : if (!DECL_DECLARED_CONSTEXPR_P (function))
2773 : : {
2774 : 0 : error ("call to non-%<constexpr%> function %qD", function);
2775 : 0 : explain_invalid_constexpr_fn (function);
2776 : : }
2777 : : else
2778 : : /* virtual_offset is only set for virtual bases, which make the
2779 : : class non-literal, so we don't need to handle it here. */
2780 : 3 : error ("calling constexpr member function %qD through virtual "
2781 : : "base subobject", function);
2782 : : }
2783 : 11 : *non_constant_p = true;
2784 : 11 : return t;
2785 : : }
2786 : :
2787 : 15 : tree new_call = copy_node (t);
2788 : 15 : CALL_EXPR_FN (new_call) = function;
2789 : 15 : TREE_TYPE (new_call) = TREE_TYPE (TREE_TYPE (function));
2790 : :
2791 : 15 : tree offset = size_int (THUNK_FIXED_OFFSET (thunk_fndecl));
2792 : :
2793 : 15 : if (DECL_THIS_THUNK_P (thunk_fndecl))
2794 : : {
2795 : : /* 'this'-adjusting thunk. */
2796 : 9 : tree this_arg = CALL_EXPR_ARG (t, 0);
2797 : 9 : this_arg = build2 (POINTER_PLUS_EXPR, TREE_TYPE (this_arg),
2798 : : this_arg, offset);
2799 : 9 : CALL_EXPR_ARG (new_call, 0) = this_arg;
2800 : : }
2801 : : else
2802 : : /* Return-adjusting thunk. */
2803 : 6 : new_call = build2 (POINTER_PLUS_EXPR, TREE_TYPE (new_call),
2804 : : new_call, offset);
2805 : :
2806 : 15 : return cxx_eval_constant_expression (ctx, new_call, lval,
2807 : 15 : non_constant_p, overflow_p);
2808 : : }
2809 : :
2810 : : /* If OBJECT is of const class type, evaluate it to a CONSTRUCTOR and set
2811 : : its TREE_READONLY flag according to READONLY_P. Used for constexpr
2812 : : 'tors to detect modifying const objects in a constexpr context. */
2813 : :
2814 : : static void
2815 : 5083954 : cxx_set_object_constness (const constexpr_ctx *ctx, tree object,
2816 : : bool readonly_p, bool *non_constant_p,
2817 : : bool *overflow_p)
2818 : : {
2819 : 10167908 : if (CLASS_TYPE_P (TREE_TYPE (object))
2820 : 10167908 : && CP_TYPE_CONST_P (TREE_TYPE (object)))
2821 : : {
2822 : : /* Subobjects might not be stored in ctx->global->values but we
2823 : : can get its CONSTRUCTOR by evaluating *this. */
2824 : 651253 : tree e = cxx_eval_constant_expression (ctx, object, vc_prvalue,
2825 : : non_constant_p, overflow_p);
2826 : 651253 : if (TREE_CODE (e) == CONSTRUCTOR && !*non_constant_p)
2827 : 612882 : TREE_READONLY (e) = readonly_p;
2828 : : }
2829 : 5083954 : }
2830 : :
2831 : : /* Subroutine of cxx_eval_constant_expression.
2832 : : Evaluate the call expression tree T in the context of OLD_CALL expression
2833 : : evaluation. */
2834 : :
2835 : : static tree
2836 : 79301452 : cxx_eval_call_expression (const constexpr_ctx *ctx, tree t,
2837 : : value_cat lval,
2838 : : bool *non_constant_p, bool *overflow_p)
2839 : : {
2840 : 79301452 : location_t loc = cp_expr_loc_or_input_loc (t);
2841 : 79301452 : tree fun = get_function_named_in_call (t);
2842 : 79301452 : constexpr_call new_call
2843 : 79301452 : = { NULL, NULL, NULL, 0, ctx->manifestly_const_eval };
2844 : 79301452 : int depth_ok;
2845 : :
2846 : 79301452 : if (fun == NULL_TREE)
2847 : 265750 : return cxx_eval_internal_function (ctx, t, lval,
2848 : 265750 : non_constant_p, overflow_p);
2849 : :
2850 : 79035702 : if (TREE_CODE (fun) != FUNCTION_DECL)
2851 : : {
2852 : : /* Might be a constexpr function pointer. */
2853 : 84781 : fun = cxx_eval_constant_expression (ctx, fun, vc_prvalue,
2854 : : non_constant_p, overflow_p);
2855 : 84781 : STRIP_NOPS (fun);
2856 : 84781 : if (TREE_CODE (fun) == ADDR_EXPR)
2857 : 8467 : fun = TREE_OPERAND (fun, 0);
2858 : : /* For TARGET_VTABLE_USES_DESCRIPTORS targets, there is no
2859 : : indirection, the called expression is a pointer into the
2860 : : virtual table which should contain FDESC_EXPR. Extract the
2861 : : FUNCTION_DECL from there. */
2862 : : else if (TARGET_VTABLE_USES_DESCRIPTORS
2863 : : && TREE_CODE (fun) == POINTER_PLUS_EXPR
2864 : : && TREE_CODE (TREE_OPERAND (fun, 0)) == ADDR_EXPR
2865 : : && TREE_CODE (TREE_OPERAND (fun, 1)) == INTEGER_CST)
2866 : : {
2867 : : tree d = TREE_OPERAND (TREE_OPERAND (fun, 0), 0);
2868 : : if (VAR_P (d)
2869 : : && DECL_VTABLE_OR_VTT_P (d)
2870 : : && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE
2871 : : && TREE_TYPE (TREE_TYPE (d)) == vtable_entry_type
2872 : : && DECL_INITIAL (d)
2873 : : && TREE_CODE (DECL_INITIAL (d)) == CONSTRUCTOR)
2874 : : {
2875 : : tree i = int_const_binop (TRUNC_DIV_EXPR, TREE_OPERAND (fun, 1),
2876 : : TYPE_SIZE_UNIT (vtable_entry_type));
2877 : : HOST_WIDE_INT idx = find_array_ctor_elt (DECL_INITIAL (d), i);
2878 : : if (idx >= 0)
2879 : : {
2880 : : tree fdesc
2881 : : = (*CONSTRUCTOR_ELTS (DECL_INITIAL (d)))[idx].value;
2882 : : if (TREE_CODE (fdesc) == FDESC_EXPR
2883 : : && integer_zerop (TREE_OPERAND (fdesc, 1)))
2884 : : fun = TREE_OPERAND (fdesc, 0);
2885 : : }
2886 : : }
2887 : : }
2888 : : }
2889 : 79035702 : if (TREE_CODE (fun) != FUNCTION_DECL)
2890 : : {
2891 : 76314 : if (!ctx->quiet && !*non_constant_p)
2892 : 0 : error_at (loc, "expression %qE does not designate a %<constexpr%> "
2893 : : "function", fun);
2894 : 76314 : *non_constant_p = true;
2895 : 76314 : return t;
2896 : : }
2897 : 78959388 : if (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun))
2898 : 9306526 : fun = DECL_CLONED_FUNCTION (fun);
2899 : :
2900 : 78959388 : if (is_ubsan_builtin_p (fun))
2901 : 58 : return void_node;
2902 : :
2903 : 78959330 : if (fndecl_built_in_p (fun))
2904 : 9986664 : return cxx_eval_builtin_function_call (ctx, t, fun,
2905 : 9986664 : lval, non_constant_p, overflow_p);
2906 : 68972666 : if (DECL_THUNK_P (fun))
2907 : 26 : return cxx_eval_thunk_call (ctx, t, fun, lval, non_constant_p, overflow_p);
2908 : 68972640 : if (!maybe_constexpr_fn (fun))
2909 : : {
2910 : 97576 : if (TREE_CODE (t) == CALL_EXPR
2911 : 97511 : && cxx_replaceable_global_alloc_fn (fun)
2912 : 155744 : && (CALL_FROM_NEW_OR_DELETE_P (t)
2913 : 19155 : || is_std_allocator_allocate (ctx->call)))
2914 : : {
2915 : 40720 : const bool new_op_p = IDENTIFIER_NEW_OP_P (DECL_NAME (fun));
2916 : 40720 : const int nargs = call_expr_nargs (t);
2917 : 40720 : tree arg0 = NULL_TREE;
2918 : 64334 : for (int i = 0; i < nargs; ++i)
2919 : : {
2920 : 41198 : tree arg = CALL_EXPR_ARG (t, i);
2921 : 41198 : arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
2922 : : non_constant_p, overflow_p);
2923 : : /* Deleting a non-constant pointer has a better error message
2924 : : below. */
2925 : 41198 : if (new_op_p || i != 0)
2926 : 39809 : VERIFY_CONSTANT (arg);
2927 : 22225 : if (i == 0)
2928 : : arg0 = arg;
2929 : : }
2930 : 23136 : gcc_assert (arg0);
2931 : 23136 : if (new_op_p)
2932 : : {
2933 : : /* FIXME: We should not get here; the VERIFY_CONSTANT above
2934 : : should have already caught it. But currently a conversion
2935 : : from pointer type to arithmetic type is only considered
2936 : : non-constant for CONVERT_EXPRs, not NOP_EXPRs. */
2937 : 21747 : if (!tree_fits_uhwi_p (arg0))
2938 : : {
2939 : 24 : if (!ctx->quiet)
2940 : 6 : error_at (loc, "cannot allocate array: size not constant");
2941 : 24 : *non_constant_p = true;
2942 : 24 : return t;
2943 : : }
2944 : 43446 : tree type = build_array_type_nelts (char_type_node,
2945 : 21723 : tree_to_uhwi (arg0));
2946 : 21723 : tree var = build_decl (loc, VAR_DECL,
2947 : 21723 : (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
2948 : : & OVL_OP_FLAG_VEC)
2949 : : ? heap_vec_uninit_identifier
2950 : : : heap_uninit_identifier,
2951 : 21723 : type);
2952 : 21723 : DECL_ARTIFICIAL (var) = 1;
2953 : 21723 : TREE_STATIC (var) = 1;
2954 : : // Temporarily register the artificial var in varpool,
2955 : : // so that comparisons of its address against NULL are folded
2956 : : // through nonzero_address even with
2957 : : // -fno-delete-null-pointer-checks or that comparison of
2958 : : // addresses of different heap artificial vars is folded too.
2959 : : // See PR98988 and PR99031.
2960 : 21723 : varpool_node::finalize_decl (var);
2961 : 21723 : ctx->global->heap_vars.safe_push (var);
2962 : 21723 : ctx->global->put_value (var, NULL_TREE);
2963 : 21723 : return fold_convert (ptr_type_node, build_address (var));
2964 : : }
2965 : : else
2966 : : {
2967 : 1389 : STRIP_NOPS (arg0);
2968 : 1389 : if (TREE_CODE (arg0) == ADDR_EXPR
2969 : 1389 : && VAR_P (TREE_OPERAND (arg0, 0)))
2970 : : {
2971 : 1389 : tree var = TREE_OPERAND (arg0, 0);
2972 : 1389 : if (DECL_NAME (var) == heap_uninit_identifier
2973 : 1389 : || DECL_NAME (var) == heap_identifier)
2974 : : {
2975 : 1265 : if (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
2976 : : & OVL_OP_FLAG_VEC)
2977 : : {
2978 : 9 : if (!ctx->quiet)
2979 : : {
2980 : 3 : auto_diagnostic_group d;
2981 : 3 : error_at (loc, "array deallocation of object "
2982 : : "allocated with non-array "
2983 : : "allocation");
2984 : 3 : inform (DECL_SOURCE_LOCATION (var),
2985 : : "allocation performed here");
2986 : 3 : }
2987 : 9 : *non_constant_p = true;
2988 : 9 : return t;
2989 : : }
2990 : 1256 : DECL_NAME (var) = heap_deleted_identifier;
2991 : 1256 : ctx->global->destroy_value (var);
2992 : 1256 : ctx->global->heap_dealloc_count++;
2993 : 1256 : return void_node;
2994 : : }
2995 : 124 : else if (DECL_NAME (var) == heap_vec_uninit_identifier
2996 : 124 : || DECL_NAME (var) == heap_vec_identifier)
2997 : : {
2998 : 100 : if ((IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
2999 : : & OVL_OP_FLAG_VEC) == 0)
3000 : : {
3001 : 9 : if (!ctx->quiet)
3002 : : {
3003 : 3 : auto_diagnostic_group d;
3004 : 3 : error_at (loc, "non-array deallocation of "
3005 : : "object allocated with array "
3006 : : "allocation");
3007 : 3 : inform (DECL_SOURCE_LOCATION (var),
3008 : : "allocation performed here");
3009 : 3 : }
3010 : 9 : *non_constant_p = true;
3011 : 9 : return t;
3012 : : }
3013 : 91 : DECL_NAME (var) = heap_deleted_identifier;
3014 : 91 : ctx->global->destroy_value (var);
3015 : 91 : ctx->global->heap_dealloc_count++;
3016 : 91 : return void_node;
3017 : : }
3018 : 24 : else if (DECL_NAME (var) == heap_deleted_identifier)
3019 : : {
3020 : 15 : if (!ctx->quiet)
3021 : 6 : error_at (loc, "deallocation of already deallocated "
3022 : : "storage");
3023 : 15 : *non_constant_p = true;
3024 : 15 : return t;
3025 : : }
3026 : : }
3027 : 9 : if (!ctx->quiet)
3028 : 3 : error_at (loc, "deallocation of storage that was "
3029 : : "not previously allocated");
3030 : 9 : *non_constant_p = true;
3031 : 9 : return t;
3032 : : }
3033 : : }
3034 : : /* Allow placement new in std::construct_at, just return the second
3035 : : argument. */
3036 : 56856 : if (TREE_CODE (t) == CALL_EXPR
3037 : 56791 : && cxx_placement_new_fn (fun)
3038 : 87335 : && is_std_construct_at (ctx->call))
3039 : : {
3040 : 15741 : const int nargs = call_expr_nargs (t);
3041 : 15741 : tree arg1 = NULL_TREE;
3042 : 47223 : for (int i = 0; i < nargs; ++i)
3043 : : {
3044 : 31482 : tree arg = CALL_EXPR_ARG (t, i);
3045 : 31482 : arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
3046 : : non_constant_p, overflow_p);
3047 : 31482 : if (i == 1)
3048 : : arg1 = arg;
3049 : : else
3050 : 31482 : VERIFY_CONSTANT (arg);
3051 : : }
3052 : 15741 : gcc_assert (arg1);
3053 : : return arg1;
3054 : : }
3055 : 41115 : else if (cxx_dynamic_cast_fn_p (fun))
3056 : 1756 : return cxx_eval_dynamic_cast_fn (ctx, t, non_constant_p, overflow_p);
3057 : :
3058 : 39359 : if (!ctx->quiet)
3059 : : {
3060 : 58 : if (!lambda_static_thunk_p (fun))
3061 : 58 : error_at (loc, "call to non-%<constexpr%> function %qD", fun);
3062 : 58 : explain_invalid_constexpr_fn (fun);
3063 : : }
3064 : 39359 : *non_constant_p = true;
3065 : 39359 : return t;
3066 : : }
3067 : :
3068 : 68875064 : constexpr_ctx new_ctx = *ctx;
3069 : 77960744 : if (DECL_CONSTRUCTOR_P (fun) && !ctx->object
3070 : 71023488 : && TREE_CODE (t) == AGGR_INIT_EXPR)
3071 : : {
3072 : : /* We want to have an initialization target for an AGGR_INIT_EXPR.
3073 : : If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
3074 : 172 : new_ctx.object = AGGR_INIT_EXPR_SLOT (t);
3075 : 172 : tree ctor = new_ctx.ctor = build_constructor (DECL_CONTEXT (fun), NULL);
3076 : 172 : CONSTRUCTOR_NO_CLEARING (ctor) = true;
3077 : 172 : ctx->global->put_value (new_ctx.object, ctor);
3078 : 172 : ctx = &new_ctx;
3079 : : }
3080 : : /* An immediate invocation is manifestly constant evaluated including the
3081 : : arguments of the call, so use mce_true even for the argument
3082 : : evaluation. */
3083 : 137750128 : if (DECL_IMMEDIATE_FUNCTION_P (fun))
3084 : : {
3085 : 375053 : new_ctx.manifestly_const_eval = mce_true;
3086 : 375053 : new_call.manifestly_const_eval = mce_true;
3087 : 375053 : ctx = &new_ctx;
3088 : : }
3089 : :
3090 : : /* We used to shortcut trivial constructor/op= here, but nowadays
3091 : : we can only get a trivial function here with -fno-elide-constructors. */
3092 : 68875064 : gcc_checking_assert (!trivial_fn_p (fun)
3093 : : || !flag_elide_constructors
3094 : : /* We don't elide constructors when processing
3095 : : a noexcept-expression. */
3096 : : || cp_noexcept_operand);
3097 : :
3098 : 68875064 : bool non_constant_args = false;
3099 : 68875064 : new_call.bindings
3100 : 68875064 : = cxx_bind_parameters_in_call (ctx, t, fun, non_constant_p,
3101 : : overflow_p, &non_constant_args);
3102 : :
3103 : : /* We build up the bindings list before we know whether we already have this
3104 : : call cached. If we don't end up saving these bindings, ggc_free them when
3105 : : this function exits. */
3106 : 68875064 : class free_bindings
3107 : : {
3108 : : tree *bindings;
3109 : : public:
3110 : 68875064 : free_bindings (tree &b): bindings (&b) { }
3111 : 68875064 : ~free_bindings () { if (bindings) ggc_free (*bindings); }
3112 : 3206681 : void preserve () { bindings = NULL; }
3113 : 68875064 : } fb (new_call.bindings);
3114 : :
3115 : 68875064 : if (*non_constant_p)
3116 : : return t;
3117 : :
3118 : : /* We can't defer instantiating the function any longer. */
3119 : 52637195 : if (!DECL_INITIAL (fun)
3120 : 4416853 : && (DECL_TEMPLOID_INSTANTIATION (fun) || DECL_DEFAULTED_FN (fun))
3121 : 52637195 : && !uid_sensitive_constexpr_evaluation_p ())
3122 : : {
3123 : 2684383 : location_t save_loc = input_location;
3124 : 2684383 : input_location = loc;
3125 : 2684383 : ++function_depth;
3126 : 2684383 : if (ctx->manifestly_const_eval == mce_true)
3127 : 66059 : FNDECL_MANIFESTLY_CONST_EVALUATED (fun) = true;
3128 : 2684383 : if (DECL_TEMPLOID_INSTANTIATION (fun))
3129 : 2684378 : instantiate_decl (fun, /*defer_ok*/false, /*expl_inst*/false);
3130 : : else
3131 : 5 : synthesize_method (fun);
3132 : 2684383 : --function_depth;
3133 : 2684383 : input_location = save_loc;
3134 : : }
3135 : :
3136 : : /* If in direct recursive call, optimize definition search. */
3137 : 52637195 : if (ctx && ctx->call && ctx->call->fundef && ctx->call->fundef->decl == fun)
3138 : 49371 : new_call.fundef = ctx->call->fundef;
3139 : : else
3140 : : {
3141 : 52587824 : new_call.fundef = retrieve_constexpr_fundef (fun);
3142 : 52587824 : if (new_call.fundef == NULL || new_call.fundef->body == NULL
3143 : 50476285 : || new_call.fundef->result == error_mark_node
3144 : 50377326 : || fun == current_function_decl)
3145 : : {
3146 : 2210501 : if (!ctx->quiet)
3147 : : {
3148 : : /* We need to check for current_function_decl here in case we're
3149 : : being called during cp_fold_function, because at that point
3150 : : DECL_INITIAL is set properly and we have a fundef but we
3151 : : haven't lowered invisirefs yet (c++/70344). */
3152 : 130 : if (DECL_INITIAL (fun) == error_mark_node
3153 : 130 : || fun == current_function_decl)
3154 : 15 : error_at (loc, "%qD called in a constant expression before its "
3155 : : "definition is complete", fun);
3156 : 115 : else if (DECL_INITIAL (fun))
3157 : : {
3158 : : /* The definition of fun was somehow unsuitable. But pretend
3159 : : that lambda static thunks don't exist. */
3160 : 78 : if (!lambda_static_thunk_p (fun))
3161 : 78 : error_at (loc, "%qD called in a constant expression", fun);
3162 : 78 : explain_invalid_constexpr_fn (fun);
3163 : : }
3164 : : else
3165 : 37 : error_at (loc, "%qD used before its definition", fun);
3166 : : }
3167 : 2210501 : *non_constant_p = true;
3168 : 2210501 : return t;
3169 : : }
3170 : : }
3171 : :
3172 : 50426694 : depth_ok = push_cx_call_context (t);
3173 : :
3174 : : /* Remember the object we are constructing or destructing. */
3175 : 50426694 : tree new_obj = NULL_TREE;
3176 : 100853388 : if (DECL_CONSTRUCTOR_P (fun) || DECL_DESTRUCTOR_P (fun))
3177 : : {
3178 : : /* In a cdtor, it should be the first `this' argument.
3179 : : At this point it has already been evaluated in the call
3180 : : to cxx_bind_parameters_in_call. */
3181 : 6934429 : new_obj = TREE_VEC_ELT (new_call.bindings, 0);
3182 : 6934429 : bool empty_base = false;
3183 : 6934429 : new_obj = cxx_fold_indirect_ref (ctx, loc, DECL_CONTEXT (fun), new_obj,
3184 : : &empty_base);
3185 : : /* If we're initializing an empty class, don't set constness, because
3186 : : cxx_fold_indirect_ref will return the wrong object to set constness
3187 : : of. */
3188 : 6934429 : if (empty_base)
3189 : : new_obj = NULL_TREE;
3190 : 2195252 : else if (ctx->call && ctx->call->fundef
3191 : 9826452 : && DECL_CONSTRUCTOR_P (ctx->call->fundef->decl))
3192 : : {
3193 : 1432066 : tree cur_obj = TREE_VEC_ELT (ctx->call->bindings, 0);
3194 : 1432066 : STRIP_NOPS (cur_obj);
3195 : 1432066 : if (TREE_CODE (cur_obj) == ADDR_EXPR)
3196 : 1327362 : cur_obj = TREE_OPERAND (cur_obj, 0);
3197 : 1432066 : if (new_obj == cur_obj)
3198 : : /* We're calling the target constructor of a delegating
3199 : : constructor, or accessing a base subobject through a
3200 : : NOP_EXPR as part of a call to a base constructor, so
3201 : : there is no new (sub)object. */
3202 : 1504302 : new_obj = NULL_TREE;
3203 : : }
3204 : : }
3205 : :
3206 : 50426694 : tree result = NULL_TREE;
3207 : :
3208 : 50426694 : constexpr_call *entry = NULL;
3209 : 50426694 : if (depth_ok && !non_constant_args && ctx->strict)
3210 : : {
3211 : 17533351 : new_call.hash = constexpr_fundef_hasher::hash (new_call.fundef);
3212 : 17533351 : new_call.hash
3213 : 17533351 : = iterative_hash_template_arg (new_call.bindings, new_call.hash);
3214 : 17533351 : new_call.hash
3215 : 17533351 : = iterative_hash_object (ctx->manifestly_const_eval, new_call.hash);
3216 : :
3217 : : /* If we have seen this call before, we are done. */
3218 : 17533351 : maybe_initialize_constexpr_call_table ();
3219 : 17533351 : bool insert = depth_ok < constexpr_cache_depth;
3220 : 17533351 : constexpr_call **slot
3221 : 17657290 : = constexpr_call_table->find_slot (&new_call,
3222 : : insert ? INSERT : NO_INSERT);
3223 : 17533351 : entry = slot ? *slot : NULL;
3224 : 17444875 : if (entry == NULL)
3225 : : {
3226 : : /* Only cache up to constexpr_cache_depth to limit memory use. */
3227 : 3295157 : if (insert)
3228 : : {
3229 : : /* We need to keep a pointer to the entry, not just the slot, as
3230 : : the slot can move during evaluation of the body. */
3231 : 3206681 : *slot = entry = ggc_alloc<constexpr_call> ();
3232 : 3206681 : *entry = new_call;
3233 : 3206681 : fb.preserve ();
3234 : : }
3235 : : }
3236 : : /* Calls that are in progress have their result set to NULL, so that we
3237 : : can detect circular dependencies. Now that we only cache up to
3238 : : constexpr_cache_depth this won't catch circular dependencies that
3239 : : start deeper, but they'll hit the recursion or ops limit. */
3240 : 14238194 : else if (entry->result == NULL)
3241 : : {
3242 : 12 : if (!ctx->quiet)
3243 : 0 : error ("call has circular dependency");
3244 : 12 : *non_constant_p = true;
3245 : 12 : entry->result = result = error_mark_node;
3246 : : }
3247 : : else
3248 : 14238182 : result = entry->result;
3249 : : }
3250 : :
3251 : 50426694 : if (!depth_ok)
3252 : : {
3253 : 24 : if (!ctx->quiet)
3254 : 3 : error ("%<constexpr%> evaluation depth exceeds maximum of %d (use "
3255 : : "%<-fconstexpr-depth=%> to increase the maximum)",
3256 : : max_constexpr_depth);
3257 : 24 : *non_constant_p = true;
3258 : 24 : result = error_mark_node;
3259 : : }
3260 : : else
3261 : : {
3262 : 50426670 : bool cacheable = !!entry;
3263 : 50426670 : if (result && result != error_mark_node)
3264 : : /* OK */;
3265 : 38836813 : else if (!DECL_SAVED_TREE (fun))
3266 : : {
3267 : : /* When at_eof >= 3, cgraph has started throwing away
3268 : : DECL_SAVED_TREE, so fail quietly. FIXME we get here because of
3269 : : late code generation for VEC_INIT_EXPR, which needs to be
3270 : : completely reconsidered. */
3271 : 0 : gcc_assert (at_eof >= 3 && ctx->quiet);
3272 : 0 : *non_constant_p = true;
3273 : : }
3274 : 38836813 : else if (tree copy = get_fundef_copy (new_call.fundef))
3275 : : {
3276 : 38836813 : tree body, parms, res;
3277 : 38836813 : releasing_vec ctors;
3278 : :
3279 : : /* Reuse or create a new unshared copy of this function's body. */
3280 : 38836813 : body = TREE_PURPOSE (copy);
3281 : 38836813 : parms = TREE_VALUE (copy);
3282 : 38836813 : res = TREE_TYPE (copy);
3283 : :
3284 : : /* Associate the bindings with the remapped parms. */
3285 : 38836813 : tree bound = new_call.bindings;
3286 : 38836813 : tree remapped = parms;
3287 : 83693387 : for (int i = 0; i < TREE_VEC_LENGTH (bound); ++i)
3288 : : {
3289 : 44856574 : tree arg = TREE_VEC_ELT (bound, i);
3290 : 44856574 : if (entry)
3291 : : {
3292 : : /* Unshare args going into the hash table to separate them
3293 : : from the caller's context, for better GC and to avoid
3294 : : problems with verify_gimple. */
3295 : 1748525 : arg = unshare_expr_without_location (arg);
3296 : 1748525 : TREE_VEC_ELT (bound, i) = arg;
3297 : :
3298 : : /* And then unshare again so the callee doesn't change the
3299 : : argument values in the hash table. XXX Could we unshare
3300 : : lazily in cxx_eval_store_expression? */
3301 : 1748525 : arg = unshare_constructor (arg);
3302 : 1748525 : if (TREE_CODE (arg) == CONSTRUCTOR)
3303 : 682431 : vec_safe_push (ctors, arg);
3304 : : }
3305 : 44856574 : ctx->global->put_value (remapped, arg);
3306 : 44856574 : remapped = DECL_CHAIN (remapped);
3307 : : }
3308 : 38836813 : if (remapped)
3309 : : {
3310 : : /* We shouldn't have any parms without args, but fail gracefully
3311 : : in error recovery. */
3312 : 6 : gcc_checking_assert (seen_error ());
3313 : 6 : *non_constant_p = true;
3314 : : }
3315 : : /* Add the RESULT_DECL to the values map, too. */
3316 : 38836813 : gcc_assert (!DECL_BY_REFERENCE (res));
3317 : 38836813 : ctx->global->put_value (res, NULL_TREE);
3318 : :
3319 : : /* Remember the current call we're evaluating. */
3320 : 38836813 : constexpr_ctx call_ctx = *ctx;
3321 : 38836813 : call_ctx.call = &new_call;
3322 : 38836813 : unsigned save_heap_alloc_count = ctx->global->heap_vars.length ();
3323 : 38836813 : unsigned save_heap_dealloc_count = ctx->global->heap_dealloc_count;
3324 : :
3325 : : /* Make sure we fold std::is_constant_evaluated to true in an
3326 : : immediate function. */
3327 : 77673626 : if (DECL_IMMEDIATE_FUNCTION_P (fun))
3328 : 240098 : call_ctx.manifestly_const_eval = mce_true;
3329 : :
3330 : : /* If this is a constexpr destructor, the object's const and volatile
3331 : : semantics are no longer in effect; see [class.dtor]p5. */
3332 : 43920767 : if (new_obj && DECL_DESTRUCTOR_P (fun))
3333 : 158689 : cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/false,
3334 : : non_constant_p, overflow_p);
3335 : :
3336 : : /* If this is a constructor, we are beginning the lifetime of the
3337 : : object we are initializing. */
3338 : 38836813 : if (new_obj
3339 : 10167908 : && DECL_CONSTRUCTOR_P (fun)
3340 : 4925265 : && TREE_CODE (new_obj) == COMPONENT_REF
3341 : 40910777 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (new_obj, 0))) == UNION_TYPE)
3342 : : {
3343 : 8564 : tree activate = build2 (INIT_EXPR, TREE_TYPE (new_obj),
3344 : : new_obj,
3345 : 8564 : build_constructor (TREE_TYPE (new_obj),
3346 : : NULL));
3347 : 8564 : cxx_eval_constant_expression (ctx, activate,
3348 : : lval, non_constant_p, overflow_p);
3349 : 8564 : ggc_free (activate);
3350 : : }
3351 : :
3352 : 38836813 : tree jump_target = NULL_TREE;
3353 : 38836813 : cxx_eval_constant_expression (&call_ctx, body,
3354 : : vc_discard, non_constant_p, overflow_p,
3355 : : &jump_target);
3356 : :
3357 : 77673626 : if (DECL_CONSTRUCTOR_P (fun))
3358 : : /* This can be null for a subobject constructor call, in
3359 : : which case what we care about is the initialization
3360 : : side-effects rather than the value. We could get at the
3361 : : value by evaluating *this, but we don't bother; there's
3362 : : no need to put such a call in the hash table. */
3363 : 6772494 : result = lval ? ctx->object : ctx->ctor;
3364 : 32064319 : else if (VOID_TYPE_P (TREE_TYPE (res)))
3365 : 1165322 : result = void_node;
3366 : : else
3367 : : {
3368 : 30898997 : result = ctx->global->get_value (res);
3369 : 12531603 : if (result == NULL_TREE && !*non_constant_p
3370 : 30899173 : && !DECL_DESTRUCTOR_P (fun))
3371 : : {
3372 : 88 : if (!ctx->quiet)
3373 : 11 : error ("%<constexpr%> call flows off the end "
3374 : : "of the function");
3375 : 88 : *non_constant_p = true;
3376 : : }
3377 : : }
3378 : :
3379 : : /* At this point, the object's constructor will have run, so
3380 : : the object is no longer under construction, and its possible
3381 : : 'const' semantics now apply. Make a note of this fact by
3382 : : marking the CONSTRUCTOR TREE_READONLY. */
3383 : 43920767 : if (new_obj && DECL_CONSTRUCTOR_P (fun))
3384 : 4925265 : cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/true,
3385 : : non_constant_p, overflow_p);
3386 : :
3387 : : /* Remove the parms/result from the values map. */
3388 : 38836813 : destroy_value_checked (ctx, res, non_constant_p);
3389 : 83693393 : for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
3390 : 44856580 : destroy_value_checked (ctx, parm, non_constant_p);
3391 : :
3392 : : /* Free any parameter CONSTRUCTORs we aren't returning directly. */
3393 : 39519244 : while (!ctors->is_empty ())
3394 : : {
3395 : 682431 : tree c = ctors->pop ();
3396 : 682431 : if (c != result)
3397 : 682431 : free_constructor (c);
3398 : : }
3399 : :
3400 : : /* Make the unshared function copy we used available for re-use. */
3401 : 38836813 : save_fundef_copy (fun, copy);
3402 : :
3403 : : /* If the call allocated some heap object that hasn't been
3404 : : deallocated during the call, or if it deallocated some heap
3405 : : object it has not allocated, the call isn't really stateless
3406 : : for the constexpr evaluation and should not be cached.
3407 : : It is fine if the call allocates something and deallocates it
3408 : : too. */
3409 : 38836813 : if (cacheable
3410 : 44691831 : && (save_heap_alloc_count != ctx->global->heap_vars.length ()
3411 : 5854530 : || (save_heap_dealloc_count
3412 : 5854530 : != ctx->global->heap_dealloc_count)))
3413 : : {
3414 : 488 : tree heap_var;
3415 : 488 : unsigned int i;
3416 : 488 : if ((ctx->global->heap_vars.length ()
3417 : 488 : - ctx->global->heap_dealloc_count)
3418 : 488 : != save_heap_alloc_count - save_heap_dealloc_count)
3419 : : cacheable = false;
3420 : : else
3421 : 1714 : FOR_EACH_VEC_ELT_FROM (ctx->global->heap_vars, i, heap_var,
3422 : : save_heap_alloc_count)
3423 : 1329 : if (DECL_NAME (heap_var) != heap_deleted_identifier)
3424 : : {
3425 : : cacheable = false;
3426 : : break;
3427 : : }
3428 : : /* And don't cache a ref to a deleted heap variable (119162). */
3429 : 385 : if (cacheable
3430 : 385 : && (cp_walk_tree_without_duplicates
3431 : : (&result, find_deleted_heap_var, NULL)))
3432 : : cacheable = false;
3433 : : }
3434 : :
3435 : : /* Rewrite all occurrences of the function's RESULT_DECL with the
3436 : : current object under construction. */
3437 : 38836813 : if (!*non_constant_p
3438 : 19495300 : && ctx->object
3439 : 5889667 : && CLASS_TYPE_P (TREE_TYPE (res))
3440 : 39781230 : && !is_empty_class (TREE_TYPE (res)))
3441 : : {
3442 : 342543 : if (!same_type_ignoring_top_level_qualifiers_p
3443 : 342543 : (TREE_TYPE (res), TREE_TYPE (ctx->object)))
3444 : 0 : *non_constant_p = true;
3445 : 342543 : else if (replace_decl (&result, res, ctx->object))
3446 : : {
3447 : 256 : cacheable = false;
3448 : 256 : result = cxx_eval_constant_expression (ctx, result, lval,
3449 : : non_constant_p,
3450 : : overflow_p);
3451 : : }
3452 : : }
3453 : :
3454 : : /* Only cache a permitted result of a constant expression. */
3455 : 38836813 : if (cacheable && !reduced_constant_expression_p (result))
3456 : : cacheable = false;
3457 : 38836813 : }
3458 : : else
3459 : : /* Couldn't get a function copy to evaluate. */
3460 : 0 : *non_constant_p = true;
3461 : :
3462 : 50426670 : if (result == error_mark_node)
3463 : 0 : *non_constant_p = true;
3464 : 50426670 : if (*non_constant_p || *overflow_p)
3465 : 19341669 : result = error_mark_node;
3466 : 31085001 : else if (!result)
3467 : 1579313 : result = void_node;
3468 : 50426670 : if (entry)
3469 : 17444875 : entry->result = cacheable ? result : error_mark_node;
3470 : : }
3471 : :
3472 : : /* The result of a constexpr function must be completely initialized.
3473 : :
3474 : : However, in C++20, a constexpr constructor doesn't necessarily have
3475 : : to initialize all the fields, so we don't clear CONSTRUCTOR_NO_CLEARING
3476 : : in order to detect reading an unitialized object in constexpr instead
3477 : : of value-initializing it. (reduced_constant_expression_p is expected to
3478 : : take care of clearing the flag.) */
3479 : 50426694 : if (TREE_CODE (result) == CONSTRUCTOR
3480 : 50426694 : && (cxx_dialect < cxx20
3481 : 5439638 : || !DECL_CONSTRUCTOR_P (fun)))
3482 : 3068438 : clear_no_implicit_zero (result);
3483 : :
3484 : 50426694 : pop_cx_call_context ();
3485 : 50426694 : return result;
3486 : 68875064 : }
3487 : :
3488 : : /* Return true if T is a valid constant initializer. If a CONSTRUCTOR
3489 : : initializes all the members, the CONSTRUCTOR_NO_CLEARING flag will be
3490 : : cleared.
3491 : : FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
3492 : :
3493 : : bool
3494 : 556460616 : reduced_constant_expression_p (tree t)
3495 : : {
3496 : 556460616 : if (t == NULL_TREE)
3497 : : return false;
3498 : :
3499 : 553781610 : switch (TREE_CODE (t))
3500 : : {
3501 : : case PTRMEM_CST:
3502 : : /* Even if we can't lower this yet, it's constant. */
3503 : : return true;
3504 : :
3505 : 39647426 : case CONSTRUCTOR:
3506 : : /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
3507 : 39647426 : tree field;
3508 : 39647426 : if (!AGGREGATE_TYPE_P (TREE_TYPE (t)))
3509 : : /* A constant vector would be folded to VECTOR_CST.
3510 : : A CONSTRUCTOR of scalar type means uninitialized. */
3511 : : return false;
3512 : 39632358 : if (CONSTRUCTOR_NO_CLEARING (t))
3513 : : {
3514 : 1016577 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
3515 : : {
3516 : : /* There must be a valid constant initializer at every array
3517 : : index. */
3518 : 247 : tree min = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (t)));
3519 : 247 : tree max = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (t)));
3520 : 247 : tree cursor = min;
3521 : 16566 : for (auto &e: CONSTRUCTOR_ELTS (t))
3522 : : {
3523 : 15935 : if (!reduced_constant_expression_p (e.value))
3524 : : return false;
3525 : 15885 : if (array_index_cmp (cursor, e.index) != 0)
3526 : : return false;
3527 : 15825 : if (TREE_CODE (e.index) == RANGE_EXPR)
3528 : 0 : cursor = TREE_OPERAND (e.index, 1);
3529 : 15825 : if (TREE_CODE (e.value) == RAW_DATA_CST)
3530 : 0 : cursor
3531 : 0 : = int_const_binop (PLUS_EXPR, cursor,
3532 : 0 : size_int (RAW_DATA_LENGTH (e.value)));
3533 : : else
3534 : 15825 : cursor = int_const_binop (PLUS_EXPR, cursor,
3535 : 15825 : size_one_node);
3536 : : }
3537 : 137 : if (find_array_ctor_elt (t, max) == -1)
3538 : : return false;
3539 : 51 : goto ok;
3540 : : }
3541 : 1016330 : else if (cxx_dialect >= cxx20
3542 : 1016330 : && TREE_CODE (TREE_TYPE (t)) == UNION_TYPE)
3543 : : {
3544 : 3860261 : if (CONSTRUCTOR_NELTS (t) == 0)
3545 : : /* An initialized union has a constructor element. */
3546 : : return false;
3547 : : /* And it only initializes one member. */
3548 : : field = NULL_TREE;
3549 : : }
3550 : : else
3551 : 1016315 : field = next_subobject_field (TYPE_FIELDS (TREE_TYPE (t)));
3552 : : }
3553 : : else
3554 : : field = NULL_TREE;
3555 : 217610089 : for (auto &e: CONSTRUCTOR_ELTS (t))
3556 : : {
3557 : : /* If VAL is null, we're in the middle of initializing this
3558 : : element. */
3559 : 127883023 : if (!reduced_constant_expression_p (e.value))
3560 : : return false;
3561 : : /* We want to remove initializers for empty fields in a struct to
3562 : : avoid confusing output_constructor. */
3563 : 127428406 : if (is_empty_field (e.index)
3564 : 127428406 : && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE)
3565 : : return false;
3566 : : /* Check for non-empty fields between initialized fields when
3567 : : CONSTRUCTOR_NO_CLEARING. */
3568 : 126803237 : for (; field && e.index != field;
3569 : 84026 : field = next_subobject_field (DECL_CHAIN (field)))
3570 : 85456 : if (!is_really_empty_class (TREE_TYPE (field),
3571 : : /*ignore_vptr*/false))
3572 : : return false;
3573 : 126717781 : if (field)
3574 : 941540 : field = next_subobject_field (DECL_CHAIN (field));
3575 : : }
3576 : : /* There could be a non-empty field at the end. */
3577 : 38572048 : for (; field; field = next_subobject_field (DECL_CHAIN (field)))
3578 : 105928 : if (!is_really_empty_class (TREE_TYPE (field), /*ignore_vptr*/false))
3579 : : return false;
3580 : 38466120 : ok:
3581 : 38466171 : if (CONSTRUCTOR_NO_CLEARING (t))
3582 : : /* All the fields are initialized. */
3583 : 925691 : CONSTRUCTOR_NO_CLEARING (t) = false;
3584 : : return true;
3585 : :
3586 : 514100440 : default:
3587 : : /* FIXME are we calling this too much? */
3588 : 514100440 : return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE;
3589 : : }
3590 : : }
3591 : :
3592 : : /* *TP was not deemed constant by reduced_constant_expression_p. Explain
3593 : : why and suggest what could be done about it. */
3594 : :
3595 : : static tree
3596 : 886 : verify_constant_explain_r (tree *tp, int *walk_subtrees, void *)
3597 : : {
3598 : 886 : bool ref_p = false;
3599 : :
3600 : : /* No need to look into types or unevaluated operands. */
3601 : 886 : if (TYPE_P (*tp) || unevaluated_p (TREE_CODE (*tp)))
3602 : : {
3603 : 0 : *walk_subtrees = false;
3604 : 0 : return NULL_TREE;
3605 : : }
3606 : :
3607 : 886 : switch (TREE_CODE (*tp))
3608 : : {
3609 : 46 : CASE_CONVERT:
3610 : 46 : if (TREE_CODE (TREE_OPERAND (*tp, 0)) != ADDR_EXPR)
3611 : : break;
3612 : 33 : ref_p = TYPE_REF_P (TREE_TYPE (*tp));
3613 : 33 : *tp = TREE_OPERAND (*tp, 0);
3614 : 165 : gcc_fallthrough ();
3615 : 165 : case ADDR_EXPR:
3616 : 165 : {
3617 : 165 : tree op = TREE_OPERAND (*tp, 0);
3618 : 165 : if (VAR_P (op)
3619 : 96 : && DECL_DECLARED_CONSTEXPR_P (op)
3620 : 33 : && !TREE_STATIC (op)
3621 : : /* ??? We should also say something about temporaries. */
3622 : 195 : && !DECL_ARTIFICIAL (op))
3623 : : {
3624 : 27 : if (ref_p)
3625 : 6 : inform (location_of (*tp), "reference to %qD is not a constant "
3626 : : "expression", op);
3627 : : else
3628 : 21 : inform (location_of (*tp), "pointer to %qD is not a constant "
3629 : : "expression", op);
3630 : 27 : const location_t op_loc = DECL_SOURCE_LOCATION (op);
3631 : 27 : rich_location richloc (line_table, op_loc);
3632 : 27 : richloc.add_fixit_insert_before (op_loc, "static ");
3633 : 27 : inform (&richloc,
3634 : : "address of non-static constexpr variable %qD may differ on "
3635 : : "each invocation of the enclosing function; add %<static%> "
3636 : : "to give it a constant address", op);
3637 : 27 : }
3638 : : break;
3639 : : }
3640 : : default:
3641 : : break;
3642 : : }
3643 : :
3644 : : return NULL_TREE;
3645 : : }
3646 : :
3647 : : /* Some expressions may have constant operands but are not constant
3648 : : themselves, such as 1/0. Call this function to check for that
3649 : : condition.
3650 : :
3651 : : We only call this in places that require an arithmetic constant, not in
3652 : : places where we might have a non-constant expression that can be a
3653 : : component of a constant expression, such as the address of a constexpr
3654 : : variable that might be dereferenced later. */
3655 : :
3656 : : static bool
3657 : 451319185 : verify_constant (tree t, bool allow_non_constant, bool *non_constant_p,
3658 : : bool *overflow_p)
3659 : : {
3660 : 365362376 : if (!*non_constant_p && !reduced_constant_expression_p (t)
3661 : 463237330 : && t != void_node)
3662 : : {
3663 : 11918118 : if (!allow_non_constant)
3664 : : {
3665 : 229 : auto_diagnostic_group d;
3666 : 324 : error_at (cp_expr_loc_or_input_loc (t),
3667 : : "%q+E is not a constant expression", t);
3668 : 229 : cp_walk_tree_without_duplicates (&t, verify_constant_explain_r,
3669 : : nullptr);
3670 : 229 : }
3671 : 11918118 : *non_constant_p = true;
3672 : : }
3673 : 451319185 : if (TREE_OVERFLOW_P (t))
3674 : : {
3675 : 696 : if (!allow_non_constant)
3676 : : {
3677 : 155 : permerror (input_location, "overflow in constant expression");
3678 : : /* If we're being permissive (and are in an enforcing
3679 : : context), ignore the overflow. */
3680 : 155 : if (flag_permissive)
3681 : 75 : return *non_constant_p;
3682 : : }
3683 : 621 : *overflow_p = true;
3684 : : }
3685 : 451319110 : return *non_constant_p;
3686 : : }
3687 : :
3688 : : /* Check whether the shift operation with code CODE and type TYPE on LHS
3689 : : and RHS is undefined. If it is, give an error with an explanation,
3690 : : and return true; return false otherwise. */
3691 : :
3692 : : static bool
3693 : 32194351 : cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx,
3694 : : enum tree_code code, tree type, tree lhs, tree rhs)
3695 : : {
3696 : 32194351 : if ((code != LSHIFT_EXPR && code != RSHIFT_EXPR)
3697 : 3085922 : || TREE_CODE (lhs) != INTEGER_CST
3698 : 3085922 : || TREE_CODE (rhs) != INTEGER_CST)
3699 : : return false;
3700 : :
3701 : 3085922 : tree lhstype = TREE_TYPE (lhs);
3702 : 3085922 : unsigned HOST_WIDE_INT uprec = TYPE_PRECISION (TREE_TYPE (lhs));
3703 : :
3704 : : /* [expr.shift] The behavior is undefined if the right operand
3705 : : is negative, or greater than or equal to the length in bits
3706 : : of the promoted left operand. */
3707 : 3085922 : if (tree_int_cst_sgn (rhs) == -1)
3708 : : {
3709 : 126 : if (!ctx->quiet)
3710 : 35 : permerror (loc, "right operand of shift expression %q+E is negative",
3711 : : build2_loc (loc, code, type, lhs, rhs));
3712 : 126 : return (!flag_permissive || ctx->quiet);
3713 : : }
3714 : 3085796 : if (compare_tree_int (rhs, uprec) >= 0)
3715 : : {
3716 : 270 : if (!ctx->quiet)
3717 : 34 : permerror (loc, "right operand of shift expression %q+E is greater "
3718 : : "than or equal to the precision %wu of the left operand",
3719 : : build2_loc (loc, code, type, lhs, rhs), uprec);
3720 : 270 : return (!flag_permissive || ctx->quiet);
3721 : : }
3722 : :
3723 : : /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
3724 : : if E1 has a signed type and non-negative value, and E1x2^E2 is
3725 : : representable in the corresponding unsigned type of the result type,
3726 : : then that value, converted to the result type, is the resulting value;
3727 : : otherwise, the behavior is undefined.
3728 : : For C++20:
3729 : : The value of E1 << E2 is the unique value congruent to E1 x 2^E2 modulo
3730 : : 2^N, where N is the range exponent of the type of the result. */
3731 : 3085526 : if (code == LSHIFT_EXPR
3732 : 2958728 : && !TYPE_OVERFLOW_WRAPS (lhstype)
3733 : 2213319 : && cxx_dialect >= cxx11
3734 : 5283003 : && cxx_dialect < cxx20)
3735 : : {
3736 : 1686132 : if (tree_int_cst_sgn (lhs) == -1)
3737 : : {
3738 : 128 : if (!ctx->quiet)
3739 : 23 : permerror (loc,
3740 : : "left operand of shift expression %q+E is negative",
3741 : : build2_loc (loc, code, type, lhs, rhs));
3742 : 128 : return (!flag_permissive || ctx->quiet);
3743 : : }
3744 : : /* For signed x << y the following:
3745 : : (unsigned) x >> ((prec (lhs) - 1) - y)
3746 : : if > 1, is undefined. The right-hand side of this formula
3747 : : is the highest bit of the LHS that can be set (starting from 0),
3748 : : so that the shift doesn't overflow. We then right-shift the LHS
3749 : : to see whether any other bit is set making the original shift
3750 : : undefined -- the result is not representable in the corresponding
3751 : : unsigned type. */
3752 : 1686004 : tree t = build_int_cst (unsigned_type_node, uprec - 1);
3753 : 1686004 : t = fold_build2 (MINUS_EXPR, unsigned_type_node, t, rhs);
3754 : 1686004 : tree ulhs = fold_convert (unsigned_type_for (lhstype), lhs);
3755 : 1686004 : t = fold_build2 (RSHIFT_EXPR, TREE_TYPE (ulhs), ulhs, t);
3756 : 1686004 : if (tree_int_cst_lt (integer_one_node, t))
3757 : : {
3758 : 86 : if (!ctx->quiet)
3759 : 8 : permerror (loc, "shift expression %q+E overflows",
3760 : : build2_loc (loc, code, type, lhs, rhs));
3761 : 86 : return (!flag_permissive || ctx->quiet);
3762 : : }
3763 : : }
3764 : : return false;
3765 : : }
3766 : :
3767 : : /* Subroutine of cxx_eval_constant_expression.
3768 : : Attempt to reduce the unary expression tree T to a compile time value.
3769 : : If successful, return the value. Otherwise issue a diagnostic
3770 : : and return error_mark_node. */
3771 : :
3772 : : static tree
3773 : 21466588 : cxx_eval_unary_expression (const constexpr_ctx *ctx, tree t,
3774 : : bool /*lval*/,
3775 : : bool *non_constant_p, bool *overflow_p)
3776 : : {
3777 : 21466588 : tree r;
3778 : 21466588 : tree orig_arg = TREE_OPERAND (t, 0);
3779 : 21466588 : tree arg = cxx_eval_constant_expression (ctx, orig_arg, vc_prvalue,
3780 : : non_constant_p, overflow_p);
3781 : 21466588 : VERIFY_CONSTANT (arg);
3782 : 18263704 : location_t loc = EXPR_LOCATION (t);
3783 : 18263704 : enum tree_code code = TREE_CODE (t);
3784 : 18263704 : tree type = TREE_TYPE (t);
3785 : 18263704 : r = fold_unary_loc (loc, code, type, arg);
3786 : 18263704 : if (r == NULL_TREE)
3787 : : {
3788 : 15 : if (arg == orig_arg)
3789 : : r = t;
3790 : : else
3791 : 15 : r = build1_loc (loc, code, type, arg);
3792 : : }
3793 : 18263704 : VERIFY_CONSTANT (r);
3794 : : return r;
3795 : : }
3796 : :
3797 : : /* Helper function for cxx_eval_binary_expression. Try to optimize
3798 : : original POINTER_PLUS_EXPR T, LHS p+ RHS, return NULL_TREE if the
3799 : : generic folding should be used. */
3800 : :
3801 : : static tree
3802 : 2016924 : cxx_fold_pointer_plus_expression (const constexpr_ctx *ctx, tree t,
3803 : : tree lhs, tree rhs, bool *non_constant_p,
3804 : : bool *overflow_p)
3805 : : {
3806 : 2016924 : STRIP_NOPS (lhs);
3807 : 2016924 : if (TREE_CODE (lhs) != ADDR_EXPR)
3808 : : return NULL_TREE;
3809 : :
3810 : 1931214 : lhs = TREE_OPERAND (lhs, 0);
3811 : :
3812 : : /* &A[i] p+ j => &A[i + j] */
3813 : 1931214 : if (TREE_CODE (lhs) == ARRAY_REF
3814 : 7617 : && TREE_CODE (TREE_OPERAND (lhs, 1)) == INTEGER_CST
3815 : 7617 : && TREE_CODE (rhs) == INTEGER_CST
3816 : 7617 : && TYPE_SIZE_UNIT (TREE_TYPE (lhs))
3817 : 1938831 : && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs))) == INTEGER_CST)
3818 : : {
3819 : 7617 : tree orig_type = TREE_TYPE (t);
3820 : 7617 : location_t loc = EXPR_LOCATION (t);
3821 : 7617 : tree type = TREE_TYPE (lhs);
3822 : :
3823 : 7617 : t = fold_convert_loc (loc, ssizetype, TREE_OPERAND (lhs, 1));
3824 : 7617 : tree nelts = array_type_nelts_top (TREE_TYPE (TREE_OPERAND (lhs, 0)));
3825 : 7617 : nelts = cxx_eval_constant_expression (ctx, nelts, vc_prvalue,
3826 : : non_constant_p, overflow_p);
3827 : 7617 : if (*non_constant_p)
3828 : : return NULL_TREE;
3829 : : /* Don't fold an out-of-bound access. */
3830 : 7605 : if (!tree_int_cst_le (t, nelts))
3831 : : return NULL_TREE;
3832 : 7605 : rhs = cp_fold_convert (ssizetype, rhs);
3833 : : /* Don't fold if rhs can't be divided exactly by TYPE_SIZE_UNIT.
3834 : : constexpr int A[1]; ... (char *)&A[0] + 1 */
3835 : 7605 : if (!integer_zerop (fold_build2_loc (loc, TRUNC_MOD_EXPR, sizetype,
3836 : 7605 : rhs, TYPE_SIZE_UNIT (type))))
3837 : : return NULL_TREE;
3838 : : /* Make sure to treat the second operand of POINTER_PLUS_EXPR
3839 : : as signed. */
3840 : 7573 : rhs = fold_build2_loc (loc, EXACT_DIV_EXPR, ssizetype, rhs,
3841 : 7573 : TYPE_SIZE_UNIT (type));
3842 : 7573 : t = size_binop_loc (loc, PLUS_EXPR, rhs, t);
3843 : 7573 : t = build4_loc (loc, ARRAY_REF, type, TREE_OPERAND (lhs, 0),
3844 : : t, NULL_TREE, NULL_TREE);
3845 : 7573 : t = cp_build_addr_expr (t, tf_warning_or_error);
3846 : 7573 : t = cp_fold_convert (orig_type, t);
3847 : 7573 : return cxx_eval_constant_expression (ctx, t, vc_prvalue,
3848 : 7573 : non_constant_p, overflow_p);
3849 : : }
3850 : :
3851 : : return NULL_TREE;
3852 : : }
3853 : :
3854 : : /* Try to fold expressions like
3855 : : (struct S *) (&a[0].D.2378 + 12)
3856 : : into
3857 : : &MEM <struct T> [(void *)&a + 12B]
3858 : : This is something normally done by gimple_fold_stmt_to_constant_1
3859 : : on GIMPLE, but is undesirable on GENERIC if we are e.g. going to
3860 : : dereference the address because some details are lost.
3861 : : For pointer comparisons we want such folding though so that
3862 : : match.pd address_compare optimization works. */
3863 : :
3864 : : static tree
3865 : 1779528 : cxx_maybe_fold_addr_pointer_plus (tree t)
3866 : : {
3867 : 3566157 : while (CONVERT_EXPR_P (t)
3868 : 2051832 : && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0))))
3869 : 265203 : t = TREE_OPERAND (t, 0);
3870 : 1779528 : if (TREE_CODE (t) != POINTER_PLUS_EXPR)
3871 : : return NULL_TREE;
3872 : 1427618 : tree op0 = TREE_OPERAND (t, 0);
3873 : 1427618 : tree op1 = TREE_OPERAND (t, 1);
3874 : 1427618 : if (TREE_CODE (op1) != INTEGER_CST)
3875 : : return NULL_TREE;
3876 : 1427618 : while (CONVERT_EXPR_P (op0)
3877 : 2854198 : && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
3878 : 1426580 : op0 = TREE_OPERAND (op0, 0);
3879 : 1427618 : if (TREE_CODE (op0) != ADDR_EXPR)
3880 : : return NULL_TREE;
3881 : 1426841 : op1 = fold_convert (ptr_type_node, op1);
3882 : 1426841 : tree r = fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (op0)), op0, op1);
3883 : 1426841 : return build1_loc (EXPR_LOCATION (t), ADDR_EXPR, TREE_TYPE (op0), r);
3884 : : }
3885 : :
3886 : : /* Subroutine of cxx_eval_constant_expression.
3887 : : Like cxx_eval_unary_expression, except for binary expressions. */
3888 : :
3889 : : static tree
3890 : 47650537 : cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t,
3891 : : value_cat lval,
3892 : : bool *non_constant_p, bool *overflow_p)
3893 : : {
3894 : 47650537 : tree r = NULL_TREE;
3895 : 47650537 : tree orig_lhs = TREE_OPERAND (t, 0);
3896 : 47650537 : tree orig_rhs = TREE_OPERAND (t, 1);
3897 : 47650537 : tree lhs, rhs;
3898 : 47650537 : lhs = cxx_eval_constant_expression (ctx, orig_lhs, vc_prvalue,
3899 : : non_constant_p, overflow_p);
3900 : : /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer
3901 : : subtraction. */
3902 : 47650537 : if (*non_constant_p)
3903 : : return t;
3904 : 35103045 : rhs = cxx_eval_constant_expression (ctx, orig_rhs, vc_prvalue,
3905 : : non_constant_p, overflow_p);
3906 : 35103045 : if (*non_constant_p)
3907 : : return t;
3908 : :
3909 : 34089425 : location_t loc = EXPR_LOCATION (t);
3910 : 34089425 : enum tree_code code = TREE_CODE (t);
3911 : 34089425 : tree type = TREE_TYPE (t);
3912 : :
3913 : 34089425 : if (code == EQ_EXPR || code == NE_EXPR)
3914 : : {
3915 : 3551721 : bool is_code_eq = (code == EQ_EXPR);
3916 : :
3917 : 3551721 : if (TREE_CODE (lhs) == PTRMEM_CST
3918 : 187 : && TREE_CODE (rhs) == PTRMEM_CST)
3919 : : {
3920 : 54 : tree lmem = PTRMEM_CST_MEMBER (lhs);
3921 : 54 : tree rmem = PTRMEM_CST_MEMBER (rhs);
3922 : 54 : bool eq;
3923 : 54 : if (TREE_CODE (lmem) == TREE_CODE (rmem)
3924 : 54 : && TREE_CODE (lmem) == FIELD_DECL
3925 : 54 : && TREE_CODE (DECL_CONTEXT (lmem)) == UNION_TYPE
3926 : 81 : && same_type_p (DECL_CONTEXT (lmem),
3927 : : DECL_CONTEXT (rmem)))
3928 : : /* If both refer to (possibly different) members of the same union
3929 : : (12.3), they compare equal. */
3930 : : eq = true;
3931 : : else
3932 : 27 : eq = cp_tree_equal (lhs, rhs);
3933 : 54 : r = constant_boolean_node (eq == is_code_eq, type);
3934 : 54 : }
3935 : 3551667 : else if ((TREE_CODE (lhs) == PTRMEM_CST
3936 : 3551534 : || TREE_CODE (rhs) == PTRMEM_CST)
3937 : 3551667 : && (null_member_pointer_value_p (lhs)
3938 : 133 : || null_member_pointer_value_p (rhs)))
3939 : 133 : r = constant_boolean_node (!is_code_eq, type);
3940 : 3551534 : else if (TREE_CODE (lhs) == PTRMEM_CST)
3941 : 0 : lhs = cplus_expand_constant (lhs);
3942 : 3551534 : else if (TREE_CODE (rhs) == PTRMEM_CST)
3943 : 0 : rhs = cplus_expand_constant (rhs);
3944 : : }
3945 : 0 : if (r == NULL_TREE
3946 : 34089238 : && TREE_CODE_CLASS (code) == tcc_comparison
3947 : 11581141 : && POINTER_TYPE_P (TREE_TYPE (lhs)))
3948 : : {
3949 : 889764 : if (tree lhso = cxx_maybe_fold_addr_pointer_plus (lhs))
3950 : 680162 : lhs = fold_convert (TREE_TYPE (lhs), lhso);
3951 : 889764 : if (tree rhso = cxx_maybe_fold_addr_pointer_plus (rhs))
3952 : 746679 : rhs = fold_convert (TREE_TYPE (rhs), rhso);
3953 : : }
3954 : 2017079 : if (code == POINTER_PLUS_EXPR && !*non_constant_p
3955 : 36106504 : && integer_zerop (lhs) && !integer_zerop (rhs))
3956 : : {
3957 : 155 : if (!ctx->quiet)
3958 : 45 : error ("arithmetic involving a null pointer in %qE", lhs);
3959 : 155 : *non_constant_p = true;
3960 : 155 : return t;
3961 : : }
3962 : 34089270 : else if (code == POINTER_PLUS_EXPR)
3963 : 2016924 : r = cxx_fold_pointer_plus_expression (ctx, t, lhs, rhs, non_constant_p,
3964 : : overflow_p);
3965 : 32072346 : else if (code == SPACESHIP_EXPR)
3966 : : {
3967 : 16701 : r = genericize_spaceship (loc, type, lhs, rhs);
3968 : 16701 : return cxx_eval_constant_expression (ctx, r, lval, non_constant_p,
3969 : 16701 : overflow_p);
3970 : : }
3971 : :
3972 : 34072569 : if (r == NULL_TREE)
3973 : : {
3974 : 34064809 : if (ctx->manifestly_const_eval == mce_true
3975 : 16867533 : && (flag_constexpr_fp_except
3976 : 16867530 : || TREE_CODE (type) != REAL_TYPE))
3977 : : {
3978 : 16855972 : auto ofcc = make_temp_override (folding_cxx_constexpr, true);
3979 : 16855972 : r = fold_binary_initializer_loc (loc, code, type, lhs, rhs);
3980 : 16855972 : }
3981 : : else
3982 : 17208837 : r = fold_binary_loc (loc, code, type, lhs, rhs);
3983 : : }
3984 : :
3985 : 34064809 : if (r == NULL_TREE
3986 : 1878316 : && (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
3987 : 110 : && TREE_CODE (lhs) == INTEGER_CST
3988 : 98 : && TREE_CODE (rhs) == INTEGER_CST
3989 : 35950885 : && wi::neg_p (wi::to_wide (rhs)))
3990 : : {
3991 : : /* For diagnostics and -fpermissive emulate previous behavior of
3992 : : handling shifts by negative amount. */
3993 : 98 : tree nrhs = const_unop (NEGATE_EXPR, TREE_TYPE (rhs), rhs);
3994 : 98 : if (nrhs)
3995 : 119 : r = fold_binary_loc (loc,
3996 : : code == LSHIFT_EXPR ? RSHIFT_EXPR : LSHIFT_EXPR,
3997 : : type, lhs, nrhs);
3998 : : }
3999 : :
4000 : 34072569 : if (r == NULL_TREE)
4001 : : {
4002 : 1878218 : if (lhs == orig_lhs && rhs == orig_rhs)
4003 : : r = t;
4004 : : else
4005 : 328023 : r = build2_loc (loc, code, type, lhs, rhs);
4006 : : }
4007 : 32194351 : else if (cxx_eval_check_shift_p (loc, ctx, code, type, lhs, rhs))
4008 : 600 : *non_constant_p = true;
4009 : : /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
4010 : : a local array in a constexpr function. */
4011 : 34072569 : bool ptr = INDIRECT_TYPE_P (TREE_TYPE (lhs));
4012 : 31123677 : if (!ptr)
4013 : 31123677 : VERIFY_CONSTANT (r);
4014 : : return r;
4015 : : }
4016 : :
4017 : : /* Subroutine of cxx_eval_constant_expression.
4018 : : Attempt to evaluate condition expressions. */
4019 : :
4020 : : static tree
4021 : 7352876 : cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t,
4022 : : value_cat lval,
4023 : : bool *non_constant_p, bool *overflow_p,
4024 : : tree *jump_target)
4025 : : {
4026 : 7352876 : tree val = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
4027 : : vc_prvalue,
4028 : : non_constant_p, overflow_p);
4029 : 7352876 : VERIFY_CONSTANT (val);
4030 : 5628664 : if (TREE_CODE (t) == IF_STMT && IF_STMT_CONSTEVAL_P (t))
4031 : : {
4032 : : /* Evaluate the condition as if it was
4033 : : if (__builtin_is_constant_evaluated ()), i.e. defer it if not
4034 : : ctx->manifestly_const_eval (as sometimes we try to constant evaluate
4035 : : without manifestly_const_eval even expressions or parts thereof which
4036 : : will later be manifestly const_eval evaluated), otherwise fold it to
4037 : : true. */
4038 : 776489 : if (ctx->manifestly_const_eval == mce_unknown)
4039 : : {
4040 : 771042 : *non_constant_p = true;
4041 : 771042 : return t;
4042 : : }
4043 : 5447 : val = constant_boolean_node (ctx->manifestly_const_eval == mce_true,
4044 : : boolean_type_node);
4045 : : }
4046 : : /* Don't VERIFY_CONSTANT the other operands. */
4047 : 4857622 : const bool zero_p = integer_zerop (val);
4048 : 4857622 : if (zero_p)
4049 : 2689917 : val = TREE_OPERAND (t, 2);
4050 : : else
4051 : 2167705 : val = TREE_OPERAND (t, 1);
4052 : 4857622 : if (TREE_CODE (t) == IF_STMT && !val)
4053 : 928734 : val = void_node;
4054 : :
4055 : : /* P2564: If we aren't in immediate function context (including a manifestly
4056 : : constant-evaluated expression), check any uses of immediate functions in
4057 : : the arm we're discarding. But don't do this inside a call; we already
4058 : : checked when parsing the function. */
4059 : 4857622 : if (ctx->manifestly_const_eval != mce_true
4060 : 2351526 : && !in_immediate_context ()
4061 : 2351501 : && !ctx->call
4062 : 5494208 : && cp_fold_immediate (&TREE_OPERAND (t, zero_p ? 1 : 2),
4063 : 636586 : ctx->manifestly_const_eval))
4064 : : {
4065 : 7 : *non_constant_p = true;
4066 : 7 : return t;
4067 : : }
4068 : :
4069 : : /* A TARGET_EXPR may be nested inside another TARGET_EXPR, but still
4070 : : serve as the initializer for the same object as the outer TARGET_EXPR,
4071 : : as in
4072 : : A a = true ? A{} : A{};
4073 : : so strip the inner TARGET_EXPR so we don't materialize a temporary. */
4074 : 4857615 : if (TREE_CODE (val) == TARGET_EXPR)
4075 : 665 : val = TARGET_EXPR_INITIAL (val);
4076 : 4857615 : return cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
4077 : 4857615 : overflow_p, jump_target);
4078 : : }
4079 : :
4080 : : /* Subroutine of cxx_eval_constant_expression.
4081 : : Attempt to evaluate vector condition expressions. Unlike
4082 : : cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal
4083 : : ternary arithmetics operation, where all 3 arguments have to be
4084 : : evaluated as constants and then folding computes the result from
4085 : : them. */
4086 : :
4087 : : static tree
4088 : 832 : cxx_eval_vector_conditional_expression (const constexpr_ctx *ctx, tree t,
4089 : : bool *non_constant_p, bool *overflow_p)
4090 : : {
4091 : 832 : tree arg1 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
4092 : : vc_prvalue,
4093 : : non_constant_p, overflow_p);
4094 : 832 : VERIFY_CONSTANT (arg1);
4095 : 655 : tree arg2 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
4096 : : vc_prvalue,
4097 : : non_constant_p, overflow_p);
4098 : 655 : VERIFY_CONSTANT (arg2);
4099 : 655 : tree arg3 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2),
4100 : : vc_prvalue,
4101 : : non_constant_p, overflow_p);
4102 : 655 : VERIFY_CONSTANT (arg3);
4103 : 655 : location_t loc = EXPR_LOCATION (t);
4104 : 655 : tree type = TREE_TYPE (t);
4105 : 655 : tree r = fold_ternary_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
4106 : 655 : if (r == NULL_TREE)
4107 : : {
4108 : 0 : if (arg1 == TREE_OPERAND (t, 0)
4109 : 0 : && arg2 == TREE_OPERAND (t, 1)
4110 : 0 : && arg3 == TREE_OPERAND (t, 2))
4111 : : r = t;
4112 : : else
4113 : 0 : r = build3_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
4114 : : }
4115 : 655 : VERIFY_CONSTANT (r);
4116 : : return r;
4117 : : }
4118 : :
4119 : : /* Returns less than, equal to, or greater than zero if KEY is found to be
4120 : : less than, to match, or to be greater than the constructor_elt's INDEX. */
4121 : :
4122 : : static int
4123 : 64267 : array_index_cmp (tree key, tree index)
4124 : : {
4125 : 64267 : gcc_assert (TREE_CODE (key) == INTEGER_CST);
4126 : :
4127 : 64267 : switch (TREE_CODE (index))
4128 : : {
4129 : 60945 : case INTEGER_CST:
4130 : 60945 : return tree_int_cst_compare (key, index);
4131 : 3322 : case RANGE_EXPR:
4132 : 3322 : {
4133 : 3322 : tree lo = TREE_OPERAND (index, 0);
4134 : 3322 : tree hi = TREE_OPERAND (index, 1);
4135 : 3322 : if (tree_int_cst_lt (key, lo))
4136 : : return -1;
4137 : 2983 : else if (tree_int_cst_lt (hi, key))
4138 : : return 1;
4139 : : else
4140 : 2983 : return 0;
4141 : : }
4142 : 0 : default:
4143 : 0 : gcc_unreachable ();
4144 : : }
4145 : : }
4146 : :
4147 : : /* Extract a single INTEGER_CST from RAW_DATA_CST RAW_DATA at
4148 : : relative index OFF. */
4149 : :
4150 : : static tree
4151 : 29084 : raw_data_cst_elt (tree raw_data, unsigned int off)
4152 : : {
4153 : 29084 : return build_int_cst (TREE_TYPE (raw_data),
4154 : 29084 : TYPE_UNSIGNED (TREE_TYPE (raw_data))
4155 : 58168 : ? (HOST_WIDE_INT)
4156 : 29084 : RAW_DATA_UCHAR_ELT (raw_data, off)
4157 : : : (HOST_WIDE_INT)
4158 : 0 : RAW_DATA_SCHAR_ELT (raw_data, off));
4159 : : }
4160 : :
4161 : : /* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1
4162 : : if none. If INSERT is true, insert a matching element rather than fail. */
4163 : :
4164 : : static HOST_WIDE_INT
4165 : 1396305 : find_array_ctor_elt (tree ary, tree dindex, bool insert)
4166 : : {
4167 : 1396305 : if (tree_int_cst_sgn (dindex) < 0)
4168 : : return -1;
4169 : :
4170 : 1396305 : unsigned HOST_WIDE_INT i = tree_to_uhwi (dindex);
4171 : 1396305 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ary);
4172 : 1396305 : unsigned HOST_WIDE_INT len = vec_safe_length (elts);
4173 : :
4174 : 2148727 : unsigned HOST_WIDE_INT end = len;
4175 : 2148727 : unsigned HOST_WIDE_INT begin = 0;
4176 : :
4177 : : /* If the last element of the CONSTRUCTOR has its own index, we can assume
4178 : : that the same is true of the other elements and index directly. */
4179 : 1307273 : if (end > 0)
4180 : : {
4181 : 1286993 : tree cindex = (*elts)[end - 1].index;
4182 : 1286993 : if (cindex == NULL_TREE)
4183 : : {
4184 : : /* Verify that if the last index is missing, all indexes
4185 : : are missing and there is no RAW_DATA_CST. */
4186 : 0 : if (flag_checking)
4187 : 0 : for (unsigned int j = 0; j < len - 1; ++j)
4188 : 0 : gcc_assert ((*elts)[j].index == NULL_TREE
4189 : : && TREE_CODE ((*elts)[j].value) != RAW_DATA_CST);
4190 : 0 : if (i < end)
4191 : 0 : return i;
4192 : : else
4193 : : {
4194 : 0 : begin = end;
4195 : 0 : if (i == end)
4196 : : /* If the element is to be added right at the end,
4197 : : make sure it is added with cleared index too. */
4198 : 841454 : dindex = NULL_TREE;
4199 : 0 : else if (insert)
4200 : : /* Otherwise, in order not to break the assumption
4201 : : that CONSTRUCTOR either has all indexes or none,
4202 : : we need to add indexes to all elements. */
4203 : 0 : for (unsigned int j = 0; j < len; ++j)
4204 : 0 : (*elts)[j].index = build_int_cst (TREE_TYPE (dindex), j);
4205 : : }
4206 : : }
4207 : 1286993 : else if (TREE_CODE (cindex) == INTEGER_CST
4208 : 1286993 : && compare_tree_int (cindex, end - 1) == 0)
4209 : : {
4210 : 1269832 : tree value = (*elts)[end - 1].value;
4211 : 1269832 : if (i < end)
4212 : : {
4213 : 554941 : if (i == end - 1 && TREE_CODE (value) == RAW_DATA_CST)
4214 : : begin = end - 1;
4215 : : else
4216 : 554851 : return i;
4217 : : }
4218 : 714891 : else if (TREE_CODE (value) == RAW_DATA_CST
4219 : 747069 : && wi::to_offset (dindex) < (wi::to_offset (cindex)
4220 : 32178 : + RAW_DATA_LENGTH (value)))
4221 : 16089 : begin = end - 1;
4222 : : else
4223 : 698802 : begin = end;
4224 : : }
4225 : : }
4226 : :
4227 : : /* Otherwise, find a matching index by means of a binary search. */
4228 : 857209 : while (begin != end)
4229 : : {
4230 : 48379 : unsigned HOST_WIDE_INT middle = (begin + end) / 2;
4231 : 48379 : constructor_elt &elt = (*elts)[middle];
4232 : 48379 : tree idx = elt.index;
4233 : :
4234 : 48379 : int cmp = array_index_cmp (dindex, idx);
4235 : 48379 : if (cmp > 0
4236 : 42834 : && TREE_CODE (elt.value) == RAW_DATA_CST
4237 : 131253 : && wi::to_offset (dindex) < (wi::to_offset (idx)
4238 : 82874 : + RAW_DATA_LENGTH (elt.value)))
4239 : 28950 : cmp = 0;
4240 : 48379 : if (cmp < 0)
4241 : : end = middle;
4242 : 46508 : else if (cmp > 0)
4243 : 13884 : begin = middle + 1;
4244 : : else
4245 : : {
4246 : 32624 : if (insert && TREE_CODE (elt.value) == RAW_DATA_CST)
4247 : : {
4248 : : /* We need to split the RAW_DATA_CST elt. */
4249 : 122 : constructor_elt e;
4250 : 122 : gcc_checking_assert (TREE_CODE (idx) != RANGE_EXPR);
4251 : 244 : unsigned int off = (wi::to_offset (dindex)
4252 : 244 : - wi::to_offset (idx)).to_uhwi ();
4253 : 122 : tree value = elt.value;
4254 : 122 : unsigned int len = RAW_DATA_LENGTH (value);
4255 : 122 : if (off > 1 && len >= off + 3)
4256 : 22 : value = copy_node (elt.value);
4257 : 122 : if (off)
4258 : : {
4259 : 33 : if (off > 1)
4260 : 33 : RAW_DATA_LENGTH (elt.value) = off;
4261 : : else
4262 : 0 : elt.value = raw_data_cst_elt (elt.value, 0);
4263 : 33 : e.index = size_binop (PLUS_EXPR, elt.index,
4264 : : build_int_cst (TREE_TYPE (elt.index),
4265 : : off));
4266 : 33 : e.value = NULL_TREE;
4267 : 33 : ++middle;
4268 : 33 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e);
4269 : : }
4270 : 122 : (*elts)[middle].value = raw_data_cst_elt (value, off);
4271 : 122 : if (len >= off + 2)
4272 : : {
4273 : 111 : e.index = (*elts)[middle].index;
4274 : 111 : e.index = size_binop (PLUS_EXPR, e.index,
4275 : : build_one_cst (TREE_TYPE (e.index)));
4276 : 111 : if (len >= off + 3)
4277 : : {
4278 : 111 : RAW_DATA_LENGTH (value) -= off + 1;
4279 : 111 : RAW_DATA_POINTER (value) += off + 1;
4280 : 111 : e.value = value;
4281 : : }
4282 : : else
4283 : 0 : e.value = raw_data_cst_elt (value, off + 1);
4284 : 111 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e);
4285 : : }
4286 : 122 : return middle;
4287 : : }
4288 : 572 : if (insert && TREE_CODE (idx) == RANGE_EXPR)
4289 : : {
4290 : : /* We need to split the range. */
4291 : 434 : constructor_elt e;
4292 : 434 : tree lo = TREE_OPERAND (idx, 0);
4293 : 434 : tree hi = TREE_OPERAND (idx, 1);
4294 : 434 : tree value = elt.value;
4295 : 434 : dindex = fold_convert (sizetype, dindex);
4296 : 434 : if (tree_int_cst_lt (lo, dindex))
4297 : : {
4298 : : /* There are still some lower elts; shorten the range. */
4299 : 190 : tree new_hi = int_const_binop (MINUS_EXPR, dindex,
4300 : 95 : size_one_node);
4301 : 95 : if (tree_int_cst_equal (lo, new_hi))
4302 : : /* Only one element left, no longer a range. */
4303 : 43 : elt.index = lo;
4304 : : else
4305 : 52 : TREE_OPERAND (idx, 1) = new_hi;
4306 : : /* Append the element we want to insert. */
4307 : 95 : ++middle;
4308 : 95 : e.index = dindex;
4309 : 95 : e.value = unshare_constructor (value);
4310 : 95 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e);
4311 : : }
4312 : : else
4313 : : /* No lower elts, the range elt is now ours. */
4314 : 339 : elt.index = dindex;
4315 : :
4316 : 434 : if (tree_int_cst_lt (dindex, hi))
4317 : : {
4318 : : /* There are still some higher elts; append a range. */
4319 : 592 : tree new_lo = int_const_binop (PLUS_EXPR, dindex,
4320 : 296 : size_one_node);
4321 : 296 : if (tree_int_cst_equal (new_lo, hi))
4322 : 155 : e.index = hi;
4323 : : else
4324 : 141 : e.index = build2 (RANGE_EXPR, sizetype, new_lo, hi);
4325 : 296 : e.value = unshare_constructor (value);
4326 : 296 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e);
4327 : : }
4328 : : }
4329 : 32502 : return middle;
4330 : : }
4331 : : }
4332 : :
4333 : 808830 : if (insert)
4334 : : {
4335 : 806079 : constructor_elt e = { dindex, NULL_TREE };
4336 : 806079 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), end, e);
4337 : 806079 : return end;
4338 : : }
4339 : :
4340 : : return -1;
4341 : : }
4342 : :
4343 : : /* Return a pointer to the constructor_elt of CTOR which matches INDEX. If no
4344 : : matching constructor_elt exists, then add one to CTOR.
4345 : :
4346 : : As an optimization, if POS_HINT is non-negative then it is used as a guess
4347 : : for the (integer) index of the matching constructor_elt within CTOR. */
4348 : :
4349 : : static constructor_elt *
4350 : 8339298 : get_or_insert_ctor_field (tree ctor, tree index, int pos_hint = -1)
4351 : : {
4352 : : /* Check the hint first. */
4353 : 761325 : if (pos_hint >= 0 && (unsigned)pos_hint < CONSTRUCTOR_NELTS (ctor)
4354 : 9100623 : && CONSTRUCTOR_ELT (ctor, pos_hint)->index == index)
4355 : : return CONSTRUCTOR_ELT (ctor, pos_hint);
4356 : :
4357 : 7577978 : tree type = TREE_TYPE (ctor);
4358 : 7577978 : if (TREE_CODE (type) == VECTOR_TYPE && index == NULL_TREE)
4359 : : {
4360 : 128 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (ctor), index, NULL_TREE);
4361 : 128 : return &CONSTRUCTOR_ELTS (ctor)->last();
4362 : : }
4363 : 7577850 : else if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
4364 : : {
4365 : 1105973 : if (TREE_CODE (index) == RANGE_EXPR)
4366 : : {
4367 : : /* Support for RANGE_EXPR index lookups is currently limited to
4368 : : accessing an existing element via POS_HINT, or appending a new
4369 : : element to the end of CTOR. ??? Support for other access
4370 : : patterns may also be needed. */
4371 : 3 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
4372 : 3 : if (vec_safe_length (elts))
4373 : : {
4374 : 3 : tree lo = TREE_OPERAND (index, 0);
4375 : 3 : gcc_assert (array_index_cmp (elts->last().index, lo) < 0);
4376 : : }
4377 : 3 : CONSTRUCTOR_APPEND_ELT (elts, index, NULL_TREE);
4378 : 3 : return &elts->last();
4379 : : }
4380 : :
4381 : 1105970 : HOST_WIDE_INT i = find_array_ctor_elt (ctor, index, /*insert*/true);
4382 : 1105970 : gcc_assert (i >= 0);
4383 : 1105970 : constructor_elt *cep = CONSTRUCTOR_ELT (ctor, i);
4384 : 1105970 : gcc_assert (cep->index == NULL_TREE
4385 : : || TREE_CODE (cep->index) != RANGE_EXPR);
4386 : : return cep;
4387 : : }
4388 : : else
4389 : : {
4390 : 6471877 : gcc_assert (TREE_CODE (index) == FIELD_DECL
4391 : : && (same_type_ignoring_top_level_qualifiers_p
4392 : : (DECL_CONTEXT (index), TREE_TYPE (ctor))));
4393 : :
4394 : : /* We must keep the CONSTRUCTOR's ELTS in FIELD order.
4395 : : Usually we meet initializers in that order, but it is
4396 : : possible for base types to be placed not in program
4397 : : order. */
4398 : 6471877 : tree fields = TYPE_FIELDS (DECL_CONTEXT (index));
4399 : 6471877 : unsigned HOST_WIDE_INT idx = 0;
4400 : 6471877 : constructor_elt *cep = NULL;
4401 : :
4402 : : /* Check if we're changing the active member of a union. */
4403 : 159291 : if (TREE_CODE (type) == UNION_TYPE && CONSTRUCTOR_NELTS (ctor)
4404 : 6514979 : && CONSTRUCTOR_ELT (ctor, 0)->index != index)
4405 : 3736 : vec_safe_truncate (CONSTRUCTOR_ELTS (ctor), 0);
4406 : : /* If the bit offset of INDEX is larger than that of the last
4407 : : constructor_elt, then we can just immediately append a new
4408 : : constructor_elt to the end of CTOR. */
4409 : 6468141 : else if (CONSTRUCTOR_NELTS (ctor)
4410 : 7182153 : && tree_int_cst_compare (bit_position (index),
4411 : 6965800 : bit_position (CONSTRUCTOR_ELTS (ctor)
4412 : 3482900 : ->last().index)) > 0)
4413 : : {
4414 : 1516700 : idx = CONSTRUCTOR_NELTS (ctor);
4415 : 1516700 : goto insert;
4416 : : }
4417 : :
4418 : : /* Otherwise, we need to iterate over CTOR to find or insert INDEX
4419 : : appropriately. */
4420 : :
4421 : 6515018 : for (; vec_safe_iterate (CONSTRUCTOR_ELTS (ctor), idx, &cep);
4422 : 1559841 : idx++, fields = DECL_CHAIN (fields))
4423 : : {
4424 : 3526041 : if (index == cep->index)
4425 : 1963002 : goto found;
4426 : :
4427 : : /* The field we're initializing must be on the field
4428 : : list. Look to see if it is present before the
4429 : : field the current ELT initializes. */
4430 : 10943648 : for (; fields != cep->index; fields = DECL_CHAIN (fields))
4431 : 9383807 : if (index == fields)
4432 : 3198 : goto insert;
4433 : : }
4434 : : /* We fell off the end of the CONSTRUCTOR, so insert a new
4435 : : entry at the end. */
4436 : :
4437 : 4508875 : insert:
4438 : 4508875 : {
4439 : 4508875 : constructor_elt ce = { index, NULL_TREE };
4440 : :
4441 : 4508875 : vec_safe_insert (CONSTRUCTOR_ELTS (ctor), idx, ce);
4442 : 4508875 : cep = CONSTRUCTOR_ELT (ctor, idx);
4443 : : }
4444 : 6471877 : found:;
4445 : :
4446 : 6471877 : return cep;
4447 : : }
4448 : : }
4449 : :
4450 : : /* Under the control of CTX, issue a detailed diagnostic for
4451 : : an out-of-bounds subscript INDEX into the expression ARRAY. */
4452 : :
4453 : : static void
4454 : 541 : diag_array_subscript (location_t loc, const constexpr_ctx *ctx, tree array, tree index)
4455 : : {
4456 : 541 : if (!ctx->quiet)
4457 : : {
4458 : 115 : tree arraytype = TREE_TYPE (array);
4459 : :
4460 : : /* Convert the unsigned array subscript to a signed integer to avoid
4461 : : printing huge numbers for small negative values. */
4462 : 115 : tree sidx = fold_convert (ssizetype, index);
4463 : 115 : STRIP_ANY_LOCATION_WRAPPER (array);
4464 : 115 : if (DECL_P (array))
4465 : : {
4466 : 75 : auto_diagnostic_group d;
4467 : 75 : if (TYPE_DOMAIN (arraytype))
4468 : 69 : error_at (loc, "array subscript value %qE is outside the bounds "
4469 : : "of array %qD of type %qT", sidx, array, arraytype);
4470 : : else
4471 : 6 : error_at (loc, "nonzero array subscript %qE is used with array %qD of "
4472 : : "type %qT with unknown bounds", sidx, array, arraytype);
4473 : 75 : inform (DECL_SOURCE_LOCATION (array), "declared here");
4474 : 75 : }
4475 : 40 : else if (TYPE_DOMAIN (arraytype))
4476 : 37 : error_at (loc, "array subscript value %qE is outside the bounds "
4477 : : "of array type %qT", sidx, arraytype);
4478 : : else
4479 : 3 : error_at (loc, "nonzero array subscript %qE is used with array of type %qT "
4480 : : "with unknown bounds", sidx, arraytype);
4481 : : }
4482 : 541 : }
4483 : :
4484 : : /* Return the number of elements for TYPE (which is an ARRAY_TYPE or
4485 : : a VECTOR_TYPE). */
4486 : :
4487 : : static tree
4488 : 2715228 : get_array_or_vector_nelts (const constexpr_ctx *ctx, tree type,
4489 : : bool *non_constant_p, bool *overflow_p)
4490 : : {
4491 : 2715228 : tree nelts;
4492 : 2715228 : if (TREE_CODE (type) == ARRAY_TYPE)
4493 : : {
4494 : 2715228 : if (TYPE_DOMAIN (type))
4495 : 2715001 : nelts = array_type_nelts_top (type);
4496 : : else
4497 : 227 : nelts = size_zero_node;
4498 : : }
4499 : 0 : else if (VECTOR_TYPE_P (type))
4500 : 0 : nelts = size_int (TYPE_VECTOR_SUBPARTS (type));
4501 : : else
4502 : 0 : gcc_unreachable ();
4503 : :
4504 : : /* For VLAs, the number of elements won't be an integer constant. */
4505 : 2715228 : nelts = cxx_eval_constant_expression (ctx, nelts, vc_prvalue,
4506 : : non_constant_p, overflow_p);
4507 : 2715228 : return nelts;
4508 : : }
4509 : :
4510 : : /* Extract element INDEX consisting of CHARS_PER_ELT chars from
4511 : : STRING_CST STRING. */
4512 : :
4513 : : static tree
4514 : 389499 : extract_string_elt (tree string, unsigned chars_per_elt, unsigned index)
4515 : : {
4516 : 389499 : tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (string)));
4517 : 389499 : tree r;
4518 : :
4519 : 389499 : if (chars_per_elt == 1)
4520 : 363930 : r = build_int_cst (type, TREE_STRING_POINTER (string)[index]);
4521 : : else
4522 : : {
4523 : 25569 : const unsigned char *ptr
4524 : 25569 : = ((const unsigned char *)TREE_STRING_POINTER (string)
4525 : 25569 : + index * chars_per_elt);
4526 : 25569 : r = native_interpret_expr (type, ptr, chars_per_elt);
4527 : : }
4528 : 389499 : return r;
4529 : : }
4530 : :
4531 : : /* Subroutine of cxx_eval_array_reference. T is an ARRAY_REF; evaluate the
4532 : : subscript, diagnose any problems with it, and return the result. */
4533 : :
4534 : : static tree
4535 : 2718278 : eval_and_check_array_index (const constexpr_ctx *ctx,
4536 : : tree t, bool allow_one_past,
4537 : : bool *non_constant_p, bool *overflow_p)
4538 : : {
4539 : 2718278 : location_t loc = cp_expr_loc_or_input_loc (t);
4540 : 2718278 : tree ary = TREE_OPERAND (t, 0);
4541 : 2718278 : t = TREE_OPERAND (t, 1);
4542 : 2718278 : tree index = cxx_eval_constant_expression (ctx, t, vc_prvalue,
4543 : : non_constant_p, overflow_p);
4544 : 2718278 : VERIFY_CONSTANT (index);
4545 : :
4546 : 2714710 : if (!tree_fits_shwi_p (index)
4547 : 2714710 : || tree_int_cst_sgn (index) < 0)
4548 : : {
4549 : 108 : diag_array_subscript (loc, ctx, ary, index);
4550 : 108 : *non_constant_p = true;
4551 : 108 : return t;
4552 : : }
4553 : :
4554 : 2714602 : tree nelts = get_array_or_vector_nelts (ctx, TREE_TYPE (ary), non_constant_p,
4555 : : overflow_p);
4556 : 2714602 : VERIFY_CONSTANT (nelts);
4557 : 2714539 : if (allow_one_past
4558 : 4432164 : ? !tree_int_cst_le (index, nelts)
4559 : 1717625 : : !tree_int_cst_lt (index, nelts))
4560 : : {
4561 : 433 : diag_array_subscript (loc, ctx, ary, index);
4562 : 433 : *non_constant_p = true;
4563 : 433 : return t;
4564 : : }
4565 : :
4566 : : return index;
4567 : : }
4568 : :
4569 : : /* Subroutine of cxx_eval_constant_expression.
4570 : : Attempt to reduce a reference to an array slot. */
4571 : :
4572 : : static tree
4573 : 1871869 : cxx_eval_array_reference (const constexpr_ctx *ctx, tree t,
4574 : : value_cat lval,
4575 : : bool *non_constant_p, bool *overflow_p)
4576 : : {
4577 : 1871869 : tree oldary = TREE_OPERAND (t, 0);
4578 : 1871869 : tree ary = cxx_eval_constant_expression (ctx, oldary,
4579 : : lval,
4580 : : non_constant_p, overflow_p);
4581 : 1871869 : if (*non_constant_p)
4582 : : return t;
4583 : 1691649 : if (!lval
4584 : 691283 : && TREE_CODE (ary) == VIEW_CONVERT_EXPR
4585 : 13282 : && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary, 0)))
4586 : 1704931 : && (TYPE_MAIN_VARIANT (TREE_TYPE (t))
4587 : 13282 : == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary, 0))))))
4588 : 13282 : ary = TREE_OPERAND (ary, 0);
4589 : :
4590 : 1691649 : tree oldidx = TREE_OPERAND (t, 1);
4591 : 1691649 : tree index = eval_and_check_array_index (ctx, t, lval,
4592 : : non_constant_p, overflow_p);
4593 : 1691649 : if (*non_constant_p)
4594 : : return t;
4595 : :
4596 : 1687531 : if (lval && ary == oldary && index == oldidx)
4597 : : return t;
4598 : 791556 : else if (lval == vc_discard)
4599 : : return t;
4600 : 791556 : else if (lval)
4601 : 100701 : return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL);
4602 : :
4603 : 690855 : unsigned len = 0, elem_nchars = 1;
4604 : 690855 : tree elem_type = TREE_TYPE (TREE_TYPE (ary));
4605 : 690855 : if (TREE_CODE (ary) == CONSTRUCTOR)
4606 : 290198 : len = CONSTRUCTOR_NELTS (ary);
4607 : 400657 : else if (TREE_CODE (ary) == STRING_CST)
4608 : : {
4609 : 387383 : elem_nchars = (TYPE_PRECISION (elem_type)
4610 : 387383 : / TYPE_PRECISION (char_type_node));
4611 : 387383 : len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars;
4612 : : }
4613 : 13274 : else if (TREE_CODE (ary) == VECTOR_CST)
4614 : : /* We don't create variable-length VECTOR_CSTs. */
4615 : 13274 : len = VECTOR_CST_NELTS (ary).to_constant ();
4616 : : else
4617 : : {
4618 : : /* We can't do anything with other tree codes, so use
4619 : : VERIFY_CONSTANT to complain and fail. */
4620 : 0 : VERIFY_CONSTANT (ary);
4621 : 0 : gcc_unreachable ();
4622 : : }
4623 : :
4624 : 690855 : bool found;
4625 : 690855 : HOST_WIDE_INT i = 0;
4626 : 690855 : if (TREE_CODE (ary) == CONSTRUCTOR)
4627 : : {
4628 : 290198 : HOST_WIDE_INT ix = find_array_ctor_elt (ary, index);
4629 : 290198 : found = (ix >= 0);
4630 : 290198 : if (found)
4631 : 287533 : i = ix;
4632 : : }
4633 : : else
4634 : : {
4635 : 400657 : i = tree_to_shwi (index);
4636 : 400657 : found = (i < len);
4637 : : }
4638 : :
4639 : 690855 : if (found)
4640 : : {
4641 : 687495 : tree r;
4642 : 687495 : if (TREE_CODE (ary) == CONSTRUCTOR)
4643 : : {
4644 : 287533 : r = (*CONSTRUCTOR_ELTS (ary))[i].value;
4645 : 287533 : if (TREE_CODE (r) == RAW_DATA_CST)
4646 : : {
4647 : 28962 : tree ridx = (*CONSTRUCTOR_ELTS (ary))[i].index;
4648 : 28962 : gcc_checking_assert (ridx);
4649 : 28962 : unsigned int off
4650 : 28962 : = (wi::to_offset (index) - wi::to_offset (ridx)).to_uhwi ();
4651 : 28962 : r = raw_data_cst_elt (r, off);
4652 : : }
4653 : : }
4654 : 399962 : else if (TREE_CODE (ary) == VECTOR_CST)
4655 : 13274 : r = VECTOR_CST_ELT (ary, i);
4656 : : else
4657 : 386688 : r = extract_string_elt (ary, elem_nchars, i);
4658 : :
4659 : 428924 : if (r)
4660 : : /* Don't VERIFY_CONSTANT here. */
4661 : 687495 : return r;
4662 : :
4663 : : /* Otherwise the element doesn't have a value yet. */
4664 : : }
4665 : :
4666 : : /* Not found. */
4667 : :
4668 : 3360 : if (is_really_empty_class (elem_type, /*ignore_vptr*/false))
4669 : 49 : return build_constructor (elem_type, NULL);
4670 : :
4671 : 3311 : if (TREE_CODE (ary) == CONSTRUCTOR
4672 : 3311 : && CONSTRUCTOR_NO_CLEARING (ary))
4673 : : {
4674 : : /* 'ary' is part of the aggregate initializer we're currently
4675 : : building; if there's no initializer for this element yet,
4676 : : that's an error. */
4677 : 51 : if (!ctx->quiet)
4678 : 16 : error ("accessing uninitialized array element");
4679 : 51 : *non_constant_p = true;
4680 : 51 : return t;
4681 : : }
4682 : :
4683 : : /* If it's within the array bounds but doesn't have an explicit
4684 : : initializer, it's initialized from {}. But use build_value_init
4685 : : directly for non-aggregates to avoid creating a garbage CONSTRUCTOR. */
4686 : 3260 : tree val;
4687 : 3260 : constexpr_ctx new_ctx;
4688 : 3260 : if (CP_AGGREGATE_TYPE_P (elem_type))
4689 : : {
4690 : 794 : tree empty_ctor = build_constructor (init_list_type_node, NULL);
4691 : 794 : val = digest_init (elem_type, empty_ctor, tf_warning_or_error);
4692 : : }
4693 : : else
4694 : 2466 : val = build_value_init (elem_type, tf_warning_or_error);
4695 : :
4696 : : /* Create a new constructor only if we don't already have a suitable one. */
4697 : 3260 : const bool new_ctor = (!SCALAR_TYPE_P (elem_type)
4698 : 4080 : && (!ctx->ctor
4699 : 15 : || !same_type_ignoring_top_level_qualifiers_p
4700 : 15 : (elem_type, TREE_TYPE (ctx->ctor))));
4701 : 811 : if (new_ctor)
4702 : : {
4703 : 811 : new_ctx = *ctx;
4704 : : /* We clear the object here. We used to replace it with T, but that
4705 : : caused problems (101371, 108158); and anyway, T is the initializer,
4706 : : not the target object. */
4707 : 811 : new_ctx.object = NULL_TREE;
4708 : 811 : new_ctx.ctor = build_constructor (elem_type, NULL);
4709 : 811 : ctx = &new_ctx;
4710 : : }
4711 : 3260 : t = cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
4712 : : overflow_p);
4713 : 3260 : if (new_ctor && t != ctx->ctor)
4714 : 778 : free_constructor (ctx->ctor);
4715 : : return t;
4716 : : }
4717 : :
4718 : : /* Subroutine of cxx_eval_constant_expression.
4719 : : Attempt to reduce a field access of a value of class type. */
4720 : :
4721 : : static tree
4722 : 37105937 : cxx_eval_component_reference (const constexpr_ctx *ctx, tree t,
4723 : : value_cat lval,
4724 : : bool *non_constant_p, bool *overflow_p)
4725 : : {
4726 : 37105937 : unsigned HOST_WIDE_INT i;
4727 : 37105937 : tree field;
4728 : 37105937 : tree value;
4729 : 37105937 : tree part = TREE_OPERAND (t, 1);
4730 : 37105937 : tree orig_whole = TREE_OPERAND (t, 0);
4731 : 37105937 : tree whole = cxx_eval_constant_expression (ctx, orig_whole,
4732 : : lval,
4733 : : non_constant_p, overflow_p);
4734 : 37105937 : if (*non_constant_p)
4735 : : return t;
4736 : 22188601 : if (INDIRECT_REF_P (whole)
4737 : 22188601 : && integer_zerop (TREE_OPERAND (whole, 0)))
4738 : : {
4739 : 177 : if (!ctx->quiet)
4740 : 39 : error ("dereferencing a null pointer in %qE", orig_whole);
4741 : 177 : *non_constant_p = true;
4742 : 177 : return t;
4743 : : }
4744 : :
4745 : 22188424 : if (TREE_CODE (whole) == PTRMEM_CST)
4746 : 1375 : whole = cplus_expand_constant (whole);
4747 : 22188424 : if (whole == orig_whole)
4748 : : return t;
4749 : 10023147 : if (lval == vc_discard)
4750 : : return t;
4751 : 10023123 : if (lval)
4752 : 6857854 : return fold_build3 (COMPONENT_REF, TREE_TYPE (t),
4753 : : whole, part, NULL_TREE);
4754 : : /* Don't VERIFY_CONSTANT here; we only want to check that we got a
4755 : : CONSTRUCTOR. */
4756 : 3165269 : if (TREE_CODE (whole) != CONSTRUCTOR)
4757 : : {
4758 : 0 : if (!ctx->quiet)
4759 : 0 : error ("%qE is not a constant expression", orig_whole);
4760 : 0 : *non_constant_p = true;
4761 : 0 : return t;
4762 : : }
4763 : 3163457 : if ((cxx_dialect < cxx14 || CONSTRUCTOR_MUTABLE_POISON (whole))
4764 : 3167251 : && DECL_MUTABLE_P (part))
4765 : : {
4766 : 132 : if (!ctx->quiet)
4767 : 16 : error ("mutable %qD is not usable in a constant expression", part);
4768 : 132 : *non_constant_p = true;
4769 : 132 : return t;
4770 : : }
4771 : :
4772 : 3165137 : bool pmf = TYPE_PTRMEMFUNC_P (TREE_TYPE (whole));
4773 : 4293722 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
4774 : : {
4775 : : /* Use name match for PMF fields, as a variant will have a
4776 : : different FIELD_DECL with a different type. */
4777 : 4291311 : if (pmf ? DECL_NAME (field) == DECL_NAME (part)
4778 : : : field == part)
4779 : : {
4780 : 3162726 : if (value)
4781 : : {
4782 : 3162708 : STRIP_ANY_LOCATION_WRAPPER (value);
4783 : 3162708 : return value;
4784 : : }
4785 : : else
4786 : : /* We're in the middle of initializing it. */
4787 : : break;
4788 : : }
4789 : : }
4790 : 2429 : if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE)
4791 : : {
4792 : 89 : if (CONSTRUCTOR_NELTS (whole) > 0)
4793 : : {
4794 : : /* DR 1188 says we don't have to deal with this. */
4795 : 71 : if (!ctx->quiet)
4796 : : {
4797 : 18 : constructor_elt *cep = CONSTRUCTOR_ELT (whole, 0);
4798 : 18 : if (cep->value == NULL_TREE)
4799 : 9 : error ("accessing uninitialized member %qD", part);
4800 : : else
4801 : 9 : error ("accessing %qD member instead of initialized %qD member "
4802 : : "in constant expression", part, cep->index);
4803 : : }
4804 : 71 : *non_constant_p = true;
4805 : 71 : return t;
4806 : : }
4807 : 18 : else if (!CONSTRUCTOR_NO_CLEARING (whole))
4808 : : {
4809 : : /* Value-initialized union, check if looking at the first member. */
4810 : 15 : tree first = next_aggregate_field (TYPE_FIELDS (TREE_TYPE (whole)));
4811 : 15 : if (first != part)
4812 : : {
4813 : 9 : if (!ctx->quiet)
4814 : 3 : error ("accessing %qD member instead of initialized %qD "
4815 : : "member in constant expression", part, first);
4816 : 9 : *non_constant_p = true;
4817 : 9 : return t;
4818 : : }
4819 : : }
4820 : : }
4821 : :
4822 : : /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty
4823 : : classes never get represented; throw together a value now. */
4824 : 2349 : if (is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
4825 : 1614 : return build_constructor (TREE_TYPE (t), NULL);
4826 : :
4827 : 735 : gcc_assert (DECL_CONTEXT (part) == TYPE_MAIN_VARIANT (TREE_TYPE (whole)));
4828 : :
4829 : 735 : if (CONSTRUCTOR_NO_CLEARING (whole))
4830 : : {
4831 : : /* 'whole' is part of the aggregate initializer we're currently
4832 : : building; if there's no initializer for this member yet, that's an
4833 : : error. */
4834 : 435 : if (!ctx->quiet)
4835 : 21 : error ("accessing uninitialized member %qD", part);
4836 : 435 : *non_constant_p = true;
4837 : 435 : return t;
4838 : : }
4839 : :
4840 : : /* If there's no explicit init for this field, it's value-initialized. */
4841 : 300 : value = build_value_init (TREE_TYPE (t), tf_warning_or_error);
4842 : 300 : return cxx_eval_constant_expression (ctx, value,
4843 : : lval,
4844 : 300 : non_constant_p, overflow_p);
4845 : : }
4846 : :
4847 : : /* Subroutine of cxx_eval_constant_expression.
4848 : : Attempt to reduce a field access of a value of class type that is
4849 : : expressed as a BIT_FIELD_REF. */
4850 : :
4851 : : static tree
4852 : 49 : cxx_eval_bit_field_ref (const constexpr_ctx *ctx, tree t,
4853 : : value_cat lval,
4854 : : bool *non_constant_p, bool *overflow_p)
4855 : : {
4856 : 49 : tree orig_whole = TREE_OPERAND (t, 0);
4857 : 49 : tree retval, fldval, utype, mask;
4858 : 49 : bool fld_seen = false;
4859 : 49 : HOST_WIDE_INT istart, isize;
4860 : 49 : tree whole = cxx_eval_constant_expression (ctx, orig_whole,
4861 : : lval,
4862 : : non_constant_p, overflow_p);
4863 : 49 : tree start, field, value;
4864 : 49 : unsigned HOST_WIDE_INT i;
4865 : :
4866 : 49 : if (whole == orig_whole)
4867 : : return t;
4868 : : /* Don't VERIFY_CONSTANT here; we only want to check that we got a
4869 : : CONSTRUCTOR. */
4870 : 2 : if (!*non_constant_p
4871 : 2 : && TREE_CODE (whole) != VECTOR_CST
4872 : 0 : && TREE_CODE (whole) != CONSTRUCTOR)
4873 : : {
4874 : 0 : if (!ctx->quiet)
4875 : 0 : error ("%qE is not a constant expression", orig_whole);
4876 : 0 : *non_constant_p = true;
4877 : : }
4878 : 2 : if (*non_constant_p)
4879 : : return t;
4880 : :
4881 : 2 : if (TREE_CODE (whole) == VECTOR_CST || !INTEGRAL_TYPE_P (TREE_TYPE (t)))
4882 : : {
4883 : 2 : if (tree r = fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
4884 : : TREE_OPERAND (t, 1), TREE_OPERAND (t, 2)))
4885 : : return r;
4886 : 0 : if (!ctx->quiet)
4887 : 0 : error ("%qE is not a constant expression", orig_whole);
4888 : 0 : *non_constant_p = true;
4889 : 0 : return t;
4890 : : }
4891 : :
4892 : 0 : start = TREE_OPERAND (t, 2);
4893 : 0 : istart = tree_to_shwi (start);
4894 : 0 : isize = tree_to_shwi (TREE_OPERAND (t, 1));
4895 : 0 : utype = TREE_TYPE (t);
4896 : 0 : if (!TYPE_UNSIGNED (utype))
4897 : 0 : utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1);
4898 : 0 : retval = build_int_cst (utype, 0);
4899 : 0 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
4900 : : {
4901 : 0 : tree bitpos = bit_position (field);
4902 : 0 : STRIP_ANY_LOCATION_WRAPPER (value);
4903 : 0 : if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1))
4904 : : return value;
4905 : 0 : if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE
4906 : 0 : && TREE_CODE (value) == INTEGER_CST
4907 : 0 : && tree_fits_shwi_p (bitpos)
4908 : 0 : && tree_fits_shwi_p (DECL_SIZE (field)))
4909 : : {
4910 : 0 : HOST_WIDE_INT bit = tree_to_shwi (bitpos);
4911 : 0 : HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field));
4912 : 0 : HOST_WIDE_INT shift;
4913 : 0 : if (bit >= istart && bit + sz <= istart + isize)
4914 : : {
4915 : 0 : fldval = fold_convert (utype, value);
4916 : 0 : mask = build_int_cst_type (utype, -1);
4917 : 0 : mask = fold_build2 (LSHIFT_EXPR, utype, mask,
4918 : : size_int (TYPE_PRECISION (utype) - sz));
4919 : 0 : mask = fold_build2 (RSHIFT_EXPR, utype, mask,
4920 : : size_int (TYPE_PRECISION (utype) - sz));
4921 : 0 : fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask);
4922 : 0 : shift = bit - istart;
4923 : 0 : if (BYTES_BIG_ENDIAN)
4924 : : shift = TYPE_PRECISION (utype) - shift - sz;
4925 : 0 : fldval = fold_build2 (LSHIFT_EXPR, utype, fldval,
4926 : : size_int (shift));
4927 : 0 : retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval);
4928 : 0 : fld_seen = true;
4929 : : }
4930 : : }
4931 : : }
4932 : 0 : if (fld_seen)
4933 : 0 : return fold_convert (TREE_TYPE (t), retval);
4934 : 0 : gcc_unreachable ();
4935 : : return error_mark_node;
4936 : : }
4937 : :
4938 : : /* Helper for cxx_eval_bit_cast.
4939 : : Check [bit.cast]/3 rules, bit_cast is constexpr only if the To and From
4940 : : types and types of all subobjects have is_union_v<T>, is_pointer_v<T>,
4941 : : is_member_pointer_v<T>, is_volatile_v<T> false and has no non-static
4942 : : data members of reference type. */
4943 : :
4944 : : static bool
4945 : 5526 : check_bit_cast_type (const constexpr_ctx *ctx, location_t loc, tree type,
4946 : : tree orig_type)
4947 : : {
4948 : 6097 : if (TREE_CODE (type) == UNION_TYPE)
4949 : : {
4950 : 63 : if (!ctx->quiet)
4951 : : {
4952 : 21 : if (type == orig_type)
4953 : 6 : error_at (loc, "%qs is not a constant expression because %qT is "
4954 : : "a union type", "__builtin_bit_cast", type);
4955 : : else
4956 : 15 : error_at (loc, "%qs is not a constant expression because %qT "
4957 : : "contains a union type", "__builtin_bit_cast",
4958 : : orig_type);
4959 : : }
4960 : 63 : return true;
4961 : : }
4962 : : if (TREE_CODE (type) == POINTER_TYPE)
4963 : : {
4964 : 68 : if (!ctx->quiet)
4965 : : {
4966 : 18 : if (type == orig_type)
4967 : 6 : error_at (loc, "%qs is not a constant expression because %qT is "
4968 : : "a pointer type", "__builtin_bit_cast", type);
4969 : : else
4970 : 12 : error_at (loc, "%qs is not a constant expression because %qT "
4971 : : "contains a pointer type", "__builtin_bit_cast",
4972 : : orig_type);
4973 : : }
4974 : 68 : return true;
4975 : : }
4976 : : if (TREE_CODE (type) == REFERENCE_TYPE)
4977 : : {
4978 : 0 : if (!ctx->quiet)
4979 : : {
4980 : 0 : if (type == orig_type)
4981 : 0 : error_at (loc, "%qs is not a constant expression because %qT is "
4982 : : "a reference type", "__builtin_bit_cast", type);
4983 : : else
4984 : 0 : error_at (loc, "%qs is not a constant expression because %qT "
4985 : : "contains a reference type", "__builtin_bit_cast",
4986 : : orig_type);
4987 : : }
4988 : 0 : return true;
4989 : : }
4990 : 1505 : if (TYPE_PTRMEM_P (type))
4991 : : {
4992 : 36 : if (!ctx->quiet)
4993 : : {
4994 : 12 : if (type == orig_type)
4995 : 12 : error_at (loc, "%qs is not a constant expression because %qT is "
4996 : : "a pointer to member type", "__builtin_bit_cast",
4997 : : type);
4998 : : else
4999 : 0 : error_at (loc, "%qs is not a constant expression because %qT "
5000 : : "contains a pointer to member type",
5001 : : "__builtin_bit_cast", orig_type);
5002 : : }
5003 : 36 : return true;
5004 : : }
5005 : 5930 : if (TYPE_VOLATILE (type))
5006 : : {
5007 : 0 : if (!ctx->quiet)
5008 : : {
5009 : 0 : if (type == orig_type)
5010 : 0 : error_at (loc, "%qs is not a constant expression because %qT is "
5011 : : "volatile", "__builtin_bit_cast", type);
5012 : : else
5013 : 0 : error_at (loc, "%qs is not a constant expression because %qT "
5014 : : "contains a volatile subobject",
5015 : : "__builtin_bit_cast", orig_type);
5016 : : }
5017 : 0 : return true;
5018 : : }
5019 : 5930 : if (TREE_CODE (type) == RECORD_TYPE)
5020 : 31348 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5021 : 29951 : if (TREE_CODE (field) == FIELD_DECL
5022 : 29951 : && check_bit_cast_type (ctx, loc, TREE_TYPE (field), orig_type))
5023 : : return true;
5024 : 5840 : if (TREE_CODE (type) == ARRAY_TYPE)
5025 : 571 : return check_bit_cast_type (ctx, loc, TREE_TYPE (type), orig_type);
5026 : : return false;
5027 : : }
5028 : :
5029 : : /* Helper function for cxx_eval_bit_cast. For unsigned char or
5030 : : std::byte members of CONSTRUCTOR (recursively) if they contain
5031 : : some indeterminate bits (as set in MASK), remove the ctor elts,
5032 : : mark the CONSTRUCTOR as CONSTRUCTOR_NO_CLEARING and clear the
5033 : : bits in MASK. */
5034 : :
5035 : : static void
5036 : 659 : clear_uchar_or_std_byte_in_mask (location_t loc, tree t, unsigned char *mask)
5037 : : {
5038 : 659 : if (TREE_CODE (t) != CONSTRUCTOR)
5039 : : return;
5040 : :
5041 : : unsigned i, j = 0;
5042 : : tree index, value;
5043 : 2258 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), i, index, value)
5044 : : {
5045 : 1599 : tree type = TREE_TYPE (value);
5046 : 1599 : if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
5047 : 2759 : && DECL_BIT_FIELD_TYPE (index) != NULL_TREE)
5048 : : {
5049 : 270 : if (is_byte_access_type_not_plain_char (DECL_BIT_FIELD_TYPE (index)))
5050 : : {
5051 : 135 : HOST_WIDE_INT fldsz = TYPE_PRECISION (TREE_TYPE (index));
5052 : 135 : gcc_assert (fldsz != 0);
5053 : 135 : HOST_WIDE_INT pos = int_byte_position (index);
5054 : 135 : HOST_WIDE_INT bpos
5055 : 135 : = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (index));
5056 : 135 : bpos %= BITS_PER_UNIT;
5057 : 135 : HOST_WIDE_INT end
5058 : 135 : = ROUND_UP (bpos + fldsz, BITS_PER_UNIT) / BITS_PER_UNIT;
5059 : 135 : gcc_assert (end == 1 || end == 2);
5060 : 135 : unsigned char *p = mask + pos;
5061 : 135 : unsigned char mask_save[2];
5062 : 135 : mask_save[0] = mask[pos];
5063 : 135 : mask_save[1] = end == 2 ? mask[pos + 1] : 0;
5064 : 135 : if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
5065 : : sorry_at (loc, "PDP11 bit-field handling unsupported"
5066 : : " in %qs", "__builtin_bit_cast");
5067 : 135 : else if (BYTES_BIG_ENDIAN)
5068 : : {
5069 : : /* Big endian. */
5070 : : if (bpos + fldsz <= BITS_PER_UNIT)
5071 : : *p &= ~(((1 << fldsz) - 1)
5072 : : << (BITS_PER_UNIT - bpos - fldsz));
5073 : : else
5074 : : {
5075 : : gcc_assert (bpos);
5076 : : *p &= ~(((1U << BITS_PER_UNIT) - 1) >> bpos);
5077 : : p++;
5078 : : fldsz -= BITS_PER_UNIT - bpos;
5079 : : gcc_assert (fldsz && fldsz < BITS_PER_UNIT);
5080 : : *p &= ((1U << BITS_PER_UNIT) - 1) >> fldsz;
5081 : : }
5082 : : }
5083 : : else
5084 : : {
5085 : : /* Little endian. */
5086 : 135 : if (bpos + fldsz <= BITS_PER_UNIT)
5087 : 135 : *p &= ~(((1 << fldsz) - 1) << bpos);
5088 : : else
5089 : : {
5090 : 0 : gcc_assert (bpos);
5091 : 0 : *p &= ~(((1 << BITS_PER_UNIT) - 1) << bpos);
5092 : 0 : p++;
5093 : 0 : fldsz -= BITS_PER_UNIT - bpos;
5094 : 0 : gcc_assert (fldsz && fldsz < BITS_PER_UNIT);
5095 : 0 : *p &= ~((1 << fldsz) - 1);
5096 : : }
5097 : : }
5098 : 135 : if (mask_save[0] != mask[pos]
5099 : 99 : || (end == 2 && mask_save[1] != mask[pos + 1]))
5100 : : {
5101 : 36 : CONSTRUCTOR_NO_CLEARING (t) = 1;
5102 : 36 : continue;
5103 : : }
5104 : : }
5105 : : }
5106 : 1329 : else if (is_byte_access_type_not_plain_char (type))
5107 : : {
5108 : 228 : HOST_WIDE_INT pos;
5109 : 228 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5110 : 132 : pos = tree_to_shwi (index);
5111 : : else
5112 : 96 : pos = int_byte_position (index);
5113 : 228 : if (mask[pos])
5114 : : {
5115 : 48 : CONSTRUCTOR_NO_CLEARING (t) = 1;
5116 : 48 : mask[pos] = 0;
5117 : 48 : continue;
5118 : : }
5119 : : }
5120 : 1515 : if (TREE_CODE (value) == CONSTRUCTOR)
5121 : : {
5122 : 264 : HOST_WIDE_INT pos;
5123 : 264 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5124 : 144 : pos = tree_to_shwi (index)
5125 : 72 : * tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))));
5126 : : else
5127 : 192 : pos = int_byte_position (index);
5128 : 264 : clear_uchar_or_std_byte_in_mask (loc, value, mask + pos);
5129 : : }
5130 : 1515 : if (i != j)
5131 : : {
5132 : 180 : CONSTRUCTOR_ELT (t, j)->index = index;
5133 : 180 : CONSTRUCTOR_ELT (t, j)->value = value;
5134 : : }
5135 : 1515 : ++j;
5136 : : }
5137 : 1318 : if (CONSTRUCTOR_NELTS (t) != j)
5138 : 84 : vec_safe_truncate (CONSTRUCTOR_ELTS (t), j);
5139 : : }
5140 : :
5141 : : /* Subroutine of cxx_eval_constant_expression.
5142 : : Attempt to evaluate a BIT_CAST_EXPR. */
5143 : :
5144 : : static tree
5145 : 1316 : cxx_eval_bit_cast (const constexpr_ctx *ctx, tree t, bool *non_constant_p,
5146 : : bool *overflow_p)
5147 : : {
5148 : 1316 : if (check_bit_cast_type (ctx, EXPR_LOCATION (t), TREE_TYPE (t),
5149 : 1316 : TREE_TYPE (t))
5150 : 4276 : || check_bit_cast_type (ctx, cp_expr_loc_or_loc (TREE_OPERAND (t, 0),
5151 : 1235 : EXPR_LOCATION (t)),
5152 : 1235 : TREE_TYPE (TREE_OPERAND (t, 0)),
5153 : 1235 : TREE_TYPE (TREE_OPERAND (t, 0))))
5154 : : {
5155 : 167 : *non_constant_p = true;
5156 : 167 : return t;
5157 : : }
5158 : :
5159 : 1149 : tree op = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), vc_prvalue,
5160 : : non_constant_p, overflow_p);
5161 : 1149 : if (*non_constant_p)
5162 : : return t;
5163 : :
5164 : 941 : location_t loc = EXPR_LOCATION (t);
5165 : 941 : if (BITS_PER_UNIT != 8 || CHAR_BIT != 8)
5166 : : {
5167 : : if (!ctx->quiet)
5168 : : sorry_at (loc, "%qs cannot be constant evaluated on the target",
5169 : : "__builtin_bit_cast");
5170 : : *non_constant_p = true;
5171 : : return t;
5172 : : }
5173 : :
5174 : 941 : if (!tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (t))))
5175 : : {
5176 : 0 : if (!ctx->quiet)
5177 : 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
5178 : : "type is too large", "__builtin_bit_cast");
5179 : 0 : *non_constant_p = true;
5180 : 0 : return t;
5181 : : }
5182 : :
5183 : 941 : HOST_WIDE_INT len = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (t)));
5184 : 941 : if (len < 0 || (int) len != len)
5185 : : {
5186 : 0 : if (!ctx->quiet)
5187 : 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
5188 : : "type is too large", "__builtin_bit_cast");
5189 : 0 : *non_constant_p = true;
5190 : 0 : return t;
5191 : : }
5192 : :
5193 : 941 : unsigned char buf[64];
5194 : 941 : unsigned char *ptr, *mask;
5195 : 941 : size_t alen = (size_t) len * 2;
5196 : 941 : if (alen <= sizeof (buf))
5197 : : ptr = buf;
5198 : : else
5199 : 3 : ptr = XNEWVEC (unsigned char, alen);
5200 : 941 : mask = ptr + (size_t) len;
5201 : : /* At the beginning consider everything indeterminate. */
5202 : 941 : memset (mask, ~0, (size_t) len);
5203 : :
5204 : 941 : if (native_encode_initializer (op, ptr, len, 0, mask) != len)
5205 : : {
5206 : 0 : if (!ctx->quiet)
5207 : 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
5208 : : "argument cannot be encoded", "__builtin_bit_cast");
5209 : 0 : *non_constant_p = true;
5210 : 0 : if (ptr != buf)
5211 : 0 : XDELETE (ptr);
5212 : 0 : return t;
5213 : : }
5214 : :
5215 : 941 : tree r = NULL_TREE;
5216 : 941 : if (can_native_interpret_type_p (TREE_TYPE (t)))
5217 : : {
5218 : 546 : r = native_interpret_expr (TREE_TYPE (t), ptr, len);
5219 : 546 : if (is_byte_access_type_not_plain_char (TREE_TYPE (t)))
5220 : : {
5221 : 46 : gcc_assert (len == 1);
5222 : 46 : if (mask[0])
5223 : : {
5224 : 24 : memset (mask, 0, len);
5225 : 24 : r = build_constructor (TREE_TYPE (r), NULL);
5226 : 24 : CONSTRUCTOR_NO_CLEARING (r) = 1;
5227 : : }
5228 : : }
5229 : : }
5230 : 395 : else if (TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE)
5231 : : {
5232 : 395 : r = native_interpret_aggregate (TREE_TYPE (t), ptr, 0, len);
5233 : 395 : if (r != NULL_TREE)
5234 : : {
5235 : 395 : clear_type_padding_in_mask (TREE_TYPE (t), mask);
5236 : 395 : clear_uchar_or_std_byte_in_mask (loc, r, mask);
5237 : 395 : if (CHECKING_P)
5238 : : {
5239 : 395 : tree e = cxx_eval_bare_aggregate (ctx, r, vc_prvalue,
5240 : : non_constant_p, overflow_p);
5241 : 395 : gcc_checking_assert (e == r);
5242 : : r = e;
5243 : : }
5244 : : }
5245 : : }
5246 : :
5247 : 941 : if (r != NULL_TREE)
5248 : : {
5249 : 7932 : for (int i = 0; i < len; i++)
5250 : 7081 : if (mask[i])
5251 : : {
5252 : 90 : if (!ctx->quiet)
5253 : 30 : error_at (loc, "%qs accessing uninitialized byte at offset %d",
5254 : : "__builtin_bit_cast", i);
5255 : 90 : *non_constant_p = true;
5256 : 90 : r = t;
5257 : 90 : break;
5258 : : }
5259 : 941 : if (ptr != buf)
5260 : 3 : XDELETE (ptr);
5261 : 941 : return r;
5262 : : }
5263 : :
5264 : 0 : if (!ctx->quiet)
5265 : 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
5266 : : "argument cannot be interpreted", "__builtin_bit_cast");
5267 : 0 : *non_constant_p = true;
5268 : 0 : if (ptr != buf)
5269 : 0 : XDELETE (ptr);
5270 : : return t;
5271 : : }
5272 : :
5273 : : /* Subroutine of cxx_eval_constant_expression.
5274 : : Evaluate a short-circuited logical expression T in the context
5275 : : of a given constexpr CALL. BAILOUT_VALUE is the value for
5276 : : early return. CONTINUE_VALUE is used here purely for
5277 : : sanity check purposes. */
5278 : :
5279 : : static tree
5280 : 9960238 : cxx_eval_logical_expression (const constexpr_ctx *ctx, tree t,
5281 : : tree bailout_value, tree continue_value,
5282 : : bool *non_constant_p, bool *overflow_p)
5283 : : {
5284 : 9960238 : tree r;
5285 : 9960238 : tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
5286 : : vc_prvalue, non_constant_p,
5287 : : overflow_p);
5288 : 9960238 : VERIFY_CONSTANT (lhs);
5289 : 7579124 : if (tree_int_cst_equal (lhs, bailout_value))
5290 : : return lhs;
5291 : 6983720 : gcc_assert (tree_int_cst_equal (lhs, continue_value));
5292 : 6983720 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
5293 : : vc_prvalue, non_constant_p,
5294 : : overflow_p);
5295 : 6983720 : VERIFY_CONSTANT (r);
5296 : : return r;
5297 : : }
5298 : :
5299 : : /* REF is a COMPONENT_REF designating a particular field. V is a vector of
5300 : : CONSTRUCTOR elements to initialize (part of) an object containing that
5301 : : field. Return a pointer to the constructor_elt corresponding to the
5302 : : initialization of the field. */
5303 : :
5304 : : static constructor_elt *
5305 : 0 : base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref)
5306 : : {
5307 : 0 : tree aggr = TREE_OPERAND (ref, 0);
5308 : 0 : tree field = TREE_OPERAND (ref, 1);
5309 : 0 : HOST_WIDE_INT i;
5310 : 0 : constructor_elt *ce;
5311 : :
5312 : 0 : gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
5313 : :
5314 : 0 : if (TREE_CODE (aggr) == COMPONENT_REF)
5315 : : {
5316 : 0 : constructor_elt *base_ce
5317 : 0 : = base_field_constructor_elt (v, aggr);
5318 : 0 : v = CONSTRUCTOR_ELTS (base_ce->value);
5319 : : }
5320 : :
5321 : 0 : for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
5322 : 0 : if (ce->index == field)
5323 : 0 : return ce;
5324 : :
5325 : 0 : gcc_unreachable ();
5326 : : return NULL;
5327 : : }
5328 : :
5329 : : /* Some of the expressions fed to the constexpr mechanism are calls to
5330 : : constructors, which have type void. In that case, return the type being
5331 : : initialized by the constructor. */
5332 : :
5333 : : static tree
5334 : 346081481 : initialized_type (tree t)
5335 : : {
5336 : 347443432 : if (TYPE_P (t))
5337 : : return t;
5338 : 347443119 : tree type = TREE_TYPE (t);
5339 : 347443119 : if (TREE_CODE (t) == CALL_EXPR)
5340 : : {
5341 : : /* A constructor call has void type, so we need to look deeper. */
5342 : 38105322 : tree fn = get_function_named_in_call (t);
5343 : 38105309 : if (fn && TREE_CODE (fn) == FUNCTION_DECL
5344 : 76173853 : && DECL_CXX_CONSTRUCTOR_P (fn))
5345 : 1674043 : type = DECL_CONTEXT (fn);
5346 : : }
5347 : 309337797 : else if (TREE_CODE (t) == COMPOUND_EXPR)
5348 : 1361951 : return initialized_type (TREE_OPERAND (t, 1));
5349 : 307975846 : else if (TREE_CODE (t) == AGGR_INIT_EXPR)
5350 : 447629 : type = TREE_TYPE (AGGR_INIT_EXPR_SLOT (t));
5351 : 346081168 : return cv_unqualified (type);
5352 : : }
5353 : :
5354 : : /* We're about to initialize element INDEX of an array or class from VALUE.
5355 : : Set up NEW_CTX appropriately by adjusting .object to refer to the
5356 : : subobject and creating a new CONSTRUCTOR if the element is itself
5357 : : a class or array. */
5358 : :
5359 : : static void
5360 : 1571139 : init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx,
5361 : : tree index, tree &value)
5362 : : {
5363 : 1571139 : new_ctx = *ctx;
5364 : :
5365 : 1571139 : if (index && TREE_CODE (index) != INTEGER_CST
5366 : 1460416 : && TREE_CODE (index) != FIELD_DECL
5367 : 3 : && TREE_CODE (index) != RANGE_EXPR)
5368 : : /* This won't have an element in the new CONSTRUCTOR. */
5369 : : return;
5370 : :
5371 : 1571139 : tree type = initialized_type (value);
5372 : 1571139 : if (!AGGREGATE_TYPE_P (type) && !VECTOR_TYPE_P (type))
5373 : : /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
5374 : : return;
5375 : 746023 : if (VECTOR_TYPE_P (type)
5376 : 1133 : && VECTOR_TYPE_P (TREE_TYPE (ctx->ctor))
5377 : 746059 : && index == NULL_TREE)
5378 : : /* A vector inside of a vector CONSTRUCTOR, e.g. when a larger
5379 : : vector is constructed from smaller vectors, doesn't get its own
5380 : : CONSTRUCTOR either. */
5381 : : return;
5382 : :
5383 : : /* The sub-aggregate initializer might contain a placeholder;
5384 : : update object to refer to the subobject and ctor to refer to
5385 : : the (newly created) sub-initializer. */
5386 : 745987 : if (ctx->object)
5387 : : {
5388 : 583147 : if (index == NULL_TREE || TREE_CODE (index) == RANGE_EXPR)
5389 : : /* There's no well-defined subobject for this index. */
5390 : 3 : new_ctx.object = NULL_TREE;
5391 : : else
5392 : 583144 : new_ctx.object = build_ctor_subob_ref (index, type, ctx->object);
5393 : : }
5394 : :
5395 : 745987 : if (is_empty_class (type))
5396 : : /* Leave ctor null for an empty subobject, they aren't represented in the
5397 : : result of evaluation. */
5398 : 710506 : new_ctx.ctor = NULL_TREE;
5399 : : else
5400 : : {
5401 : 35481 : tree elt = build_constructor (type, NULL);
5402 : 35481 : CONSTRUCTOR_NO_CLEARING (elt) = true;
5403 : 35481 : new_ctx.ctor = elt;
5404 : : }
5405 : :
5406 : 745987 : if (TREE_CODE (value) == TARGET_EXPR)
5407 : : /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
5408 : 25067 : value = TARGET_EXPR_INITIAL (value);
5409 : : }
5410 : :
5411 : : /* We're about to process an initializer for a class or array TYPE. Make
5412 : : sure that CTX is set up appropriately. */
5413 : :
5414 : : static void
5415 : 1202648 : verify_ctor_sanity (const constexpr_ctx *ctx, tree type)
5416 : : {
5417 : : /* We don't bother building a ctor for an empty base subobject. */
5418 : 1202648 : if (is_empty_class (type))
5419 : : return;
5420 : :
5421 : : /* We're in the middle of an initializer that might involve placeholders;
5422 : : our caller should have created a CONSTRUCTOR for us to put the
5423 : : initializer into. We will either return that constructor or T. */
5424 : 494281 : gcc_assert (ctx->ctor);
5425 : 494281 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
5426 : : (type, TREE_TYPE (ctx->ctor)));
5427 : : /* We used to check that ctx->ctor was empty, but that isn't the case when
5428 : : the object is zero-initialized before calling the constructor. */
5429 : 494281 : if (ctx->object)
5430 : : {
5431 : 378691 : tree otype = TREE_TYPE (ctx->object);
5432 : 378691 : gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, otype)
5433 : : /* Handle flexible array members. */
5434 : : || (TREE_CODE (otype) == ARRAY_TYPE
5435 : : && TYPE_DOMAIN (otype) == NULL_TREE
5436 : : && TREE_CODE (type) == ARRAY_TYPE
5437 : : && (same_type_ignoring_top_level_qualifiers_p
5438 : : (TREE_TYPE (type), TREE_TYPE (otype)))));
5439 : : }
5440 : 494281 : gcc_assert (!ctx->object || !DECL_P (ctx->object)
5441 : : || ctx->global->get_value (ctx->object) == ctx->ctor);
5442 : : }
5443 : :
5444 : : /* Subroutine of cxx_eval_constant_expression.
5445 : : The expression tree T denotes a C-style array or a C-style
5446 : : aggregate. Reduce it to a constant expression. */
5447 : :
5448 : : static tree
5449 : 1202022 : cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t,
5450 : : value_cat lval,
5451 : : bool *non_constant_p, bool *overflow_p)
5452 : : {
5453 : 1202022 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
5454 : 1202022 : bool changed = false;
5455 : 1202022 : gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t));
5456 : 1202022 : tree type = TREE_TYPE (t);
5457 : :
5458 : 1202022 : constexpr_ctx new_ctx;
5459 : 1202022 : if (TYPE_PTRMEMFUNC_P (type) || VECTOR_TYPE_P (type))
5460 : : {
5461 : : /* We don't really need the ctx->ctor business for a PMF or
5462 : : vector, but it's simpler to use the same code. */
5463 : 25213 : new_ctx = *ctx;
5464 : 25213 : new_ctx.ctor = build_constructor (type, NULL);
5465 : 25213 : new_ctx.object = NULL_TREE;
5466 : 25213 : ctx = &new_ctx;
5467 : 1202022 : };
5468 : 1202022 : verify_ctor_sanity (ctx, type);
5469 : 1202022 : vec<constructor_elt, va_gc> **p = nullptr;
5470 : 1202022 : if (ctx->ctor)
5471 : : {
5472 : 1202011 : p = &CONSTRUCTOR_ELTS (ctx->ctor);
5473 : 2403921 : vec_alloc (*p, vec_safe_length (v));
5474 : 1202011 : if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (t))
5475 : 16070 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor) = 1;
5476 : : }
5477 : :
5478 : 1202022 : unsigned i;
5479 : 1202022 : tree index, value;
5480 : 1202022 : bool constant_p = true;
5481 : 1202022 : bool side_effects_p = false;
5482 : 2500332 : FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value)
5483 : : {
5484 : 1565069 : tree orig_value = value;
5485 : 1565069 : init_subob_ctx (ctx, new_ctx, index, value);
5486 : : /* Like in cxx_eval_store_expression, omit entries for empty fields. */
5487 : 1565069 : bool no_slot = new_ctx.ctor == NULL_TREE;
5488 : 1565069 : int pos_hint = -1;
5489 : 1565069 : if (new_ctx.ctor != ctx->ctor && !no_slot)
5490 : : {
5491 : : /* If we built a new CONSTRUCTOR, attach it now so that other
5492 : : initializers can refer to it. */
5493 : 29571 : constructor_elt *cep = get_or_insert_ctor_field (ctx->ctor, index);
5494 : 29571 : cep->value = new_ctx.ctor;
5495 : 29571 : pos_hint = cep - (*p)->begin();
5496 : 29571 : }
5497 : 1535498 : else if (TREE_CODE (type) == UNION_TYPE)
5498 : : /* Otherwise if we're constructing a non-aggregate union member, set
5499 : : the active union member now so that we can later detect and diagnose
5500 : : if its initializer attempts to activate another member. */
5501 : 957 : get_or_insert_ctor_field (ctx->ctor, index);
5502 : 1565069 : tree elt = cxx_eval_constant_expression (&new_ctx, value,
5503 : : lval,
5504 : : non_constant_p, overflow_p);
5505 : : /* Don't VERIFY_CONSTANT here. */
5506 : 1565069 : if (ctx->quiet && *non_constant_p)
5507 : : break;
5508 : 1298310 : if (elt != orig_value)
5509 : 119152 : changed = true;
5510 : :
5511 : 1298310 : if (!TREE_CONSTANT (elt))
5512 : 412247 : constant_p = false;
5513 : 1298310 : if (TREE_SIDE_EFFECTS (elt))
5514 : 111 : side_effects_p = true;
5515 : 1298310 : if (index && TREE_CODE (index) == COMPONENT_REF)
5516 : : {
5517 : : /* This is an initialization of a vfield inside a base
5518 : : subaggregate that we already initialized; push this
5519 : : initialization into the previous initialization. */
5520 : 0 : constructor_elt *inner = base_field_constructor_elt (*p, index);
5521 : 0 : inner->value = elt;
5522 : 0 : changed = true;
5523 : 0 : }
5524 : 1298310 : else if (no_slot)
5525 : : /* This is an initializer for an empty field; now that we've
5526 : : checked that it's constant, we can ignore it. */
5527 : : changed = true;
5528 : 587899 : else if (index
5529 : 587771 : && (TREE_CODE (index) == NOP_EXPR
5530 : 587771 : || TREE_CODE (index) == POINTER_PLUS_EXPR))
5531 : : {
5532 : : /* Old representation of empty bases. FIXME remove. */
5533 : 0 : gcc_checking_assert (false);
5534 : : gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index))));
5535 : : changed = true;
5536 : : }
5537 : : else
5538 : : {
5539 : 587899 : if (TREE_CODE (type) == UNION_TYPE
5540 : 587899 : && (*p)->last().index != index)
5541 : : /* The initializer erroneously changed the active union member that
5542 : : we're initializing. */
5543 : 8 : gcc_assert (*non_constant_p);
5544 : : else
5545 : : {
5546 : : /* The initializer might have mutated the underlying CONSTRUCTOR,
5547 : : so recompute the location of the target constructer_elt. */
5548 : 587891 : constructor_elt *cep
5549 : 587891 : = get_or_insert_ctor_field (ctx->ctor, index, pos_hint);
5550 : 587891 : cep->value = elt;
5551 : : }
5552 : :
5553 : : /* Adding or replacing an element might change the ctor's flags. */
5554 : 587899 : TREE_CONSTANT (ctx->ctor) = constant_p;
5555 : 587899 : TREE_SIDE_EFFECTS (ctx->ctor) = side_effects_p;
5556 : : }
5557 : : }
5558 : 1202022 : if (*non_constant_p)
5559 : : return t;
5560 : 935230 : if (!changed)
5561 : : {
5562 : 151538 : if (VECTOR_TYPE_P (type))
5563 : 13936 : t = fold (t);
5564 : 151538 : return t;
5565 : : }
5566 : 783692 : t = ctx->ctor;
5567 : 783692 : if (!t)
5568 : 11 : t = build_constructor (type, NULL);
5569 : : /* We're done building this CONSTRUCTOR, so now we can interpret an
5570 : : element without an explicit initializer as value-initialized. */
5571 : 783692 : CONSTRUCTOR_NO_CLEARING (t) = false;
5572 : 783692 : TREE_CONSTANT (t) = constant_p;
5573 : 783692 : TREE_SIDE_EFFECTS (t) = side_effects_p;
5574 : 783692 : if (VECTOR_TYPE_P (type))
5575 : 1084 : t = fold (t);
5576 : : return t;
5577 : : }
5578 : :
5579 : : /* Subroutine of cxx_eval_constant_expression.
5580 : : The expression tree T is a VEC_INIT_EXPR which denotes the desired
5581 : : initialization of a non-static data member of array type. Reduce it to a
5582 : : CONSTRUCTOR.
5583 : :
5584 : : Note that apart from value-initialization (when VALUE_INIT is true),
5585 : : this is only intended to support value-initialization and the
5586 : : initializations done by defaulted constructors for classes with
5587 : : non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
5588 : : will either be NULL_TREE for the default constructor, or a COMPONENT_REF
5589 : : for the copy/move constructor. */
5590 : :
5591 : : static tree
5592 : 626 : cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init,
5593 : : bool value_init, value_cat lval,
5594 : : bool *non_constant_p, bool *overflow_p)
5595 : : {
5596 : 626 : tree elttype = TREE_TYPE (atype);
5597 : 626 : verify_ctor_sanity (ctx, atype);
5598 : 626 : vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
5599 : 626 : bool pre_init = false;
5600 : 626 : unsigned HOST_WIDE_INT i;
5601 : 626 : tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
5602 : :
5603 : 626 : if (init && TREE_CODE (init) == CONSTRUCTOR)
5604 : 0 : return cxx_eval_bare_aggregate (ctx, init, lval,
5605 : 0 : non_constant_p, overflow_p);
5606 : :
5607 : : /* For the default constructor, build up a call to the default
5608 : : constructor of the element type. We only need to handle class types
5609 : : here, as for a constructor to be constexpr, all members must be
5610 : : initialized, which for a defaulted default constructor means they must
5611 : : be of a class type with a constexpr default constructor. */
5612 : 626 : if (TREE_CODE (elttype) == ARRAY_TYPE)
5613 : : /* We only do this at the lowest level. */;
5614 : 583 : else if (value_init)
5615 : : {
5616 : 144 : init = build_value_init (elttype, complain);
5617 : 144 : pre_init = true;
5618 : : }
5619 : 439 : else if (!init)
5620 : : {
5621 : 375 : releasing_vec argvec;
5622 : 375 : init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
5623 : : &argvec, elttype, LOOKUP_NORMAL,
5624 : : complain);
5625 : 375 : init = build_aggr_init_expr (elttype, init);
5626 : 375 : pre_init = true;
5627 : 375 : }
5628 : :
5629 : 626 : bool zeroed_out = false;
5630 : 626 : if (!CONSTRUCTOR_NO_CLEARING (ctx->ctor))
5631 : : {
5632 : : /* We're initializing an array object that had been zero-initialized
5633 : : earlier. Truncate ctx->ctor, and propagate its zeroed state by
5634 : : clearing CONSTRUCTOR_NO_CLEARING on each of the aggregate element
5635 : : initializers we append to it. */
5636 : 156 : gcc_checking_assert (initializer_zerop (ctx->ctor));
5637 : 156 : zeroed_out = true;
5638 : 156 : vec_safe_truncate (*p, 0);
5639 : : }
5640 : :
5641 : 626 : tree nelts = get_array_or_vector_nelts (ctx, atype, non_constant_p,
5642 : : overflow_p);
5643 : 626 : unsigned HOST_WIDE_INT max = tree_to_uhwi (nelts);
5644 : 6279 : for (i = 0; i < max; ++i)
5645 : : {
5646 : 6070 : tree idx = build_int_cst (size_type_node, i);
5647 : 6070 : tree eltinit;
5648 : 6070 : bool reuse = false;
5649 : 6070 : constexpr_ctx new_ctx;
5650 : 6383 : init_subob_ctx (ctx, new_ctx, idx, pre_init ? init : elttype);
5651 : 6070 : bool no_slot = new_ctx.ctor == NULL_TREE;
5652 : 6070 : if (new_ctx.ctor != ctx->ctor && !no_slot)
5653 : : {
5654 : 5910 : if (zeroed_out)
5655 : 5445 : CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = false;
5656 : 5910 : CONSTRUCTOR_APPEND_ELT (*p, idx, new_ctx.ctor);
5657 : : }
5658 : 6070 : if (TREE_CODE (elttype) == ARRAY_TYPE)
5659 : : {
5660 : : /* A multidimensional array; recurse. */
5661 : 175 : if (value_init || init == NULL_TREE)
5662 : : {
5663 : 167 : eltinit = NULL_TREE;
5664 : 167 : reuse = i == 0;
5665 : : }
5666 : : else
5667 : 8 : eltinit = cp_build_array_ref (input_location, init, idx, complain);
5668 : 175 : eltinit = cxx_eval_vec_init_1 (&new_ctx, elttype, eltinit, value_init,
5669 : : lval,
5670 : : non_constant_p, overflow_p);
5671 : : }
5672 : 5895 : else if (pre_init)
5673 : : {
5674 : : /* Initializing an element using value or default initialization
5675 : : we just pre-built above. */
5676 : 5757 : if (init == void_node)
5677 : : /* Trivial default-init, don't do anything to the CONSTRUCTOR. */
5678 : 3 : return ctx->ctor;
5679 : 5754 : eltinit = init;
5680 : 5754 : if (CLASS_TYPE_P (elttype) && new_ctx.object)
5681 : : /* Clarify what object is being initialized (118285). */
5682 : 5628 : eltinit = build2 (INIT_EXPR, elttype, new_ctx.object, eltinit);
5683 : 5754 : eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval,
5684 : : non_constant_p, overflow_p);
5685 : 5754 : reuse = i == 0;
5686 : : }
5687 : : else
5688 : : {
5689 : : /* Copying an element. */
5690 : 138 : eltinit = cp_build_array_ref (input_location, init, idx, complain);
5691 : 138 : if (!lvalue_p (init))
5692 : 111 : eltinit = move (eltinit);
5693 : 138 : eltinit = (perform_implicit_conversion_flags
5694 : 138 : (elttype, eltinit, complain,
5695 : : LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING));
5696 : 138 : if (CLASS_TYPE_P (elttype) && new_ctx.object)
5697 : : /* Clarify what object is being initialized (118285). */
5698 : 121 : eltinit = build2 (INIT_EXPR, elttype, new_ctx.object, eltinit);
5699 : 138 : eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval,
5700 : : non_constant_p, overflow_p);
5701 : : }
5702 : 6067 : if (*non_constant_p)
5703 : : break;
5704 : 5990 : if (no_slot)
5705 : : {
5706 : : /* This is an initializer for an empty subobject; now that we've
5707 : : checked that it's constant, we can ignore it. */
5708 : 18 : gcc_checking_assert (i == 0);
5709 : : break;
5710 : : }
5711 : 5972 : else if (new_ctx.ctor != ctx->ctor)
5712 : : {
5713 : : /* We appended this element above; update the value. */
5714 : 5852 : gcc_assert ((*p)->last().index == idx);
5715 : 5852 : (*p)->last().value = eltinit;
5716 : : }
5717 : : else
5718 : 120 : CONSTRUCTOR_APPEND_ELT (*p, idx, eltinit);
5719 : : /* Reuse the result of cxx_eval_constant_expression call
5720 : : from the first iteration to all others if it is a constant
5721 : : initializer that doesn't require relocations. */
5722 : 5972 : if (reuse
5723 : 5972 : && max > 1
5724 : 5972 : && (eltinit == NULL_TREE
5725 : 475 : || (initializer_constant_valid_p (eltinit, TREE_TYPE (eltinit))
5726 : 475 : == null_pointer_node)))
5727 : : {
5728 : 319 : if (new_ctx.ctor != ctx->ctor)
5729 : 205 : eltinit = new_ctx.ctor;
5730 : 319 : tree range = build2 (RANGE_EXPR, size_type_node,
5731 : 319 : build_int_cst (size_type_node, 1),
5732 : 319 : build_int_cst (size_type_node, max - 1));
5733 : 319 : CONSTRUCTOR_APPEND_ELT (*p, range, unshare_constructor (eltinit));
5734 : 319 : break;
5735 : : }
5736 : 5653 : else if (i == 0)
5737 : 206 : vec_safe_reserve (*p, max);
5738 : : }
5739 : :
5740 : 623 : if (!*non_constant_p)
5741 : : {
5742 : 546 : init = ctx->ctor;
5743 : 546 : CONSTRUCTOR_NO_CLEARING (init) = false;
5744 : : }
5745 : 623 : return init;
5746 : : }
5747 : :
5748 : : static tree
5749 : 514 : cxx_eval_vec_init (const constexpr_ctx *ctx, tree t,
5750 : : value_cat lval,
5751 : : bool *non_constant_p, bool *overflow_p)
5752 : : {
5753 : 514 : tree atype = TREE_TYPE (t);
5754 : 514 : tree init = VEC_INIT_EXPR_INIT (t);
5755 : 514 : bool value_init = VEC_INIT_EXPR_VALUE_INIT (t);
5756 : 514 : if (!init || !BRACE_ENCLOSED_INITIALIZER_P (init))
5757 : : ;
5758 : 72 : else if (CONSTRUCTOR_NELTS (init) == 0
5759 : 72 : && !CP_AGGREGATE_TYPE_P (strip_array_types (atype)))
5760 : : {
5761 : : /* Handle {} as value-init. */
5762 : : init = NULL_TREE;
5763 : : value_init = true;
5764 : : }
5765 : : else
5766 : : {
5767 : : /* This is a more complicated case, like needing to loop over trailing
5768 : : elements; call build_vec_init and evaluate the result. */
5769 : 63 : tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
5770 : 63 : constexpr_ctx new_ctx = *ctx;
5771 : 63 : if (!ctx->object)
5772 : : {
5773 : : /* We want to have an initialization target for an VEC_INIT_EXPR.
5774 : : If we don't already have one in CTX, use the VEC_INIT_EXPR_SLOT. */
5775 : 51 : new_ctx.object = VEC_INIT_EXPR_SLOT (t);
5776 : 51 : tree ctor = new_ctx.ctor = build_constructor (atype, NULL);
5777 : 51 : CONSTRUCTOR_NO_CLEARING (ctor) = true;
5778 : 51 : ctx->global->put_value (new_ctx.object, ctor);
5779 : 51 : ctx = &new_ctx;
5780 : : }
5781 : 63 : init = expand_vec_init_expr (ctx->object, t, complain);
5782 : 63 : return cxx_eval_constant_expression (ctx, init, lval, non_constant_p,
5783 : : overflow_p);
5784 : : }
5785 : 451 : tree r = cxx_eval_vec_init_1 (ctx, atype, init, value_init,
5786 : : lval, non_constant_p, overflow_p);
5787 : 451 : if (*non_constant_p)
5788 : : return t;
5789 : : else
5790 : 393 : return r;
5791 : : }
5792 : :
5793 : : /* Like same_type_ignoring_top_level_qualifiers_p, but also handle the case
5794 : : where the desired type is an array of unknown bounds because the variable
5795 : : has had its bounds deduced since the wrapping expression was created. */
5796 : :
5797 : : static bool
5798 : 159080665 : same_type_ignoring_tlq_and_bounds_p (tree type1, tree type2)
5799 : : {
5800 : 159080665 : while (TREE_CODE (type1) == ARRAY_TYPE
5801 : 13322 : && TREE_CODE (type2) == ARRAY_TYPE
5802 : 159080669 : && (!TYPE_DOMAIN (type1) || !TYPE_DOMAIN (type2)))
5803 : : {
5804 : 2 : type1 = TREE_TYPE (type1);
5805 : 2 : type2 = TREE_TYPE (type2);
5806 : : }
5807 : 159080665 : return same_type_ignoring_top_level_qualifiers_p (type1, type2);
5808 : : }
5809 : :
5810 : : /* Try to determine the currently active union member for an expression
5811 : : with UNION_TYPE. If it can be determined, return the FIELD_DECL,
5812 : : otherwise return NULL_TREE. */
5813 : :
5814 : : static tree
5815 : 54 : cxx_union_active_member (const constexpr_ctx *ctx, tree t)
5816 : : {
5817 : 54 : constexpr_ctx new_ctx = *ctx;
5818 : 54 : new_ctx.quiet = true;
5819 : 54 : bool non_constant_p = false, overflow_p = false;
5820 : 54 : tree ctor = cxx_eval_constant_expression (&new_ctx, t, vc_prvalue,
5821 : : &non_constant_p,
5822 : : &overflow_p);
5823 : 54 : if (TREE_CODE (ctor) == CONSTRUCTOR
5824 : 24 : && CONSTRUCTOR_NELTS (ctor) == 1
5825 : 12 : && CONSTRUCTOR_ELT (ctor, 0)->index
5826 : 66 : && TREE_CODE (CONSTRUCTOR_ELT (ctor, 0)->index) == FIELD_DECL)
5827 : : return CONSTRUCTOR_ELT (ctor, 0)->index;
5828 : : return NULL_TREE;
5829 : : }
5830 : :
5831 : : /* Helper function for cxx_fold_indirect_ref_1, called recursively. */
5832 : :
5833 : : static tree
5834 : 4390960 : cxx_fold_indirect_ref_1 (const constexpr_ctx *ctx, location_t loc, tree type,
5835 : : tree op, unsigned HOST_WIDE_INT off, bool *empty_base)
5836 : : {
5837 : 5670599 : tree optype = TREE_TYPE (op);
5838 : 5670599 : unsigned HOST_WIDE_INT const_nunits;
5839 : 5670599 : if (off == 0 && similar_type_p (optype, type))
5840 : : return op;
5841 : 4389498 : else if (TREE_CODE (optype) == COMPLEX_TYPE
5842 : 4389498 : && similar_type_p (type, TREE_TYPE (optype)))
5843 : : {
5844 : : /* *(foo *)&complexfoo => __real__ complexfoo */
5845 : 0 : if (off == 0)
5846 : 0 : return build1_loc (loc, REALPART_EXPR, type, op);
5847 : : /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
5848 : 0 : else if (tree_to_uhwi (TYPE_SIZE_UNIT (type)) == off)
5849 : 0 : return build1_loc (loc, IMAGPART_EXPR, type, op);
5850 : : }
5851 : : /* ((foo*)&vectorfoo)[x] => BIT_FIELD_REF<vectorfoo,...> */
5852 : 4389498 : else if (VECTOR_TYPE_P (optype)
5853 : 0 : && similar_type_p (type, TREE_TYPE (optype))
5854 : 4389498 : && TYPE_VECTOR_SUBPARTS (optype).is_constant (&const_nunits))
5855 : : {
5856 : 0 : unsigned HOST_WIDE_INT part_width = tree_to_uhwi (TYPE_SIZE_UNIT (type));
5857 : 0 : unsigned HOST_WIDE_INT max_offset = part_width * const_nunits;
5858 : 0 : if (off < max_offset && off % part_width == 0)
5859 : : {
5860 : 0 : tree index = bitsize_int (off * BITS_PER_UNIT);
5861 : 0 : return build3_loc (loc, BIT_FIELD_REF, type, op,
5862 : 0 : TYPE_SIZE (type), index);
5863 : : }
5864 : : }
5865 : : /* ((foo *)&fooarray)[x] => fooarray[x] */
5866 : 4389498 : else if (TREE_CODE (optype) == ARRAY_TYPE
5867 : 1279639 : && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (optype)))
5868 : 5669137 : && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (optype))))
5869 : : {
5870 : 1279639 : tree type_domain = TYPE_DOMAIN (optype);
5871 : 1279639 : tree min_val = size_zero_node;
5872 : 1279639 : if (type_domain && TYPE_MIN_VALUE (type_domain))
5873 : 1279634 : min_val = TYPE_MIN_VALUE (type_domain);
5874 : 1279639 : unsigned HOST_WIDE_INT el_sz
5875 : 1279639 : = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (optype)));
5876 : 1279639 : unsigned HOST_WIDE_INT idx = off / el_sz;
5877 : 1279639 : unsigned HOST_WIDE_INT rem = off % el_sz;
5878 : 1279639 : if (tree_fits_uhwi_p (min_val))
5879 : : {
5880 : 1279639 : tree index = size_int (idx + tree_to_uhwi (min_val));
5881 : 1279639 : op = build4_loc (loc, ARRAY_REF, TREE_TYPE (optype), op, index,
5882 : : NULL_TREE, NULL_TREE);
5883 : 1279639 : return cxx_fold_indirect_ref_1 (ctx, loc, type, op, rem,
5884 : 1279639 : empty_base);
5885 : : }
5886 : : }
5887 : : /* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */
5888 : 3109859 : else if (TREE_CODE (optype) == RECORD_TYPE
5889 : 3109859 : || TREE_CODE (optype) == UNION_TYPE)
5890 : : {
5891 : 3106761 : if (TREE_CODE (optype) == UNION_TYPE)
5892 : : /* For unions prefer the currently active member. */
5893 : 54 : if (tree field = cxx_union_active_member (ctx, op))
5894 : : {
5895 : 12 : unsigned HOST_WIDE_INT el_sz
5896 : 12 : = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field)));
5897 : 12 : if (off < el_sz)
5898 : : {
5899 : 12 : tree cop = build3 (COMPONENT_REF, TREE_TYPE (field),
5900 : : op, field, NULL_TREE);
5901 : 12 : if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop,
5902 : : off, empty_base))
5903 : : return ret;
5904 : : }
5905 : : }
5906 : :
5907 : : /* Handle conversion to "as base" type. */
5908 : 3106755 : if (CLASS_TYPE_P (optype)
5909 : 6213434 : && CLASSTYPE_AS_BASE (optype) == type)
5910 : : return op;
5911 : :
5912 : : /* Handle conversion to an empty base class, which is represented with a
5913 : : NOP_EXPR. Do this before spelunking into the non-empty subobjects,
5914 : : which is likely to be a waste of time (109678). */
5915 : 3106226 : if (is_empty_class (type)
5916 : 3094143 : && CLASS_TYPE_P (optype)
5917 : 6200369 : && lookup_base (optype, type, ba_any, NULL, tf_none, off))
5918 : : {
5919 : 1745089 : if (empty_base)
5920 : 1745089 : *empty_base = true;
5921 : 1745089 : return op;
5922 : : }
5923 : :
5924 : 1361137 : for (tree field = TYPE_FIELDS (optype);
5925 : 17637637 : field; field = DECL_CHAIN (field))
5926 : 17633836 : if (TREE_CODE (field) == FIELD_DECL
5927 : 1361546 : && TREE_TYPE (field) != error_mark_node
5928 : 18995382 : && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (field))))
5929 : : {
5930 : 1361546 : tree pos = byte_position (field);
5931 : 1361546 : if (!tree_fits_uhwi_p (pos))
5932 : 0 : continue;
5933 : 1361546 : unsigned HOST_WIDE_INT upos = tree_to_uhwi (pos);
5934 : 1361546 : unsigned HOST_WIDE_INT el_sz
5935 : 1361546 : = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field)));
5936 : 1361546 : if (upos <= off && off < upos + el_sz)
5937 : : {
5938 : 1361040 : tree cop = build3 (COMPONENT_REF, TREE_TYPE (field),
5939 : : op, field, NULL_TREE);
5940 : 1361040 : if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop,
5941 : : off - upos,
5942 : : empty_base))
5943 : : return ret;
5944 : : }
5945 : : }
5946 : : }
5947 : :
5948 : : return NULL_TREE;
5949 : : }
5950 : :
5951 : : /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
5952 : : match. We want to be less strict for simple *& folding; if we have a
5953 : : non-const temporary that we access through a const pointer, that should
5954 : : work. We handle this here rather than change fold_indirect_ref_1
5955 : : because we're dealing with things like ADDR_EXPR of INTEGER_CST which
5956 : : don't really make sense outside of constant expression evaluation. Also
5957 : : we want to allow folding to COMPONENT_REF, which could cause trouble
5958 : : with TBAA in fold_indirect_ref_1. */
5959 : :
5960 : : static tree
5961 : 78806853 : cxx_fold_indirect_ref (const constexpr_ctx *ctx, location_t loc, tree type,
5962 : : tree op0, bool *empty_base /* = NULL*/)
5963 : : {
5964 : 78806853 : tree sub = op0;
5965 : 78806853 : tree subtype;
5966 : :
5967 : : /* STRIP_NOPS, but stop if REINTERPRET_CAST_P. */
5968 : 162051659 : while (CONVERT_EXPR_P (sub) || TREE_CODE (sub) == NON_LVALUE_EXPR
5969 : 217511721 : || TREE_CODE (sub) == VIEW_CONVERT_EXPR)
5970 : : {
5971 : 58546791 : if (TREE_CODE (sub) == NOP_EXPR
5972 : 58546791 : && REINTERPRET_CAST_P (sub))
5973 : : return NULL_TREE;
5974 : 58513400 : sub = TREE_OPERAND (sub, 0);
5975 : : }
5976 : :
5977 : 78773462 : subtype = TREE_TYPE (sub);
5978 : 78773462 : if (!INDIRECT_TYPE_P (subtype))
5979 : : return NULL_TREE;
5980 : :
5981 : : /* Canonicalizes the given OBJ/OFF pair by iteratively absorbing
5982 : : the innermost component into the offset until it would make the
5983 : : offset positive, so that cxx_fold_indirect_ref_1 can identify
5984 : : more folding opportunities. */
5985 : 81803251 : auto canonicalize_obj_off = [] (tree& obj, tree& off) {
5986 : 3029908 : while (TREE_CODE (obj) == COMPONENT_REF
5987 : : /* We need to preserve union member accesses so that we can
5988 : : later properly diagnose accessing the wrong member. */
5989 : 2562055 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (obj, 0))) == RECORD_TYPE
5990 : 7367818 : && (tree_int_cst_sign_bit (off) || integer_zerop (off)))
5991 : : {
5992 : 1868756 : tree field = TREE_OPERAND (obj, 1);
5993 : 1868756 : tree pos = byte_position (field);
5994 : 1868756 : if (integer_zerop (off) && integer_nonzerop (pos))
5995 : : /* If the offset is already 0, keep going as long as the
5996 : : component is at position 0. */
5997 : : break;
5998 : 1865910 : off = int_const_binop (PLUS_EXPR, off, pos);
5999 : 1865910 : obj = TREE_OPERAND (obj, 0);
6000 : : }
6001 : 3029908 : };
6002 : :
6003 : 78773343 : if (TREE_CODE (sub) == ADDR_EXPR)
6004 : : {
6005 : 24415674 : tree op = TREE_OPERAND (sub, 0);
6006 : 24415674 : tree optype = TREE_TYPE (op);
6007 : :
6008 : : /* *&CONST_DECL -> to the value of the const decl. */
6009 : 24415674 : if (TREE_CODE (op) == CONST_DECL)
6010 : 0 : return DECL_INITIAL (op);
6011 : : /* *&p => p; make sure to handle *&"str"[cst] here. */
6012 : 24415674 : if (similar_type_p (optype, type))
6013 : : {
6014 : 22455146 : tree fop = fold_read_from_constant_string (op);
6015 : 22455146 : if (fop)
6016 : : return fop;
6017 : : else
6018 : 22443152 : return op;
6019 : : }
6020 : : else
6021 : : {
6022 : 1960528 : tree off = integer_zero_node;
6023 : 1960528 : canonicalize_obj_off (op, off);
6024 : 1960528 : gcc_assert (integer_zerop (off));
6025 : 1960528 : return cxx_fold_indirect_ref_1 (ctx, loc, type, op, 0, empty_base);
6026 : : }
6027 : : }
6028 : 54357669 : else if (TREE_CODE (sub) == POINTER_PLUS_EXPR
6029 : 54357669 : && tree_fits_uhwi_p (TREE_OPERAND (sub, 1)))
6030 : : {
6031 : 1183327 : tree op00 = TREE_OPERAND (sub, 0);
6032 : 1183327 : tree off = TREE_OPERAND (sub, 1);
6033 : :
6034 : 1183327 : STRIP_NOPS (op00);
6035 : 1183327 : if (TREE_CODE (op00) == ADDR_EXPR)
6036 : : {
6037 : 1069380 : tree obj = TREE_OPERAND (op00, 0);
6038 : 1069380 : canonicalize_obj_off (obj, off);
6039 : 1069380 : return cxx_fold_indirect_ref_1 (ctx, loc, type, obj,
6040 : : tree_to_uhwi (off), empty_base);
6041 : : }
6042 : : }
6043 : : /* *(foo *)fooarrptr => (*fooarrptr)[0] */
6044 : 53174342 : else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
6045 : 53174342 : && similar_type_p (type, TREE_TYPE (TREE_TYPE (subtype))))
6046 : : {
6047 : 370 : tree type_domain;
6048 : 370 : tree min_val = size_zero_node;
6049 : 370 : tree newsub
6050 : 370 : = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (subtype), sub, NULL);
6051 : 370 : if (newsub)
6052 : : sub = newsub;
6053 : : else
6054 : 370 : sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub);
6055 : 370 : type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
6056 : 370 : if (type_domain && TYPE_MIN_VALUE (type_domain))
6057 : 370 : min_val = TYPE_MIN_VALUE (type_domain);
6058 : 370 : return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
6059 : 370 : NULL_TREE);
6060 : : }
6061 : :
6062 : : return NULL_TREE;
6063 : : }
6064 : :
6065 : : static tree
6066 : 37401005 : cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t,
6067 : : value_cat lval,
6068 : : bool *non_constant_p, bool *overflow_p)
6069 : : {
6070 : 37401005 : tree orig_op0 = TREE_OPERAND (t, 0);
6071 : 37401005 : bool empty_base = false;
6072 : :
6073 : : /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second
6074 : : operand is an integer-zero. Otherwise reject the MEM_REF for now. */
6075 : :
6076 : 37401005 : if (TREE_CODE (t) == MEM_REF
6077 : 37401005 : && (!TREE_OPERAND (t, 1) || !integer_zerop (TREE_OPERAND (t, 1))))
6078 : : {
6079 : 9 : gcc_assert (ctx->quiet);
6080 : 9 : *non_constant_p = true;
6081 : 9 : return t;
6082 : : }
6083 : :
6084 : : /* First try to simplify it directly. */
6085 : 37400996 : tree r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t),
6086 : : orig_op0, &empty_base);
6087 : 37400996 : if (!r)
6088 : : {
6089 : : /* If that didn't work, evaluate the operand first. */
6090 : 36117862 : tree op0 = cxx_eval_constant_expression (ctx, orig_op0,
6091 : : vc_prvalue, non_constant_p,
6092 : : overflow_p);
6093 : : /* Don't VERIFY_CONSTANT here. */
6094 : 36117862 : if (*non_constant_p)
6095 : : return t;
6096 : :
6097 : 34468356 : if (!lval && integer_zerop (op0))
6098 : : {
6099 : 71 : if (!ctx->quiet)
6100 : 12 : error ("dereferencing a null pointer");
6101 : 71 : *non_constant_p = true;
6102 : 71 : return t;
6103 : : }
6104 : :
6105 : 34468285 : r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t), op0,
6106 : : &empty_base);
6107 : 34468285 : if (r == NULL_TREE)
6108 : : {
6109 : : /* We couldn't fold to a constant value. Make sure it's not
6110 : : something we should have been able to fold. */
6111 : 16860199 : tree sub = op0;
6112 : 16860199 : STRIP_NOPS (sub);
6113 : 16860199 : if (TREE_CODE (sub) == ADDR_EXPR)
6114 : : {
6115 : 1502 : gcc_assert (!similar_type_p
6116 : : (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t)));
6117 : : /* DR 1188 says we don't have to deal with this. */
6118 : 1502 : if (!ctx->quiet)
6119 : 5 : error_at (cp_expr_loc_or_input_loc (t),
6120 : : "accessing value of %qE through a %qT glvalue in a "
6121 : : "constant expression", build_fold_indirect_ref (sub),
6122 : 4 : TREE_TYPE (t));
6123 : 1502 : *non_constant_p = true;
6124 : 1502 : return t;
6125 : : }
6126 : :
6127 : 16858697 : if (lval == vc_glvalue && op0 != orig_op0)
6128 : 1814393 : return build1 (INDIRECT_REF, TREE_TYPE (t), op0);
6129 : 15044304 : if (!lval)
6130 : 9063956 : VERIFY_CONSTANT (t);
6131 : 15044304 : return t;
6132 : : }
6133 : : }
6134 : :
6135 : 18891220 : r = cxx_eval_constant_expression (ctx, r,
6136 : : lval, non_constant_p, overflow_p);
6137 : 18891220 : if (*non_constant_p)
6138 : : return t;
6139 : :
6140 : : /* If we're pulling out the value of an empty base, just return an empty
6141 : : CONSTRUCTOR. */
6142 : 12810914 : if (empty_base && !lval)
6143 : : {
6144 : 14359 : r = build_constructor (TREE_TYPE (t), NULL);
6145 : 14359 : TREE_CONSTANT (r) = true;
6146 : : }
6147 : :
6148 : : return r;
6149 : : }
6150 : :
6151 : : /* Complain about R, a DECL that is accessed outside its lifetime. */
6152 : :
6153 : : static void
6154 : 34 : outside_lifetime_error (location_t loc, tree r)
6155 : : {
6156 : 34 : auto_diagnostic_group d;
6157 : 34 : if (DECL_NAME (r) == heap_deleted_identifier)
6158 : : {
6159 : : /* Provide a more accurate message for deleted variables. */
6160 : 6 : error_at (loc, "use of allocated storage after deallocation "
6161 : : "in a constant expression");
6162 : 6 : inform (DECL_SOURCE_LOCATION (r), "allocated here");
6163 : : }
6164 : : else
6165 : : {
6166 : 28 : error_at (loc, "accessing %qE outside its lifetime", r);
6167 : 28 : inform (DECL_SOURCE_LOCATION (r), "declared here");
6168 : : }
6169 : 34 : }
6170 : :
6171 : : /* Complain about R, a VAR_DECL, not being usable in a constant expression.
6172 : : FUNDEF_P is true if we're checking a constexpr function body.
6173 : : Shared between potential_constant_expression and
6174 : : cxx_eval_constant_expression. */
6175 : :
6176 : : static void
6177 : 294 : non_const_var_error (location_t loc, tree r, bool fundef_p)
6178 : : {
6179 : 294 : auto_diagnostic_group d;
6180 : 294 : tree type = TREE_TYPE (r);
6181 : 294 : if (DECL_NAME (r) == heap_uninit_identifier
6182 : 294 : || DECL_NAME (r) == heap_identifier
6183 : 291 : || DECL_NAME (r) == heap_vec_uninit_identifier
6184 : 585 : || DECL_NAME (r) == heap_vec_identifier)
6185 : : {
6186 : 3 : if (constexpr_error (loc, fundef_p, "the content of uninitialized "
6187 : : "storage is not usable in a constant expression"))
6188 : 3 : inform (DECL_SOURCE_LOCATION (r), "allocated here");
6189 : 3 : return;
6190 : : }
6191 : 291 : if (DECL_NAME (r) == heap_deleted_identifier)
6192 : : {
6193 : 0 : if (constexpr_error (loc, fundef_p, "use of allocated storage after "
6194 : : "deallocation in a constant expression"))
6195 : 0 : inform (DECL_SOURCE_LOCATION (r), "allocated here");
6196 : 0 : return;
6197 : : }
6198 : 291 : if (!constexpr_error (loc, fundef_p, "the value of %qD is not usable in "
6199 : : "a constant expression", r))
6200 : : return;
6201 : : /* Avoid error cascade. */
6202 : 290 : if (DECL_INITIAL (r) == error_mark_node)
6203 : : return;
6204 : 276 : if (DECL_DECLARED_CONSTEXPR_P (r))
6205 : 3 : inform (DECL_SOURCE_LOCATION (r),
6206 : : "%qD used in its own initializer", r);
6207 : 273 : else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6208 : : {
6209 : 216 : if (!CP_TYPE_CONST_P (type))
6210 : 187 : inform (DECL_SOURCE_LOCATION (r),
6211 : : "%q#D is not const", r);
6212 : 29 : else if (CP_TYPE_VOLATILE_P (type))
6213 : 0 : inform (DECL_SOURCE_LOCATION (r),
6214 : : "%q#D is volatile", r);
6215 : 29 : else if (!DECL_INITIAL (r)
6216 : 9 : || !TREE_CONSTANT (DECL_INITIAL (r))
6217 : 37 : || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r))
6218 : 29 : inform (DECL_SOURCE_LOCATION (r),
6219 : : "%qD was not initialized with a constant "
6220 : : "expression", r);
6221 : : else
6222 : 0 : gcc_unreachable ();
6223 : : }
6224 : 57 : else if (TYPE_REF_P (type))
6225 : 9 : inform (DECL_SOURCE_LOCATION (r),
6226 : : "%qD was not initialized with a constant "
6227 : : "expression", r);
6228 : : else
6229 : : {
6230 : 48 : if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r))
6231 : 48 : inform (DECL_SOURCE_LOCATION (r),
6232 : : "%qD was not declared %<constexpr%>", r);
6233 : : else
6234 : 0 : inform (DECL_SOURCE_LOCATION (r),
6235 : : "%qD does not have integral or enumeration type",
6236 : : r);
6237 : : }
6238 : 294 : }
6239 : :
6240 : : /* Subroutine of cxx_eval_constant_expression.
6241 : : Like cxx_eval_unary_expression, except for trinary expressions. */
6242 : :
6243 : : static tree
6244 : 16 : cxx_eval_trinary_expression (const constexpr_ctx *ctx, tree t,
6245 : : value_cat lval,
6246 : : bool *non_constant_p, bool *overflow_p)
6247 : : {
6248 : 16 : int i;
6249 : 16 : tree args[3];
6250 : 16 : tree val;
6251 : :
6252 : 37 : for (i = 0; i < 3; i++)
6253 : : {
6254 : 30 : args[i] = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, i),
6255 : : lval,
6256 : : non_constant_p, overflow_p);
6257 : 30 : VERIFY_CONSTANT (args[i]);
6258 : : }
6259 : :
6260 : 7 : val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t),
6261 : : args[0], args[1], args[2]);
6262 : 7 : if (val == NULL_TREE)
6263 : : return t;
6264 : 7 : VERIFY_CONSTANT (val);
6265 : : return val;
6266 : : }
6267 : :
6268 : : /* True if T was declared in a function declared to be constexpr, and
6269 : : therefore potentially constant in C++14. */
6270 : :
6271 : : bool
6272 : 75140116 : var_in_constexpr_fn (tree t)
6273 : : {
6274 : 75140116 : tree ctx = DECL_CONTEXT (t);
6275 : 75140116 : return (ctx && TREE_CODE (ctx) == FUNCTION_DECL
6276 : 142972660 : && DECL_DECLARED_CONSTEXPR_P (ctx));
6277 : : }
6278 : :
6279 : : /* True if a function might be constexpr: either a function that was
6280 : : declared constexpr, or a C++17 lambda op(). */
6281 : :
6282 : : bool
6283 : 363516200 : maybe_constexpr_fn (tree t)
6284 : : {
6285 : 363516200 : return (DECL_DECLARED_CONSTEXPR_P (t)
6286 : 144071952 : || (cxx_dialect >= cxx17 && LAMBDA_FUNCTION_P (t))
6287 : 506064900 : || (flag_implicit_constexpr
6288 : 65 : && DECL_DECLARED_INLINE_P (STRIP_TEMPLATE (t))));
6289 : : }
6290 : :
6291 : : /* True if T was declared in a function that might be constexpr: either a
6292 : : function that was declared constexpr, or a C++17 lambda op(). */
6293 : :
6294 : : bool
6295 : 61031613 : var_in_maybe_constexpr_fn (tree t)
6296 : : {
6297 : 122062646 : return (DECL_FUNCTION_SCOPE_P (t)
6298 : 116120952 : && maybe_constexpr_fn (DECL_CONTEXT (t)));
6299 : : }
6300 : :
6301 : : /* We're assigning INIT to TARGET. In do_build_copy_constructor and
6302 : : build_over_call we implement trivial copy of a class with tail padding using
6303 : : assignment of character arrays, which is valid in normal code, but not in
6304 : : constexpr evaluation. We don't need to worry about clobbering tail padding
6305 : : in constexpr evaluation, so strip the type punning. */
6306 : :
6307 : : static void
6308 : 38744248 : maybe_simplify_trivial_copy (tree &target, tree &init)
6309 : : {
6310 : 38744248 : if (TREE_CODE (target) == MEM_REF
6311 : 880 : && TREE_CODE (init) == MEM_REF
6312 : 880 : && TREE_TYPE (target) == TREE_TYPE (init)
6313 : 880 : && TREE_CODE (TREE_TYPE (target)) == ARRAY_TYPE
6314 : 38745128 : && TREE_TYPE (TREE_TYPE (target)) == unsigned_char_type_node)
6315 : : {
6316 : 880 : target = build_fold_indirect_ref (TREE_OPERAND (target, 0));
6317 : 880 : init = build_fold_indirect_ref (TREE_OPERAND (init, 0));
6318 : : }
6319 : 38744248 : }
6320 : :
6321 : : /* Returns true if REF, which is a COMPONENT_REF, has any fields
6322 : : of constant type. This does not check for 'mutable', so the
6323 : : caller is expected to be mindful of that. */
6324 : :
6325 : : static bool
6326 : 277 : cref_has_const_field (tree ref)
6327 : : {
6328 : 346 : while (TREE_CODE (ref) == COMPONENT_REF)
6329 : : {
6330 : 337 : if (CP_TYPE_CONST_P (TREE_TYPE (TREE_OPERAND (ref, 1))))
6331 : : return true;
6332 : 69 : ref = TREE_OPERAND (ref, 0);
6333 : : }
6334 : : return false;
6335 : : }
6336 : :
6337 : : /* Return true if we are modifying something that is const during constant
6338 : : expression evaluation. CODE is the code of the statement, OBJ is the
6339 : : object in question, MUTABLE_P is true if one of the subobjects were
6340 : : declared mutable. */
6341 : :
6342 : : static bool
6343 : 31951644 : modifying_const_object_p (tree_code code, tree obj, bool mutable_p)
6344 : : {
6345 : : /* If this is initialization, there's no problem. */
6346 : 31951644 : if (code != MODIFY_EXPR)
6347 : : return false;
6348 : :
6349 : : /* [basic.type.qualifier] "A const object is an object of type
6350 : : const T or a non-mutable subobject of a const object." */
6351 : 6844365 : if (mutable_p)
6352 : : return false;
6353 : :
6354 : 6843275 : if (TREE_READONLY (obj))
6355 : : return true;
6356 : :
6357 : 6841595 : if (CP_TYPE_CONST_P (TREE_TYPE (obj)))
6358 : : {
6359 : : /* Although a COMPONENT_REF may have a const type, we should
6360 : : only consider it modifying a const object when any of the
6361 : : field components is const. This can happen when using
6362 : : constructs such as const_cast<const T &>(m), making something
6363 : : const even though it wasn't declared const. */
6364 : 3011 : if (TREE_CODE (obj) == COMPONENT_REF)
6365 : 277 : return cref_has_const_field (obj);
6366 : : else
6367 : : return true;
6368 : : }
6369 : :
6370 : : return false;
6371 : : }
6372 : :
6373 : : /* Evaluate an INIT_EXPR or MODIFY_EXPR. */
6374 : :
6375 : : static tree
6376 : 42651179 : cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
6377 : : value_cat lval,
6378 : : bool *non_constant_p, bool *overflow_p)
6379 : : {
6380 : 42651179 : constexpr_ctx new_ctx = *ctx;
6381 : :
6382 : 42651179 : tree init = TREE_OPERAND (t, 1);
6383 : :
6384 : 42651179 : if (TREE_CLOBBER_P (init)
6385 : 42651179 : && CLOBBER_KIND (init) < CLOBBER_OBJECT_END)
6386 : : /* Only handle clobbers ending the lifetime of objects. */
6387 : 3906931 : return void_node;
6388 : :
6389 : : /* First we figure out where we're storing to. */
6390 : 38744248 : tree target = TREE_OPERAND (t, 0);
6391 : :
6392 : 38744248 : maybe_simplify_trivial_copy (target, init);
6393 : :
6394 : 38744248 : tree type = TREE_TYPE (target);
6395 : 38744248 : bool preeval = SCALAR_TYPE_P (type) || TREE_CODE (t) == MODIFY_EXPR;
6396 : 27876977 : if (preeval && !TREE_CLOBBER_P (init))
6397 : : {
6398 : : /* Evaluate the value to be stored without knowing what object it will be
6399 : : stored in, so that any side-effects happen first. */
6400 : 27855562 : if (!SCALAR_TYPE_P (type))
6401 : 13621 : new_ctx.ctor = new_ctx.object = NULL_TREE;
6402 : 27855562 : init = cxx_eval_constant_expression (&new_ctx, init, vc_prvalue,
6403 : : non_constant_p, overflow_p);
6404 : 27855562 : if (*non_constant_p)
6405 : : return t;
6406 : : }
6407 : :
6408 : 28704500 : bool evaluated = false;
6409 : 28704500 : if (lval == vc_glvalue)
6410 : : {
6411 : : /* If we want to return a reference to the target, we need to evaluate it
6412 : : as a whole; otherwise, only evaluate the innermost piece to avoid
6413 : : building up unnecessary *_REFs. */
6414 : 0 : target = cxx_eval_constant_expression (ctx, target, lval,
6415 : : non_constant_p, overflow_p);
6416 : 0 : evaluated = true;
6417 : 0 : if (*non_constant_p)
6418 : : return t;
6419 : : }
6420 : :
6421 : : /* Find the underlying variable. */
6422 : 28704500 : releasing_vec refs;
6423 : 28704500 : tree object = NULL_TREE;
6424 : : /* If we're modifying a const object, save it. */
6425 : 28704500 : tree const_object_being_modified = NULL_TREE;
6426 : 28704500 : bool mutable_p = false;
6427 : 95288020 : for (tree probe = target; object == NULL_TREE; )
6428 : : {
6429 : 66583732 : switch (TREE_CODE (probe))
6430 : : {
6431 : 9174921 : case BIT_FIELD_REF:
6432 : 9174921 : case COMPONENT_REF:
6433 : 9174921 : case ARRAY_REF:
6434 : 9174921 : {
6435 : 9174921 : tree ob = TREE_OPERAND (probe, 0);
6436 : 9174921 : tree elt = TREE_OPERAND (probe, 1);
6437 : 9174921 : if (TREE_CODE (elt) == FIELD_DECL && DECL_MUTABLE_P (elt))
6438 : : mutable_p = true;
6439 : 9174921 : if (TREE_CODE (probe) == ARRAY_REF)
6440 : : {
6441 : 1026629 : elt = eval_and_check_array_index (ctx, probe, false,
6442 : : non_constant_p, overflow_p);
6443 : 1026629 : if (*non_constant_p)
6444 : 54 : return t;
6445 : : }
6446 : : /* We don't check modifying_const_object_p for ARRAY_REFs. Given
6447 : : "int a[10]", an ARRAY_REF "a[2]" can be "const int", even though
6448 : : the array isn't const. Instead, check "a" in the next iteration;
6449 : : that will detect modifying "const int a[10]". */
6450 : 8148292 : else if (evaluated
6451 : 3247356 : && modifying_const_object_p (TREE_CODE (t), probe,
6452 : : mutable_p)
6453 : 8148560 : && const_object_being_modified == NULL_TREE)
6454 : : const_object_being_modified = probe;
6455 : :
6456 : : /* Track named member accesses for unions to validate modifications
6457 : : that change active member. */
6458 : 9174867 : if (!evaluated && TREE_CODE (probe) == COMPONENT_REF)
6459 : 4900936 : vec_safe_push (refs, probe);
6460 : : else
6461 : 4273931 : vec_safe_push (refs, NULL_TREE);
6462 : :
6463 : 9174867 : vec_safe_push (refs, elt);
6464 : 9174867 : vec_safe_push (refs, TREE_TYPE (probe));
6465 : 9174867 : probe = ob;
6466 : : }
6467 : 9174867 : break;
6468 : :
6469 : 18 : case REALPART_EXPR:
6470 : 18 : gcc_assert (refs->is_empty ());
6471 : 18 : vec_safe_push (refs, NULL_TREE);
6472 : 18 : vec_safe_push (refs, probe);
6473 : 18 : vec_safe_push (refs, TREE_TYPE (probe));
6474 : 18 : probe = TREE_OPERAND (probe, 0);
6475 : 18 : break;
6476 : :
6477 : 20 : case IMAGPART_EXPR:
6478 : 20 : gcc_assert (refs->is_empty ());
6479 : 20 : vec_safe_push (refs, NULL_TREE);
6480 : 20 : vec_safe_push (refs, probe);
6481 : 20 : vec_safe_push (refs, TREE_TYPE (probe));
6482 : 20 : probe = TREE_OPERAND (probe, 0);
6483 : 20 : break;
6484 : :
6485 : 57408773 : default:
6486 : 57408773 : if (evaluated)
6487 : : object = probe;
6488 : : else
6489 : : {
6490 : 28704485 : tree pvar = tree_strip_any_location_wrapper (probe);
6491 : 28704485 : if (VAR_P (pvar) && DECL_ANON_UNION_VAR_P (pvar))
6492 : : {
6493 : : /* Stores to DECL_ANON_UNION_VAR_P var are allowed to change
6494 : : active union member. */
6495 : 18 : probe = DECL_VALUE_EXPR (pvar);
6496 : 18 : break;
6497 : : }
6498 : 28704467 : probe = cxx_eval_constant_expression (ctx, probe, vc_glvalue,
6499 : : non_constant_p, overflow_p);
6500 : 28704467 : evaluated = true;
6501 : 28704467 : if (*non_constant_p)
6502 : : return t;
6503 : : }
6504 : : break;
6505 : : }
6506 : : }
6507 : :
6508 : 28704288 : if (modifying_const_object_p (TREE_CODE (t), object, mutable_p)
6509 : 28704288 : && const_object_being_modified == NULL_TREE)
6510 : 28704288 : const_object_being_modified = object;
6511 : :
6512 : 28704288 : if (DECL_P (object)
6513 : 27510308 : && TREE_CLOBBER_P (init)
6514 : 28725703 : && DECL_NAME (object) == heap_deleted_identifier)
6515 : : /* Ignore clobbers of deleted allocations for now; we'll get a better error
6516 : : message later when operator delete is called. */
6517 : 15 : return void_node;
6518 : :
6519 : : /* And then find/build up our initializer for the path to the subobject
6520 : : we're initializing. */
6521 : 28704273 : tree *valp;
6522 : 28704273 : if (DECL_P (object))
6523 : 27510293 : valp = ctx->global->get_value_ptr (object, TREE_CODE (t) == INIT_EXPR);
6524 : : else
6525 : 1193980 : valp = NULL;
6526 : 28704273 : if (!valp)
6527 : : {
6528 : : /* A constant-expression cannot modify objects from outside the
6529 : : constant-expression. */
6530 : 1197002 : if (!ctx->quiet)
6531 : : {
6532 : 22 : auto_diagnostic_group d;
6533 : 22 : if (DECL_P (object) && DECL_NAME (object) == heap_deleted_identifier)
6534 : : {
6535 : 0 : error ("modification of allocated storage after deallocation "
6536 : : "is not a constant expression");
6537 : 0 : inform (DECL_SOURCE_LOCATION (object), "allocated here");
6538 : : }
6539 : 22 : else if (DECL_P (object) && ctx->global->is_outside_lifetime (object))
6540 : : {
6541 : 10 : if (TREE_CLOBBER_P (init))
6542 : 3 : error ("destroying %qE outside its lifetime", object);
6543 : : else
6544 : 7 : error ("modification of %qE outside its lifetime "
6545 : : "is not a constant expression", object);
6546 : 10 : inform (DECL_SOURCE_LOCATION (object), "declared here");
6547 : : }
6548 : : else
6549 : : {
6550 : 12 : if (TREE_CLOBBER_P (init))
6551 : 6 : error ("destroying %qE from outside current evaluation "
6552 : : "is not a constant expression", object);
6553 : : else
6554 : 6 : error ("modification of %qE from outside current evaluation "
6555 : : "is not a constant expression", object);
6556 : : }
6557 : 22 : }
6558 : 1197002 : *non_constant_p = true;
6559 : 1197002 : return t;
6560 : : }
6561 : :
6562 : : /* Handle explicit end-of-lifetime. */
6563 : 27507271 : if (TREE_CLOBBER_P (init))
6564 : : {
6565 : 21370 : if (refs->is_empty ())
6566 : 9135 : ctx->global->destroy_value (object);
6567 : 21370 : return void_node;
6568 : : }
6569 : :
6570 : 27485901 : type = TREE_TYPE (object);
6571 : 27485901 : bool no_zero_init = true;
6572 : 27485901 : bool zero_padding_bits = false;
6573 : :
6574 : 54971802 : auto_vec<tree *> ctors;
6575 : 54971802 : releasing_vec indexes;
6576 : 54971802 : auto_vec<int> index_pos_hints;
6577 : 27485901 : bool activated_union_member_p = false;
6578 : 27485901 : bool empty_base = false;
6579 : 34467361 : while (!refs->is_empty ())
6580 : : {
6581 : 6989863 : if (*valp == NULL_TREE)
6582 : : {
6583 : 886893 : *valp = build_constructor (type, NULL);
6584 : 886893 : CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
6585 : 886893 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
6586 : : }
6587 : 6102970 : else if (STRIP_ANY_LOCATION_WRAPPER (*valp),
6588 : 6102970 : TREE_CODE (*valp) == STRING_CST)
6589 : : {
6590 : : /* An array was initialized with a string constant, and now
6591 : : we're writing into one of its elements. Explode the
6592 : : single initialization into a set of element
6593 : : initializations. */
6594 : 2656 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
6595 : :
6596 : 2656 : tree string = *valp;
6597 : 2656 : tree elt_type = TREE_TYPE (type);
6598 : 2656 : unsigned chars_per_elt = (TYPE_PRECISION (elt_type)
6599 : 2656 : / TYPE_PRECISION (char_type_node));
6600 : 2656 : unsigned num_elts = TREE_STRING_LENGTH (string) / chars_per_elt;
6601 : 2656 : tree ary_ctor = build_constructor (type, NULL);
6602 : :
6603 : 2656 : vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor), num_elts);
6604 : 5467 : for (unsigned ix = 0; ix != num_elts; ix++)
6605 : : {
6606 : 2811 : constructor_elt elt =
6607 : : {
6608 : 2811 : build_int_cst (size_type_node, ix),
6609 : 2811 : extract_string_elt (string, chars_per_elt, ix)
6610 : 2811 : };
6611 : 2811 : CONSTRUCTOR_ELTS (ary_ctor)->quick_push (elt);
6612 : : }
6613 : :
6614 : 2656 : *valp = ary_ctor;
6615 : : }
6616 : :
6617 : 6989863 : enum tree_code code = TREE_CODE (type);
6618 : 6989863 : tree reftype = refs->pop();
6619 : 6989863 : tree index = refs->pop();
6620 : 6989863 : bool is_access_expr = refs->pop() != NULL_TREE;
6621 : :
6622 : 6989863 : if (code == COMPLEX_TYPE)
6623 : : {
6624 : 38 : if (TREE_CODE (*valp) == COMPLEX_CST)
6625 : 36 : *valp = build2 (COMPLEX_EXPR, type, TREE_REALPART (*valp),
6626 : 36 : TREE_IMAGPART (*valp));
6627 : 2 : else if (TREE_CODE (*valp) == CONSTRUCTOR
6628 : 1 : && CONSTRUCTOR_NELTS (*valp) == 0
6629 : 3 : && CONSTRUCTOR_NO_CLEARING (*valp))
6630 : : {
6631 : 1 : tree r = build_constructor (reftype, NULL);
6632 : 1 : CONSTRUCTOR_NO_CLEARING (r) = 1;
6633 : 1 : *valp = build2 (COMPLEX_EXPR, type, r, r);
6634 : : }
6635 : 38 : gcc_assert (TREE_CODE (*valp) == COMPLEX_EXPR);
6636 : 38 : ctors.safe_push (valp);
6637 : 38 : vec_safe_push (indexes, index);
6638 : 38 : valp = &TREE_OPERAND (*valp, TREE_CODE (index) == IMAGPART_EXPR);
6639 : 38 : gcc_checking_assert (refs->is_empty ());
6640 : : type = reftype;
6641 : 8365 : break;
6642 : : }
6643 : :
6644 : : /* If the value of object is already zero-initialized, any new ctors for
6645 : : subobjects will also be zero-initialized. Similarly with zeroing of
6646 : : padding bits. */
6647 : 6989825 : no_zero_init = CONSTRUCTOR_NO_CLEARING (*valp);
6648 : 6989825 : zero_padding_bits = CONSTRUCTOR_ZERO_PADDING_BITS (*valp);
6649 : :
6650 : 6989825 : if (code == RECORD_TYPE && is_empty_field (index))
6651 : : /* Don't build a sub-CONSTRUCTOR for an empty base or field, as they
6652 : : have no data and might have an offset lower than previously declared
6653 : : fields, which confuses the middle-end. The code below will notice
6654 : : that we don't have a CONSTRUCTOR for our inner target and just
6655 : : return init. */
6656 : : {
6657 : : empty_base = true;
6658 : : break;
6659 : : }
6660 : :
6661 : : /* If a union is zero-initialized, its first non-static named data member
6662 : : is zero-initialized (and therefore active). */
6663 : 6981460 : if (code == UNION_TYPE
6664 : 6981460 : && !no_zero_init
6665 : 6981460 : && CONSTRUCTOR_NELTS (*valp) == 0)
6666 : 2306 : if (tree first = next_aggregate_field (TYPE_FIELDS (type)))
6667 : 2306 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (*valp), first, NULL_TREE);
6668 : :
6669 : : /* Check for implicit change of active member for a union. */
6670 : 6981460 : if (code == UNION_TYPE
6671 : 157444 : && (CONSTRUCTOR_NELTS (*valp) == 0
6672 : 43077 : || CONSTRUCTOR_ELT (*valp, 0)->index != index)
6673 : : /* An INIT_EXPR of the last member in an access chain is always OK,
6674 : : but still check implicit change of members earlier on; see
6675 : : cpp2a/constexpr-union6.C. */
6676 : 7099560 : && !(TREE_CODE (t) == INIT_EXPR && refs->is_empty ()))
6677 : : {
6678 : 83538 : bool has_active_member = CONSTRUCTOR_NELTS (*valp) != 0;
6679 : 83538 : tree inner = strip_array_types (reftype);
6680 : :
6681 : 83538 : if (has_active_member && cxx_dialect < cxx20)
6682 : : {
6683 : 68 : if (!ctx->quiet)
6684 : 22 : error_at (cp_expr_loc_or_input_loc (t),
6685 : : "change of the active member of a union "
6686 : : "from %qD to %qD is not a constant expression "
6687 : : "before C++20",
6688 : 22 : CONSTRUCTOR_ELT (*valp, 0)->index,
6689 : : index);
6690 : 68 : *non_constant_p = true;
6691 : : }
6692 : 83470 : else if (!is_access_expr
6693 : 83470 : || (TREE_CODE (t) == MODIFY_EXPR
6694 : 1133 : && CLASS_TYPE_P (inner)
6695 : 18 : && !type_has_non_deleted_trivial_default_ctor (inner)))
6696 : : {
6697 : : /* Diagnose changing active union member after initialization
6698 : : without a valid member access expression, as described in
6699 : : [class.union.general] p5. */
6700 : 82334 : if (!ctx->quiet)
6701 : : {
6702 : 36 : auto_diagnostic_group d;
6703 : 36 : if (has_active_member)
6704 : 24 : error_at (cp_expr_loc_or_input_loc (t),
6705 : : "accessing %qD member instead of initialized "
6706 : : "%qD member in constant expression",
6707 : 24 : index, CONSTRUCTOR_ELT (*valp, 0)->index);
6708 : : else
6709 : 12 : error_at (cp_expr_loc_or_input_loc (t),
6710 : : "accessing uninitialized member %qD",
6711 : : index);
6712 : 36 : if (is_access_expr)
6713 : 3 : inform (DECL_SOURCE_LOCATION (index),
6714 : : "%qD does not implicitly begin its lifetime "
6715 : : "because %qT does not have a non-deleted "
6716 : : "trivial default constructor, use "
6717 : : "%<std::construct_at%> instead",
6718 : : index, inner);
6719 : : else
6720 : 33 : inform (DECL_SOURCE_LOCATION (index),
6721 : : "initializing %qD requires a member access "
6722 : : "expression as the left operand of the assignment",
6723 : : index);
6724 : 36 : }
6725 : 82334 : *non_constant_p = true;
6726 : : }
6727 : 1136 : else if (has_active_member && CONSTRUCTOR_NO_CLEARING (*valp))
6728 : : {
6729 : : /* Diagnose changing the active union member while the union
6730 : : is in the process of being initialized. */
6731 : 9 : if (!ctx->quiet)
6732 : 3 : error_at (cp_expr_loc_or_input_loc (t),
6733 : : "change of the active member of a union "
6734 : : "from %qD to %qD during initialization",
6735 : 3 : CONSTRUCTOR_ELT (*valp, 0)->index,
6736 : : index);
6737 : 9 : *non_constant_p = true;
6738 : : }
6739 : : no_zero_init = true;
6740 : : }
6741 : :
6742 : 6981460 : ctors.safe_push (valp);
6743 : 6981460 : vec_safe_push (indexes, index);
6744 : :
6745 : 6981460 : constructor_elt *cep
6746 : 6981460 : = get_or_insert_ctor_field (*valp, index);
6747 : 6981460 : index_pos_hints.safe_push (cep - CONSTRUCTOR_ELTS (*valp)->begin());
6748 : :
6749 : 6981460 : if (code == UNION_TYPE)
6750 : 157444 : activated_union_member_p = true;
6751 : :
6752 : 6981460 : valp = &cep->value;
6753 : 6981460 : type = reftype;
6754 : : }
6755 : :
6756 : : /* For initialization of an empty base, the original target will be
6757 : : *(base*)this, evaluation of which resolves to the object
6758 : : argument, which has the derived type rather than the base type. */
6759 : 54963437 : if (!empty_base && !(same_type_ignoring_top_level_qualifiers_p
6760 : 27477536 : (initialized_type (init), type)))
6761 : : {
6762 : 2840 : gcc_assert (is_empty_class (TREE_TYPE (target)));
6763 : : empty_base = true;
6764 : : }
6765 : :
6766 : : /* Detect modifying a constant object in constexpr evaluation.
6767 : : We have found a const object that is being modified. Figure out
6768 : : if we need to issue an error. Consider
6769 : :
6770 : : struct A {
6771 : : int n;
6772 : : constexpr A() : n(1) { n = 2; } // #1
6773 : : };
6774 : : struct B {
6775 : : const A a;
6776 : : constexpr B() { a.n = 3; } // #2
6777 : : };
6778 : : constexpr B b{};
6779 : :
6780 : : #1 is OK, since we're modifying an object under construction, but
6781 : : #2 is wrong, since "a" is const and has been fully constructed.
6782 : : To track it, we use the TREE_READONLY bit in the object's CONSTRUCTOR
6783 : : which means that the object is read-only. For the example above, the
6784 : : *ctors stack at the point of #2 will look like:
6785 : :
6786 : : ctors[0] = {.a={.n=2}} TREE_READONLY = 0
6787 : : ctors[1] = {.n=2} TREE_READONLY = 1
6788 : :
6789 : : and we're modifying "b.a", so we search the stack and see if the
6790 : : constructor for "b.a" has already run. */
6791 : 27485901 : if (const_object_being_modified)
6792 : : {
6793 : 3981 : bool fail = false;
6794 : 3981 : tree const_objtype
6795 : 3981 : = strip_array_types (TREE_TYPE (const_object_being_modified));
6796 : 3981 : if (!CLASS_TYPE_P (const_objtype))
6797 : : fail = true;
6798 : : else
6799 : : {
6800 : : /* [class.ctor]p5 "A constructor can be invoked for a const,
6801 : : volatile, or const volatile object. const and volatile
6802 : : semantics are not applied on an object under construction.
6803 : : They come into effect when the constructor for the most
6804 : : derived object ends." */
6805 : 11797 : for (tree *elt : ctors)
6806 : 4045 : if (same_type_ignoring_top_level_qualifiers_p
6807 : 4045 : (TREE_TYPE (const_object_being_modified), TREE_TYPE (*elt)))
6808 : : {
6809 : 3876 : fail = TREE_READONLY (*elt);
6810 : 3876 : break;
6811 : : }
6812 : : }
6813 : 3876 : if (fail)
6814 : : {
6815 : 198 : if (!ctx->quiet)
6816 : 63 : modifying_const_object_error (t, const_object_being_modified);
6817 : 198 : *non_constant_p = true;
6818 : 198 : return t;
6819 : : }
6820 : : }
6821 : :
6822 : 27485703 : if (!preeval)
6823 : : {
6824 : : /* We're handling an INIT_EXPR of class type, so the value of the
6825 : : initializer can depend on the object it's initializing. */
6826 : :
6827 : : /* Create a new CONSTRUCTOR in case evaluation of the initializer
6828 : : wants to modify it. */
6829 : 10189770 : if (*valp == NULL_TREE)
6830 : : {
6831 : 10065684 : *valp = build_constructor (type, NULL);
6832 : 10065684 : CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
6833 : 10065684 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
6834 : : }
6835 : 10189770 : new_ctx.ctor = empty_base ? NULL_TREE : *valp;
6836 : 10189770 : new_ctx.object = target;
6837 : : /* Avoid temporary materialization when initializing from a TARGET_EXPR.
6838 : : We don't need to mess with AGGR_EXPR_SLOT/VEC_INIT_EXPR_SLOT because
6839 : : expansion of those trees uses ctx instead. */
6840 : 10189770 : if (TREE_CODE (init) == TARGET_EXPR)
6841 : 3344032 : if (tree tinit = TARGET_EXPR_INITIAL (init))
6842 : 3344032 : init = tinit;
6843 : 10189770 : init = cxx_eval_constant_expression (&new_ctx, init, vc_prvalue,
6844 : : non_constant_p, overflow_p);
6845 : : /* The hash table might have moved since the get earlier, and the
6846 : : initializer might have mutated the underlying CONSTRUCTORs, so we must
6847 : : recompute VALP. */
6848 : 10189770 : valp = ctx->global->get_value_ptr (object, TREE_CODE (t) == INIT_EXPR);
6849 : 10929189 : for (unsigned i = 0; i < vec_safe_length (indexes); i++)
6850 : : {
6851 : 739419 : ctors[i] = valp;
6852 : 739419 : constructor_elt *cep
6853 : 739419 : = get_or_insert_ctor_field (*valp, indexes[i], index_pos_hints[i]);
6854 : 739419 : valp = &cep->value;
6855 : : }
6856 : : }
6857 : :
6858 : 27485703 : if (*non_constant_p)
6859 : : return t;
6860 : :
6861 : : /* Don't share a CONSTRUCTOR that might be changed later. */
6862 : 23506411 : init = unshare_constructor (init);
6863 : :
6864 : 23506411 : gcc_checking_assert (!*valp || (same_type_ignoring_top_level_qualifiers_p
6865 : : (TREE_TYPE (*valp), type)));
6866 : 23506411 : if (empty_base)
6867 : : {
6868 : : /* Just evaluate the initializer and return, since there's no actual data
6869 : : to store, and we didn't build a CONSTRUCTOR. */
6870 : 8447 : if (!*valp)
6871 : : {
6872 : : /* But do make sure we have something in *valp. */
6873 : 0 : *valp = build_constructor (type, nullptr);
6874 : 0 : CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
6875 : 0 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
6876 : : }
6877 : : }
6878 : 23497964 : else if (*valp && TREE_CODE (*valp) == CONSTRUCTOR
6879 : 6296058 : && TREE_CODE (init) == CONSTRUCTOR)
6880 : : {
6881 : : /* An outer ctx->ctor might be pointing to *valp, so replace
6882 : : its contents. */
6883 : 1429022 : CONSTRUCTOR_ELTS (*valp) = CONSTRUCTOR_ELTS (init);
6884 : 1429022 : TREE_CONSTANT (*valp) = TREE_CONSTANT (init);
6885 : 1429022 : TREE_SIDE_EFFECTS (*valp) = TREE_SIDE_EFFECTS (init);
6886 : 4287066 : CONSTRUCTOR_NO_CLEARING (*valp)
6887 : 1429022 : = CONSTRUCTOR_NO_CLEARING (init);
6888 : 4287066 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp)
6889 : 1429022 : = CONSTRUCTOR_ZERO_PADDING_BITS (init);
6890 : : }
6891 : : else
6892 : 22068942 : *valp = init;
6893 : :
6894 : : /* After initialization, 'const' semantics apply to the value of the
6895 : : object. Make a note of this fact by marking the CONSTRUCTOR
6896 : : TREE_READONLY. */
6897 : 23506411 : if (TREE_CODE (t) == INIT_EXPR
6898 : 17852796 : && !empty_base
6899 : 17844349 : && TREE_CODE (*valp) == CONSTRUCTOR
6900 : 24923825 : && TYPE_READONLY (type))
6901 : : {
6902 : 17508 : if (INDIRECT_REF_P (target)
6903 : 32589 : && (is_this_parameter
6904 : 15081 : (tree_strip_nop_conversions (TREE_OPERAND (target, 0)))))
6905 : : /* We've just initialized '*this' (perhaps via the target
6906 : : constructor of a delegating constructor). Leave it up to the
6907 : : caller that set 'this' to set TREE_READONLY appropriately. */
6908 : 26 : gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
6909 : : (TREE_TYPE (target), type) || empty_base);
6910 : : else
6911 : 17482 : TREE_READONLY (*valp) = true;
6912 : : }
6913 : :
6914 : : /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing
6915 : : CONSTRUCTORs, if any. */
6916 : 23506411 : bool c = TREE_CONSTANT (init);
6917 : 23506411 : bool s = TREE_SIDE_EFFECTS (init);
6918 : 23506411 : if (!indexes->is_empty ())
6919 : : {
6920 : 4174273 : tree last = indexes->last ();
6921 : 4174273 : if (TREE_CODE (last) == REALPART_EXPR
6922 : 4174273 : || TREE_CODE (last) == IMAGPART_EXPR)
6923 : : {
6924 : : /* And canonicalize COMPLEX_EXPR into COMPLEX_CST if
6925 : : possible. */
6926 : 38 : tree *cexpr = ctors.last ();
6927 : 38 : if (tree c = const_binop (COMPLEX_EXPR, TREE_TYPE (*cexpr),
6928 : 38 : TREE_OPERAND (*cexpr, 0),
6929 : 38 : TREE_OPERAND (*cexpr, 1)))
6930 : 37 : *cexpr = c;
6931 : : else
6932 : : {
6933 : 2 : TREE_CONSTANT (*cexpr)
6934 : 1 : = (TREE_CONSTANT (TREE_OPERAND (*cexpr, 0))
6935 : 1 : & TREE_CONSTANT (TREE_OPERAND (*cexpr, 1)));
6936 : 2 : TREE_SIDE_EFFECTS (*cexpr)
6937 : 1 : = (TREE_SIDE_EFFECTS (TREE_OPERAND (*cexpr, 0))
6938 : 1 : | TREE_SIDE_EFFECTS (TREE_OPERAND (*cexpr, 1)));
6939 : : }
6940 : 38 : c = TREE_CONSTANT (*cexpr);
6941 : 38 : s = TREE_SIDE_EFFECTS (*cexpr);
6942 : : }
6943 : : }
6944 : 23506411 : if (!c || s || activated_union_member_p)
6945 : 11346278 : for (tree *elt : ctors)
6946 : : {
6947 : 1107617 : if (TREE_CODE (*elt) != CONSTRUCTOR)
6948 : 0 : continue;
6949 : 1107617 : if (!c)
6950 : 807607 : TREE_CONSTANT (*elt) = false;
6951 : 1107617 : if (s)
6952 : 0 : TREE_SIDE_EFFECTS (*elt) = true;
6953 : : /* Clear CONSTRUCTOR_NO_CLEARING since we've activated a member of
6954 : : this union. */
6955 : 1107617 : if (TREE_CODE (TREE_TYPE (*elt)) == UNION_TYPE)
6956 : 74813 : CONSTRUCTOR_NO_CLEARING (*elt) = false;
6957 : : }
6958 : :
6959 : 23506411 : if (lval)
6960 : 20851767 : return target;
6961 : : else
6962 : : return init;
6963 : 28704500 : }
6964 : :
6965 : : /* Evaluate a ++ or -- expression. */
6966 : :
6967 : : static tree
6968 : 1817590 : cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t,
6969 : : value_cat lval,
6970 : : bool *non_constant_p, bool *overflow_p)
6971 : : {
6972 : 1817590 : enum tree_code code = TREE_CODE (t);
6973 : 1817590 : tree type = TREE_TYPE (t);
6974 : 1817590 : tree op = TREE_OPERAND (t, 0);
6975 : 1817590 : tree offset = TREE_OPERAND (t, 1);
6976 : 1817590 : gcc_assert (TREE_CONSTANT (offset));
6977 : :
6978 : : /* OFFSET is constant, but perhaps not constant enough. We need to
6979 : : e.g. bash FLOAT_EXPRs to REAL_CSTs. */
6980 : 1817590 : offset = fold_simple (offset);
6981 : :
6982 : : /* The operand as an lvalue. */
6983 : 1817590 : op = cxx_eval_constant_expression (ctx, op, vc_glvalue,
6984 : : non_constant_p, overflow_p);
6985 : :
6986 : : /* The operand as an rvalue. */
6987 : 1817590 : tree val
6988 : 1817590 : = cxx_eval_constant_expression (ctx, op, vc_prvalue,
6989 : : non_constant_p, overflow_p);
6990 : : /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
6991 : : a local array in a constexpr function. */
6992 : 1817590 : bool ptr = INDIRECT_TYPE_P (TREE_TYPE (val));
6993 : 755133 : if (!ptr)
6994 : 755133 : VERIFY_CONSTANT (val);
6995 : :
6996 : : /* The modified value. */
6997 : 1778013 : bool inc = (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR);
6998 : 1778013 : tree mod;
6999 : 1778013 : if (INDIRECT_TYPE_P (type))
7000 : : {
7001 : : /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
7002 : 1062457 : offset = convert_to_ptrofftype (offset);
7003 : 1062457 : if (!inc)
7004 : 78896 : offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset);
7005 : 1062457 : mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset);
7006 : : }
7007 : 715556 : else if (c_promoting_integer_type_p (type)
7008 : 1719 : && !TYPE_UNSIGNED (type)
7009 : 715625 : && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
7010 : : {
7011 : 69 : offset = fold_convert (integer_type_node, offset);
7012 : 69 : mod = fold_convert (integer_type_node, val);
7013 : 125 : tree t = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, integer_type_node,
7014 : : mod, offset);
7015 : 69 : mod = fold_convert (type, t);
7016 : 69 : if (TREE_OVERFLOW_P (mod) && !TREE_OVERFLOW_P (t))
7017 : 9 : TREE_OVERFLOW (mod) = false;
7018 : : }
7019 : : else
7020 : 1069030 : mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset);
7021 : 1778013 : if (!ptr)
7022 : 715556 : VERIFY_CONSTANT (mod);
7023 : :
7024 : : /* Storing the modified value. */
7025 : 2758527 : tree store = build2_loc (cp_expr_loc_or_loc (t, input_location),
7026 : : MODIFY_EXPR, type, op, mod);
7027 : 1778013 : mod = cxx_eval_constant_expression (ctx, store, lval,
7028 : : non_constant_p, overflow_p);
7029 : 1778013 : ggc_free (store);
7030 : 1778013 : if (*non_constant_p)
7031 : : return t;
7032 : :
7033 : : /* And the value of the expression. */
7034 : 1729542 : if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
7035 : : /* Prefix ops are lvalues, but the caller might want an rvalue;
7036 : : lval has already been taken into account in the store above. */
7037 : : return mod;
7038 : : else
7039 : : /* Postfix ops are rvalues. */
7040 : 458004 : return val;
7041 : : }
7042 : :
7043 : : /* Predicates for the meaning of *jump_target. */
7044 : :
7045 : : static bool
7046 : 31516263 : returns (tree *jump_target)
7047 : : {
7048 : 31516263 : return *jump_target
7049 : 4101648 : && TREE_CODE (*jump_target) == RETURN_EXPR;
7050 : : }
7051 : :
7052 : : static bool
7053 : 32489009 : breaks (tree *jump_target)
7054 : : {
7055 : 32489009 : return *jump_target
7056 : 32489009 : && ((TREE_CODE (*jump_target) == LABEL_DECL
7057 : 23 : && LABEL_DECL_BREAK (*jump_target))
7058 : 727149 : || TREE_CODE (*jump_target) == BREAK_STMT
7059 : 687491 : || TREE_CODE (*jump_target) == EXIT_EXPR);
7060 : : }
7061 : :
7062 : : static bool
7063 : 55506971 : continues (tree *jump_target)
7064 : : {
7065 : 55506971 : return *jump_target
7066 : 55506971 : && ((TREE_CODE (*jump_target) == LABEL_DECL
7067 : 35 : && LABEL_DECL_CONTINUE (*jump_target))
7068 : 707868 : || TREE_CODE (*jump_target) == CONTINUE_STMT);
7069 : :
7070 : : }
7071 : :
7072 : : static bool
7073 : 6126654 : switches (tree *jump_target)
7074 : : {
7075 : 6126654 : return *jump_target
7076 : 6126652 : && TREE_CODE (*jump_target) == INTEGER_CST;
7077 : : }
7078 : :
7079 : : /* Subroutine of cxx_eval_statement_list. Determine whether the statement
7080 : : STMT matches *jump_target. If we're looking for a case label and we see
7081 : : the default label, note it in ctx->css_state. */
7082 : :
7083 : : static bool
7084 : 247867 : label_matches (const constexpr_ctx *ctx, tree *jump_target, tree stmt)
7085 : : {
7086 : 247867 : switch (TREE_CODE (*jump_target))
7087 : : {
7088 : 15 : case LABEL_DECL:
7089 : 15 : if (TREE_CODE (stmt) == LABEL_EXPR
7090 : 15 : && LABEL_EXPR_LABEL (stmt) == *jump_target)
7091 : : return true;
7092 : : break;
7093 : :
7094 : 245314 : case INTEGER_CST:
7095 : 245314 : if (TREE_CODE (stmt) == CASE_LABEL_EXPR)
7096 : : {
7097 : 245314 : gcc_assert (ctx->css_state != NULL);
7098 : 245314 : if (!CASE_LOW (stmt))
7099 : : {
7100 : : /* default: should appear just once in a SWITCH_EXPR
7101 : : body (excluding nested SWITCH_EXPR). */
7102 : 43556 : gcc_assert (*ctx->css_state != css_default_seen);
7103 : : /* When evaluating SWITCH_EXPR body for the second time,
7104 : : return true for the default: label. */
7105 : 43556 : if (*ctx->css_state == css_default_processing)
7106 : : return true;
7107 : 21802 : *ctx->css_state = css_default_seen;
7108 : : }
7109 : 201758 : else if (CASE_HIGH (stmt))
7110 : : {
7111 : 40 : if (tree_int_cst_le (CASE_LOW (stmt), *jump_target)
7112 : 62 : && tree_int_cst_le (*jump_target, CASE_HIGH (stmt)))
7113 : : return true;
7114 : : }
7115 : 201718 : else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt)))
7116 : : return true;
7117 : : }
7118 : : break;
7119 : :
7120 : : case BREAK_STMT:
7121 : : case CONTINUE_STMT:
7122 : : /* These two are handled directly in cxx_eval_loop_expr by testing
7123 : : breaks (jump_target) or continues (jump_target). */
7124 : : break;
7125 : :
7126 : 0 : default:
7127 : 0 : gcc_unreachable ();
7128 : : }
7129 : : return false;
7130 : : }
7131 : :
7132 : : /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
7133 : : semantics, for switch, break, continue, and return. */
7134 : :
7135 : : static tree
7136 : 23261463 : cxx_eval_statement_list (const constexpr_ctx *ctx, tree t,
7137 : : bool *non_constant_p, bool *overflow_p,
7138 : : tree *jump_target)
7139 : : {
7140 : 23261463 : tree local_target;
7141 : : /* In a statement-expression we want to return the last value.
7142 : : For empty statement expression return void_node. */
7143 : 23261463 : tree r = void_node;
7144 : 23261463 : if (!jump_target)
7145 : : {
7146 : 461 : local_target = NULL_TREE;
7147 : 461 : jump_target = &local_target;
7148 : : }
7149 : 48759383 : for (tree_stmt_iterator i = tsi_start (t); !tsi_end_p (i); ++i)
7150 : : {
7151 : 40246449 : tree stmt = *i;
7152 : :
7153 : : /* We've found a continue, so skip everything until we reach
7154 : : the label its jumping to. */
7155 : 40246449 : if (continues (jump_target))
7156 : : {
7157 : 2553 : if (label_matches (ctx, jump_target, stmt))
7158 : : /* Found it. */
7159 : 3 : *jump_target = NULL_TREE;
7160 : : else
7161 : 2550 : continue;
7162 : : }
7163 : 40243899 : if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT)
7164 : 8312894 : continue;
7165 : :
7166 : 31931005 : value_cat lval = vc_discard;
7167 : : /* The result of a statement-expression is not wrapped in EXPR_STMT. */
7168 : 31931005 : if (tsi_one_before_end_p (i) && TREE_CODE (stmt) != EXPR_STMT)
7169 : : lval = vc_prvalue;
7170 : :
7171 : 31931005 : r = cxx_eval_constant_expression (ctx, stmt, lval,
7172 : : non_constant_p, overflow_p,
7173 : : jump_target);
7174 : 31931005 : if (*non_constant_p)
7175 : : break;
7176 : 20443537 : if (returns (jump_target) || breaks (jump_target))
7177 : : break;
7178 : : }
7179 : 23261463 : if (*jump_target && jump_target == &local_target)
7180 : : {
7181 : : /* We aren't communicating the jump to our caller, so give up. We don't
7182 : : need to support evaluation of jumps out of statement-exprs. */
7183 : 24 : if (!ctx->quiet)
7184 : 3 : error_at (cp_expr_loc_or_input_loc (r),
7185 : : "statement is not a constant expression");
7186 : 24 : *non_constant_p = true;
7187 : : }
7188 : 23261463 : return r;
7189 : : }
7190 : :
7191 : : /* Evaluate a LOOP_EXPR for side-effects. Handles break and return
7192 : : semantics; continue semantics are covered by cxx_eval_statement_list. */
7193 : :
7194 : : static tree
7195 : 481235 : cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t,
7196 : : bool *non_constant_p, bool *overflow_p,
7197 : : tree *jump_target)
7198 : : {
7199 : 481235 : tree local_target;
7200 : 481235 : if (!jump_target)
7201 : : {
7202 : 53 : local_target = NULL_TREE;
7203 : 53 : jump_target = &local_target;
7204 : : }
7205 : :
7206 : 481235 : tree body, cond = NULL_TREE, expr = NULL_TREE;
7207 : 481235 : tree cond_prep = NULL_TREE, cond_cleanup = NULL_TREE;
7208 : 481235 : unsigned cond_cleanup_depth = 0;
7209 : 481235 : int count = 0;
7210 : 481235 : switch (TREE_CODE (t))
7211 : : {
7212 : 53 : case LOOP_EXPR:
7213 : 53 : body = LOOP_EXPR_BODY (t);
7214 : 53 : break;
7215 : 401598 : case DO_STMT:
7216 : 401598 : body = DO_BODY (t);
7217 : 401598 : cond = DO_COND (t);
7218 : 401598 : break;
7219 : 36607 : case WHILE_STMT:
7220 : 36607 : body = WHILE_BODY (t);
7221 : 36607 : cond = WHILE_COND (t);
7222 : 36607 : cond_prep = WHILE_COND_PREP (t);
7223 : 36607 : cond_cleanup = WHILE_COND_CLEANUP (t);
7224 : 36607 : count = -1;
7225 : 36607 : break;
7226 : 42977 : case FOR_STMT:
7227 : 42977 : if (FOR_INIT_STMT (t))
7228 : 0 : cxx_eval_constant_expression (ctx, FOR_INIT_STMT (t), vc_discard,
7229 : : non_constant_p, overflow_p, jump_target);
7230 : 42977 : if (*non_constant_p)
7231 : : return NULL_TREE;
7232 : 42977 : body = FOR_BODY (t);
7233 : 42977 : cond = FOR_COND (t);
7234 : 42977 : expr = FOR_EXPR (t);
7235 : 42977 : cond_prep = FOR_COND_PREP (t);
7236 : 42977 : cond_cleanup = FOR_COND_CLEANUP (t);
7237 : 42977 : count = -1;
7238 : 42977 : break;
7239 : 0 : default:
7240 : 0 : gcc_unreachable ();
7241 : : }
7242 : 481235 : if (cond_prep)
7243 : 12 : gcc_assert (TREE_CODE (cond_prep) == BIND_EXPR);
7244 : 7458481 : auto cleanup_cond = [&] {
7245 : : /* Clean up the condition variable after each iteration. */
7246 : 6977246 : if (cond_cleanup_depth && !*non_constant_p)
7247 : : {
7248 : 198 : auto_vec<tree, 4> cleanups (cond_cleanup_depth);
7249 : 198 : tree s = BIND_EXPR_BODY (cond_prep);
7250 : 198 : unsigned i;
7251 : 396 : for (i = cond_cleanup_depth; i; --i)
7252 : : {
7253 : 198 : tree_stmt_iterator iter = tsi_last (s);
7254 : 198 : s = tsi_stmt (iter);
7255 : 198 : cleanups.quick_push (CLEANUP_EXPR (s));
7256 : 198 : s = CLEANUP_BODY (s);
7257 : : }
7258 : 198 : tree c;
7259 : 792 : FOR_EACH_VEC_ELT_REVERSE (cleanups, i, c)
7260 : 198 : cxx_eval_constant_expression (ctx, c, vc_discard, non_constant_p,
7261 : : overflow_p);
7262 : 198 : }
7263 : 6977246 : if (cond_prep)
7264 : 198 : for (tree decl = BIND_EXPR_VARS (cond_prep);
7265 : 396 : decl; decl = DECL_CHAIN (decl))
7266 : 198 : destroy_value_checked (ctx, decl, non_constant_p);
7267 : 481235 : };
7268 : 6576181 : do
7269 : : {
7270 : 6576181 : if (count != -1)
7271 : : {
7272 : 6496597 : if (body)
7273 : 6496597 : cxx_eval_constant_expression (ctx, body, vc_discard,
7274 : : non_constant_p, overflow_p,
7275 : : jump_target);
7276 : 6496597 : if (breaks (jump_target))
7277 : : {
7278 : 586 : *jump_target = NULL_TREE;
7279 : 586 : break;
7280 : : }
7281 : :
7282 : 6496011 : if (TREE_CODE (t) != LOOP_EXPR && continues (jump_target))
7283 : 1072 : *jump_target = NULL_TREE;
7284 : :
7285 : 6496011 : if (expr)
7286 : 924367 : cxx_eval_constant_expression (ctx, expr, vc_prvalue,
7287 : : non_constant_p, overflow_p,
7288 : : jump_target);
7289 : 6496011 : cleanup_cond ();
7290 : : }
7291 : :
7292 : 6575595 : if (cond_prep)
7293 : : {
7294 : 198 : for (tree decl = BIND_EXPR_VARS (cond_prep);
7295 : 396 : decl; decl = DECL_CHAIN (decl))
7296 : 198 : ctx->global->clear_value (decl);
7297 : 198 : if (cond_cleanup)
7298 : : {
7299 : : /* If COND_CLEANUP is non-NULL, we need to evaluate DEPTH
7300 : : nested STATEMENT_LISTs from inside of BIND_EXPR_BODY,
7301 : : but defer the evaluation of CLEANUP_EXPRs of CLEANUP_STMT
7302 : : at the end of those STATEMENT_LISTs. */
7303 : 198 : cond_cleanup_depth = 0;
7304 : 198 : tree s = BIND_EXPR_BODY (cond_prep);
7305 : 198 : for (unsigned depth = tree_to_uhwi (cond_cleanup);
7306 : 396 : depth; --depth)
7307 : : {
7308 : 594 : for (tree_stmt_iterator i = tsi_start (s);
7309 : 594 : !tsi_end_p (i); ++i)
7310 : : {
7311 : 594 : tree stmt = *i;
7312 : 594 : if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT)
7313 : 0 : continue;
7314 : 594 : if (tsi_one_before_end_p (i))
7315 : : {
7316 : : /* The last statement in the STATEMENT_LIST
7317 : : has to be a CLEANUP_STMT (verified in
7318 : : finish_loop_cond_prep). We want to
7319 : : evaluate just its CLEANUP_BODY part but not
7320 : : CLEANUP_EXPR part just yet. */
7321 : 198 : gcc_assert (TREE_CODE (stmt) == CLEANUP_STMT);
7322 : : /* If the CLEANUP_STMT is not actually to be
7323 : : evaluated, don't increment cond_cleanup_depth
7324 : : so that we don't evaluate the CLEANUP_EXPR
7325 : : for it later either. */
7326 : 198 : if (*jump_target)
7327 : : {
7328 : : depth = 1;
7329 : : break;
7330 : : }
7331 : 198 : ++cond_cleanup_depth;
7332 : : /* If not in the innermost one, next iteration
7333 : : will handle CLEANUP_BODY similarly. */
7334 : 198 : if (depth > 1)
7335 : : {
7336 : 0 : s = CLEANUP_BODY (stmt);
7337 : 0 : break;
7338 : : }
7339 : : /* The innermost one can be evaluated normally. */
7340 : 198 : cxx_eval_constant_expression (ctx,
7341 : 198 : CLEANUP_BODY (stmt),
7342 : : vc_discard,
7343 : : non_constant_p,
7344 : : overflow_p,
7345 : : jump_target);
7346 : 198 : break;
7347 : : }
7348 : : /* And so should be evaluated statements which aren't
7349 : : last in the STATEMENT_LIST. */
7350 : 396 : cxx_eval_constant_expression (ctx, stmt, vc_discard,
7351 : : non_constant_p, overflow_p,
7352 : : jump_target);
7353 : 396 : if (*non_constant_p
7354 : 198 : || returns (jump_target)
7355 : 396 : || breaks (jump_target)
7356 : 792 : || continues (jump_target))
7357 : : {
7358 : : depth = 1;
7359 : : break;
7360 : : }
7361 : : }
7362 : : }
7363 : : }
7364 : : else
7365 : 0 : cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (cond_prep),
7366 : : vc_discard, non_constant_p,
7367 : : overflow_p, jump_target);
7368 : : }
7369 : :
7370 : 6575595 : if (cond)
7371 : : {
7372 : 6574440 : tree res
7373 : 6574440 : = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
7374 : : non_constant_p, overflow_p,
7375 : : jump_target);
7376 : 6574440 : if (res)
7377 : : {
7378 : 6558233 : if (verify_constant (res, ctx->quiet, non_constant_p,
7379 : : overflow_p))
7380 : : break;
7381 : 6476048 : if (integer_zerop (res))
7382 : : break;
7383 : : }
7384 : : else
7385 : 16207 : gcc_assert (*jump_target);
7386 : : }
7387 : :
7388 : 6111415 : if (++count >= constexpr_loop_limit)
7389 : : {
7390 : 28 : if (!ctx->quiet)
7391 : 9 : error_at (cp_expr_loc_or_input_loc (t),
7392 : : "%<constexpr%> loop iteration count exceeds limit of %d "
7393 : : "(use %<-fconstexpr-loop-limit=%> to increase the limit)",
7394 : : constexpr_loop_limit);
7395 : 28 : *non_constant_p = true;
7396 : 28 : break;
7397 : : }
7398 : : }
7399 : 12206547 : while (!returns (jump_target)
7400 : 6095160 : && !breaks (jump_target)
7401 : 6095160 : && !continues (jump_target)
7402 : 186 : && (!switches (jump_target) || count == 0)
7403 : 12222928 : && !*non_constant_p);
7404 : :
7405 : 481235 : cleanup_cond ();
7406 : :
7407 : 481235 : return NULL_TREE;
7408 : : }
7409 : :
7410 : : /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
7411 : : semantics. */
7412 : :
7413 : : static tree
7414 : 25769 : cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t,
7415 : : bool *non_constant_p, bool *overflow_p,
7416 : : tree *jump_target)
7417 : : {
7418 : 25769 : tree cond
7419 : 25769 : = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_COND (t) : SWITCH_COND (t);
7420 : 25769 : cond = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
7421 : : non_constant_p, overflow_p);
7422 : 25769 : VERIFY_CONSTANT (cond);
7423 : 25312 : if (TREE_CODE (cond) != INTEGER_CST)
7424 : : {
7425 : : /* If the condition doesn't reduce to an INTEGER_CST it isn't a usable
7426 : : switch condition even if it's constant enough for other things
7427 : : (c++/113545). */
7428 : 6 : gcc_checking_assert (ctx->quiet);
7429 : 6 : *non_constant_p = true;
7430 : 6 : return t;
7431 : : }
7432 : :
7433 : 25306 : *jump_target = cond;
7434 : :
7435 : 25306 : tree body
7436 : 25306 : = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_BODY (t) : SWITCH_BODY (t);
7437 : 25306 : constexpr_ctx new_ctx = *ctx;
7438 : 25306 : constexpr_switch_state css = css_default_not_seen;
7439 : 25306 : new_ctx.css_state = &css;
7440 : 25306 : cxx_eval_constant_expression (&new_ctx, body, vc_discard,
7441 : : non_constant_p, overflow_p, jump_target);
7442 : 47110 : if (switches (jump_target) && css == css_default_seen)
7443 : : {
7444 : : /* If the SWITCH_EXPR body has default: label, process it once again,
7445 : : this time instructing label_matches to return true for default:
7446 : : label on switches (jump_target). */
7447 : 21754 : css = css_default_processing;
7448 : 21754 : cxx_eval_constant_expression (&new_ctx, body, vc_discard,
7449 : : non_constant_p, overflow_p, jump_target);
7450 : : }
7451 : 25306 : if (breaks (jump_target) || switches (jump_target))
7452 : 19168 : *jump_target = NULL_TREE;
7453 : : return NULL_TREE;
7454 : : }
7455 : :
7456 : : /* Find the object of TYPE under initialization in CTX. */
7457 : :
7458 : : static tree
7459 : 16644 : lookup_placeholder (const constexpr_ctx *ctx, value_cat lval, tree type)
7460 : : {
7461 : 16644 : if (!ctx)
7462 : : return NULL_TREE;
7463 : :
7464 : : /* Prefer the outermost matching object, but don't cross
7465 : : CONSTRUCTOR_PLACEHOLDER_BOUNDARY constructors. */
7466 : 16312 : if (ctx->ctor && !CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor))
7467 : 510 : if (tree outer_ob = lookup_placeholder (ctx->parent, lval, type))
7468 : : return outer_ob;
7469 : :
7470 : : /* We could use ctx->object unconditionally, but using ctx->ctor when we
7471 : : can is a minor optimization. */
7472 : 16134 : if (!lval && ctx->ctor && same_type_p (TREE_TYPE (ctx->ctor), type))
7473 : 312 : return ctx->ctor;
7474 : :
7475 : 15822 : if (!ctx->object)
7476 : : return NULL_TREE;
7477 : :
7478 : : /* Since an object cannot have a field of its own type, we can search outward
7479 : : from ctx->object to find the unique containing object of TYPE. */
7480 : : tree ob = ctx->object;
7481 : 15813 : while (ob)
7482 : : {
7483 : 15813 : if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob), type))
7484 : : break;
7485 : 223 : if (handled_component_p (ob))
7486 : 221 : ob = TREE_OPERAND (ob, 0);
7487 : : else
7488 : : ob = NULL_TREE;
7489 : : }
7490 : :
7491 : : return ob;
7492 : : }
7493 : :
7494 : : /* Complain about an attempt to evaluate inline assembly. If FUNDEF_P is
7495 : : true, we're checking a constexpr function body. */
7496 : :
7497 : : static void
7498 : 31 : inline_asm_in_constexpr_error (location_t loc, bool fundef_p)
7499 : : {
7500 : 31 : auto_diagnostic_group d;
7501 : 31 : if (constexpr_error (loc, fundef_p, "inline assembly is not a "
7502 : : "constant expression"))
7503 : 31 : inform (loc, "only unevaluated inline assembly is allowed in a "
7504 : : "%<constexpr%> function in C++20");
7505 : 31 : }
7506 : :
7507 : : /* We're getting the constant value of DECL in a manifestly constant-evaluated
7508 : : context; maybe complain about that. */
7509 : :
7510 : : static void
7511 : 49320998 : maybe_warn_about_constant_value (location_t loc, tree decl)
7512 : : {
7513 : 49320998 : static bool explained = false;
7514 : 49320998 : if (cxx_dialect >= cxx17
7515 : 49175289 : && warn_interference_size
7516 : 49175289 : && !OPTION_SET_P (param_destruct_interfere_size)
7517 : 49175289 : && DECL_CONTEXT (decl) == std_node
7518 : 5005655 : && DECL_NAME (decl)
7519 : 5005649 : && id_equal (DECL_NAME (decl), "hardware_destructive_interference_size")
7520 : 12 : && (LOCATION_FILE (input_location) != main_input_filename
7521 : 6 : || module_exporting_p ())
7522 : 49321001 : && warning_at (loc, OPT_Winterference_size, "use of %qD", decl)
7523 : 49321007 : && !explained)
7524 : : {
7525 : 6 : explained = true;
7526 : 6 : inform (loc, "its value can vary between compiler versions or "
7527 : : "with different %<-mtune%> or %<-mcpu%> flags");
7528 : 6 : inform (loc, "if this use is part of a public ABI, change it to "
7529 : : "instead use a constant variable you define");
7530 : 6 : inform (loc, "the default value for the current CPU tuning "
7531 : : "is %d bytes", param_destruct_interfere_size);
7532 : 6 : inform (loc, "you can stabilize this value with %<--param "
7533 : : "hardware_destructive_interference_size=%d%>, or disable "
7534 : : "this warning with %<-Wno-interference-size%>",
7535 : : param_destruct_interfere_size);
7536 : : }
7537 : 49320998 : }
7538 : :
7539 : : /* For element type ELT_TYPE, return the appropriate type of the heap object
7540 : : containing such element(s). COOKIE_SIZE is NULL or the size of cookie
7541 : : in bytes. If COOKIE_SIZE is NULL, return array type
7542 : : ELT_TYPE[FULL_SIZE / sizeof(ELT_TYPE)], otherwise return
7543 : : struct { size_t[COOKIE_SIZE/sizeof(size_t)]; ELT_TYPE[N]; }
7544 : : where N is computed such that the size of the struct fits into FULL_SIZE.
7545 : : If ARG_SIZE is non-NULL, it is the first argument to the new operator.
7546 : : It should be passed if ELT_TYPE is zero sized type in which case FULL_SIZE
7547 : : will be also 0 and so it is not possible to determine the actual array
7548 : : size. CTX, NON_CONSTANT_P and OVERFLOW_P are used during constant
7549 : : expression evaluation of subexpressions of ARG_SIZE. */
7550 : :
7551 : : static tree
7552 : 1916 : build_new_constexpr_heap_type (const constexpr_ctx *ctx, tree elt_type,
7553 : : tree cookie_size, tree full_size, tree arg_size,
7554 : : bool *non_constant_p, bool *overflow_p)
7555 : : {
7556 : 1916 : gcc_assert (cookie_size == NULL_TREE || tree_fits_uhwi_p (cookie_size));
7557 : 1916 : gcc_assert (tree_fits_uhwi_p (full_size));
7558 : 1916 : unsigned HOST_WIDE_INT csz = cookie_size ? tree_to_uhwi (cookie_size) : 0;
7559 : 1916 : if (arg_size)
7560 : : {
7561 : 9 : STRIP_NOPS (arg_size);
7562 : 9 : if (cookie_size)
7563 : : {
7564 : 0 : if (TREE_CODE (arg_size) != PLUS_EXPR)
7565 : : arg_size = NULL_TREE;
7566 : 0 : else if (TREE_CODE (TREE_OPERAND (arg_size, 0)) == INTEGER_CST
7567 : 0 : && tree_int_cst_equal (cookie_size,
7568 : 0 : TREE_OPERAND (arg_size, 0)))
7569 : : {
7570 : 0 : arg_size = TREE_OPERAND (arg_size, 1);
7571 : 0 : STRIP_NOPS (arg_size);
7572 : : }
7573 : 0 : else if (TREE_CODE (TREE_OPERAND (arg_size, 1)) == INTEGER_CST
7574 : 0 : && tree_int_cst_equal (cookie_size,
7575 : 0 : TREE_OPERAND (arg_size, 1)))
7576 : : {
7577 : 0 : arg_size = TREE_OPERAND (arg_size, 0);
7578 : 0 : STRIP_NOPS (arg_size);
7579 : : }
7580 : : else
7581 : : arg_size = NULL_TREE;
7582 : : }
7583 : 9 : if (arg_size && TREE_CODE (arg_size) == MULT_EXPR)
7584 : : {
7585 : 9 : tree op0 = TREE_OPERAND (arg_size, 0);
7586 : 9 : tree op1 = TREE_OPERAND (arg_size, 1);
7587 : 9 : if (integer_zerop (op0))
7588 : 9 : arg_size
7589 : 9 : = cxx_eval_constant_expression (ctx, op1, vc_prvalue,
7590 : : non_constant_p, overflow_p);
7591 : 0 : else if (integer_zerop (op1))
7592 : 0 : arg_size
7593 : 0 : = cxx_eval_constant_expression (ctx, op0, vc_prvalue,
7594 : : non_constant_p, overflow_p);
7595 : : else
7596 : : arg_size = NULL_TREE;
7597 : : }
7598 : : else
7599 : : arg_size = NULL_TREE;
7600 : : }
7601 : :
7602 : 9 : unsigned HOST_WIDE_INT fsz = tree_to_uhwi (arg_size ? arg_size : full_size);
7603 : 1916 : if (!arg_size)
7604 : : {
7605 : 1907 : unsigned HOST_WIDE_INT esz = int_size_in_bytes (elt_type);
7606 : 1907 : gcc_assert (fsz >= csz);
7607 : 1907 : fsz -= csz;
7608 : 1907 : if (esz)
7609 : 1907 : fsz /= esz;
7610 : : }
7611 : 1916 : tree itype2 = build_index_type (size_int (fsz - 1));
7612 : 1916 : if (!cookie_size)
7613 : 1913 : return build_cplus_array_type (elt_type, itype2);
7614 : 3 : return build_new_constexpr_heap_type (elt_type, cookie_size, itype2);
7615 : : }
7616 : :
7617 : : /* Attempt to reduce the expression T to a constant value.
7618 : : On failure, issue diagnostic and return error_mark_node. */
7619 : : /* FIXME unify with c_fully_fold */
7620 : : /* FIXME overflow_p is too global */
7621 : :
7622 : : static tree
7623 : 1210083969 : cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
7624 : : value_cat lval,
7625 : : bool *non_constant_p, bool *overflow_p,
7626 : : tree *jump_target /* = NULL */)
7627 : : {
7628 : 1210083969 : if (jump_target && *jump_target)
7629 : : {
7630 : : /* If we are jumping, ignore all statements/expressions except those
7631 : : that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */
7632 : 1018127 : switch (TREE_CODE (t))
7633 : : {
7634 : : case BIND_EXPR:
7635 : : case STATEMENT_LIST:
7636 : : case LOOP_EXPR:
7637 : : case COND_EXPR:
7638 : : case IF_STMT:
7639 : : case DO_STMT:
7640 : : case WHILE_STMT:
7641 : : case FOR_STMT:
7642 : : break;
7643 : 245314 : case LABEL_EXPR:
7644 : 245314 : case CASE_LABEL_EXPR:
7645 : 245314 : if (label_matches (ctx, jump_target, t))
7646 : : /* Found it. */
7647 : 25256 : *jump_target = NULL_TREE;
7648 : 245314 : return NULL_TREE;
7649 : : default:
7650 : : return NULL_TREE;
7651 : : }
7652 : : }
7653 : 1209266057 : if (error_operand_p (t))
7654 : : {
7655 : 133 : *non_constant_p = true;
7656 : 133 : return t;
7657 : : }
7658 : :
7659 : : /* Change the input location to the currently processed expression for
7660 : : better error messages when a subexpression has no location. */
7661 : 1209265924 : location_t loc = cp_expr_loc_or_input_loc (t);
7662 : 2418526454 : iloc_sentinel sentinel (loc);
7663 : :
7664 : 1209265924 : STRIP_ANY_LOCATION_WRAPPER (t);
7665 : :
7666 : 1209265924 : if (CONSTANT_CLASS_P (t))
7667 : : {
7668 : 232411732 : if (TREE_OVERFLOW (t))
7669 : : {
7670 : 48 : if (!ctx->quiet)
7671 : 15 : permerror (input_location, "overflow in constant expression");
7672 : 48 : if (!flag_permissive || ctx->quiet)
7673 : 45 : *overflow_p = true;
7674 : : }
7675 : :
7676 : 232411732 : if (TREE_CODE (t) == INTEGER_CST
7677 : 225631240 : && TYPE_PTR_P (TREE_TYPE (t))
7678 : : /* INTEGER_CST with pointer-to-method type is only used
7679 : : for a virtual method in a pointer to member function.
7680 : : Don't reject those. */
7681 : 840115 : && TREE_CODE (TREE_TYPE (TREE_TYPE (t))) != METHOD_TYPE
7682 : 233251573 : && !integer_zerop (t))
7683 : : {
7684 : 6 : if (!ctx->quiet)
7685 : 0 : error ("value %qE of type %qT is not a constant expression",
7686 : 0 : t, TREE_TYPE (t));
7687 : 6 : *non_constant_p = true;
7688 : : }
7689 : :
7690 : 232411732 : return t;
7691 : : }
7692 : :
7693 : : /* Avoid excessively long constexpr evaluations. */
7694 : 976854192 : if (++ctx->global->constexpr_ops_count >= constexpr_ops_limit)
7695 : : {
7696 : 9 : if (!ctx->quiet)
7697 : 3 : error_at (loc,
7698 : : "%<constexpr%> evaluation operation count exceeds limit of "
7699 : : "%wd (use %<-fconstexpr-ops-limit=%> to increase the limit)",
7700 : : constexpr_ops_limit);
7701 : 9 : ctx->global->constexpr_ops_count = INTTYPE_MINIMUM (HOST_WIDE_INT);
7702 : 9 : *non_constant_p = true;
7703 : 9 : return t;
7704 : : }
7705 : :
7706 : 976854183 : constexpr_ctx new_ctx;
7707 : 976854183 : tree r = t;
7708 : :
7709 : 976854183 : tree_code tcode = TREE_CODE (t);
7710 : 976854183 : switch (tcode)
7711 : : {
7712 : 18694116 : case RESULT_DECL:
7713 : 18694116 : if (lval)
7714 : : return t;
7715 : : /* We ask for an rvalue for the RESULT_DECL when indirecting
7716 : : through an invisible reference, or in named return value
7717 : : optimization. */
7718 : 9525 : if (tree v = ctx->global->get_value (t))
7719 : : return v;
7720 : : else
7721 : : {
7722 : 3 : if (!ctx->quiet)
7723 : 0 : error ("%qE is not a constant expression", t);
7724 : 3 : *non_constant_p = true;
7725 : : }
7726 : 3 : break;
7727 : :
7728 : 116435145 : case VAR_DECL:
7729 : 116435145 : if (DECL_HAS_VALUE_EXPR_P (t))
7730 : : {
7731 : 876708 : if (is_normal_capture_proxy (t)
7732 : 876708 : && current_function_decl == DECL_CONTEXT (t))
7733 : : {
7734 : : /* Function parms aren't constexpr within the function
7735 : : definition, so don't try to look at the closure. But if the
7736 : : captured variable is constant, try to evaluate it directly. */
7737 : 313235 : r = DECL_CAPTURED_VARIABLE (t);
7738 : : }
7739 : : else
7740 : 563473 : r = DECL_VALUE_EXPR (t);
7741 : :
7742 : 876708 : tree type = TREE_TYPE (t);
7743 : 876708 : if (TYPE_REF_P (type) != TYPE_REF_P (TREE_TYPE (r)))
7744 : : {
7745 : : /* Adjust r to match the reference-ness of t. */
7746 : 205015 : if (TYPE_REF_P (type))
7747 : 205012 : r = build_address (r);
7748 : : else
7749 : 3 : r = convert_from_reference (r);
7750 : : }
7751 : 876708 : return cxx_eval_constant_expression (ctx, r, lval, non_constant_p,
7752 : 876708 : overflow_p);
7753 : : }
7754 : : /* fall through */
7755 : 124473865 : case CONST_DECL:
7756 : : /* We used to not check lval for CONST_DECL, but darwin.cc uses
7757 : : CONST_DECL for aggregate constants. */
7758 : 124473865 : if (lval)
7759 : : return t;
7760 : 100467769 : else if (t == ctx->object)
7761 : 650284 : return ctx->ctor;
7762 : 99817485 : if (VAR_P (t))
7763 : : {
7764 : 90902057 : if (tree v = ctx->global->get_value (t))
7765 : : {
7766 : : r = v;
7767 : : break;
7768 : : }
7769 : 82568287 : if (ctx->global->is_outside_lifetime (t))
7770 : : {
7771 : 96 : if (!ctx->quiet)
7772 : 28 : outside_lifetime_error (loc, t);
7773 : 96 : *non_constant_p = true;
7774 : 96 : break;
7775 : : }
7776 : : }
7777 : 91483619 : if (ctx->manifestly_const_eval == mce_true)
7778 : 49320998 : maybe_warn_about_constant_value (loc, t);
7779 : 91483619 : if (COMPLETE_TYPE_P (TREE_TYPE (t))
7780 : 91483619 : && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
7781 : : {
7782 : : /* If the class is empty, we aren't actually loading anything. */
7783 : 79138 : r = build_constructor (TREE_TYPE (t), NULL);
7784 : 79138 : TREE_CONSTANT (r) = true;
7785 : : }
7786 : 91404481 : else if (ctx->strict)
7787 : 91102001 : r = decl_really_constant_value (t, /*unshare_p=*/false);
7788 : : else
7789 : 302480 : r = decl_constant_value (t, /*unshare_p=*/false);
7790 : 91480922 : if (TREE_CODE (r) == TARGET_EXPR
7791 : 91480922 : && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR)
7792 : 0 : r = TARGET_EXPR_INITIAL (r);
7793 : 91480922 : if (DECL_P (r)
7794 : : /* P2280 allows references to unknown. */
7795 : 91480922 : && !(VAR_P (t) && TYPE_REF_P (TREE_TYPE (t))))
7796 : : {
7797 : 25471731 : if (!ctx->quiet)
7798 : 146 : non_const_var_error (loc, r, /*fundef_p*/false);
7799 : 25471731 : *non_constant_p = true;
7800 : : }
7801 : : break;
7802 : :
7803 : : case DEBUG_BEGIN_STMT:
7804 : : /* ??? It might be nice to retain this information somehow, so
7805 : : as to be able to step into a constexpr function call. */
7806 : : /* Fall through. */
7807 : :
7808 : : case FUNCTION_DECL:
7809 : : case TEMPLATE_DECL:
7810 : : case LABEL_DECL:
7811 : : case LABEL_EXPR:
7812 : : case CASE_LABEL_EXPR:
7813 : : case PREDICT_EXPR:
7814 : : return t;
7815 : :
7816 : 109072394 : case PARM_DECL:
7817 : 109072394 : if (lval && !TYPE_REF_P (TREE_TYPE (t)))
7818 : : {
7819 : : /* glvalue use. */
7820 : 4100602 : if (TREE_ADDRESSABLE (TREE_TYPE (t)))
7821 : 66945 : if (tree v = ctx->global->get_value (t))
7822 : 789707188 : r = v;
7823 : : }
7824 : 104971792 : else if (tree v = ctx->global->get_value (t))
7825 : : {
7826 : 43058272 : r = v;
7827 : 43058272 : if (TREE_ADDRESSABLE (TREE_TYPE (t)))
7828 : 4773 : r = cxx_eval_constant_expression (ctx, r, vc_prvalue,
7829 : : non_constant_p, overflow_p);
7830 : : }
7831 : 61913520 : else if (lval)
7832 : : /* Defer in case this is only used for its type. */;
7833 : 61913520 : else if (ctx->global->is_outside_lifetime (t))
7834 : : {
7835 : 18 : if (!ctx->quiet)
7836 : 6 : outside_lifetime_error (loc, t);
7837 : 18 : *non_constant_p = true;
7838 : 18 : break;
7839 : : }
7840 : 61913502 : else if (COMPLETE_TYPE_P (TREE_TYPE (t))
7841 : 61913502 : && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
7842 : : {
7843 : : /* If the class is empty, we aren't actually loading anything. */
7844 : 6015 : r = build_constructor (TREE_TYPE (t), NULL);
7845 : 6015 : TREE_CONSTANT (r) = true;
7846 : : }
7847 : 61907487 : else if (TYPE_REF_P (TREE_TYPE (t)))
7848 : : /* P2280 allows references to unknown... */;
7849 : 51922964 : else if (is_this_parameter (t))
7850 : : /* ...as well as the this pointer. */;
7851 : : else
7852 : : {
7853 : 41737933 : if (!ctx->quiet)
7854 : 194 : error ("%qE is not a constant expression", t);
7855 : 41737933 : *non_constant_p = true;
7856 : : }
7857 : : break;
7858 : :
7859 : 79301452 : case CALL_EXPR:
7860 : 79301452 : case AGGR_INIT_EXPR:
7861 : 79301452 : r = cxx_eval_call_expression (ctx, t, lval,
7862 : : non_constant_p, overflow_p);
7863 : 79301452 : break;
7864 : :
7865 : 4547417 : case DECL_EXPR:
7866 : 4547417 : {
7867 : 4547417 : r = DECL_EXPR_DECL (t);
7868 : 4547417 : if (TREE_CODE (r) == USING_DECL)
7869 : : {
7870 : 4187 : r = void_node;
7871 : 4187 : break;
7872 : : }
7873 : :
7874 : 4543230 : if (VAR_P (r)
7875 : 4543230 : && (TREE_STATIC (r)
7876 : 4537883 : || (CP_DECL_THREAD_LOCAL_P (r) && !DECL_REALLY_EXTERN (r)))
7877 : : /* Allow __FUNCTION__ etc. */
7878 : 5347 : && !DECL_ARTIFICIAL (r)
7879 : 4543252 : && !decl_constant_var_p (r))
7880 : : {
7881 : 8 : if (!ctx->quiet)
7882 : : {
7883 : 2 : if (CP_DECL_THREAD_LOCAL_P (r))
7884 : 1 : error_at (loc, "control passes through definition of %qD "
7885 : : "with thread storage duration", r);
7886 : : else
7887 : 1 : error_at (loc, "control passes through definition of %qD "
7888 : : "with static storage duration", r);
7889 : : }
7890 : 8 : *non_constant_p = true;
7891 : 8 : break;
7892 : : }
7893 : :
7894 : : /* make_rtl_for_nonlocal_decl could have deferred emission of
7895 : : a local static var, but if it appears in a statement expression
7896 : : which is constant expression evaluated to e.g. just the address
7897 : : of the variable, its DECL_EXPR will never be seen during
7898 : : gimple lowering's record_vars_into as the statement expression
7899 : : will not be in the IL at all. */
7900 : 4543222 : if (VAR_P (r)
7901 : 4543222 : && TREE_STATIC (r)
7902 : 5339 : && !DECL_REALLY_EXTERN (r)
7903 : 5339 : && DECL_FUNCTION_SCOPE_P (r)
7904 : 5339 : && !var_in_maybe_constexpr_fn (r)
7905 : 4543225 : && decl_constant_var_p (r))
7906 : : {
7907 : 3 : varpool_node *node = varpool_node::get (r);
7908 : 3 : if (node == NULL || !node->definition)
7909 : 3 : rest_of_decl_compilation (r, 0, at_eof);
7910 : : }
7911 : :
7912 : 9074156 : if (AGGREGATE_TYPE_P (TREE_TYPE (r))
7913 : 8711494 : || VECTOR_TYPE_P (TREE_TYPE (r)))
7914 : : {
7915 : 375150 : new_ctx = *ctx;
7916 : 375150 : new_ctx.object = r;
7917 : 375150 : new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL);
7918 : 375150 : CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
7919 : 375150 : ctx->global->put_value (r, new_ctx.ctor);
7920 : 375150 : ctx = &new_ctx;
7921 : : }
7922 : :
7923 : 4543222 : if (tree init = DECL_INITIAL (r))
7924 : : {
7925 : 2821601 : init = cxx_eval_constant_expression (ctx, init, vc_prvalue,
7926 : : non_constant_p, overflow_p);
7927 : : /* Don't share a CONSTRUCTOR that might be changed. */
7928 : 2821601 : init = unshare_constructor (init);
7929 : : /* Remember that a constant object's constructor has already
7930 : : run. */
7931 : 5643202 : if (CLASS_TYPE_P (TREE_TYPE (r))
7932 : 3090470 : && CP_TYPE_CONST_P (TREE_TYPE (r)))
7933 : 529 : TREE_READONLY (init) = true;
7934 : 2821601 : ctx->global->put_value (r, init);
7935 : : }
7936 : 1721621 : else if (ctx == &new_ctx)
7937 : : /* We gave it a CONSTRUCTOR above. */;
7938 : : else
7939 : 1622315 : ctx->global->put_value (r, NULL_TREE);
7940 : : }
7941 : : break;
7942 : :
7943 : 10140202 : case TARGET_EXPR:
7944 : 10140202 : {
7945 : 10140202 : tree type = TREE_TYPE (t);
7946 : :
7947 : 10140202 : if (!literal_type_p (type))
7948 : : {
7949 : 10644 : if (!ctx->quiet)
7950 : : {
7951 : 0 : auto_diagnostic_group d;
7952 : 0 : error ("temporary of non-literal type %qT in a "
7953 : : "constant expression", type);
7954 : 0 : explain_non_literal_class (type);
7955 : 0 : }
7956 : 10644 : *non_constant_p = true;
7957 : 5045098 : break;
7958 : : }
7959 : 10129558 : gcc_checking_assert (!TARGET_EXPR_DIRECT_INIT_P (t));
7960 : : /* Avoid evaluating a TARGET_EXPR more than once. */
7961 : 10129558 : tree slot = TARGET_EXPR_SLOT (t);
7962 : 10129558 : if (tree v = ctx->global->get_value (slot))
7963 : : {
7964 : 833 : if (lval)
7965 : 4541175 : return slot;
7966 : : r = v;
7967 : : break;
7968 : : }
7969 : 10128725 : if ((AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type)))
7970 : : {
7971 : : /* We're being expanded without an explicit target, so start
7972 : : initializing a new object; expansion with an explicit target
7973 : : strips the TARGET_EXPR before we get here. */
7974 : 8088239 : new_ctx = *ctx;
7975 : : /* Link CTX to NEW_CTX so that lookup_placeholder can resolve
7976 : : any PLACEHOLDER_EXPR within the initializer that refers to the
7977 : : former object under construction. */
7978 : 8088239 : new_ctx.parent = ctx;
7979 : 8088239 : new_ctx.ctor = build_constructor (type, NULL);
7980 : 8088239 : CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
7981 : 8088239 : new_ctx.object = slot;
7982 : 8088239 : ctx->global->put_value (new_ctx.object, new_ctx.ctor);
7983 : 8088239 : ctx = &new_ctx;
7984 : : }
7985 : : /* Pass vc_prvalue because this indicates
7986 : : initialization of a temporary. */
7987 : 10128725 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), vc_prvalue,
7988 : : non_constant_p, overflow_p);
7989 : 10128725 : if (*non_constant_p)
7990 : : break;
7991 : : /* If the initializer is complex, evaluate it to initialize slot. */
7992 : 5094726 : bool is_complex = target_expr_needs_replace (t);
7993 : 5094726 : if (!is_complex)
7994 : : {
7995 : 5094696 : r = unshare_constructor (r);
7996 : : /* Adjust the type of the result to the type of the temporary. */
7997 : 5094696 : r = adjust_temp_type (type, r);
7998 : 5094696 : ctx->global->put_value (slot, r);
7999 : : }
8000 : 5094726 : if (TARGET_EXPR_CLEANUP (t) && !CLEANUP_EH_ONLY (t))
8001 : 242669 : ctx->global->cleanups->safe_push (TARGET_EXPR_CLEANUP (t));
8002 : 5094726 : if (ctx->save_exprs)
8003 : 1384599 : ctx->save_exprs->safe_push (slot);
8004 : 5094726 : if (lval)
8005 : : return slot;
8006 : 553929 : if (is_complex)
8007 : 0 : r = ctx->global->get_value (slot);
8008 : : }
8009 : 553929 : break;
8010 : :
8011 : 42651179 : case INIT_EXPR:
8012 : 42651179 : case MODIFY_EXPR:
8013 : 42651179 : gcc_assert (jump_target == NULL || *jump_target == NULL_TREE);
8014 : 42651179 : r = cxx_eval_store_expression (ctx, t, lval,
8015 : : non_constant_p, overflow_p);
8016 : 42651179 : break;
8017 : :
8018 : 0 : case SCOPE_REF:
8019 : 0 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
8020 : : lval,
8021 : : non_constant_p, overflow_p);
8022 : 0 : break;
8023 : :
8024 : 27260678 : case RETURN_EXPR:
8025 : 27260678 : if (TREE_OPERAND (t, 0) != NULL_TREE)
8026 : 27247613 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
8027 : : lval,
8028 : : non_constant_p, overflow_p);
8029 : : /* FALLTHRU */
8030 : 27281443 : case BREAK_STMT:
8031 : 27281443 : case CONTINUE_STMT:
8032 : 27281443 : if (jump_target)
8033 : 27281443 : *jump_target = t;
8034 : : else
8035 : : {
8036 : : /* Can happen with ({ return true; }) && false; passed to
8037 : : maybe_constant_value. There is nothing to jump over in this
8038 : : case, and the bug will be diagnosed later. */
8039 : 0 : gcc_assert (ctx->quiet);
8040 : 0 : *non_constant_p = true;
8041 : : }
8042 : : break;
8043 : :
8044 : 532685 : case SAVE_EXPR:
8045 : : /* Avoid evaluating a SAVE_EXPR more than once. */
8046 : 532685 : if (tree v = ctx->global->get_value (t))
8047 : : r = v;
8048 : : else
8049 : : {
8050 : 526827 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), vc_prvalue,
8051 : : non_constant_p, overflow_p);
8052 : 526827 : if (*non_constant_p)
8053 : : break;
8054 : 5982 : ctx->global->put_value (t, r);
8055 : 5982 : if (ctx->save_exprs)
8056 : 5550 : ctx->save_exprs->safe_push (t);
8057 : : }
8058 : : break;
8059 : :
8060 : 0 : case TRY_CATCH_EXPR:
8061 : 0 : if (TREE_OPERAND (t, 0) == NULL_TREE)
8062 : : {
8063 : 0 : r = void_node;
8064 : 0 : break;
8065 : : }
8066 : : /* FALLTHRU */
8067 : 48266903 : case NON_LVALUE_EXPR:
8068 : 48266903 : case TRY_BLOCK:
8069 : 48266903 : case MUST_NOT_THROW_EXPR:
8070 : 48266903 : case EXPR_STMT:
8071 : 48266903 : case EH_SPEC_BLOCK:
8072 : 48266903 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
8073 : : lval,
8074 : : non_constant_p, overflow_p,
8075 : : jump_target);
8076 : 48266903 : break;
8077 : :
8078 : 34017769 : case CLEANUP_POINT_EXPR:
8079 : 34017769 : {
8080 : 34017769 : auto_vec<tree, 2> cleanups;
8081 : 34017769 : vec<tree> *prev_cleanups = ctx->global->cleanups;
8082 : 34017769 : ctx->global->cleanups = &cleanups;
8083 : :
8084 : 34017769 : auto_vec<tree, 10> save_exprs;
8085 : 34017769 : constexpr_ctx new_ctx = *ctx;
8086 : 34017769 : new_ctx.save_exprs = &save_exprs;
8087 : :
8088 : 34017769 : r = cxx_eval_constant_expression (&new_ctx, TREE_OPERAND (t, 0),
8089 : : lval,
8090 : : non_constant_p, overflow_p,
8091 : : jump_target);
8092 : :
8093 : 34017769 : ctx->global->cleanups = prev_cleanups;
8094 : 34017769 : unsigned int i;
8095 : 34017769 : tree cleanup;
8096 : : /* Evaluate the cleanups. */
8097 : 68220154 : FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup)
8098 : 184616 : cxx_eval_constant_expression (&new_ctx, cleanup, vc_discard,
8099 : : non_constant_p, overflow_p);
8100 : :
8101 : : /* Forget SAVE_EXPRs and TARGET_EXPRs created by this
8102 : : full-expression. */
8103 : 103443456 : for (tree save_expr : save_exprs)
8104 : 1390149 : destroy_value_checked (ctx, save_expr, non_constant_p);
8105 : 34017769 : }
8106 : 34017769 : break;
8107 : :
8108 : 478 : case TRY_FINALLY_EXPR:
8109 : 478 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
8110 : : non_constant_p, overflow_p,
8111 : : jump_target);
8112 : 478 : if (!*non_constant_p)
8113 : : /* Also evaluate the cleanup. */
8114 : 463 : cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), vc_discard,
8115 : : non_constant_p, overflow_p);
8116 : : break;
8117 : :
8118 : 18 : case EH_ELSE_EXPR:
8119 : : /* Evaluate any cleanup that applies to non-EH exits. */
8120 : 18 : cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), vc_discard,
8121 : : non_constant_p, overflow_p);
8122 : :
8123 : : /* We do not have constexpr exceptions yet, so skip the EH path. */
8124 : 18 : break;
8125 : :
8126 : 819379 : case CLEANUP_STMT:
8127 : 819379 : r = cxx_eval_constant_expression (ctx, CLEANUP_BODY (t), lval,
8128 : : non_constant_p, overflow_p,
8129 : : jump_target);
8130 : 819379 : if (!CLEANUP_EH_ONLY (t) && !*non_constant_p)
8131 : : {
8132 : 31586 : iloc_sentinel ils (loc);
8133 : : /* Also evaluate the cleanup. */
8134 : 31586 : cxx_eval_constant_expression (ctx, CLEANUP_EXPR (t), vc_discard,
8135 : : non_constant_p, overflow_p);
8136 : 31586 : }
8137 : : break;
8138 : :
8139 : : /* These differ from cxx_eval_unary_expression in that this doesn't
8140 : : check for a constant operand or result; an address can be
8141 : : constant without its operand being, and vice versa. */
8142 : 37401005 : case MEM_REF:
8143 : 37401005 : case INDIRECT_REF:
8144 : 37401005 : r = cxx_eval_indirect_ref (ctx, t, lval,
8145 : : non_constant_p, overflow_p);
8146 : 37401005 : break;
8147 : :
8148 : 36325503 : case ADDR_EXPR:
8149 : 36325503 : {
8150 : 36325503 : tree oldop = TREE_OPERAND (t, 0);
8151 : 36325503 : tree op = cxx_eval_constant_expression (ctx, oldop, vc_glvalue,
8152 : : non_constant_p, overflow_p);
8153 : : /* Don't VERIFY_CONSTANT here. */
8154 : 36325503 : if (*non_constant_p)
8155 : : return t;
8156 : 33035180 : gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR);
8157 : : /* This function does more aggressive folding than fold itself. */
8158 : 33035180 : r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
8159 : 33035180 : if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
8160 : : {
8161 : 23205183 : ggc_free (r);
8162 : 23205183 : return t;
8163 : : }
8164 : : break;
8165 : : }
8166 : :
8167 : 466339 : case REALPART_EXPR:
8168 : 466339 : case IMAGPART_EXPR:
8169 : 466339 : if (lval)
8170 : : {
8171 : 1052 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
8172 : : non_constant_p, overflow_p);
8173 : 1052 : if (r == error_mark_node)
8174 : : ;
8175 : 1052 : else if (r == TREE_OPERAND (t, 0) || lval == vc_discard)
8176 : : r = t;
8177 : : else
8178 : 1024 : r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), r);
8179 : : break;
8180 : : }
8181 : : /* FALLTHRU */
8182 : 21466588 : case CONJ_EXPR:
8183 : 21466588 : case FIX_TRUNC_EXPR:
8184 : 21466588 : case FLOAT_EXPR:
8185 : 21466588 : case NEGATE_EXPR:
8186 : 21466588 : case ABS_EXPR:
8187 : 21466588 : case ABSU_EXPR:
8188 : 21466588 : case BIT_NOT_EXPR:
8189 : 21466588 : case TRUTH_NOT_EXPR:
8190 : 21466588 : case FIXED_CONVERT_EXPR:
8191 : 21466588 : case VEC_DUPLICATE_EXPR:
8192 : 21466588 : r = cxx_eval_unary_expression (ctx, t, lval,
8193 : : non_constant_p, overflow_p);
8194 : 21466588 : break;
8195 : :
8196 : 8462194 : case SIZEOF_EXPR:
8197 : 8462194 : r = fold_sizeof_expr (t);
8198 : : /* In a template, fold_sizeof_expr may merely create a new SIZEOF_EXPR,
8199 : : which could lead to an infinite recursion. */
8200 : 8462194 : if (TREE_CODE (r) != SIZEOF_EXPR)
8201 : 8462194 : r = cxx_eval_constant_expression (ctx, r, lval,
8202 : : non_constant_p, overflow_p,
8203 : : jump_target);
8204 : : else
8205 : : {
8206 : 0 : *non_constant_p = true;
8207 : 0 : gcc_assert (ctx->quiet);
8208 : : }
8209 : :
8210 : : break;
8211 : :
8212 : 3794057 : case COMPOUND_EXPR:
8213 : 3794057 : {
8214 : : /* check_return_expr sometimes wraps a TARGET_EXPR in a
8215 : : COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
8216 : : introduced by build_call_a. */
8217 : 3794057 : tree op0 = TREE_OPERAND (t, 0);
8218 : 3794057 : tree op1 = TREE_OPERAND (t, 1);
8219 : 3794057 : STRIP_NOPS (op1);
8220 : 1397922 : if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
8221 : 3888823 : || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
8222 : 3410104 : r = cxx_eval_constant_expression (ctx, op0,
8223 : : lval, non_constant_p, overflow_p,
8224 : : jump_target);
8225 : : else
8226 : : {
8227 : : /* Check that the LHS is constant and then discard it. */
8228 : 383953 : cxx_eval_constant_expression (ctx, op0, vc_discard,
8229 : : non_constant_p, overflow_p,
8230 : : jump_target);
8231 : 383953 : if (*non_constant_p)
8232 : : return t;
8233 : 273716 : op1 = TREE_OPERAND (t, 1);
8234 : 273716 : r = cxx_eval_constant_expression (ctx, op1,
8235 : : lval, non_constant_p, overflow_p,
8236 : : jump_target);
8237 : : }
8238 : : }
8239 : : break;
8240 : :
8241 : 47650537 : case POINTER_PLUS_EXPR:
8242 : 47650537 : case POINTER_DIFF_EXPR:
8243 : 47650537 : case PLUS_EXPR:
8244 : 47650537 : case MINUS_EXPR:
8245 : 47650537 : case MULT_EXPR:
8246 : 47650537 : case TRUNC_DIV_EXPR:
8247 : 47650537 : case CEIL_DIV_EXPR:
8248 : 47650537 : case FLOOR_DIV_EXPR:
8249 : 47650537 : case ROUND_DIV_EXPR:
8250 : 47650537 : case TRUNC_MOD_EXPR:
8251 : 47650537 : case CEIL_MOD_EXPR:
8252 : 47650537 : case ROUND_MOD_EXPR:
8253 : 47650537 : case RDIV_EXPR:
8254 : 47650537 : case EXACT_DIV_EXPR:
8255 : 47650537 : case MIN_EXPR:
8256 : 47650537 : case MAX_EXPR:
8257 : 47650537 : case LSHIFT_EXPR:
8258 : 47650537 : case RSHIFT_EXPR:
8259 : 47650537 : case LROTATE_EXPR:
8260 : 47650537 : case RROTATE_EXPR:
8261 : 47650537 : case BIT_IOR_EXPR:
8262 : 47650537 : case BIT_XOR_EXPR:
8263 : 47650537 : case BIT_AND_EXPR:
8264 : 47650537 : case TRUTH_XOR_EXPR:
8265 : 47650537 : case LT_EXPR:
8266 : 47650537 : case LE_EXPR:
8267 : 47650537 : case GT_EXPR:
8268 : 47650537 : case GE_EXPR:
8269 : 47650537 : case EQ_EXPR:
8270 : 47650537 : case NE_EXPR:
8271 : 47650537 : case SPACESHIP_EXPR:
8272 : 47650537 : case UNORDERED_EXPR:
8273 : 47650537 : case ORDERED_EXPR:
8274 : 47650537 : case UNLT_EXPR:
8275 : 47650537 : case UNLE_EXPR:
8276 : 47650537 : case UNGT_EXPR:
8277 : 47650537 : case UNGE_EXPR:
8278 : 47650537 : case UNEQ_EXPR:
8279 : 47650537 : case LTGT_EXPR:
8280 : 47650537 : case RANGE_EXPR:
8281 : 47650537 : case COMPLEX_EXPR:
8282 : 47650537 : r = cxx_eval_binary_expression (ctx, t, lval,
8283 : : non_constant_p, overflow_p);
8284 : 47650537 : break;
8285 : :
8286 : : /* fold can introduce non-IF versions of these; still treat them as
8287 : : short-circuiting. */
8288 : 7355260 : case TRUTH_AND_EXPR:
8289 : 7355260 : case TRUTH_ANDIF_EXPR:
8290 : 7355260 : r = cxx_eval_logical_expression (ctx, t, boolean_false_node,
8291 : : boolean_true_node,
8292 : : non_constant_p, overflow_p);
8293 : 7355260 : break;
8294 : :
8295 : 2604978 : case TRUTH_OR_EXPR:
8296 : 2604978 : case TRUTH_ORIF_EXPR:
8297 : 2604978 : r = cxx_eval_logical_expression (ctx, t, boolean_true_node,
8298 : : boolean_false_node,
8299 : : non_constant_p, overflow_p);
8300 : 2604978 : break;
8301 : :
8302 : 1871869 : case ARRAY_REF:
8303 : 1871869 : r = cxx_eval_array_reference (ctx, t, lval,
8304 : : non_constant_p, overflow_p);
8305 : 1871869 : break;
8306 : :
8307 : 37105943 : case COMPONENT_REF:
8308 : 37105943 : if (is_overloaded_fn (t))
8309 : : {
8310 : : /* We can only get here in checking mode via
8311 : : build_non_dependent_expr, because any expression that
8312 : : calls or takes the address of the function will have
8313 : : pulled a FUNCTION_DECL out of the COMPONENT_REF. */
8314 : 6 : gcc_checking_assert (ctx->quiet || errorcount);
8315 : 6 : *non_constant_p = true;
8316 : 6 : return t;
8317 : : }
8318 : 37105937 : r = cxx_eval_component_reference (ctx, t, lval,
8319 : : non_constant_p, overflow_p);
8320 : 37105937 : break;
8321 : :
8322 : 49 : case BIT_FIELD_REF:
8323 : 49 : r = cxx_eval_bit_field_ref (ctx, t, lval,
8324 : : non_constant_p, overflow_p);
8325 : 49 : break;
8326 : :
8327 : 7503878 : case COND_EXPR:
8328 : 7503878 : case IF_STMT:
8329 : 7503878 : if (jump_target && *jump_target)
8330 : : {
8331 : 151002 : tree orig_jump = *jump_target;
8332 : 151002 : tree arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 1))
8333 : 302004 : ? TREE_OPERAND (t, 1) : void_node);
8334 : : /* When jumping to a label, the label might be either in the
8335 : : then or else blocks, so process then block first in skipping
8336 : : mode first, and if we are still in the skipping mode at its end,
8337 : : process the else block too. */
8338 : 151002 : r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p,
8339 : : overflow_p, jump_target);
8340 : : /* It's possible that we found the label in the then block. But
8341 : : it could have been followed by another jumping statement, e.g.
8342 : : say we're looking for case 1:
8343 : : if (cond)
8344 : : {
8345 : : // skipped statements
8346 : : case 1:; // clears up *jump_target
8347 : : return 1; // and sets it to a RETURN_EXPR
8348 : : }
8349 : : else { ... }
8350 : : in which case we need not go looking to the else block.
8351 : : (goto is not allowed in a constexpr function.) */
8352 : 151002 : if (*jump_target == orig_jump)
8353 : : {
8354 : 150942 : arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 2))
8355 : 301884 : ? TREE_OPERAND (t, 2) : void_node);
8356 : 150942 : r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p,
8357 : : overflow_p, jump_target);
8358 : : }
8359 : : break;
8360 : : }
8361 : 7352876 : r = cxx_eval_conditional_expression (ctx, t, lval,
8362 : : non_constant_p, overflow_p,
8363 : : jump_target);
8364 : 7352876 : break;
8365 : 832 : case VEC_COND_EXPR:
8366 : 832 : r = cxx_eval_vector_conditional_expression (ctx, t, non_constant_p,
8367 : : overflow_p);
8368 : 832 : break;
8369 : :
8370 : 12372452 : case CONSTRUCTOR:
8371 : 12372452 : if (TREE_CONSTANT (t) && reduced_constant_expression_p (t))
8372 : : {
8373 : : /* Don't re-process a constant CONSTRUCTOR. */
8374 : 11170825 : verify_constructor_flags (t);
8375 : 11170825 : if (TREE_CONSTANT (t))
8376 : : return t;
8377 : : }
8378 : 1201627 : r = cxx_eval_bare_aggregate (ctx, t, lval,
8379 : : non_constant_p, overflow_p);
8380 : 1201627 : break;
8381 : :
8382 : 514 : case VEC_INIT_EXPR:
8383 : : /* We can get this in a defaulted constructor for a class with a
8384 : : non-static data member of array type. Either the initializer will
8385 : : be NULL, meaning default-initialization, or it will be an lvalue
8386 : : or xvalue of the same type, meaning direct-initialization from the
8387 : : corresponding member. */
8388 : 514 : r = cxx_eval_vec_init (ctx, t, lval,
8389 : : non_constant_p, overflow_p);
8390 : 514 : break;
8391 : :
8392 : 16 : case VEC_PERM_EXPR:
8393 : 16 : r = cxx_eval_trinary_expression (ctx, t, lval,
8394 : : non_constant_p, overflow_p);
8395 : 16 : break;
8396 : :
8397 : 4 : case PAREN_EXPR:
8398 : 4 : gcc_assert (!REF_PARENTHESIZED_P (t));
8399 : : /* A PAREN_EXPR resulting from __builtin_assoc_barrier has no effect in
8400 : : constant expressions since it's unaffected by -fassociative-math. */
8401 : 4 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
8402 : : non_constant_p, overflow_p);
8403 : 4 : break;
8404 : :
8405 : 177123885 : case NOP_EXPR:
8406 : 177123885 : if (REINTERPRET_CAST_P (t))
8407 : : {
8408 : 33399 : if (!ctx->quiet)
8409 : 8 : error_at (loc,
8410 : : "%<reinterpret_cast%> is not a constant expression");
8411 : 33399 : *non_constant_p = true;
8412 : 33399 : return t;
8413 : : }
8414 : : /* FALLTHROUGH. */
8415 : 211526142 : case CONVERT_EXPR:
8416 : 211526142 : case VIEW_CONVERT_EXPR:
8417 : 211526142 : case UNARY_PLUS_EXPR:
8418 : 211526142 : {
8419 : 211526142 : tree oldop = TREE_OPERAND (t, 0);
8420 : :
8421 : 211526142 : tree op = cxx_eval_constant_expression (ctx, oldop,
8422 : 211526142 : VOID_TYPE_P (TREE_TYPE (t))
8423 : : ? vc_discard
8424 : : : tcode == VIEW_CONVERT_EXPR
8425 : 200421345 : ? lval : vc_prvalue,
8426 : : non_constant_p, overflow_p);
8427 : 211523445 : if (*non_constant_p)
8428 : : return t;
8429 : 175176841 : tree type = TREE_TYPE (t);
8430 : :
8431 : 175176841 : if (VOID_TYPE_P (type))
8432 : 8481840 : return void_node;
8433 : :
8434 : 166695001 : if (TREE_CODE (t) == CONVERT_EXPR
8435 : 18802636 : && ARITHMETIC_TYPE_P (type)
8436 : 2179340 : && INDIRECT_TYPE_P (TREE_TYPE (op))
8437 : 166738681 : && ctx->manifestly_const_eval == mce_true)
8438 : : {
8439 : 228 : if (!ctx->quiet)
8440 : 48 : error_at (loc,
8441 : : "conversion from pointer type %qT to arithmetic type "
8442 : 48 : "%qT in a constant expression", TREE_TYPE (op), type);
8443 : 228 : *non_constant_p = true;
8444 : 228 : return t;
8445 : : }
8446 : :
8447 : : /* [expr.const]: a conversion from type cv void* to a pointer-to-object
8448 : : type cannot be part of a core constant expression as a resolution to
8449 : : DR 1312. */
8450 : 64454284 : if (TYPE_PTROB_P (type)
8451 : 63283780 : && TYPE_PTR_P (TREE_TYPE (op))
8452 : 43632258 : && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (op)))
8453 : : /* Inside a call to std::construct_at,
8454 : : std::allocator<T>::{,de}allocate, or
8455 : : std::source_location::current, we permit casting from void*
8456 : : because that is compiler-generated code. */
8457 : 37151 : && !is_std_construct_at (ctx->call)
8458 : 5814 : && !is_std_allocator_allocate (ctx->call)
8459 : 166699727 : && !is_std_source_location_current (ctx->call))
8460 : : {
8461 : : /* Likewise, don't error when casting from void* when OP is
8462 : : &heap uninit and similar. */
8463 : 4777 : tree sop = tree_strip_nop_conversions (op);
8464 : 4777 : tree decl = NULL_TREE;
8465 : 4777 : if (TREE_CODE (sop) == ADDR_EXPR)
8466 : 2005 : decl = TREE_OPERAND (sop, 0);
8467 : 2005 : if (decl
8468 : 2005 : && VAR_P (decl)
8469 : 1899 : && DECL_ARTIFICIAL (decl)
8470 : 3866 : && (DECL_NAME (decl) == heap_identifier
8471 : 1196 : || DECL_NAME (decl) == heap_uninit_identifier
8472 : 437 : || DECL_NAME (decl) == heap_vec_identifier
8473 : 313 : || DECL_NAME (decl) == heap_vec_uninit_identifier))
8474 : : /* OK */;
8475 : : /* P2738 (C++26): a conversion from a prvalue P of type "pointer to
8476 : : cv void" to a pointer-to-object type T unless P is a null
8477 : : pointer value or points to an object whose type is similar to
8478 : : T. */
8479 : 2932 : else if (cxx_dialect > cxx23)
8480 : : {
8481 : 2816 : if (integer_zerop (sop))
8482 : 43 : return build_int_cst (type, 0);
8483 : 2773 : r = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (type), sop);
8484 : 2773 : if (r)
8485 : : {
8486 : 2759 : r = build1 (ADDR_EXPR, type, r);
8487 : 2759 : break;
8488 : : }
8489 : 14 : if (!ctx->quiet)
8490 : : {
8491 : 3 : gcc_assert (TREE_CODE (sop) == ADDR_EXPR);
8492 : 3 : auto_diagnostic_group d;
8493 : 3 : error_at (loc, "cast from %qT is not allowed in a "
8494 : : "constant expression because "
8495 : : "pointed-to type %qT is not similar to %qT",
8496 : 3 : TREE_TYPE (op), TREE_TYPE (TREE_TYPE (sop)),
8497 : 3 : TREE_TYPE (type));
8498 : 3 : tree obj = build_fold_indirect_ref (sop);
8499 : 3 : if (TREE_CODE (obj) == COMPONENT_REF)
8500 : 2 : obj = TREE_OPERAND (obj, 1);
8501 : 3 : if (DECL_P (obj))
8502 : 3 : inform (DECL_SOURCE_LOCATION (obj),
8503 : : "pointed-to object declared here");
8504 : 3 : }
8505 : 14 : *non_constant_p = true;
8506 : 14 : return t;
8507 : : }
8508 : : else
8509 : : {
8510 : 116 : if (!ctx->quiet)
8511 : 26 : error_at (loc, "cast from %qT is not allowed in a "
8512 : : "constant expression before C++26",
8513 : 26 : TREE_TYPE (op));
8514 : 116 : *non_constant_p = true;
8515 : 116 : return t;
8516 : : }
8517 : : }
8518 : :
8519 : 166691841 : if (TREE_CODE (op) == PTRMEM_CST && !TYPE_PTRMEM_P (type))
8520 : : {
8521 : 1841 : op = cplus_expand_constant (op);
8522 : 1841 : if (TREE_CODE (op) == PTRMEM_CST)
8523 : : {
8524 : 21 : if (!ctx->quiet)
8525 : 3 : error_at (loc, "%qE is not a constant expression when the "
8526 : : "class %qT is still incomplete", op,
8527 : 3 : PTRMEM_CST_CLASS (op));
8528 : 21 : *non_constant_p = true;
8529 : 21 : return t;
8530 : : }
8531 : : }
8532 : :
8533 : 166691820 : if (TREE_CODE (op) == PTRMEM_CST && tcode == NOP_EXPR)
8534 : : {
8535 : 785 : if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (op))
8536 : 785 : && !can_convert_qual (type, op))
8537 : 6 : op = cplus_expand_constant (op);
8538 : 785 : return cp_fold_convert (type, op);
8539 : : }
8540 : :
8541 : 166691035 : if (INDIRECT_TYPE_P (type) && TREE_CODE (op) == INTEGER_CST)
8542 : : {
8543 : 240710 : if (integer_zerop (op))
8544 : : {
8545 : 183802 : if (TYPE_REF_P (type))
8546 : : {
8547 : 29 : if (!ctx->quiet)
8548 : 4 : error_at (loc, "dereferencing a null pointer");
8549 : 29 : *non_constant_p = true;
8550 : 29 : return t;
8551 : : }
8552 : : }
8553 : 56908 : else if (TYPE_PTR_P (type)
8554 : 56908 : && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
8555 : : /* INTEGER_CST with pointer-to-method type is only used
8556 : : for a virtual method in a pointer to member function.
8557 : : Don't reject those. */
8558 : : ;
8559 : : else
8560 : : {
8561 : : /* This detects for example:
8562 : : reinterpret_cast<void*>(sizeof 0)
8563 : : */
8564 : 56902 : if (!ctx->quiet)
8565 : 21 : error_at (loc, "%<reinterpret_cast<%T>(%E)%> is not "
8566 : : "a constant expression",
8567 : : type, op);
8568 : 56902 : *non_constant_p = true;
8569 : 56902 : return t;
8570 : : }
8571 : : }
8572 : :
8573 : 166634104 : if (INDIRECT_TYPE_P (type)
8574 : 94357281 : && TREE_CODE (op) == NOP_EXPR
8575 : 40766930 : && TREE_TYPE (op) == ptr_type_node
8576 : 48167 : && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR
8577 : 35270 : && VAR_P (TREE_OPERAND (TREE_OPERAND (op, 0), 0))
8578 : 166648506 : && (DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0),
8579 : 14402 : 0)) == heap_uninit_identifier
8580 : 12783 : || DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0),
8581 : 12783 : 0)) == heap_vec_uninit_identifier))
8582 : : {
8583 : 1916 : tree var = TREE_OPERAND (TREE_OPERAND (op, 0), 0);
8584 : 1916 : tree var_size = TYPE_SIZE_UNIT (TREE_TYPE (var));
8585 : 1916 : tree elt_type = TREE_TYPE (type);
8586 : 1916 : tree cookie_size = NULL_TREE;
8587 : 1916 : tree arg_size = NULL_TREE;
8588 : 1916 : if (TREE_CODE (elt_type) == RECORD_TYPE
8589 : 1916 : && TYPE_NAME (elt_type) == heap_identifier)
8590 : : {
8591 : 3 : tree fld1 = TYPE_FIELDS (elt_type);
8592 : 3 : tree fld2 = DECL_CHAIN (fld1);
8593 : 3 : elt_type = TREE_TYPE (TREE_TYPE (fld2));
8594 : 3 : cookie_size = TYPE_SIZE_UNIT (TREE_TYPE (fld1));
8595 : : }
8596 : 1916 : DECL_NAME (var)
8597 : 1916 : = (DECL_NAME (var) == heap_uninit_identifier
8598 : 1916 : ? heap_identifier : heap_vec_identifier);
8599 : : /* For zero sized elt_type, try to recover how many outer_nelts
8600 : : it should have. */
8601 : 1916 : if ((cookie_size ? tree_int_cst_equal (var_size, cookie_size)
8602 : 1913 : : integer_zerop (var_size))
8603 : 15 : && !int_size_in_bytes (elt_type)
8604 : 9 : && TREE_CODE (oldop) == CALL_EXPR
8605 : 1928 : && call_expr_nargs (oldop) >= 1)
8606 : 9 : if (tree fun = get_function_named_in_call (oldop))
8607 : 9 : if (cxx_replaceable_global_alloc_fn (fun)
8608 : 9 : && IDENTIFIER_NEW_OP_P (DECL_NAME (fun)))
8609 : 9 : arg_size = CALL_EXPR_ARG (oldop, 0);
8610 : 1916 : TREE_TYPE (var)
8611 : 1916 : = build_new_constexpr_heap_type (ctx, elt_type, cookie_size,
8612 : : var_size, arg_size,
8613 : : non_constant_p, overflow_p);
8614 : 1916 : TREE_TYPE (TREE_OPERAND (op, 0))
8615 : 3832 : = build_pointer_type (TREE_TYPE (var));
8616 : : }
8617 : :
8618 : 166634104 : if (op == oldop && tcode != UNARY_PLUS_EXPR)
8619 : : /* We didn't fold at the top so we could check for ptr-int
8620 : : conversion. */
8621 : 27979666 : return fold (t);
8622 : :
8623 : 138654438 : tree sop;
8624 : :
8625 : : /* Handle an array's bounds having been deduced after we built
8626 : : the wrapping expression. */
8627 : 138654438 : if (same_type_ignoring_tlq_and_bounds_p (type, TREE_TYPE (op)))
8628 : : r = op;
8629 : 45626844 : else if (sop = tree_strip_nop_conversions (op),
8630 : 66053071 : sop != op && (same_type_ignoring_tlq_and_bounds_p
8631 : 20426227 : (type, TREE_TYPE (sop))))
8632 : : r = sop;
8633 : 36829552 : else if (tcode == UNARY_PLUS_EXPR)
8634 : 0 : r = fold_convert (TREE_TYPE (t), op);
8635 : : else
8636 : 36829552 : r = fold_build1 (tcode, type, op);
8637 : :
8638 : : /* Conversion of an out-of-range value has implementation-defined
8639 : : behavior; the language considers it different from arithmetic
8640 : : overflow, which is undefined. */
8641 : 138654438 : if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op))
8642 : 10917 : TREE_OVERFLOW (r) = false;
8643 : : }
8644 : : break;
8645 : :
8646 : 5754 : case EXCESS_PRECISION_EXPR:
8647 : 5754 : {
8648 : 5754 : tree oldop = TREE_OPERAND (t, 0);
8649 : :
8650 : 5754 : tree op = cxx_eval_constant_expression (ctx, oldop,
8651 : : lval,
8652 : : non_constant_p, overflow_p);
8653 : 5754 : if (*non_constant_p)
8654 : : return t;
8655 : 5754 : r = fold_convert (TREE_TYPE (t), op);
8656 : 5754 : break;
8657 : : }
8658 : :
8659 : 71673 : case EMPTY_CLASS_EXPR:
8660 : : /* Handle EMPTY_CLASS_EXPR produced by build_call_a by lowering
8661 : : it to an appropriate CONSTRUCTOR. */
8662 : 71673 : return build_constructor (TREE_TYPE (t), NULL);
8663 : :
8664 : 23261463 : case STATEMENT_LIST:
8665 : 23261463 : new_ctx = *ctx;
8666 : 23261463 : new_ctx.ctor = new_ctx.object = NULL_TREE;
8667 : 23261463 : return cxx_eval_statement_list (&new_ctx, t,
8668 : 23261463 : non_constant_p, overflow_p, jump_target);
8669 : :
8670 : 11860770 : case BIND_EXPR:
8671 : : /* Pre-emptively clear the vars declared by this BIND_EXPR from the value
8672 : : map, so that when checking whether they're already destroyed later we
8673 : : don't get confused by remnants of previous calls. */
8674 : 16699192 : for (tree decl = BIND_EXPR_VARS (t); decl; decl = DECL_CHAIN (decl))
8675 : 4838422 : ctx->global->clear_value (decl);
8676 : 11860770 : r = cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t),
8677 : : lval,
8678 : : non_constant_p, overflow_p,
8679 : : jump_target);
8680 : 16699192 : for (tree decl = BIND_EXPR_VARS (t); decl; decl = DECL_CHAIN (decl))
8681 : 4838422 : destroy_value_checked (ctx, decl, non_constant_p);
8682 : : break;
8683 : :
8684 : 1817590 : case PREINCREMENT_EXPR:
8685 : 1817590 : case POSTINCREMENT_EXPR:
8686 : 1817590 : case PREDECREMENT_EXPR:
8687 : 1817590 : case POSTDECREMENT_EXPR:
8688 : 1817590 : return cxx_eval_increment_expression (ctx, t,
8689 : 1817590 : lval, non_constant_p, overflow_p);
8690 : :
8691 : 413 : case LAMBDA_EXPR:
8692 : 413 : case NEW_EXPR:
8693 : 413 : case VEC_NEW_EXPR:
8694 : 413 : case DELETE_EXPR:
8695 : 413 : case VEC_DELETE_EXPR:
8696 : 413 : case THROW_EXPR:
8697 : 413 : case MODOP_EXPR:
8698 : : /* GCC internal stuff. */
8699 : 413 : case VA_ARG_EXPR:
8700 : 413 : case BASELINK:
8701 : 413 : case OFFSET_REF:
8702 : 413 : if (!ctx->quiet)
8703 : 102 : error_at (loc, "expression %qE is not a constant expression", t);
8704 : 413 : *non_constant_p = true;
8705 : 413 : break;
8706 : :
8707 : 81465 : case OBJ_TYPE_REF:
8708 : : /* Virtual function lookup. We don't need to do anything fancy. */
8709 : 81465 : return cxx_eval_constant_expression (ctx, OBJ_TYPE_REF_EXPR (t),
8710 : 81465 : lval, non_constant_p, overflow_p);
8711 : :
8712 : 16134 : case PLACEHOLDER_EXPR:
8713 : : /* Use of the value or address of the current object. */
8714 : 16134 : if (tree ctor = lookup_placeholder (ctx, lval, TREE_TYPE (t)))
8715 : : {
8716 : 15902 : if (TREE_CODE (ctor) == CONSTRUCTOR)
8717 : : return ctor;
8718 : : else
8719 : 15590 : return cxx_eval_constant_expression (ctx, ctor, lval,
8720 : 15590 : non_constant_p, overflow_p);
8721 : : }
8722 : : /* A placeholder without a referent. We can get here when
8723 : : checking whether NSDMIs are noexcept, or in massage_init_elt;
8724 : : just say it's non-constant for now. */
8725 : 232 : gcc_assert (ctx->quiet);
8726 : 232 : *non_constant_p = true;
8727 : 232 : break;
8728 : :
8729 : 165 : case EXIT_EXPR:
8730 : 165 : {
8731 : 165 : tree cond = TREE_OPERAND (t, 0);
8732 : 165 : cond = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
8733 : : non_constant_p, overflow_p);
8734 : 165 : VERIFY_CONSTANT (cond);
8735 : 165 : if (integer_nonzerop (cond))
8736 : 35 : *jump_target = t;
8737 : : }
8738 : : break;
8739 : :
8740 : 9 : case GOTO_EXPR:
8741 : 9 : if (breaks (&TREE_OPERAND (t, 0))
8742 : 9 : || continues (&TREE_OPERAND (t, 0)))
8743 : 3 : *jump_target = TREE_OPERAND (t, 0);
8744 : : else
8745 : : {
8746 : 6 : if (!ctx->quiet)
8747 : 1 : error_at (loc, "%<goto%> is not a constant expression");
8748 : 6 : *non_constant_p = true;
8749 : : }
8750 : : break;
8751 : :
8752 : 481235 : case LOOP_EXPR:
8753 : 481235 : case DO_STMT:
8754 : 481235 : case WHILE_STMT:
8755 : 481235 : case FOR_STMT:
8756 : 481235 : cxx_eval_loop_expr (ctx, t,
8757 : : non_constant_p, overflow_p, jump_target);
8758 : 481235 : break;
8759 : :
8760 : 25769 : case SWITCH_EXPR:
8761 : 25769 : case SWITCH_STMT:
8762 : 25769 : cxx_eval_switch_expr (ctx, t,
8763 : : non_constant_p, overflow_p, jump_target);
8764 : 25769 : break;
8765 : :
8766 : 92 : case REQUIRES_EXPR:
8767 : : /* It's possible to get a requires-expression in a constant
8768 : : expression. For example:
8769 : :
8770 : : template<typename T> concept bool C() {
8771 : : return requires (T t) { t; };
8772 : : }
8773 : :
8774 : : template<typename T> requires !C<T>() void f(T);
8775 : :
8776 : : Normalization leaves f with the associated constraint
8777 : : '!requires (T t) { ... }' which is not transformed into
8778 : : a constraint. */
8779 : 92 : if (!processing_template_decl)
8780 : 92 : return evaluate_requires_expr (t);
8781 : : else
8782 : 0 : *non_constant_p = true;
8783 : 0 : return t;
8784 : :
8785 : 7062 : case ANNOTATE_EXPR:
8786 : 7062 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
8787 : : lval,
8788 : : non_constant_p, overflow_p,
8789 : : jump_target);
8790 : 7062 : break;
8791 : :
8792 : 12208 : case USING_STMT:
8793 : 12208 : r = void_node;
8794 : 12208 : break;
8795 : :
8796 : 106 : case ASSERTION_STMT:
8797 : 106 : case PRECONDITION_STMT:
8798 : 106 : case POSTCONDITION_STMT:
8799 : 106 : {
8800 : 106 : contract_semantic semantic = get_contract_semantic (t);
8801 : 106 : if (semantic == CCS_IGNORE)
8802 : : break;
8803 : :
8804 : 90 : if (!cxx_eval_assert (ctx, CONTRACT_CONDITION (t),
8805 : : G_("contract predicate is false in "
8806 : : "constant expression"),
8807 : 90 : EXPR_LOCATION (t), checked_contract_p (semantic),
8808 : : non_constant_p, overflow_p))
8809 : 38 : *non_constant_p = true;
8810 : 90 : r = void_node;
8811 : : }
8812 : 90 : break;
8813 : :
8814 : 609860 : case TEMPLATE_ID_EXPR:
8815 : 609860 : {
8816 : : /* We can evaluate template-id that refers to a concept only if
8817 : : the template arguments are non-dependent. */
8818 : 609860 : gcc_assert (concept_check_p (t));
8819 : :
8820 : 609860 : if (!value_dependent_expression_p (t)
8821 : 609860 : && !uid_sensitive_constexpr_evaluation_p ())
8822 : 609814 : r = evaluate_concept_check (t);
8823 : : else
8824 : 46 : *non_constant_p = true;
8825 : :
8826 : : break;
8827 : : }
8828 : :
8829 : 48 : case ASM_EXPR:
8830 : 48 : if (!ctx->quiet)
8831 : 28 : inline_asm_in_constexpr_error (loc, /*constexpr_fundef_p*/false);
8832 : 48 : *non_constant_p = true;
8833 : 48 : return t;
8834 : :
8835 : 1316 : case BIT_CAST_EXPR:
8836 : 1316 : if (lval)
8837 : : {
8838 : 0 : if (!ctx->quiet)
8839 : 0 : error_at (EXPR_LOCATION (t),
8840 : : "address of a call to %qs is not a constant expression",
8841 : : "__builtin_bit_cast");
8842 : 0 : *non_constant_p = true;
8843 : 0 : return t;
8844 : : }
8845 : 1316 : r = cxx_eval_bit_cast (ctx, t, non_constant_p, overflow_p);
8846 : 1316 : break;
8847 : :
8848 : 0 : case OMP_PARALLEL:
8849 : 0 : case OMP_TASK:
8850 : 0 : case OMP_FOR:
8851 : 0 : case OMP_SIMD:
8852 : 0 : case OMP_DISTRIBUTE:
8853 : 0 : case OMP_TASKLOOP:
8854 : 0 : case OMP_LOOP:
8855 : 0 : case OMP_TEAMS:
8856 : 0 : case OMP_TARGET_DATA:
8857 : 0 : case OMP_TARGET:
8858 : 0 : case OMP_SECTIONS:
8859 : 0 : case OMP_ORDERED:
8860 : 0 : case OMP_CRITICAL:
8861 : 0 : case OMP_SINGLE:
8862 : 0 : case OMP_SCAN:
8863 : 0 : case OMP_SCOPE:
8864 : 0 : case OMP_SECTION:
8865 : 0 : case OMP_STRUCTURED_BLOCK:
8866 : 0 : case OMP_MASTER:
8867 : 0 : case OMP_MASKED:
8868 : 0 : case OMP_TASKGROUP:
8869 : 0 : case OMP_TARGET_UPDATE:
8870 : 0 : case OMP_TARGET_ENTER_DATA:
8871 : 0 : case OMP_TARGET_EXIT_DATA:
8872 : 0 : case OMP_ATOMIC:
8873 : 0 : case OMP_ATOMIC_READ:
8874 : 0 : case OMP_ATOMIC_CAPTURE_OLD:
8875 : 0 : case OMP_ATOMIC_CAPTURE_NEW:
8876 : 0 : case OMP_DEPOBJ:
8877 : 0 : case OACC_PARALLEL:
8878 : 0 : case OACC_KERNELS:
8879 : 0 : case OACC_SERIAL:
8880 : 0 : case OACC_DATA:
8881 : 0 : case OACC_HOST_DATA:
8882 : 0 : case OACC_LOOP:
8883 : 0 : case OACC_CACHE:
8884 : 0 : case OACC_DECLARE:
8885 : 0 : case OACC_ENTER_DATA:
8886 : 0 : case OACC_EXIT_DATA:
8887 : 0 : case OACC_UPDATE:
8888 : 0 : if (!ctx->quiet)
8889 : 0 : error_at (EXPR_LOCATION (t),
8890 : : "statement is not a constant expression");
8891 : 0 : *non_constant_p = true;
8892 : 0 : break;
8893 : :
8894 : 0 : default:
8895 : 0 : if (STATEMENT_CODE_P (TREE_CODE (t)))
8896 : : {
8897 : : /* This function doesn't know how to deal with pre-genericize
8898 : : statements; this can only happen with statement-expressions,
8899 : : so for now just fail. */
8900 : 0 : if (!ctx->quiet)
8901 : 0 : error_at (EXPR_LOCATION (t),
8902 : : "statement is not a constant expression");
8903 : : }
8904 : 0 : else if (flag_checking)
8905 : 0 : internal_error ("unexpected expression %qE of kind %s", t,
8906 : : get_tree_code_name (TREE_CODE (t)));
8907 : 0 : *non_constant_p = true;
8908 : 0 : break;
8909 : : }
8910 : :
8911 : 789707188 : if (r == error_mark_node)
8912 : 19342329 : *non_constant_p = true;
8913 : :
8914 : 789707188 : if (*non_constant_p)
8915 : 240968627 : return t;
8916 : : else
8917 : : return r;
8918 : : }
8919 : :
8920 : : /* P0859: A function is needed for constant evaluation if it is a constexpr
8921 : : function that is named by an expression ([basic.def.odr]) that is
8922 : : potentially constant evaluated.
8923 : :
8924 : : So we need to instantiate any constexpr functions mentioned by the
8925 : : expression even if the definition isn't needed for evaluating the
8926 : : expression. */
8927 : :
8928 : : static tree
8929 : 352213214 : instantiate_cx_fn_r (tree *tp, int *walk_subtrees, void */*data*/)
8930 : : {
8931 : 352213214 : if (TREE_CODE (*tp) == FUNCTION_DECL
8932 : 9521459 : && DECL_DECLARED_CONSTEXPR_P (*tp)
8933 : 9421719 : && !DECL_INITIAL (*tp)
8934 : 1565571 : && !trivial_fn_p (*tp)
8935 : 1565568 : && (DECL_TEMPLOID_INSTANTIATION (*tp) || DECL_DEFAULTED_FN (*tp))
8936 : 352213214 : && !uid_sensitive_constexpr_evaluation_p ())
8937 : : {
8938 : 1565508 : ++function_depth;
8939 : 1565508 : if (DECL_TEMPLOID_INSTANTIATION (*tp))
8940 : 1565503 : instantiate_decl (*tp, /*defer_ok*/false, /*expl_inst*/false);
8941 : : else
8942 : 5 : synthesize_method (*tp);
8943 : 1565508 : --function_depth;
8944 : : }
8945 : 350647706 : else if (TREE_CODE (*tp) == CALL_EXPR
8946 : 341220205 : || TREE_CODE (*tp) == AGGR_INIT_EXPR)
8947 : : {
8948 : 9570561 : if (EXPR_HAS_LOCATION (*tp))
8949 : 9570458 : input_location = EXPR_LOCATION (*tp);
8950 : : }
8951 : :
8952 : 352213214 : if (!EXPR_P (*tp))
8953 : 210312063 : *walk_subtrees = 0;
8954 : :
8955 : 352213214 : return NULL_TREE;
8956 : : }
8957 : :
8958 : : static void
8959 : 180566289 : instantiate_constexpr_fns (tree t)
8960 : : {
8961 : 180566289 : location_t loc = input_location;
8962 : 180566289 : cp_walk_tree_without_duplicates (&t, instantiate_cx_fn_r, NULL);
8963 : 180566289 : input_location = loc;
8964 : 180566289 : }
8965 : :
8966 : : /* Look for heap variables in the expression *TP. */
8967 : :
8968 : : static tree
8969 : 62440 : find_heap_var_refs (tree *tp, int *walk_subtrees, void */*data*/)
8970 : : {
8971 : 62440 : if (VAR_P (*tp)
8972 : 62440 : && (DECL_NAME (*tp) == heap_uninit_identifier
8973 : 480 : || DECL_NAME (*tp) == heap_identifier
8974 : 245 : || DECL_NAME (*tp) == heap_vec_uninit_identifier
8975 : 201 : || DECL_NAME (*tp) == heap_vec_identifier
8976 : 20 : || DECL_NAME (*tp) == heap_deleted_identifier))
8977 : : return *tp;
8978 : :
8979 : 42212 : if (TYPE_P (*tp))
8980 : 0 : *walk_subtrees = 0;
8981 : : return NULL_TREE;
8982 : : }
8983 : :
8984 : : /* Look for deleted heap variables in the expression *TP. */
8985 : :
8986 : : static tree
8987 : 400 : find_deleted_heap_var (tree *tp, int *walk_subtrees, void */*data*/)
8988 : : {
8989 : 400 : if (VAR_P (*tp)
8990 : 400 : && DECL_NAME (*tp) == heap_deleted_identifier)
8991 : : return *tp;
8992 : :
8993 : 388 : if (TYPE_P (*tp))
8994 : 0 : *walk_subtrees = 0;
8995 : : return NULL_TREE;
8996 : : }
8997 : :
8998 : : /* Find immediate function decls in *TP if any. */
8999 : :
9000 : : static tree
9001 : 143428622 : find_immediate_fndecl (tree *tp, int */*walk_subtrees*/, void */*data*/)
9002 : : {
9003 : 144593938 : if (TREE_CODE (*tp) == FUNCTION_DECL && DECL_IMMEDIATE_FUNCTION_P (*tp))
9004 : : return *tp;
9005 : 143428226 : if (TREE_CODE (*tp) == PTRMEM_CST
9006 : 7065 : && TREE_CODE (PTRMEM_CST_MEMBER (*tp)) == FUNCTION_DECL
9007 : 143434746 : && DECL_IMMEDIATE_FUNCTION_P (PTRMEM_CST_MEMBER (*tp)))
9008 : : return PTRMEM_CST_MEMBER (*tp);
9009 : : return NULL_TREE;
9010 : : }
9011 : :
9012 : : /* T has TREE_CONSTANT set but has been deemed not a valid C++ constant
9013 : : expression. Return a version of T that has TREE_CONSTANT cleared. */
9014 : :
9015 : : static tree
9016 : 109034 : mark_non_constant (tree t)
9017 : : {
9018 : 109034 : gcc_checking_assert (TREE_CONSTANT (t));
9019 : :
9020 : : /* This isn't actually constant, so unset TREE_CONSTANT.
9021 : : Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires
9022 : : it to be set if it is invariant address, even when it is not
9023 : : a valid C++ constant expression. Wrap it with a NOP_EXPR
9024 : : instead. */
9025 : 109034 : if (EXPR_P (t) && TREE_CODE (t) != ADDR_EXPR)
9026 : 108218 : t = copy_node (t);
9027 : 816 : else if (TREE_CODE (t) == CONSTRUCTOR)
9028 : 234 : t = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (t), t);
9029 : : else
9030 : 582 : t = build_nop (TREE_TYPE (t), t);
9031 : 109034 : TREE_CONSTANT (t) = false;
9032 : 109034 : return t;
9033 : : }
9034 : :
9035 : : /* ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
9036 : : STRICT has the same sense as for constant_value_1: true if we only allow
9037 : : conforming C++ constant expressions, or false if we want a constant value
9038 : : even if it doesn't conform.
9039 : : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
9040 : : per P0595 even when ALLOW_NON_CONSTANT is true.
9041 : : CONSTEXPR_DTOR is true when evaluating the dtor of a constexpr variable.
9042 : : OBJECT must be non-NULL in that case. */
9043 : :
9044 : : static tree
9045 : 336230034 : cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant,
9046 : : bool strict = true,
9047 : : mce_value manifestly_const_eval = mce_unknown,
9048 : : bool constexpr_dtor = false,
9049 : : tree object = NULL_TREE)
9050 : : {
9051 : 336230034 : auto_timevar time (TV_CONSTEXPR);
9052 : :
9053 : 336230034 : bool non_constant_p = false;
9054 : 336230034 : bool overflow_p = false;
9055 : :
9056 : 336230034 : if (BRACE_ENCLOSED_INITIALIZER_P (t))
9057 : : {
9058 : 0 : gcc_checking_assert (allow_non_constant);
9059 : : return t;
9060 : : }
9061 : :
9062 : 336230034 : constexpr_global_ctx global_ctx;
9063 : 336230034 : constexpr_ctx ctx = { &global_ctx, NULL, NULL, NULL, NULL, NULL, NULL,
9064 : : allow_non_constant, strict,
9065 : 336230034 : !allow_non_constant ? mce_true : manifestly_const_eval };
9066 : :
9067 : : /* Turn off -frounding-math for manifestly constant evaluation. */
9068 : 336230034 : warning_sentinel rm (flag_rounding_math,
9069 : 336230034 : ctx.manifestly_const_eval == mce_true);
9070 : 336230034 : tree type = (object
9071 : 336230034 : ? cv_unqualified (TREE_TYPE (object))
9072 : 317032806 : : initialized_type (t));
9073 : 336230034 : tree r = t;
9074 : 336230034 : bool is_consteval = false;
9075 : 336230034 : if (VOID_TYPE_P (type))
9076 : : {
9077 : 2670252 : if (!constexpr_dtor)
9078 : : {
9079 : 2670252 : if (cxx_dialect < cxx20)
9080 : : return t;
9081 : 2350656 : if (TREE_CODE (t) != CALL_EXPR && TREE_CODE (t) != AGGR_INIT_EXPR)
9082 : : return t;
9083 : : /* Calls to immediate functions returning void need to be
9084 : : evaluated. */
9085 : 2350645 : tree fndecl = cp_get_callee_fndecl_nofold (t);
9086 : 4701290 : if (fndecl == NULL_TREE || !DECL_IMMEDIATE_FUNCTION_P (fndecl))
9087 : : return t;
9088 : : else
9089 : : is_consteval = true;
9090 : : }
9091 : : }
9092 : 333559782 : else if (cxx_dialect >= cxx20
9093 : 127256954 : && (TREE_CODE (t) == CALL_EXPR
9094 : 108649529 : || TREE_CODE (t) == AGGR_INIT_EXPR
9095 : 107593135 : || TREE_CODE (t) == TARGET_EXPR))
9096 : : {
9097 : 20954145 : tree x = t;
9098 : 20954145 : if (TREE_CODE (x) == TARGET_EXPR)
9099 : 1290326 : x = TARGET_EXPR_INITIAL (x);
9100 : 20954145 : tree fndecl = cp_get_callee_fndecl_nofold (x);
9101 : 41202627 : if (fndecl && DECL_IMMEDIATE_FUNCTION_P (fndecl))
9102 : : is_consteval = true;
9103 : : }
9104 : 333559821 : if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
9105 : : {
9106 : : /* In C++14 an NSDMI can participate in aggregate initialization,
9107 : : and can refer to the address of the object being initialized, so
9108 : : we need to pass in the relevant VAR_DECL if we want to do the
9109 : : evaluation in a single pass. The evaluation will dynamically
9110 : : update ctx.values for the VAR_DECL. We use the same strategy
9111 : : for C++11 constexpr constructors that refer to the object being
9112 : : initialized. */
9113 : 20543824 : if (constexpr_dtor)
9114 : : {
9115 : 131 : gcc_assert (object && VAR_P (object));
9116 : 131 : gcc_assert (DECL_DECLARED_CONSTEXPR_P (object));
9117 : 131 : gcc_assert (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object));
9118 : 131 : if (error_operand_p (DECL_INITIAL (object)))
9119 : : return t;
9120 : 122 : ctx.ctor = unshare_expr (DECL_INITIAL (object));
9121 : 122 : TREE_READONLY (ctx.ctor) = false;
9122 : : /* Temporarily force decl_really_constant_value to return false
9123 : : for it, we want to use ctx.ctor for the current value instead. */
9124 : 122 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = false;
9125 : : }
9126 : : else
9127 : : {
9128 : 20543693 : ctx.ctor = build_constructor (type, NULL);
9129 : 20543693 : CONSTRUCTOR_NO_CLEARING (ctx.ctor) = true;
9130 : : }
9131 : 20543815 : if (!object)
9132 : : {
9133 : 11661479 : if (TREE_CODE (t) == CALL_EXPR)
9134 : : {
9135 : : /* If T is calling a constructor to initialize an object, reframe
9136 : : it as an AGGR_INIT_EXPR to avoid trying to modify an object
9137 : : from outside the constant evaluation, which will fail even if
9138 : : the value is actually constant (is_constant_evaluated3.C). */
9139 : 4308394 : tree fn = cp_get_callee_fndecl_nofold (t);
9140 : 8616780 : if (fn && DECL_CONSTRUCTOR_P (fn))
9141 : : {
9142 : 1674043 : object = CALL_EXPR_ARG (t, 0);
9143 : 1674043 : object = build_fold_indirect_ref (object);
9144 : 1674043 : r = build_aggr_init_expr (type, r);
9145 : : }
9146 : : }
9147 : 7353085 : else if (TREE_CODE (t) == TARGET_EXPR)
9148 : 2727876 : object = TARGET_EXPR_SLOT (t);
9149 : 4625209 : else if (TREE_CODE (t) == AGGR_INIT_EXPR)
9150 : 395936 : object = AGGR_INIT_EXPR_SLOT (t);
9151 : : }
9152 : 20543815 : ctx.object = object;
9153 : 20543815 : if (object)
9154 : 13680191 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
9155 : : (type, TREE_TYPE (object)));
9156 : 13680191 : if (object && DECL_P (object))
9157 : 11916930 : global_ctx.put_value (object, ctx.ctor);
9158 : 20543815 : if (TREE_CODE (r) == TARGET_EXPR)
9159 : : /* Avoid creating another CONSTRUCTOR when we expand the
9160 : : TARGET_EXPR. */
9161 : 2775003 : r = TARGET_EXPR_INITIAL (r);
9162 : : }
9163 : :
9164 : 333559812 : auto_vec<tree, 16> cleanups;
9165 : 333559812 : global_ctx.cleanups = &cleanups;
9166 : :
9167 : 333559812 : if (manifestly_const_eval == mce_true)
9168 : 180566289 : instantiate_constexpr_fns (r);
9169 : 333559812 : r = cxx_eval_constant_expression (&ctx, r, vc_prvalue,
9170 : : &non_constant_p, &overflow_p);
9171 : :
9172 : : /* If we got a non-simple TARGET_EXPR, the initializer was a sequence
9173 : : of statements, and the result ought to be stored in ctx.ctor. */
9174 : 333557115 : if (r == void_node && !constexpr_dtor && ctx.ctor)
9175 : 8 : r = ctx.ctor;
9176 : :
9177 : 333557011 : if (!constexpr_dtor)
9178 : 333556993 : verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p);
9179 : : else
9180 : 122 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = true;
9181 : :
9182 : 333557115 : unsigned int i;
9183 : 333557115 : tree cleanup;
9184 : : /* Evaluate the cleanups. */
9185 : 667172283 : FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup)
9186 : 58053 : cxx_eval_constant_expression (&ctx, cleanup, vc_discard,
9187 : : &non_constant_p, &overflow_p);
9188 : :
9189 : : /* Mutable logic is a bit tricky: we want to allow initialization of
9190 : : constexpr variables with mutable members, but we can't copy those
9191 : : members to another constexpr variable. */
9192 : 333557115 : if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_MUTABLE_POISON (r))
9193 : : {
9194 : 93 : if (!allow_non_constant)
9195 : 6 : error ("%qE is not a constant expression because it refers to "
9196 : : "mutable subobjects of %qT", t, type);
9197 : 93 : non_constant_p = true;
9198 : : }
9199 : :
9200 : 333557115 : if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_NO_CLEARING (r))
9201 : : {
9202 : 84545 : if (!allow_non_constant)
9203 : 37 : error ("%qE is not a constant expression because it refers to "
9204 : : "an incompletely initialized variable", t);
9205 : 84545 : TREE_CONSTANT (r) = false;
9206 : 84545 : non_constant_p = true;
9207 : : }
9208 : :
9209 : 252211447 : if (!non_constant_p && cxx_dialect >= cxx20
9210 : 585768562 : && !global_ctx.heap_vars.is_empty ())
9211 : : {
9212 : 20681 : tree heap_var = cp_walk_tree_without_duplicates (&r, find_heap_var_refs,
9213 : : NULL);
9214 : 20681 : unsigned int i;
9215 : 20681 : if (heap_var)
9216 : : {
9217 : 20228 : if (!allow_non_constant && !non_constant_p)
9218 : 9 : error_at (DECL_SOURCE_LOCATION (heap_var),
9219 : : "%qE is not a constant expression because it refers to "
9220 : : "a result of %<operator new%>", t);
9221 : 20228 : r = t;
9222 : 20228 : non_constant_p = true;
9223 : : }
9224 : 42296 : FOR_EACH_VEC_ELT (global_ctx.heap_vars, i, heap_var)
9225 : : {
9226 : 21615 : if (DECL_NAME (heap_var) != heap_deleted_identifier)
9227 : : {
9228 : 20301 : if (!allow_non_constant && !non_constant_p)
9229 : 13 : error_at (DECL_SOURCE_LOCATION (heap_var),
9230 : : "%qE is not a constant expression because allocated "
9231 : : "storage has not been deallocated", t);
9232 : 20301 : r = t;
9233 : 20301 : non_constant_p = true;
9234 : : }
9235 : 21615 : varpool_node::get (heap_var)->remove ();
9236 : : }
9237 : : }
9238 : :
9239 : : /* Check that immediate invocation does not return an expression referencing
9240 : : any immediate function decls. */
9241 : 333557115 : if (!non_constant_p && cxx_dialect >= cxx20)
9242 : 185641808 : if (tree immediate_fndecl
9243 : 92820904 : = cp_walk_tree_without_duplicates (&r, find_immediate_fndecl,
9244 : : NULL))
9245 : : {
9246 : 501 : if (!allow_non_constant && !non_constant_p)
9247 : : {
9248 : 72 : if (is_consteval)
9249 : 36 : error_at (cp_expr_loc_or_input_loc (t),
9250 : : "immediate evaluation returns address of immediate "
9251 : : "function %qD", immediate_fndecl);
9252 : : else
9253 : 54 : error_at (cp_expr_loc_or_input_loc (t),
9254 : : "constant evaluation returns address of immediate "
9255 : : "function %qD", immediate_fndecl);
9256 : : }
9257 : 501 : r = t;
9258 : 501 : non_constant_p = true;
9259 : : }
9260 : :
9261 : 333557115 : if (non_constant_p)
9262 : : /* If we saw something bad, go back to our argument. The wrapping below is
9263 : : only for the cases of TREE_CONSTANT argument or overflow. */
9264 : 81366487 : r = t;
9265 : :
9266 : 333557115 : if (!non_constant_p && overflow_p)
9267 : 211 : non_constant_p = true;
9268 : :
9269 : : /* Unshare the result. */
9270 : 333557115 : bool should_unshare = true;
9271 : 333557115 : if (r == t || (TREE_CODE (t) == TARGET_EXPR
9272 : 946058 : && TARGET_EXPR_INITIAL (t) == r))
9273 : : should_unshare = false;
9274 : :
9275 : 333557115 : if (non_constant_p && !allow_non_constant)
9276 : 2098 : return error_mark_node;
9277 : 333555017 : else if (constexpr_dtor)
9278 : : return r;
9279 : 333554913 : else if (non_constant_p && TREE_CONSTANT (r))
9280 : 104782 : r = mark_non_constant (r);
9281 : 333450131 : else if (non_constant_p)
9282 : : return t;
9283 : :
9284 : : /* Check we are not trying to return the wrong type. */
9285 : 252295095 : if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (r)))
9286 : : {
9287 : : /* If so, this is not a constant expression. */
9288 : 95 : if (!allow_non_constant)
9289 : 0 : error ("%qE is not a constant expression because it initializes "
9290 : 0 : "a %qT rather than %qT", t, TREE_TYPE (t), type);
9291 : 95 : return t;
9292 : : }
9293 : :
9294 : 252295000 : if (should_unshare)
9295 : 126076516 : r = unshare_expr (r);
9296 : :
9297 : 252295000 : if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
9298 : : {
9299 : 8564643 : r = adjust_temp_type (type, r);
9300 : 8564643 : if (TREE_CODE (t) == TARGET_EXPR
9301 : 8564643 : && TARGET_EXPR_INITIAL (t) == r)
9302 : : return t;
9303 : 7764929 : else if (TREE_CODE (t) == CONSTRUCTOR || TREE_CODE (t) == CALL_EXPR)
9304 : : /* Don't add a TARGET_EXPR if our argument didn't have one. */;
9305 : 890201 : else if (TREE_CODE (t) == TARGET_EXPR && TARGET_EXPR_CLEANUP (t))
9306 : 2379 : r = get_target_expr (r);
9307 : : else
9308 : : {
9309 : 887822 : r = get_target_expr (r, tf_warning_or_error | tf_no_cleanup);
9310 : 887822 : TREE_CONSTANT (r) = true;
9311 : : }
9312 : : }
9313 : :
9314 : 251495286 : if (TREE_CODE (t) == TARGET_EXPR
9315 : 146344 : && TREE_CODE (r) == TARGET_EXPR)
9316 : : {
9317 : : /* Preserve this flag for potential_constant_expression, and the others
9318 : : for good measure. */
9319 : 146283 : TARGET_EXPR_ELIDING_P (r) = TARGET_EXPR_ELIDING_P (t);
9320 : 146283 : TARGET_EXPR_IMPLICIT_P (r) = TARGET_EXPR_IMPLICIT_P (t);
9321 : 146283 : TARGET_EXPR_LIST_INIT_P (r) = TARGET_EXPR_LIST_INIT_P (t);
9322 : 146283 : TARGET_EXPR_DIRECT_INIT_P (r) = TARGET_EXPR_DIRECT_INIT_P (t);
9323 : : }
9324 : :
9325 : : /* Remember the original location if that wouldn't need a wrapper. */
9326 : 251495286 : if (location_t loc = EXPR_LOCATION (t))
9327 : 101483220 : protected_set_expr_location (r, loc);
9328 : :
9329 : 251495286 : return r;
9330 : 672454674 : }
9331 : :
9332 : : /* If T represents a constant expression returns its reduced value.
9333 : : Otherwise return error_mark_node. */
9334 : :
9335 : : tree
9336 : 114828975 : cxx_constant_value (tree t, tree decl /* = NULL_TREE */,
9337 : : tsubst_flags_t complain /* = tf_error */)
9338 : : {
9339 : 114828975 : bool sfinae = !(complain & tf_error);
9340 : 114828975 : tree r = cxx_eval_outermost_constant_expr (t, sfinae, true, mce_true, false, decl);
9341 : 114828975 : if (sfinae && !TREE_CONSTANT (r))
9342 : 626 : r = error_mark_node;
9343 : 114828975 : return r;
9344 : : }
9345 : :
9346 : : /* Like cxx_constant_value, but used for evaluation of constexpr destructors
9347 : : of constexpr variables. The actual initializer of DECL is not modified. */
9348 : :
9349 : : void
9350 : 131 : cxx_constant_dtor (tree t, tree decl)
9351 : : {
9352 : 131 : cxx_eval_outermost_constant_expr (t, false, true, mce_true, true, decl);
9353 : 131 : }
9354 : :
9355 : : /* Helper routine for fold_simple function. Either return simplified
9356 : : expression T, otherwise NULL_TREE.
9357 : : In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold
9358 : : even if we are within template-declaration. So be careful on call, as in
9359 : : such case types can be undefined. */
9360 : :
9361 : : static tree
9362 : 95672336 : fold_simple_1 (tree t)
9363 : : {
9364 : 95672336 : tree op1;
9365 : 95672336 : enum tree_code code = TREE_CODE (t);
9366 : :
9367 : 95672336 : switch (code)
9368 : : {
9369 : : case INTEGER_CST:
9370 : : case REAL_CST:
9371 : : case VECTOR_CST:
9372 : : case FIXED_CST:
9373 : : case COMPLEX_CST:
9374 : : return t;
9375 : :
9376 : 819158 : case SIZEOF_EXPR:
9377 : 819158 : return fold_sizeof_expr (t);
9378 : :
9379 : 28336079 : case ABS_EXPR:
9380 : 28336079 : case ABSU_EXPR:
9381 : 28336079 : case CONJ_EXPR:
9382 : 28336079 : case REALPART_EXPR:
9383 : 28336079 : case IMAGPART_EXPR:
9384 : 28336079 : case NEGATE_EXPR:
9385 : 28336079 : case BIT_NOT_EXPR:
9386 : 28336079 : case TRUTH_NOT_EXPR:
9387 : 28336079 : case VIEW_CONVERT_EXPR:
9388 : 28336079 : CASE_CONVERT:
9389 : 28336079 : case FLOAT_EXPR:
9390 : 28336079 : case FIX_TRUNC_EXPR:
9391 : 28336079 : case FIXED_CONVERT_EXPR:
9392 : 28336079 : case ADDR_SPACE_CONVERT_EXPR:
9393 : :
9394 : 28336079 : op1 = TREE_OPERAND (t, 0);
9395 : :
9396 : 28336079 : t = const_unop (code, TREE_TYPE (t), op1);
9397 : 28336079 : if (!t)
9398 : : return NULL_TREE;
9399 : :
9400 : 1802323 : if (CONVERT_EXPR_CODE_P (code)
9401 : 1802323 : && TREE_OVERFLOW_P (t) && !TREE_OVERFLOW_P (op1))
9402 : 0 : TREE_OVERFLOW (t) = false;
9403 : : return t;
9404 : :
9405 : : default:
9406 : : return NULL_TREE;
9407 : : }
9408 : : }
9409 : :
9410 : : /* If T is a simple constant expression, returns its simplified value.
9411 : : Otherwise returns T. In contrast to maybe_constant_value we
9412 : : simplify only few operations on constant-expressions, and we don't
9413 : : try to simplify constexpressions. */
9414 : :
9415 : : tree
9416 : 96159433 : fold_simple (tree t)
9417 : : {
9418 : 96159433 : if (processing_template_decl)
9419 : : return t;
9420 : :
9421 : 95672336 : tree r = fold_simple_1 (t);
9422 : 95672336 : if (r)
9423 : : return r;
9424 : :
9425 : : return t;
9426 : : }
9427 : :
9428 : : /* Try folding the expression T to a simple constant.
9429 : : Returns that constant, otherwise returns T. */
9430 : :
9431 : : tree
9432 : 473610 : fold_to_constant (tree t)
9433 : : {
9434 : 473610 : tree r = fold (t);
9435 : 473610 : if (CONSTANT_CLASS_P (r) && !TREE_OVERFLOW (r))
9436 : : return r;
9437 : : else
9438 : 34760 : return t;
9439 : : }
9440 : :
9441 : : /* If T is a constant expression, returns its reduced value.
9442 : : Otherwise, if T does not have TREE_CONSTANT set, returns T.
9443 : : Otherwise, returns a version of T without TREE_CONSTANT.
9444 : : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated
9445 : : as per P0595. */
9446 : :
9447 : : static GTY((deletable)) hash_map<tree, tree> *cv_cache;
9448 : :
9449 : : tree
9450 : 482323307 : maybe_constant_value (tree t, tree decl /* = NULL_TREE */,
9451 : : mce_value manifestly_const_eval /* = mce_unknown */)
9452 : : {
9453 : 482323307 : tree r;
9454 : :
9455 : 482323307 : if (!is_nondependent_constant_expression (t))
9456 : : {
9457 : 0 : if (TREE_OVERFLOW_P (t)
9458 : 114791619 : || (!processing_template_decl && TREE_CONSTANT (t)))
9459 : 4252 : t = mark_non_constant (t);
9460 : 114791619 : return t;
9461 : : }
9462 : 367531688 : else if (CONSTANT_CLASS_P (t))
9463 : : /* No caching or evaluation needed. */
9464 : : return t;
9465 : :
9466 : : /* Don't constant evaluate an unevaluated non-manifestly-constant operand,
9467 : : but at least try folding it to a simple constant. */
9468 : 194794659 : if (cp_unevaluated_operand && manifestly_const_eval != mce_true)
9469 : 439469 : return fold_to_constant (t);
9470 : :
9471 : 191272268 : if (manifestly_const_eval != mce_unknown)
9472 : 85890570 : return cxx_eval_outermost_constant_expr (t, true, true,
9473 : 85887873 : manifestly_const_eval, false, decl);
9474 : :
9475 : 108464620 : if (cv_cache == NULL)
9476 : 132374 : cv_cache = hash_map<tree, tree>::create_ggc (101);
9477 : 108464620 : if (tree *cached = cv_cache->get (t))
9478 : : {
9479 : 8992346 : r = *cached;
9480 : 8992346 : if (r != t)
9481 : : {
9482 : : /* Clear processing_template_decl for sake of break_out_target_exprs;
9483 : : entries in the cv_cache are non-templated. */
9484 : 3298458 : processing_template_decl_sentinel ptds;
9485 : :
9486 : 3298458 : r = break_out_target_exprs (r, /*clear_loc*/true);
9487 : 3298458 : protected_set_expr_location (r, EXPR_LOCATION (t));
9488 : 3298458 : }
9489 : 8992346 : return r;
9490 : : }
9491 : :
9492 : 99472274 : uid_sensitive_constexpr_evaluation_checker c;
9493 : 99472274 : r = cxx_eval_outermost_constant_expr (t, true, true,
9494 : : manifestly_const_eval, false, decl);
9495 : 99472274 : gcc_checking_assert (r == t
9496 : : || CONVERT_EXPR_P (t)
9497 : : || TREE_CODE (t) == VIEW_CONVERT_EXPR
9498 : : || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
9499 : : || !cp_tree_equal (r, t));
9500 : 99472274 : if (!c.evaluation_restricted_p ())
9501 : 97756585 : cv_cache->put (t, r);
9502 : : return r;
9503 : : }
9504 : :
9505 : : /* Dispose of the whole CV_CACHE. */
9506 : :
9507 : : static void
9508 : 22478941 : clear_cv_cache (void)
9509 : : {
9510 : 22478941 : if (cv_cache != NULL)
9511 : 22140856 : cv_cache->empty ();
9512 : 22478941 : }
9513 : :
9514 : : /* Dispose of the whole CV_CACHE and FOLD_CACHE. */
9515 : :
9516 : : void
9517 : 22478941 : clear_cv_and_fold_caches ()
9518 : : {
9519 : 22478941 : clear_cv_cache ();
9520 : 22478941 : clear_fold_cache ();
9521 : 22478941 : }
9522 : :
9523 : : /* Internal function handling expressions in templates for
9524 : : fold_non_dependent_expr and fold_non_dependent_init.
9525 : :
9526 : : If we're in a template, but T isn't value dependent, simplify
9527 : : it. We're supposed to treat:
9528 : :
9529 : : template <typename T> void f(T[1 + 1]);
9530 : : template <typename T> void f(T[2]);
9531 : :
9532 : : as two declarations of the same function, for example. */
9533 : :
9534 : : static tree
9535 : 23993065 : fold_non_dependent_expr_template (tree t, tsubst_flags_t complain,
9536 : : bool manifestly_const_eval,
9537 : : tree object)
9538 : : {
9539 : 23993065 : gcc_assert (processing_template_decl);
9540 : :
9541 : 23993065 : if (is_nondependent_constant_expression (t))
9542 : : {
9543 : 13063604 : processing_template_decl_sentinel s;
9544 : 13063604 : t = instantiate_non_dependent_expr_internal (t, complain);
9545 : :
9546 : 13063604 : if (type_unknown_p (t) || BRACE_ENCLOSED_INITIALIZER_P (t))
9547 : : {
9548 : 0 : if (TREE_OVERFLOW_P (t))
9549 : : {
9550 : 0 : t = build_nop (TREE_TYPE (t), t);
9551 : 0 : TREE_CONSTANT (t) = false;
9552 : : }
9553 : 0 : return t;
9554 : : }
9555 : 13063604 : else if (CONSTANT_CLASS_P (t))
9556 : : /* No evaluation needed. */
9557 : : return t;
9558 : :
9559 : : /* Don't constant evaluate an unevaluated non-manifestly-constant operand,
9560 : : but at least try folding it to a simple constant. */
9561 : 3366175 : if (cp_unevaluated_operand && !manifestly_const_eval)
9562 : 89 : return fold_to_constant (t);
9563 : :
9564 : 3366086 : tree r = cxx_eval_outermost_constant_expr (t, true, true,
9565 : : mce_value (manifestly_const_eval),
9566 : : false, object);
9567 : : /* cp_tree_equal looks through NOPs, so allow them. */
9568 : 3366086 : gcc_checking_assert (r == t
9569 : : || CONVERT_EXPR_P (t)
9570 : : || TREE_CODE (t) == VIEW_CONVERT_EXPR
9571 : : || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
9572 : : || !cp_tree_equal (r, t));
9573 : 3366086 : return r;
9574 : 13063604 : }
9575 : 10929461 : else if (TREE_OVERFLOW_P (t))
9576 : : {
9577 : 0 : t = build_nop (TREE_TYPE (t), t);
9578 : 0 : TREE_CONSTANT (t) = false;
9579 : : }
9580 : :
9581 : : return t;
9582 : : }
9583 : :
9584 : : /* Like maybe_constant_value but first fully instantiate the argument.
9585 : :
9586 : : Note: this is equivalent to instantiate_non_dependent_expr (t, complain)
9587 : : followed by maybe_constant_value but is more efficient,
9588 : : because it calls instantiation_dependent_expression_p and
9589 : : potential_constant_expression at most once.
9590 : : The manifestly_const_eval argument is passed to maybe_constant_value.
9591 : :
9592 : : Callers should generally pass their active complain, or if they are in a
9593 : : non-template, diagnosing context, they can use the default of
9594 : : tf_warning_or_error. Callers that might be within a template context, don't
9595 : : have a complain parameter, and aren't going to remember the result for long
9596 : : (e.g. null_ptr_cst_p), can pass tf_none and deal with error_mark_node
9597 : : appropriately. */
9598 : :
9599 : : tree
9600 : 98050800 : fold_non_dependent_expr (tree t,
9601 : : tsubst_flags_t complain /* = tf_warning_or_error */,
9602 : : bool manifestly_const_eval /* = false */,
9603 : : tree object /* = NULL_TREE */)
9604 : : {
9605 : 98050800 : if (t == NULL_TREE)
9606 : : return NULL_TREE;
9607 : :
9608 : 97603238 : if (processing_template_decl)
9609 : 23037671 : return fold_non_dependent_expr_template (t, complain,
9610 : 23037671 : manifestly_const_eval, object);
9611 : :
9612 : 74565567 : return maybe_constant_value (t, object, mce_value (manifestly_const_eval));
9613 : : }
9614 : :
9615 : : /* Like fold_non_dependent_expr, but if EXPR couldn't be folded to a constant,
9616 : : return the original expression. */
9617 : :
9618 : : tree
9619 : 1955334 : maybe_fold_non_dependent_expr (tree expr,
9620 : : tsubst_flags_t complain/*=tf_warning_or_error*/)
9621 : : {
9622 : 1955334 : tree t = fold_non_dependent_expr (expr, complain);
9623 : 1955334 : if (t && TREE_CONSTANT (t))
9624 : 977646 : return t;
9625 : :
9626 : : return expr;
9627 : : }
9628 : :
9629 : : /* Like maybe_constant_init but first fully instantiate the argument. */
9630 : :
9631 : : tree
9632 : 20821092 : fold_non_dependent_init (tree t,
9633 : : tsubst_flags_t complain /*=tf_warning_or_error*/,
9634 : : bool manifestly_const_eval /*=false*/,
9635 : : tree object /* = NULL_TREE */)
9636 : : {
9637 : 20821092 : if (t == NULL_TREE)
9638 : : return NULL_TREE;
9639 : :
9640 : 20821092 : if (processing_template_decl)
9641 : : {
9642 : 955394 : t = fold_non_dependent_expr_template (t, complain,
9643 : : manifestly_const_eval, object);
9644 : : /* maybe_constant_init does this stripping, so do it here too. */
9645 : 955394 : if (TREE_CODE (t) == TARGET_EXPR)
9646 : : {
9647 : 61 : tree init = TARGET_EXPR_INITIAL (t);
9648 : 61 : if (TREE_CODE (init) == CONSTRUCTOR)
9649 : 955394 : t = init;
9650 : : }
9651 : 955394 : return t;
9652 : : }
9653 : :
9654 : 19865698 : return maybe_constant_init (t, object, manifestly_const_eval);
9655 : : }
9656 : :
9657 : : /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
9658 : : than wrapped in a TARGET_EXPR.
9659 : : ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
9660 : : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
9661 : : per P0595 even when ALLOW_NON_CONSTANT is true. */
9662 : :
9663 : : static tree
9664 : 51447485 : maybe_constant_init_1 (tree t, tree decl, bool allow_non_constant,
9665 : : bool manifestly_const_eval)
9666 : : {
9667 : 51447485 : if (!t)
9668 : : return t;
9669 : 51447485 : if (TREE_CODE (t) == EXPR_STMT)
9670 : 20655 : t = TREE_OPERAND (t, 0);
9671 : 51447485 : if (TREE_CODE (t) == CONVERT_EXPR
9672 : 51447485 : && VOID_TYPE_P (TREE_TYPE (t)))
9673 : 30049 : t = TREE_OPERAND (t, 0);
9674 : : /* If the types don't match, the INIT_EXPR is initializing a subobject of
9675 : : DECL and losing that information would cause mischief later. */
9676 : 51447485 : if (TREE_CODE (t) == INIT_EXPR
9677 : 51447485 : && (!decl
9678 : 9473 : || same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (decl),
9679 : 9473 : TREE_TYPE (t))))
9680 : 23668 : t = TREE_OPERAND (t, 1);
9681 : 51447485 : if (TREE_CODE (t) == TARGET_EXPR)
9682 : 478593 : t = TARGET_EXPR_INITIAL (t);
9683 : 51447485 : if (!is_nondependent_static_init_expression (t))
9684 : : /* Don't try to evaluate it. */;
9685 : 43229950 : else if (CONSTANT_CLASS_P (t) && TREE_CODE (t) != PTRMEM_CST)
9686 : : /* No evaluation needed. PTRMEM_CST needs the immediate fn check. */;
9687 : : else
9688 : : {
9689 : : /* [basic.start.static] allows constant-initialization of variables with
9690 : : static or thread storage duration even if it isn't required, but we
9691 : : shouldn't bend the rules the same way for automatic variables. */
9692 : 9489431 : bool is_static = (decl && DECL_P (decl)
9693 : 31323035 : && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)));
9694 : : if (is_static)
9695 : : manifestly_const_eval = true;
9696 : :
9697 : 22067245 : if (cp_unevaluated_operand && !manifestly_const_eval)
9698 : 34052 : return fold_to_constant (t);
9699 : :
9700 : 22033193 : t = cxx_eval_outermost_constant_expr (t, allow_non_constant, !is_static,
9701 : : mce_value (manifestly_const_eval),
9702 : : false, decl);
9703 : : }
9704 : 51413433 : if (TREE_CODE (t) == TARGET_EXPR)
9705 : : {
9706 : 641964 : tree init = TARGET_EXPR_INITIAL (t);
9707 : 641964 : if (TREE_CODE (init) == CONSTRUCTOR)
9708 : 51447485 : t = init;
9709 : : }
9710 : : return t;
9711 : : }
9712 : :
9713 : : /* Wrapper for maybe_constant_init_1 which permits non constants. */
9714 : :
9715 : : tree
9716 : 51446116 : maybe_constant_init (tree t, tree decl, bool manifestly_const_eval)
9717 : : {
9718 : 51446116 : return maybe_constant_init_1 (t, decl, true, manifestly_const_eval);
9719 : : }
9720 : :
9721 : : /* Wrapper for maybe_constant_init_1 which does not permit non constants. */
9722 : :
9723 : : tree
9724 : 1369 : cxx_constant_init (tree t, tree decl)
9725 : : {
9726 : 1369 : return maybe_constant_init_1 (t, decl, false, true);
9727 : : }
9728 : :
9729 : : #if 0
9730 : : /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
9731 : : /* Return true if the object referred to by REF has automatic or thread
9732 : : local storage. */
9733 : :
9734 : : enum { ck_ok, ck_bad, ck_unknown };
9735 : : static int
9736 : : check_automatic_or_tls (tree ref)
9737 : : {
9738 : : machine_mode mode;
9739 : : poly_int64 bitsize, bitpos;
9740 : : tree offset;
9741 : : int volatilep = 0, unsignedp = 0;
9742 : : tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset,
9743 : : &mode, &unsignedp, &volatilep, false);
9744 : : duration_kind dk;
9745 : :
9746 : : /* If there isn't a decl in the middle, we don't know the linkage here,
9747 : : and this isn't a constant expression anyway. */
9748 : : if (!DECL_P (decl))
9749 : : return ck_unknown;
9750 : : dk = decl_storage_duration (decl);
9751 : : return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok;
9752 : : }
9753 : : #endif
9754 : :
9755 : : /* Data structure for passing data from potential_constant_expression_1
9756 : : to check_for_return_continue via cp_walk_tree. */
9757 : : struct check_for_return_continue_data {
9758 : : hash_set<tree> *pset;
9759 : : tree continue_stmt;
9760 : : tree break_stmt;
9761 : : };
9762 : :
9763 : : /* Helper function for potential_constant_expression_1 SWITCH_STMT handling,
9764 : : called through cp_walk_tree. Return the first RETURN_EXPR found, or note
9765 : : the first CONTINUE_STMT and/or BREAK_STMT if RETURN_EXPR is not found. */
9766 : : static tree
9767 : 22083763 : check_for_return_continue (tree *tp, int *walk_subtrees, void *data)
9768 : : {
9769 : 22083763 : tree t = *tp, s, b;
9770 : 22083763 : check_for_return_continue_data *d = (check_for_return_continue_data *) data;
9771 : 22083763 : switch (TREE_CODE (t))
9772 : : {
9773 : : case RETURN_EXPR:
9774 : : return t;
9775 : :
9776 : 30 : case CONTINUE_STMT:
9777 : 30 : if (d->continue_stmt == NULL_TREE)
9778 : 30 : d->continue_stmt = t;
9779 : : break;
9780 : :
9781 : 3485 : case BREAK_STMT:
9782 : 3485 : if (d->break_stmt == NULL_TREE)
9783 : 2637 : d->break_stmt = t;
9784 : : break;
9785 : :
9786 : : #define RECUR(x) \
9787 : : if (tree r = cp_walk_tree (&x, check_for_return_continue, data, \
9788 : : d->pset)) \
9789 : : return r
9790 : :
9791 : : /* For loops, walk subtrees manually, so that continue stmts found
9792 : : inside of the bodies of the loops are ignored. */
9793 : 11681 : case DO_STMT:
9794 : 11681 : *walk_subtrees = 0;
9795 : 11681 : RECUR (DO_COND (t));
9796 : 11681 : s = d->continue_stmt;
9797 : 11681 : b = d->break_stmt;
9798 : 11681 : RECUR (DO_BODY (t));
9799 : 11681 : d->continue_stmt = s;
9800 : 11681 : d->break_stmt = b;
9801 : 11681 : break;
9802 : :
9803 : 100 : case WHILE_STMT:
9804 : 100 : *walk_subtrees = 0;
9805 : 100 : RECUR (WHILE_COND_PREP (t));
9806 : 100 : RECUR (WHILE_COND (t));
9807 : 100 : s = d->continue_stmt;
9808 : 100 : b = d->break_stmt;
9809 : 100 : RECUR (WHILE_BODY (t));
9810 : 93 : d->continue_stmt = s;
9811 : 93 : d->break_stmt = b;
9812 : 93 : break;
9813 : :
9814 : 100 : case FOR_STMT:
9815 : 100 : *walk_subtrees = 0;
9816 : 100 : RECUR (FOR_INIT_STMT (t));
9817 : 100 : RECUR (FOR_COND_PREP (t));
9818 : 100 : RECUR (FOR_COND (t));
9819 : 100 : RECUR (FOR_EXPR (t));
9820 : 100 : s = d->continue_stmt;
9821 : 100 : b = d->break_stmt;
9822 : 100 : RECUR (FOR_BODY (t));
9823 : 62 : d->continue_stmt = s;
9824 : 62 : d->break_stmt = b;
9825 : 62 : break;
9826 : :
9827 : 0 : case RANGE_FOR_STMT:
9828 : 0 : *walk_subtrees = 0;
9829 : 0 : RECUR (RANGE_FOR_EXPR (t));
9830 : 0 : s = d->continue_stmt;
9831 : 0 : b = d->break_stmt;
9832 : 0 : RECUR (RANGE_FOR_BODY (t));
9833 : 0 : d->continue_stmt = s;
9834 : 0 : d->break_stmt = b;
9835 : 0 : break;
9836 : :
9837 : 174 : case SWITCH_STMT:
9838 : 174 : *walk_subtrees = 0;
9839 : 174 : RECUR (SWITCH_STMT_COND (t));
9840 : 174 : b = d->break_stmt;
9841 : 174 : RECUR (SWITCH_STMT_BODY (t));
9842 : 154 : d->break_stmt = b;
9843 : 154 : break;
9844 : : #undef RECUR
9845 : :
9846 : : case STATEMENT_LIST:
9847 : : case CONSTRUCTOR:
9848 : : break;
9849 : :
9850 : 21362132 : default:
9851 : 21362132 : if (!EXPR_P (t))
9852 : 6158819 : *walk_subtrees = 0;
9853 : : break;
9854 : : }
9855 : :
9856 : : return NULL_TREE;
9857 : : }
9858 : :
9859 : : /* Return true if T denotes a potentially constant expression. Issue
9860 : : diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
9861 : : an lvalue-rvalue conversion is implied. If NOW is true, we want to
9862 : : consider the expression in the current context, independent of constexpr
9863 : : substitution. If FUNDEF_P is true, we're checking a constexpr function body
9864 : : and hard errors should not be reported by constexpr_error.
9865 : :
9866 : : C++0x [expr.const] used to say
9867 : :
9868 : : 6 An expression is a potential constant expression if it is
9869 : : a constant expression where all occurrences of function
9870 : : parameters are replaced by arbitrary constant expressions
9871 : : of the appropriate type.
9872 : :
9873 : : 2 A conditional expression is a constant expression unless it
9874 : : involves one of the following as a potentially evaluated
9875 : : subexpression (3.2), but subexpressions of logical AND (5.14),
9876 : : logical OR (5.15), and conditional (5.16) operations that are
9877 : : not evaluated are not considered. */
9878 : :
9879 : : static bool
9880 : 2740939038 : potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
9881 : : bool fundef_p, tsubst_flags_t flags,
9882 : : tree *jump_target)
9883 : : {
9884 : : #define RECUR(T,RV) \
9885 : : potential_constant_expression_1 ((T), (RV), strict, now, fundef_p, flags, \
9886 : : jump_target)
9887 : :
9888 : 2740939038 : enum { any = false, rval = true };
9889 : 2740939038 : int i;
9890 : 2740939038 : tree tmp;
9891 : :
9892 : 2740939038 : if (t == error_mark_node)
9893 : : return false;
9894 : 2740929168 : if (t == NULL_TREE)
9895 : : return true;
9896 : 2736063704 : location_t loc = cp_expr_loc_or_input_loc (t);
9897 : :
9898 : 2736063704 : if (*jump_target)
9899 : : /* If we are jumping, ignore everything. This is simpler than the
9900 : : cxx_eval_constant_expression handling because we only need to be
9901 : : conservatively correct, and we don't necessarily have a constant value
9902 : : available, so we don't bother with switch tracking. */
9903 : : return true;
9904 : :
9905 : 808953 : if (TREE_THIS_VOLATILE (t) && want_rval
9906 : 308462 : && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (t))
9907 : 2733472898 : && !NULLPTR_TYPE_P (TREE_TYPE (t)))
9908 : : {
9909 : 77582 : if (flags & tf_error)
9910 : 21 : constexpr_error (loc, fundef_p, "lvalue-to-rvalue conversion of "
9911 : : "a volatile lvalue %qE with type %qT", t,
9912 : 21 : TREE_TYPE (t));
9913 : 77582 : return false;
9914 : : }
9915 : 2733317708 : if (CONSTANT_CLASS_P (t))
9916 : : return true;
9917 : 2090973444 : if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED)
9918 : 2090973444 : && TREE_TYPE (t) == error_mark_node)
9919 : : return false;
9920 : :
9921 : 2090973419 : switch (TREE_CODE (t))
9922 : : {
9923 : : case FUNCTION_DECL:
9924 : : case BASELINK:
9925 : : case TEMPLATE_DECL:
9926 : : case OVERLOAD:
9927 : : case TEMPLATE_ID_EXPR:
9928 : : case LABEL_DECL:
9929 : : case CASE_LABEL_EXPR:
9930 : : case PREDICT_EXPR:
9931 : : case CONST_DECL:
9932 : : case SIZEOF_EXPR:
9933 : : case ALIGNOF_EXPR:
9934 : : case OFFSETOF_EXPR:
9935 : : case NOEXCEPT_EXPR:
9936 : : case TEMPLATE_PARM_INDEX:
9937 : : case TRAIT_EXPR:
9938 : : case IDENTIFIER_NODE:
9939 : : case USERDEF_LITERAL:
9940 : : /* We can see a FIELD_DECL in a pointer-to-member expression. */
9941 : : case FIELD_DECL:
9942 : : case RESULT_DECL:
9943 : : case USING_DECL:
9944 : : case USING_STMT:
9945 : : case PLACEHOLDER_EXPR:
9946 : : case REQUIRES_EXPR:
9947 : : case STATIC_ASSERT:
9948 : : case DEBUG_BEGIN_STMT:
9949 : : return true;
9950 : :
9951 : 13616260 : case RETURN_EXPR:
9952 : 13616260 : if (!RECUR (TREE_OPERAND (t, 0), any))
9953 : : return false;
9954 : : /* FALLTHROUGH */
9955 : :
9956 : 13494114 : case BREAK_STMT:
9957 : 13494114 : case CONTINUE_STMT:
9958 : 13494114 : *jump_target = t;
9959 : 13494114 : return true;
9960 : :
9961 : 189193540 : case PARM_DECL:
9962 : 189193540 : if (now && want_rval)
9963 : : {
9964 : 55580247 : tree type = TREE_TYPE (t);
9965 : 55580247 : if (dependent_type_p (type)
9966 : 36540077 : || !COMPLETE_TYPE_P (processing_template_decl
9967 : : ? type : complete_type (type))
9968 : 92120322 : || is_really_empty_class (type, /*ignore_vptr*/false))
9969 : : /* An empty class has no data to read. */
9970 : 19040474 : return true;
9971 : 36539773 : if (flags & tf_error)
9972 : 12 : constexpr_error (input_location, fundef_p,
9973 : : "%qE is not a constant expression", t);
9974 : 36539773 : return false;
9975 : : }
9976 : : return true;
9977 : :
9978 : 162346747 : case AGGR_INIT_EXPR:
9979 : 162346747 : case CALL_EXPR:
9980 : : /* -- an invocation of a function other than a constexpr function
9981 : : or a constexpr constructor. */
9982 : 162346747 : {
9983 : 162346747 : tree fun = get_function_named_in_call (t);
9984 : 162346747 : const int nargs = call_expr_nargs (t);
9985 : 162346747 : i = 0;
9986 : :
9987 : 162346747 : if (fun == NULL_TREE)
9988 : : {
9989 : : /* Reset to allow the function to continue past the end
9990 : : of the block below. Otherwise return early. */
9991 : 87070 : bool bail = true;
9992 : :
9993 : 87070 : if (TREE_CODE (t) == CALL_EXPR
9994 : 87070 : && CALL_EXPR_FN (t) == NULL_TREE)
9995 : 87070 : switch (CALL_EXPR_IFN (t))
9996 : : {
9997 : : /* These should be ignored, they are optimized away from
9998 : : constexpr functions. */
9999 : : case IFN_UBSAN_NULL:
10000 : : case IFN_UBSAN_BOUNDS:
10001 : : case IFN_UBSAN_VPTR:
10002 : : case IFN_FALLTHROUGH:
10003 : : case IFN_ASSUME:
10004 : : return true;
10005 : :
10006 : : case IFN_ADD_OVERFLOW:
10007 : : case IFN_SUB_OVERFLOW:
10008 : : case IFN_MUL_OVERFLOW:
10009 : : case IFN_LAUNDER:
10010 : : case IFN_VEC_CONVERT:
10011 : : bail = false;
10012 : : break;
10013 : :
10014 : : default:
10015 : : break;
10016 : : }
10017 : :
10018 : : if (bail)
10019 : : {
10020 : : /* fold_call_expr can't do anything with IFN calls. */
10021 : 22 : if (flags & tf_error)
10022 : 0 : constexpr_error (loc, fundef_p,
10023 : : "call to internal function %qE", t);
10024 : 22 : return false;
10025 : : }
10026 : : }
10027 : :
10028 : 162259677 : if (fun && is_overloaded_fn (fun))
10029 : : {
10030 : 147160418 : if (!RECUR (fun, true))
10031 : : return false;
10032 : 146540774 : fun = get_fns (fun);
10033 : :
10034 : 146540774 : if (TREE_CODE (fun) == FUNCTION_DECL)
10035 : : {
10036 : 126754719 : if (builtin_valid_in_constant_expr_p (fun))
10037 : : return true;
10038 : 126082239 : if (!maybe_constexpr_fn (fun)
10039 : : /* Allow any built-in function; if the expansion
10040 : : isn't constant, we'll deal with that then. */
10041 : 39360325 : && !fndecl_built_in_p (fun)
10042 : : /* In C++20, replaceable global allocation functions
10043 : : are constant expressions. */
10044 : 28023890 : && (!cxx_replaceable_global_alloc_fn (fun)
10045 : 126089 : || TREE_CODE (t) != CALL_EXPR
10046 : 126089 : || (!CALL_FROM_NEW_OR_DELETE_P (t)
10047 : 62708 : && (current_function_decl == NULL_TREE
10048 : 62708 : || !is_std_allocator_allocate
10049 : 62708 : (current_function_decl))))
10050 : : /* Allow placement new in std::construct_at. */
10051 : 27903879 : && (!cxx_placement_new_fn (fun)
10052 : 45887 : || TREE_CODE (t) != CALL_EXPR
10053 : 45887 : || current_function_decl == NULL_TREE
10054 : 45881 : || !is_std_construct_at (current_function_decl))
10055 : 153955823 : && !cxx_dynamic_cast_fn_p (fun))
10056 : : {
10057 : 27871624 : if ((flags & tf_error)
10058 : 27871624 : && constexpr_error (loc, fundef_p,
10059 : : "call to non-%<constexpr%> "
10060 : : "function %qD", fun))
10061 : 172 : explain_invalid_constexpr_fn (fun);
10062 : 27871624 : return false;
10063 : : }
10064 : : }
10065 : :
10066 : 117996670 : fun = OVL_FIRST (fun);
10067 : : /* Skip initial arguments to base constructors. */
10068 : 117996670 : if (DECL_BASE_CONSTRUCTOR_P (fun))
10069 : 2025057 : i = num_artificial_parms_for (fun);
10070 : : }
10071 : 15099259 : else if (fun)
10072 : : {
10073 : 15099259 : if (TREE_TYPE (fun)
10074 : 15099259 : && FUNCTION_POINTER_TYPE_P (TREE_TYPE (fun)))
10075 : : want_rval = rval;
10076 : : else
10077 : : want_rval = any;
10078 : 15099259 : if (RECUR (fun, want_rval))
10079 : : /* Might end up being a constant function pointer. But it
10080 : : could also be a function object with constexpr op(), so
10081 : : we pass 'any' so that the underlying VAR_DECL is deemed
10082 : : as potentially-constant even though it wasn't declared
10083 : : constexpr. */;
10084 : : else
10085 : : return false;
10086 : : }
10087 : 254562709 : for (; i < nargs; ++i)
10088 : : {
10089 : 139630210 : tree x = get_nth_callarg (t, i);
10090 : : /* In a template, reference arguments haven't been converted to
10091 : : REFERENCE_TYPE and we might not even know if the parameter
10092 : : is a reference, so accept lvalue constants too. */
10093 : 139630210 : bool rv = processing_template_decl ? any : rval;
10094 : : /* Don't require an immediately constant value, as constexpr
10095 : : substitution might not use the value of the argument. */
10096 : 139630210 : bool sub_now = false;
10097 : 139630210 : if (!potential_constant_expression_1 (x, rv, strict,
10098 : : sub_now, fundef_p, flags,
10099 : : jump_target))
10100 : : return false;
10101 : : }
10102 : : return true;
10103 : : }
10104 : :
10105 : 118263478 : case NON_LVALUE_EXPR:
10106 : : /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
10107 : : -- an lvalue of integral type that refers to a non-volatile
10108 : : const variable or static data member initialized with
10109 : : constant expressions, or
10110 : :
10111 : : -- an lvalue of literal type that refers to non-volatile
10112 : : object defined with constexpr, or that refers to a
10113 : : sub-object of such an object; */
10114 : 118263478 : return RECUR (TREE_OPERAND (t, 0), rval);
10115 : :
10116 : 27539 : case EXCESS_PRECISION_EXPR:
10117 : 27539 : return RECUR (TREE_OPERAND (t, 0), rval);
10118 : :
10119 : 218125526 : case VAR_DECL:
10120 : 218125526 : if (DECL_HAS_VALUE_EXPR_P (t))
10121 : : {
10122 : 2956714 : if (now && is_normal_capture_proxy (t))
10123 : : {
10124 : : /* -- in a lambda-expression, a reference to this or to a
10125 : : variable with automatic storage duration defined outside that
10126 : : lambda-expression, where the reference would be an
10127 : : odr-use. */
10128 : :
10129 : 354416 : if (want_rval)
10130 : : /* Since we're doing an lvalue-rvalue conversion, this might
10131 : : not be an odr-use, so evaluate the variable directly. */
10132 : 353723 : return RECUR (DECL_CAPTURED_VARIABLE (t), rval);
10133 : :
10134 : 693 : if (flags & tf_error)
10135 : : {
10136 : 3 : tree cap = DECL_CAPTURED_VARIABLE (t);
10137 : 3 : auto_diagnostic_group d;
10138 : 3 : if (constexpr_error (input_location, fundef_p,
10139 : : "lambda capture of %qE is not a "
10140 : : "constant expression", cap)
10141 : 3 : && decl_constant_var_p (cap))
10142 : 3 : inform (input_location, "because it is used as a glvalue");
10143 : 3 : }
10144 : 693 : return false;
10145 : : }
10146 : : /* Treat __PRETTY_FUNCTION__ inside a template function as
10147 : : potentially-constant. */
10148 : 5203478 : else if (DECL_PRETTY_FUNCTION_P (t)
10149 : 2683806 : && DECL_VALUE_EXPR (t) == error_mark_node)
10150 : : return true;
10151 : 2602293 : return RECUR (DECL_VALUE_EXPR (t), rval);
10152 : : }
10153 : 215168812 : if (want_rval
10154 : 148656025 : && (now || !var_in_maybe_constexpr_fn (t))
10155 : 140562467 : && !type_dependent_expression_p (t)
10156 : 110537885 : && !decl_maybe_constant_var_p (t)
10157 : 47691215 : && (strict
10158 : 1139444 : || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t))
10159 : 375496 : || (DECL_INITIAL (t)
10160 : 374989 : && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t)))
10161 : 47690294 : && COMPLETE_TYPE_P (TREE_TYPE (t))
10162 : 262844428 : && !is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
10163 : : {
10164 : 47672216 : if (flags & tf_error)
10165 : 148 : non_const_var_error (loc, t, fundef_p);
10166 : 47672216 : return false;
10167 : : }
10168 : : return true;
10169 : :
10170 : 194853930 : case NOP_EXPR:
10171 : 194853930 : if (REINTERPRET_CAST_P (t))
10172 : : {
10173 : 52188 : if (flags & tf_error)
10174 : 34 : constexpr_error (loc, fundef_p, "%<reinterpret_cast%> is not a "
10175 : : "constant expression");
10176 : 52188 : return false;
10177 : : }
10178 : : /* FALLTHRU */
10179 : 492118225 : case CONVERT_EXPR:
10180 : 492118225 : case VIEW_CONVERT_EXPR:
10181 : : /* -- a reinterpret_cast. FIXME not implemented, and this rule
10182 : : may change to something more specific to type-punning (DR 1312). */
10183 : 492118225 : {
10184 : 492118225 : tree from = TREE_OPERAND (t, 0);
10185 : 492118225 : if (location_wrapper_p (t))
10186 : : {
10187 : 265859909 : iloc_sentinel ils = loc;
10188 : 265859909 : return (RECUR (from, want_rval));
10189 : 265859909 : }
10190 : 226258316 : if (INDIRECT_TYPE_P (TREE_TYPE (t)))
10191 : : {
10192 : 93502139 : STRIP_ANY_LOCATION_WRAPPER (from);
10193 : 93502139 : if (TREE_CODE (from) == INTEGER_CST
10194 : 93502139 : && !integer_zerop (from))
10195 : : {
10196 : 1358 : if (flags & tf_error)
10197 : 20 : constexpr_error (loc, fundef_p,
10198 : : "%<reinterpret_cast%> from integer to "
10199 : : "pointer");
10200 : 1358 : return false;
10201 : : }
10202 : : }
10203 : 226256958 : return (RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR));
10204 : : }
10205 : :
10206 : 11169 : case ADDRESSOF_EXPR:
10207 : : /* This is like ADDR_EXPR, except it won't form pointer-to-member. */
10208 : 11169 : t = TREE_OPERAND (t, 0);
10209 : 11169 : goto handle_addr_expr;
10210 : :
10211 : 46295618 : case ADDR_EXPR:
10212 : : /* -- a unary operator & that is applied to an lvalue that
10213 : : designates an object with thread or automatic storage
10214 : : duration; */
10215 : 46295618 : t = TREE_OPERAND (t, 0);
10216 : :
10217 : 46295618 : if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t))
10218 : : /* A pointer-to-member constant. */
10219 : : return true;
10220 : :
10221 : 46306557 : handle_addr_expr:
10222 : : #if 0
10223 : : /* FIXME adjust when issue 1197 is fully resolved. For now don't do
10224 : : any checking here, as we might dereference the pointer later. If
10225 : : we remove this code, also remove check_automatic_or_tls. */
10226 : : i = check_automatic_or_tls (t);
10227 : : if (i == ck_ok)
10228 : : return true;
10229 : : if (i == ck_bad)
10230 : : {
10231 : : if (flags & tf_error)
10232 : : error ("address-of an object %qE with thread local or "
10233 : : "automatic storage is not a constant expression", t);
10234 : : return false;
10235 : : }
10236 : : #endif
10237 : 46306557 : return RECUR (t, any);
10238 : :
10239 : 62304819 : case COMPONENT_REF:
10240 : 62304819 : case ARROW_EXPR:
10241 : 62304819 : case OFFSET_REF:
10242 : : /* -- a class member access unless its postfix-expression is
10243 : : of literal type or of pointer to literal type. */
10244 : : /* This test would be redundant, as it follows from the
10245 : : postfix-expression being a potential constant expression. */
10246 : 62304819 : if (type_unknown_p (t))
10247 : : return true;
10248 : 56593248 : if (is_overloaded_fn (t))
10249 : : /* In a template, a COMPONENT_REF of a function expresses ob.fn(),
10250 : : which uses ob as an lvalue. */
10251 : 58063917 : want_rval = false;
10252 : 58063917 : gcc_fallthrough ();
10253 : :
10254 : 58063917 : case REALPART_EXPR:
10255 : 58063917 : case IMAGPART_EXPR:
10256 : 58063917 : case BIT_FIELD_REF:
10257 : 58063917 : return RECUR (TREE_OPERAND (t, 0), want_rval);
10258 : :
10259 : 194140 : case EXPR_PACK_EXPANSION:
10260 : 194140 : return RECUR (PACK_EXPANSION_PATTERN (t), want_rval);
10261 : :
10262 : : case PACK_INDEX_EXPR:
10263 : : return true;
10264 : :
10265 : 61306588 : case INDIRECT_REF:
10266 : 61306588 : {
10267 : 61306588 : tree x = TREE_OPERAND (t, 0);
10268 : 61306588 : STRIP_NOPS (x);
10269 : 61306588 : if (is_this_parameter (x) && !is_capture_proxy (x))
10270 : : {
10271 : 24322948 : if (now || !var_in_maybe_constexpr_fn (x))
10272 : : {
10273 : 16542636 : if (flags & tf_error)
10274 : 9 : constexpr_error (loc, fundef_p, "use of %<this%> in a "
10275 : : "constant expression");
10276 : 16542636 : return false;
10277 : : }
10278 : : return true;
10279 : : }
10280 : 36983640 : return RECUR (x, rval);
10281 : : }
10282 : :
10283 : 13906903 : case STATEMENT_LIST:
10284 : 47817963 : for (tree stmt : tsi_range (t))
10285 : 34343873 : if (!RECUR (stmt, any))
10286 : 252272026 : return false;
10287 : : return true;
10288 : :
10289 : 3253986 : case MODIFY_EXPR:
10290 : 3253986 : if (cxx_dialect < cxx14)
10291 : 1911 : goto fail;
10292 : 3252075 : if (!RECUR (TREE_OPERAND (t, 0), any))
10293 : : return false;
10294 : : /* Just ignore clobbers. */
10295 : 2862963 : if (TREE_CLOBBER_P (TREE_OPERAND (t, 1)))
10296 : : return true;
10297 : 1394809 : if (!RECUR (TREE_OPERAND (t, 1), rval))
10298 : : return false;
10299 : : return true;
10300 : :
10301 : 110093 : case MODOP_EXPR:
10302 : 110093 : if (cxx_dialect < cxx14)
10303 : 96 : goto fail;
10304 : 109997 : if (!RECUR (TREE_OPERAND (t, 0), rval))
10305 : : return false;
10306 : 99016 : if (!RECUR (TREE_OPERAND (t, 2), rval))
10307 : : return false;
10308 : : return true;
10309 : :
10310 : 246246 : case DO_STMT:
10311 : 246246 : if (!RECUR (DO_COND (t), rval))
10312 : : return false;
10313 : 246246 : if (!RECUR (DO_BODY (t), any))
10314 : : return false;
10315 : 246173 : if (breaks (jump_target) || continues (jump_target))
10316 : 3 : *jump_target = NULL_TREE;
10317 : : return true;
10318 : :
10319 : 190444 : case FOR_STMT:
10320 : 190444 : if (!RECUR (FOR_INIT_STMT (t), any))
10321 : : return false;
10322 : 190444 : if (!RECUR (FOR_COND_PREP (t), any))
10323 : : return false;
10324 : 190444 : tmp = FOR_COND (t);
10325 : 190444 : if (!RECUR (tmp, rval))
10326 : : return false;
10327 : 190189 : if (tmp)
10328 : : {
10329 : 147591 : if (!processing_template_decl)
10330 : 146335 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
10331 : : /* If we couldn't evaluate the condition, it might not ever be
10332 : : true. */
10333 : 147591 : if (!integer_onep (tmp))
10334 : : {
10335 : : /* Before returning true, check if the for body can contain
10336 : : a return. */
10337 : 147591 : hash_set<tree> pset;
10338 : 147591 : check_for_return_continue_data data = { &pset, NULL_TREE,
10339 : 147591 : NULL_TREE };
10340 : 147591 : if (tree ret_expr
10341 : 147591 : = cp_walk_tree (&FOR_BODY (t), check_for_return_continue,
10342 : : &data, &pset))
10343 : 90340 : *jump_target = ret_expr;
10344 : 147591 : return true;
10345 : 147591 : }
10346 : : }
10347 : 42598 : if (!RECUR (FOR_EXPR (t), any))
10348 : : return false;
10349 : 42598 : if (!RECUR (FOR_BODY (t), any))
10350 : : return false;
10351 : 42598 : if (!RECUR (FOR_COND_CLEANUP (t), any))
10352 : : return false;
10353 : 42598 : if (breaks (jump_target) || continues (jump_target))
10354 : 3 : *jump_target = NULL_TREE;
10355 : : return true;
10356 : :
10357 : 21 : case RANGE_FOR_STMT:
10358 : 21 : if (!RECUR (RANGE_FOR_INIT_STMT (t), any))
10359 : : return false;
10360 : 21 : if (!RECUR (RANGE_FOR_EXPR (t), any))
10361 : : return false;
10362 : 21 : if (!RECUR (RANGE_FOR_BODY (t), any))
10363 : : return false;
10364 : 15 : if (breaks (jump_target) || continues (jump_target))
10365 : 0 : *jump_target = NULL_TREE;
10366 : : return true;
10367 : :
10368 : 87328 : case WHILE_STMT:
10369 : 87328 : if (!RECUR (WHILE_COND_PREP (t), any))
10370 : : return false;
10371 : 87328 : tmp = WHILE_COND (t);
10372 : 87328 : if (!RECUR (tmp, rval))
10373 : : return false;
10374 : 87220 : if (!processing_template_decl)
10375 : 87210 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
10376 : : /* If we couldn't evaluate the condition, it might not ever be true. */
10377 : 87220 : if (!integer_onep (tmp))
10378 : : {
10379 : : /* Before returning true, check if the while body can contain
10380 : : a return. */
10381 : 84789 : hash_set<tree> pset;
10382 : 84789 : check_for_return_continue_data data = { &pset, NULL_TREE,
10383 : 84789 : NULL_TREE };
10384 : 84789 : if (tree ret_expr
10385 : 84789 : = cp_walk_tree (&WHILE_BODY (t), check_for_return_continue,
10386 : : &data, &pset))
10387 : 136 : *jump_target = ret_expr;
10388 : 84789 : return true;
10389 : 84789 : }
10390 : 2431 : if (!RECUR (WHILE_BODY (t), any))
10391 : : return false;
10392 : 2427 : if (!RECUR (WHILE_COND_CLEANUP (t), any))
10393 : : return false;
10394 : 2427 : if (breaks (jump_target) || continues (jump_target))
10395 : 30 : *jump_target = NULL_TREE;
10396 : : return true;
10397 : :
10398 : 9471 : case SWITCH_STMT:
10399 : 9471 : if (!RECUR (SWITCH_STMT_COND (t), rval))
10400 : : return false;
10401 : : /* FIXME we don't check SWITCH_STMT_BODY currently, because even
10402 : : unreachable labels would be checked and it is enough if there is
10403 : : a single switch cond value for which it is a valid constant
10404 : : expression. We need to check if there are any RETURN_EXPRs
10405 : : or CONTINUE_STMTs inside of the body though, as in that case
10406 : : we need to set *jump_target. */
10407 : : else
10408 : : {
10409 : 9465 : hash_set<tree> pset;
10410 : 9465 : check_for_return_continue_data data = { &pset, NULL_TREE,
10411 : 9465 : NULL_TREE };
10412 : 9465 : if (tree ret_expr
10413 : 9465 : = cp_walk_tree (&SWITCH_STMT_BODY (t), check_for_return_continue,
10414 : : &data, &pset))
10415 : : /* The switch might return. */
10416 : 9270 : *jump_target = ret_expr;
10417 : 195 : else if (data.continue_stmt)
10418 : : /* The switch can't return, but might continue. */
10419 : 3 : *jump_target = data.continue_stmt;
10420 : 9465 : }
10421 : 9465 : return true;
10422 : :
10423 : 44 : case STMT_EXPR:
10424 : 44 : return RECUR (STMT_EXPR_STMT (t), rval);
10425 : :
10426 : 211414 : case LAMBDA_EXPR:
10427 : 211414 : if (cxx_dialect >= cxx17)
10428 : : /* In C++17 lambdas can be constexpr, don't give up yet. */
10429 : : return true;
10430 : 212 : else if (flags & tf_error)
10431 : 0 : constexpr_error (loc, fundef_p, "lambda-expression is not a "
10432 : : "constant expression before C++17");
10433 : : return false;
10434 : :
10435 : 113702 : case NEW_EXPR:
10436 : 113702 : case VEC_NEW_EXPR:
10437 : 113702 : case DELETE_EXPR:
10438 : 113702 : case VEC_DELETE_EXPR:
10439 : 113702 : if (cxx_dialect >= cxx20)
10440 : : /* In C++20, new-expressions are potentially constant. */
10441 : : return true;
10442 : 86627 : else if (flags & tf_error)
10443 : 0 : constexpr_error (loc, fundef_p, "new-expression is not a "
10444 : : "constant expression before C++20");
10445 : : return false;
10446 : :
10447 : 64837 : case DYNAMIC_CAST_EXPR:
10448 : 64837 : case PSEUDO_DTOR_EXPR:
10449 : 64837 : case THROW_EXPR:
10450 : 64837 : case OMP_PARALLEL:
10451 : 64837 : case OMP_TASK:
10452 : 64837 : case OMP_FOR:
10453 : 64837 : case OMP_SIMD:
10454 : 64837 : case OMP_DISTRIBUTE:
10455 : 64837 : case OMP_TASKLOOP:
10456 : 64837 : case OMP_LOOP:
10457 : 64837 : case OMP_TEAMS:
10458 : 64837 : case OMP_TARGET_DATA:
10459 : 64837 : case OMP_TARGET:
10460 : 64837 : case OMP_SECTIONS:
10461 : 64837 : case OMP_ORDERED:
10462 : 64837 : case OMP_CRITICAL:
10463 : 64837 : case OMP_SINGLE:
10464 : 64837 : case OMP_SCAN:
10465 : 64837 : case OMP_SCOPE:
10466 : 64837 : case OMP_SECTION:
10467 : 64837 : case OMP_MASTER:
10468 : 64837 : case OMP_MASKED:
10469 : 64837 : case OMP_TASKGROUP:
10470 : 64837 : case OMP_TARGET_UPDATE:
10471 : 64837 : case OMP_TARGET_ENTER_DATA:
10472 : 64837 : case OMP_TARGET_EXIT_DATA:
10473 : 64837 : case OMP_ATOMIC:
10474 : 64837 : case OMP_ATOMIC_READ:
10475 : 64837 : case OMP_ATOMIC_CAPTURE_OLD:
10476 : 64837 : case OMP_ATOMIC_CAPTURE_NEW:
10477 : 64837 : case OMP_DEPOBJ:
10478 : 64837 : case OACC_PARALLEL:
10479 : 64837 : case OACC_KERNELS:
10480 : 64837 : case OACC_SERIAL:
10481 : 64837 : case OACC_DATA:
10482 : 64837 : case OACC_HOST_DATA:
10483 : 64837 : case OACC_LOOP:
10484 : 64837 : case OACC_CACHE:
10485 : 64837 : case OACC_DECLARE:
10486 : 64837 : case OACC_ENTER_DATA:
10487 : 64837 : case OACC_EXIT_DATA:
10488 : 64837 : case OACC_UPDATE:
10489 : 64837 : case OMP_ARRAY_SECTION:
10490 : : /* GCC internal stuff. */
10491 : 64837 : case VA_ARG_EXPR:
10492 : 64837 : case TRANSACTION_EXPR:
10493 : 64837 : case AT_ENCODE_EXPR:
10494 : 64837 : fail:
10495 : 64837 : if (flags & tf_error)
10496 : 21 : constexpr_error (loc, fundef_p, "expression %qE is not a constant "
10497 : : "expression", t);
10498 : : return false;
10499 : :
10500 : 479 : case ASM_EXPR:
10501 : 479 : if (flags & tf_error)
10502 : 3 : inline_asm_in_constexpr_error (loc, fundef_p);
10503 : : return false;
10504 : :
10505 : 341372 : case OBJ_TYPE_REF:
10506 : 341372 : if (cxx_dialect >= cxx20)
10507 : : /* In C++20 virtual calls can be constexpr, don't give up yet. */
10508 : : return true;
10509 : 223221 : else if (flags & tf_error)
10510 : 0 : constexpr_error (loc, fundef_p, "virtual functions cannot be "
10511 : : "%<constexpr%> before C++20");
10512 : : return false;
10513 : :
10514 : 3055 : case TYPEID_EXPR:
10515 : : /* In C++20, a typeid expression whose operand is of polymorphic
10516 : : class type can be constexpr. */
10517 : 3055 : {
10518 : 3055 : tree e = TREE_OPERAND (t, 0);
10519 : 3055 : if (cxx_dialect < cxx20
10520 : 509 : && strict
10521 : 509 : && !TYPE_P (e)
10522 : 269 : && !type_dependent_expression_p (e)
10523 : 3061 : && TYPE_POLYMORPHIC_P (TREE_TYPE (e)))
10524 : : {
10525 : 4 : if (flags & tf_error)
10526 : 1 : constexpr_error (loc, fundef_p, "%<typeid%> is not a "
10527 : : "constant expression because %qE is "
10528 : : "of polymorphic type", e);
10529 : 4 : return false;
10530 : : }
10531 : : return true;
10532 : : }
10533 : :
10534 : 25398567 : case POINTER_DIFF_EXPR:
10535 : 25398567 : case MINUS_EXPR:
10536 : 25398567 : want_rval = true;
10537 : 25398567 : goto binary;
10538 : :
10539 : 57550184 : case LT_EXPR:
10540 : 57550184 : case LE_EXPR:
10541 : 57550184 : case GT_EXPR:
10542 : 57550184 : case GE_EXPR:
10543 : 57550184 : case EQ_EXPR:
10544 : 57550184 : case NE_EXPR:
10545 : 57550184 : case SPACESHIP_EXPR:
10546 : 57550184 : want_rval = true;
10547 : 57550184 : goto binary;
10548 : :
10549 : 1257270 : case PREINCREMENT_EXPR:
10550 : 1257270 : case POSTINCREMENT_EXPR:
10551 : 1257270 : case PREDECREMENT_EXPR:
10552 : 1257270 : case POSTDECREMENT_EXPR:
10553 : 1257270 : if (cxx_dialect < cxx14)
10554 : 8775 : goto fail;
10555 : 1248495 : goto unary;
10556 : :
10557 : 929724 : case BIT_NOT_EXPR:
10558 : : /* A destructor. */
10559 : 929724 : if (TYPE_P (TREE_OPERAND (t, 0)))
10560 : : return true;
10561 : : /* fall through. */
10562 : :
10563 : 44376489 : case CONJ_EXPR:
10564 : 44376489 : case SAVE_EXPR:
10565 : 44376489 : case FIX_TRUNC_EXPR:
10566 : 44376489 : case FLOAT_EXPR:
10567 : 44376489 : case NEGATE_EXPR:
10568 : 44376489 : case ABS_EXPR:
10569 : 44376489 : case ABSU_EXPR:
10570 : 44376489 : case TRUTH_NOT_EXPR:
10571 : 44376489 : case FIXED_CONVERT_EXPR:
10572 : 44376489 : case UNARY_PLUS_EXPR:
10573 : 44376489 : case UNARY_LEFT_FOLD_EXPR:
10574 : 44376489 : case UNARY_RIGHT_FOLD_EXPR:
10575 : 44376489 : case VEC_DUPLICATE_EXPR:
10576 : 929724 : unary:
10577 : 44376489 : return RECUR (TREE_OPERAND (t, 0), rval);
10578 : :
10579 : 26570899 : case CAST_EXPR:
10580 : 26570899 : case CONST_CAST_EXPR:
10581 : 26570899 : case STATIC_CAST_EXPR:
10582 : 26570899 : case REINTERPRET_CAST_EXPR:
10583 : 26570899 : case IMPLICIT_CONV_EXPR:
10584 : 26570899 : if (!cast_valid_in_integral_constant_expression_p (TREE_TYPE (t)))
10585 : : /* In C++98, a conversion to non-integral type can't be part of a
10586 : : constant expression. */
10587 : : {
10588 : 281 : if (flags & tf_error)
10589 : 0 : constexpr_error (loc, fundef_p,
10590 : : "cast to non-integral type %qT in a constant "
10591 : 0 : "expression", TREE_TYPE (t));
10592 : 281 : return false;
10593 : : }
10594 : : /* This might be a conversion from a class to a (potentially) literal
10595 : : type. Let's consider it potentially constant since the conversion
10596 : : might be a constexpr user-defined conversion. */
10597 : 26570618 : else if (cxx_dialect >= cxx11
10598 : 26552102 : && (dependent_type_p (TREE_TYPE (t))
10599 : 7373360 : || !COMPLETE_TYPE_P (TREE_TYPE (t))
10600 : 7370596 : || literal_type_p (TREE_TYPE (t)))
10601 : 26544029 : && TREE_OPERAND (t, 0)
10602 : 52906630 : && (TREE_CODE (t) != CAST_EXPR
10603 : 21213060 : || !TREE_CHAIN (TREE_OPERAND (t, 0))))
10604 : : {
10605 : 26317052 : tree from = TREE_OPERAND (t, 0);
10606 : 26317052 : if (TREE_CODE (t) == CAST_EXPR)
10607 : 21194100 : from = TREE_VALUE (from);
10608 : 26317052 : tree type = TREE_TYPE (from);
10609 : : /* If this is a dependent type, it could end up being a class
10610 : : with conversions. */
10611 : 26317052 : if (type == NULL_TREE || WILDCARD_TYPE_P (type))
10612 : : return true;
10613 : : /* Or a non-dependent class which has conversions. */
10614 : 329216 : else if (CLASS_TYPE_P (type)
10615 : 329216 : && (TYPE_HAS_CONVERSION (type) || dependent_scope_p (type)))
10616 : 87420 : return true;
10617 : : }
10618 : :
10619 : 22364037 : return (RECUR (TREE_OPERAND (t, 0),
10620 : 22364037 : !TYPE_REF_P (TREE_TYPE (t))));
10621 : :
10622 : 6441580 : case BIND_EXPR:
10623 : 6441580 : return RECUR (BIND_EXPR_BODY (t), want_rval);
10624 : :
10625 : 34854467 : case CLEANUP_POINT_EXPR:
10626 : 34854467 : case MUST_NOT_THROW_EXPR:
10627 : 34854467 : case TRY_CATCH_EXPR:
10628 : 34854467 : case TRY_BLOCK:
10629 : 34854467 : case EH_SPEC_BLOCK:
10630 : 34854467 : case EXPR_STMT:
10631 : 34854467 : case PAREN_EXPR:
10632 : : /* For convenience. */
10633 : 34854467 : case LOOP_EXPR:
10634 : 34854467 : case EXIT_EXPR:
10635 : 34854467 : return RECUR (TREE_OPERAND (t, 0), want_rval);
10636 : :
10637 : 3797048 : case DECL_EXPR:
10638 : 3797048 : tmp = DECL_EXPR_DECL (t);
10639 : 3736664 : if (VAR_P (tmp) && !DECL_ARTIFICIAL (tmp)
10640 : 6209964 : && (processing_template_decl
10641 : 2164951 : ? !decl_maybe_constant_var_p (tmp)
10642 : 1916986 : : !decl_constant_var_p (tmp)))
10643 : : {
10644 : 1896226 : if (CP_DECL_THREAD_LOCAL_P (tmp) && !DECL_REALLY_EXTERN (tmp))
10645 : : {
10646 : 5 : if (flags & tf_error)
10647 : 2 : constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p,
10648 : : "%qD defined %<thread_local%> in "
10649 : : "%<constexpr%> context", tmp);
10650 : 5 : return false;
10651 : : }
10652 : 1896221 : else if (TREE_STATIC (tmp))
10653 : : {
10654 : 80 : if (flags & tf_error)
10655 : 15 : constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p,
10656 : : "%qD defined %<static%> in %<constexpr%> "
10657 : : "context", tmp);
10658 : 80 : return false;
10659 : : }
10660 : 1896141 : else if (!check_for_uninitialized_const_var
10661 : 1896141 : (tmp, /*constexpr_context_p=*/true, flags))
10662 : : return false;
10663 : : }
10664 : 3793218 : if (VAR_P (tmp))
10665 : 3732834 : return RECUR (DECL_INITIAL (tmp), want_rval);
10666 : : return true;
10667 : :
10668 : 2663 : case TRY_FINALLY_EXPR:
10669 : 2663 : return (RECUR (TREE_OPERAND (t, 0), want_rval)
10670 : 2663 : && RECUR (TREE_OPERAND (t, 1), any));
10671 : :
10672 : 19023720 : case SCOPE_REF:
10673 : 19023720 : return RECUR (TREE_OPERAND (t, 1), want_rval);
10674 : :
10675 : 18991646 : case TARGET_EXPR:
10676 : 18991646 : if (!TARGET_EXPR_DIRECT_INIT_P (t)
10677 : 18956856 : && !TARGET_EXPR_ELIDING_P (t)
10678 : 34346347 : && !literal_type_p (TREE_TYPE (t)))
10679 : : {
10680 : 1239457 : if (flags & tf_error)
10681 : : {
10682 : 20 : auto_diagnostic_group d;
10683 : 20 : if (constexpr_error (loc, fundef_p,
10684 : : "temporary of non-literal type %qT in a "
10685 : 20 : "constant expression", TREE_TYPE (t)))
10686 : 20 : explain_non_literal_class (TREE_TYPE (t));
10687 : 20 : }
10688 : 1239457 : return false;
10689 : : }
10690 : : /* FALLTHRU */
10691 : 33019970 : case INIT_EXPR:
10692 : 33019970 : return RECUR (TREE_OPERAND (t, 1), rval);
10693 : :
10694 : 24969511 : case CONSTRUCTOR:
10695 : 24969511 : {
10696 : 24969511 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
10697 : 24969511 : constructor_elt *ce;
10698 : 86408429 : for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
10699 : 62249704 : if (!RECUR (ce->value, want_rval))
10700 : : return false;
10701 : : return true;
10702 : : }
10703 : :
10704 : 19687509 : case TREE_LIST:
10705 : 19687509 : {
10706 : 19687509 : gcc_assert (TREE_PURPOSE (t) == NULL_TREE
10707 : : || DECL_P (TREE_PURPOSE (t)));
10708 : 19687509 : if (!RECUR (TREE_VALUE (t), want_rval))
10709 : : return false;
10710 : 17399559 : if (TREE_CHAIN (t) == NULL_TREE)
10711 : : return true;
10712 : 26905 : return RECUR (TREE_CHAIN (t), want_rval);
10713 : : }
10714 : :
10715 : 9567322 : case TRUNC_DIV_EXPR:
10716 : 9567322 : case CEIL_DIV_EXPR:
10717 : 9567322 : case FLOOR_DIV_EXPR:
10718 : 9567322 : case ROUND_DIV_EXPR:
10719 : 9567322 : case TRUNC_MOD_EXPR:
10720 : 9567322 : case CEIL_MOD_EXPR:
10721 : 9567322 : case ROUND_MOD_EXPR:
10722 : 9567322 : {
10723 : 9567322 : tree denom = TREE_OPERAND (t, 1);
10724 : 9567322 : if (!RECUR (denom, rval))
10725 : : return false;
10726 : : /* We can't call cxx_eval_outermost_constant_expr on an expression
10727 : : that hasn't been through instantiate_non_dependent_expr yet. */
10728 : 9091377 : if (!processing_template_decl)
10729 : 3896520 : denom = cxx_eval_outermost_constant_expr (denom, true);
10730 : 9091377 : if (integer_zerop (denom))
10731 : : {
10732 : 335 : if (flags & tf_error)
10733 : 35 : constexpr_error (input_location, fundef_p,
10734 : : "division by zero is not a constant expression");
10735 : 335 : return false;
10736 : : }
10737 : : else
10738 : : {
10739 : 9091042 : want_rval = true;
10740 : 9091042 : return RECUR (TREE_OPERAND (t, 0), want_rval);
10741 : : }
10742 : : }
10743 : :
10744 : 4004111 : case COMPOUND_EXPR:
10745 : 4004111 : {
10746 : : /* check_return_expr sometimes wraps a TARGET_EXPR in a
10747 : : COMPOUND_EXPR; don't get confused. */
10748 : 4004111 : tree op0 = TREE_OPERAND (t, 0);
10749 : 4004111 : tree op1 = TREE_OPERAND (t, 1);
10750 : 4004111 : STRIP_NOPS (op1);
10751 : 4004111 : if (TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
10752 : 510112 : return RECUR (op0, want_rval);
10753 : : else
10754 : 3493999 : goto binary;
10755 : : }
10756 : :
10757 : : /* If the first operand is the non-short-circuit constant, look at
10758 : : the second operand; otherwise we only care about the first one for
10759 : : potentiality. */
10760 : 16865688 : case TRUTH_AND_EXPR:
10761 : 16865688 : case TRUTH_ANDIF_EXPR:
10762 : 16865688 : tmp = boolean_true_node;
10763 : 16865688 : goto truth;
10764 : 7239612 : case TRUTH_OR_EXPR:
10765 : 7239612 : case TRUTH_ORIF_EXPR:
10766 : 7239612 : tmp = boolean_false_node;
10767 : 24105300 : truth:
10768 : 24105300 : {
10769 : 24105300 : tree op0 = TREE_OPERAND (t, 0);
10770 : 24105300 : tree op1 = TREE_OPERAND (t, 1);
10771 : 24105300 : if (!RECUR (op0, rval))
10772 : : return false;
10773 : 16541721 : if (!(flags & tf_error) && RECUR (op1, rval))
10774 : : /* When quiet, try to avoid expensive trial evaluation by first
10775 : : checking potentiality of the second operand. */
10776 : : return true;
10777 : 1218212 : if (!processing_template_decl)
10778 : 803648 : op0 = cxx_eval_outermost_constant_expr (op0, true);
10779 : 1218212 : if (tree_int_cst_equal (op0, tmp))
10780 : 5333 : return (flags & tf_error) ? RECUR (op1, rval) : false;
10781 : : else
10782 : : return true;
10783 : : }
10784 : :
10785 : : case PLUS_EXPR:
10786 : : case MULT_EXPR:
10787 : : case POINTER_PLUS_EXPR:
10788 : : case RDIV_EXPR:
10789 : : case EXACT_DIV_EXPR:
10790 : : case MIN_EXPR:
10791 : : case MAX_EXPR:
10792 : : case LSHIFT_EXPR:
10793 : : case RSHIFT_EXPR:
10794 : : case LROTATE_EXPR:
10795 : : case RROTATE_EXPR:
10796 : : case BIT_IOR_EXPR:
10797 : : case BIT_XOR_EXPR:
10798 : : case BIT_AND_EXPR:
10799 : : case TRUTH_XOR_EXPR:
10800 : : case UNORDERED_EXPR:
10801 : : case ORDERED_EXPR:
10802 : : case UNLT_EXPR:
10803 : : case UNLE_EXPR:
10804 : : case UNGT_EXPR:
10805 : : case UNGE_EXPR:
10806 : : case UNEQ_EXPR:
10807 : : case LTGT_EXPR:
10808 : : case RANGE_EXPR:
10809 : : case COMPLEX_EXPR:
10810 : 183917744 : want_rval = true;
10811 : : /* Fall through. */
10812 : 183917744 : case ARRAY_REF:
10813 : 183917744 : case ARRAY_RANGE_REF:
10814 : 183917744 : case MEMBER_REF:
10815 : 183917744 : case DOTSTAR_EXPR:
10816 : 183917744 : case MEM_REF:
10817 : 183917744 : case BINARY_LEFT_FOLD_EXPR:
10818 : 183917744 : case BINARY_RIGHT_FOLD_EXPR:
10819 : 183917744 : binary:
10820 : 384415494 : for (i = 0; i < 2; ++i)
10821 : 289863677 : if (!RECUR (TREE_OPERAND (t, i), want_rval))
10822 : : return false;
10823 : : return true;
10824 : :
10825 : : case VEC_PERM_EXPR:
10826 : 90035 : for (i = 0; i < 3; ++i)
10827 : 90001 : if (!RECUR (TREE_OPERAND (t, i), true))
10828 : : return false;
10829 : : return true;
10830 : :
10831 : 4396974 : case COND_EXPR:
10832 : 4396974 : if (COND_EXPR_IS_VEC_DELETE (t) && cxx_dialect < cxx20)
10833 : : {
10834 : 8 : if (flags & tf_error)
10835 : 2 : constexpr_error (loc, fundef_p, "%<delete[]%> is not a "
10836 : : "constant expression");
10837 : 8 : return false;
10838 : : }
10839 : : /* Fall through. */
10840 : 8006216 : case IF_STMT:
10841 : 8006216 : case VEC_COND_EXPR:
10842 : : /* If the condition is a known constant, we know which of the legs we
10843 : : care about; otherwise we only require that the condition and
10844 : : either of the legs be potentially constant. */
10845 : 8006216 : tmp = TREE_OPERAND (t, 0);
10846 : 8006216 : if (!RECUR (tmp, rval))
10847 : : return false;
10848 : 6659672 : if (!processing_template_decl)
10849 : 5705092 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
10850 : : /* potential_constant_expression* isn't told if it is called for
10851 : : manifestly_const_eval or not, so for consteval if always
10852 : : process both branches as if the condition is not a known
10853 : : constant. */
10854 : 6659672 : if (TREE_CODE (t) != IF_STMT || !IF_STMT_CONSTEVAL_P (t))
10855 : : {
10856 : 6649736 : if (integer_zerop (tmp))
10857 : 1646263 : return RECUR (TREE_OPERAND (t, 2), want_rval);
10858 : 5003473 : else if (TREE_CODE (tmp) == INTEGER_CST)
10859 : 1720393 : return RECUR (TREE_OPERAND (t, 1), want_rval);
10860 : : }
10861 : 3293016 : tmp = *jump_target;
10862 : 3856778 : for (i = 1; i < 3; ++i)
10863 : : {
10864 : 3785315 : tree this_jump_target = tmp;
10865 : 3785315 : if (potential_constant_expression_1 (TREE_OPERAND (t, i),
10866 : : want_rval, strict, now, fundef_p,
10867 : : tf_none, &this_jump_target))
10868 : : {
10869 : 3221553 : if (returns (&this_jump_target))
10870 : 843610 : *jump_target = this_jump_target;
10871 : 2377943 : else if (!returns (jump_target))
10872 : : {
10873 : 2377943 : if (breaks (&this_jump_target)
10874 : 2377943 : || continues (&this_jump_target))
10875 : 58 : *jump_target = this_jump_target;
10876 : 2377943 : if (i == 1)
10877 : : {
10878 : : /* If the then branch is potentially constant, but
10879 : : does not return, check if the else branch
10880 : : couldn't return, break or continue. */
10881 : 1957301 : hash_set<tree> pset;
10882 : 1957301 : check_for_return_continue_data data = { &pset, NULL_TREE,
10883 : 1957301 : NULL_TREE };
10884 : 3914602 : if (tree ret_expr
10885 : 1957301 : = cp_walk_tree (&TREE_OPERAND (t, 2),
10886 : : check_for_return_continue, &data,
10887 : : &pset))
10888 : 15731 : *jump_target = ret_expr;
10889 : 1941570 : else if (*jump_target == NULL_TREE)
10890 : : {
10891 : 1941515 : if (data.continue_stmt)
10892 : 0 : *jump_target = data.continue_stmt;
10893 : 1941515 : else if (data.break_stmt)
10894 : 8 : *jump_target = data.break_stmt;
10895 : : }
10896 : 1957301 : }
10897 : : }
10898 : 3221553 : return true;
10899 : : }
10900 : : }
10901 : 71463 : if (flags & tf_error)
10902 : : {
10903 : 3 : if (TREE_CODE (t) == IF_STMT)
10904 : 3 : constexpr_error (loc, fundef_p, "neither branch of %<if%> is a "
10905 : : "constant expression");
10906 : : else
10907 : 0 : constexpr_error (loc, fundef_p, "expression %qE is not a "
10908 : : "constant expression", t);
10909 : : }
10910 : : return false;
10911 : :
10912 : 901 : case VEC_INIT_EXPR:
10913 : 901 : if (VEC_INIT_EXPR_IS_CONSTEXPR (t))
10914 : : return true;
10915 : 372 : if (flags & tf_error)
10916 : : {
10917 : 3 : if (constexpr_error (loc, fundef_p, "non-constant array "
10918 : : "initialization"))
10919 : 3 : diagnose_non_constexpr_vec_init (t);
10920 : : }
10921 : : return false;
10922 : :
10923 : : case TYPE_DECL:
10924 : : case TAG_DEFN:
10925 : : /* We can see these in statement-expressions. */
10926 : : return true;
10927 : :
10928 : 416316 : case CLEANUP_STMT:
10929 : 416316 : if (!RECUR (CLEANUP_BODY (t), any))
10930 : : return false;
10931 : 415781 : if (!CLEANUP_EH_ONLY (t) && !RECUR (CLEANUP_EXPR (t), any))
10932 : : return false;
10933 : : return true;
10934 : :
10935 : : case EMPTY_CLASS_EXPR:
10936 : : return true;
10937 : :
10938 : 11 : case GOTO_EXPR:
10939 : 11 : {
10940 : 11 : tree *target = &TREE_OPERAND (t, 0);
10941 : : /* Gotos representing break, continue and cdtor return are OK. */
10942 : 11 : if (breaks (target) || continues (target) || returns (target))
10943 : : {
10944 : 3 : *jump_target = *target;
10945 : 3 : return true;
10946 : : }
10947 : 8 : if (flags & tf_error)
10948 : 2 : constexpr_error (loc, fundef_p, "%<goto%> is not a constant "
10949 : : "expression");
10950 : : return false;
10951 : : }
10952 : :
10953 : 48 : case ASSERTION_STMT:
10954 : 48 : case PRECONDITION_STMT:
10955 : 48 : case POSTCONDITION_STMT:
10956 : 48 : if (!checked_contract_p (get_contract_semantic (t)))
10957 : : return true;
10958 : 33 : return RECUR (CONTRACT_CONDITION (t), rval);
10959 : :
10960 : 191 : case LABEL_EXPR:
10961 : 191 : t = LABEL_EXPR_LABEL (t);
10962 : 191 : if (DECL_ARTIFICIAL (t) || cxx_dialect >= cxx23)
10963 : : return true;
10964 : 29 : else if (flags & tf_error)
10965 : 6 : constexpr_error (loc, fundef_p, "label definition in %<constexpr%> "
10966 : : "function only available with %<-std=c++23%> or "
10967 : : "%<-std=gnu++23%>");
10968 : : return false;
10969 : :
10970 : 2409 : case ANNOTATE_EXPR:
10971 : 2409 : return RECUR (TREE_OPERAND (t, 0), rval);
10972 : :
10973 : 16970 : case BIT_CAST_EXPR:
10974 : 16970 : return RECUR (TREE_OPERAND (t, 0), rval);
10975 : :
10976 : : /* Coroutine await, yield and return expressions are not. */
10977 : : case CO_AWAIT_EXPR:
10978 : : case CO_YIELD_EXPR:
10979 : : case CO_RETURN_EXPR:
10980 : : return false;
10981 : :
10982 : : /* Assume a TU-local entity is not constant, we'll error later when
10983 : : instantiating. */
10984 : : case TU_LOCAL_ENTITY:
10985 : : return false;
10986 : :
10987 : 30 : case NONTYPE_ARGUMENT_PACK:
10988 : 30 : {
10989 : 30 : tree args = ARGUMENT_PACK_ARGS (t);
10990 : 30 : int len = TREE_VEC_LENGTH (args);
10991 : 90 : for (int i = 0; i < len; ++i)
10992 : 60 : if (!RECUR (TREE_VEC_ELT (args, i), any))
10993 : : return false;
10994 : : return true;
10995 : : }
10996 : :
10997 : 0 : default:
10998 : 0 : if (objc_non_constant_expr_p (t))
10999 : : return false;
11000 : :
11001 : 0 : sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t)));
11002 : 0 : gcc_unreachable ();
11003 : : return false;
11004 : : }
11005 : : #undef RECUR
11006 : : }
11007 : :
11008 : : bool
11009 : 1022897612 : potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
11010 : : bool fundef_p, tsubst_flags_t flags)
11011 : : {
11012 : 1022897612 : if (flags & tf_error)
11013 : : {
11014 : : /* Check potentiality quietly first, as that could be performed more
11015 : : efficiently in some cases (currently only for TRUTH_*_EXPR). If
11016 : : that fails, replay the check noisily to give errors. */
11017 : 4356327 : flags &= ~tf_error;
11018 : 4356327 : if (potential_constant_expression_1 (t, want_rval, strict, now, fundef_p,
11019 : : flags))
11020 : : return true;
11021 : 768 : flags |= tf_error;
11022 : : }
11023 : :
11024 : 1018542053 : tree target = NULL_TREE;
11025 : 1018542053 : return potential_constant_expression_1 (t, want_rval, strict, now, fundef_p,
11026 : 1018542053 : flags, &target);
11027 : : }
11028 : :
11029 : : /* The main entry point to the above. */
11030 : :
11031 : : bool
11032 : 328823085 : potential_constant_expression (tree t)
11033 : : {
11034 : 328823085 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
11035 : : /*now*/false, /*fundef_p*/false,
11036 : 328823085 : tf_none);
11037 : : }
11038 : :
11039 : : /* As above, but require a constant rvalue. */
11040 : :
11041 : : bool
11042 : 18481055 : potential_rvalue_constant_expression (tree t)
11043 : : {
11044 : 18481055 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
11045 : : /*now*/false, /*fundef_p*/false,
11046 : 18481055 : tf_none);
11047 : : }
11048 : :
11049 : : /* Like above, but complain about non-constant expressions. */
11050 : :
11051 : : bool
11052 : 70 : require_potential_constant_expression (tree t)
11053 : : {
11054 : 70 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
11055 : : /*now*/false, /*fundef_p*/false,
11056 : 70 : tf_warning_or_error);
11057 : : }
11058 : :
11059 : : /* Cross product of the above. */
11060 : :
11061 : : bool
11062 : 72385 : require_potential_rvalue_constant_expression (tree t)
11063 : : {
11064 : 72385 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
11065 : : /*now*/false, /*fundef_p*/false,
11066 : 72385 : tf_warning_or_error);
11067 : : }
11068 : :
11069 : : /* Like require_potential_rvalue_constant_expression, but fundef_p is true. */
11070 : :
11071 : : bool
11072 : 189 : require_potential_rvalue_constant_expression_fncheck (tree t)
11073 : : {
11074 : 189 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
11075 : : /*now*/false, /*fundef_p*/true,
11076 : 189 : tf_warning_or_error);
11077 : : }
11078 : :
11079 : : /* Like above, but don't consider PARM_DECL a potential_constant_expression. */
11080 : :
11081 : : bool
11082 : 411 : require_rvalue_constant_expression (tree t)
11083 : : {
11084 : 411 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
11085 : : /*now*/true, /*fundef_p*/false,
11086 : 411 : tf_warning_or_error);
11087 : : }
11088 : :
11089 : : /* Like potential_constant_expression, but don't consider possible constexpr
11090 : : substitution of the current function. That is, PARM_DECL qualifies under
11091 : : potential_constant_expression, but not here.
11092 : :
11093 : : This is basically what you can check when any actual constant values might
11094 : : be value-dependent. */
11095 : :
11096 : : bool
11097 : 520711977 : is_constant_expression (tree t)
11098 : : {
11099 : 520711977 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
11100 : : /*now*/true, /*fundef_p*/false,
11101 : 520711977 : tf_none);
11102 : : }
11103 : :
11104 : : /* As above, but expect an rvalue. */
11105 : :
11106 : : bool
11107 : 94721356 : is_rvalue_constant_expression (tree t)
11108 : : {
11109 : 94721356 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
11110 : : /*now*/true, /*fundef_p*/false,
11111 : 94721356 : tf_none);
11112 : : }
11113 : :
11114 : : /* Like above, but complain about non-constant expressions. */
11115 : :
11116 : : bool
11117 : 4283272 : require_constant_expression (tree t)
11118 : : {
11119 : 4283272 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
11120 : : /*now*/true, /*fundef_p*/false,
11121 : 4283272 : tf_warning_or_error);
11122 : : }
11123 : :
11124 : : /* Like is_constant_expression, but allow const variables that are not allowed
11125 : : under constexpr rules. */
11126 : :
11127 : : bool
11128 : 51447485 : is_static_init_expression (tree t)
11129 : : {
11130 : 51447485 : return potential_constant_expression_1 (t, /*want_rval*/false,
11131 : : /*strict*/false, /*now*/true,
11132 : 51447485 : /*fundef_p*/false, tf_none);
11133 : : }
11134 : :
11135 : : /* Returns true if T is a potential constant expression that is not
11136 : : instantiation-dependent, and therefore a candidate for constant folding even
11137 : : in a template. */
11138 : :
11139 : : bool
11140 : 506391164 : is_nondependent_constant_expression (tree t)
11141 : : {
11142 : 506391164 : return (!type_unknown_p (t)
11143 : 506391115 : && is_constant_expression (t)
11144 : 911847938 : && !instantiation_dependent_expression_p (t));
11145 : : }
11146 : :
11147 : : /* Returns true if T is a potential static initializer expression that is not
11148 : : instantiation-dependent. */
11149 : :
11150 : : bool
11151 : 51447485 : is_nondependent_static_init_expression (tree t)
11152 : : {
11153 : 51447485 : return (!type_unknown_p (t)
11154 : 51447485 : && is_static_init_expression (t)
11155 : 94687063 : && !instantiation_dependent_expression_p (t));
11156 : : }
11157 : :
11158 : : /* True iff FN is an implicitly constexpr function. */
11159 : :
11160 : : bool
11161 : 117776 : decl_implicit_constexpr_p (tree fn)
11162 : : {
11163 : 117776 : if (!(flag_implicit_constexpr
11164 : 0 : && TREE_CODE (fn) == FUNCTION_DECL
11165 : 0 : && DECL_DECLARED_CONSTEXPR_P (fn)))
11166 : : return false;
11167 : :
11168 : 0 : if (DECL_CLONED_FUNCTION_P (fn))
11169 : 0 : fn = DECL_CLONED_FUNCTION (fn);
11170 : :
11171 : 0 : return (DECL_LANG_SPECIFIC (fn)
11172 : 0 : && DECL_LANG_SPECIFIC (fn)->u.fn.implicit_constexpr);
11173 : : }
11174 : :
11175 : : /* Finalize constexpr processing after parsing. */
11176 : :
11177 : : void
11178 : 92011 : fini_constexpr (void)
11179 : : {
11180 : : /* The contexpr call and fundef copies tables are no longer needed. */
11181 : 92011 : constexpr_call_table = NULL;
11182 : 92011 : fundef_copies_table = NULL;
11183 : 92011 : }
11184 : :
11185 : : #include "gt-cp-constexpr.h"
|